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