blob: 370d40ff15296ff8c1c19f3294a011ba94450b75 [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
Lalit Chandivade2232be02010-07-30 14:38:47 +0530478 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
479 if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
480 cmd->result = DID_NO_CONNECT << 16;
481 else
482 cmd->result = DID_REQUEUE << 16;
483 goto qc_fail_command;
484 }
485
Mike Christie7fb19212008-01-31 13:36:45 -0600486 if (!sess) {
487 cmd->result = DID_IMM_RETRY << 16;
488 goto qc_fail_command;
489 }
490
491 rval = iscsi_session_chkready(sess);
492 if (rval) {
493 cmd->result = rval;
494 goto qc_fail_command;
495 }
496
David Somayajuluafaf5a22006-09-19 10:28:00 -0700497 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
498 if (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD) {
499 cmd->result = DID_NO_CONNECT << 16;
500 goto qc_fail_command;
501 }
Mike Christiec5e98e92008-08-17 15:24:39 -0500502 return SCSI_MLQUEUE_TARGET_BUSY;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700503 }
504
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530505 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
506 test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
507 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
508 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
509 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
510 !test_bit(AF_ONLINE, &ha->flags) ||
511 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
David C Somayajulu477ffb92007-01-22 12:26:11 -0800512 goto qc_host_busy;
513
David Somayajuluafaf5a22006-09-19 10:28:00 -0700514 spin_unlock_irq(ha->host->host_lock);
515
516 srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd, done);
517 if (!srb)
518 goto qc_host_busy_lock;
519
520 rval = qla4xxx_send_command_to_isp(ha, srb);
521 if (rval != QLA_SUCCESS)
522 goto qc_host_busy_free_sp;
523
524 spin_lock_irq(ha->host->host_lock);
525 return 0;
526
527qc_host_busy_free_sp:
528 qla4xxx_srb_free_dma(ha, srb);
529 mempool_free(srb, ha->srb_mempool);
530
531qc_host_busy_lock:
532 spin_lock_irq(ha->host->host_lock);
533
534qc_host_busy:
535 return SCSI_MLQUEUE_HOST_BUSY;
536
537qc_fail_command:
538 done(cmd);
539
540 return 0;
541}
542
543/**
544 * qla4xxx_mem_free - frees memory allocated to adapter
545 * @ha: Pointer to host adapter structure.
546 *
547 * Frees memory previously allocated by qla4xxx_mem_alloc
548 **/
549static void qla4xxx_mem_free(struct scsi_qla_host *ha)
550{
551 if (ha->queues)
552 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
553 ha->queues_dma);
554
555 ha->queues_len = 0;
556 ha->queues = NULL;
557 ha->queues_dma = 0;
558 ha->request_ring = NULL;
559 ha->request_dma = 0;
560 ha->response_ring = NULL;
561 ha->response_dma = 0;
562 ha->shadow_regs = NULL;
563 ha->shadow_regs_dma = 0;
564
565 /* Free srb pool. */
566 if (ha->srb_mempool)
567 mempool_destroy(ha->srb_mempool);
568
569 ha->srb_mempool = NULL;
570
571 /* release io space registers */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530572 if (is_qla8022(ha)) {
573 if (ha->nx_pcibase)
574 iounmap(
575 (struct device_reg_82xx __iomem *)ha->nx_pcibase);
576
577 if (ha->nx_db_wr_ptr)
578 iounmap(
579 (struct device_reg_82xx __iomem *)ha->nx_db_wr_ptr);
580 } else if (ha->reg)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700581 iounmap(ha->reg);
582 pci_release_regions(ha->pdev);
583}
584
585/**
586 * qla4xxx_mem_alloc - allocates memory for use by adapter.
587 * @ha: Pointer to host adapter structure
588 *
589 * Allocates DMA memory for request and response queues. Also allocates memory
590 * for srbs.
591 **/
592static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
593{
594 unsigned long align;
595
596 /* Allocate contiguous block of DMA memory for queues. */
597 ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
598 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
599 sizeof(struct shadow_regs) +
600 MEM_ALIGN_VALUE +
601 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
602 ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
603 &ha->queues_dma, GFP_KERNEL);
604 if (ha->queues == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530605 ql4_printk(KERN_WARNING, ha,
606 "Memory Allocation failed - queues.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700607
608 goto mem_alloc_error_exit;
609 }
610 memset(ha->queues, 0, ha->queues_len);
611
612 /*
613 * As per RISC alignment requirements -- the bus-address must be a
614 * multiple of the request-ring size (in bytes).
615 */
616 align = 0;
617 if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
618 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
619 (MEM_ALIGN_VALUE - 1));
620
621 /* Update request and response queue pointers. */
622 ha->request_dma = ha->queues_dma + align;
623 ha->request_ring = (struct queue_entry *) (ha->queues + align);
624 ha->response_dma = ha->queues_dma + align +
625 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
626 ha->response_ring = (struct queue_entry *) (ha->queues + align +
627 (REQUEST_QUEUE_DEPTH *
628 QUEUE_SIZE));
629 ha->shadow_regs_dma = ha->queues_dma + align +
630 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
631 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
632 ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
633 (REQUEST_QUEUE_DEPTH *
634 QUEUE_SIZE) +
635 (RESPONSE_QUEUE_DEPTH *
636 QUEUE_SIZE));
637
638 /* Allocate memory for srb pool. */
639 ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
640 mempool_free_slab, srb_cachep);
641 if (ha->srb_mempool == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530642 ql4_printk(KERN_WARNING, ha,
643 "Memory Allocation failed - SRB Pool.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700644
645 goto mem_alloc_error_exit;
646 }
647
648 return QLA_SUCCESS;
649
650mem_alloc_error_exit:
651 qla4xxx_mem_free(ha);
652 return QLA_ERROR;
653}
654
655/**
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530656 * qla4_8xxx_check_fw_alive - Check firmware health
657 * @ha: Pointer to host adapter structure.
658 *
659 * Context: Interrupt
660 **/
661static void qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
662{
663 uint32_t fw_heartbeat_counter, halt_status;
664
665 fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
Lalit Chandivade2232be02010-07-30 14:38:47 +0530666 /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
667 if (fw_heartbeat_counter == 0xffffffff) {
668 DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
669 "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
670 ha->host_no, __func__));
671 return;
672 }
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530673
674 if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
675 ha->seconds_since_last_heartbeat++;
676 /* FW not alive after 2 seconds */
677 if (ha->seconds_since_last_heartbeat == 2) {
678 ha->seconds_since_last_heartbeat = 0;
679 halt_status = qla4_8xxx_rd_32(ha,
680 QLA82XX_PEG_HALT_STATUS1);
Nilesh Javali21033632010-07-30 14:28:07 +0530681
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530682 /* Since we cannot change dev_state in interrupt
683 * context, set appropriate DPC flag then wakeup
684 * DPC */
685 if (halt_status & HALT_STATUS_UNRECOVERABLE)
686 set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
687 else {
688 printk("scsi%ld: %s: detect abort needed!\n",
689 ha->host_no, __func__);
690 set_bit(DPC_RESET_HA, &ha->dpc_flags);
691 }
692 qla4xxx_wake_dpc(ha);
Nilesh Javali21033632010-07-30 14:28:07 +0530693 qla4xxx_mailbox_premature_completion(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530694 }
695 }
696 ha->fw_heartbeat_counter = fw_heartbeat_counter;
697}
698
699/**
700 * qla4_8xxx_watchdog - Poll dev state
701 * @ha: Pointer to host adapter structure.
702 *
703 * Context: Interrupt
704 **/
705void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
706{
707 uint32_t dev_state;
708
709 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
710
711 /* don't poll if reset is going on */
712 if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags)) {
713 if (dev_state == QLA82XX_DEV_NEED_RESET &&
714 !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
715 printk("scsi%ld: %s: HW State: NEED RESET!\n",
716 ha->host_no, __func__);
717 set_bit(DPC_RESET_HA, &ha->dpc_flags);
718 qla4xxx_wake_dpc(ha);
Nilesh Javali21033632010-07-30 14:28:07 +0530719 qla4xxx_mailbox_premature_completion(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530720 } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
721 !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
722 printk("scsi%ld: %s: HW State: NEED QUIES!\n",
723 ha->host_no, __func__);
724 set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
725 qla4xxx_wake_dpc(ha);
726 } else {
727 /* Check firmware health */
728 qla4_8xxx_check_fw_alive(ha);
729 }
730 }
731}
732
733/**
David Somayajuluafaf5a22006-09-19 10:28:00 -0700734 * qla4xxx_timer - checks every second for work to do.
735 * @ha: Pointer to host adapter structure.
736 **/
737static void qla4xxx_timer(struct scsi_qla_host *ha)
738{
739 struct ddb_entry *ddb_entry, *dtemp;
740 int start_dpc = 0;
Lalit Chandivade2232be02010-07-30 14:38:47 +0530741 uint16_t w;
742
743 /* If we are in the middle of AER/EEH processing
744 * skip any processing and reschedule the timer
745 */
746 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
747 mod_timer(&ha->timer, jiffies + HZ);
748 return;
749 }
750
751 /* Hardware read to trigger an EEH error during mailbox waits. */
752 if (!pci_channel_offline(ha->pdev))
753 pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700754
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530755 if (test_bit(AF_HBA_GOING_AWAY, &ha->flags)) {
756 DEBUG2(ql4_printk(KERN_INFO, ha, "%s exited. HBA GOING AWAY\n",
757 __func__));
758 return;
759 }
760
761 if (is_qla8022(ha)) {
762 qla4_8xxx_watchdog(ha);
763 }
764
David Somayajuluafaf5a22006-09-19 10:28:00 -0700765 /* Search for relogin's to time-out and port down retry. */
766 list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
767 /* Count down time between sending relogins */
768 if (adapter_up(ha) &&
769 !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
770 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
771 if (atomic_read(&ddb_entry->retry_relogin_timer) !=
772 INVALID_ENTRY) {
773 if (atomic_read(&ddb_entry->retry_relogin_timer)
774 == 0) {
775 atomic_set(&ddb_entry->
776 retry_relogin_timer,
777 INVALID_ENTRY);
778 set_bit(DPC_RELOGIN_DEVICE,
779 &ha->dpc_flags);
780 set_bit(DF_RELOGIN, &ddb_entry->flags);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530781 DEBUG2(printk("scsi%ld: %s: ddb [%d]"
David Somayajuluafaf5a22006-09-19 10:28:00 -0700782 " login device\n",
783 ha->host_no, __func__,
784 ddb_entry->fw_ddb_index));
785 } else
786 atomic_dec(&ddb_entry->
787 retry_relogin_timer);
788 }
789 }
790
791 /* Wait for relogin to timeout */
792 if (atomic_read(&ddb_entry->relogin_timer) &&
793 (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
794 /*
795 * If the relogin times out and the device is
796 * still NOT ONLINE then try and relogin again.
797 */
798 if (atomic_read(&ddb_entry->state) !=
799 DDB_STATE_ONLINE &&
800 ddb_entry->fw_ddb_device_state ==
801 DDB_DS_SESSION_FAILED) {
802 /* Reset retry relogin timer */
803 atomic_inc(&ddb_entry->relogin_retry_count);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530804 DEBUG2(printk("scsi%ld: ddb [%d] relogin"
David Somayajuluafaf5a22006-09-19 10:28:00 -0700805 " timed out-retrying"
806 " relogin (%d)\n",
807 ha->host_no,
808 ddb_entry->fw_ddb_index,
809 atomic_read(&ddb_entry->
810 relogin_retry_count))
811 );
812 start_dpc++;
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530813 DEBUG(printk("scsi%ld:%d:%d: ddb [%d] "
David Somayajuluafaf5a22006-09-19 10:28:00 -0700814 "initate relogin after"
815 " %d seconds\n",
816 ha->host_no, ddb_entry->bus,
817 ddb_entry->target,
818 ddb_entry->fw_ddb_index,
819 ddb_entry->default_time2wait + 4)
820 );
821
822 atomic_set(&ddb_entry->retry_relogin_timer,
823 ddb_entry->default_time2wait + 4);
824 }
825 }
826 }
827
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530828 if (!is_qla8022(ha)) {
829 /* Check for heartbeat interval. */
830 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
831 ha->heartbeat_interval != 0) {
832 ha->seconds_since_last_heartbeat++;
833 if (ha->seconds_since_last_heartbeat >
834 ha->heartbeat_interval + 2)
835 set_bit(DPC_RESET_HA, &ha->dpc_flags);
836 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700837 }
838
David Somayajuluafaf5a22006-09-19 10:28:00 -0700839 /* Wakeup the dpc routine for this adapter, if needed. */
840 if ((start_dpc ||
841 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
842 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
843 test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530844 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -0700845 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
846 test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +0530847 test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530848 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
849 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -0700850 test_bit(DPC_AEN, &ha->dpc_flags)) &&
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530851 !test_bit(AF_DPC_SCHEDULED, &ha->flags) &&
David Somayajuluafaf5a22006-09-19 10:28:00 -0700852 ha->dpc_thread) {
853 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
854 " - dpc flags = 0x%lx\n",
855 ha->host_no, __func__, ha->dpc_flags));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530856 qla4xxx_wake_dpc(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700857 }
858
859 /* Reschedule timer thread to call us back in one second */
860 mod_timer(&ha->timer, jiffies + HZ);
861
862 DEBUG2(ha->seconds_since_last_intr++);
863}
864
865/**
866 * qla4xxx_cmd_wait - waits for all outstanding commands to complete
867 * @ha: Pointer to host adapter structure.
868 *
869 * This routine stalls the driver until all outstanding commands are returned.
870 * Caller must release the Hardware Lock prior to calling this routine.
871 **/
872static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
873{
874 uint32_t index = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700875 unsigned long flags;
876 struct scsi_cmnd *cmd;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700877
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530878 unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
879
880 DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
881 "complete\n", WAIT_CMD_TOV));
882
883 while (!time_after_eq(jiffies, wtime)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700884 spin_lock_irqsave(&ha->hardware_lock, flags);
885 /* Find a command that hasn't completed. */
886 for (index = 0; index < ha->host->can_queue; index++) {
887 cmd = scsi_host_find_tag(ha->host, index);
888 if (cmd != NULL)
889 break;
890 }
891 spin_unlock_irqrestore(&ha->hardware_lock, flags);
892
893 /* If No Commands are pending, wait is complete */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530894 if (index == ha->host->can_queue)
895 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700896
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530897 msleep(1000);
898 }
899 /* If we timed out on waiting for commands to come back
900 * return ERROR. */
901 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700902}
903
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530904int qla4xxx_hw_reset(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700905{
David Somayajuluafaf5a22006-09-19 10:28:00 -0700906 uint32_t ctrl_status;
David C Somayajulu477ffb92007-01-22 12:26:11 -0800907 unsigned long flags = 0;
908
909 DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700910
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530911 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
912 return QLA_ERROR;
913
David Somayajuluafaf5a22006-09-19 10:28:00 -0700914 spin_lock_irqsave(&ha->hardware_lock, flags);
915
916 /*
917 * If the SCSI Reset Interrupt bit is set, clear it.
918 * Otherwise, the Soft Reset won't work.
919 */
920 ctrl_status = readw(&ha->reg->ctrl_status);
921 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
922 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
923
924 /* Issue Soft Reset */
925 writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
926 readl(&ha->reg->ctrl_status);
927
928 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530929 return QLA_SUCCESS;
David C Somayajulu477ffb92007-01-22 12:26:11 -0800930}
931
932/**
933 * qla4xxx_soft_reset - performs soft reset.
934 * @ha: Pointer to host adapter structure.
935 **/
936int qla4xxx_soft_reset(struct scsi_qla_host *ha)
937{
938 uint32_t max_wait_time;
939 unsigned long flags = 0;
940 int status = QLA_ERROR;
941 uint32_t ctrl_status;
942
943 qla4xxx_hw_reset(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700944
945 /* Wait until the Network Reset Intr bit is cleared */
946 max_wait_time = RESET_INTR_TOV;
947 do {
948 spin_lock_irqsave(&ha->hardware_lock, flags);
949 ctrl_status = readw(&ha->reg->ctrl_status);
950 spin_unlock_irqrestore(&ha->hardware_lock, flags);
951
952 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
953 break;
954
955 msleep(1000);
956 } while ((--max_wait_time));
957
958 if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
959 DEBUG2(printk(KERN_WARNING
960 "scsi%ld: Network Reset Intr not cleared by "
961 "Network function, clearing it now!\n",
962 ha->host_no));
963 spin_lock_irqsave(&ha->hardware_lock, flags);
964 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
965 readl(&ha->reg->ctrl_status);
966 spin_unlock_irqrestore(&ha->hardware_lock, flags);
967 }
968
969 /* Wait until the firmware tells us the Soft Reset is done */
970 max_wait_time = SOFT_RESET_TOV;
971 do {
972 spin_lock_irqsave(&ha->hardware_lock, flags);
973 ctrl_status = readw(&ha->reg->ctrl_status);
974 spin_unlock_irqrestore(&ha->hardware_lock, flags);
975
976 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
977 status = QLA_SUCCESS;
978 break;
979 }
980
981 msleep(1000);
982 } while ((--max_wait_time));
983
984 /*
985 * Also, make sure that the SCSI Reset Interrupt bit has been cleared
986 * after the soft reset has taken place.
987 */
988 spin_lock_irqsave(&ha->hardware_lock, flags);
989 ctrl_status = readw(&ha->reg->ctrl_status);
990 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
991 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
992 readl(&ha->reg->ctrl_status);
993 }
994 spin_unlock_irqrestore(&ha->hardware_lock, flags);
995
996 /* If soft reset fails then most probably the bios on other
997 * function is also enabled.
998 * Since the initialization is sequential the other fn
999 * wont be able to acknowledge the soft reset.
1000 * Issue a force soft reset to workaround this scenario.
1001 */
1002 if (max_wait_time == 0) {
1003 /* Issue Force Soft Reset */
1004 spin_lock_irqsave(&ha->hardware_lock, flags);
1005 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
1006 readl(&ha->reg->ctrl_status);
1007 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1008 /* Wait until the firmware tells us the Soft Reset is done */
1009 max_wait_time = SOFT_RESET_TOV;
1010 do {
1011 spin_lock_irqsave(&ha->hardware_lock, flags);
1012 ctrl_status = readw(&ha->reg->ctrl_status);
1013 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1014
1015 if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
1016 status = QLA_SUCCESS;
1017 break;
1018 }
1019
1020 msleep(1000);
1021 } while ((--max_wait_time));
1022 }
1023
1024 return status;
1025}
1026
1027/**
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301028 * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001029 * @ha: Pointer to host adapter structure.
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301030 * @res: returned scsi status
David Somayajuluafaf5a22006-09-19 10:28:00 -07001031 *
1032 * This routine is called just prior to a HARD RESET to return all
1033 * outstanding commands back to the Operating System.
1034 * Caller should make sure that the following locks are released
1035 * before this calling routine: Hardware lock, and io_request_lock.
1036 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301037static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001038{
1039 struct srb *srb;
1040 int i;
1041 unsigned long flags;
1042
1043 spin_lock_irqsave(&ha->hardware_lock, flags);
1044 for (i = 0; i < ha->host->can_queue; i++) {
1045 srb = qla4xxx_del_from_active_array(ha, i);
1046 if (srb != NULL) {
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301047 srb->cmd->result = res;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301048 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001049 }
1050 }
1051 spin_unlock_irqrestore(&ha->hardware_lock, flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001052}
1053
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301054void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
1055{
1056 clear_bit(AF_ONLINE, &ha->flags);
1057
1058 /* Disable the board */
1059 ql4_printk(KERN_INFO, ha, "Disabling the board\n");
1060 set_bit(AF_HBA_GOING_AWAY, &ha->flags);
1061
1062 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
1063 qla4xxx_mark_all_devices_missing(ha);
1064 clear_bit(AF_INIT_DONE, &ha->flags);
1065}
1066
David Somayajuluafaf5a22006-09-19 10:28:00 -07001067/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07001068 * qla4xxx_recover_adapter - recovers adapter after a fatal error
1069 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001070 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301071static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001072{
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301073 int status = QLA_ERROR;
1074 uint8_t reset_chip = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001075
1076 /* Stall incoming I/O until we are done */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301077 scsi_block_requests(ha->host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001078 clear_bit(AF_ONLINE, &ha->flags);
Mike Christie50a29ae2008-03-04 13:26:53 -06001079
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301080 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001081
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301082 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001083
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301084 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
1085 reset_chip = 1;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001086
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301087 /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
1088 * do not reset adapter, jump to initialize_adapter */
1089 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1090 status = QLA_SUCCESS;
1091 goto recover_ha_init_adapter;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001092 }
1093
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301094 /* For the ISP-82xx adapter, issue a stop_firmware if invoked
1095 * from eh_host_reset or ioctl module */
1096 if (is_qla8022(ha) && !reset_chip &&
1097 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
1098
1099 DEBUG2(ql4_printk(KERN_INFO, ha,
1100 "scsi%ld: %s - Performing stop_firmware...\n",
1101 ha->host_no, __func__));
1102 status = ha->isp_ops->reset_firmware(ha);
1103 if (status == QLA_SUCCESS) {
1104 qla4xxx_cmd_wait(ha);
1105 ha->isp_ops->disable_intrs(ha);
1106 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1107 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1108 } else {
1109 /* If the stop_firmware fails then
1110 * reset the entire chip */
1111 reset_chip = 1;
1112 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1113 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1114 }
1115 }
1116
1117 /* Issue full chip reset if recovering from a catastrophic error,
1118 * or if stop_firmware fails for ISP-82xx.
1119 * This is the default case for ISP-4xxx */
1120 if (!is_qla8022(ha) || reset_chip) {
1121 qla4xxx_cmd_wait(ha);
1122 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1123 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1124 DEBUG2(ql4_printk(KERN_INFO, ha,
1125 "scsi%ld: %s - Performing chip reset..\n",
1126 ha->host_no, __func__));
1127 status = ha->isp_ops->reset_chip(ha);
1128 }
1129
1130 /* Flush any pending ddb changed AENs */
1131 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1132
1133recover_ha_init_adapter:
1134 /* Upon successful firmware/chip reset, re-initialize the adapter */
1135 if (status == QLA_SUCCESS) {
1136 /* For ISP-4xxx, force function 1 to always initialize
1137 * before function 3 to prevent both funcions from
1138 * stepping on top of the other */
1139 if (!is_qla8022(ha) && (ha->mac_index == 3))
1140 ssleep(6);
1141
1142 /* NOTE: AF_ONLINE flag set upon successful completion of
1143 * qla4xxx_initialize_adapter */
1144 status = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
1145 }
1146
1147 /* Retry failed adapter initialization, if necessary
1148 * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
1149 * case to prevent ping-pong resets between functions */
1150 if (!test_bit(AF_ONLINE, &ha->flags) &&
1151 !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001152 /* Adapter initialization failed, see if we can retry
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301153 * resetting the ha.
1154 * Since we don't want to block the DPC for too long
1155 * with multiple resets in the same thread,
1156 * utilize DPC to retry */
David Somayajuluafaf5a22006-09-19 10:28:00 -07001157 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
1158 ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
1159 DEBUG2(printk("scsi%ld: recover adapter - retrying "
1160 "(%d) more times\n", ha->host_no,
1161 ha->retry_reset_ha_cnt));
1162 set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1163 status = QLA_ERROR;
1164 } else {
1165 if (ha->retry_reset_ha_cnt > 0) {
1166 /* Schedule another Reset HA--DPC will retry */
1167 ha->retry_reset_ha_cnt--;
1168 DEBUG2(printk("scsi%ld: recover adapter - "
1169 "retry remaining %d\n",
1170 ha->host_no,
1171 ha->retry_reset_ha_cnt));
1172 status = QLA_ERROR;
1173 }
1174
1175 if (ha->retry_reset_ha_cnt == 0) {
1176 /* Recover adapter retries have been exhausted.
1177 * Adapter DEAD */
1178 DEBUG2(printk("scsi%ld: recover adapter "
1179 "failed - board disabled\n",
1180 ha->host_no));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301181 qla4xxx_dead_adapter_cleanup(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001182 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1183 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301184 clear_bit(DPC_RESET_HA_FW_CONTEXT,
David Somayajuluafaf5a22006-09-19 10:28:00 -07001185 &ha->dpc_flags);
1186 status = QLA_ERROR;
1187 }
1188 }
1189 } else {
1190 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301191 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001192 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1193 }
1194
1195 ha->adapter_error_count++;
1196
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301197 if (test_bit(AF_ONLINE, &ha->flags))
1198 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001199
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301200 scsi_unblock_requests(ha->host);
1201
1202 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
1203 DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
1204 status == QLA_ERROR ? "FAILED" : "SUCCEDED"));
1205
David Somayajuluafaf5a22006-09-19 10:28:00 -07001206 return status;
1207}
1208
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301209void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
1210{
1211 if (ha->dpc_thread &&
1212 !test_bit(AF_DPC_SCHEDULED, &ha->flags)) {
1213 set_bit(AF_DPC_SCHEDULED, &ha->flags);
1214 queue_work(ha->dpc_thread, &ha->dpc_work);
1215 }
1216}
1217
David Somayajuluafaf5a22006-09-19 10:28:00 -07001218/**
1219 * qla4xxx_do_dpc - dpc routine
1220 * @data: in our case pointer to adapter structure
1221 *
1222 * This routine is a task that is schedule by the interrupt handler
1223 * to perform the background processing for interrupts. We put it
1224 * on a task queue that is consumed whenever the scheduler runs; that's
1225 * so you can do anything (i.e. put the process to sleep etc). In fact,
1226 * the mid-level tries to sleep when it reaches the driver threshold
1227 * "host->can_queue". This can cause a panic if we were in our interrupt code.
1228 **/
David Howellsc4028952006-11-22 14:57:56 +00001229static void qla4xxx_do_dpc(struct work_struct *work)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001230{
David Howellsc4028952006-11-22 14:57:56 +00001231 struct scsi_qla_host *ha =
1232 container_of(work, struct scsi_qla_host, dpc_work);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001233 struct ddb_entry *ddb_entry, *dtemp;
David C Somayajulu477ffb92007-01-22 12:26:11 -08001234 int status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001235
David C Somayajuluf26b9042006-11-15 16:41:09 -08001236 DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301237 "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
1238 ha->host_no, __func__, ha->flags, ha->dpc_flags))
David Somayajuluafaf5a22006-09-19 10:28:00 -07001239
1240 /* Initialization not yet finished. Don't do anything yet. */
1241 if (!test_bit(AF_INIT_DONE, &ha->flags))
Vikas Chaudhary0753b482010-07-30 14:27:19 +05301242 goto do_dpc_exit;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001243
Lalit Chandivade2232be02010-07-30 14:38:47 +05301244 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1245 DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
1246 ha->host_no, __func__, ha->flags));
1247 goto do_dpc_exit;
1248 }
1249
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301250 /* HBA is in the process of being permanently disabled.
1251 * Don't process anything */
1252 if (test_bit(AF_HBA_GOING_AWAY, &ha->flags))
1253 return;
1254
1255 if (is_qla8022(ha)) {
1256 if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
1257 qla4_8xxx_idc_lock(ha);
1258 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
1259 QLA82XX_DEV_FAILED);
1260 qla4_8xxx_idc_unlock(ha);
1261 ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
1262 qla4_8xxx_device_state_handler(ha);
1263 }
1264 if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
1265 qla4_8xxx_need_qsnt_handler(ha);
1266 }
1267 }
1268
1269 if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
1270 (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07001271 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301272 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
1273 if (ql4xdontresethba) {
1274 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
1275 ha->host_no, __func__));
1276 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
1277 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1278 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1279 goto dpc_post_reset_ha;
1280 }
1281 if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
1282 test_bit(DPC_RESET_HA, &ha->dpc_flags))
1283 qla4xxx_recover_adapter(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001284
David C Somayajulu477ffb92007-01-22 12:26:11 -08001285 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001286 uint8_t wait_time = RESET_INTR_TOV;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001287
David Somayajuluafaf5a22006-09-19 10:28:00 -07001288 while ((readw(&ha->reg->ctrl_status) &
1289 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
1290 if (--wait_time == 0)
1291 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001292 msleep(1000);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001293 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001294 if (wait_time == 0)
1295 DEBUG2(printk("scsi%ld: %s: SR|FSR "
1296 "bit not cleared-- resetting\n",
1297 ha->host_no, __func__));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301298 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
David C Somayajulu477ffb92007-01-22 12:26:11 -08001299 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
1300 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301301 status = qla4xxx_recover_adapter(ha);
David C Somayajulu477ffb92007-01-22 12:26:11 -08001302 }
1303 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1304 if (status == QLA_SUCCESS)
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301305 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001306 }
1307 }
1308
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301309dpc_post_reset_ha:
David Somayajuluafaf5a22006-09-19 10:28:00 -07001310 /* ---- process AEN? --- */
1311 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
1312 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
1313
1314 /* ---- Get DHCP IP Address? --- */
1315 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
1316 qla4xxx_get_dhcp_ip_address(ha);
1317
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301318 /* ---- link change? --- */
1319 if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
1320 if (!test_bit(AF_LINK_UP, &ha->flags)) {
1321 /* ---- link down? --- */
1322 list_for_each_entry_safe(ddb_entry, dtemp,
1323 &ha->ddb_list, list) {
1324 if (atomic_read(&ddb_entry->state) ==
1325 DDB_STATE_ONLINE)
1326 qla4xxx_mark_device_missing(ha,
1327 ddb_entry);
1328 }
1329 } else {
1330 /* ---- link up? --- *
1331 * F/W will auto login to all devices ONLY ONCE after
1332 * link up during driver initialization and runtime
1333 * fatal error recovery. Therefore, the driver must
1334 * manually relogin to devices when recovering from
1335 * connection failures, logouts, expired KATO, etc. */
1336
1337 list_for_each_entry_safe(ddb_entry, dtemp,
1338 &ha->ddb_list, list) {
1339 if ((atomic_read(&ddb_entry->state) ==
1340 DDB_STATE_MISSING) ||
1341 (atomic_read(&ddb_entry->state) ==
1342 DDB_STATE_DEAD)) {
1343 if (ddb_entry->fw_ddb_device_state ==
1344 DDB_DS_SESSION_ACTIVE) {
1345 atomic_set(&ddb_entry->state,
1346 DDB_STATE_ONLINE);
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301347 ql4_printk(KERN_INFO, ha,
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301348 "scsi%ld: %s: ddb[%d]"
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301349 " marked ONLINE\n",
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301350 ha->host_no, __func__,
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301351 ddb_entry->fw_ddb_index);
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301352
1353 iscsi_unblock_session(
1354 ddb_entry->sess);
1355 } else
1356 qla4xxx_relogin_device(
1357 ha, ddb_entry);
1358 }
1359
1360 }
1361 }
1362 }
1363
David Somayajuluafaf5a22006-09-19 10:28:00 -07001364 /* ---- relogin device? --- */
1365 if (adapter_up(ha) &&
1366 test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
1367 list_for_each_entry_safe(ddb_entry, dtemp,
1368 &ha->ddb_list, list) {
1369 if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
1370 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
1371 qla4xxx_relogin_device(ha, ddb_entry);
1372
1373 /*
1374 * If mbx cmd times out there is no point
1375 * in continuing further.
1376 * With large no of targets this can hang
1377 * the system.
1378 */
1379 if (test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
1380 printk(KERN_WARNING "scsi%ld: %s: "
1381 "need to reset hba\n",
1382 ha->host_no, __func__);
1383 break;
1384 }
1385 }
1386 }
Vikas Chaudhary0753b482010-07-30 14:27:19 +05301387
1388do_dpc_exit:
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301389 clear_bit(AF_DPC_SCHEDULED, &ha->flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001390}
1391
1392/**
1393 * qla4xxx_free_adapter - release the adapter
1394 * @ha: pointer to adapter structure
1395 **/
1396static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
1397{
1398
1399 if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
1400 /* Turn-off interrupts on the card. */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301401 ha->isp_ops->disable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001402 }
1403
David Somayajuluafaf5a22006-09-19 10:28:00 -07001404 /* Remove timer thread, if present */
1405 if (ha->timer_active)
1406 qla4xxx_stop_timer(ha);
1407
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301408 /* Kill the kernel thread for this host */
1409 if (ha->dpc_thread)
1410 destroy_workqueue(ha->dpc_thread);
1411
1412 /* Put firmware in known state */
1413 ha->isp_ops->reset_firmware(ha);
1414
1415 if (is_qla8022(ha)) {
1416 qla4_8xxx_idc_lock(ha);
1417 qla4_8xxx_clear_drv_active(ha);
1418 qla4_8xxx_idc_unlock(ha);
1419 }
1420
David Somayajuluafaf5a22006-09-19 10:28:00 -07001421 /* Detach interrupts */
1422 if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301423 qla4xxx_free_irqs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001424
David C Somayajulubee4fe82007-05-23 18:03:32 -07001425 /* free extra memory */
1426 qla4xxx_mem_free(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301427}
David C Somayajulubee4fe82007-05-23 18:03:32 -07001428
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301429int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
1430{
1431 int status = 0;
1432 uint8_t revision_id;
1433 unsigned long mem_base, mem_len, db_base, db_len;
1434 struct pci_dev *pdev = ha->pdev;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001435
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301436 status = pci_request_regions(pdev, DRIVER_NAME);
1437 if (status) {
1438 printk(KERN_WARNING
1439 "scsi(%ld) Failed to reserve PIO regions (%s) "
1440 "status=%d\n", ha->host_no, pci_name(pdev), status);
1441 goto iospace_error_exit;
1442 }
1443
1444 pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
1445 DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
1446 __func__, revision_id));
1447 ha->revision_id = revision_id;
1448
1449 /* remap phys address */
1450 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
1451 mem_len = pci_resource_len(pdev, 0);
1452 DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
1453 __func__, mem_base, mem_len));
1454
1455 /* mapping of pcibase pointer */
1456 ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
1457 if (!ha->nx_pcibase) {
1458 printk(KERN_ERR
1459 "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
1460 pci_release_regions(ha->pdev);
1461 goto iospace_error_exit;
1462 }
1463
1464 /* Mapping of IO base pointer, door bell read and write pointer */
1465
1466 /* mapping of IO base pointer */
1467 ha->qla4_8xxx_reg =
1468 (struct device_reg_82xx __iomem *)((uint8_t *)ha->nx_pcibase +
1469 0xbc000 + (ha->pdev->devfn << 11));
1470
1471 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
1472 db_len = pci_resource_len(pdev, 4);
1473
1474 /* mapping of doorbell write pointer */
1475 ha->nx_db_wr_ptr = (unsigned long)ioremap(db_base +
1476 (ha->pdev->devfn << 12), 4);
1477 if (!ha->nx_db_wr_ptr) {
1478 printk(KERN_ERR
1479 "cannot remap MMIO doorbell-write (%s), aborting\n",
1480 pci_name(pdev));
1481 goto iospace_error_exit;
1482 }
1483 /* mapping of doorbell read pointer */
1484 ha->nx_db_rd_ptr = (uint8_t *) ha->nx_pcibase + (512 * 1024) +
1485 (ha->pdev->devfn * 8);
1486 if (!ha->nx_db_rd_ptr)
1487 printk(KERN_ERR
1488 "cannot remap MMIO doorbell-read (%s), aborting\n",
1489 pci_name(pdev));
1490 return 0;
1491
1492iospace_error_exit:
1493 return -ENOMEM;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001494}
1495
1496/***
1497 * qla4xxx_iospace_config - maps registers
1498 * @ha: pointer to adapter structure
1499 *
1500 * This routines maps HBA's registers from the pci address space
1501 * into the kernel virtual address space for memory mapped i/o.
1502 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301503int qla4xxx_iospace_config(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001504{
1505 unsigned long pio, pio_len, pio_flags;
1506 unsigned long mmio, mmio_len, mmio_flags;
1507
1508 pio = pci_resource_start(ha->pdev, 0);
1509 pio_len = pci_resource_len(ha->pdev, 0);
1510 pio_flags = pci_resource_flags(ha->pdev, 0);
1511 if (pio_flags & IORESOURCE_IO) {
1512 if (pio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301513 ql4_printk(KERN_WARNING, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07001514 "Invalid PCI I/O region size\n");
1515 pio = 0;
1516 }
1517 } else {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301518 ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001519 pio = 0;
1520 }
1521
1522 /* Use MMIO operations for all accesses. */
1523 mmio = pci_resource_start(ha->pdev, 1);
1524 mmio_len = pci_resource_len(ha->pdev, 1);
1525 mmio_flags = pci_resource_flags(ha->pdev, 1);
1526
1527 if (!(mmio_flags & IORESOURCE_MEM)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301528 ql4_printk(KERN_ERR, ha,
1529 "region #0 not an MMIO resource, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001530
1531 goto iospace_error_exit;
1532 }
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301533
David Somayajuluafaf5a22006-09-19 10:28:00 -07001534 if (mmio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301535 ql4_printk(KERN_ERR, ha,
1536 "Invalid PCI mem region size, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001537 goto iospace_error_exit;
1538 }
1539
1540 if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301541 ql4_printk(KERN_WARNING, ha,
1542 "Failed to reserve PIO/MMIO regions\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001543
1544 goto iospace_error_exit;
1545 }
1546
1547 ha->pio_address = pio;
1548 ha->pio_length = pio_len;
1549 ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
1550 if (!ha->reg) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301551 ql4_printk(KERN_ERR, ha,
1552 "cannot remap MMIO, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001553
1554 goto iospace_error_exit;
1555 }
1556
1557 return 0;
1558
1559iospace_error_exit:
1560 return -ENOMEM;
1561}
1562
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301563static struct isp_operations qla4xxx_isp_ops = {
1564 .iospace_config = qla4xxx_iospace_config,
1565 .pci_config = qla4xxx_pci_config,
1566 .disable_intrs = qla4xxx_disable_intrs,
1567 .enable_intrs = qla4xxx_enable_intrs,
1568 .start_firmware = qla4xxx_start_firmware,
1569 .intr_handler = qla4xxx_intr_handler,
1570 .interrupt_service_routine = qla4xxx_interrupt_service_routine,
1571 .reset_chip = qla4xxx_soft_reset,
1572 .reset_firmware = qla4xxx_hw_reset,
1573 .queue_iocb = qla4xxx_queue_iocb,
1574 .complete_iocb = qla4xxx_complete_iocb,
1575 .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
1576 .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
1577 .get_sys_info = qla4xxx_get_sys_info,
1578};
1579
1580static struct isp_operations qla4_8xxx_isp_ops = {
1581 .iospace_config = qla4_8xxx_iospace_config,
1582 .pci_config = qla4_8xxx_pci_config,
1583 .disable_intrs = qla4_8xxx_disable_intrs,
1584 .enable_intrs = qla4_8xxx_enable_intrs,
1585 .start_firmware = qla4_8xxx_load_risc,
1586 .intr_handler = qla4_8xxx_intr_handler,
1587 .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
1588 .reset_chip = qla4_8xxx_isp_reset,
1589 .reset_firmware = qla4_8xxx_stop_firmware,
1590 .queue_iocb = qla4_8xxx_queue_iocb,
1591 .complete_iocb = qla4_8xxx_complete_iocb,
1592 .rd_shdw_req_q_out = qla4_8xxx_rd_shdw_req_q_out,
1593 .rd_shdw_rsp_q_in = qla4_8xxx_rd_shdw_rsp_q_in,
1594 .get_sys_info = qla4_8xxx_get_sys_info,
1595};
1596
1597uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
1598{
1599 return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
1600}
1601
1602uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
1603{
1604 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
1605}
1606
1607uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
1608{
1609 return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
1610}
1611
1612uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
1613{
1614 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
1615}
1616
David Somayajuluafaf5a22006-09-19 10:28:00 -07001617/**
1618 * qla4xxx_probe_adapter - callback function to probe HBA
1619 * @pdev: pointer to pci_dev structure
1620 * @pci_device_id: pointer to pci_device entry
1621 *
1622 * This routine will probe for Qlogic 4xxx iSCSI host adapters.
1623 * It returns zero if successful. It also initializes all data necessary for
1624 * the driver.
1625 **/
1626static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
1627 const struct pci_device_id *ent)
1628{
1629 int ret = -ENODEV, status;
1630 struct Scsi_Host *host;
1631 struct scsi_qla_host *ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001632 uint8_t init_retry_count = 0;
1633 char buf[34];
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301634 struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001635
1636 if (pci_enable_device(pdev))
1637 return -1;
1638
1639 host = scsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha));
1640 if (host == NULL) {
1641 printk(KERN_WARNING
1642 "qla4xxx: Couldn't allocate host from scsi layer!\n");
1643 goto probe_disable_device;
1644 }
1645
1646 /* Clear our data area */
1647 ha = (struct scsi_qla_host *) host->hostdata;
1648 memset(ha, 0, sizeof(*ha));
1649
1650 /* Save the information from PCI BIOS. */
1651 ha->pdev = pdev;
1652 ha->host = host;
1653 ha->host_no = host->host_no;
1654
Lalit Chandivade2232be02010-07-30 14:38:47 +05301655 pci_enable_pcie_error_reporting(pdev);
1656
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301657 /* Setup Runtime configurable options */
1658 if (is_qla8022(ha)) {
1659 ha->isp_ops = &qla4_8xxx_isp_ops;
1660 rwlock_init(&ha->hw_lock);
1661 ha->qdr_sn_window = -1;
1662 ha->ddr_mn_window = -1;
1663 ha->curr_window = 255;
1664 ha->func_num = PCI_FUNC(ha->pdev->devfn);
1665 nx_legacy_intr = &legacy_intr[ha->func_num];
1666 ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
1667 ha->nx_legacy_intr.tgt_status_reg =
1668 nx_legacy_intr->tgt_status_reg;
1669 ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
1670 ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
1671 } else {
1672 ha->isp_ops = &qla4xxx_isp_ops;
1673 }
1674
Lalit Chandivade2232be02010-07-30 14:38:47 +05301675 /* Set EEH reset type to fundamental if required by hba */
1676 if (is_qla8022(ha))
1677 pdev->needs_freset = 1;
1678
David Somayajuluafaf5a22006-09-19 10:28:00 -07001679 /* Configure PCI I/O space. */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301680 ret = ha->isp_ops->iospace_config(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001681 if (ret)
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301682 goto probe_failed_ioconfig;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001683
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301684 ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
David Somayajuluafaf5a22006-09-19 10:28:00 -07001685 pdev->device, pdev->irq, ha->reg);
1686
1687 qla4xxx_config_dma_addressing(ha);
1688
1689 /* Initialize lists and spinlocks. */
1690 INIT_LIST_HEAD(&ha->ddb_list);
1691 INIT_LIST_HEAD(&ha->free_srb_q);
1692
1693 mutex_init(&ha->mbox_sem);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301694 init_completion(&ha->mbx_intr_comp);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001695
1696 spin_lock_init(&ha->hardware_lock);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001697
1698 /* Allocate dma buffers */
1699 if (qla4xxx_mem_alloc(ha)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301700 ql4_printk(KERN_WARNING, ha,
1701 "[ERROR] Failed to allocate memory for adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001702
1703 ret = -ENOMEM;
1704 goto probe_failed;
1705 }
1706
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301707 if (is_qla8022(ha))
1708 (void) qla4_8xxx_get_flash_info(ha);
1709
David Somayajuluafaf5a22006-09-19 10:28:00 -07001710 /*
1711 * Initialize the Host adapter request/response queues and
1712 * firmware
1713 * NOTE: interrupts enabled upon successful completion
1714 */
1715 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301716 while ((!test_bit(AF_ONLINE, &ha->flags)) &&
1717 init_retry_count++ < MAX_INIT_RETRIES) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001718 DEBUG2(printk("scsi: %s: retrying adapter initialization "
1719 "(%d)\n", __func__, init_retry_count));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301720
1721 if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
1722 continue;
1723
David Somayajuluafaf5a22006-09-19 10:28:00 -07001724 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1725 }
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301726
1727 if (!test_bit(AF_ONLINE, &ha->flags)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301728 ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001729
1730 ret = -ENODEV;
1731 goto probe_failed;
1732 }
1733
1734 host->cmd_per_lun = 3;
1735 host->max_channel = 0;
1736 host->max_lun = MAX_LUNS - 1;
1737 host->max_id = MAX_TARGETS;
1738 host->max_cmd_len = IOCB_MAX_CDB_LEN;
1739 host->can_queue = MAX_SRBS ;
1740 host->transportt = qla4xxx_scsi_transport;
1741
1742 ret = scsi_init_shared_tag_map(host, MAX_SRBS);
1743 if (ret) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301744 ql4_printk(KERN_WARNING, ha,
1745 "scsi_init_shared_tag_map failed\n");
1746 goto probe_failed;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001747 }
1748
1749 /* Startup the kernel thread for this host adapter. */
1750 DEBUG2(printk("scsi: %s: Starting kernel thread for "
1751 "qla4xxx_dpc\n", __func__));
1752 sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
1753 ha->dpc_thread = create_singlethread_workqueue(buf);
1754 if (!ha->dpc_thread) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301755 ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001756 ret = -ENODEV;
1757 goto probe_failed;
1758 }
David Howellsc4028952006-11-22 14:57:56 +00001759 INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001760
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301761 /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
1762 * (which is called indirectly by qla4xxx_initialize_adapter),
1763 * so that irqs will be registered after crbinit but before
1764 * mbx_intr_enable.
1765 */
1766 if (!is_qla8022(ha)) {
1767 ret = qla4xxx_request_irqs(ha);
1768 if (ret) {
1769 ql4_printk(KERN_WARNING, ha, "Failed to reserve "
1770 "interrupt %d already in use.\n", pdev->irq);
1771 goto probe_failed;
1772 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001773 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001774
Lalit Chandivade2232be02010-07-30 14:38:47 +05301775 pci_save_state(ha->pdev);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301776 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001777
1778 /* Start timer thread. */
1779 qla4xxx_start_timer(ha, qla4xxx_timer, 1);
1780
1781 set_bit(AF_INIT_DONE, &ha->flags);
1782
1783 pci_set_drvdata(pdev, ha);
1784
1785 ret = scsi_add_host(host, &pdev->dev);
1786 if (ret)
1787 goto probe_failed;
1788
David Somayajuluafaf5a22006-09-19 10:28:00 -07001789 printk(KERN_INFO
1790 " QLogic iSCSI HBA Driver version: %s\n"
1791 " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
1792 qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
1793 ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
1794 ha->patch_number, ha->build_number);
Mike Christie921601b2008-01-31 13:36:49 -06001795 scsi_scan_host(host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001796 return 0;
1797
David Somayajuluafaf5a22006-09-19 10:28:00 -07001798probe_failed:
1799 qla4xxx_free_adapter(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301800
1801probe_failed_ioconfig:
Lalit Chandivade2232be02010-07-30 14:38:47 +05301802 pci_disable_pcie_error_reporting(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001803 scsi_host_put(ha->host);
1804
1805probe_disable_device:
1806 pci_disable_device(pdev);
1807
1808 return ret;
1809}
1810
1811/**
1812 * qla4xxx_remove_adapter - calback function to remove adapter.
1813 * @pci_dev: PCI device pointer
1814 **/
1815static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
1816{
1817 struct scsi_qla_host *ha;
1818
1819 ha = pci_get_drvdata(pdev);
1820
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301821 set_bit(AF_HBA_GOING_AWAY, &ha->flags);
David C Somayajulubee4fe82007-05-23 18:03:32 -07001822
David Somayajuluafaf5a22006-09-19 10:28:00 -07001823 /* remove devs from iscsi_sessions to scsi_devices */
1824 qla4xxx_free_ddb_list(ha);
1825
1826 scsi_remove_host(ha->host);
1827
1828 qla4xxx_free_adapter(ha);
1829
1830 scsi_host_put(ha->host);
1831
Lalit Chandivade2232be02010-07-30 14:38:47 +05301832 pci_disable_pcie_error_reporting(pdev);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301833 pci_disable_device(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001834 pci_set_drvdata(pdev, NULL);
1835}
1836
1837/**
1838 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
1839 * @ha: HA context
1840 *
1841 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
1842 * supported addressing method.
1843 */
Adrian Bunk47975472007-04-26 00:35:16 -07001844static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001845{
1846 int retval;
1847
1848 /* Update our PCI device dma_mask for full 64 bit mask */
Yang Hongyang6a355282009-04-06 19:01:13 -07001849 if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
1850 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001851 dev_dbg(&ha->pdev->dev,
1852 "Failed to set 64 bit PCI consistent mask; "
1853 "using 32 bit.\n");
1854 retval = pci_set_consistent_dma_mask(ha->pdev,
Yang Hongyang284901a2009-04-06 19:01:15 -07001855 DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001856 }
1857 } else
Yang Hongyang284901a2009-04-06 19:01:15 -07001858 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001859}
1860
1861static int qla4xxx_slave_alloc(struct scsi_device *sdev)
1862{
1863 struct iscsi_cls_session *sess = starget_to_session(sdev->sdev_target);
1864 struct ddb_entry *ddb = sess->dd_data;
1865
1866 sdev->hostdata = ddb;
1867 sdev->tagged_supported = 1;
Mike Christied510d962008-07-11 19:50:33 -05001868 scsi_activate_tcq(sdev, QL4_DEF_QDEPTH);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001869 return 0;
1870}
1871
1872static int qla4xxx_slave_configure(struct scsi_device *sdev)
1873{
1874 sdev->tagged_supported = 1;
1875 return 0;
1876}
1877
1878static void qla4xxx_slave_destroy(struct scsi_device *sdev)
1879{
1880 scsi_deactivate_tcq(sdev, 1);
1881}
1882
1883/**
1884 * qla4xxx_del_from_active_array - returns an active srb
1885 * @ha: Pointer to host adapter structure.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001886 * @index: index into the active_array
David Somayajuluafaf5a22006-09-19 10:28:00 -07001887 *
1888 * This routine removes and returns the srb at the specified index
1889 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301890struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
1891 uint32_t index)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001892{
1893 struct srb *srb = NULL;
Vikas Chaudhary53698872010-04-28 11:41:59 +05301894 struct scsi_cmnd *cmd = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001895
Vikas Chaudhary53698872010-04-28 11:41:59 +05301896 cmd = scsi_host_find_tag(ha->host, index);
1897 if (!cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001898 return srb;
1899
Vikas Chaudhary53698872010-04-28 11:41:59 +05301900 srb = (struct srb *)CMD_SP(cmd);
1901 if (!srb)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001902 return srb;
1903
1904 /* update counters */
1905 if (srb->flags & SRB_DMA_VALID) {
1906 ha->req_q_count += srb->iocb_cnt;
1907 ha->iocb_cnt -= srb->iocb_cnt;
1908 if (srb->cmd)
Vikas Chaudhary53698872010-04-28 11:41:59 +05301909 srb->cmd->host_scribble =
1910 (unsigned char *)(unsigned long) MAX_SRBS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001911 }
1912 return srb;
1913}
1914
1915/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07001916 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301917 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001918 * @cmd: Scsi Command to wait on.
1919 *
1920 * This routine waits for the command to be returned by the Firmware
1921 * for some max time.
1922 **/
1923static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
1924 struct scsi_cmnd *cmd)
1925{
1926 int done = 0;
1927 struct srb *rp;
1928 uint32_t max_wait_time = EH_WAIT_CMD_TOV;
Lalit Chandivade2232be02010-07-30 14:38:47 +05301929 int ret = SUCCESS;
1930
1931 /* Dont wait on command if PCI error is being handled
1932 * by PCI AER driver
1933 */
1934 if (unlikely(pci_channel_offline(ha->pdev)) ||
1935 (test_bit(AF_EEH_BUSY, &ha->flags))) {
1936 ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
1937 ha->host_no, __func__);
1938 return ret;
1939 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001940
1941 do {
1942 /* Checking to see if its returned to OS */
Vikas Chaudhary53698872010-04-28 11:41:59 +05301943 rp = (struct srb *) CMD_SP(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001944 if (rp == NULL) {
1945 done++;
1946 break;
1947 }
1948
1949 msleep(2000);
1950 } while (max_wait_time--);
1951
1952 return done;
1953}
1954
1955/**
1956 * qla4xxx_wait_for_hba_online - waits for HBA to come online
1957 * @ha: Pointer to host adapter structure
1958 **/
1959static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
1960{
1961 unsigned long wait_online;
1962
1963 wait_online = jiffies + (30 * HZ);
1964 while (time_before(jiffies, wait_online)) {
1965
1966 if (adapter_up(ha))
1967 return QLA_SUCCESS;
1968 else if (ha->retry_reset_ha_cnt == 0)
1969 return QLA_ERROR;
1970
1971 msleep(2000);
1972 }
1973
1974 return QLA_ERROR;
1975}
1976
1977/**
Mike Christiece545032008-02-29 18:25:20 -06001978 * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001979 * @ha: pointer to HBA
David Somayajuluafaf5a22006-09-19 10:28:00 -07001980 * @t: target id
1981 * @l: lun id
1982 *
1983 * This function waits for all outstanding commands to a lun to complete. It
1984 * returns 0 if all pending commands are returned and 1 otherwise.
1985 **/
Mike Christiece545032008-02-29 18:25:20 -06001986static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
1987 struct scsi_target *stgt,
1988 struct scsi_device *sdev)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001989{
1990 int cnt;
1991 int status = 0;
1992 struct scsi_cmnd *cmd;
1993
1994 /*
Mike Christiece545032008-02-29 18:25:20 -06001995 * Waiting for all commands for the designated target or dev
1996 * in the active array
David Somayajuluafaf5a22006-09-19 10:28:00 -07001997 */
1998 for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
1999 cmd = scsi_host_find_tag(ha->host, cnt);
Mike Christiece545032008-02-29 18:25:20 -06002000 if (cmd && stgt == scsi_target(cmd->device) &&
2001 (!sdev || sdev == cmd->device)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002002 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
2003 status++;
2004 break;
2005 }
2006 }
2007 }
2008 return status;
2009}
2010
2011/**
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302012 * qla4xxx_eh_abort - callback for abort task.
2013 * @cmd: Pointer to Linux's SCSI command structure
2014 *
2015 * This routine is called by the Linux OS to abort the specified
2016 * command.
2017 **/
2018static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
2019{
2020 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2021 unsigned int id = cmd->device->id;
2022 unsigned int lun = cmd->device->lun;
2023 unsigned long serial = cmd->serial_number;
2024 struct srb *srb = NULL;
2025 int ret = SUCCESS;
2026 int wait = 0;
2027
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302028 ql4_printk(KERN_INFO, ha,
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302029 "scsi%ld:%d:%d: Abort command issued cmd=%p, pid=%ld\n",
2030 ha->host_no, id, lun, cmd, serial);
2031
2032 srb = (struct srb *) CMD_SP(cmd);
2033
2034 if (!srb)
2035 return SUCCESS;
2036
2037 kref_get(&srb->srb_ref);
2038
2039 if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
2040 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
2041 ha->host_no, id, lun));
2042 ret = FAILED;
2043 } else {
2044 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
2045 ha->host_no, id, lun));
2046 wait = 1;
2047 }
2048
2049 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
2050
2051 /* Wait for command to complete */
2052 if (wait) {
2053 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
2054 DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
2055 ha->host_no, id, lun));
2056 ret = FAILED;
2057 }
2058 }
2059
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302060 ql4_printk(KERN_INFO, ha,
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302061 "scsi%ld:%d:%d: Abort command - %s\n",
2062 ha->host_no, id, lun, (ret == SUCCESS) ? "succeded" : "failed");
2063
2064 return ret;
2065}
2066
2067/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002068 * qla4xxx_eh_device_reset - callback for target reset.
2069 * @cmd: Pointer to Linux's SCSI command structure
2070 *
2071 * This routine is called by the Linux OS to reset all luns on the
2072 * specified target.
2073 **/
2074static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
2075{
2076 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2077 struct ddb_entry *ddb_entry = cmd->device->hostdata;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002078 int ret = FAILED, stat;
2079
Karen Higgins612f7342009-07-15 15:03:01 -05002080 if (!ddb_entry)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002081 return ret;
2082
Mike Christiec01be6d2010-07-22 16:59:49 +05302083 ret = iscsi_block_scsi_eh(cmd);
2084 if (ret)
2085 return ret;
2086 ret = FAILED;
2087
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302088 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002089 "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
2090 cmd->device->channel, cmd->device->id, cmd->device->lun);
2091
2092 DEBUG2(printk(KERN_INFO
2093 "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
2094 "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
Jens Axboe242f9dc2008-09-14 05:55:09 -07002095 cmd, jiffies, cmd->request->timeout / HZ,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002096 ha->dpc_flags, cmd->result, cmd->allowed));
2097
2098 /* FIXME: wait for hba to go online */
2099 stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
2100 if (stat != QLA_SUCCESS) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302101 ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002102 goto eh_dev_reset_done;
2103 }
2104
Mike Christiece545032008-02-29 18:25:20 -06002105 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2106 cmd->device)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302107 ql4_printk(KERN_INFO, ha,
Mike Christiece545032008-02-29 18:25:20 -06002108 "DEVICE RESET FAILED - waiting for "
2109 "commands.\n");
2110 goto eh_dev_reset_done;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002111 }
2112
David C Somayajulu9d562912008-03-19 11:23:03 -07002113 /* Send marker. */
2114 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2115 MM_LUN_RESET) != QLA_SUCCESS)
2116 goto eh_dev_reset_done;
2117
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302118 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002119 "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
2120 ha->host_no, cmd->device->channel, cmd->device->id,
2121 cmd->device->lun);
2122
2123 ret = SUCCESS;
2124
2125eh_dev_reset_done:
2126
2127 return ret;
2128}
2129
2130/**
Mike Christiece545032008-02-29 18:25:20 -06002131 * qla4xxx_eh_target_reset - callback for target reset.
2132 * @cmd: Pointer to Linux's SCSI command structure
2133 *
2134 * This routine is called by the Linux OS to reset the target.
2135 **/
2136static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
2137{
2138 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2139 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christiec01be6d2010-07-22 16:59:49 +05302140 int stat, ret;
Mike Christiece545032008-02-29 18:25:20 -06002141
2142 if (!ddb_entry)
2143 return FAILED;
2144
Mike Christiec01be6d2010-07-22 16:59:49 +05302145 ret = iscsi_block_scsi_eh(cmd);
2146 if (ret)
2147 return ret;
2148
Mike Christiece545032008-02-29 18:25:20 -06002149 starget_printk(KERN_INFO, scsi_target(cmd->device),
2150 "WARM TARGET RESET ISSUED.\n");
2151
2152 DEBUG2(printk(KERN_INFO
2153 "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
2154 "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
Jens Axboe242f9dc2008-09-14 05:55:09 -07002155 ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
Mike Christiece545032008-02-29 18:25:20 -06002156 ha->dpc_flags, cmd->result, cmd->allowed));
2157
2158 stat = qla4xxx_reset_target(ha, ddb_entry);
2159 if (stat != QLA_SUCCESS) {
2160 starget_printk(KERN_INFO, scsi_target(cmd->device),
2161 "WARM TARGET RESET FAILED.\n");
2162 return FAILED;
2163 }
2164
Mike Christiece545032008-02-29 18:25:20 -06002165 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2166 NULL)) {
2167 starget_printk(KERN_INFO, scsi_target(cmd->device),
2168 "WARM TARGET DEVICE RESET FAILED - "
2169 "waiting for commands.\n");
2170 return FAILED;
2171 }
2172
David C Somayajulu9d562912008-03-19 11:23:03 -07002173 /* Send marker. */
2174 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2175 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
2176 starget_printk(KERN_INFO, scsi_target(cmd->device),
2177 "WARM TARGET DEVICE RESET FAILED - "
2178 "marker iocb failed.\n");
2179 return FAILED;
2180 }
2181
Mike Christiece545032008-02-29 18:25:20 -06002182 starget_printk(KERN_INFO, scsi_target(cmd->device),
2183 "WARM TARGET RESET SUCCEEDED.\n");
2184 return SUCCESS;
2185}
2186
2187/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002188 * qla4xxx_eh_host_reset - kernel callback
2189 * @cmd: Pointer to Linux's SCSI command structure
2190 *
2191 * This routine is invoked by the Linux kernel to perform fatal error
2192 * recovery on the specified adapter.
2193 **/
2194static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
2195{
2196 int return_status = FAILED;
2197 struct scsi_qla_host *ha;
2198
2199 ha = (struct scsi_qla_host *) cmd->device->host->hostdata;
2200
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302201 if (ql4xdontresethba) {
2202 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
2203 ha->host_no, __func__));
2204 return FAILED;
2205 }
2206
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302207 ql4_printk(KERN_INFO, ha,
Karen Higginsdca05c42009-07-15 15:03:00 -05002208 "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002209 cmd->device->channel, cmd->device->id, cmd->device->lun);
2210
2211 if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
2212 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
2213 "DEAD.\n", ha->host_no, cmd->device->channel,
2214 __func__));
2215
2216 return FAILED;
2217 }
2218
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302219 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
2220 if (is_qla8022(ha))
2221 set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2222 else
2223 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2224 }
Mike Christie50a29ae2008-03-04 13:26:53 -06002225
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302226 if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002227 return_status = SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002228
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302229 ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
David Somayajuluafaf5a22006-09-19 10:28:00 -07002230 return_status == FAILED ? "FAILED" : "SUCCEDED");
2231
2232 return return_status;
2233}
2234
Lalit Chandivade2232be02010-07-30 14:38:47 +05302235/* PCI AER driver recovers from all correctable errors w/o
2236 * driver intervention. For uncorrectable errors PCI AER
2237 * driver calls the following device driver's callbacks
2238 *
2239 * - Fatal Errors - link_reset
2240 * - Non-Fatal Errors - driver's pci_error_detected() which
2241 * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
2242 *
2243 * PCI AER driver calls
2244 * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
2245 * returns RECOVERED or NEED_RESET if fw_hung
2246 * NEED_RESET - driver's slot_reset()
2247 * DISCONNECT - device is dead & cannot recover
2248 * RECOVERED - driver's pci_resume()
2249 */
2250static pci_ers_result_t
2251qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
2252{
2253 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2254
2255 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
2256 ha->host_no, __func__, state);
2257
2258 if (!is_aer_supported(ha))
2259 return PCI_ERS_RESULT_NONE;
2260
2261 switch (state) {
2262 case pci_channel_io_normal:
2263 clear_bit(AF_EEH_BUSY, &ha->flags);
2264 return PCI_ERS_RESULT_CAN_RECOVER;
2265 case pci_channel_io_frozen:
2266 set_bit(AF_EEH_BUSY, &ha->flags);
2267 qla4xxx_mailbox_premature_completion(ha);
2268 qla4xxx_free_irqs(ha);
2269 pci_disable_device(pdev);
2270 return PCI_ERS_RESULT_NEED_RESET;
2271 case pci_channel_io_perm_failure:
2272 set_bit(AF_EEH_BUSY, &ha->flags);
2273 set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
2274 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
2275 return PCI_ERS_RESULT_DISCONNECT;
2276 }
2277 return PCI_ERS_RESULT_NEED_RESET;
2278}
2279
2280/**
2281 * qla4xxx_pci_mmio_enabled() gets called if
2282 * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
2283 * and read/write to the device still works.
2284 **/
2285static pci_ers_result_t
2286qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
2287{
2288 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2289
2290 if (!is_aer_supported(ha))
2291 return PCI_ERS_RESULT_NONE;
2292
2293 if (test_bit(AF_FW_RECOVERY, &ha->flags)) {
2294 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: firmware hang -- "
2295 "mmio_enabled\n", ha->host_no, __func__);
2296 return PCI_ERS_RESULT_NEED_RESET;
2297 } else
2298 return PCI_ERS_RESULT_RECOVERED;
2299}
2300
2301uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
2302{
2303 uint32_t rval = QLA_ERROR;
2304 int fn;
2305 struct pci_dev *other_pdev = NULL;
2306
2307 ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
2308
2309 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2310
2311 if (test_bit(AF_ONLINE, &ha->flags)) {
2312 clear_bit(AF_ONLINE, &ha->flags);
2313 qla4xxx_mark_all_devices_missing(ha);
2314 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2315 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
2316 }
2317
2318 fn = PCI_FUNC(ha->pdev->devfn);
2319 while (fn > 0) {
2320 fn--;
2321 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
2322 "func %x\n", ha->host_no, __func__, fn);
2323 /* Get the pci device given the domain, bus,
2324 * slot/function number */
2325 other_pdev =
2326 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
2327 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
2328 fn));
2329
2330 if (!other_pdev)
2331 continue;
2332
2333 if (atomic_read(&other_pdev->enable_cnt)) {
2334 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
2335 "func in enabled state%x\n", ha->host_no,
2336 __func__, fn);
2337 pci_dev_put(other_pdev);
2338 break;
2339 }
2340 pci_dev_put(other_pdev);
2341 }
2342
2343 /* The first function on the card, the reset owner will
2344 * start & initialize the firmware. The other functions
2345 * on the card will reset the firmware context
2346 */
2347 if (!fn) {
2348 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
2349 "0x%x is the owner\n", ha->host_no, __func__,
2350 ha->pdev->devfn);
2351
2352 qla4_8xxx_idc_lock(ha);
2353 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2354 QLA82XX_DEV_COLD);
2355
2356 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
2357 QLA82XX_IDC_VERSION);
2358
2359 qla4_8xxx_idc_unlock(ha);
2360 clear_bit(AF_FW_RECOVERY, &ha->flags);
2361 rval = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
2362 qla4_8xxx_idc_lock(ha);
2363
2364 if (rval != QLA_SUCCESS) {
2365 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
2366 "FAILED\n", ha->host_no, __func__);
2367 qla4_8xxx_clear_drv_active(ha);
2368 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2369 QLA82XX_DEV_FAILED);
2370 } else {
2371 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
2372 "READY\n", ha->host_no, __func__);
2373 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2374 QLA82XX_DEV_READY);
2375 /* Clear driver state register */
2376 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
2377 qla4_8xxx_set_drv_active(ha);
2378 ha->isp_ops->enable_intrs(ha);
2379 }
2380 qla4_8xxx_idc_unlock(ha);
2381 } else {
2382 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
2383 "the reset owner\n", ha->host_no, __func__,
2384 ha->pdev->devfn);
2385 if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
2386 QLA82XX_DEV_READY)) {
2387 clear_bit(AF_FW_RECOVERY, &ha->flags);
2388 rval = qla4xxx_initialize_adapter(ha,
2389 PRESERVE_DDB_LIST);
2390 if (rval == QLA_SUCCESS)
2391 ha->isp_ops->enable_intrs(ha);
2392 qla4_8xxx_idc_lock(ha);
2393 qla4_8xxx_set_drv_active(ha);
2394 qla4_8xxx_idc_unlock(ha);
2395 }
2396 }
2397 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2398 return rval;
2399}
2400
2401static pci_ers_result_t
2402qla4xxx_pci_slot_reset(struct pci_dev *pdev)
2403{
2404 pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
2405 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2406 int rc;
2407
2408 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
2409 ha->host_no, __func__);
2410
2411 if (!is_aer_supported(ha))
2412 return PCI_ERS_RESULT_NONE;
2413
2414 /* Restore the saved state of PCIe device -
2415 * BAR registers, PCI Config space, PCIX, MSI,
2416 * IOV states
2417 */
2418 pci_restore_state(pdev);
2419
2420 /* pci_restore_state() clears the saved_state flag of the device
2421 * save restored state which resets saved_state flag
2422 */
2423 pci_save_state(pdev);
2424
2425 /* Initialize device or resume if in suspended state */
2426 rc = pci_enable_device(pdev);
2427 if (rc) {
2428 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Cant re-enable "
2429 "device after reset\n", ha->host_no, __func__);
2430 goto exit_slot_reset;
2431 }
2432
2433 ret = qla4xxx_request_irqs(ha);
2434 if (ret) {
2435 ql4_printk(KERN_WARNING, ha, "Failed to reserve interrupt %d"
2436 " already in use.\n", pdev->irq);
2437 goto exit_slot_reset;
2438 }
2439
2440 if (is_qla8022(ha)) {
2441 if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
2442 ret = PCI_ERS_RESULT_RECOVERED;
2443 goto exit_slot_reset;
2444 } else
2445 goto exit_slot_reset;
2446 }
2447
2448exit_slot_reset:
2449 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
2450 "device after reset\n", ha->host_no, __func__, ret);
2451 return ret;
2452}
2453
2454static void
2455qla4xxx_pci_resume(struct pci_dev *pdev)
2456{
2457 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2458 int ret;
2459
2460 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
2461 ha->host_no, __func__);
2462
2463 ret = qla4xxx_wait_for_hba_online(ha);
2464 if (ret != QLA_SUCCESS) {
2465 ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
2466 "resume I/O from slot/link_reset\n", ha->host_no,
2467 __func__);
2468 }
2469
2470 pci_cleanup_aer_uncorrect_error_status(pdev);
2471 clear_bit(AF_EEH_BUSY, &ha->flags);
2472}
2473
2474static struct pci_error_handlers qla4xxx_err_handler = {
2475 .error_detected = qla4xxx_pci_error_detected,
2476 .mmio_enabled = qla4xxx_pci_mmio_enabled,
2477 .slot_reset = qla4xxx_pci_slot_reset,
2478 .resume = qla4xxx_pci_resume,
2479};
2480
David Somayajuluafaf5a22006-09-19 10:28:00 -07002481static struct pci_device_id qla4xxx_pci_tbl[] = {
2482 {
2483 .vendor = PCI_VENDOR_ID_QLOGIC,
2484 .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
2485 .subvendor = PCI_ANY_ID,
2486 .subdevice = PCI_ANY_ID,
2487 },
2488 {
2489 .vendor = PCI_VENDOR_ID_QLOGIC,
2490 .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
2491 .subvendor = PCI_ANY_ID,
2492 .subdevice = PCI_ANY_ID,
2493 },
David C Somayajulud9150582006-11-15 17:38:40 -08002494 {
2495 .vendor = PCI_VENDOR_ID_QLOGIC,
2496 .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
2497 .subvendor = PCI_ANY_ID,
2498 .subdevice = PCI_ANY_ID,
2499 },
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302500 {
2501 .vendor = PCI_VENDOR_ID_QLOGIC,
2502 .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
2503 .subvendor = PCI_ANY_ID,
2504 .subdevice = PCI_ANY_ID,
2505 },
David Somayajuluafaf5a22006-09-19 10:28:00 -07002506 {0, 0},
2507};
2508MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
2509
Adrian Bunk47975472007-04-26 00:35:16 -07002510static struct pci_driver qla4xxx_pci_driver = {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002511 .name = DRIVER_NAME,
2512 .id_table = qla4xxx_pci_tbl,
2513 .probe = qla4xxx_probe_adapter,
2514 .remove = qla4xxx_remove_adapter,
Lalit Chandivade2232be02010-07-30 14:38:47 +05302515 .err_handler = &qla4xxx_err_handler,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002516};
2517
2518static int __init qla4xxx_module_init(void)
2519{
2520 int ret;
2521
2522 /* Allocate cache for SRBs. */
2523 srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09002524 SLAB_HWCACHE_ALIGN, NULL);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002525 if (srb_cachep == NULL) {
2526 printk(KERN_ERR
2527 "%s: Unable to allocate SRB cache..."
2528 "Failing load!\n", DRIVER_NAME);
2529 ret = -ENOMEM;
2530 goto no_srp_cache;
2531 }
2532
2533 /* Derive version string. */
2534 strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
Andrew Vasquez11010fe2006-10-06 09:54:59 -07002535 if (ql4xextended_error_logging)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002536 strcat(qla4xxx_version_str, "-debug");
2537
2538 qla4xxx_scsi_transport =
2539 iscsi_register_transport(&qla4xxx_iscsi_transport);
2540 if (!qla4xxx_scsi_transport){
2541 ret = -ENODEV;
2542 goto release_srb_cache;
2543 }
2544
David Somayajuluafaf5a22006-09-19 10:28:00 -07002545 ret = pci_register_driver(&qla4xxx_pci_driver);
2546 if (ret)
2547 goto unregister_transport;
2548
2549 printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
2550 return 0;
Doug Maxey5ae16db2006-10-05 23:50:07 -05002551
David Somayajuluafaf5a22006-09-19 10:28:00 -07002552unregister_transport:
2553 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
2554release_srb_cache:
2555 kmem_cache_destroy(srb_cachep);
2556no_srp_cache:
2557 return ret;
2558}
2559
2560static void __exit qla4xxx_module_exit(void)
2561{
David Somayajuluafaf5a22006-09-19 10:28:00 -07002562 pci_unregister_driver(&qla4xxx_pci_driver);
2563 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
2564 kmem_cache_destroy(srb_cachep);
2565}
2566
2567module_init(qla4xxx_module_init);
2568module_exit(qla4xxx_module_exit);
2569
2570MODULE_AUTHOR("QLogic Corporation");
2571MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
2572MODULE_LICENSE("GPL");
2573MODULE_VERSION(QLA4XXX_DRIVER_VERSION);