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