blob: 0e4688c9e0a2ed9ffbc2177360c8a4f27cef17e3 [file] [log] [blame]
David Somayajuluafaf5a22006-09-19 10:28:00 -07001/*
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"
David C Somayajulubee4fe82007-05-23 18:03:32 -070013#include "ql4_version.h"
14#include "ql4_glbl.h"
15#include "ql4_dbg.h"
16#include "ql4_inline.h"
David Somayajuluafaf5a22006-09-19 10:28:00 -070017
18/*
19 * Driver version
20 */
Adrian Bunk47975472007-04-26 00:35:16 -070021static char qla4xxx_version_str[40];
David Somayajuluafaf5a22006-09-19 10:28:00 -070022
23/*
24 * SRB allocation cache
25 */
Christoph Lametere18b8902006-12-06 20:33:20 -080026static struct kmem_cache *srb_cachep;
David Somayajuluafaf5a22006-09-19 10:28:00 -070027
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
Andrew Vasquez11010fe2006-10-06 09:54:59 -070041int 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,
David Somayajuluafaf5a22006-09-19 10:28:00 -070044 "Option to enable extended error logging, "
45 "Default is 0 - no logging, 1 - debug logging");
46
David C Somayajulu477ffb92007-01-22 12:26:11 -080047int ql4_mod_unload = 0;
48
David Somayajuluafaf5a22006-09-19 10:28:00 -070049/*
50 * SCSI host template entry points
51 */
Adrian Bunk47975472007-04-26 00:35:16 -070052static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -070053
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) {
376 if (cmd->use_sg) {
377 pci_unmap_sg(ha->pdev, cmd->request_buffer,
378 cmd->use_sg, cmd->sc_data_direction);
379 } else if (cmd->request_bufflen) {
380 pci_unmap_single(ha->pdev, srb->dma_handle,
381 cmd->request_bufflen,
382 cmd->sc_data_direction);
383 }
384 srb->flags &= ~SRB_DMA_VALID;
385 }
386 cmd->SCp.ptr = NULL;
387}
388
389void qla4xxx_srb_compl(struct scsi_qla_host *ha, struct srb *srb)
390{
391 struct scsi_cmnd *cmd = srb->cmd;
392
393 qla4xxx_srb_free_dma(ha, srb);
394
395 mempool_free(srb, ha->srb_mempool);
396
397 cmd->scsi_done(cmd);
398}
399
400/**
401 * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
402 * @cmd: Pointer to Linux's SCSI command structure
403 * @done_fn: Function that the driver calls to notify the SCSI mid-layer
404 * that the command has been processed.
405 *
406 * Remarks:
407 * This routine is invoked by Linux to send a SCSI command to the driver.
408 * The mid-level driver tries to ensure that queuecommand never gets
409 * invoked concurrently with itself or the interrupt handler (although
410 * the interrupt handler may call this routine as part of request-
411 * completion handling). Unfortunely, it sometimes calls the scheduler
412 * in interrupt context which is a big NO! NO!.
413 **/
414static int qla4xxx_queuecommand(struct scsi_cmnd *cmd,
415 void (*done)(struct scsi_cmnd *))
416{
417 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
418 struct ddb_entry *ddb_entry = cmd->device->hostdata;
419 struct srb *srb;
420 int rval;
421
422 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
423 if (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD) {
424 cmd->result = DID_NO_CONNECT << 16;
425 goto qc_fail_command;
426 }
427 goto qc_host_busy;
428 }
429
David C Somayajulu477ffb92007-01-22 12:26:11 -0800430 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags))
431 goto qc_host_busy;
432
David Somayajuluafaf5a22006-09-19 10:28:00 -0700433 spin_unlock_irq(ha->host->host_lock);
434
435 srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd, done);
436 if (!srb)
437 goto qc_host_busy_lock;
438
439 rval = qla4xxx_send_command_to_isp(ha, srb);
440 if (rval != QLA_SUCCESS)
441 goto qc_host_busy_free_sp;
442
443 spin_lock_irq(ha->host->host_lock);
444 return 0;
445
446qc_host_busy_free_sp:
447 qla4xxx_srb_free_dma(ha, srb);
448 mempool_free(srb, ha->srb_mempool);
449
450qc_host_busy_lock:
451 spin_lock_irq(ha->host->host_lock);
452
453qc_host_busy:
454 return SCSI_MLQUEUE_HOST_BUSY;
455
456qc_fail_command:
457 done(cmd);
458
459 return 0;
460}
461
462/**
463 * qla4xxx_mem_free - frees memory allocated to adapter
464 * @ha: Pointer to host adapter structure.
465 *
466 * Frees memory previously allocated by qla4xxx_mem_alloc
467 **/
468static void qla4xxx_mem_free(struct scsi_qla_host *ha)
469{
470 if (ha->queues)
471 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
472 ha->queues_dma);
473
474 ha->queues_len = 0;
475 ha->queues = NULL;
476 ha->queues_dma = 0;
477 ha->request_ring = NULL;
478 ha->request_dma = 0;
479 ha->response_ring = NULL;
480 ha->response_dma = 0;
481 ha->shadow_regs = NULL;
482 ha->shadow_regs_dma = 0;
483
484 /* Free srb pool. */
485 if (ha->srb_mempool)
486 mempool_destroy(ha->srb_mempool);
487
488 ha->srb_mempool = NULL;
489
490 /* release io space registers */
491 if (ha->reg)
492 iounmap(ha->reg);
493 pci_release_regions(ha->pdev);
494}
495
496/**
497 * qla4xxx_mem_alloc - allocates memory for use by adapter.
498 * @ha: Pointer to host adapter structure
499 *
500 * Allocates DMA memory for request and response queues. Also allocates memory
501 * for srbs.
502 **/
503static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
504{
505 unsigned long align;
506
507 /* Allocate contiguous block of DMA memory for queues. */
508 ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
509 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
510 sizeof(struct shadow_regs) +
511 MEM_ALIGN_VALUE +
512 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
513 ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
514 &ha->queues_dma, GFP_KERNEL);
515 if (ha->queues == NULL) {
516 dev_warn(&ha->pdev->dev,
517 "Memory Allocation failed - queues.\n");
518
519 goto mem_alloc_error_exit;
520 }
521 memset(ha->queues, 0, ha->queues_len);
522
523 /*
524 * As per RISC alignment requirements -- the bus-address must be a
525 * multiple of the request-ring size (in bytes).
526 */
527 align = 0;
528 if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
529 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
530 (MEM_ALIGN_VALUE - 1));
531
532 /* Update request and response queue pointers. */
533 ha->request_dma = ha->queues_dma + align;
534 ha->request_ring = (struct queue_entry *) (ha->queues + align);
535 ha->response_dma = ha->queues_dma + align +
536 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
537 ha->response_ring = (struct queue_entry *) (ha->queues + align +
538 (REQUEST_QUEUE_DEPTH *
539 QUEUE_SIZE));
540 ha->shadow_regs_dma = ha->queues_dma + align +
541 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
542 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
543 ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
544 (REQUEST_QUEUE_DEPTH *
545 QUEUE_SIZE) +
546 (RESPONSE_QUEUE_DEPTH *
547 QUEUE_SIZE));
548
549 /* Allocate memory for srb pool. */
550 ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
551 mempool_free_slab, srb_cachep);
552 if (ha->srb_mempool == NULL) {
553 dev_warn(&ha->pdev->dev,
554 "Memory Allocation failed - SRB Pool.\n");
555
556 goto mem_alloc_error_exit;
557 }
558
559 return QLA_SUCCESS;
560
561mem_alloc_error_exit:
562 qla4xxx_mem_free(ha);
563 return QLA_ERROR;
564}
565
566/**
567 * qla4xxx_timer - checks every second for work to do.
568 * @ha: Pointer to host adapter structure.
569 **/
570static void qla4xxx_timer(struct scsi_qla_host *ha)
571{
572 struct ddb_entry *ddb_entry, *dtemp;
573 int start_dpc = 0;
574
575 /* Search for relogin's to time-out and port down retry. */
576 list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
577 /* Count down time between sending relogins */
578 if (adapter_up(ha) &&
579 !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
580 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
581 if (atomic_read(&ddb_entry->retry_relogin_timer) !=
582 INVALID_ENTRY) {
583 if (atomic_read(&ddb_entry->retry_relogin_timer)
584 == 0) {
585 atomic_set(&ddb_entry->
586 retry_relogin_timer,
587 INVALID_ENTRY);
588 set_bit(DPC_RELOGIN_DEVICE,
589 &ha->dpc_flags);
590 set_bit(DF_RELOGIN, &ddb_entry->flags);
591 DEBUG2(printk("scsi%ld: %s: index [%d]"
592 " login device\n",
593 ha->host_no, __func__,
594 ddb_entry->fw_ddb_index));
595 } else
596 atomic_dec(&ddb_entry->
597 retry_relogin_timer);
598 }
599 }
600
601 /* Wait for relogin to timeout */
602 if (atomic_read(&ddb_entry->relogin_timer) &&
603 (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
604 /*
605 * If the relogin times out and the device is
606 * still NOT ONLINE then try and relogin again.
607 */
608 if (atomic_read(&ddb_entry->state) !=
609 DDB_STATE_ONLINE &&
610 ddb_entry->fw_ddb_device_state ==
611 DDB_DS_SESSION_FAILED) {
612 /* Reset retry relogin timer */
613 atomic_inc(&ddb_entry->relogin_retry_count);
614 DEBUG2(printk("scsi%ld: index[%d] relogin"
615 " timed out-retrying"
616 " relogin (%d)\n",
617 ha->host_no,
618 ddb_entry->fw_ddb_index,
619 atomic_read(&ddb_entry->
620 relogin_retry_count))
621 );
622 start_dpc++;
623 DEBUG(printk("scsi%ld:%d:%d: index [%d] "
624 "initate relogin after"
625 " %d seconds\n",
626 ha->host_no, ddb_entry->bus,
627 ddb_entry->target,
628 ddb_entry->fw_ddb_index,
629 ddb_entry->default_time2wait + 4)
630 );
631
632 atomic_set(&ddb_entry->retry_relogin_timer,
633 ddb_entry->default_time2wait + 4);
634 }
635 }
636 }
637
638 /* Check for heartbeat interval. */
639 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
640 ha->heartbeat_interval != 0) {
641 ha->seconds_since_last_heartbeat++;
642 if (ha->seconds_since_last_heartbeat >
643 ha->heartbeat_interval + 2)
644 set_bit(DPC_RESET_HA, &ha->dpc_flags);
645 }
646
647
648 /* Wakeup the dpc routine for this adapter, if needed. */
649 if ((start_dpc ||
650 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
651 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
652 test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
653 test_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags) ||
654 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
655 test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
656 test_bit(DPC_AEN, &ha->dpc_flags)) &&
657 ha->dpc_thread) {
658 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
659 " - dpc flags = 0x%lx\n",
660 ha->host_no, __func__, ha->dpc_flags));
661 queue_work(ha->dpc_thread, &ha->dpc_work);
662 }
663
664 /* Reschedule timer thread to call us back in one second */
665 mod_timer(&ha->timer, jiffies + HZ);
666
667 DEBUG2(ha->seconds_since_last_intr++);
668}
669
670/**
671 * qla4xxx_cmd_wait - waits for all outstanding commands to complete
672 * @ha: Pointer to host adapter structure.
673 *
674 * This routine stalls the driver until all outstanding commands are returned.
675 * Caller must release the Hardware Lock prior to calling this routine.
676 **/
677static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
678{
679 uint32_t index = 0;
680 int stat = QLA_SUCCESS;
681 unsigned long flags;
682 struct scsi_cmnd *cmd;
683 int wait_cnt = WAIT_CMD_TOV; /*
684 * Initialized for 30 seconds as we
685 * expect all commands to retuned
686 * ASAP.
687 */
688
689 while (wait_cnt) {
690 spin_lock_irqsave(&ha->hardware_lock, flags);
691 /* Find a command that hasn't completed. */
692 for (index = 0; index < ha->host->can_queue; index++) {
693 cmd = scsi_host_find_tag(ha->host, index);
694 if (cmd != NULL)
695 break;
696 }
697 spin_unlock_irqrestore(&ha->hardware_lock, flags);
698
699 /* If No Commands are pending, wait is complete */
700 if (index == ha->host->can_queue) {
701 break;
702 }
703
704 /* If we timed out on waiting for commands to come back
705 * return ERROR.
706 */
707 wait_cnt--;
708 if (wait_cnt == 0)
709 stat = QLA_ERROR;
710 else {
711 msleep(1000);
712 }
713 } /* End of While (wait_cnt) */
714
715 return stat;
716}
717
David C Somayajulubee4fe82007-05-23 18:03:32 -0700718void qla4xxx_hw_reset(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700719{
David Somayajuluafaf5a22006-09-19 10:28:00 -0700720 uint32_t ctrl_status;
David C Somayajulu477ffb92007-01-22 12:26:11 -0800721 unsigned long flags = 0;
722
723 DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700724
725 spin_lock_irqsave(&ha->hardware_lock, flags);
726
727 /*
728 * If the SCSI Reset Interrupt bit is set, clear it.
729 * Otherwise, the Soft Reset won't work.
730 */
731 ctrl_status = readw(&ha->reg->ctrl_status);
732 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
733 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
734
735 /* Issue Soft Reset */
736 writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
737 readl(&ha->reg->ctrl_status);
738
739 spin_unlock_irqrestore(&ha->hardware_lock, flags);
David C Somayajulu477ffb92007-01-22 12:26:11 -0800740}
741
742/**
743 * qla4xxx_soft_reset - performs soft reset.
744 * @ha: Pointer to host adapter structure.
745 **/
746int qla4xxx_soft_reset(struct scsi_qla_host *ha)
747{
748 uint32_t max_wait_time;
749 unsigned long flags = 0;
750 int status = QLA_ERROR;
751 uint32_t ctrl_status;
752
753 qla4xxx_hw_reset(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700754
755 /* Wait until the Network Reset Intr bit is cleared */
756 max_wait_time = RESET_INTR_TOV;
757 do {
758 spin_lock_irqsave(&ha->hardware_lock, flags);
759 ctrl_status = readw(&ha->reg->ctrl_status);
760 spin_unlock_irqrestore(&ha->hardware_lock, flags);
761
762 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
763 break;
764
765 msleep(1000);
766 } while ((--max_wait_time));
767
768 if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
769 DEBUG2(printk(KERN_WARNING
770 "scsi%ld: Network Reset Intr not cleared by "
771 "Network function, clearing it now!\n",
772 ha->host_no));
773 spin_lock_irqsave(&ha->hardware_lock, flags);
774 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
775 readl(&ha->reg->ctrl_status);
776 spin_unlock_irqrestore(&ha->hardware_lock, flags);
777 }
778
779 /* Wait until the firmware tells us the Soft Reset is done */
780 max_wait_time = SOFT_RESET_TOV;
781 do {
782 spin_lock_irqsave(&ha->hardware_lock, flags);
783 ctrl_status = readw(&ha->reg->ctrl_status);
784 spin_unlock_irqrestore(&ha->hardware_lock, flags);
785
786 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
787 status = QLA_SUCCESS;
788 break;
789 }
790
791 msleep(1000);
792 } while ((--max_wait_time));
793
794 /*
795 * Also, make sure that the SCSI Reset Interrupt bit has been cleared
796 * after the soft reset has taken place.
797 */
798 spin_lock_irqsave(&ha->hardware_lock, flags);
799 ctrl_status = readw(&ha->reg->ctrl_status);
800 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
801 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
802 readl(&ha->reg->ctrl_status);
803 }
804 spin_unlock_irqrestore(&ha->hardware_lock, flags);
805
806 /* If soft reset fails then most probably the bios on other
807 * function is also enabled.
808 * Since the initialization is sequential the other fn
809 * wont be able to acknowledge the soft reset.
810 * Issue a force soft reset to workaround this scenario.
811 */
812 if (max_wait_time == 0) {
813 /* Issue Force Soft Reset */
814 spin_lock_irqsave(&ha->hardware_lock, flags);
815 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
816 readl(&ha->reg->ctrl_status);
817 spin_unlock_irqrestore(&ha->hardware_lock, flags);
818 /* Wait until the firmware tells us the Soft Reset is done */
819 max_wait_time = SOFT_RESET_TOV;
820 do {
821 spin_lock_irqsave(&ha->hardware_lock, flags);
822 ctrl_status = readw(&ha->reg->ctrl_status);
823 spin_unlock_irqrestore(&ha->hardware_lock, flags);
824
825 if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
826 status = QLA_SUCCESS;
827 break;
828 }
829
830 msleep(1000);
831 } while ((--max_wait_time));
832 }
833
834 return status;
835}
836
837/**
David Somayajuluafaf5a22006-09-19 10:28:00 -0700838 * qla4xxx_flush_active_srbs - returns all outstanding i/o requests to O.S.
839 * @ha: Pointer to host adapter structure.
840 *
841 * This routine is called just prior to a HARD RESET to return all
842 * outstanding commands back to the Operating System.
843 * Caller should make sure that the following locks are released
844 * before this calling routine: Hardware lock, and io_request_lock.
845 **/
846static void qla4xxx_flush_active_srbs(struct scsi_qla_host *ha)
847{
848 struct srb *srb;
849 int i;
850 unsigned long flags;
851
852 spin_lock_irqsave(&ha->hardware_lock, flags);
853 for (i = 0; i < ha->host->can_queue; i++) {
854 srb = qla4xxx_del_from_active_array(ha, i);
855 if (srb != NULL) {
856 srb->cmd->result = DID_RESET << 16;
857 qla4xxx_srb_compl(ha, srb);
858 }
859 }
860 spin_unlock_irqrestore(&ha->hardware_lock, flags);
861
862}
863
864/**
David Somayajuluafaf5a22006-09-19 10:28:00 -0700865 * qla4xxx_recover_adapter - recovers adapter after a fatal error
866 * @ha: Pointer to host adapter structure.
867 * @renew_ddb_list: Indicates what to do with the adapter's ddb list
868 * after adapter recovery has completed.
869 * 0=preserve ddb list, 1=destroy and rebuild ddb list
870 **/
871static int qla4xxx_recover_adapter(struct scsi_qla_host *ha,
872 uint8_t renew_ddb_list)
873{
874 int status;
875
876 /* Stall incoming I/O until we are done */
877 clear_bit(AF_ONLINE, &ha->flags);
878 DEBUG2(printk("scsi%ld: %s calling qla4xxx_cmd_wait\n", ha->host_no,
879 __func__));
880
881 /* Wait for outstanding commands to complete.
882 * Stalls the driver for max 30 secs
883 */
884 status = qla4xxx_cmd_wait(ha);
885
886 qla4xxx_disable_intrs(ha);
887
888 /* Flush any pending ddb changed AENs */
889 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
890
891 /* Reset the firmware. If successful, function
892 * returns with ISP interrupts enabled.
893 */
894 if (status == QLA_SUCCESS) {
895 DEBUG2(printk("scsi%ld: %s - Performing soft reset..\n",
896 ha->host_no, __func__));
David C Somayajuluf26b9042006-11-15 16:41:09 -0800897 qla4xxx_flush_active_srbs(ha);
898 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS)
899 status = qla4xxx_soft_reset(ha);
900 else
901 status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700902 }
903
904 /* Flush any pending ddb changed AENs */
905 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
906
907 /* Re-initialize firmware. If successful, function returns
908 * with ISP interrupts enabled */
909 if (status == QLA_SUCCESS) {
910 DEBUG2(printk("scsi%ld: %s - Initializing adapter..\n",
911 ha->host_no, __func__));
912
913 /* If successful, AF_ONLINE flag set in
914 * qla4xxx_initialize_adapter */
915 status = qla4xxx_initialize_adapter(ha, renew_ddb_list);
916 }
917
918 /* Failed adapter initialization?
919 * Retry reset_ha only if invoked via DPC (DPC_RESET_HA) */
920 if ((test_bit(AF_ONLINE, &ha->flags) == 0) &&
921 (test_bit(DPC_RESET_HA, &ha->dpc_flags))) {
922 /* Adapter initialization failed, see if we can retry
923 * resetting the ha */
924 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
925 ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
926 DEBUG2(printk("scsi%ld: recover adapter - retrying "
927 "(%d) more times\n", ha->host_no,
928 ha->retry_reset_ha_cnt));
929 set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
930 status = QLA_ERROR;
931 } else {
932 if (ha->retry_reset_ha_cnt > 0) {
933 /* Schedule another Reset HA--DPC will retry */
934 ha->retry_reset_ha_cnt--;
935 DEBUG2(printk("scsi%ld: recover adapter - "
936 "retry remaining %d\n",
937 ha->host_no,
938 ha->retry_reset_ha_cnt));
939 status = QLA_ERROR;
940 }
941
942 if (ha->retry_reset_ha_cnt == 0) {
943 /* Recover adapter retries have been exhausted.
944 * Adapter DEAD */
945 DEBUG2(printk("scsi%ld: recover adapter "
946 "failed - board disabled\n",
947 ha->host_no));
948 qla4xxx_flush_active_srbs(ha);
949 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
950 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
951 clear_bit(DPC_RESET_HA_DESTROY_DDB_LIST,
952 &ha->dpc_flags);
953 status = QLA_ERROR;
954 }
955 }
956 } else {
957 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
958 clear_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags);
959 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
960 }
961
962 ha->adapter_error_count++;
963
964 if (status == QLA_SUCCESS)
965 qla4xxx_enable_intrs(ha);
966
967 DEBUG2(printk("scsi%ld: recover adapter .. DONE\n", ha->host_no));
968 return status;
969}
970
971/**
972 * qla4xxx_do_dpc - dpc routine
973 * @data: in our case pointer to adapter structure
974 *
975 * This routine is a task that is schedule by the interrupt handler
976 * to perform the background processing for interrupts. We put it
977 * on a task queue that is consumed whenever the scheduler runs; that's
978 * so you can do anything (i.e. put the process to sleep etc). In fact,
979 * the mid-level tries to sleep when it reaches the driver threshold
980 * "host->can_queue". This can cause a panic if we were in our interrupt code.
981 **/
David Howellsc4028952006-11-22 14:57:56 +0000982static void qla4xxx_do_dpc(struct work_struct *work)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700983{
David Howellsc4028952006-11-22 14:57:56 +0000984 struct scsi_qla_host *ha =
985 container_of(work, struct scsi_qla_host, dpc_work);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700986 struct ddb_entry *ddb_entry, *dtemp;
David C Somayajulu477ffb92007-01-22 12:26:11 -0800987 int status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700988
David C Somayajuluf26b9042006-11-15 16:41:09 -0800989 DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
David C Somayajulu477ffb92007-01-22 12:26:11 -0800990 "flags = 0x%08lx, dpc_flags = 0x%08lx ctrl_stat = 0x%08x\n",
991 ha->host_no, __func__, ha->flags, ha->dpc_flags,
992 readw(&ha->reg->ctrl_status)));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700993
994 /* Initialization not yet finished. Don't do anything yet. */
995 if (!test_bit(AF_INIT_DONE, &ha->flags))
996 return;
997
998 if (adapter_up(ha) ||
999 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1000 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1001 test_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags)) {
David C Somayajuluf26b9042006-11-15 16:41:09 -08001002 if (test_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags) ||
1003 test_bit(DPC_RESET_HA, &ha->dpc_flags))
David Somayajuluafaf5a22006-09-19 10:28:00 -07001004 qla4xxx_recover_adapter(ha, PRESERVE_DDB_LIST);
1005
David C Somayajulu477ffb92007-01-22 12:26:11 -08001006 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001007 uint8_t wait_time = RESET_INTR_TOV;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001008
David Somayajuluafaf5a22006-09-19 10:28:00 -07001009 while ((readw(&ha->reg->ctrl_status) &
1010 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
1011 if (--wait_time == 0)
1012 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001013 msleep(1000);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001014 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001015 if (wait_time == 0)
1016 DEBUG2(printk("scsi%ld: %s: SR|FSR "
1017 "bit not cleared-- resetting\n",
1018 ha->host_no, __func__));
David C Somayajulu477ffb92007-01-22 12:26:11 -08001019 qla4xxx_flush_active_srbs(ha);
1020 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
1021 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1022 status = qla4xxx_initialize_adapter(ha,
1023 PRESERVE_DDB_LIST);
1024 }
1025 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1026 if (status == QLA_SUCCESS)
1027 qla4xxx_enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001028 }
1029 }
1030
1031 /* ---- process AEN? --- */
1032 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
1033 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
1034
1035 /* ---- Get DHCP IP Address? --- */
1036 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
1037 qla4xxx_get_dhcp_ip_address(ha);
1038
1039 /* ---- relogin device? --- */
1040 if (adapter_up(ha) &&
1041 test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
1042 list_for_each_entry_safe(ddb_entry, dtemp,
1043 &ha->ddb_list, list) {
1044 if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
1045 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
1046 qla4xxx_relogin_device(ha, ddb_entry);
1047
1048 /*
1049 * If mbx cmd times out there is no point
1050 * in continuing further.
1051 * With large no of targets this can hang
1052 * the system.
1053 */
1054 if (test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
1055 printk(KERN_WARNING "scsi%ld: %s: "
1056 "need to reset hba\n",
1057 ha->host_no, __func__);
1058 break;
1059 }
1060 }
1061 }
1062}
1063
1064/**
1065 * qla4xxx_free_adapter - release the adapter
1066 * @ha: pointer to adapter structure
1067 **/
1068static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
1069{
1070
1071 if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
1072 /* Turn-off interrupts on the card. */
1073 qla4xxx_disable_intrs(ha);
1074 }
1075
1076 /* Kill the kernel thread for this host */
1077 if (ha->dpc_thread)
1078 destroy_workqueue(ha->dpc_thread);
1079
1080 /* Issue Soft Reset to put firmware in unknown state */
David C Somayajuluf26b9042006-11-15 16:41:09 -08001081 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS)
David C Somayajulu477ffb92007-01-22 12:26:11 -08001082 qla4xxx_hw_reset(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001083
1084 /* Remove timer thread, if present */
1085 if (ha->timer_active)
1086 qla4xxx_stop_timer(ha);
1087
David Somayajuluafaf5a22006-09-19 10:28:00 -07001088 /* Detach interrupts */
1089 if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
1090 free_irq(ha->pdev->irq, ha);
1091
David C Somayajulubee4fe82007-05-23 18:03:32 -07001092 /* free extra memory */
1093 qla4xxx_mem_free(ha);
1094
David Somayajuluafaf5a22006-09-19 10:28:00 -07001095 pci_disable_device(ha->pdev);
1096
1097}
1098
1099/***
1100 * qla4xxx_iospace_config - maps registers
1101 * @ha: pointer to adapter structure
1102 *
1103 * This routines maps HBA's registers from the pci address space
1104 * into the kernel virtual address space for memory mapped i/o.
1105 **/
1106static int qla4xxx_iospace_config(struct scsi_qla_host *ha)
1107{
1108 unsigned long pio, pio_len, pio_flags;
1109 unsigned long mmio, mmio_len, mmio_flags;
1110
1111 pio = pci_resource_start(ha->pdev, 0);
1112 pio_len = pci_resource_len(ha->pdev, 0);
1113 pio_flags = pci_resource_flags(ha->pdev, 0);
1114 if (pio_flags & IORESOURCE_IO) {
1115 if (pio_len < MIN_IOBASE_LEN) {
1116 dev_warn(&ha->pdev->dev,
1117 "Invalid PCI I/O region size\n");
1118 pio = 0;
1119 }
1120 } else {
1121 dev_warn(&ha->pdev->dev, "region #0 not a PIO resource\n");
1122 pio = 0;
1123 }
1124
1125 /* Use MMIO operations for all accesses. */
1126 mmio = pci_resource_start(ha->pdev, 1);
1127 mmio_len = pci_resource_len(ha->pdev, 1);
1128 mmio_flags = pci_resource_flags(ha->pdev, 1);
1129
1130 if (!(mmio_flags & IORESOURCE_MEM)) {
1131 dev_err(&ha->pdev->dev,
1132 "region #0 not an MMIO resource, aborting\n");
1133
1134 goto iospace_error_exit;
1135 }
1136 if (mmio_len < MIN_IOBASE_LEN) {
1137 dev_err(&ha->pdev->dev,
1138 "Invalid PCI mem region size, aborting\n");
1139 goto iospace_error_exit;
1140 }
1141
1142 if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
1143 dev_warn(&ha->pdev->dev,
1144 "Failed to reserve PIO/MMIO regions\n");
1145
1146 goto iospace_error_exit;
1147 }
1148
1149 ha->pio_address = pio;
1150 ha->pio_length = pio_len;
1151 ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
1152 if (!ha->reg) {
1153 dev_err(&ha->pdev->dev,
1154 "cannot remap MMIO, aborting\n");
1155
1156 goto iospace_error_exit;
1157 }
1158
1159 return 0;
1160
1161iospace_error_exit:
1162 return -ENOMEM;
1163}
1164
1165/**
1166 * qla4xxx_probe_adapter - callback function to probe HBA
1167 * @pdev: pointer to pci_dev structure
1168 * @pci_device_id: pointer to pci_device entry
1169 *
1170 * This routine will probe for Qlogic 4xxx iSCSI host adapters.
1171 * It returns zero if successful. It also initializes all data necessary for
1172 * the driver.
1173 **/
1174static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
1175 const struct pci_device_id *ent)
1176{
1177 int ret = -ENODEV, status;
1178 struct Scsi_Host *host;
1179 struct scsi_qla_host *ha;
1180 struct ddb_entry *ddb_entry, *ddbtemp;
1181 uint8_t init_retry_count = 0;
1182 char buf[34];
1183
1184 if (pci_enable_device(pdev))
1185 return -1;
1186
1187 host = scsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha));
1188 if (host == NULL) {
1189 printk(KERN_WARNING
1190 "qla4xxx: Couldn't allocate host from scsi layer!\n");
1191 goto probe_disable_device;
1192 }
1193
1194 /* Clear our data area */
1195 ha = (struct scsi_qla_host *) host->hostdata;
1196 memset(ha, 0, sizeof(*ha));
1197
1198 /* Save the information from PCI BIOS. */
1199 ha->pdev = pdev;
1200 ha->host = host;
1201 ha->host_no = host->host_no;
1202
1203 /* Configure PCI I/O space. */
1204 ret = qla4xxx_iospace_config(ha);
1205 if (ret)
1206 goto probe_failed;
1207
1208 dev_info(&ha->pdev->dev, "Found an ISP%04x, irq %d, iobase 0x%p\n",
1209 pdev->device, pdev->irq, ha->reg);
1210
1211 qla4xxx_config_dma_addressing(ha);
1212
1213 /* Initialize lists and spinlocks. */
1214 INIT_LIST_HEAD(&ha->ddb_list);
1215 INIT_LIST_HEAD(&ha->free_srb_q);
1216
1217 mutex_init(&ha->mbox_sem);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001218
1219 spin_lock_init(&ha->hardware_lock);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001220
1221 /* Allocate dma buffers */
1222 if (qla4xxx_mem_alloc(ha)) {
1223 dev_warn(&ha->pdev->dev,
1224 "[ERROR] Failed to allocate memory for adapter\n");
1225
1226 ret = -ENOMEM;
1227 goto probe_failed;
1228 }
1229
1230 /*
1231 * Initialize the Host adapter request/response queues and
1232 * firmware
1233 * NOTE: interrupts enabled upon successful completion
1234 */
1235 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1236 while (status == QLA_ERROR && init_retry_count++ < MAX_INIT_RETRIES) {
1237 DEBUG2(printk("scsi: %s: retrying adapter initialization "
1238 "(%d)\n", __func__, init_retry_count));
1239 qla4xxx_soft_reset(ha);
1240 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1241 }
1242 if (status == QLA_ERROR) {
1243 dev_warn(&ha->pdev->dev, "Failed to initialize adapter\n");
1244
1245 ret = -ENODEV;
1246 goto probe_failed;
1247 }
1248
1249 host->cmd_per_lun = 3;
1250 host->max_channel = 0;
1251 host->max_lun = MAX_LUNS - 1;
1252 host->max_id = MAX_TARGETS;
1253 host->max_cmd_len = IOCB_MAX_CDB_LEN;
1254 host->can_queue = MAX_SRBS ;
1255 host->transportt = qla4xxx_scsi_transport;
1256
1257 ret = scsi_init_shared_tag_map(host, MAX_SRBS);
1258 if (ret) {
1259 dev_warn(&ha->pdev->dev, "scsi_init_shared_tag_map failed");
1260 goto probe_failed;
1261 }
1262
1263 /* Startup the kernel thread for this host adapter. */
1264 DEBUG2(printk("scsi: %s: Starting kernel thread for "
1265 "qla4xxx_dpc\n", __func__));
1266 sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
1267 ha->dpc_thread = create_singlethread_workqueue(buf);
1268 if (!ha->dpc_thread) {
1269 dev_warn(&ha->pdev->dev, "Unable to start DPC thread!\n");
1270 ret = -ENODEV;
1271 goto probe_failed;
1272 }
David Howellsc4028952006-11-22 14:57:56 +00001273 INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001274
1275 ret = request_irq(pdev->irq, qla4xxx_intr_handler,
Thomas Gleixner38515e92007-02-14 00:33:16 -08001276 IRQF_DISABLED | IRQF_SHARED, "qla4xxx", ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001277 if (ret) {
1278 dev_warn(&ha->pdev->dev, "Failed to reserve interrupt %d"
1279 " already in use.\n", pdev->irq);
1280 goto probe_failed;
1281 }
1282 set_bit(AF_IRQ_ATTACHED, &ha->flags);
1283 host->irq = pdev->irq;
1284 DEBUG(printk("scsi%d: irq %d attached\n", ha->host_no, ha->pdev->irq));
1285
1286 qla4xxx_enable_intrs(ha);
1287
1288 /* Start timer thread. */
1289 qla4xxx_start_timer(ha, qla4xxx_timer, 1);
1290
1291 set_bit(AF_INIT_DONE, &ha->flags);
1292
1293 pci_set_drvdata(pdev, ha);
1294
1295 ret = scsi_add_host(host, &pdev->dev);
1296 if (ret)
1297 goto probe_failed;
1298
1299 /* Update transport device information for all devices. */
1300 list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
1301 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE)
1302 if (qla4xxx_add_sess(ddb_entry))
1303 goto remove_host;
1304 }
1305
1306 printk(KERN_INFO
1307 " QLogic iSCSI HBA Driver version: %s\n"
1308 " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
1309 qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
1310 ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
1311 ha->patch_number, ha->build_number);
1312
1313 return 0;
1314
1315remove_host:
1316 qla4xxx_free_ddb_list(ha);
1317 scsi_remove_host(host);
1318
1319probe_failed:
1320 qla4xxx_free_adapter(ha);
1321 scsi_host_put(ha->host);
1322
1323probe_disable_device:
1324 pci_disable_device(pdev);
1325
1326 return ret;
1327}
1328
1329/**
1330 * qla4xxx_remove_adapter - calback function to remove adapter.
1331 * @pci_dev: PCI device pointer
1332 **/
1333static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
1334{
1335 struct scsi_qla_host *ha;
1336
1337 ha = pci_get_drvdata(pdev);
1338
David C Somayajulubee4fe82007-05-23 18:03:32 -07001339 qla4xxx_disable_intrs(ha);
1340
1341 while (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags))
1342 ssleep(1);
1343
David Somayajuluafaf5a22006-09-19 10:28:00 -07001344 /* remove devs from iscsi_sessions to scsi_devices */
1345 qla4xxx_free_ddb_list(ha);
1346
1347 scsi_remove_host(ha->host);
1348
1349 qla4xxx_free_adapter(ha);
1350
1351 scsi_host_put(ha->host);
1352
1353 pci_set_drvdata(pdev, NULL);
1354}
1355
1356/**
1357 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
1358 * @ha: HA context
1359 *
1360 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
1361 * supported addressing method.
1362 */
Adrian Bunk47975472007-04-26 00:35:16 -07001363static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001364{
1365 int retval;
1366
1367 /* Update our PCI device dma_mask for full 64 bit mask */
1368 if (pci_set_dma_mask(ha->pdev, DMA_64BIT_MASK) == 0) {
1369 if (pci_set_consistent_dma_mask(ha->pdev, DMA_64BIT_MASK)) {
1370 dev_dbg(&ha->pdev->dev,
1371 "Failed to set 64 bit PCI consistent mask; "
1372 "using 32 bit.\n");
1373 retval = pci_set_consistent_dma_mask(ha->pdev,
1374 DMA_32BIT_MASK);
1375 }
1376 } else
1377 retval = pci_set_dma_mask(ha->pdev, DMA_32BIT_MASK);
1378}
1379
1380static int qla4xxx_slave_alloc(struct scsi_device *sdev)
1381{
1382 struct iscsi_cls_session *sess = starget_to_session(sdev->sdev_target);
1383 struct ddb_entry *ddb = sess->dd_data;
1384
1385 sdev->hostdata = ddb;
1386 sdev->tagged_supported = 1;
1387 scsi_activate_tcq(sdev, sdev->host->can_queue);
1388 return 0;
1389}
1390
1391static int qla4xxx_slave_configure(struct scsi_device *sdev)
1392{
1393 sdev->tagged_supported = 1;
1394 return 0;
1395}
1396
1397static void qla4xxx_slave_destroy(struct scsi_device *sdev)
1398{
1399 scsi_deactivate_tcq(sdev, 1);
1400}
1401
1402/**
1403 * qla4xxx_del_from_active_array - returns an active srb
1404 * @ha: Pointer to host adapter structure.
1405 * @index: index into to the active_array
1406 *
1407 * This routine removes and returns the srb at the specified index
1408 **/
1409struct srb * qla4xxx_del_from_active_array(struct scsi_qla_host *ha, uint32_t index)
1410{
1411 struct srb *srb = NULL;
1412 struct scsi_cmnd *cmd;
1413
1414 if (!(cmd = scsi_host_find_tag(ha->host, index)))
1415 return srb;
1416
1417 if (!(srb = (struct srb *)cmd->host_scribble))
1418 return srb;
1419
1420 /* update counters */
1421 if (srb->flags & SRB_DMA_VALID) {
1422 ha->req_q_count += srb->iocb_cnt;
1423 ha->iocb_cnt -= srb->iocb_cnt;
1424 if (srb->cmd)
1425 srb->cmd->host_scribble = NULL;
1426 }
1427 return srb;
1428}
1429
1430/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07001431 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
1432 * @ha: actual ha whose done queue will contain the comd returned by firmware.
1433 * @cmd: Scsi Command to wait on.
1434 *
1435 * This routine waits for the command to be returned by the Firmware
1436 * for some max time.
1437 **/
1438static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
1439 struct scsi_cmnd *cmd)
1440{
1441 int done = 0;
1442 struct srb *rp;
1443 uint32_t max_wait_time = EH_WAIT_CMD_TOV;
1444
1445 do {
1446 /* Checking to see if its returned to OS */
1447 rp = (struct srb *) cmd->SCp.ptr;
1448 if (rp == NULL) {
1449 done++;
1450 break;
1451 }
1452
1453 msleep(2000);
1454 } while (max_wait_time--);
1455
1456 return done;
1457}
1458
1459/**
1460 * qla4xxx_wait_for_hba_online - waits for HBA to come online
1461 * @ha: Pointer to host adapter structure
1462 **/
1463static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
1464{
1465 unsigned long wait_online;
1466
1467 wait_online = jiffies + (30 * HZ);
1468 while (time_before(jiffies, wait_online)) {
1469
1470 if (adapter_up(ha))
1471 return QLA_SUCCESS;
1472 else if (ha->retry_reset_ha_cnt == 0)
1473 return QLA_ERROR;
1474
1475 msleep(2000);
1476 }
1477
1478 return QLA_ERROR;
1479}
1480
1481/**
1482 * qla4xxx_eh_wait_for_active_target_commands - wait for active cmds to finish.
1483 * @ha: pointer to to HBA
1484 * @t: target id
1485 * @l: lun id
1486 *
1487 * This function waits for all outstanding commands to a lun to complete. It
1488 * returns 0 if all pending commands are returned and 1 otherwise.
1489 **/
1490static int qla4xxx_eh_wait_for_active_target_commands(struct scsi_qla_host *ha,
1491 int t, int l)
1492{
1493 int cnt;
1494 int status = 0;
1495 struct scsi_cmnd *cmd;
1496
1497 /*
1498 * Waiting for all commands for the designated target in the active
1499 * array
1500 */
1501 for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
1502 cmd = scsi_host_find_tag(ha->host, cnt);
1503 if (cmd && cmd->device->id == t && cmd->device->lun == l) {
1504 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
1505 status++;
1506 break;
1507 }
1508 }
1509 }
1510 return status;
1511}
1512
1513/**
1514 * qla4xxx_eh_device_reset - callback for target reset.
1515 * @cmd: Pointer to Linux's SCSI command structure
1516 *
1517 * This routine is called by the Linux OS to reset all luns on the
1518 * specified target.
1519 **/
1520static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
1521{
1522 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
1523 struct ddb_entry *ddb_entry = cmd->device->hostdata;
1524 struct srb *sp;
1525 int ret = FAILED, stat;
1526
1527 sp = (struct srb *) cmd->SCp.ptr;
1528 if (!sp || !ddb_entry)
1529 return ret;
1530
1531 dev_info(&ha->pdev->dev,
1532 "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
1533 cmd->device->channel, cmd->device->id, cmd->device->lun);
1534
1535 DEBUG2(printk(KERN_INFO
1536 "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
1537 "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
1538 cmd, jiffies, cmd->timeout_per_command / HZ,
1539 ha->dpc_flags, cmd->result, cmd->allowed));
1540
1541 /* FIXME: wait for hba to go online */
1542 stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
1543 if (stat != QLA_SUCCESS) {
1544 dev_info(&ha->pdev->dev, "DEVICE RESET FAILED. %d\n", stat);
1545 goto eh_dev_reset_done;
1546 }
1547
1548 /* Send marker. */
1549 ha->marker_needed = 1;
1550
1551 /*
1552 * If we are coming down the EH path, wait for all commands to complete
1553 * for the device.
1554 */
1555 if (cmd->device->host->shost_state == SHOST_RECOVERY) {
1556 if (qla4xxx_eh_wait_for_active_target_commands(ha,
1557 cmd->device->id,
1558 cmd->device->lun)){
1559 dev_info(&ha->pdev->dev,
1560 "DEVICE RESET FAILED - waiting for "
1561 "commands.\n");
1562 goto eh_dev_reset_done;
1563 }
1564 }
1565
1566 dev_info(&ha->pdev->dev,
1567 "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
1568 ha->host_no, cmd->device->channel, cmd->device->id,
1569 cmd->device->lun);
1570
1571 ret = SUCCESS;
1572
1573eh_dev_reset_done:
1574
1575 return ret;
1576}
1577
1578/**
1579 * qla4xxx_eh_host_reset - kernel callback
1580 * @cmd: Pointer to Linux's SCSI command structure
1581 *
1582 * This routine is invoked by the Linux kernel to perform fatal error
1583 * recovery on the specified adapter.
1584 **/
1585static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
1586{
1587 int return_status = FAILED;
1588 struct scsi_qla_host *ha;
1589
1590 ha = (struct scsi_qla_host *) cmd->device->host->hostdata;
1591
1592 dev_info(&ha->pdev->dev,
1593 "scsi(%ld:%d:%d:%d): ADAPTER RESET ISSUED.\n", ha->host_no,
1594 cmd->device->channel, cmd->device->id, cmd->device->lun);
1595
1596 if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
1597 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
1598 "DEAD.\n", ha->host_no, cmd->device->channel,
1599 __func__));
1600
1601 return FAILED;
1602 }
1603
1604 if (qla4xxx_recover_adapter(ha, PRESERVE_DDB_LIST) == QLA_SUCCESS) {
1605 return_status = SUCCESS;
1606 }
1607
1608 dev_info(&ha->pdev->dev, "HOST RESET %s.\n",
1609 return_status == FAILED ? "FAILED" : "SUCCEDED");
1610
1611 return return_status;
1612}
1613
1614
1615static struct pci_device_id qla4xxx_pci_tbl[] = {
1616 {
1617 .vendor = PCI_VENDOR_ID_QLOGIC,
1618 .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
1619 .subvendor = PCI_ANY_ID,
1620 .subdevice = PCI_ANY_ID,
1621 },
1622 {
1623 .vendor = PCI_VENDOR_ID_QLOGIC,
1624 .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
1625 .subvendor = PCI_ANY_ID,
1626 .subdevice = PCI_ANY_ID,
1627 },
David C Somayajulud9150582006-11-15 17:38:40 -08001628 {
1629 .vendor = PCI_VENDOR_ID_QLOGIC,
1630 .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
1631 .subvendor = PCI_ANY_ID,
1632 .subdevice = PCI_ANY_ID,
1633 },
David Somayajuluafaf5a22006-09-19 10:28:00 -07001634 {0, 0},
1635};
1636MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
1637
Adrian Bunk47975472007-04-26 00:35:16 -07001638static struct pci_driver qla4xxx_pci_driver = {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001639 .name = DRIVER_NAME,
1640 .id_table = qla4xxx_pci_tbl,
1641 .probe = qla4xxx_probe_adapter,
1642 .remove = qla4xxx_remove_adapter,
1643};
1644
1645static int __init qla4xxx_module_init(void)
1646{
1647 int ret;
1648
1649 /* Allocate cache for SRBs. */
1650 srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
1651 SLAB_HWCACHE_ALIGN, NULL, NULL);
1652 if (srb_cachep == NULL) {
1653 printk(KERN_ERR
1654 "%s: Unable to allocate SRB cache..."
1655 "Failing load!\n", DRIVER_NAME);
1656 ret = -ENOMEM;
1657 goto no_srp_cache;
1658 }
1659
1660 /* Derive version string. */
1661 strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
Andrew Vasquez11010fe2006-10-06 09:54:59 -07001662 if (ql4xextended_error_logging)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001663 strcat(qla4xxx_version_str, "-debug");
1664
1665 qla4xxx_scsi_transport =
1666 iscsi_register_transport(&qla4xxx_iscsi_transport);
1667 if (!qla4xxx_scsi_transport){
1668 ret = -ENODEV;
1669 goto release_srb_cache;
1670 }
1671
David Somayajuluafaf5a22006-09-19 10:28:00 -07001672 ret = pci_register_driver(&qla4xxx_pci_driver);
1673 if (ret)
1674 goto unregister_transport;
1675
1676 printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
1677 return 0;
Doug Maxey5ae16db2006-10-05 23:50:07 -05001678
David Somayajuluafaf5a22006-09-19 10:28:00 -07001679unregister_transport:
1680 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
1681release_srb_cache:
1682 kmem_cache_destroy(srb_cachep);
1683no_srp_cache:
1684 return ret;
1685}
1686
1687static void __exit qla4xxx_module_exit(void)
1688{
David C Somayajulu477ffb92007-01-22 12:26:11 -08001689 ql4_mod_unload = 1;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001690 pci_unregister_driver(&qla4xxx_pci_driver);
1691 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
1692 kmem_cache_destroy(srb_cachep);
1693}
1694
1695module_init(qla4xxx_module_init);
1696module_exit(qla4xxx_module_exit);
1697
1698MODULE_AUTHOR("QLogic Corporation");
1699MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
1700MODULE_LICENSE("GPL");
1701MODULE_VERSION(QLA4XXX_DRIVER_VERSION);