d69744a62fe40cb82dbde3d5c7608879c1c4be8d
[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, 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, int reason)
1239 {
1240         if (reason != SCSI_QDEPTH_DEFAULT)
1241                 return -EOPNOTSUPP;
1242
1243         scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
1244         return sdev->queue_depth;
1245 }
1246
1247 static int
1248 qla2x00_change_queue_type(struct scsi_device *sdev, int tag_type)
1249 {
1250         if (sdev->tagged_supported) {
1251                 scsi_set_tag_type(sdev, tag_type);
1252                 if (tag_type)
1253                         scsi_activate_tcq(sdev, sdev->queue_depth);
1254                 else
1255                         scsi_deactivate_tcq(sdev, sdev->queue_depth);
1256         } else
1257                 tag_type = 0;
1258
1259         return tag_type;
1260 }
1261
1262 /**
1263  * qla2x00_config_dma_addressing() - Configure OS DMA addressing method.
1264  * @ha: HA context
1265  *
1266  * At exit, the @ha's flags.enable_64bit_addressing set to indicated
1267  * supported addressing method.
1268  */
1269 static void
1270 qla2x00_config_dma_addressing(struct qla_hw_data *ha)
1271 {
1272         /* Assume a 32bit DMA mask. */
1273         ha->flags.enable_64bit_addressing = 0;
1274
1275         if (!dma_set_mask(&ha->pdev->dev, DMA_BIT_MASK(64))) {
1276                 /* Any upper-dword bits set? */
1277                 if (MSD(dma_get_required_mask(&ha->pdev->dev)) &&
1278                     !pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
1279                         /* Ok, a 64bit DMA mask is applicable. */
1280                         ha->flags.enable_64bit_addressing = 1;
1281                         ha->isp_ops->calc_req_entries = qla2x00_calc_iocbs_64;
1282                         ha->isp_ops->build_iocbs = qla2x00_build_scsi_iocbs_64;
1283                         return;
1284                 }
1285         }
1286
1287         dma_set_mask(&ha->pdev->dev, DMA_BIT_MASK(32));
1288         pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(32));
1289 }
1290
1291 static void
1292 qla2x00_enable_intrs(struct qla_hw_data *ha)
1293 {
1294         unsigned long flags = 0;
1295         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1296
1297         spin_lock_irqsave(&ha->hardware_lock, flags);
1298         ha->interrupts_on = 1;
1299         /* enable risc and host interrupts */
1300         WRT_REG_WORD(&reg->ictrl, ICR_EN_INT | ICR_EN_RISC);
1301         RD_REG_WORD(&reg->ictrl);
1302         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1303
1304 }
1305
1306 static void
1307 qla2x00_disable_intrs(struct qla_hw_data *ha)
1308 {
1309         unsigned long flags = 0;
1310         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1311
1312         spin_lock_irqsave(&ha->hardware_lock, flags);
1313         ha->interrupts_on = 0;
1314         /* disable risc and host interrupts */
1315         WRT_REG_WORD(&reg->ictrl, 0);
1316         RD_REG_WORD(&reg->ictrl);
1317         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1318 }
1319
1320 static void
1321 qla24xx_enable_intrs(struct qla_hw_data *ha)
1322 {
1323         unsigned long flags = 0;
1324         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1325
1326         spin_lock_irqsave(&ha->hardware_lock, flags);
1327         ha->interrupts_on = 1;
1328         WRT_REG_DWORD(&reg->ictrl, ICRX_EN_RISC_INT);
1329         RD_REG_DWORD(&reg->ictrl);
1330         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1331 }
1332
1333 static void
1334 qla24xx_disable_intrs(struct qla_hw_data *ha)
1335 {
1336         unsigned long flags = 0;
1337         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1338
1339         if (IS_NOPOLLING_TYPE(ha))
1340                 return;
1341         spin_lock_irqsave(&ha->hardware_lock, flags);
1342         ha->interrupts_on = 0;
1343         WRT_REG_DWORD(&reg->ictrl, 0);
1344         RD_REG_DWORD(&reg->ictrl);
1345         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1346 }
1347
1348 static struct isp_operations qla2100_isp_ops = {
1349         .pci_config             = qla2100_pci_config,
1350         .reset_chip             = qla2x00_reset_chip,
1351         .chip_diag              = qla2x00_chip_diag,
1352         .config_rings           = qla2x00_config_rings,
1353         .reset_adapter          = qla2x00_reset_adapter,
1354         .nvram_config           = qla2x00_nvram_config,
1355         .update_fw_options      = qla2x00_update_fw_options,
1356         .load_risc              = qla2x00_load_risc,
1357         .pci_info_str           = qla2x00_pci_info_str,
1358         .fw_version_str         = qla2x00_fw_version_str,
1359         .intr_handler           = qla2100_intr_handler,
1360         .enable_intrs           = qla2x00_enable_intrs,
1361         .disable_intrs          = qla2x00_disable_intrs,
1362         .abort_command          = qla2x00_abort_command,
1363         .target_reset           = qla2x00_abort_target,
1364         .lun_reset              = qla2x00_lun_reset,
1365         .fabric_login           = qla2x00_login_fabric,
1366         .fabric_logout          = qla2x00_fabric_logout,
1367         .calc_req_entries       = qla2x00_calc_iocbs_32,
1368         .build_iocbs            = qla2x00_build_scsi_iocbs_32,
1369         .prep_ms_iocb           = qla2x00_prep_ms_iocb,
1370         .prep_ms_fdmi_iocb      = qla2x00_prep_ms_fdmi_iocb,
1371         .read_nvram             = qla2x00_read_nvram_data,
1372         .write_nvram            = qla2x00_write_nvram_data,
1373         .fw_dump                = qla2100_fw_dump,
1374         .beacon_on              = NULL,
1375         .beacon_off             = NULL,
1376         .beacon_blink           = NULL,
1377         .read_optrom            = qla2x00_read_optrom_data,
1378         .write_optrom           = qla2x00_write_optrom_data,
1379         .get_flash_version      = qla2x00_get_flash_version,
1380         .start_scsi             = qla2x00_start_scsi,
1381 };
1382
1383 static struct isp_operations qla2300_isp_ops = {
1384         .pci_config             = qla2300_pci_config,
1385         .reset_chip             = qla2x00_reset_chip,
1386         .chip_diag              = qla2x00_chip_diag,
1387         .config_rings           = qla2x00_config_rings,
1388         .reset_adapter          = qla2x00_reset_adapter,
1389         .nvram_config           = qla2x00_nvram_config,
1390         .update_fw_options      = qla2x00_update_fw_options,
1391         .load_risc              = qla2x00_load_risc,
1392         .pci_info_str           = qla2x00_pci_info_str,
1393         .fw_version_str         = qla2x00_fw_version_str,
1394         .intr_handler           = qla2300_intr_handler,
1395         .enable_intrs           = qla2x00_enable_intrs,
1396         .disable_intrs          = qla2x00_disable_intrs,
1397         .abort_command          = qla2x00_abort_command,
1398         .target_reset           = qla2x00_abort_target,
1399         .lun_reset              = qla2x00_lun_reset,
1400         .fabric_login           = qla2x00_login_fabric,
1401         .fabric_logout          = qla2x00_fabric_logout,
1402         .calc_req_entries       = qla2x00_calc_iocbs_32,
1403         .build_iocbs            = qla2x00_build_scsi_iocbs_32,
1404         .prep_ms_iocb           = qla2x00_prep_ms_iocb,
1405         .prep_ms_fdmi_iocb      = qla2x00_prep_ms_fdmi_iocb,
1406         .read_nvram             = qla2x00_read_nvram_data,
1407         .write_nvram            = qla2x00_write_nvram_data,
1408         .fw_dump                = qla2300_fw_dump,
1409         .beacon_on              = qla2x00_beacon_on,
1410         .beacon_off             = qla2x00_beacon_off,
1411         .beacon_blink           = qla2x00_beacon_blink,
1412         .read_optrom            = qla2x00_read_optrom_data,
1413         .write_optrom           = qla2x00_write_optrom_data,
1414         .get_flash_version      = qla2x00_get_flash_version,
1415         .start_scsi             = qla2x00_start_scsi,
1416 };
1417
1418 static struct isp_operations qla24xx_isp_ops = {
1419         .pci_config             = qla24xx_pci_config,
1420         .reset_chip             = qla24xx_reset_chip,
1421         .chip_diag              = qla24xx_chip_diag,
1422         .config_rings           = qla24xx_config_rings,
1423         .reset_adapter          = qla24xx_reset_adapter,
1424         .nvram_config           = qla24xx_nvram_config,
1425         .update_fw_options      = qla24xx_update_fw_options,
1426         .load_risc              = qla24xx_load_risc,
1427         .pci_info_str           = qla24xx_pci_info_str,
1428         .fw_version_str         = qla24xx_fw_version_str,
1429         .intr_handler           = qla24xx_intr_handler,
1430         .enable_intrs           = qla24xx_enable_intrs,
1431         .disable_intrs          = qla24xx_disable_intrs,
1432         .abort_command          = qla24xx_abort_command,
1433         .target_reset           = qla24xx_abort_target,
1434         .lun_reset              = qla24xx_lun_reset,
1435         .fabric_login           = qla24xx_login_fabric,
1436         .fabric_logout          = qla24xx_fabric_logout,
1437         .calc_req_entries       = NULL,
1438         .build_iocbs            = NULL,
1439         .prep_ms_iocb           = qla24xx_prep_ms_iocb,
1440         .prep_ms_fdmi_iocb      = qla24xx_prep_ms_fdmi_iocb,
1441         .read_nvram             = qla24xx_read_nvram_data,
1442         .write_nvram            = qla24xx_write_nvram_data,
1443         .fw_dump                = qla24xx_fw_dump,
1444         .beacon_on              = qla24xx_beacon_on,
1445         .beacon_off             = qla24xx_beacon_off,
1446         .beacon_blink           = qla24xx_beacon_blink,
1447         .read_optrom            = qla24xx_read_optrom_data,
1448         .write_optrom           = qla24xx_write_optrom_data,
1449         .get_flash_version      = qla24xx_get_flash_version,
1450         .start_scsi             = qla24xx_start_scsi,
1451 };
1452
1453 static struct isp_operations qla25xx_isp_ops = {
1454         .pci_config             = qla25xx_pci_config,
1455         .reset_chip             = qla24xx_reset_chip,
1456         .chip_diag              = qla24xx_chip_diag,
1457         .config_rings           = qla24xx_config_rings,
1458         .reset_adapter          = qla24xx_reset_adapter,
1459         .nvram_config           = qla24xx_nvram_config,
1460         .update_fw_options      = qla24xx_update_fw_options,
1461         .load_risc              = qla24xx_load_risc,
1462         .pci_info_str           = qla24xx_pci_info_str,
1463         .fw_version_str         = qla24xx_fw_version_str,
1464         .intr_handler           = qla24xx_intr_handler,
1465         .enable_intrs           = qla24xx_enable_intrs,
1466         .disable_intrs          = qla24xx_disable_intrs,
1467         .abort_command          = qla24xx_abort_command,
1468         .target_reset           = qla24xx_abort_target,
1469         .lun_reset              = qla24xx_lun_reset,
1470         .fabric_login           = qla24xx_login_fabric,
1471         .fabric_logout          = qla24xx_fabric_logout,
1472         .calc_req_entries       = NULL,
1473         .build_iocbs            = NULL,
1474         .prep_ms_iocb           = qla24xx_prep_ms_iocb,
1475         .prep_ms_fdmi_iocb      = qla24xx_prep_ms_fdmi_iocb,
1476         .read_nvram             = qla25xx_read_nvram_data,
1477         .write_nvram            = qla25xx_write_nvram_data,
1478         .fw_dump                = qla25xx_fw_dump,
1479         .beacon_on              = qla24xx_beacon_on,
1480         .beacon_off             = qla24xx_beacon_off,
1481         .beacon_blink           = qla24xx_beacon_blink,
1482         .read_optrom            = qla25xx_read_optrom_data,
1483         .write_optrom           = qla24xx_write_optrom_data,
1484         .get_flash_version      = qla24xx_get_flash_version,
1485         .start_scsi             = qla24xx_start_scsi,
1486 };
1487
1488 static struct isp_operations qla81xx_isp_ops = {
1489         .pci_config             = qla25xx_pci_config,
1490         .reset_chip             = qla24xx_reset_chip,
1491         .chip_diag              = qla24xx_chip_diag,
1492         .config_rings           = qla24xx_config_rings,
1493         .reset_adapter          = qla24xx_reset_adapter,
1494         .nvram_config           = qla81xx_nvram_config,
1495         .update_fw_options      = qla81xx_update_fw_options,
1496         .load_risc              = qla81xx_load_risc,
1497         .pci_info_str           = qla24xx_pci_info_str,
1498         .fw_version_str         = qla24xx_fw_version_str,
1499         .intr_handler           = qla24xx_intr_handler,
1500         .enable_intrs           = qla24xx_enable_intrs,
1501         .disable_intrs          = qla24xx_disable_intrs,
1502         .abort_command          = qla24xx_abort_command,
1503         .target_reset           = qla24xx_abort_target,
1504         .lun_reset              = qla24xx_lun_reset,
1505         .fabric_login           = qla24xx_login_fabric,
1506         .fabric_logout          = qla24xx_fabric_logout,
1507         .calc_req_entries       = NULL,
1508         .build_iocbs            = NULL,
1509         .prep_ms_iocb           = qla24xx_prep_ms_iocb,
1510         .prep_ms_fdmi_iocb      = qla24xx_prep_ms_fdmi_iocb,
1511         .read_nvram             = NULL,
1512         .write_nvram            = NULL,
1513         .fw_dump                = qla81xx_fw_dump,
1514         .beacon_on              = qla24xx_beacon_on,
1515         .beacon_off             = qla24xx_beacon_off,
1516         .beacon_blink           = qla24xx_beacon_blink,
1517         .read_optrom            = qla25xx_read_optrom_data,
1518         .write_optrom           = qla24xx_write_optrom_data,
1519         .get_flash_version      = qla24xx_get_flash_version,
1520         .start_scsi             = qla24xx_start_scsi,
1521 };
1522
1523 static inline void
1524 qla2x00_set_isp_flags(struct qla_hw_data *ha)
1525 {
1526         ha->device_type = DT_EXTENDED_IDS;
1527         switch (ha->pdev->device) {
1528         case PCI_DEVICE_ID_QLOGIC_ISP2100:
1529                 ha->device_type |= DT_ISP2100;
1530                 ha->device_type &= ~DT_EXTENDED_IDS;
1531                 ha->fw_srisc_address = RISC_START_ADDRESS_2100;
1532                 break;
1533         case PCI_DEVICE_ID_QLOGIC_ISP2200:
1534                 ha->device_type |= DT_ISP2200;
1535                 ha->device_type &= ~DT_EXTENDED_IDS;
1536                 ha->fw_srisc_address = RISC_START_ADDRESS_2100;
1537                 break;
1538         case PCI_DEVICE_ID_QLOGIC_ISP2300:
1539                 ha->device_type |= DT_ISP2300;
1540                 ha->device_type |= DT_ZIO_SUPPORTED;
1541                 ha->fw_srisc_address = RISC_START_ADDRESS_2300;
1542                 break;
1543         case PCI_DEVICE_ID_QLOGIC_ISP2312:
1544                 ha->device_type |= DT_ISP2312;
1545                 ha->device_type |= DT_ZIO_SUPPORTED;
1546                 ha->fw_srisc_address = RISC_START_ADDRESS_2300;
1547                 break;
1548         case PCI_DEVICE_ID_QLOGIC_ISP2322:
1549                 ha->device_type |= DT_ISP2322;
1550                 ha->device_type |= DT_ZIO_SUPPORTED;
1551                 if (ha->pdev->subsystem_vendor == 0x1028 &&
1552                     ha->pdev->subsystem_device == 0x0170)
1553                         ha->device_type |= DT_OEM_001;
1554                 ha->fw_srisc_address = RISC_START_ADDRESS_2300;
1555                 break;
1556         case PCI_DEVICE_ID_QLOGIC_ISP6312:
1557                 ha->device_type |= DT_ISP6312;
1558                 ha->fw_srisc_address = RISC_START_ADDRESS_2300;
1559                 break;
1560         case PCI_DEVICE_ID_QLOGIC_ISP6322:
1561                 ha->device_type |= DT_ISP6322;
1562                 ha->fw_srisc_address = RISC_START_ADDRESS_2300;
1563                 break;
1564         case PCI_DEVICE_ID_QLOGIC_ISP2422:
1565                 ha->device_type |= DT_ISP2422;
1566                 ha->device_type |= DT_ZIO_SUPPORTED;
1567                 ha->device_type |= DT_FWI2;
1568                 ha->device_type |= DT_IIDMA;
1569                 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
1570                 break;
1571         case PCI_DEVICE_ID_QLOGIC_ISP2432:
1572                 ha->device_type |= DT_ISP2432;
1573                 ha->device_type |= DT_ZIO_SUPPORTED;
1574                 ha->device_type |= DT_FWI2;
1575                 ha->device_type |= DT_IIDMA;
1576                 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
1577                 break;
1578         case PCI_DEVICE_ID_QLOGIC_ISP8432:
1579                 ha->device_type |= DT_ISP8432;
1580                 ha->device_type |= DT_ZIO_SUPPORTED;
1581                 ha->device_type |= DT_FWI2;
1582                 ha->device_type |= DT_IIDMA;
1583                 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
1584                 break;
1585         case PCI_DEVICE_ID_QLOGIC_ISP5422:
1586                 ha->device_type |= DT_ISP5422;
1587                 ha->device_type |= DT_FWI2;
1588                 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
1589                 break;
1590         case PCI_DEVICE_ID_QLOGIC_ISP5432:
1591                 ha->device_type |= DT_ISP5432;
1592                 ha->device_type |= DT_FWI2;
1593                 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
1594                 break;
1595         case PCI_DEVICE_ID_QLOGIC_ISP2532:
1596                 ha->device_type |= DT_ISP2532;
1597                 ha->device_type |= DT_ZIO_SUPPORTED;
1598                 ha->device_type |= DT_FWI2;
1599                 ha->device_type |= DT_IIDMA;
1600                 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
1601                 break;
1602         case PCI_DEVICE_ID_QLOGIC_ISP8001:
1603                 ha->device_type |= DT_ISP8001;
1604                 ha->device_type |= DT_ZIO_SUPPORTED;
1605                 ha->device_type |= DT_FWI2;
1606                 ha->device_type |= DT_IIDMA;
1607                 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
1608                 break;
1609         }
1610
1611         /* Get adapter physical port no from interrupt pin register. */
1612         pci_read_config_byte(ha->pdev, PCI_INTERRUPT_PIN, &ha->port_no);
1613         if (ha->port_no & 1)
1614                 ha->flags.port0 = 1;
1615         else
1616                 ha->flags.port0 = 0;
1617 }
1618
1619 static int
1620 qla2x00_iospace_config(struct qla_hw_data *ha)
1621 {
1622         resource_size_t pio;
1623         uint16_t msix;
1624         int cpus;
1625
1626         if (pci_request_selected_regions(ha->pdev, ha->bars,
1627             QLA2XXX_DRIVER_NAME)) {
1628                 qla_printk(KERN_WARNING, ha,
1629                     "Failed to reserve PIO/MMIO regions (%s)\n",
1630                     pci_name(ha->pdev));
1631
1632                 goto iospace_error_exit;
1633         }
1634         if (!(ha->bars & 1))
1635                 goto skip_pio;
1636
1637         /* We only need PIO for Flash operations on ISP2312 v2 chips. */
1638         pio = pci_resource_start(ha->pdev, 0);
1639         if (pci_resource_flags(ha->pdev, 0) & IORESOURCE_IO) {
1640                 if (pci_resource_len(ha->pdev, 0) < MIN_IOBASE_LEN) {
1641                         qla_printk(KERN_WARNING, ha,
1642                             "Invalid PCI I/O region size (%s)...\n",
1643                                 pci_name(ha->pdev));
1644                         pio = 0;
1645                 }
1646         } else {
1647                 qla_printk(KERN_WARNING, ha,
1648                     "region #0 not a PIO resource (%s)...\n",
1649                     pci_name(ha->pdev));
1650                 pio = 0;
1651         }
1652         ha->pio_address = pio;
1653
1654 skip_pio:
1655         /* Use MMIO operations for all accesses. */
1656         if (!(pci_resource_flags(ha->pdev, 1) & IORESOURCE_MEM)) {
1657                 qla_printk(KERN_ERR, ha,
1658                     "region #1 not an MMIO resource (%s), aborting\n",
1659                     pci_name(ha->pdev));
1660                 goto iospace_error_exit;
1661         }
1662         if (pci_resource_len(ha->pdev, 1) < MIN_IOBASE_LEN) {
1663                 qla_printk(KERN_ERR, ha,
1664                     "Invalid PCI mem region size (%s), aborting\n",
1665                         pci_name(ha->pdev));
1666                 goto iospace_error_exit;
1667         }
1668
1669         ha->iobase = ioremap(pci_resource_start(ha->pdev, 1), MIN_IOBASE_LEN);
1670         if (!ha->iobase) {
1671                 qla_printk(KERN_ERR, ha,
1672                     "cannot remap MMIO (%s), aborting\n", pci_name(ha->pdev));
1673
1674                 goto iospace_error_exit;
1675         }
1676
1677         /* Determine queue resources */
1678         ha->max_req_queues = ha->max_rsp_queues = 1;
1679         if ((ql2xmaxqueues <= 1 || ql2xmultique_tag < 1) &&
1680                 (!IS_QLA25XX(ha) && !IS_QLA81XX(ha)))
1681                 goto mqiobase_exit;
1682         ha->mqiobase = ioremap(pci_resource_start(ha->pdev, 3),
1683                         pci_resource_len(ha->pdev, 3));
1684         if (ha->mqiobase) {
1685                 /* Read MSIX vector size of the board */
1686                 pci_read_config_word(ha->pdev, QLA_PCI_MSIX_CONTROL, &msix);
1687                 ha->msix_count = msix;
1688                 /* Max queues are bounded by available msix vectors */
1689                 /* queue 0 uses two msix vectors */
1690                 if (ql2xmultique_tag) {
1691                         cpus = num_online_cpus();
1692                         ha->max_rsp_queues = (ha->msix_count - 1 > cpus) ?
1693                                 (cpus + 1) : (ha->msix_count - 1);
1694                         ha->max_req_queues = 2;
1695                 } else if (ql2xmaxqueues > 1) {
1696                         ha->max_req_queues = ql2xmaxqueues > QLA_MQ_SIZE ?
1697                                                 QLA_MQ_SIZE : ql2xmaxqueues;
1698                         DEBUG2(qla_printk(KERN_INFO, ha, "QoS mode set, max no"
1699                         " of request queues:%d\n", ha->max_req_queues));
1700                 }
1701                 qla_printk(KERN_INFO, ha,
1702                         "MSI-X vector count: %d\n", msix);
1703         } else
1704                 qla_printk(KERN_INFO, ha, "BAR 3 not enabled\n");
1705
1706 mqiobase_exit:
1707         ha->msix_count = ha->max_rsp_queues + 1;
1708         return (0);
1709
1710 iospace_error_exit:
1711         return (-ENOMEM);
1712 }
1713
1714 static void
1715 qla2xxx_scan_start(struct Scsi_Host *shost)
1716 {
1717         scsi_qla_host_t *vha = shost_priv(shost);
1718
1719         if (vha->hw->flags.running_gold_fw)
1720                 return;
1721
1722         set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
1723         set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
1724         set_bit(RSCN_UPDATE, &vha->dpc_flags);
1725         set_bit(NPIV_CONFIG_NEEDED, &vha->dpc_flags);
1726 }
1727
1728 static int
1729 qla2xxx_scan_finished(struct Scsi_Host *shost, unsigned long time)
1730 {
1731         scsi_qla_host_t *vha = shost_priv(shost);
1732
1733         if (!vha->host)
1734                 return 1;
1735         if (time > vha->hw->loop_reset_delay * HZ)
1736                 return 1;
1737
1738         return atomic_read(&vha->loop_state) == LOOP_READY;
1739 }
1740
1741 /*
1742  * PCI driver interface
1743  */
1744 static int __devinit
1745 qla2x00_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
1746 {
1747         int     ret = -ENODEV;
1748         struct Scsi_Host *host;
1749         scsi_qla_host_t *base_vha = NULL;
1750         struct qla_hw_data *ha;
1751         char pci_info[30];
1752         char fw_str[30];
1753         struct scsi_host_template *sht;
1754         int bars, max_id, mem_only = 0;
1755         uint16_t req_length = 0, rsp_length = 0;
1756         struct req_que *req = NULL;
1757         struct rsp_que *rsp = NULL;
1758
1759         bars = pci_select_bars(pdev, IORESOURCE_MEM | IORESOURCE_IO);
1760         sht = &qla2xxx_driver_template;
1761         if (pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2422 ||
1762             pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2432 ||
1763             pdev->device == PCI_DEVICE_ID_QLOGIC_ISP8432 ||
1764             pdev->device == PCI_DEVICE_ID_QLOGIC_ISP5422 ||
1765             pdev->device == PCI_DEVICE_ID_QLOGIC_ISP5432 ||
1766             pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2532 ||
1767             pdev->device == PCI_DEVICE_ID_QLOGIC_ISP8001) {
1768                 bars = pci_select_bars(pdev, IORESOURCE_MEM);
1769                 mem_only = 1;
1770         }
1771
1772         if (mem_only) {
1773                 if (pci_enable_device_mem(pdev))
1774                         goto probe_out;
1775         } else {
1776                 if (pci_enable_device(pdev))
1777                         goto probe_out;
1778         }
1779
1780         /* This may fail but that's ok */
1781         pci_enable_pcie_error_reporting(pdev);
1782
1783         ha = kzalloc(sizeof(struct qla_hw_data), GFP_KERNEL);
1784         if (!ha) {
1785                 DEBUG(printk("Unable to allocate memory for ha\n"));
1786                 goto probe_out;
1787         }
1788         ha->pdev = pdev;
1789
1790         /* Clear our data area */
1791         ha->bars = bars;
1792         ha->mem_only = mem_only;
1793         spin_lock_init(&ha->hardware_lock);
1794
1795         /* Set ISP-type information. */
1796         qla2x00_set_isp_flags(ha);
1797         /* Configure PCI I/O space */
1798         ret = qla2x00_iospace_config(ha);
1799         if (ret)
1800                 goto probe_hw_failed;
1801
1802         qla_printk(KERN_INFO, ha,
1803             "Found an ISP%04X, irq %d, iobase 0x%p\n", pdev->device, pdev->irq,
1804             ha->iobase);
1805
1806         ha->prev_topology = 0;
1807         ha->init_cb_size = sizeof(init_cb_t);
1808         ha->link_data_rate = PORT_SPEED_UNKNOWN;
1809         ha->optrom_size = OPTROM_SIZE_2300;
1810
1811         /* Assign ISP specific operations. */
1812         max_id = MAX_TARGETS_2200;
1813         if (IS_QLA2100(ha)) {
1814                 max_id = MAX_TARGETS_2100;
1815                 ha->mbx_count = MAILBOX_REGISTER_COUNT_2100;
1816                 req_length = REQUEST_ENTRY_CNT_2100;
1817                 rsp_length = RESPONSE_ENTRY_CNT_2100;
1818                 ha->max_loop_id = SNS_LAST_LOOP_ID_2100;
1819                 ha->gid_list_info_size = 4;
1820                 ha->flash_conf_off = ~0;
1821                 ha->flash_data_off = ~0;
1822                 ha->nvram_conf_off = ~0;
1823                 ha->nvram_data_off = ~0;
1824                 ha->isp_ops = &qla2100_isp_ops;
1825         } else if (IS_QLA2200(ha)) {
1826                 ha->mbx_count = MAILBOX_REGISTER_COUNT;
1827                 req_length = REQUEST_ENTRY_CNT_2200;
1828                 rsp_length = RESPONSE_ENTRY_CNT_2100;
1829                 ha->max_loop_id = SNS_LAST_LOOP_ID_2100;
1830                 ha->gid_list_info_size = 4;
1831                 ha->flash_conf_off = ~0;
1832                 ha->flash_data_off = ~0;
1833                 ha->nvram_conf_off = ~0;
1834                 ha->nvram_data_off = ~0;
1835                 ha->isp_ops = &qla2100_isp_ops;
1836         } else if (IS_QLA23XX(ha)) {
1837                 ha->mbx_count = MAILBOX_REGISTER_COUNT;
1838                 req_length = REQUEST_ENTRY_CNT_2200;
1839                 rsp_length = RESPONSE_ENTRY_CNT_2300;
1840                 ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
1841                 ha->gid_list_info_size = 6;
1842                 if (IS_QLA2322(ha) || IS_QLA6322(ha))
1843                         ha->optrom_size = OPTROM_SIZE_2322;
1844                 ha->flash_conf_off = ~0;
1845                 ha->flash_data_off = ~0;
1846                 ha->nvram_conf_off = ~0;
1847                 ha->nvram_data_off = ~0;
1848                 ha->isp_ops = &qla2300_isp_ops;
1849         } else if (IS_QLA24XX_TYPE(ha)) {
1850                 ha->mbx_count = MAILBOX_REGISTER_COUNT;
1851                 req_length = REQUEST_ENTRY_CNT_24XX;
1852                 rsp_length = RESPONSE_ENTRY_CNT_2300;
1853                 ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
1854                 ha->init_cb_size = sizeof(struct mid_init_cb_24xx);
1855                 ha->gid_list_info_size = 8;
1856                 ha->optrom_size = OPTROM_SIZE_24XX;
1857                 ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA24XX;
1858                 ha->isp_ops = &qla24xx_isp_ops;
1859                 ha->flash_conf_off = FARX_ACCESS_FLASH_CONF;
1860                 ha->flash_data_off = FARX_ACCESS_FLASH_DATA;
1861                 ha->nvram_conf_off = FARX_ACCESS_NVRAM_CONF;
1862                 ha->nvram_data_off = FARX_ACCESS_NVRAM_DATA;
1863         } else if (IS_QLA25XX(ha)) {
1864                 ha->mbx_count = MAILBOX_REGISTER_COUNT;
1865                 req_length = REQUEST_ENTRY_CNT_24XX;
1866                 rsp_length = RESPONSE_ENTRY_CNT_2300;
1867                 ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
1868                 ha->init_cb_size = sizeof(struct mid_init_cb_24xx);
1869                 ha->gid_list_info_size = 8;
1870                 ha->optrom_size = OPTROM_SIZE_25XX;
1871                 ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA25XX;
1872                 ha->isp_ops = &qla25xx_isp_ops;
1873                 ha->flash_conf_off = FARX_ACCESS_FLASH_CONF;
1874                 ha->flash_data_off = FARX_ACCESS_FLASH_DATA;
1875                 ha->nvram_conf_off = FARX_ACCESS_NVRAM_CONF;
1876                 ha->nvram_data_off = FARX_ACCESS_NVRAM_DATA;
1877         } else if (IS_QLA81XX(ha)) {
1878                 ha->mbx_count = MAILBOX_REGISTER_COUNT;
1879                 req_length = REQUEST_ENTRY_CNT_24XX;
1880                 rsp_length = RESPONSE_ENTRY_CNT_2300;
1881                 ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
1882                 ha->init_cb_size = sizeof(struct mid_init_cb_81xx);
1883                 ha->gid_list_info_size = 8;
1884                 ha->optrom_size = OPTROM_SIZE_81XX;
1885                 ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA25XX;
1886                 ha->isp_ops = &qla81xx_isp_ops;
1887                 ha->flash_conf_off = FARX_ACCESS_FLASH_CONF_81XX;
1888                 ha->flash_data_off = FARX_ACCESS_FLASH_DATA_81XX;
1889                 ha->nvram_conf_off = ~0;
1890                 ha->nvram_data_off = ~0;
1891         }
1892
1893         mutex_init(&ha->vport_lock);
1894         init_completion(&ha->mbx_cmd_comp);
1895         complete(&ha->mbx_cmd_comp);
1896         init_completion(&ha->mbx_intr_comp);
1897
1898         set_bit(0, (unsigned long *) ha->vp_idx_map);
1899
1900         qla2x00_config_dma_addressing(ha);
1901         ret = qla2x00_mem_alloc(ha, req_length, rsp_length, &req, &rsp);
1902         if (!ret) {
1903                 qla_printk(KERN_WARNING, ha,
1904                     "[ERROR] Failed to allocate memory for adapter\n");
1905
1906                 goto probe_hw_failed;
1907         }
1908
1909         req->max_q_depth = MAX_Q_DEPTH;
1910         if (ql2xmaxqdepth != 0 && ql2xmaxqdepth <= 0xffffU)
1911                 req->max_q_depth = ql2xmaxqdepth;
1912
1913
1914         base_vha = qla2x00_create_host(sht, ha);
1915         if (!base_vha) {
1916                 qla_printk(KERN_WARNING, ha,
1917                     "[ERROR] Failed to allocate memory for scsi_host\n");
1918
1919                 ret = -ENOMEM;
1920                 qla2x00_mem_free(ha);
1921                 qla2x00_free_req_que(ha, req);
1922                 qla2x00_free_rsp_que(ha, rsp);
1923                 goto probe_hw_failed;
1924         }
1925
1926         pci_set_drvdata(pdev, base_vha);
1927
1928         host = base_vha->host;
1929         base_vha->req = req;
1930         host->can_queue = req->length + 128;
1931         if (IS_QLA2XXX_MIDTYPE(ha))
1932                 base_vha->mgmt_svr_loop_id = 10 + base_vha->vp_idx;
1933         else
1934                 base_vha->mgmt_svr_loop_id = MANAGEMENT_SERVER +
1935                                                 base_vha->vp_idx;
1936         if (IS_QLA2100(ha))
1937                 host->sg_tablesize = 32;
1938         host->max_id = max_id;
1939         host->this_id = 255;
1940         host->cmd_per_lun = 3;
1941         host->unique_id = host->host_no;
1942         host->max_cmd_len = MAX_CMDSZ;
1943         host->max_channel = MAX_BUSES - 1;
1944         host->max_lun = MAX_LUNS;
1945         host->transportt = qla2xxx_transport_template;
1946
1947         /* Set up the irqs */
1948         ret = qla2x00_request_irqs(ha, rsp);
1949         if (ret)
1950                 goto probe_init_failed;
1951         /* Alloc arrays of request and response ring ptrs */
1952 que_init:
1953         if (!qla2x00_alloc_queues(ha)) {
1954                 qla_printk(KERN_WARNING, ha,
1955                 "[ERROR] Failed to allocate memory for queue"
1956                 " pointers\n");
1957                 goto probe_init_failed;
1958         }
1959         ha->rsp_q_map[0] = rsp;
1960         ha->req_q_map[0] = req;
1961         rsp->req = req;
1962         req->rsp = rsp;
1963         set_bit(0, ha->req_qid_map);
1964         set_bit(0, ha->rsp_qid_map);
1965         /* FWI2-capable only. */
1966         req->req_q_in = &ha->iobase->isp24.req_q_in;
1967         req->req_q_out = &ha->iobase->isp24.req_q_out;
1968         rsp->rsp_q_in = &ha->iobase->isp24.rsp_q_in;
1969         rsp->rsp_q_out = &ha->iobase->isp24.rsp_q_out;
1970         if (ha->mqenable) {
1971                 req->req_q_in = &ha->mqiobase->isp25mq.req_q_in;
1972                 req->req_q_out = &ha->mqiobase->isp25mq.req_q_out;
1973                 rsp->rsp_q_in = &ha->mqiobase->isp25mq.rsp_q_in;
1974                 rsp->rsp_q_out =  &ha->mqiobase->isp25mq.rsp_q_out;
1975         }
1976
1977         if (qla2x00_initialize_adapter(base_vha)) {
1978                 qla_printk(KERN_WARNING, ha,
1979                     "Failed to initialize adapter\n");
1980
1981                 DEBUG2(printk("scsi(%ld): Failed to initialize adapter - "
1982                     "Adapter flags %x.\n",
1983                     base_vha->host_no, base_vha->device_flags));
1984
1985                 ret = -ENODEV;
1986                 goto probe_failed;
1987         }
1988
1989         if (ha->mqenable) {
1990                 if (qla25xx_setup_mode(base_vha)) {
1991                         qla_printk(KERN_WARNING, ha,
1992                                 "Can't create queues, falling back to single"
1993                                 " queue mode\n");
1994                         goto que_init;
1995                 }
1996         }
1997
1998         if (ha->flags.running_gold_fw)
1999                 goto skip_dpc;
2000
2001         /*
2002          * Startup the kernel thread for this host adapter
2003          */
2004         ha->dpc_thread = kthread_create(qla2x00_do_dpc, ha,
2005                         "%s_dpc", base_vha->host_str);
2006         if (IS_ERR(ha->dpc_thread)) {
2007                 qla_printk(KERN_WARNING, ha,
2008                     "Unable to start DPC thread!\n");
2009                 ret = PTR_ERR(ha->dpc_thread);
2010                 goto probe_failed;
2011         }
2012
2013 skip_dpc:
2014         list_add_tail(&base_vha->list, &ha->vp_list);
2015         base_vha->host->irq = ha->pdev->irq;
2016
2017         /* Initialized the timer */
2018         qla2x00_start_timer(base_vha, qla2x00_timer, WATCH_INTERVAL);
2019
2020         DEBUG2(printk("DEBUG: detect hba %ld at address = %p\n",
2021             base_vha->host_no, ha));
2022
2023         base_vha->flags.init_done = 1;
2024         base_vha->flags.online = 1;
2025
2026         ret = scsi_add_host(host, &pdev->dev);
2027         if (ret)
2028                 goto probe_failed;
2029
2030         ha->isp_ops->enable_intrs(ha);
2031
2032         scsi_scan_host(host);
2033
2034         qla2x00_alloc_sysfs_attr(base_vha);
2035
2036         qla2x00_init_host_attr(base_vha);
2037
2038         qla2x00_dfs_setup(base_vha);
2039
2040         qla_printk(KERN_INFO, ha, "\n"
2041             " QLogic Fibre Channel HBA Driver: %s\n"
2042             "  QLogic %s - %s\n"
2043             "  ISP%04X: %s @ %s hdma%c, host#=%ld, fw=%s\n",
2044             qla2x00_version_str, ha->model_number,
2045             ha->model_desc ? ha->model_desc : "", pdev->device,
2046             ha->isp_ops->pci_info_str(base_vha, pci_info), pci_name(pdev),
2047             ha->flags.enable_64bit_addressing ? '+' : '-', base_vha->host_no,
2048             ha->isp_ops->fw_version_str(base_vha, fw_str));
2049
2050         return 0;
2051
2052 probe_init_failed:
2053         qla2x00_free_req_que(ha, req);
2054         qla2x00_free_rsp_que(ha, rsp);
2055         ha->max_req_queues = ha->max_rsp_queues = 0;
2056
2057 probe_failed:
2058         if (base_vha->timer_active)
2059                 qla2x00_stop_timer(base_vha);
2060         base_vha->flags.online = 0;
2061         if (ha->dpc_thread) {
2062                 struct task_struct *t = ha->dpc_thread;
2063
2064                 ha->dpc_thread = NULL;
2065                 kthread_stop(t);
2066         }
2067
2068         qla2x00_free_device(base_vha);
2069
2070         scsi_host_put(base_vha->host);
2071
2072 probe_hw_failed:
2073         if (ha->iobase)
2074                 iounmap(ha->iobase);
2075
2076         pci_release_selected_regions(ha->pdev, ha->bars);
2077         kfree(ha);
2078         ha = NULL;
2079
2080 probe_out:
2081         pci_disable_device(pdev);
2082         return ret;
2083 }
2084
2085 static void
2086 qla2x00_remove_one(struct pci_dev *pdev)
2087 {
2088         scsi_qla_host_t *base_vha, *vha, *temp;
2089         struct qla_hw_data  *ha;
2090
2091         base_vha = pci_get_drvdata(pdev);
2092         ha = base_vha->hw;
2093
2094         list_for_each_entry_safe(vha, temp, &ha->vp_list, list) {
2095                 if (vha && vha->fc_vport)
2096                         fc_vport_terminate(vha->fc_vport);
2097         }
2098
2099         set_bit(UNLOADING, &base_vha->dpc_flags);
2100
2101         qla2x00_abort_all_cmds(base_vha, DID_NO_CONNECT << 16);
2102
2103         qla2x00_dfs_remove(base_vha);
2104
2105         qla84xx_put_chip(base_vha);
2106
2107         /* Disable timer */
2108         if (base_vha->timer_active)
2109                 qla2x00_stop_timer(base_vha);
2110
2111         base_vha->flags.online = 0;
2112
2113         /* Flush the work queue and remove it */
2114         if (ha->wq) {
2115                 flush_workqueue(ha->wq);
2116                 destroy_workqueue(ha->wq);
2117                 ha->wq = NULL;
2118         }
2119
2120         /* Kill the kernel thread for this host */
2121         if (ha->dpc_thread) {
2122                 struct task_struct *t = ha->dpc_thread;
2123
2124                 /*
2125                  * qla2xxx_wake_dpc checks for ->dpc_thread
2126                  * so we need to zero it out.
2127                  */
2128                 ha->dpc_thread = NULL;
2129                 kthread_stop(t);
2130         }
2131
2132         qla2x00_free_sysfs_attr(base_vha);
2133
2134         fc_remove_host(base_vha->host);
2135
2136         scsi_remove_host(base_vha->host);
2137
2138         qla2x00_free_device(base_vha);
2139
2140         scsi_host_put(base_vha->host);
2141
2142         if (ha->iobase)
2143                 iounmap(ha->iobase);
2144
2145         if (ha->mqiobase)
2146                 iounmap(ha->mqiobase);
2147
2148         pci_release_selected_regions(ha->pdev, ha->bars);
2149         kfree(ha);
2150         ha = NULL;
2151
2152         pci_disable_device(pdev);
2153         pci_set_drvdata(pdev, NULL);
2154 }
2155
2156 static void
2157 qla2x00_free_device(scsi_qla_host_t *vha)
2158 {
2159         struct qla_hw_data *ha = vha->hw;
2160
2161         qla25xx_delete_queues(vha);
2162
2163         if (ha->flags.fce_enabled)
2164                 qla2x00_disable_fce_trace(vha, NULL, NULL);
2165
2166         if (ha->eft)
2167                 qla2x00_disable_eft_trace(vha);
2168
2169         /* Stop currently executing firmware. */
2170         qla2x00_try_to_stop_firmware(vha);
2171
2172         /* turn-off interrupts on the card */
2173         if (ha->interrupts_on)
2174                 ha->isp_ops->disable_intrs(ha);
2175
2176         qla2x00_free_irqs(vha);
2177
2178         qla2x00_mem_free(ha);
2179
2180         qla2x00_free_queues(ha);
2181 }
2182
2183 static inline void
2184 qla2x00_schedule_rport_del(struct scsi_qla_host *vha, fc_port_t *fcport,
2185     int defer)
2186 {
2187         struct fc_rport *rport;
2188         scsi_qla_host_t *base_vha;
2189
2190         if (!fcport->rport)
2191                 return;
2192
2193         rport = fcport->rport;
2194         if (defer) {
2195                 base_vha = pci_get_drvdata(vha->hw->pdev);
2196                 spin_lock_irq(vha->host->host_lock);
2197                 fcport->drport = rport;
2198                 spin_unlock_irq(vha->host->host_lock);
2199                 set_bit(FCPORT_UPDATE_NEEDED, &base_vha->dpc_flags);
2200                 qla2xxx_wake_dpc(base_vha);
2201         } else
2202                 fc_remote_port_delete(rport);
2203 }
2204
2205 /*
2206  * qla2x00_mark_device_lost Updates fcport state when device goes offline.
2207  *
2208  * Input: ha = adapter block pointer.  fcport = port structure pointer.
2209  *
2210  * Return: None.
2211  *
2212  * Context:
2213  */
2214 void qla2x00_mark_device_lost(scsi_qla_host_t *vha, fc_port_t *fcport,
2215     int do_login, int defer)
2216 {
2217         if (atomic_read(&fcport->state) == FCS_ONLINE &&
2218             vha->vp_idx == fcport->vp_idx) {
2219                 atomic_set(&fcport->state, FCS_DEVICE_LOST);
2220                 qla2x00_schedule_rport_del(vha, fcport, defer);
2221         }
2222         /*
2223          * We may need to retry the login, so don't change the state of the
2224          * port but do the retries.
2225          */
2226         if (atomic_read(&fcport->state) != FCS_DEVICE_DEAD)
2227                 atomic_set(&fcport->state, FCS_DEVICE_LOST);
2228
2229         if (!do_login)
2230                 return;
2231
2232         if (fcport->login_retry == 0) {
2233                 fcport->login_retry = vha->hw->login_retry_count;
2234                 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
2235
2236                 DEBUG(printk("scsi(%ld): Port login retry: "
2237                     "%02x%02x%02x%02x%02x%02x%02x%02x, "
2238                     "id = 0x%04x retry cnt=%d\n",
2239                     vha->host_no,
2240                     fcport->port_name[0],
2241                     fcport->port_name[1],
2242                     fcport->port_name[2],
2243                     fcport->port_name[3],
2244                     fcport->port_name[4],
2245                     fcport->port_name[5],
2246                     fcport->port_name[6],
2247                     fcport->port_name[7],
2248                     fcport->loop_id,
2249                     fcport->login_retry));
2250         }
2251 }
2252
2253 /*
2254  * qla2x00_mark_all_devices_lost
2255  *      Updates fcport state when device goes offline.
2256  *
2257  * Input:
2258  *      ha = adapter block pointer.
2259  *      fcport = port structure pointer.
2260  *
2261  * Return:
2262  *      None.
2263  *
2264  * Context:
2265  */
2266 void
2267 qla2x00_mark_all_devices_lost(scsi_qla_host_t *vha, int defer)
2268 {
2269         fc_port_t *fcport;
2270
2271         list_for_each_entry(fcport, &vha->vp_fcports, list) {
2272                 if (vha->vp_idx != 0 && vha->vp_idx != fcport->vp_idx)
2273                         continue;
2274
2275                 /*
2276                  * No point in marking the device as lost, if the device is
2277                  * already DEAD.
2278                  */
2279                 if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD)
2280                         continue;
2281                 if (atomic_read(&fcport->state) == FCS_ONLINE) {
2282                         if (defer)
2283                                 qla2x00_schedule_rport_del(vha, fcport, defer);
2284                         else if (vha->vp_idx == fcport->vp_idx)
2285                                 qla2x00_schedule_rport_del(vha, fcport, defer);
2286                 }
2287                 atomic_set(&fcport->state, FCS_DEVICE_LOST);
2288         }
2289 }
2290
2291 /*
2292 * qla2x00_mem_alloc
2293 *      Allocates adapter memory.
2294 *
2295 * Returns:
2296 *      0  = success.
2297 *      !0  = failure.
2298 */
2299 static int
2300 qla2x00_mem_alloc(struct qla_hw_data *ha, uint16_t req_len, uint16_t rsp_len,
2301         struct req_que **req, struct rsp_que **rsp)
2302 {
2303         char    name[16];
2304
2305         ha->init_cb = dma_alloc_coherent(&ha->pdev->dev, ha->init_cb_size,
2306                 &ha->init_cb_dma, GFP_KERNEL);
2307         if (!ha->init_cb)
2308                 goto fail;
2309
2310         ha->gid_list = dma_alloc_coherent(&ha->pdev->dev, GID_LIST_SIZE,
2311                 &ha->gid_list_dma, GFP_KERNEL);
2312         if (!ha->gid_list)
2313                 goto fail_free_init_cb;
2314
2315         ha->srb_mempool = mempool_create_slab_pool(SRB_MIN_REQ, srb_cachep);
2316         if (!ha->srb_mempool)
2317                 goto fail_free_gid_list;
2318
2319         /* Get memory for cached NVRAM */
2320         ha->nvram = kzalloc(MAX_NVRAM_SIZE, GFP_KERNEL);
2321         if (!ha->nvram)
2322                 goto fail_free_srb_mempool;
2323
2324         snprintf(name, sizeof(name), "%s_%d", QLA2XXX_DRIVER_NAME,
2325                 ha->pdev->device);
2326         ha->s_dma_pool = dma_pool_create(name, &ha->pdev->dev,
2327                 DMA_POOL_SIZE, 8, 0);
2328         if (!ha->s_dma_pool)
2329                 goto fail_free_nvram;
2330
2331         /* Allocate memory for SNS commands */
2332         if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
2333         /* Get consistent memory allocated for SNS commands */
2334                 ha->sns_cmd = dma_alloc_coherent(&ha->pdev->dev,
2335                 sizeof(struct sns_cmd_pkt), &ha->sns_cmd_dma, GFP_KERNEL);
2336                 if (!ha->sns_cmd)
2337                         goto fail_dma_pool;
2338         } else {
2339         /* Get consistent memory allocated for MS IOCB */
2340                 ha->ms_iocb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
2341                         &ha->ms_iocb_dma);
2342                 if (!ha->ms_iocb)
2343                         goto fail_dma_pool;
2344         /* Get consistent memory allocated for CT SNS commands */
2345                 ha->ct_sns = dma_alloc_coherent(&ha->pdev->dev,
2346                         sizeof(struct ct_sns_pkt), &ha->ct_sns_dma, GFP_KERNEL);
2347                 if (!ha->ct_sns)
2348                         goto fail_free_ms_iocb;
2349         }
2350
2351         /* Allocate memory for request ring */
2352         *req = kzalloc(sizeof(struct req_que), GFP_KERNEL);
2353         if (!*req) {
2354                 DEBUG(printk("Unable to allocate memory for req\n"));
2355                 goto fail_req;
2356         }
2357         (*req)->length = req_len;
2358         (*req)->ring = dma_alloc_coherent(&ha->pdev->dev,
2359                 ((*req)->length + 1) * sizeof(request_t),
2360                 &(*req)->dma, GFP_KERNEL);
2361         if (!(*req)->ring) {
2362                 DEBUG(printk("Unable to allocate memory for req_ring\n"));
2363                 goto fail_req_ring;
2364         }
2365         /* Allocate memory for response ring */
2366         *rsp = kzalloc(sizeof(struct rsp_que), GFP_KERNEL);
2367         if (!*rsp) {
2368                 qla_printk(KERN_WARNING, ha,
2369                         "Unable to allocate memory for rsp\n");
2370                 goto fail_rsp;
2371         }
2372         (*rsp)->hw = ha;
2373         (*rsp)->length = rsp_len;
2374         (*rsp)->ring = dma_alloc_coherent(&ha->pdev->dev,
2375                 ((*rsp)->length + 1) * sizeof(response_t),
2376                 &(*rsp)->dma, GFP_KERNEL);
2377         if (!(*rsp)->ring) {
2378                 qla_printk(KERN_WARNING, ha,
2379                         "Unable to allocate memory for rsp_ring\n");
2380                 goto fail_rsp_ring;
2381         }
2382         (*req)->rsp = *rsp;
2383         (*rsp)->req = *req;
2384         /* Allocate memory for NVRAM data for vports */
2385         if (ha->nvram_npiv_size) {
2386                 ha->npiv_info = kzalloc(sizeof(struct qla_npiv_entry) *
2387                                         ha->nvram_npiv_size, GFP_KERNEL);
2388                 if (!ha->npiv_info) {
2389                         qla_printk(KERN_WARNING, ha,
2390                                 "Unable to allocate memory for npiv info\n");
2391                         goto fail_npiv_info;
2392                 }
2393         } else
2394                 ha->npiv_info = NULL;
2395
2396         /* Get consistent memory allocated for EX-INIT-CB. */
2397         if (IS_QLA81XX(ha)) {
2398                 ha->ex_init_cb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
2399                     &ha->ex_init_cb_dma);
2400                 if (!ha->ex_init_cb)
2401                         goto fail_ex_init_cb;
2402         }
2403
2404         INIT_LIST_HEAD(&ha->vp_list);
2405         return 1;
2406
2407 fail_ex_init_cb:
2408         kfree(ha->npiv_info);
2409 fail_npiv_info:
2410         dma_free_coherent(&ha->pdev->dev, ((*rsp)->length + 1) *
2411                 sizeof(response_t), (*rsp)->ring, (*rsp)->dma);
2412         (*rsp)->ring = NULL;
2413         (*rsp)->dma = 0;
2414 fail_rsp_ring:
2415         kfree(*rsp);
2416 fail_rsp:
2417         dma_free_coherent(&ha->pdev->dev, ((*req)->length + 1) *
2418                 sizeof(request_t), (*req)->ring, (*req)->dma);
2419         (*req)->ring = NULL;
2420         (*req)->dma = 0;
2421 fail_req_ring:
2422         kfree(*req);
2423 fail_req:
2424         dma_free_coherent(&ha->pdev->dev, sizeof(struct ct_sns_pkt),
2425                 ha->ct_sns, ha->ct_sns_dma);
2426         ha->ct_sns = NULL;
2427         ha->ct_sns_dma = 0;
2428 fail_free_ms_iocb:
2429         dma_pool_free(ha->s_dma_pool, ha->ms_iocb, ha->ms_iocb_dma);
2430         ha->ms_iocb = NULL;
2431         ha->ms_iocb_dma = 0;
2432 fail_dma_pool:
2433         dma_pool_destroy(ha->s_dma_pool);
2434         ha->s_dma_pool = NULL;
2435 fail_free_nvram:
2436         kfree(ha->nvram);
2437         ha->nvram = NULL;
2438 fail_free_srb_mempool:
2439         mempool_destroy(ha->srb_mempool);
2440         ha->srb_mempool = NULL;
2441 fail_free_gid_list:
2442         dma_free_coherent(&ha->pdev->dev, GID_LIST_SIZE, ha->gid_list,
2443         ha->gid_list_dma);
2444         ha->gid_list = NULL;
2445         ha->gid_list_dma = 0;
2446 fail_free_init_cb:
2447         dma_free_coherent(&ha->pdev->dev, ha->init_cb_size, ha->init_cb,
2448         ha->init_cb_dma);
2449         ha->init_cb = NULL;
2450         ha->init_cb_dma = 0;
2451 fail:
2452         DEBUG(printk("%s: Memory allocation failure\n", __func__));
2453         return -ENOMEM;
2454 }
2455
2456 /*
2457 * qla2x00_mem_free
2458 *      Frees all adapter allocated memory.
2459 *
2460 * Input:
2461 *      ha = adapter block pointer.
2462 */
2463 static void
2464 qla2x00_mem_free(struct qla_hw_data *ha)
2465 {
2466         if (ha->srb_mempool)
2467                 mempool_destroy(ha->srb_mempool);
2468
2469         if (ha->fce)
2470                 dma_free_coherent(&ha->pdev->dev, FCE_SIZE, ha->fce,
2471                 ha->fce_dma);
2472
2473         if (ha->fw_dump) {
2474                 if (ha->eft)
2475                         dma_free_coherent(&ha->pdev->dev,
2476                         ntohl(ha->fw_dump->eft_size), ha->eft, ha->eft_dma);
2477                 vfree(ha->fw_dump);
2478         }
2479
2480         if (ha->dcbx_tlv)
2481                 dma_free_coherent(&ha->pdev->dev, DCBX_TLV_DATA_SIZE,
2482                     ha->dcbx_tlv, ha->dcbx_tlv_dma);
2483
2484         if (ha->xgmac_data)
2485                 dma_free_coherent(&ha->pdev->dev, XGMAC_DATA_SIZE,
2486                     ha->xgmac_data, ha->xgmac_data_dma);
2487
2488         if (ha->sns_cmd)
2489                 dma_free_coherent(&ha->pdev->dev, sizeof(struct sns_cmd_pkt),
2490                 ha->sns_cmd, ha->sns_cmd_dma);
2491
2492         if (ha->ct_sns)
2493                 dma_free_coherent(&ha->pdev->dev, sizeof(struct ct_sns_pkt),
2494                 ha->ct_sns, ha->ct_sns_dma);
2495
2496         if (ha->sfp_data)
2497                 dma_pool_free(ha->s_dma_pool, ha->sfp_data, ha->sfp_data_dma);
2498
2499         if (ha->edc_data)
2500                 dma_pool_free(ha->s_dma_pool, ha->edc_data, ha->edc_data_dma);
2501
2502         if (ha->ms_iocb)
2503                 dma_pool_free(ha->s_dma_pool, ha->ms_iocb, ha->ms_iocb_dma);
2504
2505         if (ha->ex_init_cb)
2506                 dma_pool_free(ha->s_dma_pool, ha->ex_init_cb, ha->ex_init_cb_dma);
2507
2508         if (ha->s_dma_pool)
2509                 dma_pool_destroy(ha->s_dma_pool);
2510
2511         if (ha->gid_list)
2512                 dma_free_coherent(&ha->pdev->dev, GID_LIST_SIZE, ha->gid_list,
2513                 ha->gid_list_dma);
2514
2515         if (ha->init_cb)
2516                 dma_free_coherent(&ha->pdev->dev, ha->init_cb_size,
2517                 ha->init_cb, ha->init_cb_dma);
2518         vfree(ha->optrom_buffer);
2519         kfree(ha->nvram);
2520         kfree(ha->npiv_info);
2521
2522         ha->srb_mempool = NULL;
2523         ha->eft = NULL;
2524         ha->eft_dma = 0;
2525         ha->sns_cmd = NULL;
2526         ha->sns_cmd_dma = 0;
2527         ha->ct_sns = NULL;
2528         ha->ct_sns_dma = 0;
2529         ha->ms_iocb = NULL;
2530         ha->ms_iocb_dma = 0;
2531         ha->init_cb = NULL;
2532         ha->init_cb_dma = 0;
2533         ha->ex_init_cb = NULL;
2534         ha->ex_init_cb_dma = 0;
2535
2536         ha->s_dma_pool = NULL;
2537
2538         ha->gid_list = NULL;
2539         ha->gid_list_dma = 0;
2540
2541         ha->fw_dump = NULL;
2542         ha->fw_dumped = 0;
2543         ha->fw_dump_reading = 0;
2544 }
2545
2546 struct scsi_qla_host *qla2x00_create_host(struct scsi_host_template *sht,
2547                                                 struct qla_hw_data *ha)
2548 {
2549         struct Scsi_Host *host;
2550         struct scsi_qla_host *vha = NULL;
2551
2552         host = scsi_host_alloc(sht, sizeof(scsi_qla_host_t));
2553         if (host == NULL) {
2554                 printk(KERN_WARNING
2555                 "qla2xxx: Couldn't allocate host from scsi layer!\n");
2556                 goto fail;
2557         }
2558
2559         /* Clear our data area */
2560         vha = shost_priv(host);
2561         memset(vha, 0, sizeof(scsi_qla_host_t));
2562
2563         vha->host = host;
2564         vha->host_no = host->host_no;
2565         vha->hw = ha;
2566
2567         INIT_LIST_HEAD(&vha->vp_fcports);
2568         INIT_LIST_HEAD(&vha->work_list);
2569         INIT_LIST_HEAD(&vha->list);
2570
2571         spin_lock_init(&vha->work_lock);
2572
2573         sprintf(vha->host_str, "%s_%ld", QLA2XXX_DRIVER_NAME, vha->host_no);
2574         return vha;
2575
2576 fail:
2577         return vha;
2578 }
2579
2580 static struct qla_work_evt *
2581 qla2x00_alloc_work(struct scsi_qla_host *vha, enum qla_work_type type)
2582 {
2583         struct qla_work_evt *e;
2584
2585         e = kzalloc(sizeof(struct qla_work_evt), GFP_ATOMIC);
2586         if (!e)
2587                 return NULL;
2588
2589         INIT_LIST_HEAD(&e->list);
2590         e->type = type;
2591         e->flags = QLA_EVT_FLAG_FREE;
2592         return e;
2593 }
2594
2595 static int
2596 qla2x00_post_work(struct scsi_qla_host *vha, struct qla_work_evt *e)
2597 {
2598         unsigned long flags;
2599
2600         spin_lock_irqsave(&vha->work_lock, flags);
2601         list_add_tail(&e->list, &vha->work_list);
2602         spin_unlock_irqrestore(&vha->work_lock, flags);
2603         qla2xxx_wake_dpc(vha);
2604
2605         return QLA_SUCCESS;
2606 }
2607
2608 int
2609 qla2x00_post_aen_work(struct scsi_qla_host *vha, enum fc_host_event_code code,
2610     u32 data)
2611 {
2612         struct qla_work_evt *e;
2613
2614         e = qla2x00_alloc_work(vha, QLA_EVT_AEN);
2615         if (!e)
2616                 return QLA_FUNCTION_FAILED;
2617
2618         e->u.aen.code = code;
2619         e->u.aen.data = data;
2620         return qla2x00_post_work(vha, e);
2621 }
2622
2623 int
2624 qla2x00_post_idc_ack_work(struct scsi_qla_host *vha, uint16_t *mb)
2625 {
2626         struct qla_work_evt *e;
2627
2628         e = qla2x00_alloc_work(vha, QLA_EVT_IDC_ACK);
2629         if (!e)
2630                 return QLA_FUNCTION_FAILED;
2631
2632         memcpy(e->u.idc_ack.mb, mb, QLA_IDC_ACK_REGS * sizeof(uint16_t));
2633         return qla2x00_post_work(vha, e);
2634 }
2635
2636 #define qla2x00_post_async_work(name, type)     \
2637 int qla2x00_post_async_##name##_work(           \
2638     struct scsi_qla_host *vha,                  \
2639     fc_port_t *fcport, uint16_t *data)          \
2640 {                                               \
2641         struct qla_work_evt *e;                 \
2642                                                 \
2643         e = qla2x00_alloc_work(vha, type);      \
2644         if (!e)                                 \
2645                 return QLA_FUNCTION_FAILED;     \
2646                                                 \
2647         e->u.logio.fcport = fcport;             \
2648         if (data) {                             \
2649                 e->u.logio.data[0] = data[0];   \
2650                 e->u.logio.data[1] = data[1];   \
2651         }                                       \
2652         return qla2x00_post_work(vha, e);       \
2653 }
2654
2655 qla2x00_post_async_work(login, QLA_EVT_ASYNC_LOGIN);
2656 qla2x00_post_async_work(login_done, QLA_EVT_ASYNC_LOGIN_DONE);
2657 qla2x00_post_async_work(logout, QLA_EVT_ASYNC_LOGOUT);
2658 qla2x00_post_async_work(logout_done, QLA_EVT_ASYNC_LOGOUT_DONE);
2659
2660 int
2661 qla2x00_post_uevent_work(struct scsi_qla_host *vha, u32 code)
2662 {
2663         struct qla_work_evt *e;
2664
2665         e = qla2x00_alloc_work(vha, QLA_EVT_UEVENT);
2666         if (!e)
2667                 return QLA_FUNCTION_FAILED;
2668
2669         e->u.uevent.code = code;
2670         return qla2x00_post_work(vha, e);
2671 }
2672
2673 static void
2674 qla2x00_uevent_emit(struct scsi_qla_host *vha, u32 code)
2675 {
2676         char event_string[40];
2677         char *envp[] = { event_string, NULL };
2678
2679         switch (code) {
2680         case QLA_UEVENT_CODE_FW_DUMP:
2681                 snprintf(event_string, sizeof(event_string), "FW_DUMP=%ld",
2682                     vha->host_no);
2683                 break;
2684         default:
2685                 /* do nothing */
2686                 break;
2687         }
2688         kobject_uevent_env(&vha->hw->pdev->dev.kobj, KOBJ_CHANGE, envp);
2689 }
2690
2691 void
2692 qla2x00_do_work(struct scsi_qla_host *vha)
2693 {
2694         struct qla_work_evt *e, *tmp;
2695         unsigned long flags;
2696         LIST_HEAD(work);
2697
2698         spin_lock_irqsave(&vha->work_lock, flags);
2699         list_splice_init(&vha->work_list, &work);
2700         spin_unlock_irqrestore(&vha->work_lock, flags);
2701
2702         list_for_each_entry_safe(e, tmp, &work, list) {
2703                 list_del_init(&e->list);
2704
2705                 switch (e->type) {
2706                 case QLA_EVT_AEN:
2707                         fc_host_post_event(vha->host, fc_get_event_number(),
2708                             e->u.aen.code, e->u.aen.data);
2709                         break;
2710                 case QLA_EVT_IDC_ACK:
2711                         qla81xx_idc_ack(vha, e->u.idc_ack.mb);
2712                         break;
2713                 case QLA_EVT_ASYNC_LOGIN:
2714                         qla2x00_async_login(vha, e->u.logio.fcport,
2715                             e->u.logio.data);
2716                         break;
2717                 case QLA_EVT_ASYNC_LOGIN_DONE:
2718                         qla2x00_async_login_done(vha, e->u.logio.fcport,
2719                             e->u.logio.data);
2720                         break;
2721                 case QLA_EVT_ASYNC_LOGOUT:
2722                         qla2x00_async_logout(vha, e->u.logio.fcport);
2723                         break;
2724                 case QLA_EVT_ASYNC_LOGOUT_DONE:
2725                         qla2x00_async_logout_done(vha, e->u.logio.fcport,
2726                             e->u.logio.data);
2727                         break;
2728                 case QLA_EVT_UEVENT:
2729                         qla2x00_uevent_emit(vha, e->u.uevent.code);
2730                         break;
2731                 }
2732                 if (e->flags & QLA_EVT_FLAG_FREE)
2733                         kfree(e);
2734         }
2735 }
2736
2737 /* Relogins all the fcports of a vport
2738  * Context: dpc thread
2739  */
2740 void qla2x00_relogin(struct scsi_qla_host *vha)
2741 {
2742         fc_port_t       *fcport;
2743         int status;
2744         uint16_t        next_loopid = 0;
2745         struct qla_hw_data *ha = vha->hw;
2746         uint16_t data[2];
2747
2748         list_for_each_entry(fcport, &vha->vp_fcports, list) {
2749         /*
2750          * If the port is not ONLINE then try to login
2751          * to it if we haven't run out of retries.
2752          */
2753                 if (atomic_read(&fcport->state) !=
2754                         FCS_ONLINE && fcport->login_retry) {
2755
2756                         fcport->login_retry--;
2757                         if (fcport->flags & FCF_FABRIC_DEVICE) {
2758                                 if (fcport->flags & FCF_TAPE_PRESENT)
2759                                         ha->isp_ops->fabric_logout(vha,
2760                                                         fcport->loop_id,
2761                                                         fcport->d_id.b.domain,
2762                                                         fcport->d_id.b.area,
2763                                                         fcport->d_id.b.al_pa);
2764
2765                                 if (IS_ALOGIO_CAPABLE(ha)) {
2766                                         data[0] = 0;
2767                                         data[1] = QLA_LOGIO_LOGIN_RETRIED;
2768                                         status = qla2x00_post_async_login_work(
2769                                             vha, fcport, data);
2770                                         if (status == QLA_SUCCESS)
2771                                                 continue;
2772                                         /* Attempt a retry. */
2773                                         status = 1;
2774                                 } else
2775                                         status = qla2x00_fabric_login(vha,
2776                                             fcport, &next_loopid);
2777                         } else
2778                                 status = qla2x00_local_device_login(vha,
2779                                                                 fcport);
2780
2781                         if (status == QLA_SUCCESS) {
2782                                 fcport->old_loop_id = fcport->loop_id;
2783
2784                                 DEBUG(printk("scsi(%ld): port login OK: logged "
2785                                 "in ID 0x%x\n", vha->host_no, fcport->loop_id));
2786
2787                                 qla2x00_update_fcport(vha, fcport);
2788
2789                         } else if (status == 1) {
2790                                 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
2791                                 /* retry the login again */
2792                                 DEBUG(printk("scsi(%ld): Retrying"
2793                                 " %d login again loop_id 0x%x\n",
2794                                 vha->host_no, fcport->login_retry,
2795                                                 fcport->loop_id));
2796                         } else {
2797                                 fcport->login_retry = 0;
2798                         }
2799
2800                         if (fcport->login_retry == 0 && status != QLA_SUCCESS)
2801                                 fcport->loop_id = FC_NO_LOOP_ID;
2802                 }
2803                 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
2804                         break;
2805         }
2806 }
2807
2808 /**************************************************************************
2809 * qla2x00_do_dpc
2810 *   This kernel thread is a task that is schedule by the interrupt handler
2811 *   to perform the background processing for interrupts.
2812 *
2813 * Notes:
2814 * This task always run in the context of a kernel thread.  It
2815 * is kick-off by the driver's detect code and starts up
2816 * up one per adapter. It immediately goes to sleep and waits for
2817 * some fibre event.  When either the interrupt handler or
2818 * the timer routine detects a event it will one of the task
2819 * bits then wake us up.
2820 **************************************************************************/
2821 static int
2822 qla2x00_do_dpc(void *data)
2823 {
2824         int             rval;
2825         scsi_qla_host_t *base_vha;
2826         struct qla_hw_data *ha;
2827
2828         ha = (struct qla_hw_data *)data;
2829         base_vha = pci_get_drvdata(ha->pdev);
2830
2831         set_user_nice(current, -20);
2832
2833         while (!kthread_should_stop()) {
2834                 DEBUG3(printk("qla2x00: DPC handler sleeping\n"));
2835
2836                 set_current_state(TASK_INTERRUPTIBLE);
2837                 schedule();
2838                 __set_current_state(TASK_RUNNING);
2839
2840                 DEBUG3(printk("qla2x00: DPC handler waking up\n"));
2841
2842                 /* Initialization not yet finished. Don't do anything yet. */
2843                 if (!base_vha->flags.init_done)
2844                         continue;
2845
2846                 DEBUG3(printk("scsi(%ld): DPC handler\n", base_vha->host_no));
2847
2848                 ha->dpc_active = 1;
2849
2850                 if (ha->flags.mbox_busy) {
2851                         ha->dpc_active = 0;
2852                         continue;
2853                 }
2854
2855                 qla2x00_do_work(base_vha);
2856
2857                 if (test_and_clear_bit(ISP_ABORT_NEEDED,
2858                                                 &base_vha->dpc_flags)) {
2859
2860                         DEBUG(printk("scsi(%ld): dpc: sched "
2861                             "qla2x00_abort_isp ha = %p\n",
2862                             base_vha->host_no, ha));
2863                         if (!(test_and_set_bit(ABORT_ISP_ACTIVE,
2864                             &base_vha->dpc_flags))) {
2865
2866                                 if (qla2x00_abort_isp(base_vha)) {
2867                                         /* failed. retry later */
2868                                         set_bit(ISP_ABORT_NEEDED,
2869                                             &base_vha->dpc_flags);
2870                                 }
2871                                 clear_bit(ABORT_ISP_ACTIVE,
2872                                                 &base_vha->dpc_flags);
2873                         }
2874
2875                         DEBUG(printk("scsi(%ld): dpc: qla2x00_abort_isp end\n",
2876                             base_vha->host_no));
2877                 }
2878
2879                 if (test_bit(FCPORT_UPDATE_NEEDED, &base_vha->dpc_flags)) {
2880                         qla2x00_update_fcports(base_vha);
2881                         clear_bit(FCPORT_UPDATE_NEEDED, &base_vha->dpc_flags);
2882                 }
2883
2884                 if (test_and_clear_bit(RESET_MARKER_NEEDED,
2885                                                         &base_vha->dpc_flags) &&
2886                     (!(test_and_set_bit(RESET_ACTIVE, &base_vha->dpc_flags)))) {
2887
2888                         DEBUG(printk("scsi(%ld): qla2x00_reset_marker()\n",
2889                             base_vha->host_no));
2890
2891                         qla2x00_rst_aen(base_vha);
2892                         clear_bit(RESET_ACTIVE, &base_vha->dpc_flags);
2893                 }
2894
2895                 /* Retry each device up to login retry count */
2896                 if ((test_and_clear_bit(RELOGIN_NEEDED,
2897                                                 &base_vha->dpc_flags)) &&
2898                     !test_bit(LOOP_RESYNC_NEEDED, &base_vha->dpc_flags) &&
2899                     atomic_read(&base_vha->loop_state) != LOOP_DOWN) {
2900
2901                         DEBUG(printk("scsi(%ld): qla2x00_port_login()\n",
2902                                         base_vha->host_no));
2903                         qla2x00_relogin(base_vha);
2904
2905                         DEBUG(printk("scsi(%ld): qla2x00_port_login - end\n",
2906                             base_vha->host_no));
2907                 }
2908
2909                 if (test_and_clear_bit(LOOP_RESYNC_NEEDED,
2910                                                         &base_vha->dpc_flags)) {
2911
2912                         DEBUG(printk("scsi(%ld): qla2x00_loop_resync()\n",
2913                                 base_vha->host_no));
2914
2915                         if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE,
2916                             &base_vha->dpc_flags))) {
2917
2918                                 rval = qla2x00_loop_resync(base_vha);
2919
2920                                 clear_bit(LOOP_RESYNC_ACTIVE,
2921                                                 &base_vha->dpc_flags);
2922                         }
2923
2924                         DEBUG(printk("scsi(%ld): qla2x00_loop_resync - end\n",
2925                             base_vha->host_no));
2926                 }
2927
2928                 if (test_bit(NPIV_CONFIG_NEEDED, &base_vha->dpc_flags) &&
2929                     atomic_read(&base_vha->loop_state) == LOOP_READY) {
2930                         clear_bit(NPIV_CONFIG_NEEDED, &base_vha->dpc_flags);
2931                         qla2xxx_flash_npiv_conf(base_vha);
2932                 }
2933
2934                 if (!ha->interrupts_on)
2935                         ha->isp_ops->enable_intrs(ha);
2936
2937                 if (test_and_clear_bit(BEACON_BLINK_NEEDED,
2938                                         &base_vha->dpc_flags))
2939                         ha->isp_ops->beacon_blink(base_vha);
2940
2941                 qla2x00_do_dpc_all_vps(base_vha);
2942
2943                 ha->dpc_active = 0;
2944         } /* End of while(1) */
2945
2946         DEBUG(printk("scsi(%ld): DPC handler exiting\n", base_vha->host_no));
2947
2948         /*
2949          * Make sure that nobody tries to wake us up again.
2950          */
2951         ha->dpc_active = 0;
2952
2953         /* Cleanup any residual CTX SRBs. */
2954         qla2x00_abort_all_cmds(base_vha, DID_NO_CONNECT << 16);
2955
2956         return 0;
2957 }
2958
2959 void
2960 qla2xxx_wake_dpc(struct scsi_qla_host *vha)
2961 {
2962         struct qla_hw_data *ha = vha->hw;
2963         struct task_struct *t = ha->dpc_thread;
2964
2965         if (!test_bit(UNLOADING, &vha->dpc_flags) && t)
2966                 wake_up_process(t);
2967 }
2968
2969 /*
2970 *  qla2x00_rst_aen
2971 *      Processes asynchronous reset.
2972 *
2973 * Input:
2974 *      ha  = adapter block pointer.
2975 */
2976 static void
2977 qla2x00_rst_aen(scsi_qla_host_t *vha)
2978 {
2979         if (vha->flags.online && !vha->flags.reset_active &&
2980             !atomic_read(&vha->loop_down_timer) &&
2981             !(test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))) {
2982                 do {
2983                         clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
2984
2985                         /*
2986                          * Issue marker command only when we are going to start
2987                          * the I/O.
2988                          */
2989                         vha->marker_needed = 1;
2990                 } while (!atomic_read(&vha->loop_down_timer) &&
2991                     (test_bit(RESET_MARKER_NEEDED, &vha->dpc_flags)));
2992         }
2993 }
2994
2995 static void
2996 qla2x00_sp_free_dma(srb_t *sp)
2997 {
2998         struct scsi_cmnd *cmd = sp->cmd;
2999
3000         if (sp->flags & SRB_DMA_VALID) {
3001                 scsi_dma_unmap(cmd);
3002                 sp->flags &= ~SRB_DMA_VALID;
3003         }
3004         CMD_SP(cmd) = NULL;
3005 }
3006
3007 void
3008 qla2x00_sp_compl(struct qla_hw_data *ha, srb_t *sp)
3009 {
3010         struct scsi_cmnd *cmd = sp->cmd;
3011
3012         qla2x00_sp_free_dma(sp);
3013
3014         mempool_free(sp, ha->srb_mempool);
3015
3016         cmd->scsi_done(cmd);
3017 }
3018
3019 /**************************************************************************
3020 *   qla2x00_timer
3021 *
3022 * Description:
3023 *   One second timer
3024 *
3025 * Context: Interrupt
3026 ***************************************************************************/
3027 void
3028 qla2x00_timer(scsi_qla_host_t *vha)
3029 {
3030         unsigned long   cpu_flags = 0;
3031         fc_port_t       *fcport;
3032         int             start_dpc = 0;
3033         int             index;
3034         srb_t           *sp;
3035         int             t;
3036         struct qla_hw_data *ha = vha->hw;
3037         struct req_que *req;
3038         /*
3039          * Ports - Port down timer.
3040          *
3041          * Whenever, a port is in the LOST state we start decrementing its port
3042          * down timer every second until it reaches zero. Once  it reaches zero
3043          * the port it marked DEAD.
3044          */
3045         t = 0;
3046         list_for_each_entry(fcport, &vha->vp_fcports, list) {
3047                 if (fcport->port_type != FCT_TARGET)
3048                         continue;
3049
3050                 if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
3051
3052                         if (atomic_read(&fcport->port_down_timer) == 0)
3053                                 continue;
3054
3055                         if (atomic_dec_and_test(&fcport->port_down_timer) != 0)
3056                                 atomic_set(&fcport->state, FCS_DEVICE_DEAD);
3057
3058                         DEBUG(printk("scsi(%ld): fcport-%d - port retry count: "
3059                             "%d remaining\n",
3060                             vha->host_no,
3061                             t, atomic_read(&fcport->port_down_timer)));
3062                 }
3063                 t++;
3064         } /* End of for fcport  */
3065
3066
3067         /* Loop down handler. */
3068         if (atomic_read(&vha->loop_down_timer) > 0 &&
3069             !(test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))
3070                 && vha->flags.online) {
3071
3072                 if (atomic_read(&vha->loop_down_timer) ==
3073                     vha->loop_down_abort_time) {
3074
3075                         DEBUG(printk("scsi(%ld): Loop Down - aborting the "
3076                             "queues before time expire\n",
3077                             vha->host_no));
3078
3079                         if (!IS_QLA2100(ha) && vha->link_down_timeout)
3080                                 atomic_set(&vha->loop_state, LOOP_DEAD);
3081
3082                         /* Schedule an ISP abort to return any tape commands. */
3083                         /* NPIV - scan physical port only */
3084                         if (!vha->vp_idx) {
3085                                 spin_lock_irqsave(&ha->hardware_lock,
3086                                     cpu_flags);
3087                                 req = ha->req_q_map[0];
3088                                 for (index = 1;
3089                                     index < MAX_OUTSTANDING_COMMANDS;
3090                                     index++) {
3091                                         fc_port_t *sfcp;
3092
3093                                         sp = req->outstanding_cmds[index];
3094                                         if (!sp)
3095                                                 continue;
3096                                         if (sp->ctx)
3097                                                 continue;
3098                                         sfcp = sp->fcport;
3099                                         if (!(sfcp->flags & FCF_TAPE_PRESENT))
3100                                                 continue;
3101
3102                                         set_bit(ISP_ABORT_NEEDED,
3103                                                         &vha->dpc_flags);
3104                                         break;
3105                                 }
3106                                 spin_unlock_irqrestore(&ha->hardware_lock,
3107                                                                 cpu_flags);
3108                         }
3109                         start_dpc++;
3110                 }
3111
3112                 /* if the loop has been down for 4 minutes, reinit adapter */
3113                 if (atomic_dec_and_test(&vha->loop_down_timer) != 0) {
3114                         if (!(vha->device_flags & DFLG_NO_CABLE)) {
3115                                 DEBUG(printk("scsi(%ld): Loop down - "
3116                                     "aborting ISP.\n",
3117                                     vha->host_no));
3118                                 qla_printk(KERN_WARNING, ha,
3119                                     "Loop down - aborting ISP.\n");
3120
3121                                 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3122                         }
3123                 }
3124                 DEBUG3(printk("scsi(%ld): Loop Down - seconds remaining %d\n",
3125                     vha->host_no,
3126                     atomic_read(&vha->loop_down_timer)));
3127         }
3128
3129         /* Check if beacon LED needs to be blinked */
3130         if (ha->beacon_blink_led == 1) {
3131                 set_bit(BEACON_BLINK_NEEDED, &vha->dpc_flags);
3132                 start_dpc++;
3133         }
3134
3135         /* Process any deferred work. */
3136         if (!list_empty(&vha->work_list))
3137                 start_dpc++;
3138
3139         /* Schedule the DPC routine if needed */
3140         if ((test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
3141             test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags) ||
3142             test_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags) ||
3143             start_dpc ||
3144             test_bit(RESET_MARKER_NEEDED, &vha->dpc_flags) ||
3145             test_bit(BEACON_BLINK_NEEDED, &vha->dpc_flags) ||
3146             test_bit(VP_DPC_NEEDED, &vha->dpc_flags) ||
3147             test_bit(RELOGIN_NEEDED, &vha->dpc_flags)))
3148                 qla2xxx_wake_dpc(vha);
3149
3150         qla2x00_restart_timer(vha, WATCH_INTERVAL);
3151 }
3152
3153 /* Firmware interface routines. */
3154
3155 #define FW_BLOBS        7
3156 #define FW_ISP21XX      0
3157 #define FW_ISP22XX      1
3158 #define FW_ISP2300      2
3159 #define FW_ISP2322      3
3160 #define FW_ISP24XX      4
3161 #define FW_ISP25XX      5
3162 #define FW_ISP81XX      6
3163
3164 #define FW_FILE_ISP21XX "ql2100_fw.bin"
3165 #define FW_FILE_ISP22XX "ql2200_fw.bin"
3166 #define FW_FILE_ISP2300 "ql2300_fw.bin"
3167 #define FW_FILE_ISP2322 "ql2322_fw.bin"
3168 #define FW_FILE_ISP24XX "ql2400_fw.bin"
3169 #define FW_FILE_ISP25XX "ql2500_fw.bin"
3170 #define FW_FILE_ISP81XX "ql8100_fw.bin"
3171
3172 static DEFINE_MUTEX(qla_fw_lock);
3173
3174 static struct fw_blob qla_fw_blobs[FW_BLOBS] = {
3175         { .name = FW_FILE_ISP21XX, .segs = { 0x1000, 0 }, },
3176         { .name = FW_FILE_ISP22XX, .segs = { 0x1000, 0 }, },
3177         { .name = FW_FILE_ISP2300, .segs = { 0x800, 0 }, },
3178         { .name = FW_FILE_ISP2322, .segs = { 0x800, 0x1c000, 0x1e000, 0 }, },
3179         { .name = FW_FILE_ISP24XX, },
3180         { .name = FW_FILE_ISP25XX, },
3181         { .name = FW_FILE_ISP81XX, },
3182 };
3183
3184 struct fw_blob *
3185 qla2x00_request_firmware(scsi_qla_host_t *vha)
3186 {
3187         struct qla_hw_data *ha = vha->hw;
3188         struct fw_blob *blob;
3189
3190         blob = NULL;
3191         if (IS_QLA2100(ha)) {
3192                 blob = &qla_fw_blobs[FW_ISP21XX];
3193         } else if (IS_QLA2200(ha)) {
3194                 blob = &qla_fw_blobs[FW_ISP22XX];
3195         } else if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
3196                 blob = &qla_fw_blobs[FW_ISP2300];
3197         } else if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
3198                 blob = &qla_fw_blobs[FW_ISP2322];
3199         } else if (IS_QLA24XX_TYPE(ha)) {
3200                 blob = &qla_fw_blobs[FW_ISP24XX];
3201         } else if (IS_QLA25XX(ha)) {
3202                 blob = &qla_fw_blobs[FW_ISP25XX];
3203         } else if (IS_QLA81XX(ha)) {
3204                 blob = &qla_fw_blobs[FW_ISP81XX];
3205         }
3206
3207         mutex_lock(&qla_fw_lock);
3208         if (blob->fw)
3209                 goto out;
3210
3211         if (request_firmware(&blob->fw, blob->name, &ha->pdev->dev)) {
3212                 DEBUG2(printk("scsi(%ld): Failed to load firmware image "
3213                     "(%s).\n", vha->host_no, blob->name));
3214                 blob->fw = NULL;
3215                 blob = NULL;
3216                 goto out;
3217         }
3218
3219 out:
3220         mutex_unlock(&qla_fw_lock);
3221         return blob;
3222 }
3223
3224 static void
3225 qla2x00_release_firmware(void)
3226 {
3227         int idx;
3228
3229         mutex_lock(&qla_fw_lock);
3230         for (idx = 0; idx < FW_BLOBS; idx++)
3231                 if (qla_fw_blobs[idx].fw)
3232                         release_firmware(qla_fw_blobs[idx].fw);
3233         mutex_unlock(&qla_fw_lock);
3234 }
3235
3236 static pci_ers_result_t
3237 qla2xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
3238 {
3239         scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
3240
3241         switch (state) {
3242         case pci_channel_io_normal:
3243                 return PCI_ERS_RESULT_CAN_RECOVER;
3244         case pci_channel_io_frozen:
3245                 pci_disable_device(pdev);
3246                 return PCI_ERS_RESULT_NEED_RESET;
3247         case pci_channel_io_perm_failure:
3248                 qla2x00_abort_all_cmds(base_vha, DID_NO_CONNECT << 16);
3249                 return PCI_ERS_RESULT_DISCONNECT;
3250         }
3251         return PCI_ERS_RESULT_NEED_RESET;
3252 }
3253
3254 static pci_ers_result_t
3255 qla2xxx_pci_mmio_enabled(struct pci_dev *pdev)
3256 {
3257         int risc_paused = 0;
3258         uint32_t stat;
3259         unsigned long flags;
3260         scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
3261         struct qla_hw_data *ha = base_vha->hw;
3262         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
3263         struct device_reg_24xx __iomem *reg24 = &ha->iobase->isp24;
3264
3265         spin_lock_irqsave(&ha->hardware_lock, flags);
3266         if (IS_QLA2100(ha) || IS_QLA2200(ha)){
3267                 stat = RD_REG_DWORD(&reg->hccr);
3268                 if (stat & HCCR_RISC_PAUSE)
3269                         risc_paused = 1;
3270         } else if (IS_QLA23XX(ha)) {
3271                 stat = RD_REG_DWORD(&reg->u.isp2300.host_status);
3272                 if (stat & HSR_RISC_PAUSED)
3273                         risc_paused = 1;
3274         } else if (IS_FWI2_CAPABLE(ha)) {
3275                 stat = RD_REG_DWORD(&reg24->host_status);
3276                 if (stat & HSRX_RISC_PAUSED)
3277                         risc_paused = 1;
3278         }
3279         spin_unlock_irqrestore(&ha->hardware_lock, flags);
3280
3281         if (risc_paused) {
3282                 qla_printk(KERN_INFO, ha, "RISC paused -- mmio_enabled, "
3283                     "Dumping firmware!\n");
3284                 ha->isp_ops->fw_dump(base_vha, 0);
3285
3286                 return PCI_ERS_RESULT_NEED_RESET;
3287         } else
3288                 return PCI_ERS_RESULT_RECOVERED;
3289 }
3290
3291 static pci_ers_result_t
3292 qla2xxx_pci_slot_reset(struct pci_dev *pdev)
3293 {
3294         pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
3295         scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
3296         struct qla_hw_data *ha = base_vha->hw;
3297         int rc;
3298
3299         if (ha->mem_only)
3300                 rc = pci_enable_device_mem(pdev);
3301         else
3302                 rc = pci_enable_device(pdev);
3303
3304         if (rc) {
3305                 qla_printk(KERN_WARNING, ha,
3306                     "Can't re-enable PCI device after reset.\n");
3307
3308                 return ret;
3309         }
3310         pci_set_master(pdev);
3311
3312         if (ha->isp_ops->pci_config(base_vha))
3313                 return ret;
3314
3315         set_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
3316         if (qla2x00_abort_isp(base_vha) == QLA_SUCCESS)
3317                 ret =  PCI_ERS_RESULT_RECOVERED;
3318         clear_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
3319
3320         return ret;
3321 }
3322
3323 static void
3324 qla2xxx_pci_resume(struct pci_dev *pdev)
3325 {
3326         scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
3327         struct qla_hw_data *ha = base_vha->hw;
3328         int ret;
3329
3330         ret = qla2x00_wait_for_hba_online(base_vha);
3331         if (ret != QLA_SUCCESS) {
3332                 qla_printk(KERN_ERR, ha,
3333                     "the device failed to resume I/O "
3334                     "from slot/link_reset");
3335         }
3336         pci_cleanup_aer_uncorrect_error_status(pdev);
3337 }
3338
3339 static struct pci_error_handlers qla2xxx_err_handler = {
3340         .error_detected = qla2xxx_pci_error_detected,
3341         .mmio_enabled = qla2xxx_pci_mmio_enabled,
3342         .slot_reset = qla2xxx_pci_slot_reset,
3343         .resume = qla2xxx_pci_resume,
3344 };
3345
3346 static struct pci_device_id qla2xxx_pci_tbl[] = {
3347         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2100) },
3348         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2200) },
3349         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2300) },
3350         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2312) },
3351         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2322) },
3352         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP6312) },
3353         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP6322) },
3354         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2422) },
3355         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2432) },
3356         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP8432) },
3357         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP5422) },
3358         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP5432) },
3359         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2532) },
3360         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP8001) },
3361         { 0 },
3362 };
3363 MODULE_DEVICE_TABLE(pci, qla2xxx_pci_tbl);
3364
3365 static struct pci_driver qla2xxx_pci_driver = {
3366         .name           = QLA2XXX_DRIVER_NAME,
3367         .driver         = {
3368                 .owner          = THIS_MODULE,
3369         },
3370         .id_table       = qla2xxx_pci_tbl,
3371         .probe          = qla2x00_probe_one,
3372         .remove         = qla2x00_remove_one,
3373         .err_handler    = &qla2xxx_err_handler,
3374 };
3375
3376 /**
3377  * qla2x00_module_init - Module initialization.
3378  **/
3379 static int __init
3380 qla2x00_module_init(void)
3381 {
3382         int ret = 0;
3383
3384         /* Allocate cache for SRBs. */
3385         srb_cachep = kmem_cache_create("qla2xxx_srbs", sizeof(srb_t), 0,
3386             SLAB_HWCACHE_ALIGN, NULL);
3387         if (srb_cachep == NULL) {
3388                 printk(KERN_ERR
3389                     "qla2xxx: Unable to allocate SRB cache...Failing load!\n");
3390                 return -ENOMEM;
3391         }
3392
3393         /* Derive version string. */
3394         strcpy(qla2x00_version_str, QLA2XXX_VERSION);
3395         if (ql2xextended_error_logging)
3396                 strcat(qla2x00_version_str, "-debug");
3397
3398         qla2xxx_transport_template =
3399             fc_attach_transport(&qla2xxx_transport_functions);
3400         if (!qla2xxx_transport_template) {
3401                 kmem_cache_destroy(srb_cachep);
3402                 return -ENODEV;
3403         }
3404         qla2xxx_transport_vport_template =
3405             fc_attach_transport(&qla2xxx_transport_vport_functions);
3406         if (!qla2xxx_transport_vport_template) {
3407                 kmem_cache_destroy(srb_cachep);
3408                 fc_release_transport(qla2xxx_transport_template);
3409                 return -ENODEV;
3410         }
3411
3412         printk(KERN_INFO "QLogic Fibre Channel HBA Driver: %s\n",
3413             qla2x00_version_str);
3414         ret = pci_register_driver(&qla2xxx_pci_driver);
3415         if (ret) {
3416                 kmem_cache_destroy(srb_cachep);
3417                 fc_release_transport(qla2xxx_transport_template);
3418                 fc_release_transport(qla2xxx_transport_vport_template);
3419         }
3420         return ret;
3421 }
3422
3423 /**
3424  * qla2x00_module_exit - Module cleanup.
3425  **/
3426 static void __exit
3427 qla2x00_module_exit(void)
3428 {
3429         pci_unregister_driver(&qla2xxx_pci_driver);
3430         qla2x00_release_firmware();
3431         kmem_cache_destroy(srb_cachep);
3432         fc_release_transport(qla2xxx_transport_template);
3433         fc_release_transport(qla2xxx_transport_vport_template);
3434 }
3435
3436 module_init(qla2x00_module_init);
3437 module_exit(qla2x00_module_exit);
3438
3439 MODULE_AUTHOR("QLogic Corporation");
3440 MODULE_DESCRIPTION("QLogic Fibre Channel HBA Driver");
3441 MODULE_LICENSE("GPL");
3442 MODULE_VERSION(QLA2XXX_VERSION);
3443 MODULE_FIRMWARE(FW_FILE_ISP21XX);
3444 MODULE_FIRMWARE(FW_FILE_ISP22XX);
3445 MODULE_FIRMWARE(FW_FILE_ISP2300);
3446 MODULE_FIRMWARE(FW_FILE_ISP2322);
3447 MODULE_FIRMWARE(FW_FILE_ISP24XX);
3448 MODULE_FIRMWARE(FW_FILE_ISP25XX);
3449 MODULE_FIRMWARE(FW_FILE_ISP81XX);