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