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>
9 #include <linux/blkdev.h>
10 #include <linux/iscsi_boot_sysfs.h>
12 #include <scsi/scsi_tcq.h>
13 #include <scsi/scsicam.h>
16 #include "ql4_version.h"
19 #include "ql4_inline.h"
24 static char qla4xxx_version_str[40];
27 * SRB allocation cache
29 static struct kmem_cache *srb_cachep;
32 * Module parameter information and variables
34 int ql4xdontresethba = 0;
35 module_param(ql4xdontresethba, int, S_IRUGO | S_IWUSR);
36 MODULE_PARM_DESC(ql4xdontresethba,
37 "Don't reset the HBA for driver recovery \n"
38 " 0 - It will reset HBA (Default)\n"
39 " 1 - It will NOT reset HBA");
41 int ql4xextended_error_logging = 0; /* 0 = off, 1 = log errors */
42 module_param(ql4xextended_error_logging, int, S_IRUGO | S_IWUSR);
43 MODULE_PARM_DESC(ql4xextended_error_logging,
44 "Option to enable extended error logging, "
45 "Default is 0 - no logging, 1 - debug logging");
47 int ql4xenablemsix = 1;
48 module_param(ql4xenablemsix, int, S_IRUGO|S_IWUSR);
49 MODULE_PARM_DESC(ql4xenablemsix,
50 "Set to enable MSI or MSI-X interrupt mechanism.\n"
51 " 0 = enable INTx interrupt mechanism.\n"
52 " 1 = enable MSI-X interrupt mechanism (Default).\n"
53 " 2 = enable MSI interrupt mechanism.");
55 #define QL4_DEF_QDEPTH 32
56 static int ql4xmaxqdepth = QL4_DEF_QDEPTH;
57 module_param(ql4xmaxqdepth, int, S_IRUGO | S_IWUSR);
58 MODULE_PARM_DESC(ql4xmaxqdepth,
59 "Maximum queue depth to report for target devices.\n"
62 static int ql4xsess_recovery_tmo = QL4_SESS_RECOVERY_TMO;
63 module_param(ql4xsess_recovery_tmo, int, S_IRUGO);
64 MODULE_PARM_DESC(ql4xsess_recovery_tmo,
65 "Target Session Recovery Timeout.\n"
68 static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha);
70 * SCSI host template entry points
72 static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
75 * iSCSI template entry points
77 static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
78 enum iscsi_param param, char *buf);
79 static int qla4xxx_host_get_param(struct Scsi_Host *shost,
80 enum iscsi_host_param param, char *buf);
81 static int qla4xxx_iface_set_param(struct Scsi_Host *shost, void *data,
83 static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
84 enum iscsi_param_type param_type,
85 int param, char *buf);
86 static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
87 static struct iscsi_endpoint *qla4xxx_ep_connect(struct Scsi_Host *shost,
88 struct sockaddr *dst_addr,
90 static int qla4xxx_ep_poll(struct iscsi_endpoint *ep, int timeout_ms);
91 static void qla4xxx_ep_disconnect(struct iscsi_endpoint *ep);
92 static int qla4xxx_get_ep_param(struct iscsi_endpoint *ep,
93 enum iscsi_param param, char *buf);
94 static int qla4xxx_conn_start(struct iscsi_cls_conn *conn);
95 static struct iscsi_cls_conn *
96 qla4xxx_conn_create(struct iscsi_cls_session *cls_sess, uint32_t conn_idx);
97 static int qla4xxx_conn_bind(struct iscsi_cls_session *cls_session,
98 struct iscsi_cls_conn *cls_conn,
99 uint64_t transport_fd, int is_leading);
100 static void qla4xxx_conn_destroy(struct iscsi_cls_conn *conn);
101 static struct iscsi_cls_session *
102 qla4xxx_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max,
103 uint16_t qdepth, uint32_t initial_cmdsn);
104 static void qla4xxx_session_destroy(struct iscsi_cls_session *sess);
105 static void qla4xxx_task_work(struct work_struct *wdata);
106 static int qla4xxx_alloc_pdu(struct iscsi_task *, uint8_t);
107 static int qla4xxx_task_xmit(struct iscsi_task *);
108 static void qla4xxx_task_cleanup(struct iscsi_task *);
109 static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session);
110 static void qla4xxx_conn_get_stats(struct iscsi_cls_conn *cls_conn,
111 struct iscsi_stats *stats);
113 * SCSI host template entry points
115 static int qla4xxx_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmd);
116 static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
117 static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
118 static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
119 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
120 static int qla4xxx_slave_alloc(struct scsi_device *device);
121 static int qla4xxx_slave_configure(struct scsi_device *device);
122 static void qla4xxx_slave_destroy(struct scsi_device *sdev);
123 static mode_t ql4_attr_is_visible(int param_type, int param);
124 static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type);
126 static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
127 QLA82XX_LEGACY_INTR_CONFIG;
129 static struct scsi_host_template qla4xxx_driver_template = {
130 .module = THIS_MODULE,
132 .proc_name = DRIVER_NAME,
133 .queuecommand = qla4xxx_queuecommand,
135 .eh_abort_handler = qla4xxx_eh_abort,
136 .eh_device_reset_handler = qla4xxx_eh_device_reset,
137 .eh_target_reset_handler = qla4xxx_eh_target_reset,
138 .eh_host_reset_handler = qla4xxx_eh_host_reset,
139 .eh_timed_out = qla4xxx_eh_cmd_timed_out,
141 .slave_configure = qla4xxx_slave_configure,
142 .slave_alloc = qla4xxx_slave_alloc,
143 .slave_destroy = qla4xxx_slave_destroy,
147 .use_clustering = ENABLE_CLUSTERING,
148 .sg_tablesize = SG_ALL,
150 .max_sectors = 0xFFFF,
151 .shost_attrs = qla4xxx_host_attrs,
152 .host_reset = qla4xxx_host_reset,
153 .vendor_id = SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_QLOGIC,
156 static struct iscsi_transport qla4xxx_iscsi_transport = {
157 .owner = THIS_MODULE,
159 .caps = CAP_TEXT_NEGO |
160 CAP_DATA_PATH_OFFLOAD | CAP_HDRDGST |
161 CAP_DATADGST | CAP_LOGIN_OFFLOAD |
163 .attr_is_visible = ql4_attr_is_visible,
164 .create_session = qla4xxx_session_create,
165 .destroy_session = qla4xxx_session_destroy,
166 .start_conn = qla4xxx_conn_start,
167 .create_conn = qla4xxx_conn_create,
168 .bind_conn = qla4xxx_conn_bind,
169 .stop_conn = iscsi_conn_stop,
170 .destroy_conn = qla4xxx_conn_destroy,
171 .set_param = iscsi_set_param,
172 .get_conn_param = qla4xxx_conn_get_param,
173 .get_session_param = iscsi_session_get_param,
174 .get_ep_param = qla4xxx_get_ep_param,
175 .ep_connect = qla4xxx_ep_connect,
176 .ep_poll = qla4xxx_ep_poll,
177 .ep_disconnect = qla4xxx_ep_disconnect,
178 .get_stats = qla4xxx_conn_get_stats,
179 .send_pdu = iscsi_conn_send_pdu,
180 .xmit_task = qla4xxx_task_xmit,
181 .cleanup_task = qla4xxx_task_cleanup,
182 .alloc_pdu = qla4xxx_alloc_pdu,
184 .get_host_param = qla4xxx_host_get_param,
185 .set_iface_param = qla4xxx_iface_set_param,
186 .get_iface_param = qla4xxx_get_iface_param,
187 .bsg_request = qla4xxx_bsg_request,
190 static struct scsi_transport_template *qla4xxx_scsi_transport;
192 static mode_t ql4_attr_is_visible(int param_type, int param)
194 switch (param_type) {
195 case ISCSI_HOST_PARAM:
197 case ISCSI_HOST_PARAM_HWADDRESS:
198 case ISCSI_HOST_PARAM_IPADDRESS:
199 case ISCSI_HOST_PARAM_INITIATOR_NAME:
206 case ISCSI_PARAM_PERSISTENT_ADDRESS:
207 case ISCSI_PARAM_PERSISTENT_PORT:
208 case ISCSI_PARAM_CONN_ADDRESS:
209 case ISCSI_PARAM_CONN_PORT:
210 case ISCSI_PARAM_TARGET_NAME:
211 case ISCSI_PARAM_TPGT:
212 case ISCSI_PARAM_TARGET_ALIAS:
213 case ISCSI_PARAM_MAX_BURST:
214 case ISCSI_PARAM_MAX_R2T:
215 case ISCSI_PARAM_FIRST_BURST:
216 case ISCSI_PARAM_MAX_RECV_DLENGTH:
217 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
218 case ISCSI_PARAM_IFACE_NAME:
223 case ISCSI_NET_PARAM:
225 case ISCSI_NET_PARAM_IPV4_ADDR:
226 case ISCSI_NET_PARAM_IPV4_SUBNET:
227 case ISCSI_NET_PARAM_IPV4_GW:
228 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
229 case ISCSI_NET_PARAM_IFACE_ENABLE:
230 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
231 case ISCSI_NET_PARAM_IPV6_ADDR:
232 case ISCSI_NET_PARAM_IPV6_ROUTER:
233 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
234 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
235 case ISCSI_NET_PARAM_VLAN_ID:
236 case ISCSI_NET_PARAM_VLAN_PRIORITY:
237 case ISCSI_NET_PARAM_VLAN_ENABLED:
238 case ISCSI_NET_PARAM_MTU:
239 case ISCSI_NET_PARAM_PORT:
249 static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
250 enum iscsi_param_type param_type,
251 int param, char *buf)
253 struct Scsi_Host *shost = iscsi_iface_to_shost(iface);
254 struct scsi_qla_host *ha = to_qla_host(shost);
257 if (param_type != ISCSI_NET_PARAM)
261 case ISCSI_NET_PARAM_IPV4_ADDR:
262 len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
264 case ISCSI_NET_PARAM_IPV4_SUBNET:
265 len = sprintf(buf, "%pI4\n", &ha->ip_config.subnet_mask);
267 case ISCSI_NET_PARAM_IPV4_GW:
268 len = sprintf(buf, "%pI4\n", &ha->ip_config.gateway);
270 case ISCSI_NET_PARAM_IFACE_ENABLE:
271 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
272 len = sprintf(buf, "%s\n",
273 (ha->ip_config.ipv4_options &
274 IPOPT_IPV4_PROTOCOL_ENABLE) ?
275 "enabled" : "disabled");
276 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
277 len = sprintf(buf, "%s\n",
278 (ha->ip_config.ipv6_options &
279 IPV6_OPT_IPV6_PROTOCOL_ENABLE) ?
280 "enabled" : "disabled");
282 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
283 len = sprintf(buf, "%s\n",
284 (ha->ip_config.tcp_options & TCPOPT_DHCP_ENABLE) ?
287 case ISCSI_NET_PARAM_IPV6_ADDR:
288 if (iface->iface_num == 0)
289 len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr0);
290 if (iface->iface_num == 1)
291 len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr1);
293 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
294 len = sprintf(buf, "%pI6\n",
295 &ha->ip_config.ipv6_link_local_addr);
297 case ISCSI_NET_PARAM_IPV6_ROUTER:
298 len = sprintf(buf, "%pI6\n",
299 &ha->ip_config.ipv6_default_router_addr);
301 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
302 len = sprintf(buf, "%s\n",
303 (ha->ip_config.ipv6_addl_options &
304 IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) ?
307 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
308 len = sprintf(buf, "%s\n",
309 (ha->ip_config.ipv6_addl_options &
310 IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR) ?
313 case ISCSI_NET_PARAM_VLAN_ID:
314 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
315 len = sprintf(buf, "%d\n",
316 (ha->ip_config.ipv4_vlan_tag &
318 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
319 len = sprintf(buf, "%d\n",
320 (ha->ip_config.ipv6_vlan_tag &
323 case ISCSI_NET_PARAM_VLAN_PRIORITY:
324 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
325 len = sprintf(buf, "%d\n",
326 ((ha->ip_config.ipv4_vlan_tag >> 13) &
327 ISCSI_MAX_VLAN_PRIORITY));
328 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
329 len = sprintf(buf, "%d\n",
330 ((ha->ip_config.ipv6_vlan_tag >> 13) &
331 ISCSI_MAX_VLAN_PRIORITY));
333 case ISCSI_NET_PARAM_VLAN_ENABLED:
334 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
335 len = sprintf(buf, "%s\n",
336 (ha->ip_config.ipv4_options &
337 IPOPT_VLAN_TAGGING_ENABLE) ?
338 "enabled" : "disabled");
339 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
340 len = sprintf(buf, "%s\n",
341 (ha->ip_config.ipv6_options &
342 IPV6_OPT_VLAN_TAGGING_ENABLE) ?
343 "enabled" : "disabled");
345 case ISCSI_NET_PARAM_MTU:
346 len = sprintf(buf, "%d\n", ha->ip_config.eth_mtu_size);
348 case ISCSI_NET_PARAM_PORT:
349 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
350 len = sprintf(buf, "%d\n", ha->ip_config.ipv4_port);
351 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
352 len = sprintf(buf, "%d\n", ha->ip_config.ipv6_port);
361 static struct iscsi_endpoint *
362 qla4xxx_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
366 struct iscsi_endpoint *ep;
367 struct qla_endpoint *qla_ep;
368 struct scsi_qla_host *ha;
369 struct sockaddr_in *addr;
370 struct sockaddr_in6 *addr6;
372 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
375 printk(KERN_ERR "%s: shost is NULL\n",
380 ha = iscsi_host_priv(shost);
382 ep = iscsi_create_endpoint(sizeof(struct qla_endpoint));
388 qla_ep = ep->dd_data;
389 memset(qla_ep, 0, sizeof(struct qla_endpoint));
390 if (dst_addr->sa_family == AF_INET) {
391 memcpy(&qla_ep->dst_addr, dst_addr, sizeof(struct sockaddr_in));
392 addr = (struct sockaddr_in *)&qla_ep->dst_addr;
393 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI4\n", __func__,
394 (char *)&addr->sin_addr));
395 } else if (dst_addr->sa_family == AF_INET6) {
396 memcpy(&qla_ep->dst_addr, dst_addr,
397 sizeof(struct sockaddr_in6));
398 addr6 = (struct sockaddr_in6 *)&qla_ep->dst_addr;
399 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI6\n", __func__,
400 (char *)&addr6->sin6_addr));
403 qla_ep->host = shost;
408 static int qla4xxx_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
410 struct qla_endpoint *qla_ep;
411 struct scsi_qla_host *ha;
414 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
415 qla_ep = ep->dd_data;
416 ha = to_qla_host(qla_ep->host);
424 static void qla4xxx_ep_disconnect(struct iscsi_endpoint *ep)
426 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
427 iscsi_destroy_endpoint(ep);
430 static int qla4xxx_get_ep_param(struct iscsi_endpoint *ep,
431 enum iscsi_param param,
434 struct qla_endpoint *qla_ep = ep->dd_data;
435 struct sockaddr *dst_addr;
437 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
440 case ISCSI_PARAM_CONN_PORT:
441 case ISCSI_PARAM_CONN_ADDRESS:
445 dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
449 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
450 &qla_ep->dst_addr, param, buf);
456 static void qla4xxx_conn_get_stats(struct iscsi_cls_conn *cls_conn,
457 struct iscsi_stats *stats)
459 struct iscsi_session *sess;
460 struct iscsi_cls_session *cls_sess;
461 struct ddb_entry *ddb_entry;
462 struct scsi_qla_host *ha;
463 struct ql_iscsi_stats *ql_iscsi_stats;
466 dma_addr_t iscsi_stats_dma;
468 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
470 cls_sess = iscsi_conn_to_session(cls_conn);
471 sess = cls_sess->dd_data;
472 ddb_entry = sess->dd_data;
475 stats_size = PAGE_ALIGN(sizeof(struct ql_iscsi_stats));
476 /* Allocate memory */
477 ql_iscsi_stats = dma_alloc_coherent(&ha->pdev->dev, stats_size,
478 &iscsi_stats_dma, GFP_KERNEL);
479 if (!ql_iscsi_stats) {
480 ql4_printk(KERN_ERR, ha,
481 "Unable to allocate memory for iscsi stats\n");
485 ret = qla4xxx_get_mgmt_data(ha, ddb_entry->fw_ddb_index, stats_size,
487 if (ret != QLA_SUCCESS) {
488 ql4_printk(KERN_ERR, ha,
489 "Unable to retreive iscsi stats\n");
494 stats->txdata_octets = le64_to_cpu(ql_iscsi_stats->tx_data_octets);
495 stats->rxdata_octets = le64_to_cpu(ql_iscsi_stats->rx_data_octets);
497 stats->noptx_pdus = le32_to_cpu(ql_iscsi_stats->tx_nopout_pdus);
498 stats->scsicmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_cmd_pdus);
499 stats->tmfcmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_tmf_cmd_pdus);
500 stats->login_pdus = le32_to_cpu(ql_iscsi_stats->tx_login_cmd_pdus);
501 stats->text_pdus = le32_to_cpu(ql_iscsi_stats->tx_text_cmd_pdus);
502 stats->dataout_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_write_pdus);
503 stats->logout_pdus = le32_to_cpu(ql_iscsi_stats->tx_logout_cmd_pdus);
504 stats->snack_pdus = le32_to_cpu(ql_iscsi_stats->tx_snack_req_pdus);
506 stats->noprx_pdus = le32_to_cpu(ql_iscsi_stats->rx_nopin_pdus);
507 stats->scsirsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_resp_pdus);
508 stats->tmfrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_tmf_resp_pdus);
509 stats->textrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_text_resp_pdus);
510 stats->datain_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_read_pdus);
511 stats->logoutrsp_pdus =
512 le32_to_cpu(ql_iscsi_stats->rx_logout_resp_pdus);
513 stats->r2t_pdus = le32_to_cpu(ql_iscsi_stats->rx_r2t_pdus);
514 stats->async_pdus = le32_to_cpu(ql_iscsi_stats->rx_async_pdus);
515 stats->rjt_pdus = le32_to_cpu(ql_iscsi_stats->rx_reject_pdus);
518 dma_free_coherent(&ha->pdev->dev, stats_size, ql_iscsi_stats,
524 static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
526 struct iscsi_cls_session *session;
527 struct iscsi_session *sess;
529 enum blk_eh_timer_return ret = BLK_EH_NOT_HANDLED;
531 session = starget_to_session(scsi_target(sc->device));
532 sess = session->dd_data;
534 spin_lock_irqsave(&session->lock, flags);
535 if (session->state == ISCSI_SESSION_FAILED)
536 ret = BLK_EH_RESET_TIMER;
537 spin_unlock_irqrestore(&session->lock, flags);
542 static int qla4xxx_host_get_param(struct Scsi_Host *shost,
543 enum iscsi_host_param param, char *buf)
545 struct scsi_qla_host *ha = to_qla_host(shost);
549 case ISCSI_HOST_PARAM_HWADDRESS:
550 len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
552 case ISCSI_HOST_PARAM_IPADDRESS:
553 len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
555 case ISCSI_HOST_PARAM_INITIATOR_NAME:
556 len = sprintf(buf, "%s\n", ha->name_string);
565 static void qla4xxx_create_ipv4_iface(struct scsi_qla_host *ha)
571 ha->iface_ipv4 = iscsi_create_iface(ha->host,
572 &qla4xxx_iscsi_transport,
573 ISCSI_IFACE_TYPE_IPV4, 0, 0);
575 ql4_printk(KERN_ERR, ha, "Could not create IPv4 iSCSI "
579 static void qla4xxx_create_ipv6_iface(struct scsi_qla_host *ha)
581 if (!ha->iface_ipv6_0)
583 ha->iface_ipv6_0 = iscsi_create_iface(ha->host,
584 &qla4xxx_iscsi_transport,
585 ISCSI_IFACE_TYPE_IPV6, 0,
587 if (!ha->iface_ipv6_0)
588 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
591 if (!ha->iface_ipv6_1)
593 ha->iface_ipv6_1 = iscsi_create_iface(ha->host,
594 &qla4xxx_iscsi_transport,
595 ISCSI_IFACE_TYPE_IPV6, 1,
597 if (!ha->iface_ipv6_1)
598 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
602 static void qla4xxx_create_ifaces(struct scsi_qla_host *ha)
604 if (ha->ip_config.ipv4_options & IPOPT_IPV4_PROTOCOL_ENABLE)
605 qla4xxx_create_ipv4_iface(ha);
607 if (ha->ip_config.ipv6_options & IPV6_OPT_IPV6_PROTOCOL_ENABLE)
608 qla4xxx_create_ipv6_iface(ha);
611 static void qla4xxx_destroy_ipv4_iface(struct scsi_qla_host *ha)
613 if (ha->iface_ipv4) {
614 iscsi_destroy_iface(ha->iface_ipv4);
615 ha->iface_ipv4 = NULL;
619 static void qla4xxx_destroy_ipv6_iface(struct scsi_qla_host *ha)
621 if (ha->iface_ipv6_0) {
622 iscsi_destroy_iface(ha->iface_ipv6_0);
623 ha->iface_ipv6_0 = NULL;
625 if (ha->iface_ipv6_1) {
626 iscsi_destroy_iface(ha->iface_ipv6_1);
627 ha->iface_ipv6_1 = NULL;
631 static void qla4xxx_destroy_ifaces(struct scsi_qla_host *ha)
633 qla4xxx_destroy_ipv4_iface(ha);
634 qla4xxx_destroy_ipv6_iface(ha);
637 static void qla4xxx_set_ipv6(struct scsi_qla_host *ha,
638 struct iscsi_iface_param_info *iface_param,
639 struct addr_ctrl_blk *init_fw_cb)
642 * iface_num 0 is valid for IPv6 Addr, linklocal, router, autocfg.
643 * iface_num 1 is valid only for IPv6 Addr.
645 switch (iface_param->param) {
646 case ISCSI_NET_PARAM_IPV6_ADDR:
647 if (iface_param->iface_num & 0x1)
649 memcpy(init_fw_cb->ipv6_addr1, iface_param->value,
650 sizeof(init_fw_cb->ipv6_addr1));
653 memcpy(init_fw_cb->ipv6_addr0, iface_param->value,
654 sizeof(init_fw_cb->ipv6_addr0));
656 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
657 if (iface_param->iface_num & 0x1)
659 memcpy(init_fw_cb->ipv6_if_id, &iface_param->value[8],
660 sizeof(init_fw_cb->ipv6_if_id));
662 case ISCSI_NET_PARAM_IPV6_ROUTER:
663 if (iface_param->iface_num & 0x1)
665 memcpy(init_fw_cb->ipv6_dflt_rtr_addr, iface_param->value,
666 sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
668 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
669 /* Autocfg applies to even interface */
670 if (iface_param->iface_num & 0x1)
673 if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_DISABLE)
674 init_fw_cb->ipv6_addtl_opts &=
676 ~IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
677 else if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_ND_ENABLE)
678 init_fw_cb->ipv6_addtl_opts |=
680 IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
682 ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
685 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
686 /* Autocfg applies to even interface */
687 if (iface_param->iface_num & 0x1)
690 if (iface_param->value[0] ==
691 ISCSI_IPV6_LINKLOCAL_AUTOCFG_ENABLE)
692 init_fw_cb->ipv6_addtl_opts |= cpu_to_le16(
693 IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
694 else if (iface_param->value[0] ==
695 ISCSI_IPV6_LINKLOCAL_AUTOCFG_DISABLE)
696 init_fw_cb->ipv6_addtl_opts &= cpu_to_le16(
697 ~IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
699 ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
700 "IPv6 linklocal addr\n");
702 case ISCSI_NET_PARAM_IPV6_ROUTER_AUTOCFG:
703 /* Autocfg applies to even interface */
704 if (iface_param->iface_num & 0x1)
707 if (iface_param->value[0] == ISCSI_IPV6_ROUTER_AUTOCFG_ENABLE)
708 memset(init_fw_cb->ipv6_dflt_rtr_addr, 0,
709 sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
711 case ISCSI_NET_PARAM_IFACE_ENABLE:
712 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
713 init_fw_cb->ipv6_opts |=
714 cpu_to_le16(IPV6_OPT_IPV6_PROTOCOL_ENABLE);
715 qla4xxx_create_ipv6_iface(ha);
717 init_fw_cb->ipv6_opts &=
718 cpu_to_le16(~IPV6_OPT_IPV6_PROTOCOL_ENABLE &
720 qla4xxx_destroy_ipv6_iface(ha);
723 case ISCSI_NET_PARAM_VLAN_TAG:
724 if (iface_param->len != sizeof(init_fw_cb->ipv6_vlan_tag))
726 init_fw_cb->ipv6_vlan_tag =
727 cpu_to_be16(*(uint16_t *)iface_param->value);
729 case ISCSI_NET_PARAM_VLAN_ENABLED:
730 if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
731 init_fw_cb->ipv6_opts |=
732 cpu_to_le16(IPV6_OPT_VLAN_TAGGING_ENABLE);
734 init_fw_cb->ipv6_opts &=
735 cpu_to_le16(~IPV6_OPT_VLAN_TAGGING_ENABLE);
737 case ISCSI_NET_PARAM_MTU:
738 init_fw_cb->eth_mtu_size =
739 cpu_to_le16(*(uint16_t *)iface_param->value);
741 case ISCSI_NET_PARAM_PORT:
742 /* Autocfg applies to even interface */
743 if (iface_param->iface_num & 0x1)
746 init_fw_cb->ipv6_port =
747 cpu_to_le16(*(uint16_t *)iface_param->value);
750 ql4_printk(KERN_ERR, ha, "Unknown IPv6 param = %d\n",
756 static void qla4xxx_set_ipv4(struct scsi_qla_host *ha,
757 struct iscsi_iface_param_info *iface_param,
758 struct addr_ctrl_blk *init_fw_cb)
760 switch (iface_param->param) {
761 case ISCSI_NET_PARAM_IPV4_ADDR:
762 memcpy(init_fw_cb->ipv4_addr, iface_param->value,
763 sizeof(init_fw_cb->ipv4_addr));
765 case ISCSI_NET_PARAM_IPV4_SUBNET:
766 memcpy(init_fw_cb->ipv4_subnet, iface_param->value,
767 sizeof(init_fw_cb->ipv4_subnet));
769 case ISCSI_NET_PARAM_IPV4_GW:
770 memcpy(init_fw_cb->ipv4_gw_addr, iface_param->value,
771 sizeof(init_fw_cb->ipv4_gw_addr));
773 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
774 if (iface_param->value[0] == ISCSI_BOOTPROTO_DHCP)
775 init_fw_cb->ipv4_tcp_opts |=
776 cpu_to_le16(TCPOPT_DHCP_ENABLE);
777 else if (iface_param->value[0] == ISCSI_BOOTPROTO_STATIC)
778 init_fw_cb->ipv4_tcp_opts &=
779 cpu_to_le16(~TCPOPT_DHCP_ENABLE);
781 ql4_printk(KERN_ERR, ha, "Invalid IPv4 bootproto\n");
783 case ISCSI_NET_PARAM_IFACE_ENABLE:
784 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
785 init_fw_cb->ipv4_ip_opts |=
786 cpu_to_le16(IPOPT_IPV4_PROTOCOL_ENABLE);
787 qla4xxx_create_ipv4_iface(ha);
789 init_fw_cb->ipv4_ip_opts &=
790 cpu_to_le16(~IPOPT_IPV4_PROTOCOL_ENABLE &
792 qla4xxx_destroy_ipv4_iface(ha);
795 case ISCSI_NET_PARAM_VLAN_TAG:
796 if (iface_param->len != sizeof(init_fw_cb->ipv4_vlan_tag))
798 init_fw_cb->ipv4_vlan_tag =
799 cpu_to_be16(*(uint16_t *)iface_param->value);
801 case ISCSI_NET_PARAM_VLAN_ENABLED:
802 if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
803 init_fw_cb->ipv4_ip_opts |=
804 cpu_to_le16(IPOPT_VLAN_TAGGING_ENABLE);
806 init_fw_cb->ipv4_ip_opts &=
807 cpu_to_le16(~IPOPT_VLAN_TAGGING_ENABLE);
809 case ISCSI_NET_PARAM_MTU:
810 init_fw_cb->eth_mtu_size =
811 cpu_to_le16(*(uint16_t *)iface_param->value);
813 case ISCSI_NET_PARAM_PORT:
814 init_fw_cb->ipv4_port =
815 cpu_to_le16(*(uint16_t *)iface_param->value);
818 ql4_printk(KERN_ERR, ha, "Unknown IPv4 param = %d\n",
825 qla4xxx_initcb_to_acb(struct addr_ctrl_blk *init_fw_cb)
827 struct addr_ctrl_blk_def *acb;
828 acb = (struct addr_ctrl_blk_def *)init_fw_cb;
829 memset(acb->reserved1, 0, sizeof(acb->reserved1));
830 memset(acb->reserved2, 0, sizeof(acb->reserved2));
831 memset(acb->reserved3, 0, sizeof(acb->reserved3));
832 memset(acb->reserved4, 0, sizeof(acb->reserved4));
833 memset(acb->reserved5, 0, sizeof(acb->reserved5));
834 memset(acb->reserved6, 0, sizeof(acb->reserved6));
835 memset(acb->reserved7, 0, sizeof(acb->reserved7));
836 memset(acb->reserved8, 0, sizeof(acb->reserved8));
837 memset(acb->reserved9, 0, sizeof(acb->reserved9));
838 memset(acb->reserved10, 0, sizeof(acb->reserved10));
839 memset(acb->reserved11, 0, sizeof(acb->reserved11));
840 memset(acb->reserved12, 0, sizeof(acb->reserved12));
841 memset(acb->reserved13, 0, sizeof(acb->reserved13));
842 memset(acb->reserved14, 0, sizeof(acb->reserved14));
843 memset(acb->reserved15, 0, sizeof(acb->reserved15));
847 qla4xxx_iface_set_param(struct Scsi_Host *shost, void *data, uint32_t len)
849 struct scsi_qla_host *ha = to_qla_host(shost);
851 struct iscsi_iface_param_info *iface_param = NULL;
852 struct addr_ctrl_blk *init_fw_cb = NULL;
853 dma_addr_t init_fw_cb_dma;
854 uint32_t mbox_cmd[MBOX_REG_COUNT];
855 uint32_t mbox_sts[MBOX_REG_COUNT];
859 init_fw_cb = dma_alloc_coherent(&ha->pdev->dev,
860 sizeof(struct addr_ctrl_blk),
861 &init_fw_cb_dma, GFP_KERNEL);
863 ql4_printk(KERN_ERR, ha, "%s: Unable to alloc init_cb\n",
868 memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
869 memset(&mbox_cmd, 0, sizeof(mbox_cmd));
870 memset(&mbox_sts, 0, sizeof(mbox_sts));
872 if (qla4xxx_get_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma)) {
873 ql4_printk(KERN_ERR, ha, "%s: get ifcb failed\n", __func__);
875 goto exit_init_fw_cb;
878 nla_for_each_attr(attr, data, len, rem) {
879 iface_param = nla_data(attr);
881 if (iface_param->param_type != ISCSI_NET_PARAM)
884 switch (iface_param->iface_type) {
885 case ISCSI_IFACE_TYPE_IPV4:
886 switch (iface_param->iface_num) {
888 qla4xxx_set_ipv4(ha, iface_param, init_fw_cb);
891 /* Cannot have more than one IPv4 interface */
892 ql4_printk(KERN_ERR, ha, "Invalid IPv4 iface "
894 iface_param->iface_num);
898 case ISCSI_IFACE_TYPE_IPV6:
899 switch (iface_param->iface_num) {
902 qla4xxx_set_ipv6(ha, iface_param, init_fw_cb);
905 /* Cannot have more than two IPv6 interface */
906 ql4_printk(KERN_ERR, ha, "Invalid IPv6 iface "
908 iface_param->iface_num);
913 ql4_printk(KERN_ERR, ha, "Invalid iface type\n");
918 init_fw_cb->cookie = cpu_to_le32(0x11BEAD5A);
920 rval = qla4xxx_set_flash(ha, init_fw_cb_dma, FLASH_SEGMENT_IFCB,
921 sizeof(struct addr_ctrl_blk),
922 FLASH_OPT_RMW_COMMIT);
923 if (rval != QLA_SUCCESS) {
924 ql4_printk(KERN_ERR, ha, "%s: set flash mbx failed\n",
927 goto exit_init_fw_cb;
930 qla4xxx_disable_acb(ha);
932 qla4xxx_initcb_to_acb(init_fw_cb);
934 rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma);
935 if (rval != QLA_SUCCESS) {
936 ql4_printk(KERN_ERR, ha, "%s: set acb mbx failed\n",
939 goto exit_init_fw_cb;
942 memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
943 qla4xxx_update_local_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb,
947 dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk),
948 init_fw_cb, init_fw_cb_dma);
953 static int qla4xxx_conn_get_param(struct iscsi_cls_conn *cls_conn,
954 enum iscsi_param param, char *buf)
956 struct iscsi_conn *conn;
957 struct qla_conn *qla_conn;
958 struct sockaddr *dst_addr;
961 conn = cls_conn->dd_data;
962 qla_conn = conn->dd_data;
963 dst_addr = &qla_conn->qla_ep->dst_addr;
966 case ISCSI_PARAM_CONN_PORT:
967 case ISCSI_PARAM_CONN_ADDRESS:
968 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
969 dst_addr, param, buf);
971 return iscsi_conn_get_param(cls_conn, param, buf);
978 static struct iscsi_cls_session *
979 qla4xxx_session_create(struct iscsi_endpoint *ep,
980 uint16_t cmds_max, uint16_t qdepth,
981 uint32_t initial_cmdsn)
983 struct iscsi_cls_session *cls_sess;
984 struct scsi_qla_host *ha;
985 struct qla_endpoint *qla_ep;
986 struct ddb_entry *ddb_entry;
988 uint32_t mbx_sts = 0;
989 struct iscsi_session *sess;
990 struct sockaddr *dst_addr;
993 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
995 printk(KERN_ERR "qla4xxx: missing ep.\n");
999 qla_ep = ep->dd_data;
1000 dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
1001 ha = to_qla_host(qla_ep->host);
1004 ddb_index = find_first_zero_bit(ha->ddb_idx_map, MAX_DDB_ENTRIES);
1006 if (ddb_index >= MAX_DDB_ENTRIES) {
1007 DEBUG2(ql4_printk(KERN_INFO, ha,
1008 "Free DDB index not available\n"));
1012 if (test_and_set_bit(ddb_index, ha->ddb_idx_map))
1015 DEBUG2(ql4_printk(KERN_INFO, ha,
1016 "Found a free DDB index at %d\n", ddb_index));
1017 ret = qla4xxx_req_ddb_entry(ha, ddb_index, &mbx_sts);
1018 if (ret == QLA_ERROR) {
1019 if (mbx_sts == MBOX_STS_COMMAND_ERROR) {
1020 ql4_printk(KERN_INFO, ha,
1021 "DDB index = %d not available trying next\n",
1025 DEBUG2(ql4_printk(KERN_INFO, ha,
1026 "Free FW DDB not available\n"));
1030 cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport, qla_ep->host,
1031 cmds_max, sizeof(struct ddb_entry),
1032 sizeof(struct ql4_task_data),
1033 initial_cmdsn, ddb_index);
1037 sess = cls_sess->dd_data;
1038 ddb_entry = sess->dd_data;
1039 ddb_entry->fw_ddb_index = ddb_index;
1040 ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
1042 ddb_entry->sess = cls_sess;
1043 cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
1044 ha->fw_ddb_index_map[ddb_entry->fw_ddb_index] = ddb_entry;
1050 static void qla4xxx_session_destroy(struct iscsi_cls_session *cls_sess)
1052 struct iscsi_session *sess;
1053 struct ddb_entry *ddb_entry;
1054 struct scsi_qla_host *ha;
1055 unsigned long flags;
1057 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1058 sess = cls_sess->dd_data;
1059 ddb_entry = sess->dd_data;
1062 qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
1064 spin_lock_irqsave(&ha->hardware_lock, flags);
1065 qla4xxx_free_ddb(ha, ddb_entry);
1066 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1067 iscsi_session_teardown(cls_sess);
1070 static struct iscsi_cls_conn *
1071 qla4xxx_conn_create(struct iscsi_cls_session *cls_sess, uint32_t conn_idx)
1073 struct iscsi_cls_conn *cls_conn;
1074 struct iscsi_session *sess;
1075 struct ddb_entry *ddb_entry;
1077 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1078 cls_conn = iscsi_conn_setup(cls_sess, sizeof(struct qla_conn),
1080 sess = cls_sess->dd_data;
1081 ddb_entry = sess->dd_data;
1082 ddb_entry->conn = cls_conn;
1087 static int qla4xxx_conn_bind(struct iscsi_cls_session *cls_session,
1088 struct iscsi_cls_conn *cls_conn,
1089 uint64_t transport_fd, int is_leading)
1091 struct iscsi_conn *conn;
1092 struct qla_conn *qla_conn;
1093 struct iscsi_endpoint *ep;
1095 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1097 if (iscsi_conn_bind(cls_session, cls_conn, is_leading))
1099 ep = iscsi_lookup_endpoint(transport_fd);
1100 conn = cls_conn->dd_data;
1101 qla_conn = conn->dd_data;
1102 qla_conn->qla_ep = ep->dd_data;
1106 static int qla4xxx_conn_start(struct iscsi_cls_conn *cls_conn)
1108 struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
1109 struct iscsi_session *sess;
1110 struct ddb_entry *ddb_entry;
1111 struct scsi_qla_host *ha;
1112 struct dev_db_entry *fw_ddb_entry;
1113 dma_addr_t fw_ddb_entry_dma;
1114 uint32_t mbx_sts = 0;
1116 int status = QLA_SUCCESS;
1118 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1119 sess = cls_sess->dd_data;
1120 ddb_entry = sess->dd_data;
1123 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1124 &fw_ddb_entry_dma, GFP_KERNEL);
1125 if (!fw_ddb_entry) {
1126 ql4_printk(KERN_ERR, ha,
1127 "%s: Unable to allocate dma buffer\n", __func__);
1131 ret = qla4xxx_set_param_ddbentry(ha, ddb_entry, cls_conn, &mbx_sts);
1133 /* If iscsid is stopped and started then no need to do
1134 * set param again since ddb state will be already
1135 * active and FW does not allow set ddb to an
1139 if (ddb_entry->fw_ddb_device_state ==
1140 DDB_DS_SESSION_ACTIVE) {
1141 iscsi_conn_start(ddb_entry->conn);
1142 iscsi_conn_login_event(ddb_entry->conn,
1143 ISCSI_CONN_STATE_LOGGED_IN);
1144 goto exit_set_param;
1147 ql4_printk(KERN_ERR, ha, "%s: Failed set param for index[%d]\n",
1148 __func__, ddb_entry->fw_ddb_index);
1149 goto exit_conn_start;
1152 status = qla4xxx_conn_open(ha, ddb_entry->fw_ddb_index);
1153 if (status == QLA_ERROR) {
1154 ql4_printk(KERN_ERR, ha, "%s: Login failed: %s\n", __func__,
1157 goto exit_conn_start;
1160 if (ddb_entry->fw_ddb_device_state == DDB_DS_NO_CONNECTION_ACTIVE)
1161 ddb_entry->fw_ddb_device_state = DDB_DS_LOGIN_IN_PROCESS;
1163 DEBUG2(printk(KERN_INFO "%s: DDB state [%d]\n", __func__,
1164 ddb_entry->fw_ddb_device_state));
1170 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1171 fw_ddb_entry, fw_ddb_entry_dma);
1175 static void qla4xxx_conn_destroy(struct iscsi_cls_conn *cls_conn)
1177 struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
1178 struct iscsi_session *sess;
1179 struct scsi_qla_host *ha;
1180 struct ddb_entry *ddb_entry;
1183 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1184 sess = cls_sess->dd_data;
1185 ddb_entry = sess->dd_data;
1188 options = LOGOUT_OPTION_CLOSE_SESSION;
1189 if (qla4xxx_session_logout_ddb(ha, ddb_entry, options) == QLA_ERROR)
1190 ql4_printk(KERN_ERR, ha, "%s: Logout failed\n", __func__);
1193 static void qla4xxx_task_work(struct work_struct *wdata)
1195 struct ql4_task_data *task_data;
1196 struct scsi_qla_host *ha;
1197 struct passthru_status *sts;
1198 struct iscsi_task *task;
1199 struct iscsi_hdr *hdr;
1202 struct iscsi_conn *conn;
1206 task_data = container_of(wdata, struct ql4_task_data, task_work);
1208 task = task_data->task;
1209 sts = &task_data->sts;
1210 hdr_len = sizeof(struct iscsi_hdr);
1212 DEBUG3(printk(KERN_INFO "Status returned\n"));
1213 DEBUG3(qla4xxx_dump_buffer(sts, 64));
1214 DEBUG3(printk(KERN_INFO "Response buffer"));
1215 DEBUG3(qla4xxx_dump_buffer(task_data->resp_buffer, 64));
1219 switch (sts->completionStatus) {
1220 case PASSTHRU_STATUS_COMPLETE:
1221 hdr = (struct iscsi_hdr *)task_data->resp_buffer;
1222 /* Assign back the itt in hdr, until we use the PREASSIGN_TAG */
1225 data = task_data->resp_buffer + hdr_len;
1226 data_len = task_data->resp_len - hdr_len;
1227 iscsi_complete_pdu(conn, hdr, data, data_len);
1230 ql4_printk(KERN_ERR, ha, "Passthru failed status = 0x%x\n",
1231 sts->completionStatus);
1237 static int qla4xxx_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
1239 struct ql4_task_data *task_data;
1240 struct iscsi_session *sess;
1241 struct ddb_entry *ddb_entry;
1242 struct scsi_qla_host *ha;
1245 sess = task->conn->session;
1246 ddb_entry = sess->dd_data;
1248 task_data = task->dd_data;
1249 memset(task_data, 0, sizeof(struct ql4_task_data));
1252 ql4_printk(KERN_INFO, ha,
1253 "%s: SCSI Commands not implemented\n", __func__);
1257 hdr_len = sizeof(struct iscsi_hdr);
1259 task_data->task = task;
1261 if (task->data_count) {
1262 task_data->data_dma = dma_map_single(&ha->pdev->dev, task->data,
1267 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
1268 __func__, task->conn->max_recv_dlength, hdr_len));
1270 task_data->resp_len = task->conn->max_recv_dlength + hdr_len;
1271 task_data->resp_buffer = dma_alloc_coherent(&ha->pdev->dev,
1272 task_data->resp_len,
1273 &task_data->resp_dma,
1275 if (!task_data->resp_buffer)
1276 goto exit_alloc_pdu;
1278 task_data->req_len = task->data_count + hdr_len;
1279 task_data->req_buffer = dma_alloc_coherent(&ha->pdev->dev,
1281 &task_data->req_dma,
1283 if (!task_data->req_buffer)
1284 goto exit_alloc_pdu;
1286 task->hdr = task_data->req_buffer;
1288 INIT_WORK(&task_data->task_work, qla4xxx_task_work);
1293 if (task_data->resp_buffer)
1294 dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
1295 task_data->resp_buffer, task_data->resp_dma);
1297 if (task_data->req_buffer)
1298 dma_free_coherent(&ha->pdev->dev, task_data->req_len,
1299 task_data->req_buffer, task_data->req_dma);
1303 static void qla4xxx_task_cleanup(struct iscsi_task *task)
1305 struct ql4_task_data *task_data;
1306 struct iscsi_session *sess;
1307 struct ddb_entry *ddb_entry;
1308 struct scsi_qla_host *ha;
1311 hdr_len = sizeof(struct iscsi_hdr);
1312 sess = task->conn->session;
1313 ddb_entry = sess->dd_data;
1315 task_data = task->dd_data;
1317 if (task->data_count) {
1318 dma_unmap_single(&ha->pdev->dev, task_data->data_dma,
1319 task->data_count, PCI_DMA_TODEVICE);
1322 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
1323 __func__, task->conn->max_recv_dlength, hdr_len));
1325 dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
1326 task_data->resp_buffer, task_data->resp_dma);
1327 dma_free_coherent(&ha->pdev->dev, task_data->req_len,
1328 task_data->req_buffer, task_data->req_dma);
1332 static int qla4xxx_task_xmit(struct iscsi_task *task)
1334 struct scsi_cmnd *sc = task->sc;
1335 struct iscsi_session *sess = task->conn->session;
1336 struct ddb_entry *ddb_entry = sess->dd_data;
1337 struct scsi_qla_host *ha = ddb_entry->ha;
1340 return qla4xxx_send_passthru0(task);
1342 ql4_printk(KERN_INFO, ha, "%s: scsi cmd xmit not implemented\n",
1347 void qla4xxx_update_session_conn_param(struct scsi_qla_host *ha,
1348 struct ddb_entry *ddb_entry)
1350 struct iscsi_cls_session *cls_sess;
1351 struct iscsi_cls_conn *cls_conn;
1352 struct iscsi_session *sess;
1353 struct iscsi_conn *conn;
1355 dma_addr_t fw_ddb_entry_dma;
1356 struct dev_db_entry *fw_ddb_entry;
1358 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1359 &fw_ddb_entry_dma, GFP_KERNEL);
1360 if (!fw_ddb_entry) {
1361 ql4_printk(KERN_ERR, ha,
1362 "%s: Unable to allocate dma buffer\n", __func__);
1366 if (qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index, fw_ddb_entry,
1367 fw_ddb_entry_dma, NULL, NULL, &ddb_state,
1368 NULL, NULL, NULL) == QLA_ERROR) {
1369 DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: failed "
1370 "get_ddb_entry for fw_ddb_index %d\n",
1371 ha->host_no, __func__,
1372 ddb_entry->fw_ddb_index));
1376 cls_sess = ddb_entry->sess;
1377 sess = cls_sess->dd_data;
1379 cls_conn = ddb_entry->conn;
1380 conn = cls_conn->dd_data;
1383 conn->max_recv_dlength = BYTE_UNITS *
1384 le16_to_cpu(fw_ddb_entry->iscsi_max_rcv_data_seg_len);
1386 conn->max_xmit_dlength = BYTE_UNITS *
1387 le16_to_cpu(fw_ddb_entry->iscsi_max_snd_data_seg_len);
1389 sess->initial_r2t_en =
1390 (BIT_10 & le16_to_cpu(fw_ddb_entry->iscsi_options));
1392 sess->max_r2t = le16_to_cpu(fw_ddb_entry->iscsi_max_outsnd_r2t);
1394 sess->imm_data_en = (BIT_11 & le16_to_cpu(fw_ddb_entry->iscsi_options));
1396 sess->first_burst = BYTE_UNITS *
1397 le16_to_cpu(fw_ddb_entry->iscsi_first_burst_len);
1399 sess->max_burst = BYTE_UNITS *
1400 le16_to_cpu(fw_ddb_entry->iscsi_max_burst_len);
1402 sess->time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
1404 sess->time2retain = le16_to_cpu(fw_ddb_entry->iscsi_def_time2retain);
1406 sess->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
1408 memcpy(sess->initiatorname, ha->name_string,
1409 min(sizeof(ha->name_string), sizeof(sess->initiatorname)));
1416 static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
1417 unsigned long interval)
1419 DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
1420 __func__, ha->host->host_no));
1421 init_timer(&ha->timer);
1422 ha->timer.expires = jiffies + interval * HZ;
1423 ha->timer.data = (unsigned long)ha;
1424 ha->timer.function = (void (*)(unsigned long))func;
1425 add_timer(&ha->timer);
1426 ha->timer_active = 1;
1429 static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
1431 del_timer_sync(&ha->timer);
1432 ha->timer_active = 0;
1436 * qla4xxx_mark_device_missing - blocks the session
1437 * @cls_session: Pointer to the session to be blocked
1438 * @ddb_entry: Pointer to device database entry
1440 * This routine marks a device missing and close connection.
1442 void qla4xxx_mark_device_missing(struct iscsi_cls_session *cls_session)
1444 iscsi_block_session(cls_session);
1448 * qla4xxx_mark_all_devices_missing - mark all devices as missing.
1449 * @ha: Pointer to host adapter structure.
1451 * This routine marks a device missing and resets the relogin retry count.
1453 void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
1455 iscsi_host_for_each_session(ha->host, qla4xxx_mark_device_missing);
1458 static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
1459 struct ddb_entry *ddb_entry,
1460 struct scsi_cmnd *cmd)
1464 srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
1468 kref_init(&srb->srb_ref);
1470 srb->ddb = ddb_entry;
1473 CMD_SP(cmd) = (void *)srb;
1478 static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
1480 struct scsi_cmnd *cmd = srb->cmd;
1482 if (srb->flags & SRB_DMA_VALID) {
1483 scsi_dma_unmap(cmd);
1484 srb->flags &= ~SRB_DMA_VALID;
1489 void qla4xxx_srb_compl(struct kref *ref)
1491 struct srb *srb = container_of(ref, struct srb, srb_ref);
1492 struct scsi_cmnd *cmd = srb->cmd;
1493 struct scsi_qla_host *ha = srb->ha;
1495 qla4xxx_srb_free_dma(ha, srb);
1497 mempool_free(srb, ha->srb_mempool);
1499 cmd->scsi_done(cmd);
1503 * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
1505 * @cmd: Pointer to Linux's SCSI command structure
1508 * This routine is invoked by Linux to send a SCSI command to the driver.
1509 * The mid-level driver tries to ensure that queuecommand never gets
1510 * invoked concurrently with itself or the interrupt handler (although
1511 * the interrupt handler may call this routine as part of request-
1512 * completion handling). Unfortunely, it sometimes calls the scheduler
1513 * in interrupt context which is a big NO! NO!.
1515 static int qla4xxx_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
1517 struct scsi_qla_host *ha = to_qla_host(host);
1518 struct ddb_entry *ddb_entry = cmd->device->hostdata;
1519 struct iscsi_cls_session *sess = ddb_entry->sess;
1523 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1524 if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
1525 cmd->result = DID_NO_CONNECT << 16;
1527 cmd->result = DID_REQUEUE << 16;
1528 goto qc_fail_command;
1532 cmd->result = DID_IMM_RETRY << 16;
1533 goto qc_fail_command;
1536 rval = iscsi_session_chkready(sess);
1539 goto qc_fail_command;
1542 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1543 test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
1544 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1545 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
1546 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
1547 !test_bit(AF_ONLINE, &ha->flags) ||
1548 !test_bit(AF_LINK_UP, &ha->flags) ||
1549 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
1552 srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd);
1556 rval = qla4xxx_send_command_to_isp(ha, srb);
1557 if (rval != QLA_SUCCESS)
1558 goto qc_host_busy_free_sp;
1562 qc_host_busy_free_sp:
1563 qla4xxx_srb_free_dma(ha, srb);
1564 mempool_free(srb, ha->srb_mempool);
1567 return SCSI_MLQUEUE_HOST_BUSY;
1570 cmd->scsi_done(cmd);
1576 * qla4xxx_mem_free - frees memory allocated to adapter
1577 * @ha: Pointer to host adapter structure.
1579 * Frees memory previously allocated by qla4xxx_mem_alloc
1581 static void qla4xxx_mem_free(struct scsi_qla_host *ha)
1584 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
1590 ha->request_ring = NULL;
1591 ha->request_dma = 0;
1592 ha->response_ring = NULL;
1593 ha->response_dma = 0;
1594 ha->shadow_regs = NULL;
1595 ha->shadow_regs_dma = 0;
1597 /* Free srb pool. */
1598 if (ha->srb_mempool)
1599 mempool_destroy(ha->srb_mempool);
1601 ha->srb_mempool = NULL;
1603 if (ha->chap_dma_pool)
1604 dma_pool_destroy(ha->chap_dma_pool);
1607 vfree(ha->chap_list);
1608 ha->chap_list = NULL;
1610 /* release io space registers */
1611 if (is_qla8022(ha)) {
1614 (struct device_reg_82xx __iomem *)ha->nx_pcibase);
1617 pci_release_regions(ha->pdev);
1621 * qla4xxx_mem_alloc - allocates memory for use by adapter.
1622 * @ha: Pointer to host adapter structure
1624 * Allocates DMA memory for request and response queues. Also allocates memory
1627 static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
1629 unsigned long align;
1631 /* Allocate contiguous block of DMA memory for queues. */
1632 ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
1633 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
1634 sizeof(struct shadow_regs) +
1636 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
1637 ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
1638 &ha->queues_dma, GFP_KERNEL);
1639 if (ha->queues == NULL) {
1640 ql4_printk(KERN_WARNING, ha,
1641 "Memory Allocation failed - queues.\n");
1643 goto mem_alloc_error_exit;
1645 memset(ha->queues, 0, ha->queues_len);
1648 * As per RISC alignment requirements -- the bus-address must be a
1649 * multiple of the request-ring size (in bytes).
1652 if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
1653 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
1654 (MEM_ALIGN_VALUE - 1));
1656 /* Update request and response queue pointers. */
1657 ha->request_dma = ha->queues_dma + align;
1658 ha->request_ring = (struct queue_entry *) (ha->queues + align);
1659 ha->response_dma = ha->queues_dma + align +
1660 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
1661 ha->response_ring = (struct queue_entry *) (ha->queues + align +
1662 (REQUEST_QUEUE_DEPTH *
1664 ha->shadow_regs_dma = ha->queues_dma + align +
1665 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
1666 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
1667 ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
1668 (REQUEST_QUEUE_DEPTH *
1670 (RESPONSE_QUEUE_DEPTH *
1673 /* Allocate memory for srb pool. */
1674 ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
1675 mempool_free_slab, srb_cachep);
1676 if (ha->srb_mempool == NULL) {
1677 ql4_printk(KERN_WARNING, ha,
1678 "Memory Allocation failed - SRB Pool.\n");
1680 goto mem_alloc_error_exit;
1683 ha->chap_dma_pool = dma_pool_create("ql4_chap", &ha->pdev->dev,
1684 CHAP_DMA_BLOCK_SIZE, 8, 0);
1686 if (ha->chap_dma_pool == NULL) {
1687 ql4_printk(KERN_WARNING, ha,
1688 "%s: chap_dma_pool allocation failed..\n", __func__);
1689 goto mem_alloc_error_exit;
1694 mem_alloc_error_exit:
1695 qla4xxx_mem_free(ha);
1700 * qla4_8xxx_check_fw_alive - Check firmware health
1701 * @ha: Pointer to host adapter structure.
1703 * Context: Interrupt
1705 static void qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
1707 uint32_t fw_heartbeat_counter, halt_status;
1709 fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
1710 /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
1711 if (fw_heartbeat_counter == 0xffffffff) {
1712 DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
1713 "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
1714 ha->host_no, __func__));
1718 if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
1719 ha->seconds_since_last_heartbeat++;
1720 /* FW not alive after 2 seconds */
1721 if (ha->seconds_since_last_heartbeat == 2) {
1722 ha->seconds_since_last_heartbeat = 0;
1723 halt_status = qla4_8xxx_rd_32(ha,
1724 QLA82XX_PEG_HALT_STATUS1);
1726 ql4_printk(KERN_INFO, ha,
1727 "scsi(%ld): %s, Dumping hw/fw registers:\n "
1728 " PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2:"
1729 " 0x%x,\n PEG_NET_0_PC: 0x%x, PEG_NET_1_PC:"
1730 " 0x%x,\n PEG_NET_2_PC: 0x%x, PEG_NET_3_PC:"
1731 " 0x%x,\n PEG_NET_4_PC: 0x%x\n",
1732 ha->host_no, __func__, halt_status,
1734 QLA82XX_PEG_HALT_STATUS2),
1735 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_0 +
1737 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_1 +
1739 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_2 +
1741 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_3 +
1743 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_4 +
1746 /* Since we cannot change dev_state in interrupt
1747 * context, set appropriate DPC flag then wakeup
1749 if (halt_status & HALT_STATUS_UNRECOVERABLE)
1750 set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
1752 printk("scsi%ld: %s: detect abort needed!\n",
1753 ha->host_no, __func__);
1754 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1756 qla4xxx_wake_dpc(ha);
1757 qla4xxx_mailbox_premature_completion(ha);
1760 ha->seconds_since_last_heartbeat = 0;
1762 ha->fw_heartbeat_counter = fw_heartbeat_counter;
1766 * qla4_8xxx_watchdog - Poll dev state
1767 * @ha: Pointer to host adapter structure.
1769 * Context: Interrupt
1771 void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
1775 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
1777 /* don't poll if reset is going on */
1778 if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
1779 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1780 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags))) {
1781 if (dev_state == QLA82XX_DEV_NEED_RESET &&
1782 !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
1783 if (!ql4xdontresethba) {
1784 ql4_printk(KERN_INFO, ha, "%s: HW State: "
1785 "NEED RESET!\n", __func__);
1786 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1787 qla4xxx_wake_dpc(ha);
1788 qla4xxx_mailbox_premature_completion(ha);
1790 } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
1791 !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
1792 ql4_printk(KERN_INFO, ha, "%s: HW State: NEED QUIES!\n",
1794 set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
1795 qla4xxx_wake_dpc(ha);
1797 /* Check firmware health */
1798 qla4_8xxx_check_fw_alive(ha);
1804 * qla4xxx_timer - checks every second for work to do.
1805 * @ha: Pointer to host adapter structure.
1807 static void qla4xxx_timer(struct scsi_qla_host *ha)
1812 /* If we are in the middle of AER/EEH processing
1813 * skip any processing and reschedule the timer
1815 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1816 mod_timer(&ha->timer, jiffies + HZ);
1820 /* Hardware read to trigger an EEH error during mailbox waits. */
1821 if (!pci_channel_offline(ha->pdev))
1822 pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
1824 if (is_qla8022(ha)) {
1825 qla4_8xxx_watchdog(ha);
1828 if (!is_qla8022(ha)) {
1829 /* Check for heartbeat interval. */
1830 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
1831 ha->heartbeat_interval != 0) {
1832 ha->seconds_since_last_heartbeat++;
1833 if (ha->seconds_since_last_heartbeat >
1834 ha->heartbeat_interval + 2)
1835 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1839 /* Wakeup the dpc routine for this adapter, if needed. */
1841 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1842 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
1843 test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
1844 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
1845 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1846 test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
1847 test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
1848 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
1849 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
1850 test_bit(DPC_AEN, &ha->dpc_flags)) {
1851 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
1852 " - dpc flags = 0x%lx\n",
1853 ha->host_no, __func__, ha->dpc_flags));
1854 qla4xxx_wake_dpc(ha);
1857 /* Reschedule timer thread to call us back in one second */
1858 mod_timer(&ha->timer, jiffies + HZ);
1860 DEBUG2(ha->seconds_since_last_intr++);
1864 * qla4xxx_cmd_wait - waits for all outstanding commands to complete
1865 * @ha: Pointer to host adapter structure.
1867 * This routine stalls the driver until all outstanding commands are returned.
1868 * Caller must release the Hardware Lock prior to calling this routine.
1870 static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
1873 unsigned long flags;
1874 struct scsi_cmnd *cmd;
1876 unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
1878 DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
1879 "complete\n", WAIT_CMD_TOV));
1881 while (!time_after_eq(jiffies, wtime)) {
1882 spin_lock_irqsave(&ha->hardware_lock, flags);
1883 /* Find a command that hasn't completed. */
1884 for (index = 0; index < ha->host->can_queue; index++) {
1885 cmd = scsi_host_find_tag(ha->host, index);
1887 * We cannot just check if the index is valid,
1888 * becase if we are run from the scsi eh, then
1889 * the scsi/block layer is going to prevent
1890 * the tag from being released.
1892 if (cmd != NULL && CMD_SP(cmd))
1895 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1897 /* If No Commands are pending, wait is complete */
1898 if (index == ha->host->can_queue)
1903 /* If we timed out on waiting for commands to come back
1908 int qla4xxx_hw_reset(struct scsi_qla_host *ha)
1910 uint32_t ctrl_status;
1911 unsigned long flags = 0;
1913 DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
1915 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
1918 spin_lock_irqsave(&ha->hardware_lock, flags);
1921 * If the SCSI Reset Interrupt bit is set, clear it.
1922 * Otherwise, the Soft Reset won't work.
1924 ctrl_status = readw(&ha->reg->ctrl_status);
1925 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
1926 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
1928 /* Issue Soft Reset */
1929 writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
1930 readl(&ha->reg->ctrl_status);
1932 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1937 * qla4xxx_soft_reset - performs soft reset.
1938 * @ha: Pointer to host adapter structure.
1940 int qla4xxx_soft_reset(struct scsi_qla_host *ha)
1942 uint32_t max_wait_time;
1943 unsigned long flags = 0;
1945 uint32_t ctrl_status;
1947 status = qla4xxx_hw_reset(ha);
1948 if (status != QLA_SUCCESS)
1952 /* Wait until the Network Reset Intr bit is cleared */
1953 max_wait_time = RESET_INTR_TOV;
1955 spin_lock_irqsave(&ha->hardware_lock, flags);
1956 ctrl_status = readw(&ha->reg->ctrl_status);
1957 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1959 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
1963 } while ((--max_wait_time));
1965 if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
1966 DEBUG2(printk(KERN_WARNING
1967 "scsi%ld: Network Reset Intr not cleared by "
1968 "Network function, clearing it now!\n",
1970 spin_lock_irqsave(&ha->hardware_lock, flags);
1971 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
1972 readl(&ha->reg->ctrl_status);
1973 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1976 /* Wait until the firmware tells us the Soft Reset is done */
1977 max_wait_time = SOFT_RESET_TOV;
1979 spin_lock_irqsave(&ha->hardware_lock, flags);
1980 ctrl_status = readw(&ha->reg->ctrl_status);
1981 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1983 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
1984 status = QLA_SUCCESS;
1989 } while ((--max_wait_time));
1992 * Also, make sure that the SCSI Reset Interrupt bit has been cleared
1993 * after the soft reset has taken place.
1995 spin_lock_irqsave(&ha->hardware_lock, flags);
1996 ctrl_status = readw(&ha->reg->ctrl_status);
1997 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
1998 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
1999 readl(&ha->reg->ctrl_status);
2001 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2003 /* If soft reset fails then most probably the bios on other
2004 * function is also enabled.
2005 * Since the initialization is sequential the other fn
2006 * wont be able to acknowledge the soft reset.
2007 * Issue a force soft reset to workaround this scenario.
2009 if (max_wait_time == 0) {
2010 /* Issue Force Soft Reset */
2011 spin_lock_irqsave(&ha->hardware_lock, flags);
2012 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
2013 readl(&ha->reg->ctrl_status);
2014 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2015 /* Wait until the firmware tells us the Soft Reset is done */
2016 max_wait_time = SOFT_RESET_TOV;
2018 spin_lock_irqsave(&ha->hardware_lock, flags);
2019 ctrl_status = readw(&ha->reg->ctrl_status);
2020 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2022 if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
2023 status = QLA_SUCCESS;
2028 } while ((--max_wait_time));
2035 * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
2036 * @ha: Pointer to host adapter structure.
2037 * @res: returned scsi status
2039 * This routine is called just prior to a HARD RESET to return all
2040 * outstanding commands back to the Operating System.
2041 * Caller should make sure that the following locks are released
2042 * before this calling routine: Hardware lock, and io_request_lock.
2044 static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
2048 unsigned long flags;
2050 spin_lock_irqsave(&ha->hardware_lock, flags);
2051 for (i = 0; i < ha->host->can_queue; i++) {
2052 srb = qla4xxx_del_from_active_array(ha, i);
2054 srb->cmd->result = res;
2055 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
2058 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2061 void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
2063 clear_bit(AF_ONLINE, &ha->flags);
2065 /* Disable the board */
2066 ql4_printk(KERN_INFO, ha, "Disabling the board\n");
2068 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
2069 qla4xxx_mark_all_devices_missing(ha);
2070 clear_bit(AF_INIT_DONE, &ha->flags);
2073 static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session)
2075 struct iscsi_session *sess;
2076 struct ddb_entry *ddb_entry;
2078 sess = cls_session->dd_data;
2079 ddb_entry = sess->dd_data;
2080 ddb_entry->fw_ddb_device_state = DDB_DS_SESSION_FAILED;
2081 iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_CONN_FAILED);
2085 * qla4xxx_recover_adapter - recovers adapter after a fatal error
2086 * @ha: Pointer to host adapter structure.
2088 static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
2090 int status = QLA_ERROR;
2091 uint8_t reset_chip = 0;
2093 /* Stall incoming I/O until we are done */
2094 scsi_block_requests(ha->host);
2095 clear_bit(AF_ONLINE, &ha->flags);
2096 clear_bit(AF_LINK_UP, &ha->flags);
2098 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
2100 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2102 iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
2104 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
2107 /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
2108 * do not reset adapter, jump to initialize_adapter */
2109 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
2110 status = QLA_SUCCESS;
2111 goto recover_ha_init_adapter;
2114 /* For the ISP-82xx adapter, issue a stop_firmware if invoked
2115 * from eh_host_reset or ioctl module */
2116 if (is_qla8022(ha) && !reset_chip &&
2117 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
2119 DEBUG2(ql4_printk(KERN_INFO, ha,
2120 "scsi%ld: %s - Performing stop_firmware...\n",
2121 ha->host_no, __func__));
2122 status = ha->isp_ops->reset_firmware(ha);
2123 if (status == QLA_SUCCESS) {
2124 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
2125 qla4xxx_cmd_wait(ha);
2126 ha->isp_ops->disable_intrs(ha);
2127 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2128 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
2130 /* If the stop_firmware fails then
2131 * reset the entire chip */
2133 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2134 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2138 /* Issue full chip reset if recovering from a catastrophic error,
2139 * or if stop_firmware fails for ISP-82xx.
2140 * This is the default case for ISP-4xxx */
2141 if (!is_qla8022(ha) || reset_chip) {
2142 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
2143 qla4xxx_cmd_wait(ha);
2144 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2145 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
2146 DEBUG2(ql4_printk(KERN_INFO, ha,
2147 "scsi%ld: %s - Performing chip reset..\n",
2148 ha->host_no, __func__));
2149 status = ha->isp_ops->reset_chip(ha);
2152 /* Flush any pending ddb changed AENs */
2153 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2155 recover_ha_init_adapter:
2156 /* Upon successful firmware/chip reset, re-initialize the adapter */
2157 if (status == QLA_SUCCESS) {
2158 /* For ISP-4xxx, force function 1 to always initialize
2159 * before function 3 to prevent both funcions from
2160 * stepping on top of the other */
2161 if (!is_qla8022(ha) && (ha->mac_index == 3))
2164 /* NOTE: AF_ONLINE flag set upon successful completion of
2165 * qla4xxx_initialize_adapter */
2166 status = qla4xxx_initialize_adapter(ha);
2169 /* Retry failed adapter initialization, if necessary
2170 * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
2171 * case to prevent ping-pong resets between functions */
2172 if (!test_bit(AF_ONLINE, &ha->flags) &&
2173 !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
2174 /* Adapter initialization failed, see if we can retry
2176 * Since we don't want to block the DPC for too long
2177 * with multiple resets in the same thread,
2178 * utilize DPC to retry */
2179 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
2180 ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
2181 DEBUG2(printk("scsi%ld: recover adapter - retrying "
2182 "(%d) more times\n", ha->host_no,
2183 ha->retry_reset_ha_cnt));
2184 set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
2187 if (ha->retry_reset_ha_cnt > 0) {
2188 /* Schedule another Reset HA--DPC will retry */
2189 ha->retry_reset_ha_cnt--;
2190 DEBUG2(printk("scsi%ld: recover adapter - "
2191 "retry remaining %d\n",
2193 ha->retry_reset_ha_cnt));
2197 if (ha->retry_reset_ha_cnt == 0) {
2198 /* Recover adapter retries have been exhausted.
2200 DEBUG2(printk("scsi%ld: recover adapter "
2201 "failed - board disabled\n",
2203 qla4xxx_dead_adapter_cleanup(ha);
2204 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
2205 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
2206 clear_bit(DPC_RESET_HA_FW_CONTEXT,
2212 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
2213 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2214 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
2217 ha->adapter_error_count++;
2219 if (test_bit(AF_ONLINE, &ha->flags))
2220 ha->isp_ops->enable_intrs(ha);
2222 scsi_unblock_requests(ha->host);
2224 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2225 DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
2226 status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
2231 static void qla4xxx_relogin_devices(struct iscsi_cls_session *cls_session)
2233 struct iscsi_session *sess;
2234 struct ddb_entry *ddb_entry;
2235 struct scsi_qla_host *ha;
2237 sess = cls_session->dd_data;
2238 ddb_entry = sess->dd_data;
2240 if (!iscsi_is_session_online(cls_session)) {
2241 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
2242 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
2243 " unblock session\n", ha->host_no, __func__,
2244 ddb_entry->fw_ddb_index);
2245 iscsi_unblock_session(ddb_entry->sess);
2247 /* Trigger relogin */
2248 iscsi_session_failure(cls_session->dd_data,
2249 ISCSI_ERR_CONN_FAILED);
2254 static void qla4xxx_relogin_all_devices(struct scsi_qla_host *ha)
2256 iscsi_host_for_each_session(ha->host, qla4xxx_relogin_devices);
2259 void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
2262 queue_work(ha->dpc_thread, &ha->dpc_work);
2266 * qla4xxx_do_dpc - dpc routine
2267 * @data: in our case pointer to adapter structure
2269 * This routine is a task that is schedule by the interrupt handler
2270 * to perform the background processing for interrupts. We put it
2271 * on a task queue that is consumed whenever the scheduler runs; that's
2272 * so you can do anything (i.e. put the process to sleep etc). In fact,
2273 * the mid-level tries to sleep when it reaches the driver threshold
2274 * "host->can_queue". This can cause a panic if we were in our interrupt code.
2276 static void qla4xxx_do_dpc(struct work_struct *work)
2278 struct scsi_qla_host *ha =
2279 container_of(work, struct scsi_qla_host, dpc_work);
2280 int status = QLA_ERROR;
2282 DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
2283 "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
2284 ha->host_no, __func__, ha->flags, ha->dpc_flags))
2286 /* Initialization not yet finished. Don't do anything yet. */
2287 if (!test_bit(AF_INIT_DONE, &ha->flags))
2290 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
2291 DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
2292 ha->host_no, __func__, ha->flags));
2296 if (is_qla8022(ha)) {
2297 if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
2298 qla4_8xxx_idc_lock(ha);
2299 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2300 QLA82XX_DEV_FAILED);
2301 qla4_8xxx_idc_unlock(ha);
2302 ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
2303 qla4_8xxx_device_state_handler(ha);
2305 if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
2306 qla4_8xxx_need_qsnt_handler(ha);
2310 if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
2311 (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
2312 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
2313 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
2314 if (ql4xdontresethba) {
2315 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
2316 ha->host_no, __func__));
2317 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
2318 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
2319 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2320 goto dpc_post_reset_ha;
2322 if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
2323 test_bit(DPC_RESET_HA, &ha->dpc_flags))
2324 qla4xxx_recover_adapter(ha);
2326 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
2327 uint8_t wait_time = RESET_INTR_TOV;
2329 while ((readw(&ha->reg->ctrl_status) &
2330 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
2331 if (--wait_time == 0)
2336 DEBUG2(printk("scsi%ld: %s: SR|FSR "
2337 "bit not cleared-- resetting\n",
2338 ha->host_no, __func__));
2339 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
2340 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
2341 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2342 status = qla4xxx_recover_adapter(ha);
2344 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
2345 if (status == QLA_SUCCESS)
2346 ha->isp_ops->enable_intrs(ha);
2351 /* ---- process AEN? --- */
2352 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
2353 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
2355 /* ---- Get DHCP IP Address? --- */
2356 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
2357 qla4xxx_get_dhcp_ip_address(ha);
2359 /* ---- link change? --- */
2360 if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
2361 if (!test_bit(AF_LINK_UP, &ha->flags)) {
2362 /* ---- link down? --- */
2363 qla4xxx_mark_all_devices_missing(ha);
2365 /* ---- link up? --- *
2366 * F/W will auto login to all devices ONLY ONCE after
2367 * link up during driver initialization and runtime
2368 * fatal error recovery. Therefore, the driver must
2369 * manually relogin to devices when recovering from
2370 * connection failures, logouts, expired KATO, etc. */
2372 qla4xxx_relogin_all_devices(ha);
2378 * qla4xxx_free_adapter - release the adapter
2379 * @ha: pointer to adapter structure
2381 static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
2384 if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
2385 /* Turn-off interrupts on the card. */
2386 ha->isp_ops->disable_intrs(ha);
2389 /* Remove timer thread, if present */
2390 if (ha->timer_active)
2391 qla4xxx_stop_timer(ha);
2393 /* Kill the kernel thread for this host */
2395 destroy_workqueue(ha->dpc_thread);
2397 /* Kill the kernel thread for this host */
2399 destroy_workqueue(ha->task_wq);
2401 /* Put firmware in known state */
2402 ha->isp_ops->reset_firmware(ha);
2404 if (is_qla8022(ha)) {
2405 qla4_8xxx_idc_lock(ha);
2406 qla4_8xxx_clear_drv_active(ha);
2407 qla4_8xxx_idc_unlock(ha);
2410 /* Detach interrupts */
2411 if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
2412 qla4xxx_free_irqs(ha);
2414 /* free extra memory */
2415 qla4xxx_mem_free(ha);
2418 int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
2421 uint8_t revision_id;
2422 unsigned long mem_base, mem_len, db_base, db_len;
2423 struct pci_dev *pdev = ha->pdev;
2425 status = pci_request_regions(pdev, DRIVER_NAME);
2428 "scsi(%ld) Failed to reserve PIO regions (%s) "
2429 "status=%d\n", ha->host_no, pci_name(pdev), status);
2430 goto iospace_error_exit;
2433 pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
2434 DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
2435 __func__, revision_id));
2436 ha->revision_id = revision_id;
2438 /* remap phys address */
2439 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
2440 mem_len = pci_resource_len(pdev, 0);
2441 DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
2442 __func__, mem_base, mem_len));
2444 /* mapping of pcibase pointer */
2445 ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
2446 if (!ha->nx_pcibase) {
2448 "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
2449 pci_release_regions(ha->pdev);
2450 goto iospace_error_exit;
2453 /* Mapping of IO base pointer, door bell read and write pointer */
2455 /* mapping of IO base pointer */
2457 (struct device_reg_82xx __iomem *)((uint8_t *)ha->nx_pcibase +
2458 0xbc000 + (ha->pdev->devfn << 11));
2460 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
2461 db_len = pci_resource_len(pdev, 4);
2463 ha->nx_db_wr_ptr = (ha->pdev->devfn == 4 ? QLA82XX_CAM_RAM_DB1 :
2464 QLA82XX_CAM_RAM_DB2);
2472 * qla4xxx_iospace_config - maps registers
2473 * @ha: pointer to adapter structure
2475 * This routines maps HBA's registers from the pci address space
2476 * into the kernel virtual address space for memory mapped i/o.
2478 int qla4xxx_iospace_config(struct scsi_qla_host *ha)
2480 unsigned long pio, pio_len, pio_flags;
2481 unsigned long mmio, mmio_len, mmio_flags;
2483 pio = pci_resource_start(ha->pdev, 0);
2484 pio_len = pci_resource_len(ha->pdev, 0);
2485 pio_flags = pci_resource_flags(ha->pdev, 0);
2486 if (pio_flags & IORESOURCE_IO) {
2487 if (pio_len < MIN_IOBASE_LEN) {
2488 ql4_printk(KERN_WARNING, ha,
2489 "Invalid PCI I/O region size\n");
2493 ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
2497 /* Use MMIO operations for all accesses. */
2498 mmio = pci_resource_start(ha->pdev, 1);
2499 mmio_len = pci_resource_len(ha->pdev, 1);
2500 mmio_flags = pci_resource_flags(ha->pdev, 1);
2502 if (!(mmio_flags & IORESOURCE_MEM)) {
2503 ql4_printk(KERN_ERR, ha,
2504 "region #0 not an MMIO resource, aborting\n");
2506 goto iospace_error_exit;
2509 if (mmio_len < MIN_IOBASE_LEN) {
2510 ql4_printk(KERN_ERR, ha,
2511 "Invalid PCI mem region size, aborting\n");
2512 goto iospace_error_exit;
2515 if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
2516 ql4_printk(KERN_WARNING, ha,
2517 "Failed to reserve PIO/MMIO regions\n");
2519 goto iospace_error_exit;
2522 ha->pio_address = pio;
2523 ha->pio_length = pio_len;
2524 ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
2526 ql4_printk(KERN_ERR, ha,
2527 "cannot remap MMIO, aborting\n");
2529 goto iospace_error_exit;
2538 static struct isp_operations qla4xxx_isp_ops = {
2539 .iospace_config = qla4xxx_iospace_config,
2540 .pci_config = qla4xxx_pci_config,
2541 .disable_intrs = qla4xxx_disable_intrs,
2542 .enable_intrs = qla4xxx_enable_intrs,
2543 .start_firmware = qla4xxx_start_firmware,
2544 .intr_handler = qla4xxx_intr_handler,
2545 .interrupt_service_routine = qla4xxx_interrupt_service_routine,
2546 .reset_chip = qla4xxx_soft_reset,
2547 .reset_firmware = qla4xxx_hw_reset,
2548 .queue_iocb = qla4xxx_queue_iocb,
2549 .complete_iocb = qla4xxx_complete_iocb,
2550 .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
2551 .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
2552 .get_sys_info = qla4xxx_get_sys_info,
2555 static struct isp_operations qla4_8xxx_isp_ops = {
2556 .iospace_config = qla4_8xxx_iospace_config,
2557 .pci_config = qla4_8xxx_pci_config,
2558 .disable_intrs = qla4_8xxx_disable_intrs,
2559 .enable_intrs = qla4_8xxx_enable_intrs,
2560 .start_firmware = qla4_8xxx_load_risc,
2561 .intr_handler = qla4_8xxx_intr_handler,
2562 .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
2563 .reset_chip = qla4_8xxx_isp_reset,
2564 .reset_firmware = qla4_8xxx_stop_firmware,
2565 .queue_iocb = qla4_8xxx_queue_iocb,
2566 .complete_iocb = qla4_8xxx_complete_iocb,
2567 .rd_shdw_req_q_out = qla4_8xxx_rd_shdw_req_q_out,
2568 .rd_shdw_rsp_q_in = qla4_8xxx_rd_shdw_rsp_q_in,
2569 .get_sys_info = qla4_8xxx_get_sys_info,
2572 uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
2574 return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
2577 uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
2579 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
2582 uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
2584 return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
2587 uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
2589 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
2592 static ssize_t qla4xxx_show_boot_eth_info(void *data, int type, char *buf)
2594 struct scsi_qla_host *ha = data;
2599 case ISCSI_BOOT_ETH_FLAGS:
2600 rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
2602 case ISCSI_BOOT_ETH_INDEX:
2603 rc = sprintf(str, "0\n");
2605 case ISCSI_BOOT_ETH_MAC:
2606 rc = sysfs_format_mac(str, ha->my_mac,
2616 static mode_t qla4xxx_eth_get_attr_visibility(void *data, int type)
2621 case ISCSI_BOOT_ETH_FLAGS:
2622 case ISCSI_BOOT_ETH_MAC:
2623 case ISCSI_BOOT_ETH_INDEX:
2633 static ssize_t qla4xxx_show_boot_ini_info(void *data, int type, char *buf)
2635 struct scsi_qla_host *ha = data;
2640 case ISCSI_BOOT_INI_INITIATOR_NAME:
2641 rc = sprintf(str, "%s\n", ha->name_string);
2650 static mode_t qla4xxx_ini_get_attr_visibility(void *data, int type)
2655 case ISCSI_BOOT_INI_INITIATOR_NAME:
2666 qla4xxx_show_boot_tgt_info(struct ql4_boot_session_info *boot_sess, int type,
2669 struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
2674 case ISCSI_BOOT_TGT_NAME:
2675 rc = sprintf(buf, "%s\n", (char *)&boot_sess->target_name);
2677 case ISCSI_BOOT_TGT_IP_ADDR:
2678 if (boot_sess->conn_list[0].dest_ipaddr.ip_type == 0x1)
2679 rc = sprintf(buf, "%pI4\n",
2680 &boot_conn->dest_ipaddr.ip_address);
2682 rc = sprintf(str, "%pI6\n",
2683 &boot_conn->dest_ipaddr.ip_address);
2685 case ISCSI_BOOT_TGT_PORT:
2686 rc = sprintf(str, "%d\n", boot_conn->dest_port);
2688 case ISCSI_BOOT_TGT_CHAP_NAME:
2689 rc = sprintf(str, "%.*s\n",
2690 boot_conn->chap.target_chap_name_length,
2691 (char *)&boot_conn->chap.target_chap_name);
2693 case ISCSI_BOOT_TGT_CHAP_SECRET:
2694 rc = sprintf(str, "%.*s\n",
2695 boot_conn->chap.target_secret_length,
2696 (char *)&boot_conn->chap.target_secret);
2698 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
2699 rc = sprintf(str, "%.*s\n",
2700 boot_conn->chap.intr_chap_name_length,
2701 (char *)&boot_conn->chap.intr_chap_name);
2703 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
2704 rc = sprintf(str, "%.*s\n",
2705 boot_conn->chap.intr_secret_length,
2706 (char *)&boot_conn->chap.intr_secret);
2708 case ISCSI_BOOT_TGT_FLAGS:
2709 rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
2711 case ISCSI_BOOT_TGT_NIC_ASSOC:
2712 rc = sprintf(str, "0\n");
2721 static ssize_t qla4xxx_show_boot_tgt_pri_info(void *data, int type, char *buf)
2723 struct scsi_qla_host *ha = data;
2724 struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_pri_sess);
2726 return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
2729 static ssize_t qla4xxx_show_boot_tgt_sec_info(void *data, int type, char *buf)
2731 struct scsi_qla_host *ha = data;
2732 struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_sec_sess);
2734 return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
2737 static mode_t qla4xxx_tgt_get_attr_visibility(void *data, int type)
2742 case ISCSI_BOOT_TGT_NAME:
2743 case ISCSI_BOOT_TGT_IP_ADDR:
2744 case ISCSI_BOOT_TGT_PORT:
2745 case ISCSI_BOOT_TGT_CHAP_NAME:
2746 case ISCSI_BOOT_TGT_CHAP_SECRET:
2747 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
2748 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
2749 case ISCSI_BOOT_TGT_NIC_ASSOC:
2750 case ISCSI_BOOT_TGT_FLAGS:
2760 static void qla4xxx_boot_release(void *data)
2762 struct scsi_qla_host *ha = data;
2764 scsi_host_put(ha->host);
2767 static int get_fw_boot_info(struct scsi_qla_host *ha, uint16_t ddb_index[])
2770 uint32_t addr, pri_addr, sec_addr;
2774 uint8_t *buf = NULL;
2775 size_t size = 13 * sizeof(uint8_t);
2776 int ret = QLA_SUCCESS;
2778 func_num = PCI_FUNC(ha->pdev->devfn);
2780 ql4_printk(KERN_INFO, ha, "%s: Get FW boot info for 0x%x func %d\n",
2781 __func__, ha->pdev->device, func_num);
2783 if (is_qla40XX(ha)) {
2784 if (func_num == 1) {
2785 addr = NVRAM_PORT0_BOOT_MODE;
2786 pri_addr = NVRAM_PORT0_BOOT_PRI_TGT;
2787 sec_addr = NVRAM_PORT0_BOOT_SEC_TGT;
2788 } else if (func_num == 3) {
2789 addr = NVRAM_PORT1_BOOT_MODE;
2790 pri_addr = NVRAM_PORT1_BOOT_PRI_TGT;
2791 sec_addr = NVRAM_PORT1_BOOT_SEC_TGT;
2794 goto exit_boot_info;
2797 /* Check Boot Mode */
2798 val = rd_nvram_byte(ha, addr);
2799 if (!(val & 0x07)) {
2800 DEBUG2(ql4_printk(KERN_ERR, ha,
2801 "%s: Failed Boot options : 0x%x\n",
2804 goto exit_boot_info;
2807 /* get primary valid target index */
2808 val = rd_nvram_byte(ha, pri_addr);
2810 ddb_index[0] = (val & 0x7f);
2812 /* get secondary valid target index */
2813 val = rd_nvram_byte(ha, sec_addr);
2815 ddb_index[1] = (val & 0x7f);
2817 } else if (is_qla8022(ha)) {
2818 buf = dma_alloc_coherent(&ha->pdev->dev, size,
2819 &buf_dma, GFP_KERNEL);
2821 DEBUG2(ql4_printk(KERN_ERR, ha,
2822 "%s: Unable to allocate dma buffer\n",
2825 goto exit_boot_info;
2828 if (ha->port_num == 0)
2829 offset = BOOT_PARAM_OFFSET_PORT0;
2830 else if (ha->port_num == 1)
2831 offset = BOOT_PARAM_OFFSET_PORT1;
2834 goto exit_boot_info_free;
2836 addr = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_iscsi_param * 4) +
2838 if (qla4xxx_get_flash(ha, buf_dma, addr,
2839 13 * sizeof(uint8_t)) != QLA_SUCCESS) {
2840 DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: Get Flash"
2841 "failed\n", ha->host_no, __func__));
2843 goto exit_boot_info_free;
2845 /* Check Boot Mode */
2846 if (!(buf[1] & 0x07)) {
2847 DEBUG2(ql4_printk(KERN_INFO, ha,
2848 "Failed: Boot options : 0x%x\n",
2851 goto exit_boot_info_free;
2854 /* get primary valid target index */
2856 ddb_index[0] = buf[2] & 0x7f;
2858 /* get secondary valid target index */
2859 if (buf[11] & BIT_7)
2860 ddb_index[1] = buf[11] & 0x7f;
2863 goto exit_boot_info;
2866 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Primary target ID %d, Secondary"
2867 " target ID %d\n", __func__, ddb_index[0],
2870 exit_boot_info_free:
2871 dma_free_coherent(&ha->pdev->dev, size, buf, buf_dma);
2877 * qla4xxx_get_bidi_chap - Get a BIDI CHAP user and password
2878 * @ha: pointer to adapter structure
2879 * @username: CHAP username to be returned
2880 * @password: CHAP password to be returned
2882 * If a boot entry has BIDI CHAP enabled then we need to set the BIDI CHAP
2883 * user and password in the sysfs entry in /sys/firmware/iscsi_boot#/.
2884 * So from the CHAP cache find the first BIDI CHAP entry and set it
2885 * to the boot record in sysfs.
2887 static int qla4xxx_get_bidi_chap(struct scsi_qla_host *ha, char *username,
2890 int i, ret = -EINVAL;
2891 int max_chap_entries = 0;
2892 struct ql4_chap_table *chap_table;
2895 max_chap_entries = (ha->hw.flt_chap_size / 2) /
2896 sizeof(struct ql4_chap_table);
2898 max_chap_entries = MAX_CHAP_ENTRIES_40XX;
2900 if (!ha->chap_list) {
2901 ql4_printk(KERN_ERR, ha, "Do not have CHAP table cache\n");
2905 mutex_lock(&ha->chap_sem);
2906 for (i = 0; i < max_chap_entries; i++) {
2907 chap_table = (struct ql4_chap_table *)ha->chap_list + i;
2908 if (chap_table->cookie !=
2909 __constant_cpu_to_le16(CHAP_VALID_COOKIE)) {
2913 if (chap_table->flags & BIT_7) /* local */
2916 if (!(chap_table->flags & BIT_6)) /* Not BIDI */
2919 strncpy(password, chap_table->secret, QL4_CHAP_MAX_SECRET_LEN);
2920 strncpy(username, chap_table->name, QL4_CHAP_MAX_NAME_LEN);
2924 mutex_unlock(&ha->chap_sem);
2930 static int qla4xxx_get_boot_target(struct scsi_qla_host *ha,
2931 struct ql4_boot_session_info *boot_sess,
2934 struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
2935 struct dev_db_entry *fw_ddb_entry;
2936 dma_addr_t fw_ddb_entry_dma;
2939 int ret = QLA_SUCCESS;
2941 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
2942 &fw_ddb_entry_dma, GFP_KERNEL);
2943 if (!fw_ddb_entry) {
2944 DEBUG2(ql4_printk(KERN_ERR, ha,
2945 "%s: Unable to allocate dma buffer.\n",
2951 if (qla4xxx_bootdb_by_index(ha, fw_ddb_entry,
2952 fw_ddb_entry_dma, ddb_index)) {
2953 DEBUG2(ql4_printk(KERN_ERR, ha,
2954 "%s: Flash DDB read Failed\n", __func__));
2956 goto exit_boot_target;
2959 /* Update target name and IP from DDB */
2960 memcpy(boot_sess->target_name, fw_ddb_entry->iscsi_name,
2961 min(sizeof(boot_sess->target_name),
2962 sizeof(fw_ddb_entry->iscsi_name)));
2964 options = le16_to_cpu(fw_ddb_entry->options);
2965 if (options & DDB_OPT_IPV6_DEVICE) {
2966 memcpy(&boot_conn->dest_ipaddr.ip_address,
2967 &fw_ddb_entry->ip_addr[0], IPv6_ADDR_LEN);
2969 boot_conn->dest_ipaddr.ip_type = 0x1;
2970 memcpy(&boot_conn->dest_ipaddr.ip_address,
2971 &fw_ddb_entry->ip_addr[0], IP_ADDR_LEN);
2974 boot_conn->dest_port = le16_to_cpu(fw_ddb_entry->port);
2976 /* update chap information */
2977 idx = __le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
2979 if (BIT_7 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
2981 DEBUG2(ql4_printk(KERN_INFO, ha, "Setting chap\n"));
2983 ret = qla4xxx_get_chap(ha, (char *)&boot_conn->chap.
2985 (char *)&boot_conn->chap.target_secret,
2988 ql4_printk(KERN_ERR, ha, "Failed to set chap\n");
2990 goto exit_boot_target;
2993 boot_conn->chap.target_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
2994 boot_conn->chap.target_secret_length = QL4_CHAP_MAX_SECRET_LEN;
2997 if (BIT_4 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
2999 DEBUG2(ql4_printk(KERN_INFO, ha, "Setting BIDI chap\n"));
3001 ret = qla4xxx_get_bidi_chap(ha,
3002 (char *)&boot_conn->chap.intr_chap_name,
3003 (char *)&boot_conn->chap.intr_secret);
3006 ql4_printk(KERN_ERR, ha, "Failed to set BIDI chap\n");
3008 goto exit_boot_target;
3011 boot_conn->chap.intr_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
3012 boot_conn->chap.intr_secret_length = QL4_CHAP_MAX_SECRET_LEN;
3016 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3017 fw_ddb_entry, fw_ddb_entry_dma);
3021 static int qla4xxx_get_boot_info(struct scsi_qla_host *ha)
3023 uint16_t ddb_index[2];
3024 int ret = QLA_ERROR;
3027 memset(ddb_index, 0, sizeof(ddb_index));
3028 ddb_index[0] = 0xffff;
3029 ddb_index[1] = 0xffff;
3030 ret = get_fw_boot_info(ha, ddb_index);
3031 if (ret != QLA_SUCCESS) {
3032 DEBUG2(ql4_printk(KERN_ERR, ha,
3033 "%s: Failed to set boot info.\n", __func__));
3037 if (ddb_index[0] == 0xffff)
3040 rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_pri_sess),
3042 if (rval != QLA_SUCCESS) {
3043 DEBUG2(ql4_printk(KERN_ERR, ha, "%s: Failed to get "
3044 "primary target\n", __func__));
3049 if (ddb_index[1] == 0xffff)
3050 goto exit_get_boot_info;
3052 rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_sec_sess),
3054 if (rval != QLA_SUCCESS) {
3055 DEBUG2(ql4_printk(KERN_ERR, ha, "%s: Failed to get "
3056 "secondary target\n", __func__));
3064 static int qla4xxx_setup_boot_info(struct scsi_qla_host *ha)
3066 struct iscsi_boot_kobj *boot_kobj;
3068 if (qla4xxx_get_boot_info(ha) != QLA_SUCCESS)
3071 ha->boot_kset = iscsi_boot_create_host_kset(ha->host->host_no);
3075 if (!scsi_host_get(ha->host))
3077 boot_kobj = iscsi_boot_create_target(ha->boot_kset, 0, ha,
3078 qla4xxx_show_boot_tgt_pri_info,
3079 qla4xxx_tgt_get_attr_visibility,
3080 qla4xxx_boot_release);
3084 if (!scsi_host_get(ha->host))
3086 boot_kobj = iscsi_boot_create_target(ha->boot_kset, 1, ha,
3087 qla4xxx_show_boot_tgt_sec_info,
3088 qla4xxx_tgt_get_attr_visibility,
3089 qla4xxx_boot_release);
3093 if (!scsi_host_get(ha->host))
3095 boot_kobj = iscsi_boot_create_initiator(ha->boot_kset, 0, ha,
3096 qla4xxx_show_boot_ini_info,
3097 qla4xxx_ini_get_attr_visibility,
3098 qla4xxx_boot_release);
3102 if (!scsi_host_get(ha->host))
3104 boot_kobj = iscsi_boot_create_ethernet(ha->boot_kset, 0, ha,
3105 qla4xxx_show_boot_eth_info,
3106 qla4xxx_eth_get_attr_visibility,
3107 qla4xxx_boot_release);
3114 scsi_host_put(ha->host);
3116 iscsi_boot_destroy_kset(ha->boot_kset);
3122 * qla4xxx_create chap_list - Create CHAP list from FLASH
3123 * @ha: pointer to adapter structure
3125 * Read flash and make a list of CHAP entries, during login when a CHAP entry
3126 * is received, it will be checked in this list. If entry exist then the CHAP
3127 * entry index is set in the DDB. If CHAP entry does not exist in this list
3128 * then a new entry is added in FLASH in CHAP table and the index obtained is
3131 static void qla4xxx_create_chap_list(struct scsi_qla_host *ha)
3134 uint8_t *chap_flash_data = NULL;
3136 dma_addr_t chap_dma;
3137 uint32_t chap_size = 0;
3140 chap_size = MAX_CHAP_ENTRIES_40XX *
3141 sizeof(struct ql4_chap_table);
3142 else /* Single region contains CHAP info for both
3143 * ports which is divided into half for each port.
3145 chap_size = ha->hw.flt_chap_size / 2;
3147 chap_flash_data = dma_alloc_coherent(&ha->pdev->dev, chap_size,
3148 &chap_dma, GFP_KERNEL);
3149 if (!chap_flash_data) {
3150 ql4_printk(KERN_ERR, ha, "No memory for chap_flash_data\n");
3154 offset = FLASH_CHAP_OFFSET;
3156 offset = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_region_chap << 2);
3157 if (ha->port_num == 1)
3158 offset += chap_size;
3161 rval = qla4xxx_get_flash(ha, chap_dma, offset, chap_size);
3162 if (rval != QLA_SUCCESS)
3163 goto exit_chap_list;
3165 if (ha->chap_list == NULL)
3166 ha->chap_list = vmalloc(chap_size);
3167 if (ha->chap_list == NULL) {
3168 ql4_printk(KERN_ERR, ha, "No memory for ha->chap_list\n");
3169 goto exit_chap_list;
3172 memcpy(ha->chap_list, chap_flash_data, chap_size);
3175 dma_free_coherent(&ha->pdev->dev, chap_size,
3176 chap_flash_data, chap_dma);
3181 * qla4xxx_probe_adapter - callback function to probe HBA
3182 * @pdev: pointer to pci_dev structure
3183 * @pci_device_id: pointer to pci_device entry
3185 * This routine will probe for Qlogic 4xxx iSCSI host adapters.
3186 * It returns zero if successful. It also initializes all data necessary for
3189 static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
3190 const struct pci_device_id *ent)
3192 int ret = -ENODEV, status;
3193 struct Scsi_Host *host;
3194 struct scsi_qla_host *ha;
3195 uint8_t init_retry_count = 0;
3197 struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
3200 if (pci_enable_device(pdev))
3203 host = iscsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha), 0);
3206 "qla4xxx: Couldn't allocate host from scsi layer!\n");
3207 goto probe_disable_device;
3210 /* Clear our data area */
3211 ha = to_qla_host(host);
3212 memset(ha, 0, sizeof(*ha));
3214 /* Save the information from PCI BIOS. */
3217 ha->host_no = host->host_no;
3219 pci_enable_pcie_error_reporting(pdev);
3221 /* Setup Runtime configurable options */
3222 if (is_qla8022(ha)) {
3223 ha->isp_ops = &qla4_8xxx_isp_ops;
3224 rwlock_init(&ha->hw_lock);
3225 ha->qdr_sn_window = -1;
3226 ha->ddr_mn_window = -1;
3227 ha->curr_window = 255;
3228 ha->func_num = PCI_FUNC(ha->pdev->devfn);
3229 nx_legacy_intr = &legacy_intr[ha->func_num];
3230 ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
3231 ha->nx_legacy_intr.tgt_status_reg =
3232 nx_legacy_intr->tgt_status_reg;
3233 ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
3234 ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
3236 ha->isp_ops = &qla4xxx_isp_ops;
3239 /* Set EEH reset type to fundamental if required by hba */
3241 pdev->needs_freset = 1;
3243 /* Configure PCI I/O space. */
3244 ret = ha->isp_ops->iospace_config(ha);
3246 goto probe_failed_ioconfig;
3248 ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
3249 pdev->device, pdev->irq, ha->reg);
3251 qla4xxx_config_dma_addressing(ha);
3253 /* Initialize lists and spinlocks. */
3254 INIT_LIST_HEAD(&ha->free_srb_q);
3256 mutex_init(&ha->mbox_sem);
3257 mutex_init(&ha->chap_sem);
3258 init_completion(&ha->mbx_intr_comp);
3259 init_completion(&ha->disable_acb_comp);
3261 spin_lock_init(&ha->hardware_lock);
3263 /* Allocate dma buffers */
3264 if (qla4xxx_mem_alloc(ha)) {
3265 ql4_printk(KERN_WARNING, ha,
3266 "[ERROR] Failed to allocate memory for adapter\n");
3272 host->cmd_per_lun = 3;
3273 host->max_channel = 0;
3274 host->max_lun = MAX_LUNS - 1;
3275 host->max_id = MAX_TARGETS;
3276 host->max_cmd_len = IOCB_MAX_CDB_LEN;
3277 host->can_queue = MAX_SRBS ;
3278 host->transportt = qla4xxx_scsi_transport;
3280 ret = scsi_init_shared_tag_map(host, MAX_SRBS);
3282 ql4_printk(KERN_WARNING, ha,
3283 "%s: scsi_init_shared_tag_map failed\n", __func__);
3287 pci_set_drvdata(pdev, ha);
3289 ret = scsi_add_host(host, &pdev->dev);
3294 (void) qla4_8xxx_get_flash_info(ha);
3297 * Initialize the Host adapter request/response queues and
3299 * NOTE: interrupts enabled upon successful completion
3301 status = qla4xxx_initialize_adapter(ha);
3302 while ((!test_bit(AF_ONLINE, &ha->flags)) &&
3303 init_retry_count++ < MAX_INIT_RETRIES) {
3305 if (is_qla8022(ha)) {
3306 qla4_8xxx_idc_lock(ha);
3307 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
3308 qla4_8xxx_idc_unlock(ha);
3309 if (dev_state == QLA82XX_DEV_FAILED) {
3310 ql4_printk(KERN_WARNING, ha, "%s: don't retry "
3311 "initialize adapter. H/W is in failed state\n",
3316 DEBUG2(printk("scsi: %s: retrying adapter initialization "
3317 "(%d)\n", __func__, init_retry_count));
3319 if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
3322 status = qla4xxx_initialize_adapter(ha);
3325 if (!test_bit(AF_ONLINE, &ha->flags)) {
3326 ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
3328 if (is_qla8022(ha) && ql4xdontresethba) {
3329 /* Put the device in failed state. */
3330 DEBUG2(printk(KERN_ERR "HW STATE: FAILED\n"));
3331 qla4_8xxx_idc_lock(ha);
3332 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
3333 QLA82XX_DEV_FAILED);
3334 qla4_8xxx_idc_unlock(ha);
3340 /* Startup the kernel thread for this host adapter. */
3341 DEBUG2(printk("scsi: %s: Starting kernel thread for "
3342 "qla4xxx_dpc\n", __func__));
3343 sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
3344 ha->dpc_thread = create_singlethread_workqueue(buf);
3345 if (!ha->dpc_thread) {
3346 ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
3350 INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
3352 sprintf(buf, "qla4xxx_%lu_task", ha->host_no);
3353 ha->task_wq = alloc_workqueue(buf, WQ_MEM_RECLAIM, 1);
3355 ql4_printk(KERN_WARNING, ha, "Unable to start task thread!\n");
3360 /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
3361 * (which is called indirectly by qla4xxx_initialize_adapter),
3362 * so that irqs will be registered after crbinit but before
3365 if (!is_qla8022(ha)) {
3366 ret = qla4xxx_request_irqs(ha);
3368 ql4_printk(KERN_WARNING, ha, "Failed to reserve "
3369 "interrupt %d already in use.\n", pdev->irq);
3374 pci_save_state(ha->pdev);
3375 ha->isp_ops->enable_intrs(ha);
3377 /* Start timer thread. */
3378 qla4xxx_start_timer(ha, qla4xxx_timer, 1);
3380 set_bit(AF_INIT_DONE, &ha->flags);
3383 " QLogic iSCSI HBA Driver version: %s\n"
3384 " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
3385 qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
3386 ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
3387 ha->patch_number, ha->build_number);
3389 qla4xxx_create_chap_list(ha);
3391 if (qla4xxx_setup_boot_info(ha))
3392 ql4_printk(KERN_ERR, ha, "%s:ISCSI boot info setup failed\n",
3395 qla4xxx_create_ifaces(ha);
3399 scsi_remove_host(ha->host);
3402 qla4xxx_free_adapter(ha);
3404 probe_failed_ioconfig:
3405 pci_disable_pcie_error_reporting(pdev);
3406 scsi_host_put(ha->host);
3408 probe_disable_device:
3409 pci_disable_device(pdev);
3415 * qla4xxx_prevent_other_port_reinit - prevent other port from re-initialize
3416 * @ha: pointer to adapter structure
3418 * Mark the other ISP-4xxx port to indicate that the driver is being removed,
3419 * so that the other port will not re-initialize while in the process of
3420 * removing the ha due to driver unload or hba hotplug.
3422 static void qla4xxx_prevent_other_port_reinit(struct scsi_qla_host *ha)
3424 struct scsi_qla_host *other_ha = NULL;
3425 struct pci_dev *other_pdev = NULL;
3426 int fn = ISP4XXX_PCI_FN_2;
3428 /*iscsi function numbers for ISP4xxx is 1 and 3*/
3429 if (PCI_FUNC(ha->pdev->devfn) & BIT_1)
3430 fn = ISP4XXX_PCI_FN_1;
3433 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
3434 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
3437 /* Get other_ha if other_pdev is valid and state is enable*/
3439 if (atomic_read(&other_pdev->enable_cnt)) {
3440 other_ha = pci_get_drvdata(other_pdev);
3442 set_bit(AF_HA_REMOVAL, &other_ha->flags);
3443 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: "
3444 "Prevent %s reinit\n", __func__,
3445 dev_name(&other_ha->pdev->dev)));
3448 pci_dev_put(other_pdev);
3453 * qla4xxx_remove_adapter - calback function to remove adapter.
3454 * @pci_dev: PCI device pointer
3456 static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
3458 struct scsi_qla_host *ha;
3460 ha = pci_get_drvdata(pdev);
3462 if (!is_qla8022(ha))
3463 qla4xxx_prevent_other_port_reinit(ha);
3465 /* destroy iface from sysfs */
3466 qla4xxx_destroy_ifaces(ha);
3469 iscsi_boot_destroy_kset(ha->boot_kset);
3471 scsi_remove_host(ha->host);
3473 qla4xxx_free_adapter(ha);
3475 scsi_host_put(ha->host);
3477 pci_disable_pcie_error_reporting(pdev);
3478 pci_disable_device(pdev);
3479 pci_set_drvdata(pdev, NULL);
3483 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
3486 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
3487 * supported addressing method.
3489 static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
3493 /* Update our PCI device dma_mask for full 64 bit mask */
3494 if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
3495 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
3496 dev_dbg(&ha->pdev->dev,
3497 "Failed to set 64 bit PCI consistent mask; "
3499 retval = pci_set_consistent_dma_mask(ha->pdev,
3503 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
3506 static int qla4xxx_slave_alloc(struct scsi_device *sdev)
3508 struct iscsi_cls_session *cls_sess;
3509 struct iscsi_session *sess;
3510 struct ddb_entry *ddb;
3511 int queue_depth = QL4_DEF_QDEPTH;
3513 cls_sess = starget_to_session(sdev->sdev_target);
3514 sess = cls_sess->dd_data;
3515 ddb = sess->dd_data;
3517 sdev->hostdata = ddb;
3518 sdev->tagged_supported = 1;
3520 if (ql4xmaxqdepth != 0 && ql4xmaxqdepth <= 0xffffU)
3521 queue_depth = ql4xmaxqdepth;
3523 scsi_activate_tcq(sdev, queue_depth);
3527 static int qla4xxx_slave_configure(struct scsi_device *sdev)
3529 sdev->tagged_supported = 1;
3533 static void qla4xxx_slave_destroy(struct scsi_device *sdev)
3535 scsi_deactivate_tcq(sdev, 1);
3539 * qla4xxx_del_from_active_array - returns an active srb
3540 * @ha: Pointer to host adapter structure.
3541 * @index: index into the active_array
3543 * This routine removes and returns the srb at the specified index
3545 struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
3548 struct srb *srb = NULL;
3549 struct scsi_cmnd *cmd = NULL;
3551 cmd = scsi_host_find_tag(ha->host, index);
3555 srb = (struct srb *)CMD_SP(cmd);
3559 /* update counters */
3560 if (srb->flags & SRB_DMA_VALID) {
3561 ha->req_q_count += srb->iocb_cnt;
3562 ha->iocb_cnt -= srb->iocb_cnt;
3564 srb->cmd->host_scribble =
3565 (unsigned char *)(unsigned long) MAX_SRBS;
3571 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
3572 * @ha: Pointer to host adapter structure.
3573 * @cmd: Scsi Command to wait on.
3575 * This routine waits for the command to be returned by the Firmware
3576 * for some max time.
3578 static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
3579 struct scsi_cmnd *cmd)
3583 uint32_t max_wait_time = EH_WAIT_CMD_TOV;
3586 /* Dont wait on command if PCI error is being handled
3589 if (unlikely(pci_channel_offline(ha->pdev)) ||
3590 (test_bit(AF_EEH_BUSY, &ha->flags))) {
3591 ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
3592 ha->host_no, __func__);
3597 /* Checking to see if its returned to OS */
3598 rp = (struct srb *) CMD_SP(cmd);
3605 } while (max_wait_time--);
3611 * qla4xxx_wait_for_hba_online - waits for HBA to come online
3612 * @ha: Pointer to host adapter structure
3614 static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
3616 unsigned long wait_online;
3618 wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
3619 while (time_before(jiffies, wait_online)) {
3631 * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
3632 * @ha: pointer to HBA
3636 * This function waits for all outstanding commands to a lun to complete. It
3637 * returns 0 if all pending commands are returned and 1 otherwise.
3639 static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
3640 struct scsi_target *stgt,
3641 struct scsi_device *sdev)
3645 struct scsi_cmnd *cmd;
3648 * Waiting for all commands for the designated target or dev
3649 * in the active array
3651 for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
3652 cmd = scsi_host_find_tag(ha->host, cnt);
3653 if (cmd && stgt == scsi_target(cmd->device) &&
3654 (!sdev || sdev == cmd->device)) {
3655 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
3665 * qla4xxx_eh_abort - callback for abort task.
3666 * @cmd: Pointer to Linux's SCSI command structure
3668 * This routine is called by the Linux OS to abort the specified
3671 static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
3673 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
3674 unsigned int id = cmd->device->id;
3675 unsigned int lun = cmd->device->lun;
3676 unsigned long flags;
3677 struct srb *srb = NULL;
3681 ql4_printk(KERN_INFO, ha,
3682 "scsi%ld:%d:%d: Abort command issued cmd=%p\n",
3683 ha->host_no, id, lun, cmd);
3685 spin_lock_irqsave(&ha->hardware_lock, flags);
3686 srb = (struct srb *) CMD_SP(cmd);
3688 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3691 kref_get(&srb->srb_ref);
3692 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3694 if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
3695 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
3696 ha->host_no, id, lun));
3699 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
3700 ha->host_no, id, lun));
3704 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
3706 /* Wait for command to complete */
3708 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
3709 DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
3710 ha->host_no, id, lun));
3715 ql4_printk(KERN_INFO, ha,
3716 "scsi%ld:%d:%d: Abort command - %s\n",
3717 ha->host_no, id, lun, (ret == SUCCESS) ? "succeeded" : "failed");
3723 * qla4xxx_eh_device_reset - callback for target reset.
3724 * @cmd: Pointer to Linux's SCSI command structure
3726 * This routine is called by the Linux OS to reset all luns on the
3729 static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
3731 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
3732 struct ddb_entry *ddb_entry = cmd->device->hostdata;
3733 int ret = FAILED, stat;
3738 ret = iscsi_block_scsi_eh(cmd);
3743 ql4_printk(KERN_INFO, ha,
3744 "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
3745 cmd->device->channel, cmd->device->id, cmd->device->lun);
3747 DEBUG2(printk(KERN_INFO
3748 "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
3749 "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
3750 cmd, jiffies, cmd->request->timeout / HZ,
3751 ha->dpc_flags, cmd->result, cmd->allowed));
3753 /* FIXME: wait for hba to go online */
3754 stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
3755 if (stat != QLA_SUCCESS) {
3756 ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
3757 goto eh_dev_reset_done;
3760 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
3762 ql4_printk(KERN_INFO, ha,
3763 "DEVICE RESET FAILED - waiting for "
3765 goto eh_dev_reset_done;
3769 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
3770 MM_LUN_RESET) != QLA_SUCCESS)
3771 goto eh_dev_reset_done;
3773 ql4_printk(KERN_INFO, ha,
3774 "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
3775 ha->host_no, cmd->device->channel, cmd->device->id,
3786 * qla4xxx_eh_target_reset - callback for target reset.
3787 * @cmd: Pointer to Linux's SCSI command structure
3789 * This routine is called by the Linux OS to reset the target.
3791 static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
3793 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
3794 struct ddb_entry *ddb_entry = cmd->device->hostdata;
3800 ret = iscsi_block_scsi_eh(cmd);
3804 starget_printk(KERN_INFO, scsi_target(cmd->device),
3805 "WARM TARGET RESET ISSUED.\n");
3807 DEBUG2(printk(KERN_INFO
3808 "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
3809 "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
3810 ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
3811 ha->dpc_flags, cmd->result, cmd->allowed));
3813 stat = qla4xxx_reset_target(ha, ddb_entry);
3814 if (stat != QLA_SUCCESS) {
3815 starget_printk(KERN_INFO, scsi_target(cmd->device),
3816 "WARM TARGET RESET FAILED.\n");
3820 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
3822 starget_printk(KERN_INFO, scsi_target(cmd->device),
3823 "WARM TARGET DEVICE RESET FAILED - "
3824 "waiting for commands.\n");
3829 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
3830 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
3831 starget_printk(KERN_INFO, scsi_target(cmd->device),
3832 "WARM TARGET DEVICE RESET FAILED - "
3833 "marker iocb failed.\n");
3837 starget_printk(KERN_INFO, scsi_target(cmd->device),
3838 "WARM TARGET RESET SUCCEEDED.\n");
3843 * qla4xxx_eh_host_reset - kernel callback
3844 * @cmd: Pointer to Linux's SCSI command structure
3846 * This routine is invoked by the Linux kernel to perform fatal error
3847 * recovery on the specified adapter.
3849 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
3851 int return_status = FAILED;
3852 struct scsi_qla_host *ha;
3854 ha = to_qla_host(cmd->device->host);
3856 if (ql4xdontresethba) {
3857 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
3858 ha->host_no, __func__));
3862 ql4_printk(KERN_INFO, ha,
3863 "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
3864 cmd->device->channel, cmd->device->id, cmd->device->lun);
3866 if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
3867 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
3868 "DEAD.\n", ha->host_no, cmd->device->channel,
3874 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
3876 set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
3878 set_bit(DPC_RESET_HA, &ha->dpc_flags);
3881 if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
3882 return_status = SUCCESS;
3884 ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
3885 return_status == FAILED ? "FAILED" : "SUCCEEDED");
3887 return return_status;
3890 static int qla4xxx_context_reset(struct scsi_qla_host *ha)
3892 uint32_t mbox_cmd[MBOX_REG_COUNT];
3893 uint32_t mbox_sts[MBOX_REG_COUNT];
3894 struct addr_ctrl_blk_def *acb = NULL;
3895 uint32_t acb_len = sizeof(struct addr_ctrl_blk_def);
3896 int rval = QLA_SUCCESS;
3899 acb = dma_alloc_coherent(&ha->pdev->dev,
3900 sizeof(struct addr_ctrl_blk_def),
3901 &acb_dma, GFP_KERNEL);
3903 ql4_printk(KERN_ERR, ha, "%s: Unable to alloc acb\n",
3906 goto exit_port_reset;
3909 memset(acb, 0, acb_len);
3911 rval = qla4xxx_get_acb(ha, acb_dma, PRIMARI_ACB, acb_len);
3912 if (rval != QLA_SUCCESS) {
3917 rval = qla4xxx_disable_acb(ha);
3918 if (rval != QLA_SUCCESS) {
3923 wait_for_completion_timeout(&ha->disable_acb_comp,
3924 DISABLE_ACB_TOV * HZ);
3926 rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], acb_dma);
3927 if (rval != QLA_SUCCESS) {
3933 dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk_def),
3936 DEBUG2(ql4_printk(KERN_INFO, ha, "%s %s\n", __func__,
3937 rval == QLA_SUCCESS ? "SUCCEEDED" : "FAILED"));
3941 static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type)
3943 struct scsi_qla_host *ha = to_qla_host(shost);
3944 int rval = QLA_SUCCESS;
3946 if (ql4xdontresethba) {
3947 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Don't Reset HBA\n",
3950 goto exit_host_reset;
3953 rval = qla4xxx_wait_for_hba_online(ha);
3954 if (rval != QLA_SUCCESS) {
3955 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Unable to reset host "
3956 "adapter\n", __func__));
3958 goto exit_host_reset;
3961 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
3962 goto recover_adapter;
3964 switch (reset_type) {
3965 case SCSI_ADAPTER_RESET:
3966 set_bit(DPC_RESET_HA, &ha->dpc_flags);
3968 case SCSI_FIRMWARE_RESET:
3969 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
3971 /* set firmware context reset */
3972 set_bit(DPC_RESET_HA_FW_CONTEXT,
3975 rval = qla4xxx_context_reset(ha);
3976 goto exit_host_reset;
3983 rval = qla4xxx_recover_adapter(ha);
3984 if (rval != QLA_SUCCESS) {
3985 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: recover adapter fail\n",
3994 /* PCI AER driver recovers from all correctable errors w/o
3995 * driver intervention. For uncorrectable errors PCI AER
3996 * driver calls the following device driver's callbacks
3998 * - Fatal Errors - link_reset
3999 * - Non-Fatal Errors - driver's pci_error_detected() which
4000 * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
4002 * PCI AER driver calls
4003 * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
4004 * returns RECOVERED or NEED_RESET if fw_hung
4005 * NEED_RESET - driver's slot_reset()
4006 * DISCONNECT - device is dead & cannot recover
4007 * RECOVERED - driver's pci_resume()
4009 static pci_ers_result_t
4010 qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
4012 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
4014 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
4015 ha->host_no, __func__, state);
4017 if (!is_aer_supported(ha))
4018 return PCI_ERS_RESULT_NONE;
4021 case pci_channel_io_normal:
4022 clear_bit(AF_EEH_BUSY, &ha->flags);
4023 return PCI_ERS_RESULT_CAN_RECOVER;
4024 case pci_channel_io_frozen:
4025 set_bit(AF_EEH_BUSY, &ha->flags);
4026 qla4xxx_mailbox_premature_completion(ha);
4027 qla4xxx_free_irqs(ha);
4028 pci_disable_device(pdev);
4029 /* Return back all IOs */
4030 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
4031 return PCI_ERS_RESULT_NEED_RESET;
4032 case pci_channel_io_perm_failure:
4033 set_bit(AF_EEH_BUSY, &ha->flags);
4034 set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
4035 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
4036 return PCI_ERS_RESULT_DISCONNECT;
4038 return PCI_ERS_RESULT_NEED_RESET;
4042 * qla4xxx_pci_mmio_enabled() gets called if
4043 * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
4044 * and read/write to the device still works.
4046 static pci_ers_result_t
4047 qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
4049 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
4051 if (!is_aer_supported(ha))
4052 return PCI_ERS_RESULT_NONE;
4054 return PCI_ERS_RESULT_RECOVERED;
4057 static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
4059 uint32_t rval = QLA_ERROR;
4062 struct pci_dev *other_pdev = NULL;
4064 ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
4066 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
4068 if (test_bit(AF_ONLINE, &ha->flags)) {
4069 clear_bit(AF_ONLINE, &ha->flags);
4070 clear_bit(AF_LINK_UP, &ha->flags);
4071 iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
4072 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
4075 fn = PCI_FUNC(ha->pdev->devfn);
4078 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
4079 "func %x\n", ha->host_no, __func__, fn);
4080 /* Get the pci device given the domain, bus,
4081 * slot/function number */
4083 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
4084 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
4090 if (atomic_read(&other_pdev->enable_cnt)) {
4091 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
4092 "func in enabled state%x\n", ha->host_no,
4094 pci_dev_put(other_pdev);
4097 pci_dev_put(other_pdev);
4100 /* The first function on the card, the reset owner will
4101 * start & initialize the firmware. The other functions
4102 * on the card will reset the firmware context
4105 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
4106 "0x%x is the owner\n", ha->host_no, __func__,
4109 qla4_8xxx_idc_lock(ha);
4110 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
4113 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
4114 QLA82XX_IDC_VERSION);
4116 qla4_8xxx_idc_unlock(ha);
4117 clear_bit(AF_FW_RECOVERY, &ha->flags);
4118 rval = qla4xxx_initialize_adapter(ha);
4119 qla4_8xxx_idc_lock(ha);
4121 if (rval != QLA_SUCCESS) {
4122 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
4123 "FAILED\n", ha->host_no, __func__);
4124 qla4_8xxx_clear_drv_active(ha);
4125 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
4126 QLA82XX_DEV_FAILED);
4128 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
4129 "READY\n", ha->host_no, __func__);
4130 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
4132 /* Clear driver state register */
4133 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
4134 qla4_8xxx_set_drv_active(ha);
4135 ret = qla4xxx_request_irqs(ha);
4137 ql4_printk(KERN_WARNING, ha, "Failed to "
4138 "reserve interrupt %d already in use.\n",
4142 ha->isp_ops->enable_intrs(ha);
4146 qla4_8xxx_idc_unlock(ha);
4148 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
4149 "the reset owner\n", ha->host_no, __func__,
4151 if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
4152 QLA82XX_DEV_READY)) {
4153 clear_bit(AF_FW_RECOVERY, &ha->flags);
4154 rval = qla4xxx_initialize_adapter(ha);
4155 if (rval == QLA_SUCCESS) {
4156 ret = qla4xxx_request_irqs(ha);
4158 ql4_printk(KERN_WARNING, ha, "Failed to"
4159 " reserve interrupt %d already in"
4160 " use.\n", ha->pdev->irq);
4163 ha->isp_ops->enable_intrs(ha);
4167 qla4_8xxx_idc_lock(ha);
4168 qla4_8xxx_set_drv_active(ha);
4169 qla4_8xxx_idc_unlock(ha);
4172 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
4176 static pci_ers_result_t
4177 qla4xxx_pci_slot_reset(struct pci_dev *pdev)
4179 pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
4180 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
4183 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
4184 ha->host_no, __func__);
4186 if (!is_aer_supported(ha))
4187 return PCI_ERS_RESULT_NONE;
4189 /* Restore the saved state of PCIe device -
4190 * BAR registers, PCI Config space, PCIX, MSI,
4193 pci_restore_state(pdev);
4195 /* pci_restore_state() clears the saved_state flag of the device
4196 * save restored state which resets saved_state flag
4198 pci_save_state(pdev);
4200 /* Initialize device or resume if in suspended state */
4201 rc = pci_enable_device(pdev);
4203 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Can't re-enable "
4204 "device after reset\n", ha->host_no, __func__);
4205 goto exit_slot_reset;
4208 ha->isp_ops->disable_intrs(ha);
4210 if (is_qla8022(ha)) {
4211 if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
4212 ret = PCI_ERS_RESULT_RECOVERED;
4213 goto exit_slot_reset;
4215 goto exit_slot_reset;
4219 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
4220 "device after reset\n", ha->host_no, __func__, ret);
4225 qla4xxx_pci_resume(struct pci_dev *pdev)
4227 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
4230 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
4231 ha->host_no, __func__);
4233 ret = qla4xxx_wait_for_hba_online(ha);
4234 if (ret != QLA_SUCCESS) {
4235 ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
4236 "resume I/O from slot/link_reset\n", ha->host_no,
4240 pci_cleanup_aer_uncorrect_error_status(pdev);
4241 clear_bit(AF_EEH_BUSY, &ha->flags);
4244 static struct pci_error_handlers qla4xxx_err_handler = {
4245 .error_detected = qla4xxx_pci_error_detected,
4246 .mmio_enabled = qla4xxx_pci_mmio_enabled,
4247 .slot_reset = qla4xxx_pci_slot_reset,
4248 .resume = qla4xxx_pci_resume,
4251 static struct pci_device_id qla4xxx_pci_tbl[] = {
4253 .vendor = PCI_VENDOR_ID_QLOGIC,
4254 .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
4255 .subvendor = PCI_ANY_ID,
4256 .subdevice = PCI_ANY_ID,
4259 .vendor = PCI_VENDOR_ID_QLOGIC,
4260 .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
4261 .subvendor = PCI_ANY_ID,
4262 .subdevice = PCI_ANY_ID,
4265 .vendor = PCI_VENDOR_ID_QLOGIC,
4266 .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
4267 .subvendor = PCI_ANY_ID,
4268 .subdevice = PCI_ANY_ID,
4271 .vendor = PCI_VENDOR_ID_QLOGIC,
4272 .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
4273 .subvendor = PCI_ANY_ID,
4274 .subdevice = PCI_ANY_ID,
4278 MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
4280 static struct pci_driver qla4xxx_pci_driver = {
4281 .name = DRIVER_NAME,
4282 .id_table = qla4xxx_pci_tbl,
4283 .probe = qla4xxx_probe_adapter,
4284 .remove = qla4xxx_remove_adapter,
4285 .err_handler = &qla4xxx_err_handler,
4288 static int __init qla4xxx_module_init(void)
4292 /* Allocate cache for SRBs. */
4293 srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
4294 SLAB_HWCACHE_ALIGN, NULL);
4295 if (srb_cachep == NULL) {
4297 "%s: Unable to allocate SRB cache..."
4298 "Failing load!\n", DRIVER_NAME);
4303 /* Derive version string. */
4304 strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
4305 if (ql4xextended_error_logging)
4306 strcat(qla4xxx_version_str, "-debug");
4308 qla4xxx_scsi_transport =
4309 iscsi_register_transport(&qla4xxx_iscsi_transport);
4310 if (!qla4xxx_scsi_transport){
4312 goto release_srb_cache;
4315 ret = pci_register_driver(&qla4xxx_pci_driver);
4317 goto unregister_transport;
4319 printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
4322 unregister_transport:
4323 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
4325 kmem_cache_destroy(srb_cachep);
4330 static void __exit qla4xxx_module_exit(void)
4332 pci_unregister_driver(&qla4xxx_pci_driver);
4333 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
4334 kmem_cache_destroy(srb_cachep);
4337 module_init(qla4xxx_module_init);
4338 module_exit(qla4xxx_module_exit);
4340 MODULE_AUTHOR("QLogic Corporation");
4341 MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
4342 MODULE_LICENSE("GPL");
4343 MODULE_VERSION(QLA4XXX_DRIVER_VERSION);