blob: d429e2f5c70e10b1fe5849eaab2cec1e7e5b27cd [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070040/****************************************************************************/
41/* QLogic ISP2x00 Hardware Support Functions. */
42/****************************************************************************/
43
44/*
45* qla2x00_initialize_adapter
46* Initialize board.
47*
48* Input:
49* ha = adapter block pointer.
50*
51* Returns:
52* 0 = success
53*/
54int
55qla2x00_initialize_adapter(scsi_qla_host_t *ha)
56{
57 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59 /* Clear adapter flags. */
60 ha->flags.online = 0;
61 ha->flags.reset_active = 0;
62 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
63 atomic_set(&ha->loop_state, LOOP_DOWN);
Andrew Vasquez26032212007-01-29 10:22:23 -080064 ha->device_flags = DFLG_NO_CABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 ha->dpc_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 ha->flags.management_server_logged_in = 0;
67 ha->marker_needed = 0;
68 ha->mbx_flags = 0;
69 ha->isp_abort_cnt = 0;
70 ha->beacon_blink_led = 0;
Andrew Vasquezcca53352005-08-26 19:08:30 -070071 set_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Andrew Vasquez0107109e2005-07-06 10:31:37 -070073 qla_printk(KERN_INFO, ha, "Configuring PCI space...\n");
Andrew Vasquezfd34f552007-07-19 15:06:00 -070074 rval = ha->isp_ops->pci_config(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 if (rval) {
Andrew Vasquez7c98a042007-01-29 10:22:29 -080076 DEBUG2(printk("scsi(%ld): Unable to configure PCI space.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 ha->host_no));
78 return (rval);
79 }
80
Andrew Vasquezfd34f552007-07-19 15:06:00 -070081 ha->isp_ops->reset_chip(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Andrew Vasquezfd34f552007-07-19 15:06:00 -070083 ha->isp_ops->get_flash_version(ha, ha->request_ring);
Andrew Vasquez30c47662007-01-29 10:22:21 -080084
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 qla_printk(KERN_INFO, ha, "Configure NVRAM parameters...\n");
Andrew Vasquez0107109e2005-07-06 10:31:37 -070086
Andrew Vasquezfd34f552007-07-19 15:06:00 -070087 ha->isp_ops->nvram_config(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Andrew Vasquezd4c760c2006-06-23 16:10:39 -070089 if (ha->flags.disable_serdes) {
90 /* Mask HBA via NVRAM settings? */
91 qla_printk(KERN_INFO, ha, "Masking HBA WWPN "
92 "%02x%02x%02x%02x%02x%02x%02x%02x (via NVRAM).\n",
93 ha->port_name[0], ha->port_name[1],
94 ha->port_name[2], ha->port_name[3],
95 ha->port_name[4], ha->port_name[5],
96 ha->port_name[6], ha->port_name[7]);
97 return QLA_FUNCTION_FAILED;
98 }
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 qla_printk(KERN_INFO, ha, "Verifying loaded RISC code...\n");
101
Andrew Vasquezd19044c2006-11-22 08:22:19 -0800102 if (qla2x00_isp_firmware(ha) != QLA_SUCCESS) {
Andrew Vasquezfd34f552007-07-19 15:06:00 -0700103 rval = ha->isp_ops->chip_diag(ha);
Andrew Vasquezd19044c2006-11-22 08:22:19 -0800104 if (rval)
105 return (rval);
106 rval = qla2x00_setup_chip(ha);
107 if (rval)
108 return (rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 }
Andrew Vasquezd19044c2006-11-22 08:22:19 -0800110 rval = qla2x00_init_rings(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 return (rval);
113}
114
115/**
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700116 * qla2100_pci_config() - Setup ISP21xx PCI configuration registers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 * @ha: HA context
118 *
119 * Returns 0 on success.
120 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700121int
122qla2100_pci_config(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Andrew Vasqueza157b102007-05-07 07:43:01 -0700124 uint16_t w;
Adam Kropelin27b2f672005-09-16 19:28:20 -0700125 uint32_t d;
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700126 unsigned long flags;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700127 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 pci_set_master(ha->pdev);
Andrew Vasquezaf6177d2007-07-19 15:06:02 -0700130 pci_try_set_mwi(ha->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
Andrew Vasqueza157b102007-05-07 07:43:01 -0700133 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
135
136 /* Reset expansion ROM address decode enable */
Adam Kropelin27b2f672005-09-16 19:28:20 -0700137 pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
138 d &= ~PCI_ROM_ADDRESS_ENABLE;
139 pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700141 /* Get PCI bus information. */
142 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez3d716442005-07-06 10:30:26 -0700143 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700144 spin_unlock_irqrestore(&ha->hardware_lock, flags);
145
146 return QLA_SUCCESS;
147}
148
149/**
150 * qla2300_pci_config() - Setup ISP23xx PCI configuration registers.
151 * @ha: HA context
152 *
153 * Returns 0 on success.
154 */
155int
156qla2300_pci_config(scsi_qla_host_t *ha)
157{
Andrew Vasqueza157b102007-05-07 07:43:01 -0700158 uint16_t w;
Adam Kropelin27b2f672005-09-16 19:28:20 -0700159 uint32_t d;
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700160 unsigned long flags = 0;
161 uint32_t cnt;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700162 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700163
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700164 pci_set_master(ha->pdev);
Andrew Vasquezaf6177d2007-07-19 15:06:02 -0700165 pci_try_set_mwi(ha->pdev);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700166
167 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
Andrew Vasqueza157b102007-05-07 07:43:01 -0700168 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700169
170 if (IS_QLA2322(ha) || IS_QLA6322(ha))
171 w &= ~PCI_COMMAND_INTX_DISABLE;
Andrew Vasqueza157b102007-05-07 07:43:01 -0700172 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700173
174 /*
175 * If this is a 2300 card and not 2312, reset the
176 * COMMAND_INVALIDATE due to a bug in the 2300. Unfortunately,
177 * the 2310 also reports itself as a 2300 so we need to get the
178 * fb revision level -- a 6 indicates it really is a 2300 and
179 * not a 2310.
180 */
181 if (IS_QLA2300(ha)) {
182 spin_lock_irqsave(&ha->hardware_lock, flags);
183
184 /* Pause RISC. */
Andrew Vasquez3d716442005-07-06 10:30:26 -0700185 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700186 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquez3d716442005-07-06 10:30:26 -0700187 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700188 break;
189
190 udelay(10);
191 }
192
193 /* Select FPM registers. */
Andrew Vasquez3d716442005-07-06 10:30:26 -0700194 WRT_REG_WORD(&reg->ctrl_status, 0x20);
195 RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700196
197 /* Get the fb rev level */
Andrew Vasquez3d716442005-07-06 10:30:26 -0700198 ha->fb_rev = RD_FB_CMD_REG(ha, reg);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700199
200 if (ha->fb_rev == FPM_2300)
Andrew Vasqueza157b102007-05-07 07:43:01 -0700201 pci_clear_mwi(ha->pdev);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700202
203 /* Deselect FPM registers. */
Andrew Vasquez3d716442005-07-06 10:30:26 -0700204 WRT_REG_WORD(&reg->ctrl_status, 0x0);
205 RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700206
207 /* Release RISC module. */
Andrew Vasquez3d716442005-07-06 10:30:26 -0700208 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700209 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquez3d716442005-07-06 10:30:26 -0700210 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) == 0)
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700211 break;
212
213 udelay(10);
214 }
215
216 spin_unlock_irqrestore(&ha->hardware_lock, flags);
217 }
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700218
219 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
220
221 /* Reset expansion ROM address decode enable */
Adam Kropelin27b2f672005-09-16 19:28:20 -0700222 pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
223 d &= ~PCI_ROM_ADDRESS_ENABLE;
224 pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700225
226 /* Get PCI bus information. */
227 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez3d716442005-07-06 10:30:26 -0700228 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700229 spin_unlock_irqrestore(&ha->hardware_lock, flags);
230
231 return QLA_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
234/**
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700235 * qla24xx_pci_config() - Setup ISP24xx PCI configuration registers.
236 * @ha: HA context
237 *
238 * Returns 0 on success.
239 */
240int
241qla24xx_pci_config(scsi_qla_host_t *ha)
242{
Andrew Vasqueza157b102007-05-07 07:43:01 -0700243 uint16_t w;
Adam Kropelin27b2f672005-09-16 19:28:20 -0700244 uint32_t d;
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700245 unsigned long flags = 0;
246 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700247
248 pci_set_master(ha->pdev);
Andrew Vasquezaf6177d2007-07-19 15:06:02 -0700249 pci_try_set_mwi(ha->pdev);
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700250
251 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
Andrew Vasqueza157b102007-05-07 07:43:01 -0700252 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700253 w &= ~PCI_COMMAND_INTX_DISABLE;
254 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
255
256 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
257
258 /* PCI-X -- adjust Maximum Memory Read Byte Count (2048). */
Andrew Vasquezf85ec182007-07-19 15:06:01 -0700259 if (pci_find_capability(ha->pdev, PCI_CAP_ID_PCIX))
260 pcix_set_mmrbc(ha->pdev, 2048);
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700261
262 /* PCIe -- adjust Maximum Read Request Size (2048). */
Andrew Vasquezf85ec182007-07-19 15:06:01 -0700263 if (pci_find_capability(ha->pdev, PCI_CAP_ID_EXP))
264 pcie_set_readrq(ha->pdev, 2048);
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700265
266 /* Reset expansion ROM address decode enable */
Adam Kropelin27b2f672005-09-16 19:28:20 -0700267 pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
268 d &= ~PCI_ROM_ADDRESS_ENABLE;
269 pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700270
Auke Kok44c10132007-06-08 15:46:36 -0700271 ha->chip_revision = ha->pdev->revision;
Andrew Vasqueza8488ab2007-01-29 10:22:19 -0800272
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700273 /* Get PCI bus information. */
274 spin_lock_irqsave(&ha->hardware_lock, flags);
275 ha->pci_attr = RD_REG_DWORD(&reg->ctrl_status);
276 spin_unlock_irqrestore(&ha->hardware_lock, flags);
277
278 return QLA_SUCCESS;
279}
280
281/**
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -0700282 * qla25xx_pci_config() - Setup ISP25xx PCI configuration registers.
283 * @ha: HA context
284 *
285 * Returns 0 on success.
286 */
287int
288qla25xx_pci_config(scsi_qla_host_t *ha)
289{
290 uint16_t w;
291 uint32_t d;
292
293 pci_set_master(ha->pdev);
294 pci_try_set_mwi(ha->pdev);
295
296 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
297 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
298 w &= ~PCI_COMMAND_INTX_DISABLE;
299 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
300
301 /* PCIe -- adjust Maximum Read Request Size (2048). */
302 if (pci_find_capability(ha->pdev, PCI_CAP_ID_EXP))
303 pcie_set_readrq(ha->pdev, 2048);
304
305 /* Reset expansion ROM address decode enable */
306 pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
307 d &= ~PCI_ROM_ADDRESS_ENABLE;
308 pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
309
310 ha->chip_revision = ha->pdev->revision;
311
312 return QLA_SUCCESS;
313}
314
315/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 * qla2x00_isp_firmware() - Choose firmware image.
317 * @ha: HA context
318 *
319 * Returns 0 on success.
320 */
321static int
322qla2x00_isp_firmware(scsi_qla_host_t *ha)
323{
324 int rval;
325
326 /* Assume loading risc code */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700327 rval = QLA_FUNCTION_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 if (ha->flags.disable_risc_code_load) {
330 DEBUG2(printk("scsi(%ld): RISC CODE NOT loaded\n",
331 ha->host_no));
332 qla_printk(KERN_INFO, ha, "RISC CODE NOT loaded\n");
333
334 /* Verify checksum of loaded RISC code. */
Andrew Vasquez441d1072006-05-17 15:09:34 -0700335 rval = qla2x00_verify_checksum(ha, ha->fw_srisc_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
337
338 if (rval) {
339 DEBUG2_3(printk("scsi(%ld): **** Load RISC code ****\n",
340 ha->host_no));
341 }
342
343 return (rval);
344}
345
346/**
347 * qla2x00_reset_chip() - Reset ISP chip.
348 * @ha: HA context
349 *
350 * Returns 0 on success.
351 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700352void
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700353qla2x00_reset_chip(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
355 unsigned long flags = 0;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700356 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 uint32_t cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 uint16_t cmd;
359
Andrew Vasquezfd34f552007-07-19 15:06:00 -0700360 ha->isp_ops->disable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 spin_lock_irqsave(&ha->hardware_lock, flags);
363
364 /* Turn off master enable */
365 cmd = 0;
366 pci_read_config_word(ha->pdev, PCI_COMMAND, &cmd);
367 cmd &= ~PCI_COMMAND_MASTER;
368 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
369
370 if (!IS_QLA2100(ha)) {
371 /* Pause RISC. */
372 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
373 if (IS_QLA2200(ha) || IS_QLA2300(ha)) {
374 for (cnt = 0; cnt < 30000; cnt++) {
375 if ((RD_REG_WORD(&reg->hccr) &
376 HCCR_RISC_PAUSE) != 0)
377 break;
378 udelay(100);
379 }
380 } else {
381 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
382 udelay(10);
383 }
384
385 /* Select FPM registers. */
386 WRT_REG_WORD(&reg->ctrl_status, 0x20);
387 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
388
389 /* FPM Soft Reset. */
390 WRT_REG_WORD(&reg->fpm_diag_config, 0x100);
391 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
392
393 /* Toggle Fpm Reset. */
394 if (!IS_QLA2200(ha)) {
395 WRT_REG_WORD(&reg->fpm_diag_config, 0x0);
396 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
397 }
398
399 /* Select frame buffer registers. */
400 WRT_REG_WORD(&reg->ctrl_status, 0x10);
401 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
402
403 /* Reset frame buffer FIFOs. */
404 if (IS_QLA2200(ha)) {
405 WRT_FB_CMD_REG(ha, reg, 0xa000);
406 RD_FB_CMD_REG(ha, reg); /* PCI Posting. */
407 } else {
408 WRT_FB_CMD_REG(ha, reg, 0x00fc);
409
410 /* Read back fb_cmd until zero or 3 seconds max */
411 for (cnt = 0; cnt < 3000; cnt++) {
412 if ((RD_FB_CMD_REG(ha, reg) & 0xff) == 0)
413 break;
414 udelay(100);
415 }
416 }
417
418 /* Select RISC module registers. */
419 WRT_REG_WORD(&reg->ctrl_status, 0);
420 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
421
422 /* Reset RISC processor. */
423 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
424 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
425
426 /* Release RISC processor. */
427 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
428 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
429 }
430
431 WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
432 WRT_REG_WORD(&reg->hccr, HCCR_CLR_HOST_INT);
433
434 /* Reset ISP chip. */
435 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
436
437 /* Wait for RISC to recover from reset. */
438 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
439 /*
440 * It is necessary to for a delay here since the card doesn't
441 * respond to PCI reads during a reset. On some architectures
442 * this will result in an MCA.
443 */
444 udelay(20);
445 for (cnt = 30000; cnt; cnt--) {
446 if ((RD_REG_WORD(&reg->ctrl_status) &
447 CSR_ISP_SOFT_RESET) == 0)
448 break;
449 udelay(100);
450 }
451 } else
452 udelay(10);
453
454 /* Reset RISC processor. */
455 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
456
457 WRT_REG_WORD(&reg->semaphore, 0);
458
459 /* Release RISC processor. */
460 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
461 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
462
463 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
464 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquezffb39f02006-05-17 15:09:06 -0700465 if (RD_MAILBOX_REG(ha, reg, 0) != MBS_BUSY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 udelay(100);
469 }
470 } else
471 udelay(100);
472
473 /* Turn on master enable */
474 cmd |= PCI_COMMAND_MASTER;
475 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
476
477 /* Disable RISC pause on FPM parity error. */
478 if (!IS_QLA2100(ha)) {
479 WRT_REG_WORD(&reg->hccr, HCCR_DISABLE_PARITY_PAUSE);
480 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
481 }
482
483 spin_unlock_irqrestore(&ha->hardware_lock, flags);
484}
485
486/**
Andrew Vasquez88c26662005-07-08 17:59:26 -0700487 * qla24xx_reset_risc() - Perform full reset of ISP24xx RISC.
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700488 * @ha: HA context
489 *
490 * Returns 0 on success.
491 */
Andrew Vasquez88c26662005-07-08 17:59:26 -0700492static inline void
493qla24xx_reset_risc(scsi_qla_host_t *ha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700494{
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -0700495 int hw_evt = 0;
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700496 unsigned long flags = 0;
497 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
498 uint32_t cnt, d2;
Andrew Vasquez335a1cc2005-11-08 14:37:48 -0800499 uint16_t wd;
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700500
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700501 spin_lock_irqsave(&ha->hardware_lock, flags);
502
503 /* Reset RISC. */
504 WRT_REG_DWORD(&reg->ctrl_status, CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
505 for (cnt = 0; cnt < 30000; cnt++) {
506 if ((RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE) == 0)
507 break;
508
509 udelay(10);
510 }
511
512 WRT_REG_DWORD(&reg->ctrl_status,
513 CSRX_ISP_SOFT_RESET|CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
Andrew Vasquez335a1cc2005-11-08 14:37:48 -0800514 pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
Andrew Vasquez88c26662005-07-08 17:59:26 -0700515
Andrew Vasquez335a1cc2005-11-08 14:37:48 -0800516 udelay(100);
Andrew Vasquez88c26662005-07-08 17:59:26 -0700517 /* Wait for firmware to complete NVRAM accesses. */
Andrew Vasquez88c26662005-07-08 17:59:26 -0700518 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
519 for (cnt = 10000 ; cnt && d2; cnt--) {
520 udelay(5);
521 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
522 barrier();
523 }
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -0700524 if (cnt == 0)
525 hw_evt = 1;
Andrew Vasquez88c26662005-07-08 17:59:26 -0700526
Andrew Vasquez335a1cc2005-11-08 14:37:48 -0800527 /* Wait for soft-reset to complete. */
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700528 d2 = RD_REG_DWORD(&reg->ctrl_status);
529 for (cnt = 6000000 ; cnt && (d2 & CSRX_ISP_SOFT_RESET); cnt--) {
530 udelay(5);
531 d2 = RD_REG_DWORD(&reg->ctrl_status);
532 barrier();
533 }
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -0700534 if (cnt == 0 || hw_evt)
535 qla2xxx_hw_event_log(ha, HW_EVENT_RESET_ERR,
536 RD_REG_WORD(&reg->mailbox1), RD_REG_WORD(&reg->mailbox2),
537 RD_REG_WORD(&reg->mailbox3));
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700538
539 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
540 RD_REG_DWORD(&reg->hccr);
541
542 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
543 RD_REG_DWORD(&reg->hccr);
544
545 WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_RESET);
546 RD_REG_DWORD(&reg->hccr);
547
548 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
549 for (cnt = 6000000 ; cnt && d2; cnt--) {
550 udelay(5);
551 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
552 barrier();
553 }
554
555 spin_unlock_irqrestore(&ha->hardware_lock, flags);
556}
557
558/**
Andrew Vasquez88c26662005-07-08 17:59:26 -0700559 * qla24xx_reset_chip() - Reset ISP24xx chip.
560 * @ha: HA context
561 *
562 * Returns 0 on success.
563 */
564void
565qla24xx_reset_chip(scsi_qla_host_t *ha)
566{
Andrew Vasquezfd34f552007-07-19 15:06:00 -0700567 ha->isp_ops->disable_intrs(ha);
Andrew Vasquez88c26662005-07-08 17:59:26 -0700568
569 /* Perform RISC reset. */
570 qla24xx_reset_risc(ha);
571}
572
573/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 * qla2x00_chip_diag() - Test chip for proper operation.
575 * @ha: HA context
576 *
577 * Returns 0 on success.
578 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700579int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580qla2x00_chip_diag(scsi_qla_host_t *ha)
581{
582 int rval;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700583 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 unsigned long flags = 0;
585 uint16_t data;
586 uint32_t cnt;
587 uint16_t mb[5];
588
589 /* Assume a failed state */
590 rval = QLA_FUNCTION_FAILED;
591
592 DEBUG3(printk("scsi(%ld): Testing device at %lx.\n",
593 ha->host_no, (u_long)&reg->flash_address));
594
595 spin_lock_irqsave(&ha->hardware_lock, flags);
596
597 /* Reset ISP chip. */
598 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
599
600 /*
601 * We need to have a delay here since the card will not respond while
602 * in reset causing an MCA on some architectures.
603 */
604 udelay(20);
605 data = qla2x00_debounce_register(&reg->ctrl_status);
606 for (cnt = 6000000 ; cnt && (data & CSR_ISP_SOFT_RESET); cnt--) {
607 udelay(5);
608 data = RD_REG_WORD(&reg->ctrl_status);
609 barrier();
610 }
611
612 if (!cnt)
613 goto chip_diag_failed;
614
615 DEBUG3(printk("scsi(%ld): Reset register cleared by chip reset\n",
616 ha->host_no));
617
618 /* Reset RISC processor. */
619 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
620 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
621
622 /* Workaround for QLA2312 PCI parity error */
623 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
624 data = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 0));
625 for (cnt = 6000000; cnt && (data == MBS_BUSY); cnt--) {
626 udelay(5);
627 data = RD_MAILBOX_REG(ha, reg, 0);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700628 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
630 } else
631 udelay(10);
632
633 if (!cnt)
634 goto chip_diag_failed;
635
636 /* Check product ID of chip */
637 DEBUG3(printk("scsi(%ld): Checking product ID of chip\n", ha->host_no));
638
639 mb[1] = RD_MAILBOX_REG(ha, reg, 1);
640 mb[2] = RD_MAILBOX_REG(ha, reg, 2);
641 mb[3] = RD_MAILBOX_REG(ha, reg, 3);
642 mb[4] = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 4));
643 if (mb[1] != PROD_ID_1 || (mb[2] != PROD_ID_2 && mb[2] != PROD_ID_2a) ||
644 mb[3] != PROD_ID_3) {
645 qla_printk(KERN_WARNING, ha,
646 "Wrong product ID = 0x%x,0x%x,0x%x\n", mb[1], mb[2], mb[3]);
647
648 goto chip_diag_failed;
649 }
650 ha->product_id[0] = mb[1];
651 ha->product_id[1] = mb[2];
652 ha->product_id[2] = mb[3];
653 ha->product_id[3] = mb[4];
654
655 /* Adjust fw RISC transfer size */
656 if (ha->request_q_length > 1024)
657 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
658 else
659 ha->fw_transfer_size = REQUEST_ENTRY_SIZE *
660 ha->request_q_length;
661
662 if (IS_QLA2200(ha) &&
663 RD_MAILBOX_REG(ha, reg, 7) == QLA2200A_RISC_ROM_VER) {
664 /* Limit firmware transfer size with a 2200A */
665 DEBUG3(printk("scsi(%ld): Found QLA2200A chip.\n",
666 ha->host_no));
667
andrew.vasquez@qlogic.comea5b6382006-03-09 14:27:08 -0800668 ha->device_type |= DT_ISP2200A;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 ha->fw_transfer_size = 128;
670 }
671
672 /* Wrap Incoming Mailboxes Test. */
673 spin_unlock_irqrestore(&ha->hardware_lock, flags);
674
675 DEBUG3(printk("scsi(%ld): Checking mailboxes.\n", ha->host_no));
676 rval = qla2x00_mbx_reg_test(ha);
677 if (rval) {
678 DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
679 ha->host_no));
680 qla_printk(KERN_WARNING, ha,
681 "Failed mailbox send register test\n");
682 }
683 else {
684 /* Flag a successful rval */
685 rval = QLA_SUCCESS;
686 }
687 spin_lock_irqsave(&ha->hardware_lock, flags);
688
689chip_diag_failed:
690 if (rval)
691 DEBUG2_3(printk("scsi(%ld): Chip diagnostics **** FAILED "
692 "****\n", ha->host_no));
693
694 spin_unlock_irqrestore(&ha->hardware_lock, flags);
695
696 return (rval);
697}
698
699/**
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700700 * qla24xx_chip_diag() - Test ISP24xx for proper operation.
701 * @ha: HA context
702 *
703 * Returns 0 on success.
704 */
705int
706qla24xx_chip_diag(scsi_qla_host_t *ha)
707{
708 int rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700709
Andrew Vasquez88c26662005-07-08 17:59:26 -0700710 /* Perform RISC reset. */
711 qla24xx_reset_risc(ha);
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700712
713 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
714
715 rval = qla2x00_mbx_reg_test(ha);
716 if (rval) {
717 DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
718 ha->host_no));
719 qla_printk(KERN_WARNING, ha,
720 "Failed mailbox send register test\n");
721 } else {
722 /* Flag a successful rval */
723 rval = QLA_SUCCESS;
724 }
725
726 return rval;
727}
728
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700729void
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700730qla2x00_alloc_fw_dump(scsi_qla_host_t *ha)
731{
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700732 int rval;
733 uint32_t dump_size, fixed_size, mem_size, req_q_size, rsp_q_size,
Andrew Vasquezdf613b92008-01-17 09:02:17 -0800734 eft_size, fce_size;
735 dma_addr_t tc_dma;
736 void *tc;
Andrew Vasquezd4e3e042006-05-17 15:09:50 -0700737
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700738 if (ha->fw_dump) {
739 qla_printk(KERN_WARNING, ha,
740 "Firmware dump previously allocated.\n");
741 return;
Andrew Vasquezd4e3e042006-05-17 15:09:50 -0700742 }
743
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700744 ha->fw_dumped = 0;
Andrew Vasquezdf613b92008-01-17 09:02:17 -0800745 fixed_size = mem_size = eft_size = fce_size = 0;
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700746 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
747 fixed_size = sizeof(struct qla2100_fw_dump);
748 } else if (IS_QLA23XX(ha)) {
749 fixed_size = offsetof(struct qla2300_fw_dump, data_ram);
750 mem_size = (ha->fw_memory_size - 0x11000 + 1) *
751 sizeof(uint16_t);
Andrew Vasqueze4289242007-07-19 15:05:56 -0700752 } else if (IS_FWI2_CAPABLE(ha)) {
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -0700753 fixed_size = IS_QLA25XX(ha) ?
754 offsetof(struct qla25xx_fw_dump, ext_mem):
755 offsetof(struct qla24xx_fw_dump, ext_mem);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700756 mem_size = (ha->fw_memory_size - 0x100000 + 1) *
757 sizeof(uint32_t);
758
759 /* Allocate memory for Extended Trace Buffer. */
Andrew Vasquezdf613b92008-01-17 09:02:17 -0800760 tc = dma_alloc_coherent(&ha->pdev->dev, EFT_SIZE, &tc_dma,
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700761 GFP_KERNEL);
Andrew Vasquezdf613b92008-01-17 09:02:17 -0800762 if (!tc) {
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700763 qla_printk(KERN_WARNING, ha, "Unable to allocate "
764 "(%d KB) for EFT.\n", EFT_SIZE / 1024);
765 goto cont_alloc;
766 }
767
Andrew Vasquezfc447652008-01-17 09:02:18 -0800768 memset(tc, 0, EFT_SIZE);
Andrew Vasquezdf613b92008-01-17 09:02:17 -0800769 rval = qla2x00_enable_eft_trace(ha, tc_dma, EFT_NUM_BUFFERS);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700770 if (rval) {
771 qla_printk(KERN_WARNING, ha, "Unable to initialize "
772 "EFT (%d).\n", rval);
Andrew Vasquezdf613b92008-01-17 09:02:17 -0800773 dma_free_coherent(&ha->pdev->dev, EFT_SIZE, tc,
774 tc_dma);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700775 goto cont_alloc;
776 }
777
778 qla_printk(KERN_INFO, ha, "Allocated (%d KB) for EFT...\n",
779 EFT_SIZE / 1024);
780
781 eft_size = EFT_SIZE;
Andrew Vasquezdf613b92008-01-17 09:02:17 -0800782 ha->eft_dma = tc_dma;
783 ha->eft = tc;
784
785 /* Allocate memory for Fibre Channel Event Buffer. */
786 if (!IS_QLA25XX(ha))
787 goto cont_alloc;
788
789 tc = dma_alloc_coherent(&ha->pdev->dev, FCE_SIZE, &tc_dma,
790 GFP_KERNEL);
791 if (!tc) {
792 qla_printk(KERN_WARNING, ha, "Unable to allocate "
793 "(%d KB) for FCE.\n", FCE_SIZE / 1024);
794 goto cont_alloc;
795 }
796
797 memset(tc, 0, FCE_SIZE);
798 rval = qla2x00_enable_fce_trace(ha, tc_dma, FCE_NUM_BUFFERS,
799 ha->fce_mb, &ha->fce_bufs);
800 if (rval) {
801 qla_printk(KERN_WARNING, ha, "Unable to initialize "
802 "FCE (%d).\n", rval);
803 dma_free_coherent(&ha->pdev->dev, FCE_SIZE, tc,
804 tc_dma);
805 ha->flags.fce_enabled = 0;
806 goto cont_alloc;
807 }
808
809 qla_printk(KERN_INFO, ha, "Allocated (%d KB) for FCE...\n",
810 FCE_SIZE / 1024);
811
812 fce_size = sizeof(struct qla2xxx_fce_chain) + EFT_SIZE;
813 ha->flags.fce_enabled = 1;
814 ha->fce_dma = tc_dma;
815 ha->fce = tc;
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700816 }
817cont_alloc:
818 req_q_size = ha->request_q_length * sizeof(request_t);
819 rsp_q_size = ha->response_q_length * sizeof(response_t);
820
821 dump_size = offsetof(struct qla2xxx_fw_dump, isp);
822 dump_size += fixed_size + mem_size + req_q_size + rsp_q_size +
Andrew Vasquezdf613b92008-01-17 09:02:17 -0800823 eft_size + fce_size;
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700824
Andrew Vasquezd4e3e042006-05-17 15:09:50 -0700825 ha->fw_dump = vmalloc(dump_size);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700826 if (!ha->fw_dump) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700827 qla_printk(KERN_WARNING, ha, "Unable to allocate (%d KB) for "
Andrew Vasquezd4e3e042006-05-17 15:09:50 -0700828 "firmware dump!!!\n", dump_size / 1024);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700829
830 if (ha->eft) {
831 dma_free_coherent(&ha->pdev->dev, eft_size, ha->eft,
832 ha->eft_dma);
833 ha->eft = NULL;
834 ha->eft_dma = 0;
835 }
836 return;
837 }
838
839 qla_printk(KERN_INFO, ha, "Allocated (%d KB) for firmware dump...\n",
840 dump_size / 1024);
841
842 ha->fw_dump_len = dump_size;
843 ha->fw_dump->signature[0] = 'Q';
844 ha->fw_dump->signature[1] = 'L';
845 ha->fw_dump->signature[2] = 'G';
846 ha->fw_dump->signature[3] = 'C';
847 ha->fw_dump->version = __constant_htonl(1);
848
849 ha->fw_dump->fixed_size = htonl(fixed_size);
850 ha->fw_dump->mem_size = htonl(mem_size);
851 ha->fw_dump->req_q_size = htonl(req_q_size);
852 ha->fw_dump->rsp_q_size = htonl(rsp_q_size);
853
854 ha->fw_dump->eft_size = htonl(eft_size);
855 ha->fw_dump->eft_addr_l = htonl(LSD(ha->eft_dma));
856 ha->fw_dump->eft_addr_h = htonl(MSD(ha->eft_dma));
857
858 ha->fw_dump->header_size =
859 htonl(offsetof(struct qla2xxx_fw_dump, isp));
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700860}
861
862/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 * qla2x00_resize_request_q() - Resize request queue given available ISP memory.
864 * @ha: HA context
865 *
866 * Returns 0 on success.
867 */
868static void
869qla2x00_resize_request_q(scsi_qla_host_t *ha)
870{
871 int rval;
872 uint16_t fw_iocb_cnt = 0;
873 uint16_t request_q_length = REQUEST_ENTRY_CNT_2XXX_EXT_MEM;
874 dma_addr_t request_dma;
875 request_t *request_ring;
876
877 /* Valid only on recent ISPs. */
878 if (IS_QLA2100(ha) || IS_QLA2200(ha))
879 return;
880
881 /* Retrieve IOCB counts available to the firmware. */
Seokmann Ju4d0ea242007-09-20 14:07:43 -0700882 rval = qla2x00_get_resource_cnts(ha, NULL, NULL, NULL, &fw_iocb_cnt,
883 &ha->max_npiv_vports);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 if (rval)
885 return;
886 /* No point in continuing if current settings are sufficient. */
887 if (fw_iocb_cnt < 1024)
888 return;
889 if (ha->request_q_length >= request_q_length)
890 return;
891
892 /* Attempt to claim larger area for request queue. */
893 request_ring = dma_alloc_coherent(&ha->pdev->dev,
894 (request_q_length + 1) * sizeof(request_t), &request_dma,
895 GFP_KERNEL);
896 if (request_ring == NULL)
897 return;
898
899 /* Resize successful, report extensions. */
900 qla_printk(KERN_INFO, ha, "Extended memory detected (%d KB)...\n",
901 (ha->fw_memory_size + 1) / 1024);
902 qla_printk(KERN_INFO, ha, "Resizing request queue depth "
903 "(%d -> %d)...\n", ha->request_q_length, request_q_length);
904
905 /* Clear old allocations. */
906 dma_free_coherent(&ha->pdev->dev,
907 (ha->request_q_length + 1) * sizeof(request_t), ha->request_ring,
908 ha->request_dma);
909
910 /* Begin using larger queue. */
911 ha->request_q_length = request_q_length;
912 ha->request_ring = request_ring;
913 ha->request_dma = request_dma;
914}
915
916/**
917 * qla2x00_setup_chip() - Load and start RISC firmware.
918 * @ha: HA context
919 *
920 * Returns 0 on success.
921 */
922static int
923qla2x00_setup_chip(scsi_qla_host_t *ha)
924{
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700925 int rval;
926 uint32_t srisc_address = 0;
Andrew Vasquez3db06522008-01-31 12:33:49 -0800927 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
928 unsigned long flags;
929
930 if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
931 /* Disable SRAM, Instruction RAM and GP RAM parity. */
932 spin_lock_irqsave(&ha->hardware_lock, flags);
933 WRT_REG_WORD(&reg->hccr, (HCCR_ENABLE_PARITY + 0x0));
934 RD_REG_WORD(&reg->hccr);
935 spin_unlock_irqrestore(&ha->hardware_lock, flags);
936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
938 /* Load firmware sequences */
Andrew Vasquezfd34f552007-07-19 15:06:00 -0700939 rval = ha->isp_ops->load_risc(ha, &srisc_address);
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700940 if (rval == QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 DEBUG(printk("scsi(%ld): Verifying Checksum of loaded RISC "
942 "code.\n", ha->host_no));
943
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700944 rval = qla2x00_verify_checksum(ha, srisc_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 if (rval == QLA_SUCCESS) {
946 /* Start firmware execution. */
947 DEBUG(printk("scsi(%ld): Checksum OK, start "
948 "firmware.\n", ha->host_no));
949
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700950 rval = qla2x00_execute_fw(ha, srisc_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 /* Retrieve firmware information. */
952 if (rval == QLA_SUCCESS && ha->fw_major_version == 0) {
953 qla2x00_get_fw_version(ha,
954 &ha->fw_major_version,
955 &ha->fw_minor_version,
956 &ha->fw_subminor_version,
957 &ha->fw_attributes, &ha->fw_memory_size);
958 qla2x00_resize_request_q(ha);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700959 ha->flags.npiv_supported = 0;
Seokmann Ju4d0ea242007-09-20 14:07:43 -0700960 if ((IS_QLA24XX(ha) || IS_QLA25XX(ha)) &&
961 (ha->fw_attributes & BIT_2)) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700962 ha->flags.npiv_supported = 1;
Seokmann Ju4d0ea242007-09-20 14:07:43 -0700963 if ((!ha->max_npiv_vports) ||
964 ((ha->max_npiv_vports + 1) %
Andrew Vasquezeb66dc62007-11-12 10:30:58 -0800965 MIN_MULTI_ID_FABRIC))
Seokmann Ju4d0ea242007-09-20 14:07:43 -0700966 ha->max_npiv_vports =
Andrew Vasquezeb66dc62007-11-12 10:30:58 -0800967 MIN_MULTI_ID_FABRIC - 1;
Seokmann Ju4d0ea242007-09-20 14:07:43 -0700968 }
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700969
970 if (ql2xallocfwdump)
971 qla2x00_alloc_fw_dump(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 }
973 } else {
974 DEBUG2(printk(KERN_INFO
975 "scsi(%ld): ISP Firmware failed checksum.\n",
976 ha->host_no));
977 }
978 }
979
Andrew Vasquez3db06522008-01-31 12:33:49 -0800980 if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
981 /* Enable proper parity. */
982 spin_lock_irqsave(&ha->hardware_lock, flags);
983 if (IS_QLA2300(ha))
984 /* SRAM parity */
985 WRT_REG_WORD(&reg->hccr, HCCR_ENABLE_PARITY + 0x1);
986 else
987 /* SRAM, Instruction RAM and GP RAM parity */
988 WRT_REG_WORD(&reg->hccr, HCCR_ENABLE_PARITY + 0x7);
989 RD_REG_WORD(&reg->hccr);
990 spin_unlock_irqrestore(&ha->hardware_lock, flags);
991 }
992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 if (rval) {
994 DEBUG2_3(printk("scsi(%ld): Setup chip **** FAILED ****.\n",
995 ha->host_no));
996 }
997
998 return (rval);
999}
1000
1001/**
1002 * qla2x00_init_response_q_entries() - Initializes response queue entries.
1003 * @ha: HA context
1004 *
1005 * Beginning of request ring has initialization control block already built
1006 * by nvram config routine.
1007 *
1008 * Returns 0 on success.
1009 */
1010static void
1011qla2x00_init_response_q_entries(scsi_qla_host_t *ha)
1012{
1013 uint16_t cnt;
1014 response_t *pkt;
1015
1016 pkt = ha->response_ring_ptr;
1017 for (cnt = 0; cnt < ha->response_q_length; cnt++) {
1018 pkt->signature = RESPONSE_PROCESSED;
1019 pkt++;
1020 }
1021
1022}
1023
1024/**
1025 * qla2x00_update_fw_options() - Read and process firmware options.
1026 * @ha: HA context
1027 *
1028 * Returns 0 on success.
1029 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001030void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031qla2x00_update_fw_options(scsi_qla_host_t *ha)
1032{
1033 uint16_t swing, emphasis, tx_sens, rx_sens;
1034
1035 memset(ha->fw_options, 0, sizeof(ha->fw_options));
1036 qla2x00_get_fw_options(ha, ha->fw_options);
1037
1038 if (IS_QLA2100(ha) || IS_QLA2200(ha))
1039 return;
1040
1041 /* Serial Link options. */
1042 DEBUG3(printk("scsi(%ld): Serial link options:\n",
1043 ha->host_no));
1044 DEBUG3(qla2x00_dump_buffer((uint8_t *)&ha->fw_seriallink_options,
1045 sizeof(ha->fw_seriallink_options)));
1046
1047 ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
1048 if (ha->fw_seriallink_options[3] & BIT_2) {
1049 ha->fw_options[1] |= FO1_SET_EMPHASIS_SWING;
1050
1051 /* 1G settings */
1052 swing = ha->fw_seriallink_options[2] & (BIT_2 | BIT_1 | BIT_0);
1053 emphasis = (ha->fw_seriallink_options[2] &
1054 (BIT_4 | BIT_3)) >> 3;
1055 tx_sens = ha->fw_seriallink_options[0] &
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001056 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 rx_sens = (ha->fw_seriallink_options[0] &
1058 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
1059 ha->fw_options[10] = (emphasis << 14) | (swing << 8);
1060 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
1061 if (rx_sens == 0x0)
1062 rx_sens = 0x3;
1063 ha->fw_options[10] |= (tx_sens << 4) | rx_sens;
1064 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
1065 ha->fw_options[10] |= BIT_5 |
1066 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
1067 (tx_sens & (BIT_1 | BIT_0));
1068
1069 /* 2G settings */
1070 swing = (ha->fw_seriallink_options[2] &
1071 (BIT_7 | BIT_6 | BIT_5)) >> 5;
1072 emphasis = ha->fw_seriallink_options[3] & (BIT_1 | BIT_0);
1073 tx_sens = ha->fw_seriallink_options[1] &
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001074 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 rx_sens = (ha->fw_seriallink_options[1] &
1076 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
1077 ha->fw_options[11] = (emphasis << 14) | (swing << 8);
1078 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
1079 if (rx_sens == 0x0)
1080 rx_sens = 0x3;
1081 ha->fw_options[11] |= (tx_sens << 4) | rx_sens;
1082 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
1083 ha->fw_options[11] |= BIT_5 |
1084 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
1085 (tx_sens & (BIT_1 | BIT_0));
1086 }
1087
1088 /* FCP2 options. */
1089 /* Return command IOCBs without waiting for an ABTS to complete. */
1090 ha->fw_options[3] |= BIT_13;
1091
1092 /* LED scheme. */
1093 if (ha->flags.enable_led_scheme)
1094 ha->fw_options[2] |= BIT_12;
1095
andrew.vasquez@qlogic.com48c02fd2006-03-09 14:27:18 -08001096 /* Detect ISP6312. */
1097 if (IS_QLA6312(ha))
1098 ha->fw_options[2] |= BIT_13;
1099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 /* Update firmware options. */
1101 qla2x00_set_fw_options(ha, ha->fw_options);
1102}
1103
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001104void
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001105qla24xx_update_fw_options(scsi_qla_host_t *ha)
1106{
1107 int rval;
1108
1109 /* Update Serial Link options. */
andrew.vasquez@qlogic.comf94097e2006-01-13 17:05:32 -08001110 if ((le16_to_cpu(ha->fw_seriallink_options24[0]) & BIT_0) == 0)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001111 return;
1112
andrew.vasquez@qlogic.comf94097e2006-01-13 17:05:32 -08001113 rval = qla2x00_set_serdes_params(ha,
1114 le16_to_cpu(ha->fw_seriallink_options24[1]),
1115 le16_to_cpu(ha->fw_seriallink_options24[2]),
1116 le16_to_cpu(ha->fw_seriallink_options24[3]));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001117 if (rval != QLA_SUCCESS) {
1118 qla_printk(KERN_WARNING, ha,
1119 "Unable to update Serial Link options (%x).\n", rval);
1120 }
1121}
1122
1123void
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001124qla2x00_config_rings(struct scsi_qla_host *ha)
1125{
Andrew Vasquez3d716442005-07-06 10:30:26 -07001126 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001127
1128 /* Setup ring parameters in initialization control block. */
1129 ha->init_cb->request_q_outpointer = __constant_cpu_to_le16(0);
1130 ha->init_cb->response_q_inpointer = __constant_cpu_to_le16(0);
1131 ha->init_cb->request_q_length = cpu_to_le16(ha->request_q_length);
1132 ha->init_cb->response_q_length = cpu_to_le16(ha->response_q_length);
1133 ha->init_cb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
1134 ha->init_cb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
1135 ha->init_cb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
1136 ha->init_cb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
1137
1138 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), 0);
1139 WRT_REG_WORD(ISP_REQ_Q_OUT(ha, reg), 0);
1140 WRT_REG_WORD(ISP_RSP_Q_IN(ha, reg), 0);
1141 WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), 0);
1142 RD_REG_WORD(ISP_RSP_Q_OUT(ha, reg)); /* PCI Posting. */
1143}
1144
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001145void
1146qla24xx_config_rings(struct scsi_qla_host *ha)
1147{
1148 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1149 struct init_cb_24xx *icb;
1150
1151 /* Setup ring parameters in initialization control block. */
1152 icb = (struct init_cb_24xx *)ha->init_cb;
1153 icb->request_q_outpointer = __constant_cpu_to_le16(0);
1154 icb->response_q_inpointer = __constant_cpu_to_le16(0);
1155 icb->request_q_length = cpu_to_le16(ha->request_q_length);
1156 icb->response_q_length = cpu_to_le16(ha->response_q_length);
1157 icb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
1158 icb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
1159 icb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
1160 icb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
1161
1162 WRT_REG_DWORD(&reg->req_q_in, 0);
1163 WRT_REG_DWORD(&reg->req_q_out, 0);
1164 WRT_REG_DWORD(&reg->rsp_q_in, 0);
1165 WRT_REG_DWORD(&reg->rsp_q_out, 0);
1166 RD_REG_DWORD(&reg->rsp_q_out);
1167}
1168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169/**
1170 * qla2x00_init_rings() - Initializes firmware.
1171 * @ha: HA context
1172 *
1173 * Beginning of request ring has initialization control block already built
1174 * by nvram config routine.
1175 *
1176 * Returns 0 on success.
1177 */
1178static int
1179qla2x00_init_rings(scsi_qla_host_t *ha)
1180{
1181 int rval;
1182 unsigned long flags = 0;
1183 int cnt;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001184 struct mid_init_cb_24xx *mid_init_cb =
1185 (struct mid_init_cb_24xx *) ha->init_cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
1187 spin_lock_irqsave(&ha->hardware_lock, flags);
1188
1189 /* Clear outstanding commands array. */
1190 for (cnt = 0; cnt < MAX_OUTSTANDING_COMMANDS; cnt++)
1191 ha->outstanding_cmds[cnt] = NULL;
1192
1193 ha->current_outstanding_cmd = 0;
1194
1195 /* Clear RSCN queue. */
1196 ha->rscn_in_ptr = 0;
1197 ha->rscn_out_ptr = 0;
1198
1199 /* Initialize firmware. */
1200 ha->request_ring_ptr = ha->request_ring;
1201 ha->req_ring_index = 0;
1202 ha->req_q_cnt = ha->request_q_length;
1203 ha->response_ring_ptr = ha->response_ring;
1204 ha->rsp_ring_index = 0;
1205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 /* Initialize response queue entries */
1207 qla2x00_init_response_q_entries(ha);
1208
Andrew Vasquezfd34f552007-07-19 15:06:00 -07001209 ha->isp_ops->config_rings(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1212
1213 /* Update any ISP specific firmware options before initialization. */
Andrew Vasquezfd34f552007-07-19 15:06:00 -07001214 ha->isp_ops->update_fw_options(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 DEBUG(printk("scsi(%ld): Issue init firmware.\n", ha->host_no));
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001217
Seokmann Juc48339d2008-01-17 09:02:19 -08001218 if (ha->flags.npiv_supported)
1219 mid_init_cb->count = cpu_to_le16(ha->max_npiv_vports);
1220
Andrew Vasquezeb66dc62007-11-12 10:30:58 -08001221 mid_init_cb->options = __constant_cpu_to_le16(BIT_1);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001222
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001223 rval = qla2x00_init_firmware(ha, ha->init_cb_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 if (rval) {
1225 DEBUG2_3(printk("scsi(%ld): Init firmware **** FAILED ****.\n",
1226 ha->host_no));
1227 } else {
1228 DEBUG3(printk("scsi(%ld): Init firmware -- success.\n",
1229 ha->host_no));
1230 }
1231
1232 return (rval);
1233}
1234
1235/**
1236 * qla2x00_fw_ready() - Waits for firmware ready.
1237 * @ha: HA context
1238 *
1239 * Returns 0 on success.
1240 */
1241static int
1242qla2x00_fw_ready(scsi_qla_host_t *ha)
1243{
1244 int rval;
1245 unsigned long wtime, mtime;
1246 uint16_t min_wait; /* Minimum wait time if loop is down */
1247 uint16_t wait_time; /* Wait time if loop is coming ready */
1248 uint16_t fw_state;
1249
1250 rval = QLA_SUCCESS;
1251
1252 /* 20 seconds for loop down. */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001253 min_wait = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
1255 /*
1256 * Firmware should take at most one RATOV to login, plus 5 seconds for
1257 * our own processing.
1258 */
1259 if ((wait_time = (ha->retry_count*ha->login_timeout) + 5) < min_wait) {
1260 wait_time = min_wait;
1261 }
1262
1263 /* Min wait time if loop down */
1264 mtime = jiffies + (min_wait * HZ);
1265
1266 /* wait time before firmware ready */
1267 wtime = jiffies + (wait_time * HZ);
1268
1269 /* Wait for ISP to finish LIP */
1270 if (!ha->flags.init_done)
1271 qla_printk(KERN_INFO, ha, "Waiting for LIP to complete...\n");
1272
1273 DEBUG3(printk("scsi(%ld): Waiting for LIP to complete...\n",
1274 ha->host_no));
1275
1276 do {
1277 rval = qla2x00_get_firmware_state(ha, &fw_state);
1278 if (rval == QLA_SUCCESS) {
1279 if (fw_state < FSTATE_LOSS_OF_SYNC) {
1280 ha->device_flags &= ~DFLG_NO_CABLE;
1281 }
1282 if (fw_state == FSTATE_READY) {
1283 DEBUG(printk("scsi(%ld): F/W Ready - OK \n",
1284 ha->host_no));
1285
1286 qla2x00_get_retry_cnt(ha, &ha->retry_count,
1287 &ha->login_timeout, &ha->r_a_tov);
1288
1289 rval = QLA_SUCCESS;
1290 break;
1291 }
1292
1293 rval = QLA_FUNCTION_FAILED;
1294
1295 if (atomic_read(&ha->loop_down_timer) &&
Andrew Vasquez7d7abc72006-06-23 16:11:17 -07001296 fw_state != FSTATE_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 /* Loop down. Timeout on min_wait for states
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001298 * other than Wait for Login.
1299 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 if (time_after_eq(jiffies, mtime)) {
1301 qla_printk(KERN_INFO, ha,
1302 "Cable is unplugged...\n");
1303
1304 ha->device_flags |= DFLG_NO_CABLE;
1305 break;
1306 }
1307 }
1308 } else {
1309 /* Mailbox cmd failed. Timeout on min_wait. */
1310 if (time_after_eq(jiffies, mtime))
1311 break;
1312 }
1313
1314 if (time_after_eq(jiffies, wtime))
1315 break;
1316
1317 /* Delay for a while */
1318 msleep(500);
1319
1320 DEBUG3(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
1321 ha->host_no, fw_state, jiffies));
1322 } while (1);
1323
1324 DEBUG(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
1325 ha->host_no, fw_state, jiffies));
1326
1327 if (rval) {
1328 DEBUG2_3(printk("scsi(%ld): Firmware ready **** FAILED ****.\n",
1329 ha->host_no));
1330 }
1331
1332 return (rval);
1333}
1334
1335/*
1336* qla2x00_configure_hba
1337* Setup adapter context.
1338*
1339* Input:
1340* ha = adapter state pointer.
1341*
1342* Returns:
1343* 0 = success
1344*
1345* Context:
1346* Kernel context.
1347*/
1348static int
1349qla2x00_configure_hba(scsi_qla_host_t *ha)
1350{
1351 int rval;
1352 uint16_t loop_id;
1353 uint16_t topo;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001354 uint16_t sw_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 uint8_t al_pa;
1356 uint8_t area;
1357 uint8_t domain;
1358 char connect_type[22];
1359
1360 /* Get host addresses. */
1361 rval = qla2x00_get_adapter_id(ha,
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001362 &loop_id, &al_pa, &area, &domain, &topo, &sw_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 if (rval != QLA_SUCCESS) {
Andrew Vasquez23443b12005-12-06 10:57:06 -08001364 if (LOOP_TRANSITION(ha) || atomic_read(&ha->loop_down_timer) ||
Ravi Anand33135aa2005-11-08 14:37:20 -08001365 (rval == QLA_COMMAND_ERROR && loop_id == 0x7)) {
1366 DEBUG2(printk("%s(%ld) Loop is in a transition state\n",
1367 __func__, ha->host_no));
1368 } else {
1369 qla_printk(KERN_WARNING, ha,
1370 "ERROR -- Unable to get host loop ID.\n");
1371 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
1372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 return (rval);
1374 }
1375
1376 if (topo == 4) {
1377 qla_printk(KERN_INFO, ha,
1378 "Cannot get topology - retrying.\n");
1379 return (QLA_FUNCTION_FAILED);
1380 }
1381
1382 ha->loop_id = loop_id;
1383
1384 /* initialize */
1385 ha->min_external_loopid = SNS_FIRST_LOOP_ID;
1386 ha->operating_mode = LOOP;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001387 ha->switch_cap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
1389 switch (topo) {
1390 case 0:
1391 DEBUG3(printk("scsi(%ld): HBA in NL topology.\n",
1392 ha->host_no));
1393 ha->current_topology = ISP_CFG_NL;
1394 strcpy(connect_type, "(Loop)");
1395 break;
1396
1397 case 1:
1398 DEBUG3(printk("scsi(%ld): HBA in FL topology.\n",
1399 ha->host_no));
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001400 ha->switch_cap = sw_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 ha->current_topology = ISP_CFG_FL;
1402 strcpy(connect_type, "(FL_Port)");
1403 break;
1404
1405 case 2:
1406 DEBUG3(printk("scsi(%ld): HBA in N P2P topology.\n",
1407 ha->host_no));
1408 ha->operating_mode = P2P;
1409 ha->current_topology = ISP_CFG_N;
1410 strcpy(connect_type, "(N_Port-to-N_Port)");
1411 break;
1412
1413 case 3:
1414 DEBUG3(printk("scsi(%ld): HBA in F P2P topology.\n",
1415 ha->host_no));
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001416 ha->switch_cap = sw_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 ha->operating_mode = P2P;
1418 ha->current_topology = ISP_CFG_F;
1419 strcpy(connect_type, "(F_Port)");
1420 break;
1421
1422 default:
1423 DEBUG3(printk("scsi(%ld): HBA in unknown topology %x. "
1424 "Using NL.\n",
1425 ha->host_no, topo));
1426 ha->current_topology = ISP_CFG_NL;
1427 strcpy(connect_type, "(Loop)");
1428 break;
1429 }
1430
1431 /* Save Host port and loop ID. */
1432 /* byte order - Big Endian */
1433 ha->d_id.b.domain = domain;
1434 ha->d_id.b.area = area;
1435 ha->d_id.b.al_pa = al_pa;
1436
1437 if (!ha->flags.init_done)
1438 qla_printk(KERN_INFO, ha,
1439 "Topology - %s, Host Loop address 0x%x\n",
1440 connect_type, ha->loop_id);
1441
1442 if (rval) {
1443 DEBUG2_3(printk("scsi(%ld): FAILED.\n", ha->host_no));
1444 } else {
1445 DEBUG3(printk("scsi(%ld): exiting normally.\n", ha->host_no));
1446 }
1447
1448 return(rval);
1449}
1450
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08001451static inline void
1452qla2x00_set_model_info(scsi_qla_host_t *ha, uint8_t *model, size_t len, char *def)
1453{
1454 char *st, *en;
1455 uint16_t index;
1456
1457 if (memcmp(model, BINZERO, len) != 0) {
1458 strncpy(ha->model_number, model, len);
1459 st = en = ha->model_number;
1460 en += len - 1;
1461 while (en > st) {
1462 if (*en != 0x20 && *en != 0x00)
1463 break;
1464 *en-- = '\0';
1465 }
1466
1467 index = (ha->pdev->subsystem_device & 0xff);
1468 if (ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
1469 index < QLA_MODEL_NAMES)
1470 ha->model_desc = qla2x00_model_name[index * 2 + 1];
1471 } else {
1472 index = (ha->pdev->subsystem_device & 0xff);
1473 if (ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
1474 index < QLA_MODEL_NAMES) {
1475 strcpy(ha->model_number,
1476 qla2x00_model_name[index * 2]);
1477 ha->model_desc = qla2x00_model_name[index * 2 + 1];
1478 } else {
1479 strcpy(ha->model_number, def);
1480 }
1481 }
1482}
1483
David Miller4e08df32007-04-16 12:37:43 -07001484/* On sparc systems, obtain port and node WWN from firmware
1485 * properties.
1486 */
1487static void qla2xxx_nvram_wwn_from_ofw(scsi_qla_host_t *ha, nvram_t *nv)
1488{
1489#ifdef CONFIG_SPARC
1490 struct pci_dev *pdev = ha->pdev;
David S. Miller15576bc2007-05-08 00:36:49 -07001491 struct device_node *dp = pci_device_to_OF_node(pdev);
1492 const u8 *val;
David Miller4e08df32007-04-16 12:37:43 -07001493 int len;
1494
1495 val = of_get_property(dp, "port-wwn", &len);
1496 if (val && len >= WWN_SIZE)
1497 memcpy(nv->port_name, val, WWN_SIZE);
1498
1499 val = of_get_property(dp, "node-wwn", &len);
1500 if (val && len >= WWN_SIZE)
1501 memcpy(nv->node_name, val, WWN_SIZE);
1502#endif
1503}
1504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505/*
1506* NVRAM configuration for ISP 2xxx
1507*
1508* Input:
1509* ha = adapter block pointer.
1510*
1511* Output:
1512* initialization control block in response_ring
1513* host adapters parameters in host adapter block
1514*
1515* Returns:
1516* 0 = success.
1517*/
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001518int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519qla2x00_nvram_config(scsi_qla_host_t *ha)
1520{
David Miller4e08df32007-04-16 12:37:43 -07001521 int rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001522 uint8_t chksum = 0;
1523 uint16_t cnt;
1524 uint8_t *dptr1, *dptr2;
1525 init_cb_t *icb = ha->init_cb;
Seokmann Ju281afe12007-07-26 13:43:34 -07001526 nvram_t *nv = ha->nvram;
1527 uint8_t *ptr = ha->nvram;
Andrew Vasquez3d716442005-07-06 10:30:26 -07001528 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
David Miller4e08df32007-04-16 12:37:43 -07001530 rval = QLA_SUCCESS;
1531
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 /* Determine NVRAM starting address. */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001533 ha->nvram_size = sizeof(nvram_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 ha->nvram_base = 0;
1535 if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha))
1536 if ((RD_REG_WORD(&reg->ctrl_status) >> 14) == 1)
1537 ha->nvram_base = 0x80;
1538
1539 /* Get NVRAM data and calculate checksum. */
Andrew Vasquezfd34f552007-07-19 15:06:00 -07001540 ha->isp_ops->read_nvram(ha, ptr, ha->nvram_base, ha->nvram_size);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001541 for (cnt = 0, chksum = 0; cnt < ha->nvram_size; cnt++)
1542 chksum += *ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
1544 DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
Seokmann Ju281afe12007-07-26 13:43:34 -07001545 DEBUG5(qla2x00_dump_buffer((uint8_t *)nv, ha->nvram_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
1547 /* Bad NVRAM data, set defaults parameters. */
1548 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' ||
1549 nv->id[2] != 'P' || nv->id[3] != ' ' || nv->nvram_version < 1) {
1550 /* Reset NVRAM data. */
1551 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
1552 "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
1553 nv->nvram_version);
David Miller4e08df32007-04-16 12:37:43 -07001554 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
1555 "invalid -- WWPN) defaults.\n");
1556
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -07001557 if (chksum)
1558 qla2xxx_hw_event_log(ha, HW_EVENT_NVRAM_CHKSUM_ERR, 0,
1559 MSW(chksum), LSW(chksum));
1560
David Miller4e08df32007-04-16 12:37:43 -07001561 /*
1562 * Set default initialization control block.
1563 */
1564 memset(nv, 0, ha->nvram_size);
1565 nv->parameter_block_version = ICB_VERSION;
1566
1567 if (IS_QLA23XX(ha)) {
1568 nv->firmware_options[0] = BIT_2 | BIT_1;
1569 nv->firmware_options[1] = BIT_7 | BIT_5;
1570 nv->add_firmware_options[0] = BIT_5;
1571 nv->add_firmware_options[1] = BIT_5 | BIT_4;
1572 nv->frame_payload_size = __constant_cpu_to_le16(2048);
1573 nv->special_options[1] = BIT_7;
1574 } else if (IS_QLA2200(ha)) {
1575 nv->firmware_options[0] = BIT_2 | BIT_1;
1576 nv->firmware_options[1] = BIT_7 | BIT_5;
1577 nv->add_firmware_options[0] = BIT_5;
1578 nv->add_firmware_options[1] = BIT_5 | BIT_4;
1579 nv->frame_payload_size = __constant_cpu_to_le16(1024);
1580 } else if (IS_QLA2100(ha)) {
1581 nv->firmware_options[0] = BIT_3 | BIT_1;
1582 nv->firmware_options[1] = BIT_5;
1583 nv->frame_payload_size = __constant_cpu_to_le16(1024);
1584 }
1585
1586 nv->max_iocb_allocation = __constant_cpu_to_le16(256);
1587 nv->execution_throttle = __constant_cpu_to_le16(16);
1588 nv->retry_count = 8;
1589 nv->retry_delay = 1;
1590
1591 nv->port_name[0] = 33;
1592 nv->port_name[3] = 224;
1593 nv->port_name[4] = 139;
1594
1595 qla2xxx_nvram_wwn_from_ofw(ha, nv);
1596
1597 nv->login_timeout = 4;
1598
1599 /*
1600 * Set default host adapter parameters
1601 */
1602 nv->host_p[1] = BIT_2;
1603 nv->reset_delay = 5;
1604 nv->port_down_retry_count = 8;
1605 nv->max_luns_per_target = __constant_cpu_to_le16(8);
1606 nv->link_down_timeout = 60;
1607
1608 rval = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 }
1610
1611#if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
1612 /*
1613 * The SN2 does not provide BIOS emulation which means you can't change
1614 * potentially bogus BIOS settings. Force the use of default settings
1615 * for link rate and frame size. Hope that the rest of the settings
1616 * are valid.
1617 */
1618 if (ia64_platform_is("sn2")) {
1619 nv->frame_payload_size = __constant_cpu_to_le16(2048);
1620 if (IS_QLA23XX(ha))
1621 nv->special_options[1] = BIT_7;
1622 }
1623#endif
1624
1625 /* Reset Initialization control block */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001626 memset(icb, 0, ha->init_cb_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
1628 /*
1629 * Setup driver NVRAM options.
1630 */
1631 nv->firmware_options[0] |= (BIT_6 | BIT_1);
1632 nv->firmware_options[0] &= ~(BIT_5 | BIT_4);
1633 nv->firmware_options[1] |= (BIT_5 | BIT_0);
1634 nv->firmware_options[1] &= ~BIT_4;
1635
1636 if (IS_QLA23XX(ha)) {
1637 nv->firmware_options[0] |= BIT_2;
1638 nv->firmware_options[0] &= ~BIT_3;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001639 nv->add_firmware_options[1] |= BIT_5 | BIT_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
1641 if (IS_QLA2300(ha)) {
1642 if (ha->fb_rev == FPM_2310) {
1643 strcpy(ha->model_number, "QLA2310");
1644 } else {
1645 strcpy(ha->model_number, "QLA2300");
1646 }
1647 } else {
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08001648 qla2x00_set_model_info(ha, nv->model_number,
1649 sizeof(nv->model_number), "QLA23xx");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 }
1651 } else if (IS_QLA2200(ha)) {
1652 nv->firmware_options[0] |= BIT_2;
1653 /*
1654 * 'Point-to-point preferred, else loop' is not a safe
1655 * connection mode setting.
1656 */
1657 if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
1658 (BIT_5 | BIT_4)) {
1659 /* Force 'loop preferred, else point-to-point'. */
1660 nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
1661 nv->add_firmware_options[0] |= BIT_5;
1662 }
1663 strcpy(ha->model_number, "QLA22xx");
1664 } else /*if (IS_QLA2100(ha))*/ {
1665 strcpy(ha->model_number, "QLA2100");
1666 }
1667
1668 /*
1669 * Copy over NVRAM RISC parameter block to initialization control block.
1670 */
1671 dptr1 = (uint8_t *)icb;
1672 dptr2 = (uint8_t *)&nv->parameter_block_version;
1673 cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
1674 while (cnt--)
1675 *dptr1++ = *dptr2++;
1676
1677 /* Copy 2nd half. */
1678 dptr1 = (uint8_t *)icb->add_firmware_options;
1679 cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
1680 while (cnt--)
1681 *dptr1++ = *dptr2++;
1682
Andrew Vasquez5341e862006-05-17 15:09:16 -07001683 /* Use alternate WWN? */
1684 if (nv->host_p[1] & BIT_7) {
1685 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
1686 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
1687 }
1688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 /* Prepare nodename */
1690 if ((icb->firmware_options[1] & BIT_6) == 0) {
1691 /*
1692 * Firmware will apply the following mask if the nodename was
1693 * not provided.
1694 */
1695 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
1696 icb->node_name[0] &= 0xF0;
1697 }
1698
1699 /*
1700 * Set host adapter parameters.
1701 */
Andrew Vasquez01819442006-06-23 16:11:10 -07001702 if (nv->host_p[0] & BIT_7)
Andrew Vasquez11010fe2006-10-06 09:54:59 -07001703 ql2xextended_error_logging = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
1705 /* Always load RISC code on non ISP2[12]00 chips. */
1706 if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
1707 ha->flags.disable_risc_code_load = 0;
1708 ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
1709 ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
1710 ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
Andrew Vasquez06c22bd2005-08-26 19:09:00 -07001711 ha->flags.enable_led_scheme = (nv->special_options[1] & BIT_4) ? 1 : 0;
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07001712 ha->flags.disable_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
1714 ha->operating_mode =
1715 (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
1716
1717 memcpy(ha->fw_seriallink_options, nv->seriallink_options,
1718 sizeof(ha->fw_seriallink_options));
1719
1720 /* save HBA serial number */
1721 ha->serial0 = icb->port_name[5];
1722 ha->serial1 = icb->port_name[6];
1723 ha->serial2 = icb->port_name[7];
Andrew Vasquez3d716442005-07-06 10:30:26 -07001724 ha->node_name = icb->node_name;
1725 ha->port_name = icb->port_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
1727 icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
1728
1729 ha->retry_count = nv->retry_count;
1730
1731 /* Set minimum login_timeout to 4 seconds. */
1732 if (nv->login_timeout < ql2xlogintimeout)
1733 nv->login_timeout = ql2xlogintimeout;
1734 if (nv->login_timeout < 4)
1735 nv->login_timeout = 4;
1736 ha->login_timeout = nv->login_timeout;
1737 icb->login_timeout = nv->login_timeout;
1738
Andrew Vasquez00a537b2008-02-28 14:06:11 -08001739 /* Set minimum RATOV to 100 tenths of a second. */
1740 ha->r_a_tov = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 ha->loop_reset_delay = nv->reset_delay;
1743
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 /* Link Down Timeout = 0:
1745 *
1746 * When Port Down timer expires we will start returning
1747 * I/O's to OS with "DID_NO_CONNECT".
1748 *
1749 * Link Down Timeout != 0:
1750 *
1751 * The driver waits for the link to come up after link down
1752 * before returning I/Os to OS with "DID_NO_CONNECT".
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001753 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 if (nv->link_down_timeout == 0) {
1755 ha->loop_down_abort_time =
Andrew Vasquez 354d6b22005-04-23 02:47:27 -04001756 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 } else {
1758 ha->link_down_timeout = nv->link_down_timeout;
1759 ha->loop_down_abort_time =
1760 (LOOP_DOWN_TIME - ha->link_down_timeout);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 /*
1764 * Need enough time to try and get the port back.
1765 */
1766 ha->port_down_retry_count = nv->port_down_retry_count;
1767 if (qlport_down_retry)
1768 ha->port_down_retry_count = qlport_down_retry;
1769 /* Set login_retry_count */
1770 ha->login_retry_count = nv->retry_count;
1771 if (ha->port_down_retry_count == nv->port_down_retry_count &&
1772 ha->port_down_retry_count > 3)
1773 ha->login_retry_count = ha->port_down_retry_count;
1774 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
1775 ha->login_retry_count = ha->port_down_retry_count;
1776 if (ql2xloginretrycount)
1777 ha->login_retry_count = ql2xloginretrycount;
1778
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 icb->lun_enables = __constant_cpu_to_le16(0);
1780 icb->command_resource_count = 0;
1781 icb->immediate_notify_resource_count = 0;
1782 icb->timeout = __constant_cpu_to_le16(0);
1783
1784 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
1785 /* Enable RIO */
1786 icb->firmware_options[0] &= ~BIT_3;
1787 icb->add_firmware_options[0] &=
1788 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
1789 icb->add_firmware_options[0] |= BIT_2;
1790 icb->response_accumulation_timer = 3;
1791 icb->interrupt_delay_timer = 5;
1792
1793 ha->flags.process_response_queue = 1;
1794 } else {
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001795 /* Enable ZIO. */
1796 if (!ha->flags.init_done) {
1797 ha->zio_mode = icb->add_firmware_options[0] &
1798 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
1799 ha->zio_timer = icb->interrupt_delay_timer ?
1800 icb->interrupt_delay_timer: 2;
1801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 icb->add_firmware_options[0] &=
1803 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001804 ha->flags.process_response_queue = 0;
1805 if (ha->zio_mode != QLA_ZIO_DISABLED) {
andrew.vasquez@qlogic.com4a59f712006-03-09 14:27:39 -08001806 ha->zio_mode = QLA_ZIO_MODE_6;
1807
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001808 DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer "
1809 "delay (%d us).\n", ha->host_no, ha->zio_mode,
1810 ha->zio_timer * 100));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 qla_printk(KERN_INFO, ha,
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001812 "ZIO mode %d enabled; timer delay (%d us).\n",
1813 ha->zio_mode, ha->zio_timer * 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001815 icb->add_firmware_options[0] |= (uint8_t)ha->zio_mode;
1816 icb->interrupt_delay_timer = (uint8_t)ha->zio_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 ha->flags.process_response_queue = 1;
1818 }
1819 }
1820
David Miller4e08df32007-04-16 12:37:43 -07001821 if (rval) {
1822 DEBUG2_3(printk(KERN_WARNING
1823 "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
1824 }
1825 return (rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826}
1827
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001828static void
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001829qla2x00_rport_del(void *data)
1830{
1831 fc_port_t *fcport = data;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08001832 struct fc_rport *rport;
1833 unsigned long flags;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001834
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08001835 spin_lock_irqsave(&fcport->rport_lock, flags);
1836 rport = fcport->drport;
1837 fcport->drport = NULL;
1838 spin_unlock_irqrestore(&fcport->rport_lock, flags);
1839 if (rport)
1840 fc_remote_port_delete(rport);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001841}
1842
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843/**
1844 * qla2x00_alloc_fcport() - Allocate a generic fcport.
1845 * @ha: HA context
1846 * @flags: allocation flags
1847 *
1848 * Returns a pointer to the allocated fcport, or NULL, if none available.
1849 */
Adrian Bunk413975a2006-06-30 02:33:06 -07001850static fc_port_t *
Al Viroc53033f2005-10-21 03:22:08 -04001851qla2x00_alloc_fcport(scsi_qla_host_t *ha, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852{
1853 fc_port_t *fcport;
1854
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02001855 fcport = kzalloc(sizeof(fc_port_t), flags);
1856 if (!fcport)
1857 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
1859 /* Setup fcport template structure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 fcport->ha = ha;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001861 fcport->vp_idx = ha->vp_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 fcport->port_type = FCT_UNKNOWN;
1863 fcport->loop_id = FC_NO_LOOP_ID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 atomic_set(&fcport->state, FCS_UNCONFIGURED);
1865 fcport->flags = FCF_RLC_SUPPORT;
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07001866 fcport->supported_classes = FC_COS_UNSPECIFIED;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08001867 spin_lock_init(&fcport->rport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02001869 return fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870}
1871
1872/*
1873 * qla2x00_configure_loop
1874 * Updates Fibre Channel Device Database with what is actually on loop.
1875 *
1876 * Input:
1877 * ha = adapter block pointer.
1878 *
1879 * Returns:
1880 * 0 = success.
1881 * 1 = error.
1882 * 2 = database was full and device was not configured.
1883 */
1884static int
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001885qla2x00_configure_loop(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886{
1887 int rval;
1888 unsigned long flags, save_flags;
1889
1890 rval = QLA_SUCCESS;
1891
1892 /* Get Initiator ID */
1893 if (test_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags)) {
1894 rval = qla2x00_configure_hba(ha);
1895 if (rval != QLA_SUCCESS) {
1896 DEBUG(printk("scsi(%ld): Unable to configure HBA.\n",
1897 ha->host_no));
1898 return (rval);
1899 }
1900 }
1901
1902 save_flags = flags = ha->dpc_flags;
1903 DEBUG(printk("scsi(%ld): Configure loop -- dpc flags =0x%lx\n",
1904 ha->host_no, flags));
1905
1906 /*
1907 * If we have both an RSCN and PORT UPDATE pending then handle them
1908 * both at the same time.
1909 */
1910 clear_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1911 clear_bit(RSCN_UPDATE, &ha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
1913 /* Determine what we need to do */
1914 if (ha->current_topology == ISP_CFG_FL &&
1915 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1916
1917 ha->flags.rscn_queue_overflow = 1;
1918 set_bit(RSCN_UPDATE, &flags);
1919
1920 } else if (ha->current_topology == ISP_CFG_F &&
1921 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1922
1923 ha->flags.rscn_queue_overflow = 1;
1924 set_bit(RSCN_UPDATE, &flags);
1925 clear_bit(LOCAL_LOOP_UPDATE, &flags);
1926
Andrew Vasquez21333b42006-05-17 15:09:56 -07001927 } else if (ha->current_topology == ISP_CFG_N) {
1928 clear_bit(RSCN_UPDATE, &flags);
1929
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 } else if (!ha->flags.online ||
1931 (test_bit(ABORT_ISP_ACTIVE, &flags))) {
1932
1933 ha->flags.rscn_queue_overflow = 1;
1934 set_bit(RSCN_UPDATE, &flags);
1935 set_bit(LOCAL_LOOP_UPDATE, &flags);
1936 }
1937
1938 if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
1939 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1940 rval = QLA_FUNCTION_FAILED;
1941 } else {
1942 rval = qla2x00_configure_local_loop(ha);
1943 }
1944 }
1945
1946 if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
Andrew Vasquez23443b12005-12-06 10:57:06 -08001947 if (LOOP_TRANSITION(ha)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 rval = QLA_FUNCTION_FAILED;
1949 } else {
1950 rval = qla2x00_configure_fabric(ha);
1951 }
1952 }
1953
1954 if (rval == QLA_SUCCESS) {
1955 if (atomic_read(&ha->loop_down_timer) ||
1956 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1957 rval = QLA_FUNCTION_FAILED;
1958 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 atomic_set(&ha->loop_state, LOOP_READY);
1960
1961 DEBUG(printk("scsi(%ld): LOOP READY\n", ha->host_no));
1962 }
1963 }
1964
1965 if (rval) {
1966 DEBUG2_3(printk("%s(%ld): *** FAILED ***\n",
1967 __func__, ha->host_no));
1968 } else {
1969 DEBUG3(printk("%s: exiting normally\n", __func__));
1970 }
1971
1972 /* Restore state if a resync event occured during processing */
1973 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1974 if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
1975 set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1976 if (test_bit(RSCN_UPDATE, &save_flags))
1977 set_bit(RSCN_UPDATE, &ha->dpc_flags);
1978 }
1979
1980 return (rval);
1981}
1982
1983
1984
1985/*
1986 * qla2x00_configure_local_loop
1987 * Updates Fibre Channel Device Database with local loop devices.
1988 *
1989 * Input:
1990 * ha = adapter block pointer.
1991 *
1992 * Returns:
1993 * 0 = success.
1994 */
1995static int
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001996qla2x00_configure_local_loop(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
1998 int rval, rval2;
1999 int found_devs;
2000 int found;
2001 fc_port_t *fcport, *new_fcport;
2002
2003 uint16_t index;
2004 uint16_t entries;
2005 char *id_iter;
2006 uint16_t loop_id;
2007 uint8_t domain, area, al_pa;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002008 scsi_qla_host_t *pha = to_qla_parent(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
2010 found_devs = 0;
2011 new_fcport = NULL;
2012 entries = MAX_FIBRE_DEVICES;
2013
2014 DEBUG3(printk("scsi(%ld): Getting FCAL position map\n", ha->host_no));
2015 DEBUG3(qla2x00_get_fcal_position_map(ha, NULL));
2016
2017 /* Get list of logged in devices. */
2018 memset(ha->gid_list, 0, GID_LIST_SIZE);
2019 rval = qla2x00_get_id_list(ha, ha->gid_list, ha->gid_list_dma,
2020 &entries);
2021 if (rval != QLA_SUCCESS)
2022 goto cleanup_allocation;
2023
2024 DEBUG3(printk("scsi(%ld): Entries in ID list (%d)\n",
2025 ha->host_no, entries));
2026 DEBUG3(qla2x00_dump_buffer((uint8_t *)ha->gid_list,
2027 entries * sizeof(struct gid_list_info)));
2028
2029 /* Allocate temporary fcport for any new fcports discovered. */
2030 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2031 if (new_fcport == NULL) {
2032 rval = QLA_MEMORY_ALLOC_FAILED;
2033 goto cleanup_allocation;
2034 }
2035 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
2036
2037 /*
2038 * Mark local devices that were present with FCF_DEVICE_LOST for now.
2039 */
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002040 list_for_each_entry(fcport, &pha->fcports, list) {
2041 if (fcport->vp_idx != ha->vp_idx)
2042 continue;
2043
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 if (atomic_read(&fcport->state) == FCS_ONLINE &&
2045 fcport->port_type != FCT_BROADCAST &&
2046 (fcport->flags & FCF_FABRIC_DEVICE) == 0) {
2047
2048 DEBUG(printk("scsi(%ld): Marking port lost, "
2049 "loop_id=0x%04x\n",
2050 ha->host_no, fcport->loop_id));
2051
2052 atomic_set(&fcport->state, FCS_DEVICE_LOST);
2053 fcport->flags &= ~FCF_FARP_DONE;
2054 }
2055 }
2056
2057 /* Add devices to port list. */
2058 id_iter = (char *)ha->gid_list;
2059 for (index = 0; index < entries; index++) {
2060 domain = ((struct gid_list_info *)id_iter)->domain;
2061 area = ((struct gid_list_info *)id_iter)->area;
2062 al_pa = ((struct gid_list_info *)id_iter)->al_pa;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002063 if (IS_QLA2100(ha) || IS_QLA2200(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 loop_id = (uint16_t)
2065 ((struct gid_list_info *)id_iter)->loop_id_2100;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002066 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 loop_id = le16_to_cpu(
2068 ((struct gid_list_info *)id_iter)->loop_id);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002069 id_iter += ha->gid_list_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
2071 /* Bypass reserved domain fields. */
2072 if ((domain & 0xf0) == 0xf0)
2073 continue;
2074
2075 /* Bypass if not same domain and area of adapter. */
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07002076 if (area && domain &&
2077 (area != ha->d_id.b.area || domain != ha->d_id.b.domain))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 continue;
2079
2080 /* Bypass invalid local loop ID. */
2081 if (loop_id > LAST_LOCAL_LOOP_ID)
2082 continue;
2083
2084 /* Fill in member data. */
2085 new_fcport->d_id.b.domain = domain;
2086 new_fcport->d_id.b.area = area;
2087 new_fcport->d_id.b.al_pa = al_pa;
2088 new_fcport->loop_id = loop_id;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002089 new_fcport->vp_idx = ha->vp_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 rval2 = qla2x00_get_port_database(ha, new_fcport, 0);
2091 if (rval2 != QLA_SUCCESS) {
2092 DEBUG2(printk("scsi(%ld): Failed to retrieve fcport "
2093 "information -- get_port_database=%x, "
2094 "loop_id=0x%04x\n",
2095 ha->host_no, rval2, new_fcport->loop_id));
andrew.vasquez@qlogic.comc9d02ac2006-01-13 17:05:26 -08002096 DEBUG2(printk("scsi(%ld): Scheduling resync...\n",
2097 ha->host_no));
2098 set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 continue;
2100 }
2101
2102 /* Check for matching device in port list. */
2103 found = 0;
2104 fcport = NULL;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002105 list_for_each_entry(fcport, &pha->fcports, list) {
2106 if (fcport->vp_idx != ha->vp_idx)
2107 continue;
2108
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 if (memcmp(new_fcport->port_name, fcport->port_name,
2110 WWN_SIZE))
2111 continue;
2112
2113 fcport->flags &= ~(FCF_FABRIC_DEVICE |
2114 FCF_PERSISTENT_BOUND);
2115 fcport->loop_id = new_fcport->loop_id;
2116 fcport->port_type = new_fcport->port_type;
2117 fcport->d_id.b24 = new_fcport->d_id.b24;
2118 memcpy(fcport->node_name, new_fcport->node_name,
2119 WWN_SIZE);
2120
2121 found++;
2122 break;
2123 }
2124
2125 if (!found) {
2126 /* New device, add to fcports list. */
2127 new_fcport->flags &= ~FCF_PERSISTENT_BOUND;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002128 if (ha->parent) {
2129 new_fcport->ha = ha;
2130 new_fcport->vp_idx = ha->vp_idx;
2131 list_add_tail(&new_fcport->vp_fcport,
2132 &ha->vp_fcports);
2133 }
2134 list_add_tail(&new_fcport->list, &pha->fcports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
2136 /* Allocate a new replacement fcport. */
2137 fcport = new_fcport;
2138 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2139 if (new_fcport == NULL) {
2140 rval = QLA_MEMORY_ALLOC_FAILED;
2141 goto cleanup_allocation;
2142 }
2143 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
2144 }
2145
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002146 /* Base iIDMA settings on HBA port speed. */
Andrew Vasqueza3cbdfa2007-08-13 10:13:18 -07002147 fcport->fp_speed = ha->link_data_rate;
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002148
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 qla2x00_update_fcport(ha, fcport);
2150
2151 found_devs++;
2152 }
2153
2154cleanup_allocation:
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002155 kfree(new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
2157 if (rval != QLA_SUCCESS) {
2158 DEBUG2(printk("scsi(%ld): Configure local loop error exit: "
2159 "rval=%x\n", ha->host_no, rval));
2160 }
2161
2162 if (found_devs) {
2163 ha->device_flags |= DFLG_LOCAL_DEVICES;
2164 ha->device_flags &= ~DFLG_RETRY_LOCAL_DEVICES;
2165 }
2166
2167 return (rval);
2168}
2169
2170static void
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002171qla2x00_iidma_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
2172{
2173#define LS_UNKNOWN 2
Andrew Vasqueza3cbdfa2007-08-13 10:13:18 -07002174 static char *link_speeds[5] = { "1", "2", "?", "4", "8" };
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002175 int rval;
Andrew Vasqueza3cbdfa2007-08-13 10:13:18 -07002176 uint16_t mb[6];
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002177
Andrew Vasquezc76f2c02007-07-19 15:05:57 -07002178 if (!IS_IIDMA_CAPABLE(ha))
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002179 return;
2180
Andrew Vasquez39bd9622007-09-20 14:07:34 -07002181 if (fcport->fp_speed == PORT_SPEED_UNKNOWN ||
2182 fcport->fp_speed > ha->link_data_rate)
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002183 return;
2184
Andrew Vasqueza3cbdfa2007-08-13 10:13:18 -07002185 rval = qla2x00_set_idma_speed(ha, fcport->loop_id, fcport->fp_speed,
2186 mb);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002187 if (rval != QLA_SUCCESS) {
2188 DEBUG2(printk("scsi(%ld): Unable to adjust iIDMA "
2189 "%02x%02x%02x%02x%02x%02x%02x%02x -- %04x %x %04x %04x.\n",
2190 ha->host_no, fcport->port_name[0], fcport->port_name[1],
2191 fcport->port_name[2], fcport->port_name[3],
2192 fcport->port_name[4], fcport->port_name[5],
2193 fcport->port_name[6], fcport->port_name[7], rval,
Andrew Vasqueza3cbdfa2007-08-13 10:13:18 -07002194 fcport->fp_speed, mb[0], mb[1]));
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002195 } else {
2196 DEBUG2(qla_printk(KERN_INFO, ha,
2197 "iIDMA adjusted to %s GB/s on "
2198 "%02x%02x%02x%02x%02x%02x%02x%02x.\n",
Andrew Vasqueza3cbdfa2007-08-13 10:13:18 -07002199 link_speeds[fcport->fp_speed], fcport->port_name[0],
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002200 fcport->port_name[1], fcport->port_name[2],
2201 fcport->port_name[3], fcport->port_name[4],
2202 fcport->port_name[5], fcport->port_name[6],
2203 fcport->port_name[7]));
2204 }
2205}
2206
Adrian Bunk23be3312006-11-24 02:46:01 +01002207static void
8482e1182005-04-17 15:04:54 -05002208qla2x00_reg_remote_port(scsi_qla_host_t *ha, fc_port_t *fcport)
2209{
2210 struct fc_rport_identifiers rport_ids;
bdf79622005-04-17 15:06:53 -05002211 struct fc_rport *rport;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002212 unsigned long flags;
8482e1182005-04-17 15:04:54 -05002213
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002214 if (fcport->drport)
2215 qla2x00_rport_del(fcport);
2216 if (fcport->rport)
2217 return;
8482e1182005-04-17 15:04:54 -05002218
Andrew Vasquezf8b02a82005-08-31 15:21:20 -07002219 rport_ids.node_name = wwn_to_u64(fcport->node_name);
2220 rport_ids.port_name = wwn_to_u64(fcport->port_name);
8482e1182005-04-17 15:04:54 -05002221 rport_ids.port_id = fcport->d_id.b.domain << 16 |
2222 fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
2223 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002224 rport = fc_remote_port_add(ha->host, 0, &rport_ids);
Andrew Vasquez77d74142005-07-08 18:00:36 -07002225 if (!rport) {
2226 qla_printk(KERN_WARNING, ha,
2227 "Unable to allocate fc remote port!\n");
2228 return;
2229 }
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002230 spin_lock_irqsave(&fcport->rport_lock, flags);
2231 fcport->rport = rport;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002232 *((fc_port_t **)rport->dd_data) = fcport;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002233 spin_unlock_irqrestore(&fcport->rport_lock, flags);
2234
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07002235 rport->supported_classes = fcport->supported_classes;
Andrew Vasquez77d74142005-07-08 18:00:36 -07002236
2237 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
8482e1182005-04-17 15:04:54 -05002238 if (fcport->port_type == FCT_INITIATOR)
2239 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2240 if (fcport->port_type == FCT_TARGET)
2241 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
Andrew Vasquez77d74142005-07-08 18:00:36 -07002242 fc_remote_port_rolechg(rport, rport_ids.roles);
8482e1182005-04-17 15:04:54 -05002243}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
2245/*
Adrian Bunk23be3312006-11-24 02:46:01 +01002246 * qla2x00_update_fcport
2247 * Updates device on list.
2248 *
2249 * Input:
2250 * ha = adapter block pointer.
2251 * fcport = port structure pointer.
2252 *
2253 * Return:
2254 * 0 - Success
2255 * BIT_0 - error
2256 *
2257 * Context:
2258 * Kernel context.
2259 */
2260void
2261qla2x00_update_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
2262{
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002263 scsi_qla_host_t *pha = to_qla_parent(ha);
2264
Adrian Bunk23be3312006-11-24 02:46:01 +01002265 fcport->ha = ha;
2266 fcport->login_retry = 0;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002267 fcport->port_login_retry_count = pha->port_down_retry_count *
Adrian Bunk23be3312006-11-24 02:46:01 +01002268 PORT_RETRY_TIME;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002269 atomic_set(&fcport->port_down_timer, pha->port_down_retry_count *
Adrian Bunk23be3312006-11-24 02:46:01 +01002270 PORT_RETRY_TIME);
2271 fcport->flags &= ~FCF_LOGIN_NEEDED;
2272
2273 qla2x00_iidma_fcport(ha, fcport);
2274
2275 atomic_set(&fcport->state, FCS_ONLINE);
2276
2277 qla2x00_reg_remote_port(ha, fcport);
2278}
2279
2280/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 * qla2x00_configure_fabric
2282 * Setup SNS devices with loop ID's.
2283 *
2284 * Input:
2285 * ha = adapter block pointer.
2286 *
2287 * Returns:
2288 * 0 = success.
2289 * BIT_0 = error
2290 */
2291static int
2292qla2x00_configure_fabric(scsi_qla_host_t *ha)
2293{
2294 int rval, rval2;
2295 fc_port_t *fcport, *fcptemp;
2296 uint16_t next_loopid;
2297 uint16_t mb[MAILBOX_REGISTER_COUNT];
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002298 uint16_t loop_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 LIST_HEAD(new_fcports);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002300 scsi_qla_host_t *pha = to_qla_parent(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
2302 /* If FL port exists, then SNS is present */
Andrew Vasqueze4289242007-07-19 15:05:56 -07002303 if (IS_FWI2_CAPABLE(ha))
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002304 loop_id = NPH_F_PORT;
2305 else
2306 loop_id = SNS_FL_PORT;
Andrew Vasquez90991c82006-10-02 12:00:46 -07002307 rval = qla2x00_get_port_name(ha, loop_id, ha->fabric_node_name, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 if (rval != QLA_SUCCESS) {
2309 DEBUG2(printk("scsi(%ld): MBC_GET_PORT_NAME Failed, No FL "
2310 "Port\n", ha->host_no));
2311
2312 ha->device_flags &= ~SWITCH_FOUND;
2313 return (QLA_SUCCESS);
2314 }
Andrew Vasquez90991c82006-10-02 12:00:46 -07002315 ha->device_flags |= SWITCH_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
2317 /* Mark devices that need re-synchronization. */
2318 rval2 = qla2x00_device_resync(ha);
2319 if (rval2 == QLA_RSCNS_HANDLED) {
2320 /* No point doing the scan, just continue. */
2321 return (QLA_SUCCESS);
2322 }
2323 do {
Andrew Vasquezcca53352005-08-26 19:08:30 -07002324 /* FDMI support. */
2325 if (ql2xfdmienable &&
2326 test_and_clear_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags))
2327 qla2x00_fdmi_register(ha);
2328
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 /* Ensure we are logged into the SNS. */
Andrew Vasqueze4289242007-07-19 15:05:56 -07002330 if (IS_FWI2_CAPABLE(ha))
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002331 loop_id = NPH_SNS;
2332 else
2333 loop_id = SIMPLE_NAME_SERVER;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07002334 ha->isp_ops->fabric_login(ha, loop_id, 0xff, 0xff,
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002335 0xfc, mb, BIT_1 | BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 if (mb[0] != MBS_COMMAND_COMPLETE) {
2337 DEBUG2(qla_printk(KERN_INFO, ha,
2338 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002339 "mb[2]=%x mb[6]=%x mb[7]=%x\n", loop_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 mb[0], mb[1], mb[2], mb[6], mb[7]));
2341 return (QLA_SUCCESS);
2342 }
2343
2344 if (test_and_clear_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags)) {
2345 if (qla2x00_rft_id(ha)) {
2346 /* EMPTY */
2347 DEBUG2(printk("scsi(%ld): Register FC-4 "
2348 "TYPE failed.\n", ha->host_no));
2349 }
2350 if (qla2x00_rff_id(ha)) {
2351 /* EMPTY */
2352 DEBUG2(printk("scsi(%ld): Register FC-4 "
2353 "Features failed.\n", ha->host_no));
2354 }
2355 if (qla2x00_rnn_id(ha)) {
2356 /* EMPTY */
2357 DEBUG2(printk("scsi(%ld): Register Node Name "
2358 "failed.\n", ha->host_no));
2359 } else if (qla2x00_rsnn_nn(ha)) {
2360 /* EMPTY */
2361 DEBUG2(printk("scsi(%ld): Register Symbolic "
2362 "Node Name failed.\n", ha->host_no));
2363 }
2364 }
2365
2366 rval = qla2x00_find_all_fabric_devs(ha, &new_fcports);
2367 if (rval != QLA_SUCCESS)
2368 break;
2369
2370 /*
2371 * Logout all previous fabric devices marked lost, except
2372 * tape devices.
2373 */
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002374 list_for_each_entry(fcport, &pha->fcports, list) {
2375 if (fcport->vp_idx !=ha->vp_idx)
2376 continue;
2377
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2379 break;
2380
2381 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0)
2382 continue;
2383
2384 if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
2385 qla2x00_mark_device_lost(ha, fcport,
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002386 ql2xplogiabsentdevice, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 if (fcport->loop_id != FC_NO_LOOP_ID &&
2388 (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2389 fcport->port_type != FCT_INITIATOR &&
2390 fcport->port_type != FCT_BROADCAST) {
Andrew Vasquezfd34f552007-07-19 15:06:00 -07002391 ha->isp_ops->fabric_logout(ha,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07002392 fcport->loop_id,
2393 fcport->d_id.b.domain,
2394 fcport->d_id.b.area,
2395 fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 fcport->loop_id = FC_NO_LOOP_ID;
2397 }
2398 }
2399 }
2400
2401 /* Starting free loop ID. */
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002402 next_loopid = pha->min_external_loopid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
2404 /*
2405 * Scan through our port list and login entries that need to be
2406 * logged in.
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 (atomic_read(&ha->loop_down_timer) ||
2413 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2414 break;
2415
2416 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2417 (fcport->flags & FCF_LOGIN_NEEDED) == 0)
2418 continue;
2419
2420 if (fcport->loop_id == FC_NO_LOOP_ID) {
2421 fcport->loop_id = next_loopid;
2422 rval = qla2x00_find_new_loop_id(ha, fcport);
2423 if (rval != QLA_SUCCESS) {
2424 /* Ran out of IDs to use */
2425 break;
2426 }
2427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 /* Login and update database */
2429 qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
2430 }
2431
2432 /* Exit if out of loop IDs. */
2433 if (rval != QLA_SUCCESS) {
2434 break;
2435 }
2436
2437 /*
2438 * Login and add the new devices to our port list.
2439 */
2440 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2441 if (atomic_read(&ha->loop_down_timer) ||
2442 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2443 break;
2444
2445 /* Find a new loop ID to use. */
2446 fcport->loop_id = next_loopid;
2447 rval = qla2x00_find_new_loop_id(ha, fcport);
2448 if (rval != QLA_SUCCESS) {
2449 /* Ran out of IDs to use */
2450 break;
2451 }
2452
bdf79622005-04-17 15:06:53 -05002453 /* Login and update database */
2454 qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002455
2456 if (ha->parent) {
2457 fcport->ha = ha;
2458 fcport->vp_idx = ha->vp_idx;
2459 list_add_tail(&fcport->vp_fcport,
2460 &ha->vp_fcports);
2461 list_move_tail(&fcport->list,
2462 &ha->parent->fcports);
2463 } else
2464 list_move_tail(&fcport->list, &ha->fcports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 }
2466 } while (0);
2467
2468 /* Free all new device structures not processed. */
2469 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2470 list_del(&fcport->list);
2471 kfree(fcport);
2472 }
2473
2474 if (rval) {
2475 DEBUG2(printk("scsi(%ld): Configure fabric error exit: "
2476 "rval=%d\n", ha->host_no, rval));
2477 }
2478
2479 return (rval);
2480}
2481
2482
2483/*
2484 * qla2x00_find_all_fabric_devs
2485 *
2486 * Input:
2487 * ha = adapter block pointer.
2488 * dev = database device entry pointer.
2489 *
2490 * Returns:
2491 * 0 = success.
2492 *
2493 * Context:
2494 * Kernel context.
2495 */
2496static int
2497qla2x00_find_all_fabric_devs(scsi_qla_host_t *ha, struct list_head *new_fcports)
2498{
2499 int rval;
2500 uint16_t loop_id;
2501 fc_port_t *fcport, *new_fcport, *fcptemp;
2502 int found;
2503
2504 sw_info_t *swl;
2505 int swl_idx;
2506 int first_dev, last_dev;
2507 port_id_t wrap, nxt_d_id;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002508 int vp_index;
2509 int empty_vp_index;
2510 int found_vp;
2511 scsi_qla_host_t *vha;
2512 scsi_qla_host_t *pha = to_qla_parent(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
2514 rval = QLA_SUCCESS;
2515
2516 /* Try GID_PT to get device list, else GAN. */
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02002517 swl = kcalloc(MAX_FIBRE_DEVICES, sizeof(sw_info_t), GFP_ATOMIC);
2518 if (!swl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 /*EMPTY*/
2520 DEBUG2(printk("scsi(%ld): GID_PT allocations failed, fallback "
2521 "on GA_NXT\n", ha->host_no));
2522 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 if (qla2x00_gid_pt(ha, swl) != QLA_SUCCESS) {
2524 kfree(swl);
2525 swl = NULL;
2526 } else if (qla2x00_gpn_id(ha, swl) != QLA_SUCCESS) {
2527 kfree(swl);
2528 swl = NULL;
2529 } else if (qla2x00_gnn_id(ha, swl) != QLA_SUCCESS) {
2530 kfree(swl);
2531 swl = NULL;
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002532 } else if (qla2x00_gfpn_id(ha, swl) == QLA_SUCCESS) {
2533 qla2x00_gpsc(ha, swl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 }
2535 }
2536 swl_idx = 0;
2537
2538 /* Allocate temporary fcport for any new fcports discovered. */
2539 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2540 if (new_fcport == NULL) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002541 kfree(swl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 return (QLA_MEMORY_ALLOC_FAILED);
2543 }
2544 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002545 new_fcport->vp_idx = ha->vp_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 /* Set start port ID scan at adapter ID. */
2547 first_dev = 1;
2548 last_dev = 0;
2549
2550 /* Starting free loop ID. */
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002551 loop_id = pha->min_external_loopid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 for (; loop_id <= ha->last_loop_id; loop_id++) {
Andrew Vasquez3d716442005-07-06 10:30:26 -07002553 if (qla2x00_is_reserved_id(ha, loop_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 continue;
2555
Andrew Vasquez23443b12005-12-06 10:57:06 -08002556 if (atomic_read(&ha->loop_down_timer) || LOOP_TRANSITION(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 break;
2558
2559 if (swl != NULL) {
2560 if (last_dev) {
2561 wrap.b24 = new_fcport->d_id.b24;
2562 } else {
2563 new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
2564 memcpy(new_fcport->node_name,
2565 swl[swl_idx].node_name, WWN_SIZE);
2566 memcpy(new_fcport->port_name,
2567 swl[swl_idx].port_name, WWN_SIZE);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002568 memcpy(new_fcport->fabric_port_name,
2569 swl[swl_idx].fabric_port_name, WWN_SIZE);
2570 new_fcport->fp_speed = swl[swl_idx].fp_speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
2572 if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
2573 last_dev = 1;
2574 }
2575 swl_idx++;
2576 }
2577 } else {
2578 /* Send GA_NXT to the switch */
2579 rval = qla2x00_ga_nxt(ha, new_fcport);
2580 if (rval != QLA_SUCCESS) {
2581 qla_printk(KERN_WARNING, ha,
2582 "SNS scan failed -- assuming zero-entry "
2583 "result...\n");
2584 list_for_each_entry_safe(fcport, fcptemp,
2585 new_fcports, list) {
2586 list_del(&fcport->list);
2587 kfree(fcport);
2588 }
2589 rval = QLA_SUCCESS;
2590 break;
2591 }
2592 }
2593
2594 /* If wrap on switch device list, exit. */
2595 if (first_dev) {
2596 wrap.b24 = new_fcport->d_id.b24;
2597 first_dev = 0;
2598 } else if (new_fcport->d_id.b24 == wrap.b24) {
2599 DEBUG2(printk("scsi(%ld): device wrap (%02x%02x%02x)\n",
2600 ha->host_no, new_fcport->d_id.b.domain,
2601 new_fcport->d_id.b.area, new_fcport->d_id.b.al_pa));
2602 break;
2603 }
2604
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002605 /* Bypass if same physical adapter. */
2606 if (new_fcport->d_id.b24 == pha->d_id.b24)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 continue;
2608
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002609 /* Bypass virtual ports of the same host. */
2610 if (pha->num_vhosts) {
Andrew Vasquezeb66dc62007-11-12 10:30:58 -08002611 for_each_mapped_vp_idx(pha, vp_index) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002612 empty_vp_index = 1;
2613 found_vp = 0;
2614 list_for_each_entry(vha, &pha->vp_list,
2615 vp_list) {
2616 if (vp_index == vha->vp_idx) {
2617 empty_vp_index = 0;
2618 found_vp = 1;
2619 break;
2620 }
2621 }
2622
2623 if (empty_vp_index)
2624 continue;
2625
2626 if (found_vp &&
2627 new_fcport->d_id.b24 == vha->d_id.b24)
2628 break;
2629 }
Andrew Vasquezeb66dc62007-11-12 10:30:58 -08002630
2631 if (vp_index <= pha->max_npiv_vports)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002632 continue;
2633 }
2634
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07002635 /* Bypass if same domain and area of adapter. */
2636 if (((new_fcport->d_id.b24 & 0xffff00) ==
2637 (ha->d_id.b24 & 0xffff00)) && ha->current_topology ==
2638 ISP_CFG_FL)
2639 continue;
2640
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 /* Bypass reserved domain fields. */
2642 if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
2643 continue;
2644
2645 /* Locate matching device in database. */
2646 found = 0;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002647 list_for_each_entry(fcport, &pha->fcports, list) {
2648 if (new_fcport->vp_idx != fcport->vp_idx)
2649 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 if (memcmp(new_fcport->port_name, fcport->port_name,
2651 WWN_SIZE))
2652 continue;
2653
2654 found++;
2655
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002656 /* Update port state. */
2657 memcpy(fcport->fabric_port_name,
2658 new_fcport->fabric_port_name, WWN_SIZE);
2659 fcport->fp_speed = new_fcport->fp_speed;
2660
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 /*
2662 * If address the same and state FCS_ONLINE, nothing
2663 * changed.
2664 */
2665 if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
2666 atomic_read(&fcport->state) == FCS_ONLINE) {
2667 break;
2668 }
2669
2670 /*
2671 * If device was not a fabric device before.
2672 */
2673 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
2674 fcport->d_id.b24 = new_fcport->d_id.b24;
2675 fcport->loop_id = FC_NO_LOOP_ID;
2676 fcport->flags |= (FCF_FABRIC_DEVICE |
2677 FCF_LOGIN_NEEDED);
2678 fcport->flags &= ~FCF_PERSISTENT_BOUND;
2679 break;
2680 }
2681
2682 /*
2683 * Port ID changed or device was marked to be updated;
2684 * Log it out if still logged in and mark it for
2685 * relogin later.
2686 */
2687 fcport->d_id.b24 = new_fcport->d_id.b24;
2688 fcport->flags |= FCF_LOGIN_NEEDED;
2689 if (fcport->loop_id != FC_NO_LOOP_ID &&
2690 (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2691 fcport->port_type != FCT_INITIATOR &&
2692 fcport->port_type != FCT_BROADCAST) {
Andrew Vasquezfd34f552007-07-19 15:06:00 -07002693 ha->isp_ops->fabric_logout(ha, fcport->loop_id,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07002694 fcport->d_id.b.domain, fcport->d_id.b.area,
2695 fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 fcport->loop_id = FC_NO_LOOP_ID;
2697 }
2698
2699 break;
2700 }
2701
2702 if (found)
2703 continue;
2704
2705 /* If device was not in our fcports list, then add it. */
2706 list_add_tail(&new_fcport->list, new_fcports);
2707
2708 /* Allocate a new replacement fcport. */
2709 nxt_d_id.b24 = new_fcport->d_id.b24;
2710 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2711 if (new_fcport == NULL) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002712 kfree(swl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 return (QLA_MEMORY_ALLOC_FAILED);
2714 }
2715 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2716 new_fcport->d_id.b24 = nxt_d_id.b24;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002717 new_fcport->vp_idx = ha->vp_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 }
2719
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002720 kfree(swl);
2721 kfree(new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
2723 if (!list_empty(new_fcports))
2724 ha->device_flags |= DFLG_FABRIC_DEVICES;
2725
2726 return (rval);
2727}
2728
2729/*
2730 * qla2x00_find_new_loop_id
2731 * Scan through our port list and find a new usable loop ID.
2732 *
2733 * Input:
2734 * ha: adapter state pointer.
2735 * dev: port structure pointer.
2736 *
2737 * Returns:
2738 * qla2x00 local function return status code.
2739 *
2740 * Context:
2741 * Kernel context.
2742 */
Adrian Bunk413975a2006-06-30 02:33:06 -07002743static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744qla2x00_find_new_loop_id(scsi_qla_host_t *ha, fc_port_t *dev)
2745{
2746 int rval;
2747 int found;
2748 fc_port_t *fcport;
2749 uint16_t first_loop_id;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002750 scsi_qla_host_t *pha = to_qla_parent(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751
2752 rval = QLA_SUCCESS;
2753
2754 /* Save starting loop ID. */
2755 first_loop_id = dev->loop_id;
2756
2757 for (;;) {
2758 /* Skip loop ID if already used by adapter. */
2759 if (dev->loop_id == ha->loop_id) {
2760 dev->loop_id++;
2761 }
2762
2763 /* Skip reserved loop IDs. */
Andrew Vasquez3d716442005-07-06 10:30:26 -07002764 while (qla2x00_is_reserved_id(ha, dev->loop_id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 dev->loop_id++;
2766 }
2767
2768 /* Reset loop ID if passed the end. */
2769 if (dev->loop_id > ha->last_loop_id) {
2770 /* first loop ID. */
2771 dev->loop_id = ha->min_external_loopid;
2772 }
2773
2774 /* Check for loop ID being already in use. */
2775 found = 0;
2776 fcport = NULL;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002777 list_for_each_entry(fcport, &pha->fcports, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 if (fcport->loop_id == dev->loop_id && fcport != dev) {
2779 /* ID possibly in use */
2780 found++;
2781 break;
2782 }
2783 }
2784
2785 /* If not in use then it is free to use. */
2786 if (!found) {
2787 break;
2788 }
2789
2790 /* ID in use. Try next value. */
2791 dev->loop_id++;
2792
2793 /* If wrap around. No free ID to use. */
2794 if (dev->loop_id == first_loop_id) {
2795 dev->loop_id = FC_NO_LOOP_ID;
2796 rval = QLA_FUNCTION_FAILED;
2797 break;
2798 }
2799 }
2800
2801 return (rval);
2802}
2803
2804/*
2805 * qla2x00_device_resync
2806 * Marks devices in the database that needs resynchronization.
2807 *
2808 * Input:
2809 * ha = adapter block pointer.
2810 *
2811 * Context:
2812 * Kernel context.
2813 */
2814static int
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002815qla2x00_device_resync(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816{
2817 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 uint32_t mask;
2819 fc_port_t *fcport;
2820 uint32_t rscn_entry;
2821 uint8_t rscn_out_iter;
2822 uint8_t format;
2823 port_id_t d_id;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002824 scsi_qla_host_t *pha = to_qla_parent(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825
2826 rval = QLA_RSCNS_HANDLED;
2827
2828 while (ha->rscn_out_ptr != ha->rscn_in_ptr ||
2829 ha->flags.rscn_queue_overflow) {
2830
2831 rscn_entry = ha->rscn_queue[ha->rscn_out_ptr];
2832 format = MSB(MSW(rscn_entry));
2833 d_id.b.domain = LSB(MSW(rscn_entry));
2834 d_id.b.area = MSB(LSW(rscn_entry));
2835 d_id.b.al_pa = LSB(LSW(rscn_entry));
2836
2837 DEBUG(printk("scsi(%ld): RSCN queue entry[%d] = "
2838 "[%02x/%02x%02x%02x].\n",
2839 ha->host_no, ha->rscn_out_ptr, format, d_id.b.domain,
2840 d_id.b.area, d_id.b.al_pa));
2841
2842 ha->rscn_out_ptr++;
2843 if (ha->rscn_out_ptr == MAX_RSCN_COUNT)
2844 ha->rscn_out_ptr = 0;
2845
2846 /* Skip duplicate entries. */
2847 for (rscn_out_iter = ha->rscn_out_ptr;
2848 !ha->flags.rscn_queue_overflow &&
2849 rscn_out_iter != ha->rscn_in_ptr;
2850 rscn_out_iter = (rscn_out_iter ==
2851 (MAX_RSCN_COUNT - 1)) ? 0: rscn_out_iter + 1) {
2852
2853 if (rscn_entry != ha->rscn_queue[rscn_out_iter])
2854 break;
2855
2856 DEBUG(printk("scsi(%ld): Skipping duplicate RSCN queue "
2857 "entry found at [%d].\n", ha->host_no,
2858 rscn_out_iter));
2859
2860 ha->rscn_out_ptr = rscn_out_iter;
2861 }
2862
2863 /* Queue overflow, set switch default case. */
2864 if (ha->flags.rscn_queue_overflow) {
2865 DEBUG(printk("scsi(%ld): device_resync: rscn "
2866 "overflow.\n", ha->host_no));
2867
2868 format = 3;
2869 ha->flags.rscn_queue_overflow = 0;
2870 }
2871
2872 switch (format) {
2873 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 mask = 0xffffff;
2875 break;
2876 case 1:
2877 mask = 0xffff00;
2878 break;
2879 case 2:
2880 mask = 0xff0000;
2881 break;
2882 default:
2883 mask = 0x0;
2884 d_id.b24 = 0;
2885 ha->rscn_out_ptr = ha->rscn_in_ptr;
2886 break;
2887 }
2888
2889 rval = QLA_SUCCESS;
2890
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002891 list_for_each_entry(fcport, &pha->fcports, list) {
2892 if (fcport->vp_idx != ha->vp_idx)
2893 continue;
2894
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2896 (fcport->d_id.b24 & mask) != d_id.b24 ||
2897 fcport->port_type == FCT_BROADCAST)
2898 continue;
2899
2900 if (atomic_read(&fcport->state) == FCS_ONLINE) {
2901 if (format != 3 ||
2902 fcport->port_type != FCT_INITIATOR) {
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002903 qla2x00_mark_device_lost(ha, fcport,
2904 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 }
2906 }
2907 fcport->flags &= ~FCF_FARP_DONE;
2908 }
2909 }
2910 return (rval);
2911}
2912
2913/*
2914 * qla2x00_fabric_dev_login
2915 * Login fabric target device and update FC port database.
2916 *
2917 * Input:
2918 * ha: adapter state pointer.
2919 * fcport: port structure list pointer.
2920 * next_loopid: contains value of a new loop ID that can be used
2921 * by the next login attempt.
2922 *
2923 * Returns:
2924 * qla2x00 local function return status code.
2925 *
2926 * Context:
2927 * Kernel context.
2928 */
2929static int
2930qla2x00_fabric_dev_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2931 uint16_t *next_loopid)
2932{
2933 int rval;
2934 int retry;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002935 uint8_t opts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936
2937 rval = QLA_SUCCESS;
2938 retry = 0;
2939
2940 rval = qla2x00_fabric_login(ha, fcport, next_loopid);
2941 if (rval == QLA_SUCCESS) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002942 /* Send an ADISC to tape devices.*/
2943 opts = 0;
2944 if (fcport->flags & FCF_TAPE_PRESENT)
2945 opts |= BIT_1;
2946 rval = qla2x00_get_port_database(ha, fcport, opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 if (rval != QLA_SUCCESS) {
Andrew Vasquezfd34f552007-07-19 15:06:00 -07002948 ha->isp_ops->fabric_logout(ha, fcport->loop_id,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07002949 fcport->d_id.b.domain, fcport->d_id.b.area,
2950 fcport->d_id.b.al_pa);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002951 qla2x00_mark_device_lost(ha, fcport, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 } else {
2953 qla2x00_update_fcport(ha, fcport);
2954 }
2955 }
2956
2957 return (rval);
2958}
2959
2960/*
2961 * qla2x00_fabric_login
2962 * Issue fabric login command.
2963 *
2964 * Input:
2965 * ha = adapter block pointer.
2966 * device = pointer to FC device type structure.
2967 *
2968 * Returns:
2969 * 0 - Login successfully
2970 * 1 - Login failed
2971 * 2 - Initiator device
2972 * 3 - Fatal error
2973 */
2974int
2975qla2x00_fabric_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2976 uint16_t *next_loopid)
2977{
2978 int rval;
2979 int retry;
2980 uint16_t tmp_loopid;
2981 uint16_t mb[MAILBOX_REGISTER_COUNT];
2982
2983 retry = 0;
2984 tmp_loopid = 0;
2985
2986 for (;;) {
2987 DEBUG(printk("scsi(%ld): Trying Fabric Login w/loop id 0x%04x "
2988 "for port %02x%02x%02x.\n",
2989 ha->host_no, fcport->loop_id, fcport->d_id.b.domain,
2990 fcport->d_id.b.area, fcport->d_id.b.al_pa));
2991
2992 /* Login fcport on switch. */
Andrew Vasquezfd34f552007-07-19 15:06:00 -07002993 ha->isp_ops->fabric_login(ha, fcport->loop_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 fcport->d_id.b.domain, fcport->d_id.b.area,
2995 fcport->d_id.b.al_pa, mb, BIT_0);
2996 if (mb[0] == MBS_PORT_ID_USED) {
2997 /*
2998 * Device has another loop ID. The firmware team
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002999 * recommends the driver perform an implicit login with
3000 * the specified ID again. The ID we just used is save
3001 * here so we return with an ID that can be tried by
3002 * the next login.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 */
3004 retry++;
3005 tmp_loopid = fcport->loop_id;
3006 fcport->loop_id = mb[1];
3007
3008 DEBUG(printk("Fabric Login: port in use - next "
3009 "loop id=0x%04x, port Id=%02x%02x%02x.\n",
3010 fcport->loop_id, fcport->d_id.b.domain,
3011 fcport->d_id.b.area, fcport->d_id.b.al_pa));
3012
3013 } else if (mb[0] == MBS_COMMAND_COMPLETE) {
3014 /*
3015 * Login succeeded.
3016 */
3017 if (retry) {
3018 /* A retry occurred before. */
3019 *next_loopid = tmp_loopid;
3020 } else {
3021 /*
3022 * No retry occurred before. Just increment the
3023 * ID value for next login.
3024 */
3025 *next_loopid = (fcport->loop_id + 1);
3026 }
3027
3028 if (mb[1] & BIT_0) {
3029 fcport->port_type = FCT_INITIATOR;
3030 } else {
3031 fcport->port_type = FCT_TARGET;
3032 if (mb[1] & BIT_1) {
3033 fcport->flags |= FCF_TAPE_PRESENT;
3034 }
3035 }
3036
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07003037 if (mb[10] & BIT_0)
3038 fcport->supported_classes |= FC_COS_CLASS2;
3039 if (mb[10] & BIT_1)
3040 fcport->supported_classes |= FC_COS_CLASS3;
3041
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 rval = QLA_SUCCESS;
3043 break;
3044 } else if (mb[0] == MBS_LOOP_ID_USED) {
3045 /*
3046 * Loop ID already used, try next loop ID.
3047 */
3048 fcport->loop_id++;
3049 rval = qla2x00_find_new_loop_id(ha, fcport);
3050 if (rval != QLA_SUCCESS) {
3051 /* Ran out of loop IDs to use */
3052 break;
3053 }
3054 } else if (mb[0] == MBS_COMMAND_ERROR) {
3055 /*
3056 * Firmware possibly timed out during login. If NO
3057 * retries are left to do then the device is declared
3058 * dead.
3059 */
3060 *next_loopid = fcport->loop_id;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07003061 ha->isp_ops->fabric_logout(ha, fcport->loop_id,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07003062 fcport->d_id.b.domain, fcport->d_id.b.area,
3063 fcport->d_id.b.al_pa);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08003064 qla2x00_mark_device_lost(ha, fcport, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065
3066 rval = 1;
3067 break;
3068 } else {
3069 /*
3070 * unrecoverable / not handled error
3071 */
3072 DEBUG2(printk("%s(%ld): failed=%x port_id=%02x%02x%02x "
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003073 "loop_id=%x jiffies=%lx.\n",
3074 __func__, ha->host_no, mb[0],
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 fcport->d_id.b.domain, fcport->d_id.b.area,
3076 fcport->d_id.b.al_pa, fcport->loop_id, jiffies));
3077
3078 *next_loopid = fcport->loop_id;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07003079 ha->isp_ops->fabric_logout(ha, fcport->loop_id,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07003080 fcport->d_id.b.domain, fcport->d_id.b.area,
3081 fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 fcport->loop_id = FC_NO_LOOP_ID;
Andrew Vasquez0eedfcf2005-10-27 11:09:38 -07003083 fcport->login_retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
3085 rval = 3;
3086 break;
3087 }
3088 }
3089
3090 return (rval);
3091}
3092
3093/*
3094 * qla2x00_local_device_login
3095 * Issue local device login command.
3096 *
3097 * Input:
3098 * ha = adapter block pointer.
3099 * loop_id = loop id of device to login to.
3100 *
3101 * Returns (Where's the #define!!!!):
3102 * 0 - Login successfully
3103 * 1 - Login failed
3104 * 3 - Fatal error
3105 */
3106int
andrew.vasquez@qlogic.com9a52a572006-03-09 14:27:44 -08003107qla2x00_local_device_login(scsi_qla_host_t *ha, fc_port_t *fcport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108{
3109 int rval;
3110 uint16_t mb[MAILBOX_REGISTER_COUNT];
3111
3112 memset(mb, 0, sizeof(mb));
andrew.vasquez@qlogic.com9a52a572006-03-09 14:27:44 -08003113 rval = qla2x00_login_local_device(ha, fcport, mb, BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 if (rval == QLA_SUCCESS) {
3115 /* Interrogate mailbox registers for any errors */
3116 if (mb[0] == MBS_COMMAND_ERROR)
3117 rval = 1;
3118 else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
3119 /* device not in PCB table */
3120 rval = 3;
3121 }
3122
3123 return (rval);
3124}
3125
3126/*
3127 * qla2x00_loop_resync
3128 * Resync with fibre channel devices.
3129 *
3130 * Input:
3131 * ha = adapter block pointer.
3132 *
3133 * Returns:
3134 * 0 = success
3135 */
3136int
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003137qla2x00_loop_resync(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138{
3139 int rval;
3140 uint32_t wait_time;
3141
3142 rval = QLA_SUCCESS;
3143
3144 atomic_set(&ha->loop_state, LOOP_UPDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3146 if (ha->flags.online) {
3147 if (!(rval = qla2x00_fw_ready(ha))) {
3148 /* Wait at most MAX_TARGET RSCNs for a stable link. */
3149 wait_time = 256;
3150 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 atomic_set(&ha->loop_state, LOOP_UPDATE);
3152
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003153 /* Issue a marker after FW becomes ready. */
3154 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
3155 ha->marker_needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156
3157 /* Remap devices on Loop. */
3158 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
3159
3160 qla2x00_configure_loop(ha);
3161 wait_time--;
3162 } while (!atomic_read(&ha->loop_down_timer) &&
3163 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
3164 wait_time &&
3165 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
3166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 }
3168
3169 if (test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
3170 return (QLA_FUNCTION_FAILED);
3171 }
3172
3173 if (rval) {
3174 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
3175 }
3176
3177 return (rval);
3178}
3179
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180void
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08003181qla2x00_update_fcports(scsi_qla_host_t *ha)
3182{
3183 fc_port_t *fcport;
3184
3185 /* Go with deferred removal of rport references. */
3186 list_for_each_entry(fcport, &ha->fcports, list)
3187 if (fcport->drport)
3188 qla2x00_rport_del(fcport);
3189}
3190
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191/*
3192* qla2x00_abort_isp
3193* Resets ISP and aborts all outstanding commands.
3194*
3195* Input:
3196* ha = adapter block pointer.
3197*
3198* Returns:
3199* 0 = success
3200*/
3201int
3202qla2x00_abort_isp(scsi_qla_host_t *ha)
3203{
Andrew Vasquez476e8972006-08-23 14:54:55 -07003204 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 uint8_t status = 0;
3206
3207 if (ha->flags.online) {
3208 ha->flags.online = 0;
3209 clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210
3211 qla_printk(KERN_INFO, ha,
3212 "Performing ISP error recovery - ha= %p.\n", ha);
Andrew Vasquezfd34f552007-07-19 15:06:00 -07003213 ha->isp_ops->reset_chip(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214
3215 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
3216 if (atomic_read(&ha->loop_state) != LOOP_DOWN) {
3217 atomic_set(&ha->loop_state, LOOP_DOWN);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08003218 qla2x00_mark_all_devices_lost(ha, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 } else {
3220 if (!atomic_read(&ha->loop_down_timer))
3221 atomic_set(&ha->loop_down_timer,
3222 LOOP_DOWN_TIME);
3223 }
3224
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 /* Requeue all commands in outstanding command list. */
Andrew Vasquezdf4bf0b2008-01-31 12:33:46 -08003226 qla2x00_abort_all_cmds(ha, DID_RESET << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227
Andrew Vasquezfd34f552007-07-19 15:06:00 -07003228 ha->isp_ops->get_flash_version(ha, ha->request_ring);
Andrew Vasquez30c47662007-01-29 10:22:21 -08003229
Andrew Vasquezfd34f552007-07-19 15:06:00 -07003230 ha->isp_ops->nvram_config(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231
3232 if (!qla2x00_restart_isp(ha)) {
3233 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3234
3235 if (!atomic_read(&ha->loop_down_timer)) {
3236 /*
3237 * Issue marker command only when we are going
3238 * to start the I/O .
3239 */
3240 ha->marker_needed = 1;
3241 }
3242
3243 ha->flags.online = 1;
3244
Andrew Vasquezfd34f552007-07-19 15:06:00 -07003245 ha->isp_ops->enable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003247 ha->isp_abort_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
Andrew Vasquez476e8972006-08-23 14:54:55 -07003249
3250 if (ha->eft) {
Andrew Vasquez0afb4672008-01-31 12:33:47 -08003251 memset(ha->eft, 0, EFT_SIZE);
Andrew Vasquez00b6bd22008-01-17 09:02:16 -08003252 rval = qla2x00_enable_eft_trace(ha,
Andrew Vasquez476e8972006-08-23 14:54:55 -07003253 ha->eft_dma, EFT_NUM_BUFFERS);
3254 if (rval) {
3255 qla_printk(KERN_WARNING, ha,
3256 "Unable to reinitialize EFT "
3257 "(%d).\n", rval);
3258 }
3259 }
Andrew Vasquezdf613b92008-01-17 09:02:17 -08003260
3261 if (ha->fce) {
3262 ha->flags.fce_enabled = 1;
3263 memset(ha->fce, 0,
3264 fce_calc_size(ha->fce_bufs));
3265 rval = qla2x00_enable_fce_trace(ha,
3266 ha->fce_dma, ha->fce_bufs, ha->fce_mb,
3267 &ha->fce_bufs);
3268 if (rval) {
3269 qla_printk(KERN_WARNING, ha,
3270 "Unable to reinitialize FCE "
3271 "(%d).\n", rval);
3272 ha->flags.fce_enabled = 0;
3273 }
3274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 } else { /* failed the ISP abort */
3276 ha->flags.online = 1;
3277 if (test_bit(ISP_ABORT_RETRY, &ha->dpc_flags)) {
3278 if (ha->isp_abort_cnt == 0) {
3279 qla_printk(KERN_WARNING, ha,
3280 "ISP error recovery failed - "
3281 "board disabled\n");
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003282 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 * The next call disables the board
3284 * completely.
3285 */
Andrew Vasquezfd34f552007-07-19 15:06:00 -07003286 ha->isp_ops->reset_adapter(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 ha->flags.online = 0;
3288 clear_bit(ISP_ABORT_RETRY,
3289 &ha->dpc_flags);
3290 status = 0;
3291 } else { /* schedule another ISP abort */
3292 ha->isp_abort_cnt--;
3293 DEBUG(printk("qla%ld: ISP abort - "
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003294 "retry remaining %d\n",
Andrew Vasquez744f11fd2006-06-23 16:11:05 -07003295 ha->host_no, ha->isp_abort_cnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 status = 1;
3297 }
3298 } else {
3299 ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
3300 DEBUG(printk("qla2x00(%ld): ISP error recovery "
3301 "- retrying (%d) more times\n",
Andrew Vasquez744f11fd2006-06-23 16:11:05 -07003302 ha->host_no, ha->isp_abort_cnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 set_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3304 status = 1;
3305 }
3306 }
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003307
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 }
3309
3310 if (status) {
3311 qla_printk(KERN_INFO, ha,
3312 "qla2x00_abort_isp: **** FAILED ****\n");
3313 } else {
3314 DEBUG(printk(KERN_INFO
3315 "qla2x00_abort_isp(%ld): exiting.\n",
Andrew Vasquez744f11fd2006-06-23 16:11:05 -07003316 ha->host_no));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 }
3318
3319 return(status);
3320}
3321
3322/*
3323* qla2x00_restart_isp
3324* restarts the ISP after a reset
3325*
3326* Input:
3327* ha = adapter block pointer.
3328*
3329* Returns:
3330* 0 = success
3331*/
3332static int
3333qla2x00_restart_isp(scsi_qla_host_t *ha)
3334{
3335 uint8_t status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 uint32_t wait_time;
3337
3338 /* If firmware needs to be loaded */
3339 if (qla2x00_isp_firmware(ha)) {
3340 ha->flags.online = 0;
Andrew Vasquez3db06522008-01-31 12:33:49 -08003341 if (!(status = ha->isp_ops->chip_diag(ha)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 status = qla2x00_setup_chip(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 }
3344
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 if (!status && !(status = qla2x00_init_rings(ha))) {
3346 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3347 if (!(status = qla2x00_fw_ready(ha))) {
3348 DEBUG(printk("%s(): Start configure loop, "
Andrew Vasquez744f11fd2006-06-23 16:11:05 -07003349 "status = %d\n", __func__, status));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003350
3351 /* Issue a marker after FW becomes ready. */
3352 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
3353
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 ha->flags.online = 1;
3355 /* Wait at most MAX_TARGET RSCNs for a stable link. */
3356 wait_time = 256;
3357 do {
3358 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
3359 qla2x00_configure_loop(ha);
3360 wait_time--;
3361 } while (!atomic_read(&ha->loop_down_timer) &&
3362 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
3363 wait_time &&
3364 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
3365 }
3366
3367 /* if no cable then assume it's good */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003368 if ((ha->device_flags & DFLG_NO_CABLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 status = 0;
3370
3371 DEBUG(printk("%s(): Configure loop done, status = 0x%x\n",
3372 __func__,
Andrew Vasquez744f11fd2006-06-23 16:11:05 -07003373 status));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 }
3375 return (status);
3376}
3377
3378/*
3379* qla2x00_reset_adapter
3380* Reset adapter.
3381*
3382* Input:
3383* ha = adapter block pointer.
3384*/
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003385void
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386qla2x00_reset_adapter(scsi_qla_host_t *ha)
3387{
3388 unsigned long flags = 0;
Andrew Vasquez3d716442005-07-06 10:30:26 -07003389 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390
3391 ha->flags.online = 0;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07003392 ha->isp_ops->disable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 spin_lock_irqsave(&ha->hardware_lock, flags);
3395 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
3396 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
3397 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
3398 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
3399 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3400}
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003401
3402void
3403qla24xx_reset_adapter(scsi_qla_host_t *ha)
3404{
3405 unsigned long flags = 0;
3406 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
3407
3408 ha->flags.online = 0;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07003409 ha->isp_ops->disable_intrs(ha);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003410
3411 spin_lock_irqsave(&ha->hardware_lock, flags);
3412 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
3413 RD_REG_DWORD(&reg->hccr);
3414 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
3415 RD_REG_DWORD(&reg->hccr);
3416 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3417}
3418
David Miller4e08df32007-04-16 12:37:43 -07003419/* On sparc systems, obtain port and node WWN from firmware
3420 * properties.
3421 */
3422static void qla24xx_nvram_wwn_from_ofw(scsi_qla_host_t *ha, struct nvram_24xx *nv)
3423{
3424#ifdef CONFIG_SPARC
3425 struct pci_dev *pdev = ha->pdev;
David S. Miller15576bc2007-05-08 00:36:49 -07003426 struct device_node *dp = pci_device_to_OF_node(pdev);
3427 const u8 *val;
David Miller4e08df32007-04-16 12:37:43 -07003428 int len;
3429
3430 val = of_get_property(dp, "port-wwn", &len);
3431 if (val && len >= WWN_SIZE)
3432 memcpy(nv->port_name, val, WWN_SIZE);
3433
3434 val = of_get_property(dp, "node-wwn", &len);
3435 if (val && len >= WWN_SIZE)
3436 memcpy(nv->node_name, val, WWN_SIZE);
3437#endif
3438}
3439
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003440int
3441qla24xx_nvram_config(scsi_qla_host_t *ha)
3442{
David Miller4e08df32007-04-16 12:37:43 -07003443 int rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003444 struct init_cb_24xx *icb;
3445 struct nvram_24xx *nv;
3446 uint32_t *dptr;
3447 uint8_t *dptr1, *dptr2;
3448 uint32_t chksum;
3449 uint16_t cnt;
3450
David Miller4e08df32007-04-16 12:37:43 -07003451 rval = QLA_SUCCESS;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003452 icb = (struct init_cb_24xx *)ha->init_cb;
Seokmann Ju281afe12007-07-26 13:43:34 -07003453 nv = ha->nvram;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003454
3455 /* Determine NVRAM starting address. */
3456 ha->nvram_size = sizeof(struct nvram_24xx);
3457 ha->nvram_base = FA_NVRAM_FUNC0_ADDR;
andrew.vasquez@qlogic.com6f641792006-03-09 14:27:34 -08003458 ha->vpd_size = FA_NVRAM_VPD_SIZE;
3459 ha->vpd_base = FA_NVRAM_VPD0_ADDR;
3460 if (PCI_FUNC(ha->pdev->devfn)) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003461 ha->nvram_base = FA_NVRAM_FUNC1_ADDR;
andrew.vasquez@qlogic.com6f641792006-03-09 14:27:34 -08003462 ha->vpd_base = FA_NVRAM_VPD1_ADDR;
3463 }
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003464
Seokmann Ju281afe12007-07-26 13:43:34 -07003465 /* Get VPD data into cache */
3466 ha->vpd = ha->nvram + VPD_OFFSET;
3467 ha->isp_ops->read_nvram(ha, (uint8_t *)ha->vpd,
3468 ha->nvram_base - FA_NVRAM_FUNC0_ADDR, FA_NVRAM_VPD_SIZE * 4);
3469
3470 /* Get NVRAM data into cache and calculate checksum. */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003471 dptr = (uint32_t *)nv;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07003472 ha->isp_ops->read_nvram(ha, (uint8_t *)dptr, ha->nvram_base,
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003473 ha->nvram_size);
3474 for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++)
3475 chksum += le32_to_cpu(*dptr++);
3476
3477 DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
Seokmann Ju281afe12007-07-26 13:43:34 -07003478 DEBUG5(qla2x00_dump_buffer((uint8_t *)nv, ha->nvram_size));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003479
3480 /* Bad NVRAM data, set defaults parameters. */
3481 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
3482 || nv->id[3] != ' ' ||
3483 nv->nvram_version < __constant_cpu_to_le16(ICB_VERSION)) {
3484 /* Reset NVRAM data. */
3485 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
3486 "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
3487 le16_to_cpu(nv->nvram_version));
David Miller4e08df32007-04-16 12:37:43 -07003488 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
3489 "invalid -- WWPN) defaults.\n");
3490
3491 /*
3492 * Set default initialization control block.
3493 */
3494 memset(nv, 0, ha->nvram_size);
3495 nv->nvram_version = __constant_cpu_to_le16(ICB_VERSION);
3496 nv->version = __constant_cpu_to_le16(ICB_VERSION);
3497 nv->frame_payload_size = __constant_cpu_to_le16(2048);
3498 nv->execution_throttle = __constant_cpu_to_le16(0xFFFF);
3499 nv->exchange_count = __constant_cpu_to_le16(0);
3500 nv->hard_address = __constant_cpu_to_le16(124);
3501 nv->port_name[0] = 0x21;
3502 nv->port_name[1] = 0x00 + PCI_FUNC(ha->pdev->devfn);
3503 nv->port_name[2] = 0x00;
3504 nv->port_name[3] = 0xe0;
3505 nv->port_name[4] = 0x8b;
3506 nv->port_name[5] = 0x1c;
3507 nv->port_name[6] = 0x55;
3508 nv->port_name[7] = 0x86;
3509 nv->node_name[0] = 0x20;
3510 nv->node_name[1] = 0x00;
3511 nv->node_name[2] = 0x00;
3512 nv->node_name[3] = 0xe0;
3513 nv->node_name[4] = 0x8b;
3514 nv->node_name[5] = 0x1c;
3515 nv->node_name[6] = 0x55;
3516 nv->node_name[7] = 0x86;
3517 qla24xx_nvram_wwn_from_ofw(ha, nv);
3518 nv->login_retry_count = __constant_cpu_to_le16(8);
3519 nv->interrupt_delay_timer = __constant_cpu_to_le16(0);
3520 nv->login_timeout = __constant_cpu_to_le16(0);
3521 nv->firmware_options_1 =
3522 __constant_cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
3523 nv->firmware_options_2 = __constant_cpu_to_le32(2 << 4);
3524 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_12);
3525 nv->firmware_options_3 = __constant_cpu_to_le32(2 << 13);
3526 nv->host_p = __constant_cpu_to_le32(BIT_11|BIT_10);
3527 nv->efi_parameters = __constant_cpu_to_le32(0);
3528 nv->reset_delay = 5;
3529 nv->max_luns_per_target = __constant_cpu_to_le16(128);
3530 nv->port_down_retry_count = __constant_cpu_to_le16(30);
3531 nv->link_down_timeout = __constant_cpu_to_le16(30);
3532
3533 rval = 1;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003534 }
3535
3536 /* Reset Initialization control block */
3537 memset(icb, 0, sizeof(struct init_cb_24xx));
3538
3539 /* Copy 1st segment. */
3540 dptr1 = (uint8_t *)icb;
3541 dptr2 = (uint8_t *)&nv->version;
3542 cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
3543 while (cnt--)
3544 *dptr1++ = *dptr2++;
3545
3546 icb->login_retry_count = nv->login_retry_count;
Andrew Vasquez3ea66e22006-06-23 16:11:27 -07003547 icb->link_down_on_nos = nv->link_down_on_nos;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003548
3549 /* Copy 2nd segment. */
3550 dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
3551 dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
3552 cnt = (uint8_t *)&icb->reserved_3 -
3553 (uint8_t *)&icb->interrupt_delay_timer;
3554 while (cnt--)
3555 *dptr1++ = *dptr2++;
3556
3557 /*
3558 * Setup driver NVRAM options.
3559 */
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08003560 qla2x00_set_model_info(ha, nv->model_name, sizeof(nv->model_name),
3561 "QLA2462");
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003562
Andrew Vasquez5341e862006-05-17 15:09:16 -07003563 /* Use alternate WWN? */
3564 if (nv->host_p & __constant_cpu_to_le32(BIT_15)) {
3565 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
3566 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
3567 }
3568
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003569 /* Prepare nodename */
Andrew Vasquezfd0e7e42006-05-17 15:09:11 -07003570 if ((icb->firmware_options_1 & __constant_cpu_to_le32(BIT_14)) == 0) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003571 /*
3572 * Firmware will apply the following mask if the nodename was
3573 * not provided.
3574 */
3575 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
3576 icb->node_name[0] &= 0xF0;
3577 }
3578
3579 /* Set host adapter parameters. */
3580 ha->flags.disable_risc_code_load = 0;
Andrew Vasquez0c8c39a2006-12-13 19:20:30 -08003581 ha->flags.enable_lip_reset = 0;
3582 ha->flags.enable_lip_full_login =
3583 le32_to_cpu(nv->host_p) & BIT_10 ? 1: 0;
3584 ha->flags.enable_target_reset =
3585 le32_to_cpu(nv->host_p) & BIT_11 ? 1: 0;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003586 ha->flags.enable_led_scheme = 0;
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07003587 ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003588
Andrew Vasquezfd0e7e42006-05-17 15:09:11 -07003589 ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
3590 (BIT_6 | BIT_5 | BIT_4)) >> 4;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003591
3592 memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
3593 sizeof(ha->fw_seriallink_options24));
3594
3595 /* save HBA serial number */
3596 ha->serial0 = icb->port_name[5];
3597 ha->serial1 = icb->port_name[6];
3598 ha->serial2 = icb->port_name[7];
3599 ha->node_name = icb->node_name;
3600 ha->port_name = icb->port_name;
3601
andrew.vasquez@qlogic.combc8fb3c2006-01-13 17:05:42 -08003602 icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
3603
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003604 ha->retry_count = le16_to_cpu(nv->login_retry_count);
3605
3606 /* Set minimum login_timeout to 4 seconds. */
3607 if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
3608 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
3609 if (le16_to_cpu(nv->login_timeout) < 4)
3610 nv->login_timeout = __constant_cpu_to_le16(4);
3611 ha->login_timeout = le16_to_cpu(nv->login_timeout);
3612 icb->login_timeout = cpu_to_le16(nv->login_timeout);
3613
Andrew Vasquez00a537b2008-02-28 14:06:11 -08003614 /* Set minimum RATOV to 100 tenths of a second. */
3615 ha->r_a_tov = 100;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003616
3617 ha->loop_reset_delay = nv->reset_delay;
3618
3619 /* Link Down Timeout = 0:
3620 *
3621 * When Port Down timer expires we will start returning
3622 * I/O's to OS with "DID_NO_CONNECT".
3623 *
3624 * Link Down Timeout != 0:
3625 *
3626 * The driver waits for the link to come up after link down
3627 * before returning I/Os to OS with "DID_NO_CONNECT".
3628 */
3629 if (le16_to_cpu(nv->link_down_timeout) == 0) {
3630 ha->loop_down_abort_time =
3631 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
3632 } else {
3633 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
3634 ha->loop_down_abort_time =
3635 (LOOP_DOWN_TIME - ha->link_down_timeout);
3636 }
3637
3638 /* Need enough time to try and get the port back. */
3639 ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
3640 if (qlport_down_retry)
3641 ha->port_down_retry_count = qlport_down_retry;
3642
3643 /* Set login_retry_count */
3644 ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
3645 if (ha->port_down_retry_count ==
3646 le16_to_cpu(nv->port_down_retry_count) &&
3647 ha->port_down_retry_count > 3)
3648 ha->login_retry_count = ha->port_down_retry_count;
3649 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
3650 ha->login_retry_count = ha->port_down_retry_count;
3651 if (ql2xloginretrycount)
3652 ha->login_retry_count = ql2xloginretrycount;
3653
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07003654 /* Enable ZIO. */
3655 if (!ha->flags.init_done) {
3656 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
3657 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
3658 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
3659 le16_to_cpu(icb->interrupt_delay_timer): 2;
3660 }
3661 icb->firmware_options_2 &= __constant_cpu_to_le32(
3662 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
3663 ha->flags.process_response_queue = 0;
3664 if (ha->zio_mode != QLA_ZIO_DISABLED) {
andrew.vasquez@qlogic.com4a59f712006-03-09 14:27:39 -08003665 ha->zio_mode = QLA_ZIO_MODE_6;
3666
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07003667 DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer delay "
3668 "(%d us).\n", ha->host_no, ha->zio_mode,
3669 ha->zio_timer * 100));
3670 qla_printk(KERN_INFO, ha,
3671 "ZIO mode %d enabled; timer delay (%d us).\n",
3672 ha->zio_mode, ha->zio_timer * 100);
3673
3674 icb->firmware_options_2 |= cpu_to_le32(
3675 (uint32_t)ha->zio_mode);
3676 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
3677 ha->flags.process_response_queue = 1;
3678 }
3679
David Miller4e08df32007-04-16 12:37:43 -07003680 if (rval) {
3681 DEBUG2_3(printk(KERN_WARNING
3682 "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
3683 }
3684 return (rval);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003685}
3686
Adrian Bunk413975a2006-06-30 02:33:06 -07003687static int
Andrew Vasquezd1c61902006-05-17 15:09:00 -07003688qla24xx_load_risc_flash(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3689{
3690 int rval;
3691 int segments, fragment;
3692 uint32_t faddr;
3693 uint32_t *dcode, dlen;
3694 uint32_t risc_addr;
3695 uint32_t risc_size;
3696 uint32_t i;
3697
3698 rval = QLA_SUCCESS;
3699
3700 segments = FA_RISC_CODE_SEGMENTS;
3701 faddr = FA_RISC_CODE_ADDR;
3702 dcode = (uint32_t *)ha->request_ring;
3703 *srisc_addr = 0;
3704
3705 /* Validate firmware image by checking version. */
3706 qla24xx_read_flash_data(ha, dcode, faddr + 4, 4);
3707 for (i = 0; i < 4; i++)
3708 dcode[i] = be32_to_cpu(dcode[i]);
3709 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
3710 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
3711 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
3712 dcode[3] == 0)) {
3713 qla_printk(KERN_WARNING, ha,
3714 "Unable to verify integrity of flash firmware image!\n");
3715 qla_printk(KERN_WARNING, ha,
3716 "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
3717 dcode[1], dcode[2], dcode[3]);
3718
3719 return QLA_FUNCTION_FAILED;
3720 }
3721
3722 while (segments && rval == QLA_SUCCESS) {
3723 /* Read segment's load information. */
3724 qla24xx_read_flash_data(ha, dcode, faddr, 4);
3725
3726 risc_addr = be32_to_cpu(dcode[2]);
3727 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
3728 risc_size = be32_to_cpu(dcode[3]);
3729
3730 fragment = 0;
3731 while (risc_size > 0 && rval == QLA_SUCCESS) {
3732 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
3733 if (dlen > risc_size)
3734 dlen = risc_size;
3735
3736 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3737 "addr %x, number of dwords 0x%x, offset 0x%x.\n",
3738 ha->host_no, risc_addr, dlen, faddr));
3739
3740 qla24xx_read_flash_data(ha, dcode, faddr, dlen);
3741 for (i = 0; i < dlen; i++)
3742 dcode[i] = swab32(dcode[i]);
3743
3744 rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3745 dlen);
3746 if (rval) {
3747 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3748 "segment %d of firmware\n", ha->host_no,
3749 fragment));
3750 qla_printk(KERN_WARNING, ha,
3751 "[ERROR] Failed to load segment %d of "
3752 "firmware\n", fragment);
3753 break;
3754 }
3755
3756 faddr += dlen;
3757 risc_addr += dlen;
3758 risc_size -= dlen;
3759 fragment++;
3760 }
3761
3762 /* Next segment. */
3763 segments--;
3764 }
3765
3766 return rval;
3767}
3768
Andrew Vasquezd1c61902006-05-17 15:09:00 -07003769#define QLA_FW_URL "ftp://ftp.qlogic.com/outgoing/linux/firmware/"
3770
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003771int
Andrew Vasquez54333832005-11-09 15:49:04 -08003772qla2x00_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3773{
3774 int rval;
3775 int i, fragment;
3776 uint16_t *wcode, *fwcode;
3777 uint32_t risc_addr, risc_size, fwclen, wlen, *seg;
3778 struct fw_blob *blob;
3779
3780 /* Load firmware blob. */
3781 blob = qla2x00_request_firmware(ha);
3782 if (!blob) {
3783 qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
Andrew Vasquezd1c61902006-05-17 15:09:00 -07003784 qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
3785 "from: " QLA_FW_URL ".\n");
Andrew Vasquez54333832005-11-09 15:49:04 -08003786 return QLA_FUNCTION_FAILED;
3787 }
3788
3789 rval = QLA_SUCCESS;
3790
3791 wcode = (uint16_t *)ha->request_ring;
3792 *srisc_addr = 0;
3793 fwcode = (uint16_t *)blob->fw->data;
3794 fwclen = 0;
3795
3796 /* Validate firmware image by checking version. */
3797 if (blob->fw->size < 8 * sizeof(uint16_t)) {
3798 qla_printk(KERN_WARNING, ha,
3799 "Unable to verify integrity of firmware image (%Zd)!\n",
3800 blob->fw->size);
3801 goto fail_fw_integrity;
3802 }
3803 for (i = 0; i < 4; i++)
3804 wcode[i] = be16_to_cpu(fwcode[i + 4]);
3805 if ((wcode[0] == 0xffff && wcode[1] == 0xffff && wcode[2] == 0xffff &&
3806 wcode[3] == 0xffff) || (wcode[0] == 0 && wcode[1] == 0 &&
3807 wcode[2] == 0 && wcode[3] == 0)) {
3808 qla_printk(KERN_WARNING, ha,
3809 "Unable to verify integrity of firmware image!\n");
3810 qla_printk(KERN_WARNING, ha,
3811 "Firmware data: %04x %04x %04x %04x!\n", wcode[0],
3812 wcode[1], wcode[2], wcode[3]);
3813 goto fail_fw_integrity;
3814 }
3815
3816 seg = blob->segs;
3817 while (*seg && rval == QLA_SUCCESS) {
3818 risc_addr = *seg;
3819 *srisc_addr = *srisc_addr == 0 ? *seg : *srisc_addr;
3820 risc_size = be16_to_cpu(fwcode[3]);
3821
3822 /* Validate firmware image size. */
3823 fwclen += risc_size * sizeof(uint16_t);
3824 if (blob->fw->size < fwclen) {
3825 qla_printk(KERN_WARNING, ha,
3826 "Unable to verify integrity of firmware image "
3827 "(%Zd)!\n", blob->fw->size);
3828 goto fail_fw_integrity;
3829 }
3830
3831 fragment = 0;
3832 while (risc_size > 0 && rval == QLA_SUCCESS) {
3833 wlen = (uint16_t)(ha->fw_transfer_size >> 1);
3834 if (wlen > risc_size)
3835 wlen = risc_size;
3836
3837 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3838 "addr %x, number of words 0x%x.\n", ha->host_no,
3839 risc_addr, wlen));
3840
3841 for (i = 0; i < wlen; i++)
3842 wcode[i] = swab16(fwcode[i]);
3843
3844 rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3845 wlen);
3846 if (rval) {
3847 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3848 "segment %d of firmware\n", ha->host_no,
3849 fragment));
3850 qla_printk(KERN_WARNING, ha,
3851 "[ERROR] Failed to load segment %d of "
3852 "firmware\n", fragment);
3853 break;
3854 }
3855
3856 fwcode += wlen;
3857 risc_addr += wlen;
3858 risc_size -= wlen;
3859 fragment++;
3860 }
3861
3862 /* Next segment. */
3863 seg++;
3864 }
3865 return rval;
3866
3867fail_fw_integrity:
3868 return QLA_FUNCTION_FAILED;
3869}
3870
3871int
3872qla24xx_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003873{
3874 int rval;
3875 int segments, fragment;
3876 uint32_t *dcode, dlen;
3877 uint32_t risc_addr;
3878 uint32_t risc_size;
3879 uint32_t i;
Andrew Vasquez54333832005-11-09 15:49:04 -08003880 struct fw_blob *blob;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003881 uint32_t *fwcode, fwclen;
3882
Andrew Vasquez54333832005-11-09 15:49:04 -08003883 /* Load firmware blob. */
3884 blob = qla2x00_request_firmware(ha);
3885 if (!blob) {
3886 qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
Andrew Vasquezd1c61902006-05-17 15:09:00 -07003887 qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
3888 "from: " QLA_FW_URL ".\n");
3889
3890 /* Try to load RISC code from flash. */
3891 qla_printk(KERN_ERR, ha, "Attempting to load (potentially "
3892 "outdated) firmware from flash.\n");
3893 return qla24xx_load_risc_flash(ha, srisc_addr);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003894 }
3895
3896 rval = QLA_SUCCESS;
3897
3898 segments = FA_RISC_CODE_SEGMENTS;
3899 dcode = (uint32_t *)ha->request_ring;
3900 *srisc_addr = 0;
Andrew Vasquez54333832005-11-09 15:49:04 -08003901 fwcode = (uint32_t *)blob->fw->data;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003902 fwclen = 0;
3903
3904 /* Validate firmware image by checking version. */
Andrew Vasquez54333832005-11-09 15:49:04 -08003905 if (blob->fw->size < 8 * sizeof(uint32_t)) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003906 qla_printk(KERN_WARNING, ha,
Andrew Vasquez54333832005-11-09 15:49:04 -08003907 "Unable to verify integrity of firmware image (%Zd)!\n",
3908 blob->fw->size);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003909 goto fail_fw_integrity;
3910 }
3911 for (i = 0; i < 4; i++)
3912 dcode[i] = be32_to_cpu(fwcode[i + 4]);
3913 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
3914 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
3915 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
3916 dcode[3] == 0)) {
3917 qla_printk(KERN_WARNING, ha,
Andrew Vasquez54333832005-11-09 15:49:04 -08003918 "Unable to verify integrity of firmware image!\n");
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003919 qla_printk(KERN_WARNING, ha,
3920 "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
3921 dcode[1], dcode[2], dcode[3]);
3922 goto fail_fw_integrity;
3923 }
3924
3925 while (segments && rval == QLA_SUCCESS) {
3926 risc_addr = be32_to_cpu(fwcode[2]);
3927 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
3928 risc_size = be32_to_cpu(fwcode[3]);
3929
3930 /* Validate firmware image size. */
3931 fwclen += risc_size * sizeof(uint32_t);
Andrew Vasquez54333832005-11-09 15:49:04 -08003932 if (blob->fw->size < fwclen) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003933 qla_printk(KERN_WARNING, ha,
Andrew Vasquez54333832005-11-09 15:49:04 -08003934 "Unable to verify integrity of firmware image "
3935 "(%Zd)!\n", blob->fw->size);
3936
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003937 goto fail_fw_integrity;
3938 }
3939
3940 fragment = 0;
3941 while (risc_size > 0 && rval == QLA_SUCCESS) {
3942 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
3943 if (dlen > risc_size)
3944 dlen = risc_size;
3945
3946 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3947 "addr %x, number of dwords 0x%x.\n", ha->host_no,
3948 risc_addr, dlen));
3949
3950 for (i = 0; i < dlen; i++)
3951 dcode[i] = swab32(fwcode[i]);
3952
andrew.vasquez@qlogic.com590f98e2006-01-13 17:05:37 -08003953 rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3954 dlen);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003955 if (rval) {
3956 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3957 "segment %d of firmware\n", ha->host_no,
3958 fragment));
3959 qla_printk(KERN_WARNING, ha,
3960 "[ERROR] Failed to load segment %d of "
3961 "firmware\n", fragment);
3962 break;
3963 }
3964
3965 fwcode += dlen;
3966 risc_addr += dlen;
3967 risc_size -= dlen;
3968 fragment++;
3969 }
3970
3971 /* Next segment. */
3972 segments--;
3973 }
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003974 return rval;
3975
3976fail_fw_integrity:
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003977 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003978}
Andrew Vasquez18c6c122006-10-13 09:33:38 -07003979
3980void
3981qla2x00_try_to_stop_firmware(scsi_qla_host_t *ha)
3982{
3983 int ret, retries;
3984
Andrew Vasqueze4289242007-07-19 15:05:56 -07003985 if (!IS_FWI2_CAPABLE(ha))
Andrew Vasquez18c6c122006-10-13 09:33:38 -07003986 return;
Andrew Vasquez75edf812007-05-07 07:43:00 -07003987 if (!ha->fw_major_version)
3988 return;
Andrew Vasquez18c6c122006-10-13 09:33:38 -07003989
3990 ret = qla2x00_stop_firmware(ha);
Andrew Vasquez7c7f1f22008-02-28 14:06:09 -08003991 for (retries = 5; ret != QLA_SUCCESS && ret != QLA_FUNCTION_TIMEOUT &&
3992 retries ; retries--) {
Andrew Vasquez18c6c122006-10-13 09:33:38 -07003993 qla2x00_reset_chip(ha);
3994 if (qla2x00_chip_diag(ha) != QLA_SUCCESS)
3995 continue;
3996 if (qla2x00_setup_chip(ha) != QLA_SUCCESS)
3997 continue;
3998 qla_printk(KERN_INFO, ha,
3999 "Attempting retry of stop-firmware command...\n");
4000 ret = qla2x00_stop_firmware(ha);
4001 }
4002}
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07004003
4004int
4005qla24xx_configure_vhba(scsi_qla_host_t *ha)
4006{
4007 int rval = QLA_SUCCESS;
4008 uint16_t mb[MAILBOX_REGISTER_COUNT];
4009
4010 if (!ha->parent)
4011 return -EINVAL;
4012
4013 rval = qla2x00_fw_ready(ha);
4014 if (rval == QLA_SUCCESS) {
4015 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
4016 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
4017 }
4018
4019 ha->flags.management_server_logged_in = 0;
4020
4021 /* Login to SNS first */
4022 qla24xx_login_fabric(ha, NPH_SNS, 0xff, 0xff, 0xfc,
4023 mb, BIT_1);
4024 if (mb[0] != MBS_COMMAND_COMPLETE) {
4025 DEBUG15(qla_printk(KERN_INFO, ha,
4026 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
4027 "mb[2]=%x mb[6]=%x mb[7]=%x\n", NPH_SNS,
4028 mb[0], mb[1], mb[2], mb[6], mb[7]));
4029 return (QLA_FUNCTION_FAILED);
4030 }
4031
4032 atomic_set(&ha->loop_down_timer, 0);
4033 atomic_set(&ha->loop_state, LOOP_UP);
4034 set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
4035 set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
4036 rval = qla2x00_loop_resync(ha);
4037
4038 return rval;
4039}