[SCSI] qla2xxx: Add firmware-dump kobject uevent notification.
[linux-2.6.git] / drivers / scsi / qla2xxx / qla_os.c
1 /*
2  * QLogic Fibre Channel HBA Driver
3  * Copyright (c)  2003-2008 QLogic Corporation
4  *
5  * See LICENSE.qla2xxx for copyright and licensing details.
6  */
7 #include "qla_def.h"
8
9 #include <linux/moduleparam.h>
10 #include <linux/vmalloc.h>
11 #include <linux/delay.h>
12 #include <linux/kthread.h>
13 #include <linux/mutex.h>
14 #include <linux/kobject.h>
15
16 #include <scsi/scsi_tcq.h>
17 #include <scsi/scsicam.h>
18 #include <scsi/scsi_transport.h>
19 #include <scsi/scsi_transport_fc.h>
20
21 /*
22  * Driver version
23  */
24 char qla2x00_version_str[40];
25
26 /*
27  * SRB allocation cache
28  */
29 static struct kmem_cache *srb_cachep;
30
31 int ql2xlogintimeout = 20;
32 module_param(ql2xlogintimeout, int, S_IRUGO|S_IRUSR);
33 MODULE_PARM_DESC(ql2xlogintimeout,
34                 "Login timeout value in seconds.");
35
36 int qlport_down_retry;
37 module_param(qlport_down_retry, int, S_IRUGO|S_IRUSR);
38 MODULE_PARM_DESC(qlport_down_retry,
39                 "Maximum number of command retries to a port that returns "
40                 "a PORT-DOWN status.");
41
42 int ql2xplogiabsentdevice;
43 module_param(ql2xplogiabsentdevice, int, S_IRUGO|S_IWUSR);
44 MODULE_PARM_DESC(ql2xplogiabsentdevice,
45                 "Option to enable PLOGI to devices that are not present after "
46                 "a Fabric scan.  This is needed for several broken switches. "
47                 "Default is 0 - no PLOGI. 1 - perfom PLOGI.");
48
49 int ql2xloginretrycount = 0;
50 module_param(ql2xloginretrycount, int, S_IRUGO|S_IRUSR);
51 MODULE_PARM_DESC(ql2xloginretrycount,
52                 "Specify an alternate value for the NVRAM login retry count.");
53
54 int ql2xallocfwdump = 1;
55 module_param(ql2xallocfwdump, int, S_IRUGO|S_IRUSR);
56 MODULE_PARM_DESC(ql2xallocfwdump,
57                 "Option to enable allocation of memory for a firmware dump "
58                 "during HBA initialization.  Memory allocation requirements "
59                 "vary by ISP type.  Default is 1 - allocate memory.");
60
61 int ql2xextended_error_logging;
62 module_param(ql2xextended_error_logging, int, S_IRUGO|S_IWUSR);
63 MODULE_PARM_DESC(ql2xextended_error_logging,
64                 "Option to enable extended error logging, "
65                 "Default is 0 - no logging. 1 - log errors.");
66
67 static void qla2x00_free_device(scsi_qla_host_t *);
68
69 int ql2xfdmienable=1;
70 module_param(ql2xfdmienable, int, S_IRUGO|S_IRUSR);
71 MODULE_PARM_DESC(ql2xfdmienable,
72                 "Enables FDMI registratons "
73                 "Default is 0 - no FDMI. 1 - perfom FDMI.");
74
75 #define MAX_Q_DEPTH    32
76 static int ql2xmaxqdepth = MAX_Q_DEPTH;
77 module_param(ql2xmaxqdepth, int, S_IRUGO|S_IWUSR);
78 MODULE_PARM_DESC(ql2xmaxqdepth,
79                 "Maximum queue depth to report for target devices.");
80
81 int ql2xqfulltracking = 1;
82 module_param(ql2xqfulltracking, int, S_IRUGO|S_IWUSR);
83 MODULE_PARM_DESC(ql2xqfulltracking,
84                 "Controls whether the driver tracks queue full status "
85                 "returns and dynamically adjusts a scsi device's queue "
86                 "depth.  Default is 1, perform tracking.  Set to 0 to "
87                 "disable dynamic tracking and adjustment of queue depth.");
88
89 int ql2xqfullrampup = 120;
90 module_param(ql2xqfullrampup, int, S_IRUGO|S_IWUSR);
91 MODULE_PARM_DESC(ql2xqfullrampup,
92                 "Number of seconds to wait to begin to ramp-up the queue "
93                 "depth for a device after a queue-full condition has been "
94                 "detected.  Default is 120 seconds.");
95
96 int ql2xiidmaenable=1;
97 module_param(ql2xiidmaenable, int, S_IRUGO|S_IRUSR);
98 MODULE_PARM_DESC(ql2xiidmaenable,
99                 "Enables iIDMA settings "
100                 "Default is 1 - perform iIDMA. 0 - no iIDMA.");
101
102 int ql2xmaxqueues = 1;
103 module_param(ql2xmaxqueues, int, S_IRUGO|S_IRUSR);
104 MODULE_PARM_DESC(ql2xmaxqueues,
105                 "Enables MQ settings "
106                 "Default is 1 for single queue. Set it to number \
107                         of queues in MQ mode.");
108
109 int ql2xmultique_tag;
110 module_param(ql2xmultique_tag, int, S_IRUGO|S_IRUSR);
111 MODULE_PARM_DESC(ql2xmultique_tag,
112                 "Enables CPU affinity settings for the driver "
113                 "Default is 0 for no affinity of request and response IO. "
114                 "Set it to 1 to turn on the cpu affinity.");
115
116 int ql2xfwloadbin;
117 module_param(ql2xfwloadbin, int, S_IRUGO|S_IRUSR);
118 MODULE_PARM_DESC(ql2xfwloadbin,
119                 "Option to specify location from which to load ISP firmware:\n"
120                 " 2 -- load firmware via the request_firmware() (hotplug)\n"
121                 "      interface.\n"
122                 " 1 -- load firmware from flash.\n"
123                 " 0 -- use default semantics.\n");
124
125 /*
126  * SCSI host template entry points
127  */
128 static int qla2xxx_slave_configure(struct scsi_device * device);
129 static int qla2xxx_slave_alloc(struct scsi_device *);
130 static int qla2xxx_scan_finished(struct Scsi_Host *, unsigned long time);
131 static void qla2xxx_scan_start(struct Scsi_Host *);
132 static void qla2xxx_slave_destroy(struct scsi_device *);
133 static int qla2xxx_queuecommand(struct scsi_cmnd *cmd,
134                 void (*fn)(struct scsi_cmnd *));
135 static int qla2xxx_eh_abort(struct scsi_cmnd *);
136 static int qla2xxx_eh_device_reset(struct scsi_cmnd *);
137 static int qla2xxx_eh_target_reset(struct scsi_cmnd *);
138 static int qla2xxx_eh_bus_reset(struct scsi_cmnd *);
139 static int qla2xxx_eh_host_reset(struct scsi_cmnd *);
140
141 static int qla2x00_change_queue_depth(struct scsi_device *, int);
142 static int qla2x00_change_queue_type(struct scsi_device *, int);
143
144 struct scsi_host_template qla2xxx_driver_template = {
145         .module                 = THIS_MODULE,
146         .name                   = QLA2XXX_DRIVER_NAME,
147         .queuecommand           = qla2xxx_queuecommand,
148
149         .eh_abort_handler       = qla2xxx_eh_abort,
150         .eh_device_reset_handler = qla2xxx_eh_device_reset,
151         .eh_target_reset_handler = qla2xxx_eh_target_reset,
152         .eh_bus_reset_handler   = qla2xxx_eh_bus_reset,
153         .eh_host_reset_handler  = qla2xxx_eh_host_reset,
154
155         .slave_configure        = qla2xxx_slave_configure,
156
157         .slave_alloc            = qla2xxx_slave_alloc,
158         .slave_destroy          = qla2xxx_slave_destroy,
159         .scan_finished          = qla2xxx_scan_finished,
160         .scan_start             = qla2xxx_scan_start,
161         .change_queue_depth     = qla2x00_change_queue_depth,
162         .change_queue_type      = qla2x00_change_queue_type,
163         .this_id                = -1,
164         .cmd_per_lun            = 3,
165         .use_clustering         = ENABLE_CLUSTERING,
166         .sg_tablesize           = SG_ALL,
167
168         .max_sectors            = 0xFFFF,
169         .shost_attrs            = qla2x00_host_attrs,
170 };
171
172 static struct scsi_transport_template *qla2xxx_transport_template = NULL;
173 struct scsi_transport_template *qla2xxx_transport_vport_template = NULL;
174
175 /* TODO Convert to inlines
176  *
177  * Timer routines
178  */
179
180 __inline__ void
181 qla2x00_start_timer(scsi_qla_host_t *vha, void *func, unsigned long interval)
182 {
183         init_timer(&vha->timer);
184         vha->timer.expires = jiffies + interval * HZ;
185         vha->timer.data = (unsigned long)vha;
186         vha->timer.function = (void (*)(unsigned long))func;
187         add_timer(&vha->timer);
188         vha->timer_active = 1;
189 }
190
191 static inline void
192 qla2x00_restart_timer(scsi_qla_host_t *vha, unsigned long interval)
193 {
194         mod_timer(&vha->timer, jiffies + interval * HZ);
195 }
196
197 static __inline__ void
198 qla2x00_stop_timer(scsi_qla_host_t *vha)
199 {
200         del_timer_sync(&vha->timer);
201         vha->timer_active = 0;
202 }
203
204 static int qla2x00_do_dpc(void *data);
205
206 static void qla2x00_rst_aen(scsi_qla_host_t *);
207
208 static int qla2x00_mem_alloc(struct qla_hw_data *, uint16_t, uint16_t,
209         struct req_que **, struct rsp_que **);
210 static void qla2x00_mem_free(struct qla_hw_data *);
211 static void qla2x00_sp_free_dma(srb_t *);
212
213 /* -------------------------------------------------------------------------- */
214 static int qla2x00_alloc_queues(struct qla_hw_data *ha)
215 {
216         ha->req_q_map = kzalloc(sizeof(struct req_que *) * ha->max_req_queues,
217                                 GFP_KERNEL);
218         if (!ha->req_q_map) {
219                 qla_printk(KERN_WARNING, ha,
220                         "Unable to allocate memory for request queue ptrs\n");
221                 goto fail_req_map;
222         }
223
224         ha->rsp_q_map = kzalloc(sizeof(struct rsp_que *) * ha->max_rsp_queues,
225                                 GFP_KERNEL);
226         if (!ha->rsp_q_map) {
227                 qla_printk(KERN_WARNING, ha,
228                         "Unable to allocate memory for response queue ptrs\n");
229                 goto fail_rsp_map;
230         }
231         set_bit(0, ha->rsp_qid_map);
232         set_bit(0, ha->req_qid_map);
233         return 1;
234
235 fail_rsp_map:
236         kfree(ha->req_q_map);
237         ha->req_q_map = NULL;
238 fail_req_map:
239         return -ENOMEM;
240 }
241
242 static void qla2x00_free_req_que(struct qla_hw_data *ha, struct req_que *req)
243 {
244         if (req && req->ring)
245                 dma_free_coherent(&ha->pdev->dev,
246                 (req->length + 1) * sizeof(request_t),
247                 req->ring, req->dma);
248
249         kfree(req);
250         req = NULL;
251 }
252
253 static void qla2x00_free_rsp_que(struct qla_hw_data *ha, struct rsp_que *rsp)
254 {
255         if (rsp && rsp->ring)
256                 dma_free_coherent(&ha->pdev->dev,
257                 (rsp->length + 1) * sizeof(response_t),
258                 rsp->ring, rsp->dma);
259
260         kfree(rsp);
261         rsp = NULL;
262 }
263
264 static void qla2x00_free_queues(struct qla_hw_data *ha)
265 {
266         struct req_que *req;
267         struct rsp_que *rsp;
268         int cnt;
269
270         for (cnt = 0; cnt < ha->max_req_queues; cnt++) {
271                 req = ha->req_q_map[cnt];
272                 qla2x00_free_req_que(ha, req);
273         }
274         kfree(ha->req_q_map);
275         ha->req_q_map = NULL;
276
277         for (cnt = 0; cnt < ha->max_rsp_queues; cnt++) {
278                 rsp = ha->rsp_q_map[cnt];
279                 qla2x00_free_rsp_que(ha, rsp);
280         }
281         kfree(ha->rsp_q_map);
282         ha->rsp_q_map = NULL;
283 }
284
285 static int qla25xx_setup_mode(struct scsi_qla_host *vha)
286 {
287         uint16_t options = 0;
288         int ques, req, ret;
289         struct qla_hw_data *ha = vha->hw;
290
291         if (!(ha->fw_attributes & BIT_6)) {
292                 qla_printk(KERN_INFO, ha,
293                         "Firmware is not multi-queue capable\n");
294                 goto fail;
295         }
296         if (ql2xmultique_tag) {
297                 /* create a request queue for IO */
298                 options |= BIT_7;
299                 req = qla25xx_create_req_que(ha, options, 0, 0, -1,
300                         QLA_DEFAULT_QUE_QOS);
301                 if (!req) {
302                         qla_printk(KERN_WARNING, ha,
303                                 "Can't create request queue\n");
304                         goto fail;
305                 }
306                 ha->wq = create_workqueue("qla2xxx_wq");
307                 vha->req = ha->req_q_map[req];
308                 options |= BIT_1;
309                 for (ques = 1; ques < ha->max_rsp_queues; ques++) {
310                         ret = qla25xx_create_rsp_que(ha, options, 0, 0, req);
311                         if (!ret) {
312                                 qla_printk(KERN_WARNING, ha,
313                                         "Response Queue create failed\n");
314                                 goto fail2;
315                         }
316                 }
317                 ha->flags.cpu_affinity_enabled = 1;
318
319                 DEBUG2(qla_printk(KERN_INFO, ha,
320                         "CPU affinity mode enabled, no. of response"
321                         " queues:%d, no. of request queues:%d\n",
322                         ha->max_rsp_queues, ha->max_req_queues));
323         }
324         return 0;
325 fail2:
326         qla25xx_delete_queues(vha);
327         destroy_workqueue(ha->wq);
328         ha->wq = NULL;
329 fail:
330         ha->mqenable = 0;
331         kfree(ha->req_q_map);
332         kfree(ha->rsp_q_map);
333         ha->max_req_queues = ha->max_rsp_queues = 1;
334         return 1;
335 }
336
337 static char *
338 qla2x00_pci_info_str(struct scsi_qla_host *vha, char *str)
339 {
340         struct qla_hw_data *ha = vha->hw;
341         static char *pci_bus_modes[] = {
342                 "33", "66", "100", "133",
343         };
344         uint16_t pci_bus;
345
346         strcpy(str, "PCI");
347         pci_bus = (ha->pci_attr & (BIT_9 | BIT_10)) >> 9;
348         if (pci_bus) {
349                 strcat(str, "-X (");
350                 strcat(str, pci_bus_modes[pci_bus]);
351         } else {
352                 pci_bus = (ha->pci_attr & BIT_8) >> 8;
353                 strcat(str, " (");
354                 strcat(str, pci_bus_modes[pci_bus]);
355         }
356         strcat(str, " MHz)");
357
358         return (str);
359 }
360
361 static char *
362 qla24xx_pci_info_str(struct scsi_qla_host *vha, char *str)
363 {
364         static char *pci_bus_modes[] = { "33", "66", "100", "133", };
365         struct qla_hw_data *ha = vha->hw;
366         uint32_t pci_bus;
367         int pcie_reg;
368
369         pcie_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_EXP);
370         if (pcie_reg) {
371                 char lwstr[6];
372                 uint16_t pcie_lstat, lspeed, lwidth;
373
374                 pcie_reg += 0x12;
375                 pci_read_config_word(ha->pdev, pcie_reg, &pcie_lstat);
376                 lspeed = pcie_lstat & (BIT_0 | BIT_1 | BIT_2 | BIT_3);
377                 lwidth = (pcie_lstat &
378                     (BIT_4 | BIT_5 | BIT_6 | BIT_7 | BIT_8 | BIT_9)) >> 4;
379
380                 strcpy(str, "PCIe (");
381                 if (lspeed == 1)
382                         strcat(str, "2.5GT/s ");
383                 else if (lspeed == 2)
384                         strcat(str, "5.0GT/s ");
385                 else
386                         strcat(str, "<unknown> ");
387                 snprintf(lwstr, sizeof(lwstr), "x%d)", lwidth);
388                 strcat(str, lwstr);
389
390                 return str;
391         }
392
393         strcpy(str, "PCI");
394         pci_bus = (ha->pci_attr & CSRX_PCIX_BUS_MODE_MASK) >> 8;
395         if (pci_bus == 0 || pci_bus == 8) {
396                 strcat(str, " (");
397                 strcat(str, pci_bus_modes[pci_bus >> 3]);
398         } else {
399                 strcat(str, "-X ");
400                 if (pci_bus & BIT_2)
401                         strcat(str, "Mode 2");
402                 else
403                         strcat(str, "Mode 1");
404                 strcat(str, " (");
405                 strcat(str, pci_bus_modes[pci_bus & ~BIT_2]);
406         }
407         strcat(str, " MHz)");
408
409         return str;
410 }
411
412 static char *
413 qla2x00_fw_version_str(struct scsi_qla_host *vha, char *str)
414 {
415         char un_str[10];
416         struct qla_hw_data *ha = vha->hw;
417
418         sprintf(str, "%d.%02d.%02d ", ha->fw_major_version,
419             ha->fw_minor_version,
420             ha->fw_subminor_version);
421
422         if (ha->fw_attributes & BIT_9) {
423                 strcat(str, "FLX");
424                 return (str);
425         }
426
427         switch (ha->fw_attributes & 0xFF) {
428         case 0x7:
429                 strcat(str, "EF");
430                 break;
431         case 0x17:
432                 strcat(str, "TP");
433                 break;
434         case 0x37:
435                 strcat(str, "IP");
436                 break;
437         case 0x77:
438                 strcat(str, "VI");
439                 break;
440         default:
441                 sprintf(un_str, "(%x)", ha->fw_attributes);
442                 strcat(str, un_str);
443                 break;
444         }
445         if (ha->fw_attributes & 0x100)
446                 strcat(str, "X");
447
448         return (str);
449 }
450
451 static char *
452 qla24xx_fw_version_str(struct scsi_qla_host *vha, char *str)
453 {
454         struct qla_hw_data *ha = vha->hw;
455
456         sprintf(str, "%d.%02d.%02d (%x)", ha->fw_major_version,
457             ha->fw_minor_version, ha->fw_subminor_version, ha->fw_attributes);
458         return str;
459 }
460
461 static inline srb_t *
462 qla2x00_get_new_sp(scsi_qla_host_t *vha, fc_port_t *fcport,
463     struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
464 {
465         srb_t *sp;
466         struct qla_hw_data *ha = vha->hw;
467
468         sp = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
469         if (!sp)
470                 return sp;
471
472         sp->fcport = fcport;
473         sp->cmd = cmd;
474         sp->flags = 0;
475         CMD_SP(cmd) = (void *)sp;
476         cmd->scsi_done = done;
477         sp->ctx = NULL;
478
479         return sp;
480 }
481
482 static int
483 qla2xxx_queuecommand(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
484 {
485         scsi_qla_host_t *vha = shost_priv(cmd->device->host);
486         fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
487         struct fc_rport *rport = starget_to_rport(scsi_target(cmd->device));
488         struct qla_hw_data *ha = vha->hw;
489         struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
490         srb_t *sp;
491         int rval;
492
493         if (unlikely(pci_channel_offline(ha->pdev))) {
494                 if (ha->pdev->error_state == pci_channel_io_frozen)
495                         cmd->result = DID_REQUEUE << 16;
496                 else
497                         cmd->result = DID_NO_CONNECT << 16;
498                 goto qc24_fail_command;
499         }
500
501         rval = fc_remote_port_chkready(rport);
502         if (rval) {
503                 cmd->result = rval;
504                 goto qc24_fail_command;
505         }
506
507         /* Close window on fcport/rport state-transitioning. */
508         if (fcport->drport)
509                 goto qc24_target_busy;
510
511         if (atomic_read(&fcport->state) != FCS_ONLINE) {
512                 if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD ||
513                     atomic_read(&base_vha->loop_state) == LOOP_DEAD) {
514                         cmd->result = DID_NO_CONNECT << 16;
515                         goto qc24_fail_command;
516                 }
517                 goto qc24_target_busy;
518         }
519
520         spin_unlock_irq(vha->host->host_lock);
521
522         sp = qla2x00_get_new_sp(base_vha, fcport, cmd, done);
523         if (!sp)
524                 goto qc24_host_busy_lock;
525
526         rval = ha->isp_ops->start_scsi(sp);
527         if (rval != QLA_SUCCESS)
528                 goto qc24_host_busy_free_sp;
529
530         spin_lock_irq(vha->host->host_lock);
531
532         return 0;
533
534 qc24_host_busy_free_sp:
535         qla2x00_sp_free_dma(sp);
536         mempool_free(sp, ha->srb_mempool);
537
538 qc24_host_busy_lock:
539         spin_lock_irq(vha->host->host_lock);
540         return SCSI_MLQUEUE_HOST_BUSY;
541
542 qc24_target_busy:
543         return SCSI_MLQUEUE_TARGET_BUSY;
544
545 qc24_fail_command:
546         done(cmd);
547
548         return 0;
549 }
550
551
552 /*
553  * qla2x00_eh_wait_on_command
554  *    Waits for the command to be returned by the Firmware for some
555  *    max time.
556  *
557  * Input:
558  *    cmd = Scsi Command to wait on.
559  *
560  * Return:
561  *    Not Found : 0
562  *    Found : 1
563  */
564 static int
565 qla2x00_eh_wait_on_command(struct scsi_cmnd *cmd)
566 {
567 #define ABORT_POLLING_PERIOD    1000
568 #define ABORT_WAIT_ITER         ((10 * 1000) / (ABORT_POLLING_PERIOD))
569         unsigned long wait_iter = ABORT_WAIT_ITER;
570         int ret = QLA_SUCCESS;
571
572         while (CMD_SP(cmd) && wait_iter--) {
573                 msleep(ABORT_POLLING_PERIOD);
574         }
575         if (CMD_SP(cmd))
576                 ret = QLA_FUNCTION_FAILED;
577
578         return ret;
579 }
580
581 /*
582  * qla2x00_wait_for_hba_online
583  *    Wait till the HBA is online after going through
584  *    <= MAX_RETRIES_OF_ISP_ABORT  or
585  *    finally HBA is disabled ie marked offline
586  *
587  * Input:
588  *     ha - pointer to host adapter structure
589  *
590  * Note:
591  *    Does context switching-Release SPIN_LOCK
592  *    (if any) before calling this routine.
593  *
594  * Return:
595  *    Success (Adapter is online) : 0
596  *    Failed  (Adapter is offline/disabled) : 1
597  */
598 int
599 qla2x00_wait_for_hba_online(scsi_qla_host_t *vha)
600 {
601         int             return_status;
602         unsigned long   wait_online;
603         struct qla_hw_data *ha = vha->hw;
604         scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
605
606         wait_online = jiffies + (MAX_LOOP_TIMEOUT * HZ);
607         while (((test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags)) ||
608             test_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags) ||
609             test_bit(ISP_ABORT_RETRY, &base_vha->dpc_flags) ||
610             ha->dpc_active) && time_before(jiffies, wait_online)) {
611
612                 msleep(1000);
613         }
614         if (base_vha->flags.online)
615                 return_status = QLA_SUCCESS;
616         else
617                 return_status = QLA_FUNCTION_FAILED;
618
619         return (return_status);
620 }
621
622 int
623 qla2x00_wait_for_chip_reset(scsi_qla_host_t *vha)
624 {
625         int             return_status;
626         unsigned long   wait_reset;
627         struct qla_hw_data *ha = vha->hw;
628         scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
629
630         wait_reset = jiffies + (MAX_LOOP_TIMEOUT * HZ);
631         while (((test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags)) ||
632             test_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags) ||
633             test_bit(ISP_ABORT_RETRY, &base_vha->dpc_flags) ||
634             ha->dpc_active) && time_before(jiffies, wait_reset)) {
635
636                 msleep(1000);
637
638                 if (!test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags) &&
639                     ha->flags.chip_reset_done)
640                         break;
641         }
642         if (ha->flags.chip_reset_done)
643                 return_status = QLA_SUCCESS;
644         else
645                 return_status = QLA_FUNCTION_FAILED;
646
647         return return_status;
648 }
649
650 /*
651  * qla2x00_wait_for_loop_ready
652  *    Wait for MAX_LOOP_TIMEOUT(5 min) value for loop
653  *    to be in LOOP_READY state.
654  * Input:
655  *     ha - pointer to host adapter structure
656  *
657  * Note:
658  *    Does context switching-Release SPIN_LOCK
659  *    (if any) before calling this routine.
660  *
661  *
662  * Return:
663  *    Success (LOOP_READY) : 0
664  *    Failed  (LOOP_NOT_READY) : 1
665  */
666 static inline int
667 qla2x00_wait_for_loop_ready(scsi_qla_host_t *vha)
668 {
669         int      return_status = QLA_SUCCESS;
670         unsigned long loop_timeout ;
671         struct qla_hw_data *ha = vha->hw;
672         scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
673
674         /* wait for 5 min at the max for loop to be ready */
675         loop_timeout = jiffies + (MAX_LOOP_TIMEOUT * HZ);
676
677         while ((!atomic_read(&base_vha->loop_down_timer) &&
678             atomic_read(&base_vha->loop_state) == LOOP_DOWN) ||
679             atomic_read(&base_vha->loop_state) != LOOP_READY) {
680                 if (atomic_read(&base_vha->loop_state) == LOOP_DEAD) {
681                         return_status = QLA_FUNCTION_FAILED;
682                         break;
683                 }
684                 msleep(1000);
685                 if (time_after_eq(jiffies, loop_timeout)) {
686                         return_status = QLA_FUNCTION_FAILED;
687                         break;
688                 }
689         }
690         return (return_status);
691 }
692
693 void
694 qla2x00_abort_fcport_cmds(fc_port_t *fcport)
695 {
696         int cnt;
697         unsigned long flags;
698         srb_t *sp;
699         scsi_qla_host_t *vha = fcport->vha;
700         struct qla_hw_data *ha = vha->hw;
701         struct req_que *req;
702
703         spin_lock_irqsave(&ha->hardware_lock, flags);
704         req = vha->req;
705         for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
706                 sp = req->outstanding_cmds[cnt];
707                 if (!sp)
708                         continue;
709                 if (sp->fcport != fcport)
710                         continue;
711                 if (sp->ctx)
712                         continue;
713
714                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
715                 if (ha->isp_ops->abort_command(sp)) {
716                         DEBUG2(qla_printk(KERN_WARNING, ha,
717                         "Abort failed --  %lx\n",
718                         sp->cmd->serial_number));
719                 } else {
720                         if (qla2x00_eh_wait_on_command(sp->cmd) !=
721                                 QLA_SUCCESS)
722                                 DEBUG2(qla_printk(KERN_WARNING, ha,
723                                 "Abort failed while waiting --  %lx\n",
724                                 sp->cmd->serial_number));
725                 }
726                 spin_lock_irqsave(&ha->hardware_lock, flags);
727         }
728         spin_unlock_irqrestore(&ha->hardware_lock, flags);
729 }
730
731 static void
732 qla2x00_block_error_handler(struct scsi_cmnd *cmnd)
733 {
734         struct Scsi_Host *shost = cmnd->device->host;
735         struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
736         unsigned long flags;
737
738         spin_lock_irqsave(shost->host_lock, flags);
739         while (rport->port_state == FC_PORTSTATE_BLOCKED) {
740                 spin_unlock_irqrestore(shost->host_lock, flags);
741                 msleep(1000);
742                 spin_lock_irqsave(shost->host_lock, flags);
743         }
744         spin_unlock_irqrestore(shost->host_lock, flags);
745         return;
746 }
747
748 /**************************************************************************
749 * qla2xxx_eh_abort
750 *
751 * Description:
752 *    The abort function will abort the specified command.
753 *
754 * Input:
755 *    cmd = Linux SCSI command packet to be aborted.
756 *
757 * Returns:
758 *    Either SUCCESS or FAILED.
759 *
760 * Note:
761 *    Only return FAILED if command not returned by firmware.
762 **************************************************************************/
763 static int
764 qla2xxx_eh_abort(struct scsi_cmnd *cmd)
765 {
766         scsi_qla_host_t *vha = shost_priv(cmd->device->host);
767         srb_t *sp;
768         int ret, i;
769         unsigned int id, lun;
770         unsigned long serial;
771         unsigned long flags;
772         int wait = 0;
773         struct qla_hw_data *ha = vha->hw;
774         struct req_que *req = vha->req;
775         srb_t *spt;
776
777         qla2x00_block_error_handler(cmd);
778
779         if (!CMD_SP(cmd))
780                 return SUCCESS;
781
782         ret = SUCCESS;
783
784         id = cmd->device->id;
785         lun = cmd->device->lun;
786         serial = cmd->serial_number;
787         spt = (srb_t *) CMD_SP(cmd);
788         if (!spt)
789                 return SUCCESS;
790
791         /* Check active list for command command. */
792         spin_lock_irqsave(&ha->hardware_lock, flags);
793         for (i = 1; i < MAX_OUTSTANDING_COMMANDS; i++) {
794                 sp = req->outstanding_cmds[i];
795
796                 if (sp == NULL)
797                         continue;
798                 if (sp->ctx)
799                         continue;
800                 if (sp->cmd != cmd)
801                         continue;
802
803                 DEBUG2(printk("%s(%ld): aborting sp %p from RISC."
804                 " pid=%ld.\n", __func__, vha->host_no, sp, serial));
805
806                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
807                 if (ha->isp_ops->abort_command(sp)) {
808                         DEBUG2(printk("%s(%ld): abort_command "
809                         "mbx failed.\n", __func__, vha->host_no));
810                         ret = FAILED;
811                 } else {
812                         DEBUG3(printk("%s(%ld): abort_command "
813                         "mbx success.\n", __func__, vha->host_no));
814                         wait = 1;
815                 }
816                 spin_lock_irqsave(&ha->hardware_lock, flags);
817                 break;
818         }
819         spin_unlock_irqrestore(&ha->hardware_lock, flags);
820
821         /* Wait for the command to be returned. */
822         if (wait) {
823                 if (qla2x00_eh_wait_on_command(cmd) != QLA_SUCCESS) {
824                         qla_printk(KERN_ERR, ha,
825                             "scsi(%ld:%d:%d): Abort handler timed out -- %lx "
826                             "%x.\n", vha->host_no, id, lun, serial, ret);
827                         ret = FAILED;
828                 }
829         }
830
831         qla_printk(KERN_INFO, ha,
832             "scsi(%ld:%d:%d): Abort command issued -- %d %lx %x.\n",
833             vha->host_no, id, lun, wait, serial, ret);
834
835         return ret;
836 }
837
838 enum nexus_wait_type {
839         WAIT_HOST = 0,
840         WAIT_TARGET,
841         WAIT_LUN,
842 };
843
844 static int
845 qla2x00_eh_wait_for_pending_commands(scsi_qla_host_t *vha, unsigned int t,
846         unsigned int l, srb_t *sp, enum nexus_wait_type type)
847 {
848         int cnt, match, status;
849         unsigned long flags;
850         struct qla_hw_data *ha = vha->hw;
851         struct req_que *req;
852
853         status = QLA_SUCCESS;
854         if (!sp)
855                 return status;
856
857         spin_lock_irqsave(&ha->hardware_lock, flags);
858         req = vha->req;
859         for (cnt = 1; status == QLA_SUCCESS &&
860                 cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
861                 sp = req->outstanding_cmds[cnt];
862                 if (!sp)
863                         continue;
864                 if (sp->ctx)
865                         continue;
866                 if (vha->vp_idx != sp->fcport->vha->vp_idx)
867                         continue;
868                 match = 0;
869                 switch (type) {
870                 case WAIT_HOST:
871                         match = 1;
872                         break;
873                 case WAIT_TARGET:
874                         match = sp->cmd->device->id == t;
875                         break;
876                 case WAIT_LUN:
877                         match = (sp->cmd->device->id == t &&
878                                 sp->cmd->device->lun == l);
879                         break;
880                 }
881                 if (!match)
882                         continue;
883
884                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
885                 status = qla2x00_eh_wait_on_command(sp->cmd);
886                 spin_lock_irqsave(&ha->hardware_lock, flags);
887         }
888         spin_unlock_irqrestore(&ha->hardware_lock, flags);
889
890         return status;
891 }
892
893 static char *reset_errors[] = {
894         "HBA not online",
895         "HBA not ready",
896         "Task management failed",
897         "Waiting for command completions",
898 };
899
900 static int
901 __qla2xxx_eh_generic_reset(char *name, enum nexus_wait_type type,
902     struct scsi_cmnd *cmd, int (*do_reset)(struct fc_port *, unsigned int, int))
903 {
904         scsi_qla_host_t *vha = shost_priv(cmd->device->host);
905         fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
906         int err;
907
908         qla2x00_block_error_handler(cmd);
909
910         if (!fcport)
911                 return FAILED;
912
913         qla_printk(KERN_INFO, vha->hw, "scsi(%ld:%d:%d): %s RESET ISSUED.\n",
914             vha->host_no, cmd->device->id, cmd->device->lun, name);
915
916         err = 0;
917         if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS)
918                 goto eh_reset_failed;
919         err = 1;
920         if (qla2x00_wait_for_loop_ready(vha) != QLA_SUCCESS)
921                 goto eh_reset_failed;
922         err = 2;
923         if (do_reset(fcport, cmd->device->lun, cmd->request->cpu + 1)
924                 != QLA_SUCCESS)
925                 goto eh_reset_failed;
926         err = 3;
927         if (qla2x00_eh_wait_for_pending_commands(vha, cmd->device->id,
928             cmd->device->lun, (srb_t *) CMD_SP(cmd), type) != QLA_SUCCESS)
929                 goto eh_reset_failed;
930
931         qla_printk(KERN_INFO, vha->hw, "scsi(%ld:%d:%d): %s RESET SUCCEEDED.\n",
932             vha->host_no, cmd->device->id, cmd->device->lun, name);
933
934         return SUCCESS;
935
936  eh_reset_failed:
937         qla_printk(KERN_INFO, vha->hw, "scsi(%ld:%d:%d): %s RESET FAILED: %s.\n"
938             , vha->host_no, cmd->device->id, cmd->device->lun, name,
939             reset_errors[err]);
940         return FAILED;
941 }
942
943 static int
944 qla2xxx_eh_device_reset(struct scsi_cmnd *cmd)
945 {
946         scsi_qla_host_t *vha = shost_priv(cmd->device->host);
947         struct qla_hw_data *ha = vha->hw;
948
949         return __qla2xxx_eh_generic_reset("DEVICE", WAIT_LUN, cmd,
950             ha->isp_ops->lun_reset);
951 }
952
953 static int
954 qla2xxx_eh_target_reset(struct scsi_cmnd *cmd)
955 {
956         scsi_qla_host_t *vha = shost_priv(cmd->device->host);
957         struct qla_hw_data *ha = vha->hw;
958
959         return __qla2xxx_eh_generic_reset("TARGET", WAIT_TARGET, cmd,
960             ha->isp_ops->target_reset);
961 }
962
963 /**************************************************************************
964 * qla2xxx_eh_bus_reset
965 *
966 * Description:
967 *    The bus reset function will reset the bus and abort any executing
968 *    commands.
969 *
970 * Input:
971 *    cmd = Linux SCSI command packet of the command that cause the
972 *          bus reset.
973 *
974 * Returns:
975 *    SUCCESS/FAILURE (defined as macro in scsi.h).
976 *
977 **************************************************************************/
978 static int
979 qla2xxx_eh_bus_reset(struct scsi_cmnd *cmd)
980 {
981         scsi_qla_host_t *vha = shost_priv(cmd->device->host);
982         fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
983         int ret = FAILED;
984         unsigned int id, lun;
985         unsigned long serial;
986         srb_t *sp = (srb_t *) CMD_SP(cmd);
987
988         qla2x00_block_error_handler(cmd);
989
990         id = cmd->device->id;
991         lun = cmd->device->lun;
992         serial = cmd->serial_number;
993
994         if (!fcport)
995                 return ret;
996
997         qla_printk(KERN_INFO, vha->hw,
998             "scsi(%ld:%d:%d): BUS RESET ISSUED.\n", vha->host_no, id, lun);
999
1000         if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
1001                 DEBUG2(printk("%s failed:board disabled\n",__func__));
1002                 goto eh_bus_reset_done;
1003         }
1004
1005         if (qla2x00_wait_for_loop_ready(vha) == QLA_SUCCESS) {
1006                 if (qla2x00_loop_reset(vha) == QLA_SUCCESS)
1007                         ret = SUCCESS;
1008         }
1009         if (ret == FAILED)
1010                 goto eh_bus_reset_done;
1011
1012         /* Flush outstanding commands. */
1013         if (qla2x00_eh_wait_for_pending_commands(vha, 0, 0, sp, WAIT_HOST) !=
1014             QLA_SUCCESS)
1015                 ret = FAILED;
1016
1017 eh_bus_reset_done:
1018         qla_printk(KERN_INFO, vha->hw, "%s: reset %s\n", __func__,
1019             (ret == FAILED) ? "failed" : "succeded");
1020
1021         return ret;
1022 }
1023
1024 /**************************************************************************
1025 * qla2xxx_eh_host_reset
1026 *
1027 * Description:
1028 *    The reset function will reset the Adapter.
1029 *
1030 * Input:
1031 *      cmd = Linux SCSI command packet of the command that cause the
1032 *            adapter reset.
1033 *
1034 * Returns:
1035 *      Either SUCCESS or FAILED.
1036 *
1037 * Note:
1038 **************************************************************************/
1039 static int
1040 qla2xxx_eh_host_reset(struct scsi_cmnd *cmd)
1041 {
1042         scsi_qla_host_t *vha = shost_priv(cmd->device->host);
1043         fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
1044         struct qla_hw_data *ha = vha->hw;
1045         int ret = FAILED;
1046         unsigned int id, lun;
1047         unsigned long serial;
1048         srb_t *sp = (srb_t *) CMD_SP(cmd);
1049         scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
1050
1051         qla2x00_block_error_handler(cmd);
1052
1053         id = cmd->device->id;
1054         lun = cmd->device->lun;
1055         serial = cmd->serial_number;
1056
1057         if (!fcport)
1058                 return ret;
1059
1060         qla_printk(KERN_INFO, ha,
1061             "scsi(%ld:%d:%d): ADAPTER RESET ISSUED.\n", vha->host_no, id, lun);
1062
1063         if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS)
1064                 goto eh_host_reset_lock;
1065
1066         /*
1067          * Fixme-may be dpc thread is active and processing
1068          * loop_resync,so wait a while for it to
1069          * be completed and then issue big hammer.Otherwise
1070          * it may cause I/O failure as big hammer marks the
1071          * devices as lost kicking of the port_down_timer
1072          * while dpc is stuck for the mailbox to complete.
1073          */
1074         qla2x00_wait_for_loop_ready(vha);
1075         if (vha != base_vha) {
1076                 if (qla2x00_vp_abort_isp(vha))
1077                         goto eh_host_reset_lock;
1078         } else {
1079                 if (ha->wq)
1080                         flush_workqueue(ha->wq);
1081
1082                 set_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
1083                 if (qla2x00_abort_isp(base_vha)) {
1084                         clear_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
1085                         /* failed. schedule dpc to try */
1086                         set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
1087
1088                         if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS)
1089                                 goto eh_host_reset_lock;
1090                 }
1091                 clear_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
1092         }
1093
1094         /* Waiting for command to be returned to OS.*/
1095         if (qla2x00_eh_wait_for_pending_commands(vha, 0, 0, sp, WAIT_HOST) ==
1096                 QLA_SUCCESS)
1097                 ret = SUCCESS;
1098
1099 eh_host_reset_lock:
1100         qla_printk(KERN_INFO, ha, "%s: reset %s\n", __func__,
1101             (ret == FAILED) ? "failed" : "succeded");
1102
1103         return ret;
1104 }
1105
1106 /*
1107 * qla2x00_loop_reset
1108 *      Issue loop reset.
1109 *
1110 * Input:
1111 *      ha = adapter block pointer.
1112 *
1113 * Returns:
1114 *      0 = success
1115 */
1116 int
1117 qla2x00_loop_reset(scsi_qla_host_t *vha)
1118 {
1119         int ret;
1120         struct fc_port *fcport;
1121         struct qla_hw_data *ha = vha->hw;
1122
1123         if (ha->flags.enable_lip_full_login && !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) {
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         scsi_qla_host_t *base_vha;
2186
2187         if (!fcport->rport)
2188                 return;
2189
2190         rport = fcport->rport;
2191         if (defer) {
2192                 base_vha = pci_get_drvdata(vha->hw->pdev);
2193                 spin_lock_irq(vha->host->host_lock);
2194                 fcport->drport = rport;
2195                 spin_unlock_irq(vha->host->host_lock);
2196                 set_bit(FCPORT_UPDATE_NEEDED, &base_vha->dpc_flags);
2197                 qla2xxx_wake_dpc(base_vha);
2198         } else
2199                 fc_remote_port_delete(rport);
2200 }
2201
2202 /*
2203  * qla2x00_mark_device_lost Updates fcport state when device goes offline.
2204  *
2205  * Input: ha = adapter block pointer.  fcport = port structure pointer.
2206  *
2207  * Return: None.
2208  *
2209  * Context:
2210  */
2211 void qla2x00_mark_device_lost(scsi_qla_host_t *vha, fc_port_t *fcport,
2212     int do_login, int defer)
2213 {
2214         if (atomic_read(&fcport->state) == FCS_ONLINE &&
2215             vha->vp_idx == fcport->vp_idx) {
2216                 atomic_set(&fcport->state, FCS_DEVICE_LOST);
2217                 qla2x00_schedule_rport_del(vha, fcport, defer);
2218         }
2219         /*
2220          * We may need to retry the login, so don't change the state of the
2221          * port but do the retries.
2222          */
2223         if (atomic_read(&fcport->state) != FCS_DEVICE_DEAD)
2224                 atomic_set(&fcport->state, FCS_DEVICE_LOST);
2225
2226         if (!do_login)
2227                 return;
2228
2229         if (fcport->login_retry == 0) {
2230                 fcport->login_retry = vha->hw->login_retry_count;
2231                 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
2232
2233                 DEBUG(printk("scsi(%ld): Port login retry: "
2234                     "%02x%02x%02x%02x%02x%02x%02x%02x, "
2235                     "id = 0x%04x retry cnt=%d\n",
2236                     vha->host_no,
2237                     fcport->port_name[0],
2238                     fcport->port_name[1],
2239                     fcport->port_name[2],
2240                     fcport->port_name[3],
2241                     fcport->port_name[4],
2242                     fcport->port_name[5],
2243                     fcport->port_name[6],
2244                     fcport->port_name[7],
2245                     fcport->loop_id,
2246                     fcport->login_retry));
2247         }
2248 }
2249
2250 /*
2251  * qla2x00_mark_all_devices_lost
2252  *      Updates fcport state when device goes offline.
2253  *
2254  * Input:
2255  *      ha = adapter block pointer.
2256  *      fcport = port structure pointer.
2257  *
2258  * Return:
2259  *      None.
2260  *
2261  * Context:
2262  */
2263 void
2264 qla2x00_mark_all_devices_lost(scsi_qla_host_t *vha, int defer)
2265 {
2266         fc_port_t *fcport;
2267
2268         list_for_each_entry(fcport, &vha->vp_fcports, list) {
2269                 if (vha->vp_idx != 0 && vha->vp_idx != fcport->vp_idx)
2270                         continue;
2271
2272                 /*
2273                  * No point in marking the device as lost, if the device is
2274                  * already DEAD.
2275                  */
2276                 if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD)
2277                         continue;
2278                 if (atomic_read(&fcport->state) == FCS_ONLINE) {
2279                         if (defer)
2280                                 qla2x00_schedule_rport_del(vha, fcport, defer);
2281                         else if (vha->vp_idx == fcport->vp_idx)
2282                                 qla2x00_schedule_rport_del(vha, fcport, defer);
2283                 }
2284                 atomic_set(&fcport->state, FCS_DEVICE_LOST);
2285         }
2286 }
2287
2288 /*
2289 * qla2x00_mem_alloc
2290 *      Allocates adapter memory.
2291 *
2292 * Returns:
2293 *      0  = success.
2294 *      !0  = failure.
2295 */
2296 static int
2297 qla2x00_mem_alloc(struct qla_hw_data *ha, uint16_t req_len, uint16_t rsp_len,
2298         struct req_que **req, struct rsp_que **rsp)
2299 {
2300         char    name[16];
2301
2302         ha->init_cb = dma_alloc_coherent(&ha->pdev->dev, ha->init_cb_size,
2303                 &ha->init_cb_dma, GFP_KERNEL);
2304         if (!ha->init_cb)
2305                 goto fail;
2306
2307         ha->gid_list = dma_alloc_coherent(&ha->pdev->dev, GID_LIST_SIZE,
2308                 &ha->gid_list_dma, GFP_KERNEL);
2309         if (!ha->gid_list)
2310                 goto fail_free_init_cb;
2311
2312         ha->srb_mempool = mempool_create_slab_pool(SRB_MIN_REQ, srb_cachep);
2313         if (!ha->srb_mempool)
2314                 goto fail_free_gid_list;
2315
2316         /* Get memory for cached NVRAM */
2317         ha->nvram = kzalloc(MAX_NVRAM_SIZE, GFP_KERNEL);
2318         if (!ha->nvram)
2319                 goto fail_free_srb_mempool;
2320
2321         snprintf(name, sizeof(name), "%s_%d", QLA2XXX_DRIVER_NAME,
2322                 ha->pdev->device);
2323         ha->s_dma_pool = dma_pool_create(name, &ha->pdev->dev,
2324                 DMA_POOL_SIZE, 8, 0);
2325         if (!ha->s_dma_pool)
2326                 goto fail_free_nvram;
2327
2328         /* Allocate memory for SNS commands */
2329         if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
2330         /* Get consistent memory allocated for SNS commands */
2331                 ha->sns_cmd = dma_alloc_coherent(&ha->pdev->dev,
2332                 sizeof(struct sns_cmd_pkt), &ha->sns_cmd_dma, GFP_KERNEL);
2333                 if (!ha->sns_cmd)
2334                         goto fail_dma_pool;
2335         } else {
2336         /* Get consistent memory allocated for MS IOCB */
2337                 ha->ms_iocb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
2338                         &ha->ms_iocb_dma);
2339                 if (!ha->ms_iocb)
2340                         goto fail_dma_pool;
2341         /* Get consistent memory allocated for CT SNS commands */
2342                 ha->ct_sns = dma_alloc_coherent(&ha->pdev->dev,
2343                         sizeof(struct ct_sns_pkt), &ha->ct_sns_dma, GFP_KERNEL);
2344                 if (!ha->ct_sns)
2345                         goto fail_free_ms_iocb;
2346         }
2347
2348         /* Allocate memory for request ring */
2349         *req = kzalloc(sizeof(struct req_que), GFP_KERNEL);
2350         if (!*req) {
2351                 DEBUG(printk("Unable to allocate memory for req\n"));
2352                 goto fail_req;
2353         }
2354         (*req)->length = req_len;
2355         (*req)->ring = dma_alloc_coherent(&ha->pdev->dev,
2356                 ((*req)->length + 1) * sizeof(request_t),
2357                 &(*req)->dma, GFP_KERNEL);
2358         if (!(*req)->ring) {
2359                 DEBUG(printk("Unable to allocate memory for req_ring\n"));
2360                 goto fail_req_ring;
2361         }
2362         /* Allocate memory for response ring */
2363         *rsp = kzalloc(sizeof(struct rsp_que), GFP_KERNEL);
2364         if (!*rsp) {
2365                 qla_printk(KERN_WARNING, ha,
2366                         "Unable to allocate memory for rsp\n");
2367                 goto fail_rsp;
2368         }
2369         (*rsp)->hw = ha;
2370         (*rsp)->length = rsp_len;
2371         (*rsp)->ring = dma_alloc_coherent(&ha->pdev->dev,
2372                 ((*rsp)->length + 1) * sizeof(response_t),
2373                 &(*rsp)->dma, GFP_KERNEL);
2374         if (!(*rsp)->ring) {
2375                 qla_printk(KERN_WARNING, ha,
2376                         "Unable to allocate memory for rsp_ring\n");
2377                 goto fail_rsp_ring;
2378         }
2379         (*req)->rsp = *rsp;
2380         (*rsp)->req = *req;
2381         /* Allocate memory for NVRAM data for vports */
2382         if (ha->nvram_npiv_size) {
2383                 ha->npiv_info = kzalloc(sizeof(struct qla_npiv_entry) *
2384                                         ha->nvram_npiv_size, GFP_KERNEL);
2385                 if (!ha->npiv_info) {
2386                         qla_printk(KERN_WARNING, ha,
2387                                 "Unable to allocate memory for npiv info\n");
2388                         goto fail_npiv_info;
2389                 }
2390         } else
2391                 ha->npiv_info = NULL;
2392
2393         /* Get consistent memory allocated for EX-INIT-CB. */
2394         if (IS_QLA81XX(ha)) {
2395                 ha->ex_init_cb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
2396                     &ha->ex_init_cb_dma);
2397                 if (!ha->ex_init_cb)
2398                         goto fail_ex_init_cb;
2399         }
2400
2401         INIT_LIST_HEAD(&ha->vp_list);
2402         return 1;
2403
2404 fail_ex_init_cb:
2405         kfree(ha->npiv_info);
2406 fail_npiv_info:
2407         dma_free_coherent(&ha->pdev->dev, ((*rsp)->length + 1) *
2408                 sizeof(response_t), (*rsp)->ring, (*rsp)->dma);
2409         (*rsp)->ring = NULL;
2410         (*rsp)->dma = 0;
2411 fail_rsp_ring:
2412         kfree(*rsp);
2413 fail_rsp:
2414         dma_free_coherent(&ha->pdev->dev, ((*req)->length + 1) *
2415                 sizeof(request_t), (*req)->ring, (*req)->dma);
2416         (*req)->ring = NULL;
2417         (*req)->dma = 0;
2418 fail_req_ring:
2419         kfree(*req);
2420 fail_req:
2421         dma_free_coherent(&ha->pdev->dev, sizeof(struct ct_sns_pkt),
2422                 ha->ct_sns, ha->ct_sns_dma);
2423         ha->ct_sns = NULL;
2424         ha->ct_sns_dma = 0;
2425 fail_free_ms_iocb:
2426         dma_pool_free(ha->s_dma_pool, ha->ms_iocb, ha->ms_iocb_dma);
2427         ha->ms_iocb = NULL;
2428         ha->ms_iocb_dma = 0;
2429 fail_dma_pool:
2430         dma_pool_destroy(ha->s_dma_pool);
2431         ha->s_dma_pool = NULL;
2432 fail_free_nvram:
2433         kfree(ha->nvram);
2434         ha->nvram = NULL;
2435 fail_free_srb_mempool:
2436         mempool_destroy(ha->srb_mempool);
2437         ha->srb_mempool = NULL;
2438 fail_free_gid_list:
2439         dma_free_coherent(&ha->pdev->dev, GID_LIST_SIZE, ha->gid_list,
2440         ha->gid_list_dma);
2441         ha->gid_list = NULL;
2442         ha->gid_list_dma = 0;
2443 fail_free_init_cb:
2444         dma_free_coherent(&ha->pdev->dev, ha->init_cb_size, ha->init_cb,
2445         ha->init_cb_dma);
2446         ha->init_cb = NULL;
2447         ha->init_cb_dma = 0;
2448 fail:
2449         DEBUG(printk("%s: Memory allocation failure\n", __func__));
2450         return -ENOMEM;
2451 }
2452
2453 /*
2454 * qla2x00_mem_free
2455 *      Frees all adapter allocated memory.
2456 *
2457 * Input:
2458 *      ha = adapter block pointer.
2459 */
2460 static void
2461 qla2x00_mem_free(struct qla_hw_data *ha)
2462 {
2463         if (ha->srb_mempool)
2464                 mempool_destroy(ha->srb_mempool);
2465
2466         if (ha->fce)
2467                 dma_free_coherent(&ha->pdev->dev, FCE_SIZE, ha->fce,
2468                 ha->fce_dma);
2469
2470         if (ha->fw_dump) {
2471                 if (ha->eft)
2472                         dma_free_coherent(&ha->pdev->dev,
2473                         ntohl(ha->fw_dump->eft_size), ha->eft, ha->eft_dma);
2474                 vfree(ha->fw_dump);
2475         }
2476
2477         if (ha->dcbx_tlv)
2478                 dma_free_coherent(&ha->pdev->dev, DCBX_TLV_DATA_SIZE,
2479                     ha->dcbx_tlv, ha->dcbx_tlv_dma);
2480
2481         if (ha->xgmac_data)
2482                 dma_free_coherent(&ha->pdev->dev, XGMAC_DATA_SIZE,
2483                     ha->xgmac_data, ha->xgmac_data_dma);
2484
2485         if (ha->sns_cmd)
2486                 dma_free_coherent(&ha->pdev->dev, sizeof(struct sns_cmd_pkt),
2487                 ha->sns_cmd, ha->sns_cmd_dma);
2488
2489         if (ha->ct_sns)
2490                 dma_free_coherent(&ha->pdev->dev, sizeof(struct ct_sns_pkt),
2491                 ha->ct_sns, ha->ct_sns_dma);
2492
2493         if (ha->sfp_data)
2494                 dma_pool_free(ha->s_dma_pool, ha->sfp_data, ha->sfp_data_dma);
2495
2496         if (ha->edc_data)
2497                 dma_pool_free(ha->s_dma_pool, ha->edc_data, ha->edc_data_dma);
2498
2499         if (ha->ms_iocb)
2500                 dma_pool_free(ha->s_dma_pool, ha->ms_iocb, ha->ms_iocb_dma);
2501
2502         if (ha->ex_init_cb)
2503                 dma_pool_free(ha->s_dma_pool, ha->ex_init_cb, ha->ex_init_cb_dma);
2504
2505         if (ha->s_dma_pool)
2506                 dma_pool_destroy(ha->s_dma_pool);
2507
2508         if (ha->gid_list)
2509                 dma_free_coherent(&ha->pdev->dev, GID_LIST_SIZE, ha->gid_list,
2510                 ha->gid_list_dma);
2511
2512         if (ha->init_cb)
2513                 dma_free_coherent(&ha->pdev->dev, ha->init_cb_size,
2514                 ha->init_cb, ha->init_cb_dma);
2515         vfree(ha->optrom_buffer);
2516         kfree(ha->nvram);
2517         kfree(ha->npiv_info);
2518
2519         ha->srb_mempool = NULL;
2520         ha->eft = NULL;
2521         ha->eft_dma = 0;
2522         ha->sns_cmd = NULL;
2523         ha->sns_cmd_dma = 0;
2524         ha->ct_sns = NULL;
2525         ha->ct_sns_dma = 0;
2526         ha->ms_iocb = NULL;
2527         ha->ms_iocb_dma = 0;
2528         ha->init_cb = NULL;
2529         ha->init_cb_dma = 0;
2530         ha->ex_init_cb = NULL;
2531         ha->ex_init_cb_dma = 0;
2532
2533         ha->s_dma_pool = NULL;
2534
2535         ha->gid_list = NULL;
2536         ha->gid_list_dma = 0;
2537
2538         ha->fw_dump = NULL;
2539         ha->fw_dumped = 0;
2540         ha->fw_dump_reading = 0;
2541 }
2542
2543 struct scsi_qla_host *qla2x00_create_host(struct scsi_host_template *sht,
2544                                                 struct qla_hw_data *ha)
2545 {
2546         struct Scsi_Host *host;
2547         struct scsi_qla_host *vha = NULL;
2548
2549         host = scsi_host_alloc(sht, sizeof(scsi_qla_host_t));
2550         if (host == NULL) {
2551                 printk(KERN_WARNING
2552                 "qla2xxx: Couldn't allocate host from scsi layer!\n");
2553                 goto fail;
2554         }
2555
2556         /* Clear our data area */
2557         vha = shost_priv(host);
2558         memset(vha, 0, sizeof(scsi_qla_host_t));
2559
2560         vha->host = host;
2561         vha->host_no = host->host_no;
2562         vha->hw = ha;
2563
2564         INIT_LIST_HEAD(&vha->vp_fcports);
2565         INIT_LIST_HEAD(&vha->work_list);
2566         INIT_LIST_HEAD(&vha->list);
2567
2568         spin_lock_init(&vha->work_lock);
2569
2570         sprintf(vha->host_str, "%s_%ld", QLA2XXX_DRIVER_NAME, vha->host_no);
2571         return vha;
2572
2573 fail:
2574         return vha;
2575 }
2576
2577 static struct qla_work_evt *
2578 qla2x00_alloc_work(struct scsi_qla_host *vha, enum qla_work_type type)
2579 {
2580         struct qla_work_evt *e;
2581
2582         e = kzalloc(sizeof(struct qla_work_evt), GFP_ATOMIC);
2583         if (!e)
2584                 return NULL;
2585
2586         INIT_LIST_HEAD(&e->list);
2587         e->type = type;
2588         e->flags = QLA_EVT_FLAG_FREE;
2589         return e;
2590 }
2591
2592 static int
2593 qla2x00_post_work(struct scsi_qla_host *vha, struct qla_work_evt *e)
2594 {
2595         unsigned long flags;
2596
2597         spin_lock_irqsave(&vha->work_lock, flags);
2598         list_add_tail(&e->list, &vha->work_list);
2599         spin_unlock_irqrestore(&vha->work_lock, flags);
2600         qla2xxx_wake_dpc(vha);
2601
2602         return QLA_SUCCESS;
2603 }
2604
2605 int
2606 qla2x00_post_aen_work(struct scsi_qla_host *vha, enum fc_host_event_code code,
2607     u32 data)
2608 {
2609         struct qla_work_evt *e;
2610
2611         e = qla2x00_alloc_work(vha, QLA_EVT_AEN);
2612         if (!e)
2613                 return QLA_FUNCTION_FAILED;
2614
2615         e->u.aen.code = code;
2616         e->u.aen.data = data;
2617         return qla2x00_post_work(vha, e);
2618 }
2619
2620 int
2621 qla2x00_post_idc_ack_work(struct scsi_qla_host *vha, uint16_t *mb)
2622 {
2623         struct qla_work_evt *e;
2624
2625         e = qla2x00_alloc_work(vha, QLA_EVT_IDC_ACK);
2626         if (!e)
2627                 return QLA_FUNCTION_FAILED;
2628
2629         memcpy(e->u.idc_ack.mb, mb, QLA_IDC_ACK_REGS * sizeof(uint16_t));
2630         return qla2x00_post_work(vha, e);
2631 }
2632
2633 #define qla2x00_post_async_work(name, type)     \
2634 int qla2x00_post_async_##name##_work(           \
2635     struct scsi_qla_host *vha,                  \
2636     fc_port_t *fcport, uint16_t *data)          \
2637 {                                               \
2638         struct qla_work_evt *e;                 \
2639                                                 \
2640         e = qla2x00_alloc_work(vha, type);      \
2641         if (!e)                                 \
2642                 return QLA_FUNCTION_FAILED;     \
2643                                                 \
2644         e->u.logio.fcport = fcport;             \
2645         if (data) {                             \
2646                 e->u.logio.data[0] = data[0];   \
2647                 e->u.logio.data[1] = data[1];   \
2648         }                                       \
2649         return qla2x00_post_work(vha, e);       \
2650 }
2651
2652 qla2x00_post_async_work(login, QLA_EVT_ASYNC_LOGIN);
2653 qla2x00_post_async_work(login_done, QLA_EVT_ASYNC_LOGIN_DONE);
2654 qla2x00_post_async_work(logout, QLA_EVT_ASYNC_LOGOUT);
2655 qla2x00_post_async_work(logout_done, QLA_EVT_ASYNC_LOGOUT_DONE);
2656
2657 int
2658 qla2x00_post_uevent_work(struct scsi_qla_host *vha, u32 code)
2659 {
2660         struct qla_work_evt *e;
2661
2662         e = qla2x00_alloc_work(vha, QLA_EVT_UEVENT);
2663         if (!e)
2664                 return QLA_FUNCTION_FAILED;
2665
2666         e->u.uevent.code = code;
2667         return qla2x00_post_work(vha, e);
2668 }
2669
2670 static void
2671 qla2x00_uevent_emit(struct scsi_qla_host *vha, u32 code)
2672 {
2673         char event_string[40];
2674         char *envp[] = { event_string, NULL };
2675
2676         switch (code) {
2677         case QLA_UEVENT_CODE_FW_DUMP:
2678                 snprintf(event_string, sizeof(event_string), "FW_DUMP=%ld",
2679                     vha->host_no);
2680                 break;
2681         default:
2682                 /* do nothing */
2683                 break;
2684         }
2685         kobject_uevent_env(&vha->hw->pdev->dev.kobj, KOBJ_CHANGE, envp);
2686 }
2687
2688 void
2689 qla2x00_do_work(struct scsi_qla_host *vha)
2690 {
2691         struct qla_work_evt *e, *tmp;
2692         unsigned long flags;
2693         LIST_HEAD(work);
2694
2695         spin_lock_irqsave(&vha->work_lock, flags);
2696         list_splice_init(&vha->work_list, &work);
2697         spin_unlock_irqrestore(&vha->work_lock, flags);
2698
2699         list_for_each_entry_safe(e, tmp, &work, list) {
2700                 list_del_init(&e->list);
2701
2702                 switch (e->type) {
2703                 case QLA_EVT_AEN:
2704                         fc_host_post_event(vha->host, fc_get_event_number(),
2705                             e->u.aen.code, e->u.aen.data);
2706                         break;
2707                 case QLA_EVT_IDC_ACK:
2708                         qla81xx_idc_ack(vha, e->u.idc_ack.mb);
2709                         break;
2710                 case QLA_EVT_ASYNC_LOGIN:
2711                         qla2x00_async_login(vha, e->u.logio.fcport,
2712                             e->u.logio.data);
2713                         break;
2714                 case QLA_EVT_ASYNC_LOGIN_DONE:
2715                         qla2x00_async_login_done(vha, e->u.logio.fcport,
2716                             e->u.logio.data);
2717                         break;
2718                 case QLA_EVT_ASYNC_LOGOUT:
2719                         qla2x00_async_logout(vha, e->u.logio.fcport);
2720                         break;
2721                 case QLA_EVT_ASYNC_LOGOUT_DONE:
2722                         qla2x00_async_logout_done(vha, e->u.logio.fcport,
2723                             e->u.logio.data);
2724                         break;
2725                 case QLA_EVT_UEVENT:
2726                         qla2x00_uevent_emit(vha, e->u.uevent.code);
2727                         break;
2728                 }
2729                 if (e->flags & QLA_EVT_FLAG_FREE)
2730                         kfree(e);
2731         }
2732 }
2733
2734 /* Relogins all the fcports of a vport
2735  * Context: dpc thread
2736  */
2737 void qla2x00_relogin(struct scsi_qla_host *vha)
2738 {
2739         fc_port_t       *fcport;
2740         int status;
2741         uint16_t        next_loopid = 0;
2742         struct qla_hw_data *ha = vha->hw;
2743         uint16_t data[2];
2744
2745         list_for_each_entry(fcport, &vha->vp_fcports, list) {
2746         /*
2747          * If the port is not ONLINE then try to login
2748          * to it if we haven't run out of retries.
2749          */
2750                 if (atomic_read(&fcport->state) !=
2751                         FCS_ONLINE && fcport->login_retry) {
2752
2753                         fcport->login_retry--;
2754                         if (fcport->flags & FCF_FABRIC_DEVICE) {
2755                                 if (fcport->flags & FCF_TAPE_PRESENT)
2756                                         ha->isp_ops->fabric_logout(vha,
2757                                                         fcport->loop_id,
2758                                                         fcport->d_id.b.domain,
2759                                                         fcport->d_id.b.area,
2760                                                         fcport->d_id.b.al_pa);
2761
2762                                 if (IS_ALOGIO_CAPABLE(ha)) {
2763                                         data[0] = 0;
2764                                         data[1] = QLA_LOGIO_LOGIN_RETRIED;
2765                                         status = qla2x00_post_async_login_work(
2766                                             vha, fcport, data);
2767                                         if (status == QLA_SUCCESS)
2768                                                 continue;
2769                                         /* Attempt a retry. */
2770                                         status = 1;
2771                                 } else
2772                                         status = qla2x00_fabric_login(vha,
2773                                             fcport, &next_loopid);
2774                         } else
2775                                 status = qla2x00_local_device_login(vha,
2776                                                                 fcport);
2777
2778                         if (status == QLA_SUCCESS) {
2779                                 fcport->old_loop_id = fcport->loop_id;
2780
2781                                 DEBUG(printk("scsi(%ld): port login OK: logged "
2782                                 "in ID 0x%x\n", vha->host_no, fcport->loop_id));
2783
2784                                 qla2x00_update_fcport(vha, fcport);
2785
2786                         } else if (status == 1) {
2787                                 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
2788                                 /* retry the login again */
2789                                 DEBUG(printk("scsi(%ld): Retrying"
2790                                 " %d login again loop_id 0x%x\n",
2791                                 vha->host_no, fcport->login_retry,
2792                                                 fcport->loop_id));
2793                         } else {
2794                                 fcport->login_retry = 0;
2795                         }
2796
2797                         if (fcport->login_retry == 0 && status != QLA_SUCCESS)
2798                                 fcport->loop_id = FC_NO_LOOP_ID;
2799                 }
2800                 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
2801                         break;
2802         }
2803 }
2804
2805 /**************************************************************************
2806 * qla2x00_do_dpc
2807 *   This kernel thread is a task that is schedule by the interrupt handler
2808 *   to perform the background processing for interrupts.
2809 *
2810 * Notes:
2811 * This task always run in the context of a kernel thread.  It
2812 * is kick-off by the driver's detect code and starts up
2813 * up one per adapter. It immediately goes to sleep and waits for
2814 * some fibre event.  When either the interrupt handler or
2815 * the timer routine detects a event it will one of the task
2816 * bits then wake us up.
2817 **************************************************************************/
2818 static int
2819 qla2x00_do_dpc(void *data)
2820 {
2821         int             rval;
2822         scsi_qla_host_t *base_vha;
2823         struct qla_hw_data *ha;
2824
2825         ha = (struct qla_hw_data *)data;
2826         base_vha = pci_get_drvdata(ha->pdev);
2827
2828         set_user_nice(current, -20);
2829
2830         while (!kthread_should_stop()) {
2831                 DEBUG3(printk("qla2x00: DPC handler sleeping\n"));
2832
2833                 set_current_state(TASK_INTERRUPTIBLE);
2834                 schedule();
2835                 __set_current_state(TASK_RUNNING);
2836
2837                 DEBUG3(printk("qla2x00: DPC handler waking up\n"));
2838
2839                 /* Initialization not yet finished. Don't do anything yet. */
2840                 if (!base_vha->flags.init_done)
2841                         continue;
2842
2843                 DEBUG3(printk("scsi(%ld): DPC handler\n", base_vha->host_no));
2844
2845                 ha->dpc_active = 1;
2846
2847                 if (ha->flags.mbox_busy) {
2848                         ha->dpc_active = 0;
2849                         continue;
2850                 }
2851
2852                 qla2x00_do_work(base_vha);
2853
2854                 if (test_and_clear_bit(ISP_ABORT_NEEDED,
2855                                                 &base_vha->dpc_flags)) {
2856
2857                         DEBUG(printk("scsi(%ld): dpc: sched "
2858                             "qla2x00_abort_isp ha = %p\n",
2859                             base_vha->host_no, ha));
2860                         if (!(test_and_set_bit(ABORT_ISP_ACTIVE,
2861                             &base_vha->dpc_flags))) {
2862
2863                                 if (qla2x00_abort_isp(base_vha)) {
2864                                         /* failed. retry later */
2865                                         set_bit(ISP_ABORT_NEEDED,
2866                                             &base_vha->dpc_flags);
2867                                 }
2868                                 clear_bit(ABORT_ISP_ACTIVE,
2869                                                 &base_vha->dpc_flags);
2870                         }
2871
2872                         DEBUG(printk("scsi(%ld): dpc: qla2x00_abort_isp end\n",
2873                             base_vha->host_no));
2874                 }
2875
2876                 if (test_bit(FCPORT_UPDATE_NEEDED, &base_vha->dpc_flags)) {
2877                         qla2x00_update_fcports(base_vha);
2878                         clear_bit(FCPORT_UPDATE_NEEDED, &base_vha->dpc_flags);
2879                 }
2880
2881                 if (test_and_clear_bit(RESET_MARKER_NEEDED,
2882                                                         &base_vha->dpc_flags) &&
2883                     (!(test_and_set_bit(RESET_ACTIVE, &base_vha->dpc_flags)))) {
2884
2885                         DEBUG(printk("scsi(%ld): qla2x00_reset_marker()\n",
2886                             base_vha->host_no));
2887
2888                         qla2x00_rst_aen(base_vha);
2889                         clear_bit(RESET_ACTIVE, &base_vha->dpc_flags);
2890                 }
2891
2892                 /* Retry each device up to login retry count */
2893                 if ((test_and_clear_bit(RELOGIN_NEEDED,
2894                                                 &base_vha->dpc_flags)) &&
2895                     !test_bit(LOOP_RESYNC_NEEDED, &base_vha->dpc_flags) &&
2896                     atomic_read(&base_vha->loop_state) != LOOP_DOWN) {
2897
2898                         DEBUG(printk("scsi(%ld): qla2x00_port_login()\n",
2899                                         base_vha->host_no));
2900                         qla2x00_relogin(base_vha);
2901
2902                         DEBUG(printk("scsi(%ld): qla2x00_port_login - end\n",
2903                             base_vha->host_no));
2904                 }
2905
2906                 if (test_and_clear_bit(LOOP_RESYNC_NEEDED,
2907                                                         &base_vha->dpc_flags)) {
2908
2909                         DEBUG(printk("scsi(%ld): qla2x00_loop_resync()\n",
2910                                 base_vha->host_no));
2911
2912                         if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE,
2913                             &base_vha->dpc_flags))) {
2914
2915                                 rval = qla2x00_loop_resync(base_vha);
2916
2917                                 clear_bit(LOOP_RESYNC_ACTIVE,
2918                                                 &base_vha->dpc_flags);
2919                         }
2920
2921                         DEBUG(printk("scsi(%ld): qla2x00_loop_resync - end\n",
2922                             base_vha->host_no));
2923                 }
2924
2925                 if (test_bit(NPIV_CONFIG_NEEDED, &base_vha->dpc_flags) &&
2926                     atomic_read(&base_vha->loop_state) == LOOP_READY) {
2927                         clear_bit(NPIV_CONFIG_NEEDED, &base_vha->dpc_flags);
2928                         qla2xxx_flash_npiv_conf(base_vha);
2929                 }
2930
2931                 if (!ha->interrupts_on)
2932                         ha->isp_ops->enable_intrs(ha);
2933
2934                 if (test_and_clear_bit(BEACON_BLINK_NEEDED,
2935                                         &base_vha->dpc_flags))
2936                         ha->isp_ops->beacon_blink(base_vha);
2937
2938                 qla2x00_do_dpc_all_vps(base_vha);
2939
2940                 ha->dpc_active = 0;
2941         } /* End of while(1) */
2942
2943         DEBUG(printk("scsi(%ld): DPC handler exiting\n", base_vha->host_no));
2944
2945         /*
2946          * Make sure that nobody tries to wake us up again.
2947          */
2948         ha->dpc_active = 0;
2949
2950         /* Cleanup any residual CTX SRBs. */
2951         qla2x00_abort_all_cmds(base_vha, DID_NO_CONNECT << 16);
2952
2953         return 0;
2954 }
2955
2956 void
2957 qla2xxx_wake_dpc(struct scsi_qla_host *vha)
2958 {
2959         struct qla_hw_data *ha = vha->hw;
2960         struct task_struct *t = ha->dpc_thread;
2961
2962         if (!test_bit(UNLOADING, &vha->dpc_flags) && t)
2963                 wake_up_process(t);
2964 }
2965
2966 /*
2967 *  qla2x00_rst_aen
2968 *      Processes asynchronous reset.
2969 *
2970 * Input:
2971 *      ha  = adapter block pointer.
2972 */
2973 static void
2974 qla2x00_rst_aen(scsi_qla_host_t *vha)
2975 {
2976         if (vha->flags.online && !vha->flags.reset_active &&
2977             !atomic_read(&vha->loop_down_timer) &&
2978             !(test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))) {
2979                 do {
2980                         clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
2981
2982                         /*
2983                          * Issue marker command only when we are going to start
2984                          * the I/O.
2985                          */
2986                         vha->marker_needed = 1;
2987                 } while (!atomic_read(&vha->loop_down_timer) &&
2988                     (test_bit(RESET_MARKER_NEEDED, &vha->dpc_flags)));
2989         }
2990 }
2991
2992 static void
2993 qla2x00_sp_free_dma(srb_t *sp)
2994 {
2995         struct scsi_cmnd *cmd = sp->cmd;
2996
2997         if (sp->flags & SRB_DMA_VALID) {
2998                 scsi_dma_unmap(cmd);
2999                 sp->flags &= ~SRB_DMA_VALID;
3000         }
3001         CMD_SP(cmd) = NULL;
3002 }
3003
3004 void
3005 qla2x00_sp_compl(struct qla_hw_data *ha, srb_t *sp)
3006 {
3007         struct scsi_cmnd *cmd = sp->cmd;
3008
3009         qla2x00_sp_free_dma(sp);
3010
3011         mempool_free(sp, ha->srb_mempool);
3012
3013         cmd->scsi_done(cmd);
3014 }
3015
3016 /**************************************************************************
3017 *   qla2x00_timer
3018 *
3019 * Description:
3020 *   One second timer
3021 *
3022 * Context: Interrupt
3023 ***************************************************************************/
3024 void
3025 qla2x00_timer(scsi_qla_host_t *vha)
3026 {
3027         unsigned long   cpu_flags = 0;
3028         fc_port_t       *fcport;
3029         int             start_dpc = 0;
3030         int             index;
3031         srb_t           *sp;
3032         int             t;
3033         struct qla_hw_data *ha = vha->hw;
3034         struct req_que *req;
3035         /*
3036          * Ports - Port down timer.
3037          *
3038          * Whenever, a port is in the LOST state we start decrementing its port
3039          * down timer every second until it reaches zero. Once  it reaches zero
3040          * the port it marked DEAD.
3041          */
3042         t = 0;
3043         list_for_each_entry(fcport, &vha->vp_fcports, list) {
3044                 if (fcport->port_type != FCT_TARGET)
3045                         continue;
3046
3047                 if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
3048
3049                         if (atomic_read(&fcport->port_down_timer) == 0)
3050                                 continue;
3051
3052                         if (atomic_dec_and_test(&fcport->port_down_timer) != 0)
3053                                 atomic_set(&fcport->state, FCS_DEVICE_DEAD);
3054
3055                         DEBUG(printk("scsi(%ld): fcport-%d - port retry count: "
3056                             "%d remaining\n",
3057                             vha->host_no,
3058                             t, atomic_read(&fcport->port_down_timer)));
3059                 }
3060                 t++;
3061         } /* End of for fcport  */
3062
3063
3064         /* Loop down handler. */
3065         if (atomic_read(&vha->loop_down_timer) > 0 &&
3066             !(test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))
3067                 && vha->flags.online) {
3068
3069                 if (atomic_read(&vha->loop_down_timer) ==
3070                     vha->loop_down_abort_time) {
3071
3072                         DEBUG(printk("scsi(%ld): Loop Down - aborting the "
3073                             "queues before time expire\n",
3074                             vha->host_no));
3075
3076                         if (!IS_QLA2100(ha) && vha->link_down_timeout)
3077                                 atomic_set(&vha->loop_state, LOOP_DEAD);
3078
3079                         /* Schedule an ISP abort to return any tape commands. */
3080                         /* NPIV - scan physical port only */
3081                         if (!vha->vp_idx) {
3082                                 spin_lock_irqsave(&ha->hardware_lock,
3083                                     cpu_flags);
3084                                 req = ha->req_q_map[0];
3085                                 for (index = 1;
3086                                     index < MAX_OUTSTANDING_COMMANDS;
3087                                     index++) {
3088                                         fc_port_t *sfcp;
3089
3090                                         sp = req->outstanding_cmds[index];
3091                                         if (!sp)
3092                                                 continue;
3093                                         if (sp->ctx)
3094                                                 continue;
3095                                         sfcp = sp->fcport;
3096                                         if (!(sfcp->flags & FCF_TAPE_PRESENT))
3097                                                 continue;
3098
3099                                         set_bit(ISP_ABORT_NEEDED,
3100                                                         &vha->dpc_flags);
3101                                         break;
3102                                 }
3103                                 spin_unlock_irqrestore(&ha->hardware_lock,
3104                                                                 cpu_flags);
3105                         }
3106                         start_dpc++;
3107                 }
3108
3109                 /* if the loop has been down for 4 minutes, reinit adapter */
3110                 if (atomic_dec_and_test(&vha->loop_down_timer) != 0) {
3111                         if (!(vha->device_flags & DFLG_NO_CABLE)) {
3112                                 DEBUG(printk("scsi(%ld): Loop down - "
3113                                     "aborting ISP.\n",
3114                                     vha->host_no));
3115                                 qla_printk(KERN_WARNING, ha,
3116                                     "Loop down - aborting ISP.\n");
3117
3118                                 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3119                         }
3120                 }
3121                 DEBUG3(printk("scsi(%ld): Loop Down - seconds remaining %d\n",
3122                     vha->host_no,
3123                     atomic_read(&vha->loop_down_timer)));
3124         }
3125
3126         /* Check if beacon LED needs to be blinked */
3127         if (ha->beacon_blink_led == 1) {
3128                 set_bit(BEACON_BLINK_NEEDED, &vha->dpc_flags);
3129                 start_dpc++;
3130         }
3131
3132         /* Process any deferred work. */
3133         if (!list_empty(&vha->work_list))
3134                 start_dpc++;
3135
3136         /* Schedule the DPC routine if needed */
3137         if ((test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
3138             test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags) ||
3139             test_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags) ||
3140             start_dpc ||
3141             test_bit(RESET_MARKER_NEEDED, &vha->dpc_flags) ||
3142             test_bit(BEACON_BLINK_NEEDED, &vha->dpc_flags) ||
3143             test_bit(VP_DPC_NEEDED, &vha->dpc_flags) ||
3144             test_bit(RELOGIN_NEEDED, &vha->dpc_flags)))
3145                 qla2xxx_wake_dpc(vha);
3146
3147         qla2x00_restart_timer(vha, WATCH_INTERVAL);
3148 }
3149
3150 /* Firmware interface routines. */
3151
3152 #define FW_BLOBS        7
3153 #define FW_ISP21XX      0
3154 #define FW_ISP22XX      1
3155 #define FW_ISP2300      2
3156 #define FW_ISP2322      3
3157 #define FW_ISP24XX      4
3158 #define FW_ISP25XX      5
3159 #define FW_ISP81XX      6
3160
3161 #define FW_FILE_ISP21XX "ql2100_fw.bin"
3162 #define FW_FILE_ISP22XX "ql2200_fw.bin"
3163 #define FW_FILE_ISP2300 "ql2300_fw.bin"
3164 #define FW_FILE_ISP2322 "ql2322_fw.bin"
3165 #define FW_FILE_ISP24XX "ql2400_fw.bin"
3166 #define FW_FILE_ISP25XX "ql2500_fw.bin"
3167 #define FW_FILE_ISP81XX "ql8100_fw.bin"
3168
3169 static DEFINE_MUTEX(qla_fw_lock);
3170
3171 static struct fw_blob qla_fw_blobs[FW_BLOBS] = {
3172         { .name = FW_FILE_ISP21XX, .segs = { 0x1000, 0 }, },
3173         { .name = FW_FILE_ISP22XX, .segs = { 0x1000, 0 }, },
3174         { .name = FW_FILE_ISP2300, .segs = { 0x800, 0 }, },
3175         { .name = FW_FILE_ISP2322, .segs = { 0x800, 0x1c000, 0x1e000, 0 }, },
3176         { .name = FW_FILE_ISP24XX, },
3177         { .name = FW_FILE_ISP25XX, },
3178         { .name = FW_FILE_ISP81XX, },
3179 };
3180
3181 struct fw_blob *
3182 qla2x00_request_firmware(scsi_qla_host_t *vha)
3183 {
3184         struct qla_hw_data *ha = vha->hw;
3185         struct fw_blob *blob;
3186
3187         blob = NULL;
3188         if (IS_QLA2100(ha)) {
3189                 blob = &qla_fw_blobs[FW_ISP21XX];
3190         } else if (IS_QLA2200(ha)) {
3191                 blob = &qla_fw_blobs[FW_ISP22XX];
3192         } else if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
3193                 blob = &qla_fw_blobs[FW_ISP2300];
3194         } else if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
3195                 blob = &qla_fw_blobs[FW_ISP2322];
3196         } else if (IS_QLA24XX_TYPE(ha)) {
3197                 blob = &qla_fw_blobs[FW_ISP24XX];
3198         } else if (IS_QLA25XX(ha)) {
3199                 blob = &qla_fw_blobs[FW_ISP25XX];
3200         } else if (IS_QLA81XX(ha)) {
3201                 blob = &qla_fw_blobs[FW_ISP81XX];
3202         }
3203
3204         mutex_lock(&qla_fw_lock);
3205         if (blob->fw)
3206                 goto out;
3207
3208         if (request_firmware(&blob->fw, blob->name, &ha->pdev->dev)) {
3209                 DEBUG2(printk("scsi(%ld): Failed to load firmware image "
3210                     "(%s).\n", vha->host_no, blob->name));
3211                 blob->fw = NULL;
3212                 blob = NULL;
3213                 goto out;
3214         }
3215
3216 out:
3217         mutex_unlock(&qla_fw_lock);
3218         return blob;
3219 }
3220
3221 static void
3222 qla2x00_release_firmware(void)
3223 {
3224         int idx;
3225
3226         mutex_lock(&qla_fw_lock);
3227         for (idx = 0; idx < FW_BLOBS; idx++)
3228                 if (qla_fw_blobs[idx].fw)
3229                         release_firmware(qla_fw_blobs[idx].fw);
3230         mutex_unlock(&qla_fw_lock);
3231 }
3232
3233 static pci_ers_result_t
3234 qla2xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
3235 {
3236         scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
3237
3238         switch (state) {
3239         case pci_channel_io_normal:
3240                 return PCI_ERS_RESULT_CAN_RECOVER;
3241         case pci_channel_io_frozen:
3242                 pci_disable_device(pdev);
3243                 return PCI_ERS_RESULT_NEED_RESET;
3244         case pci_channel_io_perm_failure:
3245                 qla2x00_abort_all_cmds(base_vha, DID_NO_CONNECT << 16);
3246                 return PCI_ERS_RESULT_DISCONNECT;
3247         }
3248         return PCI_ERS_RESULT_NEED_RESET;
3249 }
3250
3251 static pci_ers_result_t
3252 qla2xxx_pci_mmio_enabled(struct pci_dev *pdev)
3253 {
3254         int risc_paused = 0;
3255         uint32_t stat;
3256         unsigned long flags;
3257         scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
3258         struct qla_hw_data *ha = base_vha->hw;
3259         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
3260         struct device_reg_24xx __iomem *reg24 = &ha->iobase->isp24;
3261
3262         spin_lock_irqsave(&ha->hardware_lock, flags);
3263         if (IS_QLA2100(ha) || IS_QLA2200(ha)){
3264                 stat = RD_REG_DWORD(&reg->hccr);
3265                 if (stat & HCCR_RISC_PAUSE)
3266                         risc_paused = 1;
3267         } else if (IS_QLA23XX(ha)) {
3268                 stat = RD_REG_DWORD(&reg->u.isp2300.host_status);
3269                 if (stat & HSR_RISC_PAUSED)
3270                         risc_paused = 1;
3271         } else if (IS_FWI2_CAPABLE(ha)) {
3272                 stat = RD_REG_DWORD(&reg24->host_status);
3273                 if (stat & HSRX_RISC_PAUSED)
3274                         risc_paused = 1;
3275         }
3276         spin_unlock_irqrestore(&ha->hardware_lock, flags);
3277
3278         if (risc_paused) {
3279                 qla_printk(KERN_INFO, ha, "RISC paused -- mmio_enabled, "
3280                     "Dumping firmware!\n");
3281                 ha->isp_ops->fw_dump(base_vha, 0);
3282
3283                 return PCI_ERS_RESULT_NEED_RESET;
3284         } else
3285                 return PCI_ERS_RESULT_RECOVERED;
3286 }
3287
3288 static pci_ers_result_t
3289 qla2xxx_pci_slot_reset(struct pci_dev *pdev)
3290 {
3291         pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
3292         scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
3293         struct qla_hw_data *ha = base_vha->hw;
3294         int rc;
3295
3296         if (ha->mem_only)
3297                 rc = pci_enable_device_mem(pdev);
3298         else
3299                 rc = pci_enable_device(pdev);
3300
3301         if (rc) {
3302                 qla_printk(KERN_WARNING, ha,
3303                     "Can't re-enable PCI device after reset.\n");
3304
3305                 return ret;
3306         }
3307         pci_set_master(pdev);
3308
3309         if (ha->isp_ops->pci_config(base_vha))
3310                 return ret;
3311
3312         set_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
3313         if (qla2x00_abort_isp(base_vha) == QLA_SUCCESS)
3314                 ret =  PCI_ERS_RESULT_RECOVERED;
3315         clear_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
3316
3317         return ret;
3318 }
3319
3320 static void
3321 qla2xxx_pci_resume(struct pci_dev *pdev)
3322 {
3323         scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
3324         struct qla_hw_data *ha = base_vha->hw;
3325         int ret;
3326
3327         ret = qla2x00_wait_for_hba_online(base_vha);
3328         if (ret != QLA_SUCCESS) {
3329                 qla_printk(KERN_ERR, ha,
3330                     "the device failed to resume I/O "
3331                     "from slot/link_reset");
3332         }
3333         pci_cleanup_aer_uncorrect_error_status(pdev);
3334 }
3335
3336 static struct pci_error_handlers qla2xxx_err_handler = {
3337         .error_detected = qla2xxx_pci_error_detected,
3338         .mmio_enabled = qla2xxx_pci_mmio_enabled,
3339         .slot_reset = qla2xxx_pci_slot_reset,
3340         .resume = qla2xxx_pci_resume,
3341 };
3342
3343 static struct pci_device_id qla2xxx_pci_tbl[] = {
3344         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2100) },
3345         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2200) },
3346         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2300) },
3347         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2312) },
3348         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2322) },
3349         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP6312) },
3350         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP6322) },
3351         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2422) },
3352         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2432) },
3353         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP8432) },
3354         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP5422) },
3355         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP5432) },
3356         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2532) },
3357         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP8001) },
3358         { 0 },
3359 };
3360 MODULE_DEVICE_TABLE(pci, qla2xxx_pci_tbl);
3361
3362 static struct pci_driver qla2xxx_pci_driver = {
3363         .name           = QLA2XXX_DRIVER_NAME,
3364         .driver         = {
3365                 .owner          = THIS_MODULE,
3366         },
3367         .id_table       = qla2xxx_pci_tbl,
3368         .probe          = qla2x00_probe_one,
3369         .remove         = qla2x00_remove_one,
3370         .err_handler    = &qla2xxx_err_handler,
3371 };
3372
3373 /**
3374  * qla2x00_module_init - Module initialization.
3375  **/
3376 static int __init
3377 qla2x00_module_init(void)
3378 {
3379         int ret = 0;
3380
3381         /* Allocate cache for SRBs. */
3382         srb_cachep = kmem_cache_create("qla2xxx_srbs", sizeof(srb_t), 0,
3383             SLAB_HWCACHE_ALIGN, NULL);
3384         if (srb_cachep == NULL) {
3385                 printk(KERN_ERR
3386                     "qla2xxx: Unable to allocate SRB cache...Failing load!\n");
3387                 return -ENOMEM;
3388         }
3389
3390         /* Derive version string. */
3391         strcpy(qla2x00_version_str, QLA2XXX_VERSION);
3392         if (ql2xextended_error_logging)
3393                 strcat(qla2x00_version_str, "-debug");
3394
3395         qla2xxx_transport_template =
3396             fc_attach_transport(&qla2xxx_transport_functions);
3397         if (!qla2xxx_transport_template) {
3398                 kmem_cache_destroy(srb_cachep);
3399                 return -ENODEV;
3400         }
3401         qla2xxx_transport_vport_template =
3402             fc_attach_transport(&qla2xxx_transport_vport_functions);
3403         if (!qla2xxx_transport_vport_template) {
3404                 kmem_cache_destroy(srb_cachep);
3405                 fc_release_transport(qla2xxx_transport_template);
3406                 return -ENODEV;
3407         }
3408
3409         printk(KERN_INFO "QLogic Fibre Channel HBA Driver: %s\n",
3410             qla2x00_version_str);
3411         ret = pci_register_driver(&qla2xxx_pci_driver);
3412         if (ret) {
3413                 kmem_cache_destroy(srb_cachep);
3414                 fc_release_transport(qla2xxx_transport_template);
3415                 fc_release_transport(qla2xxx_transport_vport_template);
3416         }
3417         return ret;
3418 }
3419
3420 /**
3421  * qla2x00_module_exit - Module cleanup.
3422  **/
3423 static void __exit
3424 qla2x00_module_exit(void)
3425 {
3426         pci_unregister_driver(&qla2xxx_pci_driver);
3427         qla2x00_release_firmware();
3428         kmem_cache_destroy(srb_cachep);
3429         fc_release_transport(qla2xxx_transport_template);
3430         fc_release_transport(qla2xxx_transport_vport_template);
3431 }
3432
3433 module_init(qla2x00_module_init);
3434 module_exit(qla2x00_module_exit);
3435
3436 MODULE_AUTHOR("QLogic Corporation");
3437 MODULE_DESCRIPTION("QLogic Fibre Channel HBA Driver");
3438 MODULE_LICENSE("GPL");
3439 MODULE_VERSION(QLA2XXX_VERSION);
3440 MODULE_FIRMWARE(FW_FILE_ISP21XX);
3441 MODULE_FIRMWARE(FW_FILE_ISP22XX);
3442 MODULE_FIRMWARE(FW_FILE_ISP2300);
3443 MODULE_FIRMWARE(FW_FILE_ISP2322);
3444 MODULE_FIRMWARE(FW_FILE_ISP24XX);
3445 MODULE_FIRMWARE(FW_FILE_ISP25XX);
3446 MODULE_FIRMWARE(FW_FILE_ISP81XX);