blob: 3022c153415661bb20d74d156af0b1f17ff77b31 [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,
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500144 .iface_param_mask = ISCSI_NET_IPV4_ADDR |
145 ISCSI_NET_IPV4_SUBNET |
146 ISCSI_NET_IPV4_GW |
147 ISCSI_NET_IPV4_BOOTPROTO |
148 ISCSI_NET_IFACE_ENABLE |
149 ISCSI_NET_IPV6_LINKLOCAL |
150 ISCSI_NET_IPV6_ADDR |
151 ISCSI_NET_IPV6_ROUTER |
152 ISCSI_NET_IPV6_ADDR_AUTOCFG |
153 ISCSI_NET_IPV6_LINKLOCAL_AUTOCFG |
154 ISCSI_NET_IFACE_ENABLE,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700155 .tgt_dscvr = qla4xxx_tgt_dscvr,
Mike Christie3128c6c2011-07-25 13:48:42 -0500156 .attr_is_visible = ql4_attr_is_visible,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700157 .get_conn_param = qla4xxx_conn_get_param,
158 .get_session_param = qla4xxx_sess_get_param,
Mike Christieaa1e93a2007-05-30 12:57:09 -0500159 .get_host_param = qla4xxx_host_get_param,
Mike Christied00efe32011-07-25 13:48:38 -0500160 .set_iface_param = qla4xxx_iface_set_param,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700161 .session_recovery_timedout = qla4xxx_recovery_timedout,
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500162 .get_iface_param = qla4xxx_get_iface_param,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700163};
164
165static struct scsi_transport_template *qla4xxx_scsi_transport;
166
Mike Christie3128c6c2011-07-25 13:48:42 -0500167static mode_t ql4_attr_is_visible(int param_type, int param)
168{
169 switch (param_type) {
170 case ISCSI_PARAM:
171 switch (param) {
172 case ISCSI_PARAM_CONN_ADDRESS:
173 case ISCSI_PARAM_CONN_PORT:
Mike Christie1d063c12011-07-25 13:48:43 -0500174 case ISCSI_PARAM_TARGET_NAME:
175 case ISCSI_PARAM_TPGT:
176 case ISCSI_PARAM_TARGET_ALIAS:
Mike Christie3128c6c2011-07-25 13:48:42 -0500177 return S_IRUGO;
178 default:
179 return 0;
180 }
181 }
182
183 return 0;
184}
185
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500186static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
187 enum iscsi_param_type param_type,
188 int param, char *buf)
189{
190 struct Scsi_Host *shost = iscsi_iface_to_shost(iface);
191 struct scsi_qla_host *ha = to_qla_host(shost);
192 int len = -ENOSYS;
193
194 if (param_type != ISCSI_NET_PARAM)
195 return -ENOSYS;
196
197 switch (param) {
198 case ISCSI_NET_PARAM_IPV4_ADDR:
199 len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
200 break;
201 case ISCSI_NET_PARAM_IPV4_SUBNET:
202 len = sprintf(buf, "%pI4\n", &ha->ip_config.subnet_mask);
203 break;
204 case ISCSI_NET_PARAM_IPV4_GW:
205 len = sprintf(buf, "%pI4\n", &ha->ip_config.gateway);
206 break;
207 case ISCSI_NET_PARAM_IFACE_ENABLE:
208 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
209 len = sprintf(buf, "%s\n",
210 (ha->ip_config.ipv4_options &
211 IPOPT_IPV4_PROTOCOL_ENABLE) ?
212 "enabled" : "disabled");
213 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
214 len = sprintf(buf, "%s\n",
215 (ha->ip_config.ipv6_options &
216 IPV6_OPT_IPV6_PROTOCOL_ENABLE) ?
217 "enabled" : "disabled");
218 break;
219 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
220 len = sprintf(buf, "%s\n",
221 (ha->ip_config.tcp_options & TCPOPT_DHCP_ENABLE) ?
222 "dhcp" : "static");
223 break;
224 case ISCSI_NET_PARAM_IPV6_ADDR:
225 if (iface->iface_num == 0)
226 len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr0);
227 if (iface->iface_num == 1)
228 len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr1);
229 break;
230 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
231 len = sprintf(buf, "%pI6\n",
232 &ha->ip_config.ipv6_link_local_addr);
233 break;
234 case ISCSI_NET_PARAM_IPV6_ROUTER:
235 len = sprintf(buf, "%pI6\n",
236 &ha->ip_config.ipv6_default_router_addr);
237 break;
238 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
239 len = sprintf(buf, "%s\n",
240 (ha->ip_config.ipv6_addl_options &
241 IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) ?
242 "nd" : "static");
243 break;
244 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
245 len = sprintf(buf, "%s\n",
246 (ha->ip_config.ipv6_addl_options &
247 IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR) ?
248 "auto" : "static");
249 break;
250 default:
251 len = -ENOSYS;
252 }
253
254 return len;
255}
256
Mike Christie5c656af2009-07-15 15:02:59 -0500257static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
258{
259 struct iscsi_cls_session *session;
260 struct ddb_entry *ddb_entry;
261
262 session = starget_to_session(scsi_target(sc->device));
263 ddb_entry = session->dd_data;
264
265 /* if we are not logged in then the LLD is going to clean up the cmd */
266 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
267 return BLK_EH_RESET_TIMER;
268 else
269 return BLK_EH_NOT_HANDLED;
270}
271
David Somayajuluafaf5a22006-09-19 10:28:00 -0700272static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session)
273{
274 struct ddb_entry *ddb_entry = session->dd_data;
275 struct scsi_qla_host *ha = ddb_entry->ha;
276
Mike Christie568d3032008-01-31 13:36:47 -0600277 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
278 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700279
Vikas Chaudhary3013cea2010-07-30 14:25:46 +0530280 DEBUG2(printk("scsi%ld: %s: ddb [%d] session recovery timeout "
Mike Christie568d3032008-01-31 13:36:47 -0600281 "of (%d) secs exhausted, marking device DEAD.\n",
282 ha->host_no, __func__, ddb_entry->fw_ddb_index,
Vikas Chaudhary30387272011-03-21 03:34:32 -0700283 ddb_entry->sess->recovery_tmo));
Mike Christie568d3032008-01-31 13:36:47 -0600284 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700285}
286
Mike Christieaa1e93a2007-05-30 12:57:09 -0500287static int qla4xxx_host_get_param(struct Scsi_Host *shost,
288 enum iscsi_host_param param, char *buf)
289{
290 struct scsi_qla_host *ha = to_qla_host(shost);
291 int len;
292
293 switch (param) {
294 case ISCSI_HOST_PARAM_HWADDRESS:
Michael Chan7ffc49a2007-12-24 21:28:09 -0800295 len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
Mike Christieaa1e93a2007-05-30 12:57:09 -0500296 break;
Mike Christie22236962007-05-30 12:57:24 -0500297 case ISCSI_HOST_PARAM_IPADDRESS:
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500298 len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
Mike Christie22236962007-05-30 12:57:24 -0500299 break;
Mike Christie8ad57812007-05-30 12:57:13 -0500300 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie22236962007-05-30 12:57:24 -0500301 len = sprintf(buf, "%s\n", ha->name_string);
Mike Christie8ad57812007-05-30 12:57:13 -0500302 break;
Mike Christieaa1e93a2007-05-30 12:57:09 -0500303 default:
304 return -ENOSYS;
305 }
306
307 return len;
308}
309
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500310static void qla4xxx_create_ipv4_iface(struct scsi_qla_host *ha)
311{
312 if (ha->iface_ipv4)
313 return;
314
315 /* IPv4 */
316 ha->iface_ipv4 = iscsi_create_iface(ha->host,
317 &qla4xxx_iscsi_transport,
318 ISCSI_IFACE_TYPE_IPV4, 0, 0);
319 if (!ha->iface_ipv4)
320 ql4_printk(KERN_ERR, ha, "Could not create IPv4 iSCSI "
321 "iface0.\n");
322}
323
324static void qla4xxx_create_ipv6_iface(struct scsi_qla_host *ha)
325{
326 if (!ha->iface_ipv6_0)
327 /* IPv6 iface-0 */
328 ha->iface_ipv6_0 = iscsi_create_iface(ha->host,
329 &qla4xxx_iscsi_transport,
330 ISCSI_IFACE_TYPE_IPV6, 0,
331 0);
332 if (!ha->iface_ipv6_0)
333 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
334 "iface0.\n");
335
336 if (!ha->iface_ipv6_1)
337 /* IPv6 iface-1 */
338 ha->iface_ipv6_1 = iscsi_create_iface(ha->host,
339 &qla4xxx_iscsi_transport,
340 ISCSI_IFACE_TYPE_IPV6, 1,
341 0);
342 if (!ha->iface_ipv6_1)
343 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
344 "iface1.\n");
345}
346
347static void qla4xxx_create_ifaces(struct scsi_qla_host *ha)
348{
349 if (ha->ip_config.ipv4_options & IPOPT_IPV4_PROTOCOL_ENABLE)
350 qla4xxx_create_ipv4_iface(ha);
351
352 if (ha->ip_config.ipv6_options & IPV6_OPT_IPV6_PROTOCOL_ENABLE)
353 qla4xxx_create_ipv6_iface(ha);
354}
355
356static void qla4xxx_destroy_ipv4_iface(struct scsi_qla_host *ha)
357{
358 if (ha->iface_ipv4) {
359 iscsi_destroy_iface(ha->iface_ipv4);
360 ha->iface_ipv4 = NULL;
361 }
362}
363
364static void qla4xxx_destroy_ipv6_iface(struct scsi_qla_host *ha)
365{
366 if (ha->iface_ipv6_0) {
367 iscsi_destroy_iface(ha->iface_ipv6_0);
368 ha->iface_ipv6_0 = NULL;
369 }
370 if (ha->iface_ipv6_1) {
371 iscsi_destroy_iface(ha->iface_ipv6_1);
372 ha->iface_ipv6_1 = NULL;
373 }
374}
375
376static void qla4xxx_destroy_ifaces(struct scsi_qla_host *ha)
377{
378 qla4xxx_destroy_ipv4_iface(ha);
379 qla4xxx_destroy_ipv6_iface(ha);
380}
381
Mike Christied00efe32011-07-25 13:48:38 -0500382static void qla4xxx_set_ipv6(struct scsi_qla_host *ha,
383 struct iscsi_iface_param_info *iface_param,
384 struct addr_ctrl_blk *init_fw_cb)
385{
386 /*
387 * iface_num 0 is valid for IPv6 Addr, linklocal, router, autocfg.
388 * iface_num 1 is valid only for IPv6 Addr.
389 */
390 switch (iface_param->param) {
391 case ISCSI_NET_PARAM_IPV6_ADDR:
392 if (iface_param->iface_num & 0x1)
393 /* IPv6 Addr 1 */
394 memcpy(init_fw_cb->ipv6_addr1, iface_param->value,
395 sizeof(init_fw_cb->ipv6_addr1));
396 else
397 /* IPv6 Addr 0 */
398 memcpy(init_fw_cb->ipv6_addr0, iface_param->value,
399 sizeof(init_fw_cb->ipv6_addr0));
400 break;
401 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
402 if (iface_param->iface_num & 0x1)
403 break;
404 memcpy(init_fw_cb->ipv6_if_id, &iface_param->value[8],
405 sizeof(init_fw_cb->ipv6_if_id));
406 break;
407 case ISCSI_NET_PARAM_IPV6_ROUTER:
408 if (iface_param->iface_num & 0x1)
409 break;
410 memcpy(init_fw_cb->ipv6_dflt_rtr_addr, iface_param->value,
411 sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
412 break;
413 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
414 /* Autocfg applies to even interface */
415 if (iface_param->iface_num & 0x1)
416 break;
417
418 if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_DISABLE)
419 init_fw_cb->ipv6_addtl_opts &=
420 cpu_to_le16(
421 ~IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
422 else if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_ND_ENABLE)
423 init_fw_cb->ipv6_addtl_opts |=
424 cpu_to_le16(
425 IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
426 else
427 ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
428 "IPv6 addr\n");
429 break;
430 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
431 /* Autocfg applies to even interface */
432 if (iface_param->iface_num & 0x1)
433 break;
434
435 if (iface_param->value[0] ==
436 ISCSI_IPV6_LINKLOCAL_AUTOCFG_ENABLE)
437 init_fw_cb->ipv6_addtl_opts |= cpu_to_le16(
438 IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
439 else if (iface_param->value[0] ==
440 ISCSI_IPV6_LINKLOCAL_AUTOCFG_DISABLE)
441 init_fw_cb->ipv6_addtl_opts &= cpu_to_le16(
442 ~IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
443 else
444 ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
445 "IPv6 linklocal addr\n");
446 break;
447 case ISCSI_NET_PARAM_IPV6_ROUTER_AUTOCFG:
448 /* Autocfg applies to even interface */
449 if (iface_param->iface_num & 0x1)
450 break;
451
452 if (iface_param->value[0] == ISCSI_IPV6_ROUTER_AUTOCFG_ENABLE)
453 memset(init_fw_cb->ipv6_dflt_rtr_addr, 0,
454 sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
455 break;
456 case ISCSI_NET_PARAM_IFACE_ENABLE:
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500457 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
Mike Christied00efe32011-07-25 13:48:38 -0500458 init_fw_cb->ipv6_opts |=
459 cpu_to_le16(IPV6_OPT_IPV6_PROTOCOL_ENABLE);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500460 qla4xxx_create_ipv6_iface(ha);
461 } else {
Mike Christied00efe32011-07-25 13:48:38 -0500462 init_fw_cb->ipv6_opts &=
463 cpu_to_le16(~IPV6_OPT_IPV6_PROTOCOL_ENABLE &
464 0xFFFF);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500465 qla4xxx_destroy_ipv6_iface(ha);
466 }
Mike Christied00efe32011-07-25 13:48:38 -0500467 break;
468 case ISCSI_NET_PARAM_VLAN_ID:
469 if (iface_param->len != sizeof(init_fw_cb->ipv6_vlan_tag))
470 break;
471 init_fw_cb->ipv6_vlan_tag = *(uint16_t *)iface_param->value;
472 break;
473 default:
474 ql4_printk(KERN_ERR, ha, "Unknown IPv6 param = %d\n",
475 iface_param->param);
476 break;
477 }
478}
479
480static void qla4xxx_set_ipv4(struct scsi_qla_host *ha,
481 struct iscsi_iface_param_info *iface_param,
482 struct addr_ctrl_blk *init_fw_cb)
483{
484 switch (iface_param->param) {
485 case ISCSI_NET_PARAM_IPV4_ADDR:
486 memcpy(init_fw_cb->ipv4_addr, iface_param->value,
487 sizeof(init_fw_cb->ipv4_addr));
488 break;
489 case ISCSI_NET_PARAM_IPV4_SUBNET:
490 memcpy(init_fw_cb->ipv4_subnet, iface_param->value,
491 sizeof(init_fw_cb->ipv4_subnet));
492 break;
493 case ISCSI_NET_PARAM_IPV4_GW:
494 memcpy(init_fw_cb->ipv4_gw_addr, iface_param->value,
495 sizeof(init_fw_cb->ipv4_gw_addr));
496 break;
497 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
498 if (iface_param->value[0] == ISCSI_BOOTPROTO_DHCP)
499 init_fw_cb->ipv4_tcp_opts |=
500 cpu_to_le16(TCPOPT_DHCP_ENABLE);
501 else if (iface_param->value[0] == ISCSI_BOOTPROTO_STATIC)
502 init_fw_cb->ipv4_tcp_opts &=
503 cpu_to_le16(~TCPOPT_DHCP_ENABLE);
504 else
505 ql4_printk(KERN_ERR, ha, "Invalid IPv4 bootproto\n");
506 break;
507 case ISCSI_NET_PARAM_IFACE_ENABLE:
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500508 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
Mike Christied00efe32011-07-25 13:48:38 -0500509 init_fw_cb->ipv4_ip_opts |=
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500510 cpu_to_le16(IPOPT_IPV4_PROTOCOL_ENABLE);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500511 qla4xxx_create_ipv4_iface(ha);
512 } else {
Mike Christied00efe32011-07-25 13:48:38 -0500513 init_fw_cb->ipv4_ip_opts &=
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500514 cpu_to_le16(~IPOPT_IPV4_PROTOCOL_ENABLE &
Mike Christied00efe32011-07-25 13:48:38 -0500515 0xFFFF);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500516 qla4xxx_destroy_ipv4_iface(ha);
517 }
Mike Christied00efe32011-07-25 13:48:38 -0500518 break;
519 case ISCSI_NET_PARAM_VLAN_ID:
520 if (iface_param->len != sizeof(init_fw_cb->ipv4_vlan_tag))
521 break;
522 init_fw_cb->ipv4_vlan_tag = *(uint16_t *)iface_param->value;
523 break;
524 default:
525 ql4_printk(KERN_ERR, ha, "Unknown IPv4 param = %d\n",
526 iface_param->param);
527 break;
528 }
529}
530
531static void
532qla4xxx_initcb_to_acb(struct addr_ctrl_blk *init_fw_cb)
533{
534 struct addr_ctrl_blk_def *acb;
535 acb = (struct addr_ctrl_blk_def *)init_fw_cb;
536 memset(acb->reserved1, 0, sizeof(acb->reserved1));
537 memset(acb->reserved2, 0, sizeof(acb->reserved2));
538 memset(acb->reserved3, 0, sizeof(acb->reserved3));
539 memset(acb->reserved4, 0, sizeof(acb->reserved4));
540 memset(acb->reserved5, 0, sizeof(acb->reserved5));
541 memset(acb->reserved6, 0, sizeof(acb->reserved6));
542 memset(acb->reserved7, 0, sizeof(acb->reserved7));
543 memset(acb->reserved8, 0, sizeof(acb->reserved8));
544 memset(acb->reserved9, 0, sizeof(acb->reserved9));
545 memset(acb->reserved10, 0, sizeof(acb->reserved10));
546 memset(acb->reserved11, 0, sizeof(acb->reserved11));
547 memset(acb->reserved12, 0, sizeof(acb->reserved12));
548 memset(acb->reserved13, 0, sizeof(acb->reserved13));
549 memset(acb->reserved14, 0, sizeof(acb->reserved14));
550 memset(acb->reserved15, 0, sizeof(acb->reserved15));
551}
552
553static int
554qla4xxx_iface_set_param(struct Scsi_Host *shost, char *data, int count)
555{
556 struct scsi_qla_host *ha = to_qla_host(shost);
557 int rval = 0;
558 struct iscsi_iface_param_info *iface_param = NULL;
559 struct addr_ctrl_blk *init_fw_cb = NULL;
560 dma_addr_t init_fw_cb_dma;
561 uint32_t mbox_cmd[MBOX_REG_COUNT];
562 uint32_t mbox_sts[MBOX_REG_COUNT];
563 uint32_t total_param_count;
564 uint32_t length;
565
566 init_fw_cb = dma_alloc_coherent(&ha->pdev->dev,
567 sizeof(struct addr_ctrl_blk),
568 &init_fw_cb_dma, GFP_KERNEL);
569 if (!init_fw_cb) {
570 ql4_printk(KERN_ERR, ha, "%s: Unable to alloc init_cb\n",
571 __func__);
572 return -ENOMEM;
573 }
574
575 memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
576 memset(&mbox_cmd, 0, sizeof(mbox_cmd));
577 memset(&mbox_sts, 0, sizeof(mbox_sts));
578
579 if (qla4xxx_get_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma)) {
580 ql4_printk(KERN_ERR, ha, "%s: get ifcb failed\n", __func__);
581 rval = -EIO;
582 goto exit_init_fw_cb;
583 }
584
585 total_param_count = count;
586 iface_param = (struct iscsi_iface_param_info *)data;
587
588 for ( ; total_param_count != 0; total_param_count--) {
589 length = iface_param->len;
590
591 if (iface_param->param_type != ISCSI_NET_PARAM)
592 continue;
593
594 switch (iface_param->iface_type) {
595 case ISCSI_IFACE_TYPE_IPV4:
596 switch (iface_param->iface_num) {
597 case 0:
598 qla4xxx_set_ipv4(ha, iface_param, init_fw_cb);
599 break;
600 default:
601 /* Cannot have more than one IPv4 interface */
602 ql4_printk(KERN_ERR, ha, "Invalid IPv4 iface "
603 "number = %d\n",
604 iface_param->iface_num);
605 break;
606 }
607 break;
608 case ISCSI_IFACE_TYPE_IPV6:
609 switch (iface_param->iface_num) {
610 case 0:
611 case 1:
612 qla4xxx_set_ipv6(ha, iface_param, init_fw_cb);
613 break;
614 default:
615 /* Cannot have more than two IPv6 interface */
616 ql4_printk(KERN_ERR, ha, "Invalid IPv6 iface "
617 "number = %d\n",
618 iface_param->iface_num);
619 break;
620 }
621 break;
622 default:
623 ql4_printk(KERN_ERR, ha, "Invalid iface type\n");
624 break;
625 }
626
627 iface_param = (struct iscsi_iface_param_info *)
628 ((uint8_t *)iface_param +
629 sizeof(struct iscsi_iface_param_info) + length);
630 }
631
632 init_fw_cb->cookie = cpu_to_le32(0x11BEAD5A);
633
634 rval = qla4xxx_set_flash(ha, init_fw_cb_dma, FLASH_SEGMENT_IFCB,
635 sizeof(struct addr_ctrl_blk),
636 FLASH_OPT_RMW_COMMIT);
637 if (rval != QLA_SUCCESS) {
638 ql4_printk(KERN_ERR, ha, "%s: set flash mbx failed\n",
639 __func__);
640 rval = -EIO;
641 goto exit_init_fw_cb;
642 }
643
644 qla4xxx_disable_acb(ha);
645
646 qla4xxx_initcb_to_acb(init_fw_cb);
647
648 rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma);
649 if (rval != QLA_SUCCESS) {
650 ql4_printk(KERN_ERR, ha, "%s: set acb mbx failed\n",
651 __func__);
652 rval = -EIO;
653 goto exit_init_fw_cb;
654 }
655
656 memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
657 qla4xxx_update_local_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb,
658 init_fw_cb_dma);
659
660exit_init_fw_cb:
661 dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk),
662 init_fw_cb, init_fw_cb_dma);
663
664 return rval;
665}
666
David Somayajuluafaf5a22006-09-19 10:28:00 -0700667static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
668 enum iscsi_param param, char *buf)
669{
670 struct ddb_entry *ddb_entry = sess->dd_data;
671 int len;
672
673 switch (param) {
674 case ISCSI_PARAM_TARGET_NAME:
675 len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
676 ddb_entry->iscsi_name);
677 break;
678 case ISCSI_PARAM_TPGT:
679 len = sprintf(buf, "%u\n", ddb_entry->tpgt);
680 break;
Vikas Chaudhary3b2bef12010-07-10 14:51:30 +0530681 case ISCSI_PARAM_TARGET_ALIAS:
682 len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
683 ddb_entry->iscsi_alias);
684 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700685 default:
686 return -ENOSYS;
687 }
688
689 return len;
690}
691
692static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
693 enum iscsi_param param, char *buf)
694{
695 struct iscsi_cls_session *session;
696 struct ddb_entry *ddb_entry;
697 int len;
698
699 session = iscsi_dev_to_session(conn->dev.parent);
700 ddb_entry = session->dd_data;
701
702 switch (param) {
703 case ISCSI_PARAM_CONN_PORT:
704 len = sprintf(buf, "%hu\n", ddb_entry->port);
705 break;
706 case ISCSI_PARAM_CONN_ADDRESS:
707 /* TODO: what are the ipv6 bits */
Harvey Harrison63779432008-10-31 00:56:00 -0700708 len = sprintf(buf, "%pI4\n", &ddb_entry->ip_addr);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700709 break;
710 default:
711 return -ENOSYS;
712 }
713
714 return len;
715}
716
Mike Christie2174a042007-05-30 12:57:10 -0500717static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
718 enum iscsi_tgt_dscvr type, uint32_t enable,
719 struct sockaddr *dst_addr)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700720{
721 struct scsi_qla_host *ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700722 struct sockaddr_in *addr;
723 struct sockaddr_in6 *addr6;
724 int ret = 0;
725
David Somayajuluafaf5a22006-09-19 10:28:00 -0700726 ha = (struct scsi_qla_host *) shost->hostdata;
727
728 switch (type) {
729 case ISCSI_TGT_DSCVR_SEND_TARGETS:
730 if (dst_addr->sa_family == AF_INET) {
731 addr = (struct sockaddr_in *)dst_addr;
732 if (qla4xxx_send_tgts(ha, (char *)&addr->sin_addr,
733 addr->sin_port) != QLA_SUCCESS)
734 ret = -EIO;
735 } else if (dst_addr->sa_family == AF_INET6) {
736 /*
737 * TODO: fix qla4xxx_send_tgts
738 */
739 addr6 = (struct sockaddr_in6 *)dst_addr;
740 if (qla4xxx_send_tgts(ha, (char *)&addr6->sin6_addr,
741 addr6->sin6_port) != QLA_SUCCESS)
742 ret = -EIO;
743 } else
744 ret = -ENOSYS;
745 break;
746 default:
747 ret = -ENOSYS;
748 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700749 return ret;
750}
751
752void qla4xxx_destroy_sess(struct ddb_entry *ddb_entry)
753{
754 if (!ddb_entry->sess)
755 return;
756
757 if (ddb_entry->conn) {
Mike Christie26974782007-12-13 12:43:29 -0600758 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700759 iscsi_remove_session(ddb_entry->sess);
760 }
761 iscsi_free_session(ddb_entry->sess);
762}
763
764int qla4xxx_add_sess(struct ddb_entry *ddb_entry)
765{
766 int err;
767
Vikas Chaudhary30387272011-03-21 03:34:32 -0700768 ddb_entry->sess->recovery_tmo = ql4xsess_recovery_tmo;
Vikas Chaudhary3013cea2010-07-30 14:25:46 +0530769
David Somayajuluafaf5a22006-09-19 10:28:00 -0700770 err = iscsi_add_session(ddb_entry->sess, ddb_entry->fw_ddb_index);
771 if (err) {
772 DEBUG2(printk(KERN_ERR "Could not add session.\n"));
773 return err;
774 }
775
Mike Christie5d91e202008-05-21 15:54:01 -0500776 ddb_entry->conn = iscsi_create_conn(ddb_entry->sess, 0, 0);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700777 if (!ddb_entry->conn) {
778 iscsi_remove_session(ddb_entry->sess);
779 DEBUG2(printk(KERN_ERR "Could not add connection.\n"));
780 return -ENOMEM;
781 }
Mike Christieb6359302008-01-31 13:36:44 -0600782
783 /* finally ready to go */
784 iscsi_unblock_session(ddb_entry->sess);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700785 return 0;
786}
787
788struct ddb_entry *qla4xxx_alloc_sess(struct scsi_qla_host *ha)
789{
790 struct ddb_entry *ddb_entry;
791 struct iscsi_cls_session *sess;
792
Mike Christie5d91e202008-05-21 15:54:01 -0500793 sess = iscsi_alloc_session(ha->host, &qla4xxx_iscsi_transport,
794 sizeof(struct ddb_entry));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700795 if (!sess)
796 return NULL;
797
798 ddb_entry = sess->dd_data;
799 memset(ddb_entry, 0, sizeof(*ddb_entry));
800 ddb_entry->ha = ha;
801 ddb_entry->sess = sess;
802 return ddb_entry;
803}
804
Mike Christie024f8012008-03-04 13:26:54 -0600805static void qla4xxx_scan_start(struct Scsi_Host *shost)
806{
807 struct scsi_qla_host *ha = shost_priv(shost);
808 struct ddb_entry *ddb_entry, *ddbtemp;
809
810 /* finish setup of sessions that were already setup in firmware */
811 list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
812 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE)
813 qla4xxx_add_sess(ddb_entry);
814 }
815}
816
David Somayajuluafaf5a22006-09-19 10:28:00 -0700817/*
818 * Timer routines
819 */
820
821static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
822 unsigned long interval)
823{
824 DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
825 __func__, ha->host->host_no));
826 init_timer(&ha->timer);
827 ha->timer.expires = jiffies + interval * HZ;
828 ha->timer.data = (unsigned long)ha;
829 ha->timer.function = (void (*)(unsigned long))func;
830 add_timer(&ha->timer);
831 ha->timer_active = 1;
832}
833
834static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
835{
836 del_timer_sync(&ha->timer);
837 ha->timer_active = 0;
838}
839
840/***
841 * qla4xxx_mark_device_missing - mark a device as missing.
842 * @ha: Pointer to host adapter structure.
843 * @ddb_entry: Pointer to device database entry
844 *
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530845 * This routine marks a device missing and close connection.
David Somayajuluafaf5a22006-09-19 10:28:00 -0700846 **/
847void qla4xxx_mark_device_missing(struct scsi_qla_host *ha,
848 struct ddb_entry *ddb_entry)
849{
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530850 if ((atomic_read(&ddb_entry->state) != DDB_STATE_DEAD)) {
851 atomic_set(&ddb_entry->state, DDB_STATE_MISSING);
852 DEBUG2(printk("scsi%ld: ddb [%d] marked MISSING\n",
853 ha->host_no, ddb_entry->fw_ddb_index));
854 } else
855 DEBUG2(printk("scsi%ld: ddb [%d] DEAD\n", ha->host_no,
856 ddb_entry->fw_ddb_index))
857
Mike Christieb6359302008-01-31 13:36:44 -0600858 iscsi_block_session(ddb_entry->sess);
Mike Christiee5bd7b52008-09-24 11:46:10 -0500859 iscsi_conn_error_event(ddb_entry->conn, ISCSI_ERR_CONN_FAILED);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700860}
861
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530862/**
863 * qla4xxx_mark_all_devices_missing - mark all devices as missing.
864 * @ha: Pointer to host adapter structure.
865 *
866 * This routine marks a device missing and resets the relogin retry count.
867 **/
868void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
869{
870 struct ddb_entry *ddb_entry, *ddbtemp;
871 list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
872 qla4xxx_mark_device_missing(ha, ddb_entry);
873 }
874}
875
David Somayajuluafaf5a22006-09-19 10:28:00 -0700876static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
877 struct ddb_entry *ddb_entry,
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -0700878 struct scsi_cmnd *cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700879{
880 struct srb *srb;
881
882 srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
883 if (!srb)
884 return srb;
885
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530886 kref_init(&srb->srb_ref);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700887 srb->ha = ha;
888 srb->ddb = ddb_entry;
889 srb->cmd = cmd;
890 srb->flags = 0;
Vikas Chaudhary53698872010-04-28 11:41:59 +0530891 CMD_SP(cmd) = (void *)srb;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700892
893 return srb;
894}
895
896static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
897{
898 struct scsi_cmnd *cmd = srb->cmd;
899
900 if (srb->flags & SRB_DMA_VALID) {
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +0900901 scsi_dma_unmap(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700902 srb->flags &= ~SRB_DMA_VALID;
903 }
Vikas Chaudhary53698872010-04-28 11:41:59 +0530904 CMD_SP(cmd) = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700905}
906
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530907void qla4xxx_srb_compl(struct kref *ref)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700908{
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530909 struct srb *srb = container_of(ref, struct srb, srb_ref);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700910 struct scsi_cmnd *cmd = srb->cmd;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530911 struct scsi_qla_host *ha = srb->ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700912
913 qla4xxx_srb_free_dma(ha, srb);
914
915 mempool_free(srb, ha->srb_mempool);
916
917 cmd->scsi_done(cmd);
918}
919
920/**
921 * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -0700922 * @host: scsi host
David Somayajuluafaf5a22006-09-19 10:28:00 -0700923 * @cmd: Pointer to Linux's SCSI command structure
David Somayajuluafaf5a22006-09-19 10:28:00 -0700924 *
925 * Remarks:
926 * This routine is invoked by Linux to send a SCSI command to the driver.
927 * The mid-level driver tries to ensure that queuecommand never gets
928 * invoked concurrently with itself or the interrupt handler (although
929 * the interrupt handler may call this routine as part of request-
930 * completion handling). Unfortunely, it sometimes calls the scheduler
931 * in interrupt context which is a big NO! NO!.
932 **/
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -0700933static int qla4xxx_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700934{
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -0700935 struct scsi_qla_host *ha = to_qla_host(host);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700936 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christie7fb19212008-01-31 13:36:45 -0600937 struct iscsi_cls_session *sess = ddb_entry->sess;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700938 struct srb *srb;
939 int rval;
940
Lalit Chandivade2232be02010-07-30 14:38:47 +0530941 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
942 if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
943 cmd->result = DID_NO_CONNECT << 16;
944 else
945 cmd->result = DID_REQUEUE << 16;
946 goto qc_fail_command;
947 }
948
Mike Christie7fb19212008-01-31 13:36:45 -0600949 if (!sess) {
950 cmd->result = DID_IMM_RETRY << 16;
951 goto qc_fail_command;
952 }
953
954 rval = iscsi_session_chkready(sess);
955 if (rval) {
956 cmd->result = rval;
957 goto qc_fail_command;
958 }
959
David Somayajuluafaf5a22006-09-19 10:28:00 -0700960 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
961 if (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD) {
962 cmd->result = DID_NO_CONNECT << 16;
963 goto qc_fail_command;
964 }
Mike Christiec5e98e92008-08-17 15:24:39 -0500965 return SCSI_MLQUEUE_TARGET_BUSY;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700966 }
967
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530968 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
969 test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
970 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
971 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
972 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
973 !test_bit(AF_ONLINE, &ha->flags) ||
974 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
David C Somayajulu477ffb92007-01-22 12:26:11 -0800975 goto qc_host_busy;
976
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -0700977 srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700978 if (!srb)
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -0700979 goto qc_host_busy;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700980
981 rval = qla4xxx_send_command_to_isp(ha, srb);
982 if (rval != QLA_SUCCESS)
983 goto qc_host_busy_free_sp;
984
David Somayajuluafaf5a22006-09-19 10:28:00 -0700985 return 0;
986
987qc_host_busy_free_sp:
988 qla4xxx_srb_free_dma(ha, srb);
989 mempool_free(srb, ha->srb_mempool);
990
David Somayajuluafaf5a22006-09-19 10:28:00 -0700991qc_host_busy:
992 return SCSI_MLQUEUE_HOST_BUSY;
993
994qc_fail_command:
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -0700995 cmd->scsi_done(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700996
997 return 0;
998}
999
1000/**
1001 * qla4xxx_mem_free - frees memory allocated to adapter
1002 * @ha: Pointer to host adapter structure.
1003 *
1004 * Frees memory previously allocated by qla4xxx_mem_alloc
1005 **/
1006static void qla4xxx_mem_free(struct scsi_qla_host *ha)
1007{
1008 if (ha->queues)
1009 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
1010 ha->queues_dma);
1011
1012 ha->queues_len = 0;
1013 ha->queues = NULL;
1014 ha->queues_dma = 0;
1015 ha->request_ring = NULL;
1016 ha->request_dma = 0;
1017 ha->response_ring = NULL;
1018 ha->response_dma = 0;
1019 ha->shadow_regs = NULL;
1020 ha->shadow_regs_dma = 0;
1021
1022 /* Free srb pool. */
1023 if (ha->srb_mempool)
1024 mempool_destroy(ha->srb_mempool);
1025
1026 ha->srb_mempool = NULL;
1027
1028 /* release io space registers */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301029 if (is_qla8022(ha)) {
1030 if (ha->nx_pcibase)
1031 iounmap(
1032 (struct device_reg_82xx __iomem *)ha->nx_pcibase);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301033 } else if (ha->reg)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001034 iounmap(ha->reg);
1035 pci_release_regions(ha->pdev);
1036}
1037
1038/**
1039 * qla4xxx_mem_alloc - allocates memory for use by adapter.
1040 * @ha: Pointer to host adapter structure
1041 *
1042 * Allocates DMA memory for request and response queues. Also allocates memory
1043 * for srbs.
1044 **/
1045static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
1046{
1047 unsigned long align;
1048
1049 /* Allocate contiguous block of DMA memory for queues. */
1050 ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
1051 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
1052 sizeof(struct shadow_regs) +
1053 MEM_ALIGN_VALUE +
1054 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
1055 ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
1056 &ha->queues_dma, GFP_KERNEL);
1057 if (ha->queues == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301058 ql4_printk(KERN_WARNING, ha,
1059 "Memory Allocation failed - queues.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001060
1061 goto mem_alloc_error_exit;
1062 }
1063 memset(ha->queues, 0, ha->queues_len);
1064
1065 /*
1066 * As per RISC alignment requirements -- the bus-address must be a
1067 * multiple of the request-ring size (in bytes).
1068 */
1069 align = 0;
1070 if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
1071 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
1072 (MEM_ALIGN_VALUE - 1));
1073
1074 /* Update request and response queue pointers. */
1075 ha->request_dma = ha->queues_dma + align;
1076 ha->request_ring = (struct queue_entry *) (ha->queues + align);
1077 ha->response_dma = ha->queues_dma + align +
1078 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
1079 ha->response_ring = (struct queue_entry *) (ha->queues + align +
1080 (REQUEST_QUEUE_DEPTH *
1081 QUEUE_SIZE));
1082 ha->shadow_regs_dma = ha->queues_dma + align +
1083 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
1084 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
1085 ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
1086 (REQUEST_QUEUE_DEPTH *
1087 QUEUE_SIZE) +
1088 (RESPONSE_QUEUE_DEPTH *
1089 QUEUE_SIZE));
1090
1091 /* Allocate memory for srb pool. */
1092 ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
1093 mempool_free_slab, srb_cachep);
1094 if (ha->srb_mempool == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301095 ql4_printk(KERN_WARNING, ha,
1096 "Memory Allocation failed - SRB Pool.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001097
1098 goto mem_alloc_error_exit;
1099 }
1100
1101 return QLA_SUCCESS;
1102
1103mem_alloc_error_exit:
1104 qla4xxx_mem_free(ha);
1105 return QLA_ERROR;
1106}
1107
1108/**
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301109 * qla4_8xxx_check_fw_alive - Check firmware health
1110 * @ha: Pointer to host adapter structure.
1111 *
1112 * Context: Interrupt
1113 **/
1114static void qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
1115{
1116 uint32_t fw_heartbeat_counter, halt_status;
1117
1118 fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
Lalit Chandivade2232be02010-07-30 14:38:47 +05301119 /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
1120 if (fw_heartbeat_counter == 0xffffffff) {
1121 DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
1122 "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
1123 ha->host_no, __func__));
1124 return;
1125 }
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301126
1127 if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
1128 ha->seconds_since_last_heartbeat++;
1129 /* FW not alive after 2 seconds */
1130 if (ha->seconds_since_last_heartbeat == 2) {
1131 ha->seconds_since_last_heartbeat = 0;
1132 halt_status = qla4_8xxx_rd_32(ha,
Vikas Chaudhary68d92eb2011-05-17 23:17:05 -07001133 QLA82XX_PEG_HALT_STATUS1);
1134
1135 ql4_printk(KERN_INFO, ha,
1136 "scsi(%ld): %s, Dumping hw/fw registers:\n "
1137 " PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2:"
1138 " 0x%x,\n PEG_NET_0_PC: 0x%x, PEG_NET_1_PC:"
1139 " 0x%x,\n PEG_NET_2_PC: 0x%x, PEG_NET_3_PC:"
1140 " 0x%x,\n PEG_NET_4_PC: 0x%x\n",
1141 ha->host_no, __func__, halt_status,
1142 qla4_8xxx_rd_32(ha,
1143 QLA82XX_PEG_HALT_STATUS2),
1144 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_0 +
1145 0x3c),
1146 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_1 +
1147 0x3c),
1148 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_2 +
1149 0x3c),
1150 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_3 +
1151 0x3c),
1152 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_4 +
1153 0x3c));
Nilesh Javali21033632010-07-30 14:28:07 +05301154
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301155 /* Since we cannot change dev_state in interrupt
1156 * context, set appropriate DPC flag then wakeup
1157 * DPC */
1158 if (halt_status & HALT_STATUS_UNRECOVERABLE)
1159 set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
1160 else {
1161 printk("scsi%ld: %s: detect abort needed!\n",
1162 ha->host_no, __func__);
1163 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1164 }
1165 qla4xxx_wake_dpc(ha);
Nilesh Javali21033632010-07-30 14:28:07 +05301166 qla4xxx_mailbox_premature_completion(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301167 }
Lalit Chandivade99457d72010-10-06 22:49:32 -07001168 } else
1169 ha->seconds_since_last_heartbeat = 0;
1170
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301171 ha->fw_heartbeat_counter = fw_heartbeat_counter;
1172}
1173
1174/**
1175 * qla4_8xxx_watchdog - Poll dev state
1176 * @ha: Pointer to host adapter structure.
1177 *
1178 * Context: Interrupt
1179 **/
1180void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
1181{
1182 uint32_t dev_state;
1183
1184 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
1185
1186 /* don't poll if reset is going on */
Lalit Chandivaded56a1f72010-12-02 22:12:45 -08001187 if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
1188 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
Vikas Chaudhary977f46a2011-05-17 23:17:08 -07001189 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags))) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301190 if (dev_state == QLA82XX_DEV_NEED_RESET &&
1191 !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
Vikas Chaudhary3930b8c2010-12-02 22:12:47 -08001192 if (!ql4xdontresethba) {
1193 ql4_printk(KERN_INFO, ha, "%s: HW State: "
1194 "NEED RESET!\n", __func__);
1195 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1196 qla4xxx_wake_dpc(ha);
1197 qla4xxx_mailbox_premature_completion(ha);
1198 }
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301199 } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
1200 !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
Vikas Chaudhary3930b8c2010-12-02 22:12:47 -08001201 ql4_printk(KERN_INFO, ha, "%s: HW State: NEED QUIES!\n",
1202 __func__);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301203 set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
1204 qla4xxx_wake_dpc(ha);
1205 } else {
1206 /* Check firmware health */
1207 qla4_8xxx_check_fw_alive(ha);
1208 }
1209 }
1210}
1211
1212/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07001213 * qla4xxx_timer - checks every second for work to do.
1214 * @ha: Pointer to host adapter structure.
1215 **/
1216static void qla4xxx_timer(struct scsi_qla_host *ha)
1217{
1218 struct ddb_entry *ddb_entry, *dtemp;
1219 int start_dpc = 0;
Lalit Chandivade2232be02010-07-30 14:38:47 +05301220 uint16_t w;
1221
1222 /* If we are in the middle of AER/EEH processing
1223 * skip any processing and reschedule the timer
1224 */
1225 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1226 mod_timer(&ha->timer, jiffies + HZ);
1227 return;
1228 }
1229
1230 /* Hardware read to trigger an EEH error during mailbox waits. */
1231 if (!pci_channel_offline(ha->pdev))
1232 pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001233
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301234 if (is_qla8022(ha)) {
1235 qla4_8xxx_watchdog(ha);
1236 }
1237
David Somayajuluafaf5a22006-09-19 10:28:00 -07001238 /* Search for relogin's to time-out and port down retry. */
1239 list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
1240 /* Count down time between sending relogins */
1241 if (adapter_up(ha) &&
1242 !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
1243 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
1244 if (atomic_read(&ddb_entry->retry_relogin_timer) !=
1245 INVALID_ENTRY) {
1246 if (atomic_read(&ddb_entry->retry_relogin_timer)
1247 == 0) {
1248 atomic_set(&ddb_entry->
1249 retry_relogin_timer,
1250 INVALID_ENTRY);
1251 set_bit(DPC_RELOGIN_DEVICE,
1252 &ha->dpc_flags);
1253 set_bit(DF_RELOGIN, &ddb_entry->flags);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301254 DEBUG2(printk("scsi%ld: %s: ddb [%d]"
David Somayajuluafaf5a22006-09-19 10:28:00 -07001255 " login device\n",
1256 ha->host_no, __func__,
1257 ddb_entry->fw_ddb_index));
1258 } else
1259 atomic_dec(&ddb_entry->
1260 retry_relogin_timer);
1261 }
1262 }
1263
1264 /* Wait for relogin to timeout */
1265 if (atomic_read(&ddb_entry->relogin_timer) &&
1266 (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
1267 /*
1268 * If the relogin times out and the device is
1269 * still NOT ONLINE then try and relogin again.
1270 */
1271 if (atomic_read(&ddb_entry->state) !=
1272 DDB_STATE_ONLINE &&
1273 ddb_entry->fw_ddb_device_state ==
1274 DDB_DS_SESSION_FAILED) {
1275 /* Reset retry relogin timer */
1276 atomic_inc(&ddb_entry->relogin_retry_count);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301277 DEBUG2(printk("scsi%ld: ddb [%d] relogin"
David Somayajuluafaf5a22006-09-19 10:28:00 -07001278 " timed out-retrying"
1279 " relogin (%d)\n",
1280 ha->host_no,
1281 ddb_entry->fw_ddb_index,
1282 atomic_read(&ddb_entry->
1283 relogin_retry_count))
1284 );
1285 start_dpc++;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301286 DEBUG(printk("scsi%ld:%d:%d: ddb [%d] "
Justin P. Mattockcd09b2c2011-01-28 11:49:08 -08001287 "initiate relogin after"
David Somayajuluafaf5a22006-09-19 10:28:00 -07001288 " %d seconds\n",
1289 ha->host_no, ddb_entry->bus,
1290 ddb_entry->target,
1291 ddb_entry->fw_ddb_index,
1292 ddb_entry->default_time2wait + 4)
1293 );
1294
1295 atomic_set(&ddb_entry->retry_relogin_timer,
1296 ddb_entry->default_time2wait + 4);
1297 }
1298 }
1299 }
1300
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301301 if (!is_qla8022(ha)) {
1302 /* Check for heartbeat interval. */
1303 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
1304 ha->heartbeat_interval != 0) {
1305 ha->seconds_since_last_heartbeat++;
1306 if (ha->seconds_since_last_heartbeat >
1307 ha->heartbeat_interval + 2)
1308 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1309 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001310 }
1311
David Somayajuluafaf5a22006-09-19 10:28:00 -07001312 /* Wakeup the dpc routine for this adapter, if needed. */
Lalit Chandivade1b468072011-05-17 23:17:09 -07001313 if (start_dpc ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07001314 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1315 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
1316 test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301317 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07001318 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1319 test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301320 test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301321 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
1322 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
Lalit Chandivade1b468072011-05-17 23:17:09 -07001323 test_bit(DPC_AEN, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001324 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
1325 " - dpc flags = 0x%lx\n",
1326 ha->host_no, __func__, ha->dpc_flags));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301327 qla4xxx_wake_dpc(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001328 }
1329
1330 /* Reschedule timer thread to call us back in one second */
1331 mod_timer(&ha->timer, jiffies + HZ);
1332
1333 DEBUG2(ha->seconds_since_last_intr++);
1334}
1335
1336/**
1337 * qla4xxx_cmd_wait - waits for all outstanding commands to complete
1338 * @ha: Pointer to host adapter structure.
1339 *
1340 * This routine stalls the driver until all outstanding commands are returned.
1341 * Caller must release the Hardware Lock prior to calling this routine.
1342 **/
1343static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
1344{
1345 uint32_t index = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001346 unsigned long flags;
1347 struct scsi_cmnd *cmd;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001348
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301349 unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
1350
1351 DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
1352 "complete\n", WAIT_CMD_TOV));
1353
1354 while (!time_after_eq(jiffies, wtime)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001355 spin_lock_irqsave(&ha->hardware_lock, flags);
1356 /* Find a command that hasn't completed. */
1357 for (index = 0; index < ha->host->can_queue; index++) {
1358 cmd = scsi_host_find_tag(ha->host, index);
Mike Christiea1e00632010-10-26 05:45:30 -07001359 /*
1360 * We cannot just check if the index is valid,
1361 * becase if we are run from the scsi eh, then
1362 * the scsi/block layer is going to prevent
1363 * the tag from being released.
1364 */
1365 if (cmd != NULL && CMD_SP(cmd))
David Somayajuluafaf5a22006-09-19 10:28:00 -07001366 break;
1367 }
1368 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1369
1370 /* If No Commands are pending, wait is complete */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301371 if (index == ha->host->can_queue)
1372 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001373
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301374 msleep(1000);
1375 }
1376 /* If we timed out on waiting for commands to come back
1377 * return ERROR. */
1378 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001379}
1380
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301381int qla4xxx_hw_reset(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001382{
David Somayajuluafaf5a22006-09-19 10:28:00 -07001383 uint32_t ctrl_status;
David C Somayajulu477ffb92007-01-22 12:26:11 -08001384 unsigned long flags = 0;
1385
1386 DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001387
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301388 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
1389 return QLA_ERROR;
1390
David Somayajuluafaf5a22006-09-19 10:28:00 -07001391 spin_lock_irqsave(&ha->hardware_lock, flags);
1392
1393 /*
1394 * If the SCSI Reset Interrupt bit is set, clear it.
1395 * Otherwise, the Soft Reset won't work.
1396 */
1397 ctrl_status = readw(&ha->reg->ctrl_status);
1398 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
1399 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
1400
1401 /* Issue Soft Reset */
1402 writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
1403 readl(&ha->reg->ctrl_status);
1404
1405 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301406 return QLA_SUCCESS;
David C Somayajulu477ffb92007-01-22 12:26:11 -08001407}
1408
1409/**
1410 * qla4xxx_soft_reset - performs soft reset.
1411 * @ha: Pointer to host adapter structure.
1412 **/
1413int qla4xxx_soft_reset(struct scsi_qla_host *ha)
1414{
1415 uint32_t max_wait_time;
1416 unsigned long flags = 0;
Vikas Chaudharyf931c532010-10-06 22:48:07 -07001417 int status;
David C Somayajulu477ffb92007-01-22 12:26:11 -08001418 uint32_t ctrl_status;
1419
Vikas Chaudharyf931c532010-10-06 22:48:07 -07001420 status = qla4xxx_hw_reset(ha);
1421 if (status != QLA_SUCCESS)
1422 return status;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001423
Vikas Chaudharyf931c532010-10-06 22:48:07 -07001424 status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001425 /* Wait until the Network Reset Intr bit is cleared */
1426 max_wait_time = RESET_INTR_TOV;
1427 do {
1428 spin_lock_irqsave(&ha->hardware_lock, flags);
1429 ctrl_status = readw(&ha->reg->ctrl_status);
1430 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1431
1432 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
1433 break;
1434
1435 msleep(1000);
1436 } while ((--max_wait_time));
1437
1438 if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
1439 DEBUG2(printk(KERN_WARNING
1440 "scsi%ld: Network Reset Intr not cleared by "
1441 "Network function, clearing it now!\n",
1442 ha->host_no));
1443 spin_lock_irqsave(&ha->hardware_lock, flags);
1444 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
1445 readl(&ha->reg->ctrl_status);
1446 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1447 }
1448
1449 /* Wait until the firmware tells us the Soft Reset is done */
1450 max_wait_time = SOFT_RESET_TOV;
1451 do {
1452 spin_lock_irqsave(&ha->hardware_lock, flags);
1453 ctrl_status = readw(&ha->reg->ctrl_status);
1454 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1455
1456 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
1457 status = QLA_SUCCESS;
1458 break;
1459 }
1460
1461 msleep(1000);
1462 } while ((--max_wait_time));
1463
1464 /*
1465 * Also, make sure that the SCSI Reset Interrupt bit has been cleared
1466 * after the soft reset has taken place.
1467 */
1468 spin_lock_irqsave(&ha->hardware_lock, flags);
1469 ctrl_status = readw(&ha->reg->ctrl_status);
1470 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
1471 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
1472 readl(&ha->reg->ctrl_status);
1473 }
1474 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1475
1476 /* If soft reset fails then most probably the bios on other
1477 * function is also enabled.
1478 * Since the initialization is sequential the other fn
1479 * wont be able to acknowledge the soft reset.
1480 * Issue a force soft reset to workaround this scenario.
1481 */
1482 if (max_wait_time == 0) {
1483 /* Issue Force Soft Reset */
1484 spin_lock_irqsave(&ha->hardware_lock, flags);
1485 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
1486 readl(&ha->reg->ctrl_status);
1487 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1488 /* Wait until the firmware tells us the Soft Reset is done */
1489 max_wait_time = SOFT_RESET_TOV;
1490 do {
1491 spin_lock_irqsave(&ha->hardware_lock, flags);
1492 ctrl_status = readw(&ha->reg->ctrl_status);
1493 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1494
1495 if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
1496 status = QLA_SUCCESS;
1497 break;
1498 }
1499
1500 msleep(1000);
1501 } while ((--max_wait_time));
1502 }
1503
1504 return status;
1505}
1506
1507/**
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301508 * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001509 * @ha: Pointer to host adapter structure.
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301510 * @res: returned scsi status
David Somayajuluafaf5a22006-09-19 10:28:00 -07001511 *
1512 * This routine is called just prior to a HARD RESET to return all
1513 * outstanding commands back to the Operating System.
1514 * Caller should make sure that the following locks are released
1515 * before this calling routine: Hardware lock, and io_request_lock.
1516 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301517static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001518{
1519 struct srb *srb;
1520 int i;
1521 unsigned long flags;
1522
1523 spin_lock_irqsave(&ha->hardware_lock, flags);
1524 for (i = 0; i < ha->host->can_queue; i++) {
1525 srb = qla4xxx_del_from_active_array(ha, i);
1526 if (srb != NULL) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301527 srb->cmd->result = res;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301528 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001529 }
1530 }
1531 spin_unlock_irqrestore(&ha->hardware_lock, flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001532}
1533
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301534void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
1535{
1536 clear_bit(AF_ONLINE, &ha->flags);
1537
1538 /* Disable the board */
1539 ql4_printk(KERN_INFO, ha, "Disabling the board\n");
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301540
1541 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
1542 qla4xxx_mark_all_devices_missing(ha);
1543 clear_bit(AF_INIT_DONE, &ha->flags);
1544}
1545
David Somayajuluafaf5a22006-09-19 10:28:00 -07001546/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07001547 * qla4xxx_recover_adapter - recovers adapter after a fatal error
1548 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001549 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301550static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001551{
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301552 int status = QLA_ERROR;
1553 uint8_t reset_chip = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001554
1555 /* Stall incoming I/O until we are done */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301556 scsi_block_requests(ha->host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001557 clear_bit(AF_ONLINE, &ha->flags);
Mike Christie50a29ae2008-03-04 13:26:53 -06001558
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301559 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001560
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301561 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001562
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301563 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
1564 reset_chip = 1;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001565
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301566 /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
1567 * do not reset adapter, jump to initialize_adapter */
1568 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1569 status = QLA_SUCCESS;
1570 goto recover_ha_init_adapter;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001571 }
1572
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301573 /* For the ISP-82xx adapter, issue a stop_firmware if invoked
1574 * from eh_host_reset or ioctl module */
1575 if (is_qla8022(ha) && !reset_chip &&
1576 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
1577
1578 DEBUG2(ql4_printk(KERN_INFO, ha,
1579 "scsi%ld: %s - Performing stop_firmware...\n",
1580 ha->host_no, __func__));
1581 status = ha->isp_ops->reset_firmware(ha);
1582 if (status == QLA_SUCCESS) {
Nilesh Javali2bd1e2b2010-10-06 22:49:20 -07001583 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
1584 qla4xxx_cmd_wait(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301585 ha->isp_ops->disable_intrs(ha);
1586 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1587 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1588 } else {
1589 /* If the stop_firmware fails then
1590 * reset the entire chip */
1591 reset_chip = 1;
1592 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1593 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1594 }
1595 }
1596
1597 /* Issue full chip reset if recovering from a catastrophic error,
1598 * or if stop_firmware fails for ISP-82xx.
1599 * This is the default case for ISP-4xxx */
1600 if (!is_qla8022(ha) || reset_chip) {
Nilesh Javali2bd1e2b2010-10-06 22:49:20 -07001601 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
1602 qla4xxx_cmd_wait(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301603 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1604 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1605 DEBUG2(ql4_printk(KERN_INFO, ha,
1606 "scsi%ld: %s - Performing chip reset..\n",
1607 ha->host_no, __func__));
1608 status = ha->isp_ops->reset_chip(ha);
1609 }
1610
1611 /* Flush any pending ddb changed AENs */
1612 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1613
1614recover_ha_init_adapter:
1615 /* Upon successful firmware/chip reset, re-initialize the adapter */
1616 if (status == QLA_SUCCESS) {
1617 /* For ISP-4xxx, force function 1 to always initialize
1618 * before function 3 to prevent both funcions from
1619 * stepping on top of the other */
1620 if (!is_qla8022(ha) && (ha->mac_index == 3))
1621 ssleep(6);
1622
1623 /* NOTE: AF_ONLINE flag set upon successful completion of
1624 * qla4xxx_initialize_adapter */
1625 status = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
1626 }
1627
1628 /* Retry failed adapter initialization, if necessary
1629 * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
1630 * case to prevent ping-pong resets between functions */
1631 if (!test_bit(AF_ONLINE, &ha->flags) &&
1632 !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001633 /* Adapter initialization failed, see if we can retry
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301634 * resetting the ha.
1635 * Since we don't want to block the DPC for too long
1636 * with multiple resets in the same thread,
1637 * utilize DPC to retry */
David Somayajuluafaf5a22006-09-19 10:28:00 -07001638 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
1639 ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
1640 DEBUG2(printk("scsi%ld: recover adapter - retrying "
1641 "(%d) more times\n", ha->host_no,
1642 ha->retry_reset_ha_cnt));
1643 set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1644 status = QLA_ERROR;
1645 } else {
1646 if (ha->retry_reset_ha_cnt > 0) {
1647 /* Schedule another Reset HA--DPC will retry */
1648 ha->retry_reset_ha_cnt--;
1649 DEBUG2(printk("scsi%ld: recover adapter - "
1650 "retry remaining %d\n",
1651 ha->host_no,
1652 ha->retry_reset_ha_cnt));
1653 status = QLA_ERROR;
1654 }
1655
1656 if (ha->retry_reset_ha_cnt == 0) {
1657 /* Recover adapter retries have been exhausted.
1658 * Adapter DEAD */
1659 DEBUG2(printk("scsi%ld: recover adapter "
1660 "failed - board disabled\n",
1661 ha->host_no));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301662 qla4xxx_dead_adapter_cleanup(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001663 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1664 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301665 clear_bit(DPC_RESET_HA_FW_CONTEXT,
David Somayajuluafaf5a22006-09-19 10:28:00 -07001666 &ha->dpc_flags);
1667 status = QLA_ERROR;
1668 }
1669 }
1670 } else {
1671 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301672 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001673 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1674 }
1675
1676 ha->adapter_error_count++;
1677
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301678 if (test_bit(AF_ONLINE, &ha->flags))
1679 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001680
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301681 scsi_unblock_requests(ha->host);
1682
1683 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
1684 DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001685 status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301686
David Somayajuluafaf5a22006-09-19 10:28:00 -07001687 return status;
1688}
1689
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07001690static void qla4xxx_relogin_all_devices(struct scsi_qla_host *ha)
1691{
1692 struct ddb_entry *ddb_entry, *dtemp;
1693
1694 list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
1695 if ((atomic_read(&ddb_entry->state) == DDB_STATE_MISSING) ||
1696 (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD)) {
1697 if (ddb_entry->fw_ddb_device_state ==
1698 DDB_DS_SESSION_ACTIVE) {
1699 atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
1700 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
1701 " marked ONLINE\n", ha->host_no, __func__,
1702 ddb_entry->fw_ddb_index);
1703
1704 iscsi_unblock_session(ddb_entry->sess);
1705 } else
1706 qla4xxx_relogin_device(ha, ddb_entry);
1707 }
1708 }
1709}
1710
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301711void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
1712{
Lalit Chandivade1b468072011-05-17 23:17:09 -07001713 if (ha->dpc_thread)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301714 queue_work(ha->dpc_thread, &ha->dpc_work);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301715}
1716
David Somayajuluafaf5a22006-09-19 10:28:00 -07001717/**
1718 * qla4xxx_do_dpc - dpc routine
1719 * @data: in our case pointer to adapter structure
1720 *
1721 * This routine is a task that is schedule by the interrupt handler
1722 * to perform the background processing for interrupts. We put it
1723 * on a task queue that is consumed whenever the scheduler runs; that's
1724 * so you can do anything (i.e. put the process to sleep etc). In fact,
1725 * the mid-level tries to sleep when it reaches the driver threshold
1726 * "host->can_queue". This can cause a panic if we were in our interrupt code.
1727 **/
David Howellsc4028952006-11-22 14:57:56 +00001728static void qla4xxx_do_dpc(struct work_struct *work)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001729{
David Howellsc4028952006-11-22 14:57:56 +00001730 struct scsi_qla_host *ha =
1731 container_of(work, struct scsi_qla_host, dpc_work);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001732 struct ddb_entry *ddb_entry, *dtemp;
David C Somayajulu477ffb92007-01-22 12:26:11 -08001733 int status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001734
David C Somayajuluf26b9042006-11-15 16:41:09 -08001735 DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301736 "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
1737 ha->host_no, __func__, ha->flags, ha->dpc_flags))
David Somayajuluafaf5a22006-09-19 10:28:00 -07001738
1739 /* Initialization not yet finished. Don't do anything yet. */
1740 if (!test_bit(AF_INIT_DONE, &ha->flags))
Lalit Chandivade1b468072011-05-17 23:17:09 -07001741 return;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001742
Lalit Chandivade2232be02010-07-30 14:38:47 +05301743 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1744 DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
1745 ha->host_no, __func__, ha->flags));
Lalit Chandivade1b468072011-05-17 23:17:09 -07001746 return;
Lalit Chandivade2232be02010-07-30 14:38:47 +05301747 }
1748
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301749 if (is_qla8022(ha)) {
1750 if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
1751 qla4_8xxx_idc_lock(ha);
1752 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
1753 QLA82XX_DEV_FAILED);
1754 qla4_8xxx_idc_unlock(ha);
1755 ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
1756 qla4_8xxx_device_state_handler(ha);
1757 }
1758 if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
1759 qla4_8xxx_need_qsnt_handler(ha);
1760 }
1761 }
1762
1763 if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
1764 (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07001765 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301766 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
1767 if (ql4xdontresethba) {
1768 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
1769 ha->host_no, __func__));
1770 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
1771 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1772 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1773 goto dpc_post_reset_ha;
1774 }
1775 if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
1776 test_bit(DPC_RESET_HA, &ha->dpc_flags))
1777 qla4xxx_recover_adapter(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001778
David C Somayajulu477ffb92007-01-22 12:26:11 -08001779 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001780 uint8_t wait_time = RESET_INTR_TOV;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001781
David Somayajuluafaf5a22006-09-19 10:28:00 -07001782 while ((readw(&ha->reg->ctrl_status) &
1783 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
1784 if (--wait_time == 0)
1785 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001786 msleep(1000);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001787 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001788 if (wait_time == 0)
1789 DEBUG2(printk("scsi%ld: %s: SR|FSR "
1790 "bit not cleared-- resetting\n",
1791 ha->host_no, __func__));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301792 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
David C Somayajulu477ffb92007-01-22 12:26:11 -08001793 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
1794 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301795 status = qla4xxx_recover_adapter(ha);
David C Somayajulu477ffb92007-01-22 12:26:11 -08001796 }
1797 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1798 if (status == QLA_SUCCESS)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301799 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001800 }
1801 }
1802
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301803dpc_post_reset_ha:
David Somayajuluafaf5a22006-09-19 10:28:00 -07001804 /* ---- process AEN? --- */
1805 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
1806 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
1807
1808 /* ---- Get DHCP IP Address? --- */
1809 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
1810 qla4xxx_get_dhcp_ip_address(ha);
1811
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301812 /* ---- link change? --- */
1813 if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
1814 if (!test_bit(AF_LINK_UP, &ha->flags)) {
1815 /* ---- link down? --- */
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07001816 qla4xxx_mark_all_devices_missing(ha);
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301817 } else {
1818 /* ---- link up? --- *
1819 * F/W will auto login to all devices ONLY ONCE after
1820 * link up during driver initialization and runtime
1821 * fatal error recovery. Therefore, the driver must
1822 * manually relogin to devices when recovering from
1823 * connection failures, logouts, expired KATO, etc. */
1824
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07001825 qla4xxx_relogin_all_devices(ha);
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301826 }
1827 }
1828
David Somayajuluafaf5a22006-09-19 10:28:00 -07001829 /* ---- relogin device? --- */
1830 if (adapter_up(ha) &&
1831 test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
1832 list_for_each_entry_safe(ddb_entry, dtemp,
1833 &ha->ddb_list, list) {
1834 if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
1835 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
1836 qla4xxx_relogin_device(ha, ddb_entry);
1837
1838 /*
1839 * If mbx cmd times out there is no point
1840 * in continuing further.
1841 * With large no of targets this can hang
1842 * the system.
1843 */
1844 if (test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
1845 printk(KERN_WARNING "scsi%ld: %s: "
1846 "need to reset hba\n",
1847 ha->host_no, __func__);
1848 break;
1849 }
1850 }
1851 }
Vikas Chaudhary0753b482010-07-30 14:27:19 +05301852
David Somayajuluafaf5a22006-09-19 10:28:00 -07001853}
1854
1855/**
1856 * qla4xxx_free_adapter - release the adapter
1857 * @ha: pointer to adapter structure
1858 **/
1859static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
1860{
1861
1862 if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
1863 /* Turn-off interrupts on the card. */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301864 ha->isp_ops->disable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001865 }
1866
David Somayajuluafaf5a22006-09-19 10:28:00 -07001867 /* Remove timer thread, if present */
1868 if (ha->timer_active)
1869 qla4xxx_stop_timer(ha);
1870
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301871 /* Kill the kernel thread for this host */
1872 if (ha->dpc_thread)
1873 destroy_workqueue(ha->dpc_thread);
1874
1875 /* Put firmware in known state */
1876 ha->isp_ops->reset_firmware(ha);
1877
1878 if (is_qla8022(ha)) {
1879 qla4_8xxx_idc_lock(ha);
1880 qla4_8xxx_clear_drv_active(ha);
1881 qla4_8xxx_idc_unlock(ha);
1882 }
1883
David Somayajuluafaf5a22006-09-19 10:28:00 -07001884 /* Detach interrupts */
1885 if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301886 qla4xxx_free_irqs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001887
David C Somayajulubee4fe82007-05-23 18:03:32 -07001888 /* free extra memory */
1889 qla4xxx_mem_free(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301890}
David C Somayajulubee4fe82007-05-23 18:03:32 -07001891
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301892int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
1893{
1894 int status = 0;
1895 uint8_t revision_id;
1896 unsigned long mem_base, mem_len, db_base, db_len;
1897 struct pci_dev *pdev = ha->pdev;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001898
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301899 status = pci_request_regions(pdev, DRIVER_NAME);
1900 if (status) {
1901 printk(KERN_WARNING
1902 "scsi(%ld) Failed to reserve PIO regions (%s) "
1903 "status=%d\n", ha->host_no, pci_name(pdev), status);
1904 goto iospace_error_exit;
1905 }
1906
1907 pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
1908 DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
1909 __func__, revision_id));
1910 ha->revision_id = revision_id;
1911
1912 /* remap phys address */
1913 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
1914 mem_len = pci_resource_len(pdev, 0);
1915 DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
1916 __func__, mem_base, mem_len));
1917
1918 /* mapping of pcibase pointer */
1919 ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
1920 if (!ha->nx_pcibase) {
1921 printk(KERN_ERR
1922 "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
1923 pci_release_regions(ha->pdev);
1924 goto iospace_error_exit;
1925 }
1926
1927 /* Mapping of IO base pointer, door bell read and write pointer */
1928
1929 /* mapping of IO base pointer */
1930 ha->qla4_8xxx_reg =
1931 (struct device_reg_82xx __iomem *)((uint8_t *)ha->nx_pcibase +
1932 0xbc000 + (ha->pdev->devfn << 11));
1933
1934 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
1935 db_len = pci_resource_len(pdev, 4);
1936
Shyam Sundar2657c802010-10-06 22:50:29 -07001937 ha->nx_db_wr_ptr = (ha->pdev->devfn == 4 ? QLA82XX_CAM_RAM_DB1 :
1938 QLA82XX_CAM_RAM_DB2);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301939
Shyam Sundar2657c802010-10-06 22:50:29 -07001940 return 0;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301941iospace_error_exit:
1942 return -ENOMEM;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001943}
1944
1945/***
1946 * qla4xxx_iospace_config - maps registers
1947 * @ha: pointer to adapter structure
1948 *
1949 * This routines maps HBA's registers from the pci address space
1950 * into the kernel virtual address space for memory mapped i/o.
1951 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301952int qla4xxx_iospace_config(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001953{
1954 unsigned long pio, pio_len, pio_flags;
1955 unsigned long mmio, mmio_len, mmio_flags;
1956
1957 pio = pci_resource_start(ha->pdev, 0);
1958 pio_len = pci_resource_len(ha->pdev, 0);
1959 pio_flags = pci_resource_flags(ha->pdev, 0);
1960 if (pio_flags & IORESOURCE_IO) {
1961 if (pio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301962 ql4_printk(KERN_WARNING, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07001963 "Invalid PCI I/O region size\n");
1964 pio = 0;
1965 }
1966 } else {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301967 ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001968 pio = 0;
1969 }
1970
1971 /* Use MMIO operations for all accesses. */
1972 mmio = pci_resource_start(ha->pdev, 1);
1973 mmio_len = pci_resource_len(ha->pdev, 1);
1974 mmio_flags = pci_resource_flags(ha->pdev, 1);
1975
1976 if (!(mmio_flags & IORESOURCE_MEM)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301977 ql4_printk(KERN_ERR, ha,
1978 "region #0 not an MMIO resource, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001979
1980 goto iospace_error_exit;
1981 }
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301982
David Somayajuluafaf5a22006-09-19 10:28:00 -07001983 if (mmio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301984 ql4_printk(KERN_ERR, ha,
1985 "Invalid PCI mem region size, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001986 goto iospace_error_exit;
1987 }
1988
1989 if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301990 ql4_printk(KERN_WARNING, ha,
1991 "Failed to reserve PIO/MMIO regions\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001992
1993 goto iospace_error_exit;
1994 }
1995
1996 ha->pio_address = pio;
1997 ha->pio_length = pio_len;
1998 ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
1999 if (!ha->reg) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302000 ql4_printk(KERN_ERR, ha,
2001 "cannot remap MMIO, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002002
2003 goto iospace_error_exit;
2004 }
2005
2006 return 0;
2007
2008iospace_error_exit:
2009 return -ENOMEM;
2010}
2011
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302012static struct isp_operations qla4xxx_isp_ops = {
2013 .iospace_config = qla4xxx_iospace_config,
2014 .pci_config = qla4xxx_pci_config,
2015 .disable_intrs = qla4xxx_disable_intrs,
2016 .enable_intrs = qla4xxx_enable_intrs,
2017 .start_firmware = qla4xxx_start_firmware,
2018 .intr_handler = qla4xxx_intr_handler,
2019 .interrupt_service_routine = qla4xxx_interrupt_service_routine,
2020 .reset_chip = qla4xxx_soft_reset,
2021 .reset_firmware = qla4xxx_hw_reset,
2022 .queue_iocb = qla4xxx_queue_iocb,
2023 .complete_iocb = qla4xxx_complete_iocb,
2024 .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
2025 .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
2026 .get_sys_info = qla4xxx_get_sys_info,
2027};
2028
2029static struct isp_operations qla4_8xxx_isp_ops = {
2030 .iospace_config = qla4_8xxx_iospace_config,
2031 .pci_config = qla4_8xxx_pci_config,
2032 .disable_intrs = qla4_8xxx_disable_intrs,
2033 .enable_intrs = qla4_8xxx_enable_intrs,
2034 .start_firmware = qla4_8xxx_load_risc,
2035 .intr_handler = qla4_8xxx_intr_handler,
2036 .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
2037 .reset_chip = qla4_8xxx_isp_reset,
2038 .reset_firmware = qla4_8xxx_stop_firmware,
2039 .queue_iocb = qla4_8xxx_queue_iocb,
2040 .complete_iocb = qla4_8xxx_complete_iocb,
2041 .rd_shdw_req_q_out = qla4_8xxx_rd_shdw_req_q_out,
2042 .rd_shdw_rsp_q_in = qla4_8xxx_rd_shdw_rsp_q_in,
2043 .get_sys_info = qla4_8xxx_get_sys_info,
2044};
2045
2046uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
2047{
2048 return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
2049}
2050
2051uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
2052{
2053 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
2054}
2055
2056uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
2057{
2058 return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
2059}
2060
2061uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
2062{
2063 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
2064}
2065
David Somayajuluafaf5a22006-09-19 10:28:00 -07002066/**
2067 * qla4xxx_probe_adapter - callback function to probe HBA
2068 * @pdev: pointer to pci_dev structure
2069 * @pci_device_id: pointer to pci_device entry
2070 *
2071 * This routine will probe for Qlogic 4xxx iSCSI host adapters.
2072 * It returns zero if successful. It also initializes all data necessary for
2073 * the driver.
2074 **/
2075static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
2076 const struct pci_device_id *ent)
2077{
2078 int ret = -ENODEV, status;
2079 struct Scsi_Host *host;
2080 struct scsi_qla_host *ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002081 uint8_t init_retry_count = 0;
2082 char buf[34];
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302083 struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
Prasanna Mumbaif9880e72011-03-21 03:34:26 -07002084 uint32_t dev_state;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002085
2086 if (pci_enable_device(pdev))
2087 return -1;
2088
2089 host = scsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha));
2090 if (host == NULL) {
2091 printk(KERN_WARNING
2092 "qla4xxx: Couldn't allocate host from scsi layer!\n");
2093 goto probe_disable_device;
2094 }
2095
2096 /* Clear our data area */
2097 ha = (struct scsi_qla_host *) host->hostdata;
2098 memset(ha, 0, sizeof(*ha));
2099
2100 /* Save the information from PCI BIOS. */
2101 ha->pdev = pdev;
2102 ha->host = host;
2103 ha->host_no = host->host_no;
2104
Lalit Chandivade2232be02010-07-30 14:38:47 +05302105 pci_enable_pcie_error_reporting(pdev);
2106
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302107 /* Setup Runtime configurable options */
2108 if (is_qla8022(ha)) {
2109 ha->isp_ops = &qla4_8xxx_isp_ops;
2110 rwlock_init(&ha->hw_lock);
2111 ha->qdr_sn_window = -1;
2112 ha->ddr_mn_window = -1;
2113 ha->curr_window = 255;
2114 ha->func_num = PCI_FUNC(ha->pdev->devfn);
2115 nx_legacy_intr = &legacy_intr[ha->func_num];
2116 ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
2117 ha->nx_legacy_intr.tgt_status_reg =
2118 nx_legacy_intr->tgt_status_reg;
2119 ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
2120 ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
2121 } else {
2122 ha->isp_ops = &qla4xxx_isp_ops;
2123 }
2124
Lalit Chandivade2232be02010-07-30 14:38:47 +05302125 /* Set EEH reset type to fundamental if required by hba */
2126 if (is_qla8022(ha))
2127 pdev->needs_freset = 1;
2128
David Somayajuluafaf5a22006-09-19 10:28:00 -07002129 /* Configure PCI I/O space. */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302130 ret = ha->isp_ops->iospace_config(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002131 if (ret)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302132 goto probe_failed_ioconfig;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002133
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302134 ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
David Somayajuluafaf5a22006-09-19 10:28:00 -07002135 pdev->device, pdev->irq, ha->reg);
2136
2137 qla4xxx_config_dma_addressing(ha);
2138
2139 /* Initialize lists and spinlocks. */
2140 INIT_LIST_HEAD(&ha->ddb_list);
2141 INIT_LIST_HEAD(&ha->free_srb_q);
2142
2143 mutex_init(&ha->mbox_sem);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302144 init_completion(&ha->mbx_intr_comp);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002145
2146 spin_lock_init(&ha->hardware_lock);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002147
2148 /* Allocate dma buffers */
2149 if (qla4xxx_mem_alloc(ha)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302150 ql4_printk(KERN_WARNING, ha,
2151 "[ERROR] Failed to allocate memory for adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002152
2153 ret = -ENOMEM;
2154 goto probe_failed;
2155 }
2156
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302157 if (is_qla8022(ha))
2158 (void) qla4_8xxx_get_flash_info(ha);
2159
David Somayajuluafaf5a22006-09-19 10:28:00 -07002160 /*
2161 * Initialize the Host adapter request/response queues and
2162 * firmware
2163 * NOTE: interrupts enabled upon successful completion
2164 */
2165 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302166 while ((!test_bit(AF_ONLINE, &ha->flags)) &&
2167 init_retry_count++ < MAX_INIT_RETRIES) {
Prasanna Mumbaif9880e72011-03-21 03:34:26 -07002168
2169 if (is_qla8022(ha)) {
2170 qla4_8xxx_idc_lock(ha);
2171 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
2172 qla4_8xxx_idc_unlock(ha);
2173 if (dev_state == QLA82XX_DEV_FAILED) {
2174 ql4_printk(KERN_WARNING, ha, "%s: don't retry "
2175 "initialize adapter. H/W is in failed state\n",
2176 __func__);
2177 break;
2178 }
2179 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002180 DEBUG2(printk("scsi: %s: retrying adapter initialization "
2181 "(%d)\n", __func__, init_retry_count));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302182
2183 if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
2184 continue;
2185
David Somayajuluafaf5a22006-09-19 10:28:00 -07002186 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
2187 }
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302188
2189 if (!test_bit(AF_ONLINE, &ha->flags)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302190 ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002191
Lalit Chandivadefe998522010-12-02 22:12:36 -08002192 if (is_qla8022(ha) && ql4xdontresethba) {
2193 /* Put the device in failed state. */
2194 DEBUG2(printk(KERN_ERR "HW STATE: FAILED\n"));
2195 qla4_8xxx_idc_lock(ha);
2196 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2197 QLA82XX_DEV_FAILED);
2198 qla4_8xxx_idc_unlock(ha);
2199 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002200 ret = -ENODEV;
2201 goto probe_failed;
2202 }
2203
2204 host->cmd_per_lun = 3;
2205 host->max_channel = 0;
2206 host->max_lun = MAX_LUNS - 1;
2207 host->max_id = MAX_TARGETS;
2208 host->max_cmd_len = IOCB_MAX_CDB_LEN;
2209 host->can_queue = MAX_SRBS ;
2210 host->transportt = qla4xxx_scsi_transport;
2211
2212 ret = scsi_init_shared_tag_map(host, MAX_SRBS);
2213 if (ret) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302214 ql4_printk(KERN_WARNING, ha,
2215 "scsi_init_shared_tag_map failed\n");
2216 goto probe_failed;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002217 }
2218
2219 /* Startup the kernel thread for this host adapter. */
2220 DEBUG2(printk("scsi: %s: Starting kernel thread for "
2221 "qla4xxx_dpc\n", __func__));
2222 sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
2223 ha->dpc_thread = create_singlethread_workqueue(buf);
2224 if (!ha->dpc_thread) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302225 ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002226 ret = -ENODEV;
2227 goto probe_failed;
2228 }
David Howellsc4028952006-11-22 14:57:56 +00002229 INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002230
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302231 /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
2232 * (which is called indirectly by qla4xxx_initialize_adapter),
2233 * so that irqs will be registered after crbinit but before
2234 * mbx_intr_enable.
2235 */
2236 if (!is_qla8022(ha)) {
2237 ret = qla4xxx_request_irqs(ha);
2238 if (ret) {
2239 ql4_printk(KERN_WARNING, ha, "Failed to reserve "
2240 "interrupt %d already in use.\n", pdev->irq);
2241 goto probe_failed;
2242 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002243 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002244
Lalit Chandivade2232be02010-07-30 14:38:47 +05302245 pci_save_state(ha->pdev);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302246 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002247
2248 /* Start timer thread. */
2249 qla4xxx_start_timer(ha, qla4xxx_timer, 1);
2250
2251 set_bit(AF_INIT_DONE, &ha->flags);
2252
2253 pci_set_drvdata(pdev, ha);
2254
2255 ret = scsi_add_host(host, &pdev->dev);
2256 if (ret)
2257 goto probe_failed;
2258
David Somayajuluafaf5a22006-09-19 10:28:00 -07002259 printk(KERN_INFO
2260 " QLogic iSCSI HBA Driver version: %s\n"
2261 " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
2262 qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
2263 ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
2264 ha->patch_number, ha->build_number);
Mike Christie921601b2008-01-31 13:36:49 -06002265 scsi_scan_host(host);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05002266
2267 qla4xxx_create_ifaces(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002268 return 0;
2269
David Somayajuluafaf5a22006-09-19 10:28:00 -07002270probe_failed:
2271 qla4xxx_free_adapter(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302272
2273probe_failed_ioconfig:
Lalit Chandivade2232be02010-07-30 14:38:47 +05302274 pci_disable_pcie_error_reporting(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002275 scsi_host_put(ha->host);
2276
2277probe_disable_device:
2278 pci_disable_device(pdev);
2279
2280 return ret;
2281}
2282
2283/**
Karen Higgins7eece5a2011-03-21 03:34:29 -07002284 * qla4xxx_prevent_other_port_reinit - prevent other port from re-initialize
2285 * @ha: pointer to adapter structure
2286 *
2287 * Mark the other ISP-4xxx port to indicate that the driver is being removed,
2288 * so that the other port will not re-initialize while in the process of
2289 * removing the ha due to driver unload or hba hotplug.
2290 **/
2291static void qla4xxx_prevent_other_port_reinit(struct scsi_qla_host *ha)
2292{
2293 struct scsi_qla_host *other_ha = NULL;
2294 struct pci_dev *other_pdev = NULL;
2295 int fn = ISP4XXX_PCI_FN_2;
2296
2297 /*iscsi function numbers for ISP4xxx is 1 and 3*/
2298 if (PCI_FUNC(ha->pdev->devfn) & BIT_1)
2299 fn = ISP4XXX_PCI_FN_1;
2300
2301 other_pdev =
2302 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
2303 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
2304 fn));
2305
2306 /* Get other_ha if other_pdev is valid and state is enable*/
2307 if (other_pdev) {
2308 if (atomic_read(&other_pdev->enable_cnt)) {
2309 other_ha = pci_get_drvdata(other_pdev);
2310 if (other_ha) {
2311 set_bit(AF_HA_REMOVAL, &other_ha->flags);
2312 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: "
2313 "Prevent %s reinit\n", __func__,
2314 dev_name(&other_ha->pdev->dev)));
2315 }
2316 }
2317 pci_dev_put(other_pdev);
2318 }
2319}
2320
2321/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002322 * qla4xxx_remove_adapter - calback function to remove adapter.
2323 * @pci_dev: PCI device pointer
2324 **/
2325static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
2326{
2327 struct scsi_qla_host *ha;
2328
2329 ha = pci_get_drvdata(pdev);
2330
Karen Higgins7eece5a2011-03-21 03:34:29 -07002331 if (!is_qla8022(ha))
2332 qla4xxx_prevent_other_port_reinit(ha);
David C Somayajulubee4fe82007-05-23 18:03:32 -07002333
David Somayajuluafaf5a22006-09-19 10:28:00 -07002334 /* remove devs from iscsi_sessions to scsi_devices */
2335 qla4xxx_free_ddb_list(ha);
2336
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05002337 /* destroy iface from sysfs */
2338 qla4xxx_destroy_ifaces(ha);
2339
David Somayajuluafaf5a22006-09-19 10:28:00 -07002340 scsi_remove_host(ha->host);
2341
2342 qla4xxx_free_adapter(ha);
2343
2344 scsi_host_put(ha->host);
2345
Lalit Chandivade2232be02010-07-30 14:38:47 +05302346 pci_disable_pcie_error_reporting(pdev);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302347 pci_disable_device(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002348 pci_set_drvdata(pdev, NULL);
2349}
2350
2351/**
2352 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
2353 * @ha: HA context
2354 *
2355 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
2356 * supported addressing method.
2357 */
Adrian Bunk47975472007-04-26 00:35:16 -07002358static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002359{
2360 int retval;
2361
2362 /* Update our PCI device dma_mask for full 64 bit mask */
Yang Hongyang6a355282009-04-06 19:01:13 -07002363 if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
2364 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002365 dev_dbg(&ha->pdev->dev,
2366 "Failed to set 64 bit PCI consistent mask; "
2367 "using 32 bit.\n");
2368 retval = pci_set_consistent_dma_mask(ha->pdev,
Yang Hongyang284901a2009-04-06 19:01:15 -07002369 DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07002370 }
2371 } else
Yang Hongyang284901a2009-04-06 19:01:15 -07002372 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07002373}
2374
2375static int qla4xxx_slave_alloc(struct scsi_device *sdev)
2376{
2377 struct iscsi_cls_session *sess = starget_to_session(sdev->sdev_target);
2378 struct ddb_entry *ddb = sess->dd_data;
Vikas Chaudhary8bb40332011-03-21 03:34:31 -07002379 int queue_depth = QL4_DEF_QDEPTH;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002380
2381 sdev->hostdata = ddb;
2382 sdev->tagged_supported = 1;
Vikas Chaudhary8bb40332011-03-21 03:34:31 -07002383
2384 if (ql4xmaxqdepth != 0 && ql4xmaxqdepth <= 0xffffU)
2385 queue_depth = ql4xmaxqdepth;
2386
2387 scsi_activate_tcq(sdev, queue_depth);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002388 return 0;
2389}
2390
2391static int qla4xxx_slave_configure(struct scsi_device *sdev)
2392{
2393 sdev->tagged_supported = 1;
2394 return 0;
2395}
2396
2397static void qla4xxx_slave_destroy(struct scsi_device *sdev)
2398{
2399 scsi_deactivate_tcq(sdev, 1);
2400}
2401
2402/**
2403 * qla4xxx_del_from_active_array - returns an active srb
2404 * @ha: Pointer to host adapter structure.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02002405 * @index: index into the active_array
David Somayajuluafaf5a22006-09-19 10:28:00 -07002406 *
2407 * This routine removes and returns the srb at the specified index
2408 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302409struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
2410 uint32_t index)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002411{
2412 struct srb *srb = NULL;
Vikas Chaudhary53698872010-04-28 11:41:59 +05302413 struct scsi_cmnd *cmd = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002414
Vikas Chaudhary53698872010-04-28 11:41:59 +05302415 cmd = scsi_host_find_tag(ha->host, index);
2416 if (!cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002417 return srb;
2418
Vikas Chaudhary53698872010-04-28 11:41:59 +05302419 srb = (struct srb *)CMD_SP(cmd);
2420 if (!srb)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002421 return srb;
2422
2423 /* update counters */
2424 if (srb->flags & SRB_DMA_VALID) {
2425 ha->req_q_count += srb->iocb_cnt;
2426 ha->iocb_cnt -= srb->iocb_cnt;
2427 if (srb->cmd)
Vikas Chaudhary53698872010-04-28 11:41:59 +05302428 srb->cmd->host_scribble =
2429 (unsigned char *)(unsigned long) MAX_SRBS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002430 }
2431 return srb;
2432}
2433
2434/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002435 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302436 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07002437 * @cmd: Scsi Command to wait on.
2438 *
2439 * This routine waits for the command to be returned by the Firmware
2440 * for some max time.
2441 **/
2442static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
2443 struct scsi_cmnd *cmd)
2444{
2445 int done = 0;
2446 struct srb *rp;
2447 uint32_t max_wait_time = EH_WAIT_CMD_TOV;
Lalit Chandivade2232be02010-07-30 14:38:47 +05302448 int ret = SUCCESS;
2449
2450 /* Dont wait on command if PCI error is being handled
2451 * by PCI AER driver
2452 */
2453 if (unlikely(pci_channel_offline(ha->pdev)) ||
2454 (test_bit(AF_EEH_BUSY, &ha->flags))) {
2455 ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
2456 ha->host_no, __func__);
2457 return ret;
2458 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002459
2460 do {
2461 /* Checking to see if its returned to OS */
Vikas Chaudhary53698872010-04-28 11:41:59 +05302462 rp = (struct srb *) CMD_SP(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002463 if (rp == NULL) {
2464 done++;
2465 break;
2466 }
2467
2468 msleep(2000);
2469 } while (max_wait_time--);
2470
2471 return done;
2472}
2473
2474/**
2475 * qla4xxx_wait_for_hba_online - waits for HBA to come online
2476 * @ha: Pointer to host adapter structure
2477 **/
2478static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
2479{
2480 unsigned long wait_online;
2481
Vikas Chaudharyf581a3f2010-10-06 22:47:48 -07002482 wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002483 while (time_before(jiffies, wait_online)) {
2484
2485 if (adapter_up(ha))
2486 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002487
2488 msleep(2000);
2489 }
2490
2491 return QLA_ERROR;
2492}
2493
2494/**
Mike Christiece545032008-02-29 18:25:20 -06002495 * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02002496 * @ha: pointer to HBA
David Somayajuluafaf5a22006-09-19 10:28:00 -07002497 * @t: target id
2498 * @l: lun id
2499 *
2500 * This function waits for all outstanding commands to a lun to complete. It
2501 * returns 0 if all pending commands are returned and 1 otherwise.
2502 **/
Mike Christiece545032008-02-29 18:25:20 -06002503static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
2504 struct scsi_target *stgt,
2505 struct scsi_device *sdev)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002506{
2507 int cnt;
2508 int status = 0;
2509 struct scsi_cmnd *cmd;
2510
2511 /*
Mike Christiece545032008-02-29 18:25:20 -06002512 * Waiting for all commands for the designated target or dev
2513 * in the active array
David Somayajuluafaf5a22006-09-19 10:28:00 -07002514 */
2515 for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
2516 cmd = scsi_host_find_tag(ha->host, cnt);
Mike Christiece545032008-02-29 18:25:20 -06002517 if (cmd && stgt == scsi_target(cmd->device) &&
2518 (!sdev || sdev == cmd->device)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002519 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
2520 status++;
2521 break;
2522 }
2523 }
2524 }
2525 return status;
2526}
2527
2528/**
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302529 * qla4xxx_eh_abort - callback for abort task.
2530 * @cmd: Pointer to Linux's SCSI command structure
2531 *
2532 * This routine is called by the Linux OS to abort the specified
2533 * command.
2534 **/
2535static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
2536{
2537 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2538 unsigned int id = cmd->device->id;
2539 unsigned int lun = cmd->device->lun;
Mike Christie92b3e5b2010-10-06 22:51:17 -07002540 unsigned long flags;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302541 struct srb *srb = NULL;
2542 int ret = SUCCESS;
2543 int wait = 0;
2544
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302545 ql4_printk(KERN_INFO, ha,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002546 "scsi%ld:%d:%d: Abort command issued cmd=%p\n",
2547 ha->host_no, id, lun, cmd);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302548
Mike Christie92b3e5b2010-10-06 22:51:17 -07002549 spin_lock_irqsave(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302550 srb = (struct srb *) CMD_SP(cmd);
Mike Christie92b3e5b2010-10-06 22:51:17 -07002551 if (!srb) {
2552 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302553 return SUCCESS;
Mike Christie92b3e5b2010-10-06 22:51:17 -07002554 }
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302555 kref_get(&srb->srb_ref);
Mike Christie92b3e5b2010-10-06 22:51:17 -07002556 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302557
2558 if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
2559 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
2560 ha->host_no, id, lun));
2561 ret = FAILED;
2562 } else {
2563 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
2564 ha->host_no, id, lun));
2565 wait = 1;
2566 }
2567
2568 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
2569
2570 /* Wait for command to complete */
2571 if (wait) {
2572 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
2573 DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
2574 ha->host_no, id, lun));
2575 ret = FAILED;
2576 }
2577 }
2578
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302579 ql4_printk(KERN_INFO, ha,
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302580 "scsi%ld:%d:%d: Abort command - %s\n",
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002581 ha->host_no, id, lun, (ret == SUCCESS) ? "succeeded" : "failed");
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302582
2583 return ret;
2584}
2585
2586/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002587 * qla4xxx_eh_device_reset - callback for target reset.
2588 * @cmd: Pointer to Linux's SCSI command structure
2589 *
2590 * This routine is called by the Linux OS to reset all luns on the
2591 * specified target.
2592 **/
2593static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
2594{
2595 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2596 struct ddb_entry *ddb_entry = cmd->device->hostdata;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002597 int ret = FAILED, stat;
2598
Karen Higgins612f7342009-07-15 15:03:01 -05002599 if (!ddb_entry)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002600 return ret;
2601
Mike Christiec01be6d2010-07-22 16:59:49 +05302602 ret = iscsi_block_scsi_eh(cmd);
2603 if (ret)
2604 return ret;
2605 ret = FAILED;
2606
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302607 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002608 "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
2609 cmd->device->channel, cmd->device->id, cmd->device->lun);
2610
2611 DEBUG2(printk(KERN_INFO
2612 "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
2613 "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
Jens Axboe242f9dc2008-09-14 05:55:09 -07002614 cmd, jiffies, cmd->request->timeout / HZ,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002615 ha->dpc_flags, cmd->result, cmd->allowed));
2616
2617 /* FIXME: wait for hba to go online */
2618 stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
2619 if (stat != QLA_SUCCESS) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302620 ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002621 goto eh_dev_reset_done;
2622 }
2623
Mike Christiece545032008-02-29 18:25:20 -06002624 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2625 cmd->device)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302626 ql4_printk(KERN_INFO, ha,
Mike Christiece545032008-02-29 18:25:20 -06002627 "DEVICE RESET FAILED - waiting for "
2628 "commands.\n");
2629 goto eh_dev_reset_done;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002630 }
2631
David C Somayajulu9d562912008-03-19 11:23:03 -07002632 /* Send marker. */
2633 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2634 MM_LUN_RESET) != QLA_SUCCESS)
2635 goto eh_dev_reset_done;
2636
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302637 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002638 "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
2639 ha->host_no, cmd->device->channel, cmd->device->id,
2640 cmd->device->lun);
2641
2642 ret = SUCCESS;
2643
2644eh_dev_reset_done:
2645
2646 return ret;
2647}
2648
2649/**
Mike Christiece545032008-02-29 18:25:20 -06002650 * qla4xxx_eh_target_reset - callback for target reset.
2651 * @cmd: Pointer to Linux's SCSI command structure
2652 *
2653 * This routine is called by the Linux OS to reset the target.
2654 **/
2655static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
2656{
2657 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2658 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christiec01be6d2010-07-22 16:59:49 +05302659 int stat, ret;
Mike Christiece545032008-02-29 18:25:20 -06002660
2661 if (!ddb_entry)
2662 return FAILED;
2663
Mike Christiec01be6d2010-07-22 16:59:49 +05302664 ret = iscsi_block_scsi_eh(cmd);
2665 if (ret)
2666 return ret;
2667
Mike Christiece545032008-02-29 18:25:20 -06002668 starget_printk(KERN_INFO, scsi_target(cmd->device),
2669 "WARM TARGET RESET ISSUED.\n");
2670
2671 DEBUG2(printk(KERN_INFO
2672 "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
2673 "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
Jens Axboe242f9dc2008-09-14 05:55:09 -07002674 ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
Mike Christiece545032008-02-29 18:25:20 -06002675 ha->dpc_flags, cmd->result, cmd->allowed));
2676
2677 stat = qla4xxx_reset_target(ha, ddb_entry);
2678 if (stat != QLA_SUCCESS) {
2679 starget_printk(KERN_INFO, scsi_target(cmd->device),
2680 "WARM TARGET RESET FAILED.\n");
2681 return FAILED;
2682 }
2683
Mike Christiece545032008-02-29 18:25:20 -06002684 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2685 NULL)) {
2686 starget_printk(KERN_INFO, scsi_target(cmd->device),
2687 "WARM TARGET DEVICE RESET FAILED - "
2688 "waiting for commands.\n");
2689 return FAILED;
2690 }
2691
David C Somayajulu9d562912008-03-19 11:23:03 -07002692 /* Send marker. */
2693 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2694 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
2695 starget_printk(KERN_INFO, scsi_target(cmd->device),
2696 "WARM TARGET DEVICE RESET FAILED - "
2697 "marker iocb failed.\n");
2698 return FAILED;
2699 }
2700
Mike Christiece545032008-02-29 18:25:20 -06002701 starget_printk(KERN_INFO, scsi_target(cmd->device),
2702 "WARM TARGET RESET SUCCEEDED.\n");
2703 return SUCCESS;
2704}
2705
2706/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002707 * qla4xxx_eh_host_reset - kernel callback
2708 * @cmd: Pointer to Linux's SCSI command structure
2709 *
2710 * This routine is invoked by the Linux kernel to perform fatal error
2711 * recovery on the specified adapter.
2712 **/
2713static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
2714{
2715 int return_status = FAILED;
2716 struct scsi_qla_host *ha;
2717
2718 ha = (struct scsi_qla_host *) cmd->device->host->hostdata;
2719
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302720 if (ql4xdontresethba) {
2721 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
2722 ha->host_no, __func__));
2723 return FAILED;
2724 }
2725
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302726 ql4_printk(KERN_INFO, ha,
Karen Higginsdca05c42009-07-15 15:03:00 -05002727 "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002728 cmd->device->channel, cmd->device->id, cmd->device->lun);
2729
2730 if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
2731 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
2732 "DEAD.\n", ha->host_no, cmd->device->channel,
2733 __func__));
2734
2735 return FAILED;
2736 }
2737
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302738 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
2739 if (is_qla8022(ha))
2740 set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2741 else
2742 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2743 }
Mike Christie50a29ae2008-03-04 13:26:53 -06002744
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302745 if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002746 return_status = SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002747
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302748 ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002749 return_status == FAILED ? "FAILED" : "SUCCEEDED");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002750
2751 return return_status;
2752}
2753
Lalit Chandivade2232be02010-07-30 14:38:47 +05302754/* PCI AER driver recovers from all correctable errors w/o
2755 * driver intervention. For uncorrectable errors PCI AER
2756 * driver calls the following device driver's callbacks
2757 *
2758 * - Fatal Errors - link_reset
2759 * - Non-Fatal Errors - driver's pci_error_detected() which
2760 * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
2761 *
2762 * PCI AER driver calls
2763 * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
2764 * returns RECOVERED or NEED_RESET if fw_hung
2765 * NEED_RESET - driver's slot_reset()
2766 * DISCONNECT - device is dead & cannot recover
2767 * RECOVERED - driver's pci_resume()
2768 */
2769static pci_ers_result_t
2770qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
2771{
2772 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2773
2774 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
2775 ha->host_no, __func__, state);
2776
2777 if (!is_aer_supported(ha))
2778 return PCI_ERS_RESULT_NONE;
2779
2780 switch (state) {
2781 case pci_channel_io_normal:
2782 clear_bit(AF_EEH_BUSY, &ha->flags);
2783 return PCI_ERS_RESULT_CAN_RECOVER;
2784 case pci_channel_io_frozen:
2785 set_bit(AF_EEH_BUSY, &ha->flags);
2786 qla4xxx_mailbox_premature_completion(ha);
2787 qla4xxx_free_irqs(ha);
2788 pci_disable_device(pdev);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002789 /* Return back all IOs */
2790 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
Lalit Chandivade2232be02010-07-30 14:38:47 +05302791 return PCI_ERS_RESULT_NEED_RESET;
2792 case pci_channel_io_perm_failure:
2793 set_bit(AF_EEH_BUSY, &ha->flags);
2794 set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
2795 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
2796 return PCI_ERS_RESULT_DISCONNECT;
2797 }
2798 return PCI_ERS_RESULT_NEED_RESET;
2799}
2800
2801/**
2802 * qla4xxx_pci_mmio_enabled() gets called if
2803 * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
2804 * and read/write to the device still works.
2805 **/
2806static pci_ers_result_t
2807qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
2808{
2809 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2810
2811 if (!is_aer_supported(ha))
2812 return PCI_ERS_RESULT_NONE;
2813
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002814 return PCI_ERS_RESULT_RECOVERED;
Lalit Chandivade2232be02010-07-30 14:38:47 +05302815}
2816
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002817static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
Lalit Chandivade2232be02010-07-30 14:38:47 +05302818{
2819 uint32_t rval = QLA_ERROR;
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002820 uint32_t ret = 0;
Lalit Chandivade2232be02010-07-30 14:38:47 +05302821 int fn;
2822 struct pci_dev *other_pdev = NULL;
2823
2824 ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
2825
2826 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2827
2828 if (test_bit(AF_ONLINE, &ha->flags)) {
2829 clear_bit(AF_ONLINE, &ha->flags);
2830 qla4xxx_mark_all_devices_missing(ha);
2831 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
Lalit Chandivade2232be02010-07-30 14:38:47 +05302832 }
2833
2834 fn = PCI_FUNC(ha->pdev->devfn);
2835 while (fn > 0) {
2836 fn--;
2837 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
2838 "func %x\n", ha->host_no, __func__, fn);
2839 /* Get the pci device given the domain, bus,
2840 * slot/function number */
2841 other_pdev =
2842 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
2843 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
2844 fn));
2845
2846 if (!other_pdev)
2847 continue;
2848
2849 if (atomic_read(&other_pdev->enable_cnt)) {
2850 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
2851 "func in enabled state%x\n", ha->host_no,
2852 __func__, fn);
2853 pci_dev_put(other_pdev);
2854 break;
2855 }
2856 pci_dev_put(other_pdev);
2857 }
2858
2859 /* The first function on the card, the reset owner will
2860 * start & initialize the firmware. The other functions
2861 * on the card will reset the firmware context
2862 */
2863 if (!fn) {
2864 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
2865 "0x%x is the owner\n", ha->host_no, __func__,
2866 ha->pdev->devfn);
2867
2868 qla4_8xxx_idc_lock(ha);
2869 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2870 QLA82XX_DEV_COLD);
2871
2872 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
2873 QLA82XX_IDC_VERSION);
2874
2875 qla4_8xxx_idc_unlock(ha);
2876 clear_bit(AF_FW_RECOVERY, &ha->flags);
2877 rval = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
2878 qla4_8xxx_idc_lock(ha);
2879
2880 if (rval != QLA_SUCCESS) {
2881 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
2882 "FAILED\n", ha->host_no, __func__);
2883 qla4_8xxx_clear_drv_active(ha);
2884 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2885 QLA82XX_DEV_FAILED);
2886 } else {
2887 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
2888 "READY\n", ha->host_no, __func__);
2889 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2890 QLA82XX_DEV_READY);
2891 /* Clear driver state register */
2892 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
2893 qla4_8xxx_set_drv_active(ha);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002894 ret = qla4xxx_request_irqs(ha);
2895 if (ret) {
2896 ql4_printk(KERN_WARNING, ha, "Failed to "
2897 "reserve interrupt %d already in use.\n",
2898 ha->pdev->irq);
2899 rval = QLA_ERROR;
2900 } else {
2901 ha->isp_ops->enable_intrs(ha);
2902 rval = QLA_SUCCESS;
2903 }
Lalit Chandivade2232be02010-07-30 14:38:47 +05302904 }
2905 qla4_8xxx_idc_unlock(ha);
2906 } else {
2907 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
2908 "the reset owner\n", ha->host_no, __func__,
2909 ha->pdev->devfn);
2910 if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
2911 QLA82XX_DEV_READY)) {
2912 clear_bit(AF_FW_RECOVERY, &ha->flags);
2913 rval = qla4xxx_initialize_adapter(ha,
2914 PRESERVE_DDB_LIST);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002915 if (rval == QLA_SUCCESS) {
2916 ret = qla4xxx_request_irqs(ha);
2917 if (ret) {
2918 ql4_printk(KERN_WARNING, ha, "Failed to"
2919 " reserve interrupt %d already in"
2920 " use.\n", ha->pdev->irq);
2921 rval = QLA_ERROR;
2922 } else {
2923 ha->isp_ops->enable_intrs(ha);
2924 rval = QLA_SUCCESS;
2925 }
2926 }
Lalit Chandivade2232be02010-07-30 14:38:47 +05302927 qla4_8xxx_idc_lock(ha);
2928 qla4_8xxx_set_drv_active(ha);
2929 qla4_8xxx_idc_unlock(ha);
2930 }
2931 }
2932 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2933 return rval;
2934}
2935
2936static pci_ers_result_t
2937qla4xxx_pci_slot_reset(struct pci_dev *pdev)
2938{
2939 pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
2940 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2941 int rc;
2942
2943 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
2944 ha->host_no, __func__);
2945
2946 if (!is_aer_supported(ha))
2947 return PCI_ERS_RESULT_NONE;
2948
2949 /* Restore the saved state of PCIe device -
2950 * BAR registers, PCI Config space, PCIX, MSI,
2951 * IOV states
2952 */
2953 pci_restore_state(pdev);
2954
2955 /* pci_restore_state() clears the saved_state flag of the device
2956 * save restored state which resets saved_state flag
2957 */
2958 pci_save_state(pdev);
2959
2960 /* Initialize device or resume if in suspended state */
2961 rc = pci_enable_device(pdev);
2962 if (rc) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002963 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Can't re-enable "
Lalit Chandivade2232be02010-07-30 14:38:47 +05302964 "device after reset\n", ha->host_no, __func__);
2965 goto exit_slot_reset;
2966 }
2967
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002968 ha->isp_ops->disable_intrs(ha);
Lalit Chandivade2232be02010-07-30 14:38:47 +05302969
2970 if (is_qla8022(ha)) {
2971 if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
2972 ret = PCI_ERS_RESULT_RECOVERED;
2973 goto exit_slot_reset;
2974 } else
2975 goto exit_slot_reset;
2976 }
2977
2978exit_slot_reset:
2979 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
2980 "device after reset\n", ha->host_no, __func__, ret);
2981 return ret;
2982}
2983
2984static void
2985qla4xxx_pci_resume(struct pci_dev *pdev)
2986{
2987 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2988 int ret;
2989
2990 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
2991 ha->host_no, __func__);
2992
2993 ret = qla4xxx_wait_for_hba_online(ha);
2994 if (ret != QLA_SUCCESS) {
2995 ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
2996 "resume I/O from slot/link_reset\n", ha->host_no,
2997 __func__);
2998 }
2999
3000 pci_cleanup_aer_uncorrect_error_status(pdev);
3001 clear_bit(AF_EEH_BUSY, &ha->flags);
3002}
3003
3004static struct pci_error_handlers qla4xxx_err_handler = {
3005 .error_detected = qla4xxx_pci_error_detected,
3006 .mmio_enabled = qla4xxx_pci_mmio_enabled,
3007 .slot_reset = qla4xxx_pci_slot_reset,
3008 .resume = qla4xxx_pci_resume,
3009};
3010
David Somayajuluafaf5a22006-09-19 10:28:00 -07003011static struct pci_device_id qla4xxx_pci_tbl[] = {
3012 {
3013 .vendor = PCI_VENDOR_ID_QLOGIC,
3014 .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
3015 .subvendor = PCI_ANY_ID,
3016 .subdevice = PCI_ANY_ID,
3017 },
3018 {
3019 .vendor = PCI_VENDOR_ID_QLOGIC,
3020 .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
3021 .subvendor = PCI_ANY_ID,
3022 .subdevice = PCI_ANY_ID,
3023 },
David C Somayajulud9150582006-11-15 17:38:40 -08003024 {
3025 .vendor = PCI_VENDOR_ID_QLOGIC,
3026 .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
3027 .subvendor = PCI_ANY_ID,
3028 .subdevice = PCI_ANY_ID,
3029 },
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303030 {
3031 .vendor = PCI_VENDOR_ID_QLOGIC,
3032 .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
3033 .subvendor = PCI_ANY_ID,
3034 .subdevice = PCI_ANY_ID,
3035 },
David Somayajuluafaf5a22006-09-19 10:28:00 -07003036 {0, 0},
3037};
3038MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
3039
Adrian Bunk47975472007-04-26 00:35:16 -07003040static struct pci_driver qla4xxx_pci_driver = {
David Somayajuluafaf5a22006-09-19 10:28:00 -07003041 .name = DRIVER_NAME,
3042 .id_table = qla4xxx_pci_tbl,
3043 .probe = qla4xxx_probe_adapter,
3044 .remove = qla4xxx_remove_adapter,
Lalit Chandivade2232be02010-07-30 14:38:47 +05303045 .err_handler = &qla4xxx_err_handler,
David Somayajuluafaf5a22006-09-19 10:28:00 -07003046};
3047
3048static int __init qla4xxx_module_init(void)
3049{
3050 int ret;
3051
3052 /* Allocate cache for SRBs. */
3053 srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09003054 SLAB_HWCACHE_ALIGN, NULL);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003055 if (srb_cachep == NULL) {
3056 printk(KERN_ERR
3057 "%s: Unable to allocate SRB cache..."
3058 "Failing load!\n", DRIVER_NAME);
3059 ret = -ENOMEM;
3060 goto no_srp_cache;
3061 }
3062
3063 /* Derive version string. */
3064 strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
Andrew Vasquez11010fe2006-10-06 09:54:59 -07003065 if (ql4xextended_error_logging)
David Somayajuluafaf5a22006-09-19 10:28:00 -07003066 strcat(qla4xxx_version_str, "-debug");
3067
3068 qla4xxx_scsi_transport =
3069 iscsi_register_transport(&qla4xxx_iscsi_transport);
3070 if (!qla4xxx_scsi_transport){
3071 ret = -ENODEV;
3072 goto release_srb_cache;
3073 }
3074
David Somayajuluafaf5a22006-09-19 10:28:00 -07003075 ret = pci_register_driver(&qla4xxx_pci_driver);
3076 if (ret)
3077 goto unregister_transport;
3078
3079 printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
3080 return 0;
Doug Maxey5ae16db2006-10-05 23:50:07 -05003081
David Somayajuluafaf5a22006-09-19 10:28:00 -07003082unregister_transport:
3083 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
3084release_srb_cache:
3085 kmem_cache_destroy(srb_cachep);
3086no_srp_cache:
3087 return ret;
3088}
3089
3090static void __exit qla4xxx_module_exit(void)
3091{
David Somayajuluafaf5a22006-09-19 10:28:00 -07003092 pci_unregister_driver(&qla4xxx_pci_driver);
3093 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
3094 kmem_cache_destroy(srb_cachep);
3095}
3096
3097module_init(qla4xxx_module_init);
3098module_exit(qla4xxx_module_exit);
3099
3100MODULE_AUTHOR("QLogic Corporation");
3101MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
3102MODULE_LICENSE("GPL");
3103MODULE_VERSION(QLA4XXX_DRIVER_VERSION);