b4b185c7bad642e5d82e77ee65275234001c2f52
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / scsi / qla2xxx / qla_os.c
1 /*
2  * QLogic Fibre Channel HBA Driver
3  * Copyright (c)  2003-2011 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
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 /*
23  * Driver version
24  */
25 char qla2x00_version_str[40];
26
27 static int apidev_major;
28
29 /*
30  * SRB allocation cache
31  */
32 static struct kmem_cache *srb_cachep;
33
34 /*
35  * CT6 CTX allocation cache
36  */
37 static struct kmem_cache *ctx_cachep;
38 /*
39  * error level for logging
40  */
41 int ql_errlev = ql_log_all;
42
43 int ql2xlogintimeout = 20;
44 module_param(ql2xlogintimeout, int, S_IRUGO);
45 MODULE_PARM_DESC(ql2xlogintimeout,
46                 "Login timeout value in seconds.");
47
48 int qlport_down_retry;
49 module_param(qlport_down_retry, int, S_IRUGO);
50 MODULE_PARM_DESC(qlport_down_retry,
51                 "Maximum number of command retries to a port that returns "
52                 "a PORT-DOWN status.");
53
54 int ql2xplogiabsentdevice;
55 module_param(ql2xplogiabsentdevice, int, S_IRUGO|S_IWUSR);
56 MODULE_PARM_DESC(ql2xplogiabsentdevice,
57                 "Option to enable PLOGI to devices that are not present after "
58                 "a Fabric scan.  This is needed for several broken switches. "
59                 "Default is 0 - no PLOGI. 1 - perfom PLOGI.");
60
61 int ql2xloginretrycount = 0;
62 module_param(ql2xloginretrycount, int, S_IRUGO);
63 MODULE_PARM_DESC(ql2xloginretrycount,
64                 "Specify an alternate value for the NVRAM login retry count.");
65
66 int ql2xallocfwdump = 1;
67 module_param(ql2xallocfwdump, int, S_IRUGO);
68 MODULE_PARM_DESC(ql2xallocfwdump,
69                 "Option to enable allocation of memory for a firmware dump "
70                 "during HBA initialization.  Memory allocation requirements "
71                 "vary by ISP type.  Default is 1 - allocate memory.");
72
73 int ql2xextended_error_logging;
74 module_param(ql2xextended_error_logging, int, S_IRUGO|S_IWUSR);
75 MODULE_PARM_DESC(ql2xextended_error_logging,
76                 "Option to enable extended error logging,\n"
77                 "\t\tDefault is 0 - no logging.  0x40000000 - Module Init & Probe.\n"
78                 "\t\t0x20000000 - Mailbox Cmnds. 0x10000000 - Device Discovery.\n"
79                 "\t\t0x08000000 - IO tracing.    0x04000000 - DPC Thread.\n"
80                 "\t\t0x02000000 - Async events.  0x01000000 - Timer routines.\n"
81                 "\t\t0x00800000 - User space.    0x00400000 - Task Management.\n"
82                 "\t\t0x00200000 - AER/EEH.       0x00100000 - Multi Q.\n"
83                 "\t\t0x00080000 - P3P Specific.  0x00040000 - Virtual Port.\n"
84                 "\t\t0x00020000 - Buffer Dump.   0x00010000 - Misc.\n"
85                 "\t\t0x7fffffff - For enabling all logs, can be too many logs.\n"
86                 "\t\t0x1e400000 - Preferred value for capturing essential "
87                 "debug information (equivalent to old "
88                 "ql2xextended_error_logging=1).\n"
89                 "\t\tDo LOGICAL OR of the value to enable more than one level");
90
91 int ql2xshiftctondsd = 6;
92 module_param(ql2xshiftctondsd, int, S_IRUGO);
93 MODULE_PARM_DESC(ql2xshiftctondsd,
94                 "Set to control shifting of command type processing "
95                 "based on total number of SG elements.");
96
97 static void qla2x00_free_device(scsi_qla_host_t *);
98
99 int ql2xfdmienable=1;
100 module_param(ql2xfdmienable, int, S_IRUGO);
101 MODULE_PARM_DESC(ql2xfdmienable,
102                 "Enables FDMI registrations. "
103                 "0 - no FDMI. Default is 1 - perform FDMI.");
104
105 #define MAX_Q_DEPTH    32
106 static int ql2xmaxqdepth = MAX_Q_DEPTH;
107 module_param(ql2xmaxqdepth, int, S_IRUGO|S_IWUSR);
108 MODULE_PARM_DESC(ql2xmaxqdepth,
109                 "Maximum queue depth to report for target devices.");
110
111 /* Do not change the value of this after module load */
112 int ql2xenabledif = 0;
113 module_param(ql2xenabledif, int, S_IRUGO|S_IWUSR);
114 MODULE_PARM_DESC(ql2xenabledif,
115                 " Enable T10-CRC-DIF "
116                 " Default is 0 - No DIF Support. 1 - Enable it"
117                 ", 2 - Enable DIF for all types, except Type 0.");
118
119 int ql2xenablehba_err_chk = 2;
120 module_param(ql2xenablehba_err_chk, int, S_IRUGO|S_IWUSR);
121 MODULE_PARM_DESC(ql2xenablehba_err_chk,
122                 " Enable T10-CRC-DIF Error isolation by HBA:\n"
123                 " Default is 1.\n"
124                 "  0 -- Error isolation disabled\n"
125                 "  1 -- Error isolation enabled only for DIX Type 0\n"
126                 "  2 -- Error isolation enabled for all Types\n");
127
128 int ql2xiidmaenable=1;
129 module_param(ql2xiidmaenable, int, S_IRUGO);
130 MODULE_PARM_DESC(ql2xiidmaenable,
131                 "Enables iIDMA settings "
132                 "Default is 1 - perform iIDMA. 0 - no iIDMA.");
133
134 int ql2xmaxqueues = 1;
135 module_param(ql2xmaxqueues, int, S_IRUGO);
136 MODULE_PARM_DESC(ql2xmaxqueues,
137                 "Enables MQ settings "
138                 "Default is 1 for single queue. Set it to number "
139                 "of queues in MQ mode.");
140
141 int ql2xmultique_tag;
142 module_param(ql2xmultique_tag, int, S_IRUGO);
143 MODULE_PARM_DESC(ql2xmultique_tag,
144                 "Enables CPU affinity settings for the driver "
145                 "Default is 0 for no affinity of request and response IO. "
146                 "Set it to 1 to turn on the cpu affinity.");
147
148 int ql2xfwloadbin;
149 module_param(ql2xfwloadbin, int, S_IRUGO|S_IWUSR);
150 MODULE_PARM_DESC(ql2xfwloadbin,
151                 "Option to specify location from which to load ISP firmware:.\n"
152                 " 2 -- load firmware via the request_firmware() (hotplug).\n"
153                 "      interface.\n"
154                 " 1 -- load firmware from flash.\n"
155                 " 0 -- use default semantics.\n");
156
157 int ql2xetsenable;
158 module_param(ql2xetsenable, int, S_IRUGO);
159 MODULE_PARM_DESC(ql2xetsenable,
160                 "Enables firmware ETS burst."
161                 "Default is 0 - skip ETS enablement.");
162
163 int ql2xdbwr = 1;
164 module_param(ql2xdbwr, int, S_IRUGO|S_IWUSR);
165 MODULE_PARM_DESC(ql2xdbwr,
166                 "Option to specify scheme for request queue posting.\n"
167                 " 0 -- Regular doorbell.\n"
168                 " 1 -- CAMRAM doorbell (faster).\n");
169
170 int ql2xtargetreset = 1;
171 module_param(ql2xtargetreset, int, S_IRUGO);
172 MODULE_PARM_DESC(ql2xtargetreset,
173                  "Enable target reset."
174                  "Default is 1 - use hw defaults.");
175
176 int ql2xgffidenable;
177 module_param(ql2xgffidenable, int, S_IRUGO);
178 MODULE_PARM_DESC(ql2xgffidenable,
179                 "Enables GFF_ID checks of port type. "
180                 "Default is 0 - Do not use GFF_ID information.");
181
182 int ql2xasynctmfenable;
183 module_param(ql2xasynctmfenable, int, S_IRUGO);
184 MODULE_PARM_DESC(ql2xasynctmfenable,
185                 "Enables issue of TM IOCBs asynchronously via IOCB mechanism"
186                 "Default is 0 - Issue TM IOCBs via mailbox mechanism.");
187
188 int ql2xdontresethba;
189 module_param(ql2xdontresethba, int, S_IRUGO|S_IWUSR);
190 MODULE_PARM_DESC(ql2xdontresethba,
191                 "Option to specify reset behaviour.\n"
192                 " 0 (Default) -- Reset on failure.\n"
193                 " 1 -- Do not reset on failure.\n");
194
195 uint ql2xmaxlun = MAX_LUNS;
196 module_param(ql2xmaxlun, uint, S_IRUGO);
197 MODULE_PARM_DESC(ql2xmaxlun,
198                 "Defines the maximum LU number to register with the SCSI "
199                 "midlayer. Default is 65535.");
200
201 int ql2xmdcapmask = 0x1F;
202 module_param(ql2xmdcapmask, int, S_IRUGO);
203 MODULE_PARM_DESC(ql2xmdcapmask,
204                 "Set the Minidump driver capture mask level. "
205                 "Default is 0x1F - Can be set to 0x3, 0x7, 0xF, 0x1F, 0x7F.");
206
207 int ql2xmdenable = 1;
208 module_param(ql2xmdenable, int, S_IRUGO);
209 MODULE_PARM_DESC(ql2xmdenable,
210                 "Enable/disable MiniDump. "
211                 "0 - MiniDump disabled. "
212                 "1 (Default) - MiniDump enabled.");
213
214 /*
215  * SCSI host template entry points
216  */
217 static int qla2xxx_slave_configure(struct scsi_device * device);
218 static int qla2xxx_slave_alloc(struct scsi_device *);
219 static int qla2xxx_scan_finished(struct Scsi_Host *, unsigned long time);
220 static void qla2xxx_scan_start(struct Scsi_Host *);
221 static void qla2xxx_slave_destroy(struct scsi_device *);
222 static int qla2xxx_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmd);
223 static int qla2xxx_eh_abort(struct scsi_cmnd *);
224 static int qla2xxx_eh_device_reset(struct scsi_cmnd *);
225 static int qla2xxx_eh_target_reset(struct scsi_cmnd *);
226 static int qla2xxx_eh_bus_reset(struct scsi_cmnd *);
227 static int qla2xxx_eh_host_reset(struct scsi_cmnd *);
228
229 static int qla2x00_change_queue_depth(struct scsi_device *, int, int);
230 static int qla2x00_change_queue_type(struct scsi_device *, int);
231
232 struct scsi_host_template qla2xxx_driver_template = {
233         .module                 = THIS_MODULE,
234         .name                   = QLA2XXX_DRIVER_NAME,
235         .queuecommand           = qla2xxx_queuecommand,
236
237         .eh_abort_handler       = qla2xxx_eh_abort,
238         .eh_device_reset_handler = qla2xxx_eh_device_reset,
239         .eh_target_reset_handler = qla2xxx_eh_target_reset,
240         .eh_bus_reset_handler   = qla2xxx_eh_bus_reset,
241         .eh_host_reset_handler  = qla2xxx_eh_host_reset,
242
243         .slave_configure        = qla2xxx_slave_configure,
244
245         .slave_alloc            = qla2xxx_slave_alloc,
246         .slave_destroy          = qla2xxx_slave_destroy,
247         .scan_finished          = qla2xxx_scan_finished,
248         .scan_start             = qla2xxx_scan_start,
249         .change_queue_depth     = qla2x00_change_queue_depth,
250         .change_queue_type      = qla2x00_change_queue_type,
251         .this_id                = -1,
252         .cmd_per_lun            = 3,
253         .use_clustering         = ENABLE_CLUSTERING,
254         .sg_tablesize           = SG_ALL,
255
256         .max_sectors            = 0xFFFF,
257         .shost_attrs            = qla2x00_host_attrs,
258 };
259
260 static struct scsi_transport_template *qla2xxx_transport_template = NULL;
261 struct scsi_transport_template *qla2xxx_transport_vport_template = NULL;
262
263 /* TODO Convert to inlines
264  *
265  * Timer routines
266  */
267
268 __inline__ void
269 qla2x00_start_timer(scsi_qla_host_t *vha, void *func, unsigned long interval)
270 {
271         init_timer(&vha->timer);
272         vha->timer.expires = jiffies + interval * HZ;
273         vha->timer.data = (unsigned long)vha;
274         vha->timer.function = (void (*)(unsigned long))func;
275         add_timer(&vha->timer);
276         vha->timer_active = 1;
277 }
278
279 static inline void
280 qla2x00_restart_timer(scsi_qla_host_t *vha, unsigned long interval)
281 {
282         /* Currently used for 82XX only. */
283         if (vha->device_flags & DFLG_DEV_FAILED) {
284                 ql_dbg(ql_dbg_timer, vha, 0x600d,
285                     "Device in a failed state, returning.\n");
286                 return;
287         }
288
289         mod_timer(&vha->timer, jiffies + interval * HZ);
290 }
291
292 static __inline__ void
293 qla2x00_stop_timer(scsi_qla_host_t *vha)
294 {
295         del_timer_sync(&vha->timer);
296         vha->timer_active = 0;
297 }
298
299 static int qla2x00_do_dpc(void *data);
300
301 static void qla2x00_rst_aen(scsi_qla_host_t *);
302
303 static int qla2x00_mem_alloc(struct qla_hw_data *, uint16_t, uint16_t,
304         struct req_que **, struct rsp_que **);
305 static void qla2x00_free_fw_dump(struct qla_hw_data *);
306 static void qla2x00_mem_free(struct qla_hw_data *);
307 static void qla2x00_sp_free_dma(srb_t *);
308
309 /* -------------------------------------------------------------------------- */
310 static int qla2x00_alloc_queues(struct qla_hw_data *ha)
311 {
312         scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
313         ha->req_q_map = kzalloc(sizeof(struct req_que *) * ha->max_req_queues,
314                                 GFP_KERNEL);
315         if (!ha->req_q_map) {
316                 ql_log(ql_log_fatal, vha, 0x003b,
317                     "Unable to allocate memory for request queue ptrs.\n");
318                 goto fail_req_map;
319         }
320
321         ha->rsp_q_map = kzalloc(sizeof(struct rsp_que *) * ha->max_rsp_queues,
322                                 GFP_KERNEL);
323         if (!ha->rsp_q_map) {
324                 ql_log(ql_log_fatal, vha, 0x003c,
325                     "Unable to allocate memory for response queue ptrs.\n");
326                 goto fail_rsp_map;
327         }
328         set_bit(0, ha->rsp_qid_map);
329         set_bit(0, ha->req_qid_map);
330         return 1;
331
332 fail_rsp_map:
333         kfree(ha->req_q_map);
334         ha->req_q_map = NULL;
335 fail_req_map:
336         return -ENOMEM;
337 }
338
339 static void qla2x00_free_req_que(struct qla_hw_data *ha, struct req_que *req)
340 {
341         if (req && req->ring)
342                 dma_free_coherent(&ha->pdev->dev,
343                 (req->length + 1) * sizeof(request_t),
344                 req->ring, req->dma);
345
346         kfree(req);
347         req = NULL;
348 }
349
350 static void qla2x00_free_rsp_que(struct qla_hw_data *ha, struct rsp_que *rsp)
351 {
352         if (rsp && rsp->ring)
353                 dma_free_coherent(&ha->pdev->dev,
354                 (rsp->length + 1) * sizeof(response_t),
355                 rsp->ring, rsp->dma);
356
357         kfree(rsp);
358         rsp = NULL;
359 }
360
361 static void qla2x00_free_queues(struct qla_hw_data *ha)
362 {
363         struct req_que *req;
364         struct rsp_que *rsp;
365         int cnt;
366
367         for (cnt = 0; cnt < ha->max_req_queues; cnt++) {
368                 req = ha->req_q_map[cnt];
369                 qla2x00_free_req_que(ha, req);
370         }
371         kfree(ha->req_q_map);
372         ha->req_q_map = NULL;
373
374         for (cnt = 0; cnt < ha->max_rsp_queues; cnt++) {
375                 rsp = ha->rsp_q_map[cnt];
376                 qla2x00_free_rsp_que(ha, rsp);
377         }
378         kfree(ha->rsp_q_map);
379         ha->rsp_q_map = NULL;
380 }
381
382 static int qla25xx_setup_mode(struct scsi_qla_host *vha)
383 {
384         uint16_t options = 0;
385         int ques, req, ret;
386         struct qla_hw_data *ha = vha->hw;
387
388         if (!(ha->fw_attributes & BIT_6)) {
389                 ql_log(ql_log_warn, vha, 0x00d8,
390                     "Firmware is not multi-queue capable.\n");
391                 goto fail;
392         }
393         if (ql2xmultique_tag) {
394                 /* create a request queue for IO */
395                 options |= BIT_7;
396                 req = qla25xx_create_req_que(ha, options, 0, 0, -1,
397                         QLA_DEFAULT_QUE_QOS);
398                 if (!req) {
399                         ql_log(ql_log_warn, vha, 0x00e0,
400                             "Failed to create request queue.\n");
401                         goto fail;
402                 }
403                 ha->wq = alloc_workqueue("qla2xxx_wq", WQ_MEM_RECLAIM, 1);
404                 vha->req = ha->req_q_map[req];
405                 options |= BIT_1;
406                 for (ques = 1; ques < ha->max_rsp_queues; ques++) {
407                         ret = qla25xx_create_rsp_que(ha, options, 0, 0, req);
408                         if (!ret) {
409                                 ql_log(ql_log_warn, vha, 0x00e8,
410                                     "Failed to create response queue.\n");
411                                 goto fail2;
412                         }
413                 }
414                 ha->flags.cpu_affinity_enabled = 1;
415                 ql_dbg(ql_dbg_multiq, vha, 0xc007,
416                     "CPU affinity mode enalbed, "
417                     "no. of response queues:%d no. of request queues:%d.\n",
418                     ha->max_rsp_queues, ha->max_req_queues);
419                 ql_dbg(ql_dbg_init, vha, 0x00e9,
420                     "CPU affinity mode enalbed, "
421                     "no. of response queues:%d no. of request queues:%d.\n",
422                     ha->max_rsp_queues, ha->max_req_queues);
423         }
424         return 0;
425 fail2:
426         qla25xx_delete_queues(vha);
427         destroy_workqueue(ha->wq);
428         ha->wq = NULL;
429         vha->req = ha->req_q_map[0];
430 fail:
431         ha->mqenable = 0;
432         kfree(ha->req_q_map);
433         kfree(ha->rsp_q_map);
434         ha->max_req_queues = ha->max_rsp_queues = 1;
435         return 1;
436 }
437
438 static char *
439 qla2x00_pci_info_str(struct scsi_qla_host *vha, char *str)
440 {
441         struct qla_hw_data *ha = vha->hw;
442         static char *pci_bus_modes[] = {
443                 "33", "66", "100", "133",
444         };
445         uint16_t pci_bus;
446
447         strcpy(str, "PCI");
448         pci_bus = (ha->pci_attr & (BIT_9 | BIT_10)) >> 9;
449         if (pci_bus) {
450                 strcat(str, "-X (");
451                 strcat(str, pci_bus_modes[pci_bus]);
452         } else {
453                 pci_bus = (ha->pci_attr & BIT_8) >> 8;
454                 strcat(str, " (");
455                 strcat(str, pci_bus_modes[pci_bus]);
456         }
457         strcat(str, " MHz)");
458
459         return (str);
460 }
461
462 static char *
463 qla24xx_pci_info_str(struct scsi_qla_host *vha, char *str)
464 {
465         static char *pci_bus_modes[] = { "33", "66", "100", "133", };
466         struct qla_hw_data *ha = vha->hw;
467         uint32_t pci_bus;
468         int pcie_reg;
469
470         pcie_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_EXP);
471         if (pcie_reg) {
472                 char lwstr[6];
473                 uint16_t pcie_lstat, lspeed, lwidth;
474
475                 pcie_reg += 0x12;
476                 pci_read_config_word(ha->pdev, pcie_reg, &pcie_lstat);
477                 lspeed = pcie_lstat & (BIT_0 | BIT_1 | BIT_2 | BIT_3);
478                 lwidth = (pcie_lstat &
479                     (BIT_4 | BIT_5 | BIT_6 | BIT_7 | BIT_8 | BIT_9)) >> 4;
480
481                 strcpy(str, "PCIe (");
482                 if (lspeed == 1)
483                         strcat(str, "2.5GT/s ");
484                 else if (lspeed == 2)
485                         strcat(str, "5.0GT/s ");
486                 else
487                         strcat(str, "<unknown> ");
488                 snprintf(lwstr, sizeof(lwstr), "x%d)", lwidth);
489                 strcat(str, lwstr);
490
491                 return str;
492         }
493
494         strcpy(str, "PCI");
495         pci_bus = (ha->pci_attr & CSRX_PCIX_BUS_MODE_MASK) >> 8;
496         if (pci_bus == 0 || pci_bus == 8) {
497                 strcat(str, " (");
498                 strcat(str, pci_bus_modes[pci_bus >> 3]);
499         } else {
500                 strcat(str, "-X ");
501                 if (pci_bus & BIT_2)
502                         strcat(str, "Mode 2");
503                 else
504                         strcat(str, "Mode 1");
505                 strcat(str, " (");
506                 strcat(str, pci_bus_modes[pci_bus & ~BIT_2]);
507         }
508         strcat(str, " MHz)");
509
510         return str;
511 }
512
513 static char *
514 qla2x00_fw_version_str(struct scsi_qla_host *vha, char *str)
515 {
516         char un_str[10];
517         struct qla_hw_data *ha = vha->hw;
518
519         sprintf(str, "%d.%02d.%02d ", ha->fw_major_version,
520             ha->fw_minor_version,
521             ha->fw_subminor_version);
522
523         if (ha->fw_attributes & BIT_9) {
524                 strcat(str, "FLX");
525                 return (str);
526         }
527
528         switch (ha->fw_attributes & 0xFF) {
529         case 0x7:
530                 strcat(str, "EF");
531                 break;
532         case 0x17:
533                 strcat(str, "TP");
534                 break;
535         case 0x37:
536                 strcat(str, "IP");
537                 break;
538         case 0x77:
539                 strcat(str, "VI");
540                 break;
541         default:
542                 sprintf(un_str, "(%x)", ha->fw_attributes);
543                 strcat(str, un_str);
544                 break;
545         }
546         if (ha->fw_attributes & 0x100)
547                 strcat(str, "X");
548
549         return (str);
550 }
551
552 static char *
553 qla24xx_fw_version_str(struct scsi_qla_host *vha, char *str)
554 {
555         struct qla_hw_data *ha = vha->hw;
556
557         sprintf(str, "%d.%02d.%02d (%x)", ha->fw_major_version,
558             ha->fw_minor_version, ha->fw_subminor_version, ha->fw_attributes);
559         return str;
560 }
561
562 static inline srb_t *
563 qla2x00_get_new_sp(scsi_qla_host_t *vha, fc_port_t *fcport,
564         struct scsi_cmnd *cmd)
565 {
566         srb_t *sp;
567         struct qla_hw_data *ha = vha->hw;
568
569         sp = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
570         if (!sp) {
571                 ql_log(ql_log_warn, vha, 0x3006,
572                     "Memory allocation failed for sp.\n");
573                 return sp;
574         }
575
576         atomic_set(&sp->ref_count, 1);
577         sp->fcport = fcport;
578         sp->cmd = cmd;
579         sp->flags = 0;
580         CMD_SP(cmd) = (void *)sp;
581         sp->ctx = NULL;
582
583         return sp;
584 }
585
586 static int
587 qla2xxx_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
588 {
589         scsi_qla_host_t *vha = shost_priv(host);
590         fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
591         struct fc_rport *rport = starget_to_rport(scsi_target(cmd->device));
592         struct qla_hw_data *ha = vha->hw;
593         struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
594         srb_t *sp;
595         int rval;
596
597         if (ha->flags.eeh_busy) {
598                 if (ha->flags.pci_channel_io_perm_failure) {
599                         ql_dbg(ql_dbg_io, vha, 0x3001,
600                             "PCI Channel IO permanent failure, exiting "
601                             "cmd=%p.\n", cmd);
602                         cmd->result = DID_NO_CONNECT << 16;
603                 } else {
604                         ql_dbg(ql_dbg_io, vha, 0x3002,
605                             "EEH_Busy, Requeuing the cmd=%p.\n", cmd);
606                         cmd->result = DID_REQUEUE << 16;
607                 }
608                 goto qc24_fail_command;
609         }
610
611         rval = fc_remote_port_chkready(rport);
612         if (rval) {
613                 cmd->result = rval;
614                 ql_dbg(ql_dbg_io, vha, 0x3003,
615                     "fc_remote_port_chkready failed for cmd=%p, rval=0x%x.\n",
616                     cmd, rval);
617                 goto qc24_fail_command;
618         }
619
620         if (!vha->flags.difdix_supported &&
621                 scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
622                         ql_dbg(ql_dbg_io, vha, 0x3004,
623                             "DIF Cap not reg, fail DIF capable cmd's:%p.\n",
624                             cmd);
625                         cmd->result = DID_NO_CONNECT << 16;
626                         goto qc24_fail_command;
627         }
628         if (atomic_read(&fcport->state) != FCS_ONLINE) {
629                 if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD ||
630                         atomic_read(&base_vha->loop_state) == LOOP_DEAD) {
631                         ql_dbg(ql_dbg_io, vha, 0x3005,
632                             "Returning DNC, fcport_state=%d loop_state=%d.\n",
633                             atomic_read(&fcport->state),
634                             atomic_read(&base_vha->loop_state));
635                         cmd->result = DID_NO_CONNECT << 16;
636                         goto qc24_fail_command;
637                 }
638                 goto qc24_target_busy;
639         }
640
641         sp = qla2x00_get_new_sp(base_vha, fcport, cmd);
642         if (!sp)
643                 goto qc24_host_busy;
644
645         rval = ha->isp_ops->start_scsi(sp);
646         if (rval != QLA_SUCCESS) {
647                 ql_dbg(ql_dbg_io, vha, 0x3013,
648                     "Start scsi failed rval=%d for cmd=%p.\n", rval, cmd);
649                 goto qc24_host_busy_free_sp;
650         }
651
652         return 0;
653
654 qc24_host_busy_free_sp:
655         qla2x00_sp_free_dma(sp);
656         mempool_free(sp, ha->srb_mempool);
657
658 qc24_host_busy:
659         return SCSI_MLQUEUE_HOST_BUSY;
660
661 qc24_target_busy:
662         return SCSI_MLQUEUE_TARGET_BUSY;
663
664 qc24_fail_command:
665         cmd->scsi_done(cmd);
666
667         return 0;
668 }
669
670 /*
671  * qla2x00_eh_wait_on_command
672  *    Waits for the command to be returned by the Firmware for some
673  *    max time.
674  *
675  * Input:
676  *    cmd = Scsi Command to wait on.
677  *
678  * Return:
679  *    Not Found : 0
680  *    Found : 1
681  */
682 static int
683 qla2x00_eh_wait_on_command(struct scsi_cmnd *cmd)
684 {
685 #define ABORT_POLLING_PERIOD    1000
686 #define ABORT_WAIT_ITER         ((10 * 1000) / (ABORT_POLLING_PERIOD))
687         unsigned long wait_iter = ABORT_WAIT_ITER;
688         scsi_qla_host_t *vha = shost_priv(cmd->device->host);
689         struct qla_hw_data *ha = vha->hw;
690         int ret = QLA_SUCCESS;
691
692         if (unlikely(pci_channel_offline(ha->pdev)) || ha->flags.eeh_busy) {
693                 ql_dbg(ql_dbg_taskm, vha, 0x8005,
694                     "Return:eh_wait.\n");
695                 return ret;
696         }
697
698         while (CMD_SP(cmd) && wait_iter--) {
699                 msleep(ABORT_POLLING_PERIOD);
700         }
701         if (CMD_SP(cmd))
702                 ret = QLA_FUNCTION_FAILED;
703
704         return ret;
705 }
706
707 /*
708  * qla2x00_wait_for_hba_online
709  *    Wait till the HBA is online after going through
710  *    <= MAX_RETRIES_OF_ISP_ABORT  or
711  *    finally HBA is disabled ie marked offline
712  *
713  * Input:
714  *     ha - pointer to host adapter structure
715  *
716  * Note:
717  *    Does context switching-Release SPIN_LOCK
718  *    (if any) before calling this routine.
719  *
720  * Return:
721  *    Success (Adapter is online) : 0
722  *    Failed  (Adapter is offline/disabled) : 1
723  */
724 int
725 qla2x00_wait_for_hba_online(scsi_qla_host_t *vha)
726 {
727         int             return_status;
728         unsigned long   wait_online;
729         struct qla_hw_data *ha = vha->hw;
730         scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
731
732         wait_online = jiffies + (MAX_LOOP_TIMEOUT * HZ);
733         while (((test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags)) ||
734             test_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags) ||
735             test_bit(ISP_ABORT_RETRY, &base_vha->dpc_flags) ||
736             ha->dpc_active) && time_before(jiffies, wait_online)) {
737
738                 msleep(1000);
739         }
740         if (base_vha->flags.online)
741                 return_status = QLA_SUCCESS;
742         else
743                 return_status = QLA_FUNCTION_FAILED;
744
745         return (return_status);
746 }
747
748 /*
749  * qla2x00_wait_for_reset_ready
750  *    Wait till the HBA is online after going through
751  *    <= MAX_RETRIES_OF_ISP_ABORT  or
752  *    finally HBA is disabled ie marked offline or flash
753  *    operations are in progress.
754  *
755  * Input:
756  *     ha - pointer to host adapter structure
757  *
758  * Note:
759  *    Does context switching-Release SPIN_LOCK
760  *    (if any) before calling this routine.
761  *
762  * Return:
763  *    Success (Adapter is online/no flash ops) : 0
764  *    Failed  (Adapter is offline/disabled/flash ops in progress) : 1
765  */
766 static int
767 qla2x00_wait_for_reset_ready(scsi_qla_host_t *vha)
768 {
769         int             return_status;
770         unsigned long   wait_online;
771         struct qla_hw_data *ha = vha->hw;
772         scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
773
774         wait_online = jiffies + (MAX_LOOP_TIMEOUT * HZ);
775         while (((test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags)) ||
776             test_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags) ||
777             test_bit(ISP_ABORT_RETRY, &base_vha->dpc_flags) ||
778             ha->optrom_state != QLA_SWAITING ||
779             ha->dpc_active) && time_before(jiffies, wait_online))
780                 msleep(1000);
781
782         if (base_vha->flags.online &&  ha->optrom_state == QLA_SWAITING)
783                 return_status = QLA_SUCCESS;
784         else
785                 return_status = QLA_FUNCTION_FAILED;
786
787         ql_dbg(ql_dbg_taskm, vha, 0x8019,
788             "%s return status=%d.\n", __func__, return_status);
789
790         return return_status;
791 }
792
793 int
794 qla2x00_wait_for_chip_reset(scsi_qla_host_t *vha)
795 {
796         int             return_status;
797         unsigned long   wait_reset;
798         struct qla_hw_data *ha = vha->hw;
799         scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
800
801         wait_reset = jiffies + (MAX_LOOP_TIMEOUT * HZ);
802         while (((test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags)) ||
803             test_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags) ||
804             test_bit(ISP_ABORT_RETRY, &base_vha->dpc_flags) ||
805             ha->dpc_active) && time_before(jiffies, wait_reset)) {
806
807                 msleep(1000);
808
809                 if (!test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags) &&
810                     ha->flags.chip_reset_done)
811                         break;
812         }
813         if (ha->flags.chip_reset_done)
814                 return_status = QLA_SUCCESS;
815         else
816                 return_status = QLA_FUNCTION_FAILED;
817
818         return return_status;
819 }
820
821 static void
822 sp_get(struct srb *sp)
823 {
824         atomic_inc(&sp->ref_count);
825 }
826
827 /**************************************************************************
828 * qla2xxx_eh_abort
829 *
830 * Description:
831 *    The abort function will abort the specified command.
832 *
833 * Input:
834 *    cmd = Linux SCSI command packet to be aborted.
835 *
836 * Returns:
837 *    Either SUCCESS or FAILED.
838 *
839 * Note:
840 *    Only return FAILED if command not returned by firmware.
841 **************************************************************************/
842 static int
843 qla2xxx_eh_abort(struct scsi_cmnd *cmd)
844 {
845         scsi_qla_host_t *vha = shost_priv(cmd->device->host);
846         srb_t *sp;
847         int ret;
848         unsigned int id, lun;
849         unsigned long flags;
850         int wait = 0;
851         struct qla_hw_data *ha = vha->hw;
852
853         if (!CMD_SP(cmd))
854                 return SUCCESS;
855
856         ret = fc_block_scsi_eh(cmd);
857         if (ret != 0)
858                 return ret;
859         ret = SUCCESS;
860
861         id = cmd->device->id;
862         lun = cmd->device->lun;
863
864         spin_lock_irqsave(&ha->hardware_lock, flags);
865         sp = (srb_t *) CMD_SP(cmd);
866         if (!sp) {
867                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
868                 return SUCCESS;
869         }
870
871         ql_dbg(ql_dbg_taskm, vha, 0x8002,
872             "Aborting from RISC nexus=%ld:%d:%d sp=%p cmd=%p\n",
873             vha->host_no, id, lun, sp, cmd);
874
875         /* Get a reference to the sp and drop the lock.*/
876         sp_get(sp);
877
878         spin_unlock_irqrestore(&ha->hardware_lock, flags);
879         if (ha->isp_ops->abort_command(sp)) {
880                 ret = FAILED;
881                 ql_dbg(ql_dbg_taskm, vha, 0x8003,
882                     "Abort command mbx failed cmd=%p.\n", cmd);
883         } else {
884                 ql_dbg(ql_dbg_taskm, vha, 0x8004,
885                     "Abort command mbx success cmd=%p.\n", cmd);
886                 wait = 1;
887         }
888
889         spin_lock_irqsave(&ha->hardware_lock, flags);
890         qla2x00_sp_compl(ha, sp);
891         spin_unlock_irqrestore(&ha->hardware_lock, flags);
892
893         /* Did the command return during mailbox execution? */
894         if (ret == FAILED && !CMD_SP(cmd))
895                 ret = SUCCESS;
896
897         /* Wait for the command to be returned. */
898         if (wait) {
899                 if (qla2x00_eh_wait_on_command(cmd) != QLA_SUCCESS) {
900                         ql_log(ql_log_warn, vha, 0x8006,
901                             "Abort handler timed out cmd=%p.\n", cmd);
902                         ret = FAILED;
903                 }
904         }
905
906         ql_log(ql_log_info, vha, 0x801c,
907             "Abort command issued nexus=%ld:%d:%d --  %d %x.\n",
908             vha->host_no, id, lun, wait, ret);
909
910         return ret;
911 }
912
913 int
914 qla2x00_eh_wait_for_pending_commands(scsi_qla_host_t *vha, unsigned int t,
915         unsigned int l, enum nexus_wait_type type)
916 {
917         int cnt, match, status;
918         unsigned long flags;
919         struct qla_hw_data *ha = vha->hw;
920         struct req_que *req;
921         srb_t *sp;
922
923         status = QLA_SUCCESS;
924
925         spin_lock_irqsave(&ha->hardware_lock, flags);
926         req = vha->req;
927         for (cnt = 1; status == QLA_SUCCESS &&
928                 cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
929                 sp = req->outstanding_cmds[cnt];
930                 if (!sp)
931                         continue;
932                 if ((sp->ctx) && !IS_PROT_IO(sp))
933                         continue;
934                 if (vha->vp_idx != sp->fcport->vha->vp_idx)
935                         continue;
936                 match = 0;
937                 switch (type) {
938                 case WAIT_HOST:
939                         match = 1;
940                         break;
941                 case WAIT_TARGET:
942                         match = sp->cmd->device->id == t;
943                         break;
944                 case WAIT_LUN:
945                         match = (sp->cmd->device->id == t &&
946                                 sp->cmd->device->lun == l);
947                         break;
948                 }
949                 if (!match)
950                         continue;
951
952                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
953                 status = qla2x00_eh_wait_on_command(sp->cmd);
954                 spin_lock_irqsave(&ha->hardware_lock, flags);
955         }
956         spin_unlock_irqrestore(&ha->hardware_lock, flags);
957
958         return status;
959 }
960
961 static char *reset_errors[] = {
962         "HBA not online",
963         "HBA not ready",
964         "Task management failed",
965         "Waiting for command completions",
966 };
967
968 static int
969 __qla2xxx_eh_generic_reset(char *name, enum nexus_wait_type type,
970     struct scsi_cmnd *cmd, int (*do_reset)(struct fc_port *, unsigned int, int))
971 {
972         scsi_qla_host_t *vha = shost_priv(cmd->device->host);
973         fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
974         int err;
975
976         if (!fcport) {
977                 return FAILED;
978         }
979
980         err = fc_block_scsi_eh(cmd);
981         if (err != 0)
982                 return err;
983
984         ql_log(ql_log_info, vha, 0x8009,
985             "%s RESET ISSUED nexus=%ld:%d:%d cmd=%p.\n", name, vha->host_no,
986             cmd->device->id, cmd->device->lun, cmd);
987
988         err = 0;
989         if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
990                 ql_log(ql_log_warn, vha, 0x800a,
991                     "Wait for hba online failed for cmd=%p.\n", cmd);
992                 goto eh_reset_failed;
993         }
994         err = 2;
995         if (do_reset(fcport, cmd->device->lun, cmd->request->cpu + 1)
996                 != QLA_SUCCESS) {
997                 ql_log(ql_log_warn, vha, 0x800c,
998                     "do_reset failed for cmd=%p.\n", cmd);
999                 goto eh_reset_failed;
1000         }
1001         err = 3;
1002         if (qla2x00_eh_wait_for_pending_commands(vha, cmd->device->id,
1003             cmd->device->lun, type) != QLA_SUCCESS) {
1004                 ql_log(ql_log_warn, vha, 0x800d,
1005                     "wait for peding cmds failed for cmd=%p.\n", cmd);
1006                 goto eh_reset_failed;
1007         }
1008
1009         ql_log(ql_log_info, vha, 0x800e,
1010             "%s RESET SUCCEEDED nexus:%ld:%d:%d cmd=%p.\n", name,
1011             vha->host_no, cmd->device->id, cmd->device->lun, cmd);
1012
1013         return SUCCESS;
1014
1015 eh_reset_failed:
1016         ql_log(ql_log_info, vha, 0x800f,
1017             "%s RESET FAILED: %s nexus=%ld:%d:%d cmd=%p.\n", name,
1018             reset_errors[err], vha->host_no, cmd->device->id, cmd->device->lun,
1019             cmd);
1020         return FAILED;
1021 }
1022
1023 static int
1024 qla2xxx_eh_device_reset(struct scsi_cmnd *cmd)
1025 {
1026         scsi_qla_host_t *vha = shost_priv(cmd->device->host);
1027         struct qla_hw_data *ha = vha->hw;
1028
1029         return __qla2xxx_eh_generic_reset("DEVICE", WAIT_LUN, cmd,
1030             ha->isp_ops->lun_reset);
1031 }
1032
1033 static int
1034 qla2xxx_eh_target_reset(struct scsi_cmnd *cmd)
1035 {
1036         scsi_qla_host_t *vha = shost_priv(cmd->device->host);
1037         struct qla_hw_data *ha = vha->hw;
1038
1039         return __qla2xxx_eh_generic_reset("TARGET", WAIT_TARGET, cmd,
1040             ha->isp_ops->target_reset);
1041 }
1042
1043 /**************************************************************************
1044 * qla2xxx_eh_bus_reset
1045 *
1046 * Description:
1047 *    The bus reset function will reset the bus and abort any executing
1048 *    commands.
1049 *
1050 * Input:
1051 *    cmd = Linux SCSI command packet of the command that cause the
1052 *          bus reset.
1053 *
1054 * Returns:
1055 *    SUCCESS/FAILURE (defined as macro in scsi.h).
1056 *
1057 **************************************************************************/
1058 static int
1059 qla2xxx_eh_bus_reset(struct scsi_cmnd *cmd)
1060 {
1061         scsi_qla_host_t *vha = shost_priv(cmd->device->host);
1062         fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
1063         int ret = FAILED;
1064         unsigned int id, lun;
1065
1066         id = cmd->device->id;
1067         lun = cmd->device->lun;
1068
1069         if (!fcport) {
1070                 return ret;
1071         }
1072
1073         ret = fc_block_scsi_eh(cmd);
1074         if (ret != 0)
1075                 return ret;
1076         ret = FAILED;
1077
1078         ql_log(ql_log_info, vha, 0x8012,
1079             "BUS RESET ISSUED nexus=%ld:%d%d.\n", vha->host_no, id, lun);
1080
1081         if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
1082                 ql_log(ql_log_fatal, vha, 0x8013,
1083                     "Wait for hba online failed board disabled.\n");
1084                 goto eh_bus_reset_done;
1085         }
1086
1087         if (qla2x00_loop_reset(vha) == QLA_SUCCESS)
1088                 ret = SUCCESS;
1089
1090         if (ret == FAILED)
1091                 goto eh_bus_reset_done;
1092
1093         /* Flush outstanding commands. */
1094         if (qla2x00_eh_wait_for_pending_commands(vha, 0, 0, WAIT_HOST) !=
1095             QLA_SUCCESS) {
1096                 ql_log(ql_log_warn, vha, 0x8014,
1097                     "Wait for pending commands failed.\n");
1098                 ret = FAILED;
1099         }
1100
1101 eh_bus_reset_done:
1102         ql_log(ql_log_warn, vha, 0x802b,
1103             "BUS RESET %s nexus=%ld:%d:%d.\n",
1104             (ret == FAILED) ? "FAILED" : "SUCCEDED", vha->host_no, id, lun);
1105
1106         return ret;
1107 }
1108
1109 /**************************************************************************
1110 * qla2xxx_eh_host_reset
1111 *
1112 * Description:
1113 *    The reset function will reset the Adapter.
1114 *
1115 * Input:
1116 *      cmd = Linux SCSI command packet of the command that cause the
1117 *            adapter reset.
1118 *
1119 * Returns:
1120 *      Either SUCCESS or FAILED.
1121 *
1122 * Note:
1123 **************************************************************************/
1124 static int
1125 qla2xxx_eh_host_reset(struct scsi_cmnd *cmd)
1126 {
1127         scsi_qla_host_t *vha = shost_priv(cmd->device->host);
1128         fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
1129         struct qla_hw_data *ha = vha->hw;
1130         int ret = FAILED;
1131         unsigned int id, lun;
1132         scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
1133
1134         id = cmd->device->id;
1135         lun = cmd->device->lun;
1136
1137         if (!fcport) {
1138                 return ret;
1139         }
1140
1141         ret = fc_block_scsi_eh(cmd);
1142         if (ret != 0)
1143                 return ret;
1144         ret = FAILED;
1145
1146         ql_log(ql_log_info, vha, 0x8018,
1147             "ADAPTER RESET ISSUED nexus=%ld:%d:%d.\n", vha->host_no, id, lun);
1148
1149         if (qla2x00_wait_for_reset_ready(vha) != QLA_SUCCESS)
1150                 goto eh_host_reset_lock;
1151
1152         if (vha != base_vha) {
1153                 if (qla2x00_vp_abort_isp(vha))
1154                         goto eh_host_reset_lock;
1155         } else {
1156                 if (IS_QLA82XX(vha->hw)) {
1157                         if (!qla82xx_fcoe_ctx_reset(vha)) {
1158                                 /* Ctx reset success */
1159                                 ret = SUCCESS;
1160                                 goto eh_host_reset_lock;
1161                         }
1162                         /* fall thru if ctx reset failed */
1163                 }
1164                 if (ha->wq)
1165                         flush_workqueue(ha->wq);
1166
1167                 set_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
1168                 if (ha->isp_ops->abort_isp(base_vha)) {
1169                         clear_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
1170                         /* failed. schedule dpc to try */
1171                         set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
1172
1173                         if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
1174                                 ql_log(ql_log_warn, vha, 0x802a,
1175                                     "wait for hba online failed.\n");
1176                                 goto eh_host_reset_lock;
1177                         }
1178                 }
1179                 clear_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
1180         }
1181
1182         /* Waiting for command to be returned to OS.*/
1183         if (qla2x00_eh_wait_for_pending_commands(vha, 0, 0, WAIT_HOST) ==
1184                 QLA_SUCCESS)
1185                 ret = SUCCESS;
1186
1187 eh_host_reset_lock:
1188         ql_log(ql_log_info, vha, 0x8017,
1189             "ADAPTER RESET %s nexus=%ld:%d:%d.\n",
1190             (ret == FAILED) ? "FAILED" : "SUCCEEDED", vha->host_no, id, lun);
1191
1192         return ret;
1193 }
1194
1195 /*
1196 * qla2x00_loop_reset
1197 *      Issue loop reset.
1198 *
1199 * Input:
1200 *      ha = adapter block pointer.
1201 *
1202 * Returns:
1203 *      0 = success
1204 */
1205 int
1206 qla2x00_loop_reset(scsi_qla_host_t *vha)
1207 {
1208         int ret;
1209         struct fc_port *fcport;
1210         struct qla_hw_data *ha = vha->hw;
1211
1212         if (ql2xtargetreset == 1 && ha->flags.enable_target_reset) {
1213                 list_for_each_entry(fcport, &vha->vp_fcports, list) {
1214                         if (fcport->port_type != FCT_TARGET)
1215                                 continue;
1216
1217                         ret = ha->isp_ops->target_reset(fcport, 0, 0);
1218                         if (ret != QLA_SUCCESS) {
1219                                 ql_dbg(ql_dbg_taskm, vha, 0x802c,
1220                                     "Bus Reset failed: Target Reset=%d "
1221                                     "d_id=%x.\n", ret, fcport->d_id.b24);
1222                         }
1223                 }
1224         }
1225
1226         if (ha->flags.enable_lip_full_login && !IS_QLA8XXX_TYPE(ha)) {
1227                 ret = qla2x00_full_login_lip(vha);
1228                 if (ret != QLA_SUCCESS) {
1229                         ql_dbg(ql_dbg_taskm, vha, 0x802d,
1230                             "full_login_lip=%d.\n", ret);
1231                 }
1232                 atomic_set(&vha->loop_state, LOOP_DOWN);
1233                 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
1234                 qla2x00_mark_all_devices_lost(vha, 0);
1235         }
1236
1237         if (ha->flags.enable_lip_reset) {
1238                 ret = qla2x00_lip_reset(vha);
1239                 if (ret != QLA_SUCCESS)
1240                         ql_dbg(ql_dbg_taskm, vha, 0x802e,
1241                             "lip_reset failed (%d).\n", ret);
1242         }
1243
1244         /* Issue marker command only when we are going to start the I/O */
1245         vha->marker_needed = 1;
1246
1247         return QLA_SUCCESS;
1248 }
1249
1250 void
1251 qla2x00_abort_all_cmds(scsi_qla_host_t *vha, int res)
1252 {
1253         int que, cnt;
1254         unsigned long flags;
1255         srb_t *sp;
1256         struct srb_ctx *ctx;
1257         struct qla_hw_data *ha = vha->hw;
1258         struct req_que *req;
1259
1260         spin_lock_irqsave(&ha->hardware_lock, flags);
1261         for (que = 0; que < ha->max_req_queues; que++) {
1262                 req = ha->req_q_map[que];
1263                 if (!req)
1264                         continue;
1265                 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
1266                         sp = req->outstanding_cmds[cnt];
1267                         if (sp) {
1268                                 req->outstanding_cmds[cnt] = NULL;
1269                                 if (!sp->ctx ||
1270                                         (sp->flags & SRB_FCP_CMND_DMA_VALID) ||
1271                                         IS_PROT_IO(sp)) {
1272                                         sp->cmd->result = res;
1273                                         qla2x00_sp_compl(ha, sp);
1274                                 } else {
1275                                         ctx = sp->ctx;
1276                                         if (ctx->type == SRB_ELS_CMD_RPT ||
1277                                             ctx->type == SRB_ELS_CMD_HST ||
1278                                             ctx->type == SRB_CT_CMD) {
1279                                                 struct fc_bsg_job *bsg_job =
1280                                                     ctx->u.bsg_job;
1281                                                 if (bsg_job->request->msgcode
1282                                                     == FC_BSG_HST_CT)
1283                                                         kfree(sp->fcport);
1284                                                 bsg_job->req->errors = 0;
1285                                                 bsg_job->reply->result = res;
1286                                                 bsg_job->job_done(bsg_job);
1287                                                 kfree(sp->ctx);
1288                                                 mempool_free(sp,
1289                                                         ha->srb_mempool);
1290                                         } else {
1291                                                 ctx->u.iocb_cmd->free(sp);
1292                                         }
1293                                 }
1294                         }
1295                 }
1296         }
1297         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1298 }
1299
1300 static int
1301 qla2xxx_slave_alloc(struct scsi_device *sdev)
1302 {
1303         struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1304
1305         if (!rport || fc_remote_port_chkready(rport))
1306                 return -ENXIO;
1307
1308         sdev->hostdata = *(fc_port_t **)rport->dd_data;
1309
1310         return 0;
1311 }
1312
1313 static int
1314 qla2xxx_slave_configure(struct scsi_device *sdev)
1315 {
1316         scsi_qla_host_t *vha = shost_priv(sdev->host);
1317         struct req_que *req = vha->req;
1318
1319         if (sdev->tagged_supported)
1320                 scsi_activate_tcq(sdev, req->max_q_depth);
1321         else
1322                 scsi_deactivate_tcq(sdev, req->max_q_depth);
1323         return 0;
1324 }
1325
1326 static void
1327 qla2xxx_slave_destroy(struct scsi_device *sdev)
1328 {
1329         sdev->hostdata = NULL;
1330 }
1331
1332 static void qla2x00_handle_queue_full(struct scsi_device *sdev, int qdepth)
1333 {
1334         fc_port_t *fcport = (struct fc_port *) sdev->hostdata;
1335
1336         if (!scsi_track_queue_full(sdev, qdepth))
1337                 return;
1338
1339         ql_dbg(ql_dbg_io, fcport->vha, 0x3029,
1340             "Queue depth adjusted-down to %d for nexus=%ld:%d:%d.\n",
1341             sdev->queue_depth, fcport->vha->host_no, sdev->id, sdev->lun);
1342 }
1343
1344 static void qla2x00_adjust_sdev_qdepth_up(struct scsi_device *sdev, int qdepth)
1345 {
1346         fc_port_t *fcport = sdev->hostdata;
1347         struct scsi_qla_host *vha = fcport->vha;
1348         struct req_que *req = NULL;
1349
1350         req = vha->req;
1351         if (!req)
1352                 return;
1353
1354         if (req->max_q_depth <= sdev->queue_depth || req->max_q_depth < qdepth)
1355                 return;
1356
1357         if (sdev->ordered_tags)
1358                 scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG, qdepth);
1359         else
1360                 scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, qdepth);
1361
1362         ql_dbg(ql_dbg_io, vha, 0x302a,
1363             "Queue depth adjusted-up to %d for nexus=%ld:%d:%d.\n",
1364             sdev->queue_depth, fcport->vha->host_no, sdev->id, sdev->lun);
1365 }
1366
1367 static int
1368 qla2x00_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
1369 {
1370         switch (reason) {
1371         case SCSI_QDEPTH_DEFAULT:
1372                 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
1373                 break;
1374         case SCSI_QDEPTH_QFULL:
1375                 qla2x00_handle_queue_full(sdev, qdepth);
1376                 break;
1377         case SCSI_QDEPTH_RAMP_UP:
1378                 qla2x00_adjust_sdev_qdepth_up(sdev, qdepth);
1379                 break;
1380         default:
1381                 return -EOPNOTSUPP;
1382         }
1383
1384         return sdev->queue_depth;
1385 }
1386
1387 static int
1388 qla2x00_change_queue_type(struct scsi_device *sdev, int tag_type)
1389 {
1390         if (sdev->tagged_supported) {
1391                 scsi_set_tag_type(sdev, tag_type);
1392                 if (tag_type)
1393                         scsi_activate_tcq(sdev, sdev->queue_depth);
1394                 else
1395                         scsi_deactivate_tcq(sdev, sdev->queue_depth);
1396         } else
1397                 tag_type = 0;
1398
1399         return tag_type;
1400 }
1401
1402 /**
1403  * qla2x00_config_dma_addressing() - Configure OS DMA addressing method.
1404  * @ha: HA context
1405  *
1406  * At exit, the @ha's flags.enable_64bit_addressing set to indicated
1407  * supported addressing method.
1408  */
1409 static void
1410 qla2x00_config_dma_addressing(struct qla_hw_data *ha)
1411 {
1412         /* Assume a 32bit DMA mask. */
1413         ha->flags.enable_64bit_addressing = 0;
1414
1415         if (!dma_set_mask(&ha->pdev->dev, DMA_BIT_MASK(64))) {
1416                 /* Any upper-dword bits set? */
1417                 if (MSD(dma_get_required_mask(&ha->pdev->dev)) &&
1418                     !pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
1419                         /* Ok, a 64bit DMA mask is applicable. */
1420                         ha->flags.enable_64bit_addressing = 1;
1421                         ha->isp_ops->calc_req_entries = qla2x00_calc_iocbs_64;
1422                         ha->isp_ops->build_iocbs = qla2x00_build_scsi_iocbs_64;
1423                         return;
1424                 }
1425         }
1426
1427         dma_set_mask(&ha->pdev->dev, DMA_BIT_MASK(32));
1428         pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(32));
1429 }
1430
1431 static void
1432 qla2x00_enable_intrs(struct qla_hw_data *ha)
1433 {
1434         unsigned long flags = 0;
1435         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1436
1437         spin_lock_irqsave(&ha->hardware_lock, flags);
1438         ha->interrupts_on = 1;
1439         /* enable risc and host interrupts */
1440         WRT_REG_WORD(&reg->ictrl, ICR_EN_INT | ICR_EN_RISC);
1441         RD_REG_WORD(&reg->ictrl);
1442         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1443
1444 }
1445
1446 static void
1447 qla2x00_disable_intrs(struct qla_hw_data *ha)
1448 {
1449         unsigned long flags = 0;
1450         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1451
1452         spin_lock_irqsave(&ha->hardware_lock, flags);
1453         ha->interrupts_on = 0;
1454         /* disable risc and host interrupts */
1455         WRT_REG_WORD(&reg->ictrl, 0);
1456         RD_REG_WORD(&reg->ictrl);
1457         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1458 }
1459
1460 static void
1461 qla24xx_enable_intrs(struct qla_hw_data *ha)
1462 {
1463         unsigned long flags = 0;
1464         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1465
1466         spin_lock_irqsave(&ha->hardware_lock, flags);
1467         ha->interrupts_on = 1;
1468         WRT_REG_DWORD(&reg->ictrl, ICRX_EN_RISC_INT);
1469         RD_REG_DWORD(&reg->ictrl);
1470         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1471 }
1472
1473 static void
1474 qla24xx_disable_intrs(struct qla_hw_data *ha)
1475 {
1476         unsigned long flags = 0;
1477         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1478
1479         if (IS_NOPOLLING_TYPE(ha))
1480                 return;
1481         spin_lock_irqsave(&ha->hardware_lock, flags);
1482         ha->interrupts_on = 0;
1483         WRT_REG_DWORD(&reg->ictrl, 0);
1484         RD_REG_DWORD(&reg->ictrl);
1485         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1486 }
1487
1488 static int
1489 qla2x00_iospace_config(struct qla_hw_data *ha)
1490 {
1491         resource_size_t pio;
1492         uint16_t msix;
1493         int cpus;
1494
1495         if (IS_QLA82XX(ha))
1496                 return qla82xx_iospace_config(ha);
1497
1498         if (pci_request_selected_regions(ha->pdev, ha->bars,
1499             QLA2XXX_DRIVER_NAME)) {
1500                 ql_log_pci(ql_log_fatal, ha->pdev, 0x0011,
1501                     "Failed to reserve PIO/MMIO regions (%s), aborting.\n",
1502                     pci_name(ha->pdev));
1503                 goto iospace_error_exit;
1504         }
1505         if (!(ha->bars & 1))
1506                 goto skip_pio;
1507
1508         /* We only need PIO for Flash operations on ISP2312 v2 chips. */
1509         pio = pci_resource_start(ha->pdev, 0);
1510         if (pci_resource_flags(ha->pdev, 0) & IORESOURCE_IO) {
1511                 if (pci_resource_len(ha->pdev, 0) < MIN_IOBASE_LEN) {
1512                         ql_log_pci(ql_log_warn, ha->pdev, 0x0012,
1513                             "Invalid pci I/O region size (%s).\n",
1514                             pci_name(ha->pdev));
1515                         pio = 0;
1516                 }
1517         } else {
1518                 ql_log_pci(ql_log_warn, ha->pdev, 0x0013,
1519                     "Region #0 no a PIO resource (%s).\n",
1520                     pci_name(ha->pdev));
1521                 pio = 0;
1522         }
1523         ha->pio_address = pio;
1524         ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0014,
1525             "PIO address=%llu.\n",
1526             (unsigned long long)ha->pio_address);
1527
1528 skip_pio:
1529         /* Use MMIO operations for all accesses. */
1530         if (!(pci_resource_flags(ha->pdev, 1) & IORESOURCE_MEM)) {
1531                 ql_log_pci(ql_log_fatal, ha->pdev, 0x0015,
1532                     "Region #1 not an MMIO resource (%s), aborting.\n",
1533                     pci_name(ha->pdev));
1534                 goto iospace_error_exit;
1535         }
1536         if (pci_resource_len(ha->pdev, 1) < MIN_IOBASE_LEN) {
1537                 ql_log_pci(ql_log_fatal, ha->pdev, 0x0016,
1538                     "Invalid PCI mem region size (%s), aborting.\n",
1539                     pci_name(ha->pdev));
1540                 goto iospace_error_exit;
1541         }
1542
1543         ha->iobase = ioremap(pci_resource_start(ha->pdev, 1), MIN_IOBASE_LEN);
1544         if (!ha->iobase) {
1545                 ql_log_pci(ql_log_fatal, ha->pdev, 0x0017,
1546                     "Cannot remap MMIO (%s), aborting.\n",
1547                     pci_name(ha->pdev));
1548                 goto iospace_error_exit;
1549         }
1550
1551         /* Determine queue resources */
1552         ha->max_req_queues = ha->max_rsp_queues = 1;
1553         if ((ql2xmaxqueues <= 1 && !ql2xmultique_tag) ||
1554                 (ql2xmaxqueues > 1 && ql2xmultique_tag) ||
1555                 (!IS_QLA25XX(ha) && !IS_QLA81XX(ha)))
1556                 goto mqiobase_exit;
1557
1558         ha->mqiobase = ioremap(pci_resource_start(ha->pdev, 3),
1559                         pci_resource_len(ha->pdev, 3));
1560         if (ha->mqiobase) {
1561                 ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0018,
1562                     "MQIO Base=%p.\n", ha->mqiobase);
1563                 /* Read MSIX vector size of the board */
1564                 pci_read_config_word(ha->pdev, QLA_PCI_MSIX_CONTROL, &msix);
1565                 ha->msix_count = msix;
1566                 /* Max queues are bounded by available msix vectors */
1567                 /* queue 0 uses two msix vectors */
1568                 if (ql2xmultique_tag) {
1569                         cpus = num_online_cpus();
1570                         ha->max_rsp_queues = (ha->msix_count - 1 > cpus) ?
1571                                 (cpus + 1) : (ha->msix_count - 1);
1572                         ha->max_req_queues = 2;
1573                 } else if (ql2xmaxqueues > 1) {
1574                         ha->max_req_queues = ql2xmaxqueues > QLA_MQ_SIZE ?
1575                             QLA_MQ_SIZE : ql2xmaxqueues;
1576                         ql_dbg_pci(ql_dbg_multiq, ha->pdev, 0xc008,
1577                             "QoS mode set, max no of request queues:%d.\n",
1578                             ha->max_req_queues);
1579                         ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0019,
1580                             "QoS mode set, max no of request queues:%d.\n",
1581                             ha->max_req_queues);
1582                 }
1583                 ql_log_pci(ql_log_info, ha->pdev, 0x001a,
1584                     "MSI-X vector count: %d.\n", msix);
1585         } else
1586                 ql_log_pci(ql_log_info, ha->pdev, 0x001b,
1587                     "BAR 3 not enabled.\n");
1588
1589 mqiobase_exit:
1590         ha->msix_count = ha->max_rsp_queues + 1;
1591         ql_dbg_pci(ql_dbg_init, ha->pdev, 0x001c,
1592             "MSIX Count:%d.\n", ha->msix_count);
1593         return (0);
1594
1595 iospace_error_exit:
1596         return (-ENOMEM);
1597 }
1598
1599
1600 static struct isp_operations qla2100_isp_ops = {
1601         .pci_config             = qla2100_pci_config,
1602         .reset_chip             = qla2x00_reset_chip,
1603         .chip_diag              = qla2x00_chip_diag,
1604         .config_rings           = qla2x00_config_rings,
1605         .reset_adapter          = qla2x00_reset_adapter,
1606         .nvram_config           = qla2x00_nvram_config,
1607         .update_fw_options      = qla2x00_update_fw_options,
1608         .load_risc              = qla2x00_load_risc,
1609         .pci_info_str           = qla2x00_pci_info_str,
1610         .fw_version_str         = qla2x00_fw_version_str,
1611         .intr_handler           = qla2100_intr_handler,
1612         .enable_intrs           = qla2x00_enable_intrs,
1613         .disable_intrs          = qla2x00_disable_intrs,
1614         .abort_command          = qla2x00_abort_command,
1615         .target_reset           = qla2x00_abort_target,
1616         .lun_reset              = qla2x00_lun_reset,
1617         .fabric_login           = qla2x00_login_fabric,
1618         .fabric_logout          = qla2x00_fabric_logout,
1619         .calc_req_entries       = qla2x00_calc_iocbs_32,
1620         .build_iocbs            = qla2x00_build_scsi_iocbs_32,
1621         .prep_ms_iocb           = qla2x00_prep_ms_iocb,
1622         .prep_ms_fdmi_iocb      = qla2x00_prep_ms_fdmi_iocb,
1623         .read_nvram             = qla2x00_read_nvram_data,
1624         .write_nvram            = qla2x00_write_nvram_data,
1625         .fw_dump                = qla2100_fw_dump,
1626         .beacon_on              = NULL,
1627         .beacon_off             = NULL,
1628         .beacon_blink           = NULL,
1629         .read_optrom            = qla2x00_read_optrom_data,
1630         .write_optrom           = qla2x00_write_optrom_data,
1631         .get_flash_version      = qla2x00_get_flash_version,
1632         .start_scsi             = qla2x00_start_scsi,
1633         .abort_isp              = qla2x00_abort_isp,
1634         .iospace_config         = qla2x00_iospace_config,
1635 };
1636
1637 static struct isp_operations qla2300_isp_ops = {
1638         .pci_config             = qla2300_pci_config,
1639         .reset_chip             = qla2x00_reset_chip,
1640         .chip_diag              = qla2x00_chip_diag,
1641         .config_rings           = qla2x00_config_rings,
1642         .reset_adapter          = qla2x00_reset_adapter,
1643         .nvram_config           = qla2x00_nvram_config,
1644         .update_fw_options      = qla2x00_update_fw_options,
1645         .load_risc              = qla2x00_load_risc,
1646         .pci_info_str           = qla2x00_pci_info_str,
1647         .fw_version_str         = qla2x00_fw_version_str,
1648         .intr_handler           = qla2300_intr_handler,
1649         .enable_intrs           = qla2x00_enable_intrs,
1650         .disable_intrs          = qla2x00_disable_intrs,
1651         .abort_command          = qla2x00_abort_command,
1652         .target_reset           = qla2x00_abort_target,
1653         .lun_reset              = qla2x00_lun_reset,
1654         .fabric_login           = qla2x00_login_fabric,
1655         .fabric_logout          = qla2x00_fabric_logout,
1656         .calc_req_entries       = qla2x00_calc_iocbs_32,
1657         .build_iocbs            = qla2x00_build_scsi_iocbs_32,
1658         .prep_ms_iocb           = qla2x00_prep_ms_iocb,
1659         .prep_ms_fdmi_iocb      = qla2x00_prep_ms_fdmi_iocb,
1660         .read_nvram             = qla2x00_read_nvram_data,
1661         .write_nvram            = qla2x00_write_nvram_data,
1662         .fw_dump                = qla2300_fw_dump,
1663         .beacon_on              = qla2x00_beacon_on,
1664         .beacon_off             = qla2x00_beacon_off,
1665         .beacon_blink           = qla2x00_beacon_blink,
1666         .read_optrom            = qla2x00_read_optrom_data,
1667         .write_optrom           = qla2x00_write_optrom_data,
1668         .get_flash_version      = qla2x00_get_flash_version,
1669         .start_scsi             = qla2x00_start_scsi,
1670         .abort_isp              = qla2x00_abort_isp,
1671         .iospace_config         = qla2x00_iospace_config,
1672 };
1673
1674 static struct isp_operations qla24xx_isp_ops = {
1675         .pci_config             = qla24xx_pci_config,
1676         .reset_chip             = qla24xx_reset_chip,
1677         .chip_diag              = qla24xx_chip_diag,
1678         .config_rings           = qla24xx_config_rings,
1679         .reset_adapter          = qla24xx_reset_adapter,
1680         .nvram_config           = qla24xx_nvram_config,
1681         .update_fw_options      = qla24xx_update_fw_options,
1682         .load_risc              = qla24xx_load_risc,
1683         .pci_info_str           = qla24xx_pci_info_str,
1684         .fw_version_str         = qla24xx_fw_version_str,
1685         .intr_handler           = qla24xx_intr_handler,
1686         .enable_intrs           = qla24xx_enable_intrs,
1687         .disable_intrs          = qla24xx_disable_intrs,
1688         .abort_command          = qla24xx_abort_command,
1689         .target_reset           = qla24xx_abort_target,
1690         .lun_reset              = qla24xx_lun_reset,
1691         .fabric_login           = qla24xx_login_fabric,
1692         .fabric_logout          = qla24xx_fabric_logout,
1693         .calc_req_entries       = NULL,
1694         .build_iocbs            = NULL,
1695         .prep_ms_iocb           = qla24xx_prep_ms_iocb,
1696         .prep_ms_fdmi_iocb      = qla24xx_prep_ms_fdmi_iocb,
1697         .read_nvram             = qla24xx_read_nvram_data,
1698         .write_nvram            = qla24xx_write_nvram_data,
1699         .fw_dump                = qla24xx_fw_dump,
1700         .beacon_on              = qla24xx_beacon_on,
1701         .beacon_off             = qla24xx_beacon_off,
1702         .beacon_blink           = qla24xx_beacon_blink,
1703         .read_optrom            = qla24xx_read_optrom_data,
1704         .write_optrom           = qla24xx_write_optrom_data,
1705         .get_flash_version      = qla24xx_get_flash_version,
1706         .start_scsi             = qla24xx_start_scsi,
1707         .abort_isp              = qla2x00_abort_isp,
1708         .iospace_config         = qla2x00_iospace_config,
1709 };
1710
1711 static struct isp_operations qla25xx_isp_ops = {
1712         .pci_config             = qla25xx_pci_config,
1713         .reset_chip             = qla24xx_reset_chip,
1714         .chip_diag              = qla24xx_chip_diag,
1715         .config_rings           = qla24xx_config_rings,
1716         .reset_adapter          = qla24xx_reset_adapter,
1717         .nvram_config           = qla24xx_nvram_config,
1718         .update_fw_options      = qla24xx_update_fw_options,
1719         .load_risc              = qla24xx_load_risc,
1720         .pci_info_str           = qla24xx_pci_info_str,
1721         .fw_version_str         = qla24xx_fw_version_str,
1722         .intr_handler           = qla24xx_intr_handler,
1723         .enable_intrs           = qla24xx_enable_intrs,
1724         .disable_intrs          = qla24xx_disable_intrs,
1725         .abort_command          = qla24xx_abort_command,
1726         .target_reset           = qla24xx_abort_target,
1727         .lun_reset              = qla24xx_lun_reset,
1728         .fabric_login           = qla24xx_login_fabric,
1729         .fabric_logout          = qla24xx_fabric_logout,
1730         .calc_req_entries       = NULL,
1731         .build_iocbs            = NULL,
1732         .prep_ms_iocb           = qla24xx_prep_ms_iocb,
1733         .prep_ms_fdmi_iocb      = qla24xx_prep_ms_fdmi_iocb,
1734         .read_nvram             = qla25xx_read_nvram_data,
1735         .write_nvram            = qla25xx_write_nvram_data,
1736         .fw_dump                = qla25xx_fw_dump,
1737         .beacon_on              = qla24xx_beacon_on,
1738         .beacon_off             = qla24xx_beacon_off,
1739         .beacon_blink           = qla24xx_beacon_blink,
1740         .read_optrom            = qla25xx_read_optrom_data,
1741         .write_optrom           = qla24xx_write_optrom_data,
1742         .get_flash_version      = qla24xx_get_flash_version,
1743         .start_scsi             = qla24xx_dif_start_scsi,
1744         .abort_isp              = qla2x00_abort_isp,
1745         .iospace_config         = qla2x00_iospace_config,
1746 };
1747
1748 static struct isp_operations qla81xx_isp_ops = {
1749         .pci_config             = qla25xx_pci_config,
1750         .reset_chip             = qla24xx_reset_chip,
1751         .chip_diag              = qla24xx_chip_diag,
1752         .config_rings           = qla24xx_config_rings,
1753         .reset_adapter          = qla24xx_reset_adapter,
1754         .nvram_config           = qla81xx_nvram_config,
1755         .update_fw_options      = qla81xx_update_fw_options,
1756         .load_risc              = qla81xx_load_risc,
1757         .pci_info_str           = qla24xx_pci_info_str,
1758         .fw_version_str         = qla24xx_fw_version_str,
1759         .intr_handler           = qla24xx_intr_handler,
1760         .enable_intrs           = qla24xx_enable_intrs,
1761         .disable_intrs          = qla24xx_disable_intrs,
1762         .abort_command          = qla24xx_abort_command,
1763         .target_reset           = qla24xx_abort_target,
1764         .lun_reset              = qla24xx_lun_reset,
1765         .fabric_login           = qla24xx_login_fabric,
1766         .fabric_logout          = qla24xx_fabric_logout,
1767         .calc_req_entries       = NULL,
1768         .build_iocbs            = NULL,
1769         .prep_ms_iocb           = qla24xx_prep_ms_iocb,
1770         .prep_ms_fdmi_iocb      = qla24xx_prep_ms_fdmi_iocb,
1771         .read_nvram             = NULL,
1772         .write_nvram            = NULL,
1773         .fw_dump                = qla81xx_fw_dump,
1774         .beacon_on              = qla24xx_beacon_on,
1775         .beacon_off             = qla24xx_beacon_off,
1776         .beacon_blink           = qla24xx_beacon_blink,
1777         .read_optrom            = qla25xx_read_optrom_data,
1778         .write_optrom           = qla24xx_write_optrom_data,
1779         .get_flash_version      = qla24xx_get_flash_version,
1780         .start_scsi             = qla24xx_dif_start_scsi,
1781         .abort_isp              = qla2x00_abort_isp,
1782         .iospace_config         = qla2x00_iospace_config,
1783 };
1784
1785 static struct isp_operations qla82xx_isp_ops = {
1786         .pci_config             = qla82xx_pci_config,
1787         .reset_chip             = qla82xx_reset_chip,
1788         .chip_diag              = qla24xx_chip_diag,
1789         .config_rings           = qla82xx_config_rings,
1790         .reset_adapter          = qla24xx_reset_adapter,
1791         .nvram_config           = qla81xx_nvram_config,
1792         .update_fw_options      = qla24xx_update_fw_options,
1793         .load_risc              = qla82xx_load_risc,
1794         .pci_info_str           = qla82xx_pci_info_str,
1795         .fw_version_str         = qla24xx_fw_version_str,
1796         .intr_handler           = qla82xx_intr_handler,
1797         .enable_intrs           = qla82xx_enable_intrs,
1798         .disable_intrs          = qla82xx_disable_intrs,
1799         .abort_command          = qla24xx_abort_command,
1800         .target_reset           = qla24xx_abort_target,
1801         .lun_reset              = qla24xx_lun_reset,
1802         .fabric_login           = qla24xx_login_fabric,
1803         .fabric_logout          = qla24xx_fabric_logout,
1804         .calc_req_entries       = NULL,
1805         .build_iocbs            = NULL,
1806         .prep_ms_iocb           = qla24xx_prep_ms_iocb,
1807         .prep_ms_fdmi_iocb      = qla24xx_prep_ms_fdmi_iocb,
1808         .read_nvram             = qla24xx_read_nvram_data,
1809         .write_nvram            = qla24xx_write_nvram_data,
1810         .fw_dump                = qla24xx_fw_dump,
1811         .beacon_on              = qla82xx_beacon_on,
1812         .beacon_off             = qla82xx_beacon_off,
1813         .beacon_blink           = NULL,
1814         .read_optrom            = qla82xx_read_optrom_data,
1815         .write_optrom           = qla82xx_write_optrom_data,
1816         .get_flash_version      = qla24xx_get_flash_version,
1817         .start_scsi             = qla82xx_start_scsi,
1818         .abort_isp              = qla82xx_abort_isp,
1819         .iospace_config         = qla82xx_iospace_config,
1820 };
1821
1822 static inline void
1823 qla2x00_set_isp_flags(struct qla_hw_data *ha)
1824 {
1825         ha->device_type = DT_EXTENDED_IDS;
1826         switch (ha->pdev->device) {
1827         case PCI_DEVICE_ID_QLOGIC_ISP2100:
1828                 ha->device_type |= DT_ISP2100;
1829                 ha->device_type &= ~DT_EXTENDED_IDS;
1830                 ha->fw_srisc_address = RISC_START_ADDRESS_2100;
1831                 break;
1832         case PCI_DEVICE_ID_QLOGIC_ISP2200:
1833                 ha->device_type |= DT_ISP2200;
1834                 ha->device_type &= ~DT_EXTENDED_IDS;
1835                 ha->fw_srisc_address = RISC_START_ADDRESS_2100;
1836                 break;
1837         case PCI_DEVICE_ID_QLOGIC_ISP2300:
1838                 ha->device_type |= DT_ISP2300;
1839                 ha->device_type |= DT_ZIO_SUPPORTED;
1840                 ha->fw_srisc_address = RISC_START_ADDRESS_2300;
1841                 break;
1842         case PCI_DEVICE_ID_QLOGIC_ISP2312:
1843                 ha->device_type |= DT_ISP2312;
1844                 ha->device_type |= DT_ZIO_SUPPORTED;
1845                 ha->fw_srisc_address = RISC_START_ADDRESS_2300;
1846                 break;
1847         case PCI_DEVICE_ID_QLOGIC_ISP2322:
1848                 ha->device_type |= DT_ISP2322;
1849                 ha->device_type |= DT_ZIO_SUPPORTED;
1850                 if (ha->pdev->subsystem_vendor == 0x1028 &&
1851                     ha->pdev->subsystem_device == 0x0170)
1852                         ha->device_type |= DT_OEM_001;
1853                 ha->fw_srisc_address = RISC_START_ADDRESS_2300;
1854                 break;
1855         case PCI_DEVICE_ID_QLOGIC_ISP6312:
1856                 ha->device_type |= DT_ISP6312;
1857                 ha->fw_srisc_address = RISC_START_ADDRESS_2300;
1858                 break;
1859         case PCI_DEVICE_ID_QLOGIC_ISP6322:
1860                 ha->device_type |= DT_ISP6322;
1861                 ha->fw_srisc_address = RISC_START_ADDRESS_2300;
1862                 break;
1863         case PCI_DEVICE_ID_QLOGIC_ISP2422:
1864                 ha->device_type |= DT_ISP2422;
1865                 ha->device_type |= DT_ZIO_SUPPORTED;
1866                 ha->device_type |= DT_FWI2;
1867                 ha->device_type |= DT_IIDMA;
1868                 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
1869                 break;
1870         case PCI_DEVICE_ID_QLOGIC_ISP2432:
1871                 ha->device_type |= DT_ISP2432;
1872                 ha->device_type |= DT_ZIO_SUPPORTED;
1873                 ha->device_type |= DT_FWI2;
1874                 ha->device_type |= DT_IIDMA;
1875                 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
1876                 break;
1877         case PCI_DEVICE_ID_QLOGIC_ISP8432:
1878                 ha->device_type |= DT_ISP8432;
1879                 ha->device_type |= DT_ZIO_SUPPORTED;
1880                 ha->device_type |= DT_FWI2;
1881                 ha->device_type |= DT_IIDMA;
1882                 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
1883                 break;
1884         case PCI_DEVICE_ID_QLOGIC_ISP5422:
1885                 ha->device_type |= DT_ISP5422;
1886                 ha->device_type |= DT_FWI2;
1887                 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
1888                 break;
1889         case PCI_DEVICE_ID_QLOGIC_ISP5432:
1890                 ha->device_type |= DT_ISP5432;
1891                 ha->device_type |= DT_FWI2;
1892                 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
1893                 break;
1894         case PCI_DEVICE_ID_QLOGIC_ISP2532:
1895                 ha->device_type |= DT_ISP2532;
1896                 ha->device_type |= DT_ZIO_SUPPORTED;
1897                 ha->device_type |= DT_FWI2;
1898                 ha->device_type |= DT_IIDMA;
1899                 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
1900                 break;
1901         case PCI_DEVICE_ID_QLOGIC_ISP8001:
1902                 ha->device_type |= DT_ISP8001;
1903                 ha->device_type |= DT_ZIO_SUPPORTED;
1904                 ha->device_type |= DT_FWI2;
1905                 ha->device_type |= DT_IIDMA;
1906                 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
1907                 break;
1908         case PCI_DEVICE_ID_QLOGIC_ISP8021:
1909                 ha->device_type |= DT_ISP8021;
1910                 ha->device_type |= DT_ZIO_SUPPORTED;
1911                 ha->device_type |= DT_FWI2;
1912                 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
1913                 /* Initialize 82XX ISP flags */
1914                 qla82xx_init_flags(ha);
1915                 break;
1916         }
1917
1918         if (IS_QLA82XX(ha))
1919                 ha->port_no = !(ha->portnum & 1);
1920         else
1921                 /* Get adapter physical port no from interrupt pin register. */
1922                 pci_read_config_byte(ha->pdev, PCI_INTERRUPT_PIN, &ha->port_no);
1923
1924         if (ha->port_no & 1)
1925                 ha->flags.port0 = 1;
1926         else
1927                 ha->flags.port0 = 0;
1928         ql_dbg_pci(ql_dbg_init, ha->pdev, 0x000b,
1929             "device_type=0x%x port=%d fw_srisc_address=0x%x.\n",
1930             ha->device_type, ha->flags.port0, ha->fw_srisc_address);
1931 }
1932
1933 static void
1934 qla2xxx_scan_start(struct Scsi_Host *shost)
1935 {
1936         scsi_qla_host_t *vha = shost_priv(shost);
1937
1938         if (vha->hw->flags.running_gold_fw)
1939                 return;
1940
1941         set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
1942         set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
1943         set_bit(RSCN_UPDATE, &vha->dpc_flags);
1944         set_bit(NPIV_CONFIG_NEEDED, &vha->dpc_flags);
1945 }
1946
1947 static int
1948 qla2xxx_scan_finished(struct Scsi_Host *shost, unsigned long time)
1949 {
1950         scsi_qla_host_t *vha = shost_priv(shost);
1951
1952         if (!vha->host)
1953                 return 1;
1954         if (time > vha->hw->loop_reset_delay * HZ)
1955                 return 1;
1956
1957         return atomic_read(&vha->loop_state) == LOOP_READY;
1958 }
1959
1960 /*
1961  * PCI driver interface
1962  */
1963 static int __devinit
1964 qla2x00_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
1965 {
1966         int     ret = -ENODEV;
1967         struct Scsi_Host *host;
1968         scsi_qla_host_t *base_vha = NULL;
1969         struct qla_hw_data *ha;
1970         char pci_info[30];
1971         char fw_str[30];
1972         struct scsi_host_template *sht;
1973         int bars, max_id, mem_only = 0;
1974         uint16_t req_length = 0, rsp_length = 0;
1975         struct req_que *req = NULL;
1976         struct rsp_que *rsp = NULL;
1977
1978         bars = pci_select_bars(pdev, IORESOURCE_MEM | IORESOURCE_IO);
1979         sht = &qla2xxx_driver_template;
1980         if (pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2422 ||
1981             pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2432 ||
1982             pdev->device == PCI_DEVICE_ID_QLOGIC_ISP8432 ||
1983             pdev->device == PCI_DEVICE_ID_QLOGIC_ISP5422 ||
1984             pdev->device == PCI_DEVICE_ID_QLOGIC_ISP5432 ||
1985             pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2532 ||
1986             pdev->device == PCI_DEVICE_ID_QLOGIC_ISP8001 ||
1987             pdev->device == PCI_DEVICE_ID_QLOGIC_ISP8021) {
1988                 bars = pci_select_bars(pdev, IORESOURCE_MEM);
1989                 mem_only = 1;
1990                 ql_dbg_pci(ql_dbg_init, pdev, 0x0007,
1991                     "Mem only adapter.\n");
1992         }
1993         ql_dbg_pci(ql_dbg_init, pdev, 0x0008,
1994             "Bars=%d.\n", bars);
1995
1996         if (mem_only) {
1997                 if (pci_enable_device_mem(pdev))
1998                         goto probe_out;
1999         } else {
2000                 if (pci_enable_device(pdev))
2001                         goto probe_out;
2002         }
2003
2004         /* This may fail but that's ok */
2005         pci_enable_pcie_error_reporting(pdev);
2006
2007         ha = kzalloc(sizeof(struct qla_hw_data), GFP_KERNEL);
2008         if (!ha) {
2009                 ql_log_pci(ql_log_fatal, pdev, 0x0009,
2010                     "Unable to allocate memory for ha.\n");
2011                 goto probe_out;
2012         }
2013         ql_dbg_pci(ql_dbg_init, pdev, 0x000a,
2014             "Memory allocated for ha=%p.\n", ha);
2015         ha->pdev = pdev;
2016
2017         /* Clear our data area */
2018         ha->bars = bars;
2019         ha->mem_only = mem_only;
2020         spin_lock_init(&ha->hardware_lock);
2021         spin_lock_init(&ha->vport_slock);
2022
2023         /* Set ISP-type information. */
2024         qla2x00_set_isp_flags(ha);
2025
2026         /* Set EEH reset type to fundamental if required by hba */
2027         if ( IS_QLA24XX(ha) || IS_QLA25XX(ha) || IS_QLA81XX(ha)) {
2028                 pdev->needs_freset = 1;
2029         }
2030
2031         ha->prev_topology = 0;
2032         ha->init_cb_size = sizeof(init_cb_t);
2033         ha->link_data_rate = PORT_SPEED_UNKNOWN;
2034         ha->optrom_size = OPTROM_SIZE_2300;
2035
2036         /* Assign ISP specific operations. */
2037         max_id = MAX_TARGETS_2200;
2038         if (IS_QLA2100(ha)) {
2039                 max_id = MAX_TARGETS_2100;
2040                 ha->mbx_count = MAILBOX_REGISTER_COUNT_2100;
2041                 req_length = REQUEST_ENTRY_CNT_2100;
2042                 rsp_length = RESPONSE_ENTRY_CNT_2100;
2043                 ha->max_loop_id = SNS_LAST_LOOP_ID_2100;
2044                 ha->gid_list_info_size = 4;
2045                 ha->flash_conf_off = ~0;
2046                 ha->flash_data_off = ~0;
2047                 ha->nvram_conf_off = ~0;
2048                 ha->nvram_data_off = ~0;
2049                 ha->isp_ops = &qla2100_isp_ops;
2050         } else if (IS_QLA2200(ha)) {
2051                 ha->mbx_count = MAILBOX_REGISTER_COUNT;
2052                 req_length = REQUEST_ENTRY_CNT_2200;
2053                 rsp_length = RESPONSE_ENTRY_CNT_2100;
2054                 ha->max_loop_id = SNS_LAST_LOOP_ID_2100;
2055                 ha->gid_list_info_size = 4;
2056                 ha->flash_conf_off = ~0;
2057                 ha->flash_data_off = ~0;
2058                 ha->nvram_conf_off = ~0;
2059                 ha->nvram_data_off = ~0;
2060                 ha->isp_ops = &qla2100_isp_ops;
2061         } else if (IS_QLA23XX(ha)) {
2062                 ha->mbx_count = MAILBOX_REGISTER_COUNT;
2063                 req_length = REQUEST_ENTRY_CNT_2200;
2064                 rsp_length = RESPONSE_ENTRY_CNT_2300;
2065                 ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
2066                 ha->gid_list_info_size = 6;
2067                 if (IS_QLA2322(ha) || IS_QLA6322(ha))
2068                         ha->optrom_size = OPTROM_SIZE_2322;
2069                 ha->flash_conf_off = ~0;
2070                 ha->flash_data_off = ~0;
2071                 ha->nvram_conf_off = ~0;
2072                 ha->nvram_data_off = ~0;
2073                 ha->isp_ops = &qla2300_isp_ops;
2074         } else if (IS_QLA24XX_TYPE(ha)) {
2075                 ha->mbx_count = MAILBOX_REGISTER_COUNT;
2076                 req_length = REQUEST_ENTRY_CNT_24XX;
2077                 rsp_length = RESPONSE_ENTRY_CNT_2300;
2078                 ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
2079                 ha->init_cb_size = sizeof(struct mid_init_cb_24xx);
2080                 ha->gid_list_info_size = 8;
2081                 ha->optrom_size = OPTROM_SIZE_24XX;
2082                 ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA24XX;
2083                 ha->isp_ops = &qla24xx_isp_ops;
2084                 ha->flash_conf_off = FARX_ACCESS_FLASH_CONF;
2085                 ha->flash_data_off = FARX_ACCESS_FLASH_DATA;
2086                 ha->nvram_conf_off = FARX_ACCESS_NVRAM_CONF;
2087                 ha->nvram_data_off = FARX_ACCESS_NVRAM_DATA;
2088         } else if (IS_QLA25XX(ha)) {
2089                 ha->mbx_count = MAILBOX_REGISTER_COUNT;
2090                 req_length = REQUEST_ENTRY_CNT_24XX;
2091                 rsp_length = RESPONSE_ENTRY_CNT_2300;
2092                 ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
2093                 ha->init_cb_size = sizeof(struct mid_init_cb_24xx);
2094                 ha->gid_list_info_size = 8;
2095                 ha->optrom_size = OPTROM_SIZE_25XX;
2096                 ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA25XX;
2097                 ha->isp_ops = &qla25xx_isp_ops;
2098                 ha->flash_conf_off = FARX_ACCESS_FLASH_CONF;
2099                 ha->flash_data_off = FARX_ACCESS_FLASH_DATA;
2100                 ha->nvram_conf_off = FARX_ACCESS_NVRAM_CONF;
2101                 ha->nvram_data_off = FARX_ACCESS_NVRAM_DATA;
2102         } else if (IS_QLA81XX(ha)) {
2103                 ha->mbx_count = MAILBOX_REGISTER_COUNT;
2104                 req_length = REQUEST_ENTRY_CNT_24XX;
2105                 rsp_length = RESPONSE_ENTRY_CNT_2300;
2106                 ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
2107                 ha->init_cb_size = sizeof(struct mid_init_cb_81xx);
2108                 ha->gid_list_info_size = 8;
2109                 ha->optrom_size = OPTROM_SIZE_81XX;
2110                 ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA25XX;
2111                 ha->isp_ops = &qla81xx_isp_ops;
2112                 ha->flash_conf_off = FARX_ACCESS_FLASH_CONF_81XX;
2113                 ha->flash_data_off = FARX_ACCESS_FLASH_DATA_81XX;
2114                 ha->nvram_conf_off = ~0;
2115                 ha->nvram_data_off = ~0;
2116         } else if (IS_QLA82XX(ha)) {
2117                 ha->mbx_count = MAILBOX_REGISTER_COUNT;
2118                 req_length = REQUEST_ENTRY_CNT_82XX;
2119                 rsp_length = RESPONSE_ENTRY_CNT_82XX;
2120                 ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
2121                 ha->init_cb_size = sizeof(struct mid_init_cb_81xx);
2122                 ha->gid_list_info_size = 8;
2123                 ha->optrom_size = OPTROM_SIZE_82XX;
2124                 ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA25XX;
2125                 ha->isp_ops = &qla82xx_isp_ops;
2126                 ha->flash_conf_off = FARX_ACCESS_FLASH_CONF;
2127                 ha->flash_data_off = FARX_ACCESS_FLASH_DATA;
2128                 ha->nvram_conf_off = FARX_ACCESS_NVRAM_CONF;
2129                 ha->nvram_data_off = FARX_ACCESS_NVRAM_DATA;
2130         }
2131         ql_dbg_pci(ql_dbg_init, pdev, 0x001e,
2132             "mbx_count=%d, req_length=%d, "
2133             "rsp_length=%d, max_loop_id=%d, init_cb_size=%d, "
2134             "gid_list_info_size=%d, optrom_size=%d, nvram_npiv_size=%d, .\n",
2135             ha->mbx_count, req_length, rsp_length, ha->max_loop_id,
2136             ha->init_cb_size, ha->gid_list_info_size, ha->optrom_size,
2137             ha->nvram_npiv_size);
2138         ql_dbg_pci(ql_dbg_init, pdev, 0x001f,
2139             "isp_ops=%p, flash_conf_off=%d, "
2140             "flash_data_off=%d, nvram_conf_off=%d, nvram_data_off=%d.\n",
2141             ha->isp_ops, ha->flash_conf_off, ha->flash_data_off,
2142             ha->nvram_conf_off, ha->nvram_data_off);
2143
2144         /* Configure PCI I/O space */
2145         ret = ha->isp_ops->iospace_config(ha);
2146         if (ret)
2147                 goto probe_hw_failed;
2148
2149         ql_log_pci(ql_log_info, pdev, 0x001d,
2150             "Found an ISP%04X irq %d iobase 0x%p.\n",
2151             pdev->device, pdev->irq, ha->iobase);
2152         mutex_init(&ha->vport_lock);
2153         init_completion(&ha->mbx_cmd_comp);
2154         complete(&ha->mbx_cmd_comp);
2155         init_completion(&ha->mbx_intr_comp);
2156         init_completion(&ha->dcbx_comp);
2157
2158         set_bit(0, (unsigned long *) ha->vp_idx_map);
2159
2160         qla2x00_config_dma_addressing(ha);
2161         ql_dbg_pci(ql_dbg_init, pdev, 0x0020,
2162             "64 Bit addressing is %s.\n",
2163             ha->flags.enable_64bit_addressing ? "enable" :
2164             "disable");
2165         ret = qla2x00_mem_alloc(ha, req_length, rsp_length, &req, &rsp);
2166         if (!ret) {
2167                 ql_log_pci(ql_log_fatal, pdev, 0x0031,
2168                     "Failed to allocate memory for adapter, aborting.\n");
2169
2170                 goto probe_hw_failed;
2171         }
2172
2173         req->max_q_depth = MAX_Q_DEPTH;
2174         if (ql2xmaxqdepth != 0 && ql2xmaxqdepth <= 0xffffU)
2175                 req->max_q_depth = ql2xmaxqdepth;
2176
2177
2178         base_vha = qla2x00_create_host(sht, ha);
2179         if (!base_vha) {
2180                 ret = -ENOMEM;
2181                 qla2x00_mem_free(ha);
2182                 qla2x00_free_req_que(ha, req);
2183                 qla2x00_free_rsp_que(ha, rsp);
2184                 goto probe_hw_failed;
2185         }
2186
2187         pci_set_drvdata(pdev, base_vha);
2188
2189         host = base_vha->host;
2190         base_vha->req = req;
2191         host->can_queue = req->length + 128;
2192         if (IS_QLA2XXX_MIDTYPE(ha))
2193                 base_vha->mgmt_svr_loop_id = 10 + base_vha->vp_idx;
2194         else
2195                 base_vha->mgmt_svr_loop_id = MANAGEMENT_SERVER +
2196                                                 base_vha->vp_idx;
2197
2198         /* Set the SG table size based on ISP type */
2199         if (!IS_FWI2_CAPABLE(ha)) {
2200                 if (IS_QLA2100(ha))
2201                         host->sg_tablesize = 32;
2202         } else {
2203                 if (!IS_QLA82XX(ha))
2204                         host->sg_tablesize = QLA_SG_ALL;
2205         }
2206         ql_dbg(ql_dbg_init, base_vha, 0x0032,
2207             "can_queue=%d, req=%p, "
2208             "mgmt_svr_loop_id=%d, sg_tablesize=%d.\n",
2209             host->can_queue, base_vha->req,
2210             base_vha->mgmt_svr_loop_id, host->sg_tablesize);
2211         host->max_id = max_id;
2212         host->this_id = 255;
2213         host->cmd_per_lun = 3;
2214         host->unique_id = host->host_no;
2215         if (IS_T10_PI_CAPABLE(ha) && ql2xenabledif)
2216                 host->max_cmd_len = 32;
2217         else
2218                 host->max_cmd_len = MAX_CMDSZ;
2219         host->max_channel = MAX_BUSES - 1;
2220         host->max_lun = ql2xmaxlun;
2221         host->transportt = qla2xxx_transport_template;
2222         sht->vendor_id = (SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_QLOGIC);
2223
2224         ql_dbg(ql_dbg_init, base_vha, 0x0033,
2225             "max_id=%d this_id=%d "
2226             "cmd_per_len=%d unique_id=%d max_cmd_len=%d max_channel=%d "
2227             "max_lun=%d transportt=%p, vendor_id=%llu.\n", host->max_id,
2228             host->this_id, host->cmd_per_lun, host->unique_id,
2229             host->max_cmd_len, host->max_channel, host->max_lun,
2230             host->transportt, sht->vendor_id);
2231
2232         /* Set up the irqs */
2233         ret = qla2x00_request_irqs(ha, rsp);
2234         if (ret)
2235                 goto probe_init_failed;
2236
2237         pci_save_state(pdev);
2238
2239         /* Alloc arrays of request and response ring ptrs */
2240 que_init:
2241         if (!qla2x00_alloc_queues(ha)) {
2242                 ql_log(ql_log_fatal, base_vha, 0x003d,
2243                     "Failed to allocate memory for queue pointers.. aborting.\n");
2244                 goto probe_init_failed;
2245         }
2246
2247         ha->rsp_q_map[0] = rsp;
2248         ha->req_q_map[0] = req;
2249         rsp->req = req;
2250         req->rsp = rsp;
2251         set_bit(0, ha->req_qid_map);
2252         set_bit(0, ha->rsp_qid_map);
2253         /* FWI2-capable only. */
2254         req->req_q_in = &ha->iobase->isp24.req_q_in;
2255         req->req_q_out = &ha->iobase->isp24.req_q_out;
2256         rsp->rsp_q_in = &ha->iobase->isp24.rsp_q_in;
2257         rsp->rsp_q_out = &ha->iobase->isp24.rsp_q_out;
2258         if (ha->mqenable) {
2259                 req->req_q_in = &ha->mqiobase->isp25mq.req_q_in;
2260                 req->req_q_out = &ha->mqiobase->isp25mq.req_q_out;
2261                 rsp->rsp_q_in = &ha->mqiobase->isp25mq.rsp_q_in;
2262                 rsp->rsp_q_out =  &ha->mqiobase->isp25mq.rsp_q_out;
2263         }
2264
2265         if (IS_QLA82XX(ha)) {
2266                 req->req_q_out = &ha->iobase->isp82.req_q_out[0];
2267                 rsp->rsp_q_in = &ha->iobase->isp82.rsp_q_in[0];
2268                 rsp->rsp_q_out = &ha->iobase->isp82.rsp_q_out[0];
2269         }
2270
2271         ql_dbg(ql_dbg_multiq, base_vha, 0xc009,
2272             "rsp_q_map=%p req_q_map=%p rsp->req=%p req->rsp=%p.\n",
2273             ha->rsp_q_map, ha->req_q_map, rsp->req, req->rsp);
2274         ql_dbg(ql_dbg_multiq, base_vha, 0xc00a,
2275             "req->req_q_in=%p req->req_q_out=%p "
2276             "rsp->rsp_q_in=%p rsp->rsp_q_out=%p.\n",
2277             req->req_q_in, req->req_q_out,
2278             rsp->rsp_q_in, rsp->rsp_q_out);
2279         ql_dbg(ql_dbg_init, base_vha, 0x003e,
2280             "rsp_q_map=%p req_q_map=%p rsp->req=%p req->rsp=%p.\n",
2281             ha->rsp_q_map, ha->req_q_map, rsp->req, req->rsp);
2282         ql_dbg(ql_dbg_init, base_vha, 0x003f,
2283             "req->req_q_in=%p req->req_q_out=%p rsp->rsp_q_in=%p rsp->rsp_q_out=%p.\n",
2284             req->req_q_in, req->req_q_out, rsp->rsp_q_in, rsp->rsp_q_out);
2285
2286         if (qla2x00_initialize_adapter(base_vha)) {
2287                 ql_log(ql_log_fatal, base_vha, 0x00d6,
2288                     "Failed to initialize adapter - Adapter flags %x.\n",
2289                     base_vha->device_flags);
2290
2291                 if (IS_QLA82XX(ha)) {
2292                         qla82xx_idc_lock(ha);
2293                         qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2294                                 QLA82XX_DEV_FAILED);
2295                         qla82xx_idc_unlock(ha);
2296                         ql_log(ql_log_fatal, base_vha, 0x00d7,
2297                             "HW State: FAILED.\n");
2298                 }
2299
2300                 ret = -ENODEV;
2301                 goto probe_failed;
2302         }
2303
2304         if (ha->mqenable) {
2305                 if (qla25xx_setup_mode(base_vha)) {
2306                         ql_log(ql_log_warn, base_vha, 0x00ec,
2307                             "Failed to create queues, falling back to single queue mode.\n");
2308                         goto que_init;
2309                 }
2310         }
2311
2312         if (ha->flags.running_gold_fw)
2313                 goto skip_dpc;
2314
2315         /*
2316          * Startup the kernel thread for this host adapter
2317          */
2318         ha->dpc_thread = kthread_create(qla2x00_do_dpc, ha,
2319             "%s_dpc", base_vha->host_str);
2320         if (IS_ERR(ha->dpc_thread)) {
2321                 ql_log(ql_log_fatal, base_vha, 0x00ed,
2322                     "Failed to start DPC thread.\n");
2323                 ret = PTR_ERR(ha->dpc_thread);
2324                 goto probe_failed;
2325         }
2326         ql_dbg(ql_dbg_init, base_vha, 0x00ee,
2327             "DPC thread started successfully.\n");
2328
2329 skip_dpc:
2330         list_add_tail(&base_vha->list, &ha->vp_list);
2331         base_vha->host->irq = ha->pdev->irq;
2332
2333         /* Initialized the timer */
2334         qla2x00_start_timer(base_vha, qla2x00_timer, WATCH_INTERVAL);
2335         ql_dbg(ql_dbg_init, base_vha, 0x00ef,
2336             "Started qla2x00_timer with "
2337             "interval=%d.\n", WATCH_INTERVAL);
2338         ql_dbg(ql_dbg_init, base_vha, 0x00f0,
2339             "Detected hba at address=%p.\n",
2340             ha);
2341
2342         if (IS_T10_PI_CAPABLE(ha) && ql2xenabledif) {
2343                 if (ha->fw_attributes & BIT_4) {
2344                         int prot = 0;
2345                         base_vha->flags.difdix_supported = 1;
2346                         ql_dbg(ql_dbg_init, base_vha, 0x00f1,
2347                             "Registering for DIF/DIX type 1 and 3 protection.\n");
2348                         if (ql2xenabledif == 1)
2349                                 prot = SHOST_DIX_TYPE0_PROTECTION;
2350                         scsi_host_set_prot(host,
2351                             prot | SHOST_DIF_TYPE1_PROTECTION
2352                             | SHOST_DIF_TYPE2_PROTECTION
2353                             | SHOST_DIF_TYPE3_PROTECTION
2354                             | SHOST_DIX_TYPE1_PROTECTION
2355                             | SHOST_DIX_TYPE2_PROTECTION
2356                             | SHOST_DIX_TYPE3_PROTECTION);
2357                         scsi_host_set_guard(host, SHOST_DIX_GUARD_CRC);
2358                 } else
2359                         base_vha->flags.difdix_supported = 0;
2360         }
2361
2362         ha->isp_ops->enable_intrs(ha);
2363
2364         ret = scsi_add_host(host, &pdev->dev);
2365         if (ret)
2366                 goto probe_failed;
2367
2368         base_vha->flags.init_done = 1;
2369         base_vha->flags.online = 1;
2370
2371         ql_dbg(ql_dbg_init, base_vha, 0x00f2,
2372             "Init done and hba is online.\n");
2373
2374         scsi_scan_host(host);
2375
2376         qla2x00_alloc_sysfs_attr(base_vha);
2377
2378         qla2x00_init_host_attr(base_vha);
2379
2380         qla2x00_dfs_setup(base_vha);
2381
2382         ql_log(ql_log_info, base_vha, 0x00fb,
2383             "QLogic %s - %s.\n",
2384             ha->model_number, ha->model_desc ? ha->model_desc : "");
2385         ql_log(ql_log_info, base_vha, 0x00fc,
2386             "ISP%04X: %s @ %s hdma%c host#=%ld fw=%s.\n",
2387             pdev->device, ha->isp_ops->pci_info_str(base_vha, pci_info),
2388             pci_name(pdev), ha->flags.enable_64bit_addressing ? '+' : '-',
2389             base_vha->host_no,
2390             ha->isp_ops->fw_version_str(base_vha, fw_str));
2391
2392         return 0;
2393
2394 probe_init_failed:
2395         qla2x00_free_req_que(ha, req);
2396         qla2x00_free_rsp_que(ha, rsp);
2397         ha->max_req_queues = ha->max_rsp_queues = 0;
2398
2399 probe_failed:
2400         if (base_vha->timer_active)
2401                 qla2x00_stop_timer(base_vha);
2402         base_vha->flags.online = 0;
2403         if (ha->dpc_thread) {
2404                 struct task_struct *t = ha->dpc_thread;
2405
2406                 ha->dpc_thread = NULL;
2407                 kthread_stop(t);
2408         }
2409
2410         qla2x00_free_device(base_vha);
2411
2412         scsi_host_put(base_vha->host);
2413
2414 probe_hw_failed:
2415         if (IS_QLA82XX(ha)) {
2416                 qla82xx_idc_lock(ha);
2417                 qla82xx_clear_drv_active(ha);
2418                 qla82xx_idc_unlock(ha);
2419                 iounmap((device_reg_t __iomem *)ha->nx_pcibase);
2420                 if (!ql2xdbwr)
2421                         iounmap((device_reg_t __iomem *)ha->nxdb_wr_ptr);
2422         } else {
2423                 if (ha->iobase)
2424                         iounmap(ha->iobase);
2425         }
2426         pci_release_selected_regions(ha->pdev, ha->bars);
2427         kfree(ha);
2428         ha = NULL;
2429
2430 probe_out:
2431         pci_disable_device(pdev);
2432         return ret;
2433 }
2434
2435 static void
2436 qla2x00_shutdown(struct pci_dev *pdev)
2437 {
2438         scsi_qla_host_t *vha;
2439         struct qla_hw_data  *ha;
2440
2441         vha = pci_get_drvdata(pdev);
2442         ha = vha->hw;
2443
2444         /* Turn-off FCE trace */
2445         if (ha->flags.fce_enabled) {
2446                 qla2x00_disable_fce_trace(vha, NULL, NULL);
2447                 ha->flags.fce_enabled = 0;
2448         }
2449
2450         /* Turn-off EFT trace */
2451         if (ha->eft)
2452                 qla2x00_disable_eft_trace(vha);
2453
2454         /* Stop currently executing firmware. */
2455         qla2x00_try_to_stop_firmware(vha);
2456
2457         /* Turn adapter off line */
2458         vha->flags.online = 0;
2459
2460         /* turn-off interrupts on the card */
2461         if (ha->interrupts_on) {
2462                 vha->flags.init_done = 0;
2463                 ha->isp_ops->disable_intrs(ha);
2464         }
2465
2466         qla2x00_free_irqs(vha);
2467
2468         qla2x00_free_fw_dump(ha);
2469 }
2470
2471 static void
2472 qla2x00_remove_one(struct pci_dev *pdev)
2473 {
2474         scsi_qla_host_t *base_vha, *vha;
2475         struct qla_hw_data  *ha;
2476         unsigned long flags;
2477
2478         base_vha = pci_get_drvdata(pdev);
2479         ha = base_vha->hw;
2480
2481         mutex_lock(&ha->vport_lock);
2482         while (ha->cur_vport_count) {
2483                 struct Scsi_Host *scsi_host;
2484
2485                 spin_lock_irqsave(&ha->vport_slock, flags);
2486
2487                 BUG_ON(base_vha->list.next == &ha->vp_list);
2488                 /* This assumes first entry in ha->vp_list is always base vha */
2489                 vha = list_first_entry(&base_vha->list, scsi_qla_host_t, list);
2490                 scsi_host = scsi_host_get(vha->host);
2491
2492                 spin_unlock_irqrestore(&ha->vport_slock, flags);
2493                 mutex_unlock(&ha->vport_lock);
2494
2495                 fc_vport_terminate(vha->fc_vport);
2496                 scsi_host_put(vha->host);
2497
2498                 mutex_lock(&ha->vport_lock);
2499         }
2500         mutex_unlock(&ha->vport_lock);
2501
2502         set_bit(UNLOADING, &base_vha->dpc_flags);
2503
2504         qla2x00_abort_all_cmds(base_vha, DID_NO_CONNECT << 16);
2505
2506         qla2x00_dfs_remove(base_vha);
2507
2508         qla84xx_put_chip(base_vha);
2509
2510         /* Disable timer */
2511         if (base_vha->timer_active)
2512                 qla2x00_stop_timer(base_vha);
2513
2514         base_vha->flags.online = 0;
2515
2516         /* Flush the work queue and remove it */
2517         if (ha->wq) {
2518                 flush_workqueue(ha->wq);
2519                 destroy_workqueue(ha->wq);
2520                 ha->wq = NULL;
2521         }
2522
2523         /* Kill the kernel thread for this host */
2524         if (ha->dpc_thread) {
2525                 struct task_struct *t = ha->dpc_thread;
2526
2527                 /*
2528                  * qla2xxx_wake_dpc checks for ->dpc_thread
2529                  * so we need to zero it out.
2530                  */
2531                 ha->dpc_thread = NULL;
2532                 kthread_stop(t);
2533         }
2534
2535         qla2x00_free_sysfs_attr(base_vha);
2536
2537         fc_remove_host(base_vha->host);
2538
2539         scsi_remove_host(base_vha->host);
2540
2541         qla2x00_free_device(base_vha);
2542
2543         scsi_host_put(base_vha->host);
2544
2545         if (IS_QLA82XX(ha)) {
2546                 qla82xx_idc_lock(ha);
2547                 qla82xx_clear_drv_active(ha);
2548                 qla82xx_idc_unlock(ha);
2549
2550                 iounmap((device_reg_t __iomem *)ha->nx_pcibase);
2551                 if (!ql2xdbwr)
2552                         iounmap((device_reg_t __iomem *)ha->nxdb_wr_ptr);
2553         } else {
2554                 if (ha->iobase)
2555                         iounmap(ha->iobase);
2556
2557                 if (ha->mqiobase)
2558                         iounmap(ha->mqiobase);
2559         }
2560
2561         pci_release_selected_regions(ha->pdev, ha->bars);
2562         kfree(ha);
2563         ha = NULL;
2564
2565         pci_disable_pcie_error_reporting(pdev);
2566
2567         pci_disable_device(pdev);
2568         pci_set_drvdata(pdev, NULL);
2569 }
2570
2571 static void
2572 qla2x00_free_device(scsi_qla_host_t *vha)
2573 {
2574         struct qla_hw_data *ha = vha->hw;
2575
2576         qla2x00_abort_all_cmds(vha, DID_NO_CONNECT << 16);
2577
2578         /* Disable timer */
2579         if (vha->timer_active)
2580                 qla2x00_stop_timer(vha);
2581
2582         /* Kill the kernel thread for this host */
2583         if (ha->dpc_thread) {
2584                 struct task_struct *t = ha->dpc_thread;
2585
2586                 /*
2587                  * qla2xxx_wake_dpc checks for ->dpc_thread
2588                  * so we need to zero it out.
2589                  */
2590                 ha->dpc_thread = NULL;
2591                 kthread_stop(t);
2592         }
2593
2594         qla25xx_delete_queues(vha);
2595
2596         if (ha->flags.fce_enabled)
2597                 qla2x00_disable_fce_trace(vha, NULL, NULL);
2598
2599         if (ha->eft)
2600                 qla2x00_disable_eft_trace(vha);
2601
2602         /* Stop currently executing firmware. */
2603         qla2x00_try_to_stop_firmware(vha);
2604
2605         vha->flags.online = 0;
2606
2607         /* turn-off interrupts on the card */
2608         if (ha->interrupts_on) {
2609                 vha->flags.init_done = 0;
2610                 ha->isp_ops->disable_intrs(ha);
2611         }
2612
2613         qla2x00_free_irqs(vha);
2614
2615         qla2x00_free_fcports(vha);
2616
2617         qla2x00_mem_free(ha);
2618
2619         qla82xx_md_free(vha);
2620
2621         qla2x00_free_queues(ha);
2622 }
2623
2624 void qla2x00_free_fcports(struct scsi_qla_host *vha)
2625 {
2626         fc_port_t *fcport, *tfcport;
2627
2628         list_for_each_entry_safe(fcport, tfcport, &vha->vp_fcports, list) {
2629                 list_del(&fcport->list);
2630                 kfree(fcport);
2631                 fcport = NULL;
2632         }
2633 }
2634
2635 static inline void
2636 qla2x00_schedule_rport_del(struct scsi_qla_host *vha, fc_port_t *fcport,
2637     int defer)
2638 {
2639         struct fc_rport *rport;
2640         scsi_qla_host_t *base_vha;
2641         unsigned long flags;
2642
2643         if (!fcport->rport)
2644                 return;
2645
2646         rport = fcport->rport;
2647         if (defer) {
2648                 base_vha = pci_get_drvdata(vha->hw->pdev);
2649                 spin_lock_irqsave(vha->host->host_lock, flags);
2650                 fcport->drport = rport;
2651                 spin_unlock_irqrestore(vha->host->host_lock, flags);
2652                 set_bit(FCPORT_UPDATE_NEEDED, &base_vha->dpc_flags);
2653                 qla2xxx_wake_dpc(base_vha);
2654         } else
2655                 fc_remote_port_delete(rport);
2656 }
2657
2658 /*
2659  * qla2x00_mark_device_lost Updates fcport state when device goes offline.
2660  *
2661  * Input: ha = adapter block pointer.  fcport = port structure pointer.
2662  *
2663  * Return: None.
2664  *
2665  * Context:
2666  */
2667 void qla2x00_mark_device_lost(scsi_qla_host_t *vha, fc_port_t *fcport,
2668     int do_login, int defer)
2669 {
2670         if (atomic_read(&fcport->state) == FCS_ONLINE &&
2671             vha->vp_idx == fcport->vp_idx) {
2672                 qla2x00_set_fcport_state(fcport, FCS_DEVICE_LOST);
2673                 qla2x00_schedule_rport_del(vha, fcport, defer);
2674         }
2675         /*
2676          * We may need to retry the login, so don't change the state of the
2677          * port but do the retries.
2678          */
2679         if (atomic_read(&fcport->state) != FCS_DEVICE_DEAD)
2680                 qla2x00_set_fcport_state(fcport, FCS_DEVICE_LOST);
2681
2682         if (!do_login)
2683                 return;
2684
2685         if (fcport->login_retry == 0) {
2686                 fcport->login_retry = vha->hw->login_retry_count;
2687                 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
2688
2689                 ql_dbg(ql_dbg_disc, vha, 0x2067,
2690                     "Port login retry "
2691                     "%02x%02x%02x%02x%02x%02x%02x%02x, "
2692                     "id = 0x%04x retry cnt=%d.\n",
2693                     fcport->port_name[0], fcport->port_name[1],
2694                     fcport->port_name[2], fcport->port_name[3],
2695                     fcport->port_name[4], fcport->port_name[5],
2696                     fcport->port_name[6], fcport->port_name[7],
2697                     fcport->loop_id, fcport->login_retry);
2698         }
2699 }
2700
2701 /*
2702  * qla2x00_mark_all_devices_lost
2703  *      Updates fcport state when device goes offline.
2704  *
2705  * Input:
2706  *      ha = adapter block pointer.
2707  *      fcport = port structure pointer.
2708  *
2709  * Return:
2710  *      None.
2711  *
2712  * Context:
2713  */
2714 void
2715 qla2x00_mark_all_devices_lost(scsi_qla_host_t *vha, int defer)
2716 {
2717         fc_port_t *fcport;
2718
2719         list_for_each_entry(fcport, &vha->vp_fcports, list) {
2720                 if (vha->vp_idx != 0 && vha->vp_idx != fcport->vp_idx)
2721                         continue;
2722
2723                 /*
2724                  * No point in marking the device as lost, if the device is
2725                  * already DEAD.
2726                  */
2727                 if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD)
2728                         continue;
2729                 if (atomic_read(&fcport->state) == FCS_ONLINE) {
2730                         qla2x00_set_fcport_state(fcport, FCS_DEVICE_LOST);
2731                         if (defer)
2732                                 qla2x00_schedule_rport_del(vha, fcport, defer);
2733                         else if (vha->vp_idx == fcport->vp_idx)
2734                                 qla2x00_schedule_rport_del(vha, fcport, defer);
2735                 }
2736         }
2737 }
2738
2739 /*
2740 * qla2x00_mem_alloc
2741 *      Allocates adapter memory.
2742 *
2743 * Returns:
2744 *      0  = success.
2745 *      !0  = failure.
2746 */
2747 static int
2748 qla2x00_mem_alloc(struct qla_hw_data *ha, uint16_t req_len, uint16_t rsp_len,
2749         struct req_que **req, struct rsp_que **rsp)
2750 {
2751         char    name[16];
2752
2753         ha->init_cb = dma_alloc_coherent(&ha->pdev->dev, ha->init_cb_size,
2754                 &ha->init_cb_dma, GFP_KERNEL);
2755         if (!ha->init_cb)
2756                 goto fail;
2757
2758         ha->gid_list = dma_alloc_coherent(&ha->pdev->dev, GID_LIST_SIZE,
2759                 &ha->gid_list_dma, GFP_KERNEL);
2760         if (!ha->gid_list)
2761                 goto fail_free_init_cb;
2762
2763         ha->srb_mempool = mempool_create_slab_pool(SRB_MIN_REQ, srb_cachep);
2764         if (!ha->srb_mempool)
2765                 goto fail_free_gid_list;
2766
2767         if (IS_QLA82XX(ha)) {
2768                 /* Allocate cache for CT6 Ctx. */
2769                 if (!ctx_cachep) {
2770                         ctx_cachep = kmem_cache_create("qla2xxx_ctx",
2771                                 sizeof(struct ct6_dsd), 0,
2772                                 SLAB_HWCACHE_ALIGN, NULL);
2773                         if (!ctx_cachep)
2774                                 goto fail_free_gid_list;
2775                 }
2776                 ha->ctx_mempool = mempool_create_slab_pool(SRB_MIN_REQ,
2777                         ctx_cachep);
2778                 if (!ha->ctx_mempool)
2779                         goto fail_free_srb_mempool;
2780                 ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0021,
2781                     "ctx_cachep=%p ctx_mempool=%p.\n",
2782                     ctx_cachep, ha->ctx_mempool);
2783         }
2784
2785         /* Get memory for cached NVRAM */
2786         ha->nvram = kzalloc(MAX_NVRAM_SIZE, GFP_KERNEL);
2787         if (!ha->nvram)
2788                 goto fail_free_ctx_mempool;
2789
2790         snprintf(name, sizeof(name), "%s_%d", QLA2XXX_DRIVER_NAME,
2791                 ha->pdev->device);
2792         ha->s_dma_pool = dma_pool_create(name, &ha->pdev->dev,
2793                 DMA_POOL_SIZE, 8, 0);
2794         if (!ha->s_dma_pool)
2795                 goto fail_free_nvram;
2796
2797         ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0022,
2798             "init_cb=%p gid_list=%p, srb_mempool=%p s_dma_pool=%p.\n",
2799             ha->init_cb, ha->gid_list, ha->srb_mempool, ha->s_dma_pool);
2800
2801         if (IS_QLA82XX(ha) || ql2xenabledif) {
2802                 ha->dl_dma_pool = dma_pool_create(name, &ha->pdev->dev,
2803                         DSD_LIST_DMA_POOL_SIZE, 8, 0);
2804                 if (!ha->dl_dma_pool) {
2805                         ql_log_pci(ql_log_fatal, ha->pdev, 0x0023,
2806                             "Failed to allocate memory for dl_dma_pool.\n");
2807                         goto fail_s_dma_pool;
2808                 }
2809
2810                 ha->fcp_cmnd_dma_pool = dma_pool_create(name, &ha->pdev->dev,
2811                         FCP_CMND_DMA_POOL_SIZE, 8, 0);
2812                 if (!ha->fcp_cmnd_dma_pool) {
2813                         ql_log_pci(ql_log_fatal, ha->pdev, 0x0024,
2814                             "Failed to allocate memory for fcp_cmnd_dma_pool.\n");
2815                         goto fail_dl_dma_pool;
2816                 }
2817                 ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0025,
2818                     "dl_dma_pool=%p fcp_cmnd_dma_pool=%p.\n",
2819                     ha->dl_dma_pool, ha->fcp_cmnd_dma_pool);
2820         }
2821
2822         /* Allocate memory for SNS commands */
2823         if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
2824         /* Get consistent memory allocated for SNS commands */
2825                 ha->sns_cmd = dma_alloc_coherent(&ha->pdev->dev,
2826                 sizeof(struct sns_cmd_pkt), &ha->sns_cmd_dma, GFP_KERNEL);
2827                 if (!ha->sns_cmd)
2828                         goto fail_dma_pool;
2829                 ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0026,
2830                     "sns_cmd: %p.\n", ha->sns_cmd);
2831         } else {
2832         /* Get consistent memory allocated for MS IOCB */
2833                 ha->ms_iocb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
2834                         &ha->ms_iocb_dma);
2835                 if (!ha->ms_iocb)
2836                         goto fail_dma_pool;
2837         /* Get consistent memory allocated for CT SNS commands */
2838                 ha->ct_sns = dma_alloc_coherent(&ha->pdev->dev,
2839                         sizeof(struct ct_sns_pkt), &ha->ct_sns_dma, GFP_KERNEL);
2840                 if (!ha->ct_sns)
2841                         goto fail_free_ms_iocb;
2842                 ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0027,
2843                     "ms_iocb=%p ct_sns=%p.\n",
2844                     ha->ms_iocb, ha->ct_sns);
2845         }
2846
2847         /* Allocate memory for request ring */
2848         *req = kzalloc(sizeof(struct req_que), GFP_KERNEL);
2849         if (!*req) {
2850                 ql_log_pci(ql_log_fatal, ha->pdev, 0x0028,
2851                     "Failed to allocate memory for req.\n");
2852                 goto fail_req;
2853         }
2854         (*req)->length = req_len;
2855         (*req)->ring = dma_alloc_coherent(&ha->pdev->dev,
2856                 ((*req)->length + 1) * sizeof(request_t),
2857                 &(*req)->dma, GFP_KERNEL);
2858         if (!(*req)->ring) {
2859                 ql_log_pci(ql_log_fatal, ha->pdev, 0x0029,
2860                     "Failed to allocate memory for req_ring.\n");
2861                 goto fail_req_ring;
2862         }
2863         /* Allocate memory for response ring */
2864         *rsp = kzalloc(sizeof(struct rsp_que), GFP_KERNEL);
2865         if (!*rsp) {
2866                 ql_log_pci(ql_log_fatal, ha->pdev, 0x002a,
2867                     "Failed to allocate memory for rsp.\n");
2868                 goto fail_rsp;
2869         }
2870         (*rsp)->hw = ha;
2871         (*rsp)->length = rsp_len;
2872         (*rsp)->ring = dma_alloc_coherent(&ha->pdev->dev,
2873                 ((*rsp)->length + 1) * sizeof(response_t),
2874                 &(*rsp)->dma, GFP_KERNEL);
2875         if (!(*rsp)->ring) {
2876                 ql_log_pci(ql_log_fatal, ha->pdev, 0x002b,
2877                     "Failed to allocate memory for rsp_ring.\n");
2878                 goto fail_rsp_ring;
2879         }
2880         (*req)->rsp = *rsp;
2881         (*rsp)->req = *req;
2882         ql_dbg_pci(ql_dbg_init, ha->pdev, 0x002c,
2883             "req=%p req->length=%d req->ring=%p rsp=%p "
2884             "rsp->length=%d rsp->ring=%p.\n",
2885             *req, (*req)->length, (*req)->ring, *rsp, (*rsp)->length,
2886             (*rsp)->ring);
2887         /* Allocate memory for NVRAM data for vports */
2888         if (ha->nvram_npiv_size) {
2889                 ha->npiv_info = kzalloc(sizeof(struct qla_npiv_entry) *
2890                     ha->nvram_npiv_size, GFP_KERNEL);
2891                 if (!ha->npiv_info) {
2892                         ql_log_pci(ql_log_fatal, ha->pdev, 0x002d,
2893                             "Failed to allocate memory for npiv_info.\n");
2894                         goto fail_npiv_info;
2895                 }
2896         } else
2897                 ha->npiv_info = NULL;
2898
2899         /* Get consistent memory allocated for EX-INIT-CB. */
2900         if (IS_QLA8XXX_TYPE(ha)) {
2901                 ha->ex_init_cb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
2902                     &ha->ex_init_cb_dma);
2903                 if (!ha->ex_init_cb)
2904                         goto fail_ex_init_cb;
2905                 ql_dbg_pci(ql_dbg_init, ha->pdev, 0x002e,
2906                     "ex_init_cb=%p.\n", ha->ex_init_cb);
2907         }
2908
2909         INIT_LIST_HEAD(&ha->gbl_dsd_list);
2910
2911         /* Get consistent memory allocated for Async Port-Database. */
2912         if (!IS_FWI2_CAPABLE(ha)) {
2913                 ha->async_pd = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
2914                         &ha->async_pd_dma);
2915                 if (!ha->async_pd)
2916                         goto fail_async_pd;
2917                 ql_dbg_pci(ql_dbg_init, ha->pdev, 0x002f,
2918                     "async_pd=%p.\n", ha->async_pd);
2919         }
2920
2921         INIT_LIST_HEAD(&ha->vp_list);
2922         return 1;
2923
2924 fail_async_pd:
2925         dma_pool_free(ha->s_dma_pool, ha->ex_init_cb, ha->ex_init_cb_dma);
2926 fail_ex_init_cb:
2927         kfree(ha->npiv_info);
2928 fail_npiv_info:
2929         dma_free_coherent(&ha->pdev->dev, ((*rsp)->length + 1) *
2930                 sizeof(response_t), (*rsp)->ring, (*rsp)->dma);
2931         (*rsp)->ring = NULL;
2932         (*rsp)->dma = 0;
2933 fail_rsp_ring:
2934         kfree(*rsp);
2935 fail_rsp:
2936         dma_free_coherent(&ha->pdev->dev, ((*req)->length + 1) *
2937                 sizeof(request_t), (*req)->ring, (*req)->dma);
2938         (*req)->ring = NULL;
2939         (*req)->dma = 0;
2940 fail_req_ring:
2941         kfree(*req);
2942 fail_req:
2943         dma_free_coherent(&ha->pdev->dev, sizeof(struct ct_sns_pkt),
2944                 ha->ct_sns, ha->ct_sns_dma);
2945         ha->ct_sns = NULL;
2946         ha->ct_sns_dma = 0;
2947 fail_free_ms_iocb:
2948         dma_pool_free(ha->s_dma_pool, ha->ms_iocb, ha->ms_iocb_dma);
2949         ha->ms_iocb = NULL;
2950         ha->ms_iocb_dma = 0;
2951 fail_dma_pool:
2952         if (IS_QLA82XX(ha) || ql2xenabledif) {
2953                 dma_pool_destroy(ha->fcp_cmnd_dma_pool);
2954                 ha->fcp_cmnd_dma_pool = NULL;
2955         }
2956 fail_dl_dma_pool:
2957         if (IS_QLA82XX(ha) || ql2xenabledif) {
2958                 dma_pool_destroy(ha->dl_dma_pool);
2959                 ha->dl_dma_pool = NULL;
2960         }
2961 fail_s_dma_pool:
2962         dma_pool_destroy(ha->s_dma_pool);
2963         ha->s_dma_pool = NULL;
2964 fail_free_nvram:
2965         kfree(ha->nvram);
2966         ha->nvram = NULL;
2967 fail_free_ctx_mempool:
2968         mempool_destroy(ha->ctx_mempool);
2969         ha->ctx_mempool = NULL;
2970 fail_free_srb_mempool:
2971         mempool_destroy(ha->srb_mempool);
2972         ha->srb_mempool = NULL;
2973 fail_free_gid_list:
2974         dma_free_coherent(&ha->pdev->dev, GID_LIST_SIZE, ha->gid_list,
2975         ha->gid_list_dma);
2976         ha->gid_list = NULL;
2977         ha->gid_list_dma = 0;
2978 fail_free_init_cb:
2979         dma_free_coherent(&ha->pdev->dev, ha->init_cb_size, ha->init_cb,
2980         ha->init_cb_dma);
2981         ha->init_cb = NULL;
2982         ha->init_cb_dma = 0;
2983 fail:
2984         ql_log(ql_log_fatal, NULL, 0x0030,
2985             "Memory allocation failure.\n");
2986         return -ENOMEM;
2987 }
2988
2989 /*
2990 * qla2x00_free_fw_dump
2991 *       Frees fw dump stuff.
2992 *
2993 * Input:
2994 *       ha = adapter block pointer.
2995 */
2996 static void
2997 qla2x00_free_fw_dump(struct qla_hw_data *ha)
2998 {
2999         if (ha->fce)
3000                 dma_free_coherent(&ha->pdev->dev, FCE_SIZE, ha->fce,
3001                     ha->fce_dma);
3002
3003         if (ha->fw_dump) {
3004                 if (ha->eft)
3005                         dma_free_coherent(&ha->pdev->dev,
3006                             ntohl(ha->fw_dump->eft_size), ha->eft, ha->eft_dma);
3007                 vfree(ha->fw_dump);
3008         }
3009         ha->fce = NULL;
3010         ha->fce_dma = 0;
3011         ha->eft = NULL;
3012         ha->eft_dma = 0;
3013         ha->fw_dump = NULL;
3014         ha->fw_dumped = 0;
3015         ha->fw_dump_reading = 0;
3016 }
3017
3018 /*
3019 * qla2x00_mem_free
3020 *      Frees all adapter allocated memory.
3021 *
3022 * Input:
3023 *      ha = adapter block pointer.
3024 */
3025 static void
3026 qla2x00_mem_free(struct qla_hw_data *ha)
3027 {
3028         qla2x00_free_fw_dump(ha);
3029
3030         if (ha->srb_mempool)
3031                 mempool_destroy(ha->srb_mempool);
3032
3033         if (ha->dcbx_tlv)
3034                 dma_free_coherent(&ha->pdev->dev, DCBX_TLV_DATA_SIZE,
3035                     ha->dcbx_tlv, ha->dcbx_tlv_dma);
3036
3037         if (ha->xgmac_data)
3038                 dma_free_coherent(&ha->pdev->dev, XGMAC_DATA_SIZE,
3039                     ha->xgmac_data, ha->xgmac_data_dma);
3040
3041         if (ha->sns_cmd)
3042                 dma_free_coherent(&ha->pdev->dev, sizeof(struct sns_cmd_pkt),
3043                 ha->sns_cmd, ha->sns_cmd_dma);
3044
3045         if (ha->ct_sns)
3046                 dma_free_coherent(&ha->pdev->dev, sizeof(struct ct_sns_pkt),
3047                 ha->ct_sns, ha->ct_sns_dma);
3048
3049         if (ha->sfp_data)
3050                 dma_pool_free(ha->s_dma_pool, ha->sfp_data, ha->sfp_data_dma);
3051
3052         if (ha->edc_data)
3053                 dma_pool_free(ha->s_dma_pool, ha->edc_data, ha->edc_data_dma);
3054
3055         if (ha->ms_iocb)
3056                 dma_pool_free(ha->s_dma_pool, ha->ms_iocb, ha->ms_iocb_dma);
3057
3058         if (ha->ex_init_cb)
3059                 dma_pool_free(ha->s_dma_pool,
3060                         ha->ex_init_cb, ha->ex_init_cb_dma);
3061
3062         if (ha->async_pd)
3063                 dma_pool_free(ha->s_dma_pool, ha->async_pd, ha->async_pd_dma);
3064
3065         if (ha->s_dma_pool)
3066                 dma_pool_destroy(ha->s_dma_pool);
3067
3068         if (ha->gid_list)
3069                 dma_free_coherent(&ha->pdev->dev, GID_LIST_SIZE, ha->gid_list,
3070                 ha->gid_list_dma);
3071
3072         if (IS_QLA82XX(ha)) {
3073                 if (!list_empty(&ha->gbl_dsd_list)) {
3074                         struct dsd_dma *dsd_ptr, *tdsd_ptr;
3075
3076                         /* clean up allocated prev pool */
3077                         list_for_each_entry_safe(dsd_ptr,
3078                                 tdsd_ptr, &ha->gbl_dsd_list, list) {
3079                                 dma_pool_free(ha->dl_dma_pool,
3080                                 dsd_ptr->dsd_addr, dsd_ptr->dsd_list_dma);
3081                                 list_del(&dsd_ptr->list);
3082                                 kfree(dsd_ptr);
3083                         }
3084                 }
3085         }
3086
3087         if (ha->dl_dma_pool)
3088                 dma_pool_destroy(ha->dl_dma_pool);
3089
3090         if (ha->fcp_cmnd_dma_pool)
3091                 dma_pool_destroy(ha->fcp_cmnd_dma_pool);
3092
3093         if (ha->ctx_mempool)
3094                 mempool_destroy(ha->ctx_mempool);
3095
3096         if (ha->init_cb)
3097                 dma_free_coherent(&ha->pdev->dev, ha->init_cb_size,
3098                         ha->init_cb, ha->init_cb_dma);
3099         vfree(ha->optrom_buffer);
3100         kfree(ha->nvram);
3101         kfree(ha->npiv_info);
3102
3103         ha->srb_mempool = NULL;
3104         ha->ctx_mempool = NULL;
3105         ha->sns_cmd = NULL;
3106         ha->sns_cmd_dma = 0;
3107         ha->ct_sns = NULL;
3108         ha->ct_sns_dma = 0;
3109         ha->ms_iocb = NULL;
3110         ha->ms_iocb_dma = 0;
3111         ha->init_cb = NULL;
3112         ha->init_cb_dma = 0;
3113         ha->ex_init_cb = NULL;
3114         ha->ex_init_cb_dma = 0;
3115         ha->async_pd = NULL;
3116         ha->async_pd_dma = 0;
3117
3118         ha->s_dma_pool = NULL;
3119         ha->dl_dma_pool = NULL;
3120         ha->fcp_cmnd_dma_pool = NULL;
3121
3122         ha->gid_list = NULL;
3123         ha->gid_list_dma = 0;
3124 }
3125
3126 struct scsi_qla_host *qla2x00_create_host(struct scsi_host_template *sht,
3127                                                 struct qla_hw_data *ha)
3128 {
3129         struct Scsi_Host *host;
3130         struct scsi_qla_host *vha = NULL;
3131
3132         host = scsi_host_alloc(sht, sizeof(scsi_qla_host_t));
3133         if (host == NULL) {
3134                 ql_log_pci(ql_log_fatal, ha->pdev, 0x0107,
3135                     "Failed to allocate host from the scsi layer, aborting.\n");
3136                 goto fail;
3137         }
3138
3139         /* Clear our data area */
3140         vha = shost_priv(host);
3141         memset(vha, 0, sizeof(scsi_qla_host_t));
3142
3143         vha->host = host;
3144         vha->host_no = host->host_no;
3145         vha->hw = ha;
3146
3147         INIT_LIST_HEAD(&vha->vp_fcports);
3148         INIT_LIST_HEAD(&vha->work_list);
3149         INIT_LIST_HEAD(&vha->list);
3150
3151         spin_lock_init(&vha->work_lock);
3152
3153         sprintf(vha->host_str, "%s_%ld", QLA2XXX_DRIVER_NAME, vha->host_no);
3154         ql_dbg(ql_dbg_init, vha, 0x0041,
3155             "Allocated the host=%p hw=%p vha=%p dev_name=%s",
3156             vha->host, vha->hw, vha,
3157             dev_name(&(ha->pdev->dev)));
3158
3159         return vha;
3160
3161 fail:
3162         return vha;
3163 }
3164
3165 static struct qla_work_evt *
3166 qla2x00_alloc_work(struct scsi_qla_host *vha, enum qla_work_type type)
3167 {
3168         struct qla_work_evt *e;
3169         uint8_t bail;
3170
3171         QLA_VHA_MARK_BUSY(vha, bail);
3172         if (bail)
3173                 return NULL;
3174
3175         e = kzalloc(sizeof(struct qla_work_evt), GFP_ATOMIC);
3176         if (!e) {
3177                 QLA_VHA_MARK_NOT_BUSY(vha);
3178                 return NULL;
3179         }
3180
3181         INIT_LIST_HEAD(&e->list);
3182         e->type = type;
3183         e->flags = QLA_EVT_FLAG_FREE;
3184         return e;
3185 }
3186
3187 static int
3188 qla2x00_post_work(struct scsi_qla_host *vha, struct qla_work_evt *e)
3189 {
3190         unsigned long flags;
3191
3192         spin_lock_irqsave(&vha->work_lock, flags);
3193         list_add_tail(&e->list, &vha->work_list);
3194         spin_unlock_irqrestore(&vha->work_lock, flags);
3195         qla2xxx_wake_dpc(vha);
3196
3197         return QLA_SUCCESS;
3198 }
3199
3200 int
3201 qla2x00_post_aen_work(struct scsi_qla_host *vha, enum fc_host_event_code code,
3202     u32 data)
3203 {
3204         struct qla_work_evt *e;
3205
3206         e = qla2x00_alloc_work(vha, QLA_EVT_AEN);
3207         if (!e)
3208                 return QLA_FUNCTION_FAILED;
3209
3210         e->u.aen.code = code;
3211         e->u.aen.data = data;
3212         return qla2x00_post_work(vha, e);
3213 }
3214
3215 int
3216 qla2x00_post_idc_ack_work(struct scsi_qla_host *vha, uint16_t *mb)
3217 {
3218         struct qla_work_evt *e;
3219
3220         e = qla2x00_alloc_work(vha, QLA_EVT_IDC_ACK);
3221         if (!e)
3222                 return QLA_FUNCTION_FAILED;
3223
3224         memcpy(e->u.idc_ack.mb, mb, QLA_IDC_ACK_REGS * sizeof(uint16_t));
3225         return qla2x00_post_work(vha, e);
3226 }
3227
3228 #define qla2x00_post_async_work(name, type)     \
3229 int qla2x00_post_async_##name##_work(           \
3230     struct scsi_qla_host *vha,                  \
3231     fc_port_t *fcport, uint16_t *data)          \
3232 {                                               \
3233         struct qla_work_evt *e;                 \
3234                                                 \
3235         e = qla2x00_alloc_work(vha, type);      \
3236         if (!e)                                 \
3237                 return QLA_FUNCTION_FAILED;     \
3238                                                 \
3239         e->u.logio.fcport = fcport;             \
3240         if (data) {                             \
3241                 e->u.logio.data[0] = data[0];   \
3242                 e->u.logio.data[1] = data[1];   \
3243         }                                       \
3244         return qla2x00_post_work(vha, e);       \
3245 }
3246
3247 qla2x00_post_async_work(login, QLA_EVT_ASYNC_LOGIN);
3248 qla2x00_post_async_work(login_done, QLA_EVT_ASYNC_LOGIN_DONE);
3249 qla2x00_post_async_work(logout, QLA_EVT_ASYNC_LOGOUT);
3250 qla2x00_post_async_work(logout_done, QLA_EVT_ASYNC_LOGOUT_DONE);
3251 qla2x00_post_async_work(adisc, QLA_EVT_ASYNC_ADISC);
3252 qla2x00_post_async_work(adisc_done, QLA_EVT_ASYNC_ADISC_DONE);
3253
3254 int
3255 qla2x00_post_uevent_work(struct scsi_qla_host *vha, u32 code)
3256 {
3257         struct qla_work_evt *e;
3258
3259         e = qla2x00_alloc_work(vha, QLA_EVT_UEVENT);
3260         if (!e)
3261                 return QLA_FUNCTION_FAILED;
3262
3263         e->u.uevent.code = code;
3264         return qla2x00_post_work(vha, e);
3265 }
3266
3267 static void
3268 qla2x00_uevent_emit(struct scsi_qla_host *vha, u32 code)
3269 {
3270         char event_string[40];
3271         char *envp[] = { event_string, NULL };
3272
3273         switch (code) {
3274         case QLA_UEVENT_CODE_FW_DUMP:
3275                 snprintf(event_string, sizeof(event_string), "FW_DUMP=%ld",
3276                     vha->host_no);
3277                 break;
3278         default:
3279                 /* do nothing */
3280                 break;
3281         }
3282         kobject_uevent_env(&vha->hw->pdev->dev.kobj, KOBJ_CHANGE, envp);
3283 }
3284
3285 void
3286 qla2x00_do_work(struct scsi_qla_host *vha)
3287 {
3288         struct qla_work_evt *e, *tmp;
3289         unsigned long flags;
3290         LIST_HEAD(work);
3291
3292         spin_lock_irqsave(&vha->work_lock, flags);
3293         list_splice_init(&vha->work_list, &work);
3294         spin_unlock_irqrestore(&vha->work_lock, flags);
3295
3296         list_for_each_entry_safe(e, tmp, &work, list) {
3297                 list_del_init(&e->list);
3298
3299                 switch (e->type) {
3300                 case QLA_EVT_AEN:
3301                         fc_host_post_event(vha->host, fc_get_event_number(),
3302                             e->u.aen.code, e->u.aen.data);
3303                         break;
3304                 case QLA_EVT_IDC_ACK:
3305                         qla81xx_idc_ack(vha, e->u.idc_ack.mb);
3306                         break;
3307                 case QLA_EVT_ASYNC_LOGIN:
3308                         qla2x00_async_login(vha, e->u.logio.fcport,
3309                             e->u.logio.data);
3310                         break;
3311                 case QLA_EVT_ASYNC_LOGIN_DONE:
3312                         qla2x00_async_login_done(vha, e->u.logio.fcport,
3313                             e->u.logio.data);
3314                         break;
3315                 case QLA_EVT_ASYNC_LOGOUT:
3316                         qla2x00_async_logout(vha, e->u.logio.fcport);
3317                         break;
3318                 case QLA_EVT_ASYNC_LOGOUT_DONE:
3319                         qla2x00_async_logout_done(vha, e->u.logio.fcport,
3320                             e->u.logio.data);
3321                         break;
3322                 case QLA_EVT_ASYNC_ADISC:
3323                         qla2x00_async_adisc(vha, e->u.logio.fcport,
3324                             e->u.logio.data);
3325                         break;
3326                 case QLA_EVT_ASYNC_ADISC_DONE:
3327                         qla2x00_async_adisc_done(vha, e->u.logio.fcport,
3328                             e->u.logio.data);
3329                         break;
3330                 case QLA_EVT_UEVENT:
3331                         qla2x00_uevent_emit(vha, e->u.uevent.code);
3332                         break;
3333                 }
3334                 if (e->flags & QLA_EVT_FLAG_FREE)
3335                         kfree(e);
3336
3337                 /* For each work completed decrement vha ref count */
3338                 QLA_VHA_MARK_NOT_BUSY(vha);
3339         }
3340 }
3341
3342 /* Relogins all the fcports of a vport
3343  * Context: dpc thread
3344  */
3345 void qla2x00_relogin(struct scsi_qla_host *vha)
3346 {
3347         fc_port_t       *fcport;
3348         int status;
3349         uint16_t        next_loopid = 0;
3350         struct qla_hw_data *ha = vha->hw;
3351         uint16_t data[2];
3352
3353         list_for_each_entry(fcport, &vha->vp_fcports, list) {
3354         /*
3355          * If the port is not ONLINE then try to login
3356          * to it if we haven't run out of retries.
3357          */
3358                 if (atomic_read(&fcport->state) != FCS_ONLINE &&
3359                     fcport->login_retry && !(fcport->flags & FCF_ASYNC_SENT)) {
3360                         fcport->login_retry--;
3361                         if (fcport->flags & FCF_FABRIC_DEVICE) {
3362                                 if (fcport->flags & FCF_FCP2_DEVICE)
3363                                         ha->isp_ops->fabric_logout(vha,
3364                                                         fcport->loop_id,
3365                                                         fcport->d_id.b.domain,
3366                                                         fcport->d_id.b.area,
3367                                                         fcport->d_id.b.al_pa);
3368
3369                                 if (fcport->loop_id == FC_NO_LOOP_ID) {
3370                                         fcport->loop_id = next_loopid =
3371                                             ha->min_external_loopid;
3372                                         status = qla2x00_find_new_loop_id(
3373                                             vha, fcport);
3374                                         if (status != QLA_SUCCESS) {
3375                                                 /* Ran out of IDs to use */
3376                                                 break;
3377                                         }
3378                                 }
3379
3380                                 if (IS_ALOGIO_CAPABLE(ha)) {
3381                                         fcport->flags |= FCF_ASYNC_SENT;
3382                                         data[0] = 0;
3383                                         data[1] = QLA_LOGIO_LOGIN_RETRIED;
3384                                         status = qla2x00_post_async_login_work(
3385                                             vha, fcport, data);
3386                                         if (status == QLA_SUCCESS)
3387                                                 continue;
3388                                         /* Attempt a retry. */
3389                                         status = 1;
3390                                 } else
3391                                         status = qla2x00_fabric_login(vha,
3392                                             fcport, &next_loopid);
3393                         } else
3394                                 status = qla2x00_local_device_login(vha,
3395                                                                 fcport);
3396
3397                         if (status == QLA_SUCCESS) {
3398                                 fcport->old_loop_id = fcport->loop_id;
3399
3400                                 ql_dbg(ql_dbg_disc, vha, 0x2003,
3401                                     "Port login OK: logged in ID 0x%x.\n",
3402                                     fcport->loop_id);
3403
3404                                 qla2x00_update_fcport(vha, fcport);
3405
3406                         } else if (status == 1) {
3407                                 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
3408                                 /* retry the login again */
3409                                 ql_dbg(ql_dbg_disc, vha, 0x2007,
3410                                     "Retrying %d login again loop_id 0x%x.\n",
3411                                     fcport->login_retry, fcport->loop_id);
3412                         } else {
3413                                 fcport->login_retry = 0;
3414                         }
3415
3416                         if (fcport->login_retry == 0 && status != QLA_SUCCESS)
3417                                 fcport->loop_id = FC_NO_LOOP_ID;
3418                 }
3419                 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
3420                         break;
3421         }
3422 }
3423
3424 /**************************************************************************
3425 * qla2x00_do_dpc
3426 *   This kernel thread is a task that is schedule by the interrupt handler
3427 *   to perform the background processing for interrupts.
3428 *
3429 * Notes:
3430 * This task always run in the context of a kernel thread.  It
3431 * is kick-off by the driver's detect code and starts up
3432 * up one per adapter. It immediately goes to sleep and waits for
3433 * some fibre event.  When either the interrupt handler or
3434 * the timer routine detects a event it will one of the task
3435 * bits then wake us up.
3436 **************************************************************************/
3437 static int
3438 qla2x00_do_dpc(void *data)
3439 {
3440         int             rval;
3441         scsi_qla_host_t *base_vha;
3442         struct qla_hw_data *ha;
3443
3444         ha = (struct qla_hw_data *)data;
3445         base_vha = pci_get_drvdata(ha->pdev);
3446
3447         set_user_nice(current, -20);
3448
3449         set_current_state(TASK_INTERRUPTIBLE);
3450         while (!kthread_should_stop()) {
3451                 ql_dbg(ql_dbg_dpc, base_vha, 0x4000,
3452                     "DPC handler sleeping.\n");
3453
3454                 schedule();
3455                 __set_current_state(TASK_RUNNING);
3456
3457                 if (!base_vha->flags.init_done || ha->flags.mbox_busy)
3458                         goto end_loop;
3459
3460                 if (ha->flags.eeh_busy) {
3461                         ql_dbg(ql_dbg_dpc, base_vha, 0x4003,
3462                             "eeh_busy=%d.\n", ha->flags.eeh_busy);
3463                         goto end_loop;
3464                 }
3465
3466                 ha->dpc_active = 1;
3467
3468                 ql_dbg(ql_dbg_dpc, base_vha, 0x4001,
3469                     "DPC handler waking up.\n");
3470                 ql_dbg(ql_dbg_dpc, base_vha, 0x4002,
3471                     "dpc_flags=0x%lx.\n", base_vha->dpc_flags);
3472
3473                 qla2x00_do_work(base_vha);
3474
3475                 if (IS_QLA82XX(ha)) {
3476                         if (test_and_clear_bit(ISP_UNRECOVERABLE,
3477                                 &base_vha->dpc_flags)) {
3478                                 qla82xx_idc_lock(ha);
3479                                 qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
3480                                         QLA82XX_DEV_FAILED);
3481                                 qla82xx_idc_unlock(ha);
3482                                 ql_log(ql_log_info, base_vha, 0x4004,
3483                                     "HW State: FAILED.\n");
3484                                 qla82xx_device_state_handler(base_vha);
3485                                 continue;
3486                         }
3487
3488                         if (test_and_clear_bit(FCOE_CTX_RESET_NEEDED,
3489                                 &base_vha->dpc_flags)) {
3490
3491                                 ql_dbg(ql_dbg_dpc, base_vha, 0x4005,
3492                                     "FCoE context reset scheduled.\n");
3493                                 if (!(test_and_set_bit(ABORT_ISP_ACTIVE,
3494                                         &base_vha->dpc_flags))) {
3495                                         if (qla82xx_fcoe_ctx_reset(base_vha)) {
3496                                                 /* FCoE-ctx reset failed.
3497                                                  * Escalate to chip-reset
3498                                                  */
3499                                                 set_bit(ISP_ABORT_NEEDED,
3500                                                         &base_vha->dpc_flags);
3501                                         }
3502                                         clear_bit(ABORT_ISP_ACTIVE,
3503                                                 &base_vha->dpc_flags);
3504                                 }
3505
3506                                 ql_dbg(ql_dbg_dpc, base_vha, 0x4006,
3507                                     "FCoE context reset end.\n");
3508                         }
3509                 }
3510
3511                 if (test_and_clear_bit(ISP_ABORT_NEEDED,
3512                                                 &base_vha->dpc_flags)) {
3513
3514                         ql_dbg(ql_dbg_dpc, base_vha, 0x4007,
3515                             "ISP abort scheduled.\n");
3516                         if (!(test_and_set_bit(ABORT_ISP_ACTIVE,
3517                             &base_vha->dpc_flags))) {
3518
3519                                 if (ha->isp_ops->abort_isp(base_vha)) {
3520                                         /* failed. retry later */
3521                                         set_bit(ISP_ABORT_NEEDED,
3522                                             &base_vha->dpc_flags);
3523                                 }
3524                                 clear_bit(ABORT_ISP_ACTIVE,
3525                                                 &base_vha->dpc_flags);
3526                         }
3527
3528                         ql_dbg(ql_dbg_dpc, base_vha, 0x4008,
3529                             "ISP abort end.\n");
3530                 }
3531
3532                 if (test_bit(FCPORT_UPDATE_NEEDED, &base_vha->dpc_flags)) {
3533                         qla2x00_update_fcports(base_vha);
3534                         clear_bit(FCPORT_UPDATE_NEEDED, &base_vha->dpc_flags);
3535                 }
3536
3537                 if (test_bit(ISP_QUIESCE_NEEDED, &base_vha->dpc_flags)) {
3538                         ql_dbg(ql_dbg_dpc, base_vha, 0x4009,
3539                             "Quiescence mode scheduled.\n");
3540                         qla82xx_device_state_handler(base_vha);
3541                         clear_bit(ISP_QUIESCE_NEEDED, &base_vha->dpc_flags);
3542                         if (!ha->flags.quiesce_owner) {
3543                                 qla2x00_perform_loop_resync(base_vha);
3544
3545                                 qla82xx_idc_lock(ha);
3546                                 qla82xx_clear_qsnt_ready(base_vha);
3547                                 qla82xx_idc_unlock(ha);
3548                         }
3549                         ql_dbg(ql_dbg_dpc, base_vha, 0x400a,
3550                             "Quiescence mode end.\n");
3551                 }
3552
3553                 if (test_and_clear_bit(RESET_MARKER_NEEDED,
3554                                                         &base_vha->dpc_flags) &&
3555                     (!(test_and_set_bit(RESET_ACTIVE, &base_vha->dpc_flags)))) {
3556
3557                         ql_dbg(ql_dbg_dpc, base_vha, 0x400b,
3558                             "Reset marker scheduled.\n");
3559                         qla2x00_rst_aen(base_vha);
3560                         clear_bit(RESET_ACTIVE, &base_vha->dpc_flags);
3561                         ql_dbg(ql_dbg_dpc, base_vha, 0x400c,
3562                             "Reset marker end.\n");
3563                 }
3564
3565                 /* Retry each device up to login retry count */
3566                 if ((test_and_clear_bit(RELOGIN_NEEDED,
3567                                                 &base_vha->dpc_flags)) &&
3568                     !test_bit(LOOP_RESYNC_NEEDED, &base_vha->dpc_flags) &&
3569                     atomic_read(&base_vha->loop_state) != LOOP_DOWN) {
3570
3571                         ql_dbg(ql_dbg_dpc, base_vha, 0x400d,
3572                             "Relogin scheduled.\n");
3573                         qla2x00_relogin(base_vha);
3574                         ql_dbg(ql_dbg_dpc, base_vha, 0x400e,
3575                             "Relogin end.\n");
3576                 }
3577
3578                 if (test_and_clear_bit(LOOP_RESYNC_NEEDED,
3579                                                         &base_vha->dpc_flags)) {
3580
3581                         ql_dbg(ql_dbg_dpc, base_vha, 0x400f,
3582                             "Loop resync scheduled.\n");
3583
3584                         if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE,
3585                             &base_vha->dpc_flags))) {
3586
3587                                 rval = qla2x00_loop_resync(base_vha);
3588
3589                                 clear_bit(LOOP_RESYNC_ACTIVE,
3590                                                 &base_vha->dpc_flags);
3591                         }
3592
3593                         ql_dbg(ql_dbg_dpc, base_vha, 0x4010,
3594                             "Loop resync end.\n");
3595                 }
3596
3597                 if (test_bit(NPIV_CONFIG_NEEDED, &base_vha->dpc_flags) &&
3598                     atomic_read(&base_vha->loop_state) == LOOP_READY) {
3599                         clear_bit(NPIV_CONFIG_NEEDED, &base_vha->dpc_flags);
3600                         qla2xxx_flash_npiv_conf(base_vha);
3601                 }
3602
3603                 if (!ha->interrupts_on)
3604                         ha->isp_ops->enable_intrs(ha);
3605
3606                 if (test_and_clear_bit(BEACON_BLINK_NEEDED,
3607                                         &base_vha->dpc_flags))
3608                         ha->isp_ops->beacon_blink(base_vha);
3609
3610                 qla2x00_do_dpc_all_vps(base_vha);
3611
3612                 ha->dpc_active = 0;
3613 end_loop:
3614                 set_current_state(TASK_INTERRUPTIBLE);
3615         } /* End of while(1) */
3616         __set_current_state(TASK_RUNNING);
3617
3618         ql_dbg(ql_dbg_dpc, base_vha, 0x4011,
3619             "DPC handler exiting.\n");
3620
3621         /*
3622          * Make sure that nobody tries to wake us up again.
3623          */
3624         ha->dpc_active = 0;
3625
3626         /* Cleanup any residual CTX SRBs. */
3627         qla2x00_abort_all_cmds(base_vha, DID_NO_CONNECT << 16);
3628
3629         return 0;
3630 }
3631
3632 void
3633 qla2xxx_wake_dpc(struct scsi_qla_host *vha)
3634 {
3635         struct qla_hw_data *ha = vha->hw;
3636         struct task_struct *t = ha->dpc_thread;
3637
3638         if (!test_bit(UNLOADING, &vha->dpc_flags) && t)
3639                 wake_up_process(t);
3640 }
3641
3642 /*
3643 *  qla2x00_rst_aen
3644 *      Processes asynchronous reset.
3645 *
3646 * Input:
3647 *      ha  = adapter block pointer.
3648 */
3649 static void
3650 qla2x00_rst_aen(scsi_qla_host_t *vha)
3651 {
3652         if (vha->flags.online && !vha->flags.reset_active &&
3653             !atomic_read(&vha->loop_down_timer) &&
3654             !(test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))) {
3655                 do {
3656                         clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
3657
3658                         /*
3659                          * Issue marker command only when we are going to start
3660                          * the I/O.
3661                          */
3662                         vha->marker_needed = 1;
3663                 } while (!atomic_read(&vha->loop_down_timer) &&
3664                     (test_bit(RESET_MARKER_NEEDED, &vha->dpc_flags)));
3665         }
3666 }
3667
3668 static void
3669 qla2x00_sp_free_dma(srb_t *sp)
3670 {
3671         struct scsi_cmnd *cmd = sp->cmd;
3672         struct qla_hw_data *ha = sp->fcport->vha->hw;
3673
3674         if (sp->flags & SRB_DMA_VALID) {
3675                 scsi_dma_unmap(cmd);
3676                 sp->flags &= ~SRB_DMA_VALID;
3677         }
3678
3679         if (sp->flags & SRB_CRC_PROT_DMA_VALID) {
3680                 dma_unmap_sg(&ha->pdev->dev, scsi_prot_sglist(cmd),
3681                     scsi_prot_sg_count(cmd), cmd->sc_data_direction);
3682                 sp->flags &= ~SRB_CRC_PROT_DMA_VALID;
3683         }
3684
3685         if (sp->flags & SRB_CRC_CTX_DSD_VALID) {
3686                 /* List assured to be having elements */
3687                 qla2x00_clean_dsd_pool(ha, sp);
3688                 sp->flags &= ~SRB_CRC_CTX_DSD_VALID;
3689         }
3690
3691         if (sp->flags & SRB_CRC_CTX_DMA_VALID) {
3692                 dma_pool_free(ha->dl_dma_pool, sp->ctx,
3693                     ((struct crc_context *)sp->ctx)->crc_ctx_dma);
3694                 sp->flags &= ~SRB_CRC_CTX_DMA_VALID;
3695         }
3696
3697         if (sp->flags & SRB_FCP_CMND_DMA_VALID) {
3698                 struct ct6_dsd *ctx = sp->ctx;
3699                 dma_pool_free(ha->fcp_cmnd_dma_pool, ctx->fcp_cmnd,
3700                         ctx->fcp_cmnd_dma);
3701                 list_splice(&ctx->dsd_list, &ha->gbl_dsd_list);
3702                 ha->gbl_dsd_inuse -= ctx->dsd_use_cnt;
3703                 ha->gbl_dsd_avail += ctx->dsd_use_cnt;
3704                 mempool_free(sp->ctx, ha->ctx_mempool);
3705                 sp->ctx = NULL;
3706         }
3707
3708         CMD_SP(cmd) = NULL;
3709 }
3710
3711 static void
3712 qla2x00_sp_final_compl(struct qla_hw_data *ha, srb_t *sp)
3713 {
3714         struct scsi_cmnd *cmd = sp->cmd;
3715
3716         qla2x00_sp_free_dma(sp);
3717         mempool_free(sp, ha->srb_mempool);
3718         cmd->scsi_done(cmd);
3719 }
3720
3721 void
3722 qla2x00_sp_compl(struct qla_hw_data *ha, srb_t *sp)
3723 {
3724         if (atomic_read(&sp->ref_count) == 0) {
3725                 ql_dbg(ql_dbg_io, sp->fcport->vha, 0x3015,
3726                     "SP reference-count to ZERO -- sp=%p cmd=%p.\n",
3727                     sp, sp->cmd);
3728                 if (ql2xextended_error_logging & ql_dbg_io)
3729                         BUG();
3730                 return;
3731         }
3732         if (!atomic_dec_and_test(&sp->ref_count))
3733                 return;
3734         qla2x00_sp_final_compl(ha, sp);
3735 }
3736
3737 /**************************************************************************
3738 *   qla2x00_timer
3739 *
3740 * Description:
3741 *   One second timer
3742 *
3743 * Context: Interrupt
3744 ***************************************************************************/
3745 void
3746 qla2x00_timer(scsi_qla_host_t *vha)
3747 {
3748         unsigned long   cpu_flags = 0;
3749         int             start_dpc = 0;
3750         int             index;
3751         srb_t           *sp;
3752         uint16_t        w;
3753         struct qla_hw_data *ha = vha->hw;
3754         struct req_que *req;
3755
3756         if (ha->flags.eeh_busy) {
3757                 ql_dbg(ql_dbg_timer, vha, 0x6000,
3758                     "EEH = %d, restarting timer.\n",
3759                     ha->flags.eeh_busy);
3760                 qla2x00_restart_timer(vha, WATCH_INTERVAL);
3761                 return;
3762         }
3763
3764         /* Hardware read to raise pending EEH errors during mailbox waits. */
3765         if (!pci_channel_offline(ha->pdev))
3766                 pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
3767
3768         /* Make sure qla82xx_watchdog is run only for physical port */
3769         if (!vha->vp_idx && IS_QLA82XX(ha)) {
3770                 if (test_bit(ISP_QUIESCE_NEEDED, &vha->dpc_flags))
3771                         start_dpc++;
3772                 qla82xx_watchdog(vha);
3773         }
3774
3775         /* Loop down handler. */
3776         if (atomic_read(&vha->loop_down_timer) > 0 &&
3777             !(test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags)) &&
3778             !(test_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags))
3779                 && vha->flags.online) {
3780
3781                 if (atomic_read(&vha->loop_down_timer) ==
3782                     vha->loop_down_abort_time) {
3783
3784                         ql_log(ql_log_info, vha, 0x6008,
3785                             "Loop down - aborting the queues before time expires.\n");
3786
3787                         if (!IS_QLA2100(ha) && vha->link_down_timeout)
3788                                 atomic_set(&vha->loop_state, LOOP_DEAD);
3789
3790                         /*
3791                          * Schedule an ISP abort to return any FCP2-device
3792                          * commands.
3793                          */
3794                         /* NPIV - scan physical port only */
3795                         if (!vha->vp_idx) {
3796                                 spin_lock_irqsave(&ha->hardware_lock,
3797                                     cpu_flags);
3798                                 req = ha->req_q_map[0];
3799                                 for (index = 1;
3800                                     index < MAX_OUTSTANDING_COMMANDS;
3801                                     index++) {
3802                                         fc_port_t *sfcp;
3803
3804                                         sp = req->outstanding_cmds[index];
3805                                         if (!sp)
3806                                                 continue;
3807                                         if (sp->ctx && !IS_PROT_IO(sp))
3808                                                 continue;
3809                                         sfcp = sp->fcport;
3810                                         if (!(sfcp->flags & FCF_FCP2_DEVICE))
3811                                                 continue;
3812
3813                                         if (IS_QLA82XX(ha))
3814                                                 set_bit(FCOE_CTX_RESET_NEEDED,
3815                                                         &vha->dpc_flags);
3816                                         else
3817                                                 set_bit(ISP_ABORT_NEEDED,
3818                                                         &vha->dpc_flags);
3819                                         break;
3820                                 }
3821                                 spin_unlock_irqrestore(&ha->hardware_lock,
3822                                                                 cpu_flags);
3823                         }
3824                         start_dpc++;
3825                 }
3826
3827                 /* if the loop has been down for 4 minutes, reinit adapter */
3828                 if (atomic_dec_and_test(&vha->loop_down_timer) != 0) {
3829                         if (!(vha->device_flags & DFLG_NO_CABLE)) {
3830                                 ql_log(ql_log_warn, vha, 0x6009,
3831                                     "Loop down - aborting ISP.\n");
3832
3833                                 if (IS_QLA82XX(ha))
3834                                         set_bit(FCOE_CTX_RESET_NEEDED,
3835                                                 &vha->dpc_flags);
3836                                 else
3837                                         set_bit(ISP_ABORT_NEEDED,
3838                                                 &vha->dpc_flags);
3839                         }
3840                 }
3841                 ql_dbg(ql_dbg_timer, vha, 0x600a,
3842                     "Loop down - seconds remaining %d.\n",
3843                     atomic_read(&vha->loop_down_timer));
3844         }
3845
3846         /* Check if beacon LED needs to be blinked for physical host only */
3847         if (!vha->vp_idx && (ha->beacon_blink_led == 1)) {
3848                 /* There is no beacon_blink function for ISP82xx */
3849                 if (!IS_QLA82XX(ha)) {
3850                         set_bit(BEACON_BLINK_NEEDED, &vha->dpc_flags);
3851                         start_dpc++;
3852                 }
3853         }
3854
3855         /* Process any deferred work. */
3856         if (!list_empty(&vha->work_list))
3857                 start_dpc++;
3858
3859         /* Schedule the DPC routine if needed */
3860         if ((test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
3861             test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags) ||
3862             test_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags) ||
3863             start_dpc ||
3864             test_bit(RESET_MARKER_NEEDED, &vha->dpc_flags) ||
3865             test_bit(BEACON_BLINK_NEEDED, &vha->dpc_flags) ||
3866             test_bit(ISP_UNRECOVERABLE, &vha->dpc_flags) ||
3867             test_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags) ||
3868             test_bit(VP_DPC_NEEDED, &vha->dpc_flags) ||
3869             test_bit(RELOGIN_NEEDED, &vha->dpc_flags))) {
3870                 ql_dbg(ql_dbg_timer, vha, 0x600b,
3871                     "isp_abort_needed=%d loop_resync_needed=%d "
3872                     "fcport_update_needed=%d start_dpc=%d "
3873                     "reset_marker_needed=%d",
3874                     test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags),
3875                     test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags),
3876                     test_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags),
3877                     start_dpc,
3878                     test_bit(RESET_MARKER_NEEDED, &vha->dpc_flags));
3879                 ql_dbg(ql_dbg_timer, vha, 0x600c,
3880                     "beacon_blink_needed=%d isp_unrecoverable=%d "
3881                     "fcoe_ctx_reset_needed=%d vp_dpc_needed=%d "
3882                     "relogin_needed=%d.\n",
3883                     test_bit(BEACON_BLINK_NEEDED, &vha->dpc_flags),
3884                     test_bit(ISP_UNRECOVERABLE, &vha->dpc_flags),
3885                     test_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags),
3886                     test_bit(VP_DPC_NEEDED, &vha->dpc_flags),
3887                     test_bit(RELOGIN_NEEDED, &vha->dpc_flags));
3888                 qla2xxx_wake_dpc(vha);
3889         }
3890
3891         qla2x00_restart_timer(vha, WATCH_INTERVAL);
3892 }
3893
3894 /* Firmware interface routines. */
3895
3896 #define FW_BLOBS        8
3897 #define FW_ISP21XX      0
3898 #define FW_ISP22XX      1
3899 #define FW_ISP2300      2
3900 #define FW_ISP2322      3
3901 #define FW_ISP24XX      4
3902 #define FW_ISP25XX      5
3903 #define FW_ISP81XX      6
3904 #define FW_ISP82XX      7
3905
3906 #define FW_FILE_ISP21XX "ql2100_fw.bin"
3907 #define FW_FILE_ISP22XX "ql2200_fw.bin"
3908 #define FW_FILE_ISP2300 "ql2300_fw.bin"
3909 #define FW_FILE_ISP2322 "ql2322_fw.bin"
3910 #define FW_FILE_ISP24XX "ql2400_fw.bin"
3911 #define FW_FILE_ISP25XX "ql2500_fw.bin"
3912 #define FW_FILE_ISP81XX "ql8100_fw.bin"
3913 #define FW_FILE_ISP82XX "ql8200_fw.bin"
3914
3915 static DEFINE_MUTEX(qla_fw_lock);
3916
3917 static struct fw_blob qla_fw_blobs[FW_BLOBS] = {
3918         { .name = FW_FILE_ISP21XX, .segs = { 0x1000, 0 }, },
3919         { .name = FW_FILE_ISP22XX, .segs = { 0x1000, 0 }, },
3920         { .name = FW_FILE_ISP2300, .segs = { 0x800, 0 }, },
3921         { .name = FW_FILE_ISP2322, .segs = { 0x800, 0x1c000, 0x1e000, 0 }, },
3922         { .name = FW_FILE_ISP24XX, },
3923         { .name = FW_FILE_ISP25XX, },
3924         { .name = FW_FILE_ISP81XX, },
3925         { .name = FW_FILE_ISP82XX, },
3926 };
3927
3928 struct fw_blob *
3929 qla2x00_request_firmware(scsi_qla_host_t *vha)
3930 {
3931         struct qla_hw_data *ha = vha->hw;
3932         struct fw_blob *blob;
3933
3934         blob = NULL;
3935         if (IS_QLA2100(ha)) {
3936                 blob = &qla_fw_blobs[FW_ISP21XX];
3937         } else if (IS_QLA2200(ha)) {
3938                 blob = &qla_fw_blobs[FW_ISP22XX];
3939         } else if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
3940                 blob = &qla_fw_blobs[FW_ISP2300];
3941         } else if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
3942                 blob = &qla_fw_blobs[FW_ISP2322];
3943         } else if (IS_QLA24XX_TYPE(ha)) {
3944                 blob = &qla_fw_blobs[FW_ISP24XX];
3945         } else if (IS_QLA25XX(ha)) {
3946                 blob = &qla_fw_blobs[FW_ISP25XX];
3947         } else if (IS_QLA81XX(ha)) {
3948                 blob = &qla_fw_blobs[FW_ISP81XX];
3949         } else if (IS_QLA82XX(ha)) {
3950                 blob = &qla_fw_blobs[FW_ISP82XX];
3951         }
3952
3953         mutex_lock(&qla_fw_lock);
3954         if (blob->fw)
3955                 goto out;
3956
3957         if (request_firmware(&blob->fw, blob->name, &ha->pdev->dev)) {
3958                 ql_log(ql_log_warn, vha, 0x0063,
3959                     "Failed to load firmware image (%s).\n", blob->name);
3960                 blob->fw = NULL;
3961                 blob = NULL;
3962                 goto out;
3963         }
3964
3965 out:
3966         mutex_unlock(&qla_fw_lock);
3967         return blob;
3968 }
3969
3970 static void
3971 qla2x00_release_firmware(void)
3972 {
3973         int idx;
3974
3975         mutex_lock(&qla_fw_lock);
3976         for (idx = 0; idx < FW_BLOBS; idx++)
3977                 if (qla_fw_blobs[idx].fw)
3978                         release_firmware(qla_fw_blobs[idx].fw);
3979         mutex_unlock(&qla_fw_lock);
3980 }
3981
3982 static pci_ers_result_t
3983 qla2xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
3984 {
3985         scsi_qla_host_t *vha = pci_get_drvdata(pdev);
3986         struct qla_hw_data *ha = vha->hw;
3987
3988         ql_dbg(ql_dbg_aer, vha, 0x9000,
3989             "PCI error detected, state %x.\n", state);
3990
3991         switch (state) {
3992         case pci_channel_io_normal:
3993                 ha->flags.eeh_busy = 0;
3994                 return PCI_ERS_RESULT_CAN_RECOVER;
3995         case pci_channel_io_frozen:
3996                 ha->flags.eeh_busy = 1;
3997                 /* For ISP82XX complete any pending mailbox cmd */
3998                 if (IS_QLA82XX(ha)) {
3999                         ha->flags.isp82xx_fw_hung = 1;
4000                         ql_dbg(ql_dbg_aer, vha, 0x9001, "Pci channel io frozen\n");
4001                         qla82xx_clear_pending_mbx(vha);
4002                 }
4003                 qla2x00_free_irqs(vha);
4004                 pci_disable_device(pdev);
4005                 /* Return back all IOs */
4006                 qla2x00_abort_all_cmds(vha, DID_RESET << 16);
4007                 return PCI_ERS_RESULT_NEED_RESET;
4008         case pci_channel_io_perm_failure:
4009                 ha->flags.pci_channel_io_perm_failure = 1;
4010                 qla2x00_abort_all_cmds(vha, DID_NO_CONNECT << 16);
4011                 return PCI_ERS_RESULT_DISCONNECT;
4012         }
4013         return PCI_ERS_RESULT_NEED_RESET;
4014 }
4015
4016 static pci_ers_result_t
4017 qla2xxx_pci_mmio_enabled(struct pci_dev *pdev)
4018 {
4019         int risc_paused = 0;
4020         uint32_t stat;
4021         unsigned long flags;
4022         scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
4023         struct qla_hw_data *ha = base_vha->hw;
4024         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
4025         struct device_reg_24xx __iomem *reg24 = &ha->iobase->isp24;
4026
4027         if (IS_QLA82XX(ha))
4028                 return PCI_ERS_RESULT_RECOVERED;
4029
4030         spin_lock_irqsave(&ha->hardware_lock, flags);
4031         if (IS_QLA2100(ha) || IS_QLA2200(ha)){
4032                 stat = RD_REG_DWORD(&reg->hccr);
4033                 if (stat & HCCR_RISC_PAUSE)
4034                         risc_paused = 1;
4035         } else if (IS_QLA23XX(ha)) {
4036                 stat = RD_REG_DWORD(&reg->u.isp2300.host_status);
4037                 if (stat & HSR_RISC_PAUSED)
4038                         risc_paused = 1;
4039         } else if (IS_FWI2_CAPABLE(ha)) {
4040                 stat = RD_REG_DWORD(&reg24->host_status);
4041                 if (stat & HSRX_RISC_PAUSED)
4042                         risc_paused = 1;
4043         }
4044         spin_unlock_irqrestore(&ha->hardware_lock, flags);
4045
4046         if (risc_paused) {
4047                 ql_log(ql_log_info, base_vha, 0x9003,
4048                     "RISC paused -- mmio_enabled, Dumping firmware.\n");
4049                 ha->isp_ops->fw_dump(base_vha, 0);
4050
4051                 return PCI_ERS_RESULT_NEED_RESET;
4052         } else
4053                 return PCI_ERS_RESULT_RECOVERED;
4054 }
4055
4056 uint32_t qla82xx_error_recovery(scsi_qla_host_t *base_vha)
4057 {
4058         uint32_t rval = QLA_FUNCTION_FAILED;
4059         uint32_t drv_active = 0;
4060         struct qla_hw_data *ha = base_vha->hw;
4061         int fn;
4062         struct pci_dev *other_pdev = NULL;
4063
4064         ql_dbg(ql_dbg_aer, base_vha, 0x9006,
4065             "Entered %s.\n", __func__);
4066
4067         set_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
4068
4069         if (base_vha->flags.online) {
4070                 /* Abort all outstanding commands,
4071                  * so as to be requeued later */
4072                 qla2x00_abort_isp_cleanup(base_vha);
4073         }
4074
4075
4076         fn = PCI_FUNC(ha->pdev->devfn);
4077         while (fn > 0) {
4078                 fn--;
4079                 ql_dbg(ql_dbg_aer, base_vha, 0x9007,
4080                     "Finding pci device at function = 0x%x.\n", fn);
4081                 other_pdev =
4082                     pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
4083                     ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
4084                     fn));
4085
4086                 if (!other_pdev)
4087                         continue;
4088                 if (atomic_read(&other_pdev->enable_cnt)) {
4089                         ql_dbg(ql_dbg_aer, base_vha, 0x9008,
4090                             "Found PCI func available and enable at 0x%x.\n",
4091                             fn);
4092                         pci_dev_put(other_pdev);
4093                         break;
4094                 }
4095                 pci_dev_put(other_pdev);
4096         }
4097
4098         if (!fn) {
4099                 /* Reset owner */
4100                 ql_dbg(ql_dbg_aer, base_vha, 0x9009,
4101                     "This devfn is reset owner = 0x%x.\n",
4102                     ha->pdev->devfn);
4103                 qla82xx_idc_lock(ha);
4104
4105                 qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
4106                     QLA82XX_DEV_INITIALIZING);
4107
4108                 qla82xx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
4109                     QLA82XX_IDC_VERSION);
4110
4111                 drv_active = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_ACTIVE);
4112                 ql_dbg(ql_dbg_aer, base_vha, 0x900a,
4113                     "drv_active = 0x%x.\n", drv_active);
4114
4115                 qla82xx_idc_unlock(ha);
4116                 /* Reset if device is not already reset
4117                  * drv_active would be 0 if a reset has already been done
4118                  */
4119                 if (drv_active)
4120                         rval = qla82xx_start_firmware(base_vha);
4121                 else
4122                         rval = QLA_SUCCESS;
4123                 qla82xx_idc_lock(ha);
4124
4125                 if (rval != QLA_SUCCESS) {
4126                         ql_log(ql_log_info, base_vha, 0x900b,
4127                             "HW State: FAILED.\n");
4128                         qla82xx_clear_drv_active(ha);
4129                         qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
4130                             QLA82XX_DEV_FAILED);
4131                 } else {
4132                         ql_log(ql_log_info, base_vha, 0x900c,
4133                             "HW State: READY.\n");
4134                         qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
4135                             QLA82XX_DEV_READY);
4136                         qla82xx_idc_unlock(ha);
4137                         ha->flags.isp82xx_fw_hung = 0;
4138                         rval = qla82xx_restart_isp(base_vha);
4139                         qla82xx_idc_lock(ha);
4140                         /* Clear driver state register */
4141                         qla82xx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
4142                         qla82xx_set_drv_active(base_vha);
4143                 }
4144                 qla82xx_idc_unlock(ha);
4145         } else {
4146                 ql_dbg(ql_dbg_aer, base_vha, 0x900d,
4147                     "This devfn is not reset owner = 0x%x.\n",
4148                     ha->pdev->devfn);
4149                 if ((qla82xx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
4150                     QLA82XX_DEV_READY)) {
4151                         ha->flags.isp82xx_fw_hung = 0;
4152                         rval = qla82xx_restart_isp(base_vha);
4153                         qla82xx_idc_lock(ha);
4154                         qla82xx_set_drv_active(base_vha);
4155                         qla82xx_idc_unlock(ha);
4156                 }
4157         }
4158         clear_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
4159
4160         return rval;
4161 }
4162
4163 static pci_ers_result_t
4164 qla2xxx_pci_slot_reset(struct pci_dev *pdev)
4165 {
4166         pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
4167         scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
4168         struct qla_hw_data *ha = base_vha->hw;
4169         struct rsp_que *rsp;
4170         int rc, retries = 10;
4171
4172         ql_dbg(ql_dbg_aer, base_vha, 0x9004,
4173             "Slot Reset.\n");
4174
4175         /* Workaround: qla2xxx driver which access hardware earlier
4176          * needs error state to be pci_channel_io_online.
4177          * Otherwise mailbox command timesout.
4178          */
4179         pdev->error_state = pci_channel_io_normal;
4180
4181         pci_restore_state(pdev);
4182
4183         /* pci_restore_state() clears the saved_state flag of the device
4184          * save restored state which resets saved_state flag
4185          */
4186         pci_save_state(pdev);
4187
4188         if (ha->mem_only)
4189                 rc = pci_enable_device_mem(pdev);
4190         else
4191                 rc = pci_enable_device(pdev);
4192
4193         if (rc) {
4194                 ql_log(ql_log_warn, base_vha, 0x9005,
4195                     "Can't re-enable PCI device after reset.\n");
4196                 goto exit_slot_reset;
4197         }
4198
4199         rsp = ha->rsp_q_map[0];
4200         if (qla2x00_request_irqs(ha, rsp))
4201                 goto exit_slot_reset;
4202
4203         if (ha->isp_ops->pci_config(base_vha))
4204                 goto exit_slot_reset;
4205
4206         if (IS_QLA82XX(ha)) {
4207                 if (qla82xx_error_recovery(base_vha) == QLA_SUCCESS) {
4208                         ret = PCI_ERS_RESULT_RECOVERED;
4209                         goto exit_slot_reset;
4210                 } else
4211                         goto exit_slot_reset;
4212         }
4213
4214         while (ha->flags.mbox_busy && retries--)
4215                 msleep(1000);
4216
4217         set_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
4218         if (ha->isp_ops->abort_isp(base_vha) == QLA_SUCCESS)
4219                 ret =  PCI_ERS_RESULT_RECOVERED;
4220         clear_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
4221
4222
4223 exit_slot_reset:
4224         ql_dbg(ql_dbg_aer, base_vha, 0x900e,
4225             "slot_reset return %x.\n", ret);
4226
4227         return ret;
4228 }
4229
4230 static void
4231 qla2xxx_pci_resume(struct pci_dev *pdev)
4232 {
4233         scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
4234         struct qla_hw_data *ha = base_vha->hw;
4235         int ret;
4236
4237         ql_dbg(ql_dbg_aer, base_vha, 0x900f,
4238             "pci_resume.\n");
4239
4240         ret = qla2x00_wait_for_hba_online(base_vha);
4241         if (ret != QLA_SUCCESS) {
4242                 ql_log(ql_log_fatal, base_vha, 0x9002,
4243                     "The device failed to resume I/O from slot/link_reset.\n");
4244         }
4245
4246         pci_cleanup_aer_uncorrect_error_status(pdev);
4247
4248         ha->flags.eeh_busy = 0;
4249 }
4250
4251 static struct pci_error_handlers qla2xxx_err_handler = {
4252         .error_detected = qla2xxx_pci_error_detected,
4253         .mmio_enabled = qla2xxx_pci_mmio_enabled,
4254         .slot_reset = qla2xxx_pci_slot_reset,
4255         .resume = qla2xxx_pci_resume,
4256 };
4257
4258 static struct pci_device_id qla2xxx_pci_tbl[] = {
4259         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2100) },
4260         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2200) },
4261         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2300) },
4262         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2312) },
4263         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2322) },
4264         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP6312) },
4265         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP6322) },
4266         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2422) },
4267         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2432) },
4268         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP8432) },
4269         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP5422) },
4270         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP5432) },
4271         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2532) },
4272         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP8001) },
4273         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP8021) },
4274         { 0 },
4275 };
4276 MODULE_DEVICE_TABLE(pci, qla2xxx_pci_tbl);
4277
4278 static struct pci_driver qla2xxx_pci_driver = {
4279         .name           = QLA2XXX_DRIVER_NAME,
4280         .driver         = {
4281                 .owner          = THIS_MODULE,
4282         },
4283         .id_table       = qla2xxx_pci_tbl,
4284         .probe          = qla2x00_probe_one,
4285         .remove         = qla2x00_remove_one,
4286         .shutdown       = qla2x00_shutdown,
4287         .err_handler    = &qla2xxx_err_handler,
4288 };
4289
4290 static struct file_operations apidev_fops = {
4291         .owner = THIS_MODULE,
4292         .llseek = noop_llseek,
4293 };
4294
4295 /**
4296  * qla2x00_module_init - Module initialization.
4297  **/
4298 static int __init
4299 qla2x00_module_init(void)
4300 {
4301         int ret = 0;
4302
4303         /* Allocate cache for SRBs. */
4304         srb_cachep = kmem_cache_create("qla2xxx_srbs", sizeof(srb_t), 0,
4305             SLAB_HWCACHE_ALIGN, NULL);
4306         if (srb_cachep == NULL) {
4307                 ql_log(ql_log_fatal, NULL, 0x0001,
4308                     "Unable to allocate SRB cache...Failing load!.\n");
4309                 return -ENOMEM;
4310         }
4311
4312         /* Derive version string. */
4313         strcpy(qla2x00_version_str, QLA2XXX_VERSION);
4314         if (ql2xextended_error_logging)
4315                 strcat(qla2x00_version_str, "-debug");
4316
4317         qla2xxx_transport_template =
4318             fc_attach_transport(&qla2xxx_transport_functions);
4319         if (!qla2xxx_transport_template) {
4320                 kmem_cache_destroy(srb_cachep);
4321                 ql_log(ql_log_fatal, NULL, 0x0002,
4322                     "fc_attach_transport failed...Failing load!.\n");
4323                 return -ENODEV;
4324         }
4325
4326         apidev_major = register_chrdev(0, QLA2XXX_APIDEV, &apidev_fops);
4327         if (apidev_major < 0) {
4328                 ql_log(ql_log_fatal, NULL, 0x0003,
4329                     "Unable to register char device %s.\n", QLA2XXX_APIDEV);
4330         }
4331
4332         qla2xxx_transport_vport_template =
4333             fc_attach_transport(&qla2xxx_transport_vport_functions);
4334         if (!qla2xxx_transport_vport_template) {
4335                 kmem_cache_destroy(srb_cachep);
4336                 fc_release_transport(qla2xxx_transport_template);
4337                 ql_log(ql_log_fatal, NULL, 0x0004,
4338                     "fc_attach_transport vport failed...Failing load!.\n");
4339                 return -ENODEV;
4340         }
4341         ql_log(ql_log_info, NULL, 0x0005,
4342             "QLogic Fibre Channel HBA Driver: %s.\n",
4343             qla2x00_version_str);
4344         ret = pci_register_driver(&qla2xxx_pci_driver);
4345         if (ret) {
4346                 kmem_cache_destroy(srb_cachep);
4347                 fc_release_transport(qla2xxx_transport_template);
4348                 fc_release_transport(qla2xxx_transport_vport_template);
4349                 ql_log(ql_log_fatal, NULL, 0x0006,
4350                     "pci_register_driver failed...ret=%d Failing load!.\n",
4351                     ret);
4352         }
4353         return ret;
4354 }
4355
4356 /**
4357  * qla2x00_module_exit - Module cleanup.
4358  **/
4359 static void __exit
4360 qla2x00_module_exit(void)
4361 {
4362         unregister_chrdev(apidev_major, QLA2XXX_APIDEV);
4363         pci_unregister_driver(&qla2xxx_pci_driver);
4364         qla2x00_release_firmware();
4365         kmem_cache_destroy(srb_cachep);
4366         if (ctx_cachep)
4367                 kmem_cache_destroy(ctx_cachep);
4368         fc_release_transport(qla2xxx_transport_template);
4369         fc_release_transport(qla2xxx_transport_vport_template);
4370 }
4371
4372 module_init(qla2x00_module_init);
4373 module_exit(qla2x00_module_exit);
4374
4375 MODULE_AUTHOR("QLogic Corporation");
4376 MODULE_DESCRIPTION("QLogic Fibre Channel HBA Driver");
4377 MODULE_LICENSE("GPL");
4378 MODULE_VERSION(QLA2XXX_VERSION);
4379 MODULE_FIRMWARE(FW_FILE_ISP21XX);
4380 MODULE_FIRMWARE(FW_FILE_ISP22XX);
4381 MODULE_FIRMWARE(FW_FILE_ISP2300);
4382 MODULE_FIRMWARE(FW_FILE_ISP2322);
4383 MODULE_FIRMWARE(FW_FILE_ISP24XX);
4384 MODULE_FIRMWARE(FW_FILE_ISP25XX);