[SCSI] qla4xxx: Dump HW/FW reg to figure out what caused FW to be hung for ISP82XX
[linux-2.6-block.git] / drivers / scsi / qla4xxx / ql4_os.c
1 /*
2  * QLogic iSCSI HBA Driver
3  * Copyright (c)  2003-2010 QLogic Corporation
4  *
5  * See LICENSE.qla4xxx for copyright and licensing details.
6  */
7 #include <linux/moduleparam.h>
8 #include <linux/slab.h>
9
10 #include <scsi/scsi_tcq.h>
11 #include <scsi/scsicam.h>
12
13 #include "ql4_def.h"
14 #include "ql4_version.h"
15 #include "ql4_glbl.h"
16 #include "ql4_dbg.h"
17 #include "ql4_inline.h"
18
19 /*
20  * Driver version
21  */
22 static char qla4xxx_version_str[40];
23
24 /*
25  * SRB allocation cache
26  */
27 static struct kmem_cache *srb_cachep;
28
29 /*
30  * Module parameter information and variables
31  */
32 int ql4xdontresethba = 0;
33 module_param(ql4xdontresethba, int, S_IRUGO | S_IWUSR);
34 MODULE_PARM_DESC(ql4xdontresethba,
35                 "Don't reset the HBA for driver recovery \n"
36                 " 0 - It will reset HBA (Default)\n"
37                 " 1 - It will NOT reset HBA");
38
39 int ql4xextended_error_logging = 0; /* 0 = off, 1 = log errors */
40 module_param(ql4xextended_error_logging, int, S_IRUGO | S_IWUSR);
41 MODULE_PARM_DESC(ql4xextended_error_logging,
42                  "Option to enable extended error logging, "
43                  "Default is 0 - no logging, 1 - debug logging");
44
45 int ql4xenablemsix = 1;
46 module_param(ql4xenablemsix, int, S_IRUGO|S_IWUSR);
47 MODULE_PARM_DESC(ql4xenablemsix,
48                 "Set to enable MSI or MSI-X interrupt mechanism.\n"
49                 " 0 = enable INTx interrupt mechanism.\n"
50                 " 1 = enable MSI-X interrupt mechanism (Default).\n"
51                 " 2 = enable MSI interrupt mechanism.");
52
53 #define QL4_DEF_QDEPTH 32
54 static int ql4xmaxqdepth = QL4_DEF_QDEPTH;
55 module_param(ql4xmaxqdepth, int, S_IRUGO | S_IWUSR);
56 MODULE_PARM_DESC(ql4xmaxqdepth,
57                 "Maximum queue depth to report for target devices.\n"
58                 " Default: 32.");
59
60 static int ql4xsess_recovery_tmo = QL4_SESS_RECOVERY_TMO;
61 module_param(ql4xsess_recovery_tmo, int, S_IRUGO);
62 MODULE_PARM_DESC(ql4xsess_recovery_tmo,
63                 "Target Session Recovery Timeout.\n"
64                 " Default: 30 sec.");
65
66 /*
67  * SCSI host template entry points
68  */
69 static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
70
71 /*
72  * iSCSI template entry points
73  */
74 static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
75                              enum iscsi_tgt_dscvr type, uint32_t enable,
76                              struct sockaddr *dst_addr);
77 static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
78                                   enum iscsi_param param, char *buf);
79 static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
80                                   enum iscsi_param param, char *buf);
81 static int qla4xxx_host_get_param(struct Scsi_Host *shost,
82                                   enum iscsi_host_param param, char *buf);
83 static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session);
84 static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
85
86 /*
87  * SCSI host template entry points
88  */
89 static int qla4xxx_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmd);
90 static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
91 static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
92 static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
93 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
94 static int qla4xxx_slave_alloc(struct scsi_device *device);
95 static int qla4xxx_slave_configure(struct scsi_device *device);
96 static void qla4xxx_slave_destroy(struct scsi_device *sdev);
97 static void qla4xxx_scan_start(struct Scsi_Host *shost);
98
99 static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
100     QLA82XX_LEGACY_INTR_CONFIG;
101
102 static struct scsi_host_template qla4xxx_driver_template = {
103         .module                 = THIS_MODULE,
104         .name                   = DRIVER_NAME,
105         .proc_name              = DRIVER_NAME,
106         .queuecommand           = qla4xxx_queuecommand,
107
108         .eh_abort_handler       = qla4xxx_eh_abort,
109         .eh_device_reset_handler = qla4xxx_eh_device_reset,
110         .eh_target_reset_handler = qla4xxx_eh_target_reset,
111         .eh_host_reset_handler  = qla4xxx_eh_host_reset,
112         .eh_timed_out           = qla4xxx_eh_cmd_timed_out,
113
114         .slave_configure        = qla4xxx_slave_configure,
115         .slave_alloc            = qla4xxx_slave_alloc,
116         .slave_destroy          = qla4xxx_slave_destroy,
117
118         .scan_finished          = iscsi_scan_finished,
119         .scan_start             = qla4xxx_scan_start,
120
121         .this_id                = -1,
122         .cmd_per_lun            = 3,
123         .use_clustering         = ENABLE_CLUSTERING,
124         .sg_tablesize           = SG_ALL,
125
126         .max_sectors            = 0xFFFF,
127 };
128
129 static struct iscsi_transport qla4xxx_iscsi_transport = {
130         .owner                  = THIS_MODULE,
131         .name                   = DRIVER_NAME,
132         .caps                   = CAP_FW_DB | CAP_SENDTARGETS_OFFLOAD |
133                                   CAP_DATA_PATH_OFFLOAD,
134         .param_mask             = ISCSI_CONN_PORT | ISCSI_CONN_ADDRESS |
135                                   ISCSI_TARGET_NAME | ISCSI_TPGT |
136                                   ISCSI_TARGET_ALIAS,
137         .host_param_mask        = ISCSI_HOST_HWADDRESS |
138                                   ISCSI_HOST_IPADDRESS |
139                                   ISCSI_HOST_INITIATOR_NAME,
140         .tgt_dscvr              = qla4xxx_tgt_dscvr,
141         .get_conn_param         = qla4xxx_conn_get_param,
142         .get_session_param      = qla4xxx_sess_get_param,
143         .get_host_param         = qla4xxx_host_get_param,
144         .session_recovery_timedout = qla4xxx_recovery_timedout,
145 };
146
147 static struct scsi_transport_template *qla4xxx_scsi_transport;
148
149 static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
150 {
151         struct iscsi_cls_session *session;
152         struct ddb_entry *ddb_entry;
153
154         session = starget_to_session(scsi_target(sc->device));
155         ddb_entry = session->dd_data;
156
157         /* if we are not logged in then the LLD is going to clean up the cmd */
158         if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
159                 return BLK_EH_RESET_TIMER;
160         else
161                 return BLK_EH_NOT_HANDLED;
162 }
163
164 static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session)
165 {
166         struct ddb_entry *ddb_entry = session->dd_data;
167         struct scsi_qla_host *ha = ddb_entry->ha;
168
169         if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
170                 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
171
172                 DEBUG2(printk("scsi%ld: %s: ddb [%d] session recovery timeout "
173                               "of (%d) secs exhausted, marking device DEAD.\n",
174                               ha->host_no, __func__, ddb_entry->fw_ddb_index,
175                               ddb_entry->sess->recovery_tmo));
176         }
177 }
178
179 static int qla4xxx_host_get_param(struct Scsi_Host *shost,
180                                   enum iscsi_host_param param, char *buf)
181 {
182         struct scsi_qla_host *ha = to_qla_host(shost);
183         int len;
184
185         switch (param) {
186         case ISCSI_HOST_PARAM_HWADDRESS:
187                 len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
188                 break;
189         case ISCSI_HOST_PARAM_IPADDRESS:
190                 len = sprintf(buf, "%d.%d.%d.%d\n", ha->ip_address[0],
191                               ha->ip_address[1], ha->ip_address[2],
192                               ha->ip_address[3]);
193                 break;
194         case ISCSI_HOST_PARAM_INITIATOR_NAME:
195                 len = sprintf(buf, "%s\n", ha->name_string);
196                 break;
197         default:
198                 return -ENOSYS;
199         }
200
201         return len;
202 }
203
204 static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
205                                   enum iscsi_param param, char *buf)
206 {
207         struct ddb_entry *ddb_entry = sess->dd_data;
208         int len;
209
210         switch (param) {
211         case ISCSI_PARAM_TARGET_NAME:
212                 len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
213                                ddb_entry->iscsi_name);
214                 break;
215         case ISCSI_PARAM_TPGT:
216                 len = sprintf(buf, "%u\n", ddb_entry->tpgt);
217                 break;
218         case ISCSI_PARAM_TARGET_ALIAS:
219                 len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
220                     ddb_entry->iscsi_alias);
221                 break;
222         default:
223                 return -ENOSYS;
224         }
225
226         return len;
227 }
228
229 static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
230                                   enum iscsi_param param, char *buf)
231 {
232         struct iscsi_cls_session *session;
233         struct ddb_entry *ddb_entry;
234         int len;
235
236         session = iscsi_dev_to_session(conn->dev.parent);
237         ddb_entry = session->dd_data;
238
239         switch (param) {
240         case ISCSI_PARAM_CONN_PORT:
241                 len = sprintf(buf, "%hu\n", ddb_entry->port);
242                 break;
243         case ISCSI_PARAM_CONN_ADDRESS:
244                 /* TODO: what are the ipv6 bits */
245                 len = sprintf(buf, "%pI4\n", &ddb_entry->ip_addr);
246                 break;
247         default:
248                 return -ENOSYS;
249         }
250
251         return len;
252 }
253
254 static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
255                              enum iscsi_tgt_dscvr type, uint32_t enable,
256                              struct sockaddr *dst_addr)
257 {
258         struct scsi_qla_host *ha;
259         struct sockaddr_in *addr;
260         struct sockaddr_in6 *addr6;
261         int ret = 0;
262
263         ha = (struct scsi_qla_host *) shost->hostdata;
264
265         switch (type) {
266         case ISCSI_TGT_DSCVR_SEND_TARGETS:
267                 if (dst_addr->sa_family == AF_INET) {
268                         addr = (struct sockaddr_in *)dst_addr;
269                         if (qla4xxx_send_tgts(ha, (char *)&addr->sin_addr,
270                                               addr->sin_port) != QLA_SUCCESS)
271                                 ret = -EIO;
272                 } else if (dst_addr->sa_family == AF_INET6) {
273                         /*
274                          * TODO: fix qla4xxx_send_tgts
275                          */
276                         addr6 = (struct sockaddr_in6 *)dst_addr;
277                         if (qla4xxx_send_tgts(ha, (char *)&addr6->sin6_addr,
278                                               addr6->sin6_port) != QLA_SUCCESS)
279                                 ret = -EIO;
280                 } else
281                         ret = -ENOSYS;
282                 break;
283         default:
284                 ret = -ENOSYS;
285         }
286         return ret;
287 }
288
289 void qla4xxx_destroy_sess(struct ddb_entry *ddb_entry)
290 {
291         if (!ddb_entry->sess)
292                 return;
293
294         if (ddb_entry->conn) {
295                 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
296                 iscsi_remove_session(ddb_entry->sess);
297         }
298         iscsi_free_session(ddb_entry->sess);
299 }
300
301 int qla4xxx_add_sess(struct ddb_entry *ddb_entry)
302 {
303         int err;
304
305         ddb_entry->sess->recovery_tmo = ql4xsess_recovery_tmo;
306
307         err = iscsi_add_session(ddb_entry->sess, ddb_entry->fw_ddb_index);
308         if (err) {
309                 DEBUG2(printk(KERN_ERR "Could not add session.\n"));
310                 return err;
311         }
312
313         ddb_entry->conn = iscsi_create_conn(ddb_entry->sess, 0, 0);
314         if (!ddb_entry->conn) {
315                 iscsi_remove_session(ddb_entry->sess);
316                 DEBUG2(printk(KERN_ERR "Could not add connection.\n"));
317                 return -ENOMEM;
318         }
319
320         /* finally ready to go */
321         iscsi_unblock_session(ddb_entry->sess);
322         return 0;
323 }
324
325 struct ddb_entry *qla4xxx_alloc_sess(struct scsi_qla_host *ha)
326 {
327         struct ddb_entry *ddb_entry;
328         struct iscsi_cls_session *sess;
329
330         sess = iscsi_alloc_session(ha->host, &qla4xxx_iscsi_transport,
331                                    sizeof(struct ddb_entry));
332         if (!sess)
333                 return NULL;
334
335         ddb_entry = sess->dd_data;
336         memset(ddb_entry, 0, sizeof(*ddb_entry));
337         ddb_entry->ha = ha;
338         ddb_entry->sess = sess;
339         return ddb_entry;
340 }
341
342 static void qla4xxx_scan_start(struct Scsi_Host *shost)
343 {
344         struct scsi_qla_host *ha = shost_priv(shost);
345         struct ddb_entry *ddb_entry, *ddbtemp;
346
347         /* finish setup of sessions that were already setup in firmware */
348         list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
349                 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE)
350                         qla4xxx_add_sess(ddb_entry);
351         }
352 }
353
354 /*
355  * Timer routines
356  */
357
358 static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
359                                 unsigned long interval)
360 {
361         DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
362                      __func__, ha->host->host_no));
363         init_timer(&ha->timer);
364         ha->timer.expires = jiffies + interval * HZ;
365         ha->timer.data = (unsigned long)ha;
366         ha->timer.function = (void (*)(unsigned long))func;
367         add_timer(&ha->timer);
368         ha->timer_active = 1;
369 }
370
371 static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
372 {
373         del_timer_sync(&ha->timer);
374         ha->timer_active = 0;
375 }
376
377 /***
378  * qla4xxx_mark_device_missing - mark a device as missing.
379  * @ha: Pointer to host adapter structure.
380  * @ddb_entry: Pointer to device database entry
381  *
382  * This routine marks a device missing and close connection.
383  **/
384 void qla4xxx_mark_device_missing(struct scsi_qla_host *ha,
385                                  struct ddb_entry *ddb_entry)
386 {
387         if ((atomic_read(&ddb_entry->state) != DDB_STATE_DEAD)) {
388                 atomic_set(&ddb_entry->state, DDB_STATE_MISSING);
389                 DEBUG2(printk("scsi%ld: ddb [%d] marked MISSING\n",
390                     ha->host_no, ddb_entry->fw_ddb_index));
391         } else
392                 DEBUG2(printk("scsi%ld: ddb [%d] DEAD\n", ha->host_no,
393                     ddb_entry->fw_ddb_index))
394
395         iscsi_block_session(ddb_entry->sess);
396         iscsi_conn_error_event(ddb_entry->conn, ISCSI_ERR_CONN_FAILED);
397 }
398
399 /**
400  * qla4xxx_mark_all_devices_missing - mark all devices as missing.
401  * @ha: Pointer to host adapter structure.
402  *
403  * This routine marks a device missing and resets the relogin retry count.
404  **/
405 void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
406 {
407         struct ddb_entry *ddb_entry, *ddbtemp;
408         list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
409                 qla4xxx_mark_device_missing(ha, ddb_entry);
410         }
411 }
412
413 static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
414                                        struct ddb_entry *ddb_entry,
415                                        struct scsi_cmnd *cmd,
416                                        void (*done)(struct scsi_cmnd *))
417 {
418         struct srb *srb;
419
420         srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
421         if (!srb)
422                 return srb;
423
424         kref_init(&srb->srb_ref);
425         srb->ha = ha;
426         srb->ddb = ddb_entry;
427         srb->cmd = cmd;
428         srb->flags = 0;
429         CMD_SP(cmd) = (void *)srb;
430         cmd->scsi_done = done;
431
432         return srb;
433 }
434
435 static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
436 {
437         struct scsi_cmnd *cmd = srb->cmd;
438
439         if (srb->flags & SRB_DMA_VALID) {
440                 scsi_dma_unmap(cmd);
441                 srb->flags &= ~SRB_DMA_VALID;
442         }
443         CMD_SP(cmd) = NULL;
444 }
445
446 void qla4xxx_srb_compl(struct kref *ref)
447 {
448         struct srb *srb = container_of(ref, struct srb, srb_ref);
449         struct scsi_cmnd *cmd = srb->cmd;
450         struct scsi_qla_host *ha = srb->ha;
451
452         qla4xxx_srb_free_dma(ha, srb);
453
454         mempool_free(srb, ha->srb_mempool);
455
456         cmd->scsi_done(cmd);
457 }
458
459 /**
460  * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
461  * @cmd: Pointer to Linux's SCSI command structure
462  * @done_fn: Function that the driver calls to notify the SCSI mid-layer
463  *      that the command has been processed.
464  *
465  * Remarks:
466  * This routine is invoked by Linux to send a SCSI command to the driver.
467  * The mid-level driver tries to ensure that queuecommand never gets
468  * invoked concurrently with itself or the interrupt handler (although
469  * the interrupt handler may call this routine as part of request-
470  * completion handling).   Unfortunely, it sometimes calls the scheduler
471  * in interrupt context which is a big NO! NO!.
472  **/
473 static int qla4xxx_queuecommand_lck(struct scsi_cmnd *cmd,
474                                 void (*done)(struct scsi_cmnd *))
475 {
476         struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
477         struct ddb_entry *ddb_entry = cmd->device->hostdata;
478         struct iscsi_cls_session *sess = ddb_entry->sess;
479         struct srb *srb;
480         int rval;
481
482         if (test_bit(AF_EEH_BUSY, &ha->flags)) {
483                 if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
484                         cmd->result = DID_NO_CONNECT << 16;
485                 else
486                         cmd->result = DID_REQUEUE << 16;
487                 goto qc_fail_command;
488         }
489
490         if (!sess) {
491                 cmd->result = DID_IMM_RETRY << 16;
492                 goto qc_fail_command;
493         }
494
495         rval = iscsi_session_chkready(sess);
496         if (rval) {
497                 cmd->result = rval;
498                 goto qc_fail_command;
499         }
500
501         if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
502                 if (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD) {
503                         cmd->result = DID_NO_CONNECT << 16;
504                         goto qc_fail_command;
505                 }
506                 return SCSI_MLQUEUE_TARGET_BUSY;
507         }
508
509         if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
510             test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
511             test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
512             test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
513             test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
514             !test_bit(AF_ONLINE, &ha->flags) ||
515             test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
516                 goto qc_host_busy;
517
518         spin_unlock_irq(ha->host->host_lock);
519
520         srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd, done);
521         if (!srb)
522                 goto qc_host_busy_lock;
523
524         rval = qla4xxx_send_command_to_isp(ha, srb);
525         if (rval != QLA_SUCCESS)
526                 goto qc_host_busy_free_sp;
527
528         spin_lock_irq(ha->host->host_lock);
529         return 0;
530
531 qc_host_busy_free_sp:
532         qla4xxx_srb_free_dma(ha, srb);
533         mempool_free(srb, ha->srb_mempool);
534
535 qc_host_busy_lock:
536         spin_lock_irq(ha->host->host_lock);
537
538 qc_host_busy:
539         return SCSI_MLQUEUE_HOST_BUSY;
540
541 qc_fail_command:
542         done(cmd);
543
544         return 0;
545 }
546
547 static DEF_SCSI_QCMD(qla4xxx_queuecommand)
548
549 /**
550  * qla4xxx_mem_free - frees memory allocated to adapter
551  * @ha: Pointer to host adapter structure.
552  *
553  * Frees memory previously allocated by qla4xxx_mem_alloc
554  **/
555 static void qla4xxx_mem_free(struct scsi_qla_host *ha)
556 {
557         if (ha->queues)
558                 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
559                                   ha->queues_dma);
560
561         ha->queues_len = 0;
562         ha->queues = NULL;
563         ha->queues_dma = 0;
564         ha->request_ring = NULL;
565         ha->request_dma = 0;
566         ha->response_ring = NULL;
567         ha->response_dma = 0;
568         ha->shadow_regs = NULL;
569         ha->shadow_regs_dma = 0;
570
571         /* Free srb pool. */
572         if (ha->srb_mempool)
573                 mempool_destroy(ha->srb_mempool);
574
575         ha->srb_mempool = NULL;
576
577         /* release io space registers  */
578         if (is_qla8022(ha)) {
579                 if (ha->nx_pcibase)
580                         iounmap(
581                             (struct device_reg_82xx __iomem *)ha->nx_pcibase);
582         } else if (ha->reg)
583                 iounmap(ha->reg);
584         pci_release_regions(ha->pdev);
585 }
586
587 /**
588  * qla4xxx_mem_alloc - allocates memory for use by adapter.
589  * @ha: Pointer to host adapter structure
590  *
591  * Allocates DMA memory for request and response queues. Also allocates memory
592  * for srbs.
593  **/
594 static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
595 {
596         unsigned long align;
597
598         /* Allocate contiguous block of DMA memory for queues. */
599         ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
600                           (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
601                           sizeof(struct shadow_regs) +
602                           MEM_ALIGN_VALUE +
603                           (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
604         ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
605                                         &ha->queues_dma, GFP_KERNEL);
606         if (ha->queues == NULL) {
607                 ql4_printk(KERN_WARNING, ha,
608                     "Memory Allocation failed - queues.\n");
609
610                 goto mem_alloc_error_exit;
611         }
612         memset(ha->queues, 0, ha->queues_len);
613
614         /*
615          * As per RISC alignment requirements -- the bus-address must be a
616          * multiple of the request-ring size (in bytes).
617          */
618         align = 0;
619         if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
620                 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
621                                            (MEM_ALIGN_VALUE - 1));
622
623         /* Update request and response queue pointers. */
624         ha->request_dma = ha->queues_dma + align;
625         ha->request_ring = (struct queue_entry *) (ha->queues + align);
626         ha->response_dma = ha->queues_dma + align +
627                 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
628         ha->response_ring = (struct queue_entry *) (ha->queues + align +
629                                                     (REQUEST_QUEUE_DEPTH *
630                                                      QUEUE_SIZE));
631         ha->shadow_regs_dma = ha->queues_dma + align +
632                 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
633                 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
634         ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
635                                                   (REQUEST_QUEUE_DEPTH *
636                                                    QUEUE_SIZE) +
637                                                   (RESPONSE_QUEUE_DEPTH *
638                                                    QUEUE_SIZE));
639
640         /* Allocate memory for srb pool. */
641         ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
642                                          mempool_free_slab, srb_cachep);
643         if (ha->srb_mempool == NULL) {
644                 ql4_printk(KERN_WARNING, ha,
645                     "Memory Allocation failed - SRB Pool.\n");
646
647                 goto mem_alloc_error_exit;
648         }
649
650         return QLA_SUCCESS;
651
652 mem_alloc_error_exit:
653         qla4xxx_mem_free(ha);
654         return QLA_ERROR;
655 }
656
657 /**
658  * qla4_8xxx_check_fw_alive  - Check firmware health
659  * @ha: Pointer to host adapter structure.
660  *
661  * Context: Interrupt
662  **/
663 static void qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
664 {
665         uint32_t fw_heartbeat_counter, halt_status;
666
667         fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
668         /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
669         if (fw_heartbeat_counter == 0xffffffff) {
670                 DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
671                     "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
672                     ha->host_no, __func__));
673                 return;
674         }
675
676         if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
677                 ha->seconds_since_last_heartbeat++;
678                 /* FW not alive after 2 seconds */
679                 if (ha->seconds_since_last_heartbeat == 2) {
680                         ha->seconds_since_last_heartbeat = 0;
681                         halt_status = qla4_8xxx_rd_32(ha,
682                                                       QLA82XX_PEG_HALT_STATUS1);
683
684                         ql4_printk(KERN_INFO, ha,
685                                    "scsi(%ld): %s, Dumping hw/fw registers:\n "
686                                    " PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2:"
687                                    " 0x%x,\n PEG_NET_0_PC: 0x%x, PEG_NET_1_PC:"
688                                    " 0x%x,\n PEG_NET_2_PC: 0x%x, PEG_NET_3_PC:"
689                                    " 0x%x,\n PEG_NET_4_PC: 0x%x\n",
690                                    ha->host_no, __func__, halt_status,
691                                    qla4_8xxx_rd_32(ha,
692                                                    QLA82XX_PEG_HALT_STATUS2),
693                                    qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_0 +
694                                                    0x3c),
695                                    qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_1 +
696                                                    0x3c),
697                                    qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_2 +
698                                                    0x3c),
699                                    qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_3 +
700                                                    0x3c),
701                                    qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_4 +
702                                                    0x3c));
703
704                         /* Since we cannot change dev_state in interrupt
705                          * context, set appropriate DPC flag then wakeup
706                          * DPC */
707                         if (halt_status & HALT_STATUS_UNRECOVERABLE)
708                                 set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
709                         else {
710                                 printk("scsi%ld: %s: detect abort needed!\n",
711                                     ha->host_no, __func__);
712                                 set_bit(DPC_RESET_HA, &ha->dpc_flags);
713                         }
714                         qla4xxx_wake_dpc(ha);
715                         qla4xxx_mailbox_premature_completion(ha);
716                 }
717         } else
718                 ha->seconds_since_last_heartbeat = 0;
719
720         ha->fw_heartbeat_counter = fw_heartbeat_counter;
721 }
722
723 /**
724  * qla4_8xxx_watchdog - Poll dev state
725  * @ha: Pointer to host adapter structure.
726  *
727  * Context: Interrupt
728  **/
729 void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
730 {
731         uint32_t dev_state;
732
733         dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
734
735         /* don't poll if reset is going on */
736         if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
737             test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
738             test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags))) {
739                 if (dev_state == QLA82XX_DEV_NEED_RESET &&
740                     !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
741                         if (!ql4xdontresethba) {
742                                 ql4_printk(KERN_INFO, ha, "%s: HW State: "
743                                     "NEED RESET!\n", __func__);
744                                 set_bit(DPC_RESET_HA, &ha->dpc_flags);
745                                 qla4xxx_wake_dpc(ha);
746                                 qla4xxx_mailbox_premature_completion(ha);
747                         }
748                 } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
749                     !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
750                         ql4_printk(KERN_INFO, ha, "%s: HW State: NEED QUIES!\n",
751                             __func__);
752                         set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
753                         qla4xxx_wake_dpc(ha);
754                 } else  {
755                         /* Check firmware health */
756                         qla4_8xxx_check_fw_alive(ha);
757                 }
758         }
759 }
760
761 /**
762  * qla4xxx_timer - checks every second for work to do.
763  * @ha: Pointer to host adapter structure.
764  **/
765 static void qla4xxx_timer(struct scsi_qla_host *ha)
766 {
767         struct ddb_entry *ddb_entry, *dtemp;
768         int start_dpc = 0;
769         uint16_t w;
770
771         /* If we are in the middle of AER/EEH processing
772          * skip any processing and reschedule the timer
773          */
774         if (test_bit(AF_EEH_BUSY, &ha->flags)) {
775                 mod_timer(&ha->timer, jiffies + HZ);
776                 return;
777         }
778
779         /* Hardware read to trigger an EEH error during mailbox waits. */
780         if (!pci_channel_offline(ha->pdev))
781                 pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
782
783         if (is_qla8022(ha)) {
784                 qla4_8xxx_watchdog(ha);
785         }
786
787         /* Search for relogin's to time-out and port down retry. */
788         list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
789                 /* Count down time between sending relogins */
790                 if (adapter_up(ha) &&
791                     !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
792                     atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
793                         if (atomic_read(&ddb_entry->retry_relogin_timer) !=
794                             INVALID_ENTRY) {
795                                 if (atomic_read(&ddb_entry->retry_relogin_timer)
796                                                 == 0) {
797                                         atomic_set(&ddb_entry->
798                                                 retry_relogin_timer,
799                                                 INVALID_ENTRY);
800                                         set_bit(DPC_RELOGIN_DEVICE,
801                                                 &ha->dpc_flags);
802                                         set_bit(DF_RELOGIN, &ddb_entry->flags);
803                                         DEBUG2(printk("scsi%ld: %s: ddb [%d]"
804                                                       " login device\n",
805                                                       ha->host_no, __func__,
806                                                       ddb_entry->fw_ddb_index));
807                                 } else
808                                         atomic_dec(&ddb_entry->
809                                                         retry_relogin_timer);
810                         }
811                 }
812
813                 /* Wait for relogin to timeout */
814                 if (atomic_read(&ddb_entry->relogin_timer) &&
815                     (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
816                         /*
817                          * If the relogin times out and the device is
818                          * still NOT ONLINE then try and relogin again.
819                          */
820                         if (atomic_read(&ddb_entry->state) !=
821                             DDB_STATE_ONLINE &&
822                             ddb_entry->fw_ddb_device_state ==
823                             DDB_DS_SESSION_FAILED) {
824                                 /* Reset retry relogin timer */
825                                 atomic_inc(&ddb_entry->relogin_retry_count);
826                                 DEBUG2(printk("scsi%ld: ddb [%d] relogin"
827                                               " timed out-retrying"
828                                               " relogin (%d)\n",
829                                               ha->host_no,
830                                               ddb_entry->fw_ddb_index,
831                                               atomic_read(&ddb_entry->
832                                                           relogin_retry_count))
833                                         );
834                                 start_dpc++;
835                                 DEBUG(printk("scsi%ld:%d:%d: ddb [%d] "
836                                              "initiate relogin after"
837                                              " %d seconds\n",
838                                              ha->host_no, ddb_entry->bus,
839                                              ddb_entry->target,
840                                              ddb_entry->fw_ddb_index,
841                                              ddb_entry->default_time2wait + 4)
842                                         );
843
844                                 atomic_set(&ddb_entry->retry_relogin_timer,
845                                            ddb_entry->default_time2wait + 4);
846                         }
847                 }
848         }
849
850         if (!is_qla8022(ha)) {
851                 /* Check for heartbeat interval. */
852                 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
853                     ha->heartbeat_interval != 0) {
854                         ha->seconds_since_last_heartbeat++;
855                         if (ha->seconds_since_last_heartbeat >
856                             ha->heartbeat_interval + 2)
857                                 set_bit(DPC_RESET_HA, &ha->dpc_flags);
858                 }
859         }
860
861         /* Wakeup the dpc routine for this adapter, if needed. */
862         if ((start_dpc ||
863              test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
864              test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
865              test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
866              test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
867              test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
868              test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
869              test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
870              test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
871              test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
872              test_bit(DPC_AEN, &ha->dpc_flags)) &&
873              !test_bit(AF_DPC_SCHEDULED, &ha->flags) &&
874              ha->dpc_thread) {
875                 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
876                               " - dpc flags = 0x%lx\n",
877                               ha->host_no, __func__, ha->dpc_flags));
878                 qla4xxx_wake_dpc(ha);
879         }
880
881         /* Reschedule timer thread to call us back in one second */
882         mod_timer(&ha->timer, jiffies + HZ);
883
884         DEBUG2(ha->seconds_since_last_intr++);
885 }
886
887 /**
888  * qla4xxx_cmd_wait - waits for all outstanding commands to complete
889  * @ha: Pointer to host adapter structure.
890  *
891  * This routine stalls the driver until all outstanding commands are returned.
892  * Caller must release the Hardware Lock prior to calling this routine.
893  **/
894 static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
895 {
896         uint32_t index = 0;
897         unsigned long flags;
898         struct scsi_cmnd *cmd;
899
900         unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
901
902         DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
903             "complete\n", WAIT_CMD_TOV));
904
905         while (!time_after_eq(jiffies, wtime)) {
906                 spin_lock_irqsave(&ha->hardware_lock, flags);
907                 /* Find a command that hasn't completed. */
908                 for (index = 0; index < ha->host->can_queue; index++) {
909                         cmd = scsi_host_find_tag(ha->host, index);
910                         /*
911                          * We cannot just check if the index is valid,
912                          * becase if we are run from the scsi eh, then
913                          * the scsi/block layer is going to prevent
914                          * the tag from being released.
915                          */
916                         if (cmd != NULL && CMD_SP(cmd))
917                                 break;
918                 }
919                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
920
921                 /* If No Commands are pending, wait is complete */
922                 if (index == ha->host->can_queue)
923                         return QLA_SUCCESS;
924
925                 msleep(1000);
926         }
927         /* If we timed out on waiting for commands to come back
928          * return ERROR. */
929         return QLA_ERROR;
930 }
931
932 int qla4xxx_hw_reset(struct scsi_qla_host *ha)
933 {
934         uint32_t ctrl_status;
935         unsigned long flags = 0;
936
937         DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
938
939         if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
940                 return QLA_ERROR;
941
942         spin_lock_irqsave(&ha->hardware_lock, flags);
943
944         /*
945          * If the SCSI Reset Interrupt bit is set, clear it.
946          * Otherwise, the Soft Reset won't work.
947          */
948         ctrl_status = readw(&ha->reg->ctrl_status);
949         if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
950                 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
951
952         /* Issue Soft Reset */
953         writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
954         readl(&ha->reg->ctrl_status);
955
956         spin_unlock_irqrestore(&ha->hardware_lock, flags);
957         return QLA_SUCCESS;
958 }
959
960 /**
961  * qla4xxx_soft_reset - performs soft reset.
962  * @ha: Pointer to host adapter structure.
963  **/
964 int qla4xxx_soft_reset(struct scsi_qla_host *ha)
965 {
966         uint32_t max_wait_time;
967         unsigned long flags = 0;
968         int status;
969         uint32_t ctrl_status;
970
971         status = qla4xxx_hw_reset(ha);
972         if (status != QLA_SUCCESS)
973                 return status;
974
975         status = QLA_ERROR;
976         /* Wait until the Network Reset Intr bit is cleared */
977         max_wait_time = RESET_INTR_TOV;
978         do {
979                 spin_lock_irqsave(&ha->hardware_lock, flags);
980                 ctrl_status = readw(&ha->reg->ctrl_status);
981                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
982
983                 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
984                         break;
985
986                 msleep(1000);
987         } while ((--max_wait_time));
988
989         if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
990                 DEBUG2(printk(KERN_WARNING
991                               "scsi%ld: Network Reset Intr not cleared by "
992                               "Network function, clearing it now!\n",
993                               ha->host_no));
994                 spin_lock_irqsave(&ha->hardware_lock, flags);
995                 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
996                 readl(&ha->reg->ctrl_status);
997                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
998         }
999
1000         /* Wait until the firmware tells us the Soft Reset is done */
1001         max_wait_time = SOFT_RESET_TOV;
1002         do {
1003                 spin_lock_irqsave(&ha->hardware_lock, flags);
1004                 ctrl_status = readw(&ha->reg->ctrl_status);
1005                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1006
1007                 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
1008                         status = QLA_SUCCESS;
1009                         break;
1010                 }
1011
1012                 msleep(1000);
1013         } while ((--max_wait_time));
1014
1015         /*
1016          * Also, make sure that the SCSI Reset Interrupt bit has been cleared
1017          * after the soft reset has taken place.
1018          */
1019         spin_lock_irqsave(&ha->hardware_lock, flags);
1020         ctrl_status = readw(&ha->reg->ctrl_status);
1021         if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
1022                 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
1023                 readl(&ha->reg->ctrl_status);
1024         }
1025         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1026
1027         /* If soft reset fails then most probably the bios on other
1028          * function is also enabled.
1029          * Since the initialization is sequential the other fn
1030          * wont be able to acknowledge the soft reset.
1031          * Issue a force soft reset to workaround this scenario.
1032          */
1033         if (max_wait_time == 0) {
1034                 /* Issue Force Soft Reset */
1035                 spin_lock_irqsave(&ha->hardware_lock, flags);
1036                 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
1037                 readl(&ha->reg->ctrl_status);
1038                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1039                 /* Wait until the firmware tells us the Soft Reset is done */
1040                 max_wait_time = SOFT_RESET_TOV;
1041                 do {
1042                         spin_lock_irqsave(&ha->hardware_lock, flags);
1043                         ctrl_status = readw(&ha->reg->ctrl_status);
1044                         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1045
1046                         if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
1047                                 status = QLA_SUCCESS;
1048                                 break;
1049                         }
1050
1051                         msleep(1000);
1052                 } while ((--max_wait_time));
1053         }
1054
1055         return status;
1056 }
1057
1058 /**
1059  * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
1060  * @ha: Pointer to host adapter structure.
1061  * @res: returned scsi status
1062  *
1063  * This routine is called just prior to a HARD RESET to return all
1064  * outstanding commands back to the Operating System.
1065  * Caller should make sure that the following locks are released
1066  * before this calling routine: Hardware lock, and io_request_lock.
1067  **/
1068 static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
1069 {
1070         struct srb *srb;
1071         int i;
1072         unsigned long flags;
1073
1074         spin_lock_irqsave(&ha->hardware_lock, flags);
1075         for (i = 0; i < ha->host->can_queue; i++) {
1076                 srb = qla4xxx_del_from_active_array(ha, i);
1077                 if (srb != NULL) {
1078                         srb->cmd->result = res;
1079                         kref_put(&srb->srb_ref, qla4xxx_srb_compl);
1080                 }
1081         }
1082         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1083 }
1084
1085 void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
1086 {
1087         clear_bit(AF_ONLINE, &ha->flags);
1088
1089         /* Disable the board */
1090         ql4_printk(KERN_INFO, ha, "Disabling the board\n");
1091
1092         qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
1093         qla4xxx_mark_all_devices_missing(ha);
1094         clear_bit(AF_INIT_DONE, &ha->flags);
1095 }
1096
1097 /**
1098  * qla4xxx_recover_adapter - recovers adapter after a fatal error
1099  * @ha: Pointer to host adapter structure.
1100  **/
1101 static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
1102 {
1103         int status = QLA_ERROR;
1104         uint8_t reset_chip = 0;
1105
1106         /* Stall incoming I/O until we are done */
1107         scsi_block_requests(ha->host);
1108         clear_bit(AF_ONLINE, &ha->flags);
1109
1110         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
1111
1112         set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
1113
1114         if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
1115                 reset_chip = 1;
1116
1117         /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
1118          * do not reset adapter, jump to initialize_adapter */
1119         if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1120                 status = QLA_SUCCESS;
1121                 goto recover_ha_init_adapter;
1122         }
1123
1124         /* For the ISP-82xx adapter, issue a stop_firmware if invoked
1125          * from eh_host_reset or ioctl module */
1126         if (is_qla8022(ha) && !reset_chip &&
1127             test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
1128
1129                 DEBUG2(ql4_printk(KERN_INFO, ha,
1130                     "scsi%ld: %s - Performing stop_firmware...\n",
1131                     ha->host_no, __func__));
1132                 status = ha->isp_ops->reset_firmware(ha);
1133                 if (status == QLA_SUCCESS) {
1134                         if (!test_bit(AF_FW_RECOVERY, &ha->flags))
1135                                 qla4xxx_cmd_wait(ha);
1136                         ha->isp_ops->disable_intrs(ha);
1137                         qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1138                         qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1139                 } else {
1140                         /* If the stop_firmware fails then
1141                          * reset the entire chip */
1142                         reset_chip = 1;
1143                         clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1144                         set_bit(DPC_RESET_HA, &ha->dpc_flags);
1145                 }
1146         }
1147
1148         /* Issue full chip reset if recovering from a catastrophic error,
1149          * or if stop_firmware fails for ISP-82xx.
1150          * This is the default case for ISP-4xxx */
1151         if (!is_qla8022(ha) || reset_chip) {
1152                 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
1153                         qla4xxx_cmd_wait(ha);
1154                 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1155                 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1156                 DEBUG2(ql4_printk(KERN_INFO, ha,
1157                     "scsi%ld: %s - Performing chip reset..\n",
1158                     ha->host_no, __func__));
1159                 status = ha->isp_ops->reset_chip(ha);
1160         }
1161
1162         /* Flush any pending ddb changed AENs */
1163         qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1164
1165 recover_ha_init_adapter:
1166         /* Upon successful firmware/chip reset, re-initialize the adapter */
1167         if (status == QLA_SUCCESS) {
1168                 /* For ISP-4xxx, force function 1 to always initialize
1169                  * before function 3 to prevent both funcions from
1170                  * stepping on top of the other */
1171                 if (!is_qla8022(ha) && (ha->mac_index == 3))
1172                         ssleep(6);
1173
1174                 /* NOTE: AF_ONLINE flag set upon successful completion of
1175                  *       qla4xxx_initialize_adapter */
1176                 status = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
1177         }
1178
1179         /* Retry failed adapter initialization, if necessary
1180          * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
1181          * case to prevent ping-pong resets between functions */
1182         if (!test_bit(AF_ONLINE, &ha->flags) &&
1183             !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1184                 /* Adapter initialization failed, see if we can retry
1185                  * resetting the ha.
1186                  * Since we don't want to block the DPC for too long
1187                  * with multiple resets in the same thread,
1188                  * utilize DPC to retry */
1189                 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
1190                         ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
1191                         DEBUG2(printk("scsi%ld: recover adapter - retrying "
1192                                       "(%d) more times\n", ha->host_no,
1193                                       ha->retry_reset_ha_cnt));
1194                         set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1195                         status = QLA_ERROR;
1196                 } else {
1197                         if (ha->retry_reset_ha_cnt > 0) {
1198                                 /* Schedule another Reset HA--DPC will retry */
1199                                 ha->retry_reset_ha_cnt--;
1200                                 DEBUG2(printk("scsi%ld: recover adapter - "
1201                                               "retry remaining %d\n",
1202                                               ha->host_no,
1203                                               ha->retry_reset_ha_cnt));
1204                                 status = QLA_ERROR;
1205                         }
1206
1207                         if (ha->retry_reset_ha_cnt == 0) {
1208                                 /* Recover adapter retries have been exhausted.
1209                                  * Adapter DEAD */
1210                                 DEBUG2(printk("scsi%ld: recover adapter "
1211                                               "failed - board disabled\n",
1212                                               ha->host_no));
1213                                 qla4xxx_dead_adapter_cleanup(ha);
1214                                 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1215                                 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
1216                                 clear_bit(DPC_RESET_HA_FW_CONTEXT,
1217                                           &ha->dpc_flags);
1218                                 status = QLA_ERROR;
1219                         }
1220                 }
1221         } else {
1222                 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
1223                 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1224                 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1225         }
1226
1227         ha->adapter_error_count++;
1228
1229         if (test_bit(AF_ONLINE, &ha->flags))
1230                 ha->isp_ops->enable_intrs(ha);
1231
1232         scsi_unblock_requests(ha->host);
1233
1234         clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
1235         DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
1236             status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
1237
1238         return status;
1239 }
1240
1241 static void qla4xxx_relogin_all_devices(struct scsi_qla_host *ha)
1242 {
1243         struct ddb_entry *ddb_entry, *dtemp;
1244
1245         list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
1246                 if ((atomic_read(&ddb_entry->state) == DDB_STATE_MISSING) ||
1247                     (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD)) {
1248                         if (ddb_entry->fw_ddb_device_state ==
1249                             DDB_DS_SESSION_ACTIVE) {
1250                                 atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
1251                                 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
1252                                     " marked ONLINE\n", ha->host_no, __func__,
1253                                     ddb_entry->fw_ddb_index);
1254
1255                                 iscsi_unblock_session(ddb_entry->sess);
1256                         } else
1257                                 qla4xxx_relogin_device(ha, ddb_entry);
1258                 }
1259         }
1260 }
1261
1262 void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
1263 {
1264         if (ha->dpc_thread &&
1265             !test_bit(AF_DPC_SCHEDULED, &ha->flags)) {
1266                 set_bit(AF_DPC_SCHEDULED, &ha->flags);
1267                 queue_work(ha->dpc_thread, &ha->dpc_work);
1268         }
1269 }
1270
1271 /**
1272  * qla4xxx_do_dpc - dpc routine
1273  * @data: in our case pointer to adapter structure
1274  *
1275  * This routine is a task that is schedule by the interrupt handler
1276  * to perform the background processing for interrupts.  We put it
1277  * on a task queue that is consumed whenever the scheduler runs; that's
1278  * so you can do anything (i.e. put the process to sleep etc).  In fact,
1279  * the mid-level tries to sleep when it reaches the driver threshold
1280  * "host->can_queue". This can cause a panic if we were in our interrupt code.
1281  **/
1282 static void qla4xxx_do_dpc(struct work_struct *work)
1283 {
1284         struct scsi_qla_host *ha =
1285                 container_of(work, struct scsi_qla_host, dpc_work);
1286         struct ddb_entry *ddb_entry, *dtemp;
1287         int status = QLA_ERROR;
1288
1289         DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
1290             "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
1291             ha->host_no, __func__, ha->flags, ha->dpc_flags))
1292
1293         /* Initialization not yet finished. Don't do anything yet. */
1294         if (!test_bit(AF_INIT_DONE, &ha->flags))
1295                 goto do_dpc_exit;
1296
1297         if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1298                 DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
1299                     ha->host_no, __func__, ha->flags));
1300                 goto do_dpc_exit;
1301         }
1302
1303         if (is_qla8022(ha)) {
1304                 if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
1305                         qla4_8xxx_idc_lock(ha);
1306                         qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
1307                             QLA82XX_DEV_FAILED);
1308                         qla4_8xxx_idc_unlock(ha);
1309                         ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
1310                         qla4_8xxx_device_state_handler(ha);
1311                 }
1312                 if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
1313                         qla4_8xxx_need_qsnt_handler(ha);
1314                 }
1315         }
1316
1317         if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
1318             (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1319             test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1320             test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
1321                 if (ql4xdontresethba) {
1322                         DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
1323                             ha->host_no, __func__));
1324                         clear_bit(DPC_RESET_HA, &ha->dpc_flags);
1325                         clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1326                         clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1327                         goto dpc_post_reset_ha;
1328                 }
1329                 if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
1330                     test_bit(DPC_RESET_HA, &ha->dpc_flags))
1331                         qla4xxx_recover_adapter(ha);
1332
1333                 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1334                         uint8_t wait_time = RESET_INTR_TOV;
1335
1336                         while ((readw(&ha->reg->ctrl_status) &
1337                                 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
1338                                 if (--wait_time == 0)
1339                                         break;
1340                                 msleep(1000);
1341                         }
1342                         if (wait_time == 0)
1343                                 DEBUG2(printk("scsi%ld: %s: SR|FSR "
1344                                               "bit not cleared-- resetting\n",
1345                                               ha->host_no, __func__));
1346                         qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1347                         if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
1348                                 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1349                                 status = qla4xxx_recover_adapter(ha);
1350                         }
1351                         clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1352                         if (status == QLA_SUCCESS)
1353                                 ha->isp_ops->enable_intrs(ha);
1354                 }
1355         }
1356
1357 dpc_post_reset_ha:
1358         /* ---- process AEN? --- */
1359         if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
1360                 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
1361
1362         /* ---- Get DHCP IP Address? --- */
1363         if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
1364                 qla4xxx_get_dhcp_ip_address(ha);
1365
1366         /* ---- link change? --- */
1367         if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
1368                 if (!test_bit(AF_LINK_UP, &ha->flags)) {
1369                         /* ---- link down? --- */
1370                         qla4xxx_mark_all_devices_missing(ha);
1371                 } else {
1372                         /* ---- link up? --- *
1373                          * F/W will auto login to all devices ONLY ONCE after
1374                          * link up during driver initialization and runtime
1375                          * fatal error recovery.  Therefore, the driver must
1376                          * manually relogin to devices when recovering from
1377                          * connection failures, logouts, expired KATO, etc. */
1378
1379                         qla4xxx_relogin_all_devices(ha);
1380                 }
1381         }
1382
1383         /* ---- relogin device? --- */
1384         if (adapter_up(ha) &&
1385             test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
1386                 list_for_each_entry_safe(ddb_entry, dtemp,
1387                                          &ha->ddb_list, list) {
1388                         if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
1389                             atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
1390                                 qla4xxx_relogin_device(ha, ddb_entry);
1391
1392                         /*
1393                          * If mbx cmd times out there is no point
1394                          * in continuing further.
1395                          * With large no of targets this can hang
1396                          * the system.
1397                          */
1398                         if (test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
1399                                 printk(KERN_WARNING "scsi%ld: %s: "
1400                                        "need to reset hba\n",
1401                                        ha->host_no, __func__);
1402                                 break;
1403                         }
1404                 }
1405         }
1406
1407 do_dpc_exit:
1408         clear_bit(AF_DPC_SCHEDULED, &ha->flags);
1409 }
1410
1411 /**
1412  * qla4xxx_free_adapter - release the adapter
1413  * @ha: pointer to adapter structure
1414  **/
1415 static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
1416 {
1417
1418         if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
1419                 /* Turn-off interrupts on the card. */
1420                 ha->isp_ops->disable_intrs(ha);
1421         }
1422
1423         /* Remove timer thread, if present */
1424         if (ha->timer_active)
1425                 qla4xxx_stop_timer(ha);
1426
1427         /* Kill the kernel thread for this host */
1428         if (ha->dpc_thread)
1429                 destroy_workqueue(ha->dpc_thread);
1430
1431         /* Put firmware in known state */
1432         ha->isp_ops->reset_firmware(ha);
1433
1434         if (is_qla8022(ha)) {
1435                 qla4_8xxx_idc_lock(ha);
1436                 qla4_8xxx_clear_drv_active(ha);
1437                 qla4_8xxx_idc_unlock(ha);
1438         }
1439
1440         /* Detach interrupts */
1441         if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
1442                 qla4xxx_free_irqs(ha);
1443
1444         /* free extra memory */
1445         qla4xxx_mem_free(ha);
1446 }
1447
1448 int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
1449 {
1450         int status = 0;
1451         uint8_t revision_id;
1452         unsigned long mem_base, mem_len, db_base, db_len;
1453         struct pci_dev *pdev = ha->pdev;
1454
1455         status = pci_request_regions(pdev, DRIVER_NAME);
1456         if (status) {
1457                 printk(KERN_WARNING
1458                     "scsi(%ld) Failed to reserve PIO regions (%s) "
1459                     "status=%d\n", ha->host_no, pci_name(pdev), status);
1460                 goto iospace_error_exit;
1461         }
1462
1463         pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
1464         DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
1465             __func__, revision_id));
1466         ha->revision_id = revision_id;
1467
1468         /* remap phys address */
1469         mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
1470         mem_len = pci_resource_len(pdev, 0);
1471         DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
1472             __func__, mem_base, mem_len));
1473
1474         /* mapping of pcibase pointer */
1475         ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
1476         if (!ha->nx_pcibase) {
1477                 printk(KERN_ERR
1478                     "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
1479                 pci_release_regions(ha->pdev);
1480                 goto iospace_error_exit;
1481         }
1482
1483         /* Mapping of IO base pointer, door bell read and write pointer */
1484
1485         /* mapping of IO base pointer */
1486         ha->qla4_8xxx_reg =
1487             (struct device_reg_82xx  __iomem *)((uint8_t *)ha->nx_pcibase +
1488             0xbc000 + (ha->pdev->devfn << 11));
1489
1490         db_base = pci_resource_start(pdev, 4);  /* doorbell is on bar 4 */
1491         db_len = pci_resource_len(pdev, 4);
1492
1493         ha->nx_db_wr_ptr = (ha->pdev->devfn == 4 ? QLA82XX_CAM_RAM_DB1 :
1494             QLA82XX_CAM_RAM_DB2);
1495
1496         return 0;
1497 iospace_error_exit:
1498         return -ENOMEM;
1499 }
1500
1501 /***
1502  * qla4xxx_iospace_config - maps registers
1503  * @ha: pointer to adapter structure
1504  *
1505  * This routines maps HBA's registers from the pci address space
1506  * into the kernel virtual address space for memory mapped i/o.
1507  **/
1508 int qla4xxx_iospace_config(struct scsi_qla_host *ha)
1509 {
1510         unsigned long pio, pio_len, pio_flags;
1511         unsigned long mmio, mmio_len, mmio_flags;
1512
1513         pio = pci_resource_start(ha->pdev, 0);
1514         pio_len = pci_resource_len(ha->pdev, 0);
1515         pio_flags = pci_resource_flags(ha->pdev, 0);
1516         if (pio_flags & IORESOURCE_IO) {
1517                 if (pio_len < MIN_IOBASE_LEN) {
1518                         ql4_printk(KERN_WARNING, ha,
1519                                 "Invalid PCI I/O region size\n");
1520                         pio = 0;
1521                 }
1522         } else {
1523                 ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
1524                 pio = 0;
1525         }
1526
1527         /* Use MMIO operations for all accesses. */
1528         mmio = pci_resource_start(ha->pdev, 1);
1529         mmio_len = pci_resource_len(ha->pdev, 1);
1530         mmio_flags = pci_resource_flags(ha->pdev, 1);
1531
1532         if (!(mmio_flags & IORESOURCE_MEM)) {
1533                 ql4_printk(KERN_ERR, ha,
1534                     "region #0 not an MMIO resource, aborting\n");
1535
1536                 goto iospace_error_exit;
1537         }
1538
1539         if (mmio_len < MIN_IOBASE_LEN) {
1540                 ql4_printk(KERN_ERR, ha,
1541                     "Invalid PCI mem region size, aborting\n");
1542                 goto iospace_error_exit;
1543         }
1544
1545         if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
1546                 ql4_printk(KERN_WARNING, ha,
1547                     "Failed to reserve PIO/MMIO regions\n");
1548
1549                 goto iospace_error_exit;
1550         }
1551
1552         ha->pio_address = pio;
1553         ha->pio_length = pio_len;
1554         ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
1555         if (!ha->reg) {
1556                 ql4_printk(KERN_ERR, ha,
1557                     "cannot remap MMIO, aborting\n");
1558
1559                 goto iospace_error_exit;
1560         }
1561
1562         return 0;
1563
1564 iospace_error_exit:
1565         return -ENOMEM;
1566 }
1567
1568 static struct isp_operations qla4xxx_isp_ops = {
1569         .iospace_config         = qla4xxx_iospace_config,
1570         .pci_config             = qla4xxx_pci_config,
1571         .disable_intrs          = qla4xxx_disable_intrs,
1572         .enable_intrs           = qla4xxx_enable_intrs,
1573         .start_firmware         = qla4xxx_start_firmware,
1574         .intr_handler           = qla4xxx_intr_handler,
1575         .interrupt_service_routine = qla4xxx_interrupt_service_routine,
1576         .reset_chip             = qla4xxx_soft_reset,
1577         .reset_firmware         = qla4xxx_hw_reset,
1578         .queue_iocb             = qla4xxx_queue_iocb,
1579         .complete_iocb          = qla4xxx_complete_iocb,
1580         .rd_shdw_req_q_out      = qla4xxx_rd_shdw_req_q_out,
1581         .rd_shdw_rsp_q_in       = qla4xxx_rd_shdw_rsp_q_in,
1582         .get_sys_info           = qla4xxx_get_sys_info,
1583 };
1584
1585 static struct isp_operations qla4_8xxx_isp_ops = {
1586         .iospace_config         = qla4_8xxx_iospace_config,
1587         .pci_config             = qla4_8xxx_pci_config,
1588         .disable_intrs          = qla4_8xxx_disable_intrs,
1589         .enable_intrs           = qla4_8xxx_enable_intrs,
1590         .start_firmware         = qla4_8xxx_load_risc,
1591         .intr_handler           = qla4_8xxx_intr_handler,
1592         .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
1593         .reset_chip             = qla4_8xxx_isp_reset,
1594         .reset_firmware         = qla4_8xxx_stop_firmware,
1595         .queue_iocb             = qla4_8xxx_queue_iocb,
1596         .complete_iocb          = qla4_8xxx_complete_iocb,
1597         .rd_shdw_req_q_out      = qla4_8xxx_rd_shdw_req_q_out,
1598         .rd_shdw_rsp_q_in       = qla4_8xxx_rd_shdw_rsp_q_in,
1599         .get_sys_info           = qla4_8xxx_get_sys_info,
1600 };
1601
1602 uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
1603 {
1604         return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
1605 }
1606
1607 uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
1608 {
1609         return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
1610 }
1611
1612 uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
1613 {
1614         return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
1615 }
1616
1617 uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
1618 {
1619         return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
1620 }
1621
1622 /**
1623  * qla4xxx_probe_adapter - callback function to probe HBA
1624  * @pdev: pointer to pci_dev structure
1625  * @pci_device_id: pointer to pci_device entry
1626  *
1627  * This routine will probe for Qlogic 4xxx iSCSI host adapters.
1628  * It returns zero if successful. It also initializes all data necessary for
1629  * the driver.
1630  **/
1631 static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
1632                                            const struct pci_device_id *ent)
1633 {
1634         int ret = -ENODEV, status;
1635         struct Scsi_Host *host;
1636         struct scsi_qla_host *ha;
1637         uint8_t init_retry_count = 0;
1638         char buf[34];
1639         struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
1640         uint32_t dev_state;
1641
1642         if (pci_enable_device(pdev))
1643                 return -1;
1644
1645         host = scsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha));
1646         if (host == NULL) {
1647                 printk(KERN_WARNING
1648                        "qla4xxx: Couldn't allocate host from scsi layer!\n");
1649                 goto probe_disable_device;
1650         }
1651
1652         /* Clear our data area */
1653         ha = (struct scsi_qla_host *) host->hostdata;
1654         memset(ha, 0, sizeof(*ha));
1655
1656         /* Save the information from PCI BIOS.  */
1657         ha->pdev = pdev;
1658         ha->host = host;
1659         ha->host_no = host->host_no;
1660
1661         pci_enable_pcie_error_reporting(pdev);
1662
1663         /* Setup Runtime configurable options */
1664         if (is_qla8022(ha)) {
1665                 ha->isp_ops = &qla4_8xxx_isp_ops;
1666                 rwlock_init(&ha->hw_lock);
1667                 ha->qdr_sn_window = -1;
1668                 ha->ddr_mn_window = -1;
1669                 ha->curr_window = 255;
1670                 ha->func_num = PCI_FUNC(ha->pdev->devfn);
1671                 nx_legacy_intr = &legacy_intr[ha->func_num];
1672                 ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
1673                 ha->nx_legacy_intr.tgt_status_reg =
1674                         nx_legacy_intr->tgt_status_reg;
1675                 ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
1676                 ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
1677         } else {
1678                 ha->isp_ops = &qla4xxx_isp_ops;
1679         }
1680
1681         /* Set EEH reset type to fundamental if required by hba */
1682         if (is_qla8022(ha))
1683                 pdev->needs_freset = 1;
1684
1685         /* Configure PCI I/O space. */
1686         ret = ha->isp_ops->iospace_config(ha);
1687         if (ret)
1688                 goto probe_failed_ioconfig;
1689
1690         ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
1691                    pdev->device, pdev->irq, ha->reg);
1692
1693         qla4xxx_config_dma_addressing(ha);
1694
1695         /* Initialize lists and spinlocks. */
1696         INIT_LIST_HEAD(&ha->ddb_list);
1697         INIT_LIST_HEAD(&ha->free_srb_q);
1698
1699         mutex_init(&ha->mbox_sem);
1700         init_completion(&ha->mbx_intr_comp);
1701
1702         spin_lock_init(&ha->hardware_lock);
1703
1704         /* Allocate dma buffers */
1705         if (qla4xxx_mem_alloc(ha)) {
1706                 ql4_printk(KERN_WARNING, ha,
1707                     "[ERROR] Failed to allocate memory for adapter\n");
1708
1709                 ret = -ENOMEM;
1710                 goto probe_failed;
1711         }
1712
1713         if (is_qla8022(ha))
1714                 (void) qla4_8xxx_get_flash_info(ha);
1715
1716         /*
1717          * Initialize the Host adapter request/response queues and
1718          * firmware
1719          * NOTE: interrupts enabled upon successful completion
1720          */
1721         status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1722         while ((!test_bit(AF_ONLINE, &ha->flags)) &&
1723             init_retry_count++ < MAX_INIT_RETRIES) {
1724
1725                 if (is_qla8022(ha)) {
1726                         qla4_8xxx_idc_lock(ha);
1727                         dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
1728                         qla4_8xxx_idc_unlock(ha);
1729                         if (dev_state == QLA82XX_DEV_FAILED) {
1730                                 ql4_printk(KERN_WARNING, ha, "%s: don't retry "
1731                                     "initialize adapter. H/W is in failed state\n",
1732                                     __func__);
1733                                 break;
1734                         }
1735                 }
1736                 DEBUG2(printk("scsi: %s: retrying adapter initialization "
1737                               "(%d)\n", __func__, init_retry_count));
1738
1739                 if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
1740                         continue;
1741
1742                 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1743         }
1744
1745         if (!test_bit(AF_ONLINE, &ha->flags)) {
1746                 ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
1747
1748                 if (is_qla8022(ha) && ql4xdontresethba) {
1749                         /* Put the device in failed state. */
1750                         DEBUG2(printk(KERN_ERR "HW STATE: FAILED\n"));
1751                         qla4_8xxx_idc_lock(ha);
1752                         qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
1753                             QLA82XX_DEV_FAILED);
1754                         qla4_8xxx_idc_unlock(ha);
1755                 }
1756                 ret = -ENODEV;
1757                 goto probe_failed;
1758         }
1759
1760         host->cmd_per_lun = 3;
1761         host->max_channel = 0;
1762         host->max_lun = MAX_LUNS - 1;
1763         host->max_id = MAX_TARGETS;
1764         host->max_cmd_len = IOCB_MAX_CDB_LEN;
1765         host->can_queue = MAX_SRBS ;
1766         host->transportt = qla4xxx_scsi_transport;
1767
1768         ret = scsi_init_shared_tag_map(host, MAX_SRBS);
1769         if (ret) {
1770                 ql4_printk(KERN_WARNING, ha,
1771                     "scsi_init_shared_tag_map failed\n");
1772                 goto probe_failed;
1773         }
1774
1775         /* Startup the kernel thread for this host adapter. */
1776         DEBUG2(printk("scsi: %s: Starting kernel thread for "
1777                       "qla4xxx_dpc\n", __func__));
1778         sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
1779         ha->dpc_thread = create_singlethread_workqueue(buf);
1780         if (!ha->dpc_thread) {
1781                 ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
1782                 ret = -ENODEV;
1783                 goto probe_failed;
1784         }
1785         INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
1786
1787         /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
1788          * (which is called indirectly by qla4xxx_initialize_adapter),
1789          * so that irqs will be registered after crbinit but before
1790          * mbx_intr_enable.
1791          */
1792         if (!is_qla8022(ha)) {
1793                 ret = qla4xxx_request_irqs(ha);
1794                 if (ret) {
1795                         ql4_printk(KERN_WARNING, ha, "Failed to reserve "
1796                             "interrupt %d already in use.\n", pdev->irq);
1797                         goto probe_failed;
1798                 }
1799         }
1800
1801         pci_save_state(ha->pdev);
1802         ha->isp_ops->enable_intrs(ha);
1803
1804         /* Start timer thread. */
1805         qla4xxx_start_timer(ha, qla4xxx_timer, 1);
1806
1807         set_bit(AF_INIT_DONE, &ha->flags);
1808
1809         pci_set_drvdata(pdev, ha);
1810
1811         ret = scsi_add_host(host, &pdev->dev);
1812         if (ret)
1813                 goto probe_failed;
1814
1815         printk(KERN_INFO
1816                " QLogic iSCSI HBA Driver version: %s\n"
1817                "  QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
1818                qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
1819                ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
1820                ha->patch_number, ha->build_number);
1821         scsi_scan_host(host);
1822         return 0;
1823
1824 probe_failed:
1825         qla4xxx_free_adapter(ha);
1826
1827 probe_failed_ioconfig:
1828         pci_disable_pcie_error_reporting(pdev);
1829         scsi_host_put(ha->host);
1830
1831 probe_disable_device:
1832         pci_disable_device(pdev);
1833
1834         return ret;
1835 }
1836
1837 /**
1838  * qla4xxx_prevent_other_port_reinit - prevent other port from re-initialize
1839  * @ha: pointer to adapter structure
1840  *
1841  * Mark the other ISP-4xxx port to indicate that the driver is being removed,
1842  * so that the other port will not re-initialize while in the process of
1843  * removing the ha due to driver unload or hba hotplug.
1844  **/
1845 static void qla4xxx_prevent_other_port_reinit(struct scsi_qla_host *ha)
1846 {
1847         struct scsi_qla_host *other_ha = NULL;
1848         struct pci_dev *other_pdev = NULL;
1849         int fn = ISP4XXX_PCI_FN_2;
1850
1851         /*iscsi function numbers for ISP4xxx is 1 and 3*/
1852         if (PCI_FUNC(ha->pdev->devfn) & BIT_1)
1853                 fn = ISP4XXX_PCI_FN_1;
1854
1855         other_pdev =
1856                 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
1857                 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
1858                 fn));
1859
1860         /* Get other_ha if other_pdev is valid and state is enable*/
1861         if (other_pdev) {
1862                 if (atomic_read(&other_pdev->enable_cnt)) {
1863                         other_ha = pci_get_drvdata(other_pdev);
1864                         if (other_ha) {
1865                                 set_bit(AF_HA_REMOVAL, &other_ha->flags);
1866                                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: "
1867                                     "Prevent %s reinit\n", __func__,
1868                                     dev_name(&other_ha->pdev->dev)));
1869                         }
1870                 }
1871                 pci_dev_put(other_pdev);
1872         }
1873 }
1874
1875 /**
1876  * qla4xxx_remove_adapter - calback function to remove adapter.
1877  * @pci_dev: PCI device pointer
1878  **/
1879 static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
1880 {
1881         struct scsi_qla_host *ha;
1882
1883         ha = pci_get_drvdata(pdev);
1884
1885         if (!is_qla8022(ha))
1886                 qla4xxx_prevent_other_port_reinit(ha);
1887
1888         /* remove devs from iscsi_sessions to scsi_devices */
1889         qla4xxx_free_ddb_list(ha);
1890
1891         scsi_remove_host(ha->host);
1892
1893         qla4xxx_free_adapter(ha);
1894
1895         scsi_host_put(ha->host);
1896
1897         pci_disable_pcie_error_reporting(pdev);
1898         pci_disable_device(pdev);
1899         pci_set_drvdata(pdev, NULL);
1900 }
1901
1902 /**
1903  * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
1904  * @ha: HA context
1905  *
1906  * At exit, the @ha's flags.enable_64bit_addressing set to indicated
1907  * supported addressing method.
1908  */
1909 static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
1910 {
1911         int retval;
1912
1913         /* Update our PCI device dma_mask for full 64 bit mask */
1914         if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
1915                 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
1916                         dev_dbg(&ha->pdev->dev,
1917                                   "Failed to set 64 bit PCI consistent mask; "
1918                                    "using 32 bit.\n");
1919                         retval = pci_set_consistent_dma_mask(ha->pdev,
1920                                                              DMA_BIT_MASK(32));
1921                 }
1922         } else
1923                 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
1924 }
1925
1926 static int qla4xxx_slave_alloc(struct scsi_device *sdev)
1927 {
1928         struct iscsi_cls_session *sess = starget_to_session(sdev->sdev_target);
1929         struct ddb_entry *ddb = sess->dd_data;
1930         int queue_depth = QL4_DEF_QDEPTH;
1931
1932         sdev->hostdata = ddb;
1933         sdev->tagged_supported = 1;
1934
1935         if (ql4xmaxqdepth != 0 && ql4xmaxqdepth <= 0xffffU)
1936                 queue_depth = ql4xmaxqdepth;
1937
1938         scsi_activate_tcq(sdev, queue_depth);
1939         return 0;
1940 }
1941
1942 static int qla4xxx_slave_configure(struct scsi_device *sdev)
1943 {
1944         sdev->tagged_supported = 1;
1945         return 0;
1946 }
1947
1948 static void qla4xxx_slave_destroy(struct scsi_device *sdev)
1949 {
1950         scsi_deactivate_tcq(sdev, 1);
1951 }
1952
1953 /**
1954  * qla4xxx_del_from_active_array - returns an active srb
1955  * @ha: Pointer to host adapter structure.
1956  * @index: index into the active_array
1957  *
1958  * This routine removes and returns the srb at the specified index
1959  **/
1960 struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
1961     uint32_t index)
1962 {
1963         struct srb *srb = NULL;
1964         struct scsi_cmnd *cmd = NULL;
1965
1966         cmd = scsi_host_find_tag(ha->host, index);
1967         if (!cmd)
1968                 return srb;
1969
1970         srb = (struct srb *)CMD_SP(cmd);
1971         if (!srb)
1972                 return srb;
1973
1974         /* update counters */
1975         if (srb->flags & SRB_DMA_VALID) {
1976                 ha->req_q_count += srb->iocb_cnt;
1977                 ha->iocb_cnt -= srb->iocb_cnt;
1978                 if (srb->cmd)
1979                         srb->cmd->host_scribble =
1980                                 (unsigned char *)(unsigned long) MAX_SRBS;
1981         }
1982         return srb;
1983 }
1984
1985 /**
1986  * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
1987  * @ha: Pointer to host adapter structure.
1988  * @cmd: Scsi Command to wait on.
1989  *
1990  * This routine waits for the command to be returned by the Firmware
1991  * for some max time.
1992  **/
1993 static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
1994                                       struct scsi_cmnd *cmd)
1995 {
1996         int done = 0;
1997         struct srb *rp;
1998         uint32_t max_wait_time = EH_WAIT_CMD_TOV;
1999         int ret = SUCCESS;
2000
2001         /* Dont wait on command if PCI error is being handled
2002          * by PCI AER driver
2003          */
2004         if (unlikely(pci_channel_offline(ha->pdev)) ||
2005             (test_bit(AF_EEH_BUSY, &ha->flags))) {
2006                 ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
2007                     ha->host_no, __func__);
2008                 return ret;
2009         }
2010
2011         do {
2012                 /* Checking to see if its returned to OS */
2013                 rp = (struct srb *) CMD_SP(cmd);
2014                 if (rp == NULL) {
2015                         done++;
2016                         break;
2017                 }
2018
2019                 msleep(2000);
2020         } while (max_wait_time--);
2021
2022         return done;
2023 }
2024
2025 /**
2026  * qla4xxx_wait_for_hba_online - waits for HBA to come online
2027  * @ha: Pointer to host adapter structure
2028  **/
2029 static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
2030 {
2031         unsigned long wait_online;
2032
2033         wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
2034         while (time_before(jiffies, wait_online)) {
2035
2036                 if (adapter_up(ha))
2037                         return QLA_SUCCESS;
2038
2039                 msleep(2000);
2040         }
2041
2042         return QLA_ERROR;
2043 }
2044
2045 /**
2046  * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
2047  * @ha: pointer to HBA
2048  * @t: target id
2049  * @l: lun id
2050  *
2051  * This function waits for all outstanding commands to a lun to complete. It
2052  * returns 0 if all pending commands are returned and 1 otherwise.
2053  **/
2054 static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
2055                                         struct scsi_target *stgt,
2056                                         struct scsi_device *sdev)
2057 {
2058         int cnt;
2059         int status = 0;
2060         struct scsi_cmnd *cmd;
2061
2062         /*
2063          * Waiting for all commands for the designated target or dev
2064          * in the active array
2065          */
2066         for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
2067                 cmd = scsi_host_find_tag(ha->host, cnt);
2068                 if (cmd && stgt == scsi_target(cmd->device) &&
2069                     (!sdev || sdev == cmd->device)) {
2070                         if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
2071                                 status++;
2072                                 break;
2073                         }
2074                 }
2075         }
2076         return status;
2077 }
2078
2079 /**
2080  * qla4xxx_eh_abort - callback for abort task.
2081  * @cmd: Pointer to Linux's SCSI command structure
2082  *
2083  * This routine is called by the Linux OS to abort the specified
2084  * command.
2085  **/
2086 static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
2087 {
2088         struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2089         unsigned int id = cmd->device->id;
2090         unsigned int lun = cmd->device->lun;
2091         unsigned long flags;
2092         struct srb *srb = NULL;
2093         int ret = SUCCESS;
2094         int wait = 0;
2095
2096         ql4_printk(KERN_INFO, ha,
2097             "scsi%ld:%d:%d: Abort command issued cmd=%p\n",
2098             ha->host_no, id, lun, cmd);
2099
2100         spin_lock_irqsave(&ha->hardware_lock, flags);
2101         srb = (struct srb *) CMD_SP(cmd);
2102         if (!srb) {
2103                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2104                 return SUCCESS;
2105         }
2106         kref_get(&srb->srb_ref);
2107         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2108
2109         if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
2110                 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
2111                     ha->host_no, id, lun));
2112                 ret = FAILED;
2113         } else {
2114                 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
2115                     ha->host_no, id, lun));
2116                 wait = 1;
2117         }
2118
2119         kref_put(&srb->srb_ref, qla4xxx_srb_compl);
2120
2121         /* Wait for command to complete */
2122         if (wait) {
2123                 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
2124                         DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
2125                             ha->host_no, id, lun));
2126                         ret = FAILED;
2127                 }
2128         }
2129
2130         ql4_printk(KERN_INFO, ha,
2131             "scsi%ld:%d:%d: Abort command - %s\n",
2132             ha->host_no, id, lun, (ret == SUCCESS) ? "succeeded" : "failed");
2133
2134         return ret;
2135 }
2136
2137 /**
2138  * qla4xxx_eh_device_reset - callback for target reset.
2139  * @cmd: Pointer to Linux's SCSI command structure
2140  *
2141  * This routine is called by the Linux OS to reset all luns on the
2142  * specified target.
2143  **/
2144 static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
2145 {
2146         struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2147         struct ddb_entry *ddb_entry = cmd->device->hostdata;
2148         int ret = FAILED, stat;
2149
2150         if (!ddb_entry)
2151                 return ret;
2152
2153         ret = iscsi_block_scsi_eh(cmd);
2154         if (ret)
2155                 return ret;
2156         ret = FAILED;
2157
2158         ql4_printk(KERN_INFO, ha,
2159                    "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
2160                    cmd->device->channel, cmd->device->id, cmd->device->lun);
2161
2162         DEBUG2(printk(KERN_INFO
2163                       "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
2164                       "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
2165                       cmd, jiffies, cmd->request->timeout / HZ,
2166                       ha->dpc_flags, cmd->result, cmd->allowed));
2167
2168         /* FIXME: wait for hba to go online */
2169         stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
2170         if (stat != QLA_SUCCESS) {
2171                 ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
2172                 goto eh_dev_reset_done;
2173         }
2174
2175         if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2176                                          cmd->device)) {
2177                 ql4_printk(KERN_INFO, ha,
2178                            "DEVICE RESET FAILED - waiting for "
2179                            "commands.\n");
2180                 goto eh_dev_reset_done;
2181         }
2182
2183         /* Send marker. */
2184         if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2185                 MM_LUN_RESET) != QLA_SUCCESS)
2186                 goto eh_dev_reset_done;
2187
2188         ql4_printk(KERN_INFO, ha,
2189                    "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
2190                    ha->host_no, cmd->device->channel, cmd->device->id,
2191                    cmd->device->lun);
2192
2193         ret = SUCCESS;
2194
2195 eh_dev_reset_done:
2196
2197         return ret;
2198 }
2199
2200 /**
2201  * qla4xxx_eh_target_reset - callback for target reset.
2202  * @cmd: Pointer to Linux's SCSI command structure
2203  *
2204  * This routine is called by the Linux OS to reset the target.
2205  **/
2206 static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
2207 {
2208         struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2209         struct ddb_entry *ddb_entry = cmd->device->hostdata;
2210         int stat, ret;
2211
2212         if (!ddb_entry)
2213                 return FAILED;
2214
2215         ret = iscsi_block_scsi_eh(cmd);
2216         if (ret)
2217                 return ret;
2218
2219         starget_printk(KERN_INFO, scsi_target(cmd->device),
2220                        "WARM TARGET RESET ISSUED.\n");
2221
2222         DEBUG2(printk(KERN_INFO
2223                       "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
2224                       "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
2225                       ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
2226                       ha->dpc_flags, cmd->result, cmd->allowed));
2227
2228         stat = qla4xxx_reset_target(ha, ddb_entry);
2229         if (stat != QLA_SUCCESS) {
2230                 starget_printk(KERN_INFO, scsi_target(cmd->device),
2231                                "WARM TARGET RESET FAILED.\n");
2232                 return FAILED;
2233         }
2234
2235         if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2236                                          NULL)) {
2237                 starget_printk(KERN_INFO, scsi_target(cmd->device),
2238                                "WARM TARGET DEVICE RESET FAILED - "
2239                                "waiting for commands.\n");
2240                 return FAILED;
2241         }
2242
2243         /* Send marker. */
2244         if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2245                 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
2246                 starget_printk(KERN_INFO, scsi_target(cmd->device),
2247                                "WARM TARGET DEVICE RESET FAILED - "
2248                                "marker iocb failed.\n");
2249                 return FAILED;
2250         }
2251
2252         starget_printk(KERN_INFO, scsi_target(cmd->device),
2253                        "WARM TARGET RESET SUCCEEDED.\n");
2254         return SUCCESS;
2255 }
2256
2257 /**
2258  * qla4xxx_eh_host_reset - kernel callback
2259  * @cmd: Pointer to Linux's SCSI command structure
2260  *
2261  * This routine is invoked by the Linux kernel to perform fatal error
2262  * recovery on the specified adapter.
2263  **/
2264 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
2265 {
2266         int return_status = FAILED;
2267         struct scsi_qla_host *ha;
2268
2269         ha = (struct scsi_qla_host *) cmd->device->host->hostdata;
2270
2271         if (ql4xdontresethba) {
2272                 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
2273                      ha->host_no, __func__));
2274                 return FAILED;
2275         }
2276
2277         ql4_printk(KERN_INFO, ha,
2278                    "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
2279                    cmd->device->channel, cmd->device->id, cmd->device->lun);
2280
2281         if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
2282                 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host.  Adapter "
2283                               "DEAD.\n", ha->host_no, cmd->device->channel,
2284                               __func__));
2285
2286                 return FAILED;
2287         }
2288
2289         if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
2290                 if (is_qla8022(ha))
2291                         set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2292                 else
2293                         set_bit(DPC_RESET_HA, &ha->dpc_flags);
2294         }
2295
2296         if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
2297                 return_status = SUCCESS;
2298
2299         ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
2300                    return_status == FAILED ? "FAILED" : "SUCCEEDED");
2301
2302         return return_status;
2303 }
2304
2305 /* PCI AER driver recovers from all correctable errors w/o
2306  * driver intervention. For uncorrectable errors PCI AER
2307  * driver calls the following device driver's callbacks
2308  *
2309  * - Fatal Errors - link_reset
2310  * - Non-Fatal Errors - driver's pci_error_detected() which
2311  * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
2312  *
2313  * PCI AER driver calls
2314  * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
2315  *               returns RECOVERED or NEED_RESET if fw_hung
2316  * NEED_RESET - driver's slot_reset()
2317  * DISCONNECT - device is dead & cannot recover
2318  * RECOVERED - driver's pci_resume()
2319  */
2320 static pci_ers_result_t
2321 qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
2322 {
2323         struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2324
2325         ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
2326             ha->host_no, __func__, state);
2327
2328         if (!is_aer_supported(ha))
2329                 return PCI_ERS_RESULT_NONE;
2330
2331         switch (state) {
2332         case pci_channel_io_normal:
2333                 clear_bit(AF_EEH_BUSY, &ha->flags);
2334                 return PCI_ERS_RESULT_CAN_RECOVER;
2335         case pci_channel_io_frozen:
2336                 set_bit(AF_EEH_BUSY, &ha->flags);
2337                 qla4xxx_mailbox_premature_completion(ha);
2338                 qla4xxx_free_irqs(ha);
2339                 pci_disable_device(pdev);
2340                 /* Return back all IOs */
2341                 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
2342                 return PCI_ERS_RESULT_NEED_RESET;
2343         case pci_channel_io_perm_failure:
2344                 set_bit(AF_EEH_BUSY, &ha->flags);
2345                 set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
2346                 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
2347                 return PCI_ERS_RESULT_DISCONNECT;
2348         }
2349         return PCI_ERS_RESULT_NEED_RESET;
2350 }
2351
2352 /**
2353  * qla4xxx_pci_mmio_enabled() gets called if
2354  * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
2355  * and read/write to the device still works.
2356  **/
2357 static pci_ers_result_t
2358 qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
2359 {
2360         struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2361
2362         if (!is_aer_supported(ha))
2363                 return PCI_ERS_RESULT_NONE;
2364
2365         return PCI_ERS_RESULT_RECOVERED;
2366 }
2367
2368 static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
2369 {
2370         uint32_t rval = QLA_ERROR;
2371         uint32_t ret = 0;
2372         int fn;
2373         struct pci_dev *other_pdev = NULL;
2374
2375         ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
2376
2377         set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2378
2379         if (test_bit(AF_ONLINE, &ha->flags)) {
2380                 clear_bit(AF_ONLINE, &ha->flags);
2381                 qla4xxx_mark_all_devices_missing(ha);
2382                 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2383         }
2384
2385         fn = PCI_FUNC(ha->pdev->devfn);
2386         while (fn > 0) {
2387                 fn--;
2388                 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
2389                     "func %x\n", ha->host_no, __func__, fn);
2390                 /* Get the pci device given the domain, bus,
2391                  * slot/function number */
2392                 other_pdev =
2393                     pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
2394                     ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
2395                     fn));
2396
2397                 if (!other_pdev)
2398                         continue;
2399
2400                 if (atomic_read(&other_pdev->enable_cnt)) {
2401                         ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
2402                             "func in enabled state%x\n", ha->host_no,
2403                             __func__, fn);
2404                         pci_dev_put(other_pdev);
2405                         break;
2406                 }
2407                 pci_dev_put(other_pdev);
2408         }
2409
2410         /* The first function on the card, the reset owner will
2411          * start & initialize the firmware. The other functions
2412          * on the card will reset the firmware context
2413          */
2414         if (!fn) {
2415                 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
2416                     "0x%x is the owner\n", ha->host_no, __func__,
2417                     ha->pdev->devfn);
2418
2419                 qla4_8xxx_idc_lock(ha);
2420                 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2421                     QLA82XX_DEV_COLD);
2422
2423                 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
2424                     QLA82XX_IDC_VERSION);
2425
2426                 qla4_8xxx_idc_unlock(ha);
2427                 clear_bit(AF_FW_RECOVERY, &ha->flags);
2428                 rval = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
2429                 qla4_8xxx_idc_lock(ha);
2430
2431                 if (rval != QLA_SUCCESS) {
2432                         ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
2433                             "FAILED\n", ha->host_no, __func__);
2434                         qla4_8xxx_clear_drv_active(ha);
2435                         qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2436                             QLA82XX_DEV_FAILED);
2437                 } else {
2438                         ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
2439                             "READY\n", ha->host_no, __func__);
2440                         qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2441                             QLA82XX_DEV_READY);
2442                         /* Clear driver state register */
2443                         qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
2444                         qla4_8xxx_set_drv_active(ha);
2445                         ret = qla4xxx_request_irqs(ha);
2446                         if (ret) {
2447                                 ql4_printk(KERN_WARNING, ha, "Failed to "
2448                                     "reserve interrupt %d already in use.\n",
2449                                     ha->pdev->irq);
2450                                 rval = QLA_ERROR;
2451                         } else {
2452                                 ha->isp_ops->enable_intrs(ha);
2453                                 rval = QLA_SUCCESS;
2454                         }
2455                 }
2456                 qla4_8xxx_idc_unlock(ha);
2457         } else {
2458                 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
2459                     "the reset owner\n", ha->host_no, __func__,
2460                     ha->pdev->devfn);
2461                 if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
2462                     QLA82XX_DEV_READY)) {
2463                         clear_bit(AF_FW_RECOVERY, &ha->flags);
2464                         rval = qla4xxx_initialize_adapter(ha,
2465                             PRESERVE_DDB_LIST);
2466                         if (rval == QLA_SUCCESS) {
2467                                 ret = qla4xxx_request_irqs(ha);
2468                                 if (ret) {
2469                                         ql4_printk(KERN_WARNING, ha, "Failed to"
2470                                             " reserve interrupt %d already in"
2471                                             " use.\n", ha->pdev->irq);
2472                                         rval = QLA_ERROR;
2473                                 } else {
2474                                         ha->isp_ops->enable_intrs(ha);
2475                                         rval = QLA_SUCCESS;
2476                                 }
2477                         }
2478                         qla4_8xxx_idc_lock(ha);
2479                         qla4_8xxx_set_drv_active(ha);
2480                         qla4_8xxx_idc_unlock(ha);
2481                 }
2482         }
2483         clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2484         return rval;
2485 }
2486
2487 static pci_ers_result_t
2488 qla4xxx_pci_slot_reset(struct pci_dev *pdev)
2489 {
2490         pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
2491         struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2492         int rc;
2493
2494         ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
2495             ha->host_no, __func__);
2496
2497         if (!is_aer_supported(ha))
2498                 return PCI_ERS_RESULT_NONE;
2499
2500         /* Restore the saved state of PCIe device -
2501          * BAR registers, PCI Config space, PCIX, MSI,
2502          * IOV states
2503          */
2504         pci_restore_state(pdev);
2505
2506         /* pci_restore_state() clears the saved_state flag of the device
2507          * save restored state which resets saved_state flag
2508          */
2509         pci_save_state(pdev);
2510
2511         /* Initialize device or resume if in suspended state */
2512         rc = pci_enable_device(pdev);
2513         if (rc) {
2514                 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Can't re-enable "
2515                     "device after reset\n", ha->host_no, __func__);
2516                 goto exit_slot_reset;
2517         }
2518
2519         ha->isp_ops->disable_intrs(ha);
2520
2521         if (is_qla8022(ha)) {
2522                 if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
2523                         ret = PCI_ERS_RESULT_RECOVERED;
2524                         goto exit_slot_reset;
2525                 } else
2526                         goto exit_slot_reset;
2527         }
2528
2529 exit_slot_reset:
2530         ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
2531             "device after reset\n", ha->host_no, __func__, ret);
2532         return ret;
2533 }
2534
2535 static void
2536 qla4xxx_pci_resume(struct pci_dev *pdev)
2537 {
2538         struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2539         int ret;
2540
2541         ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
2542             ha->host_no, __func__);
2543
2544         ret = qla4xxx_wait_for_hba_online(ha);
2545         if (ret != QLA_SUCCESS) {
2546                 ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
2547                     "resume I/O from slot/link_reset\n", ha->host_no,
2548                      __func__);
2549         }
2550
2551         pci_cleanup_aer_uncorrect_error_status(pdev);
2552         clear_bit(AF_EEH_BUSY, &ha->flags);
2553 }
2554
2555 static struct pci_error_handlers qla4xxx_err_handler = {
2556         .error_detected = qla4xxx_pci_error_detected,
2557         .mmio_enabled = qla4xxx_pci_mmio_enabled,
2558         .slot_reset = qla4xxx_pci_slot_reset,
2559         .resume = qla4xxx_pci_resume,
2560 };
2561
2562 static struct pci_device_id qla4xxx_pci_tbl[] = {
2563         {
2564                 .vendor         = PCI_VENDOR_ID_QLOGIC,
2565                 .device         = PCI_DEVICE_ID_QLOGIC_ISP4010,
2566                 .subvendor      = PCI_ANY_ID,
2567                 .subdevice      = PCI_ANY_ID,
2568         },
2569         {
2570                 .vendor         = PCI_VENDOR_ID_QLOGIC,
2571                 .device         = PCI_DEVICE_ID_QLOGIC_ISP4022,
2572                 .subvendor      = PCI_ANY_ID,
2573                 .subdevice      = PCI_ANY_ID,
2574         },
2575         {
2576                 .vendor         = PCI_VENDOR_ID_QLOGIC,
2577                 .device         = PCI_DEVICE_ID_QLOGIC_ISP4032,
2578                 .subvendor      = PCI_ANY_ID,
2579                 .subdevice      = PCI_ANY_ID,
2580         },
2581         {
2582                 .vendor         = PCI_VENDOR_ID_QLOGIC,
2583                 .device         = PCI_DEVICE_ID_QLOGIC_ISP8022,
2584                 .subvendor      = PCI_ANY_ID,
2585                 .subdevice      = PCI_ANY_ID,
2586         },
2587         {0, 0},
2588 };
2589 MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
2590
2591 static struct pci_driver qla4xxx_pci_driver = {
2592         .name           = DRIVER_NAME,
2593         .id_table       = qla4xxx_pci_tbl,
2594         .probe          = qla4xxx_probe_adapter,
2595         .remove         = qla4xxx_remove_adapter,
2596         .err_handler = &qla4xxx_err_handler,
2597 };
2598
2599 static int __init qla4xxx_module_init(void)
2600 {
2601         int ret;
2602
2603         /* Allocate cache for SRBs. */
2604         srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
2605                                        SLAB_HWCACHE_ALIGN, NULL);
2606         if (srb_cachep == NULL) {
2607                 printk(KERN_ERR
2608                        "%s: Unable to allocate SRB cache..."
2609                        "Failing load!\n", DRIVER_NAME);
2610                 ret = -ENOMEM;
2611                 goto no_srp_cache;
2612         }
2613
2614         /* Derive version string. */
2615         strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
2616         if (ql4xextended_error_logging)
2617                 strcat(qla4xxx_version_str, "-debug");
2618
2619         qla4xxx_scsi_transport =
2620                 iscsi_register_transport(&qla4xxx_iscsi_transport);
2621         if (!qla4xxx_scsi_transport){
2622                 ret = -ENODEV;
2623                 goto release_srb_cache;
2624         }
2625
2626         ret = pci_register_driver(&qla4xxx_pci_driver);
2627         if (ret)
2628                 goto unregister_transport;
2629
2630         printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
2631         return 0;
2632
2633 unregister_transport:
2634         iscsi_unregister_transport(&qla4xxx_iscsi_transport);
2635 release_srb_cache:
2636         kmem_cache_destroy(srb_cachep);
2637 no_srp_cache:
2638         return ret;
2639 }
2640
2641 static void __exit qla4xxx_module_exit(void)
2642 {
2643         pci_unregister_driver(&qla4xxx_pci_driver);
2644         iscsi_unregister_transport(&qla4xxx_iscsi_transport);
2645         kmem_cache_destroy(srb_cachep);
2646 }
2647
2648 module_init(qla4xxx_module_init);
2649 module_exit(qla4xxx_module_exit);
2650
2651 MODULE_AUTHOR("QLogic Corporation");
2652 MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
2653 MODULE_LICENSE("GPL");
2654 MODULE_VERSION(QLA4XXX_DRIVER_VERSION);