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