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