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