blob: daf5a4bf9b0aecca0f886a57b3f82181cc590f5e [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;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053033module_param(ql4xdiscoverywait, int, S_IRUGO | S_IWUSR);
David Somayajuluafaf5a22006-09-19 10:28:00 -070034MODULE_PARM_DESC(ql4xdiscoverywait, "Discovery wait time");
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053035
David Somayajuluafaf5a22006-09-19 10:28:00 -070036int ql4xdontresethba = 0;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053037module_param(ql4xdontresethba, int, S_IRUGO | S_IWUSR);
David Somayajuluafaf5a22006-09-19 10:28:00 -070038MODULE_PARM_DESC(ql4xdontresethba,
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053039 "Don't reset the HBA for driver recovery \n"
40 " 0 - It will reset HBA (Default)\n"
41 " 1 - It will NOT reset HBA");
David Somayajuluafaf5a22006-09-19 10:28:00 -070042
Andrew Vasquez11010fe2006-10-06 09:54:59 -070043int ql4xextended_error_logging = 0; /* 0 = off, 1 = log errors */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053044module_param(ql4xextended_error_logging, int, S_IRUGO | S_IWUSR);
Andrew Vasquez11010fe2006-10-06 09:54:59 -070045MODULE_PARM_DESC(ql4xextended_error_logging,
David Somayajuluafaf5a22006-09-19 10:28:00 -070046 "Option to enable extended error logging, "
47 "Default is 0 - no logging, 1 - debug logging");
48
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053049int ql4xenablemsix = 1;
50module_param(ql4xenablemsix, int, S_IRUGO|S_IWUSR);
51MODULE_PARM_DESC(ql4xenablemsix,
52 "Set to enable MSI or MSI-X interrupt mechanism.\n"
53 " 0 = enable INTx interrupt mechanism.\n"
54 " 1 = enable MSI-X interrupt mechanism (Default).\n"
55 " 2 = enable MSI interrupt mechanism.");
David C Somayajulu477ffb92007-01-22 12:26:11 -080056
Mike Christied510d962008-07-11 19:50:33 -050057#define QL4_DEF_QDEPTH 32
58
David Somayajuluafaf5a22006-09-19 10:28:00 -070059/*
60 * SCSI host template entry points
61 */
Adrian Bunk47975472007-04-26 00:35:16 -070062static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -070063
64/*
65 * iSCSI template entry points
66 */
Mike Christie2174a042007-05-30 12:57:10 -050067static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
68 enum iscsi_tgt_dscvr type, uint32_t enable,
69 struct sockaddr *dst_addr);
David Somayajuluafaf5a22006-09-19 10:28:00 -070070static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
71 enum iscsi_param param, char *buf);
72static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
73 enum iscsi_param param, char *buf);
Mike Christieaa1e93a2007-05-30 12:57:09 -050074static int qla4xxx_host_get_param(struct Scsi_Host *shost,
75 enum iscsi_host_param param, char *buf);
David Somayajuluafaf5a22006-09-19 10:28:00 -070076static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session);
Mike Christie5c656af2009-07-15 15:02:59 -050077static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
David Somayajuluafaf5a22006-09-19 10:28:00 -070078
79/*
80 * SCSI host template entry points
81 */
82static int qla4xxx_queuecommand(struct scsi_cmnd *cmd,
83 void (*done) (struct scsi_cmnd *));
Vikas Chaudhary09a0f712010-04-28 11:42:24 +053084static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -070085static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
Mike Christiece545032008-02-29 18:25:20 -060086static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -070087static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
88static int qla4xxx_slave_alloc(struct scsi_device *device);
89static int qla4xxx_slave_configure(struct scsi_device *device);
90static void qla4xxx_slave_destroy(struct scsi_device *sdev);
Mike Christie024f8012008-03-04 13:26:54 -060091static void qla4xxx_scan_start(struct Scsi_Host *shost);
David Somayajuluafaf5a22006-09-19 10:28:00 -070092
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053093static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
94 QLA82XX_LEGACY_INTR_CONFIG;
95
David Somayajuluafaf5a22006-09-19 10:28:00 -070096static struct scsi_host_template qla4xxx_driver_template = {
97 .module = THIS_MODULE,
98 .name = DRIVER_NAME,
99 .proc_name = DRIVER_NAME,
100 .queuecommand = qla4xxx_queuecommand,
101
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530102 .eh_abort_handler = qla4xxx_eh_abort,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700103 .eh_device_reset_handler = qla4xxx_eh_device_reset,
Mike Christiece545032008-02-29 18:25:20 -0600104 .eh_target_reset_handler = qla4xxx_eh_target_reset,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700105 .eh_host_reset_handler = qla4xxx_eh_host_reset,
Mike Christie5c656af2009-07-15 15:02:59 -0500106 .eh_timed_out = qla4xxx_eh_cmd_timed_out,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700107
108 .slave_configure = qla4xxx_slave_configure,
109 .slave_alloc = qla4xxx_slave_alloc,
110 .slave_destroy = qla4xxx_slave_destroy,
111
Mike Christie921601b2008-01-31 13:36:49 -0600112 .scan_finished = iscsi_scan_finished,
Mike Christie024f8012008-03-04 13:26:54 -0600113 .scan_start = qla4xxx_scan_start,
Mike Christie921601b2008-01-31 13:36:49 -0600114
David Somayajuluafaf5a22006-09-19 10:28:00 -0700115 .this_id = -1,
116 .cmd_per_lun = 3,
117 .use_clustering = ENABLE_CLUSTERING,
118 .sg_tablesize = SG_ALL,
119
120 .max_sectors = 0xFFFF,
121};
122
123static struct iscsi_transport qla4xxx_iscsi_transport = {
124 .owner = THIS_MODULE,
125 .name = DRIVER_NAME,
Mike Christied8196ed2007-05-30 12:57:25 -0500126 .caps = CAP_FW_DB | CAP_SENDTARGETS_OFFLOAD |
127 CAP_DATA_PATH_OFFLOAD,
Mike Christieaa1e93a2007-05-30 12:57:09 -0500128 .param_mask = ISCSI_CONN_PORT | ISCSI_CONN_ADDRESS |
129 ISCSI_TARGET_NAME | ISCSI_TPGT,
Mike Christie8ad57812007-05-30 12:57:13 -0500130 .host_param_mask = ISCSI_HOST_HWADDRESS |
Mike Christie22236962007-05-30 12:57:24 -0500131 ISCSI_HOST_IPADDRESS |
Mike Christie8ad57812007-05-30 12:57:13 -0500132 ISCSI_HOST_INITIATOR_NAME,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700133 .tgt_dscvr = qla4xxx_tgt_dscvr,
134 .get_conn_param = qla4xxx_conn_get_param,
135 .get_session_param = qla4xxx_sess_get_param,
Mike Christieaa1e93a2007-05-30 12:57:09 -0500136 .get_host_param = qla4xxx_host_get_param,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700137 .session_recovery_timedout = qla4xxx_recovery_timedout,
138};
139
140static struct scsi_transport_template *qla4xxx_scsi_transport;
141
Mike Christie5c656af2009-07-15 15:02:59 -0500142static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
143{
144 struct iscsi_cls_session *session;
145 struct ddb_entry *ddb_entry;
146
147 session = starget_to_session(scsi_target(sc->device));
148 ddb_entry = session->dd_data;
149
150 /* if we are not logged in then the LLD is going to clean up the cmd */
151 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
152 return BLK_EH_RESET_TIMER;
153 else
154 return BLK_EH_NOT_HANDLED;
155}
156
David Somayajuluafaf5a22006-09-19 10:28:00 -0700157static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session)
158{
159 struct ddb_entry *ddb_entry = session->dd_data;
160 struct scsi_qla_host *ha = ddb_entry->ha;
161
Mike Christie568d3032008-01-31 13:36:47 -0600162 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
163 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700164
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530165 DEBUG2(printk("scsi%ld: %s: ddb [%d] port down retry count "
Mike Christie568d3032008-01-31 13:36:47 -0600166 "of (%d) secs exhausted, marking device DEAD.\n",
167 ha->host_no, __func__, ddb_entry->fw_ddb_index,
168 ha->port_down_retry_count));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700169
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530170 qla4xxx_wake_dpc(ha);
Mike Christie568d3032008-01-31 13:36:47 -0600171 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700172}
173
Mike Christieaa1e93a2007-05-30 12:57:09 -0500174static int qla4xxx_host_get_param(struct Scsi_Host *shost,
175 enum iscsi_host_param param, char *buf)
176{
177 struct scsi_qla_host *ha = to_qla_host(shost);
178 int len;
179
180 switch (param) {
181 case ISCSI_HOST_PARAM_HWADDRESS:
Michael Chan7ffc49a2007-12-24 21:28:09 -0800182 len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
Mike Christieaa1e93a2007-05-30 12:57:09 -0500183 break;
Mike Christie22236962007-05-30 12:57:24 -0500184 case ISCSI_HOST_PARAM_IPADDRESS:
185 len = sprintf(buf, "%d.%d.%d.%d\n", ha->ip_address[0],
186 ha->ip_address[1], ha->ip_address[2],
187 ha->ip_address[3]);
188 break;
Mike Christie8ad57812007-05-30 12:57:13 -0500189 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie22236962007-05-30 12:57:24 -0500190 len = sprintf(buf, "%s\n", ha->name_string);
Mike Christie8ad57812007-05-30 12:57:13 -0500191 break;
Mike Christieaa1e93a2007-05-30 12:57:09 -0500192 default:
193 return -ENOSYS;
194 }
195
196 return len;
197}
198
David Somayajuluafaf5a22006-09-19 10:28:00 -0700199static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
200 enum iscsi_param param, char *buf)
201{
202 struct ddb_entry *ddb_entry = sess->dd_data;
203 int len;
204
205 switch (param) {
206 case ISCSI_PARAM_TARGET_NAME:
207 len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
208 ddb_entry->iscsi_name);
209 break;
210 case ISCSI_PARAM_TPGT:
211 len = sprintf(buf, "%u\n", ddb_entry->tpgt);
212 break;
213 default:
214 return -ENOSYS;
215 }
216
217 return len;
218}
219
220static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
221 enum iscsi_param param, char *buf)
222{
223 struct iscsi_cls_session *session;
224 struct ddb_entry *ddb_entry;
225 int len;
226
227 session = iscsi_dev_to_session(conn->dev.parent);
228 ddb_entry = session->dd_data;
229
230 switch (param) {
231 case ISCSI_PARAM_CONN_PORT:
232 len = sprintf(buf, "%hu\n", ddb_entry->port);
233 break;
234 case ISCSI_PARAM_CONN_ADDRESS:
235 /* TODO: what are the ipv6 bits */
Harvey Harrison63779432008-10-31 00:56:00 -0700236 len = sprintf(buf, "%pI4\n", &ddb_entry->ip_addr);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700237 break;
238 default:
239 return -ENOSYS;
240 }
241
242 return len;
243}
244
Mike Christie2174a042007-05-30 12:57:10 -0500245static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
246 enum iscsi_tgt_dscvr type, uint32_t enable,
247 struct sockaddr *dst_addr)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700248{
249 struct scsi_qla_host *ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700250 struct sockaddr_in *addr;
251 struct sockaddr_in6 *addr6;
252 int ret = 0;
253
David Somayajuluafaf5a22006-09-19 10:28:00 -0700254 ha = (struct scsi_qla_host *) shost->hostdata;
255
256 switch (type) {
257 case ISCSI_TGT_DSCVR_SEND_TARGETS:
258 if (dst_addr->sa_family == AF_INET) {
259 addr = (struct sockaddr_in *)dst_addr;
260 if (qla4xxx_send_tgts(ha, (char *)&addr->sin_addr,
261 addr->sin_port) != QLA_SUCCESS)
262 ret = -EIO;
263 } else if (dst_addr->sa_family == AF_INET6) {
264 /*
265 * TODO: fix qla4xxx_send_tgts
266 */
267 addr6 = (struct sockaddr_in6 *)dst_addr;
268 if (qla4xxx_send_tgts(ha, (char *)&addr6->sin6_addr,
269 addr6->sin6_port) != QLA_SUCCESS)
270 ret = -EIO;
271 } else
272 ret = -ENOSYS;
273 break;
274 default:
275 ret = -ENOSYS;
276 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700277 return ret;
278}
279
280void qla4xxx_destroy_sess(struct ddb_entry *ddb_entry)
281{
282 if (!ddb_entry->sess)
283 return;
284
285 if (ddb_entry->conn) {
Mike Christie26974782007-12-13 12:43:29 -0600286 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700287 iscsi_remove_session(ddb_entry->sess);
288 }
289 iscsi_free_session(ddb_entry->sess);
290}
291
292int qla4xxx_add_sess(struct ddb_entry *ddb_entry)
293{
294 int err;
295
Mike Christie26974782007-12-13 12:43:29 -0600296 ddb_entry->sess->recovery_tmo = ddb_entry->ha->port_down_retry_count;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700297 err = iscsi_add_session(ddb_entry->sess, ddb_entry->fw_ddb_index);
298 if (err) {
299 DEBUG2(printk(KERN_ERR "Could not add session.\n"));
300 return err;
301 }
302
Mike Christie5d91e202008-05-21 15:54:01 -0500303 ddb_entry->conn = iscsi_create_conn(ddb_entry->sess, 0, 0);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700304 if (!ddb_entry->conn) {
305 iscsi_remove_session(ddb_entry->sess);
306 DEBUG2(printk(KERN_ERR "Could not add connection.\n"));
307 return -ENOMEM;
308 }
Mike Christieb6359302008-01-31 13:36:44 -0600309
310 /* finally ready to go */
311 iscsi_unblock_session(ddb_entry->sess);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700312 return 0;
313}
314
315struct ddb_entry *qla4xxx_alloc_sess(struct scsi_qla_host *ha)
316{
317 struct ddb_entry *ddb_entry;
318 struct iscsi_cls_session *sess;
319
Mike Christie5d91e202008-05-21 15:54:01 -0500320 sess = iscsi_alloc_session(ha->host, &qla4xxx_iscsi_transport,
321 sizeof(struct ddb_entry));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700322 if (!sess)
323 return NULL;
324
325 ddb_entry = sess->dd_data;
326 memset(ddb_entry, 0, sizeof(*ddb_entry));
327 ddb_entry->ha = ha;
328 ddb_entry->sess = sess;
329 return ddb_entry;
330}
331
Mike Christie024f8012008-03-04 13:26:54 -0600332static void qla4xxx_scan_start(struct Scsi_Host *shost)
333{
334 struct scsi_qla_host *ha = shost_priv(shost);
335 struct ddb_entry *ddb_entry, *ddbtemp;
336
337 /* finish setup of sessions that were already setup in firmware */
338 list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
339 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE)
340 qla4xxx_add_sess(ddb_entry);
341 }
342}
343
David Somayajuluafaf5a22006-09-19 10:28:00 -0700344/*
345 * Timer routines
346 */
347
348static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
349 unsigned long interval)
350{
351 DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
352 __func__, ha->host->host_no));
353 init_timer(&ha->timer);
354 ha->timer.expires = jiffies + interval * HZ;
355 ha->timer.data = (unsigned long)ha;
356 ha->timer.function = (void (*)(unsigned long))func;
357 add_timer(&ha->timer);
358 ha->timer_active = 1;
359}
360
361static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
362{
363 del_timer_sync(&ha->timer);
364 ha->timer_active = 0;
365}
366
367/***
368 * qla4xxx_mark_device_missing - mark a device as missing.
369 * @ha: Pointer to host adapter structure.
370 * @ddb_entry: Pointer to device database entry
371 *
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530372 * This routine marks a device missing and close connection.
David Somayajuluafaf5a22006-09-19 10:28:00 -0700373 **/
374void qla4xxx_mark_device_missing(struct scsi_qla_host *ha,
375 struct ddb_entry *ddb_entry)
376{
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530377 if ((atomic_read(&ddb_entry->state) != DDB_STATE_DEAD)) {
378 atomic_set(&ddb_entry->state, DDB_STATE_MISSING);
379 DEBUG2(printk("scsi%ld: ddb [%d] marked MISSING\n",
380 ha->host_no, ddb_entry->fw_ddb_index));
381 } else
382 DEBUG2(printk("scsi%ld: ddb [%d] DEAD\n", ha->host_no,
383 ddb_entry->fw_ddb_index))
384
Mike Christieb6359302008-01-31 13:36:44 -0600385 iscsi_block_session(ddb_entry->sess);
Mike Christiee5bd7b52008-09-24 11:46:10 -0500386 iscsi_conn_error_event(ddb_entry->conn, ISCSI_ERR_CONN_FAILED);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700387}
388
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530389/**
390 * qla4xxx_mark_all_devices_missing - mark all devices as missing.
391 * @ha: Pointer to host adapter structure.
392 *
393 * This routine marks a device missing and resets the relogin retry count.
394 **/
395void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
396{
397 struct ddb_entry *ddb_entry, *ddbtemp;
398 list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
399 qla4xxx_mark_device_missing(ha, ddb_entry);
400 }
401}
402
David Somayajuluafaf5a22006-09-19 10:28:00 -0700403static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
404 struct ddb_entry *ddb_entry,
405 struct scsi_cmnd *cmd,
406 void (*done)(struct scsi_cmnd *))
407{
408 struct srb *srb;
409
410 srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
411 if (!srb)
412 return srb;
413
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530414 kref_init(&srb->srb_ref);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700415 srb->ha = ha;
416 srb->ddb = ddb_entry;
417 srb->cmd = cmd;
418 srb->flags = 0;
Vikas Chaudhary53698872010-04-28 11:41:59 +0530419 CMD_SP(cmd) = (void *)srb;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700420 cmd->scsi_done = done;
421
422 return srb;
423}
424
425static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
426{
427 struct scsi_cmnd *cmd = srb->cmd;
428
429 if (srb->flags & SRB_DMA_VALID) {
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +0900430 scsi_dma_unmap(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700431 srb->flags &= ~SRB_DMA_VALID;
432 }
Vikas Chaudhary53698872010-04-28 11:41:59 +0530433 CMD_SP(cmd) = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700434}
435
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530436void qla4xxx_srb_compl(struct kref *ref)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700437{
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530438 struct srb *srb = container_of(ref, struct srb, srb_ref);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700439 struct scsi_cmnd *cmd = srb->cmd;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530440 struct scsi_qla_host *ha = srb->ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700441
442 qla4xxx_srb_free_dma(ha, srb);
443
444 mempool_free(srb, ha->srb_mempool);
445
446 cmd->scsi_done(cmd);
447}
448
449/**
450 * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
451 * @cmd: Pointer to Linux's SCSI command structure
452 * @done_fn: Function that the driver calls to notify the SCSI mid-layer
453 * that the command has been processed.
454 *
455 * Remarks:
456 * This routine is invoked by Linux to send a SCSI command to the driver.
457 * The mid-level driver tries to ensure that queuecommand never gets
458 * invoked concurrently with itself or the interrupt handler (although
459 * the interrupt handler may call this routine as part of request-
460 * completion handling). Unfortunely, it sometimes calls the scheduler
461 * in interrupt context which is a big NO! NO!.
462 **/
463static int qla4xxx_queuecommand(struct scsi_cmnd *cmd,
464 void (*done)(struct scsi_cmnd *))
465{
466 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
467 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christie7fb19212008-01-31 13:36:45 -0600468 struct iscsi_cls_session *sess = ddb_entry->sess;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700469 struct srb *srb;
470 int rval;
471
Mike Christie7fb19212008-01-31 13:36:45 -0600472 if (!sess) {
473 cmd->result = DID_IMM_RETRY << 16;
474 goto qc_fail_command;
475 }
476
477 rval = iscsi_session_chkready(sess);
478 if (rval) {
479 cmd->result = rval;
480 goto qc_fail_command;
481 }
482
David Somayajuluafaf5a22006-09-19 10:28:00 -0700483 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
484 if (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD) {
485 cmd->result = DID_NO_CONNECT << 16;
486 goto qc_fail_command;
487 }
Mike Christiec5e98e92008-08-17 15:24:39 -0500488 return SCSI_MLQUEUE_TARGET_BUSY;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700489 }
490
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530491 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
492 test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
493 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
494 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
495 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
496 !test_bit(AF_ONLINE, &ha->flags) ||
497 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
David C Somayajulu477ffb92007-01-22 12:26:11 -0800498 goto qc_host_busy;
499
David Somayajuluafaf5a22006-09-19 10:28:00 -0700500 spin_unlock_irq(ha->host->host_lock);
501
502 srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd, done);
503 if (!srb)
504 goto qc_host_busy_lock;
505
506 rval = qla4xxx_send_command_to_isp(ha, srb);
507 if (rval != QLA_SUCCESS)
508 goto qc_host_busy_free_sp;
509
510 spin_lock_irq(ha->host->host_lock);
511 return 0;
512
513qc_host_busy_free_sp:
514 qla4xxx_srb_free_dma(ha, srb);
515 mempool_free(srb, ha->srb_mempool);
516
517qc_host_busy_lock:
518 spin_lock_irq(ha->host->host_lock);
519
520qc_host_busy:
521 return SCSI_MLQUEUE_HOST_BUSY;
522
523qc_fail_command:
524 done(cmd);
525
526 return 0;
527}
528
529/**
530 * qla4xxx_mem_free - frees memory allocated to adapter
531 * @ha: Pointer to host adapter structure.
532 *
533 * Frees memory previously allocated by qla4xxx_mem_alloc
534 **/
535static void qla4xxx_mem_free(struct scsi_qla_host *ha)
536{
537 if (ha->queues)
538 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
539 ha->queues_dma);
540
541 ha->queues_len = 0;
542 ha->queues = NULL;
543 ha->queues_dma = 0;
544 ha->request_ring = NULL;
545 ha->request_dma = 0;
546 ha->response_ring = NULL;
547 ha->response_dma = 0;
548 ha->shadow_regs = NULL;
549 ha->shadow_regs_dma = 0;
550
551 /* Free srb pool. */
552 if (ha->srb_mempool)
553 mempool_destroy(ha->srb_mempool);
554
555 ha->srb_mempool = NULL;
556
557 /* release io space registers */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530558 if (is_qla8022(ha)) {
559 if (ha->nx_pcibase)
560 iounmap(
561 (struct device_reg_82xx __iomem *)ha->nx_pcibase);
562
563 if (ha->nx_db_wr_ptr)
564 iounmap(
565 (struct device_reg_82xx __iomem *)ha->nx_db_wr_ptr);
566 } else if (ha->reg)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700567 iounmap(ha->reg);
568 pci_release_regions(ha->pdev);
569}
570
571/**
572 * qla4xxx_mem_alloc - allocates memory for use by adapter.
573 * @ha: Pointer to host adapter structure
574 *
575 * Allocates DMA memory for request and response queues. Also allocates memory
576 * for srbs.
577 **/
578static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
579{
580 unsigned long align;
581
582 /* Allocate contiguous block of DMA memory for queues. */
583 ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
584 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
585 sizeof(struct shadow_regs) +
586 MEM_ALIGN_VALUE +
587 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
588 ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
589 &ha->queues_dma, GFP_KERNEL);
590 if (ha->queues == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530591 ql4_printk(KERN_WARNING, ha,
592 "Memory Allocation failed - queues.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700593
594 goto mem_alloc_error_exit;
595 }
596 memset(ha->queues, 0, ha->queues_len);
597
598 /*
599 * As per RISC alignment requirements -- the bus-address must be a
600 * multiple of the request-ring size (in bytes).
601 */
602 align = 0;
603 if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
604 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
605 (MEM_ALIGN_VALUE - 1));
606
607 /* Update request and response queue pointers. */
608 ha->request_dma = ha->queues_dma + align;
609 ha->request_ring = (struct queue_entry *) (ha->queues + align);
610 ha->response_dma = ha->queues_dma + align +
611 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
612 ha->response_ring = (struct queue_entry *) (ha->queues + align +
613 (REQUEST_QUEUE_DEPTH *
614 QUEUE_SIZE));
615 ha->shadow_regs_dma = ha->queues_dma + align +
616 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
617 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
618 ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
619 (REQUEST_QUEUE_DEPTH *
620 QUEUE_SIZE) +
621 (RESPONSE_QUEUE_DEPTH *
622 QUEUE_SIZE));
623
624 /* Allocate memory for srb pool. */
625 ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
626 mempool_free_slab, srb_cachep);
627 if (ha->srb_mempool == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530628 ql4_printk(KERN_WARNING, ha,
629 "Memory Allocation failed - SRB Pool.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700630
631 goto mem_alloc_error_exit;
632 }
633
634 return QLA_SUCCESS;
635
636mem_alloc_error_exit:
637 qla4xxx_mem_free(ha);
638 return QLA_ERROR;
639}
640
641/**
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530642 * qla4_8xxx_check_fw_alive - Check firmware health
643 * @ha: Pointer to host adapter structure.
644 *
645 * Context: Interrupt
646 **/
647static void qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
648{
649 uint32_t fw_heartbeat_counter, halt_status;
650
651 fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
652
653 if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
654 ha->seconds_since_last_heartbeat++;
655 /* FW not alive after 2 seconds */
656 if (ha->seconds_since_last_heartbeat == 2) {
657 ha->seconds_since_last_heartbeat = 0;
658 halt_status = qla4_8xxx_rd_32(ha,
659 QLA82XX_PEG_HALT_STATUS1);
660 /* Since we cannot change dev_state in interrupt
661 * context, set appropriate DPC flag then wakeup
662 * DPC */
663 if (halt_status & HALT_STATUS_UNRECOVERABLE)
664 set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
665 else {
666 printk("scsi%ld: %s: detect abort needed!\n",
667 ha->host_no, __func__);
668 set_bit(DPC_RESET_HA, &ha->dpc_flags);
669 }
670 qla4xxx_wake_dpc(ha);
671 }
672 }
673 ha->fw_heartbeat_counter = fw_heartbeat_counter;
674}
675
676/**
677 * qla4_8xxx_watchdog - Poll dev state
678 * @ha: Pointer to host adapter structure.
679 *
680 * Context: Interrupt
681 **/
682void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
683{
684 uint32_t dev_state;
685
686 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
687
688 /* don't poll if reset is going on */
689 if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags)) {
690 if (dev_state == QLA82XX_DEV_NEED_RESET &&
691 !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
692 printk("scsi%ld: %s: HW State: NEED RESET!\n",
693 ha->host_no, __func__);
694 set_bit(DPC_RESET_HA, &ha->dpc_flags);
695 qla4xxx_wake_dpc(ha);
696 } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
697 !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
698 printk("scsi%ld: %s: HW State: NEED QUIES!\n",
699 ha->host_no, __func__);
700 set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
701 qla4xxx_wake_dpc(ha);
702 } else {
703 /* Check firmware health */
704 qla4_8xxx_check_fw_alive(ha);
705 }
706 }
707}
708
709/**
David Somayajuluafaf5a22006-09-19 10:28:00 -0700710 * qla4xxx_timer - checks every second for work to do.
711 * @ha: Pointer to host adapter structure.
712 **/
713static void qla4xxx_timer(struct scsi_qla_host *ha)
714{
715 struct ddb_entry *ddb_entry, *dtemp;
716 int start_dpc = 0;
717
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530718 if (test_bit(AF_HBA_GOING_AWAY, &ha->flags)) {
719 DEBUG2(ql4_printk(KERN_INFO, ha, "%s exited. HBA GOING AWAY\n",
720 __func__));
721 return;
722 }
723
724 if (is_qla8022(ha)) {
725 qla4_8xxx_watchdog(ha);
726 }
727
David Somayajuluafaf5a22006-09-19 10:28:00 -0700728 /* Search for relogin's to time-out and port down retry. */
729 list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
730 /* Count down time between sending relogins */
731 if (adapter_up(ha) &&
732 !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
733 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
734 if (atomic_read(&ddb_entry->retry_relogin_timer) !=
735 INVALID_ENTRY) {
736 if (atomic_read(&ddb_entry->retry_relogin_timer)
737 == 0) {
738 atomic_set(&ddb_entry->
739 retry_relogin_timer,
740 INVALID_ENTRY);
741 set_bit(DPC_RELOGIN_DEVICE,
742 &ha->dpc_flags);
743 set_bit(DF_RELOGIN, &ddb_entry->flags);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530744 DEBUG2(printk("scsi%ld: %s: ddb [%d]"
David Somayajuluafaf5a22006-09-19 10:28:00 -0700745 " login device\n",
746 ha->host_no, __func__,
747 ddb_entry->fw_ddb_index));
748 } else
749 atomic_dec(&ddb_entry->
750 retry_relogin_timer);
751 }
752 }
753
754 /* Wait for relogin to timeout */
755 if (atomic_read(&ddb_entry->relogin_timer) &&
756 (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
757 /*
758 * If the relogin times out and the device is
759 * still NOT ONLINE then try and relogin again.
760 */
761 if (atomic_read(&ddb_entry->state) !=
762 DDB_STATE_ONLINE &&
763 ddb_entry->fw_ddb_device_state ==
764 DDB_DS_SESSION_FAILED) {
765 /* Reset retry relogin timer */
766 atomic_inc(&ddb_entry->relogin_retry_count);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530767 DEBUG2(printk("scsi%ld: ddb [%d] relogin"
David Somayajuluafaf5a22006-09-19 10:28:00 -0700768 " timed out-retrying"
769 " relogin (%d)\n",
770 ha->host_no,
771 ddb_entry->fw_ddb_index,
772 atomic_read(&ddb_entry->
773 relogin_retry_count))
774 );
775 start_dpc++;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530776 DEBUG(printk("scsi%ld:%d:%d: ddb [%d] "
David Somayajuluafaf5a22006-09-19 10:28:00 -0700777 "initate relogin after"
778 " %d seconds\n",
779 ha->host_no, ddb_entry->bus,
780 ddb_entry->target,
781 ddb_entry->fw_ddb_index,
782 ddb_entry->default_time2wait + 4)
783 );
784
785 atomic_set(&ddb_entry->retry_relogin_timer,
786 ddb_entry->default_time2wait + 4);
787 }
788 }
789 }
790
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530791 if (!is_qla8022(ha)) {
792 /* Check for heartbeat interval. */
793 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
794 ha->heartbeat_interval != 0) {
795 ha->seconds_since_last_heartbeat++;
796 if (ha->seconds_since_last_heartbeat >
797 ha->heartbeat_interval + 2)
798 set_bit(DPC_RESET_HA, &ha->dpc_flags);
799 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700800 }
801
David Somayajuluafaf5a22006-09-19 10:28:00 -0700802 /* Wakeup the dpc routine for this adapter, if needed. */
803 if ((start_dpc ||
804 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
805 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
806 test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530807 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -0700808 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
809 test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +0530810 test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530811 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
812 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -0700813 test_bit(DPC_AEN, &ha->dpc_flags)) &&
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530814 !test_bit(AF_DPC_SCHEDULED, &ha->flags) &&
David Somayajuluafaf5a22006-09-19 10:28:00 -0700815 ha->dpc_thread) {
816 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
817 " - dpc flags = 0x%lx\n",
818 ha->host_no, __func__, ha->dpc_flags));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530819 qla4xxx_wake_dpc(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700820 }
821
822 /* Reschedule timer thread to call us back in one second */
823 mod_timer(&ha->timer, jiffies + HZ);
824
825 DEBUG2(ha->seconds_since_last_intr++);
826}
827
828/**
829 * qla4xxx_cmd_wait - waits for all outstanding commands to complete
830 * @ha: Pointer to host adapter structure.
831 *
832 * This routine stalls the driver until all outstanding commands are returned.
833 * Caller must release the Hardware Lock prior to calling this routine.
834 **/
835static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
836{
837 uint32_t index = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700838 unsigned long flags;
839 struct scsi_cmnd *cmd;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700840
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530841 unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
842
843 DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
844 "complete\n", WAIT_CMD_TOV));
845
846 while (!time_after_eq(jiffies, wtime)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700847 spin_lock_irqsave(&ha->hardware_lock, flags);
848 /* Find a command that hasn't completed. */
849 for (index = 0; index < ha->host->can_queue; index++) {
850 cmd = scsi_host_find_tag(ha->host, index);
851 if (cmd != NULL)
852 break;
853 }
854 spin_unlock_irqrestore(&ha->hardware_lock, flags);
855
856 /* If No Commands are pending, wait is complete */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530857 if (index == ha->host->can_queue)
858 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700859
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530860 msleep(1000);
861 }
862 /* If we timed out on waiting for commands to come back
863 * return ERROR. */
864 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700865}
866
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530867int qla4xxx_hw_reset(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700868{
David Somayajuluafaf5a22006-09-19 10:28:00 -0700869 uint32_t ctrl_status;
David C Somayajulu477ffb92007-01-22 12:26:11 -0800870 unsigned long flags = 0;
871
872 DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700873
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530874 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
875 return QLA_ERROR;
876
David Somayajuluafaf5a22006-09-19 10:28:00 -0700877 spin_lock_irqsave(&ha->hardware_lock, flags);
878
879 /*
880 * If the SCSI Reset Interrupt bit is set, clear it.
881 * Otherwise, the Soft Reset won't work.
882 */
883 ctrl_status = readw(&ha->reg->ctrl_status);
884 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
885 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
886
887 /* Issue Soft Reset */
888 writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
889 readl(&ha->reg->ctrl_status);
890
891 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530892 return QLA_SUCCESS;
David C Somayajulu477ffb92007-01-22 12:26:11 -0800893}
894
895/**
896 * qla4xxx_soft_reset - performs soft reset.
897 * @ha: Pointer to host adapter structure.
898 **/
899int qla4xxx_soft_reset(struct scsi_qla_host *ha)
900{
901 uint32_t max_wait_time;
902 unsigned long flags = 0;
903 int status = QLA_ERROR;
904 uint32_t ctrl_status;
905
906 qla4xxx_hw_reset(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700907
908 /* Wait until the Network Reset Intr bit is cleared */
909 max_wait_time = RESET_INTR_TOV;
910 do {
911 spin_lock_irqsave(&ha->hardware_lock, flags);
912 ctrl_status = readw(&ha->reg->ctrl_status);
913 spin_unlock_irqrestore(&ha->hardware_lock, flags);
914
915 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
916 break;
917
918 msleep(1000);
919 } while ((--max_wait_time));
920
921 if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
922 DEBUG2(printk(KERN_WARNING
923 "scsi%ld: Network Reset Intr not cleared by "
924 "Network function, clearing it now!\n",
925 ha->host_no));
926 spin_lock_irqsave(&ha->hardware_lock, flags);
927 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
928 readl(&ha->reg->ctrl_status);
929 spin_unlock_irqrestore(&ha->hardware_lock, flags);
930 }
931
932 /* Wait until the firmware tells us the Soft Reset is done */
933 max_wait_time = SOFT_RESET_TOV;
934 do {
935 spin_lock_irqsave(&ha->hardware_lock, flags);
936 ctrl_status = readw(&ha->reg->ctrl_status);
937 spin_unlock_irqrestore(&ha->hardware_lock, flags);
938
939 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
940 status = QLA_SUCCESS;
941 break;
942 }
943
944 msleep(1000);
945 } while ((--max_wait_time));
946
947 /*
948 * Also, make sure that the SCSI Reset Interrupt bit has been cleared
949 * after the soft reset has taken place.
950 */
951 spin_lock_irqsave(&ha->hardware_lock, flags);
952 ctrl_status = readw(&ha->reg->ctrl_status);
953 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
954 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
955 readl(&ha->reg->ctrl_status);
956 }
957 spin_unlock_irqrestore(&ha->hardware_lock, flags);
958
959 /* If soft reset fails then most probably the bios on other
960 * function is also enabled.
961 * Since the initialization is sequential the other fn
962 * wont be able to acknowledge the soft reset.
963 * Issue a force soft reset to workaround this scenario.
964 */
965 if (max_wait_time == 0) {
966 /* Issue Force Soft Reset */
967 spin_lock_irqsave(&ha->hardware_lock, flags);
968 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
969 readl(&ha->reg->ctrl_status);
970 spin_unlock_irqrestore(&ha->hardware_lock, flags);
971 /* Wait until the firmware tells us the Soft Reset is done */
972 max_wait_time = SOFT_RESET_TOV;
973 do {
974 spin_lock_irqsave(&ha->hardware_lock, flags);
975 ctrl_status = readw(&ha->reg->ctrl_status);
976 spin_unlock_irqrestore(&ha->hardware_lock, flags);
977
978 if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
979 status = QLA_SUCCESS;
980 break;
981 }
982
983 msleep(1000);
984 } while ((--max_wait_time));
985 }
986
987 return status;
988}
989
990/**
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530991 * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
David Somayajuluafaf5a22006-09-19 10:28:00 -0700992 * @ha: Pointer to host adapter structure.
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530993 * @res: returned scsi status
David Somayajuluafaf5a22006-09-19 10:28:00 -0700994 *
995 * This routine is called just prior to a HARD RESET to return all
996 * outstanding commands back to the Operating System.
997 * Caller should make sure that the following locks are released
998 * before this calling routine: Hardware lock, and io_request_lock.
999 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301000static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001001{
1002 struct srb *srb;
1003 int i;
1004 unsigned long flags;
1005
1006 spin_lock_irqsave(&ha->hardware_lock, flags);
1007 for (i = 0; i < ha->host->can_queue; i++) {
1008 srb = qla4xxx_del_from_active_array(ha, i);
1009 if (srb != NULL) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301010 srb->cmd->result = res;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301011 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001012 }
1013 }
1014 spin_unlock_irqrestore(&ha->hardware_lock, flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001015}
1016
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301017void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
1018{
1019 clear_bit(AF_ONLINE, &ha->flags);
1020
1021 /* Disable the board */
1022 ql4_printk(KERN_INFO, ha, "Disabling the board\n");
1023 set_bit(AF_HBA_GOING_AWAY, &ha->flags);
1024
1025 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
1026 qla4xxx_mark_all_devices_missing(ha);
1027 clear_bit(AF_INIT_DONE, &ha->flags);
1028}
1029
David Somayajuluafaf5a22006-09-19 10:28:00 -07001030/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07001031 * qla4xxx_recover_adapter - recovers adapter after a fatal error
1032 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001033 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301034static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001035{
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301036 int status = QLA_ERROR;
1037 uint8_t reset_chip = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001038
1039 /* Stall incoming I/O until we are done */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301040 scsi_block_requests(ha->host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001041 clear_bit(AF_ONLINE, &ha->flags);
Mike Christie50a29ae2008-03-04 13:26:53 -06001042
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301043 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001044
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301045 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001046
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301047 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
1048 reset_chip = 1;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001049
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301050 /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
1051 * do not reset adapter, jump to initialize_adapter */
1052 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1053 status = QLA_SUCCESS;
1054 goto recover_ha_init_adapter;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001055 }
1056
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301057 /* For the ISP-82xx adapter, issue a stop_firmware if invoked
1058 * from eh_host_reset or ioctl module */
1059 if (is_qla8022(ha) && !reset_chip &&
1060 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
1061
1062 DEBUG2(ql4_printk(KERN_INFO, ha,
1063 "scsi%ld: %s - Performing stop_firmware...\n",
1064 ha->host_no, __func__));
1065 status = ha->isp_ops->reset_firmware(ha);
1066 if (status == QLA_SUCCESS) {
1067 qla4xxx_cmd_wait(ha);
1068 ha->isp_ops->disable_intrs(ha);
1069 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1070 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1071 } else {
1072 /* If the stop_firmware fails then
1073 * reset the entire chip */
1074 reset_chip = 1;
1075 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1076 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1077 }
1078 }
1079
1080 /* Issue full chip reset if recovering from a catastrophic error,
1081 * or if stop_firmware fails for ISP-82xx.
1082 * This is the default case for ISP-4xxx */
1083 if (!is_qla8022(ha) || reset_chip) {
1084 qla4xxx_cmd_wait(ha);
1085 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1086 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1087 DEBUG2(ql4_printk(KERN_INFO, ha,
1088 "scsi%ld: %s - Performing chip reset..\n",
1089 ha->host_no, __func__));
1090 status = ha->isp_ops->reset_chip(ha);
1091 }
1092
1093 /* Flush any pending ddb changed AENs */
1094 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1095
1096recover_ha_init_adapter:
1097 /* Upon successful firmware/chip reset, re-initialize the adapter */
1098 if (status == QLA_SUCCESS) {
1099 /* For ISP-4xxx, force function 1 to always initialize
1100 * before function 3 to prevent both funcions from
1101 * stepping on top of the other */
1102 if (!is_qla8022(ha) && (ha->mac_index == 3))
1103 ssleep(6);
1104
1105 /* NOTE: AF_ONLINE flag set upon successful completion of
1106 * qla4xxx_initialize_adapter */
1107 status = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
1108 }
1109
1110 /* Retry failed adapter initialization, if necessary
1111 * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
1112 * case to prevent ping-pong resets between functions */
1113 if (!test_bit(AF_ONLINE, &ha->flags) &&
1114 !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001115 /* Adapter initialization failed, see if we can retry
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301116 * resetting the ha.
1117 * Since we don't want to block the DPC for too long
1118 * with multiple resets in the same thread,
1119 * utilize DPC to retry */
David Somayajuluafaf5a22006-09-19 10:28:00 -07001120 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
1121 ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
1122 DEBUG2(printk("scsi%ld: recover adapter - retrying "
1123 "(%d) more times\n", ha->host_no,
1124 ha->retry_reset_ha_cnt));
1125 set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1126 status = QLA_ERROR;
1127 } else {
1128 if (ha->retry_reset_ha_cnt > 0) {
1129 /* Schedule another Reset HA--DPC will retry */
1130 ha->retry_reset_ha_cnt--;
1131 DEBUG2(printk("scsi%ld: recover adapter - "
1132 "retry remaining %d\n",
1133 ha->host_no,
1134 ha->retry_reset_ha_cnt));
1135 status = QLA_ERROR;
1136 }
1137
1138 if (ha->retry_reset_ha_cnt == 0) {
1139 /* Recover adapter retries have been exhausted.
1140 * Adapter DEAD */
1141 DEBUG2(printk("scsi%ld: recover adapter "
1142 "failed - board disabled\n",
1143 ha->host_no));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301144 qla4xxx_dead_adapter_cleanup(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001145 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1146 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301147 clear_bit(DPC_RESET_HA_FW_CONTEXT,
David Somayajuluafaf5a22006-09-19 10:28:00 -07001148 &ha->dpc_flags);
1149 status = QLA_ERROR;
1150 }
1151 }
1152 } else {
1153 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301154 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001155 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1156 }
1157
1158 ha->adapter_error_count++;
1159
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301160 if (test_bit(AF_ONLINE, &ha->flags))
1161 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001162
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301163 scsi_unblock_requests(ha->host);
1164
1165 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
1166 DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
1167 status == QLA_ERROR ? "FAILED" : "SUCCEDED"));
1168
David Somayajuluafaf5a22006-09-19 10:28:00 -07001169 return status;
1170}
1171
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301172void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
1173{
1174 if (ha->dpc_thread &&
1175 !test_bit(AF_DPC_SCHEDULED, &ha->flags)) {
1176 set_bit(AF_DPC_SCHEDULED, &ha->flags);
1177 queue_work(ha->dpc_thread, &ha->dpc_work);
1178 }
1179}
1180
David Somayajuluafaf5a22006-09-19 10:28:00 -07001181/**
1182 * qla4xxx_do_dpc - dpc routine
1183 * @data: in our case pointer to adapter structure
1184 *
1185 * This routine is a task that is schedule by the interrupt handler
1186 * to perform the background processing for interrupts. We put it
1187 * on a task queue that is consumed whenever the scheduler runs; that's
1188 * so you can do anything (i.e. put the process to sleep etc). In fact,
1189 * the mid-level tries to sleep when it reaches the driver threshold
1190 * "host->can_queue". This can cause a panic if we were in our interrupt code.
1191 **/
David Howellsc4028952006-11-22 14:57:56 +00001192static void qla4xxx_do_dpc(struct work_struct *work)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001193{
David Howellsc4028952006-11-22 14:57:56 +00001194 struct scsi_qla_host *ha =
1195 container_of(work, struct scsi_qla_host, dpc_work);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001196 struct ddb_entry *ddb_entry, *dtemp;
David C Somayajulu477ffb92007-01-22 12:26:11 -08001197 int status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001198
David C Somayajuluf26b9042006-11-15 16:41:09 -08001199 DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301200 "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
1201 ha->host_no, __func__, ha->flags, ha->dpc_flags))
David Somayajuluafaf5a22006-09-19 10:28:00 -07001202
1203 /* Initialization not yet finished. Don't do anything yet. */
1204 if (!test_bit(AF_INIT_DONE, &ha->flags))
1205 return;
1206
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301207 /* HBA is in the process of being permanently disabled.
1208 * Don't process anything */
1209 if (test_bit(AF_HBA_GOING_AWAY, &ha->flags))
1210 return;
1211
1212 if (is_qla8022(ha)) {
1213 if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
1214 qla4_8xxx_idc_lock(ha);
1215 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
1216 QLA82XX_DEV_FAILED);
1217 qla4_8xxx_idc_unlock(ha);
1218 ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
1219 qla4_8xxx_device_state_handler(ha);
1220 }
1221 if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
1222 qla4_8xxx_need_qsnt_handler(ha);
1223 }
1224 }
1225
1226 if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
1227 (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07001228 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301229 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
1230 if (ql4xdontresethba) {
1231 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
1232 ha->host_no, __func__));
1233 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
1234 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1235 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1236 goto dpc_post_reset_ha;
1237 }
1238 if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
1239 test_bit(DPC_RESET_HA, &ha->dpc_flags))
1240 qla4xxx_recover_adapter(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001241
David C Somayajulu477ffb92007-01-22 12:26:11 -08001242 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001243 uint8_t wait_time = RESET_INTR_TOV;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001244
David Somayajuluafaf5a22006-09-19 10:28:00 -07001245 while ((readw(&ha->reg->ctrl_status) &
1246 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
1247 if (--wait_time == 0)
1248 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001249 msleep(1000);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001250 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001251 if (wait_time == 0)
1252 DEBUG2(printk("scsi%ld: %s: SR|FSR "
1253 "bit not cleared-- resetting\n",
1254 ha->host_no, __func__));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301255 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
David C Somayajulu477ffb92007-01-22 12:26:11 -08001256 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
1257 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301258 status = qla4xxx_recover_adapter(ha);
David C Somayajulu477ffb92007-01-22 12:26:11 -08001259 }
1260 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1261 if (status == QLA_SUCCESS)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301262 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001263 }
1264 }
1265
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301266dpc_post_reset_ha:
David Somayajuluafaf5a22006-09-19 10:28:00 -07001267 /* ---- process AEN? --- */
1268 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
1269 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
1270
1271 /* ---- Get DHCP IP Address? --- */
1272 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
1273 qla4xxx_get_dhcp_ip_address(ha);
1274
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301275 /* ---- link change? --- */
1276 if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
1277 if (!test_bit(AF_LINK_UP, &ha->flags)) {
1278 /* ---- link down? --- */
1279 list_for_each_entry_safe(ddb_entry, dtemp,
1280 &ha->ddb_list, list) {
1281 if (atomic_read(&ddb_entry->state) ==
1282 DDB_STATE_ONLINE)
1283 qla4xxx_mark_device_missing(ha,
1284 ddb_entry);
1285 }
1286 } else {
1287 /* ---- link up? --- *
1288 * F/W will auto login to all devices ONLY ONCE after
1289 * link up during driver initialization and runtime
1290 * fatal error recovery. Therefore, the driver must
1291 * manually relogin to devices when recovering from
1292 * connection failures, logouts, expired KATO, etc. */
1293
1294 list_for_each_entry_safe(ddb_entry, dtemp,
1295 &ha->ddb_list, list) {
1296 if ((atomic_read(&ddb_entry->state) ==
1297 DDB_STATE_MISSING) ||
1298 (atomic_read(&ddb_entry->state) ==
1299 DDB_STATE_DEAD)) {
1300 if (ddb_entry->fw_ddb_device_state ==
1301 DDB_DS_SESSION_ACTIVE) {
1302 atomic_set(&ddb_entry->state,
1303 DDB_STATE_ONLINE);
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301304 ql4_printk(KERN_INFO, ha,
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301305 "scsi%ld: %s: ddb[%d]"
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301306 " marked ONLINE\n",
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301307 ha->host_no, __func__,
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301308 ddb_entry->fw_ddb_index);
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301309
1310 iscsi_unblock_session(
1311 ddb_entry->sess);
1312 } else
1313 qla4xxx_relogin_device(
1314 ha, ddb_entry);
1315 }
1316
1317 }
1318 }
1319 }
1320
David Somayajuluafaf5a22006-09-19 10:28:00 -07001321 /* ---- relogin device? --- */
1322 if (adapter_up(ha) &&
1323 test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
1324 list_for_each_entry_safe(ddb_entry, dtemp,
1325 &ha->ddb_list, list) {
1326 if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
1327 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
1328 qla4xxx_relogin_device(ha, ddb_entry);
1329
1330 /*
1331 * If mbx cmd times out there is no point
1332 * in continuing further.
1333 * With large no of targets this can hang
1334 * the system.
1335 */
1336 if (test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
1337 printk(KERN_WARNING "scsi%ld: %s: "
1338 "need to reset hba\n",
1339 ha->host_no, __func__);
1340 break;
1341 }
1342 }
1343 }
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301344 clear_bit(AF_DPC_SCHEDULED, &ha->flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001345}
1346
1347/**
1348 * qla4xxx_free_adapter - release the adapter
1349 * @ha: pointer to adapter structure
1350 **/
1351static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
1352{
1353
1354 if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
1355 /* Turn-off interrupts on the card. */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301356 ha->isp_ops->disable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001357 }
1358
David Somayajuluafaf5a22006-09-19 10:28:00 -07001359 /* Remove timer thread, if present */
1360 if (ha->timer_active)
1361 qla4xxx_stop_timer(ha);
1362
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301363 /* Kill the kernel thread for this host */
1364 if (ha->dpc_thread)
1365 destroy_workqueue(ha->dpc_thread);
1366
1367 /* Put firmware in known state */
1368 ha->isp_ops->reset_firmware(ha);
1369
1370 if (is_qla8022(ha)) {
1371 qla4_8xxx_idc_lock(ha);
1372 qla4_8xxx_clear_drv_active(ha);
1373 qla4_8xxx_idc_unlock(ha);
1374 }
1375
David Somayajuluafaf5a22006-09-19 10:28:00 -07001376 /* Detach interrupts */
1377 if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301378 qla4xxx_free_irqs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001379
David C Somayajulubee4fe82007-05-23 18:03:32 -07001380 /* free extra memory */
1381 qla4xxx_mem_free(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301382}
David C Somayajulubee4fe82007-05-23 18:03:32 -07001383
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301384int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
1385{
1386 int status = 0;
1387 uint8_t revision_id;
1388 unsigned long mem_base, mem_len, db_base, db_len;
1389 struct pci_dev *pdev = ha->pdev;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001390
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301391 status = pci_request_regions(pdev, DRIVER_NAME);
1392 if (status) {
1393 printk(KERN_WARNING
1394 "scsi(%ld) Failed to reserve PIO regions (%s) "
1395 "status=%d\n", ha->host_no, pci_name(pdev), status);
1396 goto iospace_error_exit;
1397 }
1398
1399 pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
1400 DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
1401 __func__, revision_id));
1402 ha->revision_id = revision_id;
1403
1404 /* remap phys address */
1405 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
1406 mem_len = pci_resource_len(pdev, 0);
1407 DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
1408 __func__, mem_base, mem_len));
1409
1410 /* mapping of pcibase pointer */
1411 ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
1412 if (!ha->nx_pcibase) {
1413 printk(KERN_ERR
1414 "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
1415 pci_release_regions(ha->pdev);
1416 goto iospace_error_exit;
1417 }
1418
1419 /* Mapping of IO base pointer, door bell read and write pointer */
1420
1421 /* mapping of IO base pointer */
1422 ha->qla4_8xxx_reg =
1423 (struct device_reg_82xx __iomem *)((uint8_t *)ha->nx_pcibase +
1424 0xbc000 + (ha->pdev->devfn << 11));
1425
1426 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
1427 db_len = pci_resource_len(pdev, 4);
1428
1429 /* mapping of doorbell write pointer */
1430 ha->nx_db_wr_ptr = (unsigned long)ioremap(db_base +
1431 (ha->pdev->devfn << 12), 4);
1432 if (!ha->nx_db_wr_ptr) {
1433 printk(KERN_ERR
1434 "cannot remap MMIO doorbell-write (%s), aborting\n",
1435 pci_name(pdev));
1436 goto iospace_error_exit;
1437 }
1438 /* mapping of doorbell read pointer */
1439 ha->nx_db_rd_ptr = (uint8_t *) ha->nx_pcibase + (512 * 1024) +
1440 (ha->pdev->devfn * 8);
1441 if (!ha->nx_db_rd_ptr)
1442 printk(KERN_ERR
1443 "cannot remap MMIO doorbell-read (%s), aborting\n",
1444 pci_name(pdev));
1445 return 0;
1446
1447iospace_error_exit:
1448 return -ENOMEM;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001449}
1450
1451/***
1452 * qla4xxx_iospace_config - maps registers
1453 * @ha: pointer to adapter structure
1454 *
1455 * This routines maps HBA's registers from the pci address space
1456 * into the kernel virtual address space for memory mapped i/o.
1457 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301458int qla4xxx_iospace_config(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001459{
1460 unsigned long pio, pio_len, pio_flags;
1461 unsigned long mmio, mmio_len, mmio_flags;
1462
1463 pio = pci_resource_start(ha->pdev, 0);
1464 pio_len = pci_resource_len(ha->pdev, 0);
1465 pio_flags = pci_resource_flags(ha->pdev, 0);
1466 if (pio_flags & IORESOURCE_IO) {
1467 if (pio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301468 ql4_printk(KERN_WARNING, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07001469 "Invalid PCI I/O region size\n");
1470 pio = 0;
1471 }
1472 } else {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301473 ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001474 pio = 0;
1475 }
1476
1477 /* Use MMIO operations for all accesses. */
1478 mmio = pci_resource_start(ha->pdev, 1);
1479 mmio_len = pci_resource_len(ha->pdev, 1);
1480 mmio_flags = pci_resource_flags(ha->pdev, 1);
1481
1482 if (!(mmio_flags & IORESOURCE_MEM)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301483 ql4_printk(KERN_ERR, ha,
1484 "region #0 not an MMIO resource, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001485
1486 goto iospace_error_exit;
1487 }
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301488
David Somayajuluafaf5a22006-09-19 10:28:00 -07001489 if (mmio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301490 ql4_printk(KERN_ERR, ha,
1491 "Invalid PCI mem region size, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001492 goto iospace_error_exit;
1493 }
1494
1495 if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301496 ql4_printk(KERN_WARNING, ha,
1497 "Failed to reserve PIO/MMIO regions\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001498
1499 goto iospace_error_exit;
1500 }
1501
1502 ha->pio_address = pio;
1503 ha->pio_length = pio_len;
1504 ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
1505 if (!ha->reg) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301506 ql4_printk(KERN_ERR, ha,
1507 "cannot remap MMIO, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001508
1509 goto iospace_error_exit;
1510 }
1511
1512 return 0;
1513
1514iospace_error_exit:
1515 return -ENOMEM;
1516}
1517
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301518static struct isp_operations qla4xxx_isp_ops = {
1519 .iospace_config = qla4xxx_iospace_config,
1520 .pci_config = qla4xxx_pci_config,
1521 .disable_intrs = qla4xxx_disable_intrs,
1522 .enable_intrs = qla4xxx_enable_intrs,
1523 .start_firmware = qla4xxx_start_firmware,
1524 .intr_handler = qla4xxx_intr_handler,
1525 .interrupt_service_routine = qla4xxx_interrupt_service_routine,
1526 .reset_chip = qla4xxx_soft_reset,
1527 .reset_firmware = qla4xxx_hw_reset,
1528 .queue_iocb = qla4xxx_queue_iocb,
1529 .complete_iocb = qla4xxx_complete_iocb,
1530 .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
1531 .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
1532 .get_sys_info = qla4xxx_get_sys_info,
1533};
1534
1535static struct isp_operations qla4_8xxx_isp_ops = {
1536 .iospace_config = qla4_8xxx_iospace_config,
1537 .pci_config = qla4_8xxx_pci_config,
1538 .disable_intrs = qla4_8xxx_disable_intrs,
1539 .enable_intrs = qla4_8xxx_enable_intrs,
1540 .start_firmware = qla4_8xxx_load_risc,
1541 .intr_handler = qla4_8xxx_intr_handler,
1542 .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
1543 .reset_chip = qla4_8xxx_isp_reset,
1544 .reset_firmware = qla4_8xxx_stop_firmware,
1545 .queue_iocb = qla4_8xxx_queue_iocb,
1546 .complete_iocb = qla4_8xxx_complete_iocb,
1547 .rd_shdw_req_q_out = qla4_8xxx_rd_shdw_req_q_out,
1548 .rd_shdw_rsp_q_in = qla4_8xxx_rd_shdw_rsp_q_in,
1549 .get_sys_info = qla4_8xxx_get_sys_info,
1550};
1551
1552uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
1553{
1554 return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
1555}
1556
1557uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
1558{
1559 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
1560}
1561
1562uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
1563{
1564 return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
1565}
1566
1567uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
1568{
1569 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
1570}
1571
David Somayajuluafaf5a22006-09-19 10:28:00 -07001572/**
1573 * qla4xxx_probe_adapter - callback function to probe HBA
1574 * @pdev: pointer to pci_dev structure
1575 * @pci_device_id: pointer to pci_device entry
1576 *
1577 * This routine will probe for Qlogic 4xxx iSCSI host adapters.
1578 * It returns zero if successful. It also initializes all data necessary for
1579 * the driver.
1580 **/
1581static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
1582 const struct pci_device_id *ent)
1583{
1584 int ret = -ENODEV, status;
1585 struct Scsi_Host *host;
1586 struct scsi_qla_host *ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001587 uint8_t init_retry_count = 0;
1588 char buf[34];
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301589 struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001590
1591 if (pci_enable_device(pdev))
1592 return -1;
1593
1594 host = scsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha));
1595 if (host == NULL) {
1596 printk(KERN_WARNING
1597 "qla4xxx: Couldn't allocate host from scsi layer!\n");
1598 goto probe_disable_device;
1599 }
1600
1601 /* Clear our data area */
1602 ha = (struct scsi_qla_host *) host->hostdata;
1603 memset(ha, 0, sizeof(*ha));
1604
1605 /* Save the information from PCI BIOS. */
1606 ha->pdev = pdev;
1607 ha->host = host;
1608 ha->host_no = host->host_no;
1609
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301610 /* Setup Runtime configurable options */
1611 if (is_qla8022(ha)) {
1612 ha->isp_ops = &qla4_8xxx_isp_ops;
1613 rwlock_init(&ha->hw_lock);
1614 ha->qdr_sn_window = -1;
1615 ha->ddr_mn_window = -1;
1616 ha->curr_window = 255;
1617 ha->func_num = PCI_FUNC(ha->pdev->devfn);
1618 nx_legacy_intr = &legacy_intr[ha->func_num];
1619 ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
1620 ha->nx_legacy_intr.tgt_status_reg =
1621 nx_legacy_intr->tgt_status_reg;
1622 ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
1623 ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
1624 } else {
1625 ha->isp_ops = &qla4xxx_isp_ops;
1626 }
1627
David Somayajuluafaf5a22006-09-19 10:28:00 -07001628 /* Configure PCI I/O space. */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301629 ret = ha->isp_ops->iospace_config(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001630 if (ret)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301631 goto probe_failed_ioconfig;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001632
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301633 ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
David Somayajuluafaf5a22006-09-19 10:28:00 -07001634 pdev->device, pdev->irq, ha->reg);
1635
1636 qla4xxx_config_dma_addressing(ha);
1637
1638 /* Initialize lists and spinlocks. */
1639 INIT_LIST_HEAD(&ha->ddb_list);
1640 INIT_LIST_HEAD(&ha->free_srb_q);
1641
1642 mutex_init(&ha->mbox_sem);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301643 init_completion(&ha->mbx_intr_comp);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001644
1645 spin_lock_init(&ha->hardware_lock);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001646
1647 /* Allocate dma buffers */
1648 if (qla4xxx_mem_alloc(ha)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301649 ql4_printk(KERN_WARNING, ha,
1650 "[ERROR] Failed to allocate memory for adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001651
1652 ret = -ENOMEM;
1653 goto probe_failed;
1654 }
1655
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301656 if (is_qla8022(ha))
1657 (void) qla4_8xxx_get_flash_info(ha);
1658
David Somayajuluafaf5a22006-09-19 10:28:00 -07001659 /*
1660 * Initialize the Host adapter request/response queues and
1661 * firmware
1662 * NOTE: interrupts enabled upon successful completion
1663 */
1664 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301665 while ((!test_bit(AF_ONLINE, &ha->flags)) &&
1666 init_retry_count++ < MAX_INIT_RETRIES) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001667 DEBUG2(printk("scsi: %s: retrying adapter initialization "
1668 "(%d)\n", __func__, init_retry_count));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301669
1670 if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
1671 continue;
1672
David Somayajuluafaf5a22006-09-19 10:28:00 -07001673 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1674 }
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301675
1676 if (!test_bit(AF_ONLINE, &ha->flags)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301677 ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001678
1679 ret = -ENODEV;
1680 goto probe_failed;
1681 }
1682
1683 host->cmd_per_lun = 3;
1684 host->max_channel = 0;
1685 host->max_lun = MAX_LUNS - 1;
1686 host->max_id = MAX_TARGETS;
1687 host->max_cmd_len = IOCB_MAX_CDB_LEN;
1688 host->can_queue = MAX_SRBS ;
1689 host->transportt = qla4xxx_scsi_transport;
1690
1691 ret = scsi_init_shared_tag_map(host, MAX_SRBS);
1692 if (ret) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301693 ql4_printk(KERN_WARNING, ha,
1694 "scsi_init_shared_tag_map failed\n");
1695 goto probe_failed;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001696 }
1697
1698 /* Startup the kernel thread for this host adapter. */
1699 DEBUG2(printk("scsi: %s: Starting kernel thread for "
1700 "qla4xxx_dpc\n", __func__));
1701 sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
1702 ha->dpc_thread = create_singlethread_workqueue(buf);
1703 if (!ha->dpc_thread) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301704 ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001705 ret = -ENODEV;
1706 goto probe_failed;
1707 }
David Howellsc4028952006-11-22 14:57:56 +00001708 INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001709
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301710 /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
1711 * (which is called indirectly by qla4xxx_initialize_adapter),
1712 * so that irqs will be registered after crbinit but before
1713 * mbx_intr_enable.
1714 */
1715 if (!is_qla8022(ha)) {
1716 ret = qla4xxx_request_irqs(ha);
1717 if (ret) {
1718 ql4_printk(KERN_WARNING, ha, "Failed to reserve "
1719 "interrupt %d already in use.\n", pdev->irq);
1720 goto probe_failed;
1721 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001722 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001723
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301724 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001725
1726 /* Start timer thread. */
1727 qla4xxx_start_timer(ha, qla4xxx_timer, 1);
1728
1729 set_bit(AF_INIT_DONE, &ha->flags);
1730
1731 pci_set_drvdata(pdev, ha);
1732
1733 ret = scsi_add_host(host, &pdev->dev);
1734 if (ret)
1735 goto probe_failed;
1736
David Somayajuluafaf5a22006-09-19 10:28:00 -07001737 printk(KERN_INFO
1738 " QLogic iSCSI HBA Driver version: %s\n"
1739 " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
1740 qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
1741 ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
1742 ha->patch_number, ha->build_number);
Mike Christie921601b2008-01-31 13:36:49 -06001743 scsi_scan_host(host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001744 return 0;
1745
David Somayajuluafaf5a22006-09-19 10:28:00 -07001746probe_failed:
1747 qla4xxx_free_adapter(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301748
1749probe_failed_ioconfig:
David Somayajuluafaf5a22006-09-19 10:28:00 -07001750 scsi_host_put(ha->host);
1751
1752probe_disable_device:
1753 pci_disable_device(pdev);
1754
1755 return ret;
1756}
1757
1758/**
1759 * qla4xxx_remove_adapter - calback function to remove adapter.
1760 * @pci_dev: PCI device pointer
1761 **/
1762static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
1763{
1764 struct scsi_qla_host *ha;
1765
1766 ha = pci_get_drvdata(pdev);
1767
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301768 set_bit(AF_HBA_GOING_AWAY, &ha->flags);
David C Somayajulubee4fe82007-05-23 18:03:32 -07001769
David Somayajuluafaf5a22006-09-19 10:28:00 -07001770 /* remove devs from iscsi_sessions to scsi_devices */
1771 qla4xxx_free_ddb_list(ha);
1772
1773 scsi_remove_host(ha->host);
1774
1775 qla4xxx_free_adapter(ha);
1776
1777 scsi_host_put(ha->host);
1778
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301779 pci_disable_device(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001780 pci_set_drvdata(pdev, NULL);
1781}
1782
1783/**
1784 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
1785 * @ha: HA context
1786 *
1787 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
1788 * supported addressing method.
1789 */
Adrian Bunk47975472007-04-26 00:35:16 -07001790static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001791{
1792 int retval;
1793
1794 /* Update our PCI device dma_mask for full 64 bit mask */
Yang Hongyang6a355282009-04-06 19:01:13 -07001795 if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
1796 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001797 dev_dbg(&ha->pdev->dev,
1798 "Failed to set 64 bit PCI consistent mask; "
1799 "using 32 bit.\n");
1800 retval = pci_set_consistent_dma_mask(ha->pdev,
Yang Hongyang284901a2009-04-06 19:01:15 -07001801 DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001802 }
1803 } else
Yang Hongyang284901a2009-04-06 19:01:15 -07001804 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001805}
1806
1807static int qla4xxx_slave_alloc(struct scsi_device *sdev)
1808{
1809 struct iscsi_cls_session *sess = starget_to_session(sdev->sdev_target);
1810 struct ddb_entry *ddb = sess->dd_data;
1811
1812 sdev->hostdata = ddb;
1813 sdev->tagged_supported = 1;
Mike Christied510d962008-07-11 19:50:33 -05001814 scsi_activate_tcq(sdev, QL4_DEF_QDEPTH);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001815 return 0;
1816}
1817
1818static int qla4xxx_slave_configure(struct scsi_device *sdev)
1819{
1820 sdev->tagged_supported = 1;
1821 return 0;
1822}
1823
1824static void qla4xxx_slave_destroy(struct scsi_device *sdev)
1825{
1826 scsi_deactivate_tcq(sdev, 1);
1827}
1828
1829/**
1830 * qla4xxx_del_from_active_array - returns an active srb
1831 * @ha: Pointer to host adapter structure.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001832 * @index: index into the active_array
David Somayajuluafaf5a22006-09-19 10:28:00 -07001833 *
1834 * This routine removes and returns the srb at the specified index
1835 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301836struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
1837 uint32_t index)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001838{
1839 struct srb *srb = NULL;
Vikas Chaudhary53698872010-04-28 11:41:59 +05301840 struct scsi_cmnd *cmd = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001841
Vikas Chaudhary53698872010-04-28 11:41:59 +05301842 cmd = scsi_host_find_tag(ha->host, index);
1843 if (!cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001844 return srb;
1845
Vikas Chaudhary53698872010-04-28 11:41:59 +05301846 srb = (struct srb *)CMD_SP(cmd);
1847 if (!srb)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001848 return srb;
1849
1850 /* update counters */
1851 if (srb->flags & SRB_DMA_VALID) {
1852 ha->req_q_count += srb->iocb_cnt;
1853 ha->iocb_cnt -= srb->iocb_cnt;
1854 if (srb->cmd)
Vikas Chaudhary53698872010-04-28 11:41:59 +05301855 srb->cmd->host_scribble =
1856 (unsigned char *)(unsigned long) MAX_SRBS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001857 }
1858 return srb;
1859}
1860
1861/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07001862 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301863 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001864 * @cmd: Scsi Command to wait on.
1865 *
1866 * This routine waits for the command to be returned by the Firmware
1867 * for some max time.
1868 **/
1869static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
1870 struct scsi_cmnd *cmd)
1871{
1872 int done = 0;
1873 struct srb *rp;
1874 uint32_t max_wait_time = EH_WAIT_CMD_TOV;
1875
1876 do {
1877 /* Checking to see if its returned to OS */
Vikas Chaudhary53698872010-04-28 11:41:59 +05301878 rp = (struct srb *) CMD_SP(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001879 if (rp == NULL) {
1880 done++;
1881 break;
1882 }
1883
1884 msleep(2000);
1885 } while (max_wait_time--);
1886
1887 return done;
1888}
1889
1890/**
1891 * qla4xxx_wait_for_hba_online - waits for HBA to come online
1892 * @ha: Pointer to host adapter structure
1893 **/
1894static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
1895{
1896 unsigned long wait_online;
1897
1898 wait_online = jiffies + (30 * HZ);
1899 while (time_before(jiffies, wait_online)) {
1900
1901 if (adapter_up(ha))
1902 return QLA_SUCCESS;
1903 else if (ha->retry_reset_ha_cnt == 0)
1904 return QLA_ERROR;
1905
1906 msleep(2000);
1907 }
1908
1909 return QLA_ERROR;
1910}
1911
1912/**
Mike Christiece545032008-02-29 18:25:20 -06001913 * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001914 * @ha: pointer to HBA
David Somayajuluafaf5a22006-09-19 10:28:00 -07001915 * @t: target id
1916 * @l: lun id
1917 *
1918 * This function waits for all outstanding commands to a lun to complete. It
1919 * returns 0 if all pending commands are returned and 1 otherwise.
1920 **/
Mike Christiece545032008-02-29 18:25:20 -06001921static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
1922 struct scsi_target *stgt,
1923 struct scsi_device *sdev)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001924{
1925 int cnt;
1926 int status = 0;
1927 struct scsi_cmnd *cmd;
1928
1929 /*
Mike Christiece545032008-02-29 18:25:20 -06001930 * Waiting for all commands for the designated target or dev
1931 * in the active array
David Somayajuluafaf5a22006-09-19 10:28:00 -07001932 */
1933 for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
1934 cmd = scsi_host_find_tag(ha->host, cnt);
Mike Christiece545032008-02-29 18:25:20 -06001935 if (cmd && stgt == scsi_target(cmd->device) &&
1936 (!sdev || sdev == cmd->device)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001937 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
1938 status++;
1939 break;
1940 }
1941 }
1942 }
1943 return status;
1944}
1945
1946/**
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301947 * qla4xxx_eh_abort - callback for abort task.
1948 * @cmd: Pointer to Linux's SCSI command structure
1949 *
1950 * This routine is called by the Linux OS to abort the specified
1951 * command.
1952 **/
1953static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
1954{
1955 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
1956 unsigned int id = cmd->device->id;
1957 unsigned int lun = cmd->device->lun;
1958 unsigned long serial = cmd->serial_number;
1959 struct srb *srb = NULL;
1960 int ret = SUCCESS;
1961 int wait = 0;
1962
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301963 ql4_printk(KERN_INFO, ha,
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301964 "scsi%ld:%d:%d: Abort command issued cmd=%p, pid=%ld\n",
1965 ha->host_no, id, lun, cmd, serial);
1966
1967 srb = (struct srb *) CMD_SP(cmd);
1968
1969 if (!srb)
1970 return SUCCESS;
1971
1972 kref_get(&srb->srb_ref);
1973
1974 if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
1975 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
1976 ha->host_no, id, lun));
1977 ret = FAILED;
1978 } else {
1979 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
1980 ha->host_no, id, lun));
1981 wait = 1;
1982 }
1983
1984 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
1985
1986 /* Wait for command to complete */
1987 if (wait) {
1988 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
1989 DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
1990 ha->host_no, id, lun));
1991 ret = FAILED;
1992 }
1993 }
1994
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301995 ql4_printk(KERN_INFO, ha,
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301996 "scsi%ld:%d:%d: Abort command - %s\n",
1997 ha->host_no, id, lun, (ret == SUCCESS) ? "succeded" : "failed");
1998
1999 return ret;
2000}
2001
2002/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002003 * qla4xxx_eh_device_reset - callback for target reset.
2004 * @cmd: Pointer to Linux's SCSI command structure
2005 *
2006 * This routine is called by the Linux OS to reset all luns on the
2007 * specified target.
2008 **/
2009static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
2010{
2011 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2012 struct ddb_entry *ddb_entry = cmd->device->hostdata;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002013 int ret = FAILED, stat;
2014
Karen Higgins612f73482009-07-15 15:03:01 -05002015 if (!ddb_entry)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002016 return ret;
2017
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302018 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002019 "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
2020 cmd->device->channel, cmd->device->id, cmd->device->lun);
2021
2022 DEBUG2(printk(KERN_INFO
2023 "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
2024 "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
Jens Axboe242f9dc2008-09-14 05:55:09 -07002025 cmd, jiffies, cmd->request->timeout / HZ,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002026 ha->dpc_flags, cmd->result, cmd->allowed));
2027
2028 /* FIXME: wait for hba to go online */
2029 stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
2030 if (stat != QLA_SUCCESS) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302031 ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002032 goto eh_dev_reset_done;
2033 }
2034
Mike Christiece545032008-02-29 18:25:20 -06002035 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2036 cmd->device)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302037 ql4_printk(KERN_INFO, ha,
Mike Christiece545032008-02-29 18:25:20 -06002038 "DEVICE RESET FAILED - waiting for "
2039 "commands.\n");
2040 goto eh_dev_reset_done;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002041 }
2042
David C Somayajulu9d562912008-03-19 11:23:03 -07002043 /* Send marker. */
2044 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2045 MM_LUN_RESET) != QLA_SUCCESS)
2046 goto eh_dev_reset_done;
2047
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302048 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002049 "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
2050 ha->host_no, cmd->device->channel, cmd->device->id,
2051 cmd->device->lun);
2052
2053 ret = SUCCESS;
2054
2055eh_dev_reset_done:
2056
2057 return ret;
2058}
2059
2060/**
Mike Christiece545032008-02-29 18:25:20 -06002061 * qla4xxx_eh_target_reset - callback for target reset.
2062 * @cmd: Pointer to Linux's SCSI command structure
2063 *
2064 * This routine is called by the Linux OS to reset the target.
2065 **/
2066static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
2067{
2068 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2069 struct ddb_entry *ddb_entry = cmd->device->hostdata;
2070 int stat;
2071
2072 if (!ddb_entry)
2073 return FAILED;
2074
2075 starget_printk(KERN_INFO, scsi_target(cmd->device),
2076 "WARM TARGET RESET ISSUED.\n");
2077
2078 DEBUG2(printk(KERN_INFO
2079 "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
2080 "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
Jens Axboe242f9dc2008-09-14 05:55:09 -07002081 ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
Mike Christiece545032008-02-29 18:25:20 -06002082 ha->dpc_flags, cmd->result, cmd->allowed));
2083
2084 stat = qla4xxx_reset_target(ha, ddb_entry);
2085 if (stat != QLA_SUCCESS) {
2086 starget_printk(KERN_INFO, scsi_target(cmd->device),
2087 "WARM TARGET RESET FAILED.\n");
2088 return FAILED;
2089 }
2090
Mike Christiece545032008-02-29 18:25:20 -06002091 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2092 NULL)) {
2093 starget_printk(KERN_INFO, scsi_target(cmd->device),
2094 "WARM TARGET DEVICE RESET FAILED - "
2095 "waiting for commands.\n");
2096 return FAILED;
2097 }
2098
David C Somayajulu9d562912008-03-19 11:23:03 -07002099 /* Send marker. */
2100 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2101 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
2102 starget_printk(KERN_INFO, scsi_target(cmd->device),
2103 "WARM TARGET DEVICE RESET FAILED - "
2104 "marker iocb failed.\n");
2105 return FAILED;
2106 }
2107
Mike Christiece545032008-02-29 18:25:20 -06002108 starget_printk(KERN_INFO, scsi_target(cmd->device),
2109 "WARM TARGET RESET SUCCEEDED.\n");
2110 return SUCCESS;
2111}
2112
2113/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002114 * qla4xxx_eh_host_reset - kernel callback
2115 * @cmd: Pointer to Linux's SCSI command structure
2116 *
2117 * This routine is invoked by the Linux kernel to perform fatal error
2118 * recovery on the specified adapter.
2119 **/
2120static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
2121{
2122 int return_status = FAILED;
2123 struct scsi_qla_host *ha;
2124
2125 ha = (struct scsi_qla_host *) cmd->device->host->hostdata;
2126
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302127 if (ql4xdontresethba) {
2128 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
2129 ha->host_no, __func__));
2130 return FAILED;
2131 }
2132
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302133 ql4_printk(KERN_INFO, ha,
Karen Higginsdca05c42009-07-15 15:03:00 -05002134 "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002135 cmd->device->channel, cmd->device->id, cmd->device->lun);
2136
2137 if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
2138 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
2139 "DEAD.\n", ha->host_no, cmd->device->channel,
2140 __func__));
2141
2142 return FAILED;
2143 }
2144
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302145 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
2146 if (is_qla8022(ha))
2147 set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2148 else
2149 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2150 }
Mike Christie50a29ae2008-03-04 13:26:53 -06002151
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302152 if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002153 return_status = SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002154
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302155 ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
David Somayajuluafaf5a22006-09-19 10:28:00 -07002156 return_status == FAILED ? "FAILED" : "SUCCEDED");
2157
2158 return return_status;
2159}
2160
David Somayajuluafaf5a22006-09-19 10:28:00 -07002161static struct pci_device_id qla4xxx_pci_tbl[] = {
2162 {
2163 .vendor = PCI_VENDOR_ID_QLOGIC,
2164 .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
2165 .subvendor = PCI_ANY_ID,
2166 .subdevice = PCI_ANY_ID,
2167 },
2168 {
2169 .vendor = PCI_VENDOR_ID_QLOGIC,
2170 .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
2171 .subvendor = PCI_ANY_ID,
2172 .subdevice = PCI_ANY_ID,
2173 },
David C Somayajulud9150582006-11-15 17:38:40 -08002174 {
2175 .vendor = PCI_VENDOR_ID_QLOGIC,
2176 .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
2177 .subvendor = PCI_ANY_ID,
2178 .subdevice = PCI_ANY_ID,
2179 },
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302180 {
2181 .vendor = PCI_VENDOR_ID_QLOGIC,
2182 .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
2183 .subvendor = PCI_ANY_ID,
2184 .subdevice = PCI_ANY_ID,
2185 },
David Somayajuluafaf5a22006-09-19 10:28:00 -07002186 {0, 0},
2187};
2188MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
2189
Adrian Bunk47975472007-04-26 00:35:16 -07002190static struct pci_driver qla4xxx_pci_driver = {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002191 .name = DRIVER_NAME,
2192 .id_table = qla4xxx_pci_tbl,
2193 .probe = qla4xxx_probe_adapter,
2194 .remove = qla4xxx_remove_adapter,
2195};
2196
2197static int __init qla4xxx_module_init(void)
2198{
2199 int ret;
2200
2201 /* Allocate cache for SRBs. */
2202 srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09002203 SLAB_HWCACHE_ALIGN, NULL);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002204 if (srb_cachep == NULL) {
2205 printk(KERN_ERR
2206 "%s: Unable to allocate SRB cache..."
2207 "Failing load!\n", DRIVER_NAME);
2208 ret = -ENOMEM;
2209 goto no_srp_cache;
2210 }
2211
2212 /* Derive version string. */
2213 strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
Andrew Vasquez11010fe2006-10-06 09:54:59 -07002214 if (ql4xextended_error_logging)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002215 strcat(qla4xxx_version_str, "-debug");
2216
2217 qla4xxx_scsi_transport =
2218 iscsi_register_transport(&qla4xxx_iscsi_transport);
2219 if (!qla4xxx_scsi_transport){
2220 ret = -ENODEV;
2221 goto release_srb_cache;
2222 }
2223
David Somayajuluafaf5a22006-09-19 10:28:00 -07002224 ret = pci_register_driver(&qla4xxx_pci_driver);
2225 if (ret)
2226 goto unregister_transport;
2227
2228 printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
2229 return 0;
Doug Maxey5ae16db2006-10-05 23:50:07 -05002230
David Somayajuluafaf5a22006-09-19 10:28:00 -07002231unregister_transport:
2232 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
2233release_srb_cache:
2234 kmem_cache_destroy(srb_cachep);
2235no_srp_cache:
2236 return ret;
2237}
2238
2239static void __exit qla4xxx_module_exit(void)
2240{
David Somayajuluafaf5a22006-09-19 10:28:00 -07002241 pci_unregister_driver(&qla4xxx_pci_driver);
2242 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
2243 kmem_cache_destroy(srb_cachep);
2244}
2245
2246module_init(qla4xxx_module_init);
2247module_exit(qla4xxx_module_exit);
2248
2249MODULE_AUTHOR("QLogic Corporation");
2250MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
2251MODULE_LICENSE("GPL");
2252MODULE_VERSION(QLA4XXX_DRIVER_VERSION);