blob: 821384147a41cba12a2501f7b705001cbd1f56cb [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 Chaudharyf4f5df232010-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 Chaudharyf4f5df232010-07-28 15:53:44 +053035
David Somayajuluafaf5a22006-09-19 10:28:00 -070036int ql4xdontresethba = 0;
Vikas Chaudharyf4f5df232010-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 Chaudharyf4f5df232010-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 Chaudharyf4f5df232010-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 Chaudharyf4f5df232010-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 Chaudharyf4f5df232010-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 |
Vikas Chaudhary3b2bef12010-07-10 14:51:30 +0530129 ISCSI_TARGET_NAME | ISCSI_TPGT |
130 ISCSI_TARGET_ALIAS,
Mike Christie8ad57812007-05-30 12:57:13 -0500131 .host_param_mask = ISCSI_HOST_HWADDRESS |
Mike Christie22236962007-05-30 12:57:24 -0500132 ISCSI_HOST_IPADDRESS |
Mike Christie8ad57812007-05-30 12:57:13 -0500133 ISCSI_HOST_INITIATOR_NAME,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700134 .tgt_dscvr = qla4xxx_tgt_dscvr,
135 .get_conn_param = qla4xxx_conn_get_param,
136 .get_session_param = qla4xxx_sess_get_param,
Mike Christieaa1e93a2007-05-30 12:57:09 -0500137 .get_host_param = qla4xxx_host_get_param,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700138 .session_recovery_timedout = qla4xxx_recovery_timedout,
139};
140
141static struct scsi_transport_template *qla4xxx_scsi_transport;
142
Mike Christie5c656af2009-07-15 15:02:59 -0500143static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
144{
145 struct iscsi_cls_session *session;
146 struct ddb_entry *ddb_entry;
147
148 session = starget_to_session(scsi_target(sc->device));
149 ddb_entry = session->dd_data;
150
151 /* if we are not logged in then the LLD is going to clean up the cmd */
152 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
153 return BLK_EH_RESET_TIMER;
154 else
155 return BLK_EH_NOT_HANDLED;
156}
157
David Somayajuluafaf5a22006-09-19 10:28:00 -0700158static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session)
159{
160 struct ddb_entry *ddb_entry = session->dd_data;
161 struct scsi_qla_host *ha = ddb_entry->ha;
162
Mike Christie568d3032008-01-31 13:36:47 -0600163 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
164 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700165
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530166 DEBUG2(printk("scsi%ld: %s: ddb [%d] port down retry count "
Mike Christie568d3032008-01-31 13:36:47 -0600167 "of (%d) secs exhausted, marking device DEAD.\n",
168 ha->host_no, __func__, ddb_entry->fw_ddb_index,
169 ha->port_down_retry_count));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700170
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530171 qla4xxx_wake_dpc(ha);
Mike Christie568d3032008-01-31 13:36:47 -0600172 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700173}
174
Mike Christieaa1e93a2007-05-30 12:57:09 -0500175static int qla4xxx_host_get_param(struct Scsi_Host *shost,
176 enum iscsi_host_param param, char *buf)
177{
178 struct scsi_qla_host *ha = to_qla_host(shost);
179 int len;
180
181 switch (param) {
182 case ISCSI_HOST_PARAM_HWADDRESS:
Michael Chan7ffc49a2007-12-24 21:28:09 -0800183 len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
Mike Christieaa1e93a2007-05-30 12:57:09 -0500184 break;
Mike Christie22236962007-05-30 12:57:24 -0500185 case ISCSI_HOST_PARAM_IPADDRESS:
186 len = sprintf(buf, "%d.%d.%d.%d\n", ha->ip_address[0],
187 ha->ip_address[1], ha->ip_address[2],
188 ha->ip_address[3]);
189 break;
Mike Christie8ad57812007-05-30 12:57:13 -0500190 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie22236962007-05-30 12:57:24 -0500191 len = sprintf(buf, "%s\n", ha->name_string);
Mike Christie8ad57812007-05-30 12:57:13 -0500192 break;
Mike Christieaa1e93a2007-05-30 12:57:09 -0500193 default:
194 return -ENOSYS;
195 }
196
197 return len;
198}
199
David Somayajuluafaf5a22006-09-19 10:28:00 -0700200static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
201 enum iscsi_param param, char *buf)
202{
203 struct ddb_entry *ddb_entry = sess->dd_data;
204 int len;
205
206 switch (param) {
207 case ISCSI_PARAM_TARGET_NAME:
208 len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
209 ddb_entry->iscsi_name);
210 break;
211 case ISCSI_PARAM_TPGT:
212 len = sprintf(buf, "%u\n", ddb_entry->tpgt);
213 break;
Vikas Chaudhary3b2bef12010-07-10 14:51:30 +0530214 case ISCSI_PARAM_TARGET_ALIAS:
215 len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
216 ddb_entry->iscsi_alias);
217 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700218 default:
219 return -ENOSYS;
220 }
221
222 return len;
223}
224
225static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
226 enum iscsi_param param, char *buf)
227{
228 struct iscsi_cls_session *session;
229 struct ddb_entry *ddb_entry;
230 int len;
231
232 session = iscsi_dev_to_session(conn->dev.parent);
233 ddb_entry = session->dd_data;
234
235 switch (param) {
236 case ISCSI_PARAM_CONN_PORT:
237 len = sprintf(buf, "%hu\n", ddb_entry->port);
238 break;
239 case ISCSI_PARAM_CONN_ADDRESS:
240 /* TODO: what are the ipv6 bits */
Harvey Harrison63779432008-10-31 00:56:00 -0700241 len = sprintf(buf, "%pI4\n", &ddb_entry->ip_addr);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700242 break;
243 default:
244 return -ENOSYS;
245 }
246
247 return len;
248}
249
Mike Christie2174a042007-05-30 12:57:10 -0500250static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
251 enum iscsi_tgt_dscvr type, uint32_t enable,
252 struct sockaddr *dst_addr)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700253{
254 struct scsi_qla_host *ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700255 struct sockaddr_in *addr;
256 struct sockaddr_in6 *addr6;
257 int ret = 0;
258
David Somayajuluafaf5a22006-09-19 10:28:00 -0700259 ha = (struct scsi_qla_host *) shost->hostdata;
260
261 switch (type) {
262 case ISCSI_TGT_DSCVR_SEND_TARGETS:
263 if (dst_addr->sa_family == AF_INET) {
264 addr = (struct sockaddr_in *)dst_addr;
265 if (qla4xxx_send_tgts(ha, (char *)&addr->sin_addr,
266 addr->sin_port) != QLA_SUCCESS)
267 ret = -EIO;
268 } else if (dst_addr->sa_family == AF_INET6) {
269 /*
270 * TODO: fix qla4xxx_send_tgts
271 */
272 addr6 = (struct sockaddr_in6 *)dst_addr;
273 if (qla4xxx_send_tgts(ha, (char *)&addr6->sin6_addr,
274 addr6->sin6_port) != QLA_SUCCESS)
275 ret = -EIO;
276 } else
277 ret = -ENOSYS;
278 break;
279 default:
280 ret = -ENOSYS;
281 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700282 return ret;
283}
284
285void qla4xxx_destroy_sess(struct ddb_entry *ddb_entry)
286{
287 if (!ddb_entry->sess)
288 return;
289
290 if (ddb_entry->conn) {
Mike Christie26974782007-12-13 12:43:29 -0600291 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700292 iscsi_remove_session(ddb_entry->sess);
293 }
294 iscsi_free_session(ddb_entry->sess);
295}
296
297int qla4xxx_add_sess(struct ddb_entry *ddb_entry)
298{
299 int err;
300
Mike Christie26974782007-12-13 12:43:29 -0600301 ddb_entry->sess->recovery_tmo = ddb_entry->ha->port_down_retry_count;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700302 err = iscsi_add_session(ddb_entry->sess, ddb_entry->fw_ddb_index);
303 if (err) {
304 DEBUG2(printk(KERN_ERR "Could not add session.\n"));
305 return err;
306 }
307
Mike Christie5d91e202008-05-21 15:54:01 -0500308 ddb_entry->conn = iscsi_create_conn(ddb_entry->sess, 0, 0);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700309 if (!ddb_entry->conn) {
310 iscsi_remove_session(ddb_entry->sess);
311 DEBUG2(printk(KERN_ERR "Could not add connection.\n"));
312 return -ENOMEM;
313 }
Mike Christieb6359302008-01-31 13:36:44 -0600314
315 /* finally ready to go */
316 iscsi_unblock_session(ddb_entry->sess);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700317 return 0;
318}
319
320struct ddb_entry *qla4xxx_alloc_sess(struct scsi_qla_host *ha)
321{
322 struct ddb_entry *ddb_entry;
323 struct iscsi_cls_session *sess;
324
Mike Christie5d91e202008-05-21 15:54:01 -0500325 sess = iscsi_alloc_session(ha->host, &qla4xxx_iscsi_transport,
326 sizeof(struct ddb_entry));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700327 if (!sess)
328 return NULL;
329
330 ddb_entry = sess->dd_data;
331 memset(ddb_entry, 0, sizeof(*ddb_entry));
332 ddb_entry->ha = ha;
333 ddb_entry->sess = sess;
334 return ddb_entry;
335}
336
Mike Christie024f8012008-03-04 13:26:54 -0600337static void qla4xxx_scan_start(struct Scsi_Host *shost)
338{
339 struct scsi_qla_host *ha = shost_priv(shost);
340 struct ddb_entry *ddb_entry, *ddbtemp;
341
342 /* finish setup of sessions that were already setup in firmware */
343 list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
344 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE)
345 qla4xxx_add_sess(ddb_entry);
346 }
347}
348
David Somayajuluafaf5a22006-09-19 10:28:00 -0700349/*
350 * Timer routines
351 */
352
353static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
354 unsigned long interval)
355{
356 DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
357 __func__, ha->host->host_no));
358 init_timer(&ha->timer);
359 ha->timer.expires = jiffies + interval * HZ;
360 ha->timer.data = (unsigned long)ha;
361 ha->timer.function = (void (*)(unsigned long))func;
362 add_timer(&ha->timer);
363 ha->timer_active = 1;
364}
365
366static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
367{
368 del_timer_sync(&ha->timer);
369 ha->timer_active = 0;
370}
371
372/***
373 * qla4xxx_mark_device_missing - mark a device as missing.
374 * @ha: Pointer to host adapter structure.
375 * @ddb_entry: Pointer to device database entry
376 *
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530377 * This routine marks a device missing and close connection.
David Somayajuluafaf5a22006-09-19 10:28:00 -0700378 **/
379void qla4xxx_mark_device_missing(struct scsi_qla_host *ha,
380 struct ddb_entry *ddb_entry)
381{
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530382 if ((atomic_read(&ddb_entry->state) != DDB_STATE_DEAD)) {
383 atomic_set(&ddb_entry->state, DDB_STATE_MISSING);
384 DEBUG2(printk("scsi%ld: ddb [%d] marked MISSING\n",
385 ha->host_no, ddb_entry->fw_ddb_index));
386 } else
387 DEBUG2(printk("scsi%ld: ddb [%d] DEAD\n", ha->host_no,
388 ddb_entry->fw_ddb_index))
389
Mike Christieb6359302008-01-31 13:36:44 -0600390 iscsi_block_session(ddb_entry->sess);
Mike Christiee5bd7b52008-09-24 11:46:10 -0500391 iscsi_conn_error_event(ddb_entry->conn, ISCSI_ERR_CONN_FAILED);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700392}
393
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530394/**
395 * qla4xxx_mark_all_devices_missing - mark all devices as missing.
396 * @ha: Pointer to host adapter structure.
397 *
398 * This routine marks a device missing and resets the relogin retry count.
399 **/
400void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
401{
402 struct ddb_entry *ddb_entry, *ddbtemp;
403 list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
404 qla4xxx_mark_device_missing(ha, ddb_entry);
405 }
406}
407
David Somayajuluafaf5a22006-09-19 10:28:00 -0700408static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
409 struct ddb_entry *ddb_entry,
410 struct scsi_cmnd *cmd,
411 void (*done)(struct scsi_cmnd *))
412{
413 struct srb *srb;
414
415 srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
416 if (!srb)
417 return srb;
418
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530419 kref_init(&srb->srb_ref);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700420 srb->ha = ha;
421 srb->ddb = ddb_entry;
422 srb->cmd = cmd;
423 srb->flags = 0;
Vikas Chaudhary53698872010-04-28 11:41:59 +0530424 CMD_SP(cmd) = (void *)srb;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700425 cmd->scsi_done = done;
426
427 return srb;
428}
429
430static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
431{
432 struct scsi_cmnd *cmd = srb->cmd;
433
434 if (srb->flags & SRB_DMA_VALID) {
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +0900435 scsi_dma_unmap(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700436 srb->flags &= ~SRB_DMA_VALID;
437 }
Vikas Chaudhary53698872010-04-28 11:41:59 +0530438 CMD_SP(cmd) = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700439}
440
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530441void qla4xxx_srb_compl(struct kref *ref)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700442{
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530443 struct srb *srb = container_of(ref, struct srb, srb_ref);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700444 struct scsi_cmnd *cmd = srb->cmd;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530445 struct scsi_qla_host *ha = srb->ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700446
447 qla4xxx_srb_free_dma(ha, srb);
448
449 mempool_free(srb, ha->srb_mempool);
450
451 cmd->scsi_done(cmd);
452}
453
454/**
455 * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
456 * @cmd: Pointer to Linux's SCSI command structure
457 * @done_fn: Function that the driver calls to notify the SCSI mid-layer
458 * that the command has been processed.
459 *
460 * Remarks:
461 * This routine is invoked by Linux to send a SCSI command to the driver.
462 * The mid-level driver tries to ensure that queuecommand never gets
463 * invoked concurrently with itself or the interrupt handler (although
464 * the interrupt handler may call this routine as part of request-
465 * completion handling). Unfortunely, it sometimes calls the scheduler
466 * in interrupt context which is a big NO! NO!.
467 **/
468static int qla4xxx_queuecommand(struct scsi_cmnd *cmd,
469 void (*done)(struct scsi_cmnd *))
470{
471 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
472 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christie7fb19212008-01-31 13:36:45 -0600473 struct iscsi_cls_session *sess = ddb_entry->sess;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700474 struct srb *srb;
475 int rval;
476
Mike Christie7fb19212008-01-31 13:36:45 -0600477 if (!sess) {
478 cmd->result = DID_IMM_RETRY << 16;
479 goto qc_fail_command;
480 }
481
482 rval = iscsi_session_chkready(sess);
483 if (rval) {
484 cmd->result = rval;
485 goto qc_fail_command;
486 }
487
David Somayajuluafaf5a22006-09-19 10:28:00 -0700488 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
489 if (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD) {
490 cmd->result = DID_NO_CONNECT << 16;
491 goto qc_fail_command;
492 }
Mike Christiec5e98e92008-08-17 15:24:39 -0500493 return SCSI_MLQUEUE_TARGET_BUSY;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700494 }
495
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530496 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
497 test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
498 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
499 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
500 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
501 !test_bit(AF_ONLINE, &ha->flags) ||
502 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
David C Somayajulu477ffb92007-01-22 12:26:11 -0800503 goto qc_host_busy;
504
David Somayajuluafaf5a22006-09-19 10:28:00 -0700505 spin_unlock_irq(ha->host->host_lock);
506
507 srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd, done);
508 if (!srb)
509 goto qc_host_busy_lock;
510
511 rval = qla4xxx_send_command_to_isp(ha, srb);
512 if (rval != QLA_SUCCESS)
513 goto qc_host_busy_free_sp;
514
515 spin_lock_irq(ha->host->host_lock);
516 return 0;
517
518qc_host_busy_free_sp:
519 qla4xxx_srb_free_dma(ha, srb);
520 mempool_free(srb, ha->srb_mempool);
521
522qc_host_busy_lock:
523 spin_lock_irq(ha->host->host_lock);
524
525qc_host_busy:
526 return SCSI_MLQUEUE_HOST_BUSY;
527
528qc_fail_command:
529 done(cmd);
530
531 return 0;
532}
533
534/**
535 * qla4xxx_mem_free - frees memory allocated to adapter
536 * @ha: Pointer to host adapter structure.
537 *
538 * Frees memory previously allocated by qla4xxx_mem_alloc
539 **/
540static void qla4xxx_mem_free(struct scsi_qla_host *ha)
541{
542 if (ha->queues)
543 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
544 ha->queues_dma);
545
546 ha->queues_len = 0;
547 ha->queues = NULL;
548 ha->queues_dma = 0;
549 ha->request_ring = NULL;
550 ha->request_dma = 0;
551 ha->response_ring = NULL;
552 ha->response_dma = 0;
553 ha->shadow_regs = NULL;
554 ha->shadow_regs_dma = 0;
555
556 /* Free srb pool. */
557 if (ha->srb_mempool)
558 mempool_destroy(ha->srb_mempool);
559
560 ha->srb_mempool = NULL;
561
562 /* release io space registers */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530563 if (is_qla8022(ha)) {
564 if (ha->nx_pcibase)
565 iounmap(
566 (struct device_reg_82xx __iomem *)ha->nx_pcibase);
567
568 if (ha->nx_db_wr_ptr)
569 iounmap(
570 (struct device_reg_82xx __iomem *)ha->nx_db_wr_ptr);
571 } else if (ha->reg)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700572 iounmap(ha->reg);
573 pci_release_regions(ha->pdev);
574}
575
576/**
577 * qla4xxx_mem_alloc - allocates memory for use by adapter.
578 * @ha: Pointer to host adapter structure
579 *
580 * Allocates DMA memory for request and response queues. Also allocates memory
581 * for srbs.
582 **/
583static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
584{
585 unsigned long align;
586
587 /* Allocate contiguous block of DMA memory for queues. */
588 ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
589 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
590 sizeof(struct shadow_regs) +
591 MEM_ALIGN_VALUE +
592 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
593 ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
594 &ha->queues_dma, GFP_KERNEL);
595 if (ha->queues == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530596 ql4_printk(KERN_WARNING, ha,
597 "Memory Allocation failed - queues.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700598
599 goto mem_alloc_error_exit;
600 }
601 memset(ha->queues, 0, ha->queues_len);
602
603 /*
604 * As per RISC alignment requirements -- the bus-address must be a
605 * multiple of the request-ring size (in bytes).
606 */
607 align = 0;
608 if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
609 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
610 (MEM_ALIGN_VALUE - 1));
611
612 /* Update request and response queue pointers. */
613 ha->request_dma = ha->queues_dma + align;
614 ha->request_ring = (struct queue_entry *) (ha->queues + align);
615 ha->response_dma = ha->queues_dma + align +
616 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
617 ha->response_ring = (struct queue_entry *) (ha->queues + align +
618 (REQUEST_QUEUE_DEPTH *
619 QUEUE_SIZE));
620 ha->shadow_regs_dma = ha->queues_dma + align +
621 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
622 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
623 ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
624 (REQUEST_QUEUE_DEPTH *
625 QUEUE_SIZE) +
626 (RESPONSE_QUEUE_DEPTH *
627 QUEUE_SIZE));
628
629 /* Allocate memory for srb pool. */
630 ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
631 mempool_free_slab, srb_cachep);
632 if (ha->srb_mempool == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530633 ql4_printk(KERN_WARNING, ha,
634 "Memory Allocation failed - SRB Pool.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700635
636 goto mem_alloc_error_exit;
637 }
638
639 return QLA_SUCCESS;
640
641mem_alloc_error_exit:
642 qla4xxx_mem_free(ha);
643 return QLA_ERROR;
644}
645
646/**
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530647 * qla4_8xxx_check_fw_alive - Check firmware health
648 * @ha: Pointer to host adapter structure.
649 *
650 * Context: Interrupt
651 **/
652static void qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
653{
654 uint32_t fw_heartbeat_counter, halt_status;
655
656 fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
657
658 if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
659 ha->seconds_since_last_heartbeat++;
660 /* FW not alive after 2 seconds */
661 if (ha->seconds_since_last_heartbeat == 2) {
662 ha->seconds_since_last_heartbeat = 0;
663 halt_status = qla4_8xxx_rd_32(ha,
664 QLA82XX_PEG_HALT_STATUS1);
665 /* Since we cannot change dev_state in interrupt
666 * context, set appropriate DPC flag then wakeup
667 * DPC */
668 if (halt_status & HALT_STATUS_UNRECOVERABLE)
669 set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
670 else {
671 printk("scsi%ld: %s: detect abort needed!\n",
672 ha->host_no, __func__);
673 set_bit(DPC_RESET_HA, &ha->dpc_flags);
674 }
675 qla4xxx_wake_dpc(ha);
676 }
677 }
678 ha->fw_heartbeat_counter = fw_heartbeat_counter;
679}
680
681/**
682 * qla4_8xxx_watchdog - Poll dev state
683 * @ha: Pointer to host adapter structure.
684 *
685 * Context: Interrupt
686 **/
687void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
688{
689 uint32_t dev_state;
690
691 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
692
693 /* don't poll if reset is going on */
694 if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags)) {
695 if (dev_state == QLA82XX_DEV_NEED_RESET &&
696 !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
697 printk("scsi%ld: %s: HW State: NEED RESET!\n",
698 ha->host_no, __func__);
699 set_bit(DPC_RESET_HA, &ha->dpc_flags);
700 qla4xxx_wake_dpc(ha);
701 } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
702 !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
703 printk("scsi%ld: %s: HW State: NEED QUIES!\n",
704 ha->host_no, __func__);
705 set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
706 qla4xxx_wake_dpc(ha);
707 } else {
708 /* Check firmware health */
709 qla4_8xxx_check_fw_alive(ha);
710 }
711 }
712}
713
714/**
David Somayajuluafaf5a22006-09-19 10:28:00 -0700715 * qla4xxx_timer - checks every second for work to do.
716 * @ha: Pointer to host adapter structure.
717 **/
718static void qla4xxx_timer(struct scsi_qla_host *ha)
719{
720 struct ddb_entry *ddb_entry, *dtemp;
721 int start_dpc = 0;
722
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530723 if (test_bit(AF_HBA_GOING_AWAY, &ha->flags)) {
724 DEBUG2(ql4_printk(KERN_INFO, ha, "%s exited. HBA GOING AWAY\n",
725 __func__));
726 return;
727 }
728
729 if (is_qla8022(ha)) {
730 qla4_8xxx_watchdog(ha);
731 }
732
David Somayajuluafaf5a22006-09-19 10:28:00 -0700733 /* Search for relogin's to time-out and port down retry. */
734 list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
735 /* Count down time between sending relogins */
736 if (adapter_up(ha) &&
737 !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
738 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
739 if (atomic_read(&ddb_entry->retry_relogin_timer) !=
740 INVALID_ENTRY) {
741 if (atomic_read(&ddb_entry->retry_relogin_timer)
742 == 0) {
743 atomic_set(&ddb_entry->
744 retry_relogin_timer,
745 INVALID_ENTRY);
746 set_bit(DPC_RELOGIN_DEVICE,
747 &ha->dpc_flags);
748 set_bit(DF_RELOGIN, &ddb_entry->flags);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530749 DEBUG2(printk("scsi%ld: %s: ddb [%d]"
David Somayajuluafaf5a22006-09-19 10:28:00 -0700750 " login device\n",
751 ha->host_no, __func__,
752 ddb_entry->fw_ddb_index));
753 } else
754 atomic_dec(&ddb_entry->
755 retry_relogin_timer);
756 }
757 }
758
759 /* Wait for relogin to timeout */
760 if (atomic_read(&ddb_entry->relogin_timer) &&
761 (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
762 /*
763 * If the relogin times out and the device is
764 * still NOT ONLINE then try and relogin again.
765 */
766 if (atomic_read(&ddb_entry->state) !=
767 DDB_STATE_ONLINE &&
768 ddb_entry->fw_ddb_device_state ==
769 DDB_DS_SESSION_FAILED) {
770 /* Reset retry relogin timer */
771 atomic_inc(&ddb_entry->relogin_retry_count);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530772 DEBUG2(printk("scsi%ld: ddb [%d] relogin"
David Somayajuluafaf5a22006-09-19 10:28:00 -0700773 " timed out-retrying"
774 " relogin (%d)\n",
775 ha->host_no,
776 ddb_entry->fw_ddb_index,
777 atomic_read(&ddb_entry->
778 relogin_retry_count))
779 );
780 start_dpc++;
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530781 DEBUG(printk("scsi%ld:%d:%d: ddb [%d] "
David Somayajuluafaf5a22006-09-19 10:28:00 -0700782 "initate relogin after"
783 " %d seconds\n",
784 ha->host_no, ddb_entry->bus,
785 ddb_entry->target,
786 ddb_entry->fw_ddb_index,
787 ddb_entry->default_time2wait + 4)
788 );
789
790 atomic_set(&ddb_entry->retry_relogin_timer,
791 ddb_entry->default_time2wait + 4);
792 }
793 }
794 }
795
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530796 if (!is_qla8022(ha)) {
797 /* Check for heartbeat interval. */
798 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
799 ha->heartbeat_interval != 0) {
800 ha->seconds_since_last_heartbeat++;
801 if (ha->seconds_since_last_heartbeat >
802 ha->heartbeat_interval + 2)
803 set_bit(DPC_RESET_HA, &ha->dpc_flags);
804 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700805 }
806
David Somayajuluafaf5a22006-09-19 10:28:00 -0700807 /* Wakeup the dpc routine for this adapter, if needed. */
808 if ((start_dpc ||
809 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
810 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
811 test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530812 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -0700813 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
814 test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +0530815 test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530816 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
817 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -0700818 test_bit(DPC_AEN, &ha->dpc_flags)) &&
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530819 !test_bit(AF_DPC_SCHEDULED, &ha->flags) &&
David Somayajuluafaf5a22006-09-19 10:28:00 -0700820 ha->dpc_thread) {
821 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
822 " - dpc flags = 0x%lx\n",
823 ha->host_no, __func__, ha->dpc_flags));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530824 qla4xxx_wake_dpc(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700825 }
826
827 /* Reschedule timer thread to call us back in one second */
828 mod_timer(&ha->timer, jiffies + HZ);
829
830 DEBUG2(ha->seconds_since_last_intr++);
831}
832
833/**
834 * qla4xxx_cmd_wait - waits for all outstanding commands to complete
835 * @ha: Pointer to host adapter structure.
836 *
837 * This routine stalls the driver until all outstanding commands are returned.
838 * Caller must release the Hardware Lock prior to calling this routine.
839 **/
840static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
841{
842 uint32_t index = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700843 unsigned long flags;
844 struct scsi_cmnd *cmd;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700845
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530846 unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
847
848 DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
849 "complete\n", WAIT_CMD_TOV));
850
851 while (!time_after_eq(jiffies, wtime)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700852 spin_lock_irqsave(&ha->hardware_lock, flags);
853 /* Find a command that hasn't completed. */
854 for (index = 0; index < ha->host->can_queue; index++) {
855 cmd = scsi_host_find_tag(ha->host, index);
856 if (cmd != NULL)
857 break;
858 }
859 spin_unlock_irqrestore(&ha->hardware_lock, flags);
860
861 /* If No Commands are pending, wait is complete */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530862 if (index == ha->host->can_queue)
863 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700864
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530865 msleep(1000);
866 }
867 /* If we timed out on waiting for commands to come back
868 * return ERROR. */
869 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700870}
871
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530872int qla4xxx_hw_reset(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700873{
David Somayajuluafaf5a22006-09-19 10:28:00 -0700874 uint32_t ctrl_status;
David C Somayajulu477ffb92007-01-22 12:26:11 -0800875 unsigned long flags = 0;
876
877 DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700878
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530879 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
880 return QLA_ERROR;
881
David Somayajuluafaf5a22006-09-19 10:28:00 -0700882 spin_lock_irqsave(&ha->hardware_lock, flags);
883
884 /*
885 * If the SCSI Reset Interrupt bit is set, clear it.
886 * Otherwise, the Soft Reset won't work.
887 */
888 ctrl_status = readw(&ha->reg->ctrl_status);
889 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
890 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
891
892 /* Issue Soft Reset */
893 writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
894 readl(&ha->reg->ctrl_status);
895
896 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530897 return QLA_SUCCESS;
David C Somayajulu477ffb92007-01-22 12:26:11 -0800898}
899
900/**
901 * qla4xxx_soft_reset - performs soft reset.
902 * @ha: Pointer to host adapter structure.
903 **/
904int qla4xxx_soft_reset(struct scsi_qla_host *ha)
905{
906 uint32_t max_wait_time;
907 unsigned long flags = 0;
908 int status = QLA_ERROR;
909 uint32_t ctrl_status;
910
911 qla4xxx_hw_reset(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700912
913 /* Wait until the Network Reset Intr bit is cleared */
914 max_wait_time = RESET_INTR_TOV;
915 do {
916 spin_lock_irqsave(&ha->hardware_lock, flags);
917 ctrl_status = readw(&ha->reg->ctrl_status);
918 spin_unlock_irqrestore(&ha->hardware_lock, flags);
919
920 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
921 break;
922
923 msleep(1000);
924 } while ((--max_wait_time));
925
926 if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
927 DEBUG2(printk(KERN_WARNING
928 "scsi%ld: Network Reset Intr not cleared by "
929 "Network function, clearing it now!\n",
930 ha->host_no));
931 spin_lock_irqsave(&ha->hardware_lock, flags);
932 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
933 readl(&ha->reg->ctrl_status);
934 spin_unlock_irqrestore(&ha->hardware_lock, flags);
935 }
936
937 /* Wait until the firmware tells us the Soft Reset is done */
938 max_wait_time = SOFT_RESET_TOV;
939 do {
940 spin_lock_irqsave(&ha->hardware_lock, flags);
941 ctrl_status = readw(&ha->reg->ctrl_status);
942 spin_unlock_irqrestore(&ha->hardware_lock, flags);
943
944 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
945 status = QLA_SUCCESS;
946 break;
947 }
948
949 msleep(1000);
950 } while ((--max_wait_time));
951
952 /*
953 * Also, make sure that the SCSI Reset Interrupt bit has been cleared
954 * after the soft reset has taken place.
955 */
956 spin_lock_irqsave(&ha->hardware_lock, flags);
957 ctrl_status = readw(&ha->reg->ctrl_status);
958 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
959 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
960 readl(&ha->reg->ctrl_status);
961 }
962 spin_unlock_irqrestore(&ha->hardware_lock, flags);
963
964 /* If soft reset fails then most probably the bios on other
965 * function is also enabled.
966 * Since the initialization is sequential the other fn
967 * wont be able to acknowledge the soft reset.
968 * Issue a force soft reset to workaround this scenario.
969 */
970 if (max_wait_time == 0) {
971 /* Issue Force Soft Reset */
972 spin_lock_irqsave(&ha->hardware_lock, flags);
973 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
974 readl(&ha->reg->ctrl_status);
975 spin_unlock_irqrestore(&ha->hardware_lock, flags);
976 /* Wait until the firmware tells us the Soft Reset is done */
977 max_wait_time = SOFT_RESET_TOV;
978 do {
979 spin_lock_irqsave(&ha->hardware_lock, flags);
980 ctrl_status = readw(&ha->reg->ctrl_status);
981 spin_unlock_irqrestore(&ha->hardware_lock, flags);
982
983 if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
984 status = QLA_SUCCESS;
985 break;
986 }
987
988 msleep(1000);
989 } while ((--max_wait_time));
990 }
991
992 return status;
993}
994
995/**
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530996 * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
David Somayajuluafaf5a22006-09-19 10:28:00 -0700997 * @ha: Pointer to host adapter structure.
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530998 * @res: returned scsi status
David Somayajuluafaf5a22006-09-19 10:28:00 -0700999 *
1000 * This routine is called just prior to a HARD RESET to return all
1001 * outstanding commands back to the Operating System.
1002 * Caller should make sure that the following locks are released
1003 * before this calling routine: Hardware lock, and io_request_lock.
1004 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301005static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001006{
1007 struct srb *srb;
1008 int i;
1009 unsigned long flags;
1010
1011 spin_lock_irqsave(&ha->hardware_lock, flags);
1012 for (i = 0; i < ha->host->can_queue; i++) {
1013 srb = qla4xxx_del_from_active_array(ha, i);
1014 if (srb != NULL) {
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301015 srb->cmd->result = res;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301016 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001017 }
1018 }
1019 spin_unlock_irqrestore(&ha->hardware_lock, flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001020}
1021
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301022void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
1023{
1024 clear_bit(AF_ONLINE, &ha->flags);
1025
1026 /* Disable the board */
1027 ql4_printk(KERN_INFO, ha, "Disabling the board\n");
1028 set_bit(AF_HBA_GOING_AWAY, &ha->flags);
1029
1030 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
1031 qla4xxx_mark_all_devices_missing(ha);
1032 clear_bit(AF_INIT_DONE, &ha->flags);
1033}
1034
David Somayajuluafaf5a22006-09-19 10:28:00 -07001035/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07001036 * qla4xxx_recover_adapter - recovers adapter after a fatal error
1037 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001038 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301039static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001040{
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301041 int status = QLA_ERROR;
1042 uint8_t reset_chip = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001043
1044 /* Stall incoming I/O until we are done */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301045 scsi_block_requests(ha->host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001046 clear_bit(AF_ONLINE, &ha->flags);
Mike Christie50a29ae2008-03-04 13:26:53 -06001047
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301048 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001049
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301050 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001051
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301052 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
1053 reset_chip = 1;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001054
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301055 /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
1056 * do not reset adapter, jump to initialize_adapter */
1057 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1058 status = QLA_SUCCESS;
1059 goto recover_ha_init_adapter;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001060 }
1061
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301062 /* For the ISP-82xx adapter, issue a stop_firmware if invoked
1063 * from eh_host_reset or ioctl module */
1064 if (is_qla8022(ha) && !reset_chip &&
1065 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
1066
1067 DEBUG2(ql4_printk(KERN_INFO, ha,
1068 "scsi%ld: %s - Performing stop_firmware...\n",
1069 ha->host_no, __func__));
1070 status = ha->isp_ops->reset_firmware(ha);
1071 if (status == QLA_SUCCESS) {
1072 qla4xxx_cmd_wait(ha);
1073 ha->isp_ops->disable_intrs(ha);
1074 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1075 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1076 } else {
1077 /* If the stop_firmware fails then
1078 * reset the entire chip */
1079 reset_chip = 1;
1080 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1081 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1082 }
1083 }
1084
1085 /* Issue full chip reset if recovering from a catastrophic error,
1086 * or if stop_firmware fails for ISP-82xx.
1087 * This is the default case for ISP-4xxx */
1088 if (!is_qla8022(ha) || reset_chip) {
1089 qla4xxx_cmd_wait(ha);
1090 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1091 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1092 DEBUG2(ql4_printk(KERN_INFO, ha,
1093 "scsi%ld: %s - Performing chip reset..\n",
1094 ha->host_no, __func__));
1095 status = ha->isp_ops->reset_chip(ha);
1096 }
1097
1098 /* Flush any pending ddb changed AENs */
1099 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1100
1101recover_ha_init_adapter:
1102 /* Upon successful firmware/chip reset, re-initialize the adapter */
1103 if (status == QLA_SUCCESS) {
1104 /* For ISP-4xxx, force function 1 to always initialize
1105 * before function 3 to prevent both funcions from
1106 * stepping on top of the other */
1107 if (!is_qla8022(ha) && (ha->mac_index == 3))
1108 ssleep(6);
1109
1110 /* NOTE: AF_ONLINE flag set upon successful completion of
1111 * qla4xxx_initialize_adapter */
1112 status = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
1113 }
1114
1115 /* Retry failed adapter initialization, if necessary
1116 * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
1117 * case to prevent ping-pong resets between functions */
1118 if (!test_bit(AF_ONLINE, &ha->flags) &&
1119 !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001120 /* Adapter initialization failed, see if we can retry
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301121 * resetting the ha.
1122 * Since we don't want to block the DPC for too long
1123 * with multiple resets in the same thread,
1124 * utilize DPC to retry */
David Somayajuluafaf5a22006-09-19 10:28:00 -07001125 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
1126 ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
1127 DEBUG2(printk("scsi%ld: recover adapter - retrying "
1128 "(%d) more times\n", ha->host_no,
1129 ha->retry_reset_ha_cnt));
1130 set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1131 status = QLA_ERROR;
1132 } else {
1133 if (ha->retry_reset_ha_cnt > 0) {
1134 /* Schedule another Reset HA--DPC will retry */
1135 ha->retry_reset_ha_cnt--;
1136 DEBUG2(printk("scsi%ld: recover adapter - "
1137 "retry remaining %d\n",
1138 ha->host_no,
1139 ha->retry_reset_ha_cnt));
1140 status = QLA_ERROR;
1141 }
1142
1143 if (ha->retry_reset_ha_cnt == 0) {
1144 /* Recover adapter retries have been exhausted.
1145 * Adapter DEAD */
1146 DEBUG2(printk("scsi%ld: recover adapter "
1147 "failed - board disabled\n",
1148 ha->host_no));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301149 qla4xxx_dead_adapter_cleanup(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001150 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1151 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301152 clear_bit(DPC_RESET_HA_FW_CONTEXT,
David Somayajuluafaf5a22006-09-19 10:28:00 -07001153 &ha->dpc_flags);
1154 status = QLA_ERROR;
1155 }
1156 }
1157 } else {
1158 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301159 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001160 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1161 }
1162
1163 ha->adapter_error_count++;
1164
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301165 if (test_bit(AF_ONLINE, &ha->flags))
1166 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001167
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301168 scsi_unblock_requests(ha->host);
1169
1170 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
1171 DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
1172 status == QLA_ERROR ? "FAILED" : "SUCCEDED"));
1173
David Somayajuluafaf5a22006-09-19 10:28:00 -07001174 return status;
1175}
1176
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301177void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
1178{
1179 if (ha->dpc_thread &&
1180 !test_bit(AF_DPC_SCHEDULED, &ha->flags)) {
1181 set_bit(AF_DPC_SCHEDULED, &ha->flags);
1182 queue_work(ha->dpc_thread, &ha->dpc_work);
1183 }
1184}
1185
David Somayajuluafaf5a22006-09-19 10:28:00 -07001186/**
1187 * qla4xxx_do_dpc - dpc routine
1188 * @data: in our case pointer to adapter structure
1189 *
1190 * This routine is a task that is schedule by the interrupt handler
1191 * to perform the background processing for interrupts. We put it
1192 * on a task queue that is consumed whenever the scheduler runs; that's
1193 * so you can do anything (i.e. put the process to sleep etc). In fact,
1194 * the mid-level tries to sleep when it reaches the driver threshold
1195 * "host->can_queue". This can cause a panic if we were in our interrupt code.
1196 **/
David Howellsc4028952006-11-22 14:57:56 +00001197static void qla4xxx_do_dpc(struct work_struct *work)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001198{
David Howellsc4028952006-11-22 14:57:56 +00001199 struct scsi_qla_host *ha =
1200 container_of(work, struct scsi_qla_host, dpc_work);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001201 struct ddb_entry *ddb_entry, *dtemp;
David C Somayajulu477ffb92007-01-22 12:26:11 -08001202 int status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001203
David C Somayajuluf26b9042006-11-15 16:41:09 -08001204 DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301205 "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
1206 ha->host_no, __func__, ha->flags, ha->dpc_flags))
David Somayajuluafaf5a22006-09-19 10:28:00 -07001207
1208 /* Initialization not yet finished. Don't do anything yet. */
1209 if (!test_bit(AF_INIT_DONE, &ha->flags))
1210 return;
1211
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301212 /* HBA is in the process of being permanently disabled.
1213 * Don't process anything */
1214 if (test_bit(AF_HBA_GOING_AWAY, &ha->flags))
1215 return;
1216
1217 if (is_qla8022(ha)) {
1218 if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
1219 qla4_8xxx_idc_lock(ha);
1220 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
1221 QLA82XX_DEV_FAILED);
1222 qla4_8xxx_idc_unlock(ha);
1223 ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
1224 qla4_8xxx_device_state_handler(ha);
1225 }
1226 if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
1227 qla4_8xxx_need_qsnt_handler(ha);
1228 }
1229 }
1230
1231 if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
1232 (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07001233 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301234 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
1235 if (ql4xdontresethba) {
1236 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
1237 ha->host_no, __func__));
1238 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
1239 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1240 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1241 goto dpc_post_reset_ha;
1242 }
1243 if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
1244 test_bit(DPC_RESET_HA, &ha->dpc_flags))
1245 qla4xxx_recover_adapter(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001246
David C Somayajulu477ffb92007-01-22 12:26:11 -08001247 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001248 uint8_t wait_time = RESET_INTR_TOV;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001249
David Somayajuluafaf5a22006-09-19 10:28:00 -07001250 while ((readw(&ha->reg->ctrl_status) &
1251 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
1252 if (--wait_time == 0)
1253 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001254 msleep(1000);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001255 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001256 if (wait_time == 0)
1257 DEBUG2(printk("scsi%ld: %s: SR|FSR "
1258 "bit not cleared-- resetting\n",
1259 ha->host_no, __func__));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301260 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
David C Somayajulu477ffb92007-01-22 12:26:11 -08001261 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
1262 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301263 status = qla4xxx_recover_adapter(ha);
David C Somayajulu477ffb92007-01-22 12:26:11 -08001264 }
1265 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1266 if (status == QLA_SUCCESS)
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301267 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001268 }
1269 }
1270
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301271dpc_post_reset_ha:
David Somayajuluafaf5a22006-09-19 10:28:00 -07001272 /* ---- process AEN? --- */
1273 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
1274 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
1275
1276 /* ---- Get DHCP IP Address? --- */
1277 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
1278 qla4xxx_get_dhcp_ip_address(ha);
1279
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301280 /* ---- link change? --- */
1281 if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
1282 if (!test_bit(AF_LINK_UP, &ha->flags)) {
1283 /* ---- link down? --- */
1284 list_for_each_entry_safe(ddb_entry, dtemp,
1285 &ha->ddb_list, list) {
1286 if (atomic_read(&ddb_entry->state) ==
1287 DDB_STATE_ONLINE)
1288 qla4xxx_mark_device_missing(ha,
1289 ddb_entry);
1290 }
1291 } else {
1292 /* ---- link up? --- *
1293 * F/W will auto login to all devices ONLY ONCE after
1294 * link up during driver initialization and runtime
1295 * fatal error recovery. Therefore, the driver must
1296 * manually relogin to devices when recovering from
1297 * connection failures, logouts, expired KATO, etc. */
1298
1299 list_for_each_entry_safe(ddb_entry, dtemp,
1300 &ha->ddb_list, list) {
1301 if ((atomic_read(&ddb_entry->state) ==
1302 DDB_STATE_MISSING) ||
1303 (atomic_read(&ddb_entry->state) ==
1304 DDB_STATE_DEAD)) {
1305 if (ddb_entry->fw_ddb_device_state ==
1306 DDB_DS_SESSION_ACTIVE) {
1307 atomic_set(&ddb_entry->state,
1308 DDB_STATE_ONLINE);
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301309 ql4_printk(KERN_INFO, ha,
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301310 "scsi%ld: %s: ddb[%d]"
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301311 " marked ONLINE\n",
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301312 ha->host_no, __func__,
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301313 ddb_entry->fw_ddb_index);
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301314
1315 iscsi_unblock_session(
1316 ddb_entry->sess);
1317 } else
1318 qla4xxx_relogin_device(
1319 ha, ddb_entry);
1320 }
1321
1322 }
1323 }
1324 }
1325
David Somayajuluafaf5a22006-09-19 10:28:00 -07001326 /* ---- relogin device? --- */
1327 if (adapter_up(ha) &&
1328 test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
1329 list_for_each_entry_safe(ddb_entry, dtemp,
1330 &ha->ddb_list, list) {
1331 if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
1332 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
1333 qla4xxx_relogin_device(ha, ddb_entry);
1334
1335 /*
1336 * If mbx cmd times out there is no point
1337 * in continuing further.
1338 * With large no of targets this can hang
1339 * the system.
1340 */
1341 if (test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
1342 printk(KERN_WARNING "scsi%ld: %s: "
1343 "need to reset hba\n",
1344 ha->host_no, __func__);
1345 break;
1346 }
1347 }
1348 }
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301349 clear_bit(AF_DPC_SCHEDULED, &ha->flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001350}
1351
1352/**
1353 * qla4xxx_free_adapter - release the adapter
1354 * @ha: pointer to adapter structure
1355 **/
1356static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
1357{
1358
1359 if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
1360 /* Turn-off interrupts on the card. */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301361 ha->isp_ops->disable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001362 }
1363
David Somayajuluafaf5a22006-09-19 10:28:00 -07001364 /* Remove timer thread, if present */
1365 if (ha->timer_active)
1366 qla4xxx_stop_timer(ha);
1367
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301368 /* Kill the kernel thread for this host */
1369 if (ha->dpc_thread)
1370 destroy_workqueue(ha->dpc_thread);
1371
1372 /* Put firmware in known state */
1373 ha->isp_ops->reset_firmware(ha);
1374
1375 if (is_qla8022(ha)) {
1376 qla4_8xxx_idc_lock(ha);
1377 qla4_8xxx_clear_drv_active(ha);
1378 qla4_8xxx_idc_unlock(ha);
1379 }
1380
David Somayajuluafaf5a22006-09-19 10:28:00 -07001381 /* Detach interrupts */
1382 if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301383 qla4xxx_free_irqs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001384
David C Somayajulubee4fe82007-05-23 18:03:32 -07001385 /* free extra memory */
1386 qla4xxx_mem_free(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301387}
David C Somayajulubee4fe82007-05-23 18:03:32 -07001388
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301389int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
1390{
1391 int status = 0;
1392 uint8_t revision_id;
1393 unsigned long mem_base, mem_len, db_base, db_len;
1394 struct pci_dev *pdev = ha->pdev;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001395
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301396 status = pci_request_regions(pdev, DRIVER_NAME);
1397 if (status) {
1398 printk(KERN_WARNING
1399 "scsi(%ld) Failed to reserve PIO regions (%s) "
1400 "status=%d\n", ha->host_no, pci_name(pdev), status);
1401 goto iospace_error_exit;
1402 }
1403
1404 pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
1405 DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
1406 __func__, revision_id));
1407 ha->revision_id = revision_id;
1408
1409 /* remap phys address */
1410 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
1411 mem_len = pci_resource_len(pdev, 0);
1412 DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
1413 __func__, mem_base, mem_len));
1414
1415 /* mapping of pcibase pointer */
1416 ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
1417 if (!ha->nx_pcibase) {
1418 printk(KERN_ERR
1419 "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
1420 pci_release_regions(ha->pdev);
1421 goto iospace_error_exit;
1422 }
1423
1424 /* Mapping of IO base pointer, door bell read and write pointer */
1425
1426 /* mapping of IO base pointer */
1427 ha->qla4_8xxx_reg =
1428 (struct device_reg_82xx __iomem *)((uint8_t *)ha->nx_pcibase +
1429 0xbc000 + (ha->pdev->devfn << 11));
1430
1431 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
1432 db_len = pci_resource_len(pdev, 4);
1433
1434 /* mapping of doorbell write pointer */
1435 ha->nx_db_wr_ptr = (unsigned long)ioremap(db_base +
1436 (ha->pdev->devfn << 12), 4);
1437 if (!ha->nx_db_wr_ptr) {
1438 printk(KERN_ERR
1439 "cannot remap MMIO doorbell-write (%s), aborting\n",
1440 pci_name(pdev));
1441 goto iospace_error_exit;
1442 }
1443 /* mapping of doorbell read pointer */
1444 ha->nx_db_rd_ptr = (uint8_t *) ha->nx_pcibase + (512 * 1024) +
1445 (ha->pdev->devfn * 8);
1446 if (!ha->nx_db_rd_ptr)
1447 printk(KERN_ERR
1448 "cannot remap MMIO doorbell-read (%s), aborting\n",
1449 pci_name(pdev));
1450 return 0;
1451
1452iospace_error_exit:
1453 return -ENOMEM;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001454}
1455
1456/***
1457 * qla4xxx_iospace_config - maps registers
1458 * @ha: pointer to adapter structure
1459 *
1460 * This routines maps HBA's registers from the pci address space
1461 * into the kernel virtual address space for memory mapped i/o.
1462 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301463int qla4xxx_iospace_config(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001464{
1465 unsigned long pio, pio_len, pio_flags;
1466 unsigned long mmio, mmio_len, mmio_flags;
1467
1468 pio = pci_resource_start(ha->pdev, 0);
1469 pio_len = pci_resource_len(ha->pdev, 0);
1470 pio_flags = pci_resource_flags(ha->pdev, 0);
1471 if (pio_flags & IORESOURCE_IO) {
1472 if (pio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301473 ql4_printk(KERN_WARNING, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07001474 "Invalid PCI I/O region size\n");
1475 pio = 0;
1476 }
1477 } else {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301478 ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001479 pio = 0;
1480 }
1481
1482 /* Use MMIO operations for all accesses. */
1483 mmio = pci_resource_start(ha->pdev, 1);
1484 mmio_len = pci_resource_len(ha->pdev, 1);
1485 mmio_flags = pci_resource_flags(ha->pdev, 1);
1486
1487 if (!(mmio_flags & IORESOURCE_MEM)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301488 ql4_printk(KERN_ERR, ha,
1489 "region #0 not an MMIO resource, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001490
1491 goto iospace_error_exit;
1492 }
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301493
David Somayajuluafaf5a22006-09-19 10:28:00 -07001494 if (mmio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301495 ql4_printk(KERN_ERR, ha,
1496 "Invalid PCI mem region size, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001497 goto iospace_error_exit;
1498 }
1499
1500 if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301501 ql4_printk(KERN_WARNING, ha,
1502 "Failed to reserve PIO/MMIO regions\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001503
1504 goto iospace_error_exit;
1505 }
1506
1507 ha->pio_address = pio;
1508 ha->pio_length = pio_len;
1509 ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
1510 if (!ha->reg) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301511 ql4_printk(KERN_ERR, ha,
1512 "cannot remap MMIO, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001513
1514 goto iospace_error_exit;
1515 }
1516
1517 return 0;
1518
1519iospace_error_exit:
1520 return -ENOMEM;
1521}
1522
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301523static struct isp_operations qla4xxx_isp_ops = {
1524 .iospace_config = qla4xxx_iospace_config,
1525 .pci_config = qla4xxx_pci_config,
1526 .disable_intrs = qla4xxx_disable_intrs,
1527 .enable_intrs = qla4xxx_enable_intrs,
1528 .start_firmware = qla4xxx_start_firmware,
1529 .intr_handler = qla4xxx_intr_handler,
1530 .interrupt_service_routine = qla4xxx_interrupt_service_routine,
1531 .reset_chip = qla4xxx_soft_reset,
1532 .reset_firmware = qla4xxx_hw_reset,
1533 .queue_iocb = qla4xxx_queue_iocb,
1534 .complete_iocb = qla4xxx_complete_iocb,
1535 .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
1536 .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
1537 .get_sys_info = qla4xxx_get_sys_info,
1538};
1539
1540static struct isp_operations qla4_8xxx_isp_ops = {
1541 .iospace_config = qla4_8xxx_iospace_config,
1542 .pci_config = qla4_8xxx_pci_config,
1543 .disable_intrs = qla4_8xxx_disable_intrs,
1544 .enable_intrs = qla4_8xxx_enable_intrs,
1545 .start_firmware = qla4_8xxx_load_risc,
1546 .intr_handler = qla4_8xxx_intr_handler,
1547 .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
1548 .reset_chip = qla4_8xxx_isp_reset,
1549 .reset_firmware = qla4_8xxx_stop_firmware,
1550 .queue_iocb = qla4_8xxx_queue_iocb,
1551 .complete_iocb = qla4_8xxx_complete_iocb,
1552 .rd_shdw_req_q_out = qla4_8xxx_rd_shdw_req_q_out,
1553 .rd_shdw_rsp_q_in = qla4_8xxx_rd_shdw_rsp_q_in,
1554 .get_sys_info = qla4_8xxx_get_sys_info,
1555};
1556
1557uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
1558{
1559 return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
1560}
1561
1562uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
1563{
1564 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
1565}
1566
1567uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
1568{
1569 return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
1570}
1571
1572uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
1573{
1574 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
1575}
1576
David Somayajuluafaf5a22006-09-19 10:28:00 -07001577/**
1578 * qla4xxx_probe_adapter - callback function to probe HBA
1579 * @pdev: pointer to pci_dev structure
1580 * @pci_device_id: pointer to pci_device entry
1581 *
1582 * This routine will probe for Qlogic 4xxx iSCSI host adapters.
1583 * It returns zero if successful. It also initializes all data necessary for
1584 * the driver.
1585 **/
1586static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
1587 const struct pci_device_id *ent)
1588{
1589 int ret = -ENODEV, status;
1590 struct Scsi_Host *host;
1591 struct scsi_qla_host *ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001592 uint8_t init_retry_count = 0;
1593 char buf[34];
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301594 struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001595
1596 if (pci_enable_device(pdev))
1597 return -1;
1598
1599 host = scsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha));
1600 if (host == NULL) {
1601 printk(KERN_WARNING
1602 "qla4xxx: Couldn't allocate host from scsi layer!\n");
1603 goto probe_disable_device;
1604 }
1605
1606 /* Clear our data area */
1607 ha = (struct scsi_qla_host *) host->hostdata;
1608 memset(ha, 0, sizeof(*ha));
1609
1610 /* Save the information from PCI BIOS. */
1611 ha->pdev = pdev;
1612 ha->host = host;
1613 ha->host_no = host->host_no;
1614
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301615 /* Setup Runtime configurable options */
1616 if (is_qla8022(ha)) {
1617 ha->isp_ops = &qla4_8xxx_isp_ops;
1618 rwlock_init(&ha->hw_lock);
1619 ha->qdr_sn_window = -1;
1620 ha->ddr_mn_window = -1;
1621 ha->curr_window = 255;
1622 ha->func_num = PCI_FUNC(ha->pdev->devfn);
1623 nx_legacy_intr = &legacy_intr[ha->func_num];
1624 ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
1625 ha->nx_legacy_intr.tgt_status_reg =
1626 nx_legacy_intr->tgt_status_reg;
1627 ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
1628 ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
1629 } else {
1630 ha->isp_ops = &qla4xxx_isp_ops;
1631 }
1632
David Somayajuluafaf5a22006-09-19 10:28:00 -07001633 /* Configure PCI I/O space. */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301634 ret = ha->isp_ops->iospace_config(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001635 if (ret)
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301636 goto probe_failed_ioconfig;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001637
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301638 ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
David Somayajuluafaf5a22006-09-19 10:28:00 -07001639 pdev->device, pdev->irq, ha->reg);
1640
1641 qla4xxx_config_dma_addressing(ha);
1642
1643 /* Initialize lists and spinlocks. */
1644 INIT_LIST_HEAD(&ha->ddb_list);
1645 INIT_LIST_HEAD(&ha->free_srb_q);
1646
1647 mutex_init(&ha->mbox_sem);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301648 init_completion(&ha->mbx_intr_comp);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001649
1650 spin_lock_init(&ha->hardware_lock);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001651
1652 /* Allocate dma buffers */
1653 if (qla4xxx_mem_alloc(ha)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301654 ql4_printk(KERN_WARNING, ha,
1655 "[ERROR] Failed to allocate memory for adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001656
1657 ret = -ENOMEM;
1658 goto probe_failed;
1659 }
1660
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301661 if (is_qla8022(ha))
1662 (void) qla4_8xxx_get_flash_info(ha);
1663
David Somayajuluafaf5a22006-09-19 10:28:00 -07001664 /*
1665 * Initialize the Host adapter request/response queues and
1666 * firmware
1667 * NOTE: interrupts enabled upon successful completion
1668 */
1669 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301670 while ((!test_bit(AF_ONLINE, &ha->flags)) &&
1671 init_retry_count++ < MAX_INIT_RETRIES) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001672 DEBUG2(printk("scsi: %s: retrying adapter initialization "
1673 "(%d)\n", __func__, init_retry_count));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301674
1675 if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
1676 continue;
1677
David Somayajuluafaf5a22006-09-19 10:28:00 -07001678 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1679 }
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301680
1681 if (!test_bit(AF_ONLINE, &ha->flags)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301682 ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001683
1684 ret = -ENODEV;
1685 goto probe_failed;
1686 }
1687
1688 host->cmd_per_lun = 3;
1689 host->max_channel = 0;
1690 host->max_lun = MAX_LUNS - 1;
1691 host->max_id = MAX_TARGETS;
1692 host->max_cmd_len = IOCB_MAX_CDB_LEN;
1693 host->can_queue = MAX_SRBS ;
1694 host->transportt = qla4xxx_scsi_transport;
1695
1696 ret = scsi_init_shared_tag_map(host, MAX_SRBS);
1697 if (ret) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301698 ql4_printk(KERN_WARNING, ha,
1699 "scsi_init_shared_tag_map failed\n");
1700 goto probe_failed;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001701 }
1702
1703 /* Startup the kernel thread for this host adapter. */
1704 DEBUG2(printk("scsi: %s: Starting kernel thread for "
1705 "qla4xxx_dpc\n", __func__));
1706 sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
1707 ha->dpc_thread = create_singlethread_workqueue(buf);
1708 if (!ha->dpc_thread) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301709 ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001710 ret = -ENODEV;
1711 goto probe_failed;
1712 }
David Howellsc4028952006-11-22 14:57:56 +00001713 INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001714
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301715 /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
1716 * (which is called indirectly by qla4xxx_initialize_adapter),
1717 * so that irqs will be registered after crbinit but before
1718 * mbx_intr_enable.
1719 */
1720 if (!is_qla8022(ha)) {
1721 ret = qla4xxx_request_irqs(ha);
1722 if (ret) {
1723 ql4_printk(KERN_WARNING, ha, "Failed to reserve "
1724 "interrupt %d already in use.\n", pdev->irq);
1725 goto probe_failed;
1726 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001727 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001728
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301729 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001730
1731 /* Start timer thread. */
1732 qla4xxx_start_timer(ha, qla4xxx_timer, 1);
1733
1734 set_bit(AF_INIT_DONE, &ha->flags);
1735
1736 pci_set_drvdata(pdev, ha);
1737
1738 ret = scsi_add_host(host, &pdev->dev);
1739 if (ret)
1740 goto probe_failed;
1741
David Somayajuluafaf5a22006-09-19 10:28:00 -07001742 printk(KERN_INFO
1743 " QLogic iSCSI HBA Driver version: %s\n"
1744 " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
1745 qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
1746 ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
1747 ha->patch_number, ha->build_number);
Mike Christie921601b2008-01-31 13:36:49 -06001748 scsi_scan_host(host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001749 return 0;
1750
David Somayajuluafaf5a22006-09-19 10:28:00 -07001751probe_failed:
1752 qla4xxx_free_adapter(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301753
1754probe_failed_ioconfig:
David Somayajuluafaf5a22006-09-19 10:28:00 -07001755 scsi_host_put(ha->host);
1756
1757probe_disable_device:
1758 pci_disable_device(pdev);
1759
1760 return ret;
1761}
1762
1763/**
1764 * qla4xxx_remove_adapter - calback function to remove adapter.
1765 * @pci_dev: PCI device pointer
1766 **/
1767static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
1768{
1769 struct scsi_qla_host *ha;
1770
1771 ha = pci_get_drvdata(pdev);
1772
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301773 set_bit(AF_HBA_GOING_AWAY, &ha->flags);
David C Somayajulubee4fe82007-05-23 18:03:32 -07001774
David Somayajuluafaf5a22006-09-19 10:28:00 -07001775 /* remove devs from iscsi_sessions to scsi_devices */
1776 qla4xxx_free_ddb_list(ha);
1777
1778 scsi_remove_host(ha->host);
1779
1780 qla4xxx_free_adapter(ha);
1781
1782 scsi_host_put(ha->host);
1783
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301784 pci_disable_device(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001785 pci_set_drvdata(pdev, NULL);
1786}
1787
1788/**
1789 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
1790 * @ha: HA context
1791 *
1792 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
1793 * supported addressing method.
1794 */
Adrian Bunk47975472007-04-26 00:35:16 -07001795static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001796{
1797 int retval;
1798
1799 /* Update our PCI device dma_mask for full 64 bit mask */
Yang Hongyang6a355282009-04-06 19:01:13 -07001800 if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
1801 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001802 dev_dbg(&ha->pdev->dev,
1803 "Failed to set 64 bit PCI consistent mask; "
1804 "using 32 bit.\n");
1805 retval = pci_set_consistent_dma_mask(ha->pdev,
Yang Hongyang284901a2009-04-06 19:01:15 -07001806 DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001807 }
1808 } else
Yang Hongyang284901a2009-04-06 19:01:15 -07001809 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001810}
1811
1812static int qla4xxx_slave_alloc(struct scsi_device *sdev)
1813{
1814 struct iscsi_cls_session *sess = starget_to_session(sdev->sdev_target);
1815 struct ddb_entry *ddb = sess->dd_data;
1816
1817 sdev->hostdata = ddb;
1818 sdev->tagged_supported = 1;
Mike Christied510d962008-07-11 19:50:33 -05001819 scsi_activate_tcq(sdev, QL4_DEF_QDEPTH);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001820 return 0;
1821}
1822
1823static int qla4xxx_slave_configure(struct scsi_device *sdev)
1824{
1825 sdev->tagged_supported = 1;
1826 return 0;
1827}
1828
1829static void qla4xxx_slave_destroy(struct scsi_device *sdev)
1830{
1831 scsi_deactivate_tcq(sdev, 1);
1832}
1833
1834/**
1835 * qla4xxx_del_from_active_array - returns an active srb
1836 * @ha: Pointer to host adapter structure.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001837 * @index: index into the active_array
David Somayajuluafaf5a22006-09-19 10:28:00 -07001838 *
1839 * This routine removes and returns the srb at the specified index
1840 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301841struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
1842 uint32_t index)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001843{
1844 struct srb *srb = NULL;
Vikas Chaudhary53698872010-04-28 11:41:59 +05301845 struct scsi_cmnd *cmd = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001846
Vikas Chaudhary53698872010-04-28 11:41:59 +05301847 cmd = scsi_host_find_tag(ha->host, index);
1848 if (!cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001849 return srb;
1850
Vikas Chaudhary53698872010-04-28 11:41:59 +05301851 srb = (struct srb *)CMD_SP(cmd);
1852 if (!srb)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001853 return srb;
1854
1855 /* update counters */
1856 if (srb->flags & SRB_DMA_VALID) {
1857 ha->req_q_count += srb->iocb_cnt;
1858 ha->iocb_cnt -= srb->iocb_cnt;
1859 if (srb->cmd)
Vikas Chaudhary53698872010-04-28 11:41:59 +05301860 srb->cmd->host_scribble =
1861 (unsigned char *)(unsigned long) MAX_SRBS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001862 }
1863 return srb;
1864}
1865
1866/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07001867 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301868 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001869 * @cmd: Scsi Command to wait on.
1870 *
1871 * This routine waits for the command to be returned by the Firmware
1872 * for some max time.
1873 **/
1874static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
1875 struct scsi_cmnd *cmd)
1876{
1877 int done = 0;
1878 struct srb *rp;
1879 uint32_t max_wait_time = EH_WAIT_CMD_TOV;
1880
1881 do {
1882 /* Checking to see if its returned to OS */
Vikas Chaudhary53698872010-04-28 11:41:59 +05301883 rp = (struct srb *) CMD_SP(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001884 if (rp == NULL) {
1885 done++;
1886 break;
1887 }
1888
1889 msleep(2000);
1890 } while (max_wait_time--);
1891
1892 return done;
1893}
1894
1895/**
1896 * qla4xxx_wait_for_hba_online - waits for HBA to come online
1897 * @ha: Pointer to host adapter structure
1898 **/
1899static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
1900{
1901 unsigned long wait_online;
1902
1903 wait_online = jiffies + (30 * HZ);
1904 while (time_before(jiffies, wait_online)) {
1905
1906 if (adapter_up(ha))
1907 return QLA_SUCCESS;
1908 else if (ha->retry_reset_ha_cnt == 0)
1909 return QLA_ERROR;
1910
1911 msleep(2000);
1912 }
1913
1914 return QLA_ERROR;
1915}
1916
1917/**
Mike Christiece545032008-02-29 18:25:20 -06001918 * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001919 * @ha: pointer to HBA
David Somayajuluafaf5a22006-09-19 10:28:00 -07001920 * @t: target id
1921 * @l: lun id
1922 *
1923 * This function waits for all outstanding commands to a lun to complete. It
1924 * returns 0 if all pending commands are returned and 1 otherwise.
1925 **/
Mike Christiece545032008-02-29 18:25:20 -06001926static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
1927 struct scsi_target *stgt,
1928 struct scsi_device *sdev)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001929{
1930 int cnt;
1931 int status = 0;
1932 struct scsi_cmnd *cmd;
1933
1934 /*
Mike Christiece545032008-02-29 18:25:20 -06001935 * Waiting for all commands for the designated target or dev
1936 * in the active array
David Somayajuluafaf5a22006-09-19 10:28:00 -07001937 */
1938 for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
1939 cmd = scsi_host_find_tag(ha->host, cnt);
Mike Christiece545032008-02-29 18:25:20 -06001940 if (cmd && stgt == scsi_target(cmd->device) &&
1941 (!sdev || sdev == cmd->device)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001942 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
1943 status++;
1944 break;
1945 }
1946 }
1947 }
1948 return status;
1949}
1950
1951/**
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301952 * qla4xxx_eh_abort - callback for abort task.
1953 * @cmd: Pointer to Linux's SCSI command structure
1954 *
1955 * This routine is called by the Linux OS to abort the specified
1956 * command.
1957 **/
1958static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
1959{
1960 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
1961 unsigned int id = cmd->device->id;
1962 unsigned int lun = cmd->device->lun;
1963 unsigned long serial = cmd->serial_number;
1964 struct srb *srb = NULL;
1965 int ret = SUCCESS;
1966 int wait = 0;
1967
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301968 ql4_printk(KERN_INFO, ha,
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301969 "scsi%ld:%d:%d: Abort command issued cmd=%p, pid=%ld\n",
1970 ha->host_no, id, lun, cmd, serial);
1971
1972 srb = (struct srb *) CMD_SP(cmd);
1973
1974 if (!srb)
1975 return SUCCESS;
1976
1977 kref_get(&srb->srb_ref);
1978
1979 if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
1980 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
1981 ha->host_no, id, lun));
1982 ret = FAILED;
1983 } else {
1984 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
1985 ha->host_no, id, lun));
1986 wait = 1;
1987 }
1988
1989 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
1990
1991 /* Wait for command to complete */
1992 if (wait) {
1993 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
1994 DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
1995 ha->host_no, id, lun));
1996 ret = FAILED;
1997 }
1998 }
1999
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302000 ql4_printk(KERN_INFO, ha,
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302001 "scsi%ld:%d:%d: Abort command - %s\n",
2002 ha->host_no, id, lun, (ret == SUCCESS) ? "succeded" : "failed");
2003
2004 return ret;
2005}
2006
2007/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002008 * qla4xxx_eh_device_reset - callback for target reset.
2009 * @cmd: Pointer to Linux's SCSI command structure
2010 *
2011 * This routine is called by the Linux OS to reset all luns on the
2012 * specified target.
2013 **/
2014static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
2015{
2016 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2017 struct ddb_entry *ddb_entry = cmd->device->hostdata;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002018 int ret = FAILED, stat;
2019
Karen Higgins612f7342009-07-15 15:03:01 -05002020 if (!ddb_entry)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002021 return ret;
2022
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302023 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002024 "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
2025 cmd->device->channel, cmd->device->id, cmd->device->lun);
2026
2027 DEBUG2(printk(KERN_INFO
2028 "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
2029 "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
Jens Axboe242f9dc2008-09-14 05:55:09 -07002030 cmd, jiffies, cmd->request->timeout / HZ,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002031 ha->dpc_flags, cmd->result, cmd->allowed));
2032
2033 /* FIXME: wait for hba to go online */
2034 stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
2035 if (stat != QLA_SUCCESS) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302036 ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002037 goto eh_dev_reset_done;
2038 }
2039
Mike Christiece545032008-02-29 18:25:20 -06002040 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2041 cmd->device)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302042 ql4_printk(KERN_INFO, ha,
Mike Christiece545032008-02-29 18:25:20 -06002043 "DEVICE RESET FAILED - waiting for "
2044 "commands.\n");
2045 goto eh_dev_reset_done;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002046 }
2047
David C Somayajulu9d562912008-03-19 11:23:03 -07002048 /* Send marker. */
2049 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2050 MM_LUN_RESET) != QLA_SUCCESS)
2051 goto eh_dev_reset_done;
2052
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302053 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002054 "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
2055 ha->host_no, cmd->device->channel, cmd->device->id,
2056 cmd->device->lun);
2057
2058 ret = SUCCESS;
2059
2060eh_dev_reset_done:
2061
2062 return ret;
2063}
2064
2065/**
Mike Christiece545032008-02-29 18:25:20 -06002066 * qla4xxx_eh_target_reset - callback for target reset.
2067 * @cmd: Pointer to Linux's SCSI command structure
2068 *
2069 * This routine is called by the Linux OS to reset the target.
2070 **/
2071static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
2072{
2073 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2074 struct ddb_entry *ddb_entry = cmd->device->hostdata;
2075 int stat;
2076
2077 if (!ddb_entry)
2078 return FAILED;
2079
2080 starget_printk(KERN_INFO, scsi_target(cmd->device),
2081 "WARM TARGET RESET ISSUED.\n");
2082
2083 DEBUG2(printk(KERN_INFO
2084 "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
2085 "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
Jens Axboe242f9dc2008-09-14 05:55:09 -07002086 ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
Mike Christiece545032008-02-29 18:25:20 -06002087 ha->dpc_flags, cmd->result, cmd->allowed));
2088
2089 stat = qla4xxx_reset_target(ha, ddb_entry);
2090 if (stat != QLA_SUCCESS) {
2091 starget_printk(KERN_INFO, scsi_target(cmd->device),
2092 "WARM TARGET RESET FAILED.\n");
2093 return FAILED;
2094 }
2095
Mike Christiece545032008-02-29 18:25:20 -06002096 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2097 NULL)) {
2098 starget_printk(KERN_INFO, scsi_target(cmd->device),
2099 "WARM TARGET DEVICE RESET FAILED - "
2100 "waiting for commands.\n");
2101 return FAILED;
2102 }
2103
David C Somayajulu9d562912008-03-19 11:23:03 -07002104 /* Send marker. */
2105 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2106 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
2107 starget_printk(KERN_INFO, scsi_target(cmd->device),
2108 "WARM TARGET DEVICE RESET FAILED - "
2109 "marker iocb failed.\n");
2110 return FAILED;
2111 }
2112
Mike Christiece545032008-02-29 18:25:20 -06002113 starget_printk(KERN_INFO, scsi_target(cmd->device),
2114 "WARM TARGET RESET SUCCEEDED.\n");
2115 return SUCCESS;
2116}
2117
2118/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002119 * qla4xxx_eh_host_reset - kernel callback
2120 * @cmd: Pointer to Linux's SCSI command structure
2121 *
2122 * This routine is invoked by the Linux kernel to perform fatal error
2123 * recovery on the specified adapter.
2124 **/
2125static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
2126{
2127 int return_status = FAILED;
2128 struct scsi_qla_host *ha;
2129
2130 ha = (struct scsi_qla_host *) cmd->device->host->hostdata;
2131
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302132 if (ql4xdontresethba) {
2133 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
2134 ha->host_no, __func__));
2135 return FAILED;
2136 }
2137
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302138 ql4_printk(KERN_INFO, ha,
Karen Higginsdca05c42009-07-15 15:03:00 -05002139 "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002140 cmd->device->channel, cmd->device->id, cmd->device->lun);
2141
2142 if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
2143 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
2144 "DEAD.\n", ha->host_no, cmd->device->channel,
2145 __func__));
2146
2147 return FAILED;
2148 }
2149
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302150 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
2151 if (is_qla8022(ha))
2152 set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2153 else
2154 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2155 }
Mike Christie50a29ae2008-03-04 13:26:53 -06002156
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302157 if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002158 return_status = SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002159
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302160 ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
David Somayajuluafaf5a22006-09-19 10:28:00 -07002161 return_status == FAILED ? "FAILED" : "SUCCEDED");
2162
2163 return return_status;
2164}
2165
David Somayajuluafaf5a22006-09-19 10:28:00 -07002166static struct pci_device_id qla4xxx_pci_tbl[] = {
2167 {
2168 .vendor = PCI_VENDOR_ID_QLOGIC,
2169 .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
2170 .subvendor = PCI_ANY_ID,
2171 .subdevice = PCI_ANY_ID,
2172 },
2173 {
2174 .vendor = PCI_VENDOR_ID_QLOGIC,
2175 .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
2176 .subvendor = PCI_ANY_ID,
2177 .subdevice = PCI_ANY_ID,
2178 },
David C Somayajulud9150582006-11-15 17:38:40 -08002179 {
2180 .vendor = PCI_VENDOR_ID_QLOGIC,
2181 .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
2182 .subvendor = PCI_ANY_ID,
2183 .subdevice = PCI_ANY_ID,
2184 },
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302185 {
2186 .vendor = PCI_VENDOR_ID_QLOGIC,
2187 .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
2188 .subvendor = PCI_ANY_ID,
2189 .subdevice = PCI_ANY_ID,
2190 },
David Somayajuluafaf5a22006-09-19 10:28:00 -07002191 {0, 0},
2192};
2193MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
2194
Adrian Bunk47975472007-04-26 00:35:16 -07002195static struct pci_driver qla4xxx_pci_driver = {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002196 .name = DRIVER_NAME,
2197 .id_table = qla4xxx_pci_tbl,
2198 .probe = qla4xxx_probe_adapter,
2199 .remove = qla4xxx_remove_adapter,
2200};
2201
2202static int __init qla4xxx_module_init(void)
2203{
2204 int ret;
2205
2206 /* Allocate cache for SRBs. */
2207 srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09002208 SLAB_HWCACHE_ALIGN, NULL);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002209 if (srb_cachep == NULL) {
2210 printk(KERN_ERR
2211 "%s: Unable to allocate SRB cache..."
2212 "Failing load!\n", DRIVER_NAME);
2213 ret = -ENOMEM;
2214 goto no_srp_cache;
2215 }
2216
2217 /* Derive version string. */
2218 strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
Andrew Vasquez11010fe2006-10-06 09:54:59 -07002219 if (ql4xextended_error_logging)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002220 strcat(qla4xxx_version_str, "-debug");
2221
2222 qla4xxx_scsi_transport =
2223 iscsi_register_transport(&qla4xxx_iscsi_transport);
2224 if (!qla4xxx_scsi_transport){
2225 ret = -ENODEV;
2226 goto release_srb_cache;
2227 }
2228
David Somayajuluafaf5a22006-09-19 10:28:00 -07002229 ret = pci_register_driver(&qla4xxx_pci_driver);
2230 if (ret)
2231 goto unregister_transport;
2232
2233 printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
2234 return 0;
Doug Maxey5ae16db2006-10-05 23:50:07 -05002235
David Somayajuluafaf5a22006-09-19 10:28:00 -07002236unregister_transport:
2237 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
2238release_srb_cache:
2239 kmem_cache_destroy(srb_cachep);
2240no_srp_cache:
2241 return ret;
2242}
2243
2244static void __exit qla4xxx_module_exit(void)
2245{
David Somayajuluafaf5a22006-09-19 10:28:00 -07002246 pci_unregister_driver(&qla4xxx_pci_driver);
2247 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
2248 kmem_cache_destroy(srb_cachep);
2249}
2250
2251module_init(qla4xxx_module_init);
2252module_exit(qla4xxx_module_exit);
2253
2254MODULE_AUTHOR("QLogic Corporation");
2255MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
2256MODULE_LICENSE("GPL");
2257MODULE_VERSION(QLA4XXX_DRIVER_VERSION);