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