blob: d6c8b429a67518edf41eab8a60f28af3c444ffda [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;
392 cmd->SCp.ptr = (void *)srb;
393 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 }
406 cmd->SCp.ptr = NULL;
407}
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);
894
895}
896
897/**
David Somayajuluafaf5a22006-09-19 10:28:00 -0700898 * qla4xxx_recover_adapter - recovers adapter after a fatal error
899 * @ha: Pointer to host adapter structure.
900 * @renew_ddb_list: Indicates what to do with the adapter's ddb list
Mike Christie50a29ae2008-03-04 13:26:53 -0600901 *
902 * renew_ddb_list value can be 0=preserve ddb list, 1=destroy and rebuild
903 * ddb list.
David Somayajuluafaf5a22006-09-19 10:28:00 -0700904 **/
905static int qla4xxx_recover_adapter(struct scsi_qla_host *ha,
906 uint8_t renew_ddb_list)
907{
908 int status;
909
910 /* Stall incoming I/O until we are done */
911 clear_bit(AF_ONLINE, &ha->flags);
Mike Christie50a29ae2008-03-04 13:26:53 -0600912
David Somayajuluafaf5a22006-09-19 10:28:00 -0700913 DEBUG2(printk("scsi%ld: %s calling qla4xxx_cmd_wait\n", ha->host_no,
914 __func__));
915
916 /* Wait for outstanding commands to complete.
917 * Stalls the driver for max 30 secs
918 */
919 status = qla4xxx_cmd_wait(ha);
920
921 qla4xxx_disable_intrs(ha);
922
923 /* Flush any pending ddb changed AENs */
924 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
925
Karen Higginsdca05c42009-07-15 15:03:00 -0500926 qla4xxx_flush_active_srbs(ha);
927
David Somayajuluafaf5a22006-09-19 10:28:00 -0700928 /* Reset the firmware. If successful, function
929 * returns with ISP interrupts enabled.
930 */
Karen Higginsdca05c42009-07-15 15:03:00 -0500931 DEBUG2(printk("scsi%ld: %s - Performing soft reset..\n",
932 ha->host_no, __func__));
933 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS)
934 status = qla4xxx_soft_reset(ha);
935 else
936 status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700937
938 /* Flush any pending ddb changed AENs */
939 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
940
941 /* Re-initialize firmware. If successful, function returns
942 * with ISP interrupts enabled */
943 if (status == QLA_SUCCESS) {
944 DEBUG2(printk("scsi%ld: %s - Initializing adapter..\n",
945 ha->host_no, __func__));
946
947 /* If successful, AF_ONLINE flag set in
948 * qla4xxx_initialize_adapter */
949 status = qla4xxx_initialize_adapter(ha, renew_ddb_list);
950 }
951
952 /* Failed adapter initialization?
953 * Retry reset_ha only if invoked via DPC (DPC_RESET_HA) */
954 if ((test_bit(AF_ONLINE, &ha->flags) == 0) &&
955 (test_bit(DPC_RESET_HA, &ha->dpc_flags))) {
956 /* Adapter initialization failed, see if we can retry
957 * resetting the ha */
958 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
959 ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
960 DEBUG2(printk("scsi%ld: recover adapter - retrying "
961 "(%d) more times\n", ha->host_no,
962 ha->retry_reset_ha_cnt));
963 set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
964 status = QLA_ERROR;
965 } else {
966 if (ha->retry_reset_ha_cnt > 0) {
967 /* Schedule another Reset HA--DPC will retry */
968 ha->retry_reset_ha_cnt--;
969 DEBUG2(printk("scsi%ld: recover adapter - "
970 "retry remaining %d\n",
971 ha->host_no,
972 ha->retry_reset_ha_cnt));
973 status = QLA_ERROR;
974 }
975
976 if (ha->retry_reset_ha_cnt == 0) {
977 /* Recover adapter retries have been exhausted.
978 * Adapter DEAD */
979 DEBUG2(printk("scsi%ld: recover adapter "
980 "failed - board disabled\n",
981 ha->host_no));
982 qla4xxx_flush_active_srbs(ha);
983 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
984 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
985 clear_bit(DPC_RESET_HA_DESTROY_DDB_LIST,
986 &ha->dpc_flags);
987 status = QLA_ERROR;
988 }
989 }
990 } else {
991 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
992 clear_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags);
993 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
994 }
995
996 ha->adapter_error_count++;
997
998 if (status == QLA_SUCCESS)
999 qla4xxx_enable_intrs(ha);
1000
1001 DEBUG2(printk("scsi%ld: recover adapter .. DONE\n", ha->host_no));
1002 return status;
1003}
1004
1005/**
1006 * qla4xxx_do_dpc - dpc routine
1007 * @data: in our case pointer to adapter structure
1008 *
1009 * This routine is a task that is schedule by the interrupt handler
1010 * to perform the background processing for interrupts. We put it
1011 * on a task queue that is consumed whenever the scheduler runs; that's
1012 * so you can do anything (i.e. put the process to sleep etc). In fact,
1013 * the mid-level tries to sleep when it reaches the driver threshold
1014 * "host->can_queue". This can cause a panic if we were in our interrupt code.
1015 **/
David Howellsc4028952006-11-22 14:57:56 +00001016static void qla4xxx_do_dpc(struct work_struct *work)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001017{
David Howellsc4028952006-11-22 14:57:56 +00001018 struct scsi_qla_host *ha =
1019 container_of(work, struct scsi_qla_host, dpc_work);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001020 struct ddb_entry *ddb_entry, *dtemp;
David C Somayajulu477ffb92007-01-22 12:26:11 -08001021 int status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001022
David C Somayajuluf26b9042006-11-15 16:41:09 -08001023 DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
David C Somayajulu477ffb92007-01-22 12:26:11 -08001024 "flags = 0x%08lx, dpc_flags = 0x%08lx ctrl_stat = 0x%08x\n",
1025 ha->host_no, __func__, ha->flags, ha->dpc_flags,
1026 readw(&ha->reg->ctrl_status)));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001027
1028 /* Initialization not yet finished. Don't do anything yet. */
1029 if (!test_bit(AF_INIT_DONE, &ha->flags))
1030 return;
1031
1032 if (adapter_up(ha) ||
1033 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1034 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1035 test_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags)) {
David C Somayajuluf26b9042006-11-15 16:41:09 -08001036 if (test_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags) ||
1037 test_bit(DPC_RESET_HA, &ha->dpc_flags))
David Somayajuluafaf5a22006-09-19 10:28:00 -07001038 qla4xxx_recover_adapter(ha, PRESERVE_DDB_LIST);
1039
David C Somayajulu477ffb92007-01-22 12:26:11 -08001040 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001041 uint8_t wait_time = RESET_INTR_TOV;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001042
David Somayajuluafaf5a22006-09-19 10:28:00 -07001043 while ((readw(&ha->reg->ctrl_status) &
1044 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
1045 if (--wait_time == 0)
1046 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001047 msleep(1000);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001048 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001049 if (wait_time == 0)
1050 DEBUG2(printk("scsi%ld: %s: SR|FSR "
1051 "bit not cleared-- resetting\n",
1052 ha->host_no, __func__));
David C Somayajulu477ffb92007-01-22 12:26:11 -08001053 qla4xxx_flush_active_srbs(ha);
1054 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
1055 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1056 status = qla4xxx_initialize_adapter(ha,
1057 PRESERVE_DDB_LIST);
1058 }
1059 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1060 if (status == QLA_SUCCESS)
1061 qla4xxx_enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001062 }
1063 }
1064
1065 /* ---- process AEN? --- */
1066 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
1067 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
1068
1069 /* ---- Get DHCP IP Address? --- */
1070 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
1071 qla4xxx_get_dhcp_ip_address(ha);
1072
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301073 /* ---- link change? --- */
1074 if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
1075 if (!test_bit(AF_LINK_UP, &ha->flags)) {
1076 /* ---- link down? --- */
1077 list_for_each_entry_safe(ddb_entry, dtemp,
1078 &ha->ddb_list, list) {
1079 if (atomic_read(&ddb_entry->state) ==
1080 DDB_STATE_ONLINE)
1081 qla4xxx_mark_device_missing(ha,
1082 ddb_entry);
1083 }
1084 } else {
1085 /* ---- link up? --- *
1086 * F/W will auto login to all devices ONLY ONCE after
1087 * link up during driver initialization and runtime
1088 * fatal error recovery. Therefore, the driver must
1089 * manually relogin to devices when recovering from
1090 * connection failures, logouts, expired KATO, etc. */
1091
1092 list_for_each_entry_safe(ddb_entry, dtemp,
1093 &ha->ddb_list, list) {
1094 if ((atomic_read(&ddb_entry->state) ==
1095 DDB_STATE_MISSING) ||
1096 (atomic_read(&ddb_entry->state) ==
1097 DDB_STATE_DEAD)) {
1098 if (ddb_entry->fw_ddb_device_state ==
1099 DDB_DS_SESSION_ACTIVE) {
1100 atomic_set(&ddb_entry->state,
1101 DDB_STATE_ONLINE);
1102 dev_info(&ha->pdev->dev,
1103 "scsi%ld: %s: ddb[%d]"
1104 " os[%d] marked"
1105 " ONLINE\n",
1106 ha->host_no, __func__,
1107 ddb_entry->fw_ddb_index,
1108 ddb_entry->os_target_id);
1109
1110 iscsi_unblock_session(
1111 ddb_entry->sess);
1112 } else
1113 qla4xxx_relogin_device(
1114 ha, ddb_entry);
1115 }
1116
1117 }
1118 }
1119 }
1120
David Somayajuluafaf5a22006-09-19 10:28:00 -07001121 /* ---- relogin device? --- */
1122 if (adapter_up(ha) &&
1123 test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
1124 list_for_each_entry_safe(ddb_entry, dtemp,
1125 &ha->ddb_list, list) {
1126 if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
1127 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
1128 qla4xxx_relogin_device(ha, ddb_entry);
1129
1130 /*
1131 * If mbx cmd times out there is no point
1132 * in continuing further.
1133 * With large no of targets this can hang
1134 * the system.
1135 */
1136 if (test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
1137 printk(KERN_WARNING "scsi%ld: %s: "
1138 "need to reset hba\n",
1139 ha->host_no, __func__);
1140 break;
1141 }
1142 }
1143 }
1144}
1145
1146/**
1147 * qla4xxx_free_adapter - release the adapter
1148 * @ha: pointer to adapter structure
1149 **/
1150static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
1151{
1152
1153 if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
1154 /* Turn-off interrupts on the card. */
1155 qla4xxx_disable_intrs(ha);
1156 }
1157
1158 /* Kill the kernel thread for this host */
1159 if (ha->dpc_thread)
1160 destroy_workqueue(ha->dpc_thread);
1161
1162 /* Issue Soft Reset to put firmware in unknown state */
David C Somayajuluf26b9042006-11-15 16:41:09 -08001163 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS)
David C Somayajulu477ffb92007-01-22 12:26:11 -08001164 qla4xxx_hw_reset(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001165
1166 /* Remove timer thread, if present */
1167 if (ha->timer_active)
1168 qla4xxx_stop_timer(ha);
1169
David Somayajuluafaf5a22006-09-19 10:28:00 -07001170 /* Detach interrupts */
1171 if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
1172 free_irq(ha->pdev->irq, ha);
1173
David C Somayajulubee4fe82007-05-23 18:03:32 -07001174 /* free extra memory */
1175 qla4xxx_mem_free(ha);
1176
David Somayajuluafaf5a22006-09-19 10:28:00 -07001177 pci_disable_device(ha->pdev);
1178
1179}
1180
1181/***
1182 * qla4xxx_iospace_config - maps registers
1183 * @ha: pointer to adapter structure
1184 *
1185 * This routines maps HBA's registers from the pci address space
1186 * into the kernel virtual address space for memory mapped i/o.
1187 **/
1188static int qla4xxx_iospace_config(struct scsi_qla_host *ha)
1189{
1190 unsigned long pio, pio_len, pio_flags;
1191 unsigned long mmio, mmio_len, mmio_flags;
1192
1193 pio = pci_resource_start(ha->pdev, 0);
1194 pio_len = pci_resource_len(ha->pdev, 0);
1195 pio_flags = pci_resource_flags(ha->pdev, 0);
1196 if (pio_flags & IORESOURCE_IO) {
1197 if (pio_len < MIN_IOBASE_LEN) {
1198 dev_warn(&ha->pdev->dev,
1199 "Invalid PCI I/O region size\n");
1200 pio = 0;
1201 }
1202 } else {
1203 dev_warn(&ha->pdev->dev, "region #0 not a PIO resource\n");
1204 pio = 0;
1205 }
1206
1207 /* Use MMIO operations for all accesses. */
1208 mmio = pci_resource_start(ha->pdev, 1);
1209 mmio_len = pci_resource_len(ha->pdev, 1);
1210 mmio_flags = pci_resource_flags(ha->pdev, 1);
1211
1212 if (!(mmio_flags & IORESOURCE_MEM)) {
1213 dev_err(&ha->pdev->dev,
1214 "region #0 not an MMIO resource, aborting\n");
1215
1216 goto iospace_error_exit;
1217 }
1218 if (mmio_len < MIN_IOBASE_LEN) {
1219 dev_err(&ha->pdev->dev,
1220 "Invalid PCI mem region size, aborting\n");
1221 goto iospace_error_exit;
1222 }
1223
1224 if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
1225 dev_warn(&ha->pdev->dev,
1226 "Failed to reserve PIO/MMIO regions\n");
1227
1228 goto iospace_error_exit;
1229 }
1230
1231 ha->pio_address = pio;
1232 ha->pio_length = pio_len;
1233 ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
1234 if (!ha->reg) {
1235 dev_err(&ha->pdev->dev,
1236 "cannot remap MMIO, aborting\n");
1237
1238 goto iospace_error_exit;
1239 }
1240
1241 return 0;
1242
1243iospace_error_exit:
1244 return -ENOMEM;
1245}
1246
1247/**
1248 * qla4xxx_probe_adapter - callback function to probe HBA
1249 * @pdev: pointer to pci_dev structure
1250 * @pci_device_id: pointer to pci_device entry
1251 *
1252 * This routine will probe for Qlogic 4xxx iSCSI host adapters.
1253 * It returns zero if successful. It also initializes all data necessary for
1254 * the driver.
1255 **/
1256static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
1257 const struct pci_device_id *ent)
1258{
1259 int ret = -ENODEV, status;
1260 struct Scsi_Host *host;
1261 struct scsi_qla_host *ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001262 uint8_t init_retry_count = 0;
1263 char buf[34];
1264
1265 if (pci_enable_device(pdev))
1266 return -1;
1267
1268 host = scsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha));
1269 if (host == NULL) {
1270 printk(KERN_WARNING
1271 "qla4xxx: Couldn't allocate host from scsi layer!\n");
1272 goto probe_disable_device;
1273 }
1274
1275 /* Clear our data area */
1276 ha = (struct scsi_qla_host *) host->hostdata;
1277 memset(ha, 0, sizeof(*ha));
1278
1279 /* Save the information from PCI BIOS. */
1280 ha->pdev = pdev;
1281 ha->host = host;
1282 ha->host_no = host->host_no;
1283
1284 /* Configure PCI I/O space. */
1285 ret = qla4xxx_iospace_config(ha);
1286 if (ret)
1287 goto probe_failed;
1288
1289 dev_info(&ha->pdev->dev, "Found an ISP%04x, irq %d, iobase 0x%p\n",
1290 pdev->device, pdev->irq, ha->reg);
1291
1292 qla4xxx_config_dma_addressing(ha);
1293
1294 /* Initialize lists and spinlocks. */
1295 INIT_LIST_HEAD(&ha->ddb_list);
1296 INIT_LIST_HEAD(&ha->free_srb_q);
1297
1298 mutex_init(&ha->mbox_sem);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001299
1300 spin_lock_init(&ha->hardware_lock);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001301
1302 /* Allocate dma buffers */
1303 if (qla4xxx_mem_alloc(ha)) {
1304 dev_warn(&ha->pdev->dev,
1305 "[ERROR] Failed to allocate memory for adapter\n");
1306
1307 ret = -ENOMEM;
1308 goto probe_failed;
1309 }
1310
1311 /*
1312 * Initialize the Host adapter request/response queues and
1313 * firmware
1314 * NOTE: interrupts enabled upon successful completion
1315 */
1316 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1317 while (status == QLA_ERROR && init_retry_count++ < MAX_INIT_RETRIES) {
1318 DEBUG2(printk("scsi: %s: retrying adapter initialization "
1319 "(%d)\n", __func__, init_retry_count));
1320 qla4xxx_soft_reset(ha);
1321 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1322 }
1323 if (status == QLA_ERROR) {
1324 dev_warn(&ha->pdev->dev, "Failed to initialize adapter\n");
1325
1326 ret = -ENODEV;
1327 goto probe_failed;
1328 }
1329
1330 host->cmd_per_lun = 3;
1331 host->max_channel = 0;
1332 host->max_lun = MAX_LUNS - 1;
1333 host->max_id = MAX_TARGETS;
1334 host->max_cmd_len = IOCB_MAX_CDB_LEN;
1335 host->can_queue = MAX_SRBS ;
1336 host->transportt = qla4xxx_scsi_transport;
1337
1338 ret = scsi_init_shared_tag_map(host, MAX_SRBS);
1339 if (ret) {
Joe Perches898eb712007-10-18 03:06:30 -07001340 dev_warn(&ha->pdev->dev, "scsi_init_shared_tag_map failed\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001341 goto probe_failed;
1342 }
1343
1344 /* Startup the kernel thread for this host adapter. */
1345 DEBUG2(printk("scsi: %s: Starting kernel thread for "
1346 "qla4xxx_dpc\n", __func__));
1347 sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
1348 ha->dpc_thread = create_singlethread_workqueue(buf);
1349 if (!ha->dpc_thread) {
1350 dev_warn(&ha->pdev->dev, "Unable to start DPC thread!\n");
1351 ret = -ENODEV;
1352 goto probe_failed;
1353 }
David Howellsc4028952006-11-22 14:57:56 +00001354 INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001355
1356 ret = request_irq(pdev->irq, qla4xxx_intr_handler,
Thomas Gleixner38515e92007-02-14 00:33:16 -08001357 IRQF_DISABLED | IRQF_SHARED, "qla4xxx", ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001358 if (ret) {
1359 dev_warn(&ha->pdev->dev, "Failed to reserve interrupt %d"
1360 " already in use.\n", pdev->irq);
1361 goto probe_failed;
1362 }
1363 set_bit(AF_IRQ_ATTACHED, &ha->flags);
1364 host->irq = pdev->irq;
1365 DEBUG(printk("scsi%d: irq %d attached\n", ha->host_no, ha->pdev->irq));
1366
1367 qla4xxx_enable_intrs(ha);
1368
1369 /* Start timer thread. */
1370 qla4xxx_start_timer(ha, qla4xxx_timer, 1);
1371
1372 set_bit(AF_INIT_DONE, &ha->flags);
1373
1374 pci_set_drvdata(pdev, ha);
1375
1376 ret = scsi_add_host(host, &pdev->dev);
1377 if (ret)
1378 goto probe_failed;
1379
David Somayajuluafaf5a22006-09-19 10:28:00 -07001380 printk(KERN_INFO
1381 " QLogic iSCSI HBA Driver version: %s\n"
1382 " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
1383 qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
1384 ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
1385 ha->patch_number, ha->build_number);
Mike Christie921601b2008-01-31 13:36:49 -06001386 scsi_scan_host(host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001387 return 0;
1388
David Somayajuluafaf5a22006-09-19 10:28:00 -07001389probe_failed:
1390 qla4xxx_free_adapter(ha);
1391 scsi_host_put(ha->host);
1392
1393probe_disable_device:
1394 pci_disable_device(pdev);
1395
1396 return ret;
1397}
1398
1399/**
1400 * qla4xxx_remove_adapter - calback function to remove adapter.
1401 * @pci_dev: PCI device pointer
1402 **/
1403static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
1404{
1405 struct scsi_qla_host *ha;
1406
1407 ha = pci_get_drvdata(pdev);
1408
David C Somayajulubee4fe82007-05-23 18:03:32 -07001409 qla4xxx_disable_intrs(ha);
1410
1411 while (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags))
1412 ssleep(1);
1413
David Somayajuluafaf5a22006-09-19 10:28:00 -07001414 /* remove devs from iscsi_sessions to scsi_devices */
1415 qla4xxx_free_ddb_list(ha);
1416
1417 scsi_remove_host(ha->host);
1418
1419 qla4xxx_free_adapter(ha);
1420
1421 scsi_host_put(ha->host);
1422
1423 pci_set_drvdata(pdev, NULL);
1424}
1425
1426/**
1427 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
1428 * @ha: HA context
1429 *
1430 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
1431 * supported addressing method.
1432 */
Adrian Bunk47975472007-04-26 00:35:16 -07001433static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001434{
1435 int retval;
1436
1437 /* Update our PCI device dma_mask for full 64 bit mask */
Yang Hongyang6a355282009-04-06 19:01:13 -07001438 if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
1439 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001440 dev_dbg(&ha->pdev->dev,
1441 "Failed to set 64 bit PCI consistent mask; "
1442 "using 32 bit.\n");
1443 retval = pci_set_consistent_dma_mask(ha->pdev,
Yang Hongyang284901a2009-04-06 19:01:15 -07001444 DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001445 }
1446 } else
Yang Hongyang284901a2009-04-06 19:01:15 -07001447 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001448}
1449
1450static int qla4xxx_slave_alloc(struct scsi_device *sdev)
1451{
1452 struct iscsi_cls_session *sess = starget_to_session(sdev->sdev_target);
1453 struct ddb_entry *ddb = sess->dd_data;
1454
1455 sdev->hostdata = ddb;
1456 sdev->tagged_supported = 1;
Mike Christied510d962008-07-11 19:50:33 -05001457 scsi_activate_tcq(sdev, QL4_DEF_QDEPTH);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001458 return 0;
1459}
1460
1461static int qla4xxx_slave_configure(struct scsi_device *sdev)
1462{
1463 sdev->tagged_supported = 1;
1464 return 0;
1465}
1466
1467static void qla4xxx_slave_destroy(struct scsi_device *sdev)
1468{
1469 scsi_deactivate_tcq(sdev, 1);
1470}
1471
1472/**
1473 * qla4xxx_del_from_active_array - returns an active srb
1474 * @ha: Pointer to host adapter structure.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001475 * @index: index into the active_array
David Somayajuluafaf5a22006-09-19 10:28:00 -07001476 *
1477 * This routine removes and returns the srb at the specified index
1478 **/
1479struct srb * qla4xxx_del_from_active_array(struct scsi_qla_host *ha, uint32_t index)
1480{
1481 struct srb *srb = NULL;
1482 struct scsi_cmnd *cmd;
1483
1484 if (!(cmd = scsi_host_find_tag(ha->host, index)))
1485 return srb;
1486
1487 if (!(srb = (struct srb *)cmd->host_scribble))
1488 return srb;
1489
1490 /* update counters */
1491 if (srb->flags & SRB_DMA_VALID) {
1492 ha->req_q_count += srb->iocb_cnt;
1493 ha->iocb_cnt -= srb->iocb_cnt;
1494 if (srb->cmd)
1495 srb->cmd->host_scribble = NULL;
1496 }
1497 return srb;
1498}
1499
1500/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07001501 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
1502 * @ha: actual ha whose done queue will contain the comd returned by firmware.
1503 * @cmd: Scsi Command to wait on.
1504 *
1505 * This routine waits for the command to be returned by the Firmware
1506 * for some max time.
1507 **/
1508static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
1509 struct scsi_cmnd *cmd)
1510{
1511 int done = 0;
1512 struct srb *rp;
1513 uint32_t max_wait_time = EH_WAIT_CMD_TOV;
1514
1515 do {
1516 /* Checking to see if its returned to OS */
1517 rp = (struct srb *) cmd->SCp.ptr;
1518 if (rp == NULL) {
1519 done++;
1520 break;
1521 }
1522
1523 msleep(2000);
1524 } while (max_wait_time--);
1525
1526 return done;
1527}
1528
1529/**
1530 * qla4xxx_wait_for_hba_online - waits for HBA to come online
1531 * @ha: Pointer to host adapter structure
1532 **/
1533static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
1534{
1535 unsigned long wait_online;
1536
1537 wait_online = jiffies + (30 * HZ);
1538 while (time_before(jiffies, wait_online)) {
1539
1540 if (adapter_up(ha))
1541 return QLA_SUCCESS;
1542 else if (ha->retry_reset_ha_cnt == 0)
1543 return QLA_ERROR;
1544
1545 msleep(2000);
1546 }
1547
1548 return QLA_ERROR;
1549}
1550
1551/**
Mike Christiece545032008-02-29 18:25:20 -06001552 * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001553 * @ha: pointer to HBA
David Somayajuluafaf5a22006-09-19 10:28:00 -07001554 * @t: target id
1555 * @l: lun id
1556 *
1557 * This function waits for all outstanding commands to a lun to complete. It
1558 * returns 0 if all pending commands are returned and 1 otherwise.
1559 **/
Mike Christiece545032008-02-29 18:25:20 -06001560static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
1561 struct scsi_target *stgt,
1562 struct scsi_device *sdev)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001563{
1564 int cnt;
1565 int status = 0;
1566 struct scsi_cmnd *cmd;
1567
1568 /*
Mike Christiece545032008-02-29 18:25:20 -06001569 * Waiting for all commands for the designated target or dev
1570 * in the active array
David Somayajuluafaf5a22006-09-19 10:28:00 -07001571 */
1572 for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
1573 cmd = scsi_host_find_tag(ha->host, cnt);
Mike Christiece545032008-02-29 18:25:20 -06001574 if (cmd && stgt == scsi_target(cmd->device) &&
1575 (!sdev || sdev == cmd->device)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001576 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
1577 status++;
1578 break;
1579 }
1580 }
1581 }
1582 return status;
1583}
1584
1585/**
1586 * qla4xxx_eh_device_reset - callback for target reset.
1587 * @cmd: Pointer to Linux's SCSI command structure
1588 *
1589 * This routine is called by the Linux OS to reset all luns on the
1590 * specified target.
1591 **/
1592static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
1593{
1594 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
1595 struct ddb_entry *ddb_entry = cmd->device->hostdata;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001596 int ret = FAILED, stat;
1597
Karen Higgins612f73482009-07-15 15:03:01 -05001598 if (!ddb_entry)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001599 return ret;
1600
1601 dev_info(&ha->pdev->dev,
1602 "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
1603 cmd->device->channel, cmd->device->id, cmd->device->lun);
1604
1605 DEBUG2(printk(KERN_INFO
1606 "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
1607 "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
Jens Axboe242f9dc2008-09-14 05:55:09 -07001608 cmd, jiffies, cmd->request->timeout / HZ,
David Somayajuluafaf5a22006-09-19 10:28:00 -07001609 ha->dpc_flags, cmd->result, cmd->allowed));
1610
1611 /* FIXME: wait for hba to go online */
1612 stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
1613 if (stat != QLA_SUCCESS) {
1614 dev_info(&ha->pdev->dev, "DEVICE RESET FAILED. %d\n", stat);
1615 goto eh_dev_reset_done;
1616 }
1617
Mike Christiece545032008-02-29 18:25:20 -06001618 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
1619 cmd->device)) {
1620 dev_info(&ha->pdev->dev,
1621 "DEVICE RESET FAILED - waiting for "
1622 "commands.\n");
1623 goto eh_dev_reset_done;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001624 }
1625
David C Somayajulu9d562912008-03-19 11:23:03 -07001626 /* Send marker. */
1627 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
1628 MM_LUN_RESET) != QLA_SUCCESS)
1629 goto eh_dev_reset_done;
1630
David Somayajuluafaf5a22006-09-19 10:28:00 -07001631 dev_info(&ha->pdev->dev,
1632 "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
1633 ha->host_no, cmd->device->channel, cmd->device->id,
1634 cmd->device->lun);
1635
1636 ret = SUCCESS;
1637
1638eh_dev_reset_done:
1639
1640 return ret;
1641}
1642
1643/**
Mike Christiece545032008-02-29 18:25:20 -06001644 * qla4xxx_eh_target_reset - callback for target reset.
1645 * @cmd: Pointer to Linux's SCSI command structure
1646 *
1647 * This routine is called by the Linux OS to reset the target.
1648 **/
1649static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
1650{
1651 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
1652 struct ddb_entry *ddb_entry = cmd->device->hostdata;
1653 int stat;
1654
1655 if (!ddb_entry)
1656 return FAILED;
1657
1658 starget_printk(KERN_INFO, scsi_target(cmd->device),
1659 "WARM TARGET RESET ISSUED.\n");
1660
1661 DEBUG2(printk(KERN_INFO
1662 "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
1663 "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
Jens Axboe242f9dc2008-09-14 05:55:09 -07001664 ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
Mike Christiece545032008-02-29 18:25:20 -06001665 ha->dpc_flags, cmd->result, cmd->allowed));
1666
1667 stat = qla4xxx_reset_target(ha, ddb_entry);
1668 if (stat != QLA_SUCCESS) {
1669 starget_printk(KERN_INFO, scsi_target(cmd->device),
1670 "WARM TARGET RESET FAILED.\n");
1671 return FAILED;
1672 }
1673
Mike Christiece545032008-02-29 18:25:20 -06001674 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
1675 NULL)) {
1676 starget_printk(KERN_INFO, scsi_target(cmd->device),
1677 "WARM TARGET DEVICE RESET FAILED - "
1678 "waiting for commands.\n");
1679 return FAILED;
1680 }
1681
David C Somayajulu9d562912008-03-19 11:23:03 -07001682 /* Send marker. */
1683 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
1684 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
1685 starget_printk(KERN_INFO, scsi_target(cmd->device),
1686 "WARM TARGET DEVICE RESET FAILED - "
1687 "marker iocb failed.\n");
1688 return FAILED;
1689 }
1690
Mike Christiece545032008-02-29 18:25:20 -06001691 starget_printk(KERN_INFO, scsi_target(cmd->device),
1692 "WARM TARGET RESET SUCCEEDED.\n");
1693 return SUCCESS;
1694}
1695
1696/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07001697 * qla4xxx_eh_host_reset - kernel callback
1698 * @cmd: Pointer to Linux's SCSI command structure
1699 *
1700 * This routine is invoked by the Linux kernel to perform fatal error
1701 * recovery on the specified adapter.
1702 **/
1703static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
1704{
1705 int return_status = FAILED;
1706 struct scsi_qla_host *ha;
1707
1708 ha = (struct scsi_qla_host *) cmd->device->host->hostdata;
1709
1710 dev_info(&ha->pdev->dev,
Karen Higginsdca05c42009-07-15 15:03:00 -05001711 "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
David Somayajuluafaf5a22006-09-19 10:28:00 -07001712 cmd->device->channel, cmd->device->id, cmd->device->lun);
1713
1714 if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
1715 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
1716 "DEAD.\n", ha->host_no, cmd->device->channel,
1717 __func__));
1718
1719 return FAILED;
1720 }
1721
Mike Christie50a29ae2008-03-04 13:26:53 -06001722 /* make sure the dpc thread is stopped while we reset the hba */
1723 clear_bit(AF_ONLINE, &ha->flags);
1724 flush_workqueue(ha->dpc_thread);
1725
1726 if (qla4xxx_recover_adapter(ha, PRESERVE_DDB_LIST) == QLA_SUCCESS)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001727 return_status = SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001728
1729 dev_info(&ha->pdev->dev, "HOST RESET %s.\n",
1730 return_status == FAILED ? "FAILED" : "SUCCEDED");
1731
1732 return return_status;
1733}
1734
1735
1736static struct pci_device_id qla4xxx_pci_tbl[] = {
1737 {
1738 .vendor = PCI_VENDOR_ID_QLOGIC,
1739 .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
1740 .subvendor = PCI_ANY_ID,
1741 .subdevice = PCI_ANY_ID,
1742 },
1743 {
1744 .vendor = PCI_VENDOR_ID_QLOGIC,
1745 .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
1746 .subvendor = PCI_ANY_ID,
1747 .subdevice = PCI_ANY_ID,
1748 },
David C Somayajulud9150582006-11-15 17:38:40 -08001749 {
1750 .vendor = PCI_VENDOR_ID_QLOGIC,
1751 .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
1752 .subvendor = PCI_ANY_ID,
1753 .subdevice = PCI_ANY_ID,
1754 },
David Somayajuluafaf5a22006-09-19 10:28:00 -07001755 {0, 0},
1756};
1757MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
1758
Adrian Bunk47975472007-04-26 00:35:16 -07001759static struct pci_driver qla4xxx_pci_driver = {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001760 .name = DRIVER_NAME,
1761 .id_table = qla4xxx_pci_tbl,
1762 .probe = qla4xxx_probe_adapter,
1763 .remove = qla4xxx_remove_adapter,
1764};
1765
1766static int __init qla4xxx_module_init(void)
1767{
1768 int ret;
1769
1770 /* Allocate cache for SRBs. */
1771 srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09001772 SLAB_HWCACHE_ALIGN, NULL);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001773 if (srb_cachep == NULL) {
1774 printk(KERN_ERR
1775 "%s: Unable to allocate SRB cache..."
1776 "Failing load!\n", DRIVER_NAME);
1777 ret = -ENOMEM;
1778 goto no_srp_cache;
1779 }
1780
1781 /* Derive version string. */
1782 strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
Andrew Vasquez11010fe2006-10-06 09:54:59 -07001783 if (ql4xextended_error_logging)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001784 strcat(qla4xxx_version_str, "-debug");
1785
1786 qla4xxx_scsi_transport =
1787 iscsi_register_transport(&qla4xxx_iscsi_transport);
1788 if (!qla4xxx_scsi_transport){
1789 ret = -ENODEV;
1790 goto release_srb_cache;
1791 }
1792
David Somayajuluafaf5a22006-09-19 10:28:00 -07001793 ret = pci_register_driver(&qla4xxx_pci_driver);
1794 if (ret)
1795 goto unregister_transport;
1796
1797 printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
1798 return 0;
Doug Maxey5ae16db2006-10-05 23:50:07 -05001799
David Somayajuluafaf5a22006-09-19 10:28:00 -07001800unregister_transport:
1801 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
1802release_srb_cache:
1803 kmem_cache_destroy(srb_cachep);
1804no_srp_cache:
1805 return ret;
1806}
1807
1808static void __exit qla4xxx_module_exit(void)
1809{
David C Somayajulu477ffb92007-01-22 12:26:11 -08001810 ql4_mod_unload = 1;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001811 pci_unregister_driver(&qla4xxx_pci_driver);
1812 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
1813 kmem_cache_destroy(srb_cachep);
1814}
1815
1816module_init(qla4xxx_module_init);
1817module_exit(qla4xxx_module_exit);
1818
1819MODULE_AUTHOR("QLogic Corporation");
1820MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
1821MODULE_LICENSE("GPL");
1822MODULE_VERSION(QLA4XXX_DRIVER_VERSION);