blob: cf94f8636ba5af6b5504d5acb6995f0e9a7aaeae [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Andrew Vasquezfa90c542005-10-27 11:10:08 -07002 * QLogic Fibre Channel HBA Driver
3 * Copyright (c) 2003-2005 QLogic Corporation
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Andrew Vasquezfa90c542005-10-27 11:10:08 -07005 * See LICENSE.qla2xxx for copyright and licensing details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7#include "qla_def.h"
8
9#include <linux/delay.h>
Andrew Vasquez01071092005-07-06 10:31:37 -070010#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
12#include "qla_devtbl.h"
13
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 Vasquez01071092005-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 Vasquez01071092005-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 Vasqueza157b102007-05-07 07:43:01 -0700132 int ret;
133 uint16_t w;
Adam Kropelin27b2f672005-09-16 19:28:20 -0700134 uint32_t d;
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700135 unsigned long flags;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700136 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 pci_set_master(ha->pdev);
Andrew Vasqueza157b102007-05-07 07:43:01 -0700139 ret = pci_set_mwi(ha->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
Andrew Vasqueza157b102007-05-07 07:43:01 -0700142 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
144
145 /* Reset expansion ROM address decode enable */
Adam Kropelin27b2f672005-09-16 19:28:20 -0700146 pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
147 d &= ~PCI_ROM_ADDRESS_ENABLE;
148 pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700150 /* Get PCI bus information. */
151 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez3d716442005-07-06 10:30:26 -0700152 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700153 spin_unlock_irqrestore(&ha->hardware_lock, flags);
154
155 return QLA_SUCCESS;
156}
157
158/**
159 * qla2300_pci_config() - Setup ISP23xx PCI configuration registers.
160 * @ha: HA context
161 *
162 * Returns 0 on success.
163 */
164int
165qla2300_pci_config(scsi_qla_host_t *ha)
166{
Andrew Vasqueza157b102007-05-07 07:43:01 -0700167 int ret;
168 uint16_t w;
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);
Andrew Vasqueza157b102007-05-07 07:43:01 -0700175 ret = pci_set_mwi(ha->pdev);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700176
177 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
Andrew Vasqueza157b102007-05-07 07:43:01 -0700178 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700179
180 if (IS_QLA2322(ha) || IS_QLA6322(ha))
181 w &= ~PCI_COMMAND_INTX_DISABLE;
Andrew Vasqueza157b102007-05-07 07:43:01 -0700182 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700183
184 /*
185 * If this is a 2300 card and not 2312, reset the
186 * COMMAND_INVALIDATE due to a bug in the 2300. Unfortunately,
187 * the 2310 also reports itself as a 2300 so we need to get the
188 * fb revision level -- a 6 indicates it really is a 2300 and
189 * not a 2310.
190 */
191 if (IS_QLA2300(ha)) {
192 spin_lock_irqsave(&ha->hardware_lock, flags);
193
194 /* Pause RISC. */
Andrew Vasquez3d716442005-07-06 10:30:26 -0700195 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700196 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquez3d716442005-07-06 10:30:26 -0700197 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700198 break;
199
200 udelay(10);
201 }
202
203 /* Select FPM registers. */
Andrew Vasquez3d716442005-07-06 10:30:26 -0700204 WRT_REG_WORD(&reg->ctrl_status, 0x20);
205 RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700206
207 /* Get the fb rev level */
Andrew Vasquez3d716442005-07-06 10:30:26 -0700208 ha->fb_rev = RD_FB_CMD_REG(ha, reg);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700209
210 if (ha->fb_rev == FPM_2300)
Andrew Vasqueza157b102007-05-07 07:43:01 -0700211 pci_clear_mwi(ha->pdev);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700212
213 /* Deselect FPM registers. */
Andrew Vasquez3d716442005-07-06 10:30:26 -0700214 WRT_REG_WORD(&reg->ctrl_status, 0x0);
215 RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700216
217 /* Release RISC module. */
Andrew Vasquez3d716442005-07-06 10:30:26 -0700218 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700219 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquez3d716442005-07-06 10:30:26 -0700220 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) == 0)
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700221 break;
222
223 udelay(10);
224 }
225
226 spin_unlock_irqrestore(&ha->hardware_lock, flags);
227 }
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700228
229 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
230
231 /* Reset expansion ROM address decode enable */
Adam Kropelin27b2f672005-09-16 19:28:20 -0700232 pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
233 d &= ~PCI_ROM_ADDRESS_ENABLE;
234 pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700235
236 /* Get PCI bus information. */
237 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez3d716442005-07-06 10:30:26 -0700238 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700239 spin_unlock_irqrestore(&ha->hardware_lock, flags);
240
241 return QLA_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
244/**
Andrew Vasquez01071092005-07-06 10:31:37 -0700245 * qla24xx_pci_config() - Setup ISP24xx PCI configuration registers.
246 * @ha: HA context
247 *
248 * Returns 0 on success.
249 */
250int
251qla24xx_pci_config(scsi_qla_host_t *ha)
252{
Andrew Vasqueza157b102007-05-07 07:43:01 -0700253 int ret;
254 uint16_t w;
Adam Kropelin27b2f672005-09-16 19:28:20 -0700255 uint32_t d;
Andrew Vasquez01071092005-07-06 10:31:37 -0700256 unsigned long flags = 0;
257 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
258 int pcix_cmd_reg, pcie_dctl_reg;
259
260 pci_set_master(ha->pdev);
Andrew Vasqueza157b102007-05-07 07:43:01 -0700261 ret = pci_set_mwi(ha->pdev);
Andrew Vasquez01071092005-07-06 10:31:37 -0700262
263 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
Andrew Vasqueza157b102007-05-07 07:43:01 -0700264 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
Andrew Vasquez01071092005-07-06 10:31:37 -0700265 w &= ~PCI_COMMAND_INTX_DISABLE;
266 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
267
268 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
269
270 /* PCI-X -- adjust Maximum Memory Read Byte Count (2048). */
271 pcix_cmd_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_PCIX);
272 if (pcix_cmd_reg) {
273 uint16_t pcix_cmd;
274
275 pcix_cmd_reg += PCI_X_CMD;
276 pci_read_config_word(ha->pdev, pcix_cmd_reg, &pcix_cmd);
277 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
278 pcix_cmd |= 0x0008;
279 pci_write_config_word(ha->pdev, pcix_cmd_reg, pcix_cmd);
280 }
281
282 /* PCIe -- adjust Maximum Read Request Size (2048). */
283 pcie_dctl_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_EXP);
284 if (pcie_dctl_reg) {
285 uint16_t pcie_dctl;
286
287 pcie_dctl_reg += PCI_EXP_DEVCTL;
288 pci_read_config_word(ha->pdev, pcie_dctl_reg, &pcie_dctl);
289 pcie_dctl &= ~PCI_EXP_DEVCTL_READRQ;
290 pcie_dctl |= 0x4000;
291 pci_write_config_word(ha->pdev, pcie_dctl_reg, pcie_dctl);
292 }
293
294 /* Reset expansion ROM address decode enable */
Adam Kropelin27b2f672005-09-16 19:28:20 -0700295 pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
296 d &= ~PCI_ROM_ADDRESS_ENABLE;
297 pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
Andrew Vasquez01071092005-07-06 10:31:37 -0700298
Auke Kok44c10132007-06-08 15:46:36 -0700299 ha->chip_revision = ha->pdev->revision;
Andrew Vasqueza8488ab2007-01-29 10:22:19 -0800300
Andrew Vasquez01071092005-07-06 10:31:37 -0700301 /* Get PCI bus information. */
302 spin_lock_irqsave(&ha->hardware_lock, flags);
303 ha->pci_attr = RD_REG_DWORD(&reg->ctrl_status);
304 spin_unlock_irqrestore(&ha->hardware_lock, flags);
305
306 return QLA_SUCCESS;
307}
308
309/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 * qla2x00_isp_firmware() - Choose firmware image.
311 * @ha: HA context
312 *
313 * Returns 0 on success.
314 */
315static int
316qla2x00_isp_firmware(scsi_qla_host_t *ha)
317{
318 int rval;
319
320 /* Assume loading risc code */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700321 rval = QLA_FUNCTION_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 if (ha->flags.disable_risc_code_load) {
324 DEBUG2(printk("scsi(%ld): RISC CODE NOT loaded\n",
325 ha->host_no));
326 qla_printk(KERN_INFO, ha, "RISC CODE NOT loaded\n");
327
328 /* Verify checksum of loaded RISC code. */
Andrew Vasquez441d1072006-05-17 15:09:34 -0700329 rval = qla2x00_verify_checksum(ha, ha->fw_srisc_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
331
332 if (rval) {
333 DEBUG2_3(printk("scsi(%ld): **** Load RISC code ****\n",
334 ha->host_no));
335 }
336
337 return (rval);
338}
339
340/**
341 * qla2x00_reset_chip() - Reset ISP chip.
342 * @ha: HA context
343 *
344 * Returns 0 on success.
345 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700346void
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700347qla2x00_reset_chip(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
349 unsigned long flags = 0;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700350 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 uint32_t cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 uint16_t cmd;
353
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700354 ha->isp_ops.disable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 spin_lock_irqsave(&ha->hardware_lock, flags);
357
358 /* Turn off master enable */
359 cmd = 0;
360 pci_read_config_word(ha->pdev, PCI_COMMAND, &cmd);
361 cmd &= ~PCI_COMMAND_MASTER;
362 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
363
364 if (!IS_QLA2100(ha)) {
365 /* Pause RISC. */
366 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
367 if (IS_QLA2200(ha) || IS_QLA2300(ha)) {
368 for (cnt = 0; cnt < 30000; cnt++) {
369 if ((RD_REG_WORD(&reg->hccr) &
370 HCCR_RISC_PAUSE) != 0)
371 break;
372 udelay(100);
373 }
374 } else {
375 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
376 udelay(10);
377 }
378
379 /* Select FPM registers. */
380 WRT_REG_WORD(&reg->ctrl_status, 0x20);
381 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
382
383 /* FPM Soft Reset. */
384 WRT_REG_WORD(&reg->fpm_diag_config, 0x100);
385 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
386
387 /* Toggle Fpm Reset. */
388 if (!IS_QLA2200(ha)) {
389 WRT_REG_WORD(&reg->fpm_diag_config, 0x0);
390 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
391 }
392
393 /* Select frame buffer registers. */
394 WRT_REG_WORD(&reg->ctrl_status, 0x10);
395 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
396
397 /* Reset frame buffer FIFOs. */
398 if (IS_QLA2200(ha)) {
399 WRT_FB_CMD_REG(ha, reg, 0xa000);
400 RD_FB_CMD_REG(ha, reg); /* PCI Posting. */
401 } else {
402 WRT_FB_CMD_REG(ha, reg, 0x00fc);
403
404 /* Read back fb_cmd until zero or 3 seconds max */
405 for (cnt = 0; cnt < 3000; cnt++) {
406 if ((RD_FB_CMD_REG(ha, reg) & 0xff) == 0)
407 break;
408 udelay(100);
409 }
410 }
411
412 /* Select RISC module registers. */
413 WRT_REG_WORD(&reg->ctrl_status, 0);
414 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
415
416 /* Reset RISC processor. */
417 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
418 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
419
420 /* Release RISC processor. */
421 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
422 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
423 }
424
425 WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
426 WRT_REG_WORD(&reg->hccr, HCCR_CLR_HOST_INT);
427
428 /* Reset ISP chip. */
429 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
430
431 /* Wait for RISC to recover from reset. */
432 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
433 /*
434 * It is necessary to for a delay here since the card doesn't
435 * respond to PCI reads during a reset. On some architectures
436 * this will result in an MCA.
437 */
438 udelay(20);
439 for (cnt = 30000; cnt; cnt--) {
440 if ((RD_REG_WORD(&reg->ctrl_status) &
441 CSR_ISP_SOFT_RESET) == 0)
442 break;
443 udelay(100);
444 }
445 } else
446 udelay(10);
447
448 /* Reset RISC processor. */
449 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
450
451 WRT_REG_WORD(&reg->semaphore, 0);
452
453 /* Release RISC processor. */
454 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
455 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
456
457 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
458 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquezffb39f02006-05-17 15:09:06 -0700459 if (RD_MAILBOX_REG(ha, reg, 0) != MBS_BUSY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 udelay(100);
463 }
464 } else
465 udelay(100);
466
467 /* Turn on master enable */
468 cmd |= PCI_COMMAND_MASTER;
469 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
470
471 /* Disable RISC pause on FPM parity error. */
472 if (!IS_QLA2100(ha)) {
473 WRT_REG_WORD(&reg->hccr, HCCR_DISABLE_PARITY_PAUSE);
474 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
475 }
476
477 spin_unlock_irqrestore(&ha->hardware_lock, flags);
478}
479
480/**
Andrew Vasquez88c26662005-07-08 17:59:26 -0700481 * qla24xx_reset_risc() - Perform full reset of ISP24xx RISC.
Andrew Vasquez01071092005-07-06 10:31:37 -0700482 * @ha: HA context
483 *
484 * Returns 0 on success.
485 */
Andrew Vasquez88c26662005-07-08 17:59:26 -0700486static inline void
487qla24xx_reset_risc(scsi_qla_host_t *ha)
Andrew Vasquez01071092005-07-06 10:31:37 -0700488{
489 unsigned long flags = 0;
490 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
491 uint32_t cnt, d2;
Andrew Vasquez335a1cc2005-11-08 14:37:48 -0800492 uint16_t wd;
Andrew Vasquez01071092005-07-06 10:31:37 -0700493
Andrew Vasquez01071092005-07-06 10:31:37 -0700494 spin_lock_irqsave(&ha->hardware_lock, flags);
495
496 /* Reset RISC. */
497 WRT_REG_DWORD(&reg->ctrl_status, CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
498 for (cnt = 0; cnt < 30000; cnt++) {
499 if ((RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE) == 0)
500 break;
501
502 udelay(10);
503 }
504
505 WRT_REG_DWORD(&reg->ctrl_status,
506 CSRX_ISP_SOFT_RESET|CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
Andrew Vasquez335a1cc2005-11-08 14:37:48 -0800507 pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
Andrew Vasquez88c26662005-07-08 17:59:26 -0700508
Andrew Vasquez335a1cc2005-11-08 14:37:48 -0800509 udelay(100);
Andrew Vasquez88c26662005-07-08 17:59:26 -0700510 /* Wait for firmware to complete NVRAM accesses. */
Andrew Vasquez88c26662005-07-08 17:59:26 -0700511 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
512 for (cnt = 10000 ; cnt && d2; cnt--) {
513 udelay(5);
514 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
515 barrier();
516 }
517
Andrew Vasquez335a1cc2005-11-08 14:37:48 -0800518 /* Wait for soft-reset to complete. */
Andrew Vasquez01071092005-07-06 10:31:37 -0700519 d2 = RD_REG_DWORD(&reg->ctrl_status);
520 for (cnt = 6000000 ; cnt && (d2 & CSRX_ISP_SOFT_RESET); cnt--) {
521 udelay(5);
522 d2 = RD_REG_DWORD(&reg->ctrl_status);
523 barrier();
524 }
525
526 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
527 RD_REG_DWORD(&reg->hccr);
528
529 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
530 RD_REG_DWORD(&reg->hccr);
531
532 WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_RESET);
533 RD_REG_DWORD(&reg->hccr);
534
535 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
536 for (cnt = 6000000 ; cnt && d2; cnt--) {
537 udelay(5);
538 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
539 barrier();
540 }
541
542 spin_unlock_irqrestore(&ha->hardware_lock, flags);
543}
544
545/**
Andrew Vasquez88c26662005-07-08 17:59:26 -0700546 * qla24xx_reset_chip() - Reset ISP24xx chip.
547 * @ha: HA context
548 *
549 * Returns 0 on success.
550 */
551void
552qla24xx_reset_chip(scsi_qla_host_t *ha)
553{
554 ha->isp_ops.disable_intrs(ha);
555
556 /* Perform RISC reset. */
557 qla24xx_reset_risc(ha);
558}
559
560/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 * qla2x00_chip_diag() - Test chip for proper operation.
562 * @ha: HA context
563 *
564 * Returns 0 on success.
565 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700566int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567qla2x00_chip_diag(scsi_qla_host_t *ha)
568{
569 int rval;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700570 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 unsigned long flags = 0;
572 uint16_t data;
573 uint32_t cnt;
574 uint16_t mb[5];
575
576 /* Assume a failed state */
577 rval = QLA_FUNCTION_FAILED;
578
579 DEBUG3(printk("scsi(%ld): Testing device at %lx.\n",
580 ha->host_no, (u_long)&reg->flash_address));
581
582 spin_lock_irqsave(&ha->hardware_lock, flags);
583
584 /* Reset ISP chip. */
585 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
586
587 /*
588 * We need to have a delay here since the card will not respond while
589 * in reset causing an MCA on some architectures.
590 */
591 udelay(20);
592 data = qla2x00_debounce_register(&reg->ctrl_status);
593 for (cnt = 6000000 ; cnt && (data & CSR_ISP_SOFT_RESET); cnt--) {
594 udelay(5);
595 data = RD_REG_WORD(&reg->ctrl_status);
596 barrier();
597 }
598
599 if (!cnt)
600 goto chip_diag_failed;
601
602 DEBUG3(printk("scsi(%ld): Reset register cleared by chip reset\n",
603 ha->host_no));
604
605 /* Reset RISC processor. */
606 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
607 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
608
609 /* Workaround for QLA2312 PCI parity error */
610 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
611 data = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 0));
612 for (cnt = 6000000; cnt && (data == MBS_BUSY); cnt--) {
613 udelay(5);
614 data = RD_MAILBOX_REG(ha, reg, 0);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700615 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
617 } else
618 udelay(10);
619
620 if (!cnt)
621 goto chip_diag_failed;
622
623 /* Check product ID of chip */
624 DEBUG3(printk("scsi(%ld): Checking product ID of chip\n", ha->host_no));
625
626 mb[1] = RD_MAILBOX_REG(ha, reg, 1);
627 mb[2] = RD_MAILBOX_REG(ha, reg, 2);
628 mb[3] = RD_MAILBOX_REG(ha, reg, 3);
629 mb[4] = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 4));
630 if (mb[1] != PROD_ID_1 || (mb[2] != PROD_ID_2 && mb[2] != PROD_ID_2a) ||
631 mb[3] != PROD_ID_3) {
632 qla_printk(KERN_WARNING, ha,
633 "Wrong product ID = 0x%x,0x%x,0x%x\n", mb[1], mb[2], mb[3]);
634
635 goto chip_diag_failed;
636 }
637 ha->product_id[0] = mb[1];
638 ha->product_id[1] = mb[2];
639 ha->product_id[2] = mb[3];
640 ha->product_id[3] = mb[4];
641
642 /* Adjust fw RISC transfer size */
643 if (ha->request_q_length > 1024)
644 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
645 else
646 ha->fw_transfer_size = REQUEST_ENTRY_SIZE *
647 ha->request_q_length;
648
649 if (IS_QLA2200(ha) &&
650 RD_MAILBOX_REG(ha, reg, 7) == QLA2200A_RISC_ROM_VER) {
651 /* Limit firmware transfer size with a 2200A */
652 DEBUG3(printk("scsi(%ld): Found QLA2200A chip.\n",
653 ha->host_no));
654
andrew.vasquez@qlogic.comea5b6382006-03-09 14:27:08 -0800655 ha->device_type |= DT_ISP2200A;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 ha->fw_transfer_size = 128;
657 }
658
659 /* Wrap Incoming Mailboxes Test. */
660 spin_unlock_irqrestore(&ha->hardware_lock, flags);
661
662 DEBUG3(printk("scsi(%ld): Checking mailboxes.\n", ha->host_no));
663 rval = qla2x00_mbx_reg_test(ha);
664 if (rval) {
665 DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
666 ha->host_no));
667 qla_printk(KERN_WARNING, ha,
668 "Failed mailbox send register test\n");
669 }
670 else {
671 /* Flag a successful rval */
672 rval = QLA_SUCCESS;
673 }
674 spin_lock_irqsave(&ha->hardware_lock, flags);
675
676chip_diag_failed:
677 if (rval)
678 DEBUG2_3(printk("scsi(%ld): Chip diagnostics **** FAILED "
679 "****\n", ha->host_no));
680
681 spin_unlock_irqrestore(&ha->hardware_lock, flags);
682
683 return (rval);
684}
685
686/**
Andrew Vasquez01071092005-07-06 10:31:37 -0700687 * qla24xx_chip_diag() - Test ISP24xx for proper operation.
688 * @ha: HA context
689 *
690 * Returns 0 on success.
691 */
692int
693qla24xx_chip_diag(scsi_qla_host_t *ha)
694{
695 int rval;
Andrew Vasquez01071092005-07-06 10:31:37 -0700696
Andrew Vasquez88c26662005-07-08 17:59:26 -0700697 /* Perform RISC reset. */
698 qla24xx_reset_risc(ha);
Andrew Vasquez01071092005-07-06 10:31:37 -0700699
700 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
701
702 rval = qla2x00_mbx_reg_test(ha);
703 if (rval) {
704 DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
705 ha->host_no));
706 qla_printk(KERN_WARNING, ha,
707 "Failed mailbox send register test\n");
708 } else {
709 /* Flag a successful rval */
710 rval = QLA_SUCCESS;
711 }
712
713 return rval;
714}
715
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700716void
Andrew Vasquez01071092005-07-06 10:31:37 -0700717qla2x00_alloc_fw_dump(scsi_qla_host_t *ha)
718{
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700719 int rval;
720 uint32_t dump_size, fixed_size, mem_size, req_q_size, rsp_q_size,
721 eft_size;
722 dma_addr_t eft_dma;
723 void *eft;
Andrew Vasquezd4e3e042006-05-17 15:09:50 -0700724
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700725 if (ha->fw_dump) {
726 qla_printk(KERN_WARNING, ha,
727 "Firmware dump previously allocated.\n");
728 return;
Andrew Vasquezd4e3e042006-05-17 15:09:50 -0700729 }
730
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700731 ha->fw_dumped = 0;
732 fixed_size = mem_size = eft_size = 0;
733 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
734 fixed_size = sizeof(struct qla2100_fw_dump);
735 } else if (IS_QLA23XX(ha)) {
736 fixed_size = offsetof(struct qla2300_fw_dump, data_ram);
737 mem_size = (ha->fw_memory_size - 0x11000 + 1) *
738 sizeof(uint16_t);
739 } else if (IS_QLA24XX(ha) || IS_QLA54XX(ha)) {
740 fixed_size = offsetof(struct qla24xx_fw_dump, ext_mem);
741 mem_size = (ha->fw_memory_size - 0x100000 + 1) *
742 sizeof(uint32_t);
743
744 /* Allocate memory for Extended Trace Buffer. */
745 eft = dma_alloc_coherent(&ha->pdev->dev, EFT_SIZE, &eft_dma,
746 GFP_KERNEL);
747 if (!eft) {
748 qla_printk(KERN_WARNING, ha, "Unable to allocate "
749 "(%d KB) for EFT.\n", EFT_SIZE / 1024);
750 goto cont_alloc;
751 }
752
753 rval = qla2x00_trace_control(ha, TC_ENABLE, eft_dma,
754 EFT_NUM_BUFFERS);
755 if (rval) {
756 qla_printk(KERN_WARNING, ha, "Unable to initialize "
757 "EFT (%d).\n", rval);
758 dma_free_coherent(&ha->pdev->dev, EFT_SIZE, eft,
759 eft_dma);
760 goto cont_alloc;
761 }
762
763 qla_printk(KERN_INFO, ha, "Allocated (%d KB) for EFT...\n",
764 EFT_SIZE / 1024);
765
766 eft_size = EFT_SIZE;
767 memset(eft, 0, eft_size);
768 ha->eft_dma = eft_dma;
769 ha->eft = eft;
770 }
771cont_alloc:
772 req_q_size = ha->request_q_length * sizeof(request_t);
773 rsp_q_size = ha->response_q_length * sizeof(response_t);
774
775 dump_size = offsetof(struct qla2xxx_fw_dump, isp);
776 dump_size += fixed_size + mem_size + req_q_size + rsp_q_size +
777 eft_size;
778
Andrew Vasquezd4e3e042006-05-17 15:09:50 -0700779 ha->fw_dump = vmalloc(dump_size);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700780 if (!ha->fw_dump) {
Andrew Vasquez01071092005-07-06 10:31:37 -0700781 qla_printk(KERN_WARNING, ha, "Unable to allocate (%d KB) for "
Andrew Vasquezd4e3e042006-05-17 15:09:50 -0700782 "firmware dump!!!\n", dump_size / 1024);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700783
784 if (ha->eft) {
785 dma_free_coherent(&ha->pdev->dev, eft_size, ha->eft,
786 ha->eft_dma);
787 ha->eft = NULL;
788 ha->eft_dma = 0;
789 }
790 return;
791 }
792
793 qla_printk(KERN_INFO, ha, "Allocated (%d KB) for firmware dump...\n",
794 dump_size / 1024);
795
796 ha->fw_dump_len = dump_size;
797 ha->fw_dump->signature[0] = 'Q';
798 ha->fw_dump->signature[1] = 'L';
799 ha->fw_dump->signature[2] = 'G';
800 ha->fw_dump->signature[3] = 'C';
801 ha->fw_dump->version = __constant_htonl(1);
802
803 ha->fw_dump->fixed_size = htonl(fixed_size);
804 ha->fw_dump->mem_size = htonl(mem_size);
805 ha->fw_dump->req_q_size = htonl(req_q_size);
806 ha->fw_dump->rsp_q_size = htonl(rsp_q_size);
807
808 ha->fw_dump->eft_size = htonl(eft_size);
809 ha->fw_dump->eft_addr_l = htonl(LSD(ha->eft_dma));
810 ha->fw_dump->eft_addr_h = htonl(MSD(ha->eft_dma));
811
812 ha->fw_dump->header_size =
813 htonl(offsetof(struct qla2xxx_fw_dump, isp));
Andrew Vasquez01071092005-07-06 10:31:37 -0700814}
815
816/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 * qla2x00_resize_request_q() - Resize request queue given available ISP memory.
818 * @ha: HA context
819 *
820 * Returns 0 on success.
821 */
822static void
823qla2x00_resize_request_q(scsi_qla_host_t *ha)
824{
825 int rval;
826 uint16_t fw_iocb_cnt = 0;
827 uint16_t request_q_length = REQUEST_ENTRY_CNT_2XXX_EXT_MEM;
828 dma_addr_t request_dma;
829 request_t *request_ring;
830
831 /* Valid only on recent ISPs. */
832 if (IS_QLA2100(ha) || IS_QLA2200(ha))
833 return;
834
835 /* Retrieve IOCB counts available to the firmware. */
836 rval = qla2x00_get_resource_cnts(ha, NULL, NULL, NULL, &fw_iocb_cnt);
837 if (rval)
838 return;
839 /* No point in continuing if current settings are sufficient. */
840 if (fw_iocb_cnt < 1024)
841 return;
842 if (ha->request_q_length >= request_q_length)
843 return;
844
845 /* Attempt to claim larger area for request queue. */
846 request_ring = dma_alloc_coherent(&ha->pdev->dev,
847 (request_q_length + 1) * sizeof(request_t), &request_dma,
848 GFP_KERNEL);
849 if (request_ring == NULL)
850 return;
851
852 /* Resize successful, report extensions. */
853 qla_printk(KERN_INFO, ha, "Extended memory detected (%d KB)...\n",
854 (ha->fw_memory_size + 1) / 1024);
855 qla_printk(KERN_INFO, ha, "Resizing request queue depth "
856 "(%d -> %d)...\n", ha->request_q_length, request_q_length);
857
858 /* Clear old allocations. */
859 dma_free_coherent(&ha->pdev->dev,
860 (ha->request_q_length + 1) * sizeof(request_t), ha->request_ring,
861 ha->request_dma);
862
863 /* Begin using larger queue. */
864 ha->request_q_length = request_q_length;
865 ha->request_ring = request_ring;
866 ha->request_dma = request_dma;
867}
868
869/**
870 * qla2x00_setup_chip() - Load and start RISC firmware.
871 * @ha: HA context
872 *
873 * Returns 0 on success.
874 */
875static int
876qla2x00_setup_chip(scsi_qla_host_t *ha)
877{
Andrew Vasquez01071092005-07-06 10:31:37 -0700878 int rval;
879 uint32_t srisc_address = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 /* Load firmware sequences */
Andrew Vasquez01071092005-07-06 10:31:37 -0700882 rval = ha->isp_ops.load_risc(ha, &srisc_address);
883 if (rval == QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 DEBUG(printk("scsi(%ld): Verifying Checksum of loaded RISC "
885 "code.\n", ha->host_no));
886
Andrew Vasquez01071092005-07-06 10:31:37 -0700887 rval = qla2x00_verify_checksum(ha, srisc_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 if (rval == QLA_SUCCESS) {
889 /* Start firmware execution. */
890 DEBUG(printk("scsi(%ld): Checksum OK, start "
891 "firmware.\n", ha->host_no));
892
Andrew Vasquez01071092005-07-06 10:31:37 -0700893 rval = qla2x00_execute_fw(ha, srisc_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 /* Retrieve firmware information. */
895 if (rval == QLA_SUCCESS && ha->fw_major_version == 0) {
896 qla2x00_get_fw_version(ha,
897 &ha->fw_major_version,
898 &ha->fw_minor_version,
899 &ha->fw_subminor_version,
900 &ha->fw_attributes, &ha->fw_memory_size);
901 qla2x00_resize_request_q(ha);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700902
903 if (ql2xallocfwdump)
904 qla2x00_alloc_fw_dump(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 }
906 } else {
907 DEBUG2(printk(KERN_INFO
908 "scsi(%ld): ISP Firmware failed checksum.\n",
909 ha->host_no));
910 }
911 }
912
913 if (rval) {
914 DEBUG2_3(printk("scsi(%ld): Setup chip **** FAILED ****.\n",
915 ha->host_no));
916 }
917
918 return (rval);
919}
920
921/**
922 * qla2x00_init_response_q_entries() - Initializes response queue entries.
923 * @ha: HA context
924 *
925 * Beginning of request ring has initialization control block already built
926 * by nvram config routine.
927 *
928 * Returns 0 on success.
929 */
930static void
931qla2x00_init_response_q_entries(scsi_qla_host_t *ha)
932{
933 uint16_t cnt;
934 response_t *pkt;
935
936 pkt = ha->response_ring_ptr;
937 for (cnt = 0; cnt < ha->response_q_length; cnt++) {
938 pkt->signature = RESPONSE_PROCESSED;
939 pkt++;
940 }
941
942}
943
944/**
945 * qla2x00_update_fw_options() - Read and process firmware options.
946 * @ha: HA context
947 *
948 * Returns 0 on success.
949 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700950void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951qla2x00_update_fw_options(scsi_qla_host_t *ha)
952{
953 uint16_t swing, emphasis, tx_sens, rx_sens;
954
955 memset(ha->fw_options, 0, sizeof(ha->fw_options));
956 qla2x00_get_fw_options(ha, ha->fw_options);
957
958 if (IS_QLA2100(ha) || IS_QLA2200(ha))
959 return;
960
961 /* Serial Link options. */
962 DEBUG3(printk("scsi(%ld): Serial link options:\n",
963 ha->host_no));
964 DEBUG3(qla2x00_dump_buffer((uint8_t *)&ha->fw_seriallink_options,
965 sizeof(ha->fw_seriallink_options)));
966
967 ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
968 if (ha->fw_seriallink_options[3] & BIT_2) {
969 ha->fw_options[1] |= FO1_SET_EMPHASIS_SWING;
970
971 /* 1G settings */
972 swing = ha->fw_seriallink_options[2] & (BIT_2 | BIT_1 | BIT_0);
973 emphasis = (ha->fw_seriallink_options[2] &
974 (BIT_4 | BIT_3)) >> 3;
975 tx_sens = ha->fw_seriallink_options[0] &
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700976 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 rx_sens = (ha->fw_seriallink_options[0] &
978 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
979 ha->fw_options[10] = (emphasis << 14) | (swing << 8);
980 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
981 if (rx_sens == 0x0)
982 rx_sens = 0x3;
983 ha->fw_options[10] |= (tx_sens << 4) | rx_sens;
984 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
985 ha->fw_options[10] |= BIT_5 |
986 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
987 (tx_sens & (BIT_1 | BIT_0));
988
989 /* 2G settings */
990 swing = (ha->fw_seriallink_options[2] &
991 (BIT_7 | BIT_6 | BIT_5)) >> 5;
992 emphasis = ha->fw_seriallink_options[3] & (BIT_1 | BIT_0);
993 tx_sens = ha->fw_seriallink_options[1] &
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700994 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 rx_sens = (ha->fw_seriallink_options[1] &
996 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
997 ha->fw_options[11] = (emphasis << 14) | (swing << 8);
998 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
999 if (rx_sens == 0x0)
1000 rx_sens = 0x3;
1001 ha->fw_options[11] |= (tx_sens << 4) | rx_sens;
1002 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
1003 ha->fw_options[11] |= BIT_5 |
1004 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
1005 (tx_sens & (BIT_1 | BIT_0));
1006 }
1007
1008 /* FCP2 options. */
1009 /* Return command IOCBs without waiting for an ABTS to complete. */
1010 ha->fw_options[3] |= BIT_13;
1011
1012 /* LED scheme. */
1013 if (ha->flags.enable_led_scheme)
1014 ha->fw_options[2] |= BIT_12;
1015
andrew.vasquez@qlogic.com48c02fd2006-03-09 14:27:18 -08001016 /* Detect ISP6312. */
1017 if (IS_QLA6312(ha))
1018 ha->fw_options[2] |= BIT_13;
1019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 /* Update firmware options. */
1021 qla2x00_set_fw_options(ha, ha->fw_options);
1022}
1023
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001024void
Andrew Vasquez01071092005-07-06 10:31:37 -07001025qla24xx_update_fw_options(scsi_qla_host_t *ha)
1026{
1027 int rval;
1028
1029 /* Update Serial Link options. */
andrew.vasquez@qlogic.comf94097e2006-01-13 17:05:32 -08001030 if ((le16_to_cpu(ha->fw_seriallink_options24[0]) & BIT_0) == 0)
Andrew Vasquez01071092005-07-06 10:31:37 -07001031 return;
1032
andrew.vasquez@qlogic.comf94097e2006-01-13 17:05:32 -08001033 rval = qla2x00_set_serdes_params(ha,
1034 le16_to_cpu(ha->fw_seriallink_options24[1]),
1035 le16_to_cpu(ha->fw_seriallink_options24[2]),
1036 le16_to_cpu(ha->fw_seriallink_options24[3]));
Andrew Vasquez01071092005-07-06 10:31:37 -07001037 if (rval != QLA_SUCCESS) {
1038 qla_printk(KERN_WARNING, ha,
1039 "Unable to update Serial Link options (%x).\n", rval);
1040 }
1041}
1042
1043void
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001044qla2x00_config_rings(struct scsi_qla_host *ha)
1045{
Andrew Vasquez3d716442005-07-06 10:30:26 -07001046 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001047
1048 /* Setup ring parameters in initialization control block. */
1049 ha->init_cb->request_q_outpointer = __constant_cpu_to_le16(0);
1050 ha->init_cb->response_q_inpointer = __constant_cpu_to_le16(0);
1051 ha->init_cb->request_q_length = cpu_to_le16(ha->request_q_length);
1052 ha->init_cb->response_q_length = cpu_to_le16(ha->response_q_length);
1053 ha->init_cb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
1054 ha->init_cb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
1055 ha->init_cb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
1056 ha->init_cb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
1057
1058 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), 0);
1059 WRT_REG_WORD(ISP_REQ_Q_OUT(ha, reg), 0);
1060 WRT_REG_WORD(ISP_RSP_Q_IN(ha, reg), 0);
1061 WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), 0);
1062 RD_REG_WORD(ISP_RSP_Q_OUT(ha, reg)); /* PCI Posting. */
1063}
1064
Andrew Vasquez01071092005-07-06 10:31:37 -07001065void
1066qla24xx_config_rings(struct scsi_qla_host *ha)
1067{
1068 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1069 struct init_cb_24xx *icb;
1070
1071 /* Setup ring parameters in initialization control block. */
1072 icb = (struct init_cb_24xx *)ha->init_cb;
1073 icb->request_q_outpointer = __constant_cpu_to_le16(0);
1074 icb->response_q_inpointer = __constant_cpu_to_le16(0);
1075 icb->request_q_length = cpu_to_le16(ha->request_q_length);
1076 icb->response_q_length = cpu_to_le16(ha->response_q_length);
1077 icb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
1078 icb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
1079 icb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
1080 icb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
1081
1082 WRT_REG_DWORD(&reg->req_q_in, 0);
1083 WRT_REG_DWORD(&reg->req_q_out, 0);
1084 WRT_REG_DWORD(&reg->rsp_q_in, 0);
1085 WRT_REG_DWORD(&reg->rsp_q_out, 0);
1086 RD_REG_DWORD(&reg->rsp_q_out);
1087}
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089/**
1090 * qla2x00_init_rings() - Initializes firmware.
1091 * @ha: HA context
1092 *
1093 * Beginning of request ring has initialization control block already built
1094 * by nvram config routine.
1095 *
1096 * Returns 0 on success.
1097 */
1098static int
1099qla2x00_init_rings(scsi_qla_host_t *ha)
1100{
1101 int rval;
1102 unsigned long flags = 0;
1103 int cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
1105 spin_lock_irqsave(&ha->hardware_lock, flags);
1106
1107 /* Clear outstanding commands array. */
1108 for (cnt = 0; cnt < MAX_OUTSTANDING_COMMANDS; cnt++)
1109 ha->outstanding_cmds[cnt] = NULL;
1110
1111 ha->current_outstanding_cmd = 0;
1112
1113 /* Clear RSCN queue. */
1114 ha->rscn_in_ptr = 0;
1115 ha->rscn_out_ptr = 0;
1116
1117 /* Initialize firmware. */
1118 ha->request_ring_ptr = ha->request_ring;
1119 ha->req_ring_index = 0;
1120 ha->req_q_cnt = ha->request_q_length;
1121 ha->response_ring_ptr = ha->response_ring;
1122 ha->rsp_ring_index = 0;
1123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 /* Initialize response queue entries */
1125 qla2x00_init_response_q_entries(ha);
1126
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001127 ha->isp_ops.config_rings(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1130
1131 /* Update any ISP specific firmware options before initialization. */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001132 ha->isp_ops.update_fw_options(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 DEBUG(printk("scsi(%ld): Issue init firmware.\n", ha->host_no));
Andrew Vasquez01071092005-07-06 10:31:37 -07001135 rval = qla2x00_init_firmware(ha, ha->init_cb_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 if (rval) {
1137 DEBUG2_3(printk("scsi(%ld): Init firmware **** FAILED ****.\n",
1138 ha->host_no));
1139 } else {
1140 DEBUG3(printk("scsi(%ld): Init firmware -- success.\n",
1141 ha->host_no));
1142 }
1143
1144 return (rval);
1145}
1146
1147/**
1148 * qla2x00_fw_ready() - Waits for firmware ready.
1149 * @ha: HA context
1150 *
1151 * Returns 0 on success.
1152 */
1153static int
1154qla2x00_fw_ready(scsi_qla_host_t *ha)
1155{
1156 int rval;
1157 unsigned long wtime, mtime;
1158 uint16_t min_wait; /* Minimum wait time if loop is down */
1159 uint16_t wait_time; /* Wait time if loop is coming ready */
1160 uint16_t fw_state;
1161
1162 rval = QLA_SUCCESS;
1163
1164 /* 20 seconds for loop down. */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001165 min_wait = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167 /*
1168 * Firmware should take at most one RATOV to login, plus 5 seconds for
1169 * our own processing.
1170 */
1171 if ((wait_time = (ha->retry_count*ha->login_timeout) + 5) < min_wait) {
1172 wait_time = min_wait;
1173 }
1174
1175 /* Min wait time if loop down */
1176 mtime = jiffies + (min_wait * HZ);
1177
1178 /* wait time before firmware ready */
1179 wtime = jiffies + (wait_time * HZ);
1180
1181 /* Wait for ISP to finish LIP */
1182 if (!ha->flags.init_done)
1183 qla_printk(KERN_INFO, ha, "Waiting for LIP to complete...\n");
1184
1185 DEBUG3(printk("scsi(%ld): Waiting for LIP to complete...\n",
1186 ha->host_no));
1187
1188 do {
1189 rval = qla2x00_get_firmware_state(ha, &fw_state);
1190 if (rval == QLA_SUCCESS) {
1191 if (fw_state < FSTATE_LOSS_OF_SYNC) {
1192 ha->device_flags &= ~DFLG_NO_CABLE;
1193 }
1194 if (fw_state == FSTATE_READY) {
1195 DEBUG(printk("scsi(%ld): F/W Ready - OK \n",
1196 ha->host_no));
1197
1198 qla2x00_get_retry_cnt(ha, &ha->retry_count,
1199 &ha->login_timeout, &ha->r_a_tov);
1200
1201 rval = QLA_SUCCESS;
1202 break;
1203 }
1204
1205 rval = QLA_FUNCTION_FAILED;
1206
1207 if (atomic_read(&ha->loop_down_timer) &&
Andrew Vasquez7d7abc72006-06-23 16:11:17 -07001208 fw_state != FSTATE_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 /* Loop down. Timeout on min_wait for states
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001210 * other than Wait for Login.
1211 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 if (time_after_eq(jiffies, mtime)) {
1213 qla_printk(KERN_INFO, ha,
1214 "Cable is unplugged...\n");
1215
1216 ha->device_flags |= DFLG_NO_CABLE;
1217 break;
1218 }
1219 }
1220 } else {
1221 /* Mailbox cmd failed. Timeout on min_wait. */
1222 if (time_after_eq(jiffies, mtime))
1223 break;
1224 }
1225
1226 if (time_after_eq(jiffies, wtime))
1227 break;
1228
1229 /* Delay for a while */
1230 msleep(500);
1231
1232 DEBUG3(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
1233 ha->host_no, fw_state, jiffies));
1234 } while (1);
1235
1236 DEBUG(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
1237 ha->host_no, fw_state, jiffies));
1238
1239 if (rval) {
1240 DEBUG2_3(printk("scsi(%ld): Firmware ready **** FAILED ****.\n",
1241 ha->host_no));
1242 }
1243
1244 return (rval);
1245}
1246
1247/*
1248* qla2x00_configure_hba
1249* Setup adapter context.
1250*
1251* Input:
1252* ha = adapter state pointer.
1253*
1254* Returns:
1255* 0 = success
1256*
1257* Context:
1258* Kernel context.
1259*/
1260static int
1261qla2x00_configure_hba(scsi_qla_host_t *ha)
1262{
1263 int rval;
1264 uint16_t loop_id;
1265 uint16_t topo;
1266 uint8_t al_pa;
1267 uint8_t area;
1268 uint8_t domain;
1269 char connect_type[22];
1270
1271 /* Get host addresses. */
1272 rval = qla2x00_get_adapter_id(ha,
1273 &loop_id, &al_pa, &area, &domain, &topo);
1274 if (rval != QLA_SUCCESS) {
Andrew Vasquez23443b12005-12-06 10:57:06 -08001275 if (LOOP_TRANSITION(ha) || atomic_read(&ha->loop_down_timer) ||
Ravi Anand33135aa2005-11-08 14:37:20 -08001276 (rval == QLA_COMMAND_ERROR && loop_id == 0x7)) {
1277 DEBUG2(printk("%s(%ld) Loop is in a transition state\n",
1278 __func__, ha->host_no));
1279 } else {
1280 qla_printk(KERN_WARNING, ha,
1281 "ERROR -- Unable to get host loop ID.\n");
1282 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
1283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return (rval);
1285 }
1286
1287 if (topo == 4) {
1288 qla_printk(KERN_INFO, ha,
1289 "Cannot get topology - retrying.\n");
1290 return (QLA_FUNCTION_FAILED);
1291 }
1292
1293 ha->loop_id = loop_id;
1294
1295 /* initialize */
1296 ha->min_external_loopid = SNS_FIRST_LOOP_ID;
1297 ha->operating_mode = LOOP;
1298
1299 switch (topo) {
1300 case 0:
1301 DEBUG3(printk("scsi(%ld): HBA in NL topology.\n",
1302 ha->host_no));
1303 ha->current_topology = ISP_CFG_NL;
1304 strcpy(connect_type, "(Loop)");
1305 break;
1306
1307 case 1:
1308 DEBUG3(printk("scsi(%ld): HBA in FL topology.\n",
1309 ha->host_no));
1310 ha->current_topology = ISP_CFG_FL;
1311 strcpy(connect_type, "(FL_Port)");
1312 break;
1313
1314 case 2:
1315 DEBUG3(printk("scsi(%ld): HBA in N P2P topology.\n",
1316 ha->host_no));
1317 ha->operating_mode = P2P;
1318 ha->current_topology = ISP_CFG_N;
1319 strcpy(connect_type, "(N_Port-to-N_Port)");
1320 break;
1321
1322 case 3:
1323 DEBUG3(printk("scsi(%ld): HBA in F P2P topology.\n",
1324 ha->host_no));
1325 ha->operating_mode = P2P;
1326 ha->current_topology = ISP_CFG_F;
1327 strcpy(connect_type, "(F_Port)");
1328 break;
1329
1330 default:
1331 DEBUG3(printk("scsi(%ld): HBA in unknown topology %x. "
1332 "Using NL.\n",
1333 ha->host_no, topo));
1334 ha->current_topology = ISP_CFG_NL;
1335 strcpy(connect_type, "(Loop)");
1336 break;
1337 }
1338
1339 /* Save Host port and loop ID. */
1340 /* byte order - Big Endian */
1341 ha->d_id.b.domain = domain;
1342 ha->d_id.b.area = area;
1343 ha->d_id.b.al_pa = al_pa;
1344
1345 if (!ha->flags.init_done)
1346 qla_printk(KERN_INFO, ha,
1347 "Topology - %s, Host Loop address 0x%x\n",
1348 connect_type, ha->loop_id);
1349
1350 if (rval) {
1351 DEBUG2_3(printk("scsi(%ld): FAILED.\n", ha->host_no));
1352 } else {
1353 DEBUG3(printk("scsi(%ld): exiting normally.\n", ha->host_no));
1354 }
1355
1356 return(rval);
1357}
1358
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08001359static inline void
1360qla2x00_set_model_info(scsi_qla_host_t *ha, uint8_t *model, size_t len, char *def)
1361{
1362 char *st, *en;
1363 uint16_t index;
1364
1365 if (memcmp(model, BINZERO, len) != 0) {
1366 strncpy(ha->model_number, model, len);
1367 st = en = ha->model_number;
1368 en += len - 1;
1369 while (en > st) {
1370 if (*en != 0x20 && *en != 0x00)
1371 break;
1372 *en-- = '\0';
1373 }
1374
1375 index = (ha->pdev->subsystem_device & 0xff);
1376 if (ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
1377 index < QLA_MODEL_NAMES)
1378 ha->model_desc = qla2x00_model_name[index * 2 + 1];
1379 } else {
1380 index = (ha->pdev->subsystem_device & 0xff);
1381 if (ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
1382 index < QLA_MODEL_NAMES) {
1383 strcpy(ha->model_number,
1384 qla2x00_model_name[index * 2]);
1385 ha->model_desc = qla2x00_model_name[index * 2 + 1];
1386 } else {
1387 strcpy(ha->model_number, def);
1388 }
1389 }
1390}
1391
David Miller4e08df32007-04-16 12:37:43 -07001392/* On sparc systems, obtain port and node WWN from firmware
1393 * properties.
1394 */
1395static void qla2xxx_nvram_wwn_from_ofw(scsi_qla_host_t *ha, nvram_t *nv)
1396{
1397#ifdef CONFIG_SPARC
1398 struct pci_dev *pdev = ha->pdev;
David S. Miller15576bc2007-05-08 00:36:49 -07001399 struct device_node *dp = pci_device_to_OF_node(pdev);
1400 const u8 *val;
David Miller4e08df32007-04-16 12:37:43 -07001401 int len;
1402
1403 val = of_get_property(dp, "port-wwn", &len);
1404 if (val && len >= WWN_SIZE)
1405 memcpy(nv->port_name, val, WWN_SIZE);
1406
1407 val = of_get_property(dp, "node-wwn", &len);
1408 if (val && len >= WWN_SIZE)
1409 memcpy(nv->node_name, val, WWN_SIZE);
1410#endif
1411}
1412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413/*
1414* NVRAM configuration for ISP 2xxx
1415*
1416* Input:
1417* ha = adapter block pointer.
1418*
1419* Output:
1420* initialization control block in response_ring
1421* host adapters parameters in host adapter block
1422*
1423* Returns:
1424* 0 = success.
1425*/
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001426int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427qla2x00_nvram_config(scsi_qla_host_t *ha)
1428{
David Miller4e08df32007-04-16 12:37:43 -07001429 int rval;
Andrew Vasquez01071092005-07-06 10:31:37 -07001430 uint8_t chksum = 0;
1431 uint16_t cnt;
1432 uint8_t *dptr1, *dptr2;
1433 init_cb_t *icb = ha->init_cb;
1434 nvram_t *nv = (nvram_t *)ha->request_ring;
1435 uint8_t *ptr = (uint8_t *)ha->request_ring;
Andrew Vasquez3d716442005-07-06 10:30:26 -07001436 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
David Miller4e08df32007-04-16 12:37:43 -07001438 rval = QLA_SUCCESS;
1439
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 /* Determine NVRAM starting address. */
Andrew Vasquez01071092005-07-06 10:31:37 -07001441 ha->nvram_size = sizeof(nvram_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 ha->nvram_base = 0;
1443 if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha))
1444 if ((RD_REG_WORD(&reg->ctrl_status) >> 14) == 1)
1445 ha->nvram_base = 0x80;
1446
1447 /* Get NVRAM data and calculate checksum. */
Andrew Vasquez01071092005-07-06 10:31:37 -07001448 ha->isp_ops.read_nvram(ha, ptr, ha->nvram_base, ha->nvram_size);
1449 for (cnt = 0, chksum = 0; cnt < ha->nvram_size; cnt++)
1450 chksum += *ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
1452 DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
1453 DEBUG5(qla2x00_dump_buffer((uint8_t *)ha->request_ring,
Andrew Vasquez01071092005-07-06 10:31:37 -07001454 ha->nvram_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
1456 /* Bad NVRAM data, set defaults parameters. */
1457 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' ||
1458 nv->id[2] != 'P' || nv->id[3] != ' ' || nv->nvram_version < 1) {
1459 /* Reset NVRAM data. */
1460 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
1461 "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
1462 nv->nvram_version);
David Miller4e08df32007-04-16 12:37:43 -07001463 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
1464 "invalid -- WWPN) defaults.\n");
1465
1466 /*
1467 * Set default initialization control block.
1468 */
1469 memset(nv, 0, ha->nvram_size);
1470 nv->parameter_block_version = ICB_VERSION;
1471
1472 if (IS_QLA23XX(ha)) {
1473 nv->firmware_options[0] = BIT_2 | BIT_1;
1474 nv->firmware_options[1] = BIT_7 | BIT_5;
1475 nv->add_firmware_options[0] = BIT_5;
1476 nv->add_firmware_options[1] = BIT_5 | BIT_4;
1477 nv->frame_payload_size = __constant_cpu_to_le16(2048);
1478 nv->special_options[1] = BIT_7;
1479 } else if (IS_QLA2200(ha)) {
1480 nv->firmware_options[0] = BIT_2 | BIT_1;
1481 nv->firmware_options[1] = BIT_7 | BIT_5;
1482 nv->add_firmware_options[0] = BIT_5;
1483 nv->add_firmware_options[1] = BIT_5 | BIT_4;
1484 nv->frame_payload_size = __constant_cpu_to_le16(1024);
1485 } else if (IS_QLA2100(ha)) {
1486 nv->firmware_options[0] = BIT_3 | BIT_1;
1487 nv->firmware_options[1] = BIT_5;
1488 nv->frame_payload_size = __constant_cpu_to_le16(1024);
1489 }
1490
1491 nv->max_iocb_allocation = __constant_cpu_to_le16(256);
1492 nv->execution_throttle = __constant_cpu_to_le16(16);
1493 nv->retry_count = 8;
1494 nv->retry_delay = 1;
1495
1496 nv->port_name[0] = 33;
1497 nv->port_name[3] = 224;
1498 nv->port_name[4] = 139;
1499
1500 qla2xxx_nvram_wwn_from_ofw(ha, nv);
1501
1502 nv->login_timeout = 4;
1503
1504 /*
1505 * Set default host adapter parameters
1506 */
1507 nv->host_p[1] = BIT_2;
1508 nv->reset_delay = 5;
1509 nv->port_down_retry_count = 8;
1510 nv->max_luns_per_target = __constant_cpu_to_le16(8);
1511 nv->link_down_timeout = 60;
1512
1513 rval = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 }
1515
1516#if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
1517 /*
1518 * The SN2 does not provide BIOS emulation which means you can't change
1519 * potentially bogus BIOS settings. Force the use of default settings
1520 * for link rate and frame size. Hope that the rest of the settings
1521 * are valid.
1522 */
1523 if (ia64_platform_is("sn2")) {
1524 nv->frame_payload_size = __constant_cpu_to_le16(2048);
1525 if (IS_QLA23XX(ha))
1526 nv->special_options[1] = BIT_7;
1527 }
1528#endif
1529
1530 /* Reset Initialization control block */
Andrew Vasquez01071092005-07-06 10:31:37 -07001531 memset(icb, 0, ha->init_cb_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
1533 /*
1534 * Setup driver NVRAM options.
1535 */
1536 nv->firmware_options[0] |= (BIT_6 | BIT_1);
1537 nv->firmware_options[0] &= ~(BIT_5 | BIT_4);
1538 nv->firmware_options[1] |= (BIT_5 | BIT_0);
1539 nv->firmware_options[1] &= ~BIT_4;
1540
1541 if (IS_QLA23XX(ha)) {
1542 nv->firmware_options[0] |= BIT_2;
1543 nv->firmware_options[0] &= ~BIT_3;
Andrew Vasquez01071092005-07-06 10:31:37 -07001544 nv->add_firmware_options[1] |= BIT_5 | BIT_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
1546 if (IS_QLA2300(ha)) {
1547 if (ha->fb_rev == FPM_2310) {
1548 strcpy(ha->model_number, "QLA2310");
1549 } else {
1550 strcpy(ha->model_number, "QLA2300");
1551 }
1552 } else {
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08001553 qla2x00_set_model_info(ha, nv->model_number,
1554 sizeof(nv->model_number), "QLA23xx");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 }
1556 } else if (IS_QLA2200(ha)) {
1557 nv->firmware_options[0] |= BIT_2;
1558 /*
1559 * 'Point-to-point preferred, else loop' is not a safe
1560 * connection mode setting.
1561 */
1562 if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
1563 (BIT_5 | BIT_4)) {
1564 /* Force 'loop preferred, else point-to-point'. */
1565 nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
1566 nv->add_firmware_options[0] |= BIT_5;
1567 }
1568 strcpy(ha->model_number, "QLA22xx");
1569 } else /*if (IS_QLA2100(ha))*/ {
1570 strcpy(ha->model_number, "QLA2100");
1571 }
1572
1573 /*
1574 * Copy over NVRAM RISC parameter block to initialization control block.
1575 */
1576 dptr1 = (uint8_t *)icb;
1577 dptr2 = (uint8_t *)&nv->parameter_block_version;
1578 cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
1579 while (cnt--)
1580 *dptr1++ = *dptr2++;
1581
1582 /* Copy 2nd half. */
1583 dptr1 = (uint8_t *)icb->add_firmware_options;
1584 cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
1585 while (cnt--)
1586 *dptr1++ = *dptr2++;
1587
Andrew Vasquez5341e862006-05-17 15:09:16 -07001588 /* Use alternate WWN? */
1589 if (nv->host_p[1] & BIT_7) {
1590 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
1591 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
1592 }
1593
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 /* Prepare nodename */
1595 if ((icb->firmware_options[1] & BIT_6) == 0) {
1596 /*
1597 * Firmware will apply the following mask if the nodename was
1598 * not provided.
1599 */
1600 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
1601 icb->node_name[0] &= 0xF0;
1602 }
1603
1604 /*
1605 * Set host adapter parameters.
1606 */
Andrew Vasquez01819442006-06-23 16:11:10 -07001607 if (nv->host_p[0] & BIT_7)
Andrew Vasquez11010fe2006-10-06 09:54:59 -07001608 ql2xextended_error_logging = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
1610 /* Always load RISC code on non ISP2[12]00 chips. */
1611 if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
1612 ha->flags.disable_risc_code_load = 0;
1613 ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
1614 ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
1615 ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
Andrew Vasquez06c22bd2005-08-26 19:09:00 -07001616 ha->flags.enable_led_scheme = (nv->special_options[1] & BIT_4) ? 1 : 0;
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07001617 ha->flags.disable_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
1619 ha->operating_mode =
1620 (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
1621
1622 memcpy(ha->fw_seriallink_options, nv->seriallink_options,
1623 sizeof(ha->fw_seriallink_options));
1624
1625 /* save HBA serial number */
1626 ha->serial0 = icb->port_name[5];
1627 ha->serial1 = icb->port_name[6];
1628 ha->serial2 = icb->port_name[7];
Andrew Vasquez3d716442005-07-06 10:30:26 -07001629 ha->node_name = icb->node_name;
1630 ha->port_name = icb->port_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
1632 icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
1633
1634 ha->retry_count = nv->retry_count;
1635
1636 /* Set minimum login_timeout to 4 seconds. */
1637 if (nv->login_timeout < ql2xlogintimeout)
1638 nv->login_timeout = ql2xlogintimeout;
1639 if (nv->login_timeout < 4)
1640 nv->login_timeout = 4;
1641 ha->login_timeout = nv->login_timeout;
1642 icb->login_timeout = nv->login_timeout;
1643
1644 /* Set minimum RATOV to 200 tenths of a second. */
1645 ha->r_a_tov = 200;
1646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 ha->loop_reset_delay = nv->reset_delay;
1648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 /* Link Down Timeout = 0:
1650 *
1651 * When Port Down timer expires we will start returning
1652 * I/O's to OS with "DID_NO_CONNECT".
1653 *
1654 * Link Down Timeout != 0:
1655 *
1656 * The driver waits for the link to come up after link down
1657 * before returning I/Os to OS with "DID_NO_CONNECT".
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001658 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 if (nv->link_down_timeout == 0) {
1660 ha->loop_down_abort_time =
Andrew Vasquez 354d6b22005-04-23 02:47:27 -04001661 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 } else {
1663 ha->link_down_timeout = nv->link_down_timeout;
1664 ha->loop_down_abort_time =
1665 (LOOP_DOWN_TIME - ha->link_down_timeout);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 /*
1669 * Need enough time to try and get the port back.
1670 */
1671 ha->port_down_retry_count = nv->port_down_retry_count;
1672 if (qlport_down_retry)
1673 ha->port_down_retry_count = qlport_down_retry;
1674 /* Set login_retry_count */
1675 ha->login_retry_count = nv->retry_count;
1676 if (ha->port_down_retry_count == nv->port_down_retry_count &&
1677 ha->port_down_retry_count > 3)
1678 ha->login_retry_count = ha->port_down_retry_count;
1679 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
1680 ha->login_retry_count = ha->port_down_retry_count;
1681 if (ql2xloginretrycount)
1682 ha->login_retry_count = ql2xloginretrycount;
1683
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 icb->lun_enables = __constant_cpu_to_le16(0);
1685 icb->command_resource_count = 0;
1686 icb->immediate_notify_resource_count = 0;
1687 icb->timeout = __constant_cpu_to_le16(0);
1688
1689 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
1690 /* Enable RIO */
1691 icb->firmware_options[0] &= ~BIT_3;
1692 icb->add_firmware_options[0] &=
1693 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
1694 icb->add_firmware_options[0] |= BIT_2;
1695 icb->response_accumulation_timer = 3;
1696 icb->interrupt_delay_timer = 5;
1697
1698 ha->flags.process_response_queue = 1;
1699 } else {
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001700 /* Enable ZIO. */
1701 if (!ha->flags.init_done) {
1702 ha->zio_mode = icb->add_firmware_options[0] &
1703 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
1704 ha->zio_timer = icb->interrupt_delay_timer ?
1705 icb->interrupt_delay_timer: 2;
1706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 icb->add_firmware_options[0] &=
1708 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001709 ha->flags.process_response_queue = 0;
1710 if (ha->zio_mode != QLA_ZIO_DISABLED) {
andrew.vasquez@qlogic.com4a59f712006-03-09 14:27:39 -08001711 ha->zio_mode = QLA_ZIO_MODE_6;
1712
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001713 DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer "
1714 "delay (%d us).\n", ha->host_no, ha->zio_mode,
1715 ha->zio_timer * 100));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 qla_printk(KERN_INFO, ha,
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001717 "ZIO mode %d enabled; timer delay (%d us).\n",
1718 ha->zio_mode, ha->zio_timer * 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001720 icb->add_firmware_options[0] |= (uint8_t)ha->zio_mode;
1721 icb->interrupt_delay_timer = (uint8_t)ha->zio_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 ha->flags.process_response_queue = 1;
1723 }
1724 }
1725
David Miller4e08df32007-04-16 12:37:43 -07001726 if (rval) {
1727 DEBUG2_3(printk(KERN_WARNING
1728 "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
1729 }
1730 return (rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731}
1732
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001733static void
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001734qla2x00_rport_del(void *data)
1735{
1736 fc_port_t *fcport = data;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08001737 struct fc_rport *rport;
1738 unsigned long flags;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001739
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08001740 spin_lock_irqsave(&fcport->rport_lock, flags);
1741 rport = fcport->drport;
1742 fcport->drport = NULL;
1743 spin_unlock_irqrestore(&fcport->rport_lock, flags);
1744 if (rport)
1745 fc_remote_port_delete(rport);
1746
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001747}
1748
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749/**
1750 * qla2x00_alloc_fcport() - Allocate a generic fcport.
1751 * @ha: HA context
1752 * @flags: allocation flags
1753 *
1754 * Returns a pointer to the allocated fcport, or NULL, if none available.
1755 */
Adrian Bunk413975a2006-06-30 02:33:06 -07001756static fc_port_t *
Al Viroc53033f2005-10-21 03:22:08 -04001757qla2x00_alloc_fcport(scsi_qla_host_t *ha, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758{
1759 fc_port_t *fcport;
1760
1761 fcport = kmalloc(sizeof(fc_port_t), flags);
1762 if (fcport == NULL)
1763 return (fcport);
1764
1765 /* Setup fcport template structure. */
1766 memset(fcport, 0, sizeof (fc_port_t));
1767 fcport->ha = ha;
1768 fcport->port_type = FCT_UNKNOWN;
1769 fcport->loop_id = FC_NO_LOOP_ID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 atomic_set(&fcport->state, FCS_UNCONFIGURED);
1771 fcport->flags = FCF_RLC_SUPPORT;
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07001772 fcport->supported_classes = FC_COS_UNSPECIFIED;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08001773 spin_lock_init(&fcport->rport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
1775 return (fcport);
1776}
1777
1778/*
1779 * qla2x00_configure_loop
1780 * Updates Fibre Channel Device Database with what is actually on loop.
1781 *
1782 * Input:
1783 * ha = adapter block pointer.
1784 *
1785 * Returns:
1786 * 0 = success.
1787 * 1 = error.
1788 * 2 = database was full and device was not configured.
1789 */
1790static int
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001791qla2x00_configure_loop(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792{
1793 int rval;
1794 unsigned long flags, save_flags;
1795
1796 rval = QLA_SUCCESS;
1797
1798 /* Get Initiator ID */
1799 if (test_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags)) {
1800 rval = qla2x00_configure_hba(ha);
1801 if (rval != QLA_SUCCESS) {
1802 DEBUG(printk("scsi(%ld): Unable to configure HBA.\n",
1803 ha->host_no));
1804 return (rval);
1805 }
1806 }
1807
1808 save_flags = flags = ha->dpc_flags;
1809 DEBUG(printk("scsi(%ld): Configure loop -- dpc flags =0x%lx\n",
1810 ha->host_no, flags));
1811
1812 /*
1813 * If we have both an RSCN and PORT UPDATE pending then handle them
1814 * both at the same time.
1815 */
1816 clear_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1817 clear_bit(RSCN_UPDATE, &ha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
1819 /* Determine what we need to do */
1820 if (ha->current_topology == ISP_CFG_FL &&
1821 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1822
1823 ha->flags.rscn_queue_overflow = 1;
1824 set_bit(RSCN_UPDATE, &flags);
1825
1826 } else if (ha->current_topology == ISP_CFG_F &&
1827 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1828
1829 ha->flags.rscn_queue_overflow = 1;
1830 set_bit(RSCN_UPDATE, &flags);
1831 clear_bit(LOCAL_LOOP_UPDATE, &flags);
1832
Andrew Vasquez21333b42006-05-17 15:09:56 -07001833 } else if (ha->current_topology == ISP_CFG_N) {
1834 clear_bit(RSCN_UPDATE, &flags);
1835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 } else if (!ha->flags.online ||
1837 (test_bit(ABORT_ISP_ACTIVE, &flags))) {
1838
1839 ha->flags.rscn_queue_overflow = 1;
1840 set_bit(RSCN_UPDATE, &flags);
1841 set_bit(LOCAL_LOOP_UPDATE, &flags);
1842 }
1843
1844 if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
1845 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1846 rval = QLA_FUNCTION_FAILED;
1847 } else {
1848 rval = qla2x00_configure_local_loop(ha);
1849 }
1850 }
1851
1852 if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
Andrew Vasquez23443b12005-12-06 10:57:06 -08001853 if (LOOP_TRANSITION(ha)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 rval = QLA_FUNCTION_FAILED;
1855 } else {
1856 rval = qla2x00_configure_fabric(ha);
1857 }
1858 }
1859
1860 if (rval == QLA_SUCCESS) {
1861 if (atomic_read(&ha->loop_down_timer) ||
1862 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1863 rval = QLA_FUNCTION_FAILED;
1864 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 atomic_set(&ha->loop_state, LOOP_READY);
1866
1867 DEBUG(printk("scsi(%ld): LOOP READY\n", ha->host_no));
1868 }
1869 }
1870
1871 if (rval) {
1872 DEBUG2_3(printk("%s(%ld): *** FAILED ***\n",
1873 __func__, ha->host_no));
1874 } else {
1875 DEBUG3(printk("%s: exiting normally\n", __func__));
1876 }
1877
1878 /* Restore state if a resync event occured during processing */
1879 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1880 if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
1881 set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1882 if (test_bit(RSCN_UPDATE, &save_flags))
1883 set_bit(RSCN_UPDATE, &ha->dpc_flags);
1884 }
1885
1886 return (rval);
1887}
1888
1889
1890
1891/*
1892 * qla2x00_configure_local_loop
1893 * Updates Fibre Channel Device Database with local loop devices.
1894 *
1895 * Input:
1896 * ha = adapter block pointer.
1897 *
1898 * Returns:
1899 * 0 = success.
1900 */
1901static int
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001902qla2x00_configure_local_loop(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903{
1904 int rval, rval2;
1905 int found_devs;
1906 int found;
1907 fc_port_t *fcport, *new_fcport;
1908
1909 uint16_t index;
1910 uint16_t entries;
1911 char *id_iter;
1912 uint16_t loop_id;
1913 uint8_t domain, area, al_pa;
1914
1915 found_devs = 0;
1916 new_fcport = NULL;
1917 entries = MAX_FIBRE_DEVICES;
1918
1919 DEBUG3(printk("scsi(%ld): Getting FCAL position map\n", ha->host_no));
1920 DEBUG3(qla2x00_get_fcal_position_map(ha, NULL));
1921
1922 /* Get list of logged in devices. */
1923 memset(ha->gid_list, 0, GID_LIST_SIZE);
1924 rval = qla2x00_get_id_list(ha, ha->gid_list, ha->gid_list_dma,
1925 &entries);
1926 if (rval != QLA_SUCCESS)
1927 goto cleanup_allocation;
1928
1929 DEBUG3(printk("scsi(%ld): Entries in ID list (%d)\n",
1930 ha->host_no, entries));
1931 DEBUG3(qla2x00_dump_buffer((uint8_t *)ha->gid_list,
1932 entries * sizeof(struct gid_list_info)));
1933
1934 /* Allocate temporary fcport for any new fcports discovered. */
1935 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
1936 if (new_fcport == NULL) {
1937 rval = QLA_MEMORY_ALLOC_FAILED;
1938 goto cleanup_allocation;
1939 }
1940 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
1941
1942 /*
1943 * Mark local devices that were present with FCF_DEVICE_LOST for now.
1944 */
1945 list_for_each_entry(fcport, &ha->fcports, list) {
1946 if (atomic_read(&fcport->state) == FCS_ONLINE &&
1947 fcport->port_type != FCT_BROADCAST &&
1948 (fcport->flags & FCF_FABRIC_DEVICE) == 0) {
1949
1950 DEBUG(printk("scsi(%ld): Marking port lost, "
1951 "loop_id=0x%04x\n",
1952 ha->host_no, fcport->loop_id));
1953
1954 atomic_set(&fcport->state, FCS_DEVICE_LOST);
1955 fcport->flags &= ~FCF_FARP_DONE;
1956 }
1957 }
1958
1959 /* Add devices to port list. */
1960 id_iter = (char *)ha->gid_list;
1961 for (index = 0; index < entries; index++) {
1962 domain = ((struct gid_list_info *)id_iter)->domain;
1963 area = ((struct gid_list_info *)id_iter)->area;
1964 al_pa = ((struct gid_list_info *)id_iter)->al_pa;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001965 if (IS_QLA2100(ha) || IS_QLA2200(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 loop_id = (uint16_t)
1967 ((struct gid_list_info *)id_iter)->loop_id_2100;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001968 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 loop_id = le16_to_cpu(
1970 ((struct gid_list_info *)id_iter)->loop_id);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001971 id_iter += ha->gid_list_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
1973 /* Bypass reserved domain fields. */
1974 if ((domain & 0xf0) == 0xf0)
1975 continue;
1976
1977 /* Bypass if not same domain and area of adapter. */
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07001978 if (area && domain &&
1979 (area != ha->d_id.b.area || domain != ha->d_id.b.domain))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 continue;
1981
1982 /* Bypass invalid local loop ID. */
1983 if (loop_id > LAST_LOCAL_LOOP_ID)
1984 continue;
1985
1986 /* Fill in member data. */
1987 new_fcport->d_id.b.domain = domain;
1988 new_fcport->d_id.b.area = area;
1989 new_fcport->d_id.b.al_pa = al_pa;
1990 new_fcport->loop_id = loop_id;
1991 rval2 = qla2x00_get_port_database(ha, new_fcport, 0);
1992 if (rval2 != QLA_SUCCESS) {
1993 DEBUG2(printk("scsi(%ld): Failed to retrieve fcport "
1994 "information -- get_port_database=%x, "
1995 "loop_id=0x%04x\n",
1996 ha->host_no, rval2, new_fcport->loop_id));
andrew.vasquez@qlogic.comc9d02ac2006-01-13 17:05:26 -08001997 DEBUG2(printk("scsi(%ld): Scheduling resync...\n",
1998 ha->host_no));
1999 set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 continue;
2001 }
2002
2003 /* Check for matching device in port list. */
2004 found = 0;
2005 fcport = NULL;
2006 list_for_each_entry(fcport, &ha->fcports, list) {
2007 if (memcmp(new_fcport->port_name, fcport->port_name,
2008 WWN_SIZE))
2009 continue;
2010
2011 fcport->flags &= ~(FCF_FABRIC_DEVICE |
2012 FCF_PERSISTENT_BOUND);
2013 fcport->loop_id = new_fcport->loop_id;
2014 fcport->port_type = new_fcport->port_type;
2015 fcport->d_id.b24 = new_fcport->d_id.b24;
2016 memcpy(fcport->node_name, new_fcport->node_name,
2017 WWN_SIZE);
2018
2019 found++;
2020 break;
2021 }
2022
2023 if (!found) {
2024 /* New device, add to fcports list. */
2025 new_fcport->flags &= ~FCF_PERSISTENT_BOUND;
2026 list_add_tail(&new_fcport->list, &ha->fcports);
2027
2028 /* Allocate a new replacement fcport. */
2029 fcport = new_fcport;
2030 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2031 if (new_fcport == NULL) {
2032 rval = QLA_MEMORY_ALLOC_FAILED;
2033 goto cleanup_allocation;
2034 }
2035 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
2036 }
2037
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002038 /* Base iIDMA settings on HBA port speed. */
2039 switch (ha->link_data_rate) {
2040 case PORT_SPEED_1GB:
2041 fcport->fp_speed = cpu_to_be16(BIT_15);
2042 break;
2043 case PORT_SPEED_2GB:
2044 fcport->fp_speed = cpu_to_be16(BIT_14);
2045 break;
2046 case PORT_SPEED_4GB:
2047 fcport->fp_speed = cpu_to_be16(BIT_13);
2048 break;
2049 }
2050
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 qla2x00_update_fcport(ha, fcport);
2052
2053 found_devs++;
2054 }
2055
2056cleanup_allocation:
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002057 kfree(new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
2059 if (rval != QLA_SUCCESS) {
2060 DEBUG2(printk("scsi(%ld): Configure local loop error exit: "
2061 "rval=%x\n", ha->host_no, rval));
2062 }
2063
2064 if (found_devs) {
2065 ha->device_flags |= DFLG_LOCAL_DEVICES;
2066 ha->device_flags &= ~DFLG_RETRY_LOCAL_DEVICES;
2067 }
2068
2069 return (rval);
2070}
2071
2072static void
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002073qla2x00_probe_for_all_luns(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074{
2075 fc_port_t *fcport;
2076
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002077 qla2x00_mark_all_devices_lost(ha, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 list_for_each_entry(fcport, &ha->fcports, list) {
2079 if (fcport->port_type != FCT_TARGET)
2080 continue;
2081
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002082 qla2x00_update_fcport(ha, fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 }
2084}
2085
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002086static void
2087qla2x00_iidma_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
2088{
2089#define LS_UNKNOWN 2
2090 static char *link_speeds[5] = { "1", "2", "?", "4" };
2091 int rval;
2092 uint16_t port_speed, mb[6];
2093
2094 if (!IS_QLA24XX(ha))
2095 return;
2096
2097 switch (be16_to_cpu(fcport->fp_speed)) {
2098 case BIT_15:
2099 port_speed = PORT_SPEED_1GB;
2100 break;
2101 case BIT_14:
2102 port_speed = PORT_SPEED_2GB;
2103 break;
2104 case BIT_13:
2105 port_speed = PORT_SPEED_4GB;
2106 break;
2107 default:
2108 DEBUG2(printk("scsi(%ld): %02x%02x%02x%02x%02x%02x%02x%02x -- "
2109 "unsupported FM port operating speed (%04x).\n",
2110 ha->host_no, fcport->port_name[0], fcport->port_name[1],
2111 fcport->port_name[2], fcport->port_name[3],
2112 fcport->port_name[4], fcport->port_name[5],
2113 fcport->port_name[6], fcport->port_name[7],
2114 be16_to_cpu(fcport->fp_speed)));
2115 port_speed = PORT_SPEED_UNKNOWN;
2116 break;
2117 }
2118 if (port_speed == PORT_SPEED_UNKNOWN)
2119 return;
2120
2121 rval = qla2x00_set_idma_speed(ha, fcport->loop_id, port_speed, mb);
2122 if (rval != QLA_SUCCESS) {
2123 DEBUG2(printk("scsi(%ld): Unable to adjust iIDMA "
2124 "%02x%02x%02x%02x%02x%02x%02x%02x -- %04x %x %04x %04x.\n",
2125 ha->host_no, fcport->port_name[0], fcport->port_name[1],
2126 fcport->port_name[2], fcport->port_name[3],
2127 fcport->port_name[4], fcport->port_name[5],
2128 fcport->port_name[6], fcport->port_name[7], rval,
2129 port_speed, mb[0], mb[1]));
2130 } else {
2131 DEBUG2(qla_printk(KERN_INFO, ha,
2132 "iIDMA adjusted to %s GB/s on "
2133 "%02x%02x%02x%02x%02x%02x%02x%02x.\n",
2134 link_speeds[port_speed], fcport->port_name[0],
2135 fcport->port_name[1], fcport->port_name[2],
2136 fcport->port_name[3], fcport->port_name[4],
2137 fcport->port_name[5], fcport->port_name[6],
2138 fcport->port_name[7]));
2139 }
2140}
2141
Adrian Bunk23be3312006-11-24 02:46:01 +01002142static void
8482e112005-04-17 15:04:54 -05002143qla2x00_reg_remote_port(scsi_qla_host_t *ha, fc_port_t *fcport)
2144{
2145 struct fc_rport_identifiers rport_ids;
bdf79622005-04-17 15:06:53 -05002146 struct fc_rport *rport;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002147 unsigned long flags;
8482e112005-04-17 15:04:54 -05002148
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002149 if (fcport->drport)
2150 qla2x00_rport_del(fcport);
2151 if (fcport->rport)
2152 return;
8482e112005-04-17 15:04:54 -05002153
Andrew Vasquezf8b02a82005-08-31 15:21:20 -07002154 rport_ids.node_name = wwn_to_u64(fcport->node_name);
2155 rport_ids.port_name = wwn_to_u64(fcport->port_name);
8482e112005-04-17 15:04:54 -05002156 rport_ids.port_id = fcport->d_id.b.domain << 16 |
2157 fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
2158 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002159 rport = fc_remote_port_add(ha->host, 0, &rport_ids);
Andrew Vasquez77d74142005-07-08 18:00:36 -07002160 if (!rport) {
2161 qla_printk(KERN_WARNING, ha,
2162 "Unable to allocate fc remote port!\n");
2163 return;
2164 }
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002165 spin_lock_irqsave(&fcport->rport_lock, flags);
2166 fcport->rport = rport;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002167 *((fc_port_t **)rport->dd_data) = fcport;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002168 spin_unlock_irqrestore(&fcport->rport_lock, flags);
2169
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07002170 rport->supported_classes = fcport->supported_classes;
Andrew Vasquez77d74142005-07-08 18:00:36 -07002171
2172 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
8482e112005-04-17 15:04:54 -05002173 if (fcport->port_type == FCT_INITIATOR)
2174 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2175 if (fcport->port_type == FCT_TARGET)
2176 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
Andrew Vasquez77d74142005-07-08 18:00:36 -07002177 fc_remote_port_rolechg(rport, rport_ids.roles);
bdf79622005-04-17 15:06:53 -05002178
Andrew Vasquezcc4731f2005-07-06 10:32:37 -07002179 if (rport->scsi_target_id != -1 &&
2180 rport->scsi_target_id < ha->host->max_id)
bdf79622005-04-17 15:06:53 -05002181 fcport->os_target_id = rport->scsi_target_id;
8482e112005-04-17 15:04:54 -05002182}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
2184/*
Adrian Bunk23be3312006-11-24 02:46:01 +01002185 * qla2x00_update_fcport
2186 * Updates device on list.
2187 *
2188 * Input:
2189 * ha = adapter block pointer.
2190 * fcport = port structure pointer.
2191 *
2192 * Return:
2193 * 0 - Success
2194 * BIT_0 - error
2195 *
2196 * Context:
2197 * Kernel context.
2198 */
2199void
2200qla2x00_update_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
2201{
2202 fcport->ha = ha;
2203 fcport->login_retry = 0;
2204 fcport->port_login_retry_count = ha->port_down_retry_count *
2205 PORT_RETRY_TIME;
2206 atomic_set(&fcport->port_down_timer, ha->port_down_retry_count *
2207 PORT_RETRY_TIME);
2208 fcport->flags &= ~FCF_LOGIN_NEEDED;
2209
2210 qla2x00_iidma_fcport(ha, fcport);
2211
2212 atomic_set(&fcport->state, FCS_ONLINE);
2213
2214 qla2x00_reg_remote_port(ha, fcport);
2215}
2216
2217/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 * qla2x00_configure_fabric
2219 * Setup SNS devices with loop ID's.
2220 *
2221 * Input:
2222 * ha = adapter block pointer.
2223 *
2224 * Returns:
2225 * 0 = success.
2226 * BIT_0 = error
2227 */
2228static int
2229qla2x00_configure_fabric(scsi_qla_host_t *ha)
2230{
2231 int rval, rval2;
2232 fc_port_t *fcport, *fcptemp;
2233 uint16_t next_loopid;
2234 uint16_t mb[MAILBOX_REGISTER_COUNT];
Andrew Vasquez01071092005-07-06 10:31:37 -07002235 uint16_t loop_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 LIST_HEAD(new_fcports);
2237
2238 /* If FL port exists, then SNS is present */
andrew.vasquez@qlogic.com044cc6c2006-03-09 14:27:13 -08002239 if (IS_QLA24XX(ha) || IS_QLA54XX(ha))
Andrew Vasquez01071092005-07-06 10:31:37 -07002240 loop_id = NPH_F_PORT;
2241 else
2242 loop_id = SNS_FL_PORT;
Andrew Vasquez90991c82006-10-02 12:00:46 -07002243 rval = qla2x00_get_port_name(ha, loop_id, ha->fabric_node_name, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 if (rval != QLA_SUCCESS) {
2245 DEBUG2(printk("scsi(%ld): MBC_GET_PORT_NAME Failed, No FL "
2246 "Port\n", ha->host_no));
2247
2248 ha->device_flags &= ~SWITCH_FOUND;
2249 return (QLA_SUCCESS);
2250 }
Andrew Vasquez90991c82006-10-02 12:00:46 -07002251 ha->device_flags |= SWITCH_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
2253 /* Mark devices that need re-synchronization. */
2254 rval2 = qla2x00_device_resync(ha);
2255 if (rval2 == QLA_RSCNS_HANDLED) {
2256 /* No point doing the scan, just continue. */
2257 return (QLA_SUCCESS);
2258 }
2259 do {
Andrew Vasquezcca53352005-08-26 19:08:30 -07002260 /* FDMI support. */
2261 if (ql2xfdmienable &&
2262 test_and_clear_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags))
2263 qla2x00_fdmi_register(ha);
2264
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 /* Ensure we are logged into the SNS. */
andrew.vasquez@qlogic.com044cc6c2006-03-09 14:27:13 -08002266 if (IS_QLA24XX(ha) || IS_QLA54XX(ha))
Andrew Vasquez01071092005-07-06 10:31:37 -07002267 loop_id = NPH_SNS;
2268 else
2269 loop_id = SIMPLE_NAME_SERVER;
2270 ha->isp_ops.fabric_login(ha, loop_id, 0xff, 0xff,
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002271 0xfc, mb, BIT_1 | BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 if (mb[0] != MBS_COMMAND_COMPLETE) {
2273 DEBUG2(qla_printk(KERN_INFO, ha,
2274 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
Andrew Vasquez01071092005-07-06 10:31:37 -07002275 "mb[2]=%x mb[6]=%x mb[7]=%x\n", loop_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 mb[0], mb[1], mb[2], mb[6], mb[7]));
2277 return (QLA_SUCCESS);
2278 }
2279
2280 if (test_and_clear_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags)) {
2281 if (qla2x00_rft_id(ha)) {
2282 /* EMPTY */
2283 DEBUG2(printk("scsi(%ld): Register FC-4 "
2284 "TYPE failed.\n", ha->host_no));
2285 }
2286 if (qla2x00_rff_id(ha)) {
2287 /* EMPTY */
2288 DEBUG2(printk("scsi(%ld): Register FC-4 "
2289 "Features failed.\n", ha->host_no));
2290 }
2291 if (qla2x00_rnn_id(ha)) {
2292 /* EMPTY */
2293 DEBUG2(printk("scsi(%ld): Register Node Name "
2294 "failed.\n", ha->host_no));
2295 } else if (qla2x00_rsnn_nn(ha)) {
2296 /* EMPTY */
2297 DEBUG2(printk("scsi(%ld): Register Symbolic "
2298 "Node Name failed.\n", ha->host_no));
2299 }
2300 }
2301
2302 rval = qla2x00_find_all_fabric_devs(ha, &new_fcports);
2303 if (rval != QLA_SUCCESS)
2304 break;
2305
2306 /*
2307 * Logout all previous fabric devices marked lost, except
2308 * tape devices.
2309 */
2310 list_for_each_entry(fcport, &ha->fcports, list) {
2311 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2312 break;
2313
2314 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0)
2315 continue;
2316
2317 if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
2318 qla2x00_mark_device_lost(ha, fcport,
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002319 ql2xplogiabsentdevice, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 if (fcport->loop_id != FC_NO_LOOP_ID &&
2321 (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2322 fcport->port_type != FCT_INITIATOR &&
2323 fcport->port_type != FCT_BROADCAST) {
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002324 ha->isp_ops.fabric_logout(ha,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07002325 fcport->loop_id,
2326 fcport->d_id.b.domain,
2327 fcport->d_id.b.area,
2328 fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 fcport->loop_id = FC_NO_LOOP_ID;
2330 }
2331 }
2332 }
2333
2334 /* Starting free loop ID. */
2335 next_loopid = ha->min_external_loopid;
2336
2337 /*
2338 * Scan through our port list and login entries that need to be
2339 * logged in.
2340 */
2341 list_for_each_entry(fcport, &ha->fcports, list) {
2342 if (atomic_read(&ha->loop_down_timer) ||
2343 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2344 break;
2345
2346 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2347 (fcport->flags & FCF_LOGIN_NEEDED) == 0)
2348 continue;
2349
2350 if (fcport->loop_id == FC_NO_LOOP_ID) {
2351 fcport->loop_id = next_loopid;
2352 rval = qla2x00_find_new_loop_id(ha, fcport);
2353 if (rval != QLA_SUCCESS) {
2354 /* Ran out of IDs to use */
2355 break;
2356 }
2357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 /* Login and update database */
2359 qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
2360 }
2361
2362 /* Exit if out of loop IDs. */
2363 if (rval != QLA_SUCCESS) {
2364 break;
2365 }
2366
2367 /*
2368 * Login and add the new devices to our port list.
2369 */
2370 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2371 if (atomic_read(&ha->loop_down_timer) ||
2372 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2373 break;
2374
2375 /* Find a new loop ID to use. */
2376 fcport->loop_id = next_loopid;
2377 rval = qla2x00_find_new_loop_id(ha, fcport);
2378 if (rval != QLA_SUCCESS) {
2379 /* Ran out of IDs to use */
2380 break;
2381 }
2382
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 /* Remove device from the new list and add it to DB */
Akinobu Mita179e0912006-06-26 00:24:41 -07002384 list_move_tail(&fcport->list, &ha->fcports);
bdf79622005-04-17 15:06:53 -05002385
2386 /* Login and update database */
2387 qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 }
2389 } while (0);
2390
2391 /* Free all new device structures not processed. */
2392 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2393 list_del(&fcport->list);
2394 kfree(fcport);
2395 }
2396
2397 if (rval) {
2398 DEBUG2(printk("scsi(%ld): Configure fabric error exit: "
2399 "rval=%d\n", ha->host_no, rval));
2400 }
2401
2402 return (rval);
2403}
2404
2405
2406/*
2407 * qla2x00_find_all_fabric_devs
2408 *
2409 * Input:
2410 * ha = adapter block pointer.
2411 * dev = database device entry pointer.
2412 *
2413 * Returns:
2414 * 0 = success.
2415 *
2416 * Context:
2417 * Kernel context.
2418 */
2419static int
2420qla2x00_find_all_fabric_devs(scsi_qla_host_t *ha, struct list_head *new_fcports)
2421{
2422 int rval;
2423 uint16_t loop_id;
2424 fc_port_t *fcport, *new_fcport, *fcptemp;
2425 int found;
2426
2427 sw_info_t *swl;
2428 int swl_idx;
2429 int first_dev, last_dev;
2430 port_id_t wrap, nxt_d_id;
2431
2432 rval = QLA_SUCCESS;
2433
2434 /* Try GID_PT to get device list, else GAN. */
2435 swl = kmalloc(sizeof(sw_info_t) * MAX_FIBRE_DEVICES, GFP_ATOMIC);
2436 if (swl == NULL) {
2437 /*EMPTY*/
2438 DEBUG2(printk("scsi(%ld): GID_PT allocations failed, fallback "
2439 "on GA_NXT\n", ha->host_no));
2440 } else {
2441 memset(swl, 0, sizeof(sw_info_t) * MAX_FIBRE_DEVICES);
2442 if (qla2x00_gid_pt(ha, swl) != QLA_SUCCESS) {
2443 kfree(swl);
2444 swl = NULL;
2445 } else if (qla2x00_gpn_id(ha, swl) != QLA_SUCCESS) {
2446 kfree(swl);
2447 swl = NULL;
2448 } else if (qla2x00_gnn_id(ha, swl) != QLA_SUCCESS) {
2449 kfree(swl);
2450 swl = NULL;
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002451 } else if (qla2x00_gfpn_id(ha, swl) == QLA_SUCCESS) {
2452 qla2x00_gpsc(ha, swl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 }
2454 }
2455 swl_idx = 0;
2456
2457 /* Allocate temporary fcport for any new fcports discovered. */
2458 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2459 if (new_fcport == NULL) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002460 kfree(swl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 return (QLA_MEMORY_ALLOC_FAILED);
2462 }
2463 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2464
2465 /* Set start port ID scan at adapter ID. */
2466 first_dev = 1;
2467 last_dev = 0;
2468
2469 /* Starting free loop ID. */
2470 loop_id = ha->min_external_loopid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 for (; loop_id <= ha->last_loop_id; loop_id++) {
Andrew Vasquez3d716442005-07-06 10:30:26 -07002472 if (qla2x00_is_reserved_id(ha, loop_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 continue;
2474
Andrew Vasquez23443b12005-12-06 10:57:06 -08002475 if (atomic_read(&ha->loop_down_timer) || LOOP_TRANSITION(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 break;
2477
2478 if (swl != NULL) {
2479 if (last_dev) {
2480 wrap.b24 = new_fcport->d_id.b24;
2481 } else {
2482 new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
2483 memcpy(new_fcport->node_name,
2484 swl[swl_idx].node_name, WWN_SIZE);
2485 memcpy(new_fcport->port_name,
2486 swl[swl_idx].port_name, WWN_SIZE);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002487 memcpy(new_fcport->fabric_port_name,
2488 swl[swl_idx].fabric_port_name, WWN_SIZE);
2489 new_fcport->fp_speed = swl[swl_idx].fp_speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
2491 if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
2492 last_dev = 1;
2493 }
2494 swl_idx++;
2495 }
2496 } else {
2497 /* Send GA_NXT to the switch */
2498 rval = qla2x00_ga_nxt(ha, new_fcport);
2499 if (rval != QLA_SUCCESS) {
2500 qla_printk(KERN_WARNING, ha,
2501 "SNS scan failed -- assuming zero-entry "
2502 "result...\n");
2503 list_for_each_entry_safe(fcport, fcptemp,
2504 new_fcports, list) {
2505 list_del(&fcport->list);
2506 kfree(fcport);
2507 }
2508 rval = QLA_SUCCESS;
2509 break;
2510 }
2511 }
2512
2513 /* If wrap on switch device list, exit. */
2514 if (first_dev) {
2515 wrap.b24 = new_fcport->d_id.b24;
2516 first_dev = 0;
2517 } else if (new_fcport->d_id.b24 == wrap.b24) {
2518 DEBUG2(printk("scsi(%ld): device wrap (%02x%02x%02x)\n",
2519 ha->host_no, new_fcport->d_id.b.domain,
2520 new_fcport->d_id.b.area, new_fcport->d_id.b.al_pa));
2521 break;
2522 }
2523
2524 /* Bypass if host adapter. */
2525 if (new_fcport->d_id.b24 == ha->d_id.b24)
2526 continue;
2527
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07002528 /* Bypass if same domain and area of adapter. */
2529 if (((new_fcport->d_id.b24 & 0xffff00) ==
2530 (ha->d_id.b24 & 0xffff00)) && ha->current_topology ==
2531 ISP_CFG_FL)
2532 continue;
2533
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 /* Bypass reserved domain fields. */
2535 if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
2536 continue;
2537
2538 /* Locate matching device in database. */
2539 found = 0;
2540 list_for_each_entry(fcport, &ha->fcports, list) {
2541 if (memcmp(new_fcport->port_name, fcport->port_name,
2542 WWN_SIZE))
2543 continue;
2544
2545 found++;
2546
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002547 /* Update port state. */
2548 memcpy(fcport->fabric_port_name,
2549 new_fcport->fabric_port_name, WWN_SIZE);
2550 fcport->fp_speed = new_fcport->fp_speed;
2551
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 /*
2553 * If address the same and state FCS_ONLINE, nothing
2554 * changed.
2555 */
2556 if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
2557 atomic_read(&fcport->state) == FCS_ONLINE) {
2558 break;
2559 }
2560
2561 /*
2562 * If device was not a fabric device before.
2563 */
2564 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
2565 fcport->d_id.b24 = new_fcport->d_id.b24;
2566 fcport->loop_id = FC_NO_LOOP_ID;
2567 fcport->flags |= (FCF_FABRIC_DEVICE |
2568 FCF_LOGIN_NEEDED);
2569 fcport->flags &= ~FCF_PERSISTENT_BOUND;
2570 break;
2571 }
2572
2573 /*
2574 * Port ID changed or device was marked to be updated;
2575 * Log it out if still logged in and mark it for
2576 * relogin later.
2577 */
2578 fcport->d_id.b24 = new_fcport->d_id.b24;
2579 fcport->flags |= FCF_LOGIN_NEEDED;
2580 if (fcport->loop_id != FC_NO_LOOP_ID &&
2581 (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2582 fcport->port_type != FCT_INITIATOR &&
2583 fcport->port_type != FCT_BROADCAST) {
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07002584 ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2585 fcport->d_id.b.domain, fcport->d_id.b.area,
2586 fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 fcport->loop_id = FC_NO_LOOP_ID;
2588 }
2589
2590 break;
2591 }
2592
2593 if (found)
2594 continue;
2595
2596 /* If device was not in our fcports list, then add it. */
2597 list_add_tail(&new_fcport->list, new_fcports);
2598
2599 /* Allocate a new replacement fcport. */
2600 nxt_d_id.b24 = new_fcport->d_id.b24;
2601 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2602 if (new_fcport == NULL) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002603 kfree(swl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 return (QLA_MEMORY_ALLOC_FAILED);
2605 }
2606 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2607 new_fcport->d_id.b24 = nxt_d_id.b24;
2608 }
2609
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002610 kfree(swl);
2611 kfree(new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612
2613 if (!list_empty(new_fcports))
2614 ha->device_flags |= DFLG_FABRIC_DEVICES;
2615
2616 return (rval);
2617}
2618
2619/*
2620 * qla2x00_find_new_loop_id
2621 * Scan through our port list and find a new usable loop ID.
2622 *
2623 * Input:
2624 * ha: adapter state pointer.
2625 * dev: port structure pointer.
2626 *
2627 * Returns:
2628 * qla2x00 local function return status code.
2629 *
2630 * Context:
2631 * Kernel context.
2632 */
Adrian Bunk413975a2006-06-30 02:33:06 -07002633static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634qla2x00_find_new_loop_id(scsi_qla_host_t *ha, fc_port_t *dev)
2635{
2636 int rval;
2637 int found;
2638 fc_port_t *fcport;
2639 uint16_t first_loop_id;
2640
2641 rval = QLA_SUCCESS;
2642
2643 /* Save starting loop ID. */
2644 first_loop_id = dev->loop_id;
2645
2646 for (;;) {
2647 /* Skip loop ID if already used by adapter. */
2648 if (dev->loop_id == ha->loop_id) {
2649 dev->loop_id++;
2650 }
2651
2652 /* Skip reserved loop IDs. */
Andrew Vasquez3d716442005-07-06 10:30:26 -07002653 while (qla2x00_is_reserved_id(ha, dev->loop_id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 dev->loop_id++;
2655 }
2656
2657 /* Reset loop ID if passed the end. */
2658 if (dev->loop_id > ha->last_loop_id) {
2659 /* first loop ID. */
2660 dev->loop_id = ha->min_external_loopid;
2661 }
2662
2663 /* Check for loop ID being already in use. */
2664 found = 0;
2665 fcport = NULL;
2666 list_for_each_entry(fcport, &ha->fcports, list) {
2667 if (fcport->loop_id == dev->loop_id && fcport != dev) {
2668 /* ID possibly in use */
2669 found++;
2670 break;
2671 }
2672 }
2673
2674 /* If not in use then it is free to use. */
2675 if (!found) {
2676 break;
2677 }
2678
2679 /* ID in use. Try next value. */
2680 dev->loop_id++;
2681
2682 /* If wrap around. No free ID to use. */
2683 if (dev->loop_id == first_loop_id) {
2684 dev->loop_id = FC_NO_LOOP_ID;
2685 rval = QLA_FUNCTION_FAILED;
2686 break;
2687 }
2688 }
2689
2690 return (rval);
2691}
2692
2693/*
2694 * qla2x00_device_resync
2695 * Marks devices in the database that needs resynchronization.
2696 *
2697 * Input:
2698 * ha = adapter block pointer.
2699 *
2700 * Context:
2701 * Kernel context.
2702 */
2703static int
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002704qla2x00_device_resync(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705{
2706 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 uint32_t mask;
2708 fc_port_t *fcport;
2709 uint32_t rscn_entry;
2710 uint8_t rscn_out_iter;
2711 uint8_t format;
2712 port_id_t d_id;
2713
2714 rval = QLA_RSCNS_HANDLED;
2715
2716 while (ha->rscn_out_ptr != ha->rscn_in_ptr ||
2717 ha->flags.rscn_queue_overflow) {
2718
2719 rscn_entry = ha->rscn_queue[ha->rscn_out_ptr];
2720 format = MSB(MSW(rscn_entry));
2721 d_id.b.domain = LSB(MSW(rscn_entry));
2722 d_id.b.area = MSB(LSW(rscn_entry));
2723 d_id.b.al_pa = LSB(LSW(rscn_entry));
2724
2725 DEBUG(printk("scsi(%ld): RSCN queue entry[%d] = "
2726 "[%02x/%02x%02x%02x].\n",
2727 ha->host_no, ha->rscn_out_ptr, format, d_id.b.domain,
2728 d_id.b.area, d_id.b.al_pa));
2729
2730 ha->rscn_out_ptr++;
2731 if (ha->rscn_out_ptr == MAX_RSCN_COUNT)
2732 ha->rscn_out_ptr = 0;
2733
2734 /* Skip duplicate entries. */
2735 for (rscn_out_iter = ha->rscn_out_ptr;
2736 !ha->flags.rscn_queue_overflow &&
2737 rscn_out_iter != ha->rscn_in_ptr;
2738 rscn_out_iter = (rscn_out_iter ==
2739 (MAX_RSCN_COUNT - 1)) ? 0: rscn_out_iter + 1) {
2740
2741 if (rscn_entry != ha->rscn_queue[rscn_out_iter])
2742 break;
2743
2744 DEBUG(printk("scsi(%ld): Skipping duplicate RSCN queue "
2745 "entry found at [%d].\n", ha->host_no,
2746 rscn_out_iter));
2747
2748 ha->rscn_out_ptr = rscn_out_iter;
2749 }
2750
2751 /* Queue overflow, set switch default case. */
2752 if (ha->flags.rscn_queue_overflow) {
2753 DEBUG(printk("scsi(%ld): device_resync: rscn "
2754 "overflow.\n", ha->host_no));
2755
2756 format = 3;
2757 ha->flags.rscn_queue_overflow = 0;
2758 }
2759
2760 switch (format) {
2761 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 mask = 0xffffff;
2763 break;
2764 case 1:
2765 mask = 0xffff00;
2766 break;
2767 case 2:
2768 mask = 0xff0000;
2769 break;
2770 default:
2771 mask = 0x0;
2772 d_id.b24 = 0;
2773 ha->rscn_out_ptr = ha->rscn_in_ptr;
2774 break;
2775 }
2776
2777 rval = QLA_SUCCESS;
2778
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 list_for_each_entry(fcport, &ha->fcports, list) {
2780 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2781 (fcport->d_id.b24 & mask) != d_id.b24 ||
2782 fcport->port_type == FCT_BROADCAST)
2783 continue;
2784
2785 if (atomic_read(&fcport->state) == FCS_ONLINE) {
2786 if (format != 3 ||
2787 fcport->port_type != FCT_INITIATOR) {
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002788 qla2x00_mark_device_lost(ha, fcport,
2789 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 }
2791 }
2792 fcport->flags &= ~FCF_FARP_DONE;
2793 }
2794 }
2795 return (rval);
2796}
2797
2798/*
2799 * qla2x00_fabric_dev_login
2800 * Login fabric target device and update FC port database.
2801 *
2802 * Input:
2803 * ha: adapter state pointer.
2804 * fcport: port structure list pointer.
2805 * next_loopid: contains value of a new loop ID that can be used
2806 * by the next login attempt.
2807 *
2808 * Returns:
2809 * qla2x00 local function return status code.
2810 *
2811 * Context:
2812 * Kernel context.
2813 */
2814static int
2815qla2x00_fabric_dev_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2816 uint16_t *next_loopid)
2817{
2818 int rval;
2819 int retry;
Andrew Vasquez01071092005-07-06 10:31:37 -07002820 uint8_t opts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821
2822 rval = QLA_SUCCESS;
2823 retry = 0;
2824
2825 rval = qla2x00_fabric_login(ha, fcport, next_loopid);
2826 if (rval == QLA_SUCCESS) {
Andrew Vasquez01071092005-07-06 10:31:37 -07002827 /* Send an ADISC to tape devices.*/
2828 opts = 0;
2829 if (fcport->flags & FCF_TAPE_PRESENT)
2830 opts |= BIT_1;
2831 rval = qla2x00_get_port_database(ha, fcport, opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 if (rval != QLA_SUCCESS) {
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07002833 ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2834 fcport->d_id.b.domain, fcport->d_id.b.area,
2835 fcport->d_id.b.al_pa);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002836 qla2x00_mark_device_lost(ha, fcport, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 } else {
2838 qla2x00_update_fcport(ha, fcport);
2839 }
2840 }
2841
2842 return (rval);
2843}
2844
2845/*
2846 * qla2x00_fabric_login
2847 * Issue fabric login command.
2848 *
2849 * Input:
2850 * ha = adapter block pointer.
2851 * device = pointer to FC device type structure.
2852 *
2853 * Returns:
2854 * 0 - Login successfully
2855 * 1 - Login failed
2856 * 2 - Initiator device
2857 * 3 - Fatal error
2858 */
2859int
2860qla2x00_fabric_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2861 uint16_t *next_loopid)
2862{
2863 int rval;
2864 int retry;
2865 uint16_t tmp_loopid;
2866 uint16_t mb[MAILBOX_REGISTER_COUNT];
2867
2868 retry = 0;
2869 tmp_loopid = 0;
2870
2871 for (;;) {
2872 DEBUG(printk("scsi(%ld): Trying Fabric Login w/loop id 0x%04x "
2873 "for port %02x%02x%02x.\n",
2874 ha->host_no, fcport->loop_id, fcport->d_id.b.domain,
2875 fcport->d_id.b.area, fcport->d_id.b.al_pa));
2876
2877 /* Login fcport on switch. */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002878 ha->isp_ops.fabric_login(ha, fcport->loop_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 fcport->d_id.b.domain, fcport->d_id.b.area,
2880 fcport->d_id.b.al_pa, mb, BIT_0);
2881 if (mb[0] == MBS_PORT_ID_USED) {
2882 /*
2883 * Device has another loop ID. The firmware team
Andrew Vasquez01071092005-07-06 10:31:37 -07002884 * recommends the driver perform an implicit login with
2885 * the specified ID again. The ID we just used is save
2886 * here so we return with an ID that can be tried by
2887 * the next login.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 */
2889 retry++;
2890 tmp_loopid = fcport->loop_id;
2891 fcport->loop_id = mb[1];
2892
2893 DEBUG(printk("Fabric Login: port in use - next "
2894 "loop id=0x%04x, port Id=%02x%02x%02x.\n",
2895 fcport->loop_id, fcport->d_id.b.domain,
2896 fcport->d_id.b.area, fcport->d_id.b.al_pa));
2897
2898 } else if (mb[0] == MBS_COMMAND_COMPLETE) {
2899 /*
2900 * Login succeeded.
2901 */
2902 if (retry) {
2903 /* A retry occurred before. */
2904 *next_loopid = tmp_loopid;
2905 } else {
2906 /*
2907 * No retry occurred before. Just increment the
2908 * ID value for next login.
2909 */
2910 *next_loopid = (fcport->loop_id + 1);
2911 }
2912
2913 if (mb[1] & BIT_0) {
2914 fcport->port_type = FCT_INITIATOR;
2915 } else {
2916 fcport->port_type = FCT_TARGET;
2917 if (mb[1] & BIT_1) {
2918 fcport->flags |= FCF_TAPE_PRESENT;
2919 }
2920 }
2921
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07002922 if (mb[10] & BIT_0)
2923 fcport->supported_classes |= FC_COS_CLASS2;
2924 if (mb[10] & BIT_1)
2925 fcport->supported_classes |= FC_COS_CLASS3;
2926
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 rval = QLA_SUCCESS;
2928 break;
2929 } else if (mb[0] == MBS_LOOP_ID_USED) {
2930 /*
2931 * Loop ID already used, try next loop ID.
2932 */
2933 fcport->loop_id++;
2934 rval = qla2x00_find_new_loop_id(ha, fcport);
2935 if (rval != QLA_SUCCESS) {
2936 /* Ran out of loop IDs to use */
2937 break;
2938 }
2939 } else if (mb[0] == MBS_COMMAND_ERROR) {
2940 /*
2941 * Firmware possibly timed out during login. If NO
2942 * retries are left to do then the device is declared
2943 * dead.
2944 */
2945 *next_loopid = fcport->loop_id;
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07002946 ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2947 fcport->d_id.b.domain, fcport->d_id.b.area,
2948 fcport->d_id.b.al_pa);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002949 qla2x00_mark_device_lost(ha, fcport, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
2951 rval = 1;
2952 break;
2953 } else {
2954 /*
2955 * unrecoverable / not handled error
2956 */
2957 DEBUG2(printk("%s(%ld): failed=%x port_id=%02x%02x%02x "
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002958 "loop_id=%x jiffies=%lx.\n",
2959 __func__, ha->host_no, mb[0],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 fcport->d_id.b.domain, fcport->d_id.b.area,
2961 fcport->d_id.b.al_pa, fcport->loop_id, jiffies));
2962
2963 *next_loopid = fcport->loop_id;
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07002964 ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2965 fcport->d_id.b.domain, fcport->d_id.b.area,
2966 fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 fcport->loop_id = FC_NO_LOOP_ID;
Andrew Vasquez0eedfcf2005-10-27 11:09:38 -07002968 fcport->login_retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969
2970 rval = 3;
2971 break;
2972 }
2973 }
2974
2975 return (rval);
2976}
2977
2978/*
2979 * qla2x00_local_device_login
2980 * Issue local device login command.
2981 *
2982 * Input:
2983 * ha = adapter block pointer.
2984 * loop_id = loop id of device to login to.
2985 *
2986 * Returns (Where's the #define!!!!):
2987 * 0 - Login successfully
2988 * 1 - Login failed
2989 * 3 - Fatal error
2990 */
2991int
andrew.vasquez@qlogic.com9a52a572006-03-09 14:27:44 -08002992qla2x00_local_device_login(scsi_qla_host_t *ha, fc_port_t *fcport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993{
2994 int rval;
2995 uint16_t mb[MAILBOX_REGISTER_COUNT];
2996
2997 memset(mb, 0, sizeof(mb));
andrew.vasquez@qlogic.com9a52a572006-03-09 14:27:44 -08002998 rval = qla2x00_login_local_device(ha, fcport, mb, BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 if (rval == QLA_SUCCESS) {
3000 /* Interrogate mailbox registers for any errors */
3001 if (mb[0] == MBS_COMMAND_ERROR)
3002 rval = 1;
3003 else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
3004 /* device not in PCB table */
3005 rval = 3;
3006 }
3007
3008 return (rval);
3009}
3010
3011/*
3012 * qla2x00_loop_resync
3013 * Resync with fibre channel devices.
3014 *
3015 * Input:
3016 * ha = adapter block pointer.
3017 *
3018 * Returns:
3019 * 0 = success
3020 */
3021int
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003022qla2x00_loop_resync(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023{
3024 int rval;
3025 uint32_t wait_time;
3026
3027 rval = QLA_SUCCESS;
3028
3029 atomic_set(&ha->loop_state, LOOP_UPDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3031 if (ha->flags.online) {
3032 if (!(rval = qla2x00_fw_ready(ha))) {
3033 /* Wait at most MAX_TARGET RSCNs for a stable link. */
3034 wait_time = 256;
3035 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 atomic_set(&ha->loop_state, LOOP_UPDATE);
3037
Andrew Vasquez01071092005-07-06 10:31:37 -07003038 /* Issue a marker after FW becomes ready. */
3039 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
3040 ha->marker_needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041
3042 /* Remap devices on Loop. */
3043 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
3044
3045 qla2x00_configure_loop(ha);
3046 wait_time--;
3047 } while (!atomic_read(&ha->loop_down_timer) &&
3048 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
3049 wait_time &&
3050 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
3051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 }
3053
3054 if (test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
3055 return (QLA_FUNCTION_FAILED);
3056 }
3057
3058 if (rval) {
3059 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
3060 }
3061
3062 return (rval);
3063}
3064
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065void
3066qla2x00_rescan_fcports(scsi_qla_host_t *ha)
3067{
3068 int rescan_done;
3069 fc_port_t *fcport;
3070
3071 rescan_done = 0;
3072 list_for_each_entry(fcport, &ha->fcports, list) {
3073 if ((fcport->flags & FCF_RESCAN_NEEDED) == 0)
3074 continue;
3075
3076 qla2x00_update_fcport(ha, fcport);
3077 fcport->flags &= ~FCF_RESCAN_NEEDED;
3078
3079 rescan_done = 1;
3080 }
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003081 qla2x00_probe_for_all_luns(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082}
3083
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08003084void
3085qla2x00_update_fcports(scsi_qla_host_t *ha)
3086{
3087 fc_port_t *fcport;
3088
3089 /* Go with deferred removal of rport references. */
3090 list_for_each_entry(fcport, &ha->fcports, list)
3091 if (fcport->drport)
3092 qla2x00_rport_del(fcport);
3093}
3094
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095/*
3096* qla2x00_abort_isp
3097* Resets ISP and aborts all outstanding commands.
3098*
3099* Input:
3100* ha = adapter block pointer.
3101*
3102* Returns:
3103* 0 = success
3104*/
3105int
3106qla2x00_abort_isp(scsi_qla_host_t *ha)
3107{
Andrew Vasquez476e8972006-08-23 14:54:55 -07003108 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 unsigned long flags = 0;
3110 uint16_t cnt;
3111 srb_t *sp;
3112 uint8_t status = 0;
3113
3114 if (ha->flags.online) {
3115 ha->flags.online = 0;
3116 clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
3118 qla_printk(KERN_INFO, ha,
3119 "Performing ISP error recovery - ha= %p.\n", ha);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003120 ha->isp_ops.reset_chip(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121
3122 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
3123 if (atomic_read(&ha->loop_state) != LOOP_DOWN) {
3124 atomic_set(&ha->loop_state, LOOP_DOWN);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08003125 qla2x00_mark_all_devices_lost(ha, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 } else {
3127 if (!atomic_read(&ha->loop_down_timer))
3128 atomic_set(&ha->loop_down_timer,
3129 LOOP_DOWN_TIME);
3130 }
3131
3132 spin_lock_irqsave(&ha->hardware_lock, flags);
3133 /* Requeue all commands in outstanding command list. */
3134 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
3135 sp = ha->outstanding_cmds[cnt];
3136 if (sp) {
3137 ha->outstanding_cmds[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 sp->flags = 0;
f4f051e2005-04-17 15:02:26 -05003139 sp->cmd->result = DID_RESET << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 sp->cmd->host_scribble = (unsigned char *)NULL;
f4f051e2005-04-17 15:02:26 -05003141 qla2x00_sp_compl(ha, sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 }
3143 }
3144 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3145
Andrew Vasquez30c47662007-01-29 10:22:21 -08003146 ha->isp_ops.get_flash_version(ha, ha->request_ring);
3147
David Miller4e08df32007-04-16 12:37:43 -07003148 ha->isp_ops.nvram_config(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149
3150 if (!qla2x00_restart_isp(ha)) {
3151 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3152
3153 if (!atomic_read(&ha->loop_down_timer)) {
3154 /*
3155 * Issue marker command only when we are going
3156 * to start the I/O .
3157 */
3158 ha->marker_needed = 1;
3159 }
3160
3161 ha->flags.online = 1;
3162
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003163 ha->isp_ops.enable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003165 ha->isp_abort_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
Andrew Vasquez476e8972006-08-23 14:54:55 -07003167
3168 if (ha->eft) {
3169 rval = qla2x00_trace_control(ha, TC_ENABLE,
3170 ha->eft_dma, EFT_NUM_BUFFERS);
3171 if (rval) {
3172 qla_printk(KERN_WARNING, ha,
3173 "Unable to reinitialize EFT "
3174 "(%d).\n", rval);
3175 }
3176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 } else { /* failed the ISP abort */
3178 ha->flags.online = 1;
3179 if (test_bit(ISP_ABORT_RETRY, &ha->dpc_flags)) {
3180 if (ha->isp_abort_cnt == 0) {
3181 qla_printk(KERN_WARNING, ha,
3182 "ISP error recovery failed - "
3183 "board disabled\n");
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003184 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 * The next call disables the board
3186 * completely.
3187 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003188 ha->isp_ops.reset_adapter(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 ha->flags.online = 0;
3190 clear_bit(ISP_ABORT_RETRY,
3191 &ha->dpc_flags);
3192 status = 0;
3193 } else { /* schedule another ISP abort */
3194 ha->isp_abort_cnt--;
3195 DEBUG(printk("qla%ld: ISP abort - "
Andrew Vasquez01071092005-07-06 10:31:37 -07003196 "retry remaining %d\n",
Andrew Vasquez744f11fd2006-06-23 16:11:05 -07003197 ha->host_no, ha->isp_abort_cnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 status = 1;
3199 }
3200 } else {
3201 ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
3202 DEBUG(printk("qla2x00(%ld): ISP error recovery "
3203 "- retrying (%d) more times\n",
Andrew Vasquez744f11fd2006-06-23 16:11:05 -07003204 ha->host_no, ha->isp_abort_cnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 set_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3206 status = 1;
3207 }
3208 }
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003209
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 }
3211
3212 if (status) {
3213 qla_printk(KERN_INFO, ha,
3214 "qla2x00_abort_isp: **** FAILED ****\n");
3215 } else {
3216 DEBUG(printk(KERN_INFO
3217 "qla2x00_abort_isp(%ld): exiting.\n",
Andrew Vasquez744f11fd2006-06-23 16:11:05 -07003218 ha->host_no));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 }
3220
3221 return(status);
3222}
3223
3224/*
3225* qla2x00_restart_isp
3226* restarts the ISP after a reset
3227*
3228* Input:
3229* ha = adapter block pointer.
3230*
3231* Returns:
3232* 0 = success
3233*/
3234static int
3235qla2x00_restart_isp(scsi_qla_host_t *ha)
3236{
3237 uint8_t status = 0;
Andrew Vasquez3d716442005-07-06 10:30:26 -07003238 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 unsigned long flags = 0;
3240 uint32_t wait_time;
3241
3242 /* If firmware needs to be loaded */
3243 if (qla2x00_isp_firmware(ha)) {
3244 ha->flags.online = 0;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003245 if (!(status = ha->isp_ops.chip_diag(ha))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
3247 status = qla2x00_setup_chip(ha);
3248 goto done;
3249 }
3250
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 spin_lock_irqsave(&ha->hardware_lock, flags);
3252
andrew.vasquez@qlogic.com044cc6c2006-03-09 14:27:13 -08003253 if (!IS_QLA24XX(ha) && !IS_QLA54XX(ha)) {
Andrew Vasquez01071092005-07-06 10:31:37 -07003254 /*
3255 * Disable SRAM, Instruction RAM and GP RAM
3256 * parity.
3257 */
3258 WRT_REG_WORD(&reg->hccr,
3259 (HCCR_ENABLE_PARITY + 0x0));
3260 RD_REG_WORD(&reg->hccr);
3261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262
3263 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003264
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 status = qla2x00_setup_chip(ha);
3266
3267 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003268
andrew.vasquez@qlogic.com044cc6c2006-03-09 14:27:13 -08003269 if (!IS_QLA24XX(ha) && !IS_QLA54XX(ha)) {
Andrew Vasquez01071092005-07-06 10:31:37 -07003270 /* Enable proper parity */
3271 if (IS_QLA2300(ha))
3272 /* SRAM parity */
3273 WRT_REG_WORD(&reg->hccr,
3274 (HCCR_ENABLE_PARITY + 0x1));
3275 else
3276 /*
3277 * SRAM, Instruction RAM and GP RAM
3278 * parity.
3279 */
3280 WRT_REG_WORD(&reg->hccr,
3281 (HCCR_ENABLE_PARITY + 0x7));
3282 RD_REG_WORD(&reg->hccr);
3283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284
3285 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3286 }
3287 }
3288
3289 done:
3290 if (!status && !(status = qla2x00_init_rings(ha))) {
3291 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3292 if (!(status = qla2x00_fw_ready(ha))) {
3293 DEBUG(printk("%s(): Start configure loop, "
Andrew Vasquez744f11fd2006-06-23 16:11:05 -07003294 "status = %d\n", __func__, status));
Andrew Vasquez01071092005-07-06 10:31:37 -07003295
3296 /* Issue a marker after FW becomes ready. */
3297 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
3298
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 ha->flags.online = 1;
3300 /* Wait at most MAX_TARGET RSCNs for a stable link. */
3301 wait_time = 256;
3302 do {
3303 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
3304 qla2x00_configure_loop(ha);
3305 wait_time--;
3306 } while (!atomic_read(&ha->loop_down_timer) &&
3307 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
3308 wait_time &&
3309 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
3310 }
3311
3312 /* if no cable then assume it's good */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003313 if ((ha->device_flags & DFLG_NO_CABLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 status = 0;
3315
3316 DEBUG(printk("%s(): Configure loop done, status = 0x%x\n",
3317 __func__,
Andrew Vasquez744f11fd2006-06-23 16:11:05 -07003318 status));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 }
3320 return (status);
3321}
3322
3323/*
3324* qla2x00_reset_adapter
3325* Reset adapter.
3326*
3327* Input:
3328* ha = adapter block pointer.
3329*/
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003330void
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331qla2x00_reset_adapter(scsi_qla_host_t *ha)
3332{
3333 unsigned long flags = 0;
Andrew Vasquez3d716442005-07-06 10:30:26 -07003334 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335
3336 ha->flags.online = 0;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003337 ha->isp_ops.disable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 spin_lock_irqsave(&ha->hardware_lock, flags);
3340 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
3341 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
3342 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
3343 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
3344 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3345}
Andrew Vasquez01071092005-07-06 10:31:37 -07003346
3347void
3348qla24xx_reset_adapter(scsi_qla_host_t *ha)
3349{
3350 unsigned long flags = 0;
3351 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
3352
3353 ha->flags.online = 0;
3354 ha->isp_ops.disable_intrs(ha);
3355
3356 spin_lock_irqsave(&ha->hardware_lock, flags);
3357 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
3358 RD_REG_DWORD(&reg->hccr);
3359 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
3360 RD_REG_DWORD(&reg->hccr);
3361 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3362}
3363
David Miller4e08df32007-04-16 12:37:43 -07003364/* On sparc systems, obtain port and node WWN from firmware
3365 * properties.
3366 */
3367static void qla24xx_nvram_wwn_from_ofw(scsi_qla_host_t *ha, struct nvram_24xx *nv)
3368{
3369#ifdef CONFIG_SPARC
3370 struct pci_dev *pdev = ha->pdev;
David S. Miller15576bc2007-05-08 00:36:49 -07003371 struct device_node *dp = pci_device_to_OF_node(pdev);
3372 const u8 *val;
David Miller4e08df32007-04-16 12:37:43 -07003373 int len;
3374
3375 val = of_get_property(dp, "port-wwn", &len);
3376 if (val && len >= WWN_SIZE)
3377 memcpy(nv->port_name, val, WWN_SIZE);
3378
3379 val = of_get_property(dp, "node-wwn", &len);
3380 if (val && len >= WWN_SIZE)
3381 memcpy(nv->node_name, val, WWN_SIZE);
3382#endif
3383}
3384
Andrew Vasquez01071092005-07-06 10:31:37 -07003385int
3386qla24xx_nvram_config(scsi_qla_host_t *ha)
3387{
David Miller4e08df32007-04-16 12:37:43 -07003388 int rval;
Andrew Vasquez01071092005-07-06 10:31:37 -07003389 struct init_cb_24xx *icb;
3390 struct nvram_24xx *nv;
3391 uint32_t *dptr;
3392 uint8_t *dptr1, *dptr2;
3393 uint32_t chksum;
3394 uint16_t cnt;
3395
David Miller4e08df32007-04-16 12:37:43 -07003396 rval = QLA_SUCCESS;
Andrew Vasquez01071092005-07-06 10:31:37 -07003397 icb = (struct init_cb_24xx *)ha->init_cb;
3398 nv = (struct nvram_24xx *)ha->request_ring;
3399
3400 /* Determine NVRAM starting address. */
3401 ha->nvram_size = sizeof(struct nvram_24xx);
3402 ha->nvram_base = FA_NVRAM_FUNC0_ADDR;
andrew.vasquez@qlogic.com6f641792006-03-09 14:27:34 -08003403 ha->vpd_size = FA_NVRAM_VPD_SIZE;
3404 ha->vpd_base = FA_NVRAM_VPD0_ADDR;
3405 if (PCI_FUNC(ha->pdev->devfn)) {
Andrew Vasquez01071092005-07-06 10:31:37 -07003406 ha->nvram_base = FA_NVRAM_FUNC1_ADDR;
andrew.vasquez@qlogic.com6f641792006-03-09 14:27:34 -08003407 ha->vpd_base = FA_NVRAM_VPD1_ADDR;
3408 }
Andrew Vasquez01071092005-07-06 10:31:37 -07003409
3410 /* Get NVRAM data and calculate checksum. */
3411 dptr = (uint32_t *)nv;
3412 ha->isp_ops.read_nvram(ha, (uint8_t *)dptr, ha->nvram_base,
3413 ha->nvram_size);
3414 for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++)
3415 chksum += le32_to_cpu(*dptr++);
3416
3417 DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
3418 DEBUG5(qla2x00_dump_buffer((uint8_t *)ha->request_ring,
3419 ha->nvram_size));
3420
3421 /* Bad NVRAM data, set defaults parameters. */
3422 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
3423 || nv->id[3] != ' ' ||
3424 nv->nvram_version < __constant_cpu_to_le16(ICB_VERSION)) {
3425 /* Reset NVRAM data. */
3426 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
3427 "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
3428 le16_to_cpu(nv->nvram_version));
David Miller4e08df32007-04-16 12:37:43 -07003429 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
3430 "invalid -- WWPN) defaults.\n");
3431
3432 /*
3433 * Set default initialization control block.
3434 */
3435 memset(nv, 0, ha->nvram_size);
3436 nv->nvram_version = __constant_cpu_to_le16(ICB_VERSION);
3437 nv->version = __constant_cpu_to_le16(ICB_VERSION);
3438 nv->frame_payload_size = __constant_cpu_to_le16(2048);
3439 nv->execution_throttle = __constant_cpu_to_le16(0xFFFF);
3440 nv->exchange_count = __constant_cpu_to_le16(0);
3441 nv->hard_address = __constant_cpu_to_le16(124);
3442 nv->port_name[0] = 0x21;
3443 nv->port_name[1] = 0x00 + PCI_FUNC(ha->pdev->devfn);
3444 nv->port_name[2] = 0x00;
3445 nv->port_name[3] = 0xe0;
3446 nv->port_name[4] = 0x8b;
3447 nv->port_name[5] = 0x1c;
3448 nv->port_name[6] = 0x55;
3449 nv->port_name[7] = 0x86;
3450 nv->node_name[0] = 0x20;
3451 nv->node_name[1] = 0x00;
3452 nv->node_name[2] = 0x00;
3453 nv->node_name[3] = 0xe0;
3454 nv->node_name[4] = 0x8b;
3455 nv->node_name[5] = 0x1c;
3456 nv->node_name[6] = 0x55;
3457 nv->node_name[7] = 0x86;
3458 qla24xx_nvram_wwn_from_ofw(ha, nv);
3459 nv->login_retry_count = __constant_cpu_to_le16(8);
3460 nv->interrupt_delay_timer = __constant_cpu_to_le16(0);
3461 nv->login_timeout = __constant_cpu_to_le16(0);
3462 nv->firmware_options_1 =
3463 __constant_cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
3464 nv->firmware_options_2 = __constant_cpu_to_le32(2 << 4);
3465 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_12);
3466 nv->firmware_options_3 = __constant_cpu_to_le32(2 << 13);
3467 nv->host_p = __constant_cpu_to_le32(BIT_11|BIT_10);
3468 nv->efi_parameters = __constant_cpu_to_le32(0);
3469 nv->reset_delay = 5;
3470 nv->max_luns_per_target = __constant_cpu_to_le16(128);
3471 nv->port_down_retry_count = __constant_cpu_to_le16(30);
3472 nv->link_down_timeout = __constant_cpu_to_le16(30);
3473
3474 rval = 1;
Andrew Vasquez01071092005-07-06 10:31:37 -07003475 }
3476
3477 /* Reset Initialization control block */
3478 memset(icb, 0, sizeof(struct init_cb_24xx));
3479
3480 /* Copy 1st segment. */
3481 dptr1 = (uint8_t *)icb;
3482 dptr2 = (uint8_t *)&nv->version;
3483 cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
3484 while (cnt--)
3485 *dptr1++ = *dptr2++;
3486
3487 icb->login_retry_count = nv->login_retry_count;
Andrew Vasquez3ea66e22006-06-23 16:11:27 -07003488 icb->link_down_on_nos = nv->link_down_on_nos;
Andrew Vasquez01071092005-07-06 10:31:37 -07003489
3490 /* Copy 2nd segment. */
3491 dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
3492 dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
3493 cnt = (uint8_t *)&icb->reserved_3 -
3494 (uint8_t *)&icb->interrupt_delay_timer;
3495 while (cnt--)
3496 *dptr1++ = *dptr2++;
3497
3498 /*
3499 * Setup driver NVRAM options.
3500 */
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08003501 qla2x00_set_model_info(ha, nv->model_name, sizeof(nv->model_name),
3502 "QLA2462");
Andrew Vasquez01071092005-07-06 10:31:37 -07003503
Andrew Vasquez5341e862006-05-17 15:09:16 -07003504 /* Use alternate WWN? */
3505 if (nv->host_p & __constant_cpu_to_le32(BIT_15)) {
3506 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
3507 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
3508 }
3509
Andrew Vasquez01071092005-07-06 10:31:37 -07003510 /* Prepare nodename */
Andrew Vasquezfd0e7e42006-05-17 15:09:11 -07003511 if ((icb->firmware_options_1 & __constant_cpu_to_le32(BIT_14)) == 0) {
Andrew Vasquez01071092005-07-06 10:31:37 -07003512 /*
3513 * Firmware will apply the following mask if the nodename was
3514 * not provided.
3515 */
3516 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
3517 icb->node_name[0] &= 0xF0;
3518 }
3519
3520 /* Set host adapter parameters. */
3521 ha->flags.disable_risc_code_load = 0;
Andrew Vasquez0c8c39a2006-12-13 19:20:30 -08003522 ha->flags.enable_lip_reset = 0;
3523 ha->flags.enable_lip_full_login =
3524 le32_to_cpu(nv->host_p) & BIT_10 ? 1: 0;
3525 ha->flags.enable_target_reset =
3526 le32_to_cpu(nv->host_p) & BIT_11 ? 1: 0;
Andrew Vasquez01071092005-07-06 10:31:37 -07003527 ha->flags.enable_led_scheme = 0;
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07003528 ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
Andrew Vasquez01071092005-07-06 10:31:37 -07003529
Andrew Vasquezfd0e7e42006-05-17 15:09:11 -07003530 ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
3531 (BIT_6 | BIT_5 | BIT_4)) >> 4;
Andrew Vasquez01071092005-07-06 10:31:37 -07003532
3533 memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
3534 sizeof(ha->fw_seriallink_options24));
3535
3536 /* save HBA serial number */
3537 ha->serial0 = icb->port_name[5];
3538 ha->serial1 = icb->port_name[6];
3539 ha->serial2 = icb->port_name[7];
3540 ha->node_name = icb->node_name;
3541 ha->port_name = icb->port_name;
3542
andrew.vasquez@qlogic.combc8fb3c2006-01-13 17:05:42 -08003543 icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
3544
Andrew Vasquez01071092005-07-06 10:31:37 -07003545 ha->retry_count = le16_to_cpu(nv->login_retry_count);
3546
3547 /* Set minimum login_timeout to 4 seconds. */
3548 if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
3549 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
3550 if (le16_to_cpu(nv->login_timeout) < 4)
3551 nv->login_timeout = __constant_cpu_to_le16(4);
3552 ha->login_timeout = le16_to_cpu(nv->login_timeout);
3553 icb->login_timeout = cpu_to_le16(nv->login_timeout);
3554
3555 /* Set minimum RATOV to 200 tenths of a second. */
3556 ha->r_a_tov = 200;
3557
3558 ha->loop_reset_delay = nv->reset_delay;
3559
3560 /* Link Down Timeout = 0:
3561 *
3562 * When Port Down timer expires we will start returning
3563 * I/O's to OS with "DID_NO_CONNECT".
3564 *
3565 * Link Down Timeout != 0:
3566 *
3567 * The driver waits for the link to come up after link down
3568 * before returning I/Os to OS with "DID_NO_CONNECT".
3569 */
3570 if (le16_to_cpu(nv->link_down_timeout) == 0) {
3571 ha->loop_down_abort_time =
3572 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
3573 } else {
3574 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
3575 ha->loop_down_abort_time =
3576 (LOOP_DOWN_TIME - ha->link_down_timeout);
3577 }
3578
3579 /* Need enough time to try and get the port back. */
3580 ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
3581 if (qlport_down_retry)
3582 ha->port_down_retry_count = qlport_down_retry;
3583
3584 /* Set login_retry_count */
3585 ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
3586 if (ha->port_down_retry_count ==
3587 le16_to_cpu(nv->port_down_retry_count) &&
3588 ha->port_down_retry_count > 3)
3589 ha->login_retry_count = ha->port_down_retry_count;
3590 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
3591 ha->login_retry_count = ha->port_down_retry_count;
3592 if (ql2xloginretrycount)
3593 ha->login_retry_count = ql2xloginretrycount;
3594
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07003595 /* Enable ZIO. */
3596 if (!ha->flags.init_done) {
3597 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
3598 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
3599 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
3600 le16_to_cpu(icb->interrupt_delay_timer): 2;
3601 }
3602 icb->firmware_options_2 &= __constant_cpu_to_le32(
3603 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
3604 ha->flags.process_response_queue = 0;
3605 if (ha->zio_mode != QLA_ZIO_DISABLED) {
andrew.vasquez@qlogic.com4a59f712006-03-09 14:27:39 -08003606 ha->zio_mode = QLA_ZIO_MODE_6;
3607
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07003608 DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer delay "
3609 "(%d us).\n", ha->host_no, ha->zio_mode,
3610 ha->zio_timer * 100));
3611 qla_printk(KERN_INFO, ha,
3612 "ZIO mode %d enabled; timer delay (%d us).\n",
3613 ha->zio_mode, ha->zio_timer * 100);
3614
3615 icb->firmware_options_2 |= cpu_to_le32(
3616 (uint32_t)ha->zio_mode);
3617 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
3618 ha->flags.process_response_queue = 1;
3619 }
3620
David Miller4e08df32007-04-16 12:37:43 -07003621 if (rval) {
3622 DEBUG2_3(printk(KERN_WARNING
3623 "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
3624 }
3625 return (rval);
Andrew Vasquez01071092005-07-06 10:31:37 -07003626}
3627
Adrian Bunk413975a2006-06-30 02:33:06 -07003628static int
Andrew Vasquezd1c61902006-05-17 15:09:00 -07003629qla24xx_load_risc_flash(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3630{
3631 int rval;
3632 int segments, fragment;
3633 uint32_t faddr;
3634 uint32_t *dcode, dlen;
3635 uint32_t risc_addr;
3636 uint32_t risc_size;
3637 uint32_t i;
3638
3639 rval = QLA_SUCCESS;
3640
3641 segments = FA_RISC_CODE_SEGMENTS;
3642 faddr = FA_RISC_CODE_ADDR;
3643 dcode = (uint32_t *)ha->request_ring;
3644 *srisc_addr = 0;
3645
3646 /* Validate firmware image by checking version. */
3647 qla24xx_read_flash_data(ha, dcode, faddr + 4, 4);
3648 for (i = 0; i < 4; i++)
3649 dcode[i] = be32_to_cpu(dcode[i]);
3650 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
3651 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
3652 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
3653 dcode[3] == 0)) {
3654 qla_printk(KERN_WARNING, ha,
3655 "Unable to verify integrity of flash firmware image!\n");
3656 qla_printk(KERN_WARNING, ha,
3657 "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
3658 dcode[1], dcode[2], dcode[3]);
3659
3660 return QLA_FUNCTION_FAILED;
3661 }
3662
3663 while (segments && rval == QLA_SUCCESS) {
3664 /* Read segment's load information. */
3665 qla24xx_read_flash_data(ha, dcode, faddr, 4);
3666
3667 risc_addr = be32_to_cpu(dcode[2]);
3668 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
3669 risc_size = be32_to_cpu(dcode[3]);
3670
3671 fragment = 0;
3672 while (risc_size > 0 && rval == QLA_SUCCESS) {
3673 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
3674 if (dlen > risc_size)
3675 dlen = risc_size;
3676
3677 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3678 "addr %x, number of dwords 0x%x, offset 0x%x.\n",
3679 ha->host_no, risc_addr, dlen, faddr));
3680
3681 qla24xx_read_flash_data(ha, dcode, faddr, dlen);
3682 for (i = 0; i < dlen; i++)
3683 dcode[i] = swab32(dcode[i]);
3684
3685 rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3686 dlen);
3687 if (rval) {
3688 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3689 "segment %d of firmware\n", ha->host_no,
3690 fragment));
3691 qla_printk(KERN_WARNING, ha,
3692 "[ERROR] Failed to load segment %d of "
3693 "firmware\n", fragment);
3694 break;
3695 }
3696
3697 faddr += dlen;
3698 risc_addr += dlen;
3699 risc_size -= dlen;
3700 fragment++;
3701 }
3702
3703 /* Next segment. */
3704 segments--;
3705 }
3706
3707 return rval;
3708}
3709
Andrew Vasquezd1c61902006-05-17 15:09:00 -07003710#define QLA_FW_URL "ftp://ftp.qlogic.com/outgoing/linux/firmware/"
3711
Andrew Vasquez01071092005-07-06 10:31:37 -07003712int
Andrew Vasquez54333832005-11-09 15:49:04 -08003713qla2x00_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3714{
3715 int rval;
3716 int i, fragment;
3717 uint16_t *wcode, *fwcode;
3718 uint32_t risc_addr, risc_size, fwclen, wlen, *seg;
3719 struct fw_blob *blob;
3720
3721 /* Load firmware blob. */
3722 blob = qla2x00_request_firmware(ha);
3723 if (!blob) {
3724 qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
Andrew Vasquezd1c61902006-05-17 15:09:00 -07003725 qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
3726 "from: " QLA_FW_URL ".\n");
Andrew Vasquez54333832005-11-09 15:49:04 -08003727 return QLA_FUNCTION_FAILED;
3728 }
3729
3730 rval = QLA_SUCCESS;
3731
3732 wcode = (uint16_t *)ha->request_ring;
3733 *srisc_addr = 0;
3734 fwcode = (uint16_t *)blob->fw->data;
3735 fwclen = 0;
3736
3737 /* Validate firmware image by checking version. */
3738 if (blob->fw->size < 8 * sizeof(uint16_t)) {
3739 qla_printk(KERN_WARNING, ha,
3740 "Unable to verify integrity of firmware image (%Zd)!\n",
3741 blob->fw->size);
3742 goto fail_fw_integrity;
3743 }
3744 for (i = 0; i < 4; i++)
3745 wcode[i] = be16_to_cpu(fwcode[i + 4]);
3746 if ((wcode[0] == 0xffff && wcode[1] == 0xffff && wcode[2] == 0xffff &&
3747 wcode[3] == 0xffff) || (wcode[0] == 0 && wcode[1] == 0 &&
3748 wcode[2] == 0 && wcode[3] == 0)) {
3749 qla_printk(KERN_WARNING, ha,
3750 "Unable to verify integrity of firmware image!\n");
3751 qla_printk(KERN_WARNING, ha,
3752 "Firmware data: %04x %04x %04x %04x!\n", wcode[0],
3753 wcode[1], wcode[2], wcode[3]);
3754 goto fail_fw_integrity;
3755 }
3756
3757 seg = blob->segs;
3758 while (*seg && rval == QLA_SUCCESS) {
3759 risc_addr = *seg;
3760 *srisc_addr = *srisc_addr == 0 ? *seg : *srisc_addr;
3761 risc_size = be16_to_cpu(fwcode[3]);
3762
3763 /* Validate firmware image size. */
3764 fwclen += risc_size * sizeof(uint16_t);
3765 if (blob->fw->size < fwclen) {
3766 qla_printk(KERN_WARNING, ha,
3767 "Unable to verify integrity of firmware image "
3768 "(%Zd)!\n", blob->fw->size);
3769 goto fail_fw_integrity;
3770 }
3771
3772 fragment = 0;
3773 while (risc_size > 0 && rval == QLA_SUCCESS) {
3774 wlen = (uint16_t)(ha->fw_transfer_size >> 1);
3775 if (wlen > risc_size)
3776 wlen = risc_size;
3777
3778 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3779 "addr %x, number of words 0x%x.\n", ha->host_no,
3780 risc_addr, wlen));
3781
3782 for (i = 0; i < wlen; i++)
3783 wcode[i] = swab16(fwcode[i]);
3784
3785 rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3786 wlen);
3787 if (rval) {
3788 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3789 "segment %d of firmware\n", ha->host_no,
3790 fragment));
3791 qla_printk(KERN_WARNING, ha,
3792 "[ERROR] Failed to load segment %d of "
3793 "firmware\n", fragment);
3794 break;
3795 }
3796
3797 fwcode += wlen;
3798 risc_addr += wlen;
3799 risc_size -= wlen;
3800 fragment++;
3801 }
3802
3803 /* Next segment. */
3804 seg++;
3805 }
3806 return rval;
3807
3808fail_fw_integrity:
3809 return QLA_FUNCTION_FAILED;
3810}
3811
3812int
3813qla24xx_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
Andrew Vasquez01071092005-07-06 10:31:37 -07003814{
3815 int rval;
3816 int segments, fragment;
3817 uint32_t *dcode, dlen;
3818 uint32_t risc_addr;
3819 uint32_t risc_size;
3820 uint32_t i;
Andrew Vasquez54333832005-11-09 15:49:04 -08003821 struct fw_blob *blob;
Andrew Vasquez01071092005-07-06 10:31:37 -07003822 uint32_t *fwcode, fwclen;
3823
Andrew Vasquez54333832005-11-09 15:49:04 -08003824 /* Load firmware blob. */
3825 blob = qla2x00_request_firmware(ha);
3826 if (!blob) {
3827 qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
Andrew Vasquezd1c61902006-05-17 15:09:00 -07003828 qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
3829 "from: " QLA_FW_URL ".\n");
3830
3831 /* Try to load RISC code from flash. */
3832 qla_printk(KERN_ERR, ha, "Attempting to load (potentially "
3833 "outdated) firmware from flash.\n");
3834 return qla24xx_load_risc_flash(ha, srisc_addr);
Andrew Vasquez01071092005-07-06 10:31:37 -07003835 }
3836
3837 rval = QLA_SUCCESS;
3838
3839 segments = FA_RISC_CODE_SEGMENTS;
3840 dcode = (uint32_t *)ha->request_ring;
3841 *srisc_addr = 0;
Andrew Vasquez54333832005-11-09 15:49:04 -08003842 fwcode = (uint32_t *)blob->fw->data;
Andrew Vasquez01071092005-07-06 10:31:37 -07003843 fwclen = 0;
3844
3845 /* Validate firmware image by checking version. */
Andrew Vasquez54333832005-11-09 15:49:04 -08003846 if (blob->fw->size < 8 * sizeof(uint32_t)) {
Andrew Vasquez01071092005-07-06 10:31:37 -07003847 qla_printk(KERN_WARNING, ha,
Andrew Vasquez54333832005-11-09 15:49:04 -08003848 "Unable to verify integrity of firmware image (%Zd)!\n",
3849 blob->fw->size);
Andrew Vasquez01071092005-07-06 10:31:37 -07003850 goto fail_fw_integrity;
3851 }
3852 for (i = 0; i < 4; i++)
3853 dcode[i] = be32_to_cpu(fwcode[i + 4]);
3854 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
3855 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
3856 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
3857 dcode[3] == 0)) {
3858 qla_printk(KERN_WARNING, ha,
Andrew Vasquez54333832005-11-09 15:49:04 -08003859 "Unable to verify integrity of firmware image!\n");
Andrew Vasquez01071092005-07-06 10:31:37 -07003860 qla_printk(KERN_WARNING, ha,
3861 "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
3862 dcode[1], dcode[2], dcode[3]);
3863 goto fail_fw_integrity;
3864 }
3865
3866 while (segments && rval == QLA_SUCCESS) {
3867 risc_addr = be32_to_cpu(fwcode[2]);
3868 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
3869 risc_size = be32_to_cpu(fwcode[3]);
3870
3871 /* Validate firmware image size. */
3872 fwclen += risc_size * sizeof(uint32_t);
Andrew Vasquez54333832005-11-09 15:49:04 -08003873 if (blob->fw->size < fwclen) {
Andrew Vasquez01071092005-07-06 10:31:37 -07003874 qla_printk(KERN_WARNING, ha,
Andrew Vasquez54333832005-11-09 15:49:04 -08003875 "Unable to verify integrity of firmware image "
3876 "(%Zd)!\n", blob->fw->size);
3877
Andrew Vasquez01071092005-07-06 10:31:37 -07003878 goto fail_fw_integrity;
3879 }
3880
3881 fragment = 0;
3882 while (risc_size > 0 && rval == QLA_SUCCESS) {
3883 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
3884 if (dlen > risc_size)
3885 dlen = risc_size;
3886
3887 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3888 "addr %x, number of dwords 0x%x.\n", ha->host_no,
3889 risc_addr, dlen));
3890
3891 for (i = 0; i < dlen; i++)
3892 dcode[i] = swab32(fwcode[i]);
3893
andrew.vasquez@qlogic.com590f98e2006-01-13 17:05:37 -08003894 rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3895 dlen);
Andrew Vasquez01071092005-07-06 10:31:37 -07003896 if (rval) {
3897 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3898 "segment %d of firmware\n", ha->host_no,
3899 fragment));
3900 qla_printk(KERN_WARNING, ha,
3901 "[ERROR] Failed to load segment %d of "
3902 "firmware\n", fragment);
3903 break;
3904 }
3905
3906 fwcode += dlen;
3907 risc_addr += dlen;
3908 risc_size -= dlen;
3909 fragment++;
3910 }
3911
3912 /* Next segment. */
3913 segments--;
3914 }
Andrew Vasquez01071092005-07-06 10:31:37 -07003915 return rval;
3916
3917fail_fw_integrity:
Andrew Vasquez01071092005-07-06 10:31:37 -07003918 return QLA_FUNCTION_FAILED;
Andrew Vasquez01071092005-07-06 10:31:37 -07003919}
Andrew Vasquez18c6c122006-10-13 09:33:38 -07003920
3921void
3922qla2x00_try_to_stop_firmware(scsi_qla_host_t *ha)
3923{
3924 int ret, retries;
3925
3926 if (!IS_QLA24XX(ha) && !IS_QLA54XX(ha))
3927 return;
Andrew Vasquez75edf812007-05-07 07:43:00 -07003928 if (!ha->fw_major_version)
3929 return;
Andrew Vasquez18c6c122006-10-13 09:33:38 -07003930
3931 ret = qla2x00_stop_firmware(ha);
3932 for (retries = 5; ret != QLA_SUCCESS && retries ; retries--) {
3933 qla2x00_reset_chip(ha);
3934 if (qla2x00_chip_diag(ha) != QLA_SUCCESS)
3935 continue;
3936 if (qla2x00_setup_chip(ha) != QLA_SUCCESS)
3937 continue;
3938 qla_printk(KERN_INFO, ha,
3939 "Attempting retry of stop-firmware command...\n");
3940 ret = qla2x00_stop_firmware(ha);
3941 }
3942}