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