[SCSI] qla4xxx: Stop firmware before doing init firmware.
[linux-2.6-block.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);
666 /* Since we cannot change dev_state in interrupt
667 * context, set appropriate DPC flag then wakeup
668 * DPC */
669 if (halt_status & HALT_STATUS_UNRECOVERABLE)
670 set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
671 else {
672 printk("scsi%ld: %s: detect abort needed!\n",
673 ha->host_no, __func__);
674 set_bit(DPC_RESET_HA, &ha->dpc_flags);
675 }
676 qla4xxx_wake_dpc(ha);
677 }
678 }
679 ha->fw_heartbeat_counter = fw_heartbeat_counter;
680}
681
682/**
683 * qla4_8xxx_watchdog - Poll dev state
684 * @ha: Pointer to host adapter structure.
685 *
686 * Context: Interrupt
687 **/
688void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
689{
690 uint32_t dev_state;
691
692 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
693
694 /* don't poll if reset is going on */
695 if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags)) {
696 if (dev_state == QLA82XX_DEV_NEED_RESET &&
697 !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
698 printk("scsi%ld: %s: HW State: NEED RESET!\n",
699 ha->host_no, __func__);
700 set_bit(DPC_RESET_HA, &ha->dpc_flags);
701 qla4xxx_wake_dpc(ha);
702 } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
703 !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
704 printk("scsi%ld: %s: HW State: NEED QUIES!\n",
705 ha->host_no, __func__);
706 set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
707 qla4xxx_wake_dpc(ha);
708 } else {
709 /* Check firmware health */
710 qla4_8xxx_check_fw_alive(ha);
711 }
712 }
713}
714
afaf5a2d
DS
715/**
716 * qla4xxx_timer - checks every second for work to do.
717 * @ha: Pointer to host adapter structure.
718 **/
719static void qla4xxx_timer(struct scsi_qla_host *ha)
720{
721 struct ddb_entry *ddb_entry, *dtemp;
722 int start_dpc = 0;
723
f4f5df23
VC
724 if (test_bit(AF_HBA_GOING_AWAY, &ha->flags)) {
725 DEBUG2(ql4_printk(KERN_INFO, ha, "%s exited. HBA GOING AWAY\n",
726 __func__));
727 return;
728 }
729
730 if (is_qla8022(ha)) {
731 qla4_8xxx_watchdog(ha);
732 }
733
afaf5a2d
DS
734 /* Search for relogin's to time-out and port down retry. */
735 list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
736 /* Count down time between sending relogins */
737 if (adapter_up(ha) &&
738 !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
739 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
740 if (atomic_read(&ddb_entry->retry_relogin_timer) !=
741 INVALID_ENTRY) {
742 if (atomic_read(&ddb_entry->retry_relogin_timer)
743 == 0) {
744 atomic_set(&ddb_entry->
745 retry_relogin_timer,
746 INVALID_ENTRY);
747 set_bit(DPC_RELOGIN_DEVICE,
748 &ha->dpc_flags);
749 set_bit(DF_RELOGIN, &ddb_entry->flags);
f4f5df23 750 DEBUG2(printk("scsi%ld: %s: ddb [%d]"
afaf5a2d
DS
751 " login device\n",
752 ha->host_no, __func__,
753 ddb_entry->fw_ddb_index));
754 } else
755 atomic_dec(&ddb_entry->
756 retry_relogin_timer);
757 }
758 }
759
760 /* Wait for relogin to timeout */
761 if (atomic_read(&ddb_entry->relogin_timer) &&
762 (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
763 /*
764 * If the relogin times out and the device is
765 * still NOT ONLINE then try and relogin again.
766 */
767 if (atomic_read(&ddb_entry->state) !=
768 DDB_STATE_ONLINE &&
769 ddb_entry->fw_ddb_device_state ==
770 DDB_DS_SESSION_FAILED) {
771 /* Reset retry relogin timer */
772 atomic_inc(&ddb_entry->relogin_retry_count);
f4f5df23 773 DEBUG2(printk("scsi%ld: ddb [%d] relogin"
afaf5a2d
DS
774 " timed out-retrying"
775 " relogin (%d)\n",
776 ha->host_no,
777 ddb_entry->fw_ddb_index,
778 atomic_read(&ddb_entry->
779 relogin_retry_count))
780 );
781 start_dpc++;
f4f5df23 782 DEBUG(printk("scsi%ld:%d:%d: ddb [%d] "
afaf5a2d
DS
783 "initate relogin after"
784 " %d seconds\n",
785 ha->host_no, ddb_entry->bus,
786 ddb_entry->target,
787 ddb_entry->fw_ddb_index,
788 ddb_entry->default_time2wait + 4)
789 );
790
791 atomic_set(&ddb_entry->retry_relogin_timer,
792 ddb_entry->default_time2wait + 4);
793 }
794 }
795 }
796
f4f5df23
VC
797 if (!is_qla8022(ha)) {
798 /* Check for heartbeat interval. */
799 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
800 ha->heartbeat_interval != 0) {
801 ha->seconds_since_last_heartbeat++;
802 if (ha->seconds_since_last_heartbeat >
803 ha->heartbeat_interval + 2)
804 set_bit(DPC_RESET_HA, &ha->dpc_flags);
805 }
afaf5a2d
DS
806 }
807
afaf5a2d
DS
808 /* Wakeup the dpc routine for this adapter, if needed. */
809 if ((start_dpc ||
810 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
811 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
812 test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
f4f5df23 813 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
afaf5a2d
DS
814 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
815 test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
065aa1b4 816 test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
f4f5df23
VC
817 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
818 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
afaf5a2d 819 test_bit(DPC_AEN, &ha->dpc_flags)) &&
f4f5df23 820 !test_bit(AF_DPC_SCHEDULED, &ha->flags) &&
afaf5a2d
DS
821 ha->dpc_thread) {
822 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
823 " - dpc flags = 0x%lx\n",
824 ha->host_no, __func__, ha->dpc_flags));
f4f5df23 825 qla4xxx_wake_dpc(ha);
afaf5a2d
DS
826 }
827
828 /* Reschedule timer thread to call us back in one second */
829 mod_timer(&ha->timer, jiffies + HZ);
830
831 DEBUG2(ha->seconds_since_last_intr++);
832}
833
834/**
835 * qla4xxx_cmd_wait - waits for all outstanding commands to complete
836 * @ha: Pointer to host adapter structure.
837 *
838 * This routine stalls the driver until all outstanding commands are returned.
839 * Caller must release the Hardware Lock prior to calling this routine.
840 **/
841static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
842{
843 uint32_t index = 0;
afaf5a2d
DS
844 unsigned long flags;
845 struct scsi_cmnd *cmd;
afaf5a2d 846
f4f5df23
VC
847 unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
848
849 DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
850 "complete\n", WAIT_CMD_TOV));
851
852 while (!time_after_eq(jiffies, wtime)) {
afaf5a2d
DS
853 spin_lock_irqsave(&ha->hardware_lock, flags);
854 /* Find a command that hasn't completed. */
855 for (index = 0; index < ha->host->can_queue; index++) {
856 cmd = scsi_host_find_tag(ha->host, index);
857 if (cmd != NULL)
858 break;
859 }
860 spin_unlock_irqrestore(&ha->hardware_lock, flags);
861
862 /* If No Commands are pending, wait is complete */
f4f5df23
VC
863 if (index == ha->host->can_queue)
864 return QLA_SUCCESS;
afaf5a2d 865
f4f5df23
VC
866 msleep(1000);
867 }
868 /* If we timed out on waiting for commands to come back
869 * return ERROR. */
870 return QLA_ERROR;
afaf5a2d
DS
871}
872
f4f5df23 873int qla4xxx_hw_reset(struct scsi_qla_host *ha)
afaf5a2d 874{
afaf5a2d 875 uint32_t ctrl_status;
477ffb9d
DS
876 unsigned long flags = 0;
877
878 DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
afaf5a2d 879
f4f5df23
VC
880 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
881 return QLA_ERROR;
882
afaf5a2d
DS
883 spin_lock_irqsave(&ha->hardware_lock, flags);
884
885 /*
886 * If the SCSI Reset Interrupt bit is set, clear it.
887 * Otherwise, the Soft Reset won't work.
888 */
889 ctrl_status = readw(&ha->reg->ctrl_status);
890 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
891 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
892
893 /* Issue Soft Reset */
894 writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
895 readl(&ha->reg->ctrl_status);
896
897 spin_unlock_irqrestore(&ha->hardware_lock, flags);
f4f5df23 898 return QLA_SUCCESS;
477ffb9d
DS
899}
900
901/**
902 * qla4xxx_soft_reset - performs soft reset.
903 * @ha: Pointer to host adapter structure.
904 **/
905int qla4xxx_soft_reset(struct scsi_qla_host *ha)
906{
907 uint32_t max_wait_time;
908 unsigned long flags = 0;
909 int status = QLA_ERROR;
910 uint32_t ctrl_status;
911
912 qla4xxx_hw_reset(ha);
afaf5a2d
DS
913
914 /* Wait until the Network Reset Intr bit is cleared */
915 max_wait_time = RESET_INTR_TOV;
916 do {
917 spin_lock_irqsave(&ha->hardware_lock, flags);
918 ctrl_status = readw(&ha->reg->ctrl_status);
919 spin_unlock_irqrestore(&ha->hardware_lock, flags);
920
921 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
922 break;
923
924 msleep(1000);
925 } while ((--max_wait_time));
926
927 if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
928 DEBUG2(printk(KERN_WARNING
929 "scsi%ld: Network Reset Intr not cleared by "
930 "Network function, clearing it now!\n",
931 ha->host_no));
932 spin_lock_irqsave(&ha->hardware_lock, flags);
933 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
934 readl(&ha->reg->ctrl_status);
935 spin_unlock_irqrestore(&ha->hardware_lock, flags);
936 }
937
938 /* Wait until the firmware tells us the Soft Reset is done */
939 max_wait_time = SOFT_RESET_TOV;
940 do {
941 spin_lock_irqsave(&ha->hardware_lock, flags);
942 ctrl_status = readw(&ha->reg->ctrl_status);
943 spin_unlock_irqrestore(&ha->hardware_lock, flags);
944
945 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
946 status = QLA_SUCCESS;
947 break;
948 }
949
950 msleep(1000);
951 } while ((--max_wait_time));
952
953 /*
954 * Also, make sure that the SCSI Reset Interrupt bit has been cleared
955 * after the soft reset has taken place.
956 */
957 spin_lock_irqsave(&ha->hardware_lock, flags);
958 ctrl_status = readw(&ha->reg->ctrl_status);
959 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
960 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
961 readl(&ha->reg->ctrl_status);
962 }
963 spin_unlock_irqrestore(&ha->hardware_lock, flags);
964
965 /* If soft reset fails then most probably the bios on other
966 * function is also enabled.
967 * Since the initialization is sequential the other fn
968 * wont be able to acknowledge the soft reset.
969 * Issue a force soft reset to workaround this scenario.
970 */
971 if (max_wait_time == 0) {
972 /* Issue Force Soft Reset */
973 spin_lock_irqsave(&ha->hardware_lock, flags);
974 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
975 readl(&ha->reg->ctrl_status);
976 spin_unlock_irqrestore(&ha->hardware_lock, flags);
977 /* Wait until the firmware tells us the Soft Reset is done */
978 max_wait_time = SOFT_RESET_TOV;
979 do {
980 spin_lock_irqsave(&ha->hardware_lock, flags);
981 ctrl_status = readw(&ha->reg->ctrl_status);
982 spin_unlock_irqrestore(&ha->hardware_lock, flags);
983
984 if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
985 status = QLA_SUCCESS;
986 break;
987 }
988
989 msleep(1000);
990 } while ((--max_wait_time));
991 }
992
993 return status;
994}
995
afaf5a2d 996/**
f4f5df23 997 * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
afaf5a2d 998 * @ha: Pointer to host adapter structure.
f4f5df23 999 * @res: returned scsi status
afaf5a2d
DS
1000 *
1001 * This routine is called just prior to a HARD RESET to return all
1002 * outstanding commands back to the Operating System.
1003 * Caller should make sure that the following locks are released
1004 * before this calling routine: Hardware lock, and io_request_lock.
1005 **/
f4f5df23 1006static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
afaf5a2d
DS
1007{
1008 struct srb *srb;
1009 int i;
1010 unsigned long flags;
1011
1012 spin_lock_irqsave(&ha->hardware_lock, flags);
1013 for (i = 0; i < ha->host->can_queue; i++) {
1014 srb = qla4xxx_del_from_active_array(ha, i);
1015 if (srb != NULL) {
f4f5df23 1016 srb->cmd->result = res;
09a0f719 1017 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
afaf5a2d
DS
1018 }
1019 }
1020 spin_unlock_irqrestore(&ha->hardware_lock, flags);
afaf5a2d
DS
1021}
1022
f4f5df23
VC
1023void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
1024{
1025 clear_bit(AF_ONLINE, &ha->flags);
1026
1027 /* Disable the board */
1028 ql4_printk(KERN_INFO, ha, "Disabling the board\n");
1029 set_bit(AF_HBA_GOING_AWAY, &ha->flags);
1030
1031 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
1032 qla4xxx_mark_all_devices_missing(ha);
1033 clear_bit(AF_INIT_DONE, &ha->flags);
1034}
1035
afaf5a2d
DS
1036/**
1037 * qla4xxx_recover_adapter - recovers adapter after a fatal error
1038 * @ha: Pointer to host adapter structure.
afaf5a2d 1039 **/
f4f5df23 1040static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
afaf5a2d 1041{
f4f5df23
VC
1042 int status = QLA_ERROR;
1043 uint8_t reset_chip = 0;
afaf5a2d
DS
1044
1045 /* Stall incoming I/O until we are done */
f4f5df23 1046 scsi_block_requests(ha->host);
afaf5a2d 1047 clear_bit(AF_ONLINE, &ha->flags);
50a29aec 1048
f4f5df23 1049 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
afaf5a2d 1050
f4f5df23 1051 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
afaf5a2d 1052
f4f5df23
VC
1053 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
1054 reset_chip = 1;
afaf5a2d 1055
f4f5df23
VC
1056 /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
1057 * do not reset adapter, jump to initialize_adapter */
1058 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1059 status = QLA_SUCCESS;
1060 goto recover_ha_init_adapter;
1061 }
afaf5a2d 1062
f4f5df23
VC
1063 /* For the ISP-82xx adapter, issue a stop_firmware if invoked
1064 * from eh_host_reset or ioctl module */
1065 if (is_qla8022(ha) && !reset_chip &&
1066 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
1067
1068 DEBUG2(ql4_printk(KERN_INFO, ha,
1069 "scsi%ld: %s - Performing stop_firmware...\n",
1070 ha->host_no, __func__));
1071 status = ha->isp_ops->reset_firmware(ha);
1072 if (status == QLA_SUCCESS) {
1073 qla4xxx_cmd_wait(ha);
1074 ha->isp_ops->disable_intrs(ha);
1075 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1076 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1077 } else {
1078 /* If the stop_firmware fails then
1079 * reset the entire chip */
1080 reset_chip = 1;
1081 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1082 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1083 }
1084 }
dca05c4c 1085
f4f5df23
VC
1086 /* Issue full chip reset if recovering from a catastrophic error,
1087 * or if stop_firmware fails for ISP-82xx.
1088 * This is the default case for ISP-4xxx */
1089 if (!is_qla8022(ha) || reset_chip) {
1090 qla4xxx_cmd_wait(ha);
1091 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1092 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1093 DEBUG2(ql4_printk(KERN_INFO, ha,
1094 "scsi%ld: %s - Performing chip reset..\n",
1095 ha->host_no, __func__));
1096 status = ha->isp_ops->reset_chip(ha);
1097 }
afaf5a2d
DS
1098
1099 /* Flush any pending ddb changed AENs */
1100 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1101
f4f5df23
VC
1102recover_ha_init_adapter:
1103 /* Upon successful firmware/chip reset, re-initialize the adapter */
afaf5a2d 1104 if (status == QLA_SUCCESS) {
f4f5df23
VC
1105 /* For ISP-4xxx, force function 1 to always initialize
1106 * before function 3 to prevent both funcions from
1107 * stepping on top of the other */
1108 if (!is_qla8022(ha) && (ha->mac_index == 3))
1109 ssleep(6);
1110
1111 /* NOTE: AF_ONLINE flag set upon successful completion of
1112 * qla4xxx_initialize_adapter */
1113 status = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
afaf5a2d
DS
1114 }
1115
f4f5df23
VC
1116 /* Retry failed adapter initialization, if necessary
1117 * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
1118 * case to prevent ping-pong resets between functions */
1119 if (!test_bit(AF_ONLINE, &ha->flags) &&
1120 !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
afaf5a2d 1121 /* Adapter initialization failed, see if we can retry
f4f5df23
VC
1122 * resetting the ha.
1123 * Since we don't want to block the DPC for too long
1124 * with multiple resets in the same thread,
1125 * utilize DPC to retry */
afaf5a2d
DS
1126 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
1127 ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
1128 DEBUG2(printk("scsi%ld: recover adapter - retrying "
1129 "(%d) more times\n", ha->host_no,
1130 ha->retry_reset_ha_cnt));
1131 set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1132 status = QLA_ERROR;
1133 } else {
1134 if (ha->retry_reset_ha_cnt > 0) {
1135 /* Schedule another Reset HA--DPC will retry */
1136 ha->retry_reset_ha_cnt--;
1137 DEBUG2(printk("scsi%ld: recover adapter - "
1138 "retry remaining %d\n",
1139 ha->host_no,
1140 ha->retry_reset_ha_cnt));
1141 status = QLA_ERROR;
1142 }
1143
1144 if (ha->retry_reset_ha_cnt == 0) {
1145 /* Recover adapter retries have been exhausted.
1146 * Adapter DEAD */
1147 DEBUG2(printk("scsi%ld: recover adapter "
1148 "failed - board disabled\n",
1149 ha->host_no));
f4f5df23 1150 qla4xxx_dead_adapter_cleanup(ha);
afaf5a2d
DS
1151 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1152 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
f4f5df23 1153 clear_bit(DPC_RESET_HA_FW_CONTEXT,
afaf5a2d
DS
1154 &ha->dpc_flags);
1155 status = QLA_ERROR;
1156 }
1157 }
1158 } else {
1159 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
f4f5df23 1160 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
afaf5a2d
DS
1161 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1162 }
1163
1164 ha->adapter_error_count++;
1165
f4f5df23
VC
1166 if (test_bit(AF_ONLINE, &ha->flags))
1167 ha->isp_ops->enable_intrs(ha);
1168
1169 scsi_unblock_requests(ha->host);
1170
1171 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
1172 DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
1173 status == QLA_ERROR ? "FAILED" : "SUCCEDED"));
afaf5a2d 1174
afaf5a2d
DS
1175 return status;
1176}
1177
f4f5df23
VC
1178void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
1179{
1180 if (ha->dpc_thread &&
1181 !test_bit(AF_DPC_SCHEDULED, &ha->flags)) {
1182 set_bit(AF_DPC_SCHEDULED, &ha->flags);
1183 queue_work(ha->dpc_thread, &ha->dpc_work);
1184 }
1185}
1186
afaf5a2d
DS
1187/**
1188 * qla4xxx_do_dpc - dpc routine
1189 * @data: in our case pointer to adapter structure
1190 *
1191 * This routine is a task that is schedule by the interrupt handler
1192 * to perform the background processing for interrupts. We put it
1193 * on a task queue that is consumed whenever the scheduler runs; that's
1194 * so you can do anything (i.e. put the process to sleep etc). In fact,
1195 * the mid-level tries to sleep when it reaches the driver threshold
1196 * "host->can_queue". This can cause a panic if we were in our interrupt code.
1197 **/
c4028958 1198static void qla4xxx_do_dpc(struct work_struct *work)
afaf5a2d 1199{
c4028958
DH
1200 struct scsi_qla_host *ha =
1201 container_of(work, struct scsi_qla_host, dpc_work);
afaf5a2d 1202 struct ddb_entry *ddb_entry, *dtemp;
477ffb9d 1203 int status = QLA_ERROR;
afaf5a2d 1204
f26b9044 1205 DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
f4f5df23
VC
1206 "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
1207 ha->host_no, __func__, ha->flags, ha->dpc_flags))
afaf5a2d
DS
1208
1209 /* Initialization not yet finished. Don't do anything yet. */
1210 if (!test_bit(AF_INIT_DONE, &ha->flags))
1211 return;
1212
f4f5df23
VC
1213 /* HBA is in the process of being permanently disabled.
1214 * Don't process anything */
1215 if (test_bit(AF_HBA_GOING_AWAY, &ha->flags))
1216 return;
1217
1218 if (is_qla8022(ha)) {
1219 if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
1220 qla4_8xxx_idc_lock(ha);
1221 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
1222 QLA82XX_DEV_FAILED);
1223 qla4_8xxx_idc_unlock(ha);
1224 ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
1225 qla4_8xxx_device_state_handler(ha);
1226 }
1227 if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
1228 qla4_8xxx_need_qsnt_handler(ha);
1229 }
1230 }
1231
1232 if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
1233 (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
afaf5a2d 1234 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
f4f5df23
VC
1235 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
1236 if (ql4xdontresethba) {
1237 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
1238 ha->host_no, __func__));
1239 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
1240 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1241 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1242 goto dpc_post_reset_ha;
1243 }
1244 if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
1245 test_bit(DPC_RESET_HA, &ha->dpc_flags))
1246 qla4xxx_recover_adapter(ha);
afaf5a2d 1247
477ffb9d 1248 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
afaf5a2d 1249 uint8_t wait_time = RESET_INTR_TOV;
afaf5a2d 1250
afaf5a2d
DS
1251 while ((readw(&ha->reg->ctrl_status) &
1252 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
1253 if (--wait_time == 0)
1254 break;
afaf5a2d 1255 msleep(1000);
afaf5a2d 1256 }
afaf5a2d
DS
1257 if (wait_time == 0)
1258 DEBUG2(printk("scsi%ld: %s: SR|FSR "
1259 "bit not cleared-- resetting\n",
1260 ha->host_no, __func__));
f4f5df23 1261 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
477ffb9d
DS
1262 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
1263 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
f4f5df23 1264 status = qla4xxx_recover_adapter(ha);
477ffb9d
DS
1265 }
1266 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1267 if (status == QLA_SUCCESS)
f4f5df23 1268 ha->isp_ops->enable_intrs(ha);
afaf5a2d
DS
1269 }
1270 }
1271
f4f5df23 1272dpc_post_reset_ha:
afaf5a2d
DS
1273 /* ---- process AEN? --- */
1274 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
1275 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
1276
1277 /* ---- Get DHCP IP Address? --- */
1278 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
1279 qla4xxx_get_dhcp_ip_address(ha);
1280
065aa1b4
VC
1281 /* ---- link change? --- */
1282 if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
1283 if (!test_bit(AF_LINK_UP, &ha->flags)) {
1284 /* ---- link down? --- */
1285 list_for_each_entry_safe(ddb_entry, dtemp,
1286 &ha->ddb_list, list) {
1287 if (atomic_read(&ddb_entry->state) ==
1288 DDB_STATE_ONLINE)
1289 qla4xxx_mark_device_missing(ha,
1290 ddb_entry);
1291 }
1292 } else {
1293 /* ---- link up? --- *
1294 * F/W will auto login to all devices ONLY ONCE after
1295 * link up during driver initialization and runtime
1296 * fatal error recovery. Therefore, the driver must
1297 * manually relogin to devices when recovering from
1298 * connection failures, logouts, expired KATO, etc. */
1299
1300 list_for_each_entry_safe(ddb_entry, dtemp,
1301 &ha->ddb_list, list) {
1302 if ((atomic_read(&ddb_entry->state) ==
1303 DDB_STATE_MISSING) ||
1304 (atomic_read(&ddb_entry->state) ==
1305 DDB_STATE_DEAD)) {
1306 if (ddb_entry->fw_ddb_device_state ==
1307 DDB_DS_SESSION_ACTIVE) {
1308 atomic_set(&ddb_entry->state,
1309 DDB_STATE_ONLINE);
c2660df3 1310 ql4_printk(KERN_INFO, ha,
065aa1b4 1311 "scsi%ld: %s: ddb[%d]"
f4f5df23 1312 " marked ONLINE\n",
065aa1b4 1313 ha->host_no, __func__,
f4f5df23 1314 ddb_entry->fw_ddb_index);
065aa1b4
VC
1315
1316 iscsi_unblock_session(
1317 ddb_entry->sess);
1318 } else
1319 qla4xxx_relogin_device(
1320 ha, ddb_entry);
1321 }
1322
1323 }
1324 }
1325 }
1326
afaf5a2d
DS
1327 /* ---- relogin device? --- */
1328 if (adapter_up(ha) &&
1329 test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
1330 list_for_each_entry_safe(ddb_entry, dtemp,
1331 &ha->ddb_list, list) {
1332 if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
1333 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
1334 qla4xxx_relogin_device(ha, ddb_entry);
1335
1336 /*
1337 * If mbx cmd times out there is no point
1338 * in continuing further.
1339 * With large no of targets this can hang
1340 * the system.
1341 */
1342 if (test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
1343 printk(KERN_WARNING "scsi%ld: %s: "
1344 "need to reset hba\n",
1345 ha->host_no, __func__);
1346 break;
1347 }
1348 }
1349 }
f4f5df23 1350 clear_bit(AF_DPC_SCHEDULED, &ha->flags);
afaf5a2d
DS
1351}
1352
1353/**
1354 * qla4xxx_free_adapter - release the adapter
1355 * @ha: pointer to adapter structure
1356 **/
1357static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
1358{
1359
1360 if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
1361 /* Turn-off interrupts on the card. */
f4f5df23 1362 ha->isp_ops->disable_intrs(ha);
afaf5a2d
DS
1363 }
1364
f4f5df23
VC
1365 /* Remove timer thread, if present */
1366 if (ha->timer_active)
1367 qla4xxx_stop_timer(ha);
1368
afaf5a2d
DS
1369 /* Kill the kernel thread for this host */
1370 if (ha->dpc_thread)
1371 destroy_workqueue(ha->dpc_thread);
1372
f4f5df23
VC
1373 /* Put firmware in known state */
1374 ha->isp_ops->reset_firmware(ha);
afaf5a2d 1375
f4f5df23
VC
1376 if (is_qla8022(ha)) {
1377 qla4_8xxx_idc_lock(ha);
1378 qla4_8xxx_clear_drv_active(ha);
1379 qla4_8xxx_idc_unlock(ha);
1380 }
afaf5a2d 1381
afaf5a2d
DS
1382 /* Detach interrupts */
1383 if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
f4f5df23 1384 qla4xxx_free_irqs(ha);
afaf5a2d 1385
bee4fe8e
DS
1386 /* free extra memory */
1387 qla4xxx_mem_free(ha);
f4f5df23
VC
1388}
1389
1390int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
1391{
1392 int status = 0;
1393 uint8_t revision_id;
1394 unsigned long mem_base, mem_len, db_base, db_len;
1395 struct pci_dev *pdev = ha->pdev;
1396
1397 status = pci_request_regions(pdev, DRIVER_NAME);
1398 if (status) {
1399 printk(KERN_WARNING
1400 "scsi(%ld) Failed to reserve PIO regions (%s) "
1401 "status=%d\n", ha->host_no, pci_name(pdev), status);
1402 goto iospace_error_exit;
1403 }
1404
1405 pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
1406 DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
1407 __func__, revision_id));
1408 ha->revision_id = revision_id;
bee4fe8e 1409
f4f5df23
VC
1410 /* remap phys address */
1411 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
1412 mem_len = pci_resource_len(pdev, 0);
1413 DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
1414 __func__, mem_base, mem_len));
afaf5a2d 1415
f4f5df23
VC
1416 /* mapping of pcibase pointer */
1417 ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
1418 if (!ha->nx_pcibase) {
1419 printk(KERN_ERR
1420 "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
1421 pci_release_regions(ha->pdev);
1422 goto iospace_error_exit;
1423 }
1424
1425 /* Mapping of IO base pointer, door bell read and write pointer */
1426
1427 /* mapping of IO base pointer */
1428 ha->qla4_8xxx_reg =
1429 (struct device_reg_82xx __iomem *)((uint8_t *)ha->nx_pcibase +
1430 0xbc000 + (ha->pdev->devfn << 11));
1431
1432 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
1433 db_len = pci_resource_len(pdev, 4);
1434
1435 /* mapping of doorbell write pointer */
1436 ha->nx_db_wr_ptr = (unsigned long)ioremap(db_base +
1437 (ha->pdev->devfn << 12), 4);
1438 if (!ha->nx_db_wr_ptr) {
1439 printk(KERN_ERR
1440 "cannot remap MMIO doorbell-write (%s), aborting\n",
1441 pci_name(pdev));
1442 goto iospace_error_exit;
1443 }
1444 /* mapping of doorbell read pointer */
1445 ha->nx_db_rd_ptr = (uint8_t *) ha->nx_pcibase + (512 * 1024) +
1446 (ha->pdev->devfn * 8);
1447 if (!ha->nx_db_rd_ptr)
1448 printk(KERN_ERR
1449 "cannot remap MMIO doorbell-read (%s), aborting\n",
1450 pci_name(pdev));
1451 return 0;
1452
1453iospace_error_exit:
1454 return -ENOMEM;
afaf5a2d
DS
1455}
1456
1457/***
1458 * qla4xxx_iospace_config - maps registers
1459 * @ha: pointer to adapter structure
1460 *
1461 * This routines maps HBA's registers from the pci address space
1462 * into the kernel virtual address space for memory mapped i/o.
1463 **/
f4f5df23 1464int qla4xxx_iospace_config(struct scsi_qla_host *ha)
afaf5a2d
DS
1465{
1466 unsigned long pio, pio_len, pio_flags;
1467 unsigned long mmio, mmio_len, mmio_flags;
1468
1469 pio = pci_resource_start(ha->pdev, 0);
1470 pio_len = pci_resource_len(ha->pdev, 0);
1471 pio_flags = pci_resource_flags(ha->pdev, 0);
1472 if (pio_flags & IORESOURCE_IO) {
1473 if (pio_len < MIN_IOBASE_LEN) {
c2660df3 1474 ql4_printk(KERN_WARNING, ha,
afaf5a2d
DS
1475 "Invalid PCI I/O region size\n");
1476 pio = 0;
1477 }
1478 } else {
c2660df3 1479 ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
afaf5a2d
DS
1480 pio = 0;
1481 }
1482
1483 /* Use MMIO operations for all accesses. */
1484 mmio = pci_resource_start(ha->pdev, 1);
1485 mmio_len = pci_resource_len(ha->pdev, 1);
1486 mmio_flags = pci_resource_flags(ha->pdev, 1);
1487
1488 if (!(mmio_flags & IORESOURCE_MEM)) {
c2660df3
VC
1489 ql4_printk(KERN_ERR, ha,
1490 "region #0 not an MMIO resource, aborting\n");
afaf5a2d
DS
1491
1492 goto iospace_error_exit;
1493 }
c2660df3 1494
afaf5a2d 1495 if (mmio_len < MIN_IOBASE_LEN) {
c2660df3
VC
1496 ql4_printk(KERN_ERR, ha,
1497 "Invalid PCI mem region size, aborting\n");
afaf5a2d
DS
1498 goto iospace_error_exit;
1499 }
1500
1501 if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
c2660df3
VC
1502 ql4_printk(KERN_WARNING, ha,
1503 "Failed to reserve PIO/MMIO regions\n");
afaf5a2d
DS
1504
1505 goto iospace_error_exit;
1506 }
1507
1508 ha->pio_address = pio;
1509 ha->pio_length = pio_len;
1510 ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
1511 if (!ha->reg) {
c2660df3
VC
1512 ql4_printk(KERN_ERR, ha,
1513 "cannot remap MMIO, aborting\n");
afaf5a2d
DS
1514
1515 goto iospace_error_exit;
1516 }
1517
1518 return 0;
1519
1520iospace_error_exit:
1521 return -ENOMEM;
1522}
1523
f4f5df23
VC
1524static struct isp_operations qla4xxx_isp_ops = {
1525 .iospace_config = qla4xxx_iospace_config,
1526 .pci_config = qla4xxx_pci_config,
1527 .disable_intrs = qla4xxx_disable_intrs,
1528 .enable_intrs = qla4xxx_enable_intrs,
1529 .start_firmware = qla4xxx_start_firmware,
1530 .intr_handler = qla4xxx_intr_handler,
1531 .interrupt_service_routine = qla4xxx_interrupt_service_routine,
1532 .reset_chip = qla4xxx_soft_reset,
1533 .reset_firmware = qla4xxx_hw_reset,
1534 .queue_iocb = qla4xxx_queue_iocb,
1535 .complete_iocb = qla4xxx_complete_iocb,
1536 .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
1537 .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
1538 .get_sys_info = qla4xxx_get_sys_info,
1539};
1540
1541static struct isp_operations qla4_8xxx_isp_ops = {
1542 .iospace_config = qla4_8xxx_iospace_config,
1543 .pci_config = qla4_8xxx_pci_config,
1544 .disable_intrs = qla4_8xxx_disable_intrs,
1545 .enable_intrs = qla4_8xxx_enable_intrs,
1546 .start_firmware = qla4_8xxx_load_risc,
1547 .intr_handler = qla4_8xxx_intr_handler,
1548 .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
1549 .reset_chip = qla4_8xxx_isp_reset,
1550 .reset_firmware = qla4_8xxx_stop_firmware,
1551 .queue_iocb = qla4_8xxx_queue_iocb,
1552 .complete_iocb = qla4_8xxx_complete_iocb,
1553 .rd_shdw_req_q_out = qla4_8xxx_rd_shdw_req_q_out,
1554 .rd_shdw_rsp_q_in = qla4_8xxx_rd_shdw_rsp_q_in,
1555 .get_sys_info = qla4_8xxx_get_sys_info,
1556};
1557
1558uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
1559{
1560 return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
1561}
1562
1563uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
1564{
1565 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
1566}
1567
1568uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
1569{
1570 return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
1571}
1572
1573uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
1574{
1575 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
1576}
1577
afaf5a2d
DS
1578/**
1579 * qla4xxx_probe_adapter - callback function to probe HBA
1580 * @pdev: pointer to pci_dev structure
1581 * @pci_device_id: pointer to pci_device entry
1582 *
1583 * This routine will probe for Qlogic 4xxx iSCSI host adapters.
1584 * It returns zero if successful. It also initializes all data necessary for
1585 * the driver.
1586 **/
1587static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
1588 const struct pci_device_id *ent)
1589{
1590 int ret = -ENODEV, status;
1591 struct Scsi_Host *host;
1592 struct scsi_qla_host *ha;
afaf5a2d
DS
1593 uint8_t init_retry_count = 0;
1594 char buf[34];
f4f5df23 1595 struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
afaf5a2d
DS
1596
1597 if (pci_enable_device(pdev))
1598 return -1;
1599
1600 host = scsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha));
1601 if (host == NULL) {
1602 printk(KERN_WARNING
1603 "qla4xxx: Couldn't allocate host from scsi layer!\n");
1604 goto probe_disable_device;
1605 }
1606
1607 /* Clear our data area */
1608 ha = (struct scsi_qla_host *) host->hostdata;
1609 memset(ha, 0, sizeof(*ha));
1610
1611 /* Save the information from PCI BIOS. */
1612 ha->pdev = pdev;
1613 ha->host = host;
1614 ha->host_no = host->host_no;
1615
f4f5df23
VC
1616 /* Setup Runtime configurable options */
1617 if (is_qla8022(ha)) {
1618 ha->isp_ops = &qla4_8xxx_isp_ops;
1619 rwlock_init(&ha->hw_lock);
1620 ha->qdr_sn_window = -1;
1621 ha->ddr_mn_window = -1;
1622 ha->curr_window = 255;
1623 ha->func_num = PCI_FUNC(ha->pdev->devfn);
1624 nx_legacy_intr = &legacy_intr[ha->func_num];
1625 ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
1626 ha->nx_legacy_intr.tgt_status_reg =
1627 nx_legacy_intr->tgt_status_reg;
1628 ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
1629 ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
1630 } else {
1631 ha->isp_ops = &qla4xxx_isp_ops;
1632 }
1633
afaf5a2d 1634 /* Configure PCI I/O space. */
f4f5df23 1635 ret = ha->isp_ops->iospace_config(ha);
afaf5a2d 1636 if (ret)
f4f5df23 1637 goto probe_failed_ioconfig;
afaf5a2d 1638
c2660df3 1639 ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
afaf5a2d
DS
1640 pdev->device, pdev->irq, ha->reg);
1641
1642 qla4xxx_config_dma_addressing(ha);
1643
1644 /* Initialize lists and spinlocks. */
1645 INIT_LIST_HEAD(&ha->ddb_list);
1646 INIT_LIST_HEAD(&ha->free_srb_q);
1647
1648 mutex_init(&ha->mbox_sem);
f4f5df23 1649 init_completion(&ha->mbx_intr_comp);
afaf5a2d
DS
1650
1651 spin_lock_init(&ha->hardware_lock);
afaf5a2d
DS
1652
1653 /* Allocate dma buffers */
1654 if (qla4xxx_mem_alloc(ha)) {
c2660df3
VC
1655 ql4_printk(KERN_WARNING, ha,
1656 "[ERROR] Failed to allocate memory for adapter\n");
afaf5a2d
DS
1657
1658 ret = -ENOMEM;
1659 goto probe_failed;
1660 }
1661
f4f5df23
VC
1662 if (is_qla8022(ha))
1663 (void) qla4_8xxx_get_flash_info(ha);
1664
afaf5a2d
DS
1665 /*
1666 * Initialize the Host adapter request/response queues and
1667 * firmware
1668 * NOTE: interrupts enabled upon successful completion
1669 */
1670 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
f4f5df23
VC
1671 while ((!test_bit(AF_ONLINE, &ha->flags)) &&
1672 init_retry_count++ < MAX_INIT_RETRIES) {
afaf5a2d
DS
1673 DEBUG2(printk("scsi: %s: retrying adapter initialization "
1674 "(%d)\n", __func__, init_retry_count));
f4f5df23
VC
1675
1676 if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
1677 continue;
1678
afaf5a2d
DS
1679 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1680 }
f4f5df23
VC
1681
1682 if (!test_bit(AF_ONLINE, &ha->flags)) {
c2660df3 1683 ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
afaf5a2d
DS
1684
1685 ret = -ENODEV;
1686 goto probe_failed;
1687 }
1688
1689 host->cmd_per_lun = 3;
1690 host->max_channel = 0;
1691 host->max_lun = MAX_LUNS - 1;
1692 host->max_id = MAX_TARGETS;
1693 host->max_cmd_len = IOCB_MAX_CDB_LEN;
1694 host->can_queue = MAX_SRBS ;
1695 host->transportt = qla4xxx_scsi_transport;
1696
1697 ret = scsi_init_shared_tag_map(host, MAX_SRBS);
1698 if (ret) {
c2660df3
VC
1699 ql4_printk(KERN_WARNING, ha,
1700 "scsi_init_shared_tag_map failed\n");
1701 goto probe_failed;
afaf5a2d
DS
1702 }
1703
1704 /* Startup the kernel thread for this host adapter. */
1705 DEBUG2(printk("scsi: %s: Starting kernel thread for "
1706 "qla4xxx_dpc\n", __func__));
1707 sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
1708 ha->dpc_thread = create_singlethread_workqueue(buf);
1709 if (!ha->dpc_thread) {
c2660df3 1710 ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
afaf5a2d
DS
1711 ret = -ENODEV;
1712 goto probe_failed;
1713 }
c4028958 1714 INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
afaf5a2d 1715
f4f5df23
VC
1716 /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
1717 * (which is called indirectly by qla4xxx_initialize_adapter),
1718 * so that irqs will be registered after crbinit but before
1719 * mbx_intr_enable.
1720 */
1721 if (!is_qla8022(ha)) {
1722 ret = qla4xxx_request_irqs(ha);
1723 if (ret) {
1724 ql4_printk(KERN_WARNING, ha, "Failed to reserve "
1725 "interrupt %d already in use.\n", pdev->irq);
1726 goto probe_failed;
1727 }
afaf5a2d 1728 }
afaf5a2d 1729
f4f5df23 1730 ha->isp_ops->enable_intrs(ha);
afaf5a2d
DS
1731
1732 /* Start timer thread. */
1733 qla4xxx_start_timer(ha, qla4xxx_timer, 1);
1734
1735 set_bit(AF_INIT_DONE, &ha->flags);
1736
1737 pci_set_drvdata(pdev, ha);
1738
1739 ret = scsi_add_host(host, &pdev->dev);
1740 if (ret)
1741 goto probe_failed;
1742
afaf5a2d
DS
1743 printk(KERN_INFO
1744 " QLogic iSCSI HBA Driver version: %s\n"
1745 " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
1746 qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
1747 ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
1748 ha->patch_number, ha->build_number);
921601b7 1749 scsi_scan_host(host);
afaf5a2d
DS
1750 return 0;
1751
afaf5a2d
DS
1752probe_failed:
1753 qla4xxx_free_adapter(ha);
f4f5df23
VC
1754
1755probe_failed_ioconfig:
afaf5a2d
DS
1756 scsi_host_put(ha->host);
1757
1758probe_disable_device:
1759 pci_disable_device(pdev);
1760
1761 return ret;
1762}
1763
1764/**
1765 * qla4xxx_remove_adapter - calback function to remove adapter.
1766 * @pci_dev: PCI device pointer
1767 **/
1768static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
1769{
1770 struct scsi_qla_host *ha;
1771
1772 ha = pci_get_drvdata(pdev);
1773
f4f5df23 1774 set_bit(AF_HBA_GOING_AWAY, &ha->flags);
bee4fe8e 1775
afaf5a2d
DS
1776 /* remove devs from iscsi_sessions to scsi_devices */
1777 qla4xxx_free_ddb_list(ha);
1778
1779 scsi_remove_host(ha->host);
1780
1781 qla4xxx_free_adapter(ha);
1782
1783 scsi_host_put(ha->host);
1784
f4f5df23 1785 pci_disable_device(pdev);
afaf5a2d
DS
1786 pci_set_drvdata(pdev, NULL);
1787}
1788
1789/**
1790 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
1791 * @ha: HA context
1792 *
1793 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
1794 * supported addressing method.
1795 */
47975477 1796static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
afaf5a2d
DS
1797{
1798 int retval;
1799
1800 /* Update our PCI device dma_mask for full 64 bit mask */
6a35528a
YH
1801 if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
1802 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
afaf5a2d
DS
1803 dev_dbg(&ha->pdev->dev,
1804 "Failed to set 64 bit PCI consistent mask; "
1805 "using 32 bit.\n");
1806 retval = pci_set_consistent_dma_mask(ha->pdev,
284901a9 1807 DMA_BIT_MASK(32));
afaf5a2d
DS
1808 }
1809 } else
284901a9 1810 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
afaf5a2d
DS
1811}
1812
1813static int qla4xxx_slave_alloc(struct scsi_device *sdev)
1814{
1815 struct iscsi_cls_session *sess = starget_to_session(sdev->sdev_target);
1816 struct ddb_entry *ddb = sess->dd_data;
1817
1818 sdev->hostdata = ddb;
1819 sdev->tagged_supported = 1;
d510d965 1820 scsi_activate_tcq(sdev, QL4_DEF_QDEPTH);
afaf5a2d
DS
1821 return 0;
1822}
1823
1824static int qla4xxx_slave_configure(struct scsi_device *sdev)
1825{
1826 sdev->tagged_supported = 1;
1827 return 0;
1828}
1829
1830static void qla4xxx_slave_destroy(struct scsi_device *sdev)
1831{
1832 scsi_deactivate_tcq(sdev, 1);
1833}
1834
1835/**
1836 * qla4xxx_del_from_active_array - returns an active srb
1837 * @ha: Pointer to host adapter structure.
fd589a8f 1838 * @index: index into the active_array
afaf5a2d
DS
1839 *
1840 * This routine removes and returns the srb at the specified index
1841 **/
f4f5df23
VC
1842struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
1843 uint32_t index)
afaf5a2d
DS
1844{
1845 struct srb *srb = NULL;
5369887a 1846 struct scsi_cmnd *cmd = NULL;
afaf5a2d 1847
5369887a
VC
1848 cmd = scsi_host_find_tag(ha->host, index);
1849 if (!cmd)
afaf5a2d
DS
1850 return srb;
1851
5369887a
VC
1852 srb = (struct srb *)CMD_SP(cmd);
1853 if (!srb)
afaf5a2d
DS
1854 return srb;
1855
1856 /* update counters */
1857 if (srb->flags & SRB_DMA_VALID) {
1858 ha->req_q_count += srb->iocb_cnt;
1859 ha->iocb_cnt -= srb->iocb_cnt;
1860 if (srb->cmd)
5369887a
VC
1861 srb->cmd->host_scribble =
1862 (unsigned char *)(unsigned long) MAX_SRBS;
afaf5a2d
DS
1863 }
1864 return srb;
1865}
1866
afaf5a2d
DS
1867/**
1868 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
09a0f719 1869 * @ha: Pointer to host adapter structure.
afaf5a2d
DS
1870 * @cmd: Scsi Command to wait on.
1871 *
1872 * This routine waits for the command to be returned by the Firmware
1873 * for some max time.
1874 **/
1875static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
1876 struct scsi_cmnd *cmd)
1877{
1878 int done = 0;
1879 struct srb *rp;
1880 uint32_t max_wait_time = EH_WAIT_CMD_TOV;
1881
1882 do {
1883 /* Checking to see if its returned to OS */
5369887a 1884 rp = (struct srb *) CMD_SP(cmd);
afaf5a2d
DS
1885 if (rp == NULL) {
1886 done++;
1887 break;
1888 }
1889
1890 msleep(2000);
1891 } while (max_wait_time--);
1892
1893 return done;
1894}
1895
1896/**
1897 * qla4xxx_wait_for_hba_online - waits for HBA to come online
1898 * @ha: Pointer to host adapter structure
1899 **/
1900static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
1901{
1902 unsigned long wait_online;
1903
1904 wait_online = jiffies + (30 * HZ);
1905 while (time_before(jiffies, wait_online)) {
1906
1907 if (adapter_up(ha))
1908 return QLA_SUCCESS;
1909 else if (ha->retry_reset_ha_cnt == 0)
1910 return QLA_ERROR;
1911
1912 msleep(2000);
1913 }
1914
1915 return QLA_ERROR;
1916}
1917
1918/**
ce545039 1919 * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
fd589a8f 1920 * @ha: pointer to HBA
afaf5a2d
DS
1921 * @t: target id
1922 * @l: lun id
1923 *
1924 * This function waits for all outstanding commands to a lun to complete. It
1925 * returns 0 if all pending commands are returned and 1 otherwise.
1926 **/
ce545039
MC
1927static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
1928 struct scsi_target *stgt,
1929 struct scsi_device *sdev)
afaf5a2d
DS
1930{
1931 int cnt;
1932 int status = 0;
1933 struct scsi_cmnd *cmd;
1934
1935 /*
ce545039
MC
1936 * Waiting for all commands for the designated target or dev
1937 * in the active array
afaf5a2d
DS
1938 */
1939 for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
1940 cmd = scsi_host_find_tag(ha->host, cnt);
ce545039
MC
1941 if (cmd && stgt == scsi_target(cmd->device) &&
1942 (!sdev || sdev == cmd->device)) {
afaf5a2d
DS
1943 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
1944 status++;
1945 break;
1946 }
1947 }
1948 }
1949 return status;
1950}
1951
09a0f719
VC
1952/**
1953 * qla4xxx_eh_abort - callback for abort task.
1954 * @cmd: Pointer to Linux's SCSI command structure
1955 *
1956 * This routine is called by the Linux OS to abort the specified
1957 * command.
1958 **/
1959static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
1960{
1961 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
1962 unsigned int id = cmd->device->id;
1963 unsigned int lun = cmd->device->lun;
1964 unsigned long serial = cmd->serial_number;
1965 struct srb *srb = NULL;
1966 int ret = SUCCESS;
1967 int wait = 0;
1968
c2660df3 1969 ql4_printk(KERN_INFO, ha,
09a0f719
VC
1970 "scsi%ld:%d:%d: Abort command issued cmd=%p, pid=%ld\n",
1971 ha->host_no, id, lun, cmd, serial);
1972
1973 srb = (struct srb *) CMD_SP(cmd);
1974
1975 if (!srb)
1976 return SUCCESS;
1977
1978 kref_get(&srb->srb_ref);
1979
1980 if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
1981 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
1982 ha->host_no, id, lun));
1983 ret = FAILED;
1984 } else {
1985 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
1986 ha->host_no, id, lun));
1987 wait = 1;
1988 }
1989
1990 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
1991
1992 /* Wait for command to complete */
1993 if (wait) {
1994 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
1995 DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
1996 ha->host_no, id, lun));
1997 ret = FAILED;
1998 }
1999 }
2000
c2660df3 2001 ql4_printk(KERN_INFO, ha,
09a0f719
VC
2002 "scsi%ld:%d:%d: Abort command - %s\n",
2003 ha->host_no, id, lun, (ret == SUCCESS) ? "succeded" : "failed");
2004
2005 return ret;
2006}
2007
afaf5a2d
DS
2008/**
2009 * qla4xxx_eh_device_reset - callback for target reset.
2010 * @cmd: Pointer to Linux's SCSI command structure
2011 *
2012 * This routine is called by the Linux OS to reset all luns on the
2013 * specified target.
2014 **/
2015static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
2016{
2017 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2018 struct ddb_entry *ddb_entry = cmd->device->hostdata;
afaf5a2d
DS
2019 int ret = FAILED, stat;
2020
612f7348 2021 if (!ddb_entry)
afaf5a2d
DS
2022 return ret;
2023
c01be6dc
MC
2024 ret = iscsi_block_scsi_eh(cmd);
2025 if (ret)
2026 return ret;
2027 ret = FAILED;
2028
c2660df3 2029 ql4_printk(KERN_INFO, ha,
afaf5a2d
DS
2030 "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
2031 cmd->device->channel, cmd->device->id, cmd->device->lun);
2032
2033 DEBUG2(printk(KERN_INFO
2034 "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
2035 "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
242f9dcb 2036 cmd, jiffies, cmd->request->timeout / HZ,
afaf5a2d
DS
2037 ha->dpc_flags, cmd->result, cmd->allowed));
2038
2039 /* FIXME: wait for hba to go online */
2040 stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
2041 if (stat != QLA_SUCCESS) {
c2660df3 2042 ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
afaf5a2d
DS
2043 goto eh_dev_reset_done;
2044 }
2045
ce545039
MC
2046 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2047 cmd->device)) {
c2660df3 2048 ql4_printk(KERN_INFO, ha,
ce545039
MC
2049 "DEVICE RESET FAILED - waiting for "
2050 "commands.\n");
2051 goto eh_dev_reset_done;
afaf5a2d
DS
2052 }
2053
9d562913
DS
2054 /* Send marker. */
2055 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2056 MM_LUN_RESET) != QLA_SUCCESS)
2057 goto eh_dev_reset_done;
2058
c2660df3 2059 ql4_printk(KERN_INFO, ha,
afaf5a2d
DS
2060 "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
2061 ha->host_no, cmd->device->channel, cmd->device->id,
2062 cmd->device->lun);
2063
2064 ret = SUCCESS;
2065
2066eh_dev_reset_done:
2067
2068 return ret;
2069}
2070
ce545039
MC
2071/**
2072 * qla4xxx_eh_target_reset - callback for target reset.
2073 * @cmd: Pointer to Linux's SCSI command structure
2074 *
2075 * This routine is called by the Linux OS to reset the target.
2076 **/
2077static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
2078{
2079 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2080 struct ddb_entry *ddb_entry = cmd->device->hostdata;
c01be6dc 2081 int stat, ret;
ce545039
MC
2082
2083 if (!ddb_entry)
2084 return FAILED;
2085
c01be6dc
MC
2086 ret = iscsi_block_scsi_eh(cmd);
2087 if (ret)
2088 return ret;
2089
ce545039
MC
2090 starget_printk(KERN_INFO, scsi_target(cmd->device),
2091 "WARM TARGET RESET ISSUED.\n");
2092
2093 DEBUG2(printk(KERN_INFO
2094 "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
2095 "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
242f9dcb 2096 ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
ce545039
MC
2097 ha->dpc_flags, cmd->result, cmd->allowed));
2098
2099 stat = qla4xxx_reset_target(ha, ddb_entry);
2100 if (stat != QLA_SUCCESS) {
2101 starget_printk(KERN_INFO, scsi_target(cmd->device),
2102 "WARM TARGET RESET FAILED.\n");
2103 return FAILED;
2104 }
2105
ce545039
MC
2106 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2107 NULL)) {
2108 starget_printk(KERN_INFO, scsi_target(cmd->device),
2109 "WARM TARGET DEVICE RESET FAILED - "
2110 "waiting for commands.\n");
2111 return FAILED;
2112 }
2113
9d562913
DS
2114 /* Send marker. */
2115 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2116 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
2117 starget_printk(KERN_INFO, scsi_target(cmd->device),
2118 "WARM TARGET DEVICE RESET FAILED - "
2119 "marker iocb failed.\n");
2120 return FAILED;
2121 }
2122
ce545039
MC
2123 starget_printk(KERN_INFO, scsi_target(cmd->device),
2124 "WARM TARGET RESET SUCCEEDED.\n");
2125 return SUCCESS;
2126}
2127
afaf5a2d
DS
2128/**
2129 * qla4xxx_eh_host_reset - kernel callback
2130 * @cmd: Pointer to Linux's SCSI command structure
2131 *
2132 * This routine is invoked by the Linux kernel to perform fatal error
2133 * recovery on the specified adapter.
2134 **/
2135static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
2136{
2137 int return_status = FAILED;
2138 struct scsi_qla_host *ha;
2139
2140 ha = (struct scsi_qla_host *) cmd->device->host->hostdata;
2141
f4f5df23
VC
2142 if (ql4xdontresethba) {
2143 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
2144 ha->host_no, __func__));
2145 return FAILED;
2146 }
2147
c2660df3 2148 ql4_printk(KERN_INFO, ha,
dca05c4c 2149 "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
afaf5a2d
DS
2150 cmd->device->channel, cmd->device->id, cmd->device->lun);
2151
2152 if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
2153 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
2154 "DEAD.\n", ha->host_no, cmd->device->channel,
2155 __func__));
2156
2157 return FAILED;
2158 }
2159
f4f5df23
VC
2160 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
2161 if (is_qla8022(ha))
2162 set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2163 else
2164 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2165 }
50a29aec 2166
f4f5df23 2167 if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
afaf5a2d 2168 return_status = SUCCESS;
afaf5a2d 2169
c2660df3 2170 ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
afaf5a2d
DS
2171 return_status == FAILED ? "FAILED" : "SUCCEDED");
2172
2173 return return_status;
2174}
2175
afaf5a2d
DS
2176static struct pci_device_id qla4xxx_pci_tbl[] = {
2177 {
2178 .vendor = PCI_VENDOR_ID_QLOGIC,
2179 .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
2180 .subvendor = PCI_ANY_ID,
2181 .subdevice = PCI_ANY_ID,
2182 },
2183 {
2184 .vendor = PCI_VENDOR_ID_QLOGIC,
2185 .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
2186 .subvendor = PCI_ANY_ID,
2187 .subdevice = PCI_ANY_ID,
2188 },
d915058f
DS
2189 {
2190 .vendor = PCI_VENDOR_ID_QLOGIC,
2191 .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
2192 .subvendor = PCI_ANY_ID,
2193 .subdevice = PCI_ANY_ID,
2194 },
f4f5df23
VC
2195 {
2196 .vendor = PCI_VENDOR_ID_QLOGIC,
2197 .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
2198 .subvendor = PCI_ANY_ID,
2199 .subdevice = PCI_ANY_ID,
2200 },
afaf5a2d
DS
2201 {0, 0},
2202};
2203MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
2204
47975477 2205static struct pci_driver qla4xxx_pci_driver = {
afaf5a2d
DS
2206 .name = DRIVER_NAME,
2207 .id_table = qla4xxx_pci_tbl,
2208 .probe = qla4xxx_probe_adapter,
2209 .remove = qla4xxx_remove_adapter,
2210};
2211
2212static int __init qla4xxx_module_init(void)
2213{
2214 int ret;
2215
2216 /* Allocate cache for SRBs. */
2217 srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
20c2df83 2218 SLAB_HWCACHE_ALIGN, NULL);
afaf5a2d
DS
2219 if (srb_cachep == NULL) {
2220 printk(KERN_ERR
2221 "%s: Unable to allocate SRB cache..."
2222 "Failing load!\n", DRIVER_NAME);
2223 ret = -ENOMEM;
2224 goto no_srp_cache;
2225 }
2226
2227 /* Derive version string. */
2228 strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
11010fec 2229 if (ql4xextended_error_logging)
afaf5a2d
DS
2230 strcat(qla4xxx_version_str, "-debug");
2231
2232 qla4xxx_scsi_transport =
2233 iscsi_register_transport(&qla4xxx_iscsi_transport);
2234 if (!qla4xxx_scsi_transport){
2235 ret = -ENODEV;
2236 goto release_srb_cache;
2237 }
2238
afaf5a2d
DS
2239 ret = pci_register_driver(&qla4xxx_pci_driver);
2240 if (ret)
2241 goto unregister_transport;
2242
2243 printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
2244 return 0;
5ae16db3 2245
afaf5a2d
DS
2246unregister_transport:
2247 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
2248release_srb_cache:
2249 kmem_cache_destroy(srb_cachep);
2250no_srp_cache:
2251 return ret;
2252}
2253
2254static void __exit qla4xxx_module_exit(void)
2255{
2256 pci_unregister_driver(&qla4xxx_pci_driver);
2257 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
2258 kmem_cache_destroy(srb_cachep);
2259}
2260
2261module_init(qla4xxx_module_init);
2262module_exit(qla4xxx_module_exit);
2263
2264MODULE_AUTHOR("QLogic Corporation");
2265MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
2266MODULE_LICENSE("GPL");
2267MODULE_VERSION(QLA4XXX_DRIVER_VERSION);