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