blob: a6455fbe4f4fa1bbde3a43ea1d4c5f8e900db2f8 [file] [log] [blame]
David Somayajuluafaf5a22006-09-19 10:28:00 -07001/*
2 * QLogic iSCSI HBA Driver
3 * Copyright (c) 2003-2006 QLogic Corporation
4 *
5 * See LICENSE.qla4xxx for copyright and licensing details.
6 */
7#include <linux/moduleparam.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
David Somayajuluafaf5a22006-09-19 10:28:00 -07009
10#include <scsi/scsi_tcq.h>
11#include <scsi/scsicam.h>
12
13#include "ql4_def.h"
David C Somayajulubee4fe82007-05-23 18:03:32 -070014#include "ql4_version.h"
15#include "ql4_glbl.h"
16#include "ql4_dbg.h"
17#include "ql4_inline.h"
David Somayajuluafaf5a22006-09-19 10:28:00 -070018
19/*
20 * Driver version
21 */
Adrian Bunk47975472007-04-26 00:35:16 -070022static char qla4xxx_version_str[40];
David Somayajuluafaf5a22006-09-19 10:28:00 -070023
24/*
25 * SRB allocation cache
26 */
Christoph Lametere18b8902006-12-06 20:33:20 -080027static struct kmem_cache *srb_cachep;
David Somayajuluafaf5a22006-09-19 10:28:00 -070028
29/*
30 * Module parameter information and variables
31 */
32int ql4xdiscoverywait = 60;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053033module_param(ql4xdiscoverywait, int, S_IRUGO | S_IWUSR);
David Somayajuluafaf5a22006-09-19 10:28:00 -070034MODULE_PARM_DESC(ql4xdiscoverywait, "Discovery wait time");
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053035
David Somayajuluafaf5a22006-09-19 10:28:00 -070036int ql4xdontresethba = 0;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053037module_param(ql4xdontresethba, int, S_IRUGO | S_IWUSR);
David Somayajuluafaf5a22006-09-19 10:28:00 -070038MODULE_PARM_DESC(ql4xdontresethba,
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053039 "Don't reset the HBA for driver recovery \n"
40 " 0 - It will reset HBA (Default)\n"
41 " 1 - It will NOT reset HBA");
David Somayajuluafaf5a22006-09-19 10:28:00 -070042
Andrew Vasquez11010fe2006-10-06 09:54:59 -070043int ql4xextended_error_logging = 0; /* 0 = off, 1 = log errors */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053044module_param(ql4xextended_error_logging, int, S_IRUGO | S_IWUSR);
Andrew Vasquez11010fe2006-10-06 09:54:59 -070045MODULE_PARM_DESC(ql4xextended_error_logging,
David Somayajuluafaf5a22006-09-19 10:28:00 -070046 "Option to enable extended error logging, "
47 "Default is 0 - no logging, 1 - debug logging");
48
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053049int ql4xenablemsix = 1;
50module_param(ql4xenablemsix, int, S_IRUGO|S_IWUSR);
51MODULE_PARM_DESC(ql4xenablemsix,
52 "Set to enable MSI or MSI-X interrupt mechanism.\n"
53 " 0 = enable INTx interrupt mechanism.\n"
54 " 1 = enable MSI-X interrupt mechanism (Default).\n"
55 " 2 = enable MSI interrupt mechanism.");
David C Somayajulu477ffb92007-01-22 12:26:11 -080056
Mike Christied510d962008-07-11 19:50:33 -050057#define QL4_DEF_QDEPTH 32
58
David Somayajuluafaf5a22006-09-19 10:28:00 -070059/*
60 * SCSI host template entry points
61 */
Adrian Bunk47975472007-04-26 00:35:16 -070062static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -070063
64/*
65 * iSCSI template entry points
66 */
Mike Christie2174a042007-05-30 12:57:10 -050067static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
68 enum iscsi_tgt_dscvr type, uint32_t enable,
69 struct sockaddr *dst_addr);
David Somayajuluafaf5a22006-09-19 10:28:00 -070070static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
71 enum iscsi_param param, char *buf);
72static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
73 enum iscsi_param param, char *buf);
Mike Christieaa1e93a2007-05-30 12:57:09 -050074static int qla4xxx_host_get_param(struct Scsi_Host *shost,
75 enum iscsi_host_param param, char *buf);
David Somayajuluafaf5a22006-09-19 10:28:00 -070076static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session);
Mike Christie5c656af2009-07-15 15:02:59 -050077static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
David Somayajuluafaf5a22006-09-19 10:28:00 -070078
79/*
80 * SCSI host template entry points
81 */
82static int qla4xxx_queuecommand(struct scsi_cmnd *cmd,
83 void (*done) (struct scsi_cmnd *));
Vikas Chaudhary09a0f712010-04-28 11:42:24 +053084static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -070085static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
Mike Christiece545032008-02-29 18:25:20 -060086static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -070087static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
88static int qla4xxx_slave_alloc(struct scsi_device *device);
89static int qla4xxx_slave_configure(struct scsi_device *device);
90static void qla4xxx_slave_destroy(struct scsi_device *sdev);
Mike Christie024f8012008-03-04 13:26:54 -060091static void qla4xxx_scan_start(struct Scsi_Host *shost);
David Somayajuluafaf5a22006-09-19 10:28:00 -070092
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053093static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
94 QLA82XX_LEGACY_INTR_CONFIG;
95
David Somayajuluafaf5a22006-09-19 10:28:00 -070096static struct scsi_host_template qla4xxx_driver_template = {
97 .module = THIS_MODULE,
98 .name = DRIVER_NAME,
99 .proc_name = DRIVER_NAME,
100 .queuecommand = qla4xxx_queuecommand,
101
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530102 .eh_abort_handler = qla4xxx_eh_abort,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700103 .eh_device_reset_handler = qla4xxx_eh_device_reset,
Mike Christiece545032008-02-29 18:25:20 -0600104 .eh_target_reset_handler = qla4xxx_eh_target_reset,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700105 .eh_host_reset_handler = qla4xxx_eh_host_reset,
Mike Christie5c656af2009-07-15 15:02:59 -0500106 .eh_timed_out = qla4xxx_eh_cmd_timed_out,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700107
108 .slave_configure = qla4xxx_slave_configure,
109 .slave_alloc = qla4xxx_slave_alloc,
110 .slave_destroy = qla4xxx_slave_destroy,
111
Mike Christie921601b2008-01-31 13:36:49 -0600112 .scan_finished = iscsi_scan_finished,
Mike Christie024f8012008-03-04 13:26:54 -0600113 .scan_start = qla4xxx_scan_start,
Mike Christie921601b2008-01-31 13:36:49 -0600114
David Somayajuluafaf5a22006-09-19 10:28:00 -0700115 .this_id = -1,
116 .cmd_per_lun = 3,
117 .use_clustering = ENABLE_CLUSTERING,
118 .sg_tablesize = SG_ALL,
119
120 .max_sectors = 0xFFFF,
121};
122
123static struct iscsi_transport qla4xxx_iscsi_transport = {
124 .owner = THIS_MODULE,
125 .name = DRIVER_NAME,
Mike Christied8196ed2007-05-30 12:57:25 -0500126 .caps = CAP_FW_DB | CAP_SENDTARGETS_OFFLOAD |
127 CAP_DATA_PATH_OFFLOAD,
Mike Christieaa1e93a2007-05-30 12:57:09 -0500128 .param_mask = ISCSI_CONN_PORT | ISCSI_CONN_ADDRESS |
Vikas Chaudhary3b2bef12010-07-10 14:51:30 +0530129 ISCSI_TARGET_NAME | ISCSI_TPGT |
130 ISCSI_TARGET_ALIAS,
Mike Christie8ad57812007-05-30 12:57:13 -0500131 .host_param_mask = ISCSI_HOST_HWADDRESS |
Mike Christie22236962007-05-30 12:57:24 -0500132 ISCSI_HOST_IPADDRESS |
Mike Christie8ad57812007-05-30 12:57:13 -0500133 ISCSI_HOST_INITIATOR_NAME,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700134 .tgt_dscvr = qla4xxx_tgt_dscvr,
135 .get_conn_param = qla4xxx_conn_get_param,
136 .get_session_param = qla4xxx_sess_get_param,
Mike Christieaa1e93a2007-05-30 12:57:09 -0500137 .get_host_param = qla4xxx_host_get_param,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700138 .session_recovery_timedout = qla4xxx_recovery_timedout,
139};
140
141static struct scsi_transport_template *qla4xxx_scsi_transport;
142
Mike Christie5c656af2009-07-15 15:02:59 -0500143static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
144{
145 struct iscsi_cls_session *session;
146 struct ddb_entry *ddb_entry;
147
148 session = starget_to_session(scsi_target(sc->device));
149 ddb_entry = session->dd_data;
150
151 /* if we are not logged in then the LLD is going to clean up the cmd */
152 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
153 return BLK_EH_RESET_TIMER;
154 else
155 return BLK_EH_NOT_HANDLED;
156}
157
David Somayajuluafaf5a22006-09-19 10:28:00 -0700158static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session)
159{
160 struct ddb_entry *ddb_entry = session->dd_data;
161 struct scsi_qla_host *ha = ddb_entry->ha;
162
Mike Christie568d3032008-01-31 13:36:47 -0600163 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
164 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700165
Vikas Chaudhary3013cea2010-07-30 14:25:46 +0530166 DEBUG2(printk("scsi%ld: %s: ddb [%d] session recovery timeout "
Mike Christie568d3032008-01-31 13:36:47 -0600167 "of (%d) secs exhausted, marking device DEAD.\n",
168 ha->host_no, __func__, ddb_entry->fw_ddb_index,
Vikas Chaudhary3013cea2010-07-30 14:25:46 +0530169 QL4_SESS_RECOVERY_TMO));
Mike Christie568d3032008-01-31 13:36:47 -0600170 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700171}
172
Mike Christieaa1e93a2007-05-30 12:57:09 -0500173static int qla4xxx_host_get_param(struct Scsi_Host *shost,
174 enum iscsi_host_param param, char *buf)
175{
176 struct scsi_qla_host *ha = to_qla_host(shost);
177 int len;
178
179 switch (param) {
180 case ISCSI_HOST_PARAM_HWADDRESS:
Michael Chan7ffc49a2007-12-24 21:28:09 -0800181 len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
Mike Christieaa1e93a2007-05-30 12:57:09 -0500182 break;
Mike Christie22236962007-05-30 12:57:24 -0500183 case ISCSI_HOST_PARAM_IPADDRESS:
184 len = sprintf(buf, "%d.%d.%d.%d\n", ha->ip_address[0],
185 ha->ip_address[1], ha->ip_address[2],
186 ha->ip_address[3]);
187 break;
Mike Christie8ad57812007-05-30 12:57:13 -0500188 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie22236962007-05-30 12:57:24 -0500189 len = sprintf(buf, "%s\n", ha->name_string);
Mike Christie8ad57812007-05-30 12:57:13 -0500190 break;
Mike Christieaa1e93a2007-05-30 12:57:09 -0500191 default:
192 return -ENOSYS;
193 }
194
195 return len;
196}
197
David Somayajuluafaf5a22006-09-19 10:28:00 -0700198static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
199 enum iscsi_param param, char *buf)
200{
201 struct ddb_entry *ddb_entry = sess->dd_data;
202 int len;
203
204 switch (param) {
205 case ISCSI_PARAM_TARGET_NAME:
206 len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
207 ddb_entry->iscsi_name);
208 break;
209 case ISCSI_PARAM_TPGT:
210 len = sprintf(buf, "%u\n", ddb_entry->tpgt);
211 break;
Vikas Chaudhary3b2bef12010-07-10 14:51:30 +0530212 case ISCSI_PARAM_TARGET_ALIAS:
213 len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
214 ddb_entry->iscsi_alias);
215 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700216 default:
217 return -ENOSYS;
218 }
219
220 return len;
221}
222
223static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
224 enum iscsi_param param, char *buf)
225{
226 struct iscsi_cls_session *session;
227 struct ddb_entry *ddb_entry;
228 int len;
229
230 session = iscsi_dev_to_session(conn->dev.parent);
231 ddb_entry = session->dd_data;
232
233 switch (param) {
234 case ISCSI_PARAM_CONN_PORT:
235 len = sprintf(buf, "%hu\n", ddb_entry->port);
236 break;
237 case ISCSI_PARAM_CONN_ADDRESS:
238 /* TODO: what are the ipv6 bits */
Harvey Harrison63779432008-10-31 00:56:00 -0700239 len = sprintf(buf, "%pI4\n", &ddb_entry->ip_addr);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700240 break;
241 default:
242 return -ENOSYS;
243 }
244
245 return len;
246}
247
Mike Christie2174a042007-05-30 12:57:10 -0500248static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
249 enum iscsi_tgt_dscvr type, uint32_t enable,
250 struct sockaddr *dst_addr)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700251{
252 struct scsi_qla_host *ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700253 struct sockaddr_in *addr;
254 struct sockaddr_in6 *addr6;
255 int ret = 0;
256
David Somayajuluafaf5a22006-09-19 10:28:00 -0700257 ha = (struct scsi_qla_host *) shost->hostdata;
258
259 switch (type) {
260 case ISCSI_TGT_DSCVR_SEND_TARGETS:
261 if (dst_addr->sa_family == AF_INET) {
262 addr = (struct sockaddr_in *)dst_addr;
263 if (qla4xxx_send_tgts(ha, (char *)&addr->sin_addr,
264 addr->sin_port) != QLA_SUCCESS)
265 ret = -EIO;
266 } else if (dst_addr->sa_family == AF_INET6) {
267 /*
268 * TODO: fix qla4xxx_send_tgts
269 */
270 addr6 = (struct sockaddr_in6 *)dst_addr;
271 if (qla4xxx_send_tgts(ha, (char *)&addr6->sin6_addr,
272 addr6->sin6_port) != QLA_SUCCESS)
273 ret = -EIO;
274 } else
275 ret = -ENOSYS;
276 break;
277 default:
278 ret = -ENOSYS;
279 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700280 return ret;
281}
282
283void qla4xxx_destroy_sess(struct ddb_entry *ddb_entry)
284{
285 if (!ddb_entry->sess)
286 return;
287
288 if (ddb_entry->conn) {
Mike Christie26974782007-12-13 12:43:29 -0600289 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700290 iscsi_remove_session(ddb_entry->sess);
291 }
292 iscsi_free_session(ddb_entry->sess);
293}
294
295int qla4xxx_add_sess(struct ddb_entry *ddb_entry)
296{
297 int err;
298
Vikas Chaudhary3013cea2010-07-30 14:25:46 +0530299 ddb_entry->sess->recovery_tmo = QL4_SESS_RECOVERY_TMO;
300
David Somayajuluafaf5a22006-09-19 10:28:00 -0700301 err = iscsi_add_session(ddb_entry->sess, ddb_entry->fw_ddb_index);
302 if (err) {
303 DEBUG2(printk(KERN_ERR "Could not add session.\n"));
304 return err;
305 }
306
Mike Christie5d91e202008-05-21 15:54:01 -0500307 ddb_entry->conn = iscsi_create_conn(ddb_entry->sess, 0, 0);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700308 if (!ddb_entry->conn) {
309 iscsi_remove_session(ddb_entry->sess);
310 DEBUG2(printk(KERN_ERR "Could not add connection.\n"));
311 return -ENOMEM;
312 }
Mike Christieb6359302008-01-31 13:36:44 -0600313
314 /* finally ready to go */
315 iscsi_unblock_session(ddb_entry->sess);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700316 return 0;
317}
318
319struct ddb_entry *qla4xxx_alloc_sess(struct scsi_qla_host *ha)
320{
321 struct ddb_entry *ddb_entry;
322 struct iscsi_cls_session *sess;
323
Mike Christie5d91e202008-05-21 15:54:01 -0500324 sess = iscsi_alloc_session(ha->host, &qla4xxx_iscsi_transport,
325 sizeof(struct ddb_entry));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700326 if (!sess)
327 return NULL;
328
329 ddb_entry = sess->dd_data;
330 memset(ddb_entry, 0, sizeof(*ddb_entry));
331 ddb_entry->ha = ha;
332 ddb_entry->sess = sess;
333 return ddb_entry;
334}
335
Mike Christie024f8012008-03-04 13:26:54 -0600336static void qla4xxx_scan_start(struct Scsi_Host *shost)
337{
338 struct scsi_qla_host *ha = shost_priv(shost);
339 struct ddb_entry *ddb_entry, *ddbtemp;
340
341 /* finish setup of sessions that were already setup in firmware */
342 list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
343 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE)
344 qla4xxx_add_sess(ddb_entry);
345 }
346}
347
David Somayajuluafaf5a22006-09-19 10:28:00 -0700348/*
349 * Timer routines
350 */
351
352static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
353 unsigned long interval)
354{
355 DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
356 __func__, ha->host->host_no));
357 init_timer(&ha->timer);
358 ha->timer.expires = jiffies + interval * HZ;
359 ha->timer.data = (unsigned long)ha;
360 ha->timer.function = (void (*)(unsigned long))func;
361 add_timer(&ha->timer);
362 ha->timer_active = 1;
363}
364
365static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
366{
367 del_timer_sync(&ha->timer);
368 ha->timer_active = 0;
369}
370
371/***
372 * qla4xxx_mark_device_missing - mark a device as missing.
373 * @ha: Pointer to host adapter structure.
374 * @ddb_entry: Pointer to device database entry
375 *
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530376 * This routine marks a device missing and close connection.
David Somayajuluafaf5a22006-09-19 10:28:00 -0700377 **/
378void qla4xxx_mark_device_missing(struct scsi_qla_host *ha,
379 struct ddb_entry *ddb_entry)
380{
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530381 if ((atomic_read(&ddb_entry->state) != DDB_STATE_DEAD)) {
382 atomic_set(&ddb_entry->state, DDB_STATE_MISSING);
383 DEBUG2(printk("scsi%ld: ddb [%d] marked MISSING\n",
384 ha->host_no, ddb_entry->fw_ddb_index));
385 } else
386 DEBUG2(printk("scsi%ld: ddb [%d] DEAD\n", ha->host_no,
387 ddb_entry->fw_ddb_index))
388
Mike Christieb6359302008-01-31 13:36:44 -0600389 iscsi_block_session(ddb_entry->sess);
Mike Christiee5bd7b52008-09-24 11:46:10 -0500390 iscsi_conn_error_event(ddb_entry->conn, ISCSI_ERR_CONN_FAILED);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700391}
392
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530393/**
394 * qla4xxx_mark_all_devices_missing - mark all devices as missing.
395 * @ha: Pointer to host adapter structure.
396 *
397 * This routine marks a device missing and resets the relogin retry count.
398 **/
399void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
400{
401 struct ddb_entry *ddb_entry, *ddbtemp;
402 list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
403 qla4xxx_mark_device_missing(ha, ddb_entry);
404 }
405}
406
David Somayajuluafaf5a22006-09-19 10:28:00 -0700407static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
408 struct ddb_entry *ddb_entry,
409 struct scsi_cmnd *cmd,
410 void (*done)(struct scsi_cmnd *))
411{
412 struct srb *srb;
413
414 srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
415 if (!srb)
416 return srb;
417
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530418 kref_init(&srb->srb_ref);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700419 srb->ha = ha;
420 srb->ddb = ddb_entry;
421 srb->cmd = cmd;
422 srb->flags = 0;
Vikas Chaudhary53698872010-04-28 11:41:59 +0530423 CMD_SP(cmd) = (void *)srb;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700424 cmd->scsi_done = done;
425
426 return srb;
427}
428
429static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
430{
431 struct scsi_cmnd *cmd = srb->cmd;
432
433 if (srb->flags & SRB_DMA_VALID) {
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +0900434 scsi_dma_unmap(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700435 srb->flags &= ~SRB_DMA_VALID;
436 }
Vikas Chaudhary53698872010-04-28 11:41:59 +0530437 CMD_SP(cmd) = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700438}
439
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530440void qla4xxx_srb_compl(struct kref *ref)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700441{
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530442 struct srb *srb = container_of(ref, struct srb, srb_ref);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700443 struct scsi_cmnd *cmd = srb->cmd;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530444 struct scsi_qla_host *ha = srb->ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700445
446 qla4xxx_srb_free_dma(ha, srb);
447
448 mempool_free(srb, ha->srb_mempool);
449
450 cmd->scsi_done(cmd);
451}
452
453/**
454 * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
455 * @cmd: Pointer to Linux's SCSI command structure
456 * @done_fn: Function that the driver calls to notify the SCSI mid-layer
457 * that the command has been processed.
458 *
459 * Remarks:
460 * This routine is invoked by Linux to send a SCSI command to the driver.
461 * The mid-level driver tries to ensure that queuecommand never gets
462 * invoked concurrently with itself or the interrupt handler (although
463 * the interrupt handler may call this routine as part of request-
464 * completion handling). Unfortunely, it sometimes calls the scheduler
465 * in interrupt context which is a big NO! NO!.
466 **/
467static int qla4xxx_queuecommand(struct scsi_cmnd *cmd,
468 void (*done)(struct scsi_cmnd *))
469{
470 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
471 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christie7fb19212008-01-31 13:36:45 -0600472 struct iscsi_cls_session *sess = ddb_entry->sess;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700473 struct srb *srb;
474 int rval;
475
Lalit Chandivade2232be02010-07-30 14:38:47 +0530476 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
477 if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
478 cmd->result = DID_NO_CONNECT << 16;
479 else
480 cmd->result = DID_REQUEUE << 16;
481 goto qc_fail_command;
482 }
483
Mike Christie7fb19212008-01-31 13:36:45 -0600484 if (!sess) {
485 cmd->result = DID_IMM_RETRY << 16;
486 goto qc_fail_command;
487 }
488
489 rval = iscsi_session_chkready(sess);
490 if (rval) {
491 cmd->result = rval;
492 goto qc_fail_command;
493 }
494
David Somayajuluafaf5a22006-09-19 10:28:00 -0700495 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
496 if (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD) {
497 cmd->result = DID_NO_CONNECT << 16;
498 goto qc_fail_command;
499 }
Mike Christiec5e98e92008-08-17 15:24:39 -0500500 return SCSI_MLQUEUE_TARGET_BUSY;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700501 }
502
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530503 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
504 test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
505 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
506 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
507 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
508 !test_bit(AF_ONLINE, &ha->flags) ||
509 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
David C Somayajulu477ffb92007-01-22 12:26:11 -0800510 goto qc_host_busy;
511
David Somayajuluafaf5a22006-09-19 10:28:00 -0700512 spin_unlock_irq(ha->host->host_lock);
513
514 srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd, done);
515 if (!srb)
516 goto qc_host_busy_lock;
517
518 rval = qla4xxx_send_command_to_isp(ha, srb);
519 if (rval != QLA_SUCCESS)
520 goto qc_host_busy_free_sp;
521
522 spin_lock_irq(ha->host->host_lock);
523 return 0;
524
525qc_host_busy_free_sp:
526 qla4xxx_srb_free_dma(ha, srb);
527 mempool_free(srb, ha->srb_mempool);
528
529qc_host_busy_lock:
530 spin_lock_irq(ha->host->host_lock);
531
532qc_host_busy:
533 return SCSI_MLQUEUE_HOST_BUSY;
534
535qc_fail_command:
536 done(cmd);
537
538 return 0;
539}
540
541/**
542 * qla4xxx_mem_free - frees memory allocated to adapter
543 * @ha: Pointer to host adapter structure.
544 *
545 * Frees memory previously allocated by qla4xxx_mem_alloc
546 **/
547static void qla4xxx_mem_free(struct scsi_qla_host *ha)
548{
549 if (ha->queues)
550 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
551 ha->queues_dma);
552
553 ha->queues_len = 0;
554 ha->queues = NULL;
555 ha->queues_dma = 0;
556 ha->request_ring = NULL;
557 ha->request_dma = 0;
558 ha->response_ring = NULL;
559 ha->response_dma = 0;
560 ha->shadow_regs = NULL;
561 ha->shadow_regs_dma = 0;
562
563 /* Free srb pool. */
564 if (ha->srb_mempool)
565 mempool_destroy(ha->srb_mempool);
566
567 ha->srb_mempool = NULL;
568
569 /* release io space registers */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530570 if (is_qla8022(ha)) {
571 if (ha->nx_pcibase)
572 iounmap(
573 (struct device_reg_82xx __iomem *)ha->nx_pcibase);
574
575 if (ha->nx_db_wr_ptr)
576 iounmap(
577 (struct device_reg_82xx __iomem *)ha->nx_db_wr_ptr);
578 } else if (ha->reg)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700579 iounmap(ha->reg);
580 pci_release_regions(ha->pdev);
581}
582
583/**
584 * qla4xxx_mem_alloc - allocates memory for use by adapter.
585 * @ha: Pointer to host adapter structure
586 *
587 * Allocates DMA memory for request and response queues. Also allocates memory
588 * for srbs.
589 **/
590static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
591{
592 unsigned long align;
593
594 /* Allocate contiguous block of DMA memory for queues. */
595 ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
596 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
597 sizeof(struct shadow_regs) +
598 MEM_ALIGN_VALUE +
599 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
600 ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
601 &ha->queues_dma, GFP_KERNEL);
602 if (ha->queues == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530603 ql4_printk(KERN_WARNING, ha,
604 "Memory Allocation failed - queues.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700605
606 goto mem_alloc_error_exit;
607 }
608 memset(ha->queues, 0, ha->queues_len);
609
610 /*
611 * As per RISC alignment requirements -- the bus-address must be a
612 * multiple of the request-ring size (in bytes).
613 */
614 align = 0;
615 if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
616 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
617 (MEM_ALIGN_VALUE - 1));
618
619 /* Update request and response queue pointers. */
620 ha->request_dma = ha->queues_dma + align;
621 ha->request_ring = (struct queue_entry *) (ha->queues + align);
622 ha->response_dma = ha->queues_dma + align +
623 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
624 ha->response_ring = (struct queue_entry *) (ha->queues + align +
625 (REQUEST_QUEUE_DEPTH *
626 QUEUE_SIZE));
627 ha->shadow_regs_dma = ha->queues_dma + align +
628 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
629 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
630 ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
631 (REQUEST_QUEUE_DEPTH *
632 QUEUE_SIZE) +
633 (RESPONSE_QUEUE_DEPTH *
634 QUEUE_SIZE));
635
636 /* Allocate memory for srb pool. */
637 ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
638 mempool_free_slab, srb_cachep);
639 if (ha->srb_mempool == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530640 ql4_printk(KERN_WARNING, ha,
641 "Memory Allocation failed - SRB Pool.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700642
643 goto mem_alloc_error_exit;
644 }
645
646 return QLA_SUCCESS;
647
648mem_alloc_error_exit:
649 qla4xxx_mem_free(ha);
650 return QLA_ERROR;
651}
652
653/**
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530654 * qla4_8xxx_check_fw_alive - Check firmware health
655 * @ha: Pointer to host adapter structure.
656 *
657 * Context: Interrupt
658 **/
659static void qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
660{
661 uint32_t fw_heartbeat_counter, halt_status;
662
663 fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
Lalit Chandivade2232be02010-07-30 14:38:47 +0530664 /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
665 if (fw_heartbeat_counter == 0xffffffff) {
666 DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
667 "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
668 ha->host_no, __func__));
669 return;
670 }
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530671
672 if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
673 ha->seconds_since_last_heartbeat++;
674 /* FW not alive after 2 seconds */
675 if (ha->seconds_since_last_heartbeat == 2) {
676 ha->seconds_since_last_heartbeat = 0;
677 halt_status = qla4_8xxx_rd_32(ha,
678 QLA82XX_PEG_HALT_STATUS1);
Nilesh Javali21033632010-07-30 14:28:07 +0530679
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530680 /* Since we cannot change dev_state in interrupt
681 * context, set appropriate DPC flag then wakeup
682 * DPC */
683 if (halt_status & HALT_STATUS_UNRECOVERABLE)
684 set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
685 else {
686 printk("scsi%ld: %s: detect abort needed!\n",
687 ha->host_no, __func__);
688 set_bit(DPC_RESET_HA, &ha->dpc_flags);
689 }
690 qla4xxx_wake_dpc(ha);
Nilesh Javali21033632010-07-30 14:28:07 +0530691 qla4xxx_mailbox_premature_completion(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530692 }
693 }
694 ha->fw_heartbeat_counter = fw_heartbeat_counter;
695}
696
697/**
698 * qla4_8xxx_watchdog - Poll dev state
699 * @ha: Pointer to host adapter structure.
700 *
701 * Context: Interrupt
702 **/
703void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
704{
705 uint32_t dev_state;
706
707 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
708
709 /* don't poll if reset is going on */
710 if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags)) {
711 if (dev_state == QLA82XX_DEV_NEED_RESET &&
712 !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
713 printk("scsi%ld: %s: HW State: NEED RESET!\n",
714 ha->host_no, __func__);
715 set_bit(DPC_RESET_HA, &ha->dpc_flags);
716 qla4xxx_wake_dpc(ha);
Nilesh Javali21033632010-07-30 14:28:07 +0530717 qla4xxx_mailbox_premature_completion(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530718 } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
719 !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
720 printk("scsi%ld: %s: HW State: NEED QUIES!\n",
721 ha->host_no, __func__);
722 set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
723 qla4xxx_wake_dpc(ha);
724 } else {
725 /* Check firmware health */
726 qla4_8xxx_check_fw_alive(ha);
727 }
728 }
729}
730
731/**
David Somayajuluafaf5a22006-09-19 10:28:00 -0700732 * qla4xxx_timer - checks every second for work to do.
733 * @ha: Pointer to host adapter structure.
734 **/
735static void qla4xxx_timer(struct scsi_qla_host *ha)
736{
737 struct ddb_entry *ddb_entry, *dtemp;
738 int start_dpc = 0;
Lalit Chandivade2232be02010-07-30 14:38:47 +0530739 uint16_t w;
740
741 /* If we are in the middle of AER/EEH processing
742 * skip any processing and reschedule the timer
743 */
744 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
745 mod_timer(&ha->timer, jiffies + HZ);
746 return;
747 }
748
749 /* Hardware read to trigger an EEH error during mailbox waits. */
750 if (!pci_channel_offline(ha->pdev))
751 pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700752
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530753 if (test_bit(AF_HBA_GOING_AWAY, &ha->flags)) {
754 DEBUG2(ql4_printk(KERN_INFO, ha, "%s exited. HBA GOING AWAY\n",
755 __func__));
756 return;
757 }
758
759 if (is_qla8022(ha)) {
760 qla4_8xxx_watchdog(ha);
761 }
762
David Somayajuluafaf5a22006-09-19 10:28:00 -0700763 /* Search for relogin's to time-out and port down retry. */
764 list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
765 /* Count down time between sending relogins */
766 if (adapter_up(ha) &&
767 !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
768 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
769 if (atomic_read(&ddb_entry->retry_relogin_timer) !=
770 INVALID_ENTRY) {
771 if (atomic_read(&ddb_entry->retry_relogin_timer)
772 == 0) {
773 atomic_set(&ddb_entry->
774 retry_relogin_timer,
775 INVALID_ENTRY);
776 set_bit(DPC_RELOGIN_DEVICE,
777 &ha->dpc_flags);
778 set_bit(DF_RELOGIN, &ddb_entry->flags);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530779 DEBUG2(printk("scsi%ld: %s: ddb [%d]"
David Somayajuluafaf5a22006-09-19 10:28:00 -0700780 " login device\n",
781 ha->host_no, __func__,
782 ddb_entry->fw_ddb_index));
783 } else
784 atomic_dec(&ddb_entry->
785 retry_relogin_timer);
786 }
787 }
788
789 /* Wait for relogin to timeout */
790 if (atomic_read(&ddb_entry->relogin_timer) &&
791 (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
792 /*
793 * If the relogin times out and the device is
794 * still NOT ONLINE then try and relogin again.
795 */
796 if (atomic_read(&ddb_entry->state) !=
797 DDB_STATE_ONLINE &&
798 ddb_entry->fw_ddb_device_state ==
799 DDB_DS_SESSION_FAILED) {
800 /* Reset retry relogin timer */
801 atomic_inc(&ddb_entry->relogin_retry_count);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530802 DEBUG2(printk("scsi%ld: ddb [%d] relogin"
David Somayajuluafaf5a22006-09-19 10:28:00 -0700803 " timed out-retrying"
804 " relogin (%d)\n",
805 ha->host_no,
806 ddb_entry->fw_ddb_index,
807 atomic_read(&ddb_entry->
808 relogin_retry_count))
809 );
810 start_dpc++;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530811 DEBUG(printk("scsi%ld:%d:%d: ddb [%d] "
David Somayajuluafaf5a22006-09-19 10:28:00 -0700812 "initate relogin after"
813 " %d seconds\n",
814 ha->host_no, ddb_entry->bus,
815 ddb_entry->target,
816 ddb_entry->fw_ddb_index,
817 ddb_entry->default_time2wait + 4)
818 );
819
820 atomic_set(&ddb_entry->retry_relogin_timer,
821 ddb_entry->default_time2wait + 4);
822 }
823 }
824 }
825
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530826 if (!is_qla8022(ha)) {
827 /* Check for heartbeat interval. */
828 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
829 ha->heartbeat_interval != 0) {
830 ha->seconds_since_last_heartbeat++;
831 if (ha->seconds_since_last_heartbeat >
832 ha->heartbeat_interval + 2)
833 set_bit(DPC_RESET_HA, &ha->dpc_flags);
834 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700835 }
836
David Somayajuluafaf5a22006-09-19 10:28:00 -0700837 /* Wakeup the dpc routine for this adapter, if needed. */
838 if ((start_dpc ||
839 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
840 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
841 test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530842 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -0700843 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
844 test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +0530845 test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530846 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
847 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -0700848 test_bit(DPC_AEN, &ha->dpc_flags)) &&
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530849 !test_bit(AF_DPC_SCHEDULED, &ha->flags) &&
David Somayajuluafaf5a22006-09-19 10:28:00 -0700850 ha->dpc_thread) {
851 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
852 " - dpc flags = 0x%lx\n",
853 ha->host_no, __func__, ha->dpc_flags));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530854 qla4xxx_wake_dpc(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700855 }
856
857 /* Reschedule timer thread to call us back in one second */
858 mod_timer(&ha->timer, jiffies + HZ);
859
860 DEBUG2(ha->seconds_since_last_intr++);
861}
862
863/**
864 * qla4xxx_cmd_wait - waits for all outstanding commands to complete
865 * @ha: Pointer to host adapter structure.
866 *
867 * This routine stalls the driver until all outstanding commands are returned.
868 * Caller must release the Hardware Lock prior to calling this routine.
869 **/
870static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
871{
872 uint32_t index = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700873 unsigned long flags;
874 struct scsi_cmnd *cmd;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700875
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530876 unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
877
878 DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
879 "complete\n", WAIT_CMD_TOV));
880
881 while (!time_after_eq(jiffies, wtime)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700882 spin_lock_irqsave(&ha->hardware_lock, flags);
883 /* Find a command that hasn't completed. */
884 for (index = 0; index < ha->host->can_queue; index++) {
885 cmd = scsi_host_find_tag(ha->host, index);
886 if (cmd != NULL)
887 break;
888 }
889 spin_unlock_irqrestore(&ha->hardware_lock, flags);
890
891 /* If No Commands are pending, wait is complete */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530892 if (index == ha->host->can_queue)
893 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700894
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530895 msleep(1000);
896 }
897 /* If we timed out on waiting for commands to come back
898 * return ERROR. */
899 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700900}
901
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530902int qla4xxx_hw_reset(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700903{
David Somayajuluafaf5a22006-09-19 10:28:00 -0700904 uint32_t ctrl_status;
David C Somayajulu477ffb92007-01-22 12:26:11 -0800905 unsigned long flags = 0;
906
907 DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700908
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530909 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
910 return QLA_ERROR;
911
David Somayajuluafaf5a22006-09-19 10:28:00 -0700912 spin_lock_irqsave(&ha->hardware_lock, flags);
913
914 /*
915 * If the SCSI Reset Interrupt bit is set, clear it.
916 * Otherwise, the Soft Reset won't work.
917 */
918 ctrl_status = readw(&ha->reg->ctrl_status);
919 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
920 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
921
922 /* Issue Soft Reset */
923 writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
924 readl(&ha->reg->ctrl_status);
925
926 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530927 return QLA_SUCCESS;
David C Somayajulu477ffb92007-01-22 12:26:11 -0800928}
929
930/**
931 * qla4xxx_soft_reset - performs soft reset.
932 * @ha: Pointer to host adapter structure.
933 **/
934int qla4xxx_soft_reset(struct scsi_qla_host *ha)
935{
936 uint32_t max_wait_time;
937 unsigned long flags = 0;
Vikas Chaudharyf931c532010-10-06 22:48:07 -0700938 int status;
David C Somayajulu477ffb92007-01-22 12:26:11 -0800939 uint32_t ctrl_status;
940
Vikas Chaudharyf931c532010-10-06 22:48:07 -0700941 status = qla4xxx_hw_reset(ha);
942 if (status != QLA_SUCCESS)
943 return status;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700944
Vikas Chaudharyf931c532010-10-06 22:48:07 -0700945 status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700946 /* Wait until the Network Reset Intr bit is cleared */
947 max_wait_time = RESET_INTR_TOV;
948 do {
949 spin_lock_irqsave(&ha->hardware_lock, flags);
950 ctrl_status = readw(&ha->reg->ctrl_status);
951 spin_unlock_irqrestore(&ha->hardware_lock, flags);
952
953 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
954 break;
955
956 msleep(1000);
957 } while ((--max_wait_time));
958
959 if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
960 DEBUG2(printk(KERN_WARNING
961 "scsi%ld: Network Reset Intr not cleared by "
962 "Network function, clearing it now!\n",
963 ha->host_no));
964 spin_lock_irqsave(&ha->hardware_lock, flags);
965 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
966 readl(&ha->reg->ctrl_status);
967 spin_unlock_irqrestore(&ha->hardware_lock, flags);
968 }
969
970 /* Wait until the firmware tells us the Soft Reset is done */
971 max_wait_time = SOFT_RESET_TOV;
972 do {
973 spin_lock_irqsave(&ha->hardware_lock, flags);
974 ctrl_status = readw(&ha->reg->ctrl_status);
975 spin_unlock_irqrestore(&ha->hardware_lock, flags);
976
977 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
978 status = QLA_SUCCESS;
979 break;
980 }
981
982 msleep(1000);
983 } while ((--max_wait_time));
984
985 /*
986 * Also, make sure that the SCSI Reset Interrupt bit has been cleared
987 * after the soft reset has taken place.
988 */
989 spin_lock_irqsave(&ha->hardware_lock, flags);
990 ctrl_status = readw(&ha->reg->ctrl_status);
991 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
992 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
993 readl(&ha->reg->ctrl_status);
994 }
995 spin_unlock_irqrestore(&ha->hardware_lock, flags);
996
997 /* If soft reset fails then most probably the bios on other
998 * function is also enabled.
999 * Since the initialization is sequential the other fn
1000 * wont be able to acknowledge the soft reset.
1001 * Issue a force soft reset to workaround this scenario.
1002 */
1003 if (max_wait_time == 0) {
1004 /* Issue Force Soft Reset */
1005 spin_lock_irqsave(&ha->hardware_lock, flags);
1006 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
1007 readl(&ha->reg->ctrl_status);
1008 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1009 /* Wait until the firmware tells us the Soft Reset is done */
1010 max_wait_time = SOFT_RESET_TOV;
1011 do {
1012 spin_lock_irqsave(&ha->hardware_lock, flags);
1013 ctrl_status = readw(&ha->reg->ctrl_status);
1014 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1015
1016 if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
1017 status = QLA_SUCCESS;
1018 break;
1019 }
1020
1021 msleep(1000);
1022 } while ((--max_wait_time));
1023 }
1024
1025 return status;
1026}
1027
1028/**
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301029 * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001030 * @ha: Pointer to host adapter structure.
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301031 * @res: returned scsi status
David Somayajuluafaf5a22006-09-19 10:28:00 -07001032 *
1033 * This routine is called just prior to a HARD RESET to return all
1034 * outstanding commands back to the Operating System.
1035 * Caller should make sure that the following locks are released
1036 * before this calling routine: Hardware lock, and io_request_lock.
1037 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301038static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001039{
1040 struct srb *srb;
1041 int i;
1042 unsigned long flags;
1043
1044 spin_lock_irqsave(&ha->hardware_lock, flags);
1045 for (i = 0; i < ha->host->can_queue; i++) {
1046 srb = qla4xxx_del_from_active_array(ha, i);
1047 if (srb != NULL) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301048 srb->cmd->result = res;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301049 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001050 }
1051 }
1052 spin_unlock_irqrestore(&ha->hardware_lock, flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001053}
1054
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301055void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
1056{
1057 clear_bit(AF_ONLINE, &ha->flags);
1058
1059 /* Disable the board */
1060 ql4_printk(KERN_INFO, ha, "Disabling the board\n");
1061 set_bit(AF_HBA_GOING_AWAY, &ha->flags);
1062
1063 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
1064 qla4xxx_mark_all_devices_missing(ha);
1065 clear_bit(AF_INIT_DONE, &ha->flags);
1066}
1067
David Somayajuluafaf5a22006-09-19 10:28:00 -07001068/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07001069 * qla4xxx_recover_adapter - recovers adapter after a fatal error
1070 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001071 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301072static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001073{
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301074 int status = QLA_ERROR;
1075 uint8_t reset_chip = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001076
1077 /* Stall incoming I/O until we are done */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301078 scsi_block_requests(ha->host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001079 clear_bit(AF_ONLINE, &ha->flags);
Mike Christie50a29ae2008-03-04 13:26:53 -06001080
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301081 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001082
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301083 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001084
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301085 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
1086 reset_chip = 1;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001087
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301088 /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
1089 * do not reset adapter, jump to initialize_adapter */
1090 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1091 status = QLA_SUCCESS;
1092 goto recover_ha_init_adapter;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001093 }
1094
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301095 /* For the ISP-82xx adapter, issue a stop_firmware if invoked
1096 * from eh_host_reset or ioctl module */
1097 if (is_qla8022(ha) && !reset_chip &&
1098 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
1099
1100 DEBUG2(ql4_printk(KERN_INFO, ha,
1101 "scsi%ld: %s - Performing stop_firmware...\n",
1102 ha->host_no, __func__));
1103 status = ha->isp_ops->reset_firmware(ha);
1104 if (status == QLA_SUCCESS) {
Nilesh Javali2bd1e2be2010-10-06 22:49:20 -07001105 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
1106 qla4xxx_cmd_wait(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301107 ha->isp_ops->disable_intrs(ha);
1108 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1109 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1110 } else {
1111 /* If the stop_firmware fails then
1112 * reset the entire chip */
1113 reset_chip = 1;
1114 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1115 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1116 }
1117 }
1118
1119 /* Issue full chip reset if recovering from a catastrophic error,
1120 * or if stop_firmware fails for ISP-82xx.
1121 * This is the default case for ISP-4xxx */
1122 if (!is_qla8022(ha) || reset_chip) {
Nilesh Javali2bd1e2be2010-10-06 22:49:20 -07001123 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
1124 qla4xxx_cmd_wait(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301125 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1126 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1127 DEBUG2(ql4_printk(KERN_INFO, ha,
1128 "scsi%ld: %s - Performing chip reset..\n",
1129 ha->host_no, __func__));
1130 status = ha->isp_ops->reset_chip(ha);
1131 }
1132
1133 /* Flush any pending ddb changed AENs */
1134 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1135
1136recover_ha_init_adapter:
1137 /* Upon successful firmware/chip reset, re-initialize the adapter */
1138 if (status == QLA_SUCCESS) {
1139 /* For ISP-4xxx, force function 1 to always initialize
1140 * before function 3 to prevent both funcions from
1141 * stepping on top of the other */
1142 if (!is_qla8022(ha) && (ha->mac_index == 3))
1143 ssleep(6);
1144
1145 /* NOTE: AF_ONLINE flag set upon successful completion of
1146 * qla4xxx_initialize_adapter */
1147 status = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
1148 }
1149
1150 /* Retry failed adapter initialization, if necessary
1151 * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
1152 * case to prevent ping-pong resets between functions */
1153 if (!test_bit(AF_ONLINE, &ha->flags) &&
1154 !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001155 /* Adapter initialization failed, see if we can retry
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301156 * resetting the ha.
1157 * Since we don't want to block the DPC for too long
1158 * with multiple resets in the same thread,
1159 * utilize DPC to retry */
David Somayajuluafaf5a22006-09-19 10:28:00 -07001160 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
1161 ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
1162 DEBUG2(printk("scsi%ld: recover adapter - retrying "
1163 "(%d) more times\n", ha->host_no,
1164 ha->retry_reset_ha_cnt));
1165 set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1166 status = QLA_ERROR;
1167 } else {
1168 if (ha->retry_reset_ha_cnt > 0) {
1169 /* Schedule another Reset HA--DPC will retry */
1170 ha->retry_reset_ha_cnt--;
1171 DEBUG2(printk("scsi%ld: recover adapter - "
1172 "retry remaining %d\n",
1173 ha->host_no,
1174 ha->retry_reset_ha_cnt));
1175 status = QLA_ERROR;
1176 }
1177
1178 if (ha->retry_reset_ha_cnt == 0) {
1179 /* Recover adapter retries have been exhausted.
1180 * Adapter DEAD */
1181 DEBUG2(printk("scsi%ld: recover adapter "
1182 "failed - board disabled\n",
1183 ha->host_no));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301184 qla4xxx_dead_adapter_cleanup(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001185 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1186 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301187 clear_bit(DPC_RESET_HA_FW_CONTEXT,
David Somayajuluafaf5a22006-09-19 10:28:00 -07001188 &ha->dpc_flags);
1189 status = QLA_ERROR;
1190 }
1191 }
1192 } else {
1193 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301194 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001195 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1196 }
1197
1198 ha->adapter_error_count++;
1199
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301200 if (test_bit(AF_ONLINE, &ha->flags))
1201 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001202
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301203 scsi_unblock_requests(ha->host);
1204
1205 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
1206 DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
1207 status == QLA_ERROR ? "FAILED" : "SUCCEDED"));
1208
David Somayajuluafaf5a22006-09-19 10:28:00 -07001209 return status;
1210}
1211
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301212void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
1213{
1214 if (ha->dpc_thread &&
1215 !test_bit(AF_DPC_SCHEDULED, &ha->flags)) {
1216 set_bit(AF_DPC_SCHEDULED, &ha->flags);
1217 queue_work(ha->dpc_thread, &ha->dpc_work);
1218 }
1219}
1220
David Somayajuluafaf5a22006-09-19 10:28:00 -07001221/**
1222 * qla4xxx_do_dpc - dpc routine
1223 * @data: in our case pointer to adapter structure
1224 *
1225 * This routine is a task that is schedule by the interrupt handler
1226 * to perform the background processing for interrupts. We put it
1227 * on a task queue that is consumed whenever the scheduler runs; that's
1228 * so you can do anything (i.e. put the process to sleep etc). In fact,
1229 * the mid-level tries to sleep when it reaches the driver threshold
1230 * "host->can_queue". This can cause a panic if we were in our interrupt code.
1231 **/
David Howellsc4028952006-11-22 14:57:56 +00001232static void qla4xxx_do_dpc(struct work_struct *work)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001233{
David Howellsc4028952006-11-22 14:57:56 +00001234 struct scsi_qla_host *ha =
1235 container_of(work, struct scsi_qla_host, dpc_work);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001236 struct ddb_entry *ddb_entry, *dtemp;
David C Somayajulu477ffb92007-01-22 12:26:11 -08001237 int status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001238
David C Somayajuluf26b9042006-11-15 16:41:09 -08001239 DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301240 "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
1241 ha->host_no, __func__, ha->flags, ha->dpc_flags))
David Somayajuluafaf5a22006-09-19 10:28:00 -07001242
1243 /* Initialization not yet finished. Don't do anything yet. */
1244 if (!test_bit(AF_INIT_DONE, &ha->flags))
Vikas Chaudhary0753b482010-07-30 14:27:19 +05301245 goto do_dpc_exit;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001246
Lalit Chandivade2232be02010-07-30 14:38:47 +05301247 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1248 DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
1249 ha->host_no, __func__, ha->flags));
1250 goto do_dpc_exit;
1251 }
1252
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301253 /* HBA is in the process of being permanently disabled.
1254 * Don't process anything */
1255 if (test_bit(AF_HBA_GOING_AWAY, &ha->flags))
1256 return;
1257
1258 if (is_qla8022(ha)) {
1259 if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
1260 qla4_8xxx_idc_lock(ha);
1261 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
1262 QLA82XX_DEV_FAILED);
1263 qla4_8xxx_idc_unlock(ha);
1264 ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
1265 qla4_8xxx_device_state_handler(ha);
1266 }
1267 if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
1268 qla4_8xxx_need_qsnt_handler(ha);
1269 }
1270 }
1271
1272 if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
1273 (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07001274 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301275 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
1276 if (ql4xdontresethba) {
1277 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
1278 ha->host_no, __func__));
1279 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
1280 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1281 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1282 goto dpc_post_reset_ha;
1283 }
1284 if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
1285 test_bit(DPC_RESET_HA, &ha->dpc_flags))
1286 qla4xxx_recover_adapter(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001287
David C Somayajulu477ffb92007-01-22 12:26:11 -08001288 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001289 uint8_t wait_time = RESET_INTR_TOV;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001290
David Somayajuluafaf5a22006-09-19 10:28:00 -07001291 while ((readw(&ha->reg->ctrl_status) &
1292 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
1293 if (--wait_time == 0)
1294 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001295 msleep(1000);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001296 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001297 if (wait_time == 0)
1298 DEBUG2(printk("scsi%ld: %s: SR|FSR "
1299 "bit not cleared-- resetting\n",
1300 ha->host_no, __func__));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301301 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
David C Somayajulu477ffb92007-01-22 12:26:11 -08001302 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
1303 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301304 status = qla4xxx_recover_adapter(ha);
David C Somayajulu477ffb92007-01-22 12:26:11 -08001305 }
1306 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1307 if (status == QLA_SUCCESS)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301308 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001309 }
1310 }
1311
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301312dpc_post_reset_ha:
David Somayajuluafaf5a22006-09-19 10:28:00 -07001313 /* ---- process AEN? --- */
1314 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
1315 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
1316
1317 /* ---- Get DHCP IP Address? --- */
1318 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
1319 qla4xxx_get_dhcp_ip_address(ha);
1320
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301321 /* ---- link change? --- */
1322 if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
1323 if (!test_bit(AF_LINK_UP, &ha->flags)) {
1324 /* ---- link down? --- */
1325 list_for_each_entry_safe(ddb_entry, dtemp,
1326 &ha->ddb_list, list) {
1327 if (atomic_read(&ddb_entry->state) ==
1328 DDB_STATE_ONLINE)
1329 qla4xxx_mark_device_missing(ha,
1330 ddb_entry);
1331 }
1332 } else {
1333 /* ---- link up? --- *
1334 * F/W will auto login to all devices ONLY ONCE after
1335 * link up during driver initialization and runtime
1336 * fatal error recovery. Therefore, the driver must
1337 * manually relogin to devices when recovering from
1338 * connection failures, logouts, expired KATO, etc. */
1339
1340 list_for_each_entry_safe(ddb_entry, dtemp,
1341 &ha->ddb_list, list) {
1342 if ((atomic_read(&ddb_entry->state) ==
1343 DDB_STATE_MISSING) ||
1344 (atomic_read(&ddb_entry->state) ==
1345 DDB_STATE_DEAD)) {
1346 if (ddb_entry->fw_ddb_device_state ==
1347 DDB_DS_SESSION_ACTIVE) {
1348 atomic_set(&ddb_entry->state,
1349 DDB_STATE_ONLINE);
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301350 ql4_printk(KERN_INFO, ha,
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301351 "scsi%ld: %s: ddb[%d]"
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301352 " marked ONLINE\n",
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301353 ha->host_no, __func__,
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301354 ddb_entry->fw_ddb_index);
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301355
1356 iscsi_unblock_session(
1357 ddb_entry->sess);
1358 } else
1359 qla4xxx_relogin_device(
1360 ha, ddb_entry);
1361 }
1362
1363 }
1364 }
1365 }
1366
David Somayajuluafaf5a22006-09-19 10:28:00 -07001367 /* ---- relogin device? --- */
1368 if (adapter_up(ha) &&
1369 test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
1370 list_for_each_entry_safe(ddb_entry, dtemp,
1371 &ha->ddb_list, list) {
1372 if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
1373 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
1374 qla4xxx_relogin_device(ha, ddb_entry);
1375
1376 /*
1377 * If mbx cmd times out there is no point
1378 * in continuing further.
1379 * With large no of targets this can hang
1380 * the system.
1381 */
1382 if (test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
1383 printk(KERN_WARNING "scsi%ld: %s: "
1384 "need to reset hba\n",
1385 ha->host_no, __func__);
1386 break;
1387 }
1388 }
1389 }
Vikas Chaudhary0753b482010-07-30 14:27:19 +05301390
1391do_dpc_exit:
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301392 clear_bit(AF_DPC_SCHEDULED, &ha->flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001393}
1394
1395/**
1396 * qla4xxx_free_adapter - release the adapter
1397 * @ha: pointer to adapter structure
1398 **/
1399static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
1400{
1401
1402 if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
1403 /* Turn-off interrupts on the card. */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301404 ha->isp_ops->disable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001405 }
1406
David Somayajuluafaf5a22006-09-19 10:28:00 -07001407 /* Remove timer thread, if present */
1408 if (ha->timer_active)
1409 qla4xxx_stop_timer(ha);
1410
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301411 /* Kill the kernel thread for this host */
1412 if (ha->dpc_thread)
1413 destroy_workqueue(ha->dpc_thread);
1414
1415 /* Put firmware in known state */
1416 ha->isp_ops->reset_firmware(ha);
1417
1418 if (is_qla8022(ha)) {
1419 qla4_8xxx_idc_lock(ha);
1420 qla4_8xxx_clear_drv_active(ha);
1421 qla4_8xxx_idc_unlock(ha);
1422 }
1423
David Somayajuluafaf5a22006-09-19 10:28:00 -07001424 /* Detach interrupts */
1425 if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301426 qla4xxx_free_irqs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001427
David C Somayajulubee4fe82007-05-23 18:03:32 -07001428 /* free extra memory */
1429 qla4xxx_mem_free(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301430}
David C Somayajulubee4fe82007-05-23 18:03:32 -07001431
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301432int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
1433{
1434 int status = 0;
1435 uint8_t revision_id;
1436 unsigned long mem_base, mem_len, db_base, db_len;
1437 struct pci_dev *pdev = ha->pdev;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001438
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301439 status = pci_request_regions(pdev, DRIVER_NAME);
1440 if (status) {
1441 printk(KERN_WARNING
1442 "scsi(%ld) Failed to reserve PIO regions (%s) "
1443 "status=%d\n", ha->host_no, pci_name(pdev), status);
1444 goto iospace_error_exit;
1445 }
1446
1447 pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
1448 DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
1449 __func__, revision_id));
1450 ha->revision_id = revision_id;
1451
1452 /* remap phys address */
1453 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
1454 mem_len = pci_resource_len(pdev, 0);
1455 DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
1456 __func__, mem_base, mem_len));
1457
1458 /* mapping of pcibase pointer */
1459 ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
1460 if (!ha->nx_pcibase) {
1461 printk(KERN_ERR
1462 "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
1463 pci_release_regions(ha->pdev);
1464 goto iospace_error_exit;
1465 }
1466
1467 /* Mapping of IO base pointer, door bell read and write pointer */
1468
1469 /* mapping of IO base pointer */
1470 ha->qla4_8xxx_reg =
1471 (struct device_reg_82xx __iomem *)((uint8_t *)ha->nx_pcibase +
1472 0xbc000 + (ha->pdev->devfn << 11));
1473
1474 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
1475 db_len = pci_resource_len(pdev, 4);
1476
1477 /* mapping of doorbell write pointer */
1478 ha->nx_db_wr_ptr = (unsigned long)ioremap(db_base +
1479 (ha->pdev->devfn << 12), 4);
1480 if (!ha->nx_db_wr_ptr) {
1481 printk(KERN_ERR
1482 "cannot remap MMIO doorbell-write (%s), aborting\n",
1483 pci_name(pdev));
1484 goto iospace_error_exit;
1485 }
1486 /* mapping of doorbell read pointer */
1487 ha->nx_db_rd_ptr = (uint8_t *) ha->nx_pcibase + (512 * 1024) +
1488 (ha->pdev->devfn * 8);
1489 if (!ha->nx_db_rd_ptr)
1490 printk(KERN_ERR
1491 "cannot remap MMIO doorbell-read (%s), aborting\n",
1492 pci_name(pdev));
1493 return 0;
1494
1495iospace_error_exit:
1496 return -ENOMEM;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001497}
1498
1499/***
1500 * qla4xxx_iospace_config - maps registers
1501 * @ha: pointer to adapter structure
1502 *
1503 * This routines maps HBA's registers from the pci address space
1504 * into the kernel virtual address space for memory mapped i/o.
1505 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301506int qla4xxx_iospace_config(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001507{
1508 unsigned long pio, pio_len, pio_flags;
1509 unsigned long mmio, mmio_len, mmio_flags;
1510
1511 pio = pci_resource_start(ha->pdev, 0);
1512 pio_len = pci_resource_len(ha->pdev, 0);
1513 pio_flags = pci_resource_flags(ha->pdev, 0);
1514 if (pio_flags & IORESOURCE_IO) {
1515 if (pio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301516 ql4_printk(KERN_WARNING, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07001517 "Invalid PCI I/O region size\n");
1518 pio = 0;
1519 }
1520 } else {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301521 ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001522 pio = 0;
1523 }
1524
1525 /* Use MMIO operations for all accesses. */
1526 mmio = pci_resource_start(ha->pdev, 1);
1527 mmio_len = pci_resource_len(ha->pdev, 1);
1528 mmio_flags = pci_resource_flags(ha->pdev, 1);
1529
1530 if (!(mmio_flags & IORESOURCE_MEM)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301531 ql4_printk(KERN_ERR, ha,
1532 "region #0 not an MMIO resource, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001533
1534 goto iospace_error_exit;
1535 }
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301536
David Somayajuluafaf5a22006-09-19 10:28:00 -07001537 if (mmio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301538 ql4_printk(KERN_ERR, ha,
1539 "Invalid PCI mem region size, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001540 goto iospace_error_exit;
1541 }
1542
1543 if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301544 ql4_printk(KERN_WARNING, ha,
1545 "Failed to reserve PIO/MMIO regions\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001546
1547 goto iospace_error_exit;
1548 }
1549
1550 ha->pio_address = pio;
1551 ha->pio_length = pio_len;
1552 ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
1553 if (!ha->reg) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301554 ql4_printk(KERN_ERR, ha,
1555 "cannot remap MMIO, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001556
1557 goto iospace_error_exit;
1558 }
1559
1560 return 0;
1561
1562iospace_error_exit:
1563 return -ENOMEM;
1564}
1565
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301566static struct isp_operations qla4xxx_isp_ops = {
1567 .iospace_config = qla4xxx_iospace_config,
1568 .pci_config = qla4xxx_pci_config,
1569 .disable_intrs = qla4xxx_disable_intrs,
1570 .enable_intrs = qla4xxx_enable_intrs,
1571 .start_firmware = qla4xxx_start_firmware,
1572 .intr_handler = qla4xxx_intr_handler,
1573 .interrupt_service_routine = qla4xxx_interrupt_service_routine,
1574 .reset_chip = qla4xxx_soft_reset,
1575 .reset_firmware = qla4xxx_hw_reset,
1576 .queue_iocb = qla4xxx_queue_iocb,
1577 .complete_iocb = qla4xxx_complete_iocb,
1578 .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
1579 .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
1580 .get_sys_info = qla4xxx_get_sys_info,
1581};
1582
1583static struct isp_operations qla4_8xxx_isp_ops = {
1584 .iospace_config = qla4_8xxx_iospace_config,
1585 .pci_config = qla4_8xxx_pci_config,
1586 .disable_intrs = qla4_8xxx_disable_intrs,
1587 .enable_intrs = qla4_8xxx_enable_intrs,
1588 .start_firmware = qla4_8xxx_load_risc,
1589 .intr_handler = qla4_8xxx_intr_handler,
1590 .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
1591 .reset_chip = qla4_8xxx_isp_reset,
1592 .reset_firmware = qla4_8xxx_stop_firmware,
1593 .queue_iocb = qla4_8xxx_queue_iocb,
1594 .complete_iocb = qla4_8xxx_complete_iocb,
1595 .rd_shdw_req_q_out = qla4_8xxx_rd_shdw_req_q_out,
1596 .rd_shdw_rsp_q_in = qla4_8xxx_rd_shdw_rsp_q_in,
1597 .get_sys_info = qla4_8xxx_get_sys_info,
1598};
1599
1600uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
1601{
1602 return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
1603}
1604
1605uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
1606{
1607 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
1608}
1609
1610uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
1611{
1612 return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
1613}
1614
1615uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
1616{
1617 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
1618}
1619
David Somayajuluafaf5a22006-09-19 10:28:00 -07001620/**
1621 * qla4xxx_probe_adapter - callback function to probe HBA
1622 * @pdev: pointer to pci_dev structure
1623 * @pci_device_id: pointer to pci_device entry
1624 *
1625 * This routine will probe for Qlogic 4xxx iSCSI host adapters.
1626 * It returns zero if successful. It also initializes all data necessary for
1627 * the driver.
1628 **/
1629static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
1630 const struct pci_device_id *ent)
1631{
1632 int ret = -ENODEV, status;
1633 struct Scsi_Host *host;
1634 struct scsi_qla_host *ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001635 uint8_t init_retry_count = 0;
1636 char buf[34];
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301637 struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001638
1639 if (pci_enable_device(pdev))
1640 return -1;
1641
1642 host = scsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha));
1643 if (host == NULL) {
1644 printk(KERN_WARNING
1645 "qla4xxx: Couldn't allocate host from scsi layer!\n");
1646 goto probe_disable_device;
1647 }
1648
1649 /* Clear our data area */
1650 ha = (struct scsi_qla_host *) host->hostdata;
1651 memset(ha, 0, sizeof(*ha));
1652
1653 /* Save the information from PCI BIOS. */
1654 ha->pdev = pdev;
1655 ha->host = host;
1656 ha->host_no = host->host_no;
1657
Lalit Chandivade2232be02010-07-30 14:38:47 +05301658 pci_enable_pcie_error_reporting(pdev);
1659
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301660 /* Setup Runtime configurable options */
1661 if (is_qla8022(ha)) {
1662 ha->isp_ops = &qla4_8xxx_isp_ops;
1663 rwlock_init(&ha->hw_lock);
1664 ha->qdr_sn_window = -1;
1665 ha->ddr_mn_window = -1;
1666 ha->curr_window = 255;
1667 ha->func_num = PCI_FUNC(ha->pdev->devfn);
1668 nx_legacy_intr = &legacy_intr[ha->func_num];
1669 ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
1670 ha->nx_legacy_intr.tgt_status_reg =
1671 nx_legacy_intr->tgt_status_reg;
1672 ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
1673 ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
1674 } else {
1675 ha->isp_ops = &qla4xxx_isp_ops;
1676 }
1677
Lalit Chandivade2232be02010-07-30 14:38:47 +05301678 /* Set EEH reset type to fundamental if required by hba */
1679 if (is_qla8022(ha))
1680 pdev->needs_freset = 1;
1681
David Somayajuluafaf5a22006-09-19 10:28:00 -07001682 /* Configure PCI I/O space. */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301683 ret = ha->isp_ops->iospace_config(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001684 if (ret)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301685 goto probe_failed_ioconfig;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001686
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301687 ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
David Somayajuluafaf5a22006-09-19 10:28:00 -07001688 pdev->device, pdev->irq, ha->reg);
1689
1690 qla4xxx_config_dma_addressing(ha);
1691
1692 /* Initialize lists and spinlocks. */
1693 INIT_LIST_HEAD(&ha->ddb_list);
1694 INIT_LIST_HEAD(&ha->free_srb_q);
1695
1696 mutex_init(&ha->mbox_sem);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301697 init_completion(&ha->mbx_intr_comp);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001698
1699 spin_lock_init(&ha->hardware_lock);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001700
1701 /* Allocate dma buffers */
1702 if (qla4xxx_mem_alloc(ha)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301703 ql4_printk(KERN_WARNING, ha,
1704 "[ERROR] Failed to allocate memory for adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001705
1706 ret = -ENOMEM;
1707 goto probe_failed;
1708 }
1709
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301710 if (is_qla8022(ha))
1711 (void) qla4_8xxx_get_flash_info(ha);
1712
David Somayajuluafaf5a22006-09-19 10:28:00 -07001713 /*
1714 * Initialize the Host adapter request/response queues and
1715 * firmware
1716 * NOTE: interrupts enabled upon successful completion
1717 */
1718 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301719 while ((!test_bit(AF_ONLINE, &ha->flags)) &&
1720 init_retry_count++ < MAX_INIT_RETRIES) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001721 DEBUG2(printk("scsi: %s: retrying adapter initialization "
1722 "(%d)\n", __func__, init_retry_count));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301723
1724 if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
1725 continue;
1726
David Somayajuluafaf5a22006-09-19 10:28:00 -07001727 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1728 }
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301729
1730 if (!test_bit(AF_ONLINE, &ha->flags)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301731 ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001732
1733 ret = -ENODEV;
1734 goto probe_failed;
1735 }
1736
1737 host->cmd_per_lun = 3;
1738 host->max_channel = 0;
1739 host->max_lun = MAX_LUNS - 1;
1740 host->max_id = MAX_TARGETS;
1741 host->max_cmd_len = IOCB_MAX_CDB_LEN;
1742 host->can_queue = MAX_SRBS ;
1743 host->transportt = qla4xxx_scsi_transport;
1744
1745 ret = scsi_init_shared_tag_map(host, MAX_SRBS);
1746 if (ret) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301747 ql4_printk(KERN_WARNING, ha,
1748 "scsi_init_shared_tag_map failed\n");
1749 goto probe_failed;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001750 }
1751
1752 /* Startup the kernel thread for this host adapter. */
1753 DEBUG2(printk("scsi: %s: Starting kernel thread for "
1754 "qla4xxx_dpc\n", __func__));
1755 sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
1756 ha->dpc_thread = create_singlethread_workqueue(buf);
1757 if (!ha->dpc_thread) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301758 ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001759 ret = -ENODEV;
1760 goto probe_failed;
1761 }
David Howellsc4028952006-11-22 14:57:56 +00001762 INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001763
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301764 /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
1765 * (which is called indirectly by qla4xxx_initialize_adapter),
1766 * so that irqs will be registered after crbinit but before
1767 * mbx_intr_enable.
1768 */
1769 if (!is_qla8022(ha)) {
1770 ret = qla4xxx_request_irqs(ha);
1771 if (ret) {
1772 ql4_printk(KERN_WARNING, ha, "Failed to reserve "
1773 "interrupt %d already in use.\n", pdev->irq);
1774 goto probe_failed;
1775 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001776 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001777
Lalit Chandivade2232be02010-07-30 14:38:47 +05301778 pci_save_state(ha->pdev);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301779 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001780
1781 /* Start timer thread. */
1782 qla4xxx_start_timer(ha, qla4xxx_timer, 1);
1783
1784 set_bit(AF_INIT_DONE, &ha->flags);
1785
1786 pci_set_drvdata(pdev, ha);
1787
1788 ret = scsi_add_host(host, &pdev->dev);
1789 if (ret)
1790 goto probe_failed;
1791
David Somayajuluafaf5a22006-09-19 10:28:00 -07001792 printk(KERN_INFO
1793 " QLogic iSCSI HBA Driver version: %s\n"
1794 " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
1795 qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
1796 ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
1797 ha->patch_number, ha->build_number);
Mike Christie921601b2008-01-31 13:36:49 -06001798 scsi_scan_host(host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001799 return 0;
1800
David Somayajuluafaf5a22006-09-19 10:28:00 -07001801probe_failed:
1802 qla4xxx_free_adapter(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301803
1804probe_failed_ioconfig:
Lalit Chandivade2232be02010-07-30 14:38:47 +05301805 pci_disable_pcie_error_reporting(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001806 scsi_host_put(ha->host);
1807
1808probe_disable_device:
1809 pci_disable_device(pdev);
1810
1811 return ret;
1812}
1813
1814/**
1815 * qla4xxx_remove_adapter - calback function to remove adapter.
1816 * @pci_dev: PCI device pointer
1817 **/
1818static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
1819{
1820 struct scsi_qla_host *ha;
1821
1822 ha = pci_get_drvdata(pdev);
1823
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301824 set_bit(AF_HBA_GOING_AWAY, &ha->flags);
David C Somayajulubee4fe82007-05-23 18:03:32 -07001825
David Somayajuluafaf5a22006-09-19 10:28:00 -07001826 /* remove devs from iscsi_sessions to scsi_devices */
1827 qla4xxx_free_ddb_list(ha);
1828
1829 scsi_remove_host(ha->host);
1830
1831 qla4xxx_free_adapter(ha);
1832
1833 scsi_host_put(ha->host);
1834
Lalit Chandivade2232be02010-07-30 14:38:47 +05301835 pci_disable_pcie_error_reporting(pdev);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301836 pci_disable_device(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001837 pci_set_drvdata(pdev, NULL);
1838}
1839
1840/**
1841 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
1842 * @ha: HA context
1843 *
1844 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
1845 * supported addressing method.
1846 */
Adrian Bunk47975472007-04-26 00:35:16 -07001847static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001848{
1849 int retval;
1850
1851 /* Update our PCI device dma_mask for full 64 bit mask */
Yang Hongyang6a355282009-04-06 19:01:13 -07001852 if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
1853 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001854 dev_dbg(&ha->pdev->dev,
1855 "Failed to set 64 bit PCI consistent mask; "
1856 "using 32 bit.\n");
1857 retval = pci_set_consistent_dma_mask(ha->pdev,
Yang Hongyang284901a2009-04-06 19:01:15 -07001858 DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001859 }
1860 } else
Yang Hongyang284901a2009-04-06 19:01:15 -07001861 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001862}
1863
1864static int qla4xxx_slave_alloc(struct scsi_device *sdev)
1865{
1866 struct iscsi_cls_session *sess = starget_to_session(sdev->sdev_target);
1867 struct ddb_entry *ddb = sess->dd_data;
1868
1869 sdev->hostdata = ddb;
1870 sdev->tagged_supported = 1;
Mike Christied510d962008-07-11 19:50:33 -05001871 scsi_activate_tcq(sdev, QL4_DEF_QDEPTH);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001872 return 0;
1873}
1874
1875static int qla4xxx_slave_configure(struct scsi_device *sdev)
1876{
1877 sdev->tagged_supported = 1;
1878 return 0;
1879}
1880
1881static void qla4xxx_slave_destroy(struct scsi_device *sdev)
1882{
1883 scsi_deactivate_tcq(sdev, 1);
1884}
1885
1886/**
1887 * qla4xxx_del_from_active_array - returns an active srb
1888 * @ha: Pointer to host adapter structure.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001889 * @index: index into the active_array
David Somayajuluafaf5a22006-09-19 10:28:00 -07001890 *
1891 * This routine removes and returns the srb at the specified index
1892 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301893struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
1894 uint32_t index)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001895{
1896 struct srb *srb = NULL;
Vikas Chaudhary53698872010-04-28 11:41:59 +05301897 struct scsi_cmnd *cmd = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001898
Vikas Chaudhary53698872010-04-28 11:41:59 +05301899 cmd = scsi_host_find_tag(ha->host, index);
1900 if (!cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001901 return srb;
1902
Vikas Chaudhary53698872010-04-28 11:41:59 +05301903 srb = (struct srb *)CMD_SP(cmd);
1904 if (!srb)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001905 return srb;
1906
1907 /* update counters */
1908 if (srb->flags & SRB_DMA_VALID) {
1909 ha->req_q_count += srb->iocb_cnt;
1910 ha->iocb_cnt -= srb->iocb_cnt;
1911 if (srb->cmd)
Vikas Chaudhary53698872010-04-28 11:41:59 +05301912 srb->cmd->host_scribble =
1913 (unsigned char *)(unsigned long) MAX_SRBS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001914 }
1915 return srb;
1916}
1917
1918/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07001919 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301920 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001921 * @cmd: Scsi Command to wait on.
1922 *
1923 * This routine waits for the command to be returned by the Firmware
1924 * for some max time.
1925 **/
1926static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
1927 struct scsi_cmnd *cmd)
1928{
1929 int done = 0;
1930 struct srb *rp;
1931 uint32_t max_wait_time = EH_WAIT_CMD_TOV;
Lalit Chandivade2232be02010-07-30 14:38:47 +05301932 int ret = SUCCESS;
1933
1934 /* Dont wait on command if PCI error is being handled
1935 * by PCI AER driver
1936 */
1937 if (unlikely(pci_channel_offline(ha->pdev)) ||
1938 (test_bit(AF_EEH_BUSY, &ha->flags))) {
1939 ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
1940 ha->host_no, __func__);
1941 return ret;
1942 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001943
1944 do {
1945 /* Checking to see if its returned to OS */
Vikas Chaudhary53698872010-04-28 11:41:59 +05301946 rp = (struct srb *) CMD_SP(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001947 if (rp == NULL) {
1948 done++;
1949 break;
1950 }
1951
1952 msleep(2000);
1953 } while (max_wait_time--);
1954
1955 return done;
1956}
1957
1958/**
1959 * qla4xxx_wait_for_hba_online - waits for HBA to come online
1960 * @ha: Pointer to host adapter structure
1961 **/
1962static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
1963{
1964 unsigned long wait_online;
1965
Vikas Chaudharyf581a3f2010-10-06 22:47:48 -07001966 wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001967 while (time_before(jiffies, wait_online)) {
1968
1969 if (adapter_up(ha))
1970 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001971
1972 msleep(2000);
1973 }
1974
1975 return QLA_ERROR;
1976}
1977
1978/**
Mike Christiece545032008-02-29 18:25:20 -06001979 * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001980 * @ha: pointer to HBA
David Somayajuluafaf5a22006-09-19 10:28:00 -07001981 * @t: target id
1982 * @l: lun id
1983 *
1984 * This function waits for all outstanding commands to a lun to complete. It
1985 * returns 0 if all pending commands are returned and 1 otherwise.
1986 **/
Mike Christiece545032008-02-29 18:25:20 -06001987static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
1988 struct scsi_target *stgt,
1989 struct scsi_device *sdev)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001990{
1991 int cnt;
1992 int status = 0;
1993 struct scsi_cmnd *cmd;
1994
1995 /*
Mike Christiece545032008-02-29 18:25:20 -06001996 * Waiting for all commands for the designated target or dev
1997 * in the active array
David Somayajuluafaf5a22006-09-19 10:28:00 -07001998 */
1999 for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
2000 cmd = scsi_host_find_tag(ha->host, cnt);
Mike Christiece545032008-02-29 18:25:20 -06002001 if (cmd && stgt == scsi_target(cmd->device) &&
2002 (!sdev || sdev == cmd->device)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002003 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
2004 status++;
2005 break;
2006 }
2007 }
2008 }
2009 return status;
2010}
2011
2012/**
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302013 * qla4xxx_eh_abort - callback for abort task.
2014 * @cmd: Pointer to Linux's SCSI command structure
2015 *
2016 * This routine is called by the Linux OS to abort the specified
2017 * command.
2018 **/
2019static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
2020{
2021 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2022 unsigned int id = cmd->device->id;
2023 unsigned int lun = cmd->device->lun;
2024 unsigned long serial = cmd->serial_number;
2025 struct srb *srb = NULL;
2026 int ret = SUCCESS;
2027 int wait = 0;
2028
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302029 ql4_printk(KERN_INFO, ha,
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302030 "scsi%ld:%d:%d: Abort command issued cmd=%p, pid=%ld\n",
2031 ha->host_no, id, lun, cmd, serial);
2032
2033 srb = (struct srb *) CMD_SP(cmd);
2034
2035 if (!srb)
2036 return SUCCESS;
2037
2038 kref_get(&srb->srb_ref);
2039
2040 if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
2041 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
2042 ha->host_no, id, lun));
2043 ret = FAILED;
2044 } else {
2045 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
2046 ha->host_no, id, lun));
2047 wait = 1;
2048 }
2049
2050 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
2051
2052 /* Wait for command to complete */
2053 if (wait) {
2054 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
2055 DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
2056 ha->host_no, id, lun));
2057 ret = FAILED;
2058 }
2059 }
2060
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302061 ql4_printk(KERN_INFO, ha,
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302062 "scsi%ld:%d:%d: Abort command - %s\n",
2063 ha->host_no, id, lun, (ret == SUCCESS) ? "succeded" : "failed");
2064
2065 return ret;
2066}
2067
2068/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002069 * qla4xxx_eh_device_reset - callback for target reset.
2070 * @cmd: Pointer to Linux's SCSI command structure
2071 *
2072 * This routine is called by the Linux OS to reset all luns on the
2073 * specified target.
2074 **/
2075static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
2076{
2077 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2078 struct ddb_entry *ddb_entry = cmd->device->hostdata;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002079 int ret = FAILED, stat;
2080
Karen Higgins612f73482009-07-15 15:03:01 -05002081 if (!ddb_entry)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002082 return ret;
2083
Mike Christiec01be6d2010-07-22 16:59:49 +05302084 ret = iscsi_block_scsi_eh(cmd);
2085 if (ret)
2086 return ret;
2087 ret = FAILED;
2088
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302089 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002090 "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
2091 cmd->device->channel, cmd->device->id, cmd->device->lun);
2092
2093 DEBUG2(printk(KERN_INFO
2094 "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
2095 "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
Jens Axboe242f9dc2008-09-14 05:55:09 -07002096 cmd, jiffies, cmd->request->timeout / HZ,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002097 ha->dpc_flags, cmd->result, cmd->allowed));
2098
2099 /* FIXME: wait for hba to go online */
2100 stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
2101 if (stat != QLA_SUCCESS) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302102 ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002103 goto eh_dev_reset_done;
2104 }
2105
Mike Christiece545032008-02-29 18:25:20 -06002106 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2107 cmd->device)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302108 ql4_printk(KERN_INFO, ha,
Mike Christiece545032008-02-29 18:25:20 -06002109 "DEVICE RESET FAILED - waiting for "
2110 "commands.\n");
2111 goto eh_dev_reset_done;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002112 }
2113
David C Somayajulu9d562912008-03-19 11:23:03 -07002114 /* Send marker. */
2115 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2116 MM_LUN_RESET) != QLA_SUCCESS)
2117 goto eh_dev_reset_done;
2118
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302119 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002120 "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
2121 ha->host_no, cmd->device->channel, cmd->device->id,
2122 cmd->device->lun);
2123
2124 ret = SUCCESS;
2125
2126eh_dev_reset_done:
2127
2128 return ret;
2129}
2130
2131/**
Mike Christiece545032008-02-29 18:25:20 -06002132 * qla4xxx_eh_target_reset - callback for target reset.
2133 * @cmd: Pointer to Linux's SCSI command structure
2134 *
2135 * This routine is called by the Linux OS to reset the target.
2136 **/
2137static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
2138{
2139 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2140 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christiec01be6d2010-07-22 16:59:49 +05302141 int stat, ret;
Mike Christiece545032008-02-29 18:25:20 -06002142
2143 if (!ddb_entry)
2144 return FAILED;
2145
Mike Christiec01be6d2010-07-22 16:59:49 +05302146 ret = iscsi_block_scsi_eh(cmd);
2147 if (ret)
2148 return ret;
2149
Mike Christiece545032008-02-29 18:25:20 -06002150 starget_printk(KERN_INFO, scsi_target(cmd->device),
2151 "WARM TARGET RESET ISSUED.\n");
2152
2153 DEBUG2(printk(KERN_INFO
2154 "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
2155 "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
Jens Axboe242f9dc2008-09-14 05:55:09 -07002156 ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
Mike Christiece545032008-02-29 18:25:20 -06002157 ha->dpc_flags, cmd->result, cmd->allowed));
2158
2159 stat = qla4xxx_reset_target(ha, ddb_entry);
2160 if (stat != QLA_SUCCESS) {
2161 starget_printk(KERN_INFO, scsi_target(cmd->device),
2162 "WARM TARGET RESET FAILED.\n");
2163 return FAILED;
2164 }
2165
Mike Christiece545032008-02-29 18:25:20 -06002166 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2167 NULL)) {
2168 starget_printk(KERN_INFO, scsi_target(cmd->device),
2169 "WARM TARGET DEVICE RESET FAILED - "
2170 "waiting for commands.\n");
2171 return FAILED;
2172 }
2173
David C Somayajulu9d562912008-03-19 11:23:03 -07002174 /* Send marker. */
2175 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2176 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
2177 starget_printk(KERN_INFO, scsi_target(cmd->device),
2178 "WARM TARGET DEVICE RESET FAILED - "
2179 "marker iocb failed.\n");
2180 return FAILED;
2181 }
2182
Mike Christiece545032008-02-29 18:25:20 -06002183 starget_printk(KERN_INFO, scsi_target(cmd->device),
2184 "WARM TARGET RESET SUCCEEDED.\n");
2185 return SUCCESS;
2186}
2187
2188/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002189 * qla4xxx_eh_host_reset - kernel callback
2190 * @cmd: Pointer to Linux's SCSI command structure
2191 *
2192 * This routine is invoked by the Linux kernel to perform fatal error
2193 * recovery on the specified adapter.
2194 **/
2195static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
2196{
2197 int return_status = FAILED;
2198 struct scsi_qla_host *ha;
2199
2200 ha = (struct scsi_qla_host *) cmd->device->host->hostdata;
2201
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302202 if (ql4xdontresethba) {
2203 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
2204 ha->host_no, __func__));
2205 return FAILED;
2206 }
2207
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302208 ql4_printk(KERN_INFO, ha,
Karen Higginsdca05c42009-07-15 15:03:00 -05002209 "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002210 cmd->device->channel, cmd->device->id, cmd->device->lun);
2211
2212 if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
2213 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
2214 "DEAD.\n", ha->host_no, cmd->device->channel,
2215 __func__));
2216
2217 return FAILED;
2218 }
2219
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302220 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
2221 if (is_qla8022(ha))
2222 set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2223 else
2224 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2225 }
Mike Christie50a29ae2008-03-04 13:26:53 -06002226
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302227 if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002228 return_status = SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002229
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302230 ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
David Somayajuluafaf5a22006-09-19 10:28:00 -07002231 return_status == FAILED ? "FAILED" : "SUCCEDED");
2232
2233 return return_status;
2234}
2235
Lalit Chandivade2232be02010-07-30 14:38:47 +05302236/* PCI AER driver recovers from all correctable errors w/o
2237 * driver intervention. For uncorrectable errors PCI AER
2238 * driver calls the following device driver's callbacks
2239 *
2240 * - Fatal Errors - link_reset
2241 * - Non-Fatal Errors - driver's pci_error_detected() which
2242 * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
2243 *
2244 * PCI AER driver calls
2245 * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
2246 * returns RECOVERED or NEED_RESET if fw_hung
2247 * NEED_RESET - driver's slot_reset()
2248 * DISCONNECT - device is dead & cannot recover
2249 * RECOVERED - driver's pci_resume()
2250 */
2251static pci_ers_result_t
2252qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
2253{
2254 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2255
2256 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
2257 ha->host_no, __func__, state);
2258
2259 if (!is_aer_supported(ha))
2260 return PCI_ERS_RESULT_NONE;
2261
2262 switch (state) {
2263 case pci_channel_io_normal:
2264 clear_bit(AF_EEH_BUSY, &ha->flags);
2265 return PCI_ERS_RESULT_CAN_RECOVER;
2266 case pci_channel_io_frozen:
2267 set_bit(AF_EEH_BUSY, &ha->flags);
2268 qla4xxx_mailbox_premature_completion(ha);
2269 qla4xxx_free_irqs(ha);
2270 pci_disable_device(pdev);
2271 return PCI_ERS_RESULT_NEED_RESET;
2272 case pci_channel_io_perm_failure:
2273 set_bit(AF_EEH_BUSY, &ha->flags);
2274 set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
2275 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
2276 return PCI_ERS_RESULT_DISCONNECT;
2277 }
2278 return PCI_ERS_RESULT_NEED_RESET;
2279}
2280
2281/**
2282 * qla4xxx_pci_mmio_enabled() gets called if
2283 * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
2284 * and read/write to the device still works.
2285 **/
2286static pci_ers_result_t
2287qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
2288{
2289 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2290
2291 if (!is_aer_supported(ha))
2292 return PCI_ERS_RESULT_NONE;
2293
2294 if (test_bit(AF_FW_RECOVERY, &ha->flags)) {
2295 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: firmware hang -- "
2296 "mmio_enabled\n", ha->host_no, __func__);
2297 return PCI_ERS_RESULT_NEED_RESET;
2298 } else
2299 return PCI_ERS_RESULT_RECOVERED;
2300}
2301
2302uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
2303{
2304 uint32_t rval = QLA_ERROR;
2305 int fn;
2306 struct pci_dev *other_pdev = NULL;
2307
2308 ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
2309
2310 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2311
2312 if (test_bit(AF_ONLINE, &ha->flags)) {
2313 clear_bit(AF_ONLINE, &ha->flags);
2314 qla4xxx_mark_all_devices_missing(ha);
2315 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2316 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
2317 }
2318
2319 fn = PCI_FUNC(ha->pdev->devfn);
2320 while (fn > 0) {
2321 fn--;
2322 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
2323 "func %x\n", ha->host_no, __func__, fn);
2324 /* Get the pci device given the domain, bus,
2325 * slot/function number */
2326 other_pdev =
2327 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
2328 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
2329 fn));
2330
2331 if (!other_pdev)
2332 continue;
2333
2334 if (atomic_read(&other_pdev->enable_cnt)) {
2335 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
2336 "func in enabled state%x\n", ha->host_no,
2337 __func__, fn);
2338 pci_dev_put(other_pdev);
2339 break;
2340 }
2341 pci_dev_put(other_pdev);
2342 }
2343
2344 /* The first function on the card, the reset owner will
2345 * start & initialize the firmware. The other functions
2346 * on the card will reset the firmware context
2347 */
2348 if (!fn) {
2349 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
2350 "0x%x is the owner\n", ha->host_no, __func__,
2351 ha->pdev->devfn);
2352
2353 qla4_8xxx_idc_lock(ha);
2354 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2355 QLA82XX_DEV_COLD);
2356
2357 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
2358 QLA82XX_IDC_VERSION);
2359
2360 qla4_8xxx_idc_unlock(ha);
2361 clear_bit(AF_FW_RECOVERY, &ha->flags);
2362 rval = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
2363 qla4_8xxx_idc_lock(ha);
2364
2365 if (rval != QLA_SUCCESS) {
2366 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
2367 "FAILED\n", ha->host_no, __func__);
2368 qla4_8xxx_clear_drv_active(ha);
2369 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2370 QLA82XX_DEV_FAILED);
2371 } else {
2372 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
2373 "READY\n", ha->host_no, __func__);
2374 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2375 QLA82XX_DEV_READY);
2376 /* Clear driver state register */
2377 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
2378 qla4_8xxx_set_drv_active(ha);
2379 ha->isp_ops->enable_intrs(ha);
2380 }
2381 qla4_8xxx_idc_unlock(ha);
2382 } else {
2383 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
2384 "the reset owner\n", ha->host_no, __func__,
2385 ha->pdev->devfn);
2386 if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
2387 QLA82XX_DEV_READY)) {
2388 clear_bit(AF_FW_RECOVERY, &ha->flags);
2389 rval = qla4xxx_initialize_adapter(ha,
2390 PRESERVE_DDB_LIST);
2391 if (rval == QLA_SUCCESS)
2392 ha->isp_ops->enable_intrs(ha);
2393 qla4_8xxx_idc_lock(ha);
2394 qla4_8xxx_set_drv_active(ha);
2395 qla4_8xxx_idc_unlock(ha);
2396 }
2397 }
2398 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2399 return rval;
2400}
2401
2402static pci_ers_result_t
2403qla4xxx_pci_slot_reset(struct pci_dev *pdev)
2404{
2405 pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
2406 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2407 int rc;
2408
2409 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
2410 ha->host_no, __func__);
2411
2412 if (!is_aer_supported(ha))
2413 return PCI_ERS_RESULT_NONE;
2414
2415 /* Restore the saved state of PCIe device -
2416 * BAR registers, PCI Config space, PCIX, MSI,
2417 * IOV states
2418 */
2419 pci_restore_state(pdev);
2420
2421 /* pci_restore_state() clears the saved_state flag of the device
2422 * save restored state which resets saved_state flag
2423 */
2424 pci_save_state(pdev);
2425
2426 /* Initialize device or resume if in suspended state */
2427 rc = pci_enable_device(pdev);
2428 if (rc) {
2429 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Cant re-enable "
2430 "device after reset\n", ha->host_no, __func__);
2431 goto exit_slot_reset;
2432 }
2433
2434 ret = qla4xxx_request_irqs(ha);
2435 if (ret) {
2436 ql4_printk(KERN_WARNING, ha, "Failed to reserve interrupt %d"
2437 " already in use.\n", pdev->irq);
2438 goto exit_slot_reset;
2439 }
2440
2441 if (is_qla8022(ha)) {
2442 if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
2443 ret = PCI_ERS_RESULT_RECOVERED;
2444 goto exit_slot_reset;
2445 } else
2446 goto exit_slot_reset;
2447 }
2448
2449exit_slot_reset:
2450 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
2451 "device after reset\n", ha->host_no, __func__, ret);
2452 return ret;
2453}
2454
2455static void
2456qla4xxx_pci_resume(struct pci_dev *pdev)
2457{
2458 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2459 int ret;
2460
2461 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
2462 ha->host_no, __func__);
2463
2464 ret = qla4xxx_wait_for_hba_online(ha);
2465 if (ret != QLA_SUCCESS) {
2466 ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
2467 "resume I/O from slot/link_reset\n", ha->host_no,
2468 __func__);
2469 }
2470
2471 pci_cleanup_aer_uncorrect_error_status(pdev);
2472 clear_bit(AF_EEH_BUSY, &ha->flags);
2473}
2474
2475static struct pci_error_handlers qla4xxx_err_handler = {
2476 .error_detected = qla4xxx_pci_error_detected,
2477 .mmio_enabled = qla4xxx_pci_mmio_enabled,
2478 .slot_reset = qla4xxx_pci_slot_reset,
2479 .resume = qla4xxx_pci_resume,
2480};
2481
David Somayajuluafaf5a22006-09-19 10:28:00 -07002482static struct pci_device_id qla4xxx_pci_tbl[] = {
2483 {
2484 .vendor = PCI_VENDOR_ID_QLOGIC,
2485 .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
2486 .subvendor = PCI_ANY_ID,
2487 .subdevice = PCI_ANY_ID,
2488 },
2489 {
2490 .vendor = PCI_VENDOR_ID_QLOGIC,
2491 .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
2492 .subvendor = PCI_ANY_ID,
2493 .subdevice = PCI_ANY_ID,
2494 },
David C Somayajulud9150582006-11-15 17:38:40 -08002495 {
2496 .vendor = PCI_VENDOR_ID_QLOGIC,
2497 .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
2498 .subvendor = PCI_ANY_ID,
2499 .subdevice = PCI_ANY_ID,
2500 },
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302501 {
2502 .vendor = PCI_VENDOR_ID_QLOGIC,
2503 .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
2504 .subvendor = PCI_ANY_ID,
2505 .subdevice = PCI_ANY_ID,
2506 },
David Somayajuluafaf5a22006-09-19 10:28:00 -07002507 {0, 0},
2508};
2509MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
2510
Adrian Bunk47975472007-04-26 00:35:16 -07002511static struct pci_driver qla4xxx_pci_driver = {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002512 .name = DRIVER_NAME,
2513 .id_table = qla4xxx_pci_tbl,
2514 .probe = qla4xxx_probe_adapter,
2515 .remove = qla4xxx_remove_adapter,
Lalit Chandivade2232be02010-07-30 14:38:47 +05302516 .err_handler = &qla4xxx_err_handler,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002517};
2518
2519static int __init qla4xxx_module_init(void)
2520{
2521 int ret;
2522
2523 /* Allocate cache for SRBs. */
2524 srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09002525 SLAB_HWCACHE_ALIGN, NULL);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002526 if (srb_cachep == NULL) {
2527 printk(KERN_ERR
2528 "%s: Unable to allocate SRB cache..."
2529 "Failing load!\n", DRIVER_NAME);
2530 ret = -ENOMEM;
2531 goto no_srp_cache;
2532 }
2533
2534 /* Derive version string. */
2535 strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
Andrew Vasquez11010fe2006-10-06 09:54:59 -07002536 if (ql4xextended_error_logging)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002537 strcat(qla4xxx_version_str, "-debug");
2538
2539 qla4xxx_scsi_transport =
2540 iscsi_register_transport(&qla4xxx_iscsi_transport);
2541 if (!qla4xxx_scsi_transport){
2542 ret = -ENODEV;
2543 goto release_srb_cache;
2544 }
2545
David Somayajuluafaf5a22006-09-19 10:28:00 -07002546 ret = pci_register_driver(&qla4xxx_pci_driver);
2547 if (ret)
2548 goto unregister_transport;
2549
2550 printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
2551 return 0;
Doug Maxey5ae16db2006-10-05 23:50:07 -05002552
David Somayajuluafaf5a22006-09-19 10:28:00 -07002553unregister_transport:
2554 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
2555release_srb_cache:
2556 kmem_cache_destroy(srb_cachep);
2557no_srp_cache:
2558 return ret;
2559}
2560
2561static void __exit qla4xxx_module_exit(void)
2562{
David Somayajuluafaf5a22006-09-19 10:28:00 -07002563 pci_unregister_driver(&qla4xxx_pci_driver);
2564 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
2565 kmem_cache_destroy(srb_cachep);
2566}
2567
2568module_init(qla4xxx_module_init);
2569module_exit(qla4xxx_module_exit);
2570
2571MODULE_AUTHOR("QLogic Corporation");
2572MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
2573MODULE_LICENSE("GPL");
2574MODULE_VERSION(QLA4XXX_DRIVER_VERSION);