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