57890b6288b92d16f1abf5724c620cb96a827a57
[pandora-kernel.git] / drivers / scsi / qla4xxx / ql4_os.c
1 /*
2  * QLogic iSCSI HBA Driver
3  * Copyright (c)  2003-2006 QLogic Corporation
4  *
5  * See LICENSE.qla4xxx for copyright and licensing details.
6  */
7 #include <linux/moduleparam.h>
8 #include <linux/slab.h>
9
10 #include <scsi/scsi_tcq.h>
11 #include <scsi/scsicam.h>
12
13 #include "ql4_def.h"
14 #include "ql4_version.h"
15 #include "ql4_glbl.h"
16 #include "ql4_dbg.h"
17 #include "ql4_inline.h"
18
19 /*
20  * Driver version
21  */
22 static char qla4xxx_version_str[40];
23
24 /*
25  * SRB allocation cache
26  */
27 static struct kmem_cache *srb_cachep;
28
29 /*
30  * Module parameter information and variables
31  */
32 int ql4xdiscoverywait = 60;
33 module_param(ql4xdiscoverywait, int, S_IRUGO | S_IWUSR);
34 MODULE_PARM_DESC(ql4xdiscoverywait, "Discovery wait time");
35
36 int ql4xdontresethba = 0;
37 module_param(ql4xdontresethba, int, S_IRUGO | S_IWUSR);
38 MODULE_PARM_DESC(ql4xdontresethba,
39                 "Don't reset the HBA for driver recovery \n"
40                 " 0 - It will reset HBA (Default)\n"
41                 " 1 - It will NOT reset HBA");
42
43 int ql4xextended_error_logging = 0; /* 0 = off, 1 = log errors */
44 module_param(ql4xextended_error_logging, int, S_IRUGO | S_IWUSR);
45 MODULE_PARM_DESC(ql4xextended_error_logging,
46                  "Option to enable extended error logging, "
47                  "Default is 0 - no logging, 1 - debug logging");
48
49 int ql4xenablemsix = 1;
50 module_param(ql4xenablemsix, int, S_IRUGO|S_IWUSR);
51 MODULE_PARM_DESC(ql4xenablemsix,
52                 "Set to enable MSI or MSI-X interrupt mechanism.\n"
53                 " 0 = enable INTx interrupt mechanism.\n"
54                 " 1 = enable MSI-X interrupt mechanism (Default).\n"
55                 " 2 = enable MSI interrupt mechanism.");
56
57 #define QL4_DEF_QDEPTH 32
58
59 /*
60  * SCSI host template entry points
61  */
62 static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
63
64 /*
65  * iSCSI template entry points
66  */
67 static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
68                              enum iscsi_tgt_dscvr type, uint32_t enable,
69                              struct sockaddr *dst_addr);
70 static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
71                                   enum iscsi_param param, char *buf);
72 static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
73                                   enum iscsi_param param, char *buf);
74 static int qla4xxx_host_get_param(struct Scsi_Host *shost,
75                                   enum iscsi_host_param param, char *buf);
76 static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session);
77 static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
78
79 /*
80  * SCSI host template entry points
81  */
82 static int qla4xxx_queuecommand(struct scsi_cmnd *cmd,
83                                 void (*done) (struct scsi_cmnd *));
84 static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
85 static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
86 static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
87 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
88 static int qla4xxx_slave_alloc(struct scsi_device *device);
89 static int qla4xxx_slave_configure(struct scsi_device *device);
90 static void qla4xxx_slave_destroy(struct scsi_device *sdev);
91 static void qla4xxx_scan_start(struct Scsi_Host *shost);
92
93 static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
94     QLA82XX_LEGACY_INTR_CONFIG;
95
96 static struct scsi_host_template qla4xxx_driver_template = {
97         .module                 = THIS_MODULE,
98         .name                   = DRIVER_NAME,
99         .proc_name              = DRIVER_NAME,
100         .queuecommand           = qla4xxx_queuecommand,
101
102         .eh_abort_handler       = qla4xxx_eh_abort,
103         .eh_device_reset_handler = qla4xxx_eh_device_reset,
104         .eh_target_reset_handler = qla4xxx_eh_target_reset,
105         .eh_host_reset_handler  = qla4xxx_eh_host_reset,
106         .eh_timed_out           = qla4xxx_eh_cmd_timed_out,
107
108         .slave_configure        = qla4xxx_slave_configure,
109         .slave_alloc            = qla4xxx_slave_alloc,
110         .slave_destroy          = qla4xxx_slave_destroy,
111
112         .scan_finished          = iscsi_scan_finished,
113         .scan_start             = qla4xxx_scan_start,
114
115         .this_id                = -1,
116         .cmd_per_lun            = 3,
117         .use_clustering         = ENABLE_CLUSTERING,
118         .sg_tablesize           = SG_ALL,
119
120         .max_sectors            = 0xFFFF,
121 };
122
123 static struct iscsi_transport qla4xxx_iscsi_transport = {
124         .owner                  = THIS_MODULE,
125         .name                   = DRIVER_NAME,
126         .caps                   = CAP_FW_DB | CAP_SENDTARGETS_OFFLOAD |
127                                   CAP_DATA_PATH_OFFLOAD,
128         .param_mask             = ISCSI_CONN_PORT | ISCSI_CONN_ADDRESS |
129                                   ISCSI_TARGET_NAME | ISCSI_TPGT |
130                                   ISCSI_TARGET_ALIAS,
131         .host_param_mask        = ISCSI_HOST_HWADDRESS |
132                                   ISCSI_HOST_IPADDRESS |
133                                   ISCSI_HOST_INITIATOR_NAME,
134         .tgt_dscvr              = qla4xxx_tgt_dscvr,
135         .get_conn_param         = qla4xxx_conn_get_param,
136         .get_session_param      = qla4xxx_sess_get_param,
137         .get_host_param         = qla4xxx_host_get_param,
138         .session_recovery_timedout = qla4xxx_recovery_timedout,
139 };
140
141 static struct scsi_transport_template *qla4xxx_scsi_transport;
142
143 static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
144 {
145         struct iscsi_cls_session *session;
146         struct ddb_entry *ddb_entry;
147
148         session = starget_to_session(scsi_target(sc->device));
149         ddb_entry = session->dd_data;
150
151         /* if we are not logged in then the LLD is going to clean up the cmd */
152         if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
153                 return BLK_EH_RESET_TIMER;
154         else
155                 return BLK_EH_NOT_HANDLED;
156 }
157
158 static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session)
159 {
160         struct ddb_entry *ddb_entry = session->dd_data;
161         struct scsi_qla_host *ha = ddb_entry->ha;
162
163         if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
164                 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
165
166                 DEBUG2(printk("scsi%ld: %s: ddb [%d] session recovery timeout "
167                               "of (%d) secs exhausted, marking device DEAD.\n",
168                               ha->host_no, __func__, ddb_entry->fw_ddb_index,
169                               QL4_SESS_RECOVERY_TMO));
170         }
171 }
172
173 static int qla4xxx_host_get_param(struct Scsi_Host *shost,
174                                   enum iscsi_host_param param, char *buf)
175 {
176         struct scsi_qla_host *ha = to_qla_host(shost);
177         int len;
178
179         switch (param) {
180         case ISCSI_HOST_PARAM_HWADDRESS:
181                 len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
182                 break;
183         case ISCSI_HOST_PARAM_IPADDRESS:
184                 len = sprintf(buf, "%d.%d.%d.%d\n", ha->ip_address[0],
185                               ha->ip_address[1], ha->ip_address[2],
186                               ha->ip_address[3]);
187                 break;
188         case ISCSI_HOST_PARAM_INITIATOR_NAME:
189                 len = sprintf(buf, "%s\n", ha->name_string);
190                 break;
191         default:
192                 return -ENOSYS;
193         }
194
195         return len;
196 }
197
198 static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
199                                   enum iscsi_param param, char *buf)
200 {
201         struct ddb_entry *ddb_entry = sess->dd_data;
202         int len;
203
204         switch (param) {
205         case ISCSI_PARAM_TARGET_NAME:
206                 len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
207                                ddb_entry->iscsi_name);
208                 break;
209         case ISCSI_PARAM_TPGT:
210                 len = sprintf(buf, "%u\n", ddb_entry->tpgt);
211                 break;
212         case ISCSI_PARAM_TARGET_ALIAS:
213                 len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
214                     ddb_entry->iscsi_alias);
215                 break;
216         default:
217                 return -ENOSYS;
218         }
219
220         return len;
221 }
222
223 static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
224                                   enum iscsi_param param, char *buf)
225 {
226         struct iscsi_cls_session *session;
227         struct ddb_entry *ddb_entry;
228         int len;
229
230         session = iscsi_dev_to_session(conn->dev.parent);
231         ddb_entry = session->dd_data;
232
233         switch (param) {
234         case ISCSI_PARAM_CONN_PORT:
235                 len = sprintf(buf, "%hu\n", ddb_entry->port);
236                 break;
237         case ISCSI_PARAM_CONN_ADDRESS:
238                 /* TODO: what are the ipv6 bits */
239                 len = sprintf(buf, "%pI4\n", &ddb_entry->ip_addr);
240                 break;
241         default:
242                 return -ENOSYS;
243         }
244
245         return len;
246 }
247
248 static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
249                              enum iscsi_tgt_dscvr type, uint32_t enable,
250                              struct sockaddr *dst_addr)
251 {
252         struct scsi_qla_host *ha;
253         struct sockaddr_in *addr;
254         struct sockaddr_in6 *addr6;
255         int ret = 0;
256
257         ha = (struct scsi_qla_host *) shost->hostdata;
258
259         switch (type) {
260         case ISCSI_TGT_DSCVR_SEND_TARGETS:
261                 if (dst_addr->sa_family == AF_INET) {
262                         addr = (struct sockaddr_in *)dst_addr;
263                         if (qla4xxx_send_tgts(ha, (char *)&addr->sin_addr,
264                                               addr->sin_port) != QLA_SUCCESS)
265                                 ret = -EIO;
266                 } else if (dst_addr->sa_family == AF_INET6) {
267                         /*
268                          * TODO: fix qla4xxx_send_tgts
269                          */
270                         addr6 = (struct sockaddr_in6 *)dst_addr;
271                         if (qla4xxx_send_tgts(ha, (char *)&addr6->sin6_addr,
272                                               addr6->sin6_port) != QLA_SUCCESS)
273                                 ret = -EIO;
274                 } else
275                         ret = -ENOSYS;
276                 break;
277         default:
278                 ret = -ENOSYS;
279         }
280         return ret;
281 }
282
283 void qla4xxx_destroy_sess(struct ddb_entry *ddb_entry)
284 {
285         if (!ddb_entry->sess)
286                 return;
287
288         if (ddb_entry->conn) {
289                 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
290                 iscsi_remove_session(ddb_entry->sess);
291         }
292         iscsi_free_session(ddb_entry->sess);
293 }
294
295 int qla4xxx_add_sess(struct ddb_entry *ddb_entry)
296 {
297         int err;
298
299         ddb_entry->sess->recovery_tmo = QL4_SESS_RECOVERY_TMO;
300
301         err = iscsi_add_session(ddb_entry->sess, ddb_entry->fw_ddb_index);
302         if (err) {
303                 DEBUG2(printk(KERN_ERR "Could not add session.\n"));
304                 return err;
305         }
306
307         ddb_entry->conn = iscsi_create_conn(ddb_entry->sess, 0, 0);
308         if (!ddb_entry->conn) {
309                 iscsi_remove_session(ddb_entry->sess);
310                 DEBUG2(printk(KERN_ERR "Could not add connection.\n"));
311                 return -ENOMEM;
312         }
313
314         /* finally ready to go */
315         iscsi_unblock_session(ddb_entry->sess);
316         return 0;
317 }
318
319 struct ddb_entry *qla4xxx_alloc_sess(struct scsi_qla_host *ha)
320 {
321         struct ddb_entry *ddb_entry;
322         struct iscsi_cls_session *sess;
323
324         sess = iscsi_alloc_session(ha->host, &qla4xxx_iscsi_transport,
325                                    sizeof(struct ddb_entry));
326         if (!sess)
327                 return NULL;
328
329         ddb_entry = sess->dd_data;
330         memset(ddb_entry, 0, sizeof(*ddb_entry));
331         ddb_entry->ha = ha;
332         ddb_entry->sess = sess;
333         return ddb_entry;
334 }
335
336 static void qla4xxx_scan_start(struct Scsi_Host *shost)
337 {
338         struct scsi_qla_host *ha = shost_priv(shost);
339         struct ddb_entry *ddb_entry, *ddbtemp;
340
341         /* finish setup of sessions that were already setup in firmware */
342         list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
343                 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE)
344                         qla4xxx_add_sess(ddb_entry);
345         }
346 }
347
348 /*
349  * Timer routines
350  */
351
352 static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
353                                 unsigned long interval)
354 {
355         DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
356                      __func__, ha->host->host_no));
357         init_timer(&ha->timer);
358         ha->timer.expires = jiffies + interval * HZ;
359         ha->timer.data = (unsigned long)ha;
360         ha->timer.function = (void (*)(unsigned long))func;
361         add_timer(&ha->timer);
362         ha->timer_active = 1;
363 }
364
365 static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
366 {
367         del_timer_sync(&ha->timer);
368         ha->timer_active = 0;
369 }
370
371 /***
372  * qla4xxx_mark_device_missing - mark a device as missing.
373  * @ha: Pointer to host adapter structure.
374  * @ddb_entry: Pointer to device database entry
375  *
376  * This routine marks a device missing and close connection.
377  **/
378 void qla4xxx_mark_device_missing(struct scsi_qla_host *ha,
379                                  struct ddb_entry *ddb_entry)
380 {
381         if ((atomic_read(&ddb_entry->state) != DDB_STATE_DEAD)) {
382                 atomic_set(&ddb_entry->state, DDB_STATE_MISSING);
383                 DEBUG2(printk("scsi%ld: ddb [%d] marked MISSING\n",
384                     ha->host_no, ddb_entry->fw_ddb_index));
385         } else
386                 DEBUG2(printk("scsi%ld: ddb [%d] DEAD\n", ha->host_no,
387                     ddb_entry->fw_ddb_index))
388
389         iscsi_block_session(ddb_entry->sess);
390         iscsi_conn_error_event(ddb_entry->conn, ISCSI_ERR_CONN_FAILED);
391 }
392
393 /**
394  * qla4xxx_mark_all_devices_missing - mark all devices as missing.
395  * @ha: Pointer to host adapter structure.
396  *
397  * This routine marks a device missing and resets the relogin retry count.
398  **/
399 void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
400 {
401         struct ddb_entry *ddb_entry, *ddbtemp;
402         list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
403                 qla4xxx_mark_device_missing(ha, ddb_entry);
404         }
405 }
406
407 static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
408                                        struct ddb_entry *ddb_entry,
409                                        struct scsi_cmnd *cmd,
410                                        void (*done)(struct scsi_cmnd *))
411 {
412         struct srb *srb;
413
414         srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
415         if (!srb)
416                 return srb;
417
418         kref_init(&srb->srb_ref);
419         srb->ha = ha;
420         srb->ddb = ddb_entry;
421         srb->cmd = cmd;
422         srb->flags = 0;
423         CMD_SP(cmd) = (void *)srb;
424         cmd->scsi_done = done;
425
426         return srb;
427 }
428
429 static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
430 {
431         struct scsi_cmnd *cmd = srb->cmd;
432
433         if (srb->flags & SRB_DMA_VALID) {
434                 scsi_dma_unmap(cmd);
435                 srb->flags &= ~SRB_DMA_VALID;
436         }
437         CMD_SP(cmd) = NULL;
438 }
439
440 void qla4xxx_srb_compl(struct kref *ref)
441 {
442         struct srb *srb = container_of(ref, struct srb, srb_ref);
443         struct scsi_cmnd *cmd = srb->cmd;
444         struct scsi_qla_host *ha = srb->ha;
445
446         qla4xxx_srb_free_dma(ha, srb);
447
448         mempool_free(srb, ha->srb_mempool);
449
450         cmd->scsi_done(cmd);
451 }
452
453 /**
454  * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
455  * @cmd: Pointer to Linux's SCSI command structure
456  * @done_fn: Function that the driver calls to notify the SCSI mid-layer
457  *      that the command has been processed.
458  *
459  * Remarks:
460  * This routine is invoked by Linux to send a SCSI command to the driver.
461  * The mid-level driver tries to ensure that queuecommand never gets
462  * invoked concurrently with itself or the interrupt handler (although
463  * the interrupt handler may call this routine as part of request-
464  * completion handling).   Unfortunely, it sometimes calls the scheduler
465  * in interrupt context which is a big NO! NO!.
466  **/
467 static int qla4xxx_queuecommand(struct scsi_cmnd *cmd,
468                                 void (*done)(struct scsi_cmnd *))
469 {
470         struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
471         struct ddb_entry *ddb_entry = cmd->device->hostdata;
472         struct iscsi_cls_session *sess = ddb_entry->sess;
473         struct srb *srb;
474         int rval;
475
476         if (test_bit(AF_EEH_BUSY, &ha->flags)) {
477                 if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
478                         cmd->result = DID_NO_CONNECT << 16;
479                 else
480                         cmd->result = DID_REQUEUE << 16;
481                 goto qc_fail_command;
482         }
483
484         if (!sess) {
485                 cmd->result = DID_IMM_RETRY << 16;
486                 goto qc_fail_command;
487         }
488
489         rval = iscsi_session_chkready(sess);
490         if (rval) {
491                 cmd->result = rval;
492                 goto qc_fail_command;
493         }
494
495         if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
496                 if (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD) {
497                         cmd->result = DID_NO_CONNECT << 16;
498                         goto qc_fail_command;
499                 }
500                 return SCSI_MLQUEUE_TARGET_BUSY;
501         }
502
503         if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
504             test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
505             test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
506             test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
507             test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
508             !test_bit(AF_ONLINE, &ha->flags) ||
509             test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
510                 goto qc_host_busy;
511
512         spin_unlock_irq(ha->host->host_lock);
513
514         srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd, done);
515         if (!srb)
516                 goto qc_host_busy_lock;
517
518         rval = qla4xxx_send_command_to_isp(ha, srb);
519         if (rval != QLA_SUCCESS)
520                 goto qc_host_busy_free_sp;
521
522         spin_lock_irq(ha->host->host_lock);
523         return 0;
524
525 qc_host_busy_free_sp:
526         qla4xxx_srb_free_dma(ha, srb);
527         mempool_free(srb, ha->srb_mempool);
528
529 qc_host_busy_lock:
530         spin_lock_irq(ha->host->host_lock);
531
532 qc_host_busy:
533         return SCSI_MLQUEUE_HOST_BUSY;
534
535 qc_fail_command:
536         done(cmd);
537
538         return 0;
539 }
540
541 /**
542  * qla4xxx_mem_free - frees memory allocated to adapter
543  * @ha: Pointer to host adapter structure.
544  *
545  * Frees memory previously allocated by qla4xxx_mem_alloc
546  **/
547 static void qla4xxx_mem_free(struct scsi_qla_host *ha)
548 {
549         if (ha->queues)
550                 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
551                                   ha->queues_dma);
552
553         ha->queues_len = 0;
554         ha->queues = NULL;
555         ha->queues_dma = 0;
556         ha->request_ring = NULL;
557         ha->request_dma = 0;
558         ha->response_ring = NULL;
559         ha->response_dma = 0;
560         ha->shadow_regs = NULL;
561         ha->shadow_regs_dma = 0;
562
563         /* Free srb pool. */
564         if (ha->srb_mempool)
565                 mempool_destroy(ha->srb_mempool);
566
567         ha->srb_mempool = NULL;
568
569         /* release io space registers  */
570         if (is_qla8022(ha)) {
571                 if (ha->nx_pcibase)
572                         iounmap(
573                             (struct device_reg_82xx __iomem *)ha->nx_pcibase);
574
575                 if (ha->nx_db_wr_ptr)
576                         iounmap(
577                             (struct device_reg_82xx __iomem *)ha->nx_db_wr_ptr);
578         } else if (ha->reg)
579                 iounmap(ha->reg);
580         pci_release_regions(ha->pdev);
581 }
582
583 /**
584  * qla4xxx_mem_alloc - allocates memory for use by adapter.
585  * @ha: Pointer to host adapter structure
586  *
587  * Allocates DMA memory for request and response queues. Also allocates memory
588  * for srbs.
589  **/
590 static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
591 {
592         unsigned long align;
593
594         /* Allocate contiguous block of DMA memory for queues. */
595         ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
596                           (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
597                           sizeof(struct shadow_regs) +
598                           MEM_ALIGN_VALUE +
599                           (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
600         ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
601                                         &ha->queues_dma, GFP_KERNEL);
602         if (ha->queues == NULL) {
603                 ql4_printk(KERN_WARNING, ha,
604                     "Memory Allocation failed - queues.\n");
605
606                 goto mem_alloc_error_exit;
607         }
608         memset(ha->queues, 0, ha->queues_len);
609
610         /*
611          * As per RISC alignment requirements -- the bus-address must be a
612          * multiple of the request-ring size (in bytes).
613          */
614         align = 0;
615         if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
616                 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
617                                            (MEM_ALIGN_VALUE - 1));
618
619         /* Update request and response queue pointers. */
620         ha->request_dma = ha->queues_dma + align;
621         ha->request_ring = (struct queue_entry *) (ha->queues + align);
622         ha->response_dma = ha->queues_dma + align +
623                 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
624         ha->response_ring = (struct queue_entry *) (ha->queues + align +
625                                                     (REQUEST_QUEUE_DEPTH *
626                                                      QUEUE_SIZE));
627         ha->shadow_regs_dma = ha->queues_dma + align +
628                 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
629                 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
630         ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
631                                                   (REQUEST_QUEUE_DEPTH *
632                                                    QUEUE_SIZE) +
633                                                   (RESPONSE_QUEUE_DEPTH *
634                                                    QUEUE_SIZE));
635
636         /* Allocate memory for srb pool. */
637         ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
638                                          mempool_free_slab, srb_cachep);
639         if (ha->srb_mempool == NULL) {
640                 ql4_printk(KERN_WARNING, ha,
641                     "Memory Allocation failed - SRB Pool.\n");
642
643                 goto mem_alloc_error_exit;
644         }
645
646         return QLA_SUCCESS;
647
648 mem_alloc_error_exit:
649         qla4xxx_mem_free(ha);
650         return QLA_ERROR;
651 }
652
653 /**
654  * qla4_8xxx_check_fw_alive  - Check firmware health
655  * @ha: Pointer to host adapter structure.
656  *
657  * Context: Interrupt
658  **/
659 static void qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
660 {
661         uint32_t fw_heartbeat_counter, halt_status;
662
663         fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
664         /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
665         if (fw_heartbeat_counter == 0xffffffff) {
666                 DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
667                     "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
668                     ha->host_no, __func__));
669                 return;
670         }
671
672         if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
673                 ha->seconds_since_last_heartbeat++;
674                 /* FW not alive after 2 seconds */
675                 if (ha->seconds_since_last_heartbeat == 2) {
676                         ha->seconds_since_last_heartbeat = 0;
677                         halt_status = qla4_8xxx_rd_32(ha,
678                             QLA82XX_PEG_HALT_STATUS1);
679
680                         /* Since we cannot change dev_state in interrupt
681                          * context, set appropriate DPC flag then wakeup
682                          * DPC */
683                         if (halt_status & HALT_STATUS_UNRECOVERABLE)
684                                 set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
685                         else {
686                                 printk("scsi%ld: %s: detect abort needed!\n",
687                                     ha->host_no, __func__);
688                                 set_bit(DPC_RESET_HA, &ha->dpc_flags);
689                         }
690                         qla4xxx_wake_dpc(ha);
691                         qla4xxx_mailbox_premature_completion(ha);
692                 }
693         } else
694                 ha->seconds_since_last_heartbeat = 0;
695
696         ha->fw_heartbeat_counter = fw_heartbeat_counter;
697 }
698
699 /**
700  * qla4_8xxx_watchdog - Poll dev state
701  * @ha: Pointer to host adapter structure.
702  *
703  * Context: Interrupt
704  **/
705 void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
706 {
707         uint32_t dev_state;
708
709         dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
710
711         /* don't poll if reset is going on */
712         if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags)) {
713                 if (dev_state == QLA82XX_DEV_NEED_RESET &&
714                     !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
715                         printk("scsi%ld: %s: HW State: NEED RESET!\n",
716                             ha->host_no, __func__);
717                         set_bit(DPC_RESET_HA, &ha->dpc_flags);
718                         qla4xxx_wake_dpc(ha);
719                         qla4xxx_mailbox_premature_completion(ha);
720                 } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
721                     !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
722                         printk("scsi%ld: %s: HW State: NEED QUIES!\n",
723                             ha->host_no, __func__);
724                         set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
725                         qla4xxx_wake_dpc(ha);
726                 } else  {
727                         /* Check firmware health */
728                         qla4_8xxx_check_fw_alive(ha);
729                 }
730         }
731 }
732
733 /**
734  * qla4xxx_timer - checks every second for work to do.
735  * @ha: Pointer to host adapter structure.
736  **/
737 static void qla4xxx_timer(struct scsi_qla_host *ha)
738 {
739         struct ddb_entry *ddb_entry, *dtemp;
740         int start_dpc = 0;
741         uint16_t w;
742
743         /* If we are in the middle of AER/EEH processing
744          * skip any processing and reschedule the timer
745          */
746         if (test_bit(AF_EEH_BUSY, &ha->flags)) {
747                 mod_timer(&ha->timer, jiffies + HZ);
748                 return;
749         }
750
751         /* Hardware read to trigger an EEH error during mailbox waits. */
752         if (!pci_channel_offline(ha->pdev))
753                 pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
754
755         if (test_bit(AF_HBA_GOING_AWAY, &ha->flags)) {
756                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s exited. HBA GOING AWAY\n",
757                     __func__));
758                 return;
759         }
760
761         if (is_qla8022(ha)) {
762                 qla4_8xxx_watchdog(ha);
763         }
764
765         /* Search for relogin's to time-out and port down retry. */
766         list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
767                 /* Count down time between sending relogins */
768                 if (adapter_up(ha) &&
769                     !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
770                     atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
771                         if (atomic_read(&ddb_entry->retry_relogin_timer) !=
772                             INVALID_ENTRY) {
773                                 if (atomic_read(&ddb_entry->retry_relogin_timer)
774                                                 == 0) {
775                                         atomic_set(&ddb_entry->
776                                                 retry_relogin_timer,
777                                                 INVALID_ENTRY);
778                                         set_bit(DPC_RELOGIN_DEVICE,
779                                                 &ha->dpc_flags);
780                                         set_bit(DF_RELOGIN, &ddb_entry->flags);
781                                         DEBUG2(printk("scsi%ld: %s: ddb [%d]"
782                                                       " login device\n",
783                                                       ha->host_no, __func__,
784                                                       ddb_entry->fw_ddb_index));
785                                 } else
786                                         atomic_dec(&ddb_entry->
787                                                         retry_relogin_timer);
788                         }
789                 }
790
791                 /* Wait for relogin to timeout */
792                 if (atomic_read(&ddb_entry->relogin_timer) &&
793                     (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
794                         /*
795                          * If the relogin times out and the device is
796                          * still NOT ONLINE then try and relogin again.
797                          */
798                         if (atomic_read(&ddb_entry->state) !=
799                             DDB_STATE_ONLINE &&
800                             ddb_entry->fw_ddb_device_state ==
801                             DDB_DS_SESSION_FAILED) {
802                                 /* Reset retry relogin timer */
803                                 atomic_inc(&ddb_entry->relogin_retry_count);
804                                 DEBUG2(printk("scsi%ld: ddb [%d] relogin"
805                                               " timed out-retrying"
806                                               " relogin (%d)\n",
807                                               ha->host_no,
808                                               ddb_entry->fw_ddb_index,
809                                               atomic_read(&ddb_entry->
810                                                           relogin_retry_count))
811                                         );
812                                 start_dpc++;
813                                 DEBUG(printk("scsi%ld:%d:%d: ddb [%d] "
814                                              "initate relogin after"
815                                              " %d seconds\n",
816                                              ha->host_no, ddb_entry->bus,
817                                              ddb_entry->target,
818                                              ddb_entry->fw_ddb_index,
819                                              ddb_entry->default_time2wait + 4)
820                                         );
821
822                                 atomic_set(&ddb_entry->retry_relogin_timer,
823                                            ddb_entry->default_time2wait + 4);
824                         }
825                 }
826         }
827
828         if (!is_qla8022(ha)) {
829                 /* Check for heartbeat interval. */
830                 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
831                     ha->heartbeat_interval != 0) {
832                         ha->seconds_since_last_heartbeat++;
833                         if (ha->seconds_since_last_heartbeat >
834                             ha->heartbeat_interval + 2)
835                                 set_bit(DPC_RESET_HA, &ha->dpc_flags);
836                 }
837         }
838
839         /* Wakeup the dpc routine for this adapter, if needed. */
840         if ((start_dpc ||
841              test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
842              test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
843              test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
844              test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
845              test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
846              test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
847              test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
848              test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
849              test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
850              test_bit(DPC_AEN, &ha->dpc_flags)) &&
851              !test_bit(AF_DPC_SCHEDULED, &ha->flags) &&
852              ha->dpc_thread) {
853                 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
854                               " - dpc flags = 0x%lx\n",
855                               ha->host_no, __func__, ha->dpc_flags));
856                 qla4xxx_wake_dpc(ha);
857         }
858
859         /* Reschedule timer thread to call us back in one second */
860         mod_timer(&ha->timer, jiffies + HZ);
861
862         DEBUG2(ha->seconds_since_last_intr++);
863 }
864
865 /**
866  * qla4xxx_cmd_wait - waits for all outstanding commands to complete
867  * @ha: Pointer to host adapter structure.
868  *
869  * This routine stalls the driver until all outstanding commands are returned.
870  * Caller must release the Hardware Lock prior to calling this routine.
871  **/
872 static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
873 {
874         uint32_t index = 0;
875         unsigned long flags;
876         struct scsi_cmnd *cmd;
877
878         unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
879
880         DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
881             "complete\n", WAIT_CMD_TOV));
882
883         while (!time_after_eq(jiffies, wtime)) {
884                 spin_lock_irqsave(&ha->hardware_lock, flags);
885                 /* Find a command that hasn't completed. */
886                 for (index = 0; index < ha->host->can_queue; index++) {
887                         cmd = scsi_host_find_tag(ha->host, index);
888                         if (cmd != NULL)
889                                 break;
890                 }
891                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
892
893                 /* If No Commands are pending, wait is complete */
894                 if (index == ha->host->can_queue)
895                         return QLA_SUCCESS;
896
897                 msleep(1000);
898         }
899         /* If we timed out on waiting for commands to come back
900          * return ERROR. */
901         return QLA_ERROR;
902 }
903
904 int qla4xxx_hw_reset(struct scsi_qla_host *ha)
905 {
906         uint32_t ctrl_status;
907         unsigned long flags = 0;
908
909         DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
910
911         if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
912                 return QLA_ERROR;
913
914         spin_lock_irqsave(&ha->hardware_lock, flags);
915
916         /*
917          * If the SCSI Reset Interrupt bit is set, clear it.
918          * Otherwise, the Soft Reset won't work.
919          */
920         ctrl_status = readw(&ha->reg->ctrl_status);
921         if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
922                 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
923
924         /* Issue Soft Reset */
925         writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
926         readl(&ha->reg->ctrl_status);
927
928         spin_unlock_irqrestore(&ha->hardware_lock, flags);
929         return QLA_SUCCESS;
930 }
931
932 /**
933  * qla4xxx_soft_reset - performs soft reset.
934  * @ha: Pointer to host adapter structure.
935  **/
936 int qla4xxx_soft_reset(struct scsi_qla_host *ha)
937 {
938         uint32_t max_wait_time;
939         unsigned long flags = 0;
940         int status;
941         uint32_t ctrl_status;
942
943         status = qla4xxx_hw_reset(ha);
944         if (status != QLA_SUCCESS)
945                 return status;
946
947         status = QLA_ERROR;
948         /* Wait until the Network Reset Intr bit is cleared */
949         max_wait_time = RESET_INTR_TOV;
950         do {
951                 spin_lock_irqsave(&ha->hardware_lock, flags);
952                 ctrl_status = readw(&ha->reg->ctrl_status);
953                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
954
955                 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
956                         break;
957
958                 msleep(1000);
959         } while ((--max_wait_time));
960
961         if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
962                 DEBUG2(printk(KERN_WARNING
963                               "scsi%ld: Network Reset Intr not cleared by "
964                               "Network function, clearing it now!\n",
965                               ha->host_no));
966                 spin_lock_irqsave(&ha->hardware_lock, flags);
967                 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
968                 readl(&ha->reg->ctrl_status);
969                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
970         }
971
972         /* Wait until the firmware tells us the Soft Reset is done */
973         max_wait_time = SOFT_RESET_TOV;
974         do {
975                 spin_lock_irqsave(&ha->hardware_lock, flags);
976                 ctrl_status = readw(&ha->reg->ctrl_status);
977                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
978
979                 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
980                         status = QLA_SUCCESS;
981                         break;
982                 }
983
984                 msleep(1000);
985         } while ((--max_wait_time));
986
987         /*
988          * Also, make sure that the SCSI Reset Interrupt bit has been cleared
989          * after the soft reset has taken place.
990          */
991         spin_lock_irqsave(&ha->hardware_lock, flags);
992         ctrl_status = readw(&ha->reg->ctrl_status);
993         if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
994                 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
995                 readl(&ha->reg->ctrl_status);
996         }
997         spin_unlock_irqrestore(&ha->hardware_lock, flags);
998
999         /* If soft reset fails then most probably the bios on other
1000          * function is also enabled.
1001          * Since the initialization is sequential the other fn
1002          * wont be able to acknowledge the soft reset.
1003          * Issue a force soft reset to workaround this scenario.
1004          */
1005         if (max_wait_time == 0) {
1006                 /* Issue Force Soft Reset */
1007                 spin_lock_irqsave(&ha->hardware_lock, flags);
1008                 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
1009                 readl(&ha->reg->ctrl_status);
1010                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1011                 /* Wait until the firmware tells us the Soft Reset is done */
1012                 max_wait_time = SOFT_RESET_TOV;
1013                 do {
1014                         spin_lock_irqsave(&ha->hardware_lock, flags);
1015                         ctrl_status = readw(&ha->reg->ctrl_status);
1016                         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1017
1018                         if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
1019                                 status = QLA_SUCCESS;
1020                                 break;
1021                         }
1022
1023                         msleep(1000);
1024                 } while ((--max_wait_time));
1025         }
1026
1027         return status;
1028 }
1029
1030 /**
1031  * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
1032  * @ha: Pointer to host adapter structure.
1033  * @res: returned scsi status
1034  *
1035  * This routine is called just prior to a HARD RESET to return all
1036  * outstanding commands back to the Operating System.
1037  * Caller should make sure that the following locks are released
1038  * before this calling routine: Hardware lock, and io_request_lock.
1039  **/
1040 static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
1041 {
1042         struct srb *srb;
1043         int i;
1044         unsigned long flags;
1045
1046         spin_lock_irqsave(&ha->hardware_lock, flags);
1047         for (i = 0; i < ha->host->can_queue; i++) {
1048                 srb = qla4xxx_del_from_active_array(ha, i);
1049                 if (srb != NULL) {
1050                         srb->cmd->result = res;
1051                         kref_put(&srb->srb_ref, qla4xxx_srb_compl);
1052                 }
1053         }
1054         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1055 }
1056
1057 void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
1058 {
1059         clear_bit(AF_ONLINE, &ha->flags);
1060
1061         /* Disable the board */
1062         ql4_printk(KERN_INFO, ha, "Disabling the board\n");
1063         set_bit(AF_HBA_GOING_AWAY, &ha->flags);
1064
1065         qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
1066         qla4xxx_mark_all_devices_missing(ha);
1067         clear_bit(AF_INIT_DONE, &ha->flags);
1068 }
1069
1070 /**
1071  * qla4xxx_recover_adapter - recovers adapter after a fatal error
1072  * @ha: Pointer to host adapter structure.
1073  **/
1074 static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
1075 {
1076         int status = QLA_ERROR;
1077         uint8_t reset_chip = 0;
1078
1079         /* Stall incoming I/O until we are done */
1080         scsi_block_requests(ha->host);
1081         clear_bit(AF_ONLINE, &ha->flags);
1082
1083         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
1084
1085         set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
1086
1087         if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
1088                 reset_chip = 1;
1089
1090         /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
1091          * do not reset adapter, jump to initialize_adapter */
1092         if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1093                 status = QLA_SUCCESS;
1094                 goto recover_ha_init_adapter;
1095         }
1096
1097         /* For the ISP-82xx adapter, issue a stop_firmware if invoked
1098          * from eh_host_reset or ioctl module */
1099         if (is_qla8022(ha) && !reset_chip &&
1100             test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
1101
1102                 DEBUG2(ql4_printk(KERN_INFO, ha,
1103                     "scsi%ld: %s - Performing stop_firmware...\n",
1104                     ha->host_no, __func__));
1105                 status = ha->isp_ops->reset_firmware(ha);
1106                 if (status == QLA_SUCCESS) {
1107                         if (!test_bit(AF_FW_RECOVERY, &ha->flags))
1108                                 qla4xxx_cmd_wait(ha);
1109                         ha->isp_ops->disable_intrs(ha);
1110                         qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1111                         qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1112                 } else {
1113                         /* If the stop_firmware fails then
1114                          * reset the entire chip */
1115                         reset_chip = 1;
1116                         clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1117                         set_bit(DPC_RESET_HA, &ha->dpc_flags);
1118                 }
1119         }
1120
1121         /* Issue full chip reset if recovering from a catastrophic error,
1122          * or if stop_firmware fails for ISP-82xx.
1123          * This is the default case for ISP-4xxx */
1124         if (!is_qla8022(ha) || reset_chip) {
1125                 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
1126                         qla4xxx_cmd_wait(ha);
1127                 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1128                 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1129                 DEBUG2(ql4_printk(KERN_INFO, ha,
1130                     "scsi%ld: %s - Performing chip reset..\n",
1131                     ha->host_no, __func__));
1132                 status = ha->isp_ops->reset_chip(ha);
1133         }
1134
1135         /* Flush any pending ddb changed AENs */
1136         qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1137
1138 recover_ha_init_adapter:
1139         /* Upon successful firmware/chip reset, re-initialize the adapter */
1140         if (status == QLA_SUCCESS) {
1141                 /* For ISP-4xxx, force function 1 to always initialize
1142                  * before function 3 to prevent both funcions from
1143                  * stepping on top of the other */
1144                 if (!is_qla8022(ha) && (ha->mac_index == 3))
1145                         ssleep(6);
1146
1147                 /* NOTE: AF_ONLINE flag set upon successful completion of
1148                  *       qla4xxx_initialize_adapter */
1149                 status = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
1150         }
1151
1152         /* Retry failed adapter initialization, if necessary
1153          * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
1154          * case to prevent ping-pong resets between functions */
1155         if (!test_bit(AF_ONLINE, &ha->flags) &&
1156             !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1157                 /* Adapter initialization failed, see if we can retry
1158                  * resetting the ha.
1159                  * Since we don't want to block the DPC for too long
1160                  * with multiple resets in the same thread,
1161                  * utilize DPC to retry */
1162                 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
1163                         ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
1164                         DEBUG2(printk("scsi%ld: recover adapter - retrying "
1165                                       "(%d) more times\n", ha->host_no,
1166                                       ha->retry_reset_ha_cnt));
1167                         set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1168                         status = QLA_ERROR;
1169                 } else {
1170                         if (ha->retry_reset_ha_cnt > 0) {
1171                                 /* Schedule another Reset HA--DPC will retry */
1172                                 ha->retry_reset_ha_cnt--;
1173                                 DEBUG2(printk("scsi%ld: recover adapter - "
1174                                               "retry remaining %d\n",
1175                                               ha->host_no,
1176                                               ha->retry_reset_ha_cnt));
1177                                 status = QLA_ERROR;
1178                         }
1179
1180                         if (ha->retry_reset_ha_cnt == 0) {
1181                                 /* Recover adapter retries have been exhausted.
1182                                  * Adapter DEAD */
1183                                 DEBUG2(printk("scsi%ld: recover adapter "
1184                                               "failed - board disabled\n",
1185                                               ha->host_no));
1186                                 qla4xxx_dead_adapter_cleanup(ha);
1187                                 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1188                                 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
1189                                 clear_bit(DPC_RESET_HA_FW_CONTEXT,
1190                                           &ha->dpc_flags);
1191                                 status = QLA_ERROR;
1192                         }
1193                 }
1194         } else {
1195                 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
1196                 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1197                 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1198         }
1199
1200         ha->adapter_error_count++;
1201
1202         if (test_bit(AF_ONLINE, &ha->flags))
1203                 ha->isp_ops->enable_intrs(ha);
1204
1205         scsi_unblock_requests(ha->host);
1206
1207         clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
1208         DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
1209             status == QLA_ERROR ? "FAILED" : "SUCCEDED"));
1210
1211         return status;
1212 }
1213
1214 void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
1215 {
1216         if (ha->dpc_thread &&
1217             !test_bit(AF_DPC_SCHEDULED, &ha->flags)) {
1218                 set_bit(AF_DPC_SCHEDULED, &ha->flags);
1219                 queue_work(ha->dpc_thread, &ha->dpc_work);
1220         }
1221 }
1222
1223 /**
1224  * qla4xxx_do_dpc - dpc routine
1225  * @data: in our case pointer to adapter structure
1226  *
1227  * This routine is a task that is schedule by the interrupt handler
1228  * to perform the background processing for interrupts.  We put it
1229  * on a task queue that is consumed whenever the scheduler runs; that's
1230  * so you can do anything (i.e. put the process to sleep etc).  In fact,
1231  * the mid-level tries to sleep when it reaches the driver threshold
1232  * "host->can_queue". This can cause a panic if we were in our interrupt code.
1233  **/
1234 static void qla4xxx_do_dpc(struct work_struct *work)
1235 {
1236         struct scsi_qla_host *ha =
1237                 container_of(work, struct scsi_qla_host, dpc_work);
1238         struct ddb_entry *ddb_entry, *dtemp;
1239         int status = QLA_ERROR;
1240
1241         DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
1242             "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
1243             ha->host_no, __func__, ha->flags, ha->dpc_flags))
1244
1245         /* Initialization not yet finished. Don't do anything yet. */
1246         if (!test_bit(AF_INIT_DONE, &ha->flags))
1247                 goto do_dpc_exit;
1248
1249         if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1250                 DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
1251                     ha->host_no, __func__, ha->flags));
1252                 goto do_dpc_exit;
1253         }
1254
1255         /* HBA is in the process of being permanently disabled.
1256          * Don't process anything */
1257         if (test_bit(AF_HBA_GOING_AWAY, &ha->flags))
1258                 return;
1259
1260         if (is_qla8022(ha)) {
1261                 if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
1262                         qla4_8xxx_idc_lock(ha);
1263                         qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
1264                             QLA82XX_DEV_FAILED);
1265                         qla4_8xxx_idc_unlock(ha);
1266                         ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
1267                         qla4_8xxx_device_state_handler(ha);
1268                 }
1269                 if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
1270                         qla4_8xxx_need_qsnt_handler(ha);
1271                 }
1272         }
1273
1274         if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
1275             (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1276             test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1277             test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
1278                 if (ql4xdontresethba) {
1279                         DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
1280                             ha->host_no, __func__));
1281                         clear_bit(DPC_RESET_HA, &ha->dpc_flags);
1282                         clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1283                         clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1284                         goto dpc_post_reset_ha;
1285                 }
1286                 if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
1287                     test_bit(DPC_RESET_HA, &ha->dpc_flags))
1288                         qla4xxx_recover_adapter(ha);
1289
1290                 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1291                         uint8_t wait_time = RESET_INTR_TOV;
1292
1293                         while ((readw(&ha->reg->ctrl_status) &
1294                                 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
1295                                 if (--wait_time == 0)
1296                                         break;
1297                                 msleep(1000);
1298                         }
1299                         if (wait_time == 0)
1300                                 DEBUG2(printk("scsi%ld: %s: SR|FSR "
1301                                               "bit not cleared-- resetting\n",
1302                                               ha->host_no, __func__));
1303                         qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1304                         if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
1305                                 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1306                                 status = qla4xxx_recover_adapter(ha);
1307                         }
1308                         clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1309                         if (status == QLA_SUCCESS)
1310                                 ha->isp_ops->enable_intrs(ha);
1311                 }
1312         }
1313
1314 dpc_post_reset_ha:
1315         /* ---- process AEN? --- */
1316         if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
1317                 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
1318
1319         /* ---- Get DHCP IP Address? --- */
1320         if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
1321                 qla4xxx_get_dhcp_ip_address(ha);
1322
1323         /* ---- link change? --- */
1324         if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
1325                 if (!test_bit(AF_LINK_UP, &ha->flags)) {
1326                         /* ---- link down? --- */
1327                         list_for_each_entry_safe(ddb_entry, dtemp,
1328                                                  &ha->ddb_list, list) {
1329                                 if (atomic_read(&ddb_entry->state) ==
1330                                                 DDB_STATE_ONLINE)
1331                                         qla4xxx_mark_device_missing(ha,
1332                                                         ddb_entry);
1333                         }
1334                 } else {
1335                         /* ---- link up? --- *
1336                          * F/W will auto login to all devices ONLY ONCE after
1337                          * link up during driver initialization and runtime
1338                          * fatal error recovery.  Therefore, the driver must
1339                          * manually relogin to devices when recovering from
1340                          * connection failures, logouts, expired KATO, etc. */
1341
1342                         list_for_each_entry_safe(ddb_entry, dtemp,
1343                                                         &ha->ddb_list, list) {
1344                                 if ((atomic_read(&ddb_entry->state) ==
1345                                                  DDB_STATE_MISSING) ||
1346                                     (atomic_read(&ddb_entry->state) ==
1347                                                  DDB_STATE_DEAD)) {
1348                                         if (ddb_entry->fw_ddb_device_state ==
1349                                             DDB_DS_SESSION_ACTIVE) {
1350                                                 atomic_set(&ddb_entry->state,
1351                                                            DDB_STATE_ONLINE);
1352                                                 ql4_printk(KERN_INFO, ha,
1353                                                     "scsi%ld: %s: ddb[%d]"
1354                                                     " marked ONLINE\n",
1355                                                     ha->host_no, __func__,
1356                                                     ddb_entry->fw_ddb_index);
1357
1358                                                 iscsi_unblock_session(
1359                                                     ddb_entry->sess);
1360                                         } else
1361                                                 qla4xxx_relogin_device(
1362                                                     ha, ddb_entry);
1363                                 }
1364
1365                         }
1366                 }
1367         }
1368
1369         /* ---- relogin device? --- */
1370         if (adapter_up(ha) &&
1371             test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
1372                 list_for_each_entry_safe(ddb_entry, dtemp,
1373                                          &ha->ddb_list, list) {
1374                         if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
1375                             atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
1376                                 qla4xxx_relogin_device(ha, ddb_entry);
1377
1378                         /*
1379                          * If mbx cmd times out there is no point
1380                          * in continuing further.
1381                          * With large no of targets this can hang
1382                          * the system.
1383                          */
1384                         if (test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
1385                                 printk(KERN_WARNING "scsi%ld: %s: "
1386                                        "need to reset hba\n",
1387                                        ha->host_no, __func__);
1388                                 break;
1389                         }
1390                 }
1391         }
1392
1393 do_dpc_exit:
1394         clear_bit(AF_DPC_SCHEDULED, &ha->flags);
1395 }
1396
1397 /**
1398  * qla4xxx_free_adapter - release the adapter
1399  * @ha: pointer to adapter structure
1400  **/
1401 static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
1402 {
1403
1404         if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
1405                 /* Turn-off interrupts on the card. */
1406                 ha->isp_ops->disable_intrs(ha);
1407         }
1408
1409         /* Remove timer thread, if present */
1410         if (ha->timer_active)
1411                 qla4xxx_stop_timer(ha);
1412
1413         /* Kill the kernel thread for this host */
1414         if (ha->dpc_thread)
1415                 destroy_workqueue(ha->dpc_thread);
1416
1417         /* Put firmware in known state */
1418         ha->isp_ops->reset_firmware(ha);
1419
1420         if (is_qla8022(ha)) {
1421                 qla4_8xxx_idc_lock(ha);
1422                 qla4_8xxx_clear_drv_active(ha);
1423                 qla4_8xxx_idc_unlock(ha);
1424         }
1425
1426         /* Detach interrupts */
1427         if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
1428                 qla4xxx_free_irqs(ha);
1429
1430         /* free extra memory */
1431         qla4xxx_mem_free(ha);
1432 }
1433
1434 int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
1435 {
1436         int status = 0;
1437         uint8_t revision_id;
1438         unsigned long mem_base, mem_len, db_base, db_len;
1439         struct pci_dev *pdev = ha->pdev;
1440
1441         status = pci_request_regions(pdev, DRIVER_NAME);
1442         if (status) {
1443                 printk(KERN_WARNING
1444                     "scsi(%ld) Failed to reserve PIO regions (%s) "
1445                     "status=%d\n", ha->host_no, pci_name(pdev), status);
1446                 goto iospace_error_exit;
1447         }
1448
1449         pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
1450         DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
1451             __func__, revision_id));
1452         ha->revision_id = revision_id;
1453
1454         /* remap phys address */
1455         mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
1456         mem_len = pci_resource_len(pdev, 0);
1457         DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
1458             __func__, mem_base, mem_len));
1459
1460         /* mapping of pcibase pointer */
1461         ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
1462         if (!ha->nx_pcibase) {
1463                 printk(KERN_ERR
1464                     "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
1465                 pci_release_regions(ha->pdev);
1466                 goto iospace_error_exit;
1467         }
1468
1469         /* Mapping of IO base pointer, door bell read and write pointer */
1470
1471         /* mapping of IO base pointer */
1472         ha->qla4_8xxx_reg =
1473             (struct device_reg_82xx  __iomem *)((uint8_t *)ha->nx_pcibase +
1474             0xbc000 + (ha->pdev->devfn << 11));
1475
1476         db_base = pci_resource_start(pdev, 4);  /* doorbell is on bar 4 */
1477         db_len = pci_resource_len(pdev, 4);
1478
1479         /* mapping of doorbell write pointer */
1480         ha->nx_db_wr_ptr = (unsigned long)ioremap(db_base +
1481             (ha->pdev->devfn << 12), 4);
1482         if (!ha->nx_db_wr_ptr) {
1483                 printk(KERN_ERR
1484                     "cannot remap MMIO doorbell-write (%s), aborting\n",
1485                     pci_name(pdev));
1486                 goto iospace_error_exit;
1487         }
1488         /* mapping of doorbell read pointer */
1489         ha->nx_db_rd_ptr = (uint8_t *) ha->nx_pcibase + (512 * 1024) +
1490             (ha->pdev->devfn * 8);
1491         if (!ha->nx_db_rd_ptr)
1492                 printk(KERN_ERR
1493                     "cannot remap MMIO doorbell-read (%s), aborting\n",
1494                     pci_name(pdev));
1495         return 0;
1496
1497 iospace_error_exit:
1498         return -ENOMEM;
1499 }
1500
1501 /***
1502  * qla4xxx_iospace_config - maps registers
1503  * @ha: pointer to adapter structure
1504  *
1505  * This routines maps HBA's registers from the pci address space
1506  * into the kernel virtual address space for memory mapped i/o.
1507  **/
1508 int qla4xxx_iospace_config(struct scsi_qla_host *ha)
1509 {
1510         unsigned long pio, pio_len, pio_flags;
1511         unsigned long mmio, mmio_len, mmio_flags;
1512
1513         pio = pci_resource_start(ha->pdev, 0);
1514         pio_len = pci_resource_len(ha->pdev, 0);
1515         pio_flags = pci_resource_flags(ha->pdev, 0);
1516         if (pio_flags & IORESOURCE_IO) {
1517                 if (pio_len < MIN_IOBASE_LEN) {
1518                         ql4_printk(KERN_WARNING, ha,
1519                                 "Invalid PCI I/O region size\n");
1520                         pio = 0;
1521                 }
1522         } else {
1523                 ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
1524                 pio = 0;
1525         }
1526
1527         /* Use MMIO operations for all accesses. */
1528         mmio = pci_resource_start(ha->pdev, 1);
1529         mmio_len = pci_resource_len(ha->pdev, 1);
1530         mmio_flags = pci_resource_flags(ha->pdev, 1);
1531
1532         if (!(mmio_flags & IORESOURCE_MEM)) {
1533                 ql4_printk(KERN_ERR, ha,
1534                     "region #0 not an MMIO resource, aborting\n");
1535
1536                 goto iospace_error_exit;
1537         }
1538
1539         if (mmio_len < MIN_IOBASE_LEN) {
1540                 ql4_printk(KERN_ERR, ha,
1541                     "Invalid PCI mem region size, aborting\n");
1542                 goto iospace_error_exit;
1543         }
1544
1545         if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
1546                 ql4_printk(KERN_WARNING, ha,
1547                     "Failed to reserve PIO/MMIO regions\n");
1548
1549                 goto iospace_error_exit;
1550         }
1551
1552         ha->pio_address = pio;
1553         ha->pio_length = pio_len;
1554         ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
1555         if (!ha->reg) {
1556                 ql4_printk(KERN_ERR, ha,
1557                     "cannot remap MMIO, aborting\n");
1558
1559                 goto iospace_error_exit;
1560         }
1561
1562         return 0;
1563
1564 iospace_error_exit:
1565         return -ENOMEM;
1566 }
1567
1568 static struct isp_operations qla4xxx_isp_ops = {
1569         .iospace_config         = qla4xxx_iospace_config,
1570         .pci_config             = qla4xxx_pci_config,
1571         .disable_intrs          = qla4xxx_disable_intrs,
1572         .enable_intrs           = qla4xxx_enable_intrs,
1573         .start_firmware         = qla4xxx_start_firmware,
1574         .intr_handler           = qla4xxx_intr_handler,
1575         .interrupt_service_routine = qla4xxx_interrupt_service_routine,
1576         .reset_chip             = qla4xxx_soft_reset,
1577         .reset_firmware         = qla4xxx_hw_reset,
1578         .queue_iocb             = qla4xxx_queue_iocb,
1579         .complete_iocb          = qla4xxx_complete_iocb,
1580         .rd_shdw_req_q_out      = qla4xxx_rd_shdw_req_q_out,
1581         .rd_shdw_rsp_q_in       = qla4xxx_rd_shdw_rsp_q_in,
1582         .get_sys_info           = qla4xxx_get_sys_info,
1583 };
1584
1585 static struct isp_operations qla4_8xxx_isp_ops = {
1586         .iospace_config         = qla4_8xxx_iospace_config,
1587         .pci_config             = qla4_8xxx_pci_config,
1588         .disable_intrs          = qla4_8xxx_disable_intrs,
1589         .enable_intrs           = qla4_8xxx_enable_intrs,
1590         .start_firmware         = qla4_8xxx_load_risc,
1591         .intr_handler           = qla4_8xxx_intr_handler,
1592         .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
1593         .reset_chip             = qla4_8xxx_isp_reset,
1594         .reset_firmware         = qla4_8xxx_stop_firmware,
1595         .queue_iocb             = qla4_8xxx_queue_iocb,
1596         .complete_iocb          = qla4_8xxx_complete_iocb,
1597         .rd_shdw_req_q_out      = qla4_8xxx_rd_shdw_req_q_out,
1598         .rd_shdw_rsp_q_in       = qla4_8xxx_rd_shdw_rsp_q_in,
1599         .get_sys_info           = qla4_8xxx_get_sys_info,
1600 };
1601
1602 uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
1603 {
1604         return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
1605 }
1606
1607 uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
1608 {
1609         return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
1610 }
1611
1612 uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
1613 {
1614         return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
1615 }
1616
1617 uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
1618 {
1619         return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
1620 }
1621
1622 /**
1623  * qla4xxx_probe_adapter - callback function to probe HBA
1624  * @pdev: pointer to pci_dev structure
1625  * @pci_device_id: pointer to pci_device entry
1626  *
1627  * This routine will probe for Qlogic 4xxx iSCSI host adapters.
1628  * It returns zero if successful. It also initializes all data necessary for
1629  * the driver.
1630  **/
1631 static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
1632                                            const struct pci_device_id *ent)
1633 {
1634         int ret = -ENODEV, status;
1635         struct Scsi_Host *host;
1636         struct scsi_qla_host *ha;
1637         uint8_t init_retry_count = 0;
1638         char buf[34];
1639         struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
1640
1641         if (pci_enable_device(pdev))
1642                 return -1;
1643
1644         host = scsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha));
1645         if (host == NULL) {
1646                 printk(KERN_WARNING
1647                        "qla4xxx: Couldn't allocate host from scsi layer!\n");
1648                 goto probe_disable_device;
1649         }
1650
1651         /* Clear our data area */
1652         ha = (struct scsi_qla_host *) host->hostdata;
1653         memset(ha, 0, sizeof(*ha));
1654
1655         /* Save the information from PCI BIOS.  */
1656         ha->pdev = pdev;
1657         ha->host = host;
1658         ha->host_no = host->host_no;
1659
1660         pci_enable_pcie_error_reporting(pdev);
1661
1662         /* Setup Runtime configurable options */
1663         if (is_qla8022(ha)) {
1664                 ha->isp_ops = &qla4_8xxx_isp_ops;
1665                 rwlock_init(&ha->hw_lock);
1666                 ha->qdr_sn_window = -1;
1667                 ha->ddr_mn_window = -1;
1668                 ha->curr_window = 255;
1669                 ha->func_num = PCI_FUNC(ha->pdev->devfn);
1670                 nx_legacy_intr = &legacy_intr[ha->func_num];
1671                 ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
1672                 ha->nx_legacy_intr.tgt_status_reg =
1673                         nx_legacy_intr->tgt_status_reg;
1674                 ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
1675                 ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
1676         } else {
1677                 ha->isp_ops = &qla4xxx_isp_ops;
1678         }
1679
1680         /* Set EEH reset type to fundamental if required by hba */
1681         if (is_qla8022(ha))
1682                 pdev->needs_freset = 1;
1683
1684         /* Configure PCI I/O space. */
1685         ret = ha->isp_ops->iospace_config(ha);
1686         if (ret)
1687                 goto probe_failed_ioconfig;
1688
1689         ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
1690                    pdev->device, pdev->irq, ha->reg);
1691
1692         qla4xxx_config_dma_addressing(ha);
1693
1694         /* Initialize lists and spinlocks. */
1695         INIT_LIST_HEAD(&ha->ddb_list);
1696         INIT_LIST_HEAD(&ha->free_srb_q);
1697
1698         mutex_init(&ha->mbox_sem);
1699         init_completion(&ha->mbx_intr_comp);
1700
1701         spin_lock_init(&ha->hardware_lock);
1702
1703         /* Allocate dma buffers */
1704         if (qla4xxx_mem_alloc(ha)) {
1705                 ql4_printk(KERN_WARNING, ha,
1706                     "[ERROR] Failed to allocate memory for adapter\n");
1707
1708                 ret = -ENOMEM;
1709                 goto probe_failed;
1710         }
1711
1712         if (is_qla8022(ha))
1713                 (void) qla4_8xxx_get_flash_info(ha);
1714
1715         /*
1716          * Initialize the Host adapter request/response queues and
1717          * firmware
1718          * NOTE: interrupts enabled upon successful completion
1719          */
1720         status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1721         while ((!test_bit(AF_ONLINE, &ha->flags)) &&
1722             init_retry_count++ < MAX_INIT_RETRIES) {
1723                 DEBUG2(printk("scsi: %s: retrying adapter initialization "
1724                               "(%d)\n", __func__, init_retry_count));
1725
1726                 if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
1727                         continue;
1728
1729                 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1730         }
1731
1732         if (!test_bit(AF_ONLINE, &ha->flags)) {
1733                 ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
1734
1735                 ret = -ENODEV;
1736                 goto probe_failed;
1737         }
1738
1739         host->cmd_per_lun = 3;
1740         host->max_channel = 0;
1741         host->max_lun = MAX_LUNS - 1;
1742         host->max_id = MAX_TARGETS;
1743         host->max_cmd_len = IOCB_MAX_CDB_LEN;
1744         host->can_queue = MAX_SRBS ;
1745         host->transportt = qla4xxx_scsi_transport;
1746
1747         ret = scsi_init_shared_tag_map(host, MAX_SRBS);
1748         if (ret) {
1749                 ql4_printk(KERN_WARNING, ha,
1750                     "scsi_init_shared_tag_map failed\n");
1751                 goto probe_failed;
1752         }
1753
1754         /* Startup the kernel thread for this host adapter. */
1755         DEBUG2(printk("scsi: %s: Starting kernel thread for "
1756                       "qla4xxx_dpc\n", __func__));
1757         sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
1758         ha->dpc_thread = create_singlethread_workqueue(buf);
1759         if (!ha->dpc_thread) {
1760                 ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
1761                 ret = -ENODEV;
1762                 goto probe_failed;
1763         }
1764         INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
1765
1766         /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
1767          * (which is called indirectly by qla4xxx_initialize_adapter),
1768          * so that irqs will be registered after crbinit but before
1769          * mbx_intr_enable.
1770          */
1771         if (!is_qla8022(ha)) {
1772                 ret = qla4xxx_request_irqs(ha);
1773                 if (ret) {
1774                         ql4_printk(KERN_WARNING, ha, "Failed to reserve "
1775                             "interrupt %d already in use.\n", pdev->irq);
1776                         goto probe_failed;
1777                 }
1778         }
1779
1780         pci_save_state(ha->pdev);
1781         ha->isp_ops->enable_intrs(ha);
1782
1783         /* Start timer thread. */
1784         qla4xxx_start_timer(ha, qla4xxx_timer, 1);
1785
1786         set_bit(AF_INIT_DONE, &ha->flags);
1787
1788         pci_set_drvdata(pdev, ha);
1789
1790         ret = scsi_add_host(host, &pdev->dev);
1791         if (ret)
1792                 goto probe_failed;
1793
1794         printk(KERN_INFO
1795                " QLogic iSCSI HBA Driver version: %s\n"
1796                "  QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
1797                qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
1798                ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
1799                ha->patch_number, ha->build_number);
1800         scsi_scan_host(host);
1801         return 0;
1802
1803 probe_failed:
1804         qla4xxx_free_adapter(ha);
1805
1806 probe_failed_ioconfig:
1807         pci_disable_pcie_error_reporting(pdev);
1808         scsi_host_put(ha->host);
1809
1810 probe_disable_device:
1811         pci_disable_device(pdev);
1812
1813         return ret;
1814 }
1815
1816 /**
1817  * qla4xxx_remove_adapter - calback function to remove adapter.
1818  * @pci_dev: PCI device pointer
1819  **/
1820 static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
1821 {
1822         struct scsi_qla_host *ha;
1823
1824         ha = pci_get_drvdata(pdev);
1825
1826         set_bit(AF_HBA_GOING_AWAY, &ha->flags);
1827
1828         /* remove devs from iscsi_sessions to scsi_devices */
1829         qla4xxx_free_ddb_list(ha);
1830
1831         scsi_remove_host(ha->host);
1832
1833         qla4xxx_free_adapter(ha);
1834
1835         scsi_host_put(ha->host);
1836
1837         pci_disable_pcie_error_reporting(pdev);
1838         pci_disable_device(pdev);
1839         pci_set_drvdata(pdev, NULL);
1840 }
1841
1842 /**
1843  * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
1844  * @ha: HA context
1845  *
1846  * At exit, the @ha's flags.enable_64bit_addressing set to indicated
1847  * supported addressing method.
1848  */
1849 static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
1850 {
1851         int retval;
1852
1853         /* Update our PCI device dma_mask for full 64 bit mask */
1854         if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
1855                 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
1856                         dev_dbg(&ha->pdev->dev,
1857                                   "Failed to set 64 bit PCI consistent mask; "
1858                                    "using 32 bit.\n");
1859                         retval = pci_set_consistent_dma_mask(ha->pdev,
1860                                                              DMA_BIT_MASK(32));
1861                 }
1862         } else
1863                 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
1864 }
1865
1866 static int qla4xxx_slave_alloc(struct scsi_device *sdev)
1867 {
1868         struct iscsi_cls_session *sess = starget_to_session(sdev->sdev_target);
1869         struct ddb_entry *ddb = sess->dd_data;
1870
1871         sdev->hostdata = ddb;
1872         sdev->tagged_supported = 1;
1873         scsi_activate_tcq(sdev, QL4_DEF_QDEPTH);
1874         return 0;
1875 }
1876
1877 static int qla4xxx_slave_configure(struct scsi_device *sdev)
1878 {
1879         sdev->tagged_supported = 1;
1880         return 0;
1881 }
1882
1883 static void qla4xxx_slave_destroy(struct scsi_device *sdev)
1884 {
1885         scsi_deactivate_tcq(sdev, 1);
1886 }
1887
1888 /**
1889  * qla4xxx_del_from_active_array - returns an active srb
1890  * @ha: Pointer to host adapter structure.
1891  * @index: index into the active_array
1892  *
1893  * This routine removes and returns the srb at the specified index
1894  **/
1895 struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
1896     uint32_t index)
1897 {
1898         struct srb *srb = NULL;
1899         struct scsi_cmnd *cmd = NULL;
1900
1901         cmd = scsi_host_find_tag(ha->host, index);
1902         if (!cmd)
1903                 return srb;
1904
1905         srb = (struct srb *)CMD_SP(cmd);
1906         if (!srb)
1907                 return srb;
1908
1909         /* update counters */
1910         if (srb->flags & SRB_DMA_VALID) {
1911                 ha->req_q_count += srb->iocb_cnt;
1912                 ha->iocb_cnt -= srb->iocb_cnt;
1913                 if (srb->cmd)
1914                         srb->cmd->host_scribble =
1915                                 (unsigned char *)(unsigned long) MAX_SRBS;
1916         }
1917         return srb;
1918 }
1919
1920 /**
1921  * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
1922  * @ha: Pointer to host adapter structure.
1923  * @cmd: Scsi Command to wait on.
1924  *
1925  * This routine waits for the command to be returned by the Firmware
1926  * for some max time.
1927  **/
1928 static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
1929                                       struct scsi_cmnd *cmd)
1930 {
1931         int done = 0;
1932         struct srb *rp;
1933         uint32_t max_wait_time = EH_WAIT_CMD_TOV;
1934         int ret = SUCCESS;
1935
1936         /* Dont wait on command if PCI error is being handled
1937          * by PCI AER driver
1938          */
1939         if (unlikely(pci_channel_offline(ha->pdev)) ||
1940             (test_bit(AF_EEH_BUSY, &ha->flags))) {
1941                 ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
1942                     ha->host_no, __func__);
1943                 return ret;
1944         }
1945
1946         do {
1947                 /* Checking to see if its returned to OS */
1948                 rp = (struct srb *) CMD_SP(cmd);
1949                 if (rp == NULL) {
1950                         done++;
1951                         break;
1952                 }
1953
1954                 msleep(2000);
1955         } while (max_wait_time--);
1956
1957         return done;
1958 }
1959
1960 /**
1961  * qla4xxx_wait_for_hba_online - waits for HBA to come online
1962  * @ha: Pointer to host adapter structure
1963  **/
1964 static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
1965 {
1966         unsigned long wait_online;
1967
1968         wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
1969         while (time_before(jiffies, wait_online)) {
1970
1971                 if (adapter_up(ha))
1972                         return QLA_SUCCESS;
1973
1974                 msleep(2000);
1975         }
1976
1977         return QLA_ERROR;
1978 }
1979
1980 /**
1981  * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
1982  * @ha: pointer to HBA
1983  * @t: target id
1984  * @l: lun id
1985  *
1986  * This function waits for all outstanding commands to a lun to complete. It
1987  * returns 0 if all pending commands are returned and 1 otherwise.
1988  **/
1989 static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
1990                                         struct scsi_target *stgt,
1991                                         struct scsi_device *sdev)
1992 {
1993         int cnt;
1994         int status = 0;
1995         struct scsi_cmnd *cmd;
1996
1997         /*
1998          * Waiting for all commands for the designated target or dev
1999          * in the active array
2000          */
2001         for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
2002                 cmd = scsi_host_find_tag(ha->host, cnt);
2003                 if (cmd && stgt == scsi_target(cmd->device) &&
2004                     (!sdev || sdev == cmd->device)) {
2005                         if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
2006                                 status++;
2007                                 break;
2008                         }
2009                 }
2010         }
2011         return status;
2012 }
2013
2014 /**
2015  * qla4xxx_eh_abort - callback for abort task.
2016  * @cmd: Pointer to Linux's SCSI command structure
2017  *
2018  * This routine is called by the Linux OS to abort the specified
2019  * command.
2020  **/
2021 static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
2022 {
2023         struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2024         unsigned int id = cmd->device->id;
2025         unsigned int lun = cmd->device->lun;
2026         unsigned long serial = cmd->serial_number;
2027         struct srb *srb = NULL;
2028         int ret = SUCCESS;
2029         int wait = 0;
2030
2031         ql4_printk(KERN_INFO, ha,
2032             "scsi%ld:%d:%d: Abort command issued cmd=%p, pid=%ld\n",
2033             ha->host_no, id, lun, cmd, serial);
2034
2035         srb = (struct srb *) CMD_SP(cmd);
2036
2037         if (!srb)
2038                 return SUCCESS;
2039
2040         kref_get(&srb->srb_ref);
2041
2042         if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
2043                 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
2044                     ha->host_no, id, lun));
2045                 ret = FAILED;
2046         } else {
2047                 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
2048                     ha->host_no, id, lun));
2049                 wait = 1;
2050         }
2051
2052         kref_put(&srb->srb_ref, qla4xxx_srb_compl);
2053
2054         /* Wait for command to complete */
2055         if (wait) {
2056                 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
2057                         DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
2058                             ha->host_no, id, lun));
2059                         ret = FAILED;
2060                 }
2061         }
2062
2063         ql4_printk(KERN_INFO, ha,
2064             "scsi%ld:%d:%d: Abort command - %s\n",
2065             ha->host_no, id, lun, (ret == SUCCESS) ? "succeded" : "failed");
2066
2067         return ret;
2068 }
2069
2070 /**
2071  * qla4xxx_eh_device_reset - callback for target reset.
2072  * @cmd: Pointer to Linux's SCSI command structure
2073  *
2074  * This routine is called by the Linux OS to reset all luns on the
2075  * specified target.
2076  **/
2077 static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
2078 {
2079         struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2080         struct ddb_entry *ddb_entry = cmd->device->hostdata;
2081         int ret = FAILED, stat;
2082
2083         if (!ddb_entry)
2084                 return ret;
2085
2086         ret = iscsi_block_scsi_eh(cmd);
2087         if (ret)
2088                 return ret;
2089         ret = FAILED;
2090
2091         ql4_printk(KERN_INFO, ha,
2092                    "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
2093                    cmd->device->channel, cmd->device->id, cmd->device->lun);
2094
2095         DEBUG2(printk(KERN_INFO
2096                       "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
2097                       "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
2098                       cmd, jiffies, cmd->request->timeout / HZ,
2099                       ha->dpc_flags, cmd->result, cmd->allowed));
2100
2101         /* FIXME: wait for hba to go online */
2102         stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
2103         if (stat != QLA_SUCCESS) {
2104                 ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
2105                 goto eh_dev_reset_done;
2106         }
2107
2108         if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2109                                          cmd->device)) {
2110                 ql4_printk(KERN_INFO, ha,
2111                            "DEVICE RESET FAILED - waiting for "
2112                            "commands.\n");
2113                 goto eh_dev_reset_done;
2114         }
2115
2116         /* Send marker. */
2117         if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2118                 MM_LUN_RESET) != QLA_SUCCESS)
2119                 goto eh_dev_reset_done;
2120
2121         ql4_printk(KERN_INFO, ha,
2122                    "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
2123                    ha->host_no, cmd->device->channel, cmd->device->id,
2124                    cmd->device->lun);
2125
2126         ret = SUCCESS;
2127
2128 eh_dev_reset_done:
2129
2130         return ret;
2131 }
2132
2133 /**
2134  * qla4xxx_eh_target_reset - callback for target reset.
2135  * @cmd: Pointer to Linux's SCSI command structure
2136  *
2137  * This routine is called by the Linux OS to reset the target.
2138  **/
2139 static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
2140 {
2141         struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2142         struct ddb_entry *ddb_entry = cmd->device->hostdata;
2143         int stat, ret;
2144
2145         if (!ddb_entry)
2146                 return FAILED;
2147
2148         ret = iscsi_block_scsi_eh(cmd);
2149         if (ret)
2150                 return ret;
2151
2152         starget_printk(KERN_INFO, scsi_target(cmd->device),
2153                        "WARM TARGET RESET ISSUED.\n");
2154
2155         DEBUG2(printk(KERN_INFO
2156                       "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
2157                       "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
2158                       ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
2159                       ha->dpc_flags, cmd->result, cmd->allowed));
2160
2161         stat = qla4xxx_reset_target(ha, ddb_entry);
2162         if (stat != QLA_SUCCESS) {
2163                 starget_printk(KERN_INFO, scsi_target(cmd->device),
2164                                "WARM TARGET RESET FAILED.\n");
2165                 return FAILED;
2166         }
2167
2168         if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2169                                          NULL)) {
2170                 starget_printk(KERN_INFO, scsi_target(cmd->device),
2171                                "WARM TARGET DEVICE RESET FAILED - "
2172                                "waiting for commands.\n");
2173                 return FAILED;
2174         }
2175
2176         /* Send marker. */
2177         if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2178                 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
2179                 starget_printk(KERN_INFO, scsi_target(cmd->device),
2180                                "WARM TARGET DEVICE RESET FAILED - "
2181                                "marker iocb failed.\n");
2182                 return FAILED;
2183         }
2184
2185         starget_printk(KERN_INFO, scsi_target(cmd->device),
2186                        "WARM TARGET RESET SUCCEEDED.\n");
2187         return SUCCESS;
2188 }
2189
2190 /**
2191  * qla4xxx_eh_host_reset - kernel callback
2192  * @cmd: Pointer to Linux's SCSI command structure
2193  *
2194  * This routine is invoked by the Linux kernel to perform fatal error
2195  * recovery on the specified adapter.
2196  **/
2197 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
2198 {
2199         int return_status = FAILED;
2200         struct scsi_qla_host *ha;
2201
2202         ha = (struct scsi_qla_host *) cmd->device->host->hostdata;
2203
2204         if (ql4xdontresethba) {
2205                 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
2206                      ha->host_no, __func__));
2207                 return FAILED;
2208         }
2209
2210         ql4_printk(KERN_INFO, ha,
2211                    "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
2212                    cmd->device->channel, cmd->device->id, cmd->device->lun);
2213
2214         if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
2215                 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host.  Adapter "
2216                               "DEAD.\n", ha->host_no, cmd->device->channel,
2217                               __func__));
2218
2219                 return FAILED;
2220         }
2221
2222         if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
2223                 if (is_qla8022(ha))
2224                         set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2225                 else
2226                         set_bit(DPC_RESET_HA, &ha->dpc_flags);
2227         }
2228
2229         if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
2230                 return_status = SUCCESS;
2231
2232         ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
2233                    return_status == FAILED ? "FAILED" : "SUCCEDED");
2234
2235         return return_status;
2236 }
2237
2238 /* PCI AER driver recovers from all correctable errors w/o
2239  * driver intervention. For uncorrectable errors PCI AER
2240  * driver calls the following device driver's callbacks
2241  *
2242  * - Fatal Errors - link_reset
2243  * - Non-Fatal Errors - driver's pci_error_detected() which
2244  * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
2245  *
2246  * PCI AER driver calls
2247  * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
2248  *               returns RECOVERED or NEED_RESET if fw_hung
2249  * NEED_RESET - driver's slot_reset()
2250  * DISCONNECT - device is dead & cannot recover
2251  * RECOVERED - driver's pci_resume()
2252  */
2253 static pci_ers_result_t
2254 qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
2255 {
2256         struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2257
2258         ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
2259             ha->host_no, __func__, state);
2260
2261         if (!is_aer_supported(ha))
2262                 return PCI_ERS_RESULT_NONE;
2263
2264         switch (state) {
2265         case pci_channel_io_normal:
2266                 clear_bit(AF_EEH_BUSY, &ha->flags);
2267                 return PCI_ERS_RESULT_CAN_RECOVER;
2268         case pci_channel_io_frozen:
2269                 set_bit(AF_EEH_BUSY, &ha->flags);
2270                 qla4xxx_mailbox_premature_completion(ha);
2271                 qla4xxx_free_irqs(ha);
2272                 pci_disable_device(pdev);
2273                 return PCI_ERS_RESULT_NEED_RESET;
2274         case pci_channel_io_perm_failure:
2275                 set_bit(AF_EEH_BUSY, &ha->flags);
2276                 set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
2277                 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
2278                 return PCI_ERS_RESULT_DISCONNECT;
2279         }
2280         return PCI_ERS_RESULT_NEED_RESET;
2281 }
2282
2283 /**
2284  * qla4xxx_pci_mmio_enabled() gets called if
2285  * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
2286  * and read/write to the device still works.
2287  **/
2288 static pci_ers_result_t
2289 qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
2290 {
2291         struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2292
2293         if (!is_aer_supported(ha))
2294                 return PCI_ERS_RESULT_NONE;
2295
2296         if (test_bit(AF_FW_RECOVERY, &ha->flags)) {
2297                 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: firmware hang  -- "
2298                     "mmio_enabled\n", ha->host_no, __func__);
2299                 return PCI_ERS_RESULT_NEED_RESET;
2300         } else
2301                 return PCI_ERS_RESULT_RECOVERED;
2302 }
2303
2304 uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
2305 {
2306         uint32_t rval = QLA_ERROR;
2307         int fn;
2308         struct pci_dev *other_pdev = NULL;
2309
2310         ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
2311
2312         set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2313
2314         if (test_bit(AF_ONLINE, &ha->flags)) {
2315                 clear_bit(AF_ONLINE, &ha->flags);
2316                 qla4xxx_mark_all_devices_missing(ha);
2317                 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2318                 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
2319         }
2320
2321         fn = PCI_FUNC(ha->pdev->devfn);
2322         while (fn > 0) {
2323                 fn--;
2324                 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
2325                     "func %x\n", ha->host_no, __func__, fn);
2326                 /* Get the pci device given the domain, bus,
2327                  * slot/function number */
2328                 other_pdev =
2329                     pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
2330                     ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
2331                     fn));
2332
2333                 if (!other_pdev)
2334                         continue;
2335
2336                 if (atomic_read(&other_pdev->enable_cnt)) {
2337                         ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
2338                             "func in enabled state%x\n", ha->host_no,
2339                             __func__, fn);
2340                         pci_dev_put(other_pdev);
2341                         break;
2342                 }
2343                 pci_dev_put(other_pdev);
2344         }
2345
2346         /* The first function on the card, the reset owner will
2347          * start & initialize the firmware. The other functions
2348          * on the card will reset the firmware context
2349          */
2350         if (!fn) {
2351                 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
2352                     "0x%x is the owner\n", ha->host_no, __func__,
2353                     ha->pdev->devfn);
2354
2355                 qla4_8xxx_idc_lock(ha);
2356                 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2357                     QLA82XX_DEV_COLD);
2358
2359                 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
2360                     QLA82XX_IDC_VERSION);
2361
2362                 qla4_8xxx_idc_unlock(ha);
2363                 clear_bit(AF_FW_RECOVERY, &ha->flags);
2364                 rval = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
2365                 qla4_8xxx_idc_lock(ha);
2366
2367                 if (rval != QLA_SUCCESS) {
2368                         ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
2369                             "FAILED\n", ha->host_no, __func__);
2370                         qla4_8xxx_clear_drv_active(ha);
2371                         qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2372                             QLA82XX_DEV_FAILED);
2373                 } else {
2374                         ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
2375                             "READY\n", ha->host_no, __func__);
2376                         qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2377                             QLA82XX_DEV_READY);
2378                         /* Clear driver state register */
2379                         qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
2380                         qla4_8xxx_set_drv_active(ha);
2381                         ha->isp_ops->enable_intrs(ha);
2382                 }
2383                 qla4_8xxx_idc_unlock(ha);
2384         } else {
2385                 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
2386                     "the reset owner\n", ha->host_no, __func__,
2387                     ha->pdev->devfn);
2388                 if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
2389                     QLA82XX_DEV_READY)) {
2390                         clear_bit(AF_FW_RECOVERY, &ha->flags);
2391                         rval = qla4xxx_initialize_adapter(ha,
2392                             PRESERVE_DDB_LIST);
2393                         if (rval == QLA_SUCCESS)
2394                                 ha->isp_ops->enable_intrs(ha);
2395                         qla4_8xxx_idc_lock(ha);
2396                         qla4_8xxx_set_drv_active(ha);
2397                         qla4_8xxx_idc_unlock(ha);
2398                 }
2399         }
2400         clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2401         return rval;
2402 }
2403
2404 static pci_ers_result_t
2405 qla4xxx_pci_slot_reset(struct pci_dev *pdev)
2406 {
2407         pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
2408         struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2409         int rc;
2410
2411         ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
2412             ha->host_no, __func__);
2413
2414         if (!is_aer_supported(ha))
2415                 return PCI_ERS_RESULT_NONE;
2416
2417         /* Restore the saved state of PCIe device -
2418          * BAR registers, PCI Config space, PCIX, MSI,
2419          * IOV states
2420          */
2421         pci_restore_state(pdev);
2422
2423         /* pci_restore_state() clears the saved_state flag of the device
2424          * save restored state which resets saved_state flag
2425          */
2426         pci_save_state(pdev);
2427
2428         /* Initialize device or resume if in suspended state */
2429         rc = pci_enable_device(pdev);
2430         if (rc) {
2431                 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Cant re-enable "
2432                     "device after reset\n", ha->host_no, __func__);
2433                 goto exit_slot_reset;
2434         }
2435
2436         ret = qla4xxx_request_irqs(ha);
2437         if (ret) {
2438                 ql4_printk(KERN_WARNING, ha, "Failed to reserve interrupt %d"
2439                     " already in use.\n", pdev->irq);
2440                 goto exit_slot_reset;
2441         }
2442
2443         if (is_qla8022(ha)) {
2444                 if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
2445                         ret = PCI_ERS_RESULT_RECOVERED;
2446                         goto exit_slot_reset;
2447                 } else
2448                         goto exit_slot_reset;
2449         }
2450
2451 exit_slot_reset:
2452         ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
2453             "device after reset\n", ha->host_no, __func__, ret);
2454         return ret;
2455 }
2456
2457 static void
2458 qla4xxx_pci_resume(struct pci_dev *pdev)
2459 {
2460         struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2461         int ret;
2462
2463         ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
2464             ha->host_no, __func__);
2465
2466         ret = qla4xxx_wait_for_hba_online(ha);
2467         if (ret != QLA_SUCCESS) {
2468                 ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
2469                     "resume I/O from slot/link_reset\n", ha->host_no,
2470                      __func__);
2471         }
2472
2473         pci_cleanup_aer_uncorrect_error_status(pdev);
2474         clear_bit(AF_EEH_BUSY, &ha->flags);
2475 }
2476
2477 static struct pci_error_handlers qla4xxx_err_handler = {
2478         .error_detected = qla4xxx_pci_error_detected,
2479         .mmio_enabled = qla4xxx_pci_mmio_enabled,
2480         .slot_reset = qla4xxx_pci_slot_reset,
2481         .resume = qla4xxx_pci_resume,
2482 };
2483
2484 static struct pci_device_id qla4xxx_pci_tbl[] = {
2485         {
2486                 .vendor         = PCI_VENDOR_ID_QLOGIC,
2487                 .device         = PCI_DEVICE_ID_QLOGIC_ISP4010,
2488                 .subvendor      = PCI_ANY_ID,
2489                 .subdevice      = PCI_ANY_ID,
2490         },
2491         {
2492                 .vendor         = PCI_VENDOR_ID_QLOGIC,
2493                 .device         = PCI_DEVICE_ID_QLOGIC_ISP4022,
2494                 .subvendor      = PCI_ANY_ID,
2495                 .subdevice      = PCI_ANY_ID,
2496         },
2497         {
2498                 .vendor         = PCI_VENDOR_ID_QLOGIC,
2499                 .device         = PCI_DEVICE_ID_QLOGIC_ISP4032,
2500                 .subvendor      = PCI_ANY_ID,
2501                 .subdevice      = PCI_ANY_ID,
2502         },
2503         {
2504                 .vendor         = PCI_VENDOR_ID_QLOGIC,
2505                 .device         = PCI_DEVICE_ID_QLOGIC_ISP8022,
2506                 .subvendor      = PCI_ANY_ID,
2507                 .subdevice      = PCI_ANY_ID,
2508         },
2509         {0, 0},
2510 };
2511 MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
2512
2513 static struct pci_driver qla4xxx_pci_driver = {
2514         .name           = DRIVER_NAME,
2515         .id_table       = qla4xxx_pci_tbl,
2516         .probe          = qla4xxx_probe_adapter,
2517         .remove         = qla4xxx_remove_adapter,
2518         .err_handler = &qla4xxx_err_handler,
2519 };
2520
2521 static int __init qla4xxx_module_init(void)
2522 {
2523         int ret;
2524
2525         /* Allocate cache for SRBs. */
2526         srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
2527                                        SLAB_HWCACHE_ALIGN, NULL);
2528         if (srb_cachep == NULL) {
2529                 printk(KERN_ERR
2530                        "%s: Unable to allocate SRB cache..."
2531                        "Failing load!\n", DRIVER_NAME);
2532                 ret = -ENOMEM;
2533                 goto no_srp_cache;
2534         }
2535
2536         /* Derive version string. */
2537         strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
2538         if (ql4xextended_error_logging)
2539                 strcat(qla4xxx_version_str, "-debug");
2540
2541         qla4xxx_scsi_transport =
2542                 iscsi_register_transport(&qla4xxx_iscsi_transport);
2543         if (!qla4xxx_scsi_transport){
2544                 ret = -ENODEV;
2545                 goto release_srb_cache;
2546         }
2547
2548         ret = pci_register_driver(&qla4xxx_pci_driver);
2549         if (ret)
2550                 goto unregister_transport;
2551
2552         printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
2553         return 0;
2554
2555 unregister_transport:
2556         iscsi_unregister_transport(&qla4xxx_iscsi_transport);
2557 release_srb_cache:
2558         kmem_cache_destroy(srb_cachep);
2559 no_srp_cache:
2560         return ret;
2561 }
2562
2563 static void __exit qla4xxx_module_exit(void)
2564 {
2565         pci_unregister_driver(&qla4xxx_pci_driver);
2566         iscsi_unregister_transport(&qla4xxx_iscsi_transport);
2567         kmem_cache_destroy(srb_cachep);
2568 }
2569
2570 module_init(qla4xxx_module_init);
2571 module_exit(qla4xxx_module_exit);
2572
2573 MODULE_AUTHOR("QLogic Corporation");
2574 MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
2575 MODULE_LICENSE("GPL");
2576 MODULE_VERSION(QLA4XXX_DRIVER_VERSION);