xref: /openbmc/linux/drivers/scsi/qla2xxx/qla_os.c (revision c4c3c32d)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * QLogic Fibre Channel HBA Driver
4  * Copyright (c)  2003-2014 QLogic Corporation
5  */
6 #include "qla_def.h"
7 
8 #include <linux/moduleparam.h>
9 #include <linux/vmalloc.h>
10 #include <linux/delay.h>
11 #include <linux/kthread.h>
12 #include <linux/mutex.h>
13 #include <linux/kobject.h>
14 #include <linux/slab.h>
15 #include <linux/blk-mq-pci.h>
16 #include <linux/refcount.h>
17 #include <linux/crash_dump.h>
18 #include <linux/trace_events.h>
19 #include <linux/trace.h>
20 
21 #include <scsi/scsi_tcq.h>
22 #include <scsi/scsicam.h>
23 #include <scsi/scsi_transport.h>
24 #include <scsi/scsi_transport_fc.h>
25 
26 #include "qla_target.h"
27 
28 /*
29  * Driver version
30  */
31 char qla2x00_version_str[40];
32 
33 static int apidev_major;
34 
35 /*
36  * SRB allocation cache
37  */
38 struct kmem_cache *srb_cachep;
39 
40 static struct trace_array *qla_trc_array;
41 
42 int ql2xfulldump_on_mpifail;
43 module_param(ql2xfulldump_on_mpifail, int, S_IRUGO | S_IWUSR);
44 MODULE_PARM_DESC(ql2xfulldump_on_mpifail,
45 		 "Set this to take full dump on MPI hang.");
46 
47 int ql2xenforce_iocb_limit = 1;
48 module_param(ql2xenforce_iocb_limit, int, S_IRUGO | S_IWUSR);
49 MODULE_PARM_DESC(ql2xenforce_iocb_limit,
50 		 "Enforce IOCB throttling, to avoid FW congestion. (default: 1)");
51 
52 /*
53  * CT6 CTX allocation cache
54  */
55 static struct kmem_cache *ctx_cachep;
56 /*
57  * error level for logging
58  */
59 uint ql_errlev = 0x8001;
60 
61 int ql2xsecenable;
62 module_param(ql2xsecenable, int, S_IRUGO);
63 MODULE_PARM_DESC(ql2xsecenable,
64 	"Enable/disable security. 0(Default) - Security disabled. 1 - Security enabled.");
65 
66 static int ql2xenableclass2;
67 module_param(ql2xenableclass2, int, S_IRUGO|S_IRUSR);
68 MODULE_PARM_DESC(ql2xenableclass2,
69 		"Specify if Class 2 operations are supported from the very "
70 		"beginning. Default is 0 - class 2 not supported.");
71 
72 
73 int ql2xlogintimeout = 20;
74 module_param(ql2xlogintimeout, int, S_IRUGO);
75 MODULE_PARM_DESC(ql2xlogintimeout,
76 		"Login timeout value in seconds.");
77 
78 int qlport_down_retry;
79 module_param(qlport_down_retry, int, S_IRUGO);
80 MODULE_PARM_DESC(qlport_down_retry,
81 		"Maximum number of command retries to a port that returns "
82 		"a PORT-DOWN status.");
83 
84 int ql2xplogiabsentdevice;
85 module_param(ql2xplogiabsentdevice, int, S_IRUGO|S_IWUSR);
86 MODULE_PARM_DESC(ql2xplogiabsentdevice,
87 		"Option to enable PLOGI to devices that are not present after "
88 		"a Fabric scan.  This is needed for several broken switches. "
89 		"Default is 0 - no PLOGI. 1 - perform PLOGI.");
90 
91 int ql2xloginretrycount;
92 module_param(ql2xloginretrycount, int, S_IRUGO);
93 MODULE_PARM_DESC(ql2xloginretrycount,
94 		"Specify an alternate value for the NVRAM login retry count.");
95 
96 int ql2xallocfwdump = 1;
97 module_param(ql2xallocfwdump, int, S_IRUGO);
98 MODULE_PARM_DESC(ql2xallocfwdump,
99 		"Option to enable allocation of memory for a firmware dump "
100 		"during HBA initialization.  Memory allocation requirements "
101 		"vary by ISP type.  Default is 1 - allocate memory.");
102 
103 int ql2xextended_error_logging;
104 module_param(ql2xextended_error_logging, int, S_IRUGO|S_IWUSR);
105 module_param_named(logging, ql2xextended_error_logging, int, S_IRUGO|S_IWUSR);
106 MODULE_PARM_DESC(ql2xextended_error_logging,
107 		"Option to enable extended error logging,\n"
108 		"\t\tDefault is 0 - no logging.  0x40000000 - Module Init & Probe.\n"
109 		"\t\t0x20000000 - Mailbox Cmnds. 0x10000000 - Device Discovery.\n"
110 		"\t\t0x08000000 - IO tracing.    0x04000000 - DPC Thread.\n"
111 		"\t\t0x02000000 - Async events.  0x01000000 - Timer routines.\n"
112 		"\t\t0x00800000 - User space.    0x00400000 - Task Management.\n"
113 		"\t\t0x00200000 - AER/EEH.       0x00100000 - Multi Q.\n"
114 		"\t\t0x00080000 - P3P Specific.  0x00040000 - Virtual Port.\n"
115 		"\t\t0x00020000 - Buffer Dump.   0x00010000 - Misc.\n"
116 		"\t\t0x00008000 - Verbose.       0x00004000 - Target.\n"
117 		"\t\t0x00002000 - Target Mgmt.   0x00001000 - Target TMF.\n"
118 		"\t\t0x7fffffff - For enabling all logs, can be too many logs.\n"
119 		"\t\t0x1e400000 - Preferred value for capturing essential "
120 		"debug information (equivalent to old "
121 		"ql2xextended_error_logging=1).\n"
122 		"\t\tDo LOGICAL OR of the value to enable more than one level");
123 
124 int ql2xextended_error_logging_ktrace = 1;
125 module_param(ql2xextended_error_logging_ktrace, int, S_IRUGO|S_IWUSR);
126 MODULE_PARM_DESC(ql2xextended_error_logging_ktrace,
127 		"Same BIT definition as ql2xextended_error_logging, but used to control logging to kernel trace buffer (default=1).\n");
128 
129 int ql2xshiftctondsd = 6;
130 module_param(ql2xshiftctondsd, int, S_IRUGO);
131 MODULE_PARM_DESC(ql2xshiftctondsd,
132 		"Set to control shifting of command type processing "
133 		"based on total number of SG elements.");
134 
135 int ql2xfdmienable = 1;
136 module_param(ql2xfdmienable, int, S_IRUGO|S_IWUSR);
137 module_param_named(fdmi, ql2xfdmienable, int, S_IRUGO|S_IWUSR);
138 MODULE_PARM_DESC(ql2xfdmienable,
139 		"Enables FDMI registrations. "
140 		"0 - no FDMI registrations. "
141 		"1 - provide FDMI registrations (default).");
142 
143 #define MAX_Q_DEPTH	64
144 static int ql2xmaxqdepth = MAX_Q_DEPTH;
145 module_param(ql2xmaxqdepth, int, S_IRUGO|S_IWUSR);
146 MODULE_PARM_DESC(ql2xmaxqdepth,
147 		"Maximum queue depth to set for each LUN. "
148 		"Default is 64.");
149 
150 int ql2xenabledif = 2;
151 module_param(ql2xenabledif, int, S_IRUGO);
152 MODULE_PARM_DESC(ql2xenabledif,
153 		" Enable T10-CRC-DIF:\n"
154 		" Default is 2.\n"
155 		"  0 -- No DIF Support\n"
156 		"  1 -- Enable DIF for all types\n"
157 		"  2 -- Enable DIF for all types, except Type 0.\n");
158 
159 #if (IS_ENABLED(CONFIG_NVME_FC))
160 int ql2xnvmeenable = 1;
161 #else
162 int ql2xnvmeenable;
163 #endif
164 module_param(ql2xnvmeenable, int, 0644);
165 MODULE_PARM_DESC(ql2xnvmeenable,
166     "Enables NVME support. "
167     "0 - no NVMe.  Default is Y");
168 
169 int ql2xenablehba_err_chk = 2;
170 module_param(ql2xenablehba_err_chk, int, S_IRUGO|S_IWUSR);
171 MODULE_PARM_DESC(ql2xenablehba_err_chk,
172 		" Enable T10-CRC-DIF Error isolation by HBA:\n"
173 		" Default is 2.\n"
174 		"  0 -- Error isolation disabled\n"
175 		"  1 -- Error isolation enabled only for DIX Type 0\n"
176 		"  2 -- Error isolation enabled for all Types\n");
177 
178 int ql2xiidmaenable = 1;
179 module_param(ql2xiidmaenable, int, S_IRUGO);
180 MODULE_PARM_DESC(ql2xiidmaenable,
181 		"Enables iIDMA settings "
182 		"Default is 1 - perform iIDMA. 0 - no iIDMA.");
183 
184 int ql2xmqsupport = 1;
185 module_param(ql2xmqsupport, int, S_IRUGO);
186 MODULE_PARM_DESC(ql2xmqsupport,
187 		"Enable on demand multiple queue pairs support "
188 		"Default is 1 for supported. "
189 		"Set it to 0 to turn off mq qpair support.");
190 
191 int ql2xfwloadbin;
192 module_param(ql2xfwloadbin, int, S_IRUGO|S_IWUSR);
193 module_param_named(fwload, ql2xfwloadbin, int, S_IRUGO|S_IWUSR);
194 MODULE_PARM_DESC(ql2xfwloadbin,
195 		"Option to specify location from which to load ISP firmware:.\n"
196 		" 2 -- load firmware via the request_firmware() (hotplug).\n"
197 		"      interface.\n"
198 		" 1 -- load firmware from flash.\n"
199 		" 0 -- use default semantics.\n");
200 
201 int ql2xetsenable;
202 module_param(ql2xetsenable, int, S_IRUGO);
203 MODULE_PARM_DESC(ql2xetsenable,
204 		"Enables firmware ETS burst."
205 		"Default is 0 - skip ETS enablement.");
206 
207 int ql2xdbwr = 1;
208 module_param(ql2xdbwr, int, S_IRUGO|S_IWUSR);
209 MODULE_PARM_DESC(ql2xdbwr,
210 		"Option to specify scheme for request queue posting.\n"
211 		" 0 -- Regular doorbell.\n"
212 		" 1 -- CAMRAM doorbell (faster).\n");
213 
214 int ql2xgffidenable;
215 module_param(ql2xgffidenable, int, S_IRUGO);
216 MODULE_PARM_DESC(ql2xgffidenable,
217 		"Enables GFF_ID checks of port type. "
218 		"Default is 0 - Do not use GFF_ID information.");
219 
220 int ql2xasynctmfenable = 1;
221 module_param(ql2xasynctmfenable, int, S_IRUGO);
222 MODULE_PARM_DESC(ql2xasynctmfenable,
223 		"Enables issue of TM IOCBs asynchronously via IOCB mechanism"
224 		"Default is 1 - Issue TM IOCBs via mailbox mechanism.");
225 
226 int ql2xdontresethba;
227 module_param(ql2xdontresethba, int, S_IRUGO|S_IWUSR);
228 MODULE_PARM_DESC(ql2xdontresethba,
229 		"Option to specify reset behaviour.\n"
230 		" 0 (Default) -- Reset on failure.\n"
231 		" 1 -- Do not reset on failure.\n");
232 
233 uint64_t ql2xmaxlun = MAX_LUNS;
234 module_param(ql2xmaxlun, ullong, S_IRUGO);
235 MODULE_PARM_DESC(ql2xmaxlun,
236 		"Defines the maximum LU number to register with the SCSI "
237 		"midlayer. Default is 65535.");
238 
239 int ql2xmdcapmask = 0x1F;
240 module_param(ql2xmdcapmask, int, S_IRUGO);
241 MODULE_PARM_DESC(ql2xmdcapmask,
242 		"Set the Minidump driver capture mask level. "
243 		"Default is 0x1F - Can be set to 0x3, 0x7, 0xF, 0x1F, 0x7F.");
244 
245 int ql2xmdenable = 1;
246 module_param(ql2xmdenable, int, S_IRUGO);
247 MODULE_PARM_DESC(ql2xmdenable,
248 		"Enable/disable MiniDump. "
249 		"0 - MiniDump disabled. "
250 		"1 (Default) - MiniDump enabled.");
251 
252 int ql2xexlogins;
253 module_param(ql2xexlogins, uint, S_IRUGO|S_IWUSR);
254 MODULE_PARM_DESC(ql2xexlogins,
255 		 "Number of extended Logins. "
256 		 "0 (Default)- Disabled.");
257 
258 int ql2xexchoffld = 1024;
259 module_param(ql2xexchoffld, uint, 0644);
260 MODULE_PARM_DESC(ql2xexchoffld,
261 	"Number of target exchanges.");
262 
263 int ql2xiniexchg = 1024;
264 module_param(ql2xiniexchg, uint, 0644);
265 MODULE_PARM_DESC(ql2xiniexchg,
266 	"Number of initiator exchanges.");
267 
268 int ql2xfwholdabts;
269 module_param(ql2xfwholdabts, int, S_IRUGO);
270 MODULE_PARM_DESC(ql2xfwholdabts,
271 		"Allow FW to hold status IOCB until ABTS rsp received. "
272 		"0 (Default) Do not set fw option. "
273 		"1 - Set fw option to hold ABTS.");
274 
275 int ql2xmvasynctoatio = 1;
276 module_param(ql2xmvasynctoatio, int, S_IRUGO|S_IWUSR);
277 MODULE_PARM_DESC(ql2xmvasynctoatio,
278 		"Move PUREX, ABTS RX and RIDA IOCBs to ATIOQ"
279 		"0 (Default). Do not move IOCBs"
280 		"1 - Move IOCBs.");
281 
282 int ql2xautodetectsfp = 1;
283 module_param(ql2xautodetectsfp, int, 0444);
284 MODULE_PARM_DESC(ql2xautodetectsfp,
285 		 "Detect SFP range and set appropriate distance.\n"
286 		 "1 (Default): Enable\n");
287 
288 int ql2xenablemsix = 1;
289 module_param(ql2xenablemsix, int, 0444);
290 MODULE_PARM_DESC(ql2xenablemsix,
291 		 "Set to enable MSI or MSI-X interrupt mechanism.\n"
292 		 " Default is 1, enable MSI-X interrupt mechanism.\n"
293 		 " 0 -- enable traditional pin-based mechanism.\n"
294 		 " 1 -- enable MSI-X interrupt mechanism.\n"
295 		 " 2 -- enable MSI interrupt mechanism.\n");
296 
297 int qla2xuseresexchforels;
298 module_param(qla2xuseresexchforels, int, 0444);
299 MODULE_PARM_DESC(qla2xuseresexchforels,
300 		 "Reserve 1/2 of emergency exchanges for ELS.\n"
301 		 " 0 (default): disabled");
302 
303 static int ql2xprotmask;
304 module_param(ql2xprotmask, int, 0644);
305 MODULE_PARM_DESC(ql2xprotmask,
306 		 "Override DIF/DIX protection capabilities mask\n"
307 		 "Default is 0 which sets protection mask based on "
308 		 "capabilities reported by HBA firmware.\n");
309 
310 static int ql2xprotguard;
311 module_param(ql2xprotguard, int, 0644);
312 MODULE_PARM_DESC(ql2xprotguard, "Override choice of DIX checksum\n"
313 		 "  0 -- Let HBA firmware decide\n"
314 		 "  1 -- Force T10 CRC\n"
315 		 "  2 -- Force IP checksum\n");
316 
317 int ql2xdifbundlinginternalbuffers;
318 module_param(ql2xdifbundlinginternalbuffers, int, 0644);
319 MODULE_PARM_DESC(ql2xdifbundlinginternalbuffers,
320     "Force using internal buffers for DIF information\n"
321     "0 (Default). Based on check.\n"
322     "1 Force using internal buffers\n");
323 
324 int ql2xsmartsan;
325 module_param(ql2xsmartsan, int, 0444);
326 module_param_named(smartsan, ql2xsmartsan, int, 0444);
327 MODULE_PARM_DESC(ql2xsmartsan,
328 		"Send SmartSAN Management Attributes for FDMI Registration."
329 		" Default is 0 - No SmartSAN registration,"
330 		" 1 - Register SmartSAN Management Attributes.");
331 
332 int ql2xrdpenable;
333 module_param(ql2xrdpenable, int, 0444);
334 module_param_named(rdpenable, ql2xrdpenable, int, 0444);
335 MODULE_PARM_DESC(ql2xrdpenable,
336 		"Enables RDP responses. "
337 		"0 - no RDP responses (default). "
338 		"1 - provide RDP responses.");
339 int ql2xabts_wait_nvme = 1;
340 module_param(ql2xabts_wait_nvme, int, 0444);
341 MODULE_PARM_DESC(ql2xabts_wait_nvme,
342 		 "To wait for ABTS response on I/O timeouts for NVMe. (default: 1)");
343 
344 
345 static u32 ql2xdelay_before_pci_error_handling = 5;
346 module_param(ql2xdelay_before_pci_error_handling, uint, 0644);
347 MODULE_PARM_DESC(ql2xdelay_before_pci_error_handling,
348 	"Number of seconds delayed before qla begin PCI error self-handling (default: 5).\n");
349 
350 static void qla2x00_clear_drv_active(struct qla_hw_data *);
351 static void qla2x00_free_device(scsi_qla_host_t *);
352 static void qla2xxx_map_queues(struct Scsi_Host *shost);
353 static void qla2x00_destroy_deferred_work(struct qla_hw_data *);
354 
355 u32 ql2xnvme_queues = DEF_NVME_HW_QUEUES;
356 module_param(ql2xnvme_queues, uint, S_IRUGO);
357 MODULE_PARM_DESC(ql2xnvme_queues,
358 	"Number of NVMe Queues that can be configured.\n"
359 	"Final value will be min(ql2xnvme_queues, num_cpus,num_chip_queues)\n"
360 	"1 - Minimum number of queues supported\n"
361 	"8 - Default value");
362 
363 int ql2xfc2target = 1;
364 module_param(ql2xfc2target, int, 0444);
365 MODULE_PARM_DESC(qla2xfc2target,
366 		  "Enables FC2 Target support. "
367 		  "0 - FC2 Target support is disabled. "
368 		  "1 - FC2 Target support is enabled (default).");
369 
370 static struct scsi_transport_template *qla2xxx_transport_template = NULL;
371 struct scsi_transport_template *qla2xxx_transport_vport_template = NULL;
372 
373 /* TODO Convert to inlines
374  *
375  * Timer routines
376  */
377 
378 __inline__ void
379 qla2x00_start_timer(scsi_qla_host_t *vha, unsigned long interval)
380 {
381 	timer_setup(&vha->timer, qla2x00_timer, 0);
382 	vha->timer.expires = jiffies + interval * HZ;
383 	add_timer(&vha->timer);
384 	vha->timer_active = 1;
385 }
386 
387 static inline void
388 qla2x00_restart_timer(scsi_qla_host_t *vha, unsigned long interval)
389 {
390 	/* Currently used for 82XX only. */
391 	if (vha->device_flags & DFLG_DEV_FAILED) {
392 		ql_dbg(ql_dbg_timer, vha, 0x600d,
393 		    "Device in a failed state, returning.\n");
394 		return;
395 	}
396 
397 	mod_timer(&vha->timer, jiffies + interval * HZ);
398 }
399 
400 static __inline__ void
401 qla2x00_stop_timer(scsi_qla_host_t *vha)
402 {
403 	del_timer_sync(&vha->timer);
404 	vha->timer_active = 0;
405 }
406 
407 static int qla2x00_do_dpc(void *data);
408 
409 static void qla2x00_rst_aen(scsi_qla_host_t *);
410 
411 static int qla2x00_mem_alloc(struct qla_hw_data *, uint16_t, uint16_t,
412 	struct req_que **, struct rsp_que **);
413 static void qla2x00_free_fw_dump(struct qla_hw_data *);
414 static void qla2x00_mem_free(struct qla_hw_data *);
415 int qla2xxx_mqueuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd,
416 	struct qla_qpair *qpair);
417 
418 /* -------------------------------------------------------------------------- */
419 static void qla_init_base_qpair(struct scsi_qla_host *vha, struct req_que *req,
420     struct rsp_que *rsp)
421 {
422 	struct qla_hw_data *ha = vha->hw;
423 
424 	rsp->qpair = ha->base_qpair;
425 	rsp->req = req;
426 	ha->base_qpair->hw = ha;
427 	ha->base_qpair->req = req;
428 	ha->base_qpair->rsp = rsp;
429 	ha->base_qpair->vha = vha;
430 	ha->base_qpair->qp_lock_ptr = &ha->hardware_lock;
431 	ha->base_qpair->use_shadow_reg = IS_SHADOW_REG_CAPABLE(ha) ? 1 : 0;
432 	ha->base_qpair->msix = &ha->msix_entries[QLA_MSIX_RSP_Q];
433 	ha->base_qpair->srb_mempool = ha->srb_mempool;
434 	INIT_LIST_HEAD(&ha->base_qpair->hints_list);
435 	ha->base_qpair->enable_class_2 = ql2xenableclass2;
436 	/* init qpair to this cpu. Will adjust at run time. */
437 	qla_cpu_update(rsp->qpair, raw_smp_processor_id());
438 	ha->base_qpair->pdev = ha->pdev;
439 
440 	if (IS_QLA27XX(ha) || IS_QLA83XX(ha) || IS_QLA28XX(ha))
441 		ha->base_qpair->reqq_start_iocbs = qla_83xx_start_iocbs;
442 }
443 
444 static int qla2x00_alloc_queues(struct qla_hw_data *ha, struct req_que *req,
445 				struct rsp_que *rsp)
446 {
447 	scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
448 
449 	ha->req_q_map = kcalloc(ha->max_req_queues, sizeof(struct req_que *),
450 				GFP_KERNEL);
451 	if (!ha->req_q_map) {
452 		ql_log(ql_log_fatal, vha, 0x003b,
453 		    "Unable to allocate memory for request queue ptrs.\n");
454 		goto fail_req_map;
455 	}
456 
457 	ha->rsp_q_map = kcalloc(ha->max_rsp_queues, sizeof(struct rsp_que *),
458 				GFP_KERNEL);
459 	if (!ha->rsp_q_map) {
460 		ql_log(ql_log_fatal, vha, 0x003c,
461 		    "Unable to allocate memory for response queue ptrs.\n");
462 		goto fail_rsp_map;
463 	}
464 
465 	ha->base_qpair = kzalloc(sizeof(struct qla_qpair), GFP_KERNEL);
466 	if (ha->base_qpair == NULL) {
467 		ql_log(ql_log_warn, vha, 0x00e0,
468 		    "Failed to allocate base queue pair memory.\n");
469 		goto fail_base_qpair;
470 	}
471 
472 	qla_init_base_qpair(vha, req, rsp);
473 
474 	if ((ql2xmqsupport || ql2xnvmeenable) && ha->max_qpairs) {
475 		ha->queue_pair_map = kcalloc(ha->max_qpairs, sizeof(struct qla_qpair *),
476 			GFP_KERNEL);
477 		if (!ha->queue_pair_map) {
478 			ql_log(ql_log_fatal, vha, 0x0180,
479 			    "Unable to allocate memory for queue pair ptrs.\n");
480 			goto fail_qpair_map;
481 		}
482 		if (qla_mapq_alloc_qp_cpu_map(ha) != 0) {
483 			kfree(ha->queue_pair_map);
484 			ha->queue_pair_map = NULL;
485 			goto fail_qpair_map;
486 		}
487 	}
488 
489 	/*
490 	 * Make sure we record at least the request and response queue zero in
491 	 * case we need to free them if part of the probe fails.
492 	 */
493 	ha->rsp_q_map[0] = rsp;
494 	ha->req_q_map[0] = req;
495 	set_bit(0, ha->rsp_qid_map);
496 	set_bit(0, ha->req_qid_map);
497 	return 0;
498 
499 fail_qpair_map:
500 	kfree(ha->base_qpair);
501 	ha->base_qpair = NULL;
502 fail_base_qpair:
503 	kfree(ha->rsp_q_map);
504 	ha->rsp_q_map = NULL;
505 fail_rsp_map:
506 	kfree(ha->req_q_map);
507 	ha->req_q_map = NULL;
508 fail_req_map:
509 	return -ENOMEM;
510 }
511 
512 static void qla2x00_free_req_que(struct qla_hw_data *ha, struct req_que *req)
513 {
514 	if (IS_QLAFX00(ha)) {
515 		if (req && req->ring_fx00)
516 			dma_free_coherent(&ha->pdev->dev,
517 			    (req->length_fx00 + 1) * sizeof(request_t),
518 			    req->ring_fx00, req->dma_fx00);
519 	} else if (req && req->ring)
520 		dma_free_coherent(&ha->pdev->dev,
521 		(req->length + 1) * sizeof(request_t),
522 		req->ring, req->dma);
523 
524 	if (req)
525 		kfree(req->outstanding_cmds);
526 
527 	kfree(req);
528 }
529 
530 static void qla2x00_free_rsp_que(struct qla_hw_data *ha, struct rsp_que *rsp)
531 {
532 	if (IS_QLAFX00(ha)) {
533 		if (rsp && rsp->ring_fx00)
534 			dma_free_coherent(&ha->pdev->dev,
535 			    (rsp->length_fx00 + 1) * sizeof(request_t),
536 			    rsp->ring_fx00, rsp->dma_fx00);
537 	} else if (rsp && rsp->ring) {
538 		dma_free_coherent(&ha->pdev->dev,
539 		(rsp->length + 1) * sizeof(response_t),
540 		rsp->ring, rsp->dma);
541 	}
542 	kfree(rsp);
543 }
544 
545 static void qla2x00_free_queues(struct qla_hw_data *ha)
546 {
547 	struct req_que *req;
548 	struct rsp_que *rsp;
549 	int cnt;
550 	unsigned long flags;
551 
552 	if (ha->queue_pair_map) {
553 		kfree(ha->queue_pair_map);
554 		ha->queue_pair_map = NULL;
555 	}
556 	if (ha->base_qpair) {
557 		kfree(ha->base_qpair);
558 		ha->base_qpair = NULL;
559 	}
560 
561 	qla_mapq_free_qp_cpu_map(ha);
562 	spin_lock_irqsave(&ha->hardware_lock, flags);
563 	for (cnt = 0; cnt < ha->max_req_queues; cnt++) {
564 		if (!test_bit(cnt, ha->req_qid_map))
565 			continue;
566 
567 		req = ha->req_q_map[cnt];
568 		clear_bit(cnt, ha->req_qid_map);
569 		ha->req_q_map[cnt] = NULL;
570 
571 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
572 		qla2x00_free_req_que(ha, req);
573 		spin_lock_irqsave(&ha->hardware_lock, flags);
574 	}
575 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
576 
577 	kfree(ha->req_q_map);
578 	ha->req_q_map = NULL;
579 
580 
581 	spin_lock_irqsave(&ha->hardware_lock, flags);
582 	for (cnt = 0; cnt < ha->max_rsp_queues; cnt++) {
583 		if (!test_bit(cnt, ha->rsp_qid_map))
584 			continue;
585 
586 		rsp = ha->rsp_q_map[cnt];
587 		clear_bit(cnt, ha->rsp_qid_map);
588 		ha->rsp_q_map[cnt] =  NULL;
589 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
590 		qla2x00_free_rsp_que(ha, rsp);
591 		spin_lock_irqsave(&ha->hardware_lock, flags);
592 	}
593 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
594 
595 	kfree(ha->rsp_q_map);
596 	ha->rsp_q_map = NULL;
597 }
598 
599 static char *
600 qla2x00_pci_info_str(struct scsi_qla_host *vha, char *str, size_t str_len)
601 {
602 	struct qla_hw_data *ha = vha->hw;
603 	static const char *const pci_bus_modes[] = {
604 		"33", "66", "100", "133",
605 	};
606 	uint16_t pci_bus;
607 
608 	pci_bus = (ha->pci_attr & (BIT_9 | BIT_10)) >> 9;
609 	if (pci_bus) {
610 		snprintf(str, str_len, "PCI-X (%s MHz)",
611 			 pci_bus_modes[pci_bus]);
612 	} else {
613 		pci_bus = (ha->pci_attr & BIT_8) >> 8;
614 		snprintf(str, str_len, "PCI (%s MHz)", pci_bus_modes[pci_bus]);
615 	}
616 
617 	return str;
618 }
619 
620 static char *
621 qla24xx_pci_info_str(struct scsi_qla_host *vha, char *str, size_t str_len)
622 {
623 	static const char *const pci_bus_modes[] = {
624 		"33", "66", "100", "133",
625 	};
626 	struct qla_hw_data *ha = vha->hw;
627 	uint32_t pci_bus;
628 
629 	if (pci_is_pcie(ha->pdev)) {
630 		uint32_t lstat, lspeed, lwidth;
631 		const char *speed_str;
632 
633 		pcie_capability_read_dword(ha->pdev, PCI_EXP_LNKCAP, &lstat);
634 		lspeed = lstat & PCI_EXP_LNKCAP_SLS;
635 		lwidth = (lstat & PCI_EXP_LNKCAP_MLW) >> 4;
636 
637 		switch (lspeed) {
638 		case 1:
639 			speed_str = "2.5GT/s";
640 			break;
641 		case 2:
642 			speed_str = "5.0GT/s";
643 			break;
644 		case 3:
645 			speed_str = "8.0GT/s";
646 			break;
647 		case 4:
648 			speed_str = "16.0GT/s";
649 			break;
650 		default:
651 			speed_str = "<unknown>";
652 			break;
653 		}
654 		snprintf(str, str_len, "PCIe (%s x%d)", speed_str, lwidth);
655 
656 		return str;
657 	}
658 
659 	pci_bus = (ha->pci_attr & CSRX_PCIX_BUS_MODE_MASK) >> 8;
660 	if (pci_bus == 0 || pci_bus == 8)
661 		snprintf(str, str_len, "PCI (%s MHz)",
662 			 pci_bus_modes[pci_bus >> 3]);
663 	else
664 		snprintf(str, str_len, "PCI-X Mode %d (%s MHz)",
665 			 pci_bus & 4 ? 2 : 1,
666 			 pci_bus_modes[pci_bus & 3]);
667 
668 	return str;
669 }
670 
671 static char *
672 qla2x00_fw_version_str(struct scsi_qla_host *vha, char *str, size_t size)
673 {
674 	char un_str[10];
675 	struct qla_hw_data *ha = vha->hw;
676 
677 	snprintf(str, size, "%d.%02d.%02d ", ha->fw_major_version,
678 	    ha->fw_minor_version, ha->fw_subminor_version);
679 
680 	if (ha->fw_attributes & BIT_9) {
681 		strcat(str, "FLX");
682 		return (str);
683 	}
684 
685 	switch (ha->fw_attributes & 0xFF) {
686 	case 0x7:
687 		strcat(str, "EF");
688 		break;
689 	case 0x17:
690 		strcat(str, "TP");
691 		break;
692 	case 0x37:
693 		strcat(str, "IP");
694 		break;
695 	case 0x77:
696 		strcat(str, "VI");
697 		break;
698 	default:
699 		sprintf(un_str, "(%x)", ha->fw_attributes);
700 		strcat(str, un_str);
701 		break;
702 	}
703 	if (ha->fw_attributes & 0x100)
704 		strcat(str, "X");
705 
706 	return (str);
707 }
708 
709 static char *
710 qla24xx_fw_version_str(struct scsi_qla_host *vha, char *str, size_t size)
711 {
712 	struct qla_hw_data *ha = vha->hw;
713 
714 	snprintf(str, size, "%d.%02d.%02d (%x)", ha->fw_major_version,
715 	    ha->fw_minor_version, ha->fw_subminor_version, ha->fw_attributes);
716 	return str;
717 }
718 
719 void qla2x00_sp_free_dma(srb_t *sp)
720 {
721 	struct qla_hw_data *ha = sp->vha->hw;
722 	struct scsi_cmnd *cmd = GET_CMD_SP(sp);
723 
724 	if (sp->flags & SRB_DMA_VALID) {
725 		scsi_dma_unmap(cmd);
726 		sp->flags &= ~SRB_DMA_VALID;
727 	}
728 
729 	if (sp->flags & SRB_CRC_PROT_DMA_VALID) {
730 		dma_unmap_sg(&ha->pdev->dev, scsi_prot_sglist(cmd),
731 		    scsi_prot_sg_count(cmd), cmd->sc_data_direction);
732 		sp->flags &= ~SRB_CRC_PROT_DMA_VALID;
733 	}
734 
735 	if (sp->flags & SRB_CRC_CTX_DSD_VALID) {
736 		/* List assured to be having elements */
737 		qla2x00_clean_dsd_pool(ha, sp->u.scmd.crc_ctx);
738 		sp->flags &= ~SRB_CRC_CTX_DSD_VALID;
739 	}
740 
741 	if (sp->flags & SRB_CRC_CTX_DMA_VALID) {
742 		struct crc_context *ctx0 = sp->u.scmd.crc_ctx;
743 
744 		dma_pool_free(ha->dl_dma_pool, ctx0, ctx0->crc_ctx_dma);
745 		sp->flags &= ~SRB_CRC_CTX_DMA_VALID;
746 	}
747 
748 	if (sp->flags & SRB_FCP_CMND_DMA_VALID) {
749 		struct ct6_dsd *ctx1 = &sp->u.scmd.ct6_ctx;
750 
751 		dma_pool_free(ha->fcp_cmnd_dma_pool, ctx1->fcp_cmnd,
752 		    ctx1->fcp_cmnd_dma);
753 		list_splice(&ctx1->dsd_list, &ha->gbl_dsd_list);
754 		ha->gbl_dsd_inuse -= ctx1->dsd_use_cnt;
755 		ha->gbl_dsd_avail += ctx1->dsd_use_cnt;
756 	}
757 
758 	if (sp->flags & SRB_GOT_BUF)
759 		qla_put_buf(sp->qpair, &sp->u.scmd.buf_dsc);
760 }
761 
762 void qla2x00_sp_compl(srb_t *sp, int res)
763 {
764 	struct scsi_cmnd *cmd = GET_CMD_SP(sp);
765 	struct completion *comp = sp->comp;
766 
767 	/* kref: INIT */
768 	kref_put(&sp->cmd_kref, qla2x00_sp_release);
769 	cmd->result = res;
770 	sp->type = 0;
771 	scsi_done(cmd);
772 	if (comp)
773 		complete(comp);
774 }
775 
776 void qla2xxx_qpair_sp_free_dma(srb_t *sp)
777 {
778 	struct scsi_cmnd *cmd = GET_CMD_SP(sp);
779 	struct qla_hw_data *ha = sp->fcport->vha->hw;
780 
781 	if (sp->flags & SRB_DMA_VALID) {
782 		scsi_dma_unmap(cmd);
783 		sp->flags &= ~SRB_DMA_VALID;
784 	}
785 
786 	if (sp->flags & SRB_CRC_PROT_DMA_VALID) {
787 		dma_unmap_sg(&ha->pdev->dev, scsi_prot_sglist(cmd),
788 		    scsi_prot_sg_count(cmd), cmd->sc_data_direction);
789 		sp->flags &= ~SRB_CRC_PROT_DMA_VALID;
790 	}
791 
792 	if (sp->flags & SRB_CRC_CTX_DSD_VALID) {
793 		/* List assured to be having elements */
794 		qla2x00_clean_dsd_pool(ha, sp->u.scmd.crc_ctx);
795 		sp->flags &= ~SRB_CRC_CTX_DSD_VALID;
796 	}
797 
798 	if (sp->flags & SRB_DIF_BUNDL_DMA_VALID) {
799 		struct crc_context *difctx = sp->u.scmd.crc_ctx;
800 		struct dsd_dma *dif_dsd, *nxt_dsd;
801 
802 		list_for_each_entry_safe(dif_dsd, nxt_dsd,
803 		    &difctx->ldif_dma_hndl_list, list) {
804 			list_del(&dif_dsd->list);
805 			dma_pool_free(ha->dif_bundl_pool, dif_dsd->dsd_addr,
806 			    dif_dsd->dsd_list_dma);
807 			kfree(dif_dsd);
808 			difctx->no_dif_bundl--;
809 		}
810 
811 		list_for_each_entry_safe(dif_dsd, nxt_dsd,
812 		    &difctx->ldif_dsd_list, list) {
813 			list_del(&dif_dsd->list);
814 			dma_pool_free(ha->dl_dma_pool, dif_dsd->dsd_addr,
815 			    dif_dsd->dsd_list_dma);
816 			kfree(dif_dsd);
817 			difctx->no_ldif_dsd--;
818 		}
819 
820 		if (difctx->no_ldif_dsd) {
821 			ql_dbg(ql_dbg_tgt+ql_dbg_verbose, sp->vha, 0xe022,
822 			    "%s: difctx->no_ldif_dsd=%x\n",
823 			    __func__, difctx->no_ldif_dsd);
824 		}
825 
826 		if (difctx->no_dif_bundl) {
827 			ql_dbg(ql_dbg_tgt+ql_dbg_verbose, sp->vha, 0xe022,
828 			    "%s: difctx->no_dif_bundl=%x\n",
829 			    __func__, difctx->no_dif_bundl);
830 		}
831 		sp->flags &= ~SRB_DIF_BUNDL_DMA_VALID;
832 	}
833 
834 	if (sp->flags & SRB_FCP_CMND_DMA_VALID) {
835 		struct ct6_dsd *ctx1 = &sp->u.scmd.ct6_ctx;
836 
837 		dma_pool_free(ha->fcp_cmnd_dma_pool, ctx1->fcp_cmnd,
838 		    ctx1->fcp_cmnd_dma);
839 		list_splice(&ctx1->dsd_list, &ha->gbl_dsd_list);
840 		ha->gbl_dsd_inuse -= ctx1->dsd_use_cnt;
841 		ha->gbl_dsd_avail += ctx1->dsd_use_cnt;
842 		sp->flags &= ~SRB_FCP_CMND_DMA_VALID;
843 	}
844 
845 	if (sp->flags & SRB_CRC_CTX_DMA_VALID) {
846 		struct crc_context *ctx0 = sp->u.scmd.crc_ctx;
847 
848 		dma_pool_free(ha->dl_dma_pool, ctx0, ctx0->crc_ctx_dma);
849 		sp->flags &= ~SRB_CRC_CTX_DMA_VALID;
850 	}
851 
852 	if (sp->flags & SRB_GOT_BUF)
853 		qla_put_buf(sp->qpair, &sp->u.scmd.buf_dsc);
854 }
855 
856 void qla2xxx_qpair_sp_compl(srb_t *sp, int res)
857 {
858 	struct scsi_cmnd *cmd = GET_CMD_SP(sp);
859 	struct completion *comp = sp->comp;
860 
861 	/* ref: INIT */
862 	kref_put(&sp->cmd_kref, qla2x00_sp_release);
863 	cmd->result = res;
864 	sp->type = 0;
865 	scsi_done(cmd);
866 	if (comp)
867 		complete(comp);
868 }
869 
870 static int
871 qla2xxx_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
872 {
873 	scsi_qla_host_t *vha = shost_priv(host);
874 	fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
875 	struct fc_rport *rport = starget_to_rport(scsi_target(cmd->device));
876 	struct qla_hw_data *ha = vha->hw;
877 	struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
878 	srb_t *sp;
879 	int rval;
880 
881 	if (unlikely(test_bit(UNLOADING, &base_vha->dpc_flags)) ||
882 	    WARN_ON_ONCE(!rport)) {
883 		cmd->result = DID_NO_CONNECT << 16;
884 		goto qc24_fail_command;
885 	}
886 
887 	if (ha->mqenable) {
888 		uint32_t tag;
889 		uint16_t hwq;
890 		struct qla_qpair *qpair = NULL;
891 
892 		tag = blk_mq_unique_tag(scsi_cmd_to_rq(cmd));
893 		hwq = blk_mq_unique_tag_to_hwq(tag);
894 		qpair = ha->queue_pair_map[hwq];
895 
896 		if (qpair)
897 			return qla2xxx_mqueuecommand(host, cmd, qpair);
898 	}
899 
900 	if (ha->flags.eeh_busy) {
901 		if (ha->flags.pci_channel_io_perm_failure) {
902 			ql_dbg(ql_dbg_aer, vha, 0x9010,
903 			    "PCI Channel IO permanent failure, exiting "
904 			    "cmd=%p.\n", cmd);
905 			cmd->result = DID_NO_CONNECT << 16;
906 		} else {
907 			ql_dbg(ql_dbg_aer, vha, 0x9011,
908 			    "EEH_Busy, Requeuing the cmd=%p.\n", cmd);
909 			cmd->result = DID_REQUEUE << 16;
910 		}
911 		goto qc24_fail_command;
912 	}
913 
914 	rval = fc_remote_port_chkready(rport);
915 	if (rval) {
916 		cmd->result = rval;
917 		ql_dbg(ql_dbg_io + ql_dbg_verbose, vha, 0x3003,
918 		    "fc_remote_port_chkready failed for cmd=%p, rval=0x%x.\n",
919 		    cmd, rval);
920 		goto qc24_fail_command;
921 	}
922 
923 	if (!vha->flags.difdix_supported &&
924 		scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
925 			ql_dbg(ql_dbg_io, vha, 0x3004,
926 			    "DIF Cap not reg, fail DIF capable cmd's:%p.\n",
927 			    cmd);
928 			cmd->result = DID_NO_CONNECT << 16;
929 			goto qc24_fail_command;
930 	}
931 
932 	if (!fcport || fcport->deleted) {
933 		cmd->result = DID_IMM_RETRY << 16;
934 		goto qc24_fail_command;
935 	}
936 
937 	if (atomic_read(&fcport->state) != FCS_ONLINE || fcport->deleted) {
938 		if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD ||
939 			atomic_read(&base_vha->loop_state) == LOOP_DEAD) {
940 			ql_dbg(ql_dbg_io, vha, 0x3005,
941 			    "Returning DNC, fcport_state=%d loop_state=%d.\n",
942 			    atomic_read(&fcport->state),
943 			    atomic_read(&base_vha->loop_state));
944 			cmd->result = DID_NO_CONNECT << 16;
945 			goto qc24_fail_command;
946 		}
947 		goto qc24_target_busy;
948 	}
949 
950 	/*
951 	 * Return target busy if we've received a non-zero retry_delay_timer
952 	 * in a FCP_RSP.
953 	 */
954 	if (fcport->retry_delay_timestamp == 0) {
955 		/* retry delay not set */
956 	} else if (time_after(jiffies, fcport->retry_delay_timestamp))
957 		fcport->retry_delay_timestamp = 0;
958 	else
959 		goto qc24_target_busy;
960 
961 	sp = scsi_cmd_priv(cmd);
962 	/* ref: INIT */
963 	qla2xxx_init_sp(sp, vha, vha->hw->base_qpair, fcport);
964 
965 	sp->u.scmd.cmd = cmd;
966 	sp->type = SRB_SCSI_CMD;
967 	sp->free = qla2x00_sp_free_dma;
968 	sp->done = qla2x00_sp_compl;
969 
970 	rval = ha->isp_ops->start_scsi(sp);
971 	if (rval != QLA_SUCCESS) {
972 		ql_dbg(ql_dbg_io + ql_dbg_verbose, vha, 0x3013,
973 		    "Start scsi failed rval=%d for cmd=%p.\n", rval, cmd);
974 		goto qc24_host_busy_free_sp;
975 	}
976 
977 	return 0;
978 
979 qc24_host_busy_free_sp:
980 	/* ref: INIT */
981 	kref_put(&sp->cmd_kref, qla2x00_sp_release);
982 
983 qc24_target_busy:
984 	return SCSI_MLQUEUE_TARGET_BUSY;
985 
986 qc24_fail_command:
987 	scsi_done(cmd);
988 
989 	return 0;
990 }
991 
992 /* For MQ supported I/O */
993 int
994 qla2xxx_mqueuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd,
995     struct qla_qpair *qpair)
996 {
997 	scsi_qla_host_t *vha = shost_priv(host);
998 	fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
999 	struct fc_rport *rport = starget_to_rport(scsi_target(cmd->device));
1000 	struct qla_hw_data *ha = vha->hw;
1001 	struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
1002 	srb_t *sp;
1003 	int rval;
1004 
1005 	rval = rport ? fc_remote_port_chkready(rport) : (DID_NO_CONNECT << 16);
1006 	if (rval) {
1007 		cmd->result = rval;
1008 		ql_dbg(ql_dbg_io + ql_dbg_verbose, vha, 0x3076,
1009 		    "fc_remote_port_chkready failed for cmd=%p, rval=0x%x.\n",
1010 		    cmd, rval);
1011 		goto qc24_fail_command;
1012 	}
1013 
1014 	if (!qpair->online) {
1015 		ql_dbg(ql_dbg_io, vha, 0x3077,
1016 		       "qpair not online. eeh_busy=%d.\n", ha->flags.eeh_busy);
1017 		cmd->result = DID_NO_CONNECT << 16;
1018 		goto qc24_fail_command;
1019 	}
1020 
1021 	if (!fcport || fcport->deleted) {
1022 		cmd->result = DID_IMM_RETRY << 16;
1023 		goto qc24_fail_command;
1024 	}
1025 
1026 	if (atomic_read(&fcport->state) != FCS_ONLINE || fcport->deleted) {
1027 		if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD ||
1028 			atomic_read(&base_vha->loop_state) == LOOP_DEAD) {
1029 			ql_dbg(ql_dbg_io, vha, 0x3077,
1030 			    "Returning DNC, fcport_state=%d loop_state=%d.\n",
1031 			    atomic_read(&fcport->state),
1032 			    atomic_read(&base_vha->loop_state));
1033 			cmd->result = DID_NO_CONNECT << 16;
1034 			goto qc24_fail_command;
1035 		}
1036 		goto qc24_target_busy;
1037 	}
1038 
1039 	/*
1040 	 * Return target busy if we've received a non-zero retry_delay_timer
1041 	 * in a FCP_RSP.
1042 	 */
1043 	if (fcport->retry_delay_timestamp == 0) {
1044 		/* retry delay not set */
1045 	} else if (time_after(jiffies, fcport->retry_delay_timestamp))
1046 		fcport->retry_delay_timestamp = 0;
1047 	else
1048 		goto qc24_target_busy;
1049 
1050 	sp = scsi_cmd_priv(cmd);
1051 	/* ref: INIT */
1052 	qla2xxx_init_sp(sp, vha, qpair, fcport);
1053 
1054 	sp->u.scmd.cmd = cmd;
1055 	sp->type = SRB_SCSI_CMD;
1056 	sp->free = qla2xxx_qpair_sp_free_dma;
1057 	sp->done = qla2xxx_qpair_sp_compl;
1058 
1059 	rval = ha->isp_ops->start_scsi_mq(sp);
1060 	if (rval != QLA_SUCCESS) {
1061 		ql_dbg(ql_dbg_io + ql_dbg_verbose, vha, 0x3078,
1062 		    "Start scsi failed rval=%d for cmd=%p.\n", rval, cmd);
1063 		goto qc24_host_busy_free_sp;
1064 	}
1065 
1066 	return 0;
1067 
1068 qc24_host_busy_free_sp:
1069 	/* ref: INIT */
1070 	kref_put(&sp->cmd_kref, qla2x00_sp_release);
1071 
1072 qc24_target_busy:
1073 	return SCSI_MLQUEUE_TARGET_BUSY;
1074 
1075 qc24_fail_command:
1076 	scsi_done(cmd);
1077 
1078 	return 0;
1079 }
1080 
1081 /*
1082  * qla2x00_wait_for_hba_online
1083  *    Wait till the HBA is online after going through
1084  *    <= MAX_RETRIES_OF_ISP_ABORT  or
1085  *    finally HBA is disabled ie marked offline
1086  *
1087  * Input:
1088  *     ha - pointer to host adapter structure
1089  *
1090  * Note:
1091  *    Does context switching-Release SPIN_LOCK
1092  *    (if any) before calling this routine.
1093  *
1094  * Return:
1095  *    Success (Adapter is online) : 0
1096  *    Failed  (Adapter is offline/disabled) : 1
1097  */
1098 int
1099 qla2x00_wait_for_hba_online(scsi_qla_host_t *vha)
1100 {
1101 	int		return_status;
1102 	unsigned long	wait_online;
1103 	struct qla_hw_data *ha = vha->hw;
1104 	scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
1105 
1106 	wait_online = jiffies + (MAX_LOOP_TIMEOUT * HZ);
1107 	while (((test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags)) ||
1108 	    test_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags) ||
1109 	    test_bit(ISP_ABORT_RETRY, &base_vha->dpc_flags) ||
1110 	    ha->dpc_active) && time_before(jiffies, wait_online)) {
1111 
1112 		msleep(1000);
1113 	}
1114 	if (base_vha->flags.online)
1115 		return_status = QLA_SUCCESS;
1116 	else
1117 		return_status = QLA_FUNCTION_FAILED;
1118 
1119 	return (return_status);
1120 }
1121 
1122 static inline int test_fcport_count(scsi_qla_host_t *vha)
1123 {
1124 	struct qla_hw_data *ha = vha->hw;
1125 	unsigned long flags;
1126 	int res;
1127 	/* Return 0 = sleep, x=wake */
1128 
1129 	spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1130 	ql_dbg(ql_dbg_init, vha, 0x00ec,
1131 	    "tgt %p, fcport_count=%d\n",
1132 	    vha, vha->fcport_count);
1133 	res = (vha->fcport_count == 0);
1134 	if  (res) {
1135 		struct fc_port *fcport;
1136 
1137 		list_for_each_entry(fcport, &vha->vp_fcports, list) {
1138 			if (fcport->deleted != QLA_SESS_DELETED) {
1139 				/* session(s) may not be fully logged in
1140 				 * (ie fcport_count=0), but session
1141 				 * deletion thread(s) may be inflight.
1142 				 */
1143 
1144 				res = 0;
1145 				break;
1146 			}
1147 		}
1148 	}
1149 	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1150 
1151 	return res;
1152 }
1153 
1154 /*
1155  * qla2x00_wait_for_sess_deletion can only be called from remove_one.
1156  * it has dependency on UNLOADING flag to stop device discovery
1157  */
1158 void
1159 qla2x00_wait_for_sess_deletion(scsi_qla_host_t *vha)
1160 {
1161 	u8 i;
1162 
1163 	qla2x00_mark_all_devices_lost(vha);
1164 
1165 	for (i = 0; i < 10; i++) {
1166 		if (wait_event_timeout(vha->fcport_waitQ,
1167 		    test_fcport_count(vha), HZ) > 0)
1168 			break;
1169 	}
1170 
1171 	flush_workqueue(vha->hw->wq);
1172 }
1173 
1174 /*
1175  * qla2x00_wait_for_hba_ready
1176  * Wait till the HBA is ready before doing driver unload
1177  *
1178  * Input:
1179  *     ha - pointer to host adapter structure
1180  *
1181  * Note:
1182  *    Does context switching-Release SPIN_LOCK
1183  *    (if any) before calling this routine.
1184  *
1185  */
1186 static void
1187 qla2x00_wait_for_hba_ready(scsi_qla_host_t *vha)
1188 {
1189 	struct qla_hw_data *ha = vha->hw;
1190 	scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
1191 
1192 	while ((qla2x00_reset_active(vha) || ha->dpc_active ||
1193 		ha->flags.mbox_busy) ||
1194 	       test_bit(FX00_RESET_RECOVERY, &vha->dpc_flags) ||
1195 	       test_bit(FX00_TARGET_SCAN, &vha->dpc_flags)) {
1196 		if (test_bit(UNLOADING, &base_vha->dpc_flags))
1197 			break;
1198 		msleep(1000);
1199 	}
1200 }
1201 
1202 int
1203 qla2x00_wait_for_chip_reset(scsi_qla_host_t *vha)
1204 {
1205 	int		return_status;
1206 	unsigned long	wait_reset;
1207 	struct qla_hw_data *ha = vha->hw;
1208 	scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
1209 
1210 	wait_reset = jiffies + (MAX_LOOP_TIMEOUT * HZ);
1211 	while (((test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags)) ||
1212 	    test_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags) ||
1213 	    test_bit(ISP_ABORT_RETRY, &base_vha->dpc_flags) ||
1214 	    ha->dpc_active) && time_before(jiffies, wait_reset)) {
1215 
1216 		msleep(1000);
1217 
1218 		if (!test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags) &&
1219 		    ha->flags.chip_reset_done)
1220 			break;
1221 	}
1222 	if (ha->flags.chip_reset_done)
1223 		return_status = QLA_SUCCESS;
1224 	else
1225 		return_status = QLA_FUNCTION_FAILED;
1226 
1227 	return return_status;
1228 }
1229 
1230 /**************************************************************************
1231 * qla2xxx_eh_abort
1232 *
1233 * Description:
1234 *    The abort function will abort the specified command.
1235 *
1236 * Input:
1237 *    cmd = Linux SCSI command packet to be aborted.
1238 *
1239 * Returns:
1240 *    Either SUCCESS or FAILED.
1241 *
1242 * Note:
1243 *    Only return FAILED if command not returned by firmware.
1244 **************************************************************************/
1245 static int
1246 qla2xxx_eh_abort(struct scsi_cmnd *cmd)
1247 {
1248 	scsi_qla_host_t *vha = shost_priv(cmd->device->host);
1249 	DECLARE_COMPLETION_ONSTACK(comp);
1250 	srb_t *sp;
1251 	int ret;
1252 	unsigned int id;
1253 	uint64_t lun;
1254 	int rval;
1255 	struct qla_hw_data *ha = vha->hw;
1256 	uint32_t ratov_j;
1257 	struct qla_qpair *qpair;
1258 	unsigned long flags;
1259 	int fast_fail_status = SUCCESS;
1260 
1261 	if (qla2x00_isp_reg_stat(ha)) {
1262 		ql_log(ql_log_info, vha, 0x8042,
1263 		    "PCI/Register disconnect, exiting.\n");
1264 		qla_pci_set_eeh_busy(vha);
1265 		return FAILED;
1266 	}
1267 
1268 	/* Save any FAST_IO_FAIL value to return later if abort succeeds */
1269 	ret = fc_block_scsi_eh(cmd);
1270 	if (ret != 0)
1271 		fast_fail_status = ret;
1272 
1273 	sp = scsi_cmd_priv(cmd);
1274 	qpair = sp->qpair;
1275 
1276 	vha->cmd_timeout_cnt++;
1277 
1278 	if ((sp->fcport && sp->fcport->deleted) || !qpair)
1279 		return fast_fail_status != SUCCESS ? fast_fail_status : FAILED;
1280 
1281 	spin_lock_irqsave(qpair->qp_lock_ptr, flags);
1282 	sp->comp = &comp;
1283 	spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
1284 
1285 
1286 	id = cmd->device->id;
1287 	lun = cmd->device->lun;
1288 
1289 	ql_dbg(ql_dbg_taskm, vha, 0x8002,
1290 	    "Aborting from RISC nexus=%ld:%d:%llu sp=%p cmd=%p handle=%x\n",
1291 	    vha->host_no, id, lun, sp, cmd, sp->handle);
1292 
1293 	/*
1294 	 * Abort will release the original Command/sp from FW. Let the
1295 	 * original command call scsi_done. In return, he will wakeup
1296 	 * this sleeping thread.
1297 	 */
1298 	rval = ha->isp_ops->abort_command(sp);
1299 
1300 	ql_dbg(ql_dbg_taskm, vha, 0x8003,
1301 	       "Abort command mbx cmd=%p, rval=%x.\n", cmd, rval);
1302 
1303 	/* Wait for the command completion. */
1304 	ratov_j = ha->r_a_tov/10 * 4 * 1000;
1305 	ratov_j = msecs_to_jiffies(ratov_j);
1306 	switch (rval) {
1307 	case QLA_SUCCESS:
1308 		if (!wait_for_completion_timeout(&comp, ratov_j)) {
1309 			ql_dbg(ql_dbg_taskm, vha, 0xffff,
1310 			    "%s: Abort wait timer (4 * R_A_TOV[%d]) expired\n",
1311 			    __func__, ha->r_a_tov/10);
1312 			ret = FAILED;
1313 		} else {
1314 			ret = fast_fail_status;
1315 		}
1316 		break;
1317 	default:
1318 		ret = FAILED;
1319 		break;
1320 	}
1321 
1322 	sp->comp = NULL;
1323 
1324 	ql_log(ql_log_info, vha, 0x801c,
1325 	    "Abort command issued nexus=%ld:%d:%llu -- %x.\n",
1326 	    vha->host_no, id, lun, ret);
1327 
1328 	return ret;
1329 }
1330 
1331 #define ABORT_POLLING_PERIOD	1000
1332 #define ABORT_WAIT_ITER		((2 * 1000) / (ABORT_POLLING_PERIOD))
1333 
1334 /*
1335  * Returns: QLA_SUCCESS or QLA_FUNCTION_FAILED.
1336  */
1337 static int
1338 __qla2x00_eh_wait_for_pending_commands(struct qla_qpair *qpair, unsigned int t,
1339 				       uint64_t l, enum nexus_wait_type type)
1340 {
1341 	int cnt, match, status;
1342 	unsigned long flags;
1343 	scsi_qla_host_t *vha = qpair->vha;
1344 	struct req_que *req = qpair->req;
1345 	srb_t *sp;
1346 	struct scsi_cmnd *cmd;
1347 	unsigned long wait_iter = ABORT_WAIT_ITER;
1348 	bool found;
1349 	struct qla_hw_data *ha = vha->hw;
1350 
1351 	status = QLA_SUCCESS;
1352 
1353 	while (wait_iter--) {
1354 		found = false;
1355 
1356 		spin_lock_irqsave(qpair->qp_lock_ptr, flags);
1357 		for (cnt = 1; cnt < req->num_outstanding_cmds; cnt++) {
1358 			sp = req->outstanding_cmds[cnt];
1359 			if (!sp)
1360 				continue;
1361 			if (sp->type != SRB_SCSI_CMD)
1362 				continue;
1363 			if (vha->vp_idx != sp->vha->vp_idx)
1364 				continue;
1365 			match = 0;
1366 			cmd = GET_CMD_SP(sp);
1367 			switch (type) {
1368 			case WAIT_HOST:
1369 				match = 1;
1370 				break;
1371 			case WAIT_TARGET:
1372 				if (sp->fcport)
1373 					match = sp->fcport->d_id.b24 == t;
1374 				else
1375 					match = 0;
1376 				break;
1377 			case WAIT_LUN:
1378 				if (sp->fcport)
1379 					match = (sp->fcport->d_id.b24 == t &&
1380 						cmd->device->lun == l);
1381 				else
1382 					match = 0;
1383 				break;
1384 			}
1385 			if (!match)
1386 				continue;
1387 
1388 			spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
1389 
1390 			if (unlikely(pci_channel_offline(ha->pdev)) ||
1391 			    ha->flags.eeh_busy) {
1392 				ql_dbg(ql_dbg_taskm, vha, 0x8005,
1393 				    "Return:eh_wait.\n");
1394 				return status;
1395 			}
1396 
1397 			/*
1398 			 * SRB_SCSI_CMD is still in the outstanding_cmds array.
1399 			 * it means scsi_done has not called. Wait for it to
1400 			 * clear from outstanding_cmds.
1401 			 */
1402 			msleep(ABORT_POLLING_PERIOD);
1403 			spin_lock_irqsave(qpair->qp_lock_ptr, flags);
1404 			found = true;
1405 		}
1406 		spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
1407 
1408 		if (!found)
1409 			break;
1410 	}
1411 
1412 	if (wait_iter == -1)
1413 		status = QLA_FUNCTION_FAILED;
1414 
1415 	return status;
1416 }
1417 
1418 int
1419 qla2x00_eh_wait_for_pending_commands(scsi_qla_host_t *vha, unsigned int t,
1420 				     uint64_t l, enum nexus_wait_type type)
1421 {
1422 	struct qla_qpair *qpair;
1423 	struct qla_hw_data *ha = vha->hw;
1424 	int i, status = QLA_SUCCESS;
1425 
1426 	status = __qla2x00_eh_wait_for_pending_commands(ha->base_qpair, t, l,
1427 							type);
1428 	for (i = 0; status == QLA_SUCCESS && i < ha->max_qpairs; i++) {
1429 		qpair = ha->queue_pair_map[i];
1430 		if (!qpair)
1431 			continue;
1432 		status = __qla2x00_eh_wait_for_pending_commands(qpair, t, l,
1433 								type);
1434 	}
1435 	return status;
1436 }
1437 
1438 static char *reset_errors[] = {
1439 	"HBA not online",
1440 	"HBA not ready",
1441 	"Task management failed",
1442 	"Waiting for command completions",
1443 };
1444 
1445 static int
1446 qla2xxx_eh_device_reset(struct scsi_cmnd *cmd)
1447 {
1448 	struct scsi_device *sdev = cmd->device;
1449 	scsi_qla_host_t *vha = shost_priv(sdev->host);
1450 	struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1451 	fc_port_t *fcport = (struct fc_port *) sdev->hostdata;
1452 	struct qla_hw_data *ha = vha->hw;
1453 	int err;
1454 
1455 	if (qla2x00_isp_reg_stat(ha)) {
1456 		ql_log(ql_log_info, vha, 0x803e,
1457 		    "PCI/Register disconnect, exiting.\n");
1458 		qla_pci_set_eeh_busy(vha);
1459 		return FAILED;
1460 	}
1461 
1462 	if (!fcport) {
1463 		return FAILED;
1464 	}
1465 
1466 	err = fc_block_rport(rport);
1467 	if (err != 0)
1468 		return err;
1469 
1470 	if (fcport->deleted)
1471 		return FAILED;
1472 
1473 	ql_log(ql_log_info, vha, 0x8009,
1474 	    "DEVICE RESET ISSUED nexus=%ld:%d:%llu cmd=%p.\n", vha->host_no,
1475 	    sdev->id, sdev->lun, cmd);
1476 
1477 	err = 0;
1478 	if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
1479 		ql_log(ql_log_warn, vha, 0x800a,
1480 		    "Wait for hba online failed for cmd=%p.\n", cmd);
1481 		goto eh_reset_failed;
1482 	}
1483 	err = 2;
1484 	if (ha->isp_ops->lun_reset(fcport, sdev->lun, 1)
1485 		!= QLA_SUCCESS) {
1486 		ql_log(ql_log_warn, vha, 0x800c,
1487 		    "do_reset failed for cmd=%p.\n", cmd);
1488 		goto eh_reset_failed;
1489 	}
1490 	err = 3;
1491 	if (qla2x00_eh_wait_for_pending_commands(vha, sdev->id,
1492 	    sdev->lun, WAIT_LUN) != QLA_SUCCESS) {
1493 		ql_log(ql_log_warn, vha, 0x800d,
1494 		    "wait for pending cmds failed for cmd=%p.\n", cmd);
1495 		goto eh_reset_failed;
1496 	}
1497 
1498 	ql_log(ql_log_info, vha, 0x800e,
1499 	    "DEVICE RESET SUCCEEDED nexus:%ld:%d:%llu cmd=%p.\n",
1500 	    vha->host_no, sdev->id, sdev->lun, cmd);
1501 
1502 	return SUCCESS;
1503 
1504 eh_reset_failed:
1505 	ql_log(ql_log_info, vha, 0x800f,
1506 	    "DEVICE RESET FAILED: %s nexus=%ld:%d:%llu cmd=%p.\n",
1507 	    reset_errors[err], vha->host_no, sdev->id, sdev->lun,
1508 	    cmd);
1509 	vha->reset_cmd_err_cnt++;
1510 	return FAILED;
1511 }
1512 
1513 static int
1514 qla2xxx_eh_target_reset(struct scsi_cmnd *cmd)
1515 {
1516 	struct scsi_device *sdev = cmd->device;
1517 	struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1518 	scsi_qla_host_t *vha = shost_priv(rport_to_shost(rport));
1519 	struct qla_hw_data *ha = vha->hw;
1520 	fc_port_t *fcport = *(fc_port_t **)rport->dd_data;
1521 	int err;
1522 
1523 	if (qla2x00_isp_reg_stat(ha)) {
1524 		ql_log(ql_log_info, vha, 0x803f,
1525 		    "PCI/Register disconnect, exiting.\n");
1526 		qla_pci_set_eeh_busy(vha);
1527 		return FAILED;
1528 	}
1529 
1530 	if (!fcport) {
1531 		return FAILED;
1532 	}
1533 
1534 	err = fc_block_rport(rport);
1535 	if (err != 0)
1536 		return err;
1537 
1538 	if (fcport->deleted)
1539 		return FAILED;
1540 
1541 	ql_log(ql_log_info, vha, 0x8009,
1542 	    "TARGET RESET ISSUED nexus=%ld:%d cmd=%p.\n", vha->host_no,
1543 	    sdev->id, cmd);
1544 
1545 	err = 0;
1546 	if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
1547 		ql_log(ql_log_warn, vha, 0x800a,
1548 		    "Wait for hba online failed for cmd=%p.\n", cmd);
1549 		goto eh_reset_failed;
1550 	}
1551 	err = 2;
1552 	if (ha->isp_ops->target_reset(fcport, 0, 0) != QLA_SUCCESS) {
1553 		ql_log(ql_log_warn, vha, 0x800c,
1554 		    "target_reset failed for cmd=%p.\n", cmd);
1555 		goto eh_reset_failed;
1556 	}
1557 	err = 3;
1558 	if (qla2x00_eh_wait_for_pending_commands(vha, sdev->id,
1559 	    0, WAIT_TARGET) != QLA_SUCCESS) {
1560 		ql_log(ql_log_warn, vha, 0x800d,
1561 		    "wait for pending cmds failed for cmd=%p.\n", cmd);
1562 		goto eh_reset_failed;
1563 	}
1564 
1565 	ql_log(ql_log_info, vha, 0x800e,
1566 	    "TARGET RESET SUCCEEDED nexus:%ld:%d cmd=%p.\n",
1567 	    vha->host_no, sdev->id, cmd);
1568 
1569 	return SUCCESS;
1570 
1571 eh_reset_failed:
1572 	ql_log(ql_log_info, vha, 0x800f,
1573 	    "TARGET RESET FAILED: %s nexus=%ld:%d:%llu cmd=%p.\n",
1574 	    reset_errors[err], vha->host_no, cmd->device->id, cmd->device->lun,
1575 	    cmd);
1576 	vha->reset_cmd_err_cnt++;
1577 	return FAILED;
1578 }
1579 
1580 /**************************************************************************
1581 * qla2xxx_eh_bus_reset
1582 *
1583 * Description:
1584 *    The bus reset function will reset the bus and abort any executing
1585 *    commands.
1586 *
1587 * Input:
1588 *    cmd = Linux SCSI command packet of the command that cause the
1589 *          bus reset.
1590 *
1591 * Returns:
1592 *    SUCCESS/FAILURE (defined as macro in scsi.h).
1593 *
1594 **************************************************************************/
1595 static int
1596 qla2xxx_eh_bus_reset(struct scsi_cmnd *cmd)
1597 {
1598 	scsi_qla_host_t *vha = shost_priv(cmd->device->host);
1599 	int ret = FAILED;
1600 	unsigned int id;
1601 	uint64_t lun;
1602 	struct qla_hw_data *ha = vha->hw;
1603 
1604 	if (qla2x00_isp_reg_stat(ha)) {
1605 		ql_log(ql_log_info, vha, 0x8040,
1606 		    "PCI/Register disconnect, exiting.\n");
1607 		qla_pci_set_eeh_busy(vha);
1608 		return FAILED;
1609 	}
1610 
1611 	id = cmd->device->id;
1612 	lun = cmd->device->lun;
1613 
1614 	if (qla2x00_chip_is_down(vha))
1615 		return ret;
1616 
1617 	ql_log(ql_log_info, vha, 0x8012,
1618 	    "BUS RESET ISSUED nexus=%ld:%d:%llu.\n", vha->host_no, id, lun);
1619 
1620 	if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
1621 		ql_log(ql_log_fatal, vha, 0x8013,
1622 		    "Wait for hba online failed board disabled.\n");
1623 		goto eh_bus_reset_done;
1624 	}
1625 
1626 	if (qla2x00_loop_reset(vha) == QLA_SUCCESS)
1627 		ret = SUCCESS;
1628 
1629 	if (ret == FAILED)
1630 		goto eh_bus_reset_done;
1631 
1632 	/* Flush outstanding commands. */
1633 	if (qla2x00_eh_wait_for_pending_commands(vha, 0, 0, WAIT_HOST) !=
1634 	    QLA_SUCCESS) {
1635 		ql_log(ql_log_warn, vha, 0x8014,
1636 		    "Wait for pending commands failed.\n");
1637 		ret = FAILED;
1638 	}
1639 
1640 eh_bus_reset_done:
1641 	ql_log(ql_log_warn, vha, 0x802b,
1642 	    "BUS RESET %s nexus=%ld:%d:%llu.\n",
1643 	    (ret == FAILED) ? "FAILED" : "SUCCEEDED", vha->host_no, id, lun);
1644 
1645 	return ret;
1646 }
1647 
1648 /**************************************************************************
1649 * qla2xxx_eh_host_reset
1650 *
1651 * Description:
1652 *    The reset function will reset the Adapter.
1653 *
1654 * Input:
1655 *      cmd = Linux SCSI command packet of the command that cause the
1656 *            adapter reset.
1657 *
1658 * Returns:
1659 *      Either SUCCESS or FAILED.
1660 *
1661 * Note:
1662 **************************************************************************/
1663 static int
1664 qla2xxx_eh_host_reset(struct scsi_cmnd *cmd)
1665 {
1666 	scsi_qla_host_t *vha = shost_priv(cmd->device->host);
1667 	struct qla_hw_data *ha = vha->hw;
1668 	int ret = FAILED;
1669 	unsigned int id;
1670 	uint64_t lun;
1671 	scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
1672 
1673 	if (qla2x00_isp_reg_stat(ha)) {
1674 		ql_log(ql_log_info, vha, 0x8041,
1675 		    "PCI/Register disconnect, exiting.\n");
1676 		qla_pci_set_eeh_busy(vha);
1677 		return SUCCESS;
1678 	}
1679 
1680 	id = cmd->device->id;
1681 	lun = cmd->device->lun;
1682 
1683 	ql_log(ql_log_info, vha, 0x8018,
1684 	    "ADAPTER RESET ISSUED nexus=%ld:%d:%llu.\n", vha->host_no, id, lun);
1685 
1686 	/*
1687 	 * No point in issuing another reset if one is active.  Also do not
1688 	 * attempt a reset if we are updating flash.
1689 	 */
1690 	if (qla2x00_reset_active(vha) || ha->optrom_state != QLA_SWAITING)
1691 		goto eh_host_reset_lock;
1692 
1693 	if (vha != base_vha) {
1694 		if (qla2x00_vp_abort_isp(vha))
1695 			goto eh_host_reset_lock;
1696 	} else {
1697 		if (IS_P3P_TYPE(vha->hw)) {
1698 			if (!qla82xx_fcoe_ctx_reset(vha)) {
1699 				/* Ctx reset success */
1700 				ret = SUCCESS;
1701 				goto eh_host_reset_lock;
1702 			}
1703 			/* fall thru if ctx reset failed */
1704 		}
1705 		if (ha->wq)
1706 			flush_workqueue(ha->wq);
1707 
1708 		set_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
1709 		if (ha->isp_ops->abort_isp(base_vha)) {
1710 			clear_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
1711 			/* failed. schedule dpc to try */
1712 			set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
1713 
1714 			if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
1715 				ql_log(ql_log_warn, vha, 0x802a,
1716 				    "wait for hba online failed.\n");
1717 				goto eh_host_reset_lock;
1718 			}
1719 		}
1720 		clear_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
1721 	}
1722 
1723 	/* Waiting for command to be returned to OS.*/
1724 	if (qla2x00_eh_wait_for_pending_commands(vha, 0, 0, WAIT_HOST) ==
1725 		QLA_SUCCESS)
1726 		ret = SUCCESS;
1727 
1728 eh_host_reset_lock:
1729 	ql_log(ql_log_info, vha, 0x8017,
1730 	    "ADAPTER RESET %s nexus=%ld:%d:%llu.\n",
1731 	    (ret == FAILED) ? "FAILED" : "SUCCEEDED", vha->host_no, id, lun);
1732 
1733 	return ret;
1734 }
1735 
1736 /*
1737 * qla2x00_loop_reset
1738 *      Issue loop reset.
1739 *
1740 * Input:
1741 *      ha = adapter block pointer.
1742 *
1743 * Returns:
1744 *      0 = success
1745 */
1746 int
1747 qla2x00_loop_reset(scsi_qla_host_t *vha)
1748 {
1749 	int ret;
1750 	struct qla_hw_data *ha = vha->hw;
1751 
1752 	if (IS_QLAFX00(ha))
1753 		return QLA_SUCCESS;
1754 
1755 	if (ha->flags.enable_lip_full_login && !IS_CNA_CAPABLE(ha)) {
1756 		atomic_set(&vha->loop_state, LOOP_DOWN);
1757 		atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
1758 		qla2x00_mark_all_devices_lost(vha);
1759 		ret = qla2x00_full_login_lip(vha);
1760 		if (ret != QLA_SUCCESS) {
1761 			ql_dbg(ql_dbg_taskm, vha, 0x802d,
1762 			    "full_login_lip=%d.\n", ret);
1763 		}
1764 	}
1765 
1766 	if (ha->flags.enable_lip_reset) {
1767 		ret = qla2x00_lip_reset(vha);
1768 		if (ret != QLA_SUCCESS)
1769 			ql_dbg(ql_dbg_taskm, vha, 0x802e,
1770 			    "lip_reset failed (%d).\n", ret);
1771 	}
1772 
1773 	/* Issue marker command only when we are going to start the I/O */
1774 	vha->marker_needed = 1;
1775 
1776 	return QLA_SUCCESS;
1777 }
1778 
1779 /*
1780  * The caller must ensure that no completion interrupts will happen
1781  * while this function is in progress.
1782  */
1783 static void qla2x00_abort_srb(struct qla_qpair *qp, srb_t *sp, const int res,
1784 			      unsigned long *flags)
1785 	__releases(qp->qp_lock_ptr)
1786 	__acquires(qp->qp_lock_ptr)
1787 {
1788 	DECLARE_COMPLETION_ONSTACK(comp);
1789 	scsi_qla_host_t *vha = qp->vha;
1790 	struct qla_hw_data *ha = vha->hw;
1791 	struct scsi_cmnd *cmd = GET_CMD_SP(sp);
1792 	int rval;
1793 	bool ret_cmd;
1794 	uint32_t ratov_j;
1795 
1796 	lockdep_assert_held(qp->qp_lock_ptr);
1797 
1798 	if (qla2x00_chip_is_down(vha)) {
1799 		sp->done(sp, res);
1800 		return;
1801 	}
1802 
1803 	if (sp->type == SRB_NVME_CMD || sp->type == SRB_NVME_LS ||
1804 	    (sp->type == SRB_SCSI_CMD && !ha->flags.eeh_busy &&
1805 	     !test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags) &&
1806 	     !qla2x00_isp_reg_stat(ha))) {
1807 		if (sp->comp) {
1808 			sp->done(sp, res);
1809 			return;
1810 		}
1811 
1812 		sp->comp = &comp;
1813 		spin_unlock_irqrestore(qp->qp_lock_ptr, *flags);
1814 
1815 		rval = ha->isp_ops->abort_command(sp);
1816 		/* Wait for command completion. */
1817 		ret_cmd = false;
1818 		ratov_j = ha->r_a_tov/10 * 4 * 1000;
1819 		ratov_j = msecs_to_jiffies(ratov_j);
1820 		switch (rval) {
1821 		case QLA_SUCCESS:
1822 			if (wait_for_completion_timeout(&comp, ratov_j)) {
1823 				ql_dbg(ql_dbg_taskm, vha, 0xffff,
1824 				    "%s: Abort wait timer (4 * R_A_TOV[%d]) expired\n",
1825 				    __func__, ha->r_a_tov/10);
1826 				ret_cmd = true;
1827 			}
1828 			/* else FW return SP to driver */
1829 			break;
1830 		default:
1831 			ret_cmd = true;
1832 			break;
1833 		}
1834 
1835 		spin_lock_irqsave(qp->qp_lock_ptr, *flags);
1836 		if (ret_cmd && blk_mq_request_started(scsi_cmd_to_rq(cmd)))
1837 			sp->done(sp, res);
1838 	} else {
1839 		sp->done(sp, res);
1840 	}
1841 }
1842 
1843 /*
1844  * The caller must ensure that no completion interrupts will happen
1845  * while this function is in progress.
1846  */
1847 static void
1848 __qla2x00_abort_all_cmds(struct qla_qpair *qp, int res)
1849 {
1850 	int cnt;
1851 	unsigned long flags;
1852 	srb_t *sp;
1853 	scsi_qla_host_t *vha = qp->vha;
1854 	struct qla_hw_data *ha = vha->hw;
1855 	struct req_que *req;
1856 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
1857 	struct qla_tgt_cmd *cmd;
1858 
1859 	if (!ha->req_q_map)
1860 		return;
1861 	spin_lock_irqsave(qp->qp_lock_ptr, flags);
1862 	req = qp->req;
1863 	for (cnt = 1; cnt < req->num_outstanding_cmds; cnt++) {
1864 		sp = req->outstanding_cmds[cnt];
1865 		if (sp) {
1866 			/*
1867 			 * perform lockless completion during driver unload
1868 			 */
1869 			if (qla2x00_chip_is_down(vha)) {
1870 				req->outstanding_cmds[cnt] = NULL;
1871 				spin_unlock_irqrestore(qp->qp_lock_ptr, flags);
1872 				sp->done(sp, res);
1873 				spin_lock_irqsave(qp->qp_lock_ptr, flags);
1874 				continue;
1875 			}
1876 
1877 			switch (sp->cmd_type) {
1878 			case TYPE_SRB:
1879 				qla2x00_abort_srb(qp, sp, res, &flags);
1880 				break;
1881 			case TYPE_TGT_CMD:
1882 				if (!vha->hw->tgt.tgt_ops || !tgt ||
1883 				    qla_ini_mode_enabled(vha)) {
1884 					ql_dbg(ql_dbg_tgt_mgt, vha, 0xf003,
1885 					    "HOST-ABORT-HNDLR: dpc_flags=%lx. Target mode disabled\n",
1886 					    vha->dpc_flags);
1887 					continue;
1888 				}
1889 				cmd = (struct qla_tgt_cmd *)sp;
1890 				cmd->aborted = 1;
1891 				break;
1892 			case TYPE_TGT_TMCMD:
1893 				/* Skip task management functions. */
1894 				break;
1895 			default:
1896 				break;
1897 			}
1898 			req->outstanding_cmds[cnt] = NULL;
1899 		}
1900 	}
1901 	spin_unlock_irqrestore(qp->qp_lock_ptr, flags);
1902 }
1903 
1904 /*
1905  * The caller must ensure that no completion interrupts will happen
1906  * while this function is in progress.
1907  */
1908 void
1909 qla2x00_abort_all_cmds(scsi_qla_host_t *vha, int res)
1910 {
1911 	int que;
1912 	struct qla_hw_data *ha = vha->hw;
1913 
1914 	/* Continue only if initialization complete. */
1915 	if (!ha->base_qpair)
1916 		return;
1917 	__qla2x00_abort_all_cmds(ha->base_qpair, res);
1918 
1919 	if (!ha->queue_pair_map)
1920 		return;
1921 	for (que = 0; que < ha->max_qpairs; que++) {
1922 		if (!ha->queue_pair_map[que])
1923 			continue;
1924 
1925 		__qla2x00_abort_all_cmds(ha->queue_pair_map[que], res);
1926 	}
1927 }
1928 
1929 static int
1930 qla2xxx_slave_alloc(struct scsi_device *sdev)
1931 {
1932 	struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1933 
1934 	if (!rport || fc_remote_port_chkready(rport))
1935 		return -ENXIO;
1936 
1937 	sdev->hostdata = *(fc_port_t **)rport->dd_data;
1938 
1939 	return 0;
1940 }
1941 
1942 static int
1943 qla2xxx_slave_configure(struct scsi_device *sdev)
1944 {
1945 	scsi_qla_host_t *vha = shost_priv(sdev->host);
1946 	struct req_que *req = vha->req;
1947 
1948 	if (IS_T10_PI_CAPABLE(vha->hw))
1949 		blk_queue_update_dma_alignment(sdev->request_queue, 0x7);
1950 
1951 	scsi_change_queue_depth(sdev, req->max_q_depth);
1952 	return 0;
1953 }
1954 
1955 static void
1956 qla2xxx_slave_destroy(struct scsi_device *sdev)
1957 {
1958 	sdev->hostdata = NULL;
1959 }
1960 
1961 /**
1962  * qla2x00_config_dma_addressing() - Configure OS DMA addressing method.
1963  * @ha: HA context
1964  *
1965  * At exit, the @ha's flags.enable_64bit_addressing set to indicated
1966  * supported addressing method.
1967  */
1968 static void
1969 qla2x00_config_dma_addressing(struct qla_hw_data *ha)
1970 {
1971 	/* Assume a 32bit DMA mask. */
1972 	ha->flags.enable_64bit_addressing = 0;
1973 
1974 	if (!dma_set_mask(&ha->pdev->dev, DMA_BIT_MASK(64))) {
1975 		/* Any upper-dword bits set? */
1976 		if (MSD(dma_get_required_mask(&ha->pdev->dev)) &&
1977 		    !dma_set_coherent_mask(&ha->pdev->dev, DMA_BIT_MASK(64))) {
1978 			/* Ok, a 64bit DMA mask is applicable. */
1979 			ha->flags.enable_64bit_addressing = 1;
1980 			ha->isp_ops->calc_req_entries = qla2x00_calc_iocbs_64;
1981 			ha->isp_ops->build_iocbs = qla2x00_build_scsi_iocbs_64;
1982 			return;
1983 		}
1984 	}
1985 
1986 	dma_set_mask(&ha->pdev->dev, DMA_BIT_MASK(32));
1987 	dma_set_coherent_mask(&ha->pdev->dev, DMA_BIT_MASK(32));
1988 }
1989 
1990 static void
1991 qla2x00_enable_intrs(struct qla_hw_data *ha)
1992 {
1993 	unsigned long flags = 0;
1994 	struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1995 
1996 	spin_lock_irqsave(&ha->hardware_lock, flags);
1997 	ha->interrupts_on = 1;
1998 	/* enable risc and host interrupts */
1999 	wrt_reg_word(&reg->ictrl, ICR_EN_INT | ICR_EN_RISC);
2000 	rd_reg_word(&reg->ictrl);
2001 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
2002 
2003 }
2004 
2005 static void
2006 qla2x00_disable_intrs(struct qla_hw_data *ha)
2007 {
2008 	unsigned long flags = 0;
2009 	struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
2010 
2011 	spin_lock_irqsave(&ha->hardware_lock, flags);
2012 	ha->interrupts_on = 0;
2013 	/* disable risc and host interrupts */
2014 	wrt_reg_word(&reg->ictrl, 0);
2015 	rd_reg_word(&reg->ictrl);
2016 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
2017 }
2018 
2019 static void
2020 qla24xx_enable_intrs(struct qla_hw_data *ha)
2021 {
2022 	unsigned long flags = 0;
2023 	struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
2024 
2025 	spin_lock_irqsave(&ha->hardware_lock, flags);
2026 	ha->interrupts_on = 1;
2027 	wrt_reg_dword(&reg->ictrl, ICRX_EN_RISC_INT);
2028 	rd_reg_dword(&reg->ictrl);
2029 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
2030 }
2031 
2032 static void
2033 qla24xx_disable_intrs(struct qla_hw_data *ha)
2034 {
2035 	unsigned long flags = 0;
2036 	struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
2037 
2038 	if (IS_NOPOLLING_TYPE(ha))
2039 		return;
2040 	spin_lock_irqsave(&ha->hardware_lock, flags);
2041 	ha->interrupts_on = 0;
2042 	wrt_reg_dword(&reg->ictrl, 0);
2043 	rd_reg_dword(&reg->ictrl);
2044 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
2045 }
2046 
2047 static int
2048 qla2x00_iospace_config(struct qla_hw_data *ha)
2049 {
2050 	resource_size_t pio;
2051 	uint16_t msix;
2052 
2053 	if (pci_request_selected_regions(ha->pdev, ha->bars,
2054 	    QLA2XXX_DRIVER_NAME)) {
2055 		ql_log_pci(ql_log_fatal, ha->pdev, 0x0011,
2056 		    "Failed to reserve PIO/MMIO regions (%s), aborting.\n",
2057 		    pci_name(ha->pdev));
2058 		goto iospace_error_exit;
2059 	}
2060 	if (!(ha->bars & 1))
2061 		goto skip_pio;
2062 
2063 	/* We only need PIO for Flash operations on ISP2312 v2 chips. */
2064 	pio = pci_resource_start(ha->pdev, 0);
2065 	if (pci_resource_flags(ha->pdev, 0) & IORESOURCE_IO) {
2066 		if (pci_resource_len(ha->pdev, 0) < MIN_IOBASE_LEN) {
2067 			ql_log_pci(ql_log_warn, ha->pdev, 0x0012,
2068 			    "Invalid pci I/O region size (%s).\n",
2069 			    pci_name(ha->pdev));
2070 			pio = 0;
2071 		}
2072 	} else {
2073 		ql_log_pci(ql_log_warn, ha->pdev, 0x0013,
2074 		    "Region #0 no a PIO resource (%s).\n",
2075 		    pci_name(ha->pdev));
2076 		pio = 0;
2077 	}
2078 	ha->pio_address = pio;
2079 	ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0014,
2080 	    "PIO address=%llu.\n",
2081 	    (unsigned long long)ha->pio_address);
2082 
2083 skip_pio:
2084 	/* Use MMIO operations for all accesses. */
2085 	if (!(pci_resource_flags(ha->pdev, 1) & IORESOURCE_MEM)) {
2086 		ql_log_pci(ql_log_fatal, ha->pdev, 0x0015,
2087 		    "Region #1 not an MMIO resource (%s), aborting.\n",
2088 		    pci_name(ha->pdev));
2089 		goto iospace_error_exit;
2090 	}
2091 	if (pci_resource_len(ha->pdev, 1) < MIN_IOBASE_LEN) {
2092 		ql_log_pci(ql_log_fatal, ha->pdev, 0x0016,
2093 		    "Invalid PCI mem region size (%s), aborting.\n",
2094 		    pci_name(ha->pdev));
2095 		goto iospace_error_exit;
2096 	}
2097 
2098 	ha->iobase = ioremap(pci_resource_start(ha->pdev, 1), MIN_IOBASE_LEN);
2099 	if (!ha->iobase) {
2100 		ql_log_pci(ql_log_fatal, ha->pdev, 0x0017,
2101 		    "Cannot remap MMIO (%s), aborting.\n",
2102 		    pci_name(ha->pdev));
2103 		goto iospace_error_exit;
2104 	}
2105 
2106 	/* Determine queue resources */
2107 	ha->max_req_queues = ha->max_rsp_queues = 1;
2108 	ha->msix_count = QLA_BASE_VECTORS;
2109 
2110 	/* Check if FW supports MQ or not */
2111 	if (!(ha->fw_attributes & BIT_6))
2112 		goto mqiobase_exit;
2113 
2114 	if (!ql2xmqsupport || !ql2xnvmeenable ||
2115 	    (!IS_QLA25XX(ha) && !IS_QLA81XX(ha)))
2116 		goto mqiobase_exit;
2117 
2118 	ha->mqiobase = ioremap(pci_resource_start(ha->pdev, 3),
2119 			pci_resource_len(ha->pdev, 3));
2120 	if (ha->mqiobase) {
2121 		ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0018,
2122 		    "MQIO Base=%p.\n", ha->mqiobase);
2123 		/* Read MSIX vector size of the board */
2124 		pci_read_config_word(ha->pdev, QLA_PCI_MSIX_CONTROL, &msix);
2125 		ha->msix_count = msix + 1;
2126 		/* Max queues are bounded by available msix vectors */
2127 		/* MB interrupt uses 1 vector */
2128 		ha->max_req_queues = ha->msix_count - 1;
2129 		ha->max_rsp_queues = ha->max_req_queues;
2130 		/* Queue pairs is the max value minus the base queue pair */
2131 		ha->max_qpairs = ha->max_rsp_queues - 1;
2132 		ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0188,
2133 		    "Max no of queues pairs: %d.\n", ha->max_qpairs);
2134 
2135 		ql_log_pci(ql_log_info, ha->pdev, 0x001a,
2136 		    "MSI-X vector count: %d.\n", ha->msix_count);
2137 	} else
2138 		ql_log_pci(ql_log_info, ha->pdev, 0x001b,
2139 		    "BAR 3 not enabled.\n");
2140 
2141 mqiobase_exit:
2142 	ql_dbg_pci(ql_dbg_init, ha->pdev, 0x001c,
2143 	    "MSIX Count: %d.\n", ha->msix_count);
2144 	return (0);
2145 
2146 iospace_error_exit:
2147 	return (-ENOMEM);
2148 }
2149 
2150 
2151 static int
2152 qla83xx_iospace_config(struct qla_hw_data *ha)
2153 {
2154 	uint16_t msix;
2155 
2156 	if (pci_request_selected_regions(ha->pdev, ha->bars,
2157 	    QLA2XXX_DRIVER_NAME)) {
2158 		ql_log_pci(ql_log_fatal, ha->pdev, 0x0117,
2159 		    "Failed to reserve PIO/MMIO regions (%s), aborting.\n",
2160 		    pci_name(ha->pdev));
2161 
2162 		goto iospace_error_exit;
2163 	}
2164 
2165 	/* Use MMIO operations for all accesses. */
2166 	if (!(pci_resource_flags(ha->pdev, 0) & IORESOURCE_MEM)) {
2167 		ql_log_pci(ql_log_warn, ha->pdev, 0x0118,
2168 		    "Invalid pci I/O region size (%s).\n",
2169 		    pci_name(ha->pdev));
2170 		goto iospace_error_exit;
2171 	}
2172 	if (pci_resource_len(ha->pdev, 0) < MIN_IOBASE_LEN) {
2173 		ql_log_pci(ql_log_warn, ha->pdev, 0x0119,
2174 		    "Invalid PCI mem region size (%s), aborting\n",
2175 			pci_name(ha->pdev));
2176 		goto iospace_error_exit;
2177 	}
2178 
2179 	ha->iobase = ioremap(pci_resource_start(ha->pdev, 0), MIN_IOBASE_LEN);
2180 	if (!ha->iobase) {
2181 		ql_log_pci(ql_log_fatal, ha->pdev, 0x011a,
2182 		    "Cannot remap MMIO (%s), aborting.\n",
2183 		    pci_name(ha->pdev));
2184 		goto iospace_error_exit;
2185 	}
2186 
2187 	/* 64bit PCI BAR - BAR2 will correspoond to region 4 */
2188 	/* 83XX 26XX always use MQ type access for queues
2189 	 * - mbar 2, a.k.a region 4 */
2190 	ha->max_req_queues = ha->max_rsp_queues = 1;
2191 	ha->msix_count = QLA_BASE_VECTORS;
2192 	ha->mqiobase = ioremap(pci_resource_start(ha->pdev, 4),
2193 			pci_resource_len(ha->pdev, 4));
2194 
2195 	if (!ha->mqiobase) {
2196 		ql_log_pci(ql_log_fatal, ha->pdev, 0x011d,
2197 		    "BAR2/region4 not enabled\n");
2198 		goto mqiobase_exit;
2199 	}
2200 
2201 	ha->msixbase = ioremap(pci_resource_start(ha->pdev, 2),
2202 			pci_resource_len(ha->pdev, 2));
2203 	if (ha->msixbase) {
2204 		/* Read MSIX vector size of the board */
2205 		pci_read_config_word(ha->pdev,
2206 		    QLA_83XX_PCI_MSIX_CONTROL, &msix);
2207 		ha->msix_count = (msix & PCI_MSIX_FLAGS_QSIZE)  + 1;
2208 		/*
2209 		 * By default, driver uses at least two msix vectors
2210 		 * (default & rspq)
2211 		 */
2212 		if (ql2xmqsupport || ql2xnvmeenable) {
2213 			/* MB interrupt uses 1 vector */
2214 			ha->max_req_queues = ha->msix_count - 1;
2215 
2216 			/* ATIOQ needs 1 vector. That's 1 less QPair */
2217 			if (QLA_TGT_MODE_ENABLED())
2218 				ha->max_req_queues--;
2219 
2220 			ha->max_rsp_queues = ha->max_req_queues;
2221 
2222 			/* Queue pairs is the max value minus
2223 			 * the base queue pair */
2224 			ha->max_qpairs = ha->max_req_queues - 1;
2225 			ql_dbg_pci(ql_dbg_init, ha->pdev, 0x00e3,
2226 			    "Max no of queues pairs: %d.\n", ha->max_qpairs);
2227 		}
2228 		ql_log_pci(ql_log_info, ha->pdev, 0x011c,
2229 		    "MSI-X vector count: %d.\n", ha->msix_count);
2230 	} else
2231 		ql_log_pci(ql_log_info, ha->pdev, 0x011e,
2232 		    "BAR 1 not enabled.\n");
2233 
2234 mqiobase_exit:
2235 	ql_dbg_pci(ql_dbg_init, ha->pdev, 0x011f,
2236 	    "MSIX Count: %d.\n", ha->msix_count);
2237 	return 0;
2238 
2239 iospace_error_exit:
2240 	return -ENOMEM;
2241 }
2242 
2243 static struct isp_operations qla2100_isp_ops = {
2244 	.pci_config		= qla2100_pci_config,
2245 	.reset_chip		= qla2x00_reset_chip,
2246 	.chip_diag		= qla2x00_chip_diag,
2247 	.config_rings		= qla2x00_config_rings,
2248 	.reset_adapter		= qla2x00_reset_adapter,
2249 	.nvram_config		= qla2x00_nvram_config,
2250 	.update_fw_options	= qla2x00_update_fw_options,
2251 	.load_risc		= qla2x00_load_risc,
2252 	.pci_info_str		= qla2x00_pci_info_str,
2253 	.fw_version_str		= qla2x00_fw_version_str,
2254 	.intr_handler		= qla2100_intr_handler,
2255 	.enable_intrs		= qla2x00_enable_intrs,
2256 	.disable_intrs		= qla2x00_disable_intrs,
2257 	.abort_command		= qla2x00_abort_command,
2258 	.target_reset		= qla2x00_abort_target,
2259 	.lun_reset		= qla2x00_lun_reset,
2260 	.fabric_login		= qla2x00_login_fabric,
2261 	.fabric_logout		= qla2x00_fabric_logout,
2262 	.calc_req_entries	= qla2x00_calc_iocbs_32,
2263 	.build_iocbs		= qla2x00_build_scsi_iocbs_32,
2264 	.prep_ms_iocb		= qla2x00_prep_ms_iocb,
2265 	.prep_ms_fdmi_iocb	= qla2x00_prep_ms_fdmi_iocb,
2266 	.read_nvram		= qla2x00_read_nvram_data,
2267 	.write_nvram		= qla2x00_write_nvram_data,
2268 	.fw_dump		= qla2100_fw_dump,
2269 	.beacon_on		= NULL,
2270 	.beacon_off		= NULL,
2271 	.beacon_blink		= NULL,
2272 	.read_optrom		= qla2x00_read_optrom_data,
2273 	.write_optrom		= qla2x00_write_optrom_data,
2274 	.get_flash_version	= qla2x00_get_flash_version,
2275 	.start_scsi		= qla2x00_start_scsi,
2276 	.start_scsi_mq          = NULL,
2277 	.abort_isp		= qla2x00_abort_isp,
2278 	.iospace_config     	= qla2x00_iospace_config,
2279 	.initialize_adapter	= qla2x00_initialize_adapter,
2280 };
2281 
2282 static struct isp_operations qla2300_isp_ops = {
2283 	.pci_config		= qla2300_pci_config,
2284 	.reset_chip		= qla2x00_reset_chip,
2285 	.chip_diag		= qla2x00_chip_diag,
2286 	.config_rings		= qla2x00_config_rings,
2287 	.reset_adapter		= qla2x00_reset_adapter,
2288 	.nvram_config		= qla2x00_nvram_config,
2289 	.update_fw_options	= qla2x00_update_fw_options,
2290 	.load_risc		= qla2x00_load_risc,
2291 	.pci_info_str		= qla2x00_pci_info_str,
2292 	.fw_version_str		= qla2x00_fw_version_str,
2293 	.intr_handler		= qla2300_intr_handler,
2294 	.enable_intrs		= qla2x00_enable_intrs,
2295 	.disable_intrs		= qla2x00_disable_intrs,
2296 	.abort_command		= qla2x00_abort_command,
2297 	.target_reset		= qla2x00_abort_target,
2298 	.lun_reset		= qla2x00_lun_reset,
2299 	.fabric_login		= qla2x00_login_fabric,
2300 	.fabric_logout		= qla2x00_fabric_logout,
2301 	.calc_req_entries	= qla2x00_calc_iocbs_32,
2302 	.build_iocbs		= qla2x00_build_scsi_iocbs_32,
2303 	.prep_ms_iocb		= qla2x00_prep_ms_iocb,
2304 	.prep_ms_fdmi_iocb	= qla2x00_prep_ms_fdmi_iocb,
2305 	.read_nvram		= qla2x00_read_nvram_data,
2306 	.write_nvram		= qla2x00_write_nvram_data,
2307 	.fw_dump		= qla2300_fw_dump,
2308 	.beacon_on		= qla2x00_beacon_on,
2309 	.beacon_off		= qla2x00_beacon_off,
2310 	.beacon_blink		= qla2x00_beacon_blink,
2311 	.read_optrom		= qla2x00_read_optrom_data,
2312 	.write_optrom		= qla2x00_write_optrom_data,
2313 	.get_flash_version	= qla2x00_get_flash_version,
2314 	.start_scsi		= qla2x00_start_scsi,
2315 	.start_scsi_mq          = NULL,
2316 	.abort_isp		= qla2x00_abort_isp,
2317 	.iospace_config		= qla2x00_iospace_config,
2318 	.initialize_adapter	= qla2x00_initialize_adapter,
2319 };
2320 
2321 static struct isp_operations qla24xx_isp_ops = {
2322 	.pci_config		= qla24xx_pci_config,
2323 	.reset_chip		= qla24xx_reset_chip,
2324 	.chip_diag		= qla24xx_chip_diag,
2325 	.config_rings		= qla24xx_config_rings,
2326 	.reset_adapter		= qla24xx_reset_adapter,
2327 	.nvram_config		= qla24xx_nvram_config,
2328 	.update_fw_options	= qla24xx_update_fw_options,
2329 	.load_risc		= qla24xx_load_risc,
2330 	.pci_info_str		= qla24xx_pci_info_str,
2331 	.fw_version_str		= qla24xx_fw_version_str,
2332 	.intr_handler		= qla24xx_intr_handler,
2333 	.enable_intrs		= qla24xx_enable_intrs,
2334 	.disable_intrs		= qla24xx_disable_intrs,
2335 	.abort_command		= qla24xx_abort_command,
2336 	.target_reset		= qla24xx_abort_target,
2337 	.lun_reset		= qla24xx_lun_reset,
2338 	.fabric_login		= qla24xx_login_fabric,
2339 	.fabric_logout		= qla24xx_fabric_logout,
2340 	.calc_req_entries	= NULL,
2341 	.build_iocbs		= NULL,
2342 	.prep_ms_iocb		= qla24xx_prep_ms_iocb,
2343 	.prep_ms_fdmi_iocb	= qla24xx_prep_ms_fdmi_iocb,
2344 	.read_nvram		= qla24xx_read_nvram_data,
2345 	.write_nvram		= qla24xx_write_nvram_data,
2346 	.fw_dump		= qla24xx_fw_dump,
2347 	.beacon_on		= qla24xx_beacon_on,
2348 	.beacon_off		= qla24xx_beacon_off,
2349 	.beacon_blink		= qla24xx_beacon_blink,
2350 	.read_optrom		= qla24xx_read_optrom_data,
2351 	.write_optrom		= qla24xx_write_optrom_data,
2352 	.get_flash_version	= qla24xx_get_flash_version,
2353 	.start_scsi		= qla24xx_start_scsi,
2354 	.start_scsi_mq          = NULL,
2355 	.abort_isp		= qla2x00_abort_isp,
2356 	.iospace_config		= qla2x00_iospace_config,
2357 	.initialize_adapter	= qla2x00_initialize_adapter,
2358 };
2359 
2360 static struct isp_operations qla25xx_isp_ops = {
2361 	.pci_config		= qla25xx_pci_config,
2362 	.reset_chip		= qla24xx_reset_chip,
2363 	.chip_diag		= qla24xx_chip_diag,
2364 	.config_rings		= qla24xx_config_rings,
2365 	.reset_adapter		= qla24xx_reset_adapter,
2366 	.nvram_config		= qla24xx_nvram_config,
2367 	.update_fw_options	= qla24xx_update_fw_options,
2368 	.load_risc		= qla24xx_load_risc,
2369 	.pci_info_str		= qla24xx_pci_info_str,
2370 	.fw_version_str		= qla24xx_fw_version_str,
2371 	.intr_handler		= qla24xx_intr_handler,
2372 	.enable_intrs		= qla24xx_enable_intrs,
2373 	.disable_intrs		= qla24xx_disable_intrs,
2374 	.abort_command		= qla24xx_abort_command,
2375 	.target_reset		= qla24xx_abort_target,
2376 	.lun_reset		= qla24xx_lun_reset,
2377 	.fabric_login		= qla24xx_login_fabric,
2378 	.fabric_logout		= qla24xx_fabric_logout,
2379 	.calc_req_entries	= NULL,
2380 	.build_iocbs		= NULL,
2381 	.prep_ms_iocb		= qla24xx_prep_ms_iocb,
2382 	.prep_ms_fdmi_iocb	= qla24xx_prep_ms_fdmi_iocb,
2383 	.read_nvram		= qla25xx_read_nvram_data,
2384 	.write_nvram		= qla25xx_write_nvram_data,
2385 	.fw_dump		= qla25xx_fw_dump,
2386 	.beacon_on		= qla24xx_beacon_on,
2387 	.beacon_off		= qla24xx_beacon_off,
2388 	.beacon_blink		= qla24xx_beacon_blink,
2389 	.read_optrom		= qla25xx_read_optrom_data,
2390 	.write_optrom		= qla24xx_write_optrom_data,
2391 	.get_flash_version	= qla24xx_get_flash_version,
2392 	.start_scsi		= qla24xx_dif_start_scsi,
2393 	.start_scsi_mq          = qla2xxx_dif_start_scsi_mq,
2394 	.abort_isp		= qla2x00_abort_isp,
2395 	.iospace_config		= qla2x00_iospace_config,
2396 	.initialize_adapter	= qla2x00_initialize_adapter,
2397 };
2398 
2399 static struct isp_operations qla81xx_isp_ops = {
2400 	.pci_config		= qla25xx_pci_config,
2401 	.reset_chip		= qla24xx_reset_chip,
2402 	.chip_diag		= qla24xx_chip_diag,
2403 	.config_rings		= qla24xx_config_rings,
2404 	.reset_adapter		= qla24xx_reset_adapter,
2405 	.nvram_config		= qla81xx_nvram_config,
2406 	.update_fw_options	= qla24xx_update_fw_options,
2407 	.load_risc		= qla81xx_load_risc,
2408 	.pci_info_str		= qla24xx_pci_info_str,
2409 	.fw_version_str		= qla24xx_fw_version_str,
2410 	.intr_handler		= qla24xx_intr_handler,
2411 	.enable_intrs		= qla24xx_enable_intrs,
2412 	.disable_intrs		= qla24xx_disable_intrs,
2413 	.abort_command		= qla24xx_abort_command,
2414 	.target_reset		= qla24xx_abort_target,
2415 	.lun_reset		= qla24xx_lun_reset,
2416 	.fabric_login		= qla24xx_login_fabric,
2417 	.fabric_logout		= qla24xx_fabric_logout,
2418 	.calc_req_entries	= NULL,
2419 	.build_iocbs		= NULL,
2420 	.prep_ms_iocb		= qla24xx_prep_ms_iocb,
2421 	.prep_ms_fdmi_iocb	= qla24xx_prep_ms_fdmi_iocb,
2422 	.read_nvram		= NULL,
2423 	.write_nvram		= NULL,
2424 	.fw_dump		= qla81xx_fw_dump,
2425 	.beacon_on		= qla24xx_beacon_on,
2426 	.beacon_off		= qla24xx_beacon_off,
2427 	.beacon_blink		= qla83xx_beacon_blink,
2428 	.read_optrom		= qla25xx_read_optrom_data,
2429 	.write_optrom		= qla24xx_write_optrom_data,
2430 	.get_flash_version	= qla24xx_get_flash_version,
2431 	.start_scsi		= qla24xx_dif_start_scsi,
2432 	.start_scsi_mq          = qla2xxx_dif_start_scsi_mq,
2433 	.abort_isp		= qla2x00_abort_isp,
2434 	.iospace_config		= qla2x00_iospace_config,
2435 	.initialize_adapter	= qla2x00_initialize_adapter,
2436 };
2437 
2438 static struct isp_operations qla82xx_isp_ops = {
2439 	.pci_config		= qla82xx_pci_config,
2440 	.reset_chip		= qla82xx_reset_chip,
2441 	.chip_diag		= qla24xx_chip_diag,
2442 	.config_rings		= qla82xx_config_rings,
2443 	.reset_adapter		= qla24xx_reset_adapter,
2444 	.nvram_config		= qla81xx_nvram_config,
2445 	.update_fw_options	= qla24xx_update_fw_options,
2446 	.load_risc		= qla82xx_load_risc,
2447 	.pci_info_str		= qla24xx_pci_info_str,
2448 	.fw_version_str		= qla24xx_fw_version_str,
2449 	.intr_handler		= qla82xx_intr_handler,
2450 	.enable_intrs		= qla82xx_enable_intrs,
2451 	.disable_intrs		= qla82xx_disable_intrs,
2452 	.abort_command		= qla24xx_abort_command,
2453 	.target_reset		= qla24xx_abort_target,
2454 	.lun_reset		= qla24xx_lun_reset,
2455 	.fabric_login		= qla24xx_login_fabric,
2456 	.fabric_logout		= qla24xx_fabric_logout,
2457 	.calc_req_entries	= NULL,
2458 	.build_iocbs		= NULL,
2459 	.prep_ms_iocb		= qla24xx_prep_ms_iocb,
2460 	.prep_ms_fdmi_iocb	= qla24xx_prep_ms_fdmi_iocb,
2461 	.read_nvram		= qla24xx_read_nvram_data,
2462 	.write_nvram		= qla24xx_write_nvram_data,
2463 	.fw_dump		= qla82xx_fw_dump,
2464 	.beacon_on		= qla82xx_beacon_on,
2465 	.beacon_off		= qla82xx_beacon_off,
2466 	.beacon_blink		= NULL,
2467 	.read_optrom		= qla82xx_read_optrom_data,
2468 	.write_optrom		= qla82xx_write_optrom_data,
2469 	.get_flash_version	= qla82xx_get_flash_version,
2470 	.start_scsi             = qla82xx_start_scsi,
2471 	.start_scsi_mq          = NULL,
2472 	.abort_isp		= qla82xx_abort_isp,
2473 	.iospace_config     	= qla82xx_iospace_config,
2474 	.initialize_adapter	= qla2x00_initialize_adapter,
2475 };
2476 
2477 static struct isp_operations qla8044_isp_ops = {
2478 	.pci_config		= qla82xx_pci_config,
2479 	.reset_chip		= qla82xx_reset_chip,
2480 	.chip_diag		= qla24xx_chip_diag,
2481 	.config_rings		= qla82xx_config_rings,
2482 	.reset_adapter		= qla24xx_reset_adapter,
2483 	.nvram_config		= qla81xx_nvram_config,
2484 	.update_fw_options	= qla24xx_update_fw_options,
2485 	.load_risc		= qla82xx_load_risc,
2486 	.pci_info_str		= qla24xx_pci_info_str,
2487 	.fw_version_str		= qla24xx_fw_version_str,
2488 	.intr_handler		= qla8044_intr_handler,
2489 	.enable_intrs		= qla82xx_enable_intrs,
2490 	.disable_intrs		= qla82xx_disable_intrs,
2491 	.abort_command		= qla24xx_abort_command,
2492 	.target_reset		= qla24xx_abort_target,
2493 	.lun_reset		= qla24xx_lun_reset,
2494 	.fabric_login		= qla24xx_login_fabric,
2495 	.fabric_logout		= qla24xx_fabric_logout,
2496 	.calc_req_entries	= NULL,
2497 	.build_iocbs		= NULL,
2498 	.prep_ms_iocb		= qla24xx_prep_ms_iocb,
2499 	.prep_ms_fdmi_iocb	= qla24xx_prep_ms_fdmi_iocb,
2500 	.read_nvram		= NULL,
2501 	.write_nvram		= NULL,
2502 	.fw_dump		= qla8044_fw_dump,
2503 	.beacon_on		= qla82xx_beacon_on,
2504 	.beacon_off		= qla82xx_beacon_off,
2505 	.beacon_blink		= NULL,
2506 	.read_optrom		= qla8044_read_optrom_data,
2507 	.write_optrom		= qla8044_write_optrom_data,
2508 	.get_flash_version	= qla82xx_get_flash_version,
2509 	.start_scsi             = qla82xx_start_scsi,
2510 	.start_scsi_mq          = NULL,
2511 	.abort_isp		= qla8044_abort_isp,
2512 	.iospace_config		= qla82xx_iospace_config,
2513 	.initialize_adapter	= qla2x00_initialize_adapter,
2514 };
2515 
2516 static struct isp_operations qla83xx_isp_ops = {
2517 	.pci_config		= qla25xx_pci_config,
2518 	.reset_chip		= qla24xx_reset_chip,
2519 	.chip_diag		= qla24xx_chip_diag,
2520 	.config_rings		= qla24xx_config_rings,
2521 	.reset_adapter		= qla24xx_reset_adapter,
2522 	.nvram_config		= qla81xx_nvram_config,
2523 	.update_fw_options	= qla24xx_update_fw_options,
2524 	.load_risc		= qla81xx_load_risc,
2525 	.pci_info_str		= qla24xx_pci_info_str,
2526 	.fw_version_str		= qla24xx_fw_version_str,
2527 	.intr_handler		= qla24xx_intr_handler,
2528 	.enable_intrs		= qla24xx_enable_intrs,
2529 	.disable_intrs		= qla24xx_disable_intrs,
2530 	.abort_command		= qla24xx_abort_command,
2531 	.target_reset		= qla24xx_abort_target,
2532 	.lun_reset		= qla24xx_lun_reset,
2533 	.fabric_login		= qla24xx_login_fabric,
2534 	.fabric_logout		= qla24xx_fabric_logout,
2535 	.calc_req_entries	= NULL,
2536 	.build_iocbs		= NULL,
2537 	.prep_ms_iocb		= qla24xx_prep_ms_iocb,
2538 	.prep_ms_fdmi_iocb	= qla24xx_prep_ms_fdmi_iocb,
2539 	.read_nvram		= NULL,
2540 	.write_nvram		= NULL,
2541 	.fw_dump		= qla83xx_fw_dump,
2542 	.beacon_on		= qla24xx_beacon_on,
2543 	.beacon_off		= qla24xx_beacon_off,
2544 	.beacon_blink		= qla83xx_beacon_blink,
2545 	.read_optrom		= qla25xx_read_optrom_data,
2546 	.write_optrom		= qla24xx_write_optrom_data,
2547 	.get_flash_version	= qla24xx_get_flash_version,
2548 	.start_scsi		= qla24xx_dif_start_scsi,
2549 	.start_scsi_mq          = qla2xxx_dif_start_scsi_mq,
2550 	.abort_isp		= qla2x00_abort_isp,
2551 	.iospace_config		= qla83xx_iospace_config,
2552 	.initialize_adapter	= qla2x00_initialize_adapter,
2553 };
2554 
2555 static struct isp_operations qlafx00_isp_ops = {
2556 	.pci_config		= qlafx00_pci_config,
2557 	.reset_chip		= qlafx00_soft_reset,
2558 	.chip_diag		= qlafx00_chip_diag,
2559 	.config_rings		= qlafx00_config_rings,
2560 	.reset_adapter		= qlafx00_soft_reset,
2561 	.nvram_config		= NULL,
2562 	.update_fw_options	= NULL,
2563 	.load_risc		= NULL,
2564 	.pci_info_str		= qlafx00_pci_info_str,
2565 	.fw_version_str		= qlafx00_fw_version_str,
2566 	.intr_handler		= qlafx00_intr_handler,
2567 	.enable_intrs		= qlafx00_enable_intrs,
2568 	.disable_intrs		= qlafx00_disable_intrs,
2569 	.abort_command		= qla24xx_async_abort_command,
2570 	.target_reset		= qlafx00_abort_target,
2571 	.lun_reset		= qlafx00_lun_reset,
2572 	.fabric_login		= NULL,
2573 	.fabric_logout		= NULL,
2574 	.calc_req_entries	= NULL,
2575 	.build_iocbs		= NULL,
2576 	.prep_ms_iocb		= qla24xx_prep_ms_iocb,
2577 	.prep_ms_fdmi_iocb	= qla24xx_prep_ms_fdmi_iocb,
2578 	.read_nvram		= qla24xx_read_nvram_data,
2579 	.write_nvram		= qla24xx_write_nvram_data,
2580 	.fw_dump		= NULL,
2581 	.beacon_on		= qla24xx_beacon_on,
2582 	.beacon_off		= qla24xx_beacon_off,
2583 	.beacon_blink		= NULL,
2584 	.read_optrom		= qla24xx_read_optrom_data,
2585 	.write_optrom		= qla24xx_write_optrom_data,
2586 	.get_flash_version	= qla24xx_get_flash_version,
2587 	.start_scsi		= qlafx00_start_scsi,
2588 	.start_scsi_mq          = NULL,
2589 	.abort_isp		= qlafx00_abort_isp,
2590 	.iospace_config		= qlafx00_iospace_config,
2591 	.initialize_adapter	= qlafx00_initialize_adapter,
2592 };
2593 
2594 static struct isp_operations qla27xx_isp_ops = {
2595 	.pci_config		= qla25xx_pci_config,
2596 	.reset_chip		= qla24xx_reset_chip,
2597 	.chip_diag		= qla24xx_chip_diag,
2598 	.config_rings		= qla24xx_config_rings,
2599 	.reset_adapter		= qla24xx_reset_adapter,
2600 	.nvram_config		= qla81xx_nvram_config,
2601 	.update_fw_options	= qla24xx_update_fw_options,
2602 	.load_risc		= qla81xx_load_risc,
2603 	.pci_info_str		= qla24xx_pci_info_str,
2604 	.fw_version_str		= qla24xx_fw_version_str,
2605 	.intr_handler		= qla24xx_intr_handler,
2606 	.enable_intrs		= qla24xx_enable_intrs,
2607 	.disable_intrs		= qla24xx_disable_intrs,
2608 	.abort_command		= qla24xx_abort_command,
2609 	.target_reset		= qla24xx_abort_target,
2610 	.lun_reset		= qla24xx_lun_reset,
2611 	.fabric_login		= qla24xx_login_fabric,
2612 	.fabric_logout		= qla24xx_fabric_logout,
2613 	.calc_req_entries	= NULL,
2614 	.build_iocbs		= NULL,
2615 	.prep_ms_iocb		= qla24xx_prep_ms_iocb,
2616 	.prep_ms_fdmi_iocb	= qla24xx_prep_ms_fdmi_iocb,
2617 	.read_nvram		= NULL,
2618 	.write_nvram		= NULL,
2619 	.fw_dump		= qla27xx_fwdump,
2620 	.mpi_fw_dump		= qla27xx_mpi_fwdump,
2621 	.beacon_on		= qla24xx_beacon_on,
2622 	.beacon_off		= qla24xx_beacon_off,
2623 	.beacon_blink		= qla83xx_beacon_blink,
2624 	.read_optrom		= qla25xx_read_optrom_data,
2625 	.write_optrom		= qla24xx_write_optrom_data,
2626 	.get_flash_version	= qla24xx_get_flash_version,
2627 	.start_scsi		= qla24xx_dif_start_scsi,
2628 	.start_scsi_mq          = qla2xxx_dif_start_scsi_mq,
2629 	.abort_isp		= qla2x00_abort_isp,
2630 	.iospace_config		= qla83xx_iospace_config,
2631 	.initialize_adapter	= qla2x00_initialize_adapter,
2632 };
2633 
2634 static inline void
2635 qla2x00_set_isp_flags(struct qla_hw_data *ha)
2636 {
2637 	ha->device_type = DT_EXTENDED_IDS;
2638 	switch (ha->pdev->device) {
2639 	case PCI_DEVICE_ID_QLOGIC_ISP2100:
2640 		ha->isp_type |= DT_ISP2100;
2641 		ha->device_type &= ~DT_EXTENDED_IDS;
2642 		ha->fw_srisc_address = RISC_START_ADDRESS_2100;
2643 		break;
2644 	case PCI_DEVICE_ID_QLOGIC_ISP2200:
2645 		ha->isp_type |= DT_ISP2200;
2646 		ha->device_type &= ~DT_EXTENDED_IDS;
2647 		ha->fw_srisc_address = RISC_START_ADDRESS_2100;
2648 		break;
2649 	case PCI_DEVICE_ID_QLOGIC_ISP2300:
2650 		ha->isp_type |= DT_ISP2300;
2651 		ha->device_type |= DT_ZIO_SUPPORTED;
2652 		ha->fw_srisc_address = RISC_START_ADDRESS_2300;
2653 		break;
2654 	case PCI_DEVICE_ID_QLOGIC_ISP2312:
2655 		ha->isp_type |= DT_ISP2312;
2656 		ha->device_type |= DT_ZIO_SUPPORTED;
2657 		ha->fw_srisc_address = RISC_START_ADDRESS_2300;
2658 		break;
2659 	case PCI_DEVICE_ID_QLOGIC_ISP2322:
2660 		ha->isp_type |= DT_ISP2322;
2661 		ha->device_type |= DT_ZIO_SUPPORTED;
2662 		if (ha->pdev->subsystem_vendor == 0x1028 &&
2663 		    ha->pdev->subsystem_device == 0x0170)
2664 			ha->device_type |= DT_OEM_001;
2665 		ha->fw_srisc_address = RISC_START_ADDRESS_2300;
2666 		break;
2667 	case PCI_DEVICE_ID_QLOGIC_ISP6312:
2668 		ha->isp_type |= DT_ISP6312;
2669 		ha->fw_srisc_address = RISC_START_ADDRESS_2300;
2670 		break;
2671 	case PCI_DEVICE_ID_QLOGIC_ISP6322:
2672 		ha->isp_type |= DT_ISP6322;
2673 		ha->fw_srisc_address = RISC_START_ADDRESS_2300;
2674 		break;
2675 	case PCI_DEVICE_ID_QLOGIC_ISP2422:
2676 		ha->isp_type |= DT_ISP2422;
2677 		ha->device_type |= DT_ZIO_SUPPORTED;
2678 		ha->device_type |= DT_FWI2;
2679 		ha->device_type |= DT_IIDMA;
2680 		ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2681 		break;
2682 	case PCI_DEVICE_ID_QLOGIC_ISP2432:
2683 		ha->isp_type |= DT_ISP2432;
2684 		ha->device_type |= DT_ZIO_SUPPORTED;
2685 		ha->device_type |= DT_FWI2;
2686 		ha->device_type |= DT_IIDMA;
2687 		ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2688 		break;
2689 	case PCI_DEVICE_ID_QLOGIC_ISP8432:
2690 		ha->isp_type |= DT_ISP8432;
2691 		ha->device_type |= DT_ZIO_SUPPORTED;
2692 		ha->device_type |= DT_FWI2;
2693 		ha->device_type |= DT_IIDMA;
2694 		ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2695 		break;
2696 	case PCI_DEVICE_ID_QLOGIC_ISP5422:
2697 		ha->isp_type |= DT_ISP5422;
2698 		ha->device_type |= DT_FWI2;
2699 		ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2700 		break;
2701 	case PCI_DEVICE_ID_QLOGIC_ISP5432:
2702 		ha->isp_type |= DT_ISP5432;
2703 		ha->device_type |= DT_FWI2;
2704 		ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2705 		break;
2706 	case PCI_DEVICE_ID_QLOGIC_ISP2532:
2707 		ha->isp_type |= DT_ISP2532;
2708 		ha->device_type |= DT_ZIO_SUPPORTED;
2709 		ha->device_type |= DT_FWI2;
2710 		ha->device_type |= DT_IIDMA;
2711 		ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2712 		break;
2713 	case PCI_DEVICE_ID_QLOGIC_ISP8001:
2714 		ha->isp_type |= DT_ISP8001;
2715 		ha->device_type |= DT_ZIO_SUPPORTED;
2716 		ha->device_type |= DT_FWI2;
2717 		ha->device_type |= DT_IIDMA;
2718 		ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2719 		break;
2720 	case PCI_DEVICE_ID_QLOGIC_ISP8021:
2721 		ha->isp_type |= DT_ISP8021;
2722 		ha->device_type |= DT_ZIO_SUPPORTED;
2723 		ha->device_type |= DT_FWI2;
2724 		ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2725 		/* Initialize 82XX ISP flags */
2726 		qla82xx_init_flags(ha);
2727 		break;
2728 	 case PCI_DEVICE_ID_QLOGIC_ISP8044:
2729 		ha->isp_type |= DT_ISP8044;
2730 		ha->device_type |= DT_ZIO_SUPPORTED;
2731 		ha->device_type |= DT_FWI2;
2732 		ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2733 		/* Initialize 82XX ISP flags */
2734 		qla82xx_init_flags(ha);
2735 		break;
2736 	case PCI_DEVICE_ID_QLOGIC_ISP2031:
2737 		ha->isp_type |= DT_ISP2031;
2738 		ha->device_type |= DT_ZIO_SUPPORTED;
2739 		ha->device_type |= DT_FWI2;
2740 		ha->device_type |= DT_IIDMA;
2741 		ha->device_type |= DT_T10_PI;
2742 		ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2743 		break;
2744 	case PCI_DEVICE_ID_QLOGIC_ISP8031:
2745 		ha->isp_type |= DT_ISP8031;
2746 		ha->device_type |= DT_ZIO_SUPPORTED;
2747 		ha->device_type |= DT_FWI2;
2748 		ha->device_type |= DT_IIDMA;
2749 		ha->device_type |= DT_T10_PI;
2750 		ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2751 		break;
2752 	case PCI_DEVICE_ID_QLOGIC_ISPF001:
2753 		ha->isp_type |= DT_ISPFX00;
2754 		break;
2755 	case PCI_DEVICE_ID_QLOGIC_ISP2071:
2756 		ha->isp_type |= DT_ISP2071;
2757 		ha->device_type |= DT_ZIO_SUPPORTED;
2758 		ha->device_type |= DT_FWI2;
2759 		ha->device_type |= DT_IIDMA;
2760 		ha->device_type |= DT_T10_PI;
2761 		ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2762 		break;
2763 	case PCI_DEVICE_ID_QLOGIC_ISP2271:
2764 		ha->isp_type |= DT_ISP2271;
2765 		ha->device_type |= DT_ZIO_SUPPORTED;
2766 		ha->device_type |= DT_FWI2;
2767 		ha->device_type |= DT_IIDMA;
2768 		ha->device_type |= DT_T10_PI;
2769 		ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2770 		break;
2771 	case PCI_DEVICE_ID_QLOGIC_ISP2261:
2772 		ha->isp_type |= DT_ISP2261;
2773 		ha->device_type |= DT_ZIO_SUPPORTED;
2774 		ha->device_type |= DT_FWI2;
2775 		ha->device_type |= DT_IIDMA;
2776 		ha->device_type |= DT_T10_PI;
2777 		ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2778 		break;
2779 	case PCI_DEVICE_ID_QLOGIC_ISP2081:
2780 	case PCI_DEVICE_ID_QLOGIC_ISP2089:
2781 		ha->isp_type |= DT_ISP2081;
2782 		ha->device_type |= DT_ZIO_SUPPORTED;
2783 		ha->device_type |= DT_FWI2;
2784 		ha->device_type |= DT_IIDMA;
2785 		ha->device_type |= DT_T10_PI;
2786 		ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2787 		break;
2788 	case PCI_DEVICE_ID_QLOGIC_ISP2281:
2789 	case PCI_DEVICE_ID_QLOGIC_ISP2289:
2790 		ha->isp_type |= DT_ISP2281;
2791 		ha->device_type |= DT_ZIO_SUPPORTED;
2792 		ha->device_type |= DT_FWI2;
2793 		ha->device_type |= DT_IIDMA;
2794 		ha->device_type |= DT_T10_PI;
2795 		ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2796 		break;
2797 	}
2798 
2799 	if (IS_QLA82XX(ha))
2800 		ha->port_no = ha->portnum & 1;
2801 	else {
2802 		/* Get adapter physical port no from interrupt pin register. */
2803 		pci_read_config_byte(ha->pdev, PCI_INTERRUPT_PIN, &ha->port_no);
2804 		if (IS_QLA25XX(ha) || IS_QLA2031(ha) ||
2805 		    IS_QLA27XX(ha) || IS_QLA28XX(ha))
2806 			ha->port_no--;
2807 		else
2808 			ha->port_no = !(ha->port_no & 1);
2809 	}
2810 
2811 	ql_dbg_pci(ql_dbg_init, ha->pdev, 0x000b,
2812 	    "device_type=0x%x port=%d fw_srisc_address=0x%x.\n",
2813 	    ha->device_type, ha->port_no, ha->fw_srisc_address);
2814 }
2815 
2816 static void
2817 qla2xxx_scan_start(struct Scsi_Host *shost)
2818 {
2819 	scsi_qla_host_t *vha = shost_priv(shost);
2820 
2821 	if (vha->hw->flags.running_gold_fw)
2822 		return;
2823 
2824 	set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
2825 	set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
2826 	set_bit(RSCN_UPDATE, &vha->dpc_flags);
2827 	set_bit(NPIV_CONFIG_NEEDED, &vha->dpc_flags);
2828 }
2829 
2830 static int
2831 qla2xxx_scan_finished(struct Scsi_Host *shost, unsigned long time)
2832 {
2833 	scsi_qla_host_t *vha = shost_priv(shost);
2834 
2835 	if (test_bit(UNLOADING, &vha->dpc_flags))
2836 		return 1;
2837 	if (!vha->host)
2838 		return 1;
2839 	if (time > vha->hw->loop_reset_delay * HZ)
2840 		return 1;
2841 
2842 	return atomic_read(&vha->loop_state) == LOOP_READY;
2843 }
2844 
2845 static void qla_heartbeat_work_fn(struct work_struct *work)
2846 {
2847 	struct qla_hw_data *ha = container_of(work,
2848 		struct qla_hw_data, heartbeat_work);
2849 	struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
2850 
2851 	if (!ha->flags.mbox_busy && base_vha->flags.init_done)
2852 		qla_no_op_mb(base_vha);
2853 }
2854 
2855 static void qla2x00_iocb_work_fn(struct work_struct *work)
2856 {
2857 	struct scsi_qla_host *vha = container_of(work,
2858 		struct scsi_qla_host, iocb_work);
2859 	struct qla_hw_data *ha = vha->hw;
2860 	struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
2861 	int i = 2;
2862 	unsigned long flags;
2863 
2864 	if (test_bit(UNLOADING, &base_vha->dpc_flags))
2865 		return;
2866 
2867 	while (!list_empty(&vha->work_list) && i > 0) {
2868 		qla2x00_do_work(vha);
2869 		i--;
2870 	}
2871 
2872 	spin_lock_irqsave(&vha->work_lock, flags);
2873 	clear_bit(IOCB_WORK_ACTIVE, &vha->dpc_flags);
2874 	spin_unlock_irqrestore(&vha->work_lock, flags);
2875 }
2876 
2877 static void
2878 qla_trace_init(void)
2879 {
2880 	qla_trc_array = trace_array_get_by_name("qla2xxx");
2881 	if (!qla_trc_array) {
2882 		ql_log(ql_log_fatal, NULL, 0x0001,
2883 		       "Unable to create qla2xxx trace instance, instance logging will be disabled.\n");
2884 		return;
2885 	}
2886 
2887 	QLA_TRACE_ENABLE(qla_trc_array);
2888 }
2889 
2890 static void
2891 qla_trace_uninit(void)
2892 {
2893 	if (!qla_trc_array)
2894 		return;
2895 	trace_array_put(qla_trc_array);
2896 }
2897 
2898 /*
2899  * PCI driver interface
2900  */
2901 static int
2902 qla2x00_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
2903 {
2904 	int	ret = -ENODEV;
2905 	struct Scsi_Host *host;
2906 	scsi_qla_host_t *base_vha = NULL;
2907 	struct qla_hw_data *ha;
2908 	char pci_info[30];
2909 	char fw_str[30], wq_name[30];
2910 	struct scsi_host_template *sht;
2911 	int bars, mem_only = 0;
2912 	uint16_t req_length = 0, rsp_length = 0;
2913 	struct req_que *req = NULL;
2914 	struct rsp_que *rsp = NULL;
2915 	int i;
2916 
2917 	bars = pci_select_bars(pdev, IORESOURCE_MEM | IORESOURCE_IO);
2918 	sht = &qla2xxx_driver_template;
2919 	if (pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2422 ||
2920 	    pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2432 ||
2921 	    pdev->device == PCI_DEVICE_ID_QLOGIC_ISP8432 ||
2922 	    pdev->device == PCI_DEVICE_ID_QLOGIC_ISP5422 ||
2923 	    pdev->device == PCI_DEVICE_ID_QLOGIC_ISP5432 ||
2924 	    pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2532 ||
2925 	    pdev->device == PCI_DEVICE_ID_QLOGIC_ISP8001 ||
2926 	    pdev->device == PCI_DEVICE_ID_QLOGIC_ISP8021 ||
2927 	    pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2031 ||
2928 	    pdev->device == PCI_DEVICE_ID_QLOGIC_ISP8031 ||
2929 	    pdev->device == PCI_DEVICE_ID_QLOGIC_ISPF001 ||
2930 	    pdev->device == PCI_DEVICE_ID_QLOGIC_ISP8044 ||
2931 	    pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2071 ||
2932 	    pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2271 ||
2933 	    pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2261 ||
2934 	    pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2081 ||
2935 	    pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2281 ||
2936 	    pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2089 ||
2937 	    pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2289) {
2938 		bars = pci_select_bars(pdev, IORESOURCE_MEM);
2939 		mem_only = 1;
2940 		ql_dbg_pci(ql_dbg_init, pdev, 0x0007,
2941 		    "Mem only adapter.\n");
2942 	}
2943 	ql_dbg_pci(ql_dbg_init, pdev, 0x0008,
2944 	    "Bars=%d.\n", bars);
2945 
2946 	if (mem_only) {
2947 		if (pci_enable_device_mem(pdev))
2948 			return ret;
2949 	} else {
2950 		if (pci_enable_device(pdev))
2951 			return ret;
2952 	}
2953 
2954 	if (is_kdump_kernel()) {
2955 		ql2xmqsupport = 0;
2956 		ql2xallocfwdump = 0;
2957 	}
2958 
2959 	ha = kzalloc(sizeof(struct qla_hw_data), GFP_KERNEL);
2960 	if (!ha) {
2961 		ql_log_pci(ql_log_fatal, pdev, 0x0009,
2962 		    "Unable to allocate memory for ha.\n");
2963 		goto disable_device;
2964 	}
2965 	ql_dbg_pci(ql_dbg_init, pdev, 0x000a,
2966 	    "Memory allocated for ha=%p.\n", ha);
2967 	ha->pdev = pdev;
2968 	INIT_LIST_HEAD(&ha->tgt.q_full_list);
2969 	spin_lock_init(&ha->tgt.q_full_lock);
2970 	spin_lock_init(&ha->tgt.sess_lock);
2971 	spin_lock_init(&ha->tgt.atio_lock);
2972 
2973 	spin_lock_init(&ha->sadb_lock);
2974 	INIT_LIST_HEAD(&ha->sadb_tx_index_list);
2975 	INIT_LIST_HEAD(&ha->sadb_rx_index_list);
2976 
2977 	spin_lock_init(&ha->sadb_fp_lock);
2978 
2979 	if (qla_edif_sadb_build_free_pool(ha)) {
2980 		kfree(ha);
2981 		goto  disable_device;
2982 	}
2983 
2984 	atomic_set(&ha->nvme_active_aen_cnt, 0);
2985 
2986 	/* Clear our data area */
2987 	ha->bars = bars;
2988 	ha->mem_only = mem_only;
2989 	spin_lock_init(&ha->hardware_lock);
2990 	spin_lock_init(&ha->vport_slock);
2991 	mutex_init(&ha->selflogin_lock);
2992 	mutex_init(&ha->optrom_mutex);
2993 
2994 	/* Set ISP-type information. */
2995 	qla2x00_set_isp_flags(ha);
2996 
2997 	/* Set EEH reset type to fundamental if required by hba */
2998 	if (IS_QLA24XX(ha) || IS_QLA25XX(ha) || IS_QLA81XX(ha) ||
2999 	    IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha))
3000 		pdev->needs_freset = 1;
3001 
3002 	ha->prev_topology = 0;
3003 	ha->init_cb_size = sizeof(init_cb_t);
3004 	ha->link_data_rate = PORT_SPEED_UNKNOWN;
3005 	ha->optrom_size = OPTROM_SIZE_2300;
3006 	ha->max_exchg = FW_MAX_EXCHANGES_CNT;
3007 	atomic_set(&ha->num_pend_mbx_stage1, 0);
3008 	atomic_set(&ha->num_pend_mbx_stage2, 0);
3009 	atomic_set(&ha->num_pend_mbx_stage3, 0);
3010 	atomic_set(&ha->zio_threshold, DEFAULT_ZIO_THRESHOLD);
3011 	ha->last_zio_threshold = DEFAULT_ZIO_THRESHOLD;
3012 
3013 	/* Assign ISP specific operations. */
3014 	if (IS_QLA2100(ha)) {
3015 		ha->max_fibre_devices = MAX_FIBRE_DEVICES_2100;
3016 		ha->mbx_count = MAILBOX_REGISTER_COUNT_2100;
3017 		req_length = REQUEST_ENTRY_CNT_2100;
3018 		rsp_length = RESPONSE_ENTRY_CNT_2100;
3019 		ha->max_loop_id = SNS_LAST_LOOP_ID_2100;
3020 		ha->gid_list_info_size = 4;
3021 		ha->flash_conf_off = ~0;
3022 		ha->flash_data_off = ~0;
3023 		ha->nvram_conf_off = ~0;
3024 		ha->nvram_data_off = ~0;
3025 		ha->isp_ops = &qla2100_isp_ops;
3026 	} else if (IS_QLA2200(ha)) {
3027 		ha->max_fibre_devices = MAX_FIBRE_DEVICES_2100;
3028 		ha->mbx_count = MAILBOX_REGISTER_COUNT_2200;
3029 		req_length = REQUEST_ENTRY_CNT_2200;
3030 		rsp_length = RESPONSE_ENTRY_CNT_2100;
3031 		ha->max_loop_id = SNS_LAST_LOOP_ID_2100;
3032 		ha->gid_list_info_size = 4;
3033 		ha->flash_conf_off = ~0;
3034 		ha->flash_data_off = ~0;
3035 		ha->nvram_conf_off = ~0;
3036 		ha->nvram_data_off = ~0;
3037 		ha->isp_ops = &qla2100_isp_ops;
3038 	} else if (IS_QLA23XX(ha)) {
3039 		ha->max_fibre_devices = MAX_FIBRE_DEVICES_2100;
3040 		ha->mbx_count = MAILBOX_REGISTER_COUNT;
3041 		req_length = REQUEST_ENTRY_CNT_2200;
3042 		rsp_length = RESPONSE_ENTRY_CNT_2300;
3043 		ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
3044 		ha->gid_list_info_size = 6;
3045 		if (IS_QLA2322(ha) || IS_QLA6322(ha))
3046 			ha->optrom_size = OPTROM_SIZE_2322;
3047 		ha->flash_conf_off = ~0;
3048 		ha->flash_data_off = ~0;
3049 		ha->nvram_conf_off = ~0;
3050 		ha->nvram_data_off = ~0;
3051 		ha->isp_ops = &qla2300_isp_ops;
3052 	} else if (IS_QLA24XX_TYPE(ha)) {
3053 		ha->max_fibre_devices = MAX_FIBRE_DEVICES_2400;
3054 		ha->mbx_count = MAILBOX_REGISTER_COUNT;
3055 		req_length = REQUEST_ENTRY_CNT_24XX;
3056 		rsp_length = RESPONSE_ENTRY_CNT_2300;
3057 		ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
3058 		ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
3059 		ha->init_cb_size = sizeof(struct mid_init_cb_24xx);
3060 		ha->gid_list_info_size = 8;
3061 		ha->optrom_size = OPTROM_SIZE_24XX;
3062 		ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA24XX;
3063 		ha->isp_ops = &qla24xx_isp_ops;
3064 		ha->flash_conf_off = FARX_ACCESS_FLASH_CONF;
3065 		ha->flash_data_off = FARX_ACCESS_FLASH_DATA;
3066 		ha->nvram_conf_off = FARX_ACCESS_NVRAM_CONF;
3067 		ha->nvram_data_off = FARX_ACCESS_NVRAM_DATA;
3068 	} else if (IS_QLA25XX(ha)) {
3069 		ha->max_fibre_devices = MAX_FIBRE_DEVICES_2400;
3070 		ha->mbx_count = MAILBOX_REGISTER_COUNT;
3071 		req_length = REQUEST_ENTRY_CNT_24XX;
3072 		rsp_length = RESPONSE_ENTRY_CNT_2300;
3073 		ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
3074 		ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
3075 		ha->init_cb_size = sizeof(struct mid_init_cb_24xx);
3076 		ha->gid_list_info_size = 8;
3077 		ha->optrom_size = OPTROM_SIZE_25XX;
3078 		ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA25XX;
3079 		ha->isp_ops = &qla25xx_isp_ops;
3080 		ha->flash_conf_off = FARX_ACCESS_FLASH_CONF;
3081 		ha->flash_data_off = FARX_ACCESS_FLASH_DATA;
3082 		ha->nvram_conf_off = FARX_ACCESS_NVRAM_CONF;
3083 		ha->nvram_data_off = FARX_ACCESS_NVRAM_DATA;
3084 	} else if (IS_QLA81XX(ha)) {
3085 		ha->max_fibre_devices = MAX_FIBRE_DEVICES_2400;
3086 		ha->mbx_count = MAILBOX_REGISTER_COUNT;
3087 		req_length = REQUEST_ENTRY_CNT_24XX;
3088 		rsp_length = RESPONSE_ENTRY_CNT_2300;
3089 		ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
3090 		ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
3091 		ha->init_cb_size = sizeof(struct mid_init_cb_81xx);
3092 		ha->gid_list_info_size = 8;
3093 		ha->optrom_size = OPTROM_SIZE_81XX;
3094 		ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA25XX;
3095 		ha->isp_ops = &qla81xx_isp_ops;
3096 		ha->flash_conf_off = FARX_ACCESS_FLASH_CONF_81XX;
3097 		ha->flash_data_off = FARX_ACCESS_FLASH_DATA_81XX;
3098 		ha->nvram_conf_off = ~0;
3099 		ha->nvram_data_off = ~0;
3100 	} else if (IS_QLA82XX(ha)) {
3101 		ha->max_fibre_devices = MAX_FIBRE_DEVICES_2400;
3102 		ha->mbx_count = MAILBOX_REGISTER_COUNT;
3103 		req_length = REQUEST_ENTRY_CNT_82XX;
3104 		rsp_length = RESPONSE_ENTRY_CNT_82XX;
3105 		ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
3106 		ha->init_cb_size = sizeof(struct mid_init_cb_81xx);
3107 		ha->gid_list_info_size = 8;
3108 		ha->optrom_size = OPTROM_SIZE_82XX;
3109 		ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA25XX;
3110 		ha->isp_ops = &qla82xx_isp_ops;
3111 		ha->flash_conf_off = FARX_ACCESS_FLASH_CONF;
3112 		ha->flash_data_off = FARX_ACCESS_FLASH_DATA;
3113 		ha->nvram_conf_off = FARX_ACCESS_NVRAM_CONF;
3114 		ha->nvram_data_off = FARX_ACCESS_NVRAM_DATA;
3115 	} else if (IS_QLA8044(ha)) {
3116 		ha->max_fibre_devices = MAX_FIBRE_DEVICES_2400;
3117 		ha->mbx_count = MAILBOX_REGISTER_COUNT;
3118 		req_length = REQUEST_ENTRY_CNT_82XX;
3119 		rsp_length = RESPONSE_ENTRY_CNT_82XX;
3120 		ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
3121 		ha->init_cb_size = sizeof(struct mid_init_cb_81xx);
3122 		ha->gid_list_info_size = 8;
3123 		ha->optrom_size = OPTROM_SIZE_83XX;
3124 		ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA25XX;
3125 		ha->isp_ops = &qla8044_isp_ops;
3126 		ha->flash_conf_off = FARX_ACCESS_FLASH_CONF;
3127 		ha->flash_data_off = FARX_ACCESS_FLASH_DATA;
3128 		ha->nvram_conf_off = FARX_ACCESS_NVRAM_CONF;
3129 		ha->nvram_data_off = FARX_ACCESS_NVRAM_DATA;
3130 	} else if (IS_QLA83XX(ha)) {
3131 		ha->portnum = PCI_FUNC(ha->pdev->devfn);
3132 		ha->max_fibre_devices = MAX_FIBRE_DEVICES_2400;
3133 		ha->mbx_count = MAILBOX_REGISTER_COUNT;
3134 		req_length = REQUEST_ENTRY_CNT_83XX;
3135 		rsp_length = RESPONSE_ENTRY_CNT_83XX;
3136 		ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
3137 		ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
3138 		ha->init_cb_size = sizeof(struct mid_init_cb_81xx);
3139 		ha->gid_list_info_size = 8;
3140 		ha->optrom_size = OPTROM_SIZE_83XX;
3141 		ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA25XX;
3142 		ha->isp_ops = &qla83xx_isp_ops;
3143 		ha->flash_conf_off = FARX_ACCESS_FLASH_CONF_81XX;
3144 		ha->flash_data_off = FARX_ACCESS_FLASH_DATA_81XX;
3145 		ha->nvram_conf_off = ~0;
3146 		ha->nvram_data_off = ~0;
3147 	}  else if (IS_QLAFX00(ha)) {
3148 		ha->max_fibre_devices = MAX_FIBRE_DEVICES_FX00;
3149 		ha->mbx_count = MAILBOX_REGISTER_COUNT_FX00;
3150 		ha->aen_mbx_count = AEN_MAILBOX_REGISTER_COUNT_FX00;
3151 		req_length = REQUEST_ENTRY_CNT_FX00;
3152 		rsp_length = RESPONSE_ENTRY_CNT_FX00;
3153 		ha->isp_ops = &qlafx00_isp_ops;
3154 		ha->port_down_retry_count = 30; /* default value */
3155 		ha->mr.fw_hbt_cnt = QLAFX00_HEARTBEAT_INTERVAL;
3156 		ha->mr.fw_reset_timer_tick = QLAFX00_RESET_INTERVAL;
3157 		ha->mr.fw_critemp_timer_tick = QLAFX00_CRITEMP_INTERVAL;
3158 		ha->mr.fw_hbt_en = 1;
3159 		ha->mr.host_info_resend = false;
3160 		ha->mr.hinfo_resend_timer_tick = QLAFX00_HINFO_RESEND_INTERVAL;
3161 	} else if (IS_QLA27XX(ha)) {
3162 		ha->portnum = PCI_FUNC(ha->pdev->devfn);
3163 		ha->max_fibre_devices = MAX_FIBRE_DEVICES_2400;
3164 		ha->mbx_count = MAILBOX_REGISTER_COUNT;
3165 		req_length = REQUEST_ENTRY_CNT_83XX;
3166 		rsp_length = RESPONSE_ENTRY_CNT_83XX;
3167 		ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
3168 		ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
3169 		ha->init_cb_size = sizeof(struct mid_init_cb_81xx);
3170 		ha->gid_list_info_size = 8;
3171 		ha->optrom_size = OPTROM_SIZE_83XX;
3172 		ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA25XX;
3173 		ha->isp_ops = &qla27xx_isp_ops;
3174 		ha->flash_conf_off = FARX_ACCESS_FLASH_CONF_81XX;
3175 		ha->flash_data_off = FARX_ACCESS_FLASH_DATA_81XX;
3176 		ha->nvram_conf_off = ~0;
3177 		ha->nvram_data_off = ~0;
3178 	} else if (IS_QLA28XX(ha)) {
3179 		ha->portnum = PCI_FUNC(ha->pdev->devfn);
3180 		ha->max_fibre_devices = MAX_FIBRE_DEVICES_2400;
3181 		ha->mbx_count = MAILBOX_REGISTER_COUNT;
3182 		req_length = REQUEST_ENTRY_CNT_83XX;
3183 		rsp_length = RESPONSE_ENTRY_CNT_83XX;
3184 		ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
3185 		ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
3186 		ha->init_cb_size = sizeof(struct mid_init_cb_81xx);
3187 		ha->gid_list_info_size = 8;
3188 		ha->optrom_size = OPTROM_SIZE_28XX;
3189 		ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA25XX;
3190 		ha->isp_ops = &qla27xx_isp_ops;
3191 		ha->flash_conf_off = FARX_ACCESS_FLASH_CONF_28XX;
3192 		ha->flash_data_off = FARX_ACCESS_FLASH_DATA_28XX;
3193 		ha->nvram_conf_off = ~0;
3194 		ha->nvram_data_off = ~0;
3195 	}
3196 
3197 	ql_dbg_pci(ql_dbg_init, pdev, 0x001e,
3198 	    "mbx_count=%d, req_length=%d, "
3199 	    "rsp_length=%d, max_loop_id=%d, init_cb_size=%d, "
3200 	    "gid_list_info_size=%d, optrom_size=%d, nvram_npiv_size=%d, "
3201 	    "max_fibre_devices=%d.\n",
3202 	    ha->mbx_count, req_length, rsp_length, ha->max_loop_id,
3203 	    ha->init_cb_size, ha->gid_list_info_size, ha->optrom_size,
3204 	    ha->nvram_npiv_size, ha->max_fibre_devices);
3205 	ql_dbg_pci(ql_dbg_init, pdev, 0x001f,
3206 	    "isp_ops=%p, flash_conf_off=%d, "
3207 	    "flash_data_off=%d, nvram_conf_off=%d, nvram_data_off=%d.\n",
3208 	    ha->isp_ops, ha->flash_conf_off, ha->flash_data_off,
3209 	    ha->nvram_conf_off, ha->nvram_data_off);
3210 
3211 	/* Configure PCI I/O space */
3212 	ret = ha->isp_ops->iospace_config(ha);
3213 	if (ret)
3214 		goto iospace_config_failed;
3215 
3216 	ql_log_pci(ql_log_info, pdev, 0x001d,
3217 	    "Found an ISP%04X irq %d iobase 0x%p.\n",
3218 	    pdev->device, pdev->irq, ha->iobase);
3219 	mutex_init(&ha->vport_lock);
3220 	mutex_init(&ha->mq_lock);
3221 	init_completion(&ha->mbx_cmd_comp);
3222 	complete(&ha->mbx_cmd_comp);
3223 	init_completion(&ha->mbx_intr_comp);
3224 	init_completion(&ha->dcbx_comp);
3225 	init_completion(&ha->lb_portup_comp);
3226 
3227 	set_bit(0, (unsigned long *) ha->vp_idx_map);
3228 
3229 	qla2x00_config_dma_addressing(ha);
3230 	ql_dbg_pci(ql_dbg_init, pdev, 0x0020,
3231 	    "64 Bit addressing is %s.\n",
3232 	    ha->flags.enable_64bit_addressing ? "enable" :
3233 	    "disable");
3234 	ret = qla2x00_mem_alloc(ha, req_length, rsp_length, &req, &rsp);
3235 	if (ret) {
3236 		ql_log_pci(ql_log_fatal, pdev, 0x0031,
3237 		    "Failed to allocate memory for adapter, aborting.\n");
3238 
3239 		goto probe_hw_failed;
3240 	}
3241 
3242 	req->max_q_depth = MAX_Q_DEPTH;
3243 	if (ql2xmaxqdepth != 0 && ql2xmaxqdepth <= 0xffffU)
3244 		req->max_q_depth = ql2xmaxqdepth;
3245 
3246 
3247 	base_vha = qla2x00_create_host(sht, ha);
3248 	if (!base_vha) {
3249 		ret = -ENOMEM;
3250 		goto probe_hw_failed;
3251 	}
3252 
3253 	pci_set_drvdata(pdev, base_vha);
3254 	set_bit(PFLG_DRIVER_PROBING, &base_vha->pci_flags);
3255 
3256 	host = base_vha->host;
3257 	base_vha->req = req;
3258 	if (IS_QLA2XXX_MIDTYPE(ha))
3259 		base_vha->mgmt_svr_loop_id =
3260 			qla2x00_reserve_mgmt_server_loop_id(base_vha);
3261 	else
3262 		base_vha->mgmt_svr_loop_id = MANAGEMENT_SERVER +
3263 						base_vha->vp_idx;
3264 
3265 	/* Setup fcport template structure. */
3266 	ha->mr.fcport.vha = base_vha;
3267 	ha->mr.fcport.port_type = FCT_UNKNOWN;
3268 	ha->mr.fcport.loop_id = FC_NO_LOOP_ID;
3269 	qla2x00_set_fcport_state(&ha->mr.fcport, FCS_UNCONFIGURED);
3270 	ha->mr.fcport.supported_classes = FC_COS_UNSPECIFIED;
3271 	ha->mr.fcport.scan_state = 1;
3272 
3273 	qla2xxx_reset_stats(host, QLA2XX_HW_ERROR | QLA2XX_SHT_LNK_DWN |
3274 			    QLA2XX_INT_ERR | QLA2XX_CMD_TIMEOUT |
3275 			    QLA2XX_RESET_CMD_ERR | QLA2XX_TGT_SHT_LNK_DOWN);
3276 
3277 	/* Set the SG table size based on ISP type */
3278 	if (!IS_FWI2_CAPABLE(ha)) {
3279 		if (IS_QLA2100(ha))
3280 			host->sg_tablesize = 32;
3281 	} else {
3282 		if (!IS_QLA82XX(ha))
3283 			host->sg_tablesize = QLA_SG_ALL;
3284 	}
3285 	host->max_id = ha->max_fibre_devices;
3286 	host->cmd_per_lun = 3;
3287 	host->unique_id = host->host_no;
3288 	if (IS_T10_PI_CAPABLE(ha) && ql2xenabledif)
3289 		host->max_cmd_len = 32;
3290 	else
3291 		host->max_cmd_len = MAX_CMDSZ;
3292 	host->max_channel = MAX_BUSES - 1;
3293 	/* Older HBAs support only 16-bit LUNs */
3294 	if (!IS_QLAFX00(ha) && !IS_FWI2_CAPABLE(ha) &&
3295 	    ql2xmaxlun > 0xffff)
3296 		host->max_lun = 0xffff;
3297 	else
3298 		host->max_lun = ql2xmaxlun;
3299 	host->transportt = qla2xxx_transport_template;
3300 	sht->vendor_id = (SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_QLOGIC);
3301 
3302 	ql_dbg(ql_dbg_init, base_vha, 0x0033,
3303 	    "max_id=%d this_id=%d "
3304 	    "cmd_per_len=%d unique_id=%d max_cmd_len=%d max_channel=%d "
3305 	    "max_lun=%llu transportt=%p, vendor_id=%llu.\n", host->max_id,
3306 	    host->this_id, host->cmd_per_lun, host->unique_id,
3307 	    host->max_cmd_len, host->max_channel, host->max_lun,
3308 	    host->transportt, sht->vendor_id);
3309 
3310 	INIT_WORK(&ha->heartbeat_work, qla_heartbeat_work_fn);
3311 
3312 	/* Set up the irqs */
3313 	ret = qla2x00_request_irqs(ha, rsp);
3314 	if (ret)
3315 		goto probe_failed;
3316 
3317 	/* Alloc arrays of request and response ring ptrs */
3318 	ret = qla2x00_alloc_queues(ha, req, rsp);
3319 	if (ret) {
3320 		ql_log(ql_log_fatal, base_vha, 0x003d,
3321 		    "Failed to allocate memory for queue pointers..."
3322 		    "aborting.\n");
3323 		ret = -ENODEV;
3324 		goto probe_failed;
3325 	}
3326 
3327 	if (ha->mqenable) {
3328 		/* number of hardware queues supported by blk/scsi-mq*/
3329 		host->nr_hw_queues = ha->max_qpairs;
3330 
3331 		ql_dbg(ql_dbg_init, base_vha, 0x0192,
3332 			"blk/scsi-mq enabled, HW queues = %d.\n", host->nr_hw_queues);
3333 	} else {
3334 		if (ql2xnvmeenable) {
3335 			host->nr_hw_queues = ha->max_qpairs;
3336 			ql_dbg(ql_dbg_init, base_vha, 0x0194,
3337 			    "FC-NVMe support is enabled, HW queues=%d\n",
3338 			    host->nr_hw_queues);
3339 		} else {
3340 			ql_dbg(ql_dbg_init, base_vha, 0x0193,
3341 			    "blk/scsi-mq disabled.\n");
3342 		}
3343 	}
3344 
3345 	qlt_probe_one_stage1(base_vha, ha);
3346 
3347 	pci_save_state(pdev);
3348 
3349 	/* Assign back pointers */
3350 	rsp->req = req;
3351 	req->rsp = rsp;
3352 
3353 	if (IS_QLAFX00(ha)) {
3354 		ha->rsp_q_map[0] = rsp;
3355 		ha->req_q_map[0] = req;
3356 		set_bit(0, ha->req_qid_map);
3357 		set_bit(0, ha->rsp_qid_map);
3358 	}
3359 
3360 	/* FWI2-capable only. */
3361 	req->req_q_in = &ha->iobase->isp24.req_q_in;
3362 	req->req_q_out = &ha->iobase->isp24.req_q_out;
3363 	rsp->rsp_q_in = &ha->iobase->isp24.rsp_q_in;
3364 	rsp->rsp_q_out = &ha->iobase->isp24.rsp_q_out;
3365 	if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha) ||
3366 	    IS_QLA28XX(ha)) {
3367 		req->req_q_in = &ha->mqiobase->isp25mq.req_q_in;
3368 		req->req_q_out = &ha->mqiobase->isp25mq.req_q_out;
3369 		rsp->rsp_q_in = &ha->mqiobase->isp25mq.rsp_q_in;
3370 		rsp->rsp_q_out =  &ha->mqiobase->isp25mq.rsp_q_out;
3371 	}
3372 
3373 	if (IS_QLAFX00(ha)) {
3374 		req->req_q_in = &ha->iobase->ispfx00.req_q_in;
3375 		req->req_q_out = &ha->iobase->ispfx00.req_q_out;
3376 		rsp->rsp_q_in = &ha->iobase->ispfx00.rsp_q_in;
3377 		rsp->rsp_q_out = &ha->iobase->ispfx00.rsp_q_out;
3378 	}
3379 
3380 	if (IS_P3P_TYPE(ha)) {
3381 		req->req_q_out = &ha->iobase->isp82.req_q_out[0];
3382 		rsp->rsp_q_in = &ha->iobase->isp82.rsp_q_in[0];
3383 		rsp->rsp_q_out = &ha->iobase->isp82.rsp_q_out[0];
3384 	}
3385 
3386 	ql_dbg(ql_dbg_multiq, base_vha, 0xc009,
3387 	    "rsp_q_map=%p req_q_map=%p rsp->req=%p req->rsp=%p.\n",
3388 	    ha->rsp_q_map, ha->req_q_map, rsp->req, req->rsp);
3389 	ql_dbg(ql_dbg_multiq, base_vha, 0xc00a,
3390 	    "req->req_q_in=%p req->req_q_out=%p "
3391 	    "rsp->rsp_q_in=%p rsp->rsp_q_out=%p.\n",
3392 	    req->req_q_in, req->req_q_out,
3393 	    rsp->rsp_q_in, rsp->rsp_q_out);
3394 	ql_dbg(ql_dbg_init, base_vha, 0x003e,
3395 	    "rsp_q_map=%p req_q_map=%p rsp->req=%p req->rsp=%p.\n",
3396 	    ha->rsp_q_map, ha->req_q_map, rsp->req, req->rsp);
3397 	ql_dbg(ql_dbg_init, base_vha, 0x003f,
3398 	    "req->req_q_in=%p req->req_q_out=%p rsp->rsp_q_in=%p rsp->rsp_q_out=%p.\n",
3399 	    req->req_q_in, req->req_q_out, rsp->rsp_q_in, rsp->rsp_q_out);
3400 
3401 	ha->wq = alloc_workqueue("qla2xxx_wq", WQ_MEM_RECLAIM, 0);
3402 	if (unlikely(!ha->wq)) {
3403 		ret = -ENOMEM;
3404 		goto probe_failed;
3405 	}
3406 
3407 	if (ha->isp_ops->initialize_adapter(base_vha)) {
3408 		ql_log(ql_log_fatal, base_vha, 0x00d6,
3409 		    "Failed to initialize adapter - Adapter flags %x.\n",
3410 		    base_vha->device_flags);
3411 
3412 		if (IS_QLA82XX(ha)) {
3413 			qla82xx_idc_lock(ha);
3414 			qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
3415 				QLA8XXX_DEV_FAILED);
3416 			qla82xx_idc_unlock(ha);
3417 			ql_log(ql_log_fatal, base_vha, 0x00d7,
3418 			    "HW State: FAILED.\n");
3419 		} else if (IS_QLA8044(ha)) {
3420 			qla8044_idc_lock(ha);
3421 			qla8044_wr_direct(base_vha,
3422 				QLA8044_CRB_DEV_STATE_INDEX,
3423 				QLA8XXX_DEV_FAILED);
3424 			qla8044_idc_unlock(ha);
3425 			ql_log(ql_log_fatal, base_vha, 0x0150,
3426 			    "HW State: FAILED.\n");
3427 		}
3428 
3429 		ret = -ENODEV;
3430 		goto probe_failed;
3431 	}
3432 
3433 	if (IS_QLAFX00(ha))
3434 		host->can_queue = QLAFX00_MAX_CANQUEUE;
3435 	else
3436 		host->can_queue = req->num_outstanding_cmds - 10;
3437 
3438 	ql_dbg(ql_dbg_init, base_vha, 0x0032,
3439 	    "can_queue=%d, req=%p, mgmt_svr_loop_id=%d, sg_tablesize=%d.\n",
3440 	    host->can_queue, base_vha->req,
3441 	    base_vha->mgmt_svr_loop_id, host->sg_tablesize);
3442 
3443 	/* Check if FW supports MQ or not for ISP25xx */
3444 	if (IS_QLA25XX(ha) && !(ha->fw_attributes & BIT_6))
3445 		ha->mqenable = 0;
3446 
3447 	if (ha->mqenable) {
3448 		bool startit = false;
3449 
3450 		if (QLA_TGT_MODE_ENABLED())
3451 			startit = false;
3452 
3453 		if (ql2x_ini_mode == QLA2XXX_INI_MODE_ENABLED)
3454 			startit = true;
3455 
3456 		/* Create start of day qpairs for Block MQ */
3457 		for (i = 0; i < ha->max_qpairs; i++)
3458 			qla2xxx_create_qpair(base_vha, 5, 0, startit);
3459 	}
3460 	qla_init_iocb_limit(base_vha);
3461 
3462 	if (ha->flags.running_gold_fw)
3463 		goto skip_dpc;
3464 
3465 	/*
3466 	 * Startup the kernel thread for this host adapter
3467 	 */
3468 	ha->dpc_thread = kthread_create(qla2x00_do_dpc, ha,
3469 	    "%s_dpc", base_vha->host_str);
3470 	if (IS_ERR(ha->dpc_thread)) {
3471 		ql_log(ql_log_fatal, base_vha, 0x00ed,
3472 		    "Failed to start DPC thread.\n");
3473 		ret = PTR_ERR(ha->dpc_thread);
3474 		ha->dpc_thread = NULL;
3475 		goto probe_failed;
3476 	}
3477 	ql_dbg(ql_dbg_init, base_vha, 0x00ee,
3478 	    "DPC thread started successfully.\n");
3479 
3480 	/*
3481 	 * If we're not coming up in initiator mode, we might sit for
3482 	 * a while without waking up the dpc thread, which leads to a
3483 	 * stuck process warning.  So just kick the dpc once here and
3484 	 * let the kthread start (and go back to sleep in qla2x00_do_dpc).
3485 	 */
3486 	qla2xxx_wake_dpc(base_vha);
3487 
3488 	INIT_WORK(&ha->board_disable, qla2x00_disable_board_on_pci_error);
3489 
3490 	if (IS_QLA8031(ha) || IS_MCTP_CAPABLE(ha)) {
3491 		sprintf(wq_name, "qla2xxx_%lu_dpc_lp_wq", base_vha->host_no);
3492 		ha->dpc_lp_wq = create_singlethread_workqueue(wq_name);
3493 		INIT_WORK(&ha->idc_aen, qla83xx_service_idc_aen);
3494 
3495 		sprintf(wq_name, "qla2xxx_%lu_dpc_hp_wq", base_vha->host_no);
3496 		ha->dpc_hp_wq = create_singlethread_workqueue(wq_name);
3497 		INIT_WORK(&ha->nic_core_reset, qla83xx_nic_core_reset_work);
3498 		INIT_WORK(&ha->idc_state_handler,
3499 		    qla83xx_idc_state_handler_work);
3500 		INIT_WORK(&ha->nic_core_unrecoverable,
3501 		    qla83xx_nic_core_unrecoverable_work);
3502 	}
3503 
3504 skip_dpc:
3505 	list_add_tail(&base_vha->list, &ha->vp_list);
3506 	base_vha->host->irq = ha->pdev->irq;
3507 
3508 	/* Initialized the timer */
3509 	qla2x00_start_timer(base_vha, WATCH_INTERVAL);
3510 	ql_dbg(ql_dbg_init, base_vha, 0x00ef,
3511 	    "Started qla2x00_timer with "
3512 	    "interval=%d.\n", WATCH_INTERVAL);
3513 	ql_dbg(ql_dbg_init, base_vha, 0x00f0,
3514 	    "Detected hba at address=%p.\n",
3515 	    ha);
3516 
3517 	if (IS_T10_PI_CAPABLE(ha) && ql2xenabledif) {
3518 		if (ha->fw_attributes & BIT_4) {
3519 			int prot = 0, guard;
3520 
3521 			base_vha->flags.difdix_supported = 1;
3522 			ql_dbg(ql_dbg_init, base_vha, 0x00f1,
3523 			    "Registering for DIF/DIX type 1 and 3 protection.\n");
3524 			if (ql2xenabledif == 1)
3525 				prot = SHOST_DIX_TYPE0_PROTECTION;
3526 			if (ql2xprotmask)
3527 				scsi_host_set_prot(host, ql2xprotmask);
3528 			else
3529 				scsi_host_set_prot(host,
3530 				    prot | SHOST_DIF_TYPE1_PROTECTION
3531 				    | SHOST_DIF_TYPE2_PROTECTION
3532 				    | SHOST_DIF_TYPE3_PROTECTION
3533 				    | SHOST_DIX_TYPE1_PROTECTION
3534 				    | SHOST_DIX_TYPE2_PROTECTION
3535 				    | SHOST_DIX_TYPE3_PROTECTION);
3536 
3537 			guard = SHOST_DIX_GUARD_CRC;
3538 
3539 			if (IS_PI_IPGUARD_CAPABLE(ha) &&
3540 			    (ql2xenabledif > 1 || IS_PI_DIFB_DIX0_CAPABLE(ha)))
3541 				guard |= SHOST_DIX_GUARD_IP;
3542 
3543 			if (ql2xprotguard)
3544 				scsi_host_set_guard(host, ql2xprotguard);
3545 			else
3546 				scsi_host_set_guard(host, guard);
3547 		} else
3548 			base_vha->flags.difdix_supported = 0;
3549 	}
3550 
3551 	ha->isp_ops->enable_intrs(ha);
3552 
3553 	if (IS_QLAFX00(ha)) {
3554 		ret = qlafx00_fx_disc(base_vha,
3555 			&base_vha->hw->mr.fcport, FXDISC_GET_CONFIG_INFO);
3556 		host->sg_tablesize = (ha->mr.extended_io_enabled) ?
3557 		    QLA_SG_ALL : 128;
3558 	}
3559 
3560 	ret = scsi_add_host(host, &pdev->dev);
3561 	if (ret)
3562 		goto probe_failed;
3563 
3564 	base_vha->flags.init_done = 1;
3565 	base_vha->flags.online = 1;
3566 	ha->prev_minidump_failed = 0;
3567 
3568 	ql_dbg(ql_dbg_init, base_vha, 0x00f2,
3569 	    "Init done and hba is online.\n");
3570 
3571 	if (qla_ini_mode_enabled(base_vha) ||
3572 		qla_dual_mode_enabled(base_vha))
3573 		scsi_scan_host(host);
3574 	else
3575 		ql_log(ql_log_info, base_vha, 0x0122,
3576 			"skipping scsi_scan_host() for non-initiator port\n");
3577 
3578 	qla2x00_alloc_sysfs_attr(base_vha);
3579 
3580 	if (IS_QLAFX00(ha)) {
3581 		ret = qlafx00_fx_disc(base_vha,
3582 			&base_vha->hw->mr.fcport, FXDISC_GET_PORT_INFO);
3583 
3584 		/* Register system information */
3585 		ret =  qlafx00_fx_disc(base_vha,
3586 			&base_vha->hw->mr.fcport, FXDISC_REG_HOST_INFO);
3587 	}
3588 
3589 	qla2x00_init_host_attr(base_vha);
3590 
3591 	qla2x00_dfs_setup(base_vha);
3592 
3593 	ql_log(ql_log_info, base_vha, 0x00fb,
3594 	    "QLogic %s - %s.\n", ha->model_number, ha->model_desc);
3595 	ql_log(ql_log_info, base_vha, 0x00fc,
3596 	    "ISP%04X: %s @ %s hdma%c host#=%ld fw=%s.\n",
3597 	    pdev->device, ha->isp_ops->pci_info_str(base_vha, pci_info,
3598 						       sizeof(pci_info)),
3599 	    pci_name(pdev), ha->flags.enable_64bit_addressing ? '+' : '-',
3600 	    base_vha->host_no,
3601 	    ha->isp_ops->fw_version_str(base_vha, fw_str, sizeof(fw_str)));
3602 
3603 	qlt_add_target(ha, base_vha);
3604 
3605 	clear_bit(PFLG_DRIVER_PROBING, &base_vha->pci_flags);
3606 
3607 	if (test_bit(UNLOADING, &base_vha->dpc_flags))
3608 		return -ENODEV;
3609 
3610 	return 0;
3611 
3612 probe_failed:
3613 	qla_enode_stop(base_vha);
3614 	qla_edb_stop(base_vha);
3615 	vfree(base_vha->scan.l);
3616 	if (base_vha->gnl.l) {
3617 		dma_free_coherent(&ha->pdev->dev, base_vha->gnl.size,
3618 				base_vha->gnl.l, base_vha->gnl.ldma);
3619 		base_vha->gnl.l = NULL;
3620 	}
3621 
3622 	if (base_vha->timer_active)
3623 		qla2x00_stop_timer(base_vha);
3624 	base_vha->flags.online = 0;
3625 	if (ha->dpc_thread) {
3626 		struct task_struct *t = ha->dpc_thread;
3627 
3628 		ha->dpc_thread = NULL;
3629 		kthread_stop(t);
3630 	}
3631 
3632 	qla2x00_free_device(base_vha);
3633 	scsi_host_put(base_vha->host);
3634 	/*
3635 	 * Need to NULL out local req/rsp after
3636 	 * qla2x00_free_device => qla2x00_free_queues frees
3637 	 * what these are pointing to. Or else we'll
3638 	 * fall over below in qla2x00_free_req/rsp_que.
3639 	 */
3640 	req = NULL;
3641 	rsp = NULL;
3642 
3643 probe_hw_failed:
3644 	qla2x00_mem_free(ha);
3645 	qla2x00_free_req_que(ha, req);
3646 	qla2x00_free_rsp_que(ha, rsp);
3647 	qla2x00_clear_drv_active(ha);
3648 
3649 iospace_config_failed:
3650 	if (IS_P3P_TYPE(ha)) {
3651 		if (!ha->nx_pcibase)
3652 			iounmap((device_reg_t *)ha->nx_pcibase);
3653 		if (!ql2xdbwr)
3654 			iounmap((device_reg_t *)ha->nxdb_wr_ptr);
3655 	} else {
3656 		if (ha->iobase)
3657 			iounmap(ha->iobase);
3658 		if (ha->cregbase)
3659 			iounmap(ha->cregbase);
3660 	}
3661 	pci_release_selected_regions(ha->pdev, ha->bars);
3662 	kfree(ha);
3663 
3664 disable_device:
3665 	pci_disable_device(pdev);
3666 	return ret;
3667 }
3668 
3669 static void __qla_set_remove_flag(scsi_qla_host_t *base_vha)
3670 {
3671 	scsi_qla_host_t *vp;
3672 	unsigned long flags;
3673 	struct qla_hw_data *ha;
3674 
3675 	if (!base_vha)
3676 		return;
3677 
3678 	ha = base_vha->hw;
3679 
3680 	spin_lock_irqsave(&ha->vport_slock, flags);
3681 	list_for_each_entry(vp, &ha->vp_list, list)
3682 		set_bit(PFLG_DRIVER_REMOVING, &vp->pci_flags);
3683 
3684 	/*
3685 	 * Indicate device removal to prevent future board_disable
3686 	 * and wait until any pending board_disable has completed.
3687 	 */
3688 	set_bit(PFLG_DRIVER_REMOVING, &base_vha->pci_flags);
3689 	spin_unlock_irqrestore(&ha->vport_slock, flags);
3690 }
3691 
3692 static void
3693 qla2x00_shutdown(struct pci_dev *pdev)
3694 {
3695 	scsi_qla_host_t *vha;
3696 	struct qla_hw_data  *ha;
3697 
3698 	vha = pci_get_drvdata(pdev);
3699 	ha = vha->hw;
3700 
3701 	ql_log(ql_log_info, vha, 0xfffa,
3702 		"Adapter shutdown\n");
3703 
3704 	/*
3705 	 * Prevent future board_disable and wait
3706 	 * until any pending board_disable has completed.
3707 	 */
3708 	__qla_set_remove_flag(vha);
3709 	cancel_work_sync(&ha->board_disable);
3710 
3711 	if (!atomic_read(&pdev->enable_cnt))
3712 		return;
3713 
3714 	/* Notify ISPFX00 firmware */
3715 	if (IS_QLAFX00(ha))
3716 		qlafx00_driver_shutdown(vha, 20);
3717 
3718 	/* Turn-off FCE trace */
3719 	if (ha->flags.fce_enabled) {
3720 		qla2x00_disable_fce_trace(vha, NULL, NULL);
3721 		ha->flags.fce_enabled = 0;
3722 	}
3723 
3724 	/* Turn-off EFT trace */
3725 	if (ha->eft)
3726 		qla2x00_disable_eft_trace(vha);
3727 
3728 	if (IS_QLA25XX(ha) ||  IS_QLA2031(ha) || IS_QLA27XX(ha) ||
3729 	    IS_QLA28XX(ha)) {
3730 		if (ha->flags.fw_started)
3731 			qla2x00_abort_isp_cleanup(vha);
3732 	} else {
3733 		/* Stop currently executing firmware. */
3734 		qla2x00_try_to_stop_firmware(vha);
3735 	}
3736 
3737 	/* Disable timer */
3738 	if (vha->timer_active)
3739 		qla2x00_stop_timer(vha);
3740 
3741 	/* Turn adapter off line */
3742 	vha->flags.online = 0;
3743 
3744 	/* turn-off interrupts on the card */
3745 	if (ha->interrupts_on) {
3746 		vha->flags.init_done = 0;
3747 		ha->isp_ops->disable_intrs(ha);
3748 	}
3749 
3750 	qla2x00_free_irqs(vha);
3751 
3752 	qla2x00_free_fw_dump(ha);
3753 
3754 	pci_disable_device(pdev);
3755 	ql_log(ql_log_info, vha, 0xfffe,
3756 		"Adapter shutdown successfully.\n");
3757 }
3758 
3759 /* Deletes all the virtual ports for a given ha */
3760 static void
3761 qla2x00_delete_all_vps(struct qla_hw_data *ha, scsi_qla_host_t *base_vha)
3762 {
3763 	scsi_qla_host_t *vha;
3764 	unsigned long flags;
3765 
3766 	mutex_lock(&ha->vport_lock);
3767 	while (ha->cur_vport_count) {
3768 		spin_lock_irqsave(&ha->vport_slock, flags);
3769 
3770 		BUG_ON(base_vha->list.next == &ha->vp_list);
3771 		/* This assumes first entry in ha->vp_list is always base vha */
3772 		vha = list_first_entry(&base_vha->list, scsi_qla_host_t, list);
3773 		scsi_host_get(vha->host);
3774 
3775 		spin_unlock_irqrestore(&ha->vport_slock, flags);
3776 		mutex_unlock(&ha->vport_lock);
3777 
3778 		qla_nvme_delete(vha);
3779 
3780 		fc_vport_terminate(vha->fc_vport);
3781 		scsi_host_put(vha->host);
3782 
3783 		mutex_lock(&ha->vport_lock);
3784 	}
3785 	mutex_unlock(&ha->vport_lock);
3786 }
3787 
3788 /* Stops all deferred work threads */
3789 static void
3790 qla2x00_destroy_deferred_work(struct qla_hw_data *ha)
3791 {
3792 	/* Cancel all work and destroy DPC workqueues */
3793 	if (ha->dpc_lp_wq) {
3794 		cancel_work_sync(&ha->idc_aen);
3795 		destroy_workqueue(ha->dpc_lp_wq);
3796 		ha->dpc_lp_wq = NULL;
3797 	}
3798 
3799 	if (ha->dpc_hp_wq) {
3800 		cancel_work_sync(&ha->nic_core_reset);
3801 		cancel_work_sync(&ha->idc_state_handler);
3802 		cancel_work_sync(&ha->nic_core_unrecoverable);
3803 		destroy_workqueue(ha->dpc_hp_wq);
3804 		ha->dpc_hp_wq = NULL;
3805 	}
3806 
3807 	/* Kill the kernel thread for this host */
3808 	if (ha->dpc_thread) {
3809 		struct task_struct *t = ha->dpc_thread;
3810 
3811 		/*
3812 		 * qla2xxx_wake_dpc checks for ->dpc_thread
3813 		 * so we need to zero it out.
3814 		 */
3815 		ha->dpc_thread = NULL;
3816 		kthread_stop(t);
3817 	}
3818 }
3819 
3820 static void
3821 qla2x00_unmap_iobases(struct qla_hw_data *ha)
3822 {
3823 	if (IS_QLA82XX(ha)) {
3824 
3825 		iounmap((device_reg_t *)ha->nx_pcibase);
3826 		if (!ql2xdbwr)
3827 			iounmap((device_reg_t *)ha->nxdb_wr_ptr);
3828 	} else {
3829 		if (ha->iobase)
3830 			iounmap(ha->iobase);
3831 
3832 		if (ha->cregbase)
3833 			iounmap(ha->cregbase);
3834 
3835 		if (ha->mqiobase)
3836 			iounmap(ha->mqiobase);
3837 
3838 		if (ha->msixbase)
3839 			iounmap(ha->msixbase);
3840 	}
3841 }
3842 
3843 static void
3844 qla2x00_clear_drv_active(struct qla_hw_data *ha)
3845 {
3846 	if (IS_QLA8044(ha)) {
3847 		qla8044_idc_lock(ha);
3848 		qla8044_clear_drv_active(ha);
3849 		qla8044_idc_unlock(ha);
3850 	} else if (IS_QLA82XX(ha)) {
3851 		qla82xx_idc_lock(ha);
3852 		qla82xx_clear_drv_active(ha);
3853 		qla82xx_idc_unlock(ha);
3854 	}
3855 }
3856 
3857 static void
3858 qla2x00_remove_one(struct pci_dev *pdev)
3859 {
3860 	scsi_qla_host_t *base_vha;
3861 	struct qla_hw_data  *ha;
3862 
3863 	base_vha = pci_get_drvdata(pdev);
3864 	ha = base_vha->hw;
3865 	ql_log(ql_log_info, base_vha, 0xb079,
3866 	    "Removing driver\n");
3867 	__qla_set_remove_flag(base_vha);
3868 	cancel_work_sync(&ha->board_disable);
3869 
3870 	/*
3871 	 * If the PCI device is disabled then there was a PCI-disconnect and
3872 	 * qla2x00_disable_board_on_pci_error has taken care of most of the
3873 	 * resources.
3874 	 */
3875 	if (!atomic_read(&pdev->enable_cnt)) {
3876 		dma_free_coherent(&ha->pdev->dev, base_vha->gnl.size,
3877 		    base_vha->gnl.l, base_vha->gnl.ldma);
3878 		base_vha->gnl.l = NULL;
3879 		scsi_host_put(base_vha->host);
3880 		kfree(ha);
3881 		pci_set_drvdata(pdev, NULL);
3882 		return;
3883 	}
3884 	qla2x00_wait_for_hba_ready(base_vha);
3885 
3886 	/*
3887 	 * if UNLOADING flag is already set, then continue unload,
3888 	 * where it was set first.
3889 	 */
3890 	if (test_and_set_bit(UNLOADING, &base_vha->dpc_flags))
3891 		return;
3892 
3893 	if (IS_QLA25XX(ha) || IS_QLA2031(ha) || IS_QLA27XX(ha) ||
3894 	    IS_QLA28XX(ha)) {
3895 		if (ha->flags.fw_started)
3896 			qla2x00_abort_isp_cleanup(base_vha);
3897 	} else if (!IS_QLAFX00(ha)) {
3898 		if (IS_QLA8031(ha)) {
3899 			ql_dbg(ql_dbg_p3p, base_vha, 0xb07e,
3900 			    "Clearing fcoe driver presence.\n");
3901 			if (qla83xx_clear_drv_presence(base_vha) != QLA_SUCCESS)
3902 				ql_dbg(ql_dbg_p3p, base_vha, 0xb079,
3903 				    "Error while clearing DRV-Presence.\n");
3904 		}
3905 
3906 		qla2x00_try_to_stop_firmware(base_vha);
3907 	}
3908 
3909 	qla2x00_wait_for_sess_deletion(base_vha);
3910 
3911 	qla_nvme_delete(base_vha);
3912 
3913 	dma_free_coherent(&ha->pdev->dev,
3914 		base_vha->gnl.size, base_vha->gnl.l, base_vha->gnl.ldma);
3915 
3916 	base_vha->gnl.l = NULL;
3917 	qla_enode_stop(base_vha);
3918 	qla_edb_stop(base_vha);
3919 
3920 	vfree(base_vha->scan.l);
3921 
3922 	if (IS_QLAFX00(ha))
3923 		qlafx00_driver_shutdown(base_vha, 20);
3924 
3925 	qla2x00_delete_all_vps(ha, base_vha);
3926 
3927 	qla2x00_dfs_remove(base_vha);
3928 
3929 	qla84xx_put_chip(base_vha);
3930 
3931 	/* Disable timer */
3932 	if (base_vha->timer_active)
3933 		qla2x00_stop_timer(base_vha);
3934 
3935 	base_vha->flags.online = 0;
3936 
3937 	/* free DMA memory */
3938 	if (ha->exlogin_buf)
3939 		qla2x00_free_exlogin_buffer(ha);
3940 
3941 	/* free DMA memory */
3942 	if (ha->exchoffld_buf)
3943 		qla2x00_free_exchoffld_buffer(ha);
3944 
3945 	qla2x00_destroy_deferred_work(ha);
3946 
3947 	qlt_remove_target(ha, base_vha);
3948 
3949 	qla2x00_free_sysfs_attr(base_vha, true);
3950 
3951 	fc_remove_host(base_vha->host);
3952 
3953 	scsi_remove_host(base_vha->host);
3954 
3955 	qla2x00_free_device(base_vha);
3956 
3957 	qla2x00_clear_drv_active(ha);
3958 
3959 	scsi_host_put(base_vha->host);
3960 
3961 	qla2x00_unmap_iobases(ha);
3962 
3963 	pci_release_selected_regions(ha->pdev, ha->bars);
3964 	kfree(ha);
3965 
3966 	pci_disable_device(pdev);
3967 }
3968 
3969 static inline void
3970 qla24xx_free_purex_list(struct purex_list *list)
3971 {
3972 	struct purex_item *item, *next;
3973 	ulong flags;
3974 
3975 	spin_lock_irqsave(&list->lock, flags);
3976 	list_for_each_entry_safe(item, next, &list->head, list) {
3977 		list_del(&item->list);
3978 		if (item == &item->vha->default_item)
3979 			continue;
3980 		kfree(item);
3981 	}
3982 	spin_unlock_irqrestore(&list->lock, flags);
3983 }
3984 
3985 static void
3986 qla2x00_free_device(scsi_qla_host_t *vha)
3987 {
3988 	struct qla_hw_data *ha = vha->hw;
3989 
3990 	qla2x00_abort_all_cmds(vha, DID_NO_CONNECT << 16);
3991 
3992 	/* Disable timer */
3993 	if (vha->timer_active)
3994 		qla2x00_stop_timer(vha);
3995 
3996 	qla25xx_delete_queues(vha);
3997 	vha->flags.online = 0;
3998 
3999 	/* turn-off interrupts on the card */
4000 	if (ha->interrupts_on) {
4001 		vha->flags.init_done = 0;
4002 		ha->isp_ops->disable_intrs(ha);
4003 	}
4004 
4005 	qla2x00_free_fcports(vha);
4006 
4007 	qla2x00_free_irqs(vha);
4008 
4009 	/* Flush the work queue and remove it */
4010 	if (ha->wq) {
4011 		destroy_workqueue(ha->wq);
4012 		ha->wq = NULL;
4013 	}
4014 
4015 
4016 	qla24xx_free_purex_list(&vha->purex_list);
4017 
4018 	qla2x00_mem_free(ha);
4019 
4020 	qla82xx_md_free(vha);
4021 
4022 	qla_edif_sadb_release_free_pool(ha);
4023 	qla_edif_sadb_release(ha);
4024 
4025 	qla2x00_free_queues(ha);
4026 }
4027 
4028 void qla2x00_free_fcports(struct scsi_qla_host *vha)
4029 {
4030 	fc_port_t *fcport, *tfcport;
4031 
4032 	list_for_each_entry_safe(fcport, tfcport, &vha->vp_fcports, list)
4033 		qla2x00_free_fcport(fcport);
4034 }
4035 
4036 static inline void
4037 qla2x00_schedule_rport_del(struct scsi_qla_host *vha, fc_port_t *fcport)
4038 {
4039 	int now;
4040 
4041 	if (!fcport->rport)
4042 		return;
4043 
4044 	if (fcport->rport) {
4045 		ql_dbg(ql_dbg_disc, fcport->vha, 0x2109,
4046 		    "%s %8phN. rport %p roles %x\n",
4047 		    __func__, fcport->port_name, fcport->rport,
4048 		    fcport->rport->roles);
4049 		fc_remote_port_delete(fcport->rport);
4050 	}
4051 	qlt_do_generation_tick(vha, &now);
4052 }
4053 
4054 /*
4055  * qla2x00_mark_device_lost Updates fcport state when device goes offline.
4056  *
4057  * Input: ha = adapter block pointer.  fcport = port structure pointer.
4058  *
4059  * Return: None.
4060  *
4061  * Context:
4062  */
4063 void qla2x00_mark_device_lost(scsi_qla_host_t *vha, fc_port_t *fcport,
4064     int do_login)
4065 {
4066 	if (IS_QLAFX00(vha->hw)) {
4067 		qla2x00_set_fcport_state(fcport, FCS_DEVICE_LOST);
4068 		qla2x00_schedule_rport_del(vha, fcport);
4069 		return;
4070 	}
4071 
4072 	if (atomic_read(&fcport->state) == FCS_ONLINE &&
4073 	    vha->vp_idx == fcport->vha->vp_idx) {
4074 		qla2x00_set_fcport_state(fcport, FCS_DEVICE_LOST);
4075 		qla2x00_schedule_rport_del(vha, fcport);
4076 	}
4077 
4078 	/*
4079 	 * We may need to retry the login, so don't change the state of the
4080 	 * port but do the retries.
4081 	 */
4082 	if (atomic_read(&fcport->state) != FCS_DEVICE_DEAD)
4083 		qla2x00_set_fcport_state(fcport, FCS_DEVICE_LOST);
4084 
4085 	if (!do_login)
4086 		return;
4087 
4088 	set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
4089 }
4090 
4091 void
4092 qla2x00_mark_all_devices_lost(scsi_qla_host_t *vha)
4093 {
4094 	fc_port_t *fcport;
4095 
4096 	ql_dbg(ql_dbg_disc, vha, 0x20f1,
4097 	    "Mark all dev lost\n");
4098 
4099 	list_for_each_entry(fcport, &vha->vp_fcports, list) {
4100 		if (ql2xfc2target &&
4101 		    fcport->loop_id != FC_NO_LOOP_ID &&
4102 		    (fcport->flags & FCF_FCP2_DEVICE) &&
4103 		    fcport->port_type == FCT_TARGET &&
4104 		    !qla2x00_reset_active(vha)) {
4105 			ql_dbg(ql_dbg_disc, vha, 0x211a,
4106 			       "Delaying session delete for FCP2 flags 0x%x port_type = 0x%x port_id=%06x %phC",
4107 			       fcport->flags, fcport->port_type,
4108 			       fcport->d_id.b24, fcport->port_name);
4109 			continue;
4110 		}
4111 		fcport->scan_state = 0;
4112 		qlt_schedule_sess_for_deletion(fcport);
4113 	}
4114 }
4115 
4116 static void qla2x00_set_reserved_loop_ids(struct qla_hw_data *ha)
4117 {
4118 	int i;
4119 
4120 	if (IS_FWI2_CAPABLE(ha))
4121 		return;
4122 
4123 	for (i = 0; i < SNS_FIRST_LOOP_ID; i++)
4124 		set_bit(i, ha->loop_id_map);
4125 	set_bit(MANAGEMENT_SERVER, ha->loop_id_map);
4126 	set_bit(BROADCAST, ha->loop_id_map);
4127 }
4128 
4129 /*
4130 * qla2x00_mem_alloc
4131 *      Allocates adapter memory.
4132 *
4133 * Returns:
4134 *      0  = success.
4135 *      !0  = failure.
4136 */
4137 static int
4138 qla2x00_mem_alloc(struct qla_hw_data *ha, uint16_t req_len, uint16_t rsp_len,
4139 	struct req_que **req, struct rsp_que **rsp)
4140 {
4141 	char	name[16];
4142 	int rc;
4143 
4144 	if (QLA_TGT_MODE_ENABLED() || EDIF_CAP(ha)) {
4145 		ha->vp_map = kcalloc(MAX_MULTI_ID_FABRIC, sizeof(struct qla_vp_map), GFP_KERNEL);
4146 		if (!ha->vp_map)
4147 			goto fail;
4148 	}
4149 
4150 	ha->init_cb = dma_alloc_coherent(&ha->pdev->dev, ha->init_cb_size,
4151 		&ha->init_cb_dma, GFP_KERNEL);
4152 	if (!ha->init_cb)
4153 		goto fail_free_vp_map;
4154 
4155 	rc = btree_init32(&ha->host_map);
4156 	if (rc)
4157 		goto fail_free_init_cb;
4158 
4159 	if (qlt_mem_alloc(ha) < 0)
4160 		goto fail_free_btree;
4161 
4162 	ha->gid_list = dma_alloc_coherent(&ha->pdev->dev,
4163 		qla2x00_gid_list_size(ha), &ha->gid_list_dma, GFP_KERNEL);
4164 	if (!ha->gid_list)
4165 		goto fail_free_tgt_mem;
4166 
4167 	ha->srb_mempool = mempool_create_slab_pool(SRB_MIN_REQ, srb_cachep);
4168 	if (!ha->srb_mempool)
4169 		goto fail_free_gid_list;
4170 
4171 	if (IS_P3P_TYPE(ha) || IS_QLA27XX(ha) || (ql2xsecenable && IS_QLA28XX(ha))) {
4172 		/* Allocate cache for CT6 Ctx. */
4173 		if (!ctx_cachep) {
4174 			ctx_cachep = kmem_cache_create("qla2xxx_ctx",
4175 				sizeof(struct ct6_dsd), 0,
4176 				SLAB_HWCACHE_ALIGN, NULL);
4177 			if (!ctx_cachep)
4178 				goto fail_free_srb_mempool;
4179 		}
4180 		ha->ctx_mempool = mempool_create_slab_pool(SRB_MIN_REQ,
4181 			ctx_cachep);
4182 		if (!ha->ctx_mempool)
4183 			goto fail_free_srb_mempool;
4184 		ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0021,
4185 		    "ctx_cachep=%p ctx_mempool=%p.\n",
4186 		    ctx_cachep, ha->ctx_mempool);
4187 	}
4188 
4189 	/* Get memory for cached NVRAM */
4190 	ha->nvram = kzalloc(MAX_NVRAM_SIZE, GFP_KERNEL);
4191 	if (!ha->nvram)
4192 		goto fail_free_ctx_mempool;
4193 
4194 	snprintf(name, sizeof(name), "%s_%d", QLA2XXX_DRIVER_NAME,
4195 		ha->pdev->device);
4196 	ha->s_dma_pool = dma_pool_create(name, &ha->pdev->dev,
4197 		DMA_POOL_SIZE, 8, 0);
4198 	if (!ha->s_dma_pool)
4199 		goto fail_free_nvram;
4200 
4201 	ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0022,
4202 	    "init_cb=%p gid_list=%p, srb_mempool=%p s_dma_pool=%p.\n",
4203 	    ha->init_cb, ha->gid_list, ha->srb_mempool, ha->s_dma_pool);
4204 
4205 	if (IS_P3P_TYPE(ha) || ql2xenabledif || (IS_QLA28XX(ha) && ql2xsecenable)) {
4206 		ha->dl_dma_pool = dma_pool_create(name, &ha->pdev->dev,
4207 			DSD_LIST_DMA_POOL_SIZE, 8, 0);
4208 		if (!ha->dl_dma_pool) {
4209 			ql_log_pci(ql_log_fatal, ha->pdev, 0x0023,
4210 			    "Failed to allocate memory for dl_dma_pool.\n");
4211 			goto fail_s_dma_pool;
4212 		}
4213 
4214 		ha->fcp_cmnd_dma_pool = dma_pool_create(name, &ha->pdev->dev,
4215 			FCP_CMND_DMA_POOL_SIZE, 8, 0);
4216 		if (!ha->fcp_cmnd_dma_pool) {
4217 			ql_log_pci(ql_log_fatal, ha->pdev, 0x0024,
4218 			    "Failed to allocate memory for fcp_cmnd_dma_pool.\n");
4219 			goto fail_dl_dma_pool;
4220 		}
4221 
4222 		if (ql2xenabledif) {
4223 			u64 bufsize = DIF_BUNDLING_DMA_POOL_SIZE;
4224 			struct dsd_dma *dsd, *nxt;
4225 			uint i;
4226 			/* Creata a DMA pool of buffers for DIF bundling */
4227 			ha->dif_bundl_pool = dma_pool_create(name,
4228 			    &ha->pdev->dev, DIF_BUNDLING_DMA_POOL_SIZE, 8, 0);
4229 			if (!ha->dif_bundl_pool) {
4230 				ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0024,
4231 				    "%s: failed create dif_bundl_pool\n",
4232 				    __func__);
4233 				goto fail_dif_bundl_dma_pool;
4234 			}
4235 
4236 			INIT_LIST_HEAD(&ha->pool.good.head);
4237 			INIT_LIST_HEAD(&ha->pool.unusable.head);
4238 			ha->pool.good.count = 0;
4239 			ha->pool.unusable.count = 0;
4240 			for (i = 0; i < 128; i++) {
4241 				dsd = kzalloc(sizeof(*dsd), GFP_ATOMIC);
4242 				if (!dsd) {
4243 					ql_dbg_pci(ql_dbg_init, ha->pdev,
4244 					    0xe0ee, "%s: failed alloc dsd\n",
4245 					    __func__);
4246 					return -ENOMEM;
4247 				}
4248 				ha->dif_bundle_kallocs++;
4249 
4250 				dsd->dsd_addr = dma_pool_alloc(
4251 				    ha->dif_bundl_pool, GFP_ATOMIC,
4252 				    &dsd->dsd_list_dma);
4253 				if (!dsd->dsd_addr) {
4254 					ql_dbg_pci(ql_dbg_init, ha->pdev,
4255 					    0xe0ee,
4256 					    "%s: failed alloc ->dsd_addr\n",
4257 					    __func__);
4258 					kfree(dsd);
4259 					ha->dif_bundle_kallocs--;
4260 					continue;
4261 				}
4262 				ha->dif_bundle_dma_allocs++;
4263 
4264 				/*
4265 				 * if DMA buffer crosses 4G boundary,
4266 				 * put it on bad list
4267 				 */
4268 				if (MSD(dsd->dsd_list_dma) ^
4269 				    MSD(dsd->dsd_list_dma + bufsize)) {
4270 					list_add_tail(&dsd->list,
4271 					    &ha->pool.unusable.head);
4272 					ha->pool.unusable.count++;
4273 				} else {
4274 					list_add_tail(&dsd->list,
4275 					    &ha->pool.good.head);
4276 					ha->pool.good.count++;
4277 				}
4278 			}
4279 
4280 			/* return the good ones back to the pool */
4281 			list_for_each_entry_safe(dsd, nxt,
4282 			    &ha->pool.good.head, list) {
4283 				list_del(&dsd->list);
4284 				dma_pool_free(ha->dif_bundl_pool,
4285 				    dsd->dsd_addr, dsd->dsd_list_dma);
4286 				ha->dif_bundle_dma_allocs--;
4287 				kfree(dsd);
4288 				ha->dif_bundle_kallocs--;
4289 			}
4290 
4291 			ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0024,
4292 			    "%s: dif dma pool (good=%u unusable=%u)\n",
4293 			    __func__, ha->pool.good.count,
4294 			    ha->pool.unusable.count);
4295 		}
4296 
4297 		ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0025,
4298 		    "dl_dma_pool=%p fcp_cmnd_dma_pool=%p dif_bundl_pool=%p.\n",
4299 		    ha->dl_dma_pool, ha->fcp_cmnd_dma_pool,
4300 		    ha->dif_bundl_pool);
4301 	}
4302 
4303 	/* Allocate memory for SNS commands */
4304 	if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
4305 	/* Get consistent memory allocated for SNS commands */
4306 		ha->sns_cmd = dma_alloc_coherent(&ha->pdev->dev,
4307 		sizeof(struct sns_cmd_pkt), &ha->sns_cmd_dma, GFP_KERNEL);
4308 		if (!ha->sns_cmd)
4309 			goto fail_dma_pool;
4310 		ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0026,
4311 		    "sns_cmd: %p.\n", ha->sns_cmd);
4312 	} else {
4313 	/* Get consistent memory allocated for MS IOCB */
4314 		ha->ms_iocb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
4315 			&ha->ms_iocb_dma);
4316 		if (!ha->ms_iocb)
4317 			goto fail_dma_pool;
4318 	/* Get consistent memory allocated for CT SNS commands */
4319 		ha->ct_sns = dma_alloc_coherent(&ha->pdev->dev,
4320 			sizeof(struct ct_sns_pkt), &ha->ct_sns_dma, GFP_KERNEL);
4321 		if (!ha->ct_sns)
4322 			goto fail_free_ms_iocb;
4323 		ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0027,
4324 		    "ms_iocb=%p ct_sns=%p.\n",
4325 		    ha->ms_iocb, ha->ct_sns);
4326 	}
4327 
4328 	/* Allocate memory for request ring */
4329 	*req = kzalloc(sizeof(struct req_que), GFP_KERNEL);
4330 	if (!*req) {
4331 		ql_log_pci(ql_log_fatal, ha->pdev, 0x0028,
4332 		    "Failed to allocate memory for req.\n");
4333 		goto fail_req;
4334 	}
4335 	(*req)->length = req_len;
4336 	(*req)->ring = dma_alloc_coherent(&ha->pdev->dev,
4337 		((*req)->length + 1) * sizeof(request_t),
4338 		&(*req)->dma, GFP_KERNEL);
4339 	if (!(*req)->ring) {
4340 		ql_log_pci(ql_log_fatal, ha->pdev, 0x0029,
4341 		    "Failed to allocate memory for req_ring.\n");
4342 		goto fail_req_ring;
4343 	}
4344 	/* Allocate memory for response ring */
4345 	*rsp = kzalloc(sizeof(struct rsp_que), GFP_KERNEL);
4346 	if (!*rsp) {
4347 		ql_log_pci(ql_log_fatal, ha->pdev, 0x002a,
4348 		    "Failed to allocate memory for rsp.\n");
4349 		goto fail_rsp;
4350 	}
4351 	(*rsp)->hw = ha;
4352 	(*rsp)->length = rsp_len;
4353 	(*rsp)->ring = dma_alloc_coherent(&ha->pdev->dev,
4354 		((*rsp)->length + 1) * sizeof(response_t),
4355 		&(*rsp)->dma, GFP_KERNEL);
4356 	if (!(*rsp)->ring) {
4357 		ql_log_pci(ql_log_fatal, ha->pdev, 0x002b,
4358 		    "Failed to allocate memory for rsp_ring.\n");
4359 		goto fail_rsp_ring;
4360 	}
4361 	(*req)->rsp = *rsp;
4362 	(*rsp)->req = *req;
4363 	ql_dbg_pci(ql_dbg_init, ha->pdev, 0x002c,
4364 	    "req=%p req->length=%d req->ring=%p rsp=%p "
4365 	    "rsp->length=%d rsp->ring=%p.\n",
4366 	    *req, (*req)->length, (*req)->ring, *rsp, (*rsp)->length,
4367 	    (*rsp)->ring);
4368 	/* Allocate memory for NVRAM data for vports */
4369 	if (ha->nvram_npiv_size) {
4370 		ha->npiv_info = kcalloc(ha->nvram_npiv_size,
4371 					sizeof(struct qla_npiv_entry),
4372 					GFP_KERNEL);
4373 		if (!ha->npiv_info) {
4374 			ql_log_pci(ql_log_fatal, ha->pdev, 0x002d,
4375 			    "Failed to allocate memory for npiv_info.\n");
4376 			goto fail_npiv_info;
4377 		}
4378 	} else
4379 		ha->npiv_info = NULL;
4380 
4381 	/* Get consistent memory allocated for EX-INIT-CB. */
4382 	if (IS_CNA_CAPABLE(ha) || IS_QLA2031(ha) || IS_QLA27XX(ha) ||
4383 	    IS_QLA28XX(ha)) {
4384 		ha->ex_init_cb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
4385 		    &ha->ex_init_cb_dma);
4386 		if (!ha->ex_init_cb)
4387 			goto fail_ex_init_cb;
4388 		ql_dbg_pci(ql_dbg_init, ha->pdev, 0x002e,
4389 		    "ex_init_cb=%p.\n", ha->ex_init_cb);
4390 	}
4391 
4392 	/* Get consistent memory allocated for Special Features-CB. */
4393 	if (IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
4394 		ha->sf_init_cb = dma_pool_zalloc(ha->s_dma_pool, GFP_KERNEL,
4395 						&ha->sf_init_cb_dma);
4396 		if (!ha->sf_init_cb)
4397 			goto fail_sf_init_cb;
4398 		ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0199,
4399 			   "sf_init_cb=%p.\n", ha->sf_init_cb);
4400 	}
4401 
4402 	INIT_LIST_HEAD(&ha->gbl_dsd_list);
4403 
4404 	/* Get consistent memory allocated for Async Port-Database. */
4405 	if (!IS_FWI2_CAPABLE(ha)) {
4406 		ha->async_pd = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
4407 			&ha->async_pd_dma);
4408 		if (!ha->async_pd)
4409 			goto fail_async_pd;
4410 		ql_dbg_pci(ql_dbg_init, ha->pdev, 0x002f,
4411 		    "async_pd=%p.\n", ha->async_pd);
4412 	}
4413 
4414 	INIT_LIST_HEAD(&ha->vp_list);
4415 
4416 	/* Allocate memory for our loop_id bitmap */
4417 	ha->loop_id_map = kcalloc(BITS_TO_LONGS(LOOPID_MAP_SIZE),
4418 				  sizeof(long),
4419 				  GFP_KERNEL);
4420 	if (!ha->loop_id_map)
4421 		goto fail_loop_id_map;
4422 	else {
4423 		qla2x00_set_reserved_loop_ids(ha);
4424 		ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0123,
4425 		    "loop_id_map=%p.\n", ha->loop_id_map);
4426 	}
4427 
4428 	ha->sfp_data = dma_alloc_coherent(&ha->pdev->dev,
4429 	    SFP_DEV_SIZE, &ha->sfp_data_dma, GFP_KERNEL);
4430 	if (!ha->sfp_data) {
4431 		ql_dbg_pci(ql_dbg_init, ha->pdev, 0x011b,
4432 		    "Unable to allocate memory for SFP read-data.\n");
4433 		goto fail_sfp_data;
4434 	}
4435 
4436 	ha->flt = dma_alloc_coherent(&ha->pdev->dev,
4437 	    sizeof(struct qla_flt_header) + FLT_REGIONS_SIZE, &ha->flt_dma,
4438 	    GFP_KERNEL);
4439 	if (!ha->flt) {
4440 		ql_dbg_pci(ql_dbg_init, ha->pdev, 0x011b,
4441 		    "Unable to allocate memory for FLT.\n");
4442 		goto fail_flt_buffer;
4443 	}
4444 
4445 	/* allocate the purex dma pool */
4446 	ha->purex_dma_pool = dma_pool_create(name, &ha->pdev->dev,
4447 	    ELS_MAX_PAYLOAD, 8, 0);
4448 
4449 	if (!ha->purex_dma_pool) {
4450 		ql_dbg_pci(ql_dbg_init, ha->pdev, 0x011b,
4451 		    "Unable to allocate purex_dma_pool.\n");
4452 		goto fail_flt;
4453 	}
4454 
4455 	ha->elsrej.size = sizeof(struct fc_els_ls_rjt) + 16;
4456 	ha->elsrej.c = dma_alloc_coherent(&ha->pdev->dev,
4457 	    ha->elsrej.size, &ha->elsrej.cdma, GFP_KERNEL);
4458 
4459 	if (!ha->elsrej.c) {
4460 		ql_dbg_pci(ql_dbg_init, ha->pdev, 0xffff,
4461 		    "Alloc failed for els reject cmd.\n");
4462 		goto fail_elsrej;
4463 	}
4464 	ha->elsrej.c->er_cmd = ELS_LS_RJT;
4465 	ha->elsrej.c->er_reason = ELS_RJT_LOGIC;
4466 	ha->elsrej.c->er_explan = ELS_EXPL_UNAB_DATA;
4467 	return 0;
4468 
4469 fail_elsrej:
4470 	dma_pool_destroy(ha->purex_dma_pool);
4471 fail_flt:
4472 	dma_free_coherent(&ha->pdev->dev, SFP_DEV_SIZE,
4473 	    ha->flt, ha->flt_dma);
4474 
4475 fail_flt_buffer:
4476 	dma_free_coherent(&ha->pdev->dev, SFP_DEV_SIZE,
4477 	    ha->sfp_data, ha->sfp_data_dma);
4478 fail_sfp_data:
4479 	kfree(ha->loop_id_map);
4480 fail_loop_id_map:
4481 	dma_pool_free(ha->s_dma_pool, ha->async_pd, ha->async_pd_dma);
4482 fail_async_pd:
4483 	dma_pool_free(ha->s_dma_pool, ha->sf_init_cb, ha->sf_init_cb_dma);
4484 fail_sf_init_cb:
4485 	dma_pool_free(ha->s_dma_pool, ha->ex_init_cb, ha->ex_init_cb_dma);
4486 fail_ex_init_cb:
4487 	kfree(ha->npiv_info);
4488 fail_npiv_info:
4489 	dma_free_coherent(&ha->pdev->dev, ((*rsp)->length + 1) *
4490 		sizeof(response_t), (*rsp)->ring, (*rsp)->dma);
4491 	(*rsp)->ring = NULL;
4492 	(*rsp)->dma = 0;
4493 fail_rsp_ring:
4494 	kfree(*rsp);
4495 	*rsp = NULL;
4496 fail_rsp:
4497 	dma_free_coherent(&ha->pdev->dev, ((*req)->length + 1) *
4498 		sizeof(request_t), (*req)->ring, (*req)->dma);
4499 	(*req)->ring = NULL;
4500 	(*req)->dma = 0;
4501 fail_req_ring:
4502 	kfree(*req);
4503 	*req = NULL;
4504 fail_req:
4505 	dma_free_coherent(&ha->pdev->dev, sizeof(struct ct_sns_pkt),
4506 		ha->ct_sns, ha->ct_sns_dma);
4507 	ha->ct_sns = NULL;
4508 	ha->ct_sns_dma = 0;
4509 fail_free_ms_iocb:
4510 	dma_pool_free(ha->s_dma_pool, ha->ms_iocb, ha->ms_iocb_dma);
4511 	ha->ms_iocb = NULL;
4512 	ha->ms_iocb_dma = 0;
4513 
4514 	if (ha->sns_cmd)
4515 		dma_free_coherent(&ha->pdev->dev, sizeof(struct sns_cmd_pkt),
4516 		    ha->sns_cmd, ha->sns_cmd_dma);
4517 fail_dma_pool:
4518 	if (ql2xenabledif) {
4519 		struct dsd_dma *dsd, *nxt;
4520 
4521 		list_for_each_entry_safe(dsd, nxt, &ha->pool.unusable.head,
4522 		    list) {
4523 			list_del(&dsd->list);
4524 			dma_pool_free(ha->dif_bundl_pool, dsd->dsd_addr,
4525 			    dsd->dsd_list_dma);
4526 			ha->dif_bundle_dma_allocs--;
4527 			kfree(dsd);
4528 			ha->dif_bundle_kallocs--;
4529 			ha->pool.unusable.count--;
4530 		}
4531 		dma_pool_destroy(ha->dif_bundl_pool);
4532 		ha->dif_bundl_pool = NULL;
4533 	}
4534 
4535 fail_dif_bundl_dma_pool:
4536 	if (IS_QLA82XX(ha) || ql2xenabledif) {
4537 		dma_pool_destroy(ha->fcp_cmnd_dma_pool);
4538 		ha->fcp_cmnd_dma_pool = NULL;
4539 	}
4540 fail_dl_dma_pool:
4541 	if (IS_QLA82XX(ha) || ql2xenabledif) {
4542 		dma_pool_destroy(ha->dl_dma_pool);
4543 		ha->dl_dma_pool = NULL;
4544 	}
4545 fail_s_dma_pool:
4546 	dma_pool_destroy(ha->s_dma_pool);
4547 	ha->s_dma_pool = NULL;
4548 fail_free_nvram:
4549 	kfree(ha->nvram);
4550 	ha->nvram = NULL;
4551 fail_free_ctx_mempool:
4552 	mempool_destroy(ha->ctx_mempool);
4553 	ha->ctx_mempool = NULL;
4554 fail_free_srb_mempool:
4555 	mempool_destroy(ha->srb_mempool);
4556 	ha->srb_mempool = NULL;
4557 fail_free_gid_list:
4558 	dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
4559 	ha->gid_list,
4560 	ha->gid_list_dma);
4561 	ha->gid_list = NULL;
4562 	ha->gid_list_dma = 0;
4563 fail_free_tgt_mem:
4564 	qlt_mem_free(ha);
4565 fail_free_btree:
4566 	btree_destroy32(&ha->host_map);
4567 fail_free_init_cb:
4568 	dma_free_coherent(&ha->pdev->dev, ha->init_cb_size, ha->init_cb,
4569 	ha->init_cb_dma);
4570 	ha->init_cb = NULL;
4571 	ha->init_cb_dma = 0;
4572 fail_free_vp_map:
4573 	kfree(ha->vp_map);
4574 fail:
4575 	ql_log(ql_log_fatal, NULL, 0x0030,
4576 	    "Memory allocation failure.\n");
4577 	return -ENOMEM;
4578 }
4579 
4580 int
4581 qla2x00_set_exlogins_buffer(scsi_qla_host_t *vha)
4582 {
4583 	int rval;
4584 	uint16_t	size, max_cnt;
4585 	uint32_t temp;
4586 	struct qla_hw_data *ha = vha->hw;
4587 
4588 	/* Return if we don't need to alloacate any extended logins */
4589 	if (ql2xexlogins <= MAX_FIBRE_DEVICES_2400)
4590 		return QLA_SUCCESS;
4591 
4592 	if (!IS_EXLOGIN_OFFLD_CAPABLE(ha))
4593 		return QLA_SUCCESS;
4594 
4595 	ql_log(ql_log_info, vha, 0xd021, "EXLOGIN count: %d.\n", ql2xexlogins);
4596 	max_cnt = 0;
4597 	rval = qla_get_exlogin_status(vha, &size, &max_cnt);
4598 	if (rval != QLA_SUCCESS) {
4599 		ql_log_pci(ql_log_fatal, ha->pdev, 0xd029,
4600 		    "Failed to get exlogin status.\n");
4601 		return rval;
4602 	}
4603 
4604 	temp = (ql2xexlogins > max_cnt) ? max_cnt : ql2xexlogins;
4605 	temp *= size;
4606 
4607 	if (temp != ha->exlogin_size) {
4608 		qla2x00_free_exlogin_buffer(ha);
4609 		ha->exlogin_size = temp;
4610 
4611 		ql_log(ql_log_info, vha, 0xd024,
4612 		    "EXLOGIN: max_logins=%d, portdb=0x%x, total=%d.\n",
4613 		    max_cnt, size, temp);
4614 
4615 		ql_log(ql_log_info, vha, 0xd025,
4616 		    "EXLOGIN: requested size=0x%x\n", ha->exlogin_size);
4617 
4618 		/* Get consistent memory for extended logins */
4619 		ha->exlogin_buf = dma_alloc_coherent(&ha->pdev->dev,
4620 			ha->exlogin_size, &ha->exlogin_buf_dma, GFP_KERNEL);
4621 		if (!ha->exlogin_buf) {
4622 			ql_log_pci(ql_log_fatal, ha->pdev, 0xd02a,
4623 		    "Failed to allocate memory for exlogin_buf_dma.\n");
4624 			return -ENOMEM;
4625 		}
4626 	}
4627 
4628 	/* Now configure the dma buffer */
4629 	rval = qla_set_exlogin_mem_cfg(vha, ha->exlogin_buf_dma);
4630 	if (rval) {
4631 		ql_log(ql_log_fatal, vha, 0xd033,
4632 		    "Setup extended login buffer  ****FAILED****.\n");
4633 		qla2x00_free_exlogin_buffer(ha);
4634 	}
4635 
4636 	return rval;
4637 }
4638 
4639 /*
4640 * qla2x00_free_exlogin_buffer
4641 *
4642 * Input:
4643 *	ha = adapter block pointer
4644 */
4645 void
4646 qla2x00_free_exlogin_buffer(struct qla_hw_data *ha)
4647 {
4648 	if (ha->exlogin_buf) {
4649 		dma_free_coherent(&ha->pdev->dev, ha->exlogin_size,
4650 		    ha->exlogin_buf, ha->exlogin_buf_dma);
4651 		ha->exlogin_buf = NULL;
4652 		ha->exlogin_size = 0;
4653 	}
4654 }
4655 
4656 static void
4657 qla2x00_number_of_exch(scsi_qla_host_t *vha, u32 *ret_cnt, u16 max_cnt)
4658 {
4659 	u32 temp;
4660 	struct init_cb_81xx *icb = (struct init_cb_81xx *)&vha->hw->init_cb;
4661 	*ret_cnt = FW_DEF_EXCHANGES_CNT;
4662 
4663 	if (max_cnt > vha->hw->max_exchg)
4664 		max_cnt = vha->hw->max_exchg;
4665 
4666 	if (qla_ini_mode_enabled(vha)) {
4667 		if (vha->ql2xiniexchg > max_cnt)
4668 			vha->ql2xiniexchg = max_cnt;
4669 
4670 		if (vha->ql2xiniexchg > FW_DEF_EXCHANGES_CNT)
4671 			*ret_cnt = vha->ql2xiniexchg;
4672 
4673 	} else if (qla_tgt_mode_enabled(vha)) {
4674 		if (vha->ql2xexchoffld > max_cnt) {
4675 			vha->ql2xexchoffld = max_cnt;
4676 			icb->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
4677 		}
4678 
4679 		if (vha->ql2xexchoffld > FW_DEF_EXCHANGES_CNT)
4680 			*ret_cnt = vha->ql2xexchoffld;
4681 	} else if (qla_dual_mode_enabled(vha)) {
4682 		temp = vha->ql2xiniexchg + vha->ql2xexchoffld;
4683 		if (temp > max_cnt) {
4684 			vha->ql2xiniexchg -= (temp - max_cnt)/2;
4685 			vha->ql2xexchoffld -= (((temp - max_cnt)/2) + 1);
4686 			temp = max_cnt;
4687 			icb->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
4688 		}
4689 
4690 		if (temp > FW_DEF_EXCHANGES_CNT)
4691 			*ret_cnt = temp;
4692 	}
4693 }
4694 
4695 int
4696 qla2x00_set_exchoffld_buffer(scsi_qla_host_t *vha)
4697 {
4698 	int rval;
4699 	u16	size, max_cnt;
4700 	u32 actual_cnt, totsz;
4701 	struct qla_hw_data *ha = vha->hw;
4702 
4703 	if (!ha->flags.exchoffld_enabled)
4704 		return QLA_SUCCESS;
4705 
4706 	if (!IS_EXCHG_OFFLD_CAPABLE(ha))
4707 		return QLA_SUCCESS;
4708 
4709 	max_cnt = 0;
4710 	rval = qla_get_exchoffld_status(vha, &size, &max_cnt);
4711 	if (rval != QLA_SUCCESS) {
4712 		ql_log_pci(ql_log_fatal, ha->pdev, 0xd012,
4713 		    "Failed to get exlogin status.\n");
4714 		return rval;
4715 	}
4716 
4717 	qla2x00_number_of_exch(vha, &actual_cnt, max_cnt);
4718 	ql_log(ql_log_info, vha, 0xd014,
4719 	    "Actual exchange offload count: %d.\n", actual_cnt);
4720 
4721 	totsz = actual_cnt * size;
4722 
4723 	if (totsz != ha->exchoffld_size) {
4724 		qla2x00_free_exchoffld_buffer(ha);
4725 		if (actual_cnt <= FW_DEF_EXCHANGES_CNT) {
4726 			ha->exchoffld_size = 0;
4727 			ha->flags.exchoffld_enabled = 0;
4728 			return QLA_SUCCESS;
4729 		}
4730 
4731 		ha->exchoffld_size = totsz;
4732 
4733 		ql_log(ql_log_info, vha, 0xd016,
4734 		    "Exchange offload: max_count=%d, actual count=%d entry sz=0x%x, total sz=0x%x\n",
4735 		    max_cnt, actual_cnt, size, totsz);
4736 
4737 		ql_log(ql_log_info, vha, 0xd017,
4738 		    "Exchange Buffers requested size = 0x%x\n",
4739 		    ha->exchoffld_size);
4740 
4741 		/* Get consistent memory for extended logins */
4742 		ha->exchoffld_buf = dma_alloc_coherent(&ha->pdev->dev,
4743 			ha->exchoffld_size, &ha->exchoffld_buf_dma, GFP_KERNEL);
4744 		if (!ha->exchoffld_buf) {
4745 			ql_log_pci(ql_log_fatal, ha->pdev, 0xd013,
4746 			"Failed to allocate memory for Exchange Offload.\n");
4747 
4748 			if (ha->max_exchg >
4749 			    (FW_DEF_EXCHANGES_CNT + REDUCE_EXCHANGES_CNT)) {
4750 				ha->max_exchg -= REDUCE_EXCHANGES_CNT;
4751 			} else if (ha->max_exchg >
4752 			    (FW_DEF_EXCHANGES_CNT + 512)) {
4753 				ha->max_exchg -= 512;
4754 			} else {
4755 				ha->flags.exchoffld_enabled = 0;
4756 				ql_log_pci(ql_log_fatal, ha->pdev, 0xd013,
4757 				    "Disabling Exchange offload due to lack of memory\n");
4758 			}
4759 			ha->exchoffld_size = 0;
4760 
4761 			return -ENOMEM;
4762 		}
4763 	} else if (!ha->exchoffld_buf || (actual_cnt <= FW_DEF_EXCHANGES_CNT)) {
4764 		/* pathological case */
4765 		qla2x00_free_exchoffld_buffer(ha);
4766 		ha->exchoffld_size = 0;
4767 		ha->flags.exchoffld_enabled = 0;
4768 		ql_log(ql_log_info, vha, 0xd016,
4769 		    "Exchange offload not enable: offld size=%d, actual count=%d entry sz=0x%x, total sz=0x%x.\n",
4770 		    ha->exchoffld_size, actual_cnt, size, totsz);
4771 		return 0;
4772 	}
4773 
4774 	/* Now configure the dma buffer */
4775 	rval = qla_set_exchoffld_mem_cfg(vha);
4776 	if (rval) {
4777 		ql_log(ql_log_fatal, vha, 0xd02e,
4778 		    "Setup exchange offload buffer ****FAILED****.\n");
4779 		qla2x00_free_exchoffld_buffer(ha);
4780 	} else {
4781 		/* re-adjust number of target exchange */
4782 		struct init_cb_81xx *icb = (struct init_cb_81xx *)ha->init_cb;
4783 
4784 		if (qla_ini_mode_enabled(vha))
4785 			icb->exchange_count = 0;
4786 		else
4787 			icb->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
4788 	}
4789 
4790 	return rval;
4791 }
4792 
4793 /*
4794 * qla2x00_free_exchoffld_buffer
4795 *
4796 * Input:
4797 *	ha = adapter block pointer
4798 */
4799 void
4800 qla2x00_free_exchoffld_buffer(struct qla_hw_data *ha)
4801 {
4802 	if (ha->exchoffld_buf) {
4803 		dma_free_coherent(&ha->pdev->dev, ha->exchoffld_size,
4804 		    ha->exchoffld_buf, ha->exchoffld_buf_dma);
4805 		ha->exchoffld_buf = NULL;
4806 		ha->exchoffld_size = 0;
4807 	}
4808 }
4809 
4810 /*
4811 * qla2x00_free_fw_dump
4812 *	Frees fw dump stuff.
4813 *
4814 * Input:
4815 *	ha = adapter block pointer
4816 */
4817 static void
4818 qla2x00_free_fw_dump(struct qla_hw_data *ha)
4819 {
4820 	struct fwdt *fwdt = ha->fwdt;
4821 	uint j;
4822 
4823 	if (ha->fce)
4824 		dma_free_coherent(&ha->pdev->dev,
4825 		    FCE_SIZE, ha->fce, ha->fce_dma);
4826 
4827 	if (ha->eft)
4828 		dma_free_coherent(&ha->pdev->dev,
4829 		    EFT_SIZE, ha->eft, ha->eft_dma);
4830 
4831 	vfree(ha->fw_dump);
4832 
4833 	ha->fce = NULL;
4834 	ha->fce_dma = 0;
4835 	ha->flags.fce_enabled = 0;
4836 	ha->eft = NULL;
4837 	ha->eft_dma = 0;
4838 	ha->fw_dumped = false;
4839 	ha->fw_dump_cap_flags = 0;
4840 	ha->fw_dump_reading = 0;
4841 	ha->fw_dump = NULL;
4842 	ha->fw_dump_len = 0;
4843 
4844 	for (j = 0; j < 2; j++, fwdt++) {
4845 		vfree(fwdt->template);
4846 		fwdt->template = NULL;
4847 		fwdt->length = 0;
4848 	}
4849 }
4850 
4851 /*
4852 * qla2x00_mem_free
4853 *      Frees all adapter allocated memory.
4854 *
4855 * Input:
4856 *      ha = adapter block pointer.
4857 */
4858 static void
4859 qla2x00_mem_free(struct qla_hw_data *ha)
4860 {
4861 	qla2x00_free_fw_dump(ha);
4862 
4863 	if (ha->mctp_dump)
4864 		dma_free_coherent(&ha->pdev->dev, MCTP_DUMP_SIZE, ha->mctp_dump,
4865 		    ha->mctp_dump_dma);
4866 	ha->mctp_dump = NULL;
4867 
4868 	mempool_destroy(ha->srb_mempool);
4869 	ha->srb_mempool = NULL;
4870 
4871 	if (ha->dcbx_tlv)
4872 		dma_free_coherent(&ha->pdev->dev, DCBX_TLV_DATA_SIZE,
4873 		    ha->dcbx_tlv, ha->dcbx_tlv_dma);
4874 	ha->dcbx_tlv = NULL;
4875 
4876 	if (ha->xgmac_data)
4877 		dma_free_coherent(&ha->pdev->dev, XGMAC_DATA_SIZE,
4878 		    ha->xgmac_data, ha->xgmac_data_dma);
4879 	ha->xgmac_data = NULL;
4880 
4881 	if (ha->sns_cmd)
4882 		dma_free_coherent(&ha->pdev->dev, sizeof(struct sns_cmd_pkt),
4883 		ha->sns_cmd, ha->sns_cmd_dma);
4884 	ha->sns_cmd = NULL;
4885 	ha->sns_cmd_dma = 0;
4886 
4887 	if (ha->ct_sns)
4888 		dma_free_coherent(&ha->pdev->dev, sizeof(struct ct_sns_pkt),
4889 		ha->ct_sns, ha->ct_sns_dma);
4890 	ha->ct_sns = NULL;
4891 	ha->ct_sns_dma = 0;
4892 
4893 	if (ha->sfp_data)
4894 		dma_free_coherent(&ha->pdev->dev, SFP_DEV_SIZE, ha->sfp_data,
4895 		    ha->sfp_data_dma);
4896 	ha->sfp_data = NULL;
4897 
4898 	if (ha->flt)
4899 		dma_free_coherent(&ha->pdev->dev,
4900 		    sizeof(struct qla_flt_header) + FLT_REGIONS_SIZE,
4901 		    ha->flt, ha->flt_dma);
4902 	ha->flt = NULL;
4903 	ha->flt_dma = 0;
4904 
4905 	if (ha->ms_iocb)
4906 		dma_pool_free(ha->s_dma_pool, ha->ms_iocb, ha->ms_iocb_dma);
4907 	ha->ms_iocb = NULL;
4908 	ha->ms_iocb_dma = 0;
4909 
4910 	if (ha->sf_init_cb)
4911 		dma_pool_free(ha->s_dma_pool,
4912 			      ha->sf_init_cb, ha->sf_init_cb_dma);
4913 
4914 	if (ha->ex_init_cb)
4915 		dma_pool_free(ha->s_dma_pool,
4916 			ha->ex_init_cb, ha->ex_init_cb_dma);
4917 	ha->ex_init_cb = NULL;
4918 	ha->ex_init_cb_dma = 0;
4919 
4920 	if (ha->async_pd)
4921 		dma_pool_free(ha->s_dma_pool, ha->async_pd, ha->async_pd_dma);
4922 	ha->async_pd = NULL;
4923 	ha->async_pd_dma = 0;
4924 
4925 	dma_pool_destroy(ha->s_dma_pool);
4926 	ha->s_dma_pool = NULL;
4927 
4928 	if (ha->gid_list)
4929 		dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
4930 		ha->gid_list, ha->gid_list_dma);
4931 	ha->gid_list = NULL;
4932 	ha->gid_list_dma = 0;
4933 
4934 	if (IS_QLA82XX(ha)) {
4935 		if (!list_empty(&ha->gbl_dsd_list)) {
4936 			struct dsd_dma *dsd_ptr, *tdsd_ptr;
4937 
4938 			/* clean up allocated prev pool */
4939 			list_for_each_entry_safe(dsd_ptr,
4940 				tdsd_ptr, &ha->gbl_dsd_list, list) {
4941 				dma_pool_free(ha->dl_dma_pool,
4942 				dsd_ptr->dsd_addr, dsd_ptr->dsd_list_dma);
4943 				list_del(&dsd_ptr->list);
4944 				kfree(dsd_ptr);
4945 			}
4946 		}
4947 	}
4948 
4949 	dma_pool_destroy(ha->dl_dma_pool);
4950 	ha->dl_dma_pool = NULL;
4951 
4952 	dma_pool_destroy(ha->fcp_cmnd_dma_pool);
4953 	ha->fcp_cmnd_dma_pool = NULL;
4954 
4955 	mempool_destroy(ha->ctx_mempool);
4956 	ha->ctx_mempool = NULL;
4957 
4958 	if (ql2xenabledif && ha->dif_bundl_pool) {
4959 		struct dsd_dma *dsd, *nxt;
4960 
4961 		list_for_each_entry_safe(dsd, nxt, &ha->pool.unusable.head,
4962 					 list) {
4963 			list_del(&dsd->list);
4964 			dma_pool_free(ha->dif_bundl_pool, dsd->dsd_addr,
4965 				      dsd->dsd_list_dma);
4966 			ha->dif_bundle_dma_allocs--;
4967 			kfree(dsd);
4968 			ha->dif_bundle_kallocs--;
4969 			ha->pool.unusable.count--;
4970 		}
4971 		list_for_each_entry_safe(dsd, nxt, &ha->pool.good.head, list) {
4972 			list_del(&dsd->list);
4973 			dma_pool_free(ha->dif_bundl_pool, dsd->dsd_addr,
4974 				      dsd->dsd_list_dma);
4975 			ha->dif_bundle_dma_allocs--;
4976 			kfree(dsd);
4977 			ha->dif_bundle_kallocs--;
4978 		}
4979 	}
4980 
4981 	dma_pool_destroy(ha->dif_bundl_pool);
4982 	ha->dif_bundl_pool = NULL;
4983 
4984 	qlt_mem_free(ha);
4985 	qla_remove_hostmap(ha);
4986 
4987 	if (ha->init_cb)
4988 		dma_free_coherent(&ha->pdev->dev, ha->init_cb_size,
4989 			ha->init_cb, ha->init_cb_dma);
4990 
4991 	dma_pool_destroy(ha->purex_dma_pool);
4992 	ha->purex_dma_pool = NULL;
4993 
4994 	if (ha->elsrej.c) {
4995 		dma_free_coherent(&ha->pdev->dev, ha->elsrej.size,
4996 		    ha->elsrej.c, ha->elsrej.cdma);
4997 		ha->elsrej.c = NULL;
4998 	}
4999 
5000 	ha->init_cb = NULL;
5001 	ha->init_cb_dma = 0;
5002 
5003 	vfree(ha->optrom_buffer);
5004 	ha->optrom_buffer = NULL;
5005 	kfree(ha->nvram);
5006 	ha->nvram = NULL;
5007 	kfree(ha->npiv_info);
5008 	ha->npiv_info = NULL;
5009 	kfree(ha->swl);
5010 	ha->swl = NULL;
5011 	kfree(ha->loop_id_map);
5012 	ha->sf_init_cb = NULL;
5013 	ha->sf_init_cb_dma = 0;
5014 	ha->loop_id_map = NULL;
5015 
5016 	kfree(ha->vp_map);
5017 	ha->vp_map = NULL;
5018 }
5019 
5020 struct scsi_qla_host *qla2x00_create_host(const struct scsi_host_template *sht,
5021 					  struct qla_hw_data *ha)
5022 {
5023 	struct Scsi_Host *host;
5024 	struct scsi_qla_host *vha = NULL;
5025 
5026 	host = scsi_host_alloc(sht, sizeof(scsi_qla_host_t));
5027 	if (!host) {
5028 		ql_log_pci(ql_log_fatal, ha->pdev, 0x0107,
5029 		    "Failed to allocate host from the scsi layer, aborting.\n");
5030 		return NULL;
5031 	}
5032 
5033 	/* Clear our data area */
5034 	vha = shost_priv(host);
5035 	memset(vha, 0, sizeof(scsi_qla_host_t));
5036 
5037 	vha->host = host;
5038 	vha->host_no = host->host_no;
5039 	vha->hw = ha;
5040 
5041 	vha->qlini_mode = ql2x_ini_mode;
5042 	vha->ql2xexchoffld = ql2xexchoffld;
5043 	vha->ql2xiniexchg = ql2xiniexchg;
5044 
5045 	INIT_LIST_HEAD(&vha->vp_fcports);
5046 	INIT_LIST_HEAD(&vha->work_list);
5047 	INIT_LIST_HEAD(&vha->list);
5048 	INIT_LIST_HEAD(&vha->qla_cmd_list);
5049 	INIT_LIST_HEAD(&vha->logo_list);
5050 	INIT_LIST_HEAD(&vha->plogi_ack_list);
5051 	INIT_LIST_HEAD(&vha->qp_list);
5052 	INIT_LIST_HEAD(&vha->gnl.fcports);
5053 	INIT_WORK(&vha->iocb_work, qla2x00_iocb_work_fn);
5054 
5055 	INIT_LIST_HEAD(&vha->purex_list.head);
5056 	spin_lock_init(&vha->purex_list.lock);
5057 
5058 	spin_lock_init(&vha->work_lock);
5059 	spin_lock_init(&vha->cmd_list_lock);
5060 	init_waitqueue_head(&vha->fcport_waitQ);
5061 	init_waitqueue_head(&vha->vref_waitq);
5062 	qla_enode_init(vha);
5063 	qla_edb_init(vha);
5064 
5065 
5066 	vha->gnl.size = sizeof(struct get_name_list_extended) *
5067 			(ha->max_loop_id + 1);
5068 	vha->gnl.l = dma_alloc_coherent(&ha->pdev->dev,
5069 	    vha->gnl.size, &vha->gnl.ldma, GFP_KERNEL);
5070 	if (!vha->gnl.l) {
5071 		ql_log(ql_log_fatal, vha, 0xd04a,
5072 		    "Alloc failed for name list.\n");
5073 		scsi_host_put(vha->host);
5074 		return NULL;
5075 	}
5076 
5077 	/* todo: what about ext login? */
5078 	vha->scan.size = ha->max_fibre_devices * sizeof(struct fab_scan_rp);
5079 	vha->scan.l = vmalloc(vha->scan.size);
5080 	if (!vha->scan.l) {
5081 		ql_log(ql_log_fatal, vha, 0xd04a,
5082 		    "Alloc failed for scan database.\n");
5083 		dma_free_coherent(&ha->pdev->dev, vha->gnl.size,
5084 		    vha->gnl.l, vha->gnl.ldma);
5085 		vha->gnl.l = NULL;
5086 		scsi_host_put(vha->host);
5087 		return NULL;
5088 	}
5089 	INIT_DELAYED_WORK(&vha->scan.scan_work, qla_scan_work_fn);
5090 
5091 	snprintf(vha->host_str, sizeof(vha->host_str), "%s_%lu",
5092 		 QLA2XXX_DRIVER_NAME, vha->host_no);
5093 	ql_dbg(ql_dbg_init, vha, 0x0041,
5094 	    "Allocated the host=%p hw=%p vha=%p dev_name=%s",
5095 	    vha->host, vha->hw, vha,
5096 	    dev_name(&(ha->pdev->dev)));
5097 
5098 	return vha;
5099 }
5100 
5101 struct qla_work_evt *
5102 qla2x00_alloc_work(struct scsi_qla_host *vha, enum qla_work_type type)
5103 {
5104 	struct qla_work_evt *e;
5105 
5106 	if (test_bit(UNLOADING, &vha->dpc_flags))
5107 		return NULL;
5108 
5109 	if (qla_vha_mark_busy(vha))
5110 		return NULL;
5111 
5112 	e = kzalloc(sizeof(struct qla_work_evt), GFP_ATOMIC);
5113 	if (!e) {
5114 		QLA_VHA_MARK_NOT_BUSY(vha);
5115 		return NULL;
5116 	}
5117 
5118 	INIT_LIST_HEAD(&e->list);
5119 	e->type = type;
5120 	e->flags = QLA_EVT_FLAG_FREE;
5121 	return e;
5122 }
5123 
5124 int
5125 qla2x00_post_work(struct scsi_qla_host *vha, struct qla_work_evt *e)
5126 {
5127 	unsigned long flags;
5128 	bool q = false;
5129 
5130 	spin_lock_irqsave(&vha->work_lock, flags);
5131 	list_add_tail(&e->list, &vha->work_list);
5132 
5133 	if (!test_and_set_bit(IOCB_WORK_ACTIVE, &vha->dpc_flags))
5134 		q = true;
5135 
5136 	spin_unlock_irqrestore(&vha->work_lock, flags);
5137 
5138 	if (q)
5139 		queue_work(vha->hw->wq, &vha->iocb_work);
5140 
5141 	return QLA_SUCCESS;
5142 }
5143 
5144 int
5145 qla2x00_post_aen_work(struct scsi_qla_host *vha, enum fc_host_event_code code,
5146     u32 data)
5147 {
5148 	struct qla_work_evt *e;
5149 
5150 	e = qla2x00_alloc_work(vha, QLA_EVT_AEN);
5151 	if (!e)
5152 		return QLA_FUNCTION_FAILED;
5153 
5154 	e->u.aen.code = code;
5155 	e->u.aen.data = data;
5156 	return qla2x00_post_work(vha, e);
5157 }
5158 
5159 int
5160 qla2x00_post_idc_ack_work(struct scsi_qla_host *vha, uint16_t *mb)
5161 {
5162 	struct qla_work_evt *e;
5163 
5164 	e = qla2x00_alloc_work(vha, QLA_EVT_IDC_ACK);
5165 	if (!e)
5166 		return QLA_FUNCTION_FAILED;
5167 
5168 	memcpy(e->u.idc_ack.mb, mb, QLA_IDC_ACK_REGS * sizeof(uint16_t));
5169 	return qla2x00_post_work(vha, e);
5170 }
5171 
5172 #define qla2x00_post_async_work(name, type)	\
5173 int qla2x00_post_async_##name##_work(		\
5174     struct scsi_qla_host *vha,			\
5175     fc_port_t *fcport, uint16_t *data)		\
5176 {						\
5177 	struct qla_work_evt *e;			\
5178 						\
5179 	e = qla2x00_alloc_work(vha, type);	\
5180 	if (!e)					\
5181 		return QLA_FUNCTION_FAILED;	\
5182 						\
5183 	e->u.logio.fcport = fcport;		\
5184 	if (data) {				\
5185 		e->u.logio.data[0] = data[0];	\
5186 		e->u.logio.data[1] = data[1];	\
5187 	}					\
5188 	fcport->flags |= FCF_ASYNC_ACTIVE;	\
5189 	return qla2x00_post_work(vha, e);	\
5190 }
5191 
5192 qla2x00_post_async_work(login, QLA_EVT_ASYNC_LOGIN);
5193 qla2x00_post_async_work(logout, QLA_EVT_ASYNC_LOGOUT);
5194 qla2x00_post_async_work(adisc, QLA_EVT_ASYNC_ADISC);
5195 qla2x00_post_async_work(prlo, QLA_EVT_ASYNC_PRLO);
5196 qla2x00_post_async_work(prlo_done, QLA_EVT_ASYNC_PRLO_DONE);
5197 
5198 int
5199 qla2x00_post_uevent_work(struct scsi_qla_host *vha, u32 code)
5200 {
5201 	struct qla_work_evt *e;
5202 
5203 	e = qla2x00_alloc_work(vha, QLA_EVT_UEVENT);
5204 	if (!e)
5205 		return QLA_FUNCTION_FAILED;
5206 
5207 	e->u.uevent.code = code;
5208 	return qla2x00_post_work(vha, e);
5209 }
5210 
5211 static void
5212 qla2x00_uevent_emit(struct scsi_qla_host *vha, u32 code)
5213 {
5214 	char event_string[40];
5215 	char *envp[] = { event_string, NULL };
5216 
5217 	switch (code) {
5218 	case QLA_UEVENT_CODE_FW_DUMP:
5219 		snprintf(event_string, sizeof(event_string), "FW_DUMP=%lu",
5220 		    vha->host_no);
5221 		break;
5222 	default:
5223 		/* do nothing */
5224 		break;
5225 	}
5226 	kobject_uevent_env(&vha->hw->pdev->dev.kobj, KOBJ_CHANGE, envp);
5227 }
5228 
5229 int
5230 qlafx00_post_aenfx_work(struct scsi_qla_host *vha,  uint32_t evtcode,
5231 			uint32_t *data, int cnt)
5232 {
5233 	struct qla_work_evt *e;
5234 
5235 	e = qla2x00_alloc_work(vha, QLA_EVT_AENFX);
5236 	if (!e)
5237 		return QLA_FUNCTION_FAILED;
5238 
5239 	e->u.aenfx.evtcode = evtcode;
5240 	e->u.aenfx.count = cnt;
5241 	memcpy(e->u.aenfx.mbx, data, sizeof(*data) * cnt);
5242 	return qla2x00_post_work(vha, e);
5243 }
5244 
5245 void qla24xx_sched_upd_fcport(fc_port_t *fcport)
5246 {
5247 	unsigned long flags;
5248 
5249 	if (IS_SW_RESV_ADDR(fcport->d_id))
5250 		return;
5251 
5252 	spin_lock_irqsave(&fcport->vha->work_lock, flags);
5253 	if (fcport->disc_state == DSC_UPD_FCPORT) {
5254 		spin_unlock_irqrestore(&fcport->vha->work_lock, flags);
5255 		return;
5256 	}
5257 	fcport->jiffies_at_registration = jiffies;
5258 	fcport->sec_since_registration = 0;
5259 	fcport->next_disc_state = DSC_DELETED;
5260 	qla2x00_set_fcport_disc_state(fcport, DSC_UPD_FCPORT);
5261 	spin_unlock_irqrestore(&fcport->vha->work_lock, flags);
5262 
5263 	queue_work(system_unbound_wq, &fcport->reg_work);
5264 }
5265 
5266 static
5267 void qla24xx_create_new_sess(struct scsi_qla_host *vha, struct qla_work_evt *e)
5268 {
5269 	unsigned long flags;
5270 	fc_port_t *fcport =  NULL, *tfcp;
5271 	struct qlt_plogi_ack_t *pla =
5272 	    (struct qlt_plogi_ack_t *)e->u.new_sess.pla;
5273 	uint8_t free_fcport = 0;
5274 
5275 	ql_dbg(ql_dbg_disc, vha, 0xffff,
5276 	    "%s %d %8phC enter\n",
5277 	    __func__, __LINE__, e->u.new_sess.port_name);
5278 
5279 	spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
5280 	fcport = qla2x00_find_fcport_by_wwpn(vha, e->u.new_sess.port_name, 1);
5281 	if (fcport) {
5282 		fcport->d_id = e->u.new_sess.id;
5283 		if (pla) {
5284 			fcport->fw_login_state = DSC_LS_PLOGI_PEND;
5285 			memcpy(fcport->node_name,
5286 			    pla->iocb.u.isp24.u.plogi.node_name,
5287 			    WWN_SIZE);
5288 			qlt_plogi_ack_link(vha, pla, fcport, QLT_PLOGI_LINK_SAME_WWN);
5289 			/* we took an extra ref_count to prevent PLOGI ACK when
5290 			 * fcport/sess has not been created.
5291 			 */
5292 			pla->ref_count--;
5293 		}
5294 	} else {
5295 		spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5296 		fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
5297 		if (fcport) {
5298 			fcport->d_id = e->u.new_sess.id;
5299 			fcport->flags |= FCF_FABRIC_DEVICE;
5300 			fcport->fw_login_state = DSC_LS_PLOGI_PEND;
5301 			fcport->tgt_short_link_down_cnt = 0;
5302 
5303 			memcpy(fcport->port_name, e->u.new_sess.port_name,
5304 			    WWN_SIZE);
5305 
5306 			fcport->fc4_type = e->u.new_sess.fc4_type;
5307 			if (NVME_PRIORITY(vha->hw, fcport))
5308 				fcport->do_prli_nvme = 1;
5309 			else
5310 				fcport->do_prli_nvme = 0;
5311 
5312 			if (e->u.new_sess.fc4_type & FS_FCP_IS_N2N) {
5313 				fcport->dm_login_expire = jiffies +
5314 					QLA_N2N_WAIT_TIME * HZ;
5315 				fcport->fc4_type = FS_FC4TYPE_FCP;
5316 				fcport->n2n_flag = 1;
5317 				if (vha->flags.nvme_enabled)
5318 					fcport->fc4_type |= FS_FC4TYPE_NVME;
5319 			}
5320 
5321 		} else {
5322 			ql_dbg(ql_dbg_disc, vha, 0xffff,
5323 				   "%s %8phC mem alloc fail.\n",
5324 				   __func__, e->u.new_sess.port_name);
5325 
5326 			if (pla) {
5327 				list_del(&pla->list);
5328 				kmem_cache_free(qla_tgt_plogi_cachep, pla);
5329 			}
5330 			return;
5331 		}
5332 
5333 		spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
5334 		/* search again to make sure no one else got ahead */
5335 		tfcp = qla2x00_find_fcport_by_wwpn(vha,
5336 		    e->u.new_sess.port_name, 1);
5337 		if (tfcp) {
5338 			/* should rarily happen */
5339 			ql_dbg(ql_dbg_disc, vha, 0xffff,
5340 			    "%s %8phC found existing fcport b4 add. DS %d LS %d\n",
5341 			    __func__, tfcp->port_name, tfcp->disc_state,
5342 			    tfcp->fw_login_state);
5343 
5344 			free_fcport = 1;
5345 		} else {
5346 			list_add_tail(&fcport->list, &vha->vp_fcports);
5347 
5348 		}
5349 		if (pla) {
5350 			qlt_plogi_ack_link(vha, pla, fcport,
5351 			    QLT_PLOGI_LINK_SAME_WWN);
5352 			pla->ref_count--;
5353 		}
5354 	}
5355 	spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5356 
5357 	if (fcport) {
5358 		fcport->id_changed = 1;
5359 		fcport->scan_state = QLA_FCPORT_FOUND;
5360 		fcport->chip_reset = vha->hw->base_qpair->chip_reset;
5361 		memcpy(fcport->node_name, e->u.new_sess.node_name, WWN_SIZE);
5362 
5363 		if (pla) {
5364 			if (pla->iocb.u.isp24.status_subcode == ELS_PRLI) {
5365 				u16 wd3_lo;
5366 
5367 				fcport->fw_login_state = DSC_LS_PRLI_PEND;
5368 				fcport->local = 0;
5369 				fcport->loop_id =
5370 					le16_to_cpu(
5371 					    pla->iocb.u.isp24.nport_handle);
5372 				fcport->fw_login_state = DSC_LS_PRLI_PEND;
5373 				wd3_lo =
5374 				    le16_to_cpu(
5375 					pla->iocb.u.isp24.u.prli.wd3_lo);
5376 
5377 				if (wd3_lo & BIT_7)
5378 					fcport->conf_compl_supported = 1;
5379 
5380 				if ((wd3_lo & BIT_4) == 0)
5381 					fcport->port_type = FCT_INITIATOR;
5382 				else
5383 					fcport->port_type = FCT_TARGET;
5384 			}
5385 			qlt_plogi_ack_unref(vha, pla);
5386 		} else {
5387 			fc_port_t *dfcp = NULL;
5388 
5389 			spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
5390 			tfcp = qla2x00_find_fcport_by_nportid(vha,
5391 			    &e->u.new_sess.id, 1);
5392 			if (tfcp && (tfcp != fcport)) {
5393 				/*
5394 				 * We have a conflict fcport with same NportID.
5395 				 */
5396 				ql_dbg(ql_dbg_disc, vha, 0xffff,
5397 				    "%s %8phC found conflict b4 add. DS %d LS %d\n",
5398 				    __func__, tfcp->port_name, tfcp->disc_state,
5399 				    tfcp->fw_login_state);
5400 
5401 				switch (tfcp->disc_state) {
5402 				case DSC_DELETED:
5403 					break;
5404 				case DSC_DELETE_PEND:
5405 					fcport->login_pause = 1;
5406 					tfcp->conflict = fcport;
5407 					break;
5408 				default:
5409 					fcport->login_pause = 1;
5410 					tfcp->conflict = fcport;
5411 					dfcp = tfcp;
5412 					break;
5413 				}
5414 			}
5415 			spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5416 			if (dfcp)
5417 				qlt_schedule_sess_for_deletion(tfcp);
5418 
5419 			if (N2N_TOPO(vha->hw)) {
5420 				fcport->flags &= ~FCF_FABRIC_DEVICE;
5421 				fcport->keep_nport_handle = 1;
5422 				if (vha->flags.nvme_enabled) {
5423 					fcport->fc4_type =
5424 					    (FS_FC4TYPE_NVME | FS_FC4TYPE_FCP);
5425 					fcport->n2n_flag = 1;
5426 				}
5427 				fcport->fw_login_state = 0;
5428 
5429 				schedule_delayed_work(&vha->scan.scan_work, 5);
5430 			} else {
5431 				qla24xx_fcport_handle_login(vha, fcport);
5432 			}
5433 		}
5434 	}
5435 
5436 	if (free_fcport) {
5437 		qla2x00_free_fcport(fcport);
5438 		if (pla) {
5439 			list_del(&pla->list);
5440 			kmem_cache_free(qla_tgt_plogi_cachep, pla);
5441 		}
5442 	}
5443 }
5444 
5445 static void qla_sp_retry(struct scsi_qla_host *vha, struct qla_work_evt *e)
5446 {
5447 	struct srb *sp = e->u.iosb.sp;
5448 	int rval;
5449 
5450 	rval = qla2x00_start_sp(sp);
5451 	if (rval != QLA_SUCCESS) {
5452 		ql_dbg(ql_dbg_disc, vha, 0x2043,
5453 		    "%s: %s: Re-issue IOCB failed (%d).\n",
5454 		    __func__, sp->name, rval);
5455 		qla24xx_sp_unmap(vha, sp);
5456 	}
5457 }
5458 
5459 void
5460 qla2x00_do_work(struct scsi_qla_host *vha)
5461 {
5462 	struct qla_work_evt *e, *tmp;
5463 	unsigned long flags;
5464 	LIST_HEAD(work);
5465 	int rc;
5466 
5467 	spin_lock_irqsave(&vha->work_lock, flags);
5468 	list_splice_init(&vha->work_list, &work);
5469 	spin_unlock_irqrestore(&vha->work_lock, flags);
5470 
5471 	list_for_each_entry_safe(e, tmp, &work, list) {
5472 		rc = QLA_SUCCESS;
5473 		switch (e->type) {
5474 		case QLA_EVT_AEN:
5475 			fc_host_post_event(vha->host, fc_get_event_number(),
5476 			    e->u.aen.code, e->u.aen.data);
5477 			break;
5478 		case QLA_EVT_IDC_ACK:
5479 			qla81xx_idc_ack(vha, e->u.idc_ack.mb);
5480 			break;
5481 		case QLA_EVT_ASYNC_LOGIN:
5482 			qla2x00_async_login(vha, e->u.logio.fcport,
5483 			    e->u.logio.data);
5484 			break;
5485 		case QLA_EVT_ASYNC_LOGOUT:
5486 			rc = qla2x00_async_logout(vha, e->u.logio.fcport);
5487 			break;
5488 		case QLA_EVT_ASYNC_ADISC:
5489 			qla2x00_async_adisc(vha, e->u.logio.fcport,
5490 			    e->u.logio.data);
5491 			break;
5492 		case QLA_EVT_UEVENT:
5493 			qla2x00_uevent_emit(vha, e->u.uevent.code);
5494 			break;
5495 		case QLA_EVT_AENFX:
5496 			qlafx00_process_aen(vha, e);
5497 			break;
5498 		case QLA_EVT_UNMAP:
5499 			qla24xx_sp_unmap(vha, e->u.iosb.sp);
5500 			break;
5501 		case QLA_EVT_RELOGIN:
5502 			qla2x00_relogin(vha);
5503 			break;
5504 		case QLA_EVT_NEW_SESS:
5505 			qla24xx_create_new_sess(vha, e);
5506 			break;
5507 		case QLA_EVT_GPDB:
5508 			qla24xx_async_gpdb(vha, e->u.fcport.fcport,
5509 			    e->u.fcport.opt);
5510 			break;
5511 		case QLA_EVT_PRLI:
5512 			qla24xx_async_prli(vha, e->u.fcport.fcport);
5513 			break;
5514 		case QLA_EVT_GPSC:
5515 			qla24xx_async_gpsc(vha, e->u.fcport.fcport);
5516 			break;
5517 		case QLA_EVT_GNL:
5518 			qla24xx_async_gnl(vha, e->u.fcport.fcport);
5519 			break;
5520 		case QLA_EVT_NACK:
5521 			qla24xx_do_nack_work(vha, e);
5522 			break;
5523 		case QLA_EVT_ASYNC_PRLO:
5524 			rc = qla2x00_async_prlo(vha, e->u.logio.fcport);
5525 			break;
5526 		case QLA_EVT_ASYNC_PRLO_DONE:
5527 			qla2x00_async_prlo_done(vha, e->u.logio.fcport,
5528 			    e->u.logio.data);
5529 			break;
5530 		case QLA_EVT_GPNFT:
5531 			qla24xx_async_gpnft(vha, e->u.gpnft.fc4_type,
5532 			    e->u.gpnft.sp);
5533 			break;
5534 		case QLA_EVT_GPNFT_DONE:
5535 			qla24xx_async_gpnft_done(vha, e->u.iosb.sp);
5536 			break;
5537 		case QLA_EVT_GNNFT_DONE:
5538 			qla24xx_async_gnnft_done(vha, e->u.iosb.sp);
5539 			break;
5540 		case QLA_EVT_GFPNID:
5541 			qla24xx_async_gfpnid(vha, e->u.fcport.fcport);
5542 			break;
5543 		case QLA_EVT_SP_RETRY:
5544 			qla_sp_retry(vha, e);
5545 			break;
5546 		case QLA_EVT_IIDMA:
5547 			qla_do_iidma_work(vha, e->u.fcport.fcport);
5548 			break;
5549 		case QLA_EVT_ELS_PLOGI:
5550 			qla24xx_els_dcmd2_iocb(vha, ELS_DCMD_PLOGI,
5551 			    e->u.fcport.fcport, false);
5552 			break;
5553 		case QLA_EVT_SA_REPLACE:
5554 			rc = qla24xx_issue_sa_replace_iocb(vha, e);
5555 			break;
5556 		}
5557 
5558 		if (rc == EAGAIN) {
5559 			/* put 'work' at head of 'vha->work_list' */
5560 			spin_lock_irqsave(&vha->work_lock, flags);
5561 			list_splice(&work, &vha->work_list);
5562 			spin_unlock_irqrestore(&vha->work_lock, flags);
5563 			break;
5564 		}
5565 		list_del_init(&e->list);
5566 		if (e->flags & QLA_EVT_FLAG_FREE)
5567 			kfree(e);
5568 
5569 		/* For each work completed decrement vha ref count */
5570 		QLA_VHA_MARK_NOT_BUSY(vha);
5571 	}
5572 }
5573 
5574 int qla24xx_post_relogin_work(struct scsi_qla_host *vha)
5575 {
5576 	struct qla_work_evt *e;
5577 
5578 	e = qla2x00_alloc_work(vha, QLA_EVT_RELOGIN);
5579 
5580 	if (!e) {
5581 		set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
5582 		return QLA_FUNCTION_FAILED;
5583 	}
5584 
5585 	return qla2x00_post_work(vha, e);
5586 }
5587 
5588 /* Relogins all the fcports of a vport
5589  * Context: dpc thread
5590  */
5591 void qla2x00_relogin(struct scsi_qla_host *vha)
5592 {
5593 	fc_port_t       *fcport;
5594 	int status, relogin_needed = 0;
5595 	struct event_arg ea;
5596 
5597 	list_for_each_entry(fcport, &vha->vp_fcports, list) {
5598 		/*
5599 		 * If the port is not ONLINE then try to login
5600 		 * to it if we haven't run out of retries.
5601 		 */
5602 		if (atomic_read(&fcport->state) != FCS_ONLINE &&
5603 		    fcport->login_retry) {
5604 			if (fcport->scan_state != QLA_FCPORT_FOUND ||
5605 			    fcport->disc_state == DSC_LOGIN_AUTH_PEND ||
5606 			    fcport->disc_state == DSC_LOGIN_COMPLETE)
5607 				continue;
5608 
5609 			if (fcport->flags & (FCF_ASYNC_SENT|FCF_ASYNC_ACTIVE) ||
5610 				fcport->disc_state == DSC_DELETE_PEND) {
5611 				relogin_needed = 1;
5612 			} else {
5613 				if (vha->hw->current_topology != ISP_CFG_NL) {
5614 					memset(&ea, 0, sizeof(ea));
5615 					ea.fcport = fcport;
5616 					qla24xx_handle_relogin_event(vha, &ea);
5617 				} else if (vha->hw->current_topology ==
5618 					 ISP_CFG_NL &&
5619 					IS_QLA2XXX_MIDTYPE(vha->hw)) {
5620 					(void)qla24xx_fcport_handle_login(vha,
5621 									fcport);
5622 				} else if (vha->hw->current_topology ==
5623 				    ISP_CFG_NL) {
5624 					fcport->login_retry--;
5625 					status =
5626 					    qla2x00_local_device_login(vha,
5627 						fcport);
5628 					if (status == QLA_SUCCESS) {
5629 						fcport->old_loop_id =
5630 						    fcport->loop_id;
5631 						ql_dbg(ql_dbg_disc, vha, 0x2003,
5632 						    "Port login OK: logged in ID 0x%x.\n",
5633 						    fcport->loop_id);
5634 						qla2x00_update_fcport
5635 							(vha, fcport);
5636 					} else if (status == 1) {
5637 						set_bit(RELOGIN_NEEDED,
5638 						    &vha->dpc_flags);
5639 						/* retry the login again */
5640 						ql_dbg(ql_dbg_disc, vha, 0x2007,
5641 						    "Retrying %d login again loop_id 0x%x.\n",
5642 						    fcport->login_retry,
5643 						    fcport->loop_id);
5644 					} else {
5645 						fcport->login_retry = 0;
5646 					}
5647 
5648 					if (fcport->login_retry == 0 &&
5649 					    status != QLA_SUCCESS)
5650 						qla2x00_clear_loop_id(fcport);
5651 				}
5652 			}
5653 		}
5654 		if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5655 			break;
5656 	}
5657 
5658 	if (relogin_needed)
5659 		set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
5660 
5661 	ql_dbg(ql_dbg_disc, vha, 0x400e,
5662 	    "Relogin end.\n");
5663 }
5664 
5665 /* Schedule work on any of the dpc-workqueues */
5666 void
5667 qla83xx_schedule_work(scsi_qla_host_t *base_vha, int work_code)
5668 {
5669 	struct qla_hw_data *ha = base_vha->hw;
5670 
5671 	switch (work_code) {
5672 	case MBA_IDC_AEN: /* 0x8200 */
5673 		if (ha->dpc_lp_wq)
5674 			queue_work(ha->dpc_lp_wq, &ha->idc_aen);
5675 		break;
5676 
5677 	case QLA83XX_NIC_CORE_RESET: /* 0x1 */
5678 		if (!ha->flags.nic_core_reset_hdlr_active) {
5679 			if (ha->dpc_hp_wq)
5680 				queue_work(ha->dpc_hp_wq, &ha->nic_core_reset);
5681 		} else
5682 			ql_dbg(ql_dbg_p3p, base_vha, 0xb05e,
5683 			    "NIC Core reset is already active. Skip "
5684 			    "scheduling it again.\n");
5685 		break;
5686 	case QLA83XX_IDC_STATE_HANDLER: /* 0x2 */
5687 		if (ha->dpc_hp_wq)
5688 			queue_work(ha->dpc_hp_wq, &ha->idc_state_handler);
5689 		break;
5690 	case QLA83XX_NIC_CORE_UNRECOVERABLE: /* 0x3 */
5691 		if (ha->dpc_hp_wq)
5692 			queue_work(ha->dpc_hp_wq, &ha->nic_core_unrecoverable);
5693 		break;
5694 	default:
5695 		ql_log(ql_log_warn, base_vha, 0xb05f,
5696 		    "Unknown work-code=0x%x.\n", work_code);
5697 	}
5698 
5699 	return;
5700 }
5701 
5702 /* Work: Perform NIC Core Unrecoverable state handling */
5703 void
5704 qla83xx_nic_core_unrecoverable_work(struct work_struct *work)
5705 {
5706 	struct qla_hw_data *ha =
5707 		container_of(work, struct qla_hw_data, nic_core_unrecoverable);
5708 	scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
5709 	uint32_t dev_state = 0;
5710 
5711 	qla83xx_idc_lock(base_vha, 0);
5712 	qla83xx_rd_reg(base_vha, QLA83XX_IDC_DEV_STATE, &dev_state);
5713 	qla83xx_reset_ownership(base_vha);
5714 	if (ha->flags.nic_core_reset_owner) {
5715 		ha->flags.nic_core_reset_owner = 0;
5716 		qla83xx_wr_reg(base_vha, QLA83XX_IDC_DEV_STATE,
5717 		    QLA8XXX_DEV_FAILED);
5718 		ql_log(ql_log_info, base_vha, 0xb060, "HW State: FAILED.\n");
5719 		qla83xx_schedule_work(base_vha, QLA83XX_IDC_STATE_HANDLER);
5720 	}
5721 	qla83xx_idc_unlock(base_vha, 0);
5722 }
5723 
5724 /* Work: Execute IDC state handler */
5725 void
5726 qla83xx_idc_state_handler_work(struct work_struct *work)
5727 {
5728 	struct qla_hw_data *ha =
5729 		container_of(work, struct qla_hw_data, idc_state_handler);
5730 	scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
5731 	uint32_t dev_state = 0;
5732 
5733 	qla83xx_idc_lock(base_vha, 0);
5734 	qla83xx_rd_reg(base_vha, QLA83XX_IDC_DEV_STATE, &dev_state);
5735 	if (dev_state == QLA8XXX_DEV_FAILED ||
5736 			dev_state == QLA8XXX_DEV_NEED_QUIESCENT)
5737 		qla83xx_idc_state_handler(base_vha);
5738 	qla83xx_idc_unlock(base_vha, 0);
5739 }
5740 
5741 static int
5742 qla83xx_check_nic_core_fw_alive(scsi_qla_host_t *base_vha)
5743 {
5744 	int rval = QLA_SUCCESS;
5745 	unsigned long heart_beat_wait = jiffies + (1 * HZ);
5746 	uint32_t heart_beat_counter1, heart_beat_counter2;
5747 
5748 	do {
5749 		if (time_after(jiffies, heart_beat_wait)) {
5750 			ql_dbg(ql_dbg_p3p, base_vha, 0xb07c,
5751 			    "Nic Core f/w is not alive.\n");
5752 			rval = QLA_FUNCTION_FAILED;
5753 			break;
5754 		}
5755 
5756 		qla83xx_idc_lock(base_vha, 0);
5757 		qla83xx_rd_reg(base_vha, QLA83XX_FW_HEARTBEAT,
5758 		    &heart_beat_counter1);
5759 		qla83xx_idc_unlock(base_vha, 0);
5760 		msleep(100);
5761 		qla83xx_idc_lock(base_vha, 0);
5762 		qla83xx_rd_reg(base_vha, QLA83XX_FW_HEARTBEAT,
5763 		    &heart_beat_counter2);
5764 		qla83xx_idc_unlock(base_vha, 0);
5765 	} while (heart_beat_counter1 == heart_beat_counter2);
5766 
5767 	return rval;
5768 }
5769 
5770 /* Work: Perform NIC Core Reset handling */
5771 void
5772 qla83xx_nic_core_reset_work(struct work_struct *work)
5773 {
5774 	struct qla_hw_data *ha =
5775 		container_of(work, struct qla_hw_data, nic_core_reset);
5776 	scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
5777 	uint32_t dev_state = 0;
5778 
5779 	if (IS_QLA2031(ha)) {
5780 		if (qla2xxx_mctp_dump(base_vha) != QLA_SUCCESS)
5781 			ql_log(ql_log_warn, base_vha, 0xb081,
5782 			    "Failed to dump mctp\n");
5783 		return;
5784 	}
5785 
5786 	if (!ha->flags.nic_core_reset_hdlr_active) {
5787 		if (qla83xx_check_nic_core_fw_alive(base_vha) == QLA_SUCCESS) {
5788 			qla83xx_idc_lock(base_vha, 0);
5789 			qla83xx_rd_reg(base_vha, QLA83XX_IDC_DEV_STATE,
5790 			    &dev_state);
5791 			qla83xx_idc_unlock(base_vha, 0);
5792 			if (dev_state != QLA8XXX_DEV_NEED_RESET) {
5793 				ql_dbg(ql_dbg_p3p, base_vha, 0xb07a,
5794 				    "Nic Core f/w is alive.\n");
5795 				return;
5796 			}
5797 		}
5798 
5799 		ha->flags.nic_core_reset_hdlr_active = 1;
5800 		if (qla83xx_nic_core_reset(base_vha)) {
5801 			/* NIC Core reset failed. */
5802 			ql_dbg(ql_dbg_p3p, base_vha, 0xb061,
5803 			    "NIC Core reset failed.\n");
5804 		}
5805 		ha->flags.nic_core_reset_hdlr_active = 0;
5806 	}
5807 }
5808 
5809 /* Work: Handle 8200 IDC aens */
5810 void
5811 qla83xx_service_idc_aen(struct work_struct *work)
5812 {
5813 	struct qla_hw_data *ha =
5814 		container_of(work, struct qla_hw_data, idc_aen);
5815 	scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
5816 	uint32_t dev_state, idc_control;
5817 
5818 	qla83xx_idc_lock(base_vha, 0);
5819 	qla83xx_rd_reg(base_vha, QLA83XX_IDC_DEV_STATE, &dev_state);
5820 	qla83xx_rd_reg(base_vha, QLA83XX_IDC_CONTROL, &idc_control);
5821 	qla83xx_idc_unlock(base_vha, 0);
5822 	if (dev_state == QLA8XXX_DEV_NEED_RESET) {
5823 		if (idc_control & QLA83XX_IDC_GRACEFUL_RESET) {
5824 			ql_dbg(ql_dbg_p3p, base_vha, 0xb062,
5825 			    "Application requested NIC Core Reset.\n");
5826 			qla83xx_schedule_work(base_vha, QLA83XX_NIC_CORE_RESET);
5827 		} else if (qla83xx_check_nic_core_fw_alive(base_vha) ==
5828 		    QLA_SUCCESS) {
5829 			ql_dbg(ql_dbg_p3p, base_vha, 0xb07b,
5830 			    "Other protocol driver requested NIC Core Reset.\n");
5831 			qla83xx_schedule_work(base_vha, QLA83XX_NIC_CORE_RESET);
5832 		}
5833 	} else if (dev_state == QLA8XXX_DEV_FAILED ||
5834 			dev_state == QLA8XXX_DEV_NEED_QUIESCENT) {
5835 		qla83xx_schedule_work(base_vha, QLA83XX_IDC_STATE_HANDLER);
5836 	}
5837 }
5838 
5839 /*
5840  * Control the frequency of IDC lock retries
5841  */
5842 #define QLA83XX_WAIT_LOGIC_MS	100
5843 
5844 static int
5845 qla83xx_force_lock_recovery(scsi_qla_host_t *base_vha)
5846 {
5847 	int rval;
5848 	uint32_t data;
5849 	uint32_t idc_lck_rcvry_stage_mask = 0x3;
5850 	uint32_t idc_lck_rcvry_owner_mask = 0x3c;
5851 	struct qla_hw_data *ha = base_vha->hw;
5852 
5853 	ql_dbg(ql_dbg_p3p, base_vha, 0xb086,
5854 	    "Trying force recovery of the IDC lock.\n");
5855 
5856 	rval = qla83xx_rd_reg(base_vha, QLA83XX_IDC_LOCK_RECOVERY, &data);
5857 	if (rval)
5858 		return rval;
5859 
5860 	if ((data & idc_lck_rcvry_stage_mask) > 0) {
5861 		return QLA_SUCCESS;
5862 	} else {
5863 		data = (IDC_LOCK_RECOVERY_STAGE1) | (ha->portnum << 2);
5864 		rval = qla83xx_wr_reg(base_vha, QLA83XX_IDC_LOCK_RECOVERY,
5865 		    data);
5866 		if (rval)
5867 			return rval;
5868 
5869 		msleep(200);
5870 
5871 		rval = qla83xx_rd_reg(base_vha, QLA83XX_IDC_LOCK_RECOVERY,
5872 		    &data);
5873 		if (rval)
5874 			return rval;
5875 
5876 		if (((data & idc_lck_rcvry_owner_mask) >> 2) == ha->portnum) {
5877 			data &= (IDC_LOCK_RECOVERY_STAGE2 |
5878 					~(idc_lck_rcvry_stage_mask));
5879 			rval = qla83xx_wr_reg(base_vha,
5880 			    QLA83XX_IDC_LOCK_RECOVERY, data);
5881 			if (rval)
5882 				return rval;
5883 
5884 			/* Forcefully perform IDC UnLock */
5885 			rval = qla83xx_rd_reg(base_vha, QLA83XX_DRIVER_UNLOCK,
5886 			    &data);
5887 			if (rval)
5888 				return rval;
5889 			/* Clear lock-id by setting 0xff */
5890 			rval = qla83xx_wr_reg(base_vha, QLA83XX_DRIVER_LOCKID,
5891 			    0xff);
5892 			if (rval)
5893 				return rval;
5894 			/* Clear lock-recovery by setting 0x0 */
5895 			rval = qla83xx_wr_reg(base_vha,
5896 			    QLA83XX_IDC_LOCK_RECOVERY, 0x0);
5897 			if (rval)
5898 				return rval;
5899 		} else
5900 			return QLA_SUCCESS;
5901 	}
5902 
5903 	return rval;
5904 }
5905 
5906 static int
5907 qla83xx_idc_lock_recovery(scsi_qla_host_t *base_vha)
5908 {
5909 	int rval = QLA_SUCCESS;
5910 	uint32_t o_drv_lockid, n_drv_lockid;
5911 	unsigned long lock_recovery_timeout;
5912 
5913 	lock_recovery_timeout = jiffies + QLA83XX_MAX_LOCK_RECOVERY_WAIT;
5914 retry_lockid:
5915 	rval = qla83xx_rd_reg(base_vha, QLA83XX_DRIVER_LOCKID, &o_drv_lockid);
5916 	if (rval)
5917 		goto exit;
5918 
5919 	/* MAX wait time before forcing IDC Lock recovery = 2 secs */
5920 	if (time_after_eq(jiffies, lock_recovery_timeout)) {
5921 		if (qla83xx_force_lock_recovery(base_vha) == QLA_SUCCESS)
5922 			return QLA_SUCCESS;
5923 		else
5924 			return QLA_FUNCTION_FAILED;
5925 	}
5926 
5927 	rval = qla83xx_rd_reg(base_vha, QLA83XX_DRIVER_LOCKID, &n_drv_lockid);
5928 	if (rval)
5929 		goto exit;
5930 
5931 	if (o_drv_lockid == n_drv_lockid) {
5932 		msleep(QLA83XX_WAIT_LOGIC_MS);
5933 		goto retry_lockid;
5934 	} else
5935 		return QLA_SUCCESS;
5936 
5937 exit:
5938 	return rval;
5939 }
5940 
5941 /*
5942  * Context: task, can sleep
5943  */
5944 void
5945 qla83xx_idc_lock(scsi_qla_host_t *base_vha, uint16_t requester_id)
5946 {
5947 	uint32_t data;
5948 	uint32_t lock_owner;
5949 	struct qla_hw_data *ha = base_vha->hw;
5950 
5951 	might_sleep();
5952 
5953 	/* IDC-lock implementation using driver-lock/lock-id remote registers */
5954 retry_lock:
5955 	if (qla83xx_rd_reg(base_vha, QLA83XX_DRIVER_LOCK, &data)
5956 	    == QLA_SUCCESS) {
5957 		if (data) {
5958 			/* Setting lock-id to our function-number */
5959 			qla83xx_wr_reg(base_vha, QLA83XX_DRIVER_LOCKID,
5960 			    ha->portnum);
5961 		} else {
5962 			qla83xx_rd_reg(base_vha, QLA83XX_DRIVER_LOCKID,
5963 			    &lock_owner);
5964 			ql_dbg(ql_dbg_p3p, base_vha, 0xb063,
5965 			    "Failed to acquire IDC lock, acquired by %d, "
5966 			    "retrying...\n", lock_owner);
5967 
5968 			/* Retry/Perform IDC-Lock recovery */
5969 			if (qla83xx_idc_lock_recovery(base_vha)
5970 			    == QLA_SUCCESS) {
5971 				msleep(QLA83XX_WAIT_LOGIC_MS);
5972 				goto retry_lock;
5973 			} else
5974 				ql_log(ql_log_warn, base_vha, 0xb075,
5975 				    "IDC Lock recovery FAILED.\n");
5976 		}
5977 
5978 	}
5979 
5980 	return;
5981 }
5982 
5983 static bool
5984 qla25xx_rdp_rsp_reduce_size(struct scsi_qla_host *vha,
5985 	struct purex_entry_24xx *purex)
5986 {
5987 	char fwstr[16];
5988 	u32 sid = purex->s_id[2] << 16 | purex->s_id[1] << 8 | purex->s_id[0];
5989 	struct port_database_24xx *pdb;
5990 
5991 	/* Domain Controller is always logged-out. */
5992 	/* if RDP request is not from Domain Controller: */
5993 	if (sid != 0xfffc01)
5994 		return false;
5995 
5996 	ql_dbg(ql_dbg_init, vha, 0x0181, "%s: s_id=%#x\n", __func__, sid);
5997 
5998 	pdb = kzalloc(sizeof(*pdb), GFP_KERNEL);
5999 	if (!pdb) {
6000 		ql_dbg(ql_dbg_init, vha, 0x0181,
6001 		    "%s: Failed allocate pdb\n", __func__);
6002 	} else if (qla24xx_get_port_database(vha,
6003 				le16_to_cpu(purex->nport_handle), pdb)) {
6004 		ql_dbg(ql_dbg_init, vha, 0x0181,
6005 		    "%s: Failed get pdb sid=%x\n", __func__, sid);
6006 	} else if (pdb->current_login_state != PDS_PLOGI_COMPLETE &&
6007 	    pdb->current_login_state != PDS_PRLI_COMPLETE) {
6008 		ql_dbg(ql_dbg_init, vha, 0x0181,
6009 		    "%s: Port not logged in sid=%#x\n", __func__, sid);
6010 	} else {
6011 		/* RDP request is from logged in port */
6012 		kfree(pdb);
6013 		return false;
6014 	}
6015 	kfree(pdb);
6016 
6017 	vha->hw->isp_ops->fw_version_str(vha, fwstr, sizeof(fwstr));
6018 	fwstr[strcspn(fwstr, " ")] = 0;
6019 	/* if FW version allows RDP response length upto 2048 bytes: */
6020 	if (strcmp(fwstr, "8.09.00") > 0 || strcmp(fwstr, "8.05.65") == 0)
6021 		return false;
6022 
6023 	ql_dbg(ql_dbg_init, vha, 0x0181, "%s: fw=%s\n", __func__, fwstr);
6024 
6025 	/* RDP response length is to be reduced to maximum 256 bytes */
6026 	return true;
6027 }
6028 
6029 /*
6030  * Function Name: qla24xx_process_purex_iocb
6031  *
6032  * Description:
6033  * Prepare a RDP response and send to Fabric switch
6034  *
6035  * PARAMETERS:
6036  * vha:	SCSI qla host
6037  * purex: RDP request received by HBA
6038  */
6039 void qla24xx_process_purex_rdp(struct scsi_qla_host *vha,
6040 			       struct purex_item *item)
6041 {
6042 	struct qla_hw_data *ha = vha->hw;
6043 	struct purex_entry_24xx *purex =
6044 	    (struct purex_entry_24xx *)&item->iocb;
6045 	dma_addr_t rsp_els_dma;
6046 	dma_addr_t rsp_payload_dma;
6047 	dma_addr_t stat_dma;
6048 	dma_addr_t sfp_dma;
6049 	struct els_entry_24xx *rsp_els = NULL;
6050 	struct rdp_rsp_payload *rsp_payload = NULL;
6051 	struct link_statistics *stat = NULL;
6052 	uint8_t *sfp = NULL;
6053 	uint16_t sfp_flags = 0;
6054 	uint rsp_payload_length = sizeof(*rsp_payload);
6055 	int rval;
6056 
6057 	ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x0180,
6058 	    "%s: Enter\n", __func__);
6059 
6060 	ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x0181,
6061 	    "-------- ELS REQ -------\n");
6062 	ql_dump_buffer(ql_dbg_init + ql_dbg_verbose, vha, 0x0182,
6063 	    purex, sizeof(*purex));
6064 
6065 	if (qla25xx_rdp_rsp_reduce_size(vha, purex)) {
6066 		rsp_payload_length =
6067 		    offsetof(typeof(*rsp_payload), optical_elmt_desc);
6068 		ql_dbg(ql_dbg_init, vha, 0x0181,
6069 		    "Reducing RSP payload length to %u bytes...\n",
6070 		    rsp_payload_length);
6071 	}
6072 
6073 	rsp_els = dma_alloc_coherent(&ha->pdev->dev, sizeof(*rsp_els),
6074 	    &rsp_els_dma, GFP_KERNEL);
6075 	if (!rsp_els) {
6076 		ql_log(ql_log_warn, vha, 0x0183,
6077 		    "Failed allocate dma buffer ELS RSP.\n");
6078 		goto dealloc;
6079 	}
6080 
6081 	rsp_payload = dma_alloc_coherent(&ha->pdev->dev, sizeof(*rsp_payload),
6082 	    &rsp_payload_dma, GFP_KERNEL);
6083 	if (!rsp_payload) {
6084 		ql_log(ql_log_warn, vha, 0x0184,
6085 		    "Failed allocate dma buffer ELS RSP payload.\n");
6086 		goto dealloc;
6087 	}
6088 
6089 	sfp = dma_alloc_coherent(&ha->pdev->dev, SFP_RTDI_LEN,
6090 	    &sfp_dma, GFP_KERNEL);
6091 
6092 	stat = dma_alloc_coherent(&ha->pdev->dev, sizeof(*stat),
6093 	    &stat_dma, GFP_KERNEL);
6094 
6095 	/* Prepare Response IOCB */
6096 	rsp_els->entry_type = ELS_IOCB_TYPE;
6097 	rsp_els->entry_count = 1;
6098 	rsp_els->sys_define = 0;
6099 	rsp_els->entry_status = 0;
6100 	rsp_els->handle = 0;
6101 	rsp_els->nport_handle = purex->nport_handle;
6102 	rsp_els->tx_dsd_count = cpu_to_le16(1);
6103 	rsp_els->vp_index = purex->vp_idx;
6104 	rsp_els->sof_type = EST_SOFI3;
6105 	rsp_els->rx_xchg_address = purex->rx_xchg_addr;
6106 	rsp_els->rx_dsd_count = 0;
6107 	rsp_els->opcode = purex->els_frame_payload[0];
6108 
6109 	rsp_els->d_id[0] = purex->s_id[0];
6110 	rsp_els->d_id[1] = purex->s_id[1];
6111 	rsp_els->d_id[2] = purex->s_id[2];
6112 
6113 	rsp_els->control_flags = cpu_to_le16(EPD_ELS_ACC);
6114 	rsp_els->rx_byte_count = 0;
6115 	rsp_els->tx_byte_count = cpu_to_le32(rsp_payload_length);
6116 
6117 	put_unaligned_le64(rsp_payload_dma, &rsp_els->tx_address);
6118 	rsp_els->tx_len = rsp_els->tx_byte_count;
6119 
6120 	rsp_els->rx_address = 0;
6121 	rsp_els->rx_len = 0;
6122 
6123 	/* Prepare Response Payload */
6124 	rsp_payload->hdr.cmd = cpu_to_be32(0x2 << 24); /* LS_ACC */
6125 	rsp_payload->hdr.len = cpu_to_be32(le32_to_cpu(rsp_els->tx_byte_count) -
6126 					   sizeof(rsp_payload->hdr));
6127 
6128 	/* Link service Request Info Descriptor */
6129 	rsp_payload->ls_req_info_desc.desc_tag = cpu_to_be32(0x1);
6130 	rsp_payload->ls_req_info_desc.desc_len =
6131 	    cpu_to_be32(RDP_DESC_LEN(rsp_payload->ls_req_info_desc));
6132 	rsp_payload->ls_req_info_desc.req_payload_word_0 =
6133 	    cpu_to_be32p((uint32_t *)purex->els_frame_payload);
6134 
6135 	/* Link service Request Info Descriptor 2 */
6136 	rsp_payload->ls_req_info_desc2.desc_tag = cpu_to_be32(0x1);
6137 	rsp_payload->ls_req_info_desc2.desc_len =
6138 	    cpu_to_be32(RDP_DESC_LEN(rsp_payload->ls_req_info_desc2));
6139 	rsp_payload->ls_req_info_desc2.req_payload_word_0 =
6140 	    cpu_to_be32p((uint32_t *)purex->els_frame_payload);
6141 
6142 
6143 	rsp_payload->sfp_diag_desc.desc_tag = cpu_to_be32(0x10000);
6144 	rsp_payload->sfp_diag_desc.desc_len =
6145 		cpu_to_be32(RDP_DESC_LEN(rsp_payload->sfp_diag_desc));
6146 
6147 	if (sfp) {
6148 		/* SFP Flags */
6149 		memset(sfp, 0, SFP_RTDI_LEN);
6150 		rval = qla2x00_read_sfp(vha, sfp_dma, sfp, 0xa0, 0x7, 2, 0);
6151 		if (!rval) {
6152 			/* SFP Flags bits 3-0: Port Tx Laser Type */
6153 			if (sfp[0] & BIT_2 || sfp[1] & (BIT_6|BIT_5))
6154 				sfp_flags |= BIT_0; /* short wave */
6155 			else if (sfp[0] & BIT_1)
6156 				sfp_flags |= BIT_1; /* long wave 1310nm */
6157 			else if (sfp[1] & BIT_4)
6158 				sfp_flags |= BIT_1|BIT_0; /* long wave 1550nm */
6159 		}
6160 
6161 		/* SFP Type */
6162 		memset(sfp, 0, SFP_RTDI_LEN);
6163 		rval = qla2x00_read_sfp(vha, sfp_dma, sfp, 0xa0, 0x0, 1, 0);
6164 		if (!rval) {
6165 			sfp_flags |= BIT_4; /* optical */
6166 			if (sfp[0] == 0x3)
6167 				sfp_flags |= BIT_6; /* sfp+ */
6168 		}
6169 
6170 		rsp_payload->sfp_diag_desc.sfp_flags = cpu_to_be16(sfp_flags);
6171 
6172 		/* SFP Diagnostics */
6173 		memset(sfp, 0, SFP_RTDI_LEN);
6174 		rval = qla2x00_read_sfp(vha, sfp_dma, sfp, 0xa2, 0x60, 10, 0);
6175 		if (!rval) {
6176 			__be16 *trx = (__force __be16 *)sfp; /* already be16 */
6177 			rsp_payload->sfp_diag_desc.temperature = trx[0];
6178 			rsp_payload->sfp_diag_desc.vcc = trx[1];
6179 			rsp_payload->sfp_diag_desc.tx_bias = trx[2];
6180 			rsp_payload->sfp_diag_desc.tx_power = trx[3];
6181 			rsp_payload->sfp_diag_desc.rx_power = trx[4];
6182 		}
6183 	}
6184 
6185 	/* Port Speed Descriptor */
6186 	rsp_payload->port_speed_desc.desc_tag = cpu_to_be32(0x10001);
6187 	rsp_payload->port_speed_desc.desc_len =
6188 	    cpu_to_be32(RDP_DESC_LEN(rsp_payload->port_speed_desc));
6189 	rsp_payload->port_speed_desc.speed_capab = cpu_to_be16(
6190 	    qla25xx_fdmi_port_speed_capability(ha));
6191 	rsp_payload->port_speed_desc.operating_speed = cpu_to_be16(
6192 	    qla25xx_fdmi_port_speed_currently(ha));
6193 
6194 	/* Link Error Status Descriptor */
6195 	rsp_payload->ls_err_desc.desc_tag = cpu_to_be32(0x10002);
6196 	rsp_payload->ls_err_desc.desc_len =
6197 		cpu_to_be32(RDP_DESC_LEN(rsp_payload->ls_err_desc));
6198 
6199 	if (stat) {
6200 		rval = qla24xx_get_isp_stats(vha, stat, stat_dma, 0);
6201 		if (!rval) {
6202 			rsp_payload->ls_err_desc.link_fail_cnt =
6203 			    cpu_to_be32(le32_to_cpu(stat->link_fail_cnt));
6204 			rsp_payload->ls_err_desc.loss_sync_cnt =
6205 			    cpu_to_be32(le32_to_cpu(stat->loss_sync_cnt));
6206 			rsp_payload->ls_err_desc.loss_sig_cnt =
6207 			    cpu_to_be32(le32_to_cpu(stat->loss_sig_cnt));
6208 			rsp_payload->ls_err_desc.prim_seq_err_cnt =
6209 			    cpu_to_be32(le32_to_cpu(stat->prim_seq_err_cnt));
6210 			rsp_payload->ls_err_desc.inval_xmit_word_cnt =
6211 			    cpu_to_be32(le32_to_cpu(stat->inval_xmit_word_cnt));
6212 			rsp_payload->ls_err_desc.inval_crc_cnt =
6213 			    cpu_to_be32(le32_to_cpu(stat->inval_crc_cnt));
6214 			rsp_payload->ls_err_desc.pn_port_phy_type |= BIT_6;
6215 		}
6216 	}
6217 
6218 	/* Portname Descriptor */
6219 	rsp_payload->port_name_diag_desc.desc_tag = cpu_to_be32(0x10003);
6220 	rsp_payload->port_name_diag_desc.desc_len =
6221 	    cpu_to_be32(RDP_DESC_LEN(rsp_payload->port_name_diag_desc));
6222 	memcpy(rsp_payload->port_name_diag_desc.WWNN,
6223 	    vha->node_name,
6224 	    sizeof(rsp_payload->port_name_diag_desc.WWNN));
6225 	memcpy(rsp_payload->port_name_diag_desc.WWPN,
6226 	    vha->port_name,
6227 	    sizeof(rsp_payload->port_name_diag_desc.WWPN));
6228 
6229 	/* F-Port Portname Descriptor */
6230 	rsp_payload->port_name_direct_desc.desc_tag = cpu_to_be32(0x10003);
6231 	rsp_payload->port_name_direct_desc.desc_len =
6232 	    cpu_to_be32(RDP_DESC_LEN(rsp_payload->port_name_direct_desc));
6233 	memcpy(rsp_payload->port_name_direct_desc.WWNN,
6234 	    vha->fabric_node_name,
6235 	    sizeof(rsp_payload->port_name_direct_desc.WWNN));
6236 	memcpy(rsp_payload->port_name_direct_desc.WWPN,
6237 	    vha->fabric_port_name,
6238 	    sizeof(rsp_payload->port_name_direct_desc.WWPN));
6239 
6240 	/* Bufer Credit Descriptor */
6241 	rsp_payload->buffer_credit_desc.desc_tag = cpu_to_be32(0x10006);
6242 	rsp_payload->buffer_credit_desc.desc_len =
6243 		cpu_to_be32(RDP_DESC_LEN(rsp_payload->buffer_credit_desc));
6244 	rsp_payload->buffer_credit_desc.fcport_b2b = 0;
6245 	rsp_payload->buffer_credit_desc.attached_fcport_b2b = cpu_to_be32(0);
6246 	rsp_payload->buffer_credit_desc.fcport_rtt = cpu_to_be32(0);
6247 
6248 	if (ha->flags.plogi_template_valid) {
6249 		uint32_t tmp =
6250 		be16_to_cpu(ha->plogi_els_payld.fl_csp.sp_bb_cred);
6251 		rsp_payload->buffer_credit_desc.fcport_b2b = cpu_to_be32(tmp);
6252 	}
6253 
6254 	if (rsp_payload_length < sizeof(*rsp_payload))
6255 		goto send;
6256 
6257 	/* Optical Element Descriptor, Temperature */
6258 	rsp_payload->optical_elmt_desc[0].desc_tag = cpu_to_be32(0x10007);
6259 	rsp_payload->optical_elmt_desc[0].desc_len =
6260 		cpu_to_be32(RDP_DESC_LEN(*rsp_payload->optical_elmt_desc));
6261 	/* Optical Element Descriptor, Voltage */
6262 	rsp_payload->optical_elmt_desc[1].desc_tag = cpu_to_be32(0x10007);
6263 	rsp_payload->optical_elmt_desc[1].desc_len =
6264 		cpu_to_be32(RDP_DESC_LEN(*rsp_payload->optical_elmt_desc));
6265 	/* Optical Element Descriptor, Tx Bias Current */
6266 	rsp_payload->optical_elmt_desc[2].desc_tag = cpu_to_be32(0x10007);
6267 	rsp_payload->optical_elmt_desc[2].desc_len =
6268 		cpu_to_be32(RDP_DESC_LEN(*rsp_payload->optical_elmt_desc));
6269 	/* Optical Element Descriptor, Tx Power */
6270 	rsp_payload->optical_elmt_desc[3].desc_tag = cpu_to_be32(0x10007);
6271 	rsp_payload->optical_elmt_desc[3].desc_len =
6272 		cpu_to_be32(RDP_DESC_LEN(*rsp_payload->optical_elmt_desc));
6273 	/* Optical Element Descriptor, Rx Power */
6274 	rsp_payload->optical_elmt_desc[4].desc_tag = cpu_to_be32(0x10007);
6275 	rsp_payload->optical_elmt_desc[4].desc_len =
6276 		cpu_to_be32(RDP_DESC_LEN(*rsp_payload->optical_elmt_desc));
6277 
6278 	if (sfp) {
6279 		memset(sfp, 0, SFP_RTDI_LEN);
6280 		rval = qla2x00_read_sfp(vha, sfp_dma, sfp, 0xa2, 0, 64, 0);
6281 		if (!rval) {
6282 			__be16 *trx = (__force __be16 *)sfp; /* already be16 */
6283 
6284 			/* Optical Element Descriptor, Temperature */
6285 			rsp_payload->optical_elmt_desc[0].high_alarm = trx[0];
6286 			rsp_payload->optical_elmt_desc[0].low_alarm = trx[1];
6287 			rsp_payload->optical_elmt_desc[0].high_warn = trx[2];
6288 			rsp_payload->optical_elmt_desc[0].low_warn = trx[3];
6289 			rsp_payload->optical_elmt_desc[0].element_flags =
6290 			    cpu_to_be32(1 << 28);
6291 
6292 			/* Optical Element Descriptor, Voltage */
6293 			rsp_payload->optical_elmt_desc[1].high_alarm = trx[4];
6294 			rsp_payload->optical_elmt_desc[1].low_alarm = trx[5];
6295 			rsp_payload->optical_elmt_desc[1].high_warn = trx[6];
6296 			rsp_payload->optical_elmt_desc[1].low_warn = trx[7];
6297 			rsp_payload->optical_elmt_desc[1].element_flags =
6298 			    cpu_to_be32(2 << 28);
6299 
6300 			/* Optical Element Descriptor, Tx Bias Current */
6301 			rsp_payload->optical_elmt_desc[2].high_alarm = trx[8];
6302 			rsp_payload->optical_elmt_desc[2].low_alarm = trx[9];
6303 			rsp_payload->optical_elmt_desc[2].high_warn = trx[10];
6304 			rsp_payload->optical_elmt_desc[2].low_warn = trx[11];
6305 			rsp_payload->optical_elmt_desc[2].element_flags =
6306 			    cpu_to_be32(3 << 28);
6307 
6308 			/* Optical Element Descriptor, Tx Power */
6309 			rsp_payload->optical_elmt_desc[3].high_alarm = trx[12];
6310 			rsp_payload->optical_elmt_desc[3].low_alarm = trx[13];
6311 			rsp_payload->optical_elmt_desc[3].high_warn = trx[14];
6312 			rsp_payload->optical_elmt_desc[3].low_warn = trx[15];
6313 			rsp_payload->optical_elmt_desc[3].element_flags =
6314 			    cpu_to_be32(4 << 28);
6315 
6316 			/* Optical Element Descriptor, Rx Power */
6317 			rsp_payload->optical_elmt_desc[4].high_alarm = trx[16];
6318 			rsp_payload->optical_elmt_desc[4].low_alarm = trx[17];
6319 			rsp_payload->optical_elmt_desc[4].high_warn = trx[18];
6320 			rsp_payload->optical_elmt_desc[4].low_warn = trx[19];
6321 			rsp_payload->optical_elmt_desc[4].element_flags =
6322 			    cpu_to_be32(5 << 28);
6323 		}
6324 
6325 		memset(sfp, 0, SFP_RTDI_LEN);
6326 		rval = qla2x00_read_sfp(vha, sfp_dma, sfp, 0xa2, 112, 64, 0);
6327 		if (!rval) {
6328 			/* Temperature high/low alarm/warning */
6329 			rsp_payload->optical_elmt_desc[0].element_flags |=
6330 			    cpu_to_be32(
6331 				(sfp[0] >> 7 & 1) << 3 |
6332 				(sfp[0] >> 6 & 1) << 2 |
6333 				(sfp[4] >> 7 & 1) << 1 |
6334 				(sfp[4] >> 6 & 1) << 0);
6335 
6336 			/* Voltage high/low alarm/warning */
6337 			rsp_payload->optical_elmt_desc[1].element_flags |=
6338 			    cpu_to_be32(
6339 				(sfp[0] >> 5 & 1) << 3 |
6340 				(sfp[0] >> 4 & 1) << 2 |
6341 				(sfp[4] >> 5 & 1) << 1 |
6342 				(sfp[4] >> 4 & 1) << 0);
6343 
6344 			/* Tx Bias Current high/low alarm/warning */
6345 			rsp_payload->optical_elmt_desc[2].element_flags |=
6346 			    cpu_to_be32(
6347 				(sfp[0] >> 3 & 1) << 3 |
6348 				(sfp[0] >> 2 & 1) << 2 |
6349 				(sfp[4] >> 3 & 1) << 1 |
6350 				(sfp[4] >> 2 & 1) << 0);
6351 
6352 			/* Tx Power high/low alarm/warning */
6353 			rsp_payload->optical_elmt_desc[3].element_flags |=
6354 			    cpu_to_be32(
6355 				(sfp[0] >> 1 & 1) << 3 |
6356 				(sfp[0] >> 0 & 1) << 2 |
6357 				(sfp[4] >> 1 & 1) << 1 |
6358 				(sfp[4] >> 0 & 1) << 0);
6359 
6360 			/* Rx Power high/low alarm/warning */
6361 			rsp_payload->optical_elmt_desc[4].element_flags |=
6362 			    cpu_to_be32(
6363 				(sfp[1] >> 7 & 1) << 3 |
6364 				(sfp[1] >> 6 & 1) << 2 |
6365 				(sfp[5] >> 7 & 1) << 1 |
6366 				(sfp[5] >> 6 & 1) << 0);
6367 		}
6368 	}
6369 
6370 	/* Optical Product Data Descriptor */
6371 	rsp_payload->optical_prod_desc.desc_tag = cpu_to_be32(0x10008);
6372 	rsp_payload->optical_prod_desc.desc_len =
6373 		cpu_to_be32(RDP_DESC_LEN(rsp_payload->optical_prod_desc));
6374 
6375 	if (sfp) {
6376 		memset(sfp, 0, SFP_RTDI_LEN);
6377 		rval = qla2x00_read_sfp(vha, sfp_dma, sfp, 0xa0, 20, 64, 0);
6378 		if (!rval) {
6379 			memcpy(rsp_payload->optical_prod_desc.vendor_name,
6380 			    sfp + 0,
6381 			    sizeof(rsp_payload->optical_prod_desc.vendor_name));
6382 			memcpy(rsp_payload->optical_prod_desc.part_number,
6383 			    sfp + 20,
6384 			    sizeof(rsp_payload->optical_prod_desc.part_number));
6385 			memcpy(rsp_payload->optical_prod_desc.revision,
6386 			    sfp + 36,
6387 			    sizeof(rsp_payload->optical_prod_desc.revision));
6388 			memcpy(rsp_payload->optical_prod_desc.serial_number,
6389 			    sfp + 48,
6390 			    sizeof(rsp_payload->optical_prod_desc.serial_number));
6391 		}
6392 
6393 		memset(sfp, 0, SFP_RTDI_LEN);
6394 		rval = qla2x00_read_sfp(vha, sfp_dma, sfp, 0xa0, 84, 8, 0);
6395 		if (!rval) {
6396 			memcpy(rsp_payload->optical_prod_desc.date,
6397 			    sfp + 0,
6398 			    sizeof(rsp_payload->optical_prod_desc.date));
6399 		}
6400 	}
6401 
6402 send:
6403 	ql_dbg(ql_dbg_init, vha, 0x0183,
6404 	    "Sending ELS Response to RDP Request...\n");
6405 	ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x0184,
6406 	    "-------- ELS RSP -------\n");
6407 	ql_dump_buffer(ql_dbg_init + ql_dbg_verbose, vha, 0x0185,
6408 	    rsp_els, sizeof(*rsp_els));
6409 	ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x0186,
6410 	    "-------- ELS RSP PAYLOAD -------\n");
6411 	ql_dump_buffer(ql_dbg_init + ql_dbg_verbose, vha, 0x0187,
6412 	    rsp_payload, rsp_payload_length);
6413 
6414 	rval = qla2x00_issue_iocb(vha, rsp_els, rsp_els_dma, 0);
6415 
6416 	if (rval) {
6417 		ql_log(ql_log_warn, vha, 0x0188,
6418 		    "%s: iocb failed to execute -> %x\n", __func__, rval);
6419 	} else if (rsp_els->comp_status) {
6420 		ql_log(ql_log_warn, vha, 0x0189,
6421 		    "%s: iocb failed to complete -> completion=%#x subcode=(%#x,%#x)\n",
6422 		    __func__, rsp_els->comp_status,
6423 		    rsp_els->error_subcode_1, rsp_els->error_subcode_2);
6424 	} else {
6425 		ql_dbg(ql_dbg_init, vha, 0x018a, "%s: done.\n", __func__);
6426 	}
6427 
6428 dealloc:
6429 	if (stat)
6430 		dma_free_coherent(&ha->pdev->dev, sizeof(*stat),
6431 		    stat, stat_dma);
6432 	if (sfp)
6433 		dma_free_coherent(&ha->pdev->dev, SFP_RTDI_LEN,
6434 		    sfp, sfp_dma);
6435 	if (rsp_payload)
6436 		dma_free_coherent(&ha->pdev->dev, sizeof(*rsp_payload),
6437 		    rsp_payload, rsp_payload_dma);
6438 	if (rsp_els)
6439 		dma_free_coherent(&ha->pdev->dev, sizeof(*rsp_els),
6440 		    rsp_els, rsp_els_dma);
6441 }
6442 
6443 void
6444 qla24xx_free_purex_item(struct purex_item *item)
6445 {
6446 	if (item == &item->vha->default_item)
6447 		memset(&item->vha->default_item, 0, sizeof(struct purex_item));
6448 	else
6449 		kfree(item);
6450 }
6451 
6452 void qla24xx_process_purex_list(struct purex_list *list)
6453 {
6454 	struct list_head head = LIST_HEAD_INIT(head);
6455 	struct purex_item *item, *next;
6456 	ulong flags;
6457 
6458 	spin_lock_irqsave(&list->lock, flags);
6459 	list_splice_init(&list->head, &head);
6460 	spin_unlock_irqrestore(&list->lock, flags);
6461 
6462 	list_for_each_entry_safe(item, next, &head, list) {
6463 		list_del(&item->list);
6464 		item->process_item(item->vha, item);
6465 		qla24xx_free_purex_item(item);
6466 	}
6467 }
6468 
6469 /*
6470  * Context: task, can sleep
6471  */
6472 void
6473 qla83xx_idc_unlock(scsi_qla_host_t *base_vha, uint16_t requester_id)
6474 {
6475 #if 0
6476 	uint16_t options = (requester_id << 15) | BIT_7;
6477 #endif
6478 	uint16_t retry;
6479 	uint32_t data;
6480 	struct qla_hw_data *ha = base_vha->hw;
6481 
6482 	might_sleep();
6483 
6484 	/* IDC-unlock implementation using driver-unlock/lock-id
6485 	 * remote registers
6486 	 */
6487 	retry = 0;
6488 retry_unlock:
6489 	if (qla83xx_rd_reg(base_vha, QLA83XX_DRIVER_LOCKID, &data)
6490 	    == QLA_SUCCESS) {
6491 		if (data == ha->portnum) {
6492 			qla83xx_rd_reg(base_vha, QLA83XX_DRIVER_UNLOCK, &data);
6493 			/* Clearing lock-id by setting 0xff */
6494 			qla83xx_wr_reg(base_vha, QLA83XX_DRIVER_LOCKID, 0xff);
6495 		} else if (retry < 10) {
6496 			/* SV: XXX: IDC unlock retrying needed here? */
6497 
6498 			/* Retry for IDC-unlock */
6499 			msleep(QLA83XX_WAIT_LOGIC_MS);
6500 			retry++;
6501 			ql_dbg(ql_dbg_p3p, base_vha, 0xb064,
6502 			    "Failed to release IDC lock, retrying=%d\n", retry);
6503 			goto retry_unlock;
6504 		}
6505 	} else if (retry < 10) {
6506 		/* Retry for IDC-unlock */
6507 		msleep(QLA83XX_WAIT_LOGIC_MS);
6508 		retry++;
6509 		ql_dbg(ql_dbg_p3p, base_vha, 0xb065,
6510 		    "Failed to read drv-lockid, retrying=%d\n", retry);
6511 		goto retry_unlock;
6512 	}
6513 
6514 	return;
6515 
6516 #if 0
6517 	/* XXX: IDC-unlock implementation using access-control mbx */
6518 	retry = 0;
6519 retry_unlock2:
6520 	if (qla83xx_access_control(base_vha, options, 0, 0, NULL)) {
6521 		if (retry < 10) {
6522 			/* Retry for IDC-unlock */
6523 			msleep(QLA83XX_WAIT_LOGIC_MS);
6524 			retry++;
6525 			ql_dbg(ql_dbg_p3p, base_vha, 0xb066,
6526 			    "Failed to release IDC lock, retrying=%d\n", retry);
6527 			goto retry_unlock2;
6528 		}
6529 	}
6530 
6531 	return;
6532 #endif
6533 }
6534 
6535 int
6536 __qla83xx_set_drv_presence(scsi_qla_host_t *vha)
6537 {
6538 	int rval = QLA_SUCCESS;
6539 	struct qla_hw_data *ha = vha->hw;
6540 	uint32_t drv_presence;
6541 
6542 	rval = qla83xx_rd_reg(vha, QLA83XX_IDC_DRV_PRESENCE, &drv_presence);
6543 	if (rval == QLA_SUCCESS) {
6544 		drv_presence |= (1 << ha->portnum);
6545 		rval = qla83xx_wr_reg(vha, QLA83XX_IDC_DRV_PRESENCE,
6546 		    drv_presence);
6547 	}
6548 
6549 	return rval;
6550 }
6551 
6552 int
6553 qla83xx_set_drv_presence(scsi_qla_host_t *vha)
6554 {
6555 	int rval = QLA_SUCCESS;
6556 
6557 	qla83xx_idc_lock(vha, 0);
6558 	rval = __qla83xx_set_drv_presence(vha);
6559 	qla83xx_idc_unlock(vha, 0);
6560 
6561 	return rval;
6562 }
6563 
6564 int
6565 __qla83xx_clear_drv_presence(scsi_qla_host_t *vha)
6566 {
6567 	int rval = QLA_SUCCESS;
6568 	struct qla_hw_data *ha = vha->hw;
6569 	uint32_t drv_presence;
6570 
6571 	rval = qla83xx_rd_reg(vha, QLA83XX_IDC_DRV_PRESENCE, &drv_presence);
6572 	if (rval == QLA_SUCCESS) {
6573 		drv_presence &= ~(1 << ha->portnum);
6574 		rval = qla83xx_wr_reg(vha, QLA83XX_IDC_DRV_PRESENCE,
6575 		    drv_presence);
6576 	}
6577 
6578 	return rval;
6579 }
6580 
6581 int
6582 qla83xx_clear_drv_presence(scsi_qla_host_t *vha)
6583 {
6584 	int rval = QLA_SUCCESS;
6585 
6586 	qla83xx_idc_lock(vha, 0);
6587 	rval = __qla83xx_clear_drv_presence(vha);
6588 	qla83xx_idc_unlock(vha, 0);
6589 
6590 	return rval;
6591 }
6592 
6593 static void
6594 qla83xx_need_reset_handler(scsi_qla_host_t *vha)
6595 {
6596 	struct qla_hw_data *ha = vha->hw;
6597 	uint32_t drv_ack, drv_presence;
6598 	unsigned long ack_timeout;
6599 
6600 	/* Wait for IDC ACK from all functions (DRV-ACK == DRV-PRESENCE) */
6601 	ack_timeout = jiffies + (ha->fcoe_reset_timeout * HZ);
6602 	while (1) {
6603 		qla83xx_rd_reg(vha, QLA83XX_IDC_DRIVER_ACK, &drv_ack);
6604 		qla83xx_rd_reg(vha, QLA83XX_IDC_DRV_PRESENCE, &drv_presence);
6605 		if ((drv_ack & drv_presence) == drv_presence)
6606 			break;
6607 
6608 		if (time_after_eq(jiffies, ack_timeout)) {
6609 			ql_log(ql_log_warn, vha, 0xb067,
6610 			    "RESET ACK TIMEOUT! drv_presence=0x%x "
6611 			    "drv_ack=0x%x\n", drv_presence, drv_ack);
6612 			/*
6613 			 * The function(s) which did not ack in time are forced
6614 			 * to withdraw any further participation in the IDC
6615 			 * reset.
6616 			 */
6617 			if (drv_ack != drv_presence)
6618 				qla83xx_wr_reg(vha, QLA83XX_IDC_DRV_PRESENCE,
6619 				    drv_ack);
6620 			break;
6621 		}
6622 
6623 		qla83xx_idc_unlock(vha, 0);
6624 		msleep(1000);
6625 		qla83xx_idc_lock(vha, 0);
6626 	}
6627 
6628 	qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE, QLA8XXX_DEV_COLD);
6629 	ql_log(ql_log_info, vha, 0xb068, "HW State: COLD/RE-INIT.\n");
6630 }
6631 
6632 static int
6633 qla83xx_device_bootstrap(scsi_qla_host_t *vha)
6634 {
6635 	int rval = QLA_SUCCESS;
6636 	uint32_t idc_control;
6637 
6638 	qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE, QLA8XXX_DEV_INITIALIZING);
6639 	ql_log(ql_log_info, vha, 0xb069, "HW State: INITIALIZING.\n");
6640 
6641 	/* Clearing IDC-Control Graceful-Reset Bit before resetting f/w */
6642 	__qla83xx_get_idc_control(vha, &idc_control);
6643 	idc_control &= ~QLA83XX_IDC_GRACEFUL_RESET;
6644 	__qla83xx_set_idc_control(vha, 0);
6645 
6646 	qla83xx_idc_unlock(vha, 0);
6647 	rval = qla83xx_restart_nic_firmware(vha);
6648 	qla83xx_idc_lock(vha, 0);
6649 
6650 	if (rval != QLA_SUCCESS) {
6651 		ql_log(ql_log_fatal, vha, 0xb06a,
6652 		    "Failed to restart NIC f/w.\n");
6653 		qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE, QLA8XXX_DEV_FAILED);
6654 		ql_log(ql_log_info, vha, 0xb06b, "HW State: FAILED.\n");
6655 	} else {
6656 		ql_dbg(ql_dbg_p3p, vha, 0xb06c,
6657 		    "Success in restarting nic f/w.\n");
6658 		qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE, QLA8XXX_DEV_READY);
6659 		ql_log(ql_log_info, vha, 0xb06d, "HW State: READY.\n");
6660 	}
6661 
6662 	return rval;
6663 }
6664 
6665 /* Assumes idc_lock always held on entry */
6666 int
6667 qla83xx_idc_state_handler(scsi_qla_host_t *base_vha)
6668 {
6669 	struct qla_hw_data *ha = base_vha->hw;
6670 	int rval = QLA_SUCCESS;
6671 	unsigned long dev_init_timeout;
6672 	uint32_t dev_state;
6673 
6674 	/* Wait for MAX-INIT-TIMEOUT for the device to go ready */
6675 	dev_init_timeout = jiffies + (ha->fcoe_dev_init_timeout * HZ);
6676 
6677 	while (1) {
6678 
6679 		if (time_after_eq(jiffies, dev_init_timeout)) {
6680 			ql_log(ql_log_warn, base_vha, 0xb06e,
6681 			    "Initialization TIMEOUT!\n");
6682 			/* Init timeout. Disable further NIC Core
6683 			 * communication.
6684 			 */
6685 			qla83xx_wr_reg(base_vha, QLA83XX_IDC_DEV_STATE,
6686 				QLA8XXX_DEV_FAILED);
6687 			ql_log(ql_log_info, base_vha, 0xb06f,
6688 			    "HW State: FAILED.\n");
6689 		}
6690 
6691 		qla83xx_rd_reg(base_vha, QLA83XX_IDC_DEV_STATE, &dev_state);
6692 		switch (dev_state) {
6693 		case QLA8XXX_DEV_READY:
6694 			if (ha->flags.nic_core_reset_owner)
6695 				qla83xx_idc_audit(base_vha,
6696 				    IDC_AUDIT_COMPLETION);
6697 			ha->flags.nic_core_reset_owner = 0;
6698 			ql_dbg(ql_dbg_p3p, base_vha, 0xb070,
6699 			    "Reset_owner reset by 0x%x.\n",
6700 			    ha->portnum);
6701 			goto exit;
6702 		case QLA8XXX_DEV_COLD:
6703 			if (ha->flags.nic_core_reset_owner)
6704 				rval = qla83xx_device_bootstrap(base_vha);
6705 			else {
6706 			/* Wait for AEN to change device-state */
6707 				qla83xx_idc_unlock(base_vha, 0);
6708 				msleep(1000);
6709 				qla83xx_idc_lock(base_vha, 0);
6710 			}
6711 			break;
6712 		case QLA8XXX_DEV_INITIALIZING:
6713 			/* Wait for AEN to change device-state */
6714 			qla83xx_idc_unlock(base_vha, 0);
6715 			msleep(1000);
6716 			qla83xx_idc_lock(base_vha, 0);
6717 			break;
6718 		case QLA8XXX_DEV_NEED_RESET:
6719 			if (!ql2xdontresethba && ha->flags.nic_core_reset_owner)
6720 				qla83xx_need_reset_handler(base_vha);
6721 			else {
6722 				/* Wait for AEN to change device-state */
6723 				qla83xx_idc_unlock(base_vha, 0);
6724 				msleep(1000);
6725 				qla83xx_idc_lock(base_vha, 0);
6726 			}
6727 			/* reset timeout value after need reset handler */
6728 			dev_init_timeout = jiffies +
6729 			    (ha->fcoe_dev_init_timeout * HZ);
6730 			break;
6731 		case QLA8XXX_DEV_NEED_QUIESCENT:
6732 			/* XXX: DEBUG for now */
6733 			qla83xx_idc_unlock(base_vha, 0);
6734 			msleep(1000);
6735 			qla83xx_idc_lock(base_vha, 0);
6736 			break;
6737 		case QLA8XXX_DEV_QUIESCENT:
6738 			/* XXX: DEBUG for now */
6739 			if (ha->flags.quiesce_owner)
6740 				goto exit;
6741 
6742 			qla83xx_idc_unlock(base_vha, 0);
6743 			msleep(1000);
6744 			qla83xx_idc_lock(base_vha, 0);
6745 			dev_init_timeout = jiffies +
6746 			    (ha->fcoe_dev_init_timeout * HZ);
6747 			break;
6748 		case QLA8XXX_DEV_FAILED:
6749 			if (ha->flags.nic_core_reset_owner)
6750 				qla83xx_idc_audit(base_vha,
6751 				    IDC_AUDIT_COMPLETION);
6752 			ha->flags.nic_core_reset_owner = 0;
6753 			__qla83xx_clear_drv_presence(base_vha);
6754 			qla83xx_idc_unlock(base_vha, 0);
6755 			qla8xxx_dev_failed_handler(base_vha);
6756 			rval = QLA_FUNCTION_FAILED;
6757 			qla83xx_idc_lock(base_vha, 0);
6758 			goto exit;
6759 		case QLA8XXX_BAD_VALUE:
6760 			qla83xx_idc_unlock(base_vha, 0);
6761 			msleep(1000);
6762 			qla83xx_idc_lock(base_vha, 0);
6763 			break;
6764 		default:
6765 			ql_log(ql_log_warn, base_vha, 0xb071,
6766 			    "Unknown Device State: %x.\n", dev_state);
6767 			qla83xx_idc_unlock(base_vha, 0);
6768 			qla8xxx_dev_failed_handler(base_vha);
6769 			rval = QLA_FUNCTION_FAILED;
6770 			qla83xx_idc_lock(base_vha, 0);
6771 			goto exit;
6772 		}
6773 	}
6774 
6775 exit:
6776 	return rval;
6777 }
6778 
6779 void
6780 qla2x00_disable_board_on_pci_error(struct work_struct *work)
6781 {
6782 	struct qla_hw_data *ha = container_of(work, struct qla_hw_data,
6783 	    board_disable);
6784 	struct pci_dev *pdev = ha->pdev;
6785 	scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
6786 
6787 	ql_log(ql_log_warn, base_vha, 0x015b,
6788 	    "Disabling adapter.\n");
6789 
6790 	if (!atomic_read(&pdev->enable_cnt)) {
6791 		ql_log(ql_log_info, base_vha, 0xfffc,
6792 		    "PCI device disabled, no action req for PCI error=%lx\n",
6793 		    base_vha->pci_flags);
6794 		return;
6795 	}
6796 
6797 	/*
6798 	 * if UNLOADING flag is already set, then continue unload,
6799 	 * where it was set first.
6800 	 */
6801 	if (test_and_set_bit(UNLOADING, &base_vha->dpc_flags))
6802 		return;
6803 
6804 	qla2x00_wait_for_sess_deletion(base_vha);
6805 
6806 	qla2x00_delete_all_vps(ha, base_vha);
6807 
6808 	qla2x00_abort_all_cmds(base_vha, DID_NO_CONNECT << 16);
6809 
6810 	qla2x00_dfs_remove(base_vha);
6811 
6812 	qla84xx_put_chip(base_vha);
6813 
6814 	if (base_vha->timer_active)
6815 		qla2x00_stop_timer(base_vha);
6816 
6817 	base_vha->flags.online = 0;
6818 
6819 	qla2x00_destroy_deferred_work(ha);
6820 
6821 	/*
6822 	 * Do not try to stop beacon blink as it will issue a mailbox
6823 	 * command.
6824 	 */
6825 	qla2x00_free_sysfs_attr(base_vha, false);
6826 
6827 	fc_remove_host(base_vha->host);
6828 
6829 	scsi_remove_host(base_vha->host);
6830 
6831 	base_vha->flags.init_done = 0;
6832 	qla25xx_delete_queues(base_vha);
6833 	qla2x00_free_fcports(base_vha);
6834 	qla2x00_free_irqs(base_vha);
6835 	qla2x00_mem_free(ha);
6836 	qla82xx_md_free(base_vha);
6837 	qla2x00_free_queues(ha);
6838 
6839 	qla2x00_unmap_iobases(ha);
6840 
6841 	pci_release_selected_regions(ha->pdev, ha->bars);
6842 	pci_disable_device(pdev);
6843 
6844 	/*
6845 	 * Let qla2x00_remove_one cleanup qla_hw_data on device removal.
6846 	 */
6847 }
6848 
6849 /**************************************************************************
6850 * qla2x00_do_dpc
6851 *   This kernel thread is a task that is schedule by the interrupt handler
6852 *   to perform the background processing for interrupts.
6853 *
6854 * Notes:
6855 * This task always run in the context of a kernel thread.  It
6856 * is kick-off by the driver's detect code and starts up
6857 * up one per adapter. It immediately goes to sleep and waits for
6858 * some fibre event.  When either the interrupt handler or
6859 * the timer routine detects a event it will one of the task
6860 * bits then wake us up.
6861 **************************************************************************/
6862 static int
6863 qla2x00_do_dpc(void *data)
6864 {
6865 	scsi_qla_host_t *base_vha;
6866 	struct qla_hw_data *ha;
6867 	uint32_t online;
6868 	struct qla_qpair *qpair;
6869 
6870 	ha = (struct qla_hw_data *)data;
6871 	base_vha = pci_get_drvdata(ha->pdev);
6872 
6873 	set_user_nice(current, MIN_NICE);
6874 
6875 	set_current_state(TASK_INTERRUPTIBLE);
6876 	while (!kthread_should_stop()) {
6877 		ql_dbg(ql_dbg_dpc, base_vha, 0x4000,
6878 		    "DPC handler sleeping.\n");
6879 
6880 		schedule();
6881 
6882 		if (test_and_clear_bit(DO_EEH_RECOVERY, &base_vha->dpc_flags))
6883 			qla_pci_set_eeh_busy(base_vha);
6884 
6885 		if (!base_vha->flags.init_done || ha->flags.mbox_busy)
6886 			goto end_loop;
6887 
6888 		if (ha->flags.eeh_busy) {
6889 			ql_dbg(ql_dbg_dpc, base_vha, 0x4003,
6890 			    "eeh_busy=%d.\n", ha->flags.eeh_busy);
6891 			goto end_loop;
6892 		}
6893 
6894 		ha->dpc_active = 1;
6895 
6896 		ql_dbg(ql_dbg_dpc + ql_dbg_verbose, base_vha, 0x4001,
6897 		    "DPC handler waking up, dpc_flags=0x%lx.\n",
6898 		    base_vha->dpc_flags);
6899 
6900 		if (test_bit(UNLOADING, &base_vha->dpc_flags))
6901 			break;
6902 
6903 		if (IS_P3P_TYPE(ha)) {
6904 			if (IS_QLA8044(ha)) {
6905 				if (test_and_clear_bit(ISP_UNRECOVERABLE,
6906 					&base_vha->dpc_flags)) {
6907 					qla8044_idc_lock(ha);
6908 					qla8044_wr_direct(base_vha,
6909 						QLA8044_CRB_DEV_STATE_INDEX,
6910 						QLA8XXX_DEV_FAILED);
6911 					qla8044_idc_unlock(ha);
6912 					ql_log(ql_log_info, base_vha, 0x4004,
6913 						"HW State: FAILED.\n");
6914 					qla8044_device_state_handler(base_vha);
6915 					continue;
6916 				}
6917 
6918 			} else {
6919 				if (test_and_clear_bit(ISP_UNRECOVERABLE,
6920 					&base_vha->dpc_flags)) {
6921 					qla82xx_idc_lock(ha);
6922 					qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
6923 						QLA8XXX_DEV_FAILED);
6924 					qla82xx_idc_unlock(ha);
6925 					ql_log(ql_log_info, base_vha, 0x0151,
6926 						"HW State: FAILED.\n");
6927 					qla82xx_device_state_handler(base_vha);
6928 					continue;
6929 				}
6930 			}
6931 
6932 			if (test_and_clear_bit(FCOE_CTX_RESET_NEEDED,
6933 				&base_vha->dpc_flags)) {
6934 
6935 				ql_dbg(ql_dbg_dpc, base_vha, 0x4005,
6936 				    "FCoE context reset scheduled.\n");
6937 				if (!(test_and_set_bit(ABORT_ISP_ACTIVE,
6938 					&base_vha->dpc_flags))) {
6939 					if (qla82xx_fcoe_ctx_reset(base_vha)) {
6940 						/* FCoE-ctx reset failed.
6941 						 * Escalate to chip-reset
6942 						 */
6943 						set_bit(ISP_ABORT_NEEDED,
6944 							&base_vha->dpc_flags);
6945 					}
6946 					clear_bit(ABORT_ISP_ACTIVE,
6947 						&base_vha->dpc_flags);
6948 				}
6949 
6950 				ql_dbg(ql_dbg_dpc, base_vha, 0x4006,
6951 				    "FCoE context reset end.\n");
6952 			}
6953 		} else if (IS_QLAFX00(ha)) {
6954 			if (test_and_clear_bit(ISP_UNRECOVERABLE,
6955 				&base_vha->dpc_flags)) {
6956 				ql_dbg(ql_dbg_dpc, base_vha, 0x4020,
6957 				    "Firmware Reset Recovery\n");
6958 				if (qlafx00_reset_initialize(base_vha)) {
6959 					/* Failed. Abort isp later. */
6960 					if (!test_bit(UNLOADING,
6961 					    &base_vha->dpc_flags)) {
6962 						set_bit(ISP_UNRECOVERABLE,
6963 						    &base_vha->dpc_flags);
6964 						ql_dbg(ql_dbg_dpc, base_vha,
6965 						    0x4021,
6966 						    "Reset Recovery Failed\n");
6967 					}
6968 				}
6969 			}
6970 
6971 			if (test_and_clear_bit(FX00_TARGET_SCAN,
6972 				&base_vha->dpc_flags)) {
6973 				ql_dbg(ql_dbg_dpc, base_vha, 0x4022,
6974 				    "ISPFx00 Target Scan scheduled\n");
6975 				if (qlafx00_rescan_isp(base_vha)) {
6976 					if (!test_bit(UNLOADING,
6977 					    &base_vha->dpc_flags))
6978 						set_bit(ISP_UNRECOVERABLE,
6979 						    &base_vha->dpc_flags);
6980 					ql_dbg(ql_dbg_dpc, base_vha, 0x401e,
6981 					    "ISPFx00 Target Scan Failed\n");
6982 				}
6983 				ql_dbg(ql_dbg_dpc, base_vha, 0x401f,
6984 				    "ISPFx00 Target Scan End\n");
6985 			}
6986 			if (test_and_clear_bit(FX00_HOST_INFO_RESEND,
6987 				&base_vha->dpc_flags)) {
6988 				ql_dbg(ql_dbg_dpc, base_vha, 0x4023,
6989 				    "ISPFx00 Host Info resend scheduled\n");
6990 				qlafx00_fx_disc(base_vha,
6991 				    &base_vha->hw->mr.fcport,
6992 				    FXDISC_REG_HOST_INFO);
6993 			}
6994 		}
6995 
6996 		if (test_and_clear_bit(DETECT_SFP_CHANGE,
6997 		    &base_vha->dpc_flags)) {
6998 			/* Semantic:
6999 			 *  - NO-OP -- await next ISP-ABORT. Preferred method
7000 			 *             to minimize disruptions that will occur
7001 			 *             when a forced chip-reset occurs.
7002 			 *  - Force -- ISP-ABORT scheduled.
7003 			 */
7004 			/* set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags); */
7005 		}
7006 
7007 		if (test_and_clear_bit
7008 		    (ISP_ABORT_NEEDED, &base_vha->dpc_flags) &&
7009 		    !test_bit(UNLOADING, &base_vha->dpc_flags)) {
7010 			bool do_reset = true;
7011 
7012 			switch (base_vha->qlini_mode) {
7013 			case QLA2XXX_INI_MODE_ENABLED:
7014 				break;
7015 			case QLA2XXX_INI_MODE_DISABLED:
7016 				if (!qla_tgt_mode_enabled(base_vha) &&
7017 				    !ha->flags.fw_started)
7018 					do_reset = false;
7019 				break;
7020 			case QLA2XXX_INI_MODE_DUAL:
7021 				if (!qla_dual_mode_enabled(base_vha) &&
7022 				    !ha->flags.fw_started)
7023 					do_reset = false;
7024 				break;
7025 			default:
7026 				break;
7027 			}
7028 
7029 			if (do_reset && !(test_and_set_bit(ABORT_ISP_ACTIVE,
7030 			    &base_vha->dpc_flags))) {
7031 				base_vha->flags.online = 1;
7032 				ql_dbg(ql_dbg_dpc, base_vha, 0x4007,
7033 				    "ISP abort scheduled.\n");
7034 				if (ha->isp_ops->abort_isp(base_vha)) {
7035 					/* failed. retry later */
7036 					set_bit(ISP_ABORT_NEEDED,
7037 					    &base_vha->dpc_flags);
7038 				}
7039 				clear_bit(ABORT_ISP_ACTIVE,
7040 						&base_vha->dpc_flags);
7041 				ql_dbg(ql_dbg_dpc, base_vha, 0x4008,
7042 				    "ISP abort end.\n");
7043 			}
7044 		}
7045 
7046 		if (test_bit(PROCESS_PUREX_IOCB, &base_vha->dpc_flags)) {
7047 			if (atomic_read(&base_vha->loop_state) == LOOP_READY) {
7048 				qla24xx_process_purex_list
7049 					(&base_vha->purex_list);
7050 				clear_bit(PROCESS_PUREX_IOCB,
7051 				    &base_vha->dpc_flags);
7052 			}
7053 		}
7054 
7055 		if (IS_QLAFX00(ha))
7056 			goto loop_resync_check;
7057 
7058 		if (test_bit(ISP_QUIESCE_NEEDED, &base_vha->dpc_flags)) {
7059 			ql_dbg(ql_dbg_dpc, base_vha, 0x4009,
7060 			    "Quiescence mode scheduled.\n");
7061 			if (IS_P3P_TYPE(ha)) {
7062 				if (IS_QLA82XX(ha))
7063 					qla82xx_device_state_handler(base_vha);
7064 				if (IS_QLA8044(ha))
7065 					qla8044_device_state_handler(base_vha);
7066 				clear_bit(ISP_QUIESCE_NEEDED,
7067 				    &base_vha->dpc_flags);
7068 				if (!ha->flags.quiesce_owner) {
7069 					qla2x00_perform_loop_resync(base_vha);
7070 					if (IS_QLA82XX(ha)) {
7071 						qla82xx_idc_lock(ha);
7072 						qla82xx_clear_qsnt_ready(
7073 						    base_vha);
7074 						qla82xx_idc_unlock(ha);
7075 					} else if (IS_QLA8044(ha)) {
7076 						qla8044_idc_lock(ha);
7077 						qla8044_clear_qsnt_ready(
7078 						    base_vha);
7079 						qla8044_idc_unlock(ha);
7080 					}
7081 				}
7082 			} else {
7083 				clear_bit(ISP_QUIESCE_NEEDED,
7084 				    &base_vha->dpc_flags);
7085 				qla2x00_quiesce_io(base_vha);
7086 			}
7087 			ql_dbg(ql_dbg_dpc, base_vha, 0x400a,
7088 			    "Quiescence mode end.\n");
7089 		}
7090 
7091 		if (test_and_clear_bit(RESET_MARKER_NEEDED,
7092 				&base_vha->dpc_flags) &&
7093 		    (!(test_and_set_bit(RESET_ACTIVE, &base_vha->dpc_flags)))) {
7094 
7095 			ql_dbg(ql_dbg_dpc, base_vha, 0x400b,
7096 			    "Reset marker scheduled.\n");
7097 			qla2x00_rst_aen(base_vha);
7098 			clear_bit(RESET_ACTIVE, &base_vha->dpc_flags);
7099 			ql_dbg(ql_dbg_dpc, base_vha, 0x400c,
7100 			    "Reset marker end.\n");
7101 		}
7102 
7103 		/* Retry each device up to login retry count */
7104 		if (test_bit(RELOGIN_NEEDED, &base_vha->dpc_flags) &&
7105 		    !test_bit(LOOP_RESYNC_NEEDED, &base_vha->dpc_flags) &&
7106 		    atomic_read(&base_vha->loop_state) != LOOP_DOWN) {
7107 
7108 			if (!base_vha->relogin_jif ||
7109 			    time_after_eq(jiffies, base_vha->relogin_jif)) {
7110 				base_vha->relogin_jif = jiffies + HZ;
7111 				clear_bit(RELOGIN_NEEDED, &base_vha->dpc_flags);
7112 
7113 				ql_dbg(ql_dbg_disc, base_vha, 0x400d,
7114 				    "Relogin scheduled.\n");
7115 				qla24xx_post_relogin_work(base_vha);
7116 			}
7117 		}
7118 loop_resync_check:
7119 		if (!qla2x00_reset_active(base_vha) &&
7120 		    test_and_clear_bit(LOOP_RESYNC_NEEDED,
7121 		    &base_vha->dpc_flags)) {
7122 			/*
7123 			 * Allow abort_isp to complete before moving on to scanning.
7124 			 */
7125 			ql_dbg(ql_dbg_dpc, base_vha, 0x400f,
7126 			    "Loop resync scheduled.\n");
7127 
7128 			if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE,
7129 			    &base_vha->dpc_flags))) {
7130 
7131 				qla2x00_loop_resync(base_vha);
7132 
7133 				clear_bit(LOOP_RESYNC_ACTIVE,
7134 						&base_vha->dpc_flags);
7135 			}
7136 
7137 			ql_dbg(ql_dbg_dpc, base_vha, 0x4010,
7138 			    "Loop resync end.\n");
7139 		}
7140 
7141 		if (IS_QLAFX00(ha))
7142 			goto intr_on_check;
7143 
7144 		if (test_bit(NPIV_CONFIG_NEEDED, &base_vha->dpc_flags) &&
7145 		    atomic_read(&base_vha->loop_state) == LOOP_READY) {
7146 			clear_bit(NPIV_CONFIG_NEEDED, &base_vha->dpc_flags);
7147 			qla2xxx_flash_npiv_conf(base_vha);
7148 		}
7149 
7150 intr_on_check:
7151 		if (!ha->interrupts_on)
7152 			ha->isp_ops->enable_intrs(ha);
7153 
7154 		if (test_and_clear_bit(BEACON_BLINK_NEEDED,
7155 					&base_vha->dpc_flags)) {
7156 			if (ha->beacon_blink_led == 1)
7157 				ha->isp_ops->beacon_blink(base_vha);
7158 		}
7159 
7160 		/* qpair online check */
7161 		if (test_and_clear_bit(QPAIR_ONLINE_CHECK_NEEDED,
7162 		    &base_vha->dpc_flags)) {
7163 			if (ha->flags.eeh_busy ||
7164 			    ha->flags.pci_channel_io_perm_failure)
7165 				online = 0;
7166 			else
7167 				online = 1;
7168 
7169 			mutex_lock(&ha->mq_lock);
7170 			list_for_each_entry(qpair, &base_vha->qp_list,
7171 			    qp_list_elem)
7172 			qpair->online = online;
7173 			mutex_unlock(&ha->mq_lock);
7174 		}
7175 
7176 		if (test_and_clear_bit(SET_ZIO_THRESHOLD_NEEDED,
7177 				       &base_vha->dpc_flags)) {
7178 			u16 threshold = ha->nvme_last_rptd_aen + ha->last_zio_threshold;
7179 
7180 			if (threshold > ha->orig_fw_xcb_count)
7181 				threshold = ha->orig_fw_xcb_count;
7182 
7183 			ql_log(ql_log_info, base_vha, 0xffffff,
7184 			       "SET ZIO Activity exchange threshold to %d.\n",
7185 			       threshold);
7186 			if (qla27xx_set_zio_threshold(base_vha, threshold)) {
7187 				ql_log(ql_log_info, base_vha, 0xffffff,
7188 				       "Unable to SET ZIO Activity exchange threshold to %d.\n",
7189 				       threshold);
7190 			}
7191 		}
7192 
7193 		if (!IS_QLAFX00(ha))
7194 			qla2x00_do_dpc_all_vps(base_vha);
7195 
7196 		if (test_and_clear_bit(N2N_LINK_RESET,
7197 			&base_vha->dpc_flags)) {
7198 			qla2x00_lip_reset(base_vha);
7199 		}
7200 
7201 		ha->dpc_active = 0;
7202 end_loop:
7203 		set_current_state(TASK_INTERRUPTIBLE);
7204 	} /* End of while(1) */
7205 	__set_current_state(TASK_RUNNING);
7206 
7207 	ql_dbg(ql_dbg_dpc, base_vha, 0x4011,
7208 	    "DPC handler exiting.\n");
7209 
7210 	/*
7211 	 * Make sure that nobody tries to wake us up again.
7212 	 */
7213 	ha->dpc_active = 0;
7214 
7215 	/* Cleanup any residual CTX SRBs. */
7216 	qla2x00_abort_all_cmds(base_vha, DID_NO_CONNECT << 16);
7217 
7218 	return 0;
7219 }
7220 
7221 void
7222 qla2xxx_wake_dpc(struct scsi_qla_host *vha)
7223 {
7224 	struct qla_hw_data *ha = vha->hw;
7225 	struct task_struct *t = ha->dpc_thread;
7226 
7227 	if (!test_bit(UNLOADING, &vha->dpc_flags) && t)
7228 		wake_up_process(t);
7229 }
7230 
7231 /*
7232 *  qla2x00_rst_aen
7233 *      Processes asynchronous reset.
7234 *
7235 * Input:
7236 *      ha  = adapter block pointer.
7237 */
7238 static void
7239 qla2x00_rst_aen(scsi_qla_host_t *vha)
7240 {
7241 	if (vha->flags.online && !vha->flags.reset_active &&
7242 	    !atomic_read(&vha->loop_down_timer) &&
7243 	    !(test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))) {
7244 		do {
7245 			clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
7246 
7247 			/*
7248 			 * Issue marker command only when we are going to start
7249 			 * the I/O.
7250 			 */
7251 			vha->marker_needed = 1;
7252 		} while (!atomic_read(&vha->loop_down_timer) &&
7253 		    (test_bit(RESET_MARKER_NEEDED, &vha->dpc_flags)));
7254 	}
7255 }
7256 
7257 static bool qla_do_heartbeat(struct scsi_qla_host *vha)
7258 {
7259 	struct qla_hw_data *ha = vha->hw;
7260 	u32 cmpl_cnt;
7261 	u16 i;
7262 	bool do_heartbeat = false;
7263 
7264 	/*
7265 	 * Allow do_heartbeat only if we don’t have any active interrupts,
7266 	 * but there are still IOs outstanding with firmware.
7267 	 */
7268 	cmpl_cnt = ha->base_qpair->cmd_completion_cnt;
7269 	if (cmpl_cnt == ha->base_qpair->prev_completion_cnt &&
7270 	    cmpl_cnt != ha->base_qpair->cmd_cnt) {
7271 		do_heartbeat = true;
7272 		goto skip;
7273 	}
7274 	ha->base_qpair->prev_completion_cnt = cmpl_cnt;
7275 
7276 	for (i = 0; i < ha->max_qpairs; i++) {
7277 		if (ha->queue_pair_map[i]) {
7278 			cmpl_cnt = ha->queue_pair_map[i]->cmd_completion_cnt;
7279 			if (cmpl_cnt == ha->queue_pair_map[i]->prev_completion_cnt &&
7280 			    cmpl_cnt != ha->queue_pair_map[i]->cmd_cnt) {
7281 				do_heartbeat = true;
7282 				break;
7283 			}
7284 			ha->queue_pair_map[i]->prev_completion_cnt = cmpl_cnt;
7285 		}
7286 	}
7287 
7288 skip:
7289 	return do_heartbeat;
7290 }
7291 
7292 static void qla_heart_beat(struct scsi_qla_host *vha, u16 dpc_started)
7293 {
7294 	struct qla_hw_data *ha = vha->hw;
7295 
7296 	if (vha->vp_idx)
7297 		return;
7298 
7299 	if (vha->hw->flags.eeh_busy || qla2x00_chip_is_down(vha))
7300 		return;
7301 
7302 	/*
7303 	 * dpc thread cannot run if heartbeat is running at the same time.
7304 	 * We also do not want to starve heartbeat task. Therefore, do
7305 	 * heartbeat task at least once every 5 seconds.
7306 	 */
7307 	if (dpc_started &&
7308 	    time_before(jiffies, ha->last_heartbeat_run_jiffies + 5 * HZ))
7309 		return;
7310 
7311 	if (qla_do_heartbeat(vha)) {
7312 		ha->last_heartbeat_run_jiffies = jiffies;
7313 		queue_work(ha->wq, &ha->heartbeat_work);
7314 	}
7315 }
7316 
7317 static void qla_wind_down_chip(scsi_qla_host_t *vha)
7318 {
7319 	struct qla_hw_data *ha = vha->hw;
7320 
7321 	if (!ha->flags.eeh_busy)
7322 		return;
7323 	if (ha->pci_error_state)
7324 		/* system is trying to recover */
7325 		return;
7326 
7327 	/*
7328 	 * Current system is not handling PCIE error.  At this point, this is
7329 	 * best effort to wind down the adapter.
7330 	 */
7331 	if (time_after_eq(jiffies, ha->eeh_jif + ql2xdelay_before_pci_error_handling * HZ) &&
7332 	    !ha->flags.eeh_flush) {
7333 		ql_log(ql_log_info, vha, 0x9009,
7334 		    "PCI Error detected, attempting to reset hardware.\n");
7335 
7336 		ha->isp_ops->reset_chip(vha);
7337 		ha->isp_ops->disable_intrs(ha);
7338 
7339 		ha->flags.eeh_flush = EEH_FLUSH_RDY;
7340 		ha->eeh_jif = jiffies;
7341 
7342 	} else if (ha->flags.eeh_flush == EEH_FLUSH_RDY &&
7343 	    time_after_eq(jiffies, ha->eeh_jif +  5 * HZ)) {
7344 		pci_clear_master(ha->pdev);
7345 
7346 		/* flush all command */
7347 		qla2x00_abort_isp_cleanup(vha);
7348 		ha->flags.eeh_flush = EEH_FLUSH_DONE;
7349 
7350 		ql_log(ql_log_info, vha, 0x900a,
7351 		    "PCI Error handling complete, all IOs aborted.\n");
7352 	}
7353 }
7354 
7355 /**************************************************************************
7356 *   qla2x00_timer
7357 *
7358 * Description:
7359 *   One second timer
7360 *
7361 * Context: Interrupt
7362 ***************************************************************************/
7363 void
7364 qla2x00_timer(struct timer_list *t)
7365 {
7366 	scsi_qla_host_t *vha = from_timer(vha, t, timer);
7367 	unsigned long	cpu_flags = 0;
7368 	int		start_dpc = 0;
7369 	int		index;
7370 	srb_t		*sp;
7371 	uint16_t        w;
7372 	struct qla_hw_data *ha = vha->hw;
7373 	struct req_que *req;
7374 	unsigned long flags;
7375 	fc_port_t *fcport = NULL;
7376 
7377 	if (ha->flags.eeh_busy) {
7378 		qla_wind_down_chip(vha);
7379 
7380 		ql_dbg(ql_dbg_timer, vha, 0x6000,
7381 		    "EEH = %d, restarting timer.\n",
7382 		    ha->flags.eeh_busy);
7383 		qla2x00_restart_timer(vha, WATCH_INTERVAL);
7384 		return;
7385 	}
7386 
7387 	/*
7388 	 * Hardware read to raise pending EEH errors during mailbox waits. If
7389 	 * the read returns -1 then disable the board.
7390 	 */
7391 	if (!pci_channel_offline(ha->pdev)) {
7392 		pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
7393 		qla2x00_check_reg16_for_disconnect(vha, w);
7394 	}
7395 
7396 	/* Make sure qla82xx_watchdog is run only for physical port */
7397 	if (!vha->vp_idx && IS_P3P_TYPE(ha)) {
7398 		if (test_bit(ISP_QUIESCE_NEEDED, &vha->dpc_flags))
7399 			start_dpc++;
7400 		if (IS_QLA82XX(ha))
7401 			qla82xx_watchdog(vha);
7402 		else if (IS_QLA8044(ha))
7403 			qla8044_watchdog(vha);
7404 	}
7405 
7406 	if (!vha->vp_idx && IS_QLAFX00(ha))
7407 		qlafx00_timer_routine(vha);
7408 
7409 	if (vha->link_down_time < QLA2XX_MAX_LINK_DOWN_TIME)
7410 		vha->link_down_time++;
7411 
7412 	spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
7413 	list_for_each_entry(fcport, &vha->vp_fcports, list) {
7414 		if (fcport->tgt_link_down_time < QLA2XX_MAX_LINK_DOWN_TIME)
7415 			fcport->tgt_link_down_time++;
7416 	}
7417 	spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
7418 
7419 	/* Loop down handler. */
7420 	if (atomic_read(&vha->loop_down_timer) > 0 &&
7421 	    !(test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags)) &&
7422 	    !(test_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags))
7423 		&& vha->flags.online) {
7424 
7425 		if (atomic_read(&vha->loop_down_timer) ==
7426 		    vha->loop_down_abort_time) {
7427 
7428 			ql_log(ql_log_info, vha, 0x6008,
7429 			    "Loop down - aborting the queues before time expires.\n");
7430 
7431 			if (!IS_QLA2100(ha) && vha->link_down_timeout)
7432 				atomic_set(&vha->loop_state, LOOP_DEAD);
7433 
7434 			/*
7435 			 * Schedule an ISP abort to return any FCP2-device
7436 			 * commands.
7437 			 */
7438 			/* NPIV - scan physical port only */
7439 			if (!vha->vp_idx) {
7440 				spin_lock_irqsave(&ha->hardware_lock,
7441 				    cpu_flags);
7442 				req = ha->req_q_map[0];
7443 				for (index = 1;
7444 				    index < req->num_outstanding_cmds;
7445 				    index++) {
7446 					fc_port_t *sfcp;
7447 
7448 					sp = req->outstanding_cmds[index];
7449 					if (!sp)
7450 						continue;
7451 					if (sp->cmd_type != TYPE_SRB)
7452 						continue;
7453 					if (sp->type != SRB_SCSI_CMD)
7454 						continue;
7455 					sfcp = sp->fcport;
7456 					if (!(sfcp->flags & FCF_FCP2_DEVICE))
7457 						continue;
7458 
7459 					if (IS_QLA82XX(ha))
7460 						set_bit(FCOE_CTX_RESET_NEEDED,
7461 							&vha->dpc_flags);
7462 					else
7463 						set_bit(ISP_ABORT_NEEDED,
7464 							&vha->dpc_flags);
7465 					break;
7466 				}
7467 				spin_unlock_irqrestore(&ha->hardware_lock,
7468 								cpu_flags);
7469 			}
7470 			start_dpc++;
7471 		}
7472 
7473 		/* if the loop has been down for 4 minutes, reinit adapter */
7474 		if (atomic_dec_and_test(&vha->loop_down_timer) != 0) {
7475 			if (!(vha->device_flags & DFLG_NO_CABLE) && !vha->vp_idx) {
7476 				ql_log(ql_log_warn, vha, 0x6009,
7477 				    "Loop down - aborting ISP.\n");
7478 
7479 				if (IS_QLA82XX(ha))
7480 					set_bit(FCOE_CTX_RESET_NEEDED,
7481 						&vha->dpc_flags);
7482 				else
7483 					set_bit(ISP_ABORT_NEEDED,
7484 						&vha->dpc_flags);
7485 			}
7486 		}
7487 		ql_dbg(ql_dbg_timer, vha, 0x600a,
7488 		    "Loop down - seconds remaining %d.\n",
7489 		    atomic_read(&vha->loop_down_timer));
7490 	}
7491 	/* Check if beacon LED needs to be blinked for physical host only */
7492 	if (!vha->vp_idx && (ha->beacon_blink_led == 1)) {
7493 		/* There is no beacon_blink function for ISP82xx */
7494 		if (!IS_P3P_TYPE(ha)) {
7495 			set_bit(BEACON_BLINK_NEEDED, &vha->dpc_flags);
7496 			start_dpc++;
7497 		}
7498 	}
7499 
7500 	/* check if edif running */
7501 	if (vha->hw->flags.edif_enabled)
7502 		qla_edif_timer(vha);
7503 
7504 	/* Process any deferred work. */
7505 	if (!list_empty(&vha->work_list)) {
7506 		unsigned long flags;
7507 		bool q = false;
7508 
7509 		spin_lock_irqsave(&vha->work_lock, flags);
7510 		if (!test_and_set_bit(IOCB_WORK_ACTIVE, &vha->dpc_flags))
7511 			q = true;
7512 		spin_unlock_irqrestore(&vha->work_lock, flags);
7513 		if (q)
7514 			queue_work(vha->hw->wq, &vha->iocb_work);
7515 	}
7516 
7517 	/*
7518 	 * FC-NVME
7519 	 * see if the active AEN count has changed from what was last reported.
7520 	 */
7521 	index = atomic_read(&ha->nvme_active_aen_cnt);
7522 	if (!vha->vp_idx &&
7523 	    (index != ha->nvme_last_rptd_aen) &&
7524 	    ha->zio_mode == QLA_ZIO_MODE_6 &&
7525 	    !ha->flags.host_shutting_down) {
7526 		ha->nvme_last_rptd_aen = atomic_read(&ha->nvme_active_aen_cnt);
7527 		ql_log(ql_log_info, vha, 0x3002,
7528 		    "nvme: Sched: Set ZIO exchange threshold to %d.\n",
7529 		    ha->nvme_last_rptd_aen);
7530 		set_bit(SET_ZIO_THRESHOLD_NEEDED, &vha->dpc_flags);
7531 		start_dpc++;
7532 	}
7533 
7534 	if (!vha->vp_idx &&
7535 	    atomic_read(&ha->zio_threshold) != ha->last_zio_threshold &&
7536 	    IS_ZIO_THRESHOLD_CAPABLE(ha)) {
7537 		ql_log(ql_log_info, vha, 0x3002,
7538 		    "Sched: Set ZIO exchange threshold to %d.\n",
7539 		    ha->last_zio_threshold);
7540 		ha->last_zio_threshold = atomic_read(&ha->zio_threshold);
7541 		set_bit(SET_ZIO_THRESHOLD_NEEDED, &vha->dpc_flags);
7542 		start_dpc++;
7543 	}
7544 	qla_adjust_buf(vha);
7545 
7546 	/* borrowing w to signify dpc will run */
7547 	w = 0;
7548 	/* Schedule the DPC routine if needed */
7549 	if ((test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
7550 	    test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags) ||
7551 	    start_dpc ||
7552 	    test_bit(RESET_MARKER_NEEDED, &vha->dpc_flags) ||
7553 	    test_bit(BEACON_BLINK_NEEDED, &vha->dpc_flags) ||
7554 	    test_bit(ISP_UNRECOVERABLE, &vha->dpc_flags) ||
7555 	    test_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags) ||
7556 	    test_bit(VP_DPC_NEEDED, &vha->dpc_flags) ||
7557 	    test_bit(RELOGIN_NEEDED, &vha->dpc_flags) ||
7558 	    test_bit(PROCESS_PUREX_IOCB, &vha->dpc_flags))) {
7559 		ql_dbg(ql_dbg_timer, vha, 0x600b,
7560 		    "isp_abort_needed=%d loop_resync_needed=%d "
7561 		    "start_dpc=%d reset_marker_needed=%d",
7562 		    test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags),
7563 		    test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags),
7564 		    start_dpc, test_bit(RESET_MARKER_NEEDED, &vha->dpc_flags));
7565 		ql_dbg(ql_dbg_timer, vha, 0x600c,
7566 		    "beacon_blink_needed=%d isp_unrecoverable=%d "
7567 		    "fcoe_ctx_reset_needed=%d vp_dpc_needed=%d "
7568 		    "relogin_needed=%d, Process_purex_iocb=%d.\n",
7569 		    test_bit(BEACON_BLINK_NEEDED, &vha->dpc_flags),
7570 		    test_bit(ISP_UNRECOVERABLE, &vha->dpc_flags),
7571 		    test_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags),
7572 		    test_bit(VP_DPC_NEEDED, &vha->dpc_flags),
7573 		    test_bit(RELOGIN_NEEDED, &vha->dpc_flags),
7574 		    test_bit(PROCESS_PUREX_IOCB, &vha->dpc_flags));
7575 		qla2xxx_wake_dpc(vha);
7576 		w = 1;
7577 	}
7578 
7579 	qla_heart_beat(vha, w);
7580 
7581 	qla2x00_restart_timer(vha, WATCH_INTERVAL);
7582 }
7583 
7584 /* Firmware interface routines. */
7585 
7586 #define FW_ISP21XX	0
7587 #define FW_ISP22XX	1
7588 #define FW_ISP2300	2
7589 #define FW_ISP2322	3
7590 #define FW_ISP24XX	4
7591 #define FW_ISP25XX	5
7592 #define FW_ISP81XX	6
7593 #define FW_ISP82XX	7
7594 #define FW_ISP2031	8
7595 #define FW_ISP8031	9
7596 #define FW_ISP27XX	10
7597 #define FW_ISP28XX	11
7598 
7599 #define FW_FILE_ISP21XX	"ql2100_fw.bin"
7600 #define FW_FILE_ISP22XX	"ql2200_fw.bin"
7601 #define FW_FILE_ISP2300	"ql2300_fw.bin"
7602 #define FW_FILE_ISP2322	"ql2322_fw.bin"
7603 #define FW_FILE_ISP24XX	"ql2400_fw.bin"
7604 #define FW_FILE_ISP25XX	"ql2500_fw.bin"
7605 #define FW_FILE_ISP81XX	"ql8100_fw.bin"
7606 #define FW_FILE_ISP82XX	"ql8200_fw.bin"
7607 #define FW_FILE_ISP2031	"ql2600_fw.bin"
7608 #define FW_FILE_ISP8031	"ql8300_fw.bin"
7609 #define FW_FILE_ISP27XX	"ql2700_fw.bin"
7610 #define FW_FILE_ISP28XX	"ql2800_fw.bin"
7611 
7612 
7613 static DEFINE_MUTEX(qla_fw_lock);
7614 
7615 static struct fw_blob qla_fw_blobs[] = {
7616 	{ .name = FW_FILE_ISP21XX, .segs = { 0x1000, 0 }, },
7617 	{ .name = FW_FILE_ISP22XX, .segs = { 0x1000, 0 }, },
7618 	{ .name = FW_FILE_ISP2300, .segs = { 0x800, 0 }, },
7619 	{ .name = FW_FILE_ISP2322, .segs = { 0x800, 0x1c000, 0x1e000, 0 }, },
7620 	{ .name = FW_FILE_ISP24XX, },
7621 	{ .name = FW_FILE_ISP25XX, },
7622 	{ .name = FW_FILE_ISP81XX, },
7623 	{ .name = FW_FILE_ISP82XX, },
7624 	{ .name = FW_FILE_ISP2031, },
7625 	{ .name = FW_FILE_ISP8031, },
7626 	{ .name = FW_FILE_ISP27XX, },
7627 	{ .name = FW_FILE_ISP28XX, },
7628 	{ .name = NULL, },
7629 };
7630 
7631 struct fw_blob *
7632 qla2x00_request_firmware(scsi_qla_host_t *vha)
7633 {
7634 	struct qla_hw_data *ha = vha->hw;
7635 	struct fw_blob *blob;
7636 
7637 	if (IS_QLA2100(ha)) {
7638 		blob = &qla_fw_blobs[FW_ISP21XX];
7639 	} else if (IS_QLA2200(ha)) {
7640 		blob = &qla_fw_blobs[FW_ISP22XX];
7641 	} else if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
7642 		blob = &qla_fw_blobs[FW_ISP2300];
7643 	} else if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
7644 		blob = &qla_fw_blobs[FW_ISP2322];
7645 	} else if (IS_QLA24XX_TYPE(ha)) {
7646 		blob = &qla_fw_blobs[FW_ISP24XX];
7647 	} else if (IS_QLA25XX(ha)) {
7648 		blob = &qla_fw_blobs[FW_ISP25XX];
7649 	} else if (IS_QLA81XX(ha)) {
7650 		blob = &qla_fw_blobs[FW_ISP81XX];
7651 	} else if (IS_QLA82XX(ha)) {
7652 		blob = &qla_fw_blobs[FW_ISP82XX];
7653 	} else if (IS_QLA2031(ha)) {
7654 		blob = &qla_fw_blobs[FW_ISP2031];
7655 	} else if (IS_QLA8031(ha)) {
7656 		blob = &qla_fw_blobs[FW_ISP8031];
7657 	} else if (IS_QLA27XX(ha)) {
7658 		blob = &qla_fw_blobs[FW_ISP27XX];
7659 	} else if (IS_QLA28XX(ha)) {
7660 		blob = &qla_fw_blobs[FW_ISP28XX];
7661 	} else {
7662 		return NULL;
7663 	}
7664 
7665 	if (!blob->name)
7666 		return NULL;
7667 
7668 	mutex_lock(&qla_fw_lock);
7669 	if (blob->fw)
7670 		goto out;
7671 
7672 	if (request_firmware(&blob->fw, blob->name, &ha->pdev->dev)) {
7673 		ql_log(ql_log_warn, vha, 0x0063,
7674 		    "Failed to load firmware image (%s).\n", blob->name);
7675 		blob->fw = NULL;
7676 		blob = NULL;
7677 	}
7678 
7679 out:
7680 	mutex_unlock(&qla_fw_lock);
7681 	return blob;
7682 }
7683 
7684 static void
7685 qla2x00_release_firmware(void)
7686 {
7687 	struct fw_blob *blob;
7688 
7689 	mutex_lock(&qla_fw_lock);
7690 	for (blob = qla_fw_blobs; blob->name; blob++)
7691 		release_firmware(blob->fw);
7692 	mutex_unlock(&qla_fw_lock);
7693 }
7694 
7695 static void qla_pci_error_cleanup(scsi_qla_host_t *vha)
7696 {
7697 	struct qla_hw_data *ha = vha->hw;
7698 	scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
7699 	struct qla_qpair *qpair = NULL;
7700 	struct scsi_qla_host *vp, *tvp;
7701 	fc_port_t *fcport;
7702 	int i;
7703 	unsigned long flags;
7704 
7705 	ql_dbg(ql_dbg_aer, vha, 0x9000,
7706 	       "%s\n", __func__);
7707 	ha->chip_reset++;
7708 
7709 	ha->base_qpair->chip_reset = ha->chip_reset;
7710 	for (i = 0; i < ha->max_qpairs; i++) {
7711 		if (ha->queue_pair_map[i])
7712 			ha->queue_pair_map[i]->chip_reset =
7713 			    ha->base_qpair->chip_reset;
7714 	}
7715 
7716 	/*
7717 	 * purge mailbox might take a while. Slot Reset/chip reset
7718 	 * will take care of the purge
7719 	 */
7720 
7721 	mutex_lock(&ha->mq_lock);
7722 	ha->base_qpair->online = 0;
7723 	list_for_each_entry(qpair, &base_vha->qp_list, qp_list_elem)
7724 		qpair->online = 0;
7725 	wmb();
7726 	mutex_unlock(&ha->mq_lock);
7727 
7728 	qla2x00_mark_all_devices_lost(vha);
7729 
7730 	spin_lock_irqsave(&ha->vport_slock, flags);
7731 	list_for_each_entry_safe(vp, tvp, &ha->vp_list, list) {
7732 		atomic_inc(&vp->vref_count);
7733 		spin_unlock_irqrestore(&ha->vport_slock, flags);
7734 		qla2x00_mark_all_devices_lost(vp);
7735 		spin_lock_irqsave(&ha->vport_slock, flags);
7736 		atomic_dec(&vp->vref_count);
7737 	}
7738 	spin_unlock_irqrestore(&ha->vport_slock, flags);
7739 
7740 	/* Clear all async request states across all VPs. */
7741 	list_for_each_entry(fcport, &vha->vp_fcports, list)
7742 		fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
7743 
7744 	spin_lock_irqsave(&ha->vport_slock, flags);
7745 	list_for_each_entry_safe(vp, tvp, &ha->vp_list, list) {
7746 		atomic_inc(&vp->vref_count);
7747 		spin_unlock_irqrestore(&ha->vport_slock, flags);
7748 		list_for_each_entry(fcport, &vp->vp_fcports, list)
7749 			fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
7750 		spin_lock_irqsave(&ha->vport_slock, flags);
7751 		atomic_dec(&vp->vref_count);
7752 	}
7753 	spin_unlock_irqrestore(&ha->vport_slock, flags);
7754 }
7755 
7756 
7757 static pci_ers_result_t
7758 qla2xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
7759 {
7760 	scsi_qla_host_t *vha = pci_get_drvdata(pdev);
7761 	struct qla_hw_data *ha = vha->hw;
7762 	pci_ers_result_t ret = PCI_ERS_RESULT_NEED_RESET;
7763 
7764 	ql_log(ql_log_warn, vha, 0x9000,
7765 	       "PCI error detected, state %x.\n", state);
7766 	ha->pci_error_state = QLA_PCI_ERR_DETECTED;
7767 
7768 	if (!atomic_read(&pdev->enable_cnt)) {
7769 		ql_log(ql_log_info, vha, 0xffff,
7770 			"PCI device is disabled,state %x\n", state);
7771 		ret = PCI_ERS_RESULT_NEED_RESET;
7772 		goto out;
7773 	}
7774 
7775 	switch (state) {
7776 	case pci_channel_io_normal:
7777 		qla_pci_set_eeh_busy(vha);
7778 		if (ql2xmqsupport || ql2xnvmeenable) {
7779 			set_bit(QPAIR_ONLINE_CHECK_NEEDED, &vha->dpc_flags);
7780 			qla2xxx_wake_dpc(vha);
7781 		}
7782 		ret = PCI_ERS_RESULT_CAN_RECOVER;
7783 		break;
7784 	case pci_channel_io_frozen:
7785 		qla_pci_set_eeh_busy(vha);
7786 		ret = PCI_ERS_RESULT_NEED_RESET;
7787 		break;
7788 	case pci_channel_io_perm_failure:
7789 		ha->flags.pci_channel_io_perm_failure = 1;
7790 		qla2x00_abort_all_cmds(vha, DID_NO_CONNECT << 16);
7791 		if (ql2xmqsupport || ql2xnvmeenable) {
7792 			set_bit(QPAIR_ONLINE_CHECK_NEEDED, &vha->dpc_flags);
7793 			qla2xxx_wake_dpc(vha);
7794 		}
7795 		ret = PCI_ERS_RESULT_DISCONNECT;
7796 	}
7797 out:
7798 	ql_dbg(ql_dbg_aer, vha, 0x600d,
7799 	       "PCI error detected returning [%x].\n", ret);
7800 	return ret;
7801 }
7802 
7803 static pci_ers_result_t
7804 qla2xxx_pci_mmio_enabled(struct pci_dev *pdev)
7805 {
7806 	int risc_paused = 0;
7807 	uint32_t stat;
7808 	unsigned long flags;
7809 	scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
7810 	struct qla_hw_data *ha = base_vha->hw;
7811 	struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
7812 	struct device_reg_24xx __iomem *reg24 = &ha->iobase->isp24;
7813 
7814 	ql_log(ql_log_warn, base_vha, 0x9000,
7815 	       "mmio enabled\n");
7816 
7817 	ha->pci_error_state = QLA_PCI_MMIO_ENABLED;
7818 
7819 	if (IS_QLA82XX(ha))
7820 		return PCI_ERS_RESULT_RECOVERED;
7821 
7822 	if (qla2x00_isp_reg_stat(ha)) {
7823 		ql_log(ql_log_info, base_vha, 0x803f,
7824 		    "During mmio enabled, PCI/Register disconnect still detected.\n");
7825 		goto out;
7826 	}
7827 
7828 	spin_lock_irqsave(&ha->hardware_lock, flags);
7829 	if (IS_QLA2100(ha) || IS_QLA2200(ha)){
7830 		stat = rd_reg_word(&reg->hccr);
7831 		if (stat & HCCR_RISC_PAUSE)
7832 			risc_paused = 1;
7833 	} else if (IS_QLA23XX(ha)) {
7834 		stat = rd_reg_dword(&reg->u.isp2300.host_status);
7835 		if (stat & HSR_RISC_PAUSED)
7836 			risc_paused = 1;
7837 	} else if (IS_FWI2_CAPABLE(ha)) {
7838 		stat = rd_reg_dword(&reg24->host_status);
7839 		if (stat & HSRX_RISC_PAUSED)
7840 			risc_paused = 1;
7841 	}
7842 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
7843 
7844 	if (risc_paused) {
7845 		ql_log(ql_log_info, base_vha, 0x9003,
7846 		    "RISC paused -- mmio_enabled, Dumping firmware.\n");
7847 		qla2xxx_dump_fw(base_vha);
7848 	}
7849 out:
7850 	/* set PCI_ERS_RESULT_NEED_RESET to trigger call to qla2xxx_pci_slot_reset */
7851 	ql_dbg(ql_dbg_aer, base_vha, 0x600d,
7852 	       "mmio enabled returning.\n");
7853 	return PCI_ERS_RESULT_NEED_RESET;
7854 }
7855 
7856 static pci_ers_result_t
7857 qla2xxx_pci_slot_reset(struct pci_dev *pdev)
7858 {
7859 	pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
7860 	scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
7861 	struct qla_hw_data *ha = base_vha->hw;
7862 	int rc;
7863 	struct qla_qpair *qpair = NULL;
7864 
7865 	ql_log(ql_log_warn, base_vha, 0x9004,
7866 	       "Slot Reset.\n");
7867 
7868 	ha->pci_error_state = QLA_PCI_SLOT_RESET;
7869 	/* Workaround: qla2xxx driver which access hardware earlier
7870 	 * needs error state to be pci_channel_io_online.
7871 	 * Otherwise mailbox command timesout.
7872 	 */
7873 	pdev->error_state = pci_channel_io_normal;
7874 
7875 	pci_restore_state(pdev);
7876 
7877 	/* pci_restore_state() clears the saved_state flag of the device
7878 	 * save restored state which resets saved_state flag
7879 	 */
7880 	pci_save_state(pdev);
7881 
7882 	if (ha->mem_only)
7883 		rc = pci_enable_device_mem(pdev);
7884 	else
7885 		rc = pci_enable_device(pdev);
7886 
7887 	if (rc) {
7888 		ql_log(ql_log_warn, base_vha, 0x9005,
7889 		    "Can't re-enable PCI device after reset.\n");
7890 		goto exit_slot_reset;
7891 	}
7892 
7893 
7894 	if (ha->isp_ops->pci_config(base_vha))
7895 		goto exit_slot_reset;
7896 
7897 	mutex_lock(&ha->mq_lock);
7898 	list_for_each_entry(qpair, &base_vha->qp_list, qp_list_elem)
7899 		qpair->online = 1;
7900 	mutex_unlock(&ha->mq_lock);
7901 
7902 	ha->flags.eeh_busy = 0;
7903 	base_vha->flags.online = 1;
7904 	set_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
7905 	ha->isp_ops->abort_isp(base_vha);
7906 	clear_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
7907 
7908 	if (qla2x00_isp_reg_stat(ha)) {
7909 		ha->flags.eeh_busy = 1;
7910 		qla_pci_error_cleanup(base_vha);
7911 		ql_log(ql_log_warn, base_vha, 0x9005,
7912 		       "Device unable to recover from PCI error.\n");
7913 	} else {
7914 		ret =  PCI_ERS_RESULT_RECOVERED;
7915 	}
7916 
7917 exit_slot_reset:
7918 	ql_dbg(ql_dbg_aer, base_vha, 0x900e,
7919 	    "Slot Reset returning %x.\n", ret);
7920 
7921 	return ret;
7922 }
7923 
7924 static void
7925 qla2xxx_pci_resume(struct pci_dev *pdev)
7926 {
7927 	scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
7928 	struct qla_hw_data *ha = base_vha->hw;
7929 	int ret;
7930 
7931 	ql_log(ql_log_warn, base_vha, 0x900f,
7932 	       "Pci Resume.\n");
7933 
7934 
7935 	ret = qla2x00_wait_for_hba_online(base_vha);
7936 	if (ret != QLA_SUCCESS) {
7937 		ql_log(ql_log_fatal, base_vha, 0x9002,
7938 		    "The device failed to resume I/O from slot/link_reset.\n");
7939 	}
7940 	ha->pci_error_state = QLA_PCI_RESUME;
7941 	ql_dbg(ql_dbg_aer, base_vha, 0x600d,
7942 	       "Pci Resume returning.\n");
7943 }
7944 
7945 void qla_pci_set_eeh_busy(struct scsi_qla_host *vha)
7946 {
7947 	struct qla_hw_data *ha = vha->hw;
7948 	struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
7949 	bool do_cleanup = false;
7950 	unsigned long flags;
7951 
7952 	if (ha->flags.eeh_busy)
7953 		return;
7954 
7955 	spin_lock_irqsave(&base_vha->work_lock, flags);
7956 	if (!ha->flags.eeh_busy) {
7957 		ha->eeh_jif = jiffies;
7958 		ha->flags.eeh_flush = 0;
7959 
7960 		ha->flags.eeh_busy = 1;
7961 		do_cleanup = true;
7962 	}
7963 	spin_unlock_irqrestore(&base_vha->work_lock, flags);
7964 
7965 	if (do_cleanup)
7966 		qla_pci_error_cleanup(base_vha);
7967 }
7968 
7969 /*
7970  * this routine will schedule a task to pause IO from interrupt context
7971  * if caller sees a PCIE error event (register read = 0xf's)
7972  */
7973 void qla_schedule_eeh_work(struct scsi_qla_host *vha)
7974 {
7975 	struct qla_hw_data *ha = vha->hw;
7976 	struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
7977 
7978 	if (ha->flags.eeh_busy)
7979 		return;
7980 
7981 	set_bit(DO_EEH_RECOVERY, &base_vha->dpc_flags);
7982 	qla2xxx_wake_dpc(base_vha);
7983 }
7984 
7985 static void
7986 qla_pci_reset_prepare(struct pci_dev *pdev)
7987 {
7988 	scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
7989 	struct qla_hw_data *ha = base_vha->hw;
7990 	struct qla_qpair *qpair;
7991 
7992 	ql_log(ql_log_warn, base_vha, 0xffff,
7993 	    "%s.\n", __func__);
7994 
7995 	/*
7996 	 * PCI FLR/function reset is about to reset the
7997 	 * slot. Stop the chip to stop all DMA access.
7998 	 * It is assumed that pci_reset_done will be called
7999 	 * after FLR to resume Chip operation.
8000 	 */
8001 	ha->flags.eeh_busy = 1;
8002 	mutex_lock(&ha->mq_lock);
8003 	list_for_each_entry(qpair, &base_vha->qp_list, qp_list_elem)
8004 		qpair->online = 0;
8005 	mutex_unlock(&ha->mq_lock);
8006 
8007 	set_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
8008 	qla2x00_abort_isp_cleanup(base_vha);
8009 	qla2x00_abort_all_cmds(base_vha, DID_RESET << 16);
8010 }
8011 
8012 static void
8013 qla_pci_reset_done(struct pci_dev *pdev)
8014 {
8015 	scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
8016 	struct qla_hw_data *ha = base_vha->hw;
8017 	struct qla_qpair *qpair;
8018 
8019 	ql_log(ql_log_warn, base_vha, 0xffff,
8020 	    "%s.\n", __func__);
8021 
8022 	/*
8023 	 * FLR just completed by PCI layer. Resume adapter
8024 	 */
8025 	ha->flags.eeh_busy = 0;
8026 	mutex_lock(&ha->mq_lock);
8027 	list_for_each_entry(qpair, &base_vha->qp_list, qp_list_elem)
8028 		qpair->online = 1;
8029 	mutex_unlock(&ha->mq_lock);
8030 
8031 	base_vha->flags.online = 1;
8032 	ha->isp_ops->abort_isp(base_vha);
8033 	clear_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
8034 }
8035 
8036 static void qla2xxx_map_queues(struct Scsi_Host *shost)
8037 {
8038 	scsi_qla_host_t *vha = (scsi_qla_host_t *)shost->hostdata;
8039 	struct blk_mq_queue_map *qmap = &shost->tag_set.map[HCTX_TYPE_DEFAULT];
8040 
8041 	if (USER_CTRL_IRQ(vha->hw) || !vha->hw->mqiobase)
8042 		blk_mq_map_queues(qmap);
8043 	else
8044 		blk_mq_pci_map_queues(qmap, vha->hw->pdev, vha->irq_offset);
8045 }
8046 
8047 struct scsi_host_template qla2xxx_driver_template = {
8048 	.module			= THIS_MODULE,
8049 	.name			= QLA2XXX_DRIVER_NAME,
8050 	.queuecommand		= qla2xxx_queuecommand,
8051 
8052 	.eh_timed_out		= fc_eh_timed_out,
8053 	.eh_abort_handler	= qla2xxx_eh_abort,
8054 	.eh_should_retry_cmd	= fc_eh_should_retry_cmd,
8055 	.eh_device_reset_handler = qla2xxx_eh_device_reset,
8056 	.eh_target_reset_handler = qla2xxx_eh_target_reset,
8057 	.eh_bus_reset_handler	= qla2xxx_eh_bus_reset,
8058 	.eh_host_reset_handler	= qla2xxx_eh_host_reset,
8059 
8060 	.slave_configure	= qla2xxx_slave_configure,
8061 
8062 	.slave_alloc		= qla2xxx_slave_alloc,
8063 	.slave_destroy		= qla2xxx_slave_destroy,
8064 	.scan_finished		= qla2xxx_scan_finished,
8065 	.scan_start		= qla2xxx_scan_start,
8066 	.change_queue_depth	= scsi_change_queue_depth,
8067 	.map_queues             = qla2xxx_map_queues,
8068 	.this_id		= -1,
8069 	.cmd_per_lun		= 3,
8070 	.sg_tablesize		= SG_ALL,
8071 
8072 	.max_sectors		= 0xFFFF,
8073 	.shost_groups		= qla2x00_host_groups,
8074 
8075 	.supported_mode		= MODE_INITIATOR,
8076 	.track_queue_depth	= 1,
8077 	.cmd_size		= sizeof(srb_t),
8078 };
8079 
8080 static const struct pci_error_handlers qla2xxx_err_handler = {
8081 	.error_detected = qla2xxx_pci_error_detected,
8082 	.mmio_enabled = qla2xxx_pci_mmio_enabled,
8083 	.slot_reset = qla2xxx_pci_slot_reset,
8084 	.resume = qla2xxx_pci_resume,
8085 	.reset_prepare = qla_pci_reset_prepare,
8086 	.reset_done = qla_pci_reset_done,
8087 };
8088 
8089 static struct pci_device_id qla2xxx_pci_tbl[] = {
8090 	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2100) },
8091 	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2200) },
8092 	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2300) },
8093 	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2312) },
8094 	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2322) },
8095 	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP6312) },
8096 	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP6322) },
8097 	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2422) },
8098 	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2432) },
8099 	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP8432) },
8100 	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP5422) },
8101 	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP5432) },
8102 	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2532) },
8103 	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2031) },
8104 	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP8001) },
8105 	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP8021) },
8106 	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP8031) },
8107 	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISPF001) },
8108 	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP8044) },
8109 	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2071) },
8110 	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2271) },
8111 	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2261) },
8112 	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2061) },
8113 	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2081) },
8114 	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2281) },
8115 	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2089) },
8116 	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2289) },
8117 	{ 0 },
8118 };
8119 MODULE_DEVICE_TABLE(pci, qla2xxx_pci_tbl);
8120 
8121 static struct pci_driver qla2xxx_pci_driver = {
8122 	.name		= QLA2XXX_DRIVER_NAME,
8123 	.driver		= {
8124 		.owner		= THIS_MODULE,
8125 	},
8126 	.id_table	= qla2xxx_pci_tbl,
8127 	.probe		= qla2x00_probe_one,
8128 	.remove		= qla2x00_remove_one,
8129 	.shutdown	= qla2x00_shutdown,
8130 	.err_handler	= &qla2xxx_err_handler,
8131 };
8132 
8133 static const struct file_operations apidev_fops = {
8134 	.owner = THIS_MODULE,
8135 	.llseek = noop_llseek,
8136 };
8137 
8138 /**
8139  * qla2x00_module_init - Module initialization.
8140  **/
8141 static int __init
8142 qla2x00_module_init(void)
8143 {
8144 	int ret = 0;
8145 
8146 	BUILD_BUG_ON(sizeof(cmd_a64_entry_t) != 64);
8147 	BUILD_BUG_ON(sizeof(cmd_entry_t) != 64);
8148 	BUILD_BUG_ON(sizeof(cont_a64_entry_t) != 64);
8149 	BUILD_BUG_ON(sizeof(cont_entry_t) != 64);
8150 	BUILD_BUG_ON(sizeof(init_cb_t) != 96);
8151 	BUILD_BUG_ON(sizeof(mrk_entry_t) != 64);
8152 	BUILD_BUG_ON(sizeof(ms_iocb_entry_t) != 64);
8153 	BUILD_BUG_ON(sizeof(request_t) != 64);
8154 	BUILD_BUG_ON(sizeof(struct abort_entry_24xx) != 64);
8155 	BUILD_BUG_ON(sizeof(struct abort_iocb_entry_fx00) != 64);
8156 	BUILD_BUG_ON(sizeof(struct abts_entry_24xx) != 64);
8157 	BUILD_BUG_ON(sizeof(struct access_chip_84xx) != 64);
8158 	BUILD_BUG_ON(sizeof(struct access_chip_rsp_84xx) != 64);
8159 	BUILD_BUG_ON(sizeof(struct cmd_bidir) != 64);
8160 	BUILD_BUG_ON(sizeof(struct cmd_nvme) != 64);
8161 	BUILD_BUG_ON(sizeof(struct cmd_type_6) != 64);
8162 	BUILD_BUG_ON(sizeof(struct cmd_type_7) != 64);
8163 	BUILD_BUG_ON(sizeof(struct cmd_type_7_fx00) != 64);
8164 	BUILD_BUG_ON(sizeof(struct cmd_type_crc_2) != 64);
8165 	BUILD_BUG_ON(sizeof(struct ct_entry_24xx) != 64);
8166 	BUILD_BUG_ON(sizeof(struct ct_fdmi1_hba_attributes) != 2604);
8167 	BUILD_BUG_ON(sizeof(struct ct_fdmi2_hba_attributes) != 4424);
8168 	BUILD_BUG_ON(sizeof(struct ct_fdmi2_port_attributes) != 4164);
8169 	BUILD_BUG_ON(sizeof(struct ct_fdmi_hba_attr) != 260);
8170 	BUILD_BUG_ON(sizeof(struct ct_fdmi_port_attr) != 260);
8171 	BUILD_BUG_ON(sizeof(struct ct_rsp_hdr) != 16);
8172 	BUILD_BUG_ON(sizeof(struct ctio_crc2_to_fw) != 64);
8173 	BUILD_BUG_ON(sizeof(struct device_reg_24xx) != 256);
8174 	BUILD_BUG_ON(sizeof(struct device_reg_25xxmq) != 24);
8175 	BUILD_BUG_ON(sizeof(struct device_reg_2xxx) != 256);
8176 	BUILD_BUG_ON(sizeof(struct device_reg_82xx) != 1288);
8177 	BUILD_BUG_ON(sizeof(struct device_reg_fx00) != 216);
8178 	BUILD_BUG_ON(sizeof(struct els_entry_24xx) != 64);
8179 	BUILD_BUG_ON(sizeof(struct els_sts_entry_24xx) != 64);
8180 	BUILD_BUG_ON(sizeof(struct fxdisc_entry_fx00) != 64);
8181 	BUILD_BUG_ON(sizeof(struct imm_ntfy_from_isp) != 64);
8182 	BUILD_BUG_ON(sizeof(struct init_cb_24xx) != 128);
8183 	BUILD_BUG_ON(sizeof(struct init_cb_81xx) != 128);
8184 	BUILD_BUG_ON(sizeof(struct logio_entry_24xx) != 64);
8185 	BUILD_BUG_ON(sizeof(struct mbx_entry) != 64);
8186 	BUILD_BUG_ON(sizeof(struct mid_init_cb_24xx) != 5252);
8187 	BUILD_BUG_ON(sizeof(struct mrk_entry_24xx) != 64);
8188 	BUILD_BUG_ON(sizeof(struct nvram_24xx) != 512);
8189 	BUILD_BUG_ON(sizeof(struct nvram_81xx) != 512);
8190 	BUILD_BUG_ON(sizeof(struct pt_ls4_request) != 64);
8191 	BUILD_BUG_ON(sizeof(struct pt_ls4_rx_unsol) != 64);
8192 	BUILD_BUG_ON(sizeof(struct purex_entry_24xx) != 64);
8193 	BUILD_BUG_ON(sizeof(struct qla2100_fw_dump) != 123634);
8194 	BUILD_BUG_ON(sizeof(struct qla2300_fw_dump) != 136100);
8195 	BUILD_BUG_ON(sizeof(struct qla24xx_fw_dump) != 37976);
8196 	BUILD_BUG_ON(sizeof(struct qla25xx_fw_dump) != 39228);
8197 	BUILD_BUG_ON(sizeof(struct qla2xxx_fce_chain) != 52);
8198 	BUILD_BUG_ON(sizeof(struct qla2xxx_fw_dump) != 136172);
8199 	BUILD_BUG_ON(sizeof(struct qla2xxx_mq_chain) != 524);
8200 	BUILD_BUG_ON(sizeof(struct qla2xxx_mqueue_chain) != 8);
8201 	BUILD_BUG_ON(sizeof(struct qla2xxx_mqueue_header) != 12);
8202 	BUILD_BUG_ON(sizeof(struct qla2xxx_offld_chain) != 24);
8203 	BUILD_BUG_ON(sizeof(struct qla81xx_fw_dump) != 39420);
8204 	BUILD_BUG_ON(sizeof(struct qla82xx_uri_data_desc) != 28);
8205 	BUILD_BUG_ON(sizeof(struct qla82xx_uri_table_desc) != 32);
8206 	BUILD_BUG_ON(sizeof(struct qla83xx_fw_dump) != 51196);
8207 	BUILD_BUG_ON(sizeof(struct qla_fcp_prio_cfg) != FCP_PRIO_CFG_SIZE);
8208 	BUILD_BUG_ON(sizeof(struct qla_fdt_layout) != 128);
8209 	BUILD_BUG_ON(sizeof(struct qla_flt_header) != 8);
8210 	BUILD_BUG_ON(sizeof(struct qla_flt_region) != 16);
8211 	BUILD_BUG_ON(sizeof(struct qla_npiv_entry) != 24);
8212 	BUILD_BUG_ON(sizeof(struct qla_npiv_header) != 16);
8213 	BUILD_BUG_ON(sizeof(struct rdp_rsp_payload) != 336);
8214 	BUILD_BUG_ON(sizeof(struct sns_cmd_pkt) != 2064);
8215 	BUILD_BUG_ON(sizeof(struct sts_entry_24xx) != 64);
8216 	BUILD_BUG_ON(sizeof(struct tsk_mgmt_entry) != 64);
8217 	BUILD_BUG_ON(sizeof(struct tsk_mgmt_entry_fx00) != 64);
8218 	BUILD_BUG_ON(sizeof(struct verify_chip_entry_84xx) != 64);
8219 	BUILD_BUG_ON(sizeof(struct verify_chip_rsp_84xx) != 52);
8220 	BUILD_BUG_ON(sizeof(struct vf_evfp_entry_24xx) != 56);
8221 	BUILD_BUG_ON(sizeof(struct vp_config_entry_24xx) != 64);
8222 	BUILD_BUG_ON(sizeof(struct vp_ctrl_entry_24xx) != 64);
8223 	BUILD_BUG_ON(sizeof(struct vp_rpt_id_entry_24xx) != 64);
8224 	BUILD_BUG_ON(sizeof(sts21_entry_t) != 64);
8225 	BUILD_BUG_ON(sizeof(sts22_entry_t) != 64);
8226 	BUILD_BUG_ON(sizeof(sts_cont_entry_t) != 64);
8227 	BUILD_BUG_ON(sizeof(sts_entry_t) != 64);
8228 	BUILD_BUG_ON(sizeof(sw_info_t) != 32);
8229 	BUILD_BUG_ON(sizeof(target_id_t) != 2);
8230 
8231 	qla_trace_init();
8232 
8233 	/* Allocate cache for SRBs. */
8234 	srb_cachep = kmem_cache_create("qla2xxx_srbs", sizeof(srb_t), 0,
8235 	    SLAB_HWCACHE_ALIGN, NULL);
8236 	if (srb_cachep == NULL) {
8237 		ql_log(ql_log_fatal, NULL, 0x0001,
8238 		    "Unable to allocate SRB cache...Failing load!.\n");
8239 		return -ENOMEM;
8240 	}
8241 
8242 	/* Initialize target kmem_cache and mem_pools */
8243 	ret = qlt_init();
8244 	if (ret < 0) {
8245 		goto destroy_cache;
8246 	} else if (ret > 0) {
8247 		/*
8248 		 * If initiator mode is explictly disabled by qlt_init(),
8249 		 * prevent scsi_transport_fc.c:fc_scsi_scan_rport() from
8250 		 * performing scsi_scan_target() during LOOP UP event.
8251 		 */
8252 		qla2xxx_transport_functions.disable_target_scan = 1;
8253 		qla2xxx_transport_vport_functions.disable_target_scan = 1;
8254 	}
8255 
8256 	/* Derive version string. */
8257 	strcpy(qla2x00_version_str, QLA2XXX_VERSION);
8258 	if (ql2xextended_error_logging)
8259 		strcat(qla2x00_version_str, "-debug");
8260 	if (ql2xextended_error_logging == 1)
8261 		ql2xextended_error_logging = QL_DBG_DEFAULT1_MASK;
8262 
8263 	qla2xxx_transport_template =
8264 	    fc_attach_transport(&qla2xxx_transport_functions);
8265 	if (!qla2xxx_transport_template) {
8266 		ql_log(ql_log_fatal, NULL, 0x0002,
8267 		    "fc_attach_transport failed...Failing load!.\n");
8268 		ret = -ENODEV;
8269 		goto qlt_exit;
8270 	}
8271 
8272 	apidev_major = register_chrdev(0, QLA2XXX_APIDEV, &apidev_fops);
8273 	if (apidev_major < 0) {
8274 		ql_log(ql_log_fatal, NULL, 0x0003,
8275 		    "Unable to register char device %s.\n", QLA2XXX_APIDEV);
8276 	}
8277 
8278 	qla2xxx_transport_vport_template =
8279 	    fc_attach_transport(&qla2xxx_transport_vport_functions);
8280 	if (!qla2xxx_transport_vport_template) {
8281 		ql_log(ql_log_fatal, NULL, 0x0004,
8282 		    "fc_attach_transport vport failed...Failing load!.\n");
8283 		ret = -ENODEV;
8284 		goto unreg_chrdev;
8285 	}
8286 	ql_log(ql_log_info, NULL, 0x0005,
8287 	    "QLogic Fibre Channel HBA Driver: %s.\n",
8288 	    qla2x00_version_str);
8289 	ret = pci_register_driver(&qla2xxx_pci_driver);
8290 	if (ret) {
8291 		ql_log(ql_log_fatal, NULL, 0x0006,
8292 		    "pci_register_driver failed...ret=%d Failing load!.\n",
8293 		    ret);
8294 		goto release_vport_transport;
8295 	}
8296 	return ret;
8297 
8298 release_vport_transport:
8299 	fc_release_transport(qla2xxx_transport_vport_template);
8300 
8301 unreg_chrdev:
8302 	if (apidev_major >= 0)
8303 		unregister_chrdev(apidev_major, QLA2XXX_APIDEV);
8304 	fc_release_transport(qla2xxx_transport_template);
8305 
8306 qlt_exit:
8307 	qlt_exit();
8308 
8309 destroy_cache:
8310 	kmem_cache_destroy(srb_cachep);
8311 
8312 	qla_trace_uninit();
8313 	return ret;
8314 }
8315 
8316 /**
8317  * qla2x00_module_exit - Module cleanup.
8318  **/
8319 static void __exit
8320 qla2x00_module_exit(void)
8321 {
8322 	pci_unregister_driver(&qla2xxx_pci_driver);
8323 	qla2x00_release_firmware();
8324 	kmem_cache_destroy(ctx_cachep);
8325 	fc_release_transport(qla2xxx_transport_vport_template);
8326 	if (apidev_major >= 0)
8327 		unregister_chrdev(apidev_major, QLA2XXX_APIDEV);
8328 	fc_release_transport(qla2xxx_transport_template);
8329 	qlt_exit();
8330 	kmem_cache_destroy(srb_cachep);
8331 	qla_trace_uninit();
8332 }
8333 
8334 module_init(qla2x00_module_init);
8335 module_exit(qla2x00_module_exit);
8336 
8337 MODULE_AUTHOR("QLogic Corporation");
8338 MODULE_DESCRIPTION("QLogic Fibre Channel HBA Driver");
8339 MODULE_LICENSE("GPL");
8340 MODULE_FIRMWARE(FW_FILE_ISP21XX);
8341 MODULE_FIRMWARE(FW_FILE_ISP22XX);
8342 MODULE_FIRMWARE(FW_FILE_ISP2300);
8343 MODULE_FIRMWARE(FW_FILE_ISP2322);
8344 MODULE_FIRMWARE(FW_FILE_ISP24XX);
8345 MODULE_FIRMWARE(FW_FILE_ISP25XX);
8346