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