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