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