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>
11 #include <linux/inet.h>
13 #include <scsi/scsi_tcq.h>
14 #include <scsi/scsicam.h>
17 #include "ql4_version.h"
20 #include "ql4_inline.h"
25 static char qla4xxx_version_str[40];
28 * SRB allocation cache
30 static struct kmem_cache *srb_cachep;
33 * Module parameter information and variables
35 int ql4xdisablesysfsboot = 1;
36 module_param(ql4xdisablesysfsboot, int, S_IRUGO | S_IWUSR);
37 MODULE_PARM_DESC(ql4xdisablesysfsboot,
38 "Set to disable exporting boot targets to sysfs\n"
39 " 0 - Export boot targets\n"
40 " 1 - Do not export boot targets (Default)");
42 int ql4xdontresethba = 0;
43 module_param(ql4xdontresethba, int, S_IRUGO | S_IWUSR);
44 MODULE_PARM_DESC(ql4xdontresethba,
45 "Don't reset the HBA for driver recovery \n"
46 " 0 - It will reset HBA (Default)\n"
47 " 1 - It will NOT reset HBA");
49 int ql4xextended_error_logging = 0; /* 0 = off, 1 = log errors */
50 module_param(ql4xextended_error_logging, int, S_IRUGO | S_IWUSR);
51 MODULE_PARM_DESC(ql4xextended_error_logging,
52 "Option to enable extended error logging, "
53 "Default is 0 - no logging, 1 - debug logging");
55 int ql4xenablemsix = 1;
56 module_param(ql4xenablemsix, int, S_IRUGO|S_IWUSR);
57 MODULE_PARM_DESC(ql4xenablemsix,
58 "Set to enable MSI or MSI-X interrupt mechanism.\n"
59 " 0 = enable INTx interrupt mechanism.\n"
60 " 1 = enable MSI-X interrupt mechanism (Default).\n"
61 " 2 = enable MSI interrupt mechanism.");
63 #define QL4_DEF_QDEPTH 32
64 static int ql4xmaxqdepth = QL4_DEF_QDEPTH;
65 module_param(ql4xmaxqdepth, int, S_IRUGO | S_IWUSR);
66 MODULE_PARM_DESC(ql4xmaxqdepth,
67 "Maximum queue depth to report for target devices.\n"
70 static int ql4xsess_recovery_tmo = QL4_SESS_RECOVERY_TMO;
71 module_param(ql4xsess_recovery_tmo, int, S_IRUGO);
72 MODULE_PARM_DESC(ql4xsess_recovery_tmo,
73 "Target Session Recovery Timeout.\n"
74 " Default: 120 sec.");
76 static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha);
78 * SCSI host template entry points
80 static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
83 * iSCSI template entry points
85 static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
86 enum iscsi_param param, char *buf);
87 static int qla4xxx_host_get_param(struct Scsi_Host *shost,
88 enum iscsi_host_param param, char *buf);
89 static int qla4xxx_iface_set_param(struct Scsi_Host *shost, void *data,
91 static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
92 enum iscsi_param_type param_type,
93 int param, char *buf);
94 static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
95 static struct iscsi_endpoint *qla4xxx_ep_connect(struct Scsi_Host *shost,
96 struct sockaddr *dst_addr,
98 static int qla4xxx_ep_poll(struct iscsi_endpoint *ep, int timeout_ms);
99 static void qla4xxx_ep_disconnect(struct iscsi_endpoint *ep);
100 static int qla4xxx_get_ep_param(struct iscsi_endpoint *ep,
101 enum iscsi_param param, char *buf);
102 static int qla4xxx_conn_start(struct iscsi_cls_conn *conn);
103 static struct iscsi_cls_conn *
104 qla4xxx_conn_create(struct iscsi_cls_session *cls_sess, uint32_t conn_idx);
105 static int qla4xxx_conn_bind(struct iscsi_cls_session *cls_session,
106 struct iscsi_cls_conn *cls_conn,
107 uint64_t transport_fd, int is_leading);
108 static void qla4xxx_conn_destroy(struct iscsi_cls_conn *conn);
109 static struct iscsi_cls_session *
110 qla4xxx_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max,
111 uint16_t qdepth, uint32_t initial_cmdsn);
112 static void qla4xxx_session_destroy(struct iscsi_cls_session *sess);
113 static void qla4xxx_task_work(struct work_struct *wdata);
114 static int qla4xxx_alloc_pdu(struct iscsi_task *, uint8_t);
115 static int qla4xxx_task_xmit(struct iscsi_task *);
116 static void qla4xxx_task_cleanup(struct iscsi_task *);
117 static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session);
118 static void qla4xxx_conn_get_stats(struct iscsi_cls_conn *cls_conn,
119 struct iscsi_stats *stats);
121 * SCSI host template entry points
123 static int qla4xxx_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmd);
124 static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
125 static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
126 static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
127 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
128 static int qla4xxx_slave_alloc(struct scsi_device *device);
129 static int qla4xxx_slave_configure(struct scsi_device *device);
130 static void qla4xxx_slave_destroy(struct scsi_device *sdev);
131 static mode_t ql4_attr_is_visible(int param_type, int param);
132 static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type);
134 static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
135 QLA82XX_LEGACY_INTR_CONFIG;
137 static struct scsi_host_template qla4xxx_driver_template = {
138 .module = THIS_MODULE,
140 .proc_name = DRIVER_NAME,
141 .queuecommand = qla4xxx_queuecommand,
143 .eh_abort_handler = qla4xxx_eh_abort,
144 .eh_device_reset_handler = qla4xxx_eh_device_reset,
145 .eh_target_reset_handler = qla4xxx_eh_target_reset,
146 .eh_host_reset_handler = qla4xxx_eh_host_reset,
147 .eh_timed_out = qla4xxx_eh_cmd_timed_out,
149 .slave_configure = qla4xxx_slave_configure,
150 .slave_alloc = qla4xxx_slave_alloc,
151 .slave_destroy = qla4xxx_slave_destroy,
155 .use_clustering = ENABLE_CLUSTERING,
156 .sg_tablesize = SG_ALL,
158 .max_sectors = 0xFFFF,
159 .shost_attrs = qla4xxx_host_attrs,
160 .host_reset = qla4xxx_host_reset,
161 .vendor_id = SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_QLOGIC,
164 static struct iscsi_transport qla4xxx_iscsi_transport = {
165 .owner = THIS_MODULE,
167 .caps = CAP_TEXT_NEGO |
168 CAP_DATA_PATH_OFFLOAD | CAP_HDRDGST |
169 CAP_DATADGST | CAP_LOGIN_OFFLOAD |
171 .attr_is_visible = ql4_attr_is_visible,
172 .create_session = qla4xxx_session_create,
173 .destroy_session = qla4xxx_session_destroy,
174 .start_conn = qla4xxx_conn_start,
175 .create_conn = qla4xxx_conn_create,
176 .bind_conn = qla4xxx_conn_bind,
177 .stop_conn = iscsi_conn_stop,
178 .destroy_conn = qla4xxx_conn_destroy,
179 .set_param = iscsi_set_param,
180 .get_conn_param = qla4xxx_conn_get_param,
181 .get_session_param = iscsi_session_get_param,
182 .get_ep_param = qla4xxx_get_ep_param,
183 .ep_connect = qla4xxx_ep_connect,
184 .ep_poll = qla4xxx_ep_poll,
185 .ep_disconnect = qla4xxx_ep_disconnect,
186 .get_stats = qla4xxx_conn_get_stats,
187 .send_pdu = iscsi_conn_send_pdu,
188 .xmit_task = qla4xxx_task_xmit,
189 .cleanup_task = qla4xxx_task_cleanup,
190 .alloc_pdu = qla4xxx_alloc_pdu,
192 .get_host_param = qla4xxx_host_get_param,
193 .set_iface_param = qla4xxx_iface_set_param,
194 .get_iface_param = qla4xxx_get_iface_param,
195 .bsg_request = qla4xxx_bsg_request,
198 static struct scsi_transport_template *qla4xxx_scsi_transport;
200 static mode_t ql4_attr_is_visible(int param_type, int param)
202 switch (param_type) {
203 case ISCSI_HOST_PARAM:
205 case ISCSI_HOST_PARAM_HWADDRESS:
206 case ISCSI_HOST_PARAM_IPADDRESS:
207 case ISCSI_HOST_PARAM_INITIATOR_NAME:
214 case ISCSI_PARAM_PERSISTENT_ADDRESS:
215 case ISCSI_PARAM_PERSISTENT_PORT:
216 case ISCSI_PARAM_CONN_ADDRESS:
217 case ISCSI_PARAM_CONN_PORT:
218 case ISCSI_PARAM_TARGET_NAME:
219 case ISCSI_PARAM_TPGT:
220 case ISCSI_PARAM_TARGET_ALIAS:
221 case ISCSI_PARAM_MAX_BURST:
222 case ISCSI_PARAM_MAX_R2T:
223 case ISCSI_PARAM_FIRST_BURST:
224 case ISCSI_PARAM_MAX_RECV_DLENGTH:
225 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
226 case ISCSI_PARAM_IFACE_NAME:
231 case ISCSI_NET_PARAM:
233 case ISCSI_NET_PARAM_IPV4_ADDR:
234 case ISCSI_NET_PARAM_IPV4_SUBNET:
235 case ISCSI_NET_PARAM_IPV4_GW:
236 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
237 case ISCSI_NET_PARAM_IFACE_ENABLE:
238 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
239 case ISCSI_NET_PARAM_IPV6_ADDR:
240 case ISCSI_NET_PARAM_IPV6_ROUTER:
241 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
242 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
243 case ISCSI_NET_PARAM_VLAN_ID:
244 case ISCSI_NET_PARAM_VLAN_PRIORITY:
245 case ISCSI_NET_PARAM_VLAN_ENABLED:
246 case ISCSI_NET_PARAM_MTU:
247 case ISCSI_NET_PARAM_PORT:
257 static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
258 enum iscsi_param_type param_type,
259 int param, char *buf)
261 struct Scsi_Host *shost = iscsi_iface_to_shost(iface);
262 struct scsi_qla_host *ha = to_qla_host(shost);
265 if (param_type != ISCSI_NET_PARAM)
269 case ISCSI_NET_PARAM_IPV4_ADDR:
270 len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
272 case ISCSI_NET_PARAM_IPV4_SUBNET:
273 len = sprintf(buf, "%pI4\n", &ha->ip_config.subnet_mask);
275 case ISCSI_NET_PARAM_IPV4_GW:
276 len = sprintf(buf, "%pI4\n", &ha->ip_config.gateway);
278 case ISCSI_NET_PARAM_IFACE_ENABLE:
279 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
280 len = sprintf(buf, "%s\n",
281 (ha->ip_config.ipv4_options &
282 IPOPT_IPV4_PROTOCOL_ENABLE) ?
283 "enabled" : "disabled");
284 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
285 len = sprintf(buf, "%s\n",
286 (ha->ip_config.ipv6_options &
287 IPV6_OPT_IPV6_PROTOCOL_ENABLE) ?
288 "enabled" : "disabled");
290 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
291 len = sprintf(buf, "%s\n",
292 (ha->ip_config.tcp_options & TCPOPT_DHCP_ENABLE) ?
295 case ISCSI_NET_PARAM_IPV6_ADDR:
296 if (iface->iface_num == 0)
297 len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr0);
298 if (iface->iface_num == 1)
299 len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr1);
301 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
302 len = sprintf(buf, "%pI6\n",
303 &ha->ip_config.ipv6_link_local_addr);
305 case ISCSI_NET_PARAM_IPV6_ROUTER:
306 len = sprintf(buf, "%pI6\n",
307 &ha->ip_config.ipv6_default_router_addr);
309 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
310 len = sprintf(buf, "%s\n",
311 (ha->ip_config.ipv6_addl_options &
312 IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) ?
315 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
316 len = sprintf(buf, "%s\n",
317 (ha->ip_config.ipv6_addl_options &
318 IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR) ?
321 case ISCSI_NET_PARAM_VLAN_ID:
322 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
323 len = sprintf(buf, "%d\n",
324 (ha->ip_config.ipv4_vlan_tag &
326 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
327 len = sprintf(buf, "%d\n",
328 (ha->ip_config.ipv6_vlan_tag &
331 case ISCSI_NET_PARAM_VLAN_PRIORITY:
332 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
333 len = sprintf(buf, "%d\n",
334 ((ha->ip_config.ipv4_vlan_tag >> 13) &
335 ISCSI_MAX_VLAN_PRIORITY));
336 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
337 len = sprintf(buf, "%d\n",
338 ((ha->ip_config.ipv6_vlan_tag >> 13) &
339 ISCSI_MAX_VLAN_PRIORITY));
341 case ISCSI_NET_PARAM_VLAN_ENABLED:
342 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
343 len = sprintf(buf, "%s\n",
344 (ha->ip_config.ipv4_options &
345 IPOPT_VLAN_TAGGING_ENABLE) ?
346 "enabled" : "disabled");
347 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
348 len = sprintf(buf, "%s\n",
349 (ha->ip_config.ipv6_options &
350 IPV6_OPT_VLAN_TAGGING_ENABLE) ?
351 "enabled" : "disabled");
353 case ISCSI_NET_PARAM_MTU:
354 len = sprintf(buf, "%d\n", ha->ip_config.eth_mtu_size);
356 case ISCSI_NET_PARAM_PORT:
357 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
358 len = sprintf(buf, "%d\n", ha->ip_config.ipv4_port);
359 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
360 len = sprintf(buf, "%d\n", ha->ip_config.ipv6_port);
369 static struct iscsi_endpoint *
370 qla4xxx_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
374 struct iscsi_endpoint *ep;
375 struct qla_endpoint *qla_ep;
376 struct scsi_qla_host *ha;
377 struct sockaddr_in *addr;
378 struct sockaddr_in6 *addr6;
380 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
383 printk(KERN_ERR "%s: shost is NULL\n",
388 ha = iscsi_host_priv(shost);
390 ep = iscsi_create_endpoint(sizeof(struct qla_endpoint));
396 qla_ep = ep->dd_data;
397 memset(qla_ep, 0, sizeof(struct qla_endpoint));
398 if (dst_addr->sa_family == AF_INET) {
399 memcpy(&qla_ep->dst_addr, dst_addr, sizeof(struct sockaddr_in));
400 addr = (struct sockaddr_in *)&qla_ep->dst_addr;
401 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI4\n", __func__,
402 (char *)&addr->sin_addr));
403 } else if (dst_addr->sa_family == AF_INET6) {
404 memcpy(&qla_ep->dst_addr, dst_addr,
405 sizeof(struct sockaddr_in6));
406 addr6 = (struct sockaddr_in6 *)&qla_ep->dst_addr;
407 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI6\n", __func__,
408 (char *)&addr6->sin6_addr));
411 qla_ep->host = shost;
416 static int qla4xxx_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
418 struct qla_endpoint *qla_ep;
419 struct scsi_qla_host *ha;
422 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
423 qla_ep = ep->dd_data;
424 ha = to_qla_host(qla_ep->host);
426 if (adapter_up(ha) && !test_bit(AF_BUILD_DDB_LIST, &ha->flags))
432 static void qla4xxx_ep_disconnect(struct iscsi_endpoint *ep)
434 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
435 iscsi_destroy_endpoint(ep);
438 static int qla4xxx_get_ep_param(struct iscsi_endpoint *ep,
439 enum iscsi_param param,
442 struct qla_endpoint *qla_ep = ep->dd_data;
443 struct sockaddr *dst_addr;
445 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
448 case ISCSI_PARAM_CONN_PORT:
449 case ISCSI_PARAM_CONN_ADDRESS:
453 dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
457 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
458 &qla_ep->dst_addr, param, buf);
464 static void qla4xxx_conn_get_stats(struct iscsi_cls_conn *cls_conn,
465 struct iscsi_stats *stats)
467 struct iscsi_session *sess;
468 struct iscsi_cls_session *cls_sess;
469 struct ddb_entry *ddb_entry;
470 struct scsi_qla_host *ha;
471 struct ql_iscsi_stats *ql_iscsi_stats;
474 dma_addr_t iscsi_stats_dma;
476 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
478 cls_sess = iscsi_conn_to_session(cls_conn);
479 sess = cls_sess->dd_data;
480 ddb_entry = sess->dd_data;
483 stats_size = PAGE_ALIGN(sizeof(struct ql_iscsi_stats));
484 /* Allocate memory */
485 ql_iscsi_stats = dma_alloc_coherent(&ha->pdev->dev, stats_size,
486 &iscsi_stats_dma, GFP_KERNEL);
487 if (!ql_iscsi_stats) {
488 ql4_printk(KERN_ERR, ha,
489 "Unable to allocate memory for iscsi stats\n");
493 ret = qla4xxx_get_mgmt_data(ha, ddb_entry->fw_ddb_index, stats_size,
495 if (ret != QLA_SUCCESS) {
496 ql4_printk(KERN_ERR, ha,
497 "Unable to retreive iscsi stats\n");
502 stats->txdata_octets = le64_to_cpu(ql_iscsi_stats->tx_data_octets);
503 stats->rxdata_octets = le64_to_cpu(ql_iscsi_stats->rx_data_octets);
505 stats->noptx_pdus = le32_to_cpu(ql_iscsi_stats->tx_nopout_pdus);
506 stats->scsicmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_cmd_pdus);
507 stats->tmfcmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_tmf_cmd_pdus);
508 stats->login_pdus = le32_to_cpu(ql_iscsi_stats->tx_login_cmd_pdus);
509 stats->text_pdus = le32_to_cpu(ql_iscsi_stats->tx_text_cmd_pdus);
510 stats->dataout_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_write_pdus);
511 stats->logout_pdus = le32_to_cpu(ql_iscsi_stats->tx_logout_cmd_pdus);
512 stats->snack_pdus = le32_to_cpu(ql_iscsi_stats->tx_snack_req_pdus);
514 stats->noprx_pdus = le32_to_cpu(ql_iscsi_stats->rx_nopin_pdus);
515 stats->scsirsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_resp_pdus);
516 stats->tmfrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_tmf_resp_pdus);
517 stats->textrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_text_resp_pdus);
518 stats->datain_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_read_pdus);
519 stats->logoutrsp_pdus =
520 le32_to_cpu(ql_iscsi_stats->rx_logout_resp_pdus);
521 stats->r2t_pdus = le32_to_cpu(ql_iscsi_stats->rx_r2t_pdus);
522 stats->async_pdus = le32_to_cpu(ql_iscsi_stats->rx_async_pdus);
523 stats->rjt_pdus = le32_to_cpu(ql_iscsi_stats->rx_reject_pdus);
526 dma_free_coherent(&ha->pdev->dev, stats_size, ql_iscsi_stats,
532 static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
534 struct iscsi_cls_session *session;
535 struct iscsi_session *sess;
537 enum blk_eh_timer_return ret = BLK_EH_NOT_HANDLED;
539 session = starget_to_session(scsi_target(sc->device));
540 sess = session->dd_data;
542 spin_lock_irqsave(&session->lock, flags);
543 if (session->state == ISCSI_SESSION_FAILED)
544 ret = BLK_EH_RESET_TIMER;
545 spin_unlock_irqrestore(&session->lock, flags);
550 static int qla4xxx_host_get_param(struct Scsi_Host *shost,
551 enum iscsi_host_param param, char *buf)
553 struct scsi_qla_host *ha = to_qla_host(shost);
557 case ISCSI_HOST_PARAM_HWADDRESS:
558 len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
560 case ISCSI_HOST_PARAM_IPADDRESS:
561 len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
563 case ISCSI_HOST_PARAM_INITIATOR_NAME:
564 len = sprintf(buf, "%s\n", ha->name_string);
573 static void qla4xxx_create_ipv4_iface(struct scsi_qla_host *ha)
579 ha->iface_ipv4 = iscsi_create_iface(ha->host,
580 &qla4xxx_iscsi_transport,
581 ISCSI_IFACE_TYPE_IPV4, 0, 0);
583 ql4_printk(KERN_ERR, ha, "Could not create IPv4 iSCSI "
587 static void qla4xxx_create_ipv6_iface(struct scsi_qla_host *ha)
589 if (!ha->iface_ipv6_0)
591 ha->iface_ipv6_0 = iscsi_create_iface(ha->host,
592 &qla4xxx_iscsi_transport,
593 ISCSI_IFACE_TYPE_IPV6, 0,
595 if (!ha->iface_ipv6_0)
596 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
599 if (!ha->iface_ipv6_1)
601 ha->iface_ipv6_1 = iscsi_create_iface(ha->host,
602 &qla4xxx_iscsi_transport,
603 ISCSI_IFACE_TYPE_IPV6, 1,
605 if (!ha->iface_ipv6_1)
606 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
610 static void qla4xxx_create_ifaces(struct scsi_qla_host *ha)
612 if (ha->ip_config.ipv4_options & IPOPT_IPV4_PROTOCOL_ENABLE)
613 qla4xxx_create_ipv4_iface(ha);
615 if (ha->ip_config.ipv6_options & IPV6_OPT_IPV6_PROTOCOL_ENABLE)
616 qla4xxx_create_ipv6_iface(ha);
619 static void qla4xxx_destroy_ipv4_iface(struct scsi_qla_host *ha)
621 if (ha->iface_ipv4) {
622 iscsi_destroy_iface(ha->iface_ipv4);
623 ha->iface_ipv4 = NULL;
627 static void qla4xxx_destroy_ipv6_iface(struct scsi_qla_host *ha)
629 if (ha->iface_ipv6_0) {
630 iscsi_destroy_iface(ha->iface_ipv6_0);
631 ha->iface_ipv6_0 = NULL;
633 if (ha->iface_ipv6_1) {
634 iscsi_destroy_iface(ha->iface_ipv6_1);
635 ha->iface_ipv6_1 = NULL;
639 static void qla4xxx_destroy_ifaces(struct scsi_qla_host *ha)
641 qla4xxx_destroy_ipv4_iface(ha);
642 qla4xxx_destroy_ipv6_iface(ha);
645 static void qla4xxx_set_ipv6(struct scsi_qla_host *ha,
646 struct iscsi_iface_param_info *iface_param,
647 struct addr_ctrl_blk *init_fw_cb)
650 * iface_num 0 is valid for IPv6 Addr, linklocal, router, autocfg.
651 * iface_num 1 is valid only for IPv6 Addr.
653 switch (iface_param->param) {
654 case ISCSI_NET_PARAM_IPV6_ADDR:
655 if (iface_param->iface_num & 0x1)
657 memcpy(init_fw_cb->ipv6_addr1, iface_param->value,
658 sizeof(init_fw_cb->ipv6_addr1));
661 memcpy(init_fw_cb->ipv6_addr0, iface_param->value,
662 sizeof(init_fw_cb->ipv6_addr0));
664 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
665 if (iface_param->iface_num & 0x1)
667 memcpy(init_fw_cb->ipv6_if_id, &iface_param->value[8],
668 sizeof(init_fw_cb->ipv6_if_id));
670 case ISCSI_NET_PARAM_IPV6_ROUTER:
671 if (iface_param->iface_num & 0x1)
673 memcpy(init_fw_cb->ipv6_dflt_rtr_addr, iface_param->value,
674 sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
676 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
677 /* Autocfg applies to even interface */
678 if (iface_param->iface_num & 0x1)
681 if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_DISABLE)
682 init_fw_cb->ipv6_addtl_opts &=
684 ~IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
685 else if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_ND_ENABLE)
686 init_fw_cb->ipv6_addtl_opts |=
688 IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
690 ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
693 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
694 /* Autocfg applies to even interface */
695 if (iface_param->iface_num & 0x1)
698 if (iface_param->value[0] ==
699 ISCSI_IPV6_LINKLOCAL_AUTOCFG_ENABLE)
700 init_fw_cb->ipv6_addtl_opts |= cpu_to_le16(
701 IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
702 else if (iface_param->value[0] ==
703 ISCSI_IPV6_LINKLOCAL_AUTOCFG_DISABLE)
704 init_fw_cb->ipv6_addtl_opts &= cpu_to_le16(
705 ~IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
707 ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
708 "IPv6 linklocal addr\n");
710 case ISCSI_NET_PARAM_IPV6_ROUTER_AUTOCFG:
711 /* Autocfg applies to even interface */
712 if (iface_param->iface_num & 0x1)
715 if (iface_param->value[0] == ISCSI_IPV6_ROUTER_AUTOCFG_ENABLE)
716 memset(init_fw_cb->ipv6_dflt_rtr_addr, 0,
717 sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
719 case ISCSI_NET_PARAM_IFACE_ENABLE:
720 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
721 init_fw_cb->ipv6_opts |=
722 cpu_to_le16(IPV6_OPT_IPV6_PROTOCOL_ENABLE);
723 qla4xxx_create_ipv6_iface(ha);
725 init_fw_cb->ipv6_opts &=
726 cpu_to_le16(~IPV6_OPT_IPV6_PROTOCOL_ENABLE &
728 qla4xxx_destroy_ipv6_iface(ha);
731 case ISCSI_NET_PARAM_VLAN_TAG:
732 if (iface_param->len != sizeof(init_fw_cb->ipv6_vlan_tag))
734 init_fw_cb->ipv6_vlan_tag =
735 cpu_to_be16(*(uint16_t *)iface_param->value);
737 case ISCSI_NET_PARAM_VLAN_ENABLED:
738 if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
739 init_fw_cb->ipv6_opts |=
740 cpu_to_le16(IPV6_OPT_VLAN_TAGGING_ENABLE);
742 init_fw_cb->ipv6_opts &=
743 cpu_to_le16(~IPV6_OPT_VLAN_TAGGING_ENABLE);
745 case ISCSI_NET_PARAM_MTU:
746 init_fw_cb->eth_mtu_size =
747 cpu_to_le16(*(uint16_t *)iface_param->value);
749 case ISCSI_NET_PARAM_PORT:
750 /* Autocfg applies to even interface */
751 if (iface_param->iface_num & 0x1)
754 init_fw_cb->ipv6_port =
755 cpu_to_le16(*(uint16_t *)iface_param->value);
758 ql4_printk(KERN_ERR, ha, "Unknown IPv6 param = %d\n",
764 static void qla4xxx_set_ipv4(struct scsi_qla_host *ha,
765 struct iscsi_iface_param_info *iface_param,
766 struct addr_ctrl_blk *init_fw_cb)
768 switch (iface_param->param) {
769 case ISCSI_NET_PARAM_IPV4_ADDR:
770 memcpy(init_fw_cb->ipv4_addr, iface_param->value,
771 sizeof(init_fw_cb->ipv4_addr));
773 case ISCSI_NET_PARAM_IPV4_SUBNET:
774 memcpy(init_fw_cb->ipv4_subnet, iface_param->value,
775 sizeof(init_fw_cb->ipv4_subnet));
777 case ISCSI_NET_PARAM_IPV4_GW:
778 memcpy(init_fw_cb->ipv4_gw_addr, iface_param->value,
779 sizeof(init_fw_cb->ipv4_gw_addr));
781 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
782 if (iface_param->value[0] == ISCSI_BOOTPROTO_DHCP)
783 init_fw_cb->ipv4_tcp_opts |=
784 cpu_to_le16(TCPOPT_DHCP_ENABLE);
785 else if (iface_param->value[0] == ISCSI_BOOTPROTO_STATIC)
786 init_fw_cb->ipv4_tcp_opts &=
787 cpu_to_le16(~TCPOPT_DHCP_ENABLE);
789 ql4_printk(KERN_ERR, ha, "Invalid IPv4 bootproto\n");
791 case ISCSI_NET_PARAM_IFACE_ENABLE:
792 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
793 init_fw_cb->ipv4_ip_opts |=
794 cpu_to_le16(IPOPT_IPV4_PROTOCOL_ENABLE);
795 qla4xxx_create_ipv4_iface(ha);
797 init_fw_cb->ipv4_ip_opts &=
798 cpu_to_le16(~IPOPT_IPV4_PROTOCOL_ENABLE &
800 qla4xxx_destroy_ipv4_iface(ha);
803 case ISCSI_NET_PARAM_VLAN_TAG:
804 if (iface_param->len != sizeof(init_fw_cb->ipv4_vlan_tag))
806 init_fw_cb->ipv4_vlan_tag =
807 cpu_to_be16(*(uint16_t *)iface_param->value);
809 case ISCSI_NET_PARAM_VLAN_ENABLED:
810 if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
811 init_fw_cb->ipv4_ip_opts |=
812 cpu_to_le16(IPOPT_VLAN_TAGGING_ENABLE);
814 init_fw_cb->ipv4_ip_opts &=
815 cpu_to_le16(~IPOPT_VLAN_TAGGING_ENABLE);
817 case ISCSI_NET_PARAM_MTU:
818 init_fw_cb->eth_mtu_size =
819 cpu_to_le16(*(uint16_t *)iface_param->value);
821 case ISCSI_NET_PARAM_PORT:
822 init_fw_cb->ipv4_port =
823 cpu_to_le16(*(uint16_t *)iface_param->value);
826 ql4_printk(KERN_ERR, ha, "Unknown IPv4 param = %d\n",
833 qla4xxx_initcb_to_acb(struct addr_ctrl_blk *init_fw_cb)
835 struct addr_ctrl_blk_def *acb;
836 acb = (struct addr_ctrl_blk_def *)init_fw_cb;
837 memset(acb->reserved1, 0, sizeof(acb->reserved1));
838 memset(acb->reserved2, 0, sizeof(acb->reserved2));
839 memset(acb->reserved3, 0, sizeof(acb->reserved3));
840 memset(acb->reserved4, 0, sizeof(acb->reserved4));
841 memset(acb->reserved5, 0, sizeof(acb->reserved5));
842 memset(acb->reserved6, 0, sizeof(acb->reserved6));
843 memset(acb->reserved7, 0, sizeof(acb->reserved7));
844 memset(acb->reserved8, 0, sizeof(acb->reserved8));
845 memset(acb->reserved9, 0, sizeof(acb->reserved9));
846 memset(acb->reserved10, 0, sizeof(acb->reserved10));
847 memset(acb->reserved11, 0, sizeof(acb->reserved11));
848 memset(acb->reserved12, 0, sizeof(acb->reserved12));
849 memset(acb->reserved13, 0, sizeof(acb->reserved13));
850 memset(acb->reserved14, 0, sizeof(acb->reserved14));
851 memset(acb->reserved15, 0, sizeof(acb->reserved15));
855 qla4xxx_iface_set_param(struct Scsi_Host *shost, void *data, uint32_t len)
857 struct scsi_qla_host *ha = to_qla_host(shost);
859 struct iscsi_iface_param_info *iface_param = NULL;
860 struct addr_ctrl_blk *init_fw_cb = NULL;
861 dma_addr_t init_fw_cb_dma;
862 uint32_t mbox_cmd[MBOX_REG_COUNT];
863 uint32_t mbox_sts[MBOX_REG_COUNT];
867 init_fw_cb = dma_alloc_coherent(&ha->pdev->dev,
868 sizeof(struct addr_ctrl_blk),
869 &init_fw_cb_dma, GFP_KERNEL);
871 ql4_printk(KERN_ERR, ha, "%s: Unable to alloc init_cb\n",
876 memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
877 memset(&mbox_cmd, 0, sizeof(mbox_cmd));
878 memset(&mbox_sts, 0, sizeof(mbox_sts));
880 if (qla4xxx_get_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma)) {
881 ql4_printk(KERN_ERR, ha, "%s: get ifcb failed\n", __func__);
883 goto exit_init_fw_cb;
886 nla_for_each_attr(attr, data, len, rem) {
887 iface_param = nla_data(attr);
889 if (iface_param->param_type != ISCSI_NET_PARAM)
892 switch (iface_param->iface_type) {
893 case ISCSI_IFACE_TYPE_IPV4:
894 switch (iface_param->iface_num) {
896 qla4xxx_set_ipv4(ha, iface_param, init_fw_cb);
899 /* Cannot have more than one IPv4 interface */
900 ql4_printk(KERN_ERR, ha, "Invalid IPv4 iface "
902 iface_param->iface_num);
906 case ISCSI_IFACE_TYPE_IPV6:
907 switch (iface_param->iface_num) {
910 qla4xxx_set_ipv6(ha, iface_param, init_fw_cb);
913 /* Cannot have more than two IPv6 interface */
914 ql4_printk(KERN_ERR, ha, "Invalid IPv6 iface "
916 iface_param->iface_num);
921 ql4_printk(KERN_ERR, ha, "Invalid iface type\n");
926 init_fw_cb->cookie = cpu_to_le32(0x11BEAD5A);
928 rval = qla4xxx_set_flash(ha, init_fw_cb_dma, FLASH_SEGMENT_IFCB,
929 sizeof(struct addr_ctrl_blk),
930 FLASH_OPT_RMW_COMMIT);
931 if (rval != QLA_SUCCESS) {
932 ql4_printk(KERN_ERR, ha, "%s: set flash mbx failed\n",
935 goto exit_init_fw_cb;
938 qla4xxx_disable_acb(ha);
940 qla4xxx_initcb_to_acb(init_fw_cb);
942 rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma);
943 if (rval != QLA_SUCCESS) {
944 ql4_printk(KERN_ERR, ha, "%s: set acb mbx failed\n",
947 goto exit_init_fw_cb;
950 memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
951 qla4xxx_update_local_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb,
955 dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk),
956 init_fw_cb, init_fw_cb_dma);
961 static int qla4xxx_conn_get_param(struct iscsi_cls_conn *cls_conn,
962 enum iscsi_param param, char *buf)
964 struct iscsi_conn *conn;
965 struct qla_conn *qla_conn;
966 struct sockaddr *dst_addr;
969 conn = cls_conn->dd_data;
970 qla_conn = conn->dd_data;
971 dst_addr = &qla_conn->qla_ep->dst_addr;
974 case ISCSI_PARAM_CONN_PORT:
975 case ISCSI_PARAM_CONN_ADDRESS:
976 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
977 dst_addr, param, buf);
979 return iscsi_conn_get_param(cls_conn, param, buf);
986 int qla4xxx_get_ddb_index(struct scsi_qla_host *ha, uint16_t *ddb_index)
988 uint32_t mbx_sts = 0;
989 uint16_t tmp_ddb_index;
993 tmp_ddb_index = find_first_zero_bit(ha->ddb_idx_map, MAX_DDB_ENTRIES);
995 if (tmp_ddb_index >= MAX_DDB_ENTRIES) {
996 DEBUG2(ql4_printk(KERN_INFO, ha,
997 "Free DDB index not available\n"));
999 goto exit_get_ddb_index;
1002 if (test_and_set_bit(tmp_ddb_index, ha->ddb_idx_map))
1005 DEBUG2(ql4_printk(KERN_INFO, ha,
1006 "Found a free DDB index at %d\n", tmp_ddb_index));
1007 ret = qla4xxx_req_ddb_entry(ha, tmp_ddb_index, &mbx_sts);
1008 if (ret == QLA_ERROR) {
1009 if (mbx_sts == MBOX_STS_COMMAND_ERROR) {
1010 ql4_printk(KERN_INFO, ha,
1011 "DDB index = %d not available trying next\n",
1015 DEBUG2(ql4_printk(KERN_INFO, ha,
1016 "Free FW DDB not available\n"));
1019 *ddb_index = tmp_ddb_index;
1025 static int qla4xxx_match_ipaddress(struct scsi_qla_host *ha,
1026 struct ddb_entry *ddb_entry,
1027 char *existing_ipaddr,
1030 uint8_t dst_ipaddr[IPv6_ADDR_LEN];
1031 char formatted_ipaddr[DDB_IPADDR_LEN];
1032 int status = QLA_SUCCESS, ret = 0;
1034 if (ddb_entry->fw_ddb_entry.options & DDB_OPT_IPV6_DEVICE) {
1035 ret = in6_pton(user_ipaddr, strlen(user_ipaddr), dst_ipaddr,
1041 ret = sprintf(formatted_ipaddr, "%pI6", dst_ipaddr);
1043 ret = in4_pton(user_ipaddr, strlen(user_ipaddr), dst_ipaddr,
1049 ret = sprintf(formatted_ipaddr, "%pI4", dst_ipaddr);
1052 if (strcmp(existing_ipaddr, formatted_ipaddr))
1059 static int qla4xxx_match_fwdb_session(struct scsi_qla_host *ha,
1060 struct iscsi_cls_conn *cls_conn)
1062 int idx = 0, max_ddbs, rval;
1063 struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
1064 struct iscsi_session *sess, *existing_sess;
1065 struct iscsi_conn *conn, *existing_conn;
1066 struct ddb_entry *ddb_entry;
1068 sess = cls_sess->dd_data;
1069 conn = cls_conn->dd_data;
1071 if (sess->targetname == NULL ||
1072 conn->persistent_address == NULL ||
1073 conn->persistent_port == 0)
1076 max_ddbs = is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
1079 for (idx = 0; idx < max_ddbs; idx++) {
1080 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
1081 if (ddb_entry == NULL)
1084 if (ddb_entry->ddb_type != FLASH_DDB)
1087 existing_sess = ddb_entry->sess->dd_data;
1088 existing_conn = ddb_entry->conn->dd_data;
1090 if (existing_sess->targetname == NULL ||
1091 existing_conn->persistent_address == NULL ||
1092 existing_conn->persistent_port == 0)
1095 DEBUG2(ql4_printk(KERN_INFO, ha,
1096 "IQN = %s User IQN = %s\n",
1097 existing_sess->targetname,
1100 DEBUG2(ql4_printk(KERN_INFO, ha,
1101 "IP = %s User IP = %s\n",
1102 existing_conn->persistent_address,
1103 conn->persistent_address));
1105 DEBUG2(ql4_printk(KERN_INFO, ha,
1106 "Port = %d User Port = %d\n",
1107 existing_conn->persistent_port,
1108 conn->persistent_port));
1110 if (strcmp(existing_sess->targetname, sess->targetname))
1112 rval = qla4xxx_match_ipaddress(ha, ddb_entry,
1113 existing_conn->persistent_address,
1114 conn->persistent_address);
1115 if (rval == QLA_ERROR)
1117 if (existing_conn->persistent_port != conn->persistent_port)
1122 if (idx == max_ddbs)
1125 DEBUG2(ql4_printk(KERN_INFO, ha,
1126 "Match found in fwdb sessions\n"));
1130 static struct iscsi_cls_session *
1131 qla4xxx_session_create(struct iscsi_endpoint *ep,
1132 uint16_t cmds_max, uint16_t qdepth,
1133 uint32_t initial_cmdsn)
1135 struct iscsi_cls_session *cls_sess;
1136 struct scsi_qla_host *ha;
1137 struct qla_endpoint *qla_ep;
1138 struct ddb_entry *ddb_entry;
1140 struct iscsi_session *sess;
1141 struct sockaddr *dst_addr;
1144 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1146 printk(KERN_ERR "qla4xxx: missing ep.\n");
1150 qla_ep = ep->dd_data;
1151 dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
1152 ha = to_qla_host(qla_ep->host);
1154 ret = qla4xxx_get_ddb_index(ha, &ddb_index);
1155 if (ret == QLA_ERROR)
1158 cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport, qla_ep->host,
1159 cmds_max, sizeof(struct ddb_entry),
1160 sizeof(struct ql4_task_data),
1161 initial_cmdsn, ddb_index);
1165 sess = cls_sess->dd_data;
1166 ddb_entry = sess->dd_data;
1167 ddb_entry->fw_ddb_index = ddb_index;
1168 ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
1170 ddb_entry->sess = cls_sess;
1171 ddb_entry->unblock_sess = qla4xxx_unblock_ddb;
1172 ddb_entry->ddb_change = qla4xxx_ddb_change;
1173 cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
1174 ha->fw_ddb_index_map[ddb_entry->fw_ddb_index] = ddb_entry;
1180 static void qla4xxx_session_destroy(struct iscsi_cls_session *cls_sess)
1182 struct iscsi_session *sess;
1183 struct ddb_entry *ddb_entry;
1184 struct scsi_qla_host *ha;
1185 unsigned long flags;
1187 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1188 sess = cls_sess->dd_data;
1189 ddb_entry = sess->dd_data;
1192 qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
1194 spin_lock_irqsave(&ha->hardware_lock, flags);
1195 qla4xxx_free_ddb(ha, ddb_entry);
1196 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1197 iscsi_session_teardown(cls_sess);
1200 static struct iscsi_cls_conn *
1201 qla4xxx_conn_create(struct iscsi_cls_session *cls_sess, uint32_t conn_idx)
1203 struct iscsi_cls_conn *cls_conn;
1204 struct iscsi_session *sess;
1205 struct ddb_entry *ddb_entry;
1207 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1208 cls_conn = iscsi_conn_setup(cls_sess, sizeof(struct qla_conn),
1213 sess = cls_sess->dd_data;
1214 ddb_entry = sess->dd_data;
1215 ddb_entry->conn = cls_conn;
1220 static int qla4xxx_conn_bind(struct iscsi_cls_session *cls_session,
1221 struct iscsi_cls_conn *cls_conn,
1222 uint64_t transport_fd, int is_leading)
1224 struct iscsi_conn *conn;
1225 struct qla_conn *qla_conn;
1226 struct iscsi_endpoint *ep;
1228 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1230 if (iscsi_conn_bind(cls_session, cls_conn, is_leading))
1232 ep = iscsi_lookup_endpoint(transport_fd);
1233 conn = cls_conn->dd_data;
1234 qla_conn = conn->dd_data;
1235 qla_conn->qla_ep = ep->dd_data;
1239 static int qla4xxx_conn_start(struct iscsi_cls_conn *cls_conn)
1241 struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
1242 struct iscsi_session *sess;
1243 struct ddb_entry *ddb_entry;
1244 struct scsi_qla_host *ha;
1245 struct dev_db_entry *fw_ddb_entry = NULL;
1246 dma_addr_t fw_ddb_entry_dma;
1247 uint32_t mbx_sts = 0;
1249 int status = QLA_SUCCESS;
1251 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1252 sess = cls_sess->dd_data;
1253 ddb_entry = sess->dd_data;
1256 /* Check if we have matching FW DDB, if yes then do not
1257 * login to this target. This could cause target to logout previous
1260 ret = qla4xxx_match_fwdb_session(ha, cls_conn);
1261 if (ret == QLA_SUCCESS) {
1262 ql4_printk(KERN_INFO, ha,
1263 "Session already exist in FW.\n");
1265 goto exit_conn_start;
1268 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1269 &fw_ddb_entry_dma, GFP_KERNEL);
1270 if (!fw_ddb_entry) {
1271 ql4_printk(KERN_ERR, ha,
1272 "%s: Unable to allocate dma buffer\n", __func__);
1274 goto exit_conn_start;
1277 ret = qla4xxx_set_param_ddbentry(ha, ddb_entry, cls_conn, &mbx_sts);
1279 /* If iscsid is stopped and started then no need to do
1280 * set param again since ddb state will be already
1281 * active and FW does not allow set ddb to an
1285 if (ddb_entry->fw_ddb_device_state ==
1286 DDB_DS_SESSION_ACTIVE) {
1287 ddb_entry->unblock_sess(ddb_entry->sess);
1288 goto exit_set_param;
1291 ql4_printk(KERN_ERR, ha, "%s: Failed set param for index[%d]\n",
1292 __func__, ddb_entry->fw_ddb_index);
1293 goto exit_conn_start;
1296 status = qla4xxx_conn_open(ha, ddb_entry->fw_ddb_index);
1297 if (status == QLA_ERROR) {
1298 ql4_printk(KERN_ERR, ha, "%s: Login failed: %s\n", __func__,
1301 goto exit_conn_start;
1304 if (ddb_entry->fw_ddb_device_state == DDB_DS_NO_CONNECTION_ACTIVE)
1305 ddb_entry->fw_ddb_device_state = DDB_DS_LOGIN_IN_PROCESS;
1307 DEBUG2(printk(KERN_INFO "%s: DDB state [%d]\n", __func__,
1308 ddb_entry->fw_ddb_device_state));
1315 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1316 fw_ddb_entry, fw_ddb_entry_dma);
1320 static void qla4xxx_conn_destroy(struct iscsi_cls_conn *cls_conn)
1322 struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
1323 struct iscsi_session *sess;
1324 struct scsi_qla_host *ha;
1325 struct ddb_entry *ddb_entry;
1328 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1329 sess = cls_sess->dd_data;
1330 ddb_entry = sess->dd_data;
1333 options = LOGOUT_OPTION_CLOSE_SESSION;
1334 if (qla4xxx_session_logout_ddb(ha, ddb_entry, options) == QLA_ERROR)
1335 ql4_printk(KERN_ERR, ha, "%s: Logout failed\n", __func__);
1338 static void qla4xxx_task_work(struct work_struct *wdata)
1340 struct ql4_task_data *task_data;
1341 struct scsi_qla_host *ha;
1342 struct passthru_status *sts;
1343 struct iscsi_task *task;
1344 struct iscsi_hdr *hdr;
1347 struct iscsi_conn *conn;
1351 task_data = container_of(wdata, struct ql4_task_data, task_work);
1353 task = task_data->task;
1354 sts = &task_data->sts;
1355 hdr_len = sizeof(struct iscsi_hdr);
1357 DEBUG3(printk(KERN_INFO "Status returned\n"));
1358 DEBUG3(qla4xxx_dump_buffer(sts, 64));
1359 DEBUG3(printk(KERN_INFO "Response buffer"));
1360 DEBUG3(qla4xxx_dump_buffer(task_data->resp_buffer, 64));
1364 switch (sts->completionStatus) {
1365 case PASSTHRU_STATUS_COMPLETE:
1366 hdr = (struct iscsi_hdr *)task_data->resp_buffer;
1367 /* Assign back the itt in hdr, until we use the PREASSIGN_TAG */
1370 data = task_data->resp_buffer + hdr_len;
1371 data_len = task_data->resp_len - hdr_len;
1372 iscsi_complete_pdu(conn, hdr, data, data_len);
1375 ql4_printk(KERN_ERR, ha, "Passthru failed status = 0x%x\n",
1376 sts->completionStatus);
1382 static int qla4xxx_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
1384 struct ql4_task_data *task_data;
1385 struct iscsi_session *sess;
1386 struct ddb_entry *ddb_entry;
1387 struct scsi_qla_host *ha;
1390 sess = task->conn->session;
1391 ddb_entry = sess->dd_data;
1393 task_data = task->dd_data;
1394 memset(task_data, 0, sizeof(struct ql4_task_data));
1397 ql4_printk(KERN_INFO, ha,
1398 "%s: SCSI Commands not implemented\n", __func__);
1402 hdr_len = sizeof(struct iscsi_hdr);
1404 task_data->task = task;
1406 if (task->data_count) {
1407 task_data->data_dma = dma_map_single(&ha->pdev->dev, task->data,
1412 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
1413 __func__, task->conn->max_recv_dlength, hdr_len));
1415 task_data->resp_len = task->conn->max_recv_dlength + hdr_len;
1416 task_data->resp_buffer = dma_alloc_coherent(&ha->pdev->dev,
1417 task_data->resp_len,
1418 &task_data->resp_dma,
1420 if (!task_data->resp_buffer)
1421 goto exit_alloc_pdu;
1423 task_data->req_len = task->data_count + hdr_len;
1424 task_data->req_buffer = dma_alloc_coherent(&ha->pdev->dev,
1426 &task_data->req_dma,
1428 if (!task_data->req_buffer)
1429 goto exit_alloc_pdu;
1431 task->hdr = task_data->req_buffer;
1433 INIT_WORK(&task_data->task_work, qla4xxx_task_work);
1438 if (task_data->resp_buffer)
1439 dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
1440 task_data->resp_buffer, task_data->resp_dma);
1442 if (task_data->req_buffer)
1443 dma_free_coherent(&ha->pdev->dev, task_data->req_len,
1444 task_data->req_buffer, task_data->req_dma);
1448 static void qla4xxx_task_cleanup(struct iscsi_task *task)
1450 struct ql4_task_data *task_data;
1451 struct iscsi_session *sess;
1452 struct ddb_entry *ddb_entry;
1453 struct scsi_qla_host *ha;
1456 hdr_len = sizeof(struct iscsi_hdr);
1457 sess = task->conn->session;
1458 ddb_entry = sess->dd_data;
1460 task_data = task->dd_data;
1462 if (task->data_count) {
1463 dma_unmap_single(&ha->pdev->dev, task_data->data_dma,
1464 task->data_count, PCI_DMA_TODEVICE);
1467 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
1468 __func__, task->conn->max_recv_dlength, hdr_len));
1470 dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
1471 task_data->resp_buffer, task_data->resp_dma);
1472 dma_free_coherent(&ha->pdev->dev, task_data->req_len,
1473 task_data->req_buffer, task_data->req_dma);
1477 static int qla4xxx_task_xmit(struct iscsi_task *task)
1479 struct scsi_cmnd *sc = task->sc;
1480 struct iscsi_session *sess = task->conn->session;
1481 struct ddb_entry *ddb_entry = sess->dd_data;
1482 struct scsi_qla_host *ha = ddb_entry->ha;
1485 return qla4xxx_send_passthru0(task);
1487 ql4_printk(KERN_INFO, ha, "%s: scsi cmd xmit not implemented\n",
1492 static void qla4xxx_copy_fwddb_param(struct scsi_qla_host *ha,
1493 struct dev_db_entry *fw_ddb_entry,
1494 struct iscsi_cls_session *cls_sess,
1495 struct iscsi_cls_conn *cls_conn)
1498 struct iscsi_session *sess;
1499 struct iscsi_conn *conn;
1500 char ip_addr[DDB_IPADDR_LEN];
1501 uint16_t options = 0;
1503 sess = cls_sess->dd_data;
1504 conn = cls_conn->dd_data;
1506 conn->max_recv_dlength = BYTE_UNITS *
1507 le16_to_cpu(fw_ddb_entry->iscsi_max_rcv_data_seg_len);
1509 conn->max_xmit_dlength = BYTE_UNITS *
1510 le16_to_cpu(fw_ddb_entry->iscsi_max_snd_data_seg_len);
1512 sess->initial_r2t_en =
1513 (BIT_10 & le16_to_cpu(fw_ddb_entry->iscsi_options));
1515 sess->max_r2t = le16_to_cpu(fw_ddb_entry->iscsi_max_outsnd_r2t);
1517 sess->imm_data_en = (BIT_11 & le16_to_cpu(fw_ddb_entry->iscsi_options));
1519 sess->first_burst = BYTE_UNITS *
1520 le16_to_cpu(fw_ddb_entry->iscsi_first_burst_len);
1522 sess->max_burst = BYTE_UNITS *
1523 le16_to_cpu(fw_ddb_entry->iscsi_max_burst_len);
1525 sess->time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
1527 sess->time2retain = le16_to_cpu(fw_ddb_entry->iscsi_def_time2retain);
1529 conn->persistent_port = le16_to_cpu(fw_ddb_entry->port);
1531 sess->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
1533 options = le16_to_cpu(fw_ddb_entry->options);
1534 if (options & DDB_OPT_IPV6_DEVICE)
1535 sprintf(ip_addr, "%pI6", fw_ddb_entry->ip_addr);
1537 sprintf(ip_addr, "%pI4", fw_ddb_entry->ip_addr);
1539 iscsi_set_param(cls_conn, ISCSI_PARAM_TARGET_NAME,
1540 (char *)fw_ddb_entry->iscsi_name, buflen);
1541 iscsi_set_param(cls_conn, ISCSI_PARAM_INITIATOR_NAME,
1542 (char *)ha->name_string, buflen);
1543 iscsi_set_param(cls_conn, ISCSI_PARAM_PERSISTENT_ADDRESS,
1544 (char *)ip_addr, buflen);
1547 void qla4xxx_update_session_conn_fwddb_param(struct scsi_qla_host *ha,
1548 struct ddb_entry *ddb_entry)
1550 struct iscsi_cls_session *cls_sess;
1551 struct iscsi_cls_conn *cls_conn;
1553 dma_addr_t fw_ddb_entry_dma;
1554 struct dev_db_entry *fw_ddb_entry;
1556 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1557 &fw_ddb_entry_dma, GFP_KERNEL);
1558 if (!fw_ddb_entry) {
1559 ql4_printk(KERN_ERR, ha,
1560 "%s: Unable to allocate dma buffer\n", __func__);
1561 goto exit_session_conn_fwddb_param;
1564 if (qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index, fw_ddb_entry,
1565 fw_ddb_entry_dma, NULL, NULL, &ddb_state,
1566 NULL, NULL, NULL) == QLA_ERROR) {
1567 DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: failed "
1568 "get_ddb_entry for fw_ddb_index %d\n",
1569 ha->host_no, __func__,
1570 ddb_entry->fw_ddb_index));
1571 goto exit_session_conn_fwddb_param;
1574 cls_sess = ddb_entry->sess;
1576 cls_conn = ddb_entry->conn;
1579 qla4xxx_copy_fwddb_param(ha, fw_ddb_entry, cls_sess, cls_conn);
1581 exit_session_conn_fwddb_param:
1583 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1584 fw_ddb_entry, fw_ddb_entry_dma);
1587 void qla4xxx_update_session_conn_param(struct scsi_qla_host *ha,
1588 struct ddb_entry *ddb_entry)
1590 struct iscsi_cls_session *cls_sess;
1591 struct iscsi_cls_conn *cls_conn;
1592 struct iscsi_session *sess;
1593 struct iscsi_conn *conn;
1595 dma_addr_t fw_ddb_entry_dma;
1596 struct dev_db_entry *fw_ddb_entry;
1598 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1599 &fw_ddb_entry_dma, GFP_KERNEL);
1600 if (!fw_ddb_entry) {
1601 ql4_printk(KERN_ERR, ha,
1602 "%s: Unable to allocate dma buffer\n", __func__);
1603 goto exit_session_conn_param;
1606 if (qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index, fw_ddb_entry,
1607 fw_ddb_entry_dma, NULL, NULL, &ddb_state,
1608 NULL, NULL, NULL) == QLA_ERROR) {
1609 DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: failed "
1610 "get_ddb_entry for fw_ddb_index %d\n",
1611 ha->host_no, __func__,
1612 ddb_entry->fw_ddb_index));
1613 goto exit_session_conn_param;
1616 cls_sess = ddb_entry->sess;
1617 sess = cls_sess->dd_data;
1619 cls_conn = ddb_entry->conn;
1620 conn = cls_conn->dd_data;
1622 /* Update timers after login */
1623 ddb_entry->default_relogin_timeout =
1624 le16_to_cpu(fw_ddb_entry->def_timeout);
1625 ddb_entry->default_time2wait =
1626 le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
1629 conn->max_recv_dlength = BYTE_UNITS *
1630 le16_to_cpu(fw_ddb_entry->iscsi_max_rcv_data_seg_len);
1632 conn->max_xmit_dlength = BYTE_UNITS *
1633 le16_to_cpu(fw_ddb_entry->iscsi_max_snd_data_seg_len);
1635 sess->initial_r2t_en =
1636 (BIT_10 & le16_to_cpu(fw_ddb_entry->iscsi_options));
1638 sess->max_r2t = le16_to_cpu(fw_ddb_entry->iscsi_max_outsnd_r2t);
1640 sess->imm_data_en = (BIT_11 & le16_to_cpu(fw_ddb_entry->iscsi_options));
1642 sess->first_burst = BYTE_UNITS *
1643 le16_to_cpu(fw_ddb_entry->iscsi_first_burst_len);
1645 sess->max_burst = BYTE_UNITS *
1646 le16_to_cpu(fw_ddb_entry->iscsi_max_burst_len);
1648 sess->time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
1650 sess->time2retain = le16_to_cpu(fw_ddb_entry->iscsi_def_time2retain);
1652 sess->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
1654 memcpy(sess->initiatorname, ha->name_string,
1655 min(sizeof(ha->name_string), sizeof(sess->initiatorname)));
1657 exit_session_conn_param:
1659 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1660 fw_ddb_entry, fw_ddb_entry_dma);
1667 static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
1668 unsigned long interval)
1670 DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
1671 __func__, ha->host->host_no));
1672 init_timer(&ha->timer);
1673 ha->timer.expires = jiffies + interval * HZ;
1674 ha->timer.data = (unsigned long)ha;
1675 ha->timer.function = (void (*)(unsigned long))func;
1676 add_timer(&ha->timer);
1677 ha->timer_active = 1;
1680 static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
1682 del_timer_sync(&ha->timer);
1683 ha->timer_active = 0;
1687 * qla4xxx_mark_device_missing - blocks the session
1688 * @cls_session: Pointer to the session to be blocked
1689 * @ddb_entry: Pointer to device database entry
1691 * This routine marks a device missing and close connection.
1693 void qla4xxx_mark_device_missing(struct iscsi_cls_session *cls_session)
1695 iscsi_block_session(cls_session);
1699 * qla4xxx_mark_all_devices_missing - mark all devices as missing.
1700 * @ha: Pointer to host adapter structure.
1702 * This routine marks a device missing and resets the relogin retry count.
1704 void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
1706 iscsi_host_for_each_session(ha->host, qla4xxx_mark_device_missing);
1709 static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
1710 struct ddb_entry *ddb_entry,
1711 struct scsi_cmnd *cmd)
1715 srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
1719 kref_init(&srb->srb_ref);
1721 srb->ddb = ddb_entry;
1724 CMD_SP(cmd) = (void *)srb;
1729 static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
1731 struct scsi_cmnd *cmd = srb->cmd;
1733 if (srb->flags & SRB_DMA_VALID) {
1734 scsi_dma_unmap(cmd);
1735 srb->flags &= ~SRB_DMA_VALID;
1740 void qla4xxx_srb_compl(struct kref *ref)
1742 struct srb *srb = container_of(ref, struct srb, srb_ref);
1743 struct scsi_cmnd *cmd = srb->cmd;
1744 struct scsi_qla_host *ha = srb->ha;
1746 qla4xxx_srb_free_dma(ha, srb);
1748 mempool_free(srb, ha->srb_mempool);
1750 cmd->scsi_done(cmd);
1754 * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
1756 * @cmd: Pointer to Linux's SCSI command structure
1759 * This routine is invoked by Linux to send a SCSI command to the driver.
1760 * The mid-level driver tries to ensure that queuecommand never gets
1761 * invoked concurrently with itself or the interrupt handler (although
1762 * the interrupt handler may call this routine as part of request-
1763 * completion handling). Unfortunely, it sometimes calls the scheduler
1764 * in interrupt context which is a big NO! NO!.
1766 static int qla4xxx_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
1768 struct scsi_qla_host *ha = to_qla_host(host);
1769 struct ddb_entry *ddb_entry = cmd->device->hostdata;
1770 struct iscsi_cls_session *sess = ddb_entry->sess;
1774 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1775 if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
1776 cmd->result = DID_NO_CONNECT << 16;
1778 cmd->result = DID_REQUEUE << 16;
1779 goto qc_fail_command;
1783 cmd->result = DID_IMM_RETRY << 16;
1784 goto qc_fail_command;
1787 rval = iscsi_session_chkready(sess);
1790 goto qc_fail_command;
1793 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1794 test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
1795 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1796 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
1797 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
1798 !test_bit(AF_ONLINE, &ha->flags) ||
1799 !test_bit(AF_LINK_UP, &ha->flags) ||
1800 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
1803 srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd);
1807 rval = qla4xxx_send_command_to_isp(ha, srb);
1808 if (rval != QLA_SUCCESS)
1809 goto qc_host_busy_free_sp;
1813 qc_host_busy_free_sp:
1814 qla4xxx_srb_free_dma(ha, srb);
1815 mempool_free(srb, ha->srb_mempool);
1818 return SCSI_MLQUEUE_HOST_BUSY;
1821 cmd->scsi_done(cmd);
1827 * qla4xxx_mem_free - frees memory allocated to adapter
1828 * @ha: Pointer to host adapter structure.
1830 * Frees memory previously allocated by qla4xxx_mem_alloc
1832 static void qla4xxx_mem_free(struct scsi_qla_host *ha)
1835 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
1841 ha->request_ring = NULL;
1842 ha->request_dma = 0;
1843 ha->response_ring = NULL;
1844 ha->response_dma = 0;
1845 ha->shadow_regs = NULL;
1846 ha->shadow_regs_dma = 0;
1848 /* Free srb pool. */
1849 if (ha->srb_mempool)
1850 mempool_destroy(ha->srb_mempool);
1852 ha->srb_mempool = NULL;
1854 if (ha->chap_dma_pool)
1855 dma_pool_destroy(ha->chap_dma_pool);
1858 vfree(ha->chap_list);
1859 ha->chap_list = NULL;
1861 if (ha->fw_ddb_dma_pool)
1862 dma_pool_destroy(ha->fw_ddb_dma_pool);
1864 /* release io space registers */
1865 if (is_qla8022(ha)) {
1868 (struct device_reg_82xx __iomem *)ha->nx_pcibase);
1871 pci_release_regions(ha->pdev);
1875 * qla4xxx_mem_alloc - allocates memory for use by adapter.
1876 * @ha: Pointer to host adapter structure
1878 * Allocates DMA memory for request and response queues. Also allocates memory
1881 static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
1883 unsigned long align;
1885 /* Allocate contiguous block of DMA memory for queues. */
1886 ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
1887 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
1888 sizeof(struct shadow_regs) +
1890 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
1891 ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
1892 &ha->queues_dma, GFP_KERNEL);
1893 if (ha->queues == NULL) {
1894 ql4_printk(KERN_WARNING, ha,
1895 "Memory Allocation failed - queues.\n");
1897 goto mem_alloc_error_exit;
1899 memset(ha->queues, 0, ha->queues_len);
1902 * As per RISC alignment requirements -- the bus-address must be a
1903 * multiple of the request-ring size (in bytes).
1906 if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
1907 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
1908 (MEM_ALIGN_VALUE - 1));
1910 /* Update request and response queue pointers. */
1911 ha->request_dma = ha->queues_dma + align;
1912 ha->request_ring = (struct queue_entry *) (ha->queues + align);
1913 ha->response_dma = ha->queues_dma + align +
1914 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
1915 ha->response_ring = (struct queue_entry *) (ha->queues + align +
1916 (REQUEST_QUEUE_DEPTH *
1918 ha->shadow_regs_dma = ha->queues_dma + align +
1919 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
1920 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
1921 ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
1922 (REQUEST_QUEUE_DEPTH *
1924 (RESPONSE_QUEUE_DEPTH *
1927 /* Allocate memory for srb pool. */
1928 ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
1929 mempool_free_slab, srb_cachep);
1930 if (ha->srb_mempool == NULL) {
1931 ql4_printk(KERN_WARNING, ha,
1932 "Memory Allocation failed - SRB Pool.\n");
1934 goto mem_alloc_error_exit;
1937 ha->chap_dma_pool = dma_pool_create("ql4_chap", &ha->pdev->dev,
1938 CHAP_DMA_BLOCK_SIZE, 8, 0);
1940 if (ha->chap_dma_pool == NULL) {
1941 ql4_printk(KERN_WARNING, ha,
1942 "%s: chap_dma_pool allocation failed..\n", __func__);
1943 goto mem_alloc_error_exit;
1946 ha->fw_ddb_dma_pool = dma_pool_create("ql4_fw_ddb", &ha->pdev->dev,
1947 DDB_DMA_BLOCK_SIZE, 8, 0);
1949 if (ha->fw_ddb_dma_pool == NULL) {
1950 ql4_printk(KERN_WARNING, ha,
1951 "%s: fw_ddb_dma_pool allocation failed..\n",
1953 goto mem_alloc_error_exit;
1958 mem_alloc_error_exit:
1959 qla4xxx_mem_free(ha);
1964 * qla4_8xxx_check_fw_alive - Check firmware health
1965 * @ha: Pointer to host adapter structure.
1967 * Context: Interrupt
1969 static void qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
1971 uint32_t fw_heartbeat_counter, halt_status;
1973 fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
1974 /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
1975 if (fw_heartbeat_counter == 0xffffffff) {
1976 DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
1977 "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
1978 ha->host_no, __func__));
1982 if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
1983 ha->seconds_since_last_heartbeat++;
1984 /* FW not alive after 2 seconds */
1985 if (ha->seconds_since_last_heartbeat == 2) {
1986 ha->seconds_since_last_heartbeat = 0;
1987 halt_status = qla4_8xxx_rd_32(ha,
1988 QLA82XX_PEG_HALT_STATUS1);
1990 ql4_printk(KERN_INFO, ha,
1991 "scsi(%ld): %s, Dumping hw/fw registers:\n "
1992 " PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2:"
1993 " 0x%x,\n PEG_NET_0_PC: 0x%x, PEG_NET_1_PC:"
1994 " 0x%x,\n PEG_NET_2_PC: 0x%x, PEG_NET_3_PC:"
1995 " 0x%x,\n PEG_NET_4_PC: 0x%x\n",
1996 ha->host_no, __func__, halt_status,
1998 QLA82XX_PEG_HALT_STATUS2),
1999 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_0 +
2001 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_1 +
2003 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_2 +
2005 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_3 +
2007 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_4 +
2010 /* Since we cannot change dev_state in interrupt
2011 * context, set appropriate DPC flag then wakeup
2013 if (halt_status & HALT_STATUS_UNRECOVERABLE)
2014 set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
2016 printk("scsi%ld: %s: detect abort needed!\n",
2017 ha->host_no, __func__);
2018 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2020 qla4xxx_wake_dpc(ha);
2021 qla4xxx_mailbox_premature_completion(ha);
2024 ha->seconds_since_last_heartbeat = 0;
2026 ha->fw_heartbeat_counter = fw_heartbeat_counter;
2030 * qla4_8xxx_watchdog - Poll dev state
2031 * @ha: Pointer to host adapter structure.
2033 * Context: Interrupt
2035 void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
2039 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
2041 /* don't poll if reset is going on */
2042 if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
2043 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
2044 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags))) {
2045 if (dev_state == QLA82XX_DEV_NEED_RESET &&
2046 !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
2047 if (!ql4xdontresethba) {
2048 ql4_printk(KERN_INFO, ha, "%s: HW State: "
2049 "NEED RESET!\n", __func__);
2050 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2051 qla4xxx_wake_dpc(ha);
2052 qla4xxx_mailbox_premature_completion(ha);
2054 } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
2055 !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
2056 ql4_printk(KERN_INFO, ha, "%s: HW State: NEED QUIES!\n",
2058 set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
2059 qla4xxx_wake_dpc(ha);
2061 /* Check firmware health */
2062 qla4_8xxx_check_fw_alive(ha);
2067 void qla4xxx_check_relogin_flash_ddb(struct iscsi_cls_session *cls_sess)
2069 struct iscsi_session *sess;
2070 struct ddb_entry *ddb_entry;
2071 struct scsi_qla_host *ha;
2073 sess = cls_sess->dd_data;
2074 ddb_entry = sess->dd_data;
2077 if (!(ddb_entry->ddb_type == FLASH_DDB))
2080 if (adapter_up(ha) && !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
2081 !iscsi_is_session_online(cls_sess)) {
2082 if (atomic_read(&ddb_entry->retry_relogin_timer) !=
2084 if (atomic_read(&ddb_entry->retry_relogin_timer) ==
2086 atomic_set(&ddb_entry->retry_relogin_timer,
2088 set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
2089 set_bit(DF_RELOGIN, &ddb_entry->flags);
2090 DEBUG2(ql4_printk(KERN_INFO, ha,
2091 "%s: index [%d] login device\n",
2092 __func__, ddb_entry->fw_ddb_index));
2094 atomic_dec(&ddb_entry->retry_relogin_timer);
2098 /* Wait for relogin to timeout */
2099 if (atomic_read(&ddb_entry->relogin_timer) &&
2100 (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
2102 * If the relogin times out and the device is
2103 * still NOT ONLINE then try and relogin again.
2105 if (!iscsi_is_session_online(cls_sess)) {
2106 /* Reset retry relogin timer */
2107 atomic_inc(&ddb_entry->relogin_retry_count);
2108 DEBUG2(ql4_printk(KERN_INFO, ha,
2109 "%s: index[%d] relogin timed out-retrying"
2110 " relogin (%d), retry (%d)\n", __func__,
2111 ddb_entry->fw_ddb_index,
2112 atomic_read(&ddb_entry->relogin_retry_count),
2113 ddb_entry->default_time2wait + 4));
2114 set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
2115 atomic_set(&ddb_entry->retry_relogin_timer,
2116 ddb_entry->default_time2wait + 4);
2122 * qla4xxx_timer - checks every second for work to do.
2123 * @ha: Pointer to host adapter structure.
2125 static void qla4xxx_timer(struct scsi_qla_host *ha)
2130 iscsi_host_for_each_session(ha->host, qla4xxx_check_relogin_flash_ddb);
2132 /* If we are in the middle of AER/EEH processing
2133 * skip any processing and reschedule the timer
2135 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
2136 mod_timer(&ha->timer, jiffies + HZ);
2140 /* Hardware read to trigger an EEH error during mailbox waits. */
2141 if (!pci_channel_offline(ha->pdev))
2142 pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
2144 if (is_qla8022(ha)) {
2145 qla4_8xxx_watchdog(ha);
2148 if (!is_qla8022(ha)) {
2149 /* Check for heartbeat interval. */
2150 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
2151 ha->heartbeat_interval != 0) {
2152 ha->seconds_since_last_heartbeat++;
2153 if (ha->seconds_since_last_heartbeat >
2154 ha->heartbeat_interval + 2)
2155 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2159 /* Wakeup the dpc routine for this adapter, if needed. */
2161 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
2162 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
2163 test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
2164 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
2165 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
2166 test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
2167 test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
2168 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
2169 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
2170 test_bit(DPC_AEN, &ha->dpc_flags)) {
2171 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
2172 " - dpc flags = 0x%lx\n",
2173 ha->host_no, __func__, ha->dpc_flags));
2174 qla4xxx_wake_dpc(ha);
2177 /* Reschedule timer thread to call us back in one second */
2178 mod_timer(&ha->timer, jiffies + HZ);
2180 DEBUG2(ha->seconds_since_last_intr++);
2184 * qla4xxx_cmd_wait - waits for all outstanding commands to complete
2185 * @ha: Pointer to host adapter structure.
2187 * This routine stalls the driver until all outstanding commands are returned.
2188 * Caller must release the Hardware Lock prior to calling this routine.
2190 static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
2193 unsigned long flags;
2194 struct scsi_cmnd *cmd;
2196 unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
2198 DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
2199 "complete\n", WAIT_CMD_TOV));
2201 while (!time_after_eq(jiffies, wtime)) {
2202 spin_lock_irqsave(&ha->hardware_lock, flags);
2203 /* Find a command that hasn't completed. */
2204 for (index = 0; index < ha->host->can_queue; index++) {
2205 cmd = scsi_host_find_tag(ha->host, index);
2207 * We cannot just check if the index is valid,
2208 * becase if we are run from the scsi eh, then
2209 * the scsi/block layer is going to prevent
2210 * the tag from being released.
2212 if (cmd != NULL && CMD_SP(cmd))
2215 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2217 /* If No Commands are pending, wait is complete */
2218 if (index == ha->host->can_queue)
2223 /* If we timed out on waiting for commands to come back
2228 int qla4xxx_hw_reset(struct scsi_qla_host *ha)
2230 uint32_t ctrl_status;
2231 unsigned long flags = 0;
2233 DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
2235 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
2238 spin_lock_irqsave(&ha->hardware_lock, flags);
2241 * If the SCSI Reset Interrupt bit is set, clear it.
2242 * Otherwise, the Soft Reset won't work.
2244 ctrl_status = readw(&ha->reg->ctrl_status);
2245 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
2246 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
2248 /* Issue Soft Reset */
2249 writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
2250 readl(&ha->reg->ctrl_status);
2252 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2257 * qla4xxx_soft_reset - performs soft reset.
2258 * @ha: Pointer to host adapter structure.
2260 int qla4xxx_soft_reset(struct scsi_qla_host *ha)
2262 uint32_t max_wait_time;
2263 unsigned long flags = 0;
2265 uint32_t ctrl_status;
2267 status = qla4xxx_hw_reset(ha);
2268 if (status != QLA_SUCCESS)
2272 /* Wait until the Network Reset Intr bit is cleared */
2273 max_wait_time = RESET_INTR_TOV;
2275 spin_lock_irqsave(&ha->hardware_lock, flags);
2276 ctrl_status = readw(&ha->reg->ctrl_status);
2277 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2279 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
2283 } while ((--max_wait_time));
2285 if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
2286 DEBUG2(printk(KERN_WARNING
2287 "scsi%ld: Network Reset Intr not cleared by "
2288 "Network function, clearing it now!\n",
2290 spin_lock_irqsave(&ha->hardware_lock, flags);
2291 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
2292 readl(&ha->reg->ctrl_status);
2293 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2296 /* Wait until the firmware tells us the Soft Reset is done */
2297 max_wait_time = SOFT_RESET_TOV;
2299 spin_lock_irqsave(&ha->hardware_lock, flags);
2300 ctrl_status = readw(&ha->reg->ctrl_status);
2301 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2303 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
2304 status = QLA_SUCCESS;
2309 } while ((--max_wait_time));
2312 * Also, make sure that the SCSI Reset Interrupt bit has been cleared
2313 * after the soft reset has taken place.
2315 spin_lock_irqsave(&ha->hardware_lock, flags);
2316 ctrl_status = readw(&ha->reg->ctrl_status);
2317 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
2318 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
2319 readl(&ha->reg->ctrl_status);
2321 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2323 /* If soft reset fails then most probably the bios on other
2324 * function is also enabled.
2325 * Since the initialization is sequential the other fn
2326 * wont be able to acknowledge the soft reset.
2327 * Issue a force soft reset to workaround this scenario.
2329 if (max_wait_time == 0) {
2330 /* Issue Force Soft Reset */
2331 spin_lock_irqsave(&ha->hardware_lock, flags);
2332 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
2333 readl(&ha->reg->ctrl_status);
2334 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2335 /* Wait until the firmware tells us the Soft Reset is done */
2336 max_wait_time = SOFT_RESET_TOV;
2338 spin_lock_irqsave(&ha->hardware_lock, flags);
2339 ctrl_status = readw(&ha->reg->ctrl_status);
2340 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2342 if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
2343 status = QLA_SUCCESS;
2348 } while ((--max_wait_time));
2355 * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
2356 * @ha: Pointer to host adapter structure.
2357 * @res: returned scsi status
2359 * This routine is called just prior to a HARD RESET to return all
2360 * outstanding commands back to the Operating System.
2361 * Caller should make sure that the following locks are released
2362 * before this calling routine: Hardware lock, and io_request_lock.
2364 static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
2368 unsigned long flags;
2370 spin_lock_irqsave(&ha->hardware_lock, flags);
2371 for (i = 0; i < ha->host->can_queue; i++) {
2372 srb = qla4xxx_del_from_active_array(ha, i);
2374 srb->cmd->result = res;
2375 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
2378 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2381 void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
2383 clear_bit(AF_ONLINE, &ha->flags);
2385 /* Disable the board */
2386 ql4_printk(KERN_INFO, ha, "Disabling the board\n");
2388 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
2389 qla4xxx_mark_all_devices_missing(ha);
2390 clear_bit(AF_INIT_DONE, &ha->flags);
2393 static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session)
2395 struct iscsi_session *sess;
2396 struct ddb_entry *ddb_entry;
2398 sess = cls_session->dd_data;
2399 ddb_entry = sess->dd_data;
2400 ddb_entry->fw_ddb_device_state = DDB_DS_SESSION_FAILED;
2402 if (ddb_entry->ddb_type == FLASH_DDB)
2403 iscsi_block_session(ddb_entry->sess);
2405 iscsi_session_failure(cls_session->dd_data,
2406 ISCSI_ERR_CONN_FAILED);
2410 * qla4xxx_recover_adapter - recovers adapter after a fatal error
2411 * @ha: Pointer to host adapter structure.
2413 static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
2415 int status = QLA_ERROR;
2416 uint8_t reset_chip = 0;
2418 /* Stall incoming I/O until we are done */
2419 scsi_block_requests(ha->host);
2420 clear_bit(AF_ONLINE, &ha->flags);
2421 clear_bit(AF_LINK_UP, &ha->flags);
2423 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
2425 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2427 iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
2429 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
2432 /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
2433 * do not reset adapter, jump to initialize_adapter */
2434 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
2435 status = QLA_SUCCESS;
2436 goto recover_ha_init_adapter;
2439 /* For the ISP-82xx adapter, issue a stop_firmware if invoked
2440 * from eh_host_reset or ioctl module */
2441 if (is_qla8022(ha) && !reset_chip &&
2442 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
2444 DEBUG2(ql4_printk(KERN_INFO, ha,
2445 "scsi%ld: %s - Performing stop_firmware...\n",
2446 ha->host_no, __func__));
2447 status = ha->isp_ops->reset_firmware(ha);
2448 if (status == QLA_SUCCESS) {
2449 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
2450 qla4xxx_cmd_wait(ha);
2451 ha->isp_ops->disable_intrs(ha);
2452 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2453 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
2455 /* If the stop_firmware fails then
2456 * reset the entire chip */
2458 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2459 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2463 /* Issue full chip reset if recovering from a catastrophic error,
2464 * or if stop_firmware fails for ISP-82xx.
2465 * This is the default case for ISP-4xxx */
2466 if (!is_qla8022(ha) || reset_chip) {
2467 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
2468 qla4xxx_cmd_wait(ha);
2469 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2470 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
2471 DEBUG2(ql4_printk(KERN_INFO, ha,
2472 "scsi%ld: %s - Performing chip reset..\n",
2473 ha->host_no, __func__));
2474 status = ha->isp_ops->reset_chip(ha);
2477 /* Flush any pending ddb changed AENs */
2478 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2480 recover_ha_init_adapter:
2481 /* Upon successful firmware/chip reset, re-initialize the adapter */
2482 if (status == QLA_SUCCESS) {
2483 /* For ISP-4xxx, force function 1 to always initialize
2484 * before function 3 to prevent both funcions from
2485 * stepping on top of the other */
2486 if (!is_qla8022(ha) && (ha->mac_index == 3))
2489 /* NOTE: AF_ONLINE flag set upon successful completion of
2490 * qla4xxx_initialize_adapter */
2491 status = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
2494 /* Retry failed adapter initialization, if necessary
2495 * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
2496 * case to prevent ping-pong resets between functions */
2497 if (!test_bit(AF_ONLINE, &ha->flags) &&
2498 !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
2499 /* Adapter initialization failed, see if we can retry
2501 * Since we don't want to block the DPC for too long
2502 * with multiple resets in the same thread,
2503 * utilize DPC to retry */
2504 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
2505 ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
2506 DEBUG2(printk("scsi%ld: recover adapter - retrying "
2507 "(%d) more times\n", ha->host_no,
2508 ha->retry_reset_ha_cnt));
2509 set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
2512 if (ha->retry_reset_ha_cnt > 0) {
2513 /* Schedule another Reset HA--DPC will retry */
2514 ha->retry_reset_ha_cnt--;
2515 DEBUG2(printk("scsi%ld: recover adapter - "
2516 "retry remaining %d\n",
2518 ha->retry_reset_ha_cnt));
2522 if (ha->retry_reset_ha_cnt == 0) {
2523 /* Recover adapter retries have been exhausted.
2525 DEBUG2(printk("scsi%ld: recover adapter "
2526 "failed - board disabled\n",
2528 qla4xxx_dead_adapter_cleanup(ha);
2529 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
2530 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
2531 clear_bit(DPC_RESET_HA_FW_CONTEXT,
2537 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
2538 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2539 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
2542 ha->adapter_error_count++;
2544 if (test_bit(AF_ONLINE, &ha->flags))
2545 ha->isp_ops->enable_intrs(ha);
2547 scsi_unblock_requests(ha->host);
2549 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2550 DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
2551 status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
2556 static void qla4xxx_relogin_devices(struct iscsi_cls_session *cls_session)
2558 struct iscsi_session *sess;
2559 struct ddb_entry *ddb_entry;
2560 struct scsi_qla_host *ha;
2562 sess = cls_session->dd_data;
2563 ddb_entry = sess->dd_data;
2565 if (!iscsi_is_session_online(cls_session)) {
2566 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
2567 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
2568 " unblock session\n", ha->host_no, __func__,
2569 ddb_entry->fw_ddb_index);
2570 iscsi_unblock_session(ddb_entry->sess);
2572 /* Trigger relogin */
2573 if (ddb_entry->ddb_type == FLASH_DDB) {
2574 if (!test_bit(DF_RELOGIN, &ddb_entry->flags))
2575 qla4xxx_arm_relogin_timer(ddb_entry);
2577 iscsi_session_failure(cls_session->dd_data,
2578 ISCSI_ERR_CONN_FAILED);
2583 int qla4xxx_unblock_flash_ddb(struct iscsi_cls_session *cls_session)
2585 struct iscsi_session *sess;
2586 struct ddb_entry *ddb_entry;
2587 struct scsi_qla_host *ha;
2589 sess = cls_session->dd_data;
2590 ddb_entry = sess->dd_data;
2592 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
2593 " unblock session\n", ha->host_no, __func__,
2594 ddb_entry->fw_ddb_index);
2596 iscsi_unblock_session(ddb_entry->sess);
2598 /* Start scan target */
2599 if (test_bit(AF_ONLINE, &ha->flags)) {
2600 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
2601 " start scan\n", ha->host_no, __func__,
2602 ddb_entry->fw_ddb_index);
2603 scsi_queue_work(ha->host, &ddb_entry->sess->scan_work);
2608 int qla4xxx_unblock_ddb(struct iscsi_cls_session *cls_session)
2610 struct iscsi_session *sess;
2611 struct ddb_entry *ddb_entry;
2612 struct scsi_qla_host *ha;
2614 sess = cls_session->dd_data;
2615 ddb_entry = sess->dd_data;
2617 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
2618 " unblock user space session\n", ha->host_no, __func__,
2619 ddb_entry->fw_ddb_index);
2620 iscsi_conn_start(ddb_entry->conn);
2621 iscsi_conn_login_event(ddb_entry->conn,
2622 ISCSI_CONN_STATE_LOGGED_IN);
2627 static void qla4xxx_relogin_all_devices(struct scsi_qla_host *ha)
2629 iscsi_host_for_each_session(ha->host, qla4xxx_relogin_devices);
2632 static void qla4xxx_relogin_flash_ddb(struct iscsi_cls_session *cls_sess)
2634 uint16_t relogin_timer;
2635 struct iscsi_session *sess;
2636 struct ddb_entry *ddb_entry;
2637 struct scsi_qla_host *ha;
2639 sess = cls_sess->dd_data;
2640 ddb_entry = sess->dd_data;
2643 relogin_timer = max(ddb_entry->default_relogin_timeout,
2644 (uint16_t)RELOGIN_TOV);
2645 atomic_set(&ddb_entry->relogin_timer, relogin_timer);
2647 DEBUG2(ql4_printk(KERN_INFO, ha,
2648 "scsi%ld: Relogin index [%d]. TOV=%d\n", ha->host_no,
2649 ddb_entry->fw_ddb_index, relogin_timer));
2651 qla4xxx_login_flash_ddb(cls_sess);
2654 static void qla4xxx_dpc_relogin(struct iscsi_cls_session *cls_sess)
2656 struct iscsi_session *sess;
2657 struct ddb_entry *ddb_entry;
2658 struct scsi_qla_host *ha;
2660 sess = cls_sess->dd_data;
2661 ddb_entry = sess->dd_data;
2664 if (!(ddb_entry->ddb_type == FLASH_DDB))
2667 if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
2668 !iscsi_is_session_online(cls_sess)) {
2669 DEBUG2(ql4_printk(KERN_INFO, ha,
2670 "relogin issued\n"));
2671 qla4xxx_relogin_flash_ddb(cls_sess);
2675 void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
2678 queue_work(ha->dpc_thread, &ha->dpc_work);
2682 * qla4xxx_do_dpc - dpc routine
2683 * @data: in our case pointer to adapter structure
2685 * This routine is a task that is schedule by the interrupt handler
2686 * to perform the background processing for interrupts. We put it
2687 * on a task queue that is consumed whenever the scheduler runs; that's
2688 * so you can do anything (i.e. put the process to sleep etc). In fact,
2689 * the mid-level tries to sleep when it reaches the driver threshold
2690 * "host->can_queue". This can cause a panic if we were in our interrupt code.
2692 static void qla4xxx_do_dpc(struct work_struct *work)
2694 struct scsi_qla_host *ha =
2695 container_of(work, struct scsi_qla_host, dpc_work);
2696 int status = QLA_ERROR;
2698 DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
2699 "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
2700 ha->host_no, __func__, ha->flags, ha->dpc_flags))
2702 /* Initialization not yet finished. Don't do anything yet. */
2703 if (!test_bit(AF_INIT_DONE, &ha->flags))
2706 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
2707 DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
2708 ha->host_no, __func__, ha->flags));
2712 if (is_qla8022(ha)) {
2713 if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
2714 qla4_8xxx_idc_lock(ha);
2715 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2716 QLA82XX_DEV_FAILED);
2717 qla4_8xxx_idc_unlock(ha);
2718 ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
2719 qla4_8xxx_device_state_handler(ha);
2721 if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
2722 qla4_8xxx_need_qsnt_handler(ha);
2726 if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
2727 (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
2728 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
2729 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
2730 if (ql4xdontresethba) {
2731 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
2732 ha->host_no, __func__));
2733 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
2734 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
2735 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2736 goto dpc_post_reset_ha;
2738 if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
2739 test_bit(DPC_RESET_HA, &ha->dpc_flags))
2740 qla4xxx_recover_adapter(ha);
2742 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
2743 uint8_t wait_time = RESET_INTR_TOV;
2745 while ((readw(&ha->reg->ctrl_status) &
2746 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
2747 if (--wait_time == 0)
2752 DEBUG2(printk("scsi%ld: %s: SR|FSR "
2753 "bit not cleared-- resetting\n",
2754 ha->host_no, __func__));
2755 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
2756 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
2757 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2758 status = qla4xxx_recover_adapter(ha);
2760 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
2761 if (status == QLA_SUCCESS)
2762 ha->isp_ops->enable_intrs(ha);
2767 /* ---- process AEN? --- */
2768 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
2769 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
2771 /* ---- Get DHCP IP Address? --- */
2772 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
2773 qla4xxx_get_dhcp_ip_address(ha);
2775 /* ---- relogin device? --- */
2776 if (adapter_up(ha) &&
2777 test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
2778 iscsi_host_for_each_session(ha->host, qla4xxx_dpc_relogin);
2781 /* ---- link change? --- */
2782 if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
2783 if (!test_bit(AF_LINK_UP, &ha->flags)) {
2784 /* ---- link down? --- */
2785 qla4xxx_mark_all_devices_missing(ha);
2787 /* ---- link up? --- *
2788 * F/W will auto login to all devices ONLY ONCE after
2789 * link up during driver initialization and runtime
2790 * fatal error recovery. Therefore, the driver must
2791 * manually relogin to devices when recovering from
2792 * connection failures, logouts, expired KATO, etc. */
2793 if (test_and_clear_bit(AF_BUILD_DDB_LIST, &ha->flags)) {
2794 qla4xxx_build_ddb_list(ha, ha->is_reset);
2795 iscsi_host_for_each_session(ha->host,
2796 qla4xxx_login_flash_ddb);
2798 qla4xxx_relogin_all_devices(ha);
2804 * qla4xxx_free_adapter - release the adapter
2805 * @ha: pointer to adapter structure
2807 static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
2810 if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
2811 /* Turn-off interrupts on the card. */
2812 ha->isp_ops->disable_intrs(ha);
2815 /* Remove timer thread, if present */
2816 if (ha->timer_active)
2817 qla4xxx_stop_timer(ha);
2819 /* Kill the kernel thread for this host */
2821 destroy_workqueue(ha->dpc_thread);
2823 /* Kill the kernel thread for this host */
2825 destroy_workqueue(ha->task_wq);
2827 /* Put firmware in known state */
2828 ha->isp_ops->reset_firmware(ha);
2830 if (is_qla8022(ha)) {
2831 qla4_8xxx_idc_lock(ha);
2832 qla4_8xxx_clear_drv_active(ha);
2833 qla4_8xxx_idc_unlock(ha);
2836 /* Detach interrupts */
2837 if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
2838 qla4xxx_free_irqs(ha);
2840 /* free extra memory */
2841 qla4xxx_mem_free(ha);
2844 int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
2847 uint8_t revision_id;
2848 unsigned long mem_base, mem_len, db_base, db_len;
2849 struct pci_dev *pdev = ha->pdev;
2851 status = pci_request_regions(pdev, DRIVER_NAME);
2854 "scsi(%ld) Failed to reserve PIO regions (%s) "
2855 "status=%d\n", ha->host_no, pci_name(pdev), status);
2856 goto iospace_error_exit;
2859 pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
2860 DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
2861 __func__, revision_id));
2862 ha->revision_id = revision_id;
2864 /* remap phys address */
2865 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
2866 mem_len = pci_resource_len(pdev, 0);
2867 DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
2868 __func__, mem_base, mem_len));
2870 /* mapping of pcibase pointer */
2871 ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
2872 if (!ha->nx_pcibase) {
2874 "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
2875 pci_release_regions(ha->pdev);
2876 goto iospace_error_exit;
2879 /* Mapping of IO base pointer, door bell read and write pointer */
2881 /* mapping of IO base pointer */
2883 (struct device_reg_82xx __iomem *)((uint8_t *)ha->nx_pcibase +
2884 0xbc000 + (ha->pdev->devfn << 11));
2886 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
2887 db_len = pci_resource_len(pdev, 4);
2889 ha->nx_db_wr_ptr = (ha->pdev->devfn == 4 ? QLA82XX_CAM_RAM_DB1 :
2890 QLA82XX_CAM_RAM_DB2);
2898 * qla4xxx_iospace_config - maps registers
2899 * @ha: pointer to adapter structure
2901 * This routines maps HBA's registers from the pci address space
2902 * into the kernel virtual address space for memory mapped i/o.
2904 int qla4xxx_iospace_config(struct scsi_qla_host *ha)
2906 unsigned long pio, pio_len, pio_flags;
2907 unsigned long mmio, mmio_len, mmio_flags;
2909 pio = pci_resource_start(ha->pdev, 0);
2910 pio_len = pci_resource_len(ha->pdev, 0);
2911 pio_flags = pci_resource_flags(ha->pdev, 0);
2912 if (pio_flags & IORESOURCE_IO) {
2913 if (pio_len < MIN_IOBASE_LEN) {
2914 ql4_printk(KERN_WARNING, ha,
2915 "Invalid PCI I/O region size\n");
2919 ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
2923 /* Use MMIO operations for all accesses. */
2924 mmio = pci_resource_start(ha->pdev, 1);
2925 mmio_len = pci_resource_len(ha->pdev, 1);
2926 mmio_flags = pci_resource_flags(ha->pdev, 1);
2928 if (!(mmio_flags & IORESOURCE_MEM)) {
2929 ql4_printk(KERN_ERR, ha,
2930 "region #0 not an MMIO resource, aborting\n");
2932 goto iospace_error_exit;
2935 if (mmio_len < MIN_IOBASE_LEN) {
2936 ql4_printk(KERN_ERR, ha,
2937 "Invalid PCI mem region size, aborting\n");
2938 goto iospace_error_exit;
2941 if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
2942 ql4_printk(KERN_WARNING, ha,
2943 "Failed to reserve PIO/MMIO regions\n");
2945 goto iospace_error_exit;
2948 ha->pio_address = pio;
2949 ha->pio_length = pio_len;
2950 ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
2952 ql4_printk(KERN_ERR, ha,
2953 "cannot remap MMIO, aborting\n");
2955 goto iospace_error_exit;
2964 static struct isp_operations qla4xxx_isp_ops = {
2965 .iospace_config = qla4xxx_iospace_config,
2966 .pci_config = qla4xxx_pci_config,
2967 .disable_intrs = qla4xxx_disable_intrs,
2968 .enable_intrs = qla4xxx_enable_intrs,
2969 .start_firmware = qla4xxx_start_firmware,
2970 .intr_handler = qla4xxx_intr_handler,
2971 .interrupt_service_routine = qla4xxx_interrupt_service_routine,
2972 .reset_chip = qla4xxx_soft_reset,
2973 .reset_firmware = qla4xxx_hw_reset,
2974 .queue_iocb = qla4xxx_queue_iocb,
2975 .complete_iocb = qla4xxx_complete_iocb,
2976 .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
2977 .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
2978 .get_sys_info = qla4xxx_get_sys_info,
2981 static struct isp_operations qla4_8xxx_isp_ops = {
2982 .iospace_config = qla4_8xxx_iospace_config,
2983 .pci_config = qla4_8xxx_pci_config,
2984 .disable_intrs = qla4_8xxx_disable_intrs,
2985 .enable_intrs = qla4_8xxx_enable_intrs,
2986 .start_firmware = qla4_8xxx_load_risc,
2987 .intr_handler = qla4_8xxx_intr_handler,
2988 .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
2989 .reset_chip = qla4_8xxx_isp_reset,
2990 .reset_firmware = qla4_8xxx_stop_firmware,
2991 .queue_iocb = qla4_8xxx_queue_iocb,
2992 .complete_iocb = qla4_8xxx_complete_iocb,
2993 .rd_shdw_req_q_out = qla4_8xxx_rd_shdw_req_q_out,
2994 .rd_shdw_rsp_q_in = qla4_8xxx_rd_shdw_rsp_q_in,
2995 .get_sys_info = qla4_8xxx_get_sys_info,
2998 uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
3000 return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
3003 uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
3005 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
3008 uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
3010 return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
3013 uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
3015 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
3018 static ssize_t qla4xxx_show_boot_eth_info(void *data, int type, char *buf)
3020 struct scsi_qla_host *ha = data;
3025 case ISCSI_BOOT_ETH_FLAGS:
3026 rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
3028 case ISCSI_BOOT_ETH_INDEX:
3029 rc = sprintf(str, "0\n");
3031 case ISCSI_BOOT_ETH_MAC:
3032 rc = sysfs_format_mac(str, ha->my_mac,
3042 static mode_t qla4xxx_eth_get_attr_visibility(void *data, int type)
3047 case ISCSI_BOOT_ETH_FLAGS:
3048 case ISCSI_BOOT_ETH_MAC:
3049 case ISCSI_BOOT_ETH_INDEX:
3059 static ssize_t qla4xxx_show_boot_ini_info(void *data, int type, char *buf)
3061 struct scsi_qla_host *ha = data;
3066 case ISCSI_BOOT_INI_INITIATOR_NAME:
3067 rc = sprintf(str, "%s\n", ha->name_string);
3076 static mode_t qla4xxx_ini_get_attr_visibility(void *data, int type)
3081 case ISCSI_BOOT_INI_INITIATOR_NAME:
3092 qla4xxx_show_boot_tgt_info(struct ql4_boot_session_info *boot_sess, int type,
3095 struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
3100 case ISCSI_BOOT_TGT_NAME:
3101 rc = sprintf(buf, "%s\n", (char *)&boot_sess->target_name);
3103 case ISCSI_BOOT_TGT_IP_ADDR:
3104 if (boot_sess->conn_list[0].dest_ipaddr.ip_type == 0x1)
3105 rc = sprintf(buf, "%pI4\n",
3106 &boot_conn->dest_ipaddr.ip_address);
3108 rc = sprintf(str, "%pI6\n",
3109 &boot_conn->dest_ipaddr.ip_address);
3111 case ISCSI_BOOT_TGT_PORT:
3112 rc = sprintf(str, "%d\n", boot_conn->dest_port);
3114 case ISCSI_BOOT_TGT_CHAP_NAME:
3115 rc = sprintf(str, "%.*s\n",
3116 boot_conn->chap.target_chap_name_length,
3117 (char *)&boot_conn->chap.target_chap_name);
3119 case ISCSI_BOOT_TGT_CHAP_SECRET:
3120 rc = sprintf(str, "%.*s\n",
3121 boot_conn->chap.target_secret_length,
3122 (char *)&boot_conn->chap.target_secret);
3124 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
3125 rc = sprintf(str, "%.*s\n",
3126 boot_conn->chap.intr_chap_name_length,
3127 (char *)&boot_conn->chap.intr_chap_name);
3129 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
3130 rc = sprintf(str, "%.*s\n",
3131 boot_conn->chap.intr_secret_length,
3132 (char *)&boot_conn->chap.intr_secret);
3134 case ISCSI_BOOT_TGT_FLAGS:
3135 rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
3137 case ISCSI_BOOT_TGT_NIC_ASSOC:
3138 rc = sprintf(str, "0\n");
3147 static ssize_t qla4xxx_show_boot_tgt_pri_info(void *data, int type, char *buf)
3149 struct scsi_qla_host *ha = data;
3150 struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_pri_sess);
3152 return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
3155 static ssize_t qla4xxx_show_boot_tgt_sec_info(void *data, int type, char *buf)
3157 struct scsi_qla_host *ha = data;
3158 struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_sec_sess);
3160 return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
3163 static mode_t qla4xxx_tgt_get_attr_visibility(void *data, int type)
3168 case ISCSI_BOOT_TGT_NAME:
3169 case ISCSI_BOOT_TGT_IP_ADDR:
3170 case ISCSI_BOOT_TGT_PORT:
3171 case ISCSI_BOOT_TGT_CHAP_NAME:
3172 case ISCSI_BOOT_TGT_CHAP_SECRET:
3173 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
3174 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
3175 case ISCSI_BOOT_TGT_NIC_ASSOC:
3176 case ISCSI_BOOT_TGT_FLAGS:
3186 static void qla4xxx_boot_release(void *data)
3188 struct scsi_qla_host *ha = data;
3190 scsi_host_put(ha->host);
3193 static int get_fw_boot_info(struct scsi_qla_host *ha, uint16_t ddb_index[])
3196 uint32_t addr, pri_addr, sec_addr;
3200 uint8_t *buf = NULL;
3201 size_t size = 13 * sizeof(uint8_t);
3202 int ret = QLA_SUCCESS;
3204 func_num = PCI_FUNC(ha->pdev->devfn);
3206 ql4_printk(KERN_INFO, ha, "%s: Get FW boot info for 0x%x func %d\n",
3207 __func__, ha->pdev->device, func_num);
3209 if (is_qla40XX(ha)) {
3210 if (func_num == 1) {
3211 addr = NVRAM_PORT0_BOOT_MODE;
3212 pri_addr = NVRAM_PORT0_BOOT_PRI_TGT;
3213 sec_addr = NVRAM_PORT0_BOOT_SEC_TGT;
3214 } else if (func_num == 3) {
3215 addr = NVRAM_PORT1_BOOT_MODE;
3216 pri_addr = NVRAM_PORT1_BOOT_PRI_TGT;
3217 sec_addr = NVRAM_PORT1_BOOT_SEC_TGT;
3220 goto exit_boot_info;
3223 /* Check Boot Mode */
3224 val = rd_nvram_byte(ha, addr);
3225 if (!(val & 0x07)) {
3226 DEBUG2(ql4_printk(KERN_ERR, ha,
3227 "%s: Failed Boot options : 0x%x\n",
3230 goto exit_boot_info;
3233 /* get primary valid target index */
3234 val = rd_nvram_byte(ha, pri_addr);
3236 ddb_index[0] = (val & 0x7f);
3238 /* get secondary valid target index */
3239 val = rd_nvram_byte(ha, sec_addr);
3241 ddb_index[1] = (val & 0x7f);
3243 } else if (is_qla8022(ha)) {
3244 buf = dma_alloc_coherent(&ha->pdev->dev, size,
3245 &buf_dma, GFP_KERNEL);
3247 DEBUG2(ql4_printk(KERN_ERR, ha,
3248 "%s: Unable to allocate dma buffer\n",
3251 goto exit_boot_info;
3254 if (ha->port_num == 0)
3255 offset = BOOT_PARAM_OFFSET_PORT0;
3256 else if (ha->port_num == 1)
3257 offset = BOOT_PARAM_OFFSET_PORT1;
3260 goto exit_boot_info_free;
3262 addr = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_iscsi_param * 4) +
3264 if (qla4xxx_get_flash(ha, buf_dma, addr,
3265 13 * sizeof(uint8_t)) != QLA_SUCCESS) {
3266 DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: Get Flash"
3267 "failed\n", ha->host_no, __func__));
3269 goto exit_boot_info_free;
3271 /* Check Boot Mode */
3272 if (!(buf[1] & 0x07)) {
3273 DEBUG2(ql4_printk(KERN_INFO, ha,
3274 "Failed: Boot options : 0x%x\n",
3277 goto exit_boot_info_free;
3280 /* get primary valid target index */
3282 ddb_index[0] = buf[2] & 0x7f;
3284 /* get secondary valid target index */
3285 if (buf[11] & BIT_7)
3286 ddb_index[1] = buf[11] & 0x7f;
3289 goto exit_boot_info;
3292 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Primary target ID %d, Secondary"
3293 " target ID %d\n", __func__, ddb_index[0],
3296 ha->pri_ddb_idx = ddb_index[0];
3297 ha->sec_ddb_idx = ddb_index[1];
3299 exit_boot_info_free:
3300 dma_free_coherent(&ha->pdev->dev, size, buf, buf_dma);
3306 * qla4xxx_get_bidi_chap - Get a BIDI CHAP user and password
3307 * @ha: pointer to adapter structure
3308 * @username: CHAP username to be returned
3309 * @password: CHAP password to be returned
3311 * If a boot entry has BIDI CHAP enabled then we need to set the BIDI CHAP
3312 * user and password in the sysfs entry in /sys/firmware/iscsi_boot#/.
3313 * So from the CHAP cache find the first BIDI CHAP entry and set it
3314 * to the boot record in sysfs.
3316 static int qla4xxx_get_bidi_chap(struct scsi_qla_host *ha, char *username,
3319 int i, ret = -EINVAL;
3320 int max_chap_entries = 0;
3321 struct ql4_chap_table *chap_table;
3324 max_chap_entries = (ha->hw.flt_chap_size / 2) /
3325 sizeof(struct ql4_chap_table);
3327 max_chap_entries = MAX_CHAP_ENTRIES_40XX;
3329 if (!ha->chap_list) {
3330 ql4_printk(KERN_ERR, ha, "Do not have CHAP table cache\n");
3334 mutex_lock(&ha->chap_sem);
3335 for (i = 0; i < max_chap_entries; i++) {
3336 chap_table = (struct ql4_chap_table *)ha->chap_list + i;
3337 if (chap_table->cookie !=
3338 __constant_cpu_to_le16(CHAP_VALID_COOKIE)) {
3342 if (chap_table->flags & BIT_7) /* local */
3345 if (!(chap_table->flags & BIT_6)) /* Not BIDI */
3348 strncpy(password, chap_table->secret, QL4_CHAP_MAX_SECRET_LEN);
3349 strncpy(username, chap_table->name, QL4_CHAP_MAX_NAME_LEN);
3353 mutex_unlock(&ha->chap_sem);
3359 static int qla4xxx_get_boot_target(struct scsi_qla_host *ha,
3360 struct ql4_boot_session_info *boot_sess,
3363 struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
3364 struct dev_db_entry *fw_ddb_entry;
3365 dma_addr_t fw_ddb_entry_dma;
3368 int ret = QLA_SUCCESS;
3370 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3371 &fw_ddb_entry_dma, GFP_KERNEL);
3372 if (!fw_ddb_entry) {
3373 DEBUG2(ql4_printk(KERN_ERR, ha,
3374 "%s: Unable to allocate dma buffer.\n",
3380 if (qla4xxx_bootdb_by_index(ha, fw_ddb_entry,
3381 fw_ddb_entry_dma, ddb_index)) {
3382 DEBUG2(ql4_printk(KERN_ERR, ha,
3383 "%s: Flash DDB read Failed\n", __func__));
3385 goto exit_boot_target;
3388 /* Update target name and IP from DDB */
3389 memcpy(boot_sess->target_name, fw_ddb_entry->iscsi_name,
3390 min(sizeof(boot_sess->target_name),
3391 sizeof(fw_ddb_entry->iscsi_name)));
3393 options = le16_to_cpu(fw_ddb_entry->options);
3394 if (options & DDB_OPT_IPV6_DEVICE) {
3395 memcpy(&boot_conn->dest_ipaddr.ip_address,
3396 &fw_ddb_entry->ip_addr[0], IPv6_ADDR_LEN);
3398 boot_conn->dest_ipaddr.ip_type = 0x1;
3399 memcpy(&boot_conn->dest_ipaddr.ip_address,
3400 &fw_ddb_entry->ip_addr[0], IP_ADDR_LEN);
3403 boot_conn->dest_port = le16_to_cpu(fw_ddb_entry->port);
3405 /* update chap information */
3406 idx = __le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
3408 if (BIT_7 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
3410 DEBUG2(ql4_printk(KERN_INFO, ha, "Setting chap\n"));
3412 ret = qla4xxx_get_chap(ha, (char *)&boot_conn->chap.
3414 (char *)&boot_conn->chap.target_secret,
3417 ql4_printk(KERN_ERR, ha, "Failed to set chap\n");
3419 goto exit_boot_target;
3422 boot_conn->chap.target_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
3423 boot_conn->chap.target_secret_length = QL4_CHAP_MAX_SECRET_LEN;
3426 if (BIT_4 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
3428 DEBUG2(ql4_printk(KERN_INFO, ha, "Setting BIDI chap\n"));
3430 ret = qla4xxx_get_bidi_chap(ha,
3431 (char *)&boot_conn->chap.intr_chap_name,
3432 (char *)&boot_conn->chap.intr_secret);
3435 ql4_printk(KERN_ERR, ha, "Failed to set BIDI chap\n");
3437 goto exit_boot_target;
3440 boot_conn->chap.intr_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
3441 boot_conn->chap.intr_secret_length = QL4_CHAP_MAX_SECRET_LEN;
3445 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3446 fw_ddb_entry, fw_ddb_entry_dma);
3450 static int qla4xxx_get_boot_info(struct scsi_qla_host *ha)
3452 uint16_t ddb_index[2];
3453 int ret = QLA_ERROR;
3456 memset(ddb_index, 0, sizeof(ddb_index));
3457 ddb_index[0] = 0xffff;
3458 ddb_index[1] = 0xffff;
3459 ret = get_fw_boot_info(ha, ddb_index);
3460 if (ret != QLA_SUCCESS) {
3461 DEBUG2(ql4_printk(KERN_ERR, ha,
3462 "%s: Failed to set boot info.\n", __func__));
3466 if (ql4xdisablesysfsboot)
3469 if (ddb_index[0] == 0xffff)
3472 rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_pri_sess),
3474 if (rval != QLA_SUCCESS) {
3475 DEBUG2(ql4_printk(KERN_ERR, ha, "%s: Failed to get "
3476 "primary target\n", __func__));
3481 if (ddb_index[1] == 0xffff)
3482 goto exit_get_boot_info;
3484 rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_sec_sess),
3486 if (rval != QLA_SUCCESS) {
3487 DEBUG2(ql4_printk(KERN_ERR, ha, "%s: Failed to get "
3488 "secondary target\n", __func__));
3496 static int qla4xxx_setup_boot_info(struct scsi_qla_host *ha)
3498 struct iscsi_boot_kobj *boot_kobj;
3500 if (qla4xxx_get_boot_info(ha) != QLA_SUCCESS)
3503 if (ql4xdisablesysfsboot) {
3504 ql4_printk(KERN_INFO, ha,
3505 "%s: syfsboot disabled - driver will trigger login"
3506 "and publish session for discovery .\n", __func__);
3511 ha->boot_kset = iscsi_boot_create_host_kset(ha->host->host_no);
3515 if (!scsi_host_get(ha->host))
3517 boot_kobj = iscsi_boot_create_target(ha->boot_kset, 0, ha,
3518 qla4xxx_show_boot_tgt_pri_info,
3519 qla4xxx_tgt_get_attr_visibility,
3520 qla4xxx_boot_release);
3524 if (!scsi_host_get(ha->host))
3526 boot_kobj = iscsi_boot_create_target(ha->boot_kset, 1, ha,
3527 qla4xxx_show_boot_tgt_sec_info,
3528 qla4xxx_tgt_get_attr_visibility,
3529 qla4xxx_boot_release);
3533 if (!scsi_host_get(ha->host))
3535 boot_kobj = iscsi_boot_create_initiator(ha->boot_kset, 0, ha,
3536 qla4xxx_show_boot_ini_info,
3537 qla4xxx_ini_get_attr_visibility,
3538 qla4xxx_boot_release);
3542 if (!scsi_host_get(ha->host))
3544 boot_kobj = iscsi_boot_create_ethernet(ha->boot_kset, 0, ha,
3545 qla4xxx_show_boot_eth_info,
3546 qla4xxx_eth_get_attr_visibility,
3547 qla4xxx_boot_release);
3554 scsi_host_put(ha->host);
3556 iscsi_boot_destroy_kset(ha->boot_kset);
3562 * qla4xxx_create chap_list - Create CHAP list from FLASH
3563 * @ha: pointer to adapter structure
3565 * Read flash and make a list of CHAP entries, during login when a CHAP entry
3566 * is received, it will be checked in this list. If entry exist then the CHAP
3567 * entry index is set in the DDB. If CHAP entry does not exist in this list
3568 * then a new entry is added in FLASH in CHAP table and the index obtained is
3571 static void qla4xxx_create_chap_list(struct scsi_qla_host *ha)
3574 uint8_t *chap_flash_data = NULL;
3576 dma_addr_t chap_dma;
3577 uint32_t chap_size = 0;
3580 chap_size = MAX_CHAP_ENTRIES_40XX *
3581 sizeof(struct ql4_chap_table);
3582 else /* Single region contains CHAP info for both
3583 * ports which is divided into half for each port.
3585 chap_size = ha->hw.flt_chap_size / 2;
3587 chap_flash_data = dma_alloc_coherent(&ha->pdev->dev, chap_size,
3588 &chap_dma, GFP_KERNEL);
3589 if (!chap_flash_data) {
3590 ql4_printk(KERN_ERR, ha, "No memory for chap_flash_data\n");
3594 offset = FLASH_CHAP_OFFSET;
3596 offset = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_region_chap << 2);
3597 if (ha->port_num == 1)
3598 offset += chap_size;
3601 rval = qla4xxx_get_flash(ha, chap_dma, offset, chap_size);
3602 if (rval != QLA_SUCCESS)
3603 goto exit_chap_list;
3605 if (ha->chap_list == NULL)
3606 ha->chap_list = vmalloc(chap_size);
3607 if (ha->chap_list == NULL) {
3608 ql4_printk(KERN_ERR, ha, "No memory for ha->chap_list\n");
3609 goto exit_chap_list;
3612 memcpy(ha->chap_list, chap_flash_data, chap_size);
3615 dma_free_coherent(&ha->pdev->dev, chap_size,
3616 chap_flash_data, chap_dma);
3619 static void qla4xxx_get_param_ddb(struct ddb_entry *ddb_entry,
3620 struct ql4_tuple_ddb *tddb)
3622 struct scsi_qla_host *ha;
3623 struct iscsi_cls_session *cls_sess;
3624 struct iscsi_cls_conn *cls_conn;
3625 struct iscsi_session *sess;
3626 struct iscsi_conn *conn;
3628 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
3630 cls_sess = ddb_entry->sess;
3631 sess = cls_sess->dd_data;
3632 cls_conn = ddb_entry->conn;
3633 conn = cls_conn->dd_data;
3635 tddb->tpgt = sess->tpgt;
3636 tddb->port = conn->persistent_port;
3637 strncpy(tddb->iscsi_name, sess->targetname, ISCSI_NAME_SIZE);
3638 strncpy(tddb->ip_addr, conn->persistent_address, DDB_IPADDR_LEN);
3641 static void qla4xxx_convert_param_ddb(struct dev_db_entry *fw_ddb_entry,
3642 struct ql4_tuple_ddb *tddb)
3644 uint16_t options = 0;
3646 tddb->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
3647 memcpy(&tddb->iscsi_name[0], &fw_ddb_entry->iscsi_name[0],
3648 min(sizeof(tddb->iscsi_name), sizeof(fw_ddb_entry->iscsi_name)));
3650 options = le16_to_cpu(fw_ddb_entry->options);
3651 if (options & DDB_OPT_IPV6_DEVICE)
3652 sprintf(tddb->ip_addr, "%pI6", fw_ddb_entry->ip_addr);
3654 sprintf(tddb->ip_addr, "%pI4", fw_ddb_entry->ip_addr);
3656 tddb->port = le16_to_cpu(fw_ddb_entry->port);
3659 static int qla4xxx_compare_tuple_ddb(struct scsi_qla_host *ha,
3660 struct ql4_tuple_ddb *old_tddb,
3661 struct ql4_tuple_ddb *new_tddb)
3663 if (strcmp(old_tddb->iscsi_name, new_tddb->iscsi_name))
3666 if (strcmp(old_tddb->ip_addr, new_tddb->ip_addr))
3669 if (old_tddb->port != new_tddb->port)
3672 DEBUG2(ql4_printk(KERN_INFO, ha,
3673 "Match Found, fw[%d,%d,%s,%s], [%d,%d,%s,%s]",
3674 old_tddb->port, old_tddb->tpgt, old_tddb->ip_addr,
3675 old_tddb->iscsi_name, new_tddb->port, new_tddb->tpgt,
3676 new_tddb->ip_addr, new_tddb->iscsi_name));
3681 static int qla4xxx_is_session_exists(struct scsi_qla_host *ha,
3682 struct dev_db_entry *fw_ddb_entry)
3684 struct ddb_entry *ddb_entry;
3685 struct ql4_tuple_ddb *fw_tddb = NULL;
3686 struct ql4_tuple_ddb *tmp_tddb = NULL;
3688 int ret = QLA_ERROR;
3690 fw_tddb = vzalloc(sizeof(*fw_tddb));
3692 DEBUG2(ql4_printk(KERN_WARNING, ha,
3693 "Memory Allocation failed.\n"));
3698 tmp_tddb = vzalloc(sizeof(*tmp_tddb));
3700 DEBUG2(ql4_printk(KERN_WARNING, ha,
3701 "Memory Allocation failed.\n"));
3706 qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb);
3708 for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
3709 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
3710 if (ddb_entry == NULL)
3713 qla4xxx_get_param_ddb(ddb_entry, tmp_tddb);
3714 if (!qla4xxx_compare_tuple_ddb(ha, fw_tddb, tmp_tddb)) {
3715 ret = QLA_SUCCESS; /* found */
3728 static int qla4xxx_is_flash_ddb_exists(struct scsi_qla_host *ha,
3729 struct list_head *list_nt,
3730 struct dev_db_entry *fw_ddb_entry)
3732 struct qla_ddb_index *nt_ddb_idx, *nt_ddb_idx_tmp;
3733 struct ql4_tuple_ddb *fw_tddb = NULL;
3734 struct ql4_tuple_ddb *tmp_tddb = NULL;
3735 int ret = QLA_ERROR;
3737 fw_tddb = vzalloc(sizeof(*fw_tddb));
3739 DEBUG2(ql4_printk(KERN_WARNING, ha,
3740 "Memory Allocation failed.\n"));
3745 tmp_tddb = vzalloc(sizeof(*tmp_tddb));
3747 DEBUG2(ql4_printk(KERN_WARNING, ha,
3748 "Memory Allocation failed.\n"));
3753 qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb);
3755 list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
3756 qla4xxx_convert_param_ddb(&nt_ddb_idx->fw_ddb, tmp_tddb);
3757 if (!qla4xxx_compare_tuple_ddb(ha, fw_tddb, tmp_tddb)) {
3758 ret = QLA_SUCCESS; /* found */
3771 static void qla4xxx_free_nt_list(struct list_head *list_nt)
3773 struct qla_ddb_index *nt_ddb_idx, *nt_ddb_idx_tmp;
3775 /* Free up the normaltargets list */
3776 list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
3777 list_del_init(&nt_ddb_idx->list);
3783 static struct iscsi_endpoint *qla4xxx_get_ep_fwdb(struct scsi_qla_host *ha,
3784 struct dev_db_entry *fw_ddb_entry)
3786 struct iscsi_endpoint *ep;
3787 struct sockaddr_in *addr;
3788 struct sockaddr_in6 *addr6;
3789 struct sockaddr *dst_addr;
3792 /* TODO: need to destroy on unload iscsi_endpoint*/
3793 dst_addr = vmalloc(sizeof(*dst_addr));
3797 if (fw_ddb_entry->options & DDB_OPT_IPV6_DEVICE) {
3798 dst_addr->sa_family = AF_INET6;
3799 addr6 = (struct sockaddr_in6 *)dst_addr;
3800 ip = (char *)&addr6->sin6_addr;
3801 memcpy(ip, fw_ddb_entry->ip_addr, IPv6_ADDR_LEN);
3802 addr6->sin6_port = htons(le16_to_cpu(fw_ddb_entry->port));
3805 dst_addr->sa_family = AF_INET;
3806 addr = (struct sockaddr_in *)dst_addr;
3807 ip = (char *)&addr->sin_addr;
3808 memcpy(ip, fw_ddb_entry->ip_addr, IP_ADDR_LEN);
3809 addr->sin_port = htons(le16_to_cpu(fw_ddb_entry->port));
3812 ep = qla4xxx_ep_connect(ha->host, dst_addr, 0);
3817 static int qla4xxx_verify_boot_idx(struct scsi_qla_host *ha, uint16_t idx)
3819 if (ql4xdisablesysfsboot)
3821 if (idx == ha->pri_ddb_idx || idx == ha->sec_ddb_idx)
3826 static void qla4xxx_setup_flash_ddb_entry(struct scsi_qla_host *ha,
3827 struct ddb_entry *ddb_entry)
3829 ddb_entry->ddb_type = FLASH_DDB;
3830 ddb_entry->fw_ddb_index = INVALID_ENTRY;
3831 ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
3833 ddb_entry->unblock_sess = qla4xxx_unblock_flash_ddb;
3834 ddb_entry->ddb_change = qla4xxx_flash_ddb_change;
3836 atomic_set(&ddb_entry->retry_relogin_timer, INVALID_ENTRY);
3837 atomic_set(&ddb_entry->relogin_timer, 0);
3838 atomic_set(&ddb_entry->relogin_retry_count, 0);
3840 ddb_entry->default_relogin_timeout =
3841 le16_to_cpu(ddb_entry->fw_ddb_entry.def_timeout);
3842 ddb_entry->default_time2wait =
3843 le16_to_cpu(ddb_entry->fw_ddb_entry.iscsi_def_time2wait);
3846 static void qla4xxx_wait_for_ip_configuration(struct scsi_qla_host *ha)
3849 uint32_t ip_idx[IP_ADDR_COUNT] = {0, 1, 2, 3}; /* 4 IP interfaces */
3850 uint32_t sts[MBOX_REG_COUNT];
3852 unsigned long wtime;
3855 wtime = jiffies + (HZ * IP_CONFIG_TOV);
3857 for (idx = 0; idx < IP_ADDR_COUNT; idx++) {
3858 if (ip_idx[idx] == -1)
3861 ret = qla4xxx_get_ip_state(ha, 0, ip_idx[idx], sts);
3863 if (ret == QLA_ERROR) {
3868 ip_state = (sts[1] & IP_STATE_MASK) >> IP_STATE_SHIFT;
3870 DEBUG2(ql4_printk(KERN_INFO, ha,
3871 "Waiting for IP state for idx = %d, state = 0x%x\n",
3872 ip_idx[idx], ip_state));
3873 if (ip_state == IP_ADDRSTATE_UNCONFIGURED ||
3874 ip_state == IP_ADDRSTATE_INVALID ||
3875 ip_state == IP_ADDRSTATE_PREFERRED ||
3876 ip_state == IP_ADDRSTATE_DEPRICATED ||
3877 ip_state == IP_ADDRSTATE_DISABLING)
3882 /* Break if all IP states checked */
3883 if ((ip_idx[0] == -1) &&
3884 (ip_idx[1] == -1) &&
3885 (ip_idx[2] == -1) &&
3888 schedule_timeout_uninterruptible(HZ);
3889 } while (time_after(wtime, jiffies));
3892 void qla4xxx_build_ddb_list(struct scsi_qla_host *ha, int is_reset)
3896 uint32_t idx = 0, next_idx = 0;
3897 uint32_t state = 0, conn_err = 0;
3899 struct dev_db_entry *fw_ddb_entry;
3900 struct ddb_entry *ddb_entry = NULL;
3901 dma_addr_t fw_ddb_dma;
3902 struct iscsi_cls_session *cls_sess;
3903 struct iscsi_session *sess;
3904 struct iscsi_cls_conn *cls_conn;
3905 struct iscsi_endpoint *ep;
3906 uint16_t cmds_max = 32, tmo = 0;
3907 uint32_t initial_cmdsn = 0;
3908 struct list_head list_st, list_nt; /* List of sendtargets */
3909 struct qla_ddb_index *st_ddb_idx, *st_ddb_idx_tmp;
3911 unsigned long wtime;
3912 struct qla_ddb_index *nt_ddb_idx;
3914 if (!test_bit(AF_LINK_UP, &ha->flags)) {
3915 set_bit(AF_BUILD_DDB_LIST, &ha->flags);
3916 ha->is_reset = is_reset;
3919 max_ddbs = is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
3922 fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
3924 if (fw_ddb_entry == NULL) {
3925 DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
3929 INIT_LIST_HEAD(&list_st);
3930 INIT_LIST_HEAD(&list_nt);
3931 fw_idx_size = sizeof(struct qla_ddb_index);
3933 for (idx = 0; idx < max_ddbs; idx = next_idx) {
3934 ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry,
3936 &next_idx, &state, &conn_err,
3938 if (ret == QLA_ERROR)
3941 if (qla4xxx_verify_boot_idx(ha, idx) != QLA_SUCCESS)
3942 goto continue_next_st;
3944 /* Check if ST, add to the list_st */
3945 if (strlen((char *) fw_ddb_entry->iscsi_name) != 0)
3946 goto continue_next_st;
3948 st_ddb_idx = vzalloc(fw_idx_size);
3952 st_ddb_idx->fw_ddb_idx = idx;
3954 list_add_tail(&st_ddb_idx->list, &list_st);
3960 /* Before issuing conn open mbox, ensure all IPs states are configured
3961 * Note, conn open fails if IPs are not configured
3963 qla4xxx_wait_for_ip_configuration(ha);
3965 /* Go thru the STs and fire the sendtargets by issuing conn open mbx */
3966 list_for_each_entry_safe(st_ddb_idx, st_ddb_idx_tmp, &list_st, list) {
3967 qla4xxx_conn_open(ha, st_ddb_idx->fw_ddb_idx);
3970 /* Wait to ensure all sendtargets are done for min 12 sec wait */
3971 tmo = ((ha->def_timeout < LOGIN_TOV) ? LOGIN_TOV : ha->def_timeout);
3972 DEBUG2(ql4_printk(KERN_INFO, ha,
3973 "Default time to wait for build ddb %d\n", tmo));
3975 wtime = jiffies + (HZ * tmo);
3977 list_for_each_entry_safe(st_ddb_idx, st_ddb_idx_tmp, &list_st,
3979 ret = qla4xxx_get_fwddb_entry(ha,
3980 st_ddb_idx->fw_ddb_idx,
3981 NULL, 0, NULL, &next_idx,
3982 &state, &conn_err, NULL,
3984 if (ret == QLA_ERROR)
3987 if (state == DDB_DS_NO_CONNECTION_ACTIVE ||
3988 state == DDB_DS_SESSION_FAILED) {
3989 list_del_init(&st_ddb_idx->list);
3993 schedule_timeout_uninterruptible(HZ / 10);
3994 } while (time_after(wtime, jiffies));
3996 /* Free up the sendtargets list */
3997 list_for_each_entry_safe(st_ddb_idx, st_ddb_idx_tmp, &list_st, list) {
3998 list_del_init(&st_ddb_idx->list);
4002 for (idx = 0; idx < max_ddbs; idx = next_idx) {
4003 ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry,
4005 &next_idx, &state, &conn_err,
4007 if (ret == QLA_ERROR)
4010 if (qla4xxx_verify_boot_idx(ha, idx) != QLA_SUCCESS)
4011 goto continue_next_nt;
4013 /* Check if NT, then add to list it */
4014 if (strlen((char *) fw_ddb_entry->iscsi_name) == 0)
4015 goto continue_next_nt;
4017 if (state == DDB_DS_NO_CONNECTION_ACTIVE ||
4018 state == DDB_DS_SESSION_FAILED) {
4019 DEBUG2(ql4_printk(KERN_INFO, ha,
4020 "Adding DDB to session = 0x%x\n",
4022 if (is_reset == INIT_ADAPTER) {
4023 nt_ddb_idx = vmalloc(fw_idx_size);
4027 nt_ddb_idx->fw_ddb_idx = idx;
4029 memcpy(&nt_ddb_idx->fw_ddb, fw_ddb_entry,
4030 sizeof(struct dev_db_entry));
4032 if (qla4xxx_is_flash_ddb_exists(ha, &list_nt,
4033 fw_ddb_entry) == QLA_SUCCESS) {
4035 goto continue_next_nt;
4037 list_add_tail(&nt_ddb_idx->list, &list_nt);
4038 } else if (is_reset == RESET_ADAPTER) {
4039 if (qla4xxx_is_session_exists(ha,
4040 fw_ddb_entry) == QLA_SUCCESS)
4041 goto continue_next_nt;
4044 /* Create session object, with INVALID_ENTRY,
4045 * the targer_id would get set when we issue the login
4047 cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport,
4049 sizeof(struct ddb_entry),
4050 sizeof(struct ql4_task_data),
4051 initial_cmdsn, INVALID_ENTRY);
4056 * iscsi_session_setup increments the driver reference
4057 * count which wouldn't let the driver to be unloaded.
4058 * so calling module_put function to decrement the
4061 module_put(qla4xxx_iscsi_transport.owner);
4062 sess = cls_sess->dd_data;
4063 ddb_entry = sess->dd_data;
4064 ddb_entry->sess = cls_sess;
4066 cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
4067 memcpy(&ddb_entry->fw_ddb_entry, fw_ddb_entry,
4068 sizeof(struct dev_db_entry));
4070 qla4xxx_setup_flash_ddb_entry(ha, ddb_entry);
4072 cls_conn = iscsi_conn_setup(cls_sess,
4073 sizeof(struct qla_conn),
4078 ddb_entry->conn = cls_conn;
4080 /* Setup ep, for displaying attributes in sysfs */
4081 ep = qla4xxx_get_ep_fwdb(ha, fw_ddb_entry);
4083 ep->conn = cls_conn;
4086 DEBUG2(ql4_printk(KERN_ERR, ha,
4087 "Unable to get ep\n"));
4090 /* Update sess/conn params */
4091 qla4xxx_copy_fwddb_param(ha, fw_ddb_entry, cls_sess,
4094 if (is_reset == RESET_ADAPTER) {
4095 iscsi_block_session(cls_sess);
4096 /* Use the relogin path to discover new devices
4097 * by short-circuting the logic of setting
4098 * timer to relogin - instead set the flags
4099 * to initiate login right away.
4101 set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
4102 set_bit(DF_RELOGIN, &ddb_entry->flags);
4110 qla4xxx_free_nt_list(&list_nt);
4112 dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
4114 qla4xxx_free_ddb_index(ha);
4119 * qla4xxx_probe_adapter - callback function to probe HBA
4120 * @pdev: pointer to pci_dev structure
4121 * @pci_device_id: pointer to pci_device entry
4123 * This routine will probe for Qlogic 4xxx iSCSI host adapters.
4124 * It returns zero if successful. It also initializes all data necessary for
4127 static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
4128 const struct pci_device_id *ent)
4130 int ret = -ENODEV, status;
4131 struct Scsi_Host *host;
4132 struct scsi_qla_host *ha;
4133 uint8_t init_retry_count = 0;
4135 struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
4138 if (pci_enable_device(pdev))
4141 host = iscsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha), 0);
4144 "qla4xxx: Couldn't allocate host from scsi layer!\n");
4145 goto probe_disable_device;
4148 /* Clear our data area */
4149 ha = to_qla_host(host);
4150 memset(ha, 0, sizeof(*ha));
4152 /* Save the information from PCI BIOS. */
4155 ha->host_no = host->host_no;
4157 pci_enable_pcie_error_reporting(pdev);
4159 /* Setup Runtime configurable options */
4160 if (is_qla8022(ha)) {
4161 ha->isp_ops = &qla4_8xxx_isp_ops;
4162 rwlock_init(&ha->hw_lock);
4163 ha->qdr_sn_window = -1;
4164 ha->ddr_mn_window = -1;
4165 ha->curr_window = 255;
4166 ha->func_num = PCI_FUNC(ha->pdev->devfn);
4167 nx_legacy_intr = &legacy_intr[ha->func_num];
4168 ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
4169 ha->nx_legacy_intr.tgt_status_reg =
4170 nx_legacy_intr->tgt_status_reg;
4171 ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
4172 ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
4174 ha->isp_ops = &qla4xxx_isp_ops;
4177 /* Set EEH reset type to fundamental if required by hba */
4179 pdev->needs_freset = 1;
4181 /* Configure PCI I/O space. */
4182 ret = ha->isp_ops->iospace_config(ha);
4184 goto probe_failed_ioconfig;
4186 ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
4187 pdev->device, pdev->irq, ha->reg);
4189 qla4xxx_config_dma_addressing(ha);
4191 /* Initialize lists and spinlocks. */
4192 INIT_LIST_HEAD(&ha->free_srb_q);
4194 mutex_init(&ha->mbox_sem);
4195 mutex_init(&ha->chap_sem);
4196 init_completion(&ha->mbx_intr_comp);
4197 init_completion(&ha->disable_acb_comp);
4199 spin_lock_init(&ha->hardware_lock);
4201 /* Allocate dma buffers */
4202 if (qla4xxx_mem_alloc(ha)) {
4203 ql4_printk(KERN_WARNING, ha,
4204 "[ERROR] Failed to allocate memory for adapter\n");
4210 host->cmd_per_lun = 3;
4211 host->max_channel = 0;
4212 host->max_lun = MAX_LUNS - 1;
4213 host->max_id = MAX_TARGETS;
4214 host->max_cmd_len = IOCB_MAX_CDB_LEN;
4215 host->can_queue = MAX_SRBS ;
4216 host->transportt = qla4xxx_scsi_transport;
4218 ret = scsi_init_shared_tag_map(host, MAX_SRBS);
4220 ql4_printk(KERN_WARNING, ha,
4221 "%s: scsi_init_shared_tag_map failed\n", __func__);
4225 pci_set_drvdata(pdev, ha);
4227 ret = scsi_add_host(host, &pdev->dev);
4232 (void) qla4_8xxx_get_flash_info(ha);
4235 * Initialize the Host adapter request/response queues and
4237 * NOTE: interrupts enabled upon successful completion
4239 status = qla4xxx_initialize_adapter(ha, INIT_ADAPTER);
4240 while ((!test_bit(AF_ONLINE, &ha->flags)) &&
4241 init_retry_count++ < MAX_INIT_RETRIES) {
4243 if (is_qla8022(ha)) {
4244 qla4_8xxx_idc_lock(ha);
4245 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
4246 qla4_8xxx_idc_unlock(ha);
4247 if (dev_state == QLA82XX_DEV_FAILED) {
4248 ql4_printk(KERN_WARNING, ha, "%s: don't retry "
4249 "initialize adapter. H/W is in failed state\n",
4254 DEBUG2(printk("scsi: %s: retrying adapter initialization "
4255 "(%d)\n", __func__, init_retry_count));
4257 if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
4260 status = qla4xxx_initialize_adapter(ha, INIT_ADAPTER);
4263 if (!test_bit(AF_ONLINE, &ha->flags)) {
4264 ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
4266 if (is_qla8022(ha) && ql4xdontresethba) {
4267 /* Put the device in failed state. */
4268 DEBUG2(printk(KERN_ERR "HW STATE: FAILED\n"));
4269 qla4_8xxx_idc_lock(ha);
4270 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
4271 QLA82XX_DEV_FAILED);
4272 qla4_8xxx_idc_unlock(ha);
4278 /* Startup the kernel thread for this host adapter. */
4279 DEBUG2(printk("scsi: %s: Starting kernel thread for "
4280 "qla4xxx_dpc\n", __func__));
4281 sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
4282 ha->dpc_thread = create_singlethread_workqueue(buf);
4283 if (!ha->dpc_thread) {
4284 ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
4288 INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
4290 sprintf(buf, "qla4xxx_%lu_task", ha->host_no);
4291 ha->task_wq = alloc_workqueue(buf, WQ_MEM_RECLAIM, 1);
4293 ql4_printk(KERN_WARNING, ha, "Unable to start task thread!\n");
4298 /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
4299 * (which is called indirectly by qla4xxx_initialize_adapter),
4300 * so that irqs will be registered after crbinit but before
4303 if (!is_qla8022(ha)) {
4304 ret = qla4xxx_request_irqs(ha);
4306 ql4_printk(KERN_WARNING, ha, "Failed to reserve "
4307 "interrupt %d already in use.\n", pdev->irq);
4312 pci_save_state(ha->pdev);
4313 ha->isp_ops->enable_intrs(ha);
4315 /* Start timer thread. */
4316 qla4xxx_start_timer(ha, qla4xxx_timer, 1);
4318 set_bit(AF_INIT_DONE, &ha->flags);
4321 " QLogic iSCSI HBA Driver version: %s\n"
4322 " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
4323 qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
4324 ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
4325 ha->patch_number, ha->build_number);
4327 if (qla4xxx_setup_boot_info(ha))
4328 ql4_printk(KERN_ERR, ha, "%s:ISCSI boot info setup failed\n",
4331 /* Perform the build ddb list and login to each */
4332 qla4xxx_build_ddb_list(ha, INIT_ADAPTER);
4333 iscsi_host_for_each_session(ha->host, qla4xxx_login_flash_ddb);
4335 qla4xxx_create_chap_list(ha);
4337 qla4xxx_create_ifaces(ha);
4341 scsi_remove_host(ha->host);
4344 qla4xxx_free_adapter(ha);
4346 probe_failed_ioconfig:
4347 pci_disable_pcie_error_reporting(pdev);
4348 scsi_host_put(ha->host);
4350 probe_disable_device:
4351 pci_disable_device(pdev);
4357 * qla4xxx_prevent_other_port_reinit - prevent other port from re-initialize
4358 * @ha: pointer to adapter structure
4360 * Mark the other ISP-4xxx port to indicate that the driver is being removed,
4361 * so that the other port will not re-initialize while in the process of
4362 * removing the ha due to driver unload or hba hotplug.
4364 static void qla4xxx_prevent_other_port_reinit(struct scsi_qla_host *ha)
4366 struct scsi_qla_host *other_ha = NULL;
4367 struct pci_dev *other_pdev = NULL;
4368 int fn = ISP4XXX_PCI_FN_2;
4370 /*iscsi function numbers for ISP4xxx is 1 and 3*/
4371 if (PCI_FUNC(ha->pdev->devfn) & BIT_1)
4372 fn = ISP4XXX_PCI_FN_1;
4375 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
4376 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
4379 /* Get other_ha if other_pdev is valid and state is enable*/
4381 if (atomic_read(&other_pdev->enable_cnt)) {
4382 other_ha = pci_get_drvdata(other_pdev);
4384 set_bit(AF_HA_REMOVAL, &other_ha->flags);
4385 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: "
4386 "Prevent %s reinit\n", __func__,
4387 dev_name(&other_ha->pdev->dev)));
4390 pci_dev_put(other_pdev);
4394 static void qla4xxx_destroy_fw_ddb_session(struct scsi_qla_host *ha)
4396 struct ddb_entry *ddb_entry;
4400 for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
4402 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
4403 if ((ddb_entry != NULL) &&
4404 (ddb_entry->ddb_type == FLASH_DDB)) {
4406 options = LOGOUT_OPTION_CLOSE_SESSION;
4407 if (qla4xxx_session_logout_ddb(ha, ddb_entry, options)
4409 ql4_printk(KERN_ERR, ha, "%s: Logout failed\n",
4412 qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
4414 * we have decremented the reference count of the driver
4415 * when we setup the session to have the driver unload
4416 * to be seamless without actually destroying the
4419 try_module_get(qla4xxx_iscsi_transport.owner);
4420 iscsi_destroy_endpoint(ddb_entry->conn->ep);
4421 qla4xxx_free_ddb(ha, ddb_entry);
4422 iscsi_session_teardown(ddb_entry->sess);
4427 * qla4xxx_remove_adapter - calback function to remove adapter.
4428 * @pci_dev: PCI device pointer
4430 static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
4432 struct scsi_qla_host *ha;
4434 ha = pci_get_drvdata(pdev);
4436 if (!is_qla8022(ha))
4437 qla4xxx_prevent_other_port_reinit(ha);
4439 /* destroy iface from sysfs */
4440 qla4xxx_destroy_ifaces(ha);
4442 if ((!ql4xdisablesysfsboot) && ha->boot_kset)
4443 iscsi_boot_destroy_kset(ha->boot_kset);
4445 qla4xxx_destroy_fw_ddb_session(ha);
4447 scsi_remove_host(ha->host);
4449 qla4xxx_free_adapter(ha);
4451 scsi_host_put(ha->host);
4453 pci_disable_pcie_error_reporting(pdev);
4454 pci_disable_device(pdev);
4455 pci_set_drvdata(pdev, NULL);
4459 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
4462 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
4463 * supported addressing method.
4465 static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
4469 /* Update our PCI device dma_mask for full 64 bit mask */
4470 if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
4471 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
4472 dev_dbg(&ha->pdev->dev,
4473 "Failed to set 64 bit PCI consistent mask; "
4475 retval = pci_set_consistent_dma_mask(ha->pdev,
4479 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
4482 static int qla4xxx_slave_alloc(struct scsi_device *sdev)
4484 struct iscsi_cls_session *cls_sess;
4485 struct iscsi_session *sess;
4486 struct ddb_entry *ddb;
4487 int queue_depth = QL4_DEF_QDEPTH;
4489 cls_sess = starget_to_session(sdev->sdev_target);
4490 sess = cls_sess->dd_data;
4491 ddb = sess->dd_data;
4493 sdev->hostdata = ddb;
4494 sdev->tagged_supported = 1;
4496 if (ql4xmaxqdepth != 0 && ql4xmaxqdepth <= 0xffffU)
4497 queue_depth = ql4xmaxqdepth;
4499 scsi_activate_tcq(sdev, queue_depth);
4503 static int qla4xxx_slave_configure(struct scsi_device *sdev)
4505 sdev->tagged_supported = 1;
4509 static void qla4xxx_slave_destroy(struct scsi_device *sdev)
4511 scsi_deactivate_tcq(sdev, 1);
4515 * qla4xxx_del_from_active_array - returns an active srb
4516 * @ha: Pointer to host adapter structure.
4517 * @index: index into the active_array
4519 * This routine removes and returns the srb at the specified index
4521 struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
4524 struct srb *srb = NULL;
4525 struct scsi_cmnd *cmd = NULL;
4527 cmd = scsi_host_find_tag(ha->host, index);
4531 srb = (struct srb *)CMD_SP(cmd);
4535 /* update counters */
4536 if (srb->flags & SRB_DMA_VALID) {
4537 ha->req_q_count += srb->iocb_cnt;
4538 ha->iocb_cnt -= srb->iocb_cnt;
4540 srb->cmd->host_scribble =
4541 (unsigned char *)(unsigned long) MAX_SRBS;
4547 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
4548 * @ha: Pointer to host adapter structure.
4549 * @cmd: Scsi Command to wait on.
4551 * This routine waits for the command to be returned by the Firmware
4552 * for some max time.
4554 static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
4555 struct scsi_cmnd *cmd)
4559 uint32_t max_wait_time = EH_WAIT_CMD_TOV;
4562 /* Dont wait on command if PCI error is being handled
4565 if (unlikely(pci_channel_offline(ha->pdev)) ||
4566 (test_bit(AF_EEH_BUSY, &ha->flags))) {
4567 ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
4568 ha->host_no, __func__);
4573 /* Checking to see if its returned to OS */
4574 rp = (struct srb *) CMD_SP(cmd);
4581 } while (max_wait_time--);
4587 * qla4xxx_wait_for_hba_online - waits for HBA to come online
4588 * @ha: Pointer to host adapter structure
4590 static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
4592 unsigned long wait_online;
4594 wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
4595 while (time_before(jiffies, wait_online)) {
4607 * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
4608 * @ha: pointer to HBA
4612 * This function waits for all outstanding commands to a lun to complete. It
4613 * returns 0 if all pending commands are returned and 1 otherwise.
4615 static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
4616 struct scsi_target *stgt,
4617 struct scsi_device *sdev)
4621 struct scsi_cmnd *cmd;
4624 * Waiting for all commands for the designated target or dev
4625 * in the active array
4627 for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
4628 cmd = scsi_host_find_tag(ha->host, cnt);
4629 if (cmd && stgt == scsi_target(cmd->device) &&
4630 (!sdev || sdev == cmd->device)) {
4631 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
4641 * qla4xxx_eh_abort - callback for abort task.
4642 * @cmd: Pointer to Linux's SCSI command structure
4644 * This routine is called by the Linux OS to abort the specified
4647 static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
4649 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
4650 unsigned int id = cmd->device->id;
4651 unsigned int lun = cmd->device->lun;
4652 unsigned long flags;
4653 struct srb *srb = NULL;
4657 ql4_printk(KERN_INFO, ha,
4658 "scsi%ld:%d:%d: Abort command issued cmd=%p\n",
4659 ha->host_no, id, lun, cmd);
4661 spin_lock_irqsave(&ha->hardware_lock, flags);
4662 srb = (struct srb *) CMD_SP(cmd);
4664 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4667 kref_get(&srb->srb_ref);
4668 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4670 if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
4671 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
4672 ha->host_no, id, lun));
4675 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
4676 ha->host_no, id, lun));
4680 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
4682 /* Wait for command to complete */
4684 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
4685 DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
4686 ha->host_no, id, lun));
4691 ql4_printk(KERN_INFO, ha,
4692 "scsi%ld:%d:%d: Abort command - %s\n",
4693 ha->host_no, id, lun, (ret == SUCCESS) ? "succeeded" : "failed");
4699 * qla4xxx_eh_device_reset - callback for target reset.
4700 * @cmd: Pointer to Linux's SCSI command structure
4702 * This routine is called by the Linux OS to reset all luns on the
4705 static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
4707 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
4708 struct ddb_entry *ddb_entry = cmd->device->hostdata;
4709 int ret = FAILED, stat;
4714 ret = iscsi_block_scsi_eh(cmd);
4719 ql4_printk(KERN_INFO, ha,
4720 "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
4721 cmd->device->channel, cmd->device->id, cmd->device->lun);
4723 DEBUG2(printk(KERN_INFO
4724 "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
4725 "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
4726 cmd, jiffies, cmd->request->timeout / HZ,
4727 ha->dpc_flags, cmd->result, cmd->allowed));
4729 /* FIXME: wait for hba to go online */
4730 stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
4731 if (stat != QLA_SUCCESS) {
4732 ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
4733 goto eh_dev_reset_done;
4736 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
4738 ql4_printk(KERN_INFO, ha,
4739 "DEVICE RESET FAILED - waiting for "
4741 goto eh_dev_reset_done;
4745 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
4746 MM_LUN_RESET) != QLA_SUCCESS)
4747 goto eh_dev_reset_done;
4749 ql4_printk(KERN_INFO, ha,
4750 "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
4751 ha->host_no, cmd->device->channel, cmd->device->id,
4762 * qla4xxx_eh_target_reset - callback for target reset.
4763 * @cmd: Pointer to Linux's SCSI command structure
4765 * This routine is called by the Linux OS to reset the target.
4767 static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
4769 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
4770 struct ddb_entry *ddb_entry = cmd->device->hostdata;
4776 ret = iscsi_block_scsi_eh(cmd);
4780 starget_printk(KERN_INFO, scsi_target(cmd->device),
4781 "WARM TARGET RESET ISSUED.\n");
4783 DEBUG2(printk(KERN_INFO
4784 "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
4785 "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
4786 ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
4787 ha->dpc_flags, cmd->result, cmd->allowed));
4789 stat = qla4xxx_reset_target(ha, ddb_entry);
4790 if (stat != QLA_SUCCESS) {
4791 starget_printk(KERN_INFO, scsi_target(cmd->device),
4792 "WARM TARGET RESET FAILED.\n");
4796 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
4798 starget_printk(KERN_INFO, scsi_target(cmd->device),
4799 "WARM TARGET DEVICE RESET FAILED - "
4800 "waiting for commands.\n");
4805 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
4806 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
4807 starget_printk(KERN_INFO, scsi_target(cmd->device),
4808 "WARM TARGET DEVICE RESET FAILED - "
4809 "marker iocb failed.\n");
4813 starget_printk(KERN_INFO, scsi_target(cmd->device),
4814 "WARM TARGET RESET SUCCEEDED.\n");
4819 * qla4xxx_eh_host_reset - kernel callback
4820 * @cmd: Pointer to Linux's SCSI command structure
4822 * This routine is invoked by the Linux kernel to perform fatal error
4823 * recovery on the specified adapter.
4825 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
4827 int return_status = FAILED;
4828 struct scsi_qla_host *ha;
4830 ha = to_qla_host(cmd->device->host);
4832 if (ql4xdontresethba) {
4833 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
4834 ha->host_no, __func__));
4838 ql4_printk(KERN_INFO, ha,
4839 "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
4840 cmd->device->channel, cmd->device->id, cmd->device->lun);
4842 if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
4843 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
4844 "DEAD.\n", ha->host_no, cmd->device->channel,
4850 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
4852 set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
4854 set_bit(DPC_RESET_HA, &ha->dpc_flags);
4857 if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
4858 return_status = SUCCESS;
4860 ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
4861 return_status == FAILED ? "FAILED" : "SUCCEEDED");
4863 return return_status;
4866 static int qla4xxx_context_reset(struct scsi_qla_host *ha)
4868 uint32_t mbox_cmd[MBOX_REG_COUNT];
4869 uint32_t mbox_sts[MBOX_REG_COUNT];
4870 struct addr_ctrl_blk_def *acb = NULL;
4871 uint32_t acb_len = sizeof(struct addr_ctrl_blk_def);
4872 int rval = QLA_SUCCESS;
4875 acb = dma_alloc_coherent(&ha->pdev->dev,
4876 sizeof(struct addr_ctrl_blk_def),
4877 &acb_dma, GFP_KERNEL);
4879 ql4_printk(KERN_ERR, ha, "%s: Unable to alloc acb\n",
4882 goto exit_port_reset;
4885 memset(acb, 0, acb_len);
4887 rval = qla4xxx_get_acb(ha, acb_dma, PRIMARI_ACB, acb_len);
4888 if (rval != QLA_SUCCESS) {
4893 rval = qla4xxx_disable_acb(ha);
4894 if (rval != QLA_SUCCESS) {
4899 wait_for_completion_timeout(&ha->disable_acb_comp,
4900 DISABLE_ACB_TOV * HZ);
4902 rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], acb_dma);
4903 if (rval != QLA_SUCCESS) {
4909 dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk_def),
4912 DEBUG2(ql4_printk(KERN_INFO, ha, "%s %s\n", __func__,
4913 rval == QLA_SUCCESS ? "SUCCEEDED" : "FAILED"));
4917 static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type)
4919 struct scsi_qla_host *ha = to_qla_host(shost);
4920 int rval = QLA_SUCCESS;
4922 if (ql4xdontresethba) {
4923 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Don't Reset HBA\n",
4926 goto exit_host_reset;
4929 rval = qla4xxx_wait_for_hba_online(ha);
4930 if (rval != QLA_SUCCESS) {
4931 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Unable to reset host "
4932 "adapter\n", __func__));
4934 goto exit_host_reset;
4937 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
4938 goto recover_adapter;
4940 switch (reset_type) {
4941 case SCSI_ADAPTER_RESET:
4942 set_bit(DPC_RESET_HA, &ha->dpc_flags);
4944 case SCSI_FIRMWARE_RESET:
4945 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
4947 /* set firmware context reset */
4948 set_bit(DPC_RESET_HA_FW_CONTEXT,
4951 rval = qla4xxx_context_reset(ha);
4952 goto exit_host_reset;
4959 rval = qla4xxx_recover_adapter(ha);
4960 if (rval != QLA_SUCCESS) {
4961 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: recover adapter fail\n",
4970 /* PCI AER driver recovers from all correctable errors w/o
4971 * driver intervention. For uncorrectable errors PCI AER
4972 * driver calls the following device driver's callbacks
4974 * - Fatal Errors - link_reset
4975 * - Non-Fatal Errors - driver's pci_error_detected() which
4976 * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
4978 * PCI AER driver calls
4979 * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
4980 * returns RECOVERED or NEED_RESET if fw_hung
4981 * NEED_RESET - driver's slot_reset()
4982 * DISCONNECT - device is dead & cannot recover
4983 * RECOVERED - driver's pci_resume()
4985 static pci_ers_result_t
4986 qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
4988 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
4990 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
4991 ha->host_no, __func__, state);
4993 if (!is_aer_supported(ha))
4994 return PCI_ERS_RESULT_NONE;
4997 case pci_channel_io_normal:
4998 clear_bit(AF_EEH_BUSY, &ha->flags);
4999 return PCI_ERS_RESULT_CAN_RECOVER;
5000 case pci_channel_io_frozen:
5001 set_bit(AF_EEH_BUSY, &ha->flags);
5002 qla4xxx_mailbox_premature_completion(ha);
5003 qla4xxx_free_irqs(ha);
5004 pci_disable_device(pdev);
5005 /* Return back all IOs */
5006 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
5007 return PCI_ERS_RESULT_NEED_RESET;
5008 case pci_channel_io_perm_failure:
5009 set_bit(AF_EEH_BUSY, &ha->flags);
5010 set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
5011 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
5012 return PCI_ERS_RESULT_DISCONNECT;
5014 return PCI_ERS_RESULT_NEED_RESET;
5018 * qla4xxx_pci_mmio_enabled() gets called if
5019 * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
5020 * and read/write to the device still works.
5022 static pci_ers_result_t
5023 qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
5025 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
5027 if (!is_aer_supported(ha))
5028 return PCI_ERS_RESULT_NONE;
5030 return PCI_ERS_RESULT_RECOVERED;
5033 static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
5035 uint32_t rval = QLA_ERROR;
5038 struct pci_dev *other_pdev = NULL;
5040 ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
5042 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
5044 if (test_bit(AF_ONLINE, &ha->flags)) {
5045 clear_bit(AF_ONLINE, &ha->flags);
5046 clear_bit(AF_LINK_UP, &ha->flags);
5047 iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
5048 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
5051 fn = PCI_FUNC(ha->pdev->devfn);
5054 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
5055 "func %x\n", ha->host_no, __func__, fn);
5056 /* Get the pci device given the domain, bus,
5057 * slot/function number */
5059 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
5060 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
5066 if (atomic_read(&other_pdev->enable_cnt)) {
5067 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
5068 "func in enabled state%x\n", ha->host_no,
5070 pci_dev_put(other_pdev);
5073 pci_dev_put(other_pdev);
5076 /* The first function on the card, the reset owner will
5077 * start & initialize the firmware. The other functions
5078 * on the card will reset the firmware context
5081 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
5082 "0x%x is the owner\n", ha->host_no, __func__,
5085 qla4_8xxx_idc_lock(ha);
5086 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
5089 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
5090 QLA82XX_IDC_VERSION);
5092 qla4_8xxx_idc_unlock(ha);
5093 clear_bit(AF_FW_RECOVERY, &ha->flags);
5094 rval = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
5095 qla4_8xxx_idc_lock(ha);
5097 if (rval != QLA_SUCCESS) {
5098 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
5099 "FAILED\n", ha->host_no, __func__);
5100 qla4_8xxx_clear_drv_active(ha);
5101 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
5102 QLA82XX_DEV_FAILED);
5104 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
5105 "READY\n", ha->host_no, __func__);
5106 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
5108 /* Clear driver state register */
5109 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
5110 qla4_8xxx_set_drv_active(ha);
5111 ret = qla4xxx_request_irqs(ha);
5113 ql4_printk(KERN_WARNING, ha, "Failed to "
5114 "reserve interrupt %d already in use.\n",
5118 ha->isp_ops->enable_intrs(ha);
5122 qla4_8xxx_idc_unlock(ha);
5124 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
5125 "the reset owner\n", ha->host_no, __func__,
5127 if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
5128 QLA82XX_DEV_READY)) {
5129 clear_bit(AF_FW_RECOVERY, &ha->flags);
5130 rval = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
5131 if (rval == QLA_SUCCESS) {
5132 ret = qla4xxx_request_irqs(ha);
5134 ql4_printk(KERN_WARNING, ha, "Failed to"
5135 " reserve interrupt %d already in"
5136 " use.\n", ha->pdev->irq);
5139 ha->isp_ops->enable_intrs(ha);
5143 qla4_8xxx_idc_lock(ha);
5144 qla4_8xxx_set_drv_active(ha);
5145 qla4_8xxx_idc_unlock(ha);
5148 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
5152 static pci_ers_result_t
5153 qla4xxx_pci_slot_reset(struct pci_dev *pdev)
5155 pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
5156 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
5159 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
5160 ha->host_no, __func__);
5162 if (!is_aer_supported(ha))
5163 return PCI_ERS_RESULT_NONE;
5165 /* Restore the saved state of PCIe device -
5166 * BAR registers, PCI Config space, PCIX, MSI,
5169 pci_restore_state(pdev);
5171 /* pci_restore_state() clears the saved_state flag of the device
5172 * save restored state which resets saved_state flag
5174 pci_save_state(pdev);
5176 /* Initialize device or resume if in suspended state */
5177 rc = pci_enable_device(pdev);
5179 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Can't re-enable "
5180 "device after reset\n", ha->host_no, __func__);
5181 goto exit_slot_reset;
5184 ha->isp_ops->disable_intrs(ha);
5186 if (is_qla8022(ha)) {
5187 if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
5188 ret = PCI_ERS_RESULT_RECOVERED;
5189 goto exit_slot_reset;
5191 goto exit_slot_reset;
5195 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
5196 "device after reset\n", ha->host_no, __func__, ret);
5201 qla4xxx_pci_resume(struct pci_dev *pdev)
5203 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
5206 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
5207 ha->host_no, __func__);
5209 ret = qla4xxx_wait_for_hba_online(ha);
5210 if (ret != QLA_SUCCESS) {
5211 ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
5212 "resume I/O from slot/link_reset\n", ha->host_no,
5216 pci_cleanup_aer_uncorrect_error_status(pdev);
5217 clear_bit(AF_EEH_BUSY, &ha->flags);
5220 static struct pci_error_handlers qla4xxx_err_handler = {
5221 .error_detected = qla4xxx_pci_error_detected,
5222 .mmio_enabled = qla4xxx_pci_mmio_enabled,
5223 .slot_reset = qla4xxx_pci_slot_reset,
5224 .resume = qla4xxx_pci_resume,
5227 static struct pci_device_id qla4xxx_pci_tbl[] = {
5229 .vendor = PCI_VENDOR_ID_QLOGIC,
5230 .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
5231 .subvendor = PCI_ANY_ID,
5232 .subdevice = PCI_ANY_ID,
5235 .vendor = PCI_VENDOR_ID_QLOGIC,
5236 .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
5237 .subvendor = PCI_ANY_ID,
5238 .subdevice = PCI_ANY_ID,
5241 .vendor = PCI_VENDOR_ID_QLOGIC,
5242 .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
5243 .subvendor = PCI_ANY_ID,
5244 .subdevice = PCI_ANY_ID,
5247 .vendor = PCI_VENDOR_ID_QLOGIC,
5248 .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
5249 .subvendor = PCI_ANY_ID,
5250 .subdevice = PCI_ANY_ID,
5254 MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
5256 static struct pci_driver qla4xxx_pci_driver = {
5257 .name = DRIVER_NAME,
5258 .id_table = qla4xxx_pci_tbl,
5259 .probe = qla4xxx_probe_adapter,
5260 .remove = qla4xxx_remove_adapter,
5261 .err_handler = &qla4xxx_err_handler,
5264 static int __init qla4xxx_module_init(void)
5268 /* Allocate cache for SRBs. */
5269 srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
5270 SLAB_HWCACHE_ALIGN, NULL);
5271 if (srb_cachep == NULL) {
5273 "%s: Unable to allocate SRB cache..."
5274 "Failing load!\n", DRIVER_NAME);
5279 /* Derive version string. */
5280 strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
5281 if (ql4xextended_error_logging)
5282 strcat(qla4xxx_version_str, "-debug");
5284 qla4xxx_scsi_transport =
5285 iscsi_register_transport(&qla4xxx_iscsi_transport);
5286 if (!qla4xxx_scsi_transport){
5288 goto release_srb_cache;
5291 ret = pci_register_driver(&qla4xxx_pci_driver);
5293 goto unregister_transport;
5295 printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
5298 unregister_transport:
5299 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
5301 kmem_cache_destroy(srb_cachep);
5306 static void __exit qla4xxx_module_exit(void)
5308 pci_unregister_driver(&qla4xxx_pci_driver);
5309 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
5310 kmem_cache_destroy(srb_cachep);
5313 module_init(qla4xxx_module_init);
5314 module_exit(qla4xxx_module_exit);
5316 MODULE_AUTHOR("QLogic Corporation");
5317 MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
5318 MODULE_LICENSE("GPL");
5319 MODULE_VERSION(QLA4XXX_DRIVER_VERSION);