blob: e575d765ba26e7ebbc6c264e5817af9ff6f7b5bf [file] [log] [blame]
David Somayajuluafaf5a22006-09-19 10:28:00 -07001/*
2 * QLogic iSCSI HBA Driver
3 * Copyright (c) 2003-2006 QLogic Corporation
4 *
5 * See LICENSE.qla4xxx for copyright and licensing details.
6 */
7#include <linux/moduleparam.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
David Somayajuluafaf5a22006-09-19 10:28:00 -07009
10#include <scsi/scsi_tcq.h>
11#include <scsi/scsicam.h>
12
13#include "ql4_def.h"
David C Somayajulubee4fe82007-05-23 18:03:32 -070014#include "ql4_version.h"
15#include "ql4_glbl.h"
16#include "ql4_dbg.h"
17#include "ql4_inline.h"
David Somayajuluafaf5a22006-09-19 10:28:00 -070018
19/*
20 * Driver version
21 */
Adrian Bunk47975472007-04-26 00:35:16 -070022static char qla4xxx_version_str[40];
David Somayajuluafaf5a22006-09-19 10:28:00 -070023
24/*
25 * SRB allocation cache
26 */
Christoph Lametere18b8902006-12-06 20:33:20 -080027static struct kmem_cache *srb_cachep;
David Somayajuluafaf5a22006-09-19 10:28:00 -070028
29/*
30 * Module parameter information and variables
31 */
32int ql4xdiscoverywait = 60;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053033module_param(ql4xdiscoverywait, int, S_IRUGO | S_IWUSR);
David Somayajuluafaf5a22006-09-19 10:28:00 -070034MODULE_PARM_DESC(ql4xdiscoverywait, "Discovery wait time");
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053035
David Somayajuluafaf5a22006-09-19 10:28:00 -070036int ql4xdontresethba = 0;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053037module_param(ql4xdontresethba, int, S_IRUGO | S_IWUSR);
David Somayajuluafaf5a22006-09-19 10:28:00 -070038MODULE_PARM_DESC(ql4xdontresethba,
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053039 "Don't reset the HBA for driver recovery \n"
40 " 0 - It will reset HBA (Default)\n"
41 " 1 - It will NOT reset HBA");
David Somayajuluafaf5a22006-09-19 10:28:00 -070042
Andrew Vasquez11010fe2006-10-06 09:54:59 -070043int ql4xextended_error_logging = 0; /* 0 = off, 1 = log errors */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053044module_param(ql4xextended_error_logging, int, S_IRUGO | S_IWUSR);
Andrew Vasquez11010fe2006-10-06 09:54:59 -070045MODULE_PARM_DESC(ql4xextended_error_logging,
David Somayajuluafaf5a22006-09-19 10:28:00 -070046 "Option to enable extended error logging, "
47 "Default is 0 - no logging, 1 - debug logging");
48
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053049int ql4xenablemsix = 1;
50module_param(ql4xenablemsix, int, S_IRUGO|S_IWUSR);
51MODULE_PARM_DESC(ql4xenablemsix,
52 "Set to enable MSI or MSI-X interrupt mechanism.\n"
53 " 0 = enable INTx interrupt mechanism.\n"
54 " 1 = enable MSI-X interrupt mechanism (Default).\n"
55 " 2 = enable MSI interrupt mechanism.");
David C Somayajulu477ffb92007-01-22 12:26:11 -080056
Mike Christied510d962008-07-11 19:50:33 -050057#define QL4_DEF_QDEPTH 32
58
David Somayajuluafaf5a22006-09-19 10:28:00 -070059/*
60 * SCSI host template entry points
61 */
Adrian Bunk47975472007-04-26 00:35:16 -070062static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -070063
64/*
65 * iSCSI template entry points
66 */
Mike Christie2174a042007-05-30 12:57:10 -050067static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
68 enum iscsi_tgt_dscvr type, uint32_t enable,
69 struct sockaddr *dst_addr);
David Somayajuluafaf5a22006-09-19 10:28:00 -070070static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
71 enum iscsi_param param, char *buf);
72static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
73 enum iscsi_param param, char *buf);
Mike Christieaa1e93a2007-05-30 12:57:09 -050074static int qla4xxx_host_get_param(struct Scsi_Host *shost,
75 enum iscsi_host_param param, char *buf);
David Somayajuluafaf5a22006-09-19 10:28:00 -070076static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session);
Mike Christie5c656af2009-07-15 15:02:59 -050077static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
David Somayajuluafaf5a22006-09-19 10:28:00 -070078
79/*
80 * SCSI host template entry points
81 */
82static int qla4xxx_queuecommand(struct scsi_cmnd *cmd,
83 void (*done) (struct scsi_cmnd *));
Vikas Chaudhary09a0f712010-04-28 11:42:24 +053084static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -070085static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
Mike Christiece545032008-02-29 18:25:20 -060086static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -070087static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
88static int qla4xxx_slave_alloc(struct scsi_device *device);
89static int qla4xxx_slave_configure(struct scsi_device *device);
90static void qla4xxx_slave_destroy(struct scsi_device *sdev);
Mike Christie024f8012008-03-04 13:26:54 -060091static void qla4xxx_scan_start(struct Scsi_Host *shost);
David Somayajuluafaf5a22006-09-19 10:28:00 -070092
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053093static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
94 QLA82XX_LEGACY_INTR_CONFIG;
95
David Somayajuluafaf5a22006-09-19 10:28:00 -070096static struct scsi_host_template qla4xxx_driver_template = {
97 .module = THIS_MODULE,
98 .name = DRIVER_NAME,
99 .proc_name = DRIVER_NAME,
100 .queuecommand = qla4xxx_queuecommand,
101
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530102 .eh_abort_handler = qla4xxx_eh_abort,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700103 .eh_device_reset_handler = qla4xxx_eh_device_reset,
Mike Christiece545032008-02-29 18:25:20 -0600104 .eh_target_reset_handler = qla4xxx_eh_target_reset,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700105 .eh_host_reset_handler = qla4xxx_eh_host_reset,
Mike Christie5c656af2009-07-15 15:02:59 -0500106 .eh_timed_out = qla4xxx_eh_cmd_timed_out,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700107
108 .slave_configure = qla4xxx_slave_configure,
109 .slave_alloc = qla4xxx_slave_alloc,
110 .slave_destroy = qla4xxx_slave_destroy,
111
Mike Christie921601b2008-01-31 13:36:49 -0600112 .scan_finished = iscsi_scan_finished,
Mike Christie024f8012008-03-04 13:26:54 -0600113 .scan_start = qla4xxx_scan_start,
Mike Christie921601b2008-01-31 13:36:49 -0600114
David Somayajuluafaf5a22006-09-19 10:28:00 -0700115 .this_id = -1,
116 .cmd_per_lun = 3,
117 .use_clustering = ENABLE_CLUSTERING,
118 .sg_tablesize = SG_ALL,
119
120 .max_sectors = 0xFFFF,
121};
122
123static struct iscsi_transport qla4xxx_iscsi_transport = {
124 .owner = THIS_MODULE,
125 .name = DRIVER_NAME,
Mike Christied8196ed2007-05-30 12:57:25 -0500126 .caps = CAP_FW_DB | CAP_SENDTARGETS_OFFLOAD |
127 CAP_DATA_PATH_OFFLOAD,
Mike Christieaa1e93a2007-05-30 12:57:09 -0500128 .param_mask = ISCSI_CONN_PORT | ISCSI_CONN_ADDRESS |
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 Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-07-28 15:53:44 +05301040static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001041{
Vikas Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-07-28 15:53:44 +05301051 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001052
Vikas Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-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))
Vikas Chaudhary0753b482010-07-30 14:27:19 +05301211 goto do_dpc_exit;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001212
Vikas Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-07-28 15:53:44 +05301268 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001269 }
1270 }
1271
Vikas Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-07-28 15:53:44 +05301312 " marked ONLINE\n",
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301313 ha->host_no, __func__,
Vikas Chaudharyf4f5df22010-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 Chaudhary0753b482010-07-30 14:27:19 +05301350
1351do_dpc_exit:
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301352 clear_bit(AF_DPC_SCHEDULED, &ha->flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001353}
1354
1355/**
1356 * qla4xxx_free_adapter - release the adapter
1357 * @ha: pointer to adapter structure
1358 **/
1359static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
1360{
1361
1362 if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
1363 /* Turn-off interrupts on the card. */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301364 ha->isp_ops->disable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001365 }
1366
David Somayajuluafaf5a22006-09-19 10:28:00 -07001367 /* Remove timer thread, if present */
1368 if (ha->timer_active)
1369 qla4xxx_stop_timer(ha);
1370
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301371 /* Kill the kernel thread for this host */
1372 if (ha->dpc_thread)
1373 destroy_workqueue(ha->dpc_thread);
1374
1375 /* Put firmware in known state */
1376 ha->isp_ops->reset_firmware(ha);
1377
1378 if (is_qla8022(ha)) {
1379 qla4_8xxx_idc_lock(ha);
1380 qla4_8xxx_clear_drv_active(ha);
1381 qla4_8xxx_idc_unlock(ha);
1382 }
1383
David Somayajuluafaf5a22006-09-19 10:28:00 -07001384 /* Detach interrupts */
1385 if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301386 qla4xxx_free_irqs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001387
David C Somayajulubee4fe82007-05-23 18:03:32 -07001388 /* free extra memory */
1389 qla4xxx_mem_free(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301390}
David C Somayajulubee4fe82007-05-23 18:03:32 -07001391
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301392int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
1393{
1394 int status = 0;
1395 uint8_t revision_id;
1396 unsigned long mem_base, mem_len, db_base, db_len;
1397 struct pci_dev *pdev = ha->pdev;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001398
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301399 status = pci_request_regions(pdev, DRIVER_NAME);
1400 if (status) {
1401 printk(KERN_WARNING
1402 "scsi(%ld) Failed to reserve PIO regions (%s) "
1403 "status=%d\n", ha->host_no, pci_name(pdev), status);
1404 goto iospace_error_exit;
1405 }
1406
1407 pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
1408 DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
1409 __func__, revision_id));
1410 ha->revision_id = revision_id;
1411
1412 /* remap phys address */
1413 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
1414 mem_len = pci_resource_len(pdev, 0);
1415 DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
1416 __func__, mem_base, mem_len));
1417
1418 /* mapping of pcibase pointer */
1419 ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
1420 if (!ha->nx_pcibase) {
1421 printk(KERN_ERR
1422 "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
1423 pci_release_regions(ha->pdev);
1424 goto iospace_error_exit;
1425 }
1426
1427 /* Mapping of IO base pointer, door bell read and write pointer */
1428
1429 /* mapping of IO base pointer */
1430 ha->qla4_8xxx_reg =
1431 (struct device_reg_82xx __iomem *)((uint8_t *)ha->nx_pcibase +
1432 0xbc000 + (ha->pdev->devfn << 11));
1433
1434 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
1435 db_len = pci_resource_len(pdev, 4);
1436
1437 /* mapping of doorbell write pointer */
1438 ha->nx_db_wr_ptr = (unsigned long)ioremap(db_base +
1439 (ha->pdev->devfn << 12), 4);
1440 if (!ha->nx_db_wr_ptr) {
1441 printk(KERN_ERR
1442 "cannot remap MMIO doorbell-write (%s), aborting\n",
1443 pci_name(pdev));
1444 goto iospace_error_exit;
1445 }
1446 /* mapping of doorbell read pointer */
1447 ha->nx_db_rd_ptr = (uint8_t *) ha->nx_pcibase + (512 * 1024) +
1448 (ha->pdev->devfn * 8);
1449 if (!ha->nx_db_rd_ptr)
1450 printk(KERN_ERR
1451 "cannot remap MMIO doorbell-read (%s), aborting\n",
1452 pci_name(pdev));
1453 return 0;
1454
1455iospace_error_exit:
1456 return -ENOMEM;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001457}
1458
1459/***
1460 * qla4xxx_iospace_config - maps registers
1461 * @ha: pointer to adapter structure
1462 *
1463 * This routines maps HBA's registers from the pci address space
1464 * into the kernel virtual address space for memory mapped i/o.
1465 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301466int qla4xxx_iospace_config(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001467{
1468 unsigned long pio, pio_len, pio_flags;
1469 unsigned long mmio, mmio_len, mmio_flags;
1470
1471 pio = pci_resource_start(ha->pdev, 0);
1472 pio_len = pci_resource_len(ha->pdev, 0);
1473 pio_flags = pci_resource_flags(ha->pdev, 0);
1474 if (pio_flags & IORESOURCE_IO) {
1475 if (pio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301476 ql4_printk(KERN_WARNING, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07001477 "Invalid PCI I/O region size\n");
1478 pio = 0;
1479 }
1480 } else {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301481 ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001482 pio = 0;
1483 }
1484
1485 /* Use MMIO operations for all accesses. */
1486 mmio = pci_resource_start(ha->pdev, 1);
1487 mmio_len = pci_resource_len(ha->pdev, 1);
1488 mmio_flags = pci_resource_flags(ha->pdev, 1);
1489
1490 if (!(mmio_flags & IORESOURCE_MEM)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301491 ql4_printk(KERN_ERR, ha,
1492 "region #0 not an MMIO resource, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001493
1494 goto iospace_error_exit;
1495 }
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301496
David Somayajuluafaf5a22006-09-19 10:28:00 -07001497 if (mmio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301498 ql4_printk(KERN_ERR, ha,
1499 "Invalid PCI mem region size, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001500 goto iospace_error_exit;
1501 }
1502
1503 if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301504 ql4_printk(KERN_WARNING, ha,
1505 "Failed to reserve PIO/MMIO regions\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001506
1507 goto iospace_error_exit;
1508 }
1509
1510 ha->pio_address = pio;
1511 ha->pio_length = pio_len;
1512 ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
1513 if (!ha->reg) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301514 ql4_printk(KERN_ERR, ha,
1515 "cannot remap MMIO, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001516
1517 goto iospace_error_exit;
1518 }
1519
1520 return 0;
1521
1522iospace_error_exit:
1523 return -ENOMEM;
1524}
1525
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301526static struct isp_operations qla4xxx_isp_ops = {
1527 .iospace_config = qla4xxx_iospace_config,
1528 .pci_config = qla4xxx_pci_config,
1529 .disable_intrs = qla4xxx_disable_intrs,
1530 .enable_intrs = qla4xxx_enable_intrs,
1531 .start_firmware = qla4xxx_start_firmware,
1532 .intr_handler = qla4xxx_intr_handler,
1533 .interrupt_service_routine = qla4xxx_interrupt_service_routine,
1534 .reset_chip = qla4xxx_soft_reset,
1535 .reset_firmware = qla4xxx_hw_reset,
1536 .queue_iocb = qla4xxx_queue_iocb,
1537 .complete_iocb = qla4xxx_complete_iocb,
1538 .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
1539 .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
1540 .get_sys_info = qla4xxx_get_sys_info,
1541};
1542
1543static struct isp_operations qla4_8xxx_isp_ops = {
1544 .iospace_config = qla4_8xxx_iospace_config,
1545 .pci_config = qla4_8xxx_pci_config,
1546 .disable_intrs = qla4_8xxx_disable_intrs,
1547 .enable_intrs = qla4_8xxx_enable_intrs,
1548 .start_firmware = qla4_8xxx_load_risc,
1549 .intr_handler = qla4_8xxx_intr_handler,
1550 .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
1551 .reset_chip = qla4_8xxx_isp_reset,
1552 .reset_firmware = qla4_8xxx_stop_firmware,
1553 .queue_iocb = qla4_8xxx_queue_iocb,
1554 .complete_iocb = qla4_8xxx_complete_iocb,
1555 .rd_shdw_req_q_out = qla4_8xxx_rd_shdw_req_q_out,
1556 .rd_shdw_rsp_q_in = qla4_8xxx_rd_shdw_rsp_q_in,
1557 .get_sys_info = qla4_8xxx_get_sys_info,
1558};
1559
1560uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
1561{
1562 return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
1563}
1564
1565uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
1566{
1567 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
1568}
1569
1570uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
1571{
1572 return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
1573}
1574
1575uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
1576{
1577 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
1578}
1579
David Somayajuluafaf5a22006-09-19 10:28:00 -07001580/**
1581 * qla4xxx_probe_adapter - callback function to probe HBA
1582 * @pdev: pointer to pci_dev structure
1583 * @pci_device_id: pointer to pci_device entry
1584 *
1585 * This routine will probe for Qlogic 4xxx iSCSI host adapters.
1586 * It returns zero if successful. It also initializes all data necessary for
1587 * the driver.
1588 **/
1589static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
1590 const struct pci_device_id *ent)
1591{
1592 int ret = -ENODEV, status;
1593 struct Scsi_Host *host;
1594 struct scsi_qla_host *ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001595 uint8_t init_retry_count = 0;
1596 char buf[34];
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301597 struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001598
1599 if (pci_enable_device(pdev))
1600 return -1;
1601
1602 host = scsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha));
1603 if (host == NULL) {
1604 printk(KERN_WARNING
1605 "qla4xxx: Couldn't allocate host from scsi layer!\n");
1606 goto probe_disable_device;
1607 }
1608
1609 /* Clear our data area */
1610 ha = (struct scsi_qla_host *) host->hostdata;
1611 memset(ha, 0, sizeof(*ha));
1612
1613 /* Save the information from PCI BIOS. */
1614 ha->pdev = pdev;
1615 ha->host = host;
1616 ha->host_no = host->host_no;
1617
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301618 /* Setup Runtime configurable options */
1619 if (is_qla8022(ha)) {
1620 ha->isp_ops = &qla4_8xxx_isp_ops;
1621 rwlock_init(&ha->hw_lock);
1622 ha->qdr_sn_window = -1;
1623 ha->ddr_mn_window = -1;
1624 ha->curr_window = 255;
1625 ha->func_num = PCI_FUNC(ha->pdev->devfn);
1626 nx_legacy_intr = &legacy_intr[ha->func_num];
1627 ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
1628 ha->nx_legacy_intr.tgt_status_reg =
1629 nx_legacy_intr->tgt_status_reg;
1630 ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
1631 ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
1632 } else {
1633 ha->isp_ops = &qla4xxx_isp_ops;
1634 }
1635
David Somayajuluafaf5a22006-09-19 10:28:00 -07001636 /* Configure PCI I/O space. */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301637 ret = ha->isp_ops->iospace_config(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001638 if (ret)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301639 goto probe_failed_ioconfig;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001640
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301641 ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
David Somayajuluafaf5a22006-09-19 10:28:00 -07001642 pdev->device, pdev->irq, ha->reg);
1643
1644 qla4xxx_config_dma_addressing(ha);
1645
1646 /* Initialize lists and spinlocks. */
1647 INIT_LIST_HEAD(&ha->ddb_list);
1648 INIT_LIST_HEAD(&ha->free_srb_q);
1649
1650 mutex_init(&ha->mbox_sem);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301651 init_completion(&ha->mbx_intr_comp);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001652
1653 spin_lock_init(&ha->hardware_lock);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001654
1655 /* Allocate dma buffers */
1656 if (qla4xxx_mem_alloc(ha)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301657 ql4_printk(KERN_WARNING, ha,
1658 "[ERROR] Failed to allocate memory for adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001659
1660 ret = -ENOMEM;
1661 goto probe_failed;
1662 }
1663
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301664 if (is_qla8022(ha))
1665 (void) qla4_8xxx_get_flash_info(ha);
1666
David Somayajuluafaf5a22006-09-19 10:28:00 -07001667 /*
1668 * Initialize the Host adapter request/response queues and
1669 * firmware
1670 * NOTE: interrupts enabled upon successful completion
1671 */
1672 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301673 while ((!test_bit(AF_ONLINE, &ha->flags)) &&
1674 init_retry_count++ < MAX_INIT_RETRIES) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001675 DEBUG2(printk("scsi: %s: retrying adapter initialization "
1676 "(%d)\n", __func__, init_retry_count));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301677
1678 if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
1679 continue;
1680
David Somayajuluafaf5a22006-09-19 10:28:00 -07001681 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1682 }
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301683
1684 if (!test_bit(AF_ONLINE, &ha->flags)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301685 ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001686
1687 ret = -ENODEV;
1688 goto probe_failed;
1689 }
1690
1691 host->cmd_per_lun = 3;
1692 host->max_channel = 0;
1693 host->max_lun = MAX_LUNS - 1;
1694 host->max_id = MAX_TARGETS;
1695 host->max_cmd_len = IOCB_MAX_CDB_LEN;
1696 host->can_queue = MAX_SRBS ;
1697 host->transportt = qla4xxx_scsi_transport;
1698
1699 ret = scsi_init_shared_tag_map(host, MAX_SRBS);
1700 if (ret) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301701 ql4_printk(KERN_WARNING, ha,
1702 "scsi_init_shared_tag_map failed\n");
1703 goto probe_failed;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001704 }
1705
1706 /* Startup the kernel thread for this host adapter. */
1707 DEBUG2(printk("scsi: %s: Starting kernel thread for "
1708 "qla4xxx_dpc\n", __func__));
1709 sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
1710 ha->dpc_thread = create_singlethread_workqueue(buf);
1711 if (!ha->dpc_thread) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301712 ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001713 ret = -ENODEV;
1714 goto probe_failed;
1715 }
David Howellsc4028952006-11-22 14:57:56 +00001716 INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001717
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301718 /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
1719 * (which is called indirectly by qla4xxx_initialize_adapter),
1720 * so that irqs will be registered after crbinit but before
1721 * mbx_intr_enable.
1722 */
1723 if (!is_qla8022(ha)) {
1724 ret = qla4xxx_request_irqs(ha);
1725 if (ret) {
1726 ql4_printk(KERN_WARNING, ha, "Failed to reserve "
1727 "interrupt %d already in use.\n", pdev->irq);
1728 goto probe_failed;
1729 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001730 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001731
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301732 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001733
1734 /* Start timer thread. */
1735 qla4xxx_start_timer(ha, qla4xxx_timer, 1);
1736
1737 set_bit(AF_INIT_DONE, &ha->flags);
1738
1739 pci_set_drvdata(pdev, ha);
1740
1741 ret = scsi_add_host(host, &pdev->dev);
1742 if (ret)
1743 goto probe_failed;
1744
David Somayajuluafaf5a22006-09-19 10:28:00 -07001745 printk(KERN_INFO
1746 " QLogic iSCSI HBA Driver version: %s\n"
1747 " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
1748 qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
1749 ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
1750 ha->patch_number, ha->build_number);
Mike Christie921601b2008-01-31 13:36:49 -06001751 scsi_scan_host(host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001752 return 0;
1753
David Somayajuluafaf5a22006-09-19 10:28:00 -07001754probe_failed:
1755 qla4xxx_free_adapter(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301756
1757probe_failed_ioconfig:
David Somayajuluafaf5a22006-09-19 10:28:00 -07001758 scsi_host_put(ha->host);
1759
1760probe_disable_device:
1761 pci_disable_device(pdev);
1762
1763 return ret;
1764}
1765
1766/**
1767 * qla4xxx_remove_adapter - calback function to remove adapter.
1768 * @pci_dev: PCI device pointer
1769 **/
1770static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
1771{
1772 struct scsi_qla_host *ha;
1773
1774 ha = pci_get_drvdata(pdev);
1775
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301776 set_bit(AF_HBA_GOING_AWAY, &ha->flags);
David C Somayajulubee4fe82007-05-23 18:03:32 -07001777
David Somayajuluafaf5a22006-09-19 10:28:00 -07001778 /* remove devs from iscsi_sessions to scsi_devices */
1779 qla4xxx_free_ddb_list(ha);
1780
1781 scsi_remove_host(ha->host);
1782
1783 qla4xxx_free_adapter(ha);
1784
1785 scsi_host_put(ha->host);
1786
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301787 pci_disable_device(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001788 pci_set_drvdata(pdev, NULL);
1789}
1790
1791/**
1792 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
1793 * @ha: HA context
1794 *
1795 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
1796 * supported addressing method.
1797 */
Adrian Bunk47975472007-04-26 00:35:16 -07001798static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001799{
1800 int retval;
1801
1802 /* Update our PCI device dma_mask for full 64 bit mask */
Yang Hongyang6a355282009-04-06 19:01:13 -07001803 if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
1804 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001805 dev_dbg(&ha->pdev->dev,
1806 "Failed to set 64 bit PCI consistent mask; "
1807 "using 32 bit.\n");
1808 retval = pci_set_consistent_dma_mask(ha->pdev,
Yang Hongyang284901a2009-04-06 19:01:15 -07001809 DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001810 }
1811 } else
Yang Hongyang284901a2009-04-06 19:01:15 -07001812 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001813}
1814
1815static int qla4xxx_slave_alloc(struct scsi_device *sdev)
1816{
1817 struct iscsi_cls_session *sess = starget_to_session(sdev->sdev_target);
1818 struct ddb_entry *ddb = sess->dd_data;
1819
1820 sdev->hostdata = ddb;
1821 sdev->tagged_supported = 1;
Mike Christied510d962008-07-11 19:50:33 -05001822 scsi_activate_tcq(sdev, QL4_DEF_QDEPTH);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001823 return 0;
1824}
1825
1826static int qla4xxx_slave_configure(struct scsi_device *sdev)
1827{
1828 sdev->tagged_supported = 1;
1829 return 0;
1830}
1831
1832static void qla4xxx_slave_destroy(struct scsi_device *sdev)
1833{
1834 scsi_deactivate_tcq(sdev, 1);
1835}
1836
1837/**
1838 * qla4xxx_del_from_active_array - returns an active srb
1839 * @ha: Pointer to host adapter structure.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001840 * @index: index into the active_array
David Somayajuluafaf5a22006-09-19 10:28:00 -07001841 *
1842 * This routine removes and returns the srb at the specified index
1843 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301844struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
1845 uint32_t index)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001846{
1847 struct srb *srb = NULL;
Vikas Chaudhary53698872010-04-28 11:41:59 +05301848 struct scsi_cmnd *cmd = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001849
Vikas Chaudhary53698872010-04-28 11:41:59 +05301850 cmd = scsi_host_find_tag(ha->host, index);
1851 if (!cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001852 return srb;
1853
Vikas Chaudhary53698872010-04-28 11:41:59 +05301854 srb = (struct srb *)CMD_SP(cmd);
1855 if (!srb)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001856 return srb;
1857
1858 /* update counters */
1859 if (srb->flags & SRB_DMA_VALID) {
1860 ha->req_q_count += srb->iocb_cnt;
1861 ha->iocb_cnt -= srb->iocb_cnt;
1862 if (srb->cmd)
Vikas Chaudhary53698872010-04-28 11:41:59 +05301863 srb->cmd->host_scribble =
1864 (unsigned char *)(unsigned long) MAX_SRBS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001865 }
1866 return srb;
1867}
1868
1869/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07001870 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301871 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001872 * @cmd: Scsi Command to wait on.
1873 *
1874 * This routine waits for the command to be returned by the Firmware
1875 * for some max time.
1876 **/
1877static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
1878 struct scsi_cmnd *cmd)
1879{
1880 int done = 0;
1881 struct srb *rp;
1882 uint32_t max_wait_time = EH_WAIT_CMD_TOV;
1883
1884 do {
1885 /* Checking to see if its returned to OS */
Vikas Chaudhary53698872010-04-28 11:41:59 +05301886 rp = (struct srb *) CMD_SP(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001887 if (rp == NULL) {
1888 done++;
1889 break;
1890 }
1891
1892 msleep(2000);
1893 } while (max_wait_time--);
1894
1895 return done;
1896}
1897
1898/**
1899 * qla4xxx_wait_for_hba_online - waits for HBA to come online
1900 * @ha: Pointer to host adapter structure
1901 **/
1902static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
1903{
1904 unsigned long wait_online;
1905
1906 wait_online = jiffies + (30 * HZ);
1907 while (time_before(jiffies, wait_online)) {
1908
1909 if (adapter_up(ha))
1910 return QLA_SUCCESS;
1911 else if (ha->retry_reset_ha_cnt == 0)
1912 return QLA_ERROR;
1913
1914 msleep(2000);
1915 }
1916
1917 return QLA_ERROR;
1918}
1919
1920/**
Mike Christiece545032008-02-29 18:25:20 -06001921 * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001922 * @ha: pointer to HBA
David Somayajuluafaf5a22006-09-19 10:28:00 -07001923 * @t: target id
1924 * @l: lun id
1925 *
1926 * This function waits for all outstanding commands to a lun to complete. It
1927 * returns 0 if all pending commands are returned and 1 otherwise.
1928 **/
Mike Christiece545032008-02-29 18:25:20 -06001929static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
1930 struct scsi_target *stgt,
1931 struct scsi_device *sdev)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001932{
1933 int cnt;
1934 int status = 0;
1935 struct scsi_cmnd *cmd;
1936
1937 /*
Mike Christiece545032008-02-29 18:25:20 -06001938 * Waiting for all commands for the designated target or dev
1939 * in the active array
David Somayajuluafaf5a22006-09-19 10:28:00 -07001940 */
1941 for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
1942 cmd = scsi_host_find_tag(ha->host, cnt);
Mike Christiece545032008-02-29 18:25:20 -06001943 if (cmd && stgt == scsi_target(cmd->device) &&
1944 (!sdev || sdev == cmd->device)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001945 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
1946 status++;
1947 break;
1948 }
1949 }
1950 }
1951 return status;
1952}
1953
1954/**
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301955 * qla4xxx_eh_abort - callback for abort task.
1956 * @cmd: Pointer to Linux's SCSI command structure
1957 *
1958 * This routine is called by the Linux OS to abort the specified
1959 * command.
1960 **/
1961static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
1962{
1963 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
1964 unsigned int id = cmd->device->id;
1965 unsigned int lun = cmd->device->lun;
1966 unsigned long serial = cmd->serial_number;
1967 struct srb *srb = NULL;
1968 int ret = SUCCESS;
1969 int wait = 0;
1970
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301971 ql4_printk(KERN_INFO, ha,
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301972 "scsi%ld:%d:%d: Abort command issued cmd=%p, pid=%ld\n",
1973 ha->host_no, id, lun, cmd, serial);
1974
1975 srb = (struct srb *) CMD_SP(cmd);
1976
1977 if (!srb)
1978 return SUCCESS;
1979
1980 kref_get(&srb->srb_ref);
1981
1982 if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
1983 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
1984 ha->host_no, id, lun));
1985 ret = FAILED;
1986 } else {
1987 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
1988 ha->host_no, id, lun));
1989 wait = 1;
1990 }
1991
1992 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
1993
1994 /* Wait for command to complete */
1995 if (wait) {
1996 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
1997 DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
1998 ha->host_no, id, lun));
1999 ret = FAILED;
2000 }
2001 }
2002
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302003 ql4_printk(KERN_INFO, ha,
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302004 "scsi%ld:%d:%d: Abort command - %s\n",
2005 ha->host_no, id, lun, (ret == SUCCESS) ? "succeded" : "failed");
2006
2007 return ret;
2008}
2009
2010/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002011 * qla4xxx_eh_device_reset - callback for target reset.
2012 * @cmd: Pointer to Linux's SCSI command structure
2013 *
2014 * This routine is called by the Linux OS to reset all luns on the
2015 * specified target.
2016 **/
2017static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
2018{
2019 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2020 struct ddb_entry *ddb_entry = cmd->device->hostdata;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002021 int ret = FAILED, stat;
2022
Karen Higgins612f73482009-07-15 15:03:01 -05002023 if (!ddb_entry)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002024 return ret;
2025
Mike Christiec01be6d2010-07-22 16:59:49 +05302026 ret = iscsi_block_scsi_eh(cmd);
2027 if (ret)
2028 return ret;
2029 ret = FAILED;
2030
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302031 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002032 "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
2033 cmd->device->channel, cmd->device->id, cmd->device->lun);
2034
2035 DEBUG2(printk(KERN_INFO
2036 "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
2037 "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
Jens Axboe242f9dc2008-09-14 05:55:09 -07002038 cmd, jiffies, cmd->request->timeout / HZ,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002039 ha->dpc_flags, cmd->result, cmd->allowed));
2040
2041 /* FIXME: wait for hba to go online */
2042 stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
2043 if (stat != QLA_SUCCESS) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302044 ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002045 goto eh_dev_reset_done;
2046 }
2047
Mike Christiece545032008-02-29 18:25:20 -06002048 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2049 cmd->device)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302050 ql4_printk(KERN_INFO, ha,
Mike Christiece545032008-02-29 18:25:20 -06002051 "DEVICE RESET FAILED - waiting for "
2052 "commands.\n");
2053 goto eh_dev_reset_done;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002054 }
2055
David C Somayajulu9d562912008-03-19 11:23:03 -07002056 /* Send marker. */
2057 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2058 MM_LUN_RESET) != QLA_SUCCESS)
2059 goto eh_dev_reset_done;
2060
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302061 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002062 "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
2063 ha->host_no, cmd->device->channel, cmd->device->id,
2064 cmd->device->lun);
2065
2066 ret = SUCCESS;
2067
2068eh_dev_reset_done:
2069
2070 return ret;
2071}
2072
2073/**
Mike Christiece545032008-02-29 18:25:20 -06002074 * qla4xxx_eh_target_reset - callback for target reset.
2075 * @cmd: Pointer to Linux's SCSI command structure
2076 *
2077 * This routine is called by the Linux OS to reset the target.
2078 **/
2079static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
2080{
2081 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2082 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christiec01be6d2010-07-22 16:59:49 +05302083 int stat, ret;
Mike Christiece545032008-02-29 18:25:20 -06002084
2085 if (!ddb_entry)
2086 return FAILED;
2087
Mike Christiec01be6d2010-07-22 16:59:49 +05302088 ret = iscsi_block_scsi_eh(cmd);
2089 if (ret)
2090 return ret;
2091
Mike Christiece545032008-02-29 18:25:20 -06002092 starget_printk(KERN_INFO, scsi_target(cmd->device),
2093 "WARM TARGET RESET ISSUED.\n");
2094
2095 DEBUG2(printk(KERN_INFO
2096 "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
2097 "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
Jens Axboe242f9dc2008-09-14 05:55:09 -07002098 ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
Mike Christiece545032008-02-29 18:25:20 -06002099 ha->dpc_flags, cmd->result, cmd->allowed));
2100
2101 stat = qla4xxx_reset_target(ha, ddb_entry);
2102 if (stat != QLA_SUCCESS) {
2103 starget_printk(KERN_INFO, scsi_target(cmd->device),
2104 "WARM TARGET RESET FAILED.\n");
2105 return FAILED;
2106 }
2107
Mike Christiece545032008-02-29 18:25:20 -06002108 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2109 NULL)) {
2110 starget_printk(KERN_INFO, scsi_target(cmd->device),
2111 "WARM TARGET DEVICE RESET FAILED - "
2112 "waiting for commands.\n");
2113 return FAILED;
2114 }
2115
David C Somayajulu9d562912008-03-19 11:23:03 -07002116 /* Send marker. */
2117 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2118 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
2119 starget_printk(KERN_INFO, scsi_target(cmd->device),
2120 "WARM TARGET DEVICE RESET FAILED - "
2121 "marker iocb failed.\n");
2122 return FAILED;
2123 }
2124
Mike Christiece545032008-02-29 18:25:20 -06002125 starget_printk(KERN_INFO, scsi_target(cmd->device),
2126 "WARM TARGET RESET SUCCEEDED.\n");
2127 return SUCCESS;
2128}
2129
2130/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002131 * qla4xxx_eh_host_reset - kernel callback
2132 * @cmd: Pointer to Linux's SCSI command structure
2133 *
2134 * This routine is invoked by the Linux kernel to perform fatal error
2135 * recovery on the specified adapter.
2136 **/
2137static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
2138{
2139 int return_status = FAILED;
2140 struct scsi_qla_host *ha;
2141
2142 ha = (struct scsi_qla_host *) cmd->device->host->hostdata;
2143
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302144 if (ql4xdontresethba) {
2145 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
2146 ha->host_no, __func__));
2147 return FAILED;
2148 }
2149
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302150 ql4_printk(KERN_INFO, ha,
Karen Higginsdca05c42009-07-15 15:03:00 -05002151 "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002152 cmd->device->channel, cmd->device->id, cmd->device->lun);
2153
2154 if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
2155 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
2156 "DEAD.\n", ha->host_no, cmd->device->channel,
2157 __func__));
2158
2159 return FAILED;
2160 }
2161
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302162 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
2163 if (is_qla8022(ha))
2164 set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2165 else
2166 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2167 }
Mike Christie50a29ae2008-03-04 13:26:53 -06002168
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302169 if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002170 return_status = SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002171
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302172 ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
David Somayajuluafaf5a22006-09-19 10:28:00 -07002173 return_status == FAILED ? "FAILED" : "SUCCEDED");
2174
2175 return return_status;
2176}
2177
David Somayajuluafaf5a22006-09-19 10:28:00 -07002178static struct pci_device_id qla4xxx_pci_tbl[] = {
2179 {
2180 .vendor = PCI_VENDOR_ID_QLOGIC,
2181 .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
2182 .subvendor = PCI_ANY_ID,
2183 .subdevice = PCI_ANY_ID,
2184 },
2185 {
2186 .vendor = PCI_VENDOR_ID_QLOGIC,
2187 .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
2188 .subvendor = PCI_ANY_ID,
2189 .subdevice = PCI_ANY_ID,
2190 },
David C Somayajulud9150582006-11-15 17:38:40 -08002191 {
2192 .vendor = PCI_VENDOR_ID_QLOGIC,
2193 .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
2194 .subvendor = PCI_ANY_ID,
2195 .subdevice = PCI_ANY_ID,
2196 },
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302197 {
2198 .vendor = PCI_VENDOR_ID_QLOGIC,
2199 .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
2200 .subvendor = PCI_ANY_ID,
2201 .subdevice = PCI_ANY_ID,
2202 },
David Somayajuluafaf5a22006-09-19 10:28:00 -07002203 {0, 0},
2204};
2205MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
2206
Adrian Bunk47975472007-04-26 00:35:16 -07002207static struct pci_driver qla4xxx_pci_driver = {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002208 .name = DRIVER_NAME,
2209 .id_table = qla4xxx_pci_tbl,
2210 .probe = qla4xxx_probe_adapter,
2211 .remove = qla4xxx_remove_adapter,
2212};
2213
2214static int __init qla4xxx_module_init(void)
2215{
2216 int ret;
2217
2218 /* Allocate cache for SRBs. */
2219 srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09002220 SLAB_HWCACHE_ALIGN, NULL);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002221 if (srb_cachep == NULL) {
2222 printk(KERN_ERR
2223 "%s: Unable to allocate SRB cache..."
2224 "Failing load!\n", DRIVER_NAME);
2225 ret = -ENOMEM;
2226 goto no_srp_cache;
2227 }
2228
2229 /* Derive version string. */
2230 strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
Andrew Vasquez11010fe2006-10-06 09:54:59 -07002231 if (ql4xextended_error_logging)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002232 strcat(qla4xxx_version_str, "-debug");
2233
2234 qla4xxx_scsi_transport =
2235 iscsi_register_transport(&qla4xxx_iscsi_transport);
2236 if (!qla4xxx_scsi_transport){
2237 ret = -ENODEV;
2238 goto release_srb_cache;
2239 }
2240
David Somayajuluafaf5a22006-09-19 10:28:00 -07002241 ret = pci_register_driver(&qla4xxx_pci_driver);
2242 if (ret)
2243 goto unregister_transport;
2244
2245 printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
2246 return 0;
Doug Maxey5ae16db2006-10-05 23:50:07 -05002247
David Somayajuluafaf5a22006-09-19 10:28:00 -07002248unregister_transport:
2249 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
2250release_srb_cache:
2251 kmem_cache_destroy(srb_cachep);
2252no_srp_cache:
2253 return ret;
2254}
2255
2256static void __exit qla4xxx_module_exit(void)
2257{
David Somayajuluafaf5a22006-09-19 10:28:00 -07002258 pci_unregister_driver(&qla4xxx_pci_driver);
2259 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
2260 kmem_cache_destroy(srb_cachep);
2261}
2262
2263module_init(qla4xxx_module_init);
2264module_exit(qla4xxx_module_exit);
2265
2266MODULE_AUTHOR("QLogic Corporation");
2267MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
2268MODULE_LICENSE("GPL");
2269MODULE_VERSION(QLA4XXX_DRIVER_VERSION);