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