blob: df46e5d8f74e40e35fb77c04f355b536f58fb7e1 [file] [log] [blame]
David Somayajuluafaf5a22006-09-19 10:28:00 -07001/*
2 * QLogic iSCSI HBA Driver
Vikas Chaudhary7d01d062010-12-02 22:12:51 -08003 * Copyright (c) 2003-2010 QLogic Corporation
David Somayajuluafaf5a22006-09-19 10:28:00 -07004 *
5 * See LICENSE.qla4xxx for copyright and licensing details.
6 */
7#include <linux/moduleparam.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
David Somayajuluafaf5a22006-09-19 10:28:00 -07009
10#include <scsi/scsi_tcq.h>
11#include <scsi/scsicam.h>
12
13#include "ql4_def.h"
David C Somayajulubee4fe82007-05-23 18:03:32 -070014#include "ql4_version.h"
15#include "ql4_glbl.h"
16#include "ql4_dbg.h"
17#include "ql4_inline.h"
David Somayajuluafaf5a22006-09-19 10:28:00 -070018
19/*
20 * Driver version
21 */
Adrian Bunk47975472007-04-26 00:35:16 -070022static char qla4xxx_version_str[40];
David Somayajuluafaf5a22006-09-19 10:28:00 -070023
24/*
25 * SRB allocation cache
26 */
Christoph Lametere18b8902006-12-06 20:33:20 -080027static struct kmem_cache *srb_cachep;
David Somayajuluafaf5a22006-09-19 10:28:00 -070028
29/*
30 * Module parameter information and variables
31 */
David Somayajuluafaf5a22006-09-19 10:28:00 -070032int ql4xdontresethba = 0;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053033module_param(ql4xdontresethba, int, S_IRUGO | S_IWUSR);
David Somayajuluafaf5a22006-09-19 10:28:00 -070034MODULE_PARM_DESC(ql4xdontresethba,
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053035 "Don't reset the HBA for driver recovery \n"
36 " 0 - It will reset HBA (Default)\n"
37 " 1 - It will NOT reset HBA");
David Somayajuluafaf5a22006-09-19 10:28:00 -070038
Andrew Vasquez11010fe2006-10-06 09:54:59 -070039int ql4xextended_error_logging = 0; /* 0 = off, 1 = log errors */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053040module_param(ql4xextended_error_logging, int, S_IRUGO | S_IWUSR);
Andrew Vasquez11010fe2006-10-06 09:54:59 -070041MODULE_PARM_DESC(ql4xextended_error_logging,
David Somayajuluafaf5a22006-09-19 10:28:00 -070042 "Option to enable extended error logging, "
43 "Default is 0 - no logging, 1 - debug logging");
44
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053045int ql4xenablemsix = 1;
46module_param(ql4xenablemsix, int, S_IRUGO|S_IWUSR);
47MODULE_PARM_DESC(ql4xenablemsix,
48 "Set to enable MSI or MSI-X interrupt mechanism.\n"
49 " 0 = enable INTx interrupt mechanism.\n"
50 " 1 = enable MSI-X interrupt mechanism (Default).\n"
51 " 2 = enable MSI interrupt mechanism.");
David C Somayajulu477ffb92007-01-22 12:26:11 -080052
Mike Christied510d962008-07-11 19:50:33 -050053#define QL4_DEF_QDEPTH 32
Vikas Chaudhary8bb40332011-03-21 03:34:31 -070054static int ql4xmaxqdepth = QL4_DEF_QDEPTH;
55module_param(ql4xmaxqdepth, int, S_IRUGO | S_IWUSR);
56MODULE_PARM_DESC(ql4xmaxqdepth,
57 "Maximum queue depth to report for target devices.\n"
58 " Default: 32.");
Mike Christied510d962008-07-11 19:50:33 -050059
David Somayajuluafaf5a22006-09-19 10:28:00 -070060/*
61 * SCSI host template entry points
62 */
Adrian Bunk47975472007-04-26 00:35:16 -070063static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -070064
65/*
66 * iSCSI template entry points
67 */
Mike Christie2174a042007-05-30 12:57:10 -050068static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
69 enum iscsi_tgt_dscvr type, uint32_t enable,
70 struct sockaddr *dst_addr);
David Somayajuluafaf5a22006-09-19 10:28:00 -070071static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
72 enum iscsi_param param, char *buf);
73static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
74 enum iscsi_param param, char *buf);
Mike Christieaa1e93a2007-05-30 12:57:09 -050075static int qla4xxx_host_get_param(struct Scsi_Host *shost,
76 enum iscsi_host_param param, char *buf);
David Somayajuluafaf5a22006-09-19 10:28:00 -070077static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session);
Mike Christie5c656af2009-07-15 15:02:59 -050078static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
David Somayajuluafaf5a22006-09-19 10:28:00 -070079
80/*
81 * SCSI host template entry points
82 */
Jeff Garzikf2812332010-11-16 02:10:29 -050083static int qla4xxx_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmd);
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 **/
Jeff Garzikf2812332010-11-16 02:10:29 -0500467static int qla4xxx_queuecommand_lck(struct scsi_cmnd *cmd,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700468 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
Jeff Garzikf2812332010-11-16 02:10:29 -0500541static DEF_SCSI_QCMD(qla4xxx_queuecommand)
542
David Somayajuluafaf5a22006-09-19 10:28:00 -0700543/**
544 * qla4xxx_mem_free - frees memory allocated to adapter
545 * @ha: Pointer to host adapter structure.
546 *
547 * Frees memory previously allocated by qla4xxx_mem_alloc
548 **/
549static void qla4xxx_mem_free(struct scsi_qla_host *ha)
550{
551 if (ha->queues)
552 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
553 ha->queues_dma);
554
555 ha->queues_len = 0;
556 ha->queues = NULL;
557 ha->queues_dma = 0;
558 ha->request_ring = NULL;
559 ha->request_dma = 0;
560 ha->response_ring = NULL;
561 ha->response_dma = 0;
562 ha->shadow_regs = NULL;
563 ha->shadow_regs_dma = 0;
564
565 /* Free srb pool. */
566 if (ha->srb_mempool)
567 mempool_destroy(ha->srb_mempool);
568
569 ha->srb_mempool = NULL;
570
571 /* release io space registers */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530572 if (is_qla8022(ha)) {
573 if (ha->nx_pcibase)
574 iounmap(
575 (struct device_reg_82xx __iomem *)ha->nx_pcibase);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530576 } else if (ha->reg)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700577 iounmap(ha->reg);
578 pci_release_regions(ha->pdev);
579}
580
581/**
582 * qla4xxx_mem_alloc - allocates memory for use by adapter.
583 * @ha: Pointer to host adapter structure
584 *
585 * Allocates DMA memory for request and response queues. Also allocates memory
586 * for srbs.
587 **/
588static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
589{
590 unsigned long align;
591
592 /* Allocate contiguous block of DMA memory for queues. */
593 ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
594 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
595 sizeof(struct shadow_regs) +
596 MEM_ALIGN_VALUE +
597 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
598 ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
599 &ha->queues_dma, GFP_KERNEL);
600 if (ha->queues == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530601 ql4_printk(KERN_WARNING, ha,
602 "Memory Allocation failed - queues.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700603
604 goto mem_alloc_error_exit;
605 }
606 memset(ha->queues, 0, ha->queues_len);
607
608 /*
609 * As per RISC alignment requirements -- the bus-address must be a
610 * multiple of the request-ring size (in bytes).
611 */
612 align = 0;
613 if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
614 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
615 (MEM_ALIGN_VALUE - 1));
616
617 /* Update request and response queue pointers. */
618 ha->request_dma = ha->queues_dma + align;
619 ha->request_ring = (struct queue_entry *) (ha->queues + align);
620 ha->response_dma = ha->queues_dma + align +
621 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
622 ha->response_ring = (struct queue_entry *) (ha->queues + align +
623 (REQUEST_QUEUE_DEPTH *
624 QUEUE_SIZE));
625 ha->shadow_regs_dma = ha->queues_dma + align +
626 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
627 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
628 ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
629 (REQUEST_QUEUE_DEPTH *
630 QUEUE_SIZE) +
631 (RESPONSE_QUEUE_DEPTH *
632 QUEUE_SIZE));
633
634 /* Allocate memory for srb pool. */
635 ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
636 mempool_free_slab, srb_cachep);
637 if (ha->srb_mempool == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530638 ql4_printk(KERN_WARNING, ha,
639 "Memory Allocation failed - SRB Pool.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700640
641 goto mem_alloc_error_exit;
642 }
643
644 return QLA_SUCCESS;
645
646mem_alloc_error_exit:
647 qla4xxx_mem_free(ha);
648 return QLA_ERROR;
649}
650
651/**
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530652 * qla4_8xxx_check_fw_alive - Check firmware health
653 * @ha: Pointer to host adapter structure.
654 *
655 * Context: Interrupt
656 **/
657static void qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
658{
659 uint32_t fw_heartbeat_counter, halt_status;
660
661 fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
Lalit Chandivade2232be02010-07-30 14:38:47 +0530662 /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
663 if (fw_heartbeat_counter == 0xffffffff) {
664 DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
665 "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
666 ha->host_no, __func__));
667 return;
668 }
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530669
670 if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
671 ha->seconds_since_last_heartbeat++;
672 /* FW not alive after 2 seconds */
673 if (ha->seconds_since_last_heartbeat == 2) {
674 ha->seconds_since_last_heartbeat = 0;
675 halt_status = qla4_8xxx_rd_32(ha,
676 QLA82XX_PEG_HALT_STATUS1);
Nilesh Javali21033632010-07-30 14:28:07 +0530677
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530678 /* Since we cannot change dev_state in interrupt
679 * context, set appropriate DPC flag then wakeup
680 * DPC */
681 if (halt_status & HALT_STATUS_UNRECOVERABLE)
682 set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
683 else {
684 printk("scsi%ld: %s: detect abort needed!\n",
685 ha->host_no, __func__);
686 set_bit(DPC_RESET_HA, &ha->dpc_flags);
687 }
688 qla4xxx_wake_dpc(ha);
Nilesh Javali21033632010-07-30 14:28:07 +0530689 qla4xxx_mailbox_premature_completion(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530690 }
Lalit Chandivade99457d72010-10-06 22:49:32 -0700691 } else
692 ha->seconds_since_last_heartbeat = 0;
693
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530694 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 */
Lalit Chandivaded56a1f72010-12-02 22:12:45 -0800710 if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
711 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
712 test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags))) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530713 if (dev_state == QLA82XX_DEV_NEED_RESET &&
714 !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
Vikas Chaudhary3930b8c2010-12-02 22:12:47 -0800715 if (!ql4xdontresethba) {
716 ql4_printk(KERN_INFO, ha, "%s: HW State: "
717 "NEED RESET!\n", __func__);
718 set_bit(DPC_RESET_HA, &ha->dpc_flags);
719 qla4xxx_wake_dpc(ha);
720 qla4xxx_mailbox_premature_completion(ha);
721 }
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530722 } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
723 !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
Vikas Chaudhary3930b8c2010-12-02 22:12:47 -0800724 ql4_printk(KERN_INFO, ha, "%s: HW State: NEED QUIES!\n",
725 __func__);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530726 set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
727 qla4xxx_wake_dpc(ha);
728 } else {
729 /* Check firmware health */
730 qla4_8xxx_check_fw_alive(ha);
731 }
732 }
733}
734
735/**
David Somayajuluafaf5a22006-09-19 10:28:00 -0700736 * qla4xxx_timer - checks every second for work to do.
737 * @ha: Pointer to host adapter structure.
738 **/
739static void qla4xxx_timer(struct scsi_qla_host *ha)
740{
741 struct ddb_entry *ddb_entry, *dtemp;
742 int start_dpc = 0;
Lalit Chandivade2232be02010-07-30 14:38:47 +0530743 uint16_t w;
744
745 /* If we are in the middle of AER/EEH processing
746 * skip any processing and reschedule the timer
747 */
748 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
749 mod_timer(&ha->timer, jiffies + HZ);
750 return;
751 }
752
753 /* Hardware read to trigger an EEH error during mailbox waits. */
754 if (!pci_channel_offline(ha->pdev))
755 pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700756
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530757 if (is_qla8022(ha)) {
758 qla4_8xxx_watchdog(ha);
759 }
760
David Somayajuluafaf5a22006-09-19 10:28:00 -0700761 /* Search for relogin's to time-out and port down retry. */
762 list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
763 /* Count down time between sending relogins */
764 if (adapter_up(ha) &&
765 !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
766 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
767 if (atomic_read(&ddb_entry->retry_relogin_timer) !=
768 INVALID_ENTRY) {
769 if (atomic_read(&ddb_entry->retry_relogin_timer)
770 == 0) {
771 atomic_set(&ddb_entry->
772 retry_relogin_timer,
773 INVALID_ENTRY);
774 set_bit(DPC_RELOGIN_DEVICE,
775 &ha->dpc_flags);
776 set_bit(DF_RELOGIN, &ddb_entry->flags);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530777 DEBUG2(printk("scsi%ld: %s: ddb [%d]"
David Somayajuluafaf5a22006-09-19 10:28:00 -0700778 " login device\n",
779 ha->host_no, __func__,
780 ddb_entry->fw_ddb_index));
781 } else
782 atomic_dec(&ddb_entry->
783 retry_relogin_timer);
784 }
785 }
786
787 /* Wait for relogin to timeout */
788 if (atomic_read(&ddb_entry->relogin_timer) &&
789 (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
790 /*
791 * If the relogin times out and the device is
792 * still NOT ONLINE then try and relogin again.
793 */
794 if (atomic_read(&ddb_entry->state) !=
795 DDB_STATE_ONLINE &&
796 ddb_entry->fw_ddb_device_state ==
797 DDB_DS_SESSION_FAILED) {
798 /* Reset retry relogin timer */
799 atomic_inc(&ddb_entry->relogin_retry_count);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530800 DEBUG2(printk("scsi%ld: ddb [%d] relogin"
David Somayajuluafaf5a22006-09-19 10:28:00 -0700801 " timed out-retrying"
802 " relogin (%d)\n",
803 ha->host_no,
804 ddb_entry->fw_ddb_index,
805 atomic_read(&ddb_entry->
806 relogin_retry_count))
807 );
808 start_dpc++;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530809 DEBUG(printk("scsi%ld:%d:%d: ddb [%d] "
Justin P. Mattockcd09b2c2011-01-28 11:49:08 -0800810 "initiate relogin after"
David Somayajuluafaf5a22006-09-19 10:28:00 -0700811 " %d seconds\n",
812 ha->host_no, ddb_entry->bus,
813 ddb_entry->target,
814 ddb_entry->fw_ddb_index,
815 ddb_entry->default_time2wait + 4)
816 );
817
818 atomic_set(&ddb_entry->retry_relogin_timer,
819 ddb_entry->default_time2wait + 4);
820 }
821 }
822 }
823
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530824 if (!is_qla8022(ha)) {
825 /* Check for heartbeat interval. */
826 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
827 ha->heartbeat_interval != 0) {
828 ha->seconds_since_last_heartbeat++;
829 if (ha->seconds_since_last_heartbeat >
830 ha->heartbeat_interval + 2)
831 set_bit(DPC_RESET_HA, &ha->dpc_flags);
832 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700833 }
834
David Somayajuluafaf5a22006-09-19 10:28:00 -0700835 /* Wakeup the dpc routine for this adapter, if needed. */
836 if ((start_dpc ||
837 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
838 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
839 test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530840 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -0700841 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
842 test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +0530843 test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530844 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
845 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -0700846 test_bit(DPC_AEN, &ha->dpc_flags)) &&
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530847 !test_bit(AF_DPC_SCHEDULED, &ha->flags) &&
David Somayajuluafaf5a22006-09-19 10:28:00 -0700848 ha->dpc_thread) {
849 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
850 " - dpc flags = 0x%lx\n",
851 ha->host_no, __func__, ha->dpc_flags));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530852 qla4xxx_wake_dpc(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700853 }
854
855 /* Reschedule timer thread to call us back in one second */
856 mod_timer(&ha->timer, jiffies + HZ);
857
858 DEBUG2(ha->seconds_since_last_intr++);
859}
860
861/**
862 * qla4xxx_cmd_wait - waits for all outstanding commands to complete
863 * @ha: Pointer to host adapter structure.
864 *
865 * This routine stalls the driver until all outstanding commands are returned.
866 * Caller must release the Hardware Lock prior to calling this routine.
867 **/
868static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
869{
870 uint32_t index = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700871 unsigned long flags;
872 struct scsi_cmnd *cmd;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700873
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530874 unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
875
876 DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
877 "complete\n", WAIT_CMD_TOV));
878
879 while (!time_after_eq(jiffies, wtime)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700880 spin_lock_irqsave(&ha->hardware_lock, flags);
881 /* Find a command that hasn't completed. */
882 for (index = 0; index < ha->host->can_queue; index++) {
883 cmd = scsi_host_find_tag(ha->host, index);
Mike Christiea1e00632010-10-26 05:45:30 -0700884 /*
885 * We cannot just check if the index is valid,
886 * becase if we are run from the scsi eh, then
887 * the scsi/block layer is going to prevent
888 * the tag from being released.
889 */
890 if (cmd != NULL && CMD_SP(cmd))
David Somayajuluafaf5a22006-09-19 10:28:00 -0700891 break;
892 }
893 spin_unlock_irqrestore(&ha->hardware_lock, flags);
894
895 /* If No Commands are pending, wait is complete */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530896 if (index == ha->host->can_queue)
897 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700898
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530899 msleep(1000);
900 }
901 /* If we timed out on waiting for commands to come back
902 * return ERROR. */
903 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700904}
905
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530906int qla4xxx_hw_reset(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700907{
David Somayajuluafaf5a22006-09-19 10:28:00 -0700908 uint32_t ctrl_status;
David C Somayajulu477ffb92007-01-22 12:26:11 -0800909 unsigned long flags = 0;
910
911 DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700912
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530913 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
914 return QLA_ERROR;
915
David Somayajuluafaf5a22006-09-19 10:28:00 -0700916 spin_lock_irqsave(&ha->hardware_lock, flags);
917
918 /*
919 * If the SCSI Reset Interrupt bit is set, clear it.
920 * Otherwise, the Soft Reset won't work.
921 */
922 ctrl_status = readw(&ha->reg->ctrl_status);
923 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
924 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
925
926 /* Issue Soft Reset */
927 writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
928 readl(&ha->reg->ctrl_status);
929
930 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530931 return QLA_SUCCESS;
David C Somayajulu477ffb92007-01-22 12:26:11 -0800932}
933
934/**
935 * qla4xxx_soft_reset - performs soft reset.
936 * @ha: Pointer to host adapter structure.
937 **/
938int qla4xxx_soft_reset(struct scsi_qla_host *ha)
939{
940 uint32_t max_wait_time;
941 unsigned long flags = 0;
Vikas Chaudharyf931c532010-10-06 22:48:07 -0700942 int status;
David C Somayajulu477ffb92007-01-22 12:26:11 -0800943 uint32_t ctrl_status;
944
Vikas Chaudharyf931c532010-10-06 22:48:07 -0700945 status = qla4xxx_hw_reset(ha);
946 if (status != QLA_SUCCESS)
947 return status;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700948
Vikas Chaudharyf931c532010-10-06 22:48:07 -0700949 status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700950 /* Wait until the Network Reset Intr bit is cleared */
951 max_wait_time = RESET_INTR_TOV;
952 do {
953 spin_lock_irqsave(&ha->hardware_lock, flags);
954 ctrl_status = readw(&ha->reg->ctrl_status);
955 spin_unlock_irqrestore(&ha->hardware_lock, flags);
956
957 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
958 break;
959
960 msleep(1000);
961 } while ((--max_wait_time));
962
963 if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
964 DEBUG2(printk(KERN_WARNING
965 "scsi%ld: Network Reset Intr not cleared by "
966 "Network function, clearing it now!\n",
967 ha->host_no));
968 spin_lock_irqsave(&ha->hardware_lock, flags);
969 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
970 readl(&ha->reg->ctrl_status);
971 spin_unlock_irqrestore(&ha->hardware_lock, flags);
972 }
973
974 /* Wait until the firmware tells us the Soft Reset is done */
975 max_wait_time = SOFT_RESET_TOV;
976 do {
977 spin_lock_irqsave(&ha->hardware_lock, flags);
978 ctrl_status = readw(&ha->reg->ctrl_status);
979 spin_unlock_irqrestore(&ha->hardware_lock, flags);
980
981 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
982 status = QLA_SUCCESS;
983 break;
984 }
985
986 msleep(1000);
987 } while ((--max_wait_time));
988
989 /*
990 * Also, make sure that the SCSI Reset Interrupt bit has been cleared
991 * after the soft reset has taken place.
992 */
993 spin_lock_irqsave(&ha->hardware_lock, flags);
994 ctrl_status = readw(&ha->reg->ctrl_status);
995 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
996 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
997 readl(&ha->reg->ctrl_status);
998 }
999 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1000
1001 /* If soft reset fails then most probably the bios on other
1002 * function is also enabled.
1003 * Since the initialization is sequential the other fn
1004 * wont be able to acknowledge the soft reset.
1005 * Issue a force soft reset to workaround this scenario.
1006 */
1007 if (max_wait_time == 0) {
1008 /* Issue Force Soft Reset */
1009 spin_lock_irqsave(&ha->hardware_lock, flags);
1010 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
1011 readl(&ha->reg->ctrl_status);
1012 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1013 /* Wait until the firmware tells us the Soft Reset is done */
1014 max_wait_time = SOFT_RESET_TOV;
1015 do {
1016 spin_lock_irqsave(&ha->hardware_lock, flags);
1017 ctrl_status = readw(&ha->reg->ctrl_status);
1018 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1019
1020 if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
1021 status = QLA_SUCCESS;
1022 break;
1023 }
1024
1025 msleep(1000);
1026 } while ((--max_wait_time));
1027 }
1028
1029 return status;
1030}
1031
1032/**
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301033 * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001034 * @ha: Pointer to host adapter structure.
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301035 * @res: returned scsi status
David Somayajuluafaf5a22006-09-19 10:28:00 -07001036 *
1037 * This routine is called just prior to a HARD RESET to return all
1038 * outstanding commands back to the Operating System.
1039 * Caller should make sure that the following locks are released
1040 * before this calling routine: Hardware lock, and io_request_lock.
1041 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301042static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001043{
1044 struct srb *srb;
1045 int i;
1046 unsigned long flags;
1047
1048 spin_lock_irqsave(&ha->hardware_lock, flags);
1049 for (i = 0; i < ha->host->can_queue; i++) {
1050 srb = qla4xxx_del_from_active_array(ha, i);
1051 if (srb != NULL) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301052 srb->cmd->result = res;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301053 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001054 }
1055 }
1056 spin_unlock_irqrestore(&ha->hardware_lock, flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001057}
1058
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301059void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
1060{
1061 clear_bit(AF_ONLINE, &ha->flags);
1062
1063 /* Disable the board */
1064 ql4_printk(KERN_INFO, ha, "Disabling the board\n");
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301065
1066 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
1067 qla4xxx_mark_all_devices_missing(ha);
1068 clear_bit(AF_INIT_DONE, &ha->flags);
1069}
1070
David Somayajuluafaf5a22006-09-19 10:28:00 -07001071/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07001072 * qla4xxx_recover_adapter - recovers adapter after a fatal error
1073 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001074 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301075static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001076{
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301077 int status = QLA_ERROR;
1078 uint8_t reset_chip = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001079
1080 /* Stall incoming I/O until we are done */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301081 scsi_block_requests(ha->host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001082 clear_bit(AF_ONLINE, &ha->flags);
Mike Christie50a29ae2008-03-04 13:26:53 -06001083
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301084 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001085
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301086 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001087
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301088 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
1089 reset_chip = 1;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001090
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301091 /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
1092 * do not reset adapter, jump to initialize_adapter */
1093 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1094 status = QLA_SUCCESS;
1095 goto recover_ha_init_adapter;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001096 }
1097
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301098 /* For the ISP-82xx adapter, issue a stop_firmware if invoked
1099 * from eh_host_reset or ioctl module */
1100 if (is_qla8022(ha) && !reset_chip &&
1101 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
1102
1103 DEBUG2(ql4_printk(KERN_INFO, ha,
1104 "scsi%ld: %s - Performing stop_firmware...\n",
1105 ha->host_no, __func__));
1106 status = ha->isp_ops->reset_firmware(ha);
1107 if (status == QLA_SUCCESS) {
Nilesh Javali2bd1e2be2010-10-06 22:49:20 -07001108 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
1109 qla4xxx_cmd_wait(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301110 ha->isp_ops->disable_intrs(ha);
1111 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1112 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1113 } else {
1114 /* If the stop_firmware fails then
1115 * reset the entire chip */
1116 reset_chip = 1;
1117 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1118 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1119 }
1120 }
1121
1122 /* Issue full chip reset if recovering from a catastrophic error,
1123 * or if stop_firmware fails for ISP-82xx.
1124 * This is the default case for ISP-4xxx */
1125 if (!is_qla8022(ha) || reset_chip) {
Nilesh Javali2bd1e2be2010-10-06 22:49:20 -07001126 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
1127 qla4xxx_cmd_wait(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301128 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1129 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1130 DEBUG2(ql4_printk(KERN_INFO, ha,
1131 "scsi%ld: %s - Performing chip reset..\n",
1132 ha->host_no, __func__));
1133 status = ha->isp_ops->reset_chip(ha);
1134 }
1135
1136 /* Flush any pending ddb changed AENs */
1137 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1138
1139recover_ha_init_adapter:
1140 /* Upon successful firmware/chip reset, re-initialize the adapter */
1141 if (status == QLA_SUCCESS) {
1142 /* For ISP-4xxx, force function 1 to always initialize
1143 * before function 3 to prevent both funcions from
1144 * stepping on top of the other */
1145 if (!is_qla8022(ha) && (ha->mac_index == 3))
1146 ssleep(6);
1147
1148 /* NOTE: AF_ONLINE flag set upon successful completion of
1149 * qla4xxx_initialize_adapter */
1150 status = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
1151 }
1152
1153 /* Retry failed adapter initialization, if necessary
1154 * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
1155 * case to prevent ping-pong resets between functions */
1156 if (!test_bit(AF_ONLINE, &ha->flags) &&
1157 !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001158 /* Adapter initialization failed, see if we can retry
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301159 * resetting the ha.
1160 * Since we don't want to block the DPC for too long
1161 * with multiple resets in the same thread,
1162 * utilize DPC to retry */
David Somayajuluafaf5a22006-09-19 10:28:00 -07001163 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
1164 ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
1165 DEBUG2(printk("scsi%ld: recover adapter - retrying "
1166 "(%d) more times\n", ha->host_no,
1167 ha->retry_reset_ha_cnt));
1168 set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1169 status = QLA_ERROR;
1170 } else {
1171 if (ha->retry_reset_ha_cnt > 0) {
1172 /* Schedule another Reset HA--DPC will retry */
1173 ha->retry_reset_ha_cnt--;
1174 DEBUG2(printk("scsi%ld: recover adapter - "
1175 "retry remaining %d\n",
1176 ha->host_no,
1177 ha->retry_reset_ha_cnt));
1178 status = QLA_ERROR;
1179 }
1180
1181 if (ha->retry_reset_ha_cnt == 0) {
1182 /* Recover adapter retries have been exhausted.
1183 * Adapter DEAD */
1184 DEBUG2(printk("scsi%ld: recover adapter "
1185 "failed - board disabled\n",
1186 ha->host_no));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301187 qla4xxx_dead_adapter_cleanup(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001188 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1189 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301190 clear_bit(DPC_RESET_HA_FW_CONTEXT,
David Somayajuluafaf5a22006-09-19 10:28:00 -07001191 &ha->dpc_flags);
1192 status = QLA_ERROR;
1193 }
1194 }
1195 } else {
1196 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301197 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001198 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1199 }
1200
1201 ha->adapter_error_count++;
1202
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301203 if (test_bit(AF_ONLINE, &ha->flags))
1204 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001205
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301206 scsi_unblock_requests(ha->host);
1207
1208 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
1209 DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
1210 status == QLA_ERROR ? "FAILED" : "SUCCEDED"));
1211
David Somayajuluafaf5a22006-09-19 10:28:00 -07001212 return status;
1213}
1214
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301215void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
1216{
1217 if (ha->dpc_thread &&
1218 !test_bit(AF_DPC_SCHEDULED, &ha->flags)) {
1219 set_bit(AF_DPC_SCHEDULED, &ha->flags);
1220 queue_work(ha->dpc_thread, &ha->dpc_work);
1221 }
1222}
1223
David Somayajuluafaf5a22006-09-19 10:28:00 -07001224/**
1225 * qla4xxx_do_dpc - dpc routine
1226 * @data: in our case pointer to adapter structure
1227 *
1228 * This routine is a task that is schedule by the interrupt handler
1229 * to perform the background processing for interrupts. We put it
1230 * on a task queue that is consumed whenever the scheduler runs; that's
1231 * so you can do anything (i.e. put the process to sleep etc). In fact,
1232 * the mid-level tries to sleep when it reaches the driver threshold
1233 * "host->can_queue". This can cause a panic if we were in our interrupt code.
1234 **/
David Howellsc4028952006-11-22 14:57:56 +00001235static void qla4xxx_do_dpc(struct work_struct *work)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001236{
David Howellsc4028952006-11-22 14:57:56 +00001237 struct scsi_qla_host *ha =
1238 container_of(work, struct scsi_qla_host, dpc_work);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001239 struct ddb_entry *ddb_entry, *dtemp;
David C Somayajulu477ffb92007-01-22 12:26:11 -08001240 int status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001241
David C Somayajuluf26b9042006-11-15 16:41:09 -08001242 DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301243 "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
1244 ha->host_no, __func__, ha->flags, ha->dpc_flags))
David Somayajuluafaf5a22006-09-19 10:28:00 -07001245
1246 /* Initialization not yet finished. Don't do anything yet. */
1247 if (!test_bit(AF_INIT_DONE, &ha->flags))
Vikas Chaudhary0753b482010-07-30 14:27:19 +05301248 goto do_dpc_exit;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001249
Lalit Chandivade2232be02010-07-30 14:38:47 +05301250 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1251 DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
1252 ha->host_no, __func__, ha->flags));
1253 goto do_dpc_exit;
1254 }
1255
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301256 if (is_qla8022(ha)) {
1257 if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
1258 qla4_8xxx_idc_lock(ha);
1259 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
1260 QLA82XX_DEV_FAILED);
1261 qla4_8xxx_idc_unlock(ha);
1262 ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
1263 qla4_8xxx_device_state_handler(ha);
1264 }
1265 if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
1266 qla4_8xxx_need_qsnt_handler(ha);
1267 }
1268 }
1269
1270 if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
1271 (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07001272 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301273 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
1274 if (ql4xdontresethba) {
1275 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
1276 ha->host_no, __func__));
1277 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
1278 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1279 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1280 goto dpc_post_reset_ha;
1281 }
1282 if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
1283 test_bit(DPC_RESET_HA, &ha->dpc_flags))
1284 qla4xxx_recover_adapter(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001285
David C Somayajulu477ffb92007-01-22 12:26:11 -08001286 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001287 uint8_t wait_time = RESET_INTR_TOV;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001288
David Somayajuluafaf5a22006-09-19 10:28:00 -07001289 while ((readw(&ha->reg->ctrl_status) &
1290 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
1291 if (--wait_time == 0)
1292 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001293 msleep(1000);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001294 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001295 if (wait_time == 0)
1296 DEBUG2(printk("scsi%ld: %s: SR|FSR "
1297 "bit not cleared-- resetting\n",
1298 ha->host_no, __func__));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301299 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
David C Somayajulu477ffb92007-01-22 12:26:11 -08001300 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
1301 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301302 status = qla4xxx_recover_adapter(ha);
David C Somayajulu477ffb92007-01-22 12:26:11 -08001303 }
1304 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1305 if (status == QLA_SUCCESS)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301306 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001307 }
1308 }
1309
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301310dpc_post_reset_ha:
David Somayajuluafaf5a22006-09-19 10:28:00 -07001311 /* ---- process AEN? --- */
1312 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
1313 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
1314
1315 /* ---- Get DHCP IP Address? --- */
1316 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
1317 qla4xxx_get_dhcp_ip_address(ha);
1318
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301319 /* ---- link change? --- */
1320 if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
1321 if (!test_bit(AF_LINK_UP, &ha->flags)) {
1322 /* ---- link down? --- */
1323 list_for_each_entry_safe(ddb_entry, dtemp,
1324 &ha->ddb_list, list) {
1325 if (atomic_read(&ddb_entry->state) ==
1326 DDB_STATE_ONLINE)
1327 qla4xxx_mark_device_missing(ha,
1328 ddb_entry);
1329 }
1330 } else {
1331 /* ---- link up? --- *
1332 * F/W will auto login to all devices ONLY ONCE after
1333 * link up during driver initialization and runtime
1334 * fatal error recovery. Therefore, the driver must
1335 * manually relogin to devices when recovering from
1336 * connection failures, logouts, expired KATO, etc. */
1337
1338 list_for_each_entry_safe(ddb_entry, dtemp,
1339 &ha->ddb_list, list) {
1340 if ((atomic_read(&ddb_entry->state) ==
1341 DDB_STATE_MISSING) ||
1342 (atomic_read(&ddb_entry->state) ==
1343 DDB_STATE_DEAD)) {
1344 if (ddb_entry->fw_ddb_device_state ==
1345 DDB_DS_SESSION_ACTIVE) {
1346 atomic_set(&ddb_entry->state,
1347 DDB_STATE_ONLINE);
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301348 ql4_printk(KERN_INFO, ha,
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301349 "scsi%ld: %s: ddb[%d]"
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301350 " marked ONLINE\n",
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301351 ha->host_no, __func__,
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301352 ddb_entry->fw_ddb_index);
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301353
1354 iscsi_unblock_session(
1355 ddb_entry->sess);
1356 } else
1357 qla4xxx_relogin_device(
1358 ha, ddb_entry);
1359 }
1360
1361 }
1362 }
1363 }
1364
David Somayajuluafaf5a22006-09-19 10:28:00 -07001365 /* ---- relogin device? --- */
1366 if (adapter_up(ha) &&
1367 test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
1368 list_for_each_entry_safe(ddb_entry, dtemp,
1369 &ha->ddb_list, list) {
1370 if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
1371 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
1372 qla4xxx_relogin_device(ha, ddb_entry);
1373
1374 /*
1375 * If mbx cmd times out there is no point
1376 * in continuing further.
1377 * With large no of targets this can hang
1378 * the system.
1379 */
1380 if (test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
1381 printk(KERN_WARNING "scsi%ld: %s: "
1382 "need to reset hba\n",
1383 ha->host_no, __func__);
1384 break;
1385 }
1386 }
1387 }
Vikas Chaudhary0753b482010-07-30 14:27:19 +05301388
1389do_dpc_exit:
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301390 clear_bit(AF_DPC_SCHEDULED, &ha->flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001391}
1392
1393/**
1394 * qla4xxx_free_adapter - release the adapter
1395 * @ha: pointer to adapter structure
1396 **/
1397static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
1398{
1399
1400 if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
1401 /* Turn-off interrupts on the card. */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301402 ha->isp_ops->disable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001403 }
1404
David Somayajuluafaf5a22006-09-19 10:28:00 -07001405 /* Remove timer thread, if present */
1406 if (ha->timer_active)
1407 qla4xxx_stop_timer(ha);
1408
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301409 /* Kill the kernel thread for this host */
1410 if (ha->dpc_thread)
1411 destroy_workqueue(ha->dpc_thread);
1412
1413 /* Put firmware in known state */
1414 ha->isp_ops->reset_firmware(ha);
1415
1416 if (is_qla8022(ha)) {
1417 qla4_8xxx_idc_lock(ha);
1418 qla4_8xxx_clear_drv_active(ha);
1419 qla4_8xxx_idc_unlock(ha);
1420 }
1421
David Somayajuluafaf5a22006-09-19 10:28:00 -07001422 /* Detach interrupts */
1423 if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301424 qla4xxx_free_irqs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001425
David C Somayajulubee4fe82007-05-23 18:03:32 -07001426 /* free extra memory */
1427 qla4xxx_mem_free(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301428}
David C Somayajulubee4fe82007-05-23 18:03:32 -07001429
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301430int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
1431{
1432 int status = 0;
1433 uint8_t revision_id;
1434 unsigned long mem_base, mem_len, db_base, db_len;
1435 struct pci_dev *pdev = ha->pdev;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001436
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301437 status = pci_request_regions(pdev, DRIVER_NAME);
1438 if (status) {
1439 printk(KERN_WARNING
1440 "scsi(%ld) Failed to reserve PIO regions (%s) "
1441 "status=%d\n", ha->host_no, pci_name(pdev), status);
1442 goto iospace_error_exit;
1443 }
1444
1445 pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
1446 DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
1447 __func__, revision_id));
1448 ha->revision_id = revision_id;
1449
1450 /* remap phys address */
1451 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
1452 mem_len = pci_resource_len(pdev, 0);
1453 DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
1454 __func__, mem_base, mem_len));
1455
1456 /* mapping of pcibase pointer */
1457 ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
1458 if (!ha->nx_pcibase) {
1459 printk(KERN_ERR
1460 "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
1461 pci_release_regions(ha->pdev);
1462 goto iospace_error_exit;
1463 }
1464
1465 /* Mapping of IO base pointer, door bell read and write pointer */
1466
1467 /* mapping of IO base pointer */
1468 ha->qla4_8xxx_reg =
1469 (struct device_reg_82xx __iomem *)((uint8_t *)ha->nx_pcibase +
1470 0xbc000 + (ha->pdev->devfn << 11));
1471
1472 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
1473 db_len = pci_resource_len(pdev, 4);
1474
Shyam Sundar2657c802010-10-06 22:50:29 -07001475 ha->nx_db_wr_ptr = (ha->pdev->devfn == 4 ? QLA82XX_CAM_RAM_DB1 :
1476 QLA82XX_CAM_RAM_DB2);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301477
Shyam Sundar2657c802010-10-06 22:50:29 -07001478 return 0;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301479iospace_error_exit:
1480 return -ENOMEM;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001481}
1482
1483/***
1484 * qla4xxx_iospace_config - maps registers
1485 * @ha: pointer to adapter structure
1486 *
1487 * This routines maps HBA's registers from the pci address space
1488 * into the kernel virtual address space for memory mapped i/o.
1489 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301490int qla4xxx_iospace_config(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001491{
1492 unsigned long pio, pio_len, pio_flags;
1493 unsigned long mmio, mmio_len, mmio_flags;
1494
1495 pio = pci_resource_start(ha->pdev, 0);
1496 pio_len = pci_resource_len(ha->pdev, 0);
1497 pio_flags = pci_resource_flags(ha->pdev, 0);
1498 if (pio_flags & IORESOURCE_IO) {
1499 if (pio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301500 ql4_printk(KERN_WARNING, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07001501 "Invalid PCI I/O region size\n");
1502 pio = 0;
1503 }
1504 } else {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301505 ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001506 pio = 0;
1507 }
1508
1509 /* Use MMIO operations for all accesses. */
1510 mmio = pci_resource_start(ha->pdev, 1);
1511 mmio_len = pci_resource_len(ha->pdev, 1);
1512 mmio_flags = pci_resource_flags(ha->pdev, 1);
1513
1514 if (!(mmio_flags & IORESOURCE_MEM)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301515 ql4_printk(KERN_ERR, ha,
1516 "region #0 not an MMIO resource, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001517
1518 goto iospace_error_exit;
1519 }
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301520
David Somayajuluafaf5a22006-09-19 10:28:00 -07001521 if (mmio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301522 ql4_printk(KERN_ERR, ha,
1523 "Invalid PCI mem region size, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001524 goto iospace_error_exit;
1525 }
1526
1527 if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301528 ql4_printk(KERN_WARNING, ha,
1529 "Failed to reserve PIO/MMIO regions\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001530
1531 goto iospace_error_exit;
1532 }
1533
1534 ha->pio_address = pio;
1535 ha->pio_length = pio_len;
1536 ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
1537 if (!ha->reg) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301538 ql4_printk(KERN_ERR, ha,
1539 "cannot remap MMIO, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001540
1541 goto iospace_error_exit;
1542 }
1543
1544 return 0;
1545
1546iospace_error_exit:
1547 return -ENOMEM;
1548}
1549
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301550static struct isp_operations qla4xxx_isp_ops = {
1551 .iospace_config = qla4xxx_iospace_config,
1552 .pci_config = qla4xxx_pci_config,
1553 .disable_intrs = qla4xxx_disable_intrs,
1554 .enable_intrs = qla4xxx_enable_intrs,
1555 .start_firmware = qla4xxx_start_firmware,
1556 .intr_handler = qla4xxx_intr_handler,
1557 .interrupt_service_routine = qla4xxx_interrupt_service_routine,
1558 .reset_chip = qla4xxx_soft_reset,
1559 .reset_firmware = qla4xxx_hw_reset,
1560 .queue_iocb = qla4xxx_queue_iocb,
1561 .complete_iocb = qla4xxx_complete_iocb,
1562 .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
1563 .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
1564 .get_sys_info = qla4xxx_get_sys_info,
1565};
1566
1567static struct isp_operations qla4_8xxx_isp_ops = {
1568 .iospace_config = qla4_8xxx_iospace_config,
1569 .pci_config = qla4_8xxx_pci_config,
1570 .disable_intrs = qla4_8xxx_disable_intrs,
1571 .enable_intrs = qla4_8xxx_enable_intrs,
1572 .start_firmware = qla4_8xxx_load_risc,
1573 .intr_handler = qla4_8xxx_intr_handler,
1574 .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
1575 .reset_chip = qla4_8xxx_isp_reset,
1576 .reset_firmware = qla4_8xxx_stop_firmware,
1577 .queue_iocb = qla4_8xxx_queue_iocb,
1578 .complete_iocb = qla4_8xxx_complete_iocb,
1579 .rd_shdw_req_q_out = qla4_8xxx_rd_shdw_req_q_out,
1580 .rd_shdw_rsp_q_in = qla4_8xxx_rd_shdw_rsp_q_in,
1581 .get_sys_info = qla4_8xxx_get_sys_info,
1582};
1583
1584uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
1585{
1586 return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
1587}
1588
1589uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
1590{
1591 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
1592}
1593
1594uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
1595{
1596 return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
1597}
1598
1599uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
1600{
1601 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
1602}
1603
David Somayajuluafaf5a22006-09-19 10:28:00 -07001604/**
1605 * qla4xxx_probe_adapter - callback function to probe HBA
1606 * @pdev: pointer to pci_dev structure
1607 * @pci_device_id: pointer to pci_device entry
1608 *
1609 * This routine will probe for Qlogic 4xxx iSCSI host adapters.
1610 * It returns zero if successful. It also initializes all data necessary for
1611 * the driver.
1612 **/
1613static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
1614 const struct pci_device_id *ent)
1615{
1616 int ret = -ENODEV, status;
1617 struct Scsi_Host *host;
1618 struct scsi_qla_host *ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001619 uint8_t init_retry_count = 0;
1620 char buf[34];
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301621 struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
Prasanna Mumbaif9880e72011-03-21 03:34:26 -07001622 uint32_t dev_state;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001623
1624 if (pci_enable_device(pdev))
1625 return -1;
1626
1627 host = scsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha));
1628 if (host == NULL) {
1629 printk(KERN_WARNING
1630 "qla4xxx: Couldn't allocate host from scsi layer!\n");
1631 goto probe_disable_device;
1632 }
1633
1634 /* Clear our data area */
1635 ha = (struct scsi_qla_host *) host->hostdata;
1636 memset(ha, 0, sizeof(*ha));
1637
1638 /* Save the information from PCI BIOS. */
1639 ha->pdev = pdev;
1640 ha->host = host;
1641 ha->host_no = host->host_no;
1642
Lalit Chandivade2232be02010-07-30 14:38:47 +05301643 pci_enable_pcie_error_reporting(pdev);
1644
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301645 /* Setup Runtime configurable options */
1646 if (is_qla8022(ha)) {
1647 ha->isp_ops = &qla4_8xxx_isp_ops;
1648 rwlock_init(&ha->hw_lock);
1649 ha->qdr_sn_window = -1;
1650 ha->ddr_mn_window = -1;
1651 ha->curr_window = 255;
1652 ha->func_num = PCI_FUNC(ha->pdev->devfn);
1653 nx_legacy_intr = &legacy_intr[ha->func_num];
1654 ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
1655 ha->nx_legacy_intr.tgt_status_reg =
1656 nx_legacy_intr->tgt_status_reg;
1657 ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
1658 ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
1659 } else {
1660 ha->isp_ops = &qla4xxx_isp_ops;
1661 }
1662
Lalit Chandivade2232be02010-07-30 14:38:47 +05301663 /* Set EEH reset type to fundamental if required by hba */
1664 if (is_qla8022(ha))
1665 pdev->needs_freset = 1;
1666
David Somayajuluafaf5a22006-09-19 10:28:00 -07001667 /* Configure PCI I/O space. */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301668 ret = ha->isp_ops->iospace_config(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001669 if (ret)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301670 goto probe_failed_ioconfig;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001671
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301672 ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
David Somayajuluafaf5a22006-09-19 10:28:00 -07001673 pdev->device, pdev->irq, ha->reg);
1674
1675 qla4xxx_config_dma_addressing(ha);
1676
1677 /* Initialize lists and spinlocks. */
1678 INIT_LIST_HEAD(&ha->ddb_list);
1679 INIT_LIST_HEAD(&ha->free_srb_q);
1680
1681 mutex_init(&ha->mbox_sem);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301682 init_completion(&ha->mbx_intr_comp);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001683
1684 spin_lock_init(&ha->hardware_lock);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001685
1686 /* Allocate dma buffers */
1687 if (qla4xxx_mem_alloc(ha)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301688 ql4_printk(KERN_WARNING, ha,
1689 "[ERROR] Failed to allocate memory for adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001690
1691 ret = -ENOMEM;
1692 goto probe_failed;
1693 }
1694
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301695 if (is_qla8022(ha))
1696 (void) qla4_8xxx_get_flash_info(ha);
1697
David Somayajuluafaf5a22006-09-19 10:28:00 -07001698 /*
1699 * Initialize the Host adapter request/response queues and
1700 * firmware
1701 * NOTE: interrupts enabled upon successful completion
1702 */
1703 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301704 while ((!test_bit(AF_ONLINE, &ha->flags)) &&
1705 init_retry_count++ < MAX_INIT_RETRIES) {
Prasanna Mumbaif9880e72011-03-21 03:34:26 -07001706
1707 if (is_qla8022(ha)) {
1708 qla4_8xxx_idc_lock(ha);
1709 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
1710 qla4_8xxx_idc_unlock(ha);
1711 if (dev_state == QLA82XX_DEV_FAILED) {
1712 ql4_printk(KERN_WARNING, ha, "%s: don't retry "
1713 "initialize adapter. H/W is in failed state\n",
1714 __func__);
1715 break;
1716 }
1717 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001718 DEBUG2(printk("scsi: %s: retrying adapter initialization "
1719 "(%d)\n", __func__, init_retry_count));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301720
1721 if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
1722 continue;
1723
David Somayajuluafaf5a22006-09-19 10:28:00 -07001724 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1725 }
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301726
1727 if (!test_bit(AF_ONLINE, &ha->flags)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301728 ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001729
Lalit Chandivadefe998522010-12-02 22:12:36 -08001730 if (is_qla8022(ha) && ql4xdontresethba) {
1731 /* Put the device in failed state. */
1732 DEBUG2(printk(KERN_ERR "HW STATE: FAILED\n"));
1733 qla4_8xxx_idc_lock(ha);
1734 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
1735 QLA82XX_DEV_FAILED);
1736 qla4_8xxx_idc_unlock(ha);
1737 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001738 ret = -ENODEV;
1739 goto probe_failed;
1740 }
1741
1742 host->cmd_per_lun = 3;
1743 host->max_channel = 0;
1744 host->max_lun = MAX_LUNS - 1;
1745 host->max_id = MAX_TARGETS;
1746 host->max_cmd_len = IOCB_MAX_CDB_LEN;
1747 host->can_queue = MAX_SRBS ;
1748 host->transportt = qla4xxx_scsi_transport;
1749
1750 ret = scsi_init_shared_tag_map(host, MAX_SRBS);
1751 if (ret) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301752 ql4_printk(KERN_WARNING, ha,
1753 "scsi_init_shared_tag_map failed\n");
1754 goto probe_failed;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001755 }
1756
1757 /* Startup the kernel thread for this host adapter. */
1758 DEBUG2(printk("scsi: %s: Starting kernel thread for "
1759 "qla4xxx_dpc\n", __func__));
1760 sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
1761 ha->dpc_thread = create_singlethread_workqueue(buf);
1762 if (!ha->dpc_thread) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301763 ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001764 ret = -ENODEV;
1765 goto probe_failed;
1766 }
David Howellsc4028952006-11-22 14:57:56 +00001767 INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001768
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301769 /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
1770 * (which is called indirectly by qla4xxx_initialize_adapter),
1771 * so that irqs will be registered after crbinit but before
1772 * mbx_intr_enable.
1773 */
1774 if (!is_qla8022(ha)) {
1775 ret = qla4xxx_request_irqs(ha);
1776 if (ret) {
1777 ql4_printk(KERN_WARNING, ha, "Failed to reserve "
1778 "interrupt %d already in use.\n", pdev->irq);
1779 goto probe_failed;
1780 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001781 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001782
Lalit Chandivade2232be02010-07-30 14:38:47 +05301783 pci_save_state(ha->pdev);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301784 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001785
1786 /* Start timer thread. */
1787 qla4xxx_start_timer(ha, qla4xxx_timer, 1);
1788
1789 set_bit(AF_INIT_DONE, &ha->flags);
1790
1791 pci_set_drvdata(pdev, ha);
1792
1793 ret = scsi_add_host(host, &pdev->dev);
1794 if (ret)
1795 goto probe_failed;
1796
David Somayajuluafaf5a22006-09-19 10:28:00 -07001797 printk(KERN_INFO
1798 " QLogic iSCSI HBA Driver version: %s\n"
1799 " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
1800 qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
1801 ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
1802 ha->patch_number, ha->build_number);
Mike Christie921601b2008-01-31 13:36:49 -06001803 scsi_scan_host(host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001804 return 0;
1805
David Somayajuluafaf5a22006-09-19 10:28:00 -07001806probe_failed:
1807 qla4xxx_free_adapter(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301808
1809probe_failed_ioconfig:
Lalit Chandivade2232be02010-07-30 14:38:47 +05301810 pci_disable_pcie_error_reporting(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001811 scsi_host_put(ha->host);
1812
1813probe_disable_device:
1814 pci_disable_device(pdev);
1815
1816 return ret;
1817}
1818
1819/**
Karen Higgins7eece5a2011-03-21 03:34:29 -07001820 * qla4xxx_prevent_other_port_reinit - prevent other port from re-initialize
1821 * @ha: pointer to adapter structure
1822 *
1823 * Mark the other ISP-4xxx port to indicate that the driver is being removed,
1824 * so that the other port will not re-initialize while in the process of
1825 * removing the ha due to driver unload or hba hotplug.
1826 **/
1827static void qla4xxx_prevent_other_port_reinit(struct scsi_qla_host *ha)
1828{
1829 struct scsi_qla_host *other_ha = NULL;
1830 struct pci_dev *other_pdev = NULL;
1831 int fn = ISP4XXX_PCI_FN_2;
1832
1833 /*iscsi function numbers for ISP4xxx is 1 and 3*/
1834 if (PCI_FUNC(ha->pdev->devfn) & BIT_1)
1835 fn = ISP4XXX_PCI_FN_1;
1836
1837 other_pdev =
1838 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
1839 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
1840 fn));
1841
1842 /* Get other_ha if other_pdev is valid and state is enable*/
1843 if (other_pdev) {
1844 if (atomic_read(&other_pdev->enable_cnt)) {
1845 other_ha = pci_get_drvdata(other_pdev);
1846 if (other_ha) {
1847 set_bit(AF_HA_REMOVAL, &other_ha->flags);
1848 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: "
1849 "Prevent %s reinit\n", __func__,
1850 dev_name(&other_ha->pdev->dev)));
1851 }
1852 }
1853 pci_dev_put(other_pdev);
1854 }
1855}
1856
1857/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07001858 * qla4xxx_remove_adapter - calback function to remove adapter.
1859 * @pci_dev: PCI device pointer
1860 **/
1861static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
1862{
1863 struct scsi_qla_host *ha;
1864
1865 ha = pci_get_drvdata(pdev);
1866
Karen Higgins7eece5a2011-03-21 03:34:29 -07001867 if (!is_qla8022(ha))
1868 qla4xxx_prevent_other_port_reinit(ha);
David C Somayajulubee4fe82007-05-23 18:03:32 -07001869
David Somayajuluafaf5a22006-09-19 10:28:00 -07001870 /* remove devs from iscsi_sessions to scsi_devices */
1871 qla4xxx_free_ddb_list(ha);
1872
1873 scsi_remove_host(ha->host);
1874
1875 qla4xxx_free_adapter(ha);
1876
1877 scsi_host_put(ha->host);
1878
Lalit Chandivade2232be02010-07-30 14:38:47 +05301879 pci_disable_pcie_error_reporting(pdev);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301880 pci_disable_device(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001881 pci_set_drvdata(pdev, NULL);
1882}
1883
1884/**
1885 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
1886 * @ha: HA context
1887 *
1888 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
1889 * supported addressing method.
1890 */
Adrian Bunk47975472007-04-26 00:35:16 -07001891static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001892{
1893 int retval;
1894
1895 /* Update our PCI device dma_mask for full 64 bit mask */
Yang Hongyang6a355282009-04-06 19:01:13 -07001896 if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
1897 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001898 dev_dbg(&ha->pdev->dev,
1899 "Failed to set 64 bit PCI consistent mask; "
1900 "using 32 bit.\n");
1901 retval = pci_set_consistent_dma_mask(ha->pdev,
Yang Hongyang284901a2009-04-06 19:01:15 -07001902 DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001903 }
1904 } else
Yang Hongyang284901a2009-04-06 19:01:15 -07001905 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001906}
1907
1908static int qla4xxx_slave_alloc(struct scsi_device *sdev)
1909{
1910 struct iscsi_cls_session *sess = starget_to_session(sdev->sdev_target);
1911 struct ddb_entry *ddb = sess->dd_data;
Vikas Chaudhary8bb40332011-03-21 03:34:31 -07001912 int queue_depth = QL4_DEF_QDEPTH;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001913
1914 sdev->hostdata = ddb;
1915 sdev->tagged_supported = 1;
Vikas Chaudhary8bb40332011-03-21 03:34:31 -07001916
1917 if (ql4xmaxqdepth != 0 && ql4xmaxqdepth <= 0xffffU)
1918 queue_depth = ql4xmaxqdepth;
1919
1920 scsi_activate_tcq(sdev, queue_depth);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001921 return 0;
1922}
1923
1924static int qla4xxx_slave_configure(struct scsi_device *sdev)
1925{
1926 sdev->tagged_supported = 1;
1927 return 0;
1928}
1929
1930static void qla4xxx_slave_destroy(struct scsi_device *sdev)
1931{
1932 scsi_deactivate_tcq(sdev, 1);
1933}
1934
1935/**
1936 * qla4xxx_del_from_active_array - returns an active srb
1937 * @ha: Pointer to host adapter structure.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001938 * @index: index into the active_array
David Somayajuluafaf5a22006-09-19 10:28:00 -07001939 *
1940 * This routine removes and returns the srb at the specified index
1941 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301942struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
1943 uint32_t index)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001944{
1945 struct srb *srb = NULL;
Vikas Chaudhary53698872010-04-28 11:41:59 +05301946 struct scsi_cmnd *cmd = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001947
Vikas Chaudhary53698872010-04-28 11:41:59 +05301948 cmd = scsi_host_find_tag(ha->host, index);
1949 if (!cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001950 return srb;
1951
Vikas Chaudhary53698872010-04-28 11:41:59 +05301952 srb = (struct srb *)CMD_SP(cmd);
1953 if (!srb)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001954 return srb;
1955
1956 /* update counters */
1957 if (srb->flags & SRB_DMA_VALID) {
1958 ha->req_q_count += srb->iocb_cnt;
1959 ha->iocb_cnt -= srb->iocb_cnt;
1960 if (srb->cmd)
Vikas Chaudhary53698872010-04-28 11:41:59 +05301961 srb->cmd->host_scribble =
1962 (unsigned char *)(unsigned long) MAX_SRBS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001963 }
1964 return srb;
1965}
1966
1967/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07001968 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301969 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001970 * @cmd: Scsi Command to wait on.
1971 *
1972 * This routine waits for the command to be returned by the Firmware
1973 * for some max time.
1974 **/
1975static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
1976 struct scsi_cmnd *cmd)
1977{
1978 int done = 0;
1979 struct srb *rp;
1980 uint32_t max_wait_time = EH_WAIT_CMD_TOV;
Lalit Chandivade2232be02010-07-30 14:38:47 +05301981 int ret = SUCCESS;
1982
1983 /* Dont wait on command if PCI error is being handled
1984 * by PCI AER driver
1985 */
1986 if (unlikely(pci_channel_offline(ha->pdev)) ||
1987 (test_bit(AF_EEH_BUSY, &ha->flags))) {
1988 ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
1989 ha->host_no, __func__);
1990 return ret;
1991 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001992
1993 do {
1994 /* Checking to see if its returned to OS */
Vikas Chaudhary53698872010-04-28 11:41:59 +05301995 rp = (struct srb *) CMD_SP(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001996 if (rp == NULL) {
1997 done++;
1998 break;
1999 }
2000
2001 msleep(2000);
2002 } while (max_wait_time--);
2003
2004 return done;
2005}
2006
2007/**
2008 * qla4xxx_wait_for_hba_online - waits for HBA to come online
2009 * @ha: Pointer to host adapter structure
2010 **/
2011static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
2012{
2013 unsigned long wait_online;
2014
Vikas Chaudharyf581a3f2010-10-06 22:47:48 -07002015 wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002016 while (time_before(jiffies, wait_online)) {
2017
2018 if (adapter_up(ha))
2019 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002020
2021 msleep(2000);
2022 }
2023
2024 return QLA_ERROR;
2025}
2026
2027/**
Mike Christiece545032008-02-29 18:25:20 -06002028 * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02002029 * @ha: pointer to HBA
David Somayajuluafaf5a22006-09-19 10:28:00 -07002030 * @t: target id
2031 * @l: lun id
2032 *
2033 * This function waits for all outstanding commands to a lun to complete. It
2034 * returns 0 if all pending commands are returned and 1 otherwise.
2035 **/
Mike Christiece545032008-02-29 18:25:20 -06002036static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
2037 struct scsi_target *stgt,
2038 struct scsi_device *sdev)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002039{
2040 int cnt;
2041 int status = 0;
2042 struct scsi_cmnd *cmd;
2043
2044 /*
Mike Christiece545032008-02-29 18:25:20 -06002045 * Waiting for all commands for the designated target or dev
2046 * in the active array
David Somayajuluafaf5a22006-09-19 10:28:00 -07002047 */
2048 for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
2049 cmd = scsi_host_find_tag(ha->host, cnt);
Mike Christiece545032008-02-29 18:25:20 -06002050 if (cmd && stgt == scsi_target(cmd->device) &&
2051 (!sdev || sdev == cmd->device)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002052 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
2053 status++;
2054 break;
2055 }
2056 }
2057 }
2058 return status;
2059}
2060
2061/**
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302062 * qla4xxx_eh_abort - callback for abort task.
2063 * @cmd: Pointer to Linux's SCSI command structure
2064 *
2065 * This routine is called by the Linux OS to abort the specified
2066 * command.
2067 **/
2068static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
2069{
2070 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2071 unsigned int id = cmd->device->id;
2072 unsigned int lun = cmd->device->lun;
2073 unsigned long serial = cmd->serial_number;
Mike Christie92b3e5b2010-10-06 22:51:17 -07002074 unsigned long flags;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302075 struct srb *srb = NULL;
2076 int ret = SUCCESS;
2077 int wait = 0;
2078
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302079 ql4_printk(KERN_INFO, ha,
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302080 "scsi%ld:%d:%d: Abort command issued cmd=%p, pid=%ld\n",
2081 ha->host_no, id, lun, cmd, serial);
2082
Mike Christie92b3e5b2010-10-06 22:51:17 -07002083 spin_lock_irqsave(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302084 srb = (struct srb *) CMD_SP(cmd);
Mike Christie92b3e5b2010-10-06 22:51:17 -07002085 if (!srb) {
2086 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302087 return SUCCESS;
Mike Christie92b3e5b2010-10-06 22:51:17 -07002088 }
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302089 kref_get(&srb->srb_ref);
Mike Christie92b3e5b2010-10-06 22:51:17 -07002090 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302091
2092 if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
2093 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
2094 ha->host_no, id, lun));
2095 ret = FAILED;
2096 } else {
2097 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
2098 ha->host_no, id, lun));
2099 wait = 1;
2100 }
2101
2102 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
2103
2104 /* Wait for command to complete */
2105 if (wait) {
2106 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
2107 DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
2108 ha->host_no, id, lun));
2109 ret = FAILED;
2110 }
2111 }
2112
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302113 ql4_printk(KERN_INFO, ha,
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302114 "scsi%ld:%d:%d: Abort command - %s\n",
2115 ha->host_no, id, lun, (ret == SUCCESS) ? "succeded" : "failed");
2116
2117 return ret;
2118}
2119
2120/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002121 * qla4xxx_eh_device_reset - callback for target reset.
2122 * @cmd: Pointer to Linux's SCSI command structure
2123 *
2124 * This routine is called by the Linux OS to reset all luns on the
2125 * specified target.
2126 **/
2127static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
2128{
2129 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2130 struct ddb_entry *ddb_entry = cmd->device->hostdata;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002131 int ret = FAILED, stat;
2132
Karen Higgins612f73482009-07-15 15:03:01 -05002133 if (!ddb_entry)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002134 return ret;
2135
Mike Christiec01be6d2010-07-22 16:59:49 +05302136 ret = iscsi_block_scsi_eh(cmd);
2137 if (ret)
2138 return ret;
2139 ret = FAILED;
2140
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302141 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002142 "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
2143 cmd->device->channel, cmd->device->id, cmd->device->lun);
2144
2145 DEBUG2(printk(KERN_INFO
2146 "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
2147 "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
Jens Axboe242f9dc2008-09-14 05:55:09 -07002148 cmd, jiffies, cmd->request->timeout / HZ,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002149 ha->dpc_flags, cmd->result, cmd->allowed));
2150
2151 /* FIXME: wait for hba to go online */
2152 stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
2153 if (stat != QLA_SUCCESS) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302154 ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002155 goto eh_dev_reset_done;
2156 }
2157
Mike Christiece545032008-02-29 18:25:20 -06002158 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2159 cmd->device)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302160 ql4_printk(KERN_INFO, ha,
Mike Christiece545032008-02-29 18:25:20 -06002161 "DEVICE RESET FAILED - waiting for "
2162 "commands.\n");
2163 goto eh_dev_reset_done;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002164 }
2165
David C Somayajulu9d562912008-03-19 11:23:03 -07002166 /* Send marker. */
2167 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2168 MM_LUN_RESET) != QLA_SUCCESS)
2169 goto eh_dev_reset_done;
2170
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302171 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002172 "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
2173 ha->host_no, cmd->device->channel, cmd->device->id,
2174 cmd->device->lun);
2175
2176 ret = SUCCESS;
2177
2178eh_dev_reset_done:
2179
2180 return ret;
2181}
2182
2183/**
Mike Christiece545032008-02-29 18:25:20 -06002184 * qla4xxx_eh_target_reset - callback for target reset.
2185 * @cmd: Pointer to Linux's SCSI command structure
2186 *
2187 * This routine is called by the Linux OS to reset the target.
2188 **/
2189static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
2190{
2191 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2192 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christiec01be6d2010-07-22 16:59:49 +05302193 int stat, ret;
Mike Christiece545032008-02-29 18:25:20 -06002194
2195 if (!ddb_entry)
2196 return FAILED;
2197
Mike Christiec01be6d2010-07-22 16:59:49 +05302198 ret = iscsi_block_scsi_eh(cmd);
2199 if (ret)
2200 return ret;
2201
Mike Christiece545032008-02-29 18:25:20 -06002202 starget_printk(KERN_INFO, scsi_target(cmd->device),
2203 "WARM TARGET RESET ISSUED.\n");
2204
2205 DEBUG2(printk(KERN_INFO
2206 "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
2207 "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
Jens Axboe242f9dc2008-09-14 05:55:09 -07002208 ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
Mike Christiece545032008-02-29 18:25:20 -06002209 ha->dpc_flags, cmd->result, cmd->allowed));
2210
2211 stat = qla4xxx_reset_target(ha, ddb_entry);
2212 if (stat != QLA_SUCCESS) {
2213 starget_printk(KERN_INFO, scsi_target(cmd->device),
2214 "WARM TARGET RESET FAILED.\n");
2215 return FAILED;
2216 }
2217
Mike Christiece545032008-02-29 18:25:20 -06002218 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2219 NULL)) {
2220 starget_printk(KERN_INFO, scsi_target(cmd->device),
2221 "WARM TARGET DEVICE RESET FAILED - "
2222 "waiting for commands.\n");
2223 return FAILED;
2224 }
2225
David C Somayajulu9d562912008-03-19 11:23:03 -07002226 /* Send marker. */
2227 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2228 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
2229 starget_printk(KERN_INFO, scsi_target(cmd->device),
2230 "WARM TARGET DEVICE RESET FAILED - "
2231 "marker iocb failed.\n");
2232 return FAILED;
2233 }
2234
Mike Christiece545032008-02-29 18:25:20 -06002235 starget_printk(KERN_INFO, scsi_target(cmd->device),
2236 "WARM TARGET RESET SUCCEEDED.\n");
2237 return SUCCESS;
2238}
2239
2240/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002241 * qla4xxx_eh_host_reset - kernel callback
2242 * @cmd: Pointer to Linux's SCSI command structure
2243 *
2244 * This routine is invoked by the Linux kernel to perform fatal error
2245 * recovery on the specified adapter.
2246 **/
2247static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
2248{
2249 int return_status = FAILED;
2250 struct scsi_qla_host *ha;
2251
2252 ha = (struct scsi_qla_host *) cmd->device->host->hostdata;
2253
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302254 if (ql4xdontresethba) {
2255 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
2256 ha->host_no, __func__));
2257 return FAILED;
2258 }
2259
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302260 ql4_printk(KERN_INFO, ha,
Karen Higginsdca05c42009-07-15 15:03:00 -05002261 "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002262 cmd->device->channel, cmd->device->id, cmd->device->lun);
2263
2264 if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
2265 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
2266 "DEAD.\n", ha->host_no, cmd->device->channel,
2267 __func__));
2268
2269 return FAILED;
2270 }
2271
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302272 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
2273 if (is_qla8022(ha))
2274 set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2275 else
2276 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2277 }
Mike Christie50a29ae2008-03-04 13:26:53 -06002278
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302279 if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002280 return_status = SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002281
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302282 ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
David Somayajuluafaf5a22006-09-19 10:28:00 -07002283 return_status == FAILED ? "FAILED" : "SUCCEDED");
2284
2285 return return_status;
2286}
2287
Lalit Chandivade2232be02010-07-30 14:38:47 +05302288/* PCI AER driver recovers from all correctable errors w/o
2289 * driver intervention. For uncorrectable errors PCI AER
2290 * driver calls the following device driver's callbacks
2291 *
2292 * - Fatal Errors - link_reset
2293 * - Non-Fatal Errors - driver's pci_error_detected() which
2294 * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
2295 *
2296 * PCI AER driver calls
2297 * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
2298 * returns RECOVERED or NEED_RESET if fw_hung
2299 * NEED_RESET - driver's slot_reset()
2300 * DISCONNECT - device is dead & cannot recover
2301 * RECOVERED - driver's pci_resume()
2302 */
2303static pci_ers_result_t
2304qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
2305{
2306 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2307
2308 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
2309 ha->host_no, __func__, state);
2310
2311 if (!is_aer_supported(ha))
2312 return PCI_ERS_RESULT_NONE;
2313
2314 switch (state) {
2315 case pci_channel_io_normal:
2316 clear_bit(AF_EEH_BUSY, &ha->flags);
2317 return PCI_ERS_RESULT_CAN_RECOVER;
2318 case pci_channel_io_frozen:
2319 set_bit(AF_EEH_BUSY, &ha->flags);
2320 qla4xxx_mailbox_premature_completion(ha);
2321 qla4xxx_free_irqs(ha);
2322 pci_disable_device(pdev);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002323 /* Return back all IOs */
2324 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
Lalit Chandivade2232be02010-07-30 14:38:47 +05302325 return PCI_ERS_RESULT_NEED_RESET;
2326 case pci_channel_io_perm_failure:
2327 set_bit(AF_EEH_BUSY, &ha->flags);
2328 set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
2329 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
2330 return PCI_ERS_RESULT_DISCONNECT;
2331 }
2332 return PCI_ERS_RESULT_NEED_RESET;
2333}
2334
2335/**
2336 * qla4xxx_pci_mmio_enabled() gets called if
2337 * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
2338 * and read/write to the device still works.
2339 **/
2340static pci_ers_result_t
2341qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
2342{
2343 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2344
2345 if (!is_aer_supported(ha))
2346 return PCI_ERS_RESULT_NONE;
2347
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002348 return PCI_ERS_RESULT_RECOVERED;
Lalit Chandivade2232be02010-07-30 14:38:47 +05302349}
2350
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002351static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
Lalit Chandivade2232be02010-07-30 14:38:47 +05302352{
2353 uint32_t rval = QLA_ERROR;
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002354 uint32_t ret = 0;
Lalit Chandivade2232be02010-07-30 14:38:47 +05302355 int fn;
2356 struct pci_dev *other_pdev = NULL;
2357
2358 ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
2359
2360 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2361
2362 if (test_bit(AF_ONLINE, &ha->flags)) {
2363 clear_bit(AF_ONLINE, &ha->flags);
2364 qla4xxx_mark_all_devices_missing(ha);
2365 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
Lalit Chandivade2232be02010-07-30 14:38:47 +05302366 }
2367
2368 fn = PCI_FUNC(ha->pdev->devfn);
2369 while (fn > 0) {
2370 fn--;
2371 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
2372 "func %x\n", ha->host_no, __func__, fn);
2373 /* Get the pci device given the domain, bus,
2374 * slot/function number */
2375 other_pdev =
2376 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
2377 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
2378 fn));
2379
2380 if (!other_pdev)
2381 continue;
2382
2383 if (atomic_read(&other_pdev->enable_cnt)) {
2384 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
2385 "func in enabled state%x\n", ha->host_no,
2386 __func__, fn);
2387 pci_dev_put(other_pdev);
2388 break;
2389 }
2390 pci_dev_put(other_pdev);
2391 }
2392
2393 /* The first function on the card, the reset owner will
2394 * start & initialize the firmware. The other functions
2395 * on the card will reset the firmware context
2396 */
2397 if (!fn) {
2398 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
2399 "0x%x is the owner\n", ha->host_no, __func__,
2400 ha->pdev->devfn);
2401
2402 qla4_8xxx_idc_lock(ha);
2403 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2404 QLA82XX_DEV_COLD);
2405
2406 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
2407 QLA82XX_IDC_VERSION);
2408
2409 qla4_8xxx_idc_unlock(ha);
2410 clear_bit(AF_FW_RECOVERY, &ha->flags);
2411 rval = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
2412 qla4_8xxx_idc_lock(ha);
2413
2414 if (rval != QLA_SUCCESS) {
2415 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
2416 "FAILED\n", ha->host_no, __func__);
2417 qla4_8xxx_clear_drv_active(ha);
2418 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2419 QLA82XX_DEV_FAILED);
2420 } else {
2421 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
2422 "READY\n", ha->host_no, __func__);
2423 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2424 QLA82XX_DEV_READY);
2425 /* Clear driver state register */
2426 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
2427 qla4_8xxx_set_drv_active(ha);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002428 ret = qla4xxx_request_irqs(ha);
2429 if (ret) {
2430 ql4_printk(KERN_WARNING, ha, "Failed to "
2431 "reserve interrupt %d already in use.\n",
2432 ha->pdev->irq);
2433 rval = QLA_ERROR;
2434 } else {
2435 ha->isp_ops->enable_intrs(ha);
2436 rval = QLA_SUCCESS;
2437 }
Lalit Chandivade2232be02010-07-30 14:38:47 +05302438 }
2439 qla4_8xxx_idc_unlock(ha);
2440 } else {
2441 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
2442 "the reset owner\n", ha->host_no, __func__,
2443 ha->pdev->devfn);
2444 if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
2445 QLA82XX_DEV_READY)) {
2446 clear_bit(AF_FW_RECOVERY, &ha->flags);
2447 rval = qla4xxx_initialize_adapter(ha,
2448 PRESERVE_DDB_LIST);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002449 if (rval == QLA_SUCCESS) {
2450 ret = qla4xxx_request_irqs(ha);
2451 if (ret) {
2452 ql4_printk(KERN_WARNING, ha, "Failed to"
2453 " reserve interrupt %d already in"
2454 " use.\n", ha->pdev->irq);
2455 rval = QLA_ERROR;
2456 } else {
2457 ha->isp_ops->enable_intrs(ha);
2458 rval = QLA_SUCCESS;
2459 }
2460 }
Lalit Chandivade2232be02010-07-30 14:38:47 +05302461 qla4_8xxx_idc_lock(ha);
2462 qla4_8xxx_set_drv_active(ha);
2463 qla4_8xxx_idc_unlock(ha);
2464 }
2465 }
2466 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2467 return rval;
2468}
2469
2470static pci_ers_result_t
2471qla4xxx_pci_slot_reset(struct pci_dev *pdev)
2472{
2473 pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
2474 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2475 int rc;
2476
2477 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
2478 ha->host_no, __func__);
2479
2480 if (!is_aer_supported(ha))
2481 return PCI_ERS_RESULT_NONE;
2482
2483 /* Restore the saved state of PCIe device -
2484 * BAR registers, PCI Config space, PCIX, MSI,
2485 * IOV states
2486 */
2487 pci_restore_state(pdev);
2488
2489 /* pci_restore_state() clears the saved_state flag of the device
2490 * save restored state which resets saved_state flag
2491 */
2492 pci_save_state(pdev);
2493
2494 /* Initialize device or resume if in suspended state */
2495 rc = pci_enable_device(pdev);
2496 if (rc) {
2497 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Cant re-enable "
2498 "device after reset\n", ha->host_no, __func__);
2499 goto exit_slot_reset;
2500 }
2501
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002502 ha->isp_ops->disable_intrs(ha);
Lalit Chandivade2232be02010-07-30 14:38:47 +05302503
2504 if (is_qla8022(ha)) {
2505 if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
2506 ret = PCI_ERS_RESULT_RECOVERED;
2507 goto exit_slot_reset;
2508 } else
2509 goto exit_slot_reset;
2510 }
2511
2512exit_slot_reset:
2513 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
2514 "device after reset\n", ha->host_no, __func__, ret);
2515 return ret;
2516}
2517
2518static void
2519qla4xxx_pci_resume(struct pci_dev *pdev)
2520{
2521 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2522 int ret;
2523
2524 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
2525 ha->host_no, __func__);
2526
2527 ret = qla4xxx_wait_for_hba_online(ha);
2528 if (ret != QLA_SUCCESS) {
2529 ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
2530 "resume I/O from slot/link_reset\n", ha->host_no,
2531 __func__);
2532 }
2533
2534 pci_cleanup_aer_uncorrect_error_status(pdev);
2535 clear_bit(AF_EEH_BUSY, &ha->flags);
2536}
2537
2538static struct pci_error_handlers qla4xxx_err_handler = {
2539 .error_detected = qla4xxx_pci_error_detected,
2540 .mmio_enabled = qla4xxx_pci_mmio_enabled,
2541 .slot_reset = qla4xxx_pci_slot_reset,
2542 .resume = qla4xxx_pci_resume,
2543};
2544
David Somayajuluafaf5a22006-09-19 10:28:00 -07002545static struct pci_device_id qla4xxx_pci_tbl[] = {
2546 {
2547 .vendor = PCI_VENDOR_ID_QLOGIC,
2548 .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
2549 .subvendor = PCI_ANY_ID,
2550 .subdevice = PCI_ANY_ID,
2551 },
2552 {
2553 .vendor = PCI_VENDOR_ID_QLOGIC,
2554 .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
2555 .subvendor = PCI_ANY_ID,
2556 .subdevice = PCI_ANY_ID,
2557 },
David C Somayajulud9150582006-11-15 17:38:40 -08002558 {
2559 .vendor = PCI_VENDOR_ID_QLOGIC,
2560 .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
2561 .subvendor = PCI_ANY_ID,
2562 .subdevice = PCI_ANY_ID,
2563 },
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302564 {
2565 .vendor = PCI_VENDOR_ID_QLOGIC,
2566 .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
2567 .subvendor = PCI_ANY_ID,
2568 .subdevice = PCI_ANY_ID,
2569 },
David Somayajuluafaf5a22006-09-19 10:28:00 -07002570 {0, 0},
2571};
2572MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
2573
Adrian Bunk47975472007-04-26 00:35:16 -07002574static struct pci_driver qla4xxx_pci_driver = {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002575 .name = DRIVER_NAME,
2576 .id_table = qla4xxx_pci_tbl,
2577 .probe = qla4xxx_probe_adapter,
2578 .remove = qla4xxx_remove_adapter,
Lalit Chandivade2232be02010-07-30 14:38:47 +05302579 .err_handler = &qla4xxx_err_handler,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002580};
2581
2582static int __init qla4xxx_module_init(void)
2583{
2584 int ret;
2585
2586 /* Allocate cache for SRBs. */
2587 srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09002588 SLAB_HWCACHE_ALIGN, NULL);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002589 if (srb_cachep == NULL) {
2590 printk(KERN_ERR
2591 "%s: Unable to allocate SRB cache..."
2592 "Failing load!\n", DRIVER_NAME);
2593 ret = -ENOMEM;
2594 goto no_srp_cache;
2595 }
2596
2597 /* Derive version string. */
2598 strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
Andrew Vasquez11010fe2006-10-06 09:54:59 -07002599 if (ql4xextended_error_logging)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002600 strcat(qla4xxx_version_str, "-debug");
2601
2602 qla4xxx_scsi_transport =
2603 iscsi_register_transport(&qla4xxx_iscsi_transport);
2604 if (!qla4xxx_scsi_transport){
2605 ret = -ENODEV;
2606 goto release_srb_cache;
2607 }
2608
David Somayajuluafaf5a22006-09-19 10:28:00 -07002609 ret = pci_register_driver(&qla4xxx_pci_driver);
2610 if (ret)
2611 goto unregister_transport;
2612
2613 printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
2614 return 0;
Doug Maxey5ae16db2006-10-05 23:50:07 -05002615
David Somayajuluafaf5a22006-09-19 10:28:00 -07002616unregister_transport:
2617 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
2618release_srb_cache:
2619 kmem_cache_destroy(srb_cachep);
2620no_srp_cache:
2621 return ret;
2622}
2623
2624static void __exit qla4xxx_module_exit(void)
2625{
David Somayajuluafaf5a22006-09-19 10:28:00 -07002626 pci_unregister_driver(&qla4xxx_pci_driver);
2627 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
2628 kmem_cache_destroy(srb_cachep);
2629}
2630
2631module_init(qla4xxx_module_init);
2632module_exit(qla4xxx_module_exit);
2633
2634MODULE_AUTHOR("QLogic Corporation");
2635MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
2636MODULE_LICENSE("GPL");
2637MODULE_VERSION(QLA4XXX_DRIVER_VERSION);