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