blob: fd1af23e6801bc011513453d0b382ba1978fb303 [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 Chaudhary3013cea2010-07-30 14:25:46 +0530166 DEBUG2(printk("scsi%ld: %s: ddb [%d] session recovery timeout "
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,
Vikas Chaudhary3013cea2010-07-30 14:25:46 +0530169 QL4_SESS_RECOVERY_TMO));
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
Vikas Chaudhary3013cea2010-07-30 14:25:46 +0530301 ddb_entry->sess->recovery_tmo = QL4_SESS_RECOVERY_TMO;
302
David Somayajuluafaf5a22006-09-19 10:28:00 -0700303 err = iscsi_add_session(ddb_entry->sess, ddb_entry->fw_ddb_index);
304 if (err) {
305 DEBUG2(printk(KERN_ERR "Could not add session.\n"));
306 return err;
307 }
308
Mike Christie5d91e202008-05-21 15:54:01 -0500309 ddb_entry->conn = iscsi_create_conn(ddb_entry->sess, 0, 0);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700310 if (!ddb_entry->conn) {
311 iscsi_remove_session(ddb_entry->sess);
312 DEBUG2(printk(KERN_ERR "Could not add connection.\n"));
313 return -ENOMEM;
314 }
Mike Christieb6359302008-01-31 13:36:44 -0600315
316 /* finally ready to go */
317 iscsi_unblock_session(ddb_entry->sess);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700318 return 0;
319}
320
321struct ddb_entry *qla4xxx_alloc_sess(struct scsi_qla_host *ha)
322{
323 struct ddb_entry *ddb_entry;
324 struct iscsi_cls_session *sess;
325
Mike Christie5d91e202008-05-21 15:54:01 -0500326 sess = iscsi_alloc_session(ha->host, &qla4xxx_iscsi_transport,
327 sizeof(struct ddb_entry));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700328 if (!sess)
329 return NULL;
330
331 ddb_entry = sess->dd_data;
332 memset(ddb_entry, 0, sizeof(*ddb_entry));
333 ddb_entry->ha = ha;
334 ddb_entry->sess = sess;
335 return ddb_entry;
336}
337
Mike Christie024f8012008-03-04 13:26:54 -0600338static void qla4xxx_scan_start(struct Scsi_Host *shost)
339{
340 struct scsi_qla_host *ha = shost_priv(shost);
341 struct ddb_entry *ddb_entry, *ddbtemp;
342
343 /* finish setup of sessions that were already setup in firmware */
344 list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
345 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE)
346 qla4xxx_add_sess(ddb_entry);
347 }
348}
349
David Somayajuluafaf5a22006-09-19 10:28:00 -0700350/*
351 * Timer routines
352 */
353
354static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
355 unsigned long interval)
356{
357 DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
358 __func__, ha->host->host_no));
359 init_timer(&ha->timer);
360 ha->timer.expires = jiffies + interval * HZ;
361 ha->timer.data = (unsigned long)ha;
362 ha->timer.function = (void (*)(unsigned long))func;
363 add_timer(&ha->timer);
364 ha->timer_active = 1;
365}
366
367static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
368{
369 del_timer_sync(&ha->timer);
370 ha->timer_active = 0;
371}
372
373/***
374 * qla4xxx_mark_device_missing - mark a device as missing.
375 * @ha: Pointer to host adapter structure.
376 * @ddb_entry: Pointer to device database entry
377 *
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530378 * This routine marks a device missing and close connection.
David Somayajuluafaf5a22006-09-19 10:28:00 -0700379 **/
380void qla4xxx_mark_device_missing(struct scsi_qla_host *ha,
381 struct ddb_entry *ddb_entry)
382{
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530383 if ((atomic_read(&ddb_entry->state) != DDB_STATE_DEAD)) {
384 atomic_set(&ddb_entry->state, DDB_STATE_MISSING);
385 DEBUG2(printk("scsi%ld: ddb [%d] marked MISSING\n",
386 ha->host_no, ddb_entry->fw_ddb_index));
387 } else
388 DEBUG2(printk("scsi%ld: ddb [%d] DEAD\n", ha->host_no,
389 ddb_entry->fw_ddb_index))
390
Mike Christieb6359302008-01-31 13:36:44 -0600391 iscsi_block_session(ddb_entry->sess);
Mike Christiee5bd7b52008-09-24 11:46:10 -0500392 iscsi_conn_error_event(ddb_entry->conn, ISCSI_ERR_CONN_FAILED);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700393}
394
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530395/**
396 * qla4xxx_mark_all_devices_missing - mark all devices as missing.
397 * @ha: Pointer to host adapter structure.
398 *
399 * This routine marks a device missing and resets the relogin retry count.
400 **/
401void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
402{
403 struct ddb_entry *ddb_entry, *ddbtemp;
404 list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
405 qla4xxx_mark_device_missing(ha, ddb_entry);
406 }
407}
408
David Somayajuluafaf5a22006-09-19 10:28:00 -0700409static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
410 struct ddb_entry *ddb_entry,
411 struct scsi_cmnd *cmd,
412 void (*done)(struct scsi_cmnd *))
413{
414 struct srb *srb;
415
416 srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
417 if (!srb)
418 return srb;
419
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530420 kref_init(&srb->srb_ref);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700421 srb->ha = ha;
422 srb->ddb = ddb_entry;
423 srb->cmd = cmd;
424 srb->flags = 0;
Vikas Chaudhary53698872010-04-28 11:41:59 +0530425 CMD_SP(cmd) = (void *)srb;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700426 cmd->scsi_done = done;
427
428 return srb;
429}
430
431static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
432{
433 struct scsi_cmnd *cmd = srb->cmd;
434
435 if (srb->flags & SRB_DMA_VALID) {
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +0900436 scsi_dma_unmap(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700437 srb->flags &= ~SRB_DMA_VALID;
438 }
Vikas Chaudhary53698872010-04-28 11:41:59 +0530439 CMD_SP(cmd) = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700440}
441
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530442void qla4xxx_srb_compl(struct kref *ref)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700443{
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530444 struct srb *srb = container_of(ref, struct srb, srb_ref);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700445 struct scsi_cmnd *cmd = srb->cmd;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530446 struct scsi_qla_host *ha = srb->ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700447
448 qla4xxx_srb_free_dma(ha, srb);
449
450 mempool_free(srb, ha->srb_mempool);
451
452 cmd->scsi_done(cmd);
453}
454
455/**
456 * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
457 * @cmd: Pointer to Linux's SCSI command structure
458 * @done_fn: Function that the driver calls to notify the SCSI mid-layer
459 * that the command has been processed.
460 *
461 * Remarks:
462 * This routine is invoked by Linux to send a SCSI command to the driver.
463 * The mid-level driver tries to ensure that queuecommand never gets
464 * invoked concurrently with itself or the interrupt handler (although
465 * the interrupt handler may call this routine as part of request-
466 * completion handling). Unfortunely, it sometimes calls the scheduler
467 * in interrupt context which is a big NO! NO!.
468 **/
469static int qla4xxx_queuecommand(struct scsi_cmnd *cmd,
470 void (*done)(struct scsi_cmnd *))
471{
472 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
473 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christie7fb19212008-01-31 13:36:45 -0600474 struct iscsi_cls_session *sess = ddb_entry->sess;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700475 struct srb *srb;
476 int rval;
477
Mike Christie7fb19212008-01-31 13:36:45 -0600478 if (!sess) {
479 cmd->result = DID_IMM_RETRY << 16;
480 goto qc_fail_command;
481 }
482
483 rval = iscsi_session_chkready(sess);
484 if (rval) {
485 cmd->result = rval;
486 goto qc_fail_command;
487 }
488
David Somayajuluafaf5a22006-09-19 10:28:00 -0700489 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
490 if (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD) {
491 cmd->result = DID_NO_CONNECT << 16;
492 goto qc_fail_command;
493 }
Mike Christiec5e98e92008-08-17 15:24:39 -0500494 return SCSI_MLQUEUE_TARGET_BUSY;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700495 }
496
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530497 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
498 test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
499 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
500 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
501 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
502 !test_bit(AF_ONLINE, &ha->flags) ||
503 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
David C Somayajulu477ffb92007-01-22 12:26:11 -0800504 goto qc_host_busy;
505
David Somayajuluafaf5a22006-09-19 10:28:00 -0700506 spin_unlock_irq(ha->host->host_lock);
507
508 srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd, done);
509 if (!srb)
510 goto qc_host_busy_lock;
511
512 rval = qla4xxx_send_command_to_isp(ha, srb);
513 if (rval != QLA_SUCCESS)
514 goto qc_host_busy_free_sp;
515
516 spin_lock_irq(ha->host->host_lock);
517 return 0;
518
519qc_host_busy_free_sp:
520 qla4xxx_srb_free_dma(ha, srb);
521 mempool_free(srb, ha->srb_mempool);
522
523qc_host_busy_lock:
524 spin_lock_irq(ha->host->host_lock);
525
526qc_host_busy:
527 return SCSI_MLQUEUE_HOST_BUSY;
528
529qc_fail_command:
530 done(cmd);
531
532 return 0;
533}
534
535/**
536 * qla4xxx_mem_free - frees memory allocated to adapter
537 * @ha: Pointer to host adapter structure.
538 *
539 * Frees memory previously allocated by qla4xxx_mem_alloc
540 **/
541static void qla4xxx_mem_free(struct scsi_qla_host *ha)
542{
543 if (ha->queues)
544 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
545 ha->queues_dma);
546
547 ha->queues_len = 0;
548 ha->queues = NULL;
549 ha->queues_dma = 0;
550 ha->request_ring = NULL;
551 ha->request_dma = 0;
552 ha->response_ring = NULL;
553 ha->response_dma = 0;
554 ha->shadow_regs = NULL;
555 ha->shadow_regs_dma = 0;
556
557 /* Free srb pool. */
558 if (ha->srb_mempool)
559 mempool_destroy(ha->srb_mempool);
560
561 ha->srb_mempool = NULL;
562
563 /* release io space registers */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530564 if (is_qla8022(ha)) {
565 if (ha->nx_pcibase)
566 iounmap(
567 (struct device_reg_82xx __iomem *)ha->nx_pcibase);
568
569 if (ha->nx_db_wr_ptr)
570 iounmap(
571 (struct device_reg_82xx __iomem *)ha->nx_db_wr_ptr);
572 } else if (ha->reg)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700573 iounmap(ha->reg);
574 pci_release_regions(ha->pdev);
575}
576
577/**
578 * qla4xxx_mem_alloc - allocates memory for use by adapter.
579 * @ha: Pointer to host adapter structure
580 *
581 * Allocates DMA memory for request and response queues. Also allocates memory
582 * for srbs.
583 **/
584static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
585{
586 unsigned long align;
587
588 /* Allocate contiguous block of DMA memory for queues. */
589 ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
590 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
591 sizeof(struct shadow_regs) +
592 MEM_ALIGN_VALUE +
593 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
594 ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
595 &ha->queues_dma, GFP_KERNEL);
596 if (ha->queues == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530597 ql4_printk(KERN_WARNING, ha,
598 "Memory Allocation failed - queues.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700599
600 goto mem_alloc_error_exit;
601 }
602 memset(ha->queues, 0, ha->queues_len);
603
604 /*
605 * As per RISC alignment requirements -- the bus-address must be a
606 * multiple of the request-ring size (in bytes).
607 */
608 align = 0;
609 if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
610 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
611 (MEM_ALIGN_VALUE - 1));
612
613 /* Update request and response queue pointers. */
614 ha->request_dma = ha->queues_dma + align;
615 ha->request_ring = (struct queue_entry *) (ha->queues + align);
616 ha->response_dma = ha->queues_dma + align +
617 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
618 ha->response_ring = (struct queue_entry *) (ha->queues + align +
619 (REQUEST_QUEUE_DEPTH *
620 QUEUE_SIZE));
621 ha->shadow_regs_dma = ha->queues_dma + align +
622 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
623 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
624 ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
625 (REQUEST_QUEUE_DEPTH *
626 QUEUE_SIZE) +
627 (RESPONSE_QUEUE_DEPTH *
628 QUEUE_SIZE));
629
630 /* Allocate memory for srb pool. */
631 ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
632 mempool_free_slab, srb_cachep);
633 if (ha->srb_mempool == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530634 ql4_printk(KERN_WARNING, ha,
635 "Memory Allocation failed - SRB Pool.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700636
637 goto mem_alloc_error_exit;
638 }
639
640 return QLA_SUCCESS;
641
642mem_alloc_error_exit:
643 qla4xxx_mem_free(ha);
644 return QLA_ERROR;
645}
646
647/**
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530648 * qla4_8xxx_check_fw_alive - Check firmware health
649 * @ha: Pointer to host adapter structure.
650 *
651 * Context: Interrupt
652 **/
653static void qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
654{
655 uint32_t fw_heartbeat_counter, halt_status;
656
657 fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
658
659 if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
660 ha->seconds_since_last_heartbeat++;
661 /* FW not alive after 2 seconds */
662 if (ha->seconds_since_last_heartbeat == 2) {
663 ha->seconds_since_last_heartbeat = 0;
664 halt_status = qla4_8xxx_rd_32(ha,
665 QLA82XX_PEG_HALT_STATUS1);
666 /* Since we cannot change dev_state in interrupt
667 * context, set appropriate DPC flag then wakeup
668 * DPC */
669 if (halt_status & HALT_STATUS_UNRECOVERABLE)
670 set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
671 else {
672 printk("scsi%ld: %s: detect abort needed!\n",
673 ha->host_no, __func__);
674 set_bit(DPC_RESET_HA, &ha->dpc_flags);
675 }
676 qla4xxx_wake_dpc(ha);
677 }
678 }
679 ha->fw_heartbeat_counter = fw_heartbeat_counter;
680}
681
682/**
683 * qla4_8xxx_watchdog - Poll dev state
684 * @ha: Pointer to host adapter structure.
685 *
686 * Context: Interrupt
687 **/
688void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
689{
690 uint32_t dev_state;
691
692 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
693
694 /* don't poll if reset is going on */
695 if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags)) {
696 if (dev_state == QLA82XX_DEV_NEED_RESET &&
697 !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
698 printk("scsi%ld: %s: HW State: NEED RESET!\n",
699 ha->host_no, __func__);
700 set_bit(DPC_RESET_HA, &ha->dpc_flags);
701 qla4xxx_wake_dpc(ha);
702 } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
703 !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
704 printk("scsi%ld: %s: HW State: NEED QUIES!\n",
705 ha->host_no, __func__);
706 set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
707 qla4xxx_wake_dpc(ha);
708 } else {
709 /* Check firmware health */
710 qla4_8xxx_check_fw_alive(ha);
711 }
712 }
713}
714
715/**
David Somayajuluafaf5a22006-09-19 10:28:00 -0700716 * qla4xxx_timer - checks every second for work to do.
717 * @ha: Pointer to host adapter structure.
718 **/
719static void qla4xxx_timer(struct scsi_qla_host *ha)
720{
721 struct ddb_entry *ddb_entry, *dtemp;
722 int start_dpc = 0;
723
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530724 if (test_bit(AF_HBA_GOING_AWAY, &ha->flags)) {
725 DEBUG2(ql4_printk(KERN_INFO, ha, "%s exited. HBA GOING AWAY\n",
726 __func__));
727 return;
728 }
729
730 if (is_qla8022(ha)) {
731 qla4_8xxx_watchdog(ha);
732 }
733
David Somayajuluafaf5a22006-09-19 10:28:00 -0700734 /* Search for relogin's to time-out and port down retry. */
735 list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
736 /* Count down time between sending relogins */
737 if (adapter_up(ha) &&
738 !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
739 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
740 if (atomic_read(&ddb_entry->retry_relogin_timer) !=
741 INVALID_ENTRY) {
742 if (atomic_read(&ddb_entry->retry_relogin_timer)
743 == 0) {
744 atomic_set(&ddb_entry->
745 retry_relogin_timer,
746 INVALID_ENTRY);
747 set_bit(DPC_RELOGIN_DEVICE,
748 &ha->dpc_flags);
749 set_bit(DF_RELOGIN, &ddb_entry->flags);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530750 DEBUG2(printk("scsi%ld: %s: ddb [%d]"
David Somayajuluafaf5a22006-09-19 10:28:00 -0700751 " login device\n",
752 ha->host_no, __func__,
753 ddb_entry->fw_ddb_index));
754 } else
755 atomic_dec(&ddb_entry->
756 retry_relogin_timer);
757 }
758 }
759
760 /* Wait for relogin to timeout */
761 if (atomic_read(&ddb_entry->relogin_timer) &&
762 (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
763 /*
764 * If the relogin times out and the device is
765 * still NOT ONLINE then try and relogin again.
766 */
767 if (atomic_read(&ddb_entry->state) !=
768 DDB_STATE_ONLINE &&
769 ddb_entry->fw_ddb_device_state ==
770 DDB_DS_SESSION_FAILED) {
771 /* Reset retry relogin timer */
772 atomic_inc(&ddb_entry->relogin_retry_count);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530773 DEBUG2(printk("scsi%ld: ddb [%d] relogin"
David Somayajuluafaf5a22006-09-19 10:28:00 -0700774 " timed out-retrying"
775 " relogin (%d)\n",
776 ha->host_no,
777 ddb_entry->fw_ddb_index,
778 atomic_read(&ddb_entry->
779 relogin_retry_count))
780 );
781 start_dpc++;
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530782 DEBUG(printk("scsi%ld:%d:%d: ddb [%d] "
David Somayajuluafaf5a22006-09-19 10:28:00 -0700783 "initate relogin after"
784 " %d seconds\n",
785 ha->host_no, ddb_entry->bus,
786 ddb_entry->target,
787 ddb_entry->fw_ddb_index,
788 ddb_entry->default_time2wait + 4)
789 );
790
791 atomic_set(&ddb_entry->retry_relogin_timer,
792 ddb_entry->default_time2wait + 4);
793 }
794 }
795 }
796
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530797 if (!is_qla8022(ha)) {
798 /* Check for heartbeat interval. */
799 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
800 ha->heartbeat_interval != 0) {
801 ha->seconds_since_last_heartbeat++;
802 if (ha->seconds_since_last_heartbeat >
803 ha->heartbeat_interval + 2)
804 set_bit(DPC_RESET_HA, &ha->dpc_flags);
805 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700806 }
807
David Somayajuluafaf5a22006-09-19 10:28:00 -0700808 /* Wakeup the dpc routine for this adapter, if needed. */
809 if ((start_dpc ||
810 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
811 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
812 test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530813 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -0700814 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
815 test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +0530816 test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530817 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
818 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -0700819 test_bit(DPC_AEN, &ha->dpc_flags)) &&
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530820 !test_bit(AF_DPC_SCHEDULED, &ha->flags) &&
David Somayajuluafaf5a22006-09-19 10:28:00 -0700821 ha->dpc_thread) {
822 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
823 " - dpc flags = 0x%lx\n",
824 ha->host_no, __func__, ha->dpc_flags));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530825 qla4xxx_wake_dpc(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700826 }
827
828 /* Reschedule timer thread to call us back in one second */
829 mod_timer(&ha->timer, jiffies + HZ);
830
831 DEBUG2(ha->seconds_since_last_intr++);
832}
833
834/**
835 * qla4xxx_cmd_wait - waits for all outstanding commands to complete
836 * @ha: Pointer to host adapter structure.
837 *
838 * This routine stalls the driver until all outstanding commands are returned.
839 * Caller must release the Hardware Lock prior to calling this routine.
840 **/
841static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
842{
843 uint32_t index = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700844 unsigned long flags;
845 struct scsi_cmnd *cmd;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700846
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530847 unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
848
849 DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
850 "complete\n", WAIT_CMD_TOV));
851
852 while (!time_after_eq(jiffies, wtime)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700853 spin_lock_irqsave(&ha->hardware_lock, flags);
854 /* Find a command that hasn't completed. */
855 for (index = 0; index < ha->host->can_queue; index++) {
856 cmd = scsi_host_find_tag(ha->host, index);
857 if (cmd != NULL)
858 break;
859 }
860 spin_unlock_irqrestore(&ha->hardware_lock, flags);
861
862 /* If No Commands are pending, wait is complete */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530863 if (index == ha->host->can_queue)
864 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700865
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530866 msleep(1000);
867 }
868 /* If we timed out on waiting for commands to come back
869 * return ERROR. */
870 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700871}
872
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530873int qla4xxx_hw_reset(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700874{
David Somayajuluafaf5a22006-09-19 10:28:00 -0700875 uint32_t ctrl_status;
David C Somayajulu477ffb92007-01-22 12:26:11 -0800876 unsigned long flags = 0;
877
878 DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700879
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530880 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
881 return QLA_ERROR;
882
David Somayajuluafaf5a22006-09-19 10:28:00 -0700883 spin_lock_irqsave(&ha->hardware_lock, flags);
884
885 /*
886 * If the SCSI Reset Interrupt bit is set, clear it.
887 * Otherwise, the Soft Reset won't work.
888 */
889 ctrl_status = readw(&ha->reg->ctrl_status);
890 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
891 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
892
893 /* Issue Soft Reset */
894 writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
895 readl(&ha->reg->ctrl_status);
896
897 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530898 return QLA_SUCCESS;
David C Somayajulu477ffb92007-01-22 12:26:11 -0800899}
900
901/**
902 * qla4xxx_soft_reset - performs soft reset.
903 * @ha: Pointer to host adapter structure.
904 **/
905int qla4xxx_soft_reset(struct scsi_qla_host *ha)
906{
907 uint32_t max_wait_time;
908 unsigned long flags = 0;
909 int status = QLA_ERROR;
910 uint32_t ctrl_status;
911
912 qla4xxx_hw_reset(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700913
914 /* Wait until the Network Reset Intr bit is cleared */
915 max_wait_time = RESET_INTR_TOV;
916 do {
917 spin_lock_irqsave(&ha->hardware_lock, flags);
918 ctrl_status = readw(&ha->reg->ctrl_status);
919 spin_unlock_irqrestore(&ha->hardware_lock, flags);
920
921 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
922 break;
923
924 msleep(1000);
925 } while ((--max_wait_time));
926
927 if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
928 DEBUG2(printk(KERN_WARNING
929 "scsi%ld: Network Reset Intr not cleared by "
930 "Network function, clearing it now!\n",
931 ha->host_no));
932 spin_lock_irqsave(&ha->hardware_lock, flags);
933 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
934 readl(&ha->reg->ctrl_status);
935 spin_unlock_irqrestore(&ha->hardware_lock, flags);
936 }
937
938 /* Wait until the firmware tells us the Soft Reset is done */
939 max_wait_time = SOFT_RESET_TOV;
940 do {
941 spin_lock_irqsave(&ha->hardware_lock, flags);
942 ctrl_status = readw(&ha->reg->ctrl_status);
943 spin_unlock_irqrestore(&ha->hardware_lock, flags);
944
945 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
946 status = QLA_SUCCESS;
947 break;
948 }
949
950 msleep(1000);
951 } while ((--max_wait_time));
952
953 /*
954 * Also, make sure that the SCSI Reset Interrupt bit has been cleared
955 * after the soft reset has taken place.
956 */
957 spin_lock_irqsave(&ha->hardware_lock, flags);
958 ctrl_status = readw(&ha->reg->ctrl_status);
959 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
960 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
961 readl(&ha->reg->ctrl_status);
962 }
963 spin_unlock_irqrestore(&ha->hardware_lock, flags);
964
965 /* If soft reset fails then most probably the bios on other
966 * function is also enabled.
967 * Since the initialization is sequential the other fn
968 * wont be able to acknowledge the soft reset.
969 * Issue a force soft reset to workaround this scenario.
970 */
971 if (max_wait_time == 0) {
972 /* Issue Force Soft Reset */
973 spin_lock_irqsave(&ha->hardware_lock, flags);
974 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
975 readl(&ha->reg->ctrl_status);
976 spin_unlock_irqrestore(&ha->hardware_lock, flags);
977 /* Wait until the firmware tells us the Soft Reset is done */
978 max_wait_time = SOFT_RESET_TOV;
979 do {
980 spin_lock_irqsave(&ha->hardware_lock, flags);
981 ctrl_status = readw(&ha->reg->ctrl_status);
982 spin_unlock_irqrestore(&ha->hardware_lock, flags);
983
984 if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
985 status = QLA_SUCCESS;
986 break;
987 }
988
989 msleep(1000);
990 } while ((--max_wait_time));
991 }
992
993 return status;
994}
995
996/**
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530997 * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
David Somayajuluafaf5a22006-09-19 10:28:00 -0700998 * @ha: Pointer to host adapter structure.
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530999 * @res: returned scsi status
David Somayajuluafaf5a22006-09-19 10:28:00 -07001000 *
1001 * This routine is called just prior to a HARD RESET to return all
1002 * outstanding commands back to the Operating System.
1003 * Caller should make sure that the following locks are released
1004 * before this calling routine: Hardware lock, and io_request_lock.
1005 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301006static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001007{
1008 struct srb *srb;
1009 int i;
1010 unsigned long flags;
1011
1012 spin_lock_irqsave(&ha->hardware_lock, flags);
1013 for (i = 0; i < ha->host->can_queue; i++) {
1014 srb = qla4xxx_del_from_active_array(ha, i);
1015 if (srb != NULL) {
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301016 srb->cmd->result = res;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301017 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001018 }
1019 }
1020 spin_unlock_irqrestore(&ha->hardware_lock, flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001021}
1022
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301023void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
1024{
1025 clear_bit(AF_ONLINE, &ha->flags);
1026
1027 /* Disable the board */
1028 ql4_printk(KERN_INFO, ha, "Disabling the board\n");
1029 set_bit(AF_HBA_GOING_AWAY, &ha->flags);
1030
1031 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
1032 qla4xxx_mark_all_devices_missing(ha);
1033 clear_bit(AF_INIT_DONE, &ha->flags);
1034}
1035
David Somayajuluafaf5a22006-09-19 10:28:00 -07001036/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07001037 * qla4xxx_recover_adapter - recovers adapter after a fatal error
1038 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001039 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301040static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001041{
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301042 int status = QLA_ERROR;
1043 uint8_t reset_chip = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001044
1045 /* Stall incoming I/O until we are done */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301046 scsi_block_requests(ha->host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001047 clear_bit(AF_ONLINE, &ha->flags);
Mike Christie50a29ae2008-03-04 13:26:53 -06001048
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301049 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001050
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301051 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001052
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301053 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
1054 reset_chip = 1;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001055
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301056 /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
1057 * do not reset adapter, jump to initialize_adapter */
1058 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1059 status = QLA_SUCCESS;
1060 goto recover_ha_init_adapter;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001061 }
1062
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301063 /* For the ISP-82xx adapter, issue a stop_firmware if invoked
1064 * from eh_host_reset or ioctl module */
1065 if (is_qla8022(ha) && !reset_chip &&
1066 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
1067
1068 DEBUG2(ql4_printk(KERN_INFO, ha,
1069 "scsi%ld: %s - Performing stop_firmware...\n",
1070 ha->host_no, __func__));
1071 status = ha->isp_ops->reset_firmware(ha);
1072 if (status == QLA_SUCCESS) {
1073 qla4xxx_cmd_wait(ha);
1074 ha->isp_ops->disable_intrs(ha);
1075 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1076 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1077 } else {
1078 /* If the stop_firmware fails then
1079 * reset the entire chip */
1080 reset_chip = 1;
1081 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1082 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1083 }
1084 }
1085
1086 /* Issue full chip reset if recovering from a catastrophic error,
1087 * or if stop_firmware fails for ISP-82xx.
1088 * This is the default case for ISP-4xxx */
1089 if (!is_qla8022(ha) || reset_chip) {
1090 qla4xxx_cmd_wait(ha);
1091 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1092 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1093 DEBUG2(ql4_printk(KERN_INFO, ha,
1094 "scsi%ld: %s - Performing chip reset..\n",
1095 ha->host_no, __func__));
1096 status = ha->isp_ops->reset_chip(ha);
1097 }
1098
1099 /* Flush any pending ddb changed AENs */
1100 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1101
1102recover_ha_init_adapter:
1103 /* Upon successful firmware/chip reset, re-initialize the adapter */
1104 if (status == QLA_SUCCESS) {
1105 /* For ISP-4xxx, force function 1 to always initialize
1106 * before function 3 to prevent both funcions from
1107 * stepping on top of the other */
1108 if (!is_qla8022(ha) && (ha->mac_index == 3))
1109 ssleep(6);
1110
1111 /* NOTE: AF_ONLINE flag set upon successful completion of
1112 * qla4xxx_initialize_adapter */
1113 status = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
1114 }
1115
1116 /* Retry failed adapter initialization, if necessary
1117 * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
1118 * case to prevent ping-pong resets between functions */
1119 if (!test_bit(AF_ONLINE, &ha->flags) &&
1120 !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001121 /* Adapter initialization failed, see if we can retry
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301122 * resetting the ha.
1123 * Since we don't want to block the DPC for too long
1124 * with multiple resets in the same thread,
1125 * utilize DPC to retry */
David Somayajuluafaf5a22006-09-19 10:28:00 -07001126 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
1127 ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
1128 DEBUG2(printk("scsi%ld: recover adapter - retrying "
1129 "(%d) more times\n", ha->host_no,
1130 ha->retry_reset_ha_cnt));
1131 set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1132 status = QLA_ERROR;
1133 } else {
1134 if (ha->retry_reset_ha_cnt > 0) {
1135 /* Schedule another Reset HA--DPC will retry */
1136 ha->retry_reset_ha_cnt--;
1137 DEBUG2(printk("scsi%ld: recover adapter - "
1138 "retry remaining %d\n",
1139 ha->host_no,
1140 ha->retry_reset_ha_cnt));
1141 status = QLA_ERROR;
1142 }
1143
1144 if (ha->retry_reset_ha_cnt == 0) {
1145 /* Recover adapter retries have been exhausted.
1146 * Adapter DEAD */
1147 DEBUG2(printk("scsi%ld: recover adapter "
1148 "failed - board disabled\n",
1149 ha->host_no));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301150 qla4xxx_dead_adapter_cleanup(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001151 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1152 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301153 clear_bit(DPC_RESET_HA_FW_CONTEXT,
David Somayajuluafaf5a22006-09-19 10:28:00 -07001154 &ha->dpc_flags);
1155 status = QLA_ERROR;
1156 }
1157 }
1158 } else {
1159 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301160 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001161 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1162 }
1163
1164 ha->adapter_error_count++;
1165
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301166 if (test_bit(AF_ONLINE, &ha->flags))
1167 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001168
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301169 scsi_unblock_requests(ha->host);
1170
1171 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
1172 DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
1173 status == QLA_ERROR ? "FAILED" : "SUCCEDED"));
1174
David Somayajuluafaf5a22006-09-19 10:28:00 -07001175 return status;
1176}
1177
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301178void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
1179{
1180 if (ha->dpc_thread &&
1181 !test_bit(AF_DPC_SCHEDULED, &ha->flags)) {
1182 set_bit(AF_DPC_SCHEDULED, &ha->flags);
1183 queue_work(ha->dpc_thread, &ha->dpc_work);
1184 }
1185}
1186
David Somayajuluafaf5a22006-09-19 10:28:00 -07001187/**
1188 * qla4xxx_do_dpc - dpc routine
1189 * @data: in our case pointer to adapter structure
1190 *
1191 * This routine is a task that is schedule by the interrupt handler
1192 * to perform the background processing for interrupts. We put it
1193 * on a task queue that is consumed whenever the scheduler runs; that's
1194 * so you can do anything (i.e. put the process to sleep etc). In fact,
1195 * the mid-level tries to sleep when it reaches the driver threshold
1196 * "host->can_queue". This can cause a panic if we were in our interrupt code.
1197 **/
David Howellsc4028952006-11-22 14:57:56 +00001198static void qla4xxx_do_dpc(struct work_struct *work)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001199{
David Howellsc4028952006-11-22 14:57:56 +00001200 struct scsi_qla_host *ha =
1201 container_of(work, struct scsi_qla_host, dpc_work);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001202 struct ddb_entry *ddb_entry, *dtemp;
David C Somayajulu477ffb92007-01-22 12:26:11 -08001203 int status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001204
David C Somayajuluf26b9042006-11-15 16:41:09 -08001205 DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301206 "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
1207 ha->host_no, __func__, ha->flags, ha->dpc_flags))
David Somayajuluafaf5a22006-09-19 10:28:00 -07001208
1209 /* Initialization not yet finished. Don't do anything yet. */
1210 if (!test_bit(AF_INIT_DONE, &ha->flags))
1211 return;
1212
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301213 /* HBA is in the process of being permanently disabled.
1214 * Don't process anything */
1215 if (test_bit(AF_HBA_GOING_AWAY, &ha->flags))
1216 return;
1217
1218 if (is_qla8022(ha)) {
1219 if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
1220 qla4_8xxx_idc_lock(ha);
1221 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
1222 QLA82XX_DEV_FAILED);
1223 qla4_8xxx_idc_unlock(ha);
1224 ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
1225 qla4_8xxx_device_state_handler(ha);
1226 }
1227 if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
1228 qla4_8xxx_need_qsnt_handler(ha);
1229 }
1230 }
1231
1232 if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
1233 (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07001234 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301235 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
1236 if (ql4xdontresethba) {
1237 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
1238 ha->host_no, __func__));
1239 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
1240 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1241 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1242 goto dpc_post_reset_ha;
1243 }
1244 if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
1245 test_bit(DPC_RESET_HA, &ha->dpc_flags))
1246 qla4xxx_recover_adapter(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001247
David C Somayajulu477ffb92007-01-22 12:26:11 -08001248 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001249 uint8_t wait_time = RESET_INTR_TOV;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001250
David Somayajuluafaf5a22006-09-19 10:28:00 -07001251 while ((readw(&ha->reg->ctrl_status) &
1252 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
1253 if (--wait_time == 0)
1254 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001255 msleep(1000);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001256 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001257 if (wait_time == 0)
1258 DEBUG2(printk("scsi%ld: %s: SR|FSR "
1259 "bit not cleared-- resetting\n",
1260 ha->host_no, __func__));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301261 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
David C Somayajulu477ffb92007-01-22 12:26:11 -08001262 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
1263 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301264 status = qla4xxx_recover_adapter(ha);
David C Somayajulu477ffb92007-01-22 12:26:11 -08001265 }
1266 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1267 if (status == QLA_SUCCESS)
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301268 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001269 }
1270 }
1271
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301272dpc_post_reset_ha:
David Somayajuluafaf5a22006-09-19 10:28:00 -07001273 /* ---- process AEN? --- */
1274 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
1275 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
1276
1277 /* ---- Get DHCP IP Address? --- */
1278 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
1279 qla4xxx_get_dhcp_ip_address(ha);
1280
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301281 /* ---- link change? --- */
1282 if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
1283 if (!test_bit(AF_LINK_UP, &ha->flags)) {
1284 /* ---- link down? --- */
1285 list_for_each_entry_safe(ddb_entry, dtemp,
1286 &ha->ddb_list, list) {
1287 if (atomic_read(&ddb_entry->state) ==
1288 DDB_STATE_ONLINE)
1289 qla4xxx_mark_device_missing(ha,
1290 ddb_entry);
1291 }
1292 } else {
1293 /* ---- link up? --- *
1294 * F/W will auto login to all devices ONLY ONCE after
1295 * link up during driver initialization and runtime
1296 * fatal error recovery. Therefore, the driver must
1297 * manually relogin to devices when recovering from
1298 * connection failures, logouts, expired KATO, etc. */
1299
1300 list_for_each_entry_safe(ddb_entry, dtemp,
1301 &ha->ddb_list, list) {
1302 if ((atomic_read(&ddb_entry->state) ==
1303 DDB_STATE_MISSING) ||
1304 (atomic_read(&ddb_entry->state) ==
1305 DDB_STATE_DEAD)) {
1306 if (ddb_entry->fw_ddb_device_state ==
1307 DDB_DS_SESSION_ACTIVE) {
1308 atomic_set(&ddb_entry->state,
1309 DDB_STATE_ONLINE);
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301310 ql4_printk(KERN_INFO, ha,
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301311 "scsi%ld: %s: ddb[%d]"
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301312 " marked ONLINE\n",
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301313 ha->host_no, __func__,
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301314 ddb_entry->fw_ddb_index);
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301315
1316 iscsi_unblock_session(
1317 ddb_entry->sess);
1318 } else
1319 qla4xxx_relogin_device(
1320 ha, ddb_entry);
1321 }
1322
1323 }
1324 }
1325 }
1326
David Somayajuluafaf5a22006-09-19 10:28:00 -07001327 /* ---- relogin device? --- */
1328 if (adapter_up(ha) &&
1329 test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
1330 list_for_each_entry_safe(ddb_entry, dtemp,
1331 &ha->ddb_list, list) {
1332 if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
1333 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
1334 qla4xxx_relogin_device(ha, ddb_entry);
1335
1336 /*
1337 * If mbx cmd times out there is no point
1338 * in continuing further.
1339 * With large no of targets this can hang
1340 * the system.
1341 */
1342 if (test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
1343 printk(KERN_WARNING "scsi%ld: %s: "
1344 "need to reset hba\n",
1345 ha->host_no, __func__);
1346 break;
1347 }
1348 }
1349 }
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301350 clear_bit(AF_DPC_SCHEDULED, &ha->flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001351}
1352
1353/**
1354 * qla4xxx_free_adapter - release the adapter
1355 * @ha: pointer to adapter structure
1356 **/
1357static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
1358{
1359
1360 if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
1361 /* Turn-off interrupts on the card. */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301362 ha->isp_ops->disable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001363 }
1364
David Somayajuluafaf5a22006-09-19 10:28:00 -07001365 /* Remove timer thread, if present */
1366 if (ha->timer_active)
1367 qla4xxx_stop_timer(ha);
1368
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301369 /* Kill the kernel thread for this host */
1370 if (ha->dpc_thread)
1371 destroy_workqueue(ha->dpc_thread);
1372
1373 /* Put firmware in known state */
1374 ha->isp_ops->reset_firmware(ha);
1375
1376 if (is_qla8022(ha)) {
1377 qla4_8xxx_idc_lock(ha);
1378 qla4_8xxx_clear_drv_active(ha);
1379 qla4_8xxx_idc_unlock(ha);
1380 }
1381
David Somayajuluafaf5a22006-09-19 10:28:00 -07001382 /* Detach interrupts */
1383 if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301384 qla4xxx_free_irqs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001385
David C Somayajulubee4fe82007-05-23 18:03:32 -07001386 /* free extra memory */
1387 qla4xxx_mem_free(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301388}
David C Somayajulubee4fe82007-05-23 18:03:32 -07001389
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301390int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
1391{
1392 int status = 0;
1393 uint8_t revision_id;
1394 unsigned long mem_base, mem_len, db_base, db_len;
1395 struct pci_dev *pdev = ha->pdev;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001396
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301397 status = pci_request_regions(pdev, DRIVER_NAME);
1398 if (status) {
1399 printk(KERN_WARNING
1400 "scsi(%ld) Failed to reserve PIO regions (%s) "
1401 "status=%d\n", ha->host_no, pci_name(pdev), status);
1402 goto iospace_error_exit;
1403 }
1404
1405 pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
1406 DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
1407 __func__, revision_id));
1408 ha->revision_id = revision_id;
1409
1410 /* remap phys address */
1411 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
1412 mem_len = pci_resource_len(pdev, 0);
1413 DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
1414 __func__, mem_base, mem_len));
1415
1416 /* mapping of pcibase pointer */
1417 ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
1418 if (!ha->nx_pcibase) {
1419 printk(KERN_ERR
1420 "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
1421 pci_release_regions(ha->pdev);
1422 goto iospace_error_exit;
1423 }
1424
1425 /* Mapping of IO base pointer, door bell read and write pointer */
1426
1427 /* mapping of IO base pointer */
1428 ha->qla4_8xxx_reg =
1429 (struct device_reg_82xx __iomem *)((uint8_t *)ha->nx_pcibase +
1430 0xbc000 + (ha->pdev->devfn << 11));
1431
1432 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
1433 db_len = pci_resource_len(pdev, 4);
1434
1435 /* mapping of doorbell write pointer */
1436 ha->nx_db_wr_ptr = (unsigned long)ioremap(db_base +
1437 (ha->pdev->devfn << 12), 4);
1438 if (!ha->nx_db_wr_ptr) {
1439 printk(KERN_ERR
1440 "cannot remap MMIO doorbell-write (%s), aborting\n",
1441 pci_name(pdev));
1442 goto iospace_error_exit;
1443 }
1444 /* mapping of doorbell read pointer */
1445 ha->nx_db_rd_ptr = (uint8_t *) ha->nx_pcibase + (512 * 1024) +
1446 (ha->pdev->devfn * 8);
1447 if (!ha->nx_db_rd_ptr)
1448 printk(KERN_ERR
1449 "cannot remap MMIO doorbell-read (%s), aborting\n",
1450 pci_name(pdev));
1451 return 0;
1452
1453iospace_error_exit:
1454 return -ENOMEM;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001455}
1456
1457/***
1458 * qla4xxx_iospace_config - maps registers
1459 * @ha: pointer to adapter structure
1460 *
1461 * This routines maps HBA's registers from the pci address space
1462 * into the kernel virtual address space for memory mapped i/o.
1463 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301464int qla4xxx_iospace_config(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001465{
1466 unsigned long pio, pio_len, pio_flags;
1467 unsigned long mmio, mmio_len, mmio_flags;
1468
1469 pio = pci_resource_start(ha->pdev, 0);
1470 pio_len = pci_resource_len(ha->pdev, 0);
1471 pio_flags = pci_resource_flags(ha->pdev, 0);
1472 if (pio_flags & IORESOURCE_IO) {
1473 if (pio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301474 ql4_printk(KERN_WARNING, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07001475 "Invalid PCI I/O region size\n");
1476 pio = 0;
1477 }
1478 } else {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301479 ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001480 pio = 0;
1481 }
1482
1483 /* Use MMIO operations for all accesses. */
1484 mmio = pci_resource_start(ha->pdev, 1);
1485 mmio_len = pci_resource_len(ha->pdev, 1);
1486 mmio_flags = pci_resource_flags(ha->pdev, 1);
1487
1488 if (!(mmio_flags & IORESOURCE_MEM)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301489 ql4_printk(KERN_ERR, ha,
1490 "region #0 not an MMIO resource, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001491
1492 goto iospace_error_exit;
1493 }
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301494
David Somayajuluafaf5a22006-09-19 10:28:00 -07001495 if (mmio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301496 ql4_printk(KERN_ERR, ha,
1497 "Invalid PCI mem region size, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001498 goto iospace_error_exit;
1499 }
1500
1501 if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301502 ql4_printk(KERN_WARNING, ha,
1503 "Failed to reserve PIO/MMIO regions\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001504
1505 goto iospace_error_exit;
1506 }
1507
1508 ha->pio_address = pio;
1509 ha->pio_length = pio_len;
1510 ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
1511 if (!ha->reg) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301512 ql4_printk(KERN_ERR, ha,
1513 "cannot remap MMIO, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001514
1515 goto iospace_error_exit;
1516 }
1517
1518 return 0;
1519
1520iospace_error_exit:
1521 return -ENOMEM;
1522}
1523
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301524static struct isp_operations qla4xxx_isp_ops = {
1525 .iospace_config = qla4xxx_iospace_config,
1526 .pci_config = qla4xxx_pci_config,
1527 .disable_intrs = qla4xxx_disable_intrs,
1528 .enable_intrs = qla4xxx_enable_intrs,
1529 .start_firmware = qla4xxx_start_firmware,
1530 .intr_handler = qla4xxx_intr_handler,
1531 .interrupt_service_routine = qla4xxx_interrupt_service_routine,
1532 .reset_chip = qla4xxx_soft_reset,
1533 .reset_firmware = qla4xxx_hw_reset,
1534 .queue_iocb = qla4xxx_queue_iocb,
1535 .complete_iocb = qla4xxx_complete_iocb,
1536 .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
1537 .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
1538 .get_sys_info = qla4xxx_get_sys_info,
1539};
1540
1541static struct isp_operations qla4_8xxx_isp_ops = {
1542 .iospace_config = qla4_8xxx_iospace_config,
1543 .pci_config = qla4_8xxx_pci_config,
1544 .disable_intrs = qla4_8xxx_disable_intrs,
1545 .enable_intrs = qla4_8xxx_enable_intrs,
1546 .start_firmware = qla4_8xxx_load_risc,
1547 .intr_handler = qla4_8xxx_intr_handler,
1548 .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
1549 .reset_chip = qla4_8xxx_isp_reset,
1550 .reset_firmware = qla4_8xxx_stop_firmware,
1551 .queue_iocb = qla4_8xxx_queue_iocb,
1552 .complete_iocb = qla4_8xxx_complete_iocb,
1553 .rd_shdw_req_q_out = qla4_8xxx_rd_shdw_req_q_out,
1554 .rd_shdw_rsp_q_in = qla4_8xxx_rd_shdw_rsp_q_in,
1555 .get_sys_info = qla4_8xxx_get_sys_info,
1556};
1557
1558uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
1559{
1560 return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
1561}
1562
1563uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
1564{
1565 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
1566}
1567
1568uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
1569{
1570 return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
1571}
1572
1573uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
1574{
1575 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
1576}
1577
David Somayajuluafaf5a22006-09-19 10:28:00 -07001578/**
1579 * qla4xxx_probe_adapter - callback function to probe HBA
1580 * @pdev: pointer to pci_dev structure
1581 * @pci_device_id: pointer to pci_device entry
1582 *
1583 * This routine will probe for Qlogic 4xxx iSCSI host adapters.
1584 * It returns zero if successful. It also initializes all data necessary for
1585 * the driver.
1586 **/
1587static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
1588 const struct pci_device_id *ent)
1589{
1590 int ret = -ENODEV, status;
1591 struct Scsi_Host *host;
1592 struct scsi_qla_host *ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001593 uint8_t init_retry_count = 0;
1594 char buf[34];
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301595 struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001596
1597 if (pci_enable_device(pdev))
1598 return -1;
1599
1600 host = scsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha));
1601 if (host == NULL) {
1602 printk(KERN_WARNING
1603 "qla4xxx: Couldn't allocate host from scsi layer!\n");
1604 goto probe_disable_device;
1605 }
1606
1607 /* Clear our data area */
1608 ha = (struct scsi_qla_host *) host->hostdata;
1609 memset(ha, 0, sizeof(*ha));
1610
1611 /* Save the information from PCI BIOS. */
1612 ha->pdev = pdev;
1613 ha->host = host;
1614 ha->host_no = host->host_no;
1615
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301616 /* Setup Runtime configurable options */
1617 if (is_qla8022(ha)) {
1618 ha->isp_ops = &qla4_8xxx_isp_ops;
1619 rwlock_init(&ha->hw_lock);
1620 ha->qdr_sn_window = -1;
1621 ha->ddr_mn_window = -1;
1622 ha->curr_window = 255;
1623 ha->func_num = PCI_FUNC(ha->pdev->devfn);
1624 nx_legacy_intr = &legacy_intr[ha->func_num];
1625 ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
1626 ha->nx_legacy_intr.tgt_status_reg =
1627 nx_legacy_intr->tgt_status_reg;
1628 ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
1629 ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
1630 } else {
1631 ha->isp_ops = &qla4xxx_isp_ops;
1632 }
1633
David Somayajuluafaf5a22006-09-19 10:28:00 -07001634 /* Configure PCI I/O space. */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301635 ret = ha->isp_ops->iospace_config(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001636 if (ret)
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301637 goto probe_failed_ioconfig;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001638
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301639 ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
David Somayajuluafaf5a22006-09-19 10:28:00 -07001640 pdev->device, pdev->irq, ha->reg);
1641
1642 qla4xxx_config_dma_addressing(ha);
1643
1644 /* Initialize lists and spinlocks. */
1645 INIT_LIST_HEAD(&ha->ddb_list);
1646 INIT_LIST_HEAD(&ha->free_srb_q);
1647
1648 mutex_init(&ha->mbox_sem);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301649 init_completion(&ha->mbx_intr_comp);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001650
1651 spin_lock_init(&ha->hardware_lock);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001652
1653 /* Allocate dma buffers */
1654 if (qla4xxx_mem_alloc(ha)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301655 ql4_printk(KERN_WARNING, ha,
1656 "[ERROR] Failed to allocate memory for adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001657
1658 ret = -ENOMEM;
1659 goto probe_failed;
1660 }
1661
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301662 if (is_qla8022(ha))
1663 (void) qla4_8xxx_get_flash_info(ha);
1664
David Somayajuluafaf5a22006-09-19 10:28:00 -07001665 /*
1666 * Initialize the Host adapter request/response queues and
1667 * firmware
1668 * NOTE: interrupts enabled upon successful completion
1669 */
1670 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301671 while ((!test_bit(AF_ONLINE, &ha->flags)) &&
1672 init_retry_count++ < MAX_INIT_RETRIES) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001673 DEBUG2(printk("scsi: %s: retrying adapter initialization "
1674 "(%d)\n", __func__, init_retry_count));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301675
1676 if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
1677 continue;
1678
David Somayajuluafaf5a22006-09-19 10:28:00 -07001679 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1680 }
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301681
1682 if (!test_bit(AF_ONLINE, &ha->flags)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301683 ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001684
1685 ret = -ENODEV;
1686 goto probe_failed;
1687 }
1688
1689 host->cmd_per_lun = 3;
1690 host->max_channel = 0;
1691 host->max_lun = MAX_LUNS - 1;
1692 host->max_id = MAX_TARGETS;
1693 host->max_cmd_len = IOCB_MAX_CDB_LEN;
1694 host->can_queue = MAX_SRBS ;
1695 host->transportt = qla4xxx_scsi_transport;
1696
1697 ret = scsi_init_shared_tag_map(host, MAX_SRBS);
1698 if (ret) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301699 ql4_printk(KERN_WARNING, ha,
1700 "scsi_init_shared_tag_map failed\n");
1701 goto probe_failed;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001702 }
1703
1704 /* Startup the kernel thread for this host adapter. */
1705 DEBUG2(printk("scsi: %s: Starting kernel thread for "
1706 "qla4xxx_dpc\n", __func__));
1707 sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
1708 ha->dpc_thread = create_singlethread_workqueue(buf);
1709 if (!ha->dpc_thread) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301710 ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001711 ret = -ENODEV;
1712 goto probe_failed;
1713 }
David Howellsc4028952006-11-22 14:57:56 +00001714 INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001715
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301716 /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
1717 * (which is called indirectly by qla4xxx_initialize_adapter),
1718 * so that irqs will be registered after crbinit but before
1719 * mbx_intr_enable.
1720 */
1721 if (!is_qla8022(ha)) {
1722 ret = qla4xxx_request_irqs(ha);
1723 if (ret) {
1724 ql4_printk(KERN_WARNING, ha, "Failed to reserve "
1725 "interrupt %d already in use.\n", pdev->irq);
1726 goto probe_failed;
1727 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001728 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001729
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301730 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001731
1732 /* Start timer thread. */
1733 qla4xxx_start_timer(ha, qla4xxx_timer, 1);
1734
1735 set_bit(AF_INIT_DONE, &ha->flags);
1736
1737 pci_set_drvdata(pdev, ha);
1738
1739 ret = scsi_add_host(host, &pdev->dev);
1740 if (ret)
1741 goto probe_failed;
1742
David Somayajuluafaf5a22006-09-19 10:28:00 -07001743 printk(KERN_INFO
1744 " QLogic iSCSI HBA Driver version: %s\n"
1745 " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
1746 qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
1747 ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
1748 ha->patch_number, ha->build_number);
Mike Christie921601b2008-01-31 13:36:49 -06001749 scsi_scan_host(host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001750 return 0;
1751
David Somayajuluafaf5a22006-09-19 10:28:00 -07001752probe_failed:
1753 qla4xxx_free_adapter(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301754
1755probe_failed_ioconfig:
David Somayajuluafaf5a22006-09-19 10:28:00 -07001756 scsi_host_put(ha->host);
1757
1758probe_disable_device:
1759 pci_disable_device(pdev);
1760
1761 return ret;
1762}
1763
1764/**
1765 * qla4xxx_remove_adapter - calback function to remove adapter.
1766 * @pci_dev: PCI device pointer
1767 **/
1768static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
1769{
1770 struct scsi_qla_host *ha;
1771
1772 ha = pci_get_drvdata(pdev);
1773
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301774 set_bit(AF_HBA_GOING_AWAY, &ha->flags);
David C Somayajulubee4fe82007-05-23 18:03:32 -07001775
David Somayajuluafaf5a22006-09-19 10:28:00 -07001776 /* remove devs from iscsi_sessions to scsi_devices */
1777 qla4xxx_free_ddb_list(ha);
1778
1779 scsi_remove_host(ha->host);
1780
1781 qla4xxx_free_adapter(ha);
1782
1783 scsi_host_put(ha->host);
1784
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301785 pci_disable_device(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001786 pci_set_drvdata(pdev, NULL);
1787}
1788
1789/**
1790 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
1791 * @ha: HA context
1792 *
1793 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
1794 * supported addressing method.
1795 */
Adrian Bunk47975472007-04-26 00:35:16 -07001796static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001797{
1798 int retval;
1799
1800 /* Update our PCI device dma_mask for full 64 bit mask */
Yang Hongyang6a355282009-04-06 19:01:13 -07001801 if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
1802 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001803 dev_dbg(&ha->pdev->dev,
1804 "Failed to set 64 bit PCI consistent mask; "
1805 "using 32 bit.\n");
1806 retval = pci_set_consistent_dma_mask(ha->pdev,
Yang Hongyang284901a2009-04-06 19:01:15 -07001807 DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001808 }
1809 } else
Yang Hongyang284901a2009-04-06 19:01:15 -07001810 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001811}
1812
1813static int qla4xxx_slave_alloc(struct scsi_device *sdev)
1814{
1815 struct iscsi_cls_session *sess = starget_to_session(sdev->sdev_target);
1816 struct ddb_entry *ddb = sess->dd_data;
1817
1818 sdev->hostdata = ddb;
1819 sdev->tagged_supported = 1;
Mike Christied510d962008-07-11 19:50:33 -05001820 scsi_activate_tcq(sdev, QL4_DEF_QDEPTH);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001821 return 0;
1822}
1823
1824static int qla4xxx_slave_configure(struct scsi_device *sdev)
1825{
1826 sdev->tagged_supported = 1;
1827 return 0;
1828}
1829
1830static void qla4xxx_slave_destroy(struct scsi_device *sdev)
1831{
1832 scsi_deactivate_tcq(sdev, 1);
1833}
1834
1835/**
1836 * qla4xxx_del_from_active_array - returns an active srb
1837 * @ha: Pointer to host adapter structure.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001838 * @index: index into the active_array
David Somayajuluafaf5a22006-09-19 10:28:00 -07001839 *
1840 * This routine removes and returns the srb at the specified index
1841 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301842struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
1843 uint32_t index)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001844{
1845 struct srb *srb = NULL;
Vikas Chaudhary53698872010-04-28 11:41:59 +05301846 struct scsi_cmnd *cmd = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001847
Vikas Chaudhary53698872010-04-28 11:41:59 +05301848 cmd = scsi_host_find_tag(ha->host, index);
1849 if (!cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001850 return srb;
1851
Vikas Chaudhary53698872010-04-28 11:41:59 +05301852 srb = (struct srb *)CMD_SP(cmd);
1853 if (!srb)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001854 return srb;
1855
1856 /* update counters */
1857 if (srb->flags & SRB_DMA_VALID) {
1858 ha->req_q_count += srb->iocb_cnt;
1859 ha->iocb_cnt -= srb->iocb_cnt;
1860 if (srb->cmd)
Vikas Chaudhary53698872010-04-28 11:41:59 +05301861 srb->cmd->host_scribble =
1862 (unsigned char *)(unsigned long) MAX_SRBS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001863 }
1864 return srb;
1865}
1866
1867/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07001868 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301869 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001870 * @cmd: Scsi Command to wait on.
1871 *
1872 * This routine waits for the command to be returned by the Firmware
1873 * for some max time.
1874 **/
1875static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
1876 struct scsi_cmnd *cmd)
1877{
1878 int done = 0;
1879 struct srb *rp;
1880 uint32_t max_wait_time = EH_WAIT_CMD_TOV;
1881
1882 do {
1883 /* Checking to see if its returned to OS */
Vikas Chaudhary53698872010-04-28 11:41:59 +05301884 rp = (struct srb *) CMD_SP(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001885 if (rp == NULL) {
1886 done++;
1887 break;
1888 }
1889
1890 msleep(2000);
1891 } while (max_wait_time--);
1892
1893 return done;
1894}
1895
1896/**
1897 * qla4xxx_wait_for_hba_online - waits for HBA to come online
1898 * @ha: Pointer to host adapter structure
1899 **/
1900static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
1901{
1902 unsigned long wait_online;
1903
1904 wait_online = jiffies + (30 * HZ);
1905 while (time_before(jiffies, wait_online)) {
1906
1907 if (adapter_up(ha))
1908 return QLA_SUCCESS;
1909 else if (ha->retry_reset_ha_cnt == 0)
1910 return QLA_ERROR;
1911
1912 msleep(2000);
1913 }
1914
1915 return QLA_ERROR;
1916}
1917
1918/**
Mike Christiece545032008-02-29 18:25:20 -06001919 * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001920 * @ha: pointer to HBA
David Somayajuluafaf5a22006-09-19 10:28:00 -07001921 * @t: target id
1922 * @l: lun id
1923 *
1924 * This function waits for all outstanding commands to a lun to complete. It
1925 * returns 0 if all pending commands are returned and 1 otherwise.
1926 **/
Mike Christiece545032008-02-29 18:25:20 -06001927static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
1928 struct scsi_target *stgt,
1929 struct scsi_device *sdev)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001930{
1931 int cnt;
1932 int status = 0;
1933 struct scsi_cmnd *cmd;
1934
1935 /*
Mike Christiece545032008-02-29 18:25:20 -06001936 * Waiting for all commands for the designated target or dev
1937 * in the active array
David Somayajuluafaf5a22006-09-19 10:28:00 -07001938 */
1939 for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
1940 cmd = scsi_host_find_tag(ha->host, cnt);
Mike Christiece545032008-02-29 18:25:20 -06001941 if (cmd && stgt == scsi_target(cmd->device) &&
1942 (!sdev || sdev == cmd->device)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001943 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
1944 status++;
1945 break;
1946 }
1947 }
1948 }
1949 return status;
1950}
1951
1952/**
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301953 * qla4xxx_eh_abort - callback for abort task.
1954 * @cmd: Pointer to Linux's SCSI command structure
1955 *
1956 * This routine is called by the Linux OS to abort the specified
1957 * command.
1958 **/
1959static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
1960{
1961 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
1962 unsigned int id = cmd->device->id;
1963 unsigned int lun = cmd->device->lun;
1964 unsigned long serial = cmd->serial_number;
1965 struct srb *srb = NULL;
1966 int ret = SUCCESS;
1967 int wait = 0;
1968
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301969 ql4_printk(KERN_INFO, ha,
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301970 "scsi%ld:%d:%d: Abort command issued cmd=%p, pid=%ld\n",
1971 ha->host_no, id, lun, cmd, serial);
1972
1973 srb = (struct srb *) CMD_SP(cmd);
1974
1975 if (!srb)
1976 return SUCCESS;
1977
1978 kref_get(&srb->srb_ref);
1979
1980 if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
1981 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
1982 ha->host_no, id, lun));
1983 ret = FAILED;
1984 } else {
1985 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
1986 ha->host_no, id, lun));
1987 wait = 1;
1988 }
1989
1990 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
1991
1992 /* Wait for command to complete */
1993 if (wait) {
1994 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
1995 DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
1996 ha->host_no, id, lun));
1997 ret = FAILED;
1998 }
1999 }
2000
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302001 ql4_printk(KERN_INFO, ha,
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302002 "scsi%ld:%d:%d: Abort command - %s\n",
2003 ha->host_no, id, lun, (ret == SUCCESS) ? "succeded" : "failed");
2004
2005 return ret;
2006}
2007
2008/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002009 * qla4xxx_eh_device_reset - callback for target reset.
2010 * @cmd: Pointer to Linux's SCSI command structure
2011 *
2012 * This routine is called by the Linux OS to reset all luns on the
2013 * specified target.
2014 **/
2015static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
2016{
2017 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2018 struct ddb_entry *ddb_entry = cmd->device->hostdata;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002019 int ret = FAILED, stat;
2020
Karen Higgins612f7342009-07-15 15:03:01 -05002021 if (!ddb_entry)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002022 return ret;
2023
Mike Christiec01be6d2010-07-22 16:59:49 +05302024 ret = iscsi_block_scsi_eh(cmd);
2025 if (ret)
2026 return ret;
2027 ret = FAILED;
2028
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302029 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002030 "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
2031 cmd->device->channel, cmd->device->id, cmd->device->lun);
2032
2033 DEBUG2(printk(KERN_INFO
2034 "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
2035 "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
Jens Axboe242f9dc2008-09-14 05:55:09 -07002036 cmd, jiffies, cmd->request->timeout / HZ,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002037 ha->dpc_flags, cmd->result, cmd->allowed));
2038
2039 /* FIXME: wait for hba to go online */
2040 stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
2041 if (stat != QLA_SUCCESS) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302042 ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002043 goto eh_dev_reset_done;
2044 }
2045
Mike Christiece545032008-02-29 18:25:20 -06002046 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2047 cmd->device)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302048 ql4_printk(KERN_INFO, ha,
Mike Christiece545032008-02-29 18:25:20 -06002049 "DEVICE RESET FAILED - waiting for "
2050 "commands.\n");
2051 goto eh_dev_reset_done;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002052 }
2053
David C Somayajulu9d562912008-03-19 11:23:03 -07002054 /* Send marker. */
2055 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2056 MM_LUN_RESET) != QLA_SUCCESS)
2057 goto eh_dev_reset_done;
2058
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302059 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002060 "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
2061 ha->host_no, cmd->device->channel, cmd->device->id,
2062 cmd->device->lun);
2063
2064 ret = SUCCESS;
2065
2066eh_dev_reset_done:
2067
2068 return ret;
2069}
2070
2071/**
Mike Christiece545032008-02-29 18:25:20 -06002072 * qla4xxx_eh_target_reset - callback for target reset.
2073 * @cmd: Pointer to Linux's SCSI command structure
2074 *
2075 * This routine is called by the Linux OS to reset the target.
2076 **/
2077static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
2078{
2079 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2080 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christiec01be6d2010-07-22 16:59:49 +05302081 int stat, ret;
Mike Christiece545032008-02-29 18:25:20 -06002082
2083 if (!ddb_entry)
2084 return FAILED;
2085
Mike Christiec01be6d2010-07-22 16:59:49 +05302086 ret = iscsi_block_scsi_eh(cmd);
2087 if (ret)
2088 return ret;
2089
Mike Christiece545032008-02-29 18:25:20 -06002090 starget_printk(KERN_INFO, scsi_target(cmd->device),
2091 "WARM TARGET RESET ISSUED.\n");
2092
2093 DEBUG2(printk(KERN_INFO
2094 "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
2095 "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
Jens Axboe242f9dc2008-09-14 05:55:09 -07002096 ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
Mike Christiece545032008-02-29 18:25:20 -06002097 ha->dpc_flags, cmd->result, cmd->allowed));
2098
2099 stat = qla4xxx_reset_target(ha, ddb_entry);
2100 if (stat != QLA_SUCCESS) {
2101 starget_printk(KERN_INFO, scsi_target(cmd->device),
2102 "WARM TARGET RESET FAILED.\n");
2103 return FAILED;
2104 }
2105
Mike Christiece545032008-02-29 18:25:20 -06002106 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2107 NULL)) {
2108 starget_printk(KERN_INFO, scsi_target(cmd->device),
2109 "WARM TARGET DEVICE RESET FAILED - "
2110 "waiting for commands.\n");
2111 return FAILED;
2112 }
2113
David C Somayajulu9d562912008-03-19 11:23:03 -07002114 /* Send marker. */
2115 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2116 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
2117 starget_printk(KERN_INFO, scsi_target(cmd->device),
2118 "WARM TARGET DEVICE RESET FAILED - "
2119 "marker iocb failed.\n");
2120 return FAILED;
2121 }
2122
Mike Christiece545032008-02-29 18:25:20 -06002123 starget_printk(KERN_INFO, scsi_target(cmd->device),
2124 "WARM TARGET RESET SUCCEEDED.\n");
2125 return SUCCESS;
2126}
2127
2128/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002129 * qla4xxx_eh_host_reset - kernel callback
2130 * @cmd: Pointer to Linux's SCSI command structure
2131 *
2132 * This routine is invoked by the Linux kernel to perform fatal error
2133 * recovery on the specified adapter.
2134 **/
2135static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
2136{
2137 int return_status = FAILED;
2138 struct scsi_qla_host *ha;
2139
2140 ha = (struct scsi_qla_host *) cmd->device->host->hostdata;
2141
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302142 if (ql4xdontresethba) {
2143 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
2144 ha->host_no, __func__));
2145 return FAILED;
2146 }
2147
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302148 ql4_printk(KERN_INFO, ha,
Karen Higginsdca05c42009-07-15 15:03:00 -05002149 "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002150 cmd->device->channel, cmd->device->id, cmd->device->lun);
2151
2152 if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
2153 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
2154 "DEAD.\n", ha->host_no, cmd->device->channel,
2155 __func__));
2156
2157 return FAILED;
2158 }
2159
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302160 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
2161 if (is_qla8022(ha))
2162 set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2163 else
2164 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2165 }
Mike Christie50a29ae2008-03-04 13:26:53 -06002166
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302167 if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002168 return_status = SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002169
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302170 ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
David Somayajuluafaf5a22006-09-19 10:28:00 -07002171 return_status == FAILED ? "FAILED" : "SUCCEDED");
2172
2173 return return_status;
2174}
2175
David Somayajuluafaf5a22006-09-19 10:28:00 -07002176static struct pci_device_id qla4xxx_pci_tbl[] = {
2177 {
2178 .vendor = PCI_VENDOR_ID_QLOGIC,
2179 .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
2180 .subvendor = PCI_ANY_ID,
2181 .subdevice = PCI_ANY_ID,
2182 },
2183 {
2184 .vendor = PCI_VENDOR_ID_QLOGIC,
2185 .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
2186 .subvendor = PCI_ANY_ID,
2187 .subdevice = PCI_ANY_ID,
2188 },
David C Somayajulud9150582006-11-15 17:38:40 -08002189 {
2190 .vendor = PCI_VENDOR_ID_QLOGIC,
2191 .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
2192 .subvendor = PCI_ANY_ID,
2193 .subdevice = PCI_ANY_ID,
2194 },
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302195 {
2196 .vendor = PCI_VENDOR_ID_QLOGIC,
2197 .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
2198 .subvendor = PCI_ANY_ID,
2199 .subdevice = PCI_ANY_ID,
2200 },
David Somayajuluafaf5a22006-09-19 10:28:00 -07002201 {0, 0},
2202};
2203MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
2204
Adrian Bunk47975472007-04-26 00:35:16 -07002205static struct pci_driver qla4xxx_pci_driver = {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002206 .name = DRIVER_NAME,
2207 .id_table = qla4xxx_pci_tbl,
2208 .probe = qla4xxx_probe_adapter,
2209 .remove = qla4xxx_remove_adapter,
2210};
2211
2212static int __init qla4xxx_module_init(void)
2213{
2214 int ret;
2215
2216 /* Allocate cache for SRBs. */
2217 srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09002218 SLAB_HWCACHE_ALIGN, NULL);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002219 if (srb_cachep == NULL) {
2220 printk(KERN_ERR
2221 "%s: Unable to allocate SRB cache..."
2222 "Failing load!\n", DRIVER_NAME);
2223 ret = -ENOMEM;
2224 goto no_srp_cache;
2225 }
2226
2227 /* Derive version string. */
2228 strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
Andrew Vasquez11010fe2006-10-06 09:54:59 -07002229 if (ql4xextended_error_logging)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002230 strcat(qla4xxx_version_str, "-debug");
2231
2232 qla4xxx_scsi_transport =
2233 iscsi_register_transport(&qla4xxx_iscsi_transport);
2234 if (!qla4xxx_scsi_transport){
2235 ret = -ENODEV;
2236 goto release_srb_cache;
2237 }
2238
David Somayajuluafaf5a22006-09-19 10:28:00 -07002239 ret = pci_register_driver(&qla4xxx_pci_driver);
2240 if (ret)
2241 goto unregister_transport;
2242
2243 printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
2244 return 0;
Doug Maxey5ae16db2006-10-05 23:50:07 -05002245
David Somayajuluafaf5a22006-09-19 10:28:00 -07002246unregister_transport:
2247 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
2248release_srb_cache:
2249 kmem_cache_destroy(srb_cachep);
2250no_srp_cache:
2251 return ret;
2252}
2253
2254static void __exit qla4xxx_module_exit(void)
2255{
David Somayajuluafaf5a22006-09-19 10:28:00 -07002256 pci_unregister_driver(&qla4xxx_pci_driver);
2257 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
2258 kmem_cache_destroy(srb_cachep);
2259}
2260
2261module_init(qla4xxx_module_init);
2262module_exit(qla4xxx_module_exit);
2263
2264MODULE_AUTHOR("QLogic Corporation");
2265MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
2266MODULE_LICENSE("GPL");
2267MODULE_VERSION(QLA4XXX_DRIVER_VERSION);