blob: a470f2d3270d205ad35ee26519a25e9d37f90252 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Andrew Vasquezfa90c542005-10-27 11:10:08 -07002 * QLogic Fibre Channel HBA Driver
Andrew Vasquez01e58d82008-04-03 13:13:13 -07003 * Copyright (c) 2003-2008 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/*
19* QLogic ISP2x00 Hardware Support Function Prototypes.
20*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070021static int qla2x00_isp_firmware(scsi_qla_host_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070022static void qla2x00_resize_request_q(scsi_qla_host_t *);
23static int qla2x00_setup_chip(scsi_qla_host_t *);
24static void qla2x00_init_response_q_entries(scsi_qla_host_t *);
25static int qla2x00_init_rings(scsi_qla_host_t *);
26static int qla2x00_fw_ready(scsi_qla_host_t *);
27static int qla2x00_configure_hba(scsi_qla_host_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028static int qla2x00_configure_loop(scsi_qla_host_t *);
29static int qla2x00_configure_local_loop(scsi_qla_host_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030static int qla2x00_configure_fabric(scsi_qla_host_t *);
31static int qla2x00_find_all_fabric_devs(scsi_qla_host_t *, struct list_head *);
32static int qla2x00_device_resync(scsi_qla_host_t *);
33static int qla2x00_fabric_dev_login(scsi_qla_host_t *, fc_port_t *,
34 uint16_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36static int qla2x00_restart_isp(scsi_qla_host_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Adrian Bunk413975a2006-06-30 02:33:06 -070038static int qla2x00_find_new_loop_id(scsi_qla_host_t *ha, fc_port_t *dev);
39
Harihara Kadayam4d4df192008-04-03 13:13:26 -070040static struct qla_chip_state_84xx *qla84xx_get_chip(struct scsi_qla_host *);
41static int qla84xx_init_chip(scsi_qla_host_t *);
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/****************************************************************************/
44/* QLogic ISP2x00 Hardware Support Functions. */
45/****************************************************************************/
46
47/*
48* qla2x00_initialize_adapter
49* Initialize board.
50*
51* Input:
52* ha = adapter block pointer.
53*
54* Returns:
55* 0 = success
56*/
57int
58qla2x00_initialize_adapter(scsi_qla_host_t *ha)
59{
60 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62 /* Clear adapter flags. */
63 ha->flags.online = 0;
64 ha->flags.reset_active = 0;
65 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
66 atomic_set(&ha->loop_state, LOOP_DOWN);
Andrew Vasquez26032212007-01-29 10:22:23 -080067 ha->device_flags = DFLG_NO_CABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 ha->dpc_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 ha->flags.management_server_logged_in = 0;
70 ha->marker_needed = 0;
71 ha->mbx_flags = 0;
72 ha->isp_abort_cnt = 0;
73 ha->beacon_blink_led = 0;
Andrew Vasquezcca53352005-08-26 19:08:30 -070074 set_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Andrew Vasquez01071092005-07-06 10:31:37 -070076 qla_printk(KERN_INFO, ha, "Configuring PCI space...\n");
Andrew Vasquezfd34f552007-07-19 15:06:00 -070077 rval = ha->isp_ops->pci_config(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 if (rval) {
Andrew Vasquez7c98a042007-01-29 10:22:29 -080079 DEBUG2(printk("scsi(%ld): Unable to configure PCI space.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 ha->host_no));
81 return (rval);
82 }
83
Andrew Vasquezfd34f552007-07-19 15:06:00 -070084 ha->isp_ops->reset_chip(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Andrew Vasquezc00d8992008-09-11 21:22:49 -070086 rval = qla2xxx_get_flash_info(ha);
87 if (rval) {
88 DEBUG2(printk("scsi(%ld): Unable to validate FLASH data.\n",
89 ha->host_no));
90 return (rval);
91 }
92
Andrew Vasquezfd34f552007-07-19 15:06:00 -070093 ha->isp_ops->get_flash_version(ha, ha->request_ring);
Andrew Vasquez30c47662007-01-29 10:22:21 -080094
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 qla_printk(KERN_INFO, ha, "Configure NVRAM parameters...\n");
Andrew Vasquez01071092005-07-06 10:31:37 -070096
Andrew Vasquezfd34f552007-07-19 15:06:00 -070097 ha->isp_ops->nvram_config(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Andrew Vasquezd4c760c2006-06-23 16:10:39 -070099 if (ha->flags.disable_serdes) {
100 /* Mask HBA via NVRAM settings? */
101 qla_printk(KERN_INFO, ha, "Masking HBA WWPN "
102 "%02x%02x%02x%02x%02x%02x%02x%02x (via NVRAM).\n",
103 ha->port_name[0], ha->port_name[1],
104 ha->port_name[2], ha->port_name[3],
105 ha->port_name[4], ha->port_name[5],
106 ha->port_name[6], ha->port_name[7]);
107 return QLA_FUNCTION_FAILED;
108 }
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 qla_printk(KERN_INFO, ha, "Verifying loaded RISC code...\n");
111
Andrew Vasquezd19044c2006-11-22 08:22:19 -0800112 if (qla2x00_isp_firmware(ha) != QLA_SUCCESS) {
Andrew Vasquezfd34f552007-07-19 15:06:00 -0700113 rval = ha->isp_ops->chip_diag(ha);
Andrew Vasquezd19044c2006-11-22 08:22:19 -0800114 if (rval)
115 return (rval);
116 rval = qla2x00_setup_chip(ha);
117 if (rval)
118 return (rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 }
Harihara Kadayam4d4df192008-04-03 13:13:26 -0700120 if (IS_QLA84XX(ha)) {
121 ha->cs84xx = qla84xx_get_chip(ha);
122 if (!ha->cs84xx) {
123 qla_printk(KERN_ERR, ha,
124 "Unable to configure ISP84XX.\n");
125 return QLA_FUNCTION_FAILED;
126 }
127 }
Andrew Vasquezd19044c2006-11-22 08:22:19 -0800128 rval = qla2x00_init_rings(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 return (rval);
131}
132
133/**
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700134 * qla2100_pci_config() - Setup ISP21xx PCI configuration registers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 * @ha: HA context
136 *
137 * Returns 0 on success.
138 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700139int
140qla2100_pci_config(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Andrew Vasqueza157b102007-05-07 07:43:01 -0700142 uint16_t w;
Adam Kropelin27b2f672005-09-16 19:28:20 -0700143 uint32_t d;
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700144 unsigned long flags;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700145 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 pci_set_master(ha->pdev);
Andrew Vasquezaf6177d2007-07-19 15:06:02 -0700148 pci_try_set_mwi(ha->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
Andrew Vasqueza157b102007-05-07 07:43:01 -0700151 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
153
154 /* Reset expansion ROM address decode enable */
Adam Kropelin27b2f672005-09-16 19:28:20 -0700155 pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
156 d &= ~PCI_ROM_ADDRESS_ENABLE;
157 pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700159 /* Get PCI bus information. */
160 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez3d716442005-07-06 10:30:26 -0700161 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700162 spin_unlock_irqrestore(&ha->hardware_lock, flags);
163
164 return QLA_SUCCESS;
165}
166
167/**
168 * qla2300_pci_config() - Setup ISP23xx PCI configuration registers.
169 * @ha: HA context
170 *
171 * Returns 0 on success.
172 */
173int
174qla2300_pci_config(scsi_qla_host_t *ha)
175{
Andrew Vasqueza157b102007-05-07 07:43:01 -0700176 uint16_t w;
Adam Kropelin27b2f672005-09-16 19:28:20 -0700177 uint32_t d;
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700178 unsigned long flags = 0;
179 uint32_t cnt;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700180 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700181
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700182 pci_set_master(ha->pdev);
Andrew Vasquezaf6177d2007-07-19 15:06:02 -0700183 pci_try_set_mwi(ha->pdev);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700184
185 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
Andrew Vasqueza157b102007-05-07 07:43:01 -0700186 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700187
188 if (IS_QLA2322(ha) || IS_QLA6322(ha))
189 w &= ~PCI_COMMAND_INTX_DISABLE;
Andrew Vasqueza157b102007-05-07 07:43:01 -0700190 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700191
192 /*
193 * If this is a 2300 card and not 2312, reset the
194 * COMMAND_INVALIDATE due to a bug in the 2300. Unfortunately,
195 * the 2310 also reports itself as a 2300 so we need to get the
196 * fb revision level -- a 6 indicates it really is a 2300 and
197 * not a 2310.
198 */
199 if (IS_QLA2300(ha)) {
200 spin_lock_irqsave(&ha->hardware_lock, flags);
201
202 /* Pause RISC. */
Andrew Vasquez3d716442005-07-06 10:30:26 -0700203 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700204 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquez3d716442005-07-06 10:30:26 -0700205 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700206 break;
207
208 udelay(10);
209 }
210
211 /* Select FPM registers. */
Andrew Vasquez3d716442005-07-06 10:30:26 -0700212 WRT_REG_WORD(&reg->ctrl_status, 0x20);
213 RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700214
215 /* Get the fb rev level */
Andrew Vasquez3d716442005-07-06 10:30:26 -0700216 ha->fb_rev = RD_FB_CMD_REG(ha, reg);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700217
218 if (ha->fb_rev == FPM_2300)
Andrew Vasqueza157b102007-05-07 07:43:01 -0700219 pci_clear_mwi(ha->pdev);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700220
221 /* Deselect FPM registers. */
Andrew Vasquez3d716442005-07-06 10:30:26 -0700222 WRT_REG_WORD(&reg->ctrl_status, 0x0);
223 RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700224
225 /* Release RISC module. */
Andrew Vasquez3d716442005-07-06 10:30:26 -0700226 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700227 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquez3d716442005-07-06 10:30:26 -0700228 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) == 0)
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700229 break;
230
231 udelay(10);
232 }
233
234 spin_unlock_irqrestore(&ha->hardware_lock, flags);
235 }
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700236
237 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
238
239 /* Reset expansion ROM address decode enable */
Adam Kropelin27b2f672005-09-16 19:28:20 -0700240 pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
241 d &= ~PCI_ROM_ADDRESS_ENABLE;
242 pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700243
244 /* Get PCI bus information. */
245 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez3d716442005-07-06 10:30:26 -0700246 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700247 spin_unlock_irqrestore(&ha->hardware_lock, flags);
248
249 return QLA_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
252/**
Andrew Vasquez01071092005-07-06 10:31:37 -0700253 * qla24xx_pci_config() - Setup ISP24xx PCI configuration registers.
254 * @ha: HA context
255 *
256 * Returns 0 on success.
257 */
258int
259qla24xx_pci_config(scsi_qla_host_t *ha)
260{
Andrew Vasqueza157b102007-05-07 07:43:01 -0700261 uint16_t w;
Adam Kropelin27b2f672005-09-16 19:28:20 -0700262 uint32_t d;
Andrew Vasquez01071092005-07-06 10:31:37 -0700263 unsigned long flags = 0;
264 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
Andrew Vasquez01071092005-07-06 10:31:37 -0700265
266 pci_set_master(ha->pdev);
Andrew Vasquezaf6177d2007-07-19 15:06:02 -0700267 pci_try_set_mwi(ha->pdev);
Andrew Vasquez01071092005-07-06 10:31:37 -0700268
269 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
Andrew Vasqueza157b102007-05-07 07:43:01 -0700270 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
Andrew Vasquez01071092005-07-06 10:31:37 -0700271 w &= ~PCI_COMMAND_INTX_DISABLE;
272 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
273
274 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
275
276 /* PCI-X -- adjust Maximum Memory Read Byte Count (2048). */
Andrew Vasquezf85ec182007-07-19 15:06:01 -0700277 if (pci_find_capability(ha->pdev, PCI_CAP_ID_PCIX))
278 pcix_set_mmrbc(ha->pdev, 2048);
Andrew Vasquez01071092005-07-06 10:31:37 -0700279
280 /* PCIe -- adjust Maximum Read Request Size (2048). */
Andrew Vasquezf85ec182007-07-19 15:06:01 -0700281 if (pci_find_capability(ha->pdev, PCI_CAP_ID_EXP))
282 pcie_set_readrq(ha->pdev, 2048);
Andrew Vasquez01071092005-07-06 10:31:37 -0700283
284 /* Reset expansion ROM address decode enable */
Adam Kropelin27b2f672005-09-16 19:28:20 -0700285 pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
286 d &= ~PCI_ROM_ADDRESS_ENABLE;
287 pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
Andrew Vasquez01071092005-07-06 10:31:37 -0700288
Auke Kok44c10132007-06-08 15:46:36 -0700289 ha->chip_revision = ha->pdev->revision;
Andrew Vasqueza8488ab2007-01-29 10:22:19 -0800290
Andrew Vasquez01071092005-07-06 10:31:37 -0700291 /* Get PCI bus information. */
292 spin_lock_irqsave(&ha->hardware_lock, flags);
293 ha->pci_attr = RD_REG_DWORD(&reg->ctrl_status);
294 spin_unlock_irqrestore(&ha->hardware_lock, flags);
295
296 return QLA_SUCCESS;
297}
298
299/**
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -0700300 * qla25xx_pci_config() - Setup ISP25xx PCI configuration registers.
301 * @ha: HA context
302 *
303 * Returns 0 on success.
304 */
305int
306qla25xx_pci_config(scsi_qla_host_t *ha)
307{
308 uint16_t w;
309 uint32_t d;
310
311 pci_set_master(ha->pdev);
312 pci_try_set_mwi(ha->pdev);
313
314 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
315 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
316 w &= ~PCI_COMMAND_INTX_DISABLE;
317 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
318
319 /* PCIe -- adjust Maximum Read Request Size (2048). */
320 if (pci_find_capability(ha->pdev, PCI_CAP_ID_EXP))
321 pcie_set_readrq(ha->pdev, 2048);
322
323 /* Reset expansion ROM address decode enable */
324 pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
325 d &= ~PCI_ROM_ADDRESS_ENABLE;
326 pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
327
328 ha->chip_revision = ha->pdev->revision;
329
330 return QLA_SUCCESS;
331}
332
333/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 * qla2x00_isp_firmware() - Choose firmware image.
335 * @ha: HA context
336 *
337 * Returns 0 on success.
338 */
339static int
340qla2x00_isp_firmware(scsi_qla_host_t *ha)
341{
342 int rval;
Andrew Vasquez42e421b2008-07-10 16:56:01 -0700343 uint16_t loop_id, topo, sw_cap;
344 uint8_t domain, area, al_pa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 /* Assume loading risc code */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700347 rval = QLA_FUNCTION_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 if (ha->flags.disable_risc_code_load) {
350 DEBUG2(printk("scsi(%ld): RISC CODE NOT loaded\n",
351 ha->host_no));
352 qla_printk(KERN_INFO, ha, "RISC CODE NOT loaded\n");
353
354 /* Verify checksum of loaded RISC code. */
Andrew Vasquez441d1072006-05-17 15:09:34 -0700355 rval = qla2x00_verify_checksum(ha, ha->fw_srisc_address);
Andrew Vasquez42e421b2008-07-10 16:56:01 -0700356 if (rval == QLA_SUCCESS) {
357 /* And, verify we are not in ROM code. */
358 rval = qla2x00_get_adapter_id(ha, &loop_id, &al_pa,
359 &area, &domain, &topo, &sw_cap);
360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
362
363 if (rval) {
364 DEBUG2_3(printk("scsi(%ld): **** Load RISC code ****\n",
365 ha->host_no));
366 }
367
368 return (rval);
369}
370
371/**
372 * qla2x00_reset_chip() - Reset ISP chip.
373 * @ha: HA context
374 *
375 * Returns 0 on success.
376 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700377void
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700378qla2x00_reset_chip(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
380 unsigned long flags = 0;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700381 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 uint32_t cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 uint16_t cmd;
384
Andrew Vasquezfd34f552007-07-19 15:06:00 -0700385 ha->isp_ops->disable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 spin_lock_irqsave(&ha->hardware_lock, flags);
388
389 /* Turn off master enable */
390 cmd = 0;
391 pci_read_config_word(ha->pdev, PCI_COMMAND, &cmd);
392 cmd &= ~PCI_COMMAND_MASTER;
393 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
394
395 if (!IS_QLA2100(ha)) {
396 /* Pause RISC. */
397 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
398 if (IS_QLA2200(ha) || IS_QLA2300(ha)) {
399 for (cnt = 0; cnt < 30000; cnt++) {
400 if ((RD_REG_WORD(&reg->hccr) &
401 HCCR_RISC_PAUSE) != 0)
402 break;
403 udelay(100);
404 }
405 } else {
406 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
407 udelay(10);
408 }
409
410 /* Select FPM registers. */
411 WRT_REG_WORD(&reg->ctrl_status, 0x20);
412 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
413
414 /* FPM Soft Reset. */
415 WRT_REG_WORD(&reg->fpm_diag_config, 0x100);
416 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
417
418 /* Toggle Fpm Reset. */
419 if (!IS_QLA2200(ha)) {
420 WRT_REG_WORD(&reg->fpm_diag_config, 0x0);
421 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
422 }
423
424 /* Select frame buffer registers. */
425 WRT_REG_WORD(&reg->ctrl_status, 0x10);
426 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
427
428 /* Reset frame buffer FIFOs. */
429 if (IS_QLA2200(ha)) {
430 WRT_FB_CMD_REG(ha, reg, 0xa000);
431 RD_FB_CMD_REG(ha, reg); /* PCI Posting. */
432 } else {
433 WRT_FB_CMD_REG(ha, reg, 0x00fc);
434
435 /* Read back fb_cmd until zero or 3 seconds max */
436 for (cnt = 0; cnt < 3000; cnt++) {
437 if ((RD_FB_CMD_REG(ha, reg) & 0xff) == 0)
438 break;
439 udelay(100);
440 }
441 }
442
443 /* Select RISC module registers. */
444 WRT_REG_WORD(&reg->ctrl_status, 0);
445 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
446
447 /* Reset RISC processor. */
448 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
449 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
450
451 /* Release RISC processor. */
452 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
453 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
454 }
455
456 WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
457 WRT_REG_WORD(&reg->hccr, HCCR_CLR_HOST_INT);
458
459 /* Reset ISP chip. */
460 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
461
462 /* Wait for RISC to recover from reset. */
463 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
464 /*
465 * It is necessary to for a delay here since the card doesn't
466 * respond to PCI reads during a reset. On some architectures
467 * this will result in an MCA.
468 */
469 udelay(20);
470 for (cnt = 30000; cnt; cnt--) {
471 if ((RD_REG_WORD(&reg->ctrl_status) &
472 CSR_ISP_SOFT_RESET) == 0)
473 break;
474 udelay(100);
475 }
476 } else
477 udelay(10);
478
479 /* Reset RISC processor. */
480 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
481
482 WRT_REG_WORD(&reg->semaphore, 0);
483
484 /* Release RISC processor. */
485 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
486 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
487
488 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
489 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquezffb39f02006-05-17 15:09:06 -0700490 if (RD_MAILBOX_REG(ha, reg, 0) != MBS_BUSY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 udelay(100);
494 }
495 } else
496 udelay(100);
497
498 /* Turn on master enable */
499 cmd |= PCI_COMMAND_MASTER;
500 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
501
502 /* Disable RISC pause on FPM parity error. */
503 if (!IS_QLA2100(ha)) {
504 WRT_REG_WORD(&reg->hccr, HCCR_DISABLE_PARITY_PAUSE);
505 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
506 }
507
508 spin_unlock_irqrestore(&ha->hardware_lock, flags);
509}
510
511/**
Andrew Vasquez88c26662005-07-08 17:59:26 -0700512 * qla24xx_reset_risc() - Perform full reset of ISP24xx RISC.
Andrew Vasquez01071092005-07-06 10:31:37 -0700513 * @ha: HA context
514 *
515 * Returns 0 on success.
516 */
Andrew Vasquez88c26662005-07-08 17:59:26 -0700517static inline void
518qla24xx_reset_risc(scsi_qla_host_t *ha)
Andrew Vasquez01071092005-07-06 10:31:37 -0700519{
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -0700520 int hw_evt = 0;
Andrew Vasquez01071092005-07-06 10:31:37 -0700521 unsigned long flags = 0;
522 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
523 uint32_t cnt, d2;
Andrew Vasquez335a1cc2005-11-08 14:37:48 -0800524 uint16_t wd;
Andrew Vasquez01071092005-07-06 10:31:37 -0700525
Andrew Vasquez01071092005-07-06 10:31:37 -0700526 spin_lock_irqsave(&ha->hardware_lock, flags);
527
528 /* Reset RISC. */
529 WRT_REG_DWORD(&reg->ctrl_status, CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
530 for (cnt = 0; cnt < 30000; cnt++) {
531 if ((RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE) == 0)
532 break;
533
534 udelay(10);
535 }
536
537 WRT_REG_DWORD(&reg->ctrl_status,
538 CSRX_ISP_SOFT_RESET|CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
Andrew Vasquez335a1cc2005-11-08 14:37:48 -0800539 pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
Andrew Vasquez88c26662005-07-08 17:59:26 -0700540
Andrew Vasquez335a1cc2005-11-08 14:37:48 -0800541 udelay(100);
Andrew Vasquez88c26662005-07-08 17:59:26 -0700542 /* Wait for firmware to complete NVRAM accesses. */
Andrew Vasquez88c26662005-07-08 17:59:26 -0700543 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
544 for (cnt = 10000 ; cnt && d2; cnt--) {
545 udelay(5);
546 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
547 barrier();
548 }
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -0700549 if (cnt == 0)
550 hw_evt = 1;
Andrew Vasquez88c26662005-07-08 17:59:26 -0700551
Andrew Vasquez335a1cc2005-11-08 14:37:48 -0800552 /* Wait for soft-reset to complete. */
Andrew Vasquez01071092005-07-06 10:31:37 -0700553 d2 = RD_REG_DWORD(&reg->ctrl_status);
554 for (cnt = 6000000 ; cnt && (d2 & CSRX_ISP_SOFT_RESET); cnt--) {
555 udelay(5);
556 d2 = RD_REG_DWORD(&reg->ctrl_status);
557 barrier();
558 }
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -0700559 if (cnt == 0 || hw_evt)
560 qla2xxx_hw_event_log(ha, HW_EVENT_RESET_ERR,
561 RD_REG_WORD(&reg->mailbox1), RD_REG_WORD(&reg->mailbox2),
562 RD_REG_WORD(&reg->mailbox3));
Andrew Vasquez01071092005-07-06 10:31:37 -0700563
564 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
565 RD_REG_DWORD(&reg->hccr);
566
567 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
568 RD_REG_DWORD(&reg->hccr);
569
570 WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_RESET);
571 RD_REG_DWORD(&reg->hccr);
572
573 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
574 for (cnt = 6000000 ; cnt && d2; cnt--) {
575 udelay(5);
576 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
577 barrier();
578 }
579
580 spin_unlock_irqrestore(&ha->hardware_lock, flags);
581}
582
583/**
Andrew Vasquez88c26662005-07-08 17:59:26 -0700584 * qla24xx_reset_chip() - Reset ISP24xx chip.
585 * @ha: HA context
586 *
587 * Returns 0 on success.
588 */
589void
590qla24xx_reset_chip(scsi_qla_host_t *ha)
591{
Andrew Vasquezfd34f552007-07-19 15:06:00 -0700592 ha->isp_ops->disable_intrs(ha);
Andrew Vasquez88c26662005-07-08 17:59:26 -0700593
594 /* Perform RISC reset. */
595 qla24xx_reset_risc(ha);
596}
597
598/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 * qla2x00_chip_diag() - Test chip for proper operation.
600 * @ha: HA context
601 *
602 * Returns 0 on success.
603 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700604int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605qla2x00_chip_diag(scsi_qla_host_t *ha)
606{
607 int rval;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700608 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 unsigned long flags = 0;
610 uint16_t data;
611 uint32_t cnt;
612 uint16_t mb[5];
613
614 /* Assume a failed state */
615 rval = QLA_FUNCTION_FAILED;
616
617 DEBUG3(printk("scsi(%ld): Testing device at %lx.\n",
618 ha->host_no, (u_long)&reg->flash_address));
619
620 spin_lock_irqsave(&ha->hardware_lock, flags);
621
622 /* Reset ISP chip. */
623 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
624
625 /*
626 * We need to have a delay here since the card will not respond while
627 * in reset causing an MCA on some architectures.
628 */
629 udelay(20);
630 data = qla2x00_debounce_register(&reg->ctrl_status);
631 for (cnt = 6000000 ; cnt && (data & CSR_ISP_SOFT_RESET); cnt--) {
632 udelay(5);
633 data = RD_REG_WORD(&reg->ctrl_status);
634 barrier();
635 }
636
637 if (!cnt)
638 goto chip_diag_failed;
639
640 DEBUG3(printk("scsi(%ld): Reset register cleared by chip reset\n",
641 ha->host_no));
642
643 /* Reset RISC processor. */
644 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
645 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
646
647 /* Workaround for QLA2312 PCI parity error */
648 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
649 data = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 0));
650 for (cnt = 6000000; cnt && (data == MBS_BUSY); cnt--) {
651 udelay(5);
652 data = RD_MAILBOX_REG(ha, reg, 0);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700653 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 }
655 } else
656 udelay(10);
657
658 if (!cnt)
659 goto chip_diag_failed;
660
661 /* Check product ID of chip */
662 DEBUG3(printk("scsi(%ld): Checking product ID of chip\n", ha->host_no));
663
664 mb[1] = RD_MAILBOX_REG(ha, reg, 1);
665 mb[2] = RD_MAILBOX_REG(ha, reg, 2);
666 mb[3] = RD_MAILBOX_REG(ha, reg, 3);
667 mb[4] = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 4));
668 if (mb[1] != PROD_ID_1 || (mb[2] != PROD_ID_2 && mb[2] != PROD_ID_2a) ||
669 mb[3] != PROD_ID_3) {
670 qla_printk(KERN_WARNING, ha,
671 "Wrong product ID = 0x%x,0x%x,0x%x\n", mb[1], mb[2], mb[3]);
672
673 goto chip_diag_failed;
674 }
675 ha->product_id[0] = mb[1];
676 ha->product_id[1] = mb[2];
677 ha->product_id[2] = mb[3];
678 ha->product_id[3] = mb[4];
679
680 /* Adjust fw RISC transfer size */
681 if (ha->request_q_length > 1024)
682 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
683 else
684 ha->fw_transfer_size = REQUEST_ENTRY_SIZE *
685 ha->request_q_length;
686
687 if (IS_QLA2200(ha) &&
688 RD_MAILBOX_REG(ha, reg, 7) == QLA2200A_RISC_ROM_VER) {
689 /* Limit firmware transfer size with a 2200A */
690 DEBUG3(printk("scsi(%ld): Found QLA2200A chip.\n",
691 ha->host_no));
692
andrew.vasquez@qlogic.comea5b6382006-03-09 14:27:08 -0800693 ha->device_type |= DT_ISP2200A;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 ha->fw_transfer_size = 128;
695 }
696
697 /* Wrap Incoming Mailboxes Test. */
698 spin_unlock_irqrestore(&ha->hardware_lock, flags);
699
700 DEBUG3(printk("scsi(%ld): Checking mailboxes.\n", ha->host_no));
701 rval = qla2x00_mbx_reg_test(ha);
702 if (rval) {
703 DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
704 ha->host_no));
705 qla_printk(KERN_WARNING, ha,
706 "Failed mailbox send register test\n");
707 }
708 else {
709 /* Flag a successful rval */
710 rval = QLA_SUCCESS;
711 }
712 spin_lock_irqsave(&ha->hardware_lock, flags);
713
714chip_diag_failed:
715 if (rval)
716 DEBUG2_3(printk("scsi(%ld): Chip diagnostics **** FAILED "
717 "****\n", ha->host_no));
718
719 spin_unlock_irqrestore(&ha->hardware_lock, flags);
720
721 return (rval);
722}
723
724/**
Andrew Vasquez01071092005-07-06 10:31:37 -0700725 * qla24xx_chip_diag() - Test ISP24xx for proper operation.
726 * @ha: HA context
727 *
728 * Returns 0 on success.
729 */
730int
731qla24xx_chip_diag(scsi_qla_host_t *ha)
732{
733 int rval;
Andrew Vasquez01071092005-07-06 10:31:37 -0700734
Andrew Vasquez88c26662005-07-08 17:59:26 -0700735 /* Perform RISC reset. */
736 qla24xx_reset_risc(ha);
Andrew Vasquez01071092005-07-06 10:31:37 -0700737
Andrew Vasquez6d052522008-07-10 16:56:02 -0700738 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * ha->request_q_length;
Andrew Vasquez01071092005-07-06 10:31:37 -0700739
740 rval = qla2x00_mbx_reg_test(ha);
741 if (rval) {
742 DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
743 ha->host_no));
744 qla_printk(KERN_WARNING, ha,
745 "Failed mailbox send register test\n");
746 } else {
747 /* Flag a successful rval */
748 rval = QLA_SUCCESS;
749 }
750
751 return rval;
752}
753
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700754void
Andrew Vasquez01071092005-07-06 10:31:37 -0700755qla2x00_alloc_fw_dump(scsi_qla_host_t *ha)
756{
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700757 int rval;
758 uint32_t dump_size, fixed_size, mem_size, req_q_size, rsp_q_size,
Andrew Vasquezdf613b92008-01-17 09:02:17 -0800759 eft_size, fce_size;
760 dma_addr_t tc_dma;
761 void *tc;
Andrew Vasquezd4e3e042006-05-17 15:09:50 -0700762
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700763 if (ha->fw_dump) {
764 qla_printk(KERN_WARNING, ha,
765 "Firmware dump previously allocated.\n");
766 return;
Andrew Vasquezd4e3e042006-05-17 15:09:50 -0700767 }
768
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700769 ha->fw_dumped = 0;
Andrew Vasquezdf613b92008-01-17 09:02:17 -0800770 fixed_size = mem_size = eft_size = fce_size = 0;
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700771 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
772 fixed_size = sizeof(struct qla2100_fw_dump);
773 } else if (IS_QLA23XX(ha)) {
774 fixed_size = offsetof(struct qla2300_fw_dump, data_ram);
775 mem_size = (ha->fw_memory_size - 0x11000 + 1) *
776 sizeof(uint16_t);
Andrew Vasqueze4289242007-07-19 15:05:56 -0700777 } else if (IS_FWI2_CAPABLE(ha)) {
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -0700778 fixed_size = IS_QLA25XX(ha) ?
779 offsetof(struct qla25xx_fw_dump, ext_mem):
780 offsetof(struct qla24xx_fw_dump, ext_mem);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700781 mem_size = (ha->fw_memory_size - 0x100000 + 1) *
782 sizeof(uint32_t);
783
Andrew Vasquez436a7b12008-07-10 16:55:54 -0700784 /* Allocate memory for Fibre Channel Event Buffer. */
785 if (!IS_QLA25XX(ha))
786 goto try_eft;
787
788 tc = dma_alloc_coherent(&ha->pdev->dev, FCE_SIZE, &tc_dma,
789 GFP_KERNEL);
790 if (!tc) {
791 qla_printk(KERN_WARNING, ha, "Unable to allocate "
792 "(%d KB) for FCE.\n", FCE_SIZE / 1024);
793 goto try_eft;
794 }
795
796 memset(tc, 0, FCE_SIZE);
797 rval = qla2x00_enable_fce_trace(ha, tc_dma, FCE_NUM_BUFFERS,
798 ha->fce_mb, &ha->fce_bufs);
799 if (rval) {
800 qla_printk(KERN_WARNING, ha, "Unable to initialize "
801 "FCE (%d).\n", rval);
802 dma_free_coherent(&ha->pdev->dev, FCE_SIZE, tc,
803 tc_dma);
804 ha->flags.fce_enabled = 0;
805 goto try_eft;
806 }
807
808 qla_printk(KERN_INFO, ha, "Allocated (%d KB) for FCE...\n",
809 FCE_SIZE / 1024);
810
811 fce_size = sizeof(struct qla2xxx_fce_chain) + EFT_SIZE;
812 ha->flags.fce_enabled = 1;
813 ha->fce_dma = tc_dma;
814 ha->fce = tc;
815try_eft:
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700816 /* Allocate memory for Extended Trace Buffer. */
Andrew Vasquezdf613b92008-01-17 09:02:17 -0800817 tc = dma_alloc_coherent(&ha->pdev->dev, EFT_SIZE, &tc_dma,
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700818 GFP_KERNEL);
Andrew Vasquezdf613b92008-01-17 09:02:17 -0800819 if (!tc) {
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700820 qla_printk(KERN_WARNING, ha, "Unable to allocate "
821 "(%d KB) for EFT.\n", EFT_SIZE / 1024);
822 goto cont_alloc;
823 }
824
Andrew Vasquezfc447652008-01-17 09:02:18 -0800825 memset(tc, 0, EFT_SIZE);
Andrew Vasquezdf613b92008-01-17 09:02:17 -0800826 rval = qla2x00_enable_eft_trace(ha, tc_dma, EFT_NUM_BUFFERS);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700827 if (rval) {
828 qla_printk(KERN_WARNING, ha, "Unable to initialize "
829 "EFT (%d).\n", rval);
Andrew Vasquezdf613b92008-01-17 09:02:17 -0800830 dma_free_coherent(&ha->pdev->dev, EFT_SIZE, tc,
831 tc_dma);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700832 goto cont_alloc;
833 }
834
835 qla_printk(KERN_INFO, ha, "Allocated (%d KB) for EFT...\n",
836 EFT_SIZE / 1024);
837
838 eft_size = EFT_SIZE;
Andrew Vasquezdf613b92008-01-17 09:02:17 -0800839 ha->eft_dma = tc_dma;
840 ha->eft = tc;
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700841 }
842cont_alloc:
843 req_q_size = ha->request_q_length * sizeof(request_t);
844 rsp_q_size = ha->response_q_length * sizeof(response_t);
845
846 dump_size = offsetof(struct qla2xxx_fw_dump, isp);
847 dump_size += fixed_size + mem_size + req_q_size + rsp_q_size +
Andrew Vasquezdf613b92008-01-17 09:02:17 -0800848 eft_size + fce_size;
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700849
Andrew Vasquezd4e3e042006-05-17 15:09:50 -0700850 ha->fw_dump = vmalloc(dump_size);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700851 if (!ha->fw_dump) {
Andrew Vasquez01071092005-07-06 10:31:37 -0700852 qla_printk(KERN_WARNING, ha, "Unable to allocate (%d KB) for "
Andrew Vasquezd4e3e042006-05-17 15:09:50 -0700853 "firmware dump!!!\n", dump_size / 1024);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700854
855 if (ha->eft) {
856 dma_free_coherent(&ha->pdev->dev, eft_size, ha->eft,
857 ha->eft_dma);
858 ha->eft = NULL;
859 ha->eft_dma = 0;
860 }
861 return;
862 }
863
864 qla_printk(KERN_INFO, ha, "Allocated (%d KB) for firmware dump...\n",
865 dump_size / 1024);
866
867 ha->fw_dump_len = dump_size;
868 ha->fw_dump->signature[0] = 'Q';
869 ha->fw_dump->signature[1] = 'L';
870 ha->fw_dump->signature[2] = 'G';
871 ha->fw_dump->signature[3] = 'C';
872 ha->fw_dump->version = __constant_htonl(1);
873
874 ha->fw_dump->fixed_size = htonl(fixed_size);
875 ha->fw_dump->mem_size = htonl(mem_size);
876 ha->fw_dump->req_q_size = htonl(req_q_size);
877 ha->fw_dump->rsp_q_size = htonl(rsp_q_size);
878
879 ha->fw_dump->eft_size = htonl(eft_size);
880 ha->fw_dump->eft_addr_l = htonl(LSD(ha->eft_dma));
881 ha->fw_dump->eft_addr_h = htonl(MSD(ha->eft_dma));
882
883 ha->fw_dump->header_size =
884 htonl(offsetof(struct qla2xxx_fw_dump, isp));
Andrew Vasquez01071092005-07-06 10:31:37 -0700885}
886
887/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 * qla2x00_resize_request_q() - Resize request queue given available ISP memory.
889 * @ha: HA context
890 *
891 * Returns 0 on success.
892 */
893static void
894qla2x00_resize_request_q(scsi_qla_host_t *ha)
895{
896 int rval;
897 uint16_t fw_iocb_cnt = 0;
898 uint16_t request_q_length = REQUEST_ENTRY_CNT_2XXX_EXT_MEM;
899 dma_addr_t request_dma;
900 request_t *request_ring;
901
902 /* Valid only on recent ISPs. */
903 if (IS_QLA2100(ha) || IS_QLA2200(ha))
904 return;
905
906 /* Retrieve IOCB counts available to the firmware. */
Seokmann Ju4d0ea242007-09-20 14:07:43 -0700907 rval = qla2x00_get_resource_cnts(ha, NULL, NULL, NULL, &fw_iocb_cnt,
908 &ha->max_npiv_vports);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if (rval)
910 return;
911 /* No point in continuing if current settings are sufficient. */
912 if (fw_iocb_cnt < 1024)
913 return;
914 if (ha->request_q_length >= request_q_length)
915 return;
916
917 /* Attempt to claim larger area for request queue. */
918 request_ring = dma_alloc_coherent(&ha->pdev->dev,
919 (request_q_length + 1) * sizeof(request_t), &request_dma,
920 GFP_KERNEL);
921 if (request_ring == NULL)
922 return;
923
924 /* Resize successful, report extensions. */
925 qla_printk(KERN_INFO, ha, "Extended memory detected (%d KB)...\n",
926 (ha->fw_memory_size + 1) / 1024);
927 qla_printk(KERN_INFO, ha, "Resizing request queue depth "
928 "(%d -> %d)...\n", ha->request_q_length, request_q_length);
929
930 /* Clear old allocations. */
931 dma_free_coherent(&ha->pdev->dev,
932 (ha->request_q_length + 1) * sizeof(request_t), ha->request_ring,
933 ha->request_dma);
934
935 /* Begin using larger queue. */
936 ha->request_q_length = request_q_length;
937 ha->request_ring = request_ring;
938 ha->request_dma = request_dma;
939}
940
941/**
942 * qla2x00_setup_chip() - Load and start RISC firmware.
943 * @ha: HA context
944 *
945 * Returns 0 on success.
946 */
947static int
948qla2x00_setup_chip(scsi_qla_host_t *ha)
949{
Andrew Vasquez01071092005-07-06 10:31:37 -0700950 int rval;
951 uint32_t srisc_address = 0;
Andrew Vasquez3db06522008-01-31 12:33:49 -0800952 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
953 unsigned long flags;
954
955 if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
956 /* Disable SRAM, Instruction RAM and GP RAM parity. */
957 spin_lock_irqsave(&ha->hardware_lock, flags);
958 WRT_REG_WORD(&reg->hccr, (HCCR_ENABLE_PARITY + 0x0));
959 RD_REG_WORD(&reg->hccr);
960 spin_unlock_irqrestore(&ha->hardware_lock, flags);
961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
963 /* Load firmware sequences */
Andrew Vasquezfd34f552007-07-19 15:06:00 -0700964 rval = ha->isp_ops->load_risc(ha, &srisc_address);
Andrew Vasquez01071092005-07-06 10:31:37 -0700965 if (rval == QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 DEBUG(printk("scsi(%ld): Verifying Checksum of loaded RISC "
967 "code.\n", ha->host_no));
968
Andrew Vasquez01071092005-07-06 10:31:37 -0700969 rval = qla2x00_verify_checksum(ha, srisc_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 if (rval == QLA_SUCCESS) {
971 /* Start firmware execution. */
972 DEBUG(printk("scsi(%ld): Checksum OK, start "
973 "firmware.\n", ha->host_no));
974
Andrew Vasquez01071092005-07-06 10:31:37 -0700975 rval = qla2x00_execute_fw(ha, srisc_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 /* Retrieve firmware information. */
977 if (rval == QLA_SUCCESS && ha->fw_major_version == 0) {
978 qla2x00_get_fw_version(ha,
979 &ha->fw_major_version,
980 &ha->fw_minor_version,
981 &ha->fw_subminor_version,
982 &ha->fw_attributes, &ha->fw_memory_size);
983 qla2x00_resize_request_q(ha);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700984 ha->flags.npiv_supported = 0;
Mike Hernandez946fb892008-08-13 21:36:59 -0700985 if ((IS_QLA24XX(ha) || IS_QLA25XX(ha) ||
986 IS_QLA84XX(ha)) &&
987 (ha->fw_attributes & BIT_2)) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700988 ha->flags.npiv_supported = 1;
Seokmann Ju4d0ea242007-09-20 14:07:43 -0700989 if ((!ha->max_npiv_vports) ||
990 ((ha->max_npiv_vports + 1) %
Andrew Vasquezeb66dc62007-11-12 10:30:58 -0800991 MIN_MULTI_ID_FABRIC))
Seokmann Ju4d0ea242007-09-20 14:07:43 -0700992 ha->max_npiv_vports =
Andrew Vasquezeb66dc62007-11-12 10:30:58 -0800993 MIN_MULTI_ID_FABRIC - 1;
Seokmann Ju4d0ea242007-09-20 14:07:43 -0700994 }
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700995
996 if (ql2xallocfwdump)
997 qla2x00_alloc_fw_dump(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 }
999 } else {
1000 DEBUG2(printk(KERN_INFO
1001 "scsi(%ld): ISP Firmware failed checksum.\n",
1002 ha->host_no));
1003 }
1004 }
1005
Andrew Vasquez3db06522008-01-31 12:33:49 -08001006 if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
1007 /* Enable proper parity. */
1008 spin_lock_irqsave(&ha->hardware_lock, flags);
1009 if (IS_QLA2300(ha))
1010 /* SRAM parity */
1011 WRT_REG_WORD(&reg->hccr, HCCR_ENABLE_PARITY + 0x1);
1012 else
1013 /* SRAM, Instruction RAM and GP RAM parity */
1014 WRT_REG_WORD(&reg->hccr, HCCR_ENABLE_PARITY + 0x7);
1015 RD_REG_WORD(&reg->hccr);
1016 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1017 }
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 if (rval) {
1020 DEBUG2_3(printk("scsi(%ld): Setup chip **** FAILED ****.\n",
1021 ha->host_no));
1022 }
1023
1024 return (rval);
1025}
1026
1027/**
1028 * qla2x00_init_response_q_entries() - Initializes response queue entries.
1029 * @ha: HA context
1030 *
1031 * Beginning of request ring has initialization control block already built
1032 * by nvram config routine.
1033 *
1034 * Returns 0 on success.
1035 */
1036static void
1037qla2x00_init_response_q_entries(scsi_qla_host_t *ha)
1038{
1039 uint16_t cnt;
1040 response_t *pkt;
1041
1042 pkt = ha->response_ring_ptr;
1043 for (cnt = 0; cnt < ha->response_q_length; cnt++) {
1044 pkt->signature = RESPONSE_PROCESSED;
1045 pkt++;
1046 }
1047
1048}
1049
1050/**
1051 * qla2x00_update_fw_options() - Read and process firmware options.
1052 * @ha: HA context
1053 *
1054 * Returns 0 on success.
1055 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001056void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057qla2x00_update_fw_options(scsi_qla_host_t *ha)
1058{
1059 uint16_t swing, emphasis, tx_sens, rx_sens;
1060
1061 memset(ha->fw_options, 0, sizeof(ha->fw_options));
1062 qla2x00_get_fw_options(ha, ha->fw_options);
1063
1064 if (IS_QLA2100(ha) || IS_QLA2200(ha))
1065 return;
1066
1067 /* Serial Link options. */
1068 DEBUG3(printk("scsi(%ld): Serial link options:\n",
1069 ha->host_no));
1070 DEBUG3(qla2x00_dump_buffer((uint8_t *)&ha->fw_seriallink_options,
1071 sizeof(ha->fw_seriallink_options)));
1072
1073 ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
1074 if (ha->fw_seriallink_options[3] & BIT_2) {
1075 ha->fw_options[1] |= FO1_SET_EMPHASIS_SWING;
1076
1077 /* 1G settings */
1078 swing = ha->fw_seriallink_options[2] & (BIT_2 | BIT_1 | BIT_0);
1079 emphasis = (ha->fw_seriallink_options[2] &
1080 (BIT_4 | BIT_3)) >> 3;
1081 tx_sens = ha->fw_seriallink_options[0] &
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001082 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 rx_sens = (ha->fw_seriallink_options[0] &
1084 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
1085 ha->fw_options[10] = (emphasis << 14) | (swing << 8);
1086 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
1087 if (rx_sens == 0x0)
1088 rx_sens = 0x3;
1089 ha->fw_options[10] |= (tx_sens << 4) | rx_sens;
1090 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
1091 ha->fw_options[10] |= BIT_5 |
1092 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
1093 (tx_sens & (BIT_1 | BIT_0));
1094
1095 /* 2G settings */
1096 swing = (ha->fw_seriallink_options[2] &
1097 (BIT_7 | BIT_6 | BIT_5)) >> 5;
1098 emphasis = ha->fw_seriallink_options[3] & (BIT_1 | BIT_0);
1099 tx_sens = ha->fw_seriallink_options[1] &
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001100 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 rx_sens = (ha->fw_seriallink_options[1] &
1102 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
1103 ha->fw_options[11] = (emphasis << 14) | (swing << 8);
1104 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
1105 if (rx_sens == 0x0)
1106 rx_sens = 0x3;
1107 ha->fw_options[11] |= (tx_sens << 4) | rx_sens;
1108 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
1109 ha->fw_options[11] |= BIT_5 |
1110 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
1111 (tx_sens & (BIT_1 | BIT_0));
1112 }
1113
1114 /* FCP2 options. */
1115 /* Return command IOCBs without waiting for an ABTS to complete. */
1116 ha->fw_options[3] |= BIT_13;
1117
1118 /* LED scheme. */
1119 if (ha->flags.enable_led_scheme)
1120 ha->fw_options[2] |= BIT_12;
1121
andrew.vasquez@qlogic.com48c02fd2006-03-09 14:27:18 -08001122 /* Detect ISP6312. */
1123 if (IS_QLA6312(ha))
1124 ha->fw_options[2] |= BIT_13;
1125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 /* Update firmware options. */
1127 qla2x00_set_fw_options(ha, ha->fw_options);
1128}
1129
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001130void
Andrew Vasquez01071092005-07-06 10:31:37 -07001131qla24xx_update_fw_options(scsi_qla_host_t *ha)
1132{
1133 int rval;
1134
1135 /* Update Serial Link options. */
andrew.vasquez@qlogic.comf94097e2006-01-13 17:05:32 -08001136 if ((le16_to_cpu(ha->fw_seriallink_options24[0]) & BIT_0) == 0)
Andrew Vasquez01071092005-07-06 10:31:37 -07001137 return;
1138
andrew.vasquez@qlogic.comf94097e2006-01-13 17:05:32 -08001139 rval = qla2x00_set_serdes_params(ha,
1140 le16_to_cpu(ha->fw_seriallink_options24[1]),
1141 le16_to_cpu(ha->fw_seriallink_options24[2]),
1142 le16_to_cpu(ha->fw_seriallink_options24[3]));
Andrew Vasquez01071092005-07-06 10:31:37 -07001143 if (rval != QLA_SUCCESS) {
1144 qla_printk(KERN_WARNING, ha,
1145 "Unable to update Serial Link options (%x).\n", rval);
1146 }
1147}
1148
1149void
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001150qla2x00_config_rings(struct scsi_qla_host *ha)
1151{
Andrew Vasquez3d716442005-07-06 10:30:26 -07001152 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001153
1154 /* Setup ring parameters in initialization control block. */
1155 ha->init_cb->request_q_outpointer = __constant_cpu_to_le16(0);
1156 ha->init_cb->response_q_inpointer = __constant_cpu_to_le16(0);
1157 ha->init_cb->request_q_length = cpu_to_le16(ha->request_q_length);
1158 ha->init_cb->response_q_length = cpu_to_le16(ha->response_q_length);
1159 ha->init_cb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
1160 ha->init_cb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
1161 ha->init_cb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
1162 ha->init_cb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
1163
1164 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), 0);
1165 WRT_REG_WORD(ISP_REQ_Q_OUT(ha, reg), 0);
1166 WRT_REG_WORD(ISP_RSP_Q_IN(ha, reg), 0);
1167 WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), 0);
1168 RD_REG_WORD(ISP_RSP_Q_OUT(ha, reg)); /* PCI Posting. */
1169}
1170
Andrew Vasquez01071092005-07-06 10:31:37 -07001171void
1172qla24xx_config_rings(struct scsi_qla_host *ha)
1173{
1174 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1175 struct init_cb_24xx *icb;
1176
1177 /* Setup ring parameters in initialization control block. */
1178 icb = (struct init_cb_24xx *)ha->init_cb;
1179 icb->request_q_outpointer = __constant_cpu_to_le16(0);
1180 icb->response_q_inpointer = __constant_cpu_to_le16(0);
1181 icb->request_q_length = cpu_to_le16(ha->request_q_length);
1182 icb->response_q_length = cpu_to_le16(ha->response_q_length);
1183 icb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
1184 icb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
1185 icb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
1186 icb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
1187
1188 WRT_REG_DWORD(&reg->req_q_in, 0);
1189 WRT_REG_DWORD(&reg->req_q_out, 0);
1190 WRT_REG_DWORD(&reg->rsp_q_in, 0);
1191 WRT_REG_DWORD(&reg->rsp_q_out, 0);
1192 RD_REG_DWORD(&reg->rsp_q_out);
1193}
1194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195/**
1196 * qla2x00_init_rings() - Initializes firmware.
1197 * @ha: HA context
1198 *
1199 * Beginning of request ring has initialization control block already built
1200 * by nvram config routine.
1201 *
1202 * Returns 0 on success.
1203 */
1204static int
1205qla2x00_init_rings(scsi_qla_host_t *ha)
1206{
1207 int rval;
1208 unsigned long flags = 0;
1209 int cnt;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001210 struct mid_init_cb_24xx *mid_init_cb =
1211 (struct mid_init_cb_24xx *) ha->init_cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213 spin_lock_irqsave(&ha->hardware_lock, flags);
1214
1215 /* Clear outstanding commands array. */
1216 for (cnt = 0; cnt < MAX_OUTSTANDING_COMMANDS; cnt++)
1217 ha->outstanding_cmds[cnt] = NULL;
1218
1219 ha->current_outstanding_cmd = 0;
1220
1221 /* Clear RSCN queue. */
1222 ha->rscn_in_ptr = 0;
1223 ha->rscn_out_ptr = 0;
1224
1225 /* Initialize firmware. */
1226 ha->request_ring_ptr = ha->request_ring;
1227 ha->req_ring_index = 0;
1228 ha->req_q_cnt = ha->request_q_length;
1229 ha->response_ring_ptr = ha->response_ring;
1230 ha->rsp_ring_index = 0;
1231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 /* Initialize response queue entries */
1233 qla2x00_init_response_q_entries(ha);
1234
Andrew Vasquezfd34f552007-07-19 15:06:00 -07001235 ha->isp_ops->config_rings(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1238
1239 /* Update any ISP specific firmware options before initialization. */
Andrew Vasquezfd34f552007-07-19 15:06:00 -07001240 ha->isp_ops->update_fw_options(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242 DEBUG(printk("scsi(%ld): Issue init firmware.\n", ha->host_no));
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001243
Seokmann Juc48339d2008-01-17 09:02:19 -08001244 if (ha->flags.npiv_supported)
1245 mid_init_cb->count = cpu_to_le16(ha->max_npiv_vports);
1246
Andrew Vasquezeb66dc62007-11-12 10:30:58 -08001247 mid_init_cb->options = __constant_cpu_to_le16(BIT_1);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001248
Andrew Vasquez01071092005-07-06 10:31:37 -07001249 rval = qla2x00_init_firmware(ha, ha->init_cb_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 if (rval) {
1251 DEBUG2_3(printk("scsi(%ld): Init firmware **** FAILED ****.\n",
1252 ha->host_no));
1253 } else {
1254 DEBUG3(printk("scsi(%ld): Init firmware -- success.\n",
1255 ha->host_no));
1256 }
1257
1258 return (rval);
1259}
1260
1261/**
1262 * qla2x00_fw_ready() - Waits for firmware ready.
1263 * @ha: HA context
1264 *
1265 * Returns 0 on success.
1266 */
1267static int
1268qla2x00_fw_ready(scsi_qla_host_t *ha)
1269{
1270 int rval;
Harihara Kadayam4d4df192008-04-03 13:13:26 -07001271 unsigned long wtime, mtime, cs84xx_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 uint16_t min_wait; /* Minimum wait time if loop is down */
1273 uint16_t wait_time; /* Wait time if loop is coming ready */
Harihara Kadayam4d4df192008-04-03 13:13:26 -07001274 uint16_t state[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 rval = QLA_SUCCESS;
1277
1278 /* 20 seconds for loop down. */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001279 min_wait = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281 /*
1282 * Firmware should take at most one RATOV to login, plus 5 seconds for
1283 * our own processing.
1284 */
1285 if ((wait_time = (ha->retry_count*ha->login_timeout) + 5) < min_wait) {
1286 wait_time = min_wait;
1287 }
1288
1289 /* Min wait time if loop down */
1290 mtime = jiffies + (min_wait * HZ);
1291
1292 /* wait time before firmware ready */
1293 wtime = jiffies + (wait_time * HZ);
1294
1295 /* Wait for ISP to finish LIP */
1296 if (!ha->flags.init_done)
1297 qla_printk(KERN_INFO, ha, "Waiting for LIP to complete...\n");
1298
1299 DEBUG3(printk("scsi(%ld): Waiting for LIP to complete...\n",
1300 ha->host_no));
1301
1302 do {
Harihara Kadayam4d4df192008-04-03 13:13:26 -07001303 rval = qla2x00_get_firmware_state(ha, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 if (rval == QLA_SUCCESS) {
Harihara Kadayam4d4df192008-04-03 13:13:26 -07001305 if (state[0] < FSTATE_LOSS_OF_SYNC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 ha->device_flags &= ~DFLG_NO_CABLE;
1307 }
Harihara Kadayam4d4df192008-04-03 13:13:26 -07001308 if (IS_QLA84XX(ha) && state[0] != FSTATE_READY) {
1309 DEBUG16(printk("scsi(%ld): fw_state=%x "
1310 "84xx=%x.\n", ha->host_no, state[0],
1311 state[2]));
1312 if ((state[2] & FSTATE_LOGGED_IN) &&
1313 (state[2] & FSTATE_WAITING_FOR_VERIFY)) {
1314 DEBUG16(printk("scsi(%ld): Sending "
1315 "verify iocb.\n", ha->host_no));
1316
1317 cs84xx_time = jiffies;
1318 rval = qla84xx_init_chip(ha);
1319 if (rval != QLA_SUCCESS)
1320 break;
1321
1322 /* Add time taken to initialize. */
1323 cs84xx_time = jiffies - cs84xx_time;
1324 wtime += cs84xx_time;
1325 mtime += cs84xx_time;
1326 DEBUG16(printk("scsi(%ld): Increasing "
1327 "wait time by %ld. New time %ld\n",
1328 ha->host_no, cs84xx_time, wtime));
1329 }
1330 } else if (state[0] == FSTATE_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 DEBUG(printk("scsi(%ld): F/W Ready - OK \n",
1332 ha->host_no));
1333
1334 qla2x00_get_retry_cnt(ha, &ha->retry_count,
1335 &ha->login_timeout, &ha->r_a_tov);
1336
1337 rval = QLA_SUCCESS;
1338 break;
1339 }
1340
1341 rval = QLA_FUNCTION_FAILED;
1342
1343 if (atomic_read(&ha->loop_down_timer) &&
Harihara Kadayam4d4df192008-04-03 13:13:26 -07001344 state[0] != FSTATE_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 /* Loop down. Timeout on min_wait for states
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001346 * other than Wait for Login.
1347 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 if (time_after_eq(jiffies, mtime)) {
1349 qla_printk(KERN_INFO, ha,
1350 "Cable is unplugged...\n");
1351
1352 ha->device_flags |= DFLG_NO_CABLE;
1353 break;
1354 }
1355 }
1356 } else {
1357 /* Mailbox cmd failed. Timeout on min_wait. */
1358 if (time_after_eq(jiffies, mtime))
1359 break;
1360 }
1361
1362 if (time_after_eq(jiffies, wtime))
1363 break;
1364
1365 /* Delay for a while */
1366 msleep(500);
1367
1368 DEBUG3(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
Harihara Kadayam4d4df192008-04-03 13:13:26 -07001369 ha->host_no, state[0], jiffies));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 } while (1);
1371
1372 DEBUG(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
Harihara Kadayam4d4df192008-04-03 13:13:26 -07001373 ha->host_no, state[0], jiffies));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
1375 if (rval) {
1376 DEBUG2_3(printk("scsi(%ld): Firmware ready **** FAILED ****.\n",
1377 ha->host_no));
1378 }
1379
1380 return (rval);
1381}
1382
1383/*
1384* qla2x00_configure_hba
1385* Setup adapter context.
1386*
1387* Input:
1388* ha = adapter state pointer.
1389*
1390* Returns:
1391* 0 = success
1392*
1393* Context:
1394* Kernel context.
1395*/
1396static int
1397qla2x00_configure_hba(scsi_qla_host_t *ha)
1398{
1399 int rval;
1400 uint16_t loop_id;
1401 uint16_t topo;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001402 uint16_t sw_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 uint8_t al_pa;
1404 uint8_t area;
1405 uint8_t domain;
1406 char connect_type[22];
1407
1408 /* Get host addresses. */
1409 rval = qla2x00_get_adapter_id(ha,
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001410 &loop_id, &al_pa, &area, &domain, &topo, &sw_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 if (rval != QLA_SUCCESS) {
Andrew Vasquez23443b12005-12-06 10:57:06 -08001412 if (LOOP_TRANSITION(ha) || atomic_read(&ha->loop_down_timer) ||
Ravi Anand33135aa2005-11-08 14:37:20 -08001413 (rval == QLA_COMMAND_ERROR && loop_id == 0x7)) {
1414 DEBUG2(printk("%s(%ld) Loop is in a transition state\n",
1415 __func__, ha->host_no));
1416 } else {
1417 qla_printk(KERN_WARNING, ha,
1418 "ERROR -- Unable to get host loop ID.\n");
1419 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
1420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 return (rval);
1422 }
1423
1424 if (topo == 4) {
1425 qla_printk(KERN_INFO, ha,
1426 "Cannot get topology - retrying.\n");
1427 return (QLA_FUNCTION_FAILED);
1428 }
1429
1430 ha->loop_id = loop_id;
1431
1432 /* initialize */
1433 ha->min_external_loopid = SNS_FIRST_LOOP_ID;
1434 ha->operating_mode = LOOP;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001435 ha->switch_cap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
1437 switch (topo) {
1438 case 0:
1439 DEBUG3(printk("scsi(%ld): HBA in NL topology.\n",
1440 ha->host_no));
1441 ha->current_topology = ISP_CFG_NL;
1442 strcpy(connect_type, "(Loop)");
1443 break;
1444
1445 case 1:
1446 DEBUG3(printk("scsi(%ld): HBA in FL topology.\n",
1447 ha->host_no));
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001448 ha->switch_cap = sw_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 ha->current_topology = ISP_CFG_FL;
1450 strcpy(connect_type, "(FL_Port)");
1451 break;
1452
1453 case 2:
1454 DEBUG3(printk("scsi(%ld): HBA in N P2P topology.\n",
1455 ha->host_no));
1456 ha->operating_mode = P2P;
1457 ha->current_topology = ISP_CFG_N;
1458 strcpy(connect_type, "(N_Port-to-N_Port)");
1459 break;
1460
1461 case 3:
1462 DEBUG3(printk("scsi(%ld): HBA in F P2P topology.\n",
1463 ha->host_no));
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001464 ha->switch_cap = sw_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 ha->operating_mode = P2P;
1466 ha->current_topology = ISP_CFG_F;
1467 strcpy(connect_type, "(F_Port)");
1468 break;
1469
1470 default:
1471 DEBUG3(printk("scsi(%ld): HBA in unknown topology %x. "
1472 "Using NL.\n",
1473 ha->host_no, topo));
1474 ha->current_topology = ISP_CFG_NL;
1475 strcpy(connect_type, "(Loop)");
1476 break;
1477 }
1478
1479 /* Save Host port and loop ID. */
1480 /* byte order - Big Endian */
1481 ha->d_id.b.domain = domain;
1482 ha->d_id.b.area = area;
1483 ha->d_id.b.al_pa = al_pa;
1484
1485 if (!ha->flags.init_done)
1486 qla_printk(KERN_INFO, ha,
1487 "Topology - %s, Host Loop address 0x%x\n",
1488 connect_type, ha->loop_id);
1489
1490 if (rval) {
1491 DEBUG2_3(printk("scsi(%ld): FAILED.\n", ha->host_no));
1492 } else {
1493 DEBUG3(printk("scsi(%ld): exiting normally.\n", ha->host_no));
1494 }
1495
1496 return(rval);
1497}
1498
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08001499static inline void
1500qla2x00_set_model_info(scsi_qla_host_t *ha, uint8_t *model, size_t len, char *def)
1501{
1502 char *st, *en;
1503 uint16_t index;
1504
1505 if (memcmp(model, BINZERO, len) != 0) {
1506 strncpy(ha->model_number, model, len);
1507 st = en = ha->model_number;
1508 en += len - 1;
1509 while (en > st) {
1510 if (*en != 0x20 && *en != 0x00)
1511 break;
1512 *en-- = '\0';
1513 }
1514
1515 index = (ha->pdev->subsystem_device & 0xff);
1516 if (ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
1517 index < QLA_MODEL_NAMES)
Joe Carnuccio1ee27142008-07-10 16:55:53 -07001518 strncpy(ha->model_desc,
1519 qla2x00_model_name[index * 2 + 1],
1520 sizeof(ha->model_desc) - 1);
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08001521 } else {
1522 index = (ha->pdev->subsystem_device & 0xff);
1523 if (ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
1524 index < QLA_MODEL_NAMES) {
1525 strcpy(ha->model_number,
1526 qla2x00_model_name[index * 2]);
Joe Carnuccio1ee27142008-07-10 16:55:53 -07001527 strncpy(ha->model_desc,
1528 qla2x00_model_name[index * 2 + 1],
1529 sizeof(ha->model_desc) - 1);
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08001530 } else {
1531 strcpy(ha->model_number, def);
1532 }
1533 }
Joe Carnuccio1ee27142008-07-10 16:55:53 -07001534 if (IS_FWI2_CAPABLE(ha))
1535 qla2xxx_get_vpd_field(ha, "\x82", ha->model_desc,
1536 sizeof(ha->model_desc));
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08001537}
1538
David Miller4e08df32007-04-16 12:37:43 -07001539/* On sparc systems, obtain port and node WWN from firmware
1540 * properties.
1541 */
1542static void qla2xxx_nvram_wwn_from_ofw(scsi_qla_host_t *ha, nvram_t *nv)
1543{
1544#ifdef CONFIG_SPARC
1545 struct pci_dev *pdev = ha->pdev;
David S. Miller15576bc2007-05-08 00:36:49 -07001546 struct device_node *dp = pci_device_to_OF_node(pdev);
1547 const u8 *val;
David Miller4e08df32007-04-16 12:37:43 -07001548 int len;
1549
1550 val = of_get_property(dp, "port-wwn", &len);
1551 if (val && len >= WWN_SIZE)
1552 memcpy(nv->port_name, val, WWN_SIZE);
1553
1554 val = of_get_property(dp, "node-wwn", &len);
1555 if (val && len >= WWN_SIZE)
1556 memcpy(nv->node_name, val, WWN_SIZE);
1557#endif
1558}
1559
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560/*
1561* NVRAM configuration for ISP 2xxx
1562*
1563* Input:
1564* ha = adapter block pointer.
1565*
1566* Output:
1567* initialization control block in response_ring
1568* host adapters parameters in host adapter block
1569*
1570* Returns:
1571* 0 = success.
1572*/
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001573int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574qla2x00_nvram_config(scsi_qla_host_t *ha)
1575{
David Miller4e08df32007-04-16 12:37:43 -07001576 int rval;
Andrew Vasquez01071092005-07-06 10:31:37 -07001577 uint8_t chksum = 0;
1578 uint16_t cnt;
1579 uint8_t *dptr1, *dptr2;
1580 init_cb_t *icb = ha->init_cb;
Seokmann Ju281afe12007-07-26 13:43:34 -07001581 nvram_t *nv = ha->nvram;
1582 uint8_t *ptr = ha->nvram;
Andrew Vasquez3d716442005-07-06 10:30:26 -07001583 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
David Miller4e08df32007-04-16 12:37:43 -07001585 rval = QLA_SUCCESS;
1586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 /* Determine NVRAM starting address. */
Andrew Vasquez01071092005-07-06 10:31:37 -07001588 ha->nvram_size = sizeof(nvram_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 ha->nvram_base = 0;
1590 if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha))
1591 if ((RD_REG_WORD(&reg->ctrl_status) >> 14) == 1)
1592 ha->nvram_base = 0x80;
1593
1594 /* Get NVRAM data and calculate checksum. */
Andrew Vasquezfd34f552007-07-19 15:06:00 -07001595 ha->isp_ops->read_nvram(ha, ptr, ha->nvram_base, ha->nvram_size);
Andrew Vasquez01071092005-07-06 10:31:37 -07001596 for (cnt = 0, chksum = 0; cnt < ha->nvram_size; cnt++)
1597 chksum += *ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
1599 DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
Seokmann Ju281afe12007-07-26 13:43:34 -07001600 DEBUG5(qla2x00_dump_buffer((uint8_t *)nv, ha->nvram_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
1602 /* Bad NVRAM data, set defaults parameters. */
1603 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' ||
1604 nv->id[2] != 'P' || nv->id[3] != ' ' || nv->nvram_version < 1) {
1605 /* Reset NVRAM data. */
1606 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
1607 "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
1608 nv->nvram_version);
David Miller4e08df32007-04-16 12:37:43 -07001609 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
1610 "invalid -- WWPN) defaults.\n");
1611
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -07001612 if (chksum)
1613 qla2xxx_hw_event_log(ha, HW_EVENT_NVRAM_CHKSUM_ERR, 0,
1614 MSW(chksum), LSW(chksum));
1615
David Miller4e08df32007-04-16 12:37:43 -07001616 /*
1617 * Set default initialization control block.
1618 */
1619 memset(nv, 0, ha->nvram_size);
1620 nv->parameter_block_version = ICB_VERSION;
1621
1622 if (IS_QLA23XX(ha)) {
1623 nv->firmware_options[0] = BIT_2 | BIT_1;
1624 nv->firmware_options[1] = BIT_7 | BIT_5;
1625 nv->add_firmware_options[0] = BIT_5;
1626 nv->add_firmware_options[1] = BIT_5 | BIT_4;
1627 nv->frame_payload_size = __constant_cpu_to_le16(2048);
1628 nv->special_options[1] = BIT_7;
1629 } else if (IS_QLA2200(ha)) {
1630 nv->firmware_options[0] = BIT_2 | BIT_1;
1631 nv->firmware_options[1] = BIT_7 | BIT_5;
1632 nv->add_firmware_options[0] = BIT_5;
1633 nv->add_firmware_options[1] = BIT_5 | BIT_4;
1634 nv->frame_payload_size = __constant_cpu_to_le16(1024);
1635 } else if (IS_QLA2100(ha)) {
1636 nv->firmware_options[0] = BIT_3 | BIT_1;
1637 nv->firmware_options[1] = BIT_5;
1638 nv->frame_payload_size = __constant_cpu_to_le16(1024);
1639 }
1640
1641 nv->max_iocb_allocation = __constant_cpu_to_le16(256);
1642 nv->execution_throttle = __constant_cpu_to_le16(16);
1643 nv->retry_count = 8;
1644 nv->retry_delay = 1;
1645
1646 nv->port_name[0] = 33;
1647 nv->port_name[3] = 224;
1648 nv->port_name[4] = 139;
1649
1650 qla2xxx_nvram_wwn_from_ofw(ha, nv);
1651
1652 nv->login_timeout = 4;
1653
1654 /*
1655 * Set default host adapter parameters
1656 */
1657 nv->host_p[1] = BIT_2;
1658 nv->reset_delay = 5;
1659 nv->port_down_retry_count = 8;
1660 nv->max_luns_per_target = __constant_cpu_to_le16(8);
1661 nv->link_down_timeout = 60;
1662
1663 rval = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 }
1665
1666#if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
1667 /*
1668 * The SN2 does not provide BIOS emulation which means you can't change
1669 * potentially bogus BIOS settings. Force the use of default settings
1670 * for link rate and frame size. Hope that the rest of the settings
1671 * are valid.
1672 */
1673 if (ia64_platform_is("sn2")) {
1674 nv->frame_payload_size = __constant_cpu_to_le16(2048);
1675 if (IS_QLA23XX(ha))
1676 nv->special_options[1] = BIT_7;
1677 }
1678#endif
1679
1680 /* Reset Initialization control block */
Andrew Vasquez01071092005-07-06 10:31:37 -07001681 memset(icb, 0, ha->init_cb_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
1683 /*
1684 * Setup driver NVRAM options.
1685 */
1686 nv->firmware_options[0] |= (BIT_6 | BIT_1);
1687 nv->firmware_options[0] &= ~(BIT_5 | BIT_4);
1688 nv->firmware_options[1] |= (BIT_5 | BIT_0);
1689 nv->firmware_options[1] &= ~BIT_4;
1690
1691 if (IS_QLA23XX(ha)) {
1692 nv->firmware_options[0] |= BIT_2;
1693 nv->firmware_options[0] &= ~BIT_3;
Andrew Vasquez01071092005-07-06 10:31:37 -07001694 nv->add_firmware_options[1] |= BIT_5 | BIT_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
1696 if (IS_QLA2300(ha)) {
1697 if (ha->fb_rev == FPM_2310) {
1698 strcpy(ha->model_number, "QLA2310");
1699 } else {
1700 strcpy(ha->model_number, "QLA2300");
1701 }
1702 } else {
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08001703 qla2x00_set_model_info(ha, nv->model_number,
1704 sizeof(nv->model_number), "QLA23xx");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 }
1706 } else if (IS_QLA2200(ha)) {
1707 nv->firmware_options[0] |= BIT_2;
1708 /*
1709 * 'Point-to-point preferred, else loop' is not a safe
1710 * connection mode setting.
1711 */
1712 if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
1713 (BIT_5 | BIT_4)) {
1714 /* Force 'loop preferred, else point-to-point'. */
1715 nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
1716 nv->add_firmware_options[0] |= BIT_5;
1717 }
1718 strcpy(ha->model_number, "QLA22xx");
1719 } else /*if (IS_QLA2100(ha))*/ {
1720 strcpy(ha->model_number, "QLA2100");
1721 }
1722
1723 /*
1724 * Copy over NVRAM RISC parameter block to initialization control block.
1725 */
1726 dptr1 = (uint8_t *)icb;
1727 dptr2 = (uint8_t *)&nv->parameter_block_version;
1728 cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
1729 while (cnt--)
1730 *dptr1++ = *dptr2++;
1731
1732 /* Copy 2nd half. */
1733 dptr1 = (uint8_t *)icb->add_firmware_options;
1734 cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
1735 while (cnt--)
1736 *dptr1++ = *dptr2++;
1737
Andrew Vasquez5341e862006-05-17 15:09:16 -07001738 /* Use alternate WWN? */
1739 if (nv->host_p[1] & BIT_7) {
1740 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
1741 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
1742 }
1743
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 /* Prepare nodename */
1745 if ((icb->firmware_options[1] & BIT_6) == 0) {
1746 /*
1747 * Firmware will apply the following mask if the nodename was
1748 * not provided.
1749 */
1750 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
1751 icb->node_name[0] &= 0xF0;
1752 }
1753
1754 /*
1755 * Set host adapter parameters.
1756 */
Andrew Vasquez01819442006-06-23 16:11:10 -07001757 if (nv->host_p[0] & BIT_7)
Andrew Vasquez11010fe2006-10-06 09:54:59 -07001758 ql2xextended_error_logging = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
1760 /* Always load RISC code on non ISP2[12]00 chips. */
1761 if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
1762 ha->flags.disable_risc_code_load = 0;
1763 ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
1764 ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
1765 ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
Andrew Vasquez06c22bd2005-08-26 19:09:00 -07001766 ha->flags.enable_led_scheme = (nv->special_options[1] & BIT_4) ? 1 : 0;
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07001767 ha->flags.disable_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
1769 ha->operating_mode =
1770 (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
1771
1772 memcpy(ha->fw_seriallink_options, nv->seriallink_options,
1773 sizeof(ha->fw_seriallink_options));
1774
1775 /* save HBA serial number */
1776 ha->serial0 = icb->port_name[5];
1777 ha->serial1 = icb->port_name[6];
1778 ha->serial2 = icb->port_name[7];
Andrew Vasquez3d716442005-07-06 10:30:26 -07001779 ha->node_name = icb->node_name;
1780 ha->port_name = icb->port_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
1782 icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
1783
1784 ha->retry_count = nv->retry_count;
1785
1786 /* Set minimum login_timeout to 4 seconds. */
1787 if (nv->login_timeout < ql2xlogintimeout)
1788 nv->login_timeout = ql2xlogintimeout;
1789 if (nv->login_timeout < 4)
1790 nv->login_timeout = 4;
1791 ha->login_timeout = nv->login_timeout;
1792 icb->login_timeout = nv->login_timeout;
1793
Andrew Vasquez00a537b2008-02-28 14:06:11 -08001794 /* Set minimum RATOV to 100 tenths of a second. */
1795 ha->r_a_tov = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 ha->loop_reset_delay = nv->reset_delay;
1798
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 /* Link Down Timeout = 0:
1800 *
1801 * When Port Down timer expires we will start returning
1802 * I/O's to OS with "DID_NO_CONNECT".
1803 *
1804 * Link Down Timeout != 0:
1805 *
1806 * The driver waits for the link to come up after link down
1807 * before returning I/Os to OS with "DID_NO_CONNECT".
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001808 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 if (nv->link_down_timeout == 0) {
1810 ha->loop_down_abort_time =
Andrew Vasquez 354d6b22005-04-23 02:47:27 -04001811 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 } else {
1813 ha->link_down_timeout = nv->link_down_timeout;
1814 ha->loop_down_abort_time =
1815 (LOOP_DOWN_TIME - ha->link_down_timeout);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 /*
1819 * Need enough time to try and get the port back.
1820 */
1821 ha->port_down_retry_count = nv->port_down_retry_count;
1822 if (qlport_down_retry)
1823 ha->port_down_retry_count = qlport_down_retry;
1824 /* Set login_retry_count */
1825 ha->login_retry_count = nv->retry_count;
1826 if (ha->port_down_retry_count == nv->port_down_retry_count &&
1827 ha->port_down_retry_count > 3)
1828 ha->login_retry_count = ha->port_down_retry_count;
1829 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
1830 ha->login_retry_count = ha->port_down_retry_count;
1831 if (ql2xloginretrycount)
1832 ha->login_retry_count = ql2xloginretrycount;
1833
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 icb->lun_enables = __constant_cpu_to_le16(0);
1835 icb->command_resource_count = 0;
1836 icb->immediate_notify_resource_count = 0;
1837 icb->timeout = __constant_cpu_to_le16(0);
1838
1839 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
1840 /* Enable RIO */
1841 icb->firmware_options[0] &= ~BIT_3;
1842 icb->add_firmware_options[0] &=
1843 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
1844 icb->add_firmware_options[0] |= BIT_2;
1845 icb->response_accumulation_timer = 3;
1846 icb->interrupt_delay_timer = 5;
1847
1848 ha->flags.process_response_queue = 1;
1849 } else {
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001850 /* Enable ZIO. */
1851 if (!ha->flags.init_done) {
1852 ha->zio_mode = icb->add_firmware_options[0] &
1853 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
1854 ha->zio_timer = icb->interrupt_delay_timer ?
1855 icb->interrupt_delay_timer: 2;
1856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 icb->add_firmware_options[0] &=
1858 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001859 ha->flags.process_response_queue = 0;
1860 if (ha->zio_mode != QLA_ZIO_DISABLED) {
andrew.vasquez@qlogic.com4a59f712006-03-09 14:27:39 -08001861 ha->zio_mode = QLA_ZIO_MODE_6;
1862
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001863 DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer "
1864 "delay (%d us).\n", ha->host_no, ha->zio_mode,
1865 ha->zio_timer * 100));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 qla_printk(KERN_INFO, ha,
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001867 "ZIO mode %d enabled; timer delay (%d us).\n",
1868 ha->zio_mode, ha->zio_timer * 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001870 icb->add_firmware_options[0] |= (uint8_t)ha->zio_mode;
1871 icb->interrupt_delay_timer = (uint8_t)ha->zio_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 ha->flags.process_response_queue = 1;
1873 }
1874 }
1875
David Miller4e08df32007-04-16 12:37:43 -07001876 if (rval) {
1877 DEBUG2_3(printk(KERN_WARNING
1878 "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
1879 }
1880 return (rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881}
1882
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001883static void
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001884qla2x00_rport_del(void *data)
1885{
1886 fc_port_t *fcport = data;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08001887 struct fc_rport *rport;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001888
Seokmann Ju5f3a9a22008-07-10 16:55:47 -07001889 spin_lock_irq(fcport->ha->host->host_lock);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08001890 rport = fcport->drport;
1891 fcport->drport = NULL;
Seokmann Ju5f3a9a22008-07-10 16:55:47 -07001892 spin_unlock_irq(fcport->ha->host->host_lock);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08001893 if (rport)
1894 fc_remote_port_delete(rport);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001895}
1896
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897/**
1898 * qla2x00_alloc_fcport() - Allocate a generic fcport.
1899 * @ha: HA context
1900 * @flags: allocation flags
1901 *
1902 * Returns a pointer to the allocated fcport, or NULL, if none available.
1903 */
Adrian Bunk413975a2006-06-30 02:33:06 -07001904static fc_port_t *
Al Viroc53033f2005-10-21 03:22:08 -04001905qla2x00_alloc_fcport(scsi_qla_host_t *ha, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906{
1907 fc_port_t *fcport;
1908
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02001909 fcport = kzalloc(sizeof(fc_port_t), flags);
1910 if (!fcport)
1911 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
1913 /* Setup fcport template structure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 fcport->ha = ha;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001915 fcport->vp_idx = ha->vp_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 fcport->port_type = FCT_UNKNOWN;
1917 fcport->loop_id = FC_NO_LOOP_ID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 atomic_set(&fcport->state, FCS_UNCONFIGURED);
1919 fcport->flags = FCF_RLC_SUPPORT;
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07001920 fcport->supported_classes = FC_COS_UNSPECIFIED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02001922 return fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923}
1924
1925/*
1926 * qla2x00_configure_loop
1927 * Updates Fibre Channel Device Database with what is actually on loop.
1928 *
1929 * Input:
1930 * ha = adapter block pointer.
1931 *
1932 * Returns:
1933 * 0 = success.
1934 * 1 = error.
1935 * 2 = database was full and device was not configured.
1936 */
1937static int
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001938qla2x00_configure_loop(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939{
1940 int rval;
1941 unsigned long flags, save_flags;
1942
1943 rval = QLA_SUCCESS;
1944
1945 /* Get Initiator ID */
1946 if (test_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags)) {
1947 rval = qla2x00_configure_hba(ha);
1948 if (rval != QLA_SUCCESS) {
1949 DEBUG(printk("scsi(%ld): Unable to configure HBA.\n",
1950 ha->host_no));
1951 return (rval);
1952 }
1953 }
1954
1955 save_flags = flags = ha->dpc_flags;
1956 DEBUG(printk("scsi(%ld): Configure loop -- dpc flags =0x%lx\n",
1957 ha->host_no, flags));
1958
1959 /*
1960 * If we have both an RSCN and PORT UPDATE pending then handle them
1961 * both at the same time.
1962 */
1963 clear_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1964 clear_bit(RSCN_UPDATE, &ha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
1966 /* Determine what we need to do */
1967 if (ha->current_topology == ISP_CFG_FL &&
1968 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1969
1970 ha->flags.rscn_queue_overflow = 1;
1971 set_bit(RSCN_UPDATE, &flags);
1972
1973 } else if (ha->current_topology == ISP_CFG_F &&
1974 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1975
1976 ha->flags.rscn_queue_overflow = 1;
1977 set_bit(RSCN_UPDATE, &flags);
1978 clear_bit(LOCAL_LOOP_UPDATE, &flags);
1979
Andrew Vasquez21333b42006-05-17 15:09:56 -07001980 } else if (ha->current_topology == ISP_CFG_N) {
1981 clear_bit(RSCN_UPDATE, &flags);
1982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 } else if (!ha->flags.online ||
1984 (test_bit(ABORT_ISP_ACTIVE, &flags))) {
1985
1986 ha->flags.rscn_queue_overflow = 1;
1987 set_bit(RSCN_UPDATE, &flags);
1988 set_bit(LOCAL_LOOP_UPDATE, &flags);
1989 }
1990
1991 if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
1992 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1993 rval = QLA_FUNCTION_FAILED;
1994 } else {
1995 rval = qla2x00_configure_local_loop(ha);
1996 }
1997 }
1998
1999 if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
Andrew Vasquez23443b12005-12-06 10:57:06 -08002000 if (LOOP_TRANSITION(ha)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 rval = QLA_FUNCTION_FAILED;
2002 } else {
2003 rval = qla2x00_configure_fabric(ha);
2004 }
2005 }
2006
2007 if (rval == QLA_SUCCESS) {
2008 if (atomic_read(&ha->loop_down_timer) ||
2009 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
2010 rval = QLA_FUNCTION_FAILED;
2011 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 atomic_set(&ha->loop_state, LOOP_READY);
2013
2014 DEBUG(printk("scsi(%ld): LOOP READY\n", ha->host_no));
2015 }
2016 }
2017
2018 if (rval) {
2019 DEBUG2_3(printk("%s(%ld): *** FAILED ***\n",
2020 __func__, ha->host_no));
2021 } else {
2022 DEBUG3(printk("%s: exiting normally\n", __func__));
2023 }
2024
Bjorn Helgaascc3ef7b2008-09-11 21:22:51 -07002025 /* Restore state if a resync event occurred during processing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
2027 if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
2028 set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
Seokmann Ju5de1f702008-07-10 16:55:58 -07002029 if (test_bit(RSCN_UPDATE, &save_flags)) {
2030 ha->flags.rscn_queue_overflow = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 set_bit(RSCN_UPDATE, &ha->dpc_flags);
Seokmann Ju5de1f702008-07-10 16:55:58 -07002032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 }
2034
2035 return (rval);
2036}
2037
2038
2039
2040/*
2041 * qla2x00_configure_local_loop
2042 * Updates Fibre Channel Device Database with local loop devices.
2043 *
2044 * Input:
2045 * ha = adapter block pointer.
2046 *
2047 * Returns:
2048 * 0 = success.
2049 */
2050static int
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002051qla2x00_configure_local_loop(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052{
2053 int rval, rval2;
2054 int found_devs;
2055 int found;
2056 fc_port_t *fcport, *new_fcport;
2057
2058 uint16_t index;
2059 uint16_t entries;
2060 char *id_iter;
2061 uint16_t loop_id;
2062 uint8_t domain, area, al_pa;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002063 scsi_qla_host_t *pha = to_qla_parent(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
2065 found_devs = 0;
2066 new_fcport = NULL;
2067 entries = MAX_FIBRE_DEVICES;
2068
2069 DEBUG3(printk("scsi(%ld): Getting FCAL position map\n", ha->host_no));
2070 DEBUG3(qla2x00_get_fcal_position_map(ha, NULL));
2071
2072 /* Get list of logged in devices. */
2073 memset(ha->gid_list, 0, GID_LIST_SIZE);
2074 rval = qla2x00_get_id_list(ha, ha->gid_list, ha->gid_list_dma,
2075 &entries);
2076 if (rval != QLA_SUCCESS)
2077 goto cleanup_allocation;
2078
2079 DEBUG3(printk("scsi(%ld): Entries in ID list (%d)\n",
2080 ha->host_no, entries));
2081 DEBUG3(qla2x00_dump_buffer((uint8_t *)ha->gid_list,
2082 entries * sizeof(struct gid_list_info)));
2083
2084 /* Allocate temporary fcport for any new fcports discovered. */
2085 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2086 if (new_fcport == NULL) {
2087 rval = QLA_MEMORY_ALLOC_FAILED;
2088 goto cleanup_allocation;
2089 }
2090 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
2091
2092 /*
2093 * Mark local devices that were present with FCF_DEVICE_LOST for now.
2094 */
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002095 list_for_each_entry(fcport, &pha->fcports, list) {
2096 if (fcport->vp_idx != ha->vp_idx)
2097 continue;
2098
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 if (atomic_read(&fcport->state) == FCS_ONLINE &&
2100 fcport->port_type != FCT_BROADCAST &&
2101 (fcport->flags & FCF_FABRIC_DEVICE) == 0) {
2102
2103 DEBUG(printk("scsi(%ld): Marking port lost, "
2104 "loop_id=0x%04x\n",
2105 ha->host_no, fcport->loop_id));
2106
2107 atomic_set(&fcport->state, FCS_DEVICE_LOST);
2108 fcport->flags &= ~FCF_FARP_DONE;
2109 }
2110 }
2111
2112 /* Add devices to port list. */
2113 id_iter = (char *)ha->gid_list;
2114 for (index = 0; index < entries; index++) {
2115 domain = ((struct gid_list_info *)id_iter)->domain;
2116 area = ((struct gid_list_info *)id_iter)->area;
2117 al_pa = ((struct gid_list_info *)id_iter)->al_pa;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002118 if (IS_QLA2100(ha) || IS_QLA2200(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 loop_id = (uint16_t)
2120 ((struct gid_list_info *)id_iter)->loop_id_2100;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002121 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 loop_id = le16_to_cpu(
2123 ((struct gid_list_info *)id_iter)->loop_id);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002124 id_iter += ha->gid_list_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
2126 /* Bypass reserved domain fields. */
2127 if ((domain & 0xf0) == 0xf0)
2128 continue;
2129
2130 /* Bypass if not same domain and area of adapter. */
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07002131 if (area && domain &&
2132 (area != ha->d_id.b.area || domain != ha->d_id.b.domain))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 continue;
2134
2135 /* Bypass invalid local loop ID. */
2136 if (loop_id > LAST_LOCAL_LOOP_ID)
2137 continue;
2138
2139 /* Fill in member data. */
2140 new_fcport->d_id.b.domain = domain;
2141 new_fcport->d_id.b.area = area;
2142 new_fcport->d_id.b.al_pa = al_pa;
2143 new_fcport->loop_id = loop_id;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002144 new_fcport->vp_idx = ha->vp_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 rval2 = qla2x00_get_port_database(ha, new_fcport, 0);
2146 if (rval2 != QLA_SUCCESS) {
2147 DEBUG2(printk("scsi(%ld): Failed to retrieve fcport "
2148 "information -- get_port_database=%x, "
2149 "loop_id=0x%04x\n",
2150 ha->host_no, rval2, new_fcport->loop_id));
andrew.vasquez@qlogic.comc9d02ac2006-01-13 17:05:26 -08002151 DEBUG2(printk("scsi(%ld): Scheduling resync...\n",
2152 ha->host_no));
2153 set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 continue;
2155 }
2156
2157 /* Check for matching device in port list. */
2158 found = 0;
2159 fcport = NULL;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002160 list_for_each_entry(fcport, &pha->fcports, list) {
2161 if (fcport->vp_idx != ha->vp_idx)
2162 continue;
2163
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 if (memcmp(new_fcport->port_name, fcport->port_name,
2165 WWN_SIZE))
2166 continue;
2167
2168 fcport->flags &= ~(FCF_FABRIC_DEVICE |
2169 FCF_PERSISTENT_BOUND);
2170 fcport->loop_id = new_fcport->loop_id;
2171 fcport->port_type = new_fcport->port_type;
2172 fcport->d_id.b24 = new_fcport->d_id.b24;
2173 memcpy(fcport->node_name, new_fcport->node_name,
2174 WWN_SIZE);
2175
2176 found++;
2177 break;
2178 }
2179
2180 if (!found) {
2181 /* New device, add to fcports list. */
2182 new_fcport->flags &= ~FCF_PERSISTENT_BOUND;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002183 if (ha->parent) {
2184 new_fcport->ha = ha;
2185 new_fcport->vp_idx = ha->vp_idx;
2186 list_add_tail(&new_fcport->vp_fcport,
2187 &ha->vp_fcports);
2188 }
2189 list_add_tail(&new_fcport->list, &pha->fcports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
2191 /* Allocate a new replacement fcport. */
2192 fcport = new_fcport;
2193 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2194 if (new_fcport == NULL) {
2195 rval = QLA_MEMORY_ALLOC_FAILED;
2196 goto cleanup_allocation;
2197 }
2198 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
2199 }
2200
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002201 /* Base iIDMA settings on HBA port speed. */
Andrew Vasqueza3cbdfa2007-08-13 10:13:18 -07002202 fcport->fp_speed = ha->link_data_rate;
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002203
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 qla2x00_update_fcport(ha, fcport);
2205
2206 found_devs++;
2207 }
2208
2209cleanup_allocation:
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002210 kfree(new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
2212 if (rval != QLA_SUCCESS) {
2213 DEBUG2(printk("scsi(%ld): Configure local loop error exit: "
2214 "rval=%x\n", ha->host_no, rval));
2215 }
2216
2217 if (found_devs) {
2218 ha->device_flags |= DFLG_LOCAL_DEVICES;
2219 ha->device_flags &= ~DFLG_RETRY_LOCAL_DEVICES;
2220 }
2221
2222 return (rval);
2223}
2224
2225static void
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002226qla2x00_iidma_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
2227{
2228#define LS_UNKNOWN 2
Andrew Vasqueza3cbdfa2007-08-13 10:13:18 -07002229 static char *link_speeds[5] = { "1", "2", "?", "4", "8" };
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002230 int rval;
Andrew Vasqueza3cbdfa2007-08-13 10:13:18 -07002231 uint16_t mb[6];
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002232
Andrew Vasquezc76f2c02007-07-19 15:05:57 -07002233 if (!IS_IIDMA_CAPABLE(ha))
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002234 return;
2235
Andrew Vasquez39bd9622007-09-20 14:07:34 -07002236 if (fcport->fp_speed == PORT_SPEED_UNKNOWN ||
2237 fcport->fp_speed > ha->link_data_rate)
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002238 return;
2239
Andrew Vasqueza3cbdfa2007-08-13 10:13:18 -07002240 rval = qla2x00_set_idma_speed(ha, fcport->loop_id, fcport->fp_speed,
2241 mb);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002242 if (rval != QLA_SUCCESS) {
2243 DEBUG2(printk("scsi(%ld): Unable to adjust iIDMA "
2244 "%02x%02x%02x%02x%02x%02x%02x%02x -- %04x %x %04x %04x.\n",
2245 ha->host_no, fcport->port_name[0], fcport->port_name[1],
2246 fcport->port_name[2], fcport->port_name[3],
2247 fcport->port_name[4], fcport->port_name[5],
2248 fcport->port_name[6], fcport->port_name[7], rval,
Andrew Vasqueza3cbdfa2007-08-13 10:13:18 -07002249 fcport->fp_speed, mb[0], mb[1]));
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002250 } else {
2251 DEBUG2(qla_printk(KERN_INFO, ha,
2252 "iIDMA adjusted to %s GB/s on "
2253 "%02x%02x%02x%02x%02x%02x%02x%02x.\n",
Andrew Vasqueza3cbdfa2007-08-13 10:13:18 -07002254 link_speeds[fcport->fp_speed], fcport->port_name[0],
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002255 fcport->port_name[1], fcport->port_name[2],
2256 fcport->port_name[3], fcport->port_name[4],
2257 fcport->port_name[5], fcport->port_name[6],
2258 fcport->port_name[7]));
2259 }
2260}
2261
Adrian Bunk23be3312006-11-24 02:46:01 +01002262static void
8482e112005-04-17 15:04:54 -05002263qla2x00_reg_remote_port(scsi_qla_host_t *ha, fc_port_t *fcport)
2264{
2265 struct fc_rport_identifiers rport_ids;
bdf79622005-04-17 15:06:53 -05002266 struct fc_rport *rport;
8482e112005-04-17 15:04:54 -05002267
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002268 if (fcport->drport)
2269 qla2x00_rport_del(fcport);
8482e112005-04-17 15:04:54 -05002270
Andrew Vasquezf8b02a82005-08-31 15:21:20 -07002271 rport_ids.node_name = wwn_to_u64(fcport->node_name);
2272 rport_ids.port_name = wwn_to_u64(fcport->port_name);
8482e112005-04-17 15:04:54 -05002273 rport_ids.port_id = fcport->d_id.b.domain << 16 |
2274 fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
2275 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
Seokmann Ju5f3a9a22008-07-10 16:55:47 -07002276 fcport->rport = rport = fc_remote_port_add(ha->host, 0, &rport_ids);
Andrew Vasquez77d74142005-07-08 18:00:36 -07002277 if (!rport) {
2278 qla_printk(KERN_WARNING, ha,
2279 "Unable to allocate fc remote port!\n");
2280 return;
2281 }
Seokmann Ju5f3a9a22008-07-10 16:55:47 -07002282 spin_lock_irq(fcport->ha->host->host_lock);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002283 *((fc_port_t **)rport->dd_data) = fcport;
Seokmann Ju5f3a9a22008-07-10 16:55:47 -07002284 spin_unlock_irq(fcport->ha->host->host_lock);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002285
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07002286 rport->supported_classes = fcport->supported_classes;
Andrew Vasquez77d74142005-07-08 18:00:36 -07002287
2288 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
8482e112005-04-17 15:04:54 -05002289 if (fcport->port_type == FCT_INITIATOR)
2290 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2291 if (fcport->port_type == FCT_TARGET)
2292 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
Andrew Vasquez77d74142005-07-08 18:00:36 -07002293 fc_remote_port_rolechg(rport, rport_ids.roles);
8482e112005-04-17 15:04:54 -05002294}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
2296/*
Adrian Bunk23be3312006-11-24 02:46:01 +01002297 * qla2x00_update_fcport
2298 * Updates device on list.
2299 *
2300 * Input:
2301 * ha = adapter block pointer.
2302 * fcport = port structure pointer.
2303 *
2304 * Return:
2305 * 0 - Success
2306 * BIT_0 - error
2307 *
2308 * Context:
2309 * Kernel context.
2310 */
2311void
2312qla2x00_update_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
2313{
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002314 scsi_qla_host_t *pha = to_qla_parent(ha);
2315
Adrian Bunk23be3312006-11-24 02:46:01 +01002316 fcport->ha = ha;
2317 fcport->login_retry = 0;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002318 fcport->port_login_retry_count = pha->port_down_retry_count *
Adrian Bunk23be3312006-11-24 02:46:01 +01002319 PORT_RETRY_TIME;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002320 atomic_set(&fcport->port_down_timer, pha->port_down_retry_count *
Adrian Bunk23be3312006-11-24 02:46:01 +01002321 PORT_RETRY_TIME);
2322 fcport->flags &= ~FCF_LOGIN_NEEDED;
2323
2324 qla2x00_iidma_fcport(ha, fcport);
2325
2326 atomic_set(&fcport->state, FCS_ONLINE);
2327
2328 qla2x00_reg_remote_port(ha, fcport);
2329}
2330
2331/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 * qla2x00_configure_fabric
2333 * Setup SNS devices with loop ID's.
2334 *
2335 * Input:
2336 * ha = adapter block pointer.
2337 *
2338 * Returns:
2339 * 0 = success.
2340 * BIT_0 = error
2341 */
2342static int
2343qla2x00_configure_fabric(scsi_qla_host_t *ha)
2344{
2345 int rval, rval2;
2346 fc_port_t *fcport, *fcptemp;
2347 uint16_t next_loopid;
2348 uint16_t mb[MAILBOX_REGISTER_COUNT];
Andrew Vasquez01071092005-07-06 10:31:37 -07002349 uint16_t loop_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 LIST_HEAD(new_fcports);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002351 scsi_qla_host_t *pha = to_qla_parent(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
2353 /* If FL port exists, then SNS is present */
Andrew Vasqueze4289242007-07-19 15:05:56 -07002354 if (IS_FWI2_CAPABLE(ha))
Andrew Vasquez01071092005-07-06 10:31:37 -07002355 loop_id = NPH_F_PORT;
2356 else
2357 loop_id = SNS_FL_PORT;
Andrew Vasquez90991c82006-10-02 12:00:46 -07002358 rval = qla2x00_get_port_name(ha, loop_id, ha->fabric_node_name, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 if (rval != QLA_SUCCESS) {
2360 DEBUG2(printk("scsi(%ld): MBC_GET_PORT_NAME Failed, No FL "
2361 "Port\n", ha->host_no));
2362
2363 ha->device_flags &= ~SWITCH_FOUND;
2364 return (QLA_SUCCESS);
2365 }
Andrew Vasquez90991c82006-10-02 12:00:46 -07002366 ha->device_flags |= SWITCH_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
2368 /* Mark devices that need re-synchronization. */
2369 rval2 = qla2x00_device_resync(ha);
2370 if (rval2 == QLA_RSCNS_HANDLED) {
2371 /* No point doing the scan, just continue. */
2372 return (QLA_SUCCESS);
2373 }
2374 do {
Andrew Vasquezcca53352005-08-26 19:08:30 -07002375 /* FDMI support. */
2376 if (ql2xfdmienable &&
2377 test_and_clear_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags))
2378 qla2x00_fdmi_register(ha);
2379
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 /* Ensure we are logged into the SNS. */
Andrew Vasqueze4289242007-07-19 15:05:56 -07002381 if (IS_FWI2_CAPABLE(ha))
Andrew Vasquez01071092005-07-06 10:31:37 -07002382 loop_id = NPH_SNS;
2383 else
2384 loop_id = SIMPLE_NAME_SERVER;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07002385 ha->isp_ops->fabric_login(ha, loop_id, 0xff, 0xff,
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002386 0xfc, mb, BIT_1 | BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 if (mb[0] != MBS_COMMAND_COMPLETE) {
2388 DEBUG2(qla_printk(KERN_INFO, ha,
2389 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
Andrew Vasquez01071092005-07-06 10:31:37 -07002390 "mb[2]=%x mb[6]=%x mb[7]=%x\n", loop_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 mb[0], mb[1], mb[2], mb[6], mb[7]));
2392 return (QLA_SUCCESS);
2393 }
2394
2395 if (test_and_clear_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags)) {
2396 if (qla2x00_rft_id(ha)) {
2397 /* EMPTY */
2398 DEBUG2(printk("scsi(%ld): Register FC-4 "
2399 "TYPE failed.\n", ha->host_no));
2400 }
2401 if (qla2x00_rff_id(ha)) {
2402 /* EMPTY */
2403 DEBUG2(printk("scsi(%ld): Register FC-4 "
2404 "Features failed.\n", ha->host_no));
2405 }
2406 if (qla2x00_rnn_id(ha)) {
2407 /* EMPTY */
2408 DEBUG2(printk("scsi(%ld): Register Node Name "
2409 "failed.\n", ha->host_no));
2410 } else if (qla2x00_rsnn_nn(ha)) {
2411 /* EMPTY */
2412 DEBUG2(printk("scsi(%ld): Register Symbolic "
2413 "Node Name failed.\n", ha->host_no));
2414 }
2415 }
2416
2417 rval = qla2x00_find_all_fabric_devs(ha, &new_fcports);
2418 if (rval != QLA_SUCCESS)
2419 break;
2420
2421 /*
2422 * Logout all previous fabric devices marked lost, except
2423 * tape devices.
2424 */
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002425 list_for_each_entry(fcport, &pha->fcports, list) {
2426 if (fcport->vp_idx !=ha->vp_idx)
2427 continue;
2428
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2430 break;
2431
2432 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0)
2433 continue;
2434
2435 if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
2436 qla2x00_mark_device_lost(ha, fcport,
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002437 ql2xplogiabsentdevice, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 if (fcport->loop_id != FC_NO_LOOP_ID &&
2439 (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2440 fcport->port_type != FCT_INITIATOR &&
2441 fcport->port_type != FCT_BROADCAST) {
Andrew Vasquezfd34f552007-07-19 15:06:00 -07002442 ha->isp_ops->fabric_logout(ha,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07002443 fcport->loop_id,
2444 fcport->d_id.b.domain,
2445 fcport->d_id.b.area,
2446 fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 fcport->loop_id = FC_NO_LOOP_ID;
2448 }
2449 }
2450 }
2451
2452 /* Starting free loop ID. */
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002453 next_loopid = pha->min_external_loopid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
2455 /*
2456 * Scan through our port list and login entries that need to be
2457 * logged in.
2458 */
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002459 list_for_each_entry(fcport, &pha->fcports, list) {
2460 if (fcport->vp_idx != ha->vp_idx)
2461 continue;
2462
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 if (atomic_read(&ha->loop_down_timer) ||
2464 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2465 break;
2466
2467 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2468 (fcport->flags & FCF_LOGIN_NEEDED) == 0)
2469 continue;
2470
2471 if (fcport->loop_id == FC_NO_LOOP_ID) {
2472 fcport->loop_id = next_loopid;
Seokmann Jud4486fd2008-04-03 13:13:28 -07002473 rval = qla2x00_find_new_loop_id(
2474 to_qla_parent(ha), fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 if (rval != QLA_SUCCESS) {
2476 /* Ran out of IDs to use */
2477 break;
2478 }
2479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 /* Login and update database */
2481 qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
2482 }
2483
2484 /* Exit if out of loop IDs. */
2485 if (rval != QLA_SUCCESS) {
2486 break;
2487 }
2488
2489 /*
2490 * Login and add the new devices to our port list.
2491 */
2492 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2493 if (atomic_read(&ha->loop_down_timer) ||
2494 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2495 break;
2496
2497 /* Find a new loop ID to use. */
2498 fcport->loop_id = next_loopid;
Seokmann Jud4486fd2008-04-03 13:13:28 -07002499 rval = qla2x00_find_new_loop_id(to_qla_parent(ha),
2500 fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 if (rval != QLA_SUCCESS) {
2502 /* Ran out of IDs to use */
2503 break;
2504 }
2505
bdf79622005-04-17 15:06:53 -05002506 /* Login and update database */
2507 qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002508
2509 if (ha->parent) {
2510 fcport->ha = ha;
2511 fcport->vp_idx = ha->vp_idx;
2512 list_add_tail(&fcport->vp_fcport,
2513 &ha->vp_fcports);
2514 list_move_tail(&fcport->list,
2515 &ha->parent->fcports);
2516 } else
2517 list_move_tail(&fcport->list, &ha->fcports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 }
2519 } while (0);
2520
2521 /* Free all new device structures not processed. */
2522 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2523 list_del(&fcport->list);
2524 kfree(fcport);
2525 }
2526
2527 if (rval) {
2528 DEBUG2(printk("scsi(%ld): Configure fabric error exit: "
2529 "rval=%d\n", ha->host_no, rval));
2530 }
2531
2532 return (rval);
2533}
2534
2535
2536/*
2537 * qla2x00_find_all_fabric_devs
2538 *
2539 * Input:
2540 * ha = adapter block pointer.
2541 * dev = database device entry pointer.
2542 *
2543 * Returns:
2544 * 0 = success.
2545 *
2546 * Context:
2547 * Kernel context.
2548 */
2549static int
2550qla2x00_find_all_fabric_devs(scsi_qla_host_t *ha, struct list_head *new_fcports)
2551{
2552 int rval;
2553 uint16_t loop_id;
2554 fc_port_t *fcport, *new_fcport, *fcptemp;
2555 int found;
2556
2557 sw_info_t *swl;
2558 int swl_idx;
2559 int first_dev, last_dev;
2560 port_id_t wrap, nxt_d_id;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002561 int vp_index;
2562 int empty_vp_index;
2563 int found_vp;
2564 scsi_qla_host_t *vha;
2565 scsi_qla_host_t *pha = to_qla_parent(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
2567 rval = QLA_SUCCESS;
2568
2569 /* Try GID_PT to get device list, else GAN. */
Andrew Vasquez4b892582008-09-11 21:22:48 -07002570 swl = kcalloc(MAX_FIBRE_DEVICES, sizeof(sw_info_t), GFP_KERNEL);
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02002571 if (!swl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 /*EMPTY*/
2573 DEBUG2(printk("scsi(%ld): GID_PT allocations failed, fallback "
2574 "on GA_NXT\n", ha->host_no));
2575 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 if (qla2x00_gid_pt(ha, swl) != QLA_SUCCESS) {
2577 kfree(swl);
2578 swl = NULL;
2579 } else if (qla2x00_gpn_id(ha, swl) != QLA_SUCCESS) {
2580 kfree(swl);
2581 swl = NULL;
2582 } else if (qla2x00_gnn_id(ha, swl) != QLA_SUCCESS) {
2583 kfree(swl);
2584 swl = NULL;
Andrew Vasqueze5896bd2008-07-10 16:55:52 -07002585 } else if (ql2xiidmaenable &&
2586 qla2x00_gfpn_id(ha, swl) == QLA_SUCCESS) {
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002587 qla2x00_gpsc(ha, swl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 }
2589 }
2590 swl_idx = 0;
2591
2592 /* Allocate temporary fcport for any new fcports discovered. */
2593 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2594 if (new_fcport == NULL) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002595 kfree(swl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 return (QLA_MEMORY_ALLOC_FAILED);
2597 }
2598 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002599 new_fcport->vp_idx = ha->vp_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 /* Set start port ID scan at adapter ID. */
2601 first_dev = 1;
2602 last_dev = 0;
2603
2604 /* Starting free loop ID. */
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002605 loop_id = pha->min_external_loopid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 for (; loop_id <= ha->last_loop_id; loop_id++) {
Andrew Vasquez3d716442005-07-06 10:30:26 -07002607 if (qla2x00_is_reserved_id(ha, loop_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 continue;
2609
Andrew Vasquez23443b12005-12-06 10:57:06 -08002610 if (atomic_read(&ha->loop_down_timer) || LOOP_TRANSITION(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 break;
2612
2613 if (swl != NULL) {
2614 if (last_dev) {
2615 wrap.b24 = new_fcport->d_id.b24;
2616 } else {
2617 new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
2618 memcpy(new_fcport->node_name,
2619 swl[swl_idx].node_name, WWN_SIZE);
2620 memcpy(new_fcport->port_name,
2621 swl[swl_idx].port_name, WWN_SIZE);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002622 memcpy(new_fcport->fabric_port_name,
2623 swl[swl_idx].fabric_port_name, WWN_SIZE);
2624 new_fcport->fp_speed = swl[swl_idx].fp_speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
2626 if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
2627 last_dev = 1;
2628 }
2629 swl_idx++;
2630 }
2631 } else {
2632 /* Send GA_NXT to the switch */
2633 rval = qla2x00_ga_nxt(ha, new_fcport);
2634 if (rval != QLA_SUCCESS) {
2635 qla_printk(KERN_WARNING, ha,
2636 "SNS scan failed -- assuming zero-entry "
2637 "result...\n");
2638 list_for_each_entry_safe(fcport, fcptemp,
2639 new_fcports, list) {
2640 list_del(&fcport->list);
2641 kfree(fcport);
2642 }
2643 rval = QLA_SUCCESS;
2644 break;
2645 }
2646 }
2647
2648 /* If wrap on switch device list, exit. */
2649 if (first_dev) {
2650 wrap.b24 = new_fcport->d_id.b24;
2651 first_dev = 0;
2652 } else if (new_fcport->d_id.b24 == wrap.b24) {
2653 DEBUG2(printk("scsi(%ld): device wrap (%02x%02x%02x)\n",
2654 ha->host_no, new_fcport->d_id.b.domain,
2655 new_fcport->d_id.b.area, new_fcport->d_id.b.al_pa));
2656 break;
2657 }
2658
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002659 /* Bypass if same physical adapter. */
2660 if (new_fcport->d_id.b24 == pha->d_id.b24)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 continue;
2662
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002663 /* Bypass virtual ports of the same host. */
2664 if (pha->num_vhosts) {
Andrew Vasquezeb66dc62007-11-12 10:30:58 -08002665 for_each_mapped_vp_idx(pha, vp_index) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002666 empty_vp_index = 1;
2667 found_vp = 0;
2668 list_for_each_entry(vha, &pha->vp_list,
2669 vp_list) {
2670 if (vp_index == vha->vp_idx) {
2671 empty_vp_index = 0;
2672 found_vp = 1;
2673 break;
2674 }
2675 }
2676
2677 if (empty_vp_index)
2678 continue;
2679
2680 if (found_vp &&
2681 new_fcport->d_id.b24 == vha->d_id.b24)
2682 break;
2683 }
Andrew Vasquezeb66dc62007-11-12 10:30:58 -08002684
2685 if (vp_index <= pha->max_npiv_vports)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002686 continue;
2687 }
2688
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07002689 /* Bypass if same domain and area of adapter. */
2690 if (((new_fcport->d_id.b24 & 0xffff00) ==
2691 (ha->d_id.b24 & 0xffff00)) && ha->current_topology ==
2692 ISP_CFG_FL)
2693 continue;
2694
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 /* Bypass reserved domain fields. */
2696 if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
2697 continue;
2698
2699 /* Locate matching device in database. */
2700 found = 0;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002701 list_for_each_entry(fcport, &pha->fcports, list) {
2702 if (new_fcport->vp_idx != fcport->vp_idx)
2703 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 if (memcmp(new_fcport->port_name, fcport->port_name,
2705 WWN_SIZE))
2706 continue;
2707
2708 found++;
2709
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002710 /* Update port state. */
2711 memcpy(fcport->fabric_port_name,
2712 new_fcport->fabric_port_name, WWN_SIZE);
2713 fcport->fp_speed = new_fcport->fp_speed;
2714
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 /*
2716 * If address the same and state FCS_ONLINE, nothing
2717 * changed.
2718 */
2719 if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
2720 atomic_read(&fcport->state) == FCS_ONLINE) {
2721 break;
2722 }
2723
2724 /*
2725 * If device was not a fabric device before.
2726 */
2727 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
2728 fcport->d_id.b24 = new_fcport->d_id.b24;
2729 fcport->loop_id = FC_NO_LOOP_ID;
2730 fcport->flags |= (FCF_FABRIC_DEVICE |
2731 FCF_LOGIN_NEEDED);
2732 fcport->flags &= ~FCF_PERSISTENT_BOUND;
2733 break;
2734 }
2735
2736 /*
2737 * Port ID changed or device was marked to be updated;
2738 * Log it out if still logged in and mark it for
2739 * relogin later.
2740 */
2741 fcport->d_id.b24 = new_fcport->d_id.b24;
2742 fcport->flags |= FCF_LOGIN_NEEDED;
2743 if (fcport->loop_id != FC_NO_LOOP_ID &&
2744 (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2745 fcport->port_type != FCT_INITIATOR &&
2746 fcport->port_type != FCT_BROADCAST) {
Andrew Vasquezfd34f552007-07-19 15:06:00 -07002747 ha->isp_ops->fabric_logout(ha, fcport->loop_id,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07002748 fcport->d_id.b.domain, fcport->d_id.b.area,
2749 fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 fcport->loop_id = FC_NO_LOOP_ID;
2751 }
2752
2753 break;
2754 }
2755
2756 if (found)
2757 continue;
2758
2759 /* If device was not in our fcports list, then add it. */
2760 list_add_tail(&new_fcport->list, new_fcports);
2761
2762 /* Allocate a new replacement fcport. */
2763 nxt_d_id.b24 = new_fcport->d_id.b24;
2764 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2765 if (new_fcport == NULL) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002766 kfree(swl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 return (QLA_MEMORY_ALLOC_FAILED);
2768 }
2769 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2770 new_fcport->d_id.b24 = nxt_d_id.b24;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002771 new_fcport->vp_idx = ha->vp_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 }
2773
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002774 kfree(swl);
2775 kfree(new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776
2777 if (!list_empty(new_fcports))
2778 ha->device_flags |= DFLG_FABRIC_DEVICES;
2779
2780 return (rval);
2781}
2782
2783/*
2784 * qla2x00_find_new_loop_id
2785 * Scan through our port list and find a new usable loop ID.
2786 *
2787 * Input:
2788 * ha: adapter state pointer.
2789 * dev: port structure pointer.
2790 *
2791 * Returns:
2792 * qla2x00 local function return status code.
2793 *
2794 * Context:
2795 * Kernel context.
2796 */
Adrian Bunk413975a2006-06-30 02:33:06 -07002797static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798qla2x00_find_new_loop_id(scsi_qla_host_t *ha, fc_port_t *dev)
2799{
2800 int rval;
2801 int found;
2802 fc_port_t *fcport;
2803 uint16_t first_loop_id;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002804 scsi_qla_host_t *pha = to_qla_parent(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805
2806 rval = QLA_SUCCESS;
2807
2808 /* Save starting loop ID. */
2809 first_loop_id = dev->loop_id;
2810
2811 for (;;) {
2812 /* Skip loop ID if already used by adapter. */
2813 if (dev->loop_id == ha->loop_id) {
2814 dev->loop_id++;
2815 }
2816
2817 /* Skip reserved loop IDs. */
Andrew Vasquez3d716442005-07-06 10:30:26 -07002818 while (qla2x00_is_reserved_id(ha, dev->loop_id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 dev->loop_id++;
2820 }
2821
2822 /* Reset loop ID if passed the end. */
2823 if (dev->loop_id > ha->last_loop_id) {
2824 /* first loop ID. */
2825 dev->loop_id = ha->min_external_loopid;
2826 }
2827
2828 /* Check for loop ID being already in use. */
2829 found = 0;
2830 fcport = NULL;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002831 list_for_each_entry(fcport, &pha->fcports, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 if (fcport->loop_id == dev->loop_id && fcport != dev) {
2833 /* ID possibly in use */
2834 found++;
2835 break;
2836 }
2837 }
2838
2839 /* If not in use then it is free to use. */
2840 if (!found) {
2841 break;
2842 }
2843
2844 /* ID in use. Try next value. */
2845 dev->loop_id++;
2846
2847 /* If wrap around. No free ID to use. */
2848 if (dev->loop_id == first_loop_id) {
2849 dev->loop_id = FC_NO_LOOP_ID;
2850 rval = QLA_FUNCTION_FAILED;
2851 break;
2852 }
2853 }
2854
2855 return (rval);
2856}
2857
2858/*
2859 * qla2x00_device_resync
2860 * Marks devices in the database that needs resynchronization.
2861 *
2862 * Input:
2863 * ha = adapter block pointer.
2864 *
2865 * Context:
2866 * Kernel context.
2867 */
2868static int
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002869qla2x00_device_resync(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870{
2871 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 uint32_t mask;
2873 fc_port_t *fcport;
2874 uint32_t rscn_entry;
2875 uint8_t rscn_out_iter;
2876 uint8_t format;
2877 port_id_t d_id;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002878 scsi_qla_host_t *pha = to_qla_parent(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879
2880 rval = QLA_RSCNS_HANDLED;
2881
2882 while (ha->rscn_out_ptr != ha->rscn_in_ptr ||
2883 ha->flags.rscn_queue_overflow) {
2884
2885 rscn_entry = ha->rscn_queue[ha->rscn_out_ptr];
2886 format = MSB(MSW(rscn_entry));
2887 d_id.b.domain = LSB(MSW(rscn_entry));
2888 d_id.b.area = MSB(LSW(rscn_entry));
2889 d_id.b.al_pa = LSB(LSW(rscn_entry));
2890
2891 DEBUG(printk("scsi(%ld): RSCN queue entry[%d] = "
2892 "[%02x/%02x%02x%02x].\n",
2893 ha->host_no, ha->rscn_out_ptr, format, d_id.b.domain,
2894 d_id.b.area, d_id.b.al_pa));
2895
2896 ha->rscn_out_ptr++;
2897 if (ha->rscn_out_ptr == MAX_RSCN_COUNT)
2898 ha->rscn_out_ptr = 0;
2899
2900 /* Skip duplicate entries. */
2901 for (rscn_out_iter = ha->rscn_out_ptr;
2902 !ha->flags.rscn_queue_overflow &&
2903 rscn_out_iter != ha->rscn_in_ptr;
2904 rscn_out_iter = (rscn_out_iter ==
2905 (MAX_RSCN_COUNT - 1)) ? 0: rscn_out_iter + 1) {
2906
2907 if (rscn_entry != ha->rscn_queue[rscn_out_iter])
2908 break;
2909
2910 DEBUG(printk("scsi(%ld): Skipping duplicate RSCN queue "
2911 "entry found at [%d].\n", ha->host_no,
2912 rscn_out_iter));
2913
2914 ha->rscn_out_ptr = rscn_out_iter;
2915 }
2916
2917 /* Queue overflow, set switch default case. */
2918 if (ha->flags.rscn_queue_overflow) {
2919 DEBUG(printk("scsi(%ld): device_resync: rscn "
2920 "overflow.\n", ha->host_no));
2921
2922 format = 3;
2923 ha->flags.rscn_queue_overflow = 0;
2924 }
2925
2926 switch (format) {
2927 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 mask = 0xffffff;
2929 break;
2930 case 1:
2931 mask = 0xffff00;
2932 break;
2933 case 2:
2934 mask = 0xff0000;
2935 break;
2936 default:
2937 mask = 0x0;
2938 d_id.b24 = 0;
2939 ha->rscn_out_ptr = ha->rscn_in_ptr;
2940 break;
2941 }
2942
2943 rval = QLA_SUCCESS;
2944
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002945 list_for_each_entry(fcport, &pha->fcports, list) {
2946 if (fcport->vp_idx != ha->vp_idx)
2947 continue;
2948
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2950 (fcport->d_id.b24 & mask) != d_id.b24 ||
2951 fcport->port_type == FCT_BROADCAST)
2952 continue;
2953
2954 if (atomic_read(&fcport->state) == FCS_ONLINE) {
2955 if (format != 3 ||
2956 fcport->port_type != FCT_INITIATOR) {
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002957 qla2x00_mark_device_lost(ha, fcport,
2958 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 }
2960 }
2961 fcport->flags &= ~FCF_FARP_DONE;
2962 }
2963 }
2964 return (rval);
2965}
2966
2967/*
2968 * qla2x00_fabric_dev_login
2969 * Login fabric target device and update FC port database.
2970 *
2971 * Input:
2972 * ha: adapter state pointer.
2973 * fcport: port structure list pointer.
2974 * next_loopid: contains value of a new loop ID that can be used
2975 * by the next login attempt.
2976 *
2977 * Returns:
2978 * qla2x00 local function return status code.
2979 *
2980 * Context:
2981 * Kernel context.
2982 */
2983static int
2984qla2x00_fabric_dev_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2985 uint16_t *next_loopid)
2986{
2987 int rval;
2988 int retry;
Andrew Vasquez01071092005-07-06 10:31:37 -07002989 uint8_t opts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
2991 rval = QLA_SUCCESS;
2992 retry = 0;
2993
2994 rval = qla2x00_fabric_login(ha, fcport, next_loopid);
2995 if (rval == QLA_SUCCESS) {
Andrew Vasquez01071092005-07-06 10:31:37 -07002996 /* Send an ADISC to tape devices.*/
2997 opts = 0;
2998 if (fcport->flags & FCF_TAPE_PRESENT)
2999 opts |= BIT_1;
3000 rval = qla2x00_get_port_database(ha, fcport, opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 if (rval != QLA_SUCCESS) {
Andrew Vasquezfd34f552007-07-19 15:06:00 -07003002 ha->isp_ops->fabric_logout(ha, fcport->loop_id,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07003003 fcport->d_id.b.domain, fcport->d_id.b.area,
3004 fcport->d_id.b.al_pa);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08003005 qla2x00_mark_device_lost(ha, fcport, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 } else {
3007 qla2x00_update_fcport(ha, fcport);
3008 }
3009 }
3010
3011 return (rval);
3012}
3013
3014/*
3015 * qla2x00_fabric_login
3016 * Issue fabric login command.
3017 *
3018 * Input:
3019 * ha = adapter block pointer.
3020 * device = pointer to FC device type structure.
3021 *
3022 * Returns:
3023 * 0 - Login successfully
3024 * 1 - Login failed
3025 * 2 - Initiator device
3026 * 3 - Fatal error
3027 */
3028int
3029qla2x00_fabric_login(scsi_qla_host_t *ha, fc_port_t *fcport,
3030 uint16_t *next_loopid)
3031{
3032 int rval;
3033 int retry;
3034 uint16_t tmp_loopid;
3035 uint16_t mb[MAILBOX_REGISTER_COUNT];
3036
3037 retry = 0;
3038 tmp_loopid = 0;
3039
3040 for (;;) {
3041 DEBUG(printk("scsi(%ld): Trying Fabric Login w/loop id 0x%04x "
3042 "for port %02x%02x%02x.\n",
3043 ha->host_no, fcport->loop_id, fcport->d_id.b.domain,
3044 fcport->d_id.b.area, fcport->d_id.b.al_pa));
3045
3046 /* Login fcport on switch. */
Andrew Vasquezfd34f552007-07-19 15:06:00 -07003047 ha->isp_ops->fabric_login(ha, fcport->loop_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 fcport->d_id.b.domain, fcport->d_id.b.area,
3049 fcport->d_id.b.al_pa, mb, BIT_0);
3050 if (mb[0] == MBS_PORT_ID_USED) {
3051 /*
3052 * Device has another loop ID. The firmware team
Andrew Vasquez01071092005-07-06 10:31:37 -07003053 * recommends the driver perform an implicit login with
3054 * the specified ID again. The ID we just used is save
3055 * here so we return with an ID that can be tried by
3056 * the next login.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 */
3058 retry++;
3059 tmp_loopid = fcport->loop_id;
3060 fcport->loop_id = mb[1];
3061
3062 DEBUG(printk("Fabric Login: port in use - next "
3063 "loop id=0x%04x, port Id=%02x%02x%02x.\n",
3064 fcport->loop_id, fcport->d_id.b.domain,
3065 fcport->d_id.b.area, fcport->d_id.b.al_pa));
3066
3067 } else if (mb[0] == MBS_COMMAND_COMPLETE) {
3068 /*
3069 * Login succeeded.
3070 */
3071 if (retry) {
3072 /* A retry occurred before. */
3073 *next_loopid = tmp_loopid;
3074 } else {
3075 /*
3076 * No retry occurred before. Just increment the
3077 * ID value for next login.
3078 */
3079 *next_loopid = (fcport->loop_id + 1);
3080 }
3081
3082 if (mb[1] & BIT_0) {
3083 fcport->port_type = FCT_INITIATOR;
3084 } else {
3085 fcport->port_type = FCT_TARGET;
3086 if (mb[1] & BIT_1) {
3087 fcport->flags |= FCF_TAPE_PRESENT;
3088 }
3089 }
3090
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07003091 if (mb[10] & BIT_0)
3092 fcport->supported_classes |= FC_COS_CLASS2;
3093 if (mb[10] & BIT_1)
3094 fcport->supported_classes |= FC_COS_CLASS3;
3095
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 rval = QLA_SUCCESS;
3097 break;
3098 } else if (mb[0] == MBS_LOOP_ID_USED) {
3099 /*
3100 * Loop ID already used, try next loop ID.
3101 */
3102 fcport->loop_id++;
3103 rval = qla2x00_find_new_loop_id(ha, fcport);
3104 if (rval != QLA_SUCCESS) {
3105 /* Ran out of loop IDs to use */
3106 break;
3107 }
3108 } else if (mb[0] == MBS_COMMAND_ERROR) {
3109 /*
3110 * Firmware possibly timed out during login. If NO
3111 * retries are left to do then the device is declared
3112 * dead.
3113 */
3114 *next_loopid = fcport->loop_id;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07003115 ha->isp_ops->fabric_logout(ha, fcport->loop_id,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07003116 fcport->d_id.b.domain, fcport->d_id.b.area,
3117 fcport->d_id.b.al_pa);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08003118 qla2x00_mark_device_lost(ha, fcport, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119
3120 rval = 1;
3121 break;
3122 } else {
3123 /*
3124 * unrecoverable / not handled error
3125 */
3126 DEBUG2(printk("%s(%ld): failed=%x port_id=%02x%02x%02x "
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003127 "loop_id=%x jiffies=%lx.\n",
3128 __func__, ha->host_no, mb[0],
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 fcport->d_id.b.domain, fcport->d_id.b.area,
3130 fcport->d_id.b.al_pa, fcport->loop_id, jiffies));
3131
3132 *next_loopid = fcport->loop_id;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07003133 ha->isp_ops->fabric_logout(ha, fcport->loop_id,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07003134 fcport->d_id.b.domain, fcport->d_id.b.area,
3135 fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 fcport->loop_id = FC_NO_LOOP_ID;
Andrew Vasquez0eedfcf2005-10-27 11:09:38 -07003137 fcport->login_retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138
3139 rval = 3;
3140 break;
3141 }
3142 }
3143
3144 return (rval);
3145}
3146
3147/*
3148 * qla2x00_local_device_login
3149 * Issue local device login command.
3150 *
3151 * Input:
3152 * ha = adapter block pointer.
3153 * loop_id = loop id of device to login to.
3154 *
3155 * Returns (Where's the #define!!!!):
3156 * 0 - Login successfully
3157 * 1 - Login failed
3158 * 3 - Fatal error
3159 */
3160int
andrew.vasquez@qlogic.com9a52a572006-03-09 14:27:44 -08003161qla2x00_local_device_login(scsi_qla_host_t *ha, fc_port_t *fcport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162{
3163 int rval;
3164 uint16_t mb[MAILBOX_REGISTER_COUNT];
3165
3166 memset(mb, 0, sizeof(mb));
andrew.vasquez@qlogic.com9a52a572006-03-09 14:27:44 -08003167 rval = qla2x00_login_local_device(ha, fcport, mb, BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 if (rval == QLA_SUCCESS) {
3169 /* Interrogate mailbox registers for any errors */
3170 if (mb[0] == MBS_COMMAND_ERROR)
3171 rval = 1;
3172 else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
3173 /* device not in PCB table */
3174 rval = 3;
3175 }
3176
3177 return (rval);
3178}
3179
3180/*
3181 * qla2x00_loop_resync
3182 * Resync with fibre channel devices.
3183 *
3184 * Input:
3185 * ha = adapter block pointer.
3186 *
3187 * Returns:
3188 * 0 = success
3189 */
3190int
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003191qla2x00_loop_resync(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192{
3193 int rval;
3194 uint32_t wait_time;
3195
3196 rval = QLA_SUCCESS;
3197
3198 atomic_set(&ha->loop_state, LOOP_UPDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3200 if (ha->flags.online) {
3201 if (!(rval = qla2x00_fw_ready(ha))) {
3202 /* Wait at most MAX_TARGET RSCNs for a stable link. */
3203 wait_time = 256;
3204 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 atomic_set(&ha->loop_state, LOOP_UPDATE);
3206
Andrew Vasquez01071092005-07-06 10:31:37 -07003207 /* Issue a marker after FW becomes ready. */
3208 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
3209 ha->marker_needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210
3211 /* Remap devices on Loop. */
3212 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
3213
3214 qla2x00_configure_loop(ha);
3215 wait_time--;
3216 } while (!atomic_read(&ha->loop_down_timer) &&
3217 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
3218 wait_time &&
3219 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
3220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 }
3222
3223 if (test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
3224 return (QLA_FUNCTION_FAILED);
3225 }
3226
3227 if (rval) {
3228 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
3229 }
3230
3231 return (rval);
3232}
3233
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234void
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08003235qla2x00_update_fcports(scsi_qla_host_t *ha)
3236{
3237 fc_port_t *fcport;
3238
3239 /* Go with deferred removal of rport references. */
3240 list_for_each_entry(fcport, &ha->fcports, list)
Andrew Vasquezc9c5ced2008-07-24 08:31:49 -07003241 if (fcport->drport &&
3242 atomic_read(&fcport->state) != FCS_UNCONFIGURED)
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08003243 qla2x00_rport_del(fcport);
3244}
3245
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246/*
3247* qla2x00_abort_isp
3248* Resets ISP and aborts all outstanding commands.
3249*
3250* Input:
3251* ha = adapter block pointer.
3252*
3253* Returns:
3254* 0 = success
3255*/
3256int
3257qla2x00_abort_isp(scsi_qla_host_t *ha)
3258{
Andrew Vasquez476e8972006-08-23 14:54:55 -07003259 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 uint8_t status = 0;
Andrew Vasqueza7a28502008-08-13 21:36:57 -07003261 scsi_qla_host_t *vha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262
3263 if (ha->flags.online) {
3264 ha->flags.online = 0;
3265 clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
Harish Zunjarraoe5f5f6f2008-07-10 16:55:49 -07003266 ha->qla_stats.total_isp_aborts++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267
3268 qla_printk(KERN_INFO, ha,
3269 "Performing ISP error recovery - ha= %p.\n", ha);
Andrew Vasquezfd34f552007-07-19 15:06:00 -07003270 ha->isp_ops->reset_chip(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271
3272 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
3273 if (atomic_read(&ha->loop_state) != LOOP_DOWN) {
3274 atomic_set(&ha->loop_state, LOOP_DOWN);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08003275 qla2x00_mark_all_devices_lost(ha, 0);
Andrew Vasqueza7a28502008-08-13 21:36:57 -07003276 list_for_each_entry(vha, &ha->vp_list, vp_list)
3277 qla2x00_mark_all_devices_lost(vha, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 } else {
3279 if (!atomic_read(&ha->loop_down_timer))
3280 atomic_set(&ha->loop_down_timer,
3281 LOOP_DOWN_TIME);
3282 }
3283
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 /* Requeue all commands in outstanding command list. */
Andrew Vasquezdf4bf0b2008-01-31 12:33:46 -08003285 qla2x00_abort_all_cmds(ha, DID_RESET << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286
Andrew Vasquezfd34f552007-07-19 15:06:00 -07003287 ha->isp_ops->get_flash_version(ha, ha->request_ring);
Andrew Vasquez30c47662007-01-29 10:22:21 -08003288
Andrew Vasquezfd34f552007-07-19 15:06:00 -07003289 ha->isp_ops->nvram_config(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290
3291 if (!qla2x00_restart_isp(ha)) {
3292 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3293
3294 if (!atomic_read(&ha->loop_down_timer)) {
3295 /*
3296 * Issue marker command only when we are going
3297 * to start the I/O .
3298 */
3299 ha->marker_needed = 1;
3300 }
3301
3302 ha->flags.online = 1;
3303
Andrew Vasquezfd34f552007-07-19 15:06:00 -07003304 ha->isp_ops->enable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003306 ha->isp_abort_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
Andrew Vasquez476e8972006-08-23 14:54:55 -07003308
Andrew Vasquezdf613b92008-01-17 09:02:17 -08003309 if (ha->fce) {
3310 ha->flags.fce_enabled = 1;
3311 memset(ha->fce, 0,
3312 fce_calc_size(ha->fce_bufs));
3313 rval = qla2x00_enable_fce_trace(ha,
3314 ha->fce_dma, ha->fce_bufs, ha->fce_mb,
3315 &ha->fce_bufs);
3316 if (rval) {
3317 qla_printk(KERN_WARNING, ha,
3318 "Unable to reinitialize FCE "
3319 "(%d).\n", rval);
3320 ha->flags.fce_enabled = 0;
3321 }
3322 }
Andrew Vasquez436a7b12008-07-10 16:55:54 -07003323
3324 if (ha->eft) {
3325 memset(ha->eft, 0, EFT_SIZE);
3326 rval = qla2x00_enable_eft_trace(ha,
3327 ha->eft_dma, EFT_NUM_BUFFERS);
3328 if (rval) {
3329 qla_printk(KERN_WARNING, ha,
3330 "Unable to reinitialize EFT "
3331 "(%d).\n", rval);
3332 }
3333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 } else { /* failed the ISP abort */
3335 ha->flags.online = 1;
3336 if (test_bit(ISP_ABORT_RETRY, &ha->dpc_flags)) {
3337 if (ha->isp_abort_cnt == 0) {
3338 qla_printk(KERN_WARNING, ha,
3339 "ISP error recovery failed - "
3340 "board disabled\n");
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003341 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 * The next call disables the board
3343 * completely.
3344 */
Andrew Vasquezfd34f552007-07-19 15:06:00 -07003345 ha->isp_ops->reset_adapter(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 ha->flags.online = 0;
3347 clear_bit(ISP_ABORT_RETRY,
3348 &ha->dpc_flags);
3349 status = 0;
3350 } else { /* schedule another ISP abort */
3351 ha->isp_abort_cnt--;
3352 DEBUG(printk("qla%ld: ISP abort - "
Andrew Vasquez01071092005-07-06 10:31:37 -07003353 "retry remaining %d\n",
Andrew Vasquez744f11fd2006-06-23 16:11:05 -07003354 ha->host_no, ha->isp_abort_cnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 status = 1;
3356 }
3357 } else {
3358 ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
3359 DEBUG(printk("qla2x00(%ld): ISP error recovery "
3360 "- retrying (%d) more times\n",
Andrew Vasquez744f11fd2006-06-23 16:11:05 -07003361 ha->host_no, ha->isp_abort_cnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 set_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3363 status = 1;
3364 }
3365 }
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003366
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 }
3368
3369 if (status) {
3370 qla_printk(KERN_INFO, ha,
3371 "qla2x00_abort_isp: **** FAILED ****\n");
3372 } else {
3373 DEBUG(printk(KERN_INFO
3374 "qla2x00_abort_isp(%ld): exiting.\n",
Andrew Vasquez744f11fd2006-06-23 16:11:05 -07003375 ha->host_no));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 }
3377
3378 return(status);
3379}
3380
3381/*
3382* qla2x00_restart_isp
3383* restarts the ISP after a reset
3384*
3385* Input:
3386* ha = adapter block pointer.
3387*
3388* Returns:
3389* 0 = success
3390*/
3391static int
3392qla2x00_restart_isp(scsi_qla_host_t *ha)
3393{
3394 uint8_t status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 uint32_t wait_time;
3396
3397 /* If firmware needs to be loaded */
3398 if (qla2x00_isp_firmware(ha)) {
3399 ha->flags.online = 0;
Andrew Vasquez3db06522008-01-31 12:33:49 -08003400 if (!(status = ha->isp_ops->chip_diag(ha)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 status = qla2x00_setup_chip(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 }
3403
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 if (!status && !(status = qla2x00_init_rings(ha))) {
3405 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3406 if (!(status = qla2x00_fw_ready(ha))) {
3407 DEBUG(printk("%s(): Start configure loop, "
Andrew Vasquez744f11fd2006-06-23 16:11:05 -07003408 "status = %d\n", __func__, status));
Andrew Vasquez01071092005-07-06 10:31:37 -07003409
3410 /* Issue a marker after FW becomes ready. */
3411 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
3412
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 ha->flags.online = 1;
3414 /* Wait at most MAX_TARGET RSCNs for a stable link. */
3415 wait_time = 256;
3416 do {
3417 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
3418 qla2x00_configure_loop(ha);
3419 wait_time--;
3420 } while (!atomic_read(&ha->loop_down_timer) &&
3421 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
3422 wait_time &&
3423 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
3424 }
3425
3426 /* if no cable then assume it's good */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003427 if ((ha->device_flags & DFLG_NO_CABLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 status = 0;
3429
3430 DEBUG(printk("%s(): Configure loop done, status = 0x%x\n",
3431 __func__,
Andrew Vasquez744f11fd2006-06-23 16:11:05 -07003432 status));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 }
3434 return (status);
3435}
3436
3437/*
3438* qla2x00_reset_adapter
3439* Reset adapter.
3440*
3441* Input:
3442* ha = adapter block pointer.
3443*/
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003444void
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445qla2x00_reset_adapter(scsi_qla_host_t *ha)
3446{
3447 unsigned long flags = 0;
Andrew Vasquez3d716442005-07-06 10:30:26 -07003448 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449
3450 ha->flags.online = 0;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07003451 ha->isp_ops->disable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 spin_lock_irqsave(&ha->hardware_lock, flags);
3454 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
3455 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
3456 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
3457 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
3458 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3459}
Andrew Vasquez01071092005-07-06 10:31:37 -07003460
3461void
3462qla24xx_reset_adapter(scsi_qla_host_t *ha)
3463{
3464 unsigned long flags = 0;
3465 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
3466
3467 ha->flags.online = 0;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07003468 ha->isp_ops->disable_intrs(ha);
Andrew Vasquez01071092005-07-06 10:31:37 -07003469
3470 spin_lock_irqsave(&ha->hardware_lock, flags);
3471 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
3472 RD_REG_DWORD(&reg->hccr);
3473 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
3474 RD_REG_DWORD(&reg->hccr);
3475 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3476}
3477
David Miller4e08df32007-04-16 12:37:43 -07003478/* On sparc systems, obtain port and node WWN from firmware
3479 * properties.
3480 */
3481static void qla24xx_nvram_wwn_from_ofw(scsi_qla_host_t *ha, struct nvram_24xx *nv)
3482{
3483#ifdef CONFIG_SPARC
3484 struct pci_dev *pdev = ha->pdev;
David S. Miller15576bc2007-05-08 00:36:49 -07003485 struct device_node *dp = pci_device_to_OF_node(pdev);
3486 const u8 *val;
David Miller4e08df32007-04-16 12:37:43 -07003487 int len;
3488
3489 val = of_get_property(dp, "port-wwn", &len);
3490 if (val && len >= WWN_SIZE)
3491 memcpy(nv->port_name, val, WWN_SIZE);
3492
3493 val = of_get_property(dp, "node-wwn", &len);
3494 if (val && len >= WWN_SIZE)
3495 memcpy(nv->node_name, val, WWN_SIZE);
3496#endif
3497}
3498
Andrew Vasquez01071092005-07-06 10:31:37 -07003499int
3500qla24xx_nvram_config(scsi_qla_host_t *ha)
3501{
David Miller4e08df32007-04-16 12:37:43 -07003502 int rval;
Andrew Vasquez01071092005-07-06 10:31:37 -07003503 struct init_cb_24xx *icb;
3504 struct nvram_24xx *nv;
3505 uint32_t *dptr;
3506 uint8_t *dptr1, *dptr2;
3507 uint32_t chksum;
3508 uint16_t cnt;
3509
David Miller4e08df32007-04-16 12:37:43 -07003510 rval = QLA_SUCCESS;
Andrew Vasquez01071092005-07-06 10:31:37 -07003511 icb = (struct init_cb_24xx *)ha->init_cb;
Seokmann Ju281afe12007-07-26 13:43:34 -07003512 nv = ha->nvram;
Andrew Vasquez01071092005-07-06 10:31:37 -07003513
3514 /* Determine NVRAM starting address. */
3515 ha->nvram_size = sizeof(struct nvram_24xx);
3516 ha->nvram_base = FA_NVRAM_FUNC0_ADDR;
andrew.vasquez@qlogic.com6f641792006-03-09 14:27:34 -08003517 ha->vpd_size = FA_NVRAM_VPD_SIZE;
3518 ha->vpd_base = FA_NVRAM_VPD0_ADDR;
3519 if (PCI_FUNC(ha->pdev->devfn)) {
Andrew Vasquez01071092005-07-06 10:31:37 -07003520 ha->nvram_base = FA_NVRAM_FUNC1_ADDR;
andrew.vasquez@qlogic.com6f641792006-03-09 14:27:34 -08003521 ha->vpd_base = FA_NVRAM_VPD1_ADDR;
3522 }
Andrew Vasquez01071092005-07-06 10:31:37 -07003523
Seokmann Ju281afe12007-07-26 13:43:34 -07003524 /* Get VPD data into cache */
3525 ha->vpd = ha->nvram + VPD_OFFSET;
3526 ha->isp_ops->read_nvram(ha, (uint8_t *)ha->vpd,
3527 ha->nvram_base - FA_NVRAM_FUNC0_ADDR, FA_NVRAM_VPD_SIZE * 4);
3528
3529 /* Get NVRAM data into cache and calculate checksum. */
Andrew Vasquez01071092005-07-06 10:31:37 -07003530 dptr = (uint32_t *)nv;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07003531 ha->isp_ops->read_nvram(ha, (uint8_t *)dptr, ha->nvram_base,
Andrew Vasquez01071092005-07-06 10:31:37 -07003532 ha->nvram_size);
3533 for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++)
3534 chksum += le32_to_cpu(*dptr++);
3535
3536 DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
Seokmann Ju281afe12007-07-26 13:43:34 -07003537 DEBUG5(qla2x00_dump_buffer((uint8_t *)nv, ha->nvram_size));
Andrew Vasquez01071092005-07-06 10:31:37 -07003538
3539 /* Bad NVRAM data, set defaults parameters. */
3540 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
3541 || nv->id[3] != ' ' ||
3542 nv->nvram_version < __constant_cpu_to_le16(ICB_VERSION)) {
3543 /* Reset NVRAM data. */
3544 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
3545 "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
3546 le16_to_cpu(nv->nvram_version));
David Miller4e08df32007-04-16 12:37:43 -07003547 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
3548 "invalid -- WWPN) defaults.\n");
3549
3550 /*
3551 * Set default initialization control block.
3552 */
3553 memset(nv, 0, ha->nvram_size);
3554 nv->nvram_version = __constant_cpu_to_le16(ICB_VERSION);
3555 nv->version = __constant_cpu_to_le16(ICB_VERSION);
3556 nv->frame_payload_size = __constant_cpu_to_le16(2048);
3557 nv->execution_throttle = __constant_cpu_to_le16(0xFFFF);
3558 nv->exchange_count = __constant_cpu_to_le16(0);
3559 nv->hard_address = __constant_cpu_to_le16(124);
3560 nv->port_name[0] = 0x21;
3561 nv->port_name[1] = 0x00 + PCI_FUNC(ha->pdev->devfn);
3562 nv->port_name[2] = 0x00;
3563 nv->port_name[3] = 0xe0;
3564 nv->port_name[4] = 0x8b;
3565 nv->port_name[5] = 0x1c;
3566 nv->port_name[6] = 0x55;
3567 nv->port_name[7] = 0x86;
3568 nv->node_name[0] = 0x20;
3569 nv->node_name[1] = 0x00;
3570 nv->node_name[2] = 0x00;
3571 nv->node_name[3] = 0xe0;
3572 nv->node_name[4] = 0x8b;
3573 nv->node_name[5] = 0x1c;
3574 nv->node_name[6] = 0x55;
3575 nv->node_name[7] = 0x86;
3576 qla24xx_nvram_wwn_from_ofw(ha, nv);
3577 nv->login_retry_count = __constant_cpu_to_le16(8);
3578 nv->interrupt_delay_timer = __constant_cpu_to_le16(0);
3579 nv->login_timeout = __constant_cpu_to_le16(0);
3580 nv->firmware_options_1 =
3581 __constant_cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
3582 nv->firmware_options_2 = __constant_cpu_to_le32(2 << 4);
3583 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_12);
3584 nv->firmware_options_3 = __constant_cpu_to_le32(2 << 13);
3585 nv->host_p = __constant_cpu_to_le32(BIT_11|BIT_10);
3586 nv->efi_parameters = __constant_cpu_to_le32(0);
3587 nv->reset_delay = 5;
3588 nv->max_luns_per_target = __constant_cpu_to_le16(128);
3589 nv->port_down_retry_count = __constant_cpu_to_le16(30);
3590 nv->link_down_timeout = __constant_cpu_to_le16(30);
3591
3592 rval = 1;
Andrew Vasquez01071092005-07-06 10:31:37 -07003593 }
3594
3595 /* Reset Initialization control block */
3596 memset(icb, 0, sizeof(struct init_cb_24xx));
3597
3598 /* Copy 1st segment. */
3599 dptr1 = (uint8_t *)icb;
3600 dptr2 = (uint8_t *)&nv->version;
3601 cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
3602 while (cnt--)
3603 *dptr1++ = *dptr2++;
3604
3605 icb->login_retry_count = nv->login_retry_count;
Andrew Vasquez3ea66e22006-06-23 16:11:27 -07003606 icb->link_down_on_nos = nv->link_down_on_nos;
Andrew Vasquez01071092005-07-06 10:31:37 -07003607
3608 /* Copy 2nd segment. */
3609 dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
3610 dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
3611 cnt = (uint8_t *)&icb->reserved_3 -
3612 (uint8_t *)&icb->interrupt_delay_timer;
3613 while (cnt--)
3614 *dptr1++ = *dptr2++;
3615
3616 /*
3617 * Setup driver NVRAM options.
3618 */
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08003619 qla2x00_set_model_info(ha, nv->model_name, sizeof(nv->model_name),
3620 "QLA2462");
Andrew Vasquez01071092005-07-06 10:31:37 -07003621
Andrew Vasquez5341e862006-05-17 15:09:16 -07003622 /* Use alternate WWN? */
3623 if (nv->host_p & __constant_cpu_to_le32(BIT_15)) {
3624 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
3625 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
3626 }
3627
Andrew Vasquez01071092005-07-06 10:31:37 -07003628 /* Prepare nodename */
Andrew Vasquezfd0e7e42006-05-17 15:09:11 -07003629 if ((icb->firmware_options_1 & __constant_cpu_to_le32(BIT_14)) == 0) {
Andrew Vasquez01071092005-07-06 10:31:37 -07003630 /*
3631 * Firmware will apply the following mask if the nodename was
3632 * not provided.
3633 */
3634 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
3635 icb->node_name[0] &= 0xF0;
3636 }
3637
3638 /* Set host adapter parameters. */
3639 ha->flags.disable_risc_code_load = 0;
Andrew Vasquez0c8c39a2006-12-13 19:20:30 -08003640 ha->flags.enable_lip_reset = 0;
3641 ha->flags.enable_lip_full_login =
3642 le32_to_cpu(nv->host_p) & BIT_10 ? 1: 0;
3643 ha->flags.enable_target_reset =
3644 le32_to_cpu(nv->host_p) & BIT_11 ? 1: 0;
Andrew Vasquez01071092005-07-06 10:31:37 -07003645 ha->flags.enable_led_scheme = 0;
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07003646 ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
Andrew Vasquez01071092005-07-06 10:31:37 -07003647
Andrew Vasquezfd0e7e42006-05-17 15:09:11 -07003648 ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
3649 (BIT_6 | BIT_5 | BIT_4)) >> 4;
Andrew Vasquez01071092005-07-06 10:31:37 -07003650
3651 memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
3652 sizeof(ha->fw_seriallink_options24));
3653
3654 /* save HBA serial number */
3655 ha->serial0 = icb->port_name[5];
3656 ha->serial1 = icb->port_name[6];
3657 ha->serial2 = icb->port_name[7];
3658 ha->node_name = icb->node_name;
3659 ha->port_name = icb->port_name;
3660
andrew.vasquez@qlogic.combc8fb3c2006-01-13 17:05:42 -08003661 icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
3662
Andrew Vasquez01071092005-07-06 10:31:37 -07003663 ha->retry_count = le16_to_cpu(nv->login_retry_count);
3664
3665 /* Set minimum login_timeout to 4 seconds. */
3666 if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
3667 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
3668 if (le16_to_cpu(nv->login_timeout) < 4)
3669 nv->login_timeout = __constant_cpu_to_le16(4);
3670 ha->login_timeout = le16_to_cpu(nv->login_timeout);
Seokmann Juc6852c42008-04-24 15:21:29 -07003671 icb->login_timeout = nv->login_timeout;
Andrew Vasquez01071092005-07-06 10:31:37 -07003672
Andrew Vasquez00a537b2008-02-28 14:06:11 -08003673 /* Set minimum RATOV to 100 tenths of a second. */
3674 ha->r_a_tov = 100;
Andrew Vasquez01071092005-07-06 10:31:37 -07003675
3676 ha->loop_reset_delay = nv->reset_delay;
3677
3678 /* Link Down Timeout = 0:
3679 *
3680 * When Port Down timer expires we will start returning
3681 * I/O's to OS with "DID_NO_CONNECT".
3682 *
3683 * Link Down Timeout != 0:
3684 *
3685 * The driver waits for the link to come up after link down
3686 * before returning I/Os to OS with "DID_NO_CONNECT".
3687 */
3688 if (le16_to_cpu(nv->link_down_timeout) == 0) {
3689 ha->loop_down_abort_time =
3690 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
3691 } else {
3692 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
3693 ha->loop_down_abort_time =
3694 (LOOP_DOWN_TIME - ha->link_down_timeout);
3695 }
3696
3697 /* Need enough time to try and get the port back. */
3698 ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
3699 if (qlport_down_retry)
3700 ha->port_down_retry_count = qlport_down_retry;
3701
3702 /* Set login_retry_count */
3703 ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
3704 if (ha->port_down_retry_count ==
3705 le16_to_cpu(nv->port_down_retry_count) &&
3706 ha->port_down_retry_count > 3)
3707 ha->login_retry_count = ha->port_down_retry_count;
3708 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
3709 ha->login_retry_count = ha->port_down_retry_count;
3710 if (ql2xloginretrycount)
3711 ha->login_retry_count = ql2xloginretrycount;
3712
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07003713 /* Enable ZIO. */
3714 if (!ha->flags.init_done) {
3715 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
3716 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
3717 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
3718 le16_to_cpu(icb->interrupt_delay_timer): 2;
3719 }
3720 icb->firmware_options_2 &= __constant_cpu_to_le32(
3721 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
3722 ha->flags.process_response_queue = 0;
3723 if (ha->zio_mode != QLA_ZIO_DISABLED) {
andrew.vasquez@qlogic.com4a59f712006-03-09 14:27:39 -08003724 ha->zio_mode = QLA_ZIO_MODE_6;
3725
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07003726 DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer delay "
3727 "(%d us).\n", ha->host_no, ha->zio_mode,
3728 ha->zio_timer * 100));
3729 qla_printk(KERN_INFO, ha,
3730 "ZIO mode %d enabled; timer delay (%d us).\n",
3731 ha->zio_mode, ha->zio_timer * 100);
3732
3733 icb->firmware_options_2 |= cpu_to_le32(
3734 (uint32_t)ha->zio_mode);
3735 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
3736 ha->flags.process_response_queue = 1;
3737 }
3738
David Miller4e08df32007-04-16 12:37:43 -07003739 if (rval) {
3740 DEBUG2_3(printk(KERN_WARNING
3741 "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
3742 }
3743 return (rval);
Andrew Vasquez01071092005-07-06 10:31:37 -07003744}
3745
Adrian Bunk413975a2006-06-30 02:33:06 -07003746static int
Andrew Vasquezd1c61902006-05-17 15:09:00 -07003747qla24xx_load_risc_flash(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3748{
3749 int rval;
3750 int segments, fragment;
3751 uint32_t faddr;
3752 uint32_t *dcode, dlen;
3753 uint32_t risc_addr;
3754 uint32_t risc_size;
3755 uint32_t i;
3756
3757 rval = QLA_SUCCESS;
3758
3759 segments = FA_RISC_CODE_SEGMENTS;
Andrew Vasquezc00d8992008-09-11 21:22:49 -07003760 faddr = ha->flt_region_fw;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07003761 dcode = (uint32_t *)ha->request_ring;
3762 *srisc_addr = 0;
3763
3764 /* Validate firmware image by checking version. */
3765 qla24xx_read_flash_data(ha, dcode, faddr + 4, 4);
3766 for (i = 0; i < 4; i++)
3767 dcode[i] = be32_to_cpu(dcode[i]);
3768 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
3769 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
3770 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
3771 dcode[3] == 0)) {
3772 qla_printk(KERN_WARNING, ha,
3773 "Unable to verify integrity of flash firmware image!\n");
3774 qla_printk(KERN_WARNING, ha,
3775 "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
3776 dcode[1], dcode[2], dcode[3]);
3777
3778 return QLA_FUNCTION_FAILED;
3779 }
3780
3781 while (segments && rval == QLA_SUCCESS) {
3782 /* Read segment's load information. */
3783 qla24xx_read_flash_data(ha, dcode, faddr, 4);
3784
3785 risc_addr = be32_to_cpu(dcode[2]);
3786 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
3787 risc_size = be32_to_cpu(dcode[3]);
3788
3789 fragment = 0;
3790 while (risc_size > 0 && rval == QLA_SUCCESS) {
3791 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
3792 if (dlen > risc_size)
3793 dlen = risc_size;
3794
3795 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3796 "addr %x, number of dwords 0x%x, offset 0x%x.\n",
3797 ha->host_no, risc_addr, dlen, faddr));
3798
3799 qla24xx_read_flash_data(ha, dcode, faddr, dlen);
3800 for (i = 0; i < dlen; i++)
3801 dcode[i] = swab32(dcode[i]);
3802
3803 rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3804 dlen);
3805 if (rval) {
3806 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3807 "segment %d of firmware\n", ha->host_no,
3808 fragment));
3809 qla_printk(KERN_WARNING, ha,
3810 "[ERROR] Failed to load segment %d of "
3811 "firmware\n", fragment);
3812 break;
3813 }
3814
3815 faddr += dlen;
3816 risc_addr += dlen;
3817 risc_size -= dlen;
3818 fragment++;
3819 }
3820
3821 /* Next segment. */
3822 segments--;
3823 }
3824
3825 return rval;
3826}
3827
Andrew Vasquezd1c61902006-05-17 15:09:00 -07003828#define QLA_FW_URL "ftp://ftp.qlogic.com/outgoing/linux/firmware/"
3829
Andrew Vasquez01071092005-07-06 10:31:37 -07003830int
Andrew Vasquez54333832005-11-09 15:49:04 -08003831qla2x00_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3832{
3833 int rval;
3834 int i, fragment;
3835 uint16_t *wcode, *fwcode;
3836 uint32_t risc_addr, risc_size, fwclen, wlen, *seg;
3837 struct fw_blob *blob;
3838
3839 /* Load firmware blob. */
3840 blob = qla2x00_request_firmware(ha);
3841 if (!blob) {
3842 qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
Andrew Vasquezd1c61902006-05-17 15:09:00 -07003843 qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
3844 "from: " QLA_FW_URL ".\n");
Andrew Vasquez54333832005-11-09 15:49:04 -08003845 return QLA_FUNCTION_FAILED;
3846 }
3847
3848 rval = QLA_SUCCESS;
3849
3850 wcode = (uint16_t *)ha->request_ring;
3851 *srisc_addr = 0;
3852 fwcode = (uint16_t *)blob->fw->data;
3853 fwclen = 0;
3854
3855 /* Validate firmware image by checking version. */
3856 if (blob->fw->size < 8 * sizeof(uint16_t)) {
3857 qla_printk(KERN_WARNING, ha,
3858 "Unable to verify integrity of firmware image (%Zd)!\n",
3859 blob->fw->size);
3860 goto fail_fw_integrity;
3861 }
3862 for (i = 0; i < 4; i++)
3863 wcode[i] = be16_to_cpu(fwcode[i + 4]);
3864 if ((wcode[0] == 0xffff && wcode[1] == 0xffff && wcode[2] == 0xffff &&
3865 wcode[3] == 0xffff) || (wcode[0] == 0 && wcode[1] == 0 &&
3866 wcode[2] == 0 && wcode[3] == 0)) {
3867 qla_printk(KERN_WARNING, ha,
3868 "Unable to verify integrity of firmware image!\n");
3869 qla_printk(KERN_WARNING, ha,
3870 "Firmware data: %04x %04x %04x %04x!\n", wcode[0],
3871 wcode[1], wcode[2], wcode[3]);
3872 goto fail_fw_integrity;
3873 }
3874
3875 seg = blob->segs;
3876 while (*seg && rval == QLA_SUCCESS) {
3877 risc_addr = *seg;
3878 *srisc_addr = *srisc_addr == 0 ? *seg : *srisc_addr;
3879 risc_size = be16_to_cpu(fwcode[3]);
3880
3881 /* Validate firmware image size. */
3882 fwclen += risc_size * sizeof(uint16_t);
3883 if (blob->fw->size < fwclen) {
3884 qla_printk(KERN_WARNING, ha,
3885 "Unable to verify integrity of firmware image "
3886 "(%Zd)!\n", blob->fw->size);
3887 goto fail_fw_integrity;
3888 }
3889
3890 fragment = 0;
3891 while (risc_size > 0 && rval == QLA_SUCCESS) {
3892 wlen = (uint16_t)(ha->fw_transfer_size >> 1);
3893 if (wlen > risc_size)
3894 wlen = risc_size;
3895
3896 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3897 "addr %x, number of words 0x%x.\n", ha->host_no,
3898 risc_addr, wlen));
3899
3900 for (i = 0; i < wlen; i++)
3901 wcode[i] = swab16(fwcode[i]);
3902
3903 rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3904 wlen);
3905 if (rval) {
3906 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3907 "segment %d of firmware\n", ha->host_no,
3908 fragment));
3909 qla_printk(KERN_WARNING, ha,
3910 "[ERROR] Failed to load segment %d of "
3911 "firmware\n", fragment);
3912 break;
3913 }
3914
3915 fwcode += wlen;
3916 risc_addr += wlen;
3917 risc_size -= wlen;
3918 fragment++;
3919 }
3920
3921 /* Next segment. */
3922 seg++;
3923 }
3924 return rval;
3925
3926fail_fw_integrity:
3927 return QLA_FUNCTION_FAILED;
3928}
3929
3930int
3931qla24xx_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
Andrew Vasquez01071092005-07-06 10:31:37 -07003932{
3933 int rval;
3934 int segments, fragment;
3935 uint32_t *dcode, dlen;
3936 uint32_t risc_addr;
3937 uint32_t risc_size;
3938 uint32_t i;
Andrew Vasquez54333832005-11-09 15:49:04 -08003939 struct fw_blob *blob;
Andrew Vasquez01071092005-07-06 10:31:37 -07003940 uint32_t *fwcode, fwclen;
3941
Andrew Vasquez54333832005-11-09 15:49:04 -08003942 /* Load firmware blob. */
3943 blob = qla2x00_request_firmware(ha);
3944 if (!blob) {
3945 qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
Andrew Vasquezd1c61902006-05-17 15:09:00 -07003946 qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
3947 "from: " QLA_FW_URL ".\n");
3948
3949 /* Try to load RISC code from flash. */
3950 qla_printk(KERN_ERR, ha, "Attempting to load (potentially "
3951 "outdated) firmware from flash.\n");
3952 return qla24xx_load_risc_flash(ha, srisc_addr);
Andrew Vasquez01071092005-07-06 10:31:37 -07003953 }
3954
3955 rval = QLA_SUCCESS;
3956
3957 segments = FA_RISC_CODE_SEGMENTS;
3958 dcode = (uint32_t *)ha->request_ring;
3959 *srisc_addr = 0;
Andrew Vasquez54333832005-11-09 15:49:04 -08003960 fwcode = (uint32_t *)blob->fw->data;
Andrew Vasquez01071092005-07-06 10:31:37 -07003961 fwclen = 0;
3962
3963 /* Validate firmware image by checking version. */
Andrew Vasquez54333832005-11-09 15:49:04 -08003964 if (blob->fw->size < 8 * sizeof(uint32_t)) {
Andrew Vasquez01071092005-07-06 10:31:37 -07003965 qla_printk(KERN_WARNING, ha,
Andrew Vasquez54333832005-11-09 15:49:04 -08003966 "Unable to verify integrity of firmware image (%Zd)!\n",
3967 blob->fw->size);
Andrew Vasquez01071092005-07-06 10:31:37 -07003968 goto fail_fw_integrity;
3969 }
3970 for (i = 0; i < 4; i++)
3971 dcode[i] = be32_to_cpu(fwcode[i + 4]);
3972 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
3973 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
3974 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
3975 dcode[3] == 0)) {
3976 qla_printk(KERN_WARNING, ha,
Andrew Vasquez54333832005-11-09 15:49:04 -08003977 "Unable to verify integrity of firmware image!\n");
Andrew Vasquez01071092005-07-06 10:31:37 -07003978 qla_printk(KERN_WARNING, ha,
3979 "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
3980 dcode[1], dcode[2], dcode[3]);
3981 goto fail_fw_integrity;
3982 }
3983
3984 while (segments && rval == QLA_SUCCESS) {
3985 risc_addr = be32_to_cpu(fwcode[2]);
3986 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
3987 risc_size = be32_to_cpu(fwcode[3]);
3988
3989 /* Validate firmware image size. */
3990 fwclen += risc_size * sizeof(uint32_t);
Andrew Vasquez54333832005-11-09 15:49:04 -08003991 if (blob->fw->size < fwclen) {
Andrew Vasquez01071092005-07-06 10:31:37 -07003992 qla_printk(KERN_WARNING, ha,
Andrew Vasquez54333832005-11-09 15:49:04 -08003993 "Unable to verify integrity of firmware image "
3994 "(%Zd)!\n", blob->fw->size);
3995
Andrew Vasquez01071092005-07-06 10:31:37 -07003996 goto fail_fw_integrity;
3997 }
3998
3999 fragment = 0;
4000 while (risc_size > 0 && rval == QLA_SUCCESS) {
4001 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
4002 if (dlen > risc_size)
4003 dlen = risc_size;
4004
4005 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
4006 "addr %x, number of dwords 0x%x.\n", ha->host_no,
4007 risc_addr, dlen));
4008
4009 for (i = 0; i < dlen; i++)
4010 dcode[i] = swab32(fwcode[i]);
4011
andrew.vasquez@qlogic.com590f98e2006-01-13 17:05:37 -08004012 rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
4013 dlen);
Andrew Vasquez01071092005-07-06 10:31:37 -07004014 if (rval) {
4015 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
4016 "segment %d of firmware\n", ha->host_no,
4017 fragment));
4018 qla_printk(KERN_WARNING, ha,
4019 "[ERROR] Failed to load segment %d of "
4020 "firmware\n", fragment);
4021 break;
4022 }
4023
4024 fwcode += dlen;
4025 risc_addr += dlen;
4026 risc_size -= dlen;
4027 fragment++;
4028 }
4029
4030 /* Next segment. */
4031 segments--;
4032 }
Andrew Vasquez01071092005-07-06 10:31:37 -07004033 return rval;
4034
4035fail_fw_integrity:
Andrew Vasquez01071092005-07-06 10:31:37 -07004036 return QLA_FUNCTION_FAILED;
Andrew Vasquez01071092005-07-06 10:31:37 -07004037}
Andrew Vasquez18c6c122006-10-13 09:33:38 -07004038
4039void
4040qla2x00_try_to_stop_firmware(scsi_qla_host_t *ha)
4041{
4042 int ret, retries;
4043
Andrew Vasqueze4289242007-07-19 15:05:56 -07004044 if (!IS_FWI2_CAPABLE(ha))
Andrew Vasquez18c6c122006-10-13 09:33:38 -07004045 return;
Andrew Vasquez75edf812007-05-07 07:43:00 -07004046 if (!ha->fw_major_version)
4047 return;
Andrew Vasquez18c6c122006-10-13 09:33:38 -07004048
4049 ret = qla2x00_stop_firmware(ha);
Andrew Vasquez7c7f1f22008-02-28 14:06:09 -08004050 for (retries = 5; ret != QLA_SUCCESS && ret != QLA_FUNCTION_TIMEOUT &&
4051 retries ; retries--) {
Andrew Vasquez8201e202008-07-24 08:31:46 -07004052 ha->isp_ops->reset_chip(ha);
4053 if (ha->isp_ops->chip_diag(ha) != QLA_SUCCESS)
Andrew Vasquez18c6c122006-10-13 09:33:38 -07004054 continue;
4055 if (qla2x00_setup_chip(ha) != QLA_SUCCESS)
4056 continue;
4057 qla_printk(KERN_INFO, ha,
4058 "Attempting retry of stop-firmware command...\n");
4059 ret = qla2x00_stop_firmware(ha);
4060 }
4061}
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07004062
4063int
4064qla24xx_configure_vhba(scsi_qla_host_t *ha)
4065{
4066 int rval = QLA_SUCCESS;
4067 uint16_t mb[MAILBOX_REGISTER_COUNT];
4068
4069 if (!ha->parent)
4070 return -EINVAL;
4071
Seokmann Jud4486fd2008-04-03 13:13:28 -07004072 rval = qla2x00_fw_ready(ha->parent);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07004073 if (rval == QLA_SUCCESS) {
4074 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
Seokmann Ju246de422008-07-10 16:55:55 -07004075 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07004076 }
4077
4078 ha->flags.management_server_logged_in = 0;
4079
4080 /* Login to SNS first */
Seokmann Jud4486fd2008-04-03 13:13:28 -07004081 qla24xx_login_fabric(ha->parent, NPH_SNS, 0xff, 0xff, 0xfc,
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07004082 mb, BIT_1);
4083 if (mb[0] != MBS_COMMAND_COMPLETE) {
4084 DEBUG15(qla_printk(KERN_INFO, ha,
4085 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
4086 "mb[2]=%x mb[6]=%x mb[7]=%x\n", NPH_SNS,
4087 mb[0], mb[1], mb[2], mb[6], mb[7]));
4088 return (QLA_FUNCTION_FAILED);
4089 }
4090
4091 atomic_set(&ha->loop_down_timer, 0);
4092 atomic_set(&ha->loop_state, LOOP_UP);
4093 set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
4094 set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
Seokmann Jud4486fd2008-04-03 13:13:28 -07004095 rval = qla2x00_loop_resync(ha->parent);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07004096
4097 return rval;
4098}
Harihara Kadayam4d4df192008-04-03 13:13:26 -07004099
4100/* 84XX Support **************************************************************/
4101
4102static LIST_HEAD(qla_cs84xx_list);
4103static DEFINE_MUTEX(qla_cs84xx_mutex);
4104
4105static struct qla_chip_state_84xx *
4106qla84xx_get_chip(struct scsi_qla_host *ha)
4107{
4108 struct qla_chip_state_84xx *cs84xx;
4109
4110 mutex_lock(&qla_cs84xx_mutex);
4111
4112 /* Find any shared 84xx chip. */
4113 list_for_each_entry(cs84xx, &qla_cs84xx_list, list) {
4114 if (cs84xx->bus == ha->pdev->bus) {
4115 kref_get(&cs84xx->kref);
4116 goto done;
4117 }
4118 }
4119
4120 cs84xx = kzalloc(sizeof(*cs84xx), GFP_KERNEL);
4121 if (!cs84xx)
4122 goto done;
4123
4124 kref_init(&cs84xx->kref);
4125 spin_lock_init(&cs84xx->access_lock);
4126 mutex_init(&cs84xx->fw_update_mutex);
4127 cs84xx->bus = ha->pdev->bus;
4128
4129 list_add_tail(&cs84xx->list, &qla_cs84xx_list);
4130done:
4131 mutex_unlock(&qla_cs84xx_mutex);
4132 return cs84xx;
4133}
4134
4135static void
4136__qla84xx_chip_release(struct kref *kref)
4137{
4138 struct qla_chip_state_84xx *cs84xx =
4139 container_of(kref, struct qla_chip_state_84xx, kref);
4140
4141 mutex_lock(&qla_cs84xx_mutex);
4142 list_del(&cs84xx->list);
4143 mutex_unlock(&qla_cs84xx_mutex);
4144 kfree(cs84xx);
4145}
4146
4147void
4148qla84xx_put_chip(struct scsi_qla_host *ha)
4149{
4150 if (ha->cs84xx)
4151 kref_put(&ha->cs84xx->kref, __qla84xx_chip_release);
4152}
4153
4154static int
4155qla84xx_init_chip(scsi_qla_host_t *ha)
4156{
4157 int rval;
4158 uint16_t status[2];
4159
4160 mutex_lock(&ha->cs84xx->fw_update_mutex);
4161
4162 rval = qla84xx_verify_chip(ha, status);
4163
4164 mutex_unlock(&ha->cs84xx->fw_update_mutex);
4165
4166 return rval != QLA_SUCCESS || status[0] ? QLA_FUNCTION_FAILED:
4167 QLA_SUCCESS;
4168}