blob: 409b20d62ef55da70df5d46f9a9dd236e6c3b61c [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 */
32int ql4xdiscoverywait = 60;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053033module_param(ql4xdiscoverywait, int, S_IRUGO | S_IWUSR);
David Somayajuluafaf5a22006-09-19 10:28:00 -070034MODULE_PARM_DESC(ql4xdiscoverywait, "Discovery wait time");
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053035
David Somayajuluafaf5a22006-09-19 10:28:00 -070036int ql4xdontresethba = 0;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053037module_param(ql4xdontresethba, int, S_IRUGO | S_IWUSR);
David Somayajuluafaf5a22006-09-19 10:28:00 -070038MODULE_PARM_DESC(ql4xdontresethba,
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053039 "Don't reset the HBA for driver recovery \n"
40 " 0 - It will reset HBA (Default)\n"
41 " 1 - It will NOT reset HBA");
David Somayajuluafaf5a22006-09-19 10:28:00 -070042
Andrew Vasquez11010fe2006-10-06 09:54:59 -070043int ql4xextended_error_logging = 0; /* 0 = off, 1 = log errors */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053044module_param(ql4xextended_error_logging, int, S_IRUGO | S_IWUSR);
Andrew Vasquez11010fe2006-10-06 09:54:59 -070045MODULE_PARM_DESC(ql4xextended_error_logging,
David Somayajuluafaf5a22006-09-19 10:28:00 -070046 "Option to enable extended error logging, "
47 "Default is 0 - no logging, 1 - debug logging");
48
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053049int ql4xenablemsix = 1;
50module_param(ql4xenablemsix, int, S_IRUGO|S_IWUSR);
51MODULE_PARM_DESC(ql4xenablemsix,
52 "Set to enable MSI or MSI-X interrupt mechanism.\n"
53 " 0 = enable INTx interrupt mechanism.\n"
54 " 1 = enable MSI-X interrupt mechanism (Default).\n"
55 " 2 = enable MSI interrupt mechanism.");
David C Somayajulu477ffb92007-01-22 12:26:11 -080056
Mike Christied510d962008-07-11 19:50:33 -050057#define QL4_DEF_QDEPTH 32
58
David Somayajuluafaf5a22006-09-19 10:28:00 -070059/*
60 * SCSI host template entry points
61 */
Adrian Bunk47975472007-04-26 00:35:16 -070062static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -070063
64/*
65 * iSCSI template entry points
66 */
Mike Christie2174a042007-05-30 12:57:10 -050067static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
68 enum iscsi_tgt_dscvr type, uint32_t enable,
69 struct sockaddr *dst_addr);
David Somayajuluafaf5a22006-09-19 10:28:00 -070070static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
71 enum iscsi_param param, char *buf);
72static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
73 enum iscsi_param param, char *buf);
Mike Christieaa1e93a2007-05-30 12:57:09 -050074static int qla4xxx_host_get_param(struct Scsi_Host *shost,
75 enum iscsi_host_param param, char *buf);
David Somayajuluafaf5a22006-09-19 10:28:00 -070076static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session);
Mike Christie5c656af2009-07-15 15:02:59 -050077static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
David Somayajuluafaf5a22006-09-19 10:28:00 -070078
79/*
80 * SCSI host template entry points
81 */
Jeff Garzikf2812332010-11-16 02:10:29 -050082static int qla4xxx_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmd);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +053083static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -070084static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
Mike Christiece545032008-02-29 18:25:20 -060085static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -070086static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
87static int qla4xxx_slave_alloc(struct scsi_device *device);
88static int qla4xxx_slave_configure(struct scsi_device *device);
89static void qla4xxx_slave_destroy(struct scsi_device *sdev);
Mike Christie024f8012008-03-04 13:26:54 -060090static void qla4xxx_scan_start(struct Scsi_Host *shost);
David Somayajuluafaf5a22006-09-19 10:28:00 -070091
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053092static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
93 QLA82XX_LEGACY_INTR_CONFIG;
94
David Somayajuluafaf5a22006-09-19 10:28:00 -070095static struct scsi_host_template qla4xxx_driver_template = {
96 .module = THIS_MODULE,
97 .name = DRIVER_NAME,
98 .proc_name = DRIVER_NAME,
99 .queuecommand = qla4xxx_queuecommand,
100
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530101 .eh_abort_handler = qla4xxx_eh_abort,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700102 .eh_device_reset_handler = qla4xxx_eh_device_reset,
Mike Christiece545032008-02-29 18:25:20 -0600103 .eh_target_reset_handler = qla4xxx_eh_target_reset,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700104 .eh_host_reset_handler = qla4xxx_eh_host_reset,
Mike Christie5c656af2009-07-15 15:02:59 -0500105 .eh_timed_out = qla4xxx_eh_cmd_timed_out,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700106
107 .slave_configure = qla4xxx_slave_configure,
108 .slave_alloc = qla4xxx_slave_alloc,
109 .slave_destroy = qla4xxx_slave_destroy,
110
Mike Christie921601b2008-01-31 13:36:49 -0600111 .scan_finished = iscsi_scan_finished,
Mike Christie024f8012008-03-04 13:26:54 -0600112 .scan_start = qla4xxx_scan_start,
Mike Christie921601b2008-01-31 13:36:49 -0600113
David Somayajuluafaf5a22006-09-19 10:28:00 -0700114 .this_id = -1,
115 .cmd_per_lun = 3,
116 .use_clustering = ENABLE_CLUSTERING,
117 .sg_tablesize = SG_ALL,
118
119 .max_sectors = 0xFFFF,
120};
121
122static struct iscsi_transport qla4xxx_iscsi_transport = {
123 .owner = THIS_MODULE,
124 .name = DRIVER_NAME,
Mike Christied8196ed2007-05-30 12:57:25 -0500125 .caps = CAP_FW_DB | CAP_SENDTARGETS_OFFLOAD |
126 CAP_DATA_PATH_OFFLOAD,
Mike Christieaa1e93a2007-05-30 12:57:09 -0500127 .param_mask = ISCSI_CONN_PORT | ISCSI_CONN_ADDRESS |
Vikas Chaudhary3b2bef12010-07-10 14:51:30 +0530128 ISCSI_TARGET_NAME | ISCSI_TPGT |
129 ISCSI_TARGET_ALIAS,
Mike Christie8ad57812007-05-30 12:57:13 -0500130 .host_param_mask = ISCSI_HOST_HWADDRESS |
Mike Christie22236962007-05-30 12:57:24 -0500131 ISCSI_HOST_IPADDRESS |
Mike Christie8ad57812007-05-30 12:57:13 -0500132 ISCSI_HOST_INITIATOR_NAME,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700133 .tgt_dscvr = qla4xxx_tgt_dscvr,
134 .get_conn_param = qla4xxx_conn_get_param,
135 .get_session_param = qla4xxx_sess_get_param,
Mike Christieaa1e93a2007-05-30 12:57:09 -0500136 .get_host_param = qla4xxx_host_get_param,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700137 .session_recovery_timedout = qla4xxx_recovery_timedout,
138};
139
140static struct scsi_transport_template *qla4xxx_scsi_transport;
141
Mike Christie5c656af2009-07-15 15:02:59 -0500142static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
143{
144 struct iscsi_cls_session *session;
145 struct ddb_entry *ddb_entry;
146
147 session = starget_to_session(scsi_target(sc->device));
148 ddb_entry = session->dd_data;
149
150 /* if we are not logged in then the LLD is going to clean up the cmd */
151 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
152 return BLK_EH_RESET_TIMER;
153 else
154 return BLK_EH_NOT_HANDLED;
155}
156
David Somayajuluafaf5a22006-09-19 10:28:00 -0700157static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session)
158{
159 struct ddb_entry *ddb_entry = session->dd_data;
160 struct scsi_qla_host *ha = ddb_entry->ha;
161
Mike Christie568d3032008-01-31 13:36:47 -0600162 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
163 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700164
Vikas Chaudhary3013cea2010-07-30 14:25:46 +0530165 DEBUG2(printk("scsi%ld: %s: ddb [%d] session recovery timeout "
Mike Christie568d3032008-01-31 13:36:47 -0600166 "of (%d) secs exhausted, marking device DEAD.\n",
167 ha->host_no, __func__, ddb_entry->fw_ddb_index,
Vikas Chaudhary3013cea2010-07-30 14:25:46 +0530168 QL4_SESS_RECOVERY_TMO));
Mike Christie568d3032008-01-31 13:36:47 -0600169 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700170}
171
Mike Christieaa1e93a2007-05-30 12:57:09 -0500172static int qla4xxx_host_get_param(struct Scsi_Host *shost,
173 enum iscsi_host_param param, char *buf)
174{
175 struct scsi_qla_host *ha = to_qla_host(shost);
176 int len;
177
178 switch (param) {
179 case ISCSI_HOST_PARAM_HWADDRESS:
Michael Chan7ffc49a2007-12-24 21:28:09 -0800180 len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
Mike Christieaa1e93a2007-05-30 12:57:09 -0500181 break;
Mike Christie22236962007-05-30 12:57:24 -0500182 case ISCSI_HOST_PARAM_IPADDRESS:
183 len = sprintf(buf, "%d.%d.%d.%d\n", ha->ip_address[0],
184 ha->ip_address[1], ha->ip_address[2],
185 ha->ip_address[3]);
186 break;
Mike Christie8ad57812007-05-30 12:57:13 -0500187 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie22236962007-05-30 12:57:24 -0500188 len = sprintf(buf, "%s\n", ha->name_string);
Mike Christie8ad57812007-05-30 12:57:13 -0500189 break;
Mike Christieaa1e93a2007-05-30 12:57:09 -0500190 default:
191 return -ENOSYS;
192 }
193
194 return len;
195}
196
David Somayajuluafaf5a22006-09-19 10:28:00 -0700197static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
198 enum iscsi_param param, char *buf)
199{
200 struct ddb_entry *ddb_entry = sess->dd_data;
201 int len;
202
203 switch (param) {
204 case ISCSI_PARAM_TARGET_NAME:
205 len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
206 ddb_entry->iscsi_name);
207 break;
208 case ISCSI_PARAM_TPGT:
209 len = sprintf(buf, "%u\n", ddb_entry->tpgt);
210 break;
Vikas Chaudhary3b2bef12010-07-10 14:51:30 +0530211 case ISCSI_PARAM_TARGET_ALIAS:
212 len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
213 ddb_entry->iscsi_alias);
214 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700215 default:
216 return -ENOSYS;
217 }
218
219 return len;
220}
221
222static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
223 enum iscsi_param param, char *buf)
224{
225 struct iscsi_cls_session *session;
226 struct ddb_entry *ddb_entry;
227 int len;
228
229 session = iscsi_dev_to_session(conn->dev.parent);
230 ddb_entry = session->dd_data;
231
232 switch (param) {
233 case ISCSI_PARAM_CONN_PORT:
234 len = sprintf(buf, "%hu\n", ddb_entry->port);
235 break;
236 case ISCSI_PARAM_CONN_ADDRESS:
237 /* TODO: what are the ipv6 bits */
Harvey Harrison63779432008-10-31 00:56:00 -0700238 len = sprintf(buf, "%pI4\n", &ddb_entry->ip_addr);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700239 break;
240 default:
241 return -ENOSYS;
242 }
243
244 return len;
245}
246
Mike Christie2174a042007-05-30 12:57:10 -0500247static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
248 enum iscsi_tgt_dscvr type, uint32_t enable,
249 struct sockaddr *dst_addr)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700250{
251 struct scsi_qla_host *ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700252 struct sockaddr_in *addr;
253 struct sockaddr_in6 *addr6;
254 int ret = 0;
255
David Somayajuluafaf5a22006-09-19 10:28:00 -0700256 ha = (struct scsi_qla_host *) shost->hostdata;
257
258 switch (type) {
259 case ISCSI_TGT_DSCVR_SEND_TARGETS:
260 if (dst_addr->sa_family == AF_INET) {
261 addr = (struct sockaddr_in *)dst_addr;
262 if (qla4xxx_send_tgts(ha, (char *)&addr->sin_addr,
263 addr->sin_port) != QLA_SUCCESS)
264 ret = -EIO;
265 } else if (dst_addr->sa_family == AF_INET6) {
266 /*
267 * TODO: fix qla4xxx_send_tgts
268 */
269 addr6 = (struct sockaddr_in6 *)dst_addr;
270 if (qla4xxx_send_tgts(ha, (char *)&addr6->sin6_addr,
271 addr6->sin6_port) != QLA_SUCCESS)
272 ret = -EIO;
273 } else
274 ret = -ENOSYS;
275 break;
276 default:
277 ret = -ENOSYS;
278 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700279 return ret;
280}
281
282void qla4xxx_destroy_sess(struct ddb_entry *ddb_entry)
283{
284 if (!ddb_entry->sess)
285 return;
286
287 if (ddb_entry->conn) {
Mike Christie26974782007-12-13 12:43:29 -0600288 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700289 iscsi_remove_session(ddb_entry->sess);
290 }
291 iscsi_free_session(ddb_entry->sess);
292}
293
294int qla4xxx_add_sess(struct ddb_entry *ddb_entry)
295{
296 int err;
297
Vikas Chaudhary3013cea2010-07-30 14:25:46 +0530298 ddb_entry->sess->recovery_tmo = QL4_SESS_RECOVERY_TMO;
299
David Somayajuluafaf5a22006-09-19 10:28:00 -0700300 err = iscsi_add_session(ddb_entry->sess, ddb_entry->fw_ddb_index);
301 if (err) {
302 DEBUG2(printk(KERN_ERR "Could not add session.\n"));
303 return err;
304 }
305
Mike Christie5d91e202008-05-21 15:54:01 -0500306 ddb_entry->conn = iscsi_create_conn(ddb_entry->sess, 0, 0);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700307 if (!ddb_entry->conn) {
308 iscsi_remove_session(ddb_entry->sess);
309 DEBUG2(printk(KERN_ERR "Could not add connection.\n"));
310 return -ENOMEM;
311 }
Mike Christieb6359302008-01-31 13:36:44 -0600312
313 /* finally ready to go */
314 iscsi_unblock_session(ddb_entry->sess);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700315 return 0;
316}
317
318struct ddb_entry *qla4xxx_alloc_sess(struct scsi_qla_host *ha)
319{
320 struct ddb_entry *ddb_entry;
321 struct iscsi_cls_session *sess;
322
Mike Christie5d91e202008-05-21 15:54:01 -0500323 sess = iscsi_alloc_session(ha->host, &qla4xxx_iscsi_transport,
324 sizeof(struct ddb_entry));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700325 if (!sess)
326 return NULL;
327
328 ddb_entry = sess->dd_data;
329 memset(ddb_entry, 0, sizeof(*ddb_entry));
330 ddb_entry->ha = ha;
331 ddb_entry->sess = sess;
332 return ddb_entry;
333}
334
Mike Christie024f8012008-03-04 13:26:54 -0600335static void qla4xxx_scan_start(struct Scsi_Host *shost)
336{
337 struct scsi_qla_host *ha = shost_priv(shost);
338 struct ddb_entry *ddb_entry, *ddbtemp;
339
340 /* finish setup of sessions that were already setup in firmware */
341 list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
342 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE)
343 qla4xxx_add_sess(ddb_entry);
344 }
345}
346
David Somayajuluafaf5a22006-09-19 10:28:00 -0700347/*
348 * Timer routines
349 */
350
351static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
352 unsigned long interval)
353{
354 DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
355 __func__, ha->host->host_no));
356 init_timer(&ha->timer);
357 ha->timer.expires = jiffies + interval * HZ;
358 ha->timer.data = (unsigned long)ha;
359 ha->timer.function = (void (*)(unsigned long))func;
360 add_timer(&ha->timer);
361 ha->timer_active = 1;
362}
363
364static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
365{
366 del_timer_sync(&ha->timer);
367 ha->timer_active = 0;
368}
369
370/***
371 * qla4xxx_mark_device_missing - mark a device as missing.
372 * @ha: Pointer to host adapter structure.
373 * @ddb_entry: Pointer to device database entry
374 *
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530375 * This routine marks a device missing and close connection.
David Somayajuluafaf5a22006-09-19 10:28:00 -0700376 **/
377void qla4xxx_mark_device_missing(struct scsi_qla_host *ha,
378 struct ddb_entry *ddb_entry)
379{
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530380 if ((atomic_read(&ddb_entry->state) != DDB_STATE_DEAD)) {
381 atomic_set(&ddb_entry->state, DDB_STATE_MISSING);
382 DEBUG2(printk("scsi%ld: ddb [%d] marked MISSING\n",
383 ha->host_no, ddb_entry->fw_ddb_index));
384 } else
385 DEBUG2(printk("scsi%ld: ddb [%d] DEAD\n", ha->host_no,
386 ddb_entry->fw_ddb_index))
387
Mike Christieb6359302008-01-31 13:36:44 -0600388 iscsi_block_session(ddb_entry->sess);
Mike Christiee5bd7b52008-09-24 11:46:10 -0500389 iscsi_conn_error_event(ddb_entry->conn, ISCSI_ERR_CONN_FAILED);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700390}
391
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530392/**
393 * qla4xxx_mark_all_devices_missing - mark all devices as missing.
394 * @ha: Pointer to host adapter structure.
395 *
396 * This routine marks a device missing and resets the relogin retry count.
397 **/
398void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
399{
400 struct ddb_entry *ddb_entry, *ddbtemp;
401 list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
402 qla4xxx_mark_device_missing(ha, ddb_entry);
403 }
404}
405
David Somayajuluafaf5a22006-09-19 10:28:00 -0700406static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
407 struct ddb_entry *ddb_entry,
408 struct scsi_cmnd *cmd,
409 void (*done)(struct scsi_cmnd *))
410{
411 struct srb *srb;
412
413 srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
414 if (!srb)
415 return srb;
416
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530417 kref_init(&srb->srb_ref);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700418 srb->ha = ha;
419 srb->ddb = ddb_entry;
420 srb->cmd = cmd;
421 srb->flags = 0;
Vikas Chaudhary53698872010-04-28 11:41:59 +0530422 CMD_SP(cmd) = (void *)srb;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700423 cmd->scsi_done = done;
424
425 return srb;
426}
427
428static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
429{
430 struct scsi_cmnd *cmd = srb->cmd;
431
432 if (srb->flags & SRB_DMA_VALID) {
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +0900433 scsi_dma_unmap(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700434 srb->flags &= ~SRB_DMA_VALID;
435 }
Vikas Chaudhary53698872010-04-28 11:41:59 +0530436 CMD_SP(cmd) = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700437}
438
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530439void qla4xxx_srb_compl(struct kref *ref)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700440{
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530441 struct srb *srb = container_of(ref, struct srb, srb_ref);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700442 struct scsi_cmnd *cmd = srb->cmd;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530443 struct scsi_qla_host *ha = srb->ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700444
445 qla4xxx_srb_free_dma(ha, srb);
446
447 mempool_free(srb, ha->srb_mempool);
448
449 cmd->scsi_done(cmd);
450}
451
452/**
453 * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
454 * @cmd: Pointer to Linux's SCSI command structure
455 * @done_fn: Function that the driver calls to notify the SCSI mid-layer
456 * that the command has been processed.
457 *
458 * Remarks:
459 * This routine is invoked by Linux to send a SCSI command to the driver.
460 * The mid-level driver tries to ensure that queuecommand never gets
461 * invoked concurrently with itself or the interrupt handler (although
462 * the interrupt handler may call this routine as part of request-
463 * completion handling). Unfortunely, it sometimes calls the scheduler
464 * in interrupt context which is a big NO! NO!.
465 **/
Jeff Garzikf2812332010-11-16 02:10:29 -0500466static int qla4xxx_queuecommand_lck(struct scsi_cmnd *cmd,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700467 void (*done)(struct scsi_cmnd *))
468{
469 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
470 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christie7fb19212008-01-31 13:36:45 -0600471 struct iscsi_cls_session *sess = ddb_entry->sess;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700472 struct srb *srb;
473 int rval;
474
Lalit Chandivade2232be02010-07-30 14:38:47 +0530475 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
476 if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
477 cmd->result = DID_NO_CONNECT << 16;
478 else
479 cmd->result = DID_REQUEUE << 16;
480 goto qc_fail_command;
481 }
482
Mike Christie7fb19212008-01-31 13:36:45 -0600483 if (!sess) {
484 cmd->result = DID_IMM_RETRY << 16;
485 goto qc_fail_command;
486 }
487
488 rval = iscsi_session_chkready(sess);
489 if (rval) {
490 cmd->result = rval;
491 goto qc_fail_command;
492 }
493
David Somayajuluafaf5a22006-09-19 10:28:00 -0700494 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
495 if (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD) {
496 cmd->result = DID_NO_CONNECT << 16;
497 goto qc_fail_command;
498 }
Mike Christiec5e98e92008-08-17 15:24:39 -0500499 return SCSI_MLQUEUE_TARGET_BUSY;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700500 }
501
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530502 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
503 test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
504 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
505 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
506 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
507 !test_bit(AF_ONLINE, &ha->flags) ||
508 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
David C Somayajulu477ffb92007-01-22 12:26:11 -0800509 goto qc_host_busy;
510
David Somayajuluafaf5a22006-09-19 10:28:00 -0700511 spin_unlock_irq(ha->host->host_lock);
512
513 srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd, done);
514 if (!srb)
515 goto qc_host_busy_lock;
516
517 rval = qla4xxx_send_command_to_isp(ha, srb);
518 if (rval != QLA_SUCCESS)
519 goto qc_host_busy_free_sp;
520
521 spin_lock_irq(ha->host->host_lock);
522 return 0;
523
524qc_host_busy_free_sp:
525 qla4xxx_srb_free_dma(ha, srb);
526 mempool_free(srb, ha->srb_mempool);
527
528qc_host_busy_lock:
529 spin_lock_irq(ha->host->host_lock);
530
531qc_host_busy:
532 return SCSI_MLQUEUE_HOST_BUSY;
533
534qc_fail_command:
535 done(cmd);
536
537 return 0;
538}
539
Jeff Garzikf2812332010-11-16 02:10:29 -0500540static DEF_SCSI_QCMD(qla4xxx_queuecommand)
541
David Somayajuluafaf5a22006-09-19 10:28:00 -0700542/**
543 * qla4xxx_mem_free - frees memory allocated to adapter
544 * @ha: Pointer to host adapter structure.
545 *
546 * Frees memory previously allocated by qla4xxx_mem_alloc
547 **/
548static void qla4xxx_mem_free(struct scsi_qla_host *ha)
549{
550 if (ha->queues)
551 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
552 ha->queues_dma);
553
554 ha->queues_len = 0;
555 ha->queues = NULL;
556 ha->queues_dma = 0;
557 ha->request_ring = NULL;
558 ha->request_dma = 0;
559 ha->response_ring = NULL;
560 ha->response_dma = 0;
561 ha->shadow_regs = NULL;
562 ha->shadow_regs_dma = 0;
563
564 /* Free srb pool. */
565 if (ha->srb_mempool)
566 mempool_destroy(ha->srb_mempool);
567
568 ha->srb_mempool = NULL;
569
570 /* release io space registers */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530571 if (is_qla8022(ha)) {
572 if (ha->nx_pcibase)
573 iounmap(
574 (struct device_reg_82xx __iomem *)ha->nx_pcibase);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530575 } else if (ha->reg)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700576 iounmap(ha->reg);
577 pci_release_regions(ha->pdev);
578}
579
580/**
581 * qla4xxx_mem_alloc - allocates memory for use by adapter.
582 * @ha: Pointer to host adapter structure
583 *
584 * Allocates DMA memory for request and response queues. Also allocates memory
585 * for srbs.
586 **/
587static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
588{
589 unsigned long align;
590
591 /* Allocate contiguous block of DMA memory for queues. */
592 ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
593 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
594 sizeof(struct shadow_regs) +
595 MEM_ALIGN_VALUE +
596 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
597 ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
598 &ha->queues_dma, GFP_KERNEL);
599 if (ha->queues == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530600 ql4_printk(KERN_WARNING, ha,
601 "Memory Allocation failed - queues.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700602
603 goto mem_alloc_error_exit;
604 }
605 memset(ha->queues, 0, ha->queues_len);
606
607 /*
608 * As per RISC alignment requirements -- the bus-address must be a
609 * multiple of the request-ring size (in bytes).
610 */
611 align = 0;
612 if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
613 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
614 (MEM_ALIGN_VALUE - 1));
615
616 /* Update request and response queue pointers. */
617 ha->request_dma = ha->queues_dma + align;
618 ha->request_ring = (struct queue_entry *) (ha->queues + align);
619 ha->response_dma = ha->queues_dma + align +
620 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
621 ha->response_ring = (struct queue_entry *) (ha->queues + align +
622 (REQUEST_QUEUE_DEPTH *
623 QUEUE_SIZE));
624 ha->shadow_regs_dma = ha->queues_dma + align +
625 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
626 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
627 ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
628 (REQUEST_QUEUE_DEPTH *
629 QUEUE_SIZE) +
630 (RESPONSE_QUEUE_DEPTH *
631 QUEUE_SIZE));
632
633 /* Allocate memory for srb pool. */
634 ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
635 mempool_free_slab, srb_cachep);
636 if (ha->srb_mempool == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530637 ql4_printk(KERN_WARNING, ha,
638 "Memory Allocation failed - SRB Pool.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700639
640 goto mem_alloc_error_exit;
641 }
642
643 return QLA_SUCCESS;
644
645mem_alloc_error_exit:
646 qla4xxx_mem_free(ha);
647 return QLA_ERROR;
648}
649
650/**
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530651 * qla4_8xxx_check_fw_alive - Check firmware health
652 * @ha: Pointer to host adapter structure.
653 *
654 * Context: Interrupt
655 **/
656static void qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
657{
658 uint32_t fw_heartbeat_counter, halt_status;
659
660 fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
Lalit Chandivade2232be02010-07-30 14:38:47 +0530661 /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
662 if (fw_heartbeat_counter == 0xffffffff) {
663 DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
664 "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
665 ha->host_no, __func__));
666 return;
667 }
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530668
669 if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
670 ha->seconds_since_last_heartbeat++;
671 /* FW not alive after 2 seconds */
672 if (ha->seconds_since_last_heartbeat == 2) {
673 ha->seconds_since_last_heartbeat = 0;
674 halt_status = qla4_8xxx_rd_32(ha,
675 QLA82XX_PEG_HALT_STATUS1);
Nilesh Javali21033632010-07-30 14:28:07 +0530676
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530677 /* Since we cannot change dev_state in interrupt
678 * context, set appropriate DPC flag then wakeup
679 * DPC */
680 if (halt_status & HALT_STATUS_UNRECOVERABLE)
681 set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
682 else {
683 printk("scsi%ld: %s: detect abort needed!\n",
684 ha->host_no, __func__);
685 set_bit(DPC_RESET_HA, &ha->dpc_flags);
686 }
687 qla4xxx_wake_dpc(ha);
Nilesh Javali21033632010-07-30 14:28:07 +0530688 qla4xxx_mailbox_premature_completion(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530689 }
Lalit Chandivade99457d72010-10-06 22:49:32 -0700690 } else
691 ha->seconds_since_last_heartbeat = 0;
692
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530693 ha->fw_heartbeat_counter = fw_heartbeat_counter;
694}
695
696/**
697 * qla4_8xxx_watchdog - Poll dev state
698 * @ha: Pointer to host adapter structure.
699 *
700 * Context: Interrupt
701 **/
702void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
703{
704 uint32_t dev_state;
705
706 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
707
708 /* don't poll if reset is going on */
Lalit Chandivaded56a1f72010-12-02 22:12:45 -0800709 if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
710 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
711 test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags))) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530712 if (dev_state == QLA82XX_DEV_NEED_RESET &&
713 !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
Vikas Chaudhary3930b8c2010-12-02 22:12:47 -0800714 if (!ql4xdontresethba) {
715 ql4_printk(KERN_INFO, ha, "%s: HW State: "
716 "NEED RESET!\n", __func__);
717 set_bit(DPC_RESET_HA, &ha->dpc_flags);
718 qla4xxx_wake_dpc(ha);
719 qla4xxx_mailbox_premature_completion(ha);
720 }
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530721 } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
722 !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
Vikas Chaudhary3930b8c2010-12-02 22:12:47 -0800723 ql4_printk(KERN_INFO, ha, "%s: HW State: NEED QUIES!\n",
724 __func__);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530725 set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
726 qla4xxx_wake_dpc(ha);
727 } else {
728 /* Check firmware health */
729 qla4_8xxx_check_fw_alive(ha);
730 }
731 }
732}
733
734/**
David Somayajuluafaf5a22006-09-19 10:28:00 -0700735 * qla4xxx_timer - checks every second for work to do.
736 * @ha: Pointer to host adapter structure.
737 **/
738static void qla4xxx_timer(struct scsi_qla_host *ha)
739{
740 struct ddb_entry *ddb_entry, *dtemp;
741 int start_dpc = 0;
Lalit Chandivade2232be02010-07-30 14:38:47 +0530742 uint16_t w;
743
744 /* If we are in the middle of AER/EEH processing
745 * skip any processing and reschedule the timer
746 */
747 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
748 mod_timer(&ha->timer, jiffies + HZ);
749 return;
750 }
751
752 /* Hardware read to trigger an EEH error during mailbox waits. */
753 if (!pci_channel_offline(ha->pdev))
754 pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700755
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530756 if (test_bit(AF_HBA_GOING_AWAY, &ha->flags)) {
757 DEBUG2(ql4_printk(KERN_INFO, ha, "%s exited. HBA GOING AWAY\n",
758 __func__));
759 return;
760 }
761
762 if (is_qla8022(ha)) {
763 qla4_8xxx_watchdog(ha);
764 }
765
David Somayajuluafaf5a22006-09-19 10:28:00 -0700766 /* Search for relogin's to time-out and port down retry. */
767 list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
768 /* Count down time between sending relogins */
769 if (adapter_up(ha) &&
770 !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
771 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
772 if (atomic_read(&ddb_entry->retry_relogin_timer) !=
773 INVALID_ENTRY) {
774 if (atomic_read(&ddb_entry->retry_relogin_timer)
775 == 0) {
776 atomic_set(&ddb_entry->
777 retry_relogin_timer,
778 INVALID_ENTRY);
779 set_bit(DPC_RELOGIN_DEVICE,
780 &ha->dpc_flags);
781 set_bit(DF_RELOGIN, &ddb_entry->flags);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530782 DEBUG2(printk("scsi%ld: %s: ddb [%d]"
David Somayajuluafaf5a22006-09-19 10:28:00 -0700783 " login device\n",
784 ha->host_no, __func__,
785 ddb_entry->fw_ddb_index));
786 } else
787 atomic_dec(&ddb_entry->
788 retry_relogin_timer);
789 }
790 }
791
792 /* Wait for relogin to timeout */
793 if (atomic_read(&ddb_entry->relogin_timer) &&
794 (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
795 /*
796 * If the relogin times out and the device is
797 * still NOT ONLINE then try and relogin again.
798 */
799 if (atomic_read(&ddb_entry->state) !=
800 DDB_STATE_ONLINE &&
801 ddb_entry->fw_ddb_device_state ==
802 DDB_DS_SESSION_FAILED) {
803 /* Reset retry relogin timer */
804 atomic_inc(&ddb_entry->relogin_retry_count);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530805 DEBUG2(printk("scsi%ld: ddb [%d] relogin"
David Somayajuluafaf5a22006-09-19 10:28:00 -0700806 " timed out-retrying"
807 " relogin (%d)\n",
808 ha->host_no,
809 ddb_entry->fw_ddb_index,
810 atomic_read(&ddb_entry->
811 relogin_retry_count))
812 );
813 start_dpc++;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530814 DEBUG(printk("scsi%ld:%d:%d: ddb [%d] "
Justin P. Mattockcd09b2c2011-01-28 11:49:08 -0800815 "initiate relogin after"
David Somayajuluafaf5a22006-09-19 10:28:00 -0700816 " %d seconds\n",
817 ha->host_no, ddb_entry->bus,
818 ddb_entry->target,
819 ddb_entry->fw_ddb_index,
820 ddb_entry->default_time2wait + 4)
821 );
822
823 atomic_set(&ddb_entry->retry_relogin_timer,
824 ddb_entry->default_time2wait + 4);
825 }
826 }
827 }
828
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530829 if (!is_qla8022(ha)) {
830 /* Check for heartbeat interval. */
831 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
832 ha->heartbeat_interval != 0) {
833 ha->seconds_since_last_heartbeat++;
834 if (ha->seconds_since_last_heartbeat >
835 ha->heartbeat_interval + 2)
836 set_bit(DPC_RESET_HA, &ha->dpc_flags);
837 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700838 }
839
David Somayajuluafaf5a22006-09-19 10:28:00 -0700840 /* Wakeup the dpc routine for this adapter, if needed. */
841 if ((start_dpc ||
842 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
843 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
844 test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530845 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -0700846 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
847 test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +0530848 test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530849 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
850 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -0700851 test_bit(DPC_AEN, &ha->dpc_flags)) &&
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530852 !test_bit(AF_DPC_SCHEDULED, &ha->flags) &&
David Somayajuluafaf5a22006-09-19 10:28:00 -0700853 ha->dpc_thread) {
854 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
855 " - dpc flags = 0x%lx\n",
856 ha->host_no, __func__, ha->dpc_flags));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530857 qla4xxx_wake_dpc(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700858 }
859
860 /* Reschedule timer thread to call us back in one second */
861 mod_timer(&ha->timer, jiffies + HZ);
862
863 DEBUG2(ha->seconds_since_last_intr++);
864}
865
866/**
867 * qla4xxx_cmd_wait - waits for all outstanding commands to complete
868 * @ha: Pointer to host adapter structure.
869 *
870 * This routine stalls the driver until all outstanding commands are returned.
871 * Caller must release the Hardware Lock prior to calling this routine.
872 **/
873static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
874{
875 uint32_t index = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700876 unsigned long flags;
877 struct scsi_cmnd *cmd;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700878
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530879 unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
880
881 DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
882 "complete\n", WAIT_CMD_TOV));
883
884 while (!time_after_eq(jiffies, wtime)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700885 spin_lock_irqsave(&ha->hardware_lock, flags);
886 /* Find a command that hasn't completed. */
887 for (index = 0; index < ha->host->can_queue; index++) {
888 cmd = scsi_host_find_tag(ha->host, index);
Mike Christiea1e00632010-10-26 05:45:30 -0700889 /*
890 * We cannot just check if the index is valid,
891 * becase if we are run from the scsi eh, then
892 * the scsi/block layer is going to prevent
893 * the tag from being released.
894 */
895 if (cmd != NULL && CMD_SP(cmd))
David Somayajuluafaf5a22006-09-19 10:28:00 -0700896 break;
897 }
898 spin_unlock_irqrestore(&ha->hardware_lock, flags);
899
900 /* If No Commands are pending, wait is complete */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530901 if (index == ha->host->can_queue)
902 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700903
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530904 msleep(1000);
905 }
906 /* If we timed out on waiting for commands to come back
907 * return ERROR. */
908 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700909}
910
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530911int qla4xxx_hw_reset(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700912{
David Somayajuluafaf5a22006-09-19 10:28:00 -0700913 uint32_t ctrl_status;
David C Somayajulu477ffb92007-01-22 12:26:11 -0800914 unsigned long flags = 0;
915
916 DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700917
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530918 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
919 return QLA_ERROR;
920
David Somayajuluafaf5a22006-09-19 10:28:00 -0700921 spin_lock_irqsave(&ha->hardware_lock, flags);
922
923 /*
924 * If the SCSI Reset Interrupt bit is set, clear it.
925 * Otherwise, the Soft Reset won't work.
926 */
927 ctrl_status = readw(&ha->reg->ctrl_status);
928 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
929 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
930
931 /* Issue Soft Reset */
932 writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
933 readl(&ha->reg->ctrl_status);
934
935 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530936 return QLA_SUCCESS;
David C Somayajulu477ffb92007-01-22 12:26:11 -0800937}
938
939/**
940 * qla4xxx_soft_reset - performs soft reset.
941 * @ha: Pointer to host adapter structure.
942 **/
943int qla4xxx_soft_reset(struct scsi_qla_host *ha)
944{
945 uint32_t max_wait_time;
946 unsigned long flags = 0;
Vikas Chaudharyf931c532010-10-06 22:48:07 -0700947 int status;
David C Somayajulu477ffb92007-01-22 12:26:11 -0800948 uint32_t ctrl_status;
949
Vikas Chaudharyf931c532010-10-06 22:48:07 -0700950 status = qla4xxx_hw_reset(ha);
951 if (status != QLA_SUCCESS)
952 return status;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700953
Vikas Chaudharyf931c532010-10-06 22:48:07 -0700954 status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700955 /* Wait until the Network Reset Intr bit is cleared */
956 max_wait_time = RESET_INTR_TOV;
957 do {
958 spin_lock_irqsave(&ha->hardware_lock, flags);
959 ctrl_status = readw(&ha->reg->ctrl_status);
960 spin_unlock_irqrestore(&ha->hardware_lock, flags);
961
962 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
963 break;
964
965 msleep(1000);
966 } while ((--max_wait_time));
967
968 if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
969 DEBUG2(printk(KERN_WARNING
970 "scsi%ld: Network Reset Intr not cleared by "
971 "Network function, clearing it now!\n",
972 ha->host_no));
973 spin_lock_irqsave(&ha->hardware_lock, flags);
974 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
975 readl(&ha->reg->ctrl_status);
976 spin_unlock_irqrestore(&ha->hardware_lock, flags);
977 }
978
979 /* Wait until the firmware tells us the Soft Reset is done */
980 max_wait_time = SOFT_RESET_TOV;
981 do {
982 spin_lock_irqsave(&ha->hardware_lock, flags);
983 ctrl_status = readw(&ha->reg->ctrl_status);
984 spin_unlock_irqrestore(&ha->hardware_lock, flags);
985
986 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
987 status = QLA_SUCCESS;
988 break;
989 }
990
991 msleep(1000);
992 } while ((--max_wait_time));
993
994 /*
995 * Also, make sure that the SCSI Reset Interrupt bit has been cleared
996 * after the soft reset has taken place.
997 */
998 spin_lock_irqsave(&ha->hardware_lock, flags);
999 ctrl_status = readw(&ha->reg->ctrl_status);
1000 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
1001 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
1002 readl(&ha->reg->ctrl_status);
1003 }
1004 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1005
1006 /* If soft reset fails then most probably the bios on other
1007 * function is also enabled.
1008 * Since the initialization is sequential the other fn
1009 * wont be able to acknowledge the soft reset.
1010 * Issue a force soft reset to workaround this scenario.
1011 */
1012 if (max_wait_time == 0) {
1013 /* Issue Force Soft Reset */
1014 spin_lock_irqsave(&ha->hardware_lock, flags);
1015 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
1016 readl(&ha->reg->ctrl_status);
1017 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1018 /* Wait until the firmware tells us the Soft Reset is done */
1019 max_wait_time = SOFT_RESET_TOV;
1020 do {
1021 spin_lock_irqsave(&ha->hardware_lock, flags);
1022 ctrl_status = readw(&ha->reg->ctrl_status);
1023 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1024
1025 if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
1026 status = QLA_SUCCESS;
1027 break;
1028 }
1029
1030 msleep(1000);
1031 } while ((--max_wait_time));
1032 }
1033
1034 return status;
1035}
1036
1037/**
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301038 * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001039 * @ha: Pointer to host adapter structure.
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301040 * @res: returned scsi status
David Somayajuluafaf5a22006-09-19 10:28:00 -07001041 *
1042 * This routine is called just prior to a HARD RESET to return all
1043 * outstanding commands back to the Operating System.
1044 * Caller should make sure that the following locks are released
1045 * before this calling routine: Hardware lock, and io_request_lock.
1046 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301047static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001048{
1049 struct srb *srb;
1050 int i;
1051 unsigned long flags;
1052
1053 spin_lock_irqsave(&ha->hardware_lock, flags);
1054 for (i = 0; i < ha->host->can_queue; i++) {
1055 srb = qla4xxx_del_from_active_array(ha, i);
1056 if (srb != NULL) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301057 srb->cmd->result = res;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301058 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001059 }
1060 }
1061 spin_unlock_irqrestore(&ha->hardware_lock, flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001062}
1063
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301064void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
1065{
1066 clear_bit(AF_ONLINE, &ha->flags);
1067
1068 /* Disable the board */
1069 ql4_printk(KERN_INFO, ha, "Disabling the board\n");
1070 set_bit(AF_HBA_GOING_AWAY, &ha->flags);
1071
1072 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
1073 qla4xxx_mark_all_devices_missing(ha);
1074 clear_bit(AF_INIT_DONE, &ha->flags);
1075}
1076
David Somayajuluafaf5a22006-09-19 10:28:00 -07001077/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07001078 * qla4xxx_recover_adapter - recovers adapter after a fatal error
1079 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001080 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301081static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001082{
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301083 int status = QLA_ERROR;
1084 uint8_t reset_chip = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001085
1086 /* Stall incoming I/O until we are done */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301087 scsi_block_requests(ha->host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001088 clear_bit(AF_ONLINE, &ha->flags);
Mike Christie50a29ae2008-03-04 13:26:53 -06001089
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301090 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001091
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301092 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001093
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301094 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
1095 reset_chip = 1;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001096
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301097 /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
1098 * do not reset adapter, jump to initialize_adapter */
1099 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1100 status = QLA_SUCCESS;
1101 goto recover_ha_init_adapter;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001102 }
1103
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301104 /* For the ISP-82xx adapter, issue a stop_firmware if invoked
1105 * from eh_host_reset or ioctl module */
1106 if (is_qla8022(ha) && !reset_chip &&
1107 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
1108
1109 DEBUG2(ql4_printk(KERN_INFO, ha,
1110 "scsi%ld: %s - Performing stop_firmware...\n",
1111 ha->host_no, __func__));
1112 status = ha->isp_ops->reset_firmware(ha);
1113 if (status == QLA_SUCCESS) {
Nilesh Javali2bd1e2be2010-10-06 22:49:20 -07001114 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
1115 qla4xxx_cmd_wait(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301116 ha->isp_ops->disable_intrs(ha);
1117 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1118 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1119 } else {
1120 /* If the stop_firmware fails then
1121 * reset the entire chip */
1122 reset_chip = 1;
1123 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1124 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1125 }
1126 }
1127
1128 /* Issue full chip reset if recovering from a catastrophic error,
1129 * or if stop_firmware fails for ISP-82xx.
1130 * This is the default case for ISP-4xxx */
1131 if (!is_qla8022(ha) || reset_chip) {
Nilesh Javali2bd1e2be2010-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 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1135 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1136 DEBUG2(ql4_printk(KERN_INFO, ha,
1137 "scsi%ld: %s - Performing chip reset..\n",
1138 ha->host_no, __func__));
1139 status = ha->isp_ops->reset_chip(ha);
1140 }
1141
1142 /* Flush any pending ddb changed AENs */
1143 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1144
1145recover_ha_init_adapter:
1146 /* Upon successful firmware/chip reset, re-initialize the adapter */
1147 if (status == QLA_SUCCESS) {
1148 /* For ISP-4xxx, force function 1 to always initialize
1149 * before function 3 to prevent both funcions from
1150 * stepping on top of the other */
1151 if (!is_qla8022(ha) && (ha->mac_index == 3))
1152 ssleep(6);
1153
1154 /* NOTE: AF_ONLINE flag set upon successful completion of
1155 * qla4xxx_initialize_adapter */
1156 status = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
1157 }
1158
1159 /* Retry failed adapter initialization, if necessary
1160 * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
1161 * case to prevent ping-pong resets between functions */
1162 if (!test_bit(AF_ONLINE, &ha->flags) &&
1163 !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001164 /* Adapter initialization failed, see if we can retry
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301165 * resetting the ha.
1166 * Since we don't want to block the DPC for too long
1167 * with multiple resets in the same thread,
1168 * utilize DPC to retry */
David Somayajuluafaf5a22006-09-19 10:28:00 -07001169 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
1170 ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
1171 DEBUG2(printk("scsi%ld: recover adapter - retrying "
1172 "(%d) more times\n", ha->host_no,
1173 ha->retry_reset_ha_cnt));
1174 set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1175 status = QLA_ERROR;
1176 } else {
1177 if (ha->retry_reset_ha_cnt > 0) {
1178 /* Schedule another Reset HA--DPC will retry */
1179 ha->retry_reset_ha_cnt--;
1180 DEBUG2(printk("scsi%ld: recover adapter - "
1181 "retry remaining %d\n",
1182 ha->host_no,
1183 ha->retry_reset_ha_cnt));
1184 status = QLA_ERROR;
1185 }
1186
1187 if (ha->retry_reset_ha_cnt == 0) {
1188 /* Recover adapter retries have been exhausted.
1189 * Adapter DEAD */
1190 DEBUG2(printk("scsi%ld: recover adapter "
1191 "failed - board disabled\n",
1192 ha->host_no));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301193 qla4xxx_dead_adapter_cleanup(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001194 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1195 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301196 clear_bit(DPC_RESET_HA_FW_CONTEXT,
David Somayajuluafaf5a22006-09-19 10:28:00 -07001197 &ha->dpc_flags);
1198 status = QLA_ERROR;
1199 }
1200 }
1201 } else {
1202 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301203 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001204 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1205 }
1206
1207 ha->adapter_error_count++;
1208
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301209 if (test_bit(AF_ONLINE, &ha->flags))
1210 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001211
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301212 scsi_unblock_requests(ha->host);
1213
1214 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
1215 DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
1216 status == QLA_ERROR ? "FAILED" : "SUCCEDED"));
1217
David Somayajuluafaf5a22006-09-19 10:28:00 -07001218 return status;
1219}
1220
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301221void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
1222{
1223 if (ha->dpc_thread &&
1224 !test_bit(AF_DPC_SCHEDULED, &ha->flags)) {
1225 set_bit(AF_DPC_SCHEDULED, &ha->flags);
1226 queue_work(ha->dpc_thread, &ha->dpc_work);
1227 }
1228}
1229
David Somayajuluafaf5a22006-09-19 10:28:00 -07001230/**
1231 * qla4xxx_do_dpc - dpc routine
1232 * @data: in our case pointer to adapter structure
1233 *
1234 * This routine is a task that is schedule by the interrupt handler
1235 * to perform the background processing for interrupts. We put it
1236 * on a task queue that is consumed whenever the scheduler runs; that's
1237 * so you can do anything (i.e. put the process to sleep etc). In fact,
1238 * the mid-level tries to sleep when it reaches the driver threshold
1239 * "host->can_queue". This can cause a panic if we were in our interrupt code.
1240 **/
David Howellsc4028952006-11-22 14:57:56 +00001241static void qla4xxx_do_dpc(struct work_struct *work)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001242{
David Howellsc4028952006-11-22 14:57:56 +00001243 struct scsi_qla_host *ha =
1244 container_of(work, struct scsi_qla_host, dpc_work);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001245 struct ddb_entry *ddb_entry, *dtemp;
David C Somayajulu477ffb92007-01-22 12:26:11 -08001246 int status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001247
David C Somayajuluf26b9042006-11-15 16:41:09 -08001248 DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301249 "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
1250 ha->host_no, __func__, ha->flags, ha->dpc_flags))
David Somayajuluafaf5a22006-09-19 10:28:00 -07001251
1252 /* Initialization not yet finished. Don't do anything yet. */
1253 if (!test_bit(AF_INIT_DONE, &ha->flags))
Vikas Chaudhary0753b482010-07-30 14:27:19 +05301254 goto do_dpc_exit;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001255
Lalit Chandivade2232be02010-07-30 14:38:47 +05301256 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1257 DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
1258 ha->host_no, __func__, ha->flags));
1259 goto do_dpc_exit;
1260 }
1261
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301262 /* HBA is in the process of being permanently disabled.
1263 * Don't process anything */
1264 if (test_bit(AF_HBA_GOING_AWAY, &ha->flags))
1265 return;
1266
1267 if (is_qla8022(ha)) {
1268 if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
1269 qla4_8xxx_idc_lock(ha);
1270 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
1271 QLA82XX_DEV_FAILED);
1272 qla4_8xxx_idc_unlock(ha);
1273 ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
1274 qla4_8xxx_device_state_handler(ha);
1275 }
1276 if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
1277 qla4_8xxx_need_qsnt_handler(ha);
1278 }
1279 }
1280
1281 if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
1282 (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07001283 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301284 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
1285 if (ql4xdontresethba) {
1286 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
1287 ha->host_no, __func__));
1288 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
1289 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1290 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1291 goto dpc_post_reset_ha;
1292 }
1293 if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
1294 test_bit(DPC_RESET_HA, &ha->dpc_flags))
1295 qla4xxx_recover_adapter(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001296
David C Somayajulu477ffb92007-01-22 12:26:11 -08001297 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001298 uint8_t wait_time = RESET_INTR_TOV;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001299
David Somayajuluafaf5a22006-09-19 10:28:00 -07001300 while ((readw(&ha->reg->ctrl_status) &
1301 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
1302 if (--wait_time == 0)
1303 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001304 msleep(1000);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001305 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001306 if (wait_time == 0)
1307 DEBUG2(printk("scsi%ld: %s: SR|FSR "
1308 "bit not cleared-- resetting\n",
1309 ha->host_no, __func__));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301310 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
David C Somayajulu477ffb92007-01-22 12:26:11 -08001311 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
1312 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301313 status = qla4xxx_recover_adapter(ha);
David C Somayajulu477ffb92007-01-22 12:26:11 -08001314 }
1315 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1316 if (status == QLA_SUCCESS)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301317 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001318 }
1319 }
1320
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301321dpc_post_reset_ha:
David Somayajuluafaf5a22006-09-19 10:28:00 -07001322 /* ---- process AEN? --- */
1323 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
1324 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
1325
1326 /* ---- Get DHCP IP Address? --- */
1327 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
1328 qla4xxx_get_dhcp_ip_address(ha);
1329
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301330 /* ---- link change? --- */
1331 if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
1332 if (!test_bit(AF_LINK_UP, &ha->flags)) {
1333 /* ---- link down? --- */
1334 list_for_each_entry_safe(ddb_entry, dtemp,
1335 &ha->ddb_list, list) {
1336 if (atomic_read(&ddb_entry->state) ==
1337 DDB_STATE_ONLINE)
1338 qla4xxx_mark_device_missing(ha,
1339 ddb_entry);
1340 }
1341 } else {
1342 /* ---- link up? --- *
1343 * F/W will auto login to all devices ONLY ONCE after
1344 * link up during driver initialization and runtime
1345 * fatal error recovery. Therefore, the driver must
1346 * manually relogin to devices when recovering from
1347 * connection failures, logouts, expired KATO, etc. */
1348
1349 list_for_each_entry_safe(ddb_entry, dtemp,
1350 &ha->ddb_list, list) {
1351 if ((atomic_read(&ddb_entry->state) ==
1352 DDB_STATE_MISSING) ||
1353 (atomic_read(&ddb_entry->state) ==
1354 DDB_STATE_DEAD)) {
1355 if (ddb_entry->fw_ddb_device_state ==
1356 DDB_DS_SESSION_ACTIVE) {
1357 atomic_set(&ddb_entry->state,
1358 DDB_STATE_ONLINE);
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301359 ql4_printk(KERN_INFO, ha,
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301360 "scsi%ld: %s: ddb[%d]"
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301361 " marked ONLINE\n",
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301362 ha->host_no, __func__,
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301363 ddb_entry->fw_ddb_index);
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301364
1365 iscsi_unblock_session(
1366 ddb_entry->sess);
1367 } else
1368 qla4xxx_relogin_device(
1369 ha, ddb_entry);
1370 }
1371
1372 }
1373 }
1374 }
1375
David Somayajuluafaf5a22006-09-19 10:28:00 -07001376 /* ---- relogin device? --- */
1377 if (adapter_up(ha) &&
1378 test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
1379 list_for_each_entry_safe(ddb_entry, dtemp,
1380 &ha->ddb_list, list) {
1381 if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
1382 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
1383 qla4xxx_relogin_device(ha, ddb_entry);
1384
1385 /*
1386 * If mbx cmd times out there is no point
1387 * in continuing further.
1388 * With large no of targets this can hang
1389 * the system.
1390 */
1391 if (test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
1392 printk(KERN_WARNING "scsi%ld: %s: "
1393 "need to reset hba\n",
1394 ha->host_no, __func__);
1395 break;
1396 }
1397 }
1398 }
Vikas Chaudhary0753b482010-07-30 14:27:19 +05301399
1400do_dpc_exit:
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301401 clear_bit(AF_DPC_SCHEDULED, &ha->flags);
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/**
1831 * qla4xxx_remove_adapter - calback function to remove adapter.
1832 * @pci_dev: PCI device pointer
1833 **/
1834static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
1835{
1836 struct scsi_qla_host *ha;
1837
1838 ha = pci_get_drvdata(pdev);
1839
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301840 set_bit(AF_HBA_GOING_AWAY, &ha->flags);
David C Somayajulubee4fe82007-05-23 18:03:32 -07001841
David Somayajuluafaf5a22006-09-19 10:28:00 -07001842 /* remove devs from iscsi_sessions to scsi_devices */
1843 qla4xxx_free_ddb_list(ha);
1844
1845 scsi_remove_host(ha->host);
1846
1847 qla4xxx_free_adapter(ha);
1848
1849 scsi_host_put(ha->host);
1850
Lalit Chandivade2232be02010-07-30 14:38:47 +05301851 pci_disable_pcie_error_reporting(pdev);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301852 pci_disable_device(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001853 pci_set_drvdata(pdev, NULL);
1854}
1855
1856/**
1857 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
1858 * @ha: HA context
1859 *
1860 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
1861 * supported addressing method.
1862 */
Adrian Bunk47975472007-04-26 00:35:16 -07001863static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001864{
1865 int retval;
1866
1867 /* Update our PCI device dma_mask for full 64 bit mask */
Yang Hongyang6a355282009-04-06 19:01:13 -07001868 if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
1869 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001870 dev_dbg(&ha->pdev->dev,
1871 "Failed to set 64 bit PCI consistent mask; "
1872 "using 32 bit.\n");
1873 retval = pci_set_consistent_dma_mask(ha->pdev,
Yang Hongyang284901a2009-04-06 19:01:15 -07001874 DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001875 }
1876 } else
Yang Hongyang284901a2009-04-06 19:01:15 -07001877 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001878}
1879
1880static int qla4xxx_slave_alloc(struct scsi_device *sdev)
1881{
1882 struct iscsi_cls_session *sess = starget_to_session(sdev->sdev_target);
1883 struct ddb_entry *ddb = sess->dd_data;
1884
1885 sdev->hostdata = ddb;
1886 sdev->tagged_supported = 1;
Mike Christied510d962008-07-11 19:50:33 -05001887 scsi_activate_tcq(sdev, QL4_DEF_QDEPTH);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001888 return 0;
1889}
1890
1891static int qla4xxx_slave_configure(struct scsi_device *sdev)
1892{
1893 sdev->tagged_supported = 1;
1894 return 0;
1895}
1896
1897static void qla4xxx_slave_destroy(struct scsi_device *sdev)
1898{
1899 scsi_deactivate_tcq(sdev, 1);
1900}
1901
1902/**
1903 * qla4xxx_del_from_active_array - returns an active srb
1904 * @ha: Pointer to host adapter structure.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001905 * @index: index into the active_array
David Somayajuluafaf5a22006-09-19 10:28:00 -07001906 *
1907 * This routine removes and returns the srb at the specified index
1908 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301909struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
1910 uint32_t index)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001911{
1912 struct srb *srb = NULL;
Vikas Chaudhary53698872010-04-28 11:41:59 +05301913 struct scsi_cmnd *cmd = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001914
Vikas Chaudhary53698872010-04-28 11:41:59 +05301915 cmd = scsi_host_find_tag(ha->host, index);
1916 if (!cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001917 return srb;
1918
Vikas Chaudhary53698872010-04-28 11:41:59 +05301919 srb = (struct srb *)CMD_SP(cmd);
1920 if (!srb)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001921 return srb;
1922
1923 /* update counters */
1924 if (srb->flags & SRB_DMA_VALID) {
1925 ha->req_q_count += srb->iocb_cnt;
1926 ha->iocb_cnt -= srb->iocb_cnt;
1927 if (srb->cmd)
Vikas Chaudhary53698872010-04-28 11:41:59 +05301928 srb->cmd->host_scribble =
1929 (unsigned char *)(unsigned long) MAX_SRBS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001930 }
1931 return srb;
1932}
1933
1934/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07001935 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301936 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001937 * @cmd: Scsi Command to wait on.
1938 *
1939 * This routine waits for the command to be returned by the Firmware
1940 * for some max time.
1941 **/
1942static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
1943 struct scsi_cmnd *cmd)
1944{
1945 int done = 0;
1946 struct srb *rp;
1947 uint32_t max_wait_time = EH_WAIT_CMD_TOV;
Lalit Chandivade2232be02010-07-30 14:38:47 +05301948 int ret = SUCCESS;
1949
1950 /* Dont wait on command if PCI error is being handled
1951 * by PCI AER driver
1952 */
1953 if (unlikely(pci_channel_offline(ha->pdev)) ||
1954 (test_bit(AF_EEH_BUSY, &ha->flags))) {
1955 ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
1956 ha->host_no, __func__);
1957 return ret;
1958 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001959
1960 do {
1961 /* Checking to see if its returned to OS */
Vikas Chaudhary53698872010-04-28 11:41:59 +05301962 rp = (struct srb *) CMD_SP(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001963 if (rp == NULL) {
1964 done++;
1965 break;
1966 }
1967
1968 msleep(2000);
1969 } while (max_wait_time--);
1970
1971 return done;
1972}
1973
1974/**
1975 * qla4xxx_wait_for_hba_online - waits for HBA to come online
1976 * @ha: Pointer to host adapter structure
1977 **/
1978static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
1979{
1980 unsigned long wait_online;
1981
Vikas Chaudharyf581a3f2010-10-06 22:47:48 -07001982 wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001983 while (time_before(jiffies, wait_online)) {
1984
1985 if (adapter_up(ha))
1986 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001987
1988 msleep(2000);
1989 }
1990
1991 return QLA_ERROR;
1992}
1993
1994/**
Mike Christiece545032008-02-29 18:25:20 -06001995 * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001996 * @ha: pointer to HBA
David Somayajuluafaf5a22006-09-19 10:28:00 -07001997 * @t: target id
1998 * @l: lun id
1999 *
2000 * This function waits for all outstanding commands to a lun to complete. It
2001 * returns 0 if all pending commands are returned and 1 otherwise.
2002 **/
Mike Christiece545032008-02-29 18:25:20 -06002003static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
2004 struct scsi_target *stgt,
2005 struct scsi_device *sdev)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002006{
2007 int cnt;
2008 int status = 0;
2009 struct scsi_cmnd *cmd;
2010
2011 /*
Mike Christiece545032008-02-29 18:25:20 -06002012 * Waiting for all commands for the designated target or dev
2013 * in the active array
David Somayajuluafaf5a22006-09-19 10:28:00 -07002014 */
2015 for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
2016 cmd = scsi_host_find_tag(ha->host, cnt);
Mike Christiece545032008-02-29 18:25:20 -06002017 if (cmd && stgt == scsi_target(cmd->device) &&
2018 (!sdev || sdev == cmd->device)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002019 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
2020 status++;
2021 break;
2022 }
2023 }
2024 }
2025 return status;
2026}
2027
2028/**
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302029 * qla4xxx_eh_abort - callback for abort task.
2030 * @cmd: Pointer to Linux's SCSI command structure
2031 *
2032 * This routine is called by the Linux OS to abort the specified
2033 * command.
2034 **/
2035static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
2036{
2037 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2038 unsigned int id = cmd->device->id;
2039 unsigned int lun = cmd->device->lun;
2040 unsigned long serial = cmd->serial_number;
Mike Christie92b3e5b2010-10-06 22:51:17 -07002041 unsigned long flags;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302042 struct srb *srb = NULL;
2043 int ret = SUCCESS;
2044 int wait = 0;
2045
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302046 ql4_printk(KERN_INFO, ha,
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302047 "scsi%ld:%d:%d: Abort command issued cmd=%p, pid=%ld\n",
2048 ha->host_no, id, lun, cmd, serial);
2049
Mike Christie92b3e5b2010-10-06 22:51:17 -07002050 spin_lock_irqsave(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302051 srb = (struct srb *) CMD_SP(cmd);
Mike Christie92b3e5b2010-10-06 22:51:17 -07002052 if (!srb) {
2053 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302054 return SUCCESS;
Mike Christie92b3e5b2010-10-06 22:51:17 -07002055 }
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302056 kref_get(&srb->srb_ref);
Mike Christie92b3e5b2010-10-06 22:51:17 -07002057 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302058
2059 if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
2060 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
2061 ha->host_no, id, lun));
2062 ret = FAILED;
2063 } else {
2064 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
2065 ha->host_no, id, lun));
2066 wait = 1;
2067 }
2068
2069 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
2070
2071 /* Wait for command to complete */
2072 if (wait) {
2073 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
2074 DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
2075 ha->host_no, id, lun));
2076 ret = FAILED;
2077 }
2078 }
2079
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302080 ql4_printk(KERN_INFO, ha,
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302081 "scsi%ld:%d:%d: Abort command - %s\n",
2082 ha->host_no, id, lun, (ret == SUCCESS) ? "succeded" : "failed");
2083
2084 return ret;
2085}
2086
2087/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002088 * qla4xxx_eh_device_reset - callback for target reset.
2089 * @cmd: Pointer to Linux's SCSI command structure
2090 *
2091 * This routine is called by the Linux OS to reset all luns on the
2092 * specified target.
2093 **/
2094static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
2095{
2096 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2097 struct ddb_entry *ddb_entry = cmd->device->hostdata;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002098 int ret = FAILED, stat;
2099
Karen Higgins612f73482009-07-15 15:03:01 -05002100 if (!ddb_entry)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002101 return ret;
2102
Mike Christiec01be6d2010-07-22 16:59:49 +05302103 ret = iscsi_block_scsi_eh(cmd);
2104 if (ret)
2105 return ret;
2106 ret = FAILED;
2107
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302108 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002109 "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
2110 cmd->device->channel, cmd->device->id, cmd->device->lun);
2111
2112 DEBUG2(printk(KERN_INFO
2113 "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
2114 "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
Jens Axboe242f9dc2008-09-14 05:55:09 -07002115 cmd, jiffies, cmd->request->timeout / HZ,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002116 ha->dpc_flags, cmd->result, cmd->allowed));
2117
2118 /* FIXME: wait for hba to go online */
2119 stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
2120 if (stat != QLA_SUCCESS) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302121 ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002122 goto eh_dev_reset_done;
2123 }
2124
Mike Christiece545032008-02-29 18:25:20 -06002125 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2126 cmd->device)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302127 ql4_printk(KERN_INFO, ha,
Mike Christiece545032008-02-29 18:25:20 -06002128 "DEVICE RESET FAILED - waiting for "
2129 "commands.\n");
2130 goto eh_dev_reset_done;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002131 }
2132
David C Somayajulu9d562912008-03-19 11:23:03 -07002133 /* Send marker. */
2134 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2135 MM_LUN_RESET) != QLA_SUCCESS)
2136 goto eh_dev_reset_done;
2137
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302138 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002139 "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
2140 ha->host_no, cmd->device->channel, cmd->device->id,
2141 cmd->device->lun);
2142
2143 ret = SUCCESS;
2144
2145eh_dev_reset_done:
2146
2147 return ret;
2148}
2149
2150/**
Mike Christiece545032008-02-29 18:25:20 -06002151 * qla4xxx_eh_target_reset - callback for target reset.
2152 * @cmd: Pointer to Linux's SCSI command structure
2153 *
2154 * This routine is called by the Linux OS to reset the target.
2155 **/
2156static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
2157{
2158 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2159 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christiec01be6d2010-07-22 16:59:49 +05302160 int stat, ret;
Mike Christiece545032008-02-29 18:25:20 -06002161
2162 if (!ddb_entry)
2163 return FAILED;
2164
Mike Christiec01be6d2010-07-22 16:59:49 +05302165 ret = iscsi_block_scsi_eh(cmd);
2166 if (ret)
2167 return ret;
2168
Mike Christiece545032008-02-29 18:25:20 -06002169 starget_printk(KERN_INFO, scsi_target(cmd->device),
2170 "WARM TARGET RESET ISSUED.\n");
2171
2172 DEBUG2(printk(KERN_INFO
2173 "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
2174 "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
Jens Axboe242f9dc2008-09-14 05:55:09 -07002175 ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
Mike Christiece545032008-02-29 18:25:20 -06002176 ha->dpc_flags, cmd->result, cmd->allowed));
2177
2178 stat = qla4xxx_reset_target(ha, ddb_entry);
2179 if (stat != QLA_SUCCESS) {
2180 starget_printk(KERN_INFO, scsi_target(cmd->device),
2181 "WARM TARGET RESET FAILED.\n");
2182 return FAILED;
2183 }
2184
Mike Christiece545032008-02-29 18:25:20 -06002185 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2186 NULL)) {
2187 starget_printk(KERN_INFO, scsi_target(cmd->device),
2188 "WARM TARGET DEVICE RESET FAILED - "
2189 "waiting for commands.\n");
2190 return FAILED;
2191 }
2192
David C Somayajulu9d562912008-03-19 11:23:03 -07002193 /* Send marker. */
2194 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2195 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
2196 starget_printk(KERN_INFO, scsi_target(cmd->device),
2197 "WARM TARGET DEVICE RESET FAILED - "
2198 "marker iocb failed.\n");
2199 return FAILED;
2200 }
2201
Mike Christiece545032008-02-29 18:25:20 -06002202 starget_printk(KERN_INFO, scsi_target(cmd->device),
2203 "WARM TARGET RESET SUCCEEDED.\n");
2204 return SUCCESS;
2205}
2206
2207/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002208 * qla4xxx_eh_host_reset - kernel callback
2209 * @cmd: Pointer to Linux's SCSI command structure
2210 *
2211 * This routine is invoked by the Linux kernel to perform fatal error
2212 * recovery on the specified adapter.
2213 **/
2214static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
2215{
2216 int return_status = FAILED;
2217 struct scsi_qla_host *ha;
2218
2219 ha = (struct scsi_qla_host *) cmd->device->host->hostdata;
2220
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302221 if (ql4xdontresethba) {
2222 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
2223 ha->host_no, __func__));
2224 return FAILED;
2225 }
2226
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302227 ql4_printk(KERN_INFO, ha,
Karen Higginsdca05c42009-07-15 15:03:00 -05002228 "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002229 cmd->device->channel, cmd->device->id, cmd->device->lun);
2230
2231 if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
2232 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
2233 "DEAD.\n", ha->host_no, cmd->device->channel,
2234 __func__));
2235
2236 return FAILED;
2237 }
2238
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302239 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
2240 if (is_qla8022(ha))
2241 set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2242 else
2243 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2244 }
Mike Christie50a29ae2008-03-04 13:26:53 -06002245
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302246 if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002247 return_status = SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002248
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302249 ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
David Somayajuluafaf5a22006-09-19 10:28:00 -07002250 return_status == FAILED ? "FAILED" : "SUCCEDED");
2251
2252 return return_status;
2253}
2254
Lalit Chandivade2232be02010-07-30 14:38:47 +05302255/* PCI AER driver recovers from all correctable errors w/o
2256 * driver intervention. For uncorrectable errors PCI AER
2257 * driver calls the following device driver's callbacks
2258 *
2259 * - Fatal Errors - link_reset
2260 * - Non-Fatal Errors - driver's pci_error_detected() which
2261 * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
2262 *
2263 * PCI AER driver calls
2264 * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
2265 * returns RECOVERED or NEED_RESET if fw_hung
2266 * NEED_RESET - driver's slot_reset()
2267 * DISCONNECT - device is dead & cannot recover
2268 * RECOVERED - driver's pci_resume()
2269 */
2270static pci_ers_result_t
2271qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
2272{
2273 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2274
2275 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
2276 ha->host_no, __func__, state);
2277
2278 if (!is_aer_supported(ha))
2279 return PCI_ERS_RESULT_NONE;
2280
2281 switch (state) {
2282 case pci_channel_io_normal:
2283 clear_bit(AF_EEH_BUSY, &ha->flags);
2284 return PCI_ERS_RESULT_CAN_RECOVER;
2285 case pci_channel_io_frozen:
2286 set_bit(AF_EEH_BUSY, &ha->flags);
2287 qla4xxx_mailbox_premature_completion(ha);
2288 qla4xxx_free_irqs(ha);
2289 pci_disable_device(pdev);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002290 /* Return back all IOs */
2291 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
Lalit Chandivade2232be02010-07-30 14:38:47 +05302292 return PCI_ERS_RESULT_NEED_RESET;
2293 case pci_channel_io_perm_failure:
2294 set_bit(AF_EEH_BUSY, &ha->flags);
2295 set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
2296 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
2297 return PCI_ERS_RESULT_DISCONNECT;
2298 }
2299 return PCI_ERS_RESULT_NEED_RESET;
2300}
2301
2302/**
2303 * qla4xxx_pci_mmio_enabled() gets called if
2304 * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
2305 * and read/write to the device still works.
2306 **/
2307static pci_ers_result_t
2308qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
2309{
2310 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2311
2312 if (!is_aer_supported(ha))
2313 return PCI_ERS_RESULT_NONE;
2314
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002315 return PCI_ERS_RESULT_RECOVERED;
Lalit Chandivade2232be02010-07-30 14:38:47 +05302316}
2317
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002318static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
Lalit Chandivade2232be02010-07-30 14:38:47 +05302319{
2320 uint32_t rval = QLA_ERROR;
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002321 uint32_t ret = 0;
Lalit Chandivade2232be02010-07-30 14:38:47 +05302322 int fn;
2323 struct pci_dev *other_pdev = NULL;
2324
2325 ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
2326
2327 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2328
2329 if (test_bit(AF_ONLINE, &ha->flags)) {
2330 clear_bit(AF_ONLINE, &ha->flags);
2331 qla4xxx_mark_all_devices_missing(ha);
2332 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
Lalit Chandivade2232be02010-07-30 14:38:47 +05302333 }
2334
2335 fn = PCI_FUNC(ha->pdev->devfn);
2336 while (fn > 0) {
2337 fn--;
2338 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
2339 "func %x\n", ha->host_no, __func__, fn);
2340 /* Get the pci device given the domain, bus,
2341 * slot/function number */
2342 other_pdev =
2343 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
2344 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
2345 fn));
2346
2347 if (!other_pdev)
2348 continue;
2349
2350 if (atomic_read(&other_pdev->enable_cnt)) {
2351 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
2352 "func in enabled state%x\n", ha->host_no,
2353 __func__, fn);
2354 pci_dev_put(other_pdev);
2355 break;
2356 }
2357 pci_dev_put(other_pdev);
2358 }
2359
2360 /* The first function on the card, the reset owner will
2361 * start & initialize the firmware. The other functions
2362 * on the card will reset the firmware context
2363 */
2364 if (!fn) {
2365 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
2366 "0x%x is the owner\n", ha->host_no, __func__,
2367 ha->pdev->devfn);
2368
2369 qla4_8xxx_idc_lock(ha);
2370 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2371 QLA82XX_DEV_COLD);
2372
2373 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
2374 QLA82XX_IDC_VERSION);
2375
2376 qla4_8xxx_idc_unlock(ha);
2377 clear_bit(AF_FW_RECOVERY, &ha->flags);
2378 rval = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
2379 qla4_8xxx_idc_lock(ha);
2380
2381 if (rval != QLA_SUCCESS) {
2382 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
2383 "FAILED\n", ha->host_no, __func__);
2384 qla4_8xxx_clear_drv_active(ha);
2385 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2386 QLA82XX_DEV_FAILED);
2387 } else {
2388 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
2389 "READY\n", ha->host_no, __func__);
2390 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2391 QLA82XX_DEV_READY);
2392 /* Clear driver state register */
2393 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
2394 qla4_8xxx_set_drv_active(ha);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002395 ret = qla4xxx_request_irqs(ha);
2396 if (ret) {
2397 ql4_printk(KERN_WARNING, ha, "Failed to "
2398 "reserve interrupt %d already in use.\n",
2399 ha->pdev->irq);
2400 rval = QLA_ERROR;
2401 } else {
2402 ha->isp_ops->enable_intrs(ha);
2403 rval = QLA_SUCCESS;
2404 }
Lalit Chandivade2232be02010-07-30 14:38:47 +05302405 }
2406 qla4_8xxx_idc_unlock(ha);
2407 } else {
2408 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
2409 "the reset owner\n", ha->host_no, __func__,
2410 ha->pdev->devfn);
2411 if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
2412 QLA82XX_DEV_READY)) {
2413 clear_bit(AF_FW_RECOVERY, &ha->flags);
2414 rval = qla4xxx_initialize_adapter(ha,
2415 PRESERVE_DDB_LIST);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002416 if (rval == QLA_SUCCESS) {
2417 ret = qla4xxx_request_irqs(ha);
2418 if (ret) {
2419 ql4_printk(KERN_WARNING, ha, "Failed to"
2420 " reserve interrupt %d already in"
2421 " use.\n", ha->pdev->irq);
2422 rval = QLA_ERROR;
2423 } else {
2424 ha->isp_ops->enable_intrs(ha);
2425 rval = QLA_SUCCESS;
2426 }
2427 }
Lalit Chandivade2232be02010-07-30 14:38:47 +05302428 qla4_8xxx_idc_lock(ha);
2429 qla4_8xxx_set_drv_active(ha);
2430 qla4_8xxx_idc_unlock(ha);
2431 }
2432 }
2433 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2434 return rval;
2435}
2436
2437static pci_ers_result_t
2438qla4xxx_pci_slot_reset(struct pci_dev *pdev)
2439{
2440 pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
2441 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2442 int rc;
2443
2444 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
2445 ha->host_no, __func__);
2446
2447 if (!is_aer_supported(ha))
2448 return PCI_ERS_RESULT_NONE;
2449
2450 /* Restore the saved state of PCIe device -
2451 * BAR registers, PCI Config space, PCIX, MSI,
2452 * IOV states
2453 */
2454 pci_restore_state(pdev);
2455
2456 /* pci_restore_state() clears the saved_state flag of the device
2457 * save restored state which resets saved_state flag
2458 */
2459 pci_save_state(pdev);
2460
2461 /* Initialize device or resume if in suspended state */
2462 rc = pci_enable_device(pdev);
2463 if (rc) {
2464 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Cant re-enable "
2465 "device after reset\n", ha->host_no, __func__);
2466 goto exit_slot_reset;
2467 }
2468
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002469 ha->isp_ops->disable_intrs(ha);
Lalit Chandivade2232be02010-07-30 14:38:47 +05302470
2471 if (is_qla8022(ha)) {
2472 if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
2473 ret = PCI_ERS_RESULT_RECOVERED;
2474 goto exit_slot_reset;
2475 } else
2476 goto exit_slot_reset;
2477 }
2478
2479exit_slot_reset:
2480 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
2481 "device after reset\n", ha->host_no, __func__, ret);
2482 return ret;
2483}
2484
2485static void
2486qla4xxx_pci_resume(struct pci_dev *pdev)
2487{
2488 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2489 int ret;
2490
2491 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
2492 ha->host_no, __func__);
2493
2494 ret = qla4xxx_wait_for_hba_online(ha);
2495 if (ret != QLA_SUCCESS) {
2496 ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
2497 "resume I/O from slot/link_reset\n", ha->host_no,
2498 __func__);
2499 }
2500
2501 pci_cleanup_aer_uncorrect_error_status(pdev);
2502 clear_bit(AF_EEH_BUSY, &ha->flags);
2503}
2504
2505static struct pci_error_handlers qla4xxx_err_handler = {
2506 .error_detected = qla4xxx_pci_error_detected,
2507 .mmio_enabled = qla4xxx_pci_mmio_enabled,
2508 .slot_reset = qla4xxx_pci_slot_reset,
2509 .resume = qla4xxx_pci_resume,
2510};
2511
David Somayajuluafaf5a22006-09-19 10:28:00 -07002512static struct pci_device_id qla4xxx_pci_tbl[] = {
2513 {
2514 .vendor = PCI_VENDOR_ID_QLOGIC,
2515 .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
2516 .subvendor = PCI_ANY_ID,
2517 .subdevice = PCI_ANY_ID,
2518 },
2519 {
2520 .vendor = PCI_VENDOR_ID_QLOGIC,
2521 .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
2522 .subvendor = PCI_ANY_ID,
2523 .subdevice = PCI_ANY_ID,
2524 },
David C Somayajulud9150582006-11-15 17:38:40 -08002525 {
2526 .vendor = PCI_VENDOR_ID_QLOGIC,
2527 .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
2528 .subvendor = PCI_ANY_ID,
2529 .subdevice = PCI_ANY_ID,
2530 },
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302531 {
2532 .vendor = PCI_VENDOR_ID_QLOGIC,
2533 .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
2534 .subvendor = PCI_ANY_ID,
2535 .subdevice = PCI_ANY_ID,
2536 },
David Somayajuluafaf5a22006-09-19 10:28:00 -07002537 {0, 0},
2538};
2539MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
2540
Adrian Bunk47975472007-04-26 00:35:16 -07002541static struct pci_driver qla4xxx_pci_driver = {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002542 .name = DRIVER_NAME,
2543 .id_table = qla4xxx_pci_tbl,
2544 .probe = qla4xxx_probe_adapter,
2545 .remove = qla4xxx_remove_adapter,
Lalit Chandivade2232be02010-07-30 14:38:47 +05302546 .err_handler = &qla4xxx_err_handler,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002547};
2548
2549static int __init qla4xxx_module_init(void)
2550{
2551 int ret;
2552
2553 /* Allocate cache for SRBs. */
2554 srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09002555 SLAB_HWCACHE_ALIGN, NULL);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002556 if (srb_cachep == NULL) {
2557 printk(KERN_ERR
2558 "%s: Unable to allocate SRB cache..."
2559 "Failing load!\n", DRIVER_NAME);
2560 ret = -ENOMEM;
2561 goto no_srp_cache;
2562 }
2563
2564 /* Derive version string. */
2565 strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
Andrew Vasquez11010fe2006-10-06 09:54:59 -07002566 if (ql4xextended_error_logging)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002567 strcat(qla4xxx_version_str, "-debug");
2568
2569 qla4xxx_scsi_transport =
2570 iscsi_register_transport(&qla4xxx_iscsi_transport);
2571 if (!qla4xxx_scsi_transport){
2572 ret = -ENODEV;
2573 goto release_srb_cache;
2574 }
2575
David Somayajuluafaf5a22006-09-19 10:28:00 -07002576 ret = pci_register_driver(&qla4xxx_pci_driver);
2577 if (ret)
2578 goto unregister_transport;
2579
2580 printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
2581 return 0;
Doug Maxey5ae16db2006-10-05 23:50:07 -05002582
David Somayajuluafaf5a22006-09-19 10:28:00 -07002583unregister_transport:
2584 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
2585release_srb_cache:
2586 kmem_cache_destroy(srb_cachep);
2587no_srp_cache:
2588 return ret;
2589}
2590
2591static void __exit qla4xxx_module_exit(void)
2592{
David Somayajuluafaf5a22006-09-19 10:28:00 -07002593 pci_unregister_driver(&qla4xxx_pci_driver);
2594 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
2595 kmem_cache_destroy(srb_cachep);
2596}
2597
2598module_init(qla4xxx_module_init);
2599module_exit(qla4xxx_module_exit);
2600
2601MODULE_AUTHOR("QLogic Corporation");
2602MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
2603MODULE_LICENSE("GPL");
2604MODULE_VERSION(QLA4XXX_DRIVER_VERSION);