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