blob: f8a1506cb76a3305f270e73b95702f0692365438 [file] [log] [blame]
David Somayajuluafaf5a22006-09-19 10:28:00 -07001/*
2 * QLogic iSCSI HBA Driver
Vikas Chaudhary7d01d062010-12-02 22:12:51 -08003 * Copyright (c) 2003-2010 QLogic Corporation
David Somayajuluafaf5a22006-09-19 10:28:00 -07004 *
5 * See LICENSE.qla4xxx for copyright and licensing details.
6 */
7#include <linux/moduleparam.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
David Somayajuluafaf5a22006-09-19 10:28:00 -07009
10#include <scsi/scsi_tcq.h>
11#include <scsi/scsicam.h>
12
13#include "ql4_def.h"
David C Somayajulubee4fe82007-05-23 18:03:32 -070014#include "ql4_version.h"
15#include "ql4_glbl.h"
16#include "ql4_dbg.h"
17#include "ql4_inline.h"
David Somayajuluafaf5a22006-09-19 10:28:00 -070018
19/*
20 * Driver version
21 */
Adrian Bunk47975472007-04-26 00:35:16 -070022static char qla4xxx_version_str[40];
David Somayajuluafaf5a22006-09-19 10:28:00 -070023
24/*
25 * SRB allocation cache
26 */
Christoph Lametere18b8902006-12-06 20:33:20 -080027static struct kmem_cache *srb_cachep;
David Somayajuluafaf5a22006-09-19 10:28:00 -070028
29/*
30 * Module parameter information and variables
31 */
David Somayajuluafaf5a22006-09-19 10:28:00 -070032int ql4xdontresethba = 0;
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +053033module_param(ql4xdontresethba, int, S_IRUGO | S_IWUSR);
David Somayajuluafaf5a22006-09-19 10:28:00 -070034MODULE_PARM_DESC(ql4xdontresethba,
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +053035 "Don't reset the HBA for driver recovery \n"
36 " 0 - It will reset HBA (Default)\n"
37 " 1 - It will NOT reset HBA");
David Somayajuluafaf5a22006-09-19 10:28:00 -070038
Andrew Vasquez11010fe2006-10-06 09:54:59 -070039int ql4xextended_error_logging = 0; /* 0 = off, 1 = log errors */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +053040module_param(ql4xextended_error_logging, int, S_IRUGO | S_IWUSR);
Andrew Vasquez11010fe2006-10-06 09:54:59 -070041MODULE_PARM_DESC(ql4xextended_error_logging,
David Somayajuluafaf5a22006-09-19 10:28:00 -070042 "Option to enable extended error logging, "
43 "Default is 0 - no logging, 1 - debug logging");
44
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +053045int ql4xenablemsix = 1;
46module_param(ql4xenablemsix, int, S_IRUGO|S_IWUSR);
47MODULE_PARM_DESC(ql4xenablemsix,
48 "Set to enable MSI or MSI-X interrupt mechanism.\n"
49 " 0 = enable INTx interrupt mechanism.\n"
50 " 1 = enable MSI-X interrupt mechanism (Default).\n"
51 " 2 = enable MSI interrupt mechanism.");
David C Somayajulu477ffb92007-01-22 12:26:11 -080052
Mike Christied510d962008-07-11 19:50:33 -050053#define QL4_DEF_QDEPTH 32
Vikas Chaudhary8bb40332011-03-21 03:34:31 -070054static int ql4xmaxqdepth = QL4_DEF_QDEPTH;
55module_param(ql4xmaxqdepth, int, S_IRUGO | S_IWUSR);
56MODULE_PARM_DESC(ql4xmaxqdepth,
57 "Maximum queue depth to report for target devices.\n"
58 " Default: 32.");
Mike Christied510d962008-07-11 19:50:33 -050059
Vikas Chaudhary30387272011-03-21 03:34:32 -070060static int ql4xsess_recovery_tmo = QL4_SESS_RECOVERY_TMO;
61module_param(ql4xsess_recovery_tmo, int, S_IRUGO);
62MODULE_PARM_DESC(ql4xsess_recovery_tmo,
63 "Target Session Recovery Timeout.\n"
64 " Default: 30 sec.");
65
David Somayajuluafaf5a22006-09-19 10:28:00 -070066/*
67 * SCSI host template entry points
68 */
Adrian Bunk47975472007-04-26 00:35:16 -070069static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -070070
71/*
72 * iSCSI template entry points
73 */
Mike Christie2174a042007-05-30 12:57:10 -050074static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
75 enum iscsi_tgt_dscvr type, uint32_t enable,
76 struct sockaddr *dst_addr);
David Somayajuluafaf5a22006-09-19 10:28:00 -070077static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
78 enum iscsi_param param, char *buf);
79static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
80 enum iscsi_param param, char *buf);
Mike Christieaa1e93a2007-05-30 12:57:09 -050081static int qla4xxx_host_get_param(struct Scsi_Host *shost,
82 enum iscsi_host_param param, char *buf);
Mike Christied00efe32011-07-25 13:48:38 -050083static int qla4xxx_iface_set_param(struct Scsi_Host *shost, char *data,
84 int count);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -050085static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
86 enum iscsi_param_type param_type,
87 int param, char *buf);
David Somayajuluafaf5a22006-09-19 10:28:00 -070088static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session);
Mike Christie5c656af2009-07-15 15:02:59 -050089static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
David Somayajuluafaf5a22006-09-19 10:28:00 -070090
91/*
92 * SCSI host template entry points
93 */
Jeff Garzikf2812332010-11-16 02:10:29 -050094static int qla4xxx_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmd);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +053095static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -070096static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
Mike Christiece545032008-02-29 18:25:20 -060097static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -070098static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
99static int qla4xxx_slave_alloc(struct scsi_device *device);
100static int qla4xxx_slave_configure(struct scsi_device *device);
101static void qla4xxx_slave_destroy(struct scsi_device *sdev);
Mike Christie024f8012008-03-04 13:26:54 -0600102static void qla4xxx_scan_start(struct Scsi_Host *shost);
Mike Christie3128c6c2011-07-25 13:48:42 -0500103static mode_t ql4_attr_is_visible(int param_type, int param);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700104
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530105static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
106 QLA82XX_LEGACY_INTR_CONFIG;
107
David Somayajuluafaf5a22006-09-19 10:28:00 -0700108static struct scsi_host_template qla4xxx_driver_template = {
109 .module = THIS_MODULE,
110 .name = DRIVER_NAME,
111 .proc_name = DRIVER_NAME,
112 .queuecommand = qla4xxx_queuecommand,
113
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530114 .eh_abort_handler = qla4xxx_eh_abort,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700115 .eh_device_reset_handler = qla4xxx_eh_device_reset,
Mike Christiece545032008-02-29 18:25:20 -0600116 .eh_target_reset_handler = qla4xxx_eh_target_reset,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700117 .eh_host_reset_handler = qla4xxx_eh_host_reset,
Mike Christie5c656af2009-07-15 15:02:59 -0500118 .eh_timed_out = qla4xxx_eh_cmd_timed_out,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700119
120 .slave_configure = qla4xxx_slave_configure,
121 .slave_alloc = qla4xxx_slave_alloc,
122 .slave_destroy = qla4xxx_slave_destroy,
123
Mike Christie921601b2008-01-31 13:36:49 -0600124 .scan_finished = iscsi_scan_finished,
Mike Christie024f8012008-03-04 13:26:54 -0600125 .scan_start = qla4xxx_scan_start,
Mike Christie921601b2008-01-31 13:36:49 -0600126
David Somayajuluafaf5a22006-09-19 10:28:00 -0700127 .this_id = -1,
128 .cmd_per_lun = 3,
129 .use_clustering = ENABLE_CLUSTERING,
130 .sg_tablesize = SG_ALL,
131
132 .max_sectors = 0xFFFF,
Harish Zunjarrao7ad633c2011-05-17 23:17:11 -0700133 .shost_attrs = qla4xxx_host_attrs,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700134};
135
136static struct iscsi_transport qla4xxx_iscsi_transport = {
137 .owner = THIS_MODULE,
138 .name = DRIVER_NAME,
Mike Christied8196ed2007-05-30 12:57:25 -0500139 .caps = CAP_FW_DB | CAP_SENDTARGETS_OFFLOAD |
140 CAP_DATA_PATH_OFFLOAD,
Mike Christie3128c6c2011-07-25 13:48:42 -0500141 .param_mask = ISCSI_TARGET_NAME | ISCSI_TPGT |
Vikas Chaudhary3b2bef12010-07-10 14:51:30 +0530142 ISCSI_TARGET_ALIAS,
Mike Christie8ad57812007-05-30 12:57:13 -0500143 .host_param_mask = ISCSI_HOST_HWADDRESS |
Mike Christie22236962007-05-30 12:57:24 -0500144 ISCSI_HOST_IPADDRESS |
Mike Christie8ad57812007-05-30 12:57:13 -0500145 ISCSI_HOST_INITIATOR_NAME,
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500146 .iface_param_mask = ISCSI_NET_IPV4_ADDR |
147 ISCSI_NET_IPV4_SUBNET |
148 ISCSI_NET_IPV4_GW |
149 ISCSI_NET_IPV4_BOOTPROTO |
150 ISCSI_NET_IFACE_ENABLE |
151 ISCSI_NET_IPV6_LINKLOCAL |
152 ISCSI_NET_IPV6_ADDR |
153 ISCSI_NET_IPV6_ROUTER |
154 ISCSI_NET_IPV6_ADDR_AUTOCFG |
155 ISCSI_NET_IPV6_LINKLOCAL_AUTOCFG |
156 ISCSI_NET_IFACE_ENABLE,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700157 .tgt_dscvr = qla4xxx_tgt_dscvr,
Mike Christie3128c6c2011-07-25 13:48:42 -0500158 .attr_is_visible = ql4_attr_is_visible,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700159 .get_conn_param = qla4xxx_conn_get_param,
160 .get_session_param = qla4xxx_sess_get_param,
Mike Christieaa1e93a2007-05-30 12:57:09 -0500161 .get_host_param = qla4xxx_host_get_param,
Mike Christied00efe32011-07-25 13:48:38 -0500162 .set_iface_param = qla4xxx_iface_set_param,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700163 .session_recovery_timedout = qla4xxx_recovery_timedout,
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500164 .get_iface_param = qla4xxx_get_iface_param,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700165};
166
167static struct scsi_transport_template *qla4xxx_scsi_transport;
168
Mike Christie3128c6c2011-07-25 13:48:42 -0500169static mode_t ql4_attr_is_visible(int param_type, int param)
170{
171 switch (param_type) {
172 case ISCSI_PARAM:
173 switch (param) {
174 case ISCSI_PARAM_CONN_ADDRESS:
175 case ISCSI_PARAM_CONN_PORT:
176 return S_IRUGO;
177 default:
178 return 0;
179 }
180 }
181
182 return 0;
183}
184
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500185static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
186 enum iscsi_param_type param_type,
187 int param, char *buf)
188{
189 struct Scsi_Host *shost = iscsi_iface_to_shost(iface);
190 struct scsi_qla_host *ha = to_qla_host(shost);
191 int len = -ENOSYS;
192
193 if (param_type != ISCSI_NET_PARAM)
194 return -ENOSYS;
195
196 switch (param) {
197 case ISCSI_NET_PARAM_IPV4_ADDR:
198 len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
199 break;
200 case ISCSI_NET_PARAM_IPV4_SUBNET:
201 len = sprintf(buf, "%pI4\n", &ha->ip_config.subnet_mask);
202 break;
203 case ISCSI_NET_PARAM_IPV4_GW:
204 len = sprintf(buf, "%pI4\n", &ha->ip_config.gateway);
205 break;
206 case ISCSI_NET_PARAM_IFACE_ENABLE:
207 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
208 len = sprintf(buf, "%s\n",
209 (ha->ip_config.ipv4_options &
210 IPOPT_IPV4_PROTOCOL_ENABLE) ?
211 "enabled" : "disabled");
212 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
213 len = sprintf(buf, "%s\n",
214 (ha->ip_config.ipv6_options &
215 IPV6_OPT_IPV6_PROTOCOL_ENABLE) ?
216 "enabled" : "disabled");
217 break;
218 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
219 len = sprintf(buf, "%s\n",
220 (ha->ip_config.tcp_options & TCPOPT_DHCP_ENABLE) ?
221 "dhcp" : "static");
222 break;
223 case ISCSI_NET_PARAM_IPV6_ADDR:
224 if (iface->iface_num == 0)
225 len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr0);
226 if (iface->iface_num == 1)
227 len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr1);
228 break;
229 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
230 len = sprintf(buf, "%pI6\n",
231 &ha->ip_config.ipv6_link_local_addr);
232 break;
233 case ISCSI_NET_PARAM_IPV6_ROUTER:
234 len = sprintf(buf, "%pI6\n",
235 &ha->ip_config.ipv6_default_router_addr);
236 break;
237 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
238 len = sprintf(buf, "%s\n",
239 (ha->ip_config.ipv6_addl_options &
240 IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) ?
241 "nd" : "static");
242 break;
243 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
244 len = sprintf(buf, "%s\n",
245 (ha->ip_config.ipv6_addl_options &
246 IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR) ?
247 "auto" : "static");
248 break;
249 default:
250 len = -ENOSYS;
251 }
252
253 return len;
254}
255
Mike Christie5c656af2009-07-15 15:02:59 -0500256static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
257{
258 struct iscsi_cls_session *session;
259 struct ddb_entry *ddb_entry;
260
261 session = starget_to_session(scsi_target(sc->device));
262 ddb_entry = session->dd_data;
263
264 /* if we are not logged in then the LLD is going to clean up the cmd */
265 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
266 return BLK_EH_RESET_TIMER;
267 else
268 return BLK_EH_NOT_HANDLED;
269}
270
David Somayajuluafaf5a22006-09-19 10:28:00 -0700271static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session)
272{
273 struct ddb_entry *ddb_entry = session->dd_data;
274 struct scsi_qla_host *ha = ddb_entry->ha;
275
Mike Christie568d3032008-01-31 13:36:47 -0600276 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
277 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700278
Vikas Chaudhary3013cea2010-07-30 14:25:46 +0530279 DEBUG2(printk("scsi%ld: %s: ddb [%d] session recovery timeout "
Mike Christie568d3032008-01-31 13:36:47 -0600280 "of (%d) secs exhausted, marking device DEAD.\n",
281 ha->host_no, __func__, ddb_entry->fw_ddb_index,
Vikas Chaudhary30387272011-03-21 03:34:32 -0700282 ddb_entry->sess->recovery_tmo));
Mike Christie568d3032008-01-31 13:36:47 -0600283 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700284}
285
Mike Christieaa1e93a2007-05-30 12:57:09 -0500286static int qla4xxx_host_get_param(struct Scsi_Host *shost,
287 enum iscsi_host_param param, char *buf)
288{
289 struct scsi_qla_host *ha = to_qla_host(shost);
290 int len;
291
292 switch (param) {
293 case ISCSI_HOST_PARAM_HWADDRESS:
Michael Chan7ffc49a2007-12-24 21:28:09 -0800294 len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
Mike Christieaa1e93a2007-05-30 12:57:09 -0500295 break;
Mike Christie22236962007-05-30 12:57:24 -0500296 case ISCSI_HOST_PARAM_IPADDRESS:
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500297 len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
Mike Christie22236962007-05-30 12:57:24 -0500298 break;
Mike Christie8ad57812007-05-30 12:57:13 -0500299 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie22236962007-05-30 12:57:24 -0500300 len = sprintf(buf, "%s\n", ha->name_string);
Mike Christie8ad57812007-05-30 12:57:13 -0500301 break;
Mike Christieaa1e93a2007-05-30 12:57:09 -0500302 default:
303 return -ENOSYS;
304 }
305
306 return len;
307}
308
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500309static void qla4xxx_create_ipv4_iface(struct scsi_qla_host *ha)
310{
311 if (ha->iface_ipv4)
312 return;
313
314 /* IPv4 */
315 ha->iface_ipv4 = iscsi_create_iface(ha->host,
316 &qla4xxx_iscsi_transport,
317 ISCSI_IFACE_TYPE_IPV4, 0, 0);
318 if (!ha->iface_ipv4)
319 ql4_printk(KERN_ERR, ha, "Could not create IPv4 iSCSI "
320 "iface0.\n");
321}
322
323static void qla4xxx_create_ipv6_iface(struct scsi_qla_host *ha)
324{
325 if (!ha->iface_ipv6_0)
326 /* IPv6 iface-0 */
327 ha->iface_ipv6_0 = iscsi_create_iface(ha->host,
328 &qla4xxx_iscsi_transport,
329 ISCSI_IFACE_TYPE_IPV6, 0,
330 0);
331 if (!ha->iface_ipv6_0)
332 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
333 "iface0.\n");
334
335 if (!ha->iface_ipv6_1)
336 /* IPv6 iface-1 */
337 ha->iface_ipv6_1 = iscsi_create_iface(ha->host,
338 &qla4xxx_iscsi_transport,
339 ISCSI_IFACE_TYPE_IPV6, 1,
340 0);
341 if (!ha->iface_ipv6_1)
342 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
343 "iface1.\n");
344}
345
346static void qla4xxx_create_ifaces(struct scsi_qla_host *ha)
347{
348 if (ha->ip_config.ipv4_options & IPOPT_IPV4_PROTOCOL_ENABLE)
349 qla4xxx_create_ipv4_iface(ha);
350
351 if (ha->ip_config.ipv6_options & IPV6_OPT_IPV6_PROTOCOL_ENABLE)
352 qla4xxx_create_ipv6_iface(ha);
353}
354
355static void qla4xxx_destroy_ipv4_iface(struct scsi_qla_host *ha)
356{
357 if (ha->iface_ipv4) {
358 iscsi_destroy_iface(ha->iface_ipv4);
359 ha->iface_ipv4 = NULL;
360 }
361}
362
363static void qla4xxx_destroy_ipv6_iface(struct scsi_qla_host *ha)
364{
365 if (ha->iface_ipv6_0) {
366 iscsi_destroy_iface(ha->iface_ipv6_0);
367 ha->iface_ipv6_0 = NULL;
368 }
369 if (ha->iface_ipv6_1) {
370 iscsi_destroy_iface(ha->iface_ipv6_1);
371 ha->iface_ipv6_1 = NULL;
372 }
373}
374
375static void qla4xxx_destroy_ifaces(struct scsi_qla_host *ha)
376{
377 qla4xxx_destroy_ipv4_iface(ha);
378 qla4xxx_destroy_ipv6_iface(ha);
379}
380
Mike Christied00efe32011-07-25 13:48:38 -0500381static void qla4xxx_set_ipv6(struct scsi_qla_host *ha,
382 struct iscsi_iface_param_info *iface_param,
383 struct addr_ctrl_blk *init_fw_cb)
384{
385 /*
386 * iface_num 0 is valid for IPv6 Addr, linklocal, router, autocfg.
387 * iface_num 1 is valid only for IPv6 Addr.
388 */
389 switch (iface_param->param) {
390 case ISCSI_NET_PARAM_IPV6_ADDR:
391 if (iface_param->iface_num & 0x1)
392 /* IPv6 Addr 1 */
393 memcpy(init_fw_cb->ipv6_addr1, iface_param->value,
394 sizeof(init_fw_cb->ipv6_addr1));
395 else
396 /* IPv6 Addr 0 */
397 memcpy(init_fw_cb->ipv6_addr0, iface_param->value,
398 sizeof(init_fw_cb->ipv6_addr0));
399 break;
400 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
401 if (iface_param->iface_num & 0x1)
402 break;
403 memcpy(init_fw_cb->ipv6_if_id, &iface_param->value[8],
404 sizeof(init_fw_cb->ipv6_if_id));
405 break;
406 case ISCSI_NET_PARAM_IPV6_ROUTER:
407 if (iface_param->iface_num & 0x1)
408 break;
409 memcpy(init_fw_cb->ipv6_dflt_rtr_addr, iface_param->value,
410 sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
411 break;
412 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
413 /* Autocfg applies to even interface */
414 if (iface_param->iface_num & 0x1)
415 break;
416
417 if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_DISABLE)
418 init_fw_cb->ipv6_addtl_opts &=
419 cpu_to_le16(
420 ~IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
421 else if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_ND_ENABLE)
422 init_fw_cb->ipv6_addtl_opts |=
423 cpu_to_le16(
424 IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
425 else
426 ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
427 "IPv6 addr\n");
428 break;
429 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
430 /* Autocfg applies to even interface */
431 if (iface_param->iface_num & 0x1)
432 break;
433
434 if (iface_param->value[0] ==
435 ISCSI_IPV6_LINKLOCAL_AUTOCFG_ENABLE)
436 init_fw_cb->ipv6_addtl_opts |= cpu_to_le16(
437 IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
438 else if (iface_param->value[0] ==
439 ISCSI_IPV6_LINKLOCAL_AUTOCFG_DISABLE)
440 init_fw_cb->ipv6_addtl_opts &= cpu_to_le16(
441 ~IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
442 else
443 ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
444 "IPv6 linklocal addr\n");
445 break;
446 case ISCSI_NET_PARAM_IPV6_ROUTER_AUTOCFG:
447 /* Autocfg applies to even interface */
448 if (iface_param->iface_num & 0x1)
449 break;
450
451 if (iface_param->value[0] == ISCSI_IPV6_ROUTER_AUTOCFG_ENABLE)
452 memset(init_fw_cb->ipv6_dflt_rtr_addr, 0,
453 sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
454 break;
455 case ISCSI_NET_PARAM_IFACE_ENABLE:
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500456 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
Mike Christied00efe32011-07-25 13:48:38 -0500457 init_fw_cb->ipv6_opts |=
458 cpu_to_le16(IPV6_OPT_IPV6_PROTOCOL_ENABLE);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500459 qla4xxx_create_ipv6_iface(ha);
460 } else {
Mike Christied00efe32011-07-25 13:48:38 -0500461 init_fw_cb->ipv6_opts &=
462 cpu_to_le16(~IPV6_OPT_IPV6_PROTOCOL_ENABLE &
463 0xFFFF);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500464 qla4xxx_destroy_ipv6_iface(ha);
465 }
Mike Christied00efe32011-07-25 13:48:38 -0500466 break;
467 case ISCSI_NET_PARAM_VLAN_ID:
468 if (iface_param->len != sizeof(init_fw_cb->ipv6_vlan_tag))
469 break;
470 init_fw_cb->ipv6_vlan_tag = *(uint16_t *)iface_param->value;
471 break;
472 default:
473 ql4_printk(KERN_ERR, ha, "Unknown IPv6 param = %d\n",
474 iface_param->param);
475 break;
476 }
477}
478
479static void qla4xxx_set_ipv4(struct scsi_qla_host *ha,
480 struct iscsi_iface_param_info *iface_param,
481 struct addr_ctrl_blk *init_fw_cb)
482{
483 switch (iface_param->param) {
484 case ISCSI_NET_PARAM_IPV4_ADDR:
485 memcpy(init_fw_cb->ipv4_addr, iface_param->value,
486 sizeof(init_fw_cb->ipv4_addr));
487 break;
488 case ISCSI_NET_PARAM_IPV4_SUBNET:
489 memcpy(init_fw_cb->ipv4_subnet, iface_param->value,
490 sizeof(init_fw_cb->ipv4_subnet));
491 break;
492 case ISCSI_NET_PARAM_IPV4_GW:
493 memcpy(init_fw_cb->ipv4_gw_addr, iface_param->value,
494 sizeof(init_fw_cb->ipv4_gw_addr));
495 break;
496 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
497 if (iface_param->value[0] == ISCSI_BOOTPROTO_DHCP)
498 init_fw_cb->ipv4_tcp_opts |=
499 cpu_to_le16(TCPOPT_DHCP_ENABLE);
500 else if (iface_param->value[0] == ISCSI_BOOTPROTO_STATIC)
501 init_fw_cb->ipv4_tcp_opts &=
502 cpu_to_le16(~TCPOPT_DHCP_ENABLE);
503 else
504 ql4_printk(KERN_ERR, ha, "Invalid IPv4 bootproto\n");
505 break;
506 case ISCSI_NET_PARAM_IFACE_ENABLE:
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500507 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
Mike Christied00efe32011-07-25 13:48:38 -0500508 init_fw_cb->ipv4_ip_opts |=
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500509 cpu_to_le16(IPOPT_IPV4_PROTOCOL_ENABLE);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500510 qla4xxx_create_ipv4_iface(ha);
511 } else {
Mike Christied00efe32011-07-25 13:48:38 -0500512 init_fw_cb->ipv4_ip_opts &=
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500513 cpu_to_le16(~IPOPT_IPV4_PROTOCOL_ENABLE &
Mike Christied00efe32011-07-25 13:48:38 -0500514 0xFFFF);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500515 qla4xxx_destroy_ipv4_iface(ha);
516 }
Mike Christied00efe32011-07-25 13:48:38 -0500517 break;
518 case ISCSI_NET_PARAM_VLAN_ID:
519 if (iface_param->len != sizeof(init_fw_cb->ipv4_vlan_tag))
520 break;
521 init_fw_cb->ipv4_vlan_tag = *(uint16_t *)iface_param->value;
522 break;
523 default:
524 ql4_printk(KERN_ERR, ha, "Unknown IPv4 param = %d\n",
525 iface_param->param);
526 break;
527 }
528}
529
530static void
531qla4xxx_initcb_to_acb(struct addr_ctrl_blk *init_fw_cb)
532{
533 struct addr_ctrl_blk_def *acb;
534 acb = (struct addr_ctrl_blk_def *)init_fw_cb;
535 memset(acb->reserved1, 0, sizeof(acb->reserved1));
536 memset(acb->reserved2, 0, sizeof(acb->reserved2));
537 memset(acb->reserved3, 0, sizeof(acb->reserved3));
538 memset(acb->reserved4, 0, sizeof(acb->reserved4));
539 memset(acb->reserved5, 0, sizeof(acb->reserved5));
540 memset(acb->reserved6, 0, sizeof(acb->reserved6));
541 memset(acb->reserved7, 0, sizeof(acb->reserved7));
542 memset(acb->reserved8, 0, sizeof(acb->reserved8));
543 memset(acb->reserved9, 0, sizeof(acb->reserved9));
544 memset(acb->reserved10, 0, sizeof(acb->reserved10));
545 memset(acb->reserved11, 0, sizeof(acb->reserved11));
546 memset(acb->reserved12, 0, sizeof(acb->reserved12));
547 memset(acb->reserved13, 0, sizeof(acb->reserved13));
548 memset(acb->reserved14, 0, sizeof(acb->reserved14));
549 memset(acb->reserved15, 0, sizeof(acb->reserved15));
550}
551
552static int
553qla4xxx_iface_set_param(struct Scsi_Host *shost, char *data, int count)
554{
555 struct scsi_qla_host *ha = to_qla_host(shost);
556 int rval = 0;
557 struct iscsi_iface_param_info *iface_param = NULL;
558 struct addr_ctrl_blk *init_fw_cb = NULL;
559 dma_addr_t init_fw_cb_dma;
560 uint32_t mbox_cmd[MBOX_REG_COUNT];
561 uint32_t mbox_sts[MBOX_REG_COUNT];
562 uint32_t total_param_count;
563 uint32_t length;
564
565 init_fw_cb = dma_alloc_coherent(&ha->pdev->dev,
566 sizeof(struct addr_ctrl_blk),
567 &init_fw_cb_dma, GFP_KERNEL);
568 if (!init_fw_cb) {
569 ql4_printk(KERN_ERR, ha, "%s: Unable to alloc init_cb\n",
570 __func__);
571 return -ENOMEM;
572 }
573
574 memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
575 memset(&mbox_cmd, 0, sizeof(mbox_cmd));
576 memset(&mbox_sts, 0, sizeof(mbox_sts));
577
578 if (qla4xxx_get_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma)) {
579 ql4_printk(KERN_ERR, ha, "%s: get ifcb failed\n", __func__);
580 rval = -EIO;
581 goto exit_init_fw_cb;
582 }
583
584 total_param_count = count;
585 iface_param = (struct iscsi_iface_param_info *)data;
586
587 for ( ; total_param_count != 0; total_param_count--) {
588 length = iface_param->len;
589
590 if (iface_param->param_type != ISCSI_NET_PARAM)
591 continue;
592
593 switch (iface_param->iface_type) {
594 case ISCSI_IFACE_TYPE_IPV4:
595 switch (iface_param->iface_num) {
596 case 0:
597 qla4xxx_set_ipv4(ha, iface_param, init_fw_cb);
598 break;
599 default:
600 /* Cannot have more than one IPv4 interface */
601 ql4_printk(KERN_ERR, ha, "Invalid IPv4 iface "
602 "number = %d\n",
603 iface_param->iface_num);
604 break;
605 }
606 break;
607 case ISCSI_IFACE_TYPE_IPV6:
608 switch (iface_param->iface_num) {
609 case 0:
610 case 1:
611 qla4xxx_set_ipv6(ha, iface_param, init_fw_cb);
612 break;
613 default:
614 /* Cannot have more than two IPv6 interface */
615 ql4_printk(KERN_ERR, ha, "Invalid IPv6 iface "
616 "number = %d\n",
617 iface_param->iface_num);
618 break;
619 }
620 break;
621 default:
622 ql4_printk(KERN_ERR, ha, "Invalid iface type\n");
623 break;
624 }
625
626 iface_param = (struct iscsi_iface_param_info *)
627 ((uint8_t *)iface_param +
628 sizeof(struct iscsi_iface_param_info) + length);
629 }
630
631 init_fw_cb->cookie = cpu_to_le32(0x11BEAD5A);
632
633 rval = qla4xxx_set_flash(ha, init_fw_cb_dma, FLASH_SEGMENT_IFCB,
634 sizeof(struct addr_ctrl_blk),
635 FLASH_OPT_RMW_COMMIT);
636 if (rval != QLA_SUCCESS) {
637 ql4_printk(KERN_ERR, ha, "%s: set flash mbx failed\n",
638 __func__);
639 rval = -EIO;
640 goto exit_init_fw_cb;
641 }
642
643 qla4xxx_disable_acb(ha);
644
645 qla4xxx_initcb_to_acb(init_fw_cb);
646
647 rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma);
648 if (rval != QLA_SUCCESS) {
649 ql4_printk(KERN_ERR, ha, "%s: set acb mbx failed\n",
650 __func__);
651 rval = -EIO;
652 goto exit_init_fw_cb;
653 }
654
655 memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
656 qla4xxx_update_local_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb,
657 init_fw_cb_dma);
658
659exit_init_fw_cb:
660 dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk),
661 init_fw_cb, init_fw_cb_dma);
662
663 return rval;
664}
665
David Somayajuluafaf5a22006-09-19 10:28:00 -0700666static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
667 enum iscsi_param param, char *buf)
668{
669 struct ddb_entry *ddb_entry = sess->dd_data;
670 int len;
671
672 switch (param) {
673 case ISCSI_PARAM_TARGET_NAME:
674 len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
675 ddb_entry->iscsi_name);
676 break;
677 case ISCSI_PARAM_TPGT:
678 len = sprintf(buf, "%u\n", ddb_entry->tpgt);
679 break;
Vikas Chaudhary3b2bef12010-07-10 14:51:30 +0530680 case ISCSI_PARAM_TARGET_ALIAS:
681 len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
682 ddb_entry->iscsi_alias);
683 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700684 default:
685 return -ENOSYS;
686 }
687
688 return len;
689}
690
691static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
692 enum iscsi_param param, char *buf)
693{
694 struct iscsi_cls_session *session;
695 struct ddb_entry *ddb_entry;
696 int len;
697
698 session = iscsi_dev_to_session(conn->dev.parent);
699 ddb_entry = session->dd_data;
700
701 switch (param) {
702 case ISCSI_PARAM_CONN_PORT:
703 len = sprintf(buf, "%hu\n", ddb_entry->port);
704 break;
705 case ISCSI_PARAM_CONN_ADDRESS:
706 /* TODO: what are the ipv6 bits */
Harvey Harrison63779432008-10-31 00:56:00 -0700707 len = sprintf(buf, "%pI4\n", &ddb_entry->ip_addr);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700708 break;
709 default:
710 return -ENOSYS;
711 }
712
713 return len;
714}
715
Mike Christie2174a042007-05-30 12:57:10 -0500716static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
717 enum iscsi_tgt_dscvr type, uint32_t enable,
718 struct sockaddr *dst_addr)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700719{
720 struct scsi_qla_host *ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700721 struct sockaddr_in *addr;
722 struct sockaddr_in6 *addr6;
723 int ret = 0;
724
David Somayajuluafaf5a22006-09-19 10:28:00 -0700725 ha = (struct scsi_qla_host *) shost->hostdata;
726
727 switch (type) {
728 case ISCSI_TGT_DSCVR_SEND_TARGETS:
729 if (dst_addr->sa_family == AF_INET) {
730 addr = (struct sockaddr_in *)dst_addr;
731 if (qla4xxx_send_tgts(ha, (char *)&addr->sin_addr,
732 addr->sin_port) != QLA_SUCCESS)
733 ret = -EIO;
734 } else if (dst_addr->sa_family == AF_INET6) {
735 /*
736 * TODO: fix qla4xxx_send_tgts
737 */
738 addr6 = (struct sockaddr_in6 *)dst_addr;
739 if (qla4xxx_send_tgts(ha, (char *)&addr6->sin6_addr,
740 addr6->sin6_port) != QLA_SUCCESS)
741 ret = -EIO;
742 } else
743 ret = -ENOSYS;
744 break;
745 default:
746 ret = -ENOSYS;
747 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700748 return ret;
749}
750
751void qla4xxx_destroy_sess(struct ddb_entry *ddb_entry)
752{
753 if (!ddb_entry->sess)
754 return;
755
756 if (ddb_entry->conn) {
Mike Christie26974782007-12-13 12:43:29 -0600757 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700758 iscsi_remove_session(ddb_entry->sess);
759 }
760 iscsi_free_session(ddb_entry->sess);
761}
762
763int qla4xxx_add_sess(struct ddb_entry *ddb_entry)
764{
765 int err;
766
Vikas Chaudhary30387272011-03-21 03:34:32 -0700767 ddb_entry->sess->recovery_tmo = ql4xsess_recovery_tmo;
Vikas Chaudhary3013cea2010-07-30 14:25:46 +0530768
David Somayajuluafaf5a22006-09-19 10:28:00 -0700769 err = iscsi_add_session(ddb_entry->sess, ddb_entry->fw_ddb_index);
770 if (err) {
771 DEBUG2(printk(KERN_ERR "Could not add session.\n"));
772 return err;
773 }
774
Mike Christie5d91e202008-05-21 15:54:01 -0500775 ddb_entry->conn = iscsi_create_conn(ddb_entry->sess, 0, 0);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700776 if (!ddb_entry->conn) {
777 iscsi_remove_session(ddb_entry->sess);
778 DEBUG2(printk(KERN_ERR "Could not add connection.\n"));
779 return -ENOMEM;
780 }
Mike Christieb6359302008-01-31 13:36:44 -0600781
782 /* finally ready to go */
783 iscsi_unblock_session(ddb_entry->sess);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700784 return 0;
785}
786
787struct ddb_entry *qla4xxx_alloc_sess(struct scsi_qla_host *ha)
788{
789 struct ddb_entry *ddb_entry;
790 struct iscsi_cls_session *sess;
791
Mike Christie5d91e202008-05-21 15:54:01 -0500792 sess = iscsi_alloc_session(ha->host, &qla4xxx_iscsi_transport,
793 sizeof(struct ddb_entry));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700794 if (!sess)
795 return NULL;
796
797 ddb_entry = sess->dd_data;
798 memset(ddb_entry, 0, sizeof(*ddb_entry));
799 ddb_entry->ha = ha;
800 ddb_entry->sess = sess;
801 return ddb_entry;
802}
803
Mike Christie024f8012008-03-04 13:26:54 -0600804static void qla4xxx_scan_start(struct Scsi_Host *shost)
805{
806 struct scsi_qla_host *ha = shost_priv(shost);
807 struct ddb_entry *ddb_entry, *ddbtemp;
808
809 /* finish setup of sessions that were already setup in firmware */
810 list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
811 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE)
812 qla4xxx_add_sess(ddb_entry);
813 }
814}
815
David Somayajuluafaf5a22006-09-19 10:28:00 -0700816/*
817 * Timer routines
818 */
819
820static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
821 unsigned long interval)
822{
823 DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
824 __func__, ha->host->host_no));
825 init_timer(&ha->timer);
826 ha->timer.expires = jiffies + interval * HZ;
827 ha->timer.data = (unsigned long)ha;
828 ha->timer.function = (void (*)(unsigned long))func;
829 add_timer(&ha->timer);
830 ha->timer_active = 1;
831}
832
833static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
834{
835 del_timer_sync(&ha->timer);
836 ha->timer_active = 0;
837}
838
839/***
840 * qla4xxx_mark_device_missing - mark a device as missing.
841 * @ha: Pointer to host adapter structure.
842 * @ddb_entry: Pointer to device database entry
843 *
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530844 * This routine marks a device missing and close connection.
David Somayajuluafaf5a22006-09-19 10:28:00 -0700845 **/
846void qla4xxx_mark_device_missing(struct scsi_qla_host *ha,
847 struct ddb_entry *ddb_entry)
848{
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530849 if ((atomic_read(&ddb_entry->state) != DDB_STATE_DEAD)) {
850 atomic_set(&ddb_entry->state, DDB_STATE_MISSING);
851 DEBUG2(printk("scsi%ld: ddb [%d] marked MISSING\n",
852 ha->host_no, ddb_entry->fw_ddb_index));
853 } else
854 DEBUG2(printk("scsi%ld: ddb [%d] DEAD\n", ha->host_no,
855 ddb_entry->fw_ddb_index))
856
Mike Christieb6359302008-01-31 13:36:44 -0600857 iscsi_block_session(ddb_entry->sess);
Mike Christiee5bd7b52008-09-24 11:46:10 -0500858 iscsi_conn_error_event(ddb_entry->conn, ISCSI_ERR_CONN_FAILED);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700859}
860
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530861/**
862 * qla4xxx_mark_all_devices_missing - mark all devices as missing.
863 * @ha: Pointer to host adapter structure.
864 *
865 * This routine marks a device missing and resets the relogin retry count.
866 **/
867void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
868{
869 struct ddb_entry *ddb_entry, *ddbtemp;
870 list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
871 qla4xxx_mark_device_missing(ha, ddb_entry);
872 }
873}
874
David Somayajuluafaf5a22006-09-19 10:28:00 -0700875static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
876 struct ddb_entry *ddb_entry,
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -0700877 struct scsi_cmnd *cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700878{
879 struct srb *srb;
880
881 srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
882 if (!srb)
883 return srb;
884
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530885 kref_init(&srb->srb_ref);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700886 srb->ha = ha;
887 srb->ddb = ddb_entry;
888 srb->cmd = cmd;
889 srb->flags = 0;
Vikas Chaudhary53698872010-04-28 11:41:59 +0530890 CMD_SP(cmd) = (void *)srb;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700891
892 return srb;
893}
894
895static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
896{
897 struct scsi_cmnd *cmd = srb->cmd;
898
899 if (srb->flags & SRB_DMA_VALID) {
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +0900900 scsi_dma_unmap(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700901 srb->flags &= ~SRB_DMA_VALID;
902 }
Vikas Chaudhary53698872010-04-28 11:41:59 +0530903 CMD_SP(cmd) = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700904}
905
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530906void qla4xxx_srb_compl(struct kref *ref)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700907{
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530908 struct srb *srb = container_of(ref, struct srb, srb_ref);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700909 struct scsi_cmnd *cmd = srb->cmd;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530910 struct scsi_qla_host *ha = srb->ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700911
912 qla4xxx_srb_free_dma(ha, srb);
913
914 mempool_free(srb, ha->srb_mempool);
915
916 cmd->scsi_done(cmd);
917}
918
919/**
920 * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -0700921 * @host: scsi host
David Somayajuluafaf5a22006-09-19 10:28:00 -0700922 * @cmd: Pointer to Linux's SCSI command structure
David Somayajuluafaf5a22006-09-19 10:28:00 -0700923 *
924 * Remarks:
925 * This routine is invoked by Linux to send a SCSI command to the driver.
926 * The mid-level driver tries to ensure that queuecommand never gets
927 * invoked concurrently with itself or the interrupt handler (although
928 * the interrupt handler may call this routine as part of request-
929 * completion handling). Unfortunely, it sometimes calls the scheduler
930 * in interrupt context which is a big NO! NO!.
931 **/
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -0700932static int qla4xxx_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700933{
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -0700934 struct scsi_qla_host *ha = to_qla_host(host);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700935 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christie7fb19212008-01-31 13:36:45 -0600936 struct iscsi_cls_session *sess = ddb_entry->sess;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700937 struct srb *srb;
938 int rval;
939
Lalit Chandivade2232be02010-07-30 14:38:47 +0530940 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
941 if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
942 cmd->result = DID_NO_CONNECT << 16;
943 else
944 cmd->result = DID_REQUEUE << 16;
945 goto qc_fail_command;
946 }
947
Mike Christie7fb19212008-01-31 13:36:45 -0600948 if (!sess) {
949 cmd->result = DID_IMM_RETRY << 16;
950 goto qc_fail_command;
951 }
952
953 rval = iscsi_session_chkready(sess);
954 if (rval) {
955 cmd->result = rval;
956 goto qc_fail_command;
957 }
958
David Somayajuluafaf5a22006-09-19 10:28:00 -0700959 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
960 if (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD) {
961 cmd->result = DID_NO_CONNECT << 16;
962 goto qc_fail_command;
963 }
Mike Christiec5e98e92008-08-17 15:24:39 -0500964 return SCSI_MLQUEUE_TARGET_BUSY;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700965 }
966
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530967 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
968 test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
969 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
970 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
971 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
972 !test_bit(AF_ONLINE, &ha->flags) ||
973 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
David C Somayajulu477ffb92007-01-22 12:26:11 -0800974 goto qc_host_busy;
975
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -0700976 srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700977 if (!srb)
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -0700978 goto qc_host_busy;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700979
980 rval = qla4xxx_send_command_to_isp(ha, srb);
981 if (rval != QLA_SUCCESS)
982 goto qc_host_busy_free_sp;
983
David Somayajuluafaf5a22006-09-19 10:28:00 -0700984 return 0;
985
986qc_host_busy_free_sp:
987 qla4xxx_srb_free_dma(ha, srb);
988 mempool_free(srb, ha->srb_mempool);
989
David Somayajuluafaf5a22006-09-19 10:28:00 -0700990qc_host_busy:
991 return SCSI_MLQUEUE_HOST_BUSY;
992
993qc_fail_command:
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -0700994 cmd->scsi_done(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700995
996 return 0;
997}
998
999/**
1000 * qla4xxx_mem_free - frees memory allocated to adapter
1001 * @ha: Pointer to host adapter structure.
1002 *
1003 * Frees memory previously allocated by qla4xxx_mem_alloc
1004 **/
1005static void qla4xxx_mem_free(struct scsi_qla_host *ha)
1006{
1007 if (ha->queues)
1008 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
1009 ha->queues_dma);
1010
1011 ha->queues_len = 0;
1012 ha->queues = NULL;
1013 ha->queues_dma = 0;
1014 ha->request_ring = NULL;
1015 ha->request_dma = 0;
1016 ha->response_ring = NULL;
1017 ha->response_dma = 0;
1018 ha->shadow_regs = NULL;
1019 ha->shadow_regs_dma = 0;
1020
1021 /* Free srb pool. */
1022 if (ha->srb_mempool)
1023 mempool_destroy(ha->srb_mempool);
1024
1025 ha->srb_mempool = NULL;
1026
1027 /* release io space registers */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301028 if (is_qla8022(ha)) {
1029 if (ha->nx_pcibase)
1030 iounmap(
1031 (struct device_reg_82xx __iomem *)ha->nx_pcibase);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301032 } else if (ha->reg)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001033 iounmap(ha->reg);
1034 pci_release_regions(ha->pdev);
1035}
1036
1037/**
1038 * qla4xxx_mem_alloc - allocates memory for use by adapter.
1039 * @ha: Pointer to host adapter structure
1040 *
1041 * Allocates DMA memory for request and response queues. Also allocates memory
1042 * for srbs.
1043 **/
1044static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
1045{
1046 unsigned long align;
1047
1048 /* Allocate contiguous block of DMA memory for queues. */
1049 ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
1050 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
1051 sizeof(struct shadow_regs) +
1052 MEM_ALIGN_VALUE +
1053 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
1054 ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
1055 &ha->queues_dma, GFP_KERNEL);
1056 if (ha->queues == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301057 ql4_printk(KERN_WARNING, ha,
1058 "Memory Allocation failed - queues.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001059
1060 goto mem_alloc_error_exit;
1061 }
1062 memset(ha->queues, 0, ha->queues_len);
1063
1064 /*
1065 * As per RISC alignment requirements -- the bus-address must be a
1066 * multiple of the request-ring size (in bytes).
1067 */
1068 align = 0;
1069 if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
1070 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
1071 (MEM_ALIGN_VALUE - 1));
1072
1073 /* Update request and response queue pointers. */
1074 ha->request_dma = ha->queues_dma + align;
1075 ha->request_ring = (struct queue_entry *) (ha->queues + align);
1076 ha->response_dma = ha->queues_dma + align +
1077 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
1078 ha->response_ring = (struct queue_entry *) (ha->queues + align +
1079 (REQUEST_QUEUE_DEPTH *
1080 QUEUE_SIZE));
1081 ha->shadow_regs_dma = ha->queues_dma + align +
1082 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
1083 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
1084 ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
1085 (REQUEST_QUEUE_DEPTH *
1086 QUEUE_SIZE) +
1087 (RESPONSE_QUEUE_DEPTH *
1088 QUEUE_SIZE));
1089
1090 /* Allocate memory for srb pool. */
1091 ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
1092 mempool_free_slab, srb_cachep);
1093 if (ha->srb_mempool == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301094 ql4_printk(KERN_WARNING, ha,
1095 "Memory Allocation failed - SRB Pool.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001096
1097 goto mem_alloc_error_exit;
1098 }
1099
1100 return QLA_SUCCESS;
1101
1102mem_alloc_error_exit:
1103 qla4xxx_mem_free(ha);
1104 return QLA_ERROR;
1105}
1106
1107/**
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301108 * qla4_8xxx_check_fw_alive - Check firmware health
1109 * @ha: Pointer to host adapter structure.
1110 *
1111 * Context: Interrupt
1112 **/
1113static void qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
1114{
1115 uint32_t fw_heartbeat_counter, halt_status;
1116
1117 fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
Lalit Chandivade2232be02010-07-30 14:38:47 +05301118 /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
1119 if (fw_heartbeat_counter == 0xffffffff) {
1120 DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
1121 "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
1122 ha->host_no, __func__));
1123 return;
1124 }
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301125
1126 if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
1127 ha->seconds_since_last_heartbeat++;
1128 /* FW not alive after 2 seconds */
1129 if (ha->seconds_since_last_heartbeat == 2) {
1130 ha->seconds_since_last_heartbeat = 0;
1131 halt_status = qla4_8xxx_rd_32(ha,
Vikas Chaudhary68d92eb2011-05-17 23:17:05 -07001132 QLA82XX_PEG_HALT_STATUS1);
1133
1134 ql4_printk(KERN_INFO, ha,
1135 "scsi(%ld): %s, Dumping hw/fw registers:\n "
1136 " PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2:"
1137 " 0x%x,\n PEG_NET_0_PC: 0x%x, PEG_NET_1_PC:"
1138 " 0x%x,\n PEG_NET_2_PC: 0x%x, PEG_NET_3_PC:"
1139 " 0x%x,\n PEG_NET_4_PC: 0x%x\n",
1140 ha->host_no, __func__, halt_status,
1141 qla4_8xxx_rd_32(ha,
1142 QLA82XX_PEG_HALT_STATUS2),
1143 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_0 +
1144 0x3c),
1145 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_1 +
1146 0x3c),
1147 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_2 +
1148 0x3c),
1149 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_3 +
1150 0x3c),
1151 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_4 +
1152 0x3c));
Nilesh Javali21033632010-07-30 14:28:07 +05301153
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301154 /* Since we cannot change dev_state in interrupt
1155 * context, set appropriate DPC flag then wakeup
1156 * DPC */
1157 if (halt_status & HALT_STATUS_UNRECOVERABLE)
1158 set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
1159 else {
1160 printk("scsi%ld: %s: detect abort needed!\n",
1161 ha->host_no, __func__);
1162 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1163 }
1164 qla4xxx_wake_dpc(ha);
Nilesh Javali21033632010-07-30 14:28:07 +05301165 qla4xxx_mailbox_premature_completion(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301166 }
Lalit Chandivade99457d72010-10-06 22:49:32 -07001167 } else
1168 ha->seconds_since_last_heartbeat = 0;
1169
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301170 ha->fw_heartbeat_counter = fw_heartbeat_counter;
1171}
1172
1173/**
1174 * qla4_8xxx_watchdog - Poll dev state
1175 * @ha: Pointer to host adapter structure.
1176 *
1177 * Context: Interrupt
1178 **/
1179void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
1180{
1181 uint32_t dev_state;
1182
1183 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
1184
1185 /* don't poll if reset is going on */
Lalit Chandivaded56a1f72010-12-02 22:12:45 -08001186 if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
1187 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
Vikas Chaudhary977f46a2011-05-17 23:17:08 -07001188 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags))) {
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301189 if (dev_state == QLA82XX_DEV_NEED_RESET &&
1190 !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
Vikas Chaudhary3930b8c2010-12-02 22:12:47 -08001191 if (!ql4xdontresethba) {
1192 ql4_printk(KERN_INFO, ha, "%s: HW State: "
1193 "NEED RESET!\n", __func__);
1194 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1195 qla4xxx_wake_dpc(ha);
1196 qla4xxx_mailbox_premature_completion(ha);
1197 }
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301198 } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
1199 !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
Vikas Chaudhary3930b8c2010-12-02 22:12:47 -08001200 ql4_printk(KERN_INFO, ha, "%s: HW State: NEED QUIES!\n",
1201 __func__);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301202 set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
1203 qla4xxx_wake_dpc(ha);
1204 } else {
1205 /* Check firmware health */
1206 qla4_8xxx_check_fw_alive(ha);
1207 }
1208 }
1209}
1210
1211/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07001212 * qla4xxx_timer - checks every second for work to do.
1213 * @ha: Pointer to host adapter structure.
1214 **/
1215static void qla4xxx_timer(struct scsi_qla_host *ha)
1216{
1217 struct ddb_entry *ddb_entry, *dtemp;
1218 int start_dpc = 0;
Lalit Chandivade2232be02010-07-30 14:38:47 +05301219 uint16_t w;
1220
1221 /* If we are in the middle of AER/EEH processing
1222 * skip any processing and reschedule the timer
1223 */
1224 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1225 mod_timer(&ha->timer, jiffies + HZ);
1226 return;
1227 }
1228
1229 /* Hardware read to trigger an EEH error during mailbox waits. */
1230 if (!pci_channel_offline(ha->pdev))
1231 pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001232
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301233 if (is_qla8022(ha)) {
1234 qla4_8xxx_watchdog(ha);
1235 }
1236
David Somayajuluafaf5a22006-09-19 10:28:00 -07001237 /* Search for relogin's to time-out and port down retry. */
1238 list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
1239 /* Count down time between sending relogins */
1240 if (adapter_up(ha) &&
1241 !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
1242 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
1243 if (atomic_read(&ddb_entry->retry_relogin_timer) !=
1244 INVALID_ENTRY) {
1245 if (atomic_read(&ddb_entry->retry_relogin_timer)
1246 == 0) {
1247 atomic_set(&ddb_entry->
1248 retry_relogin_timer,
1249 INVALID_ENTRY);
1250 set_bit(DPC_RELOGIN_DEVICE,
1251 &ha->dpc_flags);
1252 set_bit(DF_RELOGIN, &ddb_entry->flags);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301253 DEBUG2(printk("scsi%ld: %s: ddb [%d]"
David Somayajuluafaf5a22006-09-19 10:28:00 -07001254 " login device\n",
1255 ha->host_no, __func__,
1256 ddb_entry->fw_ddb_index));
1257 } else
1258 atomic_dec(&ddb_entry->
1259 retry_relogin_timer);
1260 }
1261 }
1262
1263 /* Wait for relogin to timeout */
1264 if (atomic_read(&ddb_entry->relogin_timer) &&
1265 (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
1266 /*
1267 * If the relogin times out and the device is
1268 * still NOT ONLINE then try and relogin again.
1269 */
1270 if (atomic_read(&ddb_entry->state) !=
1271 DDB_STATE_ONLINE &&
1272 ddb_entry->fw_ddb_device_state ==
1273 DDB_DS_SESSION_FAILED) {
1274 /* Reset retry relogin timer */
1275 atomic_inc(&ddb_entry->relogin_retry_count);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301276 DEBUG2(printk("scsi%ld: ddb [%d] relogin"
David Somayajuluafaf5a22006-09-19 10:28:00 -07001277 " timed out-retrying"
1278 " relogin (%d)\n",
1279 ha->host_no,
1280 ddb_entry->fw_ddb_index,
1281 atomic_read(&ddb_entry->
1282 relogin_retry_count))
1283 );
1284 start_dpc++;
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301285 DEBUG(printk("scsi%ld:%d:%d: ddb [%d] "
Justin P. Mattockcd09b2c2011-01-28 11:49:08 -08001286 "initiate relogin after"
David Somayajuluafaf5a22006-09-19 10:28:00 -07001287 " %d seconds\n",
1288 ha->host_no, ddb_entry->bus,
1289 ddb_entry->target,
1290 ddb_entry->fw_ddb_index,
1291 ddb_entry->default_time2wait + 4)
1292 );
1293
1294 atomic_set(&ddb_entry->retry_relogin_timer,
1295 ddb_entry->default_time2wait + 4);
1296 }
1297 }
1298 }
1299
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301300 if (!is_qla8022(ha)) {
1301 /* Check for heartbeat interval. */
1302 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
1303 ha->heartbeat_interval != 0) {
1304 ha->seconds_since_last_heartbeat++;
1305 if (ha->seconds_since_last_heartbeat >
1306 ha->heartbeat_interval + 2)
1307 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1308 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001309 }
1310
David Somayajuluafaf5a22006-09-19 10:28:00 -07001311 /* Wakeup the dpc routine for this adapter, if needed. */
Lalit Chandivade1b468072011-05-17 23:17:09 -07001312 if (start_dpc ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07001313 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1314 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
1315 test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301316 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07001317 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1318 test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301319 test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301320 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
1321 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
Lalit Chandivade1b468072011-05-17 23:17:09 -07001322 test_bit(DPC_AEN, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001323 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
1324 " - dpc flags = 0x%lx\n",
1325 ha->host_no, __func__, ha->dpc_flags));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301326 qla4xxx_wake_dpc(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001327 }
1328
1329 /* Reschedule timer thread to call us back in one second */
1330 mod_timer(&ha->timer, jiffies + HZ);
1331
1332 DEBUG2(ha->seconds_since_last_intr++);
1333}
1334
1335/**
1336 * qla4xxx_cmd_wait - waits for all outstanding commands to complete
1337 * @ha: Pointer to host adapter structure.
1338 *
1339 * This routine stalls the driver until all outstanding commands are returned.
1340 * Caller must release the Hardware Lock prior to calling this routine.
1341 **/
1342static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
1343{
1344 uint32_t index = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001345 unsigned long flags;
1346 struct scsi_cmnd *cmd;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001347
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301348 unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
1349
1350 DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
1351 "complete\n", WAIT_CMD_TOV));
1352
1353 while (!time_after_eq(jiffies, wtime)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001354 spin_lock_irqsave(&ha->hardware_lock, flags);
1355 /* Find a command that hasn't completed. */
1356 for (index = 0; index < ha->host->can_queue; index++) {
1357 cmd = scsi_host_find_tag(ha->host, index);
Mike Christiea1e00632010-10-26 05:45:30 -07001358 /*
1359 * We cannot just check if the index is valid,
1360 * becase if we are run from the scsi eh, then
1361 * the scsi/block layer is going to prevent
1362 * the tag from being released.
1363 */
1364 if (cmd != NULL && CMD_SP(cmd))
David Somayajuluafaf5a22006-09-19 10:28:00 -07001365 break;
1366 }
1367 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1368
1369 /* If No Commands are pending, wait is complete */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301370 if (index == ha->host->can_queue)
1371 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001372
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301373 msleep(1000);
1374 }
1375 /* If we timed out on waiting for commands to come back
1376 * return ERROR. */
1377 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001378}
1379
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301380int qla4xxx_hw_reset(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001381{
David Somayajuluafaf5a22006-09-19 10:28:00 -07001382 uint32_t ctrl_status;
David C Somayajulu477ffb92007-01-22 12:26:11 -08001383 unsigned long flags = 0;
1384
1385 DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001386
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301387 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
1388 return QLA_ERROR;
1389
David Somayajuluafaf5a22006-09-19 10:28:00 -07001390 spin_lock_irqsave(&ha->hardware_lock, flags);
1391
1392 /*
1393 * If the SCSI Reset Interrupt bit is set, clear it.
1394 * Otherwise, the Soft Reset won't work.
1395 */
1396 ctrl_status = readw(&ha->reg->ctrl_status);
1397 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
1398 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
1399
1400 /* Issue Soft Reset */
1401 writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
1402 readl(&ha->reg->ctrl_status);
1403
1404 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301405 return QLA_SUCCESS;
David C Somayajulu477ffb92007-01-22 12:26:11 -08001406}
1407
1408/**
1409 * qla4xxx_soft_reset - performs soft reset.
1410 * @ha: Pointer to host adapter structure.
1411 **/
1412int qla4xxx_soft_reset(struct scsi_qla_host *ha)
1413{
1414 uint32_t max_wait_time;
1415 unsigned long flags = 0;
Vikas Chaudharyf931c532010-10-06 22:48:07 -07001416 int status;
David C Somayajulu477ffb92007-01-22 12:26:11 -08001417 uint32_t ctrl_status;
1418
Vikas Chaudharyf931c532010-10-06 22:48:07 -07001419 status = qla4xxx_hw_reset(ha);
1420 if (status != QLA_SUCCESS)
1421 return status;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001422
Vikas Chaudharyf931c532010-10-06 22:48:07 -07001423 status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001424 /* Wait until the Network Reset Intr bit is cleared */
1425 max_wait_time = RESET_INTR_TOV;
1426 do {
1427 spin_lock_irqsave(&ha->hardware_lock, flags);
1428 ctrl_status = readw(&ha->reg->ctrl_status);
1429 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1430
1431 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
1432 break;
1433
1434 msleep(1000);
1435 } while ((--max_wait_time));
1436
1437 if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
1438 DEBUG2(printk(KERN_WARNING
1439 "scsi%ld: Network Reset Intr not cleared by "
1440 "Network function, clearing it now!\n",
1441 ha->host_no));
1442 spin_lock_irqsave(&ha->hardware_lock, flags);
1443 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
1444 readl(&ha->reg->ctrl_status);
1445 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1446 }
1447
1448 /* Wait until the firmware tells us the Soft Reset is done */
1449 max_wait_time = SOFT_RESET_TOV;
1450 do {
1451 spin_lock_irqsave(&ha->hardware_lock, flags);
1452 ctrl_status = readw(&ha->reg->ctrl_status);
1453 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1454
1455 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
1456 status = QLA_SUCCESS;
1457 break;
1458 }
1459
1460 msleep(1000);
1461 } while ((--max_wait_time));
1462
1463 /*
1464 * Also, make sure that the SCSI Reset Interrupt bit has been cleared
1465 * after the soft reset has taken place.
1466 */
1467 spin_lock_irqsave(&ha->hardware_lock, flags);
1468 ctrl_status = readw(&ha->reg->ctrl_status);
1469 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
1470 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
1471 readl(&ha->reg->ctrl_status);
1472 }
1473 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1474
1475 /* If soft reset fails then most probably the bios on other
1476 * function is also enabled.
1477 * Since the initialization is sequential the other fn
1478 * wont be able to acknowledge the soft reset.
1479 * Issue a force soft reset to workaround this scenario.
1480 */
1481 if (max_wait_time == 0) {
1482 /* Issue Force Soft Reset */
1483 spin_lock_irqsave(&ha->hardware_lock, flags);
1484 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
1485 readl(&ha->reg->ctrl_status);
1486 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1487 /* Wait until the firmware tells us the Soft Reset is done */
1488 max_wait_time = SOFT_RESET_TOV;
1489 do {
1490 spin_lock_irqsave(&ha->hardware_lock, flags);
1491 ctrl_status = readw(&ha->reg->ctrl_status);
1492 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1493
1494 if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
1495 status = QLA_SUCCESS;
1496 break;
1497 }
1498
1499 msleep(1000);
1500 } while ((--max_wait_time));
1501 }
1502
1503 return status;
1504}
1505
1506/**
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301507 * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001508 * @ha: Pointer to host adapter structure.
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301509 * @res: returned scsi status
David Somayajuluafaf5a22006-09-19 10:28:00 -07001510 *
1511 * This routine is called just prior to a HARD RESET to return all
1512 * outstanding commands back to the Operating System.
1513 * Caller should make sure that the following locks are released
1514 * before this calling routine: Hardware lock, and io_request_lock.
1515 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301516static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001517{
1518 struct srb *srb;
1519 int i;
1520 unsigned long flags;
1521
1522 spin_lock_irqsave(&ha->hardware_lock, flags);
1523 for (i = 0; i < ha->host->can_queue; i++) {
1524 srb = qla4xxx_del_from_active_array(ha, i);
1525 if (srb != NULL) {
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301526 srb->cmd->result = res;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301527 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001528 }
1529 }
1530 spin_unlock_irqrestore(&ha->hardware_lock, flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001531}
1532
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301533void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
1534{
1535 clear_bit(AF_ONLINE, &ha->flags);
1536
1537 /* Disable the board */
1538 ql4_printk(KERN_INFO, ha, "Disabling the board\n");
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301539
1540 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
1541 qla4xxx_mark_all_devices_missing(ha);
1542 clear_bit(AF_INIT_DONE, &ha->flags);
1543}
1544
David Somayajuluafaf5a22006-09-19 10:28:00 -07001545/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07001546 * qla4xxx_recover_adapter - recovers adapter after a fatal error
1547 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001548 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301549static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001550{
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301551 int status = QLA_ERROR;
1552 uint8_t reset_chip = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001553
1554 /* Stall incoming I/O until we are done */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301555 scsi_block_requests(ha->host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001556 clear_bit(AF_ONLINE, &ha->flags);
Mike Christie50a29ae2008-03-04 13:26:53 -06001557
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301558 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001559
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301560 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001561
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301562 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
1563 reset_chip = 1;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001564
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301565 /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
1566 * do not reset adapter, jump to initialize_adapter */
1567 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1568 status = QLA_SUCCESS;
1569 goto recover_ha_init_adapter;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001570 }
1571
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301572 /* For the ISP-82xx adapter, issue a stop_firmware if invoked
1573 * from eh_host_reset or ioctl module */
1574 if (is_qla8022(ha) && !reset_chip &&
1575 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
1576
1577 DEBUG2(ql4_printk(KERN_INFO, ha,
1578 "scsi%ld: %s - Performing stop_firmware...\n",
1579 ha->host_no, __func__));
1580 status = ha->isp_ops->reset_firmware(ha);
1581 if (status == QLA_SUCCESS) {
Nilesh Javali2bd1e2b2010-10-06 22:49:20 -07001582 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
1583 qla4xxx_cmd_wait(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301584 ha->isp_ops->disable_intrs(ha);
1585 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1586 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1587 } else {
1588 /* If the stop_firmware fails then
1589 * reset the entire chip */
1590 reset_chip = 1;
1591 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1592 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1593 }
1594 }
1595
1596 /* Issue full chip reset if recovering from a catastrophic error,
1597 * or if stop_firmware fails for ISP-82xx.
1598 * This is the default case for ISP-4xxx */
1599 if (!is_qla8022(ha) || reset_chip) {
Nilesh Javali2bd1e2b2010-10-06 22:49:20 -07001600 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
1601 qla4xxx_cmd_wait(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301602 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1603 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1604 DEBUG2(ql4_printk(KERN_INFO, ha,
1605 "scsi%ld: %s - Performing chip reset..\n",
1606 ha->host_no, __func__));
1607 status = ha->isp_ops->reset_chip(ha);
1608 }
1609
1610 /* Flush any pending ddb changed AENs */
1611 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1612
1613recover_ha_init_adapter:
1614 /* Upon successful firmware/chip reset, re-initialize the adapter */
1615 if (status == QLA_SUCCESS) {
1616 /* For ISP-4xxx, force function 1 to always initialize
1617 * before function 3 to prevent both funcions from
1618 * stepping on top of the other */
1619 if (!is_qla8022(ha) && (ha->mac_index == 3))
1620 ssleep(6);
1621
1622 /* NOTE: AF_ONLINE flag set upon successful completion of
1623 * qla4xxx_initialize_adapter */
1624 status = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
1625 }
1626
1627 /* Retry failed adapter initialization, if necessary
1628 * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
1629 * case to prevent ping-pong resets between functions */
1630 if (!test_bit(AF_ONLINE, &ha->flags) &&
1631 !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001632 /* Adapter initialization failed, see if we can retry
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301633 * resetting the ha.
1634 * Since we don't want to block the DPC for too long
1635 * with multiple resets in the same thread,
1636 * utilize DPC to retry */
David Somayajuluafaf5a22006-09-19 10:28:00 -07001637 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
1638 ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
1639 DEBUG2(printk("scsi%ld: recover adapter - retrying "
1640 "(%d) more times\n", ha->host_no,
1641 ha->retry_reset_ha_cnt));
1642 set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1643 status = QLA_ERROR;
1644 } else {
1645 if (ha->retry_reset_ha_cnt > 0) {
1646 /* Schedule another Reset HA--DPC will retry */
1647 ha->retry_reset_ha_cnt--;
1648 DEBUG2(printk("scsi%ld: recover adapter - "
1649 "retry remaining %d\n",
1650 ha->host_no,
1651 ha->retry_reset_ha_cnt));
1652 status = QLA_ERROR;
1653 }
1654
1655 if (ha->retry_reset_ha_cnt == 0) {
1656 /* Recover adapter retries have been exhausted.
1657 * Adapter DEAD */
1658 DEBUG2(printk("scsi%ld: recover adapter "
1659 "failed - board disabled\n",
1660 ha->host_no));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301661 qla4xxx_dead_adapter_cleanup(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001662 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1663 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301664 clear_bit(DPC_RESET_HA_FW_CONTEXT,
David Somayajuluafaf5a22006-09-19 10:28:00 -07001665 &ha->dpc_flags);
1666 status = QLA_ERROR;
1667 }
1668 }
1669 } else {
1670 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301671 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001672 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1673 }
1674
1675 ha->adapter_error_count++;
1676
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301677 if (test_bit(AF_ONLINE, &ha->flags))
1678 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001679
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301680 scsi_unblock_requests(ha->host);
1681
1682 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
1683 DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001684 status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301685
David Somayajuluafaf5a22006-09-19 10:28:00 -07001686 return status;
1687}
1688
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07001689static void qla4xxx_relogin_all_devices(struct scsi_qla_host *ha)
1690{
1691 struct ddb_entry *ddb_entry, *dtemp;
1692
1693 list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
1694 if ((atomic_read(&ddb_entry->state) == DDB_STATE_MISSING) ||
1695 (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD)) {
1696 if (ddb_entry->fw_ddb_device_state ==
1697 DDB_DS_SESSION_ACTIVE) {
1698 atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
1699 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
1700 " marked ONLINE\n", ha->host_no, __func__,
1701 ddb_entry->fw_ddb_index);
1702
1703 iscsi_unblock_session(ddb_entry->sess);
1704 } else
1705 qla4xxx_relogin_device(ha, ddb_entry);
1706 }
1707 }
1708}
1709
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301710void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
1711{
Lalit Chandivade1b468072011-05-17 23:17:09 -07001712 if (ha->dpc_thread)
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301713 queue_work(ha->dpc_thread, &ha->dpc_work);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301714}
1715
David Somayajuluafaf5a22006-09-19 10:28:00 -07001716/**
1717 * qla4xxx_do_dpc - dpc routine
1718 * @data: in our case pointer to adapter structure
1719 *
1720 * This routine is a task that is schedule by the interrupt handler
1721 * to perform the background processing for interrupts. We put it
1722 * on a task queue that is consumed whenever the scheduler runs; that's
1723 * so you can do anything (i.e. put the process to sleep etc). In fact,
1724 * the mid-level tries to sleep when it reaches the driver threshold
1725 * "host->can_queue". This can cause a panic if we were in our interrupt code.
1726 **/
David Howellsc4028952006-11-22 14:57:56 +00001727static void qla4xxx_do_dpc(struct work_struct *work)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001728{
David Howellsc4028952006-11-22 14:57:56 +00001729 struct scsi_qla_host *ha =
1730 container_of(work, struct scsi_qla_host, dpc_work);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001731 struct ddb_entry *ddb_entry, *dtemp;
David C Somayajulu477ffb92007-01-22 12:26:11 -08001732 int status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001733
David C Somayajuluf26b9042006-11-15 16:41:09 -08001734 DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301735 "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
1736 ha->host_no, __func__, ha->flags, ha->dpc_flags))
David Somayajuluafaf5a22006-09-19 10:28:00 -07001737
1738 /* Initialization not yet finished. Don't do anything yet. */
1739 if (!test_bit(AF_INIT_DONE, &ha->flags))
Lalit Chandivade1b468072011-05-17 23:17:09 -07001740 return;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001741
Lalit Chandivade2232be02010-07-30 14:38:47 +05301742 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1743 DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
1744 ha->host_no, __func__, ha->flags));
Lalit Chandivade1b468072011-05-17 23:17:09 -07001745 return;
Lalit Chandivade2232be02010-07-30 14:38:47 +05301746 }
1747
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301748 if (is_qla8022(ha)) {
1749 if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
1750 qla4_8xxx_idc_lock(ha);
1751 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
1752 QLA82XX_DEV_FAILED);
1753 qla4_8xxx_idc_unlock(ha);
1754 ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
1755 qla4_8xxx_device_state_handler(ha);
1756 }
1757 if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
1758 qla4_8xxx_need_qsnt_handler(ha);
1759 }
1760 }
1761
1762 if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
1763 (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07001764 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301765 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
1766 if (ql4xdontresethba) {
1767 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
1768 ha->host_no, __func__));
1769 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
1770 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1771 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1772 goto dpc_post_reset_ha;
1773 }
1774 if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
1775 test_bit(DPC_RESET_HA, &ha->dpc_flags))
1776 qla4xxx_recover_adapter(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001777
David C Somayajulu477ffb92007-01-22 12:26:11 -08001778 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001779 uint8_t wait_time = RESET_INTR_TOV;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001780
David Somayajuluafaf5a22006-09-19 10:28:00 -07001781 while ((readw(&ha->reg->ctrl_status) &
1782 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
1783 if (--wait_time == 0)
1784 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001785 msleep(1000);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001786 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001787 if (wait_time == 0)
1788 DEBUG2(printk("scsi%ld: %s: SR|FSR "
1789 "bit not cleared-- resetting\n",
1790 ha->host_no, __func__));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301791 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
David C Somayajulu477ffb92007-01-22 12:26:11 -08001792 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
1793 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301794 status = qla4xxx_recover_adapter(ha);
David C Somayajulu477ffb92007-01-22 12:26:11 -08001795 }
1796 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1797 if (status == QLA_SUCCESS)
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301798 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001799 }
1800 }
1801
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301802dpc_post_reset_ha:
David Somayajuluafaf5a22006-09-19 10:28:00 -07001803 /* ---- process AEN? --- */
1804 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
1805 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
1806
1807 /* ---- Get DHCP IP Address? --- */
1808 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
1809 qla4xxx_get_dhcp_ip_address(ha);
1810
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301811 /* ---- link change? --- */
1812 if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
1813 if (!test_bit(AF_LINK_UP, &ha->flags)) {
1814 /* ---- link down? --- */
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07001815 qla4xxx_mark_all_devices_missing(ha);
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301816 } else {
1817 /* ---- link up? --- *
1818 * F/W will auto login to all devices ONLY ONCE after
1819 * link up during driver initialization and runtime
1820 * fatal error recovery. Therefore, the driver must
1821 * manually relogin to devices when recovering from
1822 * connection failures, logouts, expired KATO, etc. */
1823
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07001824 qla4xxx_relogin_all_devices(ha);
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301825 }
1826 }
1827
David Somayajuluafaf5a22006-09-19 10:28:00 -07001828 /* ---- relogin device? --- */
1829 if (adapter_up(ha) &&
1830 test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
1831 list_for_each_entry_safe(ddb_entry, dtemp,
1832 &ha->ddb_list, list) {
1833 if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
1834 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
1835 qla4xxx_relogin_device(ha, ddb_entry);
1836
1837 /*
1838 * If mbx cmd times out there is no point
1839 * in continuing further.
1840 * With large no of targets this can hang
1841 * the system.
1842 */
1843 if (test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
1844 printk(KERN_WARNING "scsi%ld: %s: "
1845 "need to reset hba\n",
1846 ha->host_no, __func__);
1847 break;
1848 }
1849 }
1850 }
Vikas Chaudhary0753b482010-07-30 14:27:19 +05301851
David Somayajuluafaf5a22006-09-19 10:28:00 -07001852}
1853
1854/**
1855 * qla4xxx_free_adapter - release the adapter
1856 * @ha: pointer to adapter structure
1857 **/
1858static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
1859{
1860
1861 if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
1862 /* Turn-off interrupts on the card. */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301863 ha->isp_ops->disable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001864 }
1865
David Somayajuluafaf5a22006-09-19 10:28:00 -07001866 /* Remove timer thread, if present */
1867 if (ha->timer_active)
1868 qla4xxx_stop_timer(ha);
1869
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301870 /* Kill the kernel thread for this host */
1871 if (ha->dpc_thread)
1872 destroy_workqueue(ha->dpc_thread);
1873
1874 /* Put firmware in known state */
1875 ha->isp_ops->reset_firmware(ha);
1876
1877 if (is_qla8022(ha)) {
1878 qla4_8xxx_idc_lock(ha);
1879 qla4_8xxx_clear_drv_active(ha);
1880 qla4_8xxx_idc_unlock(ha);
1881 }
1882
David Somayajuluafaf5a22006-09-19 10:28:00 -07001883 /* Detach interrupts */
1884 if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301885 qla4xxx_free_irqs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001886
David C Somayajulubee4fe82007-05-23 18:03:32 -07001887 /* free extra memory */
1888 qla4xxx_mem_free(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301889}
David C Somayajulubee4fe82007-05-23 18:03:32 -07001890
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301891int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
1892{
1893 int status = 0;
1894 uint8_t revision_id;
1895 unsigned long mem_base, mem_len, db_base, db_len;
1896 struct pci_dev *pdev = ha->pdev;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001897
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301898 status = pci_request_regions(pdev, DRIVER_NAME);
1899 if (status) {
1900 printk(KERN_WARNING
1901 "scsi(%ld) Failed to reserve PIO regions (%s) "
1902 "status=%d\n", ha->host_no, pci_name(pdev), status);
1903 goto iospace_error_exit;
1904 }
1905
1906 pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
1907 DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
1908 __func__, revision_id));
1909 ha->revision_id = revision_id;
1910
1911 /* remap phys address */
1912 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
1913 mem_len = pci_resource_len(pdev, 0);
1914 DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
1915 __func__, mem_base, mem_len));
1916
1917 /* mapping of pcibase pointer */
1918 ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
1919 if (!ha->nx_pcibase) {
1920 printk(KERN_ERR
1921 "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
1922 pci_release_regions(ha->pdev);
1923 goto iospace_error_exit;
1924 }
1925
1926 /* Mapping of IO base pointer, door bell read and write pointer */
1927
1928 /* mapping of IO base pointer */
1929 ha->qla4_8xxx_reg =
1930 (struct device_reg_82xx __iomem *)((uint8_t *)ha->nx_pcibase +
1931 0xbc000 + (ha->pdev->devfn << 11));
1932
1933 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
1934 db_len = pci_resource_len(pdev, 4);
1935
Shyam Sundar2657c802010-10-06 22:50:29 -07001936 ha->nx_db_wr_ptr = (ha->pdev->devfn == 4 ? QLA82XX_CAM_RAM_DB1 :
1937 QLA82XX_CAM_RAM_DB2);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301938
Shyam Sundar2657c802010-10-06 22:50:29 -07001939 return 0;
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301940iospace_error_exit:
1941 return -ENOMEM;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001942}
1943
1944/***
1945 * qla4xxx_iospace_config - maps registers
1946 * @ha: pointer to adapter structure
1947 *
1948 * This routines maps HBA's registers from the pci address space
1949 * into the kernel virtual address space for memory mapped i/o.
1950 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301951int qla4xxx_iospace_config(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001952{
1953 unsigned long pio, pio_len, pio_flags;
1954 unsigned long mmio, mmio_len, mmio_flags;
1955
1956 pio = pci_resource_start(ha->pdev, 0);
1957 pio_len = pci_resource_len(ha->pdev, 0);
1958 pio_flags = pci_resource_flags(ha->pdev, 0);
1959 if (pio_flags & IORESOURCE_IO) {
1960 if (pio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301961 ql4_printk(KERN_WARNING, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07001962 "Invalid PCI I/O region size\n");
1963 pio = 0;
1964 }
1965 } else {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301966 ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001967 pio = 0;
1968 }
1969
1970 /* Use MMIO operations for all accesses. */
1971 mmio = pci_resource_start(ha->pdev, 1);
1972 mmio_len = pci_resource_len(ha->pdev, 1);
1973 mmio_flags = pci_resource_flags(ha->pdev, 1);
1974
1975 if (!(mmio_flags & IORESOURCE_MEM)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301976 ql4_printk(KERN_ERR, ha,
1977 "region #0 not an MMIO resource, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001978
1979 goto iospace_error_exit;
1980 }
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301981
David Somayajuluafaf5a22006-09-19 10:28:00 -07001982 if (mmio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301983 ql4_printk(KERN_ERR, ha,
1984 "Invalid PCI mem region size, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001985 goto iospace_error_exit;
1986 }
1987
1988 if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301989 ql4_printk(KERN_WARNING, ha,
1990 "Failed to reserve PIO/MMIO regions\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001991
1992 goto iospace_error_exit;
1993 }
1994
1995 ha->pio_address = pio;
1996 ha->pio_length = pio_len;
1997 ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
1998 if (!ha->reg) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301999 ql4_printk(KERN_ERR, ha,
2000 "cannot remap MMIO, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002001
2002 goto iospace_error_exit;
2003 }
2004
2005 return 0;
2006
2007iospace_error_exit:
2008 return -ENOMEM;
2009}
2010
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302011static struct isp_operations qla4xxx_isp_ops = {
2012 .iospace_config = qla4xxx_iospace_config,
2013 .pci_config = qla4xxx_pci_config,
2014 .disable_intrs = qla4xxx_disable_intrs,
2015 .enable_intrs = qla4xxx_enable_intrs,
2016 .start_firmware = qla4xxx_start_firmware,
2017 .intr_handler = qla4xxx_intr_handler,
2018 .interrupt_service_routine = qla4xxx_interrupt_service_routine,
2019 .reset_chip = qla4xxx_soft_reset,
2020 .reset_firmware = qla4xxx_hw_reset,
2021 .queue_iocb = qla4xxx_queue_iocb,
2022 .complete_iocb = qla4xxx_complete_iocb,
2023 .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
2024 .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
2025 .get_sys_info = qla4xxx_get_sys_info,
2026};
2027
2028static struct isp_operations qla4_8xxx_isp_ops = {
2029 .iospace_config = qla4_8xxx_iospace_config,
2030 .pci_config = qla4_8xxx_pci_config,
2031 .disable_intrs = qla4_8xxx_disable_intrs,
2032 .enable_intrs = qla4_8xxx_enable_intrs,
2033 .start_firmware = qla4_8xxx_load_risc,
2034 .intr_handler = qla4_8xxx_intr_handler,
2035 .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
2036 .reset_chip = qla4_8xxx_isp_reset,
2037 .reset_firmware = qla4_8xxx_stop_firmware,
2038 .queue_iocb = qla4_8xxx_queue_iocb,
2039 .complete_iocb = qla4_8xxx_complete_iocb,
2040 .rd_shdw_req_q_out = qla4_8xxx_rd_shdw_req_q_out,
2041 .rd_shdw_rsp_q_in = qla4_8xxx_rd_shdw_rsp_q_in,
2042 .get_sys_info = qla4_8xxx_get_sys_info,
2043};
2044
2045uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
2046{
2047 return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
2048}
2049
2050uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
2051{
2052 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
2053}
2054
2055uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
2056{
2057 return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
2058}
2059
2060uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
2061{
2062 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
2063}
2064
David Somayajuluafaf5a22006-09-19 10:28:00 -07002065/**
2066 * qla4xxx_probe_adapter - callback function to probe HBA
2067 * @pdev: pointer to pci_dev structure
2068 * @pci_device_id: pointer to pci_device entry
2069 *
2070 * This routine will probe for Qlogic 4xxx iSCSI host adapters.
2071 * It returns zero if successful. It also initializes all data necessary for
2072 * the driver.
2073 **/
2074static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
2075 const struct pci_device_id *ent)
2076{
2077 int ret = -ENODEV, status;
2078 struct Scsi_Host *host;
2079 struct scsi_qla_host *ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002080 uint8_t init_retry_count = 0;
2081 char buf[34];
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302082 struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
Prasanna Mumbaif9880e72011-03-21 03:34:26 -07002083 uint32_t dev_state;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002084
2085 if (pci_enable_device(pdev))
2086 return -1;
2087
2088 host = scsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha));
2089 if (host == NULL) {
2090 printk(KERN_WARNING
2091 "qla4xxx: Couldn't allocate host from scsi layer!\n");
2092 goto probe_disable_device;
2093 }
2094
2095 /* Clear our data area */
2096 ha = (struct scsi_qla_host *) host->hostdata;
2097 memset(ha, 0, sizeof(*ha));
2098
2099 /* Save the information from PCI BIOS. */
2100 ha->pdev = pdev;
2101 ha->host = host;
2102 ha->host_no = host->host_no;
2103
Lalit Chandivade2232be02010-07-30 14:38:47 +05302104 pci_enable_pcie_error_reporting(pdev);
2105
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302106 /* Setup Runtime configurable options */
2107 if (is_qla8022(ha)) {
2108 ha->isp_ops = &qla4_8xxx_isp_ops;
2109 rwlock_init(&ha->hw_lock);
2110 ha->qdr_sn_window = -1;
2111 ha->ddr_mn_window = -1;
2112 ha->curr_window = 255;
2113 ha->func_num = PCI_FUNC(ha->pdev->devfn);
2114 nx_legacy_intr = &legacy_intr[ha->func_num];
2115 ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
2116 ha->nx_legacy_intr.tgt_status_reg =
2117 nx_legacy_intr->tgt_status_reg;
2118 ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
2119 ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
2120 } else {
2121 ha->isp_ops = &qla4xxx_isp_ops;
2122 }
2123
Lalit Chandivade2232be02010-07-30 14:38:47 +05302124 /* Set EEH reset type to fundamental if required by hba */
2125 if (is_qla8022(ha))
2126 pdev->needs_freset = 1;
2127
David Somayajuluafaf5a22006-09-19 10:28:00 -07002128 /* Configure PCI I/O space. */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302129 ret = ha->isp_ops->iospace_config(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002130 if (ret)
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302131 goto probe_failed_ioconfig;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002132
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302133 ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
David Somayajuluafaf5a22006-09-19 10:28:00 -07002134 pdev->device, pdev->irq, ha->reg);
2135
2136 qla4xxx_config_dma_addressing(ha);
2137
2138 /* Initialize lists and spinlocks. */
2139 INIT_LIST_HEAD(&ha->ddb_list);
2140 INIT_LIST_HEAD(&ha->free_srb_q);
2141
2142 mutex_init(&ha->mbox_sem);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302143 init_completion(&ha->mbx_intr_comp);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002144
2145 spin_lock_init(&ha->hardware_lock);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002146
2147 /* Allocate dma buffers */
2148 if (qla4xxx_mem_alloc(ha)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302149 ql4_printk(KERN_WARNING, ha,
2150 "[ERROR] Failed to allocate memory for adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002151
2152 ret = -ENOMEM;
2153 goto probe_failed;
2154 }
2155
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302156 if (is_qla8022(ha))
2157 (void) qla4_8xxx_get_flash_info(ha);
2158
David Somayajuluafaf5a22006-09-19 10:28:00 -07002159 /*
2160 * Initialize the Host adapter request/response queues and
2161 * firmware
2162 * NOTE: interrupts enabled upon successful completion
2163 */
2164 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302165 while ((!test_bit(AF_ONLINE, &ha->flags)) &&
2166 init_retry_count++ < MAX_INIT_RETRIES) {
Prasanna Mumbaif9880e72011-03-21 03:34:26 -07002167
2168 if (is_qla8022(ha)) {
2169 qla4_8xxx_idc_lock(ha);
2170 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
2171 qla4_8xxx_idc_unlock(ha);
2172 if (dev_state == QLA82XX_DEV_FAILED) {
2173 ql4_printk(KERN_WARNING, ha, "%s: don't retry "
2174 "initialize adapter. H/W is in failed state\n",
2175 __func__);
2176 break;
2177 }
2178 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002179 DEBUG2(printk("scsi: %s: retrying adapter initialization "
2180 "(%d)\n", __func__, init_retry_count));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302181
2182 if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
2183 continue;
2184
David Somayajuluafaf5a22006-09-19 10:28:00 -07002185 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
2186 }
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302187
2188 if (!test_bit(AF_ONLINE, &ha->flags)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302189 ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002190
Lalit Chandivadefe998522010-12-02 22:12:36 -08002191 if (is_qla8022(ha) && ql4xdontresethba) {
2192 /* Put the device in failed state. */
2193 DEBUG2(printk(KERN_ERR "HW STATE: FAILED\n"));
2194 qla4_8xxx_idc_lock(ha);
2195 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2196 QLA82XX_DEV_FAILED);
2197 qla4_8xxx_idc_unlock(ha);
2198 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002199 ret = -ENODEV;
2200 goto probe_failed;
2201 }
2202
2203 host->cmd_per_lun = 3;
2204 host->max_channel = 0;
2205 host->max_lun = MAX_LUNS - 1;
2206 host->max_id = MAX_TARGETS;
2207 host->max_cmd_len = IOCB_MAX_CDB_LEN;
2208 host->can_queue = MAX_SRBS ;
2209 host->transportt = qla4xxx_scsi_transport;
2210
2211 ret = scsi_init_shared_tag_map(host, MAX_SRBS);
2212 if (ret) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302213 ql4_printk(KERN_WARNING, ha,
2214 "scsi_init_shared_tag_map failed\n");
2215 goto probe_failed;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002216 }
2217
2218 /* Startup the kernel thread for this host adapter. */
2219 DEBUG2(printk("scsi: %s: Starting kernel thread for "
2220 "qla4xxx_dpc\n", __func__));
2221 sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
2222 ha->dpc_thread = create_singlethread_workqueue(buf);
2223 if (!ha->dpc_thread) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302224 ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002225 ret = -ENODEV;
2226 goto probe_failed;
2227 }
David Howellsc4028952006-11-22 14:57:56 +00002228 INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002229
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302230 /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
2231 * (which is called indirectly by qla4xxx_initialize_adapter),
2232 * so that irqs will be registered after crbinit but before
2233 * mbx_intr_enable.
2234 */
2235 if (!is_qla8022(ha)) {
2236 ret = qla4xxx_request_irqs(ha);
2237 if (ret) {
2238 ql4_printk(KERN_WARNING, ha, "Failed to reserve "
2239 "interrupt %d already in use.\n", pdev->irq);
2240 goto probe_failed;
2241 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002242 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002243
Lalit Chandivade2232be02010-07-30 14:38:47 +05302244 pci_save_state(ha->pdev);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302245 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002246
2247 /* Start timer thread. */
2248 qla4xxx_start_timer(ha, qla4xxx_timer, 1);
2249
2250 set_bit(AF_INIT_DONE, &ha->flags);
2251
2252 pci_set_drvdata(pdev, ha);
2253
2254 ret = scsi_add_host(host, &pdev->dev);
2255 if (ret)
2256 goto probe_failed;
2257
David Somayajuluafaf5a22006-09-19 10:28:00 -07002258 printk(KERN_INFO
2259 " QLogic iSCSI HBA Driver version: %s\n"
2260 " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
2261 qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
2262 ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
2263 ha->patch_number, ha->build_number);
Mike Christie921601b2008-01-31 13:36:49 -06002264 scsi_scan_host(host);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05002265
2266 qla4xxx_create_ifaces(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002267 return 0;
2268
David Somayajuluafaf5a22006-09-19 10:28:00 -07002269probe_failed:
2270 qla4xxx_free_adapter(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302271
2272probe_failed_ioconfig:
Lalit Chandivade2232be02010-07-30 14:38:47 +05302273 pci_disable_pcie_error_reporting(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002274 scsi_host_put(ha->host);
2275
2276probe_disable_device:
2277 pci_disable_device(pdev);
2278
2279 return ret;
2280}
2281
2282/**
Karen Higgins7eece5a2011-03-21 03:34:29 -07002283 * qla4xxx_prevent_other_port_reinit - prevent other port from re-initialize
2284 * @ha: pointer to adapter structure
2285 *
2286 * Mark the other ISP-4xxx port to indicate that the driver is being removed,
2287 * so that the other port will not re-initialize while in the process of
2288 * removing the ha due to driver unload or hba hotplug.
2289 **/
2290static void qla4xxx_prevent_other_port_reinit(struct scsi_qla_host *ha)
2291{
2292 struct scsi_qla_host *other_ha = NULL;
2293 struct pci_dev *other_pdev = NULL;
2294 int fn = ISP4XXX_PCI_FN_2;
2295
2296 /*iscsi function numbers for ISP4xxx is 1 and 3*/
2297 if (PCI_FUNC(ha->pdev->devfn) & BIT_1)
2298 fn = ISP4XXX_PCI_FN_1;
2299
2300 other_pdev =
2301 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
2302 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
2303 fn));
2304
2305 /* Get other_ha if other_pdev is valid and state is enable*/
2306 if (other_pdev) {
2307 if (atomic_read(&other_pdev->enable_cnt)) {
2308 other_ha = pci_get_drvdata(other_pdev);
2309 if (other_ha) {
2310 set_bit(AF_HA_REMOVAL, &other_ha->flags);
2311 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: "
2312 "Prevent %s reinit\n", __func__,
2313 dev_name(&other_ha->pdev->dev)));
2314 }
2315 }
2316 pci_dev_put(other_pdev);
2317 }
2318}
2319
2320/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002321 * qla4xxx_remove_adapter - calback function to remove adapter.
2322 * @pci_dev: PCI device pointer
2323 **/
2324static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
2325{
2326 struct scsi_qla_host *ha;
2327
2328 ha = pci_get_drvdata(pdev);
2329
Karen Higgins7eece5a2011-03-21 03:34:29 -07002330 if (!is_qla8022(ha))
2331 qla4xxx_prevent_other_port_reinit(ha);
David C Somayajulubee4fe82007-05-23 18:03:32 -07002332
David Somayajuluafaf5a22006-09-19 10:28:00 -07002333 /* remove devs from iscsi_sessions to scsi_devices */
2334 qla4xxx_free_ddb_list(ha);
2335
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05002336 /* destroy iface from sysfs */
2337 qla4xxx_destroy_ifaces(ha);
2338
David Somayajuluafaf5a22006-09-19 10:28:00 -07002339 scsi_remove_host(ha->host);
2340
2341 qla4xxx_free_adapter(ha);
2342
2343 scsi_host_put(ha->host);
2344
Lalit Chandivade2232be02010-07-30 14:38:47 +05302345 pci_disable_pcie_error_reporting(pdev);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302346 pci_disable_device(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002347 pci_set_drvdata(pdev, NULL);
2348}
2349
2350/**
2351 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
2352 * @ha: HA context
2353 *
2354 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
2355 * supported addressing method.
2356 */
Adrian Bunk47975472007-04-26 00:35:16 -07002357static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002358{
2359 int retval;
2360
2361 /* Update our PCI device dma_mask for full 64 bit mask */
Yang Hongyang6a355282009-04-06 19:01:13 -07002362 if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
2363 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002364 dev_dbg(&ha->pdev->dev,
2365 "Failed to set 64 bit PCI consistent mask; "
2366 "using 32 bit.\n");
2367 retval = pci_set_consistent_dma_mask(ha->pdev,
Yang Hongyang284901a2009-04-06 19:01:15 -07002368 DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07002369 }
2370 } else
Yang Hongyang284901a2009-04-06 19:01:15 -07002371 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07002372}
2373
2374static int qla4xxx_slave_alloc(struct scsi_device *sdev)
2375{
2376 struct iscsi_cls_session *sess = starget_to_session(sdev->sdev_target);
2377 struct ddb_entry *ddb = sess->dd_data;
Vikas Chaudhary8bb40332011-03-21 03:34:31 -07002378 int queue_depth = QL4_DEF_QDEPTH;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002379
2380 sdev->hostdata = ddb;
2381 sdev->tagged_supported = 1;
Vikas Chaudhary8bb40332011-03-21 03:34:31 -07002382
2383 if (ql4xmaxqdepth != 0 && ql4xmaxqdepth <= 0xffffU)
2384 queue_depth = ql4xmaxqdepth;
2385
2386 scsi_activate_tcq(sdev, queue_depth);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002387 return 0;
2388}
2389
2390static int qla4xxx_slave_configure(struct scsi_device *sdev)
2391{
2392 sdev->tagged_supported = 1;
2393 return 0;
2394}
2395
2396static void qla4xxx_slave_destroy(struct scsi_device *sdev)
2397{
2398 scsi_deactivate_tcq(sdev, 1);
2399}
2400
2401/**
2402 * qla4xxx_del_from_active_array - returns an active srb
2403 * @ha: Pointer to host adapter structure.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02002404 * @index: index into the active_array
David Somayajuluafaf5a22006-09-19 10:28:00 -07002405 *
2406 * This routine removes and returns the srb at the specified index
2407 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302408struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
2409 uint32_t index)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002410{
2411 struct srb *srb = NULL;
Vikas Chaudhary53698872010-04-28 11:41:59 +05302412 struct scsi_cmnd *cmd = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002413
Vikas Chaudhary53698872010-04-28 11:41:59 +05302414 cmd = scsi_host_find_tag(ha->host, index);
2415 if (!cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002416 return srb;
2417
Vikas Chaudhary53698872010-04-28 11:41:59 +05302418 srb = (struct srb *)CMD_SP(cmd);
2419 if (!srb)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002420 return srb;
2421
2422 /* update counters */
2423 if (srb->flags & SRB_DMA_VALID) {
2424 ha->req_q_count += srb->iocb_cnt;
2425 ha->iocb_cnt -= srb->iocb_cnt;
2426 if (srb->cmd)
Vikas Chaudhary53698872010-04-28 11:41:59 +05302427 srb->cmd->host_scribble =
2428 (unsigned char *)(unsigned long) MAX_SRBS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002429 }
2430 return srb;
2431}
2432
2433/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002434 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302435 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07002436 * @cmd: Scsi Command to wait on.
2437 *
2438 * This routine waits for the command to be returned by the Firmware
2439 * for some max time.
2440 **/
2441static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
2442 struct scsi_cmnd *cmd)
2443{
2444 int done = 0;
2445 struct srb *rp;
2446 uint32_t max_wait_time = EH_WAIT_CMD_TOV;
Lalit Chandivade2232be02010-07-30 14:38:47 +05302447 int ret = SUCCESS;
2448
2449 /* Dont wait on command if PCI error is being handled
2450 * by PCI AER driver
2451 */
2452 if (unlikely(pci_channel_offline(ha->pdev)) ||
2453 (test_bit(AF_EEH_BUSY, &ha->flags))) {
2454 ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
2455 ha->host_no, __func__);
2456 return ret;
2457 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002458
2459 do {
2460 /* Checking to see if its returned to OS */
Vikas Chaudhary53698872010-04-28 11:41:59 +05302461 rp = (struct srb *) CMD_SP(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002462 if (rp == NULL) {
2463 done++;
2464 break;
2465 }
2466
2467 msleep(2000);
2468 } while (max_wait_time--);
2469
2470 return done;
2471}
2472
2473/**
2474 * qla4xxx_wait_for_hba_online - waits for HBA to come online
2475 * @ha: Pointer to host adapter structure
2476 **/
2477static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
2478{
2479 unsigned long wait_online;
2480
Vikas Chaudharyf581a3f2010-10-06 22:47:48 -07002481 wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002482 while (time_before(jiffies, wait_online)) {
2483
2484 if (adapter_up(ha))
2485 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002486
2487 msleep(2000);
2488 }
2489
2490 return QLA_ERROR;
2491}
2492
2493/**
Mike Christiece545032008-02-29 18:25:20 -06002494 * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02002495 * @ha: pointer to HBA
David Somayajuluafaf5a22006-09-19 10:28:00 -07002496 * @t: target id
2497 * @l: lun id
2498 *
2499 * This function waits for all outstanding commands to a lun to complete. It
2500 * returns 0 if all pending commands are returned and 1 otherwise.
2501 **/
Mike Christiece545032008-02-29 18:25:20 -06002502static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
2503 struct scsi_target *stgt,
2504 struct scsi_device *sdev)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002505{
2506 int cnt;
2507 int status = 0;
2508 struct scsi_cmnd *cmd;
2509
2510 /*
Mike Christiece545032008-02-29 18:25:20 -06002511 * Waiting for all commands for the designated target or dev
2512 * in the active array
David Somayajuluafaf5a22006-09-19 10:28:00 -07002513 */
2514 for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
2515 cmd = scsi_host_find_tag(ha->host, cnt);
Mike Christiece545032008-02-29 18:25:20 -06002516 if (cmd && stgt == scsi_target(cmd->device) &&
2517 (!sdev || sdev == cmd->device)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002518 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
2519 status++;
2520 break;
2521 }
2522 }
2523 }
2524 return status;
2525}
2526
2527/**
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302528 * qla4xxx_eh_abort - callback for abort task.
2529 * @cmd: Pointer to Linux's SCSI command structure
2530 *
2531 * This routine is called by the Linux OS to abort the specified
2532 * command.
2533 **/
2534static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
2535{
2536 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2537 unsigned int id = cmd->device->id;
2538 unsigned int lun = cmd->device->lun;
Mike Christie92b3e5b2010-10-06 22:51:17 -07002539 unsigned long flags;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302540 struct srb *srb = NULL;
2541 int ret = SUCCESS;
2542 int wait = 0;
2543
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302544 ql4_printk(KERN_INFO, ha,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002545 "scsi%ld:%d:%d: Abort command issued cmd=%p\n",
2546 ha->host_no, id, lun, cmd);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302547
Mike Christie92b3e5b2010-10-06 22:51:17 -07002548 spin_lock_irqsave(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302549 srb = (struct srb *) CMD_SP(cmd);
Mike Christie92b3e5b2010-10-06 22:51:17 -07002550 if (!srb) {
2551 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302552 return SUCCESS;
Mike Christie92b3e5b2010-10-06 22:51:17 -07002553 }
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302554 kref_get(&srb->srb_ref);
Mike Christie92b3e5b2010-10-06 22:51:17 -07002555 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302556
2557 if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
2558 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
2559 ha->host_no, id, lun));
2560 ret = FAILED;
2561 } else {
2562 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
2563 ha->host_no, id, lun));
2564 wait = 1;
2565 }
2566
2567 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
2568
2569 /* Wait for command to complete */
2570 if (wait) {
2571 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
2572 DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
2573 ha->host_no, id, lun));
2574 ret = FAILED;
2575 }
2576 }
2577
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302578 ql4_printk(KERN_INFO, ha,
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302579 "scsi%ld:%d:%d: Abort command - %s\n",
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002580 ha->host_no, id, lun, (ret == SUCCESS) ? "succeeded" : "failed");
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302581
2582 return ret;
2583}
2584
2585/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002586 * qla4xxx_eh_device_reset - callback for target reset.
2587 * @cmd: Pointer to Linux's SCSI command structure
2588 *
2589 * This routine is called by the Linux OS to reset all luns on the
2590 * specified target.
2591 **/
2592static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
2593{
2594 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2595 struct ddb_entry *ddb_entry = cmd->device->hostdata;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002596 int ret = FAILED, stat;
2597
Karen Higgins612f7342009-07-15 15:03:01 -05002598 if (!ddb_entry)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002599 return ret;
2600
Mike Christiec01be6d2010-07-22 16:59:49 +05302601 ret = iscsi_block_scsi_eh(cmd);
2602 if (ret)
2603 return ret;
2604 ret = FAILED;
2605
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302606 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002607 "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
2608 cmd->device->channel, cmd->device->id, cmd->device->lun);
2609
2610 DEBUG2(printk(KERN_INFO
2611 "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
2612 "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
Jens Axboe242f9dc2008-09-14 05:55:09 -07002613 cmd, jiffies, cmd->request->timeout / HZ,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002614 ha->dpc_flags, cmd->result, cmd->allowed));
2615
2616 /* FIXME: wait for hba to go online */
2617 stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
2618 if (stat != QLA_SUCCESS) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302619 ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002620 goto eh_dev_reset_done;
2621 }
2622
Mike Christiece545032008-02-29 18:25:20 -06002623 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2624 cmd->device)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302625 ql4_printk(KERN_INFO, ha,
Mike Christiece545032008-02-29 18:25:20 -06002626 "DEVICE RESET FAILED - waiting for "
2627 "commands.\n");
2628 goto eh_dev_reset_done;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002629 }
2630
David C Somayajulu9d562912008-03-19 11:23:03 -07002631 /* Send marker. */
2632 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2633 MM_LUN_RESET) != QLA_SUCCESS)
2634 goto eh_dev_reset_done;
2635
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302636 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002637 "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
2638 ha->host_no, cmd->device->channel, cmd->device->id,
2639 cmd->device->lun);
2640
2641 ret = SUCCESS;
2642
2643eh_dev_reset_done:
2644
2645 return ret;
2646}
2647
2648/**
Mike Christiece545032008-02-29 18:25:20 -06002649 * qla4xxx_eh_target_reset - callback for target reset.
2650 * @cmd: Pointer to Linux's SCSI command structure
2651 *
2652 * This routine is called by the Linux OS to reset the target.
2653 **/
2654static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
2655{
2656 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2657 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christiec01be6d2010-07-22 16:59:49 +05302658 int stat, ret;
Mike Christiece545032008-02-29 18:25:20 -06002659
2660 if (!ddb_entry)
2661 return FAILED;
2662
Mike Christiec01be6d2010-07-22 16:59:49 +05302663 ret = iscsi_block_scsi_eh(cmd);
2664 if (ret)
2665 return ret;
2666
Mike Christiece545032008-02-29 18:25:20 -06002667 starget_printk(KERN_INFO, scsi_target(cmd->device),
2668 "WARM TARGET RESET ISSUED.\n");
2669
2670 DEBUG2(printk(KERN_INFO
2671 "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
2672 "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
Jens Axboe242f9dc2008-09-14 05:55:09 -07002673 ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
Mike Christiece545032008-02-29 18:25:20 -06002674 ha->dpc_flags, cmd->result, cmd->allowed));
2675
2676 stat = qla4xxx_reset_target(ha, ddb_entry);
2677 if (stat != QLA_SUCCESS) {
2678 starget_printk(KERN_INFO, scsi_target(cmd->device),
2679 "WARM TARGET RESET FAILED.\n");
2680 return FAILED;
2681 }
2682
Mike Christiece545032008-02-29 18:25:20 -06002683 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2684 NULL)) {
2685 starget_printk(KERN_INFO, scsi_target(cmd->device),
2686 "WARM TARGET DEVICE RESET FAILED - "
2687 "waiting for commands.\n");
2688 return FAILED;
2689 }
2690
David C Somayajulu9d562912008-03-19 11:23:03 -07002691 /* Send marker. */
2692 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2693 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
2694 starget_printk(KERN_INFO, scsi_target(cmd->device),
2695 "WARM TARGET DEVICE RESET FAILED - "
2696 "marker iocb failed.\n");
2697 return FAILED;
2698 }
2699
Mike Christiece545032008-02-29 18:25:20 -06002700 starget_printk(KERN_INFO, scsi_target(cmd->device),
2701 "WARM TARGET RESET SUCCEEDED.\n");
2702 return SUCCESS;
2703}
2704
2705/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002706 * qla4xxx_eh_host_reset - kernel callback
2707 * @cmd: Pointer to Linux's SCSI command structure
2708 *
2709 * This routine is invoked by the Linux kernel to perform fatal error
2710 * recovery on the specified adapter.
2711 **/
2712static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
2713{
2714 int return_status = FAILED;
2715 struct scsi_qla_host *ha;
2716
2717 ha = (struct scsi_qla_host *) cmd->device->host->hostdata;
2718
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302719 if (ql4xdontresethba) {
2720 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
2721 ha->host_no, __func__));
2722 return FAILED;
2723 }
2724
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302725 ql4_printk(KERN_INFO, ha,
Karen Higginsdca05c42009-07-15 15:03:00 -05002726 "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002727 cmd->device->channel, cmd->device->id, cmd->device->lun);
2728
2729 if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
2730 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
2731 "DEAD.\n", ha->host_no, cmd->device->channel,
2732 __func__));
2733
2734 return FAILED;
2735 }
2736
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302737 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
2738 if (is_qla8022(ha))
2739 set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2740 else
2741 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2742 }
Mike Christie50a29ae2008-03-04 13:26:53 -06002743
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302744 if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002745 return_status = SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002746
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302747 ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002748 return_status == FAILED ? "FAILED" : "SUCCEEDED");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002749
2750 return return_status;
2751}
2752
Lalit Chandivade2232be02010-07-30 14:38:47 +05302753/* PCI AER driver recovers from all correctable errors w/o
2754 * driver intervention. For uncorrectable errors PCI AER
2755 * driver calls the following device driver's callbacks
2756 *
2757 * - Fatal Errors - link_reset
2758 * - Non-Fatal Errors - driver's pci_error_detected() which
2759 * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
2760 *
2761 * PCI AER driver calls
2762 * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
2763 * returns RECOVERED or NEED_RESET if fw_hung
2764 * NEED_RESET - driver's slot_reset()
2765 * DISCONNECT - device is dead & cannot recover
2766 * RECOVERED - driver's pci_resume()
2767 */
2768static pci_ers_result_t
2769qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
2770{
2771 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2772
2773 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
2774 ha->host_no, __func__, state);
2775
2776 if (!is_aer_supported(ha))
2777 return PCI_ERS_RESULT_NONE;
2778
2779 switch (state) {
2780 case pci_channel_io_normal:
2781 clear_bit(AF_EEH_BUSY, &ha->flags);
2782 return PCI_ERS_RESULT_CAN_RECOVER;
2783 case pci_channel_io_frozen:
2784 set_bit(AF_EEH_BUSY, &ha->flags);
2785 qla4xxx_mailbox_premature_completion(ha);
2786 qla4xxx_free_irqs(ha);
2787 pci_disable_device(pdev);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002788 /* Return back all IOs */
2789 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
Lalit Chandivade2232be02010-07-30 14:38:47 +05302790 return PCI_ERS_RESULT_NEED_RESET;
2791 case pci_channel_io_perm_failure:
2792 set_bit(AF_EEH_BUSY, &ha->flags);
2793 set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
2794 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
2795 return PCI_ERS_RESULT_DISCONNECT;
2796 }
2797 return PCI_ERS_RESULT_NEED_RESET;
2798}
2799
2800/**
2801 * qla4xxx_pci_mmio_enabled() gets called if
2802 * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
2803 * and read/write to the device still works.
2804 **/
2805static pci_ers_result_t
2806qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
2807{
2808 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2809
2810 if (!is_aer_supported(ha))
2811 return PCI_ERS_RESULT_NONE;
2812
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002813 return PCI_ERS_RESULT_RECOVERED;
Lalit Chandivade2232be02010-07-30 14:38:47 +05302814}
2815
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002816static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
Lalit Chandivade2232be02010-07-30 14:38:47 +05302817{
2818 uint32_t rval = QLA_ERROR;
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002819 uint32_t ret = 0;
Lalit Chandivade2232be02010-07-30 14:38:47 +05302820 int fn;
2821 struct pci_dev *other_pdev = NULL;
2822
2823 ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
2824
2825 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2826
2827 if (test_bit(AF_ONLINE, &ha->flags)) {
2828 clear_bit(AF_ONLINE, &ha->flags);
2829 qla4xxx_mark_all_devices_missing(ha);
2830 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
Lalit Chandivade2232be02010-07-30 14:38:47 +05302831 }
2832
2833 fn = PCI_FUNC(ha->pdev->devfn);
2834 while (fn > 0) {
2835 fn--;
2836 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
2837 "func %x\n", ha->host_no, __func__, fn);
2838 /* Get the pci device given the domain, bus,
2839 * slot/function number */
2840 other_pdev =
2841 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
2842 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
2843 fn));
2844
2845 if (!other_pdev)
2846 continue;
2847
2848 if (atomic_read(&other_pdev->enable_cnt)) {
2849 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
2850 "func in enabled state%x\n", ha->host_no,
2851 __func__, fn);
2852 pci_dev_put(other_pdev);
2853 break;
2854 }
2855 pci_dev_put(other_pdev);
2856 }
2857
2858 /* The first function on the card, the reset owner will
2859 * start & initialize the firmware. The other functions
2860 * on the card will reset the firmware context
2861 */
2862 if (!fn) {
2863 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
2864 "0x%x is the owner\n", ha->host_no, __func__,
2865 ha->pdev->devfn);
2866
2867 qla4_8xxx_idc_lock(ha);
2868 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2869 QLA82XX_DEV_COLD);
2870
2871 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
2872 QLA82XX_IDC_VERSION);
2873
2874 qla4_8xxx_idc_unlock(ha);
2875 clear_bit(AF_FW_RECOVERY, &ha->flags);
2876 rval = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
2877 qla4_8xxx_idc_lock(ha);
2878
2879 if (rval != QLA_SUCCESS) {
2880 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
2881 "FAILED\n", ha->host_no, __func__);
2882 qla4_8xxx_clear_drv_active(ha);
2883 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2884 QLA82XX_DEV_FAILED);
2885 } else {
2886 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
2887 "READY\n", ha->host_no, __func__);
2888 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2889 QLA82XX_DEV_READY);
2890 /* Clear driver state register */
2891 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
2892 qla4_8xxx_set_drv_active(ha);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002893 ret = qla4xxx_request_irqs(ha);
2894 if (ret) {
2895 ql4_printk(KERN_WARNING, ha, "Failed to "
2896 "reserve interrupt %d already in use.\n",
2897 ha->pdev->irq);
2898 rval = QLA_ERROR;
2899 } else {
2900 ha->isp_ops->enable_intrs(ha);
2901 rval = QLA_SUCCESS;
2902 }
Lalit Chandivade2232be02010-07-30 14:38:47 +05302903 }
2904 qla4_8xxx_idc_unlock(ha);
2905 } else {
2906 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
2907 "the reset owner\n", ha->host_no, __func__,
2908 ha->pdev->devfn);
2909 if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
2910 QLA82XX_DEV_READY)) {
2911 clear_bit(AF_FW_RECOVERY, &ha->flags);
2912 rval = qla4xxx_initialize_adapter(ha,
2913 PRESERVE_DDB_LIST);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002914 if (rval == QLA_SUCCESS) {
2915 ret = qla4xxx_request_irqs(ha);
2916 if (ret) {
2917 ql4_printk(KERN_WARNING, ha, "Failed to"
2918 " reserve interrupt %d already in"
2919 " use.\n", ha->pdev->irq);
2920 rval = QLA_ERROR;
2921 } else {
2922 ha->isp_ops->enable_intrs(ha);
2923 rval = QLA_SUCCESS;
2924 }
2925 }
Lalit Chandivade2232be02010-07-30 14:38:47 +05302926 qla4_8xxx_idc_lock(ha);
2927 qla4_8xxx_set_drv_active(ha);
2928 qla4_8xxx_idc_unlock(ha);
2929 }
2930 }
2931 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2932 return rval;
2933}
2934
2935static pci_ers_result_t
2936qla4xxx_pci_slot_reset(struct pci_dev *pdev)
2937{
2938 pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
2939 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2940 int rc;
2941
2942 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
2943 ha->host_no, __func__);
2944
2945 if (!is_aer_supported(ha))
2946 return PCI_ERS_RESULT_NONE;
2947
2948 /* Restore the saved state of PCIe device -
2949 * BAR registers, PCI Config space, PCIX, MSI,
2950 * IOV states
2951 */
2952 pci_restore_state(pdev);
2953
2954 /* pci_restore_state() clears the saved_state flag of the device
2955 * save restored state which resets saved_state flag
2956 */
2957 pci_save_state(pdev);
2958
2959 /* Initialize device or resume if in suspended state */
2960 rc = pci_enable_device(pdev);
2961 if (rc) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002962 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Can't re-enable "
Lalit Chandivade2232be02010-07-30 14:38:47 +05302963 "device after reset\n", ha->host_no, __func__);
2964 goto exit_slot_reset;
2965 }
2966
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002967 ha->isp_ops->disable_intrs(ha);
Lalit Chandivade2232be02010-07-30 14:38:47 +05302968
2969 if (is_qla8022(ha)) {
2970 if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
2971 ret = PCI_ERS_RESULT_RECOVERED;
2972 goto exit_slot_reset;
2973 } else
2974 goto exit_slot_reset;
2975 }
2976
2977exit_slot_reset:
2978 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
2979 "device after reset\n", ha->host_no, __func__, ret);
2980 return ret;
2981}
2982
2983static void
2984qla4xxx_pci_resume(struct pci_dev *pdev)
2985{
2986 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2987 int ret;
2988
2989 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
2990 ha->host_no, __func__);
2991
2992 ret = qla4xxx_wait_for_hba_online(ha);
2993 if (ret != QLA_SUCCESS) {
2994 ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
2995 "resume I/O from slot/link_reset\n", ha->host_no,
2996 __func__);
2997 }
2998
2999 pci_cleanup_aer_uncorrect_error_status(pdev);
3000 clear_bit(AF_EEH_BUSY, &ha->flags);
3001}
3002
3003static struct pci_error_handlers qla4xxx_err_handler = {
3004 .error_detected = qla4xxx_pci_error_detected,
3005 .mmio_enabled = qla4xxx_pci_mmio_enabled,
3006 .slot_reset = qla4xxx_pci_slot_reset,
3007 .resume = qla4xxx_pci_resume,
3008};
3009
David Somayajuluafaf5a22006-09-19 10:28:00 -07003010static struct pci_device_id qla4xxx_pci_tbl[] = {
3011 {
3012 .vendor = PCI_VENDOR_ID_QLOGIC,
3013 .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
3014 .subvendor = PCI_ANY_ID,
3015 .subdevice = PCI_ANY_ID,
3016 },
3017 {
3018 .vendor = PCI_VENDOR_ID_QLOGIC,
3019 .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
3020 .subvendor = PCI_ANY_ID,
3021 .subdevice = PCI_ANY_ID,
3022 },
David C Somayajulud9150582006-11-15 17:38:40 -08003023 {
3024 .vendor = PCI_VENDOR_ID_QLOGIC,
3025 .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
3026 .subvendor = PCI_ANY_ID,
3027 .subdevice = PCI_ANY_ID,
3028 },
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303029 {
3030 .vendor = PCI_VENDOR_ID_QLOGIC,
3031 .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
3032 .subvendor = PCI_ANY_ID,
3033 .subdevice = PCI_ANY_ID,
3034 },
David Somayajuluafaf5a22006-09-19 10:28:00 -07003035 {0, 0},
3036};
3037MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
3038
Adrian Bunk47975472007-04-26 00:35:16 -07003039static struct pci_driver qla4xxx_pci_driver = {
David Somayajuluafaf5a22006-09-19 10:28:00 -07003040 .name = DRIVER_NAME,
3041 .id_table = qla4xxx_pci_tbl,
3042 .probe = qla4xxx_probe_adapter,
3043 .remove = qla4xxx_remove_adapter,
Lalit Chandivade2232be02010-07-30 14:38:47 +05303044 .err_handler = &qla4xxx_err_handler,
David Somayajuluafaf5a22006-09-19 10:28:00 -07003045};
3046
3047static int __init qla4xxx_module_init(void)
3048{
3049 int ret;
3050
3051 /* Allocate cache for SRBs. */
3052 srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09003053 SLAB_HWCACHE_ALIGN, NULL);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003054 if (srb_cachep == NULL) {
3055 printk(KERN_ERR
3056 "%s: Unable to allocate SRB cache..."
3057 "Failing load!\n", DRIVER_NAME);
3058 ret = -ENOMEM;
3059 goto no_srp_cache;
3060 }
3061
3062 /* Derive version string. */
3063 strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
Andrew Vasquez11010fe2006-10-06 09:54:59 -07003064 if (ql4xextended_error_logging)
David Somayajuluafaf5a22006-09-19 10:28:00 -07003065 strcat(qla4xxx_version_str, "-debug");
3066
3067 qla4xxx_scsi_transport =
3068 iscsi_register_transport(&qla4xxx_iscsi_transport);
3069 if (!qla4xxx_scsi_transport){
3070 ret = -ENODEV;
3071 goto release_srb_cache;
3072 }
3073
David Somayajuluafaf5a22006-09-19 10:28:00 -07003074 ret = pci_register_driver(&qla4xxx_pci_driver);
3075 if (ret)
3076 goto unregister_transport;
3077
3078 printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
3079 return 0;
Doug Maxey5ae16db2006-10-05 23:50:07 -05003080
David Somayajuluafaf5a22006-09-19 10:28:00 -07003081unregister_transport:
3082 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
3083release_srb_cache:
3084 kmem_cache_destroy(srb_cachep);
3085no_srp_cache:
3086 return ret;
3087}
3088
3089static void __exit qla4xxx_module_exit(void)
3090{
David Somayajuluafaf5a22006-09-19 10:28:00 -07003091 pci_unregister_driver(&qla4xxx_pci_driver);
3092 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
3093 kmem_cache_destroy(srb_cachep);
3094}
3095
3096module_init(qla4xxx_module_init);
3097module_exit(qla4xxx_module_exit);
3098
3099MODULE_AUTHOR("QLogic Corporation");
3100MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
3101MODULE_LICENSE("GPL");
3102MODULE_VERSION(QLA4XXX_DRIVER_VERSION);