xref: /linux/drivers/scsi/qla2xxx/qla_os.c (revision 13abf8130139c2ccd4962a7e5a8902be5e6cb5a7)
1 /*
2  *                  QLOGIC LINUX SOFTWARE
3  *
4  * QLogic ISP2x00 device driver for Linux 2.6.x
5  * Copyright (C) 2003-2005 QLogic Corporation
6  * (www.qlogic.com)
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the
10  * Free Software Foundation; either version 2, or (at your option) any
11  * later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  */
19 #include "qla_def.h"
20 
21 #include <linux/moduleparam.h>
22 #include <linux/vmalloc.h>
23 #include <linux/smp_lock.h>
24 #include <linux/delay.h>
25 
26 #include <scsi/scsi_tcq.h>
27 #include <scsi/scsicam.h>
28 #include <scsi/scsi_transport.h>
29 #include <scsi/scsi_transport_fc.h>
30 
31 /*
32  * Driver version
33  */
34 char qla2x00_version_str[40];
35 
36 /*
37  * SRB allocation cache
38  */
39 static kmem_cache_t *srb_cachep;
40 
41 /*
42  * Ioctl related information.
43  */
44 static int num_hosts;
45 
46 int ql2xlogintimeout = 20;
47 module_param(ql2xlogintimeout, int, S_IRUGO|S_IRUSR);
48 MODULE_PARM_DESC(ql2xlogintimeout,
49 		"Login timeout value in seconds.");
50 
51 int qlport_down_retry = 30;
52 module_param(qlport_down_retry, int, S_IRUGO|S_IRUSR);
53 MODULE_PARM_DESC(qlport_down_retry,
54 		"Maximum number of command retries to a port that returns"
55 		"a PORT-DOWN status.");
56 
57 int ql2xplogiabsentdevice;
58 module_param(ql2xplogiabsentdevice, int, S_IRUGO|S_IWUSR);
59 MODULE_PARM_DESC(ql2xplogiabsentdevice,
60 		"Option to enable PLOGI to devices that are not present after "
61 		"a Fabric scan.  This is needed for several broken switches."
62 		"Default is 0 - no PLOGI. 1 - perfom PLOGI.");
63 
64 int ql2xenablezio = 0;
65 module_param(ql2xenablezio, int, S_IRUGO|S_IRUSR);
66 MODULE_PARM_DESC(ql2xenablezio,
67 		"Option to enable ZIO:If 1 then enable it otherwise"
68 		" use the default set in the NVRAM."
69 		" Default is 0 : disabled");
70 
71 int ql2xintrdelaytimer = 10;
72 module_param(ql2xintrdelaytimer, int, S_IRUGO|S_IRUSR);
73 MODULE_PARM_DESC(ql2xintrdelaytimer,
74 		"ZIO: Waiting time for Firmware before it generates an "
75 		"interrupt to the host to notify completion of request.");
76 
77 int ql2xloginretrycount = 0;
78 module_param(ql2xloginretrycount, int, S_IRUGO|S_IRUSR);
79 MODULE_PARM_DESC(ql2xloginretrycount,
80 		"Specify an alternate value for the NVRAM login retry count.");
81 
82 int ql2xfwloadbin;
83 module_param(ql2xfwloadbin, int, S_IRUGO|S_IRUSR);
84 MODULE_PARM_DESC(ql2xfwloadbin,
85 		"Load ISP2xxx firmware image via hotplug.");
86 
87 static void qla2x00_free_device(scsi_qla_host_t *);
88 
89 static void qla2x00_config_dma_addressing(scsi_qla_host_t *ha);
90 
91 /*
92  * SCSI host template entry points
93  */
94 static int qla2xxx_slave_configure(struct scsi_device * device);
95 static int qla2xxx_slave_alloc(struct scsi_device *);
96 static void qla2xxx_slave_destroy(struct scsi_device *);
97 static int qla2x00_queuecommand(struct scsi_cmnd *cmd,
98 		void (*fn)(struct scsi_cmnd *));
99 static int qla24xx_queuecommand(struct scsi_cmnd *cmd,
100 		void (*fn)(struct scsi_cmnd *));
101 static int qla2xxx_eh_abort(struct scsi_cmnd *);
102 static int qla2xxx_eh_device_reset(struct scsi_cmnd *);
103 static int qla2xxx_eh_bus_reset(struct scsi_cmnd *);
104 static int qla2xxx_eh_host_reset(struct scsi_cmnd *);
105 static int qla2x00_loop_reset(scsi_qla_host_t *ha);
106 static int qla2x00_device_reset(scsi_qla_host_t *, fc_port_t *);
107 
108 static struct scsi_host_template qla2x00_driver_template = {
109 	.module			= THIS_MODULE,
110 	.name			= "qla2xxx",
111 	.queuecommand		= qla2x00_queuecommand,
112 
113 	.eh_abort_handler	= qla2xxx_eh_abort,
114 	.eh_device_reset_handler = qla2xxx_eh_device_reset,
115 	.eh_bus_reset_handler	= qla2xxx_eh_bus_reset,
116 	.eh_host_reset_handler	= qla2xxx_eh_host_reset,
117 
118 	.slave_configure	= qla2xxx_slave_configure,
119 
120 	.slave_alloc		= qla2xxx_slave_alloc,
121 	.slave_destroy		= qla2xxx_slave_destroy,
122 	.this_id		= -1,
123 	.cmd_per_lun		= 3,
124 	.use_clustering		= ENABLE_CLUSTERING,
125 	.sg_tablesize		= SG_ALL,
126 
127 	/*
128 	 * The RISC allows for each command to transfer (2^32-1) bytes of data,
129 	 * which equates to 0x800000 sectors.
130 	 */
131 	.max_sectors		= 0xFFFF,
132 };
133 
134 static struct scsi_host_template qla24xx_driver_template = {
135 	.module			= THIS_MODULE,
136 	.name			= "qla2xxx",
137 	.queuecommand		= qla24xx_queuecommand,
138 
139 	.eh_abort_handler	= qla2xxx_eh_abort,
140 	.eh_device_reset_handler = qla2xxx_eh_device_reset,
141 	.eh_bus_reset_handler	= qla2xxx_eh_bus_reset,
142 	.eh_host_reset_handler	= qla2xxx_eh_host_reset,
143 
144 	.slave_configure	= qla2xxx_slave_configure,
145 
146 	.slave_alloc		= qla2xxx_slave_alloc,
147 	.slave_destroy		= qla2xxx_slave_destroy,
148 	.this_id		= -1,
149 	.cmd_per_lun		= 3,
150 	.use_clustering		= ENABLE_CLUSTERING,
151 	.sg_tablesize		= SG_ALL,
152 
153 	.max_sectors		= 0xFFFF,
154 };
155 
156 static struct scsi_transport_template *qla2xxx_transport_template = NULL;
157 
158 /* TODO Convert to inlines
159  *
160  * Timer routines
161  */
162 #define	WATCH_INTERVAL		1       /* number of seconds */
163 
164 static void qla2x00_timer(scsi_qla_host_t *);
165 
166 static __inline__ void qla2x00_start_timer(scsi_qla_host_t *,
167     void *, unsigned long);
168 static __inline__ void qla2x00_restart_timer(scsi_qla_host_t *, unsigned long);
169 static __inline__ void qla2x00_stop_timer(scsi_qla_host_t *);
170 
171 static inline void
172 qla2x00_start_timer(scsi_qla_host_t *ha, void *func, unsigned long interval)
173 {
174 	init_timer(&ha->timer);
175 	ha->timer.expires = jiffies + interval * HZ;
176 	ha->timer.data = (unsigned long)ha;
177 	ha->timer.function = (void (*)(unsigned long))func;
178 	add_timer(&ha->timer);
179 	ha->timer_active = 1;
180 }
181 
182 static inline void
183 qla2x00_restart_timer(scsi_qla_host_t *ha, unsigned long interval)
184 {
185 	mod_timer(&ha->timer, jiffies + interval * HZ);
186 }
187 
188 static __inline__ void
189 qla2x00_stop_timer(scsi_qla_host_t *ha)
190 {
191 	del_timer_sync(&ha->timer);
192 	ha->timer_active = 0;
193 }
194 
195 static int qla2x00_do_dpc(void *data);
196 
197 static void qla2x00_rst_aen(scsi_qla_host_t *);
198 
199 static uint8_t qla2x00_mem_alloc(scsi_qla_host_t *);
200 static void qla2x00_mem_free(scsi_qla_host_t *ha);
201 static int qla2x00_allocate_sp_pool( scsi_qla_host_t *ha);
202 static void qla2x00_free_sp_pool(scsi_qla_host_t *ha);
203 static void qla2x00_sp_free_dma(scsi_qla_host_t *, srb_t *);
204 void qla2x00_sp_compl(scsi_qla_host_t *ha, srb_t *);
205 
206 /* -------------------------------------------------------------------------- */
207 
208 static char *
209 qla2x00_pci_info_str(struct scsi_qla_host *ha, char *str)
210 {
211 	static char *pci_bus_modes[] = {
212 		"33", "66", "100", "133",
213 	};
214 	uint16_t pci_bus;
215 
216 	strcpy(str, "PCI");
217 	pci_bus = (ha->pci_attr & (BIT_9 | BIT_10)) >> 9;
218 	if (pci_bus) {
219 		strcat(str, "-X (");
220 		strcat(str, pci_bus_modes[pci_bus]);
221 	} else {
222 		pci_bus = (ha->pci_attr & BIT_8) >> 8;
223 		strcat(str, " (");
224 		strcat(str, pci_bus_modes[pci_bus]);
225 	}
226 	strcat(str, " MHz)");
227 
228 	return (str);
229 }
230 
231 static char *
232 qla24xx_pci_info_str(struct scsi_qla_host *ha, char *str)
233 {
234 	static char *pci_bus_modes[] = { "33", "66", "100", "133", };
235 	uint32_t pci_bus;
236 	int pcie_reg;
237 
238 	pcie_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_EXP);
239 	if (pcie_reg) {
240 		char lwstr[6];
241 		uint16_t pcie_lstat, lspeed, lwidth;
242 
243 		pcie_reg += 0x12;
244 		pci_read_config_word(ha->pdev, pcie_reg, &pcie_lstat);
245 		lspeed = pcie_lstat & (BIT_0 | BIT_1 | BIT_2 | BIT_3);
246 		lwidth = (pcie_lstat &
247 		    (BIT_4 | BIT_5 | BIT_6 | BIT_7 | BIT_8 | BIT_9)) >> 4;
248 
249 		strcpy(str, "PCIe (");
250 		if (lspeed == 1)
251 			strcat(str, "2.5Gb/s ");
252 		else
253 			strcat(str, "<unknown> ");
254 		snprintf(lwstr, sizeof(lwstr), "x%d)", lwidth);
255 		strcat(str, lwstr);
256 
257 		return str;
258 	}
259 
260 	strcpy(str, "PCI");
261 	pci_bus = (ha->pci_attr & CSRX_PCIX_BUS_MODE_MASK) >> 8;
262 	if (pci_bus == 0 || pci_bus == 8) {
263 		strcat(str, " (");
264 		strcat(str, pci_bus_modes[pci_bus >> 3]);
265 	} else {
266 		strcat(str, "-X ");
267 		if (pci_bus & BIT_2)
268 			strcat(str, "Mode 2");
269 		else
270 			strcat(str, "Mode 1");
271 		strcat(str, " (");
272 		strcat(str, pci_bus_modes[pci_bus & ~BIT_2]);
273 	}
274 	strcat(str, " MHz)");
275 
276 	return str;
277 }
278 
279 char *
280 qla2x00_fw_version_str(struct scsi_qla_host *ha, char *str)
281 {
282 	char un_str[10];
283 
284 	sprintf(str, "%d.%02d.%02d ", ha->fw_major_version,
285 	    ha->fw_minor_version,
286 	    ha->fw_subminor_version);
287 
288 	if (ha->fw_attributes & BIT_9) {
289 		strcat(str, "FLX");
290 		return (str);
291 	}
292 
293 	switch (ha->fw_attributes & 0xFF) {
294 	case 0x7:
295 		strcat(str, "EF");
296 		break;
297 	case 0x17:
298 		strcat(str, "TP");
299 		break;
300 	case 0x37:
301 		strcat(str, "IP");
302 		break;
303 	case 0x77:
304 		strcat(str, "VI");
305 		break;
306 	default:
307 		sprintf(un_str, "(%x)", ha->fw_attributes);
308 		strcat(str, un_str);
309 		break;
310 	}
311 	if (ha->fw_attributes & 0x100)
312 		strcat(str, "X");
313 
314 	return (str);
315 }
316 
317 char *
318 qla24xx_fw_version_str(struct scsi_qla_host *ha, char *str)
319 {
320 	sprintf(str, "%d.%02d.%02d ", ha->fw_major_version,
321 	    ha->fw_minor_version,
322 	    ha->fw_subminor_version);
323 
324 	if (ha->fw_attributes & BIT_0)
325 		strcat(str, "[Class 2] ");
326 	if (ha->fw_attributes & BIT_1)
327 		strcat(str, "[IP] ");
328 	if (ha->fw_attributes & BIT_2)
329 		strcat(str, "[Multi-ID] ");
330 	if (ha->fw_attributes & BIT_13)
331 		strcat(str, "[Experimental]");
332 	return str;
333 }
334 
335 static inline srb_t *
336 qla2x00_get_new_sp(scsi_qla_host_t *ha, fc_port_t *fcport,
337     struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
338 {
339 	srb_t *sp;
340 
341 	sp = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
342 	if (!sp)
343 		return sp;
344 
345 	atomic_set(&sp->ref_count, 1);
346 	sp->ha = ha;
347 	sp->fcport = fcport;
348 	sp->cmd = cmd;
349 	sp->flags = 0;
350 	CMD_SP(cmd) = (void *)sp;
351 	cmd->scsi_done = done;
352 
353 	return sp;
354 }
355 
356 static int
357 qla2x00_queuecommand(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
358 {
359 	scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
360 	fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
361 	srb_t *sp;
362 	int rval;
363 
364 	if (!fcport) {
365 		cmd->result = DID_NO_CONNECT << 16;
366 		goto qc_fail_command;
367 	}
368 
369 	if (atomic_read(&fcport->state) != FCS_ONLINE) {
370 		if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD ||
371 		    atomic_read(&ha->loop_state) == LOOP_DEAD) {
372 			cmd->result = DID_NO_CONNECT << 16;
373 			goto qc_fail_command;
374 		}
375 		goto qc_host_busy;
376 	}
377 
378 	spin_unlock_irq(ha->host->host_lock);
379 
380 	sp = qla2x00_get_new_sp(ha, fcport, cmd, done);
381 	if (!sp)
382 		goto qc_host_busy_lock;
383 
384 	rval = qla2x00_start_scsi(sp);
385 	if (rval != QLA_SUCCESS)
386 		goto qc_host_busy_free_sp;
387 
388 	/* Manage unprocessed RIO/ZIO commands in response queue. */
389 	if (ha->flags.online && ha->flags.process_response_queue &&
390 	    ha->response_ring_ptr->signature != RESPONSE_PROCESSED) {
391 		unsigned long flags;
392 
393 		spin_lock_irqsave(&ha->hardware_lock, flags);
394 		qla2x00_process_response_queue(ha);
395 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
396 	}
397 
398 	spin_lock_irq(ha->host->host_lock);
399 
400 	return 0;
401 
402 qc_host_busy_free_sp:
403 	qla2x00_sp_free_dma(ha, sp);
404 	mempool_free(sp, ha->srb_mempool);
405 
406 qc_host_busy_lock:
407 	spin_lock_irq(ha->host->host_lock);
408 
409 qc_host_busy:
410 	return SCSI_MLQUEUE_HOST_BUSY;
411 
412 qc_fail_command:
413 	done(cmd);
414 
415 	return 0;
416 }
417 
418 
419 static int
420 qla24xx_queuecommand(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
421 {
422 	scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
423 	fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
424 	srb_t *sp;
425 	int rval;
426 
427 	if (!fcport) {
428 		cmd->result = DID_NO_CONNECT << 16;
429 		goto qc24_fail_command;
430 	}
431 
432 	if (atomic_read(&fcport->state) != FCS_ONLINE) {
433 		if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD ||
434 		    atomic_read(&ha->loop_state) == LOOP_DEAD) {
435 			cmd->result = DID_NO_CONNECT << 16;
436 			goto qc24_fail_command;
437 		}
438 		goto qc24_host_busy;
439 	}
440 
441 	spin_unlock_irq(ha->host->host_lock);
442 
443 	sp = qla2x00_get_new_sp(ha, fcport, cmd, done);
444 	if (!sp)
445 		goto qc24_host_busy_lock;
446 
447 	rval = qla24xx_start_scsi(sp);
448 	if (rval != QLA_SUCCESS)
449 		goto qc24_host_busy_free_sp;
450 
451 	spin_lock_irq(ha->host->host_lock);
452 
453 	return 0;
454 
455 qc24_host_busy_free_sp:
456 	qla2x00_sp_free_dma(ha, sp);
457 	mempool_free(sp, ha->srb_mempool);
458 
459 qc24_host_busy_lock:
460 	spin_lock_irq(ha->host->host_lock);
461 
462 qc24_host_busy:
463 	return SCSI_MLQUEUE_HOST_BUSY;
464 
465 qc24_fail_command:
466 	done(cmd);
467 
468 	return 0;
469 }
470 
471 
472 /*
473  * qla2x00_eh_wait_on_command
474  *    Waits for the command to be returned by the Firmware for some
475  *    max time.
476  *
477  * Input:
478  *    ha = actual ha whose done queue will contain the command
479  *	      returned by firmware.
480  *    cmd = Scsi Command to wait on.
481  *    flag = Abort/Reset(Bus or Device Reset)
482  *
483  * Return:
484  *    Not Found : 0
485  *    Found : 1
486  */
487 static int
488 qla2x00_eh_wait_on_command(scsi_qla_host_t *ha, struct scsi_cmnd *cmd)
489 {
490 #define ABORT_POLLING_PERIOD	HZ
491 #define ABORT_WAIT_ITER		((10 * HZ) / (ABORT_POLLING_PERIOD))
492 	unsigned long wait_iter = ABORT_WAIT_ITER;
493 	int ret = QLA_SUCCESS;
494 
495 	while (CMD_SP(cmd)) {
496 		set_current_state(TASK_UNINTERRUPTIBLE);
497 		schedule_timeout(ABORT_POLLING_PERIOD);
498 
499 		if (--wait_iter)
500 			break;
501 	}
502 	if (CMD_SP(cmd))
503 		ret = QLA_FUNCTION_FAILED;
504 
505 	return ret;
506 }
507 
508 /*
509  * qla2x00_wait_for_hba_online
510  *    Wait till the HBA is online after going through
511  *    <= MAX_RETRIES_OF_ISP_ABORT  or
512  *    finally HBA is disabled ie marked offline
513  *
514  * Input:
515  *     ha - pointer to host adapter structure
516  *
517  * Note:
518  *    Does context switching-Release SPIN_LOCK
519  *    (if any) before calling this routine.
520  *
521  * Return:
522  *    Success (Adapter is online) : 0
523  *    Failed  (Adapter is offline/disabled) : 1
524  */
525 static int
526 qla2x00_wait_for_hba_online(scsi_qla_host_t *ha)
527 {
528 	int		return_status;
529 	unsigned long	wait_online;
530 
531 	wait_online = jiffies + (MAX_LOOP_TIMEOUT * HZ);
532 	while (((test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) ||
533 	    test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags) ||
534 	    test_bit(ISP_ABORT_RETRY, &ha->dpc_flags) ||
535 	    ha->dpc_active) && time_before(jiffies, wait_online)) {
536 
537 		msleep(1000);
538 	}
539 	if (ha->flags.online)
540 		return_status = QLA_SUCCESS;
541 	else
542 		return_status = QLA_FUNCTION_FAILED;
543 
544 	DEBUG2(printk("%s return_status=%d\n",__func__,return_status));
545 
546 	return (return_status);
547 }
548 
549 /*
550  * qla2x00_wait_for_loop_ready
551  *    Wait for MAX_LOOP_TIMEOUT(5 min) value for loop
552  *    to be in LOOP_READY state.
553  * Input:
554  *     ha - pointer to host adapter structure
555  *
556  * Note:
557  *    Does context switching-Release SPIN_LOCK
558  *    (if any) before calling this routine.
559  *
560  *
561  * Return:
562  *    Success (LOOP_READY) : 0
563  *    Failed  (LOOP_NOT_READY) : 1
564  */
565 static inline int
566 qla2x00_wait_for_loop_ready(scsi_qla_host_t *ha)
567 {
568 	int 	 return_status = QLA_SUCCESS;
569 	unsigned long loop_timeout ;
570 
571 	/* wait for 5 min at the max for loop to be ready */
572 	loop_timeout = jiffies + (MAX_LOOP_TIMEOUT * HZ);
573 
574 	while ((!atomic_read(&ha->loop_down_timer) &&
575 	    atomic_read(&ha->loop_state) == LOOP_DOWN) ||
576 	    atomic_read(&ha->loop_state) != LOOP_READY) {
577 		msleep(1000);
578 		if (time_after_eq(jiffies, loop_timeout)) {
579 			return_status = QLA_FUNCTION_FAILED;
580 			break;
581 		}
582 	}
583 	return (return_status);
584 }
585 
586 /**************************************************************************
587 * qla2xxx_eh_abort
588 *
589 * Description:
590 *    The abort function will abort the specified command.
591 *
592 * Input:
593 *    cmd = Linux SCSI command packet to be aborted.
594 *
595 * Returns:
596 *    Either SUCCESS or FAILED.
597 *
598 * Note:
599 **************************************************************************/
600 int
601 qla2xxx_eh_abort(struct scsi_cmnd *cmd)
602 {
603 	scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
604 	srb_t *sp;
605 	int ret, i;
606 	unsigned int id, lun;
607 	unsigned long serial;
608 	unsigned long flags;
609 
610 	if (!CMD_SP(cmd))
611 		return FAILED;
612 
613 	ret = FAILED;
614 
615 	id = cmd->device->id;
616 	lun = cmd->device->lun;
617 	serial = cmd->serial_number;
618 
619 	/* Check active list for command command. */
620 	spin_lock_irqsave(&ha->hardware_lock, flags);
621 	for (i = 1; i < MAX_OUTSTANDING_COMMANDS; i++) {
622 		sp = ha->outstanding_cmds[i];
623 
624 		if (sp == NULL)
625 			continue;
626 
627 		if (sp->cmd != cmd)
628 			continue;
629 
630 		DEBUG2(printk("%s(%ld): aborting sp %p from RISC. pid=%ld "
631 		    "sp->state=%x\n", __func__, ha->host_no, sp, serial,
632 		    sp->state));
633 		DEBUG3(qla2x00_print_scsi_cmd(cmd);)
634 
635 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
636 		if (ha->isp_ops.abort_command(ha, sp)) {
637 			DEBUG2(printk("%s(%ld): abort_command "
638 			    "mbx failed.\n", __func__, ha->host_no));
639 		} else {
640 			DEBUG3(printk("%s(%ld): abort_command "
641 			    "mbx success.\n", __func__, ha->host_no));
642 			ret = SUCCESS;
643 		}
644 		spin_lock_irqsave(&ha->hardware_lock, flags);
645 
646 		break;
647 	}
648 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
649 
650 	/* Wait for the command to be returned. */
651 	if (ret == SUCCESS) {
652 		if (qla2x00_eh_wait_on_command(ha, cmd) != QLA_SUCCESS) {
653 			qla_printk(KERN_ERR, ha,
654 			    "scsi(%ld:%d:%d): Abort handler timed out -- %lx "
655 			    "%x.\n", ha->host_no, id, lun, serial, ret);
656 		}
657 	}
658 
659 	qla_printk(KERN_INFO, ha,
660 	    "scsi(%ld:%d:%d): Abort command issued -- %lx %x.\n", ha->host_no,
661 	    id, lun, serial, ret);
662 
663 	return ret;
664 }
665 
666 /**************************************************************************
667 * qla2x00_eh_wait_for_pending_target_commands
668 *
669 * Description:
670 *    Waits for all the commands to come back from the specified target.
671 *
672 * Input:
673 *    ha - pointer to scsi_qla_host structure.
674 *    t  - target
675 * Returns:
676 *    Either SUCCESS or FAILED.
677 *
678 * Note:
679 **************************************************************************/
680 static int
681 qla2x00_eh_wait_for_pending_target_commands(scsi_qla_host_t *ha, unsigned int t)
682 {
683 	int	cnt;
684 	int	status;
685 	srb_t		*sp;
686 	struct scsi_cmnd *cmd;
687 	unsigned long flags;
688 
689 	status = 0;
690 
691 	/*
692 	 * Waiting for all commands for the designated target in the active
693 	 * array
694 	 */
695 	for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
696 		spin_lock_irqsave(&ha->hardware_lock, flags);
697 		sp = ha->outstanding_cmds[cnt];
698 		if (sp) {
699 			cmd = sp->cmd;
700 			spin_unlock_irqrestore(&ha->hardware_lock, flags);
701 			if (cmd->device->id == t) {
702 				if (!qla2x00_eh_wait_on_command(ha, cmd)) {
703 					status = 1;
704 					break;
705 				}
706 			}
707 		} else {
708 			spin_unlock_irqrestore(&ha->hardware_lock, flags);
709 		}
710 	}
711 	return (status);
712 }
713 
714 
715 /**************************************************************************
716 * qla2xxx_eh_device_reset
717 *
718 * Description:
719 *    The device reset function will reset the target and abort any
720 *    executing commands.
721 *
722 *    NOTE: The use of SP is undefined within this context.  Do *NOT*
723 *          attempt to use this value, even if you determine it is
724 *          non-null.
725 *
726 * Input:
727 *    cmd = Linux SCSI command packet of the command that cause the
728 *          bus device reset.
729 *
730 * Returns:
731 *    SUCCESS/FAILURE (defined as macro in scsi.h).
732 *
733 **************************************************************************/
734 int
735 qla2xxx_eh_device_reset(struct scsi_cmnd *cmd)
736 {
737 	scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
738 	fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
739 	srb_t *sp;
740 	int ret;
741 	unsigned int id, lun;
742 	unsigned long serial;
743 
744 	ret = FAILED;
745 
746 	id = cmd->device->id;
747 	lun = cmd->device->lun;
748 	serial = cmd->serial_number;
749 
750 	sp = (srb_t *) CMD_SP(cmd);
751 	if (!sp || !fcport)
752 		return ret;
753 
754 	qla_printk(KERN_INFO, ha,
755 	    "scsi(%ld:%d:%d): DEVICE RESET ISSUED.\n", ha->host_no, id, lun);
756 
757 	if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
758 		goto eh_dev_reset_done;
759 
760 	if (qla2x00_wait_for_loop_ready(ha) == QLA_SUCCESS) {
761 		if (qla2x00_device_reset(ha, fcport) == 0)
762 			ret = SUCCESS;
763 
764 #if defined(LOGOUT_AFTER_DEVICE_RESET)
765 		if (ret == SUCCESS) {
766 			if (fcport->flags & FC_FABRIC_DEVICE) {
767 				ha->isp_ops.fabric_logout(ha, fcport->loop_id);
768 				qla2x00_mark_device_lost(ha, fcport);
769 			}
770 		}
771 #endif
772 	} else {
773 		DEBUG2(printk(KERN_INFO
774 		    "%s failed: loop not ready\n",__func__);)
775 	}
776 
777 	if (ret == FAILED) {
778 		DEBUG3(printk("%s(%ld): device reset failed\n",
779 		    __func__, ha->host_no));
780 		qla_printk(KERN_INFO, ha, "%s: device reset failed\n",
781 		    __func__);
782 
783 		goto eh_dev_reset_done;
784 	}
785 
786 	/*
787 	 * If we are coming down the EH path, wait for all commands to
788 	 * complete for the device.
789 	 */
790 	if (cmd->device->host->eh_active) {
791 		if (qla2x00_eh_wait_for_pending_target_commands(ha, id))
792 			ret = FAILED;
793 
794 		if (ret == FAILED) {
795 			DEBUG3(printk("%s(%ld): failed while waiting for "
796 			    "commands\n", __func__, ha->host_no));
797 			qla_printk(KERN_INFO, ha,
798 			    "%s: failed while waiting for commands\n",
799 			    __func__);
800 
801 			goto eh_dev_reset_done;
802 		}
803 	}
804 
805 	qla_printk(KERN_INFO, ha,
806 	    "scsi(%ld:%d:%d): DEVICE RESET SUCCEEDED.\n", ha->host_no, id, lun);
807 
808 eh_dev_reset_done:
809 	return ret;
810 }
811 
812 /**************************************************************************
813 * qla2x00_eh_wait_for_pending_commands
814 *
815 * Description:
816 *    Waits for all the commands to come back from the specified host.
817 *
818 * Input:
819 *    ha - pointer to scsi_qla_host structure.
820 *
821 * Returns:
822 *    1 : SUCCESS
823 *    0 : FAILED
824 *
825 * Note:
826 **************************************************************************/
827 static int
828 qla2x00_eh_wait_for_pending_commands(scsi_qla_host_t *ha)
829 {
830 	int	cnt;
831 	int	status;
832 	srb_t		*sp;
833 	struct scsi_cmnd *cmd;
834 	unsigned long flags;
835 
836 	status = 1;
837 
838 	/*
839 	 * Waiting for all commands for the designated target in the active
840 	 * array
841 	 */
842 	for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
843 		spin_lock_irqsave(&ha->hardware_lock, flags);
844 		sp = ha->outstanding_cmds[cnt];
845 		if (sp) {
846 			cmd = sp->cmd;
847 			spin_unlock_irqrestore(&ha->hardware_lock, flags);
848 			status = qla2x00_eh_wait_on_command(ha, cmd);
849 			if (status == 0)
850 				break;
851 		}
852 		else {
853 			spin_unlock_irqrestore(&ha->hardware_lock, flags);
854 		}
855 	}
856 	return (status);
857 }
858 
859 
860 /**************************************************************************
861 * qla2xxx_eh_bus_reset
862 *
863 * Description:
864 *    The bus reset function will reset the bus and abort any executing
865 *    commands.
866 *
867 * Input:
868 *    cmd = Linux SCSI command packet of the command that cause the
869 *          bus reset.
870 *
871 * Returns:
872 *    SUCCESS/FAILURE (defined as macro in scsi.h).
873 *
874 **************************************************************************/
875 int
876 qla2xxx_eh_bus_reset(struct scsi_cmnd *cmd)
877 {
878 	scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
879 	fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
880 	srb_t *sp;
881 	int ret;
882 	unsigned int id, lun;
883 	unsigned long serial;
884 
885 	ret = FAILED;
886 
887 	id = cmd->device->id;
888 	lun = cmd->device->lun;
889 	serial = cmd->serial_number;
890 
891 	sp = (srb_t *) CMD_SP(cmd);
892 	if (!sp || !fcport)
893 		return ret;
894 
895 	qla_printk(KERN_INFO, ha,
896 	    "scsi(%ld:%d:%d): LOOP RESET ISSUED.\n", ha->host_no, id, lun);
897 
898 	if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS) {
899 		DEBUG2(printk("%s failed:board disabled\n",__func__));
900 		goto eh_bus_reset_done;
901 	}
902 
903 	if (qla2x00_wait_for_loop_ready(ha) == QLA_SUCCESS) {
904 		if (qla2x00_loop_reset(ha) == QLA_SUCCESS)
905 			ret = SUCCESS;
906 	}
907 	if (ret == FAILED)
908 		goto eh_bus_reset_done;
909 
910 	/* Waiting for our command in done_queue to be returned to OS.*/
911 	if (cmd->device->host->eh_active)
912 		if (!qla2x00_eh_wait_for_pending_commands(ha))
913 			ret = FAILED;
914 
915 eh_bus_reset_done:
916 	qla_printk(KERN_INFO, ha, "%s: reset %s\n", __func__,
917 	    (ret == FAILED) ? "failed" : "succeded");
918 
919 	return ret;
920 }
921 
922 /**************************************************************************
923 * qla2xxx_eh_host_reset
924 *
925 * Description:
926 *    The reset function will reset the Adapter.
927 *
928 * Input:
929 *      cmd = Linux SCSI command packet of the command that cause the
930 *            adapter reset.
931 *
932 * Returns:
933 *      Either SUCCESS or FAILED.
934 *
935 * Note:
936 **************************************************************************/
937 int
938 qla2xxx_eh_host_reset(struct scsi_cmnd *cmd)
939 {
940 	scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
941 	fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
942 	srb_t *sp;
943 	int ret;
944 	unsigned int id, lun;
945 	unsigned long serial;
946 
947 	ret = FAILED;
948 
949 	id = cmd->device->id;
950 	lun = cmd->device->lun;
951 	serial = cmd->serial_number;
952 
953 	sp = (srb_t *) CMD_SP(cmd);
954 	if (!sp || !fcport)
955 		return ret;
956 
957 	qla_printk(KERN_INFO, ha,
958 	    "scsi(%ld:%d:%d): ADAPTER RESET ISSUED.\n", ha->host_no, id, lun);
959 
960 	if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
961 		goto eh_host_reset_lock;
962 
963 	/*
964 	 * Fixme-may be dpc thread is active and processing
965 	 * loop_resync,so wait a while for it to
966 	 * be completed and then issue big hammer.Otherwise
967 	 * it may cause I/O failure as big hammer marks the
968 	 * devices as lost kicking of the port_down_timer
969 	 * while dpc is stuck for the mailbox to complete.
970 	 */
971 	qla2x00_wait_for_loop_ready(ha);
972 	set_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
973 	if (qla2x00_abort_isp(ha)) {
974 		clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
975 		/* failed. schedule dpc to try */
976 		set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
977 
978 		if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
979 			goto eh_host_reset_lock;
980 	}
981 	clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
982 
983 	/* Waiting for our command in done_queue to be returned to OS.*/
984 	if (qla2x00_eh_wait_for_pending_commands(ha))
985 		ret = SUCCESS;
986 
987 eh_host_reset_lock:
988 	qla_printk(KERN_INFO, ha, "%s: reset %s\n", __func__,
989 	    (ret == FAILED) ? "failed" : "succeded");
990 
991 	return ret;
992 }
993 
994 /*
995 * qla2x00_loop_reset
996 *      Issue loop reset.
997 *
998 * Input:
999 *      ha = adapter block pointer.
1000 *
1001 * Returns:
1002 *      0 = success
1003 */
1004 static int
1005 qla2x00_loop_reset(scsi_qla_host_t *ha)
1006 {
1007 	int status = QLA_SUCCESS;
1008 	struct fc_port *fcport;
1009 
1010 	if (ha->flags.enable_lip_reset) {
1011 		status = qla2x00_lip_reset(ha);
1012 	}
1013 
1014 	if (status == QLA_SUCCESS && ha->flags.enable_target_reset) {
1015 		list_for_each_entry(fcport, &ha->fcports, list) {
1016 			if (fcport->port_type != FCT_TARGET)
1017 				continue;
1018 
1019 			status = qla2x00_target_reset(ha, fcport);
1020 			if (status != QLA_SUCCESS)
1021 				break;
1022 		}
1023 	}
1024 
1025 	if (status == QLA_SUCCESS &&
1026 		((!ha->flags.enable_target_reset &&
1027 		  !ha->flags.enable_lip_reset) ||
1028 		ha->flags.enable_lip_full_login)) {
1029 
1030 		status = qla2x00_full_login_lip(ha);
1031 	}
1032 
1033 	/* Issue marker command only when we are going to start the I/O */
1034 	ha->marker_needed = 1;
1035 
1036 	if (status) {
1037 		/* Empty */
1038 		DEBUG2_3(printk("%s(%ld): **** FAILED ****\n",
1039 				__func__,
1040 				ha->host_no);)
1041 	} else {
1042 		/* Empty */
1043 		DEBUG3(printk("%s(%ld): exiting normally.\n",
1044 				__func__,
1045 				ha->host_no);)
1046 	}
1047 
1048 	return(status);
1049 }
1050 
1051 /*
1052  * qla2x00_device_reset
1053  *	Issue bus device reset message to the target.
1054  *
1055  * Input:
1056  *	ha = adapter block pointer.
1057  *	t = SCSI ID.
1058  *	TARGET_QUEUE_LOCK must be released.
1059  *	ADAPTER_STATE_LOCK must be released.
1060  *
1061  * Context:
1062  *	Kernel context.
1063  */
1064 static int
1065 qla2x00_device_reset(scsi_qla_host_t *ha, fc_port_t *reset_fcport)
1066 {
1067 	/* Abort Target command will clear Reservation */
1068 	return ha->isp_ops.abort_target(reset_fcport);
1069 }
1070 
1071 static int
1072 qla2xxx_slave_alloc(struct scsi_device *sdev)
1073 {
1074 	struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1075 
1076 	if (!rport)
1077 		return -ENXIO;
1078 
1079 	sdev->hostdata = rport->dd_data;
1080 
1081 	return 0;
1082 }
1083 
1084 static int
1085 qla2xxx_slave_configure(struct scsi_device *sdev)
1086 {
1087 	scsi_qla_host_t *ha = to_qla_host(sdev->host);
1088 	struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
1089 
1090 	if (sdev->tagged_supported)
1091 		scsi_activate_tcq(sdev, 32);
1092 	else
1093 		scsi_deactivate_tcq(sdev, 32);
1094 
1095 	rport->dev_loss_tmo = ha->port_down_retry_count + 5;
1096 
1097 	return 0;
1098 }
1099 
1100 static void
1101 qla2xxx_slave_destroy(struct scsi_device *sdev)
1102 {
1103 	sdev->hostdata = NULL;
1104 }
1105 
1106 /**
1107  * qla2x00_config_dma_addressing() - Configure OS DMA addressing method.
1108  * @ha: HA context
1109  *
1110  * At exit, the @ha's flags.enable_64bit_addressing set to indicated
1111  * supported addressing method.
1112  */
1113 static void
1114 qla2x00_config_dma_addressing(scsi_qla_host_t *ha)
1115 {
1116 	/* Assume 32bit DMA address */
1117 	ha->flags.enable_64bit_addressing = 0;
1118 
1119 	/*
1120 	 * Given the two variants pci_set_dma_mask(), allow the compiler to
1121 	 * assist in setting the proper dma mask.
1122 	 */
1123 	if (sizeof(dma_addr_t) > 4) {
1124 		if (pci_set_dma_mask(ha->pdev, DMA_64BIT_MASK) == 0) {
1125 			ha->flags.enable_64bit_addressing = 1;
1126 			ha->isp_ops.calc_req_entries = qla2x00_calc_iocbs_64;
1127 			ha->isp_ops.build_iocbs = qla2x00_build_scsi_iocbs_64;
1128 
1129 			if (pci_set_consistent_dma_mask(ha->pdev,
1130 			    DMA_64BIT_MASK)) {
1131 				qla_printk(KERN_DEBUG, ha,
1132 				    "Failed to set 64 bit PCI consistent mask; "
1133 				    "using 32 bit.\n");
1134 				pci_set_consistent_dma_mask(ha->pdev,
1135 				    DMA_32BIT_MASK);
1136 			}
1137 		} else {
1138 			qla_printk(KERN_DEBUG, ha,
1139 			    "Failed to set 64 bit PCI DMA mask, falling back "
1140 			    "to 32 bit MASK.\n");
1141 			pci_set_dma_mask(ha->pdev, DMA_32BIT_MASK);
1142 		}
1143 	} else {
1144 		pci_set_dma_mask(ha->pdev, DMA_32BIT_MASK);
1145 	}
1146 }
1147 
1148 static int
1149 qla2x00_iospace_config(scsi_qla_host_t *ha)
1150 {
1151 	unsigned long	pio, pio_len, pio_flags;
1152 	unsigned long	mmio, mmio_len, mmio_flags;
1153 
1154 	/* We only need PIO for Flash operations on ISP2312 v2 chips. */
1155 	pio = pci_resource_start(ha->pdev, 0);
1156 	pio_len = pci_resource_len(ha->pdev, 0);
1157 	pio_flags = pci_resource_flags(ha->pdev, 0);
1158 	if (pio_flags & IORESOURCE_IO) {
1159 		if (pio_len < MIN_IOBASE_LEN) {
1160 			qla_printk(KERN_WARNING, ha,
1161 			    "Invalid PCI I/O region size (%s)...\n",
1162 				pci_name(ha->pdev));
1163 			pio = 0;
1164 		}
1165 	} else {
1166 		qla_printk(KERN_WARNING, ha,
1167 		    "region #0 not a PIO resource (%s)...\n",
1168 		    pci_name(ha->pdev));
1169 		pio = 0;
1170 	}
1171 
1172 	/* Use MMIO operations for all accesses. */
1173 	mmio = pci_resource_start(ha->pdev, 1);
1174 	mmio_len = pci_resource_len(ha->pdev, 1);
1175 	mmio_flags = pci_resource_flags(ha->pdev, 1);
1176 
1177 	if (!(mmio_flags & IORESOURCE_MEM)) {
1178 		qla_printk(KERN_ERR, ha,
1179 		    "region #0 not an MMIO resource (%s), aborting\n",
1180 		    pci_name(ha->pdev));
1181 		goto iospace_error_exit;
1182 	}
1183 	if (mmio_len < MIN_IOBASE_LEN) {
1184 		qla_printk(KERN_ERR, ha,
1185 		    "Invalid PCI mem region size (%s), aborting\n",
1186 			pci_name(ha->pdev));
1187 		goto iospace_error_exit;
1188 	}
1189 
1190 	if (pci_request_regions(ha->pdev, ha->brd_info->drv_name)) {
1191 		qla_printk(KERN_WARNING, ha,
1192 		    "Failed to reserve PIO/MMIO regions (%s)\n",
1193 		    pci_name(ha->pdev));
1194 
1195 		goto iospace_error_exit;
1196 	}
1197 
1198 	ha->pio_address = pio;
1199 	ha->pio_length = pio_len;
1200 	ha->iobase = ioremap(mmio, MIN_IOBASE_LEN);
1201 	if (!ha->iobase) {
1202 		qla_printk(KERN_ERR, ha,
1203 		    "cannot remap MMIO (%s), aborting\n", pci_name(ha->pdev));
1204 
1205 		goto iospace_error_exit;
1206 	}
1207 
1208 	return (0);
1209 
1210 iospace_error_exit:
1211 	return (-ENOMEM);
1212 }
1213 
1214 static void
1215 qla2x00_enable_intrs(scsi_qla_host_t *ha)
1216 {
1217 	unsigned long flags = 0;
1218 	struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1219 
1220 	spin_lock_irqsave(&ha->hardware_lock, flags);
1221 	ha->interrupts_on = 1;
1222 	/* enable risc and host interrupts */
1223 	WRT_REG_WORD(&reg->ictrl, ICR_EN_INT | ICR_EN_RISC);
1224 	RD_REG_WORD(&reg->ictrl);
1225 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
1226 
1227 }
1228 
1229 static void
1230 qla2x00_disable_intrs(scsi_qla_host_t *ha)
1231 {
1232 	unsigned long flags = 0;
1233 	struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1234 
1235 	spin_lock_irqsave(&ha->hardware_lock, flags);
1236 	ha->interrupts_on = 0;
1237 	/* disable risc and host interrupts */
1238 	WRT_REG_WORD(&reg->ictrl, 0);
1239 	RD_REG_WORD(&reg->ictrl);
1240 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
1241 }
1242 
1243 static void
1244 qla24xx_enable_intrs(scsi_qla_host_t *ha)
1245 {
1246 	unsigned long flags = 0;
1247 	struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1248 
1249 	spin_lock_irqsave(&ha->hardware_lock, flags);
1250 	ha->interrupts_on = 1;
1251 	WRT_REG_DWORD(&reg->ictrl, ICRX_EN_RISC_INT);
1252 	RD_REG_DWORD(&reg->ictrl);
1253 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
1254 }
1255 
1256 static void
1257 qla24xx_disable_intrs(scsi_qla_host_t *ha)
1258 {
1259 	unsigned long flags = 0;
1260 	struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1261 
1262 	spin_lock_irqsave(&ha->hardware_lock, flags);
1263 	ha->interrupts_on = 0;
1264 	WRT_REG_DWORD(&reg->ictrl, 0);
1265 	RD_REG_DWORD(&reg->ictrl);
1266 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
1267 }
1268 
1269 /*
1270  * PCI driver interface
1271  */
1272 int qla2x00_probe_one(struct pci_dev *pdev, struct qla_board_info *brd_info)
1273 {
1274 	int	ret = -ENODEV;
1275 	device_reg_t __iomem *reg;
1276 	struct Scsi_Host *host;
1277 	scsi_qla_host_t *ha;
1278 	unsigned long	flags = 0;
1279 	unsigned long	wait_switch = 0;
1280 	char pci_info[20];
1281 	char fw_str[30];
1282 	fc_port_t *fcport;
1283 
1284 	if (pci_enable_device(pdev))
1285 		goto probe_out;
1286 
1287 	host = scsi_host_alloc(brd_info->sht ? brd_info->sht:
1288 	    &qla2x00_driver_template, sizeof(scsi_qla_host_t));
1289 	if (host == NULL) {
1290 		printk(KERN_WARNING
1291 		    "qla2xxx: Couldn't allocate host from scsi layer!\n");
1292 		goto probe_disable_device;
1293 	}
1294 
1295 	/* Clear our data area */
1296 	ha = (scsi_qla_host_t *)host->hostdata;
1297 	memset(ha, 0, sizeof(scsi_qla_host_t));
1298 
1299 	ha->pdev = pdev;
1300 	ha->host = host;
1301 	ha->host_no = host->host_no;
1302 	ha->brd_info = brd_info;
1303 	sprintf(ha->host_str, "%s_%ld", ha->brd_info->drv_name, ha->host_no);
1304 
1305 	/* Configure PCI I/O space */
1306 	ret = qla2x00_iospace_config(ha);
1307 	if (ret)
1308 		goto probe_failed;
1309 
1310 	qla_printk(KERN_INFO, ha,
1311 	    "Found an %s, irq %d, iobase 0x%p\n", ha->brd_info->isp_name,
1312 	    pdev->irq, ha->iobase);
1313 
1314 	spin_lock_init(&ha->hardware_lock);
1315 
1316 	ha->prev_topology = 0;
1317 	ha->ports = MAX_BUSES;
1318 	ha->init_cb_size = sizeof(init_cb_t);
1319 
1320 	/* Assign ISP specific operations. */
1321 	ha->isp_ops.pci_config		= qla2100_pci_config;
1322 	ha->isp_ops.reset_chip		= qla2x00_reset_chip;
1323 	ha->isp_ops.chip_diag		= qla2x00_chip_diag;
1324 	ha->isp_ops.config_rings	= qla2x00_config_rings;
1325 	ha->isp_ops.reset_adapter	= qla2x00_reset_adapter;
1326 	ha->isp_ops.nvram_config	= qla2x00_nvram_config;
1327 	ha->isp_ops.update_fw_options	= qla2x00_update_fw_options;
1328 	ha->isp_ops.load_risc		= qla2x00_load_risc;
1329 	ha->isp_ops.pci_info_str	= qla2x00_pci_info_str;
1330 	ha->isp_ops.fw_version_str	= qla2x00_fw_version_str;
1331 	ha->isp_ops.intr_handler	= qla2100_intr_handler;
1332 	ha->isp_ops.enable_intrs	= qla2x00_enable_intrs;
1333 	ha->isp_ops.disable_intrs	= qla2x00_disable_intrs;
1334 	ha->isp_ops.abort_command	= qla2x00_abort_command;
1335 	ha->isp_ops.abort_target	= qla2x00_abort_target;
1336 	ha->isp_ops.fabric_login	= qla2x00_login_fabric;
1337 	ha->isp_ops.fabric_logout	= qla2x00_fabric_logout;
1338 	ha->isp_ops.calc_req_entries	= qla2x00_calc_iocbs_32;
1339 	ha->isp_ops.build_iocbs		= qla2x00_build_scsi_iocbs_32;
1340 	ha->isp_ops.prep_ms_iocb	= qla2x00_prep_ms_iocb;
1341 	ha->isp_ops.read_nvram		= qla2x00_read_nvram_data;
1342 	ha->isp_ops.write_nvram		= qla2x00_write_nvram_data;
1343 	ha->isp_ops.fw_dump		= qla2100_fw_dump;
1344 	ha->isp_ops.ascii_fw_dump	= qla2100_ascii_fw_dump;
1345 	if (IS_QLA2100(ha)) {
1346 		host->max_id = MAX_TARGETS_2100;
1347 		ha->mbx_count = MAILBOX_REGISTER_COUNT_2100;
1348 		ha->request_q_length = REQUEST_ENTRY_CNT_2100;
1349 		ha->response_q_length = RESPONSE_ENTRY_CNT_2100;
1350 		ha->last_loop_id = SNS_LAST_LOOP_ID_2100;
1351 		host->sg_tablesize = 32;
1352 		ha->gid_list_info_size = 4;
1353 	} else if (IS_QLA2200(ha)) {
1354 		host->max_id = MAX_TARGETS_2200;
1355 		ha->mbx_count = MAILBOX_REGISTER_COUNT;
1356 		ha->request_q_length = REQUEST_ENTRY_CNT_2200;
1357 		ha->response_q_length = RESPONSE_ENTRY_CNT_2100;
1358 		ha->last_loop_id = SNS_LAST_LOOP_ID_2100;
1359 		ha->gid_list_info_size = 4;
1360 	} else if (IS_QLA23XX(ha)) {
1361 		host->max_id = MAX_TARGETS_2200;
1362 		ha->mbx_count = MAILBOX_REGISTER_COUNT;
1363 		ha->request_q_length = REQUEST_ENTRY_CNT_2200;
1364 		ha->response_q_length = RESPONSE_ENTRY_CNT_2300;
1365 		ha->last_loop_id = SNS_LAST_LOOP_ID_2300;
1366 		ha->isp_ops.pci_config = qla2300_pci_config;
1367 		ha->isp_ops.intr_handler = qla2300_intr_handler;
1368 		ha->isp_ops.fw_dump = qla2300_fw_dump;
1369 		ha->isp_ops.ascii_fw_dump = qla2300_ascii_fw_dump;
1370 		ha->gid_list_info_size = 6;
1371 	} else if (IS_QLA24XX(ha) || IS_QLA25XX(ha)) {
1372 		host->max_id = MAX_TARGETS_2200;
1373 		ha->mbx_count = MAILBOX_REGISTER_COUNT;
1374 		ha->request_q_length = REQUEST_ENTRY_CNT_24XX;
1375 		ha->response_q_length = RESPONSE_ENTRY_CNT_2300;
1376 		ha->last_loop_id = SNS_LAST_LOOP_ID_2300;
1377 		ha->init_cb_size = sizeof(struct init_cb_24xx);
1378 		ha->isp_ops.pci_config = qla24xx_pci_config;
1379 		ha->isp_ops.reset_chip = qla24xx_reset_chip;
1380 		ha->isp_ops.chip_diag = qla24xx_chip_diag;
1381 		ha->isp_ops.config_rings = qla24xx_config_rings;
1382 		ha->isp_ops.reset_adapter = qla24xx_reset_adapter;
1383 		ha->isp_ops.nvram_config = qla24xx_nvram_config;
1384 		ha->isp_ops.update_fw_options = qla24xx_update_fw_options;
1385 		ha->isp_ops.load_risc = qla24xx_load_risc_flash;
1386 		if (ql2xfwloadbin)
1387 			ha->isp_ops.load_risc = qla24xx_load_risc_hotplug;
1388 		ha->isp_ops.pci_info_str = qla24xx_pci_info_str;
1389 		ha->isp_ops.fw_version_str = qla24xx_fw_version_str;
1390 		ha->isp_ops.intr_handler = qla24xx_intr_handler;
1391 		ha->isp_ops.enable_intrs = qla24xx_enable_intrs;
1392 		ha->isp_ops.disable_intrs = qla24xx_disable_intrs;
1393 		ha->isp_ops.abort_command = qla24xx_abort_command;
1394 		ha->isp_ops.abort_target = qla24xx_abort_target;
1395 		ha->isp_ops.fabric_login = qla24xx_login_fabric;
1396 		ha->isp_ops.fabric_logout = qla24xx_fabric_logout;
1397 		ha->isp_ops.prep_ms_iocb = qla24xx_prep_ms_iocb;
1398 		ha->isp_ops.read_nvram = qla24xx_read_nvram_data;
1399 		ha->isp_ops.write_nvram = qla24xx_write_nvram_data;
1400 		ha->isp_ops.fw_dump = qla24xx_fw_dump;
1401 		ha->isp_ops.ascii_fw_dump = qla24xx_ascii_fw_dump;
1402 		ha->gid_list_info_size = 8;
1403 	}
1404 	host->can_queue = ha->request_q_length + 128;
1405 
1406 	/* load the F/W, read paramaters, and init the H/W */
1407 	ha->instance = num_hosts;
1408 
1409 	init_MUTEX(&ha->mbx_cmd_sem);
1410 	init_MUTEX_LOCKED(&ha->mbx_intr_sem);
1411 
1412 	INIT_LIST_HEAD(&ha->list);
1413 	INIT_LIST_HEAD(&ha->fcports);
1414 	INIT_LIST_HEAD(&ha->rscn_fcports);
1415 
1416 	/*
1417 	 * These locks are used to prevent more than one CPU
1418 	 * from modifying the queue at the same time. The
1419 	 * higher level "host_lock" will reduce most
1420 	 * contention for these locks.
1421 	 */
1422 	spin_lock_init(&ha->mbx_reg_lock);
1423 
1424 	ha->dpc_pid = -1;
1425 	init_completion(&ha->dpc_inited);
1426 	init_completion(&ha->dpc_exited);
1427 
1428 	qla2x00_config_dma_addressing(ha);
1429 	if (qla2x00_mem_alloc(ha)) {
1430 		qla_printk(KERN_WARNING, ha,
1431 		    "[ERROR] Failed to allocate memory for adapter\n");
1432 
1433 		ret = -ENOMEM;
1434 		goto probe_failed;
1435 	}
1436 
1437 	if (qla2x00_initialize_adapter(ha) &&
1438 	    !(ha->device_flags & DFLG_NO_CABLE)) {
1439 
1440 		qla_printk(KERN_WARNING, ha,
1441 		    "Failed to initialize adapter\n");
1442 
1443 		DEBUG2(printk("scsi(%ld): Failed to initialize adapter - "
1444 		    "Adapter flags %x.\n",
1445 		    ha->host_no, ha->device_flags));
1446 
1447 		ret = -ENODEV;
1448 		goto probe_failed;
1449 	}
1450 
1451 	/*
1452 	 * Startup the kernel thread for this host adapter
1453 	 */
1454 	ha->dpc_should_die = 0;
1455 	ha->dpc_pid = kernel_thread(qla2x00_do_dpc, ha, 0);
1456 	if (ha->dpc_pid < 0) {
1457 		qla_printk(KERN_WARNING, ha,
1458 		    "Unable to start DPC thread!\n");
1459 
1460 		ret = -ENODEV;
1461 		goto probe_failed;
1462 	}
1463 	wait_for_completion(&ha->dpc_inited);
1464 
1465 	host->this_id = 255;
1466 	host->cmd_per_lun = 3;
1467 	host->unique_id = ha->instance;
1468 	host->max_cmd_len = MAX_CMDSZ;
1469 	host->max_channel = ha->ports - 1;
1470 	host->max_lun = MAX_LUNS;
1471 	host->transportt = qla2xxx_transport_template;
1472 
1473 	ret = request_irq(pdev->irq, ha->isp_ops.intr_handler,
1474 	    SA_INTERRUPT|SA_SHIRQ, ha->brd_info->drv_name, ha);
1475 	if (ret) {
1476 		qla_printk(KERN_WARNING, ha,
1477 		    "Failed to reserve interrupt %d already in use.\n",
1478 		    pdev->irq);
1479 		goto probe_failed;
1480 	}
1481 	host->irq = pdev->irq;
1482 
1483 	/* Initialized the timer */
1484 	qla2x00_start_timer(ha, qla2x00_timer, WATCH_INTERVAL);
1485 
1486 	DEBUG2(printk("DEBUG: detect hba %ld at address = %p\n",
1487 	    ha->host_no, ha));
1488 
1489 	ha->isp_ops.disable_intrs(ha);
1490 
1491 	spin_lock_irqsave(&ha->hardware_lock, flags);
1492 	reg = ha->iobase;
1493 	if (IS_QLA24XX(ha) || IS_QLA25XX(ha)) {
1494 		WRT_REG_DWORD(&reg->isp24.hccr, HCCRX_CLR_HOST_INT);
1495 		WRT_REG_DWORD(&reg->isp24.hccr, HCCRX_CLR_RISC_INT);
1496 	} else {
1497 		WRT_REG_WORD(&reg->isp.semaphore, 0);
1498 		WRT_REG_WORD(&reg->isp.hccr, HCCR_CLR_RISC_INT);
1499 		WRT_REG_WORD(&reg->isp.hccr, HCCR_CLR_HOST_INT);
1500 
1501 		/* Enable proper parity */
1502 		if (!IS_QLA2100(ha) && !IS_QLA2200(ha)) {
1503 			if (IS_QLA2300(ha))
1504 				/* SRAM parity */
1505 				WRT_REG_WORD(&reg->isp.hccr,
1506 				    (HCCR_ENABLE_PARITY + 0x1));
1507 			else
1508 				/* SRAM, Instruction RAM and GP RAM parity */
1509 				WRT_REG_WORD(&reg->isp.hccr,
1510 				    (HCCR_ENABLE_PARITY + 0x7));
1511 		}
1512 	}
1513 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
1514 
1515 	ha->isp_ops.enable_intrs(ha);
1516 
1517 	/* v2.19.5b6 */
1518 	/*
1519 	 * Wait around max loop_reset_delay secs for the devices to come
1520 	 * on-line. We don't want Linux scanning before we are ready.
1521 	 *
1522 	 */
1523 	for (wait_switch = jiffies + (ha->loop_reset_delay * HZ);
1524 	    time_before(jiffies,wait_switch) &&
1525 	     !(ha->device_flags & (DFLG_NO_CABLE | DFLG_FABRIC_DEVICES))
1526 	     && (ha->device_flags & SWITCH_FOUND) ;) {
1527 
1528 		qla2x00_check_fabric_devices(ha);
1529 
1530 		msleep(10);
1531 	}
1532 
1533 	pci_set_drvdata(pdev, ha);
1534 	ha->flags.init_done = 1;
1535 	num_hosts++;
1536 
1537 	ret = scsi_add_host(host, &pdev->dev);
1538 	if (ret)
1539 		goto probe_failed;
1540 
1541 	qla2x00_alloc_sysfs_attr(ha);
1542 
1543 	qla2x00_init_host_attr(ha);
1544 
1545 	qla_printk(KERN_INFO, ha, "\n"
1546 	    " QLogic Fibre Channel HBA Driver: %s\n"
1547 	    "  QLogic %s - %s\n"
1548 	    "  %s: %s @ %s hdma%c, host#=%ld, fw=%s\n", qla2x00_version_str,
1549 	    ha->model_number, ha->model_desc ? ha->model_desc: "",
1550 	    ha->brd_info->isp_name, ha->isp_ops.pci_info_str(ha, pci_info),
1551 	    pci_name(pdev), ha->flags.enable_64bit_addressing ? '+': '-',
1552 	    ha->host_no, ha->isp_ops.fw_version_str(ha, fw_str));
1553 
1554 	/* Go with fc_rport registration. */
1555 	list_for_each_entry(fcport, &ha->fcports, list)
1556 		qla2x00_reg_remote_port(ha, fcport);
1557 
1558 	return 0;
1559 
1560 probe_failed:
1561 	fc_remove_host(ha->host);
1562 
1563 	qla2x00_free_device(ha);
1564 
1565 	scsi_host_put(host);
1566 
1567 probe_disable_device:
1568 	pci_disable_device(pdev);
1569 
1570 probe_out:
1571 	return ret;
1572 }
1573 EXPORT_SYMBOL_GPL(qla2x00_probe_one);
1574 
1575 void qla2x00_remove_one(struct pci_dev *pdev)
1576 {
1577 	scsi_qla_host_t *ha;
1578 
1579 	ha = pci_get_drvdata(pdev);
1580 
1581 	qla2x00_free_sysfs_attr(ha);
1582 
1583 	fc_remove_host(ha->host);
1584 
1585 	scsi_remove_host(ha->host);
1586 
1587 	qla2x00_free_device(ha);
1588 
1589 	scsi_host_put(ha->host);
1590 
1591 	pci_set_drvdata(pdev, NULL);
1592 }
1593 EXPORT_SYMBOL_GPL(qla2x00_remove_one);
1594 
1595 static void
1596 qla2x00_free_device(scsi_qla_host_t *ha)
1597 {
1598 	int ret;
1599 
1600 	/* Abort any outstanding IO descriptors. */
1601 	if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
1602 		qla2x00_cancel_io_descriptors(ha);
1603 
1604 	/* turn-off interrupts on the card */
1605 	if (ha->interrupts_on)
1606 		ha->isp_ops.disable_intrs(ha);
1607 
1608 	/* Disable timer */
1609 	if (ha->timer_active)
1610 		qla2x00_stop_timer(ha);
1611 
1612 	/* Kill the kernel thread for this host */
1613 	if (ha->dpc_pid >= 0) {
1614 		ha->dpc_should_die = 1;
1615 		wmb();
1616 		ret = kill_proc(ha->dpc_pid, SIGHUP, 1);
1617 		if (ret) {
1618 			qla_printk(KERN_ERR, ha,
1619 			    "Unable to signal DPC thread -- (%d)\n", ret);
1620 
1621 			/* TODO: SOMETHING MORE??? */
1622 		} else {
1623 			wait_for_completion(&ha->dpc_exited);
1624 		}
1625 	}
1626 
1627 	qla2x00_mem_free(ha);
1628 
1629 
1630 	ha->flags.online = 0;
1631 
1632 	/* Detach interrupts */
1633 	if (ha->pdev->irq)
1634 		free_irq(ha->pdev->irq, ha);
1635 
1636 	/* release io space registers  */
1637 	if (ha->iobase)
1638 		iounmap(ha->iobase);
1639 	pci_release_regions(ha->pdev);
1640 
1641 	pci_disable_device(ha->pdev);
1642 }
1643 
1644 /*
1645  * qla2x00_mark_device_lost Updates fcport state when device goes offline.
1646  *
1647  * Input: ha = adapter block pointer.  fcport = port structure pointer.
1648  *
1649  * Return: None.
1650  *
1651  * Context:
1652  */
1653 void qla2x00_mark_device_lost(scsi_qla_host_t *ha, fc_port_t *fcport,
1654     int do_login)
1655 {
1656 	if (atomic_read(&fcport->state) == FCS_ONLINE && fcport->rport)
1657 		fc_remote_port_block(fcport->rport);
1658 	/*
1659 	 * We may need to retry the login, so don't change the state of the
1660 	 * port but do the retries.
1661 	 */
1662 	if (atomic_read(&fcport->state) != FCS_DEVICE_DEAD)
1663 		atomic_set(&fcport->state, FCS_DEVICE_LOST);
1664 
1665 	if (!do_login)
1666 		return;
1667 
1668 	if (fcport->login_retry == 0) {
1669 		fcport->login_retry = ha->login_retry_count;
1670 		set_bit(RELOGIN_NEEDED, &ha->dpc_flags);
1671 
1672 		DEBUG(printk("scsi(%ld): Port login retry: "
1673 		    "%02x%02x%02x%02x%02x%02x%02x%02x, "
1674 		    "id = 0x%04x retry cnt=%d\n",
1675 		    ha->host_no,
1676 		    fcport->port_name[0],
1677 		    fcport->port_name[1],
1678 		    fcport->port_name[2],
1679 		    fcport->port_name[3],
1680 		    fcport->port_name[4],
1681 		    fcport->port_name[5],
1682 		    fcport->port_name[6],
1683 		    fcport->port_name[7],
1684 		    fcport->loop_id,
1685 		    fcport->login_retry));
1686 	}
1687 }
1688 
1689 /*
1690  * qla2x00_mark_all_devices_lost
1691  *	Updates fcport state when device goes offline.
1692  *
1693  * Input:
1694  *	ha = adapter block pointer.
1695  *	fcport = port structure pointer.
1696  *
1697  * Return:
1698  *	None.
1699  *
1700  * Context:
1701  */
1702 void
1703 qla2x00_mark_all_devices_lost(scsi_qla_host_t *ha)
1704 {
1705 	fc_port_t *fcport;
1706 
1707 	list_for_each_entry(fcport, &ha->fcports, list) {
1708 		if (fcport->port_type != FCT_TARGET)
1709 			continue;
1710 
1711 		/*
1712 		 * No point in marking the device as lost, if the device is
1713 		 * already DEAD.
1714 		 */
1715 		if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD)
1716 			continue;
1717 		if (atomic_read(&fcport->state) == FCS_ONLINE && fcport->rport)
1718 			fc_remote_port_block(fcport->rport);
1719 		atomic_set(&fcport->state, FCS_DEVICE_LOST);
1720 	}
1721 }
1722 
1723 /*
1724 * qla2x00_mem_alloc
1725 *      Allocates adapter memory.
1726 *
1727 * Returns:
1728 *      0  = success.
1729 *      1  = failure.
1730 */
1731 static uint8_t
1732 qla2x00_mem_alloc(scsi_qla_host_t *ha)
1733 {
1734 	char	name[16];
1735 	uint8_t   status = 1;
1736 	int	retry= 10;
1737 
1738 	do {
1739 		/*
1740 		 * This will loop only once if everything goes well, else some
1741 		 * number of retries will be performed to get around a kernel
1742 		 * bug where available mem is not allocated until after a
1743 		 * little delay and a retry.
1744 		 */
1745 		ha->request_ring = dma_alloc_coherent(&ha->pdev->dev,
1746 		    (ha->request_q_length + 1) * sizeof(request_t),
1747 		    &ha->request_dma, GFP_KERNEL);
1748 		if (ha->request_ring == NULL) {
1749 			qla_printk(KERN_WARNING, ha,
1750 			    "Memory Allocation failed - request_ring\n");
1751 
1752 			qla2x00_mem_free(ha);
1753 			msleep(100);
1754 
1755 			continue;
1756 		}
1757 
1758 		ha->response_ring = dma_alloc_coherent(&ha->pdev->dev,
1759 		    (ha->response_q_length + 1) * sizeof(response_t),
1760 		    &ha->response_dma, GFP_KERNEL);
1761 		if (ha->response_ring == NULL) {
1762 			qla_printk(KERN_WARNING, ha,
1763 			    "Memory Allocation failed - response_ring\n");
1764 
1765 			qla2x00_mem_free(ha);
1766 			msleep(100);
1767 
1768 			continue;
1769 		}
1770 
1771 		ha->gid_list = dma_alloc_coherent(&ha->pdev->dev, GID_LIST_SIZE,
1772 		    &ha->gid_list_dma, GFP_KERNEL);
1773 		if (ha->gid_list == NULL) {
1774 			qla_printk(KERN_WARNING, ha,
1775 			    "Memory Allocation failed - gid_list\n");
1776 
1777 			qla2x00_mem_free(ha);
1778 			msleep(100);
1779 
1780 			continue;
1781 		}
1782 
1783 		ha->rlc_rsp = dma_alloc_coherent(&ha->pdev->dev,
1784 		    sizeof(rpt_lun_cmd_rsp_t), &ha->rlc_rsp_dma, GFP_KERNEL);
1785 		if (ha->rlc_rsp == NULL) {
1786 			qla_printk(KERN_WARNING, ha,
1787 				"Memory Allocation failed - rlc");
1788 
1789 			qla2x00_mem_free(ha);
1790 			msleep(100);
1791 
1792 			continue;
1793 		}
1794 
1795 		snprintf(name, sizeof(name), "qla2xxx_%ld", ha->host_no);
1796 		ha->s_dma_pool = dma_pool_create(name, &ha->pdev->dev,
1797 		    DMA_POOL_SIZE, 8, 0);
1798 		if (ha->s_dma_pool == NULL) {
1799 			qla_printk(KERN_WARNING, ha,
1800 			    "Memory Allocation failed - s_dma_pool\n");
1801 
1802 			qla2x00_mem_free(ha);
1803 			msleep(100);
1804 
1805 			continue;
1806 		}
1807 
1808 		/* get consistent memory allocated for init control block */
1809 		ha->init_cb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
1810 		    &ha->init_cb_dma);
1811 		if (ha->init_cb == NULL) {
1812 			qla_printk(KERN_WARNING, ha,
1813 			    "Memory Allocation failed - init_cb\n");
1814 
1815 			qla2x00_mem_free(ha);
1816 			msleep(100);
1817 
1818 			continue;
1819 		}
1820 		memset(ha->init_cb, 0, ha->init_cb_size);
1821 
1822 		/* Get consistent memory allocated for Get Port Database cmd */
1823 		ha->iodesc_pd = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
1824 		    &ha->iodesc_pd_dma);
1825 		if (ha->iodesc_pd == NULL) {
1826 			/* error */
1827 			qla_printk(KERN_WARNING, ha,
1828 			    "Memory Allocation failed - iodesc_pd\n");
1829 
1830 			qla2x00_mem_free(ha);
1831 			msleep(100);
1832 
1833 			continue;
1834 		}
1835 		memset(ha->iodesc_pd, 0, PORT_DATABASE_SIZE);
1836 
1837 		/* Allocate ioctl related memory. */
1838 		if (qla2x00_alloc_ioctl_mem(ha)) {
1839 			qla_printk(KERN_WARNING, ha,
1840 			    "Memory Allocation failed - ioctl_mem\n");
1841 
1842 			qla2x00_mem_free(ha);
1843 			msleep(100);
1844 
1845 			continue;
1846 		}
1847 
1848 		if (qla2x00_allocate_sp_pool(ha)) {
1849 			qla_printk(KERN_WARNING, ha,
1850 			    "Memory Allocation failed - "
1851 			    "qla2x00_allocate_sp_pool()\n");
1852 
1853 			qla2x00_mem_free(ha);
1854 			msleep(100);
1855 
1856 			continue;
1857 		}
1858 
1859 		/* Allocate memory for SNS commands */
1860 		if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
1861 			/* Get consistent memory allocated for SNS commands */
1862 			ha->sns_cmd = dma_alloc_coherent(&ha->pdev->dev,
1863 			    sizeof(struct sns_cmd_pkt), &ha->sns_cmd_dma,
1864 			    GFP_KERNEL);
1865 			if (ha->sns_cmd == NULL) {
1866 				/* error */
1867 				qla_printk(KERN_WARNING, ha,
1868 				    "Memory Allocation failed - sns_cmd\n");
1869 
1870 				qla2x00_mem_free(ha);
1871 				msleep(100);
1872 
1873 				continue;
1874 			}
1875 			memset(ha->sns_cmd, 0, sizeof(struct sns_cmd_pkt));
1876 		} else {
1877 			/* Get consistent memory allocated for MS IOCB */
1878 			ha->ms_iocb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
1879 			    &ha->ms_iocb_dma);
1880 			if (ha->ms_iocb == NULL) {
1881 				/* error */
1882 				qla_printk(KERN_WARNING, ha,
1883 				    "Memory Allocation failed - ms_iocb\n");
1884 
1885 				qla2x00_mem_free(ha);
1886 				msleep(100);
1887 
1888 				continue;
1889 			}
1890 			memset(ha->ms_iocb, 0, sizeof(ms_iocb_entry_t));
1891 
1892 			/*
1893 			 * Get consistent memory allocated for CT SNS
1894 			 * commands
1895 			 */
1896 			ha->ct_sns = dma_alloc_coherent(&ha->pdev->dev,
1897 			    sizeof(struct ct_sns_pkt), &ha->ct_sns_dma,
1898 			    GFP_KERNEL);
1899 			if (ha->ct_sns == NULL) {
1900 				/* error */
1901 				qla_printk(KERN_WARNING, ha,
1902 				    "Memory Allocation failed - ct_sns\n");
1903 
1904 				qla2x00_mem_free(ha);
1905 				msleep(100);
1906 
1907 				continue;
1908 			}
1909 			memset(ha->ct_sns, 0, sizeof(struct ct_sns_pkt));
1910 		}
1911 
1912 		/* Done all allocations without any error. */
1913 		status = 0;
1914 
1915 	} while (retry-- && status != 0);
1916 
1917 	if (status) {
1918 		printk(KERN_WARNING
1919 			"%s(): **** FAILED ****\n", __func__);
1920 	}
1921 
1922 	return(status);
1923 }
1924 
1925 /*
1926 * qla2x00_mem_free
1927 *      Frees all adapter allocated memory.
1928 *
1929 * Input:
1930 *      ha = adapter block pointer.
1931 */
1932 static void
1933 qla2x00_mem_free(scsi_qla_host_t *ha)
1934 {
1935 	struct list_head	*fcpl, *fcptemp;
1936 	fc_port_t	*fcport;
1937 	unsigned long	wtime;/* max wait time if mbx cmd is busy. */
1938 
1939 	if (ha == NULL) {
1940 		/* error */
1941 		DEBUG2(printk("%s(): ERROR invalid ha pointer.\n", __func__));
1942 		return;
1943 	}
1944 
1945 	/* Make sure all other threads are stopped. */
1946 	wtime = 60 * HZ;
1947 	while (ha->dpc_wait && wtime) {
1948 		set_current_state(TASK_INTERRUPTIBLE);
1949 		wtime = schedule_timeout(wtime);
1950 	}
1951 
1952 	/* free ioctl memory */
1953 	qla2x00_free_ioctl_mem(ha);
1954 
1955 	/* free sp pool */
1956 	qla2x00_free_sp_pool(ha);
1957 
1958 	if (ha->sns_cmd)
1959 		dma_free_coherent(&ha->pdev->dev, sizeof(struct sns_cmd_pkt),
1960 		    ha->sns_cmd, ha->sns_cmd_dma);
1961 
1962 	if (ha->ct_sns)
1963 		dma_free_coherent(&ha->pdev->dev, sizeof(struct ct_sns_pkt),
1964 		    ha->ct_sns, ha->ct_sns_dma);
1965 
1966 	if (ha->ms_iocb)
1967 		dma_pool_free(ha->s_dma_pool, ha->ms_iocb, ha->ms_iocb_dma);
1968 
1969 	if (ha->iodesc_pd)
1970 		dma_pool_free(ha->s_dma_pool, ha->iodesc_pd, ha->iodesc_pd_dma);
1971 
1972 	if (ha->init_cb)
1973 		dma_pool_free(ha->s_dma_pool, ha->init_cb, ha->init_cb_dma);
1974 
1975 	if (ha->s_dma_pool)
1976 		dma_pool_destroy(ha->s_dma_pool);
1977 
1978 	if (ha->rlc_rsp)
1979 		dma_free_coherent(&ha->pdev->dev,
1980 		    sizeof(rpt_lun_cmd_rsp_t), ha->rlc_rsp,
1981 		    ha->rlc_rsp_dma);
1982 
1983 	if (ha->gid_list)
1984 		dma_free_coherent(&ha->pdev->dev, GID_LIST_SIZE, ha->gid_list,
1985 		    ha->gid_list_dma);
1986 
1987 	if (ha->response_ring)
1988 		dma_free_coherent(&ha->pdev->dev,
1989 		    (ha->response_q_length + 1) * sizeof(response_t),
1990 		    ha->response_ring, ha->response_dma);
1991 
1992 	if (ha->request_ring)
1993 		dma_free_coherent(&ha->pdev->dev,
1994 		    (ha->request_q_length + 1) * sizeof(request_t),
1995 		    ha->request_ring, ha->request_dma);
1996 
1997 	ha->sns_cmd = NULL;
1998 	ha->sns_cmd_dma = 0;
1999 	ha->ct_sns = NULL;
2000 	ha->ct_sns_dma = 0;
2001 	ha->ms_iocb = NULL;
2002 	ha->ms_iocb_dma = 0;
2003 	ha->iodesc_pd = NULL;
2004 	ha->iodesc_pd_dma = 0;
2005 	ha->init_cb = NULL;
2006 	ha->init_cb_dma = 0;
2007 
2008 	ha->s_dma_pool = NULL;
2009 
2010 	ha->rlc_rsp = NULL;
2011 	ha->rlc_rsp_dma = 0;
2012 	ha->gid_list = NULL;
2013 	ha->gid_list_dma = 0;
2014 
2015 	ha->response_ring = NULL;
2016 	ha->response_dma = 0;
2017 	ha->request_ring = NULL;
2018 	ha->request_dma = 0;
2019 
2020 	list_for_each_safe(fcpl, fcptemp, &ha->fcports) {
2021 		fcport = list_entry(fcpl, fc_port_t, list);
2022 
2023 		/* fc ports */
2024 		list_del_init(&fcport->list);
2025 		kfree(fcport);
2026 	}
2027 	INIT_LIST_HEAD(&ha->fcports);
2028 
2029 	if (ha->fw_dump)
2030 		free_pages((unsigned long)ha->fw_dump, ha->fw_dump_order);
2031 
2032 	vfree(ha->fw_dump24);
2033 
2034 	vfree(ha->fw_dump_buffer);
2035 
2036 	ha->fw_dump = NULL;
2037 	ha->fw_dump24 = NULL;
2038 	ha->fw_dumped = 0;
2039 	ha->fw_dump_reading = 0;
2040 	ha->fw_dump_buffer = NULL;
2041 }
2042 
2043 /*
2044  * qla2x00_allocate_sp_pool
2045  * 	 This routine is called during initialization to allocate
2046  *  	 memory for local srb_t.
2047  *
2048  * Input:
2049  *	 ha   = adapter block pointer.
2050  *
2051  * Context:
2052  *      Kernel context.
2053  *
2054  * Note: Sets the ref_count for non Null sp to one.
2055  */
2056 static int
2057 qla2x00_allocate_sp_pool(scsi_qla_host_t *ha)
2058 {
2059 	int      rval;
2060 
2061 	rval = QLA_SUCCESS;
2062 	ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
2063 	    mempool_free_slab, srb_cachep);
2064 	if (ha->srb_mempool == NULL) {
2065 		qla_printk(KERN_INFO, ha, "Unable to allocate SRB mempool.\n");
2066 		rval = QLA_FUNCTION_FAILED;
2067 	}
2068 	return (rval);
2069 }
2070 
2071 /*
2072  *  This routine frees all adapter allocated memory.
2073  *
2074  */
2075 static void
2076 qla2x00_free_sp_pool( scsi_qla_host_t *ha)
2077 {
2078 	if (ha->srb_mempool) {
2079 		mempool_destroy(ha->srb_mempool);
2080 		ha->srb_mempool = NULL;
2081 	}
2082 }
2083 
2084 /**************************************************************************
2085 * qla2x00_do_dpc
2086 *   This kernel thread is a task that is schedule by the interrupt handler
2087 *   to perform the background processing for interrupts.
2088 *
2089 * Notes:
2090 * This task always run in the context of a kernel thread.  It
2091 * is kick-off by the driver's detect code and starts up
2092 * up one per adapter. It immediately goes to sleep and waits for
2093 * some fibre event.  When either the interrupt handler or
2094 * the timer routine detects a event it will one of the task
2095 * bits then wake us up.
2096 **************************************************************************/
2097 static int
2098 qla2x00_do_dpc(void *data)
2099 {
2100 	DECLARE_MUTEX_LOCKED(sem);
2101 	scsi_qla_host_t *ha;
2102 	fc_port_t	*fcport;
2103 	uint8_t		status;
2104 	uint16_t	next_loopid;
2105 
2106 	ha = (scsi_qla_host_t *)data;
2107 
2108 	lock_kernel();
2109 
2110 	daemonize("%s_dpc", ha->host_str);
2111 	allow_signal(SIGHUP);
2112 
2113 	ha->dpc_wait = &sem;
2114 
2115 	set_user_nice(current, -20);
2116 
2117 	unlock_kernel();
2118 
2119 	complete(&ha->dpc_inited);
2120 
2121 	while (1) {
2122 		DEBUG3(printk("qla2x00: DPC handler sleeping\n"));
2123 
2124 		if (down_interruptible(&sem))
2125 			break;
2126 
2127 		if (ha->dpc_should_die)
2128 			break;
2129 
2130 		DEBUG3(printk("qla2x00: DPC handler waking up\n"));
2131 
2132 		/* Initialization not yet finished. Don't do anything yet. */
2133 		if (!ha->flags.init_done || ha->dpc_active)
2134 			continue;
2135 
2136 		DEBUG3(printk("scsi(%ld): DPC handler\n", ha->host_no));
2137 
2138 		ha->dpc_active = 1;
2139 
2140 		if (ha->flags.mbox_busy) {
2141 			ha->dpc_active = 0;
2142 			continue;
2143 		}
2144 
2145 		if (test_and_clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
2146 
2147 			DEBUG(printk("scsi(%ld): dpc: sched "
2148 			    "qla2x00_abort_isp ha = %p\n",
2149 			    ha->host_no, ha));
2150 			if (!(test_and_set_bit(ABORT_ISP_ACTIVE,
2151 			    &ha->dpc_flags))) {
2152 
2153 				if (qla2x00_abort_isp(ha)) {
2154 					/* failed. retry later */
2155 					set_bit(ISP_ABORT_NEEDED,
2156 					    &ha->dpc_flags);
2157 				}
2158 				clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
2159 			}
2160 			DEBUG(printk("scsi(%ld): dpc: qla2x00_abort_isp end\n",
2161 			    ha->host_no));
2162 		}
2163 
2164 		if (test_and_clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags) &&
2165 		    (!(test_and_set_bit(RESET_ACTIVE, &ha->dpc_flags)))) {
2166 
2167 			DEBUG(printk("scsi(%ld): qla2x00_reset_marker()\n",
2168 			    ha->host_no));
2169 
2170 			qla2x00_rst_aen(ha);
2171 			clear_bit(RESET_ACTIVE, &ha->dpc_flags);
2172 		}
2173 
2174 		/* Retry each device up to login retry count */
2175 		if ((test_and_clear_bit(RELOGIN_NEEDED, &ha->dpc_flags)) &&
2176 		    !test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags) &&
2177 		    atomic_read(&ha->loop_state) != LOOP_DOWN) {
2178 
2179 			DEBUG(printk("scsi(%ld): qla2x00_port_login()\n",
2180 			    ha->host_no));
2181 
2182 			next_loopid = 0;
2183 			list_for_each_entry(fcport, &ha->fcports, list) {
2184 				if (fcport->port_type != FCT_TARGET)
2185 					continue;
2186 
2187 				/*
2188 				 * If the port is not ONLINE then try to login
2189 				 * to it if we haven't run out of retries.
2190 				 */
2191 				if (atomic_read(&fcport->state) != FCS_ONLINE &&
2192 				    fcport->login_retry) {
2193 
2194 					fcport->login_retry--;
2195 					if (fcport->flags & FCF_FABRIC_DEVICE) {
2196 						if (fcport->flags &
2197 						    FCF_TAPE_PRESENT)
2198 							ha->isp_ops.fabric_logout(
2199 							    ha, fcport->loop_id,
2200 							    fcport->d_id.b.domain,
2201 							    fcport->d_id.b.area,
2202 							    fcport->d_id.b.al_pa);
2203 						status = qla2x00_fabric_login(
2204 						    ha, fcport, &next_loopid);
2205 					} else
2206 						status =
2207 						    qla2x00_local_device_login(
2208 							ha, fcport->loop_id);
2209 
2210 					if (status == QLA_SUCCESS) {
2211 						fcport->old_loop_id = fcport->loop_id;
2212 
2213 						DEBUG(printk("scsi(%ld): port login OK: logged in ID 0x%x\n",
2214 						    ha->host_no, fcport->loop_id));
2215 
2216 						fcport->port_login_retry_count =
2217 						    ha->port_down_retry_count * PORT_RETRY_TIME;
2218 						atomic_set(&fcport->state, FCS_ONLINE);
2219 						atomic_set(&fcport->port_down_timer,
2220 						    ha->port_down_retry_count * PORT_RETRY_TIME);
2221 
2222 						fcport->login_retry = 0;
2223 					} else if (status == 1) {
2224 						set_bit(RELOGIN_NEEDED, &ha->dpc_flags);
2225 						/* retry the login again */
2226 						DEBUG(printk("scsi(%ld): Retrying %d login again loop_id 0x%x\n",
2227 						    ha->host_no,
2228 						    fcport->login_retry, fcport->loop_id));
2229 					} else {
2230 						fcport->login_retry = 0;
2231 					}
2232 				}
2233 				if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2234 					break;
2235 			}
2236 			DEBUG(printk("scsi(%ld): qla2x00_port_login - end\n",
2237 			    ha->host_no));
2238 		}
2239 
2240 		if ((test_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags)) &&
2241 		    atomic_read(&ha->loop_state) != LOOP_DOWN) {
2242 
2243 			clear_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags);
2244 			DEBUG(printk("scsi(%ld): qla2x00_login_retry()\n",
2245 			    ha->host_no));
2246 
2247 			set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
2248 
2249 			DEBUG(printk("scsi(%ld): qla2x00_login_retry - end\n",
2250 			    ha->host_no));
2251 		}
2252 
2253 		if (test_and_clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
2254 
2255 			DEBUG(printk("scsi(%ld): qla2x00_loop_resync()\n",
2256 			    ha->host_no));
2257 
2258 			if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE,
2259 			    &ha->dpc_flags))) {
2260 
2261 				qla2x00_loop_resync(ha);
2262 
2263 				clear_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags);
2264 			}
2265 
2266 			DEBUG(printk("scsi(%ld): qla2x00_loop_resync - end\n",
2267 			    ha->host_no));
2268 		}
2269 
2270 		if (test_and_clear_bit(FCPORT_RESCAN_NEEDED, &ha->dpc_flags)) {
2271 
2272 			DEBUG(printk("scsi(%ld): Rescan flagged fcports...\n",
2273 			    ha->host_no));
2274 
2275 			qla2x00_rescan_fcports(ha);
2276 
2277 			DEBUG(printk("scsi(%ld): Rescan flagged fcports..."
2278 			    "end.\n",
2279 			    ha->host_no));
2280 		}
2281 
2282 		if (!ha->interrupts_on)
2283 			ha->isp_ops.enable_intrs(ha);
2284 
2285 		ha->dpc_active = 0;
2286 	} /* End of while(1) */
2287 
2288 	DEBUG(printk("scsi(%ld): DPC handler exiting\n", ha->host_no));
2289 
2290 	/*
2291 	 * Make sure that nobody tries to wake us up again.
2292 	 */
2293 	ha->dpc_wait = NULL;
2294 	ha->dpc_active = 0;
2295 
2296 	complete_and_exit(&ha->dpc_exited, 0);
2297 }
2298 
2299 /*
2300 *  qla2x00_rst_aen
2301 *      Processes asynchronous reset.
2302 *
2303 * Input:
2304 *      ha  = adapter block pointer.
2305 */
2306 static void
2307 qla2x00_rst_aen(scsi_qla_host_t *ha)
2308 {
2309 	if (ha->flags.online && !ha->flags.reset_active &&
2310 	    !atomic_read(&ha->loop_down_timer) &&
2311 	    !(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags))) {
2312 		do {
2313 			clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
2314 
2315 			/*
2316 			 * Issue marker command only when we are going to start
2317 			 * the I/O.
2318 			 */
2319 			ha->marker_needed = 1;
2320 		} while (!atomic_read(&ha->loop_down_timer) &&
2321 		    (test_bit(RESET_MARKER_NEEDED, &ha->dpc_flags)));
2322 	}
2323 }
2324 
2325 static void
2326 qla2x00_sp_free_dma(scsi_qla_host_t *ha, srb_t *sp)
2327 {
2328 	struct scsi_cmnd *cmd = sp->cmd;
2329 
2330 	if (sp->flags & SRB_DMA_VALID) {
2331 		if (cmd->use_sg) {
2332 			dma_unmap_sg(&ha->pdev->dev, cmd->request_buffer,
2333 			    cmd->use_sg, cmd->sc_data_direction);
2334 		} else if (cmd->request_bufflen) {
2335 			dma_unmap_single(&ha->pdev->dev, sp->dma_handle,
2336 			    cmd->request_bufflen, cmd->sc_data_direction);
2337 		}
2338 		sp->flags &= ~SRB_DMA_VALID;
2339 	}
2340 	CMD_SP(cmd) = NULL;
2341 }
2342 
2343 void
2344 qla2x00_sp_compl(scsi_qla_host_t *ha, srb_t *sp)
2345 {
2346 	struct scsi_cmnd *cmd = sp->cmd;
2347 
2348 	qla2x00_sp_free_dma(ha, sp);
2349 
2350 	mempool_free(sp, ha->srb_mempool);
2351 
2352 	cmd->scsi_done(cmd);
2353 }
2354 
2355 /**************************************************************************
2356 *   qla2x00_timer
2357 *
2358 * Description:
2359 *   One second timer
2360 *
2361 * Context: Interrupt
2362 ***************************************************************************/
2363 static void
2364 qla2x00_timer(scsi_qla_host_t *ha)
2365 {
2366 	unsigned long	cpu_flags = 0;
2367 	fc_port_t	*fcport;
2368 	int		start_dpc = 0;
2369 	int		index;
2370 	srb_t		*sp;
2371 	int		t;
2372 
2373 	/*
2374 	 * Ports - Port down timer.
2375 	 *
2376 	 * Whenever, a port is in the LOST state we start decrementing its port
2377 	 * down timer every second until it reaches zero. Once  it reaches zero
2378 	 * the port it marked DEAD.
2379 	 */
2380 	t = 0;
2381 	list_for_each_entry(fcport, &ha->fcports, list) {
2382 		if (fcport->port_type != FCT_TARGET)
2383 			continue;
2384 
2385 		if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
2386 
2387 			if (atomic_read(&fcport->port_down_timer) == 0)
2388 				continue;
2389 
2390 			if (atomic_dec_and_test(&fcport->port_down_timer) != 0)
2391 				atomic_set(&fcport->state, FCS_DEVICE_DEAD);
2392 
2393 			DEBUG(printk("scsi(%ld): fcport-%d - port retry count: "
2394 			    "%d remaining\n",
2395 			    ha->host_no,
2396 			    t, atomic_read(&fcport->port_down_timer)));
2397 		}
2398 		t++;
2399 	} /* End of for fcport  */
2400 
2401 
2402 	/* Loop down handler. */
2403 	if (atomic_read(&ha->loop_down_timer) > 0 &&
2404 	    !(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags)) && ha->flags.online) {
2405 
2406 		if (atomic_read(&ha->loop_down_timer) ==
2407 		    ha->loop_down_abort_time) {
2408 
2409 			DEBUG(printk("scsi(%ld): Loop Down - aborting the "
2410 			    "queues before time expire\n",
2411 			    ha->host_no));
2412 
2413 			if (!IS_QLA2100(ha) && ha->link_down_timeout)
2414 				atomic_set(&ha->loop_state, LOOP_DEAD);
2415 
2416 			/* Schedule an ISP abort to return any tape commands. */
2417 			spin_lock_irqsave(&ha->hardware_lock, cpu_flags);
2418 			for (index = 1; index < MAX_OUTSTANDING_COMMANDS;
2419 			    index++) {
2420 				fc_port_t *sfcp;
2421 
2422 				sp = ha->outstanding_cmds[index];
2423 				if (!sp)
2424 					continue;
2425 				sfcp = sp->fcport;
2426 				if (!(sfcp->flags & FCF_TAPE_PRESENT))
2427 					continue;
2428 
2429 				set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
2430 				break;
2431 			}
2432 			spin_unlock_irqrestore(&ha->hardware_lock, cpu_flags);
2433 
2434 			set_bit(ABORT_QUEUES_NEEDED, &ha->dpc_flags);
2435 			start_dpc++;
2436 		}
2437 
2438 		/* if the loop has been down for 4 minutes, reinit adapter */
2439 		if (atomic_dec_and_test(&ha->loop_down_timer) != 0) {
2440 			DEBUG(printk("scsi(%ld): Loop down exceed 4 mins - "
2441 			    "restarting queues.\n",
2442 			    ha->host_no));
2443 
2444 			set_bit(RESTART_QUEUES_NEEDED, &ha->dpc_flags);
2445 			start_dpc++;
2446 
2447 			if (!(ha->device_flags & DFLG_NO_CABLE)) {
2448 				DEBUG(printk("scsi(%ld): Loop down - "
2449 				    "aborting ISP.\n",
2450 				    ha->host_no));
2451 				qla_printk(KERN_WARNING, ha,
2452 				    "Loop down - aborting ISP.\n");
2453 
2454 				set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
2455 			}
2456 		}
2457 		DEBUG3(printk("scsi(%ld): Loop Down - seconds remaining %d\n",
2458 		    ha->host_no,
2459 		    atomic_read(&ha->loop_down_timer)));
2460 	}
2461 
2462 	/* Schedule the DPC routine if needed */
2463 	if ((test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags) ||
2464 	    test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags) ||
2465 	    start_dpc ||
2466 	    test_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags) ||
2467 	    test_bit(RESET_MARKER_NEEDED, &ha->dpc_flags) ||
2468 	    test_bit(RELOGIN_NEEDED, &ha->dpc_flags)) &&
2469 	    ha->dpc_wait && !ha->dpc_active) {
2470 
2471 		up(ha->dpc_wait);
2472 	}
2473 
2474 	qla2x00_restart_timer(ha, WATCH_INTERVAL);
2475 }
2476 
2477 /* XXX(hch): crude hack to emulate a down_timeout() */
2478 int
2479 qla2x00_down_timeout(struct semaphore *sema, unsigned long timeout)
2480 {
2481 	const unsigned int step = HZ/10;
2482 
2483 	do {
2484 		if (!down_trylock(sema))
2485 			return 0;
2486 		set_current_state(TASK_INTERRUPTIBLE);
2487 		if (schedule_timeout(step))
2488 			break;
2489 	} while ((timeout -= step) > 0);
2490 
2491 	return -ETIMEDOUT;
2492 }
2493 
2494 static struct qla_board_info qla_board_tbl[] = {
2495 	{
2496 		.drv_name	= "qla2400",
2497 		.isp_name	= "ISP2422",
2498 		.fw_fname	= "ql2400_fw.bin",
2499 		.sht		= &qla24xx_driver_template,
2500 	},
2501 	{
2502 		.drv_name	= "qla2400",
2503 		.isp_name	= "ISP2432",
2504 		.fw_fname	= "ql2400_fw.bin",
2505 		.sht		= &qla24xx_driver_template,
2506 	},
2507 };
2508 
2509 static struct pci_device_id qla2xxx_pci_tbl[] = {
2510 	{
2511 		.vendor		= PCI_VENDOR_ID_QLOGIC,
2512 		.device		= PCI_DEVICE_ID_QLOGIC_ISP2422,
2513 		.subvendor	= PCI_ANY_ID,
2514 		.subdevice	= PCI_ANY_ID,
2515 		.driver_data	= (unsigned long)&qla_board_tbl[0],
2516 	},
2517 	{
2518 		.vendor		= PCI_VENDOR_ID_QLOGIC,
2519 		.device		= PCI_DEVICE_ID_QLOGIC_ISP2432,
2520 		.subvendor	= PCI_ANY_ID,
2521 		.subdevice	= PCI_ANY_ID,
2522 		.driver_data	= (unsigned long)&qla_board_tbl[1],
2523 	},
2524 	{0, 0},
2525 };
2526 MODULE_DEVICE_TABLE(pci, qla2xxx_pci_tbl);
2527 
2528 static int __devinit
2529 qla2xxx_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
2530 {
2531 	return qla2x00_probe_one(pdev,
2532 	    (struct qla_board_info *)id->driver_data);
2533 }
2534 
2535 static void __devexit
2536 qla2xxx_remove_one(struct pci_dev *pdev)
2537 {
2538 	qla2x00_remove_one(pdev);
2539 }
2540 
2541 static struct pci_driver qla2xxx_pci_driver = {
2542 	.name		= "qla2xxx",
2543 	.id_table	= qla2xxx_pci_tbl,
2544 	.probe		= qla2xxx_probe_one,
2545 	.remove		= __devexit_p(qla2xxx_remove_one),
2546 };
2547 
2548 /**
2549  * qla2x00_module_init - Module initialization.
2550  **/
2551 static int __init
2552 qla2x00_module_init(void)
2553 {
2554 	int ret = 0;
2555 
2556 	/* Allocate cache for SRBs. */
2557 	srb_cachep = kmem_cache_create("qla2xxx_srbs", sizeof(srb_t), 0,
2558 	    SLAB_HWCACHE_ALIGN, NULL, NULL);
2559 	if (srb_cachep == NULL) {
2560 		printk(KERN_ERR
2561 		    "qla2xxx: Unable to allocate SRB cache...Failing load!\n");
2562 		return -ENOMEM;
2563 	}
2564 
2565 	/* Derive version string. */
2566 	strcpy(qla2x00_version_str, QLA2XXX_VERSION);
2567 #if DEBUG_QLA2100
2568 	strcat(qla2x00_version_str, "-debug");
2569 #endif
2570 	qla2xxx_transport_template =
2571 	    fc_attach_transport(&qla2xxx_transport_functions);
2572 	if (!qla2xxx_transport_template)
2573 		return -ENODEV;
2574 
2575 	printk(KERN_INFO "QLogic Fibre Channel HBA Driver\n");
2576 	ret = pci_module_init(&qla2xxx_pci_driver);
2577 	if (ret) {
2578 		kmem_cache_destroy(srb_cachep);
2579 		fc_release_transport(qla2xxx_transport_template);
2580 	}
2581 	return ret;
2582 }
2583 
2584 /**
2585  * qla2x00_module_exit - Module cleanup.
2586  **/
2587 static void __exit
2588 qla2x00_module_exit(void)
2589 {
2590 	pci_unregister_driver(&qla2xxx_pci_driver);
2591 	kmem_cache_destroy(srb_cachep);
2592 	fc_release_transport(qla2xxx_transport_template);
2593 }
2594 
2595 module_init(qla2x00_module_init);
2596 module_exit(qla2x00_module_exit);
2597 
2598 MODULE_AUTHOR("QLogic Corporation");
2599 MODULE_DESCRIPTION("QLogic Fibre Channel HBA Driver");
2600 MODULE_LICENSE("GPL");
2601 MODULE_VERSION(QLA2XXX_VERSION);
2602