blob: df6da3cb124d720d318f3a75568176d46489f324 [file] [log] [blame]
David Somayajuluafaf5a22006-09-19 10:28:00 -07001/*
2 * QLogic iSCSI HBA Driver
Vikas Chaudhary7d01d062010-12-02 22:12:51 -08003 * Copyright (c) 2003-2010 QLogic Corporation
David Somayajuluafaf5a22006-09-19 10:28:00 -07004 *
5 * See LICENSE.qla4xxx for copyright and licensing details.
6 */
7#include <linux/moduleparam.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
David Somayajuluafaf5a22006-09-19 10:28:00 -07009
10#include <scsi/scsi_tcq.h>
11#include <scsi/scsicam.h>
12
13#include "ql4_def.h"
David C Somayajulubee4fe82007-05-23 18:03:32 -070014#include "ql4_version.h"
15#include "ql4_glbl.h"
16#include "ql4_dbg.h"
17#include "ql4_inline.h"
David Somayajuluafaf5a22006-09-19 10:28:00 -070018
19/*
20 * Driver version
21 */
Adrian Bunk47975472007-04-26 00:35:16 -070022static char qla4xxx_version_str[40];
David Somayajuluafaf5a22006-09-19 10:28:00 -070023
24/*
25 * SRB allocation cache
26 */
Christoph Lametere18b8902006-12-06 20:33:20 -080027static struct kmem_cache *srb_cachep;
David Somayajuluafaf5a22006-09-19 10:28:00 -070028
29/*
30 * Module parameter information and variables
31 */
David Somayajuluafaf5a22006-09-19 10:28:00 -070032int ql4xdontresethba = 0;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053033module_param(ql4xdontresethba, int, S_IRUGO | S_IWUSR);
David Somayajuluafaf5a22006-09-19 10:28:00 -070034MODULE_PARM_DESC(ql4xdontresethba,
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053035 "Don't reset the HBA for driver recovery \n"
36 " 0 - It will reset HBA (Default)\n"
37 " 1 - It will NOT reset HBA");
David Somayajuluafaf5a22006-09-19 10:28:00 -070038
Andrew Vasquez11010fe2006-10-06 09:54:59 -070039int ql4xextended_error_logging = 0; /* 0 = off, 1 = log errors */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053040module_param(ql4xextended_error_logging, int, S_IRUGO | S_IWUSR);
Andrew Vasquez11010fe2006-10-06 09:54:59 -070041MODULE_PARM_DESC(ql4xextended_error_logging,
David Somayajuluafaf5a22006-09-19 10:28:00 -070042 "Option to enable extended error logging, "
43 "Default is 0 - no logging, 1 - debug logging");
44
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053045int ql4xenablemsix = 1;
46module_param(ql4xenablemsix, int, S_IRUGO|S_IWUSR);
47MODULE_PARM_DESC(ql4xenablemsix,
48 "Set to enable MSI or MSI-X interrupt mechanism.\n"
49 " 0 = enable INTx interrupt mechanism.\n"
50 " 1 = enable MSI-X interrupt mechanism (Default).\n"
51 " 2 = enable MSI interrupt mechanism.");
David C Somayajulu477ffb92007-01-22 12:26:11 -080052
Mike Christied510d962008-07-11 19:50:33 -050053#define QL4_DEF_QDEPTH 32
Vikas Chaudhary8bb40332011-03-21 03:34:31 -070054static int ql4xmaxqdepth = QL4_DEF_QDEPTH;
55module_param(ql4xmaxqdepth, int, S_IRUGO | S_IWUSR);
56MODULE_PARM_DESC(ql4xmaxqdepth,
57 "Maximum queue depth to report for target devices.\n"
58 " Default: 32.");
Mike Christied510d962008-07-11 19:50:33 -050059
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 Chaudharyf4f5df22010-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 Christie8ad57812007-05-30 12:57:13 -0500141 .host_param_mask = ISCSI_HOST_HWADDRESS |
Mike Christie22236962007-05-30 12:57:24 -0500142 ISCSI_HOST_IPADDRESS |
Mike Christie8ad57812007-05-30 12:57:13 -0500143 ISCSI_HOST_INITIATOR_NAME,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700144 .tgt_dscvr = qla4xxx_tgt_dscvr,
Mike Christie3128c6c2011-07-25 13:48:42 -0500145 .attr_is_visible = ql4_attr_is_visible,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700146 .get_conn_param = qla4xxx_conn_get_param,
147 .get_session_param = qla4xxx_sess_get_param,
Mike Christieaa1e93a2007-05-30 12:57:09 -0500148 .get_host_param = qla4xxx_host_get_param,
Mike Christied00efe32011-07-25 13:48:38 -0500149 .set_iface_param = qla4xxx_iface_set_param,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700150 .session_recovery_timedout = qla4xxx_recovery_timedout,
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500151 .get_iface_param = qla4xxx_get_iface_param,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700152};
153
154static struct scsi_transport_template *qla4xxx_scsi_transport;
155
Mike Christie3128c6c2011-07-25 13:48:42 -0500156static mode_t ql4_attr_is_visible(int param_type, int param)
157{
158 switch (param_type) {
159 case ISCSI_PARAM:
160 switch (param) {
161 case ISCSI_PARAM_CONN_ADDRESS:
162 case ISCSI_PARAM_CONN_PORT:
Mike Christie1d063c12011-07-25 13:48:43 -0500163 case ISCSI_PARAM_TARGET_NAME:
164 case ISCSI_PARAM_TPGT:
165 case ISCSI_PARAM_TARGET_ALIAS:
Mike Christie3128c6c2011-07-25 13:48:42 -0500166 return S_IRUGO;
167 default:
168 return 0;
169 }
Mike Christieb78dbba2011-07-25 13:48:44 -0500170 case ISCSI_NET_PARAM:
171 switch (param) {
172 case ISCSI_NET_PARAM_IPV4_ADDR:
173 case ISCSI_NET_PARAM_IPV4_SUBNET:
174 case ISCSI_NET_PARAM_IPV4_GW:
175 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
176 case ISCSI_NET_PARAM_IFACE_ENABLE:
177 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
178 case ISCSI_NET_PARAM_IPV6_ADDR:
179 case ISCSI_NET_PARAM_IPV6_ROUTER:
180 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
181 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
182 return S_IRUGO;
183 default:
184 return 0;
185 }
Mike Christie3128c6c2011-07-25 13:48:42 -0500186 }
187
188 return 0;
189}
190
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500191static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
192 enum iscsi_param_type param_type,
193 int param, char *buf)
194{
195 struct Scsi_Host *shost = iscsi_iface_to_shost(iface);
196 struct scsi_qla_host *ha = to_qla_host(shost);
197 int len = -ENOSYS;
198
199 if (param_type != ISCSI_NET_PARAM)
200 return -ENOSYS;
201
202 switch (param) {
203 case ISCSI_NET_PARAM_IPV4_ADDR:
204 len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
205 break;
206 case ISCSI_NET_PARAM_IPV4_SUBNET:
207 len = sprintf(buf, "%pI4\n", &ha->ip_config.subnet_mask);
208 break;
209 case ISCSI_NET_PARAM_IPV4_GW:
210 len = sprintf(buf, "%pI4\n", &ha->ip_config.gateway);
211 break;
212 case ISCSI_NET_PARAM_IFACE_ENABLE:
213 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
214 len = sprintf(buf, "%s\n",
215 (ha->ip_config.ipv4_options &
216 IPOPT_IPV4_PROTOCOL_ENABLE) ?
217 "enabled" : "disabled");
218 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
219 len = sprintf(buf, "%s\n",
220 (ha->ip_config.ipv6_options &
221 IPV6_OPT_IPV6_PROTOCOL_ENABLE) ?
222 "enabled" : "disabled");
223 break;
224 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
225 len = sprintf(buf, "%s\n",
226 (ha->ip_config.tcp_options & TCPOPT_DHCP_ENABLE) ?
227 "dhcp" : "static");
228 break;
229 case ISCSI_NET_PARAM_IPV6_ADDR:
230 if (iface->iface_num == 0)
231 len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr0);
232 if (iface->iface_num == 1)
233 len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr1);
234 break;
235 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
236 len = sprintf(buf, "%pI6\n",
237 &ha->ip_config.ipv6_link_local_addr);
238 break;
239 case ISCSI_NET_PARAM_IPV6_ROUTER:
240 len = sprintf(buf, "%pI6\n",
241 &ha->ip_config.ipv6_default_router_addr);
242 break;
243 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
244 len = sprintf(buf, "%s\n",
245 (ha->ip_config.ipv6_addl_options &
246 IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) ?
247 "nd" : "static");
248 break;
249 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
250 len = sprintf(buf, "%s\n",
251 (ha->ip_config.ipv6_addl_options &
252 IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR) ?
253 "auto" : "static");
254 break;
255 default:
256 len = -ENOSYS;
257 }
258
259 return len;
260}
261
Mike Christie5c656af2009-07-15 15:02:59 -0500262static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
263{
264 struct iscsi_cls_session *session;
265 struct ddb_entry *ddb_entry;
266
267 session = starget_to_session(scsi_target(sc->device));
268 ddb_entry = session->dd_data;
269
270 /* if we are not logged in then the LLD is going to clean up the cmd */
271 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
272 return BLK_EH_RESET_TIMER;
273 else
274 return BLK_EH_NOT_HANDLED;
275}
276
David Somayajuluafaf5a22006-09-19 10:28:00 -0700277static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session)
278{
279 struct ddb_entry *ddb_entry = session->dd_data;
280 struct scsi_qla_host *ha = ddb_entry->ha;
281
Mike Christie568d3032008-01-31 13:36:47 -0600282 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
283 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700284
Vikas Chaudhary3013cea2010-07-30 14:25:46 +0530285 DEBUG2(printk("scsi%ld: %s: ddb [%d] session recovery timeout "
Mike Christie568d3032008-01-31 13:36:47 -0600286 "of (%d) secs exhausted, marking device DEAD.\n",
287 ha->host_no, __func__, ddb_entry->fw_ddb_index,
Vikas Chaudhary30387272011-03-21 03:34:32 -0700288 ddb_entry->sess->recovery_tmo));
Mike Christie568d3032008-01-31 13:36:47 -0600289 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700290}
291
Mike Christieaa1e93a2007-05-30 12:57:09 -0500292static int qla4xxx_host_get_param(struct Scsi_Host *shost,
293 enum iscsi_host_param param, char *buf)
294{
295 struct scsi_qla_host *ha = to_qla_host(shost);
296 int len;
297
298 switch (param) {
299 case ISCSI_HOST_PARAM_HWADDRESS:
Michael Chan7ffc49a2007-12-24 21:28:09 -0800300 len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
Mike Christieaa1e93a2007-05-30 12:57:09 -0500301 break;
Mike Christie22236962007-05-30 12:57:24 -0500302 case ISCSI_HOST_PARAM_IPADDRESS:
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500303 len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
Mike Christie22236962007-05-30 12:57:24 -0500304 break;
Mike Christie8ad57812007-05-30 12:57:13 -0500305 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie22236962007-05-30 12:57:24 -0500306 len = sprintf(buf, "%s\n", ha->name_string);
Mike Christie8ad57812007-05-30 12:57:13 -0500307 break;
Mike Christieaa1e93a2007-05-30 12:57:09 -0500308 default:
309 return -ENOSYS;
310 }
311
312 return len;
313}
314
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500315static void qla4xxx_create_ipv4_iface(struct scsi_qla_host *ha)
316{
317 if (ha->iface_ipv4)
318 return;
319
320 /* IPv4 */
321 ha->iface_ipv4 = iscsi_create_iface(ha->host,
322 &qla4xxx_iscsi_transport,
323 ISCSI_IFACE_TYPE_IPV4, 0, 0);
324 if (!ha->iface_ipv4)
325 ql4_printk(KERN_ERR, ha, "Could not create IPv4 iSCSI "
326 "iface0.\n");
327}
328
329static void qla4xxx_create_ipv6_iface(struct scsi_qla_host *ha)
330{
331 if (!ha->iface_ipv6_0)
332 /* IPv6 iface-0 */
333 ha->iface_ipv6_0 = iscsi_create_iface(ha->host,
334 &qla4xxx_iscsi_transport,
335 ISCSI_IFACE_TYPE_IPV6, 0,
336 0);
337 if (!ha->iface_ipv6_0)
338 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
339 "iface0.\n");
340
341 if (!ha->iface_ipv6_1)
342 /* IPv6 iface-1 */
343 ha->iface_ipv6_1 = iscsi_create_iface(ha->host,
344 &qla4xxx_iscsi_transport,
345 ISCSI_IFACE_TYPE_IPV6, 1,
346 0);
347 if (!ha->iface_ipv6_1)
348 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
349 "iface1.\n");
350}
351
352static void qla4xxx_create_ifaces(struct scsi_qla_host *ha)
353{
354 if (ha->ip_config.ipv4_options & IPOPT_IPV4_PROTOCOL_ENABLE)
355 qla4xxx_create_ipv4_iface(ha);
356
357 if (ha->ip_config.ipv6_options & IPV6_OPT_IPV6_PROTOCOL_ENABLE)
358 qla4xxx_create_ipv6_iface(ha);
359}
360
361static void qla4xxx_destroy_ipv4_iface(struct scsi_qla_host *ha)
362{
363 if (ha->iface_ipv4) {
364 iscsi_destroy_iface(ha->iface_ipv4);
365 ha->iface_ipv4 = NULL;
366 }
367}
368
369static void qla4xxx_destroy_ipv6_iface(struct scsi_qla_host *ha)
370{
371 if (ha->iface_ipv6_0) {
372 iscsi_destroy_iface(ha->iface_ipv6_0);
373 ha->iface_ipv6_0 = NULL;
374 }
375 if (ha->iface_ipv6_1) {
376 iscsi_destroy_iface(ha->iface_ipv6_1);
377 ha->iface_ipv6_1 = NULL;
378 }
379}
380
381static void qla4xxx_destroy_ifaces(struct scsi_qla_host *ha)
382{
383 qla4xxx_destroy_ipv4_iface(ha);
384 qla4xxx_destroy_ipv6_iface(ha);
385}
386
Mike Christied00efe32011-07-25 13:48:38 -0500387static void qla4xxx_set_ipv6(struct scsi_qla_host *ha,
388 struct iscsi_iface_param_info *iface_param,
389 struct addr_ctrl_blk *init_fw_cb)
390{
391 /*
392 * iface_num 0 is valid for IPv6 Addr, linklocal, router, autocfg.
393 * iface_num 1 is valid only for IPv6 Addr.
394 */
395 switch (iface_param->param) {
396 case ISCSI_NET_PARAM_IPV6_ADDR:
397 if (iface_param->iface_num & 0x1)
398 /* IPv6 Addr 1 */
399 memcpy(init_fw_cb->ipv6_addr1, iface_param->value,
400 sizeof(init_fw_cb->ipv6_addr1));
401 else
402 /* IPv6 Addr 0 */
403 memcpy(init_fw_cb->ipv6_addr0, iface_param->value,
404 sizeof(init_fw_cb->ipv6_addr0));
405 break;
406 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
407 if (iface_param->iface_num & 0x1)
408 break;
409 memcpy(init_fw_cb->ipv6_if_id, &iface_param->value[8],
410 sizeof(init_fw_cb->ipv6_if_id));
411 break;
412 case ISCSI_NET_PARAM_IPV6_ROUTER:
413 if (iface_param->iface_num & 0x1)
414 break;
415 memcpy(init_fw_cb->ipv6_dflt_rtr_addr, iface_param->value,
416 sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
417 break;
418 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
419 /* Autocfg applies to even interface */
420 if (iface_param->iface_num & 0x1)
421 break;
422
423 if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_DISABLE)
424 init_fw_cb->ipv6_addtl_opts &=
425 cpu_to_le16(
426 ~IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
427 else if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_ND_ENABLE)
428 init_fw_cb->ipv6_addtl_opts |=
429 cpu_to_le16(
430 IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
431 else
432 ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
433 "IPv6 addr\n");
434 break;
435 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
436 /* Autocfg applies to even interface */
437 if (iface_param->iface_num & 0x1)
438 break;
439
440 if (iface_param->value[0] ==
441 ISCSI_IPV6_LINKLOCAL_AUTOCFG_ENABLE)
442 init_fw_cb->ipv6_addtl_opts |= cpu_to_le16(
443 IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
444 else if (iface_param->value[0] ==
445 ISCSI_IPV6_LINKLOCAL_AUTOCFG_DISABLE)
446 init_fw_cb->ipv6_addtl_opts &= cpu_to_le16(
447 ~IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
448 else
449 ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
450 "IPv6 linklocal addr\n");
451 break;
452 case ISCSI_NET_PARAM_IPV6_ROUTER_AUTOCFG:
453 /* Autocfg applies to even interface */
454 if (iface_param->iface_num & 0x1)
455 break;
456
457 if (iface_param->value[0] == ISCSI_IPV6_ROUTER_AUTOCFG_ENABLE)
458 memset(init_fw_cb->ipv6_dflt_rtr_addr, 0,
459 sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
460 break;
461 case ISCSI_NET_PARAM_IFACE_ENABLE:
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500462 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
Mike Christied00efe32011-07-25 13:48:38 -0500463 init_fw_cb->ipv6_opts |=
464 cpu_to_le16(IPV6_OPT_IPV6_PROTOCOL_ENABLE);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500465 qla4xxx_create_ipv6_iface(ha);
466 } else {
Mike Christied00efe32011-07-25 13:48:38 -0500467 init_fw_cb->ipv6_opts &=
468 cpu_to_le16(~IPV6_OPT_IPV6_PROTOCOL_ENABLE &
469 0xFFFF);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500470 qla4xxx_destroy_ipv6_iface(ha);
471 }
Mike Christied00efe32011-07-25 13:48:38 -0500472 break;
473 case ISCSI_NET_PARAM_VLAN_ID:
474 if (iface_param->len != sizeof(init_fw_cb->ipv6_vlan_tag))
475 break;
476 init_fw_cb->ipv6_vlan_tag = *(uint16_t *)iface_param->value;
477 break;
478 default:
479 ql4_printk(KERN_ERR, ha, "Unknown IPv6 param = %d\n",
480 iface_param->param);
481 break;
482 }
483}
484
485static void qla4xxx_set_ipv4(struct scsi_qla_host *ha,
486 struct iscsi_iface_param_info *iface_param,
487 struct addr_ctrl_blk *init_fw_cb)
488{
489 switch (iface_param->param) {
490 case ISCSI_NET_PARAM_IPV4_ADDR:
491 memcpy(init_fw_cb->ipv4_addr, iface_param->value,
492 sizeof(init_fw_cb->ipv4_addr));
493 break;
494 case ISCSI_NET_PARAM_IPV4_SUBNET:
495 memcpy(init_fw_cb->ipv4_subnet, iface_param->value,
496 sizeof(init_fw_cb->ipv4_subnet));
497 break;
498 case ISCSI_NET_PARAM_IPV4_GW:
499 memcpy(init_fw_cb->ipv4_gw_addr, iface_param->value,
500 sizeof(init_fw_cb->ipv4_gw_addr));
501 break;
502 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
503 if (iface_param->value[0] == ISCSI_BOOTPROTO_DHCP)
504 init_fw_cb->ipv4_tcp_opts |=
505 cpu_to_le16(TCPOPT_DHCP_ENABLE);
506 else if (iface_param->value[0] == ISCSI_BOOTPROTO_STATIC)
507 init_fw_cb->ipv4_tcp_opts &=
508 cpu_to_le16(~TCPOPT_DHCP_ENABLE);
509 else
510 ql4_printk(KERN_ERR, ha, "Invalid IPv4 bootproto\n");
511 break;
512 case ISCSI_NET_PARAM_IFACE_ENABLE:
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500513 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
Mike Christied00efe32011-07-25 13:48:38 -0500514 init_fw_cb->ipv4_ip_opts |=
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500515 cpu_to_le16(IPOPT_IPV4_PROTOCOL_ENABLE);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500516 qla4xxx_create_ipv4_iface(ha);
517 } else {
Mike Christied00efe32011-07-25 13:48:38 -0500518 init_fw_cb->ipv4_ip_opts &=
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500519 cpu_to_le16(~IPOPT_IPV4_PROTOCOL_ENABLE &
Mike Christied00efe32011-07-25 13:48:38 -0500520 0xFFFF);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500521 qla4xxx_destroy_ipv4_iface(ha);
522 }
Mike Christied00efe32011-07-25 13:48:38 -0500523 break;
524 case ISCSI_NET_PARAM_VLAN_ID:
525 if (iface_param->len != sizeof(init_fw_cb->ipv4_vlan_tag))
526 break;
527 init_fw_cb->ipv4_vlan_tag = *(uint16_t *)iface_param->value;
528 break;
529 default:
530 ql4_printk(KERN_ERR, ha, "Unknown IPv4 param = %d\n",
531 iface_param->param);
532 break;
533 }
534}
535
536static void
537qla4xxx_initcb_to_acb(struct addr_ctrl_blk *init_fw_cb)
538{
539 struct addr_ctrl_blk_def *acb;
540 acb = (struct addr_ctrl_blk_def *)init_fw_cb;
541 memset(acb->reserved1, 0, sizeof(acb->reserved1));
542 memset(acb->reserved2, 0, sizeof(acb->reserved2));
543 memset(acb->reserved3, 0, sizeof(acb->reserved3));
544 memset(acb->reserved4, 0, sizeof(acb->reserved4));
545 memset(acb->reserved5, 0, sizeof(acb->reserved5));
546 memset(acb->reserved6, 0, sizeof(acb->reserved6));
547 memset(acb->reserved7, 0, sizeof(acb->reserved7));
548 memset(acb->reserved8, 0, sizeof(acb->reserved8));
549 memset(acb->reserved9, 0, sizeof(acb->reserved9));
550 memset(acb->reserved10, 0, sizeof(acb->reserved10));
551 memset(acb->reserved11, 0, sizeof(acb->reserved11));
552 memset(acb->reserved12, 0, sizeof(acb->reserved12));
553 memset(acb->reserved13, 0, sizeof(acb->reserved13));
554 memset(acb->reserved14, 0, sizeof(acb->reserved14));
555 memset(acb->reserved15, 0, sizeof(acb->reserved15));
556}
557
558static int
559qla4xxx_iface_set_param(struct Scsi_Host *shost, char *data, int count)
560{
561 struct scsi_qla_host *ha = to_qla_host(shost);
562 int rval = 0;
563 struct iscsi_iface_param_info *iface_param = NULL;
564 struct addr_ctrl_blk *init_fw_cb = NULL;
565 dma_addr_t init_fw_cb_dma;
566 uint32_t mbox_cmd[MBOX_REG_COUNT];
567 uint32_t mbox_sts[MBOX_REG_COUNT];
568 uint32_t total_param_count;
569 uint32_t length;
570
571 init_fw_cb = dma_alloc_coherent(&ha->pdev->dev,
572 sizeof(struct addr_ctrl_blk),
573 &init_fw_cb_dma, GFP_KERNEL);
574 if (!init_fw_cb) {
575 ql4_printk(KERN_ERR, ha, "%s: Unable to alloc init_cb\n",
576 __func__);
577 return -ENOMEM;
578 }
579
580 memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
581 memset(&mbox_cmd, 0, sizeof(mbox_cmd));
582 memset(&mbox_sts, 0, sizeof(mbox_sts));
583
584 if (qla4xxx_get_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma)) {
585 ql4_printk(KERN_ERR, ha, "%s: get ifcb failed\n", __func__);
586 rval = -EIO;
587 goto exit_init_fw_cb;
588 }
589
590 total_param_count = count;
591 iface_param = (struct iscsi_iface_param_info *)data;
592
593 for ( ; total_param_count != 0; total_param_count--) {
594 length = iface_param->len;
595
596 if (iface_param->param_type != ISCSI_NET_PARAM)
597 continue;
598
599 switch (iface_param->iface_type) {
600 case ISCSI_IFACE_TYPE_IPV4:
601 switch (iface_param->iface_num) {
602 case 0:
603 qla4xxx_set_ipv4(ha, iface_param, init_fw_cb);
604 break;
605 default:
606 /* Cannot have more than one IPv4 interface */
607 ql4_printk(KERN_ERR, ha, "Invalid IPv4 iface "
608 "number = %d\n",
609 iface_param->iface_num);
610 break;
611 }
612 break;
613 case ISCSI_IFACE_TYPE_IPV6:
614 switch (iface_param->iface_num) {
615 case 0:
616 case 1:
617 qla4xxx_set_ipv6(ha, iface_param, init_fw_cb);
618 break;
619 default:
620 /* Cannot have more than two IPv6 interface */
621 ql4_printk(KERN_ERR, ha, "Invalid IPv6 iface "
622 "number = %d\n",
623 iface_param->iface_num);
624 break;
625 }
626 break;
627 default:
628 ql4_printk(KERN_ERR, ha, "Invalid iface type\n");
629 break;
630 }
631
632 iface_param = (struct iscsi_iface_param_info *)
633 ((uint8_t *)iface_param +
634 sizeof(struct iscsi_iface_param_info) + length);
635 }
636
637 init_fw_cb->cookie = cpu_to_le32(0x11BEAD5A);
638
639 rval = qla4xxx_set_flash(ha, init_fw_cb_dma, FLASH_SEGMENT_IFCB,
640 sizeof(struct addr_ctrl_blk),
641 FLASH_OPT_RMW_COMMIT);
642 if (rval != QLA_SUCCESS) {
643 ql4_printk(KERN_ERR, ha, "%s: set flash mbx failed\n",
644 __func__);
645 rval = -EIO;
646 goto exit_init_fw_cb;
647 }
648
649 qla4xxx_disable_acb(ha);
650
651 qla4xxx_initcb_to_acb(init_fw_cb);
652
653 rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma);
654 if (rval != QLA_SUCCESS) {
655 ql4_printk(KERN_ERR, ha, "%s: set acb mbx failed\n",
656 __func__);
657 rval = -EIO;
658 goto exit_init_fw_cb;
659 }
660
661 memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
662 qla4xxx_update_local_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb,
663 init_fw_cb_dma);
664
665exit_init_fw_cb:
666 dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk),
667 init_fw_cb, init_fw_cb_dma);
668
669 return rval;
670}
671
David Somayajuluafaf5a22006-09-19 10:28:00 -0700672static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
673 enum iscsi_param param, char *buf)
674{
675 struct ddb_entry *ddb_entry = sess->dd_data;
676 int len;
677
678 switch (param) {
679 case ISCSI_PARAM_TARGET_NAME:
680 len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
681 ddb_entry->iscsi_name);
682 break;
683 case ISCSI_PARAM_TPGT:
684 len = sprintf(buf, "%u\n", ddb_entry->tpgt);
685 break;
Vikas Chaudhary3b2bef12010-07-10 14:51:30 +0530686 case ISCSI_PARAM_TARGET_ALIAS:
687 len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
688 ddb_entry->iscsi_alias);
689 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700690 default:
691 return -ENOSYS;
692 }
693
694 return len;
695}
696
697static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
698 enum iscsi_param param, char *buf)
699{
700 struct iscsi_cls_session *session;
701 struct ddb_entry *ddb_entry;
702 int len;
703
704 session = iscsi_dev_to_session(conn->dev.parent);
705 ddb_entry = session->dd_data;
706
707 switch (param) {
708 case ISCSI_PARAM_CONN_PORT:
709 len = sprintf(buf, "%hu\n", ddb_entry->port);
710 break;
711 case ISCSI_PARAM_CONN_ADDRESS:
712 /* TODO: what are the ipv6 bits */
Harvey Harrison63779432008-10-31 00:56:00 -0700713 len = sprintf(buf, "%pI4\n", &ddb_entry->ip_addr);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700714 break;
715 default:
716 return -ENOSYS;
717 }
718
719 return len;
720}
721
Mike Christie2174a042007-05-30 12:57:10 -0500722static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
723 enum iscsi_tgt_dscvr type, uint32_t enable,
724 struct sockaddr *dst_addr)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700725{
726 struct scsi_qla_host *ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700727 struct sockaddr_in *addr;
728 struct sockaddr_in6 *addr6;
729 int ret = 0;
730
David Somayajuluafaf5a22006-09-19 10:28:00 -0700731 ha = (struct scsi_qla_host *) shost->hostdata;
732
733 switch (type) {
734 case ISCSI_TGT_DSCVR_SEND_TARGETS:
735 if (dst_addr->sa_family == AF_INET) {
736 addr = (struct sockaddr_in *)dst_addr;
737 if (qla4xxx_send_tgts(ha, (char *)&addr->sin_addr,
738 addr->sin_port) != QLA_SUCCESS)
739 ret = -EIO;
740 } else if (dst_addr->sa_family == AF_INET6) {
741 /*
742 * TODO: fix qla4xxx_send_tgts
743 */
744 addr6 = (struct sockaddr_in6 *)dst_addr;
745 if (qla4xxx_send_tgts(ha, (char *)&addr6->sin6_addr,
746 addr6->sin6_port) != QLA_SUCCESS)
747 ret = -EIO;
748 } else
749 ret = -ENOSYS;
750 break;
751 default:
752 ret = -ENOSYS;
753 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700754 return ret;
755}
756
757void qla4xxx_destroy_sess(struct ddb_entry *ddb_entry)
758{
759 if (!ddb_entry->sess)
760 return;
761
762 if (ddb_entry->conn) {
Mike Christie26974782007-12-13 12:43:29 -0600763 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700764 iscsi_remove_session(ddb_entry->sess);
765 }
766 iscsi_free_session(ddb_entry->sess);
767}
768
769int qla4xxx_add_sess(struct ddb_entry *ddb_entry)
770{
771 int err;
772
Vikas Chaudhary30387272011-03-21 03:34:32 -0700773 ddb_entry->sess->recovery_tmo = ql4xsess_recovery_tmo;
Vikas Chaudhary3013cea2010-07-30 14:25:46 +0530774
David Somayajuluafaf5a22006-09-19 10:28:00 -0700775 err = iscsi_add_session(ddb_entry->sess, ddb_entry->fw_ddb_index);
776 if (err) {
777 DEBUG2(printk(KERN_ERR "Could not add session.\n"));
778 return err;
779 }
780
Mike Christie5d91e202008-05-21 15:54:01 -0500781 ddb_entry->conn = iscsi_create_conn(ddb_entry->sess, 0, 0);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700782 if (!ddb_entry->conn) {
783 iscsi_remove_session(ddb_entry->sess);
784 DEBUG2(printk(KERN_ERR "Could not add connection.\n"));
785 return -ENOMEM;
786 }
Mike Christieb6359302008-01-31 13:36:44 -0600787
788 /* finally ready to go */
789 iscsi_unblock_session(ddb_entry->sess);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700790 return 0;
791}
792
793struct ddb_entry *qla4xxx_alloc_sess(struct scsi_qla_host *ha)
794{
795 struct ddb_entry *ddb_entry;
796 struct iscsi_cls_session *sess;
797
Mike Christie5d91e202008-05-21 15:54:01 -0500798 sess = iscsi_alloc_session(ha->host, &qla4xxx_iscsi_transport,
799 sizeof(struct ddb_entry));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700800 if (!sess)
801 return NULL;
802
803 ddb_entry = sess->dd_data;
804 memset(ddb_entry, 0, sizeof(*ddb_entry));
805 ddb_entry->ha = ha;
806 ddb_entry->sess = sess;
807 return ddb_entry;
808}
809
Mike Christie024f8012008-03-04 13:26:54 -0600810static void qla4xxx_scan_start(struct Scsi_Host *shost)
811{
812 struct scsi_qla_host *ha = shost_priv(shost);
813 struct ddb_entry *ddb_entry, *ddbtemp;
814
815 /* finish setup of sessions that were already setup in firmware */
816 list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
817 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE)
818 qla4xxx_add_sess(ddb_entry);
819 }
820}
821
David Somayajuluafaf5a22006-09-19 10:28:00 -0700822/*
823 * Timer routines
824 */
825
826static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
827 unsigned long interval)
828{
829 DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
830 __func__, ha->host->host_no));
831 init_timer(&ha->timer);
832 ha->timer.expires = jiffies + interval * HZ;
833 ha->timer.data = (unsigned long)ha;
834 ha->timer.function = (void (*)(unsigned long))func;
835 add_timer(&ha->timer);
836 ha->timer_active = 1;
837}
838
839static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
840{
841 del_timer_sync(&ha->timer);
842 ha->timer_active = 0;
843}
844
845/***
846 * qla4xxx_mark_device_missing - mark a device as missing.
847 * @ha: Pointer to host adapter structure.
848 * @ddb_entry: Pointer to device database entry
849 *
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530850 * This routine marks a device missing and close connection.
David Somayajuluafaf5a22006-09-19 10:28:00 -0700851 **/
852void qla4xxx_mark_device_missing(struct scsi_qla_host *ha,
853 struct ddb_entry *ddb_entry)
854{
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530855 if ((atomic_read(&ddb_entry->state) != DDB_STATE_DEAD)) {
856 atomic_set(&ddb_entry->state, DDB_STATE_MISSING);
857 DEBUG2(printk("scsi%ld: ddb [%d] marked MISSING\n",
858 ha->host_no, ddb_entry->fw_ddb_index));
859 } else
860 DEBUG2(printk("scsi%ld: ddb [%d] DEAD\n", ha->host_no,
861 ddb_entry->fw_ddb_index))
862
Mike Christieb6359302008-01-31 13:36:44 -0600863 iscsi_block_session(ddb_entry->sess);
Mike Christiee5bd7b52008-09-24 11:46:10 -0500864 iscsi_conn_error_event(ddb_entry->conn, ISCSI_ERR_CONN_FAILED);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700865}
866
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530867/**
868 * qla4xxx_mark_all_devices_missing - mark all devices as missing.
869 * @ha: Pointer to host adapter structure.
870 *
871 * This routine marks a device missing and resets the relogin retry count.
872 **/
873void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
874{
875 struct ddb_entry *ddb_entry, *ddbtemp;
876 list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
877 qla4xxx_mark_device_missing(ha, ddb_entry);
878 }
879}
880
David Somayajuluafaf5a22006-09-19 10:28:00 -0700881static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
882 struct ddb_entry *ddb_entry,
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -0700883 struct scsi_cmnd *cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700884{
885 struct srb *srb;
886
887 srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
888 if (!srb)
889 return srb;
890
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530891 kref_init(&srb->srb_ref);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700892 srb->ha = ha;
893 srb->ddb = ddb_entry;
894 srb->cmd = cmd;
895 srb->flags = 0;
Vikas Chaudhary53698872010-04-28 11:41:59 +0530896 CMD_SP(cmd) = (void *)srb;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700897
898 return srb;
899}
900
901static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
902{
903 struct scsi_cmnd *cmd = srb->cmd;
904
905 if (srb->flags & SRB_DMA_VALID) {
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +0900906 scsi_dma_unmap(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700907 srb->flags &= ~SRB_DMA_VALID;
908 }
Vikas Chaudhary53698872010-04-28 11:41:59 +0530909 CMD_SP(cmd) = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700910}
911
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530912void qla4xxx_srb_compl(struct kref *ref)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700913{
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530914 struct srb *srb = container_of(ref, struct srb, srb_ref);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700915 struct scsi_cmnd *cmd = srb->cmd;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530916 struct scsi_qla_host *ha = srb->ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700917
918 qla4xxx_srb_free_dma(ha, srb);
919
920 mempool_free(srb, ha->srb_mempool);
921
922 cmd->scsi_done(cmd);
923}
924
925/**
926 * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -0700927 * @host: scsi host
David Somayajuluafaf5a22006-09-19 10:28:00 -0700928 * @cmd: Pointer to Linux's SCSI command structure
David Somayajuluafaf5a22006-09-19 10:28:00 -0700929 *
930 * Remarks:
931 * This routine is invoked by Linux to send a SCSI command to the driver.
932 * The mid-level driver tries to ensure that queuecommand never gets
933 * invoked concurrently with itself or the interrupt handler (although
934 * the interrupt handler may call this routine as part of request-
935 * completion handling). Unfortunely, it sometimes calls the scheduler
936 * in interrupt context which is a big NO! NO!.
937 **/
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -0700938static int qla4xxx_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700939{
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -0700940 struct scsi_qla_host *ha = to_qla_host(host);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700941 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christie7fb19212008-01-31 13:36:45 -0600942 struct iscsi_cls_session *sess = ddb_entry->sess;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700943 struct srb *srb;
944 int rval;
945
Lalit Chandivade2232be02010-07-30 14:38:47 +0530946 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
947 if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
948 cmd->result = DID_NO_CONNECT << 16;
949 else
950 cmd->result = DID_REQUEUE << 16;
951 goto qc_fail_command;
952 }
953
Mike Christie7fb19212008-01-31 13:36:45 -0600954 if (!sess) {
955 cmd->result = DID_IMM_RETRY << 16;
956 goto qc_fail_command;
957 }
958
959 rval = iscsi_session_chkready(sess);
960 if (rval) {
961 cmd->result = rval;
962 goto qc_fail_command;
963 }
964
David Somayajuluafaf5a22006-09-19 10:28:00 -0700965 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
966 if (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD) {
967 cmd->result = DID_NO_CONNECT << 16;
968 goto qc_fail_command;
969 }
Mike Christiec5e98e92008-08-17 15:24:39 -0500970 return SCSI_MLQUEUE_TARGET_BUSY;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700971 }
972
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530973 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
974 test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
975 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
976 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
977 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
978 !test_bit(AF_ONLINE, &ha->flags) ||
979 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
David C Somayajulu477ffb92007-01-22 12:26:11 -0800980 goto qc_host_busy;
981
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -0700982 srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700983 if (!srb)
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -0700984 goto qc_host_busy;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700985
986 rval = qla4xxx_send_command_to_isp(ha, srb);
987 if (rval != QLA_SUCCESS)
988 goto qc_host_busy_free_sp;
989
David Somayajuluafaf5a22006-09-19 10:28:00 -0700990 return 0;
991
992qc_host_busy_free_sp:
993 qla4xxx_srb_free_dma(ha, srb);
994 mempool_free(srb, ha->srb_mempool);
995
David Somayajuluafaf5a22006-09-19 10:28:00 -0700996qc_host_busy:
997 return SCSI_MLQUEUE_HOST_BUSY;
998
999qc_fail_command:
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001000 cmd->scsi_done(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001001
1002 return 0;
1003}
1004
1005/**
1006 * qla4xxx_mem_free - frees memory allocated to adapter
1007 * @ha: Pointer to host adapter structure.
1008 *
1009 * Frees memory previously allocated by qla4xxx_mem_alloc
1010 **/
1011static void qla4xxx_mem_free(struct scsi_qla_host *ha)
1012{
1013 if (ha->queues)
1014 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
1015 ha->queues_dma);
1016
1017 ha->queues_len = 0;
1018 ha->queues = NULL;
1019 ha->queues_dma = 0;
1020 ha->request_ring = NULL;
1021 ha->request_dma = 0;
1022 ha->response_ring = NULL;
1023 ha->response_dma = 0;
1024 ha->shadow_regs = NULL;
1025 ha->shadow_regs_dma = 0;
1026
1027 /* Free srb pool. */
1028 if (ha->srb_mempool)
1029 mempool_destroy(ha->srb_mempool);
1030
1031 ha->srb_mempool = NULL;
1032
1033 /* release io space registers */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301034 if (is_qla8022(ha)) {
1035 if (ha->nx_pcibase)
1036 iounmap(
1037 (struct device_reg_82xx __iomem *)ha->nx_pcibase);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301038 } else if (ha->reg)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001039 iounmap(ha->reg);
1040 pci_release_regions(ha->pdev);
1041}
1042
1043/**
1044 * qla4xxx_mem_alloc - allocates memory for use by adapter.
1045 * @ha: Pointer to host adapter structure
1046 *
1047 * Allocates DMA memory for request and response queues. Also allocates memory
1048 * for srbs.
1049 **/
1050static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
1051{
1052 unsigned long align;
1053
1054 /* Allocate contiguous block of DMA memory for queues. */
1055 ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
1056 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
1057 sizeof(struct shadow_regs) +
1058 MEM_ALIGN_VALUE +
1059 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
1060 ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
1061 &ha->queues_dma, GFP_KERNEL);
1062 if (ha->queues == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301063 ql4_printk(KERN_WARNING, ha,
1064 "Memory Allocation failed - queues.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001065
1066 goto mem_alloc_error_exit;
1067 }
1068 memset(ha->queues, 0, ha->queues_len);
1069
1070 /*
1071 * As per RISC alignment requirements -- the bus-address must be a
1072 * multiple of the request-ring size (in bytes).
1073 */
1074 align = 0;
1075 if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
1076 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
1077 (MEM_ALIGN_VALUE - 1));
1078
1079 /* Update request and response queue pointers. */
1080 ha->request_dma = ha->queues_dma + align;
1081 ha->request_ring = (struct queue_entry *) (ha->queues + align);
1082 ha->response_dma = ha->queues_dma + align +
1083 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
1084 ha->response_ring = (struct queue_entry *) (ha->queues + align +
1085 (REQUEST_QUEUE_DEPTH *
1086 QUEUE_SIZE));
1087 ha->shadow_regs_dma = ha->queues_dma + align +
1088 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
1089 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
1090 ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
1091 (REQUEST_QUEUE_DEPTH *
1092 QUEUE_SIZE) +
1093 (RESPONSE_QUEUE_DEPTH *
1094 QUEUE_SIZE));
1095
1096 /* Allocate memory for srb pool. */
1097 ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
1098 mempool_free_slab, srb_cachep);
1099 if (ha->srb_mempool == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301100 ql4_printk(KERN_WARNING, ha,
1101 "Memory Allocation failed - SRB Pool.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001102
1103 goto mem_alloc_error_exit;
1104 }
1105
1106 return QLA_SUCCESS;
1107
1108mem_alloc_error_exit:
1109 qla4xxx_mem_free(ha);
1110 return QLA_ERROR;
1111}
1112
1113/**
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301114 * qla4_8xxx_check_fw_alive - Check firmware health
1115 * @ha: Pointer to host adapter structure.
1116 *
1117 * Context: Interrupt
1118 **/
1119static void qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
1120{
1121 uint32_t fw_heartbeat_counter, halt_status;
1122
1123 fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
Lalit Chandivade2232be02010-07-30 14:38:47 +05301124 /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
1125 if (fw_heartbeat_counter == 0xffffffff) {
1126 DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
1127 "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
1128 ha->host_no, __func__));
1129 return;
1130 }
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301131
1132 if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
1133 ha->seconds_since_last_heartbeat++;
1134 /* FW not alive after 2 seconds */
1135 if (ha->seconds_since_last_heartbeat == 2) {
1136 ha->seconds_since_last_heartbeat = 0;
1137 halt_status = qla4_8xxx_rd_32(ha,
Vikas Chaudhary68d92eb2011-05-17 23:17:05 -07001138 QLA82XX_PEG_HALT_STATUS1);
1139
1140 ql4_printk(KERN_INFO, ha,
1141 "scsi(%ld): %s, Dumping hw/fw registers:\n "
1142 " PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2:"
1143 " 0x%x,\n PEG_NET_0_PC: 0x%x, PEG_NET_1_PC:"
1144 " 0x%x,\n PEG_NET_2_PC: 0x%x, PEG_NET_3_PC:"
1145 " 0x%x,\n PEG_NET_4_PC: 0x%x\n",
1146 ha->host_no, __func__, halt_status,
1147 qla4_8xxx_rd_32(ha,
1148 QLA82XX_PEG_HALT_STATUS2),
1149 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_0 +
1150 0x3c),
1151 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_1 +
1152 0x3c),
1153 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_2 +
1154 0x3c),
1155 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_3 +
1156 0x3c),
1157 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_4 +
1158 0x3c));
Nilesh Javali21033632010-07-30 14:28:07 +05301159
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301160 /* Since we cannot change dev_state in interrupt
1161 * context, set appropriate DPC flag then wakeup
1162 * DPC */
1163 if (halt_status & HALT_STATUS_UNRECOVERABLE)
1164 set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
1165 else {
1166 printk("scsi%ld: %s: detect abort needed!\n",
1167 ha->host_no, __func__);
1168 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1169 }
1170 qla4xxx_wake_dpc(ha);
Nilesh Javali21033632010-07-30 14:28:07 +05301171 qla4xxx_mailbox_premature_completion(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301172 }
Lalit Chandivade99457d72010-10-06 22:49:32 -07001173 } else
1174 ha->seconds_since_last_heartbeat = 0;
1175
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301176 ha->fw_heartbeat_counter = fw_heartbeat_counter;
1177}
1178
1179/**
1180 * qla4_8xxx_watchdog - Poll dev state
1181 * @ha: Pointer to host adapter structure.
1182 *
1183 * Context: Interrupt
1184 **/
1185void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
1186{
1187 uint32_t dev_state;
1188
1189 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
1190
1191 /* don't poll if reset is going on */
Lalit Chandivaded56a1f72010-12-02 22:12:45 -08001192 if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
1193 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
Vikas Chaudhary977f46a2011-05-17 23:17:08 -07001194 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags))) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301195 if (dev_state == QLA82XX_DEV_NEED_RESET &&
1196 !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
Vikas Chaudhary3930b8c2010-12-02 22:12:47 -08001197 if (!ql4xdontresethba) {
1198 ql4_printk(KERN_INFO, ha, "%s: HW State: "
1199 "NEED RESET!\n", __func__);
1200 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1201 qla4xxx_wake_dpc(ha);
1202 qla4xxx_mailbox_premature_completion(ha);
1203 }
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301204 } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
1205 !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
Vikas Chaudhary3930b8c2010-12-02 22:12:47 -08001206 ql4_printk(KERN_INFO, ha, "%s: HW State: NEED QUIES!\n",
1207 __func__);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301208 set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
1209 qla4xxx_wake_dpc(ha);
1210 } else {
1211 /* Check firmware health */
1212 qla4_8xxx_check_fw_alive(ha);
1213 }
1214 }
1215}
1216
1217/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07001218 * qla4xxx_timer - checks every second for work to do.
1219 * @ha: Pointer to host adapter structure.
1220 **/
1221static void qla4xxx_timer(struct scsi_qla_host *ha)
1222{
1223 struct ddb_entry *ddb_entry, *dtemp;
1224 int start_dpc = 0;
Lalit Chandivade2232be02010-07-30 14:38:47 +05301225 uint16_t w;
1226
1227 /* If we are in the middle of AER/EEH processing
1228 * skip any processing and reschedule the timer
1229 */
1230 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1231 mod_timer(&ha->timer, jiffies + HZ);
1232 return;
1233 }
1234
1235 /* Hardware read to trigger an EEH error during mailbox waits. */
1236 if (!pci_channel_offline(ha->pdev))
1237 pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001238
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301239 if (is_qla8022(ha)) {
1240 qla4_8xxx_watchdog(ha);
1241 }
1242
David Somayajuluafaf5a22006-09-19 10:28:00 -07001243 /* Search for relogin's to time-out and port down retry. */
1244 list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
1245 /* Count down time between sending relogins */
1246 if (adapter_up(ha) &&
1247 !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
1248 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
1249 if (atomic_read(&ddb_entry->retry_relogin_timer) !=
1250 INVALID_ENTRY) {
1251 if (atomic_read(&ddb_entry->retry_relogin_timer)
1252 == 0) {
1253 atomic_set(&ddb_entry->
1254 retry_relogin_timer,
1255 INVALID_ENTRY);
1256 set_bit(DPC_RELOGIN_DEVICE,
1257 &ha->dpc_flags);
1258 set_bit(DF_RELOGIN, &ddb_entry->flags);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301259 DEBUG2(printk("scsi%ld: %s: ddb [%d]"
David Somayajuluafaf5a22006-09-19 10:28:00 -07001260 " login device\n",
1261 ha->host_no, __func__,
1262 ddb_entry->fw_ddb_index));
1263 } else
1264 atomic_dec(&ddb_entry->
1265 retry_relogin_timer);
1266 }
1267 }
1268
1269 /* Wait for relogin to timeout */
1270 if (atomic_read(&ddb_entry->relogin_timer) &&
1271 (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
1272 /*
1273 * If the relogin times out and the device is
1274 * still NOT ONLINE then try and relogin again.
1275 */
1276 if (atomic_read(&ddb_entry->state) !=
1277 DDB_STATE_ONLINE &&
1278 ddb_entry->fw_ddb_device_state ==
1279 DDB_DS_SESSION_FAILED) {
1280 /* Reset retry relogin timer */
1281 atomic_inc(&ddb_entry->relogin_retry_count);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301282 DEBUG2(printk("scsi%ld: ddb [%d] relogin"
David Somayajuluafaf5a22006-09-19 10:28:00 -07001283 " timed out-retrying"
1284 " relogin (%d)\n",
1285 ha->host_no,
1286 ddb_entry->fw_ddb_index,
1287 atomic_read(&ddb_entry->
1288 relogin_retry_count))
1289 );
1290 start_dpc++;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301291 DEBUG(printk("scsi%ld:%d:%d: ddb [%d] "
Justin P. Mattockcd09b2c2011-01-28 11:49:08 -08001292 "initiate relogin after"
David Somayajuluafaf5a22006-09-19 10:28:00 -07001293 " %d seconds\n",
1294 ha->host_no, ddb_entry->bus,
1295 ddb_entry->target,
1296 ddb_entry->fw_ddb_index,
1297 ddb_entry->default_time2wait + 4)
1298 );
1299
1300 atomic_set(&ddb_entry->retry_relogin_timer,
1301 ddb_entry->default_time2wait + 4);
1302 }
1303 }
1304 }
1305
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301306 if (!is_qla8022(ha)) {
1307 /* Check for heartbeat interval. */
1308 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
1309 ha->heartbeat_interval != 0) {
1310 ha->seconds_since_last_heartbeat++;
1311 if (ha->seconds_since_last_heartbeat >
1312 ha->heartbeat_interval + 2)
1313 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1314 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001315 }
1316
David Somayajuluafaf5a22006-09-19 10:28:00 -07001317 /* Wakeup the dpc routine for this adapter, if needed. */
Lalit Chandivade1b468072011-05-17 23:17:09 -07001318 if (start_dpc ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07001319 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1320 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
1321 test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301322 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07001323 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1324 test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301325 test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301326 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
1327 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
Lalit Chandivade1b468072011-05-17 23:17:09 -07001328 test_bit(DPC_AEN, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001329 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
1330 " - dpc flags = 0x%lx\n",
1331 ha->host_no, __func__, ha->dpc_flags));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301332 qla4xxx_wake_dpc(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001333 }
1334
1335 /* Reschedule timer thread to call us back in one second */
1336 mod_timer(&ha->timer, jiffies + HZ);
1337
1338 DEBUG2(ha->seconds_since_last_intr++);
1339}
1340
1341/**
1342 * qla4xxx_cmd_wait - waits for all outstanding commands to complete
1343 * @ha: Pointer to host adapter structure.
1344 *
1345 * This routine stalls the driver until all outstanding commands are returned.
1346 * Caller must release the Hardware Lock prior to calling this routine.
1347 **/
1348static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
1349{
1350 uint32_t index = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001351 unsigned long flags;
1352 struct scsi_cmnd *cmd;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001353
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301354 unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
1355
1356 DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
1357 "complete\n", WAIT_CMD_TOV));
1358
1359 while (!time_after_eq(jiffies, wtime)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001360 spin_lock_irqsave(&ha->hardware_lock, flags);
1361 /* Find a command that hasn't completed. */
1362 for (index = 0; index < ha->host->can_queue; index++) {
1363 cmd = scsi_host_find_tag(ha->host, index);
Mike Christiea1e00632010-10-26 05:45:30 -07001364 /*
1365 * We cannot just check if the index is valid,
1366 * becase if we are run from the scsi eh, then
1367 * the scsi/block layer is going to prevent
1368 * the tag from being released.
1369 */
1370 if (cmd != NULL && CMD_SP(cmd))
David Somayajuluafaf5a22006-09-19 10:28:00 -07001371 break;
1372 }
1373 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1374
1375 /* If No Commands are pending, wait is complete */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301376 if (index == ha->host->can_queue)
1377 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001378
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301379 msleep(1000);
1380 }
1381 /* If we timed out on waiting for commands to come back
1382 * return ERROR. */
1383 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001384}
1385
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301386int qla4xxx_hw_reset(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001387{
David Somayajuluafaf5a22006-09-19 10:28:00 -07001388 uint32_t ctrl_status;
David C Somayajulu477ffb92007-01-22 12:26:11 -08001389 unsigned long flags = 0;
1390
1391 DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001392
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301393 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
1394 return QLA_ERROR;
1395
David Somayajuluafaf5a22006-09-19 10:28:00 -07001396 spin_lock_irqsave(&ha->hardware_lock, flags);
1397
1398 /*
1399 * If the SCSI Reset Interrupt bit is set, clear it.
1400 * Otherwise, the Soft Reset won't work.
1401 */
1402 ctrl_status = readw(&ha->reg->ctrl_status);
1403 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
1404 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
1405
1406 /* Issue Soft Reset */
1407 writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
1408 readl(&ha->reg->ctrl_status);
1409
1410 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301411 return QLA_SUCCESS;
David C Somayajulu477ffb92007-01-22 12:26:11 -08001412}
1413
1414/**
1415 * qla4xxx_soft_reset - performs soft reset.
1416 * @ha: Pointer to host adapter structure.
1417 **/
1418int qla4xxx_soft_reset(struct scsi_qla_host *ha)
1419{
1420 uint32_t max_wait_time;
1421 unsigned long flags = 0;
Vikas Chaudharyf931c532010-10-06 22:48:07 -07001422 int status;
David C Somayajulu477ffb92007-01-22 12:26:11 -08001423 uint32_t ctrl_status;
1424
Vikas Chaudharyf931c532010-10-06 22:48:07 -07001425 status = qla4xxx_hw_reset(ha);
1426 if (status != QLA_SUCCESS)
1427 return status;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001428
Vikas Chaudharyf931c532010-10-06 22:48:07 -07001429 status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001430 /* Wait until the Network Reset Intr bit is cleared */
1431 max_wait_time = RESET_INTR_TOV;
1432 do {
1433 spin_lock_irqsave(&ha->hardware_lock, flags);
1434 ctrl_status = readw(&ha->reg->ctrl_status);
1435 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1436
1437 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
1438 break;
1439
1440 msleep(1000);
1441 } while ((--max_wait_time));
1442
1443 if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
1444 DEBUG2(printk(KERN_WARNING
1445 "scsi%ld: Network Reset Intr not cleared by "
1446 "Network function, clearing it now!\n",
1447 ha->host_no));
1448 spin_lock_irqsave(&ha->hardware_lock, flags);
1449 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
1450 readl(&ha->reg->ctrl_status);
1451 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1452 }
1453
1454 /* Wait until the firmware tells us the Soft Reset is done */
1455 max_wait_time = SOFT_RESET_TOV;
1456 do {
1457 spin_lock_irqsave(&ha->hardware_lock, flags);
1458 ctrl_status = readw(&ha->reg->ctrl_status);
1459 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1460
1461 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
1462 status = QLA_SUCCESS;
1463 break;
1464 }
1465
1466 msleep(1000);
1467 } while ((--max_wait_time));
1468
1469 /*
1470 * Also, make sure that the SCSI Reset Interrupt bit has been cleared
1471 * after the soft reset has taken place.
1472 */
1473 spin_lock_irqsave(&ha->hardware_lock, flags);
1474 ctrl_status = readw(&ha->reg->ctrl_status);
1475 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
1476 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
1477 readl(&ha->reg->ctrl_status);
1478 }
1479 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1480
1481 /* If soft reset fails then most probably the bios on other
1482 * function is also enabled.
1483 * Since the initialization is sequential the other fn
1484 * wont be able to acknowledge the soft reset.
1485 * Issue a force soft reset to workaround this scenario.
1486 */
1487 if (max_wait_time == 0) {
1488 /* Issue Force Soft Reset */
1489 spin_lock_irqsave(&ha->hardware_lock, flags);
1490 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
1491 readl(&ha->reg->ctrl_status);
1492 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1493 /* Wait until the firmware tells us the Soft Reset is done */
1494 max_wait_time = SOFT_RESET_TOV;
1495 do {
1496 spin_lock_irqsave(&ha->hardware_lock, flags);
1497 ctrl_status = readw(&ha->reg->ctrl_status);
1498 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1499
1500 if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
1501 status = QLA_SUCCESS;
1502 break;
1503 }
1504
1505 msleep(1000);
1506 } while ((--max_wait_time));
1507 }
1508
1509 return status;
1510}
1511
1512/**
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301513 * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001514 * @ha: Pointer to host adapter structure.
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301515 * @res: returned scsi status
David Somayajuluafaf5a22006-09-19 10:28:00 -07001516 *
1517 * This routine is called just prior to a HARD RESET to return all
1518 * outstanding commands back to the Operating System.
1519 * Caller should make sure that the following locks are released
1520 * before this calling routine: Hardware lock, and io_request_lock.
1521 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301522static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001523{
1524 struct srb *srb;
1525 int i;
1526 unsigned long flags;
1527
1528 spin_lock_irqsave(&ha->hardware_lock, flags);
1529 for (i = 0; i < ha->host->can_queue; i++) {
1530 srb = qla4xxx_del_from_active_array(ha, i);
1531 if (srb != NULL) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301532 srb->cmd->result = res;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301533 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001534 }
1535 }
1536 spin_unlock_irqrestore(&ha->hardware_lock, flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001537}
1538
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301539void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
1540{
1541 clear_bit(AF_ONLINE, &ha->flags);
1542
1543 /* Disable the board */
1544 ql4_printk(KERN_INFO, ha, "Disabling the board\n");
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301545
1546 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
1547 qla4xxx_mark_all_devices_missing(ha);
1548 clear_bit(AF_INIT_DONE, &ha->flags);
1549}
1550
David Somayajuluafaf5a22006-09-19 10:28:00 -07001551/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07001552 * qla4xxx_recover_adapter - recovers adapter after a fatal error
1553 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001554 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301555static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001556{
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301557 int status = QLA_ERROR;
1558 uint8_t reset_chip = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001559
1560 /* Stall incoming I/O until we are done */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301561 scsi_block_requests(ha->host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001562 clear_bit(AF_ONLINE, &ha->flags);
Mike Christie50a29ae2008-03-04 13:26:53 -06001563
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301564 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001565
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301566 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001567
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301568 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
1569 reset_chip = 1;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001570
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301571 /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
1572 * do not reset adapter, jump to initialize_adapter */
1573 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1574 status = QLA_SUCCESS;
1575 goto recover_ha_init_adapter;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001576 }
1577
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301578 /* For the ISP-82xx adapter, issue a stop_firmware if invoked
1579 * from eh_host_reset or ioctl module */
1580 if (is_qla8022(ha) && !reset_chip &&
1581 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
1582
1583 DEBUG2(ql4_printk(KERN_INFO, ha,
1584 "scsi%ld: %s - Performing stop_firmware...\n",
1585 ha->host_no, __func__));
1586 status = ha->isp_ops->reset_firmware(ha);
1587 if (status == QLA_SUCCESS) {
Nilesh Javali2bd1e2b2010-10-06 22:49:20 -07001588 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
1589 qla4xxx_cmd_wait(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301590 ha->isp_ops->disable_intrs(ha);
1591 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1592 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1593 } else {
1594 /* If the stop_firmware fails then
1595 * reset the entire chip */
1596 reset_chip = 1;
1597 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1598 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1599 }
1600 }
1601
1602 /* Issue full chip reset if recovering from a catastrophic error,
1603 * or if stop_firmware fails for ISP-82xx.
1604 * This is the default case for ISP-4xxx */
1605 if (!is_qla8022(ha) || reset_chip) {
Nilesh Javali2bd1e2b2010-10-06 22:49:20 -07001606 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
1607 qla4xxx_cmd_wait(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301608 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1609 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1610 DEBUG2(ql4_printk(KERN_INFO, ha,
1611 "scsi%ld: %s - Performing chip reset..\n",
1612 ha->host_no, __func__));
1613 status = ha->isp_ops->reset_chip(ha);
1614 }
1615
1616 /* Flush any pending ddb changed AENs */
1617 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1618
1619recover_ha_init_adapter:
1620 /* Upon successful firmware/chip reset, re-initialize the adapter */
1621 if (status == QLA_SUCCESS) {
1622 /* For ISP-4xxx, force function 1 to always initialize
1623 * before function 3 to prevent both funcions from
1624 * stepping on top of the other */
1625 if (!is_qla8022(ha) && (ha->mac_index == 3))
1626 ssleep(6);
1627
1628 /* NOTE: AF_ONLINE flag set upon successful completion of
1629 * qla4xxx_initialize_adapter */
1630 status = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
1631 }
1632
1633 /* Retry failed adapter initialization, if necessary
1634 * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
1635 * case to prevent ping-pong resets between functions */
1636 if (!test_bit(AF_ONLINE, &ha->flags) &&
1637 !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001638 /* Adapter initialization failed, see if we can retry
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301639 * resetting the ha.
1640 * Since we don't want to block the DPC for too long
1641 * with multiple resets in the same thread,
1642 * utilize DPC to retry */
David Somayajuluafaf5a22006-09-19 10:28:00 -07001643 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
1644 ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
1645 DEBUG2(printk("scsi%ld: recover adapter - retrying "
1646 "(%d) more times\n", ha->host_no,
1647 ha->retry_reset_ha_cnt));
1648 set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1649 status = QLA_ERROR;
1650 } else {
1651 if (ha->retry_reset_ha_cnt > 0) {
1652 /* Schedule another Reset HA--DPC will retry */
1653 ha->retry_reset_ha_cnt--;
1654 DEBUG2(printk("scsi%ld: recover adapter - "
1655 "retry remaining %d\n",
1656 ha->host_no,
1657 ha->retry_reset_ha_cnt));
1658 status = QLA_ERROR;
1659 }
1660
1661 if (ha->retry_reset_ha_cnt == 0) {
1662 /* Recover adapter retries have been exhausted.
1663 * Adapter DEAD */
1664 DEBUG2(printk("scsi%ld: recover adapter "
1665 "failed - board disabled\n",
1666 ha->host_no));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301667 qla4xxx_dead_adapter_cleanup(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001668 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1669 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301670 clear_bit(DPC_RESET_HA_FW_CONTEXT,
David Somayajuluafaf5a22006-09-19 10:28:00 -07001671 &ha->dpc_flags);
1672 status = QLA_ERROR;
1673 }
1674 }
1675 } else {
1676 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301677 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001678 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1679 }
1680
1681 ha->adapter_error_count++;
1682
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301683 if (test_bit(AF_ONLINE, &ha->flags))
1684 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001685
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301686 scsi_unblock_requests(ha->host);
1687
1688 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
1689 DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001690 status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301691
David Somayajuluafaf5a22006-09-19 10:28:00 -07001692 return status;
1693}
1694
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07001695static void qla4xxx_relogin_all_devices(struct scsi_qla_host *ha)
1696{
1697 struct ddb_entry *ddb_entry, *dtemp;
1698
1699 list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
1700 if ((atomic_read(&ddb_entry->state) == DDB_STATE_MISSING) ||
1701 (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD)) {
1702 if (ddb_entry->fw_ddb_device_state ==
1703 DDB_DS_SESSION_ACTIVE) {
1704 atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
1705 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
1706 " marked ONLINE\n", ha->host_no, __func__,
1707 ddb_entry->fw_ddb_index);
1708
1709 iscsi_unblock_session(ddb_entry->sess);
1710 } else
1711 qla4xxx_relogin_device(ha, ddb_entry);
1712 }
1713 }
1714}
1715
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301716void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
1717{
Lalit Chandivade1b468072011-05-17 23:17:09 -07001718 if (ha->dpc_thread)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301719 queue_work(ha->dpc_thread, &ha->dpc_work);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301720}
1721
David Somayajuluafaf5a22006-09-19 10:28:00 -07001722/**
1723 * qla4xxx_do_dpc - dpc routine
1724 * @data: in our case pointer to adapter structure
1725 *
1726 * This routine is a task that is schedule by the interrupt handler
1727 * to perform the background processing for interrupts. We put it
1728 * on a task queue that is consumed whenever the scheduler runs; that's
1729 * so you can do anything (i.e. put the process to sleep etc). In fact,
1730 * the mid-level tries to sleep when it reaches the driver threshold
1731 * "host->can_queue". This can cause a panic if we were in our interrupt code.
1732 **/
David Howellsc4028952006-11-22 14:57:56 +00001733static void qla4xxx_do_dpc(struct work_struct *work)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001734{
David Howellsc4028952006-11-22 14:57:56 +00001735 struct scsi_qla_host *ha =
1736 container_of(work, struct scsi_qla_host, dpc_work);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001737 struct ddb_entry *ddb_entry, *dtemp;
David C Somayajulu477ffb92007-01-22 12:26:11 -08001738 int status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001739
David C Somayajuluf26b9042006-11-15 16:41:09 -08001740 DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301741 "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
1742 ha->host_no, __func__, ha->flags, ha->dpc_flags))
David Somayajuluafaf5a22006-09-19 10:28:00 -07001743
1744 /* Initialization not yet finished. Don't do anything yet. */
1745 if (!test_bit(AF_INIT_DONE, &ha->flags))
Lalit Chandivade1b468072011-05-17 23:17:09 -07001746 return;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001747
Lalit Chandivade2232be02010-07-30 14:38:47 +05301748 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1749 DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
1750 ha->host_no, __func__, ha->flags));
Lalit Chandivade1b468072011-05-17 23:17:09 -07001751 return;
Lalit Chandivade2232be02010-07-30 14:38:47 +05301752 }
1753
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301754 if (is_qla8022(ha)) {
1755 if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
1756 qla4_8xxx_idc_lock(ha);
1757 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
1758 QLA82XX_DEV_FAILED);
1759 qla4_8xxx_idc_unlock(ha);
1760 ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
1761 qla4_8xxx_device_state_handler(ha);
1762 }
1763 if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
1764 qla4_8xxx_need_qsnt_handler(ha);
1765 }
1766 }
1767
1768 if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
1769 (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07001770 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301771 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
1772 if (ql4xdontresethba) {
1773 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
1774 ha->host_no, __func__));
1775 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
1776 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1777 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1778 goto dpc_post_reset_ha;
1779 }
1780 if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
1781 test_bit(DPC_RESET_HA, &ha->dpc_flags))
1782 qla4xxx_recover_adapter(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001783
David C Somayajulu477ffb92007-01-22 12:26:11 -08001784 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001785 uint8_t wait_time = RESET_INTR_TOV;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001786
David Somayajuluafaf5a22006-09-19 10:28:00 -07001787 while ((readw(&ha->reg->ctrl_status) &
1788 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
1789 if (--wait_time == 0)
1790 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001791 msleep(1000);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001792 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001793 if (wait_time == 0)
1794 DEBUG2(printk("scsi%ld: %s: SR|FSR "
1795 "bit not cleared-- resetting\n",
1796 ha->host_no, __func__));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301797 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
David C Somayajulu477ffb92007-01-22 12:26:11 -08001798 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
1799 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301800 status = qla4xxx_recover_adapter(ha);
David C Somayajulu477ffb92007-01-22 12:26:11 -08001801 }
1802 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1803 if (status == QLA_SUCCESS)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301804 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001805 }
1806 }
1807
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301808dpc_post_reset_ha:
David Somayajuluafaf5a22006-09-19 10:28:00 -07001809 /* ---- process AEN? --- */
1810 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
1811 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
1812
1813 /* ---- Get DHCP IP Address? --- */
1814 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
1815 qla4xxx_get_dhcp_ip_address(ha);
1816
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301817 /* ---- link change? --- */
1818 if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
1819 if (!test_bit(AF_LINK_UP, &ha->flags)) {
1820 /* ---- link down? --- */
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07001821 qla4xxx_mark_all_devices_missing(ha);
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301822 } else {
1823 /* ---- link up? --- *
1824 * F/W will auto login to all devices ONLY ONCE after
1825 * link up during driver initialization and runtime
1826 * fatal error recovery. Therefore, the driver must
1827 * manually relogin to devices when recovering from
1828 * connection failures, logouts, expired KATO, etc. */
1829
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07001830 qla4xxx_relogin_all_devices(ha);
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301831 }
1832 }
1833
David Somayajuluafaf5a22006-09-19 10:28:00 -07001834 /* ---- relogin device? --- */
1835 if (adapter_up(ha) &&
1836 test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
1837 list_for_each_entry_safe(ddb_entry, dtemp,
1838 &ha->ddb_list, list) {
1839 if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
1840 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
1841 qla4xxx_relogin_device(ha, ddb_entry);
1842
1843 /*
1844 * If mbx cmd times out there is no point
1845 * in continuing further.
1846 * With large no of targets this can hang
1847 * the system.
1848 */
1849 if (test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
1850 printk(KERN_WARNING "scsi%ld: %s: "
1851 "need to reset hba\n",
1852 ha->host_no, __func__);
1853 break;
1854 }
1855 }
1856 }
Vikas Chaudhary0753b482010-07-30 14:27:19 +05301857
David Somayajuluafaf5a22006-09-19 10:28:00 -07001858}
1859
1860/**
1861 * qla4xxx_free_adapter - release the adapter
1862 * @ha: pointer to adapter structure
1863 **/
1864static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
1865{
1866
1867 if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
1868 /* Turn-off interrupts on the card. */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301869 ha->isp_ops->disable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001870 }
1871
David Somayajuluafaf5a22006-09-19 10:28:00 -07001872 /* Remove timer thread, if present */
1873 if (ha->timer_active)
1874 qla4xxx_stop_timer(ha);
1875
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301876 /* Kill the kernel thread for this host */
1877 if (ha->dpc_thread)
1878 destroy_workqueue(ha->dpc_thread);
1879
1880 /* Put firmware in known state */
1881 ha->isp_ops->reset_firmware(ha);
1882
1883 if (is_qla8022(ha)) {
1884 qla4_8xxx_idc_lock(ha);
1885 qla4_8xxx_clear_drv_active(ha);
1886 qla4_8xxx_idc_unlock(ha);
1887 }
1888
David Somayajuluafaf5a22006-09-19 10:28:00 -07001889 /* Detach interrupts */
1890 if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301891 qla4xxx_free_irqs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001892
David C Somayajulubee4fe82007-05-23 18:03:32 -07001893 /* free extra memory */
1894 qla4xxx_mem_free(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301895}
David C Somayajulubee4fe82007-05-23 18:03:32 -07001896
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301897int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
1898{
1899 int status = 0;
1900 uint8_t revision_id;
1901 unsigned long mem_base, mem_len, db_base, db_len;
1902 struct pci_dev *pdev = ha->pdev;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001903
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301904 status = pci_request_regions(pdev, DRIVER_NAME);
1905 if (status) {
1906 printk(KERN_WARNING
1907 "scsi(%ld) Failed to reserve PIO regions (%s) "
1908 "status=%d\n", ha->host_no, pci_name(pdev), status);
1909 goto iospace_error_exit;
1910 }
1911
1912 pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
1913 DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
1914 __func__, revision_id));
1915 ha->revision_id = revision_id;
1916
1917 /* remap phys address */
1918 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
1919 mem_len = pci_resource_len(pdev, 0);
1920 DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
1921 __func__, mem_base, mem_len));
1922
1923 /* mapping of pcibase pointer */
1924 ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
1925 if (!ha->nx_pcibase) {
1926 printk(KERN_ERR
1927 "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
1928 pci_release_regions(ha->pdev);
1929 goto iospace_error_exit;
1930 }
1931
1932 /* Mapping of IO base pointer, door bell read and write pointer */
1933
1934 /* mapping of IO base pointer */
1935 ha->qla4_8xxx_reg =
1936 (struct device_reg_82xx __iomem *)((uint8_t *)ha->nx_pcibase +
1937 0xbc000 + (ha->pdev->devfn << 11));
1938
1939 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
1940 db_len = pci_resource_len(pdev, 4);
1941
Shyam Sundar2657c802010-10-06 22:50:29 -07001942 ha->nx_db_wr_ptr = (ha->pdev->devfn == 4 ? QLA82XX_CAM_RAM_DB1 :
1943 QLA82XX_CAM_RAM_DB2);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301944
Shyam Sundar2657c802010-10-06 22:50:29 -07001945 return 0;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301946iospace_error_exit:
1947 return -ENOMEM;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001948}
1949
1950/***
1951 * qla4xxx_iospace_config - maps registers
1952 * @ha: pointer to adapter structure
1953 *
1954 * This routines maps HBA's registers from the pci address space
1955 * into the kernel virtual address space for memory mapped i/o.
1956 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301957int qla4xxx_iospace_config(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001958{
1959 unsigned long pio, pio_len, pio_flags;
1960 unsigned long mmio, mmio_len, mmio_flags;
1961
1962 pio = pci_resource_start(ha->pdev, 0);
1963 pio_len = pci_resource_len(ha->pdev, 0);
1964 pio_flags = pci_resource_flags(ha->pdev, 0);
1965 if (pio_flags & IORESOURCE_IO) {
1966 if (pio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301967 ql4_printk(KERN_WARNING, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07001968 "Invalid PCI I/O region size\n");
1969 pio = 0;
1970 }
1971 } else {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301972 ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001973 pio = 0;
1974 }
1975
1976 /* Use MMIO operations for all accesses. */
1977 mmio = pci_resource_start(ha->pdev, 1);
1978 mmio_len = pci_resource_len(ha->pdev, 1);
1979 mmio_flags = pci_resource_flags(ha->pdev, 1);
1980
1981 if (!(mmio_flags & IORESOURCE_MEM)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301982 ql4_printk(KERN_ERR, ha,
1983 "region #0 not an MMIO resource, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001984
1985 goto iospace_error_exit;
1986 }
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301987
David Somayajuluafaf5a22006-09-19 10:28:00 -07001988 if (mmio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301989 ql4_printk(KERN_ERR, ha,
1990 "Invalid PCI mem region size, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001991 goto iospace_error_exit;
1992 }
1993
1994 if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301995 ql4_printk(KERN_WARNING, ha,
1996 "Failed to reserve PIO/MMIO regions\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001997
1998 goto iospace_error_exit;
1999 }
2000
2001 ha->pio_address = pio;
2002 ha->pio_length = pio_len;
2003 ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
2004 if (!ha->reg) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302005 ql4_printk(KERN_ERR, ha,
2006 "cannot remap MMIO, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002007
2008 goto iospace_error_exit;
2009 }
2010
2011 return 0;
2012
2013iospace_error_exit:
2014 return -ENOMEM;
2015}
2016
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302017static struct isp_operations qla4xxx_isp_ops = {
2018 .iospace_config = qla4xxx_iospace_config,
2019 .pci_config = qla4xxx_pci_config,
2020 .disable_intrs = qla4xxx_disable_intrs,
2021 .enable_intrs = qla4xxx_enable_intrs,
2022 .start_firmware = qla4xxx_start_firmware,
2023 .intr_handler = qla4xxx_intr_handler,
2024 .interrupt_service_routine = qla4xxx_interrupt_service_routine,
2025 .reset_chip = qla4xxx_soft_reset,
2026 .reset_firmware = qla4xxx_hw_reset,
2027 .queue_iocb = qla4xxx_queue_iocb,
2028 .complete_iocb = qla4xxx_complete_iocb,
2029 .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
2030 .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
2031 .get_sys_info = qla4xxx_get_sys_info,
2032};
2033
2034static struct isp_operations qla4_8xxx_isp_ops = {
2035 .iospace_config = qla4_8xxx_iospace_config,
2036 .pci_config = qla4_8xxx_pci_config,
2037 .disable_intrs = qla4_8xxx_disable_intrs,
2038 .enable_intrs = qla4_8xxx_enable_intrs,
2039 .start_firmware = qla4_8xxx_load_risc,
2040 .intr_handler = qla4_8xxx_intr_handler,
2041 .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
2042 .reset_chip = qla4_8xxx_isp_reset,
2043 .reset_firmware = qla4_8xxx_stop_firmware,
2044 .queue_iocb = qla4_8xxx_queue_iocb,
2045 .complete_iocb = qla4_8xxx_complete_iocb,
2046 .rd_shdw_req_q_out = qla4_8xxx_rd_shdw_req_q_out,
2047 .rd_shdw_rsp_q_in = qla4_8xxx_rd_shdw_rsp_q_in,
2048 .get_sys_info = qla4_8xxx_get_sys_info,
2049};
2050
2051uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
2052{
2053 return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
2054}
2055
2056uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
2057{
2058 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
2059}
2060
2061uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
2062{
2063 return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
2064}
2065
2066uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
2067{
2068 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
2069}
2070
David Somayajuluafaf5a22006-09-19 10:28:00 -07002071/**
2072 * qla4xxx_probe_adapter - callback function to probe HBA
2073 * @pdev: pointer to pci_dev structure
2074 * @pci_device_id: pointer to pci_device entry
2075 *
2076 * This routine will probe for Qlogic 4xxx iSCSI host adapters.
2077 * It returns zero if successful. It also initializes all data necessary for
2078 * the driver.
2079 **/
2080static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
2081 const struct pci_device_id *ent)
2082{
2083 int ret = -ENODEV, status;
2084 struct Scsi_Host *host;
2085 struct scsi_qla_host *ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002086 uint8_t init_retry_count = 0;
2087 char buf[34];
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302088 struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
Prasanna Mumbaif9880e72011-03-21 03:34:26 -07002089 uint32_t dev_state;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002090
2091 if (pci_enable_device(pdev))
2092 return -1;
2093
2094 host = scsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha));
2095 if (host == NULL) {
2096 printk(KERN_WARNING
2097 "qla4xxx: Couldn't allocate host from scsi layer!\n");
2098 goto probe_disable_device;
2099 }
2100
2101 /* Clear our data area */
2102 ha = (struct scsi_qla_host *) host->hostdata;
2103 memset(ha, 0, sizeof(*ha));
2104
2105 /* Save the information from PCI BIOS. */
2106 ha->pdev = pdev;
2107 ha->host = host;
2108 ha->host_no = host->host_no;
2109
Lalit Chandivade2232be02010-07-30 14:38:47 +05302110 pci_enable_pcie_error_reporting(pdev);
2111
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302112 /* Setup Runtime configurable options */
2113 if (is_qla8022(ha)) {
2114 ha->isp_ops = &qla4_8xxx_isp_ops;
2115 rwlock_init(&ha->hw_lock);
2116 ha->qdr_sn_window = -1;
2117 ha->ddr_mn_window = -1;
2118 ha->curr_window = 255;
2119 ha->func_num = PCI_FUNC(ha->pdev->devfn);
2120 nx_legacy_intr = &legacy_intr[ha->func_num];
2121 ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
2122 ha->nx_legacy_intr.tgt_status_reg =
2123 nx_legacy_intr->tgt_status_reg;
2124 ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
2125 ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
2126 } else {
2127 ha->isp_ops = &qla4xxx_isp_ops;
2128 }
2129
Lalit Chandivade2232be02010-07-30 14:38:47 +05302130 /* Set EEH reset type to fundamental if required by hba */
2131 if (is_qla8022(ha))
2132 pdev->needs_freset = 1;
2133
David Somayajuluafaf5a22006-09-19 10:28:00 -07002134 /* Configure PCI I/O space. */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302135 ret = ha->isp_ops->iospace_config(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002136 if (ret)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302137 goto probe_failed_ioconfig;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002138
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302139 ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
David Somayajuluafaf5a22006-09-19 10:28:00 -07002140 pdev->device, pdev->irq, ha->reg);
2141
2142 qla4xxx_config_dma_addressing(ha);
2143
2144 /* Initialize lists and spinlocks. */
2145 INIT_LIST_HEAD(&ha->ddb_list);
2146 INIT_LIST_HEAD(&ha->free_srb_q);
2147
2148 mutex_init(&ha->mbox_sem);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302149 init_completion(&ha->mbx_intr_comp);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002150
2151 spin_lock_init(&ha->hardware_lock);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002152
2153 /* Allocate dma buffers */
2154 if (qla4xxx_mem_alloc(ha)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302155 ql4_printk(KERN_WARNING, ha,
2156 "[ERROR] Failed to allocate memory for adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002157
2158 ret = -ENOMEM;
2159 goto probe_failed;
2160 }
2161
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302162 if (is_qla8022(ha))
2163 (void) qla4_8xxx_get_flash_info(ha);
2164
David Somayajuluafaf5a22006-09-19 10:28:00 -07002165 /*
2166 * Initialize the Host adapter request/response queues and
2167 * firmware
2168 * NOTE: interrupts enabled upon successful completion
2169 */
2170 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302171 while ((!test_bit(AF_ONLINE, &ha->flags)) &&
2172 init_retry_count++ < MAX_INIT_RETRIES) {
Prasanna Mumbaif9880e72011-03-21 03:34:26 -07002173
2174 if (is_qla8022(ha)) {
2175 qla4_8xxx_idc_lock(ha);
2176 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
2177 qla4_8xxx_idc_unlock(ha);
2178 if (dev_state == QLA82XX_DEV_FAILED) {
2179 ql4_printk(KERN_WARNING, ha, "%s: don't retry "
2180 "initialize adapter. H/W is in failed state\n",
2181 __func__);
2182 break;
2183 }
2184 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002185 DEBUG2(printk("scsi: %s: retrying adapter initialization "
2186 "(%d)\n", __func__, init_retry_count));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302187
2188 if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
2189 continue;
2190
David Somayajuluafaf5a22006-09-19 10:28:00 -07002191 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
2192 }
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302193
2194 if (!test_bit(AF_ONLINE, &ha->flags)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302195 ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002196
Lalit Chandivadefe998522010-12-02 22:12:36 -08002197 if (is_qla8022(ha) && ql4xdontresethba) {
2198 /* Put the device in failed state. */
2199 DEBUG2(printk(KERN_ERR "HW STATE: FAILED\n"));
2200 qla4_8xxx_idc_lock(ha);
2201 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2202 QLA82XX_DEV_FAILED);
2203 qla4_8xxx_idc_unlock(ha);
2204 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002205 ret = -ENODEV;
2206 goto probe_failed;
2207 }
2208
2209 host->cmd_per_lun = 3;
2210 host->max_channel = 0;
2211 host->max_lun = MAX_LUNS - 1;
2212 host->max_id = MAX_TARGETS;
2213 host->max_cmd_len = IOCB_MAX_CDB_LEN;
2214 host->can_queue = MAX_SRBS ;
2215 host->transportt = qla4xxx_scsi_transport;
2216
2217 ret = scsi_init_shared_tag_map(host, MAX_SRBS);
2218 if (ret) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302219 ql4_printk(KERN_WARNING, ha,
2220 "scsi_init_shared_tag_map failed\n");
2221 goto probe_failed;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002222 }
2223
2224 /* Startup the kernel thread for this host adapter. */
2225 DEBUG2(printk("scsi: %s: Starting kernel thread for "
2226 "qla4xxx_dpc\n", __func__));
2227 sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
2228 ha->dpc_thread = create_singlethread_workqueue(buf);
2229 if (!ha->dpc_thread) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302230 ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002231 ret = -ENODEV;
2232 goto probe_failed;
2233 }
David Howellsc4028952006-11-22 14:57:56 +00002234 INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002235
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302236 /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
2237 * (which is called indirectly by qla4xxx_initialize_adapter),
2238 * so that irqs will be registered after crbinit but before
2239 * mbx_intr_enable.
2240 */
2241 if (!is_qla8022(ha)) {
2242 ret = qla4xxx_request_irqs(ha);
2243 if (ret) {
2244 ql4_printk(KERN_WARNING, ha, "Failed to reserve "
2245 "interrupt %d already in use.\n", pdev->irq);
2246 goto probe_failed;
2247 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002248 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002249
Lalit Chandivade2232be02010-07-30 14:38:47 +05302250 pci_save_state(ha->pdev);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302251 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002252
2253 /* Start timer thread. */
2254 qla4xxx_start_timer(ha, qla4xxx_timer, 1);
2255
2256 set_bit(AF_INIT_DONE, &ha->flags);
2257
2258 pci_set_drvdata(pdev, ha);
2259
2260 ret = scsi_add_host(host, &pdev->dev);
2261 if (ret)
2262 goto probe_failed;
2263
David Somayajuluafaf5a22006-09-19 10:28:00 -07002264 printk(KERN_INFO
2265 " QLogic iSCSI HBA Driver version: %s\n"
2266 " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
2267 qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
2268 ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
2269 ha->patch_number, ha->build_number);
Mike Christie921601b2008-01-31 13:36:49 -06002270 scsi_scan_host(host);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05002271
2272 qla4xxx_create_ifaces(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002273 return 0;
2274
David Somayajuluafaf5a22006-09-19 10:28:00 -07002275probe_failed:
2276 qla4xxx_free_adapter(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302277
2278probe_failed_ioconfig:
Lalit Chandivade2232be02010-07-30 14:38:47 +05302279 pci_disable_pcie_error_reporting(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002280 scsi_host_put(ha->host);
2281
2282probe_disable_device:
2283 pci_disable_device(pdev);
2284
2285 return ret;
2286}
2287
2288/**
Karen Higgins7eece5a2011-03-21 03:34:29 -07002289 * qla4xxx_prevent_other_port_reinit - prevent other port from re-initialize
2290 * @ha: pointer to adapter structure
2291 *
2292 * Mark the other ISP-4xxx port to indicate that the driver is being removed,
2293 * so that the other port will not re-initialize while in the process of
2294 * removing the ha due to driver unload or hba hotplug.
2295 **/
2296static void qla4xxx_prevent_other_port_reinit(struct scsi_qla_host *ha)
2297{
2298 struct scsi_qla_host *other_ha = NULL;
2299 struct pci_dev *other_pdev = NULL;
2300 int fn = ISP4XXX_PCI_FN_2;
2301
2302 /*iscsi function numbers for ISP4xxx is 1 and 3*/
2303 if (PCI_FUNC(ha->pdev->devfn) & BIT_1)
2304 fn = ISP4XXX_PCI_FN_1;
2305
2306 other_pdev =
2307 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
2308 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
2309 fn));
2310
2311 /* Get other_ha if other_pdev is valid and state is enable*/
2312 if (other_pdev) {
2313 if (atomic_read(&other_pdev->enable_cnt)) {
2314 other_ha = pci_get_drvdata(other_pdev);
2315 if (other_ha) {
2316 set_bit(AF_HA_REMOVAL, &other_ha->flags);
2317 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: "
2318 "Prevent %s reinit\n", __func__,
2319 dev_name(&other_ha->pdev->dev)));
2320 }
2321 }
2322 pci_dev_put(other_pdev);
2323 }
2324}
2325
2326/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002327 * qla4xxx_remove_adapter - calback function to remove adapter.
2328 * @pci_dev: PCI device pointer
2329 **/
2330static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
2331{
2332 struct scsi_qla_host *ha;
2333
2334 ha = pci_get_drvdata(pdev);
2335
Karen Higgins7eece5a2011-03-21 03:34:29 -07002336 if (!is_qla8022(ha))
2337 qla4xxx_prevent_other_port_reinit(ha);
David C Somayajulubee4fe82007-05-23 18:03:32 -07002338
David Somayajuluafaf5a22006-09-19 10:28:00 -07002339 /* remove devs from iscsi_sessions to scsi_devices */
2340 qla4xxx_free_ddb_list(ha);
2341
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05002342 /* destroy iface from sysfs */
2343 qla4xxx_destroy_ifaces(ha);
2344
David Somayajuluafaf5a22006-09-19 10:28:00 -07002345 scsi_remove_host(ha->host);
2346
2347 qla4xxx_free_adapter(ha);
2348
2349 scsi_host_put(ha->host);
2350
Lalit Chandivade2232be02010-07-30 14:38:47 +05302351 pci_disable_pcie_error_reporting(pdev);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302352 pci_disable_device(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002353 pci_set_drvdata(pdev, NULL);
2354}
2355
2356/**
2357 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
2358 * @ha: HA context
2359 *
2360 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
2361 * supported addressing method.
2362 */
Adrian Bunk47975472007-04-26 00:35:16 -07002363static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002364{
2365 int retval;
2366
2367 /* Update our PCI device dma_mask for full 64 bit mask */
Yang Hongyang6a355282009-04-06 19:01:13 -07002368 if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
2369 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002370 dev_dbg(&ha->pdev->dev,
2371 "Failed to set 64 bit PCI consistent mask; "
2372 "using 32 bit.\n");
2373 retval = pci_set_consistent_dma_mask(ha->pdev,
Yang Hongyang284901a2009-04-06 19:01:15 -07002374 DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07002375 }
2376 } else
Yang Hongyang284901a2009-04-06 19:01:15 -07002377 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07002378}
2379
2380static int qla4xxx_slave_alloc(struct scsi_device *sdev)
2381{
2382 struct iscsi_cls_session *sess = starget_to_session(sdev->sdev_target);
2383 struct ddb_entry *ddb = sess->dd_data;
Vikas Chaudhary8bb40332011-03-21 03:34:31 -07002384 int queue_depth = QL4_DEF_QDEPTH;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002385
2386 sdev->hostdata = ddb;
2387 sdev->tagged_supported = 1;
Vikas Chaudhary8bb40332011-03-21 03:34:31 -07002388
2389 if (ql4xmaxqdepth != 0 && ql4xmaxqdepth <= 0xffffU)
2390 queue_depth = ql4xmaxqdepth;
2391
2392 scsi_activate_tcq(sdev, queue_depth);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002393 return 0;
2394}
2395
2396static int qla4xxx_slave_configure(struct scsi_device *sdev)
2397{
2398 sdev->tagged_supported = 1;
2399 return 0;
2400}
2401
2402static void qla4xxx_slave_destroy(struct scsi_device *sdev)
2403{
2404 scsi_deactivate_tcq(sdev, 1);
2405}
2406
2407/**
2408 * qla4xxx_del_from_active_array - returns an active srb
2409 * @ha: Pointer to host adapter structure.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02002410 * @index: index into the active_array
David Somayajuluafaf5a22006-09-19 10:28:00 -07002411 *
2412 * This routine removes and returns the srb at the specified index
2413 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302414struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
2415 uint32_t index)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002416{
2417 struct srb *srb = NULL;
Vikas Chaudhary53698872010-04-28 11:41:59 +05302418 struct scsi_cmnd *cmd = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002419
Vikas Chaudhary53698872010-04-28 11:41:59 +05302420 cmd = scsi_host_find_tag(ha->host, index);
2421 if (!cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002422 return srb;
2423
Vikas Chaudhary53698872010-04-28 11:41:59 +05302424 srb = (struct srb *)CMD_SP(cmd);
2425 if (!srb)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002426 return srb;
2427
2428 /* update counters */
2429 if (srb->flags & SRB_DMA_VALID) {
2430 ha->req_q_count += srb->iocb_cnt;
2431 ha->iocb_cnt -= srb->iocb_cnt;
2432 if (srb->cmd)
Vikas Chaudhary53698872010-04-28 11:41:59 +05302433 srb->cmd->host_scribble =
2434 (unsigned char *)(unsigned long) MAX_SRBS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002435 }
2436 return srb;
2437}
2438
2439/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002440 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302441 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07002442 * @cmd: Scsi Command to wait on.
2443 *
2444 * This routine waits for the command to be returned by the Firmware
2445 * for some max time.
2446 **/
2447static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
2448 struct scsi_cmnd *cmd)
2449{
2450 int done = 0;
2451 struct srb *rp;
2452 uint32_t max_wait_time = EH_WAIT_CMD_TOV;
Lalit Chandivade2232be02010-07-30 14:38:47 +05302453 int ret = SUCCESS;
2454
2455 /* Dont wait on command if PCI error is being handled
2456 * by PCI AER driver
2457 */
2458 if (unlikely(pci_channel_offline(ha->pdev)) ||
2459 (test_bit(AF_EEH_BUSY, &ha->flags))) {
2460 ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
2461 ha->host_no, __func__);
2462 return ret;
2463 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002464
2465 do {
2466 /* Checking to see if its returned to OS */
Vikas Chaudhary53698872010-04-28 11:41:59 +05302467 rp = (struct srb *) CMD_SP(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002468 if (rp == NULL) {
2469 done++;
2470 break;
2471 }
2472
2473 msleep(2000);
2474 } while (max_wait_time--);
2475
2476 return done;
2477}
2478
2479/**
2480 * qla4xxx_wait_for_hba_online - waits for HBA to come online
2481 * @ha: Pointer to host adapter structure
2482 **/
2483static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
2484{
2485 unsigned long wait_online;
2486
Vikas Chaudharyf581a3f2010-10-06 22:47:48 -07002487 wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002488 while (time_before(jiffies, wait_online)) {
2489
2490 if (adapter_up(ha))
2491 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002492
2493 msleep(2000);
2494 }
2495
2496 return QLA_ERROR;
2497}
2498
2499/**
Mike Christiece545032008-02-29 18:25:20 -06002500 * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02002501 * @ha: pointer to HBA
David Somayajuluafaf5a22006-09-19 10:28:00 -07002502 * @t: target id
2503 * @l: lun id
2504 *
2505 * This function waits for all outstanding commands to a lun to complete. It
2506 * returns 0 if all pending commands are returned and 1 otherwise.
2507 **/
Mike Christiece545032008-02-29 18:25:20 -06002508static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
2509 struct scsi_target *stgt,
2510 struct scsi_device *sdev)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002511{
2512 int cnt;
2513 int status = 0;
2514 struct scsi_cmnd *cmd;
2515
2516 /*
Mike Christiece545032008-02-29 18:25:20 -06002517 * Waiting for all commands for the designated target or dev
2518 * in the active array
David Somayajuluafaf5a22006-09-19 10:28:00 -07002519 */
2520 for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
2521 cmd = scsi_host_find_tag(ha->host, cnt);
Mike Christiece545032008-02-29 18:25:20 -06002522 if (cmd && stgt == scsi_target(cmd->device) &&
2523 (!sdev || sdev == cmd->device)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002524 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
2525 status++;
2526 break;
2527 }
2528 }
2529 }
2530 return status;
2531}
2532
2533/**
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302534 * qla4xxx_eh_abort - callback for abort task.
2535 * @cmd: Pointer to Linux's SCSI command structure
2536 *
2537 * This routine is called by the Linux OS to abort the specified
2538 * command.
2539 **/
2540static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
2541{
2542 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2543 unsigned int id = cmd->device->id;
2544 unsigned int lun = cmd->device->lun;
Mike Christie92b3e5b2010-10-06 22:51:17 -07002545 unsigned long flags;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302546 struct srb *srb = NULL;
2547 int ret = SUCCESS;
2548 int wait = 0;
2549
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302550 ql4_printk(KERN_INFO, ha,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002551 "scsi%ld:%d:%d: Abort command issued cmd=%p\n",
2552 ha->host_no, id, lun, cmd);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302553
Mike Christie92b3e5b2010-10-06 22:51:17 -07002554 spin_lock_irqsave(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302555 srb = (struct srb *) CMD_SP(cmd);
Mike Christie92b3e5b2010-10-06 22:51:17 -07002556 if (!srb) {
2557 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302558 return SUCCESS;
Mike Christie92b3e5b2010-10-06 22:51:17 -07002559 }
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302560 kref_get(&srb->srb_ref);
Mike Christie92b3e5b2010-10-06 22:51:17 -07002561 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302562
2563 if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
2564 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
2565 ha->host_no, id, lun));
2566 ret = FAILED;
2567 } else {
2568 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
2569 ha->host_no, id, lun));
2570 wait = 1;
2571 }
2572
2573 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
2574
2575 /* Wait for command to complete */
2576 if (wait) {
2577 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
2578 DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
2579 ha->host_no, id, lun));
2580 ret = FAILED;
2581 }
2582 }
2583
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302584 ql4_printk(KERN_INFO, ha,
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302585 "scsi%ld:%d:%d: Abort command - %s\n",
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002586 ha->host_no, id, lun, (ret == SUCCESS) ? "succeeded" : "failed");
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302587
2588 return ret;
2589}
2590
2591/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002592 * qla4xxx_eh_device_reset - callback for target reset.
2593 * @cmd: Pointer to Linux's SCSI command structure
2594 *
2595 * This routine is called by the Linux OS to reset all luns on the
2596 * specified target.
2597 **/
2598static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
2599{
2600 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2601 struct ddb_entry *ddb_entry = cmd->device->hostdata;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002602 int ret = FAILED, stat;
2603
Karen Higgins612f7342009-07-15 15:03:01 -05002604 if (!ddb_entry)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002605 return ret;
2606
Mike Christiec01be6d2010-07-22 16:59:49 +05302607 ret = iscsi_block_scsi_eh(cmd);
2608 if (ret)
2609 return ret;
2610 ret = FAILED;
2611
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302612 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002613 "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
2614 cmd->device->channel, cmd->device->id, cmd->device->lun);
2615
2616 DEBUG2(printk(KERN_INFO
2617 "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
2618 "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
Jens Axboe242f9dc2008-09-14 05:55:09 -07002619 cmd, jiffies, cmd->request->timeout / HZ,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002620 ha->dpc_flags, cmd->result, cmd->allowed));
2621
2622 /* FIXME: wait for hba to go online */
2623 stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
2624 if (stat != QLA_SUCCESS) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302625 ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002626 goto eh_dev_reset_done;
2627 }
2628
Mike Christiece545032008-02-29 18:25:20 -06002629 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2630 cmd->device)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302631 ql4_printk(KERN_INFO, ha,
Mike Christiece545032008-02-29 18:25:20 -06002632 "DEVICE RESET FAILED - waiting for "
2633 "commands.\n");
2634 goto eh_dev_reset_done;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002635 }
2636
David C Somayajulu9d562912008-03-19 11:23:03 -07002637 /* Send marker. */
2638 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2639 MM_LUN_RESET) != QLA_SUCCESS)
2640 goto eh_dev_reset_done;
2641
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302642 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002643 "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
2644 ha->host_no, cmd->device->channel, cmd->device->id,
2645 cmd->device->lun);
2646
2647 ret = SUCCESS;
2648
2649eh_dev_reset_done:
2650
2651 return ret;
2652}
2653
2654/**
Mike Christiece545032008-02-29 18:25:20 -06002655 * qla4xxx_eh_target_reset - callback for target reset.
2656 * @cmd: Pointer to Linux's SCSI command structure
2657 *
2658 * This routine is called by the Linux OS to reset the target.
2659 **/
2660static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
2661{
2662 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2663 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christiec01be6d2010-07-22 16:59:49 +05302664 int stat, ret;
Mike Christiece545032008-02-29 18:25:20 -06002665
2666 if (!ddb_entry)
2667 return FAILED;
2668
Mike Christiec01be6d2010-07-22 16:59:49 +05302669 ret = iscsi_block_scsi_eh(cmd);
2670 if (ret)
2671 return ret;
2672
Mike Christiece545032008-02-29 18:25:20 -06002673 starget_printk(KERN_INFO, scsi_target(cmd->device),
2674 "WARM TARGET RESET ISSUED.\n");
2675
2676 DEBUG2(printk(KERN_INFO
2677 "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
2678 "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
Jens Axboe242f9dc2008-09-14 05:55:09 -07002679 ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
Mike Christiece545032008-02-29 18:25:20 -06002680 ha->dpc_flags, cmd->result, cmd->allowed));
2681
2682 stat = qla4xxx_reset_target(ha, ddb_entry);
2683 if (stat != QLA_SUCCESS) {
2684 starget_printk(KERN_INFO, scsi_target(cmd->device),
2685 "WARM TARGET RESET FAILED.\n");
2686 return FAILED;
2687 }
2688
Mike Christiece545032008-02-29 18:25:20 -06002689 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2690 NULL)) {
2691 starget_printk(KERN_INFO, scsi_target(cmd->device),
2692 "WARM TARGET DEVICE RESET FAILED - "
2693 "waiting for commands.\n");
2694 return FAILED;
2695 }
2696
David C Somayajulu9d562912008-03-19 11:23:03 -07002697 /* Send marker. */
2698 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2699 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
2700 starget_printk(KERN_INFO, scsi_target(cmd->device),
2701 "WARM TARGET DEVICE RESET FAILED - "
2702 "marker iocb failed.\n");
2703 return FAILED;
2704 }
2705
Mike Christiece545032008-02-29 18:25:20 -06002706 starget_printk(KERN_INFO, scsi_target(cmd->device),
2707 "WARM TARGET RESET SUCCEEDED.\n");
2708 return SUCCESS;
2709}
2710
2711/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002712 * qla4xxx_eh_host_reset - kernel callback
2713 * @cmd: Pointer to Linux's SCSI command structure
2714 *
2715 * This routine is invoked by the Linux kernel to perform fatal error
2716 * recovery on the specified adapter.
2717 **/
2718static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
2719{
2720 int return_status = FAILED;
2721 struct scsi_qla_host *ha;
2722
2723 ha = (struct scsi_qla_host *) cmd->device->host->hostdata;
2724
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302725 if (ql4xdontresethba) {
2726 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
2727 ha->host_no, __func__));
2728 return FAILED;
2729 }
2730
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302731 ql4_printk(KERN_INFO, ha,
Karen Higginsdca05c42009-07-15 15:03:00 -05002732 "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002733 cmd->device->channel, cmd->device->id, cmd->device->lun);
2734
2735 if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
2736 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
2737 "DEAD.\n", ha->host_no, cmd->device->channel,
2738 __func__));
2739
2740 return FAILED;
2741 }
2742
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302743 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
2744 if (is_qla8022(ha))
2745 set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2746 else
2747 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2748 }
Mike Christie50a29ae2008-03-04 13:26:53 -06002749
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302750 if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002751 return_status = SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002752
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302753 ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002754 return_status == FAILED ? "FAILED" : "SUCCEEDED");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002755
2756 return return_status;
2757}
2758
Lalit Chandivade2232be02010-07-30 14:38:47 +05302759/* PCI AER driver recovers from all correctable errors w/o
2760 * driver intervention. For uncorrectable errors PCI AER
2761 * driver calls the following device driver's callbacks
2762 *
2763 * - Fatal Errors - link_reset
2764 * - Non-Fatal Errors - driver's pci_error_detected() which
2765 * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
2766 *
2767 * PCI AER driver calls
2768 * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
2769 * returns RECOVERED or NEED_RESET if fw_hung
2770 * NEED_RESET - driver's slot_reset()
2771 * DISCONNECT - device is dead & cannot recover
2772 * RECOVERED - driver's pci_resume()
2773 */
2774static pci_ers_result_t
2775qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
2776{
2777 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2778
2779 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
2780 ha->host_no, __func__, state);
2781
2782 if (!is_aer_supported(ha))
2783 return PCI_ERS_RESULT_NONE;
2784
2785 switch (state) {
2786 case pci_channel_io_normal:
2787 clear_bit(AF_EEH_BUSY, &ha->flags);
2788 return PCI_ERS_RESULT_CAN_RECOVER;
2789 case pci_channel_io_frozen:
2790 set_bit(AF_EEH_BUSY, &ha->flags);
2791 qla4xxx_mailbox_premature_completion(ha);
2792 qla4xxx_free_irqs(ha);
2793 pci_disable_device(pdev);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002794 /* Return back all IOs */
2795 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
Lalit Chandivade2232be02010-07-30 14:38:47 +05302796 return PCI_ERS_RESULT_NEED_RESET;
2797 case pci_channel_io_perm_failure:
2798 set_bit(AF_EEH_BUSY, &ha->flags);
2799 set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
2800 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
2801 return PCI_ERS_RESULT_DISCONNECT;
2802 }
2803 return PCI_ERS_RESULT_NEED_RESET;
2804}
2805
2806/**
2807 * qla4xxx_pci_mmio_enabled() gets called if
2808 * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
2809 * and read/write to the device still works.
2810 **/
2811static pci_ers_result_t
2812qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
2813{
2814 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2815
2816 if (!is_aer_supported(ha))
2817 return PCI_ERS_RESULT_NONE;
2818
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002819 return PCI_ERS_RESULT_RECOVERED;
Lalit Chandivade2232be02010-07-30 14:38:47 +05302820}
2821
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002822static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
Lalit Chandivade2232be02010-07-30 14:38:47 +05302823{
2824 uint32_t rval = QLA_ERROR;
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002825 uint32_t ret = 0;
Lalit Chandivade2232be02010-07-30 14:38:47 +05302826 int fn;
2827 struct pci_dev *other_pdev = NULL;
2828
2829 ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
2830
2831 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2832
2833 if (test_bit(AF_ONLINE, &ha->flags)) {
2834 clear_bit(AF_ONLINE, &ha->flags);
2835 qla4xxx_mark_all_devices_missing(ha);
2836 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
Lalit Chandivade2232be02010-07-30 14:38:47 +05302837 }
2838
2839 fn = PCI_FUNC(ha->pdev->devfn);
2840 while (fn > 0) {
2841 fn--;
2842 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
2843 "func %x\n", ha->host_no, __func__, fn);
2844 /* Get the pci device given the domain, bus,
2845 * slot/function number */
2846 other_pdev =
2847 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
2848 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
2849 fn));
2850
2851 if (!other_pdev)
2852 continue;
2853
2854 if (atomic_read(&other_pdev->enable_cnt)) {
2855 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
2856 "func in enabled state%x\n", ha->host_no,
2857 __func__, fn);
2858 pci_dev_put(other_pdev);
2859 break;
2860 }
2861 pci_dev_put(other_pdev);
2862 }
2863
2864 /* The first function on the card, the reset owner will
2865 * start & initialize the firmware. The other functions
2866 * on the card will reset the firmware context
2867 */
2868 if (!fn) {
2869 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
2870 "0x%x is the owner\n", ha->host_no, __func__,
2871 ha->pdev->devfn);
2872
2873 qla4_8xxx_idc_lock(ha);
2874 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2875 QLA82XX_DEV_COLD);
2876
2877 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
2878 QLA82XX_IDC_VERSION);
2879
2880 qla4_8xxx_idc_unlock(ha);
2881 clear_bit(AF_FW_RECOVERY, &ha->flags);
2882 rval = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
2883 qla4_8xxx_idc_lock(ha);
2884
2885 if (rval != QLA_SUCCESS) {
2886 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
2887 "FAILED\n", ha->host_no, __func__);
2888 qla4_8xxx_clear_drv_active(ha);
2889 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2890 QLA82XX_DEV_FAILED);
2891 } else {
2892 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
2893 "READY\n", ha->host_no, __func__);
2894 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2895 QLA82XX_DEV_READY);
2896 /* Clear driver state register */
2897 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
2898 qla4_8xxx_set_drv_active(ha);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002899 ret = qla4xxx_request_irqs(ha);
2900 if (ret) {
2901 ql4_printk(KERN_WARNING, ha, "Failed to "
2902 "reserve interrupt %d already in use.\n",
2903 ha->pdev->irq);
2904 rval = QLA_ERROR;
2905 } else {
2906 ha->isp_ops->enable_intrs(ha);
2907 rval = QLA_SUCCESS;
2908 }
Lalit Chandivade2232be02010-07-30 14:38:47 +05302909 }
2910 qla4_8xxx_idc_unlock(ha);
2911 } else {
2912 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
2913 "the reset owner\n", ha->host_no, __func__,
2914 ha->pdev->devfn);
2915 if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
2916 QLA82XX_DEV_READY)) {
2917 clear_bit(AF_FW_RECOVERY, &ha->flags);
2918 rval = qla4xxx_initialize_adapter(ha,
2919 PRESERVE_DDB_LIST);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002920 if (rval == QLA_SUCCESS) {
2921 ret = qla4xxx_request_irqs(ha);
2922 if (ret) {
2923 ql4_printk(KERN_WARNING, ha, "Failed to"
2924 " reserve interrupt %d already in"
2925 " use.\n", ha->pdev->irq);
2926 rval = QLA_ERROR;
2927 } else {
2928 ha->isp_ops->enable_intrs(ha);
2929 rval = QLA_SUCCESS;
2930 }
2931 }
Lalit Chandivade2232be02010-07-30 14:38:47 +05302932 qla4_8xxx_idc_lock(ha);
2933 qla4_8xxx_set_drv_active(ha);
2934 qla4_8xxx_idc_unlock(ha);
2935 }
2936 }
2937 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2938 return rval;
2939}
2940
2941static pci_ers_result_t
2942qla4xxx_pci_slot_reset(struct pci_dev *pdev)
2943{
2944 pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
2945 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2946 int rc;
2947
2948 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
2949 ha->host_no, __func__);
2950
2951 if (!is_aer_supported(ha))
2952 return PCI_ERS_RESULT_NONE;
2953
2954 /* Restore the saved state of PCIe device -
2955 * BAR registers, PCI Config space, PCIX, MSI,
2956 * IOV states
2957 */
2958 pci_restore_state(pdev);
2959
2960 /* pci_restore_state() clears the saved_state flag of the device
2961 * save restored state which resets saved_state flag
2962 */
2963 pci_save_state(pdev);
2964
2965 /* Initialize device or resume if in suspended state */
2966 rc = pci_enable_device(pdev);
2967 if (rc) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002968 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Can't re-enable "
Lalit Chandivade2232be02010-07-30 14:38:47 +05302969 "device after reset\n", ha->host_no, __func__);
2970 goto exit_slot_reset;
2971 }
2972
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002973 ha->isp_ops->disable_intrs(ha);
Lalit Chandivade2232be02010-07-30 14:38:47 +05302974
2975 if (is_qla8022(ha)) {
2976 if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
2977 ret = PCI_ERS_RESULT_RECOVERED;
2978 goto exit_slot_reset;
2979 } else
2980 goto exit_slot_reset;
2981 }
2982
2983exit_slot_reset:
2984 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
2985 "device after reset\n", ha->host_no, __func__, ret);
2986 return ret;
2987}
2988
2989static void
2990qla4xxx_pci_resume(struct pci_dev *pdev)
2991{
2992 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2993 int ret;
2994
2995 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
2996 ha->host_no, __func__);
2997
2998 ret = qla4xxx_wait_for_hba_online(ha);
2999 if (ret != QLA_SUCCESS) {
3000 ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
3001 "resume I/O from slot/link_reset\n", ha->host_no,
3002 __func__);
3003 }
3004
3005 pci_cleanup_aer_uncorrect_error_status(pdev);
3006 clear_bit(AF_EEH_BUSY, &ha->flags);
3007}
3008
3009static struct pci_error_handlers qla4xxx_err_handler = {
3010 .error_detected = qla4xxx_pci_error_detected,
3011 .mmio_enabled = qla4xxx_pci_mmio_enabled,
3012 .slot_reset = qla4xxx_pci_slot_reset,
3013 .resume = qla4xxx_pci_resume,
3014};
3015
David Somayajuluafaf5a22006-09-19 10:28:00 -07003016static struct pci_device_id qla4xxx_pci_tbl[] = {
3017 {
3018 .vendor = PCI_VENDOR_ID_QLOGIC,
3019 .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
3020 .subvendor = PCI_ANY_ID,
3021 .subdevice = PCI_ANY_ID,
3022 },
3023 {
3024 .vendor = PCI_VENDOR_ID_QLOGIC,
3025 .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
3026 .subvendor = PCI_ANY_ID,
3027 .subdevice = PCI_ANY_ID,
3028 },
David C Somayajulud9150582006-11-15 17:38:40 -08003029 {
3030 .vendor = PCI_VENDOR_ID_QLOGIC,
3031 .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
3032 .subvendor = PCI_ANY_ID,
3033 .subdevice = PCI_ANY_ID,
3034 },
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303035 {
3036 .vendor = PCI_VENDOR_ID_QLOGIC,
3037 .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
3038 .subvendor = PCI_ANY_ID,
3039 .subdevice = PCI_ANY_ID,
3040 },
David Somayajuluafaf5a22006-09-19 10:28:00 -07003041 {0, 0},
3042};
3043MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
3044
Adrian Bunk47975472007-04-26 00:35:16 -07003045static struct pci_driver qla4xxx_pci_driver = {
David Somayajuluafaf5a22006-09-19 10:28:00 -07003046 .name = DRIVER_NAME,
3047 .id_table = qla4xxx_pci_tbl,
3048 .probe = qla4xxx_probe_adapter,
3049 .remove = qla4xxx_remove_adapter,
Lalit Chandivade2232be02010-07-30 14:38:47 +05303050 .err_handler = &qla4xxx_err_handler,
David Somayajuluafaf5a22006-09-19 10:28:00 -07003051};
3052
3053static int __init qla4xxx_module_init(void)
3054{
3055 int ret;
3056
3057 /* Allocate cache for SRBs. */
3058 srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09003059 SLAB_HWCACHE_ALIGN, NULL);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003060 if (srb_cachep == NULL) {
3061 printk(KERN_ERR
3062 "%s: Unable to allocate SRB cache..."
3063 "Failing load!\n", DRIVER_NAME);
3064 ret = -ENOMEM;
3065 goto no_srp_cache;
3066 }
3067
3068 /* Derive version string. */
3069 strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
Andrew Vasquez11010fe2006-10-06 09:54:59 -07003070 if (ql4xextended_error_logging)
David Somayajuluafaf5a22006-09-19 10:28:00 -07003071 strcat(qla4xxx_version_str, "-debug");
3072
3073 qla4xxx_scsi_transport =
3074 iscsi_register_transport(&qla4xxx_iscsi_transport);
3075 if (!qla4xxx_scsi_transport){
3076 ret = -ENODEV;
3077 goto release_srb_cache;
3078 }
3079
David Somayajuluafaf5a22006-09-19 10:28:00 -07003080 ret = pci_register_driver(&qla4xxx_pci_driver);
3081 if (ret)
3082 goto unregister_transport;
3083
3084 printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
3085 return 0;
Doug Maxey5ae16db2006-10-05 23:50:07 -05003086
David Somayajuluafaf5a22006-09-19 10:28:00 -07003087unregister_transport:
3088 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
3089release_srb_cache:
3090 kmem_cache_destroy(srb_cachep);
3091no_srp_cache:
3092 return ret;
3093}
3094
3095static void __exit qla4xxx_module_exit(void)
3096{
David Somayajuluafaf5a22006-09-19 10:28:00 -07003097 pci_unregister_driver(&qla4xxx_pci_driver);
3098 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
3099 kmem_cache_destroy(srb_cachep);
3100}
3101
3102module_init(qla4xxx_module_init);
3103module_exit(qla4xxx_module_exit);
3104
3105MODULE_AUTHOR("QLogic Corporation");
3106MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
3107MODULE_LICENSE("GPL");
3108MODULE_VERSION(QLA4XXX_DRIVER_VERSION);