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