blob: 916462ef19662bb4fc0ddc774f67f77d7978a83f [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 Vasquez0107109e2005-07-06 10:31:37 -070010#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
12#include "qla_devtbl.h"
13
David Miller4e08df32007-04-16 12:37:43 -070014#ifdef CONFIG_SPARC
15#include <asm/prom.h>
David Miller4e08df32007-04-16 12:37:43 -070016#endif
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018/*
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 Vasquez0107109e2005-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 Vasquezfd34f552007-07-19 15:06:00 -070086 ha->isp_ops->get_flash_version(ha, ha->request_ring);
Andrew Vasquez30c47662007-01-29 10:22:21 -080087
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 qla_printk(KERN_INFO, ha, "Configure NVRAM parameters...\n");
Andrew Vasquez0107109e2005-07-06 10:31:37 -070089
Andrew Vasquezfd34f552007-07-19 15:06:00 -070090 ha->isp_ops->nvram_config(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Andrew Vasquezd4c760c2006-06-23 16:10:39 -070092 if (ha->flags.disable_serdes) {
93 /* Mask HBA via NVRAM settings? */
94 qla_printk(KERN_INFO, ha, "Masking HBA WWPN "
95 "%02x%02x%02x%02x%02x%02x%02x%02x (via NVRAM).\n",
96 ha->port_name[0], ha->port_name[1],
97 ha->port_name[2], ha->port_name[3],
98 ha->port_name[4], ha->port_name[5],
99 ha->port_name[6], ha->port_name[7]);
100 return QLA_FUNCTION_FAILED;
101 }
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 qla_printk(KERN_INFO, ha, "Verifying loaded RISC code...\n");
104
Andrew Vasquezd19044c2006-11-22 08:22:19 -0800105 if (qla2x00_isp_firmware(ha) != QLA_SUCCESS) {
Andrew Vasquezfd34f552007-07-19 15:06:00 -0700106 rval = ha->isp_ops->chip_diag(ha);
Andrew Vasquezd19044c2006-11-22 08:22:19 -0800107 if (rval)
108 return (rval);
109 rval = qla2x00_setup_chip(ha);
110 if (rval)
111 return (rval);
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700112 qla2xxx_get_flash_info(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 }
Harihara Kadayam4d4df192008-04-03 13:13:26 -0700114 if (IS_QLA84XX(ha)) {
115 ha->cs84xx = qla84xx_get_chip(ha);
116 if (!ha->cs84xx) {
117 qla_printk(KERN_ERR, ha,
118 "Unable to configure ISP84XX.\n");
119 return QLA_FUNCTION_FAILED;
120 }
121 }
Andrew Vasquezd19044c2006-11-22 08:22:19 -0800122 rval = qla2x00_init_rings(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 return (rval);
125}
126
127/**
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700128 * qla2100_pci_config() - Setup ISP21xx PCI configuration registers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 * @ha: HA context
130 *
131 * Returns 0 on success.
132 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700133int
134qla2100_pci_config(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Andrew Vasqueza157b102007-05-07 07:43:01 -0700136 uint16_t w;
Adam Kropelin27b2f672005-09-16 19:28:20 -0700137 uint32_t d;
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700138 unsigned long flags;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700139 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 pci_set_master(ha->pdev);
Andrew Vasquezaf6177d2007-07-19 15:06:02 -0700142 pci_try_set_mwi(ha->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
Andrew Vasqueza157b102007-05-07 07:43:01 -0700145 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
147
148 /* Reset expansion ROM address decode enable */
Adam Kropelin27b2f672005-09-16 19:28:20 -0700149 pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
150 d &= ~PCI_ROM_ADDRESS_ENABLE;
151 pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700153 /* Get PCI bus information. */
154 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez3d716442005-07-06 10:30:26 -0700155 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700156 spin_unlock_irqrestore(&ha->hardware_lock, flags);
157
158 return QLA_SUCCESS;
159}
160
161/**
162 * qla2300_pci_config() - Setup ISP23xx PCI configuration registers.
163 * @ha: HA context
164 *
165 * Returns 0 on success.
166 */
167int
168qla2300_pci_config(scsi_qla_host_t *ha)
169{
Andrew Vasqueza157b102007-05-07 07:43:01 -0700170 uint16_t w;
Adam Kropelin27b2f672005-09-16 19:28:20 -0700171 uint32_t d;
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700172 unsigned long flags = 0;
173 uint32_t cnt;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700174 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700175
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700176 pci_set_master(ha->pdev);
Andrew Vasquezaf6177d2007-07-19 15:06:02 -0700177 pci_try_set_mwi(ha->pdev);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700178
179 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
Andrew Vasqueza157b102007-05-07 07:43:01 -0700180 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700181
182 if (IS_QLA2322(ha) || IS_QLA6322(ha))
183 w &= ~PCI_COMMAND_INTX_DISABLE;
Andrew Vasqueza157b102007-05-07 07:43:01 -0700184 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700185
186 /*
187 * If this is a 2300 card and not 2312, reset the
188 * COMMAND_INVALIDATE due to a bug in the 2300. Unfortunately,
189 * the 2310 also reports itself as a 2300 so we need to get the
190 * fb revision level -- a 6 indicates it really is a 2300 and
191 * not a 2310.
192 */
193 if (IS_QLA2300(ha)) {
194 spin_lock_irqsave(&ha->hardware_lock, flags);
195
196 /* Pause RISC. */
Andrew Vasquez3d716442005-07-06 10:30:26 -0700197 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700198 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquez3d716442005-07-06 10:30:26 -0700199 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700200 break;
201
202 udelay(10);
203 }
204
205 /* Select FPM registers. */
Andrew Vasquez3d716442005-07-06 10:30:26 -0700206 WRT_REG_WORD(&reg->ctrl_status, 0x20);
207 RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700208
209 /* Get the fb rev level */
Andrew Vasquez3d716442005-07-06 10:30:26 -0700210 ha->fb_rev = RD_FB_CMD_REG(ha, reg);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700211
212 if (ha->fb_rev == FPM_2300)
Andrew Vasqueza157b102007-05-07 07:43:01 -0700213 pci_clear_mwi(ha->pdev);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700214
215 /* Deselect FPM registers. */
Andrew Vasquez3d716442005-07-06 10:30:26 -0700216 WRT_REG_WORD(&reg->ctrl_status, 0x0);
217 RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700218
219 /* Release RISC module. */
Andrew Vasquez3d716442005-07-06 10:30:26 -0700220 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700221 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquez3d716442005-07-06 10:30:26 -0700222 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) == 0)
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700223 break;
224
225 udelay(10);
226 }
227
228 spin_unlock_irqrestore(&ha->hardware_lock, flags);
229 }
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700230
231 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
232
233 /* Reset expansion ROM address decode enable */
Adam Kropelin27b2f672005-09-16 19:28:20 -0700234 pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
235 d &= ~PCI_ROM_ADDRESS_ENABLE;
236 pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700237
238 /* Get PCI bus information. */
239 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez3d716442005-07-06 10:30:26 -0700240 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700241 spin_unlock_irqrestore(&ha->hardware_lock, flags);
242
243 return QLA_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
246/**
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700247 * qla24xx_pci_config() - Setup ISP24xx PCI configuration registers.
248 * @ha: HA context
249 *
250 * Returns 0 on success.
251 */
252int
253qla24xx_pci_config(scsi_qla_host_t *ha)
254{
Andrew Vasqueza157b102007-05-07 07:43:01 -0700255 uint16_t w;
Adam Kropelin27b2f672005-09-16 19:28:20 -0700256 uint32_t d;
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700257 unsigned long flags = 0;
258 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700259
260 pci_set_master(ha->pdev);
Andrew Vasquezaf6177d2007-07-19 15:06:02 -0700261 pci_try_set_mwi(ha->pdev);
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700262
263 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
Andrew Vasqueza157b102007-05-07 07:43:01 -0700264 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700265 w &= ~PCI_COMMAND_INTX_DISABLE;
266 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
267
268 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
269
270 /* PCI-X -- adjust Maximum Memory Read Byte Count (2048). */
Andrew Vasquezf85ec182007-07-19 15:06:01 -0700271 if (pci_find_capability(ha->pdev, PCI_CAP_ID_PCIX))
272 pcix_set_mmrbc(ha->pdev, 2048);
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700273
274 /* PCIe -- adjust Maximum Read Request Size (2048). */
Andrew Vasquezf85ec182007-07-19 15:06:01 -0700275 if (pci_find_capability(ha->pdev, PCI_CAP_ID_EXP))
276 pcie_set_readrq(ha->pdev, 2048);
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700277
278 /* Reset expansion ROM address decode enable */
Adam Kropelin27b2f672005-09-16 19:28:20 -0700279 pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
280 d &= ~PCI_ROM_ADDRESS_ENABLE;
281 pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700282
Auke Kok44c10132007-06-08 15:46:36 -0700283 ha->chip_revision = ha->pdev->revision;
Andrew Vasqueza8488ab2007-01-29 10:22:19 -0800284
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700285 /* Get PCI bus information. */
286 spin_lock_irqsave(&ha->hardware_lock, flags);
287 ha->pci_attr = RD_REG_DWORD(&reg->ctrl_status);
288 spin_unlock_irqrestore(&ha->hardware_lock, flags);
289
290 return QLA_SUCCESS;
291}
292
293/**
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -0700294 * qla25xx_pci_config() - Setup ISP25xx PCI configuration registers.
295 * @ha: HA context
296 *
297 * Returns 0 on success.
298 */
299int
300qla25xx_pci_config(scsi_qla_host_t *ha)
301{
302 uint16_t w;
303 uint32_t d;
304
305 pci_set_master(ha->pdev);
306 pci_try_set_mwi(ha->pdev);
307
308 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
309 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
310 w &= ~PCI_COMMAND_INTX_DISABLE;
311 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
312
313 /* PCIe -- adjust Maximum Read Request Size (2048). */
314 if (pci_find_capability(ha->pdev, PCI_CAP_ID_EXP))
315 pcie_set_readrq(ha->pdev, 2048);
316
317 /* Reset expansion ROM address decode enable */
318 pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
319 d &= ~PCI_ROM_ADDRESS_ENABLE;
320 pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
321
322 ha->chip_revision = ha->pdev->revision;
323
324 return QLA_SUCCESS;
325}
326
327/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 * qla2x00_isp_firmware() - Choose firmware image.
329 * @ha: HA context
330 *
331 * Returns 0 on success.
332 */
333static int
334qla2x00_isp_firmware(scsi_qla_host_t *ha)
335{
336 int rval;
337
338 /* Assume loading risc code */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700339 rval = QLA_FUNCTION_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 if (ha->flags.disable_risc_code_load) {
342 DEBUG2(printk("scsi(%ld): RISC CODE NOT loaded\n",
343 ha->host_no));
344 qla_printk(KERN_INFO, ha, "RISC CODE NOT loaded\n");
345
346 /* Verify checksum of loaded RISC code. */
Andrew Vasquez441d1072006-05-17 15:09:34 -0700347 rval = qla2x00_verify_checksum(ha, ha->fw_srisc_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
349
350 if (rval) {
351 DEBUG2_3(printk("scsi(%ld): **** Load RISC code ****\n",
352 ha->host_no));
353 }
354
355 return (rval);
356}
357
358/**
359 * qla2x00_reset_chip() - Reset ISP chip.
360 * @ha: HA context
361 *
362 * Returns 0 on success.
363 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700364void
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700365qla2x00_reset_chip(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
367 unsigned long flags = 0;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700368 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 uint32_t cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 uint16_t cmd;
371
Andrew Vasquezfd34f552007-07-19 15:06:00 -0700372 ha->isp_ops->disable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 spin_lock_irqsave(&ha->hardware_lock, flags);
375
376 /* Turn off master enable */
377 cmd = 0;
378 pci_read_config_word(ha->pdev, PCI_COMMAND, &cmd);
379 cmd &= ~PCI_COMMAND_MASTER;
380 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
381
382 if (!IS_QLA2100(ha)) {
383 /* Pause RISC. */
384 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
385 if (IS_QLA2200(ha) || IS_QLA2300(ha)) {
386 for (cnt = 0; cnt < 30000; cnt++) {
387 if ((RD_REG_WORD(&reg->hccr) &
388 HCCR_RISC_PAUSE) != 0)
389 break;
390 udelay(100);
391 }
392 } else {
393 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
394 udelay(10);
395 }
396
397 /* Select FPM registers. */
398 WRT_REG_WORD(&reg->ctrl_status, 0x20);
399 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
400
401 /* FPM Soft Reset. */
402 WRT_REG_WORD(&reg->fpm_diag_config, 0x100);
403 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
404
405 /* Toggle Fpm Reset. */
406 if (!IS_QLA2200(ha)) {
407 WRT_REG_WORD(&reg->fpm_diag_config, 0x0);
408 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
409 }
410
411 /* Select frame buffer registers. */
412 WRT_REG_WORD(&reg->ctrl_status, 0x10);
413 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
414
415 /* Reset frame buffer FIFOs. */
416 if (IS_QLA2200(ha)) {
417 WRT_FB_CMD_REG(ha, reg, 0xa000);
418 RD_FB_CMD_REG(ha, reg); /* PCI Posting. */
419 } else {
420 WRT_FB_CMD_REG(ha, reg, 0x00fc);
421
422 /* Read back fb_cmd until zero or 3 seconds max */
423 for (cnt = 0; cnt < 3000; cnt++) {
424 if ((RD_FB_CMD_REG(ha, reg) & 0xff) == 0)
425 break;
426 udelay(100);
427 }
428 }
429
430 /* Select RISC module registers. */
431 WRT_REG_WORD(&reg->ctrl_status, 0);
432 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
433
434 /* Reset RISC processor. */
435 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
436 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
437
438 /* Release RISC processor. */
439 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
440 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
441 }
442
443 WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
444 WRT_REG_WORD(&reg->hccr, HCCR_CLR_HOST_INT);
445
446 /* Reset ISP chip. */
447 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
448
449 /* Wait for RISC to recover from reset. */
450 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
451 /*
452 * It is necessary to for a delay here since the card doesn't
453 * respond to PCI reads during a reset. On some architectures
454 * this will result in an MCA.
455 */
456 udelay(20);
457 for (cnt = 30000; cnt; cnt--) {
458 if ((RD_REG_WORD(&reg->ctrl_status) &
459 CSR_ISP_SOFT_RESET) == 0)
460 break;
461 udelay(100);
462 }
463 } else
464 udelay(10);
465
466 /* Reset RISC processor. */
467 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
468
469 WRT_REG_WORD(&reg->semaphore, 0);
470
471 /* Release RISC processor. */
472 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
473 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
474
475 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
476 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquezffb39f02006-05-17 15:09:06 -0700477 if (RD_MAILBOX_REG(ha, reg, 0) != MBS_BUSY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 udelay(100);
481 }
482 } else
483 udelay(100);
484
485 /* Turn on master enable */
486 cmd |= PCI_COMMAND_MASTER;
487 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
488
489 /* Disable RISC pause on FPM parity error. */
490 if (!IS_QLA2100(ha)) {
491 WRT_REG_WORD(&reg->hccr, HCCR_DISABLE_PARITY_PAUSE);
492 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
493 }
494
495 spin_unlock_irqrestore(&ha->hardware_lock, flags);
496}
497
498/**
Andrew Vasquez88c26662005-07-08 17:59:26 -0700499 * qla24xx_reset_risc() - Perform full reset of ISP24xx RISC.
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700500 * @ha: HA context
501 *
502 * Returns 0 on success.
503 */
Andrew Vasquez88c26662005-07-08 17:59:26 -0700504static inline void
505qla24xx_reset_risc(scsi_qla_host_t *ha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700506{
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -0700507 int hw_evt = 0;
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700508 unsigned long flags = 0;
509 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
510 uint32_t cnt, d2;
Andrew Vasquez335a1cc2005-11-08 14:37:48 -0800511 uint16_t wd;
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700512
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700513 spin_lock_irqsave(&ha->hardware_lock, flags);
514
515 /* Reset RISC. */
516 WRT_REG_DWORD(&reg->ctrl_status, CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
517 for (cnt = 0; cnt < 30000; cnt++) {
518 if ((RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE) == 0)
519 break;
520
521 udelay(10);
522 }
523
524 WRT_REG_DWORD(&reg->ctrl_status,
525 CSRX_ISP_SOFT_RESET|CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
Andrew Vasquez335a1cc2005-11-08 14:37:48 -0800526 pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
Andrew Vasquez88c26662005-07-08 17:59:26 -0700527
Andrew Vasquez335a1cc2005-11-08 14:37:48 -0800528 udelay(100);
Andrew Vasquez88c26662005-07-08 17:59:26 -0700529 /* Wait for firmware to complete NVRAM accesses. */
Andrew Vasquez88c26662005-07-08 17:59:26 -0700530 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
531 for (cnt = 10000 ; cnt && d2; cnt--) {
532 udelay(5);
533 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
534 barrier();
535 }
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -0700536 if (cnt == 0)
537 hw_evt = 1;
Andrew Vasquez88c26662005-07-08 17:59:26 -0700538
Andrew Vasquez335a1cc2005-11-08 14:37:48 -0800539 /* Wait for soft-reset to complete. */
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700540 d2 = RD_REG_DWORD(&reg->ctrl_status);
541 for (cnt = 6000000 ; cnt && (d2 & CSRX_ISP_SOFT_RESET); cnt--) {
542 udelay(5);
543 d2 = RD_REG_DWORD(&reg->ctrl_status);
544 barrier();
545 }
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -0700546 if (cnt == 0 || hw_evt)
547 qla2xxx_hw_event_log(ha, HW_EVENT_RESET_ERR,
548 RD_REG_WORD(&reg->mailbox1), RD_REG_WORD(&reg->mailbox2),
549 RD_REG_WORD(&reg->mailbox3));
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700550
551 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
552 RD_REG_DWORD(&reg->hccr);
553
554 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
555 RD_REG_DWORD(&reg->hccr);
556
557 WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_RESET);
558 RD_REG_DWORD(&reg->hccr);
559
560 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
561 for (cnt = 6000000 ; cnt && d2; cnt--) {
562 udelay(5);
563 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
564 barrier();
565 }
566
567 spin_unlock_irqrestore(&ha->hardware_lock, flags);
568}
569
570/**
Andrew Vasquez88c26662005-07-08 17:59:26 -0700571 * qla24xx_reset_chip() - Reset ISP24xx chip.
572 * @ha: HA context
573 *
574 * Returns 0 on success.
575 */
576void
577qla24xx_reset_chip(scsi_qla_host_t *ha)
578{
Andrew Vasquezfd34f552007-07-19 15:06:00 -0700579 ha->isp_ops->disable_intrs(ha);
Andrew Vasquez88c26662005-07-08 17:59:26 -0700580
581 /* Perform RISC reset. */
582 qla24xx_reset_risc(ha);
583}
584
585/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 * qla2x00_chip_diag() - Test chip for proper operation.
587 * @ha: HA context
588 *
589 * Returns 0 on success.
590 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700591int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592qla2x00_chip_diag(scsi_qla_host_t *ha)
593{
594 int rval;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700595 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 unsigned long flags = 0;
597 uint16_t data;
598 uint32_t cnt;
599 uint16_t mb[5];
600
601 /* Assume a failed state */
602 rval = QLA_FUNCTION_FAILED;
603
604 DEBUG3(printk("scsi(%ld): Testing device at %lx.\n",
605 ha->host_no, (u_long)&reg->flash_address));
606
607 spin_lock_irqsave(&ha->hardware_lock, flags);
608
609 /* Reset ISP chip. */
610 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
611
612 /*
613 * We need to have a delay here since the card will not respond while
614 * in reset causing an MCA on some architectures.
615 */
616 udelay(20);
617 data = qla2x00_debounce_register(&reg->ctrl_status);
618 for (cnt = 6000000 ; cnt && (data & CSR_ISP_SOFT_RESET); cnt--) {
619 udelay(5);
620 data = RD_REG_WORD(&reg->ctrl_status);
621 barrier();
622 }
623
624 if (!cnt)
625 goto chip_diag_failed;
626
627 DEBUG3(printk("scsi(%ld): Reset register cleared by chip reset\n",
628 ha->host_no));
629
630 /* Reset RISC processor. */
631 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
632 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
633
634 /* Workaround for QLA2312 PCI parity error */
635 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
636 data = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 0));
637 for (cnt = 6000000; cnt && (data == MBS_BUSY); cnt--) {
638 udelay(5);
639 data = RD_MAILBOX_REG(ha, reg, 0);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700640 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
642 } else
643 udelay(10);
644
645 if (!cnt)
646 goto chip_diag_failed;
647
648 /* Check product ID of chip */
649 DEBUG3(printk("scsi(%ld): Checking product ID of chip\n", ha->host_no));
650
651 mb[1] = RD_MAILBOX_REG(ha, reg, 1);
652 mb[2] = RD_MAILBOX_REG(ha, reg, 2);
653 mb[3] = RD_MAILBOX_REG(ha, reg, 3);
654 mb[4] = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 4));
655 if (mb[1] != PROD_ID_1 || (mb[2] != PROD_ID_2 && mb[2] != PROD_ID_2a) ||
656 mb[3] != PROD_ID_3) {
657 qla_printk(KERN_WARNING, ha,
658 "Wrong product ID = 0x%x,0x%x,0x%x\n", mb[1], mb[2], mb[3]);
659
660 goto chip_diag_failed;
661 }
662 ha->product_id[0] = mb[1];
663 ha->product_id[1] = mb[2];
664 ha->product_id[2] = mb[3];
665 ha->product_id[3] = mb[4];
666
667 /* Adjust fw RISC transfer size */
668 if (ha->request_q_length > 1024)
669 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
670 else
671 ha->fw_transfer_size = REQUEST_ENTRY_SIZE *
672 ha->request_q_length;
673
674 if (IS_QLA2200(ha) &&
675 RD_MAILBOX_REG(ha, reg, 7) == QLA2200A_RISC_ROM_VER) {
676 /* Limit firmware transfer size with a 2200A */
677 DEBUG3(printk("scsi(%ld): Found QLA2200A chip.\n",
678 ha->host_no));
679
andrew.vasquez@qlogic.comea5b6382006-03-09 14:27:08 -0800680 ha->device_type |= DT_ISP2200A;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 ha->fw_transfer_size = 128;
682 }
683
684 /* Wrap Incoming Mailboxes Test. */
685 spin_unlock_irqrestore(&ha->hardware_lock, flags);
686
687 DEBUG3(printk("scsi(%ld): Checking mailboxes.\n", ha->host_no));
688 rval = qla2x00_mbx_reg_test(ha);
689 if (rval) {
690 DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
691 ha->host_no));
692 qla_printk(KERN_WARNING, ha,
693 "Failed mailbox send register test\n");
694 }
695 else {
696 /* Flag a successful rval */
697 rval = QLA_SUCCESS;
698 }
699 spin_lock_irqsave(&ha->hardware_lock, flags);
700
701chip_diag_failed:
702 if (rval)
703 DEBUG2_3(printk("scsi(%ld): Chip diagnostics **** FAILED "
704 "****\n", ha->host_no));
705
706 spin_unlock_irqrestore(&ha->hardware_lock, flags);
707
708 return (rval);
709}
710
711/**
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700712 * qla24xx_chip_diag() - Test ISP24xx for proper operation.
713 * @ha: HA context
714 *
715 * Returns 0 on success.
716 */
717int
718qla24xx_chip_diag(scsi_qla_host_t *ha)
719{
720 int rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700721
Andrew Vasquez88c26662005-07-08 17:59:26 -0700722 /* Perform RISC reset. */
723 qla24xx_reset_risc(ha);
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700724
725 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
726
727 rval = qla2x00_mbx_reg_test(ha);
728 if (rval) {
729 DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
730 ha->host_no));
731 qla_printk(KERN_WARNING, ha,
732 "Failed mailbox send register test\n");
733 } else {
734 /* Flag a successful rval */
735 rval = QLA_SUCCESS;
736 }
737
738 return rval;
739}
740
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700741void
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700742qla2x00_alloc_fw_dump(scsi_qla_host_t *ha)
743{
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700744 int rval;
745 uint32_t dump_size, fixed_size, mem_size, req_q_size, rsp_q_size,
Andrew Vasquezdf613b92008-01-17 09:02:17 -0800746 eft_size, fce_size;
747 dma_addr_t tc_dma;
748 void *tc;
Andrew Vasquezd4e3e042006-05-17 15:09:50 -0700749
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700750 if (ha->fw_dump) {
751 qla_printk(KERN_WARNING, ha,
752 "Firmware dump previously allocated.\n");
753 return;
Andrew Vasquezd4e3e042006-05-17 15:09:50 -0700754 }
755
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700756 ha->fw_dumped = 0;
Andrew Vasquezdf613b92008-01-17 09:02:17 -0800757 fixed_size = mem_size = eft_size = fce_size = 0;
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700758 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
759 fixed_size = sizeof(struct qla2100_fw_dump);
760 } else if (IS_QLA23XX(ha)) {
761 fixed_size = offsetof(struct qla2300_fw_dump, data_ram);
762 mem_size = (ha->fw_memory_size - 0x11000 + 1) *
763 sizeof(uint16_t);
Andrew Vasqueze4289242007-07-19 15:05:56 -0700764 } else if (IS_FWI2_CAPABLE(ha)) {
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -0700765 fixed_size = IS_QLA25XX(ha) ?
766 offsetof(struct qla25xx_fw_dump, ext_mem):
767 offsetof(struct qla24xx_fw_dump, ext_mem);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700768 mem_size = (ha->fw_memory_size - 0x100000 + 1) *
769 sizeof(uint32_t);
770
771 /* Allocate memory for Extended Trace Buffer. */
Andrew Vasquezdf613b92008-01-17 09:02:17 -0800772 tc = dma_alloc_coherent(&ha->pdev->dev, EFT_SIZE, &tc_dma,
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700773 GFP_KERNEL);
Andrew Vasquezdf613b92008-01-17 09:02:17 -0800774 if (!tc) {
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700775 qla_printk(KERN_WARNING, ha, "Unable to allocate "
776 "(%d KB) for EFT.\n", EFT_SIZE / 1024);
777 goto cont_alloc;
778 }
779
Andrew Vasquezfc447652008-01-17 09:02:18 -0800780 memset(tc, 0, EFT_SIZE);
Andrew Vasquezdf613b92008-01-17 09:02:17 -0800781 rval = qla2x00_enable_eft_trace(ha, tc_dma, EFT_NUM_BUFFERS);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700782 if (rval) {
783 qla_printk(KERN_WARNING, ha, "Unable to initialize "
784 "EFT (%d).\n", rval);
Andrew Vasquezdf613b92008-01-17 09:02:17 -0800785 dma_free_coherent(&ha->pdev->dev, EFT_SIZE, tc,
786 tc_dma);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700787 goto cont_alloc;
788 }
789
790 qla_printk(KERN_INFO, ha, "Allocated (%d KB) for EFT...\n",
791 EFT_SIZE / 1024);
792
793 eft_size = EFT_SIZE;
Andrew Vasquezdf613b92008-01-17 09:02:17 -0800794 ha->eft_dma = tc_dma;
795 ha->eft = tc;
796
797 /* Allocate memory for Fibre Channel Event Buffer. */
798 if (!IS_QLA25XX(ha))
799 goto cont_alloc;
800
801 tc = dma_alloc_coherent(&ha->pdev->dev, FCE_SIZE, &tc_dma,
802 GFP_KERNEL);
803 if (!tc) {
804 qla_printk(KERN_WARNING, ha, "Unable to allocate "
805 "(%d KB) for FCE.\n", FCE_SIZE / 1024);
806 goto cont_alloc;
807 }
808
809 memset(tc, 0, FCE_SIZE);
810 rval = qla2x00_enable_fce_trace(ha, tc_dma, FCE_NUM_BUFFERS,
811 ha->fce_mb, &ha->fce_bufs);
812 if (rval) {
813 qla_printk(KERN_WARNING, ha, "Unable to initialize "
814 "FCE (%d).\n", rval);
815 dma_free_coherent(&ha->pdev->dev, FCE_SIZE, tc,
816 tc_dma);
817 ha->flags.fce_enabled = 0;
818 goto cont_alloc;
819 }
820
821 qla_printk(KERN_INFO, ha, "Allocated (%d KB) for FCE...\n",
822 FCE_SIZE / 1024);
823
824 fce_size = sizeof(struct qla2xxx_fce_chain) + EFT_SIZE;
825 ha->flags.fce_enabled = 1;
826 ha->fce_dma = tc_dma;
827 ha->fce = tc;
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700828 }
829cont_alloc:
830 req_q_size = ha->request_q_length * sizeof(request_t);
831 rsp_q_size = ha->response_q_length * sizeof(response_t);
832
833 dump_size = offsetof(struct qla2xxx_fw_dump, isp);
834 dump_size += fixed_size + mem_size + req_q_size + rsp_q_size +
Andrew Vasquezdf613b92008-01-17 09:02:17 -0800835 eft_size + fce_size;
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700836
Andrew Vasquezd4e3e042006-05-17 15:09:50 -0700837 ha->fw_dump = vmalloc(dump_size);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700838 if (!ha->fw_dump) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700839 qla_printk(KERN_WARNING, ha, "Unable to allocate (%d KB) for "
Andrew Vasquezd4e3e042006-05-17 15:09:50 -0700840 "firmware dump!!!\n", dump_size / 1024);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700841
842 if (ha->eft) {
843 dma_free_coherent(&ha->pdev->dev, eft_size, ha->eft,
844 ha->eft_dma);
845 ha->eft = NULL;
846 ha->eft_dma = 0;
847 }
848 return;
849 }
850
851 qla_printk(KERN_INFO, ha, "Allocated (%d KB) for firmware dump...\n",
852 dump_size / 1024);
853
854 ha->fw_dump_len = dump_size;
855 ha->fw_dump->signature[0] = 'Q';
856 ha->fw_dump->signature[1] = 'L';
857 ha->fw_dump->signature[2] = 'G';
858 ha->fw_dump->signature[3] = 'C';
859 ha->fw_dump->version = __constant_htonl(1);
860
861 ha->fw_dump->fixed_size = htonl(fixed_size);
862 ha->fw_dump->mem_size = htonl(mem_size);
863 ha->fw_dump->req_q_size = htonl(req_q_size);
864 ha->fw_dump->rsp_q_size = htonl(rsp_q_size);
865
866 ha->fw_dump->eft_size = htonl(eft_size);
867 ha->fw_dump->eft_addr_l = htonl(LSD(ha->eft_dma));
868 ha->fw_dump->eft_addr_h = htonl(MSD(ha->eft_dma));
869
870 ha->fw_dump->header_size =
871 htonl(offsetof(struct qla2xxx_fw_dump, isp));
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700872}
873
874/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 * qla2x00_resize_request_q() - Resize request queue given available ISP memory.
876 * @ha: HA context
877 *
878 * Returns 0 on success.
879 */
880static void
881qla2x00_resize_request_q(scsi_qla_host_t *ha)
882{
883 int rval;
884 uint16_t fw_iocb_cnt = 0;
885 uint16_t request_q_length = REQUEST_ENTRY_CNT_2XXX_EXT_MEM;
886 dma_addr_t request_dma;
887 request_t *request_ring;
888
889 /* Valid only on recent ISPs. */
890 if (IS_QLA2100(ha) || IS_QLA2200(ha))
891 return;
892
893 /* Retrieve IOCB counts available to the firmware. */
Seokmann Ju4d0ea242007-09-20 14:07:43 -0700894 rval = qla2x00_get_resource_cnts(ha, NULL, NULL, NULL, &fw_iocb_cnt,
895 &ha->max_npiv_vports);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (rval)
897 return;
898 /* No point in continuing if current settings are sufficient. */
899 if (fw_iocb_cnt < 1024)
900 return;
901 if (ha->request_q_length >= request_q_length)
902 return;
903
904 /* Attempt to claim larger area for request queue. */
905 request_ring = dma_alloc_coherent(&ha->pdev->dev,
906 (request_q_length + 1) * sizeof(request_t), &request_dma,
907 GFP_KERNEL);
908 if (request_ring == NULL)
909 return;
910
911 /* Resize successful, report extensions. */
912 qla_printk(KERN_INFO, ha, "Extended memory detected (%d KB)...\n",
913 (ha->fw_memory_size + 1) / 1024);
914 qla_printk(KERN_INFO, ha, "Resizing request queue depth "
915 "(%d -> %d)...\n", ha->request_q_length, request_q_length);
916
917 /* Clear old allocations. */
918 dma_free_coherent(&ha->pdev->dev,
919 (ha->request_q_length + 1) * sizeof(request_t), ha->request_ring,
920 ha->request_dma);
921
922 /* Begin using larger queue. */
923 ha->request_q_length = request_q_length;
924 ha->request_ring = request_ring;
925 ha->request_dma = request_dma;
926}
927
928/**
929 * qla2x00_setup_chip() - Load and start RISC firmware.
930 * @ha: HA context
931 *
932 * Returns 0 on success.
933 */
934static int
935qla2x00_setup_chip(scsi_qla_host_t *ha)
936{
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700937 int rval;
938 uint32_t srisc_address = 0;
Andrew Vasquez3db06522008-01-31 12:33:49 -0800939 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
940 unsigned long flags;
941
942 if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
943 /* Disable SRAM, Instruction RAM and GP RAM parity. */
944 spin_lock_irqsave(&ha->hardware_lock, flags);
945 WRT_REG_WORD(&reg->hccr, (HCCR_ENABLE_PARITY + 0x0));
946 RD_REG_WORD(&reg->hccr);
947 spin_unlock_irqrestore(&ha->hardware_lock, flags);
948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950 /* Load firmware sequences */
Andrew Vasquezfd34f552007-07-19 15:06:00 -0700951 rval = ha->isp_ops->load_risc(ha, &srisc_address);
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700952 if (rval == QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 DEBUG(printk("scsi(%ld): Verifying Checksum of loaded RISC "
954 "code.\n", ha->host_no));
955
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700956 rval = qla2x00_verify_checksum(ha, srisc_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 if (rval == QLA_SUCCESS) {
958 /* Start firmware execution. */
959 DEBUG(printk("scsi(%ld): Checksum OK, start "
960 "firmware.\n", ha->host_no));
961
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700962 rval = qla2x00_execute_fw(ha, srisc_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 /* Retrieve firmware information. */
964 if (rval == QLA_SUCCESS && ha->fw_major_version == 0) {
965 qla2x00_get_fw_version(ha,
966 &ha->fw_major_version,
967 &ha->fw_minor_version,
968 &ha->fw_subminor_version,
969 &ha->fw_attributes, &ha->fw_memory_size);
970 qla2x00_resize_request_q(ha);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700971 ha->flags.npiv_supported = 0;
Seokmann Ju4d0ea242007-09-20 14:07:43 -0700972 if ((IS_QLA24XX(ha) || IS_QLA25XX(ha)) &&
973 (ha->fw_attributes & BIT_2)) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700974 ha->flags.npiv_supported = 1;
Seokmann Ju4d0ea242007-09-20 14:07:43 -0700975 if ((!ha->max_npiv_vports) ||
976 ((ha->max_npiv_vports + 1) %
Andrew Vasquezeb66dc62007-11-12 10:30:58 -0800977 MIN_MULTI_ID_FABRIC))
Seokmann Ju4d0ea242007-09-20 14:07:43 -0700978 ha->max_npiv_vports =
Andrew Vasquezeb66dc62007-11-12 10:30:58 -0800979 MIN_MULTI_ID_FABRIC - 1;
Seokmann Ju4d0ea242007-09-20 14:07:43 -0700980 }
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700981
982 if (ql2xallocfwdump)
983 qla2x00_alloc_fw_dump(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 }
985 } else {
986 DEBUG2(printk(KERN_INFO
987 "scsi(%ld): ISP Firmware failed checksum.\n",
988 ha->host_no));
989 }
990 }
991
Andrew Vasquez3db06522008-01-31 12:33:49 -0800992 if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
993 /* Enable proper parity. */
994 spin_lock_irqsave(&ha->hardware_lock, flags);
995 if (IS_QLA2300(ha))
996 /* SRAM parity */
997 WRT_REG_WORD(&reg->hccr, HCCR_ENABLE_PARITY + 0x1);
998 else
999 /* SRAM, Instruction RAM and GP RAM parity */
1000 WRT_REG_WORD(&reg->hccr, HCCR_ENABLE_PARITY + 0x7);
1001 RD_REG_WORD(&reg->hccr);
1002 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1003 }
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 if (rval) {
1006 DEBUG2_3(printk("scsi(%ld): Setup chip **** FAILED ****.\n",
1007 ha->host_no));
1008 }
1009
1010 return (rval);
1011}
1012
1013/**
1014 * qla2x00_init_response_q_entries() - Initializes response queue entries.
1015 * @ha: HA context
1016 *
1017 * Beginning of request ring has initialization control block already built
1018 * by nvram config routine.
1019 *
1020 * Returns 0 on success.
1021 */
1022static void
1023qla2x00_init_response_q_entries(scsi_qla_host_t *ha)
1024{
1025 uint16_t cnt;
1026 response_t *pkt;
1027
1028 pkt = ha->response_ring_ptr;
1029 for (cnt = 0; cnt < ha->response_q_length; cnt++) {
1030 pkt->signature = RESPONSE_PROCESSED;
1031 pkt++;
1032 }
1033
1034}
1035
1036/**
1037 * qla2x00_update_fw_options() - Read and process firmware options.
1038 * @ha: HA context
1039 *
1040 * Returns 0 on success.
1041 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001042void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043qla2x00_update_fw_options(scsi_qla_host_t *ha)
1044{
1045 uint16_t swing, emphasis, tx_sens, rx_sens;
1046
1047 memset(ha->fw_options, 0, sizeof(ha->fw_options));
1048 qla2x00_get_fw_options(ha, ha->fw_options);
1049
1050 if (IS_QLA2100(ha) || IS_QLA2200(ha))
1051 return;
1052
1053 /* Serial Link options. */
1054 DEBUG3(printk("scsi(%ld): Serial link options:\n",
1055 ha->host_no));
1056 DEBUG3(qla2x00_dump_buffer((uint8_t *)&ha->fw_seriallink_options,
1057 sizeof(ha->fw_seriallink_options)));
1058
1059 ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
1060 if (ha->fw_seriallink_options[3] & BIT_2) {
1061 ha->fw_options[1] |= FO1_SET_EMPHASIS_SWING;
1062
1063 /* 1G settings */
1064 swing = ha->fw_seriallink_options[2] & (BIT_2 | BIT_1 | BIT_0);
1065 emphasis = (ha->fw_seriallink_options[2] &
1066 (BIT_4 | BIT_3)) >> 3;
1067 tx_sens = ha->fw_seriallink_options[0] &
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001068 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 rx_sens = (ha->fw_seriallink_options[0] &
1070 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
1071 ha->fw_options[10] = (emphasis << 14) | (swing << 8);
1072 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
1073 if (rx_sens == 0x0)
1074 rx_sens = 0x3;
1075 ha->fw_options[10] |= (tx_sens << 4) | rx_sens;
1076 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
1077 ha->fw_options[10] |= BIT_5 |
1078 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
1079 (tx_sens & (BIT_1 | BIT_0));
1080
1081 /* 2G settings */
1082 swing = (ha->fw_seriallink_options[2] &
1083 (BIT_7 | BIT_6 | BIT_5)) >> 5;
1084 emphasis = ha->fw_seriallink_options[3] & (BIT_1 | BIT_0);
1085 tx_sens = ha->fw_seriallink_options[1] &
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001086 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 rx_sens = (ha->fw_seriallink_options[1] &
1088 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
1089 ha->fw_options[11] = (emphasis << 14) | (swing << 8);
1090 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
1091 if (rx_sens == 0x0)
1092 rx_sens = 0x3;
1093 ha->fw_options[11] |= (tx_sens << 4) | rx_sens;
1094 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
1095 ha->fw_options[11] |= BIT_5 |
1096 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
1097 (tx_sens & (BIT_1 | BIT_0));
1098 }
1099
1100 /* FCP2 options. */
1101 /* Return command IOCBs without waiting for an ABTS to complete. */
1102 ha->fw_options[3] |= BIT_13;
1103
1104 /* LED scheme. */
1105 if (ha->flags.enable_led_scheme)
1106 ha->fw_options[2] |= BIT_12;
1107
andrew.vasquez@qlogic.com48c02fd2006-03-09 14:27:18 -08001108 /* Detect ISP6312. */
1109 if (IS_QLA6312(ha))
1110 ha->fw_options[2] |= BIT_13;
1111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 /* Update firmware options. */
1113 qla2x00_set_fw_options(ha, ha->fw_options);
1114}
1115
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001116void
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001117qla24xx_update_fw_options(scsi_qla_host_t *ha)
1118{
1119 int rval;
1120
1121 /* Update Serial Link options. */
andrew.vasquez@qlogic.comf94097e2006-01-13 17:05:32 -08001122 if ((le16_to_cpu(ha->fw_seriallink_options24[0]) & BIT_0) == 0)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001123 return;
1124
andrew.vasquez@qlogic.comf94097e2006-01-13 17:05:32 -08001125 rval = qla2x00_set_serdes_params(ha,
1126 le16_to_cpu(ha->fw_seriallink_options24[1]),
1127 le16_to_cpu(ha->fw_seriallink_options24[2]),
1128 le16_to_cpu(ha->fw_seriallink_options24[3]));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001129 if (rval != QLA_SUCCESS) {
1130 qla_printk(KERN_WARNING, ha,
1131 "Unable to update Serial Link options (%x).\n", rval);
1132 }
1133}
1134
1135void
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001136qla2x00_config_rings(struct scsi_qla_host *ha)
1137{
Andrew Vasquez3d716442005-07-06 10:30:26 -07001138 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001139
1140 /* Setup ring parameters in initialization control block. */
1141 ha->init_cb->request_q_outpointer = __constant_cpu_to_le16(0);
1142 ha->init_cb->response_q_inpointer = __constant_cpu_to_le16(0);
1143 ha->init_cb->request_q_length = cpu_to_le16(ha->request_q_length);
1144 ha->init_cb->response_q_length = cpu_to_le16(ha->response_q_length);
1145 ha->init_cb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
1146 ha->init_cb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
1147 ha->init_cb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
1148 ha->init_cb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
1149
1150 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), 0);
1151 WRT_REG_WORD(ISP_REQ_Q_OUT(ha, reg), 0);
1152 WRT_REG_WORD(ISP_RSP_Q_IN(ha, reg), 0);
1153 WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), 0);
1154 RD_REG_WORD(ISP_RSP_Q_OUT(ha, reg)); /* PCI Posting. */
1155}
1156
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001157void
1158qla24xx_config_rings(struct scsi_qla_host *ha)
1159{
1160 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1161 struct init_cb_24xx *icb;
1162
1163 /* Setup ring parameters in initialization control block. */
1164 icb = (struct init_cb_24xx *)ha->init_cb;
1165 icb->request_q_outpointer = __constant_cpu_to_le16(0);
1166 icb->response_q_inpointer = __constant_cpu_to_le16(0);
1167 icb->request_q_length = cpu_to_le16(ha->request_q_length);
1168 icb->response_q_length = cpu_to_le16(ha->response_q_length);
1169 icb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
1170 icb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
1171 icb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
1172 icb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
1173
1174 WRT_REG_DWORD(&reg->req_q_in, 0);
1175 WRT_REG_DWORD(&reg->req_q_out, 0);
1176 WRT_REG_DWORD(&reg->rsp_q_in, 0);
1177 WRT_REG_DWORD(&reg->rsp_q_out, 0);
1178 RD_REG_DWORD(&reg->rsp_q_out);
1179}
1180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181/**
1182 * qla2x00_init_rings() - Initializes firmware.
1183 * @ha: HA context
1184 *
1185 * Beginning of request ring has initialization control block already built
1186 * by nvram config routine.
1187 *
1188 * Returns 0 on success.
1189 */
1190static int
1191qla2x00_init_rings(scsi_qla_host_t *ha)
1192{
1193 int rval;
1194 unsigned long flags = 0;
1195 int cnt;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001196 struct mid_init_cb_24xx *mid_init_cb =
1197 (struct mid_init_cb_24xx *) ha->init_cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199 spin_lock_irqsave(&ha->hardware_lock, flags);
1200
1201 /* Clear outstanding commands array. */
1202 for (cnt = 0; cnt < MAX_OUTSTANDING_COMMANDS; cnt++)
1203 ha->outstanding_cmds[cnt] = NULL;
1204
1205 ha->current_outstanding_cmd = 0;
1206
1207 /* Clear RSCN queue. */
1208 ha->rscn_in_ptr = 0;
1209 ha->rscn_out_ptr = 0;
1210
1211 /* Initialize firmware. */
1212 ha->request_ring_ptr = ha->request_ring;
1213 ha->req_ring_index = 0;
1214 ha->req_q_cnt = ha->request_q_length;
1215 ha->response_ring_ptr = ha->response_ring;
1216 ha->rsp_ring_index = 0;
1217
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 /* Initialize response queue entries */
1219 qla2x00_init_response_q_entries(ha);
1220
Andrew Vasquezfd34f552007-07-19 15:06:00 -07001221 ha->isp_ops->config_rings(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
1223 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1224
1225 /* Update any ISP specific firmware options before initialization. */
Andrew Vasquezfd34f552007-07-19 15:06:00 -07001226 ha->isp_ops->update_fw_options(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 DEBUG(printk("scsi(%ld): Issue init firmware.\n", ha->host_no));
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001229
Seokmann Juc48339d2008-01-17 09:02:19 -08001230 if (ha->flags.npiv_supported)
1231 mid_init_cb->count = cpu_to_le16(ha->max_npiv_vports);
1232
Andrew Vasquezeb66dc62007-11-12 10:30:58 -08001233 mid_init_cb->options = __constant_cpu_to_le16(BIT_1);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001234
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001235 rval = qla2x00_init_firmware(ha, ha->init_cb_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 if (rval) {
1237 DEBUG2_3(printk("scsi(%ld): Init firmware **** FAILED ****.\n",
1238 ha->host_no));
1239 } else {
1240 DEBUG3(printk("scsi(%ld): Init firmware -- success.\n",
1241 ha->host_no));
1242 }
1243
1244 return (rval);
1245}
1246
1247/**
1248 * qla2x00_fw_ready() - Waits for firmware ready.
1249 * @ha: HA context
1250 *
1251 * Returns 0 on success.
1252 */
1253static int
1254qla2x00_fw_ready(scsi_qla_host_t *ha)
1255{
1256 int rval;
Harihara Kadayam4d4df192008-04-03 13:13:26 -07001257 unsigned long wtime, mtime, cs84xx_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 uint16_t min_wait; /* Minimum wait time if loop is down */
1259 uint16_t wait_time; /* Wait time if loop is coming ready */
Harihara Kadayam4d4df192008-04-03 13:13:26 -07001260 uint16_t state[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
1262 rval = QLA_SUCCESS;
1263
1264 /* 20 seconds for loop down. */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001265 min_wait = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 /*
1268 * Firmware should take at most one RATOV to login, plus 5 seconds for
1269 * our own processing.
1270 */
1271 if ((wait_time = (ha->retry_count*ha->login_timeout) + 5) < min_wait) {
1272 wait_time = min_wait;
1273 }
1274
1275 /* Min wait time if loop down */
1276 mtime = jiffies + (min_wait * HZ);
1277
1278 /* wait time before firmware ready */
1279 wtime = jiffies + (wait_time * HZ);
1280
1281 /* Wait for ISP to finish LIP */
1282 if (!ha->flags.init_done)
1283 qla_printk(KERN_INFO, ha, "Waiting for LIP to complete...\n");
1284
1285 DEBUG3(printk("scsi(%ld): Waiting for LIP to complete...\n",
1286 ha->host_no));
1287
1288 do {
Harihara Kadayam4d4df192008-04-03 13:13:26 -07001289 rval = qla2x00_get_firmware_state(ha, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 if (rval == QLA_SUCCESS) {
Harihara Kadayam4d4df192008-04-03 13:13:26 -07001291 if (state[0] < FSTATE_LOSS_OF_SYNC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 ha->device_flags &= ~DFLG_NO_CABLE;
1293 }
Harihara Kadayam4d4df192008-04-03 13:13:26 -07001294 if (IS_QLA84XX(ha) && state[0] != FSTATE_READY) {
1295 DEBUG16(printk("scsi(%ld): fw_state=%x "
1296 "84xx=%x.\n", ha->host_no, state[0],
1297 state[2]));
1298 if ((state[2] & FSTATE_LOGGED_IN) &&
1299 (state[2] & FSTATE_WAITING_FOR_VERIFY)) {
1300 DEBUG16(printk("scsi(%ld): Sending "
1301 "verify iocb.\n", ha->host_no));
1302
1303 cs84xx_time = jiffies;
1304 rval = qla84xx_init_chip(ha);
1305 if (rval != QLA_SUCCESS)
1306 break;
1307
1308 /* Add time taken to initialize. */
1309 cs84xx_time = jiffies - cs84xx_time;
1310 wtime += cs84xx_time;
1311 mtime += cs84xx_time;
1312 DEBUG16(printk("scsi(%ld): Increasing "
1313 "wait time by %ld. New time %ld\n",
1314 ha->host_no, cs84xx_time, wtime));
1315 }
1316 } else if (state[0] == FSTATE_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 DEBUG(printk("scsi(%ld): F/W Ready - OK \n",
1318 ha->host_no));
1319
1320 qla2x00_get_retry_cnt(ha, &ha->retry_count,
1321 &ha->login_timeout, &ha->r_a_tov);
1322
1323 rval = QLA_SUCCESS;
1324 break;
1325 }
1326
1327 rval = QLA_FUNCTION_FAILED;
1328
1329 if (atomic_read(&ha->loop_down_timer) &&
Harihara Kadayam4d4df192008-04-03 13:13:26 -07001330 state[0] != FSTATE_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 /* Loop down. Timeout on min_wait for states
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001332 * other than Wait for Login.
1333 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 if (time_after_eq(jiffies, mtime)) {
1335 qla_printk(KERN_INFO, ha,
1336 "Cable is unplugged...\n");
1337
1338 ha->device_flags |= DFLG_NO_CABLE;
1339 break;
1340 }
1341 }
1342 } else {
1343 /* Mailbox cmd failed. Timeout on min_wait. */
1344 if (time_after_eq(jiffies, mtime))
1345 break;
1346 }
1347
1348 if (time_after_eq(jiffies, wtime))
1349 break;
1350
1351 /* Delay for a while */
1352 msleep(500);
1353
1354 DEBUG3(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
Harihara Kadayam4d4df192008-04-03 13:13:26 -07001355 ha->host_no, state[0], jiffies));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 } while (1);
1357
1358 DEBUG(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
Harihara Kadayam4d4df192008-04-03 13:13:26 -07001359 ha->host_no, state[0], jiffies));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361 if (rval) {
1362 DEBUG2_3(printk("scsi(%ld): Firmware ready **** FAILED ****.\n",
1363 ha->host_no));
1364 }
1365
1366 return (rval);
1367}
1368
1369/*
1370* qla2x00_configure_hba
1371* Setup adapter context.
1372*
1373* Input:
1374* ha = adapter state pointer.
1375*
1376* Returns:
1377* 0 = success
1378*
1379* Context:
1380* Kernel context.
1381*/
1382static int
1383qla2x00_configure_hba(scsi_qla_host_t *ha)
1384{
1385 int rval;
1386 uint16_t loop_id;
1387 uint16_t topo;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001388 uint16_t sw_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 uint8_t al_pa;
1390 uint8_t area;
1391 uint8_t domain;
1392 char connect_type[22];
1393
1394 /* Get host addresses. */
1395 rval = qla2x00_get_adapter_id(ha,
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001396 &loop_id, &al_pa, &area, &domain, &topo, &sw_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 if (rval != QLA_SUCCESS) {
Andrew Vasquez23443b12005-12-06 10:57:06 -08001398 if (LOOP_TRANSITION(ha) || atomic_read(&ha->loop_down_timer) ||
Ravi Anand33135aa2005-11-08 14:37:20 -08001399 (rval == QLA_COMMAND_ERROR && loop_id == 0x7)) {
1400 DEBUG2(printk("%s(%ld) Loop is in a transition state\n",
1401 __func__, ha->host_no));
1402 } else {
1403 qla_printk(KERN_WARNING, ha,
1404 "ERROR -- Unable to get host loop ID.\n");
1405 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
1406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 return (rval);
1408 }
1409
1410 if (topo == 4) {
1411 qla_printk(KERN_INFO, ha,
1412 "Cannot get topology - retrying.\n");
1413 return (QLA_FUNCTION_FAILED);
1414 }
1415
1416 ha->loop_id = loop_id;
1417
1418 /* initialize */
1419 ha->min_external_loopid = SNS_FIRST_LOOP_ID;
1420 ha->operating_mode = LOOP;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001421 ha->switch_cap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
1423 switch (topo) {
1424 case 0:
1425 DEBUG3(printk("scsi(%ld): HBA in NL topology.\n",
1426 ha->host_no));
1427 ha->current_topology = ISP_CFG_NL;
1428 strcpy(connect_type, "(Loop)");
1429 break;
1430
1431 case 1:
1432 DEBUG3(printk("scsi(%ld): HBA in FL topology.\n",
1433 ha->host_no));
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001434 ha->switch_cap = sw_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 ha->current_topology = ISP_CFG_FL;
1436 strcpy(connect_type, "(FL_Port)");
1437 break;
1438
1439 case 2:
1440 DEBUG3(printk("scsi(%ld): HBA in N P2P topology.\n",
1441 ha->host_no));
1442 ha->operating_mode = P2P;
1443 ha->current_topology = ISP_CFG_N;
1444 strcpy(connect_type, "(N_Port-to-N_Port)");
1445 break;
1446
1447 case 3:
1448 DEBUG3(printk("scsi(%ld): HBA in F P2P topology.\n",
1449 ha->host_no));
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001450 ha->switch_cap = sw_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 ha->operating_mode = P2P;
1452 ha->current_topology = ISP_CFG_F;
1453 strcpy(connect_type, "(F_Port)");
1454 break;
1455
1456 default:
1457 DEBUG3(printk("scsi(%ld): HBA in unknown topology %x. "
1458 "Using NL.\n",
1459 ha->host_no, topo));
1460 ha->current_topology = ISP_CFG_NL;
1461 strcpy(connect_type, "(Loop)");
1462 break;
1463 }
1464
1465 /* Save Host port and loop ID. */
1466 /* byte order - Big Endian */
1467 ha->d_id.b.domain = domain;
1468 ha->d_id.b.area = area;
1469 ha->d_id.b.al_pa = al_pa;
1470
1471 if (!ha->flags.init_done)
1472 qla_printk(KERN_INFO, ha,
1473 "Topology - %s, Host Loop address 0x%x\n",
1474 connect_type, ha->loop_id);
1475
1476 if (rval) {
1477 DEBUG2_3(printk("scsi(%ld): FAILED.\n", ha->host_no));
1478 } else {
1479 DEBUG3(printk("scsi(%ld): exiting normally.\n", ha->host_no));
1480 }
1481
1482 return(rval);
1483}
1484
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08001485static inline void
1486qla2x00_set_model_info(scsi_qla_host_t *ha, uint8_t *model, size_t len, char *def)
1487{
1488 char *st, *en;
1489 uint16_t index;
1490
1491 if (memcmp(model, BINZERO, len) != 0) {
1492 strncpy(ha->model_number, model, len);
1493 st = en = ha->model_number;
1494 en += len - 1;
1495 while (en > st) {
1496 if (*en != 0x20 && *en != 0x00)
1497 break;
1498 *en-- = '\0';
1499 }
1500
1501 index = (ha->pdev->subsystem_device & 0xff);
1502 if (ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
1503 index < QLA_MODEL_NAMES)
1504 ha->model_desc = qla2x00_model_name[index * 2 + 1];
1505 } else {
1506 index = (ha->pdev->subsystem_device & 0xff);
1507 if (ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
1508 index < QLA_MODEL_NAMES) {
1509 strcpy(ha->model_number,
1510 qla2x00_model_name[index * 2]);
1511 ha->model_desc = qla2x00_model_name[index * 2 + 1];
1512 } else {
1513 strcpy(ha->model_number, def);
1514 }
1515 }
1516}
1517
David Miller4e08df32007-04-16 12:37:43 -07001518/* On sparc systems, obtain port and node WWN from firmware
1519 * properties.
1520 */
1521static void qla2xxx_nvram_wwn_from_ofw(scsi_qla_host_t *ha, nvram_t *nv)
1522{
1523#ifdef CONFIG_SPARC
1524 struct pci_dev *pdev = ha->pdev;
David S. Miller15576bc2007-05-08 00:36:49 -07001525 struct device_node *dp = pci_device_to_OF_node(pdev);
1526 const u8 *val;
David Miller4e08df32007-04-16 12:37:43 -07001527 int len;
1528
1529 val = of_get_property(dp, "port-wwn", &len);
1530 if (val && len >= WWN_SIZE)
1531 memcpy(nv->port_name, val, WWN_SIZE);
1532
1533 val = of_get_property(dp, "node-wwn", &len);
1534 if (val && len >= WWN_SIZE)
1535 memcpy(nv->node_name, val, WWN_SIZE);
1536#endif
1537}
1538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539/*
1540* NVRAM configuration for ISP 2xxx
1541*
1542* Input:
1543* ha = adapter block pointer.
1544*
1545* Output:
1546* initialization control block in response_ring
1547* host adapters parameters in host adapter block
1548*
1549* Returns:
1550* 0 = success.
1551*/
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001552int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553qla2x00_nvram_config(scsi_qla_host_t *ha)
1554{
David Miller4e08df32007-04-16 12:37:43 -07001555 int rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001556 uint8_t chksum = 0;
1557 uint16_t cnt;
1558 uint8_t *dptr1, *dptr2;
1559 init_cb_t *icb = ha->init_cb;
Seokmann Ju281afe12007-07-26 13:43:34 -07001560 nvram_t *nv = ha->nvram;
1561 uint8_t *ptr = ha->nvram;
Andrew Vasquez3d716442005-07-06 10:30:26 -07001562 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
David Miller4e08df32007-04-16 12:37:43 -07001564 rval = QLA_SUCCESS;
1565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 /* Determine NVRAM starting address. */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001567 ha->nvram_size = sizeof(nvram_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 ha->nvram_base = 0;
1569 if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha))
1570 if ((RD_REG_WORD(&reg->ctrl_status) >> 14) == 1)
1571 ha->nvram_base = 0x80;
1572
1573 /* Get NVRAM data and calculate checksum. */
Andrew Vasquezfd34f552007-07-19 15:06:00 -07001574 ha->isp_ops->read_nvram(ha, ptr, ha->nvram_base, ha->nvram_size);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001575 for (cnt = 0, chksum = 0; cnt < ha->nvram_size; cnt++)
1576 chksum += *ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
1578 DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
Seokmann Ju281afe12007-07-26 13:43:34 -07001579 DEBUG5(qla2x00_dump_buffer((uint8_t *)nv, ha->nvram_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
1581 /* Bad NVRAM data, set defaults parameters. */
1582 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' ||
1583 nv->id[2] != 'P' || nv->id[3] != ' ' || nv->nvram_version < 1) {
1584 /* Reset NVRAM data. */
1585 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
1586 "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
1587 nv->nvram_version);
David Miller4e08df32007-04-16 12:37:43 -07001588 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
1589 "invalid -- WWPN) defaults.\n");
1590
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -07001591 if (chksum)
1592 qla2xxx_hw_event_log(ha, HW_EVENT_NVRAM_CHKSUM_ERR, 0,
1593 MSW(chksum), LSW(chksum));
1594
David Miller4e08df32007-04-16 12:37:43 -07001595 /*
1596 * Set default initialization control block.
1597 */
1598 memset(nv, 0, ha->nvram_size);
1599 nv->parameter_block_version = ICB_VERSION;
1600
1601 if (IS_QLA23XX(ha)) {
1602 nv->firmware_options[0] = BIT_2 | BIT_1;
1603 nv->firmware_options[1] = BIT_7 | BIT_5;
1604 nv->add_firmware_options[0] = BIT_5;
1605 nv->add_firmware_options[1] = BIT_5 | BIT_4;
1606 nv->frame_payload_size = __constant_cpu_to_le16(2048);
1607 nv->special_options[1] = BIT_7;
1608 } else if (IS_QLA2200(ha)) {
1609 nv->firmware_options[0] = BIT_2 | BIT_1;
1610 nv->firmware_options[1] = BIT_7 | BIT_5;
1611 nv->add_firmware_options[0] = BIT_5;
1612 nv->add_firmware_options[1] = BIT_5 | BIT_4;
1613 nv->frame_payload_size = __constant_cpu_to_le16(1024);
1614 } else if (IS_QLA2100(ha)) {
1615 nv->firmware_options[0] = BIT_3 | BIT_1;
1616 nv->firmware_options[1] = BIT_5;
1617 nv->frame_payload_size = __constant_cpu_to_le16(1024);
1618 }
1619
1620 nv->max_iocb_allocation = __constant_cpu_to_le16(256);
1621 nv->execution_throttle = __constant_cpu_to_le16(16);
1622 nv->retry_count = 8;
1623 nv->retry_delay = 1;
1624
1625 nv->port_name[0] = 33;
1626 nv->port_name[3] = 224;
1627 nv->port_name[4] = 139;
1628
1629 qla2xxx_nvram_wwn_from_ofw(ha, nv);
1630
1631 nv->login_timeout = 4;
1632
1633 /*
1634 * Set default host adapter parameters
1635 */
1636 nv->host_p[1] = BIT_2;
1637 nv->reset_delay = 5;
1638 nv->port_down_retry_count = 8;
1639 nv->max_luns_per_target = __constant_cpu_to_le16(8);
1640 nv->link_down_timeout = 60;
1641
1642 rval = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 }
1644
1645#if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
1646 /*
1647 * The SN2 does not provide BIOS emulation which means you can't change
1648 * potentially bogus BIOS settings. Force the use of default settings
1649 * for link rate and frame size. Hope that the rest of the settings
1650 * are valid.
1651 */
1652 if (ia64_platform_is("sn2")) {
1653 nv->frame_payload_size = __constant_cpu_to_le16(2048);
1654 if (IS_QLA23XX(ha))
1655 nv->special_options[1] = BIT_7;
1656 }
1657#endif
1658
1659 /* Reset Initialization control block */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001660 memset(icb, 0, ha->init_cb_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
1662 /*
1663 * Setup driver NVRAM options.
1664 */
1665 nv->firmware_options[0] |= (BIT_6 | BIT_1);
1666 nv->firmware_options[0] &= ~(BIT_5 | BIT_4);
1667 nv->firmware_options[1] |= (BIT_5 | BIT_0);
1668 nv->firmware_options[1] &= ~BIT_4;
1669
1670 if (IS_QLA23XX(ha)) {
1671 nv->firmware_options[0] |= BIT_2;
1672 nv->firmware_options[0] &= ~BIT_3;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001673 nv->add_firmware_options[1] |= BIT_5 | BIT_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
1675 if (IS_QLA2300(ha)) {
1676 if (ha->fb_rev == FPM_2310) {
1677 strcpy(ha->model_number, "QLA2310");
1678 } else {
1679 strcpy(ha->model_number, "QLA2300");
1680 }
1681 } else {
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08001682 qla2x00_set_model_info(ha, nv->model_number,
1683 sizeof(nv->model_number), "QLA23xx");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 }
1685 } else if (IS_QLA2200(ha)) {
1686 nv->firmware_options[0] |= BIT_2;
1687 /*
1688 * 'Point-to-point preferred, else loop' is not a safe
1689 * connection mode setting.
1690 */
1691 if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
1692 (BIT_5 | BIT_4)) {
1693 /* Force 'loop preferred, else point-to-point'. */
1694 nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
1695 nv->add_firmware_options[0] |= BIT_5;
1696 }
1697 strcpy(ha->model_number, "QLA22xx");
1698 } else /*if (IS_QLA2100(ha))*/ {
1699 strcpy(ha->model_number, "QLA2100");
1700 }
1701
1702 /*
1703 * Copy over NVRAM RISC parameter block to initialization control block.
1704 */
1705 dptr1 = (uint8_t *)icb;
1706 dptr2 = (uint8_t *)&nv->parameter_block_version;
1707 cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
1708 while (cnt--)
1709 *dptr1++ = *dptr2++;
1710
1711 /* Copy 2nd half. */
1712 dptr1 = (uint8_t *)icb->add_firmware_options;
1713 cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
1714 while (cnt--)
1715 *dptr1++ = *dptr2++;
1716
Andrew Vasquez5341e862006-05-17 15:09:16 -07001717 /* Use alternate WWN? */
1718 if (nv->host_p[1] & BIT_7) {
1719 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
1720 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
1721 }
1722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 /* Prepare nodename */
1724 if ((icb->firmware_options[1] & BIT_6) == 0) {
1725 /*
1726 * Firmware will apply the following mask if the nodename was
1727 * not provided.
1728 */
1729 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
1730 icb->node_name[0] &= 0xF0;
1731 }
1732
1733 /*
1734 * Set host adapter parameters.
1735 */
Andrew Vasquez01819442006-06-23 16:11:10 -07001736 if (nv->host_p[0] & BIT_7)
Andrew Vasquez11010fe2006-10-06 09:54:59 -07001737 ql2xextended_error_logging = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
1739 /* Always load RISC code on non ISP2[12]00 chips. */
1740 if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
1741 ha->flags.disable_risc_code_load = 0;
1742 ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
1743 ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
1744 ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
Andrew Vasquez06c22bd2005-08-26 19:09:00 -07001745 ha->flags.enable_led_scheme = (nv->special_options[1] & BIT_4) ? 1 : 0;
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07001746 ha->flags.disable_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
1748 ha->operating_mode =
1749 (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
1750
1751 memcpy(ha->fw_seriallink_options, nv->seriallink_options,
1752 sizeof(ha->fw_seriallink_options));
1753
1754 /* save HBA serial number */
1755 ha->serial0 = icb->port_name[5];
1756 ha->serial1 = icb->port_name[6];
1757 ha->serial2 = icb->port_name[7];
Andrew Vasquez3d716442005-07-06 10:30:26 -07001758 ha->node_name = icb->node_name;
1759 ha->port_name = icb->port_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
1761 icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
1762
1763 ha->retry_count = nv->retry_count;
1764
1765 /* Set minimum login_timeout to 4 seconds. */
1766 if (nv->login_timeout < ql2xlogintimeout)
1767 nv->login_timeout = ql2xlogintimeout;
1768 if (nv->login_timeout < 4)
1769 nv->login_timeout = 4;
1770 ha->login_timeout = nv->login_timeout;
1771 icb->login_timeout = nv->login_timeout;
1772
Andrew Vasquez00a537b2008-02-28 14:06:11 -08001773 /* Set minimum RATOV to 100 tenths of a second. */
1774 ha->r_a_tov = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 ha->loop_reset_delay = nv->reset_delay;
1777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 /* Link Down Timeout = 0:
1779 *
1780 * When Port Down timer expires we will start returning
1781 * I/O's to OS with "DID_NO_CONNECT".
1782 *
1783 * Link Down Timeout != 0:
1784 *
1785 * The driver waits for the link to come up after link down
1786 * before returning I/Os to OS with "DID_NO_CONNECT".
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001787 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 if (nv->link_down_timeout == 0) {
1789 ha->loop_down_abort_time =
Andrew Vasquez 354d6b22005-04-23 02:47:27 -04001790 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 } else {
1792 ha->link_down_timeout = nv->link_down_timeout;
1793 ha->loop_down_abort_time =
1794 (LOOP_DOWN_TIME - ha->link_down_timeout);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 /*
1798 * Need enough time to try and get the port back.
1799 */
1800 ha->port_down_retry_count = nv->port_down_retry_count;
1801 if (qlport_down_retry)
1802 ha->port_down_retry_count = qlport_down_retry;
1803 /* Set login_retry_count */
1804 ha->login_retry_count = nv->retry_count;
1805 if (ha->port_down_retry_count == nv->port_down_retry_count &&
1806 ha->port_down_retry_count > 3)
1807 ha->login_retry_count = ha->port_down_retry_count;
1808 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
1809 ha->login_retry_count = ha->port_down_retry_count;
1810 if (ql2xloginretrycount)
1811 ha->login_retry_count = ql2xloginretrycount;
1812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 icb->lun_enables = __constant_cpu_to_le16(0);
1814 icb->command_resource_count = 0;
1815 icb->immediate_notify_resource_count = 0;
1816 icb->timeout = __constant_cpu_to_le16(0);
1817
1818 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
1819 /* Enable RIO */
1820 icb->firmware_options[0] &= ~BIT_3;
1821 icb->add_firmware_options[0] &=
1822 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
1823 icb->add_firmware_options[0] |= BIT_2;
1824 icb->response_accumulation_timer = 3;
1825 icb->interrupt_delay_timer = 5;
1826
1827 ha->flags.process_response_queue = 1;
1828 } else {
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001829 /* Enable ZIO. */
1830 if (!ha->flags.init_done) {
1831 ha->zio_mode = icb->add_firmware_options[0] &
1832 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
1833 ha->zio_timer = icb->interrupt_delay_timer ?
1834 icb->interrupt_delay_timer: 2;
1835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 icb->add_firmware_options[0] &=
1837 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001838 ha->flags.process_response_queue = 0;
1839 if (ha->zio_mode != QLA_ZIO_DISABLED) {
andrew.vasquez@qlogic.com4a59f712006-03-09 14:27:39 -08001840 ha->zio_mode = QLA_ZIO_MODE_6;
1841
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001842 DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer "
1843 "delay (%d us).\n", ha->host_no, ha->zio_mode,
1844 ha->zio_timer * 100));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 qla_printk(KERN_INFO, ha,
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001846 "ZIO mode %d enabled; timer delay (%d us).\n",
1847 ha->zio_mode, ha->zio_timer * 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001849 icb->add_firmware_options[0] |= (uint8_t)ha->zio_mode;
1850 icb->interrupt_delay_timer = (uint8_t)ha->zio_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 ha->flags.process_response_queue = 1;
1852 }
1853 }
1854
David Miller4e08df32007-04-16 12:37:43 -07001855 if (rval) {
1856 DEBUG2_3(printk(KERN_WARNING
1857 "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
1858 }
1859 return (rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860}
1861
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001862static void
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001863qla2x00_rport_del(void *data)
1864{
1865 fc_port_t *fcport = data;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08001866 struct fc_rport *rport;
1867 unsigned long flags;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001868
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08001869 spin_lock_irqsave(&fcport->rport_lock, flags);
1870 rport = fcport->drport;
1871 fcport->drport = NULL;
1872 spin_unlock_irqrestore(&fcport->rport_lock, flags);
1873 if (rport)
1874 fc_remote_port_delete(rport);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001875}
1876
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877/**
1878 * qla2x00_alloc_fcport() - Allocate a generic fcport.
1879 * @ha: HA context
1880 * @flags: allocation flags
1881 *
1882 * Returns a pointer to the allocated fcport, or NULL, if none available.
1883 */
Adrian Bunk413975a2006-06-30 02:33:06 -07001884static fc_port_t *
Al Viroc53033f2005-10-21 03:22:08 -04001885qla2x00_alloc_fcport(scsi_qla_host_t *ha, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886{
1887 fc_port_t *fcport;
1888
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02001889 fcport = kzalloc(sizeof(fc_port_t), flags);
1890 if (!fcport)
1891 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
1893 /* Setup fcport template structure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 fcport->ha = ha;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001895 fcport->vp_idx = ha->vp_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 fcport->port_type = FCT_UNKNOWN;
1897 fcport->loop_id = FC_NO_LOOP_ID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 atomic_set(&fcport->state, FCS_UNCONFIGURED);
1899 fcport->flags = FCF_RLC_SUPPORT;
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07001900 fcport->supported_classes = FC_COS_UNSPECIFIED;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08001901 spin_lock_init(&fcport->rport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02001903 return fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904}
1905
1906/*
1907 * qla2x00_configure_loop
1908 * Updates Fibre Channel Device Database with what is actually on loop.
1909 *
1910 * Input:
1911 * ha = adapter block pointer.
1912 *
1913 * Returns:
1914 * 0 = success.
1915 * 1 = error.
1916 * 2 = database was full and device was not configured.
1917 */
1918static int
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001919qla2x00_configure_loop(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920{
1921 int rval;
1922 unsigned long flags, save_flags;
1923
1924 rval = QLA_SUCCESS;
1925
1926 /* Get Initiator ID */
1927 if (test_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags)) {
1928 rval = qla2x00_configure_hba(ha);
1929 if (rval != QLA_SUCCESS) {
1930 DEBUG(printk("scsi(%ld): Unable to configure HBA.\n",
1931 ha->host_no));
1932 return (rval);
1933 }
1934 }
1935
1936 save_flags = flags = ha->dpc_flags;
1937 DEBUG(printk("scsi(%ld): Configure loop -- dpc flags =0x%lx\n",
1938 ha->host_no, flags));
1939
1940 /*
1941 * If we have both an RSCN and PORT UPDATE pending then handle them
1942 * both at the same time.
1943 */
1944 clear_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1945 clear_bit(RSCN_UPDATE, &ha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
1947 /* Determine what we need to do */
1948 if (ha->current_topology == ISP_CFG_FL &&
1949 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1950
1951 ha->flags.rscn_queue_overflow = 1;
1952 set_bit(RSCN_UPDATE, &flags);
1953
1954 } else if (ha->current_topology == ISP_CFG_F &&
1955 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1956
1957 ha->flags.rscn_queue_overflow = 1;
1958 set_bit(RSCN_UPDATE, &flags);
1959 clear_bit(LOCAL_LOOP_UPDATE, &flags);
1960
Andrew Vasquez21333b42006-05-17 15:09:56 -07001961 } else if (ha->current_topology == ISP_CFG_N) {
1962 clear_bit(RSCN_UPDATE, &flags);
1963
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 } else if (!ha->flags.online ||
1965 (test_bit(ABORT_ISP_ACTIVE, &flags))) {
1966
1967 ha->flags.rscn_queue_overflow = 1;
1968 set_bit(RSCN_UPDATE, &flags);
1969 set_bit(LOCAL_LOOP_UPDATE, &flags);
1970 }
1971
1972 if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
1973 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1974 rval = QLA_FUNCTION_FAILED;
1975 } else {
1976 rval = qla2x00_configure_local_loop(ha);
1977 }
1978 }
1979
1980 if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
Andrew Vasquez23443b12005-12-06 10:57:06 -08001981 if (LOOP_TRANSITION(ha)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 rval = QLA_FUNCTION_FAILED;
1983 } else {
1984 rval = qla2x00_configure_fabric(ha);
1985 }
1986 }
1987
1988 if (rval == QLA_SUCCESS) {
1989 if (atomic_read(&ha->loop_down_timer) ||
1990 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1991 rval = QLA_FUNCTION_FAILED;
1992 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 atomic_set(&ha->loop_state, LOOP_READY);
1994
1995 DEBUG(printk("scsi(%ld): LOOP READY\n", ha->host_no));
1996 }
1997 }
1998
1999 if (rval) {
2000 DEBUG2_3(printk("%s(%ld): *** FAILED ***\n",
2001 __func__, ha->host_no));
2002 } else {
2003 DEBUG3(printk("%s: exiting normally\n", __func__));
2004 }
2005
2006 /* Restore state if a resync event occured during processing */
2007 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
2008 if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
2009 set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
2010 if (test_bit(RSCN_UPDATE, &save_flags))
2011 set_bit(RSCN_UPDATE, &ha->dpc_flags);
2012 }
2013
2014 return (rval);
2015}
2016
2017
2018
2019/*
2020 * qla2x00_configure_local_loop
2021 * Updates Fibre Channel Device Database with local loop devices.
2022 *
2023 * Input:
2024 * ha = adapter block pointer.
2025 *
2026 * Returns:
2027 * 0 = success.
2028 */
2029static int
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002030qla2x00_configure_local_loop(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031{
2032 int rval, rval2;
2033 int found_devs;
2034 int found;
2035 fc_port_t *fcport, *new_fcport;
2036
2037 uint16_t index;
2038 uint16_t entries;
2039 char *id_iter;
2040 uint16_t loop_id;
2041 uint8_t domain, area, al_pa;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002042 scsi_qla_host_t *pha = to_qla_parent(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
2044 found_devs = 0;
2045 new_fcport = NULL;
2046 entries = MAX_FIBRE_DEVICES;
2047
2048 DEBUG3(printk("scsi(%ld): Getting FCAL position map\n", ha->host_no));
2049 DEBUG3(qla2x00_get_fcal_position_map(ha, NULL));
2050
2051 /* Get list of logged in devices. */
2052 memset(ha->gid_list, 0, GID_LIST_SIZE);
2053 rval = qla2x00_get_id_list(ha, ha->gid_list, ha->gid_list_dma,
2054 &entries);
2055 if (rval != QLA_SUCCESS)
2056 goto cleanup_allocation;
2057
2058 DEBUG3(printk("scsi(%ld): Entries in ID list (%d)\n",
2059 ha->host_no, entries));
2060 DEBUG3(qla2x00_dump_buffer((uint8_t *)ha->gid_list,
2061 entries * sizeof(struct gid_list_info)));
2062
2063 /* Allocate temporary fcport for any new fcports discovered. */
2064 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2065 if (new_fcport == NULL) {
2066 rval = QLA_MEMORY_ALLOC_FAILED;
2067 goto cleanup_allocation;
2068 }
2069 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
2070
2071 /*
2072 * Mark local devices that were present with FCF_DEVICE_LOST for now.
2073 */
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002074 list_for_each_entry(fcport, &pha->fcports, list) {
2075 if (fcport->vp_idx != ha->vp_idx)
2076 continue;
2077
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 if (atomic_read(&fcport->state) == FCS_ONLINE &&
2079 fcport->port_type != FCT_BROADCAST &&
2080 (fcport->flags & FCF_FABRIC_DEVICE) == 0) {
2081
2082 DEBUG(printk("scsi(%ld): Marking port lost, "
2083 "loop_id=0x%04x\n",
2084 ha->host_no, fcport->loop_id));
2085
2086 atomic_set(&fcport->state, FCS_DEVICE_LOST);
2087 fcport->flags &= ~FCF_FARP_DONE;
2088 }
2089 }
2090
2091 /* Add devices to port list. */
2092 id_iter = (char *)ha->gid_list;
2093 for (index = 0; index < entries; index++) {
2094 domain = ((struct gid_list_info *)id_iter)->domain;
2095 area = ((struct gid_list_info *)id_iter)->area;
2096 al_pa = ((struct gid_list_info *)id_iter)->al_pa;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002097 if (IS_QLA2100(ha) || IS_QLA2200(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 loop_id = (uint16_t)
2099 ((struct gid_list_info *)id_iter)->loop_id_2100;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002100 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 loop_id = le16_to_cpu(
2102 ((struct gid_list_info *)id_iter)->loop_id);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002103 id_iter += ha->gid_list_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105 /* Bypass reserved domain fields. */
2106 if ((domain & 0xf0) == 0xf0)
2107 continue;
2108
2109 /* Bypass if not same domain and area of adapter. */
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07002110 if (area && domain &&
2111 (area != ha->d_id.b.area || domain != ha->d_id.b.domain))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 continue;
2113
2114 /* Bypass invalid local loop ID. */
2115 if (loop_id > LAST_LOCAL_LOOP_ID)
2116 continue;
2117
2118 /* Fill in member data. */
2119 new_fcport->d_id.b.domain = domain;
2120 new_fcport->d_id.b.area = area;
2121 new_fcport->d_id.b.al_pa = al_pa;
2122 new_fcport->loop_id = loop_id;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002123 new_fcport->vp_idx = ha->vp_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 rval2 = qla2x00_get_port_database(ha, new_fcport, 0);
2125 if (rval2 != QLA_SUCCESS) {
2126 DEBUG2(printk("scsi(%ld): Failed to retrieve fcport "
2127 "information -- get_port_database=%x, "
2128 "loop_id=0x%04x\n",
2129 ha->host_no, rval2, new_fcport->loop_id));
andrew.vasquez@qlogic.comc9d02ac2006-01-13 17:05:26 -08002130 DEBUG2(printk("scsi(%ld): Scheduling resync...\n",
2131 ha->host_no));
2132 set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 continue;
2134 }
2135
2136 /* Check for matching device in port list. */
2137 found = 0;
2138 fcport = NULL;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002139 list_for_each_entry(fcport, &pha->fcports, list) {
2140 if (fcport->vp_idx != ha->vp_idx)
2141 continue;
2142
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 if (memcmp(new_fcport->port_name, fcport->port_name,
2144 WWN_SIZE))
2145 continue;
2146
2147 fcport->flags &= ~(FCF_FABRIC_DEVICE |
2148 FCF_PERSISTENT_BOUND);
2149 fcport->loop_id = new_fcport->loop_id;
2150 fcport->port_type = new_fcport->port_type;
2151 fcport->d_id.b24 = new_fcport->d_id.b24;
2152 memcpy(fcport->node_name, new_fcport->node_name,
2153 WWN_SIZE);
2154
2155 found++;
2156 break;
2157 }
2158
2159 if (!found) {
2160 /* New device, add to fcports list. */
2161 new_fcport->flags &= ~FCF_PERSISTENT_BOUND;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002162 if (ha->parent) {
2163 new_fcport->ha = ha;
2164 new_fcport->vp_idx = ha->vp_idx;
2165 list_add_tail(&new_fcport->vp_fcport,
2166 &ha->vp_fcports);
2167 }
2168 list_add_tail(&new_fcport->list, &pha->fcports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
2170 /* Allocate a new replacement fcport. */
2171 fcport = new_fcport;
2172 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2173 if (new_fcport == NULL) {
2174 rval = QLA_MEMORY_ALLOC_FAILED;
2175 goto cleanup_allocation;
2176 }
2177 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
2178 }
2179
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002180 /* Base iIDMA settings on HBA port speed. */
Andrew Vasqueza3cbdfa2007-08-13 10:13:18 -07002181 fcport->fp_speed = ha->link_data_rate;
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002182
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 qla2x00_update_fcport(ha, fcport);
2184
2185 found_devs++;
2186 }
2187
2188cleanup_allocation:
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002189 kfree(new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
2191 if (rval != QLA_SUCCESS) {
2192 DEBUG2(printk("scsi(%ld): Configure local loop error exit: "
2193 "rval=%x\n", ha->host_no, rval));
2194 }
2195
2196 if (found_devs) {
2197 ha->device_flags |= DFLG_LOCAL_DEVICES;
2198 ha->device_flags &= ~DFLG_RETRY_LOCAL_DEVICES;
2199 }
2200
2201 return (rval);
2202}
2203
2204static void
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002205qla2x00_iidma_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
2206{
2207#define LS_UNKNOWN 2
Andrew Vasqueza3cbdfa2007-08-13 10:13:18 -07002208 static char *link_speeds[5] = { "1", "2", "?", "4", "8" };
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002209 int rval;
Andrew Vasqueza3cbdfa2007-08-13 10:13:18 -07002210 uint16_t mb[6];
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002211
Andrew Vasquezc76f2c02007-07-19 15:05:57 -07002212 if (!IS_IIDMA_CAPABLE(ha))
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002213 return;
2214
Andrew Vasquez39bd9622007-09-20 14:07:34 -07002215 if (fcport->fp_speed == PORT_SPEED_UNKNOWN ||
2216 fcport->fp_speed > ha->link_data_rate)
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002217 return;
2218
Andrew Vasqueza3cbdfa2007-08-13 10:13:18 -07002219 rval = qla2x00_set_idma_speed(ha, fcport->loop_id, fcport->fp_speed,
2220 mb);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002221 if (rval != QLA_SUCCESS) {
2222 DEBUG2(printk("scsi(%ld): Unable to adjust iIDMA "
2223 "%02x%02x%02x%02x%02x%02x%02x%02x -- %04x %x %04x %04x.\n",
2224 ha->host_no, fcport->port_name[0], fcport->port_name[1],
2225 fcport->port_name[2], fcport->port_name[3],
2226 fcport->port_name[4], fcport->port_name[5],
2227 fcport->port_name[6], fcport->port_name[7], rval,
Andrew Vasqueza3cbdfa2007-08-13 10:13:18 -07002228 fcport->fp_speed, mb[0], mb[1]));
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002229 } else {
2230 DEBUG2(qla_printk(KERN_INFO, ha,
2231 "iIDMA adjusted to %s GB/s on "
2232 "%02x%02x%02x%02x%02x%02x%02x%02x.\n",
Andrew Vasqueza3cbdfa2007-08-13 10:13:18 -07002233 link_speeds[fcport->fp_speed], fcport->port_name[0],
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002234 fcport->port_name[1], fcport->port_name[2],
2235 fcport->port_name[3], fcport->port_name[4],
2236 fcport->port_name[5], fcport->port_name[6],
2237 fcport->port_name[7]));
2238 }
2239}
2240
Adrian Bunk23be3312006-11-24 02:46:01 +01002241static void
8482e1182005-04-17 15:04:54 -05002242qla2x00_reg_remote_port(scsi_qla_host_t *ha, fc_port_t *fcport)
2243{
2244 struct fc_rport_identifiers rport_ids;
bdf79622005-04-17 15:06:53 -05002245 struct fc_rport *rport;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002246 unsigned long flags;
8482e1182005-04-17 15:04:54 -05002247
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002248 if (fcport->drport)
2249 qla2x00_rport_del(fcport);
2250 if (fcport->rport)
2251 return;
8482e1182005-04-17 15:04:54 -05002252
Andrew Vasquezf8b02a82005-08-31 15:21:20 -07002253 rport_ids.node_name = wwn_to_u64(fcport->node_name);
2254 rport_ids.port_name = wwn_to_u64(fcport->port_name);
8482e1182005-04-17 15:04:54 -05002255 rport_ids.port_id = fcport->d_id.b.domain << 16 |
2256 fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
2257 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002258 rport = fc_remote_port_add(ha->host, 0, &rport_ids);
Andrew Vasquez77d74142005-07-08 18:00:36 -07002259 if (!rport) {
2260 qla_printk(KERN_WARNING, ha,
2261 "Unable to allocate fc remote port!\n");
2262 return;
2263 }
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002264 spin_lock_irqsave(&fcport->rport_lock, flags);
2265 fcport->rport = rport;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002266 *((fc_port_t **)rport->dd_data) = fcport;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002267 spin_unlock_irqrestore(&fcport->rport_lock, flags);
2268
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07002269 rport->supported_classes = fcport->supported_classes;
Andrew Vasquez77d74142005-07-08 18:00:36 -07002270
2271 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
8482e1182005-04-17 15:04:54 -05002272 if (fcport->port_type == FCT_INITIATOR)
2273 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2274 if (fcport->port_type == FCT_TARGET)
2275 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
Andrew Vasquez77d74142005-07-08 18:00:36 -07002276 fc_remote_port_rolechg(rport, rport_ids.roles);
8482e1182005-04-17 15:04:54 -05002277}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
2279/*
Adrian Bunk23be3312006-11-24 02:46:01 +01002280 * qla2x00_update_fcport
2281 * Updates device on list.
2282 *
2283 * Input:
2284 * ha = adapter block pointer.
2285 * fcport = port structure pointer.
2286 *
2287 * Return:
2288 * 0 - Success
2289 * BIT_0 - error
2290 *
2291 * Context:
2292 * Kernel context.
2293 */
2294void
2295qla2x00_update_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
2296{
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002297 scsi_qla_host_t *pha = to_qla_parent(ha);
2298
Adrian Bunk23be3312006-11-24 02:46:01 +01002299 fcport->ha = ha;
2300 fcport->login_retry = 0;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002301 fcport->port_login_retry_count = pha->port_down_retry_count *
Adrian Bunk23be3312006-11-24 02:46:01 +01002302 PORT_RETRY_TIME;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002303 atomic_set(&fcport->port_down_timer, pha->port_down_retry_count *
Adrian Bunk23be3312006-11-24 02:46:01 +01002304 PORT_RETRY_TIME);
2305 fcport->flags &= ~FCF_LOGIN_NEEDED;
2306
2307 qla2x00_iidma_fcport(ha, fcport);
2308
2309 atomic_set(&fcport->state, FCS_ONLINE);
2310
2311 qla2x00_reg_remote_port(ha, fcport);
2312}
2313
2314/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 * qla2x00_configure_fabric
2316 * Setup SNS devices with loop ID's.
2317 *
2318 * Input:
2319 * ha = adapter block pointer.
2320 *
2321 * Returns:
2322 * 0 = success.
2323 * BIT_0 = error
2324 */
2325static int
2326qla2x00_configure_fabric(scsi_qla_host_t *ha)
2327{
2328 int rval, rval2;
2329 fc_port_t *fcport, *fcptemp;
2330 uint16_t next_loopid;
2331 uint16_t mb[MAILBOX_REGISTER_COUNT];
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002332 uint16_t loop_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 LIST_HEAD(new_fcports);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002334 scsi_qla_host_t *pha = to_qla_parent(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
2336 /* If FL port exists, then SNS is present */
Andrew Vasqueze4289242007-07-19 15:05:56 -07002337 if (IS_FWI2_CAPABLE(ha))
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002338 loop_id = NPH_F_PORT;
2339 else
2340 loop_id = SNS_FL_PORT;
Andrew Vasquez90991c82006-10-02 12:00:46 -07002341 rval = qla2x00_get_port_name(ha, loop_id, ha->fabric_node_name, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 if (rval != QLA_SUCCESS) {
2343 DEBUG2(printk("scsi(%ld): MBC_GET_PORT_NAME Failed, No FL "
2344 "Port\n", ha->host_no));
2345
2346 ha->device_flags &= ~SWITCH_FOUND;
2347 return (QLA_SUCCESS);
2348 }
Andrew Vasquez90991c82006-10-02 12:00:46 -07002349 ha->device_flags |= SWITCH_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
2351 /* Mark devices that need re-synchronization. */
2352 rval2 = qla2x00_device_resync(ha);
2353 if (rval2 == QLA_RSCNS_HANDLED) {
2354 /* No point doing the scan, just continue. */
2355 return (QLA_SUCCESS);
2356 }
2357 do {
Andrew Vasquezcca53352005-08-26 19:08:30 -07002358 /* FDMI support. */
2359 if (ql2xfdmienable &&
2360 test_and_clear_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags))
2361 qla2x00_fdmi_register(ha);
2362
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 /* Ensure we are logged into the SNS. */
Andrew Vasqueze4289242007-07-19 15:05:56 -07002364 if (IS_FWI2_CAPABLE(ha))
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002365 loop_id = NPH_SNS;
2366 else
2367 loop_id = SIMPLE_NAME_SERVER;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07002368 ha->isp_ops->fabric_login(ha, loop_id, 0xff, 0xff,
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002369 0xfc, mb, BIT_1 | BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 if (mb[0] != MBS_COMMAND_COMPLETE) {
2371 DEBUG2(qla_printk(KERN_INFO, ha,
2372 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002373 "mb[2]=%x mb[6]=%x mb[7]=%x\n", loop_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 mb[0], mb[1], mb[2], mb[6], mb[7]));
2375 return (QLA_SUCCESS);
2376 }
2377
2378 if (test_and_clear_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags)) {
2379 if (qla2x00_rft_id(ha)) {
2380 /* EMPTY */
2381 DEBUG2(printk("scsi(%ld): Register FC-4 "
2382 "TYPE failed.\n", ha->host_no));
2383 }
2384 if (qla2x00_rff_id(ha)) {
2385 /* EMPTY */
2386 DEBUG2(printk("scsi(%ld): Register FC-4 "
2387 "Features failed.\n", ha->host_no));
2388 }
2389 if (qla2x00_rnn_id(ha)) {
2390 /* EMPTY */
2391 DEBUG2(printk("scsi(%ld): Register Node Name "
2392 "failed.\n", ha->host_no));
2393 } else if (qla2x00_rsnn_nn(ha)) {
2394 /* EMPTY */
2395 DEBUG2(printk("scsi(%ld): Register Symbolic "
2396 "Node Name failed.\n", ha->host_no));
2397 }
2398 }
2399
2400 rval = qla2x00_find_all_fabric_devs(ha, &new_fcports);
2401 if (rval != QLA_SUCCESS)
2402 break;
2403
2404 /*
2405 * Logout all previous fabric devices marked lost, except
2406 * tape devices.
2407 */
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002408 list_for_each_entry(fcport, &pha->fcports, list) {
2409 if (fcport->vp_idx !=ha->vp_idx)
2410 continue;
2411
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2413 break;
2414
2415 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0)
2416 continue;
2417
2418 if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
2419 qla2x00_mark_device_lost(ha, fcport,
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002420 ql2xplogiabsentdevice, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 if (fcport->loop_id != FC_NO_LOOP_ID &&
2422 (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2423 fcport->port_type != FCT_INITIATOR &&
2424 fcport->port_type != FCT_BROADCAST) {
Andrew Vasquezfd34f552007-07-19 15:06:00 -07002425 ha->isp_ops->fabric_logout(ha,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07002426 fcport->loop_id,
2427 fcport->d_id.b.domain,
2428 fcport->d_id.b.area,
2429 fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 fcport->loop_id = FC_NO_LOOP_ID;
2431 }
2432 }
2433 }
2434
2435 /* Starting free loop ID. */
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002436 next_loopid = pha->min_external_loopid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
2438 /*
2439 * Scan through our port list and login entries that need to be
2440 * logged in.
2441 */
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002442 list_for_each_entry(fcport, &pha->fcports, list) {
2443 if (fcport->vp_idx != ha->vp_idx)
2444 continue;
2445
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 if (atomic_read(&ha->loop_down_timer) ||
2447 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2448 break;
2449
2450 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2451 (fcport->flags & FCF_LOGIN_NEEDED) == 0)
2452 continue;
2453
2454 if (fcport->loop_id == FC_NO_LOOP_ID) {
2455 fcport->loop_id = next_loopid;
2456 rval = qla2x00_find_new_loop_id(ha, fcport);
2457 if (rval != QLA_SUCCESS) {
2458 /* Ran out of IDs to use */
2459 break;
2460 }
2461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 /* Login and update database */
2463 qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
2464 }
2465
2466 /* Exit if out of loop IDs. */
2467 if (rval != QLA_SUCCESS) {
2468 break;
2469 }
2470
2471 /*
2472 * Login and add the new devices to our port list.
2473 */
2474 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2475 if (atomic_read(&ha->loop_down_timer) ||
2476 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2477 break;
2478
2479 /* Find a new loop ID to use. */
2480 fcport->loop_id = next_loopid;
2481 rval = qla2x00_find_new_loop_id(ha, fcport);
2482 if (rval != QLA_SUCCESS) {
2483 /* Ran out of IDs to use */
2484 break;
2485 }
2486
bdf79622005-04-17 15:06:53 -05002487 /* Login and update database */
2488 qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002489
2490 if (ha->parent) {
2491 fcport->ha = ha;
2492 fcport->vp_idx = ha->vp_idx;
2493 list_add_tail(&fcport->vp_fcport,
2494 &ha->vp_fcports);
2495 list_move_tail(&fcport->list,
2496 &ha->parent->fcports);
2497 } else
2498 list_move_tail(&fcport->list, &ha->fcports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 }
2500 } while (0);
2501
2502 /* Free all new device structures not processed. */
2503 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2504 list_del(&fcport->list);
2505 kfree(fcport);
2506 }
2507
2508 if (rval) {
2509 DEBUG2(printk("scsi(%ld): Configure fabric error exit: "
2510 "rval=%d\n", ha->host_no, rval));
2511 }
2512
2513 return (rval);
2514}
2515
2516
2517/*
2518 * qla2x00_find_all_fabric_devs
2519 *
2520 * Input:
2521 * ha = adapter block pointer.
2522 * dev = database device entry pointer.
2523 *
2524 * Returns:
2525 * 0 = success.
2526 *
2527 * Context:
2528 * Kernel context.
2529 */
2530static int
2531qla2x00_find_all_fabric_devs(scsi_qla_host_t *ha, struct list_head *new_fcports)
2532{
2533 int rval;
2534 uint16_t loop_id;
2535 fc_port_t *fcport, *new_fcport, *fcptemp;
2536 int found;
2537
2538 sw_info_t *swl;
2539 int swl_idx;
2540 int first_dev, last_dev;
2541 port_id_t wrap, nxt_d_id;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002542 int vp_index;
2543 int empty_vp_index;
2544 int found_vp;
2545 scsi_qla_host_t *vha;
2546 scsi_qla_host_t *pha = to_qla_parent(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547
2548 rval = QLA_SUCCESS;
2549
2550 /* Try GID_PT to get device list, else GAN. */
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02002551 swl = kcalloc(MAX_FIBRE_DEVICES, sizeof(sw_info_t), GFP_ATOMIC);
2552 if (!swl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 /*EMPTY*/
2554 DEBUG2(printk("scsi(%ld): GID_PT allocations failed, fallback "
2555 "on GA_NXT\n", ha->host_no));
2556 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 if (qla2x00_gid_pt(ha, swl) != QLA_SUCCESS) {
2558 kfree(swl);
2559 swl = NULL;
2560 } else if (qla2x00_gpn_id(ha, swl) != QLA_SUCCESS) {
2561 kfree(swl);
2562 swl = NULL;
2563 } else if (qla2x00_gnn_id(ha, swl) != QLA_SUCCESS) {
2564 kfree(swl);
2565 swl = NULL;
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002566 } else if (qla2x00_gfpn_id(ha, swl) == QLA_SUCCESS) {
2567 qla2x00_gpsc(ha, swl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 }
2569 }
2570 swl_idx = 0;
2571
2572 /* Allocate temporary fcport for any new fcports discovered. */
2573 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2574 if (new_fcport == NULL) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002575 kfree(swl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 return (QLA_MEMORY_ALLOC_FAILED);
2577 }
2578 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002579 new_fcport->vp_idx = ha->vp_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 /* Set start port ID scan at adapter ID. */
2581 first_dev = 1;
2582 last_dev = 0;
2583
2584 /* Starting free loop ID. */
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002585 loop_id = pha->min_external_loopid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 for (; loop_id <= ha->last_loop_id; loop_id++) {
Andrew Vasquez3d716442005-07-06 10:30:26 -07002587 if (qla2x00_is_reserved_id(ha, loop_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 continue;
2589
Andrew Vasquez23443b12005-12-06 10:57:06 -08002590 if (atomic_read(&ha->loop_down_timer) || LOOP_TRANSITION(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 break;
2592
2593 if (swl != NULL) {
2594 if (last_dev) {
2595 wrap.b24 = new_fcport->d_id.b24;
2596 } else {
2597 new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
2598 memcpy(new_fcport->node_name,
2599 swl[swl_idx].node_name, WWN_SIZE);
2600 memcpy(new_fcport->port_name,
2601 swl[swl_idx].port_name, WWN_SIZE);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002602 memcpy(new_fcport->fabric_port_name,
2603 swl[swl_idx].fabric_port_name, WWN_SIZE);
2604 new_fcport->fp_speed = swl[swl_idx].fp_speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
2606 if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
2607 last_dev = 1;
2608 }
2609 swl_idx++;
2610 }
2611 } else {
2612 /* Send GA_NXT to the switch */
2613 rval = qla2x00_ga_nxt(ha, new_fcport);
2614 if (rval != QLA_SUCCESS) {
2615 qla_printk(KERN_WARNING, ha,
2616 "SNS scan failed -- assuming zero-entry "
2617 "result...\n");
2618 list_for_each_entry_safe(fcport, fcptemp,
2619 new_fcports, list) {
2620 list_del(&fcport->list);
2621 kfree(fcport);
2622 }
2623 rval = QLA_SUCCESS;
2624 break;
2625 }
2626 }
2627
2628 /* If wrap on switch device list, exit. */
2629 if (first_dev) {
2630 wrap.b24 = new_fcport->d_id.b24;
2631 first_dev = 0;
2632 } else if (new_fcport->d_id.b24 == wrap.b24) {
2633 DEBUG2(printk("scsi(%ld): device wrap (%02x%02x%02x)\n",
2634 ha->host_no, new_fcport->d_id.b.domain,
2635 new_fcport->d_id.b.area, new_fcport->d_id.b.al_pa));
2636 break;
2637 }
2638
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002639 /* Bypass if same physical adapter. */
2640 if (new_fcport->d_id.b24 == pha->d_id.b24)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 continue;
2642
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002643 /* Bypass virtual ports of the same host. */
2644 if (pha->num_vhosts) {
Andrew Vasquezeb66dc62007-11-12 10:30:58 -08002645 for_each_mapped_vp_idx(pha, vp_index) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002646 empty_vp_index = 1;
2647 found_vp = 0;
2648 list_for_each_entry(vha, &pha->vp_list,
2649 vp_list) {
2650 if (vp_index == vha->vp_idx) {
2651 empty_vp_index = 0;
2652 found_vp = 1;
2653 break;
2654 }
2655 }
2656
2657 if (empty_vp_index)
2658 continue;
2659
2660 if (found_vp &&
2661 new_fcport->d_id.b24 == vha->d_id.b24)
2662 break;
2663 }
Andrew Vasquezeb66dc62007-11-12 10:30:58 -08002664
2665 if (vp_index <= pha->max_npiv_vports)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002666 continue;
2667 }
2668
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07002669 /* Bypass if same domain and area of adapter. */
2670 if (((new_fcport->d_id.b24 & 0xffff00) ==
2671 (ha->d_id.b24 & 0xffff00)) && ha->current_topology ==
2672 ISP_CFG_FL)
2673 continue;
2674
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 /* Bypass reserved domain fields. */
2676 if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
2677 continue;
2678
2679 /* Locate matching device in database. */
2680 found = 0;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002681 list_for_each_entry(fcport, &pha->fcports, list) {
2682 if (new_fcport->vp_idx != fcport->vp_idx)
2683 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 if (memcmp(new_fcport->port_name, fcport->port_name,
2685 WWN_SIZE))
2686 continue;
2687
2688 found++;
2689
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002690 /* Update port state. */
2691 memcpy(fcport->fabric_port_name,
2692 new_fcport->fabric_port_name, WWN_SIZE);
2693 fcport->fp_speed = new_fcport->fp_speed;
2694
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 /*
2696 * If address the same and state FCS_ONLINE, nothing
2697 * changed.
2698 */
2699 if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
2700 atomic_read(&fcport->state) == FCS_ONLINE) {
2701 break;
2702 }
2703
2704 /*
2705 * If device was not a fabric device before.
2706 */
2707 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
2708 fcport->d_id.b24 = new_fcport->d_id.b24;
2709 fcport->loop_id = FC_NO_LOOP_ID;
2710 fcport->flags |= (FCF_FABRIC_DEVICE |
2711 FCF_LOGIN_NEEDED);
2712 fcport->flags &= ~FCF_PERSISTENT_BOUND;
2713 break;
2714 }
2715
2716 /*
2717 * Port ID changed or device was marked to be updated;
2718 * Log it out if still logged in and mark it for
2719 * relogin later.
2720 */
2721 fcport->d_id.b24 = new_fcport->d_id.b24;
2722 fcport->flags |= FCF_LOGIN_NEEDED;
2723 if (fcport->loop_id != FC_NO_LOOP_ID &&
2724 (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2725 fcport->port_type != FCT_INITIATOR &&
2726 fcport->port_type != FCT_BROADCAST) {
Andrew Vasquezfd34f552007-07-19 15:06:00 -07002727 ha->isp_ops->fabric_logout(ha, fcport->loop_id,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07002728 fcport->d_id.b.domain, fcport->d_id.b.area,
2729 fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 fcport->loop_id = FC_NO_LOOP_ID;
2731 }
2732
2733 break;
2734 }
2735
2736 if (found)
2737 continue;
2738
2739 /* If device was not in our fcports list, then add it. */
2740 list_add_tail(&new_fcport->list, new_fcports);
2741
2742 /* Allocate a new replacement fcport. */
2743 nxt_d_id.b24 = new_fcport->d_id.b24;
2744 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2745 if (new_fcport == NULL) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002746 kfree(swl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 return (QLA_MEMORY_ALLOC_FAILED);
2748 }
2749 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2750 new_fcport->d_id.b24 = nxt_d_id.b24;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002751 new_fcport->vp_idx = ha->vp_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 }
2753
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002754 kfree(swl);
2755 kfree(new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
2757 if (!list_empty(new_fcports))
2758 ha->device_flags |= DFLG_FABRIC_DEVICES;
2759
2760 return (rval);
2761}
2762
2763/*
2764 * qla2x00_find_new_loop_id
2765 * Scan through our port list and find a new usable loop ID.
2766 *
2767 * Input:
2768 * ha: adapter state pointer.
2769 * dev: port structure pointer.
2770 *
2771 * Returns:
2772 * qla2x00 local function return status code.
2773 *
2774 * Context:
2775 * Kernel context.
2776 */
Adrian Bunk413975a2006-06-30 02:33:06 -07002777static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778qla2x00_find_new_loop_id(scsi_qla_host_t *ha, fc_port_t *dev)
2779{
2780 int rval;
2781 int found;
2782 fc_port_t *fcport;
2783 uint16_t first_loop_id;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002784 scsi_qla_host_t *pha = to_qla_parent(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
2786 rval = QLA_SUCCESS;
2787
2788 /* Save starting loop ID. */
2789 first_loop_id = dev->loop_id;
2790
2791 for (;;) {
2792 /* Skip loop ID if already used by adapter. */
2793 if (dev->loop_id == ha->loop_id) {
2794 dev->loop_id++;
2795 }
2796
2797 /* Skip reserved loop IDs. */
Andrew Vasquez3d716442005-07-06 10:30:26 -07002798 while (qla2x00_is_reserved_id(ha, dev->loop_id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 dev->loop_id++;
2800 }
2801
2802 /* Reset loop ID if passed the end. */
2803 if (dev->loop_id > ha->last_loop_id) {
2804 /* first loop ID. */
2805 dev->loop_id = ha->min_external_loopid;
2806 }
2807
2808 /* Check for loop ID being already in use. */
2809 found = 0;
2810 fcport = NULL;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002811 list_for_each_entry(fcport, &pha->fcports, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 if (fcport->loop_id == dev->loop_id && fcport != dev) {
2813 /* ID possibly in use */
2814 found++;
2815 break;
2816 }
2817 }
2818
2819 /* If not in use then it is free to use. */
2820 if (!found) {
2821 break;
2822 }
2823
2824 /* ID in use. Try next value. */
2825 dev->loop_id++;
2826
2827 /* If wrap around. No free ID to use. */
2828 if (dev->loop_id == first_loop_id) {
2829 dev->loop_id = FC_NO_LOOP_ID;
2830 rval = QLA_FUNCTION_FAILED;
2831 break;
2832 }
2833 }
2834
2835 return (rval);
2836}
2837
2838/*
2839 * qla2x00_device_resync
2840 * Marks devices in the database that needs resynchronization.
2841 *
2842 * Input:
2843 * ha = adapter block pointer.
2844 *
2845 * Context:
2846 * Kernel context.
2847 */
2848static int
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002849qla2x00_device_resync(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850{
2851 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 uint32_t mask;
2853 fc_port_t *fcport;
2854 uint32_t rscn_entry;
2855 uint8_t rscn_out_iter;
2856 uint8_t format;
2857 port_id_t d_id;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002858 scsi_qla_host_t *pha = to_qla_parent(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
2860 rval = QLA_RSCNS_HANDLED;
2861
2862 while (ha->rscn_out_ptr != ha->rscn_in_ptr ||
2863 ha->flags.rscn_queue_overflow) {
2864
2865 rscn_entry = ha->rscn_queue[ha->rscn_out_ptr];
2866 format = MSB(MSW(rscn_entry));
2867 d_id.b.domain = LSB(MSW(rscn_entry));
2868 d_id.b.area = MSB(LSW(rscn_entry));
2869 d_id.b.al_pa = LSB(LSW(rscn_entry));
2870
2871 DEBUG(printk("scsi(%ld): RSCN queue entry[%d] = "
2872 "[%02x/%02x%02x%02x].\n",
2873 ha->host_no, ha->rscn_out_ptr, format, d_id.b.domain,
2874 d_id.b.area, d_id.b.al_pa));
2875
2876 ha->rscn_out_ptr++;
2877 if (ha->rscn_out_ptr == MAX_RSCN_COUNT)
2878 ha->rscn_out_ptr = 0;
2879
2880 /* Skip duplicate entries. */
2881 for (rscn_out_iter = ha->rscn_out_ptr;
2882 !ha->flags.rscn_queue_overflow &&
2883 rscn_out_iter != ha->rscn_in_ptr;
2884 rscn_out_iter = (rscn_out_iter ==
2885 (MAX_RSCN_COUNT - 1)) ? 0: rscn_out_iter + 1) {
2886
2887 if (rscn_entry != ha->rscn_queue[rscn_out_iter])
2888 break;
2889
2890 DEBUG(printk("scsi(%ld): Skipping duplicate RSCN queue "
2891 "entry found at [%d].\n", ha->host_no,
2892 rscn_out_iter));
2893
2894 ha->rscn_out_ptr = rscn_out_iter;
2895 }
2896
2897 /* Queue overflow, set switch default case. */
2898 if (ha->flags.rscn_queue_overflow) {
2899 DEBUG(printk("scsi(%ld): device_resync: rscn "
2900 "overflow.\n", ha->host_no));
2901
2902 format = 3;
2903 ha->flags.rscn_queue_overflow = 0;
2904 }
2905
2906 switch (format) {
2907 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 mask = 0xffffff;
2909 break;
2910 case 1:
2911 mask = 0xffff00;
2912 break;
2913 case 2:
2914 mask = 0xff0000;
2915 break;
2916 default:
2917 mask = 0x0;
2918 d_id.b24 = 0;
2919 ha->rscn_out_ptr = ha->rscn_in_ptr;
2920 break;
2921 }
2922
2923 rval = QLA_SUCCESS;
2924
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002925 list_for_each_entry(fcport, &pha->fcports, list) {
2926 if (fcport->vp_idx != ha->vp_idx)
2927 continue;
2928
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2930 (fcport->d_id.b24 & mask) != d_id.b24 ||
2931 fcport->port_type == FCT_BROADCAST)
2932 continue;
2933
2934 if (atomic_read(&fcport->state) == FCS_ONLINE) {
2935 if (format != 3 ||
2936 fcport->port_type != FCT_INITIATOR) {
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002937 qla2x00_mark_device_lost(ha, fcport,
2938 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 }
2940 }
2941 fcport->flags &= ~FCF_FARP_DONE;
2942 }
2943 }
2944 return (rval);
2945}
2946
2947/*
2948 * qla2x00_fabric_dev_login
2949 * Login fabric target device and update FC port database.
2950 *
2951 * Input:
2952 * ha: adapter state pointer.
2953 * fcport: port structure list pointer.
2954 * next_loopid: contains value of a new loop ID that can be used
2955 * by the next login attempt.
2956 *
2957 * Returns:
2958 * qla2x00 local function return status code.
2959 *
2960 * Context:
2961 * Kernel context.
2962 */
2963static int
2964qla2x00_fabric_dev_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2965 uint16_t *next_loopid)
2966{
2967 int rval;
2968 int retry;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002969 uint8_t opts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970
2971 rval = QLA_SUCCESS;
2972 retry = 0;
2973
2974 rval = qla2x00_fabric_login(ha, fcport, next_loopid);
2975 if (rval == QLA_SUCCESS) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002976 /* Send an ADISC to tape devices.*/
2977 opts = 0;
2978 if (fcport->flags & FCF_TAPE_PRESENT)
2979 opts |= BIT_1;
2980 rval = qla2x00_get_port_database(ha, fcport, opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 if (rval != QLA_SUCCESS) {
Andrew Vasquezfd34f552007-07-19 15:06:00 -07002982 ha->isp_ops->fabric_logout(ha, fcport->loop_id,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07002983 fcport->d_id.b.domain, fcport->d_id.b.area,
2984 fcport->d_id.b.al_pa);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002985 qla2x00_mark_device_lost(ha, fcport, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 } else {
2987 qla2x00_update_fcport(ha, fcport);
2988 }
2989 }
2990
2991 return (rval);
2992}
2993
2994/*
2995 * qla2x00_fabric_login
2996 * Issue fabric login command.
2997 *
2998 * Input:
2999 * ha = adapter block pointer.
3000 * device = pointer to FC device type structure.
3001 *
3002 * Returns:
3003 * 0 - Login successfully
3004 * 1 - Login failed
3005 * 2 - Initiator device
3006 * 3 - Fatal error
3007 */
3008int
3009qla2x00_fabric_login(scsi_qla_host_t *ha, fc_port_t *fcport,
3010 uint16_t *next_loopid)
3011{
3012 int rval;
3013 int retry;
3014 uint16_t tmp_loopid;
3015 uint16_t mb[MAILBOX_REGISTER_COUNT];
3016
3017 retry = 0;
3018 tmp_loopid = 0;
3019
3020 for (;;) {
3021 DEBUG(printk("scsi(%ld): Trying Fabric Login w/loop id 0x%04x "
3022 "for port %02x%02x%02x.\n",
3023 ha->host_no, fcport->loop_id, fcport->d_id.b.domain,
3024 fcport->d_id.b.area, fcport->d_id.b.al_pa));
3025
3026 /* Login fcport on switch. */
Andrew Vasquezfd34f552007-07-19 15:06:00 -07003027 ha->isp_ops->fabric_login(ha, fcport->loop_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 fcport->d_id.b.domain, fcport->d_id.b.area,
3029 fcport->d_id.b.al_pa, mb, BIT_0);
3030 if (mb[0] == MBS_PORT_ID_USED) {
3031 /*
3032 * Device has another loop ID. The firmware team
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003033 * recommends the driver perform an implicit login with
3034 * the specified ID again. The ID we just used is save
3035 * here so we return with an ID that can be tried by
3036 * the next login.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 */
3038 retry++;
3039 tmp_loopid = fcport->loop_id;
3040 fcport->loop_id = mb[1];
3041
3042 DEBUG(printk("Fabric Login: port in use - next "
3043 "loop id=0x%04x, port Id=%02x%02x%02x.\n",
3044 fcport->loop_id, fcport->d_id.b.domain,
3045 fcport->d_id.b.area, fcport->d_id.b.al_pa));
3046
3047 } else if (mb[0] == MBS_COMMAND_COMPLETE) {
3048 /*
3049 * Login succeeded.
3050 */
3051 if (retry) {
3052 /* A retry occurred before. */
3053 *next_loopid = tmp_loopid;
3054 } else {
3055 /*
3056 * No retry occurred before. Just increment the
3057 * ID value for next login.
3058 */
3059 *next_loopid = (fcport->loop_id + 1);
3060 }
3061
3062 if (mb[1] & BIT_0) {
3063 fcport->port_type = FCT_INITIATOR;
3064 } else {
3065 fcport->port_type = FCT_TARGET;
3066 if (mb[1] & BIT_1) {
3067 fcport->flags |= FCF_TAPE_PRESENT;
3068 }
3069 }
3070
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07003071 if (mb[10] & BIT_0)
3072 fcport->supported_classes |= FC_COS_CLASS2;
3073 if (mb[10] & BIT_1)
3074 fcport->supported_classes |= FC_COS_CLASS3;
3075
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 rval = QLA_SUCCESS;
3077 break;
3078 } else if (mb[0] == MBS_LOOP_ID_USED) {
3079 /*
3080 * Loop ID already used, try next loop ID.
3081 */
3082 fcport->loop_id++;
3083 rval = qla2x00_find_new_loop_id(ha, fcport);
3084 if (rval != QLA_SUCCESS) {
3085 /* Ran out of loop IDs to use */
3086 break;
3087 }
3088 } else if (mb[0] == MBS_COMMAND_ERROR) {
3089 /*
3090 * Firmware possibly timed out during login. If NO
3091 * retries are left to do then the device is declared
3092 * dead.
3093 */
3094 *next_loopid = fcport->loop_id;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07003095 ha->isp_ops->fabric_logout(ha, fcport->loop_id,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07003096 fcport->d_id.b.domain, fcport->d_id.b.area,
3097 fcport->d_id.b.al_pa);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08003098 qla2x00_mark_device_lost(ha, fcport, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099
3100 rval = 1;
3101 break;
3102 } else {
3103 /*
3104 * unrecoverable / not handled error
3105 */
3106 DEBUG2(printk("%s(%ld): failed=%x port_id=%02x%02x%02x "
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003107 "loop_id=%x jiffies=%lx.\n",
3108 __func__, ha->host_no, mb[0],
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 fcport->d_id.b.domain, fcport->d_id.b.area,
3110 fcport->d_id.b.al_pa, fcport->loop_id, jiffies));
3111
3112 *next_loopid = fcport->loop_id;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07003113 ha->isp_ops->fabric_logout(ha, fcport->loop_id,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07003114 fcport->d_id.b.domain, fcport->d_id.b.area,
3115 fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 fcport->loop_id = FC_NO_LOOP_ID;
Andrew Vasquez0eedfcf2005-10-27 11:09:38 -07003117 fcport->login_retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118
3119 rval = 3;
3120 break;
3121 }
3122 }
3123
3124 return (rval);
3125}
3126
3127/*
3128 * qla2x00_local_device_login
3129 * Issue local device login command.
3130 *
3131 * Input:
3132 * ha = adapter block pointer.
3133 * loop_id = loop id of device to login to.
3134 *
3135 * Returns (Where's the #define!!!!):
3136 * 0 - Login successfully
3137 * 1 - Login failed
3138 * 3 - Fatal error
3139 */
3140int
andrew.vasquez@qlogic.com9a52a57c2006-03-09 14:27:44 -08003141qla2x00_local_device_login(scsi_qla_host_t *ha, fc_port_t *fcport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142{
3143 int rval;
3144 uint16_t mb[MAILBOX_REGISTER_COUNT];
3145
3146 memset(mb, 0, sizeof(mb));
andrew.vasquez@qlogic.com9a52a57c2006-03-09 14:27:44 -08003147 rval = qla2x00_login_local_device(ha, fcport, mb, BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 if (rval == QLA_SUCCESS) {
3149 /* Interrogate mailbox registers for any errors */
3150 if (mb[0] == MBS_COMMAND_ERROR)
3151 rval = 1;
3152 else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
3153 /* device not in PCB table */
3154 rval = 3;
3155 }
3156
3157 return (rval);
3158}
3159
3160/*
3161 * qla2x00_loop_resync
3162 * Resync with fibre channel devices.
3163 *
3164 * Input:
3165 * ha = adapter block pointer.
3166 *
3167 * Returns:
3168 * 0 = success
3169 */
3170int
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003171qla2x00_loop_resync(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172{
3173 int rval;
3174 uint32_t wait_time;
3175
3176 rval = QLA_SUCCESS;
3177
3178 atomic_set(&ha->loop_state, LOOP_UPDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3180 if (ha->flags.online) {
3181 if (!(rval = qla2x00_fw_ready(ha))) {
3182 /* Wait at most MAX_TARGET RSCNs for a stable link. */
3183 wait_time = 256;
3184 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 atomic_set(&ha->loop_state, LOOP_UPDATE);
3186
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003187 /* Issue a marker after FW becomes ready. */
3188 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
3189 ha->marker_needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190
3191 /* Remap devices on Loop. */
3192 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
3193
3194 qla2x00_configure_loop(ha);
3195 wait_time--;
3196 } while (!atomic_read(&ha->loop_down_timer) &&
3197 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
3198 wait_time &&
3199 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
3200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 }
3202
3203 if (test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
3204 return (QLA_FUNCTION_FAILED);
3205 }
3206
3207 if (rval) {
3208 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
3209 }
3210
3211 return (rval);
3212}
3213
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214void
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08003215qla2x00_update_fcports(scsi_qla_host_t *ha)
3216{
3217 fc_port_t *fcport;
3218
3219 /* Go with deferred removal of rport references. */
3220 list_for_each_entry(fcport, &ha->fcports, list)
3221 if (fcport->drport)
3222 qla2x00_rport_del(fcport);
3223}
3224
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225/*
3226* qla2x00_abort_isp
3227* Resets ISP and aborts all outstanding commands.
3228*
3229* Input:
3230* ha = adapter block pointer.
3231*
3232* Returns:
3233* 0 = success
3234*/
3235int
3236qla2x00_abort_isp(scsi_qla_host_t *ha)
3237{
Andrew Vasquez476e8972006-08-23 14:54:55 -07003238 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 uint8_t status = 0;
3240
3241 if (ha->flags.online) {
3242 ha->flags.online = 0;
3243 clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244
3245 qla_printk(KERN_INFO, ha,
3246 "Performing ISP error recovery - ha= %p.\n", ha);
Andrew Vasquezfd34f552007-07-19 15:06:00 -07003247 ha->isp_ops->reset_chip(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248
3249 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
3250 if (atomic_read(&ha->loop_state) != LOOP_DOWN) {
3251 atomic_set(&ha->loop_state, LOOP_DOWN);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08003252 qla2x00_mark_all_devices_lost(ha, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 } else {
3254 if (!atomic_read(&ha->loop_down_timer))
3255 atomic_set(&ha->loop_down_timer,
3256 LOOP_DOWN_TIME);
3257 }
3258
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 /* Requeue all commands in outstanding command list. */
Andrew Vasquezdf4bf0b2008-01-31 12:33:46 -08003260 qla2x00_abort_all_cmds(ha, DID_RESET << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261
Andrew Vasquezfd34f552007-07-19 15:06:00 -07003262 ha->isp_ops->get_flash_version(ha, ha->request_ring);
Andrew Vasquez30c47662007-01-29 10:22:21 -08003263
Andrew Vasquezfd34f552007-07-19 15:06:00 -07003264 ha->isp_ops->nvram_config(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265
3266 if (!qla2x00_restart_isp(ha)) {
3267 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3268
3269 if (!atomic_read(&ha->loop_down_timer)) {
3270 /*
3271 * Issue marker command only when we are going
3272 * to start the I/O .
3273 */
3274 ha->marker_needed = 1;
3275 }
3276
3277 ha->flags.online = 1;
3278
Andrew Vasquezfd34f552007-07-19 15:06:00 -07003279 ha->isp_ops->enable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003281 ha->isp_abort_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
Andrew Vasquez476e8972006-08-23 14:54:55 -07003283
3284 if (ha->eft) {
Andrew Vasquez0afb4672008-01-31 12:33:47 -08003285 memset(ha->eft, 0, EFT_SIZE);
Andrew Vasquez00b6bd22008-01-17 09:02:16 -08003286 rval = qla2x00_enable_eft_trace(ha,
Andrew Vasquez476e8972006-08-23 14:54:55 -07003287 ha->eft_dma, EFT_NUM_BUFFERS);
3288 if (rval) {
3289 qla_printk(KERN_WARNING, ha,
3290 "Unable to reinitialize EFT "
3291 "(%d).\n", rval);
3292 }
3293 }
Andrew Vasquezdf613b92008-01-17 09:02:17 -08003294
3295 if (ha->fce) {
3296 ha->flags.fce_enabled = 1;
3297 memset(ha->fce, 0,
3298 fce_calc_size(ha->fce_bufs));
3299 rval = qla2x00_enable_fce_trace(ha,
3300 ha->fce_dma, ha->fce_bufs, ha->fce_mb,
3301 &ha->fce_bufs);
3302 if (rval) {
3303 qla_printk(KERN_WARNING, ha,
3304 "Unable to reinitialize FCE "
3305 "(%d).\n", rval);
3306 ha->flags.fce_enabled = 0;
3307 }
3308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 } else { /* failed the ISP abort */
3310 ha->flags.online = 1;
3311 if (test_bit(ISP_ABORT_RETRY, &ha->dpc_flags)) {
3312 if (ha->isp_abort_cnt == 0) {
3313 qla_printk(KERN_WARNING, ha,
3314 "ISP error recovery failed - "
3315 "board disabled\n");
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003316 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 * The next call disables the board
3318 * completely.
3319 */
Andrew Vasquezfd34f552007-07-19 15:06:00 -07003320 ha->isp_ops->reset_adapter(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 ha->flags.online = 0;
3322 clear_bit(ISP_ABORT_RETRY,
3323 &ha->dpc_flags);
3324 status = 0;
3325 } else { /* schedule another ISP abort */
3326 ha->isp_abort_cnt--;
3327 DEBUG(printk("qla%ld: ISP abort - "
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003328 "retry remaining %d\n",
Andrew Vasquez744f11fd2006-06-23 16:11:05 -07003329 ha->host_no, ha->isp_abort_cnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 status = 1;
3331 }
3332 } else {
3333 ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
3334 DEBUG(printk("qla2x00(%ld): ISP error recovery "
3335 "- retrying (%d) more times\n",
Andrew Vasquez744f11fd2006-06-23 16:11:05 -07003336 ha->host_no, ha->isp_abort_cnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 set_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3338 status = 1;
3339 }
3340 }
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003341
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 }
3343
3344 if (status) {
3345 qla_printk(KERN_INFO, ha,
3346 "qla2x00_abort_isp: **** FAILED ****\n");
3347 } else {
3348 DEBUG(printk(KERN_INFO
3349 "qla2x00_abort_isp(%ld): exiting.\n",
Andrew Vasquez744f11fd2006-06-23 16:11:05 -07003350 ha->host_no));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 }
3352
3353 return(status);
3354}
3355
3356/*
3357* qla2x00_restart_isp
3358* restarts the ISP after a reset
3359*
3360* Input:
3361* ha = adapter block pointer.
3362*
3363* Returns:
3364* 0 = success
3365*/
3366static int
3367qla2x00_restart_isp(scsi_qla_host_t *ha)
3368{
3369 uint8_t status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 uint32_t wait_time;
3371
3372 /* If firmware needs to be loaded */
3373 if (qla2x00_isp_firmware(ha)) {
3374 ha->flags.online = 0;
Andrew Vasquez3db06522008-01-31 12:33:49 -08003375 if (!(status = ha->isp_ops->chip_diag(ha)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 status = qla2x00_setup_chip(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 }
3378
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 if (!status && !(status = qla2x00_init_rings(ha))) {
3380 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3381 if (!(status = qla2x00_fw_ready(ha))) {
3382 DEBUG(printk("%s(): Start configure loop, "
Andrew Vasquez744f11fd2006-06-23 16:11:05 -07003383 "status = %d\n", __func__, status));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003384
3385 /* Issue a marker after FW becomes ready. */
3386 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
3387
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 ha->flags.online = 1;
3389 /* Wait at most MAX_TARGET RSCNs for a stable link. */
3390 wait_time = 256;
3391 do {
3392 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
3393 qla2x00_configure_loop(ha);
3394 wait_time--;
3395 } while (!atomic_read(&ha->loop_down_timer) &&
3396 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
3397 wait_time &&
3398 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
3399 }
3400
3401 /* if no cable then assume it's good */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003402 if ((ha->device_flags & DFLG_NO_CABLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 status = 0;
3404
3405 DEBUG(printk("%s(): Configure loop done, status = 0x%x\n",
3406 __func__,
Andrew Vasquez744f11fd2006-06-23 16:11:05 -07003407 status));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 }
3409 return (status);
3410}
3411
3412/*
3413* qla2x00_reset_adapter
3414* Reset adapter.
3415*
3416* Input:
3417* ha = adapter block pointer.
3418*/
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003419void
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420qla2x00_reset_adapter(scsi_qla_host_t *ha)
3421{
3422 unsigned long flags = 0;
Andrew Vasquez3d716442005-07-06 10:30:26 -07003423 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424
3425 ha->flags.online = 0;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07003426 ha->isp_ops->disable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 spin_lock_irqsave(&ha->hardware_lock, flags);
3429 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
3430 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
3431 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
3432 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
3433 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3434}
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003435
3436void
3437qla24xx_reset_adapter(scsi_qla_host_t *ha)
3438{
3439 unsigned long flags = 0;
3440 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
3441
3442 ha->flags.online = 0;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07003443 ha->isp_ops->disable_intrs(ha);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003444
3445 spin_lock_irqsave(&ha->hardware_lock, flags);
3446 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
3447 RD_REG_DWORD(&reg->hccr);
3448 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
3449 RD_REG_DWORD(&reg->hccr);
3450 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3451}
3452
David Miller4e08df32007-04-16 12:37:43 -07003453/* On sparc systems, obtain port and node WWN from firmware
3454 * properties.
3455 */
3456static void qla24xx_nvram_wwn_from_ofw(scsi_qla_host_t *ha, struct nvram_24xx *nv)
3457{
3458#ifdef CONFIG_SPARC
3459 struct pci_dev *pdev = ha->pdev;
David S. Miller15576bc2007-05-08 00:36:49 -07003460 struct device_node *dp = pci_device_to_OF_node(pdev);
3461 const u8 *val;
David Miller4e08df32007-04-16 12:37:43 -07003462 int len;
3463
3464 val = of_get_property(dp, "port-wwn", &len);
3465 if (val && len >= WWN_SIZE)
3466 memcpy(nv->port_name, val, WWN_SIZE);
3467
3468 val = of_get_property(dp, "node-wwn", &len);
3469 if (val && len >= WWN_SIZE)
3470 memcpy(nv->node_name, val, WWN_SIZE);
3471#endif
3472}
3473
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003474int
3475qla24xx_nvram_config(scsi_qla_host_t *ha)
3476{
David Miller4e08df32007-04-16 12:37:43 -07003477 int rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003478 struct init_cb_24xx *icb;
3479 struct nvram_24xx *nv;
3480 uint32_t *dptr;
3481 uint8_t *dptr1, *dptr2;
3482 uint32_t chksum;
3483 uint16_t cnt;
3484
David Miller4e08df32007-04-16 12:37:43 -07003485 rval = QLA_SUCCESS;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003486 icb = (struct init_cb_24xx *)ha->init_cb;
Seokmann Ju281afe12007-07-26 13:43:34 -07003487 nv = ha->nvram;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003488
3489 /* Determine NVRAM starting address. */
3490 ha->nvram_size = sizeof(struct nvram_24xx);
3491 ha->nvram_base = FA_NVRAM_FUNC0_ADDR;
andrew.vasquez@qlogic.com6f641792006-03-09 14:27:34 -08003492 ha->vpd_size = FA_NVRAM_VPD_SIZE;
3493 ha->vpd_base = FA_NVRAM_VPD0_ADDR;
3494 if (PCI_FUNC(ha->pdev->devfn)) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003495 ha->nvram_base = FA_NVRAM_FUNC1_ADDR;
andrew.vasquez@qlogic.com6f641792006-03-09 14:27:34 -08003496 ha->vpd_base = FA_NVRAM_VPD1_ADDR;
3497 }
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003498
Seokmann Ju281afe12007-07-26 13:43:34 -07003499 /* Get VPD data into cache */
3500 ha->vpd = ha->nvram + VPD_OFFSET;
3501 ha->isp_ops->read_nvram(ha, (uint8_t *)ha->vpd,
3502 ha->nvram_base - FA_NVRAM_FUNC0_ADDR, FA_NVRAM_VPD_SIZE * 4);
3503
3504 /* Get NVRAM data into cache and calculate checksum. */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003505 dptr = (uint32_t *)nv;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07003506 ha->isp_ops->read_nvram(ha, (uint8_t *)dptr, ha->nvram_base,
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003507 ha->nvram_size);
3508 for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++)
3509 chksum += le32_to_cpu(*dptr++);
3510
3511 DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
Seokmann Ju281afe12007-07-26 13:43:34 -07003512 DEBUG5(qla2x00_dump_buffer((uint8_t *)nv, ha->nvram_size));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003513
3514 /* Bad NVRAM data, set defaults parameters. */
3515 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
3516 || nv->id[3] != ' ' ||
3517 nv->nvram_version < __constant_cpu_to_le16(ICB_VERSION)) {
3518 /* Reset NVRAM data. */
3519 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
3520 "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
3521 le16_to_cpu(nv->nvram_version));
David Miller4e08df32007-04-16 12:37:43 -07003522 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
3523 "invalid -- WWPN) defaults.\n");
3524
3525 /*
3526 * Set default initialization control block.
3527 */
3528 memset(nv, 0, ha->nvram_size);
3529 nv->nvram_version = __constant_cpu_to_le16(ICB_VERSION);
3530 nv->version = __constant_cpu_to_le16(ICB_VERSION);
3531 nv->frame_payload_size = __constant_cpu_to_le16(2048);
3532 nv->execution_throttle = __constant_cpu_to_le16(0xFFFF);
3533 nv->exchange_count = __constant_cpu_to_le16(0);
3534 nv->hard_address = __constant_cpu_to_le16(124);
3535 nv->port_name[0] = 0x21;
3536 nv->port_name[1] = 0x00 + PCI_FUNC(ha->pdev->devfn);
3537 nv->port_name[2] = 0x00;
3538 nv->port_name[3] = 0xe0;
3539 nv->port_name[4] = 0x8b;
3540 nv->port_name[5] = 0x1c;
3541 nv->port_name[6] = 0x55;
3542 nv->port_name[7] = 0x86;
3543 nv->node_name[0] = 0x20;
3544 nv->node_name[1] = 0x00;
3545 nv->node_name[2] = 0x00;
3546 nv->node_name[3] = 0xe0;
3547 nv->node_name[4] = 0x8b;
3548 nv->node_name[5] = 0x1c;
3549 nv->node_name[6] = 0x55;
3550 nv->node_name[7] = 0x86;
3551 qla24xx_nvram_wwn_from_ofw(ha, nv);
3552 nv->login_retry_count = __constant_cpu_to_le16(8);
3553 nv->interrupt_delay_timer = __constant_cpu_to_le16(0);
3554 nv->login_timeout = __constant_cpu_to_le16(0);
3555 nv->firmware_options_1 =
3556 __constant_cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
3557 nv->firmware_options_2 = __constant_cpu_to_le32(2 << 4);
3558 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_12);
3559 nv->firmware_options_3 = __constant_cpu_to_le32(2 << 13);
3560 nv->host_p = __constant_cpu_to_le32(BIT_11|BIT_10);
3561 nv->efi_parameters = __constant_cpu_to_le32(0);
3562 nv->reset_delay = 5;
3563 nv->max_luns_per_target = __constant_cpu_to_le16(128);
3564 nv->port_down_retry_count = __constant_cpu_to_le16(30);
3565 nv->link_down_timeout = __constant_cpu_to_le16(30);
3566
3567 rval = 1;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003568 }
3569
3570 /* Reset Initialization control block */
3571 memset(icb, 0, sizeof(struct init_cb_24xx));
3572
3573 /* Copy 1st segment. */
3574 dptr1 = (uint8_t *)icb;
3575 dptr2 = (uint8_t *)&nv->version;
3576 cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
3577 while (cnt--)
3578 *dptr1++ = *dptr2++;
3579
3580 icb->login_retry_count = nv->login_retry_count;
Andrew Vasquez3ea66e22006-06-23 16:11:27 -07003581 icb->link_down_on_nos = nv->link_down_on_nos;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003582
3583 /* Copy 2nd segment. */
3584 dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
3585 dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
3586 cnt = (uint8_t *)&icb->reserved_3 -
3587 (uint8_t *)&icb->interrupt_delay_timer;
3588 while (cnt--)
3589 *dptr1++ = *dptr2++;
3590
3591 /*
3592 * Setup driver NVRAM options.
3593 */
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08003594 qla2x00_set_model_info(ha, nv->model_name, sizeof(nv->model_name),
3595 "QLA2462");
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003596
Andrew Vasquez5341e862006-05-17 15:09:16 -07003597 /* Use alternate WWN? */
3598 if (nv->host_p & __constant_cpu_to_le32(BIT_15)) {
3599 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
3600 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
3601 }
3602
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003603 /* Prepare nodename */
Andrew Vasquezfd0e7e42006-05-17 15:09:11 -07003604 if ((icb->firmware_options_1 & __constant_cpu_to_le32(BIT_14)) == 0) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003605 /*
3606 * Firmware will apply the following mask if the nodename was
3607 * not provided.
3608 */
3609 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
3610 icb->node_name[0] &= 0xF0;
3611 }
3612
3613 /* Set host adapter parameters. */
3614 ha->flags.disable_risc_code_load = 0;
Andrew Vasquez0c8c39a2006-12-13 19:20:30 -08003615 ha->flags.enable_lip_reset = 0;
3616 ha->flags.enable_lip_full_login =
3617 le32_to_cpu(nv->host_p) & BIT_10 ? 1: 0;
3618 ha->flags.enable_target_reset =
3619 le32_to_cpu(nv->host_p) & BIT_11 ? 1: 0;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003620 ha->flags.enable_led_scheme = 0;
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07003621 ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003622
Andrew Vasquezfd0e7e42006-05-17 15:09:11 -07003623 ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
3624 (BIT_6 | BIT_5 | BIT_4)) >> 4;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003625
3626 memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
3627 sizeof(ha->fw_seriallink_options24));
3628
3629 /* save HBA serial number */
3630 ha->serial0 = icb->port_name[5];
3631 ha->serial1 = icb->port_name[6];
3632 ha->serial2 = icb->port_name[7];
3633 ha->node_name = icb->node_name;
3634 ha->port_name = icb->port_name;
3635
andrew.vasquez@qlogic.combc8fb3c2006-01-13 17:05:42 -08003636 icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
3637
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003638 ha->retry_count = le16_to_cpu(nv->login_retry_count);
3639
3640 /* Set minimum login_timeout to 4 seconds. */
3641 if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
3642 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
3643 if (le16_to_cpu(nv->login_timeout) < 4)
3644 nv->login_timeout = __constant_cpu_to_le16(4);
3645 ha->login_timeout = le16_to_cpu(nv->login_timeout);
3646 icb->login_timeout = cpu_to_le16(nv->login_timeout);
3647
Andrew Vasquez00a537b2008-02-28 14:06:11 -08003648 /* Set minimum RATOV to 100 tenths of a second. */
3649 ha->r_a_tov = 100;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003650
3651 ha->loop_reset_delay = nv->reset_delay;
3652
3653 /* Link Down Timeout = 0:
3654 *
3655 * When Port Down timer expires we will start returning
3656 * I/O's to OS with "DID_NO_CONNECT".
3657 *
3658 * Link Down Timeout != 0:
3659 *
3660 * The driver waits for the link to come up after link down
3661 * before returning I/Os to OS with "DID_NO_CONNECT".
3662 */
3663 if (le16_to_cpu(nv->link_down_timeout) == 0) {
3664 ha->loop_down_abort_time =
3665 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
3666 } else {
3667 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
3668 ha->loop_down_abort_time =
3669 (LOOP_DOWN_TIME - ha->link_down_timeout);
3670 }
3671
3672 /* Need enough time to try and get the port back. */
3673 ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
3674 if (qlport_down_retry)
3675 ha->port_down_retry_count = qlport_down_retry;
3676
3677 /* Set login_retry_count */
3678 ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
3679 if (ha->port_down_retry_count ==
3680 le16_to_cpu(nv->port_down_retry_count) &&
3681 ha->port_down_retry_count > 3)
3682 ha->login_retry_count = ha->port_down_retry_count;
3683 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
3684 ha->login_retry_count = ha->port_down_retry_count;
3685 if (ql2xloginretrycount)
3686 ha->login_retry_count = ql2xloginretrycount;
3687
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07003688 /* Enable ZIO. */
3689 if (!ha->flags.init_done) {
3690 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
3691 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
3692 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
3693 le16_to_cpu(icb->interrupt_delay_timer): 2;
3694 }
3695 icb->firmware_options_2 &= __constant_cpu_to_le32(
3696 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
3697 ha->flags.process_response_queue = 0;
3698 if (ha->zio_mode != QLA_ZIO_DISABLED) {
andrew.vasquez@qlogic.com4a59f712006-03-09 14:27:39 -08003699 ha->zio_mode = QLA_ZIO_MODE_6;
3700
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07003701 DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer delay "
3702 "(%d us).\n", ha->host_no, ha->zio_mode,
3703 ha->zio_timer * 100));
3704 qla_printk(KERN_INFO, ha,
3705 "ZIO mode %d enabled; timer delay (%d us).\n",
3706 ha->zio_mode, ha->zio_timer * 100);
3707
3708 icb->firmware_options_2 |= cpu_to_le32(
3709 (uint32_t)ha->zio_mode);
3710 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
3711 ha->flags.process_response_queue = 1;
3712 }
3713
David Miller4e08df32007-04-16 12:37:43 -07003714 if (rval) {
3715 DEBUG2_3(printk(KERN_WARNING
3716 "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
3717 }
3718 return (rval);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003719}
3720
Adrian Bunk413975a2006-06-30 02:33:06 -07003721static int
Andrew Vasquezd1c61902006-05-17 15:09:00 -07003722qla24xx_load_risc_flash(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3723{
3724 int rval;
3725 int segments, fragment;
3726 uint32_t faddr;
3727 uint32_t *dcode, dlen;
3728 uint32_t risc_addr;
3729 uint32_t risc_size;
3730 uint32_t i;
3731
3732 rval = QLA_SUCCESS;
3733
3734 segments = FA_RISC_CODE_SEGMENTS;
3735 faddr = FA_RISC_CODE_ADDR;
3736 dcode = (uint32_t *)ha->request_ring;
3737 *srisc_addr = 0;
3738
3739 /* Validate firmware image by checking version. */
3740 qla24xx_read_flash_data(ha, dcode, faddr + 4, 4);
3741 for (i = 0; i < 4; i++)
3742 dcode[i] = be32_to_cpu(dcode[i]);
3743 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
3744 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
3745 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
3746 dcode[3] == 0)) {
3747 qla_printk(KERN_WARNING, ha,
3748 "Unable to verify integrity of flash firmware image!\n");
3749 qla_printk(KERN_WARNING, ha,
3750 "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
3751 dcode[1], dcode[2], dcode[3]);
3752
3753 return QLA_FUNCTION_FAILED;
3754 }
3755
3756 while (segments && rval == QLA_SUCCESS) {
3757 /* Read segment's load information. */
3758 qla24xx_read_flash_data(ha, dcode, faddr, 4);
3759
3760 risc_addr = be32_to_cpu(dcode[2]);
3761 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
3762 risc_size = be32_to_cpu(dcode[3]);
3763
3764 fragment = 0;
3765 while (risc_size > 0 && rval == QLA_SUCCESS) {
3766 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
3767 if (dlen > risc_size)
3768 dlen = risc_size;
3769
3770 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3771 "addr %x, number of dwords 0x%x, offset 0x%x.\n",
3772 ha->host_no, risc_addr, dlen, faddr));
3773
3774 qla24xx_read_flash_data(ha, dcode, faddr, dlen);
3775 for (i = 0; i < dlen; i++)
3776 dcode[i] = swab32(dcode[i]);
3777
3778 rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3779 dlen);
3780 if (rval) {
3781 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3782 "segment %d of firmware\n", ha->host_no,
3783 fragment));
3784 qla_printk(KERN_WARNING, ha,
3785 "[ERROR] Failed to load segment %d of "
3786 "firmware\n", fragment);
3787 break;
3788 }
3789
3790 faddr += dlen;
3791 risc_addr += dlen;
3792 risc_size -= dlen;
3793 fragment++;
3794 }
3795
3796 /* Next segment. */
3797 segments--;
3798 }
3799
3800 return rval;
3801}
3802
Andrew Vasquezd1c61902006-05-17 15:09:00 -07003803#define QLA_FW_URL "ftp://ftp.qlogic.com/outgoing/linux/firmware/"
3804
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003805int
Andrew Vasquez54333832005-11-09 15:49:04 -08003806qla2x00_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3807{
3808 int rval;
3809 int i, fragment;
3810 uint16_t *wcode, *fwcode;
3811 uint32_t risc_addr, risc_size, fwclen, wlen, *seg;
3812 struct fw_blob *blob;
3813
3814 /* Load firmware blob. */
3815 blob = qla2x00_request_firmware(ha);
3816 if (!blob) {
3817 qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
Andrew Vasquezd1c61902006-05-17 15:09:00 -07003818 qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
3819 "from: " QLA_FW_URL ".\n");
Andrew Vasquez54333832005-11-09 15:49:04 -08003820 return QLA_FUNCTION_FAILED;
3821 }
3822
3823 rval = QLA_SUCCESS;
3824
3825 wcode = (uint16_t *)ha->request_ring;
3826 *srisc_addr = 0;
3827 fwcode = (uint16_t *)blob->fw->data;
3828 fwclen = 0;
3829
3830 /* Validate firmware image by checking version. */
3831 if (blob->fw->size < 8 * sizeof(uint16_t)) {
3832 qla_printk(KERN_WARNING, ha,
3833 "Unable to verify integrity of firmware image (%Zd)!\n",
3834 blob->fw->size);
3835 goto fail_fw_integrity;
3836 }
3837 for (i = 0; i < 4; i++)
3838 wcode[i] = be16_to_cpu(fwcode[i + 4]);
3839 if ((wcode[0] == 0xffff && wcode[1] == 0xffff && wcode[2] == 0xffff &&
3840 wcode[3] == 0xffff) || (wcode[0] == 0 && wcode[1] == 0 &&
3841 wcode[2] == 0 && wcode[3] == 0)) {
3842 qla_printk(KERN_WARNING, ha,
3843 "Unable to verify integrity of firmware image!\n");
3844 qla_printk(KERN_WARNING, ha,
3845 "Firmware data: %04x %04x %04x %04x!\n", wcode[0],
3846 wcode[1], wcode[2], wcode[3]);
3847 goto fail_fw_integrity;
3848 }
3849
3850 seg = blob->segs;
3851 while (*seg && rval == QLA_SUCCESS) {
3852 risc_addr = *seg;
3853 *srisc_addr = *srisc_addr == 0 ? *seg : *srisc_addr;
3854 risc_size = be16_to_cpu(fwcode[3]);
3855
3856 /* Validate firmware image size. */
3857 fwclen += risc_size * sizeof(uint16_t);
3858 if (blob->fw->size < fwclen) {
3859 qla_printk(KERN_WARNING, ha,
3860 "Unable to verify integrity of firmware image "
3861 "(%Zd)!\n", blob->fw->size);
3862 goto fail_fw_integrity;
3863 }
3864
3865 fragment = 0;
3866 while (risc_size > 0 && rval == QLA_SUCCESS) {
3867 wlen = (uint16_t)(ha->fw_transfer_size >> 1);
3868 if (wlen > risc_size)
3869 wlen = risc_size;
3870
3871 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3872 "addr %x, number of words 0x%x.\n", ha->host_no,
3873 risc_addr, wlen));
3874
3875 for (i = 0; i < wlen; i++)
3876 wcode[i] = swab16(fwcode[i]);
3877
3878 rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3879 wlen);
3880 if (rval) {
3881 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3882 "segment %d of firmware\n", ha->host_no,
3883 fragment));
3884 qla_printk(KERN_WARNING, ha,
3885 "[ERROR] Failed to load segment %d of "
3886 "firmware\n", fragment);
3887 break;
3888 }
3889
3890 fwcode += wlen;
3891 risc_addr += wlen;
3892 risc_size -= wlen;
3893 fragment++;
3894 }
3895
3896 /* Next segment. */
3897 seg++;
3898 }
3899 return rval;
3900
3901fail_fw_integrity:
3902 return QLA_FUNCTION_FAILED;
3903}
3904
3905int
3906qla24xx_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003907{
3908 int rval;
3909 int segments, fragment;
3910 uint32_t *dcode, dlen;
3911 uint32_t risc_addr;
3912 uint32_t risc_size;
3913 uint32_t i;
Andrew Vasquez54333832005-11-09 15:49:04 -08003914 struct fw_blob *blob;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003915 uint32_t *fwcode, fwclen;
3916
Andrew Vasquez54333832005-11-09 15:49:04 -08003917 /* Load firmware blob. */
3918 blob = qla2x00_request_firmware(ha);
3919 if (!blob) {
3920 qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
Andrew Vasquezd1c61902006-05-17 15:09:00 -07003921 qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
3922 "from: " QLA_FW_URL ".\n");
3923
3924 /* Try to load RISC code from flash. */
3925 qla_printk(KERN_ERR, ha, "Attempting to load (potentially "
3926 "outdated) firmware from flash.\n");
3927 return qla24xx_load_risc_flash(ha, srisc_addr);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003928 }
3929
3930 rval = QLA_SUCCESS;
3931
3932 segments = FA_RISC_CODE_SEGMENTS;
3933 dcode = (uint32_t *)ha->request_ring;
3934 *srisc_addr = 0;
Andrew Vasquez54333832005-11-09 15:49:04 -08003935 fwcode = (uint32_t *)blob->fw->data;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003936 fwclen = 0;
3937
3938 /* Validate firmware image by checking version. */
Andrew Vasquez54333832005-11-09 15:49:04 -08003939 if (blob->fw->size < 8 * sizeof(uint32_t)) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003940 qla_printk(KERN_WARNING, ha,
Andrew Vasquez54333832005-11-09 15:49:04 -08003941 "Unable to verify integrity of firmware image (%Zd)!\n",
3942 blob->fw->size);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003943 goto fail_fw_integrity;
3944 }
3945 for (i = 0; i < 4; i++)
3946 dcode[i] = be32_to_cpu(fwcode[i + 4]);
3947 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
3948 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
3949 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
3950 dcode[3] == 0)) {
3951 qla_printk(KERN_WARNING, ha,
Andrew Vasquez54333832005-11-09 15:49:04 -08003952 "Unable to verify integrity of firmware image!\n");
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003953 qla_printk(KERN_WARNING, ha,
3954 "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
3955 dcode[1], dcode[2], dcode[3]);
3956 goto fail_fw_integrity;
3957 }
3958
3959 while (segments && rval == QLA_SUCCESS) {
3960 risc_addr = be32_to_cpu(fwcode[2]);
3961 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
3962 risc_size = be32_to_cpu(fwcode[3]);
3963
3964 /* Validate firmware image size. */
3965 fwclen += risc_size * sizeof(uint32_t);
Andrew Vasquez54333832005-11-09 15:49:04 -08003966 if (blob->fw->size < fwclen) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003967 qla_printk(KERN_WARNING, ha,
Andrew Vasquez54333832005-11-09 15:49:04 -08003968 "Unable to verify integrity of firmware image "
3969 "(%Zd)!\n", blob->fw->size);
3970
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003971 goto fail_fw_integrity;
3972 }
3973
3974 fragment = 0;
3975 while (risc_size > 0 && rval == QLA_SUCCESS) {
3976 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
3977 if (dlen > risc_size)
3978 dlen = risc_size;
3979
3980 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3981 "addr %x, number of dwords 0x%x.\n", ha->host_no,
3982 risc_addr, dlen));
3983
3984 for (i = 0; i < dlen; i++)
3985 dcode[i] = swab32(fwcode[i]);
3986
andrew.vasquez@qlogic.com590f98e2006-01-13 17:05:37 -08003987 rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3988 dlen);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003989 if (rval) {
3990 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3991 "segment %d of firmware\n", ha->host_no,
3992 fragment));
3993 qla_printk(KERN_WARNING, ha,
3994 "[ERROR] Failed to load segment %d of "
3995 "firmware\n", fragment);
3996 break;
3997 }
3998
3999 fwcode += dlen;
4000 risc_addr += dlen;
4001 risc_size -= dlen;
4002 fragment++;
4003 }
4004
4005 /* Next segment. */
4006 segments--;
4007 }
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004008 return rval;
4009
4010fail_fw_integrity:
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004011 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004012}
Andrew Vasquez18c6c122006-10-13 09:33:38 -07004013
4014void
4015qla2x00_try_to_stop_firmware(scsi_qla_host_t *ha)
4016{
4017 int ret, retries;
4018
Andrew Vasqueze4289242007-07-19 15:05:56 -07004019 if (!IS_FWI2_CAPABLE(ha))
Andrew Vasquez18c6c122006-10-13 09:33:38 -07004020 return;
Andrew Vasquez75edf812007-05-07 07:43:00 -07004021 if (!ha->fw_major_version)
4022 return;
Andrew Vasquez18c6c122006-10-13 09:33:38 -07004023
4024 ret = qla2x00_stop_firmware(ha);
Andrew Vasquez7c7f1f22008-02-28 14:06:09 -08004025 for (retries = 5; ret != QLA_SUCCESS && ret != QLA_FUNCTION_TIMEOUT &&
4026 retries ; retries--) {
Andrew Vasquez18c6c122006-10-13 09:33:38 -07004027 qla2x00_reset_chip(ha);
4028 if (qla2x00_chip_diag(ha) != QLA_SUCCESS)
4029 continue;
4030 if (qla2x00_setup_chip(ha) != QLA_SUCCESS)
4031 continue;
4032 qla_printk(KERN_INFO, ha,
4033 "Attempting retry of stop-firmware command...\n");
4034 ret = qla2x00_stop_firmware(ha);
4035 }
4036}
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07004037
4038int
4039qla24xx_configure_vhba(scsi_qla_host_t *ha)
4040{
4041 int rval = QLA_SUCCESS;
4042 uint16_t mb[MAILBOX_REGISTER_COUNT];
4043
4044 if (!ha->parent)
4045 return -EINVAL;
4046
4047 rval = qla2x00_fw_ready(ha);
4048 if (rval == QLA_SUCCESS) {
4049 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
4050 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
4051 }
4052
4053 ha->flags.management_server_logged_in = 0;
4054
4055 /* Login to SNS first */
4056 qla24xx_login_fabric(ha, NPH_SNS, 0xff, 0xff, 0xfc,
4057 mb, BIT_1);
4058 if (mb[0] != MBS_COMMAND_COMPLETE) {
4059 DEBUG15(qla_printk(KERN_INFO, ha,
4060 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
4061 "mb[2]=%x mb[6]=%x mb[7]=%x\n", NPH_SNS,
4062 mb[0], mb[1], mb[2], mb[6], mb[7]));
4063 return (QLA_FUNCTION_FAILED);
4064 }
4065
4066 atomic_set(&ha->loop_down_timer, 0);
4067 atomic_set(&ha->loop_state, LOOP_UP);
4068 set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
4069 set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
4070 rval = qla2x00_loop_resync(ha);
4071
4072 return rval;
4073}
Harihara Kadayam4d4df192008-04-03 13:13:26 -07004074
4075/* 84XX Support **************************************************************/
4076
4077static LIST_HEAD(qla_cs84xx_list);
4078static DEFINE_MUTEX(qla_cs84xx_mutex);
4079
4080static struct qla_chip_state_84xx *
4081qla84xx_get_chip(struct scsi_qla_host *ha)
4082{
4083 struct qla_chip_state_84xx *cs84xx;
4084
4085 mutex_lock(&qla_cs84xx_mutex);
4086
4087 /* Find any shared 84xx chip. */
4088 list_for_each_entry(cs84xx, &qla_cs84xx_list, list) {
4089 if (cs84xx->bus == ha->pdev->bus) {
4090 kref_get(&cs84xx->kref);
4091 goto done;
4092 }
4093 }
4094
4095 cs84xx = kzalloc(sizeof(*cs84xx), GFP_KERNEL);
4096 if (!cs84xx)
4097 goto done;
4098
4099 kref_init(&cs84xx->kref);
4100 spin_lock_init(&cs84xx->access_lock);
4101 mutex_init(&cs84xx->fw_update_mutex);
4102 cs84xx->bus = ha->pdev->bus;
4103
4104 list_add_tail(&cs84xx->list, &qla_cs84xx_list);
4105done:
4106 mutex_unlock(&qla_cs84xx_mutex);
4107 return cs84xx;
4108}
4109
4110static void
4111__qla84xx_chip_release(struct kref *kref)
4112{
4113 struct qla_chip_state_84xx *cs84xx =
4114 container_of(kref, struct qla_chip_state_84xx, kref);
4115
4116 mutex_lock(&qla_cs84xx_mutex);
4117 list_del(&cs84xx->list);
4118 mutex_unlock(&qla_cs84xx_mutex);
4119 kfree(cs84xx);
4120}
4121
4122void
4123qla84xx_put_chip(struct scsi_qla_host *ha)
4124{
4125 if (ha->cs84xx)
4126 kref_put(&ha->cs84xx->kref, __qla84xx_chip_release);
4127}
4128
4129static int
4130qla84xx_init_chip(scsi_qla_host_t *ha)
4131{
4132 int rval;
4133 uint16_t status[2];
4134
4135 mutex_lock(&ha->cs84xx->fw_update_mutex);
4136
4137 rval = qla84xx_verify_chip(ha, status);
4138
4139 mutex_unlock(&ha->cs84xx->fw_update_mutex);
4140
4141 return rval != QLA_SUCCESS || status[0] ? QLA_FUNCTION_FAILED:
4142 QLA_SUCCESS;
4143}