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