blob: 32df0c5fc9cc814d2e7eedb6954645720bd173d3 [file] [log] [blame]
David Somayajuluafaf5a22006-09-19 10:28:00 -07001/*
2 * QLogic iSCSI HBA Driver
Vikas Chaudhary7d01d062010-12-02 22:12:51 -08003 * Copyright (c) 2003-2010 QLogic Corporation
David Somayajuluafaf5a22006-09-19 10:28:00 -07004 *
5 * See LICENSE.qla4xxx for copyright and licensing details.
6 */
7#include <linux/moduleparam.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
David Somayajuluafaf5a22006-09-19 10:28:00 -07009
10#include <scsi/scsi_tcq.h>
11#include <scsi/scsicam.h>
12
13#include "ql4_def.h"
David C Somayajulubee4fe82007-05-23 18:03:32 -070014#include "ql4_version.h"
15#include "ql4_glbl.h"
16#include "ql4_dbg.h"
17#include "ql4_inline.h"
David Somayajuluafaf5a22006-09-19 10:28:00 -070018
19/*
20 * Driver version
21 */
Adrian Bunk47975472007-04-26 00:35:16 -070022static char qla4xxx_version_str[40];
David Somayajuluafaf5a22006-09-19 10:28:00 -070023
24/*
25 * SRB allocation cache
26 */
Christoph Lametere18b8902006-12-06 20:33:20 -080027static struct kmem_cache *srb_cachep;
David Somayajuluafaf5a22006-09-19 10:28:00 -070028
29/*
30 * Module parameter information and variables
31 */
David Somayajuluafaf5a22006-09-19 10:28:00 -070032int ql4xdontresethba = 0;
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +053033module_param(ql4xdontresethba, int, S_IRUGO | S_IWUSR);
David Somayajuluafaf5a22006-09-19 10:28:00 -070034MODULE_PARM_DESC(ql4xdontresethba,
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +053035 "Don't reset the HBA for driver recovery \n"
36 " 0 - It will reset HBA (Default)\n"
37 " 1 - It will NOT reset HBA");
David Somayajuluafaf5a22006-09-19 10:28:00 -070038
Andrew Vasquez11010fe2006-10-06 09:54:59 -070039int ql4xextended_error_logging = 0; /* 0 = off, 1 = log errors */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +053040module_param(ql4xextended_error_logging, int, S_IRUGO | S_IWUSR);
Andrew Vasquez11010fe2006-10-06 09:54:59 -070041MODULE_PARM_DESC(ql4xextended_error_logging,
David Somayajuluafaf5a22006-09-19 10:28:00 -070042 "Option to enable extended error logging, "
43 "Default is 0 - no logging, 1 - debug logging");
44
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +053045int ql4xenablemsix = 1;
46module_param(ql4xenablemsix, int, S_IRUGO|S_IWUSR);
47MODULE_PARM_DESC(ql4xenablemsix,
48 "Set to enable MSI or MSI-X interrupt mechanism.\n"
49 " 0 = enable INTx interrupt mechanism.\n"
50 " 1 = enable MSI-X interrupt mechanism (Default).\n"
51 " 2 = enable MSI interrupt mechanism.");
David C Somayajulu477ffb92007-01-22 12:26:11 -080052
Mike Christied510d962008-07-11 19:50:33 -050053#define QL4_DEF_QDEPTH 32
Vikas Chaudhary8bb40332011-03-21 03:34:31 -070054static int ql4xmaxqdepth = QL4_DEF_QDEPTH;
55module_param(ql4xmaxqdepth, int, S_IRUGO | S_IWUSR);
56MODULE_PARM_DESC(ql4xmaxqdepth,
57 "Maximum queue depth to report for target devices.\n"
58 " Default: 32.");
Mike Christied510d962008-07-11 19:50:33 -050059
Vikas Chaudhary30387272011-03-21 03:34:32 -070060static int ql4xsess_recovery_tmo = QL4_SESS_RECOVERY_TMO;
61module_param(ql4xsess_recovery_tmo, int, S_IRUGO);
62MODULE_PARM_DESC(ql4xsess_recovery_tmo,
63 "Target Session Recovery Timeout.\n"
64 " Default: 30 sec.");
65
David Somayajuluafaf5a22006-09-19 10:28:00 -070066/*
67 * SCSI host template entry points
68 */
Adrian Bunk47975472007-04-26 00:35:16 -070069static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -070070
71/*
72 * iSCSI template entry points
73 */
Mike Christie2174a042007-05-30 12:57:10 -050074static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
75 enum iscsi_tgt_dscvr type, uint32_t enable,
76 struct sockaddr *dst_addr);
David Somayajuluafaf5a22006-09-19 10:28:00 -070077static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
78 enum iscsi_param param, char *buf);
79static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
80 enum iscsi_param param, char *buf);
Mike Christieaa1e93a2007-05-30 12:57:09 -050081static int qla4xxx_host_get_param(struct Scsi_Host *shost,
82 enum iscsi_host_param param, char *buf);
Mike Christied00efe32011-07-25 13:48:38 -050083static int qla4xxx_iface_set_param(struct Scsi_Host *shost, char *data,
84 int count);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -050085static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
86 enum iscsi_param_type param_type,
87 int param, char *buf);
David Somayajuluafaf5a22006-09-19 10:28:00 -070088static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session);
Mike Christie5c656af2009-07-15 15:02:59 -050089static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
David Somayajuluafaf5a22006-09-19 10:28:00 -070090
91/*
92 * SCSI host template entry points
93 */
Jeff Garzikf2812332010-11-16 02:10:29 -050094static int qla4xxx_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmd);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +053095static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -070096static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
Mike Christiece545032008-02-29 18:25:20 -060097static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -070098static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
99static int qla4xxx_slave_alloc(struct scsi_device *device);
100static int qla4xxx_slave_configure(struct scsi_device *device);
101static void qla4xxx_slave_destroy(struct scsi_device *sdev);
Mike Christie024f8012008-03-04 13:26:54 -0600102static void qla4xxx_scan_start(struct Scsi_Host *shost);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700103
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530104static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
105 QLA82XX_LEGACY_INTR_CONFIG;
106
David Somayajuluafaf5a22006-09-19 10:28:00 -0700107static struct scsi_host_template qla4xxx_driver_template = {
108 .module = THIS_MODULE,
109 .name = DRIVER_NAME,
110 .proc_name = DRIVER_NAME,
111 .queuecommand = qla4xxx_queuecommand,
112
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530113 .eh_abort_handler = qla4xxx_eh_abort,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700114 .eh_device_reset_handler = qla4xxx_eh_device_reset,
Mike Christiece545032008-02-29 18:25:20 -0600115 .eh_target_reset_handler = qla4xxx_eh_target_reset,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700116 .eh_host_reset_handler = qla4xxx_eh_host_reset,
Mike Christie5c656af2009-07-15 15:02:59 -0500117 .eh_timed_out = qla4xxx_eh_cmd_timed_out,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700118
119 .slave_configure = qla4xxx_slave_configure,
120 .slave_alloc = qla4xxx_slave_alloc,
121 .slave_destroy = qla4xxx_slave_destroy,
122
Mike Christie921601b2008-01-31 13:36:49 -0600123 .scan_finished = iscsi_scan_finished,
Mike Christie024f8012008-03-04 13:26:54 -0600124 .scan_start = qla4xxx_scan_start,
Mike Christie921601b2008-01-31 13:36:49 -0600125
David Somayajuluafaf5a22006-09-19 10:28:00 -0700126 .this_id = -1,
127 .cmd_per_lun = 3,
128 .use_clustering = ENABLE_CLUSTERING,
129 .sg_tablesize = SG_ALL,
130
131 .max_sectors = 0xFFFF,
Harish Zunjarrao7ad633c2011-05-17 23:17:11 -0700132 .shost_attrs = qla4xxx_host_attrs,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700133};
134
135static struct iscsi_transport qla4xxx_iscsi_transport = {
136 .owner = THIS_MODULE,
137 .name = DRIVER_NAME,
Mike Christied8196ed2007-05-30 12:57:25 -0500138 .caps = CAP_FW_DB | CAP_SENDTARGETS_OFFLOAD |
139 CAP_DATA_PATH_OFFLOAD,
Mike Christieaa1e93a2007-05-30 12:57:09 -0500140 .param_mask = ISCSI_CONN_PORT | ISCSI_CONN_ADDRESS |
Vikas Chaudhary3b2bef12010-07-10 14:51:30 +0530141 ISCSI_TARGET_NAME | ISCSI_TPGT |
142 ISCSI_TARGET_ALIAS,
Mike Christie8ad57812007-05-30 12:57:13 -0500143 .host_param_mask = ISCSI_HOST_HWADDRESS |
Mike Christie22236962007-05-30 12:57:24 -0500144 ISCSI_HOST_IPADDRESS |
Mike Christie8ad57812007-05-30 12:57:13 -0500145 ISCSI_HOST_INITIATOR_NAME,
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500146 .iface_param_mask = ISCSI_NET_IPV4_ADDR |
147 ISCSI_NET_IPV4_SUBNET |
148 ISCSI_NET_IPV4_GW |
149 ISCSI_NET_IPV4_BOOTPROTO |
150 ISCSI_NET_IFACE_ENABLE |
151 ISCSI_NET_IPV6_LINKLOCAL |
152 ISCSI_NET_IPV6_ADDR |
153 ISCSI_NET_IPV6_ROUTER |
154 ISCSI_NET_IPV6_ADDR_AUTOCFG |
155 ISCSI_NET_IPV6_LINKLOCAL_AUTOCFG |
156 ISCSI_NET_IFACE_ENABLE,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700157 .tgt_dscvr = qla4xxx_tgt_dscvr,
158 .get_conn_param = qla4xxx_conn_get_param,
159 .get_session_param = qla4xxx_sess_get_param,
Mike Christieaa1e93a2007-05-30 12:57:09 -0500160 .get_host_param = qla4xxx_host_get_param,
Mike Christied00efe32011-07-25 13:48:38 -0500161 .set_iface_param = qla4xxx_iface_set_param,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700162 .session_recovery_timedout = qla4xxx_recovery_timedout,
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500163 .get_iface_param = qla4xxx_get_iface_param,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700164};
165
166static struct scsi_transport_template *qla4xxx_scsi_transport;
167
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500168static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
169 enum iscsi_param_type param_type,
170 int param, char *buf)
171{
172 struct Scsi_Host *shost = iscsi_iface_to_shost(iface);
173 struct scsi_qla_host *ha = to_qla_host(shost);
174 int len = -ENOSYS;
175
176 if (param_type != ISCSI_NET_PARAM)
177 return -ENOSYS;
178
179 switch (param) {
180 case ISCSI_NET_PARAM_IPV4_ADDR:
181 len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
182 break;
183 case ISCSI_NET_PARAM_IPV4_SUBNET:
184 len = sprintf(buf, "%pI4\n", &ha->ip_config.subnet_mask);
185 break;
186 case ISCSI_NET_PARAM_IPV4_GW:
187 len = sprintf(buf, "%pI4\n", &ha->ip_config.gateway);
188 break;
189 case ISCSI_NET_PARAM_IFACE_ENABLE:
190 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
191 len = sprintf(buf, "%s\n",
192 (ha->ip_config.ipv4_options &
193 IPOPT_IPV4_PROTOCOL_ENABLE) ?
194 "enabled" : "disabled");
195 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
196 len = sprintf(buf, "%s\n",
197 (ha->ip_config.ipv6_options &
198 IPV6_OPT_IPV6_PROTOCOL_ENABLE) ?
199 "enabled" : "disabled");
200 break;
201 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
202 len = sprintf(buf, "%s\n",
203 (ha->ip_config.tcp_options & TCPOPT_DHCP_ENABLE) ?
204 "dhcp" : "static");
205 break;
206 case ISCSI_NET_PARAM_IPV6_ADDR:
207 if (iface->iface_num == 0)
208 len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr0);
209 if (iface->iface_num == 1)
210 len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr1);
211 break;
212 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
213 len = sprintf(buf, "%pI6\n",
214 &ha->ip_config.ipv6_link_local_addr);
215 break;
216 case ISCSI_NET_PARAM_IPV6_ROUTER:
217 len = sprintf(buf, "%pI6\n",
218 &ha->ip_config.ipv6_default_router_addr);
219 break;
220 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
221 len = sprintf(buf, "%s\n",
222 (ha->ip_config.ipv6_addl_options &
223 IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) ?
224 "nd" : "static");
225 break;
226 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
227 len = sprintf(buf, "%s\n",
228 (ha->ip_config.ipv6_addl_options &
229 IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR) ?
230 "auto" : "static");
231 break;
232 default:
233 len = -ENOSYS;
234 }
235
236 return len;
237}
238
Mike Christie5c656af2009-07-15 15:02:59 -0500239static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
240{
241 struct iscsi_cls_session *session;
242 struct ddb_entry *ddb_entry;
243
244 session = starget_to_session(scsi_target(sc->device));
245 ddb_entry = session->dd_data;
246
247 /* if we are not logged in then the LLD is going to clean up the cmd */
248 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
249 return BLK_EH_RESET_TIMER;
250 else
251 return BLK_EH_NOT_HANDLED;
252}
253
David Somayajuluafaf5a22006-09-19 10:28:00 -0700254static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session)
255{
256 struct ddb_entry *ddb_entry = session->dd_data;
257 struct scsi_qla_host *ha = ddb_entry->ha;
258
Mike Christie568d3032008-01-31 13:36:47 -0600259 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
260 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700261
Vikas Chaudhary3013cea2010-07-30 14:25:46 +0530262 DEBUG2(printk("scsi%ld: %s: ddb [%d] session recovery timeout "
Mike Christie568d3032008-01-31 13:36:47 -0600263 "of (%d) secs exhausted, marking device DEAD.\n",
264 ha->host_no, __func__, ddb_entry->fw_ddb_index,
Vikas Chaudhary30387272011-03-21 03:34:32 -0700265 ddb_entry->sess->recovery_tmo));
Mike Christie568d3032008-01-31 13:36:47 -0600266 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700267}
268
Mike Christieaa1e93a2007-05-30 12:57:09 -0500269static int qla4xxx_host_get_param(struct Scsi_Host *shost,
270 enum iscsi_host_param param, char *buf)
271{
272 struct scsi_qla_host *ha = to_qla_host(shost);
273 int len;
274
275 switch (param) {
276 case ISCSI_HOST_PARAM_HWADDRESS:
Michael Chan7ffc49a2007-12-24 21:28:09 -0800277 len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
Mike Christieaa1e93a2007-05-30 12:57:09 -0500278 break;
Mike Christie22236962007-05-30 12:57:24 -0500279 case ISCSI_HOST_PARAM_IPADDRESS:
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500280 len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
Mike Christie22236962007-05-30 12:57:24 -0500281 break;
Mike Christie8ad57812007-05-30 12:57:13 -0500282 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie22236962007-05-30 12:57:24 -0500283 len = sprintf(buf, "%s\n", ha->name_string);
Mike Christie8ad57812007-05-30 12:57:13 -0500284 break;
Mike Christieaa1e93a2007-05-30 12:57:09 -0500285 default:
286 return -ENOSYS;
287 }
288
289 return len;
290}
291
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500292static void qla4xxx_create_ipv4_iface(struct scsi_qla_host *ha)
293{
294 if (ha->iface_ipv4)
295 return;
296
297 /* IPv4 */
298 ha->iface_ipv4 = iscsi_create_iface(ha->host,
299 &qla4xxx_iscsi_transport,
300 ISCSI_IFACE_TYPE_IPV4, 0, 0);
301 if (!ha->iface_ipv4)
302 ql4_printk(KERN_ERR, ha, "Could not create IPv4 iSCSI "
303 "iface0.\n");
304}
305
306static void qla4xxx_create_ipv6_iface(struct scsi_qla_host *ha)
307{
308 if (!ha->iface_ipv6_0)
309 /* IPv6 iface-0 */
310 ha->iface_ipv6_0 = iscsi_create_iface(ha->host,
311 &qla4xxx_iscsi_transport,
312 ISCSI_IFACE_TYPE_IPV6, 0,
313 0);
314 if (!ha->iface_ipv6_0)
315 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
316 "iface0.\n");
317
318 if (!ha->iface_ipv6_1)
319 /* IPv6 iface-1 */
320 ha->iface_ipv6_1 = iscsi_create_iface(ha->host,
321 &qla4xxx_iscsi_transport,
322 ISCSI_IFACE_TYPE_IPV6, 1,
323 0);
324 if (!ha->iface_ipv6_1)
325 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
326 "iface1.\n");
327}
328
329static void qla4xxx_create_ifaces(struct scsi_qla_host *ha)
330{
331 if (ha->ip_config.ipv4_options & IPOPT_IPV4_PROTOCOL_ENABLE)
332 qla4xxx_create_ipv4_iface(ha);
333
334 if (ha->ip_config.ipv6_options & IPV6_OPT_IPV6_PROTOCOL_ENABLE)
335 qla4xxx_create_ipv6_iface(ha);
336}
337
338static void qla4xxx_destroy_ipv4_iface(struct scsi_qla_host *ha)
339{
340 if (ha->iface_ipv4) {
341 iscsi_destroy_iface(ha->iface_ipv4);
342 ha->iface_ipv4 = NULL;
343 }
344}
345
346static void qla4xxx_destroy_ipv6_iface(struct scsi_qla_host *ha)
347{
348 if (ha->iface_ipv6_0) {
349 iscsi_destroy_iface(ha->iface_ipv6_0);
350 ha->iface_ipv6_0 = NULL;
351 }
352 if (ha->iface_ipv6_1) {
353 iscsi_destroy_iface(ha->iface_ipv6_1);
354 ha->iface_ipv6_1 = NULL;
355 }
356}
357
358static void qla4xxx_destroy_ifaces(struct scsi_qla_host *ha)
359{
360 qla4xxx_destroy_ipv4_iface(ha);
361 qla4xxx_destroy_ipv6_iface(ha);
362}
363
Mike Christied00efe32011-07-25 13:48:38 -0500364static void qla4xxx_set_ipv6(struct scsi_qla_host *ha,
365 struct iscsi_iface_param_info *iface_param,
366 struct addr_ctrl_blk *init_fw_cb)
367{
368 /*
369 * iface_num 0 is valid for IPv6 Addr, linklocal, router, autocfg.
370 * iface_num 1 is valid only for IPv6 Addr.
371 */
372 switch (iface_param->param) {
373 case ISCSI_NET_PARAM_IPV6_ADDR:
374 if (iface_param->iface_num & 0x1)
375 /* IPv6 Addr 1 */
376 memcpy(init_fw_cb->ipv6_addr1, iface_param->value,
377 sizeof(init_fw_cb->ipv6_addr1));
378 else
379 /* IPv6 Addr 0 */
380 memcpy(init_fw_cb->ipv6_addr0, iface_param->value,
381 sizeof(init_fw_cb->ipv6_addr0));
382 break;
383 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
384 if (iface_param->iface_num & 0x1)
385 break;
386 memcpy(init_fw_cb->ipv6_if_id, &iface_param->value[8],
387 sizeof(init_fw_cb->ipv6_if_id));
388 break;
389 case ISCSI_NET_PARAM_IPV6_ROUTER:
390 if (iface_param->iface_num & 0x1)
391 break;
392 memcpy(init_fw_cb->ipv6_dflt_rtr_addr, iface_param->value,
393 sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
394 break;
395 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
396 /* Autocfg applies to even interface */
397 if (iface_param->iface_num & 0x1)
398 break;
399
400 if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_DISABLE)
401 init_fw_cb->ipv6_addtl_opts &=
402 cpu_to_le16(
403 ~IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
404 else if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_ND_ENABLE)
405 init_fw_cb->ipv6_addtl_opts |=
406 cpu_to_le16(
407 IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
408 else
409 ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
410 "IPv6 addr\n");
411 break;
412 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
413 /* Autocfg applies to even interface */
414 if (iface_param->iface_num & 0x1)
415 break;
416
417 if (iface_param->value[0] ==
418 ISCSI_IPV6_LINKLOCAL_AUTOCFG_ENABLE)
419 init_fw_cb->ipv6_addtl_opts |= cpu_to_le16(
420 IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
421 else if (iface_param->value[0] ==
422 ISCSI_IPV6_LINKLOCAL_AUTOCFG_DISABLE)
423 init_fw_cb->ipv6_addtl_opts &= cpu_to_le16(
424 ~IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
425 else
426 ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
427 "IPv6 linklocal addr\n");
428 break;
429 case ISCSI_NET_PARAM_IPV6_ROUTER_AUTOCFG:
430 /* Autocfg applies to even interface */
431 if (iface_param->iface_num & 0x1)
432 break;
433
434 if (iface_param->value[0] == ISCSI_IPV6_ROUTER_AUTOCFG_ENABLE)
435 memset(init_fw_cb->ipv6_dflt_rtr_addr, 0,
436 sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
437 break;
438 case ISCSI_NET_PARAM_IFACE_ENABLE:
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500439 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
Mike Christied00efe32011-07-25 13:48:38 -0500440 init_fw_cb->ipv6_opts |=
441 cpu_to_le16(IPV6_OPT_IPV6_PROTOCOL_ENABLE);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500442 qla4xxx_create_ipv6_iface(ha);
443 } else {
Mike Christied00efe32011-07-25 13:48:38 -0500444 init_fw_cb->ipv6_opts &=
445 cpu_to_le16(~IPV6_OPT_IPV6_PROTOCOL_ENABLE &
446 0xFFFF);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500447 qla4xxx_destroy_ipv6_iface(ha);
448 }
Mike Christied00efe32011-07-25 13:48:38 -0500449 break;
450 case ISCSI_NET_PARAM_VLAN_ID:
451 if (iface_param->len != sizeof(init_fw_cb->ipv6_vlan_tag))
452 break;
453 init_fw_cb->ipv6_vlan_tag = *(uint16_t *)iface_param->value;
454 break;
455 default:
456 ql4_printk(KERN_ERR, ha, "Unknown IPv6 param = %d\n",
457 iface_param->param);
458 break;
459 }
460}
461
462static void qla4xxx_set_ipv4(struct scsi_qla_host *ha,
463 struct iscsi_iface_param_info *iface_param,
464 struct addr_ctrl_blk *init_fw_cb)
465{
466 switch (iface_param->param) {
467 case ISCSI_NET_PARAM_IPV4_ADDR:
468 memcpy(init_fw_cb->ipv4_addr, iface_param->value,
469 sizeof(init_fw_cb->ipv4_addr));
470 break;
471 case ISCSI_NET_PARAM_IPV4_SUBNET:
472 memcpy(init_fw_cb->ipv4_subnet, iface_param->value,
473 sizeof(init_fw_cb->ipv4_subnet));
474 break;
475 case ISCSI_NET_PARAM_IPV4_GW:
476 memcpy(init_fw_cb->ipv4_gw_addr, iface_param->value,
477 sizeof(init_fw_cb->ipv4_gw_addr));
478 break;
479 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
480 if (iface_param->value[0] == ISCSI_BOOTPROTO_DHCP)
481 init_fw_cb->ipv4_tcp_opts |=
482 cpu_to_le16(TCPOPT_DHCP_ENABLE);
483 else if (iface_param->value[0] == ISCSI_BOOTPROTO_STATIC)
484 init_fw_cb->ipv4_tcp_opts &=
485 cpu_to_le16(~TCPOPT_DHCP_ENABLE);
486 else
487 ql4_printk(KERN_ERR, ha, "Invalid IPv4 bootproto\n");
488 break;
489 case ISCSI_NET_PARAM_IFACE_ENABLE:
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500490 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
Mike Christied00efe32011-07-25 13:48:38 -0500491 init_fw_cb->ipv4_ip_opts |=
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500492 cpu_to_le16(IPOPT_IPV4_PROTOCOL_ENABLE);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500493 qla4xxx_create_ipv4_iface(ha);
494 } else {
Mike Christied00efe32011-07-25 13:48:38 -0500495 init_fw_cb->ipv4_ip_opts &=
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500496 cpu_to_le16(~IPOPT_IPV4_PROTOCOL_ENABLE &
Mike Christied00efe32011-07-25 13:48:38 -0500497 0xFFFF);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500498 qla4xxx_destroy_ipv4_iface(ha);
499 }
Mike Christied00efe32011-07-25 13:48:38 -0500500 break;
501 case ISCSI_NET_PARAM_VLAN_ID:
502 if (iface_param->len != sizeof(init_fw_cb->ipv4_vlan_tag))
503 break;
504 init_fw_cb->ipv4_vlan_tag = *(uint16_t *)iface_param->value;
505 break;
506 default:
507 ql4_printk(KERN_ERR, ha, "Unknown IPv4 param = %d\n",
508 iface_param->param);
509 break;
510 }
511}
512
513static void
514qla4xxx_initcb_to_acb(struct addr_ctrl_blk *init_fw_cb)
515{
516 struct addr_ctrl_blk_def *acb;
517 acb = (struct addr_ctrl_blk_def *)init_fw_cb;
518 memset(acb->reserved1, 0, sizeof(acb->reserved1));
519 memset(acb->reserved2, 0, sizeof(acb->reserved2));
520 memset(acb->reserved3, 0, sizeof(acb->reserved3));
521 memset(acb->reserved4, 0, sizeof(acb->reserved4));
522 memset(acb->reserved5, 0, sizeof(acb->reserved5));
523 memset(acb->reserved6, 0, sizeof(acb->reserved6));
524 memset(acb->reserved7, 0, sizeof(acb->reserved7));
525 memset(acb->reserved8, 0, sizeof(acb->reserved8));
526 memset(acb->reserved9, 0, sizeof(acb->reserved9));
527 memset(acb->reserved10, 0, sizeof(acb->reserved10));
528 memset(acb->reserved11, 0, sizeof(acb->reserved11));
529 memset(acb->reserved12, 0, sizeof(acb->reserved12));
530 memset(acb->reserved13, 0, sizeof(acb->reserved13));
531 memset(acb->reserved14, 0, sizeof(acb->reserved14));
532 memset(acb->reserved15, 0, sizeof(acb->reserved15));
533}
534
535static int
536qla4xxx_iface_set_param(struct Scsi_Host *shost, char *data, int count)
537{
538 struct scsi_qla_host *ha = to_qla_host(shost);
539 int rval = 0;
540 struct iscsi_iface_param_info *iface_param = NULL;
541 struct addr_ctrl_blk *init_fw_cb = NULL;
542 dma_addr_t init_fw_cb_dma;
543 uint32_t mbox_cmd[MBOX_REG_COUNT];
544 uint32_t mbox_sts[MBOX_REG_COUNT];
545 uint32_t total_param_count;
546 uint32_t length;
547
548 init_fw_cb = dma_alloc_coherent(&ha->pdev->dev,
549 sizeof(struct addr_ctrl_blk),
550 &init_fw_cb_dma, GFP_KERNEL);
551 if (!init_fw_cb) {
552 ql4_printk(KERN_ERR, ha, "%s: Unable to alloc init_cb\n",
553 __func__);
554 return -ENOMEM;
555 }
556
557 memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
558 memset(&mbox_cmd, 0, sizeof(mbox_cmd));
559 memset(&mbox_sts, 0, sizeof(mbox_sts));
560
561 if (qla4xxx_get_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma)) {
562 ql4_printk(KERN_ERR, ha, "%s: get ifcb failed\n", __func__);
563 rval = -EIO;
564 goto exit_init_fw_cb;
565 }
566
567 total_param_count = count;
568 iface_param = (struct iscsi_iface_param_info *)data;
569
570 for ( ; total_param_count != 0; total_param_count--) {
571 length = iface_param->len;
572
573 if (iface_param->param_type != ISCSI_NET_PARAM)
574 continue;
575
576 switch (iface_param->iface_type) {
577 case ISCSI_IFACE_TYPE_IPV4:
578 switch (iface_param->iface_num) {
579 case 0:
580 qla4xxx_set_ipv4(ha, iface_param, init_fw_cb);
581 break;
582 default:
583 /* Cannot have more than one IPv4 interface */
584 ql4_printk(KERN_ERR, ha, "Invalid IPv4 iface "
585 "number = %d\n",
586 iface_param->iface_num);
587 break;
588 }
589 break;
590 case ISCSI_IFACE_TYPE_IPV6:
591 switch (iface_param->iface_num) {
592 case 0:
593 case 1:
594 qla4xxx_set_ipv6(ha, iface_param, init_fw_cb);
595 break;
596 default:
597 /* Cannot have more than two IPv6 interface */
598 ql4_printk(KERN_ERR, ha, "Invalid IPv6 iface "
599 "number = %d\n",
600 iface_param->iface_num);
601 break;
602 }
603 break;
604 default:
605 ql4_printk(KERN_ERR, ha, "Invalid iface type\n");
606 break;
607 }
608
609 iface_param = (struct iscsi_iface_param_info *)
610 ((uint8_t *)iface_param +
611 sizeof(struct iscsi_iface_param_info) + length);
612 }
613
614 init_fw_cb->cookie = cpu_to_le32(0x11BEAD5A);
615
616 rval = qla4xxx_set_flash(ha, init_fw_cb_dma, FLASH_SEGMENT_IFCB,
617 sizeof(struct addr_ctrl_blk),
618 FLASH_OPT_RMW_COMMIT);
619 if (rval != QLA_SUCCESS) {
620 ql4_printk(KERN_ERR, ha, "%s: set flash mbx failed\n",
621 __func__);
622 rval = -EIO;
623 goto exit_init_fw_cb;
624 }
625
626 qla4xxx_disable_acb(ha);
627
628 qla4xxx_initcb_to_acb(init_fw_cb);
629
630 rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma);
631 if (rval != QLA_SUCCESS) {
632 ql4_printk(KERN_ERR, ha, "%s: set acb mbx failed\n",
633 __func__);
634 rval = -EIO;
635 goto exit_init_fw_cb;
636 }
637
638 memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
639 qla4xxx_update_local_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb,
640 init_fw_cb_dma);
641
642exit_init_fw_cb:
643 dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk),
644 init_fw_cb, init_fw_cb_dma);
645
646 return rval;
647}
648
David Somayajuluafaf5a22006-09-19 10:28:00 -0700649static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
650 enum iscsi_param param, char *buf)
651{
652 struct ddb_entry *ddb_entry = sess->dd_data;
653 int len;
654
655 switch (param) {
656 case ISCSI_PARAM_TARGET_NAME:
657 len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
658 ddb_entry->iscsi_name);
659 break;
660 case ISCSI_PARAM_TPGT:
661 len = sprintf(buf, "%u\n", ddb_entry->tpgt);
662 break;
Vikas Chaudhary3b2bef12010-07-10 14:51:30 +0530663 case ISCSI_PARAM_TARGET_ALIAS:
664 len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
665 ddb_entry->iscsi_alias);
666 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700667 default:
668 return -ENOSYS;
669 }
670
671 return len;
672}
673
674static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
675 enum iscsi_param param, char *buf)
676{
677 struct iscsi_cls_session *session;
678 struct ddb_entry *ddb_entry;
679 int len;
680
681 session = iscsi_dev_to_session(conn->dev.parent);
682 ddb_entry = session->dd_data;
683
684 switch (param) {
685 case ISCSI_PARAM_CONN_PORT:
686 len = sprintf(buf, "%hu\n", ddb_entry->port);
687 break;
688 case ISCSI_PARAM_CONN_ADDRESS:
689 /* TODO: what are the ipv6 bits */
Harvey Harrison63779432008-10-31 00:56:00 -0700690 len = sprintf(buf, "%pI4\n", &ddb_entry->ip_addr);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700691 break;
692 default:
693 return -ENOSYS;
694 }
695
696 return len;
697}
698
Mike Christie2174a042007-05-30 12:57:10 -0500699static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
700 enum iscsi_tgt_dscvr type, uint32_t enable,
701 struct sockaddr *dst_addr)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700702{
703 struct scsi_qla_host *ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700704 struct sockaddr_in *addr;
705 struct sockaddr_in6 *addr6;
706 int ret = 0;
707
David Somayajuluafaf5a22006-09-19 10:28:00 -0700708 ha = (struct scsi_qla_host *) shost->hostdata;
709
710 switch (type) {
711 case ISCSI_TGT_DSCVR_SEND_TARGETS:
712 if (dst_addr->sa_family == AF_INET) {
713 addr = (struct sockaddr_in *)dst_addr;
714 if (qla4xxx_send_tgts(ha, (char *)&addr->sin_addr,
715 addr->sin_port) != QLA_SUCCESS)
716 ret = -EIO;
717 } else if (dst_addr->sa_family == AF_INET6) {
718 /*
719 * TODO: fix qla4xxx_send_tgts
720 */
721 addr6 = (struct sockaddr_in6 *)dst_addr;
722 if (qla4xxx_send_tgts(ha, (char *)&addr6->sin6_addr,
723 addr6->sin6_port) != QLA_SUCCESS)
724 ret = -EIO;
725 } else
726 ret = -ENOSYS;
727 break;
728 default:
729 ret = -ENOSYS;
730 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700731 return ret;
732}
733
734void qla4xxx_destroy_sess(struct ddb_entry *ddb_entry)
735{
736 if (!ddb_entry->sess)
737 return;
738
739 if (ddb_entry->conn) {
Mike Christie26974782007-12-13 12:43:29 -0600740 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700741 iscsi_remove_session(ddb_entry->sess);
742 }
743 iscsi_free_session(ddb_entry->sess);
744}
745
746int qla4xxx_add_sess(struct ddb_entry *ddb_entry)
747{
748 int err;
749
Vikas Chaudhary30387272011-03-21 03:34:32 -0700750 ddb_entry->sess->recovery_tmo = ql4xsess_recovery_tmo;
Vikas Chaudhary3013cea2010-07-30 14:25:46 +0530751
David Somayajuluafaf5a22006-09-19 10:28:00 -0700752 err = iscsi_add_session(ddb_entry->sess, ddb_entry->fw_ddb_index);
753 if (err) {
754 DEBUG2(printk(KERN_ERR "Could not add session.\n"));
755 return err;
756 }
757
Mike Christie5d91e202008-05-21 15:54:01 -0500758 ddb_entry->conn = iscsi_create_conn(ddb_entry->sess, 0, 0);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700759 if (!ddb_entry->conn) {
760 iscsi_remove_session(ddb_entry->sess);
761 DEBUG2(printk(KERN_ERR "Could not add connection.\n"));
762 return -ENOMEM;
763 }
Mike Christieb6359302008-01-31 13:36:44 -0600764
765 /* finally ready to go */
766 iscsi_unblock_session(ddb_entry->sess);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700767 return 0;
768}
769
770struct ddb_entry *qla4xxx_alloc_sess(struct scsi_qla_host *ha)
771{
772 struct ddb_entry *ddb_entry;
773 struct iscsi_cls_session *sess;
774
Mike Christie5d91e202008-05-21 15:54:01 -0500775 sess = iscsi_alloc_session(ha->host, &qla4xxx_iscsi_transport,
776 sizeof(struct ddb_entry));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700777 if (!sess)
778 return NULL;
779
780 ddb_entry = sess->dd_data;
781 memset(ddb_entry, 0, sizeof(*ddb_entry));
782 ddb_entry->ha = ha;
783 ddb_entry->sess = sess;
784 return ddb_entry;
785}
786
Mike Christie024f8012008-03-04 13:26:54 -0600787static void qla4xxx_scan_start(struct Scsi_Host *shost)
788{
789 struct scsi_qla_host *ha = shost_priv(shost);
790 struct ddb_entry *ddb_entry, *ddbtemp;
791
792 /* finish setup of sessions that were already setup in firmware */
793 list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
794 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE)
795 qla4xxx_add_sess(ddb_entry);
796 }
797}
798
David Somayajuluafaf5a22006-09-19 10:28:00 -0700799/*
800 * Timer routines
801 */
802
803static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
804 unsigned long interval)
805{
806 DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
807 __func__, ha->host->host_no));
808 init_timer(&ha->timer);
809 ha->timer.expires = jiffies + interval * HZ;
810 ha->timer.data = (unsigned long)ha;
811 ha->timer.function = (void (*)(unsigned long))func;
812 add_timer(&ha->timer);
813 ha->timer_active = 1;
814}
815
816static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
817{
818 del_timer_sync(&ha->timer);
819 ha->timer_active = 0;
820}
821
822/***
823 * qla4xxx_mark_device_missing - mark a device as missing.
824 * @ha: Pointer to host adapter structure.
825 * @ddb_entry: Pointer to device database entry
826 *
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530827 * This routine marks a device missing and close connection.
David Somayajuluafaf5a22006-09-19 10:28:00 -0700828 **/
829void qla4xxx_mark_device_missing(struct scsi_qla_host *ha,
830 struct ddb_entry *ddb_entry)
831{
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530832 if ((atomic_read(&ddb_entry->state) != DDB_STATE_DEAD)) {
833 atomic_set(&ddb_entry->state, DDB_STATE_MISSING);
834 DEBUG2(printk("scsi%ld: ddb [%d] marked MISSING\n",
835 ha->host_no, ddb_entry->fw_ddb_index));
836 } else
837 DEBUG2(printk("scsi%ld: ddb [%d] DEAD\n", ha->host_no,
838 ddb_entry->fw_ddb_index))
839
Mike Christieb6359302008-01-31 13:36:44 -0600840 iscsi_block_session(ddb_entry->sess);
Mike Christiee5bd7b52008-09-24 11:46:10 -0500841 iscsi_conn_error_event(ddb_entry->conn, ISCSI_ERR_CONN_FAILED);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700842}
843
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530844/**
845 * qla4xxx_mark_all_devices_missing - mark all devices as missing.
846 * @ha: Pointer to host adapter structure.
847 *
848 * This routine marks a device missing and resets the relogin retry count.
849 **/
850void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
851{
852 struct ddb_entry *ddb_entry, *ddbtemp;
853 list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
854 qla4xxx_mark_device_missing(ha, ddb_entry);
855 }
856}
857
David Somayajuluafaf5a22006-09-19 10:28:00 -0700858static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
859 struct ddb_entry *ddb_entry,
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -0700860 struct scsi_cmnd *cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700861{
862 struct srb *srb;
863
864 srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
865 if (!srb)
866 return srb;
867
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530868 kref_init(&srb->srb_ref);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700869 srb->ha = ha;
870 srb->ddb = ddb_entry;
871 srb->cmd = cmd;
872 srb->flags = 0;
Vikas Chaudhary53698872010-04-28 11:41:59 +0530873 CMD_SP(cmd) = (void *)srb;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700874
875 return srb;
876}
877
878static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
879{
880 struct scsi_cmnd *cmd = srb->cmd;
881
882 if (srb->flags & SRB_DMA_VALID) {
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +0900883 scsi_dma_unmap(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700884 srb->flags &= ~SRB_DMA_VALID;
885 }
Vikas Chaudhary53698872010-04-28 11:41:59 +0530886 CMD_SP(cmd) = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700887}
888
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530889void qla4xxx_srb_compl(struct kref *ref)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700890{
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530891 struct srb *srb = container_of(ref, struct srb, srb_ref);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700892 struct scsi_cmnd *cmd = srb->cmd;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530893 struct scsi_qla_host *ha = srb->ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700894
895 qla4xxx_srb_free_dma(ha, srb);
896
897 mempool_free(srb, ha->srb_mempool);
898
899 cmd->scsi_done(cmd);
900}
901
902/**
903 * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -0700904 * @host: scsi host
David Somayajuluafaf5a22006-09-19 10:28:00 -0700905 * @cmd: Pointer to Linux's SCSI command structure
David Somayajuluafaf5a22006-09-19 10:28:00 -0700906 *
907 * Remarks:
908 * This routine is invoked by Linux to send a SCSI command to the driver.
909 * The mid-level driver tries to ensure that queuecommand never gets
910 * invoked concurrently with itself or the interrupt handler (although
911 * the interrupt handler may call this routine as part of request-
912 * completion handling). Unfortunely, it sometimes calls the scheduler
913 * in interrupt context which is a big NO! NO!.
914 **/
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -0700915static int qla4xxx_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700916{
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -0700917 struct scsi_qla_host *ha = to_qla_host(host);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700918 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christie7fb19212008-01-31 13:36:45 -0600919 struct iscsi_cls_session *sess = ddb_entry->sess;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700920 struct srb *srb;
921 int rval;
922
Lalit Chandivade2232be02010-07-30 14:38:47 +0530923 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
924 if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
925 cmd->result = DID_NO_CONNECT << 16;
926 else
927 cmd->result = DID_REQUEUE << 16;
928 goto qc_fail_command;
929 }
930
Mike Christie7fb19212008-01-31 13:36:45 -0600931 if (!sess) {
932 cmd->result = DID_IMM_RETRY << 16;
933 goto qc_fail_command;
934 }
935
936 rval = iscsi_session_chkready(sess);
937 if (rval) {
938 cmd->result = rval;
939 goto qc_fail_command;
940 }
941
David Somayajuluafaf5a22006-09-19 10:28:00 -0700942 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
943 if (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD) {
944 cmd->result = DID_NO_CONNECT << 16;
945 goto qc_fail_command;
946 }
Mike Christiec5e98e92008-08-17 15:24:39 -0500947 return SCSI_MLQUEUE_TARGET_BUSY;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700948 }
949
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530950 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
951 test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
952 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
953 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
954 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
955 !test_bit(AF_ONLINE, &ha->flags) ||
956 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
David C Somayajulu477ffb92007-01-22 12:26:11 -0800957 goto qc_host_busy;
958
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -0700959 srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700960 if (!srb)
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -0700961 goto qc_host_busy;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700962
963 rval = qla4xxx_send_command_to_isp(ha, srb);
964 if (rval != QLA_SUCCESS)
965 goto qc_host_busy_free_sp;
966
David Somayajuluafaf5a22006-09-19 10:28:00 -0700967 return 0;
968
969qc_host_busy_free_sp:
970 qla4xxx_srb_free_dma(ha, srb);
971 mempool_free(srb, ha->srb_mempool);
972
David Somayajuluafaf5a22006-09-19 10:28:00 -0700973qc_host_busy:
974 return SCSI_MLQUEUE_HOST_BUSY;
975
976qc_fail_command:
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -0700977 cmd->scsi_done(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700978
979 return 0;
980}
981
982/**
983 * qla4xxx_mem_free - frees memory allocated to adapter
984 * @ha: Pointer to host adapter structure.
985 *
986 * Frees memory previously allocated by qla4xxx_mem_alloc
987 **/
988static void qla4xxx_mem_free(struct scsi_qla_host *ha)
989{
990 if (ha->queues)
991 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
992 ha->queues_dma);
993
994 ha->queues_len = 0;
995 ha->queues = NULL;
996 ha->queues_dma = 0;
997 ha->request_ring = NULL;
998 ha->request_dma = 0;
999 ha->response_ring = NULL;
1000 ha->response_dma = 0;
1001 ha->shadow_regs = NULL;
1002 ha->shadow_regs_dma = 0;
1003
1004 /* Free srb pool. */
1005 if (ha->srb_mempool)
1006 mempool_destroy(ha->srb_mempool);
1007
1008 ha->srb_mempool = NULL;
1009
1010 /* release io space registers */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301011 if (is_qla8022(ha)) {
1012 if (ha->nx_pcibase)
1013 iounmap(
1014 (struct device_reg_82xx __iomem *)ha->nx_pcibase);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301015 } else if (ha->reg)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001016 iounmap(ha->reg);
1017 pci_release_regions(ha->pdev);
1018}
1019
1020/**
1021 * qla4xxx_mem_alloc - allocates memory for use by adapter.
1022 * @ha: Pointer to host adapter structure
1023 *
1024 * Allocates DMA memory for request and response queues. Also allocates memory
1025 * for srbs.
1026 **/
1027static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
1028{
1029 unsigned long align;
1030
1031 /* Allocate contiguous block of DMA memory for queues. */
1032 ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
1033 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
1034 sizeof(struct shadow_regs) +
1035 MEM_ALIGN_VALUE +
1036 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
1037 ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
1038 &ha->queues_dma, GFP_KERNEL);
1039 if (ha->queues == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301040 ql4_printk(KERN_WARNING, ha,
1041 "Memory Allocation failed - queues.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001042
1043 goto mem_alloc_error_exit;
1044 }
1045 memset(ha->queues, 0, ha->queues_len);
1046
1047 /*
1048 * As per RISC alignment requirements -- the bus-address must be a
1049 * multiple of the request-ring size (in bytes).
1050 */
1051 align = 0;
1052 if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
1053 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
1054 (MEM_ALIGN_VALUE - 1));
1055
1056 /* Update request and response queue pointers. */
1057 ha->request_dma = ha->queues_dma + align;
1058 ha->request_ring = (struct queue_entry *) (ha->queues + align);
1059 ha->response_dma = ha->queues_dma + align +
1060 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
1061 ha->response_ring = (struct queue_entry *) (ha->queues + align +
1062 (REQUEST_QUEUE_DEPTH *
1063 QUEUE_SIZE));
1064 ha->shadow_regs_dma = ha->queues_dma + align +
1065 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
1066 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
1067 ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
1068 (REQUEST_QUEUE_DEPTH *
1069 QUEUE_SIZE) +
1070 (RESPONSE_QUEUE_DEPTH *
1071 QUEUE_SIZE));
1072
1073 /* Allocate memory for srb pool. */
1074 ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
1075 mempool_free_slab, srb_cachep);
1076 if (ha->srb_mempool == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301077 ql4_printk(KERN_WARNING, ha,
1078 "Memory Allocation failed - SRB Pool.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001079
1080 goto mem_alloc_error_exit;
1081 }
1082
1083 return QLA_SUCCESS;
1084
1085mem_alloc_error_exit:
1086 qla4xxx_mem_free(ha);
1087 return QLA_ERROR;
1088}
1089
1090/**
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301091 * qla4_8xxx_check_fw_alive - Check firmware health
1092 * @ha: Pointer to host adapter structure.
1093 *
1094 * Context: Interrupt
1095 **/
1096static void qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
1097{
1098 uint32_t fw_heartbeat_counter, halt_status;
1099
1100 fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
Lalit Chandivade2232be02010-07-30 14:38:47 +05301101 /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
1102 if (fw_heartbeat_counter == 0xffffffff) {
1103 DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
1104 "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
1105 ha->host_no, __func__));
1106 return;
1107 }
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301108
1109 if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
1110 ha->seconds_since_last_heartbeat++;
1111 /* FW not alive after 2 seconds */
1112 if (ha->seconds_since_last_heartbeat == 2) {
1113 ha->seconds_since_last_heartbeat = 0;
1114 halt_status = qla4_8xxx_rd_32(ha,
Vikas Chaudhary68d92eb2011-05-17 23:17:05 -07001115 QLA82XX_PEG_HALT_STATUS1);
1116
1117 ql4_printk(KERN_INFO, ha,
1118 "scsi(%ld): %s, Dumping hw/fw registers:\n "
1119 " PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2:"
1120 " 0x%x,\n PEG_NET_0_PC: 0x%x, PEG_NET_1_PC:"
1121 " 0x%x,\n PEG_NET_2_PC: 0x%x, PEG_NET_3_PC:"
1122 " 0x%x,\n PEG_NET_4_PC: 0x%x\n",
1123 ha->host_no, __func__, halt_status,
1124 qla4_8xxx_rd_32(ha,
1125 QLA82XX_PEG_HALT_STATUS2),
1126 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_0 +
1127 0x3c),
1128 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_1 +
1129 0x3c),
1130 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_2 +
1131 0x3c),
1132 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_3 +
1133 0x3c),
1134 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_4 +
1135 0x3c));
Nilesh Javali21033632010-07-30 14:28:07 +05301136
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301137 /* Since we cannot change dev_state in interrupt
1138 * context, set appropriate DPC flag then wakeup
1139 * DPC */
1140 if (halt_status & HALT_STATUS_UNRECOVERABLE)
1141 set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
1142 else {
1143 printk("scsi%ld: %s: detect abort needed!\n",
1144 ha->host_no, __func__);
1145 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1146 }
1147 qla4xxx_wake_dpc(ha);
Nilesh Javali21033632010-07-30 14:28:07 +05301148 qla4xxx_mailbox_premature_completion(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301149 }
Lalit Chandivade99457d72010-10-06 22:49:32 -07001150 } else
1151 ha->seconds_since_last_heartbeat = 0;
1152
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301153 ha->fw_heartbeat_counter = fw_heartbeat_counter;
1154}
1155
1156/**
1157 * qla4_8xxx_watchdog - Poll dev state
1158 * @ha: Pointer to host adapter structure.
1159 *
1160 * Context: Interrupt
1161 **/
1162void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
1163{
1164 uint32_t dev_state;
1165
1166 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
1167
1168 /* don't poll if reset is going on */
Lalit Chandivaded56a1f72010-12-02 22:12:45 -08001169 if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
1170 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
Vikas Chaudhary977f46a2011-05-17 23:17:08 -07001171 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags))) {
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301172 if (dev_state == QLA82XX_DEV_NEED_RESET &&
1173 !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
Vikas Chaudhary3930b8c2010-12-02 22:12:47 -08001174 if (!ql4xdontresethba) {
1175 ql4_printk(KERN_INFO, ha, "%s: HW State: "
1176 "NEED RESET!\n", __func__);
1177 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1178 qla4xxx_wake_dpc(ha);
1179 qla4xxx_mailbox_premature_completion(ha);
1180 }
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301181 } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
1182 !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
Vikas Chaudhary3930b8c2010-12-02 22:12:47 -08001183 ql4_printk(KERN_INFO, ha, "%s: HW State: NEED QUIES!\n",
1184 __func__);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301185 set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
1186 qla4xxx_wake_dpc(ha);
1187 } else {
1188 /* Check firmware health */
1189 qla4_8xxx_check_fw_alive(ha);
1190 }
1191 }
1192}
1193
1194/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07001195 * qla4xxx_timer - checks every second for work to do.
1196 * @ha: Pointer to host adapter structure.
1197 **/
1198static void qla4xxx_timer(struct scsi_qla_host *ha)
1199{
1200 struct ddb_entry *ddb_entry, *dtemp;
1201 int start_dpc = 0;
Lalit Chandivade2232be02010-07-30 14:38:47 +05301202 uint16_t w;
1203
1204 /* If we are in the middle of AER/EEH processing
1205 * skip any processing and reschedule the timer
1206 */
1207 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1208 mod_timer(&ha->timer, jiffies + HZ);
1209 return;
1210 }
1211
1212 /* Hardware read to trigger an EEH error during mailbox waits. */
1213 if (!pci_channel_offline(ha->pdev))
1214 pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001215
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301216 if (is_qla8022(ha)) {
1217 qla4_8xxx_watchdog(ha);
1218 }
1219
David Somayajuluafaf5a22006-09-19 10:28:00 -07001220 /* Search for relogin's to time-out and port down retry. */
1221 list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
1222 /* Count down time between sending relogins */
1223 if (adapter_up(ha) &&
1224 !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
1225 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
1226 if (atomic_read(&ddb_entry->retry_relogin_timer) !=
1227 INVALID_ENTRY) {
1228 if (atomic_read(&ddb_entry->retry_relogin_timer)
1229 == 0) {
1230 atomic_set(&ddb_entry->
1231 retry_relogin_timer,
1232 INVALID_ENTRY);
1233 set_bit(DPC_RELOGIN_DEVICE,
1234 &ha->dpc_flags);
1235 set_bit(DF_RELOGIN, &ddb_entry->flags);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301236 DEBUG2(printk("scsi%ld: %s: ddb [%d]"
David Somayajuluafaf5a22006-09-19 10:28:00 -07001237 " login device\n",
1238 ha->host_no, __func__,
1239 ddb_entry->fw_ddb_index));
1240 } else
1241 atomic_dec(&ddb_entry->
1242 retry_relogin_timer);
1243 }
1244 }
1245
1246 /* Wait for relogin to timeout */
1247 if (atomic_read(&ddb_entry->relogin_timer) &&
1248 (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
1249 /*
1250 * If the relogin times out and the device is
1251 * still NOT ONLINE then try and relogin again.
1252 */
1253 if (atomic_read(&ddb_entry->state) !=
1254 DDB_STATE_ONLINE &&
1255 ddb_entry->fw_ddb_device_state ==
1256 DDB_DS_SESSION_FAILED) {
1257 /* Reset retry relogin timer */
1258 atomic_inc(&ddb_entry->relogin_retry_count);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301259 DEBUG2(printk("scsi%ld: ddb [%d] relogin"
David Somayajuluafaf5a22006-09-19 10:28:00 -07001260 " timed out-retrying"
1261 " relogin (%d)\n",
1262 ha->host_no,
1263 ddb_entry->fw_ddb_index,
1264 atomic_read(&ddb_entry->
1265 relogin_retry_count))
1266 );
1267 start_dpc++;
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301268 DEBUG(printk("scsi%ld:%d:%d: ddb [%d] "
Justin P. Mattockcd09b2c2011-01-28 11:49:08 -08001269 "initiate relogin after"
David Somayajuluafaf5a22006-09-19 10:28:00 -07001270 " %d seconds\n",
1271 ha->host_no, ddb_entry->bus,
1272 ddb_entry->target,
1273 ddb_entry->fw_ddb_index,
1274 ddb_entry->default_time2wait + 4)
1275 );
1276
1277 atomic_set(&ddb_entry->retry_relogin_timer,
1278 ddb_entry->default_time2wait + 4);
1279 }
1280 }
1281 }
1282
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301283 if (!is_qla8022(ha)) {
1284 /* Check for heartbeat interval. */
1285 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
1286 ha->heartbeat_interval != 0) {
1287 ha->seconds_since_last_heartbeat++;
1288 if (ha->seconds_since_last_heartbeat >
1289 ha->heartbeat_interval + 2)
1290 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1291 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001292 }
1293
David Somayajuluafaf5a22006-09-19 10:28:00 -07001294 /* Wakeup the dpc routine for this adapter, if needed. */
Lalit Chandivade1b468072011-05-17 23:17:09 -07001295 if (start_dpc ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07001296 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1297 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
1298 test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301299 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07001300 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1301 test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301302 test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301303 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
1304 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
Lalit Chandivade1b468072011-05-17 23:17:09 -07001305 test_bit(DPC_AEN, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001306 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
1307 " - dpc flags = 0x%lx\n",
1308 ha->host_no, __func__, ha->dpc_flags));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301309 qla4xxx_wake_dpc(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001310 }
1311
1312 /* Reschedule timer thread to call us back in one second */
1313 mod_timer(&ha->timer, jiffies + HZ);
1314
1315 DEBUG2(ha->seconds_since_last_intr++);
1316}
1317
1318/**
1319 * qla4xxx_cmd_wait - waits for all outstanding commands to complete
1320 * @ha: Pointer to host adapter structure.
1321 *
1322 * This routine stalls the driver until all outstanding commands are returned.
1323 * Caller must release the Hardware Lock prior to calling this routine.
1324 **/
1325static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
1326{
1327 uint32_t index = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001328 unsigned long flags;
1329 struct scsi_cmnd *cmd;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001330
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301331 unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
1332
1333 DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
1334 "complete\n", WAIT_CMD_TOV));
1335
1336 while (!time_after_eq(jiffies, wtime)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001337 spin_lock_irqsave(&ha->hardware_lock, flags);
1338 /* Find a command that hasn't completed. */
1339 for (index = 0; index < ha->host->can_queue; index++) {
1340 cmd = scsi_host_find_tag(ha->host, index);
Mike Christiea1e00632010-10-26 05:45:30 -07001341 /*
1342 * We cannot just check if the index is valid,
1343 * becase if we are run from the scsi eh, then
1344 * the scsi/block layer is going to prevent
1345 * the tag from being released.
1346 */
1347 if (cmd != NULL && CMD_SP(cmd))
David Somayajuluafaf5a22006-09-19 10:28:00 -07001348 break;
1349 }
1350 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1351
1352 /* If No Commands are pending, wait is complete */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301353 if (index == ha->host->can_queue)
1354 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001355
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301356 msleep(1000);
1357 }
1358 /* If we timed out on waiting for commands to come back
1359 * return ERROR. */
1360 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001361}
1362
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301363int qla4xxx_hw_reset(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001364{
David Somayajuluafaf5a22006-09-19 10:28:00 -07001365 uint32_t ctrl_status;
David C Somayajulu477ffb92007-01-22 12:26:11 -08001366 unsigned long flags = 0;
1367
1368 DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001369
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301370 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
1371 return QLA_ERROR;
1372
David Somayajuluafaf5a22006-09-19 10:28:00 -07001373 spin_lock_irqsave(&ha->hardware_lock, flags);
1374
1375 /*
1376 * If the SCSI Reset Interrupt bit is set, clear it.
1377 * Otherwise, the Soft Reset won't work.
1378 */
1379 ctrl_status = readw(&ha->reg->ctrl_status);
1380 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
1381 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
1382
1383 /* Issue Soft Reset */
1384 writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
1385 readl(&ha->reg->ctrl_status);
1386
1387 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301388 return QLA_SUCCESS;
David C Somayajulu477ffb92007-01-22 12:26:11 -08001389}
1390
1391/**
1392 * qla4xxx_soft_reset - performs soft reset.
1393 * @ha: Pointer to host adapter structure.
1394 **/
1395int qla4xxx_soft_reset(struct scsi_qla_host *ha)
1396{
1397 uint32_t max_wait_time;
1398 unsigned long flags = 0;
Vikas Chaudharyf931c532010-10-06 22:48:07 -07001399 int status;
David C Somayajulu477ffb92007-01-22 12:26:11 -08001400 uint32_t ctrl_status;
1401
Vikas Chaudharyf931c532010-10-06 22:48:07 -07001402 status = qla4xxx_hw_reset(ha);
1403 if (status != QLA_SUCCESS)
1404 return status;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001405
Vikas Chaudharyf931c532010-10-06 22:48:07 -07001406 status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001407 /* Wait until the Network Reset Intr bit is cleared */
1408 max_wait_time = RESET_INTR_TOV;
1409 do {
1410 spin_lock_irqsave(&ha->hardware_lock, flags);
1411 ctrl_status = readw(&ha->reg->ctrl_status);
1412 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1413
1414 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
1415 break;
1416
1417 msleep(1000);
1418 } while ((--max_wait_time));
1419
1420 if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
1421 DEBUG2(printk(KERN_WARNING
1422 "scsi%ld: Network Reset Intr not cleared by "
1423 "Network function, clearing it now!\n",
1424 ha->host_no));
1425 spin_lock_irqsave(&ha->hardware_lock, flags);
1426 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
1427 readl(&ha->reg->ctrl_status);
1428 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1429 }
1430
1431 /* Wait until the firmware tells us the Soft Reset is done */
1432 max_wait_time = SOFT_RESET_TOV;
1433 do {
1434 spin_lock_irqsave(&ha->hardware_lock, flags);
1435 ctrl_status = readw(&ha->reg->ctrl_status);
1436 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1437
1438 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
1439 status = QLA_SUCCESS;
1440 break;
1441 }
1442
1443 msleep(1000);
1444 } while ((--max_wait_time));
1445
1446 /*
1447 * Also, make sure that the SCSI Reset Interrupt bit has been cleared
1448 * after the soft reset has taken place.
1449 */
1450 spin_lock_irqsave(&ha->hardware_lock, flags);
1451 ctrl_status = readw(&ha->reg->ctrl_status);
1452 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
1453 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
1454 readl(&ha->reg->ctrl_status);
1455 }
1456 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1457
1458 /* If soft reset fails then most probably the bios on other
1459 * function is also enabled.
1460 * Since the initialization is sequential the other fn
1461 * wont be able to acknowledge the soft reset.
1462 * Issue a force soft reset to workaround this scenario.
1463 */
1464 if (max_wait_time == 0) {
1465 /* Issue Force Soft Reset */
1466 spin_lock_irqsave(&ha->hardware_lock, flags);
1467 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
1468 readl(&ha->reg->ctrl_status);
1469 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1470 /* Wait until the firmware tells us the Soft Reset is done */
1471 max_wait_time = SOFT_RESET_TOV;
1472 do {
1473 spin_lock_irqsave(&ha->hardware_lock, flags);
1474 ctrl_status = readw(&ha->reg->ctrl_status);
1475 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1476
1477 if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
1478 status = QLA_SUCCESS;
1479 break;
1480 }
1481
1482 msleep(1000);
1483 } while ((--max_wait_time));
1484 }
1485
1486 return status;
1487}
1488
1489/**
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301490 * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001491 * @ha: Pointer to host adapter structure.
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301492 * @res: returned scsi status
David Somayajuluafaf5a22006-09-19 10:28:00 -07001493 *
1494 * This routine is called just prior to a HARD RESET to return all
1495 * outstanding commands back to the Operating System.
1496 * Caller should make sure that the following locks are released
1497 * before this calling routine: Hardware lock, and io_request_lock.
1498 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301499static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001500{
1501 struct srb *srb;
1502 int i;
1503 unsigned long flags;
1504
1505 spin_lock_irqsave(&ha->hardware_lock, flags);
1506 for (i = 0; i < ha->host->can_queue; i++) {
1507 srb = qla4xxx_del_from_active_array(ha, i);
1508 if (srb != NULL) {
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301509 srb->cmd->result = res;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301510 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001511 }
1512 }
1513 spin_unlock_irqrestore(&ha->hardware_lock, flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001514}
1515
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301516void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
1517{
1518 clear_bit(AF_ONLINE, &ha->flags);
1519
1520 /* Disable the board */
1521 ql4_printk(KERN_INFO, ha, "Disabling the board\n");
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301522
1523 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
1524 qla4xxx_mark_all_devices_missing(ha);
1525 clear_bit(AF_INIT_DONE, &ha->flags);
1526}
1527
David Somayajuluafaf5a22006-09-19 10:28:00 -07001528/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07001529 * qla4xxx_recover_adapter - recovers adapter after a fatal error
1530 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001531 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301532static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001533{
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301534 int status = QLA_ERROR;
1535 uint8_t reset_chip = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001536
1537 /* Stall incoming I/O until we are done */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301538 scsi_block_requests(ha->host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001539 clear_bit(AF_ONLINE, &ha->flags);
Mike Christie50a29ae2008-03-04 13:26:53 -06001540
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301541 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001542
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301543 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001544
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301545 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
1546 reset_chip = 1;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001547
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301548 /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
1549 * do not reset adapter, jump to initialize_adapter */
1550 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1551 status = QLA_SUCCESS;
1552 goto recover_ha_init_adapter;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001553 }
1554
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301555 /* For the ISP-82xx adapter, issue a stop_firmware if invoked
1556 * from eh_host_reset or ioctl module */
1557 if (is_qla8022(ha) && !reset_chip &&
1558 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
1559
1560 DEBUG2(ql4_printk(KERN_INFO, ha,
1561 "scsi%ld: %s - Performing stop_firmware...\n",
1562 ha->host_no, __func__));
1563 status = ha->isp_ops->reset_firmware(ha);
1564 if (status == QLA_SUCCESS) {
Nilesh Javali2bd1e2b2010-10-06 22:49:20 -07001565 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
1566 qla4xxx_cmd_wait(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301567 ha->isp_ops->disable_intrs(ha);
1568 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1569 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1570 } else {
1571 /* If the stop_firmware fails then
1572 * reset the entire chip */
1573 reset_chip = 1;
1574 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1575 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1576 }
1577 }
1578
1579 /* Issue full chip reset if recovering from a catastrophic error,
1580 * or if stop_firmware fails for ISP-82xx.
1581 * This is the default case for ISP-4xxx */
1582 if (!is_qla8022(ha) || reset_chip) {
Nilesh Javali2bd1e2b2010-10-06 22:49:20 -07001583 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
1584 qla4xxx_cmd_wait(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301585 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1586 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1587 DEBUG2(ql4_printk(KERN_INFO, ha,
1588 "scsi%ld: %s - Performing chip reset..\n",
1589 ha->host_no, __func__));
1590 status = ha->isp_ops->reset_chip(ha);
1591 }
1592
1593 /* Flush any pending ddb changed AENs */
1594 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1595
1596recover_ha_init_adapter:
1597 /* Upon successful firmware/chip reset, re-initialize the adapter */
1598 if (status == QLA_SUCCESS) {
1599 /* For ISP-4xxx, force function 1 to always initialize
1600 * before function 3 to prevent both funcions from
1601 * stepping on top of the other */
1602 if (!is_qla8022(ha) && (ha->mac_index == 3))
1603 ssleep(6);
1604
1605 /* NOTE: AF_ONLINE flag set upon successful completion of
1606 * qla4xxx_initialize_adapter */
1607 status = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
1608 }
1609
1610 /* Retry failed adapter initialization, if necessary
1611 * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
1612 * case to prevent ping-pong resets between functions */
1613 if (!test_bit(AF_ONLINE, &ha->flags) &&
1614 !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001615 /* Adapter initialization failed, see if we can retry
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301616 * resetting the ha.
1617 * Since we don't want to block the DPC for too long
1618 * with multiple resets in the same thread,
1619 * utilize DPC to retry */
David Somayajuluafaf5a22006-09-19 10:28:00 -07001620 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
1621 ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
1622 DEBUG2(printk("scsi%ld: recover adapter - retrying "
1623 "(%d) more times\n", ha->host_no,
1624 ha->retry_reset_ha_cnt));
1625 set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1626 status = QLA_ERROR;
1627 } else {
1628 if (ha->retry_reset_ha_cnt > 0) {
1629 /* Schedule another Reset HA--DPC will retry */
1630 ha->retry_reset_ha_cnt--;
1631 DEBUG2(printk("scsi%ld: recover adapter - "
1632 "retry remaining %d\n",
1633 ha->host_no,
1634 ha->retry_reset_ha_cnt));
1635 status = QLA_ERROR;
1636 }
1637
1638 if (ha->retry_reset_ha_cnt == 0) {
1639 /* Recover adapter retries have been exhausted.
1640 * Adapter DEAD */
1641 DEBUG2(printk("scsi%ld: recover adapter "
1642 "failed - board disabled\n",
1643 ha->host_no));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301644 qla4xxx_dead_adapter_cleanup(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001645 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1646 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301647 clear_bit(DPC_RESET_HA_FW_CONTEXT,
David Somayajuluafaf5a22006-09-19 10:28:00 -07001648 &ha->dpc_flags);
1649 status = QLA_ERROR;
1650 }
1651 }
1652 } else {
1653 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301654 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001655 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1656 }
1657
1658 ha->adapter_error_count++;
1659
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301660 if (test_bit(AF_ONLINE, &ha->flags))
1661 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001662
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301663 scsi_unblock_requests(ha->host);
1664
1665 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
1666 DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001667 status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301668
David Somayajuluafaf5a22006-09-19 10:28:00 -07001669 return status;
1670}
1671
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07001672static void qla4xxx_relogin_all_devices(struct scsi_qla_host *ha)
1673{
1674 struct ddb_entry *ddb_entry, *dtemp;
1675
1676 list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
1677 if ((atomic_read(&ddb_entry->state) == DDB_STATE_MISSING) ||
1678 (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD)) {
1679 if (ddb_entry->fw_ddb_device_state ==
1680 DDB_DS_SESSION_ACTIVE) {
1681 atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
1682 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
1683 " marked ONLINE\n", ha->host_no, __func__,
1684 ddb_entry->fw_ddb_index);
1685
1686 iscsi_unblock_session(ddb_entry->sess);
1687 } else
1688 qla4xxx_relogin_device(ha, ddb_entry);
1689 }
1690 }
1691}
1692
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301693void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
1694{
Lalit Chandivade1b468072011-05-17 23:17:09 -07001695 if (ha->dpc_thread)
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301696 queue_work(ha->dpc_thread, &ha->dpc_work);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301697}
1698
David Somayajuluafaf5a22006-09-19 10:28:00 -07001699/**
1700 * qla4xxx_do_dpc - dpc routine
1701 * @data: in our case pointer to adapter structure
1702 *
1703 * This routine is a task that is schedule by the interrupt handler
1704 * to perform the background processing for interrupts. We put it
1705 * on a task queue that is consumed whenever the scheduler runs; that's
1706 * so you can do anything (i.e. put the process to sleep etc). In fact,
1707 * the mid-level tries to sleep when it reaches the driver threshold
1708 * "host->can_queue". This can cause a panic if we were in our interrupt code.
1709 **/
David Howellsc4028952006-11-22 14:57:56 +00001710static void qla4xxx_do_dpc(struct work_struct *work)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001711{
David Howellsc4028952006-11-22 14:57:56 +00001712 struct scsi_qla_host *ha =
1713 container_of(work, struct scsi_qla_host, dpc_work);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001714 struct ddb_entry *ddb_entry, *dtemp;
David C Somayajulu477ffb92007-01-22 12:26:11 -08001715 int status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001716
David C Somayajuluf26b9042006-11-15 16:41:09 -08001717 DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301718 "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
1719 ha->host_no, __func__, ha->flags, ha->dpc_flags))
David Somayajuluafaf5a22006-09-19 10:28:00 -07001720
1721 /* Initialization not yet finished. Don't do anything yet. */
1722 if (!test_bit(AF_INIT_DONE, &ha->flags))
Lalit Chandivade1b468072011-05-17 23:17:09 -07001723 return;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001724
Lalit Chandivade2232be02010-07-30 14:38:47 +05301725 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1726 DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
1727 ha->host_no, __func__, ha->flags));
Lalit Chandivade1b468072011-05-17 23:17:09 -07001728 return;
Lalit Chandivade2232be02010-07-30 14:38:47 +05301729 }
1730
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301731 if (is_qla8022(ha)) {
1732 if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
1733 qla4_8xxx_idc_lock(ha);
1734 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
1735 QLA82XX_DEV_FAILED);
1736 qla4_8xxx_idc_unlock(ha);
1737 ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
1738 qla4_8xxx_device_state_handler(ha);
1739 }
1740 if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
1741 qla4_8xxx_need_qsnt_handler(ha);
1742 }
1743 }
1744
1745 if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
1746 (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07001747 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301748 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
1749 if (ql4xdontresethba) {
1750 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
1751 ha->host_no, __func__));
1752 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
1753 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1754 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1755 goto dpc_post_reset_ha;
1756 }
1757 if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
1758 test_bit(DPC_RESET_HA, &ha->dpc_flags))
1759 qla4xxx_recover_adapter(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001760
David C Somayajulu477ffb92007-01-22 12:26:11 -08001761 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001762 uint8_t wait_time = RESET_INTR_TOV;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001763
David Somayajuluafaf5a22006-09-19 10:28:00 -07001764 while ((readw(&ha->reg->ctrl_status) &
1765 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
1766 if (--wait_time == 0)
1767 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001768 msleep(1000);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001769 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001770 if (wait_time == 0)
1771 DEBUG2(printk("scsi%ld: %s: SR|FSR "
1772 "bit not cleared-- resetting\n",
1773 ha->host_no, __func__));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301774 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
David C Somayajulu477ffb92007-01-22 12:26:11 -08001775 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
1776 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301777 status = qla4xxx_recover_adapter(ha);
David C Somayajulu477ffb92007-01-22 12:26:11 -08001778 }
1779 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1780 if (status == QLA_SUCCESS)
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301781 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001782 }
1783 }
1784
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301785dpc_post_reset_ha:
David Somayajuluafaf5a22006-09-19 10:28:00 -07001786 /* ---- process AEN? --- */
1787 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
1788 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
1789
1790 /* ---- Get DHCP IP Address? --- */
1791 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
1792 qla4xxx_get_dhcp_ip_address(ha);
1793
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301794 /* ---- link change? --- */
1795 if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
1796 if (!test_bit(AF_LINK_UP, &ha->flags)) {
1797 /* ---- link down? --- */
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07001798 qla4xxx_mark_all_devices_missing(ha);
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301799 } else {
1800 /* ---- link up? --- *
1801 * F/W will auto login to all devices ONLY ONCE after
1802 * link up during driver initialization and runtime
1803 * fatal error recovery. Therefore, the driver must
1804 * manually relogin to devices when recovering from
1805 * connection failures, logouts, expired KATO, etc. */
1806
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07001807 qla4xxx_relogin_all_devices(ha);
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301808 }
1809 }
1810
David Somayajuluafaf5a22006-09-19 10:28:00 -07001811 /* ---- relogin device? --- */
1812 if (adapter_up(ha) &&
1813 test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
1814 list_for_each_entry_safe(ddb_entry, dtemp,
1815 &ha->ddb_list, list) {
1816 if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
1817 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
1818 qla4xxx_relogin_device(ha, ddb_entry);
1819
1820 /*
1821 * If mbx cmd times out there is no point
1822 * in continuing further.
1823 * With large no of targets this can hang
1824 * the system.
1825 */
1826 if (test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
1827 printk(KERN_WARNING "scsi%ld: %s: "
1828 "need to reset hba\n",
1829 ha->host_no, __func__);
1830 break;
1831 }
1832 }
1833 }
Vikas Chaudhary0753b482010-07-30 14:27:19 +05301834
David Somayajuluafaf5a22006-09-19 10:28:00 -07001835}
1836
1837/**
1838 * qla4xxx_free_adapter - release the adapter
1839 * @ha: pointer to adapter structure
1840 **/
1841static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
1842{
1843
1844 if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
1845 /* Turn-off interrupts on the card. */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301846 ha->isp_ops->disable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001847 }
1848
David Somayajuluafaf5a22006-09-19 10:28:00 -07001849 /* Remove timer thread, if present */
1850 if (ha->timer_active)
1851 qla4xxx_stop_timer(ha);
1852
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301853 /* Kill the kernel thread for this host */
1854 if (ha->dpc_thread)
1855 destroy_workqueue(ha->dpc_thread);
1856
1857 /* Put firmware in known state */
1858 ha->isp_ops->reset_firmware(ha);
1859
1860 if (is_qla8022(ha)) {
1861 qla4_8xxx_idc_lock(ha);
1862 qla4_8xxx_clear_drv_active(ha);
1863 qla4_8xxx_idc_unlock(ha);
1864 }
1865
David Somayajuluafaf5a22006-09-19 10:28:00 -07001866 /* Detach interrupts */
1867 if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301868 qla4xxx_free_irqs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001869
David C Somayajulubee4fe82007-05-23 18:03:32 -07001870 /* free extra memory */
1871 qla4xxx_mem_free(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301872}
David C Somayajulubee4fe82007-05-23 18:03:32 -07001873
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301874int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
1875{
1876 int status = 0;
1877 uint8_t revision_id;
1878 unsigned long mem_base, mem_len, db_base, db_len;
1879 struct pci_dev *pdev = ha->pdev;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001880
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301881 status = pci_request_regions(pdev, DRIVER_NAME);
1882 if (status) {
1883 printk(KERN_WARNING
1884 "scsi(%ld) Failed to reserve PIO regions (%s) "
1885 "status=%d\n", ha->host_no, pci_name(pdev), status);
1886 goto iospace_error_exit;
1887 }
1888
1889 pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
1890 DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
1891 __func__, revision_id));
1892 ha->revision_id = revision_id;
1893
1894 /* remap phys address */
1895 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
1896 mem_len = pci_resource_len(pdev, 0);
1897 DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
1898 __func__, mem_base, mem_len));
1899
1900 /* mapping of pcibase pointer */
1901 ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
1902 if (!ha->nx_pcibase) {
1903 printk(KERN_ERR
1904 "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
1905 pci_release_regions(ha->pdev);
1906 goto iospace_error_exit;
1907 }
1908
1909 /* Mapping of IO base pointer, door bell read and write pointer */
1910
1911 /* mapping of IO base pointer */
1912 ha->qla4_8xxx_reg =
1913 (struct device_reg_82xx __iomem *)((uint8_t *)ha->nx_pcibase +
1914 0xbc000 + (ha->pdev->devfn << 11));
1915
1916 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
1917 db_len = pci_resource_len(pdev, 4);
1918
Shyam Sundar2657c802010-10-06 22:50:29 -07001919 ha->nx_db_wr_ptr = (ha->pdev->devfn == 4 ? QLA82XX_CAM_RAM_DB1 :
1920 QLA82XX_CAM_RAM_DB2);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301921
Shyam Sundar2657c802010-10-06 22:50:29 -07001922 return 0;
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301923iospace_error_exit:
1924 return -ENOMEM;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001925}
1926
1927/***
1928 * qla4xxx_iospace_config - maps registers
1929 * @ha: pointer to adapter structure
1930 *
1931 * This routines maps HBA's registers from the pci address space
1932 * into the kernel virtual address space for memory mapped i/o.
1933 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301934int qla4xxx_iospace_config(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001935{
1936 unsigned long pio, pio_len, pio_flags;
1937 unsigned long mmio, mmio_len, mmio_flags;
1938
1939 pio = pci_resource_start(ha->pdev, 0);
1940 pio_len = pci_resource_len(ha->pdev, 0);
1941 pio_flags = pci_resource_flags(ha->pdev, 0);
1942 if (pio_flags & IORESOURCE_IO) {
1943 if (pio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301944 ql4_printk(KERN_WARNING, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07001945 "Invalid PCI I/O region size\n");
1946 pio = 0;
1947 }
1948 } else {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301949 ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001950 pio = 0;
1951 }
1952
1953 /* Use MMIO operations for all accesses. */
1954 mmio = pci_resource_start(ha->pdev, 1);
1955 mmio_len = pci_resource_len(ha->pdev, 1);
1956 mmio_flags = pci_resource_flags(ha->pdev, 1);
1957
1958 if (!(mmio_flags & IORESOURCE_MEM)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301959 ql4_printk(KERN_ERR, ha,
1960 "region #0 not an MMIO resource, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001961
1962 goto iospace_error_exit;
1963 }
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301964
David Somayajuluafaf5a22006-09-19 10:28:00 -07001965 if (mmio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301966 ql4_printk(KERN_ERR, ha,
1967 "Invalid PCI mem region size, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001968 goto iospace_error_exit;
1969 }
1970
1971 if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301972 ql4_printk(KERN_WARNING, ha,
1973 "Failed to reserve PIO/MMIO regions\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001974
1975 goto iospace_error_exit;
1976 }
1977
1978 ha->pio_address = pio;
1979 ha->pio_length = pio_len;
1980 ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
1981 if (!ha->reg) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301982 ql4_printk(KERN_ERR, ha,
1983 "cannot remap MMIO, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001984
1985 goto iospace_error_exit;
1986 }
1987
1988 return 0;
1989
1990iospace_error_exit:
1991 return -ENOMEM;
1992}
1993
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301994static struct isp_operations qla4xxx_isp_ops = {
1995 .iospace_config = qla4xxx_iospace_config,
1996 .pci_config = qla4xxx_pci_config,
1997 .disable_intrs = qla4xxx_disable_intrs,
1998 .enable_intrs = qla4xxx_enable_intrs,
1999 .start_firmware = qla4xxx_start_firmware,
2000 .intr_handler = qla4xxx_intr_handler,
2001 .interrupt_service_routine = qla4xxx_interrupt_service_routine,
2002 .reset_chip = qla4xxx_soft_reset,
2003 .reset_firmware = qla4xxx_hw_reset,
2004 .queue_iocb = qla4xxx_queue_iocb,
2005 .complete_iocb = qla4xxx_complete_iocb,
2006 .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
2007 .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
2008 .get_sys_info = qla4xxx_get_sys_info,
2009};
2010
2011static struct isp_operations qla4_8xxx_isp_ops = {
2012 .iospace_config = qla4_8xxx_iospace_config,
2013 .pci_config = qla4_8xxx_pci_config,
2014 .disable_intrs = qla4_8xxx_disable_intrs,
2015 .enable_intrs = qla4_8xxx_enable_intrs,
2016 .start_firmware = qla4_8xxx_load_risc,
2017 .intr_handler = qla4_8xxx_intr_handler,
2018 .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
2019 .reset_chip = qla4_8xxx_isp_reset,
2020 .reset_firmware = qla4_8xxx_stop_firmware,
2021 .queue_iocb = qla4_8xxx_queue_iocb,
2022 .complete_iocb = qla4_8xxx_complete_iocb,
2023 .rd_shdw_req_q_out = qla4_8xxx_rd_shdw_req_q_out,
2024 .rd_shdw_rsp_q_in = qla4_8xxx_rd_shdw_rsp_q_in,
2025 .get_sys_info = qla4_8xxx_get_sys_info,
2026};
2027
2028uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
2029{
2030 return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
2031}
2032
2033uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
2034{
2035 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
2036}
2037
2038uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
2039{
2040 return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
2041}
2042
2043uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
2044{
2045 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
2046}
2047
David Somayajuluafaf5a22006-09-19 10:28:00 -07002048/**
2049 * qla4xxx_probe_adapter - callback function to probe HBA
2050 * @pdev: pointer to pci_dev structure
2051 * @pci_device_id: pointer to pci_device entry
2052 *
2053 * This routine will probe for Qlogic 4xxx iSCSI host adapters.
2054 * It returns zero if successful. It also initializes all data necessary for
2055 * the driver.
2056 **/
2057static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
2058 const struct pci_device_id *ent)
2059{
2060 int ret = -ENODEV, status;
2061 struct Scsi_Host *host;
2062 struct scsi_qla_host *ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002063 uint8_t init_retry_count = 0;
2064 char buf[34];
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302065 struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
Prasanna Mumbaif9880e72011-03-21 03:34:26 -07002066 uint32_t dev_state;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002067
2068 if (pci_enable_device(pdev))
2069 return -1;
2070
2071 host = scsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha));
2072 if (host == NULL) {
2073 printk(KERN_WARNING
2074 "qla4xxx: Couldn't allocate host from scsi layer!\n");
2075 goto probe_disable_device;
2076 }
2077
2078 /* Clear our data area */
2079 ha = (struct scsi_qla_host *) host->hostdata;
2080 memset(ha, 0, sizeof(*ha));
2081
2082 /* Save the information from PCI BIOS. */
2083 ha->pdev = pdev;
2084 ha->host = host;
2085 ha->host_no = host->host_no;
2086
Lalit Chandivade2232be02010-07-30 14:38:47 +05302087 pci_enable_pcie_error_reporting(pdev);
2088
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302089 /* Setup Runtime configurable options */
2090 if (is_qla8022(ha)) {
2091 ha->isp_ops = &qla4_8xxx_isp_ops;
2092 rwlock_init(&ha->hw_lock);
2093 ha->qdr_sn_window = -1;
2094 ha->ddr_mn_window = -1;
2095 ha->curr_window = 255;
2096 ha->func_num = PCI_FUNC(ha->pdev->devfn);
2097 nx_legacy_intr = &legacy_intr[ha->func_num];
2098 ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
2099 ha->nx_legacy_intr.tgt_status_reg =
2100 nx_legacy_intr->tgt_status_reg;
2101 ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
2102 ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
2103 } else {
2104 ha->isp_ops = &qla4xxx_isp_ops;
2105 }
2106
Lalit Chandivade2232be02010-07-30 14:38:47 +05302107 /* Set EEH reset type to fundamental if required by hba */
2108 if (is_qla8022(ha))
2109 pdev->needs_freset = 1;
2110
David Somayajuluafaf5a22006-09-19 10:28:00 -07002111 /* Configure PCI I/O space. */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302112 ret = ha->isp_ops->iospace_config(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002113 if (ret)
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302114 goto probe_failed_ioconfig;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002115
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302116 ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
David Somayajuluafaf5a22006-09-19 10:28:00 -07002117 pdev->device, pdev->irq, ha->reg);
2118
2119 qla4xxx_config_dma_addressing(ha);
2120
2121 /* Initialize lists and spinlocks. */
2122 INIT_LIST_HEAD(&ha->ddb_list);
2123 INIT_LIST_HEAD(&ha->free_srb_q);
2124
2125 mutex_init(&ha->mbox_sem);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302126 init_completion(&ha->mbx_intr_comp);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002127
2128 spin_lock_init(&ha->hardware_lock);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002129
2130 /* Allocate dma buffers */
2131 if (qla4xxx_mem_alloc(ha)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302132 ql4_printk(KERN_WARNING, ha,
2133 "[ERROR] Failed to allocate memory for adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002134
2135 ret = -ENOMEM;
2136 goto probe_failed;
2137 }
2138
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302139 if (is_qla8022(ha))
2140 (void) qla4_8xxx_get_flash_info(ha);
2141
David Somayajuluafaf5a22006-09-19 10:28:00 -07002142 /*
2143 * Initialize the Host adapter request/response queues and
2144 * firmware
2145 * NOTE: interrupts enabled upon successful completion
2146 */
2147 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302148 while ((!test_bit(AF_ONLINE, &ha->flags)) &&
2149 init_retry_count++ < MAX_INIT_RETRIES) {
Prasanna Mumbaif9880e72011-03-21 03:34:26 -07002150
2151 if (is_qla8022(ha)) {
2152 qla4_8xxx_idc_lock(ha);
2153 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
2154 qla4_8xxx_idc_unlock(ha);
2155 if (dev_state == QLA82XX_DEV_FAILED) {
2156 ql4_printk(KERN_WARNING, ha, "%s: don't retry "
2157 "initialize adapter. H/W is in failed state\n",
2158 __func__);
2159 break;
2160 }
2161 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002162 DEBUG2(printk("scsi: %s: retrying adapter initialization "
2163 "(%d)\n", __func__, init_retry_count));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302164
2165 if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
2166 continue;
2167
David Somayajuluafaf5a22006-09-19 10:28:00 -07002168 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
2169 }
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302170
2171 if (!test_bit(AF_ONLINE, &ha->flags)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302172 ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002173
Lalit Chandivadefe998522010-12-02 22:12:36 -08002174 if (is_qla8022(ha) && ql4xdontresethba) {
2175 /* Put the device in failed state. */
2176 DEBUG2(printk(KERN_ERR "HW STATE: FAILED\n"));
2177 qla4_8xxx_idc_lock(ha);
2178 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2179 QLA82XX_DEV_FAILED);
2180 qla4_8xxx_idc_unlock(ha);
2181 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002182 ret = -ENODEV;
2183 goto probe_failed;
2184 }
2185
2186 host->cmd_per_lun = 3;
2187 host->max_channel = 0;
2188 host->max_lun = MAX_LUNS - 1;
2189 host->max_id = MAX_TARGETS;
2190 host->max_cmd_len = IOCB_MAX_CDB_LEN;
2191 host->can_queue = MAX_SRBS ;
2192 host->transportt = qla4xxx_scsi_transport;
2193
2194 ret = scsi_init_shared_tag_map(host, MAX_SRBS);
2195 if (ret) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302196 ql4_printk(KERN_WARNING, ha,
2197 "scsi_init_shared_tag_map failed\n");
2198 goto probe_failed;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002199 }
2200
2201 /* Startup the kernel thread for this host adapter. */
2202 DEBUG2(printk("scsi: %s: Starting kernel thread for "
2203 "qla4xxx_dpc\n", __func__));
2204 sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
2205 ha->dpc_thread = create_singlethread_workqueue(buf);
2206 if (!ha->dpc_thread) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302207 ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002208 ret = -ENODEV;
2209 goto probe_failed;
2210 }
David Howellsc4028952006-11-22 14:57:56 +00002211 INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002212
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302213 /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
2214 * (which is called indirectly by qla4xxx_initialize_adapter),
2215 * so that irqs will be registered after crbinit but before
2216 * mbx_intr_enable.
2217 */
2218 if (!is_qla8022(ha)) {
2219 ret = qla4xxx_request_irqs(ha);
2220 if (ret) {
2221 ql4_printk(KERN_WARNING, ha, "Failed to reserve "
2222 "interrupt %d already in use.\n", pdev->irq);
2223 goto probe_failed;
2224 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002225 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002226
Lalit Chandivade2232be02010-07-30 14:38:47 +05302227 pci_save_state(ha->pdev);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302228 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002229
2230 /* Start timer thread. */
2231 qla4xxx_start_timer(ha, qla4xxx_timer, 1);
2232
2233 set_bit(AF_INIT_DONE, &ha->flags);
2234
2235 pci_set_drvdata(pdev, ha);
2236
2237 ret = scsi_add_host(host, &pdev->dev);
2238 if (ret)
2239 goto probe_failed;
2240
David Somayajuluafaf5a22006-09-19 10:28:00 -07002241 printk(KERN_INFO
2242 " QLogic iSCSI HBA Driver version: %s\n"
2243 " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
2244 qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
2245 ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
2246 ha->patch_number, ha->build_number);
Mike Christie921601b2008-01-31 13:36:49 -06002247 scsi_scan_host(host);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05002248
2249 qla4xxx_create_ifaces(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002250 return 0;
2251
David Somayajuluafaf5a22006-09-19 10:28:00 -07002252probe_failed:
2253 qla4xxx_free_adapter(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302254
2255probe_failed_ioconfig:
Lalit Chandivade2232be02010-07-30 14:38:47 +05302256 pci_disable_pcie_error_reporting(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002257 scsi_host_put(ha->host);
2258
2259probe_disable_device:
2260 pci_disable_device(pdev);
2261
2262 return ret;
2263}
2264
2265/**
Karen Higgins7eece5a2011-03-21 03:34:29 -07002266 * qla4xxx_prevent_other_port_reinit - prevent other port from re-initialize
2267 * @ha: pointer to adapter structure
2268 *
2269 * Mark the other ISP-4xxx port to indicate that the driver is being removed,
2270 * so that the other port will not re-initialize while in the process of
2271 * removing the ha due to driver unload or hba hotplug.
2272 **/
2273static void qla4xxx_prevent_other_port_reinit(struct scsi_qla_host *ha)
2274{
2275 struct scsi_qla_host *other_ha = NULL;
2276 struct pci_dev *other_pdev = NULL;
2277 int fn = ISP4XXX_PCI_FN_2;
2278
2279 /*iscsi function numbers for ISP4xxx is 1 and 3*/
2280 if (PCI_FUNC(ha->pdev->devfn) & BIT_1)
2281 fn = ISP4XXX_PCI_FN_1;
2282
2283 other_pdev =
2284 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
2285 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
2286 fn));
2287
2288 /* Get other_ha if other_pdev is valid and state is enable*/
2289 if (other_pdev) {
2290 if (atomic_read(&other_pdev->enable_cnt)) {
2291 other_ha = pci_get_drvdata(other_pdev);
2292 if (other_ha) {
2293 set_bit(AF_HA_REMOVAL, &other_ha->flags);
2294 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: "
2295 "Prevent %s reinit\n", __func__,
2296 dev_name(&other_ha->pdev->dev)));
2297 }
2298 }
2299 pci_dev_put(other_pdev);
2300 }
2301}
2302
2303/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002304 * qla4xxx_remove_adapter - calback function to remove adapter.
2305 * @pci_dev: PCI device pointer
2306 **/
2307static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
2308{
2309 struct scsi_qla_host *ha;
2310
2311 ha = pci_get_drvdata(pdev);
2312
Karen Higgins7eece5a2011-03-21 03:34:29 -07002313 if (!is_qla8022(ha))
2314 qla4xxx_prevent_other_port_reinit(ha);
David C Somayajulubee4fe82007-05-23 18:03:32 -07002315
David Somayajuluafaf5a22006-09-19 10:28:00 -07002316 /* remove devs from iscsi_sessions to scsi_devices */
2317 qla4xxx_free_ddb_list(ha);
2318
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05002319 /* destroy iface from sysfs */
2320 qla4xxx_destroy_ifaces(ha);
2321
David Somayajuluafaf5a22006-09-19 10:28:00 -07002322 scsi_remove_host(ha->host);
2323
2324 qla4xxx_free_adapter(ha);
2325
2326 scsi_host_put(ha->host);
2327
Lalit Chandivade2232be02010-07-30 14:38:47 +05302328 pci_disable_pcie_error_reporting(pdev);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302329 pci_disable_device(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002330 pci_set_drvdata(pdev, NULL);
2331}
2332
2333/**
2334 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
2335 * @ha: HA context
2336 *
2337 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
2338 * supported addressing method.
2339 */
Adrian Bunk47975472007-04-26 00:35:16 -07002340static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002341{
2342 int retval;
2343
2344 /* Update our PCI device dma_mask for full 64 bit mask */
Yang Hongyang6a355282009-04-06 19:01:13 -07002345 if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
2346 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002347 dev_dbg(&ha->pdev->dev,
2348 "Failed to set 64 bit PCI consistent mask; "
2349 "using 32 bit.\n");
2350 retval = pci_set_consistent_dma_mask(ha->pdev,
Yang Hongyang284901a92009-04-06 19:01:15 -07002351 DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07002352 }
2353 } else
Yang Hongyang284901a92009-04-06 19:01:15 -07002354 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07002355}
2356
2357static int qla4xxx_slave_alloc(struct scsi_device *sdev)
2358{
2359 struct iscsi_cls_session *sess = starget_to_session(sdev->sdev_target);
2360 struct ddb_entry *ddb = sess->dd_data;
Vikas Chaudhary8bb40332011-03-21 03:34:31 -07002361 int queue_depth = QL4_DEF_QDEPTH;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002362
2363 sdev->hostdata = ddb;
2364 sdev->tagged_supported = 1;
Vikas Chaudhary8bb40332011-03-21 03:34:31 -07002365
2366 if (ql4xmaxqdepth != 0 && ql4xmaxqdepth <= 0xffffU)
2367 queue_depth = ql4xmaxqdepth;
2368
2369 scsi_activate_tcq(sdev, queue_depth);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002370 return 0;
2371}
2372
2373static int qla4xxx_slave_configure(struct scsi_device *sdev)
2374{
2375 sdev->tagged_supported = 1;
2376 return 0;
2377}
2378
2379static void qla4xxx_slave_destroy(struct scsi_device *sdev)
2380{
2381 scsi_deactivate_tcq(sdev, 1);
2382}
2383
2384/**
2385 * qla4xxx_del_from_active_array - returns an active srb
2386 * @ha: Pointer to host adapter structure.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02002387 * @index: index into the active_array
David Somayajuluafaf5a22006-09-19 10:28:00 -07002388 *
2389 * This routine removes and returns the srb at the specified index
2390 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302391struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
2392 uint32_t index)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002393{
2394 struct srb *srb = NULL;
Vikas Chaudhary53698872010-04-28 11:41:59 +05302395 struct scsi_cmnd *cmd = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002396
Vikas Chaudhary53698872010-04-28 11:41:59 +05302397 cmd = scsi_host_find_tag(ha->host, index);
2398 if (!cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002399 return srb;
2400
Vikas Chaudhary53698872010-04-28 11:41:59 +05302401 srb = (struct srb *)CMD_SP(cmd);
2402 if (!srb)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002403 return srb;
2404
2405 /* update counters */
2406 if (srb->flags & SRB_DMA_VALID) {
2407 ha->req_q_count += srb->iocb_cnt;
2408 ha->iocb_cnt -= srb->iocb_cnt;
2409 if (srb->cmd)
Vikas Chaudhary53698872010-04-28 11:41:59 +05302410 srb->cmd->host_scribble =
2411 (unsigned char *)(unsigned long) MAX_SRBS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002412 }
2413 return srb;
2414}
2415
2416/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002417 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302418 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07002419 * @cmd: Scsi Command to wait on.
2420 *
2421 * This routine waits for the command to be returned by the Firmware
2422 * for some max time.
2423 **/
2424static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
2425 struct scsi_cmnd *cmd)
2426{
2427 int done = 0;
2428 struct srb *rp;
2429 uint32_t max_wait_time = EH_WAIT_CMD_TOV;
Lalit Chandivade2232be02010-07-30 14:38:47 +05302430 int ret = SUCCESS;
2431
2432 /* Dont wait on command if PCI error is being handled
2433 * by PCI AER driver
2434 */
2435 if (unlikely(pci_channel_offline(ha->pdev)) ||
2436 (test_bit(AF_EEH_BUSY, &ha->flags))) {
2437 ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
2438 ha->host_no, __func__);
2439 return ret;
2440 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002441
2442 do {
2443 /* Checking to see if its returned to OS */
Vikas Chaudhary53698872010-04-28 11:41:59 +05302444 rp = (struct srb *) CMD_SP(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002445 if (rp == NULL) {
2446 done++;
2447 break;
2448 }
2449
2450 msleep(2000);
2451 } while (max_wait_time--);
2452
2453 return done;
2454}
2455
2456/**
2457 * qla4xxx_wait_for_hba_online - waits for HBA to come online
2458 * @ha: Pointer to host adapter structure
2459 **/
2460static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
2461{
2462 unsigned long wait_online;
2463
Vikas Chaudharyf581a3f2010-10-06 22:47:48 -07002464 wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002465 while (time_before(jiffies, wait_online)) {
2466
2467 if (adapter_up(ha))
2468 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002469
2470 msleep(2000);
2471 }
2472
2473 return QLA_ERROR;
2474}
2475
2476/**
Mike Christiece545032008-02-29 18:25:20 -06002477 * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02002478 * @ha: pointer to HBA
David Somayajuluafaf5a22006-09-19 10:28:00 -07002479 * @t: target id
2480 * @l: lun id
2481 *
2482 * This function waits for all outstanding commands to a lun to complete. It
2483 * returns 0 if all pending commands are returned and 1 otherwise.
2484 **/
Mike Christiece545032008-02-29 18:25:20 -06002485static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
2486 struct scsi_target *stgt,
2487 struct scsi_device *sdev)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002488{
2489 int cnt;
2490 int status = 0;
2491 struct scsi_cmnd *cmd;
2492
2493 /*
Mike Christiece545032008-02-29 18:25:20 -06002494 * Waiting for all commands for the designated target or dev
2495 * in the active array
David Somayajuluafaf5a22006-09-19 10:28:00 -07002496 */
2497 for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
2498 cmd = scsi_host_find_tag(ha->host, cnt);
Mike Christiece545032008-02-29 18:25:20 -06002499 if (cmd && stgt == scsi_target(cmd->device) &&
2500 (!sdev || sdev == cmd->device)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002501 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
2502 status++;
2503 break;
2504 }
2505 }
2506 }
2507 return status;
2508}
2509
2510/**
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302511 * qla4xxx_eh_abort - callback for abort task.
2512 * @cmd: Pointer to Linux's SCSI command structure
2513 *
2514 * This routine is called by the Linux OS to abort the specified
2515 * command.
2516 **/
2517static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
2518{
2519 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2520 unsigned int id = cmd->device->id;
2521 unsigned int lun = cmd->device->lun;
Mike Christie92b3e5b2010-10-06 22:51:17 -07002522 unsigned long flags;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302523 struct srb *srb = NULL;
2524 int ret = SUCCESS;
2525 int wait = 0;
2526
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302527 ql4_printk(KERN_INFO, ha,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002528 "scsi%ld:%d:%d: Abort command issued cmd=%p\n",
2529 ha->host_no, id, lun, cmd);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302530
Mike Christie92b3e5b2010-10-06 22:51:17 -07002531 spin_lock_irqsave(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302532 srb = (struct srb *) CMD_SP(cmd);
Mike Christie92b3e5b2010-10-06 22:51:17 -07002533 if (!srb) {
2534 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302535 return SUCCESS;
Mike Christie92b3e5b2010-10-06 22:51:17 -07002536 }
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302537 kref_get(&srb->srb_ref);
Mike Christie92b3e5b2010-10-06 22:51:17 -07002538 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302539
2540 if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
2541 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
2542 ha->host_no, id, lun));
2543 ret = FAILED;
2544 } else {
2545 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
2546 ha->host_no, id, lun));
2547 wait = 1;
2548 }
2549
2550 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
2551
2552 /* Wait for command to complete */
2553 if (wait) {
2554 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
2555 DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
2556 ha->host_no, id, lun));
2557 ret = FAILED;
2558 }
2559 }
2560
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302561 ql4_printk(KERN_INFO, ha,
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302562 "scsi%ld:%d:%d: Abort command - %s\n",
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002563 ha->host_no, id, lun, (ret == SUCCESS) ? "succeeded" : "failed");
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302564
2565 return ret;
2566}
2567
2568/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002569 * qla4xxx_eh_device_reset - callback for target reset.
2570 * @cmd: Pointer to Linux's SCSI command structure
2571 *
2572 * This routine is called by the Linux OS to reset all luns on the
2573 * specified target.
2574 **/
2575static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
2576{
2577 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2578 struct ddb_entry *ddb_entry = cmd->device->hostdata;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002579 int ret = FAILED, stat;
2580
Karen Higgins612f7342009-07-15 15:03:01 -05002581 if (!ddb_entry)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002582 return ret;
2583
Mike Christiec01be6d2010-07-22 16:59:49 +05302584 ret = iscsi_block_scsi_eh(cmd);
2585 if (ret)
2586 return ret;
2587 ret = FAILED;
2588
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302589 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002590 "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
2591 cmd->device->channel, cmd->device->id, cmd->device->lun);
2592
2593 DEBUG2(printk(KERN_INFO
2594 "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
2595 "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
Jens Axboe242f9dc2008-09-14 05:55:09 -07002596 cmd, jiffies, cmd->request->timeout / HZ,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002597 ha->dpc_flags, cmd->result, cmd->allowed));
2598
2599 /* FIXME: wait for hba to go online */
2600 stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
2601 if (stat != QLA_SUCCESS) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302602 ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002603 goto eh_dev_reset_done;
2604 }
2605
Mike Christiece545032008-02-29 18:25:20 -06002606 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2607 cmd->device)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302608 ql4_printk(KERN_INFO, ha,
Mike Christiece545032008-02-29 18:25:20 -06002609 "DEVICE RESET FAILED - waiting for "
2610 "commands.\n");
2611 goto eh_dev_reset_done;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002612 }
2613
David C Somayajulu9d562912008-03-19 11:23:03 -07002614 /* Send marker. */
2615 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2616 MM_LUN_RESET) != QLA_SUCCESS)
2617 goto eh_dev_reset_done;
2618
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302619 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002620 "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
2621 ha->host_no, cmd->device->channel, cmd->device->id,
2622 cmd->device->lun);
2623
2624 ret = SUCCESS;
2625
2626eh_dev_reset_done:
2627
2628 return ret;
2629}
2630
2631/**
Mike Christiece545032008-02-29 18:25:20 -06002632 * qla4xxx_eh_target_reset - callback for target reset.
2633 * @cmd: Pointer to Linux's SCSI command structure
2634 *
2635 * This routine is called by the Linux OS to reset the target.
2636 **/
2637static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
2638{
2639 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2640 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christiec01be6d2010-07-22 16:59:49 +05302641 int stat, ret;
Mike Christiece545032008-02-29 18:25:20 -06002642
2643 if (!ddb_entry)
2644 return FAILED;
2645
Mike Christiec01be6d2010-07-22 16:59:49 +05302646 ret = iscsi_block_scsi_eh(cmd);
2647 if (ret)
2648 return ret;
2649
Mike Christiece545032008-02-29 18:25:20 -06002650 starget_printk(KERN_INFO, scsi_target(cmd->device),
2651 "WARM TARGET RESET ISSUED.\n");
2652
2653 DEBUG2(printk(KERN_INFO
2654 "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
2655 "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
Jens Axboe242f9dc2008-09-14 05:55:09 -07002656 ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
Mike Christiece545032008-02-29 18:25:20 -06002657 ha->dpc_flags, cmd->result, cmd->allowed));
2658
2659 stat = qla4xxx_reset_target(ha, ddb_entry);
2660 if (stat != QLA_SUCCESS) {
2661 starget_printk(KERN_INFO, scsi_target(cmd->device),
2662 "WARM TARGET RESET FAILED.\n");
2663 return FAILED;
2664 }
2665
Mike Christiece545032008-02-29 18:25:20 -06002666 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2667 NULL)) {
2668 starget_printk(KERN_INFO, scsi_target(cmd->device),
2669 "WARM TARGET DEVICE RESET FAILED - "
2670 "waiting for commands.\n");
2671 return FAILED;
2672 }
2673
David C Somayajulu9d562912008-03-19 11:23:03 -07002674 /* Send marker. */
2675 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2676 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
2677 starget_printk(KERN_INFO, scsi_target(cmd->device),
2678 "WARM TARGET DEVICE RESET FAILED - "
2679 "marker iocb failed.\n");
2680 return FAILED;
2681 }
2682
Mike Christiece545032008-02-29 18:25:20 -06002683 starget_printk(KERN_INFO, scsi_target(cmd->device),
2684 "WARM TARGET RESET SUCCEEDED.\n");
2685 return SUCCESS;
2686}
2687
2688/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002689 * qla4xxx_eh_host_reset - kernel callback
2690 * @cmd: Pointer to Linux's SCSI command structure
2691 *
2692 * This routine is invoked by the Linux kernel to perform fatal error
2693 * recovery on the specified adapter.
2694 **/
2695static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
2696{
2697 int return_status = FAILED;
2698 struct scsi_qla_host *ha;
2699
2700 ha = (struct scsi_qla_host *) cmd->device->host->hostdata;
2701
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302702 if (ql4xdontresethba) {
2703 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
2704 ha->host_no, __func__));
2705 return FAILED;
2706 }
2707
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302708 ql4_printk(KERN_INFO, ha,
Karen Higginsdca05c42009-07-15 15:03:00 -05002709 "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002710 cmd->device->channel, cmd->device->id, cmd->device->lun);
2711
2712 if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
2713 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
2714 "DEAD.\n", ha->host_no, cmd->device->channel,
2715 __func__));
2716
2717 return FAILED;
2718 }
2719
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302720 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
2721 if (is_qla8022(ha))
2722 set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2723 else
2724 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2725 }
Mike Christie50a29ae2008-03-04 13:26:53 -06002726
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302727 if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002728 return_status = SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002729
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302730 ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002731 return_status == FAILED ? "FAILED" : "SUCCEEDED");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002732
2733 return return_status;
2734}
2735
Lalit Chandivade2232be02010-07-30 14:38:47 +05302736/* PCI AER driver recovers from all correctable errors w/o
2737 * driver intervention. For uncorrectable errors PCI AER
2738 * driver calls the following device driver's callbacks
2739 *
2740 * - Fatal Errors - link_reset
2741 * - Non-Fatal Errors - driver's pci_error_detected() which
2742 * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
2743 *
2744 * PCI AER driver calls
2745 * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
2746 * returns RECOVERED or NEED_RESET if fw_hung
2747 * NEED_RESET - driver's slot_reset()
2748 * DISCONNECT - device is dead & cannot recover
2749 * RECOVERED - driver's pci_resume()
2750 */
2751static pci_ers_result_t
2752qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
2753{
2754 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2755
2756 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
2757 ha->host_no, __func__, state);
2758
2759 if (!is_aer_supported(ha))
2760 return PCI_ERS_RESULT_NONE;
2761
2762 switch (state) {
2763 case pci_channel_io_normal:
2764 clear_bit(AF_EEH_BUSY, &ha->flags);
2765 return PCI_ERS_RESULT_CAN_RECOVER;
2766 case pci_channel_io_frozen:
2767 set_bit(AF_EEH_BUSY, &ha->flags);
2768 qla4xxx_mailbox_premature_completion(ha);
2769 qla4xxx_free_irqs(ha);
2770 pci_disable_device(pdev);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002771 /* Return back all IOs */
2772 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
Lalit Chandivade2232be02010-07-30 14:38:47 +05302773 return PCI_ERS_RESULT_NEED_RESET;
2774 case pci_channel_io_perm_failure:
2775 set_bit(AF_EEH_BUSY, &ha->flags);
2776 set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
2777 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
2778 return PCI_ERS_RESULT_DISCONNECT;
2779 }
2780 return PCI_ERS_RESULT_NEED_RESET;
2781}
2782
2783/**
2784 * qla4xxx_pci_mmio_enabled() gets called if
2785 * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
2786 * and read/write to the device still works.
2787 **/
2788static pci_ers_result_t
2789qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
2790{
2791 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2792
2793 if (!is_aer_supported(ha))
2794 return PCI_ERS_RESULT_NONE;
2795
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002796 return PCI_ERS_RESULT_RECOVERED;
Lalit Chandivade2232be02010-07-30 14:38:47 +05302797}
2798
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002799static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
Lalit Chandivade2232be02010-07-30 14:38:47 +05302800{
2801 uint32_t rval = QLA_ERROR;
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002802 uint32_t ret = 0;
Lalit Chandivade2232be02010-07-30 14:38:47 +05302803 int fn;
2804 struct pci_dev *other_pdev = NULL;
2805
2806 ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
2807
2808 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2809
2810 if (test_bit(AF_ONLINE, &ha->flags)) {
2811 clear_bit(AF_ONLINE, &ha->flags);
2812 qla4xxx_mark_all_devices_missing(ha);
2813 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
Lalit Chandivade2232be02010-07-30 14:38:47 +05302814 }
2815
2816 fn = PCI_FUNC(ha->pdev->devfn);
2817 while (fn > 0) {
2818 fn--;
2819 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
2820 "func %x\n", ha->host_no, __func__, fn);
2821 /* Get the pci device given the domain, bus,
2822 * slot/function number */
2823 other_pdev =
2824 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
2825 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
2826 fn));
2827
2828 if (!other_pdev)
2829 continue;
2830
2831 if (atomic_read(&other_pdev->enable_cnt)) {
2832 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
2833 "func in enabled state%x\n", ha->host_no,
2834 __func__, fn);
2835 pci_dev_put(other_pdev);
2836 break;
2837 }
2838 pci_dev_put(other_pdev);
2839 }
2840
2841 /* The first function on the card, the reset owner will
2842 * start & initialize the firmware. The other functions
2843 * on the card will reset the firmware context
2844 */
2845 if (!fn) {
2846 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
2847 "0x%x is the owner\n", ha->host_no, __func__,
2848 ha->pdev->devfn);
2849
2850 qla4_8xxx_idc_lock(ha);
2851 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2852 QLA82XX_DEV_COLD);
2853
2854 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
2855 QLA82XX_IDC_VERSION);
2856
2857 qla4_8xxx_idc_unlock(ha);
2858 clear_bit(AF_FW_RECOVERY, &ha->flags);
2859 rval = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
2860 qla4_8xxx_idc_lock(ha);
2861
2862 if (rval != QLA_SUCCESS) {
2863 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
2864 "FAILED\n", ha->host_no, __func__);
2865 qla4_8xxx_clear_drv_active(ha);
2866 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2867 QLA82XX_DEV_FAILED);
2868 } else {
2869 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
2870 "READY\n", ha->host_no, __func__);
2871 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2872 QLA82XX_DEV_READY);
2873 /* Clear driver state register */
2874 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
2875 qla4_8xxx_set_drv_active(ha);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002876 ret = qla4xxx_request_irqs(ha);
2877 if (ret) {
2878 ql4_printk(KERN_WARNING, ha, "Failed to "
2879 "reserve interrupt %d already in use.\n",
2880 ha->pdev->irq);
2881 rval = QLA_ERROR;
2882 } else {
2883 ha->isp_ops->enable_intrs(ha);
2884 rval = QLA_SUCCESS;
2885 }
Lalit Chandivade2232be02010-07-30 14:38:47 +05302886 }
2887 qla4_8xxx_idc_unlock(ha);
2888 } else {
2889 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
2890 "the reset owner\n", ha->host_no, __func__,
2891 ha->pdev->devfn);
2892 if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
2893 QLA82XX_DEV_READY)) {
2894 clear_bit(AF_FW_RECOVERY, &ha->flags);
2895 rval = qla4xxx_initialize_adapter(ha,
2896 PRESERVE_DDB_LIST);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002897 if (rval == QLA_SUCCESS) {
2898 ret = qla4xxx_request_irqs(ha);
2899 if (ret) {
2900 ql4_printk(KERN_WARNING, ha, "Failed to"
2901 " reserve interrupt %d already in"
2902 " use.\n", ha->pdev->irq);
2903 rval = QLA_ERROR;
2904 } else {
2905 ha->isp_ops->enable_intrs(ha);
2906 rval = QLA_SUCCESS;
2907 }
2908 }
Lalit Chandivade2232be02010-07-30 14:38:47 +05302909 qla4_8xxx_idc_lock(ha);
2910 qla4_8xxx_set_drv_active(ha);
2911 qla4_8xxx_idc_unlock(ha);
2912 }
2913 }
2914 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2915 return rval;
2916}
2917
2918static pci_ers_result_t
2919qla4xxx_pci_slot_reset(struct pci_dev *pdev)
2920{
2921 pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
2922 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2923 int rc;
2924
2925 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
2926 ha->host_no, __func__);
2927
2928 if (!is_aer_supported(ha))
2929 return PCI_ERS_RESULT_NONE;
2930
2931 /* Restore the saved state of PCIe device -
2932 * BAR registers, PCI Config space, PCIX, MSI,
2933 * IOV states
2934 */
2935 pci_restore_state(pdev);
2936
2937 /* pci_restore_state() clears the saved_state flag of the device
2938 * save restored state which resets saved_state flag
2939 */
2940 pci_save_state(pdev);
2941
2942 /* Initialize device or resume if in suspended state */
2943 rc = pci_enable_device(pdev);
2944 if (rc) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002945 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Can't re-enable "
Lalit Chandivade2232be02010-07-30 14:38:47 +05302946 "device after reset\n", ha->host_no, __func__);
2947 goto exit_slot_reset;
2948 }
2949
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002950 ha->isp_ops->disable_intrs(ha);
Lalit Chandivade2232be02010-07-30 14:38:47 +05302951
2952 if (is_qla8022(ha)) {
2953 if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
2954 ret = PCI_ERS_RESULT_RECOVERED;
2955 goto exit_slot_reset;
2956 } else
2957 goto exit_slot_reset;
2958 }
2959
2960exit_slot_reset:
2961 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
2962 "device after reset\n", ha->host_no, __func__, ret);
2963 return ret;
2964}
2965
2966static void
2967qla4xxx_pci_resume(struct pci_dev *pdev)
2968{
2969 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2970 int ret;
2971
2972 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
2973 ha->host_no, __func__);
2974
2975 ret = qla4xxx_wait_for_hba_online(ha);
2976 if (ret != QLA_SUCCESS) {
2977 ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
2978 "resume I/O from slot/link_reset\n", ha->host_no,
2979 __func__);
2980 }
2981
2982 pci_cleanup_aer_uncorrect_error_status(pdev);
2983 clear_bit(AF_EEH_BUSY, &ha->flags);
2984}
2985
2986static struct pci_error_handlers qla4xxx_err_handler = {
2987 .error_detected = qla4xxx_pci_error_detected,
2988 .mmio_enabled = qla4xxx_pci_mmio_enabled,
2989 .slot_reset = qla4xxx_pci_slot_reset,
2990 .resume = qla4xxx_pci_resume,
2991};
2992
David Somayajuluafaf5a22006-09-19 10:28:00 -07002993static struct pci_device_id qla4xxx_pci_tbl[] = {
2994 {
2995 .vendor = PCI_VENDOR_ID_QLOGIC,
2996 .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
2997 .subvendor = PCI_ANY_ID,
2998 .subdevice = PCI_ANY_ID,
2999 },
3000 {
3001 .vendor = PCI_VENDOR_ID_QLOGIC,
3002 .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
3003 .subvendor = PCI_ANY_ID,
3004 .subdevice = PCI_ANY_ID,
3005 },
David C Somayajulud9150582006-11-15 17:38:40 -08003006 {
3007 .vendor = PCI_VENDOR_ID_QLOGIC,
3008 .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
3009 .subvendor = PCI_ANY_ID,
3010 .subdevice = PCI_ANY_ID,
3011 },
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303012 {
3013 .vendor = PCI_VENDOR_ID_QLOGIC,
3014 .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
3015 .subvendor = PCI_ANY_ID,
3016 .subdevice = PCI_ANY_ID,
3017 },
David Somayajuluafaf5a22006-09-19 10:28:00 -07003018 {0, 0},
3019};
3020MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
3021
Adrian Bunk47975472007-04-26 00:35:16 -07003022static struct pci_driver qla4xxx_pci_driver = {
David Somayajuluafaf5a22006-09-19 10:28:00 -07003023 .name = DRIVER_NAME,
3024 .id_table = qla4xxx_pci_tbl,
3025 .probe = qla4xxx_probe_adapter,
3026 .remove = qla4xxx_remove_adapter,
Lalit Chandivade2232be02010-07-30 14:38:47 +05303027 .err_handler = &qla4xxx_err_handler,
David Somayajuluafaf5a22006-09-19 10:28:00 -07003028};
3029
3030static int __init qla4xxx_module_init(void)
3031{
3032 int ret;
3033
3034 /* Allocate cache for SRBs. */
3035 srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09003036 SLAB_HWCACHE_ALIGN, NULL);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003037 if (srb_cachep == NULL) {
3038 printk(KERN_ERR
3039 "%s: Unable to allocate SRB cache..."
3040 "Failing load!\n", DRIVER_NAME);
3041 ret = -ENOMEM;
3042 goto no_srp_cache;
3043 }
3044
3045 /* Derive version string. */
3046 strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
Andrew Vasquez11010fe2006-10-06 09:54:59 -07003047 if (ql4xextended_error_logging)
David Somayajuluafaf5a22006-09-19 10:28:00 -07003048 strcat(qla4xxx_version_str, "-debug");
3049
3050 qla4xxx_scsi_transport =
3051 iscsi_register_transport(&qla4xxx_iscsi_transport);
3052 if (!qla4xxx_scsi_transport){
3053 ret = -ENODEV;
3054 goto release_srb_cache;
3055 }
3056
David Somayajuluafaf5a22006-09-19 10:28:00 -07003057 ret = pci_register_driver(&qla4xxx_pci_driver);
3058 if (ret)
3059 goto unregister_transport;
3060
3061 printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
3062 return 0;
Doug Maxey5ae16db2006-10-05 23:50:07 -05003063
David Somayajuluafaf5a22006-09-19 10:28:00 -07003064unregister_transport:
3065 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
3066release_srb_cache:
3067 kmem_cache_destroy(srb_cachep);
3068no_srp_cache:
3069 return ret;
3070}
3071
3072static void __exit qla4xxx_module_exit(void)
3073{
David Somayajuluafaf5a22006-09-19 10:28:00 -07003074 pci_unregister_driver(&qla4xxx_pci_driver);
3075 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
3076 kmem_cache_destroy(srb_cachep);
3077}
3078
3079module_init(qla4xxx_module_init);
3080module_exit(qla4xxx_module_exit);
3081
3082MODULE_AUTHOR("QLogic Corporation");
3083MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
3084MODULE_LICENSE("GPL");
3085MODULE_VERSION(QLA4XXX_DRIVER_VERSION);