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