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