2 * QLogic iSCSI HBA Driver
3 * Copyright (c) 2003-2010 QLogic Corporation
5 * See LICENSE.qla4xxx for copyright and licensing details.
7 #include <linux/moduleparam.h>
8 #include <linux/slab.h>
10 #include <scsi/scsi_tcq.h>
11 #include <scsi/scsicam.h>
14 #include "ql4_version.h"
17 #include "ql4_inline.h"
22 static char qla4xxx_version_str[40];
25 * SRB allocation cache
27 static struct kmem_cache *srb_cachep;
30 * Module parameter information and variables
32 int ql4xdontresethba = 0;
33 module_param(ql4xdontresethba, int, S_IRUGO | S_IWUSR);
34 MODULE_PARM_DESC(ql4xdontresethba,
35 "Don't reset the HBA for driver recovery \n"
36 " 0 - It will reset HBA (Default)\n"
37 " 1 - It will NOT reset HBA");
39 int ql4xextended_error_logging = 0; /* 0 = off, 1 = log errors */
40 module_param(ql4xextended_error_logging, int, S_IRUGO | S_IWUSR);
41 MODULE_PARM_DESC(ql4xextended_error_logging,
42 "Option to enable extended error logging, "
43 "Default is 0 - no logging, 1 - debug logging");
45 int ql4xenablemsix = 1;
46 module_param(ql4xenablemsix, int, S_IRUGO|S_IWUSR);
47 MODULE_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.");
53 #define QL4_DEF_QDEPTH 32
54 static int ql4xmaxqdepth = QL4_DEF_QDEPTH;
55 module_param(ql4xmaxqdepth, int, S_IRUGO | S_IWUSR);
56 MODULE_PARM_DESC(ql4xmaxqdepth,
57 "Maximum queue depth to report for target devices.\n"
60 static int ql4xsess_recovery_tmo = QL4_SESS_RECOVERY_TMO;
61 module_param(ql4xsess_recovery_tmo, int, S_IRUGO);
62 MODULE_PARM_DESC(ql4xsess_recovery_tmo,
63 "Target Session Recovery Timeout.\n"
67 * SCSI host template entry points
69 static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
72 * iSCSI template entry points
74 static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
75 enum iscsi_tgt_dscvr type, uint32_t enable,
76 struct sockaddr *dst_addr);
77 static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
78 enum iscsi_param param, char *buf);
79 static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
80 enum iscsi_param param, char *buf);
81 static int qla4xxx_host_get_param(struct Scsi_Host *shost,
82 enum iscsi_host_param param, char *buf);
83 static int qla4xxx_iface_set_param(struct Scsi_Host *shost, char *data,
85 static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
86 enum iscsi_param_type param_type,
87 int param, char *buf);
88 static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session);
89 static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
92 * SCSI host template entry points
94 static int qla4xxx_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmd);
95 static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
96 static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
97 static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
98 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
99 static int qla4xxx_slave_alloc(struct scsi_device *device);
100 static int qla4xxx_slave_configure(struct scsi_device *device);
101 static void qla4xxx_slave_destroy(struct scsi_device *sdev);
102 static void qla4xxx_scan_start(struct Scsi_Host *shost);
103 static mode_t ql4_attr_is_visible(int param_type, int param);
105 static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
106 QLA82XX_LEGACY_INTR_CONFIG;
108 static struct scsi_host_template qla4xxx_driver_template = {
109 .module = THIS_MODULE,
111 .proc_name = DRIVER_NAME,
112 .queuecommand = qla4xxx_queuecommand,
114 .eh_abort_handler = qla4xxx_eh_abort,
115 .eh_device_reset_handler = qla4xxx_eh_device_reset,
116 .eh_target_reset_handler = qla4xxx_eh_target_reset,
117 .eh_host_reset_handler = qla4xxx_eh_host_reset,
118 .eh_timed_out = qla4xxx_eh_cmd_timed_out,
120 .slave_configure = qla4xxx_slave_configure,
121 .slave_alloc = qla4xxx_slave_alloc,
122 .slave_destroy = qla4xxx_slave_destroy,
124 .scan_finished = iscsi_scan_finished,
125 .scan_start = qla4xxx_scan_start,
129 .use_clustering = ENABLE_CLUSTERING,
130 .sg_tablesize = SG_ALL,
132 .max_sectors = 0xFFFF,
133 .shost_attrs = qla4xxx_host_attrs,
136 static struct iscsi_transport qla4xxx_iscsi_transport = {
137 .owner = THIS_MODULE,
139 .caps = CAP_FW_DB | CAP_SENDTARGETS_OFFLOAD |
140 CAP_DATA_PATH_OFFLOAD,
141 .tgt_dscvr = qla4xxx_tgt_dscvr,
142 .attr_is_visible = ql4_attr_is_visible,
143 .get_conn_param = qla4xxx_conn_get_param,
144 .get_session_param = qla4xxx_sess_get_param,
145 .get_host_param = qla4xxx_host_get_param,
146 .set_iface_param = qla4xxx_iface_set_param,
147 .session_recovery_timedout = qla4xxx_recovery_timedout,
148 .get_iface_param = qla4xxx_get_iface_param,
151 static struct scsi_transport_template *qla4xxx_scsi_transport;
153 static mode_t ql4_attr_is_visible(int param_type, int param)
155 switch (param_type) {
156 case ISCSI_HOST_PARAM:
158 case ISCSI_HOST_PARAM_HWADDRESS:
159 case ISCSI_HOST_PARAM_IPADDRESS:
160 case ISCSI_HOST_PARAM_INITIATOR_NAME:
167 case ISCSI_PARAM_CONN_ADDRESS:
168 case ISCSI_PARAM_CONN_PORT:
169 case ISCSI_PARAM_TARGET_NAME:
170 case ISCSI_PARAM_TPGT:
171 case ISCSI_PARAM_TARGET_ALIAS:
176 case ISCSI_NET_PARAM:
178 case ISCSI_NET_PARAM_IPV4_ADDR:
179 case ISCSI_NET_PARAM_IPV4_SUBNET:
180 case ISCSI_NET_PARAM_IPV4_GW:
181 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
182 case ISCSI_NET_PARAM_IFACE_ENABLE:
183 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
184 case ISCSI_NET_PARAM_IPV6_ADDR:
185 case ISCSI_NET_PARAM_IPV6_ROUTER:
186 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
187 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
188 case ISCSI_NET_PARAM_VLAN_ID:
189 case ISCSI_NET_PARAM_VLAN_PRIORITY:
190 case ISCSI_NET_PARAM_VLAN_ENABLED:
200 static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
201 enum iscsi_param_type param_type,
202 int param, char *buf)
204 struct Scsi_Host *shost = iscsi_iface_to_shost(iface);
205 struct scsi_qla_host *ha = to_qla_host(shost);
208 if (param_type != ISCSI_NET_PARAM)
212 case ISCSI_NET_PARAM_IPV4_ADDR:
213 len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
215 case ISCSI_NET_PARAM_IPV4_SUBNET:
216 len = sprintf(buf, "%pI4\n", &ha->ip_config.subnet_mask);
218 case ISCSI_NET_PARAM_IPV4_GW:
219 len = sprintf(buf, "%pI4\n", &ha->ip_config.gateway);
221 case ISCSI_NET_PARAM_IFACE_ENABLE:
222 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
223 len = sprintf(buf, "%s\n",
224 (ha->ip_config.ipv4_options &
225 IPOPT_IPV4_PROTOCOL_ENABLE) ?
226 "enabled" : "disabled");
227 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
228 len = sprintf(buf, "%s\n",
229 (ha->ip_config.ipv6_options &
230 IPV6_OPT_IPV6_PROTOCOL_ENABLE) ?
231 "enabled" : "disabled");
233 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
234 len = sprintf(buf, "%s\n",
235 (ha->ip_config.tcp_options & TCPOPT_DHCP_ENABLE) ?
238 case ISCSI_NET_PARAM_IPV6_ADDR:
239 if (iface->iface_num == 0)
240 len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr0);
241 if (iface->iface_num == 1)
242 len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr1);
244 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
245 len = sprintf(buf, "%pI6\n",
246 &ha->ip_config.ipv6_link_local_addr);
248 case ISCSI_NET_PARAM_IPV6_ROUTER:
249 len = sprintf(buf, "%pI6\n",
250 &ha->ip_config.ipv6_default_router_addr);
252 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
253 len = sprintf(buf, "%s\n",
254 (ha->ip_config.ipv6_addl_options &
255 IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) ?
258 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
259 len = sprintf(buf, "%s\n",
260 (ha->ip_config.ipv6_addl_options &
261 IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR) ?
264 case ISCSI_NET_PARAM_VLAN_ID:
265 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
266 len = sprintf(buf, "%d\n",
267 (ha->ip_config.ipv4_vlan_tag &
269 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
270 len = sprintf(buf, "%d\n",
271 (ha->ip_config.ipv6_vlan_tag &
274 case ISCSI_NET_PARAM_VLAN_PRIORITY:
275 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
276 len = sprintf(buf, "%d\n",
277 ((ha->ip_config.ipv4_vlan_tag >> 13) &
278 ISCSI_MAX_VLAN_PRIORITY));
279 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
280 len = sprintf(buf, "%d\n",
281 ((ha->ip_config.ipv6_vlan_tag >> 13) &
282 ISCSI_MAX_VLAN_PRIORITY));
284 case ISCSI_NET_PARAM_VLAN_ENABLED:
285 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
286 len = sprintf(buf, "%s\n",
287 (ha->ip_config.ipv4_options &
288 IPOPT_VLAN_TAGGING_ENABLE) ?
289 "enabled" : "disabled");
290 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
291 len = sprintf(buf, "%s\n",
292 (ha->ip_config.ipv6_options &
293 IPV6_OPT_VLAN_TAGGING_ENABLE) ?
294 "enabled" : "disabled");
303 static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
305 struct iscsi_cls_session *session;
306 struct ddb_entry *ddb_entry;
308 session = starget_to_session(scsi_target(sc->device));
309 ddb_entry = session->dd_data;
311 /* if we are not logged in then the LLD is going to clean up the cmd */
312 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
313 return BLK_EH_RESET_TIMER;
315 return BLK_EH_NOT_HANDLED;
318 static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session)
320 struct ddb_entry *ddb_entry = session->dd_data;
321 struct scsi_qla_host *ha = ddb_entry->ha;
323 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
324 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
326 DEBUG2(printk("scsi%ld: %s: ddb [%d] session recovery timeout "
327 "of (%d) secs exhausted, marking device DEAD.\n",
328 ha->host_no, __func__, ddb_entry->fw_ddb_index,
329 ddb_entry->sess->recovery_tmo));
333 static int qla4xxx_host_get_param(struct Scsi_Host *shost,
334 enum iscsi_host_param param, char *buf)
336 struct scsi_qla_host *ha = to_qla_host(shost);
340 case ISCSI_HOST_PARAM_HWADDRESS:
341 len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
343 case ISCSI_HOST_PARAM_IPADDRESS:
344 len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
346 case ISCSI_HOST_PARAM_INITIATOR_NAME:
347 len = sprintf(buf, "%s\n", ha->name_string);
356 static void qla4xxx_create_ipv4_iface(struct scsi_qla_host *ha)
362 ha->iface_ipv4 = iscsi_create_iface(ha->host,
363 &qla4xxx_iscsi_transport,
364 ISCSI_IFACE_TYPE_IPV4, 0, 0);
366 ql4_printk(KERN_ERR, ha, "Could not create IPv4 iSCSI "
370 static void qla4xxx_create_ipv6_iface(struct scsi_qla_host *ha)
372 if (!ha->iface_ipv6_0)
374 ha->iface_ipv6_0 = iscsi_create_iface(ha->host,
375 &qla4xxx_iscsi_transport,
376 ISCSI_IFACE_TYPE_IPV6, 0,
378 if (!ha->iface_ipv6_0)
379 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
382 if (!ha->iface_ipv6_1)
384 ha->iface_ipv6_1 = iscsi_create_iface(ha->host,
385 &qla4xxx_iscsi_transport,
386 ISCSI_IFACE_TYPE_IPV6, 1,
388 if (!ha->iface_ipv6_1)
389 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
393 static void qla4xxx_create_ifaces(struct scsi_qla_host *ha)
395 if (ha->ip_config.ipv4_options & IPOPT_IPV4_PROTOCOL_ENABLE)
396 qla4xxx_create_ipv4_iface(ha);
398 if (ha->ip_config.ipv6_options & IPV6_OPT_IPV6_PROTOCOL_ENABLE)
399 qla4xxx_create_ipv6_iface(ha);
402 static void qla4xxx_destroy_ipv4_iface(struct scsi_qla_host *ha)
404 if (ha->iface_ipv4) {
405 iscsi_destroy_iface(ha->iface_ipv4);
406 ha->iface_ipv4 = NULL;
410 static void qla4xxx_destroy_ipv6_iface(struct scsi_qla_host *ha)
412 if (ha->iface_ipv6_0) {
413 iscsi_destroy_iface(ha->iface_ipv6_0);
414 ha->iface_ipv6_0 = NULL;
416 if (ha->iface_ipv6_1) {
417 iscsi_destroy_iface(ha->iface_ipv6_1);
418 ha->iface_ipv6_1 = NULL;
422 static void qla4xxx_destroy_ifaces(struct scsi_qla_host *ha)
424 qla4xxx_destroy_ipv4_iface(ha);
425 qla4xxx_destroy_ipv6_iface(ha);
428 static void qla4xxx_set_ipv6(struct scsi_qla_host *ha,
429 struct iscsi_iface_param_info *iface_param,
430 struct addr_ctrl_blk *init_fw_cb)
433 * iface_num 0 is valid for IPv6 Addr, linklocal, router, autocfg.
434 * iface_num 1 is valid only for IPv6 Addr.
436 switch (iface_param->param) {
437 case ISCSI_NET_PARAM_IPV6_ADDR:
438 if (iface_param->iface_num & 0x1)
440 memcpy(init_fw_cb->ipv6_addr1, iface_param->value,
441 sizeof(init_fw_cb->ipv6_addr1));
444 memcpy(init_fw_cb->ipv6_addr0, iface_param->value,
445 sizeof(init_fw_cb->ipv6_addr0));
447 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
448 if (iface_param->iface_num & 0x1)
450 memcpy(init_fw_cb->ipv6_if_id, &iface_param->value[8],
451 sizeof(init_fw_cb->ipv6_if_id));
453 case ISCSI_NET_PARAM_IPV6_ROUTER:
454 if (iface_param->iface_num & 0x1)
456 memcpy(init_fw_cb->ipv6_dflt_rtr_addr, iface_param->value,
457 sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
459 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
460 /* Autocfg applies to even interface */
461 if (iface_param->iface_num & 0x1)
464 if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_DISABLE)
465 init_fw_cb->ipv6_addtl_opts &=
467 ~IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
468 else if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_ND_ENABLE)
469 init_fw_cb->ipv6_addtl_opts |=
471 IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
473 ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
476 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
477 /* Autocfg applies to even interface */
478 if (iface_param->iface_num & 0x1)
481 if (iface_param->value[0] ==
482 ISCSI_IPV6_LINKLOCAL_AUTOCFG_ENABLE)
483 init_fw_cb->ipv6_addtl_opts |= cpu_to_le16(
484 IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
485 else if (iface_param->value[0] ==
486 ISCSI_IPV6_LINKLOCAL_AUTOCFG_DISABLE)
487 init_fw_cb->ipv6_addtl_opts &= cpu_to_le16(
488 ~IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
490 ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
491 "IPv6 linklocal addr\n");
493 case ISCSI_NET_PARAM_IPV6_ROUTER_AUTOCFG:
494 /* Autocfg applies to even interface */
495 if (iface_param->iface_num & 0x1)
498 if (iface_param->value[0] == ISCSI_IPV6_ROUTER_AUTOCFG_ENABLE)
499 memset(init_fw_cb->ipv6_dflt_rtr_addr, 0,
500 sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
502 case ISCSI_NET_PARAM_IFACE_ENABLE:
503 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
504 init_fw_cb->ipv6_opts |=
505 cpu_to_le16(IPV6_OPT_IPV6_PROTOCOL_ENABLE);
506 qla4xxx_create_ipv6_iface(ha);
508 init_fw_cb->ipv6_opts &=
509 cpu_to_le16(~IPV6_OPT_IPV6_PROTOCOL_ENABLE &
511 qla4xxx_destroy_ipv6_iface(ha);
514 case ISCSI_NET_PARAM_VLAN_ID:
515 if (iface_param->len != sizeof(init_fw_cb->ipv6_vlan_tag))
517 init_fw_cb->ipv6_vlan_tag =
518 cpu_to_be16(*(uint16_t *)iface_param->value);
520 case ISCSI_NET_PARAM_VLAN_ENABLED:
521 if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
522 init_fw_cb->ipv6_opts |=
523 cpu_to_le16(IPV6_OPT_VLAN_TAGGING_ENABLE);
525 init_fw_cb->ipv6_opts &=
526 cpu_to_le16(~IPV6_OPT_VLAN_TAGGING_ENABLE);
529 ql4_printk(KERN_ERR, ha, "Unknown IPv6 param = %d\n",
535 static void qla4xxx_set_ipv4(struct scsi_qla_host *ha,
536 struct iscsi_iface_param_info *iface_param,
537 struct addr_ctrl_blk *init_fw_cb)
539 switch (iface_param->param) {
540 case ISCSI_NET_PARAM_IPV4_ADDR:
541 memcpy(init_fw_cb->ipv4_addr, iface_param->value,
542 sizeof(init_fw_cb->ipv4_addr));
544 case ISCSI_NET_PARAM_IPV4_SUBNET:
545 memcpy(init_fw_cb->ipv4_subnet, iface_param->value,
546 sizeof(init_fw_cb->ipv4_subnet));
548 case ISCSI_NET_PARAM_IPV4_GW:
549 memcpy(init_fw_cb->ipv4_gw_addr, iface_param->value,
550 sizeof(init_fw_cb->ipv4_gw_addr));
552 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
553 if (iface_param->value[0] == ISCSI_BOOTPROTO_DHCP)
554 init_fw_cb->ipv4_tcp_opts |=
555 cpu_to_le16(TCPOPT_DHCP_ENABLE);
556 else if (iface_param->value[0] == ISCSI_BOOTPROTO_STATIC)
557 init_fw_cb->ipv4_tcp_opts &=
558 cpu_to_le16(~TCPOPT_DHCP_ENABLE);
560 ql4_printk(KERN_ERR, ha, "Invalid IPv4 bootproto\n");
562 case ISCSI_NET_PARAM_IFACE_ENABLE:
563 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
564 init_fw_cb->ipv4_ip_opts |=
565 cpu_to_le16(IPOPT_IPV4_PROTOCOL_ENABLE);
566 qla4xxx_create_ipv4_iface(ha);
568 init_fw_cb->ipv4_ip_opts &=
569 cpu_to_le16(~IPOPT_IPV4_PROTOCOL_ENABLE &
571 qla4xxx_destroy_ipv4_iface(ha);
574 case ISCSI_NET_PARAM_VLAN_ID:
575 if (iface_param->len != sizeof(init_fw_cb->ipv4_vlan_tag))
577 init_fw_cb->ipv4_vlan_tag =
578 cpu_to_be16(*(uint16_t *)iface_param->value);
580 case ISCSI_NET_PARAM_VLAN_ENABLED:
581 if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
582 init_fw_cb->ipv4_ip_opts |=
583 cpu_to_le16(IPOPT_VLAN_TAGGING_ENABLE);
585 init_fw_cb->ipv4_ip_opts &=
586 cpu_to_le16(~IPOPT_VLAN_TAGGING_ENABLE);
589 ql4_printk(KERN_ERR, ha, "Unknown IPv4 param = %d\n",
596 qla4xxx_initcb_to_acb(struct addr_ctrl_blk *init_fw_cb)
598 struct addr_ctrl_blk_def *acb;
599 acb = (struct addr_ctrl_blk_def *)init_fw_cb;
600 memset(acb->reserved1, 0, sizeof(acb->reserved1));
601 memset(acb->reserved2, 0, sizeof(acb->reserved2));
602 memset(acb->reserved3, 0, sizeof(acb->reserved3));
603 memset(acb->reserved4, 0, sizeof(acb->reserved4));
604 memset(acb->reserved5, 0, sizeof(acb->reserved5));
605 memset(acb->reserved6, 0, sizeof(acb->reserved6));
606 memset(acb->reserved7, 0, sizeof(acb->reserved7));
607 memset(acb->reserved8, 0, sizeof(acb->reserved8));
608 memset(acb->reserved9, 0, sizeof(acb->reserved9));
609 memset(acb->reserved10, 0, sizeof(acb->reserved10));
610 memset(acb->reserved11, 0, sizeof(acb->reserved11));
611 memset(acb->reserved12, 0, sizeof(acb->reserved12));
612 memset(acb->reserved13, 0, sizeof(acb->reserved13));
613 memset(acb->reserved14, 0, sizeof(acb->reserved14));
614 memset(acb->reserved15, 0, sizeof(acb->reserved15));
618 qla4xxx_iface_set_param(struct Scsi_Host *shost, char *data, int count)
620 struct scsi_qla_host *ha = to_qla_host(shost);
622 struct iscsi_iface_param_info *iface_param = NULL;
623 struct addr_ctrl_blk *init_fw_cb = NULL;
624 dma_addr_t init_fw_cb_dma;
625 uint32_t mbox_cmd[MBOX_REG_COUNT];
626 uint32_t mbox_sts[MBOX_REG_COUNT];
627 uint32_t total_param_count;
630 init_fw_cb = dma_alloc_coherent(&ha->pdev->dev,
631 sizeof(struct addr_ctrl_blk),
632 &init_fw_cb_dma, GFP_KERNEL);
634 ql4_printk(KERN_ERR, ha, "%s: Unable to alloc init_cb\n",
639 memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
640 memset(&mbox_cmd, 0, sizeof(mbox_cmd));
641 memset(&mbox_sts, 0, sizeof(mbox_sts));
643 if (qla4xxx_get_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma)) {
644 ql4_printk(KERN_ERR, ha, "%s: get ifcb failed\n", __func__);
646 goto exit_init_fw_cb;
649 total_param_count = count;
650 iface_param = (struct iscsi_iface_param_info *)data;
652 for ( ; total_param_count != 0; total_param_count--) {
653 length = iface_param->len;
655 if (iface_param->param_type != ISCSI_NET_PARAM)
658 switch (iface_param->iface_type) {
659 case ISCSI_IFACE_TYPE_IPV4:
660 switch (iface_param->iface_num) {
662 qla4xxx_set_ipv4(ha, iface_param, init_fw_cb);
665 /* Cannot have more than one IPv4 interface */
666 ql4_printk(KERN_ERR, ha, "Invalid IPv4 iface "
668 iface_param->iface_num);
672 case ISCSI_IFACE_TYPE_IPV6:
673 switch (iface_param->iface_num) {
676 qla4xxx_set_ipv6(ha, iface_param, init_fw_cb);
679 /* Cannot have more than two IPv6 interface */
680 ql4_printk(KERN_ERR, ha, "Invalid IPv6 iface "
682 iface_param->iface_num);
687 ql4_printk(KERN_ERR, ha, "Invalid iface type\n");
691 iface_param = (struct iscsi_iface_param_info *)
692 ((uint8_t *)iface_param +
693 sizeof(struct iscsi_iface_param_info) + length);
696 init_fw_cb->cookie = cpu_to_le32(0x11BEAD5A);
698 rval = qla4xxx_set_flash(ha, init_fw_cb_dma, FLASH_SEGMENT_IFCB,
699 sizeof(struct addr_ctrl_blk),
700 FLASH_OPT_RMW_COMMIT);
701 if (rval != QLA_SUCCESS) {
702 ql4_printk(KERN_ERR, ha, "%s: set flash mbx failed\n",
705 goto exit_init_fw_cb;
708 qla4xxx_disable_acb(ha);
710 qla4xxx_initcb_to_acb(init_fw_cb);
712 rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma);
713 if (rval != QLA_SUCCESS) {
714 ql4_printk(KERN_ERR, ha, "%s: set acb mbx failed\n",
717 goto exit_init_fw_cb;
720 memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
721 qla4xxx_update_local_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb,
725 dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk),
726 init_fw_cb, init_fw_cb_dma);
731 static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
732 enum iscsi_param param, char *buf)
734 struct ddb_entry *ddb_entry = sess->dd_data;
738 case ISCSI_PARAM_TARGET_NAME:
739 len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
740 ddb_entry->iscsi_name);
742 case ISCSI_PARAM_TPGT:
743 len = sprintf(buf, "%u\n", ddb_entry->tpgt);
745 case ISCSI_PARAM_TARGET_ALIAS:
746 len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
747 ddb_entry->iscsi_alias);
756 static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
757 enum iscsi_param param, char *buf)
759 struct iscsi_cls_session *session;
760 struct ddb_entry *ddb_entry;
763 session = iscsi_dev_to_session(conn->dev.parent);
764 ddb_entry = session->dd_data;
767 case ISCSI_PARAM_CONN_PORT:
768 len = sprintf(buf, "%hu\n", ddb_entry->port);
770 case ISCSI_PARAM_CONN_ADDRESS:
771 /* TODO: what are the ipv6 bits */
772 len = sprintf(buf, "%pI4\n", &ddb_entry->ip_addr);
781 static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
782 enum iscsi_tgt_dscvr type, uint32_t enable,
783 struct sockaddr *dst_addr)
785 struct scsi_qla_host *ha;
786 struct sockaddr_in *addr;
787 struct sockaddr_in6 *addr6;
790 ha = (struct scsi_qla_host *) shost->hostdata;
793 case ISCSI_TGT_DSCVR_SEND_TARGETS:
794 if (dst_addr->sa_family == AF_INET) {
795 addr = (struct sockaddr_in *)dst_addr;
796 if (qla4xxx_send_tgts(ha, (char *)&addr->sin_addr,
797 addr->sin_port) != QLA_SUCCESS)
799 } else if (dst_addr->sa_family == AF_INET6) {
801 * TODO: fix qla4xxx_send_tgts
803 addr6 = (struct sockaddr_in6 *)dst_addr;
804 if (qla4xxx_send_tgts(ha, (char *)&addr6->sin6_addr,
805 addr6->sin6_port) != QLA_SUCCESS)
816 void qla4xxx_destroy_sess(struct ddb_entry *ddb_entry)
818 if (!ddb_entry->sess)
821 if (ddb_entry->conn) {
822 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
823 iscsi_remove_session(ddb_entry->sess);
825 iscsi_free_session(ddb_entry->sess);
828 int qla4xxx_add_sess(struct ddb_entry *ddb_entry)
832 ddb_entry->sess->recovery_tmo = ql4xsess_recovery_tmo;
834 err = iscsi_add_session(ddb_entry->sess, ddb_entry->fw_ddb_index);
836 DEBUG2(printk(KERN_ERR "Could not add session.\n"));
840 ddb_entry->conn = iscsi_create_conn(ddb_entry->sess, 0, 0);
841 if (!ddb_entry->conn) {
842 iscsi_remove_session(ddb_entry->sess);
843 DEBUG2(printk(KERN_ERR "Could not add connection.\n"));
847 /* finally ready to go */
848 iscsi_unblock_session(ddb_entry->sess);
852 struct ddb_entry *qla4xxx_alloc_sess(struct scsi_qla_host *ha)
854 struct ddb_entry *ddb_entry;
855 struct iscsi_cls_session *sess;
857 sess = iscsi_alloc_session(ha->host, &qla4xxx_iscsi_transport,
858 sizeof(struct ddb_entry));
862 ddb_entry = sess->dd_data;
863 memset(ddb_entry, 0, sizeof(*ddb_entry));
865 ddb_entry->sess = sess;
869 static void qla4xxx_scan_start(struct Scsi_Host *shost)
871 struct scsi_qla_host *ha = shost_priv(shost);
872 struct ddb_entry *ddb_entry, *ddbtemp;
874 /* finish setup of sessions that were already setup in firmware */
875 list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
876 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE)
877 qla4xxx_add_sess(ddb_entry);
885 static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
886 unsigned long interval)
888 DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
889 __func__, ha->host->host_no));
890 init_timer(&ha->timer);
891 ha->timer.expires = jiffies + interval * HZ;
892 ha->timer.data = (unsigned long)ha;
893 ha->timer.function = (void (*)(unsigned long))func;
894 add_timer(&ha->timer);
895 ha->timer_active = 1;
898 static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
900 del_timer_sync(&ha->timer);
901 ha->timer_active = 0;
905 * qla4xxx_mark_device_missing - mark a device as missing.
906 * @ha: Pointer to host adapter structure.
907 * @ddb_entry: Pointer to device database entry
909 * This routine marks a device missing and close connection.
911 void qla4xxx_mark_device_missing(struct scsi_qla_host *ha,
912 struct ddb_entry *ddb_entry)
914 if ((atomic_read(&ddb_entry->state) != DDB_STATE_DEAD)) {
915 atomic_set(&ddb_entry->state, DDB_STATE_MISSING);
916 DEBUG2(printk("scsi%ld: ddb [%d] marked MISSING\n",
917 ha->host_no, ddb_entry->fw_ddb_index));
919 DEBUG2(printk("scsi%ld: ddb [%d] DEAD\n", ha->host_no,
920 ddb_entry->fw_ddb_index))
922 iscsi_block_session(ddb_entry->sess);
923 iscsi_conn_error_event(ddb_entry->conn, ISCSI_ERR_CONN_FAILED);
927 * qla4xxx_mark_all_devices_missing - mark all devices as missing.
928 * @ha: Pointer to host adapter structure.
930 * This routine marks a device missing and resets the relogin retry count.
932 void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
934 struct ddb_entry *ddb_entry, *ddbtemp;
935 list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
936 qla4xxx_mark_device_missing(ha, ddb_entry);
940 static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
941 struct ddb_entry *ddb_entry,
942 struct scsi_cmnd *cmd)
946 srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
950 kref_init(&srb->srb_ref);
952 srb->ddb = ddb_entry;
955 CMD_SP(cmd) = (void *)srb;
960 static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
962 struct scsi_cmnd *cmd = srb->cmd;
964 if (srb->flags & SRB_DMA_VALID) {
966 srb->flags &= ~SRB_DMA_VALID;
971 void qla4xxx_srb_compl(struct kref *ref)
973 struct srb *srb = container_of(ref, struct srb, srb_ref);
974 struct scsi_cmnd *cmd = srb->cmd;
975 struct scsi_qla_host *ha = srb->ha;
977 qla4xxx_srb_free_dma(ha, srb);
979 mempool_free(srb, ha->srb_mempool);
985 * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
987 * @cmd: Pointer to Linux's SCSI command structure
990 * This routine is invoked by Linux to send a SCSI command to the driver.
991 * The mid-level driver tries to ensure that queuecommand never gets
992 * invoked concurrently with itself or the interrupt handler (although
993 * the interrupt handler may call this routine as part of request-
994 * completion handling). Unfortunely, it sometimes calls the scheduler
995 * in interrupt context which is a big NO! NO!.
997 static int qla4xxx_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
999 struct scsi_qla_host *ha = to_qla_host(host);
1000 struct ddb_entry *ddb_entry = cmd->device->hostdata;
1001 struct iscsi_cls_session *sess = ddb_entry->sess;
1005 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1006 if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
1007 cmd->result = DID_NO_CONNECT << 16;
1009 cmd->result = DID_REQUEUE << 16;
1010 goto qc_fail_command;
1014 cmd->result = DID_IMM_RETRY << 16;
1015 goto qc_fail_command;
1018 rval = iscsi_session_chkready(sess);
1021 goto qc_fail_command;
1024 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
1025 if (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD) {
1026 cmd->result = DID_NO_CONNECT << 16;
1027 goto qc_fail_command;
1029 return SCSI_MLQUEUE_TARGET_BUSY;
1032 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1033 test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
1034 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1035 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
1036 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
1037 !test_bit(AF_ONLINE, &ha->flags) ||
1038 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
1041 srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd);
1045 rval = qla4xxx_send_command_to_isp(ha, srb);
1046 if (rval != QLA_SUCCESS)
1047 goto qc_host_busy_free_sp;
1051 qc_host_busy_free_sp:
1052 qla4xxx_srb_free_dma(ha, srb);
1053 mempool_free(srb, ha->srb_mempool);
1056 return SCSI_MLQUEUE_HOST_BUSY;
1059 cmd->scsi_done(cmd);
1065 * qla4xxx_mem_free - frees memory allocated to adapter
1066 * @ha: Pointer to host adapter structure.
1068 * Frees memory previously allocated by qla4xxx_mem_alloc
1070 static void qla4xxx_mem_free(struct scsi_qla_host *ha)
1073 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
1079 ha->request_ring = NULL;
1080 ha->request_dma = 0;
1081 ha->response_ring = NULL;
1082 ha->response_dma = 0;
1083 ha->shadow_regs = NULL;
1084 ha->shadow_regs_dma = 0;
1086 /* Free srb pool. */
1087 if (ha->srb_mempool)
1088 mempool_destroy(ha->srb_mempool);
1090 ha->srb_mempool = NULL;
1092 /* release io space registers */
1093 if (is_qla8022(ha)) {
1096 (struct device_reg_82xx __iomem *)ha->nx_pcibase);
1099 pci_release_regions(ha->pdev);
1103 * qla4xxx_mem_alloc - allocates memory for use by adapter.
1104 * @ha: Pointer to host adapter structure
1106 * Allocates DMA memory for request and response queues. Also allocates memory
1109 static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
1111 unsigned long align;
1113 /* Allocate contiguous block of DMA memory for queues. */
1114 ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
1115 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
1116 sizeof(struct shadow_regs) +
1118 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
1119 ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
1120 &ha->queues_dma, GFP_KERNEL);
1121 if (ha->queues == NULL) {
1122 ql4_printk(KERN_WARNING, ha,
1123 "Memory Allocation failed - queues.\n");
1125 goto mem_alloc_error_exit;
1127 memset(ha->queues, 0, ha->queues_len);
1130 * As per RISC alignment requirements -- the bus-address must be a
1131 * multiple of the request-ring size (in bytes).
1134 if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
1135 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
1136 (MEM_ALIGN_VALUE - 1));
1138 /* Update request and response queue pointers. */
1139 ha->request_dma = ha->queues_dma + align;
1140 ha->request_ring = (struct queue_entry *) (ha->queues + align);
1141 ha->response_dma = ha->queues_dma + align +
1142 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
1143 ha->response_ring = (struct queue_entry *) (ha->queues + align +
1144 (REQUEST_QUEUE_DEPTH *
1146 ha->shadow_regs_dma = ha->queues_dma + align +
1147 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
1148 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
1149 ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
1150 (REQUEST_QUEUE_DEPTH *
1152 (RESPONSE_QUEUE_DEPTH *
1155 /* Allocate memory for srb pool. */
1156 ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
1157 mempool_free_slab, srb_cachep);
1158 if (ha->srb_mempool == NULL) {
1159 ql4_printk(KERN_WARNING, ha,
1160 "Memory Allocation failed - SRB Pool.\n");
1162 goto mem_alloc_error_exit;
1167 mem_alloc_error_exit:
1168 qla4xxx_mem_free(ha);
1173 * qla4_8xxx_check_fw_alive - Check firmware health
1174 * @ha: Pointer to host adapter structure.
1176 * Context: Interrupt
1178 static void qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
1180 uint32_t fw_heartbeat_counter, halt_status;
1182 fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
1183 /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
1184 if (fw_heartbeat_counter == 0xffffffff) {
1185 DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
1186 "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
1187 ha->host_no, __func__));
1191 if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
1192 ha->seconds_since_last_heartbeat++;
1193 /* FW not alive after 2 seconds */
1194 if (ha->seconds_since_last_heartbeat == 2) {
1195 ha->seconds_since_last_heartbeat = 0;
1196 halt_status = qla4_8xxx_rd_32(ha,
1197 QLA82XX_PEG_HALT_STATUS1);
1199 ql4_printk(KERN_INFO, ha,
1200 "scsi(%ld): %s, Dumping hw/fw registers:\n "
1201 " PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2:"
1202 " 0x%x,\n PEG_NET_0_PC: 0x%x, PEG_NET_1_PC:"
1203 " 0x%x,\n PEG_NET_2_PC: 0x%x, PEG_NET_3_PC:"
1204 " 0x%x,\n PEG_NET_4_PC: 0x%x\n",
1205 ha->host_no, __func__, halt_status,
1207 QLA82XX_PEG_HALT_STATUS2),
1208 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_0 +
1210 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_1 +
1212 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_2 +
1214 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_3 +
1216 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_4 +
1219 /* Since we cannot change dev_state in interrupt
1220 * context, set appropriate DPC flag then wakeup
1222 if (halt_status & HALT_STATUS_UNRECOVERABLE)
1223 set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
1225 printk("scsi%ld: %s: detect abort needed!\n",
1226 ha->host_no, __func__);
1227 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1229 qla4xxx_wake_dpc(ha);
1230 qla4xxx_mailbox_premature_completion(ha);
1233 ha->seconds_since_last_heartbeat = 0;
1235 ha->fw_heartbeat_counter = fw_heartbeat_counter;
1239 * qla4_8xxx_watchdog - Poll dev state
1240 * @ha: Pointer to host adapter structure.
1242 * Context: Interrupt
1244 void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
1248 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
1250 /* don't poll if reset is going on */
1251 if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
1252 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1253 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags))) {
1254 if (dev_state == QLA82XX_DEV_NEED_RESET &&
1255 !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
1256 if (!ql4xdontresethba) {
1257 ql4_printk(KERN_INFO, ha, "%s: HW State: "
1258 "NEED RESET!\n", __func__);
1259 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1260 qla4xxx_wake_dpc(ha);
1261 qla4xxx_mailbox_premature_completion(ha);
1263 } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
1264 !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
1265 ql4_printk(KERN_INFO, ha, "%s: HW State: NEED QUIES!\n",
1267 set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
1268 qla4xxx_wake_dpc(ha);
1270 /* Check firmware health */
1271 qla4_8xxx_check_fw_alive(ha);
1277 * qla4xxx_timer - checks every second for work to do.
1278 * @ha: Pointer to host adapter structure.
1280 static void qla4xxx_timer(struct scsi_qla_host *ha)
1282 struct ddb_entry *ddb_entry, *dtemp;
1286 /* If we are in the middle of AER/EEH processing
1287 * skip any processing and reschedule the timer
1289 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1290 mod_timer(&ha->timer, jiffies + HZ);
1294 /* Hardware read to trigger an EEH error during mailbox waits. */
1295 if (!pci_channel_offline(ha->pdev))
1296 pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
1298 if (is_qla8022(ha)) {
1299 qla4_8xxx_watchdog(ha);
1302 /* Search for relogin's to time-out and port down retry. */
1303 list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
1304 /* Count down time between sending relogins */
1305 if (adapter_up(ha) &&
1306 !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
1307 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
1308 if (atomic_read(&ddb_entry->retry_relogin_timer) !=
1310 if (atomic_read(&ddb_entry->retry_relogin_timer)
1312 atomic_set(&ddb_entry->
1313 retry_relogin_timer,
1315 set_bit(DPC_RELOGIN_DEVICE,
1317 set_bit(DF_RELOGIN, &ddb_entry->flags);
1318 DEBUG2(printk("scsi%ld: %s: ddb [%d]"
1320 ha->host_no, __func__,
1321 ddb_entry->fw_ddb_index));
1323 atomic_dec(&ddb_entry->
1324 retry_relogin_timer);
1328 /* Wait for relogin to timeout */
1329 if (atomic_read(&ddb_entry->relogin_timer) &&
1330 (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
1332 * If the relogin times out and the device is
1333 * still NOT ONLINE then try and relogin again.
1335 if (atomic_read(&ddb_entry->state) !=
1337 ddb_entry->fw_ddb_device_state ==
1338 DDB_DS_SESSION_FAILED) {
1339 /* Reset retry relogin timer */
1340 atomic_inc(&ddb_entry->relogin_retry_count);
1341 DEBUG2(printk("scsi%ld: ddb [%d] relogin"
1342 " timed out-retrying"
1345 ddb_entry->fw_ddb_index,
1346 atomic_read(&ddb_entry->
1347 relogin_retry_count))
1350 DEBUG(printk("scsi%ld:%d:%d: ddb [%d] "
1351 "initiate relogin after"
1353 ha->host_no, ddb_entry->bus,
1355 ddb_entry->fw_ddb_index,
1356 ddb_entry->default_time2wait + 4)
1359 atomic_set(&ddb_entry->retry_relogin_timer,
1360 ddb_entry->default_time2wait + 4);
1365 if (!is_qla8022(ha)) {
1366 /* Check for heartbeat interval. */
1367 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
1368 ha->heartbeat_interval != 0) {
1369 ha->seconds_since_last_heartbeat++;
1370 if (ha->seconds_since_last_heartbeat >
1371 ha->heartbeat_interval + 2)
1372 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1376 /* Wakeup the dpc routine for this adapter, if needed. */
1378 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1379 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
1380 test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
1381 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
1382 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1383 test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
1384 test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
1385 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
1386 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
1387 test_bit(DPC_AEN, &ha->dpc_flags)) {
1388 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
1389 " - dpc flags = 0x%lx\n",
1390 ha->host_no, __func__, ha->dpc_flags));
1391 qla4xxx_wake_dpc(ha);
1394 /* Reschedule timer thread to call us back in one second */
1395 mod_timer(&ha->timer, jiffies + HZ);
1397 DEBUG2(ha->seconds_since_last_intr++);
1401 * qla4xxx_cmd_wait - waits for all outstanding commands to complete
1402 * @ha: Pointer to host adapter structure.
1404 * This routine stalls the driver until all outstanding commands are returned.
1405 * Caller must release the Hardware Lock prior to calling this routine.
1407 static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
1410 unsigned long flags;
1411 struct scsi_cmnd *cmd;
1413 unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
1415 DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
1416 "complete\n", WAIT_CMD_TOV));
1418 while (!time_after_eq(jiffies, wtime)) {
1419 spin_lock_irqsave(&ha->hardware_lock, flags);
1420 /* Find a command that hasn't completed. */
1421 for (index = 0; index < ha->host->can_queue; index++) {
1422 cmd = scsi_host_find_tag(ha->host, index);
1424 * We cannot just check if the index is valid,
1425 * becase if we are run from the scsi eh, then
1426 * the scsi/block layer is going to prevent
1427 * the tag from being released.
1429 if (cmd != NULL && CMD_SP(cmd))
1432 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1434 /* If No Commands are pending, wait is complete */
1435 if (index == ha->host->can_queue)
1440 /* If we timed out on waiting for commands to come back
1445 int qla4xxx_hw_reset(struct scsi_qla_host *ha)
1447 uint32_t ctrl_status;
1448 unsigned long flags = 0;
1450 DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
1452 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
1455 spin_lock_irqsave(&ha->hardware_lock, flags);
1458 * If the SCSI Reset Interrupt bit is set, clear it.
1459 * Otherwise, the Soft Reset won't work.
1461 ctrl_status = readw(&ha->reg->ctrl_status);
1462 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
1463 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
1465 /* Issue Soft Reset */
1466 writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
1467 readl(&ha->reg->ctrl_status);
1469 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1474 * qla4xxx_soft_reset - performs soft reset.
1475 * @ha: Pointer to host adapter structure.
1477 int qla4xxx_soft_reset(struct scsi_qla_host *ha)
1479 uint32_t max_wait_time;
1480 unsigned long flags = 0;
1482 uint32_t ctrl_status;
1484 status = qla4xxx_hw_reset(ha);
1485 if (status != QLA_SUCCESS)
1489 /* Wait until the Network Reset Intr bit is cleared */
1490 max_wait_time = RESET_INTR_TOV;
1492 spin_lock_irqsave(&ha->hardware_lock, flags);
1493 ctrl_status = readw(&ha->reg->ctrl_status);
1494 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1496 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
1500 } while ((--max_wait_time));
1502 if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
1503 DEBUG2(printk(KERN_WARNING
1504 "scsi%ld: Network Reset Intr not cleared by "
1505 "Network function, clearing it now!\n",
1507 spin_lock_irqsave(&ha->hardware_lock, flags);
1508 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
1509 readl(&ha->reg->ctrl_status);
1510 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1513 /* Wait until the firmware tells us the Soft Reset is done */
1514 max_wait_time = SOFT_RESET_TOV;
1516 spin_lock_irqsave(&ha->hardware_lock, flags);
1517 ctrl_status = readw(&ha->reg->ctrl_status);
1518 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1520 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
1521 status = QLA_SUCCESS;
1526 } while ((--max_wait_time));
1529 * Also, make sure that the SCSI Reset Interrupt bit has been cleared
1530 * after the soft reset has taken place.
1532 spin_lock_irqsave(&ha->hardware_lock, flags);
1533 ctrl_status = readw(&ha->reg->ctrl_status);
1534 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
1535 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
1536 readl(&ha->reg->ctrl_status);
1538 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1540 /* If soft reset fails then most probably the bios on other
1541 * function is also enabled.
1542 * Since the initialization is sequential the other fn
1543 * wont be able to acknowledge the soft reset.
1544 * Issue a force soft reset to workaround this scenario.
1546 if (max_wait_time == 0) {
1547 /* Issue Force Soft Reset */
1548 spin_lock_irqsave(&ha->hardware_lock, flags);
1549 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
1550 readl(&ha->reg->ctrl_status);
1551 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1552 /* Wait until the firmware tells us the Soft Reset is done */
1553 max_wait_time = SOFT_RESET_TOV;
1555 spin_lock_irqsave(&ha->hardware_lock, flags);
1556 ctrl_status = readw(&ha->reg->ctrl_status);
1557 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1559 if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
1560 status = QLA_SUCCESS;
1565 } while ((--max_wait_time));
1572 * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
1573 * @ha: Pointer to host adapter structure.
1574 * @res: returned scsi status
1576 * This routine is called just prior to a HARD RESET to return all
1577 * outstanding commands back to the Operating System.
1578 * Caller should make sure that the following locks are released
1579 * before this calling routine: Hardware lock, and io_request_lock.
1581 static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
1585 unsigned long flags;
1587 spin_lock_irqsave(&ha->hardware_lock, flags);
1588 for (i = 0; i < ha->host->can_queue; i++) {
1589 srb = qla4xxx_del_from_active_array(ha, i);
1591 srb->cmd->result = res;
1592 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
1595 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1598 void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
1600 clear_bit(AF_ONLINE, &ha->flags);
1602 /* Disable the board */
1603 ql4_printk(KERN_INFO, ha, "Disabling the board\n");
1605 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
1606 qla4xxx_mark_all_devices_missing(ha);
1607 clear_bit(AF_INIT_DONE, &ha->flags);
1611 * qla4xxx_recover_adapter - recovers adapter after a fatal error
1612 * @ha: Pointer to host adapter structure.
1614 static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
1616 int status = QLA_ERROR;
1617 uint8_t reset_chip = 0;
1619 /* Stall incoming I/O until we are done */
1620 scsi_block_requests(ha->host);
1621 clear_bit(AF_ONLINE, &ha->flags);
1623 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
1625 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
1627 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
1630 /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
1631 * do not reset adapter, jump to initialize_adapter */
1632 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1633 status = QLA_SUCCESS;
1634 goto recover_ha_init_adapter;
1637 /* For the ISP-82xx adapter, issue a stop_firmware if invoked
1638 * from eh_host_reset or ioctl module */
1639 if (is_qla8022(ha) && !reset_chip &&
1640 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
1642 DEBUG2(ql4_printk(KERN_INFO, ha,
1643 "scsi%ld: %s - Performing stop_firmware...\n",
1644 ha->host_no, __func__));
1645 status = ha->isp_ops->reset_firmware(ha);
1646 if (status == QLA_SUCCESS) {
1647 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
1648 qla4xxx_cmd_wait(ha);
1649 ha->isp_ops->disable_intrs(ha);
1650 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1651 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1653 /* If the stop_firmware fails then
1654 * reset the entire chip */
1656 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1657 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1661 /* Issue full chip reset if recovering from a catastrophic error,
1662 * or if stop_firmware fails for ISP-82xx.
1663 * This is the default case for ISP-4xxx */
1664 if (!is_qla8022(ha) || reset_chip) {
1665 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
1666 qla4xxx_cmd_wait(ha);
1667 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1668 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1669 DEBUG2(ql4_printk(KERN_INFO, ha,
1670 "scsi%ld: %s - Performing chip reset..\n",
1671 ha->host_no, __func__));
1672 status = ha->isp_ops->reset_chip(ha);
1675 /* Flush any pending ddb changed AENs */
1676 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1678 recover_ha_init_adapter:
1679 /* Upon successful firmware/chip reset, re-initialize the adapter */
1680 if (status == QLA_SUCCESS) {
1681 /* For ISP-4xxx, force function 1 to always initialize
1682 * before function 3 to prevent both funcions from
1683 * stepping on top of the other */
1684 if (!is_qla8022(ha) && (ha->mac_index == 3))
1687 /* NOTE: AF_ONLINE flag set upon successful completion of
1688 * qla4xxx_initialize_adapter */
1689 status = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
1692 /* Retry failed adapter initialization, if necessary
1693 * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
1694 * case to prevent ping-pong resets between functions */
1695 if (!test_bit(AF_ONLINE, &ha->flags) &&
1696 !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1697 /* Adapter initialization failed, see if we can retry
1699 * Since we don't want to block the DPC for too long
1700 * with multiple resets in the same thread,
1701 * utilize DPC to retry */
1702 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
1703 ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
1704 DEBUG2(printk("scsi%ld: recover adapter - retrying "
1705 "(%d) more times\n", ha->host_no,
1706 ha->retry_reset_ha_cnt));
1707 set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1710 if (ha->retry_reset_ha_cnt > 0) {
1711 /* Schedule another Reset HA--DPC will retry */
1712 ha->retry_reset_ha_cnt--;
1713 DEBUG2(printk("scsi%ld: recover adapter - "
1714 "retry remaining %d\n",
1716 ha->retry_reset_ha_cnt));
1720 if (ha->retry_reset_ha_cnt == 0) {
1721 /* Recover adapter retries have been exhausted.
1723 DEBUG2(printk("scsi%ld: recover adapter "
1724 "failed - board disabled\n",
1726 qla4xxx_dead_adapter_cleanup(ha);
1727 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1728 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
1729 clear_bit(DPC_RESET_HA_FW_CONTEXT,
1735 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
1736 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1737 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1740 ha->adapter_error_count++;
1742 if (test_bit(AF_ONLINE, &ha->flags))
1743 ha->isp_ops->enable_intrs(ha);
1745 scsi_unblock_requests(ha->host);
1747 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
1748 DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
1749 status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
1754 static void qla4xxx_relogin_all_devices(struct scsi_qla_host *ha)
1756 struct ddb_entry *ddb_entry, *dtemp;
1758 list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
1759 if ((atomic_read(&ddb_entry->state) == DDB_STATE_MISSING) ||
1760 (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD)) {
1761 if (ddb_entry->fw_ddb_device_state ==
1762 DDB_DS_SESSION_ACTIVE) {
1763 atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
1764 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
1765 " marked ONLINE\n", ha->host_no, __func__,
1766 ddb_entry->fw_ddb_index);
1768 iscsi_unblock_session(ddb_entry->sess);
1770 qla4xxx_relogin_device(ha, ddb_entry);
1775 void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
1778 queue_work(ha->dpc_thread, &ha->dpc_work);
1782 * qla4xxx_do_dpc - dpc routine
1783 * @data: in our case pointer to adapter structure
1785 * This routine is a task that is schedule by the interrupt handler
1786 * to perform the background processing for interrupts. We put it
1787 * on a task queue that is consumed whenever the scheduler runs; that's
1788 * so you can do anything (i.e. put the process to sleep etc). In fact,
1789 * the mid-level tries to sleep when it reaches the driver threshold
1790 * "host->can_queue". This can cause a panic if we were in our interrupt code.
1792 static void qla4xxx_do_dpc(struct work_struct *work)
1794 struct scsi_qla_host *ha =
1795 container_of(work, struct scsi_qla_host, dpc_work);
1796 struct ddb_entry *ddb_entry, *dtemp;
1797 int status = QLA_ERROR;
1799 DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
1800 "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
1801 ha->host_no, __func__, ha->flags, ha->dpc_flags))
1803 /* Initialization not yet finished. Don't do anything yet. */
1804 if (!test_bit(AF_INIT_DONE, &ha->flags))
1807 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1808 DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
1809 ha->host_no, __func__, ha->flags));
1813 if (is_qla8022(ha)) {
1814 if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
1815 qla4_8xxx_idc_lock(ha);
1816 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
1817 QLA82XX_DEV_FAILED);
1818 qla4_8xxx_idc_unlock(ha);
1819 ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
1820 qla4_8xxx_device_state_handler(ha);
1822 if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
1823 qla4_8xxx_need_qsnt_handler(ha);
1827 if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
1828 (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1829 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1830 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
1831 if (ql4xdontresethba) {
1832 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
1833 ha->host_no, __func__));
1834 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
1835 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1836 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1837 goto dpc_post_reset_ha;
1839 if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
1840 test_bit(DPC_RESET_HA, &ha->dpc_flags))
1841 qla4xxx_recover_adapter(ha);
1843 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1844 uint8_t wait_time = RESET_INTR_TOV;
1846 while ((readw(&ha->reg->ctrl_status) &
1847 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
1848 if (--wait_time == 0)
1853 DEBUG2(printk("scsi%ld: %s: SR|FSR "
1854 "bit not cleared-- resetting\n",
1855 ha->host_no, __func__));
1856 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1857 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
1858 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1859 status = qla4xxx_recover_adapter(ha);
1861 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1862 if (status == QLA_SUCCESS)
1863 ha->isp_ops->enable_intrs(ha);
1868 /* ---- process AEN? --- */
1869 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
1870 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
1872 /* ---- Get DHCP IP Address? --- */
1873 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
1874 qla4xxx_get_dhcp_ip_address(ha);
1876 /* ---- link change? --- */
1877 if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
1878 if (!test_bit(AF_LINK_UP, &ha->flags)) {
1879 /* ---- link down? --- */
1880 qla4xxx_mark_all_devices_missing(ha);
1882 /* ---- link up? --- *
1883 * F/W will auto login to all devices ONLY ONCE after
1884 * link up during driver initialization and runtime
1885 * fatal error recovery. Therefore, the driver must
1886 * manually relogin to devices when recovering from
1887 * connection failures, logouts, expired KATO, etc. */
1889 qla4xxx_relogin_all_devices(ha);
1893 /* ---- relogin device? --- */
1894 if (adapter_up(ha) &&
1895 test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
1896 list_for_each_entry_safe(ddb_entry, dtemp,
1897 &ha->ddb_list, list) {
1898 if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
1899 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
1900 qla4xxx_relogin_device(ha, ddb_entry);
1903 * If mbx cmd times out there is no point
1904 * in continuing further.
1905 * With large no of targets this can hang
1908 if (test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
1909 printk(KERN_WARNING "scsi%ld: %s: "
1910 "need to reset hba\n",
1911 ha->host_no, __func__);
1920 * qla4xxx_free_adapter - release the adapter
1921 * @ha: pointer to adapter structure
1923 static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
1926 if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
1927 /* Turn-off interrupts on the card. */
1928 ha->isp_ops->disable_intrs(ha);
1931 /* Remove timer thread, if present */
1932 if (ha->timer_active)
1933 qla4xxx_stop_timer(ha);
1935 /* Kill the kernel thread for this host */
1937 destroy_workqueue(ha->dpc_thread);
1939 /* Put firmware in known state */
1940 ha->isp_ops->reset_firmware(ha);
1942 if (is_qla8022(ha)) {
1943 qla4_8xxx_idc_lock(ha);
1944 qla4_8xxx_clear_drv_active(ha);
1945 qla4_8xxx_idc_unlock(ha);
1948 /* Detach interrupts */
1949 if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
1950 qla4xxx_free_irqs(ha);
1952 /* free extra memory */
1953 qla4xxx_mem_free(ha);
1956 int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
1959 uint8_t revision_id;
1960 unsigned long mem_base, mem_len, db_base, db_len;
1961 struct pci_dev *pdev = ha->pdev;
1963 status = pci_request_regions(pdev, DRIVER_NAME);
1966 "scsi(%ld) Failed to reserve PIO regions (%s) "
1967 "status=%d\n", ha->host_no, pci_name(pdev), status);
1968 goto iospace_error_exit;
1971 pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
1972 DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
1973 __func__, revision_id));
1974 ha->revision_id = revision_id;
1976 /* remap phys address */
1977 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
1978 mem_len = pci_resource_len(pdev, 0);
1979 DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
1980 __func__, mem_base, mem_len));
1982 /* mapping of pcibase pointer */
1983 ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
1984 if (!ha->nx_pcibase) {
1986 "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
1987 pci_release_regions(ha->pdev);
1988 goto iospace_error_exit;
1991 /* Mapping of IO base pointer, door bell read and write pointer */
1993 /* mapping of IO base pointer */
1995 (struct device_reg_82xx __iomem *)((uint8_t *)ha->nx_pcibase +
1996 0xbc000 + (ha->pdev->devfn << 11));
1998 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
1999 db_len = pci_resource_len(pdev, 4);
2001 ha->nx_db_wr_ptr = (ha->pdev->devfn == 4 ? QLA82XX_CAM_RAM_DB1 :
2002 QLA82XX_CAM_RAM_DB2);
2010 * qla4xxx_iospace_config - maps registers
2011 * @ha: pointer to adapter structure
2013 * This routines maps HBA's registers from the pci address space
2014 * into the kernel virtual address space for memory mapped i/o.
2016 int qla4xxx_iospace_config(struct scsi_qla_host *ha)
2018 unsigned long pio, pio_len, pio_flags;
2019 unsigned long mmio, mmio_len, mmio_flags;
2021 pio = pci_resource_start(ha->pdev, 0);
2022 pio_len = pci_resource_len(ha->pdev, 0);
2023 pio_flags = pci_resource_flags(ha->pdev, 0);
2024 if (pio_flags & IORESOURCE_IO) {
2025 if (pio_len < MIN_IOBASE_LEN) {
2026 ql4_printk(KERN_WARNING, ha,
2027 "Invalid PCI I/O region size\n");
2031 ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
2035 /* Use MMIO operations for all accesses. */
2036 mmio = pci_resource_start(ha->pdev, 1);
2037 mmio_len = pci_resource_len(ha->pdev, 1);
2038 mmio_flags = pci_resource_flags(ha->pdev, 1);
2040 if (!(mmio_flags & IORESOURCE_MEM)) {
2041 ql4_printk(KERN_ERR, ha,
2042 "region #0 not an MMIO resource, aborting\n");
2044 goto iospace_error_exit;
2047 if (mmio_len < MIN_IOBASE_LEN) {
2048 ql4_printk(KERN_ERR, ha,
2049 "Invalid PCI mem region size, aborting\n");
2050 goto iospace_error_exit;
2053 if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
2054 ql4_printk(KERN_WARNING, ha,
2055 "Failed to reserve PIO/MMIO regions\n");
2057 goto iospace_error_exit;
2060 ha->pio_address = pio;
2061 ha->pio_length = pio_len;
2062 ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
2064 ql4_printk(KERN_ERR, ha,
2065 "cannot remap MMIO, aborting\n");
2067 goto iospace_error_exit;
2076 static struct isp_operations qla4xxx_isp_ops = {
2077 .iospace_config = qla4xxx_iospace_config,
2078 .pci_config = qla4xxx_pci_config,
2079 .disable_intrs = qla4xxx_disable_intrs,
2080 .enable_intrs = qla4xxx_enable_intrs,
2081 .start_firmware = qla4xxx_start_firmware,
2082 .intr_handler = qla4xxx_intr_handler,
2083 .interrupt_service_routine = qla4xxx_interrupt_service_routine,
2084 .reset_chip = qla4xxx_soft_reset,
2085 .reset_firmware = qla4xxx_hw_reset,
2086 .queue_iocb = qla4xxx_queue_iocb,
2087 .complete_iocb = qla4xxx_complete_iocb,
2088 .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
2089 .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
2090 .get_sys_info = qla4xxx_get_sys_info,
2093 static struct isp_operations qla4_8xxx_isp_ops = {
2094 .iospace_config = qla4_8xxx_iospace_config,
2095 .pci_config = qla4_8xxx_pci_config,
2096 .disable_intrs = qla4_8xxx_disable_intrs,
2097 .enable_intrs = qla4_8xxx_enable_intrs,
2098 .start_firmware = qla4_8xxx_load_risc,
2099 .intr_handler = qla4_8xxx_intr_handler,
2100 .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
2101 .reset_chip = qla4_8xxx_isp_reset,
2102 .reset_firmware = qla4_8xxx_stop_firmware,
2103 .queue_iocb = qla4_8xxx_queue_iocb,
2104 .complete_iocb = qla4_8xxx_complete_iocb,
2105 .rd_shdw_req_q_out = qla4_8xxx_rd_shdw_req_q_out,
2106 .rd_shdw_rsp_q_in = qla4_8xxx_rd_shdw_rsp_q_in,
2107 .get_sys_info = qla4_8xxx_get_sys_info,
2110 uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
2112 return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
2115 uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
2117 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
2120 uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
2122 return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
2125 uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
2127 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
2131 * qla4xxx_probe_adapter - callback function to probe HBA
2132 * @pdev: pointer to pci_dev structure
2133 * @pci_device_id: pointer to pci_device entry
2135 * This routine will probe for Qlogic 4xxx iSCSI host adapters.
2136 * It returns zero if successful. It also initializes all data necessary for
2139 static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
2140 const struct pci_device_id *ent)
2142 int ret = -ENODEV, status;
2143 struct Scsi_Host *host;
2144 struct scsi_qla_host *ha;
2145 uint8_t init_retry_count = 0;
2147 struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
2150 if (pci_enable_device(pdev))
2153 host = scsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha));
2156 "qla4xxx: Couldn't allocate host from scsi layer!\n");
2157 goto probe_disable_device;
2160 /* Clear our data area */
2161 ha = (struct scsi_qla_host *) host->hostdata;
2162 memset(ha, 0, sizeof(*ha));
2164 /* Save the information from PCI BIOS. */
2167 ha->host_no = host->host_no;
2169 pci_enable_pcie_error_reporting(pdev);
2171 /* Setup Runtime configurable options */
2172 if (is_qla8022(ha)) {
2173 ha->isp_ops = &qla4_8xxx_isp_ops;
2174 rwlock_init(&ha->hw_lock);
2175 ha->qdr_sn_window = -1;
2176 ha->ddr_mn_window = -1;
2177 ha->curr_window = 255;
2178 ha->func_num = PCI_FUNC(ha->pdev->devfn);
2179 nx_legacy_intr = &legacy_intr[ha->func_num];
2180 ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
2181 ha->nx_legacy_intr.tgt_status_reg =
2182 nx_legacy_intr->tgt_status_reg;
2183 ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
2184 ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
2186 ha->isp_ops = &qla4xxx_isp_ops;
2189 /* Set EEH reset type to fundamental if required by hba */
2191 pdev->needs_freset = 1;
2193 /* Configure PCI I/O space. */
2194 ret = ha->isp_ops->iospace_config(ha);
2196 goto probe_failed_ioconfig;
2198 ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
2199 pdev->device, pdev->irq, ha->reg);
2201 qla4xxx_config_dma_addressing(ha);
2203 /* Initialize lists and spinlocks. */
2204 INIT_LIST_HEAD(&ha->ddb_list);
2205 INIT_LIST_HEAD(&ha->free_srb_q);
2207 mutex_init(&ha->mbox_sem);
2208 init_completion(&ha->mbx_intr_comp);
2210 spin_lock_init(&ha->hardware_lock);
2212 /* Allocate dma buffers */
2213 if (qla4xxx_mem_alloc(ha)) {
2214 ql4_printk(KERN_WARNING, ha,
2215 "[ERROR] Failed to allocate memory for adapter\n");
2222 (void) qla4_8xxx_get_flash_info(ha);
2225 * Initialize the Host adapter request/response queues and
2227 * NOTE: interrupts enabled upon successful completion
2229 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
2230 while ((!test_bit(AF_ONLINE, &ha->flags)) &&
2231 init_retry_count++ < MAX_INIT_RETRIES) {
2233 if (is_qla8022(ha)) {
2234 qla4_8xxx_idc_lock(ha);
2235 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
2236 qla4_8xxx_idc_unlock(ha);
2237 if (dev_state == QLA82XX_DEV_FAILED) {
2238 ql4_printk(KERN_WARNING, ha, "%s: don't retry "
2239 "initialize adapter. H/W is in failed state\n",
2244 DEBUG2(printk("scsi: %s: retrying adapter initialization "
2245 "(%d)\n", __func__, init_retry_count));
2247 if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
2250 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
2253 if (!test_bit(AF_ONLINE, &ha->flags)) {
2254 ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
2256 if (is_qla8022(ha) && ql4xdontresethba) {
2257 /* Put the device in failed state. */
2258 DEBUG2(printk(KERN_ERR "HW STATE: FAILED\n"));
2259 qla4_8xxx_idc_lock(ha);
2260 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2261 QLA82XX_DEV_FAILED);
2262 qla4_8xxx_idc_unlock(ha);
2268 host->cmd_per_lun = 3;
2269 host->max_channel = 0;
2270 host->max_lun = MAX_LUNS - 1;
2271 host->max_id = MAX_TARGETS;
2272 host->max_cmd_len = IOCB_MAX_CDB_LEN;
2273 host->can_queue = MAX_SRBS ;
2274 host->transportt = qla4xxx_scsi_transport;
2276 ret = scsi_init_shared_tag_map(host, MAX_SRBS);
2278 ql4_printk(KERN_WARNING, ha,
2279 "scsi_init_shared_tag_map failed\n");
2283 /* Startup the kernel thread for this host adapter. */
2284 DEBUG2(printk("scsi: %s: Starting kernel thread for "
2285 "qla4xxx_dpc\n", __func__));
2286 sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
2287 ha->dpc_thread = create_singlethread_workqueue(buf);
2288 if (!ha->dpc_thread) {
2289 ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
2293 INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
2295 /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
2296 * (which is called indirectly by qla4xxx_initialize_adapter),
2297 * so that irqs will be registered after crbinit but before
2300 if (!is_qla8022(ha)) {
2301 ret = qla4xxx_request_irqs(ha);
2303 ql4_printk(KERN_WARNING, ha, "Failed to reserve "
2304 "interrupt %d already in use.\n", pdev->irq);
2309 pci_save_state(ha->pdev);
2310 ha->isp_ops->enable_intrs(ha);
2312 /* Start timer thread. */
2313 qla4xxx_start_timer(ha, qla4xxx_timer, 1);
2315 set_bit(AF_INIT_DONE, &ha->flags);
2317 pci_set_drvdata(pdev, ha);
2319 ret = scsi_add_host(host, &pdev->dev);
2324 " QLogic iSCSI HBA Driver version: %s\n"
2325 " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
2326 qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
2327 ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
2328 ha->patch_number, ha->build_number);
2329 scsi_scan_host(host);
2331 qla4xxx_create_ifaces(ha);
2335 qla4xxx_free_adapter(ha);
2337 probe_failed_ioconfig:
2338 pci_disable_pcie_error_reporting(pdev);
2339 scsi_host_put(ha->host);
2341 probe_disable_device:
2342 pci_disable_device(pdev);
2348 * qla4xxx_prevent_other_port_reinit - prevent other port from re-initialize
2349 * @ha: pointer to adapter structure
2351 * Mark the other ISP-4xxx port to indicate that the driver is being removed,
2352 * so that the other port will not re-initialize while in the process of
2353 * removing the ha due to driver unload or hba hotplug.
2355 static void qla4xxx_prevent_other_port_reinit(struct scsi_qla_host *ha)
2357 struct scsi_qla_host *other_ha = NULL;
2358 struct pci_dev *other_pdev = NULL;
2359 int fn = ISP4XXX_PCI_FN_2;
2361 /*iscsi function numbers for ISP4xxx is 1 and 3*/
2362 if (PCI_FUNC(ha->pdev->devfn) & BIT_1)
2363 fn = ISP4XXX_PCI_FN_1;
2366 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
2367 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
2370 /* Get other_ha if other_pdev is valid and state is enable*/
2372 if (atomic_read(&other_pdev->enable_cnt)) {
2373 other_ha = pci_get_drvdata(other_pdev);
2375 set_bit(AF_HA_REMOVAL, &other_ha->flags);
2376 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: "
2377 "Prevent %s reinit\n", __func__,
2378 dev_name(&other_ha->pdev->dev)));
2381 pci_dev_put(other_pdev);
2386 * qla4xxx_remove_adapter - calback function to remove adapter.
2387 * @pci_dev: PCI device pointer
2389 static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
2391 struct scsi_qla_host *ha;
2393 ha = pci_get_drvdata(pdev);
2395 if (!is_qla8022(ha))
2396 qla4xxx_prevent_other_port_reinit(ha);
2398 /* remove devs from iscsi_sessions to scsi_devices */
2399 qla4xxx_free_ddb_list(ha);
2401 /* destroy iface from sysfs */
2402 qla4xxx_destroy_ifaces(ha);
2404 scsi_remove_host(ha->host);
2406 qla4xxx_free_adapter(ha);
2408 scsi_host_put(ha->host);
2410 pci_disable_pcie_error_reporting(pdev);
2411 pci_disable_device(pdev);
2412 pci_set_drvdata(pdev, NULL);
2416 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
2419 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
2420 * supported addressing method.
2422 static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
2426 /* Update our PCI device dma_mask for full 64 bit mask */
2427 if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
2428 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
2429 dev_dbg(&ha->pdev->dev,
2430 "Failed to set 64 bit PCI consistent mask; "
2432 retval = pci_set_consistent_dma_mask(ha->pdev,
2436 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
2439 static int qla4xxx_slave_alloc(struct scsi_device *sdev)
2441 struct iscsi_cls_session *sess = starget_to_session(sdev->sdev_target);
2442 struct ddb_entry *ddb = sess->dd_data;
2443 int queue_depth = QL4_DEF_QDEPTH;
2445 sdev->hostdata = ddb;
2446 sdev->tagged_supported = 1;
2448 if (ql4xmaxqdepth != 0 && ql4xmaxqdepth <= 0xffffU)
2449 queue_depth = ql4xmaxqdepth;
2451 scsi_activate_tcq(sdev, queue_depth);
2455 static int qla4xxx_slave_configure(struct scsi_device *sdev)
2457 sdev->tagged_supported = 1;
2461 static void qla4xxx_slave_destroy(struct scsi_device *sdev)
2463 scsi_deactivate_tcq(sdev, 1);
2467 * qla4xxx_del_from_active_array - returns an active srb
2468 * @ha: Pointer to host adapter structure.
2469 * @index: index into the active_array
2471 * This routine removes and returns the srb at the specified index
2473 struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
2476 struct srb *srb = NULL;
2477 struct scsi_cmnd *cmd = NULL;
2479 cmd = scsi_host_find_tag(ha->host, index);
2483 srb = (struct srb *)CMD_SP(cmd);
2487 /* update counters */
2488 if (srb->flags & SRB_DMA_VALID) {
2489 ha->req_q_count += srb->iocb_cnt;
2490 ha->iocb_cnt -= srb->iocb_cnt;
2492 srb->cmd->host_scribble =
2493 (unsigned char *)(unsigned long) MAX_SRBS;
2499 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
2500 * @ha: Pointer to host adapter structure.
2501 * @cmd: Scsi Command to wait on.
2503 * This routine waits for the command to be returned by the Firmware
2504 * for some max time.
2506 static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
2507 struct scsi_cmnd *cmd)
2511 uint32_t max_wait_time = EH_WAIT_CMD_TOV;
2514 /* Dont wait on command if PCI error is being handled
2517 if (unlikely(pci_channel_offline(ha->pdev)) ||
2518 (test_bit(AF_EEH_BUSY, &ha->flags))) {
2519 ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
2520 ha->host_no, __func__);
2525 /* Checking to see if its returned to OS */
2526 rp = (struct srb *) CMD_SP(cmd);
2533 } while (max_wait_time--);
2539 * qla4xxx_wait_for_hba_online - waits for HBA to come online
2540 * @ha: Pointer to host adapter structure
2542 static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
2544 unsigned long wait_online;
2546 wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
2547 while (time_before(jiffies, wait_online)) {
2559 * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
2560 * @ha: pointer to HBA
2564 * This function waits for all outstanding commands to a lun to complete. It
2565 * returns 0 if all pending commands are returned and 1 otherwise.
2567 static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
2568 struct scsi_target *stgt,
2569 struct scsi_device *sdev)
2573 struct scsi_cmnd *cmd;
2576 * Waiting for all commands for the designated target or dev
2577 * in the active array
2579 for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
2580 cmd = scsi_host_find_tag(ha->host, cnt);
2581 if (cmd && stgt == scsi_target(cmd->device) &&
2582 (!sdev || sdev == cmd->device)) {
2583 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
2593 * qla4xxx_eh_abort - callback for abort task.
2594 * @cmd: Pointer to Linux's SCSI command structure
2596 * This routine is called by the Linux OS to abort the specified
2599 static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
2601 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2602 unsigned int id = cmd->device->id;
2603 unsigned int lun = cmd->device->lun;
2604 unsigned long flags;
2605 struct srb *srb = NULL;
2609 ql4_printk(KERN_INFO, ha,
2610 "scsi%ld:%d:%d: Abort command issued cmd=%p\n",
2611 ha->host_no, id, lun, cmd);
2613 spin_lock_irqsave(&ha->hardware_lock, flags);
2614 srb = (struct srb *) CMD_SP(cmd);
2616 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2619 kref_get(&srb->srb_ref);
2620 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2622 if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
2623 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
2624 ha->host_no, id, lun));
2627 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
2628 ha->host_no, id, lun));
2632 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
2634 /* Wait for command to complete */
2636 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
2637 DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
2638 ha->host_no, id, lun));
2643 ql4_printk(KERN_INFO, ha,
2644 "scsi%ld:%d:%d: Abort command - %s\n",
2645 ha->host_no, id, lun, (ret == SUCCESS) ? "succeeded" : "failed");
2651 * qla4xxx_eh_device_reset - callback for target reset.
2652 * @cmd: Pointer to Linux's SCSI command structure
2654 * This routine is called by the Linux OS to reset all luns on the
2657 static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
2659 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2660 struct ddb_entry *ddb_entry = cmd->device->hostdata;
2661 int ret = FAILED, stat;
2666 ret = iscsi_block_scsi_eh(cmd);
2671 ql4_printk(KERN_INFO, ha,
2672 "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
2673 cmd->device->channel, cmd->device->id, cmd->device->lun);
2675 DEBUG2(printk(KERN_INFO
2676 "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
2677 "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
2678 cmd, jiffies, cmd->request->timeout / HZ,
2679 ha->dpc_flags, cmd->result, cmd->allowed));
2681 /* FIXME: wait for hba to go online */
2682 stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
2683 if (stat != QLA_SUCCESS) {
2684 ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
2685 goto eh_dev_reset_done;
2688 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2690 ql4_printk(KERN_INFO, ha,
2691 "DEVICE RESET FAILED - waiting for "
2693 goto eh_dev_reset_done;
2697 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2698 MM_LUN_RESET) != QLA_SUCCESS)
2699 goto eh_dev_reset_done;
2701 ql4_printk(KERN_INFO, ha,
2702 "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
2703 ha->host_no, cmd->device->channel, cmd->device->id,
2714 * qla4xxx_eh_target_reset - callback for target reset.
2715 * @cmd: Pointer to Linux's SCSI command structure
2717 * This routine is called by the Linux OS to reset the target.
2719 static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
2721 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2722 struct ddb_entry *ddb_entry = cmd->device->hostdata;
2728 ret = iscsi_block_scsi_eh(cmd);
2732 starget_printk(KERN_INFO, scsi_target(cmd->device),
2733 "WARM TARGET RESET ISSUED.\n");
2735 DEBUG2(printk(KERN_INFO
2736 "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
2737 "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
2738 ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
2739 ha->dpc_flags, cmd->result, cmd->allowed));
2741 stat = qla4xxx_reset_target(ha, ddb_entry);
2742 if (stat != QLA_SUCCESS) {
2743 starget_printk(KERN_INFO, scsi_target(cmd->device),
2744 "WARM TARGET RESET FAILED.\n");
2748 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2750 starget_printk(KERN_INFO, scsi_target(cmd->device),
2751 "WARM TARGET DEVICE RESET FAILED - "
2752 "waiting for commands.\n");
2757 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2758 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
2759 starget_printk(KERN_INFO, scsi_target(cmd->device),
2760 "WARM TARGET DEVICE RESET FAILED - "
2761 "marker iocb failed.\n");
2765 starget_printk(KERN_INFO, scsi_target(cmd->device),
2766 "WARM TARGET RESET SUCCEEDED.\n");
2771 * qla4xxx_eh_host_reset - kernel callback
2772 * @cmd: Pointer to Linux's SCSI command structure
2774 * This routine is invoked by the Linux kernel to perform fatal error
2775 * recovery on the specified adapter.
2777 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
2779 int return_status = FAILED;
2780 struct scsi_qla_host *ha;
2782 ha = (struct scsi_qla_host *) cmd->device->host->hostdata;
2784 if (ql4xdontresethba) {
2785 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
2786 ha->host_no, __func__));
2790 ql4_printk(KERN_INFO, ha,
2791 "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
2792 cmd->device->channel, cmd->device->id, cmd->device->lun);
2794 if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
2795 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
2796 "DEAD.\n", ha->host_no, cmd->device->channel,
2802 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
2804 set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2806 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2809 if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
2810 return_status = SUCCESS;
2812 ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
2813 return_status == FAILED ? "FAILED" : "SUCCEEDED");
2815 return return_status;
2818 /* PCI AER driver recovers from all correctable errors w/o
2819 * driver intervention. For uncorrectable errors PCI AER
2820 * driver calls the following device driver's callbacks
2822 * - Fatal Errors - link_reset
2823 * - Non-Fatal Errors - driver's pci_error_detected() which
2824 * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
2826 * PCI AER driver calls
2827 * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
2828 * returns RECOVERED or NEED_RESET if fw_hung
2829 * NEED_RESET - driver's slot_reset()
2830 * DISCONNECT - device is dead & cannot recover
2831 * RECOVERED - driver's pci_resume()
2833 static pci_ers_result_t
2834 qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
2836 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2838 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
2839 ha->host_no, __func__, state);
2841 if (!is_aer_supported(ha))
2842 return PCI_ERS_RESULT_NONE;
2845 case pci_channel_io_normal:
2846 clear_bit(AF_EEH_BUSY, &ha->flags);
2847 return PCI_ERS_RESULT_CAN_RECOVER;
2848 case pci_channel_io_frozen:
2849 set_bit(AF_EEH_BUSY, &ha->flags);
2850 qla4xxx_mailbox_premature_completion(ha);
2851 qla4xxx_free_irqs(ha);
2852 pci_disable_device(pdev);
2853 /* Return back all IOs */
2854 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
2855 return PCI_ERS_RESULT_NEED_RESET;
2856 case pci_channel_io_perm_failure:
2857 set_bit(AF_EEH_BUSY, &ha->flags);
2858 set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
2859 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
2860 return PCI_ERS_RESULT_DISCONNECT;
2862 return PCI_ERS_RESULT_NEED_RESET;
2866 * qla4xxx_pci_mmio_enabled() gets called if
2867 * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
2868 * and read/write to the device still works.
2870 static pci_ers_result_t
2871 qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
2873 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2875 if (!is_aer_supported(ha))
2876 return PCI_ERS_RESULT_NONE;
2878 return PCI_ERS_RESULT_RECOVERED;
2881 static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
2883 uint32_t rval = QLA_ERROR;
2886 struct pci_dev *other_pdev = NULL;
2888 ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
2890 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2892 if (test_bit(AF_ONLINE, &ha->flags)) {
2893 clear_bit(AF_ONLINE, &ha->flags);
2894 qla4xxx_mark_all_devices_missing(ha);
2895 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2898 fn = PCI_FUNC(ha->pdev->devfn);
2901 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
2902 "func %x\n", ha->host_no, __func__, fn);
2903 /* Get the pci device given the domain, bus,
2904 * slot/function number */
2906 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
2907 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
2913 if (atomic_read(&other_pdev->enable_cnt)) {
2914 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
2915 "func in enabled state%x\n", ha->host_no,
2917 pci_dev_put(other_pdev);
2920 pci_dev_put(other_pdev);
2923 /* The first function on the card, the reset owner will
2924 * start & initialize the firmware. The other functions
2925 * on the card will reset the firmware context
2928 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
2929 "0x%x is the owner\n", ha->host_no, __func__,
2932 qla4_8xxx_idc_lock(ha);
2933 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2936 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
2937 QLA82XX_IDC_VERSION);
2939 qla4_8xxx_idc_unlock(ha);
2940 clear_bit(AF_FW_RECOVERY, &ha->flags);
2941 rval = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
2942 qla4_8xxx_idc_lock(ha);
2944 if (rval != QLA_SUCCESS) {
2945 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
2946 "FAILED\n", ha->host_no, __func__);
2947 qla4_8xxx_clear_drv_active(ha);
2948 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2949 QLA82XX_DEV_FAILED);
2951 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
2952 "READY\n", ha->host_no, __func__);
2953 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2955 /* Clear driver state register */
2956 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
2957 qla4_8xxx_set_drv_active(ha);
2958 ret = qla4xxx_request_irqs(ha);
2960 ql4_printk(KERN_WARNING, ha, "Failed to "
2961 "reserve interrupt %d already in use.\n",
2965 ha->isp_ops->enable_intrs(ha);
2969 qla4_8xxx_idc_unlock(ha);
2971 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
2972 "the reset owner\n", ha->host_no, __func__,
2974 if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
2975 QLA82XX_DEV_READY)) {
2976 clear_bit(AF_FW_RECOVERY, &ha->flags);
2977 rval = qla4xxx_initialize_adapter(ha,
2979 if (rval == QLA_SUCCESS) {
2980 ret = qla4xxx_request_irqs(ha);
2982 ql4_printk(KERN_WARNING, ha, "Failed to"
2983 " reserve interrupt %d already in"
2984 " use.\n", ha->pdev->irq);
2987 ha->isp_ops->enable_intrs(ha);
2991 qla4_8xxx_idc_lock(ha);
2992 qla4_8xxx_set_drv_active(ha);
2993 qla4_8xxx_idc_unlock(ha);
2996 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
3000 static pci_ers_result_t
3001 qla4xxx_pci_slot_reset(struct pci_dev *pdev)
3003 pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
3004 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
3007 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
3008 ha->host_no, __func__);
3010 if (!is_aer_supported(ha))
3011 return PCI_ERS_RESULT_NONE;
3013 /* Restore the saved state of PCIe device -
3014 * BAR registers, PCI Config space, PCIX, MSI,
3017 pci_restore_state(pdev);
3019 /* pci_restore_state() clears the saved_state flag of the device
3020 * save restored state which resets saved_state flag
3022 pci_save_state(pdev);
3024 /* Initialize device or resume if in suspended state */
3025 rc = pci_enable_device(pdev);
3027 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Can't re-enable "
3028 "device after reset\n", ha->host_no, __func__);
3029 goto exit_slot_reset;
3032 ha->isp_ops->disable_intrs(ha);
3034 if (is_qla8022(ha)) {
3035 if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
3036 ret = PCI_ERS_RESULT_RECOVERED;
3037 goto exit_slot_reset;
3039 goto exit_slot_reset;
3043 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
3044 "device after reset\n", ha->host_no, __func__, ret);
3049 qla4xxx_pci_resume(struct pci_dev *pdev)
3051 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
3054 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
3055 ha->host_no, __func__);
3057 ret = qla4xxx_wait_for_hba_online(ha);
3058 if (ret != QLA_SUCCESS) {
3059 ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
3060 "resume I/O from slot/link_reset\n", ha->host_no,
3064 pci_cleanup_aer_uncorrect_error_status(pdev);
3065 clear_bit(AF_EEH_BUSY, &ha->flags);
3068 static struct pci_error_handlers qla4xxx_err_handler = {
3069 .error_detected = qla4xxx_pci_error_detected,
3070 .mmio_enabled = qla4xxx_pci_mmio_enabled,
3071 .slot_reset = qla4xxx_pci_slot_reset,
3072 .resume = qla4xxx_pci_resume,
3075 static struct pci_device_id qla4xxx_pci_tbl[] = {
3077 .vendor = PCI_VENDOR_ID_QLOGIC,
3078 .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
3079 .subvendor = PCI_ANY_ID,
3080 .subdevice = PCI_ANY_ID,
3083 .vendor = PCI_VENDOR_ID_QLOGIC,
3084 .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
3085 .subvendor = PCI_ANY_ID,
3086 .subdevice = PCI_ANY_ID,
3089 .vendor = PCI_VENDOR_ID_QLOGIC,
3090 .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
3091 .subvendor = PCI_ANY_ID,
3092 .subdevice = PCI_ANY_ID,
3095 .vendor = PCI_VENDOR_ID_QLOGIC,
3096 .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
3097 .subvendor = PCI_ANY_ID,
3098 .subdevice = PCI_ANY_ID,
3102 MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
3104 static struct pci_driver qla4xxx_pci_driver = {
3105 .name = DRIVER_NAME,
3106 .id_table = qla4xxx_pci_tbl,
3107 .probe = qla4xxx_probe_adapter,
3108 .remove = qla4xxx_remove_adapter,
3109 .err_handler = &qla4xxx_err_handler,
3112 static int __init qla4xxx_module_init(void)
3116 /* Allocate cache for SRBs. */
3117 srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
3118 SLAB_HWCACHE_ALIGN, NULL);
3119 if (srb_cachep == NULL) {
3121 "%s: Unable to allocate SRB cache..."
3122 "Failing load!\n", DRIVER_NAME);
3127 /* Derive version string. */
3128 strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
3129 if (ql4xextended_error_logging)
3130 strcat(qla4xxx_version_str, "-debug");
3132 qla4xxx_scsi_transport =
3133 iscsi_register_transport(&qla4xxx_iscsi_transport);
3134 if (!qla4xxx_scsi_transport){
3136 goto release_srb_cache;
3139 ret = pci_register_driver(&qla4xxx_pci_driver);
3141 goto unregister_transport;
3143 printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
3146 unregister_transport:
3147 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
3149 kmem_cache_destroy(srb_cachep);
3154 static void __exit qla4xxx_module_exit(void)
3156 pci_unregister_driver(&qla4xxx_pci_driver);
3157 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
3158 kmem_cache_destroy(srb_cachep);
3161 module_init(qla4xxx_module_init);
3162 module_exit(qla4xxx_module_exit);
3164 MODULE_AUTHOR("QLogic Corporation");
3165 MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
3166 MODULE_LICENSE("GPL");
3167 MODULE_VERSION(QLA4XXX_DRIVER_VERSION);