qla_init.c revision cb8dacbf1110d8bd39413f3116ff1720f757854e
1/*
2 * QLogic Fibre Channel HBA Driver
3 * Copyright (c)  2003-2008 QLogic Corporation
4 *
5 * See LICENSE.qla2xxx for copyright and licensing details.
6 */
7#include "qla_def.h"
8
9#include <linux/delay.h>
10#include <linux/vmalloc.h>
11
12#include "qla_devtbl.h"
13
14#ifdef CONFIG_SPARC
15#include <asm/prom.h>
16#endif
17
18/* 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*/
29static int qla2x00_isp_firmware(scsi_qla_host_t *);
30static 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 *);
36static int qla2x00_configure_loop(scsi_qla_host_t *);
37static int qla2x00_configure_local_loop(scsi_qla_host_t *);
38static 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 *);
43
44static int qla2x00_restart_isp(scsi_qla_host_t *);
45
46static int qla2x00_find_new_loop_id(scsi_qla_host_t *ha, fc_port_t *dev);
47
48/****************************************************************************/
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;
66
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);
72	ha->device_flags = DFLG_NO_CABLE;
73	ha->dpc_flags = 0;
74	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;
79	set_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags);
80
81	qla_printk(KERN_INFO, ha, "Configuring PCI space...\n");
82	rval = ha->isp_ops->pci_config(ha);
83	if (rval) {
84		DEBUG2(printk("scsi(%ld): Unable to configure PCI space.\n",
85		    ha->host_no));
86		return (rval);
87	}
88
89	ha->isp_ops->reset_chip(ha);
90
91	ha->isp_ops->get_flash_version(ha, ha->request_ring);
92
93	qla_printk(KERN_INFO, ha, "Configure NVRAM parameters...\n");
94
95	ha->isp_ops->nvram_config(ha);
96
97	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
108	qla_printk(KERN_INFO, ha, "Verifying loaded RISC code...\n");
109
110	if (qla2x00_isp_firmware(ha) != QLA_SUCCESS) {
111		rval = ha->isp_ops->chip_diag(ha);
112		if (rval)
113			return (rval);
114		rval = qla2x00_setup_chip(ha);
115		if (rval)
116			return (rval);
117	}
118	rval = qla2x00_init_rings(ha);
119
120	return (rval);
121}
122
123/**
124 * qla2100_pci_config() - Setup ISP21xx PCI configuration registers.
125 * @ha: HA context
126 *
127 * Returns 0 on success.
128 */
129int
130qla2100_pci_config(scsi_qla_host_t *ha)
131{
132	uint16_t w;
133	uint32_t d;
134	unsigned long flags;
135	struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
136
137	pci_set_master(ha->pdev);
138	pci_try_set_mwi(ha->pdev);
139
140	pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
141	w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
142	pci_write_config_word(ha->pdev, PCI_COMMAND, w);
143
144	/* Reset expansion ROM address decode enable */
145	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);
148
149	/* Get PCI bus information. */
150	spin_lock_irqsave(&ha->hardware_lock, flags);
151	ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
152	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{
166	uint16_t	w;
167	uint32_t	d;
168	unsigned long   flags = 0;
169	uint32_t	cnt;
170	struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
171
172	pci_set_master(ha->pdev);
173	pci_try_set_mwi(ha->pdev);
174
175	pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
176	w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
177
178	if (IS_QLA2322(ha) || IS_QLA6322(ha))
179		w &= ~PCI_COMMAND_INTX_DISABLE;
180	pci_write_config_word(ha->pdev, PCI_COMMAND, w);
181
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. */
193		WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
194		for (cnt = 0; cnt < 30000; cnt++) {
195			if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
196				break;
197
198			udelay(10);
199		}
200
201		/* Select FPM registers. */
202		WRT_REG_WORD(&reg->ctrl_status, 0x20);
203		RD_REG_WORD(&reg->ctrl_status);
204
205		/* Get the fb rev level */
206		ha->fb_rev = RD_FB_CMD_REG(ha, reg);
207
208		if (ha->fb_rev == FPM_2300)
209			pci_clear_mwi(ha->pdev);
210
211		/* Deselect FPM registers. */
212		WRT_REG_WORD(&reg->ctrl_status, 0x0);
213		RD_REG_WORD(&reg->ctrl_status);
214
215		/* Release RISC module. */
216		WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
217		for (cnt = 0; cnt < 30000; cnt++) {
218			if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) == 0)
219				break;
220
221			udelay(10);
222		}
223
224		spin_unlock_irqrestore(&ha->hardware_lock, flags);
225	}
226
227	pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
228
229	/* Reset expansion ROM address decode enable */
230	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);
233
234	/* Get PCI bus information. */
235	spin_lock_irqsave(&ha->hardware_lock, flags);
236	ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
237	spin_unlock_irqrestore(&ha->hardware_lock, flags);
238
239	return QLA_SUCCESS;
240}
241
242/**
243 * 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{
251	uint16_t w;
252	uint32_t d;
253	unsigned long flags = 0;
254	struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
255
256	pci_set_master(ha->pdev);
257	pci_try_set_mwi(ha->pdev);
258
259	pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
260	w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
261	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). */
267	if (pci_find_capability(ha->pdev, PCI_CAP_ID_PCIX))
268		pcix_set_mmrbc(ha->pdev, 2048);
269
270	/* PCIe -- adjust Maximum Read Request Size (2048). */
271	if (pci_find_capability(ha->pdev, PCI_CAP_ID_EXP))
272		pcie_set_readrq(ha->pdev, 2048);
273
274	/* Reset expansion ROM address decode enable */
275	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);
278
279	ha->chip_revision = ha->pdev->revision;
280
281	/* 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/**
290 * 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/**
324 * 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 */
335	rval = QLA_FUNCTION_FAILED;
336
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. */
343		rval = qla2x00_verify_checksum(ha, ha->fw_srisc_address);
344	}
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 */
360void
361qla2x00_reset_chip(scsi_qla_host_t *ha)
362{
363	unsigned long   flags = 0;
364	struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
365	uint32_t	cnt;
366	uint16_t	cmd;
367
368	ha->isp_ops->disable_intrs(ha);
369
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++) {
473			if (RD_MAILBOX_REG(ha, reg, 0) != MBS_BUSY)
474				break;
475
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/**
495 * qla24xx_reset_risc() - Perform full reset of ISP24xx RISC.
496 * @ha: HA context
497 *
498 * Returns 0 on success.
499 */
500static inline void
501qla24xx_reset_risc(scsi_qla_host_t *ha)
502{
503	int hw_evt = 0;
504	unsigned long flags = 0;
505	struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
506	uint32_t cnt, d2;
507	uint16_t wd;
508
509	spin_lock_irqsave(&ha->hardware_lock, flags);
510
511	/* Reset RISC. */
512	WRT_REG_DWORD(&reg->ctrl_status, CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
513	for (cnt = 0; cnt < 30000; cnt++) {
514		if ((RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE) == 0)
515			break;
516
517		udelay(10);
518	}
519
520	WRT_REG_DWORD(&reg->ctrl_status,
521	    CSRX_ISP_SOFT_RESET|CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
522	pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
523
524	udelay(100);
525	/* Wait for firmware to complete NVRAM accesses. */
526	d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
527	for (cnt = 10000 ; cnt && d2; cnt--) {
528		udelay(5);
529		d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
530		barrier();
531	}
532	if (cnt == 0)
533		hw_evt = 1;
534
535	/* Wait for soft-reset to complete. */
536	d2 = RD_REG_DWORD(&reg->ctrl_status);
537	for (cnt = 6000000 ; cnt && (d2 & CSRX_ISP_SOFT_RESET); cnt--) {
538		udelay(5);
539		d2 = RD_REG_DWORD(&reg->ctrl_status);
540		barrier();
541	}
542	if (cnt == 0 || hw_evt)
543		qla2xxx_hw_event_log(ha, HW_EVENT_RESET_ERR,
544		    RD_REG_WORD(&reg->mailbox1), RD_REG_WORD(&reg->mailbox2),
545		    RD_REG_WORD(&reg->mailbox3));
546
547	WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
548	RD_REG_DWORD(&reg->hccr);
549
550	WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
551	RD_REG_DWORD(&reg->hccr);
552
553	WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_RESET);
554	RD_REG_DWORD(&reg->hccr);
555
556	d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
557	for (cnt = 6000000 ; cnt && d2; cnt--) {
558		udelay(5);
559		d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
560		barrier();
561	}
562
563	spin_unlock_irqrestore(&ha->hardware_lock, flags);
564}
565
566/**
567 * qla24xx_reset_chip() - Reset ISP24xx chip.
568 * @ha: HA context
569 *
570 * Returns 0 on success.
571 */
572void
573qla24xx_reset_chip(scsi_qla_host_t *ha)
574{
575	ha->isp_ops->disable_intrs(ha);
576
577	/* Perform RISC reset. */
578	qla24xx_reset_risc(ha);
579}
580
581/**
582 * qla2x00_chip_diag() - Test chip for proper operation.
583 * @ha: HA context
584 *
585 * Returns 0 on success.
586 */
587int
588qla2x00_chip_diag(scsi_qla_host_t *ha)
589{
590	int		rval;
591	struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
592	unsigned long	flags = 0;
593	uint16_t	data;
594	uint32_t	cnt;
595	uint16_t	mb[5];
596
597	/* Assume a failed state */
598	rval = QLA_FUNCTION_FAILED;
599
600	DEBUG3(printk("scsi(%ld): Testing device at %lx.\n",
601	    ha->host_no, (u_long)&reg->flash_address));
602
603	spin_lock_irqsave(&ha->hardware_lock, flags);
604
605	/* Reset ISP chip. */
606	WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
607
608	/*
609	 * We need to have a delay here since the card will not respond while
610	 * in reset causing an MCA on some architectures.
611	 */
612	udelay(20);
613	data = qla2x00_debounce_register(&reg->ctrl_status);
614	for (cnt = 6000000 ; cnt && (data & CSR_ISP_SOFT_RESET); cnt--) {
615		udelay(5);
616		data = RD_REG_WORD(&reg->ctrl_status);
617		barrier();
618	}
619
620	if (!cnt)
621		goto chip_diag_failed;
622
623	DEBUG3(printk("scsi(%ld): Reset register cleared by chip reset\n",
624	    ha->host_no));
625
626	/* Reset RISC processor. */
627	WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
628	WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
629
630	/* Workaround for QLA2312 PCI parity error */
631	if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
632		data = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 0));
633		for (cnt = 6000000; cnt && (data == MBS_BUSY); cnt--) {
634			udelay(5);
635			data = RD_MAILBOX_REG(ha, reg, 0);
636			barrier();
637		}
638	} else
639		udelay(10);
640
641	if (!cnt)
642		goto chip_diag_failed;
643
644	/* Check product ID of chip */
645	DEBUG3(printk("scsi(%ld): Checking product ID of chip\n", ha->host_no));
646
647	mb[1] = RD_MAILBOX_REG(ha, reg, 1);
648	mb[2] = RD_MAILBOX_REG(ha, reg, 2);
649	mb[3] = RD_MAILBOX_REG(ha, reg, 3);
650	mb[4] = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 4));
651	if (mb[1] != PROD_ID_1 || (mb[2] != PROD_ID_2 && mb[2] != PROD_ID_2a) ||
652	    mb[3] != PROD_ID_3) {
653		qla_printk(KERN_WARNING, ha,
654		    "Wrong product ID = 0x%x,0x%x,0x%x\n", mb[1], mb[2], mb[3]);
655
656		goto chip_diag_failed;
657	}
658	ha->product_id[0] = mb[1];
659	ha->product_id[1] = mb[2];
660	ha->product_id[2] = mb[3];
661	ha->product_id[3] = mb[4];
662
663	/* Adjust fw RISC transfer size */
664	if (ha->request_q_length > 1024)
665		ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
666	else
667		ha->fw_transfer_size = REQUEST_ENTRY_SIZE *
668		    ha->request_q_length;
669
670	if (IS_QLA2200(ha) &&
671	    RD_MAILBOX_REG(ha, reg, 7) == QLA2200A_RISC_ROM_VER) {
672		/* Limit firmware transfer size with a 2200A */
673		DEBUG3(printk("scsi(%ld): Found QLA2200A chip.\n",
674		    ha->host_no));
675
676		ha->device_type |= DT_ISP2200A;
677		ha->fw_transfer_size = 128;
678	}
679
680	/* Wrap Incoming Mailboxes Test. */
681	spin_unlock_irqrestore(&ha->hardware_lock, flags);
682
683	DEBUG3(printk("scsi(%ld): Checking mailboxes.\n", ha->host_no));
684	rval = qla2x00_mbx_reg_test(ha);
685	if (rval) {
686		DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
687		    ha->host_no));
688		qla_printk(KERN_WARNING, ha,
689		    "Failed mailbox send register test\n");
690	}
691	else {
692		/* Flag a successful rval */
693		rval = QLA_SUCCESS;
694	}
695	spin_lock_irqsave(&ha->hardware_lock, flags);
696
697chip_diag_failed:
698	if (rval)
699		DEBUG2_3(printk("scsi(%ld): Chip diagnostics **** FAILED "
700		    "****\n", ha->host_no));
701
702	spin_unlock_irqrestore(&ha->hardware_lock, flags);
703
704	return (rval);
705}
706
707/**
708 * qla24xx_chip_diag() - Test ISP24xx for proper operation.
709 * @ha: HA context
710 *
711 * Returns 0 on success.
712 */
713int
714qla24xx_chip_diag(scsi_qla_host_t *ha)
715{
716	int rval;
717
718	/* Perform RISC reset. */
719	qla24xx_reset_risc(ha);
720
721	ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
722
723	rval = qla2x00_mbx_reg_test(ha);
724	if (rval) {
725		DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
726		    ha->host_no));
727		qla_printk(KERN_WARNING, ha,
728		    "Failed mailbox send register test\n");
729	} else {
730		/* Flag a successful rval */
731		rval = QLA_SUCCESS;
732	}
733
734	return rval;
735}
736
737void
738qla2x00_alloc_fw_dump(scsi_qla_host_t *ha)
739{
740	int rval;
741	uint32_t dump_size, fixed_size, mem_size, req_q_size, rsp_q_size,
742	    eft_size, fce_size;
743	dma_addr_t tc_dma;
744	void *tc;
745
746	if (ha->fw_dump) {
747		qla_printk(KERN_WARNING, ha,
748		    "Firmware dump previously allocated.\n");
749		return;
750	}
751
752	ha->fw_dumped = 0;
753	fixed_size = mem_size = eft_size = fce_size = 0;
754	if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
755		fixed_size = sizeof(struct qla2100_fw_dump);
756	} else if (IS_QLA23XX(ha)) {
757		fixed_size = offsetof(struct qla2300_fw_dump, data_ram);
758		mem_size = (ha->fw_memory_size - 0x11000 + 1) *
759		    sizeof(uint16_t);
760	} else if (IS_FWI2_CAPABLE(ha)) {
761		fixed_size = IS_QLA25XX(ha) ?
762		    offsetof(struct qla25xx_fw_dump, ext_mem):
763		    offsetof(struct qla24xx_fw_dump, ext_mem);
764		mem_size = (ha->fw_memory_size - 0x100000 + 1) *
765		    sizeof(uint32_t);
766
767		/* Allocate memory for Extended Trace Buffer. */
768		tc = dma_alloc_coherent(&ha->pdev->dev, EFT_SIZE, &tc_dma,
769		    GFP_KERNEL);
770		if (!tc) {
771			qla_printk(KERN_WARNING, ha, "Unable to allocate "
772			    "(%d KB) for EFT.\n", EFT_SIZE / 1024);
773			goto cont_alloc;
774		}
775
776		memset(tc, 0, EFT_SIZE);
777		rval = qla2x00_enable_eft_trace(ha, tc_dma, EFT_NUM_BUFFERS);
778		if (rval) {
779			qla_printk(KERN_WARNING, ha, "Unable to initialize "
780			    "EFT (%d).\n", rval);
781			dma_free_coherent(&ha->pdev->dev, EFT_SIZE, tc,
782			    tc_dma);
783			goto cont_alloc;
784		}
785
786		qla_printk(KERN_INFO, ha, "Allocated (%d KB) for EFT...\n",
787		    EFT_SIZE / 1024);
788
789		eft_size = EFT_SIZE;
790		ha->eft_dma = tc_dma;
791		ha->eft = tc;
792
793		/* Allocate memory for Fibre Channel Event Buffer. */
794		if (!IS_QLA25XX(ha))
795			goto cont_alloc;
796
797		tc = dma_alloc_coherent(&ha->pdev->dev, FCE_SIZE, &tc_dma,
798		    GFP_KERNEL);
799		if (!tc) {
800			qla_printk(KERN_WARNING, ha, "Unable to allocate "
801			    "(%d KB) for FCE.\n", FCE_SIZE / 1024);
802			goto cont_alloc;
803		}
804
805		memset(tc, 0, FCE_SIZE);
806		rval = qla2x00_enable_fce_trace(ha, tc_dma, FCE_NUM_BUFFERS,
807		    ha->fce_mb, &ha->fce_bufs);
808		if (rval) {
809			qla_printk(KERN_WARNING, ha, "Unable to initialize "
810			    "FCE (%d).\n", rval);
811			dma_free_coherent(&ha->pdev->dev, FCE_SIZE, tc,
812			    tc_dma);
813			ha->flags.fce_enabled = 0;
814			goto cont_alloc;
815		}
816
817		qla_printk(KERN_INFO, ha, "Allocated (%d KB) for FCE...\n",
818		    FCE_SIZE / 1024);
819
820		fce_size = sizeof(struct qla2xxx_fce_chain) + EFT_SIZE;
821		ha->flags.fce_enabled = 1;
822		ha->fce_dma = tc_dma;
823		ha->fce = tc;
824	}
825cont_alloc:
826	req_q_size = ha->request_q_length * sizeof(request_t);
827	rsp_q_size = ha->response_q_length * sizeof(response_t);
828
829	dump_size = offsetof(struct qla2xxx_fw_dump, isp);
830	dump_size += fixed_size + mem_size + req_q_size + rsp_q_size +
831	    eft_size + fce_size;
832
833	ha->fw_dump = vmalloc(dump_size);
834	if (!ha->fw_dump) {
835		qla_printk(KERN_WARNING, ha, "Unable to allocate (%d KB) for "
836		    "firmware dump!!!\n", dump_size / 1024);
837
838		if (ha->eft) {
839			dma_free_coherent(&ha->pdev->dev, eft_size, ha->eft,
840			    ha->eft_dma);
841			ha->eft = NULL;
842			ha->eft_dma = 0;
843		}
844		return;
845	}
846
847	qla_printk(KERN_INFO, ha, "Allocated (%d KB) for firmware dump...\n",
848	    dump_size / 1024);
849
850	ha->fw_dump_len = dump_size;
851	ha->fw_dump->signature[0] = 'Q';
852	ha->fw_dump->signature[1] = 'L';
853	ha->fw_dump->signature[2] = 'G';
854	ha->fw_dump->signature[3] = 'C';
855	ha->fw_dump->version = __constant_htonl(1);
856
857	ha->fw_dump->fixed_size = htonl(fixed_size);
858	ha->fw_dump->mem_size = htonl(mem_size);
859	ha->fw_dump->req_q_size = htonl(req_q_size);
860	ha->fw_dump->rsp_q_size = htonl(rsp_q_size);
861
862	ha->fw_dump->eft_size = htonl(eft_size);
863	ha->fw_dump->eft_addr_l = htonl(LSD(ha->eft_dma));
864	ha->fw_dump->eft_addr_h = htonl(MSD(ha->eft_dma));
865
866	ha->fw_dump->header_size =
867	    htonl(offsetof(struct qla2xxx_fw_dump, isp));
868}
869
870/**
871 * qla2x00_resize_request_q() - Resize request queue given available ISP memory.
872 * @ha: HA context
873 *
874 * Returns 0 on success.
875 */
876static void
877qla2x00_resize_request_q(scsi_qla_host_t *ha)
878{
879	int rval;
880	uint16_t fw_iocb_cnt = 0;
881	uint16_t request_q_length = REQUEST_ENTRY_CNT_2XXX_EXT_MEM;
882	dma_addr_t request_dma;
883	request_t *request_ring;
884
885	/* Valid only on recent ISPs. */
886	if (IS_QLA2100(ha) || IS_QLA2200(ha))
887		return;
888
889	/* Retrieve IOCB counts available to the firmware. */
890	rval = qla2x00_get_resource_cnts(ha, NULL, NULL, NULL, &fw_iocb_cnt,
891	    &ha->max_npiv_vports);
892	if (rval)
893		return;
894	/* No point in continuing if current settings are sufficient. */
895	if (fw_iocb_cnt < 1024)
896		return;
897	if (ha->request_q_length >= request_q_length)
898		return;
899
900	/* Attempt to claim larger area for request queue. */
901	request_ring = dma_alloc_coherent(&ha->pdev->dev,
902	    (request_q_length + 1) * sizeof(request_t), &request_dma,
903	    GFP_KERNEL);
904	if (request_ring == NULL)
905		return;
906
907	/* Resize successful, report extensions. */
908	qla_printk(KERN_INFO, ha, "Extended memory detected (%d KB)...\n",
909	    (ha->fw_memory_size + 1) / 1024);
910	qla_printk(KERN_INFO, ha, "Resizing request queue depth "
911	    "(%d -> %d)...\n", ha->request_q_length, request_q_length);
912
913	/* Clear old allocations. */
914	dma_free_coherent(&ha->pdev->dev,
915	    (ha->request_q_length + 1) * sizeof(request_t), ha->request_ring,
916	    ha->request_dma);
917
918	/* Begin using larger queue. */
919	ha->request_q_length = request_q_length;
920	ha->request_ring = request_ring;
921	ha->request_dma = request_dma;
922}
923
924/**
925 * qla2x00_setup_chip() - Load and start RISC firmware.
926 * @ha: HA context
927 *
928 * Returns 0 on success.
929 */
930static int
931qla2x00_setup_chip(scsi_qla_host_t *ha)
932{
933	int rval;
934	uint32_t srisc_address = 0;
935	struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
936	unsigned long flags;
937
938	if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
939		/* Disable SRAM, Instruction RAM and GP RAM parity.  */
940		spin_lock_irqsave(&ha->hardware_lock, flags);
941		WRT_REG_WORD(&reg->hccr, (HCCR_ENABLE_PARITY + 0x0));
942		RD_REG_WORD(&reg->hccr);
943		spin_unlock_irqrestore(&ha->hardware_lock, flags);
944	}
945
946	/* Load firmware sequences */
947	rval = ha->isp_ops->load_risc(ha, &srisc_address);
948	if (rval == QLA_SUCCESS) {
949		DEBUG(printk("scsi(%ld): Verifying Checksum of loaded RISC "
950		    "code.\n", ha->host_no));
951
952		rval = qla2x00_verify_checksum(ha, srisc_address);
953		if (rval == QLA_SUCCESS) {
954			/* Start firmware execution. */
955			DEBUG(printk("scsi(%ld): Checksum OK, start "
956			    "firmware.\n", ha->host_no));
957
958			rval = qla2x00_execute_fw(ha, srisc_address);
959			/* Retrieve firmware information. */
960			if (rval == QLA_SUCCESS && ha->fw_major_version == 0) {
961				qla2x00_get_fw_version(ha,
962				    &ha->fw_major_version,
963				    &ha->fw_minor_version,
964				    &ha->fw_subminor_version,
965				    &ha->fw_attributes, &ha->fw_memory_size);
966				qla2x00_resize_request_q(ha);
967				ha->flags.npiv_supported = 0;
968				if ((IS_QLA24XX(ha) || IS_QLA25XX(ha)) &&
969				    (ha->fw_attributes & BIT_2)) {
970					ha->flags.npiv_supported = 1;
971					if ((!ha->max_npiv_vports) ||
972					    ((ha->max_npiv_vports + 1) %
973					    MIN_MULTI_ID_FABRIC))
974						ha->max_npiv_vports =
975						    MIN_MULTI_ID_FABRIC - 1;
976				}
977
978				if (ql2xallocfwdump)
979					qla2x00_alloc_fw_dump(ha);
980			}
981		} else {
982			DEBUG2(printk(KERN_INFO
983			    "scsi(%ld): ISP Firmware failed checksum.\n",
984			    ha->host_no));
985		}
986	}
987
988	if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
989		/* Enable proper parity. */
990		spin_lock_irqsave(&ha->hardware_lock, flags);
991		if (IS_QLA2300(ha))
992			/* SRAM parity */
993			WRT_REG_WORD(&reg->hccr, HCCR_ENABLE_PARITY + 0x1);
994		else
995			/* SRAM, Instruction RAM and GP RAM parity */
996			WRT_REG_WORD(&reg->hccr, HCCR_ENABLE_PARITY + 0x7);
997		RD_REG_WORD(&reg->hccr);
998		spin_unlock_irqrestore(&ha->hardware_lock, flags);
999	}
1000
1001	if (rval) {
1002		DEBUG2_3(printk("scsi(%ld): Setup chip **** FAILED ****.\n",
1003		    ha->host_no));
1004	}
1005
1006	return (rval);
1007}
1008
1009/**
1010 * qla2x00_init_response_q_entries() - Initializes response queue entries.
1011 * @ha: HA context
1012 *
1013 * Beginning of request ring has initialization control block already built
1014 * by nvram config routine.
1015 *
1016 * Returns 0 on success.
1017 */
1018static void
1019qla2x00_init_response_q_entries(scsi_qla_host_t *ha)
1020{
1021	uint16_t cnt;
1022	response_t *pkt;
1023
1024	pkt = ha->response_ring_ptr;
1025	for (cnt = 0; cnt < ha->response_q_length; cnt++) {
1026		pkt->signature = RESPONSE_PROCESSED;
1027		pkt++;
1028	}
1029
1030}
1031
1032/**
1033 * qla2x00_update_fw_options() - Read and process firmware options.
1034 * @ha: HA context
1035 *
1036 * Returns 0 on success.
1037 */
1038void
1039qla2x00_update_fw_options(scsi_qla_host_t *ha)
1040{
1041	uint16_t swing, emphasis, tx_sens, rx_sens;
1042
1043	memset(ha->fw_options, 0, sizeof(ha->fw_options));
1044	qla2x00_get_fw_options(ha, ha->fw_options);
1045
1046	if (IS_QLA2100(ha) || IS_QLA2200(ha))
1047		return;
1048
1049	/* Serial Link options. */
1050	DEBUG3(printk("scsi(%ld): Serial link options:\n",
1051	    ha->host_no));
1052	DEBUG3(qla2x00_dump_buffer((uint8_t *)&ha->fw_seriallink_options,
1053	    sizeof(ha->fw_seriallink_options)));
1054
1055	ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
1056	if (ha->fw_seriallink_options[3] & BIT_2) {
1057		ha->fw_options[1] |= FO1_SET_EMPHASIS_SWING;
1058
1059		/*  1G settings */
1060		swing = ha->fw_seriallink_options[2] & (BIT_2 | BIT_1 | BIT_0);
1061		emphasis = (ha->fw_seriallink_options[2] &
1062		    (BIT_4 | BIT_3)) >> 3;
1063		tx_sens = ha->fw_seriallink_options[0] &
1064		    (BIT_3 | BIT_2 | BIT_1 | BIT_0);
1065		rx_sens = (ha->fw_seriallink_options[0] &
1066		    (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
1067		ha->fw_options[10] = (emphasis << 14) | (swing << 8);
1068		if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
1069			if (rx_sens == 0x0)
1070				rx_sens = 0x3;
1071			ha->fw_options[10] |= (tx_sens << 4) | rx_sens;
1072		} else if (IS_QLA2322(ha) || IS_QLA6322(ha))
1073			ha->fw_options[10] |= BIT_5 |
1074			    ((rx_sens & (BIT_1 | BIT_0)) << 2) |
1075			    (tx_sens & (BIT_1 | BIT_0));
1076
1077		/*  2G settings */
1078		swing = (ha->fw_seriallink_options[2] &
1079		    (BIT_7 | BIT_6 | BIT_5)) >> 5;
1080		emphasis = ha->fw_seriallink_options[3] & (BIT_1 | BIT_0);
1081		tx_sens = ha->fw_seriallink_options[1] &
1082		    (BIT_3 | BIT_2 | BIT_1 | BIT_0);
1083		rx_sens = (ha->fw_seriallink_options[1] &
1084		    (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
1085		ha->fw_options[11] = (emphasis << 14) | (swing << 8);
1086		if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
1087			if (rx_sens == 0x0)
1088				rx_sens = 0x3;
1089			ha->fw_options[11] |= (tx_sens << 4) | rx_sens;
1090		} else if (IS_QLA2322(ha) || IS_QLA6322(ha))
1091			ha->fw_options[11] |= BIT_5 |
1092			    ((rx_sens & (BIT_1 | BIT_0)) << 2) |
1093			    (tx_sens & (BIT_1 | BIT_0));
1094	}
1095
1096	/* FCP2 options. */
1097	/*  Return command IOCBs without waiting for an ABTS to complete. */
1098	ha->fw_options[3] |= BIT_13;
1099
1100	/* LED scheme. */
1101	if (ha->flags.enable_led_scheme)
1102		ha->fw_options[2] |= BIT_12;
1103
1104	/* Detect ISP6312. */
1105	if (IS_QLA6312(ha))
1106		ha->fw_options[2] |= BIT_13;
1107
1108	/* Update firmware options. */
1109	qla2x00_set_fw_options(ha, ha->fw_options);
1110}
1111
1112void
1113qla24xx_update_fw_options(scsi_qla_host_t *ha)
1114{
1115	int rval;
1116
1117	/* Update Serial Link options. */
1118	if ((le16_to_cpu(ha->fw_seriallink_options24[0]) & BIT_0) == 0)
1119		return;
1120
1121	rval = qla2x00_set_serdes_params(ha,
1122	    le16_to_cpu(ha->fw_seriallink_options24[1]),
1123	    le16_to_cpu(ha->fw_seriallink_options24[2]),
1124	    le16_to_cpu(ha->fw_seriallink_options24[3]));
1125	if (rval != QLA_SUCCESS) {
1126		qla_printk(KERN_WARNING, ha,
1127		    "Unable to update Serial Link options (%x).\n", rval);
1128	}
1129}
1130
1131void
1132qla2x00_config_rings(struct scsi_qla_host *ha)
1133{
1134	struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1135
1136	/* Setup ring parameters in initialization control block. */
1137	ha->init_cb->request_q_outpointer = __constant_cpu_to_le16(0);
1138	ha->init_cb->response_q_inpointer = __constant_cpu_to_le16(0);
1139	ha->init_cb->request_q_length = cpu_to_le16(ha->request_q_length);
1140	ha->init_cb->response_q_length = cpu_to_le16(ha->response_q_length);
1141	ha->init_cb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
1142	ha->init_cb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
1143	ha->init_cb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
1144	ha->init_cb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
1145
1146	WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), 0);
1147	WRT_REG_WORD(ISP_REQ_Q_OUT(ha, reg), 0);
1148	WRT_REG_WORD(ISP_RSP_Q_IN(ha, reg), 0);
1149	WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), 0);
1150	RD_REG_WORD(ISP_RSP_Q_OUT(ha, reg));		/* PCI Posting. */
1151}
1152
1153void
1154qla24xx_config_rings(struct scsi_qla_host *ha)
1155{
1156	struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1157	struct init_cb_24xx *icb;
1158
1159	/* Setup ring parameters in initialization control block. */
1160	icb = (struct init_cb_24xx *)ha->init_cb;
1161	icb->request_q_outpointer = __constant_cpu_to_le16(0);
1162	icb->response_q_inpointer = __constant_cpu_to_le16(0);
1163	icb->request_q_length = cpu_to_le16(ha->request_q_length);
1164	icb->response_q_length = cpu_to_le16(ha->response_q_length);
1165	icb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
1166	icb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
1167	icb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
1168	icb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
1169
1170	WRT_REG_DWORD(&reg->req_q_in, 0);
1171	WRT_REG_DWORD(&reg->req_q_out, 0);
1172	WRT_REG_DWORD(&reg->rsp_q_in, 0);
1173	WRT_REG_DWORD(&reg->rsp_q_out, 0);
1174	RD_REG_DWORD(&reg->rsp_q_out);
1175}
1176
1177/**
1178 * qla2x00_init_rings() - Initializes firmware.
1179 * @ha: HA context
1180 *
1181 * Beginning of request ring has initialization control block already built
1182 * by nvram config routine.
1183 *
1184 * Returns 0 on success.
1185 */
1186static int
1187qla2x00_init_rings(scsi_qla_host_t *ha)
1188{
1189	int	rval;
1190	unsigned long flags = 0;
1191	int cnt;
1192	struct mid_init_cb_24xx *mid_init_cb =
1193	    (struct mid_init_cb_24xx *) ha->init_cb;
1194
1195	spin_lock_irqsave(&ha->hardware_lock, flags);
1196
1197	/* Clear outstanding commands array. */
1198	for (cnt = 0; cnt < MAX_OUTSTANDING_COMMANDS; cnt++)
1199		ha->outstanding_cmds[cnt] = NULL;
1200
1201	ha->current_outstanding_cmd = 0;
1202
1203	/* Clear RSCN queue. */
1204	ha->rscn_in_ptr = 0;
1205	ha->rscn_out_ptr = 0;
1206
1207	/* Initialize firmware. */
1208	ha->request_ring_ptr  = ha->request_ring;
1209	ha->req_ring_index    = 0;
1210	ha->req_q_cnt         = ha->request_q_length;
1211	ha->response_ring_ptr = ha->response_ring;
1212	ha->rsp_ring_index    = 0;
1213
1214	/* Initialize response queue entries */
1215	qla2x00_init_response_q_entries(ha);
1216
1217	ha->isp_ops->config_rings(ha);
1218
1219	spin_unlock_irqrestore(&ha->hardware_lock, flags);
1220
1221	/* Update any ISP specific firmware options before initialization. */
1222	ha->isp_ops->update_fw_options(ha);
1223
1224	DEBUG(printk("scsi(%ld): Issue init firmware.\n", ha->host_no));
1225
1226	if (ha->flags.npiv_supported)
1227		mid_init_cb->count = cpu_to_le16(ha->max_npiv_vports);
1228
1229	mid_init_cb->options = __constant_cpu_to_le16(BIT_1);
1230
1231	rval = qla2x00_init_firmware(ha, ha->init_cb_size);
1232	if (rval) {
1233		DEBUG2_3(printk("scsi(%ld): Init firmware **** FAILED ****.\n",
1234		    ha->host_no));
1235	} else {
1236		DEBUG3(printk("scsi(%ld): Init firmware -- success.\n",
1237		    ha->host_no));
1238	}
1239
1240	return (rval);
1241}
1242
1243/**
1244 * qla2x00_fw_ready() - Waits for firmware ready.
1245 * @ha: HA context
1246 *
1247 * Returns 0 on success.
1248 */
1249static int
1250qla2x00_fw_ready(scsi_qla_host_t *ha)
1251{
1252	int		rval;
1253	unsigned long	wtime, mtime;
1254	uint16_t	min_wait;	/* Minimum wait time if loop is down */
1255	uint16_t	wait_time;	/* Wait time if loop is coming ready */
1256	uint16_t	fw_state;
1257
1258	rval = QLA_SUCCESS;
1259
1260	/* 20 seconds for loop down. */
1261	min_wait = 20;
1262
1263	/*
1264	 * Firmware should take at most one RATOV to login, plus 5 seconds for
1265	 * our own processing.
1266	 */
1267	if ((wait_time = (ha->retry_count*ha->login_timeout) + 5) < min_wait) {
1268		wait_time = min_wait;
1269	}
1270
1271	/* Min wait time if loop down */
1272	mtime = jiffies + (min_wait * HZ);
1273
1274	/* wait time before firmware ready */
1275	wtime = jiffies + (wait_time * HZ);
1276
1277	/* Wait for ISP to finish LIP */
1278	if (!ha->flags.init_done)
1279 		qla_printk(KERN_INFO, ha, "Waiting for LIP to complete...\n");
1280
1281	DEBUG3(printk("scsi(%ld): Waiting for LIP to complete...\n",
1282	    ha->host_no));
1283
1284	do {
1285		rval = qla2x00_get_firmware_state(ha, &fw_state);
1286		if (rval == QLA_SUCCESS) {
1287			if (fw_state < FSTATE_LOSS_OF_SYNC) {
1288				ha->device_flags &= ~DFLG_NO_CABLE;
1289			}
1290			if (fw_state == FSTATE_READY) {
1291				DEBUG(printk("scsi(%ld): F/W Ready - OK \n",
1292				    ha->host_no));
1293
1294				qla2x00_get_retry_cnt(ha, &ha->retry_count,
1295				    &ha->login_timeout, &ha->r_a_tov);
1296
1297				rval = QLA_SUCCESS;
1298				break;
1299			}
1300
1301			rval = QLA_FUNCTION_FAILED;
1302
1303			if (atomic_read(&ha->loop_down_timer) &&
1304			    fw_state != FSTATE_READY) {
1305				/* Loop down. Timeout on min_wait for states
1306				 * other than Wait for Login.
1307				 */
1308				if (time_after_eq(jiffies, mtime)) {
1309					qla_printk(KERN_INFO, ha,
1310					    "Cable is unplugged...\n");
1311
1312					ha->device_flags |= DFLG_NO_CABLE;
1313					break;
1314				}
1315			}
1316		} else {
1317			/* Mailbox cmd failed. Timeout on min_wait. */
1318			if (time_after_eq(jiffies, mtime))
1319				break;
1320		}
1321
1322		if (time_after_eq(jiffies, wtime))
1323			break;
1324
1325		/* Delay for a while */
1326		msleep(500);
1327
1328		DEBUG3(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
1329		    ha->host_no, fw_state, jiffies));
1330	} while (1);
1331
1332	DEBUG(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
1333	    ha->host_no, fw_state, jiffies));
1334
1335	if (rval) {
1336		DEBUG2_3(printk("scsi(%ld): Firmware ready **** FAILED ****.\n",
1337		    ha->host_no));
1338	}
1339
1340	return (rval);
1341}
1342
1343/*
1344*  qla2x00_configure_hba
1345*      Setup adapter context.
1346*
1347* Input:
1348*      ha = adapter state pointer.
1349*
1350* Returns:
1351*      0 = success
1352*
1353* Context:
1354*      Kernel context.
1355*/
1356static int
1357qla2x00_configure_hba(scsi_qla_host_t *ha)
1358{
1359	int       rval;
1360	uint16_t      loop_id;
1361	uint16_t      topo;
1362	uint16_t      sw_cap;
1363	uint8_t       al_pa;
1364	uint8_t       area;
1365	uint8_t       domain;
1366	char		connect_type[22];
1367
1368	/* Get host addresses. */
1369	rval = qla2x00_get_adapter_id(ha,
1370	    &loop_id, &al_pa, &area, &domain, &topo, &sw_cap);
1371	if (rval != QLA_SUCCESS) {
1372		if (LOOP_TRANSITION(ha) || atomic_read(&ha->loop_down_timer) ||
1373		    (rval == QLA_COMMAND_ERROR && loop_id == 0x7)) {
1374			DEBUG2(printk("%s(%ld) Loop is in a transition state\n",
1375			    __func__, ha->host_no));
1376		} else {
1377			qla_printk(KERN_WARNING, ha,
1378			    "ERROR -- Unable to get host loop ID.\n");
1379			set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
1380		}
1381		return (rval);
1382	}
1383
1384	if (topo == 4) {
1385		qla_printk(KERN_INFO, ha,
1386			"Cannot get topology - retrying.\n");
1387		return (QLA_FUNCTION_FAILED);
1388	}
1389
1390	ha->loop_id = loop_id;
1391
1392	/* initialize */
1393	ha->min_external_loopid = SNS_FIRST_LOOP_ID;
1394	ha->operating_mode = LOOP;
1395	ha->switch_cap = 0;
1396
1397	switch (topo) {
1398	case 0:
1399		DEBUG3(printk("scsi(%ld): HBA in NL topology.\n",
1400		    ha->host_no));
1401		ha->current_topology = ISP_CFG_NL;
1402		strcpy(connect_type, "(Loop)");
1403		break;
1404
1405	case 1:
1406		DEBUG3(printk("scsi(%ld): HBA in FL topology.\n",
1407		    ha->host_no));
1408		ha->switch_cap = sw_cap;
1409		ha->current_topology = ISP_CFG_FL;
1410		strcpy(connect_type, "(FL_Port)");
1411		break;
1412
1413	case 2:
1414		DEBUG3(printk("scsi(%ld): HBA in N P2P topology.\n",
1415		    ha->host_no));
1416		ha->operating_mode = P2P;
1417		ha->current_topology = ISP_CFG_N;
1418		strcpy(connect_type, "(N_Port-to-N_Port)");
1419		break;
1420
1421	case 3:
1422		DEBUG3(printk("scsi(%ld): HBA in F P2P topology.\n",
1423		    ha->host_no));
1424		ha->switch_cap = sw_cap;
1425		ha->operating_mode = P2P;
1426		ha->current_topology = ISP_CFG_F;
1427		strcpy(connect_type, "(F_Port)");
1428		break;
1429
1430	default:
1431		DEBUG3(printk("scsi(%ld): HBA in unknown topology %x. "
1432		    "Using NL.\n",
1433		    ha->host_no, topo));
1434		ha->current_topology = ISP_CFG_NL;
1435		strcpy(connect_type, "(Loop)");
1436		break;
1437	}
1438
1439	/* Save Host port and loop ID. */
1440	/* byte order - Big Endian */
1441	ha->d_id.b.domain = domain;
1442	ha->d_id.b.area = area;
1443	ha->d_id.b.al_pa = al_pa;
1444
1445	if (!ha->flags.init_done)
1446 		qla_printk(KERN_INFO, ha,
1447		    "Topology - %s, Host Loop address 0x%x\n",
1448 		    connect_type, ha->loop_id);
1449
1450	if (rval) {
1451		DEBUG2_3(printk("scsi(%ld): FAILED.\n", ha->host_no));
1452	} else {
1453		DEBUG3(printk("scsi(%ld): exiting normally.\n", ha->host_no));
1454	}
1455
1456	return(rval);
1457}
1458
1459static inline void
1460qla2x00_set_model_info(scsi_qla_host_t *ha, uint8_t *model, size_t len, char *def)
1461{
1462	char *st, *en;
1463	uint16_t index;
1464
1465	if (memcmp(model, BINZERO, len) != 0) {
1466		strncpy(ha->model_number, model, len);
1467		st = en = ha->model_number;
1468		en += len - 1;
1469		while (en > st) {
1470			if (*en != 0x20 && *en != 0x00)
1471				break;
1472			*en-- = '\0';
1473		}
1474
1475		index = (ha->pdev->subsystem_device & 0xff);
1476		if (ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
1477		    index < QLA_MODEL_NAMES)
1478			ha->model_desc = qla2x00_model_name[index * 2 + 1];
1479	} else {
1480		index = (ha->pdev->subsystem_device & 0xff);
1481		if (ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
1482		    index < QLA_MODEL_NAMES) {
1483			strcpy(ha->model_number,
1484			    qla2x00_model_name[index * 2]);
1485			ha->model_desc = qla2x00_model_name[index * 2 + 1];
1486		} else {
1487			strcpy(ha->model_number, def);
1488		}
1489	}
1490}
1491
1492/* On sparc systems, obtain port and node WWN from firmware
1493 * properties.
1494 */
1495static void qla2xxx_nvram_wwn_from_ofw(scsi_qla_host_t *ha, nvram_t *nv)
1496{
1497#ifdef CONFIG_SPARC
1498	struct pci_dev *pdev = ha->pdev;
1499	struct device_node *dp = pci_device_to_OF_node(pdev);
1500	const u8 *val;
1501	int len;
1502
1503	val = of_get_property(dp, "port-wwn", &len);
1504	if (val && len >= WWN_SIZE)
1505		memcpy(nv->port_name, val, WWN_SIZE);
1506
1507	val = of_get_property(dp, "node-wwn", &len);
1508	if (val && len >= WWN_SIZE)
1509		memcpy(nv->node_name, val, WWN_SIZE);
1510#endif
1511}
1512
1513/*
1514* NVRAM configuration for ISP 2xxx
1515*
1516* Input:
1517*      ha                = adapter block pointer.
1518*
1519* Output:
1520*      initialization control block in response_ring
1521*      host adapters parameters in host adapter block
1522*
1523* Returns:
1524*      0 = success.
1525*/
1526int
1527qla2x00_nvram_config(scsi_qla_host_t *ha)
1528{
1529	int             rval;
1530	uint8_t         chksum = 0;
1531	uint16_t        cnt;
1532	uint8_t         *dptr1, *dptr2;
1533	init_cb_t       *icb = ha->init_cb;
1534	nvram_t         *nv = ha->nvram;
1535	uint8_t         *ptr = ha->nvram;
1536	struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1537
1538	rval = QLA_SUCCESS;
1539
1540	/* Determine NVRAM starting address. */
1541	ha->nvram_size = sizeof(nvram_t);
1542	ha->nvram_base = 0;
1543	if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha))
1544		if ((RD_REG_WORD(&reg->ctrl_status) >> 14) == 1)
1545			ha->nvram_base = 0x80;
1546
1547	/* Get NVRAM data and calculate checksum. */
1548	ha->isp_ops->read_nvram(ha, ptr, ha->nvram_base, ha->nvram_size);
1549	for (cnt = 0, chksum = 0; cnt < ha->nvram_size; cnt++)
1550		chksum += *ptr++;
1551
1552	DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
1553	DEBUG5(qla2x00_dump_buffer((uint8_t *)nv, ha->nvram_size));
1554
1555	/* Bad NVRAM data, set defaults parameters. */
1556	if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' ||
1557	    nv->id[2] != 'P' || nv->id[3] != ' ' || nv->nvram_version < 1) {
1558		/* Reset NVRAM data. */
1559		qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
1560		    "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
1561		    nv->nvram_version);
1562		qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
1563		    "invalid -- WWPN) defaults.\n");
1564
1565		if (chksum)
1566			qla2xxx_hw_event_log(ha, HW_EVENT_NVRAM_CHKSUM_ERR, 0,
1567			    MSW(chksum), LSW(chksum));
1568
1569		/*
1570		 * Set default initialization control block.
1571		 */
1572		memset(nv, 0, ha->nvram_size);
1573		nv->parameter_block_version = ICB_VERSION;
1574
1575		if (IS_QLA23XX(ha)) {
1576			nv->firmware_options[0] = BIT_2 | BIT_1;
1577			nv->firmware_options[1] = BIT_7 | BIT_5;
1578			nv->add_firmware_options[0] = BIT_5;
1579			nv->add_firmware_options[1] = BIT_5 | BIT_4;
1580			nv->frame_payload_size = __constant_cpu_to_le16(2048);
1581			nv->special_options[1] = BIT_7;
1582		} else if (IS_QLA2200(ha)) {
1583			nv->firmware_options[0] = BIT_2 | BIT_1;
1584			nv->firmware_options[1] = BIT_7 | BIT_5;
1585			nv->add_firmware_options[0] = BIT_5;
1586			nv->add_firmware_options[1] = BIT_5 | BIT_4;
1587			nv->frame_payload_size = __constant_cpu_to_le16(1024);
1588		} else if (IS_QLA2100(ha)) {
1589			nv->firmware_options[0] = BIT_3 | BIT_1;
1590			nv->firmware_options[1] = BIT_5;
1591			nv->frame_payload_size = __constant_cpu_to_le16(1024);
1592		}
1593
1594		nv->max_iocb_allocation = __constant_cpu_to_le16(256);
1595		nv->execution_throttle = __constant_cpu_to_le16(16);
1596		nv->retry_count = 8;
1597		nv->retry_delay = 1;
1598
1599		nv->port_name[0] = 33;
1600		nv->port_name[3] = 224;
1601		nv->port_name[4] = 139;
1602
1603		qla2xxx_nvram_wwn_from_ofw(ha, nv);
1604
1605		nv->login_timeout = 4;
1606
1607		/*
1608		 * Set default host adapter parameters
1609		 */
1610		nv->host_p[1] = BIT_2;
1611		nv->reset_delay = 5;
1612		nv->port_down_retry_count = 8;
1613		nv->max_luns_per_target = __constant_cpu_to_le16(8);
1614		nv->link_down_timeout = 60;
1615
1616		rval = 1;
1617	}
1618
1619#if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
1620	/*
1621	 * The SN2 does not provide BIOS emulation which means you can't change
1622	 * potentially bogus BIOS settings. Force the use of default settings
1623	 * for link rate and frame size.  Hope that the rest of the settings
1624	 * are valid.
1625	 */
1626	if (ia64_platform_is("sn2")) {
1627		nv->frame_payload_size = __constant_cpu_to_le16(2048);
1628		if (IS_QLA23XX(ha))
1629			nv->special_options[1] = BIT_7;
1630	}
1631#endif
1632
1633	/* Reset Initialization control block */
1634	memset(icb, 0, ha->init_cb_size);
1635
1636	/*
1637	 * Setup driver NVRAM options.
1638	 */
1639	nv->firmware_options[0] |= (BIT_6 | BIT_1);
1640	nv->firmware_options[0] &= ~(BIT_5 | BIT_4);
1641	nv->firmware_options[1] |= (BIT_5 | BIT_0);
1642	nv->firmware_options[1] &= ~BIT_4;
1643
1644	if (IS_QLA23XX(ha)) {
1645		nv->firmware_options[0] |= BIT_2;
1646		nv->firmware_options[0] &= ~BIT_3;
1647		nv->add_firmware_options[1] |= BIT_5 | BIT_4;
1648
1649		if (IS_QLA2300(ha)) {
1650			if (ha->fb_rev == FPM_2310) {
1651				strcpy(ha->model_number, "QLA2310");
1652			} else {
1653				strcpy(ha->model_number, "QLA2300");
1654			}
1655		} else {
1656			qla2x00_set_model_info(ha, nv->model_number,
1657			    sizeof(nv->model_number), "QLA23xx");
1658		}
1659	} else if (IS_QLA2200(ha)) {
1660		nv->firmware_options[0] |= BIT_2;
1661		/*
1662		 * 'Point-to-point preferred, else loop' is not a safe
1663		 * connection mode setting.
1664		 */
1665		if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
1666		    (BIT_5 | BIT_4)) {
1667			/* Force 'loop preferred, else point-to-point'. */
1668			nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
1669			nv->add_firmware_options[0] |= BIT_5;
1670		}
1671		strcpy(ha->model_number, "QLA22xx");
1672	} else /*if (IS_QLA2100(ha))*/ {
1673		strcpy(ha->model_number, "QLA2100");
1674	}
1675
1676	/*
1677	 * Copy over NVRAM RISC parameter block to initialization control block.
1678	 */
1679	dptr1 = (uint8_t *)icb;
1680	dptr2 = (uint8_t *)&nv->parameter_block_version;
1681	cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
1682	while (cnt--)
1683		*dptr1++ = *dptr2++;
1684
1685	/* Copy 2nd half. */
1686	dptr1 = (uint8_t *)icb->add_firmware_options;
1687	cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
1688	while (cnt--)
1689		*dptr1++ = *dptr2++;
1690
1691	/* Use alternate WWN? */
1692	if (nv->host_p[1] & BIT_7) {
1693		memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
1694		memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
1695	}
1696
1697	/* Prepare nodename */
1698	if ((icb->firmware_options[1] & BIT_6) == 0) {
1699		/*
1700		 * Firmware will apply the following mask if the nodename was
1701		 * not provided.
1702		 */
1703		memcpy(icb->node_name, icb->port_name, WWN_SIZE);
1704		icb->node_name[0] &= 0xF0;
1705	}
1706
1707	/*
1708	 * Set host adapter parameters.
1709	 */
1710	if (nv->host_p[0] & BIT_7)
1711		ql2xextended_error_logging = 1;
1712	ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
1713	/* Always load RISC code on non ISP2[12]00 chips. */
1714	if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
1715		ha->flags.disable_risc_code_load = 0;
1716	ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
1717	ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
1718	ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
1719	ha->flags.enable_led_scheme = (nv->special_options[1] & BIT_4) ? 1 : 0;
1720	ha->flags.disable_serdes = 0;
1721
1722	ha->operating_mode =
1723	    (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
1724
1725	memcpy(ha->fw_seriallink_options, nv->seriallink_options,
1726	    sizeof(ha->fw_seriallink_options));
1727
1728	/* save HBA serial number */
1729	ha->serial0 = icb->port_name[5];
1730	ha->serial1 = icb->port_name[6];
1731	ha->serial2 = icb->port_name[7];
1732	ha->node_name = icb->node_name;
1733	ha->port_name = icb->port_name;
1734
1735	icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
1736
1737	ha->retry_count = nv->retry_count;
1738
1739	/* Set minimum login_timeout to 4 seconds. */
1740	if (nv->login_timeout < ql2xlogintimeout)
1741		nv->login_timeout = ql2xlogintimeout;
1742	if (nv->login_timeout < 4)
1743		nv->login_timeout = 4;
1744	ha->login_timeout = nv->login_timeout;
1745	icb->login_timeout = nv->login_timeout;
1746
1747	/* Set minimum RATOV to 100 tenths of a second. */
1748	ha->r_a_tov = 100;
1749
1750	ha->loop_reset_delay = nv->reset_delay;
1751
1752	/* Link Down Timeout = 0:
1753	 *
1754	 * 	When Port Down timer expires we will start returning
1755	 *	I/O's to OS with "DID_NO_CONNECT".
1756	 *
1757	 * Link Down Timeout != 0:
1758	 *
1759	 *	 The driver waits for the link to come up after link down
1760	 *	 before returning I/Os to OS with "DID_NO_CONNECT".
1761	 */
1762	if (nv->link_down_timeout == 0) {
1763		ha->loop_down_abort_time =
1764		    (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
1765	} else {
1766		ha->link_down_timeout =	 nv->link_down_timeout;
1767		ha->loop_down_abort_time =
1768		    (LOOP_DOWN_TIME - ha->link_down_timeout);
1769	}
1770
1771	/*
1772	 * Need enough time to try and get the port back.
1773	 */
1774	ha->port_down_retry_count = nv->port_down_retry_count;
1775	if (qlport_down_retry)
1776		ha->port_down_retry_count = qlport_down_retry;
1777	/* Set login_retry_count */
1778	ha->login_retry_count  = nv->retry_count;
1779	if (ha->port_down_retry_count == nv->port_down_retry_count &&
1780	    ha->port_down_retry_count > 3)
1781		ha->login_retry_count = ha->port_down_retry_count;
1782	else if (ha->port_down_retry_count > (int)ha->login_retry_count)
1783		ha->login_retry_count = ha->port_down_retry_count;
1784	if (ql2xloginretrycount)
1785		ha->login_retry_count = ql2xloginretrycount;
1786
1787	icb->lun_enables = __constant_cpu_to_le16(0);
1788	icb->command_resource_count = 0;
1789	icb->immediate_notify_resource_count = 0;
1790	icb->timeout = __constant_cpu_to_le16(0);
1791
1792	if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
1793		/* Enable RIO */
1794		icb->firmware_options[0] &= ~BIT_3;
1795		icb->add_firmware_options[0] &=
1796		    ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
1797		icb->add_firmware_options[0] |= BIT_2;
1798		icb->response_accumulation_timer = 3;
1799		icb->interrupt_delay_timer = 5;
1800
1801		ha->flags.process_response_queue = 1;
1802	} else {
1803		/* Enable ZIO. */
1804		if (!ha->flags.init_done) {
1805			ha->zio_mode = icb->add_firmware_options[0] &
1806			    (BIT_3 | BIT_2 | BIT_1 | BIT_0);
1807			ha->zio_timer = icb->interrupt_delay_timer ?
1808			    icb->interrupt_delay_timer: 2;
1809		}
1810		icb->add_firmware_options[0] &=
1811		    ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
1812		ha->flags.process_response_queue = 0;
1813		if (ha->zio_mode != QLA_ZIO_DISABLED) {
1814			ha->zio_mode = QLA_ZIO_MODE_6;
1815
1816			DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer "
1817			    "delay (%d us).\n", ha->host_no, ha->zio_mode,
1818			    ha->zio_timer * 100));
1819			qla_printk(KERN_INFO, ha,
1820			    "ZIO mode %d enabled; timer delay (%d us).\n",
1821			    ha->zio_mode, ha->zio_timer * 100);
1822
1823			icb->add_firmware_options[0] |= (uint8_t)ha->zio_mode;
1824			icb->interrupt_delay_timer = (uint8_t)ha->zio_timer;
1825			ha->flags.process_response_queue = 1;
1826		}
1827	}
1828
1829	if (rval) {
1830		DEBUG2_3(printk(KERN_WARNING
1831		    "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
1832	}
1833	return (rval);
1834}
1835
1836static void
1837qla2x00_rport_del(void *data)
1838{
1839	fc_port_t *fcport = data;
1840	struct fc_rport *rport;
1841	unsigned long flags;
1842
1843	spin_lock_irqsave(&fcport->rport_lock, flags);
1844	rport = fcport->drport;
1845	fcport->drport = NULL;
1846	spin_unlock_irqrestore(&fcport->rport_lock, flags);
1847	if (rport)
1848		fc_remote_port_delete(rport);
1849}
1850
1851/**
1852 * qla2x00_alloc_fcport() - Allocate a generic fcport.
1853 * @ha: HA context
1854 * @flags: allocation flags
1855 *
1856 * Returns a pointer to the allocated fcport, or NULL, if none available.
1857 */
1858static fc_port_t *
1859qla2x00_alloc_fcport(scsi_qla_host_t *ha, gfp_t flags)
1860{
1861	fc_port_t *fcport;
1862
1863	fcport = kzalloc(sizeof(fc_port_t), flags);
1864	if (!fcport)
1865		return NULL;
1866
1867	/* Setup fcport template structure. */
1868	fcport->ha = ha;
1869	fcport->vp_idx = ha->vp_idx;
1870	fcport->port_type = FCT_UNKNOWN;
1871	fcport->loop_id = FC_NO_LOOP_ID;
1872	atomic_set(&fcport->state, FCS_UNCONFIGURED);
1873	fcport->flags = FCF_RLC_SUPPORT;
1874	fcport->supported_classes = FC_COS_UNSPECIFIED;
1875	spin_lock_init(&fcport->rport_lock);
1876
1877	return fcport;
1878}
1879
1880/*
1881 * qla2x00_configure_loop
1882 *      Updates Fibre Channel Device Database with what is actually on loop.
1883 *
1884 * Input:
1885 *      ha                = adapter block pointer.
1886 *
1887 * Returns:
1888 *      0 = success.
1889 *      1 = error.
1890 *      2 = database was full and device was not configured.
1891 */
1892static int
1893qla2x00_configure_loop(scsi_qla_host_t *ha)
1894{
1895	int  rval;
1896	unsigned long flags, save_flags;
1897
1898	rval = QLA_SUCCESS;
1899
1900	/* Get Initiator ID */
1901	if (test_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags)) {
1902		rval = qla2x00_configure_hba(ha);
1903		if (rval != QLA_SUCCESS) {
1904			DEBUG(printk("scsi(%ld): Unable to configure HBA.\n",
1905			    ha->host_no));
1906			return (rval);
1907		}
1908	}
1909
1910	save_flags = flags = ha->dpc_flags;
1911	DEBUG(printk("scsi(%ld): Configure loop -- dpc flags =0x%lx\n",
1912	    ha->host_no, flags));
1913
1914	/*
1915	 * If we have both an RSCN and PORT UPDATE pending then handle them
1916	 * both at the same time.
1917	 */
1918	clear_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1919	clear_bit(RSCN_UPDATE, &ha->dpc_flags);
1920
1921	/* Determine what we need to do */
1922	if (ha->current_topology == ISP_CFG_FL &&
1923	    (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1924
1925		ha->flags.rscn_queue_overflow = 1;
1926		set_bit(RSCN_UPDATE, &flags);
1927
1928	} else if (ha->current_topology == ISP_CFG_F &&
1929	    (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1930
1931		ha->flags.rscn_queue_overflow = 1;
1932		set_bit(RSCN_UPDATE, &flags);
1933		clear_bit(LOCAL_LOOP_UPDATE, &flags);
1934
1935	} else if (ha->current_topology == ISP_CFG_N) {
1936		clear_bit(RSCN_UPDATE, &flags);
1937
1938	} else if (!ha->flags.online ||
1939	    (test_bit(ABORT_ISP_ACTIVE, &flags))) {
1940
1941		ha->flags.rscn_queue_overflow = 1;
1942		set_bit(RSCN_UPDATE, &flags);
1943		set_bit(LOCAL_LOOP_UPDATE, &flags);
1944	}
1945
1946	if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
1947		if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1948			rval = QLA_FUNCTION_FAILED;
1949		} else {
1950			rval = qla2x00_configure_local_loop(ha);
1951		}
1952	}
1953
1954	if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
1955		if (LOOP_TRANSITION(ha)) {
1956			rval = QLA_FUNCTION_FAILED;
1957		} else {
1958			rval = qla2x00_configure_fabric(ha);
1959		}
1960	}
1961
1962	if (rval == QLA_SUCCESS) {
1963		if (atomic_read(&ha->loop_down_timer) ||
1964		    test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1965			rval = QLA_FUNCTION_FAILED;
1966		} else {
1967			atomic_set(&ha->loop_state, LOOP_READY);
1968
1969			DEBUG(printk("scsi(%ld): LOOP READY\n", ha->host_no));
1970		}
1971	}
1972
1973	if (rval) {
1974		DEBUG2_3(printk("%s(%ld): *** FAILED ***\n",
1975		    __func__, ha->host_no));
1976	} else {
1977		DEBUG3(printk("%s: exiting normally\n", __func__));
1978	}
1979
1980	/* Restore state if a resync event occured during processing */
1981	if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1982		if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
1983			set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1984		if (test_bit(RSCN_UPDATE, &save_flags))
1985			set_bit(RSCN_UPDATE, &ha->dpc_flags);
1986	}
1987
1988	return (rval);
1989}
1990
1991
1992
1993/*
1994 * qla2x00_configure_local_loop
1995 *	Updates Fibre Channel Device Database with local loop devices.
1996 *
1997 * Input:
1998 *	ha = adapter block pointer.
1999 *
2000 * Returns:
2001 *	0 = success.
2002 */
2003static int
2004qla2x00_configure_local_loop(scsi_qla_host_t *ha)
2005{
2006	int		rval, rval2;
2007	int		found_devs;
2008	int		found;
2009	fc_port_t	*fcport, *new_fcport;
2010
2011	uint16_t	index;
2012	uint16_t	entries;
2013	char		*id_iter;
2014	uint16_t	loop_id;
2015	uint8_t		domain, area, al_pa;
2016	scsi_qla_host_t *pha = to_qla_parent(ha);
2017
2018	found_devs = 0;
2019	new_fcport = NULL;
2020	entries = MAX_FIBRE_DEVICES;
2021
2022	DEBUG3(printk("scsi(%ld): Getting FCAL position map\n", ha->host_no));
2023	DEBUG3(qla2x00_get_fcal_position_map(ha, NULL));
2024
2025	/* Get list of logged in devices. */
2026	memset(ha->gid_list, 0, GID_LIST_SIZE);
2027	rval = qla2x00_get_id_list(ha, ha->gid_list, ha->gid_list_dma,
2028	    &entries);
2029	if (rval != QLA_SUCCESS)
2030		goto cleanup_allocation;
2031
2032	DEBUG3(printk("scsi(%ld): Entries in ID list (%d)\n",
2033	    ha->host_no, entries));
2034	DEBUG3(qla2x00_dump_buffer((uint8_t *)ha->gid_list,
2035	    entries * sizeof(struct gid_list_info)));
2036
2037	/* Allocate temporary fcport for any new fcports discovered. */
2038	new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2039	if (new_fcport == NULL) {
2040		rval = QLA_MEMORY_ALLOC_FAILED;
2041		goto cleanup_allocation;
2042	}
2043	new_fcport->flags &= ~FCF_FABRIC_DEVICE;
2044
2045	/*
2046	 * Mark local devices that were present with FCF_DEVICE_LOST for now.
2047	 */
2048	list_for_each_entry(fcport, &pha->fcports, list) {
2049		if (fcport->vp_idx != ha->vp_idx)
2050			continue;
2051
2052		if (atomic_read(&fcport->state) == FCS_ONLINE &&
2053		    fcport->port_type != FCT_BROADCAST &&
2054		    (fcport->flags & FCF_FABRIC_DEVICE) == 0) {
2055
2056			DEBUG(printk("scsi(%ld): Marking port lost, "
2057			    "loop_id=0x%04x\n",
2058			    ha->host_no, fcport->loop_id));
2059
2060			atomic_set(&fcport->state, FCS_DEVICE_LOST);
2061			fcport->flags &= ~FCF_FARP_DONE;
2062		}
2063	}
2064
2065	/* Add devices to port list. */
2066	id_iter = (char *)ha->gid_list;
2067	for (index = 0; index < entries; index++) {
2068		domain = ((struct gid_list_info *)id_iter)->domain;
2069		area = ((struct gid_list_info *)id_iter)->area;
2070		al_pa = ((struct gid_list_info *)id_iter)->al_pa;
2071		if (IS_QLA2100(ha) || IS_QLA2200(ha))
2072			loop_id = (uint16_t)
2073			    ((struct gid_list_info *)id_iter)->loop_id_2100;
2074		else
2075			loop_id = le16_to_cpu(
2076			    ((struct gid_list_info *)id_iter)->loop_id);
2077		id_iter += ha->gid_list_info_size;
2078
2079		/* Bypass reserved domain fields. */
2080		if ((domain & 0xf0) == 0xf0)
2081			continue;
2082
2083		/* Bypass if not same domain and area of adapter. */
2084		if (area && domain &&
2085		    (area != ha->d_id.b.area || domain != ha->d_id.b.domain))
2086			continue;
2087
2088		/* Bypass invalid local loop ID. */
2089		if (loop_id > LAST_LOCAL_LOOP_ID)
2090			continue;
2091
2092		/* Fill in member data. */
2093		new_fcport->d_id.b.domain = domain;
2094		new_fcport->d_id.b.area = area;
2095		new_fcport->d_id.b.al_pa = al_pa;
2096		new_fcport->loop_id = loop_id;
2097		new_fcport->vp_idx = ha->vp_idx;
2098		rval2 = qla2x00_get_port_database(ha, new_fcport, 0);
2099		if (rval2 != QLA_SUCCESS) {
2100			DEBUG2(printk("scsi(%ld): Failed to retrieve fcport "
2101			    "information -- get_port_database=%x, "
2102			    "loop_id=0x%04x\n",
2103			    ha->host_no, rval2, new_fcport->loop_id));
2104			DEBUG2(printk("scsi(%ld): Scheduling resync...\n",
2105			    ha->host_no));
2106			set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
2107			continue;
2108		}
2109
2110		/* Check for matching device in port list. */
2111		found = 0;
2112		fcport = NULL;
2113		list_for_each_entry(fcport, &pha->fcports, list) {
2114			if (fcport->vp_idx != ha->vp_idx)
2115				continue;
2116
2117			if (memcmp(new_fcport->port_name, fcport->port_name,
2118			    WWN_SIZE))
2119				continue;
2120
2121			fcport->flags &= ~(FCF_FABRIC_DEVICE |
2122			    FCF_PERSISTENT_BOUND);
2123			fcport->loop_id = new_fcport->loop_id;
2124			fcport->port_type = new_fcport->port_type;
2125			fcport->d_id.b24 = new_fcport->d_id.b24;
2126			memcpy(fcport->node_name, new_fcport->node_name,
2127			    WWN_SIZE);
2128
2129			found++;
2130			break;
2131		}
2132
2133		if (!found) {
2134			/* New device, add to fcports list. */
2135			new_fcport->flags &= ~FCF_PERSISTENT_BOUND;
2136			if (ha->parent) {
2137				new_fcport->ha = ha;
2138				new_fcport->vp_idx = ha->vp_idx;
2139				list_add_tail(&new_fcport->vp_fcport,
2140				    &ha->vp_fcports);
2141			}
2142			list_add_tail(&new_fcport->list, &pha->fcports);
2143
2144			/* Allocate a new replacement fcport. */
2145			fcport = new_fcport;
2146			new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2147			if (new_fcport == NULL) {
2148				rval = QLA_MEMORY_ALLOC_FAILED;
2149				goto cleanup_allocation;
2150			}
2151			new_fcport->flags &= ~FCF_FABRIC_DEVICE;
2152		}
2153
2154		/* Base iIDMA settings on HBA port speed. */
2155		fcport->fp_speed = ha->link_data_rate;
2156
2157		qla2x00_update_fcport(ha, fcport);
2158
2159		found_devs++;
2160	}
2161
2162cleanup_allocation:
2163	kfree(new_fcport);
2164
2165	if (rval != QLA_SUCCESS) {
2166		DEBUG2(printk("scsi(%ld): Configure local loop error exit: "
2167		    "rval=%x\n", ha->host_no, rval));
2168	}
2169
2170	if (found_devs) {
2171		ha->device_flags |= DFLG_LOCAL_DEVICES;
2172		ha->device_flags &= ~DFLG_RETRY_LOCAL_DEVICES;
2173	}
2174
2175	return (rval);
2176}
2177
2178static void
2179qla2x00_probe_for_all_luns(scsi_qla_host_t *ha)
2180{
2181	fc_port_t	*fcport;
2182
2183	qla2x00_mark_all_devices_lost(ha, 0);
2184 	list_for_each_entry(fcport, &ha->fcports, list) {
2185		if (fcport->port_type != FCT_TARGET)
2186			continue;
2187
2188		qla2x00_update_fcport(ha, fcport);
2189	}
2190}
2191
2192static void
2193qla2x00_iidma_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
2194{
2195#define LS_UNKNOWN      2
2196	static char *link_speeds[5] = { "1", "2", "?", "4", "8" };
2197	int rval;
2198	uint16_t mb[6];
2199
2200	if (!IS_IIDMA_CAPABLE(ha))
2201		return;
2202
2203	if (fcport->fp_speed == PORT_SPEED_UNKNOWN ||
2204	    fcport->fp_speed > ha->link_data_rate)
2205		return;
2206
2207	rval = qla2x00_set_idma_speed(ha, fcport->loop_id, fcport->fp_speed,
2208	    mb);
2209	if (rval != QLA_SUCCESS) {
2210		DEBUG2(printk("scsi(%ld): Unable to adjust iIDMA "
2211		    "%02x%02x%02x%02x%02x%02x%02x%02x -- %04x %x %04x %04x.\n",
2212		    ha->host_no, fcport->port_name[0], fcport->port_name[1],
2213		    fcport->port_name[2], fcport->port_name[3],
2214		    fcport->port_name[4], fcport->port_name[5],
2215		    fcport->port_name[6], fcport->port_name[7], rval,
2216		    fcport->fp_speed, mb[0], mb[1]));
2217	} else {
2218		DEBUG2(qla_printk(KERN_INFO, ha,
2219		    "iIDMA adjusted to %s GB/s on "
2220		    "%02x%02x%02x%02x%02x%02x%02x%02x.\n",
2221		    link_speeds[fcport->fp_speed], fcport->port_name[0],
2222		    fcport->port_name[1], fcport->port_name[2],
2223		    fcport->port_name[3], fcport->port_name[4],
2224		    fcport->port_name[5], fcport->port_name[6],
2225		    fcport->port_name[7]));
2226	}
2227}
2228
2229static void
2230qla2x00_reg_remote_port(scsi_qla_host_t *ha, fc_port_t *fcport)
2231{
2232	struct fc_rport_identifiers rport_ids;
2233	struct fc_rport *rport;
2234	unsigned long flags;
2235
2236	if (fcport->drport)
2237		qla2x00_rport_del(fcport);
2238	if (fcport->rport)
2239		return;
2240
2241	rport_ids.node_name = wwn_to_u64(fcport->node_name);
2242	rport_ids.port_name = wwn_to_u64(fcport->port_name);
2243	rport_ids.port_id = fcport->d_id.b.domain << 16 |
2244	    fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
2245	rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
2246	rport = fc_remote_port_add(ha->host, 0, &rport_ids);
2247	if (!rport) {
2248		qla_printk(KERN_WARNING, ha,
2249		    "Unable to allocate fc remote port!\n");
2250		return;
2251	}
2252	spin_lock_irqsave(&fcport->rport_lock, flags);
2253	fcport->rport = rport;
2254	*((fc_port_t **)rport->dd_data) = fcport;
2255	spin_unlock_irqrestore(&fcport->rport_lock, flags);
2256
2257	rport->supported_classes = fcport->supported_classes;
2258
2259	rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
2260	if (fcport->port_type == FCT_INITIATOR)
2261		rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2262	if (fcport->port_type == FCT_TARGET)
2263		rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
2264	fc_remote_port_rolechg(rport, rport_ids.roles);
2265}
2266
2267/*
2268 * qla2x00_update_fcport
2269 *	Updates device on list.
2270 *
2271 * Input:
2272 *	ha = adapter block pointer.
2273 *	fcport = port structure pointer.
2274 *
2275 * Return:
2276 *	0  - Success
2277 *  BIT_0 - error
2278 *
2279 * Context:
2280 *	Kernel context.
2281 */
2282void
2283qla2x00_update_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
2284{
2285	scsi_qla_host_t *pha = to_qla_parent(ha);
2286
2287	fcport->ha = ha;
2288	fcport->login_retry = 0;
2289	fcport->port_login_retry_count = pha->port_down_retry_count *
2290	    PORT_RETRY_TIME;
2291	atomic_set(&fcport->port_down_timer, pha->port_down_retry_count *
2292	    PORT_RETRY_TIME);
2293	fcport->flags &= ~FCF_LOGIN_NEEDED;
2294
2295	qla2x00_iidma_fcport(ha, fcport);
2296
2297	atomic_set(&fcport->state, FCS_ONLINE);
2298
2299	qla2x00_reg_remote_port(ha, fcport);
2300}
2301
2302/*
2303 * qla2x00_configure_fabric
2304 *      Setup SNS devices with loop ID's.
2305 *
2306 * Input:
2307 *      ha = adapter block pointer.
2308 *
2309 * Returns:
2310 *      0 = success.
2311 *      BIT_0 = error
2312 */
2313static int
2314qla2x00_configure_fabric(scsi_qla_host_t *ha)
2315{
2316	int	rval, rval2;
2317	fc_port_t	*fcport, *fcptemp;
2318	uint16_t	next_loopid;
2319	uint16_t	mb[MAILBOX_REGISTER_COUNT];
2320	uint16_t	loop_id;
2321	LIST_HEAD(new_fcports);
2322	scsi_qla_host_t *pha = to_qla_parent(ha);
2323
2324	/* If FL port exists, then SNS is present */
2325	if (IS_FWI2_CAPABLE(ha))
2326		loop_id = NPH_F_PORT;
2327	else
2328		loop_id = SNS_FL_PORT;
2329	rval = qla2x00_get_port_name(ha, loop_id, ha->fabric_node_name, 1);
2330	if (rval != QLA_SUCCESS) {
2331		DEBUG2(printk("scsi(%ld): MBC_GET_PORT_NAME Failed, No FL "
2332		    "Port\n", ha->host_no));
2333
2334		ha->device_flags &= ~SWITCH_FOUND;
2335		return (QLA_SUCCESS);
2336	}
2337	ha->device_flags |= SWITCH_FOUND;
2338
2339	/* Mark devices that need re-synchronization. */
2340	rval2 = qla2x00_device_resync(ha);
2341	if (rval2 == QLA_RSCNS_HANDLED) {
2342		/* No point doing the scan, just continue. */
2343		return (QLA_SUCCESS);
2344	}
2345	do {
2346		/* FDMI support. */
2347		if (ql2xfdmienable &&
2348		    test_and_clear_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags))
2349			qla2x00_fdmi_register(ha);
2350
2351		/* Ensure we are logged into the SNS. */
2352		if (IS_FWI2_CAPABLE(ha))
2353			loop_id = NPH_SNS;
2354		else
2355			loop_id = SIMPLE_NAME_SERVER;
2356		ha->isp_ops->fabric_login(ha, loop_id, 0xff, 0xff,
2357		    0xfc, mb, BIT_1 | BIT_0);
2358		if (mb[0] != MBS_COMMAND_COMPLETE) {
2359			DEBUG2(qla_printk(KERN_INFO, ha,
2360			    "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
2361			    "mb[2]=%x mb[6]=%x mb[7]=%x\n", loop_id,
2362			    mb[0], mb[1], mb[2], mb[6], mb[7]));
2363			return (QLA_SUCCESS);
2364		}
2365
2366		if (test_and_clear_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags)) {
2367			if (qla2x00_rft_id(ha)) {
2368				/* EMPTY */
2369				DEBUG2(printk("scsi(%ld): Register FC-4 "
2370				    "TYPE failed.\n", ha->host_no));
2371			}
2372			if (qla2x00_rff_id(ha)) {
2373				/* EMPTY */
2374				DEBUG2(printk("scsi(%ld): Register FC-4 "
2375				    "Features failed.\n", ha->host_no));
2376			}
2377			if (qla2x00_rnn_id(ha)) {
2378				/* EMPTY */
2379				DEBUG2(printk("scsi(%ld): Register Node Name "
2380				    "failed.\n", ha->host_no));
2381			} else if (qla2x00_rsnn_nn(ha)) {
2382				/* EMPTY */
2383				DEBUG2(printk("scsi(%ld): Register Symbolic "
2384				    "Node Name failed.\n", ha->host_no));
2385			}
2386		}
2387
2388		rval = qla2x00_find_all_fabric_devs(ha, &new_fcports);
2389		if (rval != QLA_SUCCESS)
2390			break;
2391
2392		/*
2393		 * Logout all previous fabric devices marked lost, except
2394		 * tape devices.
2395		 */
2396		list_for_each_entry(fcport, &pha->fcports, list) {
2397			if (fcport->vp_idx !=ha->vp_idx)
2398				continue;
2399
2400			if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2401				break;
2402
2403			if ((fcport->flags & FCF_FABRIC_DEVICE) == 0)
2404				continue;
2405
2406			if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
2407				qla2x00_mark_device_lost(ha, fcport,
2408				    ql2xplogiabsentdevice, 0);
2409				if (fcport->loop_id != FC_NO_LOOP_ID &&
2410				    (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2411				    fcport->port_type != FCT_INITIATOR &&
2412				    fcport->port_type != FCT_BROADCAST) {
2413					ha->isp_ops->fabric_logout(ha,
2414					    fcport->loop_id,
2415					    fcport->d_id.b.domain,
2416					    fcport->d_id.b.area,
2417					    fcport->d_id.b.al_pa);
2418					fcport->loop_id = FC_NO_LOOP_ID;
2419				}
2420			}
2421		}
2422
2423		/* Starting free loop ID. */
2424		next_loopid = pha->min_external_loopid;
2425
2426		/*
2427		 * Scan through our port list and login entries that need to be
2428		 * logged in.
2429		 */
2430		list_for_each_entry(fcport, &pha->fcports, list) {
2431			if (fcport->vp_idx != ha->vp_idx)
2432				continue;
2433
2434			if (atomic_read(&ha->loop_down_timer) ||
2435			    test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2436				break;
2437
2438			if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2439			    (fcport->flags & FCF_LOGIN_NEEDED) == 0)
2440				continue;
2441
2442			if (fcport->loop_id == FC_NO_LOOP_ID) {
2443				fcport->loop_id = next_loopid;
2444				rval = qla2x00_find_new_loop_id(ha, fcport);
2445				if (rval != QLA_SUCCESS) {
2446					/* Ran out of IDs to use */
2447					break;
2448				}
2449			}
2450			/* Login and update database */
2451			qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
2452		}
2453
2454		/* Exit if out of loop IDs. */
2455		if (rval != QLA_SUCCESS) {
2456			break;
2457		}
2458
2459		/*
2460		 * Login and add the new devices to our port list.
2461		 */
2462		list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2463			if (atomic_read(&ha->loop_down_timer) ||
2464			    test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2465				break;
2466
2467			/* Find a new loop ID to use. */
2468			fcport->loop_id = next_loopid;
2469			rval = qla2x00_find_new_loop_id(ha, fcport);
2470			if (rval != QLA_SUCCESS) {
2471				/* Ran out of IDs to use */
2472				break;
2473			}
2474
2475			/* Login and update database */
2476			qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
2477
2478			if (ha->parent) {
2479				fcport->ha = ha;
2480				fcport->vp_idx = ha->vp_idx;
2481				list_add_tail(&fcport->vp_fcport,
2482				    &ha->vp_fcports);
2483				list_move_tail(&fcport->list,
2484				    &ha->parent->fcports);
2485			} else
2486				list_move_tail(&fcport->list, &ha->fcports);
2487		}
2488	} while (0);
2489
2490	/* Free all new device structures not processed. */
2491	list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2492		list_del(&fcport->list);
2493		kfree(fcport);
2494	}
2495
2496	if (rval) {
2497		DEBUG2(printk("scsi(%ld): Configure fabric error exit: "
2498		    "rval=%d\n", ha->host_no, rval));
2499	}
2500
2501	return (rval);
2502}
2503
2504
2505/*
2506 * qla2x00_find_all_fabric_devs
2507 *
2508 * Input:
2509 *	ha = adapter block pointer.
2510 *	dev = database device entry pointer.
2511 *
2512 * Returns:
2513 *	0 = success.
2514 *
2515 * Context:
2516 *	Kernel context.
2517 */
2518static int
2519qla2x00_find_all_fabric_devs(scsi_qla_host_t *ha, struct list_head *new_fcports)
2520{
2521	int		rval;
2522	uint16_t	loop_id;
2523	fc_port_t	*fcport, *new_fcport, *fcptemp;
2524	int		found;
2525
2526	sw_info_t	*swl;
2527	int		swl_idx;
2528	int		first_dev, last_dev;
2529	port_id_t	wrap, nxt_d_id;
2530	int 		vp_index;
2531	int		empty_vp_index;
2532	int		found_vp;
2533	scsi_qla_host_t *vha;
2534	scsi_qla_host_t *pha = to_qla_parent(ha);
2535
2536	rval = QLA_SUCCESS;
2537
2538	/* Try GID_PT to get device list, else GAN. */
2539	swl = kcalloc(MAX_FIBRE_DEVICES, sizeof(sw_info_t), GFP_ATOMIC);
2540	if (!swl) {
2541		/*EMPTY*/
2542		DEBUG2(printk("scsi(%ld): GID_PT allocations failed, fallback "
2543		    "on GA_NXT\n", ha->host_no));
2544	} else {
2545		if (qla2x00_gid_pt(ha, swl) != QLA_SUCCESS) {
2546			kfree(swl);
2547			swl = NULL;
2548		} else if (qla2x00_gpn_id(ha, swl) != QLA_SUCCESS) {
2549			kfree(swl);
2550			swl = NULL;
2551		} else if (qla2x00_gnn_id(ha, swl) != QLA_SUCCESS) {
2552			kfree(swl);
2553			swl = NULL;
2554		} else if (qla2x00_gfpn_id(ha, swl) == QLA_SUCCESS) {
2555			qla2x00_gpsc(ha, swl);
2556		}
2557	}
2558	swl_idx = 0;
2559
2560	/* Allocate temporary fcport for any new fcports discovered. */
2561	new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2562	if (new_fcport == NULL) {
2563		kfree(swl);
2564		return (QLA_MEMORY_ALLOC_FAILED);
2565	}
2566	new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2567	new_fcport->vp_idx = ha->vp_idx;
2568	/* Set start port ID scan at adapter ID. */
2569	first_dev = 1;
2570	last_dev = 0;
2571
2572	/* Starting free loop ID. */
2573	loop_id = pha->min_external_loopid;
2574	for (; loop_id <= ha->last_loop_id; loop_id++) {
2575		if (qla2x00_is_reserved_id(ha, loop_id))
2576			continue;
2577
2578		if (atomic_read(&ha->loop_down_timer) || LOOP_TRANSITION(ha))
2579			break;
2580
2581		if (swl != NULL) {
2582			if (last_dev) {
2583				wrap.b24 = new_fcport->d_id.b24;
2584			} else {
2585				new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
2586				memcpy(new_fcport->node_name,
2587				    swl[swl_idx].node_name, WWN_SIZE);
2588				memcpy(new_fcport->port_name,
2589				    swl[swl_idx].port_name, WWN_SIZE);
2590				memcpy(new_fcport->fabric_port_name,
2591				    swl[swl_idx].fabric_port_name, WWN_SIZE);
2592				new_fcport->fp_speed = swl[swl_idx].fp_speed;
2593
2594				if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
2595					last_dev = 1;
2596				}
2597				swl_idx++;
2598			}
2599		} else {
2600			/* Send GA_NXT to the switch */
2601			rval = qla2x00_ga_nxt(ha, new_fcport);
2602			if (rval != QLA_SUCCESS) {
2603				qla_printk(KERN_WARNING, ha,
2604				    "SNS scan failed -- assuming zero-entry "
2605				    "result...\n");
2606				list_for_each_entry_safe(fcport, fcptemp,
2607				    new_fcports, list) {
2608					list_del(&fcport->list);
2609					kfree(fcport);
2610				}
2611				rval = QLA_SUCCESS;
2612				break;
2613			}
2614		}
2615
2616		/* If wrap on switch device list, exit. */
2617		if (first_dev) {
2618			wrap.b24 = new_fcport->d_id.b24;
2619			first_dev = 0;
2620		} else if (new_fcport->d_id.b24 == wrap.b24) {
2621			DEBUG2(printk("scsi(%ld): device wrap (%02x%02x%02x)\n",
2622			    ha->host_no, new_fcport->d_id.b.domain,
2623			    new_fcport->d_id.b.area, new_fcport->d_id.b.al_pa));
2624			break;
2625		}
2626
2627		/* Bypass if same physical adapter. */
2628		if (new_fcport->d_id.b24 == pha->d_id.b24)
2629			continue;
2630
2631		/* Bypass virtual ports of the same host. */
2632		if (pha->num_vhosts) {
2633			for_each_mapped_vp_idx(pha, vp_index) {
2634				empty_vp_index = 1;
2635				found_vp = 0;
2636				list_for_each_entry(vha, &pha->vp_list,
2637				    vp_list) {
2638					if (vp_index == vha->vp_idx) {
2639						empty_vp_index = 0;
2640						found_vp = 1;
2641						break;
2642					}
2643				}
2644
2645				if (empty_vp_index)
2646					continue;
2647
2648				if (found_vp &&
2649				    new_fcport->d_id.b24 == vha->d_id.b24)
2650					break;
2651			}
2652
2653			if (vp_index <= pha->max_npiv_vports)
2654				continue;
2655		}
2656
2657		/* Bypass if same domain and area of adapter. */
2658		if (((new_fcport->d_id.b24 & 0xffff00) ==
2659		    (ha->d_id.b24 & 0xffff00)) && ha->current_topology ==
2660			ISP_CFG_FL)
2661			    continue;
2662
2663		/* Bypass reserved domain fields. */
2664		if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
2665			continue;
2666
2667		/* Locate matching device in database. */
2668		found = 0;
2669		list_for_each_entry(fcport, &pha->fcports, list) {
2670			if (new_fcport->vp_idx != fcport->vp_idx)
2671				continue;
2672			if (memcmp(new_fcport->port_name, fcport->port_name,
2673			    WWN_SIZE))
2674				continue;
2675
2676			found++;
2677
2678			/* Update port state. */
2679			memcpy(fcport->fabric_port_name,
2680			    new_fcport->fabric_port_name, WWN_SIZE);
2681			fcport->fp_speed = new_fcport->fp_speed;
2682
2683			/*
2684			 * If address the same and state FCS_ONLINE, nothing
2685			 * changed.
2686			 */
2687			if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
2688			    atomic_read(&fcport->state) == FCS_ONLINE) {
2689				break;
2690			}
2691
2692			/*
2693			 * If device was not a fabric device before.
2694			 */
2695			if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
2696				fcport->d_id.b24 = new_fcport->d_id.b24;
2697				fcport->loop_id = FC_NO_LOOP_ID;
2698				fcport->flags |= (FCF_FABRIC_DEVICE |
2699				    FCF_LOGIN_NEEDED);
2700				fcport->flags &= ~FCF_PERSISTENT_BOUND;
2701				break;
2702			}
2703
2704			/*
2705			 * Port ID changed or device was marked to be updated;
2706			 * Log it out if still logged in and mark it for
2707			 * relogin later.
2708			 */
2709			fcport->d_id.b24 = new_fcport->d_id.b24;
2710			fcport->flags |= FCF_LOGIN_NEEDED;
2711			if (fcport->loop_id != FC_NO_LOOP_ID &&
2712			    (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2713			    fcport->port_type != FCT_INITIATOR &&
2714			    fcport->port_type != FCT_BROADCAST) {
2715				ha->isp_ops->fabric_logout(ha, fcport->loop_id,
2716				    fcport->d_id.b.domain, fcport->d_id.b.area,
2717				    fcport->d_id.b.al_pa);
2718				fcport->loop_id = FC_NO_LOOP_ID;
2719			}
2720
2721			break;
2722		}
2723
2724		if (found)
2725			continue;
2726
2727		/* If device was not in our fcports list, then add it. */
2728		list_add_tail(&new_fcport->list, new_fcports);
2729
2730		/* Allocate a new replacement fcport. */
2731		nxt_d_id.b24 = new_fcport->d_id.b24;
2732		new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2733		if (new_fcport == NULL) {
2734			kfree(swl);
2735			return (QLA_MEMORY_ALLOC_FAILED);
2736		}
2737		new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2738		new_fcport->d_id.b24 = nxt_d_id.b24;
2739		new_fcport->vp_idx = ha->vp_idx;
2740	}
2741
2742	kfree(swl);
2743	kfree(new_fcport);
2744
2745	if (!list_empty(new_fcports))
2746		ha->device_flags |= DFLG_FABRIC_DEVICES;
2747
2748	return (rval);
2749}
2750
2751/*
2752 * qla2x00_find_new_loop_id
2753 *	Scan through our port list and find a new usable loop ID.
2754 *
2755 * Input:
2756 *	ha:	adapter state pointer.
2757 *	dev:	port structure pointer.
2758 *
2759 * Returns:
2760 *	qla2x00 local function return status code.
2761 *
2762 * Context:
2763 *	Kernel context.
2764 */
2765static int
2766qla2x00_find_new_loop_id(scsi_qla_host_t *ha, fc_port_t *dev)
2767{
2768	int	rval;
2769	int	found;
2770	fc_port_t *fcport;
2771	uint16_t first_loop_id;
2772	scsi_qla_host_t *pha = to_qla_parent(ha);
2773
2774	rval = QLA_SUCCESS;
2775
2776	/* Save starting loop ID. */
2777	first_loop_id = dev->loop_id;
2778
2779	for (;;) {
2780		/* Skip loop ID if already used by adapter. */
2781		if (dev->loop_id == ha->loop_id) {
2782			dev->loop_id++;
2783		}
2784
2785		/* Skip reserved loop IDs. */
2786		while (qla2x00_is_reserved_id(ha, dev->loop_id)) {
2787			dev->loop_id++;
2788		}
2789
2790		/* Reset loop ID if passed the end. */
2791		if (dev->loop_id > ha->last_loop_id) {
2792			/* first loop ID. */
2793			dev->loop_id = ha->min_external_loopid;
2794		}
2795
2796		/* Check for loop ID being already in use. */
2797		found = 0;
2798		fcport = NULL;
2799		list_for_each_entry(fcport, &pha->fcports, list) {
2800			if (fcport->loop_id == dev->loop_id && fcport != dev) {
2801				/* ID possibly in use */
2802				found++;
2803				break;
2804			}
2805		}
2806
2807		/* If not in use then it is free to use. */
2808		if (!found) {
2809			break;
2810		}
2811
2812		/* ID in use. Try next value. */
2813		dev->loop_id++;
2814
2815		/* If wrap around. No free ID to use. */
2816		if (dev->loop_id == first_loop_id) {
2817			dev->loop_id = FC_NO_LOOP_ID;
2818			rval = QLA_FUNCTION_FAILED;
2819			break;
2820		}
2821	}
2822
2823	return (rval);
2824}
2825
2826/*
2827 * qla2x00_device_resync
2828 *	Marks devices in the database that needs resynchronization.
2829 *
2830 * Input:
2831 *	ha = adapter block pointer.
2832 *
2833 * Context:
2834 *	Kernel context.
2835 */
2836static int
2837qla2x00_device_resync(scsi_qla_host_t *ha)
2838{
2839	int	rval;
2840	uint32_t mask;
2841	fc_port_t *fcport;
2842	uint32_t rscn_entry;
2843	uint8_t rscn_out_iter;
2844	uint8_t format;
2845	port_id_t d_id;
2846	scsi_qla_host_t *pha = to_qla_parent(ha);
2847
2848	rval = QLA_RSCNS_HANDLED;
2849
2850	while (ha->rscn_out_ptr != ha->rscn_in_ptr ||
2851	    ha->flags.rscn_queue_overflow) {
2852
2853		rscn_entry = ha->rscn_queue[ha->rscn_out_ptr];
2854		format = MSB(MSW(rscn_entry));
2855		d_id.b.domain = LSB(MSW(rscn_entry));
2856		d_id.b.area = MSB(LSW(rscn_entry));
2857		d_id.b.al_pa = LSB(LSW(rscn_entry));
2858
2859		DEBUG(printk("scsi(%ld): RSCN queue entry[%d] = "
2860		    "[%02x/%02x%02x%02x].\n",
2861		    ha->host_no, ha->rscn_out_ptr, format, d_id.b.domain,
2862		    d_id.b.area, d_id.b.al_pa));
2863
2864		ha->rscn_out_ptr++;
2865		if (ha->rscn_out_ptr == MAX_RSCN_COUNT)
2866			ha->rscn_out_ptr = 0;
2867
2868		/* Skip duplicate entries. */
2869		for (rscn_out_iter = ha->rscn_out_ptr;
2870		    !ha->flags.rscn_queue_overflow &&
2871		    rscn_out_iter != ha->rscn_in_ptr;
2872		    rscn_out_iter = (rscn_out_iter ==
2873			(MAX_RSCN_COUNT - 1)) ? 0: rscn_out_iter + 1) {
2874
2875			if (rscn_entry != ha->rscn_queue[rscn_out_iter])
2876				break;
2877
2878			DEBUG(printk("scsi(%ld): Skipping duplicate RSCN queue "
2879			    "entry found at [%d].\n", ha->host_no,
2880			    rscn_out_iter));
2881
2882			ha->rscn_out_ptr = rscn_out_iter;
2883		}
2884
2885		/* Queue overflow, set switch default case. */
2886		if (ha->flags.rscn_queue_overflow) {
2887			DEBUG(printk("scsi(%ld): device_resync: rscn "
2888			    "overflow.\n", ha->host_no));
2889
2890			format = 3;
2891			ha->flags.rscn_queue_overflow = 0;
2892		}
2893
2894		switch (format) {
2895		case 0:
2896			mask = 0xffffff;
2897			break;
2898		case 1:
2899			mask = 0xffff00;
2900			break;
2901		case 2:
2902			mask = 0xff0000;
2903			break;
2904		default:
2905			mask = 0x0;
2906			d_id.b24 = 0;
2907			ha->rscn_out_ptr = ha->rscn_in_ptr;
2908			break;
2909		}
2910
2911		rval = QLA_SUCCESS;
2912
2913		list_for_each_entry(fcport, &pha->fcports, list) {
2914			if (fcport->vp_idx != ha->vp_idx)
2915				continue;
2916
2917			if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2918			    (fcport->d_id.b24 & mask) != d_id.b24 ||
2919			    fcport->port_type == FCT_BROADCAST)
2920				continue;
2921
2922			if (atomic_read(&fcport->state) == FCS_ONLINE) {
2923				if (format != 3 ||
2924				    fcport->port_type != FCT_INITIATOR) {
2925					qla2x00_mark_device_lost(ha, fcport,
2926					    0, 0);
2927				}
2928			}
2929			fcport->flags &= ~FCF_FARP_DONE;
2930		}
2931	}
2932	return (rval);
2933}
2934
2935/*
2936 * qla2x00_fabric_dev_login
2937 *	Login fabric target device and update FC port database.
2938 *
2939 * Input:
2940 *	ha:		adapter state pointer.
2941 *	fcport:		port structure list pointer.
2942 *	next_loopid:	contains value of a new loop ID that can be used
2943 *			by the next login attempt.
2944 *
2945 * Returns:
2946 *	qla2x00 local function return status code.
2947 *
2948 * Context:
2949 *	Kernel context.
2950 */
2951static int
2952qla2x00_fabric_dev_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2953    uint16_t *next_loopid)
2954{
2955	int	rval;
2956	int	retry;
2957	uint8_t opts;
2958
2959	rval = QLA_SUCCESS;
2960	retry = 0;
2961
2962	rval = qla2x00_fabric_login(ha, fcport, next_loopid);
2963	if (rval == QLA_SUCCESS) {
2964		/* Send an ADISC to tape devices.*/
2965		opts = 0;
2966		if (fcport->flags & FCF_TAPE_PRESENT)
2967			opts |= BIT_1;
2968		rval = qla2x00_get_port_database(ha, fcport, opts);
2969		if (rval != QLA_SUCCESS) {
2970			ha->isp_ops->fabric_logout(ha, fcport->loop_id,
2971			    fcport->d_id.b.domain, fcport->d_id.b.area,
2972			    fcport->d_id.b.al_pa);
2973			qla2x00_mark_device_lost(ha, fcport, 1, 0);
2974		} else {
2975			qla2x00_update_fcport(ha, fcport);
2976		}
2977	}
2978
2979	return (rval);
2980}
2981
2982/*
2983 * qla2x00_fabric_login
2984 *	Issue fabric login command.
2985 *
2986 * Input:
2987 *	ha = adapter block pointer.
2988 *	device = pointer to FC device type structure.
2989 *
2990 * Returns:
2991 *      0 - Login successfully
2992 *      1 - Login failed
2993 *      2 - Initiator device
2994 *      3 - Fatal error
2995 */
2996int
2997qla2x00_fabric_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2998    uint16_t *next_loopid)
2999{
3000	int	rval;
3001	int	retry;
3002	uint16_t tmp_loopid;
3003	uint16_t mb[MAILBOX_REGISTER_COUNT];
3004
3005	retry = 0;
3006	tmp_loopid = 0;
3007
3008	for (;;) {
3009		DEBUG(printk("scsi(%ld): Trying Fabric Login w/loop id 0x%04x "
3010 		    "for port %02x%02x%02x.\n",
3011 		    ha->host_no, fcport->loop_id, fcport->d_id.b.domain,
3012		    fcport->d_id.b.area, fcport->d_id.b.al_pa));
3013
3014		/* Login fcport on switch. */
3015		ha->isp_ops->fabric_login(ha, fcport->loop_id,
3016		    fcport->d_id.b.domain, fcport->d_id.b.area,
3017		    fcport->d_id.b.al_pa, mb, BIT_0);
3018		if (mb[0] == MBS_PORT_ID_USED) {
3019			/*
3020			 * Device has another loop ID.  The firmware team
3021			 * recommends the driver perform an implicit login with
3022			 * the specified ID again. The ID we just used is save
3023			 * here so we return with an ID that can be tried by
3024			 * the next login.
3025			 */
3026			retry++;
3027			tmp_loopid = fcport->loop_id;
3028			fcport->loop_id = mb[1];
3029
3030			DEBUG(printk("Fabric Login: port in use - next "
3031 			    "loop id=0x%04x, port Id=%02x%02x%02x.\n",
3032			    fcport->loop_id, fcport->d_id.b.domain,
3033			    fcport->d_id.b.area, fcport->d_id.b.al_pa));
3034
3035		} else if (mb[0] == MBS_COMMAND_COMPLETE) {
3036			/*
3037			 * Login succeeded.
3038			 */
3039			if (retry) {
3040				/* A retry occurred before. */
3041				*next_loopid = tmp_loopid;
3042			} else {
3043				/*
3044				 * No retry occurred before. Just increment the
3045				 * ID value for next login.
3046				 */
3047				*next_loopid = (fcport->loop_id + 1);
3048			}
3049
3050			if (mb[1] & BIT_0) {
3051				fcport->port_type = FCT_INITIATOR;
3052			} else {
3053				fcport->port_type = FCT_TARGET;
3054				if (mb[1] & BIT_1) {
3055					fcport->flags |= FCF_TAPE_PRESENT;
3056				}
3057			}
3058
3059			if (mb[10] & BIT_0)
3060				fcport->supported_classes |= FC_COS_CLASS2;
3061			if (mb[10] & BIT_1)
3062				fcport->supported_classes |= FC_COS_CLASS3;
3063
3064			rval = QLA_SUCCESS;
3065			break;
3066		} else if (mb[0] == MBS_LOOP_ID_USED) {
3067			/*
3068			 * Loop ID already used, try next loop ID.
3069			 */
3070			fcport->loop_id++;
3071			rval = qla2x00_find_new_loop_id(ha, fcport);
3072			if (rval != QLA_SUCCESS) {
3073				/* Ran out of loop IDs to use */
3074				break;
3075			}
3076		} else if (mb[0] == MBS_COMMAND_ERROR) {
3077			/*
3078			 * Firmware possibly timed out during login. If NO
3079			 * retries are left to do then the device is declared
3080			 * dead.
3081			 */
3082			*next_loopid = fcport->loop_id;
3083			ha->isp_ops->fabric_logout(ha, fcport->loop_id,
3084			    fcport->d_id.b.domain, fcport->d_id.b.area,
3085			    fcport->d_id.b.al_pa);
3086			qla2x00_mark_device_lost(ha, fcport, 1, 0);
3087
3088			rval = 1;
3089			break;
3090		} else {
3091			/*
3092			 * unrecoverable / not handled error
3093			 */
3094			DEBUG2(printk("%s(%ld): failed=%x port_id=%02x%02x%02x "
3095 			    "loop_id=%x jiffies=%lx.\n",
3096 			    __func__, ha->host_no, mb[0],
3097			    fcport->d_id.b.domain, fcport->d_id.b.area,
3098			    fcport->d_id.b.al_pa, fcport->loop_id, jiffies));
3099
3100			*next_loopid = fcport->loop_id;
3101			ha->isp_ops->fabric_logout(ha, fcport->loop_id,
3102			    fcport->d_id.b.domain, fcport->d_id.b.area,
3103			    fcport->d_id.b.al_pa);
3104			fcport->loop_id = FC_NO_LOOP_ID;
3105			fcport->login_retry = 0;
3106
3107			rval = 3;
3108			break;
3109		}
3110	}
3111
3112	return (rval);
3113}
3114
3115/*
3116 * qla2x00_local_device_login
3117 *	Issue local device login command.
3118 *
3119 * Input:
3120 *	ha = adapter block pointer.
3121 *	loop_id = loop id of device to login to.
3122 *
3123 * Returns (Where's the #define!!!!):
3124 *      0 - Login successfully
3125 *      1 - Login failed
3126 *      3 - Fatal error
3127 */
3128int
3129qla2x00_local_device_login(scsi_qla_host_t *ha, fc_port_t *fcport)
3130{
3131	int		rval;
3132	uint16_t	mb[MAILBOX_REGISTER_COUNT];
3133
3134	memset(mb, 0, sizeof(mb));
3135	rval = qla2x00_login_local_device(ha, fcport, mb, BIT_0);
3136	if (rval == QLA_SUCCESS) {
3137		/* Interrogate mailbox registers for any errors */
3138		if (mb[0] == MBS_COMMAND_ERROR)
3139			rval = 1;
3140		else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
3141			/* device not in PCB table */
3142			rval = 3;
3143	}
3144
3145	return (rval);
3146}
3147
3148/*
3149 *  qla2x00_loop_resync
3150 *      Resync with fibre channel devices.
3151 *
3152 * Input:
3153 *      ha = adapter block pointer.
3154 *
3155 * Returns:
3156 *      0 = success
3157 */
3158int
3159qla2x00_loop_resync(scsi_qla_host_t *ha)
3160{
3161	int   rval;
3162	uint32_t wait_time;
3163
3164	rval = QLA_SUCCESS;
3165
3166	atomic_set(&ha->loop_state, LOOP_UPDATE);
3167	clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3168	if (ha->flags.online) {
3169		if (!(rval = qla2x00_fw_ready(ha))) {
3170			/* Wait at most MAX_TARGET RSCNs for a stable link. */
3171			wait_time = 256;
3172			do {
3173				atomic_set(&ha->loop_state, LOOP_UPDATE);
3174
3175				/* Issue a marker after FW becomes ready. */
3176				qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
3177				ha->marker_needed = 0;
3178
3179				/* Remap devices on Loop. */
3180				clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
3181
3182				qla2x00_configure_loop(ha);
3183				wait_time--;
3184			} while (!atomic_read(&ha->loop_down_timer) &&
3185				!(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
3186				wait_time &&
3187				(test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
3188		}
3189	}
3190
3191	if (test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
3192		return (QLA_FUNCTION_FAILED);
3193	}
3194
3195	if (rval) {
3196		DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
3197	}
3198
3199	return (rval);
3200}
3201
3202void
3203qla2x00_rescan_fcports(scsi_qla_host_t *ha)
3204{
3205	int rescan_done;
3206	fc_port_t *fcport;
3207
3208	rescan_done = 0;
3209	list_for_each_entry(fcport, &ha->fcports, list) {
3210		if ((fcport->flags & FCF_RESCAN_NEEDED) == 0)
3211			continue;
3212
3213		qla2x00_update_fcport(ha, fcport);
3214		fcport->flags &= ~FCF_RESCAN_NEEDED;
3215
3216		rescan_done = 1;
3217	}
3218	qla2x00_probe_for_all_luns(ha);
3219}
3220
3221void
3222qla2x00_update_fcports(scsi_qla_host_t *ha)
3223{
3224	fc_port_t *fcport;
3225
3226	/* Go with deferred removal of rport references. */
3227	list_for_each_entry(fcport, &ha->fcports, list)
3228		if (fcport->drport)
3229			qla2x00_rport_del(fcport);
3230}
3231
3232/*
3233*  qla2x00_abort_isp
3234*      Resets ISP and aborts all outstanding commands.
3235*
3236* Input:
3237*      ha           = adapter block pointer.
3238*
3239* Returns:
3240*      0 = success
3241*/
3242int
3243qla2x00_abort_isp(scsi_qla_host_t *ha)
3244{
3245	int rval;
3246	uint8_t        status = 0;
3247
3248	if (ha->flags.online) {
3249		ha->flags.online = 0;
3250		clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
3251
3252		qla_printk(KERN_INFO, ha,
3253		    "Performing ISP error recovery - ha= %p.\n", ha);
3254		ha->isp_ops->reset_chip(ha);
3255
3256		atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
3257		if (atomic_read(&ha->loop_state) != LOOP_DOWN) {
3258			atomic_set(&ha->loop_state, LOOP_DOWN);
3259			qla2x00_mark_all_devices_lost(ha, 0);
3260		} else {
3261			if (!atomic_read(&ha->loop_down_timer))
3262				atomic_set(&ha->loop_down_timer,
3263				    LOOP_DOWN_TIME);
3264		}
3265
3266		/* Requeue all commands in outstanding command list. */
3267		qla2x00_abort_all_cmds(ha, DID_RESET << 16);
3268
3269		ha->isp_ops->get_flash_version(ha, ha->request_ring);
3270
3271		ha->isp_ops->nvram_config(ha);
3272
3273		if (!qla2x00_restart_isp(ha)) {
3274			clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3275
3276			if (!atomic_read(&ha->loop_down_timer)) {
3277				/*
3278				 * Issue marker command only when we are going
3279				 * to start the I/O .
3280				 */
3281				ha->marker_needed = 1;
3282			}
3283
3284			ha->flags.online = 1;
3285
3286			ha->isp_ops->enable_intrs(ha);
3287
3288			ha->isp_abort_cnt = 0;
3289			clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3290
3291			if (ha->eft) {
3292				memset(ha->eft, 0, EFT_SIZE);
3293				rval = qla2x00_enable_eft_trace(ha,
3294				    ha->eft_dma, EFT_NUM_BUFFERS);
3295				if (rval) {
3296					qla_printk(KERN_WARNING, ha,
3297					    "Unable to reinitialize EFT "
3298					    "(%d).\n", rval);
3299				}
3300			}
3301
3302			if (ha->fce) {
3303				ha->flags.fce_enabled = 1;
3304				memset(ha->fce, 0,
3305				    fce_calc_size(ha->fce_bufs));
3306				rval = qla2x00_enable_fce_trace(ha,
3307				    ha->fce_dma, ha->fce_bufs, ha->fce_mb,
3308				    &ha->fce_bufs);
3309				if (rval) {
3310					qla_printk(KERN_WARNING, ha,
3311					    "Unable to reinitialize FCE "
3312					    "(%d).\n", rval);
3313					ha->flags.fce_enabled = 0;
3314				}
3315			}
3316		} else {	/* failed the ISP abort */
3317			ha->flags.online = 1;
3318			if (test_bit(ISP_ABORT_RETRY, &ha->dpc_flags)) {
3319				if (ha->isp_abort_cnt == 0) {
3320 					qla_printk(KERN_WARNING, ha,
3321					    "ISP error recovery failed - "
3322					    "board disabled\n");
3323					/*
3324					 * The next call disables the board
3325					 * completely.
3326					 */
3327					ha->isp_ops->reset_adapter(ha);
3328					ha->flags.online = 0;
3329					clear_bit(ISP_ABORT_RETRY,
3330					    &ha->dpc_flags);
3331					status = 0;
3332				} else { /* schedule another ISP abort */
3333					ha->isp_abort_cnt--;
3334					DEBUG(printk("qla%ld: ISP abort - "
3335					    "retry remaining %d\n",
3336					    ha->host_no, ha->isp_abort_cnt));
3337					status = 1;
3338				}
3339			} else {
3340				ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
3341				DEBUG(printk("qla2x00(%ld): ISP error recovery "
3342				    "- retrying (%d) more times\n",
3343				    ha->host_no, ha->isp_abort_cnt));
3344				set_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3345				status = 1;
3346			}
3347		}
3348
3349	}
3350
3351	if (status) {
3352		qla_printk(KERN_INFO, ha,
3353			"qla2x00_abort_isp: **** FAILED ****\n");
3354	} else {
3355		DEBUG(printk(KERN_INFO
3356				"qla2x00_abort_isp(%ld): exiting.\n",
3357				ha->host_no));
3358	}
3359
3360	return(status);
3361}
3362
3363/*
3364*  qla2x00_restart_isp
3365*      restarts the ISP after a reset
3366*
3367* Input:
3368*      ha = adapter block pointer.
3369*
3370* Returns:
3371*      0 = success
3372*/
3373static int
3374qla2x00_restart_isp(scsi_qla_host_t *ha)
3375{
3376	uint8_t		status = 0;
3377	uint32_t wait_time;
3378
3379	/* If firmware needs to be loaded */
3380	if (qla2x00_isp_firmware(ha)) {
3381		ha->flags.online = 0;
3382		if (!(status = ha->isp_ops->chip_diag(ha)))
3383			status = qla2x00_setup_chip(ha);
3384	}
3385
3386	if (!status && !(status = qla2x00_init_rings(ha))) {
3387		clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3388		if (!(status = qla2x00_fw_ready(ha))) {
3389			DEBUG(printk("%s(): Start configure loop, "
3390			    "status = %d\n", __func__, status));
3391
3392			/* Issue a marker after FW becomes ready. */
3393			qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
3394
3395			ha->flags.online = 1;
3396			/* Wait at most MAX_TARGET RSCNs for a stable link. */
3397			wait_time = 256;
3398			do {
3399				clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
3400				qla2x00_configure_loop(ha);
3401				wait_time--;
3402			} while (!atomic_read(&ha->loop_down_timer) &&
3403				!(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
3404				wait_time &&
3405				(test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
3406		}
3407
3408		/* if no cable then assume it's good */
3409		if ((ha->device_flags & DFLG_NO_CABLE))
3410			status = 0;
3411
3412		DEBUG(printk("%s(): Configure loop done, status = 0x%x\n",
3413				__func__,
3414				status));
3415	}
3416	return (status);
3417}
3418
3419/*
3420* qla2x00_reset_adapter
3421*      Reset adapter.
3422*
3423* Input:
3424*      ha = adapter block pointer.
3425*/
3426void
3427qla2x00_reset_adapter(scsi_qla_host_t *ha)
3428{
3429	unsigned long flags = 0;
3430	struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
3431
3432	ha->flags.online = 0;
3433	ha->isp_ops->disable_intrs(ha);
3434
3435	spin_lock_irqsave(&ha->hardware_lock, flags);
3436	WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
3437	RD_REG_WORD(&reg->hccr);			/* PCI Posting. */
3438	WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
3439	RD_REG_WORD(&reg->hccr);			/* PCI Posting. */
3440	spin_unlock_irqrestore(&ha->hardware_lock, flags);
3441}
3442
3443void
3444qla24xx_reset_adapter(scsi_qla_host_t *ha)
3445{
3446	unsigned long flags = 0;
3447	struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
3448
3449	ha->flags.online = 0;
3450	ha->isp_ops->disable_intrs(ha);
3451
3452	spin_lock_irqsave(&ha->hardware_lock, flags);
3453	WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
3454	RD_REG_DWORD(&reg->hccr);
3455	WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
3456	RD_REG_DWORD(&reg->hccr);
3457	spin_unlock_irqrestore(&ha->hardware_lock, flags);
3458}
3459
3460/* On sparc systems, obtain port and node WWN from firmware
3461 * properties.
3462 */
3463static void qla24xx_nvram_wwn_from_ofw(scsi_qla_host_t *ha, struct nvram_24xx *nv)
3464{
3465#ifdef CONFIG_SPARC
3466	struct pci_dev *pdev = ha->pdev;
3467	struct device_node *dp = pci_device_to_OF_node(pdev);
3468	const u8 *val;
3469	int len;
3470
3471	val = of_get_property(dp, "port-wwn", &len);
3472	if (val && len >= WWN_SIZE)
3473		memcpy(nv->port_name, val, WWN_SIZE);
3474
3475	val = of_get_property(dp, "node-wwn", &len);
3476	if (val && len >= WWN_SIZE)
3477		memcpy(nv->node_name, val, WWN_SIZE);
3478#endif
3479}
3480
3481int
3482qla24xx_nvram_config(scsi_qla_host_t *ha)
3483{
3484	int   rval;
3485	struct init_cb_24xx *icb;
3486	struct nvram_24xx *nv;
3487	uint32_t *dptr;
3488	uint8_t  *dptr1, *dptr2;
3489	uint32_t chksum;
3490	uint16_t cnt;
3491
3492	rval = QLA_SUCCESS;
3493	icb = (struct init_cb_24xx *)ha->init_cb;
3494	nv = ha->nvram;
3495
3496	/* Determine NVRAM starting address. */
3497	ha->nvram_size = sizeof(struct nvram_24xx);
3498	ha->nvram_base = FA_NVRAM_FUNC0_ADDR;
3499	ha->vpd_size = FA_NVRAM_VPD_SIZE;
3500	ha->vpd_base = FA_NVRAM_VPD0_ADDR;
3501	if (PCI_FUNC(ha->pdev->devfn)) {
3502		ha->nvram_base = FA_NVRAM_FUNC1_ADDR;
3503		ha->vpd_base = FA_NVRAM_VPD1_ADDR;
3504	}
3505
3506	/* Get VPD data into cache */
3507	ha->vpd = ha->nvram + VPD_OFFSET;
3508	ha->isp_ops->read_nvram(ha, (uint8_t *)ha->vpd,
3509	    ha->nvram_base - FA_NVRAM_FUNC0_ADDR, FA_NVRAM_VPD_SIZE * 4);
3510
3511	/* Get NVRAM data into cache and calculate checksum. */
3512	dptr = (uint32_t *)nv;
3513	ha->isp_ops->read_nvram(ha, (uint8_t *)dptr, ha->nvram_base,
3514	    ha->nvram_size);
3515	for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++)
3516		chksum += le32_to_cpu(*dptr++);
3517
3518	DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
3519	DEBUG5(qla2x00_dump_buffer((uint8_t *)nv, ha->nvram_size));
3520
3521	/* Bad NVRAM data, set defaults parameters. */
3522	if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
3523	    || nv->id[3] != ' ' ||
3524	    nv->nvram_version < __constant_cpu_to_le16(ICB_VERSION)) {
3525		/* Reset NVRAM data. */
3526		qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
3527		    "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
3528		    le16_to_cpu(nv->nvram_version));
3529		qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
3530		    "invalid -- WWPN) defaults.\n");
3531
3532		/*
3533		 * Set default initialization control block.
3534		 */
3535		memset(nv, 0, ha->nvram_size);
3536		nv->nvram_version = __constant_cpu_to_le16(ICB_VERSION);
3537		nv->version = __constant_cpu_to_le16(ICB_VERSION);
3538		nv->frame_payload_size = __constant_cpu_to_le16(2048);
3539		nv->execution_throttle = __constant_cpu_to_le16(0xFFFF);
3540		nv->exchange_count = __constant_cpu_to_le16(0);
3541		nv->hard_address = __constant_cpu_to_le16(124);
3542		nv->port_name[0] = 0x21;
3543		nv->port_name[1] = 0x00 + PCI_FUNC(ha->pdev->devfn);
3544		nv->port_name[2] = 0x00;
3545		nv->port_name[3] = 0xe0;
3546		nv->port_name[4] = 0x8b;
3547		nv->port_name[5] = 0x1c;
3548		nv->port_name[6] = 0x55;
3549		nv->port_name[7] = 0x86;
3550		nv->node_name[0] = 0x20;
3551		nv->node_name[1] = 0x00;
3552		nv->node_name[2] = 0x00;
3553		nv->node_name[3] = 0xe0;
3554		nv->node_name[4] = 0x8b;
3555		nv->node_name[5] = 0x1c;
3556		nv->node_name[6] = 0x55;
3557		nv->node_name[7] = 0x86;
3558		qla24xx_nvram_wwn_from_ofw(ha, nv);
3559		nv->login_retry_count = __constant_cpu_to_le16(8);
3560		nv->interrupt_delay_timer = __constant_cpu_to_le16(0);
3561		nv->login_timeout = __constant_cpu_to_le16(0);
3562		nv->firmware_options_1 =
3563		    __constant_cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
3564		nv->firmware_options_2 = __constant_cpu_to_le32(2 << 4);
3565		nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_12);
3566		nv->firmware_options_3 = __constant_cpu_to_le32(2 << 13);
3567		nv->host_p = __constant_cpu_to_le32(BIT_11|BIT_10);
3568		nv->efi_parameters = __constant_cpu_to_le32(0);
3569		nv->reset_delay = 5;
3570		nv->max_luns_per_target = __constant_cpu_to_le16(128);
3571		nv->port_down_retry_count = __constant_cpu_to_le16(30);
3572		nv->link_down_timeout = __constant_cpu_to_le16(30);
3573
3574		rval = 1;
3575	}
3576
3577	/* Reset Initialization control block */
3578	memset(icb, 0, sizeof(struct init_cb_24xx));
3579
3580	/* Copy 1st segment. */
3581	dptr1 = (uint8_t *)icb;
3582	dptr2 = (uint8_t *)&nv->version;
3583	cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
3584	while (cnt--)
3585		*dptr1++ = *dptr2++;
3586
3587	icb->login_retry_count = nv->login_retry_count;
3588	icb->link_down_on_nos = nv->link_down_on_nos;
3589
3590	/* Copy 2nd segment. */
3591	dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
3592	dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
3593	cnt = (uint8_t *)&icb->reserved_3 -
3594	    (uint8_t *)&icb->interrupt_delay_timer;
3595	while (cnt--)
3596		*dptr1++ = *dptr2++;
3597
3598	/*
3599	 * Setup driver NVRAM options.
3600	 */
3601	qla2x00_set_model_info(ha, nv->model_name, sizeof(nv->model_name),
3602	    "QLA2462");
3603
3604	/* Use alternate WWN? */
3605	if (nv->host_p & __constant_cpu_to_le32(BIT_15)) {
3606		memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
3607		memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
3608	}
3609
3610	/* Prepare nodename */
3611	if ((icb->firmware_options_1 & __constant_cpu_to_le32(BIT_14)) == 0) {
3612		/*
3613		 * Firmware will apply the following mask if the nodename was
3614		 * not provided.
3615		 */
3616		memcpy(icb->node_name, icb->port_name, WWN_SIZE);
3617		icb->node_name[0] &= 0xF0;
3618	}
3619
3620	/* Set host adapter parameters. */
3621	ha->flags.disable_risc_code_load = 0;
3622	ha->flags.enable_lip_reset = 0;
3623	ha->flags.enable_lip_full_login =
3624	    le32_to_cpu(nv->host_p) & BIT_10 ? 1: 0;
3625	ha->flags.enable_target_reset =
3626	    le32_to_cpu(nv->host_p) & BIT_11 ? 1: 0;
3627	ha->flags.enable_led_scheme = 0;
3628	ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
3629
3630	ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
3631	    (BIT_6 | BIT_5 | BIT_4)) >> 4;
3632
3633	memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
3634	    sizeof(ha->fw_seriallink_options24));
3635
3636	/* save HBA serial number */
3637	ha->serial0 = icb->port_name[5];
3638	ha->serial1 = icb->port_name[6];
3639	ha->serial2 = icb->port_name[7];
3640	ha->node_name = icb->node_name;
3641	ha->port_name = icb->port_name;
3642
3643	icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
3644
3645	ha->retry_count = le16_to_cpu(nv->login_retry_count);
3646
3647	/* Set minimum login_timeout to 4 seconds. */
3648	if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
3649		nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
3650	if (le16_to_cpu(nv->login_timeout) < 4)
3651		nv->login_timeout = __constant_cpu_to_le16(4);
3652	ha->login_timeout = le16_to_cpu(nv->login_timeout);
3653	icb->login_timeout = cpu_to_le16(nv->login_timeout);
3654
3655	/* Set minimum RATOV to 100 tenths of a second. */
3656	ha->r_a_tov = 100;
3657
3658	ha->loop_reset_delay = nv->reset_delay;
3659
3660	/* Link Down Timeout = 0:
3661	 *
3662	 * 	When Port Down timer expires we will start returning
3663	 *	I/O's to OS with "DID_NO_CONNECT".
3664	 *
3665	 * Link Down Timeout != 0:
3666	 *
3667	 *	 The driver waits for the link to come up after link down
3668	 *	 before returning I/Os to OS with "DID_NO_CONNECT".
3669	 */
3670	if (le16_to_cpu(nv->link_down_timeout) == 0) {
3671		ha->loop_down_abort_time =
3672		    (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
3673	} else {
3674		ha->link_down_timeout =	le16_to_cpu(nv->link_down_timeout);
3675		ha->loop_down_abort_time =
3676		    (LOOP_DOWN_TIME - ha->link_down_timeout);
3677	}
3678
3679	/* Need enough time to try and get the port back. */
3680	ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
3681	if (qlport_down_retry)
3682		ha->port_down_retry_count = qlport_down_retry;
3683
3684	/* Set login_retry_count */
3685	ha->login_retry_count  = le16_to_cpu(nv->login_retry_count);
3686	if (ha->port_down_retry_count ==
3687	    le16_to_cpu(nv->port_down_retry_count) &&
3688	    ha->port_down_retry_count > 3)
3689		ha->login_retry_count = ha->port_down_retry_count;
3690	else if (ha->port_down_retry_count > (int)ha->login_retry_count)
3691		ha->login_retry_count = ha->port_down_retry_count;
3692	if (ql2xloginretrycount)
3693		ha->login_retry_count = ql2xloginretrycount;
3694
3695	/* Enable ZIO. */
3696	if (!ha->flags.init_done) {
3697		ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
3698		    (BIT_3 | BIT_2 | BIT_1 | BIT_0);
3699		ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
3700		    le16_to_cpu(icb->interrupt_delay_timer): 2;
3701	}
3702	icb->firmware_options_2 &= __constant_cpu_to_le32(
3703	    ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
3704	ha->flags.process_response_queue = 0;
3705	if (ha->zio_mode != QLA_ZIO_DISABLED) {
3706		ha->zio_mode = QLA_ZIO_MODE_6;
3707
3708		DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer delay "
3709		    "(%d us).\n", ha->host_no, ha->zio_mode,
3710		    ha->zio_timer * 100));
3711		qla_printk(KERN_INFO, ha,
3712		    "ZIO mode %d enabled; timer delay (%d us).\n",
3713		    ha->zio_mode, ha->zio_timer * 100);
3714
3715		icb->firmware_options_2 |= cpu_to_le32(
3716		    (uint32_t)ha->zio_mode);
3717		icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
3718		ha->flags.process_response_queue = 1;
3719	}
3720
3721	if (rval) {
3722		DEBUG2_3(printk(KERN_WARNING
3723		    "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
3724	}
3725	return (rval);
3726}
3727
3728static int
3729qla24xx_load_risc_flash(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3730{
3731	int	rval;
3732	int	segments, fragment;
3733	uint32_t faddr;
3734	uint32_t *dcode, dlen;
3735	uint32_t risc_addr;
3736	uint32_t risc_size;
3737	uint32_t i;
3738
3739	rval = QLA_SUCCESS;
3740
3741	segments = FA_RISC_CODE_SEGMENTS;
3742	faddr = FA_RISC_CODE_ADDR;
3743	dcode = (uint32_t *)ha->request_ring;
3744	*srisc_addr = 0;
3745
3746	/* Validate firmware image by checking version. */
3747	qla24xx_read_flash_data(ha, dcode, faddr + 4, 4);
3748	for (i = 0; i < 4; i++)
3749		dcode[i] = be32_to_cpu(dcode[i]);
3750	if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
3751	    dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
3752	    (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
3753		dcode[3] == 0)) {
3754		qla_printk(KERN_WARNING, ha,
3755		    "Unable to verify integrity of flash firmware image!\n");
3756		qla_printk(KERN_WARNING, ha,
3757		    "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
3758		    dcode[1], dcode[2], dcode[3]);
3759
3760		return QLA_FUNCTION_FAILED;
3761	}
3762
3763	while (segments && rval == QLA_SUCCESS) {
3764		/* Read segment's load information. */
3765		qla24xx_read_flash_data(ha, dcode, faddr, 4);
3766
3767		risc_addr = be32_to_cpu(dcode[2]);
3768		*srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
3769		risc_size = be32_to_cpu(dcode[3]);
3770
3771		fragment = 0;
3772		while (risc_size > 0 && rval == QLA_SUCCESS) {
3773			dlen = (uint32_t)(ha->fw_transfer_size >> 2);
3774			if (dlen > risc_size)
3775				dlen = risc_size;
3776
3777			DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3778			    "addr %x, number of dwords 0x%x, offset 0x%x.\n",
3779			    ha->host_no, risc_addr, dlen, faddr));
3780
3781			qla24xx_read_flash_data(ha, dcode, faddr, dlen);
3782			for (i = 0; i < dlen; i++)
3783				dcode[i] = swab32(dcode[i]);
3784
3785			rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3786			    dlen);
3787			if (rval) {
3788				DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3789				    "segment %d of firmware\n", ha->host_no,
3790				    fragment));
3791				qla_printk(KERN_WARNING, ha,
3792				    "[ERROR] Failed to load segment %d of "
3793				    "firmware\n", fragment);
3794				break;
3795			}
3796
3797			faddr += dlen;
3798			risc_addr += dlen;
3799			risc_size -= dlen;
3800			fragment++;
3801		}
3802
3803		/* Next segment. */
3804		segments--;
3805	}
3806
3807	return rval;
3808}
3809
3810#define QLA_FW_URL "ftp://ftp.qlogic.com/outgoing/linux/firmware/"
3811
3812int
3813qla2x00_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3814{
3815	int	rval;
3816	int	i, fragment;
3817	uint16_t *wcode, *fwcode;
3818	uint32_t risc_addr, risc_size, fwclen, wlen, *seg;
3819	struct fw_blob *blob;
3820
3821	/* Load firmware blob. */
3822	blob = qla2x00_request_firmware(ha);
3823	if (!blob) {
3824		qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
3825		qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
3826		    "from: " QLA_FW_URL ".\n");
3827		return QLA_FUNCTION_FAILED;
3828	}
3829
3830	rval = QLA_SUCCESS;
3831
3832	wcode = (uint16_t *)ha->request_ring;
3833	*srisc_addr = 0;
3834	fwcode = (uint16_t *)blob->fw->data;
3835	fwclen = 0;
3836
3837	/* Validate firmware image by checking version. */
3838	if (blob->fw->size < 8 * sizeof(uint16_t)) {
3839		qla_printk(KERN_WARNING, ha,
3840		    "Unable to verify integrity of firmware image (%Zd)!\n",
3841		    blob->fw->size);
3842		goto fail_fw_integrity;
3843	}
3844	for (i = 0; i < 4; i++)
3845		wcode[i] = be16_to_cpu(fwcode[i + 4]);
3846	if ((wcode[0] == 0xffff && wcode[1] == 0xffff && wcode[2] == 0xffff &&
3847	    wcode[3] == 0xffff) || (wcode[0] == 0 && wcode[1] == 0 &&
3848		wcode[2] == 0 && wcode[3] == 0)) {
3849		qla_printk(KERN_WARNING, ha,
3850		    "Unable to verify integrity of firmware image!\n");
3851		qla_printk(KERN_WARNING, ha,
3852		    "Firmware data: %04x %04x %04x %04x!\n", wcode[0],
3853		    wcode[1], wcode[2], wcode[3]);
3854		goto fail_fw_integrity;
3855	}
3856
3857	seg = blob->segs;
3858	while (*seg && rval == QLA_SUCCESS) {
3859		risc_addr = *seg;
3860		*srisc_addr = *srisc_addr == 0 ? *seg : *srisc_addr;
3861		risc_size = be16_to_cpu(fwcode[3]);
3862
3863		/* Validate firmware image size. */
3864		fwclen += risc_size * sizeof(uint16_t);
3865		if (blob->fw->size < fwclen) {
3866			qla_printk(KERN_WARNING, ha,
3867			    "Unable to verify integrity of firmware image "
3868			    "(%Zd)!\n", blob->fw->size);
3869			goto fail_fw_integrity;
3870		}
3871
3872		fragment = 0;
3873		while (risc_size > 0 && rval == QLA_SUCCESS) {
3874			wlen = (uint16_t)(ha->fw_transfer_size >> 1);
3875			if (wlen > risc_size)
3876				wlen = risc_size;
3877
3878			DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3879			    "addr %x, number of words 0x%x.\n", ha->host_no,
3880			    risc_addr, wlen));
3881
3882			for (i = 0; i < wlen; i++)
3883				wcode[i] = swab16(fwcode[i]);
3884
3885			rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3886			    wlen);
3887			if (rval) {
3888				DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3889				    "segment %d of firmware\n", ha->host_no,
3890				    fragment));
3891				qla_printk(KERN_WARNING, ha,
3892				    "[ERROR] Failed to load segment %d of "
3893				    "firmware\n", fragment);
3894				break;
3895			}
3896
3897			fwcode += wlen;
3898			risc_addr += wlen;
3899			risc_size -= wlen;
3900			fragment++;
3901		}
3902
3903		/* Next segment. */
3904		seg++;
3905	}
3906	return rval;
3907
3908fail_fw_integrity:
3909	return QLA_FUNCTION_FAILED;
3910}
3911
3912int
3913qla24xx_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3914{
3915	int	rval;
3916	int	segments, fragment;
3917	uint32_t *dcode, dlen;
3918	uint32_t risc_addr;
3919	uint32_t risc_size;
3920	uint32_t i;
3921	struct fw_blob *blob;
3922	uint32_t *fwcode, fwclen;
3923
3924	/* Load firmware blob. */
3925	blob = qla2x00_request_firmware(ha);
3926	if (!blob) {
3927		qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
3928		qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
3929		    "from: " QLA_FW_URL ".\n");
3930
3931		/* Try to load RISC code from flash. */
3932		qla_printk(KERN_ERR, ha, "Attempting to load (potentially "
3933		    "outdated) firmware from flash.\n");
3934		return qla24xx_load_risc_flash(ha, srisc_addr);
3935	}
3936
3937	rval = QLA_SUCCESS;
3938
3939	segments = FA_RISC_CODE_SEGMENTS;
3940	dcode = (uint32_t *)ha->request_ring;
3941	*srisc_addr = 0;
3942	fwcode = (uint32_t *)blob->fw->data;
3943	fwclen = 0;
3944
3945	/* Validate firmware image by checking version. */
3946	if (blob->fw->size < 8 * sizeof(uint32_t)) {
3947		qla_printk(KERN_WARNING, ha,
3948		    "Unable to verify integrity of firmware image (%Zd)!\n",
3949		    blob->fw->size);
3950		goto fail_fw_integrity;
3951	}
3952	for (i = 0; i < 4; i++)
3953		dcode[i] = be32_to_cpu(fwcode[i + 4]);
3954	if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
3955	    dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
3956	    (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
3957		dcode[3] == 0)) {
3958		qla_printk(KERN_WARNING, ha,
3959		    "Unable to verify integrity of firmware image!\n");
3960		qla_printk(KERN_WARNING, ha,
3961		    "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
3962		    dcode[1], dcode[2], dcode[3]);
3963		goto fail_fw_integrity;
3964	}
3965
3966	while (segments && rval == QLA_SUCCESS) {
3967		risc_addr = be32_to_cpu(fwcode[2]);
3968		*srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
3969		risc_size = be32_to_cpu(fwcode[3]);
3970
3971		/* Validate firmware image size. */
3972		fwclen += risc_size * sizeof(uint32_t);
3973		if (blob->fw->size < fwclen) {
3974			qla_printk(KERN_WARNING, ha,
3975			    "Unable to verify integrity of firmware image "
3976			    "(%Zd)!\n", blob->fw->size);
3977
3978			goto fail_fw_integrity;
3979		}
3980
3981		fragment = 0;
3982		while (risc_size > 0 && rval == QLA_SUCCESS) {
3983			dlen = (uint32_t)(ha->fw_transfer_size >> 2);
3984			if (dlen > risc_size)
3985				dlen = risc_size;
3986
3987			DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3988			    "addr %x, number of dwords 0x%x.\n", ha->host_no,
3989			    risc_addr, dlen));
3990
3991			for (i = 0; i < dlen; i++)
3992				dcode[i] = swab32(fwcode[i]);
3993
3994			rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3995			    dlen);
3996			if (rval) {
3997				DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3998				    "segment %d of firmware\n", ha->host_no,
3999				    fragment));
4000				qla_printk(KERN_WARNING, ha,
4001				    "[ERROR] Failed to load segment %d of "
4002				    "firmware\n", fragment);
4003				break;
4004			}
4005
4006			fwcode += dlen;
4007			risc_addr += dlen;
4008			risc_size -= dlen;
4009			fragment++;
4010		}
4011
4012		/* Next segment. */
4013		segments--;
4014	}
4015	return rval;
4016
4017fail_fw_integrity:
4018	return QLA_FUNCTION_FAILED;
4019}
4020
4021void
4022qla2x00_try_to_stop_firmware(scsi_qla_host_t *ha)
4023{
4024	int ret, retries;
4025
4026	if (!IS_FWI2_CAPABLE(ha))
4027		return;
4028	if (!ha->fw_major_version)
4029		return;
4030
4031	ret = qla2x00_stop_firmware(ha);
4032	for (retries = 5; ret != QLA_SUCCESS && ret != QLA_FUNCTION_TIMEOUT &&
4033	    retries ; retries--) {
4034		qla2x00_reset_chip(ha);
4035		if (qla2x00_chip_diag(ha) != QLA_SUCCESS)
4036			continue;
4037		if (qla2x00_setup_chip(ha) != QLA_SUCCESS)
4038			continue;
4039		qla_printk(KERN_INFO, ha,
4040		    "Attempting retry of stop-firmware command...\n");
4041		ret = qla2x00_stop_firmware(ha);
4042	}
4043}
4044
4045int
4046qla24xx_configure_vhba(scsi_qla_host_t *ha)
4047{
4048	int rval = QLA_SUCCESS;
4049	uint16_t mb[MAILBOX_REGISTER_COUNT];
4050
4051	if (!ha->parent)
4052		return -EINVAL;
4053
4054	rval = qla2x00_fw_ready(ha);
4055	if (rval == QLA_SUCCESS) {
4056		clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
4057		qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
4058	}
4059
4060	ha->flags.management_server_logged_in = 0;
4061
4062	/* Login to SNS first */
4063	qla24xx_login_fabric(ha, NPH_SNS, 0xff, 0xff, 0xfc,
4064	    mb, BIT_1);
4065	if (mb[0] != MBS_COMMAND_COMPLETE) {
4066		DEBUG15(qla_printk(KERN_INFO, ha,
4067		    "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
4068		    "mb[2]=%x mb[6]=%x mb[7]=%x\n", NPH_SNS,
4069		    mb[0], mb[1], mb[2], mb[6], mb[7]));
4070		return (QLA_FUNCTION_FAILED);
4071	}
4072
4073	atomic_set(&ha->loop_down_timer, 0);
4074	atomic_set(&ha->loop_state, LOOP_UP);
4075	set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
4076	set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
4077	rval = qla2x00_loop_resync(ha);
4078
4079	return rval;
4080}
4081