blob: 2ca43f0ebcd68f9a76b222474b5384965a5004ab [file] [log] [blame]
David Somayajuluafaf5a22006-09-19 10:28:00 -07001/*
2 * QLogic iSCSI HBA Driver
3 * Copyright (c) 2003-2006 QLogic Corporation
4 *
5 * See LICENSE.qla4xxx for copyright and licensing details.
6 */
7#include <linux/moduleparam.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
David Somayajuluafaf5a22006-09-19 10:28:00 -07009
10#include <scsi/scsi_tcq.h>
11#include <scsi/scsicam.h>
12
13#include "ql4_def.h"
David C Somayajulubee4fe82007-05-23 18:03:32 -070014#include "ql4_version.h"
15#include "ql4_glbl.h"
16#include "ql4_dbg.h"
17#include "ql4_inline.h"
David Somayajuluafaf5a22006-09-19 10:28:00 -070018
19/*
20 * Driver version
21 */
Adrian Bunk47975472007-04-26 00:35:16 -070022static char qla4xxx_version_str[40];
David Somayajuluafaf5a22006-09-19 10:28:00 -070023
24/*
25 * SRB allocation cache
26 */
Christoph Lametere18b8902006-12-06 20:33:20 -080027static struct kmem_cache *srb_cachep;
David Somayajuluafaf5a22006-09-19 10:28:00 -070028
29/*
30 * Module parameter information and variables
31 */
32int ql4xdiscoverywait = 60;
33module_param(ql4xdiscoverywait, int, S_IRUGO | S_IRUSR);
34MODULE_PARM_DESC(ql4xdiscoverywait, "Discovery wait time");
35int ql4xdontresethba = 0;
36module_param(ql4xdontresethba, int, S_IRUGO | S_IRUSR);
37MODULE_PARM_DESC(ql4xdontresethba,
38 "Dont reset the HBA when the driver gets 0x8002 AEN "
39 " default it will reset hba :0"
40 " set to 1 to avoid resetting HBA");
41
Andrew Vasquez11010fe2006-10-06 09:54:59 -070042int ql4xextended_error_logging = 0; /* 0 = off, 1 = log errors */
43module_param(ql4xextended_error_logging, int, S_IRUGO | S_IRUSR);
44MODULE_PARM_DESC(ql4xextended_error_logging,
David Somayajuluafaf5a22006-09-19 10:28:00 -070045 "Option to enable extended error logging, "
46 "Default is 0 - no logging, 1 - debug logging");
47
David C Somayajulu477ffb92007-01-22 12:26:11 -080048int ql4_mod_unload = 0;
49
Mike Christied510d962008-07-11 19:50:33 -050050#define QL4_DEF_QDEPTH 32
51
David Somayajuluafaf5a22006-09-19 10:28:00 -070052/*
53 * SCSI host template entry points
54 */
Adrian Bunk47975472007-04-26 00:35:16 -070055static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -070056
57/*
58 * iSCSI template entry points
59 */
Mike Christie2174a042007-05-30 12:57:10 -050060static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
61 enum iscsi_tgt_dscvr type, uint32_t enable,
62 struct sockaddr *dst_addr);
David Somayajuluafaf5a22006-09-19 10:28:00 -070063static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
64 enum iscsi_param param, char *buf);
65static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
66 enum iscsi_param param, char *buf);
Mike Christieaa1e93a2007-05-30 12:57:09 -050067static int qla4xxx_host_get_param(struct Scsi_Host *shost,
68 enum iscsi_host_param param, char *buf);
David Somayajuluafaf5a22006-09-19 10:28:00 -070069static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session);
Mike Christie5c656af2009-07-15 15:02:59 -050070static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
David Somayajuluafaf5a22006-09-19 10:28:00 -070071
72/*
73 * SCSI host template entry points
74 */
75static int qla4xxx_queuecommand(struct scsi_cmnd *cmd,
76 void (*done) (struct scsi_cmnd *));
77static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
Mike Christiece545032008-02-29 18:25:20 -060078static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -070079static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
80static int qla4xxx_slave_alloc(struct scsi_device *device);
81static int qla4xxx_slave_configure(struct scsi_device *device);
82static void qla4xxx_slave_destroy(struct scsi_device *sdev);
Mike Christie024f8012008-03-04 13:26:54 -060083static void qla4xxx_scan_start(struct Scsi_Host *shost);
David Somayajuluafaf5a22006-09-19 10:28:00 -070084
85static struct scsi_host_template qla4xxx_driver_template = {
86 .module = THIS_MODULE,
87 .name = DRIVER_NAME,
88 .proc_name = DRIVER_NAME,
89 .queuecommand = qla4xxx_queuecommand,
90
91 .eh_device_reset_handler = qla4xxx_eh_device_reset,
Mike Christiece545032008-02-29 18:25:20 -060092 .eh_target_reset_handler = qla4xxx_eh_target_reset,
David Somayajuluafaf5a22006-09-19 10:28:00 -070093 .eh_host_reset_handler = qla4xxx_eh_host_reset,
Mike Christie5c656af2009-07-15 15:02:59 -050094 .eh_timed_out = qla4xxx_eh_cmd_timed_out,
David Somayajuluafaf5a22006-09-19 10:28:00 -070095
96 .slave_configure = qla4xxx_slave_configure,
97 .slave_alloc = qla4xxx_slave_alloc,
98 .slave_destroy = qla4xxx_slave_destroy,
99
Mike Christie921601b2008-01-31 13:36:49 -0600100 .scan_finished = iscsi_scan_finished,
Mike Christie024f8012008-03-04 13:26:54 -0600101 .scan_start = qla4xxx_scan_start,
Mike Christie921601b2008-01-31 13:36:49 -0600102
David Somayajuluafaf5a22006-09-19 10:28:00 -0700103 .this_id = -1,
104 .cmd_per_lun = 3,
105 .use_clustering = ENABLE_CLUSTERING,
106 .sg_tablesize = SG_ALL,
107
108 .max_sectors = 0xFFFF,
109};
110
111static struct iscsi_transport qla4xxx_iscsi_transport = {
112 .owner = THIS_MODULE,
113 .name = DRIVER_NAME,
Mike Christied8196ed2007-05-30 12:57:25 -0500114 .caps = CAP_FW_DB | CAP_SENDTARGETS_OFFLOAD |
115 CAP_DATA_PATH_OFFLOAD,
Mike Christieaa1e93a2007-05-30 12:57:09 -0500116 .param_mask = ISCSI_CONN_PORT | ISCSI_CONN_ADDRESS |
117 ISCSI_TARGET_NAME | ISCSI_TPGT,
Mike Christie8ad57812007-05-30 12:57:13 -0500118 .host_param_mask = ISCSI_HOST_HWADDRESS |
Mike Christie22236962007-05-30 12:57:24 -0500119 ISCSI_HOST_IPADDRESS |
Mike Christie8ad57812007-05-30 12:57:13 -0500120 ISCSI_HOST_INITIATOR_NAME,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700121 .tgt_dscvr = qla4xxx_tgt_dscvr,
122 .get_conn_param = qla4xxx_conn_get_param,
123 .get_session_param = qla4xxx_sess_get_param,
Mike Christieaa1e93a2007-05-30 12:57:09 -0500124 .get_host_param = qla4xxx_host_get_param,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700125 .session_recovery_timedout = qla4xxx_recovery_timedout,
126};
127
128static struct scsi_transport_template *qla4xxx_scsi_transport;
129
Mike Christie5c656af2009-07-15 15:02:59 -0500130static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
131{
132 struct iscsi_cls_session *session;
133 struct ddb_entry *ddb_entry;
134
135 session = starget_to_session(scsi_target(sc->device));
136 ddb_entry = session->dd_data;
137
138 /* if we are not logged in then the LLD is going to clean up the cmd */
139 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
140 return BLK_EH_RESET_TIMER;
141 else
142 return BLK_EH_NOT_HANDLED;
143}
144
David Somayajuluafaf5a22006-09-19 10:28:00 -0700145static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session)
146{
147 struct ddb_entry *ddb_entry = session->dd_data;
148 struct scsi_qla_host *ha = ddb_entry->ha;
149
Mike Christie568d3032008-01-31 13:36:47 -0600150 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
151 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700152
Mike Christie568d3032008-01-31 13:36:47 -0600153 DEBUG2(printk("scsi%ld: %s: index [%d] port down retry count "
154 "of (%d) secs exhausted, marking device DEAD.\n",
155 ha->host_no, __func__, ddb_entry->fw_ddb_index,
156 ha->port_down_retry_count));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700157
Mike Christie568d3032008-01-31 13:36:47 -0600158 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine - dpc "
159 "flags = 0x%lx\n",
160 ha->host_no, __func__, ha->dpc_flags));
161 queue_work(ha->dpc_thread, &ha->dpc_work);
162 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700163}
164
Mike Christieaa1e93a2007-05-30 12:57:09 -0500165static int qla4xxx_host_get_param(struct Scsi_Host *shost,
166 enum iscsi_host_param param, char *buf)
167{
168 struct scsi_qla_host *ha = to_qla_host(shost);
169 int len;
170
171 switch (param) {
172 case ISCSI_HOST_PARAM_HWADDRESS:
Michael Chan7ffc49a2007-12-24 21:28:09 -0800173 len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
Mike Christieaa1e93a2007-05-30 12:57:09 -0500174 break;
Mike Christie22236962007-05-30 12:57:24 -0500175 case ISCSI_HOST_PARAM_IPADDRESS:
176 len = sprintf(buf, "%d.%d.%d.%d\n", ha->ip_address[0],
177 ha->ip_address[1], ha->ip_address[2],
178 ha->ip_address[3]);
179 break;
Mike Christie8ad57812007-05-30 12:57:13 -0500180 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie22236962007-05-30 12:57:24 -0500181 len = sprintf(buf, "%s\n", ha->name_string);
Mike Christie8ad57812007-05-30 12:57:13 -0500182 break;
Mike Christieaa1e93a2007-05-30 12:57:09 -0500183 default:
184 return -ENOSYS;
185 }
186
187 return len;
188}
189
David Somayajuluafaf5a22006-09-19 10:28:00 -0700190static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
191 enum iscsi_param param, char *buf)
192{
193 struct ddb_entry *ddb_entry = sess->dd_data;
194 int len;
195
196 switch (param) {
197 case ISCSI_PARAM_TARGET_NAME:
198 len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
199 ddb_entry->iscsi_name);
200 break;
201 case ISCSI_PARAM_TPGT:
202 len = sprintf(buf, "%u\n", ddb_entry->tpgt);
203 break;
204 default:
205 return -ENOSYS;
206 }
207
208 return len;
209}
210
211static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
212 enum iscsi_param param, char *buf)
213{
214 struct iscsi_cls_session *session;
215 struct ddb_entry *ddb_entry;
216 int len;
217
218 session = iscsi_dev_to_session(conn->dev.parent);
219 ddb_entry = session->dd_data;
220
221 switch (param) {
222 case ISCSI_PARAM_CONN_PORT:
223 len = sprintf(buf, "%hu\n", ddb_entry->port);
224 break;
225 case ISCSI_PARAM_CONN_ADDRESS:
226 /* TODO: what are the ipv6 bits */
Harvey Harrison63779432008-10-31 00:56:00 -0700227 len = sprintf(buf, "%pI4\n", &ddb_entry->ip_addr);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700228 break;
229 default:
230 return -ENOSYS;
231 }
232
233 return len;
234}
235
Mike Christie2174a042007-05-30 12:57:10 -0500236static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
237 enum iscsi_tgt_dscvr type, uint32_t enable,
238 struct sockaddr *dst_addr)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700239{
240 struct scsi_qla_host *ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700241 struct sockaddr_in *addr;
242 struct sockaddr_in6 *addr6;
243 int ret = 0;
244
David Somayajuluafaf5a22006-09-19 10:28:00 -0700245 ha = (struct scsi_qla_host *) shost->hostdata;
246
247 switch (type) {
248 case ISCSI_TGT_DSCVR_SEND_TARGETS:
249 if (dst_addr->sa_family == AF_INET) {
250 addr = (struct sockaddr_in *)dst_addr;
251 if (qla4xxx_send_tgts(ha, (char *)&addr->sin_addr,
252 addr->sin_port) != QLA_SUCCESS)
253 ret = -EIO;
254 } else if (dst_addr->sa_family == AF_INET6) {
255 /*
256 * TODO: fix qla4xxx_send_tgts
257 */
258 addr6 = (struct sockaddr_in6 *)dst_addr;
259 if (qla4xxx_send_tgts(ha, (char *)&addr6->sin6_addr,
260 addr6->sin6_port) != QLA_SUCCESS)
261 ret = -EIO;
262 } else
263 ret = -ENOSYS;
264 break;
265 default:
266 ret = -ENOSYS;
267 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700268 return ret;
269}
270
271void qla4xxx_destroy_sess(struct ddb_entry *ddb_entry)
272{
273 if (!ddb_entry->sess)
274 return;
275
276 if (ddb_entry->conn) {
Mike Christie26974782007-12-13 12:43:29 -0600277 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700278 iscsi_remove_session(ddb_entry->sess);
279 }
280 iscsi_free_session(ddb_entry->sess);
281}
282
283int qla4xxx_add_sess(struct ddb_entry *ddb_entry)
284{
285 int err;
286
Mike Christie26974782007-12-13 12:43:29 -0600287 ddb_entry->sess->recovery_tmo = ddb_entry->ha->port_down_retry_count;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700288 err = iscsi_add_session(ddb_entry->sess, ddb_entry->fw_ddb_index);
289 if (err) {
290 DEBUG2(printk(KERN_ERR "Could not add session.\n"));
291 return err;
292 }
293
Mike Christie5d91e202008-05-21 15:54:01 -0500294 ddb_entry->conn = iscsi_create_conn(ddb_entry->sess, 0, 0);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700295 if (!ddb_entry->conn) {
296 iscsi_remove_session(ddb_entry->sess);
297 DEBUG2(printk(KERN_ERR "Could not add connection.\n"));
298 return -ENOMEM;
299 }
Mike Christieb6359302008-01-31 13:36:44 -0600300
301 /* finally ready to go */
302 iscsi_unblock_session(ddb_entry->sess);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700303 return 0;
304}
305
306struct ddb_entry *qla4xxx_alloc_sess(struct scsi_qla_host *ha)
307{
308 struct ddb_entry *ddb_entry;
309 struct iscsi_cls_session *sess;
310
Mike Christie5d91e202008-05-21 15:54:01 -0500311 sess = iscsi_alloc_session(ha->host, &qla4xxx_iscsi_transport,
312 sizeof(struct ddb_entry));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700313 if (!sess)
314 return NULL;
315
316 ddb_entry = sess->dd_data;
317 memset(ddb_entry, 0, sizeof(*ddb_entry));
318 ddb_entry->ha = ha;
319 ddb_entry->sess = sess;
320 return ddb_entry;
321}
322
Mike Christie024f8012008-03-04 13:26:54 -0600323static void qla4xxx_scan_start(struct Scsi_Host *shost)
324{
325 struct scsi_qla_host *ha = shost_priv(shost);
326 struct ddb_entry *ddb_entry, *ddbtemp;
327
328 /* finish setup of sessions that were already setup in firmware */
329 list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
330 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE)
331 qla4xxx_add_sess(ddb_entry);
332 }
333}
334
David Somayajuluafaf5a22006-09-19 10:28:00 -0700335/*
336 * Timer routines
337 */
338
339static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
340 unsigned long interval)
341{
342 DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
343 __func__, ha->host->host_no));
344 init_timer(&ha->timer);
345 ha->timer.expires = jiffies + interval * HZ;
346 ha->timer.data = (unsigned long)ha;
347 ha->timer.function = (void (*)(unsigned long))func;
348 add_timer(&ha->timer);
349 ha->timer_active = 1;
350}
351
352static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
353{
354 del_timer_sync(&ha->timer);
355 ha->timer_active = 0;
356}
357
358/***
359 * qla4xxx_mark_device_missing - mark a device as missing.
360 * @ha: Pointer to host adapter structure.
361 * @ddb_entry: Pointer to device database entry
362 *
363 * This routine marks a device missing and resets the relogin retry count.
364 **/
365void qla4xxx_mark_device_missing(struct scsi_qla_host *ha,
366 struct ddb_entry *ddb_entry)
367{
368 atomic_set(&ddb_entry->state, DDB_STATE_MISSING);
369 DEBUG3(printk("scsi%d:%d:%d: index [%d] marked MISSING\n",
370 ha->host_no, ddb_entry->bus, ddb_entry->target,
371 ddb_entry->fw_ddb_index));
Mike Christieb6359302008-01-31 13:36:44 -0600372 iscsi_block_session(ddb_entry->sess);
Mike Christiee5bd7b52008-09-24 11:46:10 -0500373 iscsi_conn_error_event(ddb_entry->conn, ISCSI_ERR_CONN_FAILED);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700374}
375
376static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
377 struct ddb_entry *ddb_entry,
378 struct scsi_cmnd *cmd,
379 void (*done)(struct scsi_cmnd *))
380{
381 struct srb *srb;
382
383 srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
384 if (!srb)
385 return srb;
386
387 atomic_set(&srb->ref_count, 1);
388 srb->ha = ha;
389 srb->ddb = ddb_entry;
390 srb->cmd = cmd;
391 srb->flags = 0;
Vikas Chaudhary53698872010-04-28 11:41:59 +0530392 CMD_SP(cmd) = (void *)srb;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700393 cmd->scsi_done = done;
394
395 return srb;
396}
397
398static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
399{
400 struct scsi_cmnd *cmd = srb->cmd;
401
402 if (srb->flags & SRB_DMA_VALID) {
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +0900403 scsi_dma_unmap(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700404 srb->flags &= ~SRB_DMA_VALID;
405 }
Vikas Chaudhary53698872010-04-28 11:41:59 +0530406 CMD_SP(cmd) = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700407}
408
409void qla4xxx_srb_compl(struct scsi_qla_host *ha, struct srb *srb)
410{
411 struct scsi_cmnd *cmd = srb->cmd;
412
413 qla4xxx_srb_free_dma(ha, srb);
414
415 mempool_free(srb, ha->srb_mempool);
416
417 cmd->scsi_done(cmd);
418}
419
420/**
421 * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
422 * @cmd: Pointer to Linux's SCSI command structure
423 * @done_fn: Function that the driver calls to notify the SCSI mid-layer
424 * that the command has been processed.
425 *
426 * Remarks:
427 * This routine is invoked by Linux to send a SCSI command to the driver.
428 * The mid-level driver tries to ensure that queuecommand never gets
429 * invoked concurrently with itself or the interrupt handler (although
430 * the interrupt handler may call this routine as part of request-
431 * completion handling). Unfortunely, it sometimes calls the scheduler
432 * in interrupt context which is a big NO! NO!.
433 **/
434static int qla4xxx_queuecommand(struct scsi_cmnd *cmd,
435 void (*done)(struct scsi_cmnd *))
436{
437 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
438 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christie7fb19212008-01-31 13:36:45 -0600439 struct iscsi_cls_session *sess = ddb_entry->sess;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700440 struct srb *srb;
441 int rval;
442
Mike Christie7fb19212008-01-31 13:36:45 -0600443 if (!sess) {
444 cmd->result = DID_IMM_RETRY << 16;
445 goto qc_fail_command;
446 }
447
448 rval = iscsi_session_chkready(sess);
449 if (rval) {
450 cmd->result = rval;
451 goto qc_fail_command;
452 }
453
David Somayajuluafaf5a22006-09-19 10:28:00 -0700454 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
455 if (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD) {
456 cmd->result = DID_NO_CONNECT << 16;
457 goto qc_fail_command;
458 }
Mike Christiec5e98e92008-08-17 15:24:39 -0500459 return SCSI_MLQUEUE_TARGET_BUSY;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700460 }
461
David C Somayajulu477ffb92007-01-22 12:26:11 -0800462 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags))
463 goto qc_host_busy;
464
David Somayajuluafaf5a22006-09-19 10:28:00 -0700465 spin_unlock_irq(ha->host->host_lock);
466
467 srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd, done);
468 if (!srb)
469 goto qc_host_busy_lock;
470
471 rval = qla4xxx_send_command_to_isp(ha, srb);
472 if (rval != QLA_SUCCESS)
473 goto qc_host_busy_free_sp;
474
475 spin_lock_irq(ha->host->host_lock);
476 return 0;
477
478qc_host_busy_free_sp:
479 qla4xxx_srb_free_dma(ha, srb);
480 mempool_free(srb, ha->srb_mempool);
481
482qc_host_busy_lock:
483 spin_lock_irq(ha->host->host_lock);
484
485qc_host_busy:
486 return SCSI_MLQUEUE_HOST_BUSY;
487
488qc_fail_command:
489 done(cmd);
490
491 return 0;
492}
493
494/**
495 * qla4xxx_mem_free - frees memory allocated to adapter
496 * @ha: Pointer to host adapter structure.
497 *
498 * Frees memory previously allocated by qla4xxx_mem_alloc
499 **/
500static void qla4xxx_mem_free(struct scsi_qla_host *ha)
501{
502 if (ha->queues)
503 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
504 ha->queues_dma);
505
506 ha->queues_len = 0;
507 ha->queues = NULL;
508 ha->queues_dma = 0;
509 ha->request_ring = NULL;
510 ha->request_dma = 0;
511 ha->response_ring = NULL;
512 ha->response_dma = 0;
513 ha->shadow_regs = NULL;
514 ha->shadow_regs_dma = 0;
515
516 /* Free srb pool. */
517 if (ha->srb_mempool)
518 mempool_destroy(ha->srb_mempool);
519
520 ha->srb_mempool = NULL;
521
522 /* release io space registers */
523 if (ha->reg)
524 iounmap(ha->reg);
525 pci_release_regions(ha->pdev);
526}
527
528/**
529 * qla4xxx_mem_alloc - allocates memory for use by adapter.
530 * @ha: Pointer to host adapter structure
531 *
532 * Allocates DMA memory for request and response queues. Also allocates memory
533 * for srbs.
534 **/
535static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
536{
537 unsigned long align;
538
539 /* Allocate contiguous block of DMA memory for queues. */
540 ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
541 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
542 sizeof(struct shadow_regs) +
543 MEM_ALIGN_VALUE +
544 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
545 ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
546 &ha->queues_dma, GFP_KERNEL);
547 if (ha->queues == NULL) {
548 dev_warn(&ha->pdev->dev,
549 "Memory Allocation failed - queues.\n");
550
551 goto mem_alloc_error_exit;
552 }
553 memset(ha->queues, 0, ha->queues_len);
554
555 /*
556 * As per RISC alignment requirements -- the bus-address must be a
557 * multiple of the request-ring size (in bytes).
558 */
559 align = 0;
560 if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
561 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
562 (MEM_ALIGN_VALUE - 1));
563
564 /* Update request and response queue pointers. */
565 ha->request_dma = ha->queues_dma + align;
566 ha->request_ring = (struct queue_entry *) (ha->queues + align);
567 ha->response_dma = ha->queues_dma + align +
568 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
569 ha->response_ring = (struct queue_entry *) (ha->queues + align +
570 (REQUEST_QUEUE_DEPTH *
571 QUEUE_SIZE));
572 ha->shadow_regs_dma = ha->queues_dma + align +
573 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
574 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
575 ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
576 (REQUEST_QUEUE_DEPTH *
577 QUEUE_SIZE) +
578 (RESPONSE_QUEUE_DEPTH *
579 QUEUE_SIZE));
580
581 /* Allocate memory for srb pool. */
582 ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
583 mempool_free_slab, srb_cachep);
584 if (ha->srb_mempool == NULL) {
585 dev_warn(&ha->pdev->dev,
586 "Memory Allocation failed - SRB Pool.\n");
587
588 goto mem_alloc_error_exit;
589 }
590
591 return QLA_SUCCESS;
592
593mem_alloc_error_exit:
594 qla4xxx_mem_free(ha);
595 return QLA_ERROR;
596}
597
598/**
599 * qla4xxx_timer - checks every second for work to do.
600 * @ha: Pointer to host adapter structure.
601 **/
602static void qla4xxx_timer(struct scsi_qla_host *ha)
603{
604 struct ddb_entry *ddb_entry, *dtemp;
605 int start_dpc = 0;
606
607 /* Search for relogin's to time-out and port down retry. */
608 list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
609 /* Count down time between sending relogins */
610 if (adapter_up(ha) &&
611 !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
612 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
613 if (atomic_read(&ddb_entry->retry_relogin_timer) !=
614 INVALID_ENTRY) {
615 if (atomic_read(&ddb_entry->retry_relogin_timer)
616 == 0) {
617 atomic_set(&ddb_entry->
618 retry_relogin_timer,
619 INVALID_ENTRY);
620 set_bit(DPC_RELOGIN_DEVICE,
621 &ha->dpc_flags);
622 set_bit(DF_RELOGIN, &ddb_entry->flags);
623 DEBUG2(printk("scsi%ld: %s: index [%d]"
624 " login device\n",
625 ha->host_no, __func__,
626 ddb_entry->fw_ddb_index));
627 } else
628 atomic_dec(&ddb_entry->
629 retry_relogin_timer);
630 }
631 }
632
633 /* Wait for relogin to timeout */
634 if (atomic_read(&ddb_entry->relogin_timer) &&
635 (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
636 /*
637 * If the relogin times out and the device is
638 * still NOT ONLINE then try and relogin again.
639 */
640 if (atomic_read(&ddb_entry->state) !=
641 DDB_STATE_ONLINE &&
642 ddb_entry->fw_ddb_device_state ==
643 DDB_DS_SESSION_FAILED) {
644 /* Reset retry relogin timer */
645 atomic_inc(&ddb_entry->relogin_retry_count);
646 DEBUG2(printk("scsi%ld: index[%d] relogin"
647 " timed out-retrying"
648 " relogin (%d)\n",
649 ha->host_no,
650 ddb_entry->fw_ddb_index,
651 atomic_read(&ddb_entry->
652 relogin_retry_count))
653 );
654 start_dpc++;
655 DEBUG(printk("scsi%ld:%d:%d: index [%d] "
656 "initate relogin after"
657 " %d seconds\n",
658 ha->host_no, ddb_entry->bus,
659 ddb_entry->target,
660 ddb_entry->fw_ddb_index,
661 ddb_entry->default_time2wait + 4)
662 );
663
664 atomic_set(&ddb_entry->retry_relogin_timer,
665 ddb_entry->default_time2wait + 4);
666 }
667 }
668 }
669
670 /* Check for heartbeat interval. */
671 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
672 ha->heartbeat_interval != 0) {
673 ha->seconds_since_last_heartbeat++;
674 if (ha->seconds_since_last_heartbeat >
675 ha->heartbeat_interval + 2)
676 set_bit(DPC_RESET_HA, &ha->dpc_flags);
677 }
678
679
680 /* Wakeup the dpc routine for this adapter, if needed. */
681 if ((start_dpc ||
682 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
683 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
684 test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
685 test_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags) ||
686 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
687 test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +0530688 test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -0700689 test_bit(DPC_AEN, &ha->dpc_flags)) &&
690 ha->dpc_thread) {
691 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
692 " - dpc flags = 0x%lx\n",
693 ha->host_no, __func__, ha->dpc_flags));
694 queue_work(ha->dpc_thread, &ha->dpc_work);
695 }
696
697 /* Reschedule timer thread to call us back in one second */
698 mod_timer(&ha->timer, jiffies + HZ);
699
700 DEBUG2(ha->seconds_since_last_intr++);
701}
702
703/**
704 * qla4xxx_cmd_wait - waits for all outstanding commands to complete
705 * @ha: Pointer to host adapter structure.
706 *
707 * This routine stalls the driver until all outstanding commands are returned.
708 * Caller must release the Hardware Lock prior to calling this routine.
709 **/
710static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
711{
712 uint32_t index = 0;
713 int stat = QLA_SUCCESS;
714 unsigned long flags;
715 struct scsi_cmnd *cmd;
716 int wait_cnt = WAIT_CMD_TOV; /*
717 * Initialized for 30 seconds as we
718 * expect all commands to retuned
719 * ASAP.
720 */
721
722 while (wait_cnt) {
723 spin_lock_irqsave(&ha->hardware_lock, flags);
724 /* Find a command that hasn't completed. */
725 for (index = 0; index < ha->host->can_queue; index++) {
726 cmd = scsi_host_find_tag(ha->host, index);
727 if (cmd != NULL)
728 break;
729 }
730 spin_unlock_irqrestore(&ha->hardware_lock, flags);
731
732 /* If No Commands are pending, wait is complete */
733 if (index == ha->host->can_queue) {
734 break;
735 }
736
737 /* If we timed out on waiting for commands to come back
738 * return ERROR.
739 */
740 wait_cnt--;
741 if (wait_cnt == 0)
742 stat = QLA_ERROR;
743 else {
744 msleep(1000);
745 }
746 } /* End of While (wait_cnt) */
747
748 return stat;
749}
750
David C Somayajulubee4fe82007-05-23 18:03:32 -0700751void qla4xxx_hw_reset(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700752{
David Somayajuluafaf5a22006-09-19 10:28:00 -0700753 uint32_t ctrl_status;
David C Somayajulu477ffb92007-01-22 12:26:11 -0800754 unsigned long flags = 0;
755
756 DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700757
758 spin_lock_irqsave(&ha->hardware_lock, flags);
759
760 /*
761 * If the SCSI Reset Interrupt bit is set, clear it.
762 * Otherwise, the Soft Reset won't work.
763 */
764 ctrl_status = readw(&ha->reg->ctrl_status);
765 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
766 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
767
768 /* Issue Soft Reset */
769 writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
770 readl(&ha->reg->ctrl_status);
771
772 spin_unlock_irqrestore(&ha->hardware_lock, flags);
David C Somayajulu477ffb92007-01-22 12:26:11 -0800773}
774
775/**
776 * qla4xxx_soft_reset - performs soft reset.
777 * @ha: Pointer to host adapter structure.
778 **/
779int qla4xxx_soft_reset(struct scsi_qla_host *ha)
780{
781 uint32_t max_wait_time;
782 unsigned long flags = 0;
783 int status = QLA_ERROR;
784 uint32_t ctrl_status;
785
786 qla4xxx_hw_reset(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700787
788 /* Wait until the Network Reset Intr bit is cleared */
789 max_wait_time = RESET_INTR_TOV;
790 do {
791 spin_lock_irqsave(&ha->hardware_lock, flags);
792 ctrl_status = readw(&ha->reg->ctrl_status);
793 spin_unlock_irqrestore(&ha->hardware_lock, flags);
794
795 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
796 break;
797
798 msleep(1000);
799 } while ((--max_wait_time));
800
801 if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
802 DEBUG2(printk(KERN_WARNING
803 "scsi%ld: Network Reset Intr not cleared by "
804 "Network function, clearing it now!\n",
805 ha->host_no));
806 spin_lock_irqsave(&ha->hardware_lock, flags);
807 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
808 readl(&ha->reg->ctrl_status);
809 spin_unlock_irqrestore(&ha->hardware_lock, flags);
810 }
811
812 /* Wait until the firmware tells us the Soft Reset is done */
813 max_wait_time = SOFT_RESET_TOV;
814 do {
815 spin_lock_irqsave(&ha->hardware_lock, flags);
816 ctrl_status = readw(&ha->reg->ctrl_status);
817 spin_unlock_irqrestore(&ha->hardware_lock, flags);
818
819 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
820 status = QLA_SUCCESS;
821 break;
822 }
823
824 msleep(1000);
825 } while ((--max_wait_time));
826
827 /*
828 * Also, make sure that the SCSI Reset Interrupt bit has been cleared
829 * after the soft reset has taken place.
830 */
831 spin_lock_irqsave(&ha->hardware_lock, flags);
832 ctrl_status = readw(&ha->reg->ctrl_status);
833 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
834 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
835 readl(&ha->reg->ctrl_status);
836 }
837 spin_unlock_irqrestore(&ha->hardware_lock, flags);
838
839 /* If soft reset fails then most probably the bios on other
840 * function is also enabled.
841 * Since the initialization is sequential the other fn
842 * wont be able to acknowledge the soft reset.
843 * Issue a force soft reset to workaround this scenario.
844 */
845 if (max_wait_time == 0) {
846 /* Issue Force Soft Reset */
847 spin_lock_irqsave(&ha->hardware_lock, flags);
848 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
849 readl(&ha->reg->ctrl_status);
850 spin_unlock_irqrestore(&ha->hardware_lock, flags);
851 /* Wait until the firmware tells us the Soft Reset is done */
852 max_wait_time = SOFT_RESET_TOV;
853 do {
854 spin_lock_irqsave(&ha->hardware_lock, flags);
855 ctrl_status = readw(&ha->reg->ctrl_status);
856 spin_unlock_irqrestore(&ha->hardware_lock, flags);
857
858 if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
859 status = QLA_SUCCESS;
860 break;
861 }
862
863 msleep(1000);
864 } while ((--max_wait_time));
865 }
866
867 return status;
868}
869
870/**
David Somayajuluafaf5a22006-09-19 10:28:00 -0700871 * qla4xxx_flush_active_srbs - returns all outstanding i/o requests to O.S.
872 * @ha: Pointer to host adapter structure.
873 *
874 * This routine is called just prior to a HARD RESET to return all
875 * outstanding commands back to the Operating System.
876 * Caller should make sure that the following locks are released
877 * before this calling routine: Hardware lock, and io_request_lock.
878 **/
879static void qla4xxx_flush_active_srbs(struct scsi_qla_host *ha)
880{
881 struct srb *srb;
882 int i;
883 unsigned long flags;
884
885 spin_lock_irqsave(&ha->hardware_lock, flags);
886 for (i = 0; i < ha->host->can_queue; i++) {
887 srb = qla4xxx_del_from_active_array(ha, i);
888 if (srb != NULL) {
889 srb->cmd->result = DID_RESET << 16;
890 qla4xxx_srb_compl(ha, srb);
891 }
892 }
893 spin_unlock_irqrestore(&ha->hardware_lock, flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700894}
895
896/**
David Somayajuluafaf5a22006-09-19 10:28:00 -0700897 * qla4xxx_recover_adapter - recovers adapter after a fatal error
898 * @ha: Pointer to host adapter structure.
899 * @renew_ddb_list: Indicates what to do with the adapter's ddb list
Mike Christie50a29ae2008-03-04 13:26:53 -0600900 *
901 * renew_ddb_list value can be 0=preserve ddb list, 1=destroy and rebuild
902 * ddb list.
David Somayajuluafaf5a22006-09-19 10:28:00 -0700903 **/
904static int qla4xxx_recover_adapter(struct scsi_qla_host *ha,
905 uint8_t renew_ddb_list)
906{
907 int status;
908
909 /* Stall incoming I/O until we are done */
910 clear_bit(AF_ONLINE, &ha->flags);
Mike Christie50a29ae2008-03-04 13:26:53 -0600911
David Somayajuluafaf5a22006-09-19 10:28:00 -0700912 DEBUG2(printk("scsi%ld: %s calling qla4xxx_cmd_wait\n", ha->host_no,
913 __func__));
914
915 /* Wait for outstanding commands to complete.
916 * Stalls the driver for max 30 secs
917 */
918 status = qla4xxx_cmd_wait(ha);
919
920 qla4xxx_disable_intrs(ha);
921
922 /* Flush any pending ddb changed AENs */
923 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
924
Karen Higginsdca05c42009-07-15 15:03:00 -0500925 qla4xxx_flush_active_srbs(ha);
926
David Somayajuluafaf5a22006-09-19 10:28:00 -0700927 /* Reset the firmware. If successful, function
928 * returns with ISP interrupts enabled.
929 */
Karen Higginsdca05c42009-07-15 15:03:00 -0500930 DEBUG2(printk("scsi%ld: %s - Performing soft reset..\n",
931 ha->host_no, __func__));
932 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS)
933 status = qla4xxx_soft_reset(ha);
934 else
935 status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700936
937 /* Flush any pending ddb changed AENs */
938 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
939
940 /* Re-initialize firmware. If successful, function returns
941 * with ISP interrupts enabled */
942 if (status == QLA_SUCCESS) {
943 DEBUG2(printk("scsi%ld: %s - Initializing adapter..\n",
944 ha->host_no, __func__));
945
946 /* If successful, AF_ONLINE flag set in
947 * qla4xxx_initialize_adapter */
948 status = qla4xxx_initialize_adapter(ha, renew_ddb_list);
949 }
950
951 /* Failed adapter initialization?
952 * Retry reset_ha only if invoked via DPC (DPC_RESET_HA) */
953 if ((test_bit(AF_ONLINE, &ha->flags) == 0) &&
954 (test_bit(DPC_RESET_HA, &ha->dpc_flags))) {
955 /* Adapter initialization failed, see if we can retry
956 * resetting the ha */
957 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
958 ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
959 DEBUG2(printk("scsi%ld: recover adapter - retrying "
960 "(%d) more times\n", ha->host_no,
961 ha->retry_reset_ha_cnt));
962 set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
963 status = QLA_ERROR;
964 } else {
965 if (ha->retry_reset_ha_cnt > 0) {
966 /* Schedule another Reset HA--DPC will retry */
967 ha->retry_reset_ha_cnt--;
968 DEBUG2(printk("scsi%ld: recover adapter - "
969 "retry remaining %d\n",
970 ha->host_no,
971 ha->retry_reset_ha_cnt));
972 status = QLA_ERROR;
973 }
974
975 if (ha->retry_reset_ha_cnt == 0) {
976 /* Recover adapter retries have been exhausted.
977 * Adapter DEAD */
978 DEBUG2(printk("scsi%ld: recover adapter "
979 "failed - board disabled\n",
980 ha->host_no));
981 qla4xxx_flush_active_srbs(ha);
982 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
983 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
984 clear_bit(DPC_RESET_HA_DESTROY_DDB_LIST,
985 &ha->dpc_flags);
986 status = QLA_ERROR;
987 }
988 }
989 } else {
990 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
991 clear_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags);
992 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
993 }
994
995 ha->adapter_error_count++;
996
997 if (status == QLA_SUCCESS)
998 qla4xxx_enable_intrs(ha);
999
1000 DEBUG2(printk("scsi%ld: recover adapter .. DONE\n", ha->host_no));
1001 return status;
1002}
1003
1004/**
1005 * qla4xxx_do_dpc - dpc routine
1006 * @data: in our case pointer to adapter structure
1007 *
1008 * This routine is a task that is schedule by the interrupt handler
1009 * to perform the background processing for interrupts. We put it
1010 * on a task queue that is consumed whenever the scheduler runs; that's
1011 * so you can do anything (i.e. put the process to sleep etc). In fact,
1012 * the mid-level tries to sleep when it reaches the driver threshold
1013 * "host->can_queue". This can cause a panic if we were in our interrupt code.
1014 **/
David Howellsc4028952006-11-22 14:57:56 +00001015static void qla4xxx_do_dpc(struct work_struct *work)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001016{
David Howellsc4028952006-11-22 14:57:56 +00001017 struct scsi_qla_host *ha =
1018 container_of(work, struct scsi_qla_host, dpc_work);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001019 struct ddb_entry *ddb_entry, *dtemp;
David C Somayajulu477ffb92007-01-22 12:26:11 -08001020 int status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001021
David C Somayajuluf26b9042006-11-15 16:41:09 -08001022 DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
David C Somayajulu477ffb92007-01-22 12:26:11 -08001023 "flags = 0x%08lx, dpc_flags = 0x%08lx ctrl_stat = 0x%08x\n",
1024 ha->host_no, __func__, ha->flags, ha->dpc_flags,
1025 readw(&ha->reg->ctrl_status)));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001026
1027 /* Initialization not yet finished. Don't do anything yet. */
1028 if (!test_bit(AF_INIT_DONE, &ha->flags))
1029 return;
1030
1031 if (adapter_up(ha) ||
1032 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1033 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1034 test_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags)) {
David C Somayajuluf26b9042006-11-15 16:41:09 -08001035 if (test_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags) ||
1036 test_bit(DPC_RESET_HA, &ha->dpc_flags))
David Somayajuluafaf5a22006-09-19 10:28:00 -07001037 qla4xxx_recover_adapter(ha, PRESERVE_DDB_LIST);
1038
David C Somayajulu477ffb92007-01-22 12:26:11 -08001039 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001040 uint8_t wait_time = RESET_INTR_TOV;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001041
David Somayajuluafaf5a22006-09-19 10:28:00 -07001042 while ((readw(&ha->reg->ctrl_status) &
1043 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
1044 if (--wait_time == 0)
1045 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001046 msleep(1000);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001047 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001048 if (wait_time == 0)
1049 DEBUG2(printk("scsi%ld: %s: SR|FSR "
1050 "bit not cleared-- resetting\n",
1051 ha->host_no, __func__));
David C Somayajulu477ffb92007-01-22 12:26:11 -08001052 qla4xxx_flush_active_srbs(ha);
1053 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
1054 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1055 status = qla4xxx_initialize_adapter(ha,
1056 PRESERVE_DDB_LIST);
1057 }
1058 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1059 if (status == QLA_SUCCESS)
1060 qla4xxx_enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001061 }
1062 }
1063
1064 /* ---- process AEN? --- */
1065 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
1066 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
1067
1068 /* ---- Get DHCP IP Address? --- */
1069 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
1070 qla4xxx_get_dhcp_ip_address(ha);
1071
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301072 /* ---- link change? --- */
1073 if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
1074 if (!test_bit(AF_LINK_UP, &ha->flags)) {
1075 /* ---- link down? --- */
1076 list_for_each_entry_safe(ddb_entry, dtemp,
1077 &ha->ddb_list, list) {
1078 if (atomic_read(&ddb_entry->state) ==
1079 DDB_STATE_ONLINE)
1080 qla4xxx_mark_device_missing(ha,
1081 ddb_entry);
1082 }
1083 } else {
1084 /* ---- link up? --- *
1085 * F/W will auto login to all devices ONLY ONCE after
1086 * link up during driver initialization and runtime
1087 * fatal error recovery. Therefore, the driver must
1088 * manually relogin to devices when recovering from
1089 * connection failures, logouts, expired KATO, etc. */
1090
1091 list_for_each_entry_safe(ddb_entry, dtemp,
1092 &ha->ddb_list, list) {
1093 if ((atomic_read(&ddb_entry->state) ==
1094 DDB_STATE_MISSING) ||
1095 (atomic_read(&ddb_entry->state) ==
1096 DDB_STATE_DEAD)) {
1097 if (ddb_entry->fw_ddb_device_state ==
1098 DDB_DS_SESSION_ACTIVE) {
1099 atomic_set(&ddb_entry->state,
1100 DDB_STATE_ONLINE);
1101 dev_info(&ha->pdev->dev,
1102 "scsi%ld: %s: ddb[%d]"
1103 " os[%d] marked"
1104 " ONLINE\n",
1105 ha->host_no, __func__,
1106 ddb_entry->fw_ddb_index,
1107 ddb_entry->os_target_id);
1108
1109 iscsi_unblock_session(
1110 ddb_entry->sess);
1111 } else
1112 qla4xxx_relogin_device(
1113 ha, ddb_entry);
1114 }
1115
1116 }
1117 }
1118 }
1119
David Somayajuluafaf5a22006-09-19 10:28:00 -07001120 /* ---- relogin device? --- */
1121 if (adapter_up(ha) &&
1122 test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
1123 list_for_each_entry_safe(ddb_entry, dtemp,
1124 &ha->ddb_list, list) {
1125 if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
1126 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
1127 qla4xxx_relogin_device(ha, ddb_entry);
1128
1129 /*
1130 * If mbx cmd times out there is no point
1131 * in continuing further.
1132 * With large no of targets this can hang
1133 * the system.
1134 */
1135 if (test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
1136 printk(KERN_WARNING "scsi%ld: %s: "
1137 "need to reset hba\n",
1138 ha->host_no, __func__);
1139 break;
1140 }
1141 }
1142 }
1143}
1144
1145/**
1146 * qla4xxx_free_adapter - release the adapter
1147 * @ha: pointer to adapter structure
1148 **/
1149static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
1150{
1151
1152 if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
1153 /* Turn-off interrupts on the card. */
1154 qla4xxx_disable_intrs(ha);
1155 }
1156
1157 /* Kill the kernel thread for this host */
1158 if (ha->dpc_thread)
1159 destroy_workqueue(ha->dpc_thread);
1160
1161 /* Issue Soft Reset to put firmware in unknown state */
David C Somayajuluf26b9042006-11-15 16:41:09 -08001162 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS)
David C Somayajulu477ffb92007-01-22 12:26:11 -08001163 qla4xxx_hw_reset(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001164
1165 /* Remove timer thread, if present */
1166 if (ha->timer_active)
1167 qla4xxx_stop_timer(ha);
1168
David Somayajuluafaf5a22006-09-19 10:28:00 -07001169 /* Detach interrupts */
1170 if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
1171 free_irq(ha->pdev->irq, ha);
1172
David C Somayajulubee4fe82007-05-23 18:03:32 -07001173 /* free extra memory */
1174 qla4xxx_mem_free(ha);
1175
David Somayajuluafaf5a22006-09-19 10:28:00 -07001176 pci_disable_device(ha->pdev);
1177
1178}
1179
1180/***
1181 * qla4xxx_iospace_config - maps registers
1182 * @ha: pointer to adapter structure
1183 *
1184 * This routines maps HBA's registers from the pci address space
1185 * into the kernel virtual address space for memory mapped i/o.
1186 **/
1187static int qla4xxx_iospace_config(struct scsi_qla_host *ha)
1188{
1189 unsigned long pio, pio_len, pio_flags;
1190 unsigned long mmio, mmio_len, mmio_flags;
1191
1192 pio = pci_resource_start(ha->pdev, 0);
1193 pio_len = pci_resource_len(ha->pdev, 0);
1194 pio_flags = pci_resource_flags(ha->pdev, 0);
1195 if (pio_flags & IORESOURCE_IO) {
1196 if (pio_len < MIN_IOBASE_LEN) {
1197 dev_warn(&ha->pdev->dev,
1198 "Invalid PCI I/O region size\n");
1199 pio = 0;
1200 }
1201 } else {
1202 dev_warn(&ha->pdev->dev, "region #0 not a PIO resource\n");
1203 pio = 0;
1204 }
1205
1206 /* Use MMIO operations for all accesses. */
1207 mmio = pci_resource_start(ha->pdev, 1);
1208 mmio_len = pci_resource_len(ha->pdev, 1);
1209 mmio_flags = pci_resource_flags(ha->pdev, 1);
1210
1211 if (!(mmio_flags & IORESOURCE_MEM)) {
1212 dev_err(&ha->pdev->dev,
1213 "region #0 not an MMIO resource, aborting\n");
1214
1215 goto iospace_error_exit;
1216 }
1217 if (mmio_len < MIN_IOBASE_LEN) {
1218 dev_err(&ha->pdev->dev,
1219 "Invalid PCI mem region size, aborting\n");
1220 goto iospace_error_exit;
1221 }
1222
1223 if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
1224 dev_warn(&ha->pdev->dev,
1225 "Failed to reserve PIO/MMIO regions\n");
1226
1227 goto iospace_error_exit;
1228 }
1229
1230 ha->pio_address = pio;
1231 ha->pio_length = pio_len;
1232 ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
1233 if (!ha->reg) {
1234 dev_err(&ha->pdev->dev,
1235 "cannot remap MMIO, aborting\n");
1236
1237 goto iospace_error_exit;
1238 }
1239
1240 return 0;
1241
1242iospace_error_exit:
1243 return -ENOMEM;
1244}
1245
1246/**
1247 * qla4xxx_probe_adapter - callback function to probe HBA
1248 * @pdev: pointer to pci_dev structure
1249 * @pci_device_id: pointer to pci_device entry
1250 *
1251 * This routine will probe for Qlogic 4xxx iSCSI host adapters.
1252 * It returns zero if successful. It also initializes all data necessary for
1253 * the driver.
1254 **/
1255static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
1256 const struct pci_device_id *ent)
1257{
1258 int ret = -ENODEV, status;
1259 struct Scsi_Host *host;
1260 struct scsi_qla_host *ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001261 uint8_t init_retry_count = 0;
1262 char buf[34];
1263
1264 if (pci_enable_device(pdev))
1265 return -1;
1266
1267 host = scsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha));
1268 if (host == NULL) {
1269 printk(KERN_WARNING
1270 "qla4xxx: Couldn't allocate host from scsi layer!\n");
1271 goto probe_disable_device;
1272 }
1273
1274 /* Clear our data area */
1275 ha = (struct scsi_qla_host *) host->hostdata;
1276 memset(ha, 0, sizeof(*ha));
1277
1278 /* Save the information from PCI BIOS. */
1279 ha->pdev = pdev;
1280 ha->host = host;
1281 ha->host_no = host->host_no;
1282
1283 /* Configure PCI I/O space. */
1284 ret = qla4xxx_iospace_config(ha);
1285 if (ret)
1286 goto probe_failed;
1287
1288 dev_info(&ha->pdev->dev, "Found an ISP%04x, irq %d, iobase 0x%p\n",
1289 pdev->device, pdev->irq, ha->reg);
1290
1291 qla4xxx_config_dma_addressing(ha);
1292
1293 /* Initialize lists and spinlocks. */
1294 INIT_LIST_HEAD(&ha->ddb_list);
1295 INIT_LIST_HEAD(&ha->free_srb_q);
1296
1297 mutex_init(&ha->mbox_sem);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001298
1299 spin_lock_init(&ha->hardware_lock);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001300
1301 /* Allocate dma buffers */
1302 if (qla4xxx_mem_alloc(ha)) {
1303 dev_warn(&ha->pdev->dev,
1304 "[ERROR] Failed to allocate memory for adapter\n");
1305
1306 ret = -ENOMEM;
1307 goto probe_failed;
1308 }
1309
1310 /*
1311 * Initialize the Host adapter request/response queues and
1312 * firmware
1313 * NOTE: interrupts enabled upon successful completion
1314 */
1315 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1316 while (status == QLA_ERROR && init_retry_count++ < MAX_INIT_RETRIES) {
1317 DEBUG2(printk("scsi: %s: retrying adapter initialization "
1318 "(%d)\n", __func__, init_retry_count));
1319 qla4xxx_soft_reset(ha);
1320 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1321 }
1322 if (status == QLA_ERROR) {
1323 dev_warn(&ha->pdev->dev, "Failed to initialize adapter\n");
1324
1325 ret = -ENODEV;
1326 goto probe_failed;
1327 }
1328
1329 host->cmd_per_lun = 3;
1330 host->max_channel = 0;
1331 host->max_lun = MAX_LUNS - 1;
1332 host->max_id = MAX_TARGETS;
1333 host->max_cmd_len = IOCB_MAX_CDB_LEN;
1334 host->can_queue = MAX_SRBS ;
1335 host->transportt = qla4xxx_scsi_transport;
1336
1337 ret = scsi_init_shared_tag_map(host, MAX_SRBS);
1338 if (ret) {
Joe Perches898eb712007-10-18 03:06:30 -07001339 dev_warn(&ha->pdev->dev, "scsi_init_shared_tag_map failed\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001340 goto probe_failed;
1341 }
1342
1343 /* Startup the kernel thread for this host adapter. */
1344 DEBUG2(printk("scsi: %s: Starting kernel thread for "
1345 "qla4xxx_dpc\n", __func__));
1346 sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
1347 ha->dpc_thread = create_singlethread_workqueue(buf);
1348 if (!ha->dpc_thread) {
1349 dev_warn(&ha->pdev->dev, "Unable to start DPC thread!\n");
1350 ret = -ENODEV;
1351 goto probe_failed;
1352 }
David Howellsc4028952006-11-22 14:57:56 +00001353 INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001354
1355 ret = request_irq(pdev->irq, qla4xxx_intr_handler,
Thomas Gleixner38515e92007-02-14 00:33:16 -08001356 IRQF_DISABLED | IRQF_SHARED, "qla4xxx", ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001357 if (ret) {
1358 dev_warn(&ha->pdev->dev, "Failed to reserve interrupt %d"
1359 " already in use.\n", pdev->irq);
1360 goto probe_failed;
1361 }
1362 set_bit(AF_IRQ_ATTACHED, &ha->flags);
1363 host->irq = pdev->irq;
1364 DEBUG(printk("scsi%d: irq %d attached\n", ha->host_no, ha->pdev->irq));
1365
1366 qla4xxx_enable_intrs(ha);
1367
1368 /* Start timer thread. */
1369 qla4xxx_start_timer(ha, qla4xxx_timer, 1);
1370
1371 set_bit(AF_INIT_DONE, &ha->flags);
1372
1373 pci_set_drvdata(pdev, ha);
1374
1375 ret = scsi_add_host(host, &pdev->dev);
1376 if (ret)
1377 goto probe_failed;
1378
David Somayajuluafaf5a22006-09-19 10:28:00 -07001379 printk(KERN_INFO
1380 " QLogic iSCSI HBA Driver version: %s\n"
1381 " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
1382 qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
1383 ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
1384 ha->patch_number, ha->build_number);
Mike Christie921601b2008-01-31 13:36:49 -06001385 scsi_scan_host(host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001386 return 0;
1387
David Somayajuluafaf5a22006-09-19 10:28:00 -07001388probe_failed:
1389 qla4xxx_free_adapter(ha);
1390 scsi_host_put(ha->host);
1391
1392probe_disable_device:
1393 pci_disable_device(pdev);
1394
1395 return ret;
1396}
1397
1398/**
1399 * qla4xxx_remove_adapter - calback function to remove adapter.
1400 * @pci_dev: PCI device pointer
1401 **/
1402static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
1403{
1404 struct scsi_qla_host *ha;
1405
1406 ha = pci_get_drvdata(pdev);
1407
David C Somayajulubee4fe82007-05-23 18:03:32 -07001408 qla4xxx_disable_intrs(ha);
1409
1410 while (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags))
1411 ssleep(1);
1412
David Somayajuluafaf5a22006-09-19 10:28:00 -07001413 /* remove devs from iscsi_sessions to scsi_devices */
1414 qla4xxx_free_ddb_list(ha);
1415
1416 scsi_remove_host(ha->host);
1417
1418 qla4xxx_free_adapter(ha);
1419
1420 scsi_host_put(ha->host);
1421
1422 pci_set_drvdata(pdev, NULL);
1423}
1424
1425/**
1426 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
1427 * @ha: HA context
1428 *
1429 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
1430 * supported addressing method.
1431 */
Adrian Bunk47975472007-04-26 00:35:16 -07001432static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001433{
1434 int retval;
1435
1436 /* Update our PCI device dma_mask for full 64 bit mask */
Yang Hongyang6a355282009-04-06 19:01:13 -07001437 if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
1438 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001439 dev_dbg(&ha->pdev->dev,
1440 "Failed to set 64 bit PCI consistent mask; "
1441 "using 32 bit.\n");
1442 retval = pci_set_consistent_dma_mask(ha->pdev,
Yang Hongyang284901a2009-04-06 19:01:15 -07001443 DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001444 }
1445 } else
Yang Hongyang284901a2009-04-06 19:01:15 -07001446 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001447}
1448
1449static int qla4xxx_slave_alloc(struct scsi_device *sdev)
1450{
1451 struct iscsi_cls_session *sess = starget_to_session(sdev->sdev_target);
1452 struct ddb_entry *ddb = sess->dd_data;
1453
1454 sdev->hostdata = ddb;
1455 sdev->tagged_supported = 1;
Mike Christied510d962008-07-11 19:50:33 -05001456 scsi_activate_tcq(sdev, QL4_DEF_QDEPTH);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001457 return 0;
1458}
1459
1460static int qla4xxx_slave_configure(struct scsi_device *sdev)
1461{
1462 sdev->tagged_supported = 1;
1463 return 0;
1464}
1465
1466static void qla4xxx_slave_destroy(struct scsi_device *sdev)
1467{
1468 scsi_deactivate_tcq(sdev, 1);
1469}
1470
1471/**
1472 * qla4xxx_del_from_active_array - returns an active srb
1473 * @ha: Pointer to host adapter structure.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001474 * @index: index into the active_array
David Somayajuluafaf5a22006-09-19 10:28:00 -07001475 *
1476 * This routine removes and returns the srb at the specified index
1477 **/
1478struct srb * qla4xxx_del_from_active_array(struct scsi_qla_host *ha, uint32_t index)
1479{
1480 struct srb *srb = NULL;
Vikas Chaudhary53698872010-04-28 11:41:59 +05301481 struct scsi_cmnd *cmd = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001482
Vikas Chaudhary53698872010-04-28 11:41:59 +05301483 cmd = scsi_host_find_tag(ha->host, index);
1484 if (!cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001485 return srb;
1486
Vikas Chaudhary53698872010-04-28 11:41:59 +05301487 srb = (struct srb *)CMD_SP(cmd);
1488 if (!srb)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001489 return srb;
1490
1491 /* update counters */
1492 if (srb->flags & SRB_DMA_VALID) {
1493 ha->req_q_count += srb->iocb_cnt;
1494 ha->iocb_cnt -= srb->iocb_cnt;
1495 if (srb->cmd)
Vikas Chaudhary53698872010-04-28 11:41:59 +05301496 srb->cmd->host_scribble =
1497 (unsigned char *)(unsigned long) MAX_SRBS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001498 }
1499 return srb;
1500}
1501
1502/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07001503 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
1504 * @ha: actual ha whose done queue will contain the comd returned by firmware.
1505 * @cmd: Scsi Command to wait on.
1506 *
1507 * This routine waits for the command to be returned by the Firmware
1508 * for some max time.
1509 **/
1510static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
1511 struct scsi_cmnd *cmd)
1512{
1513 int done = 0;
1514 struct srb *rp;
1515 uint32_t max_wait_time = EH_WAIT_CMD_TOV;
1516
1517 do {
1518 /* Checking to see if its returned to OS */
Vikas Chaudhary53698872010-04-28 11:41:59 +05301519 rp = (struct srb *) CMD_SP(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001520 if (rp == NULL) {
1521 done++;
1522 break;
1523 }
1524
1525 msleep(2000);
1526 } while (max_wait_time--);
1527
1528 return done;
1529}
1530
1531/**
1532 * qla4xxx_wait_for_hba_online - waits for HBA to come online
1533 * @ha: Pointer to host adapter structure
1534 **/
1535static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
1536{
1537 unsigned long wait_online;
1538
1539 wait_online = jiffies + (30 * HZ);
1540 while (time_before(jiffies, wait_online)) {
1541
1542 if (adapter_up(ha))
1543 return QLA_SUCCESS;
1544 else if (ha->retry_reset_ha_cnt == 0)
1545 return QLA_ERROR;
1546
1547 msleep(2000);
1548 }
1549
1550 return QLA_ERROR;
1551}
1552
1553/**
Mike Christiece545032008-02-29 18:25:20 -06001554 * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001555 * @ha: pointer to HBA
David Somayajuluafaf5a22006-09-19 10:28:00 -07001556 * @t: target id
1557 * @l: lun id
1558 *
1559 * This function waits for all outstanding commands to a lun to complete. It
1560 * returns 0 if all pending commands are returned and 1 otherwise.
1561 **/
Mike Christiece545032008-02-29 18:25:20 -06001562static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
1563 struct scsi_target *stgt,
1564 struct scsi_device *sdev)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001565{
1566 int cnt;
1567 int status = 0;
1568 struct scsi_cmnd *cmd;
1569
1570 /*
Mike Christiece545032008-02-29 18:25:20 -06001571 * Waiting for all commands for the designated target or dev
1572 * in the active array
David Somayajuluafaf5a22006-09-19 10:28:00 -07001573 */
1574 for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
1575 cmd = scsi_host_find_tag(ha->host, cnt);
Mike Christiece545032008-02-29 18:25:20 -06001576 if (cmd && stgt == scsi_target(cmd->device) &&
1577 (!sdev || sdev == cmd->device)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001578 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
1579 status++;
1580 break;
1581 }
1582 }
1583 }
1584 return status;
1585}
1586
1587/**
1588 * qla4xxx_eh_device_reset - callback for target reset.
1589 * @cmd: Pointer to Linux's SCSI command structure
1590 *
1591 * This routine is called by the Linux OS to reset all luns on the
1592 * specified target.
1593 **/
1594static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
1595{
1596 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
1597 struct ddb_entry *ddb_entry = cmd->device->hostdata;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001598 int ret = FAILED, stat;
1599
Karen Higgins612f73482009-07-15 15:03:01 -05001600 if (!ddb_entry)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001601 return ret;
1602
1603 dev_info(&ha->pdev->dev,
1604 "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
1605 cmd->device->channel, cmd->device->id, cmd->device->lun);
1606
1607 DEBUG2(printk(KERN_INFO
1608 "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
1609 "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
Jens Axboe242f9dc2008-09-14 05:55:09 -07001610 cmd, jiffies, cmd->request->timeout / HZ,
David Somayajuluafaf5a22006-09-19 10:28:00 -07001611 ha->dpc_flags, cmd->result, cmd->allowed));
1612
1613 /* FIXME: wait for hba to go online */
1614 stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
1615 if (stat != QLA_SUCCESS) {
1616 dev_info(&ha->pdev->dev, "DEVICE RESET FAILED. %d\n", stat);
1617 goto eh_dev_reset_done;
1618 }
1619
Mike Christiece545032008-02-29 18:25:20 -06001620 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
1621 cmd->device)) {
1622 dev_info(&ha->pdev->dev,
1623 "DEVICE RESET FAILED - waiting for "
1624 "commands.\n");
1625 goto eh_dev_reset_done;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001626 }
1627
David C Somayajulu9d562912008-03-19 11:23:03 -07001628 /* Send marker. */
1629 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
1630 MM_LUN_RESET) != QLA_SUCCESS)
1631 goto eh_dev_reset_done;
1632
David Somayajuluafaf5a22006-09-19 10:28:00 -07001633 dev_info(&ha->pdev->dev,
1634 "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
1635 ha->host_no, cmd->device->channel, cmd->device->id,
1636 cmd->device->lun);
1637
1638 ret = SUCCESS;
1639
1640eh_dev_reset_done:
1641
1642 return ret;
1643}
1644
1645/**
Mike Christiece545032008-02-29 18:25:20 -06001646 * qla4xxx_eh_target_reset - callback for target reset.
1647 * @cmd: Pointer to Linux's SCSI command structure
1648 *
1649 * This routine is called by the Linux OS to reset the target.
1650 **/
1651static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
1652{
1653 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
1654 struct ddb_entry *ddb_entry = cmd->device->hostdata;
1655 int stat;
1656
1657 if (!ddb_entry)
1658 return FAILED;
1659
1660 starget_printk(KERN_INFO, scsi_target(cmd->device),
1661 "WARM TARGET RESET ISSUED.\n");
1662
1663 DEBUG2(printk(KERN_INFO
1664 "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
1665 "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
Jens Axboe242f9dc2008-09-14 05:55:09 -07001666 ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
Mike Christiece545032008-02-29 18:25:20 -06001667 ha->dpc_flags, cmd->result, cmd->allowed));
1668
1669 stat = qla4xxx_reset_target(ha, ddb_entry);
1670 if (stat != QLA_SUCCESS) {
1671 starget_printk(KERN_INFO, scsi_target(cmd->device),
1672 "WARM TARGET RESET FAILED.\n");
1673 return FAILED;
1674 }
1675
Mike Christiece545032008-02-29 18:25:20 -06001676 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
1677 NULL)) {
1678 starget_printk(KERN_INFO, scsi_target(cmd->device),
1679 "WARM TARGET DEVICE RESET FAILED - "
1680 "waiting for commands.\n");
1681 return FAILED;
1682 }
1683
David C Somayajulu9d562912008-03-19 11:23:03 -07001684 /* Send marker. */
1685 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
1686 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
1687 starget_printk(KERN_INFO, scsi_target(cmd->device),
1688 "WARM TARGET DEVICE RESET FAILED - "
1689 "marker iocb failed.\n");
1690 return FAILED;
1691 }
1692
Mike Christiece545032008-02-29 18:25:20 -06001693 starget_printk(KERN_INFO, scsi_target(cmd->device),
1694 "WARM TARGET RESET SUCCEEDED.\n");
1695 return SUCCESS;
1696}
1697
1698/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07001699 * qla4xxx_eh_host_reset - kernel callback
1700 * @cmd: Pointer to Linux's SCSI command structure
1701 *
1702 * This routine is invoked by the Linux kernel to perform fatal error
1703 * recovery on the specified adapter.
1704 **/
1705static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
1706{
1707 int return_status = FAILED;
1708 struct scsi_qla_host *ha;
1709
1710 ha = (struct scsi_qla_host *) cmd->device->host->hostdata;
1711
1712 dev_info(&ha->pdev->dev,
Karen Higginsdca05c42009-07-15 15:03:00 -05001713 "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
David Somayajuluafaf5a22006-09-19 10:28:00 -07001714 cmd->device->channel, cmd->device->id, cmd->device->lun);
1715
1716 if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
1717 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
1718 "DEAD.\n", ha->host_no, cmd->device->channel,
1719 __func__));
1720
1721 return FAILED;
1722 }
1723
Mike Christie50a29ae2008-03-04 13:26:53 -06001724 /* make sure the dpc thread is stopped while we reset the hba */
1725 clear_bit(AF_ONLINE, &ha->flags);
1726 flush_workqueue(ha->dpc_thread);
1727
1728 if (qla4xxx_recover_adapter(ha, PRESERVE_DDB_LIST) == QLA_SUCCESS)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001729 return_status = SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001730
1731 dev_info(&ha->pdev->dev, "HOST RESET %s.\n",
1732 return_status == FAILED ? "FAILED" : "SUCCEDED");
1733
1734 return return_status;
1735}
1736
1737
1738static struct pci_device_id qla4xxx_pci_tbl[] = {
1739 {
1740 .vendor = PCI_VENDOR_ID_QLOGIC,
1741 .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
1742 .subvendor = PCI_ANY_ID,
1743 .subdevice = PCI_ANY_ID,
1744 },
1745 {
1746 .vendor = PCI_VENDOR_ID_QLOGIC,
1747 .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
1748 .subvendor = PCI_ANY_ID,
1749 .subdevice = PCI_ANY_ID,
1750 },
David C Somayajulud9150582006-11-15 17:38:40 -08001751 {
1752 .vendor = PCI_VENDOR_ID_QLOGIC,
1753 .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
1754 .subvendor = PCI_ANY_ID,
1755 .subdevice = PCI_ANY_ID,
1756 },
David Somayajuluafaf5a22006-09-19 10:28:00 -07001757 {0, 0},
1758};
1759MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
1760
Adrian Bunk47975472007-04-26 00:35:16 -07001761static struct pci_driver qla4xxx_pci_driver = {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001762 .name = DRIVER_NAME,
1763 .id_table = qla4xxx_pci_tbl,
1764 .probe = qla4xxx_probe_adapter,
1765 .remove = qla4xxx_remove_adapter,
1766};
1767
1768static int __init qla4xxx_module_init(void)
1769{
1770 int ret;
1771
1772 /* Allocate cache for SRBs. */
1773 srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09001774 SLAB_HWCACHE_ALIGN, NULL);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001775 if (srb_cachep == NULL) {
1776 printk(KERN_ERR
1777 "%s: Unable to allocate SRB cache..."
1778 "Failing load!\n", DRIVER_NAME);
1779 ret = -ENOMEM;
1780 goto no_srp_cache;
1781 }
1782
1783 /* Derive version string. */
1784 strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
Andrew Vasquez11010fe2006-10-06 09:54:59 -07001785 if (ql4xextended_error_logging)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001786 strcat(qla4xxx_version_str, "-debug");
1787
1788 qla4xxx_scsi_transport =
1789 iscsi_register_transport(&qla4xxx_iscsi_transport);
1790 if (!qla4xxx_scsi_transport){
1791 ret = -ENODEV;
1792 goto release_srb_cache;
1793 }
1794
David Somayajuluafaf5a22006-09-19 10:28:00 -07001795 ret = pci_register_driver(&qla4xxx_pci_driver);
1796 if (ret)
1797 goto unregister_transport;
1798
1799 printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
1800 return 0;
Doug Maxey5ae16db2006-10-05 23:50:07 -05001801
David Somayajuluafaf5a22006-09-19 10:28:00 -07001802unregister_transport:
1803 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
1804release_srb_cache:
1805 kmem_cache_destroy(srb_cachep);
1806no_srp_cache:
1807 return ret;
1808}
1809
1810static void __exit qla4xxx_module_exit(void)
1811{
David C Somayajulu477ffb92007-01-22 12:26:11 -08001812 ql4_mod_unload = 1;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001813 pci_unregister_driver(&qla4xxx_pci_driver);
1814 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
1815 kmem_cache_destroy(srb_cachep);
1816}
1817
1818module_init(qla4xxx_module_init);
1819module_exit(qla4xxx_module_exit);
1820
1821MODULE_AUTHOR("QLogic Corporation");
1822MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
1823MODULE_LICENSE("GPL");
1824MODULE_VERSION(QLA4XXX_DRIVER_VERSION);