blob: a1ba085565af52c21350612ad14a6952ad4aa2ed [file] [log] [blame]
David Somayajuluafaf5a22006-09-19 10:28:00 -07001/*
2 * QLogic iSCSI HBA Driver
3 * Copyright (c) 2003-2006 QLogic Corporation
4 *
5 * See LICENSE.qla4xxx for copyright and licensing details.
6 */
7#include <linux/moduleparam.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
David Somayajuluafaf5a22006-09-19 10:28:00 -07009
10#include <scsi/scsi_tcq.h>
11#include <scsi/scsicam.h>
12
13#include "ql4_def.h"
David C Somayajulubee4fe82007-05-23 18:03:32 -070014#include "ql4_version.h"
15#include "ql4_glbl.h"
16#include "ql4_dbg.h"
17#include "ql4_inline.h"
David Somayajuluafaf5a22006-09-19 10:28:00 -070018
19/*
20 * Driver version
21 */
Adrian Bunk47975472007-04-26 00:35:16 -070022static char qla4xxx_version_str[40];
David Somayajuluafaf5a22006-09-19 10:28:00 -070023
24/*
25 * SRB allocation cache
26 */
Christoph Lametere18b8902006-12-06 20:33:20 -080027static struct kmem_cache *srb_cachep;
David Somayajuluafaf5a22006-09-19 10:28:00 -070028
29/*
30 * Module parameter information and variables
31 */
32int ql4xdiscoverywait = 60;
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +053033module_param(ql4xdiscoverywait, int, S_IRUGO | S_IWUSR);
David Somayajuluafaf5a22006-09-19 10:28:00 -070034MODULE_PARM_DESC(ql4xdiscoverywait, "Discovery wait time");
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +053035
David Somayajuluafaf5a22006-09-19 10:28:00 -070036int ql4xdontresethba = 0;
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +053037module_param(ql4xdontresethba, int, S_IRUGO | S_IWUSR);
David Somayajuluafaf5a22006-09-19 10:28:00 -070038MODULE_PARM_DESC(ql4xdontresethba,
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +053039 "Don't reset the HBA for driver recovery \n"
40 " 0 - It will reset HBA (Default)\n"
41 " 1 - It will NOT reset HBA");
David Somayajuluafaf5a22006-09-19 10:28:00 -070042
Andrew Vasquez11010fe2006-10-06 09:54:59 -070043int ql4xextended_error_logging = 0; /* 0 = off, 1 = log errors */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +053044module_param(ql4xextended_error_logging, int, S_IRUGO | S_IWUSR);
Andrew Vasquez11010fe2006-10-06 09:54:59 -070045MODULE_PARM_DESC(ql4xextended_error_logging,
David Somayajuluafaf5a22006-09-19 10:28:00 -070046 "Option to enable extended error logging, "
47 "Default is 0 - no logging, 1 - debug logging");
48
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +053049int ql4xenablemsix = 1;
50module_param(ql4xenablemsix, int, S_IRUGO|S_IWUSR);
51MODULE_PARM_DESC(ql4xenablemsix,
52 "Set to enable MSI or MSI-X interrupt mechanism.\n"
53 " 0 = enable INTx interrupt mechanism.\n"
54 " 1 = enable MSI-X interrupt mechanism (Default).\n"
55 " 2 = enable MSI interrupt mechanism.");
David C Somayajulu477ffb92007-01-22 12:26:11 -080056
Mike Christied510d962008-07-11 19:50:33 -050057#define QL4_DEF_QDEPTH 32
58
David Somayajuluafaf5a22006-09-19 10:28:00 -070059/*
60 * SCSI host template entry points
61 */
Adrian Bunk47975472007-04-26 00:35:16 -070062static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -070063
64/*
65 * iSCSI template entry points
66 */
Mike Christie2174a042007-05-30 12:57:10 -050067static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
68 enum iscsi_tgt_dscvr type, uint32_t enable,
69 struct sockaddr *dst_addr);
David Somayajuluafaf5a22006-09-19 10:28:00 -070070static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
71 enum iscsi_param param, char *buf);
72static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
73 enum iscsi_param param, char *buf);
Mike Christieaa1e93a2007-05-30 12:57:09 -050074static int qla4xxx_host_get_param(struct Scsi_Host *shost,
75 enum iscsi_host_param param, char *buf);
David Somayajuluafaf5a22006-09-19 10:28:00 -070076static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session);
Mike Christie5c656af2009-07-15 15:02:59 -050077static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
David Somayajuluafaf5a22006-09-19 10:28:00 -070078
79/*
80 * SCSI host template entry points
81 */
Jeff Garzikf2812332010-11-16 02:10:29 -050082static int qla4xxx_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmd);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +053083static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -070084static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
Mike Christiece545032008-02-29 18:25:20 -060085static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -070086static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
87static int qla4xxx_slave_alloc(struct scsi_device *device);
88static int qla4xxx_slave_configure(struct scsi_device *device);
89static void qla4xxx_slave_destroy(struct scsi_device *sdev);
Mike Christie024f8012008-03-04 13:26:54 -060090static void qla4xxx_scan_start(struct Scsi_Host *shost);
David Somayajuluafaf5a22006-09-19 10:28:00 -070091
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +053092static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
93 QLA82XX_LEGACY_INTR_CONFIG;
94
David Somayajuluafaf5a22006-09-19 10:28:00 -070095static struct scsi_host_template qla4xxx_driver_template = {
96 .module = THIS_MODULE,
97 .name = DRIVER_NAME,
98 .proc_name = DRIVER_NAME,
99 .queuecommand = qla4xxx_queuecommand,
100
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530101 .eh_abort_handler = qla4xxx_eh_abort,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700102 .eh_device_reset_handler = qla4xxx_eh_device_reset,
Mike Christiece545032008-02-29 18:25:20 -0600103 .eh_target_reset_handler = qla4xxx_eh_target_reset,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700104 .eh_host_reset_handler = qla4xxx_eh_host_reset,
Mike Christie5c656af2009-07-15 15:02:59 -0500105 .eh_timed_out = qla4xxx_eh_cmd_timed_out,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700106
107 .slave_configure = qla4xxx_slave_configure,
108 .slave_alloc = qla4xxx_slave_alloc,
109 .slave_destroy = qla4xxx_slave_destroy,
110
Mike Christie921601b2008-01-31 13:36:49 -0600111 .scan_finished = iscsi_scan_finished,
Mike Christie024f8012008-03-04 13:26:54 -0600112 .scan_start = qla4xxx_scan_start,
Mike Christie921601b2008-01-31 13:36:49 -0600113
David Somayajuluafaf5a22006-09-19 10:28:00 -0700114 .this_id = -1,
115 .cmd_per_lun = 3,
116 .use_clustering = ENABLE_CLUSTERING,
117 .sg_tablesize = SG_ALL,
118
119 .max_sectors = 0xFFFF,
120};
121
122static struct iscsi_transport qla4xxx_iscsi_transport = {
123 .owner = THIS_MODULE,
124 .name = DRIVER_NAME,
Mike Christied8196ed2007-05-30 12:57:25 -0500125 .caps = CAP_FW_DB | CAP_SENDTARGETS_OFFLOAD |
126 CAP_DATA_PATH_OFFLOAD,
Mike Christieaa1e93a2007-05-30 12:57:09 -0500127 .param_mask = ISCSI_CONN_PORT | ISCSI_CONN_ADDRESS |
Vikas Chaudhary3b2bef12010-07-10 14:51:30 +0530128 ISCSI_TARGET_NAME | ISCSI_TPGT |
129 ISCSI_TARGET_ALIAS,
Mike Christie8ad57812007-05-30 12:57:13 -0500130 .host_param_mask = ISCSI_HOST_HWADDRESS |
Mike Christie22236962007-05-30 12:57:24 -0500131 ISCSI_HOST_IPADDRESS |
Mike Christie8ad57812007-05-30 12:57:13 -0500132 ISCSI_HOST_INITIATOR_NAME,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700133 .tgt_dscvr = qla4xxx_tgt_dscvr,
134 .get_conn_param = qla4xxx_conn_get_param,
135 .get_session_param = qla4xxx_sess_get_param,
Mike Christieaa1e93a2007-05-30 12:57:09 -0500136 .get_host_param = qla4xxx_host_get_param,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700137 .session_recovery_timedout = qla4xxx_recovery_timedout,
138};
139
140static struct scsi_transport_template *qla4xxx_scsi_transport;
141
Mike Christie5c656af2009-07-15 15:02:59 -0500142static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
143{
144 struct iscsi_cls_session *session;
145 struct ddb_entry *ddb_entry;
146
147 session = starget_to_session(scsi_target(sc->device));
148 ddb_entry = session->dd_data;
149
150 /* if we are not logged in then the LLD is going to clean up the cmd */
151 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
152 return BLK_EH_RESET_TIMER;
153 else
154 return BLK_EH_NOT_HANDLED;
155}
156
David Somayajuluafaf5a22006-09-19 10:28:00 -0700157static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session)
158{
159 struct ddb_entry *ddb_entry = session->dd_data;
160 struct scsi_qla_host *ha = ddb_entry->ha;
161
Mike Christie568d3032008-01-31 13:36:47 -0600162 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
163 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700164
Vikas Chaudhary3013cea2010-07-30 14:25:46 +0530165 DEBUG2(printk("scsi%ld: %s: ddb [%d] session recovery timeout "
Mike Christie568d3032008-01-31 13:36:47 -0600166 "of (%d) secs exhausted, marking device DEAD.\n",
167 ha->host_no, __func__, ddb_entry->fw_ddb_index,
Vikas Chaudhary3013cea2010-07-30 14:25:46 +0530168 QL4_SESS_RECOVERY_TMO));
Mike Christie568d3032008-01-31 13:36:47 -0600169 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700170}
171
Mike Christieaa1e93a2007-05-30 12:57:09 -0500172static int qla4xxx_host_get_param(struct Scsi_Host *shost,
173 enum iscsi_host_param param, char *buf)
174{
175 struct scsi_qla_host *ha = to_qla_host(shost);
176 int len;
177
178 switch (param) {
179 case ISCSI_HOST_PARAM_HWADDRESS:
Michael Chan7ffc49a2007-12-24 21:28:09 -0800180 len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
Mike Christieaa1e93a2007-05-30 12:57:09 -0500181 break;
Mike Christie22236962007-05-30 12:57:24 -0500182 case ISCSI_HOST_PARAM_IPADDRESS:
183 len = sprintf(buf, "%d.%d.%d.%d\n", ha->ip_address[0],
184 ha->ip_address[1], ha->ip_address[2],
185 ha->ip_address[3]);
186 break;
Mike Christie8ad57812007-05-30 12:57:13 -0500187 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie22236962007-05-30 12:57:24 -0500188 len = sprintf(buf, "%s\n", ha->name_string);
Mike Christie8ad57812007-05-30 12:57:13 -0500189 break;
Mike Christieaa1e93a2007-05-30 12:57:09 -0500190 default:
191 return -ENOSYS;
192 }
193
194 return len;
195}
196
David Somayajuluafaf5a22006-09-19 10:28:00 -0700197static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
198 enum iscsi_param param, char *buf)
199{
200 struct ddb_entry *ddb_entry = sess->dd_data;
201 int len;
202
203 switch (param) {
204 case ISCSI_PARAM_TARGET_NAME:
205 len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
206 ddb_entry->iscsi_name);
207 break;
208 case ISCSI_PARAM_TPGT:
209 len = sprintf(buf, "%u\n", ddb_entry->tpgt);
210 break;
Vikas Chaudhary3b2bef12010-07-10 14:51:30 +0530211 case ISCSI_PARAM_TARGET_ALIAS:
212 len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
213 ddb_entry->iscsi_alias);
214 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700215 default:
216 return -ENOSYS;
217 }
218
219 return len;
220}
221
222static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
223 enum iscsi_param param, char *buf)
224{
225 struct iscsi_cls_session *session;
226 struct ddb_entry *ddb_entry;
227 int len;
228
229 session = iscsi_dev_to_session(conn->dev.parent);
230 ddb_entry = session->dd_data;
231
232 switch (param) {
233 case ISCSI_PARAM_CONN_PORT:
234 len = sprintf(buf, "%hu\n", ddb_entry->port);
235 break;
236 case ISCSI_PARAM_CONN_ADDRESS:
237 /* TODO: what are the ipv6 bits */
Harvey Harrison63779432008-10-31 00:56:00 -0700238 len = sprintf(buf, "%pI4\n", &ddb_entry->ip_addr);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700239 break;
240 default:
241 return -ENOSYS;
242 }
243
244 return len;
245}
246
Mike Christie2174a042007-05-30 12:57:10 -0500247static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
248 enum iscsi_tgt_dscvr type, uint32_t enable,
249 struct sockaddr *dst_addr)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700250{
251 struct scsi_qla_host *ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700252 struct sockaddr_in *addr;
253 struct sockaddr_in6 *addr6;
254 int ret = 0;
255
David Somayajuluafaf5a22006-09-19 10:28:00 -0700256 ha = (struct scsi_qla_host *) shost->hostdata;
257
258 switch (type) {
259 case ISCSI_TGT_DSCVR_SEND_TARGETS:
260 if (dst_addr->sa_family == AF_INET) {
261 addr = (struct sockaddr_in *)dst_addr;
262 if (qla4xxx_send_tgts(ha, (char *)&addr->sin_addr,
263 addr->sin_port) != QLA_SUCCESS)
264 ret = -EIO;
265 } else if (dst_addr->sa_family == AF_INET6) {
266 /*
267 * TODO: fix qla4xxx_send_tgts
268 */
269 addr6 = (struct sockaddr_in6 *)dst_addr;
270 if (qla4xxx_send_tgts(ha, (char *)&addr6->sin6_addr,
271 addr6->sin6_port) != QLA_SUCCESS)
272 ret = -EIO;
273 } else
274 ret = -ENOSYS;
275 break;
276 default:
277 ret = -ENOSYS;
278 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700279 return ret;
280}
281
282void qla4xxx_destroy_sess(struct ddb_entry *ddb_entry)
283{
284 if (!ddb_entry->sess)
285 return;
286
287 if (ddb_entry->conn) {
Mike Christie26974782007-12-13 12:43:29 -0600288 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700289 iscsi_remove_session(ddb_entry->sess);
290 }
291 iscsi_free_session(ddb_entry->sess);
292}
293
294int qla4xxx_add_sess(struct ddb_entry *ddb_entry)
295{
296 int err;
297
Vikas Chaudhary3013cea2010-07-30 14:25:46 +0530298 ddb_entry->sess->recovery_tmo = QL4_SESS_RECOVERY_TMO;
299
David Somayajuluafaf5a22006-09-19 10:28:00 -0700300 err = iscsi_add_session(ddb_entry->sess, ddb_entry->fw_ddb_index);
301 if (err) {
302 DEBUG2(printk(KERN_ERR "Could not add session.\n"));
303 return err;
304 }
305
Mike Christie5d91e202008-05-21 15:54:01 -0500306 ddb_entry->conn = iscsi_create_conn(ddb_entry->sess, 0, 0);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700307 if (!ddb_entry->conn) {
308 iscsi_remove_session(ddb_entry->sess);
309 DEBUG2(printk(KERN_ERR "Could not add connection.\n"));
310 return -ENOMEM;
311 }
Mike Christieb6359302008-01-31 13:36:44 -0600312
313 /* finally ready to go */
314 iscsi_unblock_session(ddb_entry->sess);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700315 return 0;
316}
317
318struct ddb_entry *qla4xxx_alloc_sess(struct scsi_qla_host *ha)
319{
320 struct ddb_entry *ddb_entry;
321 struct iscsi_cls_session *sess;
322
Mike Christie5d91e202008-05-21 15:54:01 -0500323 sess = iscsi_alloc_session(ha->host, &qla4xxx_iscsi_transport,
324 sizeof(struct ddb_entry));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700325 if (!sess)
326 return NULL;
327
328 ddb_entry = sess->dd_data;
329 memset(ddb_entry, 0, sizeof(*ddb_entry));
330 ddb_entry->ha = ha;
331 ddb_entry->sess = sess;
332 return ddb_entry;
333}
334
Mike Christie024f8012008-03-04 13:26:54 -0600335static void qla4xxx_scan_start(struct Scsi_Host *shost)
336{
337 struct scsi_qla_host *ha = shost_priv(shost);
338 struct ddb_entry *ddb_entry, *ddbtemp;
339
340 /* finish setup of sessions that were already setup in firmware */
341 list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
342 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE)
343 qla4xxx_add_sess(ddb_entry);
344 }
345}
346
David Somayajuluafaf5a22006-09-19 10:28:00 -0700347/*
348 * Timer routines
349 */
350
351static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
352 unsigned long interval)
353{
354 DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
355 __func__, ha->host->host_no));
356 init_timer(&ha->timer);
357 ha->timer.expires = jiffies + interval * HZ;
358 ha->timer.data = (unsigned long)ha;
359 ha->timer.function = (void (*)(unsigned long))func;
360 add_timer(&ha->timer);
361 ha->timer_active = 1;
362}
363
364static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
365{
366 del_timer_sync(&ha->timer);
367 ha->timer_active = 0;
368}
369
370/***
371 * qla4xxx_mark_device_missing - mark a device as missing.
372 * @ha: Pointer to host adapter structure.
373 * @ddb_entry: Pointer to device database entry
374 *
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530375 * This routine marks a device missing and close connection.
David Somayajuluafaf5a22006-09-19 10:28:00 -0700376 **/
377void qla4xxx_mark_device_missing(struct scsi_qla_host *ha,
378 struct ddb_entry *ddb_entry)
379{
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530380 if ((atomic_read(&ddb_entry->state) != DDB_STATE_DEAD)) {
381 atomic_set(&ddb_entry->state, DDB_STATE_MISSING);
382 DEBUG2(printk("scsi%ld: ddb [%d] marked MISSING\n",
383 ha->host_no, ddb_entry->fw_ddb_index));
384 } else
385 DEBUG2(printk("scsi%ld: ddb [%d] DEAD\n", ha->host_no,
386 ddb_entry->fw_ddb_index))
387
Mike Christieb6359302008-01-31 13:36:44 -0600388 iscsi_block_session(ddb_entry->sess);
Mike Christiee5bd7b542008-09-24 11:46:10 -0500389 iscsi_conn_error_event(ddb_entry->conn, ISCSI_ERR_CONN_FAILED);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700390}
391
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530392/**
393 * qla4xxx_mark_all_devices_missing - mark all devices as missing.
394 * @ha: Pointer to host adapter structure.
395 *
396 * This routine marks a device missing and resets the relogin retry count.
397 **/
398void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
399{
400 struct ddb_entry *ddb_entry, *ddbtemp;
401 list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
402 qla4xxx_mark_device_missing(ha, ddb_entry);
403 }
404}
405
David Somayajuluafaf5a22006-09-19 10:28:00 -0700406static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
407 struct ddb_entry *ddb_entry,
408 struct scsi_cmnd *cmd,
409 void (*done)(struct scsi_cmnd *))
410{
411 struct srb *srb;
412
413 srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
414 if (!srb)
415 return srb;
416
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530417 kref_init(&srb->srb_ref);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700418 srb->ha = ha;
419 srb->ddb = ddb_entry;
420 srb->cmd = cmd;
421 srb->flags = 0;
Vikas Chaudhary53698872010-04-28 11:41:59 +0530422 CMD_SP(cmd) = (void *)srb;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700423 cmd->scsi_done = done;
424
425 return srb;
426}
427
428static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
429{
430 struct scsi_cmnd *cmd = srb->cmd;
431
432 if (srb->flags & SRB_DMA_VALID) {
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +0900433 scsi_dma_unmap(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700434 srb->flags &= ~SRB_DMA_VALID;
435 }
Vikas Chaudhary53698872010-04-28 11:41:59 +0530436 CMD_SP(cmd) = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700437}
438
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530439void qla4xxx_srb_compl(struct kref *ref)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700440{
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530441 struct srb *srb = container_of(ref, struct srb, srb_ref);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700442 struct scsi_cmnd *cmd = srb->cmd;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530443 struct scsi_qla_host *ha = srb->ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700444
445 qla4xxx_srb_free_dma(ha, srb);
446
447 mempool_free(srb, ha->srb_mempool);
448
449 cmd->scsi_done(cmd);
450}
451
452/**
453 * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
454 * @cmd: Pointer to Linux's SCSI command structure
455 * @done_fn: Function that the driver calls to notify the SCSI mid-layer
456 * that the command has been processed.
457 *
458 * Remarks:
459 * This routine is invoked by Linux to send a SCSI command to the driver.
460 * The mid-level driver tries to ensure that queuecommand never gets
461 * invoked concurrently with itself or the interrupt handler (although
462 * the interrupt handler may call this routine as part of request-
463 * completion handling). Unfortunely, it sometimes calls the scheduler
464 * in interrupt context which is a big NO! NO!.
465 **/
Jeff Garzikf2812332010-11-16 02:10:29 -0500466static int qla4xxx_queuecommand_lck(struct scsi_cmnd *cmd,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700467 void (*done)(struct scsi_cmnd *))
468{
469 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
470 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christie7fb19212008-01-31 13:36:45 -0600471 struct iscsi_cls_session *sess = ddb_entry->sess;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700472 struct srb *srb;
473 int rval;
474
Lalit Chandivade2232be02010-07-30 14:38:47 +0530475 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
476 if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
477 cmd->result = DID_NO_CONNECT << 16;
478 else
479 cmd->result = DID_REQUEUE << 16;
480 goto qc_fail_command;
481 }
482
Mike Christie7fb19212008-01-31 13:36:45 -0600483 if (!sess) {
484 cmd->result = DID_IMM_RETRY << 16;
485 goto qc_fail_command;
486 }
487
488 rval = iscsi_session_chkready(sess);
489 if (rval) {
490 cmd->result = rval;
491 goto qc_fail_command;
492 }
493
David Somayajuluafaf5a22006-09-19 10:28:00 -0700494 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
495 if (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD) {
496 cmd->result = DID_NO_CONNECT << 16;
497 goto qc_fail_command;
498 }
Mike Christiec5e98e92008-08-17 15:24:39 -0500499 return SCSI_MLQUEUE_TARGET_BUSY;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700500 }
501
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530502 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
503 test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
504 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
505 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
506 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
507 !test_bit(AF_ONLINE, &ha->flags) ||
508 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
David C Somayajulu477ffb92007-01-22 12:26:11 -0800509 goto qc_host_busy;
510
David Somayajuluafaf5a22006-09-19 10:28:00 -0700511 spin_unlock_irq(ha->host->host_lock);
512
513 srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd, done);
514 if (!srb)
515 goto qc_host_busy_lock;
516
517 rval = qla4xxx_send_command_to_isp(ha, srb);
518 if (rval != QLA_SUCCESS)
519 goto qc_host_busy_free_sp;
520
521 spin_lock_irq(ha->host->host_lock);
522 return 0;
523
524qc_host_busy_free_sp:
525 qla4xxx_srb_free_dma(ha, srb);
526 mempool_free(srb, ha->srb_mempool);
527
528qc_host_busy_lock:
529 spin_lock_irq(ha->host->host_lock);
530
531qc_host_busy:
532 return SCSI_MLQUEUE_HOST_BUSY;
533
534qc_fail_command:
535 done(cmd);
536
537 return 0;
538}
539
Jeff Garzikf2812332010-11-16 02:10:29 -0500540static DEF_SCSI_QCMD(qla4xxx_queuecommand)
541
David Somayajuluafaf5a22006-09-19 10:28:00 -0700542/**
543 * qla4xxx_mem_free - frees memory allocated to adapter
544 * @ha: Pointer to host adapter structure.
545 *
546 * Frees memory previously allocated by qla4xxx_mem_alloc
547 **/
548static void qla4xxx_mem_free(struct scsi_qla_host *ha)
549{
550 if (ha->queues)
551 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
552 ha->queues_dma);
553
554 ha->queues_len = 0;
555 ha->queues = NULL;
556 ha->queues_dma = 0;
557 ha->request_ring = NULL;
558 ha->request_dma = 0;
559 ha->response_ring = NULL;
560 ha->response_dma = 0;
561 ha->shadow_regs = NULL;
562 ha->shadow_regs_dma = 0;
563
564 /* Free srb pool. */
565 if (ha->srb_mempool)
566 mempool_destroy(ha->srb_mempool);
567
568 ha->srb_mempool = NULL;
569
570 /* release io space registers */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530571 if (is_qla8022(ha)) {
572 if (ha->nx_pcibase)
573 iounmap(
574 (struct device_reg_82xx __iomem *)ha->nx_pcibase);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530575 } else if (ha->reg)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700576 iounmap(ha->reg);
577 pci_release_regions(ha->pdev);
578}
579
580/**
581 * qla4xxx_mem_alloc - allocates memory for use by adapter.
582 * @ha: Pointer to host adapter structure
583 *
584 * Allocates DMA memory for request and response queues. Also allocates memory
585 * for srbs.
586 **/
587static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
588{
589 unsigned long align;
590
591 /* Allocate contiguous block of DMA memory for queues. */
592 ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
593 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
594 sizeof(struct shadow_regs) +
595 MEM_ALIGN_VALUE +
596 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
597 ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
598 &ha->queues_dma, GFP_KERNEL);
599 if (ha->queues == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530600 ql4_printk(KERN_WARNING, ha,
601 "Memory Allocation failed - queues.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700602
603 goto mem_alloc_error_exit;
604 }
605 memset(ha->queues, 0, ha->queues_len);
606
607 /*
608 * As per RISC alignment requirements -- the bus-address must be a
609 * multiple of the request-ring size (in bytes).
610 */
611 align = 0;
612 if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
613 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
614 (MEM_ALIGN_VALUE - 1));
615
616 /* Update request and response queue pointers. */
617 ha->request_dma = ha->queues_dma + align;
618 ha->request_ring = (struct queue_entry *) (ha->queues + align);
619 ha->response_dma = ha->queues_dma + align +
620 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
621 ha->response_ring = (struct queue_entry *) (ha->queues + align +
622 (REQUEST_QUEUE_DEPTH *
623 QUEUE_SIZE));
624 ha->shadow_regs_dma = ha->queues_dma + align +
625 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
626 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
627 ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
628 (REQUEST_QUEUE_DEPTH *
629 QUEUE_SIZE) +
630 (RESPONSE_QUEUE_DEPTH *
631 QUEUE_SIZE));
632
633 /* Allocate memory for srb pool. */
634 ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
635 mempool_free_slab, srb_cachep);
636 if (ha->srb_mempool == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530637 ql4_printk(KERN_WARNING, ha,
638 "Memory Allocation failed - SRB Pool.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700639
640 goto mem_alloc_error_exit;
641 }
642
643 return QLA_SUCCESS;
644
645mem_alloc_error_exit:
646 qla4xxx_mem_free(ha);
647 return QLA_ERROR;
648}
649
650/**
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530651 * qla4_8xxx_check_fw_alive - Check firmware health
652 * @ha: Pointer to host adapter structure.
653 *
654 * Context: Interrupt
655 **/
656static void qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
657{
658 uint32_t fw_heartbeat_counter, halt_status;
659
660 fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
Lalit Chandivade2232be02010-07-30 14:38:47 +0530661 /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
662 if (fw_heartbeat_counter == 0xffffffff) {
663 DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
664 "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
665 ha->host_no, __func__));
666 return;
667 }
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530668
669 if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
670 ha->seconds_since_last_heartbeat++;
671 /* FW not alive after 2 seconds */
672 if (ha->seconds_since_last_heartbeat == 2) {
673 ha->seconds_since_last_heartbeat = 0;
674 halt_status = qla4_8xxx_rd_32(ha,
675 QLA82XX_PEG_HALT_STATUS1);
Nilesh Javali21033632010-07-30 14:28:07 +0530676
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530677 /* Since we cannot change dev_state in interrupt
678 * context, set appropriate DPC flag then wakeup
679 * DPC */
680 if (halt_status & HALT_STATUS_UNRECOVERABLE)
681 set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
682 else {
683 printk("scsi%ld: %s: detect abort needed!\n",
684 ha->host_no, __func__);
685 set_bit(DPC_RESET_HA, &ha->dpc_flags);
686 }
687 qla4xxx_wake_dpc(ha);
Nilesh Javali21033632010-07-30 14:28:07 +0530688 qla4xxx_mailbox_premature_completion(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530689 }
Lalit Chandivade99457d72010-10-06 22:49:32 -0700690 } else
691 ha->seconds_since_last_heartbeat = 0;
692
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530693 ha->fw_heartbeat_counter = fw_heartbeat_counter;
694}
695
696/**
697 * qla4_8xxx_watchdog - Poll dev state
698 * @ha: Pointer to host adapter structure.
699 *
700 * Context: Interrupt
701 **/
702void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
703{
704 uint32_t dev_state;
705
706 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
707
708 /* don't poll if reset is going on */
Lalit Chandivaded56a1f72010-12-02 22:12:45 -0800709 if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
710 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
711 test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags))) {
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530712 if (dev_state == QLA82XX_DEV_NEED_RESET &&
713 !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
714 printk("scsi%ld: %s: HW State: NEED RESET!\n",
715 ha->host_no, __func__);
716 set_bit(DPC_RESET_HA, &ha->dpc_flags);
717 qla4xxx_wake_dpc(ha);
Nilesh Javali21033632010-07-30 14:28:07 +0530718 qla4xxx_mailbox_premature_completion(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530719 } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
720 !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
721 printk("scsi%ld: %s: HW State: NEED QUIES!\n",
722 ha->host_no, __func__);
723 set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
724 qla4xxx_wake_dpc(ha);
725 } else {
726 /* Check firmware health */
727 qla4_8xxx_check_fw_alive(ha);
728 }
729 }
730}
731
732/**
David Somayajuluafaf5a22006-09-19 10:28:00 -0700733 * qla4xxx_timer - checks every second for work to do.
734 * @ha: Pointer to host adapter structure.
735 **/
736static void qla4xxx_timer(struct scsi_qla_host *ha)
737{
738 struct ddb_entry *ddb_entry, *dtemp;
739 int start_dpc = 0;
Lalit Chandivade2232be02010-07-30 14:38:47 +0530740 uint16_t w;
741
742 /* If we are in the middle of AER/EEH processing
743 * skip any processing and reschedule the timer
744 */
745 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
746 mod_timer(&ha->timer, jiffies + HZ);
747 return;
748 }
749
750 /* Hardware read to trigger an EEH error during mailbox waits. */
751 if (!pci_channel_offline(ha->pdev))
752 pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700753
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530754 if (test_bit(AF_HBA_GOING_AWAY, &ha->flags)) {
755 DEBUG2(ql4_printk(KERN_INFO, ha, "%s exited. HBA GOING AWAY\n",
756 __func__));
757 return;
758 }
759
760 if (is_qla8022(ha)) {
761 qla4_8xxx_watchdog(ha);
762 }
763
David Somayajuluafaf5a22006-09-19 10:28:00 -0700764 /* Search for relogin's to time-out and port down retry. */
765 list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
766 /* Count down time between sending relogins */
767 if (adapter_up(ha) &&
768 !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
769 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
770 if (atomic_read(&ddb_entry->retry_relogin_timer) !=
771 INVALID_ENTRY) {
772 if (atomic_read(&ddb_entry->retry_relogin_timer)
773 == 0) {
774 atomic_set(&ddb_entry->
775 retry_relogin_timer,
776 INVALID_ENTRY);
777 set_bit(DPC_RELOGIN_DEVICE,
778 &ha->dpc_flags);
779 set_bit(DF_RELOGIN, &ddb_entry->flags);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530780 DEBUG2(printk("scsi%ld: %s: ddb [%d]"
David Somayajuluafaf5a22006-09-19 10:28:00 -0700781 " login device\n",
782 ha->host_no, __func__,
783 ddb_entry->fw_ddb_index));
784 } else
785 atomic_dec(&ddb_entry->
786 retry_relogin_timer);
787 }
788 }
789
790 /* Wait for relogin to timeout */
791 if (atomic_read(&ddb_entry->relogin_timer) &&
792 (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
793 /*
794 * If the relogin times out and the device is
795 * still NOT ONLINE then try and relogin again.
796 */
797 if (atomic_read(&ddb_entry->state) !=
798 DDB_STATE_ONLINE &&
799 ddb_entry->fw_ddb_device_state ==
800 DDB_DS_SESSION_FAILED) {
801 /* Reset retry relogin timer */
802 atomic_inc(&ddb_entry->relogin_retry_count);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530803 DEBUG2(printk("scsi%ld: ddb [%d] relogin"
David Somayajuluafaf5a22006-09-19 10:28:00 -0700804 " timed out-retrying"
805 " relogin (%d)\n",
806 ha->host_no,
807 ddb_entry->fw_ddb_index,
808 atomic_read(&ddb_entry->
809 relogin_retry_count))
810 );
811 start_dpc++;
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530812 DEBUG(printk("scsi%ld:%d:%d: ddb [%d] "
David Somayajuluafaf5a22006-09-19 10:28:00 -0700813 "initate relogin after"
814 " %d seconds\n",
815 ha->host_no, ddb_entry->bus,
816 ddb_entry->target,
817 ddb_entry->fw_ddb_index,
818 ddb_entry->default_time2wait + 4)
819 );
820
821 atomic_set(&ddb_entry->retry_relogin_timer,
822 ddb_entry->default_time2wait + 4);
823 }
824 }
825 }
826
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530827 if (!is_qla8022(ha)) {
828 /* Check for heartbeat interval. */
829 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
830 ha->heartbeat_interval != 0) {
831 ha->seconds_since_last_heartbeat++;
832 if (ha->seconds_since_last_heartbeat >
833 ha->heartbeat_interval + 2)
834 set_bit(DPC_RESET_HA, &ha->dpc_flags);
835 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700836 }
837
David Somayajuluafaf5a22006-09-19 10:28:00 -0700838 /* Wakeup the dpc routine for this adapter, if needed. */
839 if ((start_dpc ||
840 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
841 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
842 test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530843 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -0700844 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
845 test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
Vikas Chaudhary065aa1b42010-04-28 11:38:11 +0530846 test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530847 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
848 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -0700849 test_bit(DPC_AEN, &ha->dpc_flags)) &&
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530850 !test_bit(AF_DPC_SCHEDULED, &ha->flags) &&
David Somayajuluafaf5a22006-09-19 10:28:00 -0700851 ha->dpc_thread) {
852 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
853 " - dpc flags = 0x%lx\n",
854 ha->host_no, __func__, ha->dpc_flags));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530855 qla4xxx_wake_dpc(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700856 }
857
858 /* Reschedule timer thread to call us back in one second */
859 mod_timer(&ha->timer, jiffies + HZ);
860
861 DEBUG2(ha->seconds_since_last_intr++);
862}
863
864/**
865 * qla4xxx_cmd_wait - waits for all outstanding commands to complete
866 * @ha: Pointer to host adapter structure.
867 *
868 * This routine stalls the driver until all outstanding commands are returned.
869 * Caller must release the Hardware Lock prior to calling this routine.
870 **/
871static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
872{
873 uint32_t index = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700874 unsigned long flags;
875 struct scsi_cmnd *cmd;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700876
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530877 unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
878
879 DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
880 "complete\n", WAIT_CMD_TOV));
881
882 while (!time_after_eq(jiffies, wtime)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700883 spin_lock_irqsave(&ha->hardware_lock, flags);
884 /* Find a command that hasn't completed. */
885 for (index = 0; index < ha->host->can_queue; index++) {
886 cmd = scsi_host_find_tag(ha->host, index);
Mike Christiea1e00632010-10-26 05:45:30 -0700887 /*
888 * We cannot just check if the index is valid,
889 * becase if we are run from the scsi eh, then
890 * the scsi/block layer is going to prevent
891 * the tag from being released.
892 */
893 if (cmd != NULL && CMD_SP(cmd))
David Somayajuluafaf5a22006-09-19 10:28:00 -0700894 break;
895 }
896 spin_unlock_irqrestore(&ha->hardware_lock, flags);
897
898 /* If No Commands are pending, wait is complete */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530899 if (index == ha->host->can_queue)
900 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700901
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530902 msleep(1000);
903 }
904 /* If we timed out on waiting for commands to come back
905 * return ERROR. */
906 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700907}
908
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530909int qla4xxx_hw_reset(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700910{
David Somayajuluafaf5a22006-09-19 10:28:00 -0700911 uint32_t ctrl_status;
David C Somayajulu477ffb92007-01-22 12:26:11 -0800912 unsigned long flags = 0;
913
914 DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700915
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530916 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
917 return QLA_ERROR;
918
David Somayajuluafaf5a22006-09-19 10:28:00 -0700919 spin_lock_irqsave(&ha->hardware_lock, flags);
920
921 /*
922 * If the SCSI Reset Interrupt bit is set, clear it.
923 * Otherwise, the Soft Reset won't work.
924 */
925 ctrl_status = readw(&ha->reg->ctrl_status);
926 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
927 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
928
929 /* Issue Soft Reset */
930 writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
931 readl(&ha->reg->ctrl_status);
932
933 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530934 return QLA_SUCCESS;
David C Somayajulu477ffb92007-01-22 12:26:11 -0800935}
936
937/**
938 * qla4xxx_soft_reset - performs soft reset.
939 * @ha: Pointer to host adapter structure.
940 **/
941int qla4xxx_soft_reset(struct scsi_qla_host *ha)
942{
943 uint32_t max_wait_time;
944 unsigned long flags = 0;
Vikas Chaudharyf931c532010-10-06 22:48:07 -0700945 int status;
David C Somayajulu477ffb92007-01-22 12:26:11 -0800946 uint32_t ctrl_status;
947
Vikas Chaudharyf931c532010-10-06 22:48:07 -0700948 status = qla4xxx_hw_reset(ha);
949 if (status != QLA_SUCCESS)
950 return status;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700951
Vikas Chaudharyf931c532010-10-06 22:48:07 -0700952 status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700953 /* Wait until the Network Reset Intr bit is cleared */
954 max_wait_time = RESET_INTR_TOV;
955 do {
956 spin_lock_irqsave(&ha->hardware_lock, flags);
957 ctrl_status = readw(&ha->reg->ctrl_status);
958 spin_unlock_irqrestore(&ha->hardware_lock, flags);
959
960 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
961 break;
962
963 msleep(1000);
964 } while ((--max_wait_time));
965
966 if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
967 DEBUG2(printk(KERN_WARNING
968 "scsi%ld: Network Reset Intr not cleared by "
969 "Network function, clearing it now!\n",
970 ha->host_no));
971 spin_lock_irqsave(&ha->hardware_lock, flags);
972 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
973 readl(&ha->reg->ctrl_status);
974 spin_unlock_irqrestore(&ha->hardware_lock, flags);
975 }
976
977 /* Wait until the firmware tells us the Soft Reset is done */
978 max_wait_time = SOFT_RESET_TOV;
979 do {
980 spin_lock_irqsave(&ha->hardware_lock, flags);
981 ctrl_status = readw(&ha->reg->ctrl_status);
982 spin_unlock_irqrestore(&ha->hardware_lock, flags);
983
984 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
985 status = QLA_SUCCESS;
986 break;
987 }
988
989 msleep(1000);
990 } while ((--max_wait_time));
991
992 /*
993 * Also, make sure that the SCSI Reset Interrupt bit has been cleared
994 * after the soft reset has taken place.
995 */
996 spin_lock_irqsave(&ha->hardware_lock, flags);
997 ctrl_status = readw(&ha->reg->ctrl_status);
998 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
999 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
1000 readl(&ha->reg->ctrl_status);
1001 }
1002 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1003
1004 /* If soft reset fails then most probably the bios on other
1005 * function is also enabled.
1006 * Since the initialization is sequential the other fn
1007 * wont be able to acknowledge the soft reset.
1008 * Issue a force soft reset to workaround this scenario.
1009 */
1010 if (max_wait_time == 0) {
1011 /* Issue Force Soft Reset */
1012 spin_lock_irqsave(&ha->hardware_lock, flags);
1013 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
1014 readl(&ha->reg->ctrl_status);
1015 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1016 /* Wait until the firmware tells us the Soft Reset is done */
1017 max_wait_time = SOFT_RESET_TOV;
1018 do {
1019 spin_lock_irqsave(&ha->hardware_lock, flags);
1020 ctrl_status = readw(&ha->reg->ctrl_status);
1021 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1022
1023 if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
1024 status = QLA_SUCCESS;
1025 break;
1026 }
1027
1028 msleep(1000);
1029 } while ((--max_wait_time));
1030 }
1031
1032 return status;
1033}
1034
1035/**
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301036 * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001037 * @ha: Pointer to host adapter structure.
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301038 * @res: returned scsi status
David Somayajuluafaf5a22006-09-19 10:28:00 -07001039 *
1040 * This routine is called just prior to a HARD RESET to return all
1041 * outstanding commands back to the Operating System.
1042 * Caller should make sure that the following locks are released
1043 * before this calling routine: Hardware lock, and io_request_lock.
1044 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301045static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001046{
1047 struct srb *srb;
1048 int i;
1049 unsigned long flags;
1050
1051 spin_lock_irqsave(&ha->hardware_lock, flags);
1052 for (i = 0; i < ha->host->can_queue; i++) {
1053 srb = qla4xxx_del_from_active_array(ha, i);
1054 if (srb != NULL) {
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301055 srb->cmd->result = res;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301056 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001057 }
1058 }
1059 spin_unlock_irqrestore(&ha->hardware_lock, flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001060}
1061
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301062void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
1063{
1064 clear_bit(AF_ONLINE, &ha->flags);
1065
1066 /* Disable the board */
1067 ql4_printk(KERN_INFO, ha, "Disabling the board\n");
1068 set_bit(AF_HBA_GOING_AWAY, &ha->flags);
1069
1070 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
1071 qla4xxx_mark_all_devices_missing(ha);
1072 clear_bit(AF_INIT_DONE, &ha->flags);
1073}
1074
David Somayajuluafaf5a22006-09-19 10:28:00 -07001075/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07001076 * qla4xxx_recover_adapter - recovers adapter after a fatal error
1077 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001078 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301079static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001080{
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301081 int status = QLA_ERROR;
1082 uint8_t reset_chip = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001083
1084 /* Stall incoming I/O until we are done */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301085 scsi_block_requests(ha->host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001086 clear_bit(AF_ONLINE, &ha->flags);
Mike Christie50a29ae2008-03-04 13:26:53 -06001087
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301088 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001089
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301090 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001091
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301092 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
1093 reset_chip = 1;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001094
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301095 /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
1096 * do not reset adapter, jump to initialize_adapter */
1097 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1098 status = QLA_SUCCESS;
1099 goto recover_ha_init_adapter;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001100 }
1101
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301102 /* For the ISP-82xx adapter, issue a stop_firmware if invoked
1103 * from eh_host_reset or ioctl module */
1104 if (is_qla8022(ha) && !reset_chip &&
1105 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
1106
1107 DEBUG2(ql4_printk(KERN_INFO, ha,
1108 "scsi%ld: %s - Performing stop_firmware...\n",
1109 ha->host_no, __func__));
1110 status = ha->isp_ops->reset_firmware(ha);
1111 if (status == QLA_SUCCESS) {
Nilesh Javali2bd1e2b2010-10-06 22:49:20 -07001112 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
1113 qla4xxx_cmd_wait(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301114 ha->isp_ops->disable_intrs(ha);
1115 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1116 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1117 } else {
1118 /* If the stop_firmware fails then
1119 * reset the entire chip */
1120 reset_chip = 1;
1121 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1122 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1123 }
1124 }
1125
1126 /* Issue full chip reset if recovering from a catastrophic error,
1127 * or if stop_firmware fails for ISP-82xx.
1128 * This is the default case for ISP-4xxx */
1129 if (!is_qla8022(ha) || reset_chip) {
Nilesh Javali2bd1e2b2010-10-06 22:49:20 -07001130 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
1131 qla4xxx_cmd_wait(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301132 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1133 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1134 DEBUG2(ql4_printk(KERN_INFO, ha,
1135 "scsi%ld: %s - Performing chip reset..\n",
1136 ha->host_no, __func__));
1137 status = ha->isp_ops->reset_chip(ha);
1138 }
1139
1140 /* Flush any pending ddb changed AENs */
1141 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1142
1143recover_ha_init_adapter:
1144 /* Upon successful firmware/chip reset, re-initialize the adapter */
1145 if (status == QLA_SUCCESS) {
1146 /* For ISP-4xxx, force function 1 to always initialize
1147 * before function 3 to prevent both funcions from
1148 * stepping on top of the other */
1149 if (!is_qla8022(ha) && (ha->mac_index == 3))
1150 ssleep(6);
1151
1152 /* NOTE: AF_ONLINE flag set upon successful completion of
1153 * qla4xxx_initialize_adapter */
1154 status = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
1155 }
1156
1157 /* Retry failed adapter initialization, if necessary
1158 * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
1159 * case to prevent ping-pong resets between functions */
1160 if (!test_bit(AF_ONLINE, &ha->flags) &&
1161 !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001162 /* Adapter initialization failed, see if we can retry
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301163 * resetting the ha.
1164 * Since we don't want to block the DPC for too long
1165 * with multiple resets in the same thread,
1166 * utilize DPC to retry */
David Somayajuluafaf5a22006-09-19 10:28:00 -07001167 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
1168 ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
1169 DEBUG2(printk("scsi%ld: recover adapter - retrying "
1170 "(%d) more times\n", ha->host_no,
1171 ha->retry_reset_ha_cnt));
1172 set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1173 status = QLA_ERROR;
1174 } else {
1175 if (ha->retry_reset_ha_cnt > 0) {
1176 /* Schedule another Reset HA--DPC will retry */
1177 ha->retry_reset_ha_cnt--;
1178 DEBUG2(printk("scsi%ld: recover adapter - "
1179 "retry remaining %d\n",
1180 ha->host_no,
1181 ha->retry_reset_ha_cnt));
1182 status = QLA_ERROR;
1183 }
1184
1185 if (ha->retry_reset_ha_cnt == 0) {
1186 /* Recover adapter retries have been exhausted.
1187 * Adapter DEAD */
1188 DEBUG2(printk("scsi%ld: recover adapter "
1189 "failed - board disabled\n",
1190 ha->host_no));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301191 qla4xxx_dead_adapter_cleanup(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001192 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1193 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301194 clear_bit(DPC_RESET_HA_FW_CONTEXT,
David Somayajuluafaf5a22006-09-19 10:28:00 -07001195 &ha->dpc_flags);
1196 status = QLA_ERROR;
1197 }
1198 }
1199 } else {
1200 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301201 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001202 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1203 }
1204
1205 ha->adapter_error_count++;
1206
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301207 if (test_bit(AF_ONLINE, &ha->flags))
1208 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001209
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301210 scsi_unblock_requests(ha->host);
1211
1212 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
1213 DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
1214 status == QLA_ERROR ? "FAILED" : "SUCCEDED"));
1215
David Somayajuluafaf5a22006-09-19 10:28:00 -07001216 return status;
1217}
1218
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301219void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
1220{
1221 if (ha->dpc_thread &&
1222 !test_bit(AF_DPC_SCHEDULED, &ha->flags)) {
1223 set_bit(AF_DPC_SCHEDULED, &ha->flags);
1224 queue_work(ha->dpc_thread, &ha->dpc_work);
1225 }
1226}
1227
David Somayajuluafaf5a22006-09-19 10:28:00 -07001228/**
1229 * qla4xxx_do_dpc - dpc routine
1230 * @data: in our case pointer to adapter structure
1231 *
1232 * This routine is a task that is schedule by the interrupt handler
1233 * to perform the background processing for interrupts. We put it
1234 * on a task queue that is consumed whenever the scheduler runs; that's
1235 * so you can do anything (i.e. put the process to sleep etc). In fact,
1236 * the mid-level tries to sleep when it reaches the driver threshold
1237 * "host->can_queue". This can cause a panic if we were in our interrupt code.
1238 **/
David Howellsc4028952006-11-22 14:57:56 +00001239static void qla4xxx_do_dpc(struct work_struct *work)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001240{
David Howellsc4028952006-11-22 14:57:56 +00001241 struct scsi_qla_host *ha =
1242 container_of(work, struct scsi_qla_host, dpc_work);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001243 struct ddb_entry *ddb_entry, *dtemp;
David C Somayajulu477ffb92007-01-22 12:26:11 -08001244 int status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001245
David C Somayajuluf26b9042006-11-15 16:41:09 -08001246 DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301247 "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
1248 ha->host_no, __func__, ha->flags, ha->dpc_flags))
David Somayajuluafaf5a22006-09-19 10:28:00 -07001249
1250 /* Initialization not yet finished. Don't do anything yet. */
1251 if (!test_bit(AF_INIT_DONE, &ha->flags))
Vikas Chaudhary0753b482010-07-30 14:27:19 +05301252 goto do_dpc_exit;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001253
Lalit Chandivade2232be02010-07-30 14:38:47 +05301254 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1255 DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
1256 ha->host_no, __func__, ha->flags));
1257 goto do_dpc_exit;
1258 }
1259
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301260 /* HBA is in the process of being permanently disabled.
1261 * Don't process anything */
1262 if (test_bit(AF_HBA_GOING_AWAY, &ha->flags))
1263 return;
1264
1265 if (is_qla8022(ha)) {
1266 if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
1267 qla4_8xxx_idc_lock(ha);
1268 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
1269 QLA82XX_DEV_FAILED);
1270 qla4_8xxx_idc_unlock(ha);
1271 ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
1272 qla4_8xxx_device_state_handler(ha);
1273 }
1274 if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
1275 qla4_8xxx_need_qsnt_handler(ha);
1276 }
1277 }
1278
1279 if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
1280 (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07001281 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301282 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
1283 if (ql4xdontresethba) {
1284 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
1285 ha->host_no, __func__));
1286 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
1287 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1288 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1289 goto dpc_post_reset_ha;
1290 }
1291 if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
1292 test_bit(DPC_RESET_HA, &ha->dpc_flags))
1293 qla4xxx_recover_adapter(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001294
David C Somayajulu477ffb92007-01-22 12:26:11 -08001295 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001296 uint8_t wait_time = RESET_INTR_TOV;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001297
David Somayajuluafaf5a22006-09-19 10:28:00 -07001298 while ((readw(&ha->reg->ctrl_status) &
1299 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
1300 if (--wait_time == 0)
1301 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001302 msleep(1000);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001303 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001304 if (wait_time == 0)
1305 DEBUG2(printk("scsi%ld: %s: SR|FSR "
1306 "bit not cleared-- resetting\n",
1307 ha->host_no, __func__));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301308 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
David C Somayajulu477ffb92007-01-22 12:26:11 -08001309 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
1310 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301311 status = qla4xxx_recover_adapter(ha);
David C Somayajulu477ffb92007-01-22 12:26:11 -08001312 }
1313 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1314 if (status == QLA_SUCCESS)
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301315 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001316 }
1317 }
1318
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301319dpc_post_reset_ha:
David Somayajuluafaf5a22006-09-19 10:28:00 -07001320 /* ---- process AEN? --- */
1321 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
1322 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
1323
1324 /* ---- Get DHCP IP Address? --- */
1325 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
1326 qla4xxx_get_dhcp_ip_address(ha);
1327
Vikas Chaudhary065aa1b42010-04-28 11:38:11 +05301328 /* ---- link change? --- */
1329 if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
1330 if (!test_bit(AF_LINK_UP, &ha->flags)) {
1331 /* ---- link down? --- */
1332 list_for_each_entry_safe(ddb_entry, dtemp,
1333 &ha->ddb_list, list) {
1334 if (atomic_read(&ddb_entry->state) ==
1335 DDB_STATE_ONLINE)
1336 qla4xxx_mark_device_missing(ha,
1337 ddb_entry);
1338 }
1339 } else {
1340 /* ---- link up? --- *
1341 * F/W will auto login to all devices ONLY ONCE after
1342 * link up during driver initialization and runtime
1343 * fatal error recovery. Therefore, the driver must
1344 * manually relogin to devices when recovering from
1345 * connection failures, logouts, expired KATO, etc. */
1346
1347 list_for_each_entry_safe(ddb_entry, dtemp,
1348 &ha->ddb_list, list) {
1349 if ((atomic_read(&ddb_entry->state) ==
1350 DDB_STATE_MISSING) ||
1351 (atomic_read(&ddb_entry->state) ==
1352 DDB_STATE_DEAD)) {
1353 if (ddb_entry->fw_ddb_device_state ==
1354 DDB_DS_SESSION_ACTIVE) {
1355 atomic_set(&ddb_entry->state,
1356 DDB_STATE_ONLINE);
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301357 ql4_printk(KERN_INFO, ha,
Vikas Chaudhary065aa1b42010-04-28 11:38:11 +05301358 "scsi%ld: %s: ddb[%d]"
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301359 " marked ONLINE\n",
Vikas Chaudhary065aa1b42010-04-28 11:38:11 +05301360 ha->host_no, __func__,
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301361 ddb_entry->fw_ddb_index);
Vikas Chaudhary065aa1b42010-04-28 11:38:11 +05301362
1363 iscsi_unblock_session(
1364 ddb_entry->sess);
1365 } else
1366 qla4xxx_relogin_device(
1367 ha, ddb_entry);
1368 }
1369
1370 }
1371 }
1372 }
1373
David Somayajuluafaf5a22006-09-19 10:28:00 -07001374 /* ---- relogin device? --- */
1375 if (adapter_up(ha) &&
1376 test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
1377 list_for_each_entry_safe(ddb_entry, dtemp,
1378 &ha->ddb_list, list) {
1379 if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
1380 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
1381 qla4xxx_relogin_device(ha, ddb_entry);
1382
1383 /*
1384 * If mbx cmd times out there is no point
1385 * in continuing further.
1386 * With large no of targets this can hang
1387 * the system.
1388 */
1389 if (test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
1390 printk(KERN_WARNING "scsi%ld: %s: "
1391 "need to reset hba\n",
1392 ha->host_no, __func__);
1393 break;
1394 }
1395 }
1396 }
Vikas Chaudhary0753b482010-07-30 14:27:19 +05301397
1398do_dpc_exit:
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301399 clear_bit(AF_DPC_SCHEDULED, &ha->flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001400}
1401
1402/**
1403 * qla4xxx_free_adapter - release the adapter
1404 * @ha: pointer to adapter structure
1405 **/
1406static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
1407{
1408
1409 if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
1410 /* Turn-off interrupts on the card. */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301411 ha->isp_ops->disable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001412 }
1413
David Somayajuluafaf5a22006-09-19 10:28:00 -07001414 /* Remove timer thread, if present */
1415 if (ha->timer_active)
1416 qla4xxx_stop_timer(ha);
1417
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301418 /* Kill the kernel thread for this host */
1419 if (ha->dpc_thread)
1420 destroy_workqueue(ha->dpc_thread);
1421
1422 /* Put firmware in known state */
1423 ha->isp_ops->reset_firmware(ha);
1424
1425 if (is_qla8022(ha)) {
1426 qla4_8xxx_idc_lock(ha);
1427 qla4_8xxx_clear_drv_active(ha);
1428 qla4_8xxx_idc_unlock(ha);
1429 }
1430
David Somayajuluafaf5a22006-09-19 10:28:00 -07001431 /* Detach interrupts */
1432 if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301433 qla4xxx_free_irqs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001434
David C Somayajulubee4fe82007-05-23 18:03:32 -07001435 /* free extra memory */
1436 qla4xxx_mem_free(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301437}
David C Somayajulubee4fe82007-05-23 18:03:32 -07001438
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301439int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
1440{
1441 int status = 0;
1442 uint8_t revision_id;
1443 unsigned long mem_base, mem_len, db_base, db_len;
1444 struct pci_dev *pdev = ha->pdev;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001445
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301446 status = pci_request_regions(pdev, DRIVER_NAME);
1447 if (status) {
1448 printk(KERN_WARNING
1449 "scsi(%ld) Failed to reserve PIO regions (%s) "
1450 "status=%d\n", ha->host_no, pci_name(pdev), status);
1451 goto iospace_error_exit;
1452 }
1453
1454 pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
1455 DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
1456 __func__, revision_id));
1457 ha->revision_id = revision_id;
1458
1459 /* remap phys address */
1460 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
1461 mem_len = pci_resource_len(pdev, 0);
1462 DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
1463 __func__, mem_base, mem_len));
1464
1465 /* mapping of pcibase pointer */
1466 ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
1467 if (!ha->nx_pcibase) {
1468 printk(KERN_ERR
1469 "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
1470 pci_release_regions(ha->pdev);
1471 goto iospace_error_exit;
1472 }
1473
1474 /* Mapping of IO base pointer, door bell read and write pointer */
1475
1476 /* mapping of IO base pointer */
1477 ha->qla4_8xxx_reg =
1478 (struct device_reg_82xx __iomem *)((uint8_t *)ha->nx_pcibase +
1479 0xbc000 + (ha->pdev->devfn << 11));
1480
1481 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
1482 db_len = pci_resource_len(pdev, 4);
1483
Shyam Sundar2657c802010-10-06 22:50:29 -07001484 ha->nx_db_wr_ptr = (ha->pdev->devfn == 4 ? QLA82XX_CAM_RAM_DB1 :
1485 QLA82XX_CAM_RAM_DB2);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301486
Shyam Sundar2657c802010-10-06 22:50:29 -07001487 return 0;
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301488iospace_error_exit:
1489 return -ENOMEM;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001490}
1491
1492/***
1493 * qla4xxx_iospace_config - maps registers
1494 * @ha: pointer to adapter structure
1495 *
1496 * This routines maps HBA's registers from the pci address space
1497 * into the kernel virtual address space for memory mapped i/o.
1498 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301499int qla4xxx_iospace_config(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001500{
1501 unsigned long pio, pio_len, pio_flags;
1502 unsigned long mmio, mmio_len, mmio_flags;
1503
1504 pio = pci_resource_start(ha->pdev, 0);
1505 pio_len = pci_resource_len(ha->pdev, 0);
1506 pio_flags = pci_resource_flags(ha->pdev, 0);
1507 if (pio_flags & IORESOURCE_IO) {
1508 if (pio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301509 ql4_printk(KERN_WARNING, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07001510 "Invalid PCI I/O region size\n");
1511 pio = 0;
1512 }
1513 } else {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301514 ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001515 pio = 0;
1516 }
1517
1518 /* Use MMIO operations for all accesses. */
1519 mmio = pci_resource_start(ha->pdev, 1);
1520 mmio_len = pci_resource_len(ha->pdev, 1);
1521 mmio_flags = pci_resource_flags(ha->pdev, 1);
1522
1523 if (!(mmio_flags & IORESOURCE_MEM)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301524 ql4_printk(KERN_ERR, ha,
1525 "region #0 not an MMIO resource, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001526
1527 goto iospace_error_exit;
1528 }
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301529
David Somayajuluafaf5a22006-09-19 10:28:00 -07001530 if (mmio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301531 ql4_printk(KERN_ERR, ha,
1532 "Invalid PCI mem region size, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001533 goto iospace_error_exit;
1534 }
1535
1536 if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301537 ql4_printk(KERN_WARNING, ha,
1538 "Failed to reserve PIO/MMIO regions\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001539
1540 goto iospace_error_exit;
1541 }
1542
1543 ha->pio_address = pio;
1544 ha->pio_length = pio_len;
1545 ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
1546 if (!ha->reg) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301547 ql4_printk(KERN_ERR, ha,
1548 "cannot remap MMIO, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001549
1550 goto iospace_error_exit;
1551 }
1552
1553 return 0;
1554
1555iospace_error_exit:
1556 return -ENOMEM;
1557}
1558
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301559static struct isp_operations qla4xxx_isp_ops = {
1560 .iospace_config = qla4xxx_iospace_config,
1561 .pci_config = qla4xxx_pci_config,
1562 .disable_intrs = qla4xxx_disable_intrs,
1563 .enable_intrs = qla4xxx_enable_intrs,
1564 .start_firmware = qla4xxx_start_firmware,
1565 .intr_handler = qla4xxx_intr_handler,
1566 .interrupt_service_routine = qla4xxx_interrupt_service_routine,
1567 .reset_chip = qla4xxx_soft_reset,
1568 .reset_firmware = qla4xxx_hw_reset,
1569 .queue_iocb = qla4xxx_queue_iocb,
1570 .complete_iocb = qla4xxx_complete_iocb,
1571 .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
1572 .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
1573 .get_sys_info = qla4xxx_get_sys_info,
1574};
1575
1576static struct isp_operations qla4_8xxx_isp_ops = {
1577 .iospace_config = qla4_8xxx_iospace_config,
1578 .pci_config = qla4_8xxx_pci_config,
1579 .disable_intrs = qla4_8xxx_disable_intrs,
1580 .enable_intrs = qla4_8xxx_enable_intrs,
1581 .start_firmware = qla4_8xxx_load_risc,
1582 .intr_handler = qla4_8xxx_intr_handler,
1583 .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
1584 .reset_chip = qla4_8xxx_isp_reset,
1585 .reset_firmware = qla4_8xxx_stop_firmware,
1586 .queue_iocb = qla4_8xxx_queue_iocb,
1587 .complete_iocb = qla4_8xxx_complete_iocb,
1588 .rd_shdw_req_q_out = qla4_8xxx_rd_shdw_req_q_out,
1589 .rd_shdw_rsp_q_in = qla4_8xxx_rd_shdw_rsp_q_in,
1590 .get_sys_info = qla4_8xxx_get_sys_info,
1591};
1592
1593uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
1594{
1595 return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
1596}
1597
1598uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
1599{
1600 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
1601}
1602
1603uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
1604{
1605 return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
1606}
1607
1608uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
1609{
1610 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
1611}
1612
David Somayajuluafaf5a22006-09-19 10:28:00 -07001613/**
1614 * qla4xxx_probe_adapter - callback function to probe HBA
1615 * @pdev: pointer to pci_dev structure
1616 * @pci_device_id: pointer to pci_device entry
1617 *
1618 * This routine will probe for Qlogic 4xxx iSCSI host adapters.
1619 * It returns zero if successful. It also initializes all data necessary for
1620 * the driver.
1621 **/
1622static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
1623 const struct pci_device_id *ent)
1624{
1625 int ret = -ENODEV, status;
1626 struct Scsi_Host *host;
1627 struct scsi_qla_host *ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001628 uint8_t init_retry_count = 0;
1629 char buf[34];
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301630 struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001631
1632 if (pci_enable_device(pdev))
1633 return -1;
1634
1635 host = scsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha));
1636 if (host == NULL) {
1637 printk(KERN_WARNING
1638 "qla4xxx: Couldn't allocate host from scsi layer!\n");
1639 goto probe_disable_device;
1640 }
1641
1642 /* Clear our data area */
1643 ha = (struct scsi_qla_host *) host->hostdata;
1644 memset(ha, 0, sizeof(*ha));
1645
1646 /* Save the information from PCI BIOS. */
1647 ha->pdev = pdev;
1648 ha->host = host;
1649 ha->host_no = host->host_no;
1650
Lalit Chandivade2232be02010-07-30 14:38:47 +05301651 pci_enable_pcie_error_reporting(pdev);
1652
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301653 /* Setup Runtime configurable options */
1654 if (is_qla8022(ha)) {
1655 ha->isp_ops = &qla4_8xxx_isp_ops;
1656 rwlock_init(&ha->hw_lock);
1657 ha->qdr_sn_window = -1;
1658 ha->ddr_mn_window = -1;
1659 ha->curr_window = 255;
1660 ha->func_num = PCI_FUNC(ha->pdev->devfn);
1661 nx_legacy_intr = &legacy_intr[ha->func_num];
1662 ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
1663 ha->nx_legacy_intr.tgt_status_reg =
1664 nx_legacy_intr->tgt_status_reg;
1665 ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
1666 ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
1667 } else {
1668 ha->isp_ops = &qla4xxx_isp_ops;
1669 }
1670
Lalit Chandivade2232be02010-07-30 14:38:47 +05301671 /* Set EEH reset type to fundamental if required by hba */
1672 if (is_qla8022(ha))
1673 pdev->needs_freset = 1;
1674
David Somayajuluafaf5a22006-09-19 10:28:00 -07001675 /* Configure PCI I/O space. */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301676 ret = ha->isp_ops->iospace_config(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001677 if (ret)
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301678 goto probe_failed_ioconfig;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001679
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301680 ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
David Somayajuluafaf5a22006-09-19 10:28:00 -07001681 pdev->device, pdev->irq, ha->reg);
1682
1683 qla4xxx_config_dma_addressing(ha);
1684
1685 /* Initialize lists and spinlocks. */
1686 INIT_LIST_HEAD(&ha->ddb_list);
1687 INIT_LIST_HEAD(&ha->free_srb_q);
1688
1689 mutex_init(&ha->mbox_sem);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301690 init_completion(&ha->mbx_intr_comp);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001691
1692 spin_lock_init(&ha->hardware_lock);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001693
1694 /* Allocate dma buffers */
1695 if (qla4xxx_mem_alloc(ha)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301696 ql4_printk(KERN_WARNING, ha,
1697 "[ERROR] Failed to allocate memory for adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001698
1699 ret = -ENOMEM;
1700 goto probe_failed;
1701 }
1702
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301703 if (is_qla8022(ha))
1704 (void) qla4_8xxx_get_flash_info(ha);
1705
David Somayajuluafaf5a22006-09-19 10:28:00 -07001706 /*
1707 * Initialize the Host adapter request/response queues and
1708 * firmware
1709 * NOTE: interrupts enabled upon successful completion
1710 */
1711 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301712 while ((!test_bit(AF_ONLINE, &ha->flags)) &&
1713 init_retry_count++ < MAX_INIT_RETRIES) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001714 DEBUG2(printk("scsi: %s: retrying adapter initialization "
1715 "(%d)\n", __func__, init_retry_count));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301716
1717 if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
1718 continue;
1719
David Somayajuluafaf5a22006-09-19 10:28:00 -07001720 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1721 }
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301722
1723 if (!test_bit(AF_ONLINE, &ha->flags)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301724 ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001725
Lalit Chandivadefe998522010-12-02 22:12:36 -08001726 if (is_qla8022(ha) && ql4xdontresethba) {
1727 /* Put the device in failed state. */
1728 DEBUG2(printk(KERN_ERR "HW STATE: FAILED\n"));
1729 qla4_8xxx_idc_lock(ha);
1730 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
1731 QLA82XX_DEV_FAILED);
1732 qla4_8xxx_idc_unlock(ha);
1733 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001734 ret = -ENODEV;
1735 goto probe_failed;
1736 }
1737
1738 host->cmd_per_lun = 3;
1739 host->max_channel = 0;
1740 host->max_lun = MAX_LUNS - 1;
1741 host->max_id = MAX_TARGETS;
1742 host->max_cmd_len = IOCB_MAX_CDB_LEN;
1743 host->can_queue = MAX_SRBS ;
1744 host->transportt = qla4xxx_scsi_transport;
1745
1746 ret = scsi_init_shared_tag_map(host, MAX_SRBS);
1747 if (ret) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301748 ql4_printk(KERN_WARNING, ha,
1749 "scsi_init_shared_tag_map failed\n");
1750 goto probe_failed;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001751 }
1752
1753 /* Startup the kernel thread for this host adapter. */
1754 DEBUG2(printk("scsi: %s: Starting kernel thread for "
1755 "qla4xxx_dpc\n", __func__));
1756 sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
1757 ha->dpc_thread = create_singlethread_workqueue(buf);
1758 if (!ha->dpc_thread) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301759 ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001760 ret = -ENODEV;
1761 goto probe_failed;
1762 }
David Howellsc4028952006-11-22 14:57:56 +00001763 INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001764
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301765 /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
1766 * (which is called indirectly by qla4xxx_initialize_adapter),
1767 * so that irqs will be registered after crbinit but before
1768 * mbx_intr_enable.
1769 */
1770 if (!is_qla8022(ha)) {
1771 ret = qla4xxx_request_irqs(ha);
1772 if (ret) {
1773 ql4_printk(KERN_WARNING, ha, "Failed to reserve "
1774 "interrupt %d already in use.\n", pdev->irq);
1775 goto probe_failed;
1776 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001777 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001778
Lalit Chandivade2232be02010-07-30 14:38:47 +05301779 pci_save_state(ha->pdev);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301780 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001781
1782 /* Start timer thread. */
1783 qla4xxx_start_timer(ha, qla4xxx_timer, 1);
1784
1785 set_bit(AF_INIT_DONE, &ha->flags);
1786
1787 pci_set_drvdata(pdev, ha);
1788
1789 ret = scsi_add_host(host, &pdev->dev);
1790 if (ret)
1791 goto probe_failed;
1792
David Somayajuluafaf5a22006-09-19 10:28:00 -07001793 printk(KERN_INFO
1794 " QLogic iSCSI HBA Driver version: %s\n"
1795 " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
1796 qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
1797 ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
1798 ha->patch_number, ha->build_number);
Mike Christie921601b2008-01-31 13:36:49 -06001799 scsi_scan_host(host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001800 return 0;
1801
David Somayajuluafaf5a22006-09-19 10:28:00 -07001802probe_failed:
1803 qla4xxx_free_adapter(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301804
1805probe_failed_ioconfig:
Lalit Chandivade2232be02010-07-30 14:38:47 +05301806 pci_disable_pcie_error_reporting(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001807 scsi_host_put(ha->host);
1808
1809probe_disable_device:
1810 pci_disable_device(pdev);
1811
1812 return ret;
1813}
1814
1815/**
1816 * qla4xxx_remove_adapter - calback function to remove adapter.
1817 * @pci_dev: PCI device pointer
1818 **/
1819static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
1820{
1821 struct scsi_qla_host *ha;
1822
1823 ha = pci_get_drvdata(pdev);
1824
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301825 set_bit(AF_HBA_GOING_AWAY, &ha->flags);
David C Somayajulubee4fe82007-05-23 18:03:32 -07001826
David Somayajuluafaf5a22006-09-19 10:28:00 -07001827 /* remove devs from iscsi_sessions to scsi_devices */
1828 qla4xxx_free_ddb_list(ha);
1829
1830 scsi_remove_host(ha->host);
1831
1832 qla4xxx_free_adapter(ha);
1833
1834 scsi_host_put(ha->host);
1835
Lalit Chandivade2232be02010-07-30 14:38:47 +05301836 pci_disable_pcie_error_reporting(pdev);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301837 pci_disable_device(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001838 pci_set_drvdata(pdev, NULL);
1839}
1840
1841/**
1842 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
1843 * @ha: HA context
1844 *
1845 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
1846 * supported addressing method.
1847 */
Adrian Bunk47975472007-04-26 00:35:16 -07001848static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001849{
1850 int retval;
1851
1852 /* Update our PCI device dma_mask for full 64 bit mask */
Yang Hongyang6a355282009-04-06 19:01:13 -07001853 if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
1854 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001855 dev_dbg(&ha->pdev->dev,
1856 "Failed to set 64 bit PCI consistent mask; "
1857 "using 32 bit.\n");
1858 retval = pci_set_consistent_dma_mask(ha->pdev,
Yang Hongyang284901a2009-04-06 19:01:15 -07001859 DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001860 }
1861 } else
Yang Hongyang284901a2009-04-06 19:01:15 -07001862 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001863}
1864
1865static int qla4xxx_slave_alloc(struct scsi_device *sdev)
1866{
1867 struct iscsi_cls_session *sess = starget_to_session(sdev->sdev_target);
1868 struct ddb_entry *ddb = sess->dd_data;
1869
1870 sdev->hostdata = ddb;
1871 sdev->tagged_supported = 1;
Mike Christied510d962008-07-11 19:50:33 -05001872 scsi_activate_tcq(sdev, QL4_DEF_QDEPTH);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001873 return 0;
1874}
1875
1876static int qla4xxx_slave_configure(struct scsi_device *sdev)
1877{
1878 sdev->tagged_supported = 1;
1879 return 0;
1880}
1881
1882static void qla4xxx_slave_destroy(struct scsi_device *sdev)
1883{
1884 scsi_deactivate_tcq(sdev, 1);
1885}
1886
1887/**
1888 * qla4xxx_del_from_active_array - returns an active srb
1889 * @ha: Pointer to host adapter structure.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001890 * @index: index into the active_array
David Somayajuluafaf5a22006-09-19 10:28:00 -07001891 *
1892 * This routine removes and returns the srb at the specified index
1893 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301894struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
1895 uint32_t index)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001896{
1897 struct srb *srb = NULL;
Vikas Chaudhary53698872010-04-28 11:41:59 +05301898 struct scsi_cmnd *cmd = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001899
Vikas Chaudhary53698872010-04-28 11:41:59 +05301900 cmd = scsi_host_find_tag(ha->host, index);
1901 if (!cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001902 return srb;
1903
Vikas Chaudhary53698872010-04-28 11:41:59 +05301904 srb = (struct srb *)CMD_SP(cmd);
1905 if (!srb)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001906 return srb;
1907
1908 /* update counters */
1909 if (srb->flags & SRB_DMA_VALID) {
1910 ha->req_q_count += srb->iocb_cnt;
1911 ha->iocb_cnt -= srb->iocb_cnt;
1912 if (srb->cmd)
Vikas Chaudhary53698872010-04-28 11:41:59 +05301913 srb->cmd->host_scribble =
1914 (unsigned char *)(unsigned long) MAX_SRBS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001915 }
1916 return srb;
1917}
1918
1919/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07001920 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301921 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001922 * @cmd: Scsi Command to wait on.
1923 *
1924 * This routine waits for the command to be returned by the Firmware
1925 * for some max time.
1926 **/
1927static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
1928 struct scsi_cmnd *cmd)
1929{
1930 int done = 0;
1931 struct srb *rp;
1932 uint32_t max_wait_time = EH_WAIT_CMD_TOV;
Lalit Chandivade2232be02010-07-30 14:38:47 +05301933 int ret = SUCCESS;
1934
1935 /* Dont wait on command if PCI error is being handled
1936 * by PCI AER driver
1937 */
1938 if (unlikely(pci_channel_offline(ha->pdev)) ||
1939 (test_bit(AF_EEH_BUSY, &ha->flags))) {
1940 ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
1941 ha->host_no, __func__);
1942 return ret;
1943 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001944
1945 do {
1946 /* Checking to see if its returned to OS */
Vikas Chaudhary53698872010-04-28 11:41:59 +05301947 rp = (struct srb *) CMD_SP(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001948 if (rp == NULL) {
1949 done++;
1950 break;
1951 }
1952
1953 msleep(2000);
1954 } while (max_wait_time--);
1955
1956 return done;
1957}
1958
1959/**
1960 * qla4xxx_wait_for_hba_online - waits for HBA to come online
1961 * @ha: Pointer to host adapter structure
1962 **/
1963static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
1964{
1965 unsigned long wait_online;
1966
Vikas Chaudharyf581a3f2010-10-06 22:47:48 -07001967 wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001968 while (time_before(jiffies, wait_online)) {
1969
1970 if (adapter_up(ha))
1971 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001972
1973 msleep(2000);
1974 }
1975
1976 return QLA_ERROR;
1977}
1978
1979/**
Mike Christiece545032008-02-29 18:25:20 -06001980 * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001981 * @ha: pointer to HBA
David Somayajuluafaf5a22006-09-19 10:28:00 -07001982 * @t: target id
1983 * @l: lun id
1984 *
1985 * This function waits for all outstanding commands to a lun to complete. It
1986 * returns 0 if all pending commands are returned and 1 otherwise.
1987 **/
Mike Christiece545032008-02-29 18:25:20 -06001988static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
1989 struct scsi_target *stgt,
1990 struct scsi_device *sdev)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001991{
1992 int cnt;
1993 int status = 0;
1994 struct scsi_cmnd *cmd;
1995
1996 /*
Mike Christiece545032008-02-29 18:25:20 -06001997 * Waiting for all commands for the designated target or dev
1998 * in the active array
David Somayajuluafaf5a22006-09-19 10:28:00 -07001999 */
2000 for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
2001 cmd = scsi_host_find_tag(ha->host, cnt);
Mike Christiece545032008-02-29 18:25:20 -06002002 if (cmd && stgt == scsi_target(cmd->device) &&
2003 (!sdev || sdev == cmd->device)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002004 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
2005 status++;
2006 break;
2007 }
2008 }
2009 }
2010 return status;
2011}
2012
2013/**
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302014 * qla4xxx_eh_abort - callback for abort task.
2015 * @cmd: Pointer to Linux's SCSI command structure
2016 *
2017 * This routine is called by the Linux OS to abort the specified
2018 * command.
2019 **/
2020static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
2021{
2022 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2023 unsigned int id = cmd->device->id;
2024 unsigned int lun = cmd->device->lun;
2025 unsigned long serial = cmd->serial_number;
Mike Christie92b3e5b2010-10-06 22:51:17 -07002026 unsigned long flags;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302027 struct srb *srb = NULL;
2028 int ret = SUCCESS;
2029 int wait = 0;
2030
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302031 ql4_printk(KERN_INFO, ha,
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302032 "scsi%ld:%d:%d: Abort command issued cmd=%p, pid=%ld\n",
2033 ha->host_no, id, lun, cmd, serial);
2034
Mike Christie92b3e5b2010-10-06 22:51:17 -07002035 spin_lock_irqsave(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302036 srb = (struct srb *) CMD_SP(cmd);
Mike Christie92b3e5b2010-10-06 22:51:17 -07002037 if (!srb) {
2038 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302039 return SUCCESS;
Mike Christie92b3e5b2010-10-06 22:51:17 -07002040 }
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302041 kref_get(&srb->srb_ref);
Mike Christie92b3e5b2010-10-06 22:51:17 -07002042 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302043
2044 if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
2045 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
2046 ha->host_no, id, lun));
2047 ret = FAILED;
2048 } else {
2049 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
2050 ha->host_no, id, lun));
2051 wait = 1;
2052 }
2053
2054 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
2055
2056 /* Wait for command to complete */
2057 if (wait) {
2058 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
2059 DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
2060 ha->host_no, id, lun));
2061 ret = FAILED;
2062 }
2063 }
2064
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302065 ql4_printk(KERN_INFO, ha,
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302066 "scsi%ld:%d:%d: Abort command - %s\n",
2067 ha->host_no, id, lun, (ret == SUCCESS) ? "succeded" : "failed");
2068
2069 return ret;
2070}
2071
2072/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002073 * qla4xxx_eh_device_reset - callback for target reset.
2074 * @cmd: Pointer to Linux's SCSI command structure
2075 *
2076 * This routine is called by the Linux OS to reset all luns on the
2077 * specified target.
2078 **/
2079static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
2080{
2081 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2082 struct ddb_entry *ddb_entry = cmd->device->hostdata;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002083 int ret = FAILED, stat;
2084
Karen Higgins612f7342009-07-15 15:03:01 -05002085 if (!ddb_entry)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002086 return ret;
2087
Mike Christiec01be6d2010-07-22 16:59:49 +05302088 ret = iscsi_block_scsi_eh(cmd);
2089 if (ret)
2090 return ret;
2091 ret = FAILED;
2092
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302093 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002094 "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
2095 cmd->device->channel, cmd->device->id, cmd->device->lun);
2096
2097 DEBUG2(printk(KERN_INFO
2098 "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
2099 "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
Jens Axboe242f9dc2008-09-14 05:55:09 -07002100 cmd, jiffies, cmd->request->timeout / HZ,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002101 ha->dpc_flags, cmd->result, cmd->allowed));
2102
2103 /* FIXME: wait for hba to go online */
2104 stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
2105 if (stat != QLA_SUCCESS) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302106 ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002107 goto eh_dev_reset_done;
2108 }
2109
Mike Christiece545032008-02-29 18:25:20 -06002110 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2111 cmd->device)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302112 ql4_printk(KERN_INFO, ha,
Mike Christiece545032008-02-29 18:25:20 -06002113 "DEVICE RESET FAILED - waiting for "
2114 "commands.\n");
2115 goto eh_dev_reset_done;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002116 }
2117
David C Somayajulu9d562912008-03-19 11:23:03 -07002118 /* Send marker. */
2119 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2120 MM_LUN_RESET) != QLA_SUCCESS)
2121 goto eh_dev_reset_done;
2122
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302123 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002124 "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
2125 ha->host_no, cmd->device->channel, cmd->device->id,
2126 cmd->device->lun);
2127
2128 ret = SUCCESS;
2129
2130eh_dev_reset_done:
2131
2132 return ret;
2133}
2134
2135/**
Mike Christiece545032008-02-29 18:25:20 -06002136 * qla4xxx_eh_target_reset - callback for target reset.
2137 * @cmd: Pointer to Linux's SCSI command structure
2138 *
2139 * This routine is called by the Linux OS to reset the target.
2140 **/
2141static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
2142{
2143 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2144 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christiec01be6d2010-07-22 16:59:49 +05302145 int stat, ret;
Mike Christiece545032008-02-29 18:25:20 -06002146
2147 if (!ddb_entry)
2148 return FAILED;
2149
Mike Christiec01be6d2010-07-22 16:59:49 +05302150 ret = iscsi_block_scsi_eh(cmd);
2151 if (ret)
2152 return ret;
2153
Mike Christiece545032008-02-29 18:25:20 -06002154 starget_printk(KERN_INFO, scsi_target(cmd->device),
2155 "WARM TARGET RESET ISSUED.\n");
2156
2157 DEBUG2(printk(KERN_INFO
2158 "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
2159 "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
Jens Axboe242f9dc2008-09-14 05:55:09 -07002160 ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
Mike Christiece545032008-02-29 18:25:20 -06002161 ha->dpc_flags, cmd->result, cmd->allowed));
2162
2163 stat = qla4xxx_reset_target(ha, ddb_entry);
2164 if (stat != QLA_SUCCESS) {
2165 starget_printk(KERN_INFO, scsi_target(cmd->device),
2166 "WARM TARGET RESET FAILED.\n");
2167 return FAILED;
2168 }
2169
Mike Christiece545032008-02-29 18:25:20 -06002170 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2171 NULL)) {
2172 starget_printk(KERN_INFO, scsi_target(cmd->device),
2173 "WARM TARGET DEVICE RESET FAILED - "
2174 "waiting for commands.\n");
2175 return FAILED;
2176 }
2177
David C Somayajulu9d562912008-03-19 11:23:03 -07002178 /* Send marker. */
2179 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2180 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
2181 starget_printk(KERN_INFO, scsi_target(cmd->device),
2182 "WARM TARGET DEVICE RESET FAILED - "
2183 "marker iocb failed.\n");
2184 return FAILED;
2185 }
2186
Mike Christiece545032008-02-29 18:25:20 -06002187 starget_printk(KERN_INFO, scsi_target(cmd->device),
2188 "WARM TARGET RESET SUCCEEDED.\n");
2189 return SUCCESS;
2190}
2191
2192/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002193 * qla4xxx_eh_host_reset - kernel callback
2194 * @cmd: Pointer to Linux's SCSI command structure
2195 *
2196 * This routine is invoked by the Linux kernel to perform fatal error
2197 * recovery on the specified adapter.
2198 **/
2199static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
2200{
2201 int return_status = FAILED;
2202 struct scsi_qla_host *ha;
2203
2204 ha = (struct scsi_qla_host *) cmd->device->host->hostdata;
2205
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302206 if (ql4xdontresethba) {
2207 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
2208 ha->host_no, __func__));
2209 return FAILED;
2210 }
2211
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302212 ql4_printk(KERN_INFO, ha,
Karen Higginsdca05c42009-07-15 15:03:00 -05002213 "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002214 cmd->device->channel, cmd->device->id, cmd->device->lun);
2215
2216 if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
2217 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
2218 "DEAD.\n", ha->host_no, cmd->device->channel,
2219 __func__));
2220
2221 return FAILED;
2222 }
2223
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302224 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
2225 if (is_qla8022(ha))
2226 set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2227 else
2228 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2229 }
Mike Christie50a29ae2008-03-04 13:26:53 -06002230
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302231 if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002232 return_status = SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002233
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302234 ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
David Somayajuluafaf5a22006-09-19 10:28:00 -07002235 return_status == FAILED ? "FAILED" : "SUCCEDED");
2236
2237 return return_status;
2238}
2239
Lalit Chandivade2232be02010-07-30 14:38:47 +05302240/* PCI AER driver recovers from all correctable errors w/o
2241 * driver intervention. For uncorrectable errors PCI AER
2242 * driver calls the following device driver's callbacks
2243 *
2244 * - Fatal Errors - link_reset
2245 * - Non-Fatal Errors - driver's pci_error_detected() which
2246 * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
2247 *
2248 * PCI AER driver calls
2249 * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
2250 * returns RECOVERED or NEED_RESET if fw_hung
2251 * NEED_RESET - driver's slot_reset()
2252 * DISCONNECT - device is dead & cannot recover
2253 * RECOVERED - driver's pci_resume()
2254 */
2255static pci_ers_result_t
2256qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
2257{
2258 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2259
2260 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
2261 ha->host_no, __func__, state);
2262
2263 if (!is_aer_supported(ha))
2264 return PCI_ERS_RESULT_NONE;
2265
2266 switch (state) {
2267 case pci_channel_io_normal:
2268 clear_bit(AF_EEH_BUSY, &ha->flags);
2269 return PCI_ERS_RESULT_CAN_RECOVER;
2270 case pci_channel_io_frozen:
2271 set_bit(AF_EEH_BUSY, &ha->flags);
2272 qla4xxx_mailbox_premature_completion(ha);
2273 qla4xxx_free_irqs(ha);
2274 pci_disable_device(pdev);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002275 /* Return back all IOs */
2276 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
Lalit Chandivade2232be02010-07-30 14:38:47 +05302277 return PCI_ERS_RESULT_NEED_RESET;
2278 case pci_channel_io_perm_failure:
2279 set_bit(AF_EEH_BUSY, &ha->flags);
2280 set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
2281 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
2282 return PCI_ERS_RESULT_DISCONNECT;
2283 }
2284 return PCI_ERS_RESULT_NEED_RESET;
2285}
2286
2287/**
2288 * qla4xxx_pci_mmio_enabled() gets called if
2289 * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
2290 * and read/write to the device still works.
2291 **/
2292static pci_ers_result_t
2293qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
2294{
2295 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2296
2297 if (!is_aer_supported(ha))
2298 return PCI_ERS_RESULT_NONE;
2299
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002300 return PCI_ERS_RESULT_RECOVERED;
Lalit Chandivade2232be02010-07-30 14:38:47 +05302301}
2302
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002303static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
Lalit Chandivade2232be02010-07-30 14:38:47 +05302304{
2305 uint32_t rval = QLA_ERROR;
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002306 uint32_t ret = 0;
Lalit Chandivade2232be02010-07-30 14:38:47 +05302307 int fn;
2308 struct pci_dev *other_pdev = NULL;
2309
2310 ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
2311
2312 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2313
2314 if (test_bit(AF_ONLINE, &ha->flags)) {
2315 clear_bit(AF_ONLINE, &ha->flags);
2316 qla4xxx_mark_all_devices_missing(ha);
2317 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
Lalit Chandivade2232be02010-07-30 14:38:47 +05302318 }
2319
2320 fn = PCI_FUNC(ha->pdev->devfn);
2321 while (fn > 0) {
2322 fn--;
2323 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
2324 "func %x\n", ha->host_no, __func__, fn);
2325 /* Get the pci device given the domain, bus,
2326 * slot/function number */
2327 other_pdev =
2328 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
2329 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
2330 fn));
2331
2332 if (!other_pdev)
2333 continue;
2334
2335 if (atomic_read(&other_pdev->enable_cnt)) {
2336 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
2337 "func in enabled state%x\n", ha->host_no,
2338 __func__, fn);
2339 pci_dev_put(other_pdev);
2340 break;
2341 }
2342 pci_dev_put(other_pdev);
2343 }
2344
2345 /* The first function on the card, the reset owner will
2346 * start & initialize the firmware. The other functions
2347 * on the card will reset the firmware context
2348 */
2349 if (!fn) {
2350 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
2351 "0x%x is the owner\n", ha->host_no, __func__,
2352 ha->pdev->devfn);
2353
2354 qla4_8xxx_idc_lock(ha);
2355 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2356 QLA82XX_DEV_COLD);
2357
2358 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
2359 QLA82XX_IDC_VERSION);
2360
2361 qla4_8xxx_idc_unlock(ha);
2362 clear_bit(AF_FW_RECOVERY, &ha->flags);
2363 rval = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
2364 qla4_8xxx_idc_lock(ha);
2365
2366 if (rval != QLA_SUCCESS) {
2367 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
2368 "FAILED\n", ha->host_no, __func__);
2369 qla4_8xxx_clear_drv_active(ha);
2370 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2371 QLA82XX_DEV_FAILED);
2372 } else {
2373 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
2374 "READY\n", ha->host_no, __func__);
2375 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2376 QLA82XX_DEV_READY);
2377 /* Clear driver state register */
2378 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
2379 qla4_8xxx_set_drv_active(ha);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002380 ret = qla4xxx_request_irqs(ha);
2381 if (ret) {
2382 ql4_printk(KERN_WARNING, ha, "Failed to "
2383 "reserve interrupt %d already in use.\n",
2384 ha->pdev->irq);
2385 rval = QLA_ERROR;
2386 } else {
2387 ha->isp_ops->enable_intrs(ha);
2388 rval = QLA_SUCCESS;
2389 }
Lalit Chandivade2232be02010-07-30 14:38:47 +05302390 }
2391 qla4_8xxx_idc_unlock(ha);
2392 } else {
2393 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
2394 "the reset owner\n", ha->host_no, __func__,
2395 ha->pdev->devfn);
2396 if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
2397 QLA82XX_DEV_READY)) {
2398 clear_bit(AF_FW_RECOVERY, &ha->flags);
2399 rval = qla4xxx_initialize_adapter(ha,
2400 PRESERVE_DDB_LIST);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002401 if (rval == QLA_SUCCESS) {
2402 ret = qla4xxx_request_irqs(ha);
2403 if (ret) {
2404 ql4_printk(KERN_WARNING, ha, "Failed to"
2405 " reserve interrupt %d already in"
2406 " use.\n", ha->pdev->irq);
2407 rval = QLA_ERROR;
2408 } else {
2409 ha->isp_ops->enable_intrs(ha);
2410 rval = QLA_SUCCESS;
2411 }
2412 }
Lalit Chandivade2232be02010-07-30 14:38:47 +05302413 qla4_8xxx_idc_lock(ha);
2414 qla4_8xxx_set_drv_active(ha);
2415 qla4_8xxx_idc_unlock(ha);
2416 }
2417 }
2418 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2419 return rval;
2420}
2421
2422static pci_ers_result_t
2423qla4xxx_pci_slot_reset(struct pci_dev *pdev)
2424{
2425 pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
2426 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2427 int rc;
2428
2429 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
2430 ha->host_no, __func__);
2431
2432 if (!is_aer_supported(ha))
2433 return PCI_ERS_RESULT_NONE;
2434
2435 /* Restore the saved state of PCIe device -
2436 * BAR registers, PCI Config space, PCIX, MSI,
2437 * IOV states
2438 */
2439 pci_restore_state(pdev);
2440
2441 /* pci_restore_state() clears the saved_state flag of the device
2442 * save restored state which resets saved_state flag
2443 */
2444 pci_save_state(pdev);
2445
2446 /* Initialize device or resume if in suspended state */
2447 rc = pci_enable_device(pdev);
2448 if (rc) {
2449 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Cant re-enable "
2450 "device after reset\n", ha->host_no, __func__);
2451 goto exit_slot_reset;
2452 }
2453
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002454 ha->isp_ops->disable_intrs(ha);
Lalit Chandivade2232be02010-07-30 14:38:47 +05302455
2456 if (is_qla8022(ha)) {
2457 if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
2458 ret = PCI_ERS_RESULT_RECOVERED;
2459 goto exit_slot_reset;
2460 } else
2461 goto exit_slot_reset;
2462 }
2463
2464exit_slot_reset:
2465 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
2466 "device after reset\n", ha->host_no, __func__, ret);
2467 return ret;
2468}
2469
2470static void
2471qla4xxx_pci_resume(struct pci_dev *pdev)
2472{
2473 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2474 int ret;
2475
2476 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
2477 ha->host_no, __func__);
2478
2479 ret = qla4xxx_wait_for_hba_online(ha);
2480 if (ret != QLA_SUCCESS) {
2481 ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
2482 "resume I/O from slot/link_reset\n", ha->host_no,
2483 __func__);
2484 }
2485
2486 pci_cleanup_aer_uncorrect_error_status(pdev);
2487 clear_bit(AF_EEH_BUSY, &ha->flags);
2488}
2489
2490static struct pci_error_handlers qla4xxx_err_handler = {
2491 .error_detected = qla4xxx_pci_error_detected,
2492 .mmio_enabled = qla4xxx_pci_mmio_enabled,
2493 .slot_reset = qla4xxx_pci_slot_reset,
2494 .resume = qla4xxx_pci_resume,
2495};
2496
David Somayajuluafaf5a22006-09-19 10:28:00 -07002497static struct pci_device_id qla4xxx_pci_tbl[] = {
2498 {
2499 .vendor = PCI_VENDOR_ID_QLOGIC,
2500 .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
2501 .subvendor = PCI_ANY_ID,
2502 .subdevice = PCI_ANY_ID,
2503 },
2504 {
2505 .vendor = PCI_VENDOR_ID_QLOGIC,
2506 .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
2507 .subvendor = PCI_ANY_ID,
2508 .subdevice = PCI_ANY_ID,
2509 },
David C Somayajulud9150582006-11-15 17:38:40 -08002510 {
2511 .vendor = PCI_VENDOR_ID_QLOGIC,
2512 .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
2513 .subvendor = PCI_ANY_ID,
2514 .subdevice = PCI_ANY_ID,
2515 },
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302516 {
2517 .vendor = PCI_VENDOR_ID_QLOGIC,
2518 .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
2519 .subvendor = PCI_ANY_ID,
2520 .subdevice = PCI_ANY_ID,
2521 },
David Somayajuluafaf5a22006-09-19 10:28:00 -07002522 {0, 0},
2523};
2524MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
2525
Adrian Bunk47975472007-04-26 00:35:16 -07002526static struct pci_driver qla4xxx_pci_driver = {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002527 .name = DRIVER_NAME,
2528 .id_table = qla4xxx_pci_tbl,
2529 .probe = qla4xxx_probe_adapter,
2530 .remove = qla4xxx_remove_adapter,
Lalit Chandivade2232be02010-07-30 14:38:47 +05302531 .err_handler = &qla4xxx_err_handler,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002532};
2533
2534static int __init qla4xxx_module_init(void)
2535{
2536 int ret;
2537
2538 /* Allocate cache for SRBs. */
2539 srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09002540 SLAB_HWCACHE_ALIGN, NULL);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002541 if (srb_cachep == NULL) {
2542 printk(KERN_ERR
2543 "%s: Unable to allocate SRB cache..."
2544 "Failing load!\n", DRIVER_NAME);
2545 ret = -ENOMEM;
2546 goto no_srp_cache;
2547 }
2548
2549 /* Derive version string. */
2550 strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
Andrew Vasquez11010fe2006-10-06 09:54:59 -07002551 if (ql4xextended_error_logging)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002552 strcat(qla4xxx_version_str, "-debug");
2553
2554 qla4xxx_scsi_transport =
2555 iscsi_register_transport(&qla4xxx_iscsi_transport);
2556 if (!qla4xxx_scsi_transport){
2557 ret = -ENODEV;
2558 goto release_srb_cache;
2559 }
2560
David Somayajuluafaf5a22006-09-19 10:28:00 -07002561 ret = pci_register_driver(&qla4xxx_pci_driver);
2562 if (ret)
2563 goto unregister_transport;
2564
2565 printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
2566 return 0;
Doug Maxey5ae16db2006-10-05 23:50:07 -05002567
David Somayajuluafaf5a22006-09-19 10:28:00 -07002568unregister_transport:
2569 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
2570release_srb_cache:
2571 kmem_cache_destroy(srb_cachep);
2572no_srp_cache:
2573 return ret;
2574}
2575
2576static void __exit qla4xxx_module_exit(void)
2577{
David Somayajuluafaf5a22006-09-19 10:28:00 -07002578 pci_unregister_driver(&qla4xxx_pci_driver);
2579 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
2580 kmem_cache_destroy(srb_cachep);
2581}
2582
2583module_init(qla4xxx_module_init);
2584module_exit(qla4xxx_module_exit);
2585
2586MODULE_AUTHOR("QLogic Corporation");
2587MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
2588MODULE_LICENSE("GPL");
2589MODULE_VERSION(QLA4XXX_DRIVER_VERSION);