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