Merge tag 'scsi-misc' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[sfrench/cifs-2.6.git] / drivers / scsi / qla2xxx / qla_os.c
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 <linux/refcount.h>
18
19 #include <scsi/scsi_tcq.h>
20 #include <scsi/scsicam.h>
21 #include <scsi/scsi_transport.h>
22 #include <scsi/scsi_transport_fc.h>
23
24 #include "qla_target.h"
25
26 /*
27  * Driver version
28  */
29 char qla2x00_version_str[40];
30
31 static int apidev_major;
32
33 /*
34  * SRB allocation cache
35  */
36 struct kmem_cache *srb_cachep;
37
38 /*
39  * CT6 CTX allocation cache
40  */
41 static struct kmem_cache *ctx_cachep;
42 /*
43  * error level for logging
44  */
45 int ql_errlev = ql_log_all;
46
47 static int ql2xenableclass2;
48 module_param(ql2xenableclass2, int, S_IRUGO|S_IRUSR);
49 MODULE_PARM_DESC(ql2xenableclass2,
50                 "Specify if Class 2 operations are supported from the very "
51                 "beginning. Default is 0 - class 2 not supported.");
52
53
54 int ql2xlogintimeout = 20;
55 module_param(ql2xlogintimeout, int, S_IRUGO);
56 MODULE_PARM_DESC(ql2xlogintimeout,
57                 "Login timeout value in seconds.");
58
59 int qlport_down_retry;
60 module_param(qlport_down_retry, int, S_IRUGO);
61 MODULE_PARM_DESC(qlport_down_retry,
62                 "Maximum number of command retries to a port that returns "
63                 "a PORT-DOWN status.");
64
65 int ql2xplogiabsentdevice;
66 module_param(ql2xplogiabsentdevice, int, S_IRUGO|S_IWUSR);
67 MODULE_PARM_DESC(ql2xplogiabsentdevice,
68                 "Option to enable PLOGI to devices that are not present after "
69                 "a Fabric scan.  This is needed for several broken switches. "
70                 "Default is 0 - no PLOGI. 1 - perfom PLOGI.");
71
72 int ql2xloginretrycount = 0;
73 module_param(ql2xloginretrycount, int, S_IRUGO);
74 MODULE_PARM_DESC(ql2xloginretrycount,
75                 "Specify an alternate value for the NVRAM login retry count.");
76
77 int ql2xallocfwdump = 1;
78 module_param(ql2xallocfwdump, int, S_IRUGO);
79 MODULE_PARM_DESC(ql2xallocfwdump,
80                 "Option to enable allocation of memory for a firmware dump "
81                 "during HBA initialization.  Memory allocation requirements "
82                 "vary by ISP type.  Default is 1 - allocate memory.");
83
84 int ql2xextended_error_logging;
85 module_param(ql2xextended_error_logging, int, S_IRUGO|S_IWUSR);
86 module_param_named(logging, ql2xextended_error_logging, int, S_IRUGO|S_IWUSR);
87 MODULE_PARM_DESC(ql2xextended_error_logging,
88                 "Option to enable extended error logging,\n"
89                 "\t\tDefault is 0 - no logging.  0x40000000 - Module Init & Probe.\n"
90                 "\t\t0x20000000 - Mailbox Cmnds. 0x10000000 - Device Discovery.\n"
91                 "\t\t0x08000000 - IO tracing.    0x04000000 - DPC Thread.\n"
92                 "\t\t0x02000000 - Async events.  0x01000000 - Timer routines.\n"
93                 "\t\t0x00800000 - User space.    0x00400000 - Task Management.\n"
94                 "\t\t0x00200000 - AER/EEH.       0x00100000 - Multi Q.\n"
95                 "\t\t0x00080000 - P3P Specific.  0x00040000 - Virtual Port.\n"
96                 "\t\t0x00020000 - Buffer Dump.   0x00010000 - Misc.\n"
97                 "\t\t0x00008000 - Verbose.       0x00004000 - Target.\n"
98                 "\t\t0x00002000 - Target Mgmt.   0x00001000 - Target TMF.\n"
99                 "\t\t0x7fffffff - For enabling all logs, can be too many logs.\n"
100                 "\t\t0x1e400000 - Preferred value for capturing essential "
101                 "debug information (equivalent to old "
102                 "ql2xextended_error_logging=1).\n"
103                 "\t\tDo LOGICAL OR of the value to enable more than one level");
104
105 int ql2xshiftctondsd = 6;
106 module_param(ql2xshiftctondsd, int, S_IRUGO);
107 MODULE_PARM_DESC(ql2xshiftctondsd,
108                 "Set to control shifting of command type processing "
109                 "based on total number of SG elements.");
110
111 int ql2xfdmienable=1;
112 module_param(ql2xfdmienable, int, S_IRUGO|S_IWUSR);
113 module_param_named(fdmi, ql2xfdmienable, int, S_IRUGO|S_IWUSR);
114 MODULE_PARM_DESC(ql2xfdmienable,
115                 "Enables FDMI registrations. "
116                 "0 - no FDMI. Default is 1 - perform FDMI.");
117
118 #define MAX_Q_DEPTH     64
119 static int ql2xmaxqdepth = MAX_Q_DEPTH;
120 module_param(ql2xmaxqdepth, int, S_IRUGO|S_IWUSR);
121 MODULE_PARM_DESC(ql2xmaxqdepth,
122                 "Maximum queue depth to set for each LUN. "
123                 "Default is 64.");
124
125 #if (IS_ENABLED(CONFIG_NVME_FC))
126 int ql2xenabledif;
127 #else
128 int ql2xenabledif = 2;
129 #endif
130 module_param(ql2xenabledif, int, S_IRUGO);
131 MODULE_PARM_DESC(ql2xenabledif,
132                 " Enable T10-CRC-DIF:\n"
133                 " Default is 2.\n"
134                 "  0 -- No DIF Support\n"
135                 "  1 -- Enable DIF for all types\n"
136                 "  2 -- Enable DIF for all types, except Type 0.\n");
137
138 #if (IS_ENABLED(CONFIG_NVME_FC))
139 int ql2xnvmeenable = 1;
140 #else
141 int ql2xnvmeenable;
142 #endif
143 module_param(ql2xnvmeenable, int, 0644);
144 MODULE_PARM_DESC(ql2xnvmeenable,
145     "Enables NVME support. "
146     "0 - no NVMe.  Default is Y");
147
148 int ql2xenablehba_err_chk = 2;
149 module_param(ql2xenablehba_err_chk, int, S_IRUGO|S_IWUSR);
150 MODULE_PARM_DESC(ql2xenablehba_err_chk,
151                 " Enable T10-CRC-DIF Error isolation by HBA:\n"
152                 " Default is 2.\n"
153                 "  0 -- Error isolation disabled\n"
154                 "  1 -- Error isolation enabled only for DIX Type 0\n"
155                 "  2 -- Error isolation enabled for all Types\n");
156
157 int ql2xiidmaenable=1;
158 module_param(ql2xiidmaenable, int, S_IRUGO);
159 MODULE_PARM_DESC(ql2xiidmaenable,
160                 "Enables iIDMA settings "
161                 "Default is 1 - perform iIDMA. 0 - no iIDMA.");
162
163 int ql2xmqsupport = 1;
164 module_param(ql2xmqsupport, int, S_IRUGO);
165 MODULE_PARM_DESC(ql2xmqsupport,
166                 "Enable on demand multiple queue pairs support "
167                 "Default is 1 for supported. "
168                 "Set it to 0 to turn off mq qpair support.");
169
170 int ql2xfwloadbin;
171 module_param(ql2xfwloadbin, int, S_IRUGO|S_IWUSR);
172 module_param_named(fwload, ql2xfwloadbin, int, S_IRUGO|S_IWUSR);
173 MODULE_PARM_DESC(ql2xfwloadbin,
174                 "Option to specify location from which to load ISP firmware:.\n"
175                 " 2 -- load firmware via the request_firmware() (hotplug).\n"
176                 "      interface.\n"
177                 " 1 -- load firmware from flash.\n"
178                 " 0 -- use default semantics.\n");
179
180 int ql2xetsenable;
181 module_param(ql2xetsenable, int, S_IRUGO);
182 MODULE_PARM_DESC(ql2xetsenable,
183                 "Enables firmware ETS burst."
184                 "Default is 0 - skip ETS enablement.");
185
186 int ql2xdbwr = 1;
187 module_param(ql2xdbwr, int, S_IRUGO|S_IWUSR);
188 MODULE_PARM_DESC(ql2xdbwr,
189                 "Option to specify scheme for request queue posting.\n"
190                 " 0 -- Regular doorbell.\n"
191                 " 1 -- CAMRAM doorbell (faster).\n");
192
193 int ql2xtargetreset = 1;
194 module_param(ql2xtargetreset, int, S_IRUGO);
195 MODULE_PARM_DESC(ql2xtargetreset,
196                  "Enable target reset."
197                  "Default is 1 - use hw defaults.");
198
199 int ql2xgffidenable;
200 module_param(ql2xgffidenable, int, S_IRUGO);
201 MODULE_PARM_DESC(ql2xgffidenable,
202                 "Enables GFF_ID checks of port type. "
203                 "Default is 0 - Do not use GFF_ID information.");
204
205 int ql2xasynctmfenable = 1;
206 module_param(ql2xasynctmfenable, int, S_IRUGO);
207 MODULE_PARM_DESC(ql2xasynctmfenable,
208                 "Enables issue of TM IOCBs asynchronously via IOCB mechanism"
209                 "Default is 1 - Issue TM IOCBs via mailbox mechanism.");
210
211 int ql2xdontresethba;
212 module_param(ql2xdontresethba, int, S_IRUGO|S_IWUSR);
213 MODULE_PARM_DESC(ql2xdontresethba,
214                 "Option to specify reset behaviour.\n"
215                 " 0 (Default) -- Reset on failure.\n"
216                 " 1 -- Do not reset on failure.\n");
217
218 uint64_t ql2xmaxlun = MAX_LUNS;
219 module_param(ql2xmaxlun, ullong, S_IRUGO);
220 MODULE_PARM_DESC(ql2xmaxlun,
221                 "Defines the maximum LU number to register with the SCSI "
222                 "midlayer. Default is 65535.");
223
224 int ql2xmdcapmask = 0x1F;
225 module_param(ql2xmdcapmask, int, S_IRUGO);
226 MODULE_PARM_DESC(ql2xmdcapmask,
227                 "Set the Minidump driver capture mask level. "
228                 "Default is 0x1F - Can be set to 0x3, 0x7, 0xF, 0x1F, 0x7F.");
229
230 int ql2xmdenable = 1;
231 module_param(ql2xmdenable, int, S_IRUGO);
232 MODULE_PARM_DESC(ql2xmdenable,
233                 "Enable/disable MiniDump. "
234                 "0 - MiniDump disabled. "
235                 "1 (Default) - MiniDump enabled.");
236
237 int ql2xexlogins = 0;
238 module_param(ql2xexlogins, uint, S_IRUGO|S_IWUSR);
239 MODULE_PARM_DESC(ql2xexlogins,
240                  "Number of extended Logins. "
241                  "0 (Default)- Disabled.");
242
243 int ql2xexchoffld = 1024;
244 module_param(ql2xexchoffld, uint, 0644);
245 MODULE_PARM_DESC(ql2xexchoffld,
246         "Number of target exchanges.");
247
248 int ql2xiniexchg = 1024;
249 module_param(ql2xiniexchg, uint, 0644);
250 MODULE_PARM_DESC(ql2xiniexchg,
251         "Number of initiator exchanges.");
252
253 int ql2xfwholdabts = 0;
254 module_param(ql2xfwholdabts, int, S_IRUGO);
255 MODULE_PARM_DESC(ql2xfwholdabts,
256                 "Allow FW to hold status IOCB until ABTS rsp received. "
257                 "0 (Default) Do not set fw option. "
258                 "1 - Set fw option to hold ABTS.");
259
260 int ql2xmvasynctoatio = 1;
261 module_param(ql2xmvasynctoatio, int, S_IRUGO|S_IWUSR);
262 MODULE_PARM_DESC(ql2xmvasynctoatio,
263                 "Move PUREX, ABTS RX and RIDA IOCBs to ATIOQ"
264                 "0 (Default). Do not move IOCBs"
265                 "1 - Move IOCBs.");
266
267 int ql2xautodetectsfp = 1;
268 module_param(ql2xautodetectsfp, int, 0444);
269 MODULE_PARM_DESC(ql2xautodetectsfp,
270                  "Detect SFP range and set appropriate distance.\n"
271                  "1 (Default): Enable\n");
272
273 int ql2xenablemsix = 1;
274 module_param(ql2xenablemsix, int, 0444);
275 MODULE_PARM_DESC(ql2xenablemsix,
276                  "Set to enable MSI or MSI-X interrupt mechanism.\n"
277                  " Default is 1, enable MSI-X interrupt mechanism.\n"
278                  " 0 -- enable traditional pin-based mechanism.\n"
279                  " 1 -- enable MSI-X interrupt mechanism.\n"
280                  " 2 -- enable MSI interrupt mechanism.\n");
281
282 int qla2xuseresexchforels;
283 module_param(qla2xuseresexchforels, int, 0444);
284 MODULE_PARM_DESC(qla2xuseresexchforels,
285                  "Reserve 1/2 of emergency exchanges for ELS.\n"
286                  " 0 (default): disabled");
287
288 /*
289  * SCSI host template entry points
290  */
291 static int qla2xxx_slave_configure(struct scsi_device * device);
292 static int qla2xxx_slave_alloc(struct scsi_device *);
293 static int qla2xxx_scan_finished(struct Scsi_Host *, unsigned long time);
294 static void qla2xxx_scan_start(struct Scsi_Host *);
295 static void qla2xxx_slave_destroy(struct scsi_device *);
296 static int qla2xxx_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmd);
297 static int qla2xxx_eh_abort(struct scsi_cmnd *);
298 static int qla2xxx_eh_device_reset(struct scsi_cmnd *);
299 static int qla2xxx_eh_target_reset(struct scsi_cmnd *);
300 static int qla2xxx_eh_bus_reset(struct scsi_cmnd *);
301 static int qla2xxx_eh_host_reset(struct scsi_cmnd *);
302
303 static void qla2x00_clear_drv_active(struct qla_hw_data *);
304 static void qla2x00_free_device(scsi_qla_host_t *);
305 static int qla2xxx_map_queues(struct Scsi_Host *shost);
306 static void qla2x00_destroy_deferred_work(struct qla_hw_data *);
307
308
309 struct scsi_host_template qla2xxx_driver_template = {
310         .module                 = THIS_MODULE,
311         .name                   = QLA2XXX_DRIVER_NAME,
312         .queuecommand           = qla2xxx_queuecommand,
313
314         .eh_timed_out           = fc_eh_timed_out,
315         .eh_abort_handler       = qla2xxx_eh_abort,
316         .eh_device_reset_handler = qla2xxx_eh_device_reset,
317         .eh_target_reset_handler = qla2xxx_eh_target_reset,
318         .eh_bus_reset_handler   = qla2xxx_eh_bus_reset,
319         .eh_host_reset_handler  = qla2xxx_eh_host_reset,
320
321         .slave_configure        = qla2xxx_slave_configure,
322
323         .slave_alloc            = qla2xxx_slave_alloc,
324         .slave_destroy          = qla2xxx_slave_destroy,
325         .scan_finished          = qla2xxx_scan_finished,
326         .scan_start             = qla2xxx_scan_start,
327         .change_queue_depth     = scsi_change_queue_depth,
328         .map_queues             = qla2xxx_map_queues,
329         .this_id                = -1,
330         .cmd_per_lun            = 3,
331         .use_clustering         = ENABLE_CLUSTERING,
332         .sg_tablesize           = SG_ALL,
333
334         .max_sectors            = 0xFFFF,
335         .shost_attrs            = qla2x00_host_attrs,
336
337         .supported_mode         = MODE_INITIATOR,
338         .track_queue_depth      = 1,
339 };
340
341 static struct scsi_transport_template *qla2xxx_transport_template = NULL;
342 struct scsi_transport_template *qla2xxx_transport_vport_template = NULL;
343
344 /* TODO Convert to inlines
345  *
346  * Timer routines
347  */
348
349 __inline__ void
350 qla2x00_start_timer(scsi_qla_host_t *vha, unsigned long interval)
351 {
352         timer_setup(&vha->timer, qla2x00_timer, 0);
353         vha->timer.expires = jiffies + interval * HZ;
354         add_timer(&vha->timer);
355         vha->timer_active = 1;
356 }
357
358 static inline void
359 qla2x00_restart_timer(scsi_qla_host_t *vha, unsigned long interval)
360 {
361         /* Currently used for 82XX only. */
362         if (vha->device_flags & DFLG_DEV_FAILED) {
363                 ql_dbg(ql_dbg_timer, vha, 0x600d,
364                     "Device in a failed state, returning.\n");
365                 return;
366         }
367
368         mod_timer(&vha->timer, jiffies + interval * HZ);
369 }
370
371 static __inline__ void
372 qla2x00_stop_timer(scsi_qla_host_t *vha)
373 {
374         del_timer_sync(&vha->timer);
375         vha->timer_active = 0;
376 }
377
378 static int qla2x00_do_dpc(void *data);
379
380 static void qla2x00_rst_aen(scsi_qla_host_t *);
381
382 static int qla2x00_mem_alloc(struct qla_hw_data *, uint16_t, uint16_t,
383         struct req_que **, struct rsp_que **);
384 static void qla2x00_free_fw_dump(struct qla_hw_data *);
385 static void qla2x00_mem_free(struct qla_hw_data *);
386 int qla2xxx_mqueuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd,
387         struct qla_qpair *qpair);
388
389 /* -------------------------------------------------------------------------- */
390 static void qla_init_base_qpair(struct scsi_qla_host *vha, struct req_que *req,
391     struct rsp_que *rsp)
392 {
393         struct qla_hw_data *ha = vha->hw;
394         rsp->qpair = ha->base_qpair;
395         rsp->req = req;
396         ha->base_qpair->hw = ha;
397         ha->base_qpair->req = req;
398         ha->base_qpair->rsp = rsp;
399         ha->base_qpair->vha = vha;
400         ha->base_qpair->qp_lock_ptr = &ha->hardware_lock;
401         ha->base_qpair->use_shadow_reg = IS_SHADOW_REG_CAPABLE(ha) ? 1 : 0;
402         ha->base_qpair->msix = &ha->msix_entries[QLA_MSIX_RSP_Q];
403         ha->base_qpair->srb_mempool = ha->srb_mempool;
404         INIT_LIST_HEAD(&ha->base_qpair->hints_list);
405         ha->base_qpair->enable_class_2 = ql2xenableclass2;
406         /* init qpair to this cpu. Will adjust at run time. */
407         qla_cpu_update(rsp->qpair, raw_smp_processor_id());
408         ha->base_qpair->pdev = ha->pdev;
409
410         if (IS_QLA27XX(ha) || IS_QLA83XX(ha))
411                 ha->base_qpair->reqq_start_iocbs = qla_83xx_start_iocbs;
412 }
413
414 static int qla2x00_alloc_queues(struct qla_hw_data *ha, struct req_que *req,
415                                 struct rsp_que *rsp)
416 {
417         scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
418         ha->req_q_map = kcalloc(ha->max_req_queues, sizeof(struct req_que *),
419                                 GFP_KERNEL);
420         if (!ha->req_q_map) {
421                 ql_log(ql_log_fatal, vha, 0x003b,
422                     "Unable to allocate memory for request queue ptrs.\n");
423                 goto fail_req_map;
424         }
425
426         ha->rsp_q_map = kcalloc(ha->max_rsp_queues, sizeof(struct rsp_que *),
427                                 GFP_KERNEL);
428         if (!ha->rsp_q_map) {
429                 ql_log(ql_log_fatal, vha, 0x003c,
430                     "Unable to allocate memory for response queue ptrs.\n");
431                 goto fail_rsp_map;
432         }
433
434         ha->base_qpair = kzalloc(sizeof(struct qla_qpair), GFP_KERNEL);
435         if (ha->base_qpair == NULL) {
436                 ql_log(ql_log_warn, vha, 0x00e0,
437                     "Failed to allocate base queue pair memory.\n");
438                 goto fail_base_qpair;
439         }
440
441         qla_init_base_qpair(vha, req, rsp);
442
443         if ((ql2xmqsupport || ql2xnvmeenable) && ha->max_qpairs) {
444                 ha->queue_pair_map = kcalloc(ha->max_qpairs, sizeof(struct qla_qpair *),
445                         GFP_KERNEL);
446                 if (!ha->queue_pair_map) {
447                         ql_log(ql_log_fatal, vha, 0x0180,
448                             "Unable to allocate memory for queue pair ptrs.\n");
449                         goto fail_qpair_map;
450                 }
451         }
452
453         /*
454          * Make sure we record at least the request and response queue zero in
455          * case we need to free them if part of the probe fails.
456          */
457         ha->rsp_q_map[0] = rsp;
458         ha->req_q_map[0] = req;
459         set_bit(0, ha->rsp_qid_map);
460         set_bit(0, ha->req_qid_map);
461         return 0;
462
463 fail_qpair_map:
464         kfree(ha->base_qpair);
465         ha->base_qpair = NULL;
466 fail_base_qpair:
467         kfree(ha->rsp_q_map);
468         ha->rsp_q_map = NULL;
469 fail_rsp_map:
470         kfree(ha->req_q_map);
471         ha->req_q_map = NULL;
472 fail_req_map:
473         return -ENOMEM;
474 }
475
476 static void qla2x00_free_req_que(struct qla_hw_data *ha, struct req_que *req)
477 {
478         if (IS_QLAFX00(ha)) {
479                 if (req && req->ring_fx00)
480                         dma_free_coherent(&ha->pdev->dev,
481                             (req->length_fx00 + 1) * sizeof(request_t),
482                             req->ring_fx00, req->dma_fx00);
483         } else if (req && req->ring)
484                 dma_free_coherent(&ha->pdev->dev,
485                 (req->length + 1) * sizeof(request_t),
486                 req->ring, req->dma);
487
488         if (req)
489                 kfree(req->outstanding_cmds);
490
491         kfree(req);
492 }
493
494 static void qla2x00_free_rsp_que(struct qla_hw_data *ha, struct rsp_que *rsp)
495 {
496         if (IS_QLAFX00(ha)) {
497                 if (rsp && rsp->ring_fx00)
498                         dma_free_coherent(&ha->pdev->dev,
499                             (rsp->length_fx00 + 1) * sizeof(request_t),
500                             rsp->ring_fx00, rsp->dma_fx00);
501         } else if (rsp && rsp->ring) {
502                 dma_free_coherent(&ha->pdev->dev,
503                 (rsp->length + 1) * sizeof(response_t),
504                 rsp->ring, rsp->dma);
505         }
506         kfree(rsp);
507 }
508
509 static void qla2x00_free_queues(struct qla_hw_data *ha)
510 {
511         struct req_que *req;
512         struct rsp_que *rsp;
513         int cnt;
514         unsigned long flags;
515
516         if (ha->queue_pair_map) {
517                 kfree(ha->queue_pair_map);
518                 ha->queue_pair_map = NULL;
519         }
520         if (ha->base_qpair) {
521                 kfree(ha->base_qpair);
522                 ha->base_qpair = NULL;
523         }
524
525         spin_lock_irqsave(&ha->hardware_lock, flags);
526         for (cnt = 0; cnt < ha->max_req_queues; cnt++) {
527                 if (!test_bit(cnt, ha->req_qid_map))
528                         continue;
529
530                 req = ha->req_q_map[cnt];
531                 clear_bit(cnt, ha->req_qid_map);
532                 ha->req_q_map[cnt] = NULL;
533
534                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
535                 qla2x00_free_req_que(ha, req);
536                 spin_lock_irqsave(&ha->hardware_lock, flags);
537         }
538         spin_unlock_irqrestore(&ha->hardware_lock, flags);
539
540         kfree(ha->req_q_map);
541         ha->req_q_map = NULL;
542
543
544         spin_lock_irqsave(&ha->hardware_lock, flags);
545         for (cnt = 0; cnt < ha->max_rsp_queues; cnt++) {
546                 if (!test_bit(cnt, ha->rsp_qid_map))
547                         continue;
548
549                 rsp = ha->rsp_q_map[cnt];
550                 clear_bit(cnt, ha->rsp_qid_map);
551                 ha->rsp_q_map[cnt] =  NULL;
552                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
553                 qla2x00_free_rsp_que(ha, rsp);
554                 spin_lock_irqsave(&ha->hardware_lock, flags);
555         }
556         spin_unlock_irqrestore(&ha->hardware_lock, flags);
557
558         kfree(ha->rsp_q_map);
559         ha->rsp_q_map = NULL;
560 }
561
562 static char *
563 qla2x00_pci_info_str(struct scsi_qla_host *vha, char *str)
564 {
565         struct qla_hw_data *ha = vha->hw;
566         static char *pci_bus_modes[] = {
567                 "33", "66", "100", "133",
568         };
569         uint16_t pci_bus;
570
571         strcpy(str, "PCI");
572         pci_bus = (ha->pci_attr & (BIT_9 | BIT_10)) >> 9;
573         if (pci_bus) {
574                 strcat(str, "-X (");
575                 strcat(str, pci_bus_modes[pci_bus]);
576         } else {
577                 pci_bus = (ha->pci_attr & BIT_8) >> 8;
578                 strcat(str, " (");
579                 strcat(str, pci_bus_modes[pci_bus]);
580         }
581         strcat(str, " MHz)");
582
583         return (str);
584 }
585
586 static char *
587 qla24xx_pci_info_str(struct scsi_qla_host *vha, char *str)
588 {
589         static char *pci_bus_modes[] = { "33", "66", "100", "133", };
590         struct qla_hw_data *ha = vha->hw;
591         uint32_t pci_bus;
592
593         if (pci_is_pcie(ha->pdev)) {
594                 char lwstr[6];
595                 uint32_t lstat, lspeed, lwidth;
596
597                 pcie_capability_read_dword(ha->pdev, PCI_EXP_LNKCAP, &lstat);
598                 lspeed = lstat & PCI_EXP_LNKCAP_SLS;
599                 lwidth = (lstat & PCI_EXP_LNKCAP_MLW) >> 4;
600
601                 strcpy(str, "PCIe (");
602                 switch (lspeed) {
603                 case 1:
604                         strcat(str, "2.5GT/s ");
605                         break;
606                 case 2:
607                         strcat(str, "5.0GT/s ");
608                         break;
609                 case 3:
610                         strcat(str, "8.0GT/s ");
611                         break;
612                 default:
613                         strcat(str, "<unknown> ");
614                         break;
615                 }
616                 snprintf(lwstr, sizeof(lwstr), "x%d)", lwidth);
617                 strcat(str, lwstr);
618
619                 return str;
620         }
621
622         strcpy(str, "PCI");
623         pci_bus = (ha->pci_attr & CSRX_PCIX_BUS_MODE_MASK) >> 8;
624         if (pci_bus == 0 || pci_bus == 8) {
625                 strcat(str, " (");
626                 strcat(str, pci_bus_modes[pci_bus >> 3]);
627         } else {
628                 strcat(str, "-X ");
629                 if (pci_bus & BIT_2)
630                         strcat(str, "Mode 2");
631                 else
632                         strcat(str, "Mode 1");
633                 strcat(str, " (");
634                 strcat(str, pci_bus_modes[pci_bus & ~BIT_2]);
635         }
636         strcat(str, " MHz)");
637
638         return str;
639 }
640
641 static char *
642 qla2x00_fw_version_str(struct scsi_qla_host *vha, char *str, size_t size)
643 {
644         char un_str[10];
645         struct qla_hw_data *ha = vha->hw;
646
647         snprintf(str, size, "%d.%02d.%02d ", ha->fw_major_version,
648             ha->fw_minor_version, ha->fw_subminor_version);
649
650         if (ha->fw_attributes & BIT_9) {
651                 strcat(str, "FLX");
652                 return (str);
653         }
654
655         switch (ha->fw_attributes & 0xFF) {
656         case 0x7:
657                 strcat(str, "EF");
658                 break;
659         case 0x17:
660                 strcat(str, "TP");
661                 break;
662         case 0x37:
663                 strcat(str, "IP");
664                 break;
665         case 0x77:
666                 strcat(str, "VI");
667                 break;
668         default:
669                 sprintf(un_str, "(%x)", ha->fw_attributes);
670                 strcat(str, un_str);
671                 break;
672         }
673         if (ha->fw_attributes & 0x100)
674                 strcat(str, "X");
675
676         return (str);
677 }
678
679 static char *
680 qla24xx_fw_version_str(struct scsi_qla_host *vha, char *str, size_t size)
681 {
682         struct qla_hw_data *ha = vha->hw;
683
684         snprintf(str, size, "%d.%02d.%02d (%x)", ha->fw_major_version,
685             ha->fw_minor_version, ha->fw_subminor_version, ha->fw_attributes);
686         return str;
687 }
688
689 void
690 qla2x00_sp_free_dma(void *ptr)
691 {
692         srb_t *sp = ptr;
693         struct qla_hw_data *ha = sp->vha->hw;
694         struct scsi_cmnd *cmd = GET_CMD_SP(sp);
695         void *ctx = GET_CMD_CTX_SP(sp);
696
697         if (sp->flags & SRB_DMA_VALID) {
698                 scsi_dma_unmap(cmd);
699                 sp->flags &= ~SRB_DMA_VALID;
700         }
701
702         if (sp->flags & SRB_CRC_PROT_DMA_VALID) {
703                 dma_unmap_sg(&ha->pdev->dev, scsi_prot_sglist(cmd),
704                     scsi_prot_sg_count(cmd), cmd->sc_data_direction);
705                 sp->flags &= ~SRB_CRC_PROT_DMA_VALID;
706         }
707
708         if (!ctx)
709                 goto end;
710
711         if (sp->flags & SRB_CRC_CTX_DSD_VALID) {
712                 /* List assured to be having elements */
713                 qla2x00_clean_dsd_pool(ha, ctx);
714                 sp->flags &= ~SRB_CRC_CTX_DSD_VALID;
715         }
716
717         if (sp->flags & SRB_CRC_CTX_DMA_VALID) {
718                 struct crc_context *ctx0 = ctx;
719
720                 dma_pool_free(ha->dl_dma_pool, ctx0, ctx0->crc_ctx_dma);
721                 sp->flags &= ~SRB_CRC_CTX_DMA_VALID;
722         }
723
724         if (sp->flags & SRB_FCP_CMND_DMA_VALID) {
725                 struct ct6_dsd *ctx1 = ctx;
726
727                 dma_pool_free(ha->fcp_cmnd_dma_pool, ctx1->fcp_cmnd,
728                     ctx1->fcp_cmnd_dma);
729                 list_splice(&ctx1->dsd_list, &ha->gbl_dsd_list);
730                 ha->gbl_dsd_inuse -= ctx1->dsd_use_cnt;
731                 ha->gbl_dsd_avail += ctx1->dsd_use_cnt;
732                 mempool_free(ctx1, ha->ctx_mempool);
733         }
734
735 end:
736         if (sp->type != SRB_NVME_CMD && sp->type != SRB_NVME_LS) {
737                 CMD_SP(cmd) = NULL;
738                 qla2x00_rel_sp(sp);
739         }
740 }
741
742 void
743 qla2x00_sp_compl(void *ptr, int res)
744 {
745         srb_t *sp = ptr;
746         struct scsi_cmnd *cmd = GET_CMD_SP(sp);
747
748         cmd->result = res;
749
750         if (atomic_read(&sp->ref_count) == 0) {
751                 ql_dbg(ql_dbg_io, sp->vha, 0x3015,
752                     "SP reference-count to ZERO -- sp=%p cmd=%p.\n",
753                     sp, GET_CMD_SP(sp));
754                 if (ql2xextended_error_logging & ql_dbg_io)
755                         WARN_ON(atomic_read(&sp->ref_count) == 0);
756                 return;
757         }
758         if (!atomic_dec_and_test(&sp->ref_count))
759                 return;
760
761         sp->free(sp);
762         cmd->scsi_done(cmd);
763 }
764
765 void
766 qla2xxx_qpair_sp_free_dma(void *ptr)
767 {
768         srb_t *sp = (srb_t *)ptr;
769         struct scsi_cmnd *cmd = GET_CMD_SP(sp);
770         struct qla_hw_data *ha = sp->fcport->vha->hw;
771         void *ctx = GET_CMD_CTX_SP(sp);
772
773         if (sp->flags & SRB_DMA_VALID) {
774                 scsi_dma_unmap(cmd);
775                 sp->flags &= ~SRB_DMA_VALID;
776         }
777
778         if (sp->flags & SRB_CRC_PROT_DMA_VALID) {
779                 dma_unmap_sg(&ha->pdev->dev, scsi_prot_sglist(cmd),
780                     scsi_prot_sg_count(cmd), cmd->sc_data_direction);
781                 sp->flags &= ~SRB_CRC_PROT_DMA_VALID;
782         }
783
784         if (!ctx)
785                 goto end;
786
787         if (sp->flags & SRB_CRC_CTX_DSD_VALID) {
788                 /* List assured to be having elements */
789                 qla2x00_clean_dsd_pool(ha, ctx);
790                 sp->flags &= ~SRB_CRC_CTX_DSD_VALID;
791         }
792
793         if (sp->flags & SRB_CRC_CTX_DMA_VALID) {
794                 struct crc_context *ctx0 = ctx;
795
796                 dma_pool_free(ha->dl_dma_pool, ctx, ctx0->crc_ctx_dma);
797                 sp->flags &= ~SRB_CRC_CTX_DMA_VALID;
798         }
799
800         if (sp->flags & SRB_FCP_CMND_DMA_VALID) {
801                 struct ct6_dsd *ctx1 = ctx;
802                 dma_pool_free(ha->fcp_cmnd_dma_pool, ctx1->fcp_cmnd,
803                     ctx1->fcp_cmnd_dma);
804                 list_splice(&ctx1->dsd_list, &ha->gbl_dsd_list);
805                 ha->gbl_dsd_inuse -= ctx1->dsd_use_cnt;
806                 ha->gbl_dsd_avail += ctx1->dsd_use_cnt;
807                 mempool_free(ctx1, ha->ctx_mempool);
808         }
809 end:
810         CMD_SP(cmd) = NULL;
811         qla2xxx_rel_qpair_sp(sp->qpair, sp);
812 }
813
814 void
815 qla2xxx_qpair_sp_compl(void *ptr, int res)
816 {
817         srb_t *sp = ptr;
818         struct scsi_cmnd *cmd = GET_CMD_SP(sp);
819
820         cmd->result = res;
821
822         if (atomic_read(&sp->ref_count) == 0) {
823                 ql_dbg(ql_dbg_io, sp->fcport->vha, 0x3079,
824                     "SP reference-count to ZERO -- sp=%p cmd=%p.\n",
825                     sp, GET_CMD_SP(sp));
826                 if (ql2xextended_error_logging & ql_dbg_io)
827                         WARN_ON(atomic_read(&sp->ref_count) == 0);
828                 return;
829         }
830         if (!atomic_dec_and_test(&sp->ref_count))
831                 return;
832
833         sp->free(sp);
834         cmd->scsi_done(cmd);
835 }
836
837 /* If we are SP1 here, we need to still take and release the host_lock as SP1
838  * does not have the changes necessary to avoid taking host->host_lock.
839  */
840 static int
841 qla2xxx_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
842 {
843         scsi_qla_host_t *vha = shost_priv(host);
844         fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
845         struct fc_rport *rport = starget_to_rport(scsi_target(cmd->device));
846         struct qla_hw_data *ha = vha->hw;
847         struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
848         srb_t *sp;
849         int rval;
850         struct qla_qpair *qpair = NULL;
851         uint32_t tag;
852         uint16_t hwq;
853
854         if (unlikely(test_bit(UNLOADING, &base_vha->dpc_flags))) {
855                 cmd->result = DID_NO_CONNECT << 16;
856                 goto qc24_fail_command;
857         }
858
859         if (ha->mqenable) {
860                 if (shost_use_blk_mq(vha->host)) {
861                         tag = blk_mq_unique_tag(cmd->request);
862                         hwq = blk_mq_unique_tag_to_hwq(tag);
863                         qpair = ha->queue_pair_map[hwq];
864                 } else if (vha->vp_idx && vha->qpair) {
865                         qpair = vha->qpair;
866                 }
867
868                 if (qpair)
869                         return qla2xxx_mqueuecommand(host, cmd, qpair);
870         }
871
872         if (ha->flags.eeh_busy) {
873                 if (ha->flags.pci_channel_io_perm_failure) {
874                         ql_dbg(ql_dbg_aer, vha, 0x9010,
875                             "PCI Channel IO permanent failure, exiting "
876                             "cmd=%p.\n", cmd);
877                         cmd->result = DID_NO_CONNECT << 16;
878                 } else {
879                         ql_dbg(ql_dbg_aer, vha, 0x9011,
880                             "EEH_Busy, Requeuing the cmd=%p.\n", cmd);
881                         cmd->result = DID_REQUEUE << 16;
882                 }
883                 goto qc24_fail_command;
884         }
885
886         rval = fc_remote_port_chkready(rport);
887         if (rval) {
888                 cmd->result = rval;
889                 ql_dbg(ql_dbg_io + ql_dbg_verbose, vha, 0x3003,
890                     "fc_remote_port_chkready failed for cmd=%p, rval=0x%x.\n",
891                     cmd, rval);
892                 goto qc24_fail_command;
893         }
894
895         if (!vha->flags.difdix_supported &&
896                 scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
897                         ql_dbg(ql_dbg_io, vha, 0x3004,
898                             "DIF Cap not reg, fail DIF capable cmd's:%p.\n",
899                             cmd);
900                         cmd->result = DID_NO_CONNECT << 16;
901                         goto qc24_fail_command;
902         }
903
904         if (!fcport) {
905                 cmd->result = DID_NO_CONNECT << 16;
906                 goto qc24_fail_command;
907         }
908
909         if (atomic_read(&fcport->state) != FCS_ONLINE) {
910                 if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD ||
911                         atomic_read(&base_vha->loop_state) == LOOP_DEAD) {
912                         ql_dbg(ql_dbg_io, vha, 0x3005,
913                             "Returning DNC, fcport_state=%d loop_state=%d.\n",
914                             atomic_read(&fcport->state),
915                             atomic_read(&base_vha->loop_state));
916                         cmd->result = DID_NO_CONNECT << 16;
917                         goto qc24_fail_command;
918                 }
919                 goto qc24_target_busy;
920         }
921
922         /*
923          * Return target busy if we've received a non-zero retry_delay_timer
924          * in a FCP_RSP.
925          */
926         if (fcport->retry_delay_timestamp == 0) {
927                 /* retry delay not set */
928         } else if (time_after(jiffies, fcport->retry_delay_timestamp))
929                 fcport->retry_delay_timestamp = 0;
930         else
931                 goto qc24_target_busy;
932
933         sp = qla2x00_get_sp(vha, fcport, GFP_ATOMIC);
934         if (!sp)
935                 goto qc24_host_busy;
936
937         sp->u.scmd.cmd = cmd;
938         sp->type = SRB_SCSI_CMD;
939         atomic_set(&sp->ref_count, 1);
940         CMD_SP(cmd) = (void *)sp;
941         sp->free = qla2x00_sp_free_dma;
942         sp->done = qla2x00_sp_compl;
943
944         rval = ha->isp_ops->start_scsi(sp);
945         if (rval != QLA_SUCCESS) {
946                 ql_dbg(ql_dbg_io + ql_dbg_verbose, vha, 0x3013,
947                     "Start scsi failed rval=%d for cmd=%p.\n", rval, cmd);
948                 goto qc24_host_busy_free_sp;
949         }
950
951         return 0;
952
953 qc24_host_busy_free_sp:
954         sp->free(sp);
955
956 qc24_host_busy:
957         return SCSI_MLQUEUE_HOST_BUSY;
958
959 qc24_target_busy:
960         return SCSI_MLQUEUE_TARGET_BUSY;
961
962 qc24_fail_command:
963         cmd->scsi_done(cmd);
964
965         return 0;
966 }
967
968 /* For MQ supported I/O */
969 int
970 qla2xxx_mqueuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd,
971     struct qla_qpair *qpair)
972 {
973         scsi_qla_host_t *vha = shost_priv(host);
974         fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
975         struct fc_rport *rport = starget_to_rport(scsi_target(cmd->device));
976         struct qla_hw_data *ha = vha->hw;
977         struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
978         srb_t *sp;
979         int rval;
980
981         rval = fc_remote_port_chkready(rport);
982         if (rval) {
983                 cmd->result = rval;
984                 ql_dbg(ql_dbg_io + ql_dbg_verbose, vha, 0x3076,
985                     "fc_remote_port_chkready failed for cmd=%p, rval=0x%x.\n",
986                     cmd, rval);
987                 goto qc24_fail_command;
988         }
989
990         if (!fcport) {
991                 cmd->result = DID_NO_CONNECT << 16;
992                 goto qc24_fail_command;
993         }
994
995         if (atomic_read(&fcport->state) != FCS_ONLINE) {
996                 if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD ||
997                         atomic_read(&base_vha->loop_state) == LOOP_DEAD) {
998                         ql_dbg(ql_dbg_io, vha, 0x3077,
999                             "Returning DNC, fcport_state=%d loop_state=%d.\n",
1000                             atomic_read(&fcport->state),
1001                             atomic_read(&base_vha->loop_state));
1002                         cmd->result = DID_NO_CONNECT << 16;
1003                         goto qc24_fail_command;
1004                 }
1005                 goto qc24_target_busy;
1006         }
1007
1008         /*
1009          * Return target busy if we've received a non-zero retry_delay_timer
1010          * in a FCP_RSP.
1011          */
1012         if (fcport->retry_delay_timestamp == 0) {
1013                 /* retry delay not set */
1014         } else if (time_after(jiffies, fcport->retry_delay_timestamp))
1015                 fcport->retry_delay_timestamp = 0;
1016         else
1017                 goto qc24_target_busy;
1018
1019         sp = qla2xxx_get_qpair_sp(vha, qpair, fcport, GFP_ATOMIC);
1020         if (!sp)
1021                 goto qc24_host_busy;
1022
1023         sp->u.scmd.cmd = cmd;
1024         sp->type = SRB_SCSI_CMD;
1025         atomic_set(&sp->ref_count, 1);
1026         CMD_SP(cmd) = (void *)sp;
1027         sp->free = qla2xxx_qpair_sp_free_dma;
1028         sp->done = qla2xxx_qpair_sp_compl;
1029         sp->qpair = qpair;
1030
1031         rval = ha->isp_ops->start_scsi_mq(sp);
1032         if (rval != QLA_SUCCESS) {
1033                 ql_dbg(ql_dbg_io + ql_dbg_verbose, vha, 0x3078,
1034                     "Start scsi failed rval=%d for cmd=%p.\n", rval, cmd);
1035                 if (rval == QLA_INTERFACE_ERROR)
1036                         goto qc24_fail_command;
1037                 goto qc24_host_busy_free_sp;
1038         }
1039
1040         return 0;
1041
1042 qc24_host_busy_free_sp:
1043         sp->free(sp);
1044
1045 qc24_host_busy:
1046         return SCSI_MLQUEUE_HOST_BUSY;
1047
1048 qc24_target_busy:
1049         return SCSI_MLQUEUE_TARGET_BUSY;
1050
1051 qc24_fail_command:
1052         cmd->scsi_done(cmd);
1053
1054         return 0;
1055 }
1056
1057 /*
1058  * qla2x00_eh_wait_on_command
1059  *    Waits for the command to be returned by the Firmware for some
1060  *    max time.
1061  *
1062  * Input:
1063  *    cmd = Scsi Command to wait on.
1064  *
1065  * Return:
1066  *    Not Found : 0
1067  *    Found : 1
1068  */
1069 static int
1070 qla2x00_eh_wait_on_command(struct scsi_cmnd *cmd)
1071 {
1072 #define ABORT_POLLING_PERIOD    1000
1073 #define ABORT_WAIT_ITER         ((2 * 1000) / (ABORT_POLLING_PERIOD))
1074         unsigned long wait_iter = ABORT_WAIT_ITER;
1075         scsi_qla_host_t *vha = shost_priv(cmd->device->host);
1076         struct qla_hw_data *ha = vha->hw;
1077         int ret = QLA_SUCCESS;
1078
1079         if (unlikely(pci_channel_offline(ha->pdev)) || ha->flags.eeh_busy) {
1080                 ql_dbg(ql_dbg_taskm, vha, 0x8005,
1081                     "Return:eh_wait.\n");
1082                 return ret;
1083         }
1084
1085         while (CMD_SP(cmd) && wait_iter--) {
1086                 msleep(ABORT_POLLING_PERIOD);
1087         }
1088         if (CMD_SP(cmd))
1089                 ret = QLA_FUNCTION_FAILED;
1090
1091         return ret;
1092 }
1093
1094 /*
1095  * qla2x00_wait_for_hba_online
1096  *    Wait till the HBA is online after going through
1097  *    <= MAX_RETRIES_OF_ISP_ABORT  or
1098  *    finally HBA is disabled ie marked offline
1099  *
1100  * Input:
1101  *     ha - pointer to host adapter structure
1102  *
1103  * Note:
1104  *    Does context switching-Release SPIN_LOCK
1105  *    (if any) before calling this routine.
1106  *
1107  * Return:
1108  *    Success (Adapter is online) : 0
1109  *    Failed  (Adapter is offline/disabled) : 1
1110  */
1111 int
1112 qla2x00_wait_for_hba_online(scsi_qla_host_t *vha)
1113 {
1114         int             return_status;
1115         unsigned long   wait_online;
1116         struct qla_hw_data *ha = vha->hw;
1117         scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
1118
1119         wait_online = jiffies + (MAX_LOOP_TIMEOUT * HZ);
1120         while (((test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags)) ||
1121             test_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags) ||
1122             test_bit(ISP_ABORT_RETRY, &base_vha->dpc_flags) ||
1123             ha->dpc_active) && time_before(jiffies, wait_online)) {
1124
1125                 msleep(1000);
1126         }
1127         if (base_vha->flags.online)
1128                 return_status = QLA_SUCCESS;
1129         else
1130                 return_status = QLA_FUNCTION_FAILED;
1131
1132         return (return_status);
1133 }
1134
1135 static inline int test_fcport_count(scsi_qla_host_t *vha)
1136 {
1137         struct qla_hw_data *ha = vha->hw;
1138         unsigned long flags;
1139         int res;
1140
1141         spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1142         ql_dbg(ql_dbg_init, vha, 0x00ec,
1143             "tgt %p, fcport_count=%d\n",
1144             vha, vha->fcport_count);
1145         res = (vha->fcport_count == 0);
1146         spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1147
1148         return res;
1149 }
1150
1151 /*
1152  * qla2x00_wait_for_sess_deletion can only be called from remove_one.
1153  * it has dependency on UNLOADING flag to stop device discovery
1154  */
1155 void
1156 qla2x00_wait_for_sess_deletion(scsi_qla_host_t *vha)
1157 {
1158         qla2x00_mark_all_devices_lost(vha, 0);
1159
1160         wait_event_timeout(vha->fcport_waitQ, test_fcport_count(vha), 10*HZ);
1161 }
1162
1163 /*
1164  * qla2x00_wait_for_hba_ready
1165  * Wait till the HBA is ready before doing driver unload
1166  *
1167  * Input:
1168  *     ha - pointer to host adapter structure
1169  *
1170  * Note:
1171  *    Does context switching-Release SPIN_LOCK
1172  *    (if any) before calling this routine.
1173  *
1174  */
1175 static void
1176 qla2x00_wait_for_hba_ready(scsi_qla_host_t *vha)
1177 {
1178         struct qla_hw_data *ha = vha->hw;
1179         scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
1180
1181         while ((qla2x00_reset_active(vha) || ha->dpc_active ||
1182                 ha->flags.mbox_busy) ||
1183                test_bit(FX00_RESET_RECOVERY, &vha->dpc_flags) ||
1184                test_bit(FX00_TARGET_SCAN, &vha->dpc_flags)) {
1185                 if (test_bit(UNLOADING, &base_vha->dpc_flags))
1186                         break;
1187                 msleep(1000);
1188         }
1189 }
1190
1191 int
1192 qla2x00_wait_for_chip_reset(scsi_qla_host_t *vha)
1193 {
1194         int             return_status;
1195         unsigned long   wait_reset;
1196         struct qla_hw_data *ha = vha->hw;
1197         scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
1198
1199         wait_reset = jiffies + (MAX_LOOP_TIMEOUT * HZ);
1200         while (((test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags)) ||
1201             test_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags) ||
1202             test_bit(ISP_ABORT_RETRY, &base_vha->dpc_flags) ||
1203             ha->dpc_active) && time_before(jiffies, wait_reset)) {
1204
1205                 msleep(1000);
1206
1207                 if (!test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags) &&
1208                     ha->flags.chip_reset_done)
1209                         break;
1210         }
1211         if (ha->flags.chip_reset_done)
1212                 return_status = QLA_SUCCESS;
1213         else
1214                 return_status = QLA_FUNCTION_FAILED;
1215
1216         return return_status;
1217 }
1218
1219 static int
1220 sp_get(struct srb *sp)
1221 {
1222         if (!refcount_inc_not_zero((refcount_t*)&sp->ref_count))
1223                 /* kref get fail */
1224                 return ENXIO;
1225         else
1226                 return 0;
1227 }
1228
1229 #define ISP_REG_DISCONNECT 0xffffffffU
1230 /**************************************************************************
1231 * qla2x00_isp_reg_stat
1232 *
1233 * Description:
1234 *       Read the host status register of ISP before aborting the command.
1235 *
1236 * Input:
1237 *       ha = pointer to host adapter structure.
1238 *
1239 *
1240 * Returns:
1241 *       Either true or false.
1242 *
1243 * Note: Return true if there is register disconnect.
1244 **************************************************************************/
1245 static inline
1246 uint32_t qla2x00_isp_reg_stat(struct qla_hw_data *ha)
1247 {
1248         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1249         struct device_reg_82xx __iomem *reg82 = &ha->iobase->isp82;
1250
1251         if (IS_P3P_TYPE(ha))
1252                 return ((RD_REG_DWORD(&reg82->host_int)) == ISP_REG_DISCONNECT);
1253         else
1254                 return ((RD_REG_DWORD(&reg->host_status)) ==
1255                         ISP_REG_DISCONNECT);
1256 }
1257
1258 /**************************************************************************
1259 * qla2xxx_eh_abort
1260 *
1261 * Description:
1262 *    The abort function will abort the specified command.
1263 *
1264 * Input:
1265 *    cmd = Linux SCSI command packet to be aborted.
1266 *
1267 * Returns:
1268 *    Either SUCCESS or FAILED.
1269 *
1270 * Note:
1271 *    Only return FAILED if command not returned by firmware.
1272 **************************************************************************/
1273 static int
1274 qla2xxx_eh_abort(struct scsi_cmnd *cmd)
1275 {
1276         scsi_qla_host_t *vha = shost_priv(cmd->device->host);
1277         srb_t *sp;
1278         int ret;
1279         unsigned int id;
1280         uint64_t lun;
1281         unsigned long flags;
1282         int rval, wait = 0;
1283         struct qla_hw_data *ha = vha->hw;
1284         struct qla_qpair *qpair;
1285
1286         if (qla2x00_isp_reg_stat(ha)) {
1287                 ql_log(ql_log_info, vha, 0x8042,
1288                     "PCI/Register disconnect, exiting.\n");
1289                 return FAILED;
1290         }
1291
1292         ret = fc_block_scsi_eh(cmd);
1293         if (ret != 0)
1294                 return ret;
1295         ret = SUCCESS;
1296
1297         sp = (srb_t *) CMD_SP(cmd);
1298         if (!sp)
1299                 return SUCCESS;
1300
1301         qpair = sp->qpair;
1302         if (!qpair)
1303                 return SUCCESS;
1304
1305         spin_lock_irqsave(qpair->qp_lock_ptr, flags);
1306         if (!CMD_SP(cmd)) {
1307                 /* there's a chance an interrupt could clear
1308                    the ptr as part of done & free */
1309                 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
1310                 return SUCCESS;
1311         }
1312
1313         if (sp_get(sp)){
1314                 /* ref_count is already 0 */
1315                 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
1316                 return SUCCESS;
1317         }
1318         spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
1319
1320         id = cmd->device->id;
1321         lun = cmd->device->lun;
1322
1323         ql_dbg(ql_dbg_taskm, vha, 0x8002,
1324             "Aborting from RISC nexus=%ld:%d:%llu sp=%p cmd=%p handle=%x\n",
1325             vha->host_no, id, lun, sp, cmd, sp->handle);
1326
1327         /* Get a reference to the sp and drop the lock.*/
1328
1329         rval = ha->isp_ops->abort_command(sp);
1330         if (rval) {
1331                 if (rval == QLA_FUNCTION_PARAMETER_ERROR)
1332                         ret = SUCCESS;
1333                 else
1334                         ret = FAILED;
1335
1336                 ql_dbg(ql_dbg_taskm, vha, 0x8003,
1337                     "Abort command mbx failed cmd=%p, rval=%x.\n", cmd, rval);
1338         } else {
1339                 ql_dbg(ql_dbg_taskm, vha, 0x8004,
1340                     "Abort command mbx success cmd=%p.\n", cmd);
1341                 wait = 1;
1342         }
1343
1344         spin_lock_irqsave(qpair->qp_lock_ptr, flags);
1345         /*
1346          * Clear the slot in the oustanding_cmds array if we can't find the
1347          * command to reclaim the resources.
1348          */
1349         if (rval == QLA_FUNCTION_PARAMETER_ERROR)
1350                 vha->req->outstanding_cmds[sp->handle] = NULL;
1351
1352         /*
1353          * sp->done will do ref_count--
1354          * sp_get() took an extra count above
1355          */
1356         sp->done(sp, DID_RESET << 16);
1357
1358         /* Did the command return during mailbox execution? */
1359         if (ret == FAILED && !CMD_SP(cmd))
1360                 ret = SUCCESS;
1361
1362         if (!CMD_SP(cmd))
1363                 wait = 0;
1364
1365         spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
1366
1367         /* Wait for the command to be returned. */
1368         if (wait) {
1369                 if (qla2x00_eh_wait_on_command(cmd) != QLA_SUCCESS) {
1370                         ql_log(ql_log_warn, vha, 0x8006,
1371                             "Abort handler timed out cmd=%p.\n", cmd);
1372                         ret = FAILED;
1373                 }
1374         }
1375
1376         ql_log(ql_log_info, vha, 0x801c,
1377             "Abort command issued nexus=%ld:%d:%llu --  %d %x.\n",
1378             vha->host_no, id, lun, wait, ret);
1379
1380         return ret;
1381 }
1382
1383 int
1384 qla2x00_eh_wait_for_pending_commands(scsi_qla_host_t *vha, unsigned int t,
1385         uint64_t l, enum nexus_wait_type type)
1386 {
1387         int cnt, match, status;
1388         unsigned long flags;
1389         struct qla_hw_data *ha = vha->hw;
1390         struct req_que *req;
1391         srb_t *sp;
1392         struct scsi_cmnd *cmd;
1393
1394         status = QLA_SUCCESS;
1395
1396         spin_lock_irqsave(&ha->hardware_lock, flags);
1397         req = vha->req;
1398         for (cnt = 1; status == QLA_SUCCESS &&
1399                 cnt < req->num_outstanding_cmds; cnt++) {
1400                 sp = req->outstanding_cmds[cnt];
1401                 if (!sp)
1402                         continue;
1403                 if (sp->type != SRB_SCSI_CMD)
1404                         continue;
1405                 if (vha->vp_idx != sp->vha->vp_idx)
1406                         continue;
1407                 match = 0;
1408                 cmd = GET_CMD_SP(sp);
1409                 switch (type) {
1410                 case WAIT_HOST:
1411                         match = 1;
1412                         break;
1413                 case WAIT_TARGET:
1414                         match = cmd->device->id == t;
1415                         break;
1416                 case WAIT_LUN:
1417                         match = (cmd->device->id == t &&
1418                                 cmd->device->lun == l);
1419                         break;
1420                 }
1421                 if (!match)
1422                         continue;
1423
1424                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1425                 status = qla2x00_eh_wait_on_command(cmd);
1426                 spin_lock_irqsave(&ha->hardware_lock, flags);
1427         }
1428         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1429
1430         return status;
1431 }
1432
1433 static char *reset_errors[] = {
1434         "HBA not online",
1435         "HBA not ready",
1436         "Task management failed",
1437         "Waiting for command completions",
1438 };
1439
1440 static int
1441 __qla2xxx_eh_generic_reset(char *name, enum nexus_wait_type type,
1442     struct scsi_cmnd *cmd, int (*do_reset)(struct fc_port *, uint64_t, int))
1443 {
1444         scsi_qla_host_t *vha = shost_priv(cmd->device->host);
1445         fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
1446         int err;
1447
1448         if (!fcport) {
1449                 return FAILED;
1450         }
1451
1452         err = fc_block_scsi_eh(cmd);
1453         if (err != 0)
1454                 return err;
1455
1456         ql_log(ql_log_info, vha, 0x8009,
1457             "%s RESET ISSUED nexus=%ld:%d:%llu cmd=%p.\n", name, vha->host_no,
1458             cmd->device->id, cmd->device->lun, cmd);
1459
1460         err = 0;
1461         if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
1462                 ql_log(ql_log_warn, vha, 0x800a,
1463                     "Wait for hba online failed for cmd=%p.\n", cmd);
1464                 goto eh_reset_failed;
1465         }
1466         err = 2;
1467         if (do_reset(fcport, cmd->device->lun, cmd->request->cpu + 1)
1468                 != QLA_SUCCESS) {
1469                 ql_log(ql_log_warn, vha, 0x800c,
1470                     "do_reset failed for cmd=%p.\n", cmd);
1471                 goto eh_reset_failed;
1472         }
1473         err = 3;
1474         if (qla2x00_eh_wait_for_pending_commands(vha, cmd->device->id,
1475             cmd->device->lun, type) != QLA_SUCCESS) {
1476                 ql_log(ql_log_warn, vha, 0x800d,
1477                     "wait for pending cmds failed for cmd=%p.\n", cmd);
1478                 goto eh_reset_failed;
1479         }
1480
1481         ql_log(ql_log_info, vha, 0x800e,
1482             "%s RESET SUCCEEDED nexus:%ld:%d:%llu cmd=%p.\n", name,
1483             vha->host_no, cmd->device->id, cmd->device->lun, cmd);
1484
1485         return SUCCESS;
1486
1487 eh_reset_failed:
1488         ql_log(ql_log_info, vha, 0x800f,
1489             "%s RESET FAILED: %s nexus=%ld:%d:%llu cmd=%p.\n", name,
1490             reset_errors[err], vha->host_no, cmd->device->id, cmd->device->lun,
1491             cmd);
1492         return FAILED;
1493 }
1494
1495 static int
1496 qla2xxx_eh_device_reset(struct scsi_cmnd *cmd)
1497 {
1498         scsi_qla_host_t *vha = shost_priv(cmd->device->host);
1499         struct qla_hw_data *ha = vha->hw;
1500
1501         if (qla2x00_isp_reg_stat(ha)) {
1502                 ql_log(ql_log_info, vha, 0x803e,
1503                     "PCI/Register disconnect, exiting.\n");
1504                 return FAILED;
1505         }
1506
1507         return __qla2xxx_eh_generic_reset("DEVICE", WAIT_LUN, cmd,
1508             ha->isp_ops->lun_reset);
1509 }
1510
1511 static int
1512 qla2xxx_eh_target_reset(struct scsi_cmnd *cmd)
1513 {
1514         scsi_qla_host_t *vha = shost_priv(cmd->device->host);
1515         struct qla_hw_data *ha = vha->hw;
1516
1517         if (qla2x00_isp_reg_stat(ha)) {
1518                 ql_log(ql_log_info, vha, 0x803f,
1519                     "PCI/Register disconnect, exiting.\n");
1520                 return FAILED;
1521         }
1522
1523         return __qla2xxx_eh_generic_reset("TARGET", WAIT_TARGET, cmd,
1524             ha->isp_ops->target_reset);
1525 }
1526
1527 /**************************************************************************
1528 * qla2xxx_eh_bus_reset
1529 *
1530 * Description:
1531 *    The bus reset function will reset the bus and abort any executing
1532 *    commands.
1533 *
1534 * Input:
1535 *    cmd = Linux SCSI command packet of the command that cause the
1536 *          bus reset.
1537 *
1538 * Returns:
1539 *    SUCCESS/FAILURE (defined as macro in scsi.h).
1540 *
1541 **************************************************************************/
1542 static int
1543 qla2xxx_eh_bus_reset(struct scsi_cmnd *cmd)
1544 {
1545         scsi_qla_host_t *vha = shost_priv(cmd->device->host);
1546         fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
1547         int ret = FAILED;
1548         unsigned int id;
1549         uint64_t lun;
1550         struct qla_hw_data *ha = vha->hw;
1551
1552         if (qla2x00_isp_reg_stat(ha)) {
1553                 ql_log(ql_log_info, vha, 0x8040,
1554                     "PCI/Register disconnect, exiting.\n");
1555                 return FAILED;
1556         }
1557
1558         id = cmd->device->id;
1559         lun = cmd->device->lun;
1560
1561         if (!fcport) {
1562                 return ret;
1563         }
1564
1565         ret = fc_block_scsi_eh(cmd);
1566         if (ret != 0)
1567                 return ret;
1568         ret = FAILED;
1569
1570         ql_log(ql_log_info, vha, 0x8012,
1571             "BUS RESET ISSUED nexus=%ld:%d:%llu.\n", vha->host_no, id, lun);
1572
1573         if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
1574                 ql_log(ql_log_fatal, vha, 0x8013,
1575                     "Wait for hba online failed board disabled.\n");
1576                 goto eh_bus_reset_done;
1577         }
1578
1579         if (qla2x00_loop_reset(vha) == QLA_SUCCESS)
1580                 ret = SUCCESS;
1581
1582         if (ret == FAILED)
1583                 goto eh_bus_reset_done;
1584
1585         /* Flush outstanding commands. */
1586         if (qla2x00_eh_wait_for_pending_commands(vha, 0, 0, WAIT_HOST) !=
1587             QLA_SUCCESS) {
1588                 ql_log(ql_log_warn, vha, 0x8014,
1589                     "Wait for pending commands failed.\n");
1590                 ret = FAILED;
1591         }
1592
1593 eh_bus_reset_done:
1594         ql_log(ql_log_warn, vha, 0x802b,
1595             "BUS RESET %s nexus=%ld:%d:%llu.\n",
1596             (ret == FAILED) ? "FAILED" : "SUCCEEDED", vha->host_no, id, lun);
1597
1598         return ret;
1599 }
1600
1601 /**************************************************************************
1602 * qla2xxx_eh_host_reset
1603 *
1604 * Description:
1605 *    The reset function will reset the Adapter.
1606 *
1607 * Input:
1608 *      cmd = Linux SCSI command packet of the command that cause the
1609 *            adapter reset.
1610 *
1611 * Returns:
1612 *      Either SUCCESS or FAILED.
1613 *
1614 * Note:
1615 **************************************************************************/
1616 static int
1617 qla2xxx_eh_host_reset(struct scsi_cmnd *cmd)
1618 {
1619         scsi_qla_host_t *vha = shost_priv(cmd->device->host);
1620         struct qla_hw_data *ha = vha->hw;
1621         int ret = FAILED;
1622         unsigned int id;
1623         uint64_t lun;
1624         scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
1625
1626         if (qla2x00_isp_reg_stat(ha)) {
1627                 ql_log(ql_log_info, vha, 0x8041,
1628                     "PCI/Register disconnect, exiting.\n");
1629                 schedule_work(&ha->board_disable);
1630                 return SUCCESS;
1631         }
1632
1633         id = cmd->device->id;
1634         lun = cmd->device->lun;
1635
1636         ql_log(ql_log_info, vha, 0x8018,
1637             "ADAPTER RESET ISSUED nexus=%ld:%d:%llu.\n", vha->host_no, id, lun);
1638
1639         /*
1640          * No point in issuing another reset if one is active.  Also do not
1641          * attempt a reset if we are updating flash.
1642          */
1643         if (qla2x00_reset_active(vha) || ha->optrom_state != QLA_SWAITING)
1644                 goto eh_host_reset_lock;
1645
1646         if (vha != base_vha) {
1647                 if (qla2x00_vp_abort_isp(vha))
1648                         goto eh_host_reset_lock;
1649         } else {
1650                 if (IS_P3P_TYPE(vha->hw)) {
1651                         if (!qla82xx_fcoe_ctx_reset(vha)) {
1652                                 /* Ctx reset success */
1653                                 ret = SUCCESS;
1654                                 goto eh_host_reset_lock;
1655                         }
1656                         /* fall thru if ctx reset failed */
1657                 }
1658                 if (ha->wq)
1659                         flush_workqueue(ha->wq);
1660
1661                 set_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
1662                 if (ha->isp_ops->abort_isp(base_vha)) {
1663                         clear_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
1664                         /* failed. schedule dpc to try */
1665                         set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
1666
1667                         if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
1668                                 ql_log(ql_log_warn, vha, 0x802a,
1669                                     "wait for hba online failed.\n");
1670                                 goto eh_host_reset_lock;
1671                         }
1672                 }
1673                 clear_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
1674         }
1675
1676         /* Waiting for command to be returned to OS.*/
1677         if (qla2x00_eh_wait_for_pending_commands(vha, 0, 0, WAIT_HOST) ==
1678                 QLA_SUCCESS)
1679                 ret = SUCCESS;
1680
1681 eh_host_reset_lock:
1682         ql_log(ql_log_info, vha, 0x8017,
1683             "ADAPTER RESET %s nexus=%ld:%d:%llu.\n",
1684             (ret == FAILED) ? "FAILED" : "SUCCEEDED", vha->host_no, id, lun);
1685
1686         return ret;
1687 }
1688
1689 /*
1690 * qla2x00_loop_reset
1691 *      Issue loop reset.
1692 *
1693 * Input:
1694 *      ha = adapter block pointer.
1695 *
1696 * Returns:
1697 *      0 = success
1698 */
1699 int
1700 qla2x00_loop_reset(scsi_qla_host_t *vha)
1701 {
1702         int ret;
1703         struct fc_port *fcport;
1704         struct qla_hw_data *ha = vha->hw;
1705
1706         if (IS_QLAFX00(ha)) {
1707                 return qlafx00_loop_reset(vha);
1708         }
1709
1710         if (ql2xtargetreset == 1 && ha->flags.enable_target_reset) {
1711                 list_for_each_entry(fcport, &vha->vp_fcports, list) {
1712                         if (fcport->port_type != FCT_TARGET)
1713                                 continue;
1714
1715                         ret = ha->isp_ops->target_reset(fcport, 0, 0);
1716                         if (ret != QLA_SUCCESS) {
1717                                 ql_dbg(ql_dbg_taskm, vha, 0x802c,
1718                                     "Bus Reset failed: Reset=%d "
1719                                     "d_id=%x.\n", ret, fcport->d_id.b24);
1720                         }
1721                 }
1722         }
1723
1724
1725         if (ha->flags.enable_lip_full_login && !IS_CNA_CAPABLE(ha)) {
1726                 atomic_set(&vha->loop_state, LOOP_DOWN);
1727                 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
1728                 qla2x00_mark_all_devices_lost(vha, 0);
1729                 ret = qla2x00_full_login_lip(vha);
1730                 if (ret != QLA_SUCCESS) {
1731                         ql_dbg(ql_dbg_taskm, vha, 0x802d,
1732                             "full_login_lip=%d.\n", ret);
1733                 }
1734         }
1735
1736         if (ha->flags.enable_lip_reset) {
1737                 ret = qla2x00_lip_reset(vha);
1738                 if (ret != QLA_SUCCESS)
1739                         ql_dbg(ql_dbg_taskm, vha, 0x802e,
1740                             "lip_reset failed (%d).\n", ret);
1741         }
1742
1743         /* Issue marker command only when we are going to start the I/O */
1744         vha->marker_needed = 1;
1745
1746         return QLA_SUCCESS;
1747 }
1748
1749 static void
1750 __qla2x00_abort_all_cmds(struct qla_qpair *qp, int res)
1751 {
1752         int cnt;
1753         unsigned long flags;
1754         srb_t *sp;
1755         scsi_qla_host_t *vha = qp->vha;
1756         struct qla_hw_data *ha = vha->hw;
1757         struct req_que *req;
1758         struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
1759         struct qla_tgt_cmd *cmd;
1760
1761         if (!ha->req_q_map)
1762                 return;
1763         spin_lock_irqsave(qp->qp_lock_ptr, flags);
1764         req = qp->req;
1765         for (cnt = 1; cnt < req->num_outstanding_cmds; cnt++) {
1766                 sp = req->outstanding_cmds[cnt];
1767                 if (sp) {
1768                         req->outstanding_cmds[cnt] = NULL;
1769                         switch (sp->cmd_type) {
1770                         case TYPE_SRB:
1771                                 if (sp->type == SRB_NVME_CMD ||
1772                                     sp->type == SRB_NVME_LS) {
1773                                         if (!sp_get(sp)) {
1774                                                 /* got sp */
1775                                                 spin_unlock_irqrestore
1776                                                         (qp->qp_lock_ptr,
1777                                                          flags);
1778                                                 qla_nvme_abort(ha, sp, res);
1779                                                 spin_lock_irqsave
1780                                                         (qp->qp_lock_ptr, flags);
1781                                         }
1782                                 } else if (GET_CMD_SP(sp) &&
1783                                     !ha->flags.eeh_busy &&
1784                                     (!test_bit(ABORT_ISP_ACTIVE,
1785                                         &vha->dpc_flags)) &&
1786                                     !qla2x00_isp_reg_stat(ha) &&
1787                                     (sp->type == SRB_SCSI_CMD)) {
1788                                         /*
1789                                          * Don't abort commands in adapter
1790                                          * during EEH recovery as it's not
1791                                          * accessible/responding.
1792                                          *
1793                                          * Get a reference to the sp and drop
1794                                          * the lock. The reference ensures this
1795                                          * sp->done() call and not the call in
1796                                          * qla2xxx_eh_abort() ends the SCSI cmd
1797                                          * (with result 'res').
1798                                          */
1799                                         if (!sp_get(sp)) {
1800                                                 spin_unlock_irqrestore
1801                                                         (qp->qp_lock_ptr, flags);
1802                                                 qla2xxx_eh_abort(
1803                                                         GET_CMD_SP(sp));
1804                                                 spin_lock_irqsave
1805                                                         (qp->qp_lock_ptr, flags);
1806                                         }
1807                                 }
1808                                 sp->done(sp, res);
1809                                 break;
1810                         case TYPE_TGT_CMD:
1811                                 if (!vha->hw->tgt.tgt_ops || !tgt ||
1812                                     qla_ini_mode_enabled(vha)) {
1813                                         ql_dbg(ql_dbg_tgt_mgt, vha, 0xf003,
1814                                             "HOST-ABORT-HNDLR: dpc_flags=%lx. Target mode disabled\n",
1815                                             vha->dpc_flags);
1816                                         continue;
1817                                 }
1818                                 cmd = (struct qla_tgt_cmd *)sp;
1819                                 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
1820                                 break;
1821                         case TYPE_TGT_TMCMD:
1822                                 /*
1823                                  * Currently, only ABTS response gets on the
1824                                  * outstanding_cmds[]
1825                                  */
1826                                 ha->tgt.tgt_ops->free_mcmd(
1827                                    (struct qla_tgt_mgmt_cmd *)sp);
1828                                 break;
1829                         default:
1830                                 break;
1831                         }
1832                 }
1833         }
1834         spin_unlock_irqrestore(qp->qp_lock_ptr, flags);
1835 }
1836
1837 void
1838 qla2x00_abort_all_cmds(scsi_qla_host_t *vha, int res)
1839 {
1840         int que;
1841         struct qla_hw_data *ha = vha->hw;
1842
1843         __qla2x00_abort_all_cmds(ha->base_qpair, res);
1844
1845         for (que = 0; que < ha->max_qpairs; que++) {
1846                 if (!ha->queue_pair_map[que])
1847                         continue;
1848
1849                 __qla2x00_abort_all_cmds(ha->queue_pair_map[que], res);
1850         }
1851 }
1852
1853 static int
1854 qla2xxx_slave_alloc(struct scsi_device *sdev)
1855 {
1856         struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1857
1858         if (!rport || fc_remote_port_chkready(rport))
1859                 return -ENXIO;
1860
1861         sdev->hostdata = *(fc_port_t **)rport->dd_data;
1862
1863         return 0;
1864 }
1865
1866 static int
1867 qla2xxx_slave_configure(struct scsi_device *sdev)
1868 {
1869         scsi_qla_host_t *vha = shost_priv(sdev->host);
1870         struct req_que *req = vha->req;
1871
1872         if (IS_T10_PI_CAPABLE(vha->hw))
1873                 blk_queue_update_dma_alignment(sdev->request_queue, 0x7);
1874
1875         scsi_change_queue_depth(sdev, req->max_q_depth);
1876         return 0;
1877 }
1878
1879 static void
1880 qla2xxx_slave_destroy(struct scsi_device *sdev)
1881 {
1882         sdev->hostdata = NULL;
1883 }
1884
1885 /**
1886  * qla2x00_config_dma_addressing() - Configure OS DMA addressing method.
1887  * @ha: HA context
1888  *
1889  * At exit, the @ha's flags.enable_64bit_addressing set to indicated
1890  * supported addressing method.
1891  */
1892 static void
1893 qla2x00_config_dma_addressing(struct qla_hw_data *ha)
1894 {
1895         /* Assume a 32bit DMA mask. */
1896         ha->flags.enable_64bit_addressing = 0;
1897
1898         if (!dma_set_mask(&ha->pdev->dev, DMA_BIT_MASK(64))) {
1899                 /* Any upper-dword bits set? */
1900                 if (MSD(dma_get_required_mask(&ha->pdev->dev)) &&
1901                     !pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
1902                         /* Ok, a 64bit DMA mask is applicable. */
1903                         ha->flags.enable_64bit_addressing = 1;
1904                         ha->isp_ops->calc_req_entries = qla2x00_calc_iocbs_64;
1905                         ha->isp_ops->build_iocbs = qla2x00_build_scsi_iocbs_64;
1906                         return;
1907                 }
1908         }
1909
1910         dma_set_mask(&ha->pdev->dev, DMA_BIT_MASK(32));
1911         pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(32));
1912 }
1913
1914 static void
1915 qla2x00_enable_intrs(struct qla_hw_data *ha)
1916 {
1917         unsigned long flags = 0;
1918         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1919
1920         spin_lock_irqsave(&ha->hardware_lock, flags);
1921         ha->interrupts_on = 1;
1922         /* enable risc and host interrupts */
1923         WRT_REG_WORD(&reg->ictrl, ICR_EN_INT | ICR_EN_RISC);
1924         RD_REG_WORD(&reg->ictrl);
1925         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1926
1927 }
1928
1929 static void
1930 qla2x00_disable_intrs(struct qla_hw_data *ha)
1931 {
1932         unsigned long flags = 0;
1933         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1934
1935         spin_lock_irqsave(&ha->hardware_lock, flags);
1936         ha->interrupts_on = 0;
1937         /* disable risc and host interrupts */
1938         WRT_REG_WORD(&reg->ictrl, 0);
1939         RD_REG_WORD(&reg->ictrl);
1940         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1941 }
1942
1943 static void
1944 qla24xx_enable_intrs(struct qla_hw_data *ha)
1945 {
1946         unsigned long flags = 0;
1947         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1948
1949         spin_lock_irqsave(&ha->hardware_lock, flags);
1950         ha->interrupts_on = 1;
1951         WRT_REG_DWORD(&reg->ictrl, ICRX_EN_RISC_INT);
1952         RD_REG_DWORD(&reg->ictrl);
1953         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1954 }
1955
1956 static void
1957 qla24xx_disable_intrs(struct qla_hw_data *ha)
1958 {
1959         unsigned long flags = 0;
1960         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1961
1962         if (IS_NOPOLLING_TYPE(ha))
1963                 return;
1964         spin_lock_irqsave(&ha->hardware_lock, flags);
1965         ha->interrupts_on = 0;
1966         WRT_REG_DWORD(&reg->ictrl, 0);
1967         RD_REG_DWORD(&reg->ictrl);
1968         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1969 }
1970
1971 static int
1972 qla2x00_iospace_config(struct qla_hw_data *ha)
1973 {
1974         resource_size_t pio;
1975         uint16_t msix;
1976
1977         if (pci_request_selected_regions(ha->pdev, ha->bars,
1978             QLA2XXX_DRIVER_NAME)) {
1979                 ql_log_pci(ql_log_fatal, ha->pdev, 0x0011,
1980                     "Failed to reserve PIO/MMIO regions (%s), aborting.\n",
1981                     pci_name(ha->pdev));
1982                 goto iospace_error_exit;
1983         }
1984         if (!(ha->bars & 1))
1985                 goto skip_pio;
1986
1987         /* We only need PIO for Flash operations on ISP2312 v2 chips. */
1988         pio = pci_resource_start(ha->pdev, 0);
1989         if (pci_resource_flags(ha->pdev, 0) & IORESOURCE_IO) {
1990                 if (pci_resource_len(ha->pdev, 0) < MIN_IOBASE_LEN) {
1991                         ql_log_pci(ql_log_warn, ha->pdev, 0x0012,
1992                             "Invalid pci I/O region size (%s).\n",
1993                             pci_name(ha->pdev));
1994                         pio = 0;
1995                 }
1996         } else {
1997                 ql_log_pci(ql_log_warn, ha->pdev, 0x0013,
1998                     "Region #0 no a PIO resource (%s).\n",
1999                     pci_name(ha->pdev));
2000                 pio = 0;
2001         }
2002         ha->pio_address = pio;
2003         ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0014,
2004             "PIO address=%llu.\n",
2005             (unsigned long long)ha->pio_address);
2006
2007 skip_pio:
2008         /* Use MMIO operations for all accesses. */
2009         if (!(pci_resource_flags(ha->pdev, 1) & IORESOURCE_MEM)) {
2010                 ql_log_pci(ql_log_fatal, ha->pdev, 0x0015,
2011                     "Region #1 not an MMIO resource (%s), aborting.\n",
2012                     pci_name(ha->pdev));
2013                 goto iospace_error_exit;
2014         }
2015         if (pci_resource_len(ha->pdev, 1) < MIN_IOBASE_LEN) {
2016                 ql_log_pci(ql_log_fatal, ha->pdev, 0x0016,
2017                     "Invalid PCI mem region size (%s), aborting.\n",
2018                     pci_name(ha->pdev));
2019                 goto iospace_error_exit;
2020         }
2021
2022         ha->iobase = ioremap(pci_resource_start(ha->pdev, 1), MIN_IOBASE_LEN);
2023         if (!ha->iobase) {
2024                 ql_log_pci(ql_log_fatal, ha->pdev, 0x0017,
2025                     "Cannot remap MMIO (%s), aborting.\n",
2026                     pci_name(ha->pdev));
2027                 goto iospace_error_exit;
2028         }
2029
2030         /* Determine queue resources */
2031         ha->max_req_queues = ha->max_rsp_queues = 1;
2032         ha->msix_count = QLA_BASE_VECTORS;
2033         if (!ql2xmqsupport || !ql2xnvmeenable ||
2034             (!IS_QLA25XX(ha) && !IS_QLA81XX(ha)))
2035                 goto mqiobase_exit;
2036
2037         ha->mqiobase = ioremap(pci_resource_start(ha->pdev, 3),
2038                         pci_resource_len(ha->pdev, 3));
2039         if (ha->mqiobase) {
2040                 ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0018,
2041                     "MQIO Base=%p.\n", ha->mqiobase);
2042                 /* Read MSIX vector size of the board */
2043                 pci_read_config_word(ha->pdev, QLA_PCI_MSIX_CONTROL, &msix);
2044                 ha->msix_count = msix + 1;
2045                 /* Max queues are bounded by available msix vectors */
2046                 /* MB interrupt uses 1 vector */
2047                 ha->max_req_queues = ha->msix_count - 1;
2048                 ha->max_rsp_queues = ha->max_req_queues;
2049                 /* Queue pairs is the max value minus the base queue pair */
2050                 ha->max_qpairs = ha->max_rsp_queues - 1;
2051                 ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0188,
2052                     "Max no of queues pairs: %d.\n", ha->max_qpairs);
2053
2054                 ql_log_pci(ql_log_info, ha->pdev, 0x001a,
2055                     "MSI-X vector count: %d.\n", ha->msix_count);
2056         } else
2057                 ql_log_pci(ql_log_info, ha->pdev, 0x001b,
2058                     "BAR 3 not enabled.\n");
2059
2060 mqiobase_exit:
2061         ql_dbg_pci(ql_dbg_init, ha->pdev, 0x001c,
2062             "MSIX Count: %d.\n", ha->msix_count);
2063         return (0);
2064
2065 iospace_error_exit:
2066         return (-ENOMEM);
2067 }
2068
2069
2070 static int
2071 qla83xx_iospace_config(struct qla_hw_data *ha)
2072 {
2073         uint16_t msix;
2074
2075         if (pci_request_selected_regions(ha->pdev, ha->bars,
2076             QLA2XXX_DRIVER_NAME)) {
2077                 ql_log_pci(ql_log_fatal, ha->pdev, 0x0117,
2078                     "Failed to reserve PIO/MMIO regions (%s), aborting.\n",
2079                     pci_name(ha->pdev));
2080
2081                 goto iospace_error_exit;
2082         }
2083
2084         /* Use MMIO operations for all accesses. */
2085         if (!(pci_resource_flags(ha->pdev, 0) & IORESOURCE_MEM)) {
2086                 ql_log_pci(ql_log_warn, ha->pdev, 0x0118,
2087                     "Invalid pci I/O region size (%s).\n",
2088                     pci_name(ha->pdev));
2089                 goto iospace_error_exit;
2090         }
2091         if (pci_resource_len(ha->pdev, 0) < MIN_IOBASE_LEN) {
2092                 ql_log_pci(ql_log_warn, ha->pdev, 0x0119,
2093                     "Invalid PCI mem region size (%s), aborting\n",
2094                         pci_name(ha->pdev));
2095                 goto iospace_error_exit;
2096         }
2097
2098         ha->iobase = ioremap(pci_resource_start(ha->pdev, 0), MIN_IOBASE_LEN);
2099         if (!ha->iobase) {
2100                 ql_log_pci(ql_log_fatal, ha->pdev, 0x011a,
2101                     "Cannot remap MMIO (%s), aborting.\n",
2102                     pci_name(ha->pdev));
2103                 goto iospace_error_exit;
2104         }
2105
2106         /* 64bit PCI BAR - BAR2 will correspoond to region 4 */
2107         /* 83XX 26XX always use MQ type access for queues
2108          * - mbar 2, a.k.a region 4 */
2109         ha->max_req_queues = ha->max_rsp_queues = 1;
2110         ha->msix_count = QLA_BASE_VECTORS;
2111         ha->mqiobase = ioremap(pci_resource_start(ha->pdev, 4),
2112                         pci_resource_len(ha->pdev, 4));
2113
2114         if (!ha->mqiobase) {
2115                 ql_log_pci(ql_log_fatal, ha->pdev, 0x011d,
2116                     "BAR2/region4 not enabled\n");
2117                 goto mqiobase_exit;
2118         }
2119
2120         ha->msixbase = ioremap(pci_resource_start(ha->pdev, 2),
2121                         pci_resource_len(ha->pdev, 2));
2122         if (ha->msixbase) {
2123                 /* Read MSIX vector size of the board */
2124                 pci_read_config_word(ha->pdev,
2125                     QLA_83XX_PCI_MSIX_CONTROL, &msix);
2126                 ha->msix_count = (msix & PCI_MSIX_FLAGS_QSIZE)  + 1;
2127                 /*
2128                  * By default, driver uses at least two msix vectors
2129                  * (default & rspq)
2130                  */
2131                 if (ql2xmqsupport || ql2xnvmeenable) {
2132                         /* MB interrupt uses 1 vector */
2133                         ha->max_req_queues = ha->msix_count - 1;
2134
2135                         /* ATIOQ needs 1 vector. That's 1 less QPair */
2136                         if (QLA_TGT_MODE_ENABLED())
2137                                 ha->max_req_queues--;
2138
2139                         ha->max_rsp_queues = ha->max_req_queues;
2140
2141                         /* Queue pairs is the max value minus
2142                          * the base queue pair */
2143                         ha->max_qpairs = ha->max_req_queues - 1;
2144                         ql_dbg_pci(ql_dbg_init, ha->pdev, 0x00e3,
2145                             "Max no of queues pairs: %d.\n", ha->max_qpairs);
2146                 }
2147                 ql_log_pci(ql_log_info, ha->pdev, 0x011c,
2148                     "MSI-X vector count: %d.\n", ha->msix_count);
2149         } else
2150                 ql_log_pci(ql_log_info, ha->pdev, 0x011e,
2151                     "BAR 1 not enabled.\n");
2152
2153 mqiobase_exit:
2154         ql_dbg_pci(ql_dbg_init, ha->pdev, 0x011f,
2155             "MSIX Count: %d.\n", ha->msix_count);
2156         return 0;
2157
2158 iospace_error_exit:
2159         return -ENOMEM;
2160 }
2161
2162 static struct isp_operations qla2100_isp_ops = {
2163         .pci_config             = qla2100_pci_config,
2164         .reset_chip             = qla2x00_reset_chip,
2165         .chip_diag              = qla2x00_chip_diag,
2166         .config_rings           = qla2x00_config_rings,
2167         .reset_adapter          = qla2x00_reset_adapter,
2168         .nvram_config           = qla2x00_nvram_config,
2169         .update_fw_options      = qla2x00_update_fw_options,
2170         .load_risc              = qla2x00_load_risc,
2171         .pci_info_str           = qla2x00_pci_info_str,
2172         .fw_version_str         = qla2x00_fw_version_str,
2173         .intr_handler           = qla2100_intr_handler,
2174         .enable_intrs           = qla2x00_enable_intrs,
2175         .disable_intrs          = qla2x00_disable_intrs,
2176         .abort_command          = qla2x00_abort_command,
2177         .target_reset           = qla2x00_abort_target,
2178         .lun_reset              = qla2x00_lun_reset,
2179         .fabric_login           = qla2x00_login_fabric,
2180         .fabric_logout          = qla2x00_fabric_logout,
2181         .calc_req_entries       = qla2x00_calc_iocbs_32,
2182         .build_iocbs            = qla2x00_build_scsi_iocbs_32,
2183         .prep_ms_iocb           = qla2x00_prep_ms_iocb,
2184         .prep_ms_fdmi_iocb      = qla2x00_prep_ms_fdmi_iocb,
2185         .read_nvram             = qla2x00_read_nvram_data,
2186         .write_nvram            = qla2x00_write_nvram_data,
2187         .fw_dump                = qla2100_fw_dump,
2188         .beacon_on              = NULL,
2189         .beacon_off             = NULL,
2190         .beacon_blink           = NULL,
2191         .read_optrom            = qla2x00_read_optrom_data,
2192         .write_optrom           = qla2x00_write_optrom_data,
2193         .get_flash_version      = qla2x00_get_flash_version,
2194         .start_scsi             = qla2x00_start_scsi,
2195         .start_scsi_mq          = NULL,
2196         .abort_isp              = qla2x00_abort_isp,
2197         .iospace_config         = qla2x00_iospace_config,
2198         .initialize_adapter     = qla2x00_initialize_adapter,
2199 };
2200
2201 static struct isp_operations qla2300_isp_ops = {
2202         .pci_config             = qla2300_pci_config,
2203         .reset_chip             = qla2x00_reset_chip,
2204         .chip_diag              = qla2x00_chip_diag,
2205         .config_rings           = qla2x00_config_rings,
2206         .reset_adapter          = qla2x00_reset_adapter,
2207         .nvram_config           = qla2x00_nvram_config,
2208         .update_fw_options      = qla2x00_update_fw_options,
2209         .load_risc              = qla2x00_load_risc,
2210         .pci_info_str           = qla2x00_pci_info_str,
2211         .fw_version_str         = qla2x00_fw_version_str,
2212         .intr_handler           = qla2300_intr_handler,
2213         .enable_intrs           = qla2x00_enable_intrs,
2214         .disable_intrs          = qla2x00_disable_intrs,
2215         .abort_command          = qla2x00_abort_command,
2216         .target_reset           = qla2x00_abort_target,
2217         .lun_reset              = qla2x00_lun_reset,
2218         .fabric_login           = qla2x00_login_fabric,
2219         .fabric_logout          = qla2x00_fabric_logout,
2220         .calc_req_entries       = qla2x00_calc_iocbs_32,
2221         .build_iocbs            = qla2x00_build_scsi_iocbs_32,
2222         .prep_ms_iocb           = qla2x00_prep_ms_iocb,
2223         .prep_ms_fdmi_iocb      = qla2x00_prep_ms_fdmi_iocb,
2224         .read_nvram             = qla2x00_read_nvram_data,
2225         .write_nvram            = qla2x00_write_nvram_data,
2226         .fw_dump                = qla2300_fw_dump,
2227         .beacon_on              = qla2x00_beacon_on,
2228         .beacon_off             = qla2x00_beacon_off,
2229         .beacon_blink           = qla2x00_beacon_blink,
2230         .read_optrom            = qla2x00_read_optrom_data,
2231         .write_optrom           = qla2x00_write_optrom_data,
2232         .get_flash_version      = qla2x00_get_flash_version,
2233         .start_scsi             = qla2x00_start_scsi,
2234         .start_scsi_mq          = NULL,
2235         .abort_isp              = qla2x00_abort_isp,
2236         .iospace_config         = qla2x00_iospace_config,
2237         .initialize_adapter     = qla2x00_initialize_adapter,
2238 };
2239
2240 static struct isp_operations qla24xx_isp_ops = {
2241         .pci_config             = qla24xx_pci_config,
2242         .reset_chip             = qla24xx_reset_chip,
2243         .chip_diag              = qla24xx_chip_diag,
2244         .config_rings           = qla24xx_config_rings,
2245         .reset_adapter          = qla24xx_reset_adapter,
2246         .nvram_config           = qla24xx_nvram_config,
2247         .update_fw_options      = qla24xx_update_fw_options,
2248         .load_risc              = qla24xx_load_risc,
2249         .pci_info_str           = qla24xx_pci_info_str,
2250         .fw_version_str         = qla24xx_fw_version_str,
2251         .intr_handler           = qla24xx_intr_handler,
2252         .enable_intrs           = qla24xx_enable_intrs,
2253         .disable_intrs          = qla24xx_disable_intrs,
2254         .abort_command          = qla24xx_abort_command,
2255         .target_reset           = qla24xx_abort_target,
2256         .lun_reset              = qla24xx_lun_reset,
2257         .fabric_login           = qla24xx_login_fabric,
2258         .fabric_logout          = qla24xx_fabric_logout,
2259         .calc_req_entries       = NULL,
2260         .build_iocbs            = NULL,
2261         .prep_ms_iocb           = qla24xx_prep_ms_iocb,
2262         .prep_ms_fdmi_iocb      = qla24xx_prep_ms_fdmi_iocb,
2263         .read_nvram             = qla24xx_read_nvram_data,
2264         .write_nvram            = qla24xx_write_nvram_data,
2265         .fw_dump                = qla24xx_fw_dump,
2266         .beacon_on              = qla24xx_beacon_on,
2267         .beacon_off             = qla24xx_beacon_off,
2268         .beacon_blink           = qla24xx_beacon_blink,
2269         .read_optrom            = qla24xx_read_optrom_data,
2270         .write_optrom           = qla24xx_write_optrom_data,
2271         .get_flash_version      = qla24xx_get_flash_version,
2272         .start_scsi             = qla24xx_start_scsi,
2273         .start_scsi_mq          = NULL,
2274         .abort_isp              = qla2x00_abort_isp,
2275         .iospace_config         = qla2x00_iospace_config,
2276         .initialize_adapter     = qla2x00_initialize_adapter,
2277 };
2278
2279 static struct isp_operations qla25xx_isp_ops = {
2280         .pci_config             = qla25xx_pci_config,
2281         .reset_chip             = qla24xx_reset_chip,
2282         .chip_diag              = qla24xx_chip_diag,
2283         .config_rings           = qla24xx_config_rings,
2284         .reset_adapter          = qla24xx_reset_adapter,
2285         .nvram_config           = qla24xx_nvram_config,
2286         .update_fw_options      = qla24xx_update_fw_options,
2287         .load_risc              = qla24xx_load_risc,
2288         .pci_info_str           = qla24xx_pci_info_str,
2289         .fw_version_str         = qla24xx_fw_version_str,
2290         .intr_handler           = qla24xx_intr_handler,
2291         .enable_intrs           = qla24xx_enable_intrs,
2292         .disable_intrs          = qla24xx_disable_intrs,
2293         .abort_command          = qla24xx_abort_command,
2294         .target_reset           = qla24xx_abort_target,
2295         .lun_reset              = qla24xx_lun_reset,
2296         .fabric_login           = qla24xx_login_fabric,
2297         .fabric_logout          = qla24xx_fabric_logout,
2298         .calc_req_entries       = NULL,
2299         .build_iocbs            = NULL,
2300         .prep_ms_iocb           = qla24xx_prep_ms_iocb,
2301         .prep_ms_fdmi_iocb      = qla24xx_prep_ms_fdmi_iocb,
2302         .read_nvram             = qla25xx_read_nvram_data,
2303         .write_nvram            = qla25xx_write_nvram_data,
2304         .fw_dump                = qla25xx_fw_dump,
2305         .beacon_on              = qla24xx_beacon_on,
2306         .beacon_off             = qla24xx_beacon_off,
2307         .beacon_blink           = qla24xx_beacon_blink,
2308         .read_optrom            = qla25xx_read_optrom_data,
2309         .write_optrom           = qla24xx_write_optrom_data,
2310         .get_flash_version      = qla24xx_get_flash_version,
2311         .start_scsi             = qla24xx_dif_start_scsi,
2312         .start_scsi_mq          = qla2xxx_dif_start_scsi_mq,
2313         .abort_isp              = qla2x00_abort_isp,
2314         .iospace_config         = qla2x00_iospace_config,
2315         .initialize_adapter     = qla2x00_initialize_adapter,
2316 };
2317
2318 static struct isp_operations qla81xx_isp_ops = {
2319         .pci_config             = qla25xx_pci_config,
2320         .reset_chip             = qla24xx_reset_chip,
2321         .chip_diag              = qla24xx_chip_diag,
2322         .config_rings           = qla24xx_config_rings,
2323         .reset_adapter          = qla24xx_reset_adapter,
2324         .nvram_config           = qla81xx_nvram_config,
2325         .update_fw_options      = qla81xx_update_fw_options,
2326         .load_risc              = qla81xx_load_risc,
2327         .pci_info_str           = qla24xx_pci_info_str,
2328         .fw_version_str         = qla24xx_fw_version_str,
2329         .intr_handler           = qla24xx_intr_handler,
2330         .enable_intrs           = qla24xx_enable_intrs,
2331         .disable_intrs          = qla24xx_disable_intrs,
2332         .abort_command          = qla24xx_abort_command,
2333         .target_reset           = qla24xx_abort_target,
2334         .lun_reset              = qla24xx_lun_reset,
2335         .fabric_login           = qla24xx_login_fabric,
2336         .fabric_logout          = qla24xx_fabric_logout,
2337         .calc_req_entries       = NULL,
2338         .build_iocbs            = NULL,
2339         .prep_ms_iocb           = qla24xx_prep_ms_iocb,
2340         .prep_ms_fdmi_iocb      = qla24xx_prep_ms_fdmi_iocb,
2341         .read_nvram             = NULL,
2342         .write_nvram            = NULL,
2343         .fw_dump                = qla81xx_fw_dump,
2344         .beacon_on              = qla24xx_beacon_on,
2345         .beacon_off             = qla24xx_beacon_off,
2346         .beacon_blink           = qla83xx_beacon_blink,
2347         .read_optrom            = qla25xx_read_optrom_data,
2348         .write_optrom           = qla24xx_write_optrom_data,
2349         .get_flash_version      = qla24xx_get_flash_version,
2350         .start_scsi             = qla24xx_dif_start_scsi,
2351         .start_scsi_mq          = qla2xxx_dif_start_scsi_mq,
2352         .abort_isp              = qla2x00_abort_isp,
2353         .iospace_config         = qla2x00_iospace_config,
2354         .initialize_adapter     = qla2x00_initialize_adapter,
2355 };
2356
2357 static struct isp_operations qla82xx_isp_ops = {
2358         .pci_config             = qla82xx_pci_config,
2359         .reset_chip             = qla82xx_reset_chip,
2360         .chip_diag              = qla24xx_chip_diag,
2361         .config_rings           = qla82xx_config_rings,
2362         .reset_adapter          = qla24xx_reset_adapter,
2363         .nvram_config           = qla81xx_nvram_config,
2364         .update_fw_options      = qla24xx_update_fw_options,
2365         .load_risc              = qla82xx_load_risc,
2366         .pci_info_str           = qla24xx_pci_info_str,
2367         .fw_version_str         = qla24xx_fw_version_str,
2368         .intr_handler           = qla82xx_intr_handler,
2369         .enable_intrs           = qla82xx_enable_intrs,
2370         .disable_intrs          = qla82xx_disable_intrs,
2371         .abort_command          = qla24xx_abort_command,
2372         .target_reset           = qla24xx_abort_target,
2373         .lun_reset              = qla24xx_lun_reset,
2374         .fabric_login           = qla24xx_login_fabric,
2375         .fabric_logout          = qla24xx_fabric_logout,
2376         .calc_req_entries       = NULL,
2377         .build_iocbs            = NULL,
2378         .prep_ms_iocb           = qla24xx_prep_ms_iocb,
2379         .prep_ms_fdmi_iocb      = qla24xx_prep_ms_fdmi_iocb,
2380         .read_nvram             = qla24xx_read_nvram_data,
2381         .write_nvram            = qla24xx_write_nvram_data,
2382         .fw_dump                = qla82xx_fw_dump,
2383         .beacon_on              = qla82xx_beacon_on,
2384         .beacon_off             = qla82xx_beacon_off,
2385         .beacon_blink           = NULL,
2386         .read_optrom            = qla82xx_read_optrom_data,
2387         .write_optrom           = qla82xx_write_optrom_data,
2388         .get_flash_version      = qla82xx_get_flash_version,
2389         .start_scsi             = qla82xx_start_scsi,
2390         .start_scsi_mq          = NULL,
2391         .abort_isp              = qla82xx_abort_isp,
2392         .iospace_config         = qla82xx_iospace_config,
2393         .initialize_adapter     = qla2x00_initialize_adapter,
2394 };
2395
2396 static struct isp_operations qla8044_isp_ops = {
2397         .pci_config             = qla82xx_pci_config,
2398         .reset_chip             = qla82xx_reset_chip,
2399         .chip_diag              = qla24xx_chip_diag,
2400         .config_rings           = qla82xx_config_rings,
2401         .reset_adapter          = qla24xx_reset_adapter,
2402         .nvram_config           = qla81xx_nvram_config,
2403         .update_fw_options      = qla24xx_update_fw_options,
2404         .load_risc              = qla82xx_load_risc,
2405         .pci_info_str           = qla24xx_pci_info_str,
2406         .fw_version_str         = qla24xx_fw_version_str,
2407         .intr_handler           = qla8044_intr_handler,
2408         .enable_intrs           = qla82xx_enable_intrs,
2409         .disable_intrs          = qla82xx_disable_intrs,
2410         .abort_command          = qla24xx_abort_command,
2411         .target_reset           = qla24xx_abort_target,
2412         .lun_reset              = qla24xx_lun_reset,
2413         .fabric_login           = qla24xx_login_fabric,
2414         .fabric_logout          = qla24xx_fabric_logout,
2415         .calc_req_entries       = NULL,
2416         .build_iocbs            = NULL,
2417         .prep_ms_iocb           = qla24xx_prep_ms_iocb,
2418         .prep_ms_fdmi_iocb      = qla24xx_prep_ms_fdmi_iocb,
2419         .read_nvram             = NULL,
2420         .write_nvram            = NULL,
2421         .fw_dump                = qla8044_fw_dump,
2422         .beacon_on              = qla82xx_beacon_on,
2423         .beacon_off             = qla82xx_beacon_off,
2424         .beacon_blink           = NULL,
2425         .read_optrom            = qla8044_read_optrom_data,
2426         .write_optrom           = qla8044_write_optrom_data,
2427         .get_flash_version      = qla82xx_get_flash_version,
2428         .start_scsi             = qla82xx_start_scsi,
2429         .start_scsi_mq          = NULL,
2430         .abort_isp              = qla8044_abort_isp,
2431         .iospace_config         = qla82xx_iospace_config,
2432         .initialize_adapter     = qla2x00_initialize_adapter,
2433 };
2434
2435 static struct isp_operations qla83xx_isp_ops = {
2436         .pci_config             = qla25xx_pci_config,
2437         .reset_chip             = qla24xx_reset_chip,
2438         .chip_diag              = qla24xx_chip_diag,
2439         .config_rings           = qla24xx_config_rings,
2440         .reset_adapter          = qla24xx_reset_adapter,
2441         .nvram_config           = qla81xx_nvram_config,
2442         .update_fw_options      = qla81xx_update_fw_options,
2443         .load_risc              = qla81xx_load_risc,
2444         .pci_info_str           = qla24xx_pci_info_str,
2445         .fw_version_str         = qla24xx_fw_version_str,
2446         .intr_handler           = qla24xx_intr_handler,
2447         .enable_intrs           = qla24xx_enable_intrs,
2448         .disable_intrs          = qla24xx_disable_intrs,
2449         .abort_command          = qla24xx_abort_command,
2450         .target_reset           = qla24xx_abort_target,
2451         .lun_reset              = qla24xx_lun_reset,
2452         .fabric_login           = qla24xx_login_fabric,
2453         .fabric_logout          = qla24xx_fabric_logout,
2454         .calc_req_entries       = NULL,
2455         .build_iocbs            = NULL,
2456         .prep_ms_iocb           = qla24xx_prep_ms_iocb,
2457         .prep_ms_fdmi_iocb      = qla24xx_prep_ms_fdmi_iocb,
2458         .read_nvram             = NULL,
2459         .write_nvram            = NULL,
2460         .fw_dump                = qla83xx_fw_dump,
2461         .beacon_on              = qla24xx_beacon_on,
2462         .beacon_off             = qla24xx_beacon_off,
2463         .beacon_blink           = qla83xx_beacon_blink,
2464         .read_optrom            = qla25xx_read_optrom_data,
2465         .write_optrom           = qla24xx_write_optrom_data,
2466         .get_flash_version      = qla24xx_get_flash_version,
2467         .start_scsi             = qla24xx_dif_start_scsi,
2468         .start_scsi_mq          = qla2xxx_dif_start_scsi_mq,
2469         .abort_isp              = qla2x00_abort_isp,
2470         .iospace_config         = qla83xx_iospace_config,
2471         .initialize_adapter     = qla2x00_initialize_adapter,
2472 };
2473
2474 static struct isp_operations qlafx00_isp_ops = {
2475         .pci_config             = qlafx00_pci_config,
2476         .reset_chip             = qlafx00_soft_reset,
2477         .chip_diag              = qlafx00_chip_diag,
2478         .config_rings           = qlafx00_config_rings,
2479         .reset_adapter          = qlafx00_soft_reset,
2480         .nvram_config           = NULL,
2481         .update_fw_options      = NULL,
2482         .load_risc              = NULL,
2483         .pci_info_str           = qlafx00_pci_info_str,
2484         .fw_version_str         = qlafx00_fw_version_str,
2485         .intr_handler           = qlafx00_intr_handler,
2486         .enable_intrs           = qlafx00_enable_intrs,
2487         .disable_intrs          = qlafx00_disable_intrs,
2488         .abort_command          = qla24xx_async_abort_command,
2489         .target_reset           = qlafx00_abort_target,
2490         .lun_reset              = qlafx00_lun_reset,
2491         .fabric_login           = NULL,
2492         .fabric_logout          = NULL,
2493         .calc_req_entries       = NULL,
2494         .build_iocbs            = NULL,
2495         .prep_ms_iocb           = qla24xx_prep_ms_iocb,
2496         .prep_ms_fdmi_iocb      = qla24xx_prep_ms_fdmi_iocb,
2497         .read_nvram             = qla24xx_read_nvram_data,
2498         .write_nvram            = qla24xx_write_nvram_data,
2499         .fw_dump                = NULL,
2500         .beacon_on              = qla24xx_beacon_on,
2501         .beacon_off             = qla24xx_beacon_off,
2502         .beacon_blink           = NULL,
2503         .read_optrom            = qla24xx_read_optrom_data,
2504         .write_optrom           = qla24xx_write_optrom_data,
2505         .get_flash_version      = qla24xx_get_flash_version,
2506         .start_scsi             = qlafx00_start_scsi,
2507         .start_scsi_mq          = NULL,
2508         .abort_isp              = qlafx00_abort_isp,
2509         .iospace_config         = qlafx00_iospace_config,
2510         .initialize_adapter     = qlafx00_initialize_adapter,
2511 };
2512
2513 static struct isp_operations qla27xx_isp_ops = {
2514         .pci_config             = qla25xx_pci_config,
2515         .reset_chip             = qla24xx_reset_chip,
2516         .chip_diag              = qla24xx_chip_diag,
2517         .config_rings           = qla24xx_config_rings,
2518         .reset_adapter          = qla24xx_reset_adapter,
2519         .nvram_config           = qla81xx_nvram_config,
2520         .update_fw_options      = qla81xx_update_fw_options,
2521         .load_risc              = qla81xx_load_risc,
2522         .pci_info_str           = qla24xx_pci_info_str,
2523         .fw_version_str         = qla24xx_fw_version_str,
2524         .intr_handler           = qla24xx_intr_handler,
2525         .enable_intrs           = qla24xx_enable_intrs,
2526         .disable_intrs          = qla24xx_disable_intrs,
2527         .abort_command          = qla24xx_abort_command,
2528         .target_reset           = qla24xx_abort_target,
2529         .lun_reset              = qla24xx_lun_reset,
2530         .fabric_login           = qla24xx_login_fabric,
2531         .fabric_logout          = qla24xx_fabric_logout,
2532         .calc_req_entries       = NULL,
2533         .build_iocbs            = NULL,
2534         .prep_ms_iocb           = qla24xx_prep_ms_iocb,
2535         .prep_ms_fdmi_iocb      = qla24xx_prep_ms_fdmi_iocb,
2536         .read_nvram             = NULL,
2537         .write_nvram            = NULL,
2538         .fw_dump                = qla27xx_fwdump,
2539         .beacon_on              = qla24xx_beacon_on,
2540         .beacon_off             = qla24xx_beacon_off,
2541         .beacon_blink           = qla83xx_beacon_blink,
2542         .read_optrom            = qla25xx_read_optrom_data,
2543         .write_optrom           = qla24xx_write_optrom_data,
2544         .get_flash_version      = qla24xx_get_flash_version,
2545         .start_scsi             = qla24xx_dif_start_scsi,
2546         .start_scsi_mq          = qla2xxx_dif_start_scsi_mq,
2547         .abort_isp              = qla2x00_abort_isp,
2548         .iospace_config         = qla83xx_iospace_config,
2549         .initialize_adapter     = qla2x00_initialize_adapter,
2550 };
2551
2552 static inline void
2553 qla2x00_set_isp_flags(struct qla_hw_data *ha)
2554 {
2555         ha->device_type = DT_EXTENDED_IDS;
2556         switch (ha->pdev->device) {
2557         case PCI_DEVICE_ID_QLOGIC_ISP2100:
2558                 ha->isp_type |= DT_ISP2100;
2559                 ha->device_type &= ~DT_EXTENDED_IDS;
2560                 ha->fw_srisc_address = RISC_START_ADDRESS_2100;
2561                 break;
2562         case PCI_DEVICE_ID_QLOGIC_ISP2200:
2563                 ha->isp_type |= DT_ISP2200;
2564                 ha->device_type &= ~DT_EXTENDED_IDS;
2565                 ha->fw_srisc_address = RISC_START_ADDRESS_2100;
2566                 break;
2567         case PCI_DEVICE_ID_QLOGIC_ISP2300:
2568                 ha->isp_type |= DT_ISP2300;
2569                 ha->device_type |= DT_ZIO_SUPPORTED;
2570                 ha->fw_srisc_address = RISC_START_ADDRESS_2300;
2571                 break;
2572         case PCI_DEVICE_ID_QLOGIC_ISP2312:
2573                 ha->isp_type |= DT_ISP2312;
2574                 ha->device_type |= DT_ZIO_SUPPORTED;
2575                 ha->fw_srisc_address = RISC_START_ADDRESS_2300;
2576                 break;
2577         case PCI_DEVICE_ID_QLOGIC_ISP2322:
2578                 ha->isp_type |= DT_ISP2322;
2579                 ha->device_type |= DT_ZIO_SUPPORTED;
2580                 if (ha->pdev->subsystem_vendor == 0x1028 &&
2581                     ha->pdev->subsystem_device == 0x0170)
2582                         ha->device_type |= DT_OEM_001;
2583                 ha->fw_srisc_address = RISC_START_ADDRESS_2300;
2584                 break;
2585         case PCI_DEVICE_ID_QLOGIC_ISP6312:
2586                 ha->isp_type |= DT_ISP6312;
2587                 ha->fw_srisc_address = RISC_START_ADDRESS_2300;
2588                 break;
2589         case PCI_DEVICE_ID_QLOGIC_ISP6322:
2590                 ha->isp_type |= DT_ISP6322;
2591                 ha->fw_srisc_address = RISC_START_ADDRESS_2300;
2592                 break;
2593         case PCI_DEVICE_ID_QLOGIC_ISP2422:
2594                 ha->isp_type |= DT_ISP2422;
2595                 ha->device_type |= DT_ZIO_SUPPORTED;
2596                 ha->device_type |= DT_FWI2;
2597                 ha->device_type |= DT_IIDMA;
2598                 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2599                 break;
2600         case PCI_DEVICE_ID_QLOGIC_ISP2432:
2601                 ha->isp_type |= DT_ISP2432;
2602                 ha->device_type |= DT_ZIO_SUPPORTED;
2603                 ha->device_type |= DT_FWI2;
2604                 ha->device_type |= DT_IIDMA;
2605                 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2606                 break;
2607         case PCI_DEVICE_ID_QLOGIC_ISP8432:
2608                 ha->isp_type |= DT_ISP8432;
2609                 ha->device_type |= DT_ZIO_SUPPORTED;
2610                 ha->device_type |= DT_FWI2;
2611                 ha->device_type |= DT_IIDMA;
2612                 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2613                 break;
2614         case PCI_DEVICE_ID_QLOGIC_ISP5422:
2615                 ha->isp_type |= DT_ISP5422;
2616                 ha->device_type |= DT_FWI2;
2617                 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2618                 break;
2619         case PCI_DEVICE_ID_QLOGIC_ISP5432:
2620                 ha->isp_type |= DT_ISP5432;
2621                 ha->device_type |= DT_FWI2;
2622                 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2623                 break;
2624         case PCI_DEVICE_ID_QLOGIC_ISP2532:
2625                 ha->isp_type |= DT_ISP2532;
2626                 ha->device_type |= DT_ZIO_SUPPORTED;
2627                 ha->device_type |= DT_FWI2;
2628                 ha->device_type |= DT_IIDMA;
2629                 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2630                 break;
2631         case PCI_DEVICE_ID_QLOGIC_ISP8001:
2632                 ha->isp_type |= DT_ISP8001;
2633                 ha->device_type |= DT_ZIO_SUPPORTED;
2634                 ha->device_type |= DT_FWI2;
2635                 ha->device_type |= DT_IIDMA;
2636                 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2637                 break;
2638         case PCI_DEVICE_ID_QLOGIC_ISP8021:
2639                 ha->isp_type |= DT_ISP8021;
2640                 ha->device_type |= DT_ZIO_SUPPORTED;
2641                 ha->device_type |= DT_FWI2;
2642                 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2643                 /* Initialize 82XX ISP flags */
2644                 qla82xx_init_flags(ha);
2645                 break;
2646          case PCI_DEVICE_ID_QLOGIC_ISP8044:
2647                 ha->isp_type |= DT_ISP8044;
2648                 ha->device_type |= DT_ZIO_SUPPORTED;
2649                 ha->device_type |= DT_FWI2;
2650                 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2651                 /* Initialize 82XX ISP flags */
2652                 qla82xx_init_flags(ha);
2653                 break;
2654         case PCI_DEVICE_ID_QLOGIC_ISP2031:
2655                 ha->isp_type |= DT_ISP2031;
2656                 ha->device_type |= DT_ZIO_SUPPORTED;
2657                 ha->device_type |= DT_FWI2;
2658                 ha->device_type |= DT_IIDMA;
2659                 ha->device_type |= DT_T10_PI;
2660                 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2661                 break;
2662         case PCI_DEVICE_ID_QLOGIC_ISP8031:
2663                 ha->isp_type |= DT_ISP8031;
2664                 ha->device_type |= DT_ZIO_SUPPORTED;
2665                 ha->device_type |= DT_FWI2;
2666                 ha->device_type |= DT_IIDMA;
2667                 ha->device_type |= DT_T10_PI;
2668                 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2669                 break;
2670         case PCI_DEVICE_ID_QLOGIC_ISPF001:
2671                 ha->isp_type |= DT_ISPFX00;
2672                 break;
2673         case PCI_DEVICE_ID_QLOGIC_ISP2071:
2674                 ha->isp_type |= DT_ISP2071;
2675                 ha->device_type |= DT_ZIO_SUPPORTED;
2676                 ha->device_type |= DT_FWI2;
2677                 ha->device_type |= DT_IIDMA;
2678                 ha->device_type |= DT_T10_PI;
2679                 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2680                 break;
2681         case PCI_DEVICE_ID_QLOGIC_ISP2271:
2682                 ha->isp_type |= DT_ISP2271;
2683                 ha->device_type |= DT_ZIO_SUPPORTED;
2684                 ha->device_type |= DT_FWI2;
2685                 ha->device_type |= DT_IIDMA;
2686                 ha->device_type |= DT_T10_PI;
2687                 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2688                 break;
2689         case PCI_DEVICE_ID_QLOGIC_ISP2261:
2690                 ha->isp_type |= DT_ISP2261;
2691                 ha->device_type |= DT_ZIO_SUPPORTED;
2692                 ha->device_type |= DT_FWI2;
2693                 ha->device_type |= DT_IIDMA;
2694                 ha->device_type |= DT_T10_PI;
2695                 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2696                 break;
2697         }
2698
2699         if (IS_QLA82XX(ha))
2700                 ha->port_no = ha->portnum & 1;
2701         else {
2702                 /* Get adapter physical port no from interrupt pin register. */
2703                 pci_read_config_byte(ha->pdev, PCI_INTERRUPT_PIN, &ha->port_no);
2704                 if (IS_QLA27XX(ha))
2705                         ha->port_no--;
2706                 else
2707                         ha->port_no = !(ha->port_no & 1);
2708         }
2709
2710         ql_dbg_pci(ql_dbg_init, ha->pdev, 0x000b,
2711             "device_type=0x%x port=%d fw_srisc_address=0x%x.\n",
2712             ha->device_type, ha->port_no, ha->fw_srisc_address);
2713 }
2714
2715 static void
2716 qla2xxx_scan_start(struct Scsi_Host *shost)
2717 {
2718         scsi_qla_host_t *vha = shost_priv(shost);
2719
2720         if (vha->hw->flags.running_gold_fw)
2721                 return;
2722
2723         set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
2724         set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
2725         set_bit(RSCN_UPDATE, &vha->dpc_flags);
2726         set_bit(NPIV_CONFIG_NEEDED, &vha->dpc_flags);
2727 }
2728
2729 static int
2730 qla2xxx_scan_finished(struct Scsi_Host *shost, unsigned long time)
2731 {
2732         scsi_qla_host_t *vha = shost_priv(shost);
2733
2734         if (test_bit(UNLOADING, &vha->dpc_flags))
2735                 return 1;
2736         if (!vha->host)
2737                 return 1;
2738         if (time > vha->hw->loop_reset_delay * HZ)
2739                 return 1;
2740
2741         return atomic_read(&vha->loop_state) == LOOP_READY;
2742 }
2743
2744 static void qla2x00_iocb_work_fn(struct work_struct *work)
2745 {
2746         struct scsi_qla_host *vha = container_of(work,
2747                 struct scsi_qla_host, iocb_work);
2748         struct qla_hw_data *ha = vha->hw;
2749         struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
2750         int i = 2;
2751         unsigned long flags;
2752
2753         if (test_bit(UNLOADING, &base_vha->dpc_flags))
2754                 return;
2755
2756         while (!list_empty(&vha->work_list) && i > 0) {
2757                 qla2x00_do_work(vha);
2758                 i--;
2759         }
2760
2761         spin_lock_irqsave(&vha->work_lock, flags);
2762         clear_bit(IOCB_WORK_ACTIVE, &vha->dpc_flags);
2763         spin_unlock_irqrestore(&vha->work_lock, flags);
2764 }
2765
2766 /*
2767  * PCI driver interface
2768  */
2769 static int
2770 qla2x00_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
2771 {
2772         int     ret = -ENODEV;
2773         struct Scsi_Host *host;
2774         scsi_qla_host_t *base_vha = NULL;
2775         struct qla_hw_data *ha;
2776         char pci_info[30];
2777         char fw_str[30], wq_name[30];
2778         struct scsi_host_template *sht;
2779         int bars, mem_only = 0;
2780         uint16_t req_length = 0, rsp_length = 0;
2781         struct req_que *req = NULL;
2782         struct rsp_que *rsp = NULL;
2783         int i;
2784
2785         bars = pci_select_bars(pdev, IORESOURCE_MEM | IORESOURCE_IO);
2786         sht = &qla2xxx_driver_template;
2787         if (pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2422 ||
2788             pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2432 ||
2789             pdev->device == PCI_DEVICE_ID_QLOGIC_ISP8432 ||
2790             pdev->device == PCI_DEVICE_ID_QLOGIC_ISP5422 ||
2791             pdev->device == PCI_DEVICE_ID_QLOGIC_ISP5432 ||
2792             pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2532 ||
2793             pdev->device == PCI_DEVICE_ID_QLOGIC_ISP8001 ||
2794             pdev->device == PCI_DEVICE_ID_QLOGIC_ISP8021 ||
2795             pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2031 ||
2796             pdev->device == PCI_DEVICE_ID_QLOGIC_ISP8031 ||
2797             pdev->device == PCI_DEVICE_ID_QLOGIC_ISPF001 ||
2798             pdev->device == PCI_DEVICE_ID_QLOGIC_ISP8044 ||
2799             pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2071 ||
2800             pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2271 ||
2801             pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2261) {
2802                 bars = pci_select_bars(pdev, IORESOURCE_MEM);
2803                 mem_only = 1;
2804                 ql_dbg_pci(ql_dbg_init, pdev, 0x0007,
2805                     "Mem only adapter.\n");
2806         }
2807         ql_dbg_pci(ql_dbg_init, pdev, 0x0008,
2808             "Bars=%d.\n", bars);
2809
2810         if (mem_only) {
2811                 if (pci_enable_device_mem(pdev))
2812                         return ret;
2813         } else {
2814                 if (pci_enable_device(pdev))
2815                         return ret;
2816         }
2817
2818         /* This may fail but that's ok */
2819         pci_enable_pcie_error_reporting(pdev);
2820
2821         ha = kzalloc(sizeof(struct qla_hw_data), GFP_KERNEL);
2822         if (!ha) {
2823                 ql_log_pci(ql_log_fatal, pdev, 0x0009,
2824                     "Unable to allocate memory for ha.\n");
2825                 goto disable_device;
2826         }
2827         ql_dbg_pci(ql_dbg_init, pdev, 0x000a,
2828             "Memory allocated for ha=%p.\n", ha);
2829         ha->pdev = pdev;
2830         INIT_LIST_HEAD(&ha->tgt.q_full_list);
2831         spin_lock_init(&ha->tgt.q_full_lock);
2832         spin_lock_init(&ha->tgt.sess_lock);
2833         spin_lock_init(&ha->tgt.atio_lock);
2834
2835         atomic_set(&ha->nvme_active_aen_cnt, 0);
2836
2837         /* Clear our data area */
2838         ha->bars = bars;
2839         ha->mem_only = mem_only;
2840         spin_lock_init(&ha->hardware_lock);
2841         spin_lock_init(&ha->vport_slock);
2842         mutex_init(&ha->selflogin_lock);
2843         mutex_init(&ha->optrom_mutex);
2844
2845         /* Set ISP-type information. */
2846         qla2x00_set_isp_flags(ha);
2847
2848         /* Set EEH reset type to fundamental if required by hba */
2849         if (IS_QLA24XX(ha) || IS_QLA25XX(ha) || IS_QLA81XX(ha) ||
2850             IS_QLA83XX(ha) || IS_QLA27XX(ha))
2851                 pdev->needs_freset = 1;
2852
2853         ha->prev_topology = 0;
2854         ha->init_cb_size = sizeof(init_cb_t);
2855         ha->link_data_rate = PORT_SPEED_UNKNOWN;
2856         ha->optrom_size = OPTROM_SIZE_2300;
2857         ha->max_exchg = FW_MAX_EXCHANGES_CNT;
2858         atomic_set(&ha->num_pend_mbx_stage1, 0);
2859         atomic_set(&ha->num_pend_mbx_stage2, 0);
2860         atomic_set(&ha->num_pend_mbx_stage3, 0);
2861         atomic_set(&ha->zio_threshold, DEFAULT_ZIO_THRESHOLD);
2862         ha->last_zio_threshold = DEFAULT_ZIO_THRESHOLD;
2863
2864         /* Assign ISP specific operations. */
2865         if (IS_QLA2100(ha)) {
2866                 ha->max_fibre_devices = MAX_FIBRE_DEVICES_2100;
2867                 ha->mbx_count = MAILBOX_REGISTER_COUNT_2100;
2868                 req_length = REQUEST_ENTRY_CNT_2100;
2869                 rsp_length = RESPONSE_ENTRY_CNT_2100;
2870                 ha->max_loop_id = SNS_LAST_LOOP_ID_2100;
2871                 ha->gid_list_info_size = 4;
2872                 ha->flash_conf_off = ~0;
2873                 ha->flash_data_off = ~0;
2874                 ha->nvram_conf_off = ~0;
2875                 ha->nvram_data_off = ~0;
2876                 ha->isp_ops = &qla2100_isp_ops;
2877         } else if (IS_QLA2200(ha)) {
2878                 ha->max_fibre_devices = MAX_FIBRE_DEVICES_2100;
2879                 ha->mbx_count = MAILBOX_REGISTER_COUNT_2200;
2880                 req_length = REQUEST_ENTRY_CNT_2200;
2881                 rsp_length = RESPONSE_ENTRY_CNT_2100;
2882                 ha->max_loop_id = SNS_LAST_LOOP_ID_2100;
2883                 ha->gid_list_info_size = 4;
2884                 ha->flash_conf_off = ~0;
2885                 ha->flash_data_off = ~0;
2886                 ha->nvram_conf_off = ~0;
2887                 ha->nvram_data_off = ~0;
2888                 ha->isp_ops = &qla2100_isp_ops;
2889         } else if (IS_QLA23XX(ha)) {
2890                 ha->max_fibre_devices = MAX_FIBRE_DEVICES_2100;
2891                 ha->mbx_count = MAILBOX_REGISTER_COUNT;
2892                 req_length = REQUEST_ENTRY_CNT_2200;
2893                 rsp_length = RESPONSE_ENTRY_CNT_2300;
2894                 ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
2895                 ha->gid_list_info_size = 6;
2896                 if (IS_QLA2322(ha) || IS_QLA6322(ha))
2897                         ha->optrom_size = OPTROM_SIZE_2322;
2898                 ha->flash_conf_off = ~0;
2899                 ha->flash_data_off = ~0;
2900                 ha->nvram_conf_off = ~0;
2901                 ha->nvram_data_off = ~0;
2902                 ha->isp_ops = &qla2300_isp_ops;
2903         } else if (IS_QLA24XX_TYPE(ha)) {
2904                 ha->max_fibre_devices = MAX_FIBRE_DEVICES_2400;
2905                 ha->mbx_count = MAILBOX_REGISTER_COUNT;
2906                 req_length = REQUEST_ENTRY_CNT_24XX;
2907                 rsp_length = RESPONSE_ENTRY_CNT_2300;
2908                 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
2909                 ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
2910                 ha->init_cb_size = sizeof(struct mid_init_cb_24xx);
2911                 ha->gid_list_info_size = 8;
2912                 ha->optrom_size = OPTROM_SIZE_24XX;
2913                 ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA24XX;
2914                 ha->isp_ops = &qla24xx_isp_ops;
2915                 ha->flash_conf_off = FARX_ACCESS_FLASH_CONF;
2916                 ha->flash_data_off = FARX_ACCESS_FLASH_DATA;
2917                 ha->nvram_conf_off = FARX_ACCESS_NVRAM_CONF;
2918                 ha->nvram_data_off = FARX_ACCESS_NVRAM_DATA;
2919         } else if (IS_QLA25XX(ha)) {
2920                 ha->max_fibre_devices = MAX_FIBRE_DEVICES_2400;
2921                 ha->mbx_count = MAILBOX_REGISTER_COUNT;
2922                 req_length = REQUEST_ENTRY_CNT_24XX;
2923                 rsp_length = RESPONSE_ENTRY_CNT_2300;
2924                 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
2925                 ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
2926                 ha->init_cb_size = sizeof(struct mid_init_cb_24xx);
2927                 ha->gid_list_info_size = 8;
2928                 ha->optrom_size = OPTROM_SIZE_25XX;
2929                 ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA25XX;
2930                 ha->isp_ops = &qla25xx_isp_ops;
2931                 ha->flash_conf_off = FARX_ACCESS_FLASH_CONF;
2932                 ha->flash_data_off = FARX_ACCESS_FLASH_DATA;
2933                 ha->nvram_conf_off = FARX_ACCESS_NVRAM_CONF;
2934                 ha->nvram_data_off = FARX_ACCESS_NVRAM_DATA;
2935         } else if (IS_QLA81XX(ha)) {
2936                 ha->max_fibre_devices = MAX_FIBRE_DEVICES_2400;
2937                 ha->mbx_count = MAILBOX_REGISTER_COUNT;
2938                 req_length = REQUEST_ENTRY_CNT_24XX;
2939                 rsp_length = RESPONSE_ENTRY_CNT_2300;
2940                 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
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_81XX;
2945                 ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA25XX;
2946                 ha->isp_ops = &qla81xx_isp_ops;
2947                 ha->flash_conf_off = FARX_ACCESS_FLASH_CONF_81XX;
2948                 ha->flash_data_off = FARX_ACCESS_FLASH_DATA_81XX;
2949                 ha->nvram_conf_off = ~0;
2950                 ha->nvram_data_off = ~0;
2951         } else if (IS_QLA82XX(ha)) {
2952                 ha->max_fibre_devices = MAX_FIBRE_DEVICES_2400;
2953                 ha->mbx_count = MAILBOX_REGISTER_COUNT;
2954                 req_length = REQUEST_ENTRY_CNT_82XX;
2955                 rsp_length = RESPONSE_ENTRY_CNT_82XX;
2956                 ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
2957                 ha->init_cb_size = sizeof(struct mid_init_cb_81xx);
2958                 ha->gid_list_info_size = 8;
2959                 ha->optrom_size = OPTROM_SIZE_82XX;
2960                 ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA25XX;
2961                 ha->isp_ops = &qla82xx_isp_ops;
2962                 ha->flash_conf_off = FARX_ACCESS_FLASH_CONF;
2963                 ha->flash_data_off = FARX_ACCESS_FLASH_DATA;
2964                 ha->nvram_conf_off = FARX_ACCESS_NVRAM_CONF;
2965                 ha->nvram_data_off = FARX_ACCESS_NVRAM_DATA;
2966         } else if (IS_QLA8044(ha)) {
2967                 ha->max_fibre_devices = MAX_FIBRE_DEVICES_2400;
2968                 ha->mbx_count = MAILBOX_REGISTER_COUNT;
2969                 req_length = REQUEST_ENTRY_CNT_82XX;
2970                 rsp_length = RESPONSE_ENTRY_CNT_82XX;
2971                 ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
2972                 ha->init_cb_size = sizeof(struct mid_init_cb_81xx);
2973                 ha->gid_list_info_size = 8;
2974                 ha->optrom_size = OPTROM_SIZE_83XX;
2975                 ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA25XX;
2976                 ha->isp_ops = &qla8044_isp_ops;
2977                 ha->flash_conf_off = FARX_ACCESS_FLASH_CONF;
2978                 ha->flash_data_off = FARX_ACCESS_FLASH_DATA;
2979                 ha->nvram_conf_off = FARX_ACCESS_NVRAM_CONF;
2980                 ha->nvram_data_off = FARX_ACCESS_NVRAM_DATA;
2981         } else if (IS_QLA83XX(ha)) {
2982                 ha->portnum = PCI_FUNC(ha->pdev->devfn);
2983                 ha->max_fibre_devices = MAX_FIBRE_DEVICES_2400;
2984                 ha->mbx_count = MAILBOX_REGISTER_COUNT;
2985                 req_length = REQUEST_ENTRY_CNT_83XX;
2986                 rsp_length = RESPONSE_ENTRY_CNT_83XX;
2987                 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
2988                 ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
2989                 ha->init_cb_size = sizeof(struct mid_init_cb_81xx);
2990                 ha->gid_list_info_size = 8;
2991                 ha->optrom_size = OPTROM_SIZE_83XX;
2992                 ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA25XX;
2993                 ha->isp_ops = &qla83xx_isp_ops;
2994                 ha->flash_conf_off = FARX_ACCESS_FLASH_CONF_81XX;
2995                 ha->flash_data_off = FARX_ACCESS_FLASH_DATA_81XX;
2996                 ha->nvram_conf_off = ~0;
2997                 ha->nvram_data_off = ~0;
2998         }  else if (IS_QLAFX00(ha)) {
2999                 ha->max_fibre_devices = MAX_FIBRE_DEVICES_FX00;
3000                 ha->mbx_count = MAILBOX_REGISTER_COUNT_FX00;
3001                 ha->aen_mbx_count = AEN_MAILBOX_REGISTER_COUNT_FX00;
3002                 req_length = REQUEST_ENTRY_CNT_FX00;
3003                 rsp_length = RESPONSE_ENTRY_CNT_FX00;
3004                 ha->isp_ops = &qlafx00_isp_ops;
3005                 ha->port_down_retry_count = 30; /* default value */
3006                 ha->mr.fw_hbt_cnt = QLAFX00_HEARTBEAT_INTERVAL;
3007                 ha->mr.fw_reset_timer_tick = QLAFX00_RESET_INTERVAL;
3008                 ha->mr.fw_critemp_timer_tick = QLAFX00_CRITEMP_INTERVAL;
3009                 ha->mr.fw_hbt_en = 1;
3010                 ha->mr.host_info_resend = false;
3011                 ha->mr.hinfo_resend_timer_tick = QLAFX00_HINFO_RESEND_INTERVAL;
3012         } else if (IS_QLA27XX(ha)) {
3013                 ha->portnum = PCI_FUNC(ha->pdev->devfn);
3014                 ha->max_fibre_devices = MAX_FIBRE_DEVICES_2400;
3015                 ha->mbx_count = MAILBOX_REGISTER_COUNT;
3016                 req_length = REQUEST_ENTRY_CNT_83XX;
3017                 rsp_length = RESPONSE_ENTRY_CNT_83XX;
3018                 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
3019                 ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
3020                 ha->init_cb_size = sizeof(struct mid_init_cb_81xx);
3021                 ha->gid_list_info_size = 8;
3022                 ha->optrom_size = OPTROM_SIZE_83XX;
3023                 ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA25XX;
3024                 ha->isp_ops = &qla27xx_isp_ops;
3025                 ha->flash_conf_off = FARX_ACCESS_FLASH_CONF_81XX;
3026                 ha->flash_data_off = FARX_ACCESS_FLASH_DATA_81XX;
3027                 ha->nvram_conf_off = ~0;
3028                 ha->nvram_data_off = ~0;
3029         }
3030
3031         ql_dbg_pci(ql_dbg_init, pdev, 0x001e,
3032             "mbx_count=%d, req_length=%d, "
3033             "rsp_length=%d, max_loop_id=%d, init_cb_size=%d, "
3034             "gid_list_info_size=%d, optrom_size=%d, nvram_npiv_size=%d, "
3035             "max_fibre_devices=%d.\n",
3036             ha->mbx_count, req_length, rsp_length, ha->max_loop_id,
3037             ha->init_cb_size, ha->gid_list_info_size, ha->optrom_size,
3038             ha->nvram_npiv_size, ha->max_fibre_devices);
3039         ql_dbg_pci(ql_dbg_init, pdev, 0x001f,
3040             "isp_ops=%p, flash_conf_off=%d, "
3041             "flash_data_off=%d, nvram_conf_off=%d, nvram_data_off=%d.\n",
3042             ha->isp_ops, ha->flash_conf_off, ha->flash_data_off,
3043             ha->nvram_conf_off, ha->nvram_data_off);
3044
3045         /* Configure PCI I/O space */
3046         ret = ha->isp_ops->iospace_config(ha);
3047         if (ret)
3048                 goto iospace_config_failed;
3049
3050         ql_log_pci(ql_log_info, pdev, 0x001d,
3051             "Found an ISP%04X irq %d iobase 0x%p.\n",
3052             pdev->device, pdev->irq, ha->iobase);
3053         mutex_init(&ha->vport_lock);
3054         mutex_init(&ha->mq_lock);
3055         init_completion(&ha->mbx_cmd_comp);
3056         complete(&ha->mbx_cmd_comp);
3057         init_completion(&ha->mbx_intr_comp);
3058         init_completion(&ha->dcbx_comp);
3059         init_completion(&ha->lb_portup_comp);
3060
3061         set_bit(0, (unsigned long *) ha->vp_idx_map);
3062
3063         qla2x00_config_dma_addressing(ha);
3064         ql_dbg_pci(ql_dbg_init, pdev, 0x0020,
3065             "64 Bit addressing is %s.\n",
3066             ha->flags.enable_64bit_addressing ? "enable" :
3067             "disable");
3068         ret = qla2x00_mem_alloc(ha, req_length, rsp_length, &req, &rsp);
3069         if (ret) {
3070                 ql_log_pci(ql_log_fatal, pdev, 0x0031,
3071                     "Failed to allocate memory for adapter, aborting.\n");
3072
3073                 goto probe_hw_failed;
3074         }
3075
3076         req->max_q_depth = MAX_Q_DEPTH;
3077         if (ql2xmaxqdepth != 0 && ql2xmaxqdepth <= 0xffffU)
3078                 req->max_q_depth = ql2xmaxqdepth;
3079
3080
3081         base_vha = qla2x00_create_host(sht, ha);
3082         if (!base_vha) {
3083                 ret = -ENOMEM;
3084                 goto probe_hw_failed;
3085         }
3086
3087         pci_set_drvdata(pdev, base_vha);
3088         set_bit(PFLG_DRIVER_PROBING, &base_vha->pci_flags);
3089
3090         host = base_vha->host;
3091         base_vha->req = req;
3092         if (IS_QLA2XXX_MIDTYPE(ha))
3093                 base_vha->mgmt_svr_loop_id =
3094                         qla2x00_reserve_mgmt_server_loop_id(base_vha);
3095         else