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