]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/scsi/qla4xxx/ql4_os.c
d3f86646cb088b0fecbbd9edcd0b26c8b51bebd1
[linux-2.6-omap-h63xx.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
9 #include <scsi/scsi_tcq.h>
10 #include <scsi/scsicam.h>
11
12 #include "ql4_def.h"
13 #include "ql4_version.h"
14 #include "ql4_glbl.h"
15 #include "ql4_dbg.h"
16 #include "ql4_inline.h"
17
18 /*
19  * Driver version
20  */
21 static char qla4xxx_version_str[40];
22
23 /*
24  * SRB allocation cache
25  */
26 static struct kmem_cache *srb_cachep;
27
28 /*
29  * Module parameter information and variables
30  */
31 int ql4xdiscoverywait = 60;
32 module_param(ql4xdiscoverywait, int, S_IRUGO | S_IRUSR);
33 MODULE_PARM_DESC(ql4xdiscoverywait, "Discovery wait time");
34 int ql4xdontresethba = 0;
35 module_param(ql4xdontresethba, int, S_IRUGO | S_IRUSR);
36 MODULE_PARM_DESC(ql4xdontresethba,
37                  "Dont reset the HBA when the driver gets 0x8002 AEN "
38                  " default it will reset hba :0"
39                  " set to 1 to avoid resetting HBA");
40
41 int ql4xextended_error_logging = 0; /* 0 = off, 1 = log errors */
42 module_param(ql4xextended_error_logging, int, S_IRUGO | S_IRUSR);
43 MODULE_PARM_DESC(ql4xextended_error_logging,
44                  "Option to enable extended error logging, "
45                  "Default is 0 - no logging, 1 - debug logging");
46
47 int ql4_mod_unload = 0;
48
49 /*
50  * SCSI host template entry points
51  */
52 static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
53
54 /*
55  * iSCSI template entry points
56  */
57 static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
58                              enum iscsi_tgt_dscvr type, uint32_t enable,
59                              struct sockaddr *dst_addr);
60 static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
61                                   enum iscsi_param param, char *buf);
62 static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
63                                   enum iscsi_param param, char *buf);
64 static int qla4xxx_host_get_param(struct Scsi_Host *shost,
65                                   enum iscsi_host_param param, char *buf);
66 static void qla4xxx_conn_stop(struct iscsi_cls_conn *conn, int flag);
67 static int qla4xxx_conn_start(struct iscsi_cls_conn *conn);
68 static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session);
69
70 /*
71  * SCSI host template entry points
72  */
73 static int qla4xxx_queuecommand(struct scsi_cmnd *cmd,
74                                 void (*done) (struct scsi_cmnd *));
75 static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
76 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
77 static int qla4xxx_slave_alloc(struct scsi_device *device);
78 static int qla4xxx_slave_configure(struct scsi_device *device);
79 static void qla4xxx_slave_destroy(struct scsi_device *sdev);
80
81 static struct scsi_host_template qla4xxx_driver_template = {
82         .module                 = THIS_MODULE,
83         .name                   = DRIVER_NAME,
84         .proc_name              = DRIVER_NAME,
85         .queuecommand           = qla4xxx_queuecommand,
86
87         .eh_device_reset_handler = qla4xxx_eh_device_reset,
88         .eh_host_reset_handler  = qla4xxx_eh_host_reset,
89
90         .slave_configure        = qla4xxx_slave_configure,
91         .slave_alloc            = qla4xxx_slave_alloc,
92         .slave_destroy          = qla4xxx_slave_destroy,
93
94         .this_id                = -1,
95         .cmd_per_lun            = 3,
96         .use_clustering         = ENABLE_CLUSTERING,
97         .use_sg_chaining        = ENABLE_SG_CHAINING,
98         .sg_tablesize           = SG_ALL,
99
100         .max_sectors            = 0xFFFF,
101 };
102
103 static struct iscsi_transport qla4xxx_iscsi_transport = {
104         .owner                  = THIS_MODULE,
105         .name                   = DRIVER_NAME,
106         .caps                   = CAP_FW_DB | CAP_SENDTARGETS_OFFLOAD |
107                                   CAP_DATA_PATH_OFFLOAD,
108         .param_mask             = ISCSI_CONN_PORT | ISCSI_CONN_ADDRESS |
109                                   ISCSI_TARGET_NAME | ISCSI_TPGT,
110         .host_param_mask        = ISCSI_HOST_HWADDRESS |
111                                   ISCSI_HOST_IPADDRESS |
112                                   ISCSI_HOST_INITIATOR_NAME,
113         .sessiondata_size       = sizeof(struct ddb_entry),
114         .host_template          = &qla4xxx_driver_template,
115
116         .tgt_dscvr              = qla4xxx_tgt_dscvr,
117         .get_conn_param         = qla4xxx_conn_get_param,
118         .get_session_param      = qla4xxx_sess_get_param,
119         .get_host_param         = qla4xxx_host_get_param,
120         .start_conn             = qla4xxx_conn_start,
121         .stop_conn              = qla4xxx_conn_stop,
122         .session_recovery_timedout = qla4xxx_recovery_timedout,
123 };
124
125 static struct scsi_transport_template *qla4xxx_scsi_transport;
126
127 static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session)
128 {
129         struct ddb_entry *ddb_entry = session->dd_data;
130         struct scsi_qla_host *ha = ddb_entry->ha;
131
132         DEBUG2(printk("scsi%ld: %s: index [%d] port down retry count of (%d) "
133                       "secs exhausted, marking device DEAD.\n", ha->host_no,
134                       __func__, ddb_entry->fw_ddb_index,
135                       ha->port_down_retry_count));
136
137         atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
138
139         DEBUG2(printk("scsi%ld: %s: scheduling dpc routine - dpc flags = "
140                       "0x%lx\n", ha->host_no, __func__, ha->dpc_flags));
141         queue_work(ha->dpc_thread, &ha->dpc_work);
142 }
143
144 static int qla4xxx_conn_start(struct iscsi_cls_conn *conn)
145 {
146         struct iscsi_cls_session *session;
147         struct ddb_entry *ddb_entry;
148
149         session = iscsi_dev_to_session(conn->dev.parent);
150         ddb_entry = session->dd_data;
151
152         DEBUG2(printk("scsi%ld: %s: index [%d] starting conn\n",
153                       ddb_entry->ha->host_no, __func__,
154                       ddb_entry->fw_ddb_index));
155         iscsi_unblock_session(session);
156         return 0;
157 }
158
159 static void qla4xxx_conn_stop(struct iscsi_cls_conn *conn, int flag)
160 {
161         struct iscsi_cls_session *session;
162         struct ddb_entry *ddb_entry;
163
164         session = iscsi_dev_to_session(conn->dev.parent);
165         ddb_entry = session->dd_data;
166
167         DEBUG2(printk("scsi%ld: %s: index [%d] stopping conn\n",
168                       ddb_entry->ha->host_no, __func__,
169                       ddb_entry->fw_ddb_index));
170         if (flag == STOP_CONN_RECOVER)
171                 iscsi_block_session(session);
172         else
173                 printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag);
174 }
175
176 static int qla4xxx_host_get_param(struct Scsi_Host *shost,
177                                   enum iscsi_host_param param, char *buf)
178 {
179         struct scsi_qla_host *ha = to_qla_host(shost);
180         int len;
181
182         switch (param) {
183         case ISCSI_HOST_PARAM_HWADDRESS:
184                 len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
185                 break;
186         case ISCSI_HOST_PARAM_IPADDRESS:
187                 len = sprintf(buf, "%d.%d.%d.%d\n", ha->ip_address[0],
188                               ha->ip_address[1], ha->ip_address[2],
189                               ha->ip_address[3]);
190                 break;
191         case ISCSI_HOST_PARAM_INITIATOR_NAME:
192                 len = sprintf(buf, "%s\n", ha->name_string);
193                 break;
194         default:
195                 return -ENOSYS;
196         }
197
198         return len;
199 }
200
201 static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
202                                   enum iscsi_param param, char *buf)
203 {
204         struct ddb_entry *ddb_entry = sess->dd_data;
205         int len;
206
207         switch (param) {
208         case ISCSI_PARAM_TARGET_NAME:
209                 len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
210                                ddb_entry->iscsi_name);
211                 break;
212         case ISCSI_PARAM_TPGT:
213                 len = sprintf(buf, "%u\n", ddb_entry->tpgt);
214                 break;
215         default:
216                 return -ENOSYS;
217         }
218
219         return len;
220 }
221
222 static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
223                                   enum iscsi_param param, char *buf)
224 {
225         struct iscsi_cls_session *session;
226         struct ddb_entry *ddb_entry;
227         int len;
228
229         session = iscsi_dev_to_session(conn->dev.parent);
230         ddb_entry = session->dd_data;
231
232         switch (param) {
233         case ISCSI_PARAM_CONN_PORT:
234                 len = sprintf(buf, "%hu\n", ddb_entry->port);
235                 break;
236         case ISCSI_PARAM_CONN_ADDRESS:
237                 /* TODO: what are the ipv6 bits */
238                 len = sprintf(buf, "%u.%u.%u.%u\n",
239                               NIPQUAD(ddb_entry->ip_addr));
240                 break;
241         default:
242                 return -ENOSYS;
243         }
244
245         return len;
246 }
247
248 static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
249                              enum iscsi_tgt_dscvr type, uint32_t enable,
250                              struct sockaddr *dst_addr)
251 {
252         struct scsi_qla_host *ha;
253         struct sockaddr_in *addr;
254         struct sockaddr_in6 *addr6;
255         int ret = 0;
256
257         ha = (struct scsi_qla_host *) shost->hostdata;
258
259         switch (type) {
260         case ISCSI_TGT_DSCVR_SEND_TARGETS:
261                 if (dst_addr->sa_family == AF_INET) {
262                         addr = (struct sockaddr_in *)dst_addr;
263                         if (qla4xxx_send_tgts(ha, (char *)&addr->sin_addr,
264                                               addr->sin_port) != QLA_SUCCESS)
265                                 ret = -EIO;
266                 } else if (dst_addr->sa_family == AF_INET6) {
267                         /*
268                          * TODO: fix qla4xxx_send_tgts
269                          */
270                         addr6 = (struct sockaddr_in6 *)dst_addr;
271                         if (qla4xxx_send_tgts(ha, (char *)&addr6->sin6_addr,
272                                               addr6->sin6_port) != QLA_SUCCESS)
273                                 ret = -EIO;
274                 } else
275                         ret = -ENOSYS;
276                 break;
277         default:
278                 ret = -ENOSYS;
279         }
280         return ret;
281 }
282
283 void qla4xxx_destroy_sess(struct ddb_entry *ddb_entry)
284 {
285         if (!ddb_entry->sess)
286                 return;
287
288         if (ddb_entry->conn) {
289                 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
290                 iscsi_remove_session(ddb_entry->sess);
291         }
292         iscsi_free_session(ddb_entry->sess);
293 }
294
295 int qla4xxx_add_sess(struct ddb_entry *ddb_entry)
296 {
297         int err;
298
299         ddb_entry->sess->recovery_tmo = ddb_entry->ha->port_down_retry_count;
300         err = iscsi_add_session(ddb_entry->sess, ddb_entry->fw_ddb_index);
301         if (err) {
302                 DEBUG2(printk(KERN_ERR "Could not add session.\n"));
303                 return err;
304         }
305
306         ddb_entry->conn = iscsi_create_conn(ddb_entry->sess, 0);
307         if (!ddb_entry->conn) {
308                 iscsi_remove_session(ddb_entry->sess);
309                 DEBUG2(printk(KERN_ERR "Could not add connection.\n"));
310                 return -ENOMEM;
311         }
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         if (!sess)
322                 return NULL;
323
324         ddb_entry = sess->dd_data;
325         memset(ddb_entry, 0, sizeof(*ddb_entry));
326         ddb_entry->ha = ha;
327         ddb_entry->sess = sess;
328         return ddb_entry;
329 }
330
331 /*
332  * Timer routines
333  */
334
335 static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
336                                 unsigned long interval)
337 {
338         DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
339                      __func__, ha->host->host_no));
340         init_timer(&ha->timer);
341         ha->timer.expires = jiffies + interval * HZ;
342         ha->timer.data = (unsigned long)ha;
343         ha->timer.function = (void (*)(unsigned long))func;
344         add_timer(&ha->timer);
345         ha->timer_active = 1;
346 }
347
348 static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
349 {
350         del_timer_sync(&ha->timer);
351         ha->timer_active = 0;
352 }
353
354 /***
355  * qla4xxx_mark_device_missing - mark a device as missing.
356  * @ha: Pointer to host adapter structure.
357  * @ddb_entry: Pointer to device database entry
358  *
359  * This routine marks a device missing and resets the relogin retry count.
360  **/
361 void qla4xxx_mark_device_missing(struct scsi_qla_host *ha,
362                                  struct ddb_entry *ddb_entry)
363 {
364         atomic_set(&ddb_entry->state, DDB_STATE_MISSING);
365         DEBUG3(printk("scsi%d:%d:%d: index [%d] marked MISSING\n",
366                       ha->host_no, ddb_entry->bus, ddb_entry->target,
367                       ddb_entry->fw_ddb_index));
368         iscsi_conn_error(ddb_entry->conn, ISCSI_ERR_CONN_FAILED);
369 }
370
371 static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
372                                        struct ddb_entry *ddb_entry,
373                                        struct scsi_cmnd *cmd,
374                                        void (*done)(struct scsi_cmnd *))
375 {
376         struct srb *srb;
377
378         srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
379         if (!srb)
380                 return srb;
381
382         atomic_set(&srb->ref_count, 1);
383         srb->ha = ha;
384         srb->ddb = ddb_entry;
385         srb->cmd = cmd;
386         srb->flags = 0;
387         cmd->SCp.ptr = (void *)srb;
388         cmd->scsi_done = done;
389
390         return srb;
391 }
392
393 static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
394 {
395         struct scsi_cmnd *cmd = srb->cmd;
396
397         if (srb->flags & SRB_DMA_VALID) {
398                 scsi_dma_unmap(cmd);
399                 srb->flags &= ~SRB_DMA_VALID;
400         }
401         cmd->SCp.ptr = NULL;
402 }
403
404 void qla4xxx_srb_compl(struct scsi_qla_host *ha, struct srb *srb)
405 {
406         struct scsi_cmnd *cmd = srb->cmd;
407
408         qla4xxx_srb_free_dma(ha, srb);
409
410         mempool_free(srb, ha->srb_mempool);
411
412         cmd->scsi_done(cmd);
413 }
414
415 /**
416  * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
417  * @cmd: Pointer to Linux's SCSI command structure
418  * @done_fn: Function that the driver calls to notify the SCSI mid-layer
419  *      that the command has been processed.
420  *
421  * Remarks:
422  * This routine is invoked by Linux to send a SCSI command to the driver.
423  * The mid-level driver tries to ensure that queuecommand never gets
424  * invoked concurrently with itself or the interrupt handler (although
425  * the interrupt handler may call this routine as part of request-
426  * completion handling).   Unfortunely, it sometimes calls the scheduler
427  * in interrupt context which is a big NO! NO!.
428  **/
429 static int qla4xxx_queuecommand(struct scsi_cmnd *cmd,
430                                 void (*done)(struct scsi_cmnd *))
431 {
432         struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
433         struct ddb_entry *ddb_entry = cmd->device->hostdata;
434         struct srb *srb;
435         int rval;
436
437         if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
438                 if (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD) {
439                         cmd->result = DID_NO_CONNECT << 16;
440                         goto qc_fail_command;
441                 }
442                 goto qc_host_busy;
443         }
444
445         if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags))
446                 goto qc_host_busy;
447
448         spin_unlock_irq(ha->host->host_lock);
449
450         srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd, done);
451         if (!srb)
452                 goto qc_host_busy_lock;
453
454         rval = qla4xxx_send_command_to_isp(ha, srb);
455         if (rval != QLA_SUCCESS)
456                 goto qc_host_busy_free_sp;
457
458         spin_lock_irq(ha->host->host_lock);
459         return 0;
460
461 qc_host_busy_free_sp:
462         qla4xxx_srb_free_dma(ha, srb);
463         mempool_free(srb, ha->srb_mempool);
464
465 qc_host_busy_lock:
466         spin_lock_irq(ha->host->host_lock);
467
468 qc_host_busy:
469         return SCSI_MLQUEUE_HOST_BUSY;
470
471 qc_fail_command:
472         done(cmd);
473
474         return 0;
475 }
476
477 /**
478  * qla4xxx_mem_free - frees memory allocated to adapter
479  * @ha: Pointer to host adapter structure.
480  *
481  * Frees memory previously allocated by qla4xxx_mem_alloc
482  **/
483 static void qla4xxx_mem_free(struct scsi_qla_host *ha)
484 {
485         if (ha->queues)
486                 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
487                                   ha->queues_dma);
488
489         ha->queues_len = 0;
490         ha->queues = NULL;
491         ha->queues_dma = 0;
492         ha->request_ring = NULL;
493         ha->request_dma = 0;
494         ha->response_ring = NULL;
495         ha->response_dma = 0;
496         ha->shadow_regs = NULL;
497         ha->shadow_regs_dma = 0;
498
499         /* Free srb pool. */
500         if (ha->srb_mempool)
501                 mempool_destroy(ha->srb_mempool);
502
503         ha->srb_mempool = NULL;
504
505         /* release io space registers  */
506         if (ha->reg)
507                 iounmap(ha->reg);
508         pci_release_regions(ha->pdev);
509 }
510
511 /**
512  * qla4xxx_mem_alloc - allocates memory for use by adapter.
513  * @ha: Pointer to host adapter structure
514  *
515  * Allocates DMA memory for request and response queues. Also allocates memory
516  * for srbs.
517  **/
518 static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
519 {
520         unsigned long align;
521
522         /* Allocate contiguous block of DMA memory for queues. */
523         ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
524                           (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
525                           sizeof(struct shadow_regs) +
526                           MEM_ALIGN_VALUE +
527                           (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
528         ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
529                                         &ha->queues_dma, GFP_KERNEL);
530         if (ha->queues == NULL) {
531                 dev_warn(&ha->pdev->dev,
532                         "Memory Allocation failed - queues.\n");
533
534                 goto mem_alloc_error_exit;
535         }
536         memset(ha->queues, 0, ha->queues_len);
537
538         /*
539          * As per RISC alignment requirements -- the bus-address must be a
540          * multiple of the request-ring size (in bytes).
541          */
542         align = 0;
543         if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
544                 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
545                                            (MEM_ALIGN_VALUE - 1));
546
547         /* Update request and response queue pointers. */
548         ha->request_dma = ha->queues_dma + align;
549         ha->request_ring = (struct queue_entry *) (ha->queues + align);
550         ha->response_dma = ha->queues_dma + align +
551                 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
552         ha->response_ring = (struct queue_entry *) (ha->queues + align +
553                                                     (REQUEST_QUEUE_DEPTH *
554                                                      QUEUE_SIZE));
555         ha->shadow_regs_dma = ha->queues_dma + align +
556                 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
557                 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
558         ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
559                                                   (REQUEST_QUEUE_DEPTH *
560                                                    QUEUE_SIZE) +
561                                                   (RESPONSE_QUEUE_DEPTH *
562                                                    QUEUE_SIZE));
563
564         /* Allocate memory for srb pool. */
565         ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
566                                          mempool_free_slab, srb_cachep);
567         if (ha->srb_mempool == NULL) {
568                 dev_warn(&ha->pdev->dev,
569                         "Memory Allocation failed - SRB Pool.\n");
570
571                 goto mem_alloc_error_exit;
572         }
573
574         return QLA_SUCCESS;
575
576 mem_alloc_error_exit:
577         qla4xxx_mem_free(ha);
578         return QLA_ERROR;
579 }
580
581 /**
582  * qla4xxx_timer - checks every second for work to do.
583  * @ha: Pointer to host adapter structure.
584  **/
585 static void qla4xxx_timer(struct scsi_qla_host *ha)
586 {
587         struct ddb_entry *ddb_entry, *dtemp;
588         int start_dpc = 0;
589
590         /* Search for relogin's to time-out and port down retry. */
591         list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
592                 /* Count down time between sending relogins */
593                 if (adapter_up(ha) &&
594                     !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
595                     atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
596                         if (atomic_read(&ddb_entry->retry_relogin_timer) !=
597                             INVALID_ENTRY) {
598                                 if (atomic_read(&ddb_entry->retry_relogin_timer)
599                                                 == 0) {
600                                         atomic_set(&ddb_entry->
601                                                 retry_relogin_timer,
602                                                 INVALID_ENTRY);
603                                         set_bit(DPC_RELOGIN_DEVICE,
604                                                 &ha->dpc_flags);
605                                         set_bit(DF_RELOGIN, &ddb_entry->flags);
606                                         DEBUG2(printk("scsi%ld: %s: index [%d]"
607                                                       " login device\n",
608                                                       ha->host_no, __func__,
609                                                       ddb_entry->fw_ddb_index));
610                                 } else
611                                         atomic_dec(&ddb_entry->
612                                                         retry_relogin_timer);
613                         }
614                 }
615
616                 /* Wait for relogin to timeout */
617                 if (atomic_read(&ddb_entry->relogin_timer) &&
618                     (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
619                         /*
620                          * If the relogin times out and the device is
621                          * still NOT ONLINE then try and relogin again.
622                          */
623                         if (atomic_read(&ddb_entry->state) !=
624                             DDB_STATE_ONLINE &&
625                             ddb_entry->fw_ddb_device_state ==
626                             DDB_DS_SESSION_FAILED) {
627                                 /* Reset retry relogin timer */
628                                 atomic_inc(&ddb_entry->relogin_retry_count);
629                                 DEBUG2(printk("scsi%ld: index[%d] relogin"
630                                               " timed out-retrying"
631                                               " relogin (%d)\n",
632                                               ha->host_no,
633                                               ddb_entry->fw_ddb_index,
634                                               atomic_read(&ddb_entry->
635                                                           relogin_retry_count))
636                                         );
637                                 start_dpc++;
638                                 DEBUG(printk("scsi%ld:%d:%d: index [%d] "
639                                              "initate relogin after"
640                                              " %d seconds\n",
641                                              ha->host_no, ddb_entry->bus,
642                                              ddb_entry->target,
643                                              ddb_entry->fw_ddb_index,
644                                              ddb_entry->default_time2wait + 4)
645                                         );
646
647                                 atomic_set(&ddb_entry->retry_relogin_timer,
648                                            ddb_entry->default_time2wait + 4);
649                         }
650                 }
651         }
652
653         /* Check for heartbeat interval. */
654         if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
655             ha->heartbeat_interval != 0) {
656                 ha->seconds_since_last_heartbeat++;
657                 if (ha->seconds_since_last_heartbeat >
658                     ha->heartbeat_interval + 2)
659                         set_bit(DPC_RESET_HA, &ha->dpc_flags);
660         }
661
662
663         /* Wakeup the dpc routine for this adapter, if needed. */
664         if ((start_dpc ||
665              test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
666              test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
667              test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
668              test_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags) ||
669              test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
670              test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
671              test_bit(DPC_AEN, &ha->dpc_flags)) &&
672              ha->dpc_thread) {
673                 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
674                               " - dpc flags = 0x%lx\n",
675                               ha->host_no, __func__, ha->dpc_flags));
676                 queue_work(ha->dpc_thread, &ha->dpc_work);
677         }
678
679         /* Reschedule timer thread to call us back in one second */
680         mod_timer(&ha->timer, jiffies + HZ);
681
682         DEBUG2(ha->seconds_since_last_intr++);
683 }
684
685 /**
686  * qla4xxx_cmd_wait - waits for all outstanding commands to complete
687  * @ha: Pointer to host adapter structure.
688  *
689  * This routine stalls the driver until all outstanding commands are returned.
690  * Caller must release the Hardware Lock prior to calling this routine.
691  **/
692 static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
693 {
694         uint32_t index = 0;
695         int stat = QLA_SUCCESS;
696         unsigned long flags;
697         struct scsi_cmnd *cmd;
698         int wait_cnt = WAIT_CMD_TOV;    /*
699                                          * Initialized for 30 seconds as we
700                                          * expect all commands to retuned
701                                          * ASAP.
702                                          */
703
704         while (wait_cnt) {
705                 spin_lock_irqsave(&ha->hardware_lock, flags);
706                 /* Find a command that hasn't completed. */
707                 for (index = 0; index < ha->host->can_queue; index++) {
708                         cmd = scsi_host_find_tag(ha->host, index);
709                         if (cmd != NULL)
710                                 break;
711                 }
712                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
713
714                 /* If No Commands are pending, wait is complete */
715                 if (index == ha->host->can_queue) {
716                         break;
717                 }
718
719                 /* If we timed out on waiting for commands to come back
720                  * return ERROR.
721                  */
722                 wait_cnt--;
723                 if (wait_cnt == 0)
724                         stat = QLA_ERROR;
725                 else {
726                         msleep(1000);
727                 }
728         }                       /* End of While (wait_cnt) */
729
730         return stat;
731 }
732
733 void qla4xxx_hw_reset(struct scsi_qla_host *ha)
734 {
735         uint32_t ctrl_status;
736         unsigned long flags = 0;
737
738         DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
739
740         spin_lock_irqsave(&ha->hardware_lock, flags);
741
742         /*
743          * If the SCSI Reset Interrupt bit is set, clear it.
744          * Otherwise, the Soft Reset won't work.
745          */
746         ctrl_status = readw(&ha->reg->ctrl_status);
747         if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
748                 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
749
750         /* Issue Soft Reset */
751         writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
752         readl(&ha->reg->ctrl_status);
753
754         spin_unlock_irqrestore(&ha->hardware_lock, flags);
755 }
756
757 /**
758  * qla4xxx_soft_reset - performs soft reset.
759  * @ha: Pointer to host adapter structure.
760  **/
761 int qla4xxx_soft_reset(struct scsi_qla_host *ha)
762 {
763         uint32_t max_wait_time;
764         unsigned long flags = 0;
765         int status = QLA_ERROR;
766         uint32_t ctrl_status;
767
768         qla4xxx_hw_reset(ha);
769
770         /* Wait until the Network Reset Intr bit is cleared */
771         max_wait_time = RESET_INTR_TOV;
772         do {
773                 spin_lock_irqsave(&ha->hardware_lock, flags);
774                 ctrl_status = readw(&ha->reg->ctrl_status);
775                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
776
777                 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
778                         break;
779
780                 msleep(1000);
781         } while ((--max_wait_time));
782
783         if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
784                 DEBUG2(printk(KERN_WARNING
785                               "scsi%ld: Network Reset Intr not cleared by "
786                               "Network function, clearing it now!\n",
787                               ha->host_no));
788                 spin_lock_irqsave(&ha->hardware_lock, flags);
789                 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
790                 readl(&ha->reg->ctrl_status);
791                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
792         }
793
794         /* Wait until the firmware tells us the Soft Reset is done */
795         max_wait_time = SOFT_RESET_TOV;
796         do {
797                 spin_lock_irqsave(&ha->hardware_lock, flags);
798                 ctrl_status = readw(&ha->reg->ctrl_status);
799                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
800
801                 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
802                         status = QLA_SUCCESS;
803                         break;
804                 }
805
806                 msleep(1000);
807         } while ((--max_wait_time));
808
809         /*
810          * Also, make sure that the SCSI Reset Interrupt bit has been cleared
811          * after the soft reset has taken place.
812          */
813         spin_lock_irqsave(&ha->hardware_lock, flags);
814         ctrl_status = readw(&ha->reg->ctrl_status);
815         if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
816                 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
817                 readl(&ha->reg->ctrl_status);
818         }
819         spin_unlock_irqrestore(&ha->hardware_lock, flags);
820
821         /* If soft reset fails then most probably the bios on other
822          * function is also enabled.
823          * Since the initialization is sequential the other fn
824          * wont be able to acknowledge the soft reset.
825          * Issue a force soft reset to workaround this scenario.
826          */
827         if (max_wait_time == 0) {
828                 /* Issue Force Soft Reset */
829                 spin_lock_irqsave(&ha->hardware_lock, flags);
830                 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
831                 readl(&ha->reg->ctrl_status);
832                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
833                 /* Wait until the firmware tells us the Soft Reset is done */
834                 max_wait_time = SOFT_RESET_TOV;
835                 do {
836                         spin_lock_irqsave(&ha->hardware_lock, flags);
837                         ctrl_status = readw(&ha->reg->ctrl_status);
838                         spin_unlock_irqrestore(&ha->hardware_lock, flags);
839
840                         if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
841                                 status = QLA_SUCCESS;
842                                 break;
843                         }
844
845                         msleep(1000);
846                 } while ((--max_wait_time));
847         }
848
849         return status;
850 }
851
852 /**
853  * qla4xxx_flush_active_srbs - returns all outstanding i/o requests to O.S.
854  * @ha: Pointer to host adapter structure.
855  *
856  * This routine is called just prior to a HARD RESET to return all
857  * outstanding commands back to the Operating System.
858  * Caller should make sure that the following locks are released
859  * before this calling routine: Hardware lock, and io_request_lock.
860  **/
861 static void qla4xxx_flush_active_srbs(struct scsi_qla_host *ha)
862 {
863         struct srb *srb;
864         int i;
865         unsigned long flags;
866
867         spin_lock_irqsave(&ha->hardware_lock, flags);
868         for (i = 0; i < ha->host->can_queue; i++) {
869                 srb = qla4xxx_del_from_active_array(ha, i);
870                 if (srb != NULL) {
871                         srb->cmd->result = DID_RESET << 16;
872                         qla4xxx_srb_compl(ha, srb);
873                 }
874         }
875         spin_unlock_irqrestore(&ha->hardware_lock, flags);
876
877 }
878
879 /**
880  * qla4xxx_recover_adapter - recovers adapter after a fatal error
881  * @ha: Pointer to host adapter structure.
882  * @renew_ddb_list: Indicates what to do with the adapter's ddb list
883  *      after adapter recovery has completed.
884  *      0=preserve ddb list, 1=destroy and rebuild ddb list
885  **/
886 static int qla4xxx_recover_adapter(struct scsi_qla_host *ha,
887                                 uint8_t renew_ddb_list)
888 {
889         int status;
890
891         /* Stall incoming I/O until we are done */
892         clear_bit(AF_ONLINE, &ha->flags);
893         DEBUG2(printk("scsi%ld: %s calling qla4xxx_cmd_wait\n", ha->host_no,
894                       __func__));
895
896         /* Wait for outstanding commands to complete.
897          * Stalls the driver for max 30 secs
898          */
899         status = qla4xxx_cmd_wait(ha);
900
901         qla4xxx_disable_intrs(ha);
902
903         /* Flush any pending ddb changed AENs */
904         qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
905
906         /* Reset the firmware.  If successful, function
907          * returns with ISP interrupts enabled.
908          */
909         if (status == QLA_SUCCESS) {
910                 DEBUG2(printk("scsi%ld: %s - Performing soft reset..\n",
911                               ha->host_no, __func__));
912                 qla4xxx_flush_active_srbs(ha);
913                 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS)
914                         status = qla4xxx_soft_reset(ha);
915                 else
916                         status = QLA_ERROR;
917         }
918
919         /* Flush any pending ddb changed AENs */
920         qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
921
922         /* Re-initialize firmware. If successful, function returns
923          * with ISP interrupts enabled */
924         if (status == QLA_SUCCESS) {
925                 DEBUG2(printk("scsi%ld: %s - Initializing adapter..\n",
926                               ha->host_no, __func__));
927
928                 /* If successful, AF_ONLINE flag set in
929                  * qla4xxx_initialize_adapter */
930                 status = qla4xxx_initialize_adapter(ha, renew_ddb_list);
931         }
932
933         /* Failed adapter initialization?
934          * Retry reset_ha only if invoked via DPC (DPC_RESET_HA) */
935         if ((test_bit(AF_ONLINE, &ha->flags) == 0) &&
936             (test_bit(DPC_RESET_HA, &ha->dpc_flags))) {
937                 /* Adapter initialization failed, see if we can retry
938                  * resetting the ha */
939                 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
940                         ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
941                         DEBUG2(printk("scsi%ld: recover adapter - retrying "
942                                       "(%d) more times\n", ha->host_no,
943                                       ha->retry_reset_ha_cnt));
944                         set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
945                         status = QLA_ERROR;
946                 } else {
947                         if (ha->retry_reset_ha_cnt > 0) {
948                                 /* Schedule another Reset HA--DPC will retry */
949                                 ha->retry_reset_ha_cnt--;
950                                 DEBUG2(printk("scsi%ld: recover adapter - "
951                                               "retry remaining %d\n",
952                                               ha->host_no,
953                                               ha->retry_reset_ha_cnt));
954                                 status = QLA_ERROR;
955                         }
956
957                         if (ha->retry_reset_ha_cnt == 0) {
958                                 /* Recover adapter retries have been exhausted.
959                                  * Adapter DEAD */
960                                 DEBUG2(printk("scsi%ld: recover adapter "
961                                               "failed - board disabled\n",
962                                               ha->host_no));
963                                 qla4xxx_flush_active_srbs(ha);
964                                 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
965                                 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
966                                 clear_bit(DPC_RESET_HA_DESTROY_DDB_LIST,
967                                           &ha->dpc_flags);
968                                 status = QLA_ERROR;
969                         }
970                 }
971         } else {
972                 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
973                 clear_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags);
974                 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
975         }
976
977         ha->adapter_error_count++;
978
979         if (status == QLA_SUCCESS)
980                 qla4xxx_enable_intrs(ha);
981
982         DEBUG2(printk("scsi%ld: recover adapter .. DONE\n", ha->host_no));
983         return status;
984 }
985
986 /**
987  * qla4xxx_do_dpc - dpc routine
988  * @data: in our case pointer to adapter structure
989  *
990  * This routine is a task that is schedule by the interrupt handler
991  * to perform the background processing for interrupts.  We put it
992  * on a task queue that is consumed whenever the scheduler runs; that's
993  * so you can do anything (i.e. put the process to sleep etc).  In fact,
994  * the mid-level tries to sleep when it reaches the driver threshold
995  * "host->can_queue". This can cause a panic if we were in our interrupt code.
996  **/
997 static void qla4xxx_do_dpc(struct work_struct *work)
998 {
999         struct scsi_qla_host *ha =
1000                 container_of(work, struct scsi_qla_host, dpc_work);
1001         struct ddb_entry *ddb_entry, *dtemp;
1002         int status = QLA_ERROR;
1003
1004         DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
1005                 "flags = 0x%08lx, dpc_flags = 0x%08lx ctrl_stat = 0x%08x\n",
1006                 ha->host_no, __func__, ha->flags, ha->dpc_flags,
1007                 readw(&ha->reg->ctrl_status)));
1008
1009         /* Initialization not yet finished. Don't do anything yet. */
1010         if (!test_bit(AF_INIT_DONE, &ha->flags))
1011                 return;
1012
1013         if (adapter_up(ha) ||
1014             test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1015             test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1016             test_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags)) {
1017                 if (test_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags) ||
1018                         test_bit(DPC_RESET_HA, &ha->dpc_flags))
1019                         qla4xxx_recover_adapter(ha, PRESERVE_DDB_LIST);
1020
1021                 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1022                         uint8_t wait_time = RESET_INTR_TOV;
1023
1024                         while ((readw(&ha->reg->ctrl_status) &
1025                                 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
1026                                 if (--wait_time == 0)
1027                                         break;
1028                                 msleep(1000);
1029                         }
1030                         if (wait_time == 0)
1031                                 DEBUG2(printk("scsi%ld: %s: SR|FSR "
1032                                               "bit not cleared-- resetting\n",
1033                                               ha->host_no, __func__));
1034                         qla4xxx_flush_active_srbs(ha);
1035                         if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
1036                                 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1037                                 status = qla4xxx_initialize_adapter(ha,
1038                                                 PRESERVE_DDB_LIST);
1039                         }
1040                         clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1041                         if (status == QLA_SUCCESS)
1042                                 qla4xxx_enable_intrs(ha);
1043                 }
1044         }
1045
1046         /* ---- process AEN? --- */
1047         if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
1048                 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
1049
1050         /* ---- Get DHCP IP Address? --- */
1051         if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
1052                 qla4xxx_get_dhcp_ip_address(ha);
1053
1054         /* ---- relogin device? --- */
1055         if (adapter_up(ha) &&
1056             test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
1057                 list_for_each_entry_safe(ddb_entry, dtemp,
1058                                          &ha->ddb_list, list) {
1059                         if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
1060                             atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
1061                                 qla4xxx_relogin_device(ha, ddb_entry);
1062
1063                         /*
1064                          * If mbx cmd times out there is no point
1065                          * in continuing further.
1066                          * With large no of targets this can hang
1067                          * the system.
1068                          */
1069                         if (test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
1070                                 printk(KERN_WARNING "scsi%ld: %s: "
1071                                        "need to reset hba\n",
1072                                        ha->host_no, __func__);
1073                                 break;
1074                         }
1075                 }
1076         }
1077 }
1078
1079 /**
1080  * qla4xxx_free_adapter - release the adapter
1081  * @ha: pointer to adapter structure
1082  **/
1083 static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
1084 {
1085
1086         if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
1087                 /* Turn-off interrupts on the card. */
1088                 qla4xxx_disable_intrs(ha);
1089         }
1090
1091         /* Kill the kernel thread for this host */
1092         if (ha->dpc_thread)
1093                 destroy_workqueue(ha->dpc_thread);
1094
1095         /* Issue Soft Reset to put firmware in unknown state */
1096         if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS)
1097                 qla4xxx_hw_reset(ha);
1098
1099         /* Remove timer thread, if present */
1100         if (ha->timer_active)
1101                 qla4xxx_stop_timer(ha);
1102
1103         /* Detach interrupts */
1104         if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
1105                 free_irq(ha->pdev->irq, ha);
1106
1107         /* free extra memory */
1108         qla4xxx_mem_free(ha);
1109
1110         pci_disable_device(ha->pdev);
1111
1112 }
1113
1114 /***
1115  * qla4xxx_iospace_config - maps registers
1116  * @ha: pointer to adapter structure
1117  *
1118  * This routines maps HBA's registers from the pci address space
1119  * into the kernel virtual address space for memory mapped i/o.
1120  **/
1121 static int qla4xxx_iospace_config(struct scsi_qla_host *ha)
1122 {
1123         unsigned long pio, pio_len, pio_flags;
1124         unsigned long mmio, mmio_len, mmio_flags;
1125
1126         pio = pci_resource_start(ha->pdev, 0);
1127         pio_len = pci_resource_len(ha->pdev, 0);
1128         pio_flags = pci_resource_flags(ha->pdev, 0);
1129         if (pio_flags & IORESOURCE_IO) {
1130                 if (pio_len < MIN_IOBASE_LEN) {
1131                         dev_warn(&ha->pdev->dev,
1132                                 "Invalid PCI I/O region size\n");
1133                         pio = 0;
1134                 }
1135         } else {
1136                 dev_warn(&ha->pdev->dev, "region #0 not a PIO resource\n");
1137                 pio = 0;
1138         }
1139
1140         /* Use MMIO operations for all accesses. */
1141         mmio = pci_resource_start(ha->pdev, 1);
1142         mmio_len = pci_resource_len(ha->pdev, 1);
1143         mmio_flags = pci_resource_flags(ha->pdev, 1);
1144
1145         if (!(mmio_flags & IORESOURCE_MEM)) {
1146                 dev_err(&ha->pdev->dev,
1147                         "region #0 not an MMIO resource, aborting\n");
1148
1149                 goto iospace_error_exit;
1150         }
1151         if (mmio_len < MIN_IOBASE_LEN) {
1152                 dev_err(&ha->pdev->dev,
1153                         "Invalid PCI mem region size, aborting\n");
1154                 goto iospace_error_exit;
1155         }
1156
1157         if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
1158                 dev_warn(&ha->pdev->dev,
1159                         "Failed to reserve PIO/MMIO regions\n");
1160
1161                 goto iospace_error_exit;
1162         }
1163
1164         ha->pio_address = pio;
1165         ha->pio_length = pio_len;
1166         ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
1167         if (!ha->reg) {
1168                 dev_err(&ha->pdev->dev,
1169                         "cannot remap MMIO, aborting\n");
1170
1171                 goto iospace_error_exit;
1172         }
1173
1174         return 0;
1175
1176 iospace_error_exit:
1177         return -ENOMEM;
1178 }
1179
1180 /**
1181  * qla4xxx_probe_adapter - callback function to probe HBA
1182  * @pdev: pointer to pci_dev structure
1183  * @pci_device_id: pointer to pci_device entry
1184  *
1185  * This routine will probe for Qlogic 4xxx iSCSI host adapters.
1186  * It returns zero if successful. It also initializes all data necessary for
1187  * the driver.
1188  **/
1189 static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
1190                                            const struct pci_device_id *ent)
1191 {
1192         int ret = -ENODEV, status;
1193         struct Scsi_Host *host;
1194         struct scsi_qla_host *ha;
1195         struct ddb_entry *ddb_entry, *ddbtemp;
1196         uint8_t init_retry_count = 0;
1197         char buf[34];
1198
1199         if (pci_enable_device(pdev))
1200                 return -1;
1201
1202         host = scsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha));
1203         if (host == NULL) {
1204                 printk(KERN_WARNING
1205                        "qla4xxx: Couldn't allocate host from scsi layer!\n");
1206                 goto probe_disable_device;
1207         }
1208
1209         /* Clear our data area */
1210         ha = (struct scsi_qla_host *) host->hostdata;
1211         memset(ha, 0, sizeof(*ha));
1212
1213         /* Save the information from PCI BIOS.  */
1214         ha->pdev = pdev;
1215         ha->host = host;
1216         ha->host_no = host->host_no;
1217
1218         /* Configure PCI I/O space. */
1219         ret = qla4xxx_iospace_config(ha);
1220         if (ret)
1221                 goto probe_failed;
1222
1223         dev_info(&ha->pdev->dev, "Found an ISP%04x, irq %d, iobase 0x%p\n",
1224                    pdev->device, pdev->irq, ha->reg);
1225
1226         qla4xxx_config_dma_addressing(ha);
1227
1228         /* Initialize lists and spinlocks. */
1229         INIT_LIST_HEAD(&ha->ddb_list);
1230         INIT_LIST_HEAD(&ha->free_srb_q);
1231
1232         mutex_init(&ha->mbox_sem);
1233
1234         spin_lock_init(&ha->hardware_lock);
1235
1236         /* Allocate dma buffers */
1237         if (qla4xxx_mem_alloc(ha)) {
1238                 dev_warn(&ha->pdev->dev,
1239                            "[ERROR] Failed to allocate memory for adapter\n");
1240
1241                 ret = -ENOMEM;
1242                 goto probe_failed;
1243         }
1244
1245         /*
1246          * Initialize the Host adapter request/response queues and
1247          * firmware
1248          * NOTE: interrupts enabled upon successful completion
1249          */
1250         status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1251         while (status == QLA_ERROR && init_retry_count++ < MAX_INIT_RETRIES) {
1252                 DEBUG2(printk("scsi: %s: retrying adapter initialization "
1253                               "(%d)\n", __func__, init_retry_count));
1254                 qla4xxx_soft_reset(ha);
1255                 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1256         }
1257         if (status == QLA_ERROR) {
1258                 dev_warn(&ha->pdev->dev, "Failed to initialize adapter\n");
1259
1260                 ret = -ENODEV;
1261                 goto probe_failed;
1262         }
1263
1264         host->cmd_per_lun = 3;
1265         host->max_channel = 0;
1266         host->max_lun = MAX_LUNS - 1;
1267         host->max_id = MAX_TARGETS;
1268         host->max_cmd_len = IOCB_MAX_CDB_LEN;
1269         host->can_queue = MAX_SRBS ;
1270         host->transportt = qla4xxx_scsi_transport;
1271
1272         ret = scsi_init_shared_tag_map(host, MAX_SRBS);
1273         if (ret) {
1274                 dev_warn(&ha->pdev->dev, "scsi_init_shared_tag_map failed\n");
1275                 goto probe_failed;
1276         }
1277
1278         /* Startup the kernel thread for this host adapter. */
1279         DEBUG2(printk("scsi: %s: Starting kernel thread for "
1280                       "qla4xxx_dpc\n", __func__));
1281         sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
1282         ha->dpc_thread = create_singlethread_workqueue(buf);
1283         if (!ha->dpc_thread) {
1284                 dev_warn(&ha->pdev->dev, "Unable to start DPC thread!\n");
1285                 ret = -ENODEV;
1286                 goto probe_failed;
1287         }
1288         INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
1289
1290         ret = request_irq(pdev->irq, qla4xxx_intr_handler,
1291                           IRQF_DISABLED | IRQF_SHARED, "qla4xxx", ha);
1292         if (ret) {
1293                 dev_warn(&ha->pdev->dev, "Failed to reserve interrupt %d"
1294                         " already in use.\n", pdev->irq);
1295                 goto probe_failed;
1296         }
1297         set_bit(AF_IRQ_ATTACHED, &ha->flags);
1298         host->irq = pdev->irq;
1299         DEBUG(printk("scsi%d: irq %d attached\n", ha->host_no, ha->pdev->irq));
1300
1301         qla4xxx_enable_intrs(ha);
1302
1303         /* Start timer thread. */
1304         qla4xxx_start_timer(ha, qla4xxx_timer, 1);
1305
1306         set_bit(AF_INIT_DONE, &ha->flags);
1307
1308         pci_set_drvdata(pdev, ha);
1309
1310         ret = scsi_add_host(host, &pdev->dev);
1311         if (ret)
1312                 goto probe_failed;
1313
1314         /* Update transport device information for all devices. */
1315         list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
1316                 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE)
1317                         if (qla4xxx_add_sess(ddb_entry))
1318                                 goto remove_host;
1319         }
1320
1321         printk(KERN_INFO
1322                " QLogic iSCSI HBA Driver version: %s\n"
1323                "  QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
1324                qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
1325                ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
1326                ha->patch_number, ha->build_number);
1327
1328         return 0;
1329
1330 remove_host:
1331         qla4xxx_free_ddb_list(ha);
1332         scsi_remove_host(host);
1333
1334 probe_failed:
1335         qla4xxx_free_adapter(ha);
1336         scsi_host_put(ha->host);
1337
1338 probe_disable_device:
1339         pci_disable_device(pdev);
1340
1341         return ret;
1342 }
1343
1344 /**
1345  * qla4xxx_remove_adapter - calback function to remove adapter.
1346  * @pci_dev: PCI device pointer
1347  **/
1348 static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
1349 {
1350         struct scsi_qla_host *ha;
1351
1352         ha = pci_get_drvdata(pdev);
1353
1354         qla4xxx_disable_intrs(ha);
1355
1356         while (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags))
1357                 ssleep(1);
1358
1359         /* remove devs from iscsi_sessions to scsi_devices */
1360         qla4xxx_free_ddb_list(ha);
1361
1362         scsi_remove_host(ha->host);
1363
1364         qla4xxx_free_adapter(ha);
1365
1366         scsi_host_put(ha->host);
1367
1368         pci_set_drvdata(pdev, NULL);
1369 }
1370
1371 /**
1372  * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
1373  * @ha: HA context
1374  *
1375  * At exit, the @ha's flags.enable_64bit_addressing set to indicated
1376  * supported addressing method.
1377  */
1378 static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
1379 {
1380         int retval;
1381
1382         /* Update our PCI device dma_mask for full 64 bit mask */
1383         if (pci_set_dma_mask(ha->pdev, DMA_64BIT_MASK) == 0) {
1384                 if (pci_set_consistent_dma_mask(ha->pdev, DMA_64BIT_MASK)) {
1385                         dev_dbg(&ha->pdev->dev,
1386                                   "Failed to set 64 bit PCI consistent mask; "
1387                                    "using 32 bit.\n");
1388                         retval = pci_set_consistent_dma_mask(ha->pdev,
1389                                                              DMA_32BIT_MASK);
1390                 }
1391         } else
1392                 retval = pci_set_dma_mask(ha->pdev, DMA_32BIT_MASK);
1393 }
1394
1395 static int qla4xxx_slave_alloc(struct scsi_device *sdev)
1396 {
1397         struct iscsi_cls_session *sess = starget_to_session(sdev->sdev_target);
1398         struct ddb_entry *ddb = sess->dd_data;
1399
1400         sdev->hostdata = ddb;
1401         sdev->tagged_supported = 1;
1402         scsi_activate_tcq(sdev, sdev->host->can_queue);
1403         return 0;
1404 }
1405
1406 static int qla4xxx_slave_configure(struct scsi_device *sdev)
1407 {
1408         sdev->tagged_supported = 1;
1409         return 0;
1410 }
1411
1412 static void qla4xxx_slave_destroy(struct scsi_device *sdev)
1413 {
1414         scsi_deactivate_tcq(sdev, 1);
1415 }
1416
1417 /**
1418  * qla4xxx_del_from_active_array - returns an active srb
1419  * @ha: Pointer to host adapter structure.
1420  * @index: index into to the active_array
1421  *
1422  * This routine removes and returns the srb at the specified index
1423  **/
1424 struct srb * qla4xxx_del_from_active_array(struct scsi_qla_host *ha, uint32_t index)
1425 {
1426         struct srb *srb = NULL;
1427         struct scsi_cmnd *cmd;
1428
1429         if (!(cmd = scsi_host_find_tag(ha->host, index)))
1430                 return srb;
1431
1432         if (!(srb = (struct srb *)cmd->host_scribble))
1433                 return srb;
1434
1435         /* update counters */
1436         if (srb->flags & SRB_DMA_VALID) {
1437                 ha->req_q_count += srb->iocb_cnt;
1438                 ha->iocb_cnt -= srb->iocb_cnt;
1439                 if (srb->cmd)
1440                         srb->cmd->host_scribble = NULL;
1441         }
1442         return srb;
1443 }
1444
1445 /**
1446  * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
1447  * @ha: actual ha whose done queue will contain the comd returned by firmware.
1448  * @cmd: Scsi Command to wait on.
1449  *
1450  * This routine waits for the command to be returned by the Firmware
1451  * for some max time.
1452  **/
1453 static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
1454                                       struct scsi_cmnd *cmd)
1455 {
1456         int done = 0;
1457         struct srb *rp;
1458         uint32_t max_wait_time = EH_WAIT_CMD_TOV;
1459
1460         do {
1461                 /* Checking to see if its returned to OS */
1462                 rp = (struct srb *) cmd->SCp.ptr;
1463                 if (rp == NULL) {
1464                         done++;
1465                         break;
1466                 }
1467
1468                 msleep(2000);
1469         } while (max_wait_time--);
1470
1471         return done;
1472 }
1473
1474 /**
1475  * qla4xxx_wait_for_hba_online - waits for HBA to come online
1476  * @ha: Pointer to host adapter structure
1477  **/
1478 static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
1479 {
1480         unsigned long wait_online;
1481
1482         wait_online = jiffies + (30 * HZ);
1483         while (time_before(jiffies, wait_online)) {
1484
1485                 if (adapter_up(ha))
1486                         return QLA_SUCCESS;
1487                 else if (ha->retry_reset_ha_cnt == 0)
1488                         return QLA_ERROR;
1489
1490                 msleep(2000);
1491         }
1492
1493         return QLA_ERROR;
1494 }
1495
1496 /**
1497  * qla4xxx_eh_wait_for_active_target_commands - wait for active cmds to finish.
1498  * @ha: pointer to to HBA
1499  * @t: target id
1500  * @l: lun id
1501  *
1502  * This function waits for all outstanding commands to a lun to complete. It
1503  * returns 0 if all pending commands are returned and 1 otherwise.
1504  **/
1505 static int qla4xxx_eh_wait_for_active_target_commands(struct scsi_qla_host *ha,
1506                                                  int t, int l)
1507 {
1508         int cnt;
1509         int status = 0;
1510         struct scsi_cmnd *cmd;
1511
1512         /*
1513          * Waiting for all commands for the designated target in the active
1514          * array
1515          */
1516         for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
1517                 cmd = scsi_host_find_tag(ha->host, cnt);
1518                 if (cmd && cmd->device->id == t && cmd->device->lun == l) {
1519                         if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
1520                                 status++;
1521                                 break;
1522                         }
1523                 }
1524         }
1525         return status;
1526 }
1527
1528 /**
1529  * qla4xxx_eh_device_reset - callback for target reset.
1530  * @cmd: Pointer to Linux's SCSI command structure
1531  *
1532  * This routine is called by the Linux OS to reset all luns on the
1533  * specified target.
1534  **/
1535 static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
1536 {
1537         struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
1538         struct ddb_entry *ddb_entry = cmd->device->hostdata;
1539         struct srb *sp;
1540         int ret = FAILED, stat;
1541
1542         sp = (struct srb *) cmd->SCp.ptr;
1543         if (!sp || !ddb_entry)
1544                 return ret;
1545
1546         dev_info(&ha->pdev->dev,
1547                    "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
1548                    cmd->device->channel, cmd->device->id, cmd->device->lun);
1549
1550         DEBUG2(printk(KERN_INFO
1551                       "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
1552                       "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
1553                       cmd, jiffies, cmd->timeout_per_command / HZ,
1554                       ha->dpc_flags, cmd->result, cmd->allowed));
1555
1556         /* FIXME: wait for hba to go online */
1557         stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
1558         if (stat != QLA_SUCCESS) {
1559                 dev_info(&ha->pdev->dev, "DEVICE RESET FAILED. %d\n", stat);
1560                 goto eh_dev_reset_done;
1561         }
1562
1563         /* Send marker. */
1564         ha->marker_needed = 1;
1565
1566         /*
1567          * If we are coming down the EH path, wait for all commands to complete
1568          * for the device.
1569          */
1570         if (cmd->device->host->shost_state == SHOST_RECOVERY) {
1571                 if (qla4xxx_eh_wait_for_active_target_commands(ha,
1572                                                           cmd->device->id,
1573                                                           cmd->device->lun)){
1574                         dev_info(&ha->pdev->dev,
1575                                    "DEVICE RESET FAILED - waiting for "
1576                                    "commands.\n");
1577                         goto eh_dev_reset_done;
1578                 }
1579         }
1580
1581         dev_info(&ha->pdev->dev,
1582                    "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
1583                    ha->host_no, cmd->device->channel, cmd->device->id,
1584                    cmd->device->lun);
1585
1586         ret = SUCCESS;
1587
1588 eh_dev_reset_done:
1589
1590         return ret;
1591 }
1592
1593 /**
1594  * qla4xxx_eh_host_reset - kernel callback
1595  * @cmd: Pointer to Linux's SCSI command structure
1596  *
1597  * This routine is invoked by the Linux kernel to perform fatal error
1598  * recovery on the specified adapter.
1599  **/
1600 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
1601 {
1602         int return_status = FAILED;
1603         struct scsi_qla_host *ha;
1604
1605         ha = (struct scsi_qla_host *) cmd->device->host->hostdata;
1606
1607         dev_info(&ha->pdev->dev,
1608                    "scsi(%ld:%d:%d:%d): ADAPTER RESET ISSUED.\n", ha->host_no,
1609                    cmd->device->channel, cmd->device->id, cmd->device->lun);
1610
1611         if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
1612                 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host.  Adapter "
1613                               "DEAD.\n", ha->host_no, cmd->device->channel,
1614                               __func__));
1615
1616                 return FAILED;
1617         }
1618
1619         if (qla4xxx_recover_adapter(ha, PRESERVE_DDB_LIST) == QLA_SUCCESS) {
1620                 return_status = SUCCESS;
1621         }
1622
1623         dev_info(&ha->pdev->dev, "HOST RESET %s.\n",
1624                    return_status == FAILED ? "FAILED" : "SUCCEDED");
1625
1626         return return_status;
1627 }
1628
1629
1630 static struct pci_device_id qla4xxx_pci_tbl[] = {
1631         {
1632                 .vendor         = PCI_VENDOR_ID_QLOGIC,
1633                 .device         = PCI_DEVICE_ID_QLOGIC_ISP4010,
1634                 .subvendor      = PCI_ANY_ID,
1635                 .subdevice      = PCI_ANY_ID,
1636         },
1637         {
1638                 .vendor         = PCI_VENDOR_ID_QLOGIC,
1639                 .device         = PCI_DEVICE_ID_QLOGIC_ISP4022,
1640                 .subvendor      = PCI_ANY_ID,
1641                 .subdevice      = PCI_ANY_ID,
1642         },
1643         {
1644                 .vendor         = PCI_VENDOR_ID_QLOGIC,
1645                 .device         = PCI_DEVICE_ID_QLOGIC_ISP4032,
1646                 .subvendor      = PCI_ANY_ID,
1647                 .subdevice      = PCI_ANY_ID,
1648         },
1649         {0, 0},
1650 };
1651 MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
1652
1653 static struct pci_driver qla4xxx_pci_driver = {
1654         .name           = DRIVER_NAME,
1655         .id_table       = qla4xxx_pci_tbl,
1656         .probe          = qla4xxx_probe_adapter,
1657         .remove         = qla4xxx_remove_adapter,
1658 };
1659
1660 static int __init qla4xxx_module_init(void)
1661 {
1662         int ret;
1663
1664         /* Allocate cache for SRBs. */
1665         srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
1666                                        SLAB_HWCACHE_ALIGN, NULL);
1667         if (srb_cachep == NULL) {
1668                 printk(KERN_ERR
1669                        "%s: Unable to allocate SRB cache..."
1670                        "Failing load!\n", DRIVER_NAME);
1671                 ret = -ENOMEM;
1672                 goto no_srp_cache;
1673         }
1674
1675         /* Derive version string. */
1676         strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
1677         if (ql4xextended_error_logging)
1678                 strcat(qla4xxx_version_str, "-debug");
1679
1680         qla4xxx_scsi_transport =
1681                 iscsi_register_transport(&qla4xxx_iscsi_transport);
1682         if (!qla4xxx_scsi_transport){
1683                 ret = -ENODEV;
1684                 goto release_srb_cache;
1685         }
1686
1687         ret = pci_register_driver(&qla4xxx_pci_driver);
1688         if (ret)
1689                 goto unregister_transport;
1690
1691         printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
1692         return 0;
1693
1694 unregister_transport:
1695         iscsi_unregister_transport(&qla4xxx_iscsi_transport);
1696 release_srb_cache:
1697         kmem_cache_destroy(srb_cachep);
1698 no_srp_cache:
1699         return ret;
1700 }
1701
1702 static void __exit qla4xxx_module_exit(void)
1703 {
1704         ql4_mod_unload = 1;
1705         pci_unregister_driver(&qla4xxx_pci_driver);
1706         iscsi_unregister_transport(&qla4xxx_iscsi_transport);
1707         kmem_cache_destroy(srb_cachep);
1708 }
1709
1710 module_init(qla4xxx_module_init);
1711 module_exit(qla4xxx_module_exit);
1712
1713 MODULE_AUTHOR("QLogic Corporation");
1714 MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
1715 MODULE_LICENSE("GPL");
1716 MODULE_VERSION(QLA4XXX_DRIVER_VERSION);