• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6/drivers/scsi/qla2xxx/
1/*
2 * QLogic Fibre Channel HBA Driver
3 * Copyright (c)  2003-2010 QLogic Corporation
4 *
5 * See LICENSE.qla2xxx for copyright and licensing details.
6 */
7#include "qla_def.h"
8
9#include <linux/moduleparam.h>
10#include <linux/vmalloc.h>
11#include <linux/delay.h>
12#include <linux/kthread.h>
13#include <linux/mutex.h>
14#include <linux/kobject.h>
15#include <linux/slab.h>
16
17#include <scsi/scsi_tcq.h>
18#include <scsi/scsicam.h>
19#include <scsi/scsi_transport.h>
20#include <scsi/scsi_transport_fc.h>
21
22/*
23 * Driver version
24 */
25char qla2x00_version_str[40];
26
27static int apidev_major;
28
29/*
30 * SRB allocation cache
31 */
32static struct kmem_cache *srb_cachep;
33
34/*
35 * CT6 CTX allocation cache
36 */
37static struct kmem_cache *ctx_cachep;
38
39int ql2xlogintimeout = 20;
40module_param(ql2xlogintimeout, int, S_IRUGO|S_IRUSR);
41MODULE_PARM_DESC(ql2xlogintimeout,
42		"Login timeout value in seconds.");
43
44int qlport_down_retry;
45module_param(qlport_down_retry, int, S_IRUGO|S_IRUSR);
46MODULE_PARM_DESC(qlport_down_retry,
47		"Maximum number of command retries to a port that returns "
48		"a PORT-DOWN status.");
49
50int ql2xplogiabsentdevice;
51module_param(ql2xplogiabsentdevice, int, S_IRUGO|S_IWUSR);
52MODULE_PARM_DESC(ql2xplogiabsentdevice,
53		"Option to enable PLOGI to devices that are not present after "
54		"a Fabric scan.  This is needed for several broken switches. "
55		"Default is 0 - no PLOGI. 1 - perfom PLOGI.");
56
57int ql2xloginretrycount = 0;
58module_param(ql2xloginretrycount, int, S_IRUGO|S_IRUSR);
59MODULE_PARM_DESC(ql2xloginretrycount,
60		"Specify an alternate value for the NVRAM login retry count.");
61
62int ql2xallocfwdump = 1;
63module_param(ql2xallocfwdump, int, S_IRUGO|S_IRUSR);
64MODULE_PARM_DESC(ql2xallocfwdump,
65		"Option to enable allocation of memory for a firmware dump "
66		"during HBA initialization.  Memory allocation requirements "
67		"vary by ISP type.  Default is 1 - allocate memory.");
68
69int ql2xextended_error_logging;
70module_param(ql2xextended_error_logging, int, S_IRUGO|S_IWUSR);
71MODULE_PARM_DESC(ql2xextended_error_logging,
72		"Option to enable extended error logging, "
73		"Default is 0 - no logging. 1 - log errors.");
74
75int ql2xshiftctondsd = 6;
76module_param(ql2xshiftctondsd, int, S_IRUGO|S_IRUSR);
77MODULE_PARM_DESC(ql2xshiftctondsd,
78		"Set to control shifting of command type processing "
79		"based on total number of SG elements.");
80
81static void qla2x00_free_device(scsi_qla_host_t *);
82
83int ql2xfdmienable=1;
84module_param(ql2xfdmienable, int, S_IRUGO|S_IRUSR);
85MODULE_PARM_DESC(ql2xfdmienable,
86		"Enables FDMI registrations. "
87		"0 - no FDMI. Default is 1 - perform FDMI.");
88
89#define MAX_Q_DEPTH    32
90static int ql2xmaxqdepth = MAX_Q_DEPTH;
91module_param(ql2xmaxqdepth, int, S_IRUGO|S_IWUSR);
92MODULE_PARM_DESC(ql2xmaxqdepth,
93		"Maximum queue depth to report for target devices.");
94
95/* Do not change the value of this after module load */
96int ql2xenabledif = 1;
97module_param(ql2xenabledif, int, S_IRUGO|S_IWUSR);
98MODULE_PARM_DESC(ql2xenabledif,
99		" Enable T10-CRC-DIF "
100		" Default is 0 - No DIF Support. 1 - Enable it");
101
102int ql2xenablehba_err_chk;
103module_param(ql2xenablehba_err_chk, int, S_IRUGO|S_IWUSR);
104MODULE_PARM_DESC(ql2xenablehba_err_chk,
105		" Enable T10-CRC-DIF Error isolation by HBA"
106		" Default is 0 - Error isolation disabled, 1 - Enable it");
107
108int ql2xiidmaenable=1;
109module_param(ql2xiidmaenable, int, S_IRUGO|S_IRUSR);
110MODULE_PARM_DESC(ql2xiidmaenable,
111		"Enables iIDMA settings "
112		"Default is 1 - perform iIDMA. 0 - no iIDMA.");
113
114int ql2xmaxqueues = 1;
115module_param(ql2xmaxqueues, int, S_IRUGO|S_IRUSR);
116MODULE_PARM_DESC(ql2xmaxqueues,
117		"Enables MQ settings "
118		"Default is 1 for single queue. Set it to number "
119		"of queues in MQ mode.");
120
121int ql2xmultique_tag;
122module_param(ql2xmultique_tag, int, S_IRUGO|S_IRUSR);
123MODULE_PARM_DESC(ql2xmultique_tag,
124		"Enables CPU affinity settings for the driver "
125		"Default is 0 for no affinity of request and response IO. "
126		"Set it to 1 to turn on the cpu affinity.");
127
128int ql2xfwloadbin;
129module_param(ql2xfwloadbin, int, S_IRUGO|S_IRUSR);
130MODULE_PARM_DESC(ql2xfwloadbin,
131		"Option to specify location from which to load ISP firmware:\n"
132		" 2 -- load firmware via the request_firmware() (hotplug)\n"
133		"      interface.\n"
134		" 1 -- load firmware from flash.\n"
135		" 0 -- use default semantics.\n");
136
137int ql2xetsenable;
138module_param(ql2xetsenable, int, S_IRUGO|S_IRUSR);
139MODULE_PARM_DESC(ql2xetsenable,
140		"Enables firmware ETS burst."
141		"Default is 0 - skip ETS enablement.");
142
143int ql2xdbwr = 1;
144module_param(ql2xdbwr, int, S_IRUGO|S_IRUSR);
145MODULE_PARM_DESC(ql2xdbwr,
146	"Option to specify scheme for request queue posting\n"
147	" 0 -- Regular doorbell.\n"
148	" 1 -- CAMRAM doorbell (faster).\n");
149
150int ql2xdontresethba;
151module_param(ql2xdontresethba, int, S_IRUGO|S_IRUSR);
152MODULE_PARM_DESC(ql2xdontresethba,
153	"Option to specify reset behaviour\n"
154	" 0 (Default) -- Reset on failure.\n"
155	" 1 -- Do not reset on failure.\n");
156
157int ql2xtargetreset = 1;
158module_param(ql2xtargetreset, int, S_IRUGO|S_IRUSR);
159MODULE_PARM_DESC(ql2xtargetreset,
160		 "Enable target reset."
161		 "Default is 1 - use hw defaults.");
162
163int ql2xgffidenable;
164module_param(ql2xgffidenable, int, S_IRUGO|S_IRUSR);
165MODULE_PARM_DESC(ql2xgffidenable,
166		"Enables GFF_ID checks of port type. "
167		"Default is 0 - Do not use GFF_ID information.");
168
169int ql2xasynctmfenable;
170module_param(ql2xasynctmfenable, int, S_IRUGO|S_IRUSR);
171MODULE_PARM_DESC(ql2xasynctmfenable,
172		"Enables issue of TM IOCBs asynchronously via IOCB mechanism"
173		"Default is 0 - Issue TM IOCBs via mailbox mechanism.");
174/*
175 * SCSI host template entry points
176 */
177static int qla2xxx_slave_configure(struct scsi_device * device);
178static int qla2xxx_slave_alloc(struct scsi_device *);
179static int qla2xxx_scan_finished(struct Scsi_Host *, unsigned long time);
180static void qla2xxx_scan_start(struct Scsi_Host *);
181static void qla2xxx_slave_destroy(struct scsi_device *);
182static int qla2xxx_queuecommand(struct scsi_cmnd *cmd,
183		void (*fn)(struct scsi_cmnd *));
184static int qla2xxx_eh_abort(struct scsi_cmnd *);
185static int qla2xxx_eh_device_reset(struct scsi_cmnd *);
186static int qla2xxx_eh_target_reset(struct scsi_cmnd *);
187static int qla2xxx_eh_bus_reset(struct scsi_cmnd *);
188static int qla2xxx_eh_host_reset(struct scsi_cmnd *);
189
190static int qla2x00_change_queue_depth(struct scsi_device *, int, int);
191static int qla2x00_change_queue_type(struct scsi_device *, int);
192
193struct scsi_host_template qla2xxx_driver_template = {
194	.module			= THIS_MODULE,
195	.name			= QLA2XXX_DRIVER_NAME,
196	.queuecommand		= qla2xxx_queuecommand,
197
198	.eh_abort_handler	= qla2xxx_eh_abort,
199	.eh_device_reset_handler = qla2xxx_eh_device_reset,
200	.eh_target_reset_handler = qla2xxx_eh_target_reset,
201	.eh_bus_reset_handler	= qla2xxx_eh_bus_reset,
202	.eh_host_reset_handler	= qla2xxx_eh_host_reset,
203
204	.slave_configure	= qla2xxx_slave_configure,
205
206	.slave_alloc		= qla2xxx_slave_alloc,
207	.slave_destroy		= qla2xxx_slave_destroy,
208	.scan_finished		= qla2xxx_scan_finished,
209	.scan_start		= qla2xxx_scan_start,
210	.change_queue_depth	= qla2x00_change_queue_depth,
211	.change_queue_type	= qla2x00_change_queue_type,
212	.this_id		= -1,
213	.cmd_per_lun		= 3,
214	.use_clustering		= ENABLE_CLUSTERING,
215	.sg_tablesize		= SG_ALL,
216
217	.max_sectors		= 0xFFFF,
218	.shost_attrs		= qla2x00_host_attrs,
219};
220
221static struct scsi_transport_template *qla2xxx_transport_template = NULL;
222struct scsi_transport_template *qla2xxx_transport_vport_template = NULL;
223
224/* TODO Convert to inlines
225 *
226 * Timer routines
227 */
228
229__inline__ void
230qla2x00_start_timer(scsi_qla_host_t *vha, void *func, unsigned long interval)
231{
232	init_timer(&vha->timer);
233	vha->timer.expires = jiffies + interval * HZ;
234	vha->timer.data = (unsigned long)vha;
235	vha->timer.function = (void (*)(unsigned long))func;
236	add_timer(&vha->timer);
237	vha->timer_active = 1;
238}
239
240static inline void
241qla2x00_restart_timer(scsi_qla_host_t *vha, unsigned long interval)
242{
243	/* Currently used for 82XX only. */
244	if (vha->device_flags & DFLG_DEV_FAILED)
245		return;
246
247	mod_timer(&vha->timer, jiffies + interval * HZ);
248}
249
250static __inline__ void
251qla2x00_stop_timer(scsi_qla_host_t *vha)
252{
253	del_timer_sync(&vha->timer);
254	vha->timer_active = 0;
255}
256
257static int qla2x00_do_dpc(void *data);
258
259static void qla2x00_rst_aen(scsi_qla_host_t *);
260
261static int qla2x00_mem_alloc(struct qla_hw_data *, uint16_t, uint16_t,
262	struct req_que **, struct rsp_que **);
263static void qla2x00_mem_free(struct qla_hw_data *);
264static void qla2x00_sp_free_dma(srb_t *);
265
266/* -------------------------------------------------------------------------- */
267static int qla2x00_alloc_queues(struct qla_hw_data *ha)
268{
269	ha->req_q_map = kzalloc(sizeof(struct req_que *) * ha->max_req_queues,
270				GFP_KERNEL);
271	if (!ha->req_q_map) {
272		qla_printk(KERN_WARNING, ha,
273			"Unable to allocate memory for request queue ptrs\n");
274		goto fail_req_map;
275	}
276
277	ha->rsp_q_map = kzalloc(sizeof(struct rsp_que *) * ha->max_rsp_queues,
278				GFP_KERNEL);
279	if (!ha->rsp_q_map) {
280		qla_printk(KERN_WARNING, ha,
281			"Unable to allocate memory for response queue ptrs\n");
282		goto fail_rsp_map;
283	}
284	set_bit(0, ha->rsp_qid_map);
285	set_bit(0, ha->req_qid_map);
286	return 1;
287
288fail_rsp_map:
289	kfree(ha->req_q_map);
290	ha->req_q_map = NULL;
291fail_req_map:
292	return -ENOMEM;
293}
294
295static void qla2x00_free_req_que(struct qla_hw_data *ha, struct req_que *req)
296{
297	if (req && req->ring)
298		dma_free_coherent(&ha->pdev->dev,
299		(req->length + 1) * sizeof(request_t),
300		req->ring, req->dma);
301
302	kfree(req);
303	req = NULL;
304}
305
306static void qla2x00_free_rsp_que(struct qla_hw_data *ha, struct rsp_que *rsp)
307{
308	if (rsp && rsp->ring)
309		dma_free_coherent(&ha->pdev->dev,
310		(rsp->length + 1) * sizeof(response_t),
311		rsp->ring, rsp->dma);
312
313	kfree(rsp);
314	rsp = NULL;
315}
316
317static void qla2x00_free_queues(struct qla_hw_data *ha)
318{
319	struct req_que *req;
320	struct rsp_que *rsp;
321	int cnt;
322
323	for (cnt = 0; cnt < ha->max_req_queues; cnt++) {
324		req = ha->req_q_map[cnt];
325		qla2x00_free_req_que(ha, req);
326	}
327	kfree(ha->req_q_map);
328	ha->req_q_map = NULL;
329
330	for (cnt = 0; cnt < ha->max_rsp_queues; cnt++) {
331		rsp = ha->rsp_q_map[cnt];
332		qla2x00_free_rsp_que(ha, rsp);
333	}
334	kfree(ha->rsp_q_map);
335	ha->rsp_q_map = NULL;
336}
337
338static int qla25xx_setup_mode(struct scsi_qla_host *vha)
339{
340	uint16_t options = 0;
341	int ques, req, ret;
342	struct qla_hw_data *ha = vha->hw;
343
344	if (!(ha->fw_attributes & BIT_6)) {
345		qla_printk(KERN_INFO, ha,
346			"Firmware is not multi-queue capable\n");
347		goto fail;
348	}
349	if (ql2xmultique_tag) {
350		/* create a request queue for IO */
351		options |= BIT_7;
352		req = qla25xx_create_req_que(ha, options, 0, 0, -1,
353			QLA_DEFAULT_QUE_QOS);
354		if (!req) {
355			qla_printk(KERN_WARNING, ha,
356				"Can't create request queue\n");
357			goto fail;
358		}
359		ha->wq = create_workqueue("qla2xxx_wq");
360		vha->req = ha->req_q_map[req];
361		options |= BIT_1;
362		for (ques = 1; ques < ha->max_rsp_queues; ques++) {
363			ret = qla25xx_create_rsp_que(ha, options, 0, 0, req);
364			if (!ret) {
365				qla_printk(KERN_WARNING, ha,
366					"Response Queue create failed\n");
367				goto fail2;
368			}
369		}
370		ha->flags.cpu_affinity_enabled = 1;
371
372		DEBUG2(qla_printk(KERN_INFO, ha,
373			"CPU affinity mode enabled, no. of response"
374			" queues:%d, no. of request queues:%d\n",
375			ha->max_rsp_queues, ha->max_req_queues));
376	}
377	return 0;
378fail2:
379	qla25xx_delete_queues(vha);
380	destroy_workqueue(ha->wq);
381	ha->wq = NULL;
382fail:
383	ha->mqenable = 0;
384	kfree(ha->req_q_map);
385	kfree(ha->rsp_q_map);
386	ha->max_req_queues = ha->max_rsp_queues = 1;
387	return 1;
388}
389
390static char *
391qla2x00_pci_info_str(struct scsi_qla_host *vha, char *str)
392{
393	struct qla_hw_data *ha = vha->hw;
394	static char *pci_bus_modes[] = {
395		"33", "66", "100", "133",
396	};
397	uint16_t pci_bus;
398
399	strcpy(str, "PCI");
400	pci_bus = (ha->pci_attr & (BIT_9 | BIT_10)) >> 9;
401	if (pci_bus) {
402		strcat(str, "-X (");
403		strcat(str, pci_bus_modes[pci_bus]);
404	} else {
405		pci_bus = (ha->pci_attr & BIT_8) >> 8;
406		strcat(str, " (");
407		strcat(str, pci_bus_modes[pci_bus]);
408	}
409	strcat(str, " MHz)");
410
411	return (str);
412}
413
414static char *
415qla24xx_pci_info_str(struct scsi_qla_host *vha, char *str)
416{
417	static char *pci_bus_modes[] = { "33", "66", "100", "133", };
418	struct qla_hw_data *ha = vha->hw;
419	uint32_t pci_bus;
420	int pcie_reg;
421
422	pcie_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_EXP);
423	if (pcie_reg) {
424		char lwstr[6];
425		uint16_t pcie_lstat, lspeed, lwidth;
426
427		pcie_reg += 0x12;
428		pci_read_config_word(ha->pdev, pcie_reg, &pcie_lstat);
429		lspeed = pcie_lstat & (BIT_0 | BIT_1 | BIT_2 | BIT_3);
430		lwidth = (pcie_lstat &
431		    (BIT_4 | BIT_5 | BIT_6 | BIT_7 | BIT_8 | BIT_9)) >> 4;
432
433		strcpy(str, "PCIe (");
434		if (lspeed == 1)
435			strcat(str, "2.5GT/s ");
436		else if (lspeed == 2)
437			strcat(str, "5.0GT/s ");
438		else
439			strcat(str, "<unknown> ");
440		snprintf(lwstr, sizeof(lwstr), "x%d)", lwidth);
441		strcat(str, lwstr);
442
443		return str;
444	}
445
446	strcpy(str, "PCI");
447	pci_bus = (ha->pci_attr & CSRX_PCIX_BUS_MODE_MASK) >> 8;
448	if (pci_bus == 0 || pci_bus == 8) {
449		strcat(str, " (");
450		strcat(str, pci_bus_modes[pci_bus >> 3]);
451	} else {
452		strcat(str, "-X ");
453		if (pci_bus & BIT_2)
454			strcat(str, "Mode 2");
455		else
456			strcat(str, "Mode 1");
457		strcat(str, " (");
458		strcat(str, pci_bus_modes[pci_bus & ~BIT_2]);
459	}
460	strcat(str, " MHz)");
461
462	return str;
463}
464
465static char *
466qla2x00_fw_version_str(struct scsi_qla_host *vha, char *str)
467{
468	char un_str[10];
469	struct qla_hw_data *ha = vha->hw;
470
471	sprintf(str, "%d.%02d.%02d ", ha->fw_major_version,
472	    ha->fw_minor_version,
473	    ha->fw_subminor_version);
474
475	if (ha->fw_attributes & BIT_9) {
476		strcat(str, "FLX");
477		return (str);
478	}
479
480	switch (ha->fw_attributes & 0xFF) {
481	case 0x7:
482		strcat(str, "EF");
483		break;
484	case 0x17:
485		strcat(str, "TP");
486		break;
487	case 0x37:
488		strcat(str, "IP");
489		break;
490	case 0x77:
491		strcat(str, "VI");
492		break;
493	default:
494		sprintf(un_str, "(%x)", ha->fw_attributes);
495		strcat(str, un_str);
496		break;
497	}
498	if (ha->fw_attributes & 0x100)
499		strcat(str, "X");
500
501	return (str);
502}
503
504static char *
505qla24xx_fw_version_str(struct scsi_qla_host *vha, char *str)
506{
507	struct qla_hw_data *ha = vha->hw;
508
509	sprintf(str, "%d.%02d.%02d (%x)", ha->fw_major_version,
510	    ha->fw_minor_version, ha->fw_subminor_version, ha->fw_attributes);
511	return str;
512}
513
514static inline srb_t *
515qla2x00_get_new_sp(scsi_qla_host_t *vha, fc_port_t *fcport,
516    struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
517{
518	srb_t *sp;
519	struct qla_hw_data *ha = vha->hw;
520
521	sp = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
522	if (!sp)
523		return sp;
524
525	atomic_set(&sp->ref_count, 1);
526	sp->fcport = fcport;
527	sp->cmd = cmd;
528	sp->flags = 0;
529	CMD_SP(cmd) = (void *)sp;
530	cmd->scsi_done = done;
531	sp->ctx = NULL;
532
533	return sp;
534}
535
536static int
537qla2xxx_queuecommand(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
538{
539	scsi_qla_host_t *vha = shost_priv(cmd->device->host);
540	fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
541	struct fc_rport *rport = starget_to_rport(scsi_target(cmd->device));
542	struct qla_hw_data *ha = vha->hw;
543	struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
544	srb_t *sp;
545	int rval;
546
547	if (ha->flags.eeh_busy) {
548		if (ha->flags.pci_channel_io_perm_failure)
549			cmd->result = DID_NO_CONNECT << 16;
550		else
551			cmd->result = DID_REQUEUE << 16;
552		goto qc24_fail_command;
553	}
554
555	rval = fc_remote_port_chkready(rport);
556	if (rval) {
557		cmd->result = rval;
558		goto qc24_fail_command;
559	}
560
561	/* Close window on fcport/rport state-transitioning. */
562	if (fcport->drport)
563		goto qc24_target_busy;
564
565	if (!vha->flags.difdix_supported &&
566		scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
567			DEBUG2(qla_printk(KERN_ERR, ha,
568			    "DIF Cap Not Reg, fail DIF capable cmd's:%x\n",
569			    cmd->cmnd[0]));
570			cmd->result = DID_NO_CONNECT << 16;
571			goto qc24_fail_command;
572	}
573	if (atomic_read(&fcport->state) != FCS_ONLINE) {
574		if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD ||
575		    atomic_read(&base_vha->loop_state) == LOOP_DEAD) {
576			cmd->result = DID_NO_CONNECT << 16;
577			goto qc24_fail_command;
578		}
579		goto qc24_target_busy;
580	}
581
582	spin_unlock_irq(vha->host->host_lock);
583
584	sp = qla2x00_get_new_sp(base_vha, fcport, cmd, done);
585	if (!sp)
586		goto qc24_host_busy_lock;
587
588	rval = ha->isp_ops->start_scsi(sp);
589	if (rval != QLA_SUCCESS)
590		goto qc24_host_busy_free_sp;
591
592	spin_lock_irq(vha->host->host_lock);
593
594	return 0;
595
596qc24_host_busy_free_sp:
597	qla2x00_sp_free_dma(sp);
598	mempool_free(sp, ha->srb_mempool);
599
600qc24_host_busy_lock:
601	spin_lock_irq(vha->host->host_lock);
602	return SCSI_MLQUEUE_HOST_BUSY;
603
604qc24_target_busy:
605	return SCSI_MLQUEUE_TARGET_BUSY;
606
607qc24_fail_command:
608	done(cmd);
609
610	return 0;
611}
612
613
614/*
615 * qla2x00_eh_wait_on_command
616 *    Waits for the command to be returned by the Firmware for some
617 *    max time.
618 *
619 * Input:
620 *    cmd = Scsi Command to wait on.
621 *
622 * Return:
623 *    Not Found : 0
624 *    Found : 1
625 */
626static int
627qla2x00_eh_wait_on_command(struct scsi_cmnd *cmd)
628{
629#define ABORT_POLLING_PERIOD	1000
630#define ABORT_WAIT_ITER		((10 * 1000) / (ABORT_POLLING_PERIOD))
631	unsigned long wait_iter = ABORT_WAIT_ITER;
632	scsi_qla_host_t *vha = shost_priv(cmd->device->host);
633	struct qla_hw_data *ha = vha->hw;
634	int ret = QLA_SUCCESS;
635
636	if (unlikely(pci_channel_offline(ha->pdev)) || ha->flags.eeh_busy) {
637		DEBUG17(qla_printk(KERN_WARNING, ha, "return:eh_wait\n"));
638		return ret;
639	}
640
641	while (CMD_SP(cmd) && wait_iter--) {
642		msleep(ABORT_POLLING_PERIOD);
643	}
644	if (CMD_SP(cmd))
645		ret = QLA_FUNCTION_FAILED;
646
647	return ret;
648}
649
650/*
651 * qla2x00_wait_for_hba_online
652 *    Wait till the HBA is online after going through
653 *    <= MAX_RETRIES_OF_ISP_ABORT  or
654 *    finally HBA is disabled ie marked offline
655 *
656 * Input:
657 *     ha - pointer to host adapter structure
658 *
659 * Note:
660 *    Does context switching-Release SPIN_LOCK
661 *    (if any) before calling this routine.
662 *
663 * Return:
664 *    Success (Adapter is online) : 0
665 *    Failed  (Adapter is offline/disabled) : 1
666 */
667int
668qla2x00_wait_for_hba_online(scsi_qla_host_t *vha)
669{
670	int		return_status;
671	unsigned long	wait_online;
672	struct qla_hw_data *ha = vha->hw;
673	scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
674
675	wait_online = jiffies + (MAX_LOOP_TIMEOUT * HZ);
676	while (((test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags)) ||
677	    test_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags) ||
678	    test_bit(ISP_ABORT_RETRY, &base_vha->dpc_flags) ||
679	    ha->dpc_active) && time_before(jiffies, wait_online)) {
680
681		msleep(1000);
682	}
683	if (base_vha->flags.online)
684		return_status = QLA_SUCCESS;
685	else
686		return_status = QLA_FUNCTION_FAILED;
687
688	return (return_status);
689}
690
691/*
692 * qla2x00_wait_for_reset_ready
693 *    Wait till the HBA is online after going through
694 *    <= MAX_RETRIES_OF_ISP_ABORT  or
695 *    finally HBA is disabled ie marked offline or flash
696 *    operations are in progress.
697 *
698 * Input:
699 *     ha - pointer to host adapter structure
700 *
701 * Note:
702 *    Does context switching-Release SPIN_LOCK
703 *    (if any) before calling this routine.
704 *
705 * Return:
706 *    Success (Adapter is online/no flash ops) : 0
707 *    Failed  (Adapter is offline/disabled/flash ops in progress) : 1
708 */
709static int
710qla2x00_wait_for_reset_ready(scsi_qla_host_t *vha)
711{
712	int		return_status;
713	unsigned long	wait_online;
714	struct qla_hw_data *ha = vha->hw;
715	scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
716
717	wait_online = jiffies + (MAX_LOOP_TIMEOUT * HZ);
718	while (((test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags)) ||
719	    test_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags) ||
720	    test_bit(ISP_ABORT_RETRY, &base_vha->dpc_flags) ||
721	    ha->optrom_state != QLA_SWAITING ||
722	    ha->dpc_active) && time_before(jiffies, wait_online))
723		msleep(1000);
724
725	if (base_vha->flags.online &&  ha->optrom_state == QLA_SWAITING)
726		return_status = QLA_SUCCESS;
727	else
728		return_status = QLA_FUNCTION_FAILED;
729
730	DEBUG2(printk("%s return_status=%d\n", __func__, return_status));
731
732	return return_status;
733}
734
735int
736qla2x00_wait_for_chip_reset(scsi_qla_host_t *vha)
737{
738	int		return_status;
739	unsigned long	wait_reset;
740	struct qla_hw_data *ha = vha->hw;
741	scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
742
743	wait_reset = jiffies + (MAX_LOOP_TIMEOUT * HZ);
744	while (((test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags)) ||
745	    test_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags) ||
746	    test_bit(ISP_ABORT_RETRY, &base_vha->dpc_flags) ||
747	    ha->dpc_active) && time_before(jiffies, wait_reset)) {
748
749		msleep(1000);
750
751		if (!test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags) &&
752		    ha->flags.chip_reset_done)
753			break;
754	}
755	if (ha->flags.chip_reset_done)
756		return_status = QLA_SUCCESS;
757	else
758		return_status = QLA_FUNCTION_FAILED;
759
760	return return_status;
761}
762
763/*
764 * qla2x00_wait_for_loop_ready
765 *    Wait for MAX_LOOP_TIMEOUT(5 min) value for loop
766 *    to be in LOOP_READY state.
767 * Input:
768 *     ha - pointer to host adapter structure
769 *
770 * Note:
771 *    Does context switching-Release SPIN_LOCK
772 *    (if any) before calling this routine.
773 *
774 *
775 * Return:
776 *    Success (LOOP_READY) : 0
777 *    Failed  (LOOP_NOT_READY) : 1
778 */
779static inline int
780qla2x00_wait_for_loop_ready(scsi_qla_host_t *vha)
781{
782	int 	 return_status = QLA_SUCCESS;
783	unsigned long loop_timeout ;
784	struct qla_hw_data *ha = vha->hw;
785	scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
786
787	/* wait for 5 min at the max for loop to be ready */
788	loop_timeout = jiffies + (MAX_LOOP_TIMEOUT * HZ);
789
790	while ((!atomic_read(&base_vha->loop_down_timer) &&
791	    atomic_read(&base_vha->loop_state) == LOOP_DOWN) ||
792	    atomic_read(&base_vha->loop_state) != LOOP_READY) {
793		if (atomic_read(&base_vha->loop_state) == LOOP_DEAD) {
794			return_status = QLA_FUNCTION_FAILED;
795			break;
796		}
797		msleep(1000);
798		if (time_after_eq(jiffies, loop_timeout)) {
799			return_status = QLA_FUNCTION_FAILED;
800			break;
801		}
802	}
803	return (return_status);
804}
805
806static void
807sp_get(struct srb *sp)
808{
809	atomic_inc(&sp->ref_count);
810}
811
812/**************************************************************************
813* qla2xxx_eh_abort
814*
815* Description:
816*    The abort function will abort the specified command.
817*
818* Input:
819*    cmd = Linux SCSI command packet to be aborted.
820*
821* Returns:
822*    Either SUCCESS or FAILED.
823*
824* Note:
825*    Only return FAILED if command not returned by firmware.
826**************************************************************************/
827static int
828qla2xxx_eh_abort(struct scsi_cmnd *cmd)
829{
830	scsi_qla_host_t *vha = shost_priv(cmd->device->host);
831	srb_t *sp;
832	int ret, i;
833	unsigned int id, lun;
834	unsigned long serial;
835	unsigned long flags;
836	int wait = 0;
837	struct qla_hw_data *ha = vha->hw;
838	struct req_que *req = vha->req;
839	srb_t *spt;
840	int got_ref = 0;
841
842	fc_block_scsi_eh(cmd);
843
844	if (!CMD_SP(cmd))
845		return SUCCESS;
846
847	ret = SUCCESS;
848
849	id = cmd->device->id;
850	lun = cmd->device->lun;
851	serial = cmd->serial_number;
852	spt = (srb_t *) CMD_SP(cmd);
853	if (!spt)
854		return SUCCESS;
855
856	/* Check active list for command command. */
857	spin_lock_irqsave(&ha->hardware_lock, flags);
858	for (i = 1; i < MAX_OUTSTANDING_COMMANDS; i++) {
859		sp = req->outstanding_cmds[i];
860
861		if (sp == NULL)
862			continue;
863		if ((sp->ctx) && !(sp->flags & SRB_FCP_CMND_DMA_VALID) &&
864		    !IS_PROT_IO(sp))
865			continue;
866		if (sp->cmd != cmd)
867			continue;
868
869		DEBUG2(printk("%s(%ld): aborting sp %p from RISC."
870		" pid=%ld.\n", __func__, vha->host_no, sp, serial));
871
872		/* Get a reference to the sp and drop the lock.*/
873		sp_get(sp);
874		got_ref++;
875
876		spin_unlock_irqrestore(&ha->hardware_lock, flags);
877		if (ha->isp_ops->abort_command(sp)) {
878			DEBUG2(printk("%s(%ld): abort_command "
879			"mbx failed.\n", __func__, vha->host_no));
880			ret = FAILED;
881		} else {
882			DEBUG3(printk("%s(%ld): abort_command "
883			"mbx success.\n", __func__, vha->host_no));
884			wait = 1;
885		}
886		spin_lock_irqsave(&ha->hardware_lock, flags);
887		break;
888	}
889	spin_unlock_irqrestore(&ha->hardware_lock, flags);
890
891	/* Wait for the command to be returned. */
892	if (wait) {
893		if (qla2x00_eh_wait_on_command(cmd) != QLA_SUCCESS) {
894			qla_printk(KERN_ERR, ha,
895			    "scsi(%ld:%d:%d): Abort handler timed out -- %lx "
896			    "%x.\n", vha->host_no, id, lun, serial, ret);
897			ret = FAILED;
898		}
899	}
900
901	if (got_ref)
902		qla2x00_sp_compl(ha, sp);
903
904	qla_printk(KERN_INFO, ha,
905	    "scsi(%ld:%d:%d): Abort command issued -- %d %lx %x.\n",
906	    vha->host_no, id, lun, wait, serial, ret);
907
908	return ret;
909}
910
911int
912qla2x00_eh_wait_for_pending_commands(scsi_qla_host_t *vha, unsigned int t,
913	unsigned int l, enum nexus_wait_type type)
914{
915	int cnt, match, status;
916	unsigned long flags;
917	struct qla_hw_data *ha = vha->hw;
918	struct req_que *req;
919	srb_t *sp;
920
921	status = QLA_SUCCESS;
922
923	spin_lock_irqsave(&ha->hardware_lock, flags);
924	req = vha->req;
925	for (cnt = 1; status == QLA_SUCCESS &&
926		cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
927		sp = req->outstanding_cmds[cnt];
928		if (!sp)
929			continue;
930		if ((sp->ctx) && !IS_PROT_IO(sp))
931			continue;
932		if (vha->vp_idx != sp->fcport->vha->vp_idx)
933			continue;
934		match = 0;
935		switch (type) {
936		case WAIT_HOST:
937			match = 1;
938			break;
939		case WAIT_TARGET:
940			match = sp->cmd->device->id == t;
941			break;
942		case WAIT_LUN:
943			match = (sp->cmd->device->id == t &&
944				sp->cmd->device->lun == l);
945			break;
946		}
947		if (!match)
948			continue;
949
950		spin_unlock_irqrestore(&ha->hardware_lock, flags);
951		status = qla2x00_eh_wait_on_command(sp->cmd);
952		spin_lock_irqsave(&ha->hardware_lock, flags);
953	}
954	spin_unlock_irqrestore(&ha->hardware_lock, flags);
955
956	return status;
957}
958
959static char *reset_errors[] = {
960	"HBA not online",
961	"HBA not ready",
962	"Task management failed",
963	"Waiting for command completions",
964};
965
966static int
967__qla2xxx_eh_generic_reset(char *name, enum nexus_wait_type type,
968    struct scsi_cmnd *cmd, int (*do_reset)(struct fc_port *, unsigned int, int))
969{
970	scsi_qla_host_t *vha = shost_priv(cmd->device->host);
971	fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
972	int err;
973
974	fc_block_scsi_eh(cmd);
975
976	if (!fcport)
977		return FAILED;
978
979	qla_printk(KERN_INFO, vha->hw, "scsi(%ld:%d:%d): %s RESET ISSUED.\n",
980	    vha->host_no, cmd->device->id, cmd->device->lun, name);
981
982	err = 0;
983	if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS)
984		goto eh_reset_failed;
985	err = 1;
986	if (qla2x00_wait_for_loop_ready(vha) != QLA_SUCCESS)
987		goto eh_reset_failed;
988	err = 2;
989	if (do_reset(fcport, cmd->device->lun, cmd->request->cpu + 1)
990		!= QLA_SUCCESS)
991		goto eh_reset_failed;
992	err = 3;
993	if (qla2x00_eh_wait_for_pending_commands(vha, cmd->device->id,
994	    cmd->device->lun, type) != QLA_SUCCESS)
995		goto eh_reset_failed;
996
997	qla_printk(KERN_INFO, vha->hw, "scsi(%ld:%d:%d): %s RESET SUCCEEDED.\n",
998	    vha->host_no, cmd->device->id, cmd->device->lun, name);
999
1000	return SUCCESS;
1001
1002eh_reset_failed:
1003	qla_printk(KERN_INFO, vha->hw, "scsi(%ld:%d:%d): %s RESET FAILED: %s.\n"
1004	    , vha->host_no, cmd->device->id, cmd->device->lun, name,
1005	    reset_errors[err]);
1006	return FAILED;
1007}
1008
1009static int
1010qla2xxx_eh_device_reset(struct scsi_cmnd *cmd)
1011{
1012	scsi_qla_host_t *vha = shost_priv(cmd->device->host);
1013	struct qla_hw_data *ha = vha->hw;
1014
1015	return __qla2xxx_eh_generic_reset("DEVICE", WAIT_LUN, cmd,
1016	    ha->isp_ops->lun_reset);
1017}
1018
1019static int
1020qla2xxx_eh_target_reset(struct scsi_cmnd *cmd)
1021{
1022	scsi_qla_host_t *vha = shost_priv(cmd->device->host);
1023	struct qla_hw_data *ha = vha->hw;
1024
1025	return __qla2xxx_eh_generic_reset("TARGET", WAIT_TARGET, cmd,
1026	    ha->isp_ops->target_reset);
1027}
1028
1029/**************************************************************************
1030* qla2xxx_eh_bus_reset
1031*
1032* Description:
1033*    The bus reset function will reset the bus and abort any executing
1034*    commands.
1035*
1036* Input:
1037*    cmd = Linux SCSI command packet of the command that cause the
1038*          bus reset.
1039*
1040* Returns:
1041*    SUCCESS/FAILURE (defined as macro in scsi.h).
1042*
1043**************************************************************************/
1044static int
1045qla2xxx_eh_bus_reset(struct scsi_cmnd *cmd)
1046{
1047	scsi_qla_host_t *vha = shost_priv(cmd->device->host);
1048	fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
1049	int ret = FAILED;
1050	unsigned int id, lun;
1051	unsigned long serial;
1052
1053	fc_block_scsi_eh(cmd);
1054
1055	id = cmd->device->id;
1056	lun = cmd->device->lun;
1057	serial = cmd->serial_number;
1058
1059	if (!fcport)
1060		return ret;
1061
1062	qla_printk(KERN_INFO, vha->hw,
1063	    "scsi(%ld:%d:%d): BUS RESET ISSUED.\n", vha->host_no, id, lun);
1064
1065	if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
1066		DEBUG2(printk("%s failed:board disabled\n",__func__));
1067		goto eh_bus_reset_done;
1068	}
1069
1070	if (qla2x00_wait_for_loop_ready(vha) == QLA_SUCCESS) {
1071		if (qla2x00_loop_reset(vha) == QLA_SUCCESS)
1072			ret = SUCCESS;
1073	}
1074	if (ret == FAILED)
1075		goto eh_bus_reset_done;
1076
1077	/* Flush outstanding commands. */
1078	if (qla2x00_eh_wait_for_pending_commands(vha, 0, 0, WAIT_HOST) !=
1079	    QLA_SUCCESS)
1080		ret = FAILED;
1081
1082eh_bus_reset_done:
1083	qla_printk(KERN_INFO, vha->hw, "%s: reset %s\n", __func__,
1084	    (ret == FAILED) ? "failed" : "succeded");
1085
1086	return ret;
1087}
1088
1089/**************************************************************************
1090* qla2xxx_eh_host_reset
1091*
1092* Description:
1093*    The reset function will reset the Adapter.
1094*
1095* Input:
1096*      cmd = Linux SCSI command packet of the command that cause the
1097*            adapter reset.
1098*
1099* Returns:
1100*      Either SUCCESS or FAILED.
1101*
1102* Note:
1103**************************************************************************/
1104static int
1105qla2xxx_eh_host_reset(struct scsi_cmnd *cmd)
1106{
1107	scsi_qla_host_t *vha = shost_priv(cmd->device->host);
1108	fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
1109	struct qla_hw_data *ha = vha->hw;
1110	int ret = FAILED;
1111	unsigned int id, lun;
1112	unsigned long serial;
1113	scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
1114
1115	fc_block_scsi_eh(cmd);
1116
1117	id = cmd->device->id;
1118	lun = cmd->device->lun;
1119	serial = cmd->serial_number;
1120
1121	if (!fcport)
1122		return ret;
1123
1124	qla_printk(KERN_INFO, ha,
1125	    "scsi(%ld:%d:%d): ADAPTER RESET ISSUED.\n", vha->host_no, id, lun);
1126
1127	if (qla2x00_wait_for_reset_ready(vha) != QLA_SUCCESS)
1128		goto eh_host_reset_lock;
1129
1130	qla2x00_wait_for_loop_ready(vha);
1131	if (vha != base_vha) {
1132		if (qla2x00_vp_abort_isp(vha))
1133			goto eh_host_reset_lock;
1134	} else {
1135		if (IS_QLA82XX(vha->hw)) {
1136			if (!qla82xx_fcoe_ctx_reset(vha)) {
1137				/* Ctx reset success */
1138				ret = SUCCESS;
1139				goto eh_host_reset_lock;
1140			}
1141			/* fall thru if ctx reset failed */
1142		}
1143		if (ha->wq)
1144			flush_workqueue(ha->wq);
1145
1146		set_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
1147		if (ha->isp_ops->abort_isp(base_vha)) {
1148			clear_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
1149			/* failed. schedule dpc to try */
1150			set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
1151
1152			if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS)
1153				goto eh_host_reset_lock;
1154		}
1155		clear_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
1156	}
1157
1158	/* Waiting for command to be returned to OS.*/
1159	if (qla2x00_eh_wait_for_pending_commands(vha, 0, 0, WAIT_HOST) ==
1160		QLA_SUCCESS)
1161		ret = SUCCESS;
1162
1163eh_host_reset_lock:
1164	qla_printk(KERN_INFO, ha, "%s: reset %s\n", __func__,
1165	    (ret == FAILED) ? "failed" : "succeded");
1166
1167	return ret;
1168}
1169
1170/*
1171* qla2x00_loop_reset
1172*      Issue loop reset.
1173*
1174* Input:
1175*      ha = adapter block pointer.
1176*
1177* Returns:
1178*      0 = success
1179*/
1180int
1181qla2x00_loop_reset(scsi_qla_host_t *vha)
1182{
1183	int ret;
1184	struct fc_port *fcport;
1185	struct qla_hw_data *ha = vha->hw;
1186
1187	if (ql2xtargetreset == 1 && ha->flags.enable_target_reset) {
1188		list_for_each_entry(fcport, &vha->vp_fcports, list) {
1189			if (fcport->port_type != FCT_TARGET)
1190				continue;
1191
1192			ret = ha->isp_ops->target_reset(fcport, 0, 0);
1193			if (ret != QLA_SUCCESS) {
1194				DEBUG2_3(printk("%s(%ld): bus_reset failed: "
1195				    "target_reset=%d d_id=%x.\n", __func__,
1196				    vha->host_no, ret, fcport->d_id.b24));
1197			}
1198		}
1199	}
1200
1201	if (ha->flags.enable_lip_full_login && !IS_QLA8XXX_TYPE(ha)) {
1202		ret = qla2x00_full_login_lip(vha);
1203		if (ret != QLA_SUCCESS) {
1204			DEBUG2_3(printk("%s(%ld): failed: "
1205			    "full_login_lip=%d.\n", __func__, vha->host_no,
1206			    ret));
1207		}
1208		atomic_set(&vha->loop_state, LOOP_DOWN);
1209		atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
1210		qla2x00_mark_all_devices_lost(vha, 0);
1211		qla2x00_wait_for_loop_ready(vha);
1212	}
1213
1214	if (ha->flags.enable_lip_reset) {
1215		ret = qla2x00_lip_reset(vha);
1216		if (ret != QLA_SUCCESS) {
1217			DEBUG2_3(printk("%s(%ld): failed: "
1218			    "lip_reset=%d.\n", __func__, vha->host_no, ret));
1219		} else
1220			qla2x00_wait_for_loop_ready(vha);
1221	}
1222
1223	/* Issue marker command only when we are going to start the I/O */
1224	vha->marker_needed = 1;
1225
1226	return QLA_SUCCESS;
1227}
1228
1229void
1230qla2x00_abort_all_cmds(scsi_qla_host_t *vha, int res)
1231{
1232	int que, cnt;
1233	unsigned long flags;
1234	srb_t *sp;
1235	struct srb_ctx *ctx;
1236	struct qla_hw_data *ha = vha->hw;
1237	struct req_que *req;
1238
1239	spin_lock_irqsave(&ha->hardware_lock, flags);
1240	for (que = 0; que < ha->max_req_queues; que++) {
1241		req = ha->req_q_map[que];
1242		if (!req)
1243			continue;
1244		for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
1245			sp = req->outstanding_cmds[cnt];
1246			if (sp) {
1247				req->outstanding_cmds[cnt] = NULL;
1248				if (!sp->ctx ||
1249					(sp->flags & SRB_FCP_CMND_DMA_VALID) ||
1250					IS_PROT_IO(sp)) {
1251					sp->cmd->result = res;
1252					qla2x00_sp_compl(ha, sp);
1253				} else {
1254					ctx = sp->ctx;
1255					if (ctx->type == SRB_LOGIN_CMD ||
1256					    ctx->type == SRB_LOGOUT_CMD) {
1257						ctx->u.iocb_cmd->free(sp);
1258					} else {
1259						struct fc_bsg_job *bsg_job =
1260						    ctx->u.bsg_job;
1261						if (bsg_job->request->msgcode
1262						    == FC_BSG_HST_CT)
1263							kfree(sp->fcport);
1264						bsg_job->req->errors = 0;
1265						bsg_job->reply->result = res;
1266						bsg_job->job_done(bsg_job);
1267						kfree(sp->ctx);
1268						mempool_free(sp,
1269							ha->srb_mempool);
1270					}
1271				}
1272			}
1273		}
1274	}
1275	spin_unlock_irqrestore(&ha->hardware_lock, flags);
1276}
1277
1278static int
1279qla2xxx_slave_alloc(struct scsi_device *sdev)
1280{
1281	struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1282
1283	if (!rport || fc_remote_port_chkready(rport))
1284		return -ENXIO;
1285
1286	sdev->hostdata = *(fc_port_t **)rport->dd_data;
1287
1288	return 0;
1289}
1290
1291static int
1292qla2xxx_slave_configure(struct scsi_device *sdev)
1293{
1294	scsi_qla_host_t *vha = shost_priv(sdev->host);
1295	struct qla_hw_data *ha = vha->hw;
1296	struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
1297	struct req_que *req = vha->req;
1298
1299	if (sdev->tagged_supported)
1300		scsi_activate_tcq(sdev, req->max_q_depth);
1301	else
1302		scsi_deactivate_tcq(sdev, req->max_q_depth);
1303
1304	rport->dev_loss_tmo = ha->port_down_retry_count;
1305
1306	return 0;
1307}
1308
1309static void
1310qla2xxx_slave_destroy(struct scsi_device *sdev)
1311{
1312	sdev->hostdata = NULL;
1313}
1314
1315static void qla2x00_handle_queue_full(struct scsi_device *sdev, int qdepth)
1316{
1317	fc_port_t *fcport = (struct fc_port *) sdev->hostdata;
1318
1319	if (!scsi_track_queue_full(sdev, qdepth))
1320		return;
1321
1322	DEBUG2(qla_printk(KERN_INFO, fcport->vha->hw,
1323		"scsi(%ld:%d:%d:%d): Queue depth adjusted-down to %d.\n",
1324		fcport->vha->host_no, sdev->channel, sdev->id, sdev->lun,
1325		sdev->queue_depth));
1326}
1327
1328static void qla2x00_adjust_sdev_qdepth_up(struct scsi_device *sdev, int qdepth)
1329{
1330	fc_port_t *fcport = sdev->hostdata;
1331	struct scsi_qla_host *vha = fcport->vha;
1332	struct qla_hw_data *ha = vha->hw;
1333	struct req_que *req = NULL;
1334
1335	req = vha->req;
1336	if (!req)
1337		return;
1338
1339	if (req->max_q_depth <= sdev->queue_depth || req->max_q_depth < qdepth)
1340		return;
1341
1342	if (sdev->ordered_tags)
1343		scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG, qdepth);
1344	else
1345		scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, qdepth);
1346
1347	DEBUG2(qla_printk(KERN_INFO, ha,
1348	       "scsi(%ld:%d:%d:%d): Queue depth adjusted-up to %d.\n",
1349	       fcport->vha->host_no, sdev->channel, sdev->id, sdev->lun,
1350	       sdev->queue_depth));
1351}
1352
1353static int
1354qla2x00_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
1355{
1356	switch (reason) {
1357	case SCSI_QDEPTH_DEFAULT:
1358		scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
1359		break;
1360	case SCSI_QDEPTH_QFULL:
1361		qla2x00_handle_queue_full(sdev, qdepth);
1362		break;
1363	case SCSI_QDEPTH_RAMP_UP:
1364		qla2x00_adjust_sdev_qdepth_up(sdev, qdepth);
1365		break;
1366	default:
1367		return -EOPNOTSUPP;
1368	}
1369
1370	return sdev->queue_depth;
1371}
1372
1373static int
1374qla2x00_change_queue_type(struct scsi_device *sdev, int tag_type)
1375{
1376	if (sdev->tagged_supported) {
1377		scsi_set_tag_type(sdev, tag_type);
1378		if (tag_type)
1379			scsi_activate_tcq(sdev, sdev->queue_depth);
1380		else
1381			scsi_deactivate_tcq(sdev, sdev->queue_depth);
1382	} else
1383		tag_type = 0;
1384
1385	return tag_type;
1386}
1387
1388/**
1389 * qla2x00_config_dma_addressing() - Configure OS DMA addressing method.
1390 * @ha: HA context
1391 *
1392 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
1393 * supported addressing method.
1394 */
1395static void
1396qla2x00_config_dma_addressing(struct qla_hw_data *ha)
1397{
1398	/* Assume a 32bit DMA mask. */
1399	ha->flags.enable_64bit_addressing = 0;
1400
1401	if (!dma_set_mask(&ha->pdev->dev, DMA_BIT_MASK(64))) {
1402		/* Any upper-dword bits set? */
1403		if (MSD(dma_get_required_mask(&ha->pdev->dev)) &&
1404		    !pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
1405			/* Ok, a 64bit DMA mask is applicable. */
1406			ha->flags.enable_64bit_addressing = 1;
1407			ha->isp_ops->calc_req_entries = qla2x00_calc_iocbs_64;
1408			ha->isp_ops->build_iocbs = qla2x00_build_scsi_iocbs_64;
1409			return;
1410		}
1411	}
1412
1413	dma_set_mask(&ha->pdev->dev, DMA_BIT_MASK(32));
1414	pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(32));
1415}
1416
1417static void
1418qla2x00_enable_intrs(struct qla_hw_data *ha)
1419{
1420	unsigned long flags = 0;
1421	struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1422
1423	spin_lock_irqsave(&ha->hardware_lock, flags);
1424	ha->interrupts_on = 1;
1425	/* enable risc and host interrupts */
1426	WRT_REG_WORD(&reg->ictrl, ICR_EN_INT | ICR_EN_RISC);
1427	RD_REG_WORD(&reg->ictrl);
1428	spin_unlock_irqrestore(&ha->hardware_lock, flags);
1429
1430}
1431
1432static void
1433qla2x00_disable_intrs(struct qla_hw_data *ha)
1434{
1435	unsigned long flags = 0;
1436	struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1437
1438	spin_lock_irqsave(&ha->hardware_lock, flags);
1439	ha->interrupts_on = 0;
1440	/* disable risc and host interrupts */
1441	WRT_REG_WORD(&reg->ictrl, 0);
1442	RD_REG_WORD(&reg->ictrl);
1443	spin_unlock_irqrestore(&ha->hardware_lock, flags);
1444}
1445
1446static void
1447qla24xx_enable_intrs(struct qla_hw_data *ha)
1448{
1449	unsigned long flags = 0;
1450	struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1451
1452	spin_lock_irqsave(&ha->hardware_lock, flags);
1453	ha->interrupts_on = 1;
1454	WRT_REG_DWORD(&reg->ictrl, ICRX_EN_RISC_INT);
1455	RD_REG_DWORD(&reg->ictrl);
1456	spin_unlock_irqrestore(&ha->hardware_lock, flags);
1457}
1458
1459static void
1460qla24xx_disable_intrs(struct qla_hw_data *ha)
1461{
1462	unsigned long flags = 0;
1463	struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1464
1465	if (IS_NOPOLLING_TYPE(ha))
1466		return;
1467	spin_lock_irqsave(&ha->hardware_lock, flags);
1468	ha->interrupts_on = 0;
1469	WRT_REG_DWORD(&reg->ictrl, 0);
1470	RD_REG_DWORD(&reg->ictrl);
1471	spin_unlock_irqrestore(&ha->hardware_lock, flags);
1472}
1473
1474static struct isp_operations qla2100_isp_ops = {
1475	.pci_config		= qla2100_pci_config,
1476	.reset_chip		= qla2x00_reset_chip,
1477	.chip_diag		= qla2x00_chip_diag,
1478	.config_rings		= qla2x00_config_rings,
1479	.reset_adapter		= qla2x00_reset_adapter,
1480	.nvram_config		= qla2x00_nvram_config,
1481	.update_fw_options	= qla2x00_update_fw_options,
1482	.load_risc		= qla2x00_load_risc,
1483	.pci_info_str		= qla2x00_pci_info_str,
1484	.fw_version_str		= qla2x00_fw_version_str,
1485	.intr_handler		= qla2100_intr_handler,
1486	.enable_intrs		= qla2x00_enable_intrs,
1487	.disable_intrs		= qla2x00_disable_intrs,
1488	.abort_command		= qla2x00_abort_command,
1489	.target_reset		= qla2x00_abort_target,
1490	.lun_reset		= qla2x00_lun_reset,
1491	.fabric_login		= qla2x00_login_fabric,
1492	.fabric_logout		= qla2x00_fabric_logout,
1493	.calc_req_entries	= qla2x00_calc_iocbs_32,
1494	.build_iocbs		= qla2x00_build_scsi_iocbs_32,
1495	.prep_ms_iocb		= qla2x00_prep_ms_iocb,
1496	.prep_ms_fdmi_iocb	= qla2x00_prep_ms_fdmi_iocb,
1497	.read_nvram		= qla2x00_read_nvram_data,
1498	.write_nvram		= qla2x00_write_nvram_data,
1499	.fw_dump		= qla2100_fw_dump,
1500	.beacon_on		= NULL,
1501	.beacon_off		= NULL,
1502	.beacon_blink		= NULL,
1503	.read_optrom		= qla2x00_read_optrom_data,
1504	.write_optrom		= qla2x00_write_optrom_data,
1505	.get_flash_version	= qla2x00_get_flash_version,
1506	.start_scsi		= qla2x00_start_scsi,
1507	.abort_isp		= qla2x00_abort_isp,
1508};
1509
1510static struct isp_operations qla2300_isp_ops = {
1511	.pci_config		= qla2300_pci_config,
1512	.reset_chip		= qla2x00_reset_chip,
1513	.chip_diag		= qla2x00_chip_diag,
1514	.config_rings		= qla2x00_config_rings,
1515	.reset_adapter		= qla2x00_reset_adapter,
1516	.nvram_config		= qla2x00_nvram_config,
1517	.update_fw_options	= qla2x00_update_fw_options,
1518	.load_risc		= qla2x00_load_risc,
1519	.pci_info_str		= qla2x00_pci_info_str,
1520	.fw_version_str		= qla2x00_fw_version_str,
1521	.intr_handler		= qla2300_intr_handler,
1522	.enable_intrs		= qla2x00_enable_intrs,
1523	.disable_intrs		= qla2x00_disable_intrs,
1524	.abort_command		= qla2x00_abort_command,
1525	.target_reset		= qla2x00_abort_target,
1526	.lun_reset		= qla2x00_lun_reset,
1527	.fabric_login		= qla2x00_login_fabric,
1528	.fabric_logout		= qla2x00_fabric_logout,
1529	.calc_req_entries	= qla2x00_calc_iocbs_32,
1530	.build_iocbs		= qla2x00_build_scsi_iocbs_32,
1531	.prep_ms_iocb		= qla2x00_prep_ms_iocb,
1532	.prep_ms_fdmi_iocb	= qla2x00_prep_ms_fdmi_iocb,
1533	.read_nvram		= qla2x00_read_nvram_data,
1534	.write_nvram		= qla2x00_write_nvram_data,
1535	.fw_dump		= qla2300_fw_dump,
1536	.beacon_on		= qla2x00_beacon_on,
1537	.beacon_off		= qla2x00_beacon_off,
1538	.beacon_blink		= qla2x00_beacon_blink,
1539	.read_optrom		= qla2x00_read_optrom_data,
1540	.write_optrom		= qla2x00_write_optrom_data,
1541	.get_flash_version	= qla2x00_get_flash_version,
1542	.start_scsi		= qla2x00_start_scsi,
1543	.abort_isp		= qla2x00_abort_isp,
1544};
1545
1546static struct isp_operations qla24xx_isp_ops = {
1547	.pci_config		= qla24xx_pci_config,
1548	.reset_chip		= qla24xx_reset_chip,
1549	.chip_diag		= qla24xx_chip_diag,
1550	.config_rings		= qla24xx_config_rings,
1551	.reset_adapter		= qla24xx_reset_adapter,
1552	.nvram_config		= qla24xx_nvram_config,
1553	.update_fw_options	= qla24xx_update_fw_options,
1554	.load_risc		= qla24xx_load_risc,
1555	.pci_info_str		= qla24xx_pci_info_str,
1556	.fw_version_str		= qla24xx_fw_version_str,
1557	.intr_handler		= qla24xx_intr_handler,
1558	.enable_intrs		= qla24xx_enable_intrs,
1559	.disable_intrs		= qla24xx_disable_intrs,
1560	.abort_command		= qla24xx_abort_command,
1561	.target_reset		= qla24xx_abort_target,
1562	.lun_reset		= qla24xx_lun_reset,
1563	.fabric_login		= qla24xx_login_fabric,
1564	.fabric_logout		= qla24xx_fabric_logout,
1565	.calc_req_entries	= NULL,
1566	.build_iocbs		= NULL,
1567	.prep_ms_iocb		= qla24xx_prep_ms_iocb,
1568	.prep_ms_fdmi_iocb	= qla24xx_prep_ms_fdmi_iocb,
1569	.read_nvram		= qla24xx_read_nvram_data,
1570	.write_nvram		= qla24xx_write_nvram_data,
1571	.fw_dump		= qla24xx_fw_dump,
1572	.beacon_on		= qla24xx_beacon_on,
1573	.beacon_off		= qla24xx_beacon_off,
1574	.beacon_blink		= qla24xx_beacon_blink,
1575	.read_optrom		= qla24xx_read_optrom_data,
1576	.write_optrom		= qla24xx_write_optrom_data,
1577	.get_flash_version	= qla24xx_get_flash_version,
1578	.start_scsi		= qla24xx_start_scsi,
1579	.abort_isp		= qla2x00_abort_isp,
1580};
1581
1582static struct isp_operations qla25xx_isp_ops = {
1583	.pci_config		= qla25xx_pci_config,
1584	.reset_chip		= qla24xx_reset_chip,
1585	.chip_diag		= qla24xx_chip_diag,
1586	.config_rings		= qla24xx_config_rings,
1587	.reset_adapter		= qla24xx_reset_adapter,
1588	.nvram_config		= qla24xx_nvram_config,
1589	.update_fw_options	= qla24xx_update_fw_options,
1590	.load_risc		= qla24xx_load_risc,
1591	.pci_info_str		= qla24xx_pci_info_str,
1592	.fw_version_str		= qla24xx_fw_version_str,
1593	.intr_handler		= qla24xx_intr_handler,
1594	.enable_intrs		= qla24xx_enable_intrs,
1595	.disable_intrs		= qla24xx_disable_intrs,
1596	.abort_command		= qla24xx_abort_command,
1597	.target_reset		= qla24xx_abort_target,
1598	.lun_reset		= qla24xx_lun_reset,
1599	.fabric_login		= qla24xx_login_fabric,
1600	.fabric_logout		= qla24xx_fabric_logout,
1601	.calc_req_entries	= NULL,
1602	.build_iocbs		= NULL,
1603	.prep_ms_iocb		= qla24xx_prep_ms_iocb,
1604	.prep_ms_fdmi_iocb	= qla24xx_prep_ms_fdmi_iocb,
1605	.read_nvram		= qla25xx_read_nvram_data,
1606	.write_nvram		= qla25xx_write_nvram_data,
1607	.fw_dump		= qla25xx_fw_dump,
1608	.beacon_on		= qla24xx_beacon_on,
1609	.beacon_off		= qla24xx_beacon_off,
1610	.beacon_blink		= qla24xx_beacon_blink,
1611	.read_optrom		= qla25xx_read_optrom_data,
1612	.write_optrom		= qla24xx_write_optrom_data,
1613	.get_flash_version	= qla24xx_get_flash_version,
1614	.start_scsi		= qla24xx_dif_start_scsi,
1615	.abort_isp		= qla2x00_abort_isp,
1616};
1617
1618static struct isp_operations qla81xx_isp_ops = {
1619	.pci_config		= qla25xx_pci_config,
1620	.reset_chip		= qla24xx_reset_chip,
1621	.chip_diag		= qla24xx_chip_diag,
1622	.config_rings		= qla24xx_config_rings,
1623	.reset_adapter		= qla24xx_reset_adapter,
1624	.nvram_config		= qla81xx_nvram_config,
1625	.update_fw_options	= qla81xx_update_fw_options,
1626	.load_risc		= qla81xx_load_risc,
1627	.pci_info_str		= qla24xx_pci_info_str,
1628	.fw_version_str		= qla24xx_fw_version_str,
1629	.intr_handler		= qla24xx_intr_handler,
1630	.enable_intrs		= qla24xx_enable_intrs,
1631	.disable_intrs		= qla24xx_disable_intrs,
1632	.abort_command		= qla24xx_abort_command,
1633	.target_reset		= qla24xx_abort_target,
1634	.lun_reset		= qla24xx_lun_reset,
1635	.fabric_login		= qla24xx_login_fabric,
1636	.fabric_logout		= qla24xx_fabric_logout,
1637	.calc_req_entries	= NULL,
1638	.build_iocbs		= NULL,
1639	.prep_ms_iocb		= qla24xx_prep_ms_iocb,
1640	.prep_ms_fdmi_iocb	= qla24xx_prep_ms_fdmi_iocb,
1641	.read_nvram		= NULL,
1642	.write_nvram		= NULL,
1643	.fw_dump		= qla81xx_fw_dump,
1644	.beacon_on		= qla24xx_beacon_on,
1645	.beacon_off		= qla24xx_beacon_off,
1646	.beacon_blink		= qla24xx_beacon_blink,
1647	.read_optrom		= qla25xx_read_optrom_data,
1648	.write_optrom		= qla24xx_write_optrom_data,
1649	.get_flash_version	= qla24xx_get_flash_version,
1650	.start_scsi		= qla24xx_dif_start_scsi,
1651	.abort_isp		= qla2x00_abort_isp,
1652};
1653
1654static struct isp_operations qla82xx_isp_ops = {
1655	.pci_config		= qla82xx_pci_config,
1656	.reset_chip		= qla82xx_reset_chip,
1657	.chip_diag		= qla24xx_chip_diag,
1658	.config_rings		= qla82xx_config_rings,
1659	.reset_adapter		= qla24xx_reset_adapter,
1660	.nvram_config		= qla81xx_nvram_config,
1661	.update_fw_options	= qla24xx_update_fw_options,
1662	.load_risc		= qla82xx_load_risc,
1663	.pci_info_str		= qla82xx_pci_info_str,
1664	.fw_version_str		= qla24xx_fw_version_str,
1665	.intr_handler		= qla82xx_intr_handler,
1666	.enable_intrs		= qla82xx_enable_intrs,
1667	.disable_intrs		= qla82xx_disable_intrs,
1668	.abort_command		= qla24xx_abort_command,
1669	.target_reset		= qla24xx_abort_target,
1670	.lun_reset		= qla24xx_lun_reset,
1671	.fabric_login		= qla24xx_login_fabric,
1672	.fabric_logout		= qla24xx_fabric_logout,
1673	.calc_req_entries	= NULL,
1674	.build_iocbs		= NULL,
1675	.prep_ms_iocb		= qla24xx_prep_ms_iocb,
1676	.prep_ms_fdmi_iocb	= qla24xx_prep_ms_fdmi_iocb,
1677	.read_nvram		= qla24xx_read_nvram_data,
1678	.write_nvram		= qla24xx_write_nvram_data,
1679	.fw_dump		= qla24xx_fw_dump,
1680	.beacon_on		= qla24xx_beacon_on,
1681	.beacon_off		= qla24xx_beacon_off,
1682	.beacon_blink		= qla24xx_beacon_blink,
1683	.read_optrom		= qla82xx_read_optrom_data,
1684	.write_optrom		= qla82xx_write_optrom_data,
1685	.get_flash_version	= qla24xx_get_flash_version,
1686	.start_scsi             = qla82xx_start_scsi,
1687	.abort_isp		= qla82xx_abort_isp,
1688};
1689
1690static inline void
1691qla2x00_set_isp_flags(struct qla_hw_data *ha)
1692{
1693	ha->device_type = DT_EXTENDED_IDS;
1694	switch (ha->pdev->device) {
1695	case PCI_DEVICE_ID_QLOGIC_ISP2100:
1696		ha->device_type |= DT_ISP2100;
1697		ha->device_type &= ~DT_EXTENDED_IDS;
1698		ha->fw_srisc_address = RISC_START_ADDRESS_2100;
1699		break;
1700	case PCI_DEVICE_ID_QLOGIC_ISP2200:
1701		ha->device_type |= DT_ISP2200;
1702		ha->device_type &= ~DT_EXTENDED_IDS;
1703		ha->fw_srisc_address = RISC_START_ADDRESS_2100;
1704		break;
1705	case PCI_DEVICE_ID_QLOGIC_ISP2300:
1706		ha->device_type |= DT_ISP2300;
1707		ha->device_type |= DT_ZIO_SUPPORTED;
1708		ha->fw_srisc_address = RISC_START_ADDRESS_2300;
1709		break;
1710	case PCI_DEVICE_ID_QLOGIC_ISP2312:
1711		ha->device_type |= DT_ISP2312;
1712		ha->device_type |= DT_ZIO_SUPPORTED;
1713		ha->fw_srisc_address = RISC_START_ADDRESS_2300;
1714		break;
1715	case PCI_DEVICE_ID_QLOGIC_ISP2322:
1716		ha->device_type |= DT_ISP2322;
1717		ha->device_type |= DT_ZIO_SUPPORTED;
1718		if (ha->pdev->subsystem_vendor == 0x1028 &&
1719		    ha->pdev->subsystem_device == 0x0170)
1720			ha->device_type |= DT_OEM_001;
1721		ha->fw_srisc_address = RISC_START_ADDRESS_2300;
1722		break;
1723	case PCI_DEVICE_ID_QLOGIC_ISP6312:
1724		ha->device_type |= DT_ISP6312;
1725		ha->fw_srisc_address = RISC_START_ADDRESS_2300;
1726		break;
1727	case PCI_DEVICE_ID_QLOGIC_ISP6322:
1728		ha->device_type |= DT_ISP6322;
1729		ha->fw_srisc_address = RISC_START_ADDRESS_2300;
1730		break;
1731	case PCI_DEVICE_ID_QLOGIC_ISP2422:
1732		ha->device_type |= DT_ISP2422;
1733		ha->device_type |= DT_ZIO_SUPPORTED;
1734		ha->device_type |= DT_FWI2;
1735		ha->device_type |= DT_IIDMA;
1736		ha->fw_srisc_address = RISC_START_ADDRESS_2400;
1737		break;
1738	case PCI_DEVICE_ID_QLOGIC_ISP2432:
1739		ha->device_type |= DT_ISP2432;
1740		ha->device_type |= DT_ZIO_SUPPORTED;
1741		ha->device_type |= DT_FWI2;
1742		ha->device_type |= DT_IIDMA;
1743		ha->fw_srisc_address = RISC_START_ADDRESS_2400;
1744		break;
1745	case PCI_DEVICE_ID_QLOGIC_ISP8432:
1746		ha->device_type |= DT_ISP8432;
1747		ha->device_type |= DT_ZIO_SUPPORTED;
1748		ha->device_type |= DT_FWI2;
1749		ha->device_type |= DT_IIDMA;
1750		ha->fw_srisc_address = RISC_START_ADDRESS_2400;
1751		break;
1752	case PCI_DEVICE_ID_QLOGIC_ISP5422:
1753		ha->device_type |= DT_ISP5422;
1754		ha->device_type |= DT_FWI2;
1755		ha->fw_srisc_address = RISC_START_ADDRESS_2400;
1756		break;
1757	case PCI_DEVICE_ID_QLOGIC_ISP5432:
1758		ha->device_type |= DT_ISP5432;
1759		ha->device_type |= DT_FWI2;
1760		ha->fw_srisc_address = RISC_START_ADDRESS_2400;
1761		break;
1762	case PCI_DEVICE_ID_QLOGIC_ISP2532:
1763		ha->device_type |= DT_ISP2532;
1764		ha->device_type |= DT_ZIO_SUPPORTED;
1765		ha->device_type |= DT_FWI2;
1766		ha->device_type |= DT_IIDMA;
1767		ha->fw_srisc_address = RISC_START_ADDRESS_2400;
1768		break;
1769	case PCI_DEVICE_ID_QLOGIC_ISP8001:
1770		ha->device_type |= DT_ISP8001;
1771		ha->device_type |= DT_ZIO_SUPPORTED;
1772		ha->device_type |= DT_FWI2;
1773		ha->device_type |= DT_IIDMA;
1774		ha->fw_srisc_address = RISC_START_ADDRESS_2400;
1775		break;
1776	case PCI_DEVICE_ID_QLOGIC_ISP8021:
1777		ha->device_type |= DT_ISP8021;
1778		ha->device_type |= DT_ZIO_SUPPORTED;
1779		ha->device_type |= DT_FWI2;
1780		ha->fw_srisc_address = RISC_START_ADDRESS_2400;
1781		/* Initialize 82XX ISP flags */
1782		qla82xx_init_flags(ha);
1783		break;
1784	}
1785
1786	if (IS_QLA82XX(ha))
1787		ha->port_no = !(ha->portnum & 1);
1788	else
1789		/* Get adapter physical port no from interrupt pin register. */
1790		pci_read_config_byte(ha->pdev, PCI_INTERRUPT_PIN, &ha->port_no);
1791
1792	if (ha->port_no & 1)
1793		ha->flags.port0 = 1;
1794	else
1795		ha->flags.port0 = 0;
1796}
1797
1798static int
1799qla2x00_iospace_config(struct qla_hw_data *ha)
1800{
1801	resource_size_t pio;
1802	uint16_t msix;
1803	int cpus;
1804
1805	if (IS_QLA82XX(ha))
1806		return qla82xx_iospace_config(ha);
1807
1808	if (pci_request_selected_regions(ha->pdev, ha->bars,
1809	    QLA2XXX_DRIVER_NAME)) {
1810		qla_printk(KERN_WARNING, ha,
1811		    "Failed to reserve PIO/MMIO regions (%s)\n",
1812		    pci_name(ha->pdev));
1813
1814		goto iospace_error_exit;
1815	}
1816	if (!(ha->bars & 1))
1817		goto skip_pio;
1818
1819	/* We only need PIO for Flash operations on ISP2312 v2 chips. */
1820	pio = pci_resource_start(ha->pdev, 0);
1821	if (pci_resource_flags(ha->pdev, 0) & IORESOURCE_IO) {
1822		if (pci_resource_len(ha->pdev, 0) < MIN_IOBASE_LEN) {
1823			qla_printk(KERN_WARNING, ha,
1824			    "Invalid PCI I/O region size (%s)...\n",
1825				pci_name(ha->pdev));
1826			pio = 0;
1827		}
1828	} else {
1829		qla_printk(KERN_WARNING, ha,
1830		    "region #0 not a PIO resource (%s)...\n",
1831		    pci_name(ha->pdev));
1832		pio = 0;
1833	}
1834	ha->pio_address = pio;
1835
1836skip_pio:
1837	/* Use MMIO operations for all accesses. */
1838	if (!(pci_resource_flags(ha->pdev, 1) & IORESOURCE_MEM)) {
1839		qla_printk(KERN_ERR, ha,
1840		    "region #1 not an MMIO resource (%s), aborting\n",
1841		    pci_name(ha->pdev));
1842		goto iospace_error_exit;
1843	}
1844	if (pci_resource_len(ha->pdev, 1) < MIN_IOBASE_LEN) {
1845		qla_printk(KERN_ERR, ha,
1846		    "Invalid PCI mem region size (%s), aborting\n",
1847			pci_name(ha->pdev));
1848		goto iospace_error_exit;
1849	}
1850
1851	ha->iobase = ioremap(pci_resource_start(ha->pdev, 1), MIN_IOBASE_LEN);
1852	if (!ha->iobase) {
1853		qla_printk(KERN_ERR, ha,
1854		    "cannot remap MMIO (%s), aborting\n", pci_name(ha->pdev));
1855
1856		goto iospace_error_exit;
1857	}
1858
1859	/* Determine queue resources */
1860	ha->max_req_queues = ha->max_rsp_queues = 1;
1861	if ((ql2xmaxqueues <= 1 && !ql2xmultique_tag) ||
1862		(ql2xmaxqueues > 1 && ql2xmultique_tag) ||
1863		(!IS_QLA25XX(ha) && !IS_QLA81XX(ha)))
1864		goto mqiobase_exit;
1865
1866	ha->mqiobase = ioremap(pci_resource_start(ha->pdev, 3),
1867			pci_resource_len(ha->pdev, 3));
1868	if (ha->mqiobase) {
1869		/* Read MSIX vector size of the board */
1870		pci_read_config_word(ha->pdev, QLA_PCI_MSIX_CONTROL, &msix);
1871		ha->msix_count = msix;
1872		/* Max queues are bounded by available msix vectors */
1873		/* queue 0 uses two msix vectors */
1874		if (ql2xmultique_tag) {
1875			cpus = num_online_cpus();
1876			ha->max_rsp_queues = (ha->msix_count - 1 > cpus) ?
1877				(cpus + 1) : (ha->msix_count - 1);
1878			ha->max_req_queues = 2;
1879		} else if (ql2xmaxqueues > 1) {
1880			ha->max_req_queues = ql2xmaxqueues > QLA_MQ_SIZE ?
1881						QLA_MQ_SIZE : ql2xmaxqueues;
1882			DEBUG2(qla_printk(KERN_INFO, ha, "QoS mode set, max no"
1883			" of request queues:%d\n", ha->max_req_queues));
1884		}
1885		qla_printk(KERN_INFO, ha,
1886			"MSI-X vector count: %d\n", msix);
1887	} else
1888		qla_printk(KERN_INFO, ha, "BAR 3 not enabled\n");
1889
1890mqiobase_exit:
1891	ha->msix_count = ha->max_rsp_queues + 1;
1892	return (0);
1893
1894iospace_error_exit:
1895	return (-ENOMEM);
1896}
1897
1898static void
1899qla2xxx_scan_start(struct Scsi_Host *shost)
1900{
1901	scsi_qla_host_t *vha = shost_priv(shost);
1902
1903	if (vha->hw->flags.running_gold_fw)
1904		return;
1905
1906	set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
1907	set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
1908	set_bit(RSCN_UPDATE, &vha->dpc_flags);
1909	set_bit(NPIV_CONFIG_NEEDED, &vha->dpc_flags);
1910}
1911
1912static int
1913qla2xxx_scan_finished(struct Scsi_Host *shost, unsigned long time)
1914{
1915	scsi_qla_host_t *vha = shost_priv(shost);
1916
1917	if (!vha->host)
1918		return 1;
1919	if (time > vha->hw->loop_reset_delay * HZ)
1920		return 1;
1921
1922	return atomic_read(&vha->loop_state) == LOOP_READY;
1923}
1924
1925/*
1926 * PCI driver interface
1927 */
1928static int __devinit
1929qla2x00_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
1930{
1931	int	ret = -ENODEV;
1932	struct Scsi_Host *host;
1933	scsi_qla_host_t *base_vha = NULL;
1934	struct qla_hw_data *ha;
1935	char pci_info[30];
1936	char fw_str[30];
1937	struct scsi_host_template *sht;
1938	int bars, max_id, mem_only = 0;
1939	uint16_t req_length = 0, rsp_length = 0;
1940	struct req_que *req = NULL;
1941	struct rsp_que *rsp = NULL;
1942
1943	bars = pci_select_bars(pdev, IORESOURCE_MEM | IORESOURCE_IO);
1944	sht = &qla2xxx_driver_template;
1945	if (pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2422 ||
1946	    pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2432 ||
1947	    pdev->device == PCI_DEVICE_ID_QLOGIC_ISP8432 ||
1948	    pdev->device == PCI_DEVICE_ID_QLOGIC_ISP5422 ||
1949	    pdev->device == PCI_DEVICE_ID_QLOGIC_ISP5432 ||
1950	    pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2532 ||
1951	    pdev->device == PCI_DEVICE_ID_QLOGIC_ISP8001 ||
1952	    pdev->device == PCI_DEVICE_ID_QLOGIC_ISP8021) {
1953		bars = pci_select_bars(pdev, IORESOURCE_MEM);
1954		mem_only = 1;
1955	}
1956
1957	if (mem_only) {
1958		if (pci_enable_device_mem(pdev))
1959			goto probe_out;
1960	} else {
1961		if (pci_enable_device(pdev))
1962			goto probe_out;
1963	}
1964
1965	/* This may fail but that's ok */
1966	pci_enable_pcie_error_reporting(pdev);
1967
1968	ha = kzalloc(sizeof(struct qla_hw_data), GFP_KERNEL);
1969	if (!ha) {
1970		DEBUG(printk("Unable to allocate memory for ha\n"));
1971		goto probe_out;
1972	}
1973	ha->pdev = pdev;
1974
1975	/* Clear our data area */
1976	ha->bars = bars;
1977	ha->mem_only = mem_only;
1978	spin_lock_init(&ha->hardware_lock);
1979
1980	/* Set ISP-type information. */
1981	qla2x00_set_isp_flags(ha);
1982
1983	/* Set EEH reset type to fundamental if required by hba */
1984	if ( IS_QLA24XX(ha) || IS_QLA25XX(ha) || IS_QLA81XX(ha)) {
1985		pdev->needs_freset = 1;
1986	}
1987
1988	/* Configure PCI I/O space */
1989	ret = qla2x00_iospace_config(ha);
1990	if (ret)
1991		goto probe_hw_failed;
1992
1993	qla_printk(KERN_INFO, ha,
1994	    "Found an ISP%04X, irq %d, iobase 0x%p\n", pdev->device, pdev->irq,
1995	    ha->iobase);
1996
1997	ha->prev_topology = 0;
1998	ha->init_cb_size = sizeof(init_cb_t);
1999	ha->link_data_rate = PORT_SPEED_UNKNOWN;
2000	ha->optrom_size = OPTROM_SIZE_2300;
2001
2002	/* Assign ISP specific operations. */
2003	max_id = MAX_TARGETS_2200;
2004	if (IS_QLA2100(ha)) {
2005		max_id = MAX_TARGETS_2100;
2006		ha->mbx_count = MAILBOX_REGISTER_COUNT_2100;
2007		req_length = REQUEST_ENTRY_CNT_2100;
2008		rsp_length = RESPONSE_ENTRY_CNT_2100;
2009		ha->max_loop_id = SNS_LAST_LOOP_ID_2100;
2010		ha->gid_list_info_size = 4;
2011		ha->flash_conf_off = ~0;
2012		ha->flash_data_off = ~0;
2013		ha->nvram_conf_off = ~0;
2014		ha->nvram_data_off = ~0;
2015		ha->isp_ops = &qla2100_isp_ops;
2016	} else if (IS_QLA2200(ha)) {
2017		ha->mbx_count = MAILBOX_REGISTER_COUNT;
2018		req_length = REQUEST_ENTRY_CNT_2200;
2019		rsp_length = RESPONSE_ENTRY_CNT_2100;
2020		ha->max_loop_id = SNS_LAST_LOOP_ID_2100;
2021		ha->gid_list_info_size = 4;
2022		ha->flash_conf_off = ~0;
2023		ha->flash_data_off = ~0;
2024		ha->nvram_conf_off = ~0;
2025		ha->nvram_data_off = ~0;
2026		ha->isp_ops = &qla2100_isp_ops;
2027	} else if (IS_QLA23XX(ha)) {
2028		ha->mbx_count = MAILBOX_REGISTER_COUNT;
2029		req_length = REQUEST_ENTRY_CNT_2200;
2030		rsp_length = RESPONSE_ENTRY_CNT_2300;
2031		ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
2032		ha->gid_list_info_size = 6;
2033		if (IS_QLA2322(ha) || IS_QLA6322(ha))
2034			ha->optrom_size = OPTROM_SIZE_2322;
2035		ha->flash_conf_off = ~0;
2036		ha->flash_data_off = ~0;
2037		ha->nvram_conf_off = ~0;
2038		ha->nvram_data_off = ~0;
2039		ha->isp_ops = &qla2300_isp_ops;
2040	} else if (IS_QLA24XX_TYPE(ha)) {
2041		ha->mbx_count = MAILBOX_REGISTER_COUNT;
2042		req_length = REQUEST_ENTRY_CNT_24XX;
2043		rsp_length = RESPONSE_ENTRY_CNT_2300;
2044		ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
2045		ha->init_cb_size = sizeof(struct mid_init_cb_24xx);
2046		ha->gid_list_info_size = 8;
2047		ha->optrom_size = OPTROM_SIZE_24XX;
2048		ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA24XX;
2049		ha->isp_ops = &qla24xx_isp_ops;
2050		ha->flash_conf_off = FARX_ACCESS_FLASH_CONF;
2051		ha->flash_data_off = FARX_ACCESS_FLASH_DATA;
2052		ha->nvram_conf_off = FARX_ACCESS_NVRAM_CONF;
2053		ha->nvram_data_off = FARX_ACCESS_NVRAM_DATA;
2054	} else if (IS_QLA25XX(ha)) {
2055		ha->mbx_count = MAILBOX_REGISTER_COUNT;
2056		req_length = REQUEST_ENTRY_CNT_24XX;
2057		rsp_length = RESPONSE_ENTRY_CNT_2300;
2058		ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
2059		ha->init_cb_size = sizeof(struct mid_init_cb_24xx);
2060		ha->gid_list_info_size = 8;
2061		ha->optrom_size = OPTROM_SIZE_25XX;
2062		ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA25XX;
2063		ha->isp_ops = &qla25xx_isp_ops;
2064		ha->flash_conf_off = FARX_ACCESS_FLASH_CONF;
2065		ha->flash_data_off = FARX_ACCESS_FLASH_DATA;
2066		ha->nvram_conf_off = FARX_ACCESS_NVRAM_CONF;
2067		ha->nvram_data_off = FARX_ACCESS_NVRAM_DATA;
2068	} else if (IS_QLA81XX(ha)) {
2069		ha->mbx_count = MAILBOX_REGISTER_COUNT;
2070		req_length = REQUEST_ENTRY_CNT_24XX;
2071		rsp_length = RESPONSE_ENTRY_CNT_2300;
2072		ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
2073		ha->init_cb_size = sizeof(struct mid_init_cb_81xx);
2074		ha->gid_list_info_size = 8;
2075		ha->optrom_size = OPTROM_SIZE_81XX;
2076		ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA25XX;
2077		ha->isp_ops = &qla81xx_isp_ops;
2078		ha->flash_conf_off = FARX_ACCESS_FLASH_CONF_81XX;
2079		ha->flash_data_off = FARX_ACCESS_FLASH_DATA_81XX;
2080		ha->nvram_conf_off = ~0;
2081		ha->nvram_data_off = ~0;
2082	} else if (IS_QLA82XX(ha)) {
2083		ha->mbx_count = MAILBOX_REGISTER_COUNT;
2084		req_length = REQUEST_ENTRY_CNT_82XX;
2085		rsp_length = RESPONSE_ENTRY_CNT_82XX;
2086		ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
2087		ha->init_cb_size = sizeof(struct mid_init_cb_81xx);
2088		ha->gid_list_info_size = 8;
2089		ha->optrom_size = OPTROM_SIZE_82XX;
2090		ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA25XX;
2091		ha->isp_ops = &qla82xx_isp_ops;
2092		ha->flash_conf_off = FARX_ACCESS_FLASH_CONF;
2093		ha->flash_data_off = FARX_ACCESS_FLASH_DATA;
2094		ha->nvram_conf_off = FARX_ACCESS_NVRAM_CONF;
2095		ha->nvram_data_off = FARX_ACCESS_NVRAM_DATA;
2096	}
2097
2098	mutex_init(&ha->vport_lock);
2099	init_completion(&ha->mbx_cmd_comp);
2100	complete(&ha->mbx_cmd_comp);
2101	init_completion(&ha->mbx_intr_comp);
2102	init_completion(&ha->dcbx_comp);
2103
2104	set_bit(0, (unsigned long *) ha->vp_idx_map);
2105
2106	qla2x00_config_dma_addressing(ha);
2107	ret = qla2x00_mem_alloc(ha, req_length, rsp_length, &req, &rsp);
2108	if (!ret) {
2109		qla_printk(KERN_WARNING, ha,
2110		    "[ERROR] Failed to allocate memory for adapter\n");
2111
2112		goto probe_hw_failed;
2113	}
2114
2115	req->max_q_depth = MAX_Q_DEPTH;
2116	if (ql2xmaxqdepth != 0 && ql2xmaxqdepth <= 0xffffU)
2117		req->max_q_depth = ql2xmaxqdepth;
2118
2119
2120	base_vha = qla2x00_create_host(sht, ha);
2121	if (!base_vha) {
2122		qla_printk(KERN_WARNING, ha,
2123		    "[ERROR] Failed to allocate memory for scsi_host\n");
2124
2125		ret = -ENOMEM;
2126		qla2x00_mem_free(ha);
2127		qla2x00_free_req_que(ha, req);
2128		qla2x00_free_rsp_que(ha, rsp);
2129		goto probe_hw_failed;
2130	}
2131
2132	pci_set_drvdata(pdev, base_vha);
2133
2134	host = base_vha->host;
2135	base_vha->req = req;
2136	host->can_queue = req->length + 128;
2137	if (IS_QLA2XXX_MIDTYPE(ha))
2138		base_vha->mgmt_svr_loop_id = 10 + base_vha->vp_idx;
2139	else
2140		base_vha->mgmt_svr_loop_id = MANAGEMENT_SERVER +
2141						base_vha->vp_idx;
2142	if (IS_QLA2100(ha))
2143		host->sg_tablesize = 32;
2144	host->max_id = max_id;
2145	host->this_id = 255;
2146	host->cmd_per_lun = 3;
2147	host->unique_id = host->host_no;
2148	if ((IS_QLA25XX(ha) || IS_QLA81XX(ha)) && ql2xenabledif)
2149		host->max_cmd_len = 32;
2150	else
2151		host->max_cmd_len = MAX_CMDSZ;
2152	host->max_channel = MAX_BUSES - 1;
2153	host->max_lun = MAX_LUNS;
2154	host->transportt = qla2xxx_transport_template;
2155	sht->vendor_id = (SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_QLOGIC);
2156
2157	/* Set up the irqs */
2158	ret = qla2x00_request_irqs(ha, rsp);
2159	if (ret)
2160		goto probe_init_failed;
2161
2162	pci_save_state(pdev);
2163
2164	/* Alloc arrays of request and response ring ptrs */
2165que_init:
2166	if (!qla2x00_alloc_queues(ha)) {
2167		qla_printk(KERN_WARNING, ha,
2168		"[ERROR] Failed to allocate memory for queue"
2169		" pointers\n");
2170		goto probe_init_failed;
2171	}
2172
2173	ha->rsp_q_map[0] = rsp;
2174	ha->req_q_map[0] = req;
2175	rsp->req = req;
2176	req->rsp = rsp;
2177	set_bit(0, ha->req_qid_map);
2178	set_bit(0, ha->rsp_qid_map);
2179	/* FWI2-capable only. */
2180	req->req_q_in = &ha->iobase->isp24.req_q_in;
2181	req->req_q_out = &ha->iobase->isp24.req_q_out;
2182	rsp->rsp_q_in = &ha->iobase->isp24.rsp_q_in;
2183	rsp->rsp_q_out = &ha->iobase->isp24.rsp_q_out;
2184	if (ha->mqenable) {
2185		req->req_q_in = &ha->mqiobase->isp25mq.req_q_in;
2186		req->req_q_out = &ha->mqiobase->isp25mq.req_q_out;
2187		rsp->rsp_q_in = &ha->mqiobase->isp25mq.rsp_q_in;
2188		rsp->rsp_q_out =  &ha->mqiobase->isp25mq.rsp_q_out;
2189	}
2190
2191	if (IS_QLA82XX(ha)) {
2192		req->req_q_out = &ha->iobase->isp82.req_q_out[0];
2193		rsp->rsp_q_in = &ha->iobase->isp82.rsp_q_in[0];
2194		rsp->rsp_q_out = &ha->iobase->isp82.rsp_q_out[0];
2195	}
2196
2197	if (qla2x00_initialize_adapter(base_vha)) {
2198		qla_printk(KERN_WARNING, ha,
2199		    "Failed to initialize adapter\n");
2200
2201		DEBUG2(printk("scsi(%ld): Failed to initialize adapter - "
2202		    "Adapter flags %x.\n",
2203		    base_vha->host_no, base_vha->device_flags));
2204
2205		if (IS_QLA82XX(ha)) {
2206			qla82xx_idc_lock(ha);
2207			qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2208				QLA82XX_DEV_FAILED);
2209			qla82xx_idc_unlock(ha);
2210			qla_printk(KERN_INFO, ha, "HW State: FAILED\n");
2211		}
2212
2213		ret = -ENODEV;
2214		goto probe_failed;
2215	}
2216
2217	if (ha->mqenable) {
2218		if (qla25xx_setup_mode(base_vha)) {
2219			qla_printk(KERN_WARNING, ha,
2220				"Can't create queues, falling back to single"
2221				" queue mode\n");
2222			goto que_init;
2223		}
2224	}
2225
2226	if (ha->flags.running_gold_fw)
2227		goto skip_dpc;
2228
2229	/*
2230	 * Startup the kernel thread for this host adapter
2231	 */
2232	ha->dpc_thread = kthread_create(qla2x00_do_dpc, ha,
2233			"%s_dpc", base_vha->host_str);
2234	if (IS_ERR(ha->dpc_thread)) {
2235		qla_printk(KERN_WARNING, ha,
2236		    "Unable to start DPC thread!\n");
2237		ret = PTR_ERR(ha->dpc_thread);
2238		goto probe_failed;
2239	}
2240
2241skip_dpc:
2242	list_add_tail(&base_vha->list, &ha->vp_list);
2243	base_vha->host->irq = ha->pdev->irq;
2244
2245	/* Initialized the timer */
2246	qla2x00_start_timer(base_vha, qla2x00_timer, WATCH_INTERVAL);
2247
2248	DEBUG2(printk("DEBUG: detect hba %ld at address = %p\n",
2249	    base_vha->host_no, ha));
2250
2251	if ((IS_QLA25XX(ha) || IS_QLA81XX(ha)) && ql2xenabledif) {
2252		if (ha->fw_attributes & BIT_4) {
2253			base_vha->flags.difdix_supported = 1;
2254			DEBUG18(qla_printk(KERN_INFO, ha,
2255			    "Registering for DIF/DIX type 1 and 3"
2256			    " protection.\n"));
2257			scsi_host_set_prot(host,
2258			    SHOST_DIF_TYPE1_PROTECTION
2259			    | SHOST_DIF_TYPE2_PROTECTION
2260			    | SHOST_DIF_TYPE3_PROTECTION
2261			    | SHOST_DIX_TYPE1_PROTECTION
2262			    | SHOST_DIX_TYPE2_PROTECTION
2263			    | SHOST_DIX_TYPE3_PROTECTION);
2264			scsi_host_set_guard(host, SHOST_DIX_GUARD_CRC);
2265		} else
2266			base_vha->flags.difdix_supported = 0;
2267	}
2268
2269	ha->isp_ops->enable_intrs(ha);
2270
2271	ret = scsi_add_host(host, &pdev->dev);
2272	if (ret)
2273		goto probe_failed;
2274
2275	base_vha->flags.init_done = 1;
2276	base_vha->flags.online = 1;
2277
2278	scsi_scan_host(host);
2279
2280	qla2x00_alloc_sysfs_attr(base_vha);
2281
2282	qla2x00_init_host_attr(base_vha);
2283
2284	qla2x00_dfs_setup(base_vha);
2285
2286	qla_printk(KERN_INFO, ha, "\n"
2287	    " QLogic Fibre Channel HBA Driver: %s\n"
2288	    "  QLogic %s - %s\n"
2289	    "  ISP%04X: %s @ %s hdma%c, host#=%ld, fw=%s\n",
2290	    qla2x00_version_str, ha->model_number,
2291	    ha->model_desc ? ha->model_desc : "", pdev->device,
2292	    ha->isp_ops->pci_info_str(base_vha, pci_info), pci_name(pdev),
2293	    ha->flags.enable_64bit_addressing ? '+' : '-', base_vha->host_no,
2294	    ha->isp_ops->fw_version_str(base_vha, fw_str));
2295
2296	return 0;
2297
2298probe_init_failed:
2299	qla2x00_free_req_que(ha, req);
2300	qla2x00_free_rsp_que(ha, rsp);
2301	ha->max_req_queues = ha->max_rsp_queues = 0;
2302
2303probe_failed:
2304	if (base_vha->timer_active)
2305		qla2x00_stop_timer(base_vha);
2306	base_vha->flags.online = 0;
2307	if (ha->dpc_thread) {
2308		struct task_struct *t = ha->dpc_thread;
2309
2310		ha->dpc_thread = NULL;
2311		kthread_stop(t);
2312	}
2313
2314	qla2x00_free_device(base_vha);
2315
2316	scsi_host_put(base_vha->host);
2317
2318probe_hw_failed:
2319	if (IS_QLA82XX(ha)) {
2320		qla82xx_idc_lock(ha);
2321		qla82xx_clear_drv_active(ha);
2322		qla82xx_idc_unlock(ha);
2323		iounmap((device_reg_t __iomem *)ha->nx_pcibase);
2324		if (!ql2xdbwr)
2325			iounmap((device_reg_t __iomem *)ha->nxdb_wr_ptr);
2326	} else {
2327		if (ha->iobase)
2328			iounmap(ha->iobase);
2329	}
2330	pci_release_selected_regions(ha->pdev, ha->bars);
2331	kfree(ha);
2332	ha = NULL;
2333
2334probe_out:
2335	pci_disable_device(pdev);
2336	return ret;
2337}
2338
2339static void
2340qla2x00_remove_one(struct pci_dev *pdev)
2341{
2342	scsi_qla_host_t *base_vha, *vha;
2343	struct qla_hw_data  *ha;
2344	unsigned long flags;
2345
2346	base_vha = pci_get_drvdata(pdev);
2347	ha = base_vha->hw;
2348
2349	spin_lock_irqsave(&ha->vport_slock, flags);
2350	list_for_each_entry(vha, &ha->vp_list, list) {
2351		atomic_inc(&vha->vref_count);
2352
2353		if (vha && vha->fc_vport) {
2354			spin_unlock_irqrestore(&ha->vport_slock, flags);
2355
2356			fc_vport_terminate(vha->fc_vport);
2357
2358			spin_lock_irqsave(&ha->vport_slock, flags);
2359		}
2360
2361		atomic_dec(&vha->vref_count);
2362	}
2363	spin_unlock_irqrestore(&ha->vport_slock, flags);
2364
2365	set_bit(UNLOADING, &base_vha->dpc_flags);
2366
2367	qla2x00_abort_all_cmds(base_vha, DID_NO_CONNECT << 16);
2368
2369	qla2x00_dfs_remove(base_vha);
2370
2371	qla84xx_put_chip(base_vha);
2372
2373	/* Disable timer */
2374	if (base_vha->timer_active)
2375		qla2x00_stop_timer(base_vha);
2376
2377	base_vha->flags.online = 0;
2378
2379	/* Flush the work queue and remove it */
2380	if (ha->wq) {
2381		flush_workqueue(ha->wq);
2382		destroy_workqueue(ha->wq);
2383		ha->wq = NULL;
2384	}
2385
2386	/* Kill the kernel thread for this host */
2387	if (ha->dpc_thread) {
2388		struct task_struct *t = ha->dpc_thread;
2389
2390		/*
2391		 * qla2xxx_wake_dpc checks for ->dpc_thread
2392		 * so we need to zero it out.
2393		 */
2394		ha->dpc_thread = NULL;
2395		kthread_stop(t);
2396	}
2397
2398	qla2x00_free_sysfs_attr(base_vha);
2399
2400	fc_remove_host(base_vha->host);
2401
2402	scsi_remove_host(base_vha->host);
2403
2404	qla2x00_free_device(base_vha);
2405
2406	scsi_host_put(base_vha->host);
2407
2408	if (IS_QLA82XX(ha)) {
2409		qla82xx_idc_lock(ha);
2410		qla82xx_clear_drv_active(ha);
2411		qla82xx_idc_unlock(ha);
2412
2413		iounmap((device_reg_t __iomem *)ha->nx_pcibase);
2414		if (!ql2xdbwr)
2415			iounmap((device_reg_t __iomem *)ha->nxdb_wr_ptr);
2416	} else {
2417		if (ha->iobase)
2418			iounmap(ha->iobase);
2419
2420		if (ha->mqiobase)
2421			iounmap(ha->mqiobase);
2422	}
2423
2424	pci_release_selected_regions(ha->pdev, ha->bars);
2425	kfree(ha);
2426	ha = NULL;
2427
2428	pci_disable_pcie_error_reporting(pdev);
2429
2430	pci_disable_device(pdev);
2431	pci_set_drvdata(pdev, NULL);
2432}
2433
2434static void
2435qla2x00_free_device(scsi_qla_host_t *vha)
2436{
2437	struct qla_hw_data *ha = vha->hw;
2438
2439	qla2x00_abort_all_cmds(vha, DID_NO_CONNECT << 16);
2440
2441	/* Disable timer */
2442	if (vha->timer_active)
2443		qla2x00_stop_timer(vha);
2444
2445	/* Kill the kernel thread for this host */
2446	if (ha->dpc_thread) {
2447		struct task_struct *t = ha->dpc_thread;
2448
2449		/*
2450		 * qla2xxx_wake_dpc checks for ->dpc_thread
2451		 * so we need to zero it out.
2452		 */
2453		ha->dpc_thread = NULL;
2454		kthread_stop(t);
2455	}
2456
2457	qla25xx_delete_queues(vha);
2458
2459	if (ha->flags.fce_enabled)
2460		qla2x00_disable_fce_trace(vha, NULL, NULL);
2461
2462	if (ha->eft)
2463		qla2x00_disable_eft_trace(vha);
2464
2465	/* Stop currently executing firmware. */
2466	qla2x00_try_to_stop_firmware(vha);
2467
2468	vha->flags.online = 0;
2469
2470	/* turn-off interrupts on the card */
2471	if (ha->interrupts_on) {
2472		vha->flags.init_done = 0;
2473		ha->isp_ops->disable_intrs(ha);
2474	}
2475
2476	qla2x00_free_irqs(vha);
2477
2478	qla2x00_free_fcports(vha);
2479
2480	qla2x00_mem_free(ha);
2481
2482	qla2x00_free_queues(ha);
2483}
2484
2485void qla2x00_free_fcports(struct scsi_qla_host *vha)
2486{
2487	fc_port_t *fcport, *tfcport;
2488
2489	list_for_each_entry_safe(fcport, tfcport, &vha->vp_fcports, list) {
2490		list_del(&fcport->list);
2491		kfree(fcport);
2492		fcport = NULL;
2493	}
2494}
2495
2496static inline void
2497qla2x00_schedule_rport_del(struct scsi_qla_host *vha, fc_port_t *fcport,
2498    int defer)
2499{
2500	struct fc_rport *rport;
2501	scsi_qla_host_t *base_vha;
2502
2503	if (!fcport->rport)
2504		return;
2505
2506	rport = fcport->rport;
2507	if (defer) {
2508		base_vha = pci_get_drvdata(vha->hw->pdev);
2509		spin_lock_irq(vha->host->host_lock);
2510		fcport->drport = rport;
2511		spin_unlock_irq(vha->host->host_lock);
2512		set_bit(FCPORT_UPDATE_NEEDED, &base_vha->dpc_flags);
2513		qla2xxx_wake_dpc(base_vha);
2514	} else
2515		fc_remote_port_delete(rport);
2516}
2517
2518/*
2519 * qla2x00_mark_device_lost Updates fcport state when device goes offline.
2520 *
2521 * Input: ha = adapter block pointer.  fcport = port structure pointer.
2522 *
2523 * Return: None.
2524 *
2525 * Context:
2526 */
2527void qla2x00_mark_device_lost(scsi_qla_host_t *vha, fc_port_t *fcport,
2528    int do_login, int defer)
2529{
2530	if (atomic_read(&fcport->state) == FCS_ONLINE &&
2531	    vha->vp_idx == fcport->vp_idx) {
2532		atomic_set(&fcport->state, FCS_DEVICE_LOST);
2533		qla2x00_schedule_rport_del(vha, fcport, defer);
2534	}
2535	/*
2536	 * We may need to retry the login, so don't change the state of the
2537	 * port but do the retries.
2538	 */
2539	if (atomic_read(&fcport->state) != FCS_DEVICE_DEAD)
2540		atomic_set(&fcport->state, FCS_DEVICE_LOST);
2541
2542	if (!do_login)
2543		return;
2544
2545	if (fcport->login_retry == 0) {
2546		fcport->login_retry = vha->hw->login_retry_count;
2547		set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
2548
2549		DEBUG(printk("scsi(%ld): Port login retry: "
2550		    "%02x%02x%02x%02x%02x%02x%02x%02x, "
2551		    "id = 0x%04x retry cnt=%d\n",
2552		    vha->host_no,
2553		    fcport->port_name[0],
2554		    fcport->port_name[1],
2555		    fcport->port_name[2],
2556		    fcport->port_name[3],
2557		    fcport->port_name[4],
2558		    fcport->port_name[5],
2559		    fcport->port_name[6],
2560		    fcport->port_name[7],
2561		    fcport->loop_id,
2562		    fcport->login_retry));
2563	}
2564}
2565
2566/*
2567 * qla2x00_mark_all_devices_lost
2568 *	Updates fcport state when device goes offline.
2569 *
2570 * Input:
2571 *	ha = adapter block pointer.
2572 *	fcport = port structure pointer.
2573 *
2574 * Return:
2575 *	None.
2576 *
2577 * Context:
2578 */
2579void
2580qla2x00_mark_all_devices_lost(scsi_qla_host_t *vha, int defer)
2581{
2582	fc_port_t *fcport;
2583
2584	list_for_each_entry(fcport, &vha->vp_fcports, list) {
2585		if (vha->vp_idx != 0 && vha->vp_idx != fcport->vp_idx)
2586			continue;
2587
2588		/*
2589		 * No point in marking the device as lost, if the device is
2590		 * already DEAD.
2591		 */
2592		if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD)
2593			continue;
2594		if (atomic_read(&fcport->state) == FCS_ONLINE) {
2595			if (defer)
2596				qla2x00_schedule_rport_del(vha, fcport, defer);
2597			else if (vha->vp_idx == fcport->vp_idx)
2598				qla2x00_schedule_rport_del(vha, fcport, defer);
2599		}
2600		atomic_set(&fcport->state, FCS_DEVICE_LOST);
2601	}
2602}
2603
2604/*
2605* qla2x00_mem_alloc
2606*      Allocates adapter memory.
2607*
2608* Returns:
2609*      0  = success.
2610*      !0  = failure.
2611*/
2612static int
2613qla2x00_mem_alloc(struct qla_hw_data *ha, uint16_t req_len, uint16_t rsp_len,
2614	struct req_que **req, struct rsp_que **rsp)
2615{
2616	char	name[16];
2617
2618	ha->init_cb = dma_alloc_coherent(&ha->pdev->dev, ha->init_cb_size,
2619		&ha->init_cb_dma, GFP_KERNEL);
2620	if (!ha->init_cb)
2621		goto fail;
2622
2623	ha->gid_list = dma_alloc_coherent(&ha->pdev->dev, GID_LIST_SIZE,
2624		&ha->gid_list_dma, GFP_KERNEL);
2625	if (!ha->gid_list)
2626		goto fail_free_init_cb;
2627
2628	ha->srb_mempool = mempool_create_slab_pool(SRB_MIN_REQ, srb_cachep);
2629	if (!ha->srb_mempool)
2630		goto fail_free_gid_list;
2631
2632	if (IS_QLA82XX(ha)) {
2633		/* Allocate cache for CT6 Ctx. */
2634		if (!ctx_cachep) {
2635			ctx_cachep = kmem_cache_create("qla2xxx_ctx",
2636				sizeof(struct ct6_dsd), 0,
2637				SLAB_HWCACHE_ALIGN, NULL);
2638			if (!ctx_cachep)
2639				goto fail_free_gid_list;
2640		}
2641		ha->ctx_mempool = mempool_create_slab_pool(SRB_MIN_REQ,
2642			ctx_cachep);
2643		if (!ha->ctx_mempool)
2644			goto fail_free_srb_mempool;
2645	}
2646
2647	/* Get memory for cached NVRAM */
2648	ha->nvram = kzalloc(MAX_NVRAM_SIZE, GFP_KERNEL);
2649	if (!ha->nvram)
2650		goto fail_free_ctx_mempool;
2651
2652	snprintf(name, sizeof(name), "%s_%d", QLA2XXX_DRIVER_NAME,
2653		ha->pdev->device);
2654	ha->s_dma_pool = dma_pool_create(name, &ha->pdev->dev,
2655		DMA_POOL_SIZE, 8, 0);
2656	if (!ha->s_dma_pool)
2657		goto fail_free_nvram;
2658
2659	if (IS_QLA82XX(ha) || ql2xenabledif) {
2660		ha->dl_dma_pool = dma_pool_create(name, &ha->pdev->dev,
2661			DSD_LIST_DMA_POOL_SIZE, 8, 0);
2662		if (!ha->dl_dma_pool) {
2663			qla_printk(KERN_WARNING, ha,
2664			    "Memory Allocation failed - dl_dma_pool\n");
2665			goto fail_s_dma_pool;
2666		}
2667
2668		ha->fcp_cmnd_dma_pool = dma_pool_create(name, &ha->pdev->dev,
2669			FCP_CMND_DMA_POOL_SIZE, 8, 0);
2670		if (!ha->fcp_cmnd_dma_pool) {
2671			qla_printk(KERN_WARNING, ha,
2672			    "Memory Allocation failed - fcp_cmnd_dma_pool\n");
2673			goto fail_dl_dma_pool;
2674		}
2675	}
2676
2677	/* Allocate memory for SNS commands */
2678	if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
2679	/* Get consistent memory allocated for SNS commands */
2680		ha->sns_cmd = dma_alloc_coherent(&ha->pdev->dev,
2681		sizeof(struct sns_cmd_pkt), &ha->sns_cmd_dma, GFP_KERNEL);
2682		if (!ha->sns_cmd)
2683			goto fail_dma_pool;
2684	} else {
2685	/* Get consistent memory allocated for MS IOCB */
2686		ha->ms_iocb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
2687			&ha->ms_iocb_dma);
2688		if (!ha->ms_iocb)
2689			goto fail_dma_pool;
2690	/* Get consistent memory allocated for CT SNS commands */
2691		ha->ct_sns = dma_alloc_coherent(&ha->pdev->dev,
2692			sizeof(struct ct_sns_pkt), &ha->ct_sns_dma, GFP_KERNEL);
2693		if (!ha->ct_sns)
2694			goto fail_free_ms_iocb;
2695	}
2696
2697	/* Allocate memory for request ring */
2698	*req = kzalloc(sizeof(struct req_que), GFP_KERNEL);
2699	if (!*req) {
2700		DEBUG(printk("Unable to allocate memory for req\n"));
2701		goto fail_req;
2702	}
2703	(*req)->length = req_len;
2704	(*req)->ring = dma_alloc_coherent(&ha->pdev->dev,
2705		((*req)->length + 1) * sizeof(request_t),
2706		&(*req)->dma, GFP_KERNEL);
2707	if (!(*req)->ring) {
2708		DEBUG(printk("Unable to allocate memory for req_ring\n"));
2709		goto fail_req_ring;
2710	}
2711	/* Allocate memory for response ring */
2712	*rsp = kzalloc(sizeof(struct rsp_que), GFP_KERNEL);
2713	if (!*rsp) {
2714		qla_printk(KERN_WARNING, ha,
2715			"Unable to allocate memory for rsp\n");
2716		goto fail_rsp;
2717	}
2718	(*rsp)->hw = ha;
2719	(*rsp)->length = rsp_len;
2720	(*rsp)->ring = dma_alloc_coherent(&ha->pdev->dev,
2721		((*rsp)->length + 1) * sizeof(response_t),
2722		&(*rsp)->dma, GFP_KERNEL);
2723	if (!(*rsp)->ring) {
2724		qla_printk(KERN_WARNING, ha,
2725			"Unable to allocate memory for rsp_ring\n");
2726		goto fail_rsp_ring;
2727	}
2728	(*req)->rsp = *rsp;
2729	(*rsp)->req = *req;
2730	/* Allocate memory for NVRAM data for vports */
2731	if (ha->nvram_npiv_size) {
2732		ha->npiv_info = kzalloc(sizeof(struct qla_npiv_entry) *
2733					ha->nvram_npiv_size, GFP_KERNEL);
2734		if (!ha->npiv_info) {
2735			qla_printk(KERN_WARNING, ha,
2736				"Unable to allocate memory for npiv info\n");
2737			goto fail_npiv_info;
2738		}
2739	} else
2740		ha->npiv_info = NULL;
2741
2742	/* Get consistent memory allocated for EX-INIT-CB. */
2743	if (IS_QLA8XXX_TYPE(ha)) {
2744		ha->ex_init_cb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
2745		    &ha->ex_init_cb_dma);
2746		if (!ha->ex_init_cb)
2747			goto fail_ex_init_cb;
2748	}
2749
2750	INIT_LIST_HEAD(&ha->gbl_dsd_list);
2751
2752	/* Get consistent memory allocated for Async Port-Database. */
2753	if (!IS_FWI2_CAPABLE(ha)) {
2754		ha->async_pd = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
2755			&ha->async_pd_dma);
2756		if (!ha->async_pd)
2757			goto fail_async_pd;
2758	}
2759
2760	INIT_LIST_HEAD(&ha->vp_list);
2761	return 1;
2762
2763fail_async_pd:
2764	dma_pool_free(ha->s_dma_pool, ha->ex_init_cb, ha->ex_init_cb_dma);
2765fail_ex_init_cb:
2766	kfree(ha->npiv_info);
2767fail_npiv_info:
2768	dma_free_coherent(&ha->pdev->dev, ((*rsp)->length + 1) *
2769		sizeof(response_t), (*rsp)->ring, (*rsp)->dma);
2770	(*rsp)->ring = NULL;
2771	(*rsp)->dma = 0;
2772fail_rsp_ring:
2773	kfree(*rsp);
2774fail_rsp:
2775	dma_free_coherent(&ha->pdev->dev, ((*req)->length + 1) *
2776		sizeof(request_t), (*req)->ring, (*req)->dma);
2777	(*req)->ring = NULL;
2778	(*req)->dma = 0;
2779fail_req_ring:
2780	kfree(*req);
2781fail_req:
2782	dma_free_coherent(&ha->pdev->dev, sizeof(struct ct_sns_pkt),
2783		ha->ct_sns, ha->ct_sns_dma);
2784	ha->ct_sns = NULL;
2785	ha->ct_sns_dma = 0;
2786fail_free_ms_iocb:
2787	dma_pool_free(ha->s_dma_pool, ha->ms_iocb, ha->ms_iocb_dma);
2788	ha->ms_iocb = NULL;
2789	ha->ms_iocb_dma = 0;
2790fail_dma_pool:
2791	if (IS_QLA82XX(ha) || ql2xenabledif) {
2792		dma_pool_destroy(ha->fcp_cmnd_dma_pool);
2793		ha->fcp_cmnd_dma_pool = NULL;
2794	}
2795fail_dl_dma_pool:
2796	if (IS_QLA82XX(ha) || ql2xenabledif) {
2797		dma_pool_destroy(ha->dl_dma_pool);
2798		ha->dl_dma_pool = NULL;
2799	}
2800fail_s_dma_pool:
2801	dma_pool_destroy(ha->s_dma_pool);
2802	ha->s_dma_pool = NULL;
2803fail_free_nvram:
2804	kfree(ha->nvram);
2805	ha->nvram = NULL;
2806fail_free_ctx_mempool:
2807	mempool_destroy(ha->ctx_mempool);
2808	ha->ctx_mempool = NULL;
2809fail_free_srb_mempool:
2810	mempool_destroy(ha->srb_mempool);
2811	ha->srb_mempool = NULL;
2812fail_free_gid_list:
2813	dma_free_coherent(&ha->pdev->dev, GID_LIST_SIZE, ha->gid_list,
2814	ha->gid_list_dma);
2815	ha->gid_list = NULL;
2816	ha->gid_list_dma = 0;
2817fail_free_init_cb:
2818	dma_free_coherent(&ha->pdev->dev, ha->init_cb_size, ha->init_cb,
2819	ha->init_cb_dma);
2820	ha->init_cb = NULL;
2821	ha->init_cb_dma = 0;
2822fail:
2823	DEBUG(printk("%s: Memory allocation failure\n", __func__));
2824	return -ENOMEM;
2825}
2826
2827/*
2828* qla2x00_mem_free
2829*      Frees all adapter allocated memory.
2830*
2831* Input:
2832*      ha = adapter block pointer.
2833*/
2834static void
2835qla2x00_mem_free(struct qla_hw_data *ha)
2836{
2837	if (ha->srb_mempool)
2838		mempool_destroy(ha->srb_mempool);
2839
2840	if (ha->fce)
2841		dma_free_coherent(&ha->pdev->dev, FCE_SIZE, ha->fce,
2842		ha->fce_dma);
2843
2844	if (ha->fw_dump) {
2845		if (ha->eft)
2846			dma_free_coherent(&ha->pdev->dev,
2847			ntohl(ha->fw_dump->eft_size), ha->eft, ha->eft_dma);
2848		vfree(ha->fw_dump);
2849	}
2850
2851	if (ha->dcbx_tlv)
2852		dma_free_coherent(&ha->pdev->dev, DCBX_TLV_DATA_SIZE,
2853		    ha->dcbx_tlv, ha->dcbx_tlv_dma);
2854
2855	if (ha->xgmac_data)
2856		dma_free_coherent(&ha->pdev->dev, XGMAC_DATA_SIZE,
2857		    ha->xgmac_data, ha->xgmac_data_dma);
2858
2859	if (ha->sns_cmd)
2860		dma_free_coherent(&ha->pdev->dev, sizeof(struct sns_cmd_pkt),
2861		ha->sns_cmd, ha->sns_cmd_dma);
2862
2863	if (ha->ct_sns)
2864		dma_free_coherent(&ha->pdev->dev, sizeof(struct ct_sns_pkt),
2865		ha->ct_sns, ha->ct_sns_dma);
2866
2867	if (ha->sfp_data)
2868		dma_pool_free(ha->s_dma_pool, ha->sfp_data, ha->sfp_data_dma);
2869
2870	if (ha->edc_data)
2871		dma_pool_free(ha->s_dma_pool, ha->edc_data, ha->edc_data_dma);
2872
2873	if (ha->ms_iocb)
2874		dma_pool_free(ha->s_dma_pool, ha->ms_iocb, ha->ms_iocb_dma);
2875
2876	if (ha->ex_init_cb)
2877		dma_pool_free(ha->s_dma_pool,
2878			ha->ex_init_cb, ha->ex_init_cb_dma);
2879
2880	if (ha->async_pd)
2881		dma_pool_free(ha->s_dma_pool, ha->async_pd, ha->async_pd_dma);
2882
2883	if (ha->s_dma_pool)
2884		dma_pool_destroy(ha->s_dma_pool);
2885
2886	if (ha->gid_list)
2887		dma_free_coherent(&ha->pdev->dev, GID_LIST_SIZE, ha->gid_list,
2888		ha->gid_list_dma);
2889
2890	if (IS_QLA82XX(ha)) {
2891		if (!list_empty(&ha->gbl_dsd_list)) {
2892			struct dsd_dma *dsd_ptr, *tdsd_ptr;
2893
2894			/* clean up allocated prev pool */
2895			list_for_each_entry_safe(dsd_ptr,
2896				tdsd_ptr, &ha->gbl_dsd_list, list) {
2897				dma_pool_free(ha->dl_dma_pool,
2898				dsd_ptr->dsd_addr, dsd_ptr->dsd_list_dma);
2899				list_del(&dsd_ptr->list);
2900				kfree(dsd_ptr);
2901			}
2902		}
2903	}
2904
2905	if (ha->dl_dma_pool)
2906		dma_pool_destroy(ha->dl_dma_pool);
2907
2908	if (ha->fcp_cmnd_dma_pool)
2909		dma_pool_destroy(ha->fcp_cmnd_dma_pool);
2910
2911	if (ha->ctx_mempool)
2912		mempool_destroy(ha->ctx_mempool);
2913
2914	if (ha->init_cb)
2915		dma_free_coherent(&ha->pdev->dev, ha->init_cb_size,
2916			ha->init_cb, ha->init_cb_dma);
2917	vfree(ha->optrom_buffer);
2918	kfree(ha->nvram);
2919	kfree(ha->npiv_info);
2920
2921	ha->srb_mempool = NULL;
2922	ha->ctx_mempool = NULL;
2923	ha->eft = NULL;
2924	ha->eft_dma = 0;
2925	ha->sns_cmd = NULL;
2926	ha->sns_cmd_dma = 0;
2927	ha->ct_sns = NULL;
2928	ha->ct_sns_dma = 0;
2929	ha->ms_iocb = NULL;
2930	ha->ms_iocb_dma = 0;
2931	ha->init_cb = NULL;
2932	ha->init_cb_dma = 0;
2933	ha->ex_init_cb = NULL;
2934	ha->ex_init_cb_dma = 0;
2935	ha->async_pd = NULL;
2936	ha->async_pd_dma = 0;
2937
2938	ha->s_dma_pool = NULL;
2939	ha->dl_dma_pool = NULL;
2940	ha->fcp_cmnd_dma_pool = NULL;
2941
2942	ha->gid_list = NULL;
2943	ha->gid_list_dma = 0;
2944
2945	ha->fw_dump = NULL;
2946	ha->fw_dumped = 0;
2947	ha->fw_dump_reading = 0;
2948}
2949
2950struct scsi_qla_host *qla2x00_create_host(struct scsi_host_template *sht,
2951						struct qla_hw_data *ha)
2952{
2953	struct Scsi_Host *host;
2954	struct scsi_qla_host *vha = NULL;
2955
2956	host = scsi_host_alloc(sht, sizeof(scsi_qla_host_t));
2957	if (host == NULL) {
2958		printk(KERN_WARNING
2959		"qla2xxx: Couldn't allocate host from scsi layer!\n");
2960		goto fail;
2961	}
2962
2963	/* Clear our data area */
2964	vha = shost_priv(host);
2965	memset(vha, 0, sizeof(scsi_qla_host_t));
2966
2967	vha->host = host;
2968	vha->host_no = host->host_no;
2969	vha->hw = ha;
2970
2971	INIT_LIST_HEAD(&vha->vp_fcports);
2972	INIT_LIST_HEAD(&vha->work_list);
2973	INIT_LIST_HEAD(&vha->list);
2974
2975	spin_lock_init(&vha->work_lock);
2976
2977	sprintf(vha->host_str, "%s_%ld", QLA2XXX_DRIVER_NAME, vha->host_no);
2978	return vha;
2979
2980fail:
2981	return vha;
2982}
2983
2984static struct qla_work_evt *
2985qla2x00_alloc_work(struct scsi_qla_host *vha, enum qla_work_type type)
2986{
2987	struct qla_work_evt *e;
2988	uint8_t bail;
2989
2990	QLA_VHA_MARK_BUSY(vha, bail);
2991	if (bail)
2992		return NULL;
2993
2994	e = kzalloc(sizeof(struct qla_work_evt), GFP_ATOMIC);
2995	if (!e) {
2996		QLA_VHA_MARK_NOT_BUSY(vha);
2997		return NULL;
2998	}
2999
3000	INIT_LIST_HEAD(&e->list);
3001	e->type = type;
3002	e->flags = QLA_EVT_FLAG_FREE;
3003	return e;
3004}
3005
3006static int
3007qla2x00_post_work(struct scsi_qla_host *vha, struct qla_work_evt *e)
3008{
3009	unsigned long flags;
3010
3011	spin_lock_irqsave(&vha->work_lock, flags);
3012	list_add_tail(&e->list, &vha->work_list);
3013	spin_unlock_irqrestore(&vha->work_lock, flags);
3014	qla2xxx_wake_dpc(vha);
3015
3016	return QLA_SUCCESS;
3017}
3018
3019int
3020qla2x00_post_aen_work(struct scsi_qla_host *vha, enum fc_host_event_code code,
3021    u32 data)
3022{
3023	struct qla_work_evt *e;
3024
3025	e = qla2x00_alloc_work(vha, QLA_EVT_AEN);
3026	if (!e)
3027		return QLA_FUNCTION_FAILED;
3028
3029	e->u.aen.code = code;
3030	e->u.aen.data = data;
3031	return qla2x00_post_work(vha, e);
3032}
3033
3034int
3035qla2x00_post_idc_ack_work(struct scsi_qla_host *vha, uint16_t *mb)
3036{
3037	struct qla_work_evt *e;
3038
3039	e = qla2x00_alloc_work(vha, QLA_EVT_IDC_ACK);
3040	if (!e)
3041		return QLA_FUNCTION_FAILED;
3042
3043	memcpy(e->u.idc_ack.mb, mb, QLA_IDC_ACK_REGS * sizeof(uint16_t));
3044	return qla2x00_post_work(vha, e);
3045}
3046
3047#define qla2x00_post_async_work(name, type)	\
3048int qla2x00_post_async_##name##_work(		\
3049    struct scsi_qla_host *vha,			\
3050    fc_port_t *fcport, uint16_t *data)		\
3051{						\
3052	struct qla_work_evt *e;			\
3053						\
3054	e = qla2x00_alloc_work(vha, type);	\
3055	if (!e)					\
3056		return QLA_FUNCTION_FAILED;	\
3057						\
3058	e->u.logio.fcport = fcport;		\
3059	if (data) {				\
3060		e->u.logio.data[0] = data[0];	\
3061		e->u.logio.data[1] = data[1];	\
3062	}					\
3063	return qla2x00_post_work(vha, e);	\
3064}
3065
3066qla2x00_post_async_work(login, QLA_EVT_ASYNC_LOGIN);
3067qla2x00_post_async_work(login_done, QLA_EVT_ASYNC_LOGIN_DONE);
3068qla2x00_post_async_work(logout, QLA_EVT_ASYNC_LOGOUT);
3069qla2x00_post_async_work(logout_done, QLA_EVT_ASYNC_LOGOUT_DONE);
3070qla2x00_post_async_work(adisc, QLA_EVT_ASYNC_ADISC);
3071qla2x00_post_async_work(adisc_done, QLA_EVT_ASYNC_ADISC_DONE);
3072
3073int
3074qla2x00_post_uevent_work(struct scsi_qla_host *vha, u32 code)
3075{
3076	struct qla_work_evt *e;
3077
3078	e = qla2x00_alloc_work(vha, QLA_EVT_UEVENT);
3079	if (!e)
3080		return QLA_FUNCTION_FAILED;
3081
3082	e->u.uevent.code = code;
3083	return qla2x00_post_work(vha, e);
3084}
3085
3086static void
3087qla2x00_uevent_emit(struct scsi_qla_host *vha, u32 code)
3088{
3089	char event_string[40];
3090	char *envp[] = { event_string, NULL };
3091
3092	switch (code) {
3093	case QLA_UEVENT_CODE_FW_DUMP:
3094		snprintf(event_string, sizeof(event_string), "FW_DUMP=%ld",
3095		    vha->host_no);
3096		break;
3097	default:
3098		/* do nothing */
3099		break;
3100	}
3101	kobject_uevent_env(&vha->hw->pdev->dev.kobj, KOBJ_CHANGE, envp);
3102}
3103
3104void
3105qla2x00_do_work(struct scsi_qla_host *vha)
3106{
3107	struct qla_work_evt *e, *tmp;
3108	unsigned long flags;
3109	LIST_HEAD(work);
3110
3111	spin_lock_irqsave(&vha->work_lock, flags);
3112	list_splice_init(&vha->work_list, &work);
3113	spin_unlock_irqrestore(&vha->work_lock, flags);
3114
3115	list_for_each_entry_safe(e, tmp, &work, list) {
3116		list_del_init(&e->list);
3117
3118		switch (e->type) {
3119		case QLA_EVT_AEN:
3120			fc_host_post_event(vha->host, fc_get_event_number(),
3121			    e->u.aen.code, e->u.aen.data);
3122			break;
3123		case QLA_EVT_IDC_ACK:
3124			qla81xx_idc_ack(vha, e->u.idc_ack.mb);
3125			break;
3126		case QLA_EVT_ASYNC_LOGIN:
3127			qla2x00_async_login(vha, e->u.logio.fcport,
3128			    e->u.logio.data);
3129			break;
3130		case QLA_EVT_ASYNC_LOGIN_DONE:
3131			qla2x00_async_login_done(vha, e->u.logio.fcport,
3132			    e->u.logio.data);
3133			break;
3134		case QLA_EVT_ASYNC_LOGOUT:
3135			qla2x00_async_logout(vha, e->u.logio.fcport);
3136			break;
3137		case QLA_EVT_ASYNC_LOGOUT_DONE:
3138			qla2x00_async_logout_done(vha, e->u.logio.fcport,
3139			    e->u.logio.data);
3140			break;
3141		case QLA_EVT_ASYNC_ADISC:
3142			qla2x00_async_adisc(vha, e->u.logio.fcport,
3143			    e->u.logio.data);
3144			break;
3145		case QLA_EVT_ASYNC_ADISC_DONE:
3146			qla2x00_async_adisc_done(vha, e->u.logio.fcport,
3147			    e->u.logio.data);
3148			break;
3149		case QLA_EVT_UEVENT:
3150			qla2x00_uevent_emit(vha, e->u.uevent.code);
3151			break;
3152		}
3153		if (e->flags & QLA_EVT_FLAG_FREE)
3154			kfree(e);
3155
3156		/* For each work completed decrement vha ref count */
3157		QLA_VHA_MARK_NOT_BUSY(vha);
3158	}
3159}
3160
3161/* Relogins all the fcports of a vport
3162 * Context: dpc thread
3163 */
3164void qla2x00_relogin(struct scsi_qla_host *vha)
3165{
3166	fc_port_t       *fcport;
3167	int status;
3168	uint16_t        next_loopid = 0;
3169	struct qla_hw_data *ha = vha->hw;
3170	uint16_t data[2];
3171
3172	list_for_each_entry(fcport, &vha->vp_fcports, list) {
3173	/*
3174	 * If the port is not ONLINE then try to login
3175	 * to it if we haven't run out of retries.
3176	 */
3177		if (atomic_read(&fcport->state) != FCS_ONLINE &&
3178		    fcport->login_retry && !(fcport->flags & FCF_ASYNC_SENT)) {
3179			fcport->login_retry--;
3180			if (fcport->flags & FCF_FABRIC_DEVICE) {
3181				if (fcport->flags & FCF_FCP2_DEVICE)
3182					ha->isp_ops->fabric_logout(vha,
3183							fcport->loop_id,
3184							fcport->d_id.b.domain,
3185							fcport->d_id.b.area,
3186							fcport->d_id.b.al_pa);
3187
3188				if (IS_ALOGIO_CAPABLE(ha)) {
3189					fcport->flags |= FCF_ASYNC_SENT;
3190					data[0] = 0;
3191					data[1] = QLA_LOGIO_LOGIN_RETRIED;
3192					status = qla2x00_post_async_login_work(
3193					    vha, fcport, data);
3194					if (status == QLA_SUCCESS)
3195						continue;
3196					/* Attempt a retry. */
3197					status = 1;
3198				} else
3199					status = qla2x00_fabric_login(vha,
3200					    fcport, &next_loopid);
3201			} else
3202				status = qla2x00_local_device_login(vha,
3203								fcport);
3204
3205			if (status == QLA_SUCCESS) {
3206				fcport->old_loop_id = fcport->loop_id;
3207
3208				DEBUG(printk("scsi(%ld): port login OK: logged "
3209				"in ID 0x%x\n", vha->host_no, fcport->loop_id));
3210
3211				qla2x00_update_fcport(vha, fcport);
3212
3213			} else if (status == 1) {
3214				set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
3215				/* retry the login again */
3216				DEBUG(printk("scsi(%ld): Retrying"
3217				" %d login again loop_id 0x%x\n",
3218				vha->host_no, fcport->login_retry,
3219						fcport->loop_id));
3220			} else {
3221				fcport->login_retry = 0;
3222			}
3223
3224			if (fcport->login_retry == 0 && status != QLA_SUCCESS)
3225				fcport->loop_id = FC_NO_LOOP_ID;
3226		}
3227		if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
3228			break;
3229	}
3230}
3231
3232/**************************************************************************
3233* qla2x00_do_dpc
3234*   This kernel thread is a task that is schedule by the interrupt handler
3235*   to perform the background processing for interrupts.
3236*
3237* Notes:
3238* This task always run in the context of a kernel thread.  It
3239* is kick-off by the driver's detect code and starts up
3240* up one per adapter. It immediately goes to sleep and waits for
3241* some fibre event.  When either the interrupt handler or
3242* the timer routine detects a event it will one of the task
3243* bits then wake us up.
3244**************************************************************************/
3245static int
3246qla2x00_do_dpc(void *data)
3247{
3248	int		rval;
3249	scsi_qla_host_t *base_vha;
3250	struct qla_hw_data *ha;
3251
3252	ha = (struct qla_hw_data *)data;
3253	base_vha = pci_get_drvdata(ha->pdev);
3254
3255	set_user_nice(current, -20);
3256
3257	while (!kthread_should_stop()) {
3258		DEBUG3(printk("qla2x00: DPC handler sleeping\n"));
3259
3260		set_current_state(TASK_INTERRUPTIBLE);
3261		schedule();
3262		__set_current_state(TASK_RUNNING);
3263
3264		DEBUG3(printk("qla2x00: DPC handler waking up\n"));
3265
3266		/* Initialization not yet finished. Don't do anything yet. */
3267		if (!base_vha->flags.init_done)
3268			continue;
3269
3270		if (ha->flags.eeh_busy) {
3271			DEBUG17(qla_printk(KERN_WARNING, ha,
3272			    "qla2x00_do_dpc: dpc_flags: %lx\n",
3273			    base_vha->dpc_flags));
3274			continue;
3275		}
3276
3277		DEBUG3(printk("scsi(%ld): DPC handler\n", base_vha->host_no));
3278
3279		ha->dpc_active = 1;
3280
3281		if (ha->flags.mbox_busy) {
3282			ha->dpc_active = 0;
3283			continue;
3284		}
3285
3286		qla2x00_do_work(base_vha);
3287
3288		if (IS_QLA82XX(ha)) {
3289			if (test_and_clear_bit(ISP_UNRECOVERABLE,
3290				&base_vha->dpc_flags)) {
3291				qla82xx_idc_lock(ha);
3292				qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
3293					QLA82XX_DEV_FAILED);
3294				qla82xx_idc_unlock(ha);
3295				qla_printk(KERN_INFO, ha,
3296					"HW State: FAILED\n");
3297				qla82xx_device_state_handler(base_vha);
3298				continue;
3299			}
3300
3301			if (test_and_clear_bit(FCOE_CTX_RESET_NEEDED,
3302				&base_vha->dpc_flags)) {
3303
3304				DEBUG(printk(KERN_INFO
3305					"scsi(%ld): dpc: sched "
3306					"qla82xx_fcoe_ctx_reset ha = %p\n",
3307					base_vha->host_no, ha));
3308				if (!(test_and_set_bit(ABORT_ISP_ACTIVE,
3309					&base_vha->dpc_flags))) {
3310					if (qla82xx_fcoe_ctx_reset(base_vha)) {
3311						/* FCoE-ctx reset failed.
3312						 * Escalate to chip-reset
3313						 */
3314						set_bit(ISP_ABORT_NEEDED,
3315							&base_vha->dpc_flags);
3316					}
3317					clear_bit(ABORT_ISP_ACTIVE,
3318						&base_vha->dpc_flags);
3319				}
3320
3321				DEBUG(printk("scsi(%ld): dpc:"
3322					" qla82xx_fcoe_ctx_reset end\n",
3323					base_vha->host_no));
3324			}
3325		}
3326
3327		if (test_and_clear_bit(ISP_ABORT_NEEDED,
3328						&base_vha->dpc_flags)) {
3329
3330			DEBUG(printk("scsi(%ld): dpc: sched "
3331			    "qla2x00_abort_isp ha = %p\n",
3332			    base_vha->host_no, ha));
3333			if (!(test_and_set_bit(ABORT_ISP_ACTIVE,
3334			    &base_vha->dpc_flags))) {
3335
3336				if (ha->isp_ops->abort_isp(base_vha)) {
3337					/* failed. retry later */
3338					set_bit(ISP_ABORT_NEEDED,
3339					    &base_vha->dpc_flags);
3340				}
3341				clear_bit(ABORT_ISP_ACTIVE,
3342						&base_vha->dpc_flags);
3343			}
3344
3345			DEBUG(printk("scsi(%ld): dpc: qla2x00_abort_isp end\n",
3346			    base_vha->host_no));
3347		}
3348
3349		if (test_bit(FCPORT_UPDATE_NEEDED, &base_vha->dpc_flags)) {
3350			qla2x00_update_fcports(base_vha);
3351			clear_bit(FCPORT_UPDATE_NEEDED, &base_vha->dpc_flags);
3352		}
3353
3354		if (test_and_clear_bit(RESET_MARKER_NEEDED,
3355							&base_vha->dpc_flags) &&
3356		    (!(test_and_set_bit(RESET_ACTIVE, &base_vha->dpc_flags)))) {
3357
3358			DEBUG(printk("scsi(%ld): qla2x00_reset_marker()\n",
3359			    base_vha->host_no));
3360
3361			qla2x00_rst_aen(base_vha);
3362			clear_bit(RESET_ACTIVE, &base_vha->dpc_flags);
3363		}
3364
3365		/* Retry each device up to login retry count */
3366		if ((test_and_clear_bit(RELOGIN_NEEDED,
3367						&base_vha->dpc_flags)) &&
3368		    !test_bit(LOOP_RESYNC_NEEDED, &base_vha->dpc_flags) &&
3369		    atomic_read(&base_vha->loop_state) != LOOP_DOWN) {
3370
3371			DEBUG(printk("scsi(%ld): qla2x00_port_login()\n",
3372					base_vha->host_no));
3373			qla2x00_relogin(base_vha);
3374
3375			DEBUG(printk("scsi(%ld): qla2x00_port_login - end\n",
3376			    base_vha->host_no));
3377		}
3378
3379		if (test_and_clear_bit(LOOP_RESYNC_NEEDED,
3380							&base_vha->dpc_flags)) {
3381
3382			DEBUG(printk("scsi(%ld): qla2x00_loop_resync()\n",
3383				base_vha->host_no));
3384
3385			if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE,
3386			    &base_vha->dpc_flags))) {
3387
3388				rval = qla2x00_loop_resync(base_vha);
3389
3390				clear_bit(LOOP_RESYNC_ACTIVE,
3391						&base_vha->dpc_flags);
3392			}
3393
3394			DEBUG(printk("scsi(%ld): qla2x00_loop_resync - end\n",
3395			    base_vha->host_no));
3396		}
3397
3398		if (test_bit(NPIV_CONFIG_NEEDED, &base_vha->dpc_flags) &&
3399		    atomic_read(&base_vha->loop_state) == LOOP_READY) {
3400			clear_bit(NPIV_CONFIG_NEEDED, &base_vha->dpc_flags);
3401			qla2xxx_flash_npiv_conf(base_vha);
3402		}
3403
3404		if (!ha->interrupts_on)
3405			ha->isp_ops->enable_intrs(ha);
3406
3407		if (test_and_clear_bit(BEACON_BLINK_NEEDED,
3408					&base_vha->dpc_flags))
3409			ha->isp_ops->beacon_blink(base_vha);
3410
3411		qla2x00_do_dpc_all_vps(base_vha);
3412
3413		ha->dpc_active = 0;
3414	} /* End of while(1) */
3415
3416	DEBUG(printk("scsi(%ld): DPC handler exiting\n", base_vha->host_no));
3417
3418	/*
3419	 * Make sure that nobody tries to wake us up again.
3420	 */
3421	ha->dpc_active = 0;
3422
3423	/* Cleanup any residual CTX SRBs. */
3424	qla2x00_abort_all_cmds(base_vha, DID_NO_CONNECT << 16);
3425
3426	return 0;
3427}
3428
3429void
3430qla2xxx_wake_dpc(struct scsi_qla_host *vha)
3431{
3432	struct qla_hw_data *ha = vha->hw;
3433	struct task_struct *t = ha->dpc_thread;
3434
3435	if (!test_bit(UNLOADING, &vha->dpc_flags) && t)
3436		wake_up_process(t);
3437}
3438
3439/*
3440*  qla2x00_rst_aen
3441*      Processes asynchronous reset.
3442*
3443* Input:
3444*      ha  = adapter block pointer.
3445*/
3446static void
3447qla2x00_rst_aen(scsi_qla_host_t *vha)
3448{
3449	if (vha->flags.online && !vha->flags.reset_active &&
3450	    !atomic_read(&vha->loop_down_timer) &&
3451	    !(test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))) {
3452		do {
3453			clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
3454
3455			/*
3456			 * Issue marker command only when we are going to start
3457			 * the I/O.
3458			 */
3459			vha->marker_needed = 1;
3460		} while (!atomic_read(&vha->loop_down_timer) &&
3461		    (test_bit(RESET_MARKER_NEEDED, &vha->dpc_flags)));
3462	}
3463}
3464
3465static void
3466qla2x00_sp_free_dma(srb_t *sp)
3467{
3468	struct scsi_cmnd *cmd = sp->cmd;
3469	struct qla_hw_data *ha = sp->fcport->vha->hw;
3470
3471	if (sp->flags & SRB_DMA_VALID) {
3472		scsi_dma_unmap(cmd);
3473		sp->flags &= ~SRB_DMA_VALID;
3474	}
3475
3476	if (sp->flags & SRB_CRC_PROT_DMA_VALID) {
3477		dma_unmap_sg(&ha->pdev->dev, scsi_prot_sglist(cmd),
3478		    scsi_prot_sg_count(cmd), cmd->sc_data_direction);
3479		sp->flags &= ~SRB_CRC_PROT_DMA_VALID;
3480	}
3481
3482	if (sp->flags & SRB_CRC_CTX_DSD_VALID) {
3483		/* List assured to be having elements */
3484		qla2x00_clean_dsd_pool(ha, sp);
3485		sp->flags &= ~SRB_CRC_CTX_DSD_VALID;
3486	}
3487
3488	if (sp->flags & SRB_CRC_CTX_DMA_VALID) {
3489		dma_pool_free(ha->dl_dma_pool, sp->ctx,
3490		    ((struct crc_context *)sp->ctx)->crc_ctx_dma);
3491		sp->flags &= ~SRB_CRC_CTX_DMA_VALID;
3492	}
3493
3494	CMD_SP(cmd) = NULL;
3495}
3496
3497static void
3498qla2x00_sp_final_compl(struct qla_hw_data *ha, srb_t *sp)
3499{
3500	struct scsi_cmnd *cmd = sp->cmd;
3501
3502	qla2x00_sp_free_dma(sp);
3503
3504	if (sp->flags & SRB_FCP_CMND_DMA_VALID) {
3505		struct ct6_dsd *ctx = sp->ctx;
3506		dma_pool_free(ha->fcp_cmnd_dma_pool, ctx->fcp_cmnd,
3507			ctx->fcp_cmnd_dma);
3508		list_splice(&ctx->dsd_list, &ha->gbl_dsd_list);
3509		ha->gbl_dsd_inuse -= ctx->dsd_use_cnt;
3510		ha->gbl_dsd_avail += ctx->dsd_use_cnt;
3511		mempool_free(sp->ctx, ha->ctx_mempool);
3512		sp->ctx = NULL;
3513	}
3514
3515	mempool_free(sp, ha->srb_mempool);
3516	cmd->scsi_done(cmd);
3517}
3518
3519void
3520qla2x00_sp_compl(struct qla_hw_data *ha, srb_t *sp)
3521{
3522	if (atomic_read(&sp->ref_count) == 0) {
3523		DEBUG2(qla_printk(KERN_WARNING, ha,
3524		    "SP reference-count to ZERO -- sp=%p\n", sp));
3525		DEBUG2(BUG());
3526		return;
3527	}
3528	if (!atomic_dec_and_test(&sp->ref_count))
3529		return;
3530	qla2x00_sp_final_compl(ha, sp);
3531}
3532
3533/**************************************************************************
3534*   qla2x00_timer
3535*
3536* Description:
3537*   One second timer
3538*
3539* Context: Interrupt
3540***************************************************************************/
3541void
3542qla2x00_timer(scsi_qla_host_t *vha)
3543{
3544	unsigned long	cpu_flags = 0;
3545	fc_port_t	*fcport;
3546	int		start_dpc = 0;
3547	int		index;
3548	srb_t		*sp;
3549	int		t;
3550	uint16_t        w;
3551	struct qla_hw_data *ha = vha->hw;
3552	struct req_que *req;
3553
3554	if (IS_QLA82XX(ha))
3555		qla82xx_watchdog(vha);
3556
3557	/* Hardware read to raise pending EEH errors during mailbox waits. */
3558	if (!pci_channel_offline(ha->pdev))
3559		pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
3560	/*
3561	 * Ports - Port down timer.
3562	 *
3563	 * Whenever, a port is in the LOST state we start decrementing its port
3564	 * down timer every second until it reaches zero. Once  it reaches zero
3565	 * the port it marked DEAD.
3566	 */
3567	t = 0;
3568	list_for_each_entry(fcport, &vha->vp_fcports, list) {
3569		if (fcport->port_type != FCT_TARGET)
3570			continue;
3571
3572		if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
3573
3574			if (atomic_read(&fcport->port_down_timer) == 0)
3575				continue;
3576
3577			if (atomic_dec_and_test(&fcport->port_down_timer) != 0)
3578				atomic_set(&fcport->state, FCS_DEVICE_DEAD);
3579
3580			DEBUG(printk("scsi(%ld): fcport-%d - port retry count: "
3581			    "%d remaining\n",
3582			    vha->host_no,
3583			    t, atomic_read(&fcport->port_down_timer)));
3584		}
3585		t++;
3586	} /* End of for fcport  */
3587
3588
3589	/* Loop down handler. */
3590	if (atomic_read(&vha->loop_down_timer) > 0 &&
3591	    !(test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))
3592		&& vha->flags.online) {
3593
3594		if (atomic_read(&vha->loop_down_timer) ==
3595		    vha->loop_down_abort_time) {
3596
3597			DEBUG(printk("scsi(%ld): Loop Down - aborting the "
3598			    "queues before time expire\n",
3599			    vha->host_no));
3600
3601			if (!IS_QLA2100(ha) && vha->link_down_timeout)
3602				atomic_set(&vha->loop_state, LOOP_DEAD);
3603
3604			/*
3605			 * Schedule an ISP abort to return any FCP2-device
3606			 * commands.
3607			 */
3608			/* NPIV - scan physical port only */
3609			if (!vha->vp_idx) {
3610				spin_lock_irqsave(&ha->hardware_lock,
3611				    cpu_flags);
3612				req = ha->req_q_map[0];
3613				for (index = 1;
3614				    index < MAX_OUTSTANDING_COMMANDS;
3615				    index++) {
3616					fc_port_t *sfcp;
3617
3618					sp = req->outstanding_cmds[index];
3619					if (!sp)
3620						continue;
3621					if (sp->ctx && !IS_PROT_IO(sp))
3622						continue;
3623					sfcp = sp->fcport;
3624					if (!(sfcp->flags & FCF_FCP2_DEVICE))
3625						continue;
3626
3627					set_bit(ISP_ABORT_NEEDED,
3628							&vha->dpc_flags);
3629					break;
3630				}
3631				spin_unlock_irqrestore(&ha->hardware_lock,
3632								cpu_flags);
3633			}
3634			start_dpc++;
3635		}
3636
3637		/* if the loop has been down for 4 minutes, reinit adapter */
3638		if (atomic_dec_and_test(&vha->loop_down_timer) != 0) {
3639			if (!(vha->device_flags & DFLG_NO_CABLE)) {
3640				DEBUG(printk("scsi(%ld): Loop down - "
3641				    "aborting ISP.\n",
3642				    vha->host_no));
3643				qla_printk(KERN_WARNING, ha,
3644				    "Loop down - aborting ISP.\n");
3645
3646				set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3647			}
3648		}
3649		DEBUG3(printk("scsi(%ld): Loop Down - seconds remaining %d\n",
3650		    vha->host_no,
3651		    atomic_read(&vha->loop_down_timer)));
3652	}
3653
3654	/* Check if beacon LED needs to be blinked */
3655	if (ha->beacon_blink_led == 1) {
3656		set_bit(BEACON_BLINK_NEEDED, &vha->dpc_flags);
3657		start_dpc++;
3658	}
3659
3660	/* Process any deferred work. */
3661	if (!list_empty(&vha->work_list))
3662		start_dpc++;
3663
3664	/* Schedule the DPC routine if needed */
3665	if ((test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
3666	    test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags) ||
3667	    test_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags) ||
3668	    start_dpc ||
3669	    test_bit(RESET_MARKER_NEEDED, &vha->dpc_flags) ||
3670	    test_bit(BEACON_BLINK_NEEDED, &vha->dpc_flags) ||
3671	    test_bit(ISP_UNRECOVERABLE, &vha->dpc_flags) ||
3672	    test_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags) ||
3673	    test_bit(VP_DPC_NEEDED, &vha->dpc_flags) ||
3674	    test_bit(RELOGIN_NEEDED, &vha->dpc_flags)))
3675		qla2xxx_wake_dpc(vha);
3676
3677	qla2x00_restart_timer(vha, WATCH_INTERVAL);
3678}
3679
3680/* Firmware interface routines. */
3681
3682#define FW_BLOBS	8
3683#define FW_ISP21XX	0
3684#define FW_ISP22XX	1
3685#define FW_ISP2300	2
3686#define FW_ISP2322	3
3687#define FW_ISP24XX	4
3688#define FW_ISP25XX	5
3689#define FW_ISP81XX	6
3690#define FW_ISP82XX	7
3691
3692#define FW_FILE_ISP21XX	"ql2100_fw.bin"
3693#define FW_FILE_ISP22XX	"ql2200_fw.bin"
3694#define FW_FILE_ISP2300	"ql2300_fw.bin"
3695#define FW_FILE_ISP2322	"ql2322_fw.bin"
3696#define FW_FILE_ISP24XX	"ql2400_fw.bin"
3697#define FW_FILE_ISP25XX	"ql2500_fw.bin"
3698#define FW_FILE_ISP81XX	"ql8100_fw.bin"
3699#define FW_FILE_ISP82XX	"ql8200_fw.bin"
3700
3701static DEFINE_MUTEX(qla_fw_lock);
3702
3703static struct fw_blob qla_fw_blobs[FW_BLOBS] = {
3704	{ .name = FW_FILE_ISP21XX, .segs = { 0x1000, 0 }, },
3705	{ .name = FW_FILE_ISP22XX, .segs = { 0x1000, 0 }, },
3706	{ .name = FW_FILE_ISP2300, .segs = { 0x800, 0 }, },
3707	{ .name = FW_FILE_ISP2322, .segs = { 0x800, 0x1c000, 0x1e000, 0 }, },
3708	{ .name = FW_FILE_ISP24XX, },
3709	{ .name = FW_FILE_ISP25XX, },
3710	{ .name = FW_FILE_ISP81XX, },
3711	{ .name = FW_FILE_ISP82XX, },
3712};
3713
3714struct fw_blob *
3715qla2x00_request_firmware(scsi_qla_host_t *vha)
3716{
3717	struct qla_hw_data *ha = vha->hw;
3718	struct fw_blob *blob;
3719
3720	blob = NULL;
3721	if (IS_QLA2100(ha)) {
3722		blob = &qla_fw_blobs[FW_ISP21XX];
3723	} else if (IS_QLA2200(ha)) {
3724		blob = &qla_fw_blobs[FW_ISP22XX];
3725	} else if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
3726		blob = &qla_fw_blobs[FW_ISP2300];
3727	} else if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
3728		blob = &qla_fw_blobs[FW_ISP2322];
3729	} else if (IS_QLA24XX_TYPE(ha)) {
3730		blob = &qla_fw_blobs[FW_ISP24XX];
3731	} else if (IS_QLA25XX(ha)) {
3732		blob = &qla_fw_blobs[FW_ISP25XX];
3733	} else if (IS_QLA81XX(ha)) {
3734		blob = &qla_fw_blobs[FW_ISP81XX];
3735	} else if (IS_QLA82XX(ha)) {
3736		blob = &qla_fw_blobs[FW_ISP82XX];
3737	}
3738
3739	mutex_lock(&qla_fw_lock);
3740	if (blob->fw)
3741		goto out;
3742
3743	if (request_firmware(&blob->fw, blob->name, &ha->pdev->dev)) {
3744		DEBUG2(printk("scsi(%ld): Failed to load firmware image "
3745		    "(%s).\n", vha->host_no, blob->name));
3746		blob->fw = NULL;
3747		blob = NULL;
3748		goto out;
3749	}
3750
3751out:
3752	mutex_unlock(&qla_fw_lock);
3753	return blob;
3754}
3755
3756static void
3757qla2x00_release_firmware(void)
3758{
3759	int idx;
3760
3761	mutex_lock(&qla_fw_lock);
3762	for (idx = 0; idx < FW_BLOBS; idx++)
3763		if (qla_fw_blobs[idx].fw)
3764			release_firmware(qla_fw_blobs[idx].fw);
3765	mutex_unlock(&qla_fw_lock);
3766}
3767
3768static pci_ers_result_t
3769qla2xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
3770{
3771	scsi_qla_host_t *vha = pci_get_drvdata(pdev);
3772	struct qla_hw_data *ha = vha->hw;
3773
3774	DEBUG2(qla_printk(KERN_WARNING, ha, "error_detected:state %x\n",
3775	    state));
3776
3777	switch (state) {
3778	case pci_channel_io_normal:
3779		ha->flags.eeh_busy = 0;
3780		return PCI_ERS_RESULT_CAN_RECOVER;
3781	case pci_channel_io_frozen:
3782		ha->flags.eeh_busy = 1;
3783		qla2x00_free_irqs(vha);
3784		pci_disable_device(pdev);
3785		return PCI_ERS_RESULT_NEED_RESET;
3786	case pci_channel_io_perm_failure:
3787		ha->flags.pci_channel_io_perm_failure = 1;
3788		qla2x00_abort_all_cmds(vha, DID_NO_CONNECT << 16);
3789		return PCI_ERS_RESULT_DISCONNECT;
3790	}
3791	return PCI_ERS_RESULT_NEED_RESET;
3792}
3793
3794static pci_ers_result_t
3795qla2xxx_pci_mmio_enabled(struct pci_dev *pdev)
3796{
3797	int risc_paused = 0;
3798	uint32_t stat;
3799	unsigned long flags;
3800	scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
3801	struct qla_hw_data *ha = base_vha->hw;
3802	struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
3803	struct device_reg_24xx __iomem *reg24 = &ha->iobase->isp24;
3804
3805	spin_lock_irqsave(&ha->hardware_lock, flags);
3806	if (IS_QLA2100(ha) || IS_QLA2200(ha)){
3807		stat = RD_REG_DWORD(&reg->hccr);
3808		if (stat & HCCR_RISC_PAUSE)
3809			risc_paused = 1;
3810	} else if (IS_QLA23XX(ha)) {
3811		stat = RD_REG_DWORD(&reg->u.isp2300.host_status);
3812		if (stat & HSR_RISC_PAUSED)
3813			risc_paused = 1;
3814	} else if (IS_FWI2_CAPABLE(ha)) {
3815		stat = RD_REG_DWORD(&reg24->host_status);
3816		if (stat & HSRX_RISC_PAUSED)
3817			risc_paused = 1;
3818	}
3819	spin_unlock_irqrestore(&ha->hardware_lock, flags);
3820
3821	if (risc_paused) {
3822		qla_printk(KERN_INFO, ha, "RISC paused -- mmio_enabled, "
3823		    "Dumping firmware!\n");
3824		ha->isp_ops->fw_dump(base_vha, 0);
3825
3826		return PCI_ERS_RESULT_NEED_RESET;
3827	} else
3828		return PCI_ERS_RESULT_RECOVERED;
3829}
3830
3831static pci_ers_result_t
3832qla2xxx_pci_slot_reset(struct pci_dev *pdev)
3833{
3834	pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
3835	scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
3836	struct qla_hw_data *ha = base_vha->hw;
3837	struct rsp_que *rsp;
3838	int rc, retries = 10;
3839
3840	DEBUG17(qla_printk(KERN_WARNING, ha, "slot_reset\n"));
3841
3842	pdev->error_state = pci_channel_io_normal;
3843
3844	pci_restore_state(pdev);
3845
3846	/* pci_restore_state() clears the saved_state flag of the device
3847	 * save restored state which resets saved_state flag
3848	 */
3849	pci_save_state(pdev);
3850
3851	if (ha->mem_only)
3852		rc = pci_enable_device_mem(pdev);
3853	else
3854		rc = pci_enable_device(pdev);
3855
3856	if (rc) {
3857		qla_printk(KERN_WARNING, ha,
3858		    "Can't re-enable PCI device after reset.\n");
3859		return ret;
3860	}
3861
3862	rsp = ha->rsp_q_map[0];
3863	if (qla2x00_request_irqs(ha, rsp))
3864		return ret;
3865
3866	if (ha->isp_ops->pci_config(base_vha))
3867		return ret;
3868
3869	while (ha->flags.mbox_busy && retries--)
3870		msleep(1000);
3871
3872	set_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
3873	if (ha->isp_ops->abort_isp(base_vha) == QLA_SUCCESS)
3874		ret =  PCI_ERS_RESULT_RECOVERED;
3875	clear_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
3876
3877
3878	DEBUG17(qla_printk(KERN_WARNING, ha,
3879	    "slot_reset-return:ret=%x\n", ret));
3880
3881	return ret;
3882}
3883
3884static void
3885qla2xxx_pci_resume(struct pci_dev *pdev)
3886{
3887	scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
3888	struct qla_hw_data *ha = base_vha->hw;
3889	int ret;
3890
3891	DEBUG17(qla_printk(KERN_WARNING, ha, "pci_resume\n"));
3892
3893	ret = qla2x00_wait_for_hba_online(base_vha);
3894	if (ret != QLA_SUCCESS) {
3895		qla_printk(KERN_ERR, ha,
3896		    "the device failed to resume I/O "
3897		    "from slot/link_reset");
3898	}
3899
3900	pci_cleanup_aer_uncorrect_error_status(pdev);
3901
3902	ha->flags.eeh_busy = 0;
3903}
3904
3905static struct pci_error_handlers qla2xxx_err_handler = {
3906	.error_detected = qla2xxx_pci_error_detected,
3907	.mmio_enabled = qla2xxx_pci_mmio_enabled,
3908	.slot_reset = qla2xxx_pci_slot_reset,
3909	.resume = qla2xxx_pci_resume,
3910};
3911
3912static struct pci_device_id qla2xxx_pci_tbl[] = {
3913	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2100) },
3914	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2200) },
3915	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2300) },
3916	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2312) },
3917	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2322) },
3918	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP6312) },
3919	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP6322) },
3920	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2422) },
3921	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2432) },
3922	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP8432) },
3923	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP5422) },
3924	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP5432) },
3925	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2532) },
3926	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP8001) },
3927	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP8021) },
3928	{ 0 },
3929};
3930MODULE_DEVICE_TABLE(pci, qla2xxx_pci_tbl);
3931
3932static struct pci_driver qla2xxx_pci_driver = {
3933	.name		= QLA2XXX_DRIVER_NAME,
3934	.driver		= {
3935		.owner		= THIS_MODULE,
3936	},
3937	.id_table	= qla2xxx_pci_tbl,
3938	.probe		= qla2x00_probe_one,
3939	.remove		= qla2x00_remove_one,
3940	.err_handler	= &qla2xxx_err_handler,
3941};
3942
3943static struct file_operations apidev_fops = {
3944	.owner = THIS_MODULE,
3945};
3946
3947/**
3948 * qla2x00_module_init - Module initialization.
3949 **/
3950static int __init
3951qla2x00_module_init(void)
3952{
3953	int ret = 0;
3954
3955	/* Allocate cache for SRBs. */
3956	srb_cachep = kmem_cache_create("qla2xxx_srbs", sizeof(srb_t), 0,
3957	    SLAB_HWCACHE_ALIGN, NULL);
3958	if (srb_cachep == NULL) {
3959		printk(KERN_ERR
3960		    "qla2xxx: Unable to allocate SRB cache...Failing load!\n");
3961		return -ENOMEM;
3962	}
3963
3964	/* Derive version string. */
3965	strcpy(qla2x00_version_str, QLA2XXX_VERSION);
3966	if (ql2xextended_error_logging)
3967		strcat(qla2x00_version_str, "-debug");
3968
3969	qla2xxx_transport_template =
3970	    fc_attach_transport(&qla2xxx_transport_functions);
3971	if (!qla2xxx_transport_template) {
3972		kmem_cache_destroy(srb_cachep);
3973		return -ENODEV;
3974	}
3975
3976	apidev_major = register_chrdev(0, QLA2XXX_APIDEV, &apidev_fops);
3977	if (apidev_major < 0) {
3978		printk(KERN_WARNING "qla2xxx: Unable to register char device "
3979		    "%s\n", QLA2XXX_APIDEV);
3980	}
3981
3982	qla2xxx_transport_vport_template =
3983	    fc_attach_transport(&qla2xxx_transport_vport_functions);
3984	if (!qla2xxx_transport_vport_template) {
3985		kmem_cache_destroy(srb_cachep);
3986		fc_release_transport(qla2xxx_transport_template);
3987		return -ENODEV;
3988	}
3989
3990	printk(KERN_INFO "QLogic Fibre Channel HBA Driver: %s\n",
3991	    qla2x00_version_str);
3992	ret = pci_register_driver(&qla2xxx_pci_driver);
3993	if (ret) {
3994		kmem_cache_destroy(srb_cachep);
3995		fc_release_transport(qla2xxx_transport_template);
3996		fc_release_transport(qla2xxx_transport_vport_template);
3997	}
3998	return ret;
3999}
4000
4001/**
4002 * qla2x00_module_exit - Module cleanup.
4003 **/
4004static void __exit
4005qla2x00_module_exit(void)
4006{
4007	unregister_chrdev(apidev_major, QLA2XXX_APIDEV);
4008	pci_unregister_driver(&qla2xxx_pci_driver);
4009	qla2x00_release_firmware();
4010	kmem_cache_destroy(srb_cachep);
4011	if (ctx_cachep)
4012		kmem_cache_destroy(ctx_cachep);
4013	fc_release_transport(qla2xxx_transport_template);
4014	fc_release_transport(qla2xxx_transport_vport_template);
4015}
4016
4017module_init(qla2x00_module_init);
4018module_exit(qla2x00_module_exit);
4019
4020MODULE_AUTHOR("QLogic Corporation");
4021MODULE_DESCRIPTION("QLogic Fibre Channel HBA Driver");
4022MODULE_LICENSE("GPL");
4023MODULE_VERSION(QLA2XXX_VERSION);
4024MODULE_FIRMWARE(FW_FILE_ISP21XX);
4025MODULE_FIRMWARE(FW_FILE_ISP22XX);
4026MODULE_FIRMWARE(FW_FILE_ISP2300);
4027MODULE_FIRMWARE(FW_FILE_ISP2322);
4028MODULE_FIRMWARE(FW_FILE_ISP24XX);
4029MODULE_FIRMWARE(FW_FILE_ISP25XX);
4030