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