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