blob: 5ec798c2bf13d4f0404d10d3b4409db9ec3a07e6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Andrew Vasquezfa90c542005-10-27 11:10:08 -07002 * QLogic Fibre Channel HBA Driver
3 * Copyright (c) 2003-2005 QLogic Corporation
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Andrew Vasquezfa90c542005-10-27 11:10:08 -07005 * See LICENSE.qla2xxx for copyright and licensing details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7#include "qla_def.h"
8
9#include <linux/delay.h>
Andrew Vasquez01071092005-07-06 10:31:37 -070010#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
12#include "qla_devtbl.h"
13
David Miller4e08df32007-04-16 12:37:43 -070014#ifdef CONFIG_SPARC
15#include <asm/prom.h>
David Miller4e08df32007-04-16 12:37:43 -070016#endif
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018/* XXX(hch): this is ugly, but we don't want to pull in exioctl.h */
19#ifndef EXT_IS_LUN_BIT_SET
20#define EXT_IS_LUN_BIT_SET(P,L) \
21 (((P)->mask[L/8] & (0x80 >> (L%8)))?1:0)
22#define EXT_SET_LUN_BIT(P,L) \
23 ((P)->mask[L/8] |= (0x80 >> (L%8)))
24#endif
25
26/*
27* QLogic ISP2x00 Hardware Support Function Prototypes.
28*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070029static int qla2x00_isp_firmware(scsi_qla_host_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030static void qla2x00_resize_request_q(scsi_qla_host_t *);
31static int qla2x00_setup_chip(scsi_qla_host_t *);
32static void qla2x00_init_response_q_entries(scsi_qla_host_t *);
33static int qla2x00_init_rings(scsi_qla_host_t *);
34static int qla2x00_fw_ready(scsi_qla_host_t *);
35static int qla2x00_configure_hba(scsi_qla_host_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036static int qla2x00_configure_loop(scsi_qla_host_t *);
37static int qla2x00_configure_local_loop(scsi_qla_host_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038static int qla2x00_configure_fabric(scsi_qla_host_t *);
39static int qla2x00_find_all_fabric_devs(scsi_qla_host_t *, struct list_head *);
40static int qla2x00_device_resync(scsi_qla_host_t *);
41static int qla2x00_fabric_dev_login(scsi_qla_host_t *, fc_port_t *,
42 uint16_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44static int qla2x00_restart_isp(scsi_qla_host_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Adrian Bunk413975a2006-06-30 02:33:06 -070046static int qla2x00_find_new_loop_id(scsi_qla_host_t *ha, fc_port_t *dev);
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048/****************************************************************************/
49/* QLogic ISP2x00 Hardware Support Functions. */
50/****************************************************************************/
51
52/*
53* qla2x00_initialize_adapter
54* Initialize board.
55*
56* Input:
57* ha = adapter block pointer.
58*
59* Returns:
60* 0 = success
61*/
62int
63qla2x00_initialize_adapter(scsi_qla_host_t *ha)
64{
65 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67 /* Clear adapter flags. */
68 ha->flags.online = 0;
69 ha->flags.reset_active = 0;
70 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
71 atomic_set(&ha->loop_state, LOOP_DOWN);
Andrew Vasquez26032212007-01-29 10:22:23 -080072 ha->device_flags = DFLG_NO_CABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 ha->dpc_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 ha->flags.management_server_logged_in = 0;
75 ha->marker_needed = 0;
76 ha->mbx_flags = 0;
77 ha->isp_abort_cnt = 0;
78 ha->beacon_blink_led = 0;
Andrew Vasquezcca53352005-08-26 19:08:30 -070079 set_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Andrew Vasquez01071092005-07-06 10:31:37 -070081 qla_printk(KERN_INFO, ha, "Configuring PCI space...\n");
Andrew Vasquezfd34f552007-07-19 15:06:00 -070082 rval = ha->isp_ops->pci_config(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 if (rval) {
Andrew Vasquez7c98a042007-01-29 10:22:29 -080084 DEBUG2(printk("scsi(%ld): Unable to configure PCI space.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 ha->host_no));
86 return (rval);
87 }
88
Andrew Vasquezfd34f552007-07-19 15:06:00 -070089 ha->isp_ops->reset_chip(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Andrew Vasquezfd34f552007-07-19 15:06:00 -070091 ha->isp_ops->get_flash_version(ha, ha->request_ring);
Andrew Vasquez30c47662007-01-29 10:22:21 -080092
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 qla_printk(KERN_INFO, ha, "Configure NVRAM parameters...\n");
Andrew Vasquez01071092005-07-06 10:31:37 -070094
Andrew Vasquezfd34f552007-07-19 15:06:00 -070095 ha->isp_ops->nvram_config(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Andrew Vasquezd4c760c2006-06-23 16:10:39 -070097 if (ha->flags.disable_serdes) {
98 /* Mask HBA via NVRAM settings? */
99 qla_printk(KERN_INFO, ha, "Masking HBA WWPN "
100 "%02x%02x%02x%02x%02x%02x%02x%02x (via NVRAM).\n",
101 ha->port_name[0], ha->port_name[1],
102 ha->port_name[2], ha->port_name[3],
103 ha->port_name[4], ha->port_name[5],
104 ha->port_name[6], ha->port_name[7]);
105 return QLA_FUNCTION_FAILED;
106 }
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 qla_printk(KERN_INFO, ha, "Verifying loaded RISC code...\n");
109
Andrew Vasquezd19044c2006-11-22 08:22:19 -0800110 if (qla2x00_isp_firmware(ha) != QLA_SUCCESS) {
Andrew Vasquezfd34f552007-07-19 15:06:00 -0700111 rval = ha->isp_ops->chip_diag(ha);
Andrew Vasquezd19044c2006-11-22 08:22:19 -0800112 if (rval)
113 return (rval);
114 rval = qla2x00_setup_chip(ha);
115 if (rval)
116 return (rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
Andrew Vasquezd19044c2006-11-22 08:22:19 -0800118 rval = qla2x00_init_rings(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 return (rval);
121}
122
123/**
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700124 * qla2100_pci_config() - Setup ISP21xx PCI configuration registers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 * @ha: HA context
126 *
127 * Returns 0 on success.
128 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700129int
130qla2100_pci_config(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Andrew Vasqueza157b102007-05-07 07:43:01 -0700132 uint16_t w;
Adam Kropelin27b2f672005-09-16 19:28:20 -0700133 uint32_t d;
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700134 unsigned long flags;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700135 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 pci_set_master(ha->pdev);
Andrew Vasquezaf6177d2007-07-19 15:06:02 -0700138 pci_try_set_mwi(ha->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
Andrew Vasqueza157b102007-05-07 07:43:01 -0700141 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
143
144 /* Reset expansion ROM address decode enable */
Adam Kropelin27b2f672005-09-16 19:28:20 -0700145 pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
146 d &= ~PCI_ROM_ADDRESS_ENABLE;
147 pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700149 /* Get PCI bus information. */
150 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez3d716442005-07-06 10:30:26 -0700151 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700152 spin_unlock_irqrestore(&ha->hardware_lock, flags);
153
154 return QLA_SUCCESS;
155}
156
157/**
158 * qla2300_pci_config() - Setup ISP23xx PCI configuration registers.
159 * @ha: HA context
160 *
161 * Returns 0 on success.
162 */
163int
164qla2300_pci_config(scsi_qla_host_t *ha)
165{
Andrew Vasqueza157b102007-05-07 07:43:01 -0700166 uint16_t w;
Adam Kropelin27b2f672005-09-16 19:28:20 -0700167 uint32_t d;
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700168 unsigned long flags = 0;
169 uint32_t cnt;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700170 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700171
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700172 pci_set_master(ha->pdev);
Andrew Vasquezaf6177d2007-07-19 15:06:02 -0700173 pci_try_set_mwi(ha->pdev);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700174
175 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
Andrew Vasqueza157b102007-05-07 07:43:01 -0700176 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700177
178 if (IS_QLA2322(ha) || IS_QLA6322(ha))
179 w &= ~PCI_COMMAND_INTX_DISABLE;
Andrew Vasqueza157b102007-05-07 07:43:01 -0700180 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700181
182 /*
183 * If this is a 2300 card and not 2312, reset the
184 * COMMAND_INVALIDATE due to a bug in the 2300. Unfortunately,
185 * the 2310 also reports itself as a 2300 so we need to get the
186 * fb revision level -- a 6 indicates it really is a 2300 and
187 * not a 2310.
188 */
189 if (IS_QLA2300(ha)) {
190 spin_lock_irqsave(&ha->hardware_lock, flags);
191
192 /* Pause RISC. */
Andrew Vasquez3d716442005-07-06 10:30:26 -0700193 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700194 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquez3d716442005-07-06 10:30:26 -0700195 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700196 break;
197
198 udelay(10);
199 }
200
201 /* Select FPM registers. */
Andrew Vasquez3d716442005-07-06 10:30:26 -0700202 WRT_REG_WORD(&reg->ctrl_status, 0x20);
203 RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700204
205 /* Get the fb rev level */
Andrew Vasquez3d716442005-07-06 10:30:26 -0700206 ha->fb_rev = RD_FB_CMD_REG(ha, reg);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700207
208 if (ha->fb_rev == FPM_2300)
Andrew Vasqueza157b102007-05-07 07:43:01 -0700209 pci_clear_mwi(ha->pdev);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700210
211 /* Deselect FPM registers. */
Andrew Vasquez3d716442005-07-06 10:30:26 -0700212 WRT_REG_WORD(&reg->ctrl_status, 0x0);
213 RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700214
215 /* Release RISC module. */
Andrew Vasquez3d716442005-07-06 10:30:26 -0700216 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700217 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquez3d716442005-07-06 10:30:26 -0700218 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) == 0)
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700219 break;
220
221 udelay(10);
222 }
223
224 spin_unlock_irqrestore(&ha->hardware_lock, flags);
225 }
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700226
227 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
228
229 /* Reset expansion ROM address decode enable */
Adam Kropelin27b2f672005-09-16 19:28:20 -0700230 pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
231 d &= ~PCI_ROM_ADDRESS_ENABLE;
232 pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700233
234 /* Get PCI bus information. */
235 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez3d716442005-07-06 10:30:26 -0700236 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700237 spin_unlock_irqrestore(&ha->hardware_lock, flags);
238
239 return QLA_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
242/**
Andrew Vasquez01071092005-07-06 10:31:37 -0700243 * qla24xx_pci_config() - Setup ISP24xx PCI configuration registers.
244 * @ha: HA context
245 *
246 * Returns 0 on success.
247 */
248int
249qla24xx_pci_config(scsi_qla_host_t *ha)
250{
Andrew Vasqueza157b102007-05-07 07:43:01 -0700251 uint16_t w;
Adam Kropelin27b2f672005-09-16 19:28:20 -0700252 uint32_t d;
Andrew Vasquez01071092005-07-06 10:31:37 -0700253 unsigned long flags = 0;
254 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
Andrew Vasquez01071092005-07-06 10:31:37 -0700255
256 pci_set_master(ha->pdev);
Andrew Vasquezaf6177d2007-07-19 15:06:02 -0700257 pci_try_set_mwi(ha->pdev);
Andrew Vasquez01071092005-07-06 10:31:37 -0700258
259 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
Andrew Vasqueza157b102007-05-07 07:43:01 -0700260 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
Andrew Vasquez01071092005-07-06 10:31:37 -0700261 w &= ~PCI_COMMAND_INTX_DISABLE;
262 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
263
264 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
265
266 /* PCI-X -- adjust Maximum Memory Read Byte Count (2048). */
Andrew Vasquezf85ec182007-07-19 15:06:01 -0700267 if (pci_find_capability(ha->pdev, PCI_CAP_ID_PCIX))
268 pcix_set_mmrbc(ha->pdev, 2048);
Andrew Vasquez01071092005-07-06 10:31:37 -0700269
270 /* PCIe -- adjust Maximum Read Request Size (2048). */
Andrew Vasquezf85ec182007-07-19 15:06:01 -0700271 if (pci_find_capability(ha->pdev, PCI_CAP_ID_EXP))
272 pcie_set_readrq(ha->pdev, 2048);
Andrew Vasquez01071092005-07-06 10:31:37 -0700273
274 /* Reset expansion ROM address decode enable */
Adam Kropelin27b2f672005-09-16 19:28:20 -0700275 pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
276 d &= ~PCI_ROM_ADDRESS_ENABLE;
277 pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
Andrew Vasquez01071092005-07-06 10:31:37 -0700278
Auke Kok44c10132007-06-08 15:46:36 -0700279 ha->chip_revision = ha->pdev->revision;
Andrew Vasqueza8488ab2007-01-29 10:22:19 -0800280
Andrew Vasquez01071092005-07-06 10:31:37 -0700281 /* Get PCI bus information. */
282 spin_lock_irqsave(&ha->hardware_lock, flags);
283 ha->pci_attr = RD_REG_DWORD(&reg->ctrl_status);
284 spin_unlock_irqrestore(&ha->hardware_lock, flags);
285
286 return QLA_SUCCESS;
287}
288
289/**
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -0700290 * qla25xx_pci_config() - Setup ISP25xx PCI configuration registers.
291 * @ha: HA context
292 *
293 * Returns 0 on success.
294 */
295int
296qla25xx_pci_config(scsi_qla_host_t *ha)
297{
298 uint16_t w;
299 uint32_t d;
300
301 pci_set_master(ha->pdev);
302 pci_try_set_mwi(ha->pdev);
303
304 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
305 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
306 w &= ~PCI_COMMAND_INTX_DISABLE;
307 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
308
309 /* PCIe -- adjust Maximum Read Request Size (2048). */
310 if (pci_find_capability(ha->pdev, PCI_CAP_ID_EXP))
311 pcie_set_readrq(ha->pdev, 2048);
312
313 /* Reset expansion ROM address decode enable */
314 pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
315 d &= ~PCI_ROM_ADDRESS_ENABLE;
316 pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
317
318 ha->chip_revision = ha->pdev->revision;
319
320 return QLA_SUCCESS;
321}
322
323/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 * qla2x00_isp_firmware() - Choose firmware image.
325 * @ha: HA context
326 *
327 * Returns 0 on success.
328 */
329static int
330qla2x00_isp_firmware(scsi_qla_host_t *ha)
331{
332 int rval;
333
334 /* Assume loading risc code */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700335 rval = QLA_FUNCTION_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 if (ha->flags.disable_risc_code_load) {
338 DEBUG2(printk("scsi(%ld): RISC CODE NOT loaded\n",
339 ha->host_no));
340 qla_printk(KERN_INFO, ha, "RISC CODE NOT loaded\n");
341
342 /* Verify checksum of loaded RISC code. */
Andrew Vasquez441d1072006-05-17 15:09:34 -0700343 rval = qla2x00_verify_checksum(ha, ha->fw_srisc_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
345
346 if (rval) {
347 DEBUG2_3(printk("scsi(%ld): **** Load RISC code ****\n",
348 ha->host_no));
349 }
350
351 return (rval);
352}
353
354/**
355 * qla2x00_reset_chip() - Reset ISP chip.
356 * @ha: HA context
357 *
358 * Returns 0 on success.
359 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700360void
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700361qla2x00_reset_chip(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 unsigned long flags = 0;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700364 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 uint32_t cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 uint16_t cmd;
367
Andrew Vasquezfd34f552007-07-19 15:06:00 -0700368 ha->isp_ops->disable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 spin_lock_irqsave(&ha->hardware_lock, flags);
371
372 /* Turn off master enable */
373 cmd = 0;
374 pci_read_config_word(ha->pdev, PCI_COMMAND, &cmd);
375 cmd &= ~PCI_COMMAND_MASTER;
376 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
377
378 if (!IS_QLA2100(ha)) {
379 /* Pause RISC. */
380 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
381 if (IS_QLA2200(ha) || IS_QLA2300(ha)) {
382 for (cnt = 0; cnt < 30000; cnt++) {
383 if ((RD_REG_WORD(&reg->hccr) &
384 HCCR_RISC_PAUSE) != 0)
385 break;
386 udelay(100);
387 }
388 } else {
389 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
390 udelay(10);
391 }
392
393 /* Select FPM registers. */
394 WRT_REG_WORD(&reg->ctrl_status, 0x20);
395 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
396
397 /* FPM Soft Reset. */
398 WRT_REG_WORD(&reg->fpm_diag_config, 0x100);
399 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
400
401 /* Toggle Fpm Reset. */
402 if (!IS_QLA2200(ha)) {
403 WRT_REG_WORD(&reg->fpm_diag_config, 0x0);
404 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
405 }
406
407 /* Select frame buffer registers. */
408 WRT_REG_WORD(&reg->ctrl_status, 0x10);
409 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
410
411 /* Reset frame buffer FIFOs. */
412 if (IS_QLA2200(ha)) {
413 WRT_FB_CMD_REG(ha, reg, 0xa000);
414 RD_FB_CMD_REG(ha, reg); /* PCI Posting. */
415 } else {
416 WRT_FB_CMD_REG(ha, reg, 0x00fc);
417
418 /* Read back fb_cmd until zero or 3 seconds max */
419 for (cnt = 0; cnt < 3000; cnt++) {
420 if ((RD_FB_CMD_REG(ha, reg) & 0xff) == 0)
421 break;
422 udelay(100);
423 }
424 }
425
426 /* Select RISC module registers. */
427 WRT_REG_WORD(&reg->ctrl_status, 0);
428 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
429
430 /* Reset RISC processor. */
431 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
432 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
433
434 /* Release RISC processor. */
435 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
436 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
437 }
438
439 WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
440 WRT_REG_WORD(&reg->hccr, HCCR_CLR_HOST_INT);
441
442 /* Reset ISP chip. */
443 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
444
445 /* Wait for RISC to recover from reset. */
446 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
447 /*
448 * It is necessary to for a delay here since the card doesn't
449 * respond to PCI reads during a reset. On some architectures
450 * this will result in an MCA.
451 */
452 udelay(20);
453 for (cnt = 30000; cnt; cnt--) {
454 if ((RD_REG_WORD(&reg->ctrl_status) &
455 CSR_ISP_SOFT_RESET) == 0)
456 break;
457 udelay(100);
458 }
459 } else
460 udelay(10);
461
462 /* Reset RISC processor. */
463 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
464
465 WRT_REG_WORD(&reg->semaphore, 0);
466
467 /* Release RISC processor. */
468 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
469 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
470
471 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
472 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquezffb39f02006-05-17 15:09:06 -0700473 if (RD_MAILBOX_REG(ha, reg, 0) != MBS_BUSY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 udelay(100);
477 }
478 } else
479 udelay(100);
480
481 /* Turn on master enable */
482 cmd |= PCI_COMMAND_MASTER;
483 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
484
485 /* Disable RISC pause on FPM parity error. */
486 if (!IS_QLA2100(ha)) {
487 WRT_REG_WORD(&reg->hccr, HCCR_DISABLE_PARITY_PAUSE);
488 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
489 }
490
491 spin_unlock_irqrestore(&ha->hardware_lock, flags);
492}
493
494/**
Andrew Vasquez88c26662005-07-08 17:59:26 -0700495 * qla24xx_reset_risc() - Perform full reset of ISP24xx RISC.
Andrew Vasquez01071092005-07-06 10:31:37 -0700496 * @ha: HA context
497 *
498 * Returns 0 on success.
499 */
Andrew Vasquez88c26662005-07-08 17:59:26 -0700500static inline void
501qla24xx_reset_risc(scsi_qla_host_t *ha)
Andrew Vasquez01071092005-07-06 10:31:37 -0700502{
503 unsigned long flags = 0;
504 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
505 uint32_t cnt, d2;
Andrew Vasquez335a1cc2005-11-08 14:37:48 -0800506 uint16_t wd;
Andrew Vasquez01071092005-07-06 10:31:37 -0700507
Andrew Vasquez01071092005-07-06 10:31:37 -0700508 spin_lock_irqsave(&ha->hardware_lock, flags);
509
510 /* Reset RISC. */
511 WRT_REG_DWORD(&reg->ctrl_status, CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
512 for (cnt = 0; cnt < 30000; cnt++) {
513 if ((RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE) == 0)
514 break;
515
516 udelay(10);
517 }
518
519 WRT_REG_DWORD(&reg->ctrl_status,
520 CSRX_ISP_SOFT_RESET|CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
Andrew Vasquez335a1cc2005-11-08 14:37:48 -0800521 pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
Andrew Vasquez88c26662005-07-08 17:59:26 -0700522
Andrew Vasquez335a1cc2005-11-08 14:37:48 -0800523 udelay(100);
Andrew Vasquez88c26662005-07-08 17:59:26 -0700524 /* Wait for firmware to complete NVRAM accesses. */
Andrew Vasquez88c26662005-07-08 17:59:26 -0700525 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
526 for (cnt = 10000 ; cnt && d2; cnt--) {
527 udelay(5);
528 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
529 barrier();
530 }
531
Andrew Vasquez335a1cc2005-11-08 14:37:48 -0800532 /* Wait for soft-reset to complete. */
Andrew Vasquez01071092005-07-06 10:31:37 -0700533 d2 = RD_REG_DWORD(&reg->ctrl_status);
534 for (cnt = 6000000 ; cnt && (d2 & CSRX_ISP_SOFT_RESET); cnt--) {
535 udelay(5);
536 d2 = RD_REG_DWORD(&reg->ctrl_status);
537 barrier();
538 }
539
540 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
541 RD_REG_DWORD(&reg->hccr);
542
543 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
544 RD_REG_DWORD(&reg->hccr);
545
546 WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_RESET);
547 RD_REG_DWORD(&reg->hccr);
548
549 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
550 for (cnt = 6000000 ; cnt && d2; cnt--) {
551 udelay(5);
552 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
553 barrier();
554 }
555
556 spin_unlock_irqrestore(&ha->hardware_lock, flags);
557}
558
559/**
Andrew Vasquez88c26662005-07-08 17:59:26 -0700560 * qla24xx_reset_chip() - Reset ISP24xx chip.
561 * @ha: HA context
562 *
563 * Returns 0 on success.
564 */
565void
566qla24xx_reset_chip(scsi_qla_host_t *ha)
567{
Andrew Vasquezfd34f552007-07-19 15:06:00 -0700568 ha->isp_ops->disable_intrs(ha);
Andrew Vasquez88c26662005-07-08 17:59:26 -0700569
570 /* Perform RISC reset. */
571 qla24xx_reset_risc(ha);
572}
573
574/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 * qla2x00_chip_diag() - Test chip for proper operation.
576 * @ha: HA context
577 *
578 * Returns 0 on success.
579 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700580int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581qla2x00_chip_diag(scsi_qla_host_t *ha)
582{
583 int rval;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700584 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 unsigned long flags = 0;
586 uint16_t data;
587 uint32_t cnt;
588 uint16_t mb[5];
589
590 /* Assume a failed state */
591 rval = QLA_FUNCTION_FAILED;
592
593 DEBUG3(printk("scsi(%ld): Testing device at %lx.\n",
594 ha->host_no, (u_long)&reg->flash_address));
595
596 spin_lock_irqsave(&ha->hardware_lock, flags);
597
598 /* Reset ISP chip. */
599 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
600
601 /*
602 * We need to have a delay here since the card will not respond while
603 * in reset causing an MCA on some architectures.
604 */
605 udelay(20);
606 data = qla2x00_debounce_register(&reg->ctrl_status);
607 for (cnt = 6000000 ; cnt && (data & CSR_ISP_SOFT_RESET); cnt--) {
608 udelay(5);
609 data = RD_REG_WORD(&reg->ctrl_status);
610 barrier();
611 }
612
613 if (!cnt)
614 goto chip_diag_failed;
615
616 DEBUG3(printk("scsi(%ld): Reset register cleared by chip reset\n",
617 ha->host_no));
618
619 /* Reset RISC processor. */
620 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
621 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
622
623 /* Workaround for QLA2312 PCI parity error */
624 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
625 data = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 0));
626 for (cnt = 6000000; cnt && (data == MBS_BUSY); cnt--) {
627 udelay(5);
628 data = RD_MAILBOX_REG(ha, reg, 0);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700629 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 }
631 } else
632 udelay(10);
633
634 if (!cnt)
635 goto chip_diag_failed;
636
637 /* Check product ID of chip */
638 DEBUG3(printk("scsi(%ld): Checking product ID of chip\n", ha->host_no));
639
640 mb[1] = RD_MAILBOX_REG(ha, reg, 1);
641 mb[2] = RD_MAILBOX_REG(ha, reg, 2);
642 mb[3] = RD_MAILBOX_REG(ha, reg, 3);
643 mb[4] = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 4));
644 if (mb[1] != PROD_ID_1 || (mb[2] != PROD_ID_2 && mb[2] != PROD_ID_2a) ||
645 mb[3] != PROD_ID_3) {
646 qla_printk(KERN_WARNING, ha,
647 "Wrong product ID = 0x%x,0x%x,0x%x\n", mb[1], mb[2], mb[3]);
648
649 goto chip_diag_failed;
650 }
651 ha->product_id[0] = mb[1];
652 ha->product_id[1] = mb[2];
653 ha->product_id[2] = mb[3];
654 ha->product_id[3] = mb[4];
655
656 /* Adjust fw RISC transfer size */
657 if (ha->request_q_length > 1024)
658 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
659 else
660 ha->fw_transfer_size = REQUEST_ENTRY_SIZE *
661 ha->request_q_length;
662
663 if (IS_QLA2200(ha) &&
664 RD_MAILBOX_REG(ha, reg, 7) == QLA2200A_RISC_ROM_VER) {
665 /* Limit firmware transfer size with a 2200A */
666 DEBUG3(printk("scsi(%ld): Found QLA2200A chip.\n",
667 ha->host_no));
668
andrew.vasquez@qlogic.comea5b6382006-03-09 14:27:08 -0800669 ha->device_type |= DT_ISP2200A;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 ha->fw_transfer_size = 128;
671 }
672
673 /* Wrap Incoming Mailboxes Test. */
674 spin_unlock_irqrestore(&ha->hardware_lock, flags);
675
676 DEBUG3(printk("scsi(%ld): Checking mailboxes.\n", ha->host_no));
677 rval = qla2x00_mbx_reg_test(ha);
678 if (rval) {
679 DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
680 ha->host_no));
681 qla_printk(KERN_WARNING, ha,
682 "Failed mailbox send register test\n");
683 }
684 else {
685 /* Flag a successful rval */
686 rval = QLA_SUCCESS;
687 }
688 spin_lock_irqsave(&ha->hardware_lock, flags);
689
690chip_diag_failed:
691 if (rval)
692 DEBUG2_3(printk("scsi(%ld): Chip diagnostics **** FAILED "
693 "****\n", ha->host_no));
694
695 spin_unlock_irqrestore(&ha->hardware_lock, flags);
696
697 return (rval);
698}
699
700/**
Andrew Vasquez01071092005-07-06 10:31:37 -0700701 * qla24xx_chip_diag() - Test ISP24xx for proper operation.
702 * @ha: HA context
703 *
704 * Returns 0 on success.
705 */
706int
707qla24xx_chip_diag(scsi_qla_host_t *ha)
708{
709 int rval;
Andrew Vasquez01071092005-07-06 10:31:37 -0700710
Andrew Vasquez88c26662005-07-08 17:59:26 -0700711 /* Perform RISC reset. */
712 qla24xx_reset_risc(ha);
Andrew Vasquez01071092005-07-06 10:31:37 -0700713
714 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
715
716 rval = qla2x00_mbx_reg_test(ha);
717 if (rval) {
718 DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
719 ha->host_no));
720 qla_printk(KERN_WARNING, ha,
721 "Failed mailbox send register test\n");
722 } else {
723 /* Flag a successful rval */
724 rval = QLA_SUCCESS;
725 }
726
727 return rval;
728}
729
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700730void
Andrew Vasquez01071092005-07-06 10:31:37 -0700731qla2x00_alloc_fw_dump(scsi_qla_host_t *ha)
732{
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700733 int rval;
734 uint32_t dump_size, fixed_size, mem_size, req_q_size, rsp_q_size,
735 eft_size;
736 dma_addr_t eft_dma;
737 void *eft;
Andrew Vasquezd4e3e042006-05-17 15:09:50 -0700738
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700739 if (ha->fw_dump) {
740 qla_printk(KERN_WARNING, ha,
741 "Firmware dump previously allocated.\n");
742 return;
Andrew Vasquezd4e3e042006-05-17 15:09:50 -0700743 }
744
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700745 ha->fw_dumped = 0;
746 fixed_size = mem_size = eft_size = 0;
747 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
748 fixed_size = sizeof(struct qla2100_fw_dump);
749 } else if (IS_QLA23XX(ha)) {
750 fixed_size = offsetof(struct qla2300_fw_dump, data_ram);
751 mem_size = (ha->fw_memory_size - 0x11000 + 1) *
752 sizeof(uint16_t);
Andrew Vasqueze4289242007-07-19 15:05:56 -0700753 } else if (IS_FWI2_CAPABLE(ha)) {
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -0700754 fixed_size = IS_QLA25XX(ha) ?
755 offsetof(struct qla25xx_fw_dump, ext_mem):
756 offsetof(struct qla24xx_fw_dump, ext_mem);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700757 mem_size = (ha->fw_memory_size - 0x100000 + 1) *
758 sizeof(uint32_t);
759
760 /* Allocate memory for Extended Trace Buffer. */
761 eft = dma_alloc_coherent(&ha->pdev->dev, EFT_SIZE, &eft_dma,
762 GFP_KERNEL);
763 if (!eft) {
764 qla_printk(KERN_WARNING, ha, "Unable to allocate "
765 "(%d KB) for EFT.\n", EFT_SIZE / 1024);
766 goto cont_alloc;
767 }
768
769 rval = qla2x00_trace_control(ha, TC_ENABLE, eft_dma,
770 EFT_NUM_BUFFERS);
771 if (rval) {
772 qla_printk(KERN_WARNING, ha, "Unable to initialize "
773 "EFT (%d).\n", rval);
774 dma_free_coherent(&ha->pdev->dev, EFT_SIZE, eft,
775 eft_dma);
776 goto cont_alloc;
777 }
778
779 qla_printk(KERN_INFO, ha, "Allocated (%d KB) for EFT...\n",
780 EFT_SIZE / 1024);
781
782 eft_size = EFT_SIZE;
783 memset(eft, 0, eft_size);
784 ha->eft_dma = eft_dma;
785 ha->eft = eft;
786 }
787cont_alloc:
788 req_q_size = ha->request_q_length * sizeof(request_t);
789 rsp_q_size = ha->response_q_length * sizeof(response_t);
790
791 dump_size = offsetof(struct qla2xxx_fw_dump, isp);
792 dump_size += fixed_size + mem_size + req_q_size + rsp_q_size +
793 eft_size;
794
Andrew Vasquezd4e3e042006-05-17 15:09:50 -0700795 ha->fw_dump = vmalloc(dump_size);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700796 if (!ha->fw_dump) {
Andrew Vasquez01071092005-07-06 10:31:37 -0700797 qla_printk(KERN_WARNING, ha, "Unable to allocate (%d KB) for "
Andrew Vasquezd4e3e042006-05-17 15:09:50 -0700798 "firmware dump!!!\n", dump_size / 1024);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700799
800 if (ha->eft) {
801 dma_free_coherent(&ha->pdev->dev, eft_size, ha->eft,
802 ha->eft_dma);
803 ha->eft = NULL;
804 ha->eft_dma = 0;
805 }
806 return;
807 }
808
809 qla_printk(KERN_INFO, ha, "Allocated (%d KB) for firmware dump...\n",
810 dump_size / 1024);
811
812 ha->fw_dump_len = dump_size;
813 ha->fw_dump->signature[0] = 'Q';
814 ha->fw_dump->signature[1] = 'L';
815 ha->fw_dump->signature[2] = 'G';
816 ha->fw_dump->signature[3] = 'C';
817 ha->fw_dump->version = __constant_htonl(1);
818
819 ha->fw_dump->fixed_size = htonl(fixed_size);
820 ha->fw_dump->mem_size = htonl(mem_size);
821 ha->fw_dump->req_q_size = htonl(req_q_size);
822 ha->fw_dump->rsp_q_size = htonl(rsp_q_size);
823
824 ha->fw_dump->eft_size = htonl(eft_size);
825 ha->fw_dump->eft_addr_l = htonl(LSD(ha->eft_dma));
826 ha->fw_dump->eft_addr_h = htonl(MSD(ha->eft_dma));
827
828 ha->fw_dump->header_size =
829 htonl(offsetof(struct qla2xxx_fw_dump, isp));
Andrew Vasquez01071092005-07-06 10:31:37 -0700830}
831
832/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 * qla2x00_resize_request_q() - Resize request queue given available ISP memory.
834 * @ha: HA context
835 *
836 * Returns 0 on success.
837 */
838static void
839qla2x00_resize_request_q(scsi_qla_host_t *ha)
840{
841 int rval;
842 uint16_t fw_iocb_cnt = 0;
843 uint16_t request_q_length = REQUEST_ENTRY_CNT_2XXX_EXT_MEM;
844 dma_addr_t request_dma;
845 request_t *request_ring;
846
847 /* Valid only on recent ISPs. */
848 if (IS_QLA2100(ha) || IS_QLA2200(ha))
849 return;
850
851 /* Retrieve IOCB counts available to the firmware. */
852 rval = qla2x00_get_resource_cnts(ha, NULL, NULL, NULL, &fw_iocb_cnt);
853 if (rval)
854 return;
855 /* No point in continuing if current settings are sufficient. */
856 if (fw_iocb_cnt < 1024)
857 return;
858 if (ha->request_q_length >= request_q_length)
859 return;
860
861 /* Attempt to claim larger area for request queue. */
862 request_ring = dma_alloc_coherent(&ha->pdev->dev,
863 (request_q_length + 1) * sizeof(request_t), &request_dma,
864 GFP_KERNEL);
865 if (request_ring == NULL)
866 return;
867
868 /* Resize successful, report extensions. */
869 qla_printk(KERN_INFO, ha, "Extended memory detected (%d KB)...\n",
870 (ha->fw_memory_size + 1) / 1024);
871 qla_printk(KERN_INFO, ha, "Resizing request queue depth "
872 "(%d -> %d)...\n", ha->request_q_length, request_q_length);
873
874 /* Clear old allocations. */
875 dma_free_coherent(&ha->pdev->dev,
876 (ha->request_q_length + 1) * sizeof(request_t), ha->request_ring,
877 ha->request_dma);
878
879 /* Begin using larger queue. */
880 ha->request_q_length = request_q_length;
881 ha->request_ring = request_ring;
882 ha->request_dma = request_dma;
883}
884
885/**
886 * qla2x00_setup_chip() - Load and start RISC firmware.
887 * @ha: HA context
888 *
889 * Returns 0 on success.
890 */
891static int
892qla2x00_setup_chip(scsi_qla_host_t *ha)
893{
Andrew Vasquez01071092005-07-06 10:31:37 -0700894 int rval;
895 uint32_t srisc_address = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 /* Load firmware sequences */
Andrew Vasquezfd34f552007-07-19 15:06:00 -0700898 rval = ha->isp_ops->load_risc(ha, &srisc_address);
Andrew Vasquez01071092005-07-06 10:31:37 -0700899 if (rval == QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 DEBUG(printk("scsi(%ld): Verifying Checksum of loaded RISC "
901 "code.\n", ha->host_no));
902
Andrew Vasquez01071092005-07-06 10:31:37 -0700903 rval = qla2x00_verify_checksum(ha, srisc_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 if (rval == QLA_SUCCESS) {
905 /* Start firmware execution. */
906 DEBUG(printk("scsi(%ld): Checksum OK, start "
907 "firmware.\n", ha->host_no));
908
Andrew Vasquez01071092005-07-06 10:31:37 -0700909 rval = qla2x00_execute_fw(ha, srisc_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 /* Retrieve firmware information. */
911 if (rval == QLA_SUCCESS && ha->fw_major_version == 0) {
912 qla2x00_get_fw_version(ha,
913 &ha->fw_major_version,
914 &ha->fw_minor_version,
915 &ha->fw_subminor_version,
916 &ha->fw_attributes, &ha->fw_memory_size);
917 qla2x00_resize_request_q(ha);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700918 ha->flags.npiv_supported = 0;
919 if (IS_QLA24XX(ha) &&
920 (ha->fw_attributes & BIT_2))
921 ha->flags.npiv_supported = 1;
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700922
923 if (ql2xallocfwdump)
924 qla2x00_alloc_fw_dump(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
926 } else {
927 DEBUG2(printk(KERN_INFO
928 "scsi(%ld): ISP Firmware failed checksum.\n",
929 ha->host_no));
930 }
931 }
932
933 if (rval) {
934 DEBUG2_3(printk("scsi(%ld): Setup chip **** FAILED ****.\n",
935 ha->host_no));
936 }
937
938 return (rval);
939}
940
941/**
942 * qla2x00_init_response_q_entries() - Initializes response queue entries.
943 * @ha: HA context
944 *
945 * Beginning of request ring has initialization control block already built
946 * by nvram config routine.
947 *
948 * Returns 0 on success.
949 */
950static void
951qla2x00_init_response_q_entries(scsi_qla_host_t *ha)
952{
953 uint16_t cnt;
954 response_t *pkt;
955
956 pkt = ha->response_ring_ptr;
957 for (cnt = 0; cnt < ha->response_q_length; cnt++) {
958 pkt->signature = RESPONSE_PROCESSED;
959 pkt++;
960 }
961
962}
963
964/**
965 * qla2x00_update_fw_options() - Read and process firmware options.
966 * @ha: HA context
967 *
968 * Returns 0 on success.
969 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700970void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971qla2x00_update_fw_options(scsi_qla_host_t *ha)
972{
973 uint16_t swing, emphasis, tx_sens, rx_sens;
974
975 memset(ha->fw_options, 0, sizeof(ha->fw_options));
976 qla2x00_get_fw_options(ha, ha->fw_options);
977
978 if (IS_QLA2100(ha) || IS_QLA2200(ha))
979 return;
980
981 /* Serial Link options. */
982 DEBUG3(printk("scsi(%ld): Serial link options:\n",
983 ha->host_no));
984 DEBUG3(qla2x00_dump_buffer((uint8_t *)&ha->fw_seriallink_options,
985 sizeof(ha->fw_seriallink_options)));
986
987 ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
988 if (ha->fw_seriallink_options[3] & BIT_2) {
989 ha->fw_options[1] |= FO1_SET_EMPHASIS_SWING;
990
991 /* 1G settings */
992 swing = ha->fw_seriallink_options[2] & (BIT_2 | BIT_1 | BIT_0);
993 emphasis = (ha->fw_seriallink_options[2] &
994 (BIT_4 | BIT_3)) >> 3;
995 tx_sens = ha->fw_seriallink_options[0] &
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700996 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 rx_sens = (ha->fw_seriallink_options[0] &
998 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
999 ha->fw_options[10] = (emphasis << 14) | (swing << 8);
1000 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
1001 if (rx_sens == 0x0)
1002 rx_sens = 0x3;
1003 ha->fw_options[10] |= (tx_sens << 4) | rx_sens;
1004 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
1005 ha->fw_options[10] |= BIT_5 |
1006 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
1007 (tx_sens & (BIT_1 | BIT_0));
1008
1009 /* 2G settings */
1010 swing = (ha->fw_seriallink_options[2] &
1011 (BIT_7 | BIT_6 | BIT_5)) >> 5;
1012 emphasis = ha->fw_seriallink_options[3] & (BIT_1 | BIT_0);
1013 tx_sens = ha->fw_seriallink_options[1] &
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001014 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 rx_sens = (ha->fw_seriallink_options[1] &
1016 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
1017 ha->fw_options[11] = (emphasis << 14) | (swing << 8);
1018 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
1019 if (rx_sens == 0x0)
1020 rx_sens = 0x3;
1021 ha->fw_options[11] |= (tx_sens << 4) | rx_sens;
1022 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
1023 ha->fw_options[11] |= BIT_5 |
1024 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
1025 (tx_sens & (BIT_1 | BIT_0));
1026 }
1027
1028 /* FCP2 options. */
1029 /* Return command IOCBs without waiting for an ABTS to complete. */
1030 ha->fw_options[3] |= BIT_13;
1031
1032 /* LED scheme. */
1033 if (ha->flags.enable_led_scheme)
1034 ha->fw_options[2] |= BIT_12;
1035
andrew.vasquez@qlogic.com48c02fd2006-03-09 14:27:18 -08001036 /* Detect ISP6312. */
1037 if (IS_QLA6312(ha))
1038 ha->fw_options[2] |= BIT_13;
1039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 /* Update firmware options. */
1041 qla2x00_set_fw_options(ha, ha->fw_options);
1042}
1043
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001044void
Andrew Vasquez01071092005-07-06 10:31:37 -07001045qla24xx_update_fw_options(scsi_qla_host_t *ha)
1046{
1047 int rval;
1048
1049 /* Update Serial Link options. */
andrew.vasquez@qlogic.comf94097e2006-01-13 17:05:32 -08001050 if ((le16_to_cpu(ha->fw_seriallink_options24[0]) & BIT_0) == 0)
Andrew Vasquez01071092005-07-06 10:31:37 -07001051 return;
1052
andrew.vasquez@qlogic.comf94097e2006-01-13 17:05:32 -08001053 rval = qla2x00_set_serdes_params(ha,
1054 le16_to_cpu(ha->fw_seriallink_options24[1]),
1055 le16_to_cpu(ha->fw_seriallink_options24[2]),
1056 le16_to_cpu(ha->fw_seriallink_options24[3]));
Andrew Vasquez01071092005-07-06 10:31:37 -07001057 if (rval != QLA_SUCCESS) {
1058 qla_printk(KERN_WARNING, ha,
1059 "Unable to update Serial Link options (%x).\n", rval);
1060 }
1061}
1062
1063void
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001064qla2x00_config_rings(struct scsi_qla_host *ha)
1065{
Andrew Vasquez3d716442005-07-06 10:30:26 -07001066 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001067
1068 /* Setup ring parameters in initialization control block. */
1069 ha->init_cb->request_q_outpointer = __constant_cpu_to_le16(0);
1070 ha->init_cb->response_q_inpointer = __constant_cpu_to_le16(0);
1071 ha->init_cb->request_q_length = cpu_to_le16(ha->request_q_length);
1072 ha->init_cb->response_q_length = cpu_to_le16(ha->response_q_length);
1073 ha->init_cb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
1074 ha->init_cb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
1075 ha->init_cb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
1076 ha->init_cb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
1077
1078 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), 0);
1079 WRT_REG_WORD(ISP_REQ_Q_OUT(ha, reg), 0);
1080 WRT_REG_WORD(ISP_RSP_Q_IN(ha, reg), 0);
1081 WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), 0);
1082 RD_REG_WORD(ISP_RSP_Q_OUT(ha, reg)); /* PCI Posting. */
1083}
1084
Andrew Vasquez01071092005-07-06 10:31:37 -07001085void
1086qla24xx_config_rings(struct scsi_qla_host *ha)
1087{
1088 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1089 struct init_cb_24xx *icb;
1090
1091 /* Setup ring parameters in initialization control block. */
1092 icb = (struct init_cb_24xx *)ha->init_cb;
1093 icb->request_q_outpointer = __constant_cpu_to_le16(0);
1094 icb->response_q_inpointer = __constant_cpu_to_le16(0);
1095 icb->request_q_length = cpu_to_le16(ha->request_q_length);
1096 icb->response_q_length = cpu_to_le16(ha->response_q_length);
1097 icb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
1098 icb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
1099 icb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
1100 icb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
1101
1102 WRT_REG_DWORD(&reg->req_q_in, 0);
1103 WRT_REG_DWORD(&reg->req_q_out, 0);
1104 WRT_REG_DWORD(&reg->rsp_q_in, 0);
1105 WRT_REG_DWORD(&reg->rsp_q_out, 0);
1106 RD_REG_DWORD(&reg->rsp_q_out);
1107}
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109/**
1110 * qla2x00_init_rings() - Initializes firmware.
1111 * @ha: HA context
1112 *
1113 * Beginning of request ring has initialization control block already built
1114 * by nvram config routine.
1115 *
1116 * Returns 0 on success.
1117 */
1118static int
1119qla2x00_init_rings(scsi_qla_host_t *ha)
1120{
1121 int rval;
1122 unsigned long flags = 0;
1123 int cnt;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001124 struct mid_init_cb_24xx *mid_init_cb =
1125 (struct mid_init_cb_24xx *) ha->init_cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 spin_lock_irqsave(&ha->hardware_lock, flags);
1128
1129 /* Clear outstanding commands array. */
1130 for (cnt = 0; cnt < MAX_OUTSTANDING_COMMANDS; cnt++)
1131 ha->outstanding_cmds[cnt] = NULL;
1132
1133 ha->current_outstanding_cmd = 0;
1134
1135 /* Clear RSCN queue. */
1136 ha->rscn_in_ptr = 0;
1137 ha->rscn_out_ptr = 0;
1138
1139 /* Initialize firmware. */
1140 ha->request_ring_ptr = ha->request_ring;
1141 ha->req_ring_index = 0;
1142 ha->req_q_cnt = ha->request_q_length;
1143 ha->response_ring_ptr = ha->response_ring;
1144 ha->rsp_ring_index = 0;
1145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 /* Initialize response queue entries */
1147 qla2x00_init_response_q_entries(ha);
1148
Andrew Vasquezfd34f552007-07-19 15:06:00 -07001149 ha->isp_ops->config_rings(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
1151 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1152
1153 /* Update any ISP specific firmware options before initialization. */
Andrew Vasquezfd34f552007-07-19 15:06:00 -07001154 ha->isp_ops->update_fw_options(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 DEBUG(printk("scsi(%ld): Issue init firmware.\n", ha->host_no));
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001157
1158 mid_init_cb->count = MAX_NUM_VPORT_FABRIC;
1159 ha->max_npiv_vports = MAX_NUM_VPORT_FABRIC;
1160
Andrew Vasquez01071092005-07-06 10:31:37 -07001161 rval = qla2x00_init_firmware(ha, ha->init_cb_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 if (rval) {
1163 DEBUG2_3(printk("scsi(%ld): Init firmware **** FAILED ****.\n",
1164 ha->host_no));
1165 } else {
1166 DEBUG3(printk("scsi(%ld): Init firmware -- success.\n",
1167 ha->host_no));
1168 }
1169
1170 return (rval);
1171}
1172
1173/**
1174 * qla2x00_fw_ready() - Waits for firmware ready.
1175 * @ha: HA context
1176 *
1177 * Returns 0 on success.
1178 */
1179static int
1180qla2x00_fw_ready(scsi_qla_host_t *ha)
1181{
1182 int rval;
1183 unsigned long wtime, mtime;
1184 uint16_t min_wait; /* Minimum wait time if loop is down */
1185 uint16_t wait_time; /* Wait time if loop is coming ready */
1186 uint16_t fw_state;
1187
1188 rval = QLA_SUCCESS;
1189
1190 /* 20 seconds for loop down. */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001191 min_wait = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
1193 /*
1194 * Firmware should take at most one RATOV to login, plus 5 seconds for
1195 * our own processing.
1196 */
1197 if ((wait_time = (ha->retry_count*ha->login_timeout) + 5) < min_wait) {
1198 wait_time = min_wait;
1199 }
1200
1201 /* Min wait time if loop down */
1202 mtime = jiffies + (min_wait * HZ);
1203
1204 /* wait time before firmware ready */
1205 wtime = jiffies + (wait_time * HZ);
1206
1207 /* Wait for ISP to finish LIP */
1208 if (!ha->flags.init_done)
1209 qla_printk(KERN_INFO, ha, "Waiting for LIP to complete...\n");
1210
1211 DEBUG3(printk("scsi(%ld): Waiting for LIP to complete...\n",
1212 ha->host_no));
1213
1214 do {
1215 rval = qla2x00_get_firmware_state(ha, &fw_state);
1216 if (rval == QLA_SUCCESS) {
1217 if (fw_state < FSTATE_LOSS_OF_SYNC) {
1218 ha->device_flags &= ~DFLG_NO_CABLE;
1219 }
1220 if (fw_state == FSTATE_READY) {
1221 DEBUG(printk("scsi(%ld): F/W Ready - OK \n",
1222 ha->host_no));
1223
1224 qla2x00_get_retry_cnt(ha, &ha->retry_count,
1225 &ha->login_timeout, &ha->r_a_tov);
1226
1227 rval = QLA_SUCCESS;
1228 break;
1229 }
1230
1231 rval = QLA_FUNCTION_FAILED;
1232
1233 if (atomic_read(&ha->loop_down_timer) &&
Andrew Vasquez7d7abc72006-06-23 16:11:17 -07001234 fw_state != FSTATE_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 /* Loop down. Timeout on min_wait for states
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001236 * other than Wait for Login.
1237 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 if (time_after_eq(jiffies, mtime)) {
1239 qla_printk(KERN_INFO, ha,
1240 "Cable is unplugged...\n");
1241
1242 ha->device_flags |= DFLG_NO_CABLE;
1243 break;
1244 }
1245 }
1246 } else {
1247 /* Mailbox cmd failed. Timeout on min_wait. */
1248 if (time_after_eq(jiffies, mtime))
1249 break;
1250 }
1251
1252 if (time_after_eq(jiffies, wtime))
1253 break;
1254
1255 /* Delay for a while */
1256 msleep(500);
1257
1258 DEBUG3(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
1259 ha->host_no, fw_state, jiffies));
1260 } while (1);
1261
1262 DEBUG(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
1263 ha->host_no, fw_state, jiffies));
1264
1265 if (rval) {
1266 DEBUG2_3(printk("scsi(%ld): Firmware ready **** FAILED ****.\n",
1267 ha->host_no));
1268 }
1269
1270 return (rval);
1271}
1272
1273/*
1274* qla2x00_configure_hba
1275* Setup adapter context.
1276*
1277* Input:
1278* ha = adapter state pointer.
1279*
1280* Returns:
1281* 0 = success
1282*
1283* Context:
1284* Kernel context.
1285*/
1286static int
1287qla2x00_configure_hba(scsi_qla_host_t *ha)
1288{
1289 int rval;
1290 uint16_t loop_id;
1291 uint16_t topo;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001292 uint16_t sw_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 uint8_t al_pa;
1294 uint8_t area;
1295 uint8_t domain;
1296 char connect_type[22];
1297
1298 /* Get host addresses. */
1299 rval = qla2x00_get_adapter_id(ha,
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001300 &loop_id, &al_pa, &area, &domain, &topo, &sw_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 if (rval != QLA_SUCCESS) {
Andrew Vasquez23443b12005-12-06 10:57:06 -08001302 if (LOOP_TRANSITION(ha) || atomic_read(&ha->loop_down_timer) ||
Ravi Anand33135aa2005-11-08 14:37:20 -08001303 (rval == QLA_COMMAND_ERROR && loop_id == 0x7)) {
1304 DEBUG2(printk("%s(%ld) Loop is in a transition state\n",
1305 __func__, ha->host_no));
1306 } else {
1307 qla_printk(KERN_WARNING, ha,
1308 "ERROR -- Unable to get host loop ID.\n");
1309 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
1310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 return (rval);
1312 }
1313
1314 if (topo == 4) {
1315 qla_printk(KERN_INFO, ha,
1316 "Cannot get topology - retrying.\n");
1317 return (QLA_FUNCTION_FAILED);
1318 }
1319
1320 ha->loop_id = loop_id;
1321
1322 /* initialize */
1323 ha->min_external_loopid = SNS_FIRST_LOOP_ID;
1324 ha->operating_mode = LOOP;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001325 ha->switch_cap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327 switch (topo) {
1328 case 0:
1329 DEBUG3(printk("scsi(%ld): HBA in NL topology.\n",
1330 ha->host_no));
1331 ha->current_topology = ISP_CFG_NL;
1332 strcpy(connect_type, "(Loop)");
1333 break;
1334
1335 case 1:
1336 DEBUG3(printk("scsi(%ld): HBA in FL topology.\n",
1337 ha->host_no));
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001338 ha->switch_cap = sw_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 ha->current_topology = ISP_CFG_FL;
1340 strcpy(connect_type, "(FL_Port)");
1341 break;
1342
1343 case 2:
1344 DEBUG3(printk("scsi(%ld): HBA in N P2P topology.\n",
1345 ha->host_no));
1346 ha->operating_mode = P2P;
1347 ha->current_topology = ISP_CFG_N;
1348 strcpy(connect_type, "(N_Port-to-N_Port)");
1349 break;
1350
1351 case 3:
1352 DEBUG3(printk("scsi(%ld): HBA in F P2P topology.\n",
1353 ha->host_no));
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001354 ha->switch_cap = sw_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 ha->operating_mode = P2P;
1356 ha->current_topology = ISP_CFG_F;
1357 strcpy(connect_type, "(F_Port)");
1358 break;
1359
1360 default:
1361 DEBUG3(printk("scsi(%ld): HBA in unknown topology %x. "
1362 "Using NL.\n",
1363 ha->host_no, topo));
1364 ha->current_topology = ISP_CFG_NL;
1365 strcpy(connect_type, "(Loop)");
1366 break;
1367 }
1368
1369 /* Save Host port and loop ID. */
1370 /* byte order - Big Endian */
1371 ha->d_id.b.domain = domain;
1372 ha->d_id.b.area = area;
1373 ha->d_id.b.al_pa = al_pa;
1374
1375 if (!ha->flags.init_done)
1376 qla_printk(KERN_INFO, ha,
1377 "Topology - %s, Host Loop address 0x%x\n",
1378 connect_type, ha->loop_id);
1379
1380 if (rval) {
1381 DEBUG2_3(printk("scsi(%ld): FAILED.\n", ha->host_no));
1382 } else {
1383 DEBUG3(printk("scsi(%ld): exiting normally.\n", ha->host_no));
1384 }
1385
1386 return(rval);
1387}
1388
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08001389static inline void
1390qla2x00_set_model_info(scsi_qla_host_t *ha, uint8_t *model, size_t len, char *def)
1391{
1392 char *st, *en;
1393 uint16_t index;
1394
1395 if (memcmp(model, BINZERO, len) != 0) {
1396 strncpy(ha->model_number, model, len);
1397 st = en = ha->model_number;
1398 en += len - 1;
1399 while (en > st) {
1400 if (*en != 0x20 && *en != 0x00)
1401 break;
1402 *en-- = '\0';
1403 }
1404
1405 index = (ha->pdev->subsystem_device & 0xff);
1406 if (ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
1407 index < QLA_MODEL_NAMES)
1408 ha->model_desc = qla2x00_model_name[index * 2 + 1];
1409 } else {
1410 index = (ha->pdev->subsystem_device & 0xff);
1411 if (ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
1412 index < QLA_MODEL_NAMES) {
1413 strcpy(ha->model_number,
1414 qla2x00_model_name[index * 2]);
1415 ha->model_desc = qla2x00_model_name[index * 2 + 1];
1416 } else {
1417 strcpy(ha->model_number, def);
1418 }
1419 }
1420}
1421
David Miller4e08df32007-04-16 12:37:43 -07001422/* On sparc systems, obtain port and node WWN from firmware
1423 * properties.
1424 */
1425static void qla2xxx_nvram_wwn_from_ofw(scsi_qla_host_t *ha, nvram_t *nv)
1426{
1427#ifdef CONFIG_SPARC
1428 struct pci_dev *pdev = ha->pdev;
David S. Miller15576bc2007-05-08 00:36:49 -07001429 struct device_node *dp = pci_device_to_OF_node(pdev);
1430 const u8 *val;
David Miller4e08df32007-04-16 12:37:43 -07001431 int len;
1432
1433 val = of_get_property(dp, "port-wwn", &len);
1434 if (val && len >= WWN_SIZE)
1435 memcpy(nv->port_name, val, WWN_SIZE);
1436
1437 val = of_get_property(dp, "node-wwn", &len);
1438 if (val && len >= WWN_SIZE)
1439 memcpy(nv->node_name, val, WWN_SIZE);
1440#endif
1441}
1442
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443/*
1444* NVRAM configuration for ISP 2xxx
1445*
1446* Input:
1447* ha = adapter block pointer.
1448*
1449* Output:
1450* initialization control block in response_ring
1451* host adapters parameters in host adapter block
1452*
1453* Returns:
1454* 0 = success.
1455*/
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001456int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457qla2x00_nvram_config(scsi_qla_host_t *ha)
1458{
David Miller4e08df32007-04-16 12:37:43 -07001459 int rval;
Andrew Vasquez01071092005-07-06 10:31:37 -07001460 uint8_t chksum = 0;
1461 uint16_t cnt;
1462 uint8_t *dptr1, *dptr2;
1463 init_cb_t *icb = ha->init_cb;
1464 nvram_t *nv = (nvram_t *)ha->request_ring;
1465 uint8_t *ptr = (uint8_t *)ha->request_ring;
Andrew Vasquez3d716442005-07-06 10:30:26 -07001466 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
David Miller4e08df32007-04-16 12:37:43 -07001468 rval = QLA_SUCCESS;
1469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 /* Determine NVRAM starting address. */
Andrew Vasquez01071092005-07-06 10:31:37 -07001471 ha->nvram_size = sizeof(nvram_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 ha->nvram_base = 0;
1473 if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha))
1474 if ((RD_REG_WORD(&reg->ctrl_status) >> 14) == 1)
1475 ha->nvram_base = 0x80;
1476
1477 /* Get NVRAM data and calculate checksum. */
Andrew Vasquezfd34f552007-07-19 15:06:00 -07001478 ha->isp_ops->read_nvram(ha, ptr, ha->nvram_base, ha->nvram_size);
Andrew Vasquez01071092005-07-06 10:31:37 -07001479 for (cnt = 0, chksum = 0; cnt < ha->nvram_size; cnt++)
1480 chksum += *ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
1482 DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
1483 DEBUG5(qla2x00_dump_buffer((uint8_t *)ha->request_ring,
Andrew Vasquez01071092005-07-06 10:31:37 -07001484 ha->nvram_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
1486 /* Bad NVRAM data, set defaults parameters. */
1487 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' ||
1488 nv->id[2] != 'P' || nv->id[3] != ' ' || nv->nvram_version < 1) {
1489 /* Reset NVRAM data. */
1490 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
1491 "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
1492 nv->nvram_version);
David Miller4e08df32007-04-16 12:37:43 -07001493 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
1494 "invalid -- WWPN) defaults.\n");
1495
1496 /*
1497 * Set default initialization control block.
1498 */
1499 memset(nv, 0, ha->nvram_size);
1500 nv->parameter_block_version = ICB_VERSION;
1501
1502 if (IS_QLA23XX(ha)) {
1503 nv->firmware_options[0] = BIT_2 | BIT_1;
1504 nv->firmware_options[1] = BIT_7 | BIT_5;
1505 nv->add_firmware_options[0] = BIT_5;
1506 nv->add_firmware_options[1] = BIT_5 | BIT_4;
1507 nv->frame_payload_size = __constant_cpu_to_le16(2048);
1508 nv->special_options[1] = BIT_7;
1509 } else if (IS_QLA2200(ha)) {
1510 nv->firmware_options[0] = BIT_2 | BIT_1;
1511 nv->firmware_options[1] = BIT_7 | BIT_5;
1512 nv->add_firmware_options[0] = BIT_5;
1513 nv->add_firmware_options[1] = BIT_5 | BIT_4;
1514 nv->frame_payload_size = __constant_cpu_to_le16(1024);
1515 } else if (IS_QLA2100(ha)) {
1516 nv->firmware_options[0] = BIT_3 | BIT_1;
1517 nv->firmware_options[1] = BIT_5;
1518 nv->frame_payload_size = __constant_cpu_to_le16(1024);
1519 }
1520
1521 nv->max_iocb_allocation = __constant_cpu_to_le16(256);
1522 nv->execution_throttle = __constant_cpu_to_le16(16);
1523 nv->retry_count = 8;
1524 nv->retry_delay = 1;
1525
1526 nv->port_name[0] = 33;
1527 nv->port_name[3] = 224;
1528 nv->port_name[4] = 139;
1529
1530 qla2xxx_nvram_wwn_from_ofw(ha, nv);
1531
1532 nv->login_timeout = 4;
1533
1534 /*
1535 * Set default host adapter parameters
1536 */
1537 nv->host_p[1] = BIT_2;
1538 nv->reset_delay = 5;
1539 nv->port_down_retry_count = 8;
1540 nv->max_luns_per_target = __constant_cpu_to_le16(8);
1541 nv->link_down_timeout = 60;
1542
1543 rval = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 }
1545
1546#if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
1547 /*
1548 * The SN2 does not provide BIOS emulation which means you can't change
1549 * potentially bogus BIOS settings. Force the use of default settings
1550 * for link rate and frame size. Hope that the rest of the settings
1551 * are valid.
1552 */
1553 if (ia64_platform_is("sn2")) {
1554 nv->frame_payload_size = __constant_cpu_to_le16(2048);
1555 if (IS_QLA23XX(ha))
1556 nv->special_options[1] = BIT_7;
1557 }
1558#endif
1559
1560 /* Reset Initialization control block */
Andrew Vasquez01071092005-07-06 10:31:37 -07001561 memset(icb, 0, ha->init_cb_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
1563 /*
1564 * Setup driver NVRAM options.
1565 */
1566 nv->firmware_options[0] |= (BIT_6 | BIT_1);
1567 nv->firmware_options[0] &= ~(BIT_5 | BIT_4);
1568 nv->firmware_options[1] |= (BIT_5 | BIT_0);
1569 nv->firmware_options[1] &= ~BIT_4;
1570
1571 if (IS_QLA23XX(ha)) {
1572 nv->firmware_options[0] |= BIT_2;
1573 nv->firmware_options[0] &= ~BIT_3;
Andrew Vasquez01071092005-07-06 10:31:37 -07001574 nv->add_firmware_options[1] |= BIT_5 | BIT_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
1576 if (IS_QLA2300(ha)) {
1577 if (ha->fb_rev == FPM_2310) {
1578 strcpy(ha->model_number, "QLA2310");
1579 } else {
1580 strcpy(ha->model_number, "QLA2300");
1581 }
1582 } else {
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08001583 qla2x00_set_model_info(ha, nv->model_number,
1584 sizeof(nv->model_number), "QLA23xx");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 }
1586 } else if (IS_QLA2200(ha)) {
1587 nv->firmware_options[0] |= BIT_2;
1588 /*
1589 * 'Point-to-point preferred, else loop' is not a safe
1590 * connection mode setting.
1591 */
1592 if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
1593 (BIT_5 | BIT_4)) {
1594 /* Force 'loop preferred, else point-to-point'. */
1595 nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
1596 nv->add_firmware_options[0] |= BIT_5;
1597 }
1598 strcpy(ha->model_number, "QLA22xx");
1599 } else /*if (IS_QLA2100(ha))*/ {
1600 strcpy(ha->model_number, "QLA2100");
1601 }
1602
1603 /*
1604 * Copy over NVRAM RISC parameter block to initialization control block.
1605 */
1606 dptr1 = (uint8_t *)icb;
1607 dptr2 = (uint8_t *)&nv->parameter_block_version;
1608 cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
1609 while (cnt--)
1610 *dptr1++ = *dptr2++;
1611
1612 /* Copy 2nd half. */
1613 dptr1 = (uint8_t *)icb->add_firmware_options;
1614 cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
1615 while (cnt--)
1616 *dptr1++ = *dptr2++;
1617
Andrew Vasquez5341e862006-05-17 15:09:16 -07001618 /* Use alternate WWN? */
1619 if (nv->host_p[1] & BIT_7) {
1620 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
1621 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
1622 }
1623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 /* Prepare nodename */
1625 if ((icb->firmware_options[1] & BIT_6) == 0) {
1626 /*
1627 * Firmware will apply the following mask if the nodename was
1628 * not provided.
1629 */
1630 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
1631 icb->node_name[0] &= 0xF0;
1632 }
1633
1634 /*
1635 * Set host adapter parameters.
1636 */
Andrew Vasquez01819442006-06-23 16:11:10 -07001637 if (nv->host_p[0] & BIT_7)
Andrew Vasquez11010fe2006-10-06 09:54:59 -07001638 ql2xextended_error_logging = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
1640 /* Always load RISC code on non ISP2[12]00 chips. */
1641 if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
1642 ha->flags.disable_risc_code_load = 0;
1643 ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
1644 ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
1645 ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
Andrew Vasquez06c22bd2005-08-26 19:09:00 -07001646 ha->flags.enable_led_scheme = (nv->special_options[1] & BIT_4) ? 1 : 0;
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07001647 ha->flags.disable_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
1649 ha->operating_mode =
1650 (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
1651
1652 memcpy(ha->fw_seriallink_options, nv->seriallink_options,
1653 sizeof(ha->fw_seriallink_options));
1654
1655 /* save HBA serial number */
1656 ha->serial0 = icb->port_name[5];
1657 ha->serial1 = icb->port_name[6];
1658 ha->serial2 = icb->port_name[7];
Andrew Vasquez3d716442005-07-06 10:30:26 -07001659 ha->node_name = icb->node_name;
1660 ha->port_name = icb->port_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
1662 icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
1663
1664 ha->retry_count = nv->retry_count;
1665
1666 /* Set minimum login_timeout to 4 seconds. */
1667 if (nv->login_timeout < ql2xlogintimeout)
1668 nv->login_timeout = ql2xlogintimeout;
1669 if (nv->login_timeout < 4)
1670 nv->login_timeout = 4;
1671 ha->login_timeout = nv->login_timeout;
1672 icb->login_timeout = nv->login_timeout;
1673
1674 /* Set minimum RATOV to 200 tenths of a second. */
1675 ha->r_a_tov = 200;
1676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 ha->loop_reset_delay = nv->reset_delay;
1678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 /* Link Down Timeout = 0:
1680 *
1681 * When Port Down timer expires we will start returning
1682 * I/O's to OS with "DID_NO_CONNECT".
1683 *
1684 * Link Down Timeout != 0:
1685 *
1686 * The driver waits for the link to come up after link down
1687 * before returning I/Os to OS with "DID_NO_CONNECT".
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001688 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 if (nv->link_down_timeout == 0) {
1690 ha->loop_down_abort_time =
Andrew Vasquez 354d6b22005-04-23 02:47:27 -04001691 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 } else {
1693 ha->link_down_timeout = nv->link_down_timeout;
1694 ha->loop_down_abort_time =
1695 (LOOP_DOWN_TIME - ha->link_down_timeout);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 /*
1699 * Need enough time to try and get the port back.
1700 */
1701 ha->port_down_retry_count = nv->port_down_retry_count;
1702 if (qlport_down_retry)
1703 ha->port_down_retry_count = qlport_down_retry;
1704 /* Set login_retry_count */
1705 ha->login_retry_count = nv->retry_count;
1706 if (ha->port_down_retry_count == nv->port_down_retry_count &&
1707 ha->port_down_retry_count > 3)
1708 ha->login_retry_count = ha->port_down_retry_count;
1709 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
1710 ha->login_retry_count = ha->port_down_retry_count;
1711 if (ql2xloginretrycount)
1712 ha->login_retry_count = ql2xloginretrycount;
1713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 icb->lun_enables = __constant_cpu_to_le16(0);
1715 icb->command_resource_count = 0;
1716 icb->immediate_notify_resource_count = 0;
1717 icb->timeout = __constant_cpu_to_le16(0);
1718
1719 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
1720 /* Enable RIO */
1721 icb->firmware_options[0] &= ~BIT_3;
1722 icb->add_firmware_options[0] &=
1723 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
1724 icb->add_firmware_options[0] |= BIT_2;
1725 icb->response_accumulation_timer = 3;
1726 icb->interrupt_delay_timer = 5;
1727
1728 ha->flags.process_response_queue = 1;
1729 } else {
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001730 /* Enable ZIO. */
1731 if (!ha->flags.init_done) {
1732 ha->zio_mode = icb->add_firmware_options[0] &
1733 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
1734 ha->zio_timer = icb->interrupt_delay_timer ?
1735 icb->interrupt_delay_timer: 2;
1736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 icb->add_firmware_options[0] &=
1738 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001739 ha->flags.process_response_queue = 0;
1740 if (ha->zio_mode != QLA_ZIO_DISABLED) {
andrew.vasquez@qlogic.com4a59f712006-03-09 14:27:39 -08001741 ha->zio_mode = QLA_ZIO_MODE_6;
1742
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001743 DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer "
1744 "delay (%d us).\n", ha->host_no, ha->zio_mode,
1745 ha->zio_timer * 100));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 qla_printk(KERN_INFO, ha,
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001747 "ZIO mode %d enabled; timer delay (%d us).\n",
1748 ha->zio_mode, ha->zio_timer * 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001750 icb->add_firmware_options[0] |= (uint8_t)ha->zio_mode;
1751 icb->interrupt_delay_timer = (uint8_t)ha->zio_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 ha->flags.process_response_queue = 1;
1753 }
1754 }
1755
David Miller4e08df32007-04-16 12:37:43 -07001756 if (rval) {
1757 DEBUG2_3(printk(KERN_WARNING
1758 "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
1759 }
1760 return (rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761}
1762
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001763static void
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001764qla2x00_rport_del(void *data)
1765{
1766 fc_port_t *fcport = data;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08001767 struct fc_rport *rport;
1768 unsigned long flags;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001769
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08001770 spin_lock_irqsave(&fcport->rport_lock, flags);
1771 rport = fcport->drport;
1772 fcport->drport = NULL;
1773 spin_unlock_irqrestore(&fcport->rport_lock, flags);
1774 if (rport)
1775 fc_remote_port_delete(rport);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001776}
1777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778/**
1779 * qla2x00_alloc_fcport() - Allocate a generic fcport.
1780 * @ha: HA context
1781 * @flags: allocation flags
1782 *
1783 * Returns a pointer to the allocated fcport, or NULL, if none available.
1784 */
Adrian Bunk413975a2006-06-30 02:33:06 -07001785static fc_port_t *
Al Viroc53033f2005-10-21 03:22:08 -04001786qla2x00_alloc_fcport(scsi_qla_host_t *ha, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
1788 fc_port_t *fcport;
1789
1790 fcport = kmalloc(sizeof(fc_port_t), flags);
1791 if (fcport == NULL)
1792 return (fcport);
1793
1794 /* Setup fcport template structure. */
1795 memset(fcport, 0, sizeof (fc_port_t));
1796 fcport->ha = ha;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001797 fcport->vp_idx = ha->vp_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 fcport->port_type = FCT_UNKNOWN;
1799 fcport->loop_id = FC_NO_LOOP_ID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 atomic_set(&fcport->state, FCS_UNCONFIGURED);
1801 fcport->flags = FCF_RLC_SUPPORT;
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07001802 fcport->supported_classes = FC_COS_UNSPECIFIED;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08001803 spin_lock_init(&fcport->rport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
1805 return (fcport);
1806}
1807
1808/*
1809 * qla2x00_configure_loop
1810 * Updates Fibre Channel Device Database with what is actually on loop.
1811 *
1812 * Input:
1813 * ha = adapter block pointer.
1814 *
1815 * Returns:
1816 * 0 = success.
1817 * 1 = error.
1818 * 2 = database was full and device was not configured.
1819 */
1820static int
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001821qla2x00_configure_loop(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822{
1823 int rval;
1824 unsigned long flags, save_flags;
1825
1826 rval = QLA_SUCCESS;
1827
1828 /* Get Initiator ID */
1829 if (test_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags)) {
1830 rval = qla2x00_configure_hba(ha);
1831 if (rval != QLA_SUCCESS) {
1832 DEBUG(printk("scsi(%ld): Unable to configure HBA.\n",
1833 ha->host_no));
1834 return (rval);
1835 }
1836 }
1837
1838 save_flags = flags = ha->dpc_flags;
1839 DEBUG(printk("scsi(%ld): Configure loop -- dpc flags =0x%lx\n",
1840 ha->host_no, flags));
1841
1842 /*
1843 * If we have both an RSCN and PORT UPDATE pending then handle them
1844 * both at the same time.
1845 */
1846 clear_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1847 clear_bit(RSCN_UPDATE, &ha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
1849 /* Determine what we need to do */
1850 if (ha->current_topology == ISP_CFG_FL &&
1851 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1852
1853 ha->flags.rscn_queue_overflow = 1;
1854 set_bit(RSCN_UPDATE, &flags);
1855
1856 } else if (ha->current_topology == ISP_CFG_F &&
1857 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1858
1859 ha->flags.rscn_queue_overflow = 1;
1860 set_bit(RSCN_UPDATE, &flags);
1861 clear_bit(LOCAL_LOOP_UPDATE, &flags);
1862
Andrew Vasquez21333b42006-05-17 15:09:56 -07001863 } else if (ha->current_topology == ISP_CFG_N) {
1864 clear_bit(RSCN_UPDATE, &flags);
1865
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 } else if (!ha->flags.online ||
1867 (test_bit(ABORT_ISP_ACTIVE, &flags))) {
1868
1869 ha->flags.rscn_queue_overflow = 1;
1870 set_bit(RSCN_UPDATE, &flags);
1871 set_bit(LOCAL_LOOP_UPDATE, &flags);
1872 }
1873
1874 if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
1875 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1876 rval = QLA_FUNCTION_FAILED;
1877 } else {
1878 rval = qla2x00_configure_local_loop(ha);
1879 }
1880 }
1881
1882 if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
Andrew Vasquez23443b12005-12-06 10:57:06 -08001883 if (LOOP_TRANSITION(ha)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 rval = QLA_FUNCTION_FAILED;
1885 } else {
1886 rval = qla2x00_configure_fabric(ha);
1887 }
1888 }
1889
1890 if (rval == QLA_SUCCESS) {
1891 if (atomic_read(&ha->loop_down_timer) ||
1892 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1893 rval = QLA_FUNCTION_FAILED;
1894 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 atomic_set(&ha->loop_state, LOOP_READY);
1896
1897 DEBUG(printk("scsi(%ld): LOOP READY\n", ha->host_no));
1898 }
1899 }
1900
1901 if (rval) {
1902 DEBUG2_3(printk("%s(%ld): *** FAILED ***\n",
1903 __func__, ha->host_no));
1904 } else {
1905 DEBUG3(printk("%s: exiting normally\n", __func__));
1906 }
1907
1908 /* Restore state if a resync event occured during processing */
1909 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1910 if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
1911 set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1912 if (test_bit(RSCN_UPDATE, &save_flags))
1913 set_bit(RSCN_UPDATE, &ha->dpc_flags);
1914 }
1915
1916 return (rval);
1917}
1918
1919
1920
1921/*
1922 * qla2x00_configure_local_loop
1923 * Updates Fibre Channel Device Database with local loop devices.
1924 *
1925 * Input:
1926 * ha = adapter block pointer.
1927 *
1928 * Returns:
1929 * 0 = success.
1930 */
1931static int
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001932qla2x00_configure_local_loop(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933{
1934 int rval, rval2;
1935 int found_devs;
1936 int found;
1937 fc_port_t *fcport, *new_fcport;
1938
1939 uint16_t index;
1940 uint16_t entries;
1941 char *id_iter;
1942 uint16_t loop_id;
1943 uint8_t domain, area, al_pa;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001944 scsi_qla_host_t *pha = to_qla_parent(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
1946 found_devs = 0;
1947 new_fcport = NULL;
1948 entries = MAX_FIBRE_DEVICES;
1949
1950 DEBUG3(printk("scsi(%ld): Getting FCAL position map\n", ha->host_no));
1951 DEBUG3(qla2x00_get_fcal_position_map(ha, NULL));
1952
1953 /* Get list of logged in devices. */
1954 memset(ha->gid_list, 0, GID_LIST_SIZE);
1955 rval = qla2x00_get_id_list(ha, ha->gid_list, ha->gid_list_dma,
1956 &entries);
1957 if (rval != QLA_SUCCESS)
1958 goto cleanup_allocation;
1959
1960 DEBUG3(printk("scsi(%ld): Entries in ID list (%d)\n",
1961 ha->host_no, entries));
1962 DEBUG3(qla2x00_dump_buffer((uint8_t *)ha->gid_list,
1963 entries * sizeof(struct gid_list_info)));
1964
1965 /* Allocate temporary fcport for any new fcports discovered. */
1966 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
1967 if (new_fcport == NULL) {
1968 rval = QLA_MEMORY_ALLOC_FAILED;
1969 goto cleanup_allocation;
1970 }
1971 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
1972
1973 /*
1974 * Mark local devices that were present with FCF_DEVICE_LOST for now.
1975 */
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001976 list_for_each_entry(fcport, &pha->fcports, list) {
1977 if (fcport->vp_idx != ha->vp_idx)
1978 continue;
1979
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 if (atomic_read(&fcport->state) == FCS_ONLINE &&
1981 fcport->port_type != FCT_BROADCAST &&
1982 (fcport->flags & FCF_FABRIC_DEVICE) == 0) {
1983
1984 DEBUG(printk("scsi(%ld): Marking port lost, "
1985 "loop_id=0x%04x\n",
1986 ha->host_no, fcport->loop_id));
1987
1988 atomic_set(&fcport->state, FCS_DEVICE_LOST);
1989 fcport->flags &= ~FCF_FARP_DONE;
1990 }
1991 }
1992
1993 /* Add devices to port list. */
1994 id_iter = (char *)ha->gid_list;
1995 for (index = 0; index < entries; index++) {
1996 domain = ((struct gid_list_info *)id_iter)->domain;
1997 area = ((struct gid_list_info *)id_iter)->area;
1998 al_pa = ((struct gid_list_info *)id_iter)->al_pa;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001999 if (IS_QLA2100(ha) || IS_QLA2200(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 loop_id = (uint16_t)
2001 ((struct gid_list_info *)id_iter)->loop_id_2100;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002002 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 loop_id = le16_to_cpu(
2004 ((struct gid_list_info *)id_iter)->loop_id);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002005 id_iter += ha->gid_list_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
2007 /* Bypass reserved domain fields. */
2008 if ((domain & 0xf0) == 0xf0)
2009 continue;
2010
2011 /* Bypass if not same domain and area of adapter. */
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07002012 if (area && domain &&
2013 (area != ha->d_id.b.area || domain != ha->d_id.b.domain))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 continue;
2015
2016 /* Bypass invalid local loop ID. */
2017 if (loop_id > LAST_LOCAL_LOOP_ID)
2018 continue;
2019
2020 /* Fill in member data. */
2021 new_fcport->d_id.b.domain = domain;
2022 new_fcport->d_id.b.area = area;
2023 new_fcport->d_id.b.al_pa = al_pa;
2024 new_fcport->loop_id = loop_id;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002025 new_fcport->vp_idx = ha->vp_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 rval2 = qla2x00_get_port_database(ha, new_fcport, 0);
2027 if (rval2 != QLA_SUCCESS) {
2028 DEBUG2(printk("scsi(%ld): Failed to retrieve fcport "
2029 "information -- get_port_database=%x, "
2030 "loop_id=0x%04x\n",
2031 ha->host_no, rval2, new_fcport->loop_id));
andrew.vasquez@qlogic.comc9d02ac2006-01-13 17:05:26 -08002032 DEBUG2(printk("scsi(%ld): Scheduling resync...\n",
2033 ha->host_no));
2034 set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 continue;
2036 }
2037
2038 /* Check for matching device in port list. */
2039 found = 0;
2040 fcport = NULL;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002041 list_for_each_entry(fcport, &pha->fcports, list) {
2042 if (fcport->vp_idx != ha->vp_idx)
2043 continue;
2044
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 if (memcmp(new_fcport->port_name, fcport->port_name,
2046 WWN_SIZE))
2047 continue;
2048
2049 fcport->flags &= ~(FCF_FABRIC_DEVICE |
2050 FCF_PERSISTENT_BOUND);
2051 fcport->loop_id = new_fcport->loop_id;
2052 fcport->port_type = new_fcport->port_type;
2053 fcport->d_id.b24 = new_fcport->d_id.b24;
2054 memcpy(fcport->node_name, new_fcport->node_name,
2055 WWN_SIZE);
2056
2057 found++;
2058 break;
2059 }
2060
2061 if (!found) {
2062 /* New device, add to fcports list. */
2063 new_fcport->flags &= ~FCF_PERSISTENT_BOUND;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002064 if (ha->parent) {
2065 new_fcport->ha = ha;
2066 new_fcport->vp_idx = ha->vp_idx;
2067 list_add_tail(&new_fcport->vp_fcport,
2068 &ha->vp_fcports);
2069 }
2070 list_add_tail(&new_fcport->list, &pha->fcports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
2072 /* Allocate a new replacement fcport. */
2073 fcport = new_fcport;
2074 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2075 if (new_fcport == NULL) {
2076 rval = QLA_MEMORY_ALLOC_FAILED;
2077 goto cleanup_allocation;
2078 }
2079 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
2080 }
2081
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002082 /* Base iIDMA settings on HBA port speed. */
2083 switch (ha->link_data_rate) {
2084 case PORT_SPEED_1GB:
2085 fcport->fp_speed = cpu_to_be16(BIT_15);
2086 break;
2087 case PORT_SPEED_2GB:
2088 fcport->fp_speed = cpu_to_be16(BIT_14);
2089 break;
2090 case PORT_SPEED_4GB:
2091 fcport->fp_speed = cpu_to_be16(BIT_13);
2092 break;
2093 }
2094
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 qla2x00_update_fcport(ha, fcport);
2096
2097 found_devs++;
2098 }
2099
2100cleanup_allocation:
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002101 kfree(new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
2103 if (rval != QLA_SUCCESS) {
2104 DEBUG2(printk("scsi(%ld): Configure local loop error exit: "
2105 "rval=%x\n", ha->host_no, rval));
2106 }
2107
2108 if (found_devs) {
2109 ha->device_flags |= DFLG_LOCAL_DEVICES;
2110 ha->device_flags &= ~DFLG_RETRY_LOCAL_DEVICES;
2111 }
2112
2113 return (rval);
2114}
2115
2116static void
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002117qla2x00_probe_for_all_luns(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118{
2119 fc_port_t *fcport;
2120
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002121 qla2x00_mark_all_devices_lost(ha, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 list_for_each_entry(fcport, &ha->fcports, list) {
2123 if (fcport->port_type != FCT_TARGET)
2124 continue;
2125
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002126 qla2x00_update_fcport(ha, fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 }
2128}
2129
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002130static void
2131qla2x00_iidma_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
2132{
2133#define LS_UNKNOWN 2
2134 static char *link_speeds[5] = { "1", "2", "?", "4" };
2135 int rval;
2136 uint16_t port_speed, mb[6];
2137
Andrew Vasquezc76f2c02007-07-19 15:05:57 -07002138 if (!IS_IIDMA_CAPABLE(ha))
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002139 return;
2140
2141 switch (be16_to_cpu(fcport->fp_speed)) {
2142 case BIT_15:
2143 port_speed = PORT_SPEED_1GB;
2144 break;
2145 case BIT_14:
2146 port_speed = PORT_SPEED_2GB;
2147 break;
2148 case BIT_13:
2149 port_speed = PORT_SPEED_4GB;
2150 break;
2151 default:
2152 DEBUG2(printk("scsi(%ld): %02x%02x%02x%02x%02x%02x%02x%02x -- "
2153 "unsupported FM port operating speed (%04x).\n",
2154 ha->host_no, fcport->port_name[0], fcport->port_name[1],
2155 fcport->port_name[2], fcport->port_name[3],
2156 fcport->port_name[4], fcport->port_name[5],
2157 fcport->port_name[6], fcport->port_name[7],
2158 be16_to_cpu(fcport->fp_speed)));
2159 port_speed = PORT_SPEED_UNKNOWN;
2160 break;
2161 }
2162 if (port_speed == PORT_SPEED_UNKNOWN)
2163 return;
2164
2165 rval = qla2x00_set_idma_speed(ha, fcport->loop_id, port_speed, mb);
2166 if (rval != QLA_SUCCESS) {
2167 DEBUG2(printk("scsi(%ld): Unable to adjust iIDMA "
2168 "%02x%02x%02x%02x%02x%02x%02x%02x -- %04x %x %04x %04x.\n",
2169 ha->host_no, fcport->port_name[0], fcport->port_name[1],
2170 fcport->port_name[2], fcport->port_name[3],
2171 fcport->port_name[4], fcport->port_name[5],
2172 fcport->port_name[6], fcport->port_name[7], rval,
2173 port_speed, mb[0], mb[1]));
2174 } else {
2175 DEBUG2(qla_printk(KERN_INFO, ha,
2176 "iIDMA adjusted to %s GB/s on "
2177 "%02x%02x%02x%02x%02x%02x%02x%02x.\n",
2178 link_speeds[port_speed], fcport->port_name[0],
2179 fcport->port_name[1], fcport->port_name[2],
2180 fcport->port_name[3], fcport->port_name[4],
2181 fcport->port_name[5], fcport->port_name[6],
2182 fcport->port_name[7]));
2183 }
2184}
2185
Adrian Bunk23be3312006-11-24 02:46:01 +01002186static void
8482e112005-04-17 15:04:54 -05002187qla2x00_reg_remote_port(scsi_qla_host_t *ha, fc_port_t *fcport)
2188{
2189 struct fc_rport_identifiers rport_ids;
bdf79622005-04-17 15:06:53 -05002190 struct fc_rport *rport;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002191 unsigned long flags;
8482e112005-04-17 15:04:54 -05002192
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002193 if (fcport->drport)
2194 qla2x00_rport_del(fcport);
2195 if (fcport->rport)
2196 return;
8482e112005-04-17 15:04:54 -05002197
Andrew Vasquezf8b02a82005-08-31 15:21:20 -07002198 rport_ids.node_name = wwn_to_u64(fcport->node_name);
2199 rport_ids.port_name = wwn_to_u64(fcport->port_name);
8482e112005-04-17 15:04:54 -05002200 rport_ids.port_id = fcport->d_id.b.domain << 16 |
2201 fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
2202 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002203 rport = fc_remote_port_add(ha->host, 0, &rport_ids);
Andrew Vasquez77d74142005-07-08 18:00:36 -07002204 if (!rport) {
2205 qla_printk(KERN_WARNING, ha,
2206 "Unable to allocate fc remote port!\n");
2207 return;
2208 }
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002209 spin_lock_irqsave(&fcport->rport_lock, flags);
2210 fcport->rport = rport;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002211 *((fc_port_t **)rport->dd_data) = fcport;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002212 spin_unlock_irqrestore(&fcport->rport_lock, flags);
2213
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07002214 rport->supported_classes = fcport->supported_classes;
Andrew Vasquez77d74142005-07-08 18:00:36 -07002215
2216 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
8482e112005-04-17 15:04:54 -05002217 if (fcport->port_type == FCT_INITIATOR)
2218 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2219 if (fcport->port_type == FCT_TARGET)
2220 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
Andrew Vasquez77d74142005-07-08 18:00:36 -07002221 fc_remote_port_rolechg(rport, rport_ids.roles);
bdf79622005-04-17 15:06:53 -05002222
Andrew Vasquezcc4731f2005-07-06 10:32:37 -07002223 if (rport->scsi_target_id != -1 &&
2224 rport->scsi_target_id < ha->host->max_id)
bdf79622005-04-17 15:06:53 -05002225 fcport->os_target_id = rport->scsi_target_id;
8482e112005-04-17 15:04:54 -05002226}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
2228/*
Adrian Bunk23be3312006-11-24 02:46:01 +01002229 * qla2x00_update_fcport
2230 * Updates device on list.
2231 *
2232 * Input:
2233 * ha = adapter block pointer.
2234 * fcport = port structure pointer.
2235 *
2236 * Return:
2237 * 0 - Success
2238 * BIT_0 - error
2239 *
2240 * Context:
2241 * Kernel context.
2242 */
2243void
2244qla2x00_update_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
2245{
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002246 scsi_qla_host_t *pha = to_qla_parent(ha);
2247
Adrian Bunk23be3312006-11-24 02:46:01 +01002248 fcport->ha = ha;
2249 fcport->login_retry = 0;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002250 fcport->port_login_retry_count = pha->port_down_retry_count *
Adrian Bunk23be3312006-11-24 02:46:01 +01002251 PORT_RETRY_TIME;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002252 atomic_set(&fcport->port_down_timer, pha->port_down_retry_count *
Adrian Bunk23be3312006-11-24 02:46:01 +01002253 PORT_RETRY_TIME);
2254 fcport->flags &= ~FCF_LOGIN_NEEDED;
2255
2256 qla2x00_iidma_fcport(ha, fcport);
2257
2258 atomic_set(&fcport->state, FCS_ONLINE);
2259
2260 qla2x00_reg_remote_port(ha, fcport);
2261}
2262
2263/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 * qla2x00_configure_fabric
2265 * Setup SNS devices with loop ID's.
2266 *
2267 * Input:
2268 * ha = adapter block pointer.
2269 *
2270 * Returns:
2271 * 0 = success.
2272 * BIT_0 = error
2273 */
2274static int
2275qla2x00_configure_fabric(scsi_qla_host_t *ha)
2276{
2277 int rval, rval2;
2278 fc_port_t *fcport, *fcptemp;
2279 uint16_t next_loopid;
2280 uint16_t mb[MAILBOX_REGISTER_COUNT];
Andrew Vasquez01071092005-07-06 10:31:37 -07002281 uint16_t loop_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 LIST_HEAD(new_fcports);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002283 scsi_qla_host_t *pha = to_qla_parent(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
2285 /* If FL port exists, then SNS is present */
Andrew Vasqueze4289242007-07-19 15:05:56 -07002286 if (IS_FWI2_CAPABLE(ha))
Andrew Vasquez01071092005-07-06 10:31:37 -07002287 loop_id = NPH_F_PORT;
2288 else
2289 loop_id = SNS_FL_PORT;
Andrew Vasquez90991c82006-10-02 12:00:46 -07002290 rval = qla2x00_get_port_name(ha, loop_id, ha->fabric_node_name, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 if (rval != QLA_SUCCESS) {
2292 DEBUG2(printk("scsi(%ld): MBC_GET_PORT_NAME Failed, No FL "
2293 "Port\n", ha->host_no));
2294
2295 ha->device_flags &= ~SWITCH_FOUND;
2296 return (QLA_SUCCESS);
2297 }
Andrew Vasquez90991c82006-10-02 12:00:46 -07002298 ha->device_flags |= SWITCH_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
2300 /* Mark devices that need re-synchronization. */
2301 rval2 = qla2x00_device_resync(ha);
2302 if (rval2 == QLA_RSCNS_HANDLED) {
2303 /* No point doing the scan, just continue. */
2304 return (QLA_SUCCESS);
2305 }
2306 do {
Andrew Vasquezcca53352005-08-26 19:08:30 -07002307 /* FDMI support. */
2308 if (ql2xfdmienable &&
2309 test_and_clear_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags))
2310 qla2x00_fdmi_register(ha);
2311
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 /* Ensure we are logged into the SNS. */
Andrew Vasqueze4289242007-07-19 15:05:56 -07002313 if (IS_FWI2_CAPABLE(ha))
Andrew Vasquez01071092005-07-06 10:31:37 -07002314 loop_id = NPH_SNS;
2315 else
2316 loop_id = SIMPLE_NAME_SERVER;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07002317 ha->isp_ops->fabric_login(ha, loop_id, 0xff, 0xff,
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002318 0xfc, mb, BIT_1 | BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 if (mb[0] != MBS_COMMAND_COMPLETE) {
2320 DEBUG2(qla_printk(KERN_INFO, ha,
2321 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
Andrew Vasquez01071092005-07-06 10:31:37 -07002322 "mb[2]=%x mb[6]=%x mb[7]=%x\n", loop_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 mb[0], mb[1], mb[2], mb[6], mb[7]));
2324 return (QLA_SUCCESS);
2325 }
2326
2327 if (test_and_clear_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags)) {
2328 if (qla2x00_rft_id(ha)) {
2329 /* EMPTY */
2330 DEBUG2(printk("scsi(%ld): Register FC-4 "
2331 "TYPE failed.\n", ha->host_no));
2332 }
2333 if (qla2x00_rff_id(ha)) {
2334 /* EMPTY */
2335 DEBUG2(printk("scsi(%ld): Register FC-4 "
2336 "Features failed.\n", ha->host_no));
2337 }
2338 if (qla2x00_rnn_id(ha)) {
2339 /* EMPTY */
2340 DEBUG2(printk("scsi(%ld): Register Node Name "
2341 "failed.\n", ha->host_no));
2342 } else if (qla2x00_rsnn_nn(ha)) {
2343 /* EMPTY */
2344 DEBUG2(printk("scsi(%ld): Register Symbolic "
2345 "Node Name failed.\n", ha->host_no));
2346 }
2347 }
2348
2349 rval = qla2x00_find_all_fabric_devs(ha, &new_fcports);
2350 if (rval != QLA_SUCCESS)
2351 break;
2352
2353 /*
2354 * Logout all previous fabric devices marked lost, except
2355 * tape devices.
2356 */
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002357 list_for_each_entry(fcport, &pha->fcports, list) {
2358 if (fcport->vp_idx !=ha->vp_idx)
2359 continue;
2360
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2362 break;
2363
2364 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0)
2365 continue;
2366
2367 if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
2368 qla2x00_mark_device_lost(ha, fcport,
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002369 ql2xplogiabsentdevice, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 if (fcport->loop_id != FC_NO_LOOP_ID &&
2371 (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2372 fcport->port_type != FCT_INITIATOR &&
2373 fcport->port_type != FCT_BROADCAST) {
Andrew Vasquezfd34f552007-07-19 15:06:00 -07002374 ha->isp_ops->fabric_logout(ha,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07002375 fcport->loop_id,
2376 fcport->d_id.b.domain,
2377 fcport->d_id.b.area,
2378 fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 fcport->loop_id = FC_NO_LOOP_ID;
2380 }
2381 }
2382 }
2383
2384 /* Starting free loop ID. */
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002385 next_loopid = pha->min_external_loopid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
2387 /*
2388 * Scan through our port list and login entries that need to be
2389 * logged in.
2390 */
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002391 list_for_each_entry(fcport, &pha->fcports, list) {
2392 if (fcport->vp_idx != ha->vp_idx)
2393 continue;
2394
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 if (atomic_read(&ha->loop_down_timer) ||
2396 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2397 break;
2398
2399 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2400 (fcport->flags & FCF_LOGIN_NEEDED) == 0)
2401 continue;
2402
2403 if (fcport->loop_id == FC_NO_LOOP_ID) {
2404 fcport->loop_id = next_loopid;
2405 rval = qla2x00_find_new_loop_id(ha, fcport);
2406 if (rval != QLA_SUCCESS) {
2407 /* Ran out of IDs to use */
2408 break;
2409 }
2410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 /* Login and update database */
2412 qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
2413 }
2414
2415 /* Exit if out of loop IDs. */
2416 if (rval != QLA_SUCCESS) {
2417 break;
2418 }
2419
2420 /*
2421 * Login and add the new devices to our port list.
2422 */
2423 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2424 if (atomic_read(&ha->loop_down_timer) ||
2425 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2426 break;
2427
2428 /* Find a new loop ID to use. */
2429 fcport->loop_id = next_loopid;
2430 rval = qla2x00_find_new_loop_id(ha, fcport);
2431 if (rval != QLA_SUCCESS) {
2432 /* Ran out of IDs to use */
2433 break;
2434 }
2435
bdf79622005-04-17 15:06:53 -05002436 /* Login and update database */
2437 qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002438
2439 if (ha->parent) {
2440 fcport->ha = ha;
2441 fcport->vp_idx = ha->vp_idx;
2442 list_add_tail(&fcport->vp_fcport,
2443 &ha->vp_fcports);
2444 list_move_tail(&fcport->list,
2445 &ha->parent->fcports);
2446 } else
2447 list_move_tail(&fcport->list, &ha->fcports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 }
2449 } while (0);
2450
2451 /* Free all new device structures not processed. */
2452 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2453 list_del(&fcport->list);
2454 kfree(fcport);
2455 }
2456
2457 if (rval) {
2458 DEBUG2(printk("scsi(%ld): Configure fabric error exit: "
2459 "rval=%d\n", ha->host_no, rval));
2460 }
2461
2462 return (rval);
2463}
2464
2465
2466/*
2467 * qla2x00_find_all_fabric_devs
2468 *
2469 * Input:
2470 * ha = adapter block pointer.
2471 * dev = database device entry pointer.
2472 *
2473 * Returns:
2474 * 0 = success.
2475 *
2476 * Context:
2477 * Kernel context.
2478 */
2479static int
2480qla2x00_find_all_fabric_devs(scsi_qla_host_t *ha, struct list_head *new_fcports)
2481{
2482 int rval;
2483 uint16_t loop_id;
2484 fc_port_t *fcport, *new_fcport, *fcptemp;
2485 int found;
2486
2487 sw_info_t *swl;
2488 int swl_idx;
2489 int first_dev, last_dev;
2490 port_id_t wrap, nxt_d_id;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002491 int vp_index;
2492 int empty_vp_index;
2493 int found_vp;
2494 scsi_qla_host_t *vha;
2495 scsi_qla_host_t *pha = to_qla_parent(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496
2497 rval = QLA_SUCCESS;
2498
2499 /* Try GID_PT to get device list, else GAN. */
2500 swl = kmalloc(sizeof(sw_info_t) * MAX_FIBRE_DEVICES, GFP_ATOMIC);
2501 if (swl == NULL) {
2502 /*EMPTY*/
2503 DEBUG2(printk("scsi(%ld): GID_PT allocations failed, fallback "
2504 "on GA_NXT\n", ha->host_no));
2505 } else {
2506 memset(swl, 0, sizeof(sw_info_t) * MAX_FIBRE_DEVICES);
2507 if (qla2x00_gid_pt(ha, swl) != QLA_SUCCESS) {
2508 kfree(swl);
2509 swl = NULL;
2510 } else if (qla2x00_gpn_id(ha, swl) != QLA_SUCCESS) {
2511 kfree(swl);
2512 swl = NULL;
2513 } else if (qla2x00_gnn_id(ha, swl) != QLA_SUCCESS) {
2514 kfree(swl);
2515 swl = NULL;
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002516 } else if (qla2x00_gfpn_id(ha, swl) == QLA_SUCCESS) {
2517 qla2x00_gpsc(ha, swl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 }
2519 }
2520 swl_idx = 0;
2521
2522 /* Allocate temporary fcport for any new fcports discovered. */
2523 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2524 if (new_fcport == NULL) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002525 kfree(swl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 return (QLA_MEMORY_ALLOC_FAILED);
2527 }
2528 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002529 new_fcport->vp_idx = ha->vp_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 /* Set start port ID scan at adapter ID. */
2531 first_dev = 1;
2532 last_dev = 0;
2533
2534 /* Starting free loop ID. */
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002535 loop_id = pha->min_external_loopid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 for (; loop_id <= ha->last_loop_id; loop_id++) {
Andrew Vasquez3d716442005-07-06 10:30:26 -07002537 if (qla2x00_is_reserved_id(ha, loop_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 continue;
2539
Andrew Vasquez23443b12005-12-06 10:57:06 -08002540 if (atomic_read(&ha->loop_down_timer) || LOOP_TRANSITION(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 break;
2542
2543 if (swl != NULL) {
2544 if (last_dev) {
2545 wrap.b24 = new_fcport->d_id.b24;
2546 } else {
2547 new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
2548 memcpy(new_fcport->node_name,
2549 swl[swl_idx].node_name, WWN_SIZE);
2550 memcpy(new_fcport->port_name,
2551 swl[swl_idx].port_name, WWN_SIZE);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002552 memcpy(new_fcport->fabric_port_name,
2553 swl[swl_idx].fabric_port_name, WWN_SIZE);
2554 new_fcport->fp_speed = swl[swl_idx].fp_speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555
2556 if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
2557 last_dev = 1;
2558 }
2559 swl_idx++;
2560 }
2561 } else {
2562 /* Send GA_NXT to the switch */
2563 rval = qla2x00_ga_nxt(ha, new_fcport);
2564 if (rval != QLA_SUCCESS) {
2565 qla_printk(KERN_WARNING, ha,
2566 "SNS scan failed -- assuming zero-entry "
2567 "result...\n");
2568 list_for_each_entry_safe(fcport, fcptemp,
2569 new_fcports, list) {
2570 list_del(&fcport->list);
2571 kfree(fcport);
2572 }
2573 rval = QLA_SUCCESS;
2574 break;
2575 }
2576 }
2577
2578 /* If wrap on switch device list, exit. */
2579 if (first_dev) {
2580 wrap.b24 = new_fcport->d_id.b24;
2581 first_dev = 0;
2582 } else if (new_fcport->d_id.b24 == wrap.b24) {
2583 DEBUG2(printk("scsi(%ld): device wrap (%02x%02x%02x)\n",
2584 ha->host_no, new_fcport->d_id.b.domain,
2585 new_fcport->d_id.b.area, new_fcport->d_id.b.al_pa));
2586 break;
2587 }
2588
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002589 /* Bypass if same physical adapter. */
2590 if (new_fcport->d_id.b24 == pha->d_id.b24)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 continue;
2592
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002593 /* Bypass virtual ports of the same host. */
2594 if (pha->num_vhosts) {
2595 vp_index = find_next_bit(
2596 (unsigned long *)pha->vp_idx_map,
2597 MAX_MULTI_ID_FABRIC + 1, 1);
2598
2599 for (;vp_index <= MAX_MULTI_ID_FABRIC;
2600 vp_index = find_next_bit(
2601 (unsigned long *)pha->vp_idx_map,
2602 MAX_MULTI_ID_FABRIC + 1, vp_index + 1)) {
2603 empty_vp_index = 1;
2604 found_vp = 0;
2605 list_for_each_entry(vha, &pha->vp_list,
2606 vp_list) {
2607 if (vp_index == vha->vp_idx) {
2608 empty_vp_index = 0;
2609 found_vp = 1;
2610 break;
2611 }
2612 }
2613
2614 if (empty_vp_index)
2615 continue;
2616
2617 if (found_vp &&
2618 new_fcport->d_id.b24 == vha->d_id.b24)
2619 break;
2620 }
2621 if (vp_index <= MAX_MULTI_ID_FABRIC)
2622 continue;
2623 }
2624
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07002625 /* Bypass if same domain and area of adapter. */
2626 if (((new_fcport->d_id.b24 & 0xffff00) ==
2627 (ha->d_id.b24 & 0xffff00)) && ha->current_topology ==
2628 ISP_CFG_FL)
2629 continue;
2630
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 /* Bypass reserved domain fields. */
2632 if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
2633 continue;
2634
2635 /* Locate matching device in database. */
2636 found = 0;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002637 list_for_each_entry(fcport, &pha->fcports, list) {
2638 if (new_fcport->vp_idx != fcport->vp_idx)
2639 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 if (memcmp(new_fcport->port_name, fcport->port_name,
2641 WWN_SIZE))
2642 continue;
2643
2644 found++;
2645
Andrew Vasquezd8b45212006-10-02 12:00:43 -07002646 /* Update port state. */
2647 memcpy(fcport->fabric_port_name,
2648 new_fcport->fabric_port_name, WWN_SIZE);
2649 fcport->fp_speed = new_fcport->fp_speed;
2650
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 /*
2652 * If address the same and state FCS_ONLINE, nothing
2653 * changed.
2654 */
2655 if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
2656 atomic_read(&fcport->state) == FCS_ONLINE) {
2657 break;
2658 }
2659
2660 /*
2661 * If device was not a fabric device before.
2662 */
2663 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
2664 fcport->d_id.b24 = new_fcport->d_id.b24;
2665 fcport->loop_id = FC_NO_LOOP_ID;
2666 fcport->flags |= (FCF_FABRIC_DEVICE |
2667 FCF_LOGIN_NEEDED);
2668 fcport->flags &= ~FCF_PERSISTENT_BOUND;
2669 break;
2670 }
2671
2672 /*
2673 * Port ID changed or device was marked to be updated;
2674 * Log it out if still logged in and mark it for
2675 * relogin later.
2676 */
2677 fcport->d_id.b24 = new_fcport->d_id.b24;
2678 fcport->flags |= FCF_LOGIN_NEEDED;
2679 if (fcport->loop_id != FC_NO_LOOP_ID &&
2680 (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2681 fcport->port_type != FCT_INITIATOR &&
2682 fcport->port_type != FCT_BROADCAST) {
Andrew Vasquezfd34f552007-07-19 15:06:00 -07002683 ha->isp_ops->fabric_logout(ha, fcport->loop_id,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07002684 fcport->d_id.b.domain, fcport->d_id.b.area,
2685 fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 fcport->loop_id = FC_NO_LOOP_ID;
2687 }
2688
2689 break;
2690 }
2691
2692 if (found)
2693 continue;
2694
2695 /* If device was not in our fcports list, then add it. */
2696 list_add_tail(&new_fcport->list, new_fcports);
2697
2698 /* Allocate a new replacement fcport. */
2699 nxt_d_id.b24 = new_fcport->d_id.b24;
2700 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2701 if (new_fcport == NULL) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002702 kfree(swl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 return (QLA_MEMORY_ALLOC_FAILED);
2704 }
2705 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2706 new_fcport->d_id.b24 = nxt_d_id.b24;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002707 new_fcport->vp_idx = ha->vp_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 }
2709
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002710 kfree(swl);
2711 kfree(new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
2713 if (!list_empty(new_fcports))
2714 ha->device_flags |= DFLG_FABRIC_DEVICES;
2715
2716 return (rval);
2717}
2718
2719/*
2720 * qla2x00_find_new_loop_id
2721 * Scan through our port list and find a new usable loop ID.
2722 *
2723 * Input:
2724 * ha: adapter state pointer.
2725 * dev: port structure pointer.
2726 *
2727 * Returns:
2728 * qla2x00 local function return status code.
2729 *
2730 * Context:
2731 * Kernel context.
2732 */
Adrian Bunk413975a2006-06-30 02:33:06 -07002733static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734qla2x00_find_new_loop_id(scsi_qla_host_t *ha, fc_port_t *dev)
2735{
2736 int rval;
2737 int found;
2738 fc_port_t *fcport;
2739 uint16_t first_loop_id;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002740 scsi_qla_host_t *pha = to_qla_parent(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741
2742 rval = QLA_SUCCESS;
2743
2744 /* Save starting loop ID. */
2745 first_loop_id = dev->loop_id;
2746
2747 for (;;) {
2748 /* Skip loop ID if already used by adapter. */
2749 if (dev->loop_id == ha->loop_id) {
2750 dev->loop_id++;
2751 }
2752
2753 /* Skip reserved loop IDs. */
Andrew Vasquez3d716442005-07-06 10:30:26 -07002754 while (qla2x00_is_reserved_id(ha, dev->loop_id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 dev->loop_id++;
2756 }
2757
2758 /* Reset loop ID if passed the end. */
2759 if (dev->loop_id > ha->last_loop_id) {
2760 /* first loop ID. */
2761 dev->loop_id = ha->min_external_loopid;
2762 }
2763
2764 /* Check for loop ID being already in use. */
2765 found = 0;
2766 fcport = NULL;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002767 list_for_each_entry(fcport, &pha->fcports, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 if (fcport->loop_id == dev->loop_id && fcport != dev) {
2769 /* ID possibly in use */
2770 found++;
2771 break;
2772 }
2773 }
2774
2775 /* If not in use then it is free to use. */
2776 if (!found) {
2777 break;
2778 }
2779
2780 /* ID in use. Try next value. */
2781 dev->loop_id++;
2782
2783 /* If wrap around. No free ID to use. */
2784 if (dev->loop_id == first_loop_id) {
2785 dev->loop_id = FC_NO_LOOP_ID;
2786 rval = QLA_FUNCTION_FAILED;
2787 break;
2788 }
2789 }
2790
2791 return (rval);
2792}
2793
2794/*
2795 * qla2x00_device_resync
2796 * Marks devices in the database that needs resynchronization.
2797 *
2798 * Input:
2799 * ha = adapter block pointer.
2800 *
2801 * Context:
2802 * Kernel context.
2803 */
2804static int
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002805qla2x00_device_resync(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806{
2807 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 uint32_t mask;
2809 fc_port_t *fcport;
2810 uint32_t rscn_entry;
2811 uint8_t rscn_out_iter;
2812 uint8_t format;
2813 port_id_t d_id;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002814 scsi_qla_host_t *pha = to_qla_parent(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815
2816 rval = QLA_RSCNS_HANDLED;
2817
2818 while (ha->rscn_out_ptr != ha->rscn_in_ptr ||
2819 ha->flags.rscn_queue_overflow) {
2820
2821 rscn_entry = ha->rscn_queue[ha->rscn_out_ptr];
2822 format = MSB(MSW(rscn_entry));
2823 d_id.b.domain = LSB(MSW(rscn_entry));
2824 d_id.b.area = MSB(LSW(rscn_entry));
2825 d_id.b.al_pa = LSB(LSW(rscn_entry));
2826
2827 DEBUG(printk("scsi(%ld): RSCN queue entry[%d] = "
2828 "[%02x/%02x%02x%02x].\n",
2829 ha->host_no, ha->rscn_out_ptr, format, d_id.b.domain,
2830 d_id.b.area, d_id.b.al_pa));
2831
2832 ha->rscn_out_ptr++;
2833 if (ha->rscn_out_ptr == MAX_RSCN_COUNT)
2834 ha->rscn_out_ptr = 0;
2835
2836 /* Skip duplicate entries. */
2837 for (rscn_out_iter = ha->rscn_out_ptr;
2838 !ha->flags.rscn_queue_overflow &&
2839 rscn_out_iter != ha->rscn_in_ptr;
2840 rscn_out_iter = (rscn_out_iter ==
2841 (MAX_RSCN_COUNT - 1)) ? 0: rscn_out_iter + 1) {
2842
2843 if (rscn_entry != ha->rscn_queue[rscn_out_iter])
2844 break;
2845
2846 DEBUG(printk("scsi(%ld): Skipping duplicate RSCN queue "
2847 "entry found at [%d].\n", ha->host_no,
2848 rscn_out_iter));
2849
2850 ha->rscn_out_ptr = rscn_out_iter;
2851 }
2852
2853 /* Queue overflow, set switch default case. */
2854 if (ha->flags.rscn_queue_overflow) {
2855 DEBUG(printk("scsi(%ld): device_resync: rscn "
2856 "overflow.\n", ha->host_no));
2857
2858 format = 3;
2859 ha->flags.rscn_queue_overflow = 0;
2860 }
2861
2862 switch (format) {
2863 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 mask = 0xffffff;
2865 break;
2866 case 1:
2867 mask = 0xffff00;
2868 break;
2869 case 2:
2870 mask = 0xff0000;
2871 break;
2872 default:
2873 mask = 0x0;
2874 d_id.b24 = 0;
2875 ha->rscn_out_ptr = ha->rscn_in_ptr;
2876 break;
2877 }
2878
2879 rval = QLA_SUCCESS;
2880
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002881 list_for_each_entry(fcport, &pha->fcports, list) {
2882 if (fcport->vp_idx != ha->vp_idx)
2883 continue;
2884
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2886 (fcport->d_id.b24 & mask) != d_id.b24 ||
2887 fcport->port_type == FCT_BROADCAST)
2888 continue;
2889
2890 if (atomic_read(&fcport->state) == FCS_ONLINE) {
2891 if (format != 3 ||
2892 fcport->port_type != FCT_INITIATOR) {
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002893 qla2x00_mark_device_lost(ha, fcport,
2894 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 }
2896 }
2897 fcport->flags &= ~FCF_FARP_DONE;
2898 }
2899 }
2900 return (rval);
2901}
2902
2903/*
2904 * qla2x00_fabric_dev_login
2905 * Login fabric target device and update FC port database.
2906 *
2907 * Input:
2908 * ha: adapter state pointer.
2909 * fcport: port structure list pointer.
2910 * next_loopid: contains value of a new loop ID that can be used
2911 * by the next login attempt.
2912 *
2913 * Returns:
2914 * qla2x00 local function return status code.
2915 *
2916 * Context:
2917 * Kernel context.
2918 */
2919static int
2920qla2x00_fabric_dev_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2921 uint16_t *next_loopid)
2922{
2923 int rval;
2924 int retry;
Andrew Vasquez01071092005-07-06 10:31:37 -07002925 uint8_t opts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926
2927 rval = QLA_SUCCESS;
2928 retry = 0;
2929
2930 rval = qla2x00_fabric_login(ha, fcport, next_loopid);
2931 if (rval == QLA_SUCCESS) {
Andrew Vasquez01071092005-07-06 10:31:37 -07002932 /* Send an ADISC to tape devices.*/
2933 opts = 0;
2934 if (fcport->flags & FCF_TAPE_PRESENT)
2935 opts |= BIT_1;
2936 rval = qla2x00_get_port_database(ha, fcport, opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 if (rval != QLA_SUCCESS) {
Andrew Vasquezfd34f552007-07-19 15:06:00 -07002938 ha->isp_ops->fabric_logout(ha, fcport->loop_id,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07002939 fcport->d_id.b.domain, fcport->d_id.b.area,
2940 fcport->d_id.b.al_pa);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002941 qla2x00_mark_device_lost(ha, fcport, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 } else {
2943 qla2x00_update_fcport(ha, fcport);
2944 }
2945 }
2946
2947 return (rval);
2948}
2949
2950/*
2951 * qla2x00_fabric_login
2952 * Issue fabric login command.
2953 *
2954 * Input:
2955 * ha = adapter block pointer.
2956 * device = pointer to FC device type structure.
2957 *
2958 * Returns:
2959 * 0 - Login successfully
2960 * 1 - Login failed
2961 * 2 - Initiator device
2962 * 3 - Fatal error
2963 */
2964int
2965qla2x00_fabric_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2966 uint16_t *next_loopid)
2967{
2968 int rval;
2969 int retry;
2970 uint16_t tmp_loopid;
2971 uint16_t mb[MAILBOX_REGISTER_COUNT];
2972
2973 retry = 0;
2974 tmp_loopid = 0;
2975
2976 for (;;) {
2977 DEBUG(printk("scsi(%ld): Trying Fabric Login w/loop id 0x%04x "
2978 "for port %02x%02x%02x.\n",
2979 ha->host_no, fcport->loop_id, fcport->d_id.b.domain,
2980 fcport->d_id.b.area, fcport->d_id.b.al_pa));
2981
2982 /* Login fcport on switch. */
Andrew Vasquezfd34f552007-07-19 15:06:00 -07002983 ha->isp_ops->fabric_login(ha, fcport->loop_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 fcport->d_id.b.domain, fcport->d_id.b.area,
2985 fcport->d_id.b.al_pa, mb, BIT_0);
2986 if (mb[0] == MBS_PORT_ID_USED) {
2987 /*
2988 * Device has another loop ID. The firmware team
Andrew Vasquez01071092005-07-06 10:31:37 -07002989 * recommends the driver perform an implicit login with
2990 * the specified ID again. The ID we just used is save
2991 * here so we return with an ID that can be tried by
2992 * the next login.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 */
2994 retry++;
2995 tmp_loopid = fcport->loop_id;
2996 fcport->loop_id = mb[1];
2997
2998 DEBUG(printk("Fabric Login: port in use - next "
2999 "loop id=0x%04x, port Id=%02x%02x%02x.\n",
3000 fcport->loop_id, fcport->d_id.b.domain,
3001 fcport->d_id.b.area, fcport->d_id.b.al_pa));
3002
3003 } else if (mb[0] == MBS_COMMAND_COMPLETE) {
3004 /*
3005 * Login succeeded.
3006 */
3007 if (retry) {
3008 /* A retry occurred before. */
3009 *next_loopid = tmp_loopid;
3010 } else {
3011 /*
3012 * No retry occurred before. Just increment the
3013 * ID value for next login.
3014 */
3015 *next_loopid = (fcport->loop_id + 1);
3016 }
3017
3018 if (mb[1] & BIT_0) {
3019 fcport->port_type = FCT_INITIATOR;
3020 } else {
3021 fcport->port_type = FCT_TARGET;
3022 if (mb[1] & BIT_1) {
3023 fcport->flags |= FCF_TAPE_PRESENT;
3024 }
3025 }
3026
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07003027 if (mb[10] & BIT_0)
3028 fcport->supported_classes |= FC_COS_CLASS2;
3029 if (mb[10] & BIT_1)
3030 fcport->supported_classes |= FC_COS_CLASS3;
3031
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 rval = QLA_SUCCESS;
3033 break;
3034 } else if (mb[0] == MBS_LOOP_ID_USED) {
3035 /*
3036 * Loop ID already used, try next loop ID.
3037 */
3038 fcport->loop_id++;
3039 rval = qla2x00_find_new_loop_id(ha, fcport);
3040 if (rval != QLA_SUCCESS) {
3041 /* Ran out of loop IDs to use */
3042 break;
3043 }
3044 } else if (mb[0] == MBS_COMMAND_ERROR) {
3045 /*
3046 * Firmware possibly timed out during login. If NO
3047 * retries are left to do then the device is declared
3048 * dead.
3049 */
3050 *next_loopid = fcport->loop_id;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07003051 ha->isp_ops->fabric_logout(ha, fcport->loop_id,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07003052 fcport->d_id.b.domain, fcport->d_id.b.area,
3053 fcport->d_id.b.al_pa);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08003054 qla2x00_mark_device_lost(ha, fcport, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055
3056 rval = 1;
3057 break;
3058 } else {
3059 /*
3060 * unrecoverable / not handled error
3061 */
3062 DEBUG2(printk("%s(%ld): failed=%x port_id=%02x%02x%02x "
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003063 "loop_id=%x jiffies=%lx.\n",
3064 __func__, ha->host_no, mb[0],
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 fcport->d_id.b.domain, fcport->d_id.b.area,
3066 fcport->d_id.b.al_pa, fcport->loop_id, jiffies));
3067
3068 *next_loopid = fcport->loop_id;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07003069 ha->isp_ops->fabric_logout(ha, fcport->loop_id,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07003070 fcport->d_id.b.domain, fcport->d_id.b.area,
3071 fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 fcport->loop_id = FC_NO_LOOP_ID;
Andrew Vasquez0eedfcf2005-10-27 11:09:38 -07003073 fcport->login_retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074
3075 rval = 3;
3076 break;
3077 }
3078 }
3079
3080 return (rval);
3081}
3082
3083/*
3084 * qla2x00_local_device_login
3085 * Issue local device login command.
3086 *
3087 * Input:
3088 * ha = adapter block pointer.
3089 * loop_id = loop id of device to login to.
3090 *
3091 * Returns (Where's the #define!!!!):
3092 * 0 - Login successfully
3093 * 1 - Login failed
3094 * 3 - Fatal error
3095 */
3096int
andrew.vasquez@qlogic.com9a52a572006-03-09 14:27:44 -08003097qla2x00_local_device_login(scsi_qla_host_t *ha, fc_port_t *fcport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098{
3099 int rval;
3100 uint16_t mb[MAILBOX_REGISTER_COUNT];
3101
3102 memset(mb, 0, sizeof(mb));
andrew.vasquez@qlogic.com9a52a572006-03-09 14:27:44 -08003103 rval = qla2x00_login_local_device(ha, fcport, mb, BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 if (rval == QLA_SUCCESS) {
3105 /* Interrogate mailbox registers for any errors */
3106 if (mb[0] == MBS_COMMAND_ERROR)
3107 rval = 1;
3108 else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
3109 /* device not in PCB table */
3110 rval = 3;
3111 }
3112
3113 return (rval);
3114}
3115
3116/*
3117 * qla2x00_loop_resync
3118 * Resync with fibre channel devices.
3119 *
3120 * Input:
3121 * ha = adapter block pointer.
3122 *
3123 * Returns:
3124 * 0 = success
3125 */
3126int
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003127qla2x00_loop_resync(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128{
3129 int rval;
3130 uint32_t wait_time;
3131
3132 rval = QLA_SUCCESS;
3133
3134 atomic_set(&ha->loop_state, LOOP_UPDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3136 if (ha->flags.online) {
3137 if (!(rval = qla2x00_fw_ready(ha))) {
3138 /* Wait at most MAX_TARGET RSCNs for a stable link. */
3139 wait_time = 256;
3140 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 atomic_set(&ha->loop_state, LOOP_UPDATE);
3142
Andrew Vasquez01071092005-07-06 10:31:37 -07003143 /* Issue a marker after FW becomes ready. */
3144 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
3145 ha->marker_needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146
3147 /* Remap devices on Loop. */
3148 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
3149
3150 qla2x00_configure_loop(ha);
3151 wait_time--;
3152 } while (!atomic_read(&ha->loop_down_timer) &&
3153 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
3154 wait_time &&
3155 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
3156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 }
3158
3159 if (test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
3160 return (QLA_FUNCTION_FAILED);
3161 }
3162
3163 if (rval) {
3164 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
3165 }
3166
3167 return (rval);
3168}
3169
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170void
3171qla2x00_rescan_fcports(scsi_qla_host_t *ha)
3172{
3173 int rescan_done;
3174 fc_port_t *fcport;
3175
3176 rescan_done = 0;
3177 list_for_each_entry(fcport, &ha->fcports, list) {
3178 if ((fcport->flags & FCF_RESCAN_NEEDED) == 0)
3179 continue;
3180
3181 qla2x00_update_fcport(ha, fcport);
3182 fcport->flags &= ~FCF_RESCAN_NEEDED;
3183
3184 rescan_done = 1;
3185 }
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003186 qla2x00_probe_for_all_luns(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187}
3188
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08003189void
3190qla2x00_update_fcports(scsi_qla_host_t *ha)
3191{
3192 fc_port_t *fcport;
3193
3194 /* Go with deferred removal of rport references. */
3195 list_for_each_entry(fcport, &ha->fcports, list)
3196 if (fcport->drport)
3197 qla2x00_rport_del(fcport);
3198}
3199
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200/*
3201* qla2x00_abort_isp
3202* Resets ISP and aborts all outstanding commands.
3203*
3204* Input:
3205* ha = adapter block pointer.
3206*
3207* Returns:
3208* 0 = success
3209*/
3210int
3211qla2x00_abort_isp(scsi_qla_host_t *ha)
3212{
Andrew Vasquez476e8972006-08-23 14:54:55 -07003213 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 unsigned long flags = 0;
3215 uint16_t cnt;
3216 srb_t *sp;
3217 uint8_t status = 0;
3218
3219 if (ha->flags.online) {
3220 ha->flags.online = 0;
3221 clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222
3223 qla_printk(KERN_INFO, ha,
3224 "Performing ISP error recovery - ha= %p.\n", ha);
Andrew Vasquezfd34f552007-07-19 15:06:00 -07003225 ha->isp_ops->reset_chip(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226
3227 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
3228 if (atomic_read(&ha->loop_state) != LOOP_DOWN) {
3229 atomic_set(&ha->loop_state, LOOP_DOWN);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08003230 qla2x00_mark_all_devices_lost(ha, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 } else {
3232 if (!atomic_read(&ha->loop_down_timer))
3233 atomic_set(&ha->loop_down_timer,
3234 LOOP_DOWN_TIME);
3235 }
3236
3237 spin_lock_irqsave(&ha->hardware_lock, flags);
3238 /* Requeue all commands in outstanding command list. */
3239 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
3240 sp = ha->outstanding_cmds[cnt];
3241 if (sp) {
3242 ha->outstanding_cmds[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 sp->flags = 0;
f4f051e2005-04-17 15:02:26 -05003244 sp->cmd->result = DID_RESET << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 sp->cmd->host_scribble = (unsigned char *)NULL;
f4f051e2005-04-17 15:02:26 -05003246 qla2x00_sp_compl(ha, sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 }
3248 }
3249 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3250
Andrew Vasquezfd34f552007-07-19 15:06:00 -07003251 ha->isp_ops->get_flash_version(ha, ha->request_ring);
Andrew Vasquez30c47662007-01-29 10:22:21 -08003252
Andrew Vasquezfd34f552007-07-19 15:06:00 -07003253 ha->isp_ops->nvram_config(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254
3255 if (!qla2x00_restart_isp(ha)) {
3256 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3257
3258 if (!atomic_read(&ha->loop_down_timer)) {
3259 /*
3260 * Issue marker command only when we are going
3261 * to start the I/O .
3262 */
3263 ha->marker_needed = 1;
3264 }
3265
3266 ha->flags.online = 1;
3267
Andrew Vasquezfd34f552007-07-19 15:06:00 -07003268 ha->isp_ops->enable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003270 ha->isp_abort_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
Andrew Vasquez476e8972006-08-23 14:54:55 -07003272
3273 if (ha->eft) {
3274 rval = qla2x00_trace_control(ha, TC_ENABLE,
3275 ha->eft_dma, EFT_NUM_BUFFERS);
3276 if (rval) {
3277 qla_printk(KERN_WARNING, ha,
3278 "Unable to reinitialize EFT "
3279 "(%d).\n", rval);
3280 }
3281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 } else { /* failed the ISP abort */
3283 ha->flags.online = 1;
3284 if (test_bit(ISP_ABORT_RETRY, &ha->dpc_flags)) {
3285 if (ha->isp_abort_cnt == 0) {
3286 qla_printk(KERN_WARNING, ha,
3287 "ISP error recovery failed - "
3288 "board disabled\n");
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003289 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 * The next call disables the board
3291 * completely.
3292 */
Andrew Vasquezfd34f552007-07-19 15:06:00 -07003293 ha->isp_ops->reset_adapter(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 ha->flags.online = 0;
3295 clear_bit(ISP_ABORT_RETRY,
3296 &ha->dpc_flags);
3297 status = 0;
3298 } else { /* schedule another ISP abort */
3299 ha->isp_abort_cnt--;
3300 DEBUG(printk("qla%ld: ISP abort - "
Andrew Vasquez01071092005-07-06 10:31:37 -07003301 "retry remaining %d\n",
Andrew Vasquez744f11fd2006-06-23 16:11:05 -07003302 ha->host_no, ha->isp_abort_cnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 status = 1;
3304 }
3305 } else {
3306 ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
3307 DEBUG(printk("qla2x00(%ld): ISP error recovery "
3308 "- retrying (%d) more times\n",
Andrew Vasquez744f11fd2006-06-23 16:11:05 -07003309 ha->host_no, ha->isp_abort_cnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 set_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3311 status = 1;
3312 }
3313 }
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003314
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 }
3316
3317 if (status) {
3318 qla_printk(KERN_INFO, ha,
3319 "qla2x00_abort_isp: **** FAILED ****\n");
3320 } else {
3321 DEBUG(printk(KERN_INFO
3322 "qla2x00_abort_isp(%ld): exiting.\n",
Andrew Vasquez744f11fd2006-06-23 16:11:05 -07003323 ha->host_no));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 }
3325
3326 return(status);
3327}
3328
3329/*
3330* qla2x00_restart_isp
3331* restarts the ISP after a reset
3332*
3333* Input:
3334* ha = adapter block pointer.
3335*
3336* Returns:
3337* 0 = success
3338*/
3339static int
3340qla2x00_restart_isp(scsi_qla_host_t *ha)
3341{
3342 uint8_t status = 0;
Andrew Vasquez3d716442005-07-06 10:30:26 -07003343 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 unsigned long flags = 0;
3345 uint32_t wait_time;
3346
3347 /* If firmware needs to be loaded */
3348 if (qla2x00_isp_firmware(ha)) {
3349 ha->flags.online = 0;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07003350 if (!(status = ha->isp_ops->chip_diag(ha))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
3352 status = qla2x00_setup_chip(ha);
3353 goto done;
3354 }
3355
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 spin_lock_irqsave(&ha->hardware_lock, flags);
3357
andrew.vasquez@qlogic.com044cc6c2006-03-09 14:27:13 -08003358 if (!IS_QLA24XX(ha) && !IS_QLA54XX(ha)) {
Andrew Vasquez01071092005-07-06 10:31:37 -07003359 /*
3360 * Disable SRAM, Instruction RAM and GP RAM
3361 * parity.
3362 */
3363 WRT_REG_WORD(&reg->hccr,
3364 (HCCR_ENABLE_PARITY + 0x0));
3365 RD_REG_WORD(&reg->hccr);
3366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367
3368 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003369
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 status = qla2x00_setup_chip(ha);
3371
3372 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003373
andrew.vasquez@qlogic.com044cc6c2006-03-09 14:27:13 -08003374 if (!IS_QLA24XX(ha) && !IS_QLA54XX(ha)) {
Andrew Vasquez01071092005-07-06 10:31:37 -07003375 /* Enable proper parity */
3376 if (IS_QLA2300(ha))
3377 /* SRAM parity */
3378 WRT_REG_WORD(&reg->hccr,
3379 (HCCR_ENABLE_PARITY + 0x1));
3380 else
3381 /*
3382 * SRAM, Instruction RAM and GP RAM
3383 * parity.
3384 */
3385 WRT_REG_WORD(&reg->hccr,
3386 (HCCR_ENABLE_PARITY + 0x7));
3387 RD_REG_WORD(&reg->hccr);
3388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389
3390 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3391 }
3392 }
3393
3394 done:
3395 if (!status && !(status = qla2x00_init_rings(ha))) {
3396 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3397 if (!(status = qla2x00_fw_ready(ha))) {
3398 DEBUG(printk("%s(): Start configure loop, "
Andrew Vasquez744f11fd2006-06-23 16:11:05 -07003399 "status = %d\n", __func__, status));
Andrew Vasquez01071092005-07-06 10:31:37 -07003400
3401 /* Issue a marker after FW becomes ready. */
3402 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
3403
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 ha->flags.online = 1;
3405 /* Wait at most MAX_TARGET RSCNs for a stable link. */
3406 wait_time = 256;
3407 do {
3408 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
3409 qla2x00_configure_loop(ha);
3410 wait_time--;
3411 } while (!atomic_read(&ha->loop_down_timer) &&
3412 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
3413 wait_time &&
3414 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
3415 }
3416
3417 /* if no cable then assume it's good */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003418 if ((ha->device_flags & DFLG_NO_CABLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 status = 0;
3420
3421 DEBUG(printk("%s(): Configure loop done, status = 0x%x\n",
3422 __func__,
Andrew Vasquez744f11fd2006-06-23 16:11:05 -07003423 status));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 }
3425 return (status);
3426}
3427
3428/*
3429* qla2x00_reset_adapter
3430* Reset adapter.
3431*
3432* Input:
3433* ha = adapter block pointer.
3434*/
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003435void
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436qla2x00_reset_adapter(scsi_qla_host_t *ha)
3437{
3438 unsigned long flags = 0;
Andrew Vasquez3d716442005-07-06 10:30:26 -07003439 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440
3441 ha->flags.online = 0;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07003442 ha->isp_ops->disable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 spin_lock_irqsave(&ha->hardware_lock, flags);
3445 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
3446 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
3447 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
3448 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
3449 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3450}
Andrew Vasquez01071092005-07-06 10:31:37 -07003451
3452void
3453qla24xx_reset_adapter(scsi_qla_host_t *ha)
3454{
3455 unsigned long flags = 0;
3456 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
3457
3458 ha->flags.online = 0;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07003459 ha->isp_ops->disable_intrs(ha);
Andrew Vasquez01071092005-07-06 10:31:37 -07003460
3461 spin_lock_irqsave(&ha->hardware_lock, flags);
3462 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
3463 RD_REG_DWORD(&reg->hccr);
3464 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
3465 RD_REG_DWORD(&reg->hccr);
3466 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3467}
3468
David Miller4e08df32007-04-16 12:37:43 -07003469/* On sparc systems, obtain port and node WWN from firmware
3470 * properties.
3471 */
3472static void qla24xx_nvram_wwn_from_ofw(scsi_qla_host_t *ha, struct nvram_24xx *nv)
3473{
3474#ifdef CONFIG_SPARC
3475 struct pci_dev *pdev = ha->pdev;
David S. Miller15576bc2007-05-08 00:36:49 -07003476 struct device_node *dp = pci_device_to_OF_node(pdev);
3477 const u8 *val;
David Miller4e08df32007-04-16 12:37:43 -07003478 int len;
3479
3480 val = of_get_property(dp, "port-wwn", &len);
3481 if (val && len >= WWN_SIZE)
3482 memcpy(nv->port_name, val, WWN_SIZE);
3483
3484 val = of_get_property(dp, "node-wwn", &len);
3485 if (val && len >= WWN_SIZE)
3486 memcpy(nv->node_name, val, WWN_SIZE);
3487#endif
3488}
3489
Andrew Vasquez01071092005-07-06 10:31:37 -07003490int
3491qla24xx_nvram_config(scsi_qla_host_t *ha)
3492{
David Miller4e08df32007-04-16 12:37:43 -07003493 int rval;
Andrew Vasquez01071092005-07-06 10:31:37 -07003494 struct init_cb_24xx *icb;
3495 struct nvram_24xx *nv;
3496 uint32_t *dptr;
3497 uint8_t *dptr1, *dptr2;
3498 uint32_t chksum;
3499 uint16_t cnt;
3500
David Miller4e08df32007-04-16 12:37:43 -07003501 rval = QLA_SUCCESS;
Andrew Vasquez01071092005-07-06 10:31:37 -07003502 icb = (struct init_cb_24xx *)ha->init_cb;
3503 nv = (struct nvram_24xx *)ha->request_ring;
3504
3505 /* Determine NVRAM starting address. */
3506 ha->nvram_size = sizeof(struct nvram_24xx);
3507 ha->nvram_base = FA_NVRAM_FUNC0_ADDR;
andrew.vasquez@qlogic.com6f641792006-03-09 14:27:34 -08003508 ha->vpd_size = FA_NVRAM_VPD_SIZE;
3509 ha->vpd_base = FA_NVRAM_VPD0_ADDR;
3510 if (PCI_FUNC(ha->pdev->devfn)) {
Andrew Vasquez01071092005-07-06 10:31:37 -07003511 ha->nvram_base = FA_NVRAM_FUNC1_ADDR;
andrew.vasquez@qlogic.com6f641792006-03-09 14:27:34 -08003512 ha->vpd_base = FA_NVRAM_VPD1_ADDR;
3513 }
Andrew Vasquez01071092005-07-06 10:31:37 -07003514
3515 /* Get NVRAM data and calculate checksum. */
3516 dptr = (uint32_t *)nv;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07003517 ha->isp_ops->read_nvram(ha, (uint8_t *)dptr, ha->nvram_base,
Andrew Vasquez01071092005-07-06 10:31:37 -07003518 ha->nvram_size);
3519 for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++)
3520 chksum += le32_to_cpu(*dptr++);
3521
3522 DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
3523 DEBUG5(qla2x00_dump_buffer((uint8_t *)ha->request_ring,
3524 ha->nvram_size));
3525
3526 /* Bad NVRAM data, set defaults parameters. */
3527 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
3528 || nv->id[3] != ' ' ||
3529 nv->nvram_version < __constant_cpu_to_le16(ICB_VERSION)) {
3530 /* Reset NVRAM data. */
3531 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
3532 "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
3533 le16_to_cpu(nv->nvram_version));
David Miller4e08df32007-04-16 12:37:43 -07003534 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
3535 "invalid -- WWPN) defaults.\n");
3536
3537 /*
3538 * Set default initialization control block.
3539 */
3540 memset(nv, 0, ha->nvram_size);
3541 nv->nvram_version = __constant_cpu_to_le16(ICB_VERSION);
3542 nv->version = __constant_cpu_to_le16(ICB_VERSION);
3543 nv->frame_payload_size = __constant_cpu_to_le16(2048);
3544 nv->execution_throttle = __constant_cpu_to_le16(0xFFFF);
3545 nv->exchange_count = __constant_cpu_to_le16(0);
3546 nv->hard_address = __constant_cpu_to_le16(124);
3547 nv->port_name[0] = 0x21;
3548 nv->port_name[1] = 0x00 + PCI_FUNC(ha->pdev->devfn);
3549 nv->port_name[2] = 0x00;
3550 nv->port_name[3] = 0xe0;
3551 nv->port_name[4] = 0x8b;
3552 nv->port_name[5] = 0x1c;
3553 nv->port_name[6] = 0x55;
3554 nv->port_name[7] = 0x86;
3555 nv->node_name[0] = 0x20;
3556 nv->node_name[1] = 0x00;
3557 nv->node_name[2] = 0x00;
3558 nv->node_name[3] = 0xe0;
3559 nv->node_name[4] = 0x8b;
3560 nv->node_name[5] = 0x1c;
3561 nv->node_name[6] = 0x55;
3562 nv->node_name[7] = 0x86;
3563 qla24xx_nvram_wwn_from_ofw(ha, nv);
3564 nv->login_retry_count = __constant_cpu_to_le16(8);
3565 nv->interrupt_delay_timer = __constant_cpu_to_le16(0);
3566 nv->login_timeout = __constant_cpu_to_le16(0);
3567 nv->firmware_options_1 =
3568 __constant_cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
3569 nv->firmware_options_2 = __constant_cpu_to_le32(2 << 4);
3570 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_12);
3571 nv->firmware_options_3 = __constant_cpu_to_le32(2 << 13);
3572 nv->host_p = __constant_cpu_to_le32(BIT_11|BIT_10);
3573 nv->efi_parameters = __constant_cpu_to_le32(0);
3574 nv->reset_delay = 5;
3575 nv->max_luns_per_target = __constant_cpu_to_le16(128);
3576 nv->port_down_retry_count = __constant_cpu_to_le16(30);
3577 nv->link_down_timeout = __constant_cpu_to_le16(30);
3578
3579 rval = 1;
Andrew Vasquez01071092005-07-06 10:31:37 -07003580 }
3581
3582 /* Reset Initialization control block */
3583 memset(icb, 0, sizeof(struct init_cb_24xx));
3584
3585 /* Copy 1st segment. */
3586 dptr1 = (uint8_t *)icb;
3587 dptr2 = (uint8_t *)&nv->version;
3588 cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
3589 while (cnt--)
3590 *dptr1++ = *dptr2++;
3591
3592 icb->login_retry_count = nv->login_retry_count;
Andrew Vasquez3ea66e22006-06-23 16:11:27 -07003593 icb->link_down_on_nos = nv->link_down_on_nos;
Andrew Vasquez01071092005-07-06 10:31:37 -07003594
3595 /* Copy 2nd segment. */
3596 dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
3597 dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
3598 cnt = (uint8_t *)&icb->reserved_3 -
3599 (uint8_t *)&icb->interrupt_delay_timer;
3600 while (cnt--)
3601 *dptr1++ = *dptr2++;
3602
3603 /*
3604 * Setup driver NVRAM options.
3605 */
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08003606 qla2x00_set_model_info(ha, nv->model_name, sizeof(nv->model_name),
3607 "QLA2462");
Andrew Vasquez01071092005-07-06 10:31:37 -07003608
Andrew Vasquez5341e862006-05-17 15:09:16 -07003609 /* Use alternate WWN? */
3610 if (nv->host_p & __constant_cpu_to_le32(BIT_15)) {
3611 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
3612 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
3613 }
3614
Andrew Vasquez01071092005-07-06 10:31:37 -07003615 /* Prepare nodename */
Andrew Vasquezfd0e7e42006-05-17 15:09:11 -07003616 if ((icb->firmware_options_1 & __constant_cpu_to_le32(BIT_14)) == 0) {
Andrew Vasquez01071092005-07-06 10:31:37 -07003617 /*
3618 * Firmware will apply the following mask if the nodename was
3619 * not provided.
3620 */
3621 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
3622 icb->node_name[0] &= 0xF0;
3623 }
3624
3625 /* Set host adapter parameters. */
3626 ha->flags.disable_risc_code_load = 0;
Andrew Vasquez0c8c39a2006-12-13 19:20:30 -08003627 ha->flags.enable_lip_reset = 0;
3628 ha->flags.enable_lip_full_login =
3629 le32_to_cpu(nv->host_p) & BIT_10 ? 1: 0;
3630 ha->flags.enable_target_reset =
3631 le32_to_cpu(nv->host_p) & BIT_11 ? 1: 0;
Andrew Vasquez01071092005-07-06 10:31:37 -07003632 ha->flags.enable_led_scheme = 0;
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07003633 ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
Andrew Vasquez01071092005-07-06 10:31:37 -07003634
Andrew Vasquezfd0e7e42006-05-17 15:09:11 -07003635 ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
3636 (BIT_6 | BIT_5 | BIT_4)) >> 4;
Andrew Vasquez01071092005-07-06 10:31:37 -07003637
3638 memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
3639 sizeof(ha->fw_seriallink_options24));
3640
3641 /* save HBA serial number */
3642 ha->serial0 = icb->port_name[5];
3643 ha->serial1 = icb->port_name[6];
3644 ha->serial2 = icb->port_name[7];
3645 ha->node_name = icb->node_name;
3646 ha->port_name = icb->port_name;
3647
andrew.vasquez@qlogic.combc8fb3c2006-01-13 17:05:42 -08003648 icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
3649
Andrew Vasquez01071092005-07-06 10:31:37 -07003650 ha->retry_count = le16_to_cpu(nv->login_retry_count);
3651
3652 /* Set minimum login_timeout to 4 seconds. */
3653 if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
3654 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
3655 if (le16_to_cpu(nv->login_timeout) < 4)
3656 nv->login_timeout = __constant_cpu_to_le16(4);
3657 ha->login_timeout = le16_to_cpu(nv->login_timeout);
3658 icb->login_timeout = cpu_to_le16(nv->login_timeout);
3659
3660 /* Set minimum RATOV to 200 tenths of a second. */
3661 ha->r_a_tov = 200;
3662
3663 ha->loop_reset_delay = nv->reset_delay;
3664
3665 /* Link Down Timeout = 0:
3666 *
3667 * When Port Down timer expires we will start returning
3668 * I/O's to OS with "DID_NO_CONNECT".
3669 *
3670 * Link Down Timeout != 0:
3671 *
3672 * The driver waits for the link to come up after link down
3673 * before returning I/Os to OS with "DID_NO_CONNECT".
3674 */
3675 if (le16_to_cpu(nv->link_down_timeout) == 0) {
3676 ha->loop_down_abort_time =
3677 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
3678 } else {
3679 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
3680 ha->loop_down_abort_time =
3681 (LOOP_DOWN_TIME - ha->link_down_timeout);
3682 }
3683
3684 /* Need enough time to try and get the port back. */
3685 ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
3686 if (qlport_down_retry)
3687 ha->port_down_retry_count = qlport_down_retry;
3688
3689 /* Set login_retry_count */
3690 ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
3691 if (ha->port_down_retry_count ==
3692 le16_to_cpu(nv->port_down_retry_count) &&
3693 ha->port_down_retry_count > 3)
3694 ha->login_retry_count = ha->port_down_retry_count;
3695 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
3696 ha->login_retry_count = ha->port_down_retry_count;
3697 if (ql2xloginretrycount)
3698 ha->login_retry_count = ql2xloginretrycount;
3699
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07003700 /* Enable ZIO. */
3701 if (!ha->flags.init_done) {
3702 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
3703 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
3704 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
3705 le16_to_cpu(icb->interrupt_delay_timer): 2;
3706 }
3707 icb->firmware_options_2 &= __constant_cpu_to_le32(
3708 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
3709 ha->flags.process_response_queue = 0;
3710 if (ha->zio_mode != QLA_ZIO_DISABLED) {
andrew.vasquez@qlogic.com4a59f712006-03-09 14:27:39 -08003711 ha->zio_mode = QLA_ZIO_MODE_6;
3712
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07003713 DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer delay "
3714 "(%d us).\n", ha->host_no, ha->zio_mode,
3715 ha->zio_timer * 100));
3716 qla_printk(KERN_INFO, ha,
3717 "ZIO mode %d enabled; timer delay (%d us).\n",
3718 ha->zio_mode, ha->zio_timer * 100);
3719
3720 icb->firmware_options_2 |= cpu_to_le32(
3721 (uint32_t)ha->zio_mode);
3722 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
3723 ha->flags.process_response_queue = 1;
3724 }
3725
David Miller4e08df32007-04-16 12:37:43 -07003726 if (rval) {
3727 DEBUG2_3(printk(KERN_WARNING
3728 "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
3729 }
3730 return (rval);
Andrew Vasquez01071092005-07-06 10:31:37 -07003731}
3732
Adrian Bunk413975a2006-06-30 02:33:06 -07003733static int
Andrew Vasquezd1c61902006-05-17 15:09:00 -07003734qla24xx_load_risc_flash(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3735{
3736 int rval;
3737 int segments, fragment;
3738 uint32_t faddr;
3739 uint32_t *dcode, dlen;
3740 uint32_t risc_addr;
3741 uint32_t risc_size;
3742 uint32_t i;
3743
3744 rval = QLA_SUCCESS;
3745
3746 segments = FA_RISC_CODE_SEGMENTS;
3747 faddr = FA_RISC_CODE_ADDR;
3748 dcode = (uint32_t *)ha->request_ring;
3749 *srisc_addr = 0;
3750
3751 /* Validate firmware image by checking version. */
3752 qla24xx_read_flash_data(ha, dcode, faddr + 4, 4);
3753 for (i = 0; i < 4; i++)
3754 dcode[i] = be32_to_cpu(dcode[i]);
3755 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
3756 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
3757 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
3758 dcode[3] == 0)) {
3759 qla_printk(KERN_WARNING, ha,
3760 "Unable to verify integrity of flash firmware image!\n");
3761 qla_printk(KERN_WARNING, ha,
3762 "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
3763 dcode[1], dcode[2], dcode[3]);
3764
3765 return QLA_FUNCTION_FAILED;
3766 }
3767
3768 while (segments && rval == QLA_SUCCESS) {
3769 /* Read segment's load information. */
3770 qla24xx_read_flash_data(ha, dcode, faddr, 4);
3771
3772 risc_addr = be32_to_cpu(dcode[2]);
3773 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
3774 risc_size = be32_to_cpu(dcode[3]);
3775
3776 fragment = 0;
3777 while (risc_size > 0 && rval == QLA_SUCCESS) {
3778 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
3779 if (dlen > risc_size)
3780 dlen = risc_size;
3781
3782 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3783 "addr %x, number of dwords 0x%x, offset 0x%x.\n",
3784 ha->host_no, risc_addr, dlen, faddr));
3785
3786 qla24xx_read_flash_data(ha, dcode, faddr, dlen);
3787 for (i = 0; i < dlen; i++)
3788 dcode[i] = swab32(dcode[i]);
3789
3790 rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3791 dlen);
3792 if (rval) {
3793 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3794 "segment %d of firmware\n", ha->host_no,
3795 fragment));
3796 qla_printk(KERN_WARNING, ha,
3797 "[ERROR] Failed to load segment %d of "
3798 "firmware\n", fragment);
3799 break;
3800 }
3801
3802 faddr += dlen;
3803 risc_addr += dlen;
3804 risc_size -= dlen;
3805 fragment++;
3806 }
3807
3808 /* Next segment. */
3809 segments--;
3810 }
3811
3812 return rval;
3813}
3814
Andrew Vasquezd1c61902006-05-17 15:09:00 -07003815#define QLA_FW_URL "ftp://ftp.qlogic.com/outgoing/linux/firmware/"
3816
Andrew Vasquez01071092005-07-06 10:31:37 -07003817int
Andrew Vasquez54333832005-11-09 15:49:04 -08003818qla2x00_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3819{
3820 int rval;
3821 int i, fragment;
3822 uint16_t *wcode, *fwcode;
3823 uint32_t risc_addr, risc_size, fwclen, wlen, *seg;
3824 struct fw_blob *blob;
3825
3826 /* Load firmware blob. */
3827 blob = qla2x00_request_firmware(ha);
3828 if (!blob) {
3829 qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
Andrew Vasquezd1c61902006-05-17 15:09:00 -07003830 qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
3831 "from: " QLA_FW_URL ".\n");
Andrew Vasquez54333832005-11-09 15:49:04 -08003832 return QLA_FUNCTION_FAILED;
3833 }
3834
3835 rval = QLA_SUCCESS;
3836
3837 wcode = (uint16_t *)ha->request_ring;
3838 *srisc_addr = 0;
3839 fwcode = (uint16_t *)blob->fw->data;
3840 fwclen = 0;
3841
3842 /* Validate firmware image by checking version. */
3843 if (blob->fw->size < 8 * sizeof(uint16_t)) {
3844 qla_printk(KERN_WARNING, ha,
3845 "Unable to verify integrity of firmware image (%Zd)!\n",
3846 blob->fw->size);
3847 goto fail_fw_integrity;
3848 }
3849 for (i = 0; i < 4; i++)
3850 wcode[i] = be16_to_cpu(fwcode[i + 4]);
3851 if ((wcode[0] == 0xffff && wcode[1] == 0xffff && wcode[2] == 0xffff &&
3852 wcode[3] == 0xffff) || (wcode[0] == 0 && wcode[1] == 0 &&
3853 wcode[2] == 0 && wcode[3] == 0)) {
3854 qla_printk(KERN_WARNING, ha,
3855 "Unable to verify integrity of firmware image!\n");
3856 qla_printk(KERN_WARNING, ha,
3857 "Firmware data: %04x %04x %04x %04x!\n", wcode[0],
3858 wcode[1], wcode[2], wcode[3]);
3859 goto fail_fw_integrity;
3860 }
3861
3862 seg = blob->segs;
3863 while (*seg && rval == QLA_SUCCESS) {
3864 risc_addr = *seg;
3865 *srisc_addr = *srisc_addr == 0 ? *seg : *srisc_addr;
3866 risc_size = be16_to_cpu(fwcode[3]);
3867
3868 /* Validate firmware image size. */
3869 fwclen += risc_size * sizeof(uint16_t);
3870 if (blob->fw->size < fwclen) {
3871 qla_printk(KERN_WARNING, ha,
3872 "Unable to verify integrity of firmware image "
3873 "(%Zd)!\n", blob->fw->size);
3874 goto fail_fw_integrity;
3875 }
3876
3877 fragment = 0;
3878 while (risc_size > 0 && rval == QLA_SUCCESS) {
3879 wlen = (uint16_t)(ha->fw_transfer_size >> 1);
3880 if (wlen > risc_size)
3881 wlen = risc_size;
3882
3883 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3884 "addr %x, number of words 0x%x.\n", ha->host_no,
3885 risc_addr, wlen));
3886
3887 for (i = 0; i < wlen; i++)
3888 wcode[i] = swab16(fwcode[i]);
3889
3890 rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3891 wlen);
3892 if (rval) {
3893 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3894 "segment %d of firmware\n", ha->host_no,
3895 fragment));
3896 qla_printk(KERN_WARNING, ha,
3897 "[ERROR] Failed to load segment %d of "
3898 "firmware\n", fragment);
3899 break;
3900 }
3901
3902 fwcode += wlen;
3903 risc_addr += wlen;
3904 risc_size -= wlen;
3905 fragment++;
3906 }
3907
3908 /* Next segment. */
3909 seg++;
3910 }
3911 return rval;
3912
3913fail_fw_integrity:
3914 return QLA_FUNCTION_FAILED;
3915}
3916
3917int
3918qla24xx_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
Andrew Vasquez01071092005-07-06 10:31:37 -07003919{
3920 int rval;
3921 int segments, fragment;
3922 uint32_t *dcode, dlen;
3923 uint32_t risc_addr;
3924 uint32_t risc_size;
3925 uint32_t i;
Andrew Vasquez54333832005-11-09 15:49:04 -08003926 struct fw_blob *blob;
Andrew Vasquez01071092005-07-06 10:31:37 -07003927 uint32_t *fwcode, fwclen;
3928
Andrew Vasquez54333832005-11-09 15:49:04 -08003929 /* Load firmware blob. */
3930 blob = qla2x00_request_firmware(ha);
3931 if (!blob) {
3932 qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
Andrew Vasquezd1c61902006-05-17 15:09:00 -07003933 qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
3934 "from: " QLA_FW_URL ".\n");
3935
3936 /* Try to load RISC code from flash. */
3937 qla_printk(KERN_ERR, ha, "Attempting to load (potentially "
3938 "outdated) firmware from flash.\n");
3939 return qla24xx_load_risc_flash(ha, srisc_addr);
Andrew Vasquez01071092005-07-06 10:31:37 -07003940 }
3941
3942 rval = QLA_SUCCESS;
3943
3944 segments = FA_RISC_CODE_SEGMENTS;
3945 dcode = (uint32_t *)ha->request_ring;
3946 *srisc_addr = 0;
Andrew Vasquez54333832005-11-09 15:49:04 -08003947 fwcode = (uint32_t *)blob->fw->data;
Andrew Vasquez01071092005-07-06 10:31:37 -07003948 fwclen = 0;
3949
3950 /* Validate firmware image by checking version. */
Andrew Vasquez54333832005-11-09 15:49:04 -08003951 if (blob->fw->size < 8 * sizeof(uint32_t)) {
Andrew Vasquez01071092005-07-06 10:31:37 -07003952 qla_printk(KERN_WARNING, ha,
Andrew Vasquez54333832005-11-09 15:49:04 -08003953 "Unable to verify integrity of firmware image (%Zd)!\n",
3954 blob->fw->size);
Andrew Vasquez01071092005-07-06 10:31:37 -07003955 goto fail_fw_integrity;
3956 }
3957 for (i = 0; i < 4; i++)
3958 dcode[i] = be32_to_cpu(fwcode[i + 4]);
3959 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
3960 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
3961 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
3962 dcode[3] == 0)) {
3963 qla_printk(KERN_WARNING, ha,
Andrew Vasquez54333832005-11-09 15:49:04 -08003964 "Unable to verify integrity of firmware image!\n");
Andrew Vasquez01071092005-07-06 10:31:37 -07003965 qla_printk(KERN_WARNING, ha,
3966 "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
3967 dcode[1], dcode[2], dcode[3]);
3968 goto fail_fw_integrity;
3969 }
3970
3971 while (segments && rval == QLA_SUCCESS) {
3972 risc_addr = be32_to_cpu(fwcode[2]);
3973 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
3974 risc_size = be32_to_cpu(fwcode[3]);
3975
3976 /* Validate firmware image size. */
3977 fwclen += risc_size * sizeof(uint32_t);
Andrew Vasquez54333832005-11-09 15:49:04 -08003978 if (blob->fw->size < fwclen) {
Andrew Vasquez01071092005-07-06 10:31:37 -07003979 qla_printk(KERN_WARNING, ha,
Andrew Vasquez54333832005-11-09 15:49:04 -08003980 "Unable to verify integrity of firmware image "
3981 "(%Zd)!\n", blob->fw->size);
3982
Andrew Vasquez01071092005-07-06 10:31:37 -07003983 goto fail_fw_integrity;
3984 }
3985
3986 fragment = 0;
3987 while (risc_size > 0 && rval == QLA_SUCCESS) {
3988 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
3989 if (dlen > risc_size)
3990 dlen = risc_size;
3991
3992 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3993 "addr %x, number of dwords 0x%x.\n", ha->host_no,
3994 risc_addr, dlen));
3995
3996 for (i = 0; i < dlen; i++)
3997 dcode[i] = swab32(fwcode[i]);
3998
andrew.vasquez@qlogic.com590f98e2006-01-13 17:05:37 -08003999 rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
4000 dlen);
Andrew Vasquez01071092005-07-06 10:31:37 -07004001 if (rval) {
4002 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
4003 "segment %d of firmware\n", ha->host_no,
4004 fragment));
4005 qla_printk(KERN_WARNING, ha,
4006 "[ERROR] Failed to load segment %d of "
4007 "firmware\n", fragment);
4008 break;
4009 }
4010
4011 fwcode += dlen;
4012 risc_addr += dlen;
4013 risc_size -= dlen;
4014 fragment++;
4015 }
4016
4017 /* Next segment. */
4018 segments--;
4019 }
Andrew Vasquez01071092005-07-06 10:31:37 -07004020 return rval;
4021
4022fail_fw_integrity:
Andrew Vasquez01071092005-07-06 10:31:37 -07004023 return QLA_FUNCTION_FAILED;
Andrew Vasquez01071092005-07-06 10:31:37 -07004024}
Andrew Vasquez18c6c122006-10-13 09:33:38 -07004025
4026void
4027qla2x00_try_to_stop_firmware(scsi_qla_host_t *ha)
4028{
4029 int ret, retries;
4030
Andrew Vasqueze4289242007-07-19 15:05:56 -07004031 if (!IS_FWI2_CAPABLE(ha))
Andrew Vasquez18c6c122006-10-13 09:33:38 -07004032 return;
Andrew Vasquez75edf812007-05-07 07:43:00 -07004033 if (!ha->fw_major_version)
4034 return;
Andrew Vasquez18c6c122006-10-13 09:33:38 -07004035
4036 ret = qla2x00_stop_firmware(ha);
4037 for (retries = 5; ret != QLA_SUCCESS && retries ; retries--) {
4038 qla2x00_reset_chip(ha);
4039 if (qla2x00_chip_diag(ha) != QLA_SUCCESS)
4040 continue;
4041 if (qla2x00_setup_chip(ha) != QLA_SUCCESS)
4042 continue;
4043 qla_printk(KERN_INFO, ha,
4044 "Attempting retry of stop-firmware command...\n");
4045 ret = qla2x00_stop_firmware(ha);
4046 }
4047}
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07004048
4049int
4050qla24xx_configure_vhba(scsi_qla_host_t *ha)
4051{
4052 int rval = QLA_SUCCESS;
4053 uint16_t mb[MAILBOX_REGISTER_COUNT];
4054
4055 if (!ha->parent)
4056 return -EINVAL;
4057
4058 rval = qla2x00_fw_ready(ha);
4059 if (rval == QLA_SUCCESS) {
4060 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
4061 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
4062 }
4063
4064 ha->flags.management_server_logged_in = 0;
4065
4066 /* Login to SNS first */
4067 qla24xx_login_fabric(ha, NPH_SNS, 0xff, 0xff, 0xfc,
4068 mb, BIT_1);
4069 if (mb[0] != MBS_COMMAND_COMPLETE) {
4070 DEBUG15(qla_printk(KERN_INFO, ha,
4071 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
4072 "mb[2]=%x mb[6]=%x mb[7]=%x\n", NPH_SNS,
4073 mb[0], mb[1], mb[2], mb[6], mb[7]));
4074 return (QLA_FUNCTION_FAILED);
4075 }
4076
4077 atomic_set(&ha->loop_down_timer, 0);
4078 atomic_set(&ha->loop_state, LOOP_UP);
4079 set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
4080 set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
4081 rval = qla2x00_loop_resync(ha);
4082
4083 return rval;
4084}