blob: 586f12f17c3365fd8a8cb61db5a97c8734f066f3 [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);
David Somayajuluafaf5a22006-09-19 10:28:00 -070085static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session);
Mike Christie5c656af2009-07-15 15:02:59 -050086static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
David Somayajuluafaf5a22006-09-19 10:28:00 -070087
88/*
89 * SCSI host template entry points
90 */
Jeff Garzikf2812332010-11-16 02:10:29 -050091static int qla4xxx_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmd);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +053092static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -070093static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
Mike Christiece545032008-02-29 18:25:20 -060094static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -070095static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
96static int qla4xxx_slave_alloc(struct scsi_device *device);
97static int qla4xxx_slave_configure(struct scsi_device *device);
98static void qla4xxx_slave_destroy(struct scsi_device *sdev);
Mike Christie024f8012008-03-04 13:26:54 -060099static void qla4xxx_scan_start(struct Scsi_Host *shost);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700100
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530101static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
102 QLA82XX_LEGACY_INTR_CONFIG;
103
David Somayajuluafaf5a22006-09-19 10:28:00 -0700104static struct scsi_host_template qla4xxx_driver_template = {
105 .module = THIS_MODULE,
106 .name = DRIVER_NAME,
107 .proc_name = DRIVER_NAME,
108 .queuecommand = qla4xxx_queuecommand,
109
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530110 .eh_abort_handler = qla4xxx_eh_abort,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700111 .eh_device_reset_handler = qla4xxx_eh_device_reset,
Mike Christiece545032008-02-29 18:25:20 -0600112 .eh_target_reset_handler = qla4xxx_eh_target_reset,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700113 .eh_host_reset_handler = qla4xxx_eh_host_reset,
Mike Christie5c656af2009-07-15 15:02:59 -0500114 .eh_timed_out = qla4xxx_eh_cmd_timed_out,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700115
116 .slave_configure = qla4xxx_slave_configure,
117 .slave_alloc = qla4xxx_slave_alloc,
118 .slave_destroy = qla4xxx_slave_destroy,
119
Mike Christie921601b2008-01-31 13:36:49 -0600120 .scan_finished = iscsi_scan_finished,
Mike Christie024f8012008-03-04 13:26:54 -0600121 .scan_start = qla4xxx_scan_start,
Mike Christie921601b2008-01-31 13:36:49 -0600122
David Somayajuluafaf5a22006-09-19 10:28:00 -0700123 .this_id = -1,
124 .cmd_per_lun = 3,
125 .use_clustering = ENABLE_CLUSTERING,
126 .sg_tablesize = SG_ALL,
127
128 .max_sectors = 0xFFFF,
Harish Zunjarrao7ad633c2011-05-17 23:17:11 -0700129 .shost_attrs = qla4xxx_host_attrs,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700130};
131
132static struct iscsi_transport qla4xxx_iscsi_transport = {
133 .owner = THIS_MODULE,
134 .name = DRIVER_NAME,
Mike Christied8196ed2007-05-30 12:57:25 -0500135 .caps = CAP_FW_DB | CAP_SENDTARGETS_OFFLOAD |
136 CAP_DATA_PATH_OFFLOAD,
Mike Christieaa1e93a2007-05-30 12:57:09 -0500137 .param_mask = ISCSI_CONN_PORT | ISCSI_CONN_ADDRESS |
Vikas Chaudhary3b2bef12010-07-10 14:51:30 +0530138 ISCSI_TARGET_NAME | ISCSI_TPGT |
139 ISCSI_TARGET_ALIAS,
Mike Christie8ad57812007-05-30 12:57:13 -0500140 .host_param_mask = ISCSI_HOST_HWADDRESS |
Mike Christie22236962007-05-30 12:57:24 -0500141 ISCSI_HOST_IPADDRESS |
Mike Christie8ad57812007-05-30 12:57:13 -0500142 ISCSI_HOST_INITIATOR_NAME,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700143 .tgt_dscvr = qla4xxx_tgt_dscvr,
144 .get_conn_param = qla4xxx_conn_get_param,
145 .get_session_param = qla4xxx_sess_get_param,
Mike Christieaa1e93a2007-05-30 12:57:09 -0500146 .get_host_param = qla4xxx_host_get_param,
Mike Christied00efe32011-07-25 13:48:38 -0500147 .set_iface_param = qla4xxx_iface_set_param,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700148 .session_recovery_timedout = qla4xxx_recovery_timedout,
149};
150
151static struct scsi_transport_template *qla4xxx_scsi_transport;
152
Mike Christie5c656af2009-07-15 15:02:59 -0500153static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
154{
155 struct iscsi_cls_session *session;
156 struct ddb_entry *ddb_entry;
157
158 session = starget_to_session(scsi_target(sc->device));
159 ddb_entry = session->dd_data;
160
161 /* if we are not logged in then the LLD is going to clean up the cmd */
162 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
163 return BLK_EH_RESET_TIMER;
164 else
165 return BLK_EH_NOT_HANDLED;
166}
167
David Somayajuluafaf5a22006-09-19 10:28:00 -0700168static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session)
169{
170 struct ddb_entry *ddb_entry = session->dd_data;
171 struct scsi_qla_host *ha = ddb_entry->ha;
172
Mike Christie568d3032008-01-31 13:36:47 -0600173 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
174 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700175
Vikas Chaudhary3013cea2010-07-30 14:25:46 +0530176 DEBUG2(printk("scsi%ld: %s: ddb [%d] session recovery timeout "
Mike Christie568d3032008-01-31 13:36:47 -0600177 "of (%d) secs exhausted, marking device DEAD.\n",
178 ha->host_no, __func__, ddb_entry->fw_ddb_index,
Vikas Chaudhary30387272011-03-21 03:34:32 -0700179 ddb_entry->sess->recovery_tmo));
Mike Christie568d3032008-01-31 13:36:47 -0600180 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700181}
182
Mike Christieaa1e93a2007-05-30 12:57:09 -0500183static int qla4xxx_host_get_param(struct Scsi_Host *shost,
184 enum iscsi_host_param param, char *buf)
185{
186 struct scsi_qla_host *ha = to_qla_host(shost);
187 int len;
188
189 switch (param) {
190 case ISCSI_HOST_PARAM_HWADDRESS:
Michael Chan7ffc49a2007-12-24 21:28:09 -0800191 len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
Mike Christieaa1e93a2007-05-30 12:57:09 -0500192 break;
Mike Christie22236962007-05-30 12:57:24 -0500193 case ISCSI_HOST_PARAM_IPADDRESS:
194 len = sprintf(buf, "%d.%d.%d.%d\n", ha->ip_address[0],
195 ha->ip_address[1], ha->ip_address[2],
196 ha->ip_address[3]);
197 break;
Mike Christie8ad57812007-05-30 12:57:13 -0500198 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie22236962007-05-30 12:57:24 -0500199 len = sprintf(buf, "%s\n", ha->name_string);
Mike Christie8ad57812007-05-30 12:57:13 -0500200 break;
Mike Christieaa1e93a2007-05-30 12:57:09 -0500201 default:
202 return -ENOSYS;
203 }
204
205 return len;
206}
207
Mike Christied00efe32011-07-25 13:48:38 -0500208static void qla4xxx_set_ipv6(struct scsi_qla_host *ha,
209 struct iscsi_iface_param_info *iface_param,
210 struct addr_ctrl_blk *init_fw_cb)
211{
212 /*
213 * iface_num 0 is valid for IPv6 Addr, linklocal, router, autocfg.
214 * iface_num 1 is valid only for IPv6 Addr.
215 */
216 switch (iface_param->param) {
217 case ISCSI_NET_PARAM_IPV6_ADDR:
218 if (iface_param->iface_num & 0x1)
219 /* IPv6 Addr 1 */
220 memcpy(init_fw_cb->ipv6_addr1, iface_param->value,
221 sizeof(init_fw_cb->ipv6_addr1));
222 else
223 /* IPv6 Addr 0 */
224 memcpy(init_fw_cb->ipv6_addr0, iface_param->value,
225 sizeof(init_fw_cb->ipv6_addr0));
226 break;
227 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
228 if (iface_param->iface_num & 0x1)
229 break;
230 memcpy(init_fw_cb->ipv6_if_id, &iface_param->value[8],
231 sizeof(init_fw_cb->ipv6_if_id));
232 break;
233 case ISCSI_NET_PARAM_IPV6_ROUTER:
234 if (iface_param->iface_num & 0x1)
235 break;
236 memcpy(init_fw_cb->ipv6_dflt_rtr_addr, iface_param->value,
237 sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
238 break;
239 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
240 /* Autocfg applies to even interface */
241 if (iface_param->iface_num & 0x1)
242 break;
243
244 if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_DISABLE)
245 init_fw_cb->ipv6_addtl_opts &=
246 cpu_to_le16(
247 ~IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
248 else if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_ND_ENABLE)
249 init_fw_cb->ipv6_addtl_opts |=
250 cpu_to_le16(
251 IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
252 else
253 ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
254 "IPv6 addr\n");
255 break;
256 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
257 /* Autocfg applies to even interface */
258 if (iface_param->iface_num & 0x1)
259 break;
260
261 if (iface_param->value[0] ==
262 ISCSI_IPV6_LINKLOCAL_AUTOCFG_ENABLE)
263 init_fw_cb->ipv6_addtl_opts |= cpu_to_le16(
264 IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
265 else if (iface_param->value[0] ==
266 ISCSI_IPV6_LINKLOCAL_AUTOCFG_DISABLE)
267 init_fw_cb->ipv6_addtl_opts &= cpu_to_le16(
268 ~IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
269 else
270 ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
271 "IPv6 linklocal addr\n");
272 break;
273 case ISCSI_NET_PARAM_IPV6_ROUTER_AUTOCFG:
274 /* Autocfg applies to even interface */
275 if (iface_param->iface_num & 0x1)
276 break;
277
278 if (iface_param->value[0] == ISCSI_IPV6_ROUTER_AUTOCFG_ENABLE)
279 memset(init_fw_cb->ipv6_dflt_rtr_addr, 0,
280 sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
281 break;
282 case ISCSI_NET_PARAM_IFACE_ENABLE:
283 if (iface_param->value[0] == ISCSI_IFACE_ENABLE)
284 init_fw_cb->ipv6_opts |=
285 cpu_to_le16(IPV6_OPT_IPV6_PROTOCOL_ENABLE);
286 else
287 init_fw_cb->ipv6_opts &=
288 cpu_to_le16(~IPV6_OPT_IPV6_PROTOCOL_ENABLE &
289 0xFFFF);
290 break;
291 case ISCSI_NET_PARAM_VLAN_ID:
292 if (iface_param->len != sizeof(init_fw_cb->ipv6_vlan_tag))
293 break;
294 init_fw_cb->ipv6_vlan_tag = *(uint16_t *)iface_param->value;
295 break;
296 default:
297 ql4_printk(KERN_ERR, ha, "Unknown IPv6 param = %d\n",
298 iface_param->param);
299 break;
300 }
301}
302
303static void qla4xxx_set_ipv4(struct scsi_qla_host *ha,
304 struct iscsi_iface_param_info *iface_param,
305 struct addr_ctrl_blk *init_fw_cb)
306{
307 switch (iface_param->param) {
308 case ISCSI_NET_PARAM_IPV4_ADDR:
309 memcpy(init_fw_cb->ipv4_addr, iface_param->value,
310 sizeof(init_fw_cb->ipv4_addr));
311 break;
312 case ISCSI_NET_PARAM_IPV4_SUBNET:
313 memcpy(init_fw_cb->ipv4_subnet, iface_param->value,
314 sizeof(init_fw_cb->ipv4_subnet));
315 break;
316 case ISCSI_NET_PARAM_IPV4_GW:
317 memcpy(init_fw_cb->ipv4_gw_addr, iface_param->value,
318 sizeof(init_fw_cb->ipv4_gw_addr));
319 break;
320 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
321 if (iface_param->value[0] == ISCSI_BOOTPROTO_DHCP)
322 init_fw_cb->ipv4_tcp_opts |=
323 cpu_to_le16(TCPOPT_DHCP_ENABLE);
324 else if (iface_param->value[0] == ISCSI_BOOTPROTO_STATIC)
325 init_fw_cb->ipv4_tcp_opts &=
326 cpu_to_le16(~TCPOPT_DHCP_ENABLE);
327 else
328 ql4_printk(KERN_ERR, ha, "Invalid IPv4 bootproto\n");
329 break;
330 case ISCSI_NET_PARAM_IFACE_ENABLE:
331 if (iface_param->value[0] == ISCSI_IFACE_ENABLE)
332 init_fw_cb->ipv4_ip_opts |=
333 cpu_to_le16(IPOPT_IPv4_PROTOCOL_ENABLE);
334 else
335 init_fw_cb->ipv4_ip_opts &=
336 cpu_to_le16(~IPOPT_IPv4_PROTOCOL_ENABLE &
337 0xFFFF);
338 break;
339 case ISCSI_NET_PARAM_VLAN_ID:
340 if (iface_param->len != sizeof(init_fw_cb->ipv4_vlan_tag))
341 break;
342 init_fw_cb->ipv4_vlan_tag = *(uint16_t *)iface_param->value;
343 break;
344 default:
345 ql4_printk(KERN_ERR, ha, "Unknown IPv4 param = %d\n",
346 iface_param->param);
347 break;
348 }
349}
350
351static void
352qla4xxx_initcb_to_acb(struct addr_ctrl_blk *init_fw_cb)
353{
354 struct addr_ctrl_blk_def *acb;
355 acb = (struct addr_ctrl_blk_def *)init_fw_cb;
356 memset(acb->reserved1, 0, sizeof(acb->reserved1));
357 memset(acb->reserved2, 0, sizeof(acb->reserved2));
358 memset(acb->reserved3, 0, sizeof(acb->reserved3));
359 memset(acb->reserved4, 0, sizeof(acb->reserved4));
360 memset(acb->reserved5, 0, sizeof(acb->reserved5));
361 memset(acb->reserved6, 0, sizeof(acb->reserved6));
362 memset(acb->reserved7, 0, sizeof(acb->reserved7));
363 memset(acb->reserved8, 0, sizeof(acb->reserved8));
364 memset(acb->reserved9, 0, sizeof(acb->reserved9));
365 memset(acb->reserved10, 0, sizeof(acb->reserved10));
366 memset(acb->reserved11, 0, sizeof(acb->reserved11));
367 memset(acb->reserved12, 0, sizeof(acb->reserved12));
368 memset(acb->reserved13, 0, sizeof(acb->reserved13));
369 memset(acb->reserved14, 0, sizeof(acb->reserved14));
370 memset(acb->reserved15, 0, sizeof(acb->reserved15));
371}
372
373static int
374qla4xxx_iface_set_param(struct Scsi_Host *shost, char *data, int count)
375{
376 struct scsi_qla_host *ha = to_qla_host(shost);
377 int rval = 0;
378 struct iscsi_iface_param_info *iface_param = NULL;
379 struct addr_ctrl_blk *init_fw_cb = NULL;
380 dma_addr_t init_fw_cb_dma;
381 uint32_t mbox_cmd[MBOX_REG_COUNT];
382 uint32_t mbox_sts[MBOX_REG_COUNT];
383 uint32_t total_param_count;
384 uint32_t length;
385
386 init_fw_cb = dma_alloc_coherent(&ha->pdev->dev,
387 sizeof(struct addr_ctrl_blk),
388 &init_fw_cb_dma, GFP_KERNEL);
389 if (!init_fw_cb) {
390 ql4_printk(KERN_ERR, ha, "%s: Unable to alloc init_cb\n",
391 __func__);
392 return -ENOMEM;
393 }
394
395 memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
396 memset(&mbox_cmd, 0, sizeof(mbox_cmd));
397 memset(&mbox_sts, 0, sizeof(mbox_sts));
398
399 if (qla4xxx_get_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma)) {
400 ql4_printk(KERN_ERR, ha, "%s: get ifcb failed\n", __func__);
401 rval = -EIO;
402 goto exit_init_fw_cb;
403 }
404
405 total_param_count = count;
406 iface_param = (struct iscsi_iface_param_info *)data;
407
408 for ( ; total_param_count != 0; total_param_count--) {
409 length = iface_param->len;
410
411 if (iface_param->param_type != ISCSI_NET_PARAM)
412 continue;
413
414 switch (iface_param->iface_type) {
415 case ISCSI_IFACE_TYPE_IPV4:
416 switch (iface_param->iface_num) {
417 case 0:
418 qla4xxx_set_ipv4(ha, iface_param, init_fw_cb);
419 break;
420 default:
421 /* Cannot have more than one IPv4 interface */
422 ql4_printk(KERN_ERR, ha, "Invalid IPv4 iface "
423 "number = %d\n",
424 iface_param->iface_num);
425 break;
426 }
427 break;
428 case ISCSI_IFACE_TYPE_IPV6:
429 switch (iface_param->iface_num) {
430 case 0:
431 case 1:
432 qla4xxx_set_ipv6(ha, iface_param, init_fw_cb);
433 break;
434 default:
435 /* Cannot have more than two IPv6 interface */
436 ql4_printk(KERN_ERR, ha, "Invalid IPv6 iface "
437 "number = %d\n",
438 iface_param->iface_num);
439 break;
440 }
441 break;
442 default:
443 ql4_printk(KERN_ERR, ha, "Invalid iface type\n");
444 break;
445 }
446
447 iface_param = (struct iscsi_iface_param_info *)
448 ((uint8_t *)iface_param +
449 sizeof(struct iscsi_iface_param_info) + length);
450 }
451
452 init_fw_cb->cookie = cpu_to_le32(0x11BEAD5A);
453
454 rval = qla4xxx_set_flash(ha, init_fw_cb_dma, FLASH_SEGMENT_IFCB,
455 sizeof(struct addr_ctrl_blk),
456 FLASH_OPT_RMW_COMMIT);
457 if (rval != QLA_SUCCESS) {
458 ql4_printk(KERN_ERR, ha, "%s: set flash mbx failed\n",
459 __func__);
460 rval = -EIO;
461 goto exit_init_fw_cb;
462 }
463
464 qla4xxx_disable_acb(ha);
465
466 qla4xxx_initcb_to_acb(init_fw_cb);
467
468 rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma);
469 if (rval != QLA_SUCCESS) {
470 ql4_printk(KERN_ERR, ha, "%s: set acb mbx failed\n",
471 __func__);
472 rval = -EIO;
473 goto exit_init_fw_cb;
474 }
475
476 memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
477 qla4xxx_update_local_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb,
478 init_fw_cb_dma);
479
480exit_init_fw_cb:
481 dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk),
482 init_fw_cb, init_fw_cb_dma);
483
484 return rval;
485}
486
David Somayajuluafaf5a22006-09-19 10:28:00 -0700487static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
488 enum iscsi_param param, char *buf)
489{
490 struct ddb_entry *ddb_entry = sess->dd_data;
491 int len;
492
493 switch (param) {
494 case ISCSI_PARAM_TARGET_NAME:
495 len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
496 ddb_entry->iscsi_name);
497 break;
498 case ISCSI_PARAM_TPGT:
499 len = sprintf(buf, "%u\n", ddb_entry->tpgt);
500 break;
Vikas Chaudhary3b2bef12010-07-10 14:51:30 +0530501 case ISCSI_PARAM_TARGET_ALIAS:
502 len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
503 ddb_entry->iscsi_alias);
504 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700505 default:
506 return -ENOSYS;
507 }
508
509 return len;
510}
511
512static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
513 enum iscsi_param param, char *buf)
514{
515 struct iscsi_cls_session *session;
516 struct ddb_entry *ddb_entry;
517 int len;
518
519 session = iscsi_dev_to_session(conn->dev.parent);
520 ddb_entry = session->dd_data;
521
522 switch (param) {
523 case ISCSI_PARAM_CONN_PORT:
524 len = sprintf(buf, "%hu\n", ddb_entry->port);
525 break;
526 case ISCSI_PARAM_CONN_ADDRESS:
527 /* TODO: what are the ipv6 bits */
Harvey Harrison63779432008-10-31 00:56:00 -0700528 len = sprintf(buf, "%pI4\n", &ddb_entry->ip_addr);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700529 break;
530 default:
531 return -ENOSYS;
532 }
533
534 return len;
535}
536
Mike Christie2174a042007-05-30 12:57:10 -0500537static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
538 enum iscsi_tgt_dscvr type, uint32_t enable,
539 struct sockaddr *dst_addr)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700540{
541 struct scsi_qla_host *ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700542 struct sockaddr_in *addr;
543 struct sockaddr_in6 *addr6;
544 int ret = 0;
545
David Somayajuluafaf5a22006-09-19 10:28:00 -0700546 ha = (struct scsi_qla_host *) shost->hostdata;
547
548 switch (type) {
549 case ISCSI_TGT_DSCVR_SEND_TARGETS:
550 if (dst_addr->sa_family == AF_INET) {
551 addr = (struct sockaddr_in *)dst_addr;
552 if (qla4xxx_send_tgts(ha, (char *)&addr->sin_addr,
553 addr->sin_port) != QLA_SUCCESS)
554 ret = -EIO;
555 } else if (dst_addr->sa_family == AF_INET6) {
556 /*
557 * TODO: fix qla4xxx_send_tgts
558 */
559 addr6 = (struct sockaddr_in6 *)dst_addr;
560 if (qla4xxx_send_tgts(ha, (char *)&addr6->sin6_addr,
561 addr6->sin6_port) != QLA_SUCCESS)
562 ret = -EIO;
563 } else
564 ret = -ENOSYS;
565 break;
566 default:
567 ret = -ENOSYS;
568 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700569 return ret;
570}
571
572void qla4xxx_destroy_sess(struct ddb_entry *ddb_entry)
573{
574 if (!ddb_entry->sess)
575 return;
576
577 if (ddb_entry->conn) {
Mike Christie26974782007-12-13 12:43:29 -0600578 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700579 iscsi_remove_session(ddb_entry->sess);
580 }
581 iscsi_free_session(ddb_entry->sess);
582}
583
584int qla4xxx_add_sess(struct ddb_entry *ddb_entry)
585{
586 int err;
587
Vikas Chaudhary30387272011-03-21 03:34:32 -0700588 ddb_entry->sess->recovery_tmo = ql4xsess_recovery_tmo;
Vikas Chaudhary3013cea2010-07-30 14:25:46 +0530589
David Somayajuluafaf5a22006-09-19 10:28:00 -0700590 err = iscsi_add_session(ddb_entry->sess, ddb_entry->fw_ddb_index);
591 if (err) {
592 DEBUG2(printk(KERN_ERR "Could not add session.\n"));
593 return err;
594 }
595
Mike Christie5d91e202008-05-21 15:54:01 -0500596 ddb_entry->conn = iscsi_create_conn(ddb_entry->sess, 0, 0);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700597 if (!ddb_entry->conn) {
598 iscsi_remove_session(ddb_entry->sess);
599 DEBUG2(printk(KERN_ERR "Could not add connection.\n"));
600 return -ENOMEM;
601 }
Mike Christieb6359302008-01-31 13:36:44 -0600602
603 /* finally ready to go */
604 iscsi_unblock_session(ddb_entry->sess);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700605 return 0;
606}
607
608struct ddb_entry *qla4xxx_alloc_sess(struct scsi_qla_host *ha)
609{
610 struct ddb_entry *ddb_entry;
611 struct iscsi_cls_session *sess;
612
Mike Christie5d91e202008-05-21 15:54:01 -0500613 sess = iscsi_alloc_session(ha->host, &qla4xxx_iscsi_transport,
614 sizeof(struct ddb_entry));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700615 if (!sess)
616 return NULL;
617
618 ddb_entry = sess->dd_data;
619 memset(ddb_entry, 0, sizeof(*ddb_entry));
620 ddb_entry->ha = ha;
621 ddb_entry->sess = sess;
622 return ddb_entry;
623}
624
Mike Christie024f8012008-03-04 13:26:54 -0600625static void qla4xxx_scan_start(struct Scsi_Host *shost)
626{
627 struct scsi_qla_host *ha = shost_priv(shost);
628 struct ddb_entry *ddb_entry, *ddbtemp;
629
630 /* finish setup of sessions that were already setup in firmware */
631 list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
632 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE)
633 qla4xxx_add_sess(ddb_entry);
634 }
635}
636
David Somayajuluafaf5a22006-09-19 10:28:00 -0700637/*
638 * Timer routines
639 */
640
641static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
642 unsigned long interval)
643{
644 DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
645 __func__, ha->host->host_no));
646 init_timer(&ha->timer);
647 ha->timer.expires = jiffies + interval * HZ;
648 ha->timer.data = (unsigned long)ha;
649 ha->timer.function = (void (*)(unsigned long))func;
650 add_timer(&ha->timer);
651 ha->timer_active = 1;
652}
653
654static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
655{
656 del_timer_sync(&ha->timer);
657 ha->timer_active = 0;
658}
659
660/***
661 * qla4xxx_mark_device_missing - mark a device as missing.
662 * @ha: Pointer to host adapter structure.
663 * @ddb_entry: Pointer to device database entry
664 *
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530665 * This routine marks a device missing and close connection.
David Somayajuluafaf5a22006-09-19 10:28:00 -0700666 **/
667void qla4xxx_mark_device_missing(struct scsi_qla_host *ha,
668 struct ddb_entry *ddb_entry)
669{
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530670 if ((atomic_read(&ddb_entry->state) != DDB_STATE_DEAD)) {
671 atomic_set(&ddb_entry->state, DDB_STATE_MISSING);
672 DEBUG2(printk("scsi%ld: ddb [%d] marked MISSING\n",
673 ha->host_no, ddb_entry->fw_ddb_index));
674 } else
675 DEBUG2(printk("scsi%ld: ddb [%d] DEAD\n", ha->host_no,
676 ddb_entry->fw_ddb_index))
677
Mike Christieb6359302008-01-31 13:36:44 -0600678 iscsi_block_session(ddb_entry->sess);
Mike Christiee5bd7b52008-09-24 11:46:10 -0500679 iscsi_conn_error_event(ddb_entry->conn, ISCSI_ERR_CONN_FAILED);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700680}
681
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530682/**
683 * qla4xxx_mark_all_devices_missing - mark all devices as missing.
684 * @ha: Pointer to host adapter structure.
685 *
686 * This routine marks a device missing and resets the relogin retry count.
687 **/
688void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
689{
690 struct ddb_entry *ddb_entry, *ddbtemp;
691 list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
692 qla4xxx_mark_device_missing(ha, ddb_entry);
693 }
694}
695
David Somayajuluafaf5a22006-09-19 10:28:00 -0700696static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
697 struct ddb_entry *ddb_entry,
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -0700698 struct scsi_cmnd *cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700699{
700 struct srb *srb;
701
702 srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
703 if (!srb)
704 return srb;
705
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530706 kref_init(&srb->srb_ref);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700707 srb->ha = ha;
708 srb->ddb = ddb_entry;
709 srb->cmd = cmd;
710 srb->flags = 0;
Vikas Chaudhary53698872010-04-28 11:41:59 +0530711 CMD_SP(cmd) = (void *)srb;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700712
713 return srb;
714}
715
716static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
717{
718 struct scsi_cmnd *cmd = srb->cmd;
719
720 if (srb->flags & SRB_DMA_VALID) {
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +0900721 scsi_dma_unmap(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700722 srb->flags &= ~SRB_DMA_VALID;
723 }
Vikas Chaudhary53698872010-04-28 11:41:59 +0530724 CMD_SP(cmd) = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700725}
726
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530727void qla4xxx_srb_compl(struct kref *ref)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700728{
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530729 struct srb *srb = container_of(ref, struct srb, srb_ref);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700730 struct scsi_cmnd *cmd = srb->cmd;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530731 struct scsi_qla_host *ha = srb->ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700732
733 qla4xxx_srb_free_dma(ha, srb);
734
735 mempool_free(srb, ha->srb_mempool);
736
737 cmd->scsi_done(cmd);
738}
739
740/**
741 * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -0700742 * @host: scsi host
David Somayajuluafaf5a22006-09-19 10:28:00 -0700743 * @cmd: Pointer to Linux's SCSI command structure
David Somayajuluafaf5a22006-09-19 10:28:00 -0700744 *
745 * Remarks:
746 * This routine is invoked by Linux to send a SCSI command to the driver.
747 * The mid-level driver tries to ensure that queuecommand never gets
748 * invoked concurrently with itself or the interrupt handler (although
749 * the interrupt handler may call this routine as part of request-
750 * completion handling). Unfortunely, it sometimes calls the scheduler
751 * in interrupt context which is a big NO! NO!.
752 **/
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -0700753static int qla4xxx_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700754{
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -0700755 struct scsi_qla_host *ha = to_qla_host(host);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700756 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christie7fb19212008-01-31 13:36:45 -0600757 struct iscsi_cls_session *sess = ddb_entry->sess;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700758 struct srb *srb;
759 int rval;
760
Lalit Chandivade2232be02010-07-30 14:38:47 +0530761 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
762 if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
763 cmd->result = DID_NO_CONNECT << 16;
764 else
765 cmd->result = DID_REQUEUE << 16;
766 goto qc_fail_command;
767 }
768
Mike Christie7fb19212008-01-31 13:36:45 -0600769 if (!sess) {
770 cmd->result = DID_IMM_RETRY << 16;
771 goto qc_fail_command;
772 }
773
774 rval = iscsi_session_chkready(sess);
775 if (rval) {
776 cmd->result = rval;
777 goto qc_fail_command;
778 }
779
David Somayajuluafaf5a22006-09-19 10:28:00 -0700780 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
781 if (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD) {
782 cmd->result = DID_NO_CONNECT << 16;
783 goto qc_fail_command;
784 }
Mike Christiec5e98e92008-08-17 15:24:39 -0500785 return SCSI_MLQUEUE_TARGET_BUSY;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700786 }
787
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530788 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
789 test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
790 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
791 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
792 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
793 !test_bit(AF_ONLINE, &ha->flags) ||
794 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
David C Somayajulu477ffb92007-01-22 12:26:11 -0800795 goto qc_host_busy;
796
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -0700797 srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700798 if (!srb)
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -0700799 goto qc_host_busy;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700800
801 rval = qla4xxx_send_command_to_isp(ha, srb);
802 if (rval != QLA_SUCCESS)
803 goto qc_host_busy_free_sp;
804
David Somayajuluafaf5a22006-09-19 10:28:00 -0700805 return 0;
806
807qc_host_busy_free_sp:
808 qla4xxx_srb_free_dma(ha, srb);
809 mempool_free(srb, ha->srb_mempool);
810
David Somayajuluafaf5a22006-09-19 10:28:00 -0700811qc_host_busy:
812 return SCSI_MLQUEUE_HOST_BUSY;
813
814qc_fail_command:
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -0700815 cmd->scsi_done(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700816
817 return 0;
818}
819
820/**
821 * qla4xxx_mem_free - frees memory allocated to adapter
822 * @ha: Pointer to host adapter structure.
823 *
824 * Frees memory previously allocated by qla4xxx_mem_alloc
825 **/
826static void qla4xxx_mem_free(struct scsi_qla_host *ha)
827{
828 if (ha->queues)
829 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
830 ha->queues_dma);
831
832 ha->queues_len = 0;
833 ha->queues = NULL;
834 ha->queues_dma = 0;
835 ha->request_ring = NULL;
836 ha->request_dma = 0;
837 ha->response_ring = NULL;
838 ha->response_dma = 0;
839 ha->shadow_regs = NULL;
840 ha->shadow_regs_dma = 0;
841
842 /* Free srb pool. */
843 if (ha->srb_mempool)
844 mempool_destroy(ha->srb_mempool);
845
846 ha->srb_mempool = NULL;
847
848 /* release io space registers */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530849 if (is_qla8022(ha)) {
850 if (ha->nx_pcibase)
851 iounmap(
852 (struct device_reg_82xx __iomem *)ha->nx_pcibase);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530853 } else if (ha->reg)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700854 iounmap(ha->reg);
855 pci_release_regions(ha->pdev);
856}
857
858/**
859 * qla4xxx_mem_alloc - allocates memory for use by adapter.
860 * @ha: Pointer to host adapter structure
861 *
862 * Allocates DMA memory for request and response queues. Also allocates memory
863 * for srbs.
864 **/
865static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
866{
867 unsigned long align;
868
869 /* Allocate contiguous block of DMA memory for queues. */
870 ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
871 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
872 sizeof(struct shadow_regs) +
873 MEM_ALIGN_VALUE +
874 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
875 ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
876 &ha->queues_dma, GFP_KERNEL);
877 if (ha->queues == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530878 ql4_printk(KERN_WARNING, ha,
879 "Memory Allocation failed - queues.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700880
881 goto mem_alloc_error_exit;
882 }
883 memset(ha->queues, 0, ha->queues_len);
884
885 /*
886 * As per RISC alignment requirements -- the bus-address must be a
887 * multiple of the request-ring size (in bytes).
888 */
889 align = 0;
890 if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
891 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
892 (MEM_ALIGN_VALUE - 1));
893
894 /* Update request and response queue pointers. */
895 ha->request_dma = ha->queues_dma + align;
896 ha->request_ring = (struct queue_entry *) (ha->queues + align);
897 ha->response_dma = ha->queues_dma + align +
898 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
899 ha->response_ring = (struct queue_entry *) (ha->queues + align +
900 (REQUEST_QUEUE_DEPTH *
901 QUEUE_SIZE));
902 ha->shadow_regs_dma = ha->queues_dma + align +
903 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
904 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
905 ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
906 (REQUEST_QUEUE_DEPTH *
907 QUEUE_SIZE) +
908 (RESPONSE_QUEUE_DEPTH *
909 QUEUE_SIZE));
910
911 /* Allocate memory for srb pool. */
912 ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
913 mempool_free_slab, srb_cachep);
914 if (ha->srb_mempool == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530915 ql4_printk(KERN_WARNING, ha,
916 "Memory Allocation failed - SRB Pool.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700917
918 goto mem_alloc_error_exit;
919 }
920
921 return QLA_SUCCESS;
922
923mem_alloc_error_exit:
924 qla4xxx_mem_free(ha);
925 return QLA_ERROR;
926}
927
928/**
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530929 * qla4_8xxx_check_fw_alive - Check firmware health
930 * @ha: Pointer to host adapter structure.
931 *
932 * Context: Interrupt
933 **/
934static void qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
935{
936 uint32_t fw_heartbeat_counter, halt_status;
937
938 fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
Lalit Chandivade2232be02010-07-30 14:38:47 +0530939 /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
940 if (fw_heartbeat_counter == 0xffffffff) {
941 DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
942 "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
943 ha->host_no, __func__));
944 return;
945 }
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530946
947 if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
948 ha->seconds_since_last_heartbeat++;
949 /* FW not alive after 2 seconds */
950 if (ha->seconds_since_last_heartbeat == 2) {
951 ha->seconds_since_last_heartbeat = 0;
952 halt_status = qla4_8xxx_rd_32(ha,
Vikas Chaudhary68d92eb2011-05-17 23:17:05 -0700953 QLA82XX_PEG_HALT_STATUS1);
954
955 ql4_printk(KERN_INFO, ha,
956 "scsi(%ld): %s, Dumping hw/fw registers:\n "
957 " PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2:"
958 " 0x%x,\n PEG_NET_0_PC: 0x%x, PEG_NET_1_PC:"
959 " 0x%x,\n PEG_NET_2_PC: 0x%x, PEG_NET_3_PC:"
960 " 0x%x,\n PEG_NET_4_PC: 0x%x\n",
961 ha->host_no, __func__, halt_status,
962 qla4_8xxx_rd_32(ha,
963 QLA82XX_PEG_HALT_STATUS2),
964 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_0 +
965 0x3c),
966 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_1 +
967 0x3c),
968 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_2 +
969 0x3c),
970 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_3 +
971 0x3c),
972 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_4 +
973 0x3c));
Nilesh Javali21033632010-07-30 14:28:07 +0530974
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530975 /* Since we cannot change dev_state in interrupt
976 * context, set appropriate DPC flag then wakeup
977 * DPC */
978 if (halt_status & HALT_STATUS_UNRECOVERABLE)
979 set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
980 else {
981 printk("scsi%ld: %s: detect abort needed!\n",
982 ha->host_no, __func__);
983 set_bit(DPC_RESET_HA, &ha->dpc_flags);
984 }
985 qla4xxx_wake_dpc(ha);
Nilesh Javali21033632010-07-30 14:28:07 +0530986 qla4xxx_mailbox_premature_completion(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530987 }
Lalit Chandivade99457d72010-10-06 22:49:32 -0700988 } else
989 ha->seconds_since_last_heartbeat = 0;
990
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530991 ha->fw_heartbeat_counter = fw_heartbeat_counter;
992}
993
994/**
995 * qla4_8xxx_watchdog - Poll dev state
996 * @ha: Pointer to host adapter structure.
997 *
998 * Context: Interrupt
999 **/
1000void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
1001{
1002 uint32_t dev_state;
1003
1004 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
1005
1006 /* don't poll if reset is going on */
Lalit Chandivaded56a1f72010-12-02 22:12:45 -08001007 if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
1008 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
Vikas Chaudhary977f46a2011-05-17 23:17:08 -07001009 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags))) {
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301010 if (dev_state == QLA82XX_DEV_NEED_RESET &&
1011 !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
Vikas Chaudhary3930b8c2010-12-02 22:12:47 -08001012 if (!ql4xdontresethba) {
1013 ql4_printk(KERN_INFO, ha, "%s: HW State: "
1014 "NEED RESET!\n", __func__);
1015 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1016 qla4xxx_wake_dpc(ha);
1017 qla4xxx_mailbox_premature_completion(ha);
1018 }
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301019 } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
1020 !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
Vikas Chaudhary3930b8c2010-12-02 22:12:47 -08001021 ql4_printk(KERN_INFO, ha, "%s: HW State: NEED QUIES!\n",
1022 __func__);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301023 set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
1024 qla4xxx_wake_dpc(ha);
1025 } else {
1026 /* Check firmware health */
1027 qla4_8xxx_check_fw_alive(ha);
1028 }
1029 }
1030}
1031
1032/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07001033 * qla4xxx_timer - checks every second for work to do.
1034 * @ha: Pointer to host adapter structure.
1035 **/
1036static void qla4xxx_timer(struct scsi_qla_host *ha)
1037{
1038 struct ddb_entry *ddb_entry, *dtemp;
1039 int start_dpc = 0;
Lalit Chandivade2232be02010-07-30 14:38:47 +05301040 uint16_t w;
1041
1042 /* If we are in the middle of AER/EEH processing
1043 * skip any processing and reschedule the timer
1044 */
1045 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1046 mod_timer(&ha->timer, jiffies + HZ);
1047 return;
1048 }
1049
1050 /* Hardware read to trigger an EEH error during mailbox waits. */
1051 if (!pci_channel_offline(ha->pdev))
1052 pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001053
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301054 if (is_qla8022(ha)) {
1055 qla4_8xxx_watchdog(ha);
1056 }
1057
David Somayajuluafaf5a22006-09-19 10:28:00 -07001058 /* Search for relogin's to time-out and port down retry. */
1059 list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
1060 /* Count down time between sending relogins */
1061 if (adapter_up(ha) &&
1062 !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
1063 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
1064 if (atomic_read(&ddb_entry->retry_relogin_timer) !=
1065 INVALID_ENTRY) {
1066 if (atomic_read(&ddb_entry->retry_relogin_timer)
1067 == 0) {
1068 atomic_set(&ddb_entry->
1069 retry_relogin_timer,
1070 INVALID_ENTRY);
1071 set_bit(DPC_RELOGIN_DEVICE,
1072 &ha->dpc_flags);
1073 set_bit(DF_RELOGIN, &ddb_entry->flags);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301074 DEBUG2(printk("scsi%ld: %s: ddb [%d]"
David Somayajuluafaf5a22006-09-19 10:28:00 -07001075 " login device\n",
1076 ha->host_no, __func__,
1077 ddb_entry->fw_ddb_index));
1078 } else
1079 atomic_dec(&ddb_entry->
1080 retry_relogin_timer);
1081 }
1082 }
1083
1084 /* Wait for relogin to timeout */
1085 if (atomic_read(&ddb_entry->relogin_timer) &&
1086 (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
1087 /*
1088 * If the relogin times out and the device is
1089 * still NOT ONLINE then try and relogin again.
1090 */
1091 if (atomic_read(&ddb_entry->state) !=
1092 DDB_STATE_ONLINE &&
1093 ddb_entry->fw_ddb_device_state ==
1094 DDB_DS_SESSION_FAILED) {
1095 /* Reset retry relogin timer */
1096 atomic_inc(&ddb_entry->relogin_retry_count);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301097 DEBUG2(printk("scsi%ld: ddb [%d] relogin"
David Somayajuluafaf5a22006-09-19 10:28:00 -07001098 " timed out-retrying"
1099 " relogin (%d)\n",
1100 ha->host_no,
1101 ddb_entry->fw_ddb_index,
1102 atomic_read(&ddb_entry->
1103 relogin_retry_count))
1104 );
1105 start_dpc++;
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301106 DEBUG(printk("scsi%ld:%d:%d: ddb [%d] "
Justin P. Mattockcd09b2c2011-01-28 11:49:08 -08001107 "initiate relogin after"
David Somayajuluafaf5a22006-09-19 10:28:00 -07001108 " %d seconds\n",
1109 ha->host_no, ddb_entry->bus,
1110 ddb_entry->target,
1111 ddb_entry->fw_ddb_index,
1112 ddb_entry->default_time2wait + 4)
1113 );
1114
1115 atomic_set(&ddb_entry->retry_relogin_timer,
1116 ddb_entry->default_time2wait + 4);
1117 }
1118 }
1119 }
1120
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301121 if (!is_qla8022(ha)) {
1122 /* Check for heartbeat interval. */
1123 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
1124 ha->heartbeat_interval != 0) {
1125 ha->seconds_since_last_heartbeat++;
1126 if (ha->seconds_since_last_heartbeat >
1127 ha->heartbeat_interval + 2)
1128 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1129 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001130 }
1131
David Somayajuluafaf5a22006-09-19 10:28:00 -07001132 /* Wakeup the dpc routine for this adapter, if needed. */
Lalit Chandivade1b468072011-05-17 23:17:09 -07001133 if (start_dpc ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07001134 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1135 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
1136 test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301137 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07001138 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1139 test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301140 test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301141 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
1142 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
Lalit Chandivade1b468072011-05-17 23:17:09 -07001143 test_bit(DPC_AEN, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001144 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
1145 " - dpc flags = 0x%lx\n",
1146 ha->host_no, __func__, ha->dpc_flags));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301147 qla4xxx_wake_dpc(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001148 }
1149
1150 /* Reschedule timer thread to call us back in one second */
1151 mod_timer(&ha->timer, jiffies + HZ);
1152
1153 DEBUG2(ha->seconds_since_last_intr++);
1154}
1155
1156/**
1157 * qla4xxx_cmd_wait - waits for all outstanding commands to complete
1158 * @ha: Pointer to host adapter structure.
1159 *
1160 * This routine stalls the driver until all outstanding commands are returned.
1161 * Caller must release the Hardware Lock prior to calling this routine.
1162 **/
1163static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
1164{
1165 uint32_t index = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001166 unsigned long flags;
1167 struct scsi_cmnd *cmd;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001168
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301169 unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
1170
1171 DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
1172 "complete\n", WAIT_CMD_TOV));
1173
1174 while (!time_after_eq(jiffies, wtime)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001175 spin_lock_irqsave(&ha->hardware_lock, flags);
1176 /* Find a command that hasn't completed. */
1177 for (index = 0; index < ha->host->can_queue; index++) {
1178 cmd = scsi_host_find_tag(ha->host, index);
Mike Christiea1e00632010-10-26 05:45:30 -07001179 /*
1180 * We cannot just check if the index is valid,
1181 * becase if we are run from the scsi eh, then
1182 * the scsi/block layer is going to prevent
1183 * the tag from being released.
1184 */
1185 if (cmd != NULL && CMD_SP(cmd))
David Somayajuluafaf5a22006-09-19 10:28:00 -07001186 break;
1187 }
1188 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1189
1190 /* If No Commands are pending, wait is complete */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301191 if (index == ha->host->can_queue)
1192 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001193
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301194 msleep(1000);
1195 }
1196 /* If we timed out on waiting for commands to come back
1197 * return ERROR. */
1198 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001199}
1200
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301201int qla4xxx_hw_reset(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001202{
David Somayajuluafaf5a22006-09-19 10:28:00 -07001203 uint32_t ctrl_status;
David C Somayajulu477ffb92007-01-22 12:26:11 -08001204 unsigned long flags = 0;
1205
1206 DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001207
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301208 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
1209 return QLA_ERROR;
1210
David Somayajuluafaf5a22006-09-19 10:28:00 -07001211 spin_lock_irqsave(&ha->hardware_lock, flags);
1212
1213 /*
1214 * If the SCSI Reset Interrupt bit is set, clear it.
1215 * Otherwise, the Soft Reset won't work.
1216 */
1217 ctrl_status = readw(&ha->reg->ctrl_status);
1218 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
1219 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
1220
1221 /* Issue Soft Reset */
1222 writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
1223 readl(&ha->reg->ctrl_status);
1224
1225 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301226 return QLA_SUCCESS;
David C Somayajulu477ffb92007-01-22 12:26:11 -08001227}
1228
1229/**
1230 * qla4xxx_soft_reset - performs soft reset.
1231 * @ha: Pointer to host adapter structure.
1232 **/
1233int qla4xxx_soft_reset(struct scsi_qla_host *ha)
1234{
1235 uint32_t max_wait_time;
1236 unsigned long flags = 0;
Vikas Chaudharyf931c532010-10-06 22:48:07 -07001237 int status;
David C Somayajulu477ffb92007-01-22 12:26:11 -08001238 uint32_t ctrl_status;
1239
Vikas Chaudharyf931c532010-10-06 22:48:07 -07001240 status = qla4xxx_hw_reset(ha);
1241 if (status != QLA_SUCCESS)
1242 return status;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001243
Vikas Chaudharyf931c532010-10-06 22:48:07 -07001244 status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001245 /* Wait until the Network Reset Intr bit is cleared */
1246 max_wait_time = RESET_INTR_TOV;
1247 do {
1248 spin_lock_irqsave(&ha->hardware_lock, flags);
1249 ctrl_status = readw(&ha->reg->ctrl_status);
1250 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1251
1252 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
1253 break;
1254
1255 msleep(1000);
1256 } while ((--max_wait_time));
1257
1258 if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
1259 DEBUG2(printk(KERN_WARNING
1260 "scsi%ld: Network Reset Intr not cleared by "
1261 "Network function, clearing it now!\n",
1262 ha->host_no));
1263 spin_lock_irqsave(&ha->hardware_lock, flags);
1264 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
1265 readl(&ha->reg->ctrl_status);
1266 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1267 }
1268
1269 /* Wait until the firmware tells us the Soft Reset is done */
1270 max_wait_time = SOFT_RESET_TOV;
1271 do {
1272 spin_lock_irqsave(&ha->hardware_lock, flags);
1273 ctrl_status = readw(&ha->reg->ctrl_status);
1274 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1275
1276 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
1277 status = QLA_SUCCESS;
1278 break;
1279 }
1280
1281 msleep(1000);
1282 } while ((--max_wait_time));
1283
1284 /*
1285 * Also, make sure that the SCSI Reset Interrupt bit has been cleared
1286 * after the soft reset has taken place.
1287 */
1288 spin_lock_irqsave(&ha->hardware_lock, flags);
1289 ctrl_status = readw(&ha->reg->ctrl_status);
1290 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
1291 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
1292 readl(&ha->reg->ctrl_status);
1293 }
1294 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1295
1296 /* If soft reset fails then most probably the bios on other
1297 * function is also enabled.
1298 * Since the initialization is sequential the other fn
1299 * wont be able to acknowledge the soft reset.
1300 * Issue a force soft reset to workaround this scenario.
1301 */
1302 if (max_wait_time == 0) {
1303 /* Issue Force Soft Reset */
1304 spin_lock_irqsave(&ha->hardware_lock, flags);
1305 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
1306 readl(&ha->reg->ctrl_status);
1307 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1308 /* Wait until the firmware tells us the Soft Reset is done */
1309 max_wait_time = SOFT_RESET_TOV;
1310 do {
1311 spin_lock_irqsave(&ha->hardware_lock, flags);
1312 ctrl_status = readw(&ha->reg->ctrl_status);
1313 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1314
1315 if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
1316 status = QLA_SUCCESS;
1317 break;
1318 }
1319
1320 msleep(1000);
1321 } while ((--max_wait_time));
1322 }
1323
1324 return status;
1325}
1326
1327/**
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301328 * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001329 * @ha: Pointer to host adapter structure.
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301330 * @res: returned scsi status
David Somayajuluafaf5a22006-09-19 10:28:00 -07001331 *
1332 * This routine is called just prior to a HARD RESET to return all
1333 * outstanding commands back to the Operating System.
1334 * Caller should make sure that the following locks are released
1335 * before this calling routine: Hardware lock, and io_request_lock.
1336 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301337static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001338{
1339 struct srb *srb;
1340 int i;
1341 unsigned long flags;
1342
1343 spin_lock_irqsave(&ha->hardware_lock, flags);
1344 for (i = 0; i < ha->host->can_queue; i++) {
1345 srb = qla4xxx_del_from_active_array(ha, i);
1346 if (srb != NULL) {
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301347 srb->cmd->result = res;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301348 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001349 }
1350 }
1351 spin_unlock_irqrestore(&ha->hardware_lock, flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001352}
1353
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301354void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
1355{
1356 clear_bit(AF_ONLINE, &ha->flags);
1357
1358 /* Disable the board */
1359 ql4_printk(KERN_INFO, ha, "Disabling the board\n");
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301360
1361 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
1362 qla4xxx_mark_all_devices_missing(ha);
1363 clear_bit(AF_INIT_DONE, &ha->flags);
1364}
1365
David Somayajuluafaf5a22006-09-19 10:28:00 -07001366/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07001367 * qla4xxx_recover_adapter - recovers adapter after a fatal error
1368 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001369 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301370static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001371{
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301372 int status = QLA_ERROR;
1373 uint8_t reset_chip = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001374
1375 /* Stall incoming I/O until we are done */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301376 scsi_block_requests(ha->host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001377 clear_bit(AF_ONLINE, &ha->flags);
Mike Christie50a29ae2008-03-04 13:26:53 -06001378
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301379 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001380
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301381 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001382
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301383 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
1384 reset_chip = 1;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001385
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301386 /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
1387 * do not reset adapter, jump to initialize_adapter */
1388 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1389 status = QLA_SUCCESS;
1390 goto recover_ha_init_adapter;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001391 }
1392
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301393 /* For the ISP-82xx adapter, issue a stop_firmware if invoked
1394 * from eh_host_reset or ioctl module */
1395 if (is_qla8022(ha) && !reset_chip &&
1396 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
1397
1398 DEBUG2(ql4_printk(KERN_INFO, ha,
1399 "scsi%ld: %s - Performing stop_firmware...\n",
1400 ha->host_no, __func__));
1401 status = ha->isp_ops->reset_firmware(ha);
1402 if (status == QLA_SUCCESS) {
Nilesh Javali2bd1e2b2010-10-06 22:49:20 -07001403 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
1404 qla4xxx_cmd_wait(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301405 ha->isp_ops->disable_intrs(ha);
1406 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1407 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1408 } else {
1409 /* If the stop_firmware fails then
1410 * reset the entire chip */
1411 reset_chip = 1;
1412 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1413 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1414 }
1415 }
1416
1417 /* Issue full chip reset if recovering from a catastrophic error,
1418 * or if stop_firmware fails for ISP-82xx.
1419 * This is the default case for ISP-4xxx */
1420 if (!is_qla8022(ha) || reset_chip) {
Nilesh Javali2bd1e2b2010-10-06 22:49:20 -07001421 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
1422 qla4xxx_cmd_wait(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301423 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1424 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1425 DEBUG2(ql4_printk(KERN_INFO, ha,
1426 "scsi%ld: %s - Performing chip reset..\n",
1427 ha->host_no, __func__));
1428 status = ha->isp_ops->reset_chip(ha);
1429 }
1430
1431 /* Flush any pending ddb changed AENs */
1432 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1433
1434recover_ha_init_adapter:
1435 /* Upon successful firmware/chip reset, re-initialize the adapter */
1436 if (status == QLA_SUCCESS) {
1437 /* For ISP-4xxx, force function 1 to always initialize
1438 * before function 3 to prevent both funcions from
1439 * stepping on top of the other */
1440 if (!is_qla8022(ha) && (ha->mac_index == 3))
1441 ssleep(6);
1442
1443 /* NOTE: AF_ONLINE flag set upon successful completion of
1444 * qla4xxx_initialize_adapter */
1445 status = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
1446 }
1447
1448 /* Retry failed adapter initialization, if necessary
1449 * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
1450 * case to prevent ping-pong resets between functions */
1451 if (!test_bit(AF_ONLINE, &ha->flags) &&
1452 !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001453 /* Adapter initialization failed, see if we can retry
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301454 * resetting the ha.
1455 * Since we don't want to block the DPC for too long
1456 * with multiple resets in the same thread,
1457 * utilize DPC to retry */
David Somayajuluafaf5a22006-09-19 10:28:00 -07001458 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
1459 ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
1460 DEBUG2(printk("scsi%ld: recover adapter - retrying "
1461 "(%d) more times\n", ha->host_no,
1462 ha->retry_reset_ha_cnt));
1463 set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1464 status = QLA_ERROR;
1465 } else {
1466 if (ha->retry_reset_ha_cnt > 0) {
1467 /* Schedule another Reset HA--DPC will retry */
1468 ha->retry_reset_ha_cnt--;
1469 DEBUG2(printk("scsi%ld: recover adapter - "
1470 "retry remaining %d\n",
1471 ha->host_no,
1472 ha->retry_reset_ha_cnt));
1473 status = QLA_ERROR;
1474 }
1475
1476 if (ha->retry_reset_ha_cnt == 0) {
1477 /* Recover adapter retries have been exhausted.
1478 * Adapter DEAD */
1479 DEBUG2(printk("scsi%ld: recover adapter "
1480 "failed - board disabled\n",
1481 ha->host_no));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301482 qla4xxx_dead_adapter_cleanup(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001483 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1484 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301485 clear_bit(DPC_RESET_HA_FW_CONTEXT,
David Somayajuluafaf5a22006-09-19 10:28:00 -07001486 &ha->dpc_flags);
1487 status = QLA_ERROR;
1488 }
1489 }
1490 } else {
1491 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301492 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001493 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1494 }
1495
1496 ha->adapter_error_count++;
1497
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301498 if (test_bit(AF_ONLINE, &ha->flags))
1499 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001500
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301501 scsi_unblock_requests(ha->host);
1502
1503 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
1504 DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001505 status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301506
David Somayajuluafaf5a22006-09-19 10:28:00 -07001507 return status;
1508}
1509
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07001510static void qla4xxx_relogin_all_devices(struct scsi_qla_host *ha)
1511{
1512 struct ddb_entry *ddb_entry, *dtemp;
1513
1514 list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
1515 if ((atomic_read(&ddb_entry->state) == DDB_STATE_MISSING) ||
1516 (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD)) {
1517 if (ddb_entry->fw_ddb_device_state ==
1518 DDB_DS_SESSION_ACTIVE) {
1519 atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
1520 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
1521 " marked ONLINE\n", ha->host_no, __func__,
1522 ddb_entry->fw_ddb_index);
1523
1524 iscsi_unblock_session(ddb_entry->sess);
1525 } else
1526 qla4xxx_relogin_device(ha, ddb_entry);
1527 }
1528 }
1529}
1530
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301531void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
1532{
Lalit Chandivade1b468072011-05-17 23:17:09 -07001533 if (ha->dpc_thread)
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301534 queue_work(ha->dpc_thread, &ha->dpc_work);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301535}
1536
David Somayajuluafaf5a22006-09-19 10:28:00 -07001537/**
1538 * qla4xxx_do_dpc - dpc routine
1539 * @data: in our case pointer to adapter structure
1540 *
1541 * This routine is a task that is schedule by the interrupt handler
1542 * to perform the background processing for interrupts. We put it
1543 * on a task queue that is consumed whenever the scheduler runs; that's
1544 * so you can do anything (i.e. put the process to sleep etc). In fact,
1545 * the mid-level tries to sleep when it reaches the driver threshold
1546 * "host->can_queue". This can cause a panic if we were in our interrupt code.
1547 **/
David Howellsc4028952006-11-22 14:57:56 +00001548static void qla4xxx_do_dpc(struct work_struct *work)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001549{
David Howellsc4028952006-11-22 14:57:56 +00001550 struct scsi_qla_host *ha =
1551 container_of(work, struct scsi_qla_host, dpc_work);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001552 struct ddb_entry *ddb_entry, *dtemp;
David C Somayajulu477ffb92007-01-22 12:26:11 -08001553 int status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001554
David C Somayajuluf26b9042006-11-15 16:41:09 -08001555 DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301556 "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
1557 ha->host_no, __func__, ha->flags, ha->dpc_flags))
David Somayajuluafaf5a22006-09-19 10:28:00 -07001558
1559 /* Initialization not yet finished. Don't do anything yet. */
1560 if (!test_bit(AF_INIT_DONE, &ha->flags))
Lalit Chandivade1b468072011-05-17 23:17:09 -07001561 return;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001562
Lalit Chandivade2232be02010-07-30 14:38:47 +05301563 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1564 DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
1565 ha->host_no, __func__, ha->flags));
Lalit Chandivade1b468072011-05-17 23:17:09 -07001566 return;
Lalit Chandivade2232be02010-07-30 14:38:47 +05301567 }
1568
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301569 if (is_qla8022(ha)) {
1570 if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
1571 qla4_8xxx_idc_lock(ha);
1572 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
1573 QLA82XX_DEV_FAILED);
1574 qla4_8xxx_idc_unlock(ha);
1575 ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
1576 qla4_8xxx_device_state_handler(ha);
1577 }
1578 if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
1579 qla4_8xxx_need_qsnt_handler(ha);
1580 }
1581 }
1582
1583 if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
1584 (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07001585 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301586 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
1587 if (ql4xdontresethba) {
1588 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
1589 ha->host_no, __func__));
1590 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
1591 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1592 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1593 goto dpc_post_reset_ha;
1594 }
1595 if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
1596 test_bit(DPC_RESET_HA, &ha->dpc_flags))
1597 qla4xxx_recover_adapter(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001598
David C Somayajulu477ffb92007-01-22 12:26:11 -08001599 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001600 uint8_t wait_time = RESET_INTR_TOV;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001601
David Somayajuluafaf5a22006-09-19 10:28:00 -07001602 while ((readw(&ha->reg->ctrl_status) &
1603 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
1604 if (--wait_time == 0)
1605 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001606 msleep(1000);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001607 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001608 if (wait_time == 0)
1609 DEBUG2(printk("scsi%ld: %s: SR|FSR "
1610 "bit not cleared-- resetting\n",
1611 ha->host_no, __func__));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301612 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
David C Somayajulu477ffb92007-01-22 12:26:11 -08001613 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
1614 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301615 status = qla4xxx_recover_adapter(ha);
David C Somayajulu477ffb92007-01-22 12:26:11 -08001616 }
1617 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1618 if (status == QLA_SUCCESS)
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301619 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001620 }
1621 }
1622
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301623dpc_post_reset_ha:
David Somayajuluafaf5a22006-09-19 10:28:00 -07001624 /* ---- process AEN? --- */
1625 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
1626 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
1627
1628 /* ---- Get DHCP IP Address? --- */
1629 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
1630 qla4xxx_get_dhcp_ip_address(ha);
1631
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301632 /* ---- link change? --- */
1633 if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
1634 if (!test_bit(AF_LINK_UP, &ha->flags)) {
1635 /* ---- link down? --- */
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07001636 qla4xxx_mark_all_devices_missing(ha);
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301637 } else {
1638 /* ---- link up? --- *
1639 * F/W will auto login to all devices ONLY ONCE after
1640 * link up during driver initialization and runtime
1641 * fatal error recovery. Therefore, the driver must
1642 * manually relogin to devices when recovering from
1643 * connection failures, logouts, expired KATO, etc. */
1644
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07001645 qla4xxx_relogin_all_devices(ha);
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301646 }
1647 }
1648
David Somayajuluafaf5a22006-09-19 10:28:00 -07001649 /* ---- relogin device? --- */
1650 if (adapter_up(ha) &&
1651 test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
1652 list_for_each_entry_safe(ddb_entry, dtemp,
1653 &ha->ddb_list, list) {
1654 if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
1655 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
1656 qla4xxx_relogin_device(ha, ddb_entry);
1657
1658 /*
1659 * If mbx cmd times out there is no point
1660 * in continuing further.
1661 * With large no of targets this can hang
1662 * the system.
1663 */
1664 if (test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
1665 printk(KERN_WARNING "scsi%ld: %s: "
1666 "need to reset hba\n",
1667 ha->host_no, __func__);
1668 break;
1669 }
1670 }
1671 }
Vikas Chaudhary0753b482010-07-30 14:27:19 +05301672
David Somayajuluafaf5a22006-09-19 10:28:00 -07001673}
1674
1675/**
1676 * qla4xxx_free_adapter - release the adapter
1677 * @ha: pointer to adapter structure
1678 **/
1679static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
1680{
1681
1682 if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
1683 /* Turn-off interrupts on the card. */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301684 ha->isp_ops->disable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001685 }
1686
David Somayajuluafaf5a22006-09-19 10:28:00 -07001687 /* Remove timer thread, if present */
1688 if (ha->timer_active)
1689 qla4xxx_stop_timer(ha);
1690
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301691 /* Kill the kernel thread for this host */
1692 if (ha->dpc_thread)
1693 destroy_workqueue(ha->dpc_thread);
1694
1695 /* Put firmware in known state */
1696 ha->isp_ops->reset_firmware(ha);
1697
1698 if (is_qla8022(ha)) {
1699 qla4_8xxx_idc_lock(ha);
1700 qla4_8xxx_clear_drv_active(ha);
1701 qla4_8xxx_idc_unlock(ha);
1702 }
1703
David Somayajuluafaf5a22006-09-19 10:28:00 -07001704 /* Detach interrupts */
1705 if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301706 qla4xxx_free_irqs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001707
David C Somayajulubee4fe82007-05-23 18:03:32 -07001708 /* free extra memory */
1709 qla4xxx_mem_free(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301710}
David C Somayajulubee4fe82007-05-23 18:03:32 -07001711
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301712int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
1713{
1714 int status = 0;
1715 uint8_t revision_id;
1716 unsigned long mem_base, mem_len, db_base, db_len;
1717 struct pci_dev *pdev = ha->pdev;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001718
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301719 status = pci_request_regions(pdev, DRIVER_NAME);
1720 if (status) {
1721 printk(KERN_WARNING
1722 "scsi(%ld) Failed to reserve PIO regions (%s) "
1723 "status=%d\n", ha->host_no, pci_name(pdev), status);
1724 goto iospace_error_exit;
1725 }
1726
1727 pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
1728 DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
1729 __func__, revision_id));
1730 ha->revision_id = revision_id;
1731
1732 /* remap phys address */
1733 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
1734 mem_len = pci_resource_len(pdev, 0);
1735 DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
1736 __func__, mem_base, mem_len));
1737
1738 /* mapping of pcibase pointer */
1739 ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
1740 if (!ha->nx_pcibase) {
1741 printk(KERN_ERR
1742 "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
1743 pci_release_regions(ha->pdev);
1744 goto iospace_error_exit;
1745 }
1746
1747 /* Mapping of IO base pointer, door bell read and write pointer */
1748
1749 /* mapping of IO base pointer */
1750 ha->qla4_8xxx_reg =
1751 (struct device_reg_82xx __iomem *)((uint8_t *)ha->nx_pcibase +
1752 0xbc000 + (ha->pdev->devfn << 11));
1753
1754 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
1755 db_len = pci_resource_len(pdev, 4);
1756
Shyam Sundar2657c802010-10-06 22:50:29 -07001757 ha->nx_db_wr_ptr = (ha->pdev->devfn == 4 ? QLA82XX_CAM_RAM_DB1 :
1758 QLA82XX_CAM_RAM_DB2);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301759
Shyam Sundar2657c802010-10-06 22:50:29 -07001760 return 0;
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301761iospace_error_exit:
1762 return -ENOMEM;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001763}
1764
1765/***
1766 * qla4xxx_iospace_config - maps registers
1767 * @ha: pointer to adapter structure
1768 *
1769 * This routines maps HBA's registers from the pci address space
1770 * into the kernel virtual address space for memory mapped i/o.
1771 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301772int qla4xxx_iospace_config(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001773{
1774 unsigned long pio, pio_len, pio_flags;
1775 unsigned long mmio, mmio_len, mmio_flags;
1776
1777 pio = pci_resource_start(ha->pdev, 0);
1778 pio_len = pci_resource_len(ha->pdev, 0);
1779 pio_flags = pci_resource_flags(ha->pdev, 0);
1780 if (pio_flags & IORESOURCE_IO) {
1781 if (pio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301782 ql4_printk(KERN_WARNING, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07001783 "Invalid PCI I/O region size\n");
1784 pio = 0;
1785 }
1786 } else {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301787 ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001788 pio = 0;
1789 }
1790
1791 /* Use MMIO operations for all accesses. */
1792 mmio = pci_resource_start(ha->pdev, 1);
1793 mmio_len = pci_resource_len(ha->pdev, 1);
1794 mmio_flags = pci_resource_flags(ha->pdev, 1);
1795
1796 if (!(mmio_flags & IORESOURCE_MEM)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301797 ql4_printk(KERN_ERR, ha,
1798 "region #0 not an MMIO resource, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001799
1800 goto iospace_error_exit;
1801 }
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301802
David Somayajuluafaf5a22006-09-19 10:28:00 -07001803 if (mmio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301804 ql4_printk(KERN_ERR, ha,
1805 "Invalid PCI mem region size, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001806 goto iospace_error_exit;
1807 }
1808
1809 if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301810 ql4_printk(KERN_WARNING, ha,
1811 "Failed to reserve PIO/MMIO regions\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001812
1813 goto iospace_error_exit;
1814 }
1815
1816 ha->pio_address = pio;
1817 ha->pio_length = pio_len;
1818 ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
1819 if (!ha->reg) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301820 ql4_printk(KERN_ERR, ha,
1821 "cannot remap MMIO, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001822
1823 goto iospace_error_exit;
1824 }
1825
1826 return 0;
1827
1828iospace_error_exit:
1829 return -ENOMEM;
1830}
1831
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301832static struct isp_operations qla4xxx_isp_ops = {
1833 .iospace_config = qla4xxx_iospace_config,
1834 .pci_config = qla4xxx_pci_config,
1835 .disable_intrs = qla4xxx_disable_intrs,
1836 .enable_intrs = qla4xxx_enable_intrs,
1837 .start_firmware = qla4xxx_start_firmware,
1838 .intr_handler = qla4xxx_intr_handler,
1839 .interrupt_service_routine = qla4xxx_interrupt_service_routine,
1840 .reset_chip = qla4xxx_soft_reset,
1841 .reset_firmware = qla4xxx_hw_reset,
1842 .queue_iocb = qla4xxx_queue_iocb,
1843 .complete_iocb = qla4xxx_complete_iocb,
1844 .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
1845 .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
1846 .get_sys_info = qla4xxx_get_sys_info,
1847};
1848
1849static struct isp_operations qla4_8xxx_isp_ops = {
1850 .iospace_config = qla4_8xxx_iospace_config,
1851 .pci_config = qla4_8xxx_pci_config,
1852 .disable_intrs = qla4_8xxx_disable_intrs,
1853 .enable_intrs = qla4_8xxx_enable_intrs,
1854 .start_firmware = qla4_8xxx_load_risc,
1855 .intr_handler = qla4_8xxx_intr_handler,
1856 .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
1857 .reset_chip = qla4_8xxx_isp_reset,
1858 .reset_firmware = qla4_8xxx_stop_firmware,
1859 .queue_iocb = qla4_8xxx_queue_iocb,
1860 .complete_iocb = qla4_8xxx_complete_iocb,
1861 .rd_shdw_req_q_out = qla4_8xxx_rd_shdw_req_q_out,
1862 .rd_shdw_rsp_q_in = qla4_8xxx_rd_shdw_rsp_q_in,
1863 .get_sys_info = qla4_8xxx_get_sys_info,
1864};
1865
1866uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
1867{
1868 return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
1869}
1870
1871uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
1872{
1873 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
1874}
1875
1876uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
1877{
1878 return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
1879}
1880
1881uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
1882{
1883 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
1884}
1885
David Somayajuluafaf5a22006-09-19 10:28:00 -07001886/**
1887 * qla4xxx_probe_adapter - callback function to probe HBA
1888 * @pdev: pointer to pci_dev structure
1889 * @pci_device_id: pointer to pci_device entry
1890 *
1891 * This routine will probe for Qlogic 4xxx iSCSI host adapters.
1892 * It returns zero if successful. It also initializes all data necessary for
1893 * the driver.
1894 **/
1895static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
1896 const struct pci_device_id *ent)
1897{
1898 int ret = -ENODEV, status;
1899 struct Scsi_Host *host;
1900 struct scsi_qla_host *ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001901 uint8_t init_retry_count = 0;
1902 char buf[34];
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301903 struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
Prasanna Mumbaif9880e72011-03-21 03:34:26 -07001904 uint32_t dev_state;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001905
1906 if (pci_enable_device(pdev))
1907 return -1;
1908
1909 host = scsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha));
1910 if (host == NULL) {
1911 printk(KERN_WARNING
1912 "qla4xxx: Couldn't allocate host from scsi layer!\n");
1913 goto probe_disable_device;
1914 }
1915
1916 /* Clear our data area */
1917 ha = (struct scsi_qla_host *) host->hostdata;
1918 memset(ha, 0, sizeof(*ha));
1919
1920 /* Save the information from PCI BIOS. */
1921 ha->pdev = pdev;
1922 ha->host = host;
1923 ha->host_no = host->host_no;
1924
Lalit Chandivade2232be02010-07-30 14:38:47 +05301925 pci_enable_pcie_error_reporting(pdev);
1926
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301927 /* Setup Runtime configurable options */
1928 if (is_qla8022(ha)) {
1929 ha->isp_ops = &qla4_8xxx_isp_ops;
1930 rwlock_init(&ha->hw_lock);
1931 ha->qdr_sn_window = -1;
1932 ha->ddr_mn_window = -1;
1933 ha->curr_window = 255;
1934 ha->func_num = PCI_FUNC(ha->pdev->devfn);
1935 nx_legacy_intr = &legacy_intr[ha->func_num];
1936 ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
1937 ha->nx_legacy_intr.tgt_status_reg =
1938 nx_legacy_intr->tgt_status_reg;
1939 ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
1940 ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
1941 } else {
1942 ha->isp_ops = &qla4xxx_isp_ops;
1943 }
1944
Lalit Chandivade2232be02010-07-30 14:38:47 +05301945 /* Set EEH reset type to fundamental if required by hba */
1946 if (is_qla8022(ha))
1947 pdev->needs_freset = 1;
1948
David Somayajuluafaf5a22006-09-19 10:28:00 -07001949 /* Configure PCI I/O space. */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301950 ret = ha->isp_ops->iospace_config(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001951 if (ret)
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301952 goto probe_failed_ioconfig;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001953
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301954 ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
David Somayajuluafaf5a22006-09-19 10:28:00 -07001955 pdev->device, pdev->irq, ha->reg);
1956
1957 qla4xxx_config_dma_addressing(ha);
1958
1959 /* Initialize lists and spinlocks. */
1960 INIT_LIST_HEAD(&ha->ddb_list);
1961 INIT_LIST_HEAD(&ha->free_srb_q);
1962
1963 mutex_init(&ha->mbox_sem);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301964 init_completion(&ha->mbx_intr_comp);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001965
1966 spin_lock_init(&ha->hardware_lock);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001967
1968 /* Allocate dma buffers */
1969 if (qla4xxx_mem_alloc(ha)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301970 ql4_printk(KERN_WARNING, ha,
1971 "[ERROR] Failed to allocate memory for adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001972
1973 ret = -ENOMEM;
1974 goto probe_failed;
1975 }
1976
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301977 if (is_qla8022(ha))
1978 (void) qla4_8xxx_get_flash_info(ha);
1979
David Somayajuluafaf5a22006-09-19 10:28:00 -07001980 /*
1981 * Initialize the Host adapter request/response queues and
1982 * firmware
1983 * NOTE: interrupts enabled upon successful completion
1984 */
1985 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301986 while ((!test_bit(AF_ONLINE, &ha->flags)) &&
1987 init_retry_count++ < MAX_INIT_RETRIES) {
Prasanna Mumbaif9880e72011-03-21 03:34:26 -07001988
1989 if (is_qla8022(ha)) {
1990 qla4_8xxx_idc_lock(ha);
1991 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
1992 qla4_8xxx_idc_unlock(ha);
1993 if (dev_state == QLA82XX_DEV_FAILED) {
1994 ql4_printk(KERN_WARNING, ha, "%s: don't retry "
1995 "initialize adapter. H/W is in failed state\n",
1996 __func__);
1997 break;
1998 }
1999 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002000 DEBUG2(printk("scsi: %s: retrying adapter initialization "
2001 "(%d)\n", __func__, init_retry_count));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302002
2003 if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
2004 continue;
2005
David Somayajuluafaf5a22006-09-19 10:28:00 -07002006 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
2007 }
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302008
2009 if (!test_bit(AF_ONLINE, &ha->flags)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302010 ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002011
Lalit Chandivadefe998522010-12-02 22:12:36 -08002012 if (is_qla8022(ha) && ql4xdontresethba) {
2013 /* Put the device in failed state. */
2014 DEBUG2(printk(KERN_ERR "HW STATE: FAILED\n"));
2015 qla4_8xxx_idc_lock(ha);
2016 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2017 QLA82XX_DEV_FAILED);
2018 qla4_8xxx_idc_unlock(ha);
2019 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002020 ret = -ENODEV;
2021 goto probe_failed;
2022 }
2023
2024 host->cmd_per_lun = 3;
2025 host->max_channel = 0;
2026 host->max_lun = MAX_LUNS - 1;
2027 host->max_id = MAX_TARGETS;
2028 host->max_cmd_len = IOCB_MAX_CDB_LEN;
2029 host->can_queue = MAX_SRBS ;
2030 host->transportt = qla4xxx_scsi_transport;
2031
2032 ret = scsi_init_shared_tag_map(host, MAX_SRBS);
2033 if (ret) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302034 ql4_printk(KERN_WARNING, ha,
2035 "scsi_init_shared_tag_map failed\n");
2036 goto probe_failed;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002037 }
2038
2039 /* Startup the kernel thread for this host adapter. */
2040 DEBUG2(printk("scsi: %s: Starting kernel thread for "
2041 "qla4xxx_dpc\n", __func__));
2042 sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
2043 ha->dpc_thread = create_singlethread_workqueue(buf);
2044 if (!ha->dpc_thread) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302045 ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002046 ret = -ENODEV;
2047 goto probe_failed;
2048 }
David Howellsc4028952006-11-22 14:57:56 +00002049 INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002050
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302051 /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
2052 * (which is called indirectly by qla4xxx_initialize_adapter),
2053 * so that irqs will be registered after crbinit but before
2054 * mbx_intr_enable.
2055 */
2056 if (!is_qla8022(ha)) {
2057 ret = qla4xxx_request_irqs(ha);
2058 if (ret) {
2059 ql4_printk(KERN_WARNING, ha, "Failed to reserve "
2060 "interrupt %d already in use.\n", pdev->irq);
2061 goto probe_failed;
2062 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002063 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002064
Lalit Chandivade2232be02010-07-30 14:38:47 +05302065 pci_save_state(ha->pdev);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302066 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002067
2068 /* Start timer thread. */
2069 qla4xxx_start_timer(ha, qla4xxx_timer, 1);
2070
2071 set_bit(AF_INIT_DONE, &ha->flags);
2072
2073 pci_set_drvdata(pdev, ha);
2074
2075 ret = scsi_add_host(host, &pdev->dev);
2076 if (ret)
2077 goto probe_failed;
2078
David Somayajuluafaf5a22006-09-19 10:28:00 -07002079 printk(KERN_INFO
2080 " QLogic iSCSI HBA Driver version: %s\n"
2081 " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
2082 qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
2083 ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
2084 ha->patch_number, ha->build_number);
Mike Christie921601b2008-01-31 13:36:49 -06002085 scsi_scan_host(host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002086 return 0;
2087
David Somayajuluafaf5a22006-09-19 10:28:00 -07002088probe_failed:
2089 qla4xxx_free_adapter(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302090
2091probe_failed_ioconfig:
Lalit Chandivade2232be02010-07-30 14:38:47 +05302092 pci_disable_pcie_error_reporting(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002093 scsi_host_put(ha->host);
2094
2095probe_disable_device:
2096 pci_disable_device(pdev);
2097
2098 return ret;
2099}
2100
2101/**
Karen Higgins7eece5a2011-03-21 03:34:29 -07002102 * qla4xxx_prevent_other_port_reinit - prevent other port from re-initialize
2103 * @ha: pointer to adapter structure
2104 *
2105 * Mark the other ISP-4xxx port to indicate that the driver is being removed,
2106 * so that the other port will not re-initialize while in the process of
2107 * removing the ha due to driver unload or hba hotplug.
2108 **/
2109static void qla4xxx_prevent_other_port_reinit(struct scsi_qla_host *ha)
2110{
2111 struct scsi_qla_host *other_ha = NULL;
2112 struct pci_dev *other_pdev = NULL;
2113 int fn = ISP4XXX_PCI_FN_2;
2114
2115 /*iscsi function numbers for ISP4xxx is 1 and 3*/
2116 if (PCI_FUNC(ha->pdev->devfn) & BIT_1)
2117 fn = ISP4XXX_PCI_FN_1;
2118
2119 other_pdev =
2120 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
2121 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
2122 fn));
2123
2124 /* Get other_ha if other_pdev is valid and state is enable*/
2125 if (other_pdev) {
2126 if (atomic_read(&other_pdev->enable_cnt)) {
2127 other_ha = pci_get_drvdata(other_pdev);
2128 if (other_ha) {
2129 set_bit(AF_HA_REMOVAL, &other_ha->flags);
2130 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: "
2131 "Prevent %s reinit\n", __func__,
2132 dev_name(&other_ha->pdev->dev)));
2133 }
2134 }
2135 pci_dev_put(other_pdev);
2136 }
2137}
2138
2139/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002140 * qla4xxx_remove_adapter - calback function to remove adapter.
2141 * @pci_dev: PCI device pointer
2142 **/
2143static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
2144{
2145 struct scsi_qla_host *ha;
2146
2147 ha = pci_get_drvdata(pdev);
2148
Karen Higgins7eece5a2011-03-21 03:34:29 -07002149 if (!is_qla8022(ha))
2150 qla4xxx_prevent_other_port_reinit(ha);
David C Somayajulubee4fe82007-05-23 18:03:32 -07002151
David Somayajuluafaf5a22006-09-19 10:28:00 -07002152 /* remove devs from iscsi_sessions to scsi_devices */
2153 qla4xxx_free_ddb_list(ha);
2154
2155 scsi_remove_host(ha->host);
2156
2157 qla4xxx_free_adapter(ha);
2158
2159 scsi_host_put(ha->host);
2160
Lalit Chandivade2232be02010-07-30 14:38:47 +05302161 pci_disable_pcie_error_reporting(pdev);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302162 pci_disable_device(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002163 pci_set_drvdata(pdev, NULL);
2164}
2165
2166/**
2167 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
2168 * @ha: HA context
2169 *
2170 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
2171 * supported addressing method.
2172 */
Adrian Bunk47975472007-04-26 00:35:16 -07002173static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002174{
2175 int retval;
2176
2177 /* Update our PCI device dma_mask for full 64 bit mask */
Yang Hongyang6a355282009-04-06 19:01:13 -07002178 if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
2179 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002180 dev_dbg(&ha->pdev->dev,
2181 "Failed to set 64 bit PCI consistent mask; "
2182 "using 32 bit.\n");
2183 retval = pci_set_consistent_dma_mask(ha->pdev,
Yang Hongyang284901a2009-04-06 19:01:15 -07002184 DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07002185 }
2186 } else
Yang Hongyang284901a2009-04-06 19:01:15 -07002187 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07002188}
2189
2190static int qla4xxx_slave_alloc(struct scsi_device *sdev)
2191{
2192 struct iscsi_cls_session *sess = starget_to_session(sdev->sdev_target);
2193 struct ddb_entry *ddb = sess->dd_data;
Vikas Chaudhary8bb40332011-03-21 03:34:31 -07002194 int queue_depth = QL4_DEF_QDEPTH;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002195
2196 sdev->hostdata = ddb;
2197 sdev->tagged_supported = 1;
Vikas Chaudhary8bb40332011-03-21 03:34:31 -07002198
2199 if (ql4xmaxqdepth != 0 && ql4xmaxqdepth <= 0xffffU)
2200 queue_depth = ql4xmaxqdepth;
2201
2202 scsi_activate_tcq(sdev, queue_depth);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002203 return 0;
2204}
2205
2206static int qla4xxx_slave_configure(struct scsi_device *sdev)
2207{
2208 sdev->tagged_supported = 1;
2209 return 0;
2210}
2211
2212static void qla4xxx_slave_destroy(struct scsi_device *sdev)
2213{
2214 scsi_deactivate_tcq(sdev, 1);
2215}
2216
2217/**
2218 * qla4xxx_del_from_active_array - returns an active srb
2219 * @ha: Pointer to host adapter structure.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02002220 * @index: index into the active_array
David Somayajuluafaf5a22006-09-19 10:28:00 -07002221 *
2222 * This routine removes and returns the srb at the specified index
2223 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302224struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
2225 uint32_t index)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002226{
2227 struct srb *srb = NULL;
Vikas Chaudhary53698872010-04-28 11:41:59 +05302228 struct scsi_cmnd *cmd = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002229
Vikas Chaudhary53698872010-04-28 11:41:59 +05302230 cmd = scsi_host_find_tag(ha->host, index);
2231 if (!cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002232 return srb;
2233
Vikas Chaudhary53698872010-04-28 11:41:59 +05302234 srb = (struct srb *)CMD_SP(cmd);
2235 if (!srb)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002236 return srb;
2237
2238 /* update counters */
2239 if (srb->flags & SRB_DMA_VALID) {
2240 ha->req_q_count += srb->iocb_cnt;
2241 ha->iocb_cnt -= srb->iocb_cnt;
2242 if (srb->cmd)
Vikas Chaudhary53698872010-04-28 11:41:59 +05302243 srb->cmd->host_scribble =
2244 (unsigned char *)(unsigned long) MAX_SRBS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002245 }
2246 return srb;
2247}
2248
2249/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002250 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302251 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07002252 * @cmd: Scsi Command to wait on.
2253 *
2254 * This routine waits for the command to be returned by the Firmware
2255 * for some max time.
2256 **/
2257static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
2258 struct scsi_cmnd *cmd)
2259{
2260 int done = 0;
2261 struct srb *rp;
2262 uint32_t max_wait_time = EH_WAIT_CMD_TOV;
Lalit Chandivade2232be02010-07-30 14:38:47 +05302263 int ret = SUCCESS;
2264
2265 /* Dont wait on command if PCI error is being handled
2266 * by PCI AER driver
2267 */
2268 if (unlikely(pci_channel_offline(ha->pdev)) ||
2269 (test_bit(AF_EEH_BUSY, &ha->flags))) {
2270 ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
2271 ha->host_no, __func__);
2272 return ret;
2273 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002274
2275 do {
2276 /* Checking to see if its returned to OS */
Vikas Chaudhary53698872010-04-28 11:41:59 +05302277 rp = (struct srb *) CMD_SP(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002278 if (rp == NULL) {
2279 done++;
2280 break;
2281 }
2282
2283 msleep(2000);
2284 } while (max_wait_time--);
2285
2286 return done;
2287}
2288
2289/**
2290 * qla4xxx_wait_for_hba_online - waits for HBA to come online
2291 * @ha: Pointer to host adapter structure
2292 **/
2293static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
2294{
2295 unsigned long wait_online;
2296
Vikas Chaudharyf581a3f2010-10-06 22:47:48 -07002297 wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002298 while (time_before(jiffies, wait_online)) {
2299
2300 if (adapter_up(ha))
2301 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002302
2303 msleep(2000);
2304 }
2305
2306 return QLA_ERROR;
2307}
2308
2309/**
Mike Christiece545032008-02-29 18:25:20 -06002310 * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02002311 * @ha: pointer to HBA
David Somayajuluafaf5a22006-09-19 10:28:00 -07002312 * @t: target id
2313 * @l: lun id
2314 *
2315 * This function waits for all outstanding commands to a lun to complete. It
2316 * returns 0 if all pending commands are returned and 1 otherwise.
2317 **/
Mike Christiece545032008-02-29 18:25:20 -06002318static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
2319 struct scsi_target *stgt,
2320 struct scsi_device *sdev)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002321{
2322 int cnt;
2323 int status = 0;
2324 struct scsi_cmnd *cmd;
2325
2326 /*
Mike Christiece545032008-02-29 18:25:20 -06002327 * Waiting for all commands for the designated target or dev
2328 * in the active array
David Somayajuluafaf5a22006-09-19 10:28:00 -07002329 */
2330 for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
2331 cmd = scsi_host_find_tag(ha->host, cnt);
Mike Christiece545032008-02-29 18:25:20 -06002332 if (cmd && stgt == scsi_target(cmd->device) &&
2333 (!sdev || sdev == cmd->device)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002334 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
2335 status++;
2336 break;
2337 }
2338 }
2339 }
2340 return status;
2341}
2342
2343/**
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302344 * qla4xxx_eh_abort - callback for abort task.
2345 * @cmd: Pointer to Linux's SCSI command structure
2346 *
2347 * This routine is called by the Linux OS to abort the specified
2348 * command.
2349 **/
2350static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
2351{
2352 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2353 unsigned int id = cmd->device->id;
2354 unsigned int lun = cmd->device->lun;
Mike Christie92b3e5b2010-10-06 22:51:17 -07002355 unsigned long flags;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302356 struct srb *srb = NULL;
2357 int ret = SUCCESS;
2358 int wait = 0;
2359
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302360 ql4_printk(KERN_INFO, ha,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002361 "scsi%ld:%d:%d: Abort command issued cmd=%p\n",
2362 ha->host_no, id, lun, cmd);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302363
Mike Christie92b3e5b2010-10-06 22:51:17 -07002364 spin_lock_irqsave(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302365 srb = (struct srb *) CMD_SP(cmd);
Mike Christie92b3e5b2010-10-06 22:51:17 -07002366 if (!srb) {
2367 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302368 return SUCCESS;
Mike Christie92b3e5b2010-10-06 22:51:17 -07002369 }
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302370 kref_get(&srb->srb_ref);
Mike Christie92b3e5b2010-10-06 22:51:17 -07002371 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302372
2373 if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
2374 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
2375 ha->host_no, id, lun));
2376 ret = FAILED;
2377 } else {
2378 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
2379 ha->host_no, id, lun));
2380 wait = 1;
2381 }
2382
2383 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
2384
2385 /* Wait for command to complete */
2386 if (wait) {
2387 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
2388 DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
2389 ha->host_no, id, lun));
2390 ret = FAILED;
2391 }
2392 }
2393
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302394 ql4_printk(KERN_INFO, ha,
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302395 "scsi%ld:%d:%d: Abort command - %s\n",
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002396 ha->host_no, id, lun, (ret == SUCCESS) ? "succeeded" : "failed");
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302397
2398 return ret;
2399}
2400
2401/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002402 * qla4xxx_eh_device_reset - callback for target reset.
2403 * @cmd: Pointer to Linux's SCSI command structure
2404 *
2405 * This routine is called by the Linux OS to reset all luns on the
2406 * specified target.
2407 **/
2408static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
2409{
2410 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2411 struct ddb_entry *ddb_entry = cmd->device->hostdata;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002412 int ret = FAILED, stat;
2413
Karen Higgins612f7342009-07-15 15:03:01 -05002414 if (!ddb_entry)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002415 return ret;
2416
Mike Christiec01be6d2010-07-22 16:59:49 +05302417 ret = iscsi_block_scsi_eh(cmd);
2418 if (ret)
2419 return ret;
2420 ret = FAILED;
2421
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302422 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002423 "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
2424 cmd->device->channel, cmd->device->id, cmd->device->lun);
2425
2426 DEBUG2(printk(KERN_INFO
2427 "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
2428 "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
Jens Axboe242f9dc2008-09-14 05:55:09 -07002429 cmd, jiffies, cmd->request->timeout / HZ,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002430 ha->dpc_flags, cmd->result, cmd->allowed));
2431
2432 /* FIXME: wait for hba to go online */
2433 stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
2434 if (stat != QLA_SUCCESS) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302435 ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002436 goto eh_dev_reset_done;
2437 }
2438
Mike Christiece545032008-02-29 18:25:20 -06002439 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2440 cmd->device)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302441 ql4_printk(KERN_INFO, ha,
Mike Christiece545032008-02-29 18:25:20 -06002442 "DEVICE RESET FAILED - waiting for "
2443 "commands.\n");
2444 goto eh_dev_reset_done;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002445 }
2446
David C Somayajulu9d562912008-03-19 11:23:03 -07002447 /* Send marker. */
2448 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2449 MM_LUN_RESET) != QLA_SUCCESS)
2450 goto eh_dev_reset_done;
2451
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302452 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002453 "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
2454 ha->host_no, cmd->device->channel, cmd->device->id,
2455 cmd->device->lun);
2456
2457 ret = SUCCESS;
2458
2459eh_dev_reset_done:
2460
2461 return ret;
2462}
2463
2464/**
Mike Christiece545032008-02-29 18:25:20 -06002465 * qla4xxx_eh_target_reset - callback for target reset.
2466 * @cmd: Pointer to Linux's SCSI command structure
2467 *
2468 * This routine is called by the Linux OS to reset the target.
2469 **/
2470static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
2471{
2472 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2473 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christiec01be6d2010-07-22 16:59:49 +05302474 int stat, ret;
Mike Christiece545032008-02-29 18:25:20 -06002475
2476 if (!ddb_entry)
2477 return FAILED;
2478
Mike Christiec01be6d2010-07-22 16:59:49 +05302479 ret = iscsi_block_scsi_eh(cmd);
2480 if (ret)
2481 return ret;
2482
Mike Christiece545032008-02-29 18:25:20 -06002483 starget_printk(KERN_INFO, scsi_target(cmd->device),
2484 "WARM TARGET RESET ISSUED.\n");
2485
2486 DEBUG2(printk(KERN_INFO
2487 "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
2488 "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
Jens Axboe242f9dc2008-09-14 05:55:09 -07002489 ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
Mike Christiece545032008-02-29 18:25:20 -06002490 ha->dpc_flags, cmd->result, cmd->allowed));
2491
2492 stat = qla4xxx_reset_target(ha, ddb_entry);
2493 if (stat != QLA_SUCCESS) {
2494 starget_printk(KERN_INFO, scsi_target(cmd->device),
2495 "WARM TARGET RESET FAILED.\n");
2496 return FAILED;
2497 }
2498
Mike Christiece545032008-02-29 18:25:20 -06002499 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2500 NULL)) {
2501 starget_printk(KERN_INFO, scsi_target(cmd->device),
2502 "WARM TARGET DEVICE RESET FAILED - "
2503 "waiting for commands.\n");
2504 return FAILED;
2505 }
2506
David C Somayajulu9d562912008-03-19 11:23:03 -07002507 /* Send marker. */
2508 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2509 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
2510 starget_printk(KERN_INFO, scsi_target(cmd->device),
2511 "WARM TARGET DEVICE RESET FAILED - "
2512 "marker iocb failed.\n");
2513 return FAILED;
2514 }
2515
Mike Christiece545032008-02-29 18:25:20 -06002516 starget_printk(KERN_INFO, scsi_target(cmd->device),
2517 "WARM TARGET RESET SUCCEEDED.\n");
2518 return SUCCESS;
2519}
2520
2521/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002522 * qla4xxx_eh_host_reset - kernel callback
2523 * @cmd: Pointer to Linux's SCSI command structure
2524 *
2525 * This routine is invoked by the Linux kernel to perform fatal error
2526 * recovery on the specified adapter.
2527 **/
2528static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
2529{
2530 int return_status = FAILED;
2531 struct scsi_qla_host *ha;
2532
2533 ha = (struct scsi_qla_host *) cmd->device->host->hostdata;
2534
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302535 if (ql4xdontresethba) {
2536 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
2537 ha->host_no, __func__));
2538 return FAILED;
2539 }
2540
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302541 ql4_printk(KERN_INFO, ha,
Karen Higginsdca05c42009-07-15 15:03:00 -05002542 "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002543 cmd->device->channel, cmd->device->id, cmd->device->lun);
2544
2545 if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
2546 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
2547 "DEAD.\n", ha->host_no, cmd->device->channel,
2548 __func__));
2549
2550 return FAILED;
2551 }
2552
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302553 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
2554 if (is_qla8022(ha))
2555 set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2556 else
2557 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2558 }
Mike Christie50a29ae2008-03-04 13:26:53 -06002559
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302560 if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002561 return_status = SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002562
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302563 ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002564 return_status == FAILED ? "FAILED" : "SUCCEEDED");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002565
2566 return return_status;
2567}
2568
Lalit Chandivade2232be02010-07-30 14:38:47 +05302569/* PCI AER driver recovers from all correctable errors w/o
2570 * driver intervention. For uncorrectable errors PCI AER
2571 * driver calls the following device driver's callbacks
2572 *
2573 * - Fatal Errors - link_reset
2574 * - Non-Fatal Errors - driver's pci_error_detected() which
2575 * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
2576 *
2577 * PCI AER driver calls
2578 * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
2579 * returns RECOVERED or NEED_RESET if fw_hung
2580 * NEED_RESET - driver's slot_reset()
2581 * DISCONNECT - device is dead & cannot recover
2582 * RECOVERED - driver's pci_resume()
2583 */
2584static pci_ers_result_t
2585qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
2586{
2587 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2588
2589 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
2590 ha->host_no, __func__, state);
2591
2592 if (!is_aer_supported(ha))
2593 return PCI_ERS_RESULT_NONE;
2594
2595 switch (state) {
2596 case pci_channel_io_normal:
2597 clear_bit(AF_EEH_BUSY, &ha->flags);
2598 return PCI_ERS_RESULT_CAN_RECOVER;
2599 case pci_channel_io_frozen:
2600 set_bit(AF_EEH_BUSY, &ha->flags);
2601 qla4xxx_mailbox_premature_completion(ha);
2602 qla4xxx_free_irqs(ha);
2603 pci_disable_device(pdev);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002604 /* Return back all IOs */
2605 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
Lalit Chandivade2232be02010-07-30 14:38:47 +05302606 return PCI_ERS_RESULT_NEED_RESET;
2607 case pci_channel_io_perm_failure:
2608 set_bit(AF_EEH_BUSY, &ha->flags);
2609 set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
2610 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
2611 return PCI_ERS_RESULT_DISCONNECT;
2612 }
2613 return PCI_ERS_RESULT_NEED_RESET;
2614}
2615
2616/**
2617 * qla4xxx_pci_mmio_enabled() gets called if
2618 * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
2619 * and read/write to the device still works.
2620 **/
2621static pci_ers_result_t
2622qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
2623{
2624 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2625
2626 if (!is_aer_supported(ha))
2627 return PCI_ERS_RESULT_NONE;
2628
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002629 return PCI_ERS_RESULT_RECOVERED;
Lalit Chandivade2232be02010-07-30 14:38:47 +05302630}
2631
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002632static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
Lalit Chandivade2232be02010-07-30 14:38:47 +05302633{
2634 uint32_t rval = QLA_ERROR;
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002635 uint32_t ret = 0;
Lalit Chandivade2232be02010-07-30 14:38:47 +05302636 int fn;
2637 struct pci_dev *other_pdev = NULL;
2638
2639 ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
2640
2641 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2642
2643 if (test_bit(AF_ONLINE, &ha->flags)) {
2644 clear_bit(AF_ONLINE, &ha->flags);
2645 qla4xxx_mark_all_devices_missing(ha);
2646 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
Lalit Chandivade2232be02010-07-30 14:38:47 +05302647 }
2648
2649 fn = PCI_FUNC(ha->pdev->devfn);
2650 while (fn > 0) {
2651 fn--;
2652 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
2653 "func %x\n", ha->host_no, __func__, fn);
2654 /* Get the pci device given the domain, bus,
2655 * slot/function number */
2656 other_pdev =
2657 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
2658 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
2659 fn));
2660
2661 if (!other_pdev)
2662 continue;
2663
2664 if (atomic_read(&other_pdev->enable_cnt)) {
2665 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
2666 "func in enabled state%x\n", ha->host_no,
2667 __func__, fn);
2668 pci_dev_put(other_pdev);
2669 break;
2670 }
2671 pci_dev_put(other_pdev);
2672 }
2673
2674 /* The first function on the card, the reset owner will
2675 * start & initialize the firmware. The other functions
2676 * on the card will reset the firmware context
2677 */
2678 if (!fn) {
2679 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
2680 "0x%x is the owner\n", ha->host_no, __func__,
2681 ha->pdev->devfn);
2682
2683 qla4_8xxx_idc_lock(ha);
2684 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2685 QLA82XX_DEV_COLD);
2686
2687 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
2688 QLA82XX_IDC_VERSION);
2689
2690 qla4_8xxx_idc_unlock(ha);
2691 clear_bit(AF_FW_RECOVERY, &ha->flags);
2692 rval = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
2693 qla4_8xxx_idc_lock(ha);
2694
2695 if (rval != QLA_SUCCESS) {
2696 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
2697 "FAILED\n", ha->host_no, __func__);
2698 qla4_8xxx_clear_drv_active(ha);
2699 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2700 QLA82XX_DEV_FAILED);
2701 } else {
2702 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
2703 "READY\n", ha->host_no, __func__);
2704 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2705 QLA82XX_DEV_READY);
2706 /* Clear driver state register */
2707 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
2708 qla4_8xxx_set_drv_active(ha);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002709 ret = qla4xxx_request_irqs(ha);
2710 if (ret) {
2711 ql4_printk(KERN_WARNING, ha, "Failed to "
2712 "reserve interrupt %d already in use.\n",
2713 ha->pdev->irq);
2714 rval = QLA_ERROR;
2715 } else {
2716 ha->isp_ops->enable_intrs(ha);
2717 rval = QLA_SUCCESS;
2718 }
Lalit Chandivade2232be02010-07-30 14:38:47 +05302719 }
2720 qla4_8xxx_idc_unlock(ha);
2721 } else {
2722 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
2723 "the reset owner\n", ha->host_no, __func__,
2724 ha->pdev->devfn);
2725 if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
2726 QLA82XX_DEV_READY)) {
2727 clear_bit(AF_FW_RECOVERY, &ha->flags);
2728 rval = qla4xxx_initialize_adapter(ha,
2729 PRESERVE_DDB_LIST);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002730 if (rval == QLA_SUCCESS) {
2731 ret = qla4xxx_request_irqs(ha);
2732 if (ret) {
2733 ql4_printk(KERN_WARNING, ha, "Failed to"
2734 " reserve interrupt %d already in"
2735 " use.\n", ha->pdev->irq);
2736 rval = QLA_ERROR;
2737 } else {
2738 ha->isp_ops->enable_intrs(ha);
2739 rval = QLA_SUCCESS;
2740 }
2741 }
Lalit Chandivade2232be02010-07-30 14:38:47 +05302742 qla4_8xxx_idc_lock(ha);
2743 qla4_8xxx_set_drv_active(ha);
2744 qla4_8xxx_idc_unlock(ha);
2745 }
2746 }
2747 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2748 return rval;
2749}
2750
2751static pci_ers_result_t
2752qla4xxx_pci_slot_reset(struct pci_dev *pdev)
2753{
2754 pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
2755 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2756 int rc;
2757
2758 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
2759 ha->host_no, __func__);
2760
2761 if (!is_aer_supported(ha))
2762 return PCI_ERS_RESULT_NONE;
2763
2764 /* Restore the saved state of PCIe device -
2765 * BAR registers, PCI Config space, PCIX, MSI,
2766 * IOV states
2767 */
2768 pci_restore_state(pdev);
2769
2770 /* pci_restore_state() clears the saved_state flag of the device
2771 * save restored state which resets saved_state flag
2772 */
2773 pci_save_state(pdev);
2774
2775 /* Initialize device or resume if in suspended state */
2776 rc = pci_enable_device(pdev);
2777 if (rc) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002778 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Can't re-enable "
Lalit Chandivade2232be02010-07-30 14:38:47 +05302779 "device after reset\n", ha->host_no, __func__);
2780 goto exit_slot_reset;
2781 }
2782
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07002783 ha->isp_ops->disable_intrs(ha);
Lalit Chandivade2232be02010-07-30 14:38:47 +05302784
2785 if (is_qla8022(ha)) {
2786 if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
2787 ret = PCI_ERS_RESULT_RECOVERED;
2788 goto exit_slot_reset;
2789 } else
2790 goto exit_slot_reset;
2791 }
2792
2793exit_slot_reset:
2794 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
2795 "device after reset\n", ha->host_no, __func__, ret);
2796 return ret;
2797}
2798
2799static void
2800qla4xxx_pci_resume(struct pci_dev *pdev)
2801{
2802 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2803 int ret;
2804
2805 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
2806 ha->host_no, __func__);
2807
2808 ret = qla4xxx_wait_for_hba_online(ha);
2809 if (ret != QLA_SUCCESS) {
2810 ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
2811 "resume I/O from slot/link_reset\n", ha->host_no,
2812 __func__);
2813 }
2814
2815 pci_cleanup_aer_uncorrect_error_status(pdev);
2816 clear_bit(AF_EEH_BUSY, &ha->flags);
2817}
2818
2819static struct pci_error_handlers qla4xxx_err_handler = {
2820 .error_detected = qla4xxx_pci_error_detected,
2821 .mmio_enabled = qla4xxx_pci_mmio_enabled,
2822 .slot_reset = qla4xxx_pci_slot_reset,
2823 .resume = qla4xxx_pci_resume,
2824};
2825
David Somayajuluafaf5a22006-09-19 10:28:00 -07002826static struct pci_device_id qla4xxx_pci_tbl[] = {
2827 {
2828 .vendor = PCI_VENDOR_ID_QLOGIC,
2829 .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
2830 .subvendor = PCI_ANY_ID,
2831 .subdevice = PCI_ANY_ID,
2832 },
2833 {
2834 .vendor = PCI_VENDOR_ID_QLOGIC,
2835 .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
2836 .subvendor = PCI_ANY_ID,
2837 .subdevice = PCI_ANY_ID,
2838 },
David C Somayajulud9150582006-11-15 17:38:40 -08002839 {
2840 .vendor = PCI_VENDOR_ID_QLOGIC,
2841 .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
2842 .subvendor = PCI_ANY_ID,
2843 .subdevice = PCI_ANY_ID,
2844 },
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302845 {
2846 .vendor = PCI_VENDOR_ID_QLOGIC,
2847 .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
2848 .subvendor = PCI_ANY_ID,
2849 .subdevice = PCI_ANY_ID,
2850 },
David Somayajuluafaf5a22006-09-19 10:28:00 -07002851 {0, 0},
2852};
2853MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
2854
Adrian Bunk47975472007-04-26 00:35:16 -07002855static struct pci_driver qla4xxx_pci_driver = {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002856 .name = DRIVER_NAME,
2857 .id_table = qla4xxx_pci_tbl,
2858 .probe = qla4xxx_probe_adapter,
2859 .remove = qla4xxx_remove_adapter,
Lalit Chandivade2232be02010-07-30 14:38:47 +05302860 .err_handler = &qla4xxx_err_handler,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002861};
2862
2863static int __init qla4xxx_module_init(void)
2864{
2865 int ret;
2866
2867 /* Allocate cache for SRBs. */
2868 srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09002869 SLAB_HWCACHE_ALIGN, NULL);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002870 if (srb_cachep == NULL) {
2871 printk(KERN_ERR
2872 "%s: Unable to allocate SRB cache..."
2873 "Failing load!\n", DRIVER_NAME);
2874 ret = -ENOMEM;
2875 goto no_srp_cache;
2876 }
2877
2878 /* Derive version string. */
2879 strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
Andrew Vasquez11010fe2006-10-06 09:54:59 -07002880 if (ql4xextended_error_logging)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002881 strcat(qla4xxx_version_str, "-debug");
2882
2883 qla4xxx_scsi_transport =
2884 iscsi_register_transport(&qla4xxx_iscsi_transport);
2885 if (!qla4xxx_scsi_transport){
2886 ret = -ENODEV;
2887 goto release_srb_cache;
2888 }
2889
David Somayajuluafaf5a22006-09-19 10:28:00 -07002890 ret = pci_register_driver(&qla4xxx_pci_driver);
2891 if (ret)
2892 goto unregister_transport;
2893
2894 printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
2895 return 0;
Doug Maxey5ae16db2006-10-05 23:50:07 -05002896
David Somayajuluafaf5a22006-09-19 10:28:00 -07002897unregister_transport:
2898 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
2899release_srb_cache:
2900 kmem_cache_destroy(srb_cachep);
2901no_srp_cache:
2902 return ret;
2903}
2904
2905static void __exit qla4xxx_module_exit(void)
2906{
David Somayajuluafaf5a22006-09-19 10:28:00 -07002907 pci_unregister_driver(&qla4xxx_pci_driver);
2908 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
2909 kmem_cache_destroy(srb_cachep);
2910}
2911
2912module_init(qla4xxx_module_init);
2913module_exit(qla4xxx_module_exit);
2914
2915MODULE_AUTHOR("QLogic Corporation");
2916MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
2917MODULE_LICENSE("GPL");
2918MODULE_VERSION(QLA4XXX_DRIVER_VERSION);