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