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