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