blob: 089fc7940d8bf157c34d5cac9b51916e6c926a8f [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
12#include "qla_devtbl.h"
13
David Miller4e08df32007-04-16 12:37:43 -070014#ifdef CONFIG_SPARC
15#include <asm/prom.h>
David Miller4e08df32007-04-16 12:37:43 -070016#endif
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018/* XXX(hch): this is ugly, but we don't want to pull in exioctl.h */
19#ifndef EXT_IS_LUN_BIT_SET
20#define EXT_IS_LUN_BIT_SET(P,L) \
21 (((P)->mask[L/8] & (0x80 >> (L%8)))?1:0)
22#define EXT_SET_LUN_BIT(P,L) \
23 ((P)->mask[L/8] |= (0x80 >> (L%8)))
24#endif
25
26/*
27* QLogic ISP2x00 Hardware Support Function Prototypes.
28*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070029static int qla2x00_isp_firmware(scsi_qla_host_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030static void qla2x00_resize_request_q(scsi_qla_host_t *);
31static int qla2x00_setup_chip(scsi_qla_host_t *);
32static void qla2x00_init_response_q_entries(scsi_qla_host_t *);
33static int qla2x00_init_rings(scsi_qla_host_t *);
34static int qla2x00_fw_ready(scsi_qla_host_t *);
35static int qla2x00_configure_hba(scsi_qla_host_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036static int qla2x00_configure_loop(scsi_qla_host_t *);
37static int qla2x00_configure_local_loop(scsi_qla_host_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038static int qla2x00_configure_fabric(scsi_qla_host_t *);
39static int qla2x00_find_all_fabric_devs(scsi_qla_host_t *, struct list_head *);
40static int qla2x00_device_resync(scsi_qla_host_t *);
41static int qla2x00_fabric_dev_login(scsi_qla_host_t *, fc_port_t *,
42 uint16_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44static int qla2x00_restart_isp(scsi_qla_host_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Adrian Bunk413975a2006-06-30 02:33:06 -070046static int qla2x00_find_new_loop_id(scsi_qla_host_t *ha, fc_port_t *dev);
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048/****************************************************************************/
49/* QLogic ISP2x00 Hardware Support Functions. */
50/****************************************************************************/
51
52/*
53* qla2x00_initialize_adapter
54* Initialize board.
55*
56* Input:
57* ha = adapter block pointer.
58*
59* Returns:
60* 0 = success
61*/
62int
63qla2x00_initialize_adapter(scsi_qla_host_t *ha)
64{
65 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67 /* Clear adapter flags. */
68 ha->flags.online = 0;
69 ha->flags.reset_active = 0;
70 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
71 atomic_set(&ha->loop_state, LOOP_DOWN);
Andrew Vasquez26032212007-01-29 10:22:23 -080072 ha->device_flags = DFLG_NO_CABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 ha->dpc_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 ha->flags.management_server_logged_in = 0;
75 ha->marker_needed = 0;
76 ha->mbx_flags = 0;
77 ha->isp_abort_cnt = 0;
78 ha->beacon_blink_led = 0;
Andrew Vasquezcca53352005-08-26 19:08:30 -070079 set_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Andrew Vasquez0107109e2005-07-06 10:31:37 -070081 qla_printk(KERN_INFO, ha, "Configuring PCI space...\n");
Andrew Vasquezabbd8872005-07-06 10:30:05 -070082 rval = ha->isp_ops.pci_config(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 if (rval) {
Andrew Vasquez7c98a042007-01-29 10:22:29 -080084 DEBUG2(printk("scsi(%ld): Unable to configure PCI space.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 ha->host_no));
86 return (rval);
87 }
88
Andrew Vasquezabbd8872005-07-06 10:30:05 -070089 ha->isp_ops.reset_chip(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Andrew Vasquez30c47662007-01-29 10:22:21 -080091 ha->isp_ops.get_flash_version(ha, ha->request_ring);
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 qla_printk(KERN_INFO, ha, "Configure NVRAM parameters...\n");
Andrew Vasquez0107109e2005-07-06 10:31:37 -070094
David Miller4e08df32007-04-16 12:37:43 -070095 ha->isp_ops.nvram_config(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Andrew Vasquezd4c760c2006-06-23 16:10:39 -070097 if (ha->flags.disable_serdes) {
98 /* Mask HBA via NVRAM settings? */
99 qla_printk(KERN_INFO, ha, "Masking HBA WWPN "
100 "%02x%02x%02x%02x%02x%02x%02x%02x (via NVRAM).\n",
101 ha->port_name[0], ha->port_name[1],
102 ha->port_name[2], ha->port_name[3],
103 ha->port_name[4], ha->port_name[5],
104 ha->port_name[6], ha->port_name[7]);
105 return QLA_FUNCTION_FAILED;
106 }
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 qla_printk(KERN_INFO, ha, "Verifying loaded RISC code...\n");
109
Andrew Vasquezd19044c2006-11-22 08:22:19 -0800110 if (qla2x00_isp_firmware(ha) != QLA_SUCCESS) {
111 rval = ha->isp_ops.chip_diag(ha);
112 if (rval)
113 return (rval);
114 rval = qla2x00_setup_chip(ha);
115 if (rval)
116 return (rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
Andrew Vasquezd19044c2006-11-22 08:22:19 -0800118 rval = qla2x00_init_rings(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 return (rval);
121}
122
123/**
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700124 * qla2100_pci_config() - Setup ISP21xx PCI configuration registers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 * @ha: HA context
126 *
127 * Returns 0 on success.
128 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700129int
130qla2100_pci_config(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700132 uint16_t w, mwi;
Adam Kropelin27b2f672005-09-16 19:28:20 -0700133 uint32_t d;
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700134 unsigned long flags;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700135 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 pci_set_master(ha->pdev);
138 mwi = 0;
139 if (pci_set_mwi(ha->pdev))
140 mwi = PCI_COMMAND_INVALIDATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
143 w |= mwi | (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
145
146 /* Reset expansion ROM address decode enable */
Adam Kropelin27b2f672005-09-16 19:28:20 -0700147 pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
148 d &= ~PCI_ROM_ADDRESS_ENABLE;
149 pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700151 /* Get PCI bus information. */
152 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez3d716442005-07-06 10:30:26 -0700153 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700154 spin_unlock_irqrestore(&ha->hardware_lock, flags);
155
156 return QLA_SUCCESS;
157}
158
159/**
160 * qla2300_pci_config() - Setup ISP23xx PCI configuration registers.
161 * @ha: HA context
162 *
163 * Returns 0 on success.
164 */
165int
166qla2300_pci_config(scsi_qla_host_t *ha)
167{
168 uint16_t w, mwi;
Adam Kropelin27b2f672005-09-16 19:28:20 -0700169 uint32_t d;
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700170 unsigned long flags = 0;
171 uint32_t cnt;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700172 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700173
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700174 pci_set_master(ha->pdev);
175 mwi = 0;
176 if (pci_set_mwi(ha->pdev))
177 mwi = PCI_COMMAND_INVALIDATE;
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700178
179 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
180 w |= mwi | (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
181
182 if (IS_QLA2322(ha) || IS_QLA6322(ha))
183 w &= ~PCI_COMMAND_INTX_DISABLE;
184
185 /*
186 * If this is a 2300 card and not 2312, reset the
187 * COMMAND_INVALIDATE due to a bug in the 2300. Unfortunately,
188 * the 2310 also reports itself as a 2300 so we need to get the
189 * fb revision level -- a 6 indicates it really is a 2300 and
190 * not a 2310.
191 */
192 if (IS_QLA2300(ha)) {
193 spin_lock_irqsave(&ha->hardware_lock, flags);
194
195 /* Pause RISC. */
Andrew Vasquez3d716442005-07-06 10:30:26 -0700196 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700197 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquez3d716442005-07-06 10:30:26 -0700198 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700199 break;
200
201 udelay(10);
202 }
203
204 /* Select FPM registers. */
Andrew Vasquez3d716442005-07-06 10:30:26 -0700205 WRT_REG_WORD(&reg->ctrl_status, 0x20);
206 RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700207
208 /* Get the fb rev level */
Andrew Vasquez3d716442005-07-06 10:30:26 -0700209 ha->fb_rev = RD_FB_CMD_REG(ha, reg);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700210
211 if (ha->fb_rev == FPM_2300)
212 w &= ~PCI_COMMAND_INVALIDATE;
213
214 /* Deselect FPM registers. */
Andrew Vasquez3d716442005-07-06 10:30:26 -0700215 WRT_REG_WORD(&reg->ctrl_status, 0x0);
216 RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700217
218 /* Release RISC module. */
Andrew Vasquez3d716442005-07-06 10:30:26 -0700219 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700220 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquez3d716442005-07-06 10:30:26 -0700221 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) == 0)
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700222 break;
223
224 udelay(10);
225 }
226
227 spin_unlock_irqrestore(&ha->hardware_lock, flags);
228 }
229 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
230
231 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
232
233 /* Reset expansion ROM address decode enable */
Adam Kropelin27b2f672005-09-16 19:28:20 -0700234 pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
235 d &= ~PCI_ROM_ADDRESS_ENABLE;
236 pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700237
238 /* Get PCI bus information. */
239 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez3d716442005-07-06 10:30:26 -0700240 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700241 spin_unlock_irqrestore(&ha->hardware_lock, flags);
242
243 return QLA_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
246/**
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700247 * qla24xx_pci_config() - Setup ISP24xx PCI configuration registers.
248 * @ha: HA context
249 *
250 * Returns 0 on success.
251 */
252int
253qla24xx_pci_config(scsi_qla_host_t *ha)
254{
255 uint16_t w, mwi;
Adam Kropelin27b2f672005-09-16 19:28:20 -0700256 uint32_t d;
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700257 unsigned long flags = 0;
258 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
259 int pcix_cmd_reg, pcie_dctl_reg;
260
261 pci_set_master(ha->pdev);
262 mwi = 0;
263 if (pci_set_mwi(ha->pdev))
264 mwi = PCI_COMMAND_INVALIDATE;
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700265
266 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
267 w |= mwi | (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
268 w &= ~PCI_COMMAND_INTX_DISABLE;
269 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
270
271 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
272
273 /* PCI-X -- adjust Maximum Memory Read Byte Count (2048). */
274 pcix_cmd_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_PCIX);
275 if (pcix_cmd_reg) {
276 uint16_t pcix_cmd;
277
278 pcix_cmd_reg += PCI_X_CMD;
279 pci_read_config_word(ha->pdev, pcix_cmd_reg, &pcix_cmd);
280 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
281 pcix_cmd |= 0x0008;
282 pci_write_config_word(ha->pdev, pcix_cmd_reg, pcix_cmd);
283 }
284
285 /* PCIe -- adjust Maximum Read Request Size (2048). */
286 pcie_dctl_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_EXP);
287 if (pcie_dctl_reg) {
288 uint16_t pcie_dctl;
289
290 pcie_dctl_reg += PCI_EXP_DEVCTL;
291 pci_read_config_word(ha->pdev, pcie_dctl_reg, &pcie_dctl);
292 pcie_dctl &= ~PCI_EXP_DEVCTL_READRQ;
293 pcie_dctl |= 0x4000;
294 pci_write_config_word(ha->pdev, pcie_dctl_reg, pcie_dctl);
295 }
296
297 /* Reset expansion ROM address decode enable */
Adam Kropelin27b2f672005-09-16 19:28:20 -0700298 pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
299 d &= ~PCI_ROM_ADDRESS_ENABLE;
300 pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700301
Andrew Vasqueza8488ab2007-01-29 10:22:19 -0800302 pci_read_config_word(ha->pdev, PCI_REVISION_ID, &ha->chip_revision);
303
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700304 /* Get PCI bus information. */
305 spin_lock_irqsave(&ha->hardware_lock, flags);
306 ha->pci_attr = RD_REG_DWORD(&reg->ctrl_status);
307 spin_unlock_irqrestore(&ha->hardware_lock, flags);
308
309 return QLA_SUCCESS;
310}
311
312/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 * qla2x00_isp_firmware() - Choose firmware image.
314 * @ha: HA context
315 *
316 * Returns 0 on success.
317 */
318static int
319qla2x00_isp_firmware(scsi_qla_host_t *ha)
320{
321 int rval;
322
323 /* Assume loading risc code */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700324 rval = QLA_FUNCTION_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 if (ha->flags.disable_risc_code_load) {
327 DEBUG2(printk("scsi(%ld): RISC CODE NOT loaded\n",
328 ha->host_no));
329 qla_printk(KERN_INFO, ha, "RISC CODE NOT loaded\n");
330
331 /* Verify checksum of loaded RISC code. */
Andrew Vasquez441d1072006-05-17 15:09:34 -0700332 rval = qla2x00_verify_checksum(ha, ha->fw_srisc_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
334
335 if (rval) {
336 DEBUG2_3(printk("scsi(%ld): **** Load RISC code ****\n",
337 ha->host_no));
338 }
339
340 return (rval);
341}
342
343/**
344 * qla2x00_reset_chip() - Reset ISP chip.
345 * @ha: HA context
346 *
347 * Returns 0 on success.
348 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700349void
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700350qla2x00_reset_chip(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
352 unsigned long flags = 0;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700353 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 uint32_t cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 uint16_t cmd;
356
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700357 ha->isp_ops.disable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 spin_lock_irqsave(&ha->hardware_lock, flags);
360
361 /* Turn off master enable */
362 cmd = 0;
363 pci_read_config_word(ha->pdev, PCI_COMMAND, &cmd);
364 cmd &= ~PCI_COMMAND_MASTER;
365 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
366
367 if (!IS_QLA2100(ha)) {
368 /* Pause RISC. */
369 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
370 if (IS_QLA2200(ha) || IS_QLA2300(ha)) {
371 for (cnt = 0; cnt < 30000; cnt++) {
372 if ((RD_REG_WORD(&reg->hccr) &
373 HCCR_RISC_PAUSE) != 0)
374 break;
375 udelay(100);
376 }
377 } else {
378 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
379 udelay(10);
380 }
381
382 /* Select FPM registers. */
383 WRT_REG_WORD(&reg->ctrl_status, 0x20);
384 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
385
386 /* FPM Soft Reset. */
387 WRT_REG_WORD(&reg->fpm_diag_config, 0x100);
388 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
389
390 /* Toggle Fpm Reset. */
391 if (!IS_QLA2200(ha)) {
392 WRT_REG_WORD(&reg->fpm_diag_config, 0x0);
393 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
394 }
395
396 /* Select frame buffer registers. */
397 WRT_REG_WORD(&reg->ctrl_status, 0x10);
398 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
399
400 /* Reset frame buffer FIFOs. */
401 if (IS_QLA2200(ha)) {
402 WRT_FB_CMD_REG(ha, reg, 0xa000);
403 RD_FB_CMD_REG(ha, reg); /* PCI Posting. */
404 } else {
405 WRT_FB_CMD_REG(ha, reg, 0x00fc);
406
407 /* Read back fb_cmd until zero or 3 seconds max */
408 for (cnt = 0; cnt < 3000; cnt++) {
409 if ((RD_FB_CMD_REG(ha, reg) & 0xff) == 0)
410 break;
411 udelay(100);
412 }
413 }
414
415 /* Select RISC module registers. */
416 WRT_REG_WORD(&reg->ctrl_status, 0);
417 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
418
419 /* Reset RISC processor. */
420 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
421 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
422
423 /* Release RISC processor. */
424 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
425 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
426 }
427
428 WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
429 WRT_REG_WORD(&reg->hccr, HCCR_CLR_HOST_INT);
430
431 /* Reset ISP chip. */
432 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
433
434 /* Wait for RISC to recover from reset. */
435 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
436 /*
437 * It is necessary to for a delay here since the card doesn't
438 * respond to PCI reads during a reset. On some architectures
439 * this will result in an MCA.
440 */
441 udelay(20);
442 for (cnt = 30000; cnt; cnt--) {
443 if ((RD_REG_WORD(&reg->ctrl_status) &
444 CSR_ISP_SOFT_RESET) == 0)
445 break;
446 udelay(100);
447 }
448 } else
449 udelay(10);
450
451 /* Reset RISC processor. */
452 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
453
454 WRT_REG_WORD(&reg->semaphore, 0);
455
456 /* Release RISC processor. */
457 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
458 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
459
460 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
461 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquezffb39f02006-05-17 15:09:06 -0700462 if (RD_MAILBOX_REG(ha, reg, 0) != MBS_BUSY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 udelay(100);
466 }
467 } else
468 udelay(100);
469
470 /* Turn on master enable */
471 cmd |= PCI_COMMAND_MASTER;
472 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
473
474 /* Disable RISC pause on FPM parity error. */
475 if (!IS_QLA2100(ha)) {
476 WRT_REG_WORD(&reg->hccr, HCCR_DISABLE_PARITY_PAUSE);
477 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
478 }
479
480 spin_unlock_irqrestore(&ha->hardware_lock, flags);
481}
482
483/**
Andrew Vasquez88c26662005-07-08 17:59:26 -0700484 * qla24xx_reset_risc() - Perform full reset of ISP24xx RISC.
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700485 * @ha: HA context
486 *
487 * Returns 0 on success.
488 */
Andrew Vasquez88c26662005-07-08 17:59:26 -0700489static inline void
490qla24xx_reset_risc(scsi_qla_host_t *ha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700491{
492 unsigned long flags = 0;
493 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
494 uint32_t cnt, d2;
Andrew Vasquez335a1cc2005-11-08 14:37:48 -0800495 uint16_t wd;
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700496
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700497 spin_lock_irqsave(&ha->hardware_lock, flags);
498
499 /* Reset RISC. */
500 WRT_REG_DWORD(&reg->ctrl_status, CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
501 for (cnt = 0; cnt < 30000; cnt++) {
502 if ((RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE) == 0)
503 break;
504
505 udelay(10);
506 }
507
508 WRT_REG_DWORD(&reg->ctrl_status,
509 CSRX_ISP_SOFT_RESET|CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
Andrew Vasquez335a1cc2005-11-08 14:37:48 -0800510 pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
Andrew Vasquez88c26662005-07-08 17:59:26 -0700511
Andrew Vasquez335a1cc2005-11-08 14:37:48 -0800512 udelay(100);
Andrew Vasquez88c26662005-07-08 17:59:26 -0700513 /* Wait for firmware to complete NVRAM accesses. */
Andrew Vasquez88c26662005-07-08 17:59:26 -0700514 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
515 for (cnt = 10000 ; cnt && d2; cnt--) {
516 udelay(5);
517 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
518 barrier();
519 }
520
Andrew Vasquez335a1cc2005-11-08 14:37:48 -0800521 /* Wait for soft-reset to complete. */
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700522 d2 = RD_REG_DWORD(&reg->ctrl_status);
523 for (cnt = 6000000 ; cnt && (d2 & CSRX_ISP_SOFT_RESET); cnt--) {
524 udelay(5);
525 d2 = RD_REG_DWORD(&reg->ctrl_status);
526 barrier();
527 }
528
529 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
530 RD_REG_DWORD(&reg->hccr);
531
532 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
533 RD_REG_DWORD(&reg->hccr);
534
535 WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_RESET);
536 RD_REG_DWORD(&reg->hccr);
537
538 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
539 for (cnt = 6000000 ; cnt && d2; cnt--) {
540 udelay(5);
541 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
542 barrier();
543 }
544
545 spin_unlock_irqrestore(&ha->hardware_lock, flags);
546}
547
548/**
Andrew Vasquez88c26662005-07-08 17:59:26 -0700549 * qla24xx_reset_chip() - Reset ISP24xx chip.
550 * @ha: HA context
551 *
552 * Returns 0 on success.
553 */
554void
555qla24xx_reset_chip(scsi_qla_host_t *ha)
556{
557 ha->isp_ops.disable_intrs(ha);
558
559 /* Perform RISC reset. */
560 qla24xx_reset_risc(ha);
561}
562
563/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 * qla2x00_chip_diag() - Test chip for proper operation.
565 * @ha: HA context
566 *
567 * Returns 0 on success.
568 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700569int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570qla2x00_chip_diag(scsi_qla_host_t *ha)
571{
572 int rval;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700573 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 unsigned long flags = 0;
575 uint16_t data;
576 uint32_t cnt;
577 uint16_t mb[5];
578
579 /* Assume a failed state */
580 rval = QLA_FUNCTION_FAILED;
581
582 DEBUG3(printk("scsi(%ld): Testing device at %lx.\n",
583 ha->host_no, (u_long)&reg->flash_address));
584
585 spin_lock_irqsave(&ha->hardware_lock, flags);
586
587 /* Reset ISP chip. */
588 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
589
590 /*
591 * We need to have a delay here since the card will not respond while
592 * in reset causing an MCA on some architectures.
593 */
594 udelay(20);
595 data = qla2x00_debounce_register(&reg->ctrl_status);
596 for (cnt = 6000000 ; cnt && (data & CSR_ISP_SOFT_RESET); cnt--) {
597 udelay(5);
598 data = RD_REG_WORD(&reg->ctrl_status);
599 barrier();
600 }
601
602 if (!cnt)
603 goto chip_diag_failed;
604
605 DEBUG3(printk("scsi(%ld): Reset register cleared by chip reset\n",
606 ha->host_no));
607
608 /* Reset RISC processor. */
609 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
610 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
611
612 /* Workaround for QLA2312 PCI parity error */
613 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
614 data = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 0));
615 for (cnt = 6000000; cnt && (data == MBS_BUSY); cnt--) {
616 udelay(5);
617 data = RD_MAILBOX_REG(ha, reg, 0);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700618 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
620 } else
621 udelay(10);
622
623 if (!cnt)
624 goto chip_diag_failed;
625
626 /* Check product ID of chip */
627 DEBUG3(printk("scsi(%ld): Checking product ID of chip\n", ha->host_no));
628
629 mb[1] = RD_MAILBOX_REG(ha, reg, 1);
630 mb[2] = RD_MAILBOX_REG(ha, reg, 2);
631 mb[3] = RD_MAILBOX_REG(ha, reg, 3);
632 mb[4] = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 4));
633 if (mb[1] != PROD_ID_1 || (mb[2] != PROD_ID_2 && mb[2] != PROD_ID_2a) ||
634 mb[3] != PROD_ID_3) {
635 qla_printk(KERN_WARNING, ha,
636 "Wrong product ID = 0x%x,0x%x,0x%x\n", mb[1], mb[2], mb[3]);
637
638 goto chip_diag_failed;
639 }
640 ha->product_id[0] = mb[1];
641 ha->product_id[1] = mb[2];
642 ha->product_id[2] = mb[3];
643 ha->product_id[3] = mb[4];
644
645 /* Adjust fw RISC transfer size */
646 if (ha->request_q_length > 1024)
647 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
648 else
649 ha->fw_transfer_size = REQUEST_ENTRY_SIZE *
650 ha->request_q_length;
651
652 if (IS_QLA2200(ha) &&
653 RD_MAILBOX_REG(ha, reg, 7) == QLA2200A_RISC_ROM_VER) {
654 /* Limit firmware transfer size with a 2200A */
655 DEBUG3(printk("scsi(%ld): Found QLA2200A chip.\n",
656 ha->host_no));
657
andrew.vasquez@qlogic.comea5b6382006-03-09 14:27:08 -0800658 ha->device_type |= DT_ISP2200A;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 ha->fw_transfer_size = 128;
660 }
661
662 /* Wrap Incoming Mailboxes Test. */
663 spin_unlock_irqrestore(&ha->hardware_lock, flags);
664
665 DEBUG3(printk("scsi(%ld): Checking mailboxes.\n", ha->host_no));
666 rval = qla2x00_mbx_reg_test(ha);
667 if (rval) {
668 DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
669 ha->host_no));
670 qla_printk(KERN_WARNING, ha,
671 "Failed mailbox send register test\n");
672 }
673 else {
674 /* Flag a successful rval */
675 rval = QLA_SUCCESS;
676 }
677 spin_lock_irqsave(&ha->hardware_lock, flags);
678
679chip_diag_failed:
680 if (rval)
681 DEBUG2_3(printk("scsi(%ld): Chip diagnostics **** FAILED "
682 "****\n", ha->host_no));
683
684 spin_unlock_irqrestore(&ha->hardware_lock, flags);
685
686 return (rval);
687}
688
689/**
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700690 * qla24xx_chip_diag() - Test ISP24xx for proper operation.
691 * @ha: HA context
692 *
693 * Returns 0 on success.
694 */
695int
696qla24xx_chip_diag(scsi_qla_host_t *ha)
697{
698 int rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700699
Andrew Vasquez88c26662005-07-08 17:59:26 -0700700 /* Perform RISC reset. */
701 qla24xx_reset_risc(ha);
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700702
703 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
704
705 rval = qla2x00_mbx_reg_test(ha);
706 if (rval) {
707 DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
708 ha->host_no));
709 qla_printk(KERN_WARNING, ha,
710 "Failed mailbox send register test\n");
711 } else {
712 /* Flag a successful rval */
713 rval = QLA_SUCCESS;
714 }
715
716 return rval;
717}
718
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700719void
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700720qla2x00_alloc_fw_dump(scsi_qla_host_t *ha)
721{
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700722 int rval;
723 uint32_t dump_size, fixed_size, mem_size, req_q_size, rsp_q_size,
724 eft_size;
725 dma_addr_t eft_dma;
726 void *eft;
Andrew Vasquezd4e3e042006-05-17 15:09:50 -0700727
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700728 if (ha->fw_dump) {
729 qla_printk(KERN_WARNING, ha,
730 "Firmware dump previously allocated.\n");
731 return;
Andrew Vasquezd4e3e042006-05-17 15:09:50 -0700732 }
733
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700734 ha->fw_dumped = 0;
735 fixed_size = mem_size = eft_size = 0;
736 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
737 fixed_size = sizeof(struct qla2100_fw_dump);
738 } else if (IS_QLA23XX(ha)) {
739 fixed_size = offsetof(struct qla2300_fw_dump, data_ram);
740 mem_size = (ha->fw_memory_size - 0x11000 + 1) *
741 sizeof(uint16_t);
742 } else if (IS_QLA24XX(ha) || IS_QLA54XX(ha)) {
743 fixed_size = offsetof(struct qla24xx_fw_dump, ext_mem);
744 mem_size = (ha->fw_memory_size - 0x100000 + 1) *
745 sizeof(uint32_t);
746
747 /* Allocate memory for Extended Trace Buffer. */
748 eft = dma_alloc_coherent(&ha->pdev->dev, EFT_SIZE, &eft_dma,
749 GFP_KERNEL);
750 if (!eft) {
751 qla_printk(KERN_WARNING, ha, "Unable to allocate "
752 "(%d KB) for EFT.\n", EFT_SIZE / 1024);
753 goto cont_alloc;
754 }
755
756 rval = qla2x00_trace_control(ha, TC_ENABLE, eft_dma,
757 EFT_NUM_BUFFERS);
758 if (rval) {
759 qla_printk(KERN_WARNING, ha, "Unable to initialize "
760 "EFT (%d).\n", rval);
761 dma_free_coherent(&ha->pdev->dev, EFT_SIZE, eft,
762 eft_dma);
763 goto cont_alloc;
764 }
765
766 qla_printk(KERN_INFO, ha, "Allocated (%d KB) for EFT...\n",
767 EFT_SIZE / 1024);
768
769 eft_size = EFT_SIZE;
770 memset(eft, 0, eft_size);
771 ha->eft_dma = eft_dma;
772 ha->eft = eft;
773 }
774cont_alloc:
775 req_q_size = ha->request_q_length * sizeof(request_t);
776 rsp_q_size = ha->response_q_length * sizeof(response_t);
777
778 dump_size = offsetof(struct qla2xxx_fw_dump, isp);
779 dump_size += fixed_size + mem_size + req_q_size + rsp_q_size +
780 eft_size;
781
Andrew Vasquezd4e3e042006-05-17 15:09:50 -0700782 ha->fw_dump = vmalloc(dump_size);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700783 if (!ha->fw_dump) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700784 qla_printk(KERN_WARNING, ha, "Unable to allocate (%d KB) for "
Andrew Vasquezd4e3e042006-05-17 15:09:50 -0700785 "firmware dump!!!\n", dump_size / 1024);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700786
787 if (ha->eft) {
788 dma_free_coherent(&ha->pdev->dev, eft_size, ha->eft,
789 ha->eft_dma);
790 ha->eft = NULL;
791 ha->eft_dma = 0;
792 }
793 return;
794 }
795
796 qla_printk(KERN_INFO, ha, "Allocated (%d KB) for firmware dump...\n",
797 dump_size / 1024);
798
799 ha->fw_dump_len = dump_size;
800 ha->fw_dump->signature[0] = 'Q';
801 ha->fw_dump->signature[1] = 'L';
802 ha->fw_dump->signature[2] = 'G';
803 ha->fw_dump->signature[3] = 'C';
804 ha->fw_dump->version = __constant_htonl(1);
805
806 ha->fw_dump->fixed_size = htonl(fixed_size);
807 ha->fw_dump->mem_size = htonl(mem_size);
808 ha->fw_dump->req_q_size = htonl(req_q_size);
809 ha->fw_dump->rsp_q_size = htonl(rsp_q_size);
810
811 ha->fw_dump->eft_size = htonl(eft_size);
812 ha->fw_dump->eft_addr_l = htonl(LSD(ha->eft_dma));
813 ha->fw_dump->eft_addr_h = htonl(MSD(ha->eft_dma));
814
815 ha->fw_dump->header_size =
816 htonl(offsetof(struct qla2xxx_fw_dump, isp));
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700817}
818
819/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 * qla2x00_resize_request_q() - Resize request queue given available ISP memory.
821 * @ha: HA context
822 *
823 * Returns 0 on success.
824 */
825static void
826qla2x00_resize_request_q(scsi_qla_host_t *ha)
827{
828 int rval;
829 uint16_t fw_iocb_cnt = 0;
830 uint16_t request_q_length = REQUEST_ENTRY_CNT_2XXX_EXT_MEM;
831 dma_addr_t request_dma;
832 request_t *request_ring;
833
834 /* Valid only on recent ISPs. */
835 if (IS_QLA2100(ha) || IS_QLA2200(ha))
836 return;
837
838 /* Retrieve IOCB counts available to the firmware. */
839 rval = qla2x00_get_resource_cnts(ha, NULL, NULL, NULL, &fw_iocb_cnt);
840 if (rval)
841 return;
842 /* No point in continuing if current settings are sufficient. */
843 if (fw_iocb_cnt < 1024)
844 return;
845 if (ha->request_q_length >= request_q_length)
846 return;
847
848 /* Attempt to claim larger area for request queue. */
849 request_ring = dma_alloc_coherent(&ha->pdev->dev,
850 (request_q_length + 1) * sizeof(request_t), &request_dma,
851 GFP_KERNEL);
852 if (request_ring == NULL)
853 return;
854
855 /* Resize successful, report extensions. */
856 qla_printk(KERN_INFO, ha, "Extended memory detected (%d KB)...\n",
857 (ha->fw_memory_size + 1) / 1024);
858 qla_printk(KERN_INFO, ha, "Resizing request queue depth "
859 "(%d -> %d)...\n", ha->request_q_length, request_q_length);
860
861 /* Clear old allocations. */
862 dma_free_coherent(&ha->pdev->dev,
863 (ha->request_q_length + 1) * sizeof(request_t), ha->request_ring,
864 ha->request_dma);
865
866 /* Begin using larger queue. */
867 ha->request_q_length = request_q_length;
868 ha->request_ring = request_ring;
869 ha->request_dma = request_dma;
870}
871
872/**
873 * qla2x00_setup_chip() - Load and start RISC firmware.
874 * @ha: HA context
875 *
876 * Returns 0 on success.
877 */
878static int
879qla2x00_setup_chip(scsi_qla_host_t *ha)
880{
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700881 int rval;
882 uint32_t srisc_address = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884 /* Load firmware sequences */
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700885 rval = ha->isp_ops.load_risc(ha, &srisc_address);
886 if (rval == QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 DEBUG(printk("scsi(%ld): Verifying Checksum of loaded RISC "
888 "code.\n", ha->host_no));
889
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700890 rval = qla2x00_verify_checksum(ha, srisc_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 if (rval == QLA_SUCCESS) {
892 /* Start firmware execution. */
893 DEBUG(printk("scsi(%ld): Checksum OK, start "
894 "firmware.\n", ha->host_no));
895
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700896 rval = qla2x00_execute_fw(ha, srisc_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 /* Retrieve firmware information. */
898 if (rval == QLA_SUCCESS && ha->fw_major_version == 0) {
899 qla2x00_get_fw_version(ha,
900 &ha->fw_major_version,
901 &ha->fw_minor_version,
902 &ha->fw_subminor_version,
903 &ha->fw_attributes, &ha->fw_memory_size);
904 qla2x00_resize_request_q(ha);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700905
906 if (ql2xallocfwdump)
907 qla2x00_alloc_fw_dump(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 }
909 } else {
910 DEBUG2(printk(KERN_INFO
911 "scsi(%ld): ISP Firmware failed checksum.\n",
912 ha->host_no));
913 }
914 }
915
916 if (rval) {
917 DEBUG2_3(printk("scsi(%ld): Setup chip **** FAILED ****.\n",
918 ha->host_no));
919 }
920
921 return (rval);
922}
923
924/**
925 * qla2x00_init_response_q_entries() - Initializes response queue entries.
926 * @ha: HA context
927 *
928 * Beginning of request ring has initialization control block already built
929 * by nvram config routine.
930 *
931 * Returns 0 on success.
932 */
933static void
934qla2x00_init_response_q_entries(scsi_qla_host_t *ha)
935{
936 uint16_t cnt;
937 response_t *pkt;
938
939 pkt = ha->response_ring_ptr;
940 for (cnt = 0; cnt < ha->response_q_length; cnt++) {
941 pkt->signature = RESPONSE_PROCESSED;
942 pkt++;
943 }
944
945}
946
947/**
948 * qla2x00_update_fw_options() - Read and process firmware options.
949 * @ha: HA context
950 *
951 * Returns 0 on success.
952 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700953void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954qla2x00_update_fw_options(scsi_qla_host_t *ha)
955{
956 uint16_t swing, emphasis, tx_sens, rx_sens;
957
958 memset(ha->fw_options, 0, sizeof(ha->fw_options));
959 qla2x00_get_fw_options(ha, ha->fw_options);
960
961 if (IS_QLA2100(ha) || IS_QLA2200(ha))
962 return;
963
964 /* Serial Link options. */
965 DEBUG3(printk("scsi(%ld): Serial link options:\n",
966 ha->host_no));
967 DEBUG3(qla2x00_dump_buffer((uint8_t *)&ha->fw_seriallink_options,
968 sizeof(ha->fw_seriallink_options)));
969
970 ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
971 if (ha->fw_seriallink_options[3] & BIT_2) {
972 ha->fw_options[1] |= FO1_SET_EMPHASIS_SWING;
973
974 /* 1G settings */
975 swing = ha->fw_seriallink_options[2] & (BIT_2 | BIT_1 | BIT_0);
976 emphasis = (ha->fw_seriallink_options[2] &
977 (BIT_4 | BIT_3)) >> 3;
978 tx_sens = ha->fw_seriallink_options[0] &
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700979 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 rx_sens = (ha->fw_seriallink_options[0] &
981 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
982 ha->fw_options[10] = (emphasis << 14) | (swing << 8);
983 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
984 if (rx_sens == 0x0)
985 rx_sens = 0x3;
986 ha->fw_options[10] |= (tx_sens << 4) | rx_sens;
987 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
988 ha->fw_options[10] |= BIT_5 |
989 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
990 (tx_sens & (BIT_1 | BIT_0));
991
992 /* 2G settings */
993 swing = (ha->fw_seriallink_options[2] &
994 (BIT_7 | BIT_6 | BIT_5)) >> 5;
995 emphasis = ha->fw_seriallink_options[3] & (BIT_1 | BIT_0);
996 tx_sens = ha->fw_seriallink_options[1] &
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700997 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 rx_sens = (ha->fw_seriallink_options[1] &
999 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
1000 ha->fw_options[11] = (emphasis << 14) | (swing << 8);
1001 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
1002 if (rx_sens == 0x0)
1003 rx_sens = 0x3;
1004 ha->fw_options[11] |= (tx_sens << 4) | rx_sens;
1005 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
1006 ha->fw_options[11] |= BIT_5 |
1007 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
1008 (tx_sens & (BIT_1 | BIT_0));
1009 }
1010
1011 /* FCP2 options. */
1012 /* Return command IOCBs without waiting for an ABTS to complete. */
1013 ha->fw_options[3] |= BIT_13;
1014
1015 /* LED scheme. */
1016 if (ha->flags.enable_led_scheme)
1017 ha->fw_options[2] |= BIT_12;
1018
andrew.vasquez@qlogic.com48c02fd2006-03-09 14:27:18 -08001019 /* Detect ISP6312. */
1020 if (IS_QLA6312(ha))
1021 ha->fw_options[2] |= BIT_13;
1022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 /* Update firmware options. */
1024 qla2x00_set_fw_options(ha, ha->fw_options);
1025}
1026
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001027void
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001028qla24xx_update_fw_options(scsi_qla_host_t *ha)
1029{
1030 int rval;
1031
1032 /* Update Serial Link options. */
andrew.vasquez@qlogic.comf94097e2006-01-13 17:05:32 -08001033 if ((le16_to_cpu(ha->fw_seriallink_options24[0]) & BIT_0) == 0)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001034 return;
1035
andrew.vasquez@qlogic.comf94097e2006-01-13 17:05:32 -08001036 rval = qla2x00_set_serdes_params(ha,
1037 le16_to_cpu(ha->fw_seriallink_options24[1]),
1038 le16_to_cpu(ha->fw_seriallink_options24[2]),
1039 le16_to_cpu(ha->fw_seriallink_options24[3]));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001040 if (rval != QLA_SUCCESS) {
1041 qla_printk(KERN_WARNING, ha,
1042 "Unable to update Serial Link options (%x).\n", rval);
1043 }
1044}
1045
1046void
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001047qla2x00_config_rings(struct scsi_qla_host *ha)
1048{
Andrew Vasquez3d716442005-07-06 10:30:26 -07001049 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001050
1051 /* Setup ring parameters in initialization control block. */
1052 ha->init_cb->request_q_outpointer = __constant_cpu_to_le16(0);
1053 ha->init_cb->response_q_inpointer = __constant_cpu_to_le16(0);
1054 ha->init_cb->request_q_length = cpu_to_le16(ha->request_q_length);
1055 ha->init_cb->response_q_length = cpu_to_le16(ha->response_q_length);
1056 ha->init_cb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
1057 ha->init_cb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
1058 ha->init_cb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
1059 ha->init_cb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
1060
1061 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), 0);
1062 WRT_REG_WORD(ISP_REQ_Q_OUT(ha, reg), 0);
1063 WRT_REG_WORD(ISP_RSP_Q_IN(ha, reg), 0);
1064 WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), 0);
1065 RD_REG_WORD(ISP_RSP_Q_OUT(ha, reg)); /* PCI Posting. */
1066}
1067
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001068void
1069qla24xx_config_rings(struct scsi_qla_host *ha)
1070{
1071 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1072 struct init_cb_24xx *icb;
1073
1074 /* Setup ring parameters in initialization control block. */
1075 icb = (struct init_cb_24xx *)ha->init_cb;
1076 icb->request_q_outpointer = __constant_cpu_to_le16(0);
1077 icb->response_q_inpointer = __constant_cpu_to_le16(0);
1078 icb->request_q_length = cpu_to_le16(ha->request_q_length);
1079 icb->response_q_length = cpu_to_le16(ha->response_q_length);
1080 icb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
1081 icb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
1082 icb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
1083 icb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
1084
1085 WRT_REG_DWORD(&reg->req_q_in, 0);
1086 WRT_REG_DWORD(&reg->req_q_out, 0);
1087 WRT_REG_DWORD(&reg->rsp_q_in, 0);
1088 WRT_REG_DWORD(&reg->rsp_q_out, 0);
1089 RD_REG_DWORD(&reg->rsp_q_out);
1090}
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092/**
1093 * qla2x00_init_rings() - Initializes firmware.
1094 * @ha: HA context
1095 *
1096 * Beginning of request ring has initialization control block already built
1097 * by nvram config routine.
1098 *
1099 * Returns 0 on success.
1100 */
1101static int
1102qla2x00_init_rings(scsi_qla_host_t *ha)
1103{
1104 int rval;
1105 unsigned long flags = 0;
1106 int cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108 spin_lock_irqsave(&ha->hardware_lock, flags);
1109
1110 /* Clear outstanding commands array. */
1111 for (cnt = 0; cnt < MAX_OUTSTANDING_COMMANDS; cnt++)
1112 ha->outstanding_cmds[cnt] = NULL;
1113
1114 ha->current_outstanding_cmd = 0;
1115
1116 /* Clear RSCN queue. */
1117 ha->rscn_in_ptr = 0;
1118 ha->rscn_out_ptr = 0;
1119
1120 /* Initialize firmware. */
1121 ha->request_ring_ptr = ha->request_ring;
1122 ha->req_ring_index = 0;
1123 ha->req_q_cnt = ha->request_q_length;
1124 ha->response_ring_ptr = ha->response_ring;
1125 ha->rsp_ring_index = 0;
1126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 /* Initialize response queue entries */
1128 qla2x00_init_response_q_entries(ha);
1129
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001130 ha->isp_ops.config_rings(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1133
1134 /* Update any ISP specific firmware options before initialization. */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001135 ha->isp_ops.update_fw_options(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
1137 DEBUG(printk("scsi(%ld): Issue init firmware.\n", ha->host_no));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001138 rval = qla2x00_init_firmware(ha, ha->init_cb_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (rval) {
1140 DEBUG2_3(printk("scsi(%ld): Init firmware **** FAILED ****.\n",
1141 ha->host_no));
1142 } else {
1143 DEBUG3(printk("scsi(%ld): Init firmware -- success.\n",
1144 ha->host_no));
1145 }
1146
1147 return (rval);
1148}
1149
1150/**
1151 * qla2x00_fw_ready() - Waits for firmware ready.
1152 * @ha: HA context
1153 *
1154 * Returns 0 on success.
1155 */
1156static int
1157qla2x00_fw_ready(scsi_qla_host_t *ha)
1158{
1159 int rval;
1160 unsigned long wtime, mtime;
1161 uint16_t min_wait; /* Minimum wait time if loop is down */
1162 uint16_t wait_time; /* Wait time if loop is coming ready */
1163 uint16_t fw_state;
1164
1165 rval = QLA_SUCCESS;
1166
1167 /* 20 seconds for loop down. */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001168 min_wait = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170 /*
1171 * Firmware should take at most one RATOV to login, plus 5 seconds for
1172 * our own processing.
1173 */
1174 if ((wait_time = (ha->retry_count*ha->login_timeout) + 5) < min_wait) {
1175 wait_time = min_wait;
1176 }
1177
1178 /* Min wait time if loop down */
1179 mtime = jiffies + (min_wait * HZ);
1180
1181 /* wait time before firmware ready */
1182 wtime = jiffies + (wait_time * HZ);
1183
1184 /* Wait for ISP to finish LIP */
1185 if (!ha->flags.init_done)
1186 qla_printk(KERN_INFO, ha, "Waiting for LIP to complete...\n");
1187
1188 DEBUG3(printk("scsi(%ld): Waiting for LIP to complete...\n",
1189 ha->host_no));
1190
1191 do {
1192 rval = qla2x00_get_firmware_state(ha, &fw_state);
1193 if (rval == QLA_SUCCESS) {
1194 if (fw_state < FSTATE_LOSS_OF_SYNC) {
1195 ha->device_flags &= ~DFLG_NO_CABLE;
1196 }
1197 if (fw_state == FSTATE_READY) {
1198 DEBUG(printk("scsi(%ld): F/W Ready - OK \n",
1199 ha->host_no));
1200
1201 qla2x00_get_retry_cnt(ha, &ha->retry_count,
1202 &ha->login_timeout, &ha->r_a_tov);
1203
1204 rval = QLA_SUCCESS;
1205 break;
1206 }
1207
1208 rval = QLA_FUNCTION_FAILED;
1209
1210 if (atomic_read(&ha->loop_down_timer) &&
Andrew Vasquez7d7abc72006-06-23 16:11:17 -07001211 fw_state != FSTATE_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 /* Loop down. Timeout on min_wait for states
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001213 * other than Wait for Login.
1214 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 if (time_after_eq(jiffies, mtime)) {
1216 qla_printk(KERN_INFO, ha,
1217 "Cable is unplugged...\n");
1218
1219 ha->device_flags |= DFLG_NO_CABLE;
1220 break;
1221 }
1222 }
1223 } else {
1224 /* Mailbox cmd failed. Timeout on min_wait. */
1225 if (time_after_eq(jiffies, mtime))
1226 break;
1227 }
1228
1229 if (time_after_eq(jiffies, wtime))
1230 break;
1231
1232 /* Delay for a while */
1233 msleep(500);
1234
1235 DEBUG3(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
1236 ha->host_no, fw_state, jiffies));
1237 } while (1);
1238
1239 DEBUG(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
1240 ha->host_no, fw_state, jiffies));
1241
1242 if (rval) {
1243 DEBUG2_3(printk("scsi(%ld): Firmware ready **** FAILED ****.\n",
1244 ha->host_no));
1245 }
1246
1247 return (rval);
1248}
1249
1250/*
1251* qla2x00_configure_hba
1252* Setup adapter context.
1253*
1254* Input:
1255* ha = adapter state pointer.
1256*
1257* Returns:
1258* 0 = success
1259*
1260* Context:
1261* Kernel context.
1262*/
1263static int
1264qla2x00_configure_hba(scsi_qla_host_t *ha)
1265{
1266 int rval;
1267 uint16_t loop_id;
1268 uint16_t topo;
1269 uint8_t al_pa;
1270 uint8_t area;
1271 uint8_t domain;
1272 char connect_type[22];
1273
1274 /* Get host addresses. */
1275 rval = qla2x00_get_adapter_id(ha,
1276 &loop_id, &al_pa, &area, &domain, &topo);
1277 if (rval != QLA_SUCCESS) {
Andrew Vasquez23443b12005-12-06 10:57:06 -08001278 if (LOOP_TRANSITION(ha) || atomic_read(&ha->loop_down_timer) ||
Ravi Anand33135aa2005-11-08 14:37:20 -08001279 (rval == QLA_COMMAND_ERROR && loop_id == 0x7)) {
1280 DEBUG2(printk("%s(%ld) Loop is in a transition state\n",
1281 __func__, ha->host_no));
1282 } else {
1283 qla_printk(KERN_WARNING, ha,
1284 "ERROR -- Unable to get host loop ID.\n");
1285 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
1286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 return (rval);
1288 }
1289
1290 if (topo == 4) {
1291 qla_printk(KERN_INFO, ha,
1292 "Cannot get topology - retrying.\n");
1293 return (QLA_FUNCTION_FAILED);
1294 }
1295
1296 ha->loop_id = loop_id;
1297
1298 /* initialize */
1299 ha->min_external_loopid = SNS_FIRST_LOOP_ID;
1300 ha->operating_mode = LOOP;
1301
1302 switch (topo) {
1303 case 0:
1304 DEBUG3(printk("scsi(%ld): HBA in NL topology.\n",
1305 ha->host_no));
1306 ha->current_topology = ISP_CFG_NL;
1307 strcpy(connect_type, "(Loop)");
1308 break;
1309
1310 case 1:
1311 DEBUG3(printk("scsi(%ld): HBA in FL topology.\n",
1312 ha->host_no));
1313 ha->current_topology = ISP_CFG_FL;
1314 strcpy(connect_type, "(FL_Port)");
1315 break;
1316
1317 case 2:
1318 DEBUG3(printk("scsi(%ld): HBA in N P2P topology.\n",
1319 ha->host_no));
1320 ha->operating_mode = P2P;
1321 ha->current_topology = ISP_CFG_N;
1322 strcpy(connect_type, "(N_Port-to-N_Port)");
1323 break;
1324
1325 case 3:
1326 DEBUG3(printk("scsi(%ld): HBA in F P2P topology.\n",
1327 ha->host_no));
1328 ha->operating_mode = P2P;
1329 ha->current_topology = ISP_CFG_F;
1330 strcpy(connect_type, "(F_Port)");
1331 break;
1332
1333 default:
1334 DEBUG3(printk("scsi(%ld): HBA in unknown topology %x. "
1335 "Using NL.\n",
1336 ha->host_no, topo));
1337 ha->current_topology = ISP_CFG_NL;
1338 strcpy(connect_type, "(Loop)");
1339 break;
1340 }
1341
1342 /* Save Host port and loop ID. */
1343 /* byte order - Big Endian */
1344 ha->d_id.b.domain = domain;
1345 ha->d_id.b.area = area;
1346 ha->d_id.b.al_pa = al_pa;
1347
1348 if (!ha->flags.init_done)
1349 qla_printk(KERN_INFO, ha,
1350 "Topology - %s, Host Loop address 0x%x\n",
1351 connect_type, ha->loop_id);
1352
1353 if (rval) {
1354 DEBUG2_3(printk("scsi(%ld): FAILED.\n", ha->host_no));
1355 } else {
1356 DEBUG3(printk("scsi(%ld): exiting normally.\n", ha->host_no));
1357 }
1358
1359 return(rval);
1360}
1361
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08001362static inline void
1363qla2x00_set_model_info(scsi_qla_host_t *ha, uint8_t *model, size_t len, char *def)
1364{
1365 char *st, *en;
1366 uint16_t index;
1367
1368 if (memcmp(model, BINZERO, len) != 0) {
1369 strncpy(ha->model_number, model, len);
1370 st = en = ha->model_number;
1371 en += len - 1;
1372 while (en > st) {
1373 if (*en != 0x20 && *en != 0x00)
1374 break;
1375 *en-- = '\0';
1376 }
1377
1378 index = (ha->pdev->subsystem_device & 0xff);
1379 if (ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
1380 index < QLA_MODEL_NAMES)
1381 ha->model_desc = qla2x00_model_name[index * 2 + 1];
1382 } else {
1383 index = (ha->pdev->subsystem_device & 0xff);
1384 if (ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
1385 index < QLA_MODEL_NAMES) {
1386 strcpy(ha->model_number,
1387 qla2x00_model_name[index * 2]);
1388 ha->model_desc = qla2x00_model_name[index * 2 + 1];
1389 } else {
1390 strcpy(ha->model_number, def);
1391 }
1392 }
1393}
1394
David Miller4e08df32007-04-16 12:37:43 -07001395/* On sparc systems, obtain port and node WWN from firmware
1396 * properties.
1397 */
1398static void qla2xxx_nvram_wwn_from_ofw(scsi_qla_host_t *ha, nvram_t *nv)
1399{
1400#ifdef CONFIG_SPARC
1401 struct pci_dev *pdev = ha->pdev;
David S. Miller15576bc2007-05-08 00:36:49 -07001402 struct device_node *dp = pci_device_to_OF_node(pdev);
1403 const u8 *val;
David Miller4e08df32007-04-16 12:37:43 -07001404 int len;
1405
1406 val = of_get_property(dp, "port-wwn", &len);
1407 if (val && len >= WWN_SIZE)
1408 memcpy(nv->port_name, val, WWN_SIZE);
1409
1410 val = of_get_property(dp, "node-wwn", &len);
1411 if (val && len >= WWN_SIZE)
1412 memcpy(nv->node_name, val, WWN_SIZE);
1413#endif
1414}
1415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416/*
1417* NVRAM configuration for ISP 2xxx
1418*
1419* Input:
1420* ha = adapter block pointer.
1421*
1422* Output:
1423* initialization control block in response_ring
1424* host adapters parameters in host adapter block
1425*
1426* Returns:
1427* 0 = success.
1428*/
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001429int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430qla2x00_nvram_config(scsi_qla_host_t *ha)
1431{
David Miller4e08df32007-04-16 12:37:43 -07001432 int rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001433 uint8_t chksum = 0;
1434 uint16_t cnt;
1435 uint8_t *dptr1, *dptr2;
1436 init_cb_t *icb = ha->init_cb;
1437 nvram_t *nv = (nvram_t *)ha->request_ring;
1438 uint8_t *ptr = (uint8_t *)ha->request_ring;
Andrew Vasquez3d716442005-07-06 10:30:26 -07001439 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
David Miller4e08df32007-04-16 12:37:43 -07001441 rval = QLA_SUCCESS;
1442
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 /* Determine NVRAM starting address. */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001444 ha->nvram_size = sizeof(nvram_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 ha->nvram_base = 0;
1446 if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha))
1447 if ((RD_REG_WORD(&reg->ctrl_status) >> 14) == 1)
1448 ha->nvram_base = 0x80;
1449
1450 /* Get NVRAM data and calculate checksum. */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001451 ha->isp_ops.read_nvram(ha, ptr, ha->nvram_base, ha->nvram_size);
1452 for (cnt = 0, chksum = 0; cnt < ha->nvram_size; cnt++)
1453 chksum += *ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455 DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
1456 DEBUG5(qla2x00_dump_buffer((uint8_t *)ha->request_ring,
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001457 ha->nvram_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
1459 /* Bad NVRAM data, set defaults parameters. */
1460 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' ||
1461 nv->id[2] != 'P' || nv->id[3] != ' ' || nv->nvram_version < 1) {
1462 /* Reset NVRAM data. */
1463 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
1464 "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
1465 nv->nvram_version);
David Miller4e08df32007-04-16 12:37:43 -07001466 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
1467 "invalid -- WWPN) defaults.\n");
1468
1469 /*
1470 * Set default initialization control block.
1471 */
1472 memset(nv, 0, ha->nvram_size);
1473 nv->parameter_block_version = ICB_VERSION;
1474
1475 if (IS_QLA23XX(ha)) {
1476 nv->firmware_options[0] = BIT_2 | BIT_1;
1477 nv->firmware_options[1] = BIT_7 | BIT_5;
1478 nv->add_firmware_options[0] = BIT_5;
1479 nv->add_firmware_options[1] = BIT_5 | BIT_4;
1480 nv->frame_payload_size = __constant_cpu_to_le16(2048);
1481 nv->special_options[1] = BIT_7;
1482 } else if (IS_QLA2200(ha)) {
1483 nv->firmware_options[0] = BIT_2 | BIT_1;
1484 nv->firmware_options[1] = BIT_7 | BIT_5;
1485 nv->add_firmware_options[0] = BIT_5;
1486 nv->add_firmware_options[1] = BIT_5 | BIT_4;
1487 nv->frame_payload_size = __constant_cpu_to_le16(1024);
1488 } else if (IS_QLA2100(ha)) {
1489 nv->firmware_options[0] = BIT_3 | BIT_1;
1490 nv->firmware_options[1] = BIT_5;
1491 nv->frame_payload_size = __constant_cpu_to_le16(1024);
1492 }
1493
1494 nv->max_iocb_allocation = __constant_cpu_to_le16(256);
1495 nv->execution_throttle = __constant_cpu_to_le16(16);
1496 nv->retry_count = 8;
1497 nv->retry_delay = 1;
1498
1499 nv->port_name[0] = 33;
1500 nv->port_name[3] = 224;
1501 nv->port_name[4] = 139;
1502
1503 qla2xxx_nvram_wwn_from_ofw(ha, nv);
1504
1505 nv->login_timeout = 4;
1506
1507 /*
1508 * Set default host adapter parameters
1509 */
1510 nv->host_p[1] = BIT_2;
1511 nv->reset_delay = 5;
1512 nv->port_down_retry_count = 8;
1513 nv->max_luns_per_target = __constant_cpu_to_le16(8);
1514 nv->link_down_timeout = 60;
1515
1516 rval = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 }
1518
1519#if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
1520 /*
1521 * The SN2 does not provide BIOS emulation which means you can't change
1522 * potentially bogus BIOS settings. Force the use of default settings
1523 * for link rate and frame size. Hope that the rest of the settings
1524 * are valid.
1525 */
1526 if (ia64_platform_is("sn2")) {
1527 nv->frame_payload_size = __constant_cpu_to_le16(2048);
1528 if (IS_QLA23XX(ha))
1529 nv->special_options[1] = BIT_7;
1530 }
1531#endif
1532
1533 /* Reset Initialization control block */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001534 memset(icb, 0, ha->init_cb_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
1536 /*
1537 * Setup driver NVRAM options.
1538 */
1539 nv->firmware_options[0] |= (BIT_6 | BIT_1);
1540 nv->firmware_options[0] &= ~(BIT_5 | BIT_4);
1541 nv->firmware_options[1] |= (BIT_5 | BIT_0);
1542 nv->firmware_options[1] &= ~BIT_4;
1543
1544 if (IS_QLA23XX(ha)) {
1545 nv->firmware_options[0] |= BIT_2;
1546 nv->firmware_options[0] &= ~BIT_3;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001547 nv->add_firmware_options[1] |= BIT_5 | BIT_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
1549 if (IS_QLA2300(ha)) {
1550 if (ha->fb_rev == FPM_2310) {
1551 strcpy(ha->model_number, "QLA2310");
1552 } else {
1553 strcpy(ha->model_number, "QLA2300");
1554 }
1555 } else {
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08001556 qla2x00_set_model_info(ha, nv->model_number,
1557 sizeof(nv->model_number), "QLA23xx");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 }
1559 } else if (IS_QLA2200(ha)) {
1560 nv->firmware_options[0] |= BIT_2;
1561 /*
1562 * 'Point-to-point preferred, else loop' is not a safe
1563 * connection mode setting.
1564 */
1565 if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
1566 (BIT_5 | BIT_4)) {
1567 /* Force 'loop preferred, else point-to-point'. */
1568 nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
1569 nv->add_firmware_options[0] |= BIT_5;
1570 }
1571 strcpy(ha->model_number, "QLA22xx");
1572 } else /*if (IS_QLA2100(ha))*/ {
1573 strcpy(ha->model_number, "QLA2100");
1574 }
1575
1576 /*
1577 * Copy over NVRAM RISC parameter block to initialization control block.
1578 */
1579 dptr1 = (uint8_t *)icb;
1580 dptr2 = (uint8_t *)&nv->parameter_block_version;
1581 cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
1582 while (cnt--)
1583 *dptr1++ = *dptr2++;
1584
1585 /* Copy 2nd half. */
1586 dptr1 = (uint8_t *)icb->add_firmware_options;
1587 cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
1588 while (cnt--)
1589 *dptr1++ = *dptr2++;
1590
Andrew Vasquez5341e862006-05-17 15:09:16 -07001591 /* Use alternate WWN? */
1592 if (nv->host_p[1] & BIT_7) {
1593 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
1594 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
1595 }
1596
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 /* Prepare nodename */
1598 if ((icb->firmware_options[1] & BIT_6) == 0) {
1599 /*
1600 * Firmware will apply the following mask if the nodename was
1601 * not provided.
1602 */
1603 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
1604 icb->node_name[0] &= 0xF0;
1605 }
1606
1607 /*
1608 * Set host adapter parameters.
1609 */
Andrew Vasquez01819442006-06-23 16:11:10 -07001610 if (nv->host_p[0] & BIT_7)
Andrew Vasquez11010fe2006-10-06 09:54:59 -07001611 ql2xextended_error_logging = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
1613 /* Always load RISC code on non ISP2[12]00 chips. */
1614 if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
1615 ha->flags.disable_risc_code_load = 0;
1616 ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
1617 ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
1618 ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
Andrew Vasquez06c22bd2005-08-26 19:09:00 -07001619 ha->flags.enable_led_scheme = (nv->special_options[1] & BIT_4) ? 1 : 0;
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07001620 ha->flags.disable_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
1622 ha->operating_mode =
1623 (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
1624
1625 memcpy(ha->fw_seriallink_options, nv->seriallink_options,
1626 sizeof(ha->fw_seriallink_options));
1627
1628 /* save HBA serial number */
1629 ha->serial0 = icb->port_name[5];
1630 ha->serial1 = icb->port_name[6];
1631 ha->serial2 = icb->port_name[7];
Andrew Vasquez3d716442005-07-06 10:30:26 -07001632 ha->node_name = icb->node_name;
1633 ha->port_name = icb->port_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
1635 icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
1636
1637 ha->retry_count = nv->retry_count;
1638
1639 /* Set minimum login_timeout to 4 seconds. */
1640 if (nv->login_timeout < ql2xlogintimeout)
1641 nv->login_timeout = ql2xlogintimeout;
1642 if (nv->login_timeout < 4)
1643 nv->login_timeout = 4;
1644 ha->login_timeout = nv->login_timeout;
1645 icb->login_timeout = nv->login_timeout;
1646
1647 /* Set minimum RATOV to 200 tenths of a second. */
1648 ha->r_a_tov = 200;
1649
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 ha->loop_reset_delay = nv->reset_delay;
1651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 /* Link Down Timeout = 0:
1653 *
1654 * When Port Down timer expires we will start returning
1655 * I/O's to OS with "DID_NO_CONNECT".
1656 *
1657 * Link Down Timeout != 0:
1658 *
1659 * The driver waits for the link to come up after link down
1660 * before returning I/Os to OS with "DID_NO_CONNECT".
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001661 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 if (nv->link_down_timeout == 0) {
1663 ha->loop_down_abort_time =
Andrew Vasquez 354d6b22005-04-23 02:47:27 -04001664 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 } else {
1666 ha->link_down_timeout = nv->link_down_timeout;
1667 ha->loop_down_abort_time =
1668 (LOOP_DOWN_TIME - ha->link_down_timeout);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 /*
1672 * Need enough time to try and get the port back.
1673 */
1674 ha->port_down_retry_count = nv->port_down_retry_count;
1675 if (qlport_down_retry)
1676 ha->port_down_retry_count = qlport_down_retry;
1677 /* Set login_retry_count */
1678 ha->login_retry_count = nv->retry_count;
1679 if (ha->port_down_retry_count == nv->port_down_retry_count &&
1680 ha->port_down_retry_count > 3)
1681 ha->login_retry_count = ha->port_down_retry_count;
1682 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
1683 ha->login_retry_count = ha->port_down_retry_count;
1684 if (ql2xloginretrycount)
1685 ha->login_retry_count = ql2xloginretrycount;
1686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 icb->lun_enables = __constant_cpu_to_le16(0);
1688 icb->command_resource_count = 0;
1689 icb->immediate_notify_resource_count = 0;
1690 icb->timeout = __constant_cpu_to_le16(0);
1691
1692 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
1693 /* Enable RIO */
1694 icb->firmware_options[0] &= ~BIT_3;
1695 icb->add_firmware_options[0] &=
1696 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
1697 icb->add_firmware_options[0] |= BIT_2;
1698 icb->response_accumulation_timer = 3;
1699 icb->interrupt_delay_timer = 5;
1700
1701 ha->flags.process_response_queue = 1;
1702 } else {
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001703 /* Enable ZIO. */
1704 if (!ha->flags.init_done) {
1705 ha->zio_mode = icb->add_firmware_options[0] &
1706 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
1707 ha->zio_timer = icb->interrupt_delay_timer ?
1708 icb->interrupt_delay_timer: 2;
1709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 icb->add_firmware_options[0] &=
1711 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001712 ha->flags.process_response_queue = 0;
1713 if (ha->zio_mode != QLA_ZIO_DISABLED) {
andrew.vasquez@qlogic.com4a59f712006-03-09 14:27:39 -08001714 ha->zio_mode = QLA_ZIO_MODE_6;
1715
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001716 DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer "
1717 "delay (%d us).\n", ha->host_no, ha->zio_mode,
1718 ha->zio_timer * 100));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 qla_printk(KERN_INFO, ha,
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001720 "ZIO mode %d enabled; timer delay (%d us).\n",
1721 ha->zio_mode, ha->zio_timer * 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001723 icb->add_firmware_options[0] |= (uint8_t)ha->zio_mode;
1724 icb->interrupt_delay_timer = (uint8_t)ha->zio_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 ha->flags.process_response_queue = 1;
1726 }
1727 }
1728
David Miller4e08df32007-04-16 12:37:43 -07001729 if (rval) {
1730 DEBUG2_3(printk(KERN_WARNING
1731 "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
1732 }
1733 return (rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734}
1735
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001736static void
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001737qla2x00_rport_del(void *data)
1738{
1739 fc_port_t *fcport = data;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08001740 struct fc_rport *rport;
1741 unsigned long flags;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001742
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08001743 spin_lock_irqsave(&fcport->rport_lock, flags);
1744 rport = fcport->drport;
1745 fcport->drport = NULL;
1746 spin_unlock_irqrestore(&fcport->rport_lock, flags);
1747 if (rport)
1748 fc_remote_port_delete(rport);
1749
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001750}
1751
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752/**
1753 * qla2x00_alloc_fcport() - Allocate a generic fcport.
1754 * @ha: HA context
1755 * @flags: allocation flags
1756 *
1757 * Returns a pointer to the allocated fcport, or NULL, if none available.
1758 */
Adrian Bunk413975a2006-06-30 02:33:06 -07001759static fc_port_t *
Al Viroc53033f2005-10-21 03:22:08 -04001760qla2x00_alloc_fcport(scsi_qla_host_t *ha, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761{
1762 fc_port_t *fcport;
1763
1764 fcport = kmalloc(sizeof(fc_port_t), flags);
1765 if (fcport == NULL)
1766 return (fcport);
1767
1768 /* Setup fcport template structure. */
1769 memset(fcport, 0, sizeof (fc_port_t));
1770 fcport->ha = ha;
1771 fcport->port_type = FCT_UNKNOWN;
1772 fcport->loop_id = FC_NO_LOOP_ID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 atomic_set(&fcport->state, FCS_UNCONFIGURED);
1774 fcport->flags = FCF_RLC_SUPPORT;
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07001775 fcport->supported_classes = FC_COS_UNSPECIFIED;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08001776 spin_lock_init(&fcport->rport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
1778 return (fcport);
1779}
1780
1781/*
1782 * qla2x00_configure_loop
1783 * Updates Fibre Channel Device Database with what is actually on loop.
1784 *
1785 * Input:
1786 * ha = adapter block pointer.
1787 *
1788 * Returns:
1789 * 0 = success.
1790 * 1 = error.
1791 * 2 = database was full and device was not configured.
1792 */
1793static int
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001794qla2x00_configure_loop(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795{
1796 int rval;
1797 unsigned long flags, save_flags;
1798
1799 rval = QLA_SUCCESS;
1800
1801 /* Get Initiator ID */
1802 if (test_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags)) {
1803 rval = qla2x00_configure_hba(ha);
1804 if (rval != QLA_SUCCESS) {
1805 DEBUG(printk("scsi(%ld): Unable to configure HBA.\n",
1806 ha->host_no));
1807 return (rval);
1808 }
1809 }
1810
1811 save_flags = flags = ha->dpc_flags;
1812 DEBUG(printk("scsi(%ld): Configure loop -- dpc flags =0x%lx\n",
1813 ha->host_no, flags));
1814
1815 /*
1816 * If we have both an RSCN and PORT UPDATE pending then handle them
1817 * both at the same time.
1818 */
1819 clear_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1820 clear_bit(RSCN_UPDATE, &ha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
1822 /* Determine what we need to do */
1823 if (ha->current_topology == ISP_CFG_FL &&
1824 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1825
1826 ha->flags.rscn_queue_overflow = 1;
1827 set_bit(RSCN_UPDATE, &flags);
1828
1829 } else if (ha->current_topology == ISP_CFG_F &&
1830 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1831
1832 ha->flags.rscn_queue_overflow = 1;
1833 set_bit(RSCN_UPDATE, &flags);
1834 clear_bit(LOCAL_LOOP_UPDATE, &flags);
1835
Andrew Vasquez21333b42006-05-17 15:09:56 -07001836 } else if (ha->current_topology == ISP_CFG_N) {
1837 clear_bit(RSCN_UPDATE, &flags);
1838
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 } else if (!ha->flags.online ||
1840 (test_bit(ABORT_ISP_ACTIVE, &flags))) {
1841
1842 ha->flags.rscn_queue_overflow = 1;
1843 set_bit(RSCN_UPDATE, &flags);
1844 set_bit(LOCAL_LOOP_UPDATE, &flags);
1845 }
1846
1847 if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
1848 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1849 rval = QLA_FUNCTION_FAILED;
1850 } else {
1851 rval = qla2x00_configure_local_loop(ha);
1852 }
1853 }
1854
1855 if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
Andrew Vasquez23443b12005-12-06 10:57:06 -08001856 if (LOOP_TRANSITION(ha)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 rval = QLA_FUNCTION_FAILED;
1858 } else {
1859 rval = qla2x00_configure_fabric(ha);
1860 }
1861 }
1862
1863 if (rval == QLA_SUCCESS) {
1864 if (atomic_read(&ha->loop_down_timer) ||
1865 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1866 rval = QLA_FUNCTION_FAILED;
1867 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 atomic_set(&ha->loop_state, LOOP_READY);
1869
1870 DEBUG(printk("scsi(%ld): LOOP READY\n", ha->host_no));
1871 }
1872 }
1873
1874 if (rval) {
1875 DEBUG2_3(printk("%s(%ld): *** FAILED ***\n",
1876 __func__, ha->host_no));
1877 } else {
1878 DEBUG3(printk("%s: exiting normally\n", __func__));
1879 }
1880
1881 /* Restore state if a resync event occured during processing */
1882 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1883 if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
1884 set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1885 if (test_bit(RSCN_UPDATE, &save_flags))
1886 set_bit(RSCN_UPDATE, &ha->dpc_flags);
1887 }
1888
1889 return (rval);
1890}
1891
1892
1893
1894/*
1895 * qla2x00_configure_local_loop
1896 * Updates Fibre Channel Device Database with local loop devices.
1897 *
1898 * Input:
1899 * ha = adapter block pointer.
1900 *
1901 * Returns:
1902 * 0 = success.
1903 */
1904static int
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001905qla2x00_configure_local_loop(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906{
1907 int rval, rval2;
1908 int found_devs;
1909 int found;
1910 fc_port_t *fcport, *new_fcport;
1911
1912 uint16_t index;
1913 uint16_t entries;
1914 char *id_iter;
1915 uint16_t loop_id;
1916 uint8_t domain, area, al_pa;
1917
1918 found_devs = 0;
1919 new_fcport = NULL;
1920 entries = MAX_FIBRE_DEVICES;
1921
1922 DEBUG3(printk("scsi(%ld): Getting FCAL position map\n", ha->host_no));
1923 DEBUG3(qla2x00_get_fcal_position_map(ha, NULL));
1924
1925 /* Get list of logged in devices. */
1926 memset(ha->gid_list, 0, GID_LIST_SIZE);
1927 rval = qla2x00_get_id_list(ha, ha->gid_list, ha->gid_list_dma,
1928 &entries);
1929 if (rval != QLA_SUCCESS)
1930 goto cleanup_allocation;
1931
1932 DEBUG3(printk("scsi(%ld): Entries in ID list (%d)\n",
1933 ha->host_no, entries));
1934 DEBUG3(qla2x00_dump_buffer((uint8_t *)ha->gid_list,
1935 entries * sizeof(struct gid_list_info)));
1936
1937 /* Allocate temporary fcport for any new fcports discovered. */
1938 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
1939 if (new_fcport == NULL) {
1940 rval = QLA_MEMORY_ALLOC_FAILED;
1941 goto cleanup_allocation;
1942 }
1943 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
1944
1945 /*
1946 * Mark local devices that were present with FCF_DEVICE_LOST for now.
1947 */
1948 list_for_each_entry(fcport, &ha->fcports, list) {
1949 if (atomic_read(&fcport->state) == FCS_ONLINE &&
1950 fcport->port_type != FCT_BROADCAST &&
1951 (fcport->flags & FCF_FABRIC_DEVICE) == 0) {
1952
1953 DEBUG(printk("scsi(%ld): Marking port lost, "
1954 "loop_id=0x%04x\n",
1955 ha->host_no, fcport->loop_id));
1956
1957 atomic_set(&fcport->state, FCS_DEVICE_LOST);
1958 fcport->flags &= ~FCF_FARP_DONE;
1959 }
1960 }
1961
1962 /* Add devices to port list. */
1963 id_iter = (char *)ha->gid_list;
1964 for (index = 0; index < entries; index++) {
1965 domain = ((struct gid_list_info *)id_iter)->domain;
1966 area = ((struct gid_list_info *)id_iter)->area;
1967 al_pa = ((struct gid_list_info *)id_iter)->al_pa;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001968 if (IS_QLA2100(ha) || IS_QLA2200(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 loop_id = (uint16_t)
1970 ((struct gid_list_info *)id_iter)->loop_id_2100;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001971 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 loop_id = le16_to_cpu(
1973 ((struct gid_list_info *)id_iter)->loop_id);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001974 id_iter += ha->gid_list_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
1976 /* Bypass reserved domain fields. */
1977 if ((domain & 0xf0) == 0xf0)
1978 continue;
1979
1980 /* Bypass if not same domain and area of adapter. */
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07001981 if (area && domain &&
1982 (area != ha->d_id.b.area || domain != ha->d_id.b.domain))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 continue;
1984
1985 /* Bypass invalid local loop ID. */
1986 if (loop_id > LAST_LOCAL_LOOP_ID)
1987 continue;
1988
1989 /* Fill in member data. */
1990 new_fcport->d_id.b.domain = domain;
1991 new_fcport->d_id.b.area = area;
1992 new_fcport->d_id.b.al_pa = al_pa;
1993 new_fcport->loop_id = loop_id;
1994 rval2 = qla2x00_get_port_database(ha, new_fcport, 0);
1995 if (rval2 != QLA_SUCCESS) {
1996 DEBUG2(printk("scsi(%ld): Failed to retrieve fcport "
1997 "information -- get_port_database=%x, "
1998 "loop_id=0x%04x\n",
1999 ha->host_no, rval2, new_fcport->loop_id));
andrew.vasquez@qlogic.comc9d02ac2006-01-13 17:05:26 -08002000 DEBUG2(printk("scsi(%ld): Scheduling resync...\n",
2001 ha->host_no));
2002 set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 continue;
2004 }
2005
2006 /* Check for matching device in port list. */
2007 found = 0;
2008 fcport = NULL;
2009 list_for_each_entry(fcport, &ha->fcports, list) {
2010 if (memcmp(new_fcport->port_name, fcport->port_name,
2011 WWN_SIZE))
2012 continue;
2013
2014 fcport->flags &= ~(FCF_FABRIC_DEVICE |
2015 FCF_PERSISTENT_BOUND);
2016 fcport->loop_id = new_fcport->loop_id;
2017 fcport->port_type = new_fcport->port_type;
2018 fcport->d_id.b24 = new_fcport->d_id.b24;
2019 memcpy(fcport->node_name, new_fcport->node_name,
2020 WWN_SIZE);
2021
2022 found++;
2023 break;
2024 }
2025
2026 if (!found) {
2027 /* New device, add to fcports list. */
2028 new_fcport->flags &= ~FCF_PERSISTENT_BOUND;
2029 list_add_tail(&new_fcport->list, &ha->fcports);
2030
2031 /* Allocate a new replacement fcport. */
2032 fcport = new_fcport;
2033 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2034 if (new_fcport == NULL) {
2035 rval = QLA_MEMORY_ALLOC_FAILED;
2036 goto cleanup_allocation;
2037 }
2038 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
2039 }
2040
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002041 /* Base iIDMA settings on HBA port speed. */
2042 switch (ha->link_data_rate) {
2043 case PORT_SPEED_1GB:
2044 fcport->fp_speed = cpu_to_be16(BIT_15);
2045 break;
2046 case PORT_SPEED_2GB:
2047 fcport->fp_speed = cpu_to_be16(BIT_14);
2048 break;
2049 case PORT_SPEED_4GB:
2050 fcport->fp_speed = cpu_to_be16(BIT_13);
2051 break;
2052 }
2053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 qla2x00_update_fcport(ha, fcport);
2055
2056 found_devs++;
2057 }
2058
2059cleanup_allocation:
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002060 kfree(new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
2062 if (rval != QLA_SUCCESS) {
2063 DEBUG2(printk("scsi(%ld): Configure local loop error exit: "
2064 "rval=%x\n", ha->host_no, rval));
2065 }
2066
2067 if (found_devs) {
2068 ha->device_flags |= DFLG_LOCAL_DEVICES;
2069 ha->device_flags &= ~DFLG_RETRY_LOCAL_DEVICES;
2070 }
2071
2072 return (rval);
2073}
2074
2075static void
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002076qla2x00_probe_for_all_luns(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077{
2078 fc_port_t *fcport;
2079
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002080 qla2x00_mark_all_devices_lost(ha, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 list_for_each_entry(fcport, &ha->fcports, list) {
2082 if (fcport->port_type != FCT_TARGET)
2083 continue;
2084
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002085 qla2x00_update_fcport(ha, fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 }
2087}
2088
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002089static void
2090qla2x00_iidma_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
2091{
2092#define LS_UNKNOWN 2
2093 static char *link_speeds[5] = { "1", "2", "?", "4" };
2094 int rval;
2095 uint16_t port_speed, mb[6];
2096
2097 if (!IS_QLA24XX(ha))
2098 return;
2099
2100 switch (be16_to_cpu(fcport->fp_speed)) {
2101 case BIT_15:
2102 port_speed = PORT_SPEED_1GB;
2103 break;
2104 case BIT_14:
2105 port_speed = PORT_SPEED_2GB;
2106 break;
2107 case BIT_13:
2108 port_speed = PORT_SPEED_4GB;
2109 break;
2110 default:
2111 DEBUG2(printk("scsi(%ld): %02x%02x%02x%02x%02x%02x%02x%02x -- "
2112 "unsupported FM port operating speed (%04x).\n",
2113 ha->host_no, fcport->port_name[0], fcport->port_name[1],
2114 fcport->port_name[2], fcport->port_name[3],
2115 fcport->port_name[4], fcport->port_name[5],
2116 fcport->port_name[6], fcport->port_name[7],
2117 be16_to_cpu(fcport->fp_speed)));
2118 port_speed = PORT_SPEED_UNKNOWN;
2119 break;
2120 }
2121 if (port_speed == PORT_SPEED_UNKNOWN)
2122 return;
2123
2124 rval = qla2x00_set_idma_speed(ha, fcport->loop_id, port_speed, mb);
2125 if (rval != QLA_SUCCESS) {
2126 DEBUG2(printk("scsi(%ld): Unable to adjust iIDMA "
2127 "%02x%02x%02x%02x%02x%02x%02x%02x -- %04x %x %04x %04x.\n",
2128 ha->host_no, fcport->port_name[0], fcport->port_name[1],
2129 fcport->port_name[2], fcport->port_name[3],
2130 fcport->port_name[4], fcport->port_name[5],
2131 fcport->port_name[6], fcport->port_name[7], rval,
2132 port_speed, mb[0], mb[1]));
2133 } else {
2134 DEBUG2(qla_printk(KERN_INFO, ha,
2135 "iIDMA adjusted to %s GB/s on "
2136 "%02x%02x%02x%02x%02x%02x%02x%02x.\n",
2137 link_speeds[port_speed], fcport->port_name[0],
2138 fcport->port_name[1], fcport->port_name[2],
2139 fcport->port_name[3], fcport->port_name[4],
2140 fcport->port_name[5], fcport->port_name[6],
2141 fcport->port_name[7]));
2142 }
2143}
2144
Adrian Bunk23be3312006-11-24 02:46:01 +01002145static void
8482e1182005-04-17 15:04:54 -05002146qla2x00_reg_remote_port(scsi_qla_host_t *ha, fc_port_t *fcport)
2147{
2148 struct fc_rport_identifiers rport_ids;
bdf79622005-04-17 15:06:53 -05002149 struct fc_rport *rport;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002150 unsigned long flags;
8482e1182005-04-17 15:04:54 -05002151
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002152 if (fcport->drport)
2153 qla2x00_rport_del(fcport);
2154 if (fcport->rport)
2155 return;
8482e1182005-04-17 15:04:54 -05002156
Andrew Vasquezf8b02a82005-08-31 15:21:20 -07002157 rport_ids.node_name = wwn_to_u64(fcport->node_name);
2158 rport_ids.port_name = wwn_to_u64(fcport->port_name);
8482e1182005-04-17 15:04:54 -05002159 rport_ids.port_id = fcport->d_id.b.domain << 16 |
2160 fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
2161 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002162 rport = fc_remote_port_add(ha->host, 0, &rport_ids);
Andrew Vasquez77d74142005-07-08 18:00:36 -07002163 if (!rport) {
2164 qla_printk(KERN_WARNING, ha,
2165 "Unable to allocate fc remote port!\n");
2166 return;
2167 }
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002168 spin_lock_irqsave(&fcport->rport_lock, flags);
2169 fcport->rport = rport;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002170 *((fc_port_t **)rport->dd_data) = fcport;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002171 spin_unlock_irqrestore(&fcport->rport_lock, flags);
2172
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07002173 rport->supported_classes = fcport->supported_classes;
Andrew Vasquez77d74142005-07-08 18:00:36 -07002174
2175 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
8482e1182005-04-17 15:04:54 -05002176 if (fcport->port_type == FCT_INITIATOR)
2177 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2178 if (fcport->port_type == FCT_TARGET)
2179 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
Andrew Vasquez77d74142005-07-08 18:00:36 -07002180 fc_remote_port_rolechg(rport, rport_ids.roles);
bdf79622005-04-17 15:06:53 -05002181
Andrew Vasquezcc4731f2005-07-06 10:32:37 -07002182 if (rport->scsi_target_id != -1 &&
2183 rport->scsi_target_id < ha->host->max_id)
bdf79622005-04-17 15:06:53 -05002184 fcport->os_target_id = rport->scsi_target_id;
8482e1182005-04-17 15:04:54 -05002185}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
2187/*
Adrian Bunk23be3312006-11-24 02:46:01 +01002188 * qla2x00_update_fcport
2189 * Updates device on list.
2190 *
2191 * Input:
2192 * ha = adapter block pointer.
2193 * fcport = port structure pointer.
2194 *
2195 * Return:
2196 * 0 - Success
2197 * BIT_0 - error
2198 *
2199 * Context:
2200 * Kernel context.
2201 */
2202void
2203qla2x00_update_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
2204{
2205 fcport->ha = ha;
2206 fcport->login_retry = 0;
2207 fcport->port_login_retry_count = ha->port_down_retry_count *
2208 PORT_RETRY_TIME;
2209 atomic_set(&fcport->port_down_timer, ha->port_down_retry_count *
2210 PORT_RETRY_TIME);
2211 fcport->flags &= ~FCF_LOGIN_NEEDED;
2212
2213 qla2x00_iidma_fcport(ha, fcport);
2214
2215 atomic_set(&fcport->state, FCS_ONLINE);
2216
2217 qla2x00_reg_remote_port(ha, fcport);
2218}
2219
2220/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 * qla2x00_configure_fabric
2222 * Setup SNS devices with loop ID's.
2223 *
2224 * Input:
2225 * ha = adapter block pointer.
2226 *
2227 * Returns:
2228 * 0 = success.
2229 * BIT_0 = error
2230 */
2231static int
2232qla2x00_configure_fabric(scsi_qla_host_t *ha)
2233{
2234 int rval, rval2;
2235 fc_port_t *fcport, *fcptemp;
2236 uint16_t next_loopid;
2237 uint16_t mb[MAILBOX_REGISTER_COUNT];
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002238 uint16_t loop_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 LIST_HEAD(new_fcports);
2240
2241 /* If FL port exists, then SNS is present */
andrew.vasquez@qlogic.com044cc6c2006-03-09 14:27:13 -08002242 if (IS_QLA24XX(ha) || IS_QLA54XX(ha))
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002243 loop_id = NPH_F_PORT;
2244 else
2245 loop_id = SNS_FL_PORT;
Andrew Vasquez90991c82006-10-02 12:00:46 -07002246 rval = qla2x00_get_port_name(ha, loop_id, ha->fabric_node_name, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 if (rval != QLA_SUCCESS) {
2248 DEBUG2(printk("scsi(%ld): MBC_GET_PORT_NAME Failed, No FL "
2249 "Port\n", ha->host_no));
2250
2251 ha->device_flags &= ~SWITCH_FOUND;
2252 return (QLA_SUCCESS);
2253 }
Andrew Vasquez90991c82006-10-02 12:00:46 -07002254 ha->device_flags |= SWITCH_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
2256 /* Mark devices that need re-synchronization. */
2257 rval2 = qla2x00_device_resync(ha);
2258 if (rval2 == QLA_RSCNS_HANDLED) {
2259 /* No point doing the scan, just continue. */
2260 return (QLA_SUCCESS);
2261 }
2262 do {
Andrew Vasquezcca53352005-08-26 19:08:30 -07002263 /* FDMI support. */
2264 if (ql2xfdmienable &&
2265 test_and_clear_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags))
2266 qla2x00_fdmi_register(ha);
2267
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 /* Ensure we are logged into the SNS. */
andrew.vasquez@qlogic.com044cc6c2006-03-09 14:27:13 -08002269 if (IS_QLA24XX(ha) || IS_QLA54XX(ha))
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002270 loop_id = NPH_SNS;
2271 else
2272 loop_id = SIMPLE_NAME_SERVER;
2273 ha->isp_ops.fabric_login(ha, loop_id, 0xff, 0xff,
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002274 0xfc, mb, BIT_1 | BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 if (mb[0] != MBS_COMMAND_COMPLETE) {
2276 DEBUG2(qla_printk(KERN_INFO, ha,
2277 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002278 "mb[2]=%x mb[6]=%x mb[7]=%x\n", loop_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 mb[0], mb[1], mb[2], mb[6], mb[7]));
2280 return (QLA_SUCCESS);
2281 }
2282
2283 if (test_and_clear_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags)) {
2284 if (qla2x00_rft_id(ha)) {
2285 /* EMPTY */
2286 DEBUG2(printk("scsi(%ld): Register FC-4 "
2287 "TYPE failed.\n", ha->host_no));
2288 }
2289 if (qla2x00_rff_id(ha)) {
2290 /* EMPTY */
2291 DEBUG2(printk("scsi(%ld): Register FC-4 "
2292 "Features failed.\n", ha->host_no));
2293 }
2294 if (qla2x00_rnn_id(ha)) {
2295 /* EMPTY */
2296 DEBUG2(printk("scsi(%ld): Register Node Name "
2297 "failed.\n", ha->host_no));
2298 } else if (qla2x00_rsnn_nn(ha)) {
2299 /* EMPTY */
2300 DEBUG2(printk("scsi(%ld): Register Symbolic "
2301 "Node Name failed.\n", ha->host_no));
2302 }
2303 }
2304
2305 rval = qla2x00_find_all_fabric_devs(ha, &new_fcports);
2306 if (rval != QLA_SUCCESS)
2307 break;
2308
2309 /*
2310 * Logout all previous fabric devices marked lost, except
2311 * tape devices.
2312 */
2313 list_for_each_entry(fcport, &ha->fcports, list) {
2314 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2315 break;
2316
2317 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0)
2318 continue;
2319
2320 if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
2321 qla2x00_mark_device_lost(ha, fcport,
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002322 ql2xplogiabsentdevice, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 if (fcport->loop_id != FC_NO_LOOP_ID &&
2324 (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2325 fcport->port_type != FCT_INITIATOR &&
2326 fcport->port_type != FCT_BROADCAST) {
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002327 ha->isp_ops.fabric_logout(ha,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07002328 fcport->loop_id,
2329 fcport->d_id.b.domain,
2330 fcport->d_id.b.area,
2331 fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 fcport->loop_id = FC_NO_LOOP_ID;
2333 }
2334 }
2335 }
2336
2337 /* Starting free loop ID. */
2338 next_loopid = ha->min_external_loopid;
2339
2340 /*
2341 * Scan through our port list and login entries that need to be
2342 * logged in.
2343 */
2344 list_for_each_entry(fcport, &ha->fcports, list) {
2345 if (atomic_read(&ha->loop_down_timer) ||
2346 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2347 break;
2348
2349 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2350 (fcport->flags & FCF_LOGIN_NEEDED) == 0)
2351 continue;
2352
2353 if (fcport->loop_id == FC_NO_LOOP_ID) {
2354 fcport->loop_id = next_loopid;
2355 rval = qla2x00_find_new_loop_id(ha, fcport);
2356 if (rval != QLA_SUCCESS) {
2357 /* Ran out of IDs to use */
2358 break;
2359 }
2360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 /* Login and update database */
2362 qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
2363 }
2364
2365 /* Exit if out of loop IDs. */
2366 if (rval != QLA_SUCCESS) {
2367 break;
2368 }
2369
2370 /*
2371 * Login and add the new devices to our port list.
2372 */
2373 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2374 if (atomic_read(&ha->loop_down_timer) ||
2375 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2376 break;
2377
2378 /* Find a new loop ID to use. */
2379 fcport->loop_id = next_loopid;
2380 rval = qla2x00_find_new_loop_id(ha, fcport);
2381 if (rval != QLA_SUCCESS) {
2382 /* Ran out of IDs to use */
2383 break;
2384 }
2385
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 /* Remove device from the new list and add it to DB */
Akinobu Mita179e0912006-06-26 00:24:41 -07002387 list_move_tail(&fcport->list, &ha->fcports);
bdf79622005-04-17 15:06:53 -05002388
2389 /* Login and update database */
2390 qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 }
2392 } while (0);
2393
2394 /* Free all new device structures not processed. */
2395 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2396 list_del(&fcport->list);
2397 kfree(fcport);
2398 }
2399
2400 if (rval) {
2401 DEBUG2(printk("scsi(%ld): Configure fabric error exit: "
2402 "rval=%d\n", ha->host_no, rval));
2403 }
2404
2405 return (rval);
2406}
2407
2408
2409/*
2410 * qla2x00_find_all_fabric_devs
2411 *
2412 * Input:
2413 * ha = adapter block pointer.
2414 * dev = database device entry pointer.
2415 *
2416 * Returns:
2417 * 0 = success.
2418 *
2419 * Context:
2420 * Kernel context.
2421 */
2422static int
2423qla2x00_find_all_fabric_devs(scsi_qla_host_t *ha, struct list_head *new_fcports)
2424{
2425 int rval;
2426 uint16_t loop_id;
2427 fc_port_t *fcport, *new_fcport, *fcptemp;
2428 int found;
2429
2430 sw_info_t *swl;
2431 int swl_idx;
2432 int first_dev, last_dev;
2433 port_id_t wrap, nxt_d_id;
2434
2435 rval = QLA_SUCCESS;
2436
2437 /* Try GID_PT to get device list, else GAN. */
2438 swl = kmalloc(sizeof(sw_info_t) * MAX_FIBRE_DEVICES, GFP_ATOMIC);
2439 if (swl == NULL) {
2440 /*EMPTY*/
2441 DEBUG2(printk("scsi(%ld): GID_PT allocations failed, fallback "
2442 "on GA_NXT\n", ha->host_no));
2443 } else {
2444 memset(swl, 0, sizeof(sw_info_t) * MAX_FIBRE_DEVICES);
2445 if (qla2x00_gid_pt(ha, swl) != QLA_SUCCESS) {
2446 kfree(swl);
2447 swl = NULL;
2448 } else if (qla2x00_gpn_id(ha, swl) != QLA_SUCCESS) {
2449 kfree(swl);
2450 swl = NULL;
2451 } else if (qla2x00_gnn_id(ha, swl) != QLA_SUCCESS) {
2452 kfree(swl);
2453 swl = NULL;
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002454 } else if (qla2x00_gfpn_id(ha, swl) == QLA_SUCCESS) {
2455 qla2x00_gpsc(ha, swl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 }
2457 }
2458 swl_idx = 0;
2459
2460 /* Allocate temporary fcport for any new fcports discovered. */
2461 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2462 if (new_fcport == NULL) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002463 kfree(swl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 return (QLA_MEMORY_ALLOC_FAILED);
2465 }
2466 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2467
2468 /* Set start port ID scan at adapter ID. */
2469 first_dev = 1;
2470 last_dev = 0;
2471
2472 /* Starting free loop ID. */
2473 loop_id = ha->min_external_loopid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 for (; loop_id <= ha->last_loop_id; loop_id++) {
Andrew Vasquez3d716442005-07-06 10:30:26 -07002475 if (qla2x00_is_reserved_id(ha, loop_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 continue;
2477
Andrew Vasquez23443b12005-12-06 10:57:06 -08002478 if (atomic_read(&ha->loop_down_timer) || LOOP_TRANSITION(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 break;
2480
2481 if (swl != NULL) {
2482 if (last_dev) {
2483 wrap.b24 = new_fcport->d_id.b24;
2484 } else {
2485 new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
2486 memcpy(new_fcport->node_name,
2487 swl[swl_idx].node_name, WWN_SIZE);
2488 memcpy(new_fcport->port_name,
2489 swl[swl_idx].port_name, WWN_SIZE);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002490 memcpy(new_fcport->fabric_port_name,
2491 swl[swl_idx].fabric_port_name, WWN_SIZE);
2492 new_fcport->fp_speed = swl[swl_idx].fp_speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
2494 if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
2495 last_dev = 1;
2496 }
2497 swl_idx++;
2498 }
2499 } else {
2500 /* Send GA_NXT to the switch */
2501 rval = qla2x00_ga_nxt(ha, new_fcport);
2502 if (rval != QLA_SUCCESS) {
2503 qla_printk(KERN_WARNING, ha,
2504 "SNS scan failed -- assuming zero-entry "
2505 "result...\n");
2506 list_for_each_entry_safe(fcport, fcptemp,
2507 new_fcports, list) {
2508 list_del(&fcport->list);
2509 kfree(fcport);
2510 }
2511 rval = QLA_SUCCESS;
2512 break;
2513 }
2514 }
2515
2516 /* If wrap on switch device list, exit. */
2517 if (first_dev) {
2518 wrap.b24 = new_fcport->d_id.b24;
2519 first_dev = 0;
2520 } else if (new_fcport->d_id.b24 == wrap.b24) {
2521 DEBUG2(printk("scsi(%ld): device wrap (%02x%02x%02x)\n",
2522 ha->host_no, new_fcport->d_id.b.domain,
2523 new_fcport->d_id.b.area, new_fcport->d_id.b.al_pa));
2524 break;
2525 }
2526
2527 /* Bypass if host adapter. */
2528 if (new_fcport->d_id.b24 == ha->d_id.b24)
2529 continue;
2530
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07002531 /* Bypass if same domain and area of adapter. */
2532 if (((new_fcport->d_id.b24 & 0xffff00) ==
2533 (ha->d_id.b24 & 0xffff00)) && ha->current_topology ==
2534 ISP_CFG_FL)
2535 continue;
2536
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 /* Bypass reserved domain fields. */
2538 if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
2539 continue;
2540
2541 /* Locate matching device in database. */
2542 found = 0;
2543 list_for_each_entry(fcport, &ha->fcports, list) {
2544 if (memcmp(new_fcport->port_name, fcport->port_name,
2545 WWN_SIZE))
2546 continue;
2547
2548 found++;
2549
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002550 /* Update port state. */
2551 memcpy(fcport->fabric_port_name,
2552 new_fcport->fabric_port_name, WWN_SIZE);
2553 fcport->fp_speed = new_fcport->fp_speed;
2554
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 /*
2556 * If address the same and state FCS_ONLINE, nothing
2557 * changed.
2558 */
2559 if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
2560 atomic_read(&fcport->state) == FCS_ONLINE) {
2561 break;
2562 }
2563
2564 /*
2565 * If device was not a fabric device before.
2566 */
2567 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
2568 fcport->d_id.b24 = new_fcport->d_id.b24;
2569 fcport->loop_id = FC_NO_LOOP_ID;
2570 fcport->flags |= (FCF_FABRIC_DEVICE |
2571 FCF_LOGIN_NEEDED);
2572 fcport->flags &= ~FCF_PERSISTENT_BOUND;
2573 break;
2574 }
2575
2576 /*
2577 * Port ID changed or device was marked to be updated;
2578 * Log it out if still logged in and mark it for
2579 * relogin later.
2580 */
2581 fcport->d_id.b24 = new_fcport->d_id.b24;
2582 fcport->flags |= FCF_LOGIN_NEEDED;
2583 if (fcport->loop_id != FC_NO_LOOP_ID &&
2584 (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2585 fcport->port_type != FCT_INITIATOR &&
2586 fcport->port_type != FCT_BROADCAST) {
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07002587 ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2588 fcport->d_id.b.domain, fcport->d_id.b.area,
2589 fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 fcport->loop_id = FC_NO_LOOP_ID;
2591 }
2592
2593 break;
2594 }
2595
2596 if (found)
2597 continue;
2598
2599 /* If device was not in our fcports list, then add it. */
2600 list_add_tail(&new_fcport->list, new_fcports);
2601
2602 /* Allocate a new replacement fcport. */
2603 nxt_d_id.b24 = new_fcport->d_id.b24;
2604 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2605 if (new_fcport == NULL) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002606 kfree(swl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 return (QLA_MEMORY_ALLOC_FAILED);
2608 }
2609 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2610 new_fcport->d_id.b24 = nxt_d_id.b24;
2611 }
2612
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002613 kfree(swl);
2614 kfree(new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
2616 if (!list_empty(new_fcports))
2617 ha->device_flags |= DFLG_FABRIC_DEVICES;
2618
2619 return (rval);
2620}
2621
2622/*
2623 * qla2x00_find_new_loop_id
2624 * Scan through our port list and find a new usable loop ID.
2625 *
2626 * Input:
2627 * ha: adapter state pointer.
2628 * dev: port structure pointer.
2629 *
2630 * Returns:
2631 * qla2x00 local function return status code.
2632 *
2633 * Context:
2634 * Kernel context.
2635 */
Adrian Bunk413975a2006-06-30 02:33:06 -07002636static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637qla2x00_find_new_loop_id(scsi_qla_host_t *ha, fc_port_t *dev)
2638{
2639 int rval;
2640 int found;
2641 fc_port_t *fcport;
2642 uint16_t first_loop_id;
2643
2644 rval = QLA_SUCCESS;
2645
2646 /* Save starting loop ID. */
2647 first_loop_id = dev->loop_id;
2648
2649 for (;;) {
2650 /* Skip loop ID if already used by adapter. */
2651 if (dev->loop_id == ha->loop_id) {
2652 dev->loop_id++;
2653 }
2654
2655 /* Skip reserved loop IDs. */
Andrew Vasquez3d716442005-07-06 10:30:26 -07002656 while (qla2x00_is_reserved_id(ha, dev->loop_id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 dev->loop_id++;
2658 }
2659
2660 /* Reset loop ID if passed the end. */
2661 if (dev->loop_id > ha->last_loop_id) {
2662 /* first loop ID. */
2663 dev->loop_id = ha->min_external_loopid;
2664 }
2665
2666 /* Check for loop ID being already in use. */
2667 found = 0;
2668 fcport = NULL;
2669 list_for_each_entry(fcport, &ha->fcports, list) {
2670 if (fcport->loop_id == dev->loop_id && fcport != dev) {
2671 /* ID possibly in use */
2672 found++;
2673 break;
2674 }
2675 }
2676
2677 /* If not in use then it is free to use. */
2678 if (!found) {
2679 break;
2680 }
2681
2682 /* ID in use. Try next value. */
2683 dev->loop_id++;
2684
2685 /* If wrap around. No free ID to use. */
2686 if (dev->loop_id == first_loop_id) {
2687 dev->loop_id = FC_NO_LOOP_ID;
2688 rval = QLA_FUNCTION_FAILED;
2689 break;
2690 }
2691 }
2692
2693 return (rval);
2694}
2695
2696/*
2697 * qla2x00_device_resync
2698 * Marks devices in the database that needs resynchronization.
2699 *
2700 * Input:
2701 * ha = adapter block pointer.
2702 *
2703 * Context:
2704 * Kernel context.
2705 */
2706static int
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002707qla2x00_device_resync(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708{
2709 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 uint32_t mask;
2711 fc_port_t *fcport;
2712 uint32_t rscn_entry;
2713 uint8_t rscn_out_iter;
2714 uint8_t format;
2715 port_id_t d_id;
2716
2717 rval = QLA_RSCNS_HANDLED;
2718
2719 while (ha->rscn_out_ptr != ha->rscn_in_ptr ||
2720 ha->flags.rscn_queue_overflow) {
2721
2722 rscn_entry = ha->rscn_queue[ha->rscn_out_ptr];
2723 format = MSB(MSW(rscn_entry));
2724 d_id.b.domain = LSB(MSW(rscn_entry));
2725 d_id.b.area = MSB(LSW(rscn_entry));
2726 d_id.b.al_pa = LSB(LSW(rscn_entry));
2727
2728 DEBUG(printk("scsi(%ld): RSCN queue entry[%d] = "
2729 "[%02x/%02x%02x%02x].\n",
2730 ha->host_no, ha->rscn_out_ptr, format, d_id.b.domain,
2731 d_id.b.area, d_id.b.al_pa));
2732
2733 ha->rscn_out_ptr++;
2734 if (ha->rscn_out_ptr == MAX_RSCN_COUNT)
2735 ha->rscn_out_ptr = 0;
2736
2737 /* Skip duplicate entries. */
2738 for (rscn_out_iter = ha->rscn_out_ptr;
2739 !ha->flags.rscn_queue_overflow &&
2740 rscn_out_iter != ha->rscn_in_ptr;
2741 rscn_out_iter = (rscn_out_iter ==
2742 (MAX_RSCN_COUNT - 1)) ? 0: rscn_out_iter + 1) {
2743
2744 if (rscn_entry != ha->rscn_queue[rscn_out_iter])
2745 break;
2746
2747 DEBUG(printk("scsi(%ld): Skipping duplicate RSCN queue "
2748 "entry found at [%d].\n", ha->host_no,
2749 rscn_out_iter));
2750
2751 ha->rscn_out_ptr = rscn_out_iter;
2752 }
2753
2754 /* Queue overflow, set switch default case. */
2755 if (ha->flags.rscn_queue_overflow) {
2756 DEBUG(printk("scsi(%ld): device_resync: rscn "
2757 "overflow.\n", ha->host_no));
2758
2759 format = 3;
2760 ha->flags.rscn_queue_overflow = 0;
2761 }
2762
2763 switch (format) {
2764 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 mask = 0xffffff;
2766 break;
2767 case 1:
2768 mask = 0xffff00;
2769 break;
2770 case 2:
2771 mask = 0xff0000;
2772 break;
2773 default:
2774 mask = 0x0;
2775 d_id.b24 = 0;
2776 ha->rscn_out_ptr = ha->rscn_in_ptr;
2777 break;
2778 }
2779
2780 rval = QLA_SUCCESS;
2781
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 list_for_each_entry(fcport, &ha->fcports, list) {
2783 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2784 (fcport->d_id.b24 & mask) != d_id.b24 ||
2785 fcport->port_type == FCT_BROADCAST)
2786 continue;
2787
2788 if (atomic_read(&fcport->state) == FCS_ONLINE) {
2789 if (format != 3 ||
2790 fcport->port_type != FCT_INITIATOR) {
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002791 qla2x00_mark_device_lost(ha, fcport,
2792 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 }
2794 }
2795 fcport->flags &= ~FCF_FARP_DONE;
2796 }
2797 }
2798 return (rval);
2799}
2800
2801/*
2802 * qla2x00_fabric_dev_login
2803 * Login fabric target device and update FC port database.
2804 *
2805 * Input:
2806 * ha: adapter state pointer.
2807 * fcport: port structure list pointer.
2808 * next_loopid: contains value of a new loop ID that can be used
2809 * by the next login attempt.
2810 *
2811 * Returns:
2812 * qla2x00 local function return status code.
2813 *
2814 * Context:
2815 * Kernel context.
2816 */
2817static int
2818qla2x00_fabric_dev_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2819 uint16_t *next_loopid)
2820{
2821 int rval;
2822 int retry;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002823 uint8_t opts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824
2825 rval = QLA_SUCCESS;
2826 retry = 0;
2827
2828 rval = qla2x00_fabric_login(ha, fcport, next_loopid);
2829 if (rval == QLA_SUCCESS) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002830 /* Send an ADISC to tape devices.*/
2831 opts = 0;
2832 if (fcport->flags & FCF_TAPE_PRESENT)
2833 opts |= BIT_1;
2834 rval = qla2x00_get_port_database(ha, fcport, opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 if (rval != QLA_SUCCESS) {
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07002836 ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2837 fcport->d_id.b.domain, fcport->d_id.b.area,
2838 fcport->d_id.b.al_pa);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002839 qla2x00_mark_device_lost(ha, fcport, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 } else {
2841 qla2x00_update_fcport(ha, fcport);
2842 }
2843 }
2844
2845 return (rval);
2846}
2847
2848/*
2849 * qla2x00_fabric_login
2850 * Issue fabric login command.
2851 *
2852 * Input:
2853 * ha = adapter block pointer.
2854 * device = pointer to FC device type structure.
2855 *
2856 * Returns:
2857 * 0 - Login successfully
2858 * 1 - Login failed
2859 * 2 - Initiator device
2860 * 3 - Fatal error
2861 */
2862int
2863qla2x00_fabric_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2864 uint16_t *next_loopid)
2865{
2866 int rval;
2867 int retry;
2868 uint16_t tmp_loopid;
2869 uint16_t mb[MAILBOX_REGISTER_COUNT];
2870
2871 retry = 0;
2872 tmp_loopid = 0;
2873
2874 for (;;) {
2875 DEBUG(printk("scsi(%ld): Trying Fabric Login w/loop id 0x%04x "
2876 "for port %02x%02x%02x.\n",
2877 ha->host_no, fcport->loop_id, fcport->d_id.b.domain,
2878 fcport->d_id.b.area, fcport->d_id.b.al_pa));
2879
2880 /* Login fcport on switch. */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002881 ha->isp_ops.fabric_login(ha, fcport->loop_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 fcport->d_id.b.domain, fcport->d_id.b.area,
2883 fcport->d_id.b.al_pa, mb, BIT_0);
2884 if (mb[0] == MBS_PORT_ID_USED) {
2885 /*
2886 * Device has another loop ID. The firmware team
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002887 * recommends the driver perform an implicit login with
2888 * the specified ID again. The ID we just used is save
2889 * here so we return with an ID that can be tried by
2890 * the next login.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 */
2892 retry++;
2893 tmp_loopid = fcport->loop_id;
2894 fcport->loop_id = mb[1];
2895
2896 DEBUG(printk("Fabric Login: port in use - next "
2897 "loop id=0x%04x, port Id=%02x%02x%02x.\n",
2898 fcport->loop_id, fcport->d_id.b.domain,
2899 fcport->d_id.b.area, fcport->d_id.b.al_pa));
2900
2901 } else if (mb[0] == MBS_COMMAND_COMPLETE) {
2902 /*
2903 * Login succeeded.
2904 */
2905 if (retry) {
2906 /* A retry occurred before. */
2907 *next_loopid = tmp_loopid;
2908 } else {
2909 /*
2910 * No retry occurred before. Just increment the
2911 * ID value for next login.
2912 */
2913 *next_loopid = (fcport->loop_id + 1);
2914 }
2915
2916 if (mb[1] & BIT_0) {
2917 fcport->port_type = FCT_INITIATOR;
2918 } else {
2919 fcport->port_type = FCT_TARGET;
2920 if (mb[1] & BIT_1) {
2921 fcport->flags |= FCF_TAPE_PRESENT;
2922 }
2923 }
2924
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07002925 if (mb[10] & BIT_0)
2926 fcport->supported_classes |= FC_COS_CLASS2;
2927 if (mb[10] & BIT_1)
2928 fcport->supported_classes |= FC_COS_CLASS3;
2929
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 rval = QLA_SUCCESS;
2931 break;
2932 } else if (mb[0] == MBS_LOOP_ID_USED) {
2933 /*
2934 * Loop ID already used, try next loop ID.
2935 */
2936 fcport->loop_id++;
2937 rval = qla2x00_find_new_loop_id(ha, fcport);
2938 if (rval != QLA_SUCCESS) {
2939 /* Ran out of loop IDs to use */
2940 break;
2941 }
2942 } else if (mb[0] == MBS_COMMAND_ERROR) {
2943 /*
2944 * Firmware possibly timed out during login. If NO
2945 * retries are left to do then the device is declared
2946 * dead.
2947 */
2948 *next_loopid = fcport->loop_id;
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07002949 ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2950 fcport->d_id.b.domain, fcport->d_id.b.area,
2951 fcport->d_id.b.al_pa);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002952 qla2x00_mark_device_lost(ha, fcport, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
2954 rval = 1;
2955 break;
2956 } else {
2957 /*
2958 * unrecoverable / not handled error
2959 */
2960 DEBUG2(printk("%s(%ld): failed=%x port_id=%02x%02x%02x "
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002961 "loop_id=%x jiffies=%lx.\n",
2962 __func__, ha->host_no, mb[0],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 fcport->d_id.b.domain, fcport->d_id.b.area,
2964 fcport->d_id.b.al_pa, fcport->loop_id, jiffies));
2965
2966 *next_loopid = fcport->loop_id;
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07002967 ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2968 fcport->d_id.b.domain, fcport->d_id.b.area,
2969 fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 fcport->loop_id = FC_NO_LOOP_ID;
Andrew Vasquez0eedfcf2005-10-27 11:09:38 -07002971 fcport->login_retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
2973 rval = 3;
2974 break;
2975 }
2976 }
2977
2978 return (rval);
2979}
2980
2981/*
2982 * qla2x00_local_device_login
2983 * Issue local device login command.
2984 *
2985 * Input:
2986 * ha = adapter block pointer.
2987 * loop_id = loop id of device to login to.
2988 *
2989 * Returns (Where's the #define!!!!):
2990 * 0 - Login successfully
2991 * 1 - Login failed
2992 * 3 - Fatal error
2993 */
2994int
andrew.vasquez@qlogic.com9a52a572006-03-09 14:27:44 -08002995qla2x00_local_device_login(scsi_qla_host_t *ha, fc_port_t *fcport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996{
2997 int rval;
2998 uint16_t mb[MAILBOX_REGISTER_COUNT];
2999
3000 memset(mb, 0, sizeof(mb));
andrew.vasquez@qlogic.com9a52a572006-03-09 14:27:44 -08003001 rval = qla2x00_login_local_device(ha, fcport, mb, BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 if (rval == QLA_SUCCESS) {
3003 /* Interrogate mailbox registers for any errors */
3004 if (mb[0] == MBS_COMMAND_ERROR)
3005 rval = 1;
3006 else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
3007 /* device not in PCB table */
3008 rval = 3;
3009 }
3010
3011 return (rval);
3012}
3013
3014/*
3015 * qla2x00_loop_resync
3016 * Resync with fibre channel devices.
3017 *
3018 * Input:
3019 * ha = adapter block pointer.
3020 *
3021 * Returns:
3022 * 0 = success
3023 */
3024int
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003025qla2x00_loop_resync(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026{
3027 int rval;
3028 uint32_t wait_time;
3029
3030 rval = QLA_SUCCESS;
3031
3032 atomic_set(&ha->loop_state, LOOP_UPDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3034 if (ha->flags.online) {
3035 if (!(rval = qla2x00_fw_ready(ha))) {
3036 /* Wait at most MAX_TARGET RSCNs for a stable link. */
3037 wait_time = 256;
3038 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 atomic_set(&ha->loop_state, LOOP_UPDATE);
3040
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003041 /* Issue a marker after FW becomes ready. */
3042 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
3043 ha->marker_needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044
3045 /* Remap devices on Loop. */
3046 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
3047
3048 qla2x00_configure_loop(ha);
3049 wait_time--;
3050 } while (!atomic_read(&ha->loop_down_timer) &&
3051 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
3052 wait_time &&
3053 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
3054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 }
3056
3057 if (test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
3058 return (QLA_FUNCTION_FAILED);
3059 }
3060
3061 if (rval) {
3062 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
3063 }
3064
3065 return (rval);
3066}
3067
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068void
3069qla2x00_rescan_fcports(scsi_qla_host_t *ha)
3070{
3071 int rescan_done;
3072 fc_port_t *fcport;
3073
3074 rescan_done = 0;
3075 list_for_each_entry(fcport, &ha->fcports, list) {
3076 if ((fcport->flags & FCF_RESCAN_NEEDED) == 0)
3077 continue;
3078
3079 qla2x00_update_fcport(ha, fcport);
3080 fcport->flags &= ~FCF_RESCAN_NEEDED;
3081
3082 rescan_done = 1;
3083 }
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003084 qla2x00_probe_for_all_luns(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085}
3086
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08003087void
3088qla2x00_update_fcports(scsi_qla_host_t *ha)
3089{
3090 fc_port_t *fcport;
3091
3092 /* Go with deferred removal of rport references. */
3093 list_for_each_entry(fcport, &ha->fcports, list)
3094 if (fcport->drport)
3095 qla2x00_rport_del(fcport);
3096}
3097
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098/*
3099* qla2x00_abort_isp
3100* Resets ISP and aborts all outstanding commands.
3101*
3102* Input:
3103* ha = adapter block pointer.
3104*
3105* Returns:
3106* 0 = success
3107*/
3108int
3109qla2x00_abort_isp(scsi_qla_host_t *ha)
3110{
Andrew Vasquez476e8972006-08-23 14:54:55 -07003111 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 unsigned long flags = 0;
3113 uint16_t cnt;
3114 srb_t *sp;
3115 uint8_t status = 0;
3116
3117 if (ha->flags.online) {
3118 ha->flags.online = 0;
3119 clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120
3121 qla_printk(KERN_INFO, ha,
3122 "Performing ISP error recovery - ha= %p.\n", ha);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003123 ha->isp_ops.reset_chip(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124
3125 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
3126 if (atomic_read(&ha->loop_state) != LOOP_DOWN) {
3127 atomic_set(&ha->loop_state, LOOP_DOWN);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08003128 qla2x00_mark_all_devices_lost(ha, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 } else {
3130 if (!atomic_read(&ha->loop_down_timer))
3131 atomic_set(&ha->loop_down_timer,
3132 LOOP_DOWN_TIME);
3133 }
3134
3135 spin_lock_irqsave(&ha->hardware_lock, flags);
3136 /* Requeue all commands in outstanding command list. */
3137 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
3138 sp = ha->outstanding_cmds[cnt];
3139 if (sp) {
3140 ha->outstanding_cmds[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 sp->flags = 0;
f4f051e2005-04-17 15:02:26 -05003142 sp->cmd->result = DID_RESET << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 sp->cmd->host_scribble = (unsigned char *)NULL;
f4f051e2005-04-17 15:02:26 -05003144 qla2x00_sp_compl(ha, sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 }
3146 }
3147 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3148
Andrew Vasquez30c47662007-01-29 10:22:21 -08003149 ha->isp_ops.get_flash_version(ha, ha->request_ring);
3150
David Miller4e08df32007-04-16 12:37:43 -07003151 ha->isp_ops.nvram_config(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152
3153 if (!qla2x00_restart_isp(ha)) {
3154 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3155
3156 if (!atomic_read(&ha->loop_down_timer)) {
3157 /*
3158 * Issue marker command only when we are going
3159 * to start the I/O .
3160 */
3161 ha->marker_needed = 1;
3162 }
3163
3164 ha->flags.online = 1;
3165
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003166 ha->isp_ops.enable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003168 ha->isp_abort_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
Andrew Vasquez476e8972006-08-23 14:54:55 -07003170
3171 if (ha->eft) {
3172 rval = qla2x00_trace_control(ha, TC_ENABLE,
3173 ha->eft_dma, EFT_NUM_BUFFERS);
3174 if (rval) {
3175 qla_printk(KERN_WARNING, ha,
3176 "Unable to reinitialize EFT "
3177 "(%d).\n", rval);
3178 }
3179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 } else { /* failed the ISP abort */
3181 ha->flags.online = 1;
3182 if (test_bit(ISP_ABORT_RETRY, &ha->dpc_flags)) {
3183 if (ha->isp_abort_cnt == 0) {
3184 qla_printk(KERN_WARNING, ha,
3185 "ISP error recovery failed - "
3186 "board disabled\n");
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003187 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 * The next call disables the board
3189 * completely.
3190 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003191 ha->isp_ops.reset_adapter(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 ha->flags.online = 0;
3193 clear_bit(ISP_ABORT_RETRY,
3194 &ha->dpc_flags);
3195 status = 0;
3196 } else { /* schedule another ISP abort */
3197 ha->isp_abort_cnt--;
3198 DEBUG(printk("qla%ld: ISP abort - "
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003199 "retry remaining %d\n",
Andrew Vasquez744f11fd2006-06-23 16:11:05 -07003200 ha->host_no, ha->isp_abort_cnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 status = 1;
3202 }
3203 } else {
3204 ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
3205 DEBUG(printk("qla2x00(%ld): ISP error recovery "
3206 "- retrying (%d) more times\n",
Andrew Vasquez744f11fd2006-06-23 16:11:05 -07003207 ha->host_no, ha->isp_abort_cnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 set_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3209 status = 1;
3210 }
3211 }
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003212
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 }
3214
3215 if (status) {
3216 qla_printk(KERN_INFO, ha,
3217 "qla2x00_abort_isp: **** FAILED ****\n");
3218 } else {
3219 DEBUG(printk(KERN_INFO
3220 "qla2x00_abort_isp(%ld): exiting.\n",
Andrew Vasquez744f11fd2006-06-23 16:11:05 -07003221 ha->host_no));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 }
3223
3224 return(status);
3225}
3226
3227/*
3228* qla2x00_restart_isp
3229* restarts the ISP after a reset
3230*
3231* Input:
3232* ha = adapter block pointer.
3233*
3234* Returns:
3235* 0 = success
3236*/
3237static int
3238qla2x00_restart_isp(scsi_qla_host_t *ha)
3239{
3240 uint8_t status = 0;
Andrew Vasquez3d716442005-07-06 10:30:26 -07003241 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 unsigned long flags = 0;
3243 uint32_t wait_time;
3244
3245 /* If firmware needs to be loaded */
3246 if (qla2x00_isp_firmware(ha)) {
3247 ha->flags.online = 0;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003248 if (!(status = ha->isp_ops.chip_diag(ha))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
3250 status = qla2x00_setup_chip(ha);
3251 goto done;
3252 }
3253
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 spin_lock_irqsave(&ha->hardware_lock, flags);
3255
andrew.vasquez@qlogic.com044cc6c2006-03-09 14:27:13 -08003256 if (!IS_QLA24XX(ha) && !IS_QLA54XX(ha)) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003257 /*
3258 * Disable SRAM, Instruction RAM and GP RAM
3259 * parity.
3260 */
3261 WRT_REG_WORD(&reg->hccr,
3262 (HCCR_ENABLE_PARITY + 0x0));
3263 RD_REG_WORD(&reg->hccr);
3264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265
3266 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003267
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 status = qla2x00_setup_chip(ha);
3269
3270 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003271
andrew.vasquez@qlogic.com044cc6c2006-03-09 14:27:13 -08003272 if (!IS_QLA24XX(ha) && !IS_QLA54XX(ha)) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003273 /* Enable proper parity */
3274 if (IS_QLA2300(ha))
3275 /* SRAM parity */
3276 WRT_REG_WORD(&reg->hccr,
3277 (HCCR_ENABLE_PARITY + 0x1));
3278 else
3279 /*
3280 * SRAM, Instruction RAM and GP RAM
3281 * parity.
3282 */
3283 WRT_REG_WORD(&reg->hccr,
3284 (HCCR_ENABLE_PARITY + 0x7));
3285 RD_REG_WORD(&reg->hccr);
3286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287
3288 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3289 }
3290 }
3291
3292 done:
3293 if (!status && !(status = qla2x00_init_rings(ha))) {
3294 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3295 if (!(status = qla2x00_fw_ready(ha))) {
3296 DEBUG(printk("%s(): Start configure loop, "
Andrew Vasquez744f11fd2006-06-23 16:11:05 -07003297 "status = %d\n", __func__, status));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003298
3299 /* Issue a marker after FW becomes ready. */
3300 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
3301
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 ha->flags.online = 1;
3303 /* Wait at most MAX_TARGET RSCNs for a stable link. */
3304 wait_time = 256;
3305 do {
3306 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
3307 qla2x00_configure_loop(ha);
3308 wait_time--;
3309 } while (!atomic_read(&ha->loop_down_timer) &&
3310 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
3311 wait_time &&
3312 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
3313 }
3314
3315 /* if no cable then assume it's good */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003316 if ((ha->device_flags & DFLG_NO_CABLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 status = 0;
3318
3319 DEBUG(printk("%s(): Configure loop done, status = 0x%x\n",
3320 __func__,
Andrew Vasquez744f11fd2006-06-23 16:11:05 -07003321 status));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 }
3323 return (status);
3324}
3325
3326/*
3327* qla2x00_reset_adapter
3328* Reset adapter.
3329*
3330* Input:
3331* ha = adapter block pointer.
3332*/
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003333void
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334qla2x00_reset_adapter(scsi_qla_host_t *ha)
3335{
3336 unsigned long flags = 0;
Andrew Vasquez3d716442005-07-06 10:30:26 -07003337 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338
3339 ha->flags.online = 0;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003340 ha->isp_ops.disable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 spin_lock_irqsave(&ha->hardware_lock, flags);
3343 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
3344 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
3345 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
3346 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
3347 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3348}
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003349
3350void
3351qla24xx_reset_adapter(scsi_qla_host_t *ha)
3352{
3353 unsigned long flags = 0;
3354 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
3355
3356 ha->flags.online = 0;
3357 ha->isp_ops.disable_intrs(ha);
3358
3359 spin_lock_irqsave(&ha->hardware_lock, flags);
3360 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
3361 RD_REG_DWORD(&reg->hccr);
3362 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
3363 RD_REG_DWORD(&reg->hccr);
3364 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3365}
3366
David Miller4e08df32007-04-16 12:37:43 -07003367/* On sparc systems, obtain port and node WWN from firmware
3368 * properties.
3369 */
3370static void qla24xx_nvram_wwn_from_ofw(scsi_qla_host_t *ha, struct nvram_24xx *nv)
3371{
3372#ifdef CONFIG_SPARC
3373 struct pci_dev *pdev = ha->pdev;
David S. Miller15576bc2007-05-08 00:36:49 -07003374 struct device_node *dp = pci_device_to_OF_node(pdev);
3375 const u8 *val;
David Miller4e08df32007-04-16 12:37:43 -07003376 int len;
3377
3378 val = of_get_property(dp, "port-wwn", &len);
3379 if (val && len >= WWN_SIZE)
3380 memcpy(nv->port_name, val, WWN_SIZE);
3381
3382 val = of_get_property(dp, "node-wwn", &len);
3383 if (val && len >= WWN_SIZE)
3384 memcpy(nv->node_name, val, WWN_SIZE);
3385#endif
3386}
3387
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003388int
3389qla24xx_nvram_config(scsi_qla_host_t *ha)
3390{
David Miller4e08df32007-04-16 12:37:43 -07003391 int rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003392 struct init_cb_24xx *icb;
3393 struct nvram_24xx *nv;
3394 uint32_t *dptr;
3395 uint8_t *dptr1, *dptr2;
3396 uint32_t chksum;
3397 uint16_t cnt;
3398
David Miller4e08df32007-04-16 12:37:43 -07003399 rval = QLA_SUCCESS;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003400 icb = (struct init_cb_24xx *)ha->init_cb;
3401 nv = (struct nvram_24xx *)ha->request_ring;
3402
3403 /* Determine NVRAM starting address. */
3404 ha->nvram_size = sizeof(struct nvram_24xx);
3405 ha->nvram_base = FA_NVRAM_FUNC0_ADDR;
andrew.vasquez@qlogic.com6f641792006-03-09 14:27:34 -08003406 ha->vpd_size = FA_NVRAM_VPD_SIZE;
3407 ha->vpd_base = FA_NVRAM_VPD0_ADDR;
3408 if (PCI_FUNC(ha->pdev->devfn)) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003409 ha->nvram_base = FA_NVRAM_FUNC1_ADDR;
andrew.vasquez@qlogic.com6f641792006-03-09 14:27:34 -08003410 ha->vpd_base = FA_NVRAM_VPD1_ADDR;
3411 }
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003412
3413 /* Get NVRAM data and calculate checksum. */
3414 dptr = (uint32_t *)nv;
3415 ha->isp_ops.read_nvram(ha, (uint8_t *)dptr, ha->nvram_base,
3416 ha->nvram_size);
3417 for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++)
3418 chksum += le32_to_cpu(*dptr++);
3419
3420 DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
3421 DEBUG5(qla2x00_dump_buffer((uint8_t *)ha->request_ring,
3422 ha->nvram_size));
3423
3424 /* Bad NVRAM data, set defaults parameters. */
3425 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
3426 || nv->id[3] != ' ' ||
3427 nv->nvram_version < __constant_cpu_to_le16(ICB_VERSION)) {
3428 /* Reset NVRAM data. */
3429 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
3430 "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
3431 le16_to_cpu(nv->nvram_version));
David Miller4e08df32007-04-16 12:37:43 -07003432 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
3433 "invalid -- WWPN) defaults.\n");
3434
3435 /*
3436 * Set default initialization control block.
3437 */
3438 memset(nv, 0, ha->nvram_size);
3439 nv->nvram_version = __constant_cpu_to_le16(ICB_VERSION);
3440 nv->version = __constant_cpu_to_le16(ICB_VERSION);
3441 nv->frame_payload_size = __constant_cpu_to_le16(2048);
3442 nv->execution_throttle = __constant_cpu_to_le16(0xFFFF);
3443 nv->exchange_count = __constant_cpu_to_le16(0);
3444 nv->hard_address = __constant_cpu_to_le16(124);
3445 nv->port_name[0] = 0x21;
3446 nv->port_name[1] = 0x00 + PCI_FUNC(ha->pdev->devfn);
3447 nv->port_name[2] = 0x00;
3448 nv->port_name[3] = 0xe0;
3449 nv->port_name[4] = 0x8b;
3450 nv->port_name[5] = 0x1c;
3451 nv->port_name[6] = 0x55;
3452 nv->port_name[7] = 0x86;
3453 nv->node_name[0] = 0x20;
3454 nv->node_name[1] = 0x00;
3455 nv->node_name[2] = 0x00;
3456 nv->node_name[3] = 0xe0;
3457 nv->node_name[4] = 0x8b;
3458 nv->node_name[5] = 0x1c;
3459 nv->node_name[6] = 0x55;
3460 nv->node_name[7] = 0x86;
3461 qla24xx_nvram_wwn_from_ofw(ha, nv);
3462 nv->login_retry_count = __constant_cpu_to_le16(8);
3463 nv->interrupt_delay_timer = __constant_cpu_to_le16(0);
3464 nv->login_timeout = __constant_cpu_to_le16(0);
3465 nv->firmware_options_1 =
3466 __constant_cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
3467 nv->firmware_options_2 = __constant_cpu_to_le32(2 << 4);
3468 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_12);
3469 nv->firmware_options_3 = __constant_cpu_to_le32(2 << 13);
3470 nv->host_p = __constant_cpu_to_le32(BIT_11|BIT_10);
3471 nv->efi_parameters = __constant_cpu_to_le32(0);
3472 nv->reset_delay = 5;
3473 nv->max_luns_per_target = __constant_cpu_to_le16(128);
3474 nv->port_down_retry_count = __constant_cpu_to_le16(30);
3475 nv->link_down_timeout = __constant_cpu_to_le16(30);
3476
3477 rval = 1;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003478 }
3479
3480 /* Reset Initialization control block */
3481 memset(icb, 0, sizeof(struct init_cb_24xx));
3482
3483 /* Copy 1st segment. */
3484 dptr1 = (uint8_t *)icb;
3485 dptr2 = (uint8_t *)&nv->version;
3486 cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
3487 while (cnt--)
3488 *dptr1++ = *dptr2++;
3489
3490 icb->login_retry_count = nv->login_retry_count;
Andrew Vasquez3ea66e22006-06-23 16:11:27 -07003491 icb->link_down_on_nos = nv->link_down_on_nos;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003492
3493 /* Copy 2nd segment. */
3494 dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
3495 dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
3496 cnt = (uint8_t *)&icb->reserved_3 -
3497 (uint8_t *)&icb->interrupt_delay_timer;
3498 while (cnt--)
3499 *dptr1++ = *dptr2++;
3500
3501 /*
3502 * Setup driver NVRAM options.
3503 */
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08003504 qla2x00_set_model_info(ha, nv->model_name, sizeof(nv->model_name),
3505 "QLA2462");
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003506
Andrew Vasquez5341e862006-05-17 15:09:16 -07003507 /* Use alternate WWN? */
3508 if (nv->host_p & __constant_cpu_to_le32(BIT_15)) {
3509 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
3510 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
3511 }
3512
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003513 /* Prepare nodename */
Andrew Vasquezfd0e7e42006-05-17 15:09:11 -07003514 if ((icb->firmware_options_1 & __constant_cpu_to_le32(BIT_14)) == 0) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003515 /*
3516 * Firmware will apply the following mask if the nodename was
3517 * not provided.
3518 */
3519 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
3520 icb->node_name[0] &= 0xF0;
3521 }
3522
3523 /* Set host adapter parameters. */
3524 ha->flags.disable_risc_code_load = 0;
Andrew Vasquez0c8c39a2006-12-13 19:20:30 -08003525 ha->flags.enable_lip_reset = 0;
3526 ha->flags.enable_lip_full_login =
3527 le32_to_cpu(nv->host_p) & BIT_10 ? 1: 0;
3528 ha->flags.enable_target_reset =
3529 le32_to_cpu(nv->host_p) & BIT_11 ? 1: 0;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003530 ha->flags.enable_led_scheme = 0;
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07003531 ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003532
Andrew Vasquezfd0e7e42006-05-17 15:09:11 -07003533 ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
3534 (BIT_6 | BIT_5 | BIT_4)) >> 4;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003535
3536 memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
3537 sizeof(ha->fw_seriallink_options24));
3538
3539 /* save HBA serial number */
3540 ha->serial0 = icb->port_name[5];
3541 ha->serial1 = icb->port_name[6];
3542 ha->serial2 = icb->port_name[7];
3543 ha->node_name = icb->node_name;
3544 ha->port_name = icb->port_name;
3545
andrew.vasquez@qlogic.combc8fb3c2006-01-13 17:05:42 -08003546 icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
3547
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003548 ha->retry_count = le16_to_cpu(nv->login_retry_count);
3549
3550 /* Set minimum login_timeout to 4 seconds. */
3551 if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
3552 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
3553 if (le16_to_cpu(nv->login_timeout) < 4)
3554 nv->login_timeout = __constant_cpu_to_le16(4);
3555 ha->login_timeout = le16_to_cpu(nv->login_timeout);
3556 icb->login_timeout = cpu_to_le16(nv->login_timeout);
3557
3558 /* Set minimum RATOV to 200 tenths of a second. */
3559 ha->r_a_tov = 200;
3560
3561 ha->loop_reset_delay = nv->reset_delay;
3562
3563 /* Link Down Timeout = 0:
3564 *
3565 * When Port Down timer expires we will start returning
3566 * I/O's to OS with "DID_NO_CONNECT".
3567 *
3568 * Link Down Timeout != 0:
3569 *
3570 * The driver waits for the link to come up after link down
3571 * before returning I/Os to OS with "DID_NO_CONNECT".
3572 */
3573 if (le16_to_cpu(nv->link_down_timeout) == 0) {
3574 ha->loop_down_abort_time =
3575 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
3576 } else {
3577 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
3578 ha->loop_down_abort_time =
3579 (LOOP_DOWN_TIME - ha->link_down_timeout);
3580 }
3581
3582 /* Need enough time to try and get the port back. */
3583 ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
3584 if (qlport_down_retry)
3585 ha->port_down_retry_count = qlport_down_retry;
3586
3587 /* Set login_retry_count */
3588 ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
3589 if (ha->port_down_retry_count ==
3590 le16_to_cpu(nv->port_down_retry_count) &&
3591 ha->port_down_retry_count > 3)
3592 ha->login_retry_count = ha->port_down_retry_count;
3593 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
3594 ha->login_retry_count = ha->port_down_retry_count;
3595 if (ql2xloginretrycount)
3596 ha->login_retry_count = ql2xloginretrycount;
3597
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07003598 /* Enable ZIO. */
3599 if (!ha->flags.init_done) {
3600 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
3601 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
3602 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
3603 le16_to_cpu(icb->interrupt_delay_timer): 2;
3604 }
3605 icb->firmware_options_2 &= __constant_cpu_to_le32(
3606 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
3607 ha->flags.process_response_queue = 0;
3608 if (ha->zio_mode != QLA_ZIO_DISABLED) {
andrew.vasquez@qlogic.com4a59f712006-03-09 14:27:39 -08003609 ha->zio_mode = QLA_ZIO_MODE_6;
3610
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07003611 DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer delay "
3612 "(%d us).\n", ha->host_no, ha->zio_mode,
3613 ha->zio_timer * 100));
3614 qla_printk(KERN_INFO, ha,
3615 "ZIO mode %d enabled; timer delay (%d us).\n",
3616 ha->zio_mode, ha->zio_timer * 100);
3617
3618 icb->firmware_options_2 |= cpu_to_le32(
3619 (uint32_t)ha->zio_mode);
3620 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
3621 ha->flags.process_response_queue = 1;
3622 }
3623
David Miller4e08df32007-04-16 12:37:43 -07003624 if (rval) {
3625 DEBUG2_3(printk(KERN_WARNING
3626 "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
3627 }
3628 return (rval);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003629}
3630
Adrian Bunk413975a2006-06-30 02:33:06 -07003631static int
Andrew Vasquezd1c61902006-05-17 15:09:00 -07003632qla24xx_load_risc_flash(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3633{
3634 int rval;
3635 int segments, fragment;
3636 uint32_t faddr;
3637 uint32_t *dcode, dlen;
3638 uint32_t risc_addr;
3639 uint32_t risc_size;
3640 uint32_t i;
3641
3642 rval = QLA_SUCCESS;
3643
3644 segments = FA_RISC_CODE_SEGMENTS;
3645 faddr = FA_RISC_CODE_ADDR;
3646 dcode = (uint32_t *)ha->request_ring;
3647 *srisc_addr = 0;
3648
3649 /* Validate firmware image by checking version. */
3650 qla24xx_read_flash_data(ha, dcode, faddr + 4, 4);
3651 for (i = 0; i < 4; i++)
3652 dcode[i] = be32_to_cpu(dcode[i]);
3653 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
3654 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
3655 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
3656 dcode[3] == 0)) {
3657 qla_printk(KERN_WARNING, ha,
3658 "Unable to verify integrity of flash firmware image!\n");
3659 qla_printk(KERN_WARNING, ha,
3660 "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
3661 dcode[1], dcode[2], dcode[3]);
3662
3663 return QLA_FUNCTION_FAILED;
3664 }
3665
3666 while (segments && rval == QLA_SUCCESS) {
3667 /* Read segment's load information. */
3668 qla24xx_read_flash_data(ha, dcode, faddr, 4);
3669
3670 risc_addr = be32_to_cpu(dcode[2]);
3671 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
3672 risc_size = be32_to_cpu(dcode[3]);
3673
3674 fragment = 0;
3675 while (risc_size > 0 && rval == QLA_SUCCESS) {
3676 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
3677 if (dlen > risc_size)
3678 dlen = risc_size;
3679
3680 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3681 "addr %x, number of dwords 0x%x, offset 0x%x.\n",
3682 ha->host_no, risc_addr, dlen, faddr));
3683
3684 qla24xx_read_flash_data(ha, dcode, faddr, dlen);
3685 for (i = 0; i < dlen; i++)
3686 dcode[i] = swab32(dcode[i]);
3687
3688 rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3689 dlen);
3690 if (rval) {
3691 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3692 "segment %d of firmware\n", ha->host_no,
3693 fragment));
3694 qla_printk(KERN_WARNING, ha,
3695 "[ERROR] Failed to load segment %d of "
3696 "firmware\n", fragment);
3697 break;
3698 }
3699
3700 faddr += dlen;
3701 risc_addr += dlen;
3702 risc_size -= dlen;
3703 fragment++;
3704 }
3705
3706 /* Next segment. */
3707 segments--;
3708 }
3709
3710 return rval;
3711}
3712
Andrew Vasquezd1c61902006-05-17 15:09:00 -07003713#define QLA_FW_URL "ftp://ftp.qlogic.com/outgoing/linux/firmware/"
3714
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003715int
Andrew Vasquez54333832005-11-09 15:49:04 -08003716qla2x00_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3717{
3718 int rval;
3719 int i, fragment;
3720 uint16_t *wcode, *fwcode;
3721 uint32_t risc_addr, risc_size, fwclen, wlen, *seg;
3722 struct fw_blob *blob;
3723
3724 /* Load firmware blob. */
3725 blob = qla2x00_request_firmware(ha);
3726 if (!blob) {
3727 qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
Andrew Vasquezd1c61902006-05-17 15:09:00 -07003728 qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
3729 "from: " QLA_FW_URL ".\n");
Andrew Vasquez54333832005-11-09 15:49:04 -08003730 return QLA_FUNCTION_FAILED;
3731 }
3732
3733 rval = QLA_SUCCESS;
3734
3735 wcode = (uint16_t *)ha->request_ring;
3736 *srisc_addr = 0;
3737 fwcode = (uint16_t *)blob->fw->data;
3738 fwclen = 0;
3739
3740 /* Validate firmware image by checking version. */
3741 if (blob->fw->size < 8 * sizeof(uint16_t)) {
3742 qla_printk(KERN_WARNING, ha,
3743 "Unable to verify integrity of firmware image (%Zd)!\n",
3744 blob->fw->size);
3745 goto fail_fw_integrity;
3746 }
3747 for (i = 0; i < 4; i++)
3748 wcode[i] = be16_to_cpu(fwcode[i + 4]);
3749 if ((wcode[0] == 0xffff && wcode[1] == 0xffff && wcode[2] == 0xffff &&
3750 wcode[3] == 0xffff) || (wcode[0] == 0 && wcode[1] == 0 &&
3751 wcode[2] == 0 && wcode[3] == 0)) {
3752 qla_printk(KERN_WARNING, ha,
3753 "Unable to verify integrity of firmware image!\n");
3754 qla_printk(KERN_WARNING, ha,
3755 "Firmware data: %04x %04x %04x %04x!\n", wcode[0],
3756 wcode[1], wcode[2], wcode[3]);
3757 goto fail_fw_integrity;
3758 }
3759
3760 seg = blob->segs;
3761 while (*seg && rval == QLA_SUCCESS) {
3762 risc_addr = *seg;
3763 *srisc_addr = *srisc_addr == 0 ? *seg : *srisc_addr;
3764 risc_size = be16_to_cpu(fwcode[3]);
3765
3766 /* Validate firmware image size. */
3767 fwclen += risc_size * sizeof(uint16_t);
3768 if (blob->fw->size < fwclen) {
3769 qla_printk(KERN_WARNING, ha,
3770 "Unable to verify integrity of firmware image "
3771 "(%Zd)!\n", blob->fw->size);
3772 goto fail_fw_integrity;
3773 }
3774
3775 fragment = 0;
3776 while (risc_size > 0 && rval == QLA_SUCCESS) {
3777 wlen = (uint16_t)(ha->fw_transfer_size >> 1);
3778 if (wlen > risc_size)
3779 wlen = risc_size;
3780
3781 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3782 "addr %x, number of words 0x%x.\n", ha->host_no,
3783 risc_addr, wlen));
3784
3785 for (i = 0; i < wlen; i++)
3786 wcode[i] = swab16(fwcode[i]);
3787
3788 rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3789 wlen);
3790 if (rval) {
3791 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3792 "segment %d of firmware\n", ha->host_no,
3793 fragment));
3794 qla_printk(KERN_WARNING, ha,
3795 "[ERROR] Failed to load segment %d of "
3796 "firmware\n", fragment);
3797 break;
3798 }
3799
3800 fwcode += wlen;
3801 risc_addr += wlen;
3802 risc_size -= wlen;
3803 fragment++;
3804 }
3805
3806 /* Next segment. */
3807 seg++;
3808 }
3809 return rval;
3810
3811fail_fw_integrity:
3812 return QLA_FUNCTION_FAILED;
3813}
3814
3815int
3816qla24xx_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003817{
3818 int rval;
3819 int segments, fragment;
3820 uint32_t *dcode, dlen;
3821 uint32_t risc_addr;
3822 uint32_t risc_size;
3823 uint32_t i;
Andrew Vasquez54333832005-11-09 15:49:04 -08003824 struct fw_blob *blob;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003825 uint32_t *fwcode, fwclen;
3826
Andrew Vasquez54333832005-11-09 15:49:04 -08003827 /* Load firmware blob. */
3828 blob = qla2x00_request_firmware(ha);
3829 if (!blob) {
3830 qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
Andrew Vasquezd1c61902006-05-17 15:09:00 -07003831 qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
3832 "from: " QLA_FW_URL ".\n");
3833
3834 /* Try to load RISC code from flash. */
3835 qla_printk(KERN_ERR, ha, "Attempting to load (potentially "
3836 "outdated) firmware from flash.\n");
3837 return qla24xx_load_risc_flash(ha, srisc_addr);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003838 }
3839
3840 rval = QLA_SUCCESS;
3841
3842 segments = FA_RISC_CODE_SEGMENTS;
3843 dcode = (uint32_t *)ha->request_ring;
3844 *srisc_addr = 0;
Andrew Vasquez54333832005-11-09 15:49:04 -08003845 fwcode = (uint32_t *)blob->fw->data;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003846 fwclen = 0;
3847
3848 /* Validate firmware image by checking version. */
Andrew Vasquez54333832005-11-09 15:49:04 -08003849 if (blob->fw->size < 8 * sizeof(uint32_t)) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003850 qla_printk(KERN_WARNING, ha,
Andrew Vasquez54333832005-11-09 15:49:04 -08003851 "Unable to verify integrity of firmware image (%Zd)!\n",
3852 blob->fw->size);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003853 goto fail_fw_integrity;
3854 }
3855 for (i = 0; i < 4; i++)
3856 dcode[i] = be32_to_cpu(fwcode[i + 4]);
3857 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
3858 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
3859 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
3860 dcode[3] == 0)) {
3861 qla_printk(KERN_WARNING, ha,
Andrew Vasquez54333832005-11-09 15:49:04 -08003862 "Unable to verify integrity of firmware image!\n");
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003863 qla_printk(KERN_WARNING, ha,
3864 "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
3865 dcode[1], dcode[2], dcode[3]);
3866 goto fail_fw_integrity;
3867 }
3868
3869 while (segments && rval == QLA_SUCCESS) {
3870 risc_addr = be32_to_cpu(fwcode[2]);
3871 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
3872 risc_size = be32_to_cpu(fwcode[3]);
3873
3874 /* Validate firmware image size. */
3875 fwclen += risc_size * sizeof(uint32_t);
Andrew Vasquez54333832005-11-09 15:49:04 -08003876 if (blob->fw->size < fwclen) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003877 qla_printk(KERN_WARNING, ha,
Andrew Vasquez54333832005-11-09 15:49:04 -08003878 "Unable to verify integrity of firmware image "
3879 "(%Zd)!\n", blob->fw->size);
3880
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003881 goto fail_fw_integrity;
3882 }
3883
3884 fragment = 0;
3885 while (risc_size > 0 && rval == QLA_SUCCESS) {
3886 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
3887 if (dlen > risc_size)
3888 dlen = risc_size;
3889
3890 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3891 "addr %x, number of dwords 0x%x.\n", ha->host_no,
3892 risc_addr, dlen));
3893
3894 for (i = 0; i < dlen; i++)
3895 dcode[i] = swab32(fwcode[i]);
3896
andrew.vasquez@qlogic.com590f98e2006-01-13 17:05:37 -08003897 rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3898 dlen);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003899 if (rval) {
3900 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3901 "segment %d of firmware\n", ha->host_no,
3902 fragment));
3903 qla_printk(KERN_WARNING, ha,
3904 "[ERROR] Failed to load segment %d of "
3905 "firmware\n", fragment);
3906 break;
3907 }
3908
3909 fwcode += dlen;
3910 risc_addr += dlen;
3911 risc_size -= dlen;
3912 fragment++;
3913 }
3914
3915 /* Next segment. */
3916 segments--;
3917 }
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003918 return rval;
3919
3920fail_fw_integrity:
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003921 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003922}
Andrew Vasquez18c6c122006-10-13 09:33:38 -07003923
3924void
3925qla2x00_try_to_stop_firmware(scsi_qla_host_t *ha)
3926{
3927 int ret, retries;
3928
3929 if (!IS_QLA24XX(ha) && !IS_QLA54XX(ha))
3930 return;
3931
3932 ret = qla2x00_stop_firmware(ha);
3933 for (retries = 5; ret != QLA_SUCCESS && retries ; retries--) {
3934 qla2x00_reset_chip(ha);
3935 if (qla2x00_chip_diag(ha) != QLA_SUCCESS)
3936 continue;
3937 if (qla2x00_setup_chip(ha) != QLA_SUCCESS)
3938 continue;
3939 qla_printk(KERN_INFO, ha,
3940 "Attempting retry of stop-firmware command...\n");
3941 ret = qla2x00_stop_firmware(ha);
3942 }
3943}