[SCSI] qla2xxx: Check to make sure multique and CPU affinity support is not enabled...
[safe/jmp/linux-2.6] / 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) ||
1680                 (ql2xmaxqueues > 1 && ql2xmultique_tag) ||
1681                 (!IS_QLA25XX(ha) && !IS_QLA81XX(ha)))
1682                 goto mqiobase_exit;
1683
1684         ha->mqiobase = ioremap(pci_resource_start(ha->pdev, 3),
1685                         pci_resource_len(ha->pdev, 3));
1686         if (ha->mqiobase) {
1687                 /* Read MSIX vector size of the board */
1688                 pci_read_config_word(ha->pdev, QLA_PCI_MSIX_CONTROL, &msix);
1689                 ha->msix_count = msix;
1690                 /* Max queues are bounded by available msix vectors */
1691                 /* queue 0 uses two msix vectors */
1692                 if (ql2xmultique_tag) {
1693                         cpus = num_online_cpus();
1694                         ha->max_rsp_queues = (ha->msix_count - 1 > cpus) ?
1695                                 (cpus + 1) : (ha->msix_count - 1);
1696                         ha->max_req_queues = 2;
1697                 } else if (ql2xmaxqueues > 1) {
1698                         ha->max_req_queues = ql2xmaxqueues > QLA_MQ_SIZE ?
1699                                                 QLA_MQ_SIZE : ql2xmaxqueues;
1700                         DEBUG2(qla_printk(KERN_INFO, ha, "QoS mode set, max no"
1701                         " of request queues:%d\n", ha->max_req_queues));
1702                 }
1703                 qla_printk(KERN_INFO, ha,
1704                         "MSI-X vector count: %d\n", msix);
1705         } else
1706                 qla_printk(KERN_INFO, ha, "BAR 3 not enabled\n");
1707
1708 mqiobase_exit:
1709         ha->msix_count = ha->max_rsp_queues + 1;
1710         return (0);
1711
1712 iospace_error_exit:
1713         return (-ENOMEM);
1714 }
1715
1716 static void
1717 qla2xxx_scan_start(struct Scsi_Host *shost)
1718 {
1719         scsi_qla_host_t *vha = shost_priv(shost);
1720
1721         if (vha->hw->flags.running_gold_fw)
1722                 return;
1723
1724         set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
1725         set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
1726         set_bit(RSCN_UPDATE, &vha->dpc_flags);
1727         set_bit(NPIV_CONFIG_NEEDED, &vha->dpc_flags);
1728 }
1729
1730 static int
1731 qla2xxx_scan_finished(struct Scsi_Host *shost, unsigned long time)
1732 {
1733         scsi_qla_host_t *vha = shost_priv(shost);
1734
1735         if (!vha->host)
1736                 return 1;
1737         if (time > vha->hw->loop_reset_delay * HZ)
1738                 return 1;
1739
1740         return atomic_read(&vha->loop_state) == LOOP_READY;
1741 }
1742
1743 /*
1744  * PCI driver interface
1745  */
1746 static int __devinit
1747 qla2x00_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
1748 {
1749         int     ret = -ENODEV;
1750         struct Scsi_Host *host;
1751         scsi_qla_host_t *base_vha = NULL;
1752         struct qla_hw_data *ha;
1753         char pci_info[30];
1754         char fw_str[30];
1755         struct scsi_host_template *sht;
1756         int bars, max_id, mem_only = 0;
1757         uint16_t req_length = 0, rsp_length = 0;
1758         struct req_que *req = NULL;
1759         struct rsp_que *rsp = NULL;
1760
1761         bars = pci_select_bars(pdev, IORESOURCE_MEM | IORESOURCE_IO);
1762         sht = &qla2xxx_driver_template;
1763         if (pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2422 ||
1764             pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2432 ||
1765             pdev->device == PCI_DEVICE_ID_QLOGIC_ISP8432 ||
1766             pdev->device == PCI_DEVICE_ID_QLOGIC_ISP5422 ||
1767             pdev->device == PCI_DEVICE_ID_QLOGIC_ISP5432 ||
1768             pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2532 ||
1769             pdev->device == PCI_DEVICE_ID_QLOGIC_ISP8001) {
1770                 bars = pci_select_bars(pdev, IORESOURCE_MEM);
1771                 mem_only = 1;
1772         }
1773
1774         if (mem_only) {
1775                 if (pci_enable_device_mem(pdev))
1776                         goto probe_out;
1777         } else {
1778                 if (pci_enable_device(pdev))
1779                         goto probe_out;
1780         }
1781
1782         /* This may fail but that's ok */
1783         pci_enable_pcie_error_reporting(pdev);
1784
1785         ha = kzalloc(sizeof(struct qla_hw_data), GFP_KERNEL);
1786         if (!ha) {
1787                 DEBUG(printk("Unable to allocate memory for ha\n"));
1788                 goto probe_out;
1789         }
1790         ha->pdev = pdev;
1791
1792         /* Clear our data area */
1793         ha->bars = bars;
1794         ha->mem_only = mem_only;
1795         spin_lock_init(&ha->hardware_lock);
1796
1797         /* Set ISP-type information. */
1798         qla2x00_set_isp_flags(ha);
1799
1800         /* Set EEH reset type to fundamental if required by hba */
1801         if ( IS_QLA24XX(ha) || IS_QLA25XX(ha) || IS_QLA81XX(ha)) {
1802                 pdev->needs_freset = 1;
1803         }
1804
1805         /* Configure PCI I/O space */
1806         ret = qla2x00_iospace_config(ha);
1807         if (ret)
1808                 goto probe_hw_failed;
1809
1810         qla_printk(KERN_INFO, ha,
1811             "Found an ISP%04X, irq %d, iobase 0x%p\n", pdev->device, pdev->irq,
1812             ha->iobase);
1813
1814         ha->prev_topology = 0;
1815         ha->init_cb_size = sizeof(init_cb_t);
1816         ha->link_data_rate = PORT_SPEED_UNKNOWN;
1817         ha->optrom_size = OPTROM_SIZE_2300;
1818
1819         /* Assign ISP specific operations. */
1820         max_id = MAX_TARGETS_2200;
1821         if (IS_QLA2100(ha)) {
1822                 max_id = MAX_TARGETS_2100;
1823                 ha->mbx_count = MAILBOX_REGISTER_COUNT_2100;
1824                 req_length = REQUEST_ENTRY_CNT_2100;
1825                 rsp_length = RESPONSE_ENTRY_CNT_2100;
1826                 ha->max_loop_id = SNS_LAST_LOOP_ID_2100;
1827                 ha->gid_list_info_size = 4;
1828                 ha->flash_conf_off = ~0;
1829                 ha->flash_data_off = ~0;
1830                 ha->nvram_conf_off = ~0;
1831                 ha->nvram_data_off = ~0;
1832                 ha->isp_ops = &qla2100_isp_ops;
1833         } else if (IS_QLA2200(ha)) {
1834                 ha->mbx_count = MAILBOX_REGISTER_COUNT;
1835                 req_length = REQUEST_ENTRY_CNT_2200;
1836                 rsp_length = RESPONSE_ENTRY_CNT_2100;
1837                 ha->max_loop_id = SNS_LAST_LOOP_ID_2100;
1838                 ha->gid_list_info_size = 4;
1839                 ha->flash_conf_off = ~0;
1840                 ha->flash_data_off = ~0;
1841                 ha->nvram_conf_off = ~0;
1842                 ha->nvram_data_off = ~0;
1843                 ha->isp_ops = &qla2100_isp_ops;
1844         } else if (IS_QLA23XX(ha)) {
1845                 ha->mbx_count = MAILBOX_REGISTER_COUNT;
1846                 req_length = REQUEST_ENTRY_CNT_2200;
1847                 rsp_length = RESPONSE_ENTRY_CNT_2300;
1848                 ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
1849                 ha->gid_list_info_size = 6;
1850                 if (IS_QLA2322(ha) || IS_QLA6322(ha))
1851                         ha->optrom_size = OPTROM_SIZE_2322;
1852                 ha->flash_conf_off = ~0;
1853                 ha->flash_data_off = ~0;
1854                 ha->nvram_conf_off = ~0;
1855                 ha->nvram_data_off = ~0;
1856                 ha->isp_ops = &qla2300_isp_ops;
1857         } else if (IS_QLA24XX_TYPE(ha)) {
1858                 ha->mbx_count = MAILBOX_REGISTER_COUNT;
1859                 req_length = REQUEST_ENTRY_CNT_24XX;
1860                 rsp_length = RESPONSE_ENTRY_CNT_2300;
1861                 ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
1862                 ha->init_cb_size = sizeof(struct mid_init_cb_24xx);
1863                 ha->gid_list_info_size = 8;
1864                 ha->optrom_size = OPTROM_SIZE_24XX;
1865                 ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA24XX;
1866                 ha->isp_ops = &qla24xx_isp_ops;
1867                 ha->flash_conf_off = FARX_ACCESS_FLASH_CONF;
1868                 ha->flash_data_off = FARX_ACCESS_FLASH_DATA;
1869                 ha->nvram_conf_off = FARX_ACCESS_NVRAM_CONF;
1870                 ha->nvram_data_off = FARX_ACCESS_NVRAM_DATA;
1871         } else if (IS_QLA25XX(ha)) {
1872                 ha->mbx_count = MAILBOX_REGISTER_COUNT;
1873                 req_length = REQUEST_ENTRY_CNT_24XX;
1874                 rsp_length = RESPONSE_ENTRY_CNT_2300;
1875                 ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
1876                 ha->init_cb_size = sizeof(struct mid_init_cb_24xx);
1877                 ha->gid_list_info_size = 8;
1878                 ha->optrom_size = OPTROM_SIZE_25XX;
1879                 ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA25XX;
1880                 ha->isp_ops = &qla25xx_isp_ops;
1881                 ha->flash_conf_off = FARX_ACCESS_FLASH_CONF;
1882                 ha->flash_data_off = FARX_ACCESS_FLASH_DATA;
1883                 ha->nvram_conf_off = FARX_ACCESS_NVRAM_CONF;
1884                 ha->nvram_data_off = FARX_ACCESS_NVRAM_DATA;
1885         } else if (IS_QLA81XX(ha)) {
1886                 ha->mbx_count = MAILBOX_REGISTER_COUNT;
1887                 req_length = REQUEST_ENTRY_CNT_24XX;
1888                 rsp_length = RESPONSE_ENTRY_CNT_2300;
1889                 ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
1890                 ha->init_cb_size = sizeof(struct mid_init_cb_81xx);
1891                 ha->gid_list_info_size = 8;
1892                 ha->optrom_size = OPTROM_SIZE_81XX;
1893                 ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA25XX;
1894                 ha->isp_ops = &qla81xx_isp_ops;
1895                 ha->flash_conf_off = FARX_ACCESS_FLASH_CONF_81XX;
1896                 ha->flash_data_off = FARX_ACCESS_FLASH_DATA_81XX;
1897                 ha->nvram_conf_off = ~0;
1898                 ha->nvram_data_off = ~0;
1899         }
1900
1901         mutex_init(&ha->vport_lock);
1902         init_completion(&ha->mbx_cmd_comp);
1903         complete(&ha->mbx_cmd_comp);
1904         init_completion(&ha->mbx_intr_comp);
1905
1906         set_bit(0, (unsigned long *) ha->vp_idx_map);
1907
1908         qla2x00_config_dma_addressing(ha);
1909         ret = qla2x00_mem_alloc(ha, req_length, rsp_length, &req, &rsp);
1910         if (!ret) {
1911                 qla_printk(KERN_WARNING, ha,
1912                     "[ERROR] Failed to allocate memory for adapter\n");
1913
1914                 goto probe_hw_failed;
1915         }
1916
1917         req->max_q_depth = MAX_Q_DEPTH;
1918         if (ql2xmaxqdepth != 0 && ql2xmaxqdepth <= 0xffffU)
1919                 req->max_q_depth = ql2xmaxqdepth;
1920
1921
1922         base_vha = qla2x00_create_host(sht, ha);
1923         if (!base_vha) {
1924                 qla_printk(KERN_WARNING, ha,
1925                     "[ERROR] Failed to allocate memory for scsi_host\n");
1926
1927                 ret = -ENOMEM;
1928                 qla2x00_mem_free(ha);
1929                 qla2x00_free_req_que(ha, req);
1930                 qla2x00_free_rsp_que(ha, rsp);
1931                 goto probe_hw_failed;
1932         }
1933
1934         pci_set_drvdata(pdev, base_vha);
1935
1936         host = base_vha->host;
1937         base_vha->req = req;
1938         host->can_queue = req->length + 128;
1939         if (IS_QLA2XXX_MIDTYPE(ha))
1940                 base_vha->mgmt_svr_loop_id = 10 + base_vha->vp_idx;
1941         else
1942                 base_vha->mgmt_svr_loop_id = MANAGEMENT_SERVER +
1943                                                 base_vha->vp_idx;
1944         if (IS_QLA2100(ha))
1945                 host->sg_tablesize = 32;
1946         host->max_id = max_id;
1947         host->this_id = 255;
1948         host->cmd_per_lun = 3;
1949         host->unique_id = host->host_no;
1950         host->max_cmd_len = MAX_CMDSZ;
1951         host->max_channel = MAX_BUSES - 1;
1952         host->max_lun = MAX_LUNS;
1953         host->transportt = qla2xxx_transport_template;
1954         sht->vendor_id = (SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_QLOGIC);
1955
1956         /* Set up the irqs */
1957         ret = qla2x00_request_irqs(ha, rsp);
1958         if (ret)
1959                 goto probe_init_failed;
1960
1961         pci_save_state(pdev);
1962
1963         /* Alloc arrays of request and response ring ptrs */
1964 que_init:
1965         if (!qla2x00_alloc_queues(ha)) {
1966                 qla_printk(KERN_WARNING, ha,
1967                 "[ERROR] Failed to allocate memory for queue"
1968                 " pointers\n");
1969                 goto probe_init_failed;
1970         }
1971         ha->rsp_q_map[0] = rsp;
1972         ha->req_q_map[0] = req;
1973         rsp->req = req;
1974         req->rsp = rsp;
1975         set_bit(0, ha->req_qid_map);
1976         set_bit(0, ha->rsp_qid_map);
1977         /* FWI2-capable only. */
1978         req->req_q_in = &ha->iobase->isp24.req_q_in;
1979         req->req_q_out = &ha->iobase->isp24.req_q_out;
1980         rsp->rsp_q_in = &ha->iobase->isp24.rsp_q_in;
1981         rsp->rsp_q_out = &ha->iobase->isp24.rsp_q_out;
1982         if (ha->mqenable) {
1983                 req->req_q_in = &ha->mqiobase->isp25mq.req_q_in;
1984                 req->req_q_out = &ha->mqiobase->isp25mq.req_q_out;
1985                 rsp->rsp_q_in = &ha->mqiobase->isp25mq.rsp_q_in;
1986                 rsp->rsp_q_out =  &ha->mqiobase->isp25mq.rsp_q_out;
1987         }
1988
1989         if (qla2x00_initialize_adapter(base_vha)) {
1990                 qla_printk(KERN_WARNING, ha,
1991                     "Failed to initialize adapter\n");
1992
1993                 DEBUG2(printk("scsi(%ld): Failed to initialize adapter - "
1994                     "Adapter flags %x.\n",
1995                     base_vha->host_no, base_vha->device_flags));
1996
1997                 ret = -ENODEV;
1998                 goto probe_failed;
1999         }
2000
2001         if (ha->mqenable) {
2002                 if (qla25xx_setup_mode(base_vha)) {
2003                         qla_printk(KERN_WARNING, ha,
2004                                 "Can't create queues, falling back to single"
2005                                 " queue mode\n");
2006                         goto que_init;
2007                 }
2008         }
2009
2010         if (ha->flags.running_gold_fw)
2011                 goto skip_dpc;
2012
2013         /*
2014          * Startup the kernel thread for this host adapter
2015          */
2016         ha->dpc_thread = kthread_create(qla2x00_do_dpc, ha,
2017                         "%s_dpc", base_vha->host_str);
2018         if (IS_ERR(ha->dpc_thread)) {
2019                 qla_printk(KERN_WARNING, ha,
2020                     "Unable to start DPC thread!\n");
2021                 ret = PTR_ERR(ha->dpc_thread);
2022                 goto probe_failed;
2023         }
2024
2025 skip_dpc:
2026         list_add_tail(&base_vha->list, &ha->vp_list);
2027         base_vha->host->irq = ha->pdev->irq;
2028
2029         /* Initialized the timer */
2030         qla2x00_start_timer(base_vha, qla2x00_timer, WATCH_INTERVAL);
2031
2032         DEBUG2(printk("DEBUG: detect hba %ld at address = %p\n",
2033             base_vha->host_no, ha));
2034
2035         ret = scsi_add_host(host, &pdev->dev);
2036         if (ret)
2037                 goto probe_failed;
2038
2039         base_vha->flags.init_done = 1;
2040         base_vha->flags.online = 1;
2041
2042         ha->isp_ops->enable_intrs(ha);
2043
2044         scsi_scan_host(host);
2045
2046         qla2x00_alloc_sysfs_attr(base_vha);
2047
2048         qla2x00_init_host_attr(base_vha);
2049
2050         qla2x00_dfs_setup(base_vha);
2051
2052         qla_printk(KERN_INFO, ha, "\n"
2053             " QLogic Fibre Channel HBA Driver: %s\n"
2054             "  QLogic %s - %s\n"
2055             "  ISP%04X: %s @ %s hdma%c, host#=%ld, fw=%s\n",
2056             qla2x00_version_str, ha->model_number,
2057             ha->model_desc ? ha->model_desc : "", pdev->device,
2058             ha->isp_ops->pci_info_str(base_vha, pci_info), pci_name(pdev),
2059             ha->flags.enable_64bit_addressing ? '+' : '-', base_vha->host_no,
2060             ha->isp_ops->fw_version_str(base_vha, fw_str));
2061
2062         return 0;
2063
2064 probe_init_failed:
2065         qla2x00_free_req_que(ha, req);
2066         qla2x00_free_rsp_que(ha, rsp);
2067         ha->max_req_queues = ha->max_rsp_queues = 0;
2068
2069 probe_failed:
2070         if (base_vha->timer_active)
2071                 qla2x00_stop_timer(base_vha);
2072         base_vha->flags.online = 0;
2073         if (ha->dpc_thread) {
2074                 struct task_struct *t = ha->dpc_thread;
2075
2076                 ha->dpc_thread = NULL;
2077                 kthread_stop(t);
2078         }
2079
2080         qla2x00_free_device(base_vha);
2081
2082         scsi_host_put(base_vha->host);
2083
2084 probe_hw_failed:
2085         if (ha->iobase)
2086                 iounmap(ha->iobase);
2087
2088         pci_release_selected_regions(ha->pdev, ha->bars);
2089         kfree(ha);
2090         ha = NULL;
2091
2092 probe_out:
2093         pci_disable_device(pdev);
2094         return ret;
2095 }
2096
2097 static void
2098 qla2x00_remove_one(struct pci_dev *pdev)
2099 {
2100         scsi_qla_host_t *base_vha, *vha, *temp;
2101         struct qla_hw_data  *ha;
2102
2103         base_vha = pci_get_drvdata(pdev);
2104         ha = base_vha->hw;
2105
2106         list_for_each_entry_safe(vha, temp, &ha->vp_list, list) {
2107                 if (vha && vha->fc_vport)
2108                         fc_vport_terminate(vha->fc_vport);
2109         }
2110
2111         set_bit(UNLOADING, &base_vha->dpc_flags);
2112
2113         qla2x00_abort_all_cmds(base_vha, DID_NO_CONNECT << 16);
2114
2115         qla2x00_dfs_remove(base_vha);
2116
2117         qla84xx_put_chip(base_vha);
2118
2119         /* Disable timer */
2120         if (base_vha->timer_active)
2121                 qla2x00_stop_timer(base_vha);
2122
2123         base_vha->flags.online = 0;
2124
2125         /* Flush the work queue and remove it */
2126         if (ha->wq) {
2127                 flush_workqueue(ha->wq);
2128                 destroy_workqueue(ha->wq);
2129                 ha->wq = NULL;
2130         }
2131
2132         /* Kill the kernel thread for this host */
2133         if (ha->dpc_thread) {
2134                 struct task_struct *t = ha->dpc_thread;
2135
2136                 /*
2137                  * qla2xxx_wake_dpc checks for ->dpc_thread
2138                  * so we need to zero it out.
2139                  */
2140                 ha->dpc_thread = NULL;
2141                 kthread_stop(t);
2142         }
2143
2144         qla2x00_free_sysfs_attr(base_vha);
2145
2146         fc_remove_host(base_vha->host);
2147
2148         scsi_remove_host(base_vha->host);
2149
2150         qla2x00_free_device(base_vha);
2151
2152         scsi_host_put(base_vha->host);
2153
2154         if (ha->iobase)
2155                 iounmap(ha->iobase);
2156
2157         if (ha->mqiobase)
2158                 iounmap(ha->mqiobase);
2159
2160         pci_release_selected_regions(ha->pdev, ha->bars);
2161         kfree(ha);
2162         ha = NULL;
2163
2164         pci_disable_pcie_error_reporting(pdev);
2165
2166         pci_disable_device(pdev);
2167         pci_set_drvdata(pdev, NULL);
2168 }
2169
2170 static void
2171 qla2x00_free_device(scsi_qla_host_t *vha)
2172 {
2173         struct qla_hw_data *ha = vha->hw;
2174
2175         qla2x00_abort_all_cmds(vha, DID_NO_CONNECT << 16);
2176
2177         /* Disable timer */
2178         if (vha->timer_active)
2179                 qla2x00_stop_timer(vha);
2180
2181         /* Kill the kernel thread for this host */
2182         if (ha->dpc_thread) {
2183                 struct task_struct *t = ha->dpc_thread;
2184
2185                 /*
2186                  * qla2xxx_wake_dpc checks for ->dpc_thread
2187                  * so we need to zero it out.
2188                  */
2189                 ha->dpc_thread = NULL;
2190                 kthread_stop(t);
2191         }
2192
2193         qla25xx_delete_queues(vha);
2194
2195         if (ha->flags.fce_enabled)
2196                 qla2x00_disable_fce_trace(vha, NULL, NULL);
2197
2198         if (ha->eft)
2199                 qla2x00_disable_eft_trace(vha);
2200
2201         /* Stop currently executing firmware. */
2202         qla2x00_try_to_stop_firmware(vha);
2203
2204         vha->flags.online = 0;
2205
2206         /* turn-off interrupts on the card */
2207         if (ha->interrupts_on)
2208                 ha->isp_ops->disable_intrs(ha);
2209
2210         qla2x00_free_irqs(vha);
2211
2212         qla2x00_mem_free(ha);
2213
2214         qla2x00_free_queues(ha);
2215 }
2216
2217 static inline void
2218 qla2x00_schedule_rport_del(struct scsi_qla_host *vha, fc_port_t *fcport,
2219     int defer)
2220 {
2221         struct fc_rport *rport;
2222         scsi_qla_host_t *base_vha;
2223
2224         if (!fcport->rport)
2225                 return;
2226
2227         rport = fcport->rport;
2228         if (defer) {
2229                 base_vha = pci_get_drvdata(vha->hw->pdev);
2230                 spin_lock_irq(vha->host->host_lock);
2231                 fcport->drport = rport;
2232                 spin_unlock_irq(vha->host->host_lock);
2233                 set_bit(FCPORT_UPDATE_NEEDED, &base_vha->dpc_flags);
2234                 qla2xxx_wake_dpc(base_vha);
2235         } else
2236                 fc_remote_port_delete(rport);
2237 }
2238
2239 /*
2240  * qla2x00_mark_device_lost Updates fcport state when device goes offline.
2241  *
2242  * Input: ha = adapter block pointer.  fcport = port structure pointer.
2243  *
2244  * Return: None.
2245  *
2246  * Context:
2247  */
2248 void qla2x00_mark_device_lost(scsi_qla_host_t *vha, fc_port_t *fcport,
2249     int do_login, int defer)
2250 {
2251         if (atomic_read(&fcport->state) == FCS_ONLINE &&
2252             vha->vp_idx == fcport->vp_idx) {
2253                 atomic_set(&fcport->state, FCS_DEVICE_LOST);
2254                 qla2x00_schedule_rport_del(vha, fcport, defer);
2255         }
2256         /*
2257          * We may need to retry the login, so don't change the state of the
2258          * port but do the retries.
2259          */
2260         if (atomic_read(&fcport->state) != FCS_DEVICE_DEAD)
2261                 atomic_set(&fcport->state, FCS_DEVICE_LOST);
2262
2263         if (!do_login)
2264                 return;
2265
2266         if (fcport->login_retry == 0) {
2267                 fcport->login_retry = vha->hw->login_retry_count;
2268                 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
2269
2270                 DEBUG(printk("scsi(%ld): Port login retry: "
2271                     "%02x%02x%02x%02x%02x%02x%02x%02x, "
2272                     "id = 0x%04x retry cnt=%d\n",
2273                     vha->host_no,
2274                     fcport->port_name[0],
2275                     fcport->port_name[1],
2276                     fcport->port_name[2],
2277                     fcport->port_name[3],
2278                     fcport->port_name[4],
2279                     fcport->port_name[5],
2280                     fcport->port_name[6],
2281                     fcport->port_name[7],
2282                     fcport->loop_id,
2283                     fcport->login_retry));
2284         }
2285 }
2286
2287 /*
2288  * qla2x00_mark_all_devices_lost
2289  *      Updates fcport state when device goes offline.
2290  *
2291  * Input:
2292  *      ha = adapter block pointer.
2293  *      fcport = port structure pointer.
2294  *
2295  * Return:
2296  *      None.
2297  *
2298  * Context:
2299  */
2300 void
2301 qla2x00_mark_all_devices_lost(scsi_qla_host_t *vha, int defer)
2302 {
2303         fc_port_t *fcport;
2304
2305         list_for_each_entry(fcport, &vha->vp_fcports, list) {
2306                 if (vha->vp_idx != 0 && vha->vp_idx != fcport->vp_idx)
2307                         continue;
2308
2309                 /*
2310                  * No point in marking the device as lost, if the device is
2311                  * already DEAD.
2312                  */
2313                 if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD)
2314                         continue;
2315                 if (atomic_read(&fcport->state) == FCS_ONLINE) {
2316                         if (defer)
2317                                 qla2x00_schedule_rport_del(vha, fcport, defer);
2318                         else if (vha->vp_idx == fcport->vp_idx)
2319                                 qla2x00_schedule_rport_del(vha, fcport, defer);
2320                 }
2321                 atomic_set(&fcport->state, FCS_DEVICE_LOST);
2322         }
2323 }
2324
2325 /*
2326 * qla2x00_mem_alloc
2327 *      Allocates adapter memory.
2328 *
2329 * Returns:
2330 *      0  = success.
2331 *      !0  = failure.
2332 */
2333 static int
2334 qla2x00_mem_alloc(struct qla_hw_data *ha, uint16_t req_len, uint16_t rsp_len,
2335         struct req_que **req, struct rsp_que **rsp)
2336 {
2337         char    name[16];
2338
2339         ha->init_cb = dma_alloc_coherent(&ha->pdev->dev, ha->init_cb_size,
2340                 &ha->init_cb_dma, GFP_KERNEL);
2341         if (!ha->init_cb)
2342                 goto fail;
2343
2344         ha->gid_list = dma_alloc_coherent(&ha->pdev->dev, GID_LIST_SIZE,
2345                 &ha->gid_list_dma, GFP_KERNEL);
2346         if (!ha->gid_list)
2347                 goto fail_free_init_cb;
2348
2349         ha->srb_mempool = mempool_create_slab_pool(SRB_MIN_REQ, srb_cachep);
2350         if (!ha->srb_mempool)
2351                 goto fail_free_gid_list;
2352
2353         /* Get memory for cached NVRAM */
2354         ha->nvram = kzalloc(MAX_NVRAM_SIZE, GFP_KERNEL);
2355         if (!ha->nvram)
2356                 goto fail_free_srb_mempool;
2357
2358         snprintf(name, sizeof(name), "%s_%d", QLA2XXX_DRIVER_NAME,
2359                 ha->pdev->device);
2360         ha->s_dma_pool = dma_pool_create(name, &ha->pdev->dev,
2361                 DMA_POOL_SIZE, 8, 0);
2362         if (!ha->s_dma_pool)
2363                 goto fail_free_nvram;
2364
2365         /* Allocate memory for SNS commands */
2366         if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
2367         /* Get consistent memory allocated for SNS commands */
2368                 ha->sns_cmd = dma_alloc_coherent(&ha->pdev->dev,
2369                 sizeof(struct sns_cmd_pkt), &ha->sns_cmd_dma, GFP_KERNEL);
2370                 if (!ha->sns_cmd)
2371                         goto fail_dma_pool;
2372         } else {
2373         /* Get consistent memory allocated for MS IOCB */
2374                 ha->ms_iocb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
2375                         &ha->ms_iocb_dma);
2376                 if (!ha->ms_iocb)
2377                         goto fail_dma_pool;
2378         /* Get consistent memory allocated for CT SNS commands */
2379                 ha->ct_sns = dma_alloc_coherent(&ha->pdev->dev,
2380                         sizeof(struct ct_sns_pkt), &ha->ct_sns_dma, GFP_KERNEL);
2381                 if (!ha->ct_sns)
2382                         goto fail_free_ms_iocb;
2383         }
2384
2385         /* Allocate memory for request ring */
2386         *req = kzalloc(sizeof(struct req_que), GFP_KERNEL);
2387         if (!*req) {
2388                 DEBUG(printk("Unable to allocate memory for req\n"));
2389                 goto fail_req;
2390         }
2391         (*req)->length = req_len;
2392         (*req)->ring = dma_alloc_coherent(&ha->pdev->dev,
2393                 ((*req)->length + 1) * sizeof(request_t),
2394                 &(*req)->dma, GFP_KERNEL);
2395         if (!(*req)->ring) {
2396                 DEBUG(printk("Unable to allocate memory for req_ring\n"));
2397                 goto fail_req_ring;
2398         }
2399         /* Allocate memory for response ring */
2400         *rsp = kzalloc(sizeof(struct rsp_que), GFP_KERNEL);
2401         if (!*rsp) {
2402                 qla_printk(KERN_WARNING, ha,
2403                         "Unable to allocate memory for rsp\n");
2404                 goto fail_rsp;
2405         }
2406         (*rsp)->hw = ha;
2407         (*rsp)->length = rsp_len;
2408         (*rsp)->ring = dma_alloc_coherent(&ha->pdev->dev,
2409                 ((*rsp)->length + 1) * sizeof(response_t),
2410                 &(*rsp)->dma, GFP_KERNEL);
2411         if (!(*rsp)->ring) {
2412                 qla_printk(KERN_WARNING, ha,
2413                         "Unable to allocate memory for rsp_ring\n");
2414                 goto fail_rsp_ring;
2415         }
2416         (*req)->rsp = *rsp;
2417         (*rsp)->req = *req;
2418         /* Allocate memory for NVRAM data for vports */
2419         if (ha->nvram_npiv_size) {
2420                 ha->npiv_info = kzalloc(sizeof(struct qla_npiv_entry) *
2421                                         ha->nvram_npiv_size, GFP_KERNEL);
2422                 if (!ha->npiv_info) {
2423                         qla_printk(KERN_WARNING, ha,
2424                                 "Unable to allocate memory for npiv info\n");
2425                         goto fail_npiv_info;
2426                 }
2427         } else
2428                 ha->npiv_info = NULL;
2429
2430         /* Get consistent memory allocated for EX-INIT-CB. */
2431         if (IS_QLA81XX(ha)) {
2432                 ha->ex_init_cb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
2433                     &ha->ex_init_cb_dma);
2434                 if (!ha->ex_init_cb)
2435                         goto fail_ex_init_cb;
2436         }
2437
2438         INIT_LIST_HEAD(&ha->vp_list);
2439         return 1;
2440
2441 fail_ex_init_cb:
2442         kfree(ha->npiv_info);
2443 fail_npiv_info:
2444         dma_free_coherent(&ha->pdev->dev, ((*rsp)->length + 1) *
2445                 sizeof(response_t), (*rsp)->ring, (*rsp)->dma);
2446         (*rsp)->ring = NULL;
2447         (*rsp)->dma = 0;
2448 fail_rsp_ring:
2449         kfree(*rsp);
2450 fail_rsp:
2451         dma_free_coherent(&ha->pdev->dev, ((*req)->length + 1) *
2452                 sizeof(request_t), (*req)->ring, (*req)->dma);
2453         (*req)->ring = NULL;
2454         (*req)->dma = 0;
2455 fail_req_ring:
2456         kfree(*req);
2457 fail_req:
2458         dma_free_coherent(&ha->pdev->dev, sizeof(struct ct_sns_pkt),
2459                 ha->ct_sns, ha->ct_sns_dma);
2460         ha->ct_sns = NULL;
2461         ha->ct_sns_dma = 0;
2462 fail_free_ms_iocb:
2463         dma_pool_free(ha->s_dma_pool, ha->ms_iocb, ha->ms_iocb_dma);
2464         ha->ms_iocb = NULL;
2465         ha->ms_iocb_dma = 0;
2466 fail_dma_pool:
2467         dma_pool_destroy(ha->s_dma_pool);
2468         ha->s_dma_pool = NULL;
2469 fail_free_nvram:
2470         kfree(ha->nvram);
2471         ha->nvram = NULL;
2472 fail_free_srb_mempool:
2473         mempool_destroy(ha->srb_mempool);
2474         ha->srb_mempool = NULL;
2475 fail_free_gid_list:
2476         dma_free_coherent(&ha->pdev->dev, GID_LIST_SIZE, ha->gid_list,
2477         ha->gid_list_dma);
2478         ha->gid_list = NULL;
2479         ha->gid_list_dma = 0;
2480 fail_free_init_cb:
2481         dma_free_coherent(&ha->pdev->dev, ha->init_cb_size, ha->init_cb,
2482         ha->init_cb_dma);
2483         ha->init_cb = NULL;
2484         ha->init_cb_dma = 0;
2485 fail:
2486         DEBUG(printk("%s: Memory allocation failure\n", __func__));
2487         return -ENOMEM;
2488 }
2489
2490 /*
2491 * qla2x00_mem_free
2492 *      Frees all adapter allocated memory.
2493 *
2494 * Input:
2495 *      ha = adapter block pointer.
2496 */
2497 static void
2498 qla2x00_mem_free(struct qla_hw_data *ha)
2499 {
2500         if (ha->srb_mempool)
2501                 mempool_destroy(ha->srb_mempool);
2502
2503         if (ha->fce)
2504                 dma_free_coherent(&ha->pdev->dev, FCE_SIZE, ha->fce,
2505                 ha->fce_dma);
2506
2507         if (ha->fw_dump) {
2508                 if (ha->eft)
2509                         dma_free_coherent(&ha->pdev->dev,
2510                         ntohl(ha->fw_dump->eft_size), ha->eft, ha->eft_dma);
2511                 vfree(ha->fw_dump);
2512         }
2513
2514         if (ha->dcbx_tlv)
2515                 dma_free_coherent(&ha->pdev->dev, DCBX_TLV_DATA_SIZE,
2516                     ha->dcbx_tlv, ha->dcbx_tlv_dma);
2517
2518         if (ha->xgmac_data)
2519                 dma_free_coherent(&ha->pdev->dev, XGMAC_DATA_SIZE,
2520                     ha->xgmac_data, ha->xgmac_data_dma);
2521
2522         if (ha->sns_cmd)
2523                 dma_free_coherent(&ha->pdev->dev, sizeof(struct sns_cmd_pkt),
2524                 ha->sns_cmd, ha->sns_cmd_dma);
2525
2526         if (ha->ct_sns)
2527                 dma_free_coherent(&ha->pdev->dev, sizeof(struct ct_sns_pkt),
2528                 ha->ct_sns, ha->ct_sns_dma);
2529
2530         if (ha->sfp_data)
2531                 dma_pool_free(ha->s_dma_pool, ha->sfp_data, ha->sfp_data_dma);
2532
2533         if (ha->edc_data)
2534                 dma_pool_free(ha->s_dma_pool, ha->edc_data, ha->edc_data_dma);
2535
2536         if (ha->ms_iocb)
2537                 dma_pool_free(ha->s_dma_pool, ha->ms_iocb, ha->ms_iocb_dma);
2538
2539         if (ha->ex_init_cb)
2540                 dma_pool_free(ha->s_dma_pool, ha->ex_init_cb, ha->ex_init_cb_dma);
2541
2542         if (ha->s_dma_pool)
2543                 dma_pool_destroy(ha->s_dma_pool);
2544
2545         if (ha->gid_list)
2546                 dma_free_coherent(&ha->pdev->dev, GID_LIST_SIZE, ha->gid_list,
2547                 ha->gid_list_dma);
2548
2549         if (ha->init_cb)
2550                 dma_free_coherent(&ha->pdev->dev, ha->init_cb_size,
2551                 ha->init_cb, ha->init_cb_dma);
2552         vfree(ha->optrom_buffer);
2553         kfree(ha->nvram);
2554         kfree(ha->npiv_info);
2555
2556         ha->srb_mempool = NULL;
2557         ha->eft = NULL;
2558         ha->eft_dma = 0;
2559         ha->sns_cmd = NULL;
2560         ha->sns_cmd_dma = 0;
2561         ha->ct_sns = NULL;
2562         ha->ct_sns_dma = 0;
2563         ha->ms_iocb = NULL;
2564         ha->ms_iocb_dma = 0;
2565         ha->init_cb = NULL;
2566         ha->init_cb_dma = 0;
2567         ha->ex_init_cb = NULL;
2568         ha->ex_init_cb_dma = 0;
2569
2570         ha->s_dma_pool = NULL;
2571
2572         ha->gid_list = NULL;
2573         ha->gid_list_dma = 0;
2574
2575         ha->fw_dump = NULL;
2576         ha->fw_dumped = 0;
2577         ha->fw_dump_reading = 0;
2578 }
2579
2580 struct scsi_qla_host *qla2x00_create_host(struct scsi_host_template *sht,
2581                                                 struct qla_hw_data *ha)
2582 {
2583         struct Scsi_Host *host;
2584         struct scsi_qla_host *vha = NULL;
2585
2586         host = scsi_host_alloc(sht, sizeof(scsi_qla_host_t));
2587         if (host == NULL) {
2588                 printk(KERN_WARNING
2589                 "qla2xxx: Couldn't allocate host from scsi layer!\n");
2590                 goto fail;
2591         }
2592
2593         /* Clear our data area */
2594         vha = shost_priv(host);
2595         memset(vha, 0, sizeof(scsi_qla_host_t));
2596
2597         vha->host = host;
2598         vha->host_no = host->host_no;
2599         vha->hw = ha;
2600
2601         INIT_LIST_HEAD(&vha->vp_fcports);
2602         INIT_LIST_HEAD(&vha->work_list);
2603         INIT_LIST_HEAD(&vha->list);
2604
2605         spin_lock_init(&vha->work_lock);
2606
2607         sprintf(vha->host_str, "%s_%ld", QLA2XXX_DRIVER_NAME, vha->host_no);
2608         return vha;
2609
2610 fail:
2611         return vha;
2612 }
2613
2614 static struct qla_work_evt *
2615 qla2x00_alloc_work(struct scsi_qla_host *vha, enum qla_work_type type)
2616 {
2617         struct qla_work_evt *e;
2618
2619         e = kzalloc(sizeof(struct qla_work_evt), GFP_ATOMIC);
2620         if (!e)
2621                 return NULL;
2622
2623         INIT_LIST_HEAD(&e->list);
2624         e->type = type;
2625         e->flags = QLA_EVT_FLAG_FREE;
2626         return e;
2627 }
2628
2629 static int
2630 qla2x00_post_work(struct scsi_qla_host *vha, struct qla_work_evt *e)
2631 {
2632         unsigned long flags;
2633
2634         spin_lock_irqsave(&vha->work_lock, flags);
2635         list_add_tail(&e->list, &vha->work_list);
2636         spin_unlock_irqrestore(&vha->work_lock, flags);
2637         qla2xxx_wake_dpc(vha);
2638
2639         return QLA_SUCCESS;
2640 }
2641
2642 int
2643 qla2x00_post_aen_work(struct scsi_qla_host *vha, enum fc_host_event_code code,
2644     u32 data)
2645 {
2646         struct qla_work_evt *e;
2647
2648         e = qla2x00_alloc_work(vha, QLA_EVT_AEN);
2649         if (!e)
2650                 return QLA_FUNCTION_FAILED;
2651
2652         e->u.aen.code = code;
2653         e->u.aen.data = data;
2654         return qla2x00_post_work(vha, e);
2655 }
2656
2657 int
2658 qla2x00_post_idc_ack_work(struct scsi_qla_host *vha, uint16_t *mb)
2659 {
2660         struct qla_work_evt *e;
2661
2662         e = qla2x00_alloc_work(vha, QLA_EVT_IDC_ACK);
2663         if (!e)
2664                 return QLA_FUNCTION_FAILED;
2665
2666         memcpy(e->u.idc_ack.mb, mb, QLA_IDC_ACK_REGS * sizeof(uint16_t));
2667         return qla2x00_post_work(vha, e);
2668 }
2669
2670 #define qla2x00_post_async_work(name, type)     \
2671 int qla2x00_post_async_##name##_work(           \
2672     struct scsi_qla_host *vha,                  \
2673     fc_port_t *fcport, uint16_t *data)          \
2674 {                                               \
2675         struct qla_work_evt *e;                 \
2676                                                 \
2677         e = qla2x00_alloc_work(vha, type);      \
2678         if (!e)                                 \
2679                 return QLA_FUNCTION_FAILED;     \
2680                                                 \
2681         e->u.logio.fcport = fcport;             \
2682         if (data) {                             \
2683                 e->u.logio.data[0] = data[0];   \
2684                 e->u.logio.data[1] = data[1];   \
2685         }                                       \
2686         return qla2x00_post_work(vha, e);       \
2687 }
2688
2689 qla2x00_post_async_work(login, QLA_EVT_ASYNC_LOGIN);
2690 qla2x00_post_async_work(login_done, QLA_EVT_ASYNC_LOGIN_DONE);
2691 qla2x00_post_async_work(logout, QLA_EVT_ASYNC_LOGOUT);
2692 qla2x00_post_async_work(logout_done, QLA_EVT_ASYNC_LOGOUT_DONE);
2693
2694 int
2695 qla2x00_post_uevent_work(struct scsi_qla_host *vha, u32 code)
2696 {
2697         struct qla_work_evt *e;
2698
2699         e = qla2x00_alloc_work(vha, QLA_EVT_UEVENT);
2700         if (!e)
2701                 return QLA_FUNCTION_FAILED;
2702
2703         e->u.uevent.code = code;
2704         return qla2x00_post_work(vha, e);
2705 }
2706
2707 static void
2708 qla2x00_uevent_emit(struct scsi_qla_host *vha, u32 code)
2709 {
2710         char event_string[40];
2711         char *envp[] = { event_string, NULL };
2712
2713         switch (code) {
2714         case QLA_UEVENT_CODE_FW_DUMP:
2715                 snprintf(event_string, sizeof(event_string), "FW_DUMP=%ld",
2716                     vha->host_no);
2717                 break;
2718         default:
2719                 /* do nothing */
2720                 break;
2721         }
2722         kobject_uevent_env(&vha->hw->pdev->dev.kobj, KOBJ_CHANGE, envp);
2723 }
2724
2725 void
2726 qla2x00_do_work(struct scsi_qla_host *vha)
2727 {
2728         struct qla_work_evt *e, *tmp;
2729         unsigned long flags;
2730         LIST_HEAD(work);
2731
2732         spin_lock_irqsave(&vha->work_lock, flags);
2733         list_splice_init(&vha->work_list, &work);
2734         spin_unlock_irqrestore(&vha->work_lock, flags);
2735
2736         list_for_each_entry_safe(e, tmp, &work, list) {
2737                 list_del_init(&e->list);
2738
2739                 switch (e->type) {
2740                 case QLA_EVT_AEN:
2741                         fc_host_post_event(vha->host, fc_get_event_number(),
2742                             e->u.aen.code, e->u.aen.data);
2743                         break;
2744                 case QLA_EVT_IDC_ACK:
2745                         qla81xx_idc_ack(vha, e->u.idc_ack.mb);
2746                         break;
2747                 case QLA_EVT_ASYNC_LOGIN:
2748                         qla2x00_async_login(vha, e->u.logio.fcport,
2749                             e->u.logio.data);
2750                         break;
2751                 case QLA_EVT_ASYNC_LOGIN_DONE:
2752                         qla2x00_async_login_done(vha, e->u.logio.fcport,
2753                             e->u.logio.data);
2754                         break;
2755                 case QLA_EVT_ASYNC_LOGOUT:
2756                         qla2x00_async_logout(vha, e->u.logio.fcport);
2757                         break;
2758                 case QLA_EVT_ASYNC_LOGOUT_DONE:
2759                         qla2x00_async_logout_done(vha, e->u.logio.fcport,
2760                             e->u.logio.data);
2761                         break;
2762                 case QLA_EVT_UEVENT:
2763                         qla2x00_uevent_emit(vha, e->u.uevent.code);
2764                         break;
2765                 }
2766                 if (e->flags & QLA_EVT_FLAG_FREE)
2767                         kfree(e);
2768         }
2769 }
2770
2771 /* Relogins all the fcports of a vport
2772  * Context: dpc thread
2773  */
2774 void qla2x00_relogin(struct scsi_qla_host *vha)
2775 {
2776         fc_port_t       *fcport;
2777         int status;
2778         uint16_t        next_loopid = 0;
2779         struct qla_hw_data *ha = vha->hw;
2780         uint16_t data[2];
2781
2782         list_for_each_entry(fcport, &vha->vp_fcports, list) {
2783         /*
2784          * If the port is not ONLINE then try to login
2785          * to it if we haven't run out of retries.
2786          */
2787                 if (atomic_read(&fcport->state) !=
2788                         FCS_ONLINE && fcport->login_retry) {
2789
2790                         fcport->login_retry--;
2791                         if (fcport->flags & FCF_FABRIC_DEVICE) {
2792                                 if (fcport->flags & FCF_FCP2_DEVICE)
2793                                         ha->isp_ops->fabric_logout(vha,
2794                                                         fcport->loop_id,
2795                                                         fcport->d_id.b.domain,
2796                                                         fcport->d_id.b.area,
2797                                                         fcport->d_id.b.al_pa);
2798
2799                                 if (IS_ALOGIO_CAPABLE(ha)) {
2800                                         data[0] = 0;
2801                                         data[1] = QLA_LOGIO_LOGIN_RETRIED;
2802                                         status = qla2x00_post_async_login_work(
2803                                             vha, fcport, data);
2804                                         if (status == QLA_SUCCESS)
2805                                                 continue;
2806                                         /* Attempt a retry. */
2807                                         status = 1;
2808                                 } else
2809                                         status = qla2x00_fabric_login(vha,
2810                                             fcport, &next_loopid);
2811                         } else
2812                                 status = qla2x00_local_device_login(vha,
2813                                                                 fcport);
2814
2815                         if (status == QLA_SUCCESS) {
2816                                 fcport->old_loop_id = fcport->loop_id;
2817
2818                                 DEBUG(printk("scsi(%ld): port login OK: logged "
2819                                 "in ID 0x%x\n", vha->host_no, fcport->loop_id));
2820
2821                                 qla2x00_update_fcport(vha, fcport);
2822
2823                         } else if (status == 1) {
2824                                 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
2825                                 /* retry the login again */
2826                                 DEBUG(printk("scsi(%ld): Retrying"
2827                                 " %d login again loop_id 0x%x\n",
2828                                 vha->host_no, fcport->login_retry,
2829                                                 fcport->loop_id));
2830                         } else {
2831                                 fcport->login_retry = 0;
2832                         }
2833
2834                         if (fcport->login_retry == 0 && status != QLA_SUCCESS)
2835                                 fcport->loop_id = FC_NO_LOOP_ID;
2836                 }
2837                 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
2838                         break;
2839         }
2840 }
2841
2842 /**************************************************************************
2843 * qla2x00_do_dpc
2844 *   This kernel thread is a task that is schedule by the interrupt handler
2845 *   to perform the background processing for interrupts.
2846 *
2847 * Notes:
2848 * This task always run in the context of a kernel thread.  It
2849 * is kick-off by the driver's detect code and starts up
2850 * up one per adapter. It immediately goes to sleep and waits for
2851 * some fibre event.  When either the interrupt handler or
2852 * the timer routine detects a event it will one of the task
2853 * bits then wake us up.
2854 **************************************************************************/
2855 static int
2856 qla2x00_do_dpc(void *data)
2857 {
2858         int             rval;
2859         scsi_qla_host_t *base_vha;
2860         struct qla_hw_data *ha;
2861
2862         ha = (struct qla_hw_data *)data;
2863         base_vha = pci_get_drvdata(ha->pdev);
2864
2865         set_user_nice(current, -20);
2866
2867         while (!kthread_should_stop()) {
2868                 DEBUG3(printk("qla2x00: DPC handler sleeping\n"));
2869
2870                 set_current_state(TASK_INTERRUPTIBLE);
2871                 schedule();
2872                 __set_current_state(TASK_RUNNING);
2873
2874                 DEBUG3(printk("qla2x00: DPC handler waking up\n"));
2875
2876                 /* Initialization not yet finished. Don't do anything yet. */
2877                 if (!base_vha->flags.init_done)
2878                         continue;
2879
2880                 if (ha->flags.eeh_busy) {
2881                         DEBUG17(qla_printk(KERN_WARNING, ha,
2882                             "qla2x00_do_dpc: dpc_flags: %lx\n",
2883                             base_vha->dpc_flags));
2884                         continue;
2885                 }
2886
2887                 DEBUG3(printk("scsi(%ld): DPC handler\n", base_vha->host_no));
2888
2889                 ha->dpc_active = 1;
2890
2891                 if (ha->flags.mbox_busy) {
2892                         ha->dpc_active = 0;
2893                         continue;
2894                 }
2895
2896                 qla2x00_do_work(base_vha);
2897
2898                 if (test_and_clear_bit(ISP_ABORT_NEEDED,
2899                                                 &base_vha->dpc_flags)) {
2900
2901                         DEBUG(printk("scsi(%ld): dpc: sched "
2902                             "qla2x00_abort_isp ha = %p\n",
2903                             base_vha->host_no, ha));
2904                         if (!(test_and_set_bit(ABORT_ISP_ACTIVE,
2905                             &base_vha->dpc_flags))) {
2906
2907                                 if (qla2x00_abort_isp(base_vha)) {
2908                                         /* failed. retry later */
2909                                         set_bit(ISP_ABORT_NEEDED,
2910                                             &base_vha->dpc_flags);
2911                                 }
2912                                 clear_bit(ABORT_ISP_ACTIVE,
2913                                                 &base_vha->dpc_flags);
2914                         }
2915
2916                         DEBUG(printk("scsi(%ld): dpc: qla2x00_abort_isp end\n",
2917                             base_vha->host_no));
2918                 }
2919
2920                 if (test_bit(FCPORT_UPDATE_NEEDED, &base_vha->dpc_flags)) {
2921                         qla2x00_update_fcports(base_vha);
2922                         clear_bit(FCPORT_UPDATE_NEEDED, &base_vha->dpc_flags);
2923                 }
2924
2925                 if (test_and_clear_bit(RESET_MARKER_NEEDED,
2926                                                         &base_vha->dpc_flags) &&
2927                     (!(test_and_set_bit(RESET_ACTIVE, &base_vha->dpc_flags)))) {
2928
2929                         DEBUG(printk("scsi(%ld): qla2x00_reset_marker()\n",
2930                             base_vha->host_no));
2931
2932                         qla2x00_rst_aen(base_vha);
2933                         clear_bit(RESET_ACTIVE, &base_vha->dpc_flags);
2934                 }
2935
2936                 /* Retry each device up to login retry count */
2937                 if ((test_and_clear_bit(RELOGIN_NEEDED,
2938                                                 &base_vha->dpc_flags)) &&
2939                     !test_bit(LOOP_RESYNC_NEEDED, &base_vha->dpc_flags) &&
2940                     atomic_read(&base_vha->loop_state) != LOOP_DOWN) {
2941
2942                         DEBUG(printk("scsi(%ld): qla2x00_port_login()\n",
2943                                         base_vha->host_no));
2944                         qla2x00_relogin(base_vha);
2945
2946                         DEBUG(printk("scsi(%ld): qla2x00_port_login - end\n",
2947                             base_vha->host_no));
2948                 }
2949
2950                 if (test_and_clear_bit(LOOP_RESYNC_NEEDED,
2951                                                         &base_vha->dpc_flags)) {
2952
2953                         DEBUG(printk("scsi(%ld): qla2x00_loop_resync()\n",
2954                                 base_vha->host_no));
2955
2956                         if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE,
2957                             &base_vha->dpc_flags))) {
2958
2959                                 rval = qla2x00_loop_resync(base_vha);
2960
2961                                 clear_bit(LOOP_RESYNC_ACTIVE,
2962                                                 &base_vha->dpc_flags);
2963                         }
2964
2965                         DEBUG(printk("scsi(%ld): qla2x00_loop_resync - end\n",
2966                             base_vha->host_no));
2967                 }
2968
2969                 if (test_bit(NPIV_CONFIG_NEEDED, &base_vha->dpc_flags) &&
2970                     atomic_read(&base_vha->loop_state) == LOOP_READY) {
2971                         clear_bit(NPIV_CONFIG_NEEDED, &base_vha->dpc_flags);
2972                         qla2xxx_flash_npiv_conf(base_vha);
2973                 }
2974
2975                 if (!ha->interrupts_on)
2976                         ha->isp_ops->enable_intrs(ha);
2977
2978                 if (test_and_clear_bit(BEACON_BLINK_NEEDED,
2979                                         &base_vha->dpc_flags))
2980                         ha->isp_ops->beacon_blink(base_vha);
2981
2982                 qla2x00_do_dpc_all_vps(base_vha);
2983
2984                 ha->dpc_active = 0;
2985         } /* End of while(1) */
2986
2987         DEBUG(printk("scsi(%ld): DPC handler exiting\n", base_vha->host_no));
2988
2989         /*
2990          * Make sure that nobody tries to wake us up again.
2991          */
2992         ha->dpc_active = 0;
2993
2994         /* Cleanup any residual CTX SRBs. */
2995         qla2x00_abort_all_cmds(base_vha, DID_NO_CONNECT << 16);
2996
2997         return 0;
2998 }
2999
3000 void
3001 qla2xxx_wake_dpc(struct scsi_qla_host *vha)
3002 {
3003         struct qla_hw_data *ha = vha->hw;
3004         struct task_struct *t = ha->dpc_thread;
3005
3006         if (!test_bit(UNLOADING, &vha->dpc_flags) && t)
3007                 wake_up_process(t);
3008 }
3009
3010 /*
3011 *  qla2x00_rst_aen
3012 *      Processes asynchronous reset.
3013 *
3014 * Input:
3015 *      ha  = adapter block pointer.
3016 */
3017 static void
3018 qla2x00_rst_aen(scsi_qla_host_t *vha)
3019 {
3020         if (vha->flags.online && !vha->flags.reset_active &&
3021             !atomic_read(&vha->loop_down_timer) &&
3022             !(test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))) {
3023                 do {
3024                         clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
3025
3026                         /*
3027                          * Issue marker command only when we are going to start
3028                          * the I/O.
3029                          */
3030                         vha->marker_needed = 1;
3031                 } while (!atomic_read(&vha->loop_down_timer) &&
3032                     (test_bit(RESET_MARKER_NEEDED, &vha->dpc_flags)));
3033         }
3034 }
3035
3036 static void
3037 qla2x00_sp_free_dma(srb_t *sp)
3038 {
3039         struct scsi_cmnd *cmd = sp->cmd;
3040
3041         if (sp->flags & SRB_DMA_VALID) {
3042                 scsi_dma_unmap(cmd);
3043                 sp->flags &= ~SRB_DMA_VALID;
3044         }
3045         CMD_SP(cmd) = NULL;
3046 }
3047
3048 void
3049 qla2x00_sp_compl(struct qla_hw_data *ha, srb_t *sp)
3050 {
3051         struct scsi_cmnd *cmd = sp->cmd;
3052
3053         qla2x00_sp_free_dma(sp);
3054
3055         mempool_free(sp, ha->srb_mempool);
3056
3057         cmd->scsi_done(cmd);
3058 }
3059
3060 /**************************************************************************
3061 *   qla2x00_timer
3062 *
3063 * Description:
3064 *   One second timer
3065 *
3066 * Context: Interrupt
3067 ***************************************************************************/
3068 void
3069 qla2x00_timer(scsi_qla_host_t *vha)
3070 {
3071         unsigned long   cpu_flags = 0;
3072         fc_port_t       *fcport;
3073         int             start_dpc = 0;
3074         int             index;
3075         srb_t           *sp;
3076         int             t;
3077         uint16_t        w;
3078         struct qla_hw_data *ha = vha->hw;
3079         struct req_que *req;
3080
3081         /* Hardware read to raise pending EEH errors during mailbox waits. */
3082         if (!pci_channel_offline(ha->pdev))
3083                 pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
3084         /*
3085          * Ports - Port down timer.
3086          *
3087          * Whenever, a port is in the LOST state we start decrementing its port
3088          * down timer every second until it reaches zero. Once  it reaches zero
3089          * the port it marked DEAD.
3090          */
3091         t = 0;
3092         list_for_each_entry(fcport, &vha->vp_fcports, list) {
3093                 if (fcport->port_type != FCT_TARGET)
3094                         continue;
3095
3096                 if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
3097
3098                         if (atomic_read(&fcport->port_down_timer) == 0)
3099                                 continue;
3100
3101                         if (atomic_dec_and_test(&fcport->port_down_timer) != 0)
3102                                 atomic_set(&fcport->state, FCS_DEVICE_DEAD);
3103
3104                         DEBUG(printk("scsi(%ld): fcport-%d - port retry count: "
3105                             "%d remaining\n",
3106                             vha->host_no,
3107                             t, atomic_read(&fcport->port_down_timer)));
3108                 }
3109                 t++;
3110         } /* End of for fcport  */
3111
3112
3113         /* Loop down handler. */
3114         if (atomic_read(&vha->loop_down_timer) > 0 &&
3115             !(test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))
3116                 && vha->flags.online) {
3117
3118                 if (atomic_read(&vha->loop_down_timer) ==
3119                     vha->loop_down_abort_time) {
3120
3121                         DEBUG(printk("scsi(%ld): Loop Down - aborting the "
3122                             "queues before time expire\n",
3123                             vha->host_no));
3124
3125                         if (!IS_QLA2100(ha) && vha->link_down_timeout)
3126                                 atomic_set(&vha->loop_state, LOOP_DEAD);
3127
3128                         /*
3129                          * Schedule an ISP abort to return any FCP2-device
3130                          * commands.
3131                          */
3132                         /* NPIV - scan physical port only */
3133                         if (!vha->vp_idx) {
3134                                 spin_lock_irqsave(&ha->hardware_lock,
3135                                     cpu_flags);
3136                                 req = ha->req_q_map[0];
3137                                 for (index = 1;
3138                                     index < MAX_OUTSTANDING_COMMANDS;
3139                                     index++) {
3140                                         fc_port_t *sfcp;
3141
3142                                         sp = req->outstanding_cmds[index];
3143                                         if (!sp)
3144                                                 continue;
3145                                         if (sp->ctx)
3146                                                 continue;
3147                                         sfcp = sp->fcport;
3148                                         if (!(sfcp->flags & FCF_FCP2_DEVICE))
3149                                                 continue;
3150
3151                                         set_bit(ISP_ABORT_NEEDED,
3152                                                         &vha->dpc_flags);
3153                                         break;
3154                                 }
3155                                 spin_unlock_irqrestore(&ha->hardware_lock,
3156                                                                 cpu_flags);
3157                         }
3158                         start_dpc++;
3159                 }
3160
3161                 /* if the loop has been down for 4 minutes, reinit adapter */
3162                 if (atomic_dec_and_test(&vha->loop_down_timer) != 0) {
3163                         if (!(vha->device_flags & DFLG_NO_CABLE)) {
3164                                 DEBUG(printk("scsi(%ld): Loop down - "
3165                                     "aborting ISP.\n",
3166                                     vha->host_no));
3167                                 qla_printk(KERN_WARNING, ha,
3168                                     "Loop down - aborting ISP.\n");
3169
3170                                 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3171                         }
3172                 }
3173                 DEBUG3(printk("scsi(%ld): Loop Down - seconds remaining %d\n",
3174                     vha->host_no,
3175                     atomic_read(&vha->loop_down_timer)));
3176         }
3177
3178         /* Check if beacon LED needs to be blinked */
3179         if (ha->beacon_blink_led == 1) {
3180                 set_bit(BEACON_BLINK_NEEDED, &vha->dpc_flags);
3181                 start_dpc++;
3182         }
3183
3184         /* Process any deferred work. */
3185         if (!list_empty(&vha->work_list))
3186                 start_dpc++;
3187
3188         /* Schedule the DPC routine if needed */
3189         if ((test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
3190             test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags) ||
3191             test_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags) ||
3192             start_dpc ||
3193             test_bit(RESET_MARKER_NEEDED, &vha->dpc_flags) ||
3194             test_bit(BEACON_BLINK_NEEDED, &vha->dpc_flags) ||
3195             test_bit(VP_DPC_NEEDED, &vha->dpc_flags) ||
3196             test_bit(RELOGIN_NEEDED, &vha->dpc_flags)))
3197                 qla2xxx_wake_dpc(vha);
3198
3199         qla2x00_restart_timer(vha, WATCH_INTERVAL);
3200 }
3201
3202 /* Firmware interface routines. */
3203
3204 #define FW_BLOBS        7
3205 #define FW_ISP21XX      0
3206 #define FW_ISP22XX      1
3207 #define FW_ISP2300      2
3208 #define FW_ISP2322      3
3209 #define FW_ISP24XX      4
3210 #define FW_ISP25XX      5
3211 #define FW_ISP81XX      6
3212
3213 #define FW_FILE_ISP21XX "ql2100_fw.bin"
3214 #define FW_FILE_ISP22XX "ql2200_fw.bin"
3215 #define FW_FILE_ISP2300 "ql2300_fw.bin"
3216 #define FW_FILE_ISP2322 "ql2322_fw.bin"
3217 #define FW_FILE_ISP24XX "ql2400_fw.bin"
3218 #define FW_FILE_ISP25XX "ql2500_fw.bin"
3219 #define FW_FILE_ISP81XX "ql8100_fw.bin"
3220
3221 static DEFINE_MUTEX(qla_fw_lock);
3222
3223 static struct fw_blob qla_fw_blobs[FW_BLOBS] = {
3224         { .name = FW_FILE_ISP21XX, .segs = { 0x1000, 0 }, },
3225         { .name = FW_FILE_ISP22XX, .segs = { 0x1000, 0 }, },
3226         { .name = FW_FILE_ISP2300, .segs = { 0x800, 0 }, },
3227         { .name = FW_FILE_ISP2322, .segs = { 0x800, 0x1c000, 0x1e000, 0 }, },
3228         { .name = FW_FILE_ISP24XX, },
3229         { .name = FW_FILE_ISP25XX, },
3230         { .name = FW_FILE_ISP81XX, },
3231 };
3232
3233 struct fw_blob *
3234 qla2x00_request_firmware(scsi_qla_host_t *vha)
3235 {
3236         struct qla_hw_data *ha = vha->hw;
3237         struct fw_blob *blob;
3238
3239         blob = NULL;
3240         if (IS_QLA2100(ha)) {
3241                 blob = &qla_fw_blobs[FW_ISP21XX];
3242         } else if (IS_QLA2200(ha)) {
3243                 blob = &qla_fw_blobs[FW_ISP22XX];
3244         } else if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
3245                 blob = &qla_fw_blobs[FW_ISP2300];
3246         } else if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
3247                 blob = &qla_fw_blobs[FW_ISP2322];
3248         } else if (IS_QLA24XX_TYPE(ha)) {
3249                 blob = &qla_fw_blobs[FW_ISP24XX];
3250         } else if (IS_QLA25XX(ha)) {
3251                 blob = &qla_fw_blobs[FW_ISP25XX];
3252         } else if (IS_QLA81XX(ha)) {
3253                 blob = &qla_fw_blobs[FW_ISP81XX];
3254         }
3255
3256         mutex_lock(&qla_fw_lock);
3257         if (blob->fw)
3258                 goto out;
3259
3260         if (request_firmware(&blob->fw, blob->name, &ha->pdev->dev)) {
3261                 DEBUG2(printk("scsi(%ld): Failed to load firmware image "
3262                     "(%s).\n", vha->host_no, blob->name));
3263                 blob->fw = NULL;
3264                 blob = NULL;
3265                 goto out;
3266         }
3267
3268 out:
3269         mutex_unlock(&qla_fw_lock);
3270         return blob;
3271 }
3272
3273 static void
3274 qla2x00_release_firmware(void)
3275 {
3276         int idx;
3277
3278         mutex_lock(&qla_fw_lock);
3279         for (idx = 0; idx < FW_BLOBS; idx++)
3280                 if (qla_fw_blobs[idx].fw)
3281                         release_firmware(qla_fw_blobs[idx].fw);
3282         mutex_unlock(&qla_fw_lock);
3283 }
3284
3285 static pci_ers_result_t
3286 qla2xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
3287 {
3288         scsi_qla_host_t *vha = pci_get_drvdata(pdev);
3289         struct qla_hw_data *ha = vha->hw;
3290
3291         DEBUG2(qla_printk(KERN_WARNING, ha, "error_detected:state %x\n",
3292             state));
3293
3294         switch (state) {
3295         case pci_channel_io_normal:
3296                 ha->flags.eeh_busy = 0;
3297                 return PCI_ERS_RESULT_CAN_RECOVER;
3298         case pci_channel_io_frozen:
3299                 ha->flags.eeh_busy = 1;
3300                 qla2x00_free_irqs(vha);
3301                 pci_disable_device(pdev);
3302                 return PCI_ERS_RESULT_NEED_RESET;
3303         case pci_channel_io_perm_failure:
3304                 ha->flags.pci_channel_io_perm_failure = 1;
3305                 qla2x00_abort_all_cmds(vha, DID_NO_CONNECT << 16);
3306                 return PCI_ERS_RESULT_DISCONNECT;
3307         }
3308         return PCI_ERS_RESULT_NEED_RESET;
3309 }
3310
3311 static pci_ers_result_t
3312 qla2xxx_pci_mmio_enabled(struct pci_dev *pdev)
3313 {
3314         int risc_paused = 0;
3315         uint32_t stat;
3316         unsigned long flags;
3317         scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
3318         struct qla_hw_data *ha = base_vha->hw;
3319         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
3320         struct device_reg_24xx __iomem *reg24 = &ha->iobase->isp24;
3321
3322         spin_lock_irqsave(&ha->hardware_lock, flags);
3323         if (IS_QLA2100(ha) || IS_QLA2200(ha)){
3324                 stat = RD_REG_DWORD(&reg->hccr);
3325                 if (stat & HCCR_RISC_PAUSE)
3326                         risc_paused = 1;
3327         } else if (IS_QLA23XX(ha)) {
3328                 stat = RD_REG_DWORD(&reg->u.isp2300.host_status);
3329                 if (stat & HSR_RISC_PAUSED)
3330                         risc_paused = 1;
3331         } else if (IS_FWI2_CAPABLE(ha)) {
3332                 stat = RD_REG_DWORD(&reg24->host_status);
3333                 if (stat & HSRX_RISC_PAUSED)
3334                         risc_paused = 1;
3335         }
3336         spin_unlock_irqrestore(&ha->hardware_lock, flags);
3337
3338         if (risc_paused) {
3339                 qla_printk(KERN_INFO, ha, "RISC paused -- mmio_enabled, "
3340                     "Dumping firmware!\n");
3341                 ha->isp_ops->fw_dump(base_vha, 0);
3342
3343                 return PCI_ERS_RESULT_NEED_RESET;
3344         } else
3345                 return PCI_ERS_RESULT_RECOVERED;
3346 }
3347
3348 static pci_ers_result_t
3349 qla2xxx_pci_slot_reset(struct pci_dev *pdev)
3350 {
3351         pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
3352         scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
3353         struct qla_hw_data *ha = base_vha->hw;
3354         struct rsp_que *rsp;
3355         int rc, retries = 10;
3356
3357         DEBUG17(qla_printk(KERN_WARNING, ha, "slot_reset\n"));
3358
3359         /* Workaround: qla2xxx driver which access hardware earlier
3360          * needs error state to be pci_channel_io_online.
3361          * Otherwise mailbox command timesout.
3362          */
3363         pdev->error_state = pci_channel_io_normal;
3364
3365         pci_restore_state(pdev);
3366
3367         /* pci_restore_state() clears the saved_state flag of the device
3368          * save restored state which resets saved_state flag
3369          */
3370         pci_save_state(pdev);
3371
3372         if (ha->mem_only)
3373                 rc = pci_enable_device_mem(pdev);
3374         else
3375                 rc = pci_enable_device(pdev);
3376
3377         if (rc) {
3378                 qla_printk(KERN_WARNING, ha,
3379                     "Can't re-enable PCI device after reset.\n");
3380                 return ret;
3381         }
3382
3383         rsp = ha->rsp_q_map[0];
3384         if (qla2x00_request_irqs(ha, rsp))
3385                 return ret;
3386
3387         if (ha->isp_ops->pci_config(base_vha))
3388                 return ret;
3389
3390         while (ha->flags.mbox_busy && retries--)
3391                 msleep(1000);
3392
3393         set_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
3394         if (qla2x00_abort_isp(base_vha) == QLA_SUCCESS)
3395                 ret =  PCI_ERS_RESULT_RECOVERED;
3396         clear_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
3397
3398         pci_cleanup_aer_uncorrect_error_status(pdev);
3399
3400         DEBUG17(qla_printk(KERN_WARNING, ha,
3401             "slot_reset-return:ret=%x\n", ret));
3402
3403         return ret;
3404 }
3405
3406 static void
3407 qla2xxx_pci_resume(struct pci_dev *pdev)
3408 {
3409         scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
3410         struct qla_hw_data *ha = base_vha->hw;
3411         int ret;
3412
3413         DEBUG17(qla_printk(KERN_WARNING, ha, "pci_resume\n"));
3414
3415         ret = qla2x00_wait_for_hba_online(base_vha);
3416         if (ret != QLA_SUCCESS) {
3417                 qla_printk(KERN_ERR, ha,
3418                     "the device failed to resume I/O "
3419                     "from slot/link_reset");
3420         }
3421
3422         ha->flags.eeh_busy = 0;
3423 }
3424
3425 static struct pci_error_handlers qla2xxx_err_handler = {
3426         .error_detected = qla2xxx_pci_error_detected,
3427         .mmio_enabled = qla2xxx_pci_mmio_enabled,
3428         .slot_reset = qla2xxx_pci_slot_reset,
3429         .resume = qla2xxx_pci_resume,
3430 };
3431
3432 static struct pci_device_id qla2xxx_pci_tbl[] = {
3433         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2100) },
3434         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2200) },
3435         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2300) },
3436         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2312) },
3437         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2322) },
3438         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP6312) },
3439         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP6322) },
3440         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2422) },
3441         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2432) },
3442         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP8432) },
3443         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP5422) },
3444         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP5432) },
3445         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2532) },
3446         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP8001) },
3447         { 0 },
3448 };
3449 MODULE_DEVICE_TABLE(pci, qla2xxx_pci_tbl);
3450
3451 static struct pci_driver qla2xxx_pci_driver = {
3452         .name           = QLA2XXX_DRIVER_NAME,
3453         .driver         = {
3454                 .owner          = THIS_MODULE,
3455         },
3456         .id_table       = qla2xxx_pci_tbl,
3457         .probe          = qla2x00_probe_one,
3458         .remove         = qla2x00_remove_one,
3459         .err_handler    = &qla2xxx_err_handler,
3460 };
3461
3462 /**
3463  * qla2x00_module_init - Module initialization.
3464  **/
3465 static int __init
3466 qla2x00_module_init(void)
3467 {
3468         int ret = 0;
3469
3470         /* Allocate cache for SRBs. */
3471         srb_cachep = kmem_cache_create("qla2xxx_srbs", sizeof(srb_t), 0,
3472             SLAB_HWCACHE_ALIGN, NULL);
3473         if (srb_cachep == NULL) {
3474                 printk(KERN_ERR
3475                     "qla2xxx: Unable to allocate SRB cache...Failing load!\n");
3476                 return -ENOMEM;
3477         }
3478
3479         /* Derive version string. */
3480         strcpy(qla2x00_version_str, QLA2XXX_VERSION);
3481         if (ql2xextended_error_logging)
3482                 strcat(qla2x00_version_str, "-debug");
3483
3484         qla2xxx_transport_template =
3485             fc_attach_transport(&qla2xxx_transport_functions);
3486         if (!qla2xxx_transport_template) {
3487                 kmem_cache_destroy(srb_cachep);
3488                 return -ENODEV;
3489         }
3490         qla2xxx_transport_vport_template =
3491             fc_attach_transport(&qla2xxx_transport_vport_functions);
3492         if (!qla2xxx_transport_vport_template) {
3493                 kmem_cache_destroy(srb_cachep);
3494                 fc_release_transport(qla2xxx_transport_template);
3495                 return -ENODEV;
3496         }
3497
3498         printk(KERN_INFO "QLogic Fibre Channel HBA Driver: %s\n",
3499             qla2x00_version_str);
3500         ret = pci_register_driver(&qla2xxx_pci_driver);
3501         if (ret) {
3502                 kmem_cache_destroy(srb_cachep);
3503                 fc_release_transport(qla2xxx_transport_template);
3504                 fc_release_transport(qla2xxx_transport_vport_template);
3505         }
3506         return ret;
3507 }
3508
3509 /**
3510  * qla2x00_module_exit - Module cleanup.
3511  **/
3512 static void __exit
3513 qla2x00_module_exit(void)
3514 {
3515         pci_unregister_driver(&qla2xxx_pci_driver);
3516         qla2x00_release_firmware();
3517         kmem_cache_destroy(srb_cachep);
3518         fc_release_transport(qla2xxx_transport_template);
3519         fc_release_transport(qla2xxx_transport_vport_template);
3520 }
3521
3522 module_init(qla2x00_module_init);
3523 module_exit(qla2x00_module_exit);
3524
3525 MODULE_AUTHOR("QLogic Corporation");
3526 MODULE_DESCRIPTION("QLogic Fibre Channel HBA Driver");
3527 MODULE_LICENSE("GPL");
3528 MODULE_VERSION(QLA2XXX_VERSION);
3529 MODULE_FIRMWARE(FW_FILE_ISP21XX);
3530 MODULE_FIRMWARE(FW_FILE_ISP22XX);
3531 MODULE_FIRMWARE(FW_FILE_ISP2300);
3532 MODULE_FIRMWARE(FW_FILE_ISP2322);
3533 MODULE_FIRMWARE(FW_FILE_ISP24XX);
3534 MODULE_FIRMWARE(FW_FILE_ISP25XX);