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