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