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