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