blob: 9c1c8453d6c726942d06cc5fcce93efc22313444 [file] [log] [blame]
David Somayajuluafaf5a22006-09-19 10:28:00 -07001/*
2 * QLogic iSCSI HBA Driver
Vikas Chaudhary7d01d062010-12-02 22:12:51 -08003 * Copyright (c) 2003-2010 QLogic Corporation
David Somayajuluafaf5a22006-09-19 10:28:00 -07004 *
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 */
David Somayajuluafaf5a22006-09-19 10:28:00 -070032int ql4xdontresethba = 0;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053033module_param(ql4xdontresethba, int, S_IRUGO | S_IWUSR);
David Somayajuluafaf5a22006-09-19 10:28:00 -070034MODULE_PARM_DESC(ql4xdontresethba,
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053035 "Don't reset the HBA for driver recovery \n"
36 " 0 - It will reset HBA (Default)\n"
37 " 1 - It will NOT reset HBA");
David Somayajuluafaf5a22006-09-19 10:28:00 -070038
Andrew Vasquez11010fe2006-10-06 09:54:59 -070039int ql4xextended_error_logging = 0; /* 0 = off, 1 = log errors */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053040module_param(ql4xextended_error_logging, int, S_IRUGO | S_IWUSR);
Andrew Vasquez11010fe2006-10-06 09:54:59 -070041MODULE_PARM_DESC(ql4xextended_error_logging,
David Somayajuluafaf5a22006-09-19 10:28:00 -070042 "Option to enable extended error logging, "
43 "Default is 0 - no logging, 1 - debug logging");
44
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053045int ql4xenablemsix = 1;
46module_param(ql4xenablemsix, int, S_IRUGO|S_IWUSR);
47MODULE_PARM_DESC(ql4xenablemsix,
48 "Set to enable MSI or MSI-X interrupt mechanism.\n"
49 " 0 = enable INTx interrupt mechanism.\n"
50 " 1 = enable MSI-X interrupt mechanism (Default).\n"
51 " 2 = enable MSI interrupt mechanism.");
David C Somayajulu477ffb92007-01-22 12:26:11 -080052
Mike Christied510d962008-07-11 19:50:33 -050053#define QL4_DEF_QDEPTH 32
Vikas Chaudhary8bb40332011-03-21 03:34:31 -070054static int ql4xmaxqdepth = QL4_DEF_QDEPTH;
55module_param(ql4xmaxqdepth, int, S_IRUGO | S_IWUSR);
56MODULE_PARM_DESC(ql4xmaxqdepth,
57 "Maximum queue depth to report for target devices.\n"
58 " Default: 32.");
Mike Christied510d962008-07-11 19:50:33 -050059
Vikas Chaudhary30387272011-03-21 03:34:32 -070060static int ql4xsess_recovery_tmo = QL4_SESS_RECOVERY_TMO;
61module_param(ql4xsess_recovery_tmo, int, S_IRUGO);
62MODULE_PARM_DESC(ql4xsess_recovery_tmo,
63 "Target Session Recovery Timeout.\n"
64 " Default: 30 sec.");
65
David Somayajuluafaf5a22006-09-19 10:28:00 -070066/*
67 * SCSI host template entry points
68 */
Adrian Bunk47975472007-04-26 00:35:16 -070069static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -070070
71/*
72 * iSCSI template entry points
73 */
Mike Christie2174a042007-05-30 12:57:10 -050074static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
75 enum iscsi_tgt_dscvr type, uint32_t enable,
76 struct sockaddr *dst_addr);
David Somayajuluafaf5a22006-09-19 10:28:00 -070077static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
78 enum iscsi_param param, char *buf);
79static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
80 enum iscsi_param param, char *buf);
Mike Christieaa1e93a2007-05-30 12:57:09 -050081static int qla4xxx_host_get_param(struct Scsi_Host *shost,
82 enum iscsi_host_param param, char *buf);
David Somayajuluafaf5a22006-09-19 10:28:00 -070083static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session);
Mike Christie5c656af2009-07-15 15:02:59 -050084static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
David Somayajuluafaf5a22006-09-19 10:28:00 -070085
86/*
87 * SCSI host template entry points
88 */
Jeff Garzikf2812332010-11-16 02:10:29 -050089static int qla4xxx_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmd);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +053090static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -070091static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
Mike Christiece545032008-02-29 18:25:20 -060092static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -070093static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
94static int qla4xxx_slave_alloc(struct scsi_device *device);
95static int qla4xxx_slave_configure(struct scsi_device *device);
96static void qla4xxx_slave_destroy(struct scsi_device *sdev);
Mike Christie024f8012008-03-04 13:26:54 -060097static void qla4xxx_scan_start(struct Scsi_Host *shost);
David Somayajuluafaf5a22006-09-19 10:28:00 -070098
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053099static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
100 QLA82XX_LEGACY_INTR_CONFIG;
101
David Somayajuluafaf5a22006-09-19 10:28:00 -0700102static struct scsi_host_template qla4xxx_driver_template = {
103 .module = THIS_MODULE,
104 .name = DRIVER_NAME,
105 .proc_name = DRIVER_NAME,
106 .queuecommand = qla4xxx_queuecommand,
107
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530108 .eh_abort_handler = qla4xxx_eh_abort,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700109 .eh_device_reset_handler = qla4xxx_eh_device_reset,
Mike Christiece545032008-02-29 18:25:20 -0600110 .eh_target_reset_handler = qla4xxx_eh_target_reset,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700111 .eh_host_reset_handler = qla4xxx_eh_host_reset,
Mike Christie5c656af2009-07-15 15:02:59 -0500112 .eh_timed_out = qla4xxx_eh_cmd_timed_out,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700113
114 .slave_configure = qla4xxx_slave_configure,
115 .slave_alloc = qla4xxx_slave_alloc,
116 .slave_destroy = qla4xxx_slave_destroy,
117
Mike Christie921601b2008-01-31 13:36:49 -0600118 .scan_finished = iscsi_scan_finished,
Mike Christie024f8012008-03-04 13:26:54 -0600119 .scan_start = qla4xxx_scan_start,
Mike Christie921601b2008-01-31 13:36:49 -0600120
David Somayajuluafaf5a22006-09-19 10:28:00 -0700121 .this_id = -1,
122 .cmd_per_lun = 3,
123 .use_clustering = ENABLE_CLUSTERING,
124 .sg_tablesize = SG_ALL,
125
126 .max_sectors = 0xFFFF,
127};
128
129static struct iscsi_transport qla4xxx_iscsi_transport = {
130 .owner = THIS_MODULE,
131 .name = DRIVER_NAME,
Mike Christied8196ed2007-05-30 12:57:25 -0500132 .caps = CAP_FW_DB | CAP_SENDTARGETS_OFFLOAD |
133 CAP_DATA_PATH_OFFLOAD,
Mike Christieaa1e93a2007-05-30 12:57:09 -0500134 .param_mask = ISCSI_CONN_PORT | ISCSI_CONN_ADDRESS |
Vikas Chaudhary3b2bef12010-07-10 14:51:30 +0530135 ISCSI_TARGET_NAME | ISCSI_TPGT |
136 ISCSI_TARGET_ALIAS,
Mike Christie8ad57812007-05-30 12:57:13 -0500137 .host_param_mask = ISCSI_HOST_HWADDRESS |
Mike Christie22236962007-05-30 12:57:24 -0500138 ISCSI_HOST_IPADDRESS |
Mike Christie8ad57812007-05-30 12:57:13 -0500139 ISCSI_HOST_INITIATOR_NAME,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700140 .tgt_dscvr = qla4xxx_tgt_dscvr,
141 .get_conn_param = qla4xxx_conn_get_param,
142 .get_session_param = qla4xxx_sess_get_param,
Mike Christieaa1e93a2007-05-30 12:57:09 -0500143 .get_host_param = qla4xxx_host_get_param,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700144 .session_recovery_timedout = qla4xxx_recovery_timedout,
145};
146
147static struct scsi_transport_template *qla4xxx_scsi_transport;
148
Mike Christie5c656af2009-07-15 15:02:59 -0500149static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
150{
151 struct iscsi_cls_session *session;
152 struct ddb_entry *ddb_entry;
153
154 session = starget_to_session(scsi_target(sc->device));
155 ddb_entry = session->dd_data;
156
157 /* if we are not logged in then the LLD is going to clean up the cmd */
158 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
159 return BLK_EH_RESET_TIMER;
160 else
161 return BLK_EH_NOT_HANDLED;
162}
163
David Somayajuluafaf5a22006-09-19 10:28:00 -0700164static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session)
165{
166 struct ddb_entry *ddb_entry = session->dd_data;
167 struct scsi_qla_host *ha = ddb_entry->ha;
168
Mike Christie568d3032008-01-31 13:36:47 -0600169 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
170 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700171
Vikas Chaudhary3013cea2010-07-30 14:25:46 +0530172 DEBUG2(printk("scsi%ld: %s: ddb [%d] session recovery timeout "
Mike Christie568d3032008-01-31 13:36:47 -0600173 "of (%d) secs exhausted, marking device DEAD.\n",
174 ha->host_no, __func__, ddb_entry->fw_ddb_index,
Vikas Chaudhary30387272011-03-21 03:34:32 -0700175 ddb_entry->sess->recovery_tmo));
Mike Christie568d3032008-01-31 13:36:47 -0600176 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700177}
178
Mike Christieaa1e93a2007-05-30 12:57:09 -0500179static int qla4xxx_host_get_param(struct Scsi_Host *shost,
180 enum iscsi_host_param param, char *buf)
181{
182 struct scsi_qla_host *ha = to_qla_host(shost);
183 int len;
184
185 switch (param) {
186 case ISCSI_HOST_PARAM_HWADDRESS:
Michael Chan7ffc49a2007-12-24 21:28:09 -0800187 len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
Mike Christieaa1e93a2007-05-30 12:57:09 -0500188 break;
Mike Christie22236962007-05-30 12:57:24 -0500189 case ISCSI_HOST_PARAM_IPADDRESS:
190 len = sprintf(buf, "%d.%d.%d.%d\n", ha->ip_address[0],
191 ha->ip_address[1], ha->ip_address[2],
192 ha->ip_address[3]);
193 break;
Mike Christie8ad57812007-05-30 12:57:13 -0500194 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie22236962007-05-30 12:57:24 -0500195 len = sprintf(buf, "%s\n", ha->name_string);
Mike Christie8ad57812007-05-30 12:57:13 -0500196 break;
Mike Christieaa1e93a2007-05-30 12:57:09 -0500197 default:
198 return -ENOSYS;
199 }
200
201 return len;
202}
203
David Somayajuluafaf5a22006-09-19 10:28:00 -0700204static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
205 enum iscsi_param param, char *buf)
206{
207 struct ddb_entry *ddb_entry = sess->dd_data;
208 int len;
209
210 switch (param) {
211 case ISCSI_PARAM_TARGET_NAME:
212 len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
213 ddb_entry->iscsi_name);
214 break;
215 case ISCSI_PARAM_TPGT:
216 len = sprintf(buf, "%u\n", ddb_entry->tpgt);
217 break;
Vikas Chaudhary3b2bef12010-07-10 14:51:30 +0530218 case ISCSI_PARAM_TARGET_ALIAS:
219 len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
220 ddb_entry->iscsi_alias);
221 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700222 default:
223 return -ENOSYS;
224 }
225
226 return len;
227}
228
229static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
230 enum iscsi_param param, char *buf)
231{
232 struct iscsi_cls_session *session;
233 struct ddb_entry *ddb_entry;
234 int len;
235
236 session = iscsi_dev_to_session(conn->dev.parent);
237 ddb_entry = session->dd_data;
238
239 switch (param) {
240 case ISCSI_PARAM_CONN_PORT:
241 len = sprintf(buf, "%hu\n", ddb_entry->port);
242 break;
243 case ISCSI_PARAM_CONN_ADDRESS:
244 /* TODO: what are the ipv6 bits */
Harvey Harrison63779432008-10-31 00:56:00 -0700245 len = sprintf(buf, "%pI4\n", &ddb_entry->ip_addr);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700246 break;
247 default:
248 return -ENOSYS;
249 }
250
251 return len;
252}
253
Mike Christie2174a042007-05-30 12:57:10 -0500254static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
255 enum iscsi_tgt_dscvr type, uint32_t enable,
256 struct sockaddr *dst_addr)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700257{
258 struct scsi_qla_host *ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700259 struct sockaddr_in *addr;
260 struct sockaddr_in6 *addr6;
261 int ret = 0;
262
David Somayajuluafaf5a22006-09-19 10:28:00 -0700263 ha = (struct scsi_qla_host *) shost->hostdata;
264
265 switch (type) {
266 case ISCSI_TGT_DSCVR_SEND_TARGETS:
267 if (dst_addr->sa_family == AF_INET) {
268 addr = (struct sockaddr_in *)dst_addr;
269 if (qla4xxx_send_tgts(ha, (char *)&addr->sin_addr,
270 addr->sin_port) != QLA_SUCCESS)
271 ret = -EIO;
272 } else if (dst_addr->sa_family == AF_INET6) {
273 /*
274 * TODO: fix qla4xxx_send_tgts
275 */
276 addr6 = (struct sockaddr_in6 *)dst_addr;
277 if (qla4xxx_send_tgts(ha, (char *)&addr6->sin6_addr,
278 addr6->sin6_port) != QLA_SUCCESS)
279 ret = -EIO;
280 } else
281 ret = -ENOSYS;
282 break;
283 default:
284 ret = -ENOSYS;
285 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700286 return ret;
287}
288
289void qla4xxx_destroy_sess(struct ddb_entry *ddb_entry)
290{
291 if (!ddb_entry->sess)
292 return;
293
294 if (ddb_entry->conn) {
Mike Christie26974782007-12-13 12:43:29 -0600295 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700296 iscsi_remove_session(ddb_entry->sess);
297 }
298 iscsi_free_session(ddb_entry->sess);
299}
300
301int qla4xxx_add_sess(struct ddb_entry *ddb_entry)
302{
303 int err;
304
Vikas Chaudhary30387272011-03-21 03:34:32 -0700305 ddb_entry->sess->recovery_tmo = ql4xsess_recovery_tmo;
Vikas Chaudhary3013cea2010-07-30 14:25:46 +0530306
David Somayajuluafaf5a22006-09-19 10:28:00 -0700307 err = iscsi_add_session(ddb_entry->sess, ddb_entry->fw_ddb_index);
308 if (err) {
309 DEBUG2(printk(KERN_ERR "Could not add session.\n"));
310 return err;
311 }
312
Mike Christie5d91e202008-05-21 15:54:01 -0500313 ddb_entry->conn = iscsi_create_conn(ddb_entry->sess, 0, 0);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700314 if (!ddb_entry->conn) {
315 iscsi_remove_session(ddb_entry->sess);
316 DEBUG2(printk(KERN_ERR "Could not add connection.\n"));
317 return -ENOMEM;
318 }
Mike Christieb6359302008-01-31 13:36:44 -0600319
320 /* finally ready to go */
321 iscsi_unblock_session(ddb_entry->sess);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700322 return 0;
323}
324
325struct ddb_entry *qla4xxx_alloc_sess(struct scsi_qla_host *ha)
326{
327 struct ddb_entry *ddb_entry;
328 struct iscsi_cls_session *sess;
329
Mike Christie5d91e202008-05-21 15:54:01 -0500330 sess = iscsi_alloc_session(ha->host, &qla4xxx_iscsi_transport,
331 sizeof(struct ddb_entry));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700332 if (!sess)
333 return NULL;
334
335 ddb_entry = sess->dd_data;
336 memset(ddb_entry, 0, sizeof(*ddb_entry));
337 ddb_entry->ha = ha;
338 ddb_entry->sess = sess;
339 return ddb_entry;
340}
341
Mike Christie024f8012008-03-04 13:26:54 -0600342static void qla4xxx_scan_start(struct Scsi_Host *shost)
343{
344 struct scsi_qla_host *ha = shost_priv(shost);
345 struct ddb_entry *ddb_entry, *ddbtemp;
346
347 /* finish setup of sessions that were already setup in firmware */
348 list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
349 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE)
350 qla4xxx_add_sess(ddb_entry);
351 }
352}
353
David Somayajuluafaf5a22006-09-19 10:28:00 -0700354/*
355 * Timer routines
356 */
357
358static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
359 unsigned long interval)
360{
361 DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
362 __func__, ha->host->host_no));
363 init_timer(&ha->timer);
364 ha->timer.expires = jiffies + interval * HZ;
365 ha->timer.data = (unsigned long)ha;
366 ha->timer.function = (void (*)(unsigned long))func;
367 add_timer(&ha->timer);
368 ha->timer_active = 1;
369}
370
371static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
372{
373 del_timer_sync(&ha->timer);
374 ha->timer_active = 0;
375}
376
377/***
378 * qla4xxx_mark_device_missing - mark a device as missing.
379 * @ha: Pointer to host adapter structure.
380 * @ddb_entry: Pointer to device database entry
381 *
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530382 * This routine marks a device missing and close connection.
David Somayajuluafaf5a22006-09-19 10:28:00 -0700383 **/
384void qla4xxx_mark_device_missing(struct scsi_qla_host *ha,
385 struct ddb_entry *ddb_entry)
386{
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530387 if ((atomic_read(&ddb_entry->state) != DDB_STATE_DEAD)) {
388 atomic_set(&ddb_entry->state, DDB_STATE_MISSING);
389 DEBUG2(printk("scsi%ld: ddb [%d] marked MISSING\n",
390 ha->host_no, ddb_entry->fw_ddb_index));
391 } else
392 DEBUG2(printk("scsi%ld: ddb [%d] DEAD\n", ha->host_no,
393 ddb_entry->fw_ddb_index))
394
Mike Christieb6359302008-01-31 13:36:44 -0600395 iscsi_block_session(ddb_entry->sess);
Mike Christiee5bd7b52008-09-24 11:46:10 -0500396 iscsi_conn_error_event(ddb_entry->conn, ISCSI_ERR_CONN_FAILED);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700397}
398
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530399/**
400 * qla4xxx_mark_all_devices_missing - mark all devices as missing.
401 * @ha: Pointer to host adapter structure.
402 *
403 * This routine marks a device missing and resets the relogin retry count.
404 **/
405void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
406{
407 struct ddb_entry *ddb_entry, *ddbtemp;
408 list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
409 qla4xxx_mark_device_missing(ha, ddb_entry);
410 }
411}
412
David Somayajuluafaf5a22006-09-19 10:28:00 -0700413static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
414 struct ddb_entry *ddb_entry,
415 struct scsi_cmnd *cmd,
416 void (*done)(struct scsi_cmnd *))
417{
418 struct srb *srb;
419
420 srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
421 if (!srb)
422 return srb;
423
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530424 kref_init(&srb->srb_ref);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700425 srb->ha = ha;
426 srb->ddb = ddb_entry;
427 srb->cmd = cmd;
428 srb->flags = 0;
Vikas Chaudhary53698872010-04-28 11:41:59 +0530429 CMD_SP(cmd) = (void *)srb;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700430 cmd->scsi_done = done;
431
432 return srb;
433}
434
435static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
436{
437 struct scsi_cmnd *cmd = srb->cmd;
438
439 if (srb->flags & SRB_DMA_VALID) {
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +0900440 scsi_dma_unmap(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700441 srb->flags &= ~SRB_DMA_VALID;
442 }
Vikas Chaudhary53698872010-04-28 11:41:59 +0530443 CMD_SP(cmd) = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700444}
445
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530446void qla4xxx_srb_compl(struct kref *ref)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700447{
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530448 struct srb *srb = container_of(ref, struct srb, srb_ref);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700449 struct scsi_cmnd *cmd = srb->cmd;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530450 struct scsi_qla_host *ha = srb->ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700451
452 qla4xxx_srb_free_dma(ha, srb);
453
454 mempool_free(srb, ha->srb_mempool);
455
456 cmd->scsi_done(cmd);
457}
458
459/**
460 * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
461 * @cmd: Pointer to Linux's SCSI command structure
462 * @done_fn: Function that the driver calls to notify the SCSI mid-layer
463 * that the command has been processed.
464 *
465 * Remarks:
466 * This routine is invoked by Linux to send a SCSI command to the driver.
467 * The mid-level driver tries to ensure that queuecommand never gets
468 * invoked concurrently with itself or the interrupt handler (although
469 * the interrupt handler may call this routine as part of request-
470 * completion handling). Unfortunely, it sometimes calls the scheduler
471 * in interrupt context which is a big NO! NO!.
472 **/
Jeff Garzikf2812332010-11-16 02:10:29 -0500473static int qla4xxx_queuecommand_lck(struct scsi_cmnd *cmd,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700474 void (*done)(struct scsi_cmnd *))
475{
476 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
477 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christie7fb19212008-01-31 13:36:45 -0600478 struct iscsi_cls_session *sess = ddb_entry->sess;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700479 struct srb *srb;
480 int rval;
481
Lalit Chandivade2232be02010-07-30 14:38:47 +0530482 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
483 if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
484 cmd->result = DID_NO_CONNECT << 16;
485 else
486 cmd->result = DID_REQUEUE << 16;
487 goto qc_fail_command;
488 }
489
Mike Christie7fb19212008-01-31 13:36:45 -0600490 if (!sess) {
491 cmd->result = DID_IMM_RETRY << 16;
492 goto qc_fail_command;
493 }
494
495 rval = iscsi_session_chkready(sess);
496 if (rval) {
497 cmd->result = rval;
498 goto qc_fail_command;
499 }
500
David Somayajuluafaf5a22006-09-19 10:28:00 -0700501 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
502 if (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD) {
503 cmd->result = DID_NO_CONNECT << 16;
504 goto qc_fail_command;
505 }
Mike Christiec5e98e92008-08-17 15:24:39 -0500506 return SCSI_MLQUEUE_TARGET_BUSY;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700507 }
508
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530509 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
510 test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
511 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
512 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
513 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
514 !test_bit(AF_ONLINE, &ha->flags) ||
515 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
David C Somayajulu477ffb92007-01-22 12:26:11 -0800516 goto qc_host_busy;
517
David Somayajuluafaf5a22006-09-19 10:28:00 -0700518 spin_unlock_irq(ha->host->host_lock);
519
520 srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd, done);
521 if (!srb)
522 goto qc_host_busy_lock;
523
524 rval = qla4xxx_send_command_to_isp(ha, srb);
525 if (rval != QLA_SUCCESS)
526 goto qc_host_busy_free_sp;
527
528 spin_lock_irq(ha->host->host_lock);
529 return 0;
530
531qc_host_busy_free_sp:
532 qla4xxx_srb_free_dma(ha, srb);
533 mempool_free(srb, ha->srb_mempool);
534
535qc_host_busy_lock:
536 spin_lock_irq(ha->host->host_lock);
537
538qc_host_busy:
539 return SCSI_MLQUEUE_HOST_BUSY;
540
541qc_fail_command:
542 done(cmd);
543
544 return 0;
545}
546
Jeff Garzikf2812332010-11-16 02:10:29 -0500547static DEF_SCSI_QCMD(qla4xxx_queuecommand)
548
David Somayajuluafaf5a22006-09-19 10:28:00 -0700549/**
550 * qla4xxx_mem_free - frees memory allocated to adapter
551 * @ha: Pointer to host adapter structure.
552 *
553 * Frees memory previously allocated by qla4xxx_mem_alloc
554 **/
555static void qla4xxx_mem_free(struct scsi_qla_host *ha)
556{
557 if (ha->queues)
558 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
559 ha->queues_dma);
560
561 ha->queues_len = 0;
562 ha->queues = NULL;
563 ha->queues_dma = 0;
564 ha->request_ring = NULL;
565 ha->request_dma = 0;
566 ha->response_ring = NULL;
567 ha->response_dma = 0;
568 ha->shadow_regs = NULL;
569 ha->shadow_regs_dma = 0;
570
571 /* Free srb pool. */
572 if (ha->srb_mempool)
573 mempool_destroy(ha->srb_mempool);
574
575 ha->srb_mempool = NULL;
576
577 /* release io space registers */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530578 if (is_qla8022(ha)) {
579 if (ha->nx_pcibase)
580 iounmap(
581 (struct device_reg_82xx __iomem *)ha->nx_pcibase);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530582 } else if (ha->reg)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700583 iounmap(ha->reg);
584 pci_release_regions(ha->pdev);
585}
586
587/**
588 * qla4xxx_mem_alloc - allocates memory for use by adapter.
589 * @ha: Pointer to host adapter structure
590 *
591 * Allocates DMA memory for request and response queues. Also allocates memory
592 * for srbs.
593 **/
594static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
595{
596 unsigned long align;
597
598 /* Allocate contiguous block of DMA memory for queues. */
599 ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
600 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
601 sizeof(struct shadow_regs) +
602 MEM_ALIGN_VALUE +
603 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
604 ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
605 &ha->queues_dma, GFP_KERNEL);
606 if (ha->queues == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530607 ql4_printk(KERN_WARNING, ha,
608 "Memory Allocation failed - queues.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700609
610 goto mem_alloc_error_exit;
611 }
612 memset(ha->queues, 0, ha->queues_len);
613
614 /*
615 * As per RISC alignment requirements -- the bus-address must be a
616 * multiple of the request-ring size (in bytes).
617 */
618 align = 0;
619 if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
620 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
621 (MEM_ALIGN_VALUE - 1));
622
623 /* Update request and response queue pointers. */
624 ha->request_dma = ha->queues_dma + align;
625 ha->request_ring = (struct queue_entry *) (ha->queues + align);
626 ha->response_dma = ha->queues_dma + align +
627 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
628 ha->response_ring = (struct queue_entry *) (ha->queues + align +
629 (REQUEST_QUEUE_DEPTH *
630 QUEUE_SIZE));
631 ha->shadow_regs_dma = ha->queues_dma + align +
632 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
633 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
634 ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
635 (REQUEST_QUEUE_DEPTH *
636 QUEUE_SIZE) +
637 (RESPONSE_QUEUE_DEPTH *
638 QUEUE_SIZE));
639
640 /* Allocate memory for srb pool. */
641 ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
642 mempool_free_slab, srb_cachep);
643 if (ha->srb_mempool == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530644 ql4_printk(KERN_WARNING, ha,
645 "Memory Allocation failed - SRB Pool.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700646
647 goto mem_alloc_error_exit;
648 }
649
650 return QLA_SUCCESS;
651
652mem_alloc_error_exit:
653 qla4xxx_mem_free(ha);
654 return QLA_ERROR;
655}
656
657/**
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530658 * qla4_8xxx_check_fw_alive - Check firmware health
659 * @ha: Pointer to host adapter structure.
660 *
661 * Context: Interrupt
662 **/
663static void qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
664{
665 uint32_t fw_heartbeat_counter, halt_status;
666
667 fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
Lalit Chandivade2232be02010-07-30 14:38:47 +0530668 /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
669 if (fw_heartbeat_counter == 0xffffffff) {
670 DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
671 "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
672 ha->host_no, __func__));
673 return;
674 }
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530675
676 if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
677 ha->seconds_since_last_heartbeat++;
678 /* FW not alive after 2 seconds */
679 if (ha->seconds_since_last_heartbeat == 2) {
680 ha->seconds_since_last_heartbeat = 0;
681 halt_status = qla4_8xxx_rd_32(ha,
Vikas Chaudhary68d92eb2011-05-17 23:17:05 -0700682 QLA82XX_PEG_HALT_STATUS1);
683
684 ql4_printk(KERN_INFO, ha,
685 "scsi(%ld): %s, Dumping hw/fw registers:\n "
686 " PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2:"
687 " 0x%x,\n PEG_NET_0_PC: 0x%x, PEG_NET_1_PC:"
688 " 0x%x,\n PEG_NET_2_PC: 0x%x, PEG_NET_3_PC:"
689 " 0x%x,\n PEG_NET_4_PC: 0x%x\n",
690 ha->host_no, __func__, halt_status,
691 qla4_8xxx_rd_32(ha,
692 QLA82XX_PEG_HALT_STATUS2),
693 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_0 +
694 0x3c),
695 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_1 +
696 0x3c),
697 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_2 +
698 0x3c),
699 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_3 +
700 0x3c),
701 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_4 +
702 0x3c));
Nilesh Javali21033632010-07-30 14:28:07 +0530703
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530704 /* Since we cannot change dev_state in interrupt
705 * context, set appropriate DPC flag then wakeup
706 * DPC */
707 if (halt_status & HALT_STATUS_UNRECOVERABLE)
708 set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
709 else {
710 printk("scsi%ld: %s: detect abort needed!\n",
711 ha->host_no, __func__);
712 set_bit(DPC_RESET_HA, &ha->dpc_flags);
713 }
714 qla4xxx_wake_dpc(ha);
Nilesh Javali21033632010-07-30 14:28:07 +0530715 qla4xxx_mailbox_premature_completion(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530716 }
Lalit Chandivade99457d72010-10-06 22:49:32 -0700717 } else
718 ha->seconds_since_last_heartbeat = 0;
719
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530720 ha->fw_heartbeat_counter = fw_heartbeat_counter;
721}
722
723/**
724 * qla4_8xxx_watchdog - Poll dev state
725 * @ha: Pointer to host adapter structure.
726 *
727 * Context: Interrupt
728 **/
729void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
730{
731 uint32_t dev_state;
732
733 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
734
735 /* don't poll if reset is going on */
Lalit Chandivaded56a1f72010-12-02 22:12:45 -0800736 if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
737 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
Vikas Chaudhary977f46a2011-05-17 23:17:08 -0700738 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags))) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530739 if (dev_state == QLA82XX_DEV_NEED_RESET &&
740 !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
Vikas Chaudhary3930b8c2010-12-02 22:12:47 -0800741 if (!ql4xdontresethba) {
742 ql4_printk(KERN_INFO, ha, "%s: HW State: "
743 "NEED RESET!\n", __func__);
744 set_bit(DPC_RESET_HA, &ha->dpc_flags);
745 qla4xxx_wake_dpc(ha);
746 qla4xxx_mailbox_premature_completion(ha);
747 }
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530748 } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
749 !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
Vikas Chaudhary3930b8c2010-12-02 22:12:47 -0800750 ql4_printk(KERN_INFO, ha, "%s: HW State: NEED QUIES!\n",
751 __func__);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530752 set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
753 qla4xxx_wake_dpc(ha);
754 } else {
755 /* Check firmware health */
756 qla4_8xxx_check_fw_alive(ha);
757 }
758 }
759}
760
761/**
David Somayajuluafaf5a22006-09-19 10:28:00 -0700762 * qla4xxx_timer - checks every second for work to do.
763 * @ha: Pointer to host adapter structure.
764 **/
765static void qla4xxx_timer(struct scsi_qla_host *ha)
766{
767 struct ddb_entry *ddb_entry, *dtemp;
768 int start_dpc = 0;
Lalit Chandivade2232be02010-07-30 14:38:47 +0530769 uint16_t w;
770
771 /* If we are in the middle of AER/EEH processing
772 * skip any processing and reschedule the timer
773 */
774 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
775 mod_timer(&ha->timer, jiffies + HZ);
776 return;
777 }
778
779 /* Hardware read to trigger an EEH error during mailbox waits. */
780 if (!pci_channel_offline(ha->pdev))
781 pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700782
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530783 if (is_qla8022(ha)) {
784 qla4_8xxx_watchdog(ha);
785 }
786
David Somayajuluafaf5a22006-09-19 10:28:00 -0700787 /* Search for relogin's to time-out and port down retry. */
788 list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
789 /* Count down time between sending relogins */
790 if (adapter_up(ha) &&
791 !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
792 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
793 if (atomic_read(&ddb_entry->retry_relogin_timer) !=
794 INVALID_ENTRY) {
795 if (atomic_read(&ddb_entry->retry_relogin_timer)
796 == 0) {
797 atomic_set(&ddb_entry->
798 retry_relogin_timer,
799 INVALID_ENTRY);
800 set_bit(DPC_RELOGIN_DEVICE,
801 &ha->dpc_flags);
802 set_bit(DF_RELOGIN, &ddb_entry->flags);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530803 DEBUG2(printk("scsi%ld: %s: ddb [%d]"
David Somayajuluafaf5a22006-09-19 10:28:00 -0700804 " login device\n",
805 ha->host_no, __func__,
806 ddb_entry->fw_ddb_index));
807 } else
808 atomic_dec(&ddb_entry->
809 retry_relogin_timer);
810 }
811 }
812
813 /* Wait for relogin to timeout */
814 if (atomic_read(&ddb_entry->relogin_timer) &&
815 (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
816 /*
817 * If the relogin times out and the device is
818 * still NOT ONLINE then try and relogin again.
819 */
820 if (atomic_read(&ddb_entry->state) !=
821 DDB_STATE_ONLINE &&
822 ddb_entry->fw_ddb_device_state ==
823 DDB_DS_SESSION_FAILED) {
824 /* Reset retry relogin timer */
825 atomic_inc(&ddb_entry->relogin_retry_count);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530826 DEBUG2(printk("scsi%ld: ddb [%d] relogin"
David Somayajuluafaf5a22006-09-19 10:28:00 -0700827 " timed out-retrying"
828 " relogin (%d)\n",
829 ha->host_no,
830 ddb_entry->fw_ddb_index,
831 atomic_read(&ddb_entry->
832 relogin_retry_count))
833 );
834 start_dpc++;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530835 DEBUG(printk("scsi%ld:%d:%d: ddb [%d] "
Justin P. Mattockcd09b2c2011-01-28 11:49:08 -0800836 "initiate relogin after"
David Somayajuluafaf5a22006-09-19 10:28:00 -0700837 " %d seconds\n",
838 ha->host_no, ddb_entry->bus,
839 ddb_entry->target,
840 ddb_entry->fw_ddb_index,
841 ddb_entry->default_time2wait + 4)
842 );
843
844 atomic_set(&ddb_entry->retry_relogin_timer,
845 ddb_entry->default_time2wait + 4);
846 }
847 }
848 }
849
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530850 if (!is_qla8022(ha)) {
851 /* Check for heartbeat interval. */
852 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
853 ha->heartbeat_interval != 0) {
854 ha->seconds_since_last_heartbeat++;
855 if (ha->seconds_since_last_heartbeat >
856 ha->heartbeat_interval + 2)
857 set_bit(DPC_RESET_HA, &ha->dpc_flags);
858 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700859 }
860
David Somayajuluafaf5a22006-09-19 10:28:00 -0700861 /* Wakeup the dpc routine for this adapter, if needed. */
Lalit Chandivade1b468072011-05-17 23:17:09 -0700862 if (start_dpc ||
David Somayajuluafaf5a22006-09-19 10:28:00 -0700863 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
864 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
865 test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530866 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -0700867 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
868 test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +0530869 test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530870 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
871 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
Lalit Chandivade1b468072011-05-17 23:17:09 -0700872 test_bit(DPC_AEN, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700873 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
874 " - dpc flags = 0x%lx\n",
875 ha->host_no, __func__, ha->dpc_flags));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530876 qla4xxx_wake_dpc(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700877 }
878
879 /* Reschedule timer thread to call us back in one second */
880 mod_timer(&ha->timer, jiffies + HZ);
881
882 DEBUG2(ha->seconds_since_last_intr++);
883}
884
885/**
886 * qla4xxx_cmd_wait - waits for all outstanding commands to complete
887 * @ha: Pointer to host adapter structure.
888 *
889 * This routine stalls the driver until all outstanding commands are returned.
890 * Caller must release the Hardware Lock prior to calling this routine.
891 **/
892static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
893{
894 uint32_t index = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700895 unsigned long flags;
896 struct scsi_cmnd *cmd;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700897
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530898 unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
899
900 DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
901 "complete\n", WAIT_CMD_TOV));
902
903 while (!time_after_eq(jiffies, wtime)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700904 spin_lock_irqsave(&ha->hardware_lock, flags);
905 /* Find a command that hasn't completed. */
906 for (index = 0; index < ha->host->can_queue; index++) {
907 cmd = scsi_host_find_tag(ha->host, index);
Mike Christiea1e00632010-10-26 05:45:30 -0700908 /*
909 * We cannot just check if the index is valid,
910 * becase if we are run from the scsi eh, then
911 * the scsi/block layer is going to prevent
912 * the tag from being released.
913 */
914 if (cmd != NULL && CMD_SP(cmd))
David Somayajuluafaf5a22006-09-19 10:28:00 -0700915 break;
916 }
917 spin_unlock_irqrestore(&ha->hardware_lock, flags);
918
919 /* If No Commands are pending, wait is complete */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530920 if (index == ha->host->can_queue)
921 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700922
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530923 msleep(1000);
924 }
925 /* If we timed out on waiting for commands to come back
926 * return ERROR. */
927 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700928}
929
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530930int qla4xxx_hw_reset(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700931{
David Somayajuluafaf5a22006-09-19 10:28:00 -0700932 uint32_t ctrl_status;
David C Somayajulu477ffb92007-01-22 12:26:11 -0800933 unsigned long flags = 0;
934
935 DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700936
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530937 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
938 return QLA_ERROR;
939
David Somayajuluafaf5a22006-09-19 10:28:00 -0700940 spin_lock_irqsave(&ha->hardware_lock, flags);
941
942 /*
943 * If the SCSI Reset Interrupt bit is set, clear it.
944 * Otherwise, the Soft Reset won't work.
945 */
946 ctrl_status = readw(&ha->reg->ctrl_status);
947 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
948 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
949
950 /* Issue Soft Reset */
951 writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
952 readl(&ha->reg->ctrl_status);
953
954 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530955 return QLA_SUCCESS;
David C Somayajulu477ffb92007-01-22 12:26:11 -0800956}
957
958/**
959 * qla4xxx_soft_reset - performs soft reset.
960 * @ha: Pointer to host adapter structure.
961 **/
962int qla4xxx_soft_reset(struct scsi_qla_host *ha)
963{
964 uint32_t max_wait_time;
965 unsigned long flags = 0;
Vikas Chaudharyf931c532010-10-06 22:48:07 -0700966 int status;
David C Somayajulu477ffb92007-01-22 12:26:11 -0800967 uint32_t ctrl_status;
968
Vikas Chaudharyf931c532010-10-06 22:48:07 -0700969 status = qla4xxx_hw_reset(ha);
970 if (status != QLA_SUCCESS)
971 return status;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700972
Vikas Chaudharyf931c532010-10-06 22:48:07 -0700973 status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700974 /* Wait until the Network Reset Intr bit is cleared */
975 max_wait_time = RESET_INTR_TOV;
976 do {
977 spin_lock_irqsave(&ha->hardware_lock, flags);
978 ctrl_status = readw(&ha->reg->ctrl_status);
979 spin_unlock_irqrestore(&ha->hardware_lock, flags);
980
981 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
982 break;
983
984 msleep(1000);
985 } while ((--max_wait_time));
986
987 if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
988 DEBUG2(printk(KERN_WARNING
989 "scsi%ld: Network Reset Intr not cleared by "
990 "Network function, clearing it now!\n",
991 ha->host_no));
992 spin_lock_irqsave(&ha->hardware_lock, flags);
993 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
994 readl(&ha->reg->ctrl_status);
995 spin_unlock_irqrestore(&ha->hardware_lock, flags);
996 }
997
998 /* Wait until the firmware tells us the Soft Reset is done */
999 max_wait_time = SOFT_RESET_TOV;
1000 do {
1001 spin_lock_irqsave(&ha->hardware_lock, flags);
1002 ctrl_status = readw(&ha->reg->ctrl_status);
1003 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1004
1005 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
1006 status = QLA_SUCCESS;
1007 break;
1008 }
1009
1010 msleep(1000);
1011 } while ((--max_wait_time));
1012
1013 /*
1014 * Also, make sure that the SCSI Reset Interrupt bit has been cleared
1015 * after the soft reset has taken place.
1016 */
1017 spin_lock_irqsave(&ha->hardware_lock, flags);
1018 ctrl_status = readw(&ha->reg->ctrl_status);
1019 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
1020 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
1021 readl(&ha->reg->ctrl_status);
1022 }
1023 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1024
1025 /* If soft reset fails then most probably the bios on other
1026 * function is also enabled.
1027 * Since the initialization is sequential the other fn
1028 * wont be able to acknowledge the soft reset.
1029 * Issue a force soft reset to workaround this scenario.
1030 */
1031 if (max_wait_time == 0) {
1032 /* Issue Force Soft Reset */
1033 spin_lock_irqsave(&ha->hardware_lock, flags);
1034 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
1035 readl(&ha->reg->ctrl_status);
1036 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1037 /* Wait until the firmware tells us the Soft Reset is done */
1038 max_wait_time = SOFT_RESET_TOV;
1039 do {
1040 spin_lock_irqsave(&ha->hardware_lock, flags);
1041 ctrl_status = readw(&ha->reg->ctrl_status);
1042 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1043
1044 if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
1045 status = QLA_SUCCESS;
1046 break;
1047 }
1048
1049 msleep(1000);
1050 } while ((--max_wait_time));
1051 }
1052
1053 return status;
1054}
1055
1056/**
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301057 * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001058 * @ha: Pointer to host adapter structure.
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301059 * @res: returned scsi status
David Somayajuluafaf5a22006-09-19 10:28:00 -07001060 *
1061 * This routine is called just prior to a HARD RESET to return all
1062 * outstanding commands back to the Operating System.
1063 * Caller should make sure that the following locks are released
1064 * before this calling routine: Hardware lock, and io_request_lock.
1065 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301066static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001067{
1068 struct srb *srb;
1069 int i;
1070 unsigned long flags;
1071
1072 spin_lock_irqsave(&ha->hardware_lock, flags);
1073 for (i = 0; i < ha->host->can_queue; i++) {
1074 srb = qla4xxx_del_from_active_array(ha, i);
1075 if (srb != NULL) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301076 srb->cmd->result = res;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301077 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001078 }
1079 }
1080 spin_unlock_irqrestore(&ha->hardware_lock, flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001081}
1082
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301083void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
1084{
1085 clear_bit(AF_ONLINE, &ha->flags);
1086
1087 /* Disable the board */
1088 ql4_printk(KERN_INFO, ha, "Disabling the board\n");
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301089
1090 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
1091 qla4xxx_mark_all_devices_missing(ha);
1092 clear_bit(AF_INIT_DONE, &ha->flags);
1093}
1094
David Somayajuluafaf5a22006-09-19 10:28:00 -07001095/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07001096 * qla4xxx_recover_adapter - recovers adapter after a fatal error
1097 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001098 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301099static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001100{
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301101 int status = QLA_ERROR;
1102 uint8_t reset_chip = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001103
1104 /* Stall incoming I/O until we are done */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301105 scsi_block_requests(ha->host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001106 clear_bit(AF_ONLINE, &ha->flags);
Mike Christie50a29ae2008-03-04 13:26:53 -06001107
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301108 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001109
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301110 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001111
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301112 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
1113 reset_chip = 1;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001114
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301115 /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
1116 * do not reset adapter, jump to initialize_adapter */
1117 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1118 status = QLA_SUCCESS;
1119 goto recover_ha_init_adapter;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001120 }
1121
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301122 /* For the ISP-82xx adapter, issue a stop_firmware if invoked
1123 * from eh_host_reset or ioctl module */
1124 if (is_qla8022(ha) && !reset_chip &&
1125 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
1126
1127 DEBUG2(ql4_printk(KERN_INFO, ha,
1128 "scsi%ld: %s - Performing stop_firmware...\n",
1129 ha->host_no, __func__));
1130 status = ha->isp_ops->reset_firmware(ha);
1131 if (status == QLA_SUCCESS) {
Nilesh Javali2bd1e2b2010-10-06 22:49:20 -07001132 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
1133 qla4xxx_cmd_wait(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301134 ha->isp_ops->disable_intrs(ha);
1135 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1136 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1137 } else {
1138 /* If the stop_firmware fails then
1139 * reset the entire chip */
1140 reset_chip = 1;
1141 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1142 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1143 }
1144 }
1145
1146 /* Issue full chip reset if recovering from a catastrophic error,
1147 * or if stop_firmware fails for ISP-82xx.
1148 * This is the default case for ISP-4xxx */
1149 if (!is_qla8022(ha) || reset_chip) {
Nilesh Javali2bd1e2b2010-10-06 22:49:20 -07001150 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
1151 qla4xxx_cmd_wait(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301152 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1153 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1154 DEBUG2(ql4_printk(KERN_INFO, ha,
1155 "scsi%ld: %s - Performing chip reset..\n",
1156 ha->host_no, __func__));
1157 status = ha->isp_ops->reset_chip(ha);
1158 }
1159
1160 /* Flush any pending ddb changed AENs */
1161 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1162
1163recover_ha_init_adapter:
1164 /* Upon successful firmware/chip reset, re-initialize the adapter */
1165 if (status == QLA_SUCCESS) {
1166 /* For ISP-4xxx, force function 1 to always initialize
1167 * before function 3 to prevent both funcions from
1168 * stepping on top of the other */
1169 if (!is_qla8022(ha) && (ha->mac_index == 3))
1170 ssleep(6);
1171
1172 /* NOTE: AF_ONLINE flag set upon successful completion of
1173 * qla4xxx_initialize_adapter */
1174 status = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
1175 }
1176
1177 /* Retry failed adapter initialization, if necessary
1178 * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
1179 * case to prevent ping-pong resets between functions */
1180 if (!test_bit(AF_ONLINE, &ha->flags) &&
1181 !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001182 /* Adapter initialization failed, see if we can retry
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301183 * resetting the ha.
1184 * Since we don't want to block the DPC for too long
1185 * with multiple resets in the same thread,
1186 * utilize DPC to retry */
David Somayajuluafaf5a22006-09-19 10:28:00 -07001187 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
1188 ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
1189 DEBUG2(printk("scsi%ld: recover adapter - retrying "
1190 "(%d) more times\n", ha->host_no,
1191 ha->retry_reset_ha_cnt));
1192 set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1193 status = QLA_ERROR;
1194 } else {
1195 if (ha->retry_reset_ha_cnt > 0) {
1196 /* Schedule another Reset HA--DPC will retry */
1197 ha->retry_reset_ha_cnt--;
1198 DEBUG2(printk("scsi%ld: recover adapter - "
1199 "retry remaining %d\n",
1200 ha->host_no,
1201 ha->retry_reset_ha_cnt));
1202 status = QLA_ERROR;
1203 }
1204
1205 if (ha->retry_reset_ha_cnt == 0) {
1206 /* Recover adapter retries have been exhausted.
1207 * Adapter DEAD */
1208 DEBUG2(printk("scsi%ld: recover adapter "
1209 "failed - board disabled\n",
1210 ha->host_no));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301211 qla4xxx_dead_adapter_cleanup(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001212 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1213 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301214 clear_bit(DPC_RESET_HA_FW_CONTEXT,
David Somayajuluafaf5a22006-09-19 10:28:00 -07001215 &ha->dpc_flags);
1216 status = QLA_ERROR;
1217 }
1218 }
1219 } else {
1220 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301221 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001222 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1223 }
1224
1225 ha->adapter_error_count++;
1226
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301227 if (test_bit(AF_ONLINE, &ha->flags))
1228 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001229
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301230 scsi_unblock_requests(ha->host);
1231
1232 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
1233 DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001234 status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301235
David Somayajuluafaf5a22006-09-19 10:28:00 -07001236 return status;
1237}
1238
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07001239static void qla4xxx_relogin_all_devices(struct scsi_qla_host *ha)
1240{
1241 struct ddb_entry *ddb_entry, *dtemp;
1242
1243 list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
1244 if ((atomic_read(&ddb_entry->state) == DDB_STATE_MISSING) ||
1245 (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD)) {
1246 if (ddb_entry->fw_ddb_device_state ==
1247 DDB_DS_SESSION_ACTIVE) {
1248 atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
1249 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
1250 " marked ONLINE\n", ha->host_no, __func__,
1251 ddb_entry->fw_ddb_index);
1252
1253 iscsi_unblock_session(ddb_entry->sess);
1254 } else
1255 qla4xxx_relogin_device(ha, ddb_entry);
1256 }
1257 }
1258}
1259
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301260void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
1261{
Lalit Chandivade1b468072011-05-17 23:17:09 -07001262 if (ha->dpc_thread)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301263 queue_work(ha->dpc_thread, &ha->dpc_work);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301264}
1265
David Somayajuluafaf5a22006-09-19 10:28:00 -07001266/**
1267 * qla4xxx_do_dpc - dpc routine
1268 * @data: in our case pointer to adapter structure
1269 *
1270 * This routine is a task that is schedule by the interrupt handler
1271 * to perform the background processing for interrupts. We put it
1272 * on a task queue that is consumed whenever the scheduler runs; that's
1273 * so you can do anything (i.e. put the process to sleep etc). In fact,
1274 * the mid-level tries to sleep when it reaches the driver threshold
1275 * "host->can_queue". This can cause a panic if we were in our interrupt code.
1276 **/
David Howellsc4028952006-11-22 14:57:56 +00001277static void qla4xxx_do_dpc(struct work_struct *work)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001278{
David Howellsc4028952006-11-22 14:57:56 +00001279 struct scsi_qla_host *ha =
1280 container_of(work, struct scsi_qla_host, dpc_work);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001281 struct ddb_entry *ddb_entry, *dtemp;
David C Somayajulu477ffb92007-01-22 12:26:11 -08001282 int status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001283
David C Somayajuluf26b9042006-11-15 16:41:09 -08001284 DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301285 "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
1286 ha->host_no, __func__, ha->flags, ha->dpc_flags))
David Somayajuluafaf5a22006-09-19 10:28:00 -07001287
1288 /* Initialization not yet finished. Don't do anything yet. */
1289 if (!test_bit(AF_INIT_DONE, &ha->flags))
Lalit Chandivade1b468072011-05-17 23:17:09 -07001290 return;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001291
Lalit Chandivade2232be02010-07-30 14:38:47 +05301292 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1293 DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
1294 ha->host_no, __func__, ha->flags));
Lalit Chandivade1b468072011-05-17 23:17:09 -07001295 return;
Lalit Chandivade2232be02010-07-30 14:38:47 +05301296 }
1297
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301298 if (is_qla8022(ha)) {
1299 if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
1300 qla4_8xxx_idc_lock(ha);
1301 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
1302 QLA82XX_DEV_FAILED);
1303 qla4_8xxx_idc_unlock(ha);
1304 ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
1305 qla4_8xxx_device_state_handler(ha);
1306 }
1307 if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
1308 qla4_8xxx_need_qsnt_handler(ha);
1309 }
1310 }
1311
1312 if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
1313 (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07001314 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301315 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
1316 if (ql4xdontresethba) {
1317 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
1318 ha->host_no, __func__));
1319 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
1320 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1321 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1322 goto dpc_post_reset_ha;
1323 }
1324 if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
1325 test_bit(DPC_RESET_HA, &ha->dpc_flags))
1326 qla4xxx_recover_adapter(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001327
David C Somayajulu477ffb92007-01-22 12:26:11 -08001328 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001329 uint8_t wait_time = RESET_INTR_TOV;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001330
David Somayajuluafaf5a22006-09-19 10:28:00 -07001331 while ((readw(&ha->reg->ctrl_status) &
1332 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
1333 if (--wait_time == 0)
1334 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001335 msleep(1000);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001336 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001337 if (wait_time == 0)
1338 DEBUG2(printk("scsi%ld: %s: SR|FSR "
1339 "bit not cleared-- resetting\n",
1340 ha->host_no, __func__));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301341 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
David C Somayajulu477ffb92007-01-22 12:26:11 -08001342 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
1343 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301344 status = qla4xxx_recover_adapter(ha);
David C Somayajulu477ffb92007-01-22 12:26:11 -08001345 }
1346 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1347 if (status == QLA_SUCCESS)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301348 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001349 }
1350 }
1351
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301352dpc_post_reset_ha:
David Somayajuluafaf5a22006-09-19 10:28:00 -07001353 /* ---- process AEN? --- */
1354 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
1355 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
1356
1357 /* ---- Get DHCP IP Address? --- */
1358 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
1359 qla4xxx_get_dhcp_ip_address(ha);
1360
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301361 /* ---- link change? --- */
1362 if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
1363 if (!test_bit(AF_LINK_UP, &ha->flags)) {
1364 /* ---- link down? --- */
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07001365 qla4xxx_mark_all_devices_missing(ha);
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301366 } else {
1367 /* ---- link up? --- *
1368 * F/W will auto login to all devices ONLY ONCE after
1369 * link up during driver initialization and runtime
1370 * fatal error recovery. Therefore, the driver must
1371 * manually relogin to devices when recovering from
1372 * connection failures, logouts, expired KATO, etc. */
1373
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07001374 qla4xxx_relogin_all_devices(ha);
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301375 }
1376 }
1377
David Somayajuluafaf5a22006-09-19 10:28:00 -07001378 /* ---- relogin device? --- */
1379 if (adapter_up(ha) &&
1380 test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
1381 list_for_each_entry_safe(ddb_entry, dtemp,
1382 &ha->ddb_list, list) {
1383 if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
1384 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
1385 qla4xxx_relogin_device(ha, ddb_entry);
1386
1387 /*
1388 * If mbx cmd times out there is no point
1389 * in continuing further.
1390 * With large no of targets this can hang
1391 * the system.
1392 */
1393 if (test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
1394 printk(KERN_WARNING "scsi%ld: %s: "
1395 "need to reset hba\n",
1396 ha->host_no, __func__);
1397 break;
1398 }
1399 }
1400 }
Vikas Chaudhary0753b482010-07-30 14:27:19 +05301401
David Somayajuluafaf5a22006-09-19 10:28:00 -07001402}
1403
1404/**
1405 * qla4xxx_free_adapter - release the adapter
1406 * @ha: pointer to adapter structure
1407 **/
1408static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
1409{
1410
1411 if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
1412 /* Turn-off interrupts on the card. */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301413 ha->isp_ops->disable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001414 }
1415
David Somayajuluafaf5a22006-09-19 10:28:00 -07001416 /* Remove timer thread, if present */
1417 if (ha->timer_active)
1418 qla4xxx_stop_timer(ha);
1419
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301420 /* Kill the kernel thread for this host */
1421 if (ha->dpc_thread)
1422 destroy_workqueue(ha->dpc_thread);
1423
1424 /* Put firmware in known state */
1425 ha->isp_ops->reset_firmware(ha);
1426
1427 if (is_qla8022(ha)) {
1428 qla4_8xxx_idc_lock(ha);
1429 qla4_8xxx_clear_drv_active(ha);
1430 qla4_8xxx_idc_unlock(ha);
1431 }
1432
David Somayajuluafaf5a22006-09-19 10:28:00 -07001433 /* Detach interrupts */
1434 if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301435 qla4xxx_free_irqs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001436
David C Somayajulubee4fe82007-05-23 18:03:32 -07001437 /* free extra memory */
1438 qla4xxx_mem_free(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301439}
David C Somayajulubee4fe82007-05-23 18:03:32 -07001440
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301441int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
1442{
1443 int status = 0;
1444 uint8_t revision_id;
1445 unsigned long mem_base, mem_len, db_base, db_len;
1446 struct pci_dev *pdev = ha->pdev;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001447
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301448 status = pci_request_regions(pdev, DRIVER_NAME);
1449 if (status) {
1450 printk(KERN_WARNING
1451 "scsi(%ld) Failed to reserve PIO regions (%s) "
1452 "status=%d\n", ha->host_no, pci_name(pdev), status);
1453 goto iospace_error_exit;
1454 }
1455
1456 pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
1457 DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
1458 __func__, revision_id));
1459 ha->revision_id = revision_id;
1460
1461 /* remap phys address */
1462 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
1463 mem_len = pci_resource_len(pdev, 0);
1464 DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
1465 __func__, mem_base, mem_len));
1466
1467 /* mapping of pcibase pointer */
1468 ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
1469 if (!ha->nx_pcibase) {
1470 printk(KERN_ERR
1471 "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
1472 pci_release_regions(ha->pdev);
1473 goto iospace_error_exit;
1474 }
1475
1476 /* Mapping of IO base pointer, door bell read and write pointer */
1477
1478 /* mapping of IO base pointer */
1479 ha->qla4_8xxx_reg =
1480 (struct device_reg_82xx __iomem *)((uint8_t *)ha->nx_pcibase +
1481 0xbc000 + (ha->pdev->devfn << 11));
1482
1483 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
1484 db_len = pci_resource_len(pdev, 4);
1485
Shyam Sundar2657c802010-10-06 22:50:29 -07001486 ha->nx_db_wr_ptr = (ha->pdev->devfn == 4 ? QLA82XX_CAM_RAM_DB1 :
1487 QLA82XX_CAM_RAM_DB2);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301488
Shyam Sundar2657c802010-10-06 22:50:29 -07001489 return 0;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301490iospace_error_exit:
1491 return -ENOMEM;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001492}
1493
1494/***
1495 * qla4xxx_iospace_config - maps registers
1496 * @ha: pointer to adapter structure
1497 *
1498 * This routines maps HBA's registers from the pci address space
1499 * into the kernel virtual address space for memory mapped i/o.
1500 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301501int qla4xxx_iospace_config(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001502{
1503 unsigned long pio, pio_len, pio_flags;
1504 unsigned long mmio, mmio_len, mmio_flags;
1505
1506 pio = pci_resource_start(ha->pdev, 0);
1507 pio_len = pci_resource_len(ha->pdev, 0);
1508 pio_flags = pci_resource_flags(ha->pdev, 0);
1509 if (pio_flags & IORESOURCE_IO) {
1510 if (pio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301511 ql4_printk(KERN_WARNING, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07001512 "Invalid PCI I/O region size\n");
1513 pio = 0;
1514 }
1515 } else {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301516 ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001517 pio = 0;
1518 }
1519
1520 /* Use MMIO operations for all accesses. */
1521 mmio = pci_resource_start(ha->pdev, 1);
1522 mmio_len = pci_resource_len(ha->pdev, 1);
1523 mmio_flags = pci_resource_flags(ha->pdev, 1);
1524
1525 if (!(mmio_flags & IORESOURCE_MEM)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301526 ql4_printk(KERN_ERR, ha,
1527 "region #0 not an MMIO resource, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001528
1529 goto iospace_error_exit;
1530 }
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301531
David Somayajuluafaf5a22006-09-19 10:28:00 -07001532 if (mmio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301533 ql4_printk(KERN_ERR, ha,
1534 "Invalid PCI mem region size, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001535 goto iospace_error_exit;
1536 }
1537
1538 if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301539 ql4_printk(KERN_WARNING, ha,
1540 "Failed to reserve PIO/MMIO regions\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001541
1542 goto iospace_error_exit;
1543 }
1544
1545 ha->pio_address = pio;
1546 ha->pio_length = pio_len;
1547 ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
1548 if (!ha->reg) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301549 ql4_printk(KERN_ERR, ha,
1550 "cannot remap MMIO, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001551
1552 goto iospace_error_exit;
1553 }
1554
1555 return 0;
1556
1557iospace_error_exit:
1558 return -ENOMEM;
1559}
1560
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301561static struct isp_operations qla4xxx_isp_ops = {
1562 .iospace_config = qla4xxx_iospace_config,
1563 .pci_config = qla4xxx_pci_config,
1564 .disable_intrs = qla4xxx_disable_intrs,
1565 .enable_intrs = qla4xxx_enable_intrs,
1566 .start_firmware = qla4xxx_start_firmware,
1567 .intr_handler = qla4xxx_intr_handler,
1568 .interrupt_service_routine = qla4xxx_interrupt_service_routine,
1569 .reset_chip = qla4xxx_soft_reset,
1570 .reset_firmware = qla4xxx_hw_reset,
1571 .queue_iocb = qla4xxx_queue_iocb,
1572 .complete_iocb = qla4xxx_complete_iocb,
1573 .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
1574 .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
1575 .get_sys_info = qla4xxx_get_sys_info,
1576};
1577
1578static struct isp_operations qla4_8xxx_isp_ops = {
1579 .iospace_config = qla4_8xxx_iospace_config,
1580 .pci_config = qla4_8xxx_pci_config,
1581 .disable_intrs = qla4_8xxx_disable_intrs,
1582 .enable_intrs = qla4_8xxx_enable_intrs,
1583 .start_firmware = qla4_8xxx_load_risc,
1584 .intr_handler = qla4_8xxx_intr_handler,
1585 .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
1586 .reset_chip = qla4_8xxx_isp_reset,
1587 .reset_firmware = qla4_8xxx_stop_firmware,
1588 .queue_iocb = qla4_8xxx_queue_iocb,
1589 .complete_iocb = qla4_8xxx_complete_iocb,
1590 .rd_shdw_req_q_out = qla4_8xxx_rd_shdw_req_q_out,
1591 .rd_shdw_rsp_q_in = qla4_8xxx_rd_shdw_rsp_q_in,
1592 .get_sys_info = qla4_8xxx_get_sys_info,
1593};
1594
1595uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
1596{
1597 return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
1598}
1599
1600uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
1601{
1602 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
1603}
1604
1605uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
1606{
1607 return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
1608}
1609
1610uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
1611{
1612 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
1613}
1614
David Somayajuluafaf5a22006-09-19 10:28:00 -07001615/**
1616 * qla4xxx_probe_adapter - callback function to probe HBA
1617 * @pdev: pointer to pci_dev structure
1618 * @pci_device_id: pointer to pci_device entry
1619 *
1620 * This routine will probe for Qlogic 4xxx iSCSI host adapters.
1621 * It returns zero if successful. It also initializes all data necessary for
1622 * the driver.
1623 **/
1624static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
1625 const struct pci_device_id *ent)
1626{
1627 int ret = -ENODEV, status;
1628 struct Scsi_Host *host;
1629 struct scsi_qla_host *ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001630 uint8_t init_retry_count = 0;
1631 char buf[34];
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301632 struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
Prasanna Mumbaif9880e72011-03-21 03:34:26 -07001633 uint32_t dev_state;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001634
1635 if (pci_enable_device(pdev))
1636 return -1;
1637
1638 host = scsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha));
1639 if (host == NULL) {
1640 printk(KERN_WARNING
1641 "qla4xxx: Couldn't allocate host from scsi layer!\n");
1642 goto probe_disable_device;
1643 }
1644
1645 /* Clear our data area */
1646 ha = (struct scsi_qla_host *) host->hostdata;
1647 memset(ha, 0, sizeof(*ha));
1648
1649 /* Save the information from PCI BIOS. */
1650 ha->pdev = pdev;
1651 ha->host = host;
1652 ha->host_no = host->host_no;
1653
Lalit Chandivade2232be02010-07-30 14:38:47 +05301654 pci_enable_pcie_error_reporting(pdev);
1655
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301656 /* Setup Runtime configurable options */
1657 if (is_qla8022(ha)) {
1658 ha->isp_ops = &qla4_8xxx_isp_ops;
1659 rwlock_init(&ha->hw_lock);
1660 ha->qdr_sn_window = -1;
1661 ha->ddr_mn_window = -1;
1662 ha->curr_window = 255;
1663 ha->func_num = PCI_FUNC(ha->pdev->devfn);
1664 nx_legacy_intr = &legacy_intr[ha->func_num];
1665 ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
1666 ha->nx_legacy_intr.tgt_status_reg =
1667 nx_legacy_intr->tgt_status_reg;
1668 ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
1669 ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
1670 } else {
1671 ha->isp_ops = &qla4xxx_isp_ops;
1672 }
1673
Lalit Chandivade2232be02010-07-30 14:38:47 +05301674 /* Set EEH reset type to fundamental if required by hba */
1675 if (is_qla8022(ha))
1676 pdev->needs_freset = 1;
1677
David Somayajuluafaf5a22006-09-19 10:28:00 -07001678 /* Configure PCI I/O space. */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301679 ret = ha->isp_ops->iospace_config(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001680 if (ret)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301681 goto probe_failed_ioconfig;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001682
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301683 ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
David Somayajuluafaf5a22006-09-19 10:28:00 -07001684 pdev->device, pdev->irq, ha->reg);
1685
1686 qla4xxx_config_dma_addressing(ha);
1687
1688 /* Initialize lists and spinlocks. */
1689 INIT_LIST_HEAD(&ha->ddb_list);
1690 INIT_LIST_HEAD(&ha->free_srb_q);
1691
1692 mutex_init(&ha->mbox_sem);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301693 init_completion(&ha->mbx_intr_comp);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001694
1695 spin_lock_init(&ha->hardware_lock);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001696
1697 /* Allocate dma buffers */
1698 if (qla4xxx_mem_alloc(ha)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301699 ql4_printk(KERN_WARNING, ha,
1700 "[ERROR] Failed to allocate memory for adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001701
1702 ret = -ENOMEM;
1703 goto probe_failed;
1704 }
1705
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301706 if (is_qla8022(ha))
1707 (void) qla4_8xxx_get_flash_info(ha);
1708
David Somayajuluafaf5a22006-09-19 10:28:00 -07001709 /*
1710 * Initialize the Host adapter request/response queues and
1711 * firmware
1712 * NOTE: interrupts enabled upon successful completion
1713 */
1714 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301715 while ((!test_bit(AF_ONLINE, &ha->flags)) &&
1716 init_retry_count++ < MAX_INIT_RETRIES) {
Prasanna Mumbaif9880e72011-03-21 03:34:26 -07001717
1718 if (is_qla8022(ha)) {
1719 qla4_8xxx_idc_lock(ha);
1720 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
1721 qla4_8xxx_idc_unlock(ha);
1722 if (dev_state == QLA82XX_DEV_FAILED) {
1723 ql4_printk(KERN_WARNING, ha, "%s: don't retry "
1724 "initialize adapter. H/W is in failed state\n",
1725 __func__);
1726 break;
1727 }
1728 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001729 DEBUG2(printk("scsi: %s: retrying adapter initialization "
1730 "(%d)\n", __func__, init_retry_count));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301731
1732 if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
1733 continue;
1734
David Somayajuluafaf5a22006-09-19 10:28:00 -07001735 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1736 }
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301737
1738 if (!test_bit(AF_ONLINE, &ha->flags)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301739 ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001740
Lalit Chandivadefe998522010-12-02 22:12:36 -08001741 if (is_qla8022(ha) && ql4xdontresethba) {
1742 /* Put the device in failed state. */
1743 DEBUG2(printk(KERN_ERR "HW STATE: FAILED\n"));
1744 qla4_8xxx_idc_lock(ha);
1745 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
1746 QLA82XX_DEV_FAILED);
1747 qla4_8xxx_idc_unlock(ha);
1748 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001749 ret = -ENODEV;
1750 goto probe_failed;
1751 }
1752
1753 host->cmd_per_lun = 3;
1754 host->max_channel = 0;
1755 host->max_lun = MAX_LUNS - 1;
1756 host->max_id = MAX_TARGETS;
1757 host->max_cmd_len = IOCB_MAX_CDB_LEN;
1758 host->can_queue = MAX_SRBS ;
1759 host->transportt = qla4xxx_scsi_transport;
1760
1761 ret = scsi_init_shared_tag_map(host, MAX_SRBS);
1762 if (ret) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301763 ql4_printk(KERN_WARNING, ha,
1764 "scsi_init_shared_tag_map failed\n");
1765 goto probe_failed;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001766 }
1767
1768 /* Startup the kernel thread for this host adapter. */
1769 DEBUG2(printk("scsi: %s: Starting kernel thread for "
1770 "qla4xxx_dpc\n", __func__));
1771 sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
1772 ha->dpc_thread = create_singlethread_workqueue(buf);
1773 if (!ha->dpc_thread) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301774 ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001775 ret = -ENODEV;
1776 goto probe_failed;
1777 }
David Howellsc4028952006-11-22 14:57:56 +00001778 INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001779
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301780 /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
1781 * (which is called indirectly by qla4xxx_initialize_adapter),
1782 * so that irqs will be registered after crbinit but before
1783 * mbx_intr_enable.
1784 */
1785 if (!is_qla8022(ha)) {
1786 ret = qla4xxx_request_irqs(ha);
1787 if (ret) {
1788 ql4_printk(KERN_WARNING, ha, "Failed to reserve "
1789 "interrupt %d already in use.\n", pdev->irq);
1790 goto probe_failed;
1791 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001792 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001793
Lalit Chandivade2232be02010-07-30 14:38:47 +05301794 pci_save_state(ha->pdev);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301795 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001796
1797 /* Start timer thread. */
1798 qla4xxx_start_timer(ha, qla4xxx_timer, 1);
1799
1800 set_bit(AF_INIT_DONE, &ha->flags);
1801
1802 pci_set_drvdata(pdev, ha);
1803
1804 ret = scsi_add_host(host, &pdev->dev);
1805 if (ret)
1806 goto probe_failed;
1807
David Somayajuluafaf5a22006-09-19 10:28:00 -07001808 printk(KERN_INFO
1809 " QLogic iSCSI HBA Driver version: %s\n"
1810 " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
1811 qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
1812 ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
1813 ha->patch_number, ha->build_number);
Mike Christie921601b2008-01-31 13:36:49 -06001814 scsi_scan_host(host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001815 return 0;
1816
David Somayajuluafaf5a22006-09-19 10:28:00 -07001817probe_failed:
1818 qla4xxx_free_adapter(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301819
1820probe_failed_ioconfig:
Lalit Chandivade2232be02010-07-30 14:38:47 +05301821 pci_disable_pcie_error_reporting(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001822 scsi_host_put(ha->host);
1823
1824probe_disable_device:
1825 pci_disable_device(pdev);
1826
1827 return ret;
1828}
1829
1830/**
Karen Higgins7eece5a2011-03-21 03:34:29 -07001831 * qla4xxx_prevent_other_port_reinit - prevent other port from re-initialize
1832 * @ha: pointer to adapter structure
1833 *
1834 * Mark the other ISP-4xxx port to indicate that the driver is being removed,
1835 * so that the other port will not re-initialize while in the process of
1836 * removing the ha due to driver unload or hba hotplug.
1837 **/
1838static void qla4xxx_prevent_other_port_reinit(struct scsi_qla_host *ha)
1839{
1840 struct scsi_qla_host *other_ha = NULL;
1841 struct pci_dev *other_pdev = NULL;
1842 int fn = ISP4XXX_PCI_FN_2;
1843
1844 /*iscsi function numbers for ISP4xxx is 1 and 3*/
1845 if (PCI_FUNC(ha->pdev->devfn) & BIT_1)
1846 fn = ISP4XXX_PCI_FN_1;
1847
1848 other_pdev =
1849 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
1850 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
1851 fn));
1852
1853 /* Get other_ha if other_pdev is valid and state is enable*/
1854 if (other_pdev) {
1855 if (atomic_read(&other_pdev->enable_cnt)) {
1856 other_ha = pci_get_drvdata(other_pdev);
1857 if (other_ha) {
1858 set_bit(AF_HA_REMOVAL, &other_ha->flags);
1859 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: "
1860 "Prevent %s reinit\n", __func__,
1861 dev_name(&other_ha->pdev->dev)));
1862 }
1863 }
1864 pci_dev_put(other_pdev);
1865 }
1866}
1867
1868/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07001869 * qla4xxx_remove_adapter - calback function to remove adapter.
1870 * @pci_dev: PCI device pointer
1871 **/
1872static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
1873{
1874 struct scsi_qla_host *ha;
1875
1876 ha = pci_get_drvdata(pdev);
1877
Karen Higgins7eece5a2011-03-21 03:34:29 -07001878 if (!is_qla8022(ha))
1879 qla4xxx_prevent_other_port_reinit(ha);
David C Somayajulubee4fe82007-05-23 18:03:32 -07001880
David Somayajuluafaf5a22006-09-19 10:28:00 -07001881 /* remove devs from iscsi_sessions to scsi_devices */
1882 qla4xxx_free_ddb_list(ha);
1883
1884 scsi_remove_host(ha->host);
1885
1886 qla4xxx_free_adapter(ha);
1887
1888 scsi_host_put(ha->host);
1889
Lalit Chandivade2232be02010-07-30 14:38:47 +05301890 pci_disable_pcie_error_reporting(pdev);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301891 pci_disable_device(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001892 pci_set_drvdata(pdev, NULL);
1893}
1894
1895/**
1896 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
1897 * @ha: HA context
1898 *
1899 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
1900 * supported addressing method.
1901 */
Adrian Bunk47975472007-04-26 00:35:16 -07001902static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001903{
1904 int retval;
1905
1906 /* Update our PCI device dma_mask for full 64 bit mask */
Yang Hongyang6a355282009-04-06 19:01:13 -07001907 if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
1908 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001909 dev_dbg(&ha->pdev->dev,
1910 "Failed to set 64 bit PCI consistent mask; "
1911 "using 32 bit.\n");
1912 retval = pci_set_consistent_dma_mask(ha->pdev,
Yang Hongyang284901a2009-04-06 19:01:15 -07001913 DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001914 }
1915 } else
Yang Hongyang284901a2009-04-06 19:01:15 -07001916 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001917}
1918
1919static int qla4xxx_slave_alloc(struct scsi_device *sdev)
1920{
1921 struct iscsi_cls_session *sess = starget_to_session(sdev->sdev_target);
1922 struct ddb_entry *ddb = sess->dd_data;
Vikas Chaudhary8bb40332011-03-21 03:34:31 -07001923 int queue_depth = QL4_DEF_QDEPTH;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001924
1925 sdev->hostdata = ddb;
1926 sdev->tagged_supported = 1;
Vikas Chaudhary8bb40332011-03-21 03:34:31 -07001927
1928 if (ql4xmaxqdepth != 0 && ql4xmaxqdepth <= 0xffffU)
1929 queue_depth = ql4xmaxqdepth;
1930
1931 scsi_activate_tcq(sdev, queue_depth);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001932 return 0;
1933}
1934
1935static int qla4xxx_slave_configure(struct scsi_device *sdev)
1936{
1937 sdev->tagged_supported = 1;
1938 return 0;
1939}
1940
1941static void qla4xxx_slave_destroy(struct scsi_device *sdev)
1942{
1943 scsi_deactivate_tcq(sdev, 1);
1944}
1945
1946/**
1947 * qla4xxx_del_from_active_array - returns an active srb
1948 * @ha: Pointer to host adapter structure.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001949 * @index: index into the active_array
David Somayajuluafaf5a22006-09-19 10:28:00 -07001950 *
1951 * This routine removes and returns the srb at the specified index
1952 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301953struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
1954 uint32_t index)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001955{
1956 struct srb *srb = NULL;
Vikas Chaudhary53698872010-04-28 11:41:59 +05301957 struct scsi_cmnd *cmd = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001958
Vikas Chaudhary53698872010-04-28 11:41:59 +05301959 cmd = scsi_host_find_tag(ha->host, index);
1960 if (!cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001961 return srb;
1962
Vikas Chaudhary53698872010-04-28 11:41:59 +05301963 srb = (struct srb *)CMD_SP(cmd);
1964 if (!srb)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001965 return srb;
1966
1967 /* update counters */
1968 if (srb->flags & SRB_DMA_VALID) {
1969 ha->req_q_count += srb->iocb_cnt;
1970 ha->iocb_cnt -= srb->iocb_cnt;
1971 if (srb->cmd)
Vikas Chaudhary53698872010-04-28 11:41:59 +05301972 srb->cmd->host_scribble =
1973 (unsigned char *)(unsigned long) MAX_SRBS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001974 }
1975 return srb;
1976}
1977
1978/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07001979 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301980 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001981 * @cmd: Scsi Command to wait on.
1982 *
1983 * This routine waits for the command to be returned by the Firmware
1984 * for some max time.
1985 **/
1986static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
1987 struct scsi_cmnd *cmd)
1988{
1989 int done = 0;
1990 struct srb *rp;
1991 uint32_t max_wait_time = EH_WAIT_CMD_TOV;
Lalit Chandivade2232be02010-07-30 14:38:47 +05301992 int ret = SUCCESS;
1993
1994 /* Dont wait on command if PCI error is being handled
1995 * by PCI AER driver
1996 */
1997 if (unlikely(pci_channel_offline(ha->pdev)) ||
1998 (test_bit(AF_EEH_BUSY, &ha->flags))) {
1999 ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
2000 ha->host_no, __func__);
2001 return ret;
2002 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002003
2004 do {
2005 /* Checking to see if its returned to OS */
Vikas Chaudhary53698872010-04-28 11:41:59 +05302006 rp = (struct srb *) CMD_SP(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002007 if (rp == NULL) {
2008 done++;
2009 break;
2010 }
2011
2012 msleep(2000);
2013 } while (max_wait_time--);
2014
2015 return done;
2016}
2017
2018/**
2019 * qla4xxx_wait_for_hba_online - waits for HBA to come online
2020 * @ha: Pointer to host adapter structure
2021 **/
2022static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
2023{
2024 unsigned long wait_online;
2025
Vikas Chaudharyf581a3f2010-10-06 22:47:48 -07002026 wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002027 while (time_before(jiffies, wait_online)) {
2028
2029 if (adapter_up(ha))
2030 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002031
2032 msleep(2000);
2033 }
2034
2035 return QLA_ERROR;
2036}
2037
2038/**
Mike Christiece545032008-02-29 18:25:20 -06002039 * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02002040 * @ha: pointer to HBA
David Somayajuluafaf5a22006-09-19 10:28:00 -07002041 * @t: target id
2042 * @l: lun id
2043 *
2044 * This function waits for all outstanding commands to a lun to complete. It
2045 * returns 0 if all pending commands are returned and 1 otherwise.
2046 **/
Mike Christiece545032008-02-29 18:25:20 -06002047static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
2048 struct scsi_target *stgt,
2049 struct scsi_device *sdev)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002050{
2051 int cnt;
2052 int status = 0;
2053 struct scsi_cmnd *cmd;
2054
2055 /*
Mike Christiece545032008-02-29 18:25:20 -06002056 * Waiting for all commands for the designated target or dev
2057 * in the active array
David Somayajuluafaf5a22006-09-19 10:28:00 -07002058 */
2059 for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
2060 cmd = scsi_host_find_tag(ha->host, cnt);
Mike Christiece545032008-02-29 18:25:20 -06002061 if (cmd && stgt == scsi_target(cmd->device) &&
2062 (!sdev || sdev == cmd->device)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002063 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
2064 status++;
2065 break;
2066 }
2067 }
2068 }
2069 return status;
2070}
2071
2072/**
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302073 * qla4xxx_eh_abort - callback for abort task.
2074 * @cmd: Pointer to Linux's SCSI command structure
2075 *
2076 * This routine is called by the Linux OS to abort the specified
2077 * command.
2078 **/
2079static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
2080{
2081 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2082 unsigned int id = cmd->device->id;
2083 unsigned int lun = cmd->device->lun;
Mike Christie92b3e5b2010-10-06 22:51:17 -07002084 unsigned long flags;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302085 struct srb *srb = NULL;
2086 int ret = SUCCESS;
2087 int wait = 0;
2088
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302089 ql4_printk(KERN_INFO, ha,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002090 "scsi%ld:%d:%d: Abort command issued cmd=%p\n",
2091 ha->host_no, id, lun, cmd);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302092
Mike Christie92b3e5b2010-10-06 22:51:17 -07002093 spin_lock_irqsave(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302094 srb = (struct srb *) CMD_SP(cmd);
Mike Christie92b3e5b2010-10-06 22:51:17 -07002095 if (!srb) {
2096 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302097 return SUCCESS;
Mike Christie92b3e5b2010-10-06 22:51:17 -07002098 }
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302099 kref_get(&srb->srb_ref);
Mike Christie92b3e5b2010-10-06 22:51:17 -07002100 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302101
2102 if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
2103 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
2104 ha->host_no, id, lun));
2105 ret = FAILED;
2106 } else {
2107 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
2108 ha->host_no, id, lun));
2109 wait = 1;
2110 }
2111
2112 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
2113
2114 /* Wait for command to complete */
2115 if (wait) {
2116 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
2117 DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
2118 ha->host_no, id, lun));
2119 ret = FAILED;
2120 }
2121 }
2122
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302123 ql4_printk(KERN_INFO, ha,
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302124 "scsi%ld:%d:%d: Abort command - %s\n",
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002125 ha->host_no, id, lun, (ret == SUCCESS) ? "succeeded" : "failed");
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302126
2127 return ret;
2128}
2129
2130/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002131 * qla4xxx_eh_device_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 all luns on the
2135 * specified target.
2136 **/
2137static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
2138{
2139 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2140 struct ddb_entry *ddb_entry = cmd->device->hostdata;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002141 int ret = FAILED, stat;
2142
Karen Higgins612f7342009-07-15 15:03:01 -05002143 if (!ddb_entry)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002144 return ret;
2145
Mike Christiec01be6d2010-07-22 16:59:49 +05302146 ret = iscsi_block_scsi_eh(cmd);
2147 if (ret)
2148 return ret;
2149 ret = FAILED;
2150
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302151 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002152 "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
2153 cmd->device->channel, cmd->device->id, cmd->device->lun);
2154
2155 DEBUG2(printk(KERN_INFO
2156 "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
2157 "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
Jens Axboe242f9dc2008-09-14 05:55:09 -07002158 cmd, jiffies, cmd->request->timeout / HZ,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002159 ha->dpc_flags, cmd->result, cmd->allowed));
2160
2161 /* FIXME: wait for hba to go online */
2162 stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
2163 if (stat != QLA_SUCCESS) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302164 ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002165 goto eh_dev_reset_done;
2166 }
2167
Mike Christiece545032008-02-29 18:25:20 -06002168 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2169 cmd->device)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302170 ql4_printk(KERN_INFO, ha,
Mike Christiece545032008-02-29 18:25:20 -06002171 "DEVICE RESET FAILED - waiting for "
2172 "commands.\n");
2173 goto eh_dev_reset_done;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002174 }
2175
David C Somayajulu9d562912008-03-19 11:23:03 -07002176 /* Send marker. */
2177 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2178 MM_LUN_RESET) != QLA_SUCCESS)
2179 goto eh_dev_reset_done;
2180
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302181 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002182 "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
2183 ha->host_no, cmd->device->channel, cmd->device->id,
2184 cmd->device->lun);
2185
2186 ret = SUCCESS;
2187
2188eh_dev_reset_done:
2189
2190 return ret;
2191}
2192
2193/**
Mike Christiece545032008-02-29 18:25:20 -06002194 * qla4xxx_eh_target_reset - callback for target reset.
2195 * @cmd: Pointer to Linux's SCSI command structure
2196 *
2197 * This routine is called by the Linux OS to reset the target.
2198 **/
2199static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
2200{
2201 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2202 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christiec01be6d2010-07-22 16:59:49 +05302203 int stat, ret;
Mike Christiece545032008-02-29 18:25:20 -06002204
2205 if (!ddb_entry)
2206 return FAILED;
2207
Mike Christiec01be6d2010-07-22 16:59:49 +05302208 ret = iscsi_block_scsi_eh(cmd);
2209 if (ret)
2210 return ret;
2211
Mike Christiece545032008-02-29 18:25:20 -06002212 starget_printk(KERN_INFO, scsi_target(cmd->device),
2213 "WARM TARGET RESET ISSUED.\n");
2214
2215 DEBUG2(printk(KERN_INFO
2216 "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
2217 "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
Jens Axboe242f9dc2008-09-14 05:55:09 -07002218 ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
Mike Christiece545032008-02-29 18:25:20 -06002219 ha->dpc_flags, cmd->result, cmd->allowed));
2220
2221 stat = qla4xxx_reset_target(ha, ddb_entry);
2222 if (stat != QLA_SUCCESS) {
2223 starget_printk(KERN_INFO, scsi_target(cmd->device),
2224 "WARM TARGET RESET FAILED.\n");
2225 return FAILED;
2226 }
2227
Mike Christiece545032008-02-29 18:25:20 -06002228 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2229 NULL)) {
2230 starget_printk(KERN_INFO, scsi_target(cmd->device),
2231 "WARM TARGET DEVICE RESET FAILED - "
2232 "waiting for commands.\n");
2233 return FAILED;
2234 }
2235
David C Somayajulu9d562912008-03-19 11:23:03 -07002236 /* Send marker. */
2237 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2238 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
2239 starget_printk(KERN_INFO, scsi_target(cmd->device),
2240 "WARM TARGET DEVICE RESET FAILED - "
2241 "marker iocb failed.\n");
2242 return FAILED;
2243 }
2244
Mike Christiece545032008-02-29 18:25:20 -06002245 starget_printk(KERN_INFO, scsi_target(cmd->device),
2246 "WARM TARGET RESET SUCCEEDED.\n");
2247 return SUCCESS;
2248}
2249
2250/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002251 * qla4xxx_eh_host_reset - kernel callback
2252 * @cmd: Pointer to Linux's SCSI command structure
2253 *
2254 * This routine is invoked by the Linux kernel to perform fatal error
2255 * recovery on the specified adapter.
2256 **/
2257static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
2258{
2259 int return_status = FAILED;
2260 struct scsi_qla_host *ha;
2261
2262 ha = (struct scsi_qla_host *) cmd->device->host->hostdata;
2263
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302264 if (ql4xdontresethba) {
2265 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
2266 ha->host_no, __func__));
2267 return FAILED;
2268 }
2269
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302270 ql4_printk(KERN_INFO, ha,
Karen Higginsdca05c42009-07-15 15:03:00 -05002271 "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002272 cmd->device->channel, cmd->device->id, cmd->device->lun);
2273
2274 if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
2275 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
2276 "DEAD.\n", ha->host_no, cmd->device->channel,
2277 __func__));
2278
2279 return FAILED;
2280 }
2281
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302282 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
2283 if (is_qla8022(ha))
2284 set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2285 else
2286 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2287 }
Mike Christie50a29ae2008-03-04 13:26:53 -06002288
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302289 if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002290 return_status = SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002291
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302292 ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002293 return_status == FAILED ? "FAILED" : "SUCCEEDED");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002294
2295 return return_status;
2296}
2297
Lalit Chandivade2232be02010-07-30 14:38:47 +05302298/* PCI AER driver recovers from all correctable errors w/o
2299 * driver intervention. For uncorrectable errors PCI AER
2300 * driver calls the following device driver's callbacks
2301 *
2302 * - Fatal Errors - link_reset
2303 * - Non-Fatal Errors - driver's pci_error_detected() which
2304 * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
2305 *
2306 * PCI AER driver calls
2307 * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
2308 * returns RECOVERED or NEED_RESET if fw_hung
2309 * NEED_RESET - driver's slot_reset()
2310 * DISCONNECT - device is dead & cannot recover
2311 * RECOVERED - driver's pci_resume()
2312 */
2313static pci_ers_result_t
2314qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
2315{
2316 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2317
2318 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
2319 ha->host_no, __func__, state);
2320
2321 if (!is_aer_supported(ha))
2322 return PCI_ERS_RESULT_NONE;
2323
2324 switch (state) {
2325 case pci_channel_io_normal:
2326 clear_bit(AF_EEH_BUSY, &ha->flags);
2327 return PCI_ERS_RESULT_CAN_RECOVER;
2328 case pci_channel_io_frozen:
2329 set_bit(AF_EEH_BUSY, &ha->flags);
2330 qla4xxx_mailbox_premature_completion(ha);
2331 qla4xxx_free_irqs(ha);
2332 pci_disable_device(pdev);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002333 /* Return back all IOs */
2334 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
Lalit Chandivade2232be02010-07-30 14:38:47 +05302335 return PCI_ERS_RESULT_NEED_RESET;
2336 case pci_channel_io_perm_failure:
2337 set_bit(AF_EEH_BUSY, &ha->flags);
2338 set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
2339 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
2340 return PCI_ERS_RESULT_DISCONNECT;
2341 }
2342 return PCI_ERS_RESULT_NEED_RESET;
2343}
2344
2345/**
2346 * qla4xxx_pci_mmio_enabled() gets called if
2347 * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
2348 * and read/write to the device still works.
2349 **/
2350static pci_ers_result_t
2351qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
2352{
2353 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2354
2355 if (!is_aer_supported(ha))
2356 return PCI_ERS_RESULT_NONE;
2357
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002358 return PCI_ERS_RESULT_RECOVERED;
Lalit Chandivade2232be02010-07-30 14:38:47 +05302359}
2360
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002361static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
Lalit Chandivade2232be02010-07-30 14:38:47 +05302362{
2363 uint32_t rval = QLA_ERROR;
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002364 uint32_t ret = 0;
Lalit Chandivade2232be02010-07-30 14:38:47 +05302365 int fn;
2366 struct pci_dev *other_pdev = NULL;
2367
2368 ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
2369
2370 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2371
2372 if (test_bit(AF_ONLINE, &ha->flags)) {
2373 clear_bit(AF_ONLINE, &ha->flags);
2374 qla4xxx_mark_all_devices_missing(ha);
2375 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
Lalit Chandivade2232be02010-07-30 14:38:47 +05302376 }
2377
2378 fn = PCI_FUNC(ha->pdev->devfn);
2379 while (fn > 0) {
2380 fn--;
2381 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
2382 "func %x\n", ha->host_no, __func__, fn);
2383 /* Get the pci device given the domain, bus,
2384 * slot/function number */
2385 other_pdev =
2386 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
2387 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
2388 fn));
2389
2390 if (!other_pdev)
2391 continue;
2392
2393 if (atomic_read(&other_pdev->enable_cnt)) {
2394 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
2395 "func in enabled state%x\n", ha->host_no,
2396 __func__, fn);
2397 pci_dev_put(other_pdev);
2398 break;
2399 }
2400 pci_dev_put(other_pdev);
2401 }
2402
2403 /* The first function on the card, the reset owner will
2404 * start & initialize the firmware. The other functions
2405 * on the card will reset the firmware context
2406 */
2407 if (!fn) {
2408 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
2409 "0x%x is the owner\n", ha->host_no, __func__,
2410 ha->pdev->devfn);
2411
2412 qla4_8xxx_idc_lock(ha);
2413 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2414 QLA82XX_DEV_COLD);
2415
2416 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
2417 QLA82XX_IDC_VERSION);
2418
2419 qla4_8xxx_idc_unlock(ha);
2420 clear_bit(AF_FW_RECOVERY, &ha->flags);
2421 rval = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
2422 qla4_8xxx_idc_lock(ha);
2423
2424 if (rval != QLA_SUCCESS) {
2425 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
2426 "FAILED\n", ha->host_no, __func__);
2427 qla4_8xxx_clear_drv_active(ha);
2428 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2429 QLA82XX_DEV_FAILED);
2430 } else {
2431 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
2432 "READY\n", ha->host_no, __func__);
2433 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2434 QLA82XX_DEV_READY);
2435 /* Clear driver state register */
2436 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
2437 qla4_8xxx_set_drv_active(ha);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002438 ret = qla4xxx_request_irqs(ha);
2439 if (ret) {
2440 ql4_printk(KERN_WARNING, ha, "Failed to "
2441 "reserve interrupt %d already in use.\n",
2442 ha->pdev->irq);
2443 rval = QLA_ERROR;
2444 } else {
2445 ha->isp_ops->enable_intrs(ha);
2446 rval = QLA_SUCCESS;
2447 }
Lalit Chandivade2232be02010-07-30 14:38:47 +05302448 }
2449 qla4_8xxx_idc_unlock(ha);
2450 } else {
2451 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
2452 "the reset owner\n", ha->host_no, __func__,
2453 ha->pdev->devfn);
2454 if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
2455 QLA82XX_DEV_READY)) {
2456 clear_bit(AF_FW_RECOVERY, &ha->flags);
2457 rval = qla4xxx_initialize_adapter(ha,
2458 PRESERVE_DDB_LIST);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002459 if (rval == QLA_SUCCESS) {
2460 ret = qla4xxx_request_irqs(ha);
2461 if (ret) {
2462 ql4_printk(KERN_WARNING, ha, "Failed to"
2463 " reserve interrupt %d already in"
2464 " use.\n", ha->pdev->irq);
2465 rval = QLA_ERROR;
2466 } else {
2467 ha->isp_ops->enable_intrs(ha);
2468 rval = QLA_SUCCESS;
2469 }
2470 }
Lalit Chandivade2232be02010-07-30 14:38:47 +05302471 qla4_8xxx_idc_lock(ha);
2472 qla4_8xxx_set_drv_active(ha);
2473 qla4_8xxx_idc_unlock(ha);
2474 }
2475 }
2476 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2477 return rval;
2478}
2479
2480static pci_ers_result_t
2481qla4xxx_pci_slot_reset(struct pci_dev *pdev)
2482{
2483 pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
2484 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2485 int rc;
2486
2487 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
2488 ha->host_no, __func__);
2489
2490 if (!is_aer_supported(ha))
2491 return PCI_ERS_RESULT_NONE;
2492
2493 /* Restore the saved state of PCIe device -
2494 * BAR registers, PCI Config space, PCIX, MSI,
2495 * IOV states
2496 */
2497 pci_restore_state(pdev);
2498
2499 /* pci_restore_state() clears the saved_state flag of the device
2500 * save restored state which resets saved_state flag
2501 */
2502 pci_save_state(pdev);
2503
2504 /* Initialize device or resume if in suspended state */
2505 rc = pci_enable_device(pdev);
2506 if (rc) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002507 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Can't re-enable "
Lalit Chandivade2232be02010-07-30 14:38:47 +05302508 "device after reset\n", ha->host_no, __func__);
2509 goto exit_slot_reset;
2510 }
2511
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002512 ha->isp_ops->disable_intrs(ha);
Lalit Chandivade2232be02010-07-30 14:38:47 +05302513
2514 if (is_qla8022(ha)) {
2515 if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
2516 ret = PCI_ERS_RESULT_RECOVERED;
2517 goto exit_slot_reset;
2518 } else
2519 goto exit_slot_reset;
2520 }
2521
2522exit_slot_reset:
2523 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
2524 "device after reset\n", ha->host_no, __func__, ret);
2525 return ret;
2526}
2527
2528static void
2529qla4xxx_pci_resume(struct pci_dev *pdev)
2530{
2531 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2532 int ret;
2533
2534 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
2535 ha->host_no, __func__);
2536
2537 ret = qla4xxx_wait_for_hba_online(ha);
2538 if (ret != QLA_SUCCESS) {
2539 ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
2540 "resume I/O from slot/link_reset\n", ha->host_no,
2541 __func__);
2542 }
2543
2544 pci_cleanup_aer_uncorrect_error_status(pdev);
2545 clear_bit(AF_EEH_BUSY, &ha->flags);
2546}
2547
2548static struct pci_error_handlers qla4xxx_err_handler = {
2549 .error_detected = qla4xxx_pci_error_detected,
2550 .mmio_enabled = qla4xxx_pci_mmio_enabled,
2551 .slot_reset = qla4xxx_pci_slot_reset,
2552 .resume = qla4xxx_pci_resume,
2553};
2554
David Somayajuluafaf5a22006-09-19 10:28:00 -07002555static struct pci_device_id qla4xxx_pci_tbl[] = {
2556 {
2557 .vendor = PCI_VENDOR_ID_QLOGIC,
2558 .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
2559 .subvendor = PCI_ANY_ID,
2560 .subdevice = PCI_ANY_ID,
2561 },
2562 {
2563 .vendor = PCI_VENDOR_ID_QLOGIC,
2564 .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
2565 .subvendor = PCI_ANY_ID,
2566 .subdevice = PCI_ANY_ID,
2567 },
David C Somayajulud9150582006-11-15 17:38:40 -08002568 {
2569 .vendor = PCI_VENDOR_ID_QLOGIC,
2570 .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
2571 .subvendor = PCI_ANY_ID,
2572 .subdevice = PCI_ANY_ID,
2573 },
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302574 {
2575 .vendor = PCI_VENDOR_ID_QLOGIC,
2576 .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
2577 .subvendor = PCI_ANY_ID,
2578 .subdevice = PCI_ANY_ID,
2579 },
David Somayajuluafaf5a22006-09-19 10:28:00 -07002580 {0, 0},
2581};
2582MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
2583
Adrian Bunk47975472007-04-26 00:35:16 -07002584static struct pci_driver qla4xxx_pci_driver = {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002585 .name = DRIVER_NAME,
2586 .id_table = qla4xxx_pci_tbl,
2587 .probe = qla4xxx_probe_adapter,
2588 .remove = qla4xxx_remove_adapter,
Lalit Chandivade2232be02010-07-30 14:38:47 +05302589 .err_handler = &qla4xxx_err_handler,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002590};
2591
2592static int __init qla4xxx_module_init(void)
2593{
2594 int ret;
2595
2596 /* Allocate cache for SRBs. */
2597 srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09002598 SLAB_HWCACHE_ALIGN, NULL);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002599 if (srb_cachep == NULL) {
2600 printk(KERN_ERR
2601 "%s: Unable to allocate SRB cache..."
2602 "Failing load!\n", DRIVER_NAME);
2603 ret = -ENOMEM;
2604 goto no_srp_cache;
2605 }
2606
2607 /* Derive version string. */
2608 strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
Andrew Vasquez11010fe2006-10-06 09:54:59 -07002609 if (ql4xextended_error_logging)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002610 strcat(qla4xxx_version_str, "-debug");
2611
2612 qla4xxx_scsi_transport =
2613 iscsi_register_transport(&qla4xxx_iscsi_transport);
2614 if (!qla4xxx_scsi_transport){
2615 ret = -ENODEV;
2616 goto release_srb_cache;
2617 }
2618
David Somayajuluafaf5a22006-09-19 10:28:00 -07002619 ret = pci_register_driver(&qla4xxx_pci_driver);
2620 if (ret)
2621 goto unregister_transport;
2622
2623 printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
2624 return 0;
Doug Maxey5ae16db2006-10-05 23:50:07 -05002625
David Somayajuluafaf5a22006-09-19 10:28:00 -07002626unregister_transport:
2627 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
2628release_srb_cache:
2629 kmem_cache_destroy(srb_cachep);
2630no_srp_cache:
2631 return ret;
2632}
2633
2634static void __exit qla4xxx_module_exit(void)
2635{
David Somayajuluafaf5a22006-09-19 10:28:00 -07002636 pci_unregister_driver(&qla4xxx_pci_driver);
2637 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
2638 kmem_cache_destroy(srb_cachep);
2639}
2640
2641module_init(qla4xxx_module_init);
2642module_exit(qla4xxx_module_exit);
2643
2644MODULE_AUTHOR("QLogic Corporation");
2645MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
2646MODULE_LICENSE("GPL");
2647MODULE_VERSION(QLA4XXX_DRIVER_VERSION);