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