821384147a41cba12a2501f7b705001cbd1f56cb
[linux-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] port down retry count "
167                               "of (%d) secs exhausted, marking device DEAD.\n",
168                               ha->host_no, __func__, ddb_entry->fw_ddb_index,
169                               ha->port_down_retry_count));
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 = ddb_entry->ha->port_down_retry_count;
302         err = iscsi_add_session(ddb_entry->sess, ddb_entry->fw_ddb_index);
303         if (err) {
304                 DEBUG2(printk(KERN_ERR "Could not add session.\n"));
305                 return err;
306         }
307
308         ddb_entry->conn = iscsi_create_conn(ddb_entry->sess, 0, 0);
309         if (!ddb_entry->conn) {
310                 iscsi_remove_session(ddb_entry->sess);
311                 DEBUG2(printk(KERN_ERR "Could not add connection.\n"));
312                 return -ENOMEM;
313         }
314
315         /* finally ready to go */
316         iscsi_unblock_session(ddb_entry->sess);
317         return 0;
318 }
319
320 struct ddb_entry *qla4xxx_alloc_sess(struct scsi_qla_host *ha)
321 {
322         struct ddb_entry *ddb_entry;
323         struct iscsi_cls_session *sess;
324
325         sess = iscsi_alloc_session(ha->host, &qla4xxx_iscsi_transport,
326                                    sizeof(struct ddb_entry));
327         if (!sess)
328                 return NULL;
329
330         ddb_entry = sess->dd_data;
331         memset(ddb_entry, 0, sizeof(*ddb_entry));
332         ddb_entry->ha = ha;
333         ddb_entry->sess = sess;
334         return ddb_entry;
335 }
336
337 static void qla4xxx_scan_start(struct Scsi_Host *shost)
338 {
339         struct scsi_qla_host *ha = shost_priv(shost);
340         struct ddb_entry *ddb_entry, *ddbtemp;
341
342         /* finish setup of sessions that were already setup in firmware */
343         list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
344                 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE)
345                         qla4xxx_add_sess(ddb_entry);
346         }
347 }
348
349 /*
350  * Timer routines
351  */
352
353 static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
354                                 unsigned long interval)
355 {
356         DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
357                      __func__, ha->host->host_no));
358         init_timer(&ha->timer);
359         ha->timer.expires = jiffies + interval * HZ;
360         ha->timer.data = (unsigned long)ha;
361         ha->timer.function = (void (*)(unsigned long))func;
362         add_timer(&ha->timer);
363         ha->timer_active = 1;
364 }
365
366 static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
367 {
368         del_timer_sync(&ha->timer);
369         ha->timer_active = 0;
370 }
371
372 /***
373  * qla4xxx_mark_device_missing - mark a device as missing.
374  * @ha: Pointer to host adapter structure.
375  * @ddb_entry: Pointer to device database entry
376  *
377  * This routine marks a device missing and close connection.
378  **/
379 void qla4xxx_mark_device_missing(struct scsi_qla_host *ha,
380                                  struct ddb_entry *ddb_entry)
381 {
382         if ((atomic_read(&ddb_entry->state) != DDB_STATE_DEAD)) {
383                 atomic_set(&ddb_entry->state, DDB_STATE_MISSING);
384                 DEBUG2(printk("scsi%ld: ddb [%d] marked MISSING\n",
385                     ha->host_no, ddb_entry->fw_ddb_index));
386         } else
387                 DEBUG2(printk("scsi%ld: ddb [%d] DEAD\n", ha->host_no,
388                     ddb_entry->fw_ddb_index))
389
390         iscsi_block_session(ddb_entry->sess);
391         iscsi_conn_error_event(ddb_entry->conn, ISCSI_ERR_CONN_FAILED);
392 }
393
394 /**
395  * qla4xxx_mark_all_devices_missing - mark all devices as missing.
396  * @ha: Pointer to host adapter structure.
397  *
398  * This routine marks a device missing and resets the relogin retry count.
399  **/
400 void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
401 {
402         struct ddb_entry *ddb_entry, *ddbtemp;
403         list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
404                 qla4xxx_mark_device_missing(ha, ddb_entry);
405         }
406 }
407
408 static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
409                                        struct ddb_entry *ddb_entry,
410                                        struct scsi_cmnd *cmd,
411                                        void (*done)(struct scsi_cmnd *))
412 {
413         struct srb *srb;
414
415         srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
416         if (!srb)
417                 return srb;
418
419         kref_init(&srb->srb_ref);
420         srb->ha = ha;
421         srb->ddb = ddb_entry;
422         srb->cmd = cmd;
423         srb->flags = 0;
424         CMD_SP(cmd) = (void *)srb;
425         cmd->scsi_done = done;
426
427         return srb;
428 }
429
430 static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
431 {
432         struct scsi_cmnd *cmd = srb->cmd;
433
434         if (srb->flags & SRB_DMA_VALID) {
435                 scsi_dma_unmap(cmd);
436                 srb->flags &= ~SRB_DMA_VALID;
437         }
438         CMD_SP(cmd) = NULL;
439 }
440
441 void qla4xxx_srb_compl(struct kref *ref)
442 {
443         struct srb *srb = container_of(ref, struct srb, srb_ref);
444         struct scsi_cmnd *cmd = srb->cmd;
445         struct scsi_qla_host *ha = srb->ha;
446
447         qla4xxx_srb_free_dma(ha, srb);
448
449         mempool_free(srb, ha->srb_mempool);
450
451         cmd->scsi_done(cmd);
452 }
453
454 /**
455  * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
456  * @cmd: Pointer to Linux's SCSI command structure
457  * @done_fn: Function that the driver calls to notify the SCSI mid-layer
458  *      that the command has been processed.
459  *
460  * Remarks:
461  * This routine is invoked by Linux to send a SCSI command to the driver.
462  * The mid-level driver tries to ensure that queuecommand never gets
463  * invoked concurrently with itself or the interrupt handler (although
464  * the interrupt handler may call this routine as part of request-
465  * completion handling).   Unfortunely, it sometimes calls the scheduler
466  * in interrupt context which is a big NO! NO!.
467  **/
468 static int qla4xxx_queuecommand(struct scsi_cmnd *cmd,
469                                 void (*done)(struct scsi_cmnd *))
470 {
471         struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
472         struct ddb_entry *ddb_entry = cmd->device->hostdata;
473         struct iscsi_cls_session *sess = ddb_entry->sess;
474         struct srb *srb;
475         int rval;
476
477         if (!sess) {
478                 cmd->result = DID_IMM_RETRY << 16;
479                 goto qc_fail_command;
480         }
481
482         rval = iscsi_session_chkready(sess);
483         if (rval) {
484                 cmd->result = rval;
485                 goto qc_fail_command;
486         }
487
488         if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
489                 if (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD) {
490                         cmd->result = DID_NO_CONNECT << 16;
491                         goto qc_fail_command;
492                 }
493                 return SCSI_MLQUEUE_TARGET_BUSY;
494         }
495
496         if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
497             test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
498             test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
499             test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
500             test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
501             !test_bit(AF_ONLINE, &ha->flags) ||
502             test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
503                 goto qc_host_busy;
504
505         spin_unlock_irq(ha->host->host_lock);
506
507         srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd, done);
508         if (!srb)
509                 goto qc_host_busy_lock;
510
511         rval = qla4xxx_send_command_to_isp(ha, srb);
512         if (rval != QLA_SUCCESS)
513                 goto qc_host_busy_free_sp;
514
515         spin_lock_irq(ha->host->host_lock);
516         return 0;
517
518 qc_host_busy_free_sp:
519         qla4xxx_srb_free_dma(ha, srb);
520         mempool_free(srb, ha->srb_mempool);
521
522 qc_host_busy_lock:
523         spin_lock_irq(ha->host->host_lock);
524
525 qc_host_busy:
526         return SCSI_MLQUEUE_HOST_BUSY;
527
528 qc_fail_command:
529         done(cmd);
530
531         return 0;
532 }
533
534 /**
535  * qla4xxx_mem_free - frees memory allocated to adapter
536  * @ha: Pointer to host adapter structure.
537  *
538  * Frees memory previously allocated by qla4xxx_mem_alloc
539  **/
540 static void qla4xxx_mem_free(struct scsi_qla_host *ha)
541 {
542         if (ha->queues)
543                 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
544                                   ha->queues_dma);
545
546         ha->queues_len = 0;
547         ha->queues = NULL;
548         ha->queues_dma = 0;
549         ha->request_ring = NULL;
550         ha->request_dma = 0;
551         ha->response_ring = NULL;
552         ha->response_dma = 0;
553         ha->shadow_regs = NULL;
554         ha->shadow_regs_dma = 0;
555
556         /* Free srb pool. */
557         if (ha->srb_mempool)
558                 mempool_destroy(ha->srb_mempool);
559
560         ha->srb_mempool = NULL;
561
562         /* release io space registers  */
563         if (is_qla8022(ha)) {
564                 if (ha->nx_pcibase)
565                         iounmap(
566                             (struct device_reg_82xx __iomem *)ha->nx_pcibase);
567
568                 if (ha->nx_db_wr_ptr)
569                         iounmap(
570                             (struct device_reg_82xx __iomem *)ha->nx_db_wr_ptr);
571         } else if (ha->reg)
572                 iounmap(ha->reg);
573         pci_release_regions(ha->pdev);
574 }
575
576 /**
577  * qla4xxx_mem_alloc - allocates memory for use by adapter.
578  * @ha: Pointer to host adapter structure
579  *
580  * Allocates DMA memory for request and response queues. Also allocates memory
581  * for srbs.
582  **/
583 static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
584 {
585         unsigned long align;
586
587         /* Allocate contiguous block of DMA memory for queues. */
588         ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
589                           (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
590                           sizeof(struct shadow_regs) +
591                           MEM_ALIGN_VALUE +
592                           (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
593         ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
594                                         &ha->queues_dma, GFP_KERNEL);
595         if (ha->queues == NULL) {
596                 ql4_printk(KERN_WARNING, ha,
597                     "Memory Allocation failed - queues.\n");
598
599                 goto mem_alloc_error_exit;
600         }
601         memset(ha->queues, 0, ha->queues_len);
602
603         /*
604          * As per RISC alignment requirements -- the bus-address must be a
605          * multiple of the request-ring size (in bytes).
606          */
607         align = 0;
608         if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
609                 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
610                                            (MEM_ALIGN_VALUE - 1));
611
612         /* Update request and response queue pointers. */
613         ha->request_dma = ha->queues_dma + align;
614         ha->request_ring = (struct queue_entry *) (ha->queues + align);
615         ha->response_dma = ha->queues_dma + align +
616                 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
617         ha->response_ring = (struct queue_entry *) (ha->queues + align +
618                                                     (REQUEST_QUEUE_DEPTH *
619                                                      QUEUE_SIZE));
620         ha->shadow_regs_dma = ha->queues_dma + align +
621                 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
622                 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
623         ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
624                                                   (REQUEST_QUEUE_DEPTH *
625                                                    QUEUE_SIZE) +
626                                                   (RESPONSE_QUEUE_DEPTH *
627                                                    QUEUE_SIZE));
628
629         /* Allocate memory for srb pool. */
630         ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
631                                          mempool_free_slab, srb_cachep);
632         if (ha->srb_mempool == NULL) {
633                 ql4_printk(KERN_WARNING, ha,
634                     "Memory Allocation failed - SRB Pool.\n");
635
636                 goto mem_alloc_error_exit;
637         }
638
639         return QLA_SUCCESS;
640
641 mem_alloc_error_exit:
642         qla4xxx_mem_free(ha);
643         return QLA_ERROR;
644 }
645
646 /**
647  * qla4_8xxx_check_fw_alive  - Check firmware health
648  * @ha: Pointer to host adapter structure.
649  *
650  * Context: Interrupt
651  **/
652 static void qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
653 {
654         uint32_t fw_heartbeat_counter, halt_status;
655
656         fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
657
658         if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
659                 ha->seconds_since_last_heartbeat++;
660                 /* FW not alive after 2 seconds */
661                 if (ha->seconds_since_last_heartbeat == 2) {
662                         ha->seconds_since_last_heartbeat = 0;
663                         halt_status = qla4_8xxx_rd_32(ha,
664                             QLA82XX_PEG_HALT_STATUS1);
665                         /* Since we cannot change dev_state in interrupt
666                          * context, set appropriate DPC flag then wakeup
667                          * DPC */
668                         if (halt_status & HALT_STATUS_UNRECOVERABLE)
669                                 set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
670                         else {
671                                 printk("scsi%ld: %s: detect abort needed!\n",
672                                     ha->host_no, __func__);
673                                 set_bit(DPC_RESET_HA, &ha->dpc_flags);
674                         }
675                         qla4xxx_wake_dpc(ha);
676                 }
677         }
678         ha->fw_heartbeat_counter = fw_heartbeat_counter;
679 }
680
681 /**
682  * qla4_8xxx_watchdog - Poll dev state
683  * @ha: Pointer to host adapter structure.
684  *
685  * Context: Interrupt
686  **/
687 void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
688 {
689         uint32_t dev_state;
690
691         dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
692
693         /* don't poll if reset is going on */
694         if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags)) {
695                 if (dev_state == QLA82XX_DEV_NEED_RESET &&
696                     !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
697                         printk("scsi%ld: %s: HW State: NEED RESET!\n",
698                             ha->host_no, __func__);
699                         set_bit(DPC_RESET_HA, &ha->dpc_flags);
700                         qla4xxx_wake_dpc(ha);
701                 } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
702                     !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
703                         printk("scsi%ld: %s: HW State: NEED QUIES!\n",
704                             ha->host_no, __func__);
705                         set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
706                         qla4xxx_wake_dpc(ha);
707                 } else  {
708                         /* Check firmware health */
709                         qla4_8xxx_check_fw_alive(ha);
710                 }
711         }
712 }
713
714 /**
715  * qla4xxx_timer - checks every second for work to do.
716  * @ha: Pointer to host adapter structure.
717  **/
718 static void qla4xxx_timer(struct scsi_qla_host *ha)
719 {
720         struct ddb_entry *ddb_entry, *dtemp;
721         int start_dpc = 0;
722
723         if (test_bit(AF_HBA_GOING_AWAY, &ha->flags)) {
724                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s exited. HBA GOING AWAY\n",
725                     __func__));
726                 return;
727         }
728
729         if (is_qla8022(ha)) {
730                 qla4_8xxx_watchdog(ha);
731         }
732
733         /* Search for relogin's to time-out and port down retry. */
734         list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
735                 /* Count down time between sending relogins */
736                 if (adapter_up(ha) &&
737                     !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
738                     atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
739                         if (atomic_read(&ddb_entry->retry_relogin_timer) !=
740                             INVALID_ENTRY) {
741                                 if (atomic_read(&ddb_entry->retry_relogin_timer)
742                                                 == 0) {
743                                         atomic_set(&ddb_entry->
744                                                 retry_relogin_timer,
745                                                 INVALID_ENTRY);
746                                         set_bit(DPC_RELOGIN_DEVICE,
747                                                 &ha->dpc_flags);
748                                         set_bit(DF_RELOGIN, &ddb_entry->flags);
749                                         DEBUG2(printk("scsi%ld: %s: ddb [%d]"
750                                                       " login device\n",
751                                                       ha->host_no, __func__,
752                                                       ddb_entry->fw_ddb_index));
753                                 } else
754                                         atomic_dec(&ddb_entry->
755                                                         retry_relogin_timer);
756                         }
757                 }
758
759                 /* Wait for relogin to timeout */
760                 if (atomic_read(&ddb_entry->relogin_timer) &&
761                     (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
762                         /*
763                          * If the relogin times out and the device is
764                          * still NOT ONLINE then try and relogin again.
765                          */
766                         if (atomic_read(&ddb_entry->state) !=
767                             DDB_STATE_ONLINE &&
768                             ddb_entry->fw_ddb_device_state ==
769                             DDB_DS_SESSION_FAILED) {
770                                 /* Reset retry relogin timer */
771                                 atomic_inc(&ddb_entry->relogin_retry_count);
772                                 DEBUG2(printk("scsi%ld: ddb [%d] relogin"
773                                               " timed out-retrying"
774                                               " relogin (%d)\n",
775                                               ha->host_no,
776                                               ddb_entry->fw_ddb_index,
777                                               atomic_read(&ddb_entry->
778                                                           relogin_retry_count))
779                                         );
780                                 start_dpc++;
781                                 DEBUG(printk("scsi%ld:%d:%d: ddb [%d] "
782                                              "initate relogin after"
783                                              " %d seconds\n",
784                                              ha->host_no, ddb_entry->bus,
785                                              ddb_entry->target,
786                                              ddb_entry->fw_ddb_index,
787                                              ddb_entry->default_time2wait + 4)
788                                         );
789
790                                 atomic_set(&ddb_entry->retry_relogin_timer,
791                                            ddb_entry->default_time2wait + 4);
792                         }
793                 }
794         }
795
796         if (!is_qla8022(ha)) {
797                 /* Check for heartbeat interval. */
798                 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
799                     ha->heartbeat_interval != 0) {
800                         ha->seconds_since_last_heartbeat++;
801                         if (ha->seconds_since_last_heartbeat >
802                             ha->heartbeat_interval + 2)
803                                 set_bit(DPC_RESET_HA, &ha->dpc_flags);
804                 }
805         }
806
807         /* Wakeup the dpc routine for this adapter, if needed. */
808         if ((start_dpc ||
809              test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
810              test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
811              test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
812              test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
813              test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
814              test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
815              test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
816              test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
817              test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
818              test_bit(DPC_AEN, &ha->dpc_flags)) &&
819              !test_bit(AF_DPC_SCHEDULED, &ha->flags) &&
820              ha->dpc_thread) {
821                 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
822                               " - dpc flags = 0x%lx\n",
823                               ha->host_no, __func__, ha->dpc_flags));
824                 qla4xxx_wake_dpc(ha);
825         }
826
827         /* Reschedule timer thread to call us back in one second */
828         mod_timer(&ha->timer, jiffies + HZ);
829
830         DEBUG2(ha->seconds_since_last_intr++);
831 }
832
833 /**
834  * qla4xxx_cmd_wait - waits for all outstanding commands to complete
835  * @ha: Pointer to host adapter structure.
836  *
837  * This routine stalls the driver until all outstanding commands are returned.
838  * Caller must release the Hardware Lock prior to calling this routine.
839  **/
840 static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
841 {
842         uint32_t index = 0;
843         unsigned long flags;
844         struct scsi_cmnd *cmd;
845
846         unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
847
848         DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
849             "complete\n", WAIT_CMD_TOV));
850
851         while (!time_after_eq(jiffies, wtime)) {
852                 spin_lock_irqsave(&ha->hardware_lock, flags);
853                 /* Find a command that hasn't completed. */
854                 for (index = 0; index < ha->host->can_queue; index++) {
855                         cmd = scsi_host_find_tag(ha->host, index);
856                         if (cmd != NULL)
857                                 break;
858                 }
859                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
860
861                 /* If No Commands are pending, wait is complete */
862                 if (index == ha->host->can_queue)
863                         return QLA_SUCCESS;
864
865                 msleep(1000);
866         }
867         /* If we timed out on waiting for commands to come back
868          * return ERROR. */
869         return QLA_ERROR;
870 }
871
872 int qla4xxx_hw_reset(struct scsi_qla_host *ha)
873 {
874         uint32_t ctrl_status;
875         unsigned long flags = 0;
876
877         DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
878
879         if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
880                 return QLA_ERROR;
881
882         spin_lock_irqsave(&ha->hardware_lock, flags);
883
884         /*
885          * If the SCSI Reset Interrupt bit is set, clear it.
886          * Otherwise, the Soft Reset won't work.
887          */
888         ctrl_status = readw(&ha->reg->ctrl_status);
889         if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
890                 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
891
892         /* Issue Soft Reset */
893         writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
894         readl(&ha->reg->ctrl_status);
895
896         spin_unlock_irqrestore(&ha->hardware_lock, flags);
897         return QLA_SUCCESS;
898 }
899
900 /**
901  * qla4xxx_soft_reset - performs soft reset.
902  * @ha: Pointer to host adapter structure.
903  **/
904 int qla4xxx_soft_reset(struct scsi_qla_host *ha)
905 {
906         uint32_t max_wait_time;
907         unsigned long flags = 0;
908         int status = QLA_ERROR;
909         uint32_t ctrl_status;
910
911         qla4xxx_hw_reset(ha);
912
913         /* Wait until the Network Reset Intr bit is cleared */
914         max_wait_time = RESET_INTR_TOV;
915         do {
916                 spin_lock_irqsave(&ha->hardware_lock, flags);
917                 ctrl_status = readw(&ha->reg->ctrl_status);
918                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
919
920                 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
921                         break;
922
923                 msleep(1000);
924         } while ((--max_wait_time));
925
926         if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
927                 DEBUG2(printk(KERN_WARNING
928                               "scsi%ld: Network Reset Intr not cleared by "
929                               "Network function, clearing it now!\n",
930                               ha->host_no));
931                 spin_lock_irqsave(&ha->hardware_lock, flags);
932                 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
933                 readl(&ha->reg->ctrl_status);
934                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
935         }
936
937         /* Wait until the firmware tells us the Soft Reset is done */
938         max_wait_time = SOFT_RESET_TOV;
939         do {
940                 spin_lock_irqsave(&ha->hardware_lock, flags);
941                 ctrl_status = readw(&ha->reg->ctrl_status);
942                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
943
944                 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
945                         status = QLA_SUCCESS;
946                         break;
947                 }
948
949                 msleep(1000);
950         } while ((--max_wait_time));
951
952         /*
953          * Also, make sure that the SCSI Reset Interrupt bit has been cleared
954          * after the soft reset has taken place.
955          */
956         spin_lock_irqsave(&ha->hardware_lock, flags);
957         ctrl_status = readw(&ha->reg->ctrl_status);
958         if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
959                 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
960                 readl(&ha->reg->ctrl_status);
961         }
962         spin_unlock_irqrestore(&ha->hardware_lock, flags);
963
964         /* If soft reset fails then most probably the bios on other
965          * function is also enabled.
966          * Since the initialization is sequential the other fn
967          * wont be able to acknowledge the soft reset.
968          * Issue a force soft reset to workaround this scenario.
969          */
970         if (max_wait_time == 0) {
971                 /* Issue Force Soft Reset */
972                 spin_lock_irqsave(&ha->hardware_lock, flags);
973                 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
974                 readl(&ha->reg->ctrl_status);
975                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
976                 /* Wait until the firmware tells us the Soft Reset is done */
977                 max_wait_time = SOFT_RESET_TOV;
978                 do {
979                         spin_lock_irqsave(&ha->hardware_lock, flags);
980                         ctrl_status = readw(&ha->reg->ctrl_status);
981                         spin_unlock_irqrestore(&ha->hardware_lock, flags);
982
983                         if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
984                                 status = QLA_SUCCESS;
985                                 break;
986                         }
987
988                         msleep(1000);
989                 } while ((--max_wait_time));
990         }
991
992         return status;
993 }
994
995 /**
996  * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
997  * @ha: Pointer to host adapter structure.
998  * @res: returned scsi status
999  *
1000  * This routine is called just prior to a HARD RESET to return all
1001  * outstanding commands back to the Operating System.
1002  * Caller should make sure that the following locks are released
1003  * before this calling routine: Hardware lock, and io_request_lock.
1004  **/
1005 static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
1006 {
1007         struct srb *srb;
1008         int i;
1009         unsigned long flags;
1010
1011         spin_lock_irqsave(&ha->hardware_lock, flags);
1012         for (i = 0; i < ha->host->can_queue; i++) {
1013                 srb = qla4xxx_del_from_active_array(ha, i);
1014                 if (srb != NULL) {
1015                         srb->cmd->result = res;
1016                         kref_put(&srb->srb_ref, qla4xxx_srb_compl);
1017                 }
1018         }
1019         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1020 }
1021
1022 void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
1023 {
1024         clear_bit(AF_ONLINE, &ha->flags);
1025
1026         /* Disable the board */
1027         ql4_printk(KERN_INFO, ha, "Disabling the board\n");
1028         set_bit(AF_HBA_GOING_AWAY, &ha->flags);
1029
1030         qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
1031         qla4xxx_mark_all_devices_missing(ha);
1032         clear_bit(AF_INIT_DONE, &ha->flags);
1033 }
1034
1035 /**
1036  * qla4xxx_recover_adapter - recovers adapter after a fatal error
1037  * @ha: Pointer to host adapter structure.
1038  **/
1039 static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
1040 {
1041         int status = QLA_ERROR;
1042         uint8_t reset_chip = 0;
1043
1044         /* Stall incoming I/O until we are done */
1045         scsi_block_requests(ha->host);
1046         clear_bit(AF_ONLINE, &ha->flags);
1047
1048         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
1049
1050         set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
1051
1052         if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
1053                 reset_chip = 1;
1054
1055         /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
1056          * do not reset adapter, jump to initialize_adapter */
1057         if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1058                 status = QLA_SUCCESS;
1059                 goto recover_ha_init_adapter;
1060         }
1061
1062         /* For the ISP-82xx adapter, issue a stop_firmware if invoked
1063          * from eh_host_reset or ioctl module */
1064         if (is_qla8022(ha) && !reset_chip &&
1065             test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
1066
1067                 DEBUG2(ql4_printk(KERN_INFO, ha,
1068                     "scsi%ld: %s - Performing stop_firmware...\n",
1069                     ha->host_no, __func__));
1070                 status = ha->isp_ops->reset_firmware(ha);
1071                 if (status == QLA_SUCCESS) {
1072                         qla4xxx_cmd_wait(ha);
1073                         ha->isp_ops->disable_intrs(ha);
1074                         qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1075                         qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1076                 } else {
1077                         /* If the stop_firmware fails then
1078                          * reset the entire chip */
1079                         reset_chip = 1;
1080                         clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1081                         set_bit(DPC_RESET_HA, &ha->dpc_flags);
1082                 }
1083         }
1084
1085         /* Issue full chip reset if recovering from a catastrophic error,
1086          * or if stop_firmware fails for ISP-82xx.
1087          * This is the default case for ISP-4xxx */
1088         if (!is_qla8022(ha) || reset_chip) {
1089                 qla4xxx_cmd_wait(ha);
1090                 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1091                 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1092                 DEBUG2(ql4_printk(KERN_INFO, ha,
1093                     "scsi%ld: %s - Performing chip reset..\n",
1094                     ha->host_no, __func__));
1095                 status = ha->isp_ops->reset_chip(ha);
1096         }
1097
1098         /* Flush any pending ddb changed AENs */
1099         qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1100
1101 recover_ha_init_adapter:
1102         /* Upon successful firmware/chip reset, re-initialize the adapter */
1103         if (status == QLA_SUCCESS) {
1104                 /* For ISP-4xxx, force function 1 to always initialize
1105                  * before function 3 to prevent both funcions from
1106                  * stepping on top of the other */
1107                 if (!is_qla8022(ha) && (ha->mac_index == 3))
1108                         ssleep(6);
1109
1110                 /* NOTE: AF_ONLINE flag set upon successful completion of
1111                  *       qla4xxx_initialize_adapter */
1112                 status = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
1113         }
1114
1115         /* Retry failed adapter initialization, if necessary
1116          * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
1117          * case to prevent ping-pong resets between functions */
1118         if (!test_bit(AF_ONLINE, &ha->flags) &&
1119             !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1120                 /* Adapter initialization failed, see if we can retry
1121                  * resetting the ha.
1122                  * Since we don't want to block the DPC for too long
1123                  * with multiple resets in the same thread,
1124                  * utilize DPC to retry */
1125                 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
1126                         ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
1127                         DEBUG2(printk("scsi%ld: recover adapter - retrying "
1128                                       "(%d) more times\n", ha->host_no,
1129                                       ha->retry_reset_ha_cnt));
1130                         set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1131                         status = QLA_ERROR;
1132                 } else {
1133                         if (ha->retry_reset_ha_cnt > 0) {
1134                                 /* Schedule another Reset HA--DPC will retry */
1135                                 ha->retry_reset_ha_cnt--;
1136                                 DEBUG2(printk("scsi%ld: recover adapter - "
1137                                               "retry remaining %d\n",
1138                                               ha->host_no,
1139                                               ha->retry_reset_ha_cnt));
1140                                 status = QLA_ERROR;
1141                         }
1142
1143                         if (ha->retry_reset_ha_cnt == 0) {
1144                                 /* Recover adapter retries have been exhausted.
1145                                  * Adapter DEAD */
1146                                 DEBUG2(printk("scsi%ld: recover adapter "
1147                                               "failed - board disabled\n",
1148                                               ha->host_no));
1149                                 qla4xxx_dead_adapter_cleanup(ha);
1150                                 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1151                                 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
1152                                 clear_bit(DPC_RESET_HA_FW_CONTEXT,
1153                                           &ha->dpc_flags);
1154                                 status = QLA_ERROR;
1155                         }
1156                 }
1157         } else {
1158                 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
1159                 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1160                 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1161         }
1162
1163         ha->adapter_error_count++;
1164
1165         if (test_bit(AF_ONLINE, &ha->flags))
1166                 ha->isp_ops->enable_intrs(ha);
1167
1168         scsi_unblock_requests(ha->host);
1169
1170         clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
1171         DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
1172             status == QLA_ERROR ? "FAILED" : "SUCCEDED"));
1173
1174         return status;
1175 }
1176
1177 void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
1178 {
1179         if (ha->dpc_thread &&
1180             !test_bit(AF_DPC_SCHEDULED, &ha->flags)) {
1181                 set_bit(AF_DPC_SCHEDULED, &ha->flags);
1182                 queue_work(ha->dpc_thread, &ha->dpc_work);
1183         }
1184 }
1185
1186 /**
1187  * qla4xxx_do_dpc - dpc routine
1188  * @data: in our case pointer to adapter structure
1189  *
1190  * This routine is a task that is schedule by the interrupt handler
1191  * to perform the background processing for interrupts.  We put it
1192  * on a task queue that is consumed whenever the scheduler runs; that's
1193  * so you can do anything (i.e. put the process to sleep etc).  In fact,
1194  * the mid-level tries to sleep when it reaches the driver threshold
1195  * "host->can_queue". This can cause a panic if we were in our interrupt code.
1196  **/
1197 static void qla4xxx_do_dpc(struct work_struct *work)
1198 {
1199         struct scsi_qla_host *ha =
1200                 container_of(work, struct scsi_qla_host, dpc_work);
1201         struct ddb_entry *ddb_entry, *dtemp;
1202         int status = QLA_ERROR;
1203
1204         DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
1205             "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
1206             ha->host_no, __func__, ha->flags, ha->dpc_flags))
1207
1208         /* Initialization not yet finished. Don't do anything yet. */
1209         if (!test_bit(AF_INIT_DONE, &ha->flags))
1210                 return;
1211
1212         /* HBA is in the process of being permanently disabled.
1213          * Don't process anything */
1214         if (test_bit(AF_HBA_GOING_AWAY, &ha->flags))
1215                 return;
1216
1217         if (is_qla8022(ha)) {
1218                 if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
1219                         qla4_8xxx_idc_lock(ha);
1220                         qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
1221                             QLA82XX_DEV_FAILED);
1222                         qla4_8xxx_idc_unlock(ha);
1223                         ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
1224                         qla4_8xxx_device_state_handler(ha);
1225                 }
1226                 if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
1227                         qla4_8xxx_need_qsnt_handler(ha);
1228                 }
1229         }
1230
1231         if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
1232             (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1233             test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1234             test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
1235                 if (ql4xdontresethba) {
1236                         DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
1237                             ha->host_no, __func__));
1238                         clear_bit(DPC_RESET_HA, &ha->dpc_flags);
1239                         clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1240                         clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1241                         goto dpc_post_reset_ha;
1242                 }
1243                 if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
1244                     test_bit(DPC_RESET_HA, &ha->dpc_flags))
1245                         qla4xxx_recover_adapter(ha);
1246
1247                 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1248                         uint8_t wait_time = RESET_INTR_TOV;
1249
1250                         while ((readw(&ha->reg->ctrl_status) &
1251                                 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
1252                                 if (--wait_time == 0)
1253                                         break;
1254                                 msleep(1000);
1255                         }
1256                         if (wait_time == 0)
1257                                 DEBUG2(printk("scsi%ld: %s: SR|FSR "
1258                                               "bit not cleared-- resetting\n",
1259                                               ha->host_no, __func__));
1260                         qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1261                         if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
1262                                 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1263                                 status = qla4xxx_recover_adapter(ha);
1264                         }
1265                         clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1266                         if (status == QLA_SUCCESS)
1267                                 ha->isp_ops->enable_intrs(ha);
1268                 }
1269         }
1270
1271 dpc_post_reset_ha:
1272         /* ---- process AEN? --- */
1273         if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
1274                 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
1275
1276         /* ---- Get DHCP IP Address? --- */
1277         if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
1278                 qla4xxx_get_dhcp_ip_address(ha);
1279
1280         /* ---- link change? --- */
1281         if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
1282                 if (!test_bit(AF_LINK_UP, &ha->flags)) {
1283                         /* ---- link down? --- */
1284                         list_for_each_entry_safe(ddb_entry, dtemp,
1285                                                  &ha->ddb_list, list) {
1286                                 if (atomic_read(&ddb_entry->state) ==
1287                                                 DDB_STATE_ONLINE)
1288                                         qla4xxx_mark_device_missing(ha,
1289                                                         ddb_entry);
1290                         }
1291                 } else {
1292                         /* ---- link up? --- *
1293                          * F/W will auto login to all devices ONLY ONCE after
1294                          * link up during driver initialization and runtime
1295                          * fatal error recovery.  Therefore, the driver must
1296                          * manually relogin to devices when recovering from
1297                          * connection failures, logouts, expired KATO, etc. */
1298
1299                         list_for_each_entry_safe(ddb_entry, dtemp,
1300                                                         &ha->ddb_list, list) {
1301                                 if ((atomic_read(&ddb_entry->state) ==
1302                                                  DDB_STATE_MISSING) ||
1303                                     (atomic_read(&ddb_entry->state) ==
1304                                                  DDB_STATE_DEAD)) {
1305                                         if (ddb_entry->fw_ddb_device_state ==
1306                                             DDB_DS_SESSION_ACTIVE) {
1307                                                 atomic_set(&ddb_entry->state,
1308                                                            DDB_STATE_ONLINE);
1309                                                 ql4_printk(KERN_INFO, ha,
1310                                                     "scsi%ld: %s: ddb[%d]"
1311                                                     " marked ONLINE\n",
1312                                                     ha->host_no, __func__,
1313                                                     ddb_entry->fw_ddb_index);
1314
1315                                                 iscsi_unblock_session(
1316                                                     ddb_entry->sess);
1317                                         } else
1318                                                 qla4xxx_relogin_device(
1319                                                     ha, ddb_entry);
1320                                 }
1321
1322                         }
1323                 }
1324         }
1325
1326         /* ---- relogin device? --- */
1327         if (adapter_up(ha) &&
1328             test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
1329                 list_for_each_entry_safe(ddb_entry, dtemp,
1330                                          &ha->ddb_list, list) {
1331                         if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
1332                             atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
1333                                 qla4xxx_relogin_device(ha, ddb_entry);
1334
1335                         /*
1336                          * If mbx cmd times out there is no point
1337                          * in continuing further.
1338                          * With large no of targets this can hang
1339                          * the system.
1340                          */
1341                         if (test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
1342                                 printk(KERN_WARNING "scsi%ld: %s: "
1343                                        "need to reset hba\n",
1344                                        ha->host_no, __func__);
1345                                 break;
1346                         }
1347                 }
1348         }
1349         clear_bit(AF_DPC_SCHEDULED, &ha->flags);
1350 }
1351
1352 /**
1353  * qla4xxx_free_adapter - release the adapter
1354  * @ha: pointer to adapter structure
1355  **/
1356 static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
1357 {
1358
1359         if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
1360                 /* Turn-off interrupts on the card. */
1361                 ha->isp_ops->disable_intrs(ha);
1362         }
1363
1364         /* Remove timer thread, if present */
1365         if (ha->timer_active)
1366                 qla4xxx_stop_timer(ha);
1367
1368         /* Kill the kernel thread for this host */
1369         if (ha->dpc_thread)
1370                 destroy_workqueue(ha->dpc_thread);
1371
1372         /* Put firmware in known state */
1373         ha->isp_ops->reset_firmware(ha);
1374
1375         if (is_qla8022(ha)) {
1376                 qla4_8xxx_idc_lock(ha);
1377                 qla4_8xxx_clear_drv_active(ha);
1378                 qla4_8xxx_idc_unlock(ha);
1379         }
1380
1381         /* Detach interrupts */
1382         if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
1383                 qla4xxx_free_irqs(ha);
1384
1385         /* free extra memory */
1386         qla4xxx_mem_free(ha);
1387 }
1388
1389 int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
1390 {
1391         int status = 0;
1392         uint8_t revision_id;
1393         unsigned long mem_base, mem_len, db_base, db_len;
1394         struct pci_dev *pdev = ha->pdev;
1395
1396         status = pci_request_regions(pdev, DRIVER_NAME);
1397         if (status) {
1398                 printk(KERN_WARNING
1399                     "scsi(%ld) Failed to reserve PIO regions (%s) "
1400                     "status=%d\n", ha->host_no, pci_name(pdev), status);
1401                 goto iospace_error_exit;
1402         }
1403
1404         pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
1405         DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
1406             __func__, revision_id));
1407         ha->revision_id = revision_id;
1408
1409         /* remap phys address */
1410         mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
1411         mem_len = pci_resource_len(pdev, 0);
1412         DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
1413             __func__, mem_base, mem_len));
1414
1415         /* mapping of pcibase pointer */
1416         ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
1417         if (!ha->nx_pcibase) {
1418                 printk(KERN_ERR
1419                     "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
1420                 pci_release_regions(ha->pdev);
1421                 goto iospace_error_exit;
1422         }
1423
1424         /* Mapping of IO base pointer, door bell read and write pointer */
1425
1426         /* mapping of IO base pointer */
1427         ha->qla4_8xxx_reg =
1428             (struct device_reg_82xx  __iomem *)((uint8_t *)ha->nx_pcibase +
1429             0xbc000 + (ha->pdev->devfn << 11));
1430
1431         db_base = pci_resource_start(pdev, 4);  /* doorbell is on bar 4 */
1432         db_len = pci_resource_len(pdev, 4);
1433
1434         /* mapping of doorbell write pointer */
1435         ha->nx_db_wr_ptr = (unsigned long)ioremap(db_base +
1436             (ha->pdev->devfn << 12), 4);
1437         if (!ha->nx_db_wr_ptr) {
1438                 printk(KERN_ERR
1439                     "cannot remap MMIO doorbell-write (%s), aborting\n",
1440                     pci_name(pdev));
1441                 goto iospace_error_exit;
1442         }
1443         /* mapping of doorbell read pointer */
1444         ha->nx_db_rd_ptr = (uint8_t *) ha->nx_pcibase + (512 * 1024) +
1445             (ha->pdev->devfn * 8);
1446         if (!ha->nx_db_rd_ptr)
1447                 printk(KERN_ERR
1448                     "cannot remap MMIO doorbell-read (%s), aborting\n",
1449                     pci_name(pdev));
1450         return 0;
1451
1452 iospace_error_exit:
1453         return -ENOMEM;
1454 }
1455
1456 /***
1457  * qla4xxx_iospace_config - maps registers
1458  * @ha: pointer to adapter structure
1459  *
1460  * This routines maps HBA's registers from the pci address space
1461  * into the kernel virtual address space for memory mapped i/o.
1462  **/
1463 int qla4xxx_iospace_config(struct scsi_qla_host *ha)
1464 {
1465         unsigned long pio, pio_len, pio_flags;
1466         unsigned long mmio, mmio_len, mmio_flags;
1467
1468         pio = pci_resource_start(ha->pdev, 0);
1469         pio_len = pci_resource_len(ha->pdev, 0);
1470         pio_flags = pci_resource_flags(ha->pdev, 0);
1471         if (pio_flags & IORESOURCE_IO) {
1472                 if (pio_len < MIN_IOBASE_LEN) {
1473                         ql4_printk(KERN_WARNING, ha,
1474                                 "Invalid PCI I/O region size\n");
1475                         pio = 0;
1476                 }
1477         } else {
1478                 ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
1479                 pio = 0;
1480         }
1481
1482         /* Use MMIO operations for all accesses. */
1483         mmio = pci_resource_start(ha->pdev, 1);
1484         mmio_len = pci_resource_len(ha->pdev, 1);
1485         mmio_flags = pci_resource_flags(ha->pdev, 1);
1486
1487         if (!(mmio_flags & IORESOURCE_MEM)) {
1488                 ql4_printk(KERN_ERR, ha,
1489                     "region #0 not an MMIO resource, aborting\n");
1490
1491                 goto iospace_error_exit;
1492         }
1493
1494         if (mmio_len < MIN_IOBASE_LEN) {
1495                 ql4_printk(KERN_ERR, ha,
1496                     "Invalid PCI mem region size, aborting\n");
1497                 goto iospace_error_exit;
1498         }
1499
1500         if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
1501                 ql4_printk(KERN_WARNING, ha,
1502                     "Failed to reserve PIO/MMIO regions\n");
1503
1504                 goto iospace_error_exit;
1505         }
1506
1507         ha->pio_address = pio;
1508         ha->pio_length = pio_len;
1509         ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
1510         if (!ha->reg) {
1511                 ql4_printk(KERN_ERR, ha,
1512                     "cannot remap MMIO, aborting\n");
1513
1514                 goto iospace_error_exit;
1515         }
1516
1517         return 0;
1518
1519 iospace_error_exit:
1520         return -ENOMEM;
1521 }
1522
1523 static struct isp_operations qla4xxx_isp_ops = {
1524         .iospace_config         = qla4xxx_iospace_config,
1525         .pci_config             = qla4xxx_pci_config,
1526         .disable_intrs          = qla4xxx_disable_intrs,
1527         .enable_intrs           = qla4xxx_enable_intrs,
1528         .start_firmware         = qla4xxx_start_firmware,
1529         .intr_handler           = qla4xxx_intr_handler,
1530         .interrupt_service_routine = qla4xxx_interrupt_service_routine,
1531         .reset_chip             = qla4xxx_soft_reset,
1532         .reset_firmware         = qla4xxx_hw_reset,
1533         .queue_iocb             = qla4xxx_queue_iocb,
1534         .complete_iocb          = qla4xxx_complete_iocb,
1535         .rd_shdw_req_q_out      = qla4xxx_rd_shdw_req_q_out,
1536         .rd_shdw_rsp_q_in       = qla4xxx_rd_shdw_rsp_q_in,
1537         .get_sys_info           = qla4xxx_get_sys_info,
1538 };
1539
1540 static struct isp_operations qla4_8xxx_isp_ops = {
1541         .iospace_config         = qla4_8xxx_iospace_config,
1542         .pci_config             = qla4_8xxx_pci_config,
1543         .disable_intrs          = qla4_8xxx_disable_intrs,
1544         .enable_intrs           = qla4_8xxx_enable_intrs,
1545         .start_firmware         = qla4_8xxx_load_risc,
1546         .intr_handler           = qla4_8xxx_intr_handler,
1547         .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
1548         .reset_chip             = qla4_8xxx_isp_reset,
1549         .reset_firmware         = qla4_8xxx_stop_firmware,
1550         .queue_iocb             = qla4_8xxx_queue_iocb,
1551         .complete_iocb          = qla4_8xxx_complete_iocb,
1552         .rd_shdw_req_q_out      = qla4_8xxx_rd_shdw_req_q_out,
1553         .rd_shdw_rsp_q_in       = qla4_8xxx_rd_shdw_rsp_q_in,
1554         .get_sys_info           = qla4_8xxx_get_sys_info,
1555 };
1556
1557 uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
1558 {
1559         return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
1560 }
1561
1562 uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
1563 {
1564         return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
1565 }
1566
1567 uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
1568 {
1569         return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
1570 }
1571
1572 uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
1573 {
1574         return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
1575 }
1576
1577 /**
1578  * qla4xxx_probe_adapter - callback function to probe HBA
1579  * @pdev: pointer to pci_dev structure
1580  * @pci_device_id: pointer to pci_device entry
1581  *
1582  * This routine will probe for Qlogic 4xxx iSCSI host adapters.
1583  * It returns zero if successful. It also initializes all data necessary for
1584  * the driver.
1585  **/
1586 static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
1587                                            const struct pci_device_id *ent)
1588 {
1589         int ret = -ENODEV, status;
1590         struct Scsi_Host *host;
1591         struct scsi_qla_host *ha;
1592         uint8_t init_retry_count = 0;
1593         char buf[34];
1594         struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
1595
1596         if (pci_enable_device(pdev))
1597                 return -1;
1598
1599         host = scsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha));
1600         if (host == NULL) {
1601                 printk(KERN_WARNING
1602                        "qla4xxx: Couldn't allocate host from scsi layer!\n");
1603                 goto probe_disable_device;
1604         }
1605
1606         /* Clear our data area */
1607         ha = (struct scsi_qla_host *) host->hostdata;
1608         memset(ha, 0, sizeof(*ha));
1609
1610         /* Save the information from PCI BIOS.  */
1611         ha->pdev = pdev;
1612         ha->host = host;
1613         ha->host_no = host->host_no;
1614
1615         /* Setup Runtime configurable options */
1616         if (is_qla8022(ha)) {
1617                 ha->isp_ops = &qla4_8xxx_isp_ops;
1618                 rwlock_init(&ha->hw_lock);
1619                 ha->qdr_sn_window = -1;
1620                 ha->ddr_mn_window = -1;
1621                 ha->curr_window = 255;
1622                 ha->func_num = PCI_FUNC(ha->pdev->devfn);
1623                 nx_legacy_intr = &legacy_intr[ha->func_num];
1624                 ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
1625                 ha->nx_legacy_intr.tgt_status_reg =
1626                         nx_legacy_intr->tgt_status_reg;
1627                 ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
1628                 ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
1629         } else {
1630                 ha->isp_ops = &qla4xxx_isp_ops;
1631         }
1632
1633         /* Configure PCI I/O space. */
1634         ret = ha->isp_ops->iospace_config(ha);
1635         if (ret)
1636                 goto probe_failed_ioconfig;
1637
1638         ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
1639                    pdev->device, pdev->irq, ha->reg);
1640
1641         qla4xxx_config_dma_addressing(ha);
1642
1643         /* Initialize lists and spinlocks. */
1644         INIT_LIST_HEAD(&ha->ddb_list);
1645         INIT_LIST_HEAD(&ha->free_srb_q);
1646
1647         mutex_init(&ha->mbox_sem);
1648         init_completion(&ha->mbx_intr_comp);
1649
1650         spin_lock_init(&ha->hardware_lock);
1651
1652         /* Allocate dma buffers */
1653         if (qla4xxx_mem_alloc(ha)) {
1654                 ql4_printk(KERN_WARNING, ha,
1655                     "[ERROR] Failed to allocate memory for adapter\n");
1656
1657                 ret = -ENOMEM;
1658                 goto probe_failed;
1659         }
1660
1661         if (is_qla8022(ha))
1662                 (void) qla4_8xxx_get_flash_info(ha);
1663
1664         /*
1665          * Initialize the Host adapter request/response queues and
1666          * firmware
1667          * NOTE: interrupts enabled upon successful completion
1668          */
1669         status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1670         while ((!test_bit(AF_ONLINE, &ha->flags)) &&
1671             init_retry_count++ < MAX_INIT_RETRIES) {
1672                 DEBUG2(printk("scsi: %s: retrying adapter initialization "
1673                               "(%d)\n", __func__, init_retry_count));
1674
1675                 if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
1676                         continue;
1677
1678                 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1679         }
1680
1681         if (!test_bit(AF_ONLINE, &ha->flags)) {
1682                 ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
1683
1684                 ret = -ENODEV;
1685                 goto probe_failed;
1686         }
1687
1688         host->cmd_per_lun = 3;
1689         host->max_channel = 0;
1690         host->max_lun = MAX_LUNS - 1;
1691         host->max_id = MAX_TARGETS;
1692         host->max_cmd_len = IOCB_MAX_CDB_LEN;
1693         host->can_queue = MAX_SRBS ;
1694         host->transportt = qla4xxx_scsi_transport;
1695
1696         ret = scsi_init_shared_tag_map(host, MAX_SRBS);
1697         if (ret) {
1698                 ql4_printk(KERN_WARNING, ha,
1699                     "scsi_init_shared_tag_map failed\n");
1700                 goto probe_failed;
1701         }
1702
1703         /* Startup the kernel thread for this host adapter. */
1704         DEBUG2(printk("scsi: %s: Starting kernel thread for "
1705                       "qla4xxx_dpc\n", __func__));
1706         sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
1707         ha->dpc_thread = create_singlethread_workqueue(buf);
1708         if (!ha->dpc_thread) {
1709                 ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
1710                 ret = -ENODEV;
1711                 goto probe_failed;
1712         }
1713         INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
1714
1715         /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
1716          * (which is called indirectly by qla4xxx_initialize_adapter),
1717          * so that irqs will be registered after crbinit but before
1718          * mbx_intr_enable.
1719          */
1720         if (!is_qla8022(ha)) {
1721                 ret = qla4xxx_request_irqs(ha);
1722                 if (ret) {
1723                         ql4_printk(KERN_WARNING, ha, "Failed to reserve "
1724                             "interrupt %d already in use.\n", pdev->irq);
1725                         goto probe_failed;
1726                 }
1727         }
1728
1729         ha->isp_ops->enable_intrs(ha);
1730
1731         /* Start timer thread. */
1732         qla4xxx_start_timer(ha, qla4xxx_timer, 1);
1733
1734         set_bit(AF_INIT_DONE, &ha->flags);
1735
1736         pci_set_drvdata(pdev, ha);
1737
1738         ret = scsi_add_host(host, &pdev->dev);
1739         if (ret)
1740                 goto probe_failed;
1741
1742         printk(KERN_INFO
1743                " QLogic iSCSI HBA Driver version: %s\n"
1744                "  QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
1745                qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
1746                ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
1747                ha->patch_number, ha->build_number);
1748         scsi_scan_host(host);
1749         return 0;
1750
1751 probe_failed:
1752         qla4xxx_free_adapter(ha);
1753
1754 probe_failed_ioconfig:
1755         scsi_host_put(ha->host);
1756
1757 probe_disable_device:
1758         pci_disable_device(pdev);
1759
1760         return ret;
1761 }
1762
1763 /**
1764  * qla4xxx_remove_adapter - calback function to remove adapter.
1765  * @pci_dev: PCI device pointer
1766  **/
1767 static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
1768 {
1769         struct scsi_qla_host *ha;
1770
1771         ha = pci_get_drvdata(pdev);
1772
1773         set_bit(AF_HBA_GOING_AWAY, &ha->flags);
1774
1775         /* remove devs from iscsi_sessions to scsi_devices */
1776         qla4xxx_free_ddb_list(ha);
1777
1778         scsi_remove_host(ha->host);
1779
1780         qla4xxx_free_adapter(ha);
1781
1782         scsi_host_put(ha->host);
1783
1784         pci_disable_device(pdev);
1785         pci_set_drvdata(pdev, NULL);
1786 }
1787
1788 /**
1789  * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
1790  * @ha: HA context
1791  *
1792  * At exit, the @ha's flags.enable_64bit_addressing set to indicated
1793  * supported addressing method.
1794  */
1795 static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
1796 {
1797         int retval;
1798
1799         /* Update our PCI device dma_mask for full 64 bit mask */
1800         if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
1801                 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
1802                         dev_dbg(&ha->pdev->dev,
1803                                   "Failed to set 64 bit PCI consistent mask; "
1804                                    "using 32 bit.\n");
1805                         retval = pci_set_consistent_dma_mask(ha->pdev,
1806                                                              DMA_BIT_MASK(32));
1807                 }
1808         } else
1809                 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
1810 }
1811
1812 static int qla4xxx_slave_alloc(struct scsi_device *sdev)
1813 {
1814         struct iscsi_cls_session *sess = starget_to_session(sdev->sdev_target);
1815         struct ddb_entry *ddb = sess->dd_data;
1816
1817         sdev->hostdata = ddb;
1818         sdev->tagged_supported = 1;
1819         scsi_activate_tcq(sdev, QL4_DEF_QDEPTH);
1820         return 0;
1821 }
1822
1823 static int qla4xxx_slave_configure(struct scsi_device *sdev)
1824 {
1825         sdev->tagged_supported = 1;
1826         return 0;
1827 }
1828
1829 static void qla4xxx_slave_destroy(struct scsi_device *sdev)
1830 {
1831         scsi_deactivate_tcq(sdev, 1);
1832 }
1833
1834 /**
1835  * qla4xxx_del_from_active_array - returns an active srb
1836  * @ha: Pointer to host adapter structure.
1837  * @index: index into the active_array
1838  *
1839  * This routine removes and returns the srb at the specified index
1840  **/
1841 struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
1842     uint32_t index)
1843 {
1844         struct srb *srb = NULL;
1845         struct scsi_cmnd *cmd = NULL;
1846
1847         cmd = scsi_host_find_tag(ha->host, index);
1848         if (!cmd)
1849                 return srb;
1850
1851         srb = (struct srb *)CMD_SP(cmd);
1852         if (!srb)
1853                 return srb;
1854
1855         /* update counters */
1856         if (srb->flags & SRB_DMA_VALID) {
1857                 ha->req_q_count += srb->iocb_cnt;
1858                 ha->iocb_cnt -= srb->iocb_cnt;
1859                 if (srb->cmd)
1860                         srb->cmd->host_scribble =
1861                                 (unsigned char *)(unsigned long) MAX_SRBS;
1862         }
1863         return srb;
1864 }
1865
1866 /**
1867  * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
1868  * @ha: Pointer to host adapter structure.
1869  * @cmd: Scsi Command to wait on.
1870  *
1871  * This routine waits for the command to be returned by the Firmware
1872  * for some max time.
1873  **/
1874 static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
1875                                       struct scsi_cmnd *cmd)
1876 {
1877         int done = 0;
1878         struct srb *rp;
1879         uint32_t max_wait_time = EH_WAIT_CMD_TOV;
1880
1881         do {
1882                 /* Checking to see if its returned to OS */
1883                 rp = (struct srb *) CMD_SP(cmd);
1884                 if (rp == NULL) {
1885                         done++;
1886                         break;
1887                 }
1888
1889                 msleep(2000);
1890         } while (max_wait_time--);
1891
1892         return done;
1893 }
1894
1895 /**
1896  * qla4xxx_wait_for_hba_online - waits for HBA to come online
1897  * @ha: Pointer to host adapter structure
1898  **/
1899 static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
1900 {
1901         unsigned long wait_online;
1902
1903         wait_online = jiffies + (30 * HZ);
1904         while (time_before(jiffies, wait_online)) {
1905
1906                 if (adapter_up(ha))
1907                         return QLA_SUCCESS;
1908                 else if (ha->retry_reset_ha_cnt == 0)
1909                         return QLA_ERROR;
1910
1911                 msleep(2000);
1912         }
1913
1914         return QLA_ERROR;
1915 }
1916
1917 /**
1918  * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
1919  * @ha: pointer to HBA
1920  * @t: target id
1921  * @l: lun id
1922  *
1923  * This function waits for all outstanding commands to a lun to complete. It
1924  * returns 0 if all pending commands are returned and 1 otherwise.
1925  **/
1926 static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
1927                                         struct scsi_target *stgt,
1928                                         struct scsi_device *sdev)
1929 {
1930         int cnt;
1931         int status = 0;
1932         struct scsi_cmnd *cmd;
1933
1934         /*
1935          * Waiting for all commands for the designated target or dev
1936          * in the active array
1937          */
1938         for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
1939                 cmd = scsi_host_find_tag(ha->host, cnt);
1940                 if (cmd && stgt == scsi_target(cmd->device) &&
1941                     (!sdev || sdev == cmd->device)) {
1942                         if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
1943                                 status++;
1944                                 break;
1945                         }
1946                 }
1947         }
1948         return status;
1949 }
1950
1951 /**
1952  * qla4xxx_eh_abort - callback for abort task.
1953  * @cmd: Pointer to Linux's SCSI command structure
1954  *
1955  * This routine is called by the Linux OS to abort the specified
1956  * command.
1957  **/
1958 static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
1959 {
1960         struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
1961         unsigned int id = cmd->device->id;
1962         unsigned int lun = cmd->device->lun;
1963         unsigned long serial = cmd->serial_number;
1964         struct srb *srb = NULL;
1965         int ret = SUCCESS;
1966         int wait = 0;
1967
1968         ql4_printk(KERN_INFO, ha,
1969             "scsi%ld:%d:%d: Abort command issued cmd=%p, pid=%ld\n",
1970             ha->host_no, id, lun, cmd, serial);
1971
1972         srb = (struct srb *) CMD_SP(cmd);
1973
1974         if (!srb)
1975                 return SUCCESS;
1976
1977         kref_get(&srb->srb_ref);
1978
1979         if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
1980                 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
1981                     ha->host_no, id, lun));
1982                 ret = FAILED;
1983         } else {
1984                 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
1985                     ha->host_no, id, lun));
1986                 wait = 1;
1987         }
1988
1989         kref_put(&srb->srb_ref, qla4xxx_srb_compl);
1990
1991         /* Wait for command to complete */
1992         if (wait) {
1993                 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
1994                         DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
1995                             ha->host_no, id, lun));
1996                         ret = FAILED;
1997                 }
1998         }
1999
2000         ql4_printk(KERN_INFO, ha,
2001             "scsi%ld:%d:%d: Abort command - %s\n",
2002             ha->host_no, id, lun, (ret == SUCCESS) ? "succeded" : "failed");
2003
2004         return ret;
2005 }
2006
2007 /**
2008  * qla4xxx_eh_device_reset - callback for target reset.
2009  * @cmd: Pointer to Linux's SCSI command structure
2010  *
2011  * This routine is called by the Linux OS to reset all luns on the
2012  * specified target.
2013  **/
2014 static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
2015 {
2016         struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2017         struct ddb_entry *ddb_entry = cmd->device->hostdata;
2018         int ret = FAILED, stat;
2019
2020         if (!ddb_entry)
2021                 return ret;
2022
2023         ql4_printk(KERN_INFO, ha,
2024                    "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
2025                    cmd->device->channel, cmd->device->id, cmd->device->lun);
2026
2027         DEBUG2(printk(KERN_INFO
2028                       "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
2029                       "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
2030                       cmd, jiffies, cmd->request->timeout / HZ,
2031                       ha->dpc_flags, cmd->result, cmd->allowed));
2032
2033         /* FIXME: wait for hba to go online */
2034         stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
2035         if (stat != QLA_SUCCESS) {
2036                 ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
2037                 goto eh_dev_reset_done;
2038         }
2039
2040         if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2041                                          cmd->device)) {
2042                 ql4_printk(KERN_INFO, ha,
2043                            "DEVICE RESET FAILED - waiting for "
2044                            "commands.\n");
2045                 goto eh_dev_reset_done;
2046         }
2047
2048         /* Send marker. */
2049         if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2050                 MM_LUN_RESET) != QLA_SUCCESS)
2051                 goto eh_dev_reset_done;
2052
2053         ql4_printk(KERN_INFO, ha,
2054                    "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
2055                    ha->host_no, cmd->device->channel, cmd->device->id,
2056                    cmd->device->lun);
2057
2058         ret = SUCCESS;
2059
2060 eh_dev_reset_done:
2061
2062         return ret;
2063 }
2064
2065 /**
2066  * qla4xxx_eh_target_reset - callback for target reset.
2067  * @cmd: Pointer to Linux's SCSI command structure
2068  *
2069  * This routine is called by the Linux OS to reset the target.
2070  **/
2071 static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
2072 {
2073         struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2074         struct ddb_entry *ddb_entry = cmd->device->hostdata;
2075         int stat;
2076
2077         if (!ddb_entry)
2078                 return FAILED;
2079
2080         starget_printk(KERN_INFO, scsi_target(cmd->device),
2081                        "WARM TARGET RESET ISSUED.\n");
2082
2083         DEBUG2(printk(KERN_INFO
2084                       "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
2085                       "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
2086                       ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
2087                       ha->dpc_flags, cmd->result, cmd->allowed));
2088
2089         stat = qla4xxx_reset_target(ha, ddb_entry);
2090         if (stat != QLA_SUCCESS) {
2091                 starget_printk(KERN_INFO, scsi_target(cmd->device),
2092                                "WARM TARGET RESET FAILED.\n");
2093                 return FAILED;
2094         }
2095
2096         if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2097                                          NULL)) {
2098                 starget_printk(KERN_INFO, scsi_target(cmd->device),
2099                                "WARM TARGET DEVICE RESET FAILED - "
2100                                "waiting for commands.\n");
2101                 return FAILED;
2102         }
2103
2104         /* Send marker. */
2105         if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2106                 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
2107                 starget_printk(KERN_INFO, scsi_target(cmd->device),
2108                                "WARM TARGET DEVICE RESET FAILED - "
2109                                "marker iocb failed.\n");
2110                 return FAILED;
2111         }
2112
2113         starget_printk(KERN_INFO, scsi_target(cmd->device),
2114                        "WARM TARGET RESET SUCCEEDED.\n");
2115         return SUCCESS;
2116 }
2117
2118 /**
2119  * qla4xxx_eh_host_reset - kernel callback
2120  * @cmd: Pointer to Linux's SCSI command structure
2121  *
2122  * This routine is invoked by the Linux kernel to perform fatal error
2123  * recovery on the specified adapter.
2124  **/
2125 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
2126 {
2127         int return_status = FAILED;
2128         struct scsi_qla_host *ha;
2129
2130         ha = (struct scsi_qla_host *) cmd->device->host->hostdata;
2131
2132         if (ql4xdontresethba) {
2133                 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
2134                      ha->host_no, __func__));
2135                 return FAILED;
2136         }
2137
2138         ql4_printk(KERN_INFO, ha,
2139                    "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
2140                    cmd->device->channel, cmd->device->id, cmd->device->lun);
2141
2142         if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
2143                 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host.  Adapter "
2144                               "DEAD.\n", ha->host_no, cmd->device->channel,
2145                               __func__));
2146
2147                 return FAILED;
2148         }
2149
2150         if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
2151                 if (is_qla8022(ha))
2152                         set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2153                 else
2154                         set_bit(DPC_RESET_HA, &ha->dpc_flags);
2155         }
2156
2157         if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
2158                 return_status = SUCCESS;
2159
2160         ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
2161                    return_status == FAILED ? "FAILED" : "SUCCEDED");
2162
2163         return return_status;
2164 }
2165
2166 static struct pci_device_id qla4xxx_pci_tbl[] = {
2167         {
2168                 .vendor         = PCI_VENDOR_ID_QLOGIC,
2169                 .device         = PCI_DEVICE_ID_QLOGIC_ISP4010,
2170                 .subvendor      = PCI_ANY_ID,
2171                 .subdevice      = PCI_ANY_ID,
2172         },
2173         {
2174                 .vendor         = PCI_VENDOR_ID_QLOGIC,
2175                 .device         = PCI_DEVICE_ID_QLOGIC_ISP4022,
2176                 .subvendor      = PCI_ANY_ID,
2177                 .subdevice      = PCI_ANY_ID,
2178         },
2179         {
2180                 .vendor         = PCI_VENDOR_ID_QLOGIC,
2181                 .device         = PCI_DEVICE_ID_QLOGIC_ISP4032,
2182                 .subvendor      = PCI_ANY_ID,
2183                 .subdevice      = PCI_ANY_ID,
2184         },
2185         {
2186                 .vendor         = PCI_VENDOR_ID_QLOGIC,
2187                 .device         = PCI_DEVICE_ID_QLOGIC_ISP8022,
2188                 .subvendor      = PCI_ANY_ID,
2189                 .subdevice      = PCI_ANY_ID,
2190         },
2191         {0, 0},
2192 };
2193 MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
2194
2195 static struct pci_driver qla4xxx_pci_driver = {
2196         .name           = DRIVER_NAME,
2197         .id_table       = qla4xxx_pci_tbl,
2198         .probe          = qla4xxx_probe_adapter,
2199         .remove         = qla4xxx_remove_adapter,
2200 };
2201
2202 static int __init qla4xxx_module_init(void)
2203 {
2204         int ret;
2205
2206         /* Allocate cache for SRBs. */
2207         srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
2208                                        SLAB_HWCACHE_ALIGN, NULL);
2209         if (srb_cachep == NULL) {
2210                 printk(KERN_ERR
2211                        "%s: Unable to allocate SRB cache..."
2212                        "Failing load!\n", DRIVER_NAME);
2213                 ret = -ENOMEM;
2214                 goto no_srp_cache;
2215         }
2216
2217         /* Derive version string. */
2218         strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
2219         if (ql4xextended_error_logging)
2220                 strcat(qla4xxx_version_str, "-debug");
2221
2222         qla4xxx_scsi_transport =
2223                 iscsi_register_transport(&qla4xxx_iscsi_transport);
2224         if (!qla4xxx_scsi_transport){
2225                 ret = -ENODEV;
2226                 goto release_srb_cache;
2227         }
2228
2229         ret = pci_register_driver(&qla4xxx_pci_driver);
2230         if (ret)
2231                 goto unregister_transport;
2232
2233         printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
2234         return 0;
2235
2236 unregister_transport:
2237         iscsi_unregister_transport(&qla4xxx_iscsi_transport);
2238 release_srb_cache:
2239         kmem_cache_destroy(srb_cachep);
2240 no_srp_cache:
2241         return ret;
2242 }
2243
2244 static void __exit qla4xxx_module_exit(void)
2245 {
2246         pci_unregister_driver(&qla4xxx_pci_driver);
2247         iscsi_unregister_transport(&qla4xxx_iscsi_transport);
2248         kmem_cache_destroy(srb_cachep);
2249 }
2250
2251 module_init(qla4xxx_module_init);
2252 module_exit(qla4xxx_module_exit);
2253
2254 MODULE_AUTHOR("QLogic Corporation");
2255 MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
2256 MODULE_LICENSE("GPL");
2257 MODULE_VERSION(QLA4XXX_DRIVER_VERSION);