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