blob: d423f7afbbd7710f19fa1cbc0020008c4d9c5238 [file] [log] [blame]
David Somayajuluafaf5a22006-09-19 10:28:00 -07001/*
2 * QLogic iSCSI HBA Driver
Vikas Chaudhary7d01d062010-12-02 22:12:51 -08003 * Copyright (c) 2003-2010 QLogic Corporation
David Somayajuluafaf5a22006-09-19 10:28:00 -07004 *
5 * See LICENSE.qla4xxx for copyright and licensing details.
6 */
7#include <linux/moduleparam.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
Manish Rangankar2a991c22011-07-25 13:48:55 -05009#include <linux/blkdev.h>
10#include <linux/iscsi_boot_sysfs.h>
Mike Christie13483732011-12-01 21:38:41 -060011#include <linux/inet.h>
David Somayajuluafaf5a22006-09-19 10:28:00 -070012
13#include <scsi/scsi_tcq.h>
14#include <scsi/scsicam.h>
15
16#include "ql4_def.h"
David C Somayajulubee4fe82007-05-23 18:03:32 -070017#include "ql4_version.h"
18#include "ql4_glbl.h"
19#include "ql4_dbg.h"
20#include "ql4_inline.h"
David Somayajuluafaf5a22006-09-19 10:28:00 -070021
22/*
23 * Driver version
24 */
Adrian Bunk47975472007-04-26 00:35:16 -070025static char qla4xxx_version_str[40];
David Somayajuluafaf5a22006-09-19 10:28:00 -070026
27/*
28 * SRB allocation cache
29 */
Christoph Lametere18b8902006-12-06 20:33:20 -080030static struct kmem_cache *srb_cachep;
David Somayajuluafaf5a22006-09-19 10:28:00 -070031
32/*
33 * Module parameter information and variables
34 */
Mike Christie13483732011-12-01 21:38:41 -060035int ql4xdisablesysfsboot = 1;
36module_param(ql4xdisablesysfsboot, int, S_IRUGO | S_IWUSR);
37MODULE_PARM_DESC(ql4xdisablesysfsboot,
Karen Higginsa4e8a712012-01-11 02:44:20 -080038 " Set to disable exporting boot targets to sysfs.\n"
39 "\t\t 0 - Export boot targets\n"
40 "\t\t 1 - Do not export boot targets (Default)");
Mike Christie13483732011-12-01 21:38:41 -060041
David Somayajuluafaf5a22006-09-19 10:28:00 -070042int ql4xdontresethba = 0;
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +053043module_param(ql4xdontresethba, int, S_IRUGO | S_IWUSR);
David Somayajuluafaf5a22006-09-19 10:28:00 -070044MODULE_PARM_DESC(ql4xdontresethba,
Karen Higginsa4e8a712012-01-11 02:44:20 -080045 " Don't reset the HBA for driver recovery.\n"
46 "\t\t 0 - It will reset HBA (Default)\n"
47 "\t\t 1 - It will NOT reset HBA");
David Somayajuluafaf5a22006-09-19 10:28:00 -070048
Karen Higginsa4e8a712012-01-11 02:44:20 -080049int ql4xextended_error_logging;
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +053050module_param(ql4xextended_error_logging, int, S_IRUGO | S_IWUSR);
Andrew Vasquez11010fe2006-10-06 09:54:59 -070051MODULE_PARM_DESC(ql4xextended_error_logging,
Karen Higginsa4e8a712012-01-11 02:44:20 -080052 " Option to enable extended error logging.\n"
53 "\t\t 0 - no logging (Default)\n"
54 "\t\t 2 - debug logging");
David Somayajuluafaf5a22006-09-19 10:28:00 -070055
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +053056int ql4xenablemsix = 1;
57module_param(ql4xenablemsix, int, S_IRUGO|S_IWUSR);
58MODULE_PARM_DESC(ql4xenablemsix,
Karen Higginsa4e8a712012-01-11 02:44:20 -080059 " Set to enable MSI or MSI-X interrupt mechanism.\n"
60 "\t\t 0 = enable INTx interrupt mechanism.\n"
61 "\t\t 1 = enable MSI-X interrupt mechanism (Default).\n"
62 "\t\t 2 = enable MSI interrupt mechanism.");
David C Somayajulu477ffb92007-01-22 12:26:11 -080063
Mike Christied510d962008-07-11 19:50:33 -050064#define QL4_DEF_QDEPTH 32
Vikas Chaudhary8bb40332011-03-21 03:34:31 -070065static int ql4xmaxqdepth = QL4_DEF_QDEPTH;
66module_param(ql4xmaxqdepth, int, S_IRUGO | S_IWUSR);
67MODULE_PARM_DESC(ql4xmaxqdepth,
Karen Higginsa4e8a712012-01-11 02:44:20 -080068 " Maximum queue depth to report for target devices.\n"
69 "\t\t Default: 32.");
Mike Christied510d962008-07-11 19:50:33 -050070
Vikas Chaudhary30387272011-03-21 03:34:32 -070071static int ql4xsess_recovery_tmo = QL4_SESS_RECOVERY_TMO;
72module_param(ql4xsess_recovery_tmo, int, S_IRUGO);
73MODULE_PARM_DESC(ql4xsess_recovery_tmo,
74 "Target Session Recovery Timeout.\n"
Karen Higginsa4e8a712012-01-11 02:44:20 -080075 "\t\t Default: 120 sec.");
Vikas Chaudhary30387272011-03-21 03:34:32 -070076
Manish Rangankarb3a271a2011-07-25 13:48:53 -050077static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -070078/*
79 * SCSI host template entry points
80 */
Adrian Bunk47975472007-04-26 00:35:16 -070081static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -070082
83/*
84 * iSCSI template entry points
85 */
David Somayajuluafaf5a22006-09-19 10:28:00 -070086static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
87 enum iscsi_param param, char *buf);
Mike Christieaa1e93a2007-05-30 12:57:09 -050088static int qla4xxx_host_get_param(struct Scsi_Host *shost,
89 enum iscsi_host_param param, char *buf);
Mike Christie00c31882011-10-06 03:56:59 -050090static int qla4xxx_iface_set_param(struct Scsi_Host *shost, void *data,
91 uint32_t len);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -050092static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
93 enum iscsi_param_type param_type,
94 int param, char *buf);
Mike Christie5c656af2009-07-15 15:02:59 -050095static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
Manish Rangankarb3a271a2011-07-25 13:48:53 -050096static struct iscsi_endpoint *qla4xxx_ep_connect(struct Scsi_Host *shost,
97 struct sockaddr *dst_addr,
98 int non_blocking);
99static int qla4xxx_ep_poll(struct iscsi_endpoint *ep, int timeout_ms);
100static void qla4xxx_ep_disconnect(struct iscsi_endpoint *ep);
101static int qla4xxx_get_ep_param(struct iscsi_endpoint *ep,
102 enum iscsi_param param, char *buf);
103static int qla4xxx_conn_start(struct iscsi_cls_conn *conn);
104static struct iscsi_cls_conn *
105qla4xxx_conn_create(struct iscsi_cls_session *cls_sess, uint32_t conn_idx);
106static int qla4xxx_conn_bind(struct iscsi_cls_session *cls_session,
107 struct iscsi_cls_conn *cls_conn,
108 uint64_t transport_fd, int is_leading);
109static void qla4xxx_conn_destroy(struct iscsi_cls_conn *conn);
110static struct iscsi_cls_session *
111qla4xxx_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max,
112 uint16_t qdepth, uint32_t initial_cmdsn);
113static void qla4xxx_session_destroy(struct iscsi_cls_session *sess);
114static void qla4xxx_task_work(struct work_struct *wdata);
115static int qla4xxx_alloc_pdu(struct iscsi_task *, uint8_t);
116static int qla4xxx_task_xmit(struct iscsi_task *);
117static void qla4xxx_task_cleanup(struct iscsi_task *);
118static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session);
119static void qla4xxx_conn_get_stats(struct iscsi_cls_conn *cls_conn,
120 struct iscsi_stats *stats);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700121/*
122 * SCSI host template entry points
123 */
Jeff Garzikf2812332010-11-16 02:10:29 -0500124static int qla4xxx_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmd);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530125static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700126static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
Mike Christiece545032008-02-29 18:25:20 -0600127static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700128static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
129static int qla4xxx_slave_alloc(struct scsi_device *device);
130static int qla4xxx_slave_configure(struct scsi_device *device);
131static void qla4xxx_slave_destroy(struct scsi_device *sdev);
Al Viro587a1f12011-07-23 23:11:19 -0400132static umode_t ql4_attr_is_visible(int param_type, int param);
Vikas Chaudhary95d31262011-08-12 02:51:29 -0700133static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700134
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530135static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
136 QLA82XX_LEGACY_INTR_CONFIG;
137
David Somayajuluafaf5a22006-09-19 10:28:00 -0700138static struct scsi_host_template qla4xxx_driver_template = {
139 .module = THIS_MODULE,
140 .name = DRIVER_NAME,
141 .proc_name = DRIVER_NAME,
142 .queuecommand = qla4xxx_queuecommand,
143
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530144 .eh_abort_handler = qla4xxx_eh_abort,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700145 .eh_device_reset_handler = qla4xxx_eh_device_reset,
Mike Christiece545032008-02-29 18:25:20 -0600146 .eh_target_reset_handler = qla4xxx_eh_target_reset,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700147 .eh_host_reset_handler = qla4xxx_eh_host_reset,
Mike Christie5c656af2009-07-15 15:02:59 -0500148 .eh_timed_out = qla4xxx_eh_cmd_timed_out,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700149
150 .slave_configure = qla4xxx_slave_configure,
151 .slave_alloc = qla4xxx_slave_alloc,
152 .slave_destroy = qla4xxx_slave_destroy,
153
154 .this_id = -1,
155 .cmd_per_lun = 3,
156 .use_clustering = ENABLE_CLUSTERING,
157 .sg_tablesize = SG_ALL,
158
159 .max_sectors = 0xFFFF,
Harish Zunjarrao7ad633c2011-05-17 23:17:11 -0700160 .shost_attrs = qla4xxx_host_attrs,
Vikas Chaudhary95d31262011-08-12 02:51:29 -0700161 .host_reset = qla4xxx_host_reset,
Vikas Chaudharya3559432011-07-25 13:48:51 -0500162 .vendor_id = SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_QLOGIC,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700163};
164
165static struct iscsi_transport qla4xxx_iscsi_transport = {
166 .owner = THIS_MODULE,
167 .name = DRIVER_NAME,
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500168 .caps = CAP_TEXT_NEGO |
169 CAP_DATA_PATH_OFFLOAD | CAP_HDRDGST |
170 CAP_DATADGST | CAP_LOGIN_OFFLOAD |
171 CAP_MULTI_R2T,
Mike Christie3128c6c2011-07-25 13:48:42 -0500172 .attr_is_visible = ql4_attr_is_visible,
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500173 .create_session = qla4xxx_session_create,
174 .destroy_session = qla4xxx_session_destroy,
175 .start_conn = qla4xxx_conn_start,
176 .create_conn = qla4xxx_conn_create,
177 .bind_conn = qla4xxx_conn_bind,
178 .stop_conn = iscsi_conn_stop,
179 .destroy_conn = qla4xxx_conn_destroy,
180 .set_param = iscsi_set_param,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700181 .get_conn_param = qla4xxx_conn_get_param,
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500182 .get_session_param = iscsi_session_get_param,
183 .get_ep_param = qla4xxx_get_ep_param,
184 .ep_connect = qla4xxx_ep_connect,
185 .ep_poll = qla4xxx_ep_poll,
186 .ep_disconnect = qla4xxx_ep_disconnect,
187 .get_stats = qla4xxx_conn_get_stats,
188 .send_pdu = iscsi_conn_send_pdu,
189 .xmit_task = qla4xxx_task_xmit,
190 .cleanup_task = qla4xxx_task_cleanup,
191 .alloc_pdu = qla4xxx_alloc_pdu,
192
Mike Christieaa1e93a2007-05-30 12:57:09 -0500193 .get_host_param = qla4xxx_host_get_param,
Mike Christied00efe32011-07-25 13:48:38 -0500194 .set_iface_param = qla4xxx_iface_set_param,
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500195 .get_iface_param = qla4xxx_get_iface_param,
Vikas Chaudharya3559432011-07-25 13:48:51 -0500196 .bsg_request = qla4xxx_bsg_request,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700197};
198
199static struct scsi_transport_template *qla4xxx_scsi_transport;
200
Al Viro587a1f12011-07-23 23:11:19 -0400201static umode_t ql4_attr_is_visible(int param_type, int param)
Mike Christie3128c6c2011-07-25 13:48:42 -0500202{
203 switch (param_type) {
Mike Christief27fb2e2011-07-25 13:48:45 -0500204 case ISCSI_HOST_PARAM:
205 switch (param) {
206 case ISCSI_HOST_PARAM_HWADDRESS:
207 case ISCSI_HOST_PARAM_IPADDRESS:
208 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Vikas Chaudhary3254dbe2012-01-19 03:06:56 -0800209 case ISCSI_HOST_PARAM_PORT_STATE:
210 case ISCSI_HOST_PARAM_PORT_SPEED:
Mike Christief27fb2e2011-07-25 13:48:45 -0500211 return S_IRUGO;
212 default:
213 return 0;
214 }
Mike Christie3128c6c2011-07-25 13:48:42 -0500215 case ISCSI_PARAM:
216 switch (param) {
Mike Christie590134f2011-10-17 22:42:13 -0500217 case ISCSI_PARAM_PERSISTENT_ADDRESS:
218 case ISCSI_PARAM_PERSISTENT_PORT:
Mike Christie3128c6c2011-07-25 13:48:42 -0500219 case ISCSI_PARAM_CONN_ADDRESS:
220 case ISCSI_PARAM_CONN_PORT:
Mike Christie1d063c12011-07-25 13:48:43 -0500221 case ISCSI_PARAM_TARGET_NAME:
222 case ISCSI_PARAM_TPGT:
223 case ISCSI_PARAM_TARGET_ALIAS:
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500224 case ISCSI_PARAM_MAX_BURST:
225 case ISCSI_PARAM_MAX_R2T:
226 case ISCSI_PARAM_FIRST_BURST:
227 case ISCSI_PARAM_MAX_RECV_DLENGTH:
228 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
Mike Christiede379202011-08-14 20:42:56 -0500229 case ISCSI_PARAM_IFACE_NAME:
Mike Christie3128c6c2011-07-25 13:48:42 -0500230 return S_IRUGO;
231 default:
232 return 0;
233 }
Mike Christieb78dbba2011-07-25 13:48:44 -0500234 case ISCSI_NET_PARAM:
235 switch (param) {
236 case ISCSI_NET_PARAM_IPV4_ADDR:
237 case ISCSI_NET_PARAM_IPV4_SUBNET:
238 case ISCSI_NET_PARAM_IPV4_GW:
239 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
240 case ISCSI_NET_PARAM_IFACE_ENABLE:
241 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
242 case ISCSI_NET_PARAM_IPV6_ADDR:
243 case ISCSI_NET_PARAM_IPV6_ROUTER:
244 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
245 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
Vikas Chaudhary6ac73e82011-07-25 13:48:49 -0500246 case ISCSI_NET_PARAM_VLAN_ID:
247 case ISCSI_NET_PARAM_VLAN_PRIORITY:
248 case ISCSI_NET_PARAM_VLAN_ENABLED:
Vikas Chaudhary943c1572011-08-01 03:26:13 -0700249 case ISCSI_NET_PARAM_MTU:
Vikas Chaudhary2ada7fc2011-08-01 03:26:19 -0700250 case ISCSI_NET_PARAM_PORT:
Mike Christieb78dbba2011-07-25 13:48:44 -0500251 return S_IRUGO;
252 default:
253 return 0;
254 }
Mike Christie3128c6c2011-07-25 13:48:42 -0500255 }
256
257 return 0;
258}
259
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500260static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
261 enum iscsi_param_type param_type,
262 int param, char *buf)
263{
264 struct Scsi_Host *shost = iscsi_iface_to_shost(iface);
265 struct scsi_qla_host *ha = to_qla_host(shost);
266 int len = -ENOSYS;
267
268 if (param_type != ISCSI_NET_PARAM)
269 return -ENOSYS;
270
271 switch (param) {
272 case ISCSI_NET_PARAM_IPV4_ADDR:
273 len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
274 break;
275 case ISCSI_NET_PARAM_IPV4_SUBNET:
276 len = sprintf(buf, "%pI4\n", &ha->ip_config.subnet_mask);
277 break;
278 case ISCSI_NET_PARAM_IPV4_GW:
279 len = sprintf(buf, "%pI4\n", &ha->ip_config.gateway);
280 break;
281 case ISCSI_NET_PARAM_IFACE_ENABLE:
282 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
283 len = sprintf(buf, "%s\n",
284 (ha->ip_config.ipv4_options &
285 IPOPT_IPV4_PROTOCOL_ENABLE) ?
286 "enabled" : "disabled");
287 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
288 len = sprintf(buf, "%s\n",
289 (ha->ip_config.ipv6_options &
290 IPV6_OPT_IPV6_PROTOCOL_ENABLE) ?
291 "enabled" : "disabled");
292 break;
293 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
294 len = sprintf(buf, "%s\n",
295 (ha->ip_config.tcp_options & TCPOPT_DHCP_ENABLE) ?
296 "dhcp" : "static");
297 break;
298 case ISCSI_NET_PARAM_IPV6_ADDR:
299 if (iface->iface_num == 0)
300 len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr0);
301 if (iface->iface_num == 1)
302 len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr1);
303 break;
304 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
305 len = sprintf(buf, "%pI6\n",
306 &ha->ip_config.ipv6_link_local_addr);
307 break;
308 case ISCSI_NET_PARAM_IPV6_ROUTER:
309 len = sprintf(buf, "%pI6\n",
310 &ha->ip_config.ipv6_default_router_addr);
311 break;
312 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
313 len = sprintf(buf, "%s\n",
314 (ha->ip_config.ipv6_addl_options &
315 IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) ?
316 "nd" : "static");
317 break;
318 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
319 len = sprintf(buf, "%s\n",
320 (ha->ip_config.ipv6_addl_options &
321 IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR) ?
322 "auto" : "static");
323 break;
Vikas Chaudhary6ac73e82011-07-25 13:48:49 -0500324 case ISCSI_NET_PARAM_VLAN_ID:
325 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
326 len = sprintf(buf, "%d\n",
327 (ha->ip_config.ipv4_vlan_tag &
328 ISCSI_MAX_VLAN_ID));
329 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
330 len = sprintf(buf, "%d\n",
331 (ha->ip_config.ipv6_vlan_tag &
332 ISCSI_MAX_VLAN_ID));
333 break;
334 case ISCSI_NET_PARAM_VLAN_PRIORITY:
335 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
336 len = sprintf(buf, "%d\n",
337 ((ha->ip_config.ipv4_vlan_tag >> 13) &
338 ISCSI_MAX_VLAN_PRIORITY));
339 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
340 len = sprintf(buf, "%d\n",
341 ((ha->ip_config.ipv6_vlan_tag >> 13) &
342 ISCSI_MAX_VLAN_PRIORITY));
343 break;
344 case ISCSI_NET_PARAM_VLAN_ENABLED:
345 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
346 len = sprintf(buf, "%s\n",
347 (ha->ip_config.ipv4_options &
348 IPOPT_VLAN_TAGGING_ENABLE) ?
349 "enabled" : "disabled");
350 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
351 len = sprintf(buf, "%s\n",
352 (ha->ip_config.ipv6_options &
353 IPV6_OPT_VLAN_TAGGING_ENABLE) ?
354 "enabled" : "disabled");
355 break;
Vikas Chaudhary943c1572011-08-01 03:26:13 -0700356 case ISCSI_NET_PARAM_MTU:
357 len = sprintf(buf, "%d\n", ha->ip_config.eth_mtu_size);
358 break;
Vikas Chaudhary2ada7fc2011-08-01 03:26:19 -0700359 case ISCSI_NET_PARAM_PORT:
360 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
361 len = sprintf(buf, "%d\n", ha->ip_config.ipv4_port);
362 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
363 len = sprintf(buf, "%d\n", ha->ip_config.ipv6_port);
364 break;
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500365 default:
366 len = -ENOSYS;
367 }
368
369 return len;
370}
371
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500372static struct iscsi_endpoint *
373qla4xxx_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
374 int non_blocking)
375{
376 int ret;
377 struct iscsi_endpoint *ep;
378 struct qla_endpoint *qla_ep;
379 struct scsi_qla_host *ha;
380 struct sockaddr_in *addr;
381 struct sockaddr_in6 *addr6;
382
383 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
384 if (!shost) {
385 ret = -ENXIO;
386 printk(KERN_ERR "%s: shost is NULL\n",
387 __func__);
388 return ERR_PTR(ret);
389 }
390
391 ha = iscsi_host_priv(shost);
392
393 ep = iscsi_create_endpoint(sizeof(struct qla_endpoint));
394 if (!ep) {
395 ret = -ENOMEM;
396 return ERR_PTR(ret);
397 }
398
399 qla_ep = ep->dd_data;
400 memset(qla_ep, 0, sizeof(struct qla_endpoint));
401 if (dst_addr->sa_family == AF_INET) {
402 memcpy(&qla_ep->dst_addr, dst_addr, sizeof(struct sockaddr_in));
403 addr = (struct sockaddr_in *)&qla_ep->dst_addr;
404 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI4\n", __func__,
405 (char *)&addr->sin_addr));
406 } else if (dst_addr->sa_family == AF_INET6) {
407 memcpy(&qla_ep->dst_addr, dst_addr,
408 sizeof(struct sockaddr_in6));
409 addr6 = (struct sockaddr_in6 *)&qla_ep->dst_addr;
410 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI6\n", __func__,
411 (char *)&addr6->sin6_addr));
412 }
413
414 qla_ep->host = shost;
415
416 return ep;
417}
418
419static int qla4xxx_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
420{
421 struct qla_endpoint *qla_ep;
422 struct scsi_qla_host *ha;
423 int ret = 0;
424
425 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
426 qla_ep = ep->dd_data;
427 ha = to_qla_host(qla_ep->host);
428
Mike Christie13483732011-12-01 21:38:41 -0600429 if (adapter_up(ha) && !test_bit(AF_BUILD_DDB_LIST, &ha->flags))
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500430 ret = 1;
431
432 return ret;
433}
434
435static void qla4xxx_ep_disconnect(struct iscsi_endpoint *ep)
436{
437 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
438 iscsi_destroy_endpoint(ep);
439}
440
441static int qla4xxx_get_ep_param(struct iscsi_endpoint *ep,
442 enum iscsi_param param,
443 char *buf)
444{
445 struct qla_endpoint *qla_ep = ep->dd_data;
446 struct sockaddr *dst_addr;
447
448 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
449
450 switch (param) {
451 case ISCSI_PARAM_CONN_PORT:
452 case ISCSI_PARAM_CONN_ADDRESS:
453 if (!qla_ep)
454 return -ENOTCONN;
455
456 dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
457 if (!dst_addr)
458 return -ENOTCONN;
459
460 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
461 &qla_ep->dst_addr, param, buf);
462 default:
463 return -ENOSYS;
464 }
465}
466
467static void qla4xxx_conn_get_stats(struct iscsi_cls_conn *cls_conn,
468 struct iscsi_stats *stats)
469{
470 struct iscsi_session *sess;
471 struct iscsi_cls_session *cls_sess;
472 struct ddb_entry *ddb_entry;
473 struct scsi_qla_host *ha;
474 struct ql_iscsi_stats *ql_iscsi_stats;
475 int stats_size;
476 int ret;
477 dma_addr_t iscsi_stats_dma;
478
479 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
480
481 cls_sess = iscsi_conn_to_session(cls_conn);
482 sess = cls_sess->dd_data;
483 ddb_entry = sess->dd_data;
484 ha = ddb_entry->ha;
485
486 stats_size = PAGE_ALIGN(sizeof(struct ql_iscsi_stats));
487 /* Allocate memory */
488 ql_iscsi_stats = dma_alloc_coherent(&ha->pdev->dev, stats_size,
489 &iscsi_stats_dma, GFP_KERNEL);
490 if (!ql_iscsi_stats) {
491 ql4_printk(KERN_ERR, ha,
492 "Unable to allocate memory for iscsi stats\n");
493 goto exit_get_stats;
494 }
495
496 ret = qla4xxx_get_mgmt_data(ha, ddb_entry->fw_ddb_index, stats_size,
497 iscsi_stats_dma);
498 if (ret != QLA_SUCCESS) {
499 ql4_printk(KERN_ERR, ha,
500 "Unable to retreive iscsi stats\n");
501 goto free_stats;
502 }
503
504 /* octets */
505 stats->txdata_octets = le64_to_cpu(ql_iscsi_stats->tx_data_octets);
506 stats->rxdata_octets = le64_to_cpu(ql_iscsi_stats->rx_data_octets);
507 /* xmit pdus */
508 stats->noptx_pdus = le32_to_cpu(ql_iscsi_stats->tx_nopout_pdus);
509 stats->scsicmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_cmd_pdus);
510 stats->tmfcmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_tmf_cmd_pdus);
511 stats->login_pdus = le32_to_cpu(ql_iscsi_stats->tx_login_cmd_pdus);
512 stats->text_pdus = le32_to_cpu(ql_iscsi_stats->tx_text_cmd_pdus);
513 stats->dataout_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_write_pdus);
514 stats->logout_pdus = le32_to_cpu(ql_iscsi_stats->tx_logout_cmd_pdus);
515 stats->snack_pdus = le32_to_cpu(ql_iscsi_stats->tx_snack_req_pdus);
516 /* recv pdus */
517 stats->noprx_pdus = le32_to_cpu(ql_iscsi_stats->rx_nopin_pdus);
518 stats->scsirsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_resp_pdus);
519 stats->tmfrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_tmf_resp_pdus);
520 stats->textrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_text_resp_pdus);
521 stats->datain_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_read_pdus);
522 stats->logoutrsp_pdus =
523 le32_to_cpu(ql_iscsi_stats->rx_logout_resp_pdus);
524 stats->r2t_pdus = le32_to_cpu(ql_iscsi_stats->rx_r2t_pdus);
525 stats->async_pdus = le32_to_cpu(ql_iscsi_stats->rx_async_pdus);
526 stats->rjt_pdus = le32_to_cpu(ql_iscsi_stats->rx_reject_pdus);
527
528free_stats:
529 dma_free_coherent(&ha->pdev->dev, stats_size, ql_iscsi_stats,
530 iscsi_stats_dma);
531exit_get_stats:
532 return;
533}
534
Mike Christie5c656af2009-07-15 15:02:59 -0500535static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
536{
537 struct iscsi_cls_session *session;
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500538 struct iscsi_session *sess;
539 unsigned long flags;
540 enum blk_eh_timer_return ret = BLK_EH_NOT_HANDLED;
Mike Christie5c656af2009-07-15 15:02:59 -0500541
542 session = starget_to_session(scsi_target(sc->device));
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500543 sess = session->dd_data;
Mike Christie5c656af2009-07-15 15:02:59 -0500544
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500545 spin_lock_irqsave(&session->lock, flags);
546 if (session->state == ISCSI_SESSION_FAILED)
547 ret = BLK_EH_RESET_TIMER;
548 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie5c656af2009-07-15 15:02:59 -0500549
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500550 return ret;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700551}
552
Vikas Chaudhary3254dbe2012-01-19 03:06:56 -0800553static void qla4xxx_set_port_speed(struct Scsi_Host *shost)
554{
555 struct scsi_qla_host *ha = to_qla_host(shost);
556 struct iscsi_cls_host *ihost = shost_priv(shost);
557 uint32_t speed = ISCSI_PORT_SPEED_UNKNOWN;
558
559 qla4xxx_get_firmware_state(ha);
560
561 switch (ha->addl_fw_state & 0x0F00) {
562 case FW_ADDSTATE_LINK_SPEED_10MBPS:
563 speed = ISCSI_PORT_SPEED_10MBPS;
564 break;
565 case FW_ADDSTATE_LINK_SPEED_100MBPS:
566 speed = ISCSI_PORT_SPEED_100MBPS;
567 break;
568 case FW_ADDSTATE_LINK_SPEED_1GBPS:
569 speed = ISCSI_PORT_SPEED_1GBPS;
570 break;
571 case FW_ADDSTATE_LINK_SPEED_10GBPS:
572 speed = ISCSI_PORT_SPEED_10GBPS;
573 break;
574 }
575 ihost->port_speed = speed;
576}
577
578static void qla4xxx_set_port_state(struct Scsi_Host *shost)
579{
580 struct scsi_qla_host *ha = to_qla_host(shost);
581 struct iscsi_cls_host *ihost = shost_priv(shost);
582 uint32_t state = ISCSI_PORT_STATE_DOWN;
583
584 if (test_bit(AF_LINK_UP, &ha->flags))
585 state = ISCSI_PORT_STATE_UP;
586
587 ihost->port_state = state;
588}
589
Mike Christieaa1e93a2007-05-30 12:57:09 -0500590static int qla4xxx_host_get_param(struct Scsi_Host *shost,
591 enum iscsi_host_param param, char *buf)
592{
593 struct scsi_qla_host *ha = to_qla_host(shost);
594 int len;
595
596 switch (param) {
597 case ISCSI_HOST_PARAM_HWADDRESS:
Michael Chan7ffc49a2007-12-24 21:28:09 -0800598 len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
Mike Christieaa1e93a2007-05-30 12:57:09 -0500599 break;
Mike Christie22236962007-05-30 12:57:24 -0500600 case ISCSI_HOST_PARAM_IPADDRESS:
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500601 len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
Mike Christie22236962007-05-30 12:57:24 -0500602 break;
Mike Christie8ad57812007-05-30 12:57:13 -0500603 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie22236962007-05-30 12:57:24 -0500604 len = sprintf(buf, "%s\n", ha->name_string);
Mike Christie8ad57812007-05-30 12:57:13 -0500605 break;
Vikas Chaudhary3254dbe2012-01-19 03:06:56 -0800606 case ISCSI_HOST_PARAM_PORT_STATE:
607 qla4xxx_set_port_state(shost);
608 len = sprintf(buf, "%s\n", iscsi_get_port_state_name(shost));
609 break;
610 case ISCSI_HOST_PARAM_PORT_SPEED:
611 qla4xxx_set_port_speed(shost);
612 len = sprintf(buf, "%s\n", iscsi_get_port_speed_name(shost));
613 break;
Mike Christieaa1e93a2007-05-30 12:57:09 -0500614 default:
615 return -ENOSYS;
616 }
617
618 return len;
619}
620
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500621static void qla4xxx_create_ipv4_iface(struct scsi_qla_host *ha)
622{
623 if (ha->iface_ipv4)
624 return;
625
626 /* IPv4 */
627 ha->iface_ipv4 = iscsi_create_iface(ha->host,
628 &qla4xxx_iscsi_transport,
629 ISCSI_IFACE_TYPE_IPV4, 0, 0);
630 if (!ha->iface_ipv4)
631 ql4_printk(KERN_ERR, ha, "Could not create IPv4 iSCSI "
632 "iface0.\n");
633}
634
635static void qla4xxx_create_ipv6_iface(struct scsi_qla_host *ha)
636{
637 if (!ha->iface_ipv6_0)
638 /* IPv6 iface-0 */
639 ha->iface_ipv6_0 = iscsi_create_iface(ha->host,
640 &qla4xxx_iscsi_transport,
641 ISCSI_IFACE_TYPE_IPV6, 0,
642 0);
643 if (!ha->iface_ipv6_0)
644 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
645 "iface0.\n");
646
647 if (!ha->iface_ipv6_1)
648 /* IPv6 iface-1 */
649 ha->iface_ipv6_1 = iscsi_create_iface(ha->host,
650 &qla4xxx_iscsi_transport,
651 ISCSI_IFACE_TYPE_IPV6, 1,
652 0);
653 if (!ha->iface_ipv6_1)
654 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
655 "iface1.\n");
656}
657
658static void qla4xxx_create_ifaces(struct scsi_qla_host *ha)
659{
660 if (ha->ip_config.ipv4_options & IPOPT_IPV4_PROTOCOL_ENABLE)
661 qla4xxx_create_ipv4_iface(ha);
662
663 if (ha->ip_config.ipv6_options & IPV6_OPT_IPV6_PROTOCOL_ENABLE)
664 qla4xxx_create_ipv6_iface(ha);
665}
666
667static void qla4xxx_destroy_ipv4_iface(struct scsi_qla_host *ha)
668{
669 if (ha->iface_ipv4) {
670 iscsi_destroy_iface(ha->iface_ipv4);
671 ha->iface_ipv4 = NULL;
672 }
673}
674
675static void qla4xxx_destroy_ipv6_iface(struct scsi_qla_host *ha)
676{
677 if (ha->iface_ipv6_0) {
678 iscsi_destroy_iface(ha->iface_ipv6_0);
679 ha->iface_ipv6_0 = NULL;
680 }
681 if (ha->iface_ipv6_1) {
682 iscsi_destroy_iface(ha->iface_ipv6_1);
683 ha->iface_ipv6_1 = NULL;
684 }
685}
686
687static void qla4xxx_destroy_ifaces(struct scsi_qla_host *ha)
688{
689 qla4xxx_destroy_ipv4_iface(ha);
690 qla4xxx_destroy_ipv6_iface(ha);
691}
692
Mike Christied00efe32011-07-25 13:48:38 -0500693static void qla4xxx_set_ipv6(struct scsi_qla_host *ha,
694 struct iscsi_iface_param_info *iface_param,
695 struct addr_ctrl_blk *init_fw_cb)
696{
697 /*
698 * iface_num 0 is valid for IPv6 Addr, linklocal, router, autocfg.
699 * iface_num 1 is valid only for IPv6 Addr.
700 */
701 switch (iface_param->param) {
702 case ISCSI_NET_PARAM_IPV6_ADDR:
703 if (iface_param->iface_num & 0x1)
704 /* IPv6 Addr 1 */
705 memcpy(init_fw_cb->ipv6_addr1, iface_param->value,
706 sizeof(init_fw_cb->ipv6_addr1));
707 else
708 /* IPv6 Addr 0 */
709 memcpy(init_fw_cb->ipv6_addr0, iface_param->value,
710 sizeof(init_fw_cb->ipv6_addr0));
711 break;
712 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
713 if (iface_param->iface_num & 0x1)
714 break;
715 memcpy(init_fw_cb->ipv6_if_id, &iface_param->value[8],
716 sizeof(init_fw_cb->ipv6_if_id));
717 break;
718 case ISCSI_NET_PARAM_IPV6_ROUTER:
719 if (iface_param->iface_num & 0x1)
720 break;
721 memcpy(init_fw_cb->ipv6_dflt_rtr_addr, iface_param->value,
722 sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
723 break;
724 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
725 /* Autocfg applies to even interface */
726 if (iface_param->iface_num & 0x1)
727 break;
728
729 if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_DISABLE)
730 init_fw_cb->ipv6_addtl_opts &=
731 cpu_to_le16(
732 ~IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
733 else if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_ND_ENABLE)
734 init_fw_cb->ipv6_addtl_opts |=
735 cpu_to_le16(
736 IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
737 else
738 ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
739 "IPv6 addr\n");
740 break;
741 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
742 /* Autocfg applies to even interface */
743 if (iface_param->iface_num & 0x1)
744 break;
745
746 if (iface_param->value[0] ==
747 ISCSI_IPV6_LINKLOCAL_AUTOCFG_ENABLE)
748 init_fw_cb->ipv6_addtl_opts |= cpu_to_le16(
749 IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
750 else if (iface_param->value[0] ==
751 ISCSI_IPV6_LINKLOCAL_AUTOCFG_DISABLE)
752 init_fw_cb->ipv6_addtl_opts &= cpu_to_le16(
753 ~IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
754 else
755 ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
756 "IPv6 linklocal addr\n");
757 break;
758 case ISCSI_NET_PARAM_IPV6_ROUTER_AUTOCFG:
759 /* Autocfg applies to even interface */
760 if (iface_param->iface_num & 0x1)
761 break;
762
763 if (iface_param->value[0] == ISCSI_IPV6_ROUTER_AUTOCFG_ENABLE)
764 memset(init_fw_cb->ipv6_dflt_rtr_addr, 0,
765 sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
766 break;
767 case ISCSI_NET_PARAM_IFACE_ENABLE:
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500768 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
Mike Christied00efe32011-07-25 13:48:38 -0500769 init_fw_cb->ipv6_opts |=
770 cpu_to_le16(IPV6_OPT_IPV6_PROTOCOL_ENABLE);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500771 qla4xxx_create_ipv6_iface(ha);
772 } else {
Mike Christied00efe32011-07-25 13:48:38 -0500773 init_fw_cb->ipv6_opts &=
774 cpu_to_le16(~IPV6_OPT_IPV6_PROTOCOL_ENABLE &
775 0xFFFF);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500776 qla4xxx_destroy_ipv6_iface(ha);
777 }
Mike Christied00efe32011-07-25 13:48:38 -0500778 break;
Mike Christie2d636732011-10-11 17:55:11 -0500779 case ISCSI_NET_PARAM_VLAN_TAG:
Mike Christied00efe32011-07-25 13:48:38 -0500780 if (iface_param->len != sizeof(init_fw_cb->ipv6_vlan_tag))
781 break;
Vikas Chaudhary6ac73e82011-07-25 13:48:49 -0500782 init_fw_cb->ipv6_vlan_tag =
783 cpu_to_be16(*(uint16_t *)iface_param->value);
784 break;
785 case ISCSI_NET_PARAM_VLAN_ENABLED:
786 if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
787 init_fw_cb->ipv6_opts |=
788 cpu_to_le16(IPV6_OPT_VLAN_TAGGING_ENABLE);
789 else
790 init_fw_cb->ipv6_opts &=
791 cpu_to_le16(~IPV6_OPT_VLAN_TAGGING_ENABLE);
Mike Christied00efe32011-07-25 13:48:38 -0500792 break;
Vikas Chaudhary943c1572011-08-01 03:26:13 -0700793 case ISCSI_NET_PARAM_MTU:
794 init_fw_cb->eth_mtu_size =
795 cpu_to_le16(*(uint16_t *)iface_param->value);
796 break;
Vikas Chaudhary2ada7fc2011-08-01 03:26:19 -0700797 case ISCSI_NET_PARAM_PORT:
798 /* Autocfg applies to even interface */
799 if (iface_param->iface_num & 0x1)
800 break;
801
802 init_fw_cb->ipv6_port =
803 cpu_to_le16(*(uint16_t *)iface_param->value);
804 break;
Mike Christied00efe32011-07-25 13:48:38 -0500805 default:
806 ql4_printk(KERN_ERR, ha, "Unknown IPv6 param = %d\n",
807 iface_param->param);
808 break;
809 }
810}
811
812static void qla4xxx_set_ipv4(struct scsi_qla_host *ha,
813 struct iscsi_iface_param_info *iface_param,
814 struct addr_ctrl_blk *init_fw_cb)
815{
816 switch (iface_param->param) {
817 case ISCSI_NET_PARAM_IPV4_ADDR:
818 memcpy(init_fw_cb->ipv4_addr, iface_param->value,
819 sizeof(init_fw_cb->ipv4_addr));
820 break;
821 case ISCSI_NET_PARAM_IPV4_SUBNET:
822 memcpy(init_fw_cb->ipv4_subnet, iface_param->value,
823 sizeof(init_fw_cb->ipv4_subnet));
824 break;
825 case ISCSI_NET_PARAM_IPV4_GW:
826 memcpy(init_fw_cb->ipv4_gw_addr, iface_param->value,
827 sizeof(init_fw_cb->ipv4_gw_addr));
828 break;
829 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
830 if (iface_param->value[0] == ISCSI_BOOTPROTO_DHCP)
831 init_fw_cb->ipv4_tcp_opts |=
832 cpu_to_le16(TCPOPT_DHCP_ENABLE);
833 else if (iface_param->value[0] == ISCSI_BOOTPROTO_STATIC)
834 init_fw_cb->ipv4_tcp_opts &=
835 cpu_to_le16(~TCPOPT_DHCP_ENABLE);
836 else
837 ql4_printk(KERN_ERR, ha, "Invalid IPv4 bootproto\n");
838 break;
839 case ISCSI_NET_PARAM_IFACE_ENABLE:
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500840 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
Mike Christied00efe32011-07-25 13:48:38 -0500841 init_fw_cb->ipv4_ip_opts |=
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500842 cpu_to_le16(IPOPT_IPV4_PROTOCOL_ENABLE);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500843 qla4xxx_create_ipv4_iface(ha);
844 } else {
Mike Christied00efe32011-07-25 13:48:38 -0500845 init_fw_cb->ipv4_ip_opts &=
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500846 cpu_to_le16(~IPOPT_IPV4_PROTOCOL_ENABLE &
Mike Christied00efe32011-07-25 13:48:38 -0500847 0xFFFF);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500848 qla4xxx_destroy_ipv4_iface(ha);
849 }
Mike Christied00efe32011-07-25 13:48:38 -0500850 break;
Mike Christie2d636732011-10-11 17:55:11 -0500851 case ISCSI_NET_PARAM_VLAN_TAG:
Mike Christied00efe32011-07-25 13:48:38 -0500852 if (iface_param->len != sizeof(init_fw_cb->ipv4_vlan_tag))
853 break;
Vikas Chaudhary6ac73e82011-07-25 13:48:49 -0500854 init_fw_cb->ipv4_vlan_tag =
855 cpu_to_be16(*(uint16_t *)iface_param->value);
856 break;
857 case ISCSI_NET_PARAM_VLAN_ENABLED:
858 if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
859 init_fw_cb->ipv4_ip_opts |=
860 cpu_to_le16(IPOPT_VLAN_TAGGING_ENABLE);
861 else
862 init_fw_cb->ipv4_ip_opts &=
863 cpu_to_le16(~IPOPT_VLAN_TAGGING_ENABLE);
Mike Christied00efe32011-07-25 13:48:38 -0500864 break;
Vikas Chaudhary943c1572011-08-01 03:26:13 -0700865 case ISCSI_NET_PARAM_MTU:
866 init_fw_cb->eth_mtu_size =
867 cpu_to_le16(*(uint16_t *)iface_param->value);
868 break;
Vikas Chaudhary2ada7fc2011-08-01 03:26:19 -0700869 case ISCSI_NET_PARAM_PORT:
870 init_fw_cb->ipv4_port =
871 cpu_to_le16(*(uint16_t *)iface_param->value);
872 break;
Mike Christied00efe32011-07-25 13:48:38 -0500873 default:
874 ql4_printk(KERN_ERR, ha, "Unknown IPv4 param = %d\n",
875 iface_param->param);
876 break;
877 }
878}
879
880static void
881qla4xxx_initcb_to_acb(struct addr_ctrl_blk *init_fw_cb)
882{
883 struct addr_ctrl_blk_def *acb;
884 acb = (struct addr_ctrl_blk_def *)init_fw_cb;
885 memset(acb->reserved1, 0, sizeof(acb->reserved1));
886 memset(acb->reserved2, 0, sizeof(acb->reserved2));
887 memset(acb->reserved3, 0, sizeof(acb->reserved3));
888 memset(acb->reserved4, 0, sizeof(acb->reserved4));
889 memset(acb->reserved5, 0, sizeof(acb->reserved5));
890 memset(acb->reserved6, 0, sizeof(acb->reserved6));
891 memset(acb->reserved7, 0, sizeof(acb->reserved7));
892 memset(acb->reserved8, 0, sizeof(acb->reserved8));
893 memset(acb->reserved9, 0, sizeof(acb->reserved9));
894 memset(acb->reserved10, 0, sizeof(acb->reserved10));
895 memset(acb->reserved11, 0, sizeof(acb->reserved11));
896 memset(acb->reserved12, 0, sizeof(acb->reserved12));
897 memset(acb->reserved13, 0, sizeof(acb->reserved13));
898 memset(acb->reserved14, 0, sizeof(acb->reserved14));
899 memset(acb->reserved15, 0, sizeof(acb->reserved15));
900}
901
902static int
Mike Christie00c31882011-10-06 03:56:59 -0500903qla4xxx_iface_set_param(struct Scsi_Host *shost, void *data, uint32_t len)
Mike Christied00efe32011-07-25 13:48:38 -0500904{
905 struct scsi_qla_host *ha = to_qla_host(shost);
906 int rval = 0;
907 struct iscsi_iface_param_info *iface_param = NULL;
908 struct addr_ctrl_blk *init_fw_cb = NULL;
909 dma_addr_t init_fw_cb_dma;
910 uint32_t mbox_cmd[MBOX_REG_COUNT];
911 uint32_t mbox_sts[MBOX_REG_COUNT];
Mike Christie00c31882011-10-06 03:56:59 -0500912 uint32_t rem = len;
913 struct nlattr *attr;
Mike Christied00efe32011-07-25 13:48:38 -0500914
915 init_fw_cb = dma_alloc_coherent(&ha->pdev->dev,
916 sizeof(struct addr_ctrl_blk),
917 &init_fw_cb_dma, GFP_KERNEL);
918 if (!init_fw_cb) {
919 ql4_printk(KERN_ERR, ha, "%s: Unable to alloc init_cb\n",
920 __func__);
921 return -ENOMEM;
922 }
923
924 memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
925 memset(&mbox_cmd, 0, sizeof(mbox_cmd));
926 memset(&mbox_sts, 0, sizeof(mbox_sts));
927
928 if (qla4xxx_get_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma)) {
929 ql4_printk(KERN_ERR, ha, "%s: get ifcb failed\n", __func__);
930 rval = -EIO;
931 goto exit_init_fw_cb;
932 }
933
Mike Christie00c31882011-10-06 03:56:59 -0500934 nla_for_each_attr(attr, data, len, rem) {
935 iface_param = nla_data(attr);
Mike Christied00efe32011-07-25 13:48:38 -0500936
937 if (iface_param->param_type != ISCSI_NET_PARAM)
938 continue;
939
940 switch (iface_param->iface_type) {
941 case ISCSI_IFACE_TYPE_IPV4:
942 switch (iface_param->iface_num) {
943 case 0:
944 qla4xxx_set_ipv4(ha, iface_param, init_fw_cb);
945 break;
946 default:
947 /* Cannot have more than one IPv4 interface */
948 ql4_printk(KERN_ERR, ha, "Invalid IPv4 iface "
949 "number = %d\n",
950 iface_param->iface_num);
951 break;
952 }
953 break;
954 case ISCSI_IFACE_TYPE_IPV6:
955 switch (iface_param->iface_num) {
956 case 0:
957 case 1:
958 qla4xxx_set_ipv6(ha, iface_param, init_fw_cb);
959 break;
960 default:
961 /* Cannot have more than two IPv6 interface */
962 ql4_printk(KERN_ERR, ha, "Invalid IPv6 iface "
963 "number = %d\n",
964 iface_param->iface_num);
965 break;
966 }
967 break;
968 default:
969 ql4_printk(KERN_ERR, ha, "Invalid iface type\n");
970 break;
971 }
Mike Christied00efe32011-07-25 13:48:38 -0500972 }
973
974 init_fw_cb->cookie = cpu_to_le32(0x11BEAD5A);
975
976 rval = qla4xxx_set_flash(ha, init_fw_cb_dma, FLASH_SEGMENT_IFCB,
977 sizeof(struct addr_ctrl_blk),
978 FLASH_OPT_RMW_COMMIT);
979 if (rval != QLA_SUCCESS) {
980 ql4_printk(KERN_ERR, ha, "%s: set flash mbx failed\n",
981 __func__);
982 rval = -EIO;
983 goto exit_init_fw_cb;
984 }
985
Vikas Chaudharyce505f92011-12-01 22:42:10 -0800986 rval = qla4xxx_disable_acb(ha);
987 if (rval != QLA_SUCCESS) {
988 ql4_printk(KERN_ERR, ha, "%s: disable acb mbx failed\n",
989 __func__);
990 rval = -EIO;
991 goto exit_init_fw_cb;
992 }
993
994 wait_for_completion_timeout(&ha->disable_acb_comp,
995 DISABLE_ACB_TOV * HZ);
Mike Christied00efe32011-07-25 13:48:38 -0500996
997 qla4xxx_initcb_to_acb(init_fw_cb);
998
999 rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma);
1000 if (rval != QLA_SUCCESS) {
1001 ql4_printk(KERN_ERR, ha, "%s: set acb mbx failed\n",
1002 __func__);
1003 rval = -EIO;
1004 goto exit_init_fw_cb;
1005 }
1006
1007 memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
1008 qla4xxx_update_local_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb,
1009 init_fw_cb_dma);
1010
1011exit_init_fw_cb:
1012 dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk),
1013 init_fw_cb, init_fw_cb_dma);
1014
1015 return rval;
1016}
1017
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001018static int qla4xxx_conn_get_param(struct iscsi_cls_conn *cls_conn,
David Somayajuluafaf5a22006-09-19 10:28:00 -07001019 enum iscsi_param param, char *buf)
1020{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001021 struct iscsi_conn *conn;
1022 struct qla_conn *qla_conn;
1023 struct sockaddr *dst_addr;
1024 int len = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001025
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001026 conn = cls_conn->dd_data;
1027 qla_conn = conn->dd_data;
1028 dst_addr = &qla_conn->qla_ep->dst_addr;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001029
1030 switch (param) {
1031 case ISCSI_PARAM_CONN_PORT:
David Somayajuluafaf5a22006-09-19 10:28:00 -07001032 case ISCSI_PARAM_CONN_ADDRESS:
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001033 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
1034 dst_addr, param, buf);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001035 default:
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001036 return iscsi_conn_get_param(cls_conn, param, buf);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001037 }
1038
1039 return len;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001040
David Somayajuluafaf5a22006-09-19 10:28:00 -07001041}
1042
Mike Christie13483732011-12-01 21:38:41 -06001043int qla4xxx_get_ddb_index(struct scsi_qla_host *ha, uint16_t *ddb_index)
1044{
1045 uint32_t mbx_sts = 0;
1046 uint16_t tmp_ddb_index;
1047 int ret;
1048
1049get_ddb_index:
1050 tmp_ddb_index = find_first_zero_bit(ha->ddb_idx_map, MAX_DDB_ENTRIES);
1051
1052 if (tmp_ddb_index >= MAX_DDB_ENTRIES) {
1053 DEBUG2(ql4_printk(KERN_INFO, ha,
1054 "Free DDB index not available\n"));
1055 ret = QLA_ERROR;
1056 goto exit_get_ddb_index;
1057 }
1058
1059 if (test_and_set_bit(tmp_ddb_index, ha->ddb_idx_map))
1060 goto get_ddb_index;
1061
1062 DEBUG2(ql4_printk(KERN_INFO, ha,
1063 "Found a free DDB index at %d\n", tmp_ddb_index));
1064 ret = qla4xxx_req_ddb_entry(ha, tmp_ddb_index, &mbx_sts);
1065 if (ret == QLA_ERROR) {
1066 if (mbx_sts == MBOX_STS_COMMAND_ERROR) {
1067 ql4_printk(KERN_INFO, ha,
1068 "DDB index = %d not available trying next\n",
1069 tmp_ddb_index);
1070 goto get_ddb_index;
1071 }
1072 DEBUG2(ql4_printk(KERN_INFO, ha,
1073 "Free FW DDB not available\n"));
1074 }
1075
1076 *ddb_index = tmp_ddb_index;
1077
1078exit_get_ddb_index:
1079 return ret;
1080}
1081
1082static int qla4xxx_match_ipaddress(struct scsi_qla_host *ha,
1083 struct ddb_entry *ddb_entry,
1084 char *existing_ipaddr,
1085 char *user_ipaddr)
1086{
1087 uint8_t dst_ipaddr[IPv6_ADDR_LEN];
1088 char formatted_ipaddr[DDB_IPADDR_LEN];
1089 int status = QLA_SUCCESS, ret = 0;
1090
1091 if (ddb_entry->fw_ddb_entry.options & DDB_OPT_IPV6_DEVICE) {
1092 ret = in6_pton(user_ipaddr, strlen(user_ipaddr), dst_ipaddr,
1093 '\0', NULL);
1094 if (ret == 0) {
1095 status = QLA_ERROR;
1096 goto out_match;
1097 }
1098 ret = sprintf(formatted_ipaddr, "%pI6", dst_ipaddr);
1099 } else {
1100 ret = in4_pton(user_ipaddr, strlen(user_ipaddr), dst_ipaddr,
1101 '\0', NULL);
1102 if (ret == 0) {
1103 status = QLA_ERROR;
1104 goto out_match;
1105 }
1106 ret = sprintf(formatted_ipaddr, "%pI4", dst_ipaddr);
1107 }
1108
1109 if (strcmp(existing_ipaddr, formatted_ipaddr))
1110 status = QLA_ERROR;
1111
1112out_match:
1113 return status;
1114}
1115
1116static int qla4xxx_match_fwdb_session(struct scsi_qla_host *ha,
1117 struct iscsi_cls_conn *cls_conn)
1118{
1119 int idx = 0, max_ddbs, rval;
1120 struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
1121 struct iscsi_session *sess, *existing_sess;
1122 struct iscsi_conn *conn, *existing_conn;
1123 struct ddb_entry *ddb_entry;
1124
1125 sess = cls_sess->dd_data;
1126 conn = cls_conn->dd_data;
1127
1128 if (sess->targetname == NULL ||
1129 conn->persistent_address == NULL ||
1130 conn->persistent_port == 0)
1131 return QLA_ERROR;
1132
1133 max_ddbs = is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
1134 MAX_DEV_DB_ENTRIES;
1135
1136 for (idx = 0; idx < max_ddbs; idx++) {
1137 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
1138 if (ddb_entry == NULL)
1139 continue;
1140
1141 if (ddb_entry->ddb_type != FLASH_DDB)
1142 continue;
1143
1144 existing_sess = ddb_entry->sess->dd_data;
1145 existing_conn = ddb_entry->conn->dd_data;
1146
1147 if (existing_sess->targetname == NULL ||
1148 existing_conn->persistent_address == NULL ||
1149 existing_conn->persistent_port == 0)
1150 continue;
1151
1152 DEBUG2(ql4_printk(KERN_INFO, ha,
1153 "IQN = %s User IQN = %s\n",
1154 existing_sess->targetname,
1155 sess->targetname));
1156
1157 DEBUG2(ql4_printk(KERN_INFO, ha,
1158 "IP = %s User IP = %s\n",
1159 existing_conn->persistent_address,
1160 conn->persistent_address));
1161
1162 DEBUG2(ql4_printk(KERN_INFO, ha,
1163 "Port = %d User Port = %d\n",
1164 existing_conn->persistent_port,
1165 conn->persistent_port));
1166
1167 if (strcmp(existing_sess->targetname, sess->targetname))
1168 continue;
1169 rval = qla4xxx_match_ipaddress(ha, ddb_entry,
1170 existing_conn->persistent_address,
1171 conn->persistent_address);
1172 if (rval == QLA_ERROR)
1173 continue;
1174 if (existing_conn->persistent_port != conn->persistent_port)
1175 continue;
1176 break;
1177 }
1178
1179 if (idx == max_ddbs)
1180 return QLA_ERROR;
1181
1182 DEBUG2(ql4_printk(KERN_INFO, ha,
1183 "Match found in fwdb sessions\n"));
1184 return QLA_SUCCESS;
1185}
1186
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001187static struct iscsi_cls_session *
1188qla4xxx_session_create(struct iscsi_endpoint *ep,
1189 uint16_t cmds_max, uint16_t qdepth,
1190 uint32_t initial_cmdsn)
1191{
1192 struct iscsi_cls_session *cls_sess;
1193 struct scsi_qla_host *ha;
1194 struct qla_endpoint *qla_ep;
1195 struct ddb_entry *ddb_entry;
Mike Christie13483732011-12-01 21:38:41 -06001196 uint16_t ddb_index;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001197 struct iscsi_session *sess;
1198 struct sockaddr *dst_addr;
1199 int ret;
1200
1201 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1202 if (!ep) {
1203 printk(KERN_ERR "qla4xxx: missing ep.\n");
1204 return NULL;
1205 }
1206
1207 qla_ep = ep->dd_data;
1208 dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
1209 ha = to_qla_host(qla_ep->host);
Manish Rangankar736cf362011-10-07 16:55:46 -07001210
Mike Christie13483732011-12-01 21:38:41 -06001211 ret = qla4xxx_get_ddb_index(ha, &ddb_index);
1212 if (ret == QLA_ERROR)
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001213 return NULL;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001214
1215 cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport, qla_ep->host,
1216 cmds_max, sizeof(struct ddb_entry),
1217 sizeof(struct ql4_task_data),
1218 initial_cmdsn, ddb_index);
1219 if (!cls_sess)
1220 return NULL;
1221
1222 sess = cls_sess->dd_data;
1223 ddb_entry = sess->dd_data;
1224 ddb_entry->fw_ddb_index = ddb_index;
1225 ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
1226 ddb_entry->ha = ha;
1227 ddb_entry->sess = cls_sess;
Mike Christie13483732011-12-01 21:38:41 -06001228 ddb_entry->unblock_sess = qla4xxx_unblock_ddb;
1229 ddb_entry->ddb_change = qla4xxx_ddb_change;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001230 cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
1231 ha->fw_ddb_index_map[ddb_entry->fw_ddb_index] = ddb_entry;
1232 ha->tot_ddbs++;
1233
1234 return cls_sess;
1235}
1236
1237static void qla4xxx_session_destroy(struct iscsi_cls_session *cls_sess)
1238{
1239 struct iscsi_session *sess;
1240 struct ddb_entry *ddb_entry;
1241 struct scsi_qla_host *ha;
1242 unsigned long flags;
1243
1244 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1245 sess = cls_sess->dd_data;
1246 ddb_entry = sess->dd_data;
1247 ha = ddb_entry->ha;
1248
Manish Rangankar736cf362011-10-07 16:55:46 -07001249 qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
1250
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001251 spin_lock_irqsave(&ha->hardware_lock, flags);
1252 qla4xxx_free_ddb(ha, ddb_entry);
1253 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1254 iscsi_session_teardown(cls_sess);
1255}
1256
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001257static struct iscsi_cls_conn *
1258qla4xxx_conn_create(struct iscsi_cls_session *cls_sess, uint32_t conn_idx)
1259{
1260 struct iscsi_cls_conn *cls_conn;
1261 struct iscsi_session *sess;
1262 struct ddb_entry *ddb_entry;
1263
1264 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1265 cls_conn = iscsi_conn_setup(cls_sess, sizeof(struct qla_conn),
1266 conn_idx);
Mike Christieff1d0312011-12-01 21:38:43 -06001267 if (!cls_conn)
1268 return NULL;
1269
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001270 sess = cls_sess->dd_data;
1271 ddb_entry = sess->dd_data;
1272 ddb_entry->conn = cls_conn;
1273
1274 return cls_conn;
1275}
1276
1277static int qla4xxx_conn_bind(struct iscsi_cls_session *cls_session,
1278 struct iscsi_cls_conn *cls_conn,
1279 uint64_t transport_fd, int is_leading)
1280{
1281 struct iscsi_conn *conn;
1282 struct qla_conn *qla_conn;
1283 struct iscsi_endpoint *ep;
1284
1285 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1286
1287 if (iscsi_conn_bind(cls_session, cls_conn, is_leading))
1288 return -EINVAL;
1289 ep = iscsi_lookup_endpoint(transport_fd);
1290 conn = cls_conn->dd_data;
1291 qla_conn = conn->dd_data;
1292 qla_conn->qla_ep = ep->dd_data;
1293 return 0;
1294}
1295
1296static int qla4xxx_conn_start(struct iscsi_cls_conn *cls_conn)
1297{
1298 struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
1299 struct iscsi_session *sess;
1300 struct ddb_entry *ddb_entry;
1301 struct scsi_qla_host *ha;
Mike Christie13483732011-12-01 21:38:41 -06001302 struct dev_db_entry *fw_ddb_entry = NULL;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001303 dma_addr_t fw_ddb_entry_dma;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001304 uint32_t mbx_sts = 0;
1305 int ret = 0;
1306 int status = QLA_SUCCESS;
1307
1308 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1309 sess = cls_sess->dd_data;
1310 ddb_entry = sess->dd_data;
1311 ha = ddb_entry->ha;
1312
Mike Christie13483732011-12-01 21:38:41 -06001313 /* Check if we have matching FW DDB, if yes then do not
1314 * login to this target. This could cause target to logout previous
1315 * connection
1316 */
1317 ret = qla4xxx_match_fwdb_session(ha, cls_conn);
1318 if (ret == QLA_SUCCESS) {
1319 ql4_printk(KERN_INFO, ha,
1320 "Session already exist in FW.\n");
1321 ret = -EEXIST;
1322 goto exit_conn_start;
1323 }
1324
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001325 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1326 &fw_ddb_entry_dma, GFP_KERNEL);
1327 if (!fw_ddb_entry) {
1328 ql4_printk(KERN_ERR, ha,
1329 "%s: Unable to allocate dma buffer\n", __func__);
Mike Christie13483732011-12-01 21:38:41 -06001330 ret = -ENOMEM;
1331 goto exit_conn_start;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001332 }
1333
1334 ret = qla4xxx_set_param_ddbentry(ha, ddb_entry, cls_conn, &mbx_sts);
1335 if (ret) {
1336 /* If iscsid is stopped and started then no need to do
1337 * set param again since ddb state will be already
1338 * active and FW does not allow set ddb to an
1339 * active session.
1340 */
1341 if (mbx_sts)
1342 if (ddb_entry->fw_ddb_device_state ==
Manish Rangankarf922da72011-10-07 16:55:49 -07001343 DDB_DS_SESSION_ACTIVE) {
Mike Christie13483732011-12-01 21:38:41 -06001344 ddb_entry->unblock_sess(ddb_entry->sess);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001345 goto exit_set_param;
Manish Rangankarf922da72011-10-07 16:55:49 -07001346 }
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001347
1348 ql4_printk(KERN_ERR, ha, "%s: Failed set param for index[%d]\n",
1349 __func__, ddb_entry->fw_ddb_index);
1350 goto exit_conn_start;
1351 }
1352
1353 status = qla4xxx_conn_open(ha, ddb_entry->fw_ddb_index);
1354 if (status == QLA_ERROR) {
Manish Rangankar0e7e8502011-07-25 13:48:54 -05001355 ql4_printk(KERN_ERR, ha, "%s: Login failed: %s\n", __func__,
1356 sess->targetname);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001357 ret = -EINVAL;
1358 goto exit_conn_start;
1359 }
1360
Manish Rangankar98270ab2011-10-07 16:55:47 -07001361 if (ddb_entry->fw_ddb_device_state == DDB_DS_NO_CONNECTION_ACTIVE)
1362 ddb_entry->fw_ddb_device_state = DDB_DS_LOGIN_IN_PROCESS;
1363
1364 DEBUG2(printk(KERN_INFO "%s: DDB state [%d]\n", __func__,
1365 ddb_entry->fw_ddb_device_state));
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001366
1367exit_set_param:
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001368 ret = 0;
1369
1370exit_conn_start:
Mike Christie13483732011-12-01 21:38:41 -06001371 if (fw_ddb_entry)
1372 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1373 fw_ddb_entry, fw_ddb_entry_dma);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001374 return ret;
1375}
1376
1377static void qla4xxx_conn_destroy(struct iscsi_cls_conn *cls_conn)
1378{
1379 struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
1380 struct iscsi_session *sess;
1381 struct scsi_qla_host *ha;
1382 struct ddb_entry *ddb_entry;
1383 int options;
1384
1385 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1386 sess = cls_sess->dd_data;
1387 ddb_entry = sess->dd_data;
1388 ha = ddb_entry->ha;
1389
1390 options = LOGOUT_OPTION_CLOSE_SESSION;
1391 if (qla4xxx_session_logout_ddb(ha, ddb_entry, options) == QLA_ERROR)
1392 ql4_printk(KERN_ERR, ha, "%s: Logout failed\n", __func__);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001393}
1394
1395static void qla4xxx_task_work(struct work_struct *wdata)
1396{
1397 struct ql4_task_data *task_data;
1398 struct scsi_qla_host *ha;
1399 struct passthru_status *sts;
1400 struct iscsi_task *task;
1401 struct iscsi_hdr *hdr;
1402 uint8_t *data;
1403 uint32_t data_len;
1404 struct iscsi_conn *conn;
1405 int hdr_len;
1406 itt_t itt;
1407
1408 task_data = container_of(wdata, struct ql4_task_data, task_work);
1409 ha = task_data->ha;
1410 task = task_data->task;
1411 sts = &task_data->sts;
1412 hdr_len = sizeof(struct iscsi_hdr);
1413
1414 DEBUG3(printk(KERN_INFO "Status returned\n"));
1415 DEBUG3(qla4xxx_dump_buffer(sts, 64));
1416 DEBUG3(printk(KERN_INFO "Response buffer"));
1417 DEBUG3(qla4xxx_dump_buffer(task_data->resp_buffer, 64));
1418
1419 conn = task->conn;
1420
1421 switch (sts->completionStatus) {
1422 case PASSTHRU_STATUS_COMPLETE:
1423 hdr = (struct iscsi_hdr *)task_data->resp_buffer;
1424 /* Assign back the itt in hdr, until we use the PREASSIGN_TAG */
1425 itt = sts->handle;
1426 hdr->itt = itt;
1427 data = task_data->resp_buffer + hdr_len;
1428 data_len = task_data->resp_len - hdr_len;
1429 iscsi_complete_pdu(conn, hdr, data, data_len);
1430 break;
1431 default:
1432 ql4_printk(KERN_ERR, ha, "Passthru failed status = 0x%x\n",
1433 sts->completionStatus);
1434 break;
1435 }
1436 return;
1437}
1438
1439static int qla4xxx_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
1440{
1441 struct ql4_task_data *task_data;
1442 struct iscsi_session *sess;
1443 struct ddb_entry *ddb_entry;
1444 struct scsi_qla_host *ha;
1445 int hdr_len;
1446
1447 sess = task->conn->session;
1448 ddb_entry = sess->dd_data;
1449 ha = ddb_entry->ha;
1450 task_data = task->dd_data;
1451 memset(task_data, 0, sizeof(struct ql4_task_data));
1452
1453 if (task->sc) {
1454 ql4_printk(KERN_INFO, ha,
1455 "%s: SCSI Commands not implemented\n", __func__);
1456 return -EINVAL;
1457 }
1458
1459 hdr_len = sizeof(struct iscsi_hdr);
1460 task_data->ha = ha;
1461 task_data->task = task;
1462
1463 if (task->data_count) {
1464 task_data->data_dma = dma_map_single(&ha->pdev->dev, task->data,
1465 task->data_count,
1466 PCI_DMA_TODEVICE);
1467 }
1468
1469 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
1470 __func__, task->conn->max_recv_dlength, hdr_len));
1471
Manish Rangankar69ca2162011-10-07 16:55:50 -07001472 task_data->resp_len = task->conn->max_recv_dlength + hdr_len;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001473 task_data->resp_buffer = dma_alloc_coherent(&ha->pdev->dev,
1474 task_data->resp_len,
1475 &task_data->resp_dma,
1476 GFP_ATOMIC);
1477 if (!task_data->resp_buffer)
1478 goto exit_alloc_pdu;
1479
Manish Rangankar69ca2162011-10-07 16:55:50 -07001480 task_data->req_len = task->data_count + hdr_len;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001481 task_data->req_buffer = dma_alloc_coherent(&ha->pdev->dev,
Manish Rangankar69ca2162011-10-07 16:55:50 -07001482 task_data->req_len,
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001483 &task_data->req_dma,
1484 GFP_ATOMIC);
1485 if (!task_data->req_buffer)
1486 goto exit_alloc_pdu;
1487
1488 task->hdr = task_data->req_buffer;
1489
1490 INIT_WORK(&task_data->task_work, qla4xxx_task_work);
1491
1492 return 0;
1493
1494exit_alloc_pdu:
1495 if (task_data->resp_buffer)
1496 dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
1497 task_data->resp_buffer, task_data->resp_dma);
1498
1499 if (task_data->req_buffer)
Manish Rangankar69ca2162011-10-07 16:55:50 -07001500 dma_free_coherent(&ha->pdev->dev, task_data->req_len,
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001501 task_data->req_buffer, task_data->req_dma);
1502 return -ENOMEM;
1503}
1504
1505static void qla4xxx_task_cleanup(struct iscsi_task *task)
1506{
1507 struct ql4_task_data *task_data;
1508 struct iscsi_session *sess;
1509 struct ddb_entry *ddb_entry;
1510 struct scsi_qla_host *ha;
1511 int hdr_len;
1512
1513 hdr_len = sizeof(struct iscsi_hdr);
1514 sess = task->conn->session;
1515 ddb_entry = sess->dd_data;
1516 ha = ddb_entry->ha;
1517 task_data = task->dd_data;
1518
1519 if (task->data_count) {
1520 dma_unmap_single(&ha->pdev->dev, task_data->data_dma,
1521 task->data_count, PCI_DMA_TODEVICE);
1522 }
1523
1524 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
1525 __func__, task->conn->max_recv_dlength, hdr_len));
1526
1527 dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
1528 task_data->resp_buffer, task_data->resp_dma);
Manish Rangankar69ca2162011-10-07 16:55:50 -07001529 dma_free_coherent(&ha->pdev->dev, task_data->req_len,
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001530 task_data->req_buffer, task_data->req_dma);
1531 return;
1532}
1533
1534static int qla4xxx_task_xmit(struct iscsi_task *task)
1535{
1536 struct scsi_cmnd *sc = task->sc;
1537 struct iscsi_session *sess = task->conn->session;
1538 struct ddb_entry *ddb_entry = sess->dd_data;
1539 struct scsi_qla_host *ha = ddb_entry->ha;
1540
1541 if (!sc)
1542 return qla4xxx_send_passthru0(task);
1543
1544 ql4_printk(KERN_INFO, ha, "%s: scsi cmd xmit not implemented\n",
1545 __func__);
1546 return -ENOSYS;
1547}
1548
Mike Christie13483732011-12-01 21:38:41 -06001549static void qla4xxx_copy_fwddb_param(struct scsi_qla_host *ha,
1550 struct dev_db_entry *fw_ddb_entry,
1551 struct iscsi_cls_session *cls_sess,
1552 struct iscsi_cls_conn *cls_conn)
1553{
1554 int buflen = 0;
1555 struct iscsi_session *sess;
1556 struct iscsi_conn *conn;
1557 char ip_addr[DDB_IPADDR_LEN];
1558 uint16_t options = 0;
1559
1560 sess = cls_sess->dd_data;
1561 conn = cls_conn->dd_data;
1562
1563 conn->max_recv_dlength = BYTE_UNITS *
1564 le16_to_cpu(fw_ddb_entry->iscsi_max_rcv_data_seg_len);
1565
1566 conn->max_xmit_dlength = BYTE_UNITS *
1567 le16_to_cpu(fw_ddb_entry->iscsi_max_snd_data_seg_len);
1568
1569 sess->initial_r2t_en =
1570 (BIT_10 & le16_to_cpu(fw_ddb_entry->iscsi_options));
1571
1572 sess->max_r2t = le16_to_cpu(fw_ddb_entry->iscsi_max_outsnd_r2t);
1573
1574 sess->imm_data_en = (BIT_11 & le16_to_cpu(fw_ddb_entry->iscsi_options));
1575
1576 sess->first_burst = BYTE_UNITS *
1577 le16_to_cpu(fw_ddb_entry->iscsi_first_burst_len);
1578
1579 sess->max_burst = BYTE_UNITS *
1580 le16_to_cpu(fw_ddb_entry->iscsi_max_burst_len);
1581
1582 sess->time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
1583
1584 sess->time2retain = le16_to_cpu(fw_ddb_entry->iscsi_def_time2retain);
1585
1586 conn->persistent_port = le16_to_cpu(fw_ddb_entry->port);
1587
1588 sess->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
1589
1590 options = le16_to_cpu(fw_ddb_entry->options);
1591 if (options & DDB_OPT_IPV6_DEVICE)
1592 sprintf(ip_addr, "%pI6", fw_ddb_entry->ip_addr);
1593 else
1594 sprintf(ip_addr, "%pI4", fw_ddb_entry->ip_addr);
1595
1596 iscsi_set_param(cls_conn, ISCSI_PARAM_TARGET_NAME,
1597 (char *)fw_ddb_entry->iscsi_name, buflen);
1598 iscsi_set_param(cls_conn, ISCSI_PARAM_INITIATOR_NAME,
1599 (char *)ha->name_string, buflen);
1600 iscsi_set_param(cls_conn, ISCSI_PARAM_PERSISTENT_ADDRESS,
1601 (char *)ip_addr, buflen);
Vikas Chaudhary6c1b8782012-01-19 03:06:54 -08001602 iscsi_set_param(cls_conn, ISCSI_PARAM_TARGET_ALIAS,
1603 (char *)fw_ddb_entry->iscsi_alias, buflen);
Mike Christie13483732011-12-01 21:38:41 -06001604}
1605
1606void qla4xxx_update_session_conn_fwddb_param(struct scsi_qla_host *ha,
1607 struct ddb_entry *ddb_entry)
1608{
1609 struct iscsi_cls_session *cls_sess;
1610 struct iscsi_cls_conn *cls_conn;
1611 uint32_t ddb_state;
1612 dma_addr_t fw_ddb_entry_dma;
1613 struct dev_db_entry *fw_ddb_entry;
1614
1615 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1616 &fw_ddb_entry_dma, GFP_KERNEL);
1617 if (!fw_ddb_entry) {
1618 ql4_printk(KERN_ERR, ha,
1619 "%s: Unable to allocate dma buffer\n", __func__);
1620 goto exit_session_conn_fwddb_param;
1621 }
1622
1623 if (qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index, fw_ddb_entry,
1624 fw_ddb_entry_dma, NULL, NULL, &ddb_state,
1625 NULL, NULL, NULL) == QLA_ERROR) {
1626 DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: failed "
1627 "get_ddb_entry for fw_ddb_index %d\n",
1628 ha->host_no, __func__,
1629 ddb_entry->fw_ddb_index));
1630 goto exit_session_conn_fwddb_param;
1631 }
1632
1633 cls_sess = ddb_entry->sess;
1634
1635 cls_conn = ddb_entry->conn;
1636
1637 /* Update params */
1638 qla4xxx_copy_fwddb_param(ha, fw_ddb_entry, cls_sess, cls_conn);
1639
1640exit_session_conn_fwddb_param:
1641 if (fw_ddb_entry)
1642 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1643 fw_ddb_entry, fw_ddb_entry_dma);
1644}
1645
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001646void qla4xxx_update_session_conn_param(struct scsi_qla_host *ha,
1647 struct ddb_entry *ddb_entry)
1648{
1649 struct iscsi_cls_session *cls_sess;
1650 struct iscsi_cls_conn *cls_conn;
1651 struct iscsi_session *sess;
1652 struct iscsi_conn *conn;
1653 uint32_t ddb_state;
1654 dma_addr_t fw_ddb_entry_dma;
1655 struct dev_db_entry *fw_ddb_entry;
1656
1657 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1658 &fw_ddb_entry_dma, GFP_KERNEL);
1659 if (!fw_ddb_entry) {
1660 ql4_printk(KERN_ERR, ha,
1661 "%s: Unable to allocate dma buffer\n", __func__);
Mike Christie13483732011-12-01 21:38:41 -06001662 goto exit_session_conn_param;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001663 }
1664
1665 if (qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index, fw_ddb_entry,
1666 fw_ddb_entry_dma, NULL, NULL, &ddb_state,
1667 NULL, NULL, NULL) == QLA_ERROR) {
1668 DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: failed "
1669 "get_ddb_entry for fw_ddb_index %d\n",
1670 ha->host_no, __func__,
1671 ddb_entry->fw_ddb_index));
Mike Christie13483732011-12-01 21:38:41 -06001672 goto exit_session_conn_param;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001673 }
1674
1675 cls_sess = ddb_entry->sess;
1676 sess = cls_sess->dd_data;
1677
1678 cls_conn = ddb_entry->conn;
1679 conn = cls_conn->dd_data;
1680
Mike Christie13483732011-12-01 21:38:41 -06001681 /* Update timers after login */
1682 ddb_entry->default_relogin_timeout =
Nilesh Javalic28eaac2011-12-18 21:40:44 -08001683 (le16_to_cpu(fw_ddb_entry->def_timeout) > LOGIN_TOV) &&
1684 (le16_to_cpu(fw_ddb_entry->def_timeout) < LOGIN_TOV * 10) ?
1685 le16_to_cpu(fw_ddb_entry->def_timeout) : LOGIN_TOV;
Mike Christie13483732011-12-01 21:38:41 -06001686 ddb_entry->default_time2wait =
1687 le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
1688
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001689 /* Update params */
1690 conn->max_recv_dlength = BYTE_UNITS *
1691 le16_to_cpu(fw_ddb_entry->iscsi_max_rcv_data_seg_len);
1692
1693 conn->max_xmit_dlength = BYTE_UNITS *
1694 le16_to_cpu(fw_ddb_entry->iscsi_max_snd_data_seg_len);
1695
1696 sess->initial_r2t_en =
1697 (BIT_10 & le16_to_cpu(fw_ddb_entry->iscsi_options));
1698
1699 sess->max_r2t = le16_to_cpu(fw_ddb_entry->iscsi_max_outsnd_r2t);
1700
1701 sess->imm_data_en = (BIT_11 & le16_to_cpu(fw_ddb_entry->iscsi_options));
1702
1703 sess->first_burst = BYTE_UNITS *
1704 le16_to_cpu(fw_ddb_entry->iscsi_first_burst_len);
1705
1706 sess->max_burst = BYTE_UNITS *
1707 le16_to_cpu(fw_ddb_entry->iscsi_max_burst_len);
1708
1709 sess->time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
1710
1711 sess->time2retain = le16_to_cpu(fw_ddb_entry->iscsi_def_time2retain);
1712
1713 sess->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
1714
1715 memcpy(sess->initiatorname, ha->name_string,
1716 min(sizeof(ha->name_string), sizeof(sess->initiatorname)));
Mike Christie13483732011-12-01 21:38:41 -06001717
Vikas Chaudhary6c1b8782012-01-19 03:06:54 -08001718 iscsi_set_param(cls_conn, ISCSI_PARAM_TARGET_ALIAS,
1719 (char *)fw_ddb_entry->iscsi_alias, 0);
1720
Mike Christie13483732011-12-01 21:38:41 -06001721exit_session_conn_param:
1722 if (fw_ddb_entry)
1723 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1724 fw_ddb_entry, fw_ddb_entry_dma);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001725}
1726
David Somayajuluafaf5a22006-09-19 10:28:00 -07001727/*
1728 * Timer routines
1729 */
1730
1731static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
1732 unsigned long interval)
1733{
1734 DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
1735 __func__, ha->host->host_no));
1736 init_timer(&ha->timer);
1737 ha->timer.expires = jiffies + interval * HZ;
1738 ha->timer.data = (unsigned long)ha;
1739 ha->timer.function = (void (*)(unsigned long))func;
1740 add_timer(&ha->timer);
1741 ha->timer_active = 1;
1742}
1743
1744static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
1745{
1746 del_timer_sync(&ha->timer);
1747 ha->timer_active = 0;
1748}
1749
1750/***
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001751 * qla4xxx_mark_device_missing - blocks the session
1752 * @cls_session: Pointer to the session to be blocked
David Somayajuluafaf5a22006-09-19 10:28:00 -07001753 * @ddb_entry: Pointer to device database entry
1754 *
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301755 * This routine marks a device missing and close connection.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001756 **/
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001757void qla4xxx_mark_device_missing(struct iscsi_cls_session *cls_session)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001758{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001759 iscsi_block_session(cls_session);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001760}
1761
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301762/**
1763 * qla4xxx_mark_all_devices_missing - mark all devices as missing.
1764 * @ha: Pointer to host adapter structure.
1765 *
1766 * This routine marks a device missing and resets the relogin retry count.
1767 **/
1768void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
1769{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001770 iscsi_host_for_each_session(ha->host, qla4xxx_mark_device_missing);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301771}
1772
David Somayajuluafaf5a22006-09-19 10:28:00 -07001773static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
1774 struct ddb_entry *ddb_entry,
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001775 struct scsi_cmnd *cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001776{
1777 struct srb *srb;
1778
1779 srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
1780 if (!srb)
1781 return srb;
1782
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301783 kref_init(&srb->srb_ref);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001784 srb->ha = ha;
1785 srb->ddb = ddb_entry;
1786 srb->cmd = cmd;
1787 srb->flags = 0;
Vikas Chaudhary53698872010-04-28 11:41:59 +05301788 CMD_SP(cmd) = (void *)srb;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001789
1790 return srb;
1791}
1792
1793static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
1794{
1795 struct scsi_cmnd *cmd = srb->cmd;
1796
1797 if (srb->flags & SRB_DMA_VALID) {
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +09001798 scsi_dma_unmap(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001799 srb->flags &= ~SRB_DMA_VALID;
1800 }
Vikas Chaudhary53698872010-04-28 11:41:59 +05301801 CMD_SP(cmd) = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001802}
1803
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301804void qla4xxx_srb_compl(struct kref *ref)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001805{
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301806 struct srb *srb = container_of(ref, struct srb, srb_ref);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001807 struct scsi_cmnd *cmd = srb->cmd;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301808 struct scsi_qla_host *ha = srb->ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001809
1810 qla4xxx_srb_free_dma(ha, srb);
1811
1812 mempool_free(srb, ha->srb_mempool);
1813
1814 cmd->scsi_done(cmd);
1815}
1816
1817/**
1818 * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001819 * @host: scsi host
David Somayajuluafaf5a22006-09-19 10:28:00 -07001820 * @cmd: Pointer to Linux's SCSI command structure
David Somayajuluafaf5a22006-09-19 10:28:00 -07001821 *
1822 * Remarks:
1823 * This routine is invoked by Linux to send a SCSI command to the driver.
1824 * The mid-level driver tries to ensure that queuecommand never gets
1825 * invoked concurrently with itself or the interrupt handler (although
1826 * the interrupt handler may call this routine as part of request-
1827 * completion handling). Unfortunely, it sometimes calls the scheduler
1828 * in interrupt context which is a big NO! NO!.
1829 **/
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001830static int qla4xxx_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001831{
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001832 struct scsi_qla_host *ha = to_qla_host(host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001833 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christie7fb19212008-01-31 13:36:45 -06001834 struct iscsi_cls_session *sess = ddb_entry->sess;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001835 struct srb *srb;
1836 int rval;
1837
Lalit Chandivade2232be02010-07-30 14:38:47 +05301838 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1839 if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
1840 cmd->result = DID_NO_CONNECT << 16;
1841 else
1842 cmd->result = DID_REQUEUE << 16;
1843 goto qc_fail_command;
1844 }
1845
Mike Christie7fb19212008-01-31 13:36:45 -06001846 if (!sess) {
1847 cmd->result = DID_IMM_RETRY << 16;
1848 goto qc_fail_command;
1849 }
1850
1851 rval = iscsi_session_chkready(sess);
1852 if (rval) {
1853 cmd->result = rval;
1854 goto qc_fail_command;
1855 }
1856
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301857 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1858 test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
1859 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1860 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
1861 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
1862 !test_bit(AF_ONLINE, &ha->flags) ||
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001863 !test_bit(AF_LINK_UP, &ha->flags) ||
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301864 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
David C Somayajulu477ffb92007-01-22 12:26:11 -08001865 goto qc_host_busy;
1866
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001867 srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001868 if (!srb)
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001869 goto qc_host_busy;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001870
1871 rval = qla4xxx_send_command_to_isp(ha, srb);
1872 if (rval != QLA_SUCCESS)
1873 goto qc_host_busy_free_sp;
1874
David Somayajuluafaf5a22006-09-19 10:28:00 -07001875 return 0;
1876
1877qc_host_busy_free_sp:
1878 qla4xxx_srb_free_dma(ha, srb);
1879 mempool_free(srb, ha->srb_mempool);
1880
David Somayajuluafaf5a22006-09-19 10:28:00 -07001881qc_host_busy:
1882 return SCSI_MLQUEUE_HOST_BUSY;
1883
1884qc_fail_command:
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001885 cmd->scsi_done(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001886
1887 return 0;
1888}
1889
1890/**
1891 * qla4xxx_mem_free - frees memory allocated to adapter
1892 * @ha: Pointer to host adapter structure.
1893 *
1894 * Frees memory previously allocated by qla4xxx_mem_alloc
1895 **/
1896static void qla4xxx_mem_free(struct scsi_qla_host *ha)
1897{
1898 if (ha->queues)
1899 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
1900 ha->queues_dma);
1901
1902 ha->queues_len = 0;
1903 ha->queues = NULL;
1904 ha->queues_dma = 0;
1905 ha->request_ring = NULL;
1906 ha->request_dma = 0;
1907 ha->response_ring = NULL;
1908 ha->response_dma = 0;
1909 ha->shadow_regs = NULL;
1910 ha->shadow_regs_dma = 0;
1911
1912 /* Free srb pool. */
1913 if (ha->srb_mempool)
1914 mempool_destroy(ha->srb_mempool);
1915
1916 ha->srb_mempool = NULL;
1917
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001918 if (ha->chap_dma_pool)
1919 dma_pool_destroy(ha->chap_dma_pool);
1920
Lalit Chandivade45494152011-10-07 16:55:42 -07001921 if (ha->chap_list)
1922 vfree(ha->chap_list);
1923 ha->chap_list = NULL;
1924
Mike Christie13483732011-12-01 21:38:41 -06001925 if (ha->fw_ddb_dma_pool)
1926 dma_pool_destroy(ha->fw_ddb_dma_pool);
1927
David Somayajuluafaf5a22006-09-19 10:28:00 -07001928 /* release io space registers */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301929 if (is_qla8022(ha)) {
1930 if (ha->nx_pcibase)
1931 iounmap(
1932 (struct device_reg_82xx __iomem *)ha->nx_pcibase);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301933 } else if (ha->reg)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001934 iounmap(ha->reg);
1935 pci_release_regions(ha->pdev);
1936}
1937
1938/**
1939 * qla4xxx_mem_alloc - allocates memory for use by adapter.
1940 * @ha: Pointer to host adapter structure
1941 *
1942 * Allocates DMA memory for request and response queues. Also allocates memory
1943 * for srbs.
1944 **/
1945static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
1946{
1947 unsigned long align;
1948
1949 /* Allocate contiguous block of DMA memory for queues. */
1950 ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
1951 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
1952 sizeof(struct shadow_regs) +
1953 MEM_ALIGN_VALUE +
1954 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
1955 ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
1956 &ha->queues_dma, GFP_KERNEL);
1957 if (ha->queues == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301958 ql4_printk(KERN_WARNING, ha,
1959 "Memory Allocation failed - queues.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001960
1961 goto mem_alloc_error_exit;
1962 }
1963 memset(ha->queues, 0, ha->queues_len);
1964
1965 /*
1966 * As per RISC alignment requirements -- the bus-address must be a
1967 * multiple of the request-ring size (in bytes).
1968 */
1969 align = 0;
1970 if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
1971 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
1972 (MEM_ALIGN_VALUE - 1));
1973
1974 /* Update request and response queue pointers. */
1975 ha->request_dma = ha->queues_dma + align;
1976 ha->request_ring = (struct queue_entry *) (ha->queues + align);
1977 ha->response_dma = ha->queues_dma + align +
1978 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
1979 ha->response_ring = (struct queue_entry *) (ha->queues + align +
1980 (REQUEST_QUEUE_DEPTH *
1981 QUEUE_SIZE));
1982 ha->shadow_regs_dma = ha->queues_dma + align +
1983 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
1984 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
1985 ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
1986 (REQUEST_QUEUE_DEPTH *
1987 QUEUE_SIZE) +
1988 (RESPONSE_QUEUE_DEPTH *
1989 QUEUE_SIZE));
1990
1991 /* Allocate memory for srb pool. */
1992 ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
1993 mempool_free_slab, srb_cachep);
1994 if (ha->srb_mempool == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301995 ql4_printk(KERN_WARNING, ha,
1996 "Memory Allocation failed - SRB Pool.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001997
1998 goto mem_alloc_error_exit;
1999 }
2000
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002001 ha->chap_dma_pool = dma_pool_create("ql4_chap", &ha->pdev->dev,
2002 CHAP_DMA_BLOCK_SIZE, 8, 0);
2003
2004 if (ha->chap_dma_pool == NULL) {
2005 ql4_printk(KERN_WARNING, ha,
2006 "%s: chap_dma_pool allocation failed..\n", __func__);
2007 goto mem_alloc_error_exit;
2008 }
2009
Mike Christie13483732011-12-01 21:38:41 -06002010 ha->fw_ddb_dma_pool = dma_pool_create("ql4_fw_ddb", &ha->pdev->dev,
2011 DDB_DMA_BLOCK_SIZE, 8, 0);
2012
2013 if (ha->fw_ddb_dma_pool == NULL) {
2014 ql4_printk(KERN_WARNING, ha,
2015 "%s: fw_ddb_dma_pool allocation failed..\n",
2016 __func__);
2017 goto mem_alloc_error_exit;
2018 }
2019
David Somayajuluafaf5a22006-09-19 10:28:00 -07002020 return QLA_SUCCESS;
2021
2022mem_alloc_error_exit:
2023 qla4xxx_mem_free(ha);
2024 return QLA_ERROR;
2025}
2026
2027/**
Mike Hernandez4f770832012-01-11 02:44:15 -08002028 * qla4_8xxx_check_temp - Check the ISP82XX temperature.
2029 * @ha: adapter block pointer.
2030 *
2031 * Note: The caller should not hold the idc lock.
2032 **/
2033static int qla4_8xxx_check_temp(struct scsi_qla_host *ha)
2034{
2035 uint32_t temp, temp_state, temp_val;
2036 int status = QLA_SUCCESS;
2037
2038 temp = qla4_8xxx_rd_32(ha, CRB_TEMP_STATE);
2039
2040 temp_state = qla82xx_get_temp_state(temp);
2041 temp_val = qla82xx_get_temp_val(temp);
2042
2043 if (temp_state == QLA82XX_TEMP_PANIC) {
2044 ql4_printk(KERN_WARNING, ha, "Device temperature %d degrees C"
2045 " exceeds maximum allowed. Hardware has been shut"
2046 " down.\n", temp_val);
2047 status = QLA_ERROR;
2048 } else if (temp_state == QLA82XX_TEMP_WARN) {
2049 if (ha->temperature == QLA82XX_TEMP_NORMAL)
2050 ql4_printk(KERN_WARNING, ha, "Device temperature %d"
2051 " degrees C exceeds operating range."
2052 " Immediate action needed.\n", temp_val);
2053 } else {
2054 if (ha->temperature == QLA82XX_TEMP_WARN)
2055 ql4_printk(KERN_INFO, ha, "Device temperature is"
2056 " now %d degrees C in normal range.\n",
2057 temp_val);
2058 }
2059 ha->temperature = temp_state;
2060 return status;
2061}
2062
2063/**
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302064 * qla4_8xxx_check_fw_alive - Check firmware health
2065 * @ha: Pointer to host adapter structure.
2066 *
2067 * Context: Interrupt
2068 **/
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002069static int qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302070{
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002071 uint32_t fw_heartbeat_counter;
2072 int status = QLA_SUCCESS;
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302073
2074 fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
Lalit Chandivade2232be02010-07-30 14:38:47 +05302075 /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
2076 if (fw_heartbeat_counter == 0xffffffff) {
2077 DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
2078 "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
2079 ha->host_no, __func__));
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002080 return status;
Lalit Chandivade2232be02010-07-30 14:38:47 +05302081 }
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302082
2083 if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
2084 ha->seconds_since_last_heartbeat++;
2085 /* FW not alive after 2 seconds */
2086 if (ha->seconds_since_last_heartbeat == 2) {
2087 ha->seconds_since_last_heartbeat = 0;
Vikas Chaudhary68d92eb2011-05-17 23:17:05 -07002088
2089 ql4_printk(KERN_INFO, ha,
2090 "scsi(%ld): %s, Dumping hw/fw registers:\n "
2091 " PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2:"
2092 " 0x%x,\n PEG_NET_0_PC: 0x%x, PEG_NET_1_PC:"
2093 " 0x%x,\n PEG_NET_2_PC: 0x%x, PEG_NET_3_PC:"
2094 " 0x%x,\n PEG_NET_4_PC: 0x%x\n",
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002095 ha->host_no, __func__,
2096 qla4_8xxx_rd_32(ha,
2097 QLA82XX_PEG_HALT_STATUS1),
Vikas Chaudhary68d92eb2011-05-17 23:17:05 -07002098 qla4_8xxx_rd_32(ha,
2099 QLA82XX_PEG_HALT_STATUS2),
2100 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_0 +
2101 0x3c),
2102 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_1 +
2103 0x3c),
2104 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_2 +
2105 0x3c),
2106 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_3 +
2107 0x3c),
2108 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_4 +
2109 0x3c));
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002110 status = QLA_ERROR;
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302111 }
Lalit Chandivade99457d72010-10-06 22:49:32 -07002112 } else
2113 ha->seconds_since_last_heartbeat = 0;
2114
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302115 ha->fw_heartbeat_counter = fw_heartbeat_counter;
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002116 return status;
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302117}
2118
2119/**
2120 * qla4_8xxx_watchdog - Poll dev state
2121 * @ha: Pointer to host adapter structure.
2122 *
2123 * Context: Interrupt
2124 **/
2125void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
2126{
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002127 uint32_t dev_state, halt_status;
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302128
2129 /* don't poll if reset is going on */
Lalit Chandivaded56a1f72010-12-02 22:12:45 -08002130 if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
2131 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
Vikas Chaudhary977f46a2011-05-17 23:17:08 -07002132 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags))) {
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002133 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
Mike Hernandez4f770832012-01-11 02:44:15 -08002134
2135 if (qla4_8xxx_check_temp(ha)) {
Giridhar Malavalie6bd0eb2012-01-11 02:44:16 -08002136 ql4_printk(KERN_INFO, ha, "disabling pause"
2137 " transmit on port 0 & 1.\n");
2138 qla4_8xxx_wr_32(ha, QLA82XX_CRB_NIU + 0x98,
2139 CRB_NIU_XG_PAUSE_CTL_P0 |
2140 CRB_NIU_XG_PAUSE_CTL_P1);
Mike Hernandez4f770832012-01-11 02:44:15 -08002141 set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
2142 qla4xxx_wake_dpc(ha);
2143 } else if (dev_state == QLA82XX_DEV_NEED_RESET &&
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302144 !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
Vikas Chaudhary3930b8c2010-12-02 22:12:47 -08002145 if (!ql4xdontresethba) {
2146 ql4_printk(KERN_INFO, ha, "%s: HW State: "
2147 "NEED RESET!\n", __func__);
2148 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2149 qla4xxx_wake_dpc(ha);
Vikas Chaudhary3930b8c2010-12-02 22:12:47 -08002150 }
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302151 } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
2152 !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
Vikas Chaudhary3930b8c2010-12-02 22:12:47 -08002153 ql4_printk(KERN_INFO, ha, "%s: HW State: NEED QUIES!\n",
2154 __func__);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302155 set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
2156 qla4xxx_wake_dpc(ha);
2157 } else {
2158 /* Check firmware health */
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002159 if (qla4_8xxx_check_fw_alive(ha)) {
Giridhar Malavalie6bd0eb2012-01-11 02:44:16 -08002160 ql4_printk(KERN_INFO, ha, "disabling pause"
2161 " transmit on port 0 & 1.\n");
2162 qla4_8xxx_wr_32(ha, QLA82XX_CRB_NIU + 0x98,
2163 CRB_NIU_XG_PAUSE_CTL_P0 |
2164 CRB_NIU_XG_PAUSE_CTL_P1);
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002165 halt_status = qla4_8xxx_rd_32(ha,
2166 QLA82XX_PEG_HALT_STATUS1);
2167
Vikas Chaudhary46801ba2012-02-13 18:30:45 +05302168 if (QLA82XX_FWERROR_CODE(halt_status) == 0x67)
Nilesh Javali527c8b22012-01-11 02:44:17 -08002169 ql4_printk(KERN_ERR, ha, "%s:"
2170 " Firmware aborted with"
2171 " error code 0x00006700."
2172 " Device is being reset\n",
2173 __func__);
2174
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002175 /* Since we cannot change dev_state in interrupt
2176 * context, set appropriate DPC flag then wakeup
2177 * DPC */
2178 if (halt_status & HALT_STATUS_UNRECOVERABLE)
2179 set_bit(DPC_HA_UNRECOVERABLE,
2180 &ha->dpc_flags);
2181 else {
2182 ql4_printk(KERN_INFO, ha, "%s: detect "
2183 "abort needed!\n", __func__);
2184 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2185 }
2186 qla4xxx_mailbox_premature_completion(ha);
2187 qla4xxx_wake_dpc(ha);
2188 }
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302189 }
2190 }
2191}
2192
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08002193static void qla4xxx_check_relogin_flash_ddb(struct iscsi_cls_session *cls_sess)
Mike Christie13483732011-12-01 21:38:41 -06002194{
2195 struct iscsi_session *sess;
2196 struct ddb_entry *ddb_entry;
2197 struct scsi_qla_host *ha;
2198
2199 sess = cls_sess->dd_data;
2200 ddb_entry = sess->dd_data;
2201 ha = ddb_entry->ha;
2202
2203 if (!(ddb_entry->ddb_type == FLASH_DDB))
2204 return;
2205
2206 if (adapter_up(ha) && !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
2207 !iscsi_is_session_online(cls_sess)) {
2208 if (atomic_read(&ddb_entry->retry_relogin_timer) !=
2209 INVALID_ENTRY) {
2210 if (atomic_read(&ddb_entry->retry_relogin_timer) ==
2211 0) {
2212 atomic_set(&ddb_entry->retry_relogin_timer,
2213 INVALID_ENTRY);
2214 set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
2215 set_bit(DF_RELOGIN, &ddb_entry->flags);
2216 DEBUG2(ql4_printk(KERN_INFO, ha,
2217 "%s: index [%d] login device\n",
2218 __func__, ddb_entry->fw_ddb_index));
2219 } else
2220 atomic_dec(&ddb_entry->retry_relogin_timer);
2221 }
2222 }
2223
2224 /* Wait for relogin to timeout */
2225 if (atomic_read(&ddb_entry->relogin_timer) &&
2226 (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
2227 /*
2228 * If the relogin times out and the device is
2229 * still NOT ONLINE then try and relogin again.
2230 */
2231 if (!iscsi_is_session_online(cls_sess)) {
2232 /* Reset retry relogin timer */
2233 atomic_inc(&ddb_entry->relogin_retry_count);
2234 DEBUG2(ql4_printk(KERN_INFO, ha,
2235 "%s: index[%d] relogin timed out-retrying"
2236 " relogin (%d), retry (%d)\n", __func__,
2237 ddb_entry->fw_ddb_index,
2238 atomic_read(&ddb_entry->relogin_retry_count),
2239 ddb_entry->default_time2wait + 4));
2240 set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
2241 atomic_set(&ddb_entry->retry_relogin_timer,
2242 ddb_entry->default_time2wait + 4);
2243 }
2244 }
2245}
2246
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302247/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002248 * qla4xxx_timer - checks every second for work to do.
2249 * @ha: Pointer to host adapter structure.
2250 **/
2251static void qla4xxx_timer(struct scsi_qla_host *ha)
2252{
David Somayajuluafaf5a22006-09-19 10:28:00 -07002253 int start_dpc = 0;
Lalit Chandivade2232be02010-07-30 14:38:47 +05302254 uint16_t w;
2255
Mike Christie13483732011-12-01 21:38:41 -06002256 iscsi_host_for_each_session(ha->host, qla4xxx_check_relogin_flash_ddb);
2257
Lalit Chandivade2232be02010-07-30 14:38:47 +05302258 /* If we are in the middle of AER/EEH processing
2259 * skip any processing and reschedule the timer
2260 */
2261 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
2262 mod_timer(&ha->timer, jiffies + HZ);
2263 return;
2264 }
2265
2266 /* Hardware read to trigger an EEH error during mailbox waits. */
2267 if (!pci_channel_offline(ha->pdev))
2268 pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002269
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302270 if (is_qla8022(ha)) {
2271 qla4_8xxx_watchdog(ha);
2272 }
2273
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302274 if (!is_qla8022(ha)) {
2275 /* Check for heartbeat interval. */
2276 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
2277 ha->heartbeat_interval != 0) {
2278 ha->seconds_since_last_heartbeat++;
2279 if (ha->seconds_since_last_heartbeat >
2280 ha->heartbeat_interval + 2)
2281 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2282 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002283 }
2284
Vikas Chaudharyff884432011-08-29 23:43:02 +05302285 /* Process any deferred work. */
2286 if (!list_empty(&ha->work_list))
2287 start_dpc++;
2288
David Somayajuluafaf5a22006-09-19 10:28:00 -07002289 /* Wakeup the dpc routine for this adapter, if needed. */
Lalit Chandivade1b468072011-05-17 23:17:09 -07002290 if (start_dpc ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07002291 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
2292 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
2293 test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302294 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07002295 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
2296 test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05302297 test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302298 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
2299 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
Lalit Chandivade1b468072011-05-17 23:17:09 -07002300 test_bit(DPC_AEN, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002301 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
2302 " - dpc flags = 0x%lx\n",
2303 ha->host_no, __func__, ha->dpc_flags));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302304 qla4xxx_wake_dpc(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002305 }
2306
2307 /* Reschedule timer thread to call us back in one second */
2308 mod_timer(&ha->timer, jiffies + HZ);
2309
2310 DEBUG2(ha->seconds_since_last_intr++);
2311}
2312
2313/**
2314 * qla4xxx_cmd_wait - waits for all outstanding commands to complete
2315 * @ha: Pointer to host adapter structure.
2316 *
2317 * This routine stalls the driver until all outstanding commands are returned.
2318 * Caller must release the Hardware Lock prior to calling this routine.
2319 **/
2320static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
2321{
2322 uint32_t index = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002323 unsigned long flags;
2324 struct scsi_cmnd *cmd;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002325
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302326 unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
2327
2328 DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
2329 "complete\n", WAIT_CMD_TOV));
2330
2331 while (!time_after_eq(jiffies, wtime)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002332 spin_lock_irqsave(&ha->hardware_lock, flags);
2333 /* Find a command that hasn't completed. */
2334 for (index = 0; index < ha->host->can_queue; index++) {
2335 cmd = scsi_host_find_tag(ha->host, index);
Mike Christiea1e00632010-10-26 05:45:30 -07002336 /*
2337 * We cannot just check if the index is valid,
2338 * becase if we are run from the scsi eh, then
2339 * the scsi/block layer is going to prevent
2340 * the tag from being released.
2341 */
2342 if (cmd != NULL && CMD_SP(cmd))
David Somayajuluafaf5a22006-09-19 10:28:00 -07002343 break;
2344 }
2345 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2346
2347 /* If No Commands are pending, wait is complete */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302348 if (index == ha->host->can_queue)
2349 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002350
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302351 msleep(1000);
2352 }
2353 /* If we timed out on waiting for commands to come back
2354 * return ERROR. */
2355 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002356}
2357
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302358int qla4xxx_hw_reset(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002359{
David Somayajuluafaf5a22006-09-19 10:28:00 -07002360 uint32_t ctrl_status;
David C Somayajulu477ffb92007-01-22 12:26:11 -08002361 unsigned long flags = 0;
2362
2363 DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -07002364
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302365 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
2366 return QLA_ERROR;
2367
David Somayajuluafaf5a22006-09-19 10:28:00 -07002368 spin_lock_irqsave(&ha->hardware_lock, flags);
2369
2370 /*
2371 * If the SCSI Reset Interrupt bit is set, clear it.
2372 * Otherwise, the Soft Reset won't work.
2373 */
2374 ctrl_status = readw(&ha->reg->ctrl_status);
2375 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
2376 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
2377
2378 /* Issue Soft Reset */
2379 writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
2380 readl(&ha->reg->ctrl_status);
2381
2382 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302383 return QLA_SUCCESS;
David C Somayajulu477ffb92007-01-22 12:26:11 -08002384}
2385
2386/**
2387 * qla4xxx_soft_reset - performs soft reset.
2388 * @ha: Pointer to host adapter structure.
2389 **/
2390int qla4xxx_soft_reset(struct scsi_qla_host *ha)
2391{
2392 uint32_t max_wait_time;
2393 unsigned long flags = 0;
Vikas Chaudharyf931c532010-10-06 22:48:07 -07002394 int status;
David C Somayajulu477ffb92007-01-22 12:26:11 -08002395 uint32_t ctrl_status;
2396
Vikas Chaudharyf931c532010-10-06 22:48:07 -07002397 status = qla4xxx_hw_reset(ha);
2398 if (status != QLA_SUCCESS)
2399 return status;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002400
Vikas Chaudharyf931c532010-10-06 22:48:07 -07002401 status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002402 /* Wait until the Network Reset Intr bit is cleared */
2403 max_wait_time = RESET_INTR_TOV;
2404 do {
2405 spin_lock_irqsave(&ha->hardware_lock, flags);
2406 ctrl_status = readw(&ha->reg->ctrl_status);
2407 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2408
2409 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
2410 break;
2411
2412 msleep(1000);
2413 } while ((--max_wait_time));
2414
2415 if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
2416 DEBUG2(printk(KERN_WARNING
2417 "scsi%ld: Network Reset Intr not cleared by "
2418 "Network function, clearing it now!\n",
2419 ha->host_no));
2420 spin_lock_irqsave(&ha->hardware_lock, flags);
2421 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
2422 readl(&ha->reg->ctrl_status);
2423 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2424 }
2425
2426 /* Wait until the firmware tells us the Soft Reset is done */
2427 max_wait_time = SOFT_RESET_TOV;
2428 do {
2429 spin_lock_irqsave(&ha->hardware_lock, flags);
2430 ctrl_status = readw(&ha->reg->ctrl_status);
2431 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2432
2433 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
2434 status = QLA_SUCCESS;
2435 break;
2436 }
2437
2438 msleep(1000);
2439 } while ((--max_wait_time));
2440
2441 /*
2442 * Also, make sure that the SCSI Reset Interrupt bit has been cleared
2443 * after the soft reset has taken place.
2444 */
2445 spin_lock_irqsave(&ha->hardware_lock, flags);
2446 ctrl_status = readw(&ha->reg->ctrl_status);
2447 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
2448 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
2449 readl(&ha->reg->ctrl_status);
2450 }
2451 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2452
2453 /* If soft reset fails then most probably the bios on other
2454 * function is also enabled.
2455 * Since the initialization is sequential the other fn
2456 * wont be able to acknowledge the soft reset.
2457 * Issue a force soft reset to workaround this scenario.
2458 */
2459 if (max_wait_time == 0) {
2460 /* Issue Force Soft Reset */
2461 spin_lock_irqsave(&ha->hardware_lock, flags);
2462 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
2463 readl(&ha->reg->ctrl_status);
2464 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2465 /* Wait until the firmware tells us the Soft Reset is done */
2466 max_wait_time = SOFT_RESET_TOV;
2467 do {
2468 spin_lock_irqsave(&ha->hardware_lock, flags);
2469 ctrl_status = readw(&ha->reg->ctrl_status);
2470 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2471
2472 if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
2473 status = QLA_SUCCESS;
2474 break;
2475 }
2476
2477 msleep(1000);
2478 } while ((--max_wait_time));
2479 }
2480
2481 return status;
2482}
2483
2484/**
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302485 * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
David Somayajuluafaf5a22006-09-19 10:28:00 -07002486 * @ha: Pointer to host adapter structure.
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302487 * @res: returned scsi status
David Somayajuluafaf5a22006-09-19 10:28:00 -07002488 *
2489 * This routine is called just prior to a HARD RESET to return all
2490 * outstanding commands back to the Operating System.
2491 * Caller should make sure that the following locks are released
2492 * before this calling routine: Hardware lock, and io_request_lock.
2493 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302494static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002495{
2496 struct srb *srb;
2497 int i;
2498 unsigned long flags;
2499
2500 spin_lock_irqsave(&ha->hardware_lock, flags);
2501 for (i = 0; i < ha->host->can_queue; i++) {
2502 srb = qla4xxx_del_from_active_array(ha, i);
2503 if (srb != NULL) {
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302504 srb->cmd->result = res;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302505 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002506 }
2507 }
2508 spin_unlock_irqrestore(&ha->hardware_lock, flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002509}
2510
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302511void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
2512{
2513 clear_bit(AF_ONLINE, &ha->flags);
2514
2515 /* Disable the board */
2516 ql4_printk(KERN_INFO, ha, "Disabling the board\n");
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302517
2518 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
2519 qla4xxx_mark_all_devices_missing(ha);
2520 clear_bit(AF_INIT_DONE, &ha->flags);
2521}
2522
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002523static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session)
2524{
2525 struct iscsi_session *sess;
2526 struct ddb_entry *ddb_entry;
2527
2528 sess = cls_session->dd_data;
2529 ddb_entry = sess->dd_data;
2530 ddb_entry->fw_ddb_device_state = DDB_DS_SESSION_FAILED;
Mike Christie13483732011-12-01 21:38:41 -06002531
2532 if (ddb_entry->ddb_type == FLASH_DDB)
2533 iscsi_block_session(ddb_entry->sess);
2534 else
2535 iscsi_session_failure(cls_session->dd_data,
2536 ISCSI_ERR_CONN_FAILED);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002537}
2538
David Somayajuluafaf5a22006-09-19 10:28:00 -07002539/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002540 * qla4xxx_recover_adapter - recovers adapter after a fatal error
2541 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07002542 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302543static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002544{
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302545 int status = QLA_ERROR;
2546 uint8_t reset_chip = 0;
Sarang Radke8e0f3a62011-12-01 22:42:09 -08002547 uint32_t dev_state;
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002548 unsigned long wait;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002549
2550 /* Stall incoming I/O until we are done */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302551 scsi_block_requests(ha->host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002552 clear_bit(AF_ONLINE, &ha->flags);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002553 clear_bit(AF_LINK_UP, &ha->flags);
Mike Christie50a29ae2008-03-04 13:26:53 -06002554
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302555 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -07002556
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302557 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002558
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002559 iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
2560
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302561 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
2562 reset_chip = 1;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002563
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302564 /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
2565 * do not reset adapter, jump to initialize_adapter */
2566 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
2567 status = QLA_SUCCESS;
2568 goto recover_ha_init_adapter;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002569 }
2570
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302571 /* For the ISP-82xx adapter, issue a stop_firmware if invoked
2572 * from eh_host_reset or ioctl module */
2573 if (is_qla8022(ha) && !reset_chip &&
2574 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
2575
2576 DEBUG2(ql4_printk(KERN_INFO, ha,
2577 "scsi%ld: %s - Performing stop_firmware...\n",
2578 ha->host_no, __func__));
2579 status = ha->isp_ops->reset_firmware(ha);
2580 if (status == QLA_SUCCESS) {
Nilesh Javali2bd1e2b2010-10-06 22:49:20 -07002581 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
2582 qla4xxx_cmd_wait(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302583 ha->isp_ops->disable_intrs(ha);
2584 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2585 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
2586 } else {
2587 /* If the stop_firmware fails then
2588 * reset the entire chip */
2589 reset_chip = 1;
2590 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2591 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2592 }
2593 }
2594
2595 /* Issue full chip reset if recovering from a catastrophic error,
2596 * or if stop_firmware fails for ISP-82xx.
2597 * This is the default case for ISP-4xxx */
2598 if (!is_qla8022(ha) || reset_chip) {
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002599 if (!is_qla8022(ha))
2600 goto chip_reset;
2601
2602 /* Check if 82XX firmware is alive or not
2603 * We may have arrived here from NEED_RESET
2604 * detection only */
2605 if (test_bit(AF_FW_RECOVERY, &ha->flags))
2606 goto chip_reset;
2607
2608 wait = jiffies + (FW_ALIVE_WAIT_TOV * HZ);
2609 while (time_before(jiffies, wait)) {
2610 if (qla4_8xxx_check_fw_alive(ha)) {
2611 qla4xxx_mailbox_premature_completion(ha);
2612 break;
2613 }
2614
2615 set_current_state(TASK_UNINTERRUPTIBLE);
2616 schedule_timeout(HZ);
2617 }
2618
Nilesh Javali2bd1e2b2010-10-06 22:49:20 -07002619 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
2620 qla4xxx_cmd_wait(ha);
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002621chip_reset:
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302622 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2623 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
2624 DEBUG2(ql4_printk(KERN_INFO, ha,
2625 "scsi%ld: %s - Performing chip reset..\n",
2626 ha->host_no, __func__));
2627 status = ha->isp_ops->reset_chip(ha);
2628 }
2629
2630 /* Flush any pending ddb changed AENs */
2631 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2632
2633recover_ha_init_adapter:
2634 /* Upon successful firmware/chip reset, re-initialize the adapter */
2635 if (status == QLA_SUCCESS) {
2636 /* For ISP-4xxx, force function 1 to always initialize
2637 * before function 3 to prevent both funcions from
2638 * stepping on top of the other */
2639 if (!is_qla8022(ha) && (ha->mac_index == 3))
2640 ssleep(6);
2641
2642 /* NOTE: AF_ONLINE flag set upon successful completion of
2643 * qla4xxx_initialize_adapter */
Mike Christie13483732011-12-01 21:38:41 -06002644 status = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302645 }
2646
2647 /* Retry failed adapter initialization, if necessary
2648 * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
2649 * case to prevent ping-pong resets between functions */
2650 if (!test_bit(AF_ONLINE, &ha->flags) &&
2651 !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002652 /* Adapter initialization failed, see if we can retry
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302653 * resetting the ha.
2654 * Since we don't want to block the DPC for too long
2655 * with multiple resets in the same thread,
2656 * utilize DPC to retry */
Sarang Radke8e0f3a62011-12-01 22:42:09 -08002657 if (is_qla8022(ha)) {
2658 qla4_8xxx_idc_lock(ha);
2659 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
2660 qla4_8xxx_idc_unlock(ha);
2661 if (dev_state == QLA82XX_DEV_FAILED) {
2662 ql4_printk(KERN_INFO, ha, "%s: don't retry "
2663 "recover adapter. H/W is in Failed "
2664 "state\n", __func__);
2665 qla4xxx_dead_adapter_cleanup(ha);
2666 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
2667 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
2668 clear_bit(DPC_RESET_HA_FW_CONTEXT,
2669 &ha->dpc_flags);
2670 status = QLA_ERROR;
2671
2672 goto exit_recover;
2673 }
2674 }
2675
David Somayajuluafaf5a22006-09-19 10:28:00 -07002676 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
2677 ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
2678 DEBUG2(printk("scsi%ld: recover adapter - retrying "
2679 "(%d) more times\n", ha->host_no,
2680 ha->retry_reset_ha_cnt));
2681 set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
2682 status = QLA_ERROR;
2683 } else {
2684 if (ha->retry_reset_ha_cnt > 0) {
2685 /* Schedule another Reset HA--DPC will retry */
2686 ha->retry_reset_ha_cnt--;
2687 DEBUG2(printk("scsi%ld: recover adapter - "
2688 "retry remaining %d\n",
2689 ha->host_no,
2690 ha->retry_reset_ha_cnt));
2691 status = QLA_ERROR;
2692 }
2693
2694 if (ha->retry_reset_ha_cnt == 0) {
2695 /* Recover adapter retries have been exhausted.
2696 * Adapter DEAD */
2697 DEBUG2(printk("scsi%ld: recover adapter "
2698 "failed - board disabled\n",
2699 ha->host_no));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302700 qla4xxx_dead_adapter_cleanup(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002701 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
2702 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302703 clear_bit(DPC_RESET_HA_FW_CONTEXT,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002704 &ha->dpc_flags);
2705 status = QLA_ERROR;
2706 }
2707 }
2708 } else {
2709 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302710 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002711 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
2712 }
2713
Sarang Radke8e0f3a62011-12-01 22:42:09 -08002714exit_recover:
David Somayajuluafaf5a22006-09-19 10:28:00 -07002715 ha->adapter_error_count++;
2716
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302717 if (test_bit(AF_ONLINE, &ha->flags))
2718 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002719
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302720 scsi_unblock_requests(ha->host);
2721
2722 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2723 DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002724 status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302725
David Somayajuluafaf5a22006-09-19 10:28:00 -07002726 return status;
2727}
2728
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002729static void qla4xxx_relogin_devices(struct iscsi_cls_session *cls_session)
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07002730{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002731 struct iscsi_session *sess;
2732 struct ddb_entry *ddb_entry;
2733 struct scsi_qla_host *ha;
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07002734
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002735 sess = cls_session->dd_data;
2736 ddb_entry = sess->dd_data;
2737 ha = ddb_entry->ha;
2738 if (!iscsi_is_session_online(cls_session)) {
2739 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
2740 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
2741 " unblock session\n", ha->host_no, __func__,
2742 ddb_entry->fw_ddb_index);
2743 iscsi_unblock_session(ddb_entry->sess);
2744 } else {
2745 /* Trigger relogin */
Mike Christie13483732011-12-01 21:38:41 -06002746 if (ddb_entry->ddb_type == FLASH_DDB) {
2747 if (!test_bit(DF_RELOGIN, &ddb_entry->flags))
2748 qla4xxx_arm_relogin_timer(ddb_entry);
2749 } else
2750 iscsi_session_failure(cls_session->dd_data,
2751 ISCSI_ERR_CONN_FAILED);
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07002752 }
2753 }
2754}
2755
Mike Christie13483732011-12-01 21:38:41 -06002756int qla4xxx_unblock_flash_ddb(struct iscsi_cls_session *cls_session)
2757{
2758 struct iscsi_session *sess;
2759 struct ddb_entry *ddb_entry;
2760 struct scsi_qla_host *ha;
2761
2762 sess = cls_session->dd_data;
2763 ddb_entry = sess->dd_data;
2764 ha = ddb_entry->ha;
2765 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
2766 " unblock session\n", ha->host_no, __func__,
2767 ddb_entry->fw_ddb_index);
2768
2769 iscsi_unblock_session(ddb_entry->sess);
2770
2771 /* Start scan target */
2772 if (test_bit(AF_ONLINE, &ha->flags)) {
2773 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
2774 " start scan\n", ha->host_no, __func__,
2775 ddb_entry->fw_ddb_index);
2776 scsi_queue_work(ha->host, &ddb_entry->sess->scan_work);
2777 }
2778 return QLA_SUCCESS;
2779}
2780
2781int qla4xxx_unblock_ddb(struct iscsi_cls_session *cls_session)
2782{
2783 struct iscsi_session *sess;
2784 struct ddb_entry *ddb_entry;
2785 struct scsi_qla_host *ha;
2786
2787 sess = cls_session->dd_data;
2788 ddb_entry = sess->dd_data;
2789 ha = ddb_entry->ha;
2790 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
2791 " unblock user space session\n", ha->host_no, __func__,
2792 ddb_entry->fw_ddb_index);
2793 iscsi_conn_start(ddb_entry->conn);
2794 iscsi_conn_login_event(ddb_entry->conn,
2795 ISCSI_CONN_STATE_LOGGED_IN);
2796
2797 return QLA_SUCCESS;
2798}
2799
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002800static void qla4xxx_relogin_all_devices(struct scsi_qla_host *ha)
2801{
2802 iscsi_host_for_each_session(ha->host, qla4xxx_relogin_devices);
2803}
2804
Mike Christie13483732011-12-01 21:38:41 -06002805static void qla4xxx_relogin_flash_ddb(struct iscsi_cls_session *cls_sess)
2806{
2807 uint16_t relogin_timer;
2808 struct iscsi_session *sess;
2809 struct ddb_entry *ddb_entry;
2810 struct scsi_qla_host *ha;
2811
2812 sess = cls_sess->dd_data;
2813 ddb_entry = sess->dd_data;
2814 ha = ddb_entry->ha;
2815
2816 relogin_timer = max(ddb_entry->default_relogin_timeout,
2817 (uint16_t)RELOGIN_TOV);
2818 atomic_set(&ddb_entry->relogin_timer, relogin_timer);
2819
2820 DEBUG2(ql4_printk(KERN_INFO, ha,
2821 "scsi%ld: Relogin index [%d]. TOV=%d\n", ha->host_no,
2822 ddb_entry->fw_ddb_index, relogin_timer));
2823
2824 qla4xxx_login_flash_ddb(cls_sess);
2825}
2826
2827static void qla4xxx_dpc_relogin(struct iscsi_cls_session *cls_sess)
2828{
2829 struct iscsi_session *sess;
2830 struct ddb_entry *ddb_entry;
2831 struct scsi_qla_host *ha;
2832
2833 sess = cls_sess->dd_data;
2834 ddb_entry = sess->dd_data;
2835 ha = ddb_entry->ha;
2836
2837 if (!(ddb_entry->ddb_type == FLASH_DDB))
2838 return;
2839
2840 if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
2841 !iscsi_is_session_online(cls_sess)) {
2842 DEBUG2(ql4_printk(KERN_INFO, ha,
2843 "relogin issued\n"));
2844 qla4xxx_relogin_flash_ddb(cls_sess);
2845 }
2846}
2847
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302848void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
2849{
Lalit Chandivade1b468072011-05-17 23:17:09 -07002850 if (ha->dpc_thread)
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302851 queue_work(ha->dpc_thread, &ha->dpc_work);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302852}
2853
Vikas Chaudharyff884432011-08-29 23:43:02 +05302854static struct qla4_work_evt *
2855qla4xxx_alloc_work(struct scsi_qla_host *ha, uint32_t data_size,
2856 enum qla4_work_type type)
2857{
2858 struct qla4_work_evt *e;
2859 uint32_t size = sizeof(struct qla4_work_evt) + data_size;
2860
2861 e = kzalloc(size, GFP_ATOMIC);
2862 if (!e)
2863 return NULL;
2864
2865 INIT_LIST_HEAD(&e->list);
2866 e->type = type;
2867 return e;
2868}
2869
2870static void qla4xxx_post_work(struct scsi_qla_host *ha,
2871 struct qla4_work_evt *e)
2872{
2873 unsigned long flags;
2874
2875 spin_lock_irqsave(&ha->work_lock, flags);
2876 list_add_tail(&e->list, &ha->work_list);
2877 spin_unlock_irqrestore(&ha->work_lock, flags);
2878 qla4xxx_wake_dpc(ha);
2879}
2880
2881int qla4xxx_post_aen_work(struct scsi_qla_host *ha,
2882 enum iscsi_host_event_code aen_code,
2883 uint32_t data_size, uint8_t *data)
2884{
2885 struct qla4_work_evt *e;
2886
2887 e = qla4xxx_alloc_work(ha, data_size, QLA4_EVENT_AEN);
2888 if (!e)
2889 return QLA_ERROR;
2890
2891 e->u.aen.code = aen_code;
2892 e->u.aen.data_size = data_size;
2893 memcpy(e->u.aen.data, data, data_size);
2894
2895 qla4xxx_post_work(ha, e);
2896
2897 return QLA_SUCCESS;
2898}
2899
2900void qla4xxx_do_work(struct scsi_qla_host *ha)
2901{
2902 struct qla4_work_evt *e, *tmp;
2903 unsigned long flags;
2904 LIST_HEAD(work);
2905
2906 spin_lock_irqsave(&ha->work_lock, flags);
2907 list_splice_init(&ha->work_list, &work);
2908 spin_unlock_irqrestore(&ha->work_lock, flags);
2909
2910 list_for_each_entry_safe(e, tmp, &work, list) {
2911 list_del_init(&e->list);
2912
2913 switch (e->type) {
2914 case QLA4_EVENT_AEN:
2915 iscsi_post_host_event(ha->host_no,
2916 &qla4xxx_iscsi_transport,
2917 e->u.aen.code,
2918 e->u.aen.data_size,
2919 e->u.aen.data);
2920 break;
2921 default:
2922 ql4_printk(KERN_WARNING, ha, "event type: 0x%x not "
2923 "supported", e->type);
2924 }
2925 kfree(e);
2926 }
2927}
2928
David Somayajuluafaf5a22006-09-19 10:28:00 -07002929/**
2930 * qla4xxx_do_dpc - dpc routine
2931 * @data: in our case pointer to adapter structure
2932 *
2933 * This routine is a task that is schedule by the interrupt handler
2934 * to perform the background processing for interrupts. We put it
2935 * on a task queue that is consumed whenever the scheduler runs; that's
2936 * so you can do anything (i.e. put the process to sleep etc). In fact,
2937 * the mid-level tries to sleep when it reaches the driver threshold
2938 * "host->can_queue". This can cause a panic if we were in our interrupt code.
2939 **/
David Howellsc4028952006-11-22 14:57:56 +00002940static void qla4xxx_do_dpc(struct work_struct *work)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002941{
David Howellsc4028952006-11-22 14:57:56 +00002942 struct scsi_qla_host *ha =
2943 container_of(work, struct scsi_qla_host, dpc_work);
David C Somayajulu477ffb92007-01-22 12:26:11 -08002944 int status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002945
David C Somayajuluf26b9042006-11-15 16:41:09 -08002946 DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302947 "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
2948 ha->host_no, __func__, ha->flags, ha->dpc_flags))
David Somayajuluafaf5a22006-09-19 10:28:00 -07002949
2950 /* Initialization not yet finished. Don't do anything yet. */
2951 if (!test_bit(AF_INIT_DONE, &ha->flags))
Lalit Chandivade1b468072011-05-17 23:17:09 -07002952 return;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002953
Lalit Chandivade2232be02010-07-30 14:38:47 +05302954 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
2955 DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
2956 ha->host_no, __func__, ha->flags));
Lalit Chandivade1b468072011-05-17 23:17:09 -07002957 return;
Lalit Chandivade2232be02010-07-30 14:38:47 +05302958 }
2959
Vikas Chaudharyff884432011-08-29 23:43:02 +05302960 /* post events to application */
2961 qla4xxx_do_work(ha);
2962
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302963 if (is_qla8022(ha)) {
2964 if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
2965 qla4_8xxx_idc_lock(ha);
2966 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2967 QLA82XX_DEV_FAILED);
2968 qla4_8xxx_idc_unlock(ha);
2969 ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
2970 qla4_8xxx_device_state_handler(ha);
2971 }
2972 if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
2973 qla4_8xxx_need_qsnt_handler(ha);
2974 }
2975 }
2976
2977 if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
2978 (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07002979 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302980 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
2981 if (ql4xdontresethba) {
2982 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
2983 ha->host_no, __func__));
2984 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
2985 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
2986 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2987 goto dpc_post_reset_ha;
2988 }
2989 if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
2990 test_bit(DPC_RESET_HA, &ha->dpc_flags))
2991 qla4xxx_recover_adapter(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002992
David C Somayajulu477ffb92007-01-22 12:26:11 -08002993 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002994 uint8_t wait_time = RESET_INTR_TOV;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002995
David Somayajuluafaf5a22006-09-19 10:28:00 -07002996 while ((readw(&ha->reg->ctrl_status) &
2997 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
2998 if (--wait_time == 0)
2999 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003000 msleep(1000);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003001 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07003002 if (wait_time == 0)
3003 DEBUG2(printk("scsi%ld: %s: SR|FSR "
3004 "bit not cleared-- resetting\n",
3005 ha->host_no, __func__));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303006 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
David C Somayajulu477ffb92007-01-22 12:26:11 -08003007 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
3008 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303009 status = qla4xxx_recover_adapter(ha);
David C Somayajulu477ffb92007-01-22 12:26:11 -08003010 }
3011 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
3012 if (status == QLA_SUCCESS)
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303013 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003014 }
3015 }
3016
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303017dpc_post_reset_ha:
David Somayajuluafaf5a22006-09-19 10:28:00 -07003018 /* ---- process AEN? --- */
3019 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
3020 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
3021
3022 /* ---- Get DHCP IP Address? --- */
3023 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
3024 qla4xxx_get_dhcp_ip_address(ha);
3025
Mike Christie13483732011-12-01 21:38:41 -06003026 /* ---- relogin device? --- */
3027 if (adapter_up(ha) &&
3028 test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
3029 iscsi_host_for_each_session(ha->host, qla4xxx_dpc_relogin);
3030 }
3031
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05303032 /* ---- link change? --- */
3033 if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
3034 if (!test_bit(AF_LINK_UP, &ha->flags)) {
3035 /* ---- link down? --- */
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07003036 qla4xxx_mark_all_devices_missing(ha);
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05303037 } else {
3038 /* ---- link up? --- *
3039 * F/W will auto login to all devices ONLY ONCE after
3040 * link up during driver initialization and runtime
3041 * fatal error recovery. Therefore, the driver must
3042 * manually relogin to devices when recovering from
3043 * connection failures, logouts, expired KATO, etc. */
Mike Christie13483732011-12-01 21:38:41 -06003044 if (test_and_clear_bit(AF_BUILD_DDB_LIST, &ha->flags)) {
3045 qla4xxx_build_ddb_list(ha, ha->is_reset);
3046 iscsi_host_for_each_session(ha->host,
3047 qla4xxx_login_flash_ddb);
3048 } else
3049 qla4xxx_relogin_all_devices(ha);
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05303050 }
3051 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07003052}
3053
3054/**
3055 * qla4xxx_free_adapter - release the adapter
3056 * @ha: pointer to adapter structure
3057 **/
3058static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
3059{
Sarang Radke8a28896012011-12-06 02:34:10 -08003060 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003061
3062 if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
3063 /* Turn-off interrupts on the card. */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303064 ha->isp_ops->disable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003065 }
3066
David Somayajuluafaf5a22006-09-19 10:28:00 -07003067 /* Remove timer thread, if present */
3068 if (ha->timer_active)
3069 qla4xxx_stop_timer(ha);
3070
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303071 /* Kill the kernel thread for this host */
3072 if (ha->dpc_thread)
3073 destroy_workqueue(ha->dpc_thread);
3074
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003075 /* Kill the kernel thread for this host */
3076 if (ha->task_wq)
3077 destroy_workqueue(ha->task_wq);
3078
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303079 /* Put firmware in known state */
3080 ha->isp_ops->reset_firmware(ha);
3081
3082 if (is_qla8022(ha)) {
3083 qla4_8xxx_idc_lock(ha);
3084 qla4_8xxx_clear_drv_active(ha);
3085 qla4_8xxx_idc_unlock(ha);
3086 }
3087
David Somayajuluafaf5a22006-09-19 10:28:00 -07003088 /* Detach interrupts */
3089 if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303090 qla4xxx_free_irqs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003091
David C Somayajulubee4fe82007-05-23 18:03:32 -07003092 /* free extra memory */
3093 qla4xxx_mem_free(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303094}
David C Somayajulubee4fe82007-05-23 18:03:32 -07003095
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303096int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
3097{
3098 int status = 0;
3099 uint8_t revision_id;
3100 unsigned long mem_base, mem_len, db_base, db_len;
3101 struct pci_dev *pdev = ha->pdev;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003102
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303103 status = pci_request_regions(pdev, DRIVER_NAME);
3104 if (status) {
3105 printk(KERN_WARNING
3106 "scsi(%ld) Failed to reserve PIO regions (%s) "
3107 "status=%d\n", ha->host_no, pci_name(pdev), status);
3108 goto iospace_error_exit;
3109 }
3110
3111 pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
3112 DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
3113 __func__, revision_id));
3114 ha->revision_id = revision_id;
3115
3116 /* remap phys address */
3117 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
3118 mem_len = pci_resource_len(pdev, 0);
3119 DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
3120 __func__, mem_base, mem_len));
3121
3122 /* mapping of pcibase pointer */
3123 ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
3124 if (!ha->nx_pcibase) {
3125 printk(KERN_ERR
3126 "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
3127 pci_release_regions(ha->pdev);
3128 goto iospace_error_exit;
3129 }
3130
3131 /* Mapping of IO base pointer, door bell read and write pointer */
3132
3133 /* mapping of IO base pointer */
3134 ha->qla4_8xxx_reg =
3135 (struct device_reg_82xx __iomem *)((uint8_t *)ha->nx_pcibase +
3136 0xbc000 + (ha->pdev->devfn << 11));
3137
3138 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
3139 db_len = pci_resource_len(pdev, 4);
3140
Shyam Sundar2657c802010-10-06 22:50:29 -07003141 ha->nx_db_wr_ptr = (ha->pdev->devfn == 4 ? QLA82XX_CAM_RAM_DB1 :
3142 QLA82XX_CAM_RAM_DB2);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303143
Shyam Sundar2657c802010-10-06 22:50:29 -07003144 return 0;
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303145iospace_error_exit:
3146 return -ENOMEM;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003147}
3148
3149/***
3150 * qla4xxx_iospace_config - maps registers
3151 * @ha: pointer to adapter structure
3152 *
3153 * This routines maps HBA's registers from the pci address space
3154 * into the kernel virtual address space for memory mapped i/o.
3155 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303156int qla4xxx_iospace_config(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07003157{
3158 unsigned long pio, pio_len, pio_flags;
3159 unsigned long mmio, mmio_len, mmio_flags;
3160
3161 pio = pci_resource_start(ha->pdev, 0);
3162 pio_len = pci_resource_len(ha->pdev, 0);
3163 pio_flags = pci_resource_flags(ha->pdev, 0);
3164 if (pio_flags & IORESOURCE_IO) {
3165 if (pio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303166 ql4_printk(KERN_WARNING, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07003167 "Invalid PCI I/O region size\n");
3168 pio = 0;
3169 }
3170 } else {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303171 ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07003172 pio = 0;
3173 }
3174
3175 /* Use MMIO operations for all accesses. */
3176 mmio = pci_resource_start(ha->pdev, 1);
3177 mmio_len = pci_resource_len(ha->pdev, 1);
3178 mmio_flags = pci_resource_flags(ha->pdev, 1);
3179
3180 if (!(mmio_flags & IORESOURCE_MEM)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303181 ql4_printk(KERN_ERR, ha,
3182 "region #0 not an MMIO resource, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07003183
3184 goto iospace_error_exit;
3185 }
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303186
David Somayajuluafaf5a22006-09-19 10:28:00 -07003187 if (mmio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303188 ql4_printk(KERN_ERR, ha,
3189 "Invalid PCI mem region size, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07003190 goto iospace_error_exit;
3191 }
3192
3193 if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303194 ql4_printk(KERN_WARNING, ha,
3195 "Failed to reserve PIO/MMIO regions\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07003196
3197 goto iospace_error_exit;
3198 }
3199
3200 ha->pio_address = pio;
3201 ha->pio_length = pio_len;
3202 ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
3203 if (!ha->reg) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303204 ql4_printk(KERN_ERR, ha,
3205 "cannot remap MMIO, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07003206
3207 goto iospace_error_exit;
3208 }
3209
3210 return 0;
3211
3212iospace_error_exit:
3213 return -ENOMEM;
3214}
3215
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303216static struct isp_operations qla4xxx_isp_ops = {
3217 .iospace_config = qla4xxx_iospace_config,
3218 .pci_config = qla4xxx_pci_config,
3219 .disable_intrs = qla4xxx_disable_intrs,
3220 .enable_intrs = qla4xxx_enable_intrs,
3221 .start_firmware = qla4xxx_start_firmware,
3222 .intr_handler = qla4xxx_intr_handler,
3223 .interrupt_service_routine = qla4xxx_interrupt_service_routine,
3224 .reset_chip = qla4xxx_soft_reset,
3225 .reset_firmware = qla4xxx_hw_reset,
3226 .queue_iocb = qla4xxx_queue_iocb,
3227 .complete_iocb = qla4xxx_complete_iocb,
3228 .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
3229 .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
3230 .get_sys_info = qla4xxx_get_sys_info,
3231};
3232
3233static struct isp_operations qla4_8xxx_isp_ops = {
3234 .iospace_config = qla4_8xxx_iospace_config,
3235 .pci_config = qla4_8xxx_pci_config,
3236 .disable_intrs = qla4_8xxx_disable_intrs,
3237 .enable_intrs = qla4_8xxx_enable_intrs,
3238 .start_firmware = qla4_8xxx_load_risc,
3239 .intr_handler = qla4_8xxx_intr_handler,
3240 .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
3241 .reset_chip = qla4_8xxx_isp_reset,
3242 .reset_firmware = qla4_8xxx_stop_firmware,
3243 .queue_iocb = qla4_8xxx_queue_iocb,
3244 .complete_iocb = qla4_8xxx_complete_iocb,
3245 .rd_shdw_req_q_out = qla4_8xxx_rd_shdw_req_q_out,
3246 .rd_shdw_rsp_q_in = qla4_8xxx_rd_shdw_rsp_q_in,
3247 .get_sys_info = qla4_8xxx_get_sys_info,
3248};
3249
3250uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
3251{
3252 return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
3253}
3254
3255uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
3256{
3257 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
3258}
3259
3260uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
3261{
3262 return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
3263}
3264
3265uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
3266{
3267 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
3268}
3269
Manish Rangankar2a991c22011-07-25 13:48:55 -05003270static ssize_t qla4xxx_show_boot_eth_info(void *data, int type, char *buf)
3271{
3272 struct scsi_qla_host *ha = data;
3273 char *str = buf;
3274 int rc;
3275
3276 switch (type) {
3277 case ISCSI_BOOT_ETH_FLAGS:
3278 rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
3279 break;
3280 case ISCSI_BOOT_ETH_INDEX:
3281 rc = sprintf(str, "0\n");
3282 break;
3283 case ISCSI_BOOT_ETH_MAC:
3284 rc = sysfs_format_mac(str, ha->my_mac,
3285 MAC_ADDR_LEN);
3286 break;
3287 default:
3288 rc = -ENOSYS;
3289 break;
3290 }
3291 return rc;
3292}
3293
Al Viro587a1f12011-07-23 23:11:19 -04003294static umode_t qla4xxx_eth_get_attr_visibility(void *data, int type)
Manish Rangankar2a991c22011-07-25 13:48:55 -05003295{
3296 int rc;
3297
3298 switch (type) {
3299 case ISCSI_BOOT_ETH_FLAGS:
3300 case ISCSI_BOOT_ETH_MAC:
3301 case ISCSI_BOOT_ETH_INDEX:
3302 rc = S_IRUGO;
3303 break;
3304 default:
3305 rc = 0;
3306 break;
3307 }
3308 return rc;
3309}
3310
3311static ssize_t qla4xxx_show_boot_ini_info(void *data, int type, char *buf)
3312{
3313 struct scsi_qla_host *ha = data;
3314 char *str = buf;
3315 int rc;
3316
3317 switch (type) {
3318 case ISCSI_BOOT_INI_INITIATOR_NAME:
3319 rc = sprintf(str, "%s\n", ha->name_string);
3320 break;
3321 default:
3322 rc = -ENOSYS;
3323 break;
3324 }
3325 return rc;
3326}
3327
Al Viro587a1f12011-07-23 23:11:19 -04003328static umode_t qla4xxx_ini_get_attr_visibility(void *data, int type)
Manish Rangankar2a991c22011-07-25 13:48:55 -05003329{
3330 int rc;
3331
3332 switch (type) {
3333 case ISCSI_BOOT_INI_INITIATOR_NAME:
3334 rc = S_IRUGO;
3335 break;
3336 default:
3337 rc = 0;
3338 break;
3339 }
3340 return rc;
3341}
3342
3343static ssize_t
3344qla4xxx_show_boot_tgt_info(struct ql4_boot_session_info *boot_sess, int type,
3345 char *buf)
3346{
3347 struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
3348 char *str = buf;
3349 int rc;
3350
3351 switch (type) {
3352 case ISCSI_BOOT_TGT_NAME:
3353 rc = sprintf(buf, "%s\n", (char *)&boot_sess->target_name);
3354 break;
3355 case ISCSI_BOOT_TGT_IP_ADDR:
3356 if (boot_sess->conn_list[0].dest_ipaddr.ip_type == 0x1)
3357 rc = sprintf(buf, "%pI4\n",
3358 &boot_conn->dest_ipaddr.ip_address);
3359 else
3360 rc = sprintf(str, "%pI6\n",
3361 &boot_conn->dest_ipaddr.ip_address);
3362 break;
3363 case ISCSI_BOOT_TGT_PORT:
3364 rc = sprintf(str, "%d\n", boot_conn->dest_port);
3365 break;
3366 case ISCSI_BOOT_TGT_CHAP_NAME:
3367 rc = sprintf(str, "%.*s\n",
3368 boot_conn->chap.target_chap_name_length,
3369 (char *)&boot_conn->chap.target_chap_name);
3370 break;
3371 case ISCSI_BOOT_TGT_CHAP_SECRET:
3372 rc = sprintf(str, "%.*s\n",
3373 boot_conn->chap.target_secret_length,
3374 (char *)&boot_conn->chap.target_secret);
3375 break;
3376 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
3377 rc = sprintf(str, "%.*s\n",
3378 boot_conn->chap.intr_chap_name_length,
3379 (char *)&boot_conn->chap.intr_chap_name);
3380 break;
3381 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
3382 rc = sprintf(str, "%.*s\n",
3383 boot_conn->chap.intr_secret_length,
3384 (char *)&boot_conn->chap.intr_secret);
3385 break;
3386 case ISCSI_BOOT_TGT_FLAGS:
3387 rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
3388 break;
3389 case ISCSI_BOOT_TGT_NIC_ASSOC:
3390 rc = sprintf(str, "0\n");
3391 break;
3392 default:
3393 rc = -ENOSYS;
3394 break;
3395 }
3396 return rc;
3397}
3398
3399static ssize_t qla4xxx_show_boot_tgt_pri_info(void *data, int type, char *buf)
3400{
3401 struct scsi_qla_host *ha = data;
3402 struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_pri_sess);
3403
3404 return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
3405}
3406
3407static ssize_t qla4xxx_show_boot_tgt_sec_info(void *data, int type, char *buf)
3408{
3409 struct scsi_qla_host *ha = data;
3410 struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_sec_sess);
3411
3412 return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
3413}
3414
Al Viro587a1f12011-07-23 23:11:19 -04003415static umode_t qla4xxx_tgt_get_attr_visibility(void *data, int type)
Manish Rangankar2a991c22011-07-25 13:48:55 -05003416{
3417 int rc;
3418
3419 switch (type) {
3420 case ISCSI_BOOT_TGT_NAME:
3421 case ISCSI_BOOT_TGT_IP_ADDR:
3422 case ISCSI_BOOT_TGT_PORT:
3423 case ISCSI_BOOT_TGT_CHAP_NAME:
3424 case ISCSI_BOOT_TGT_CHAP_SECRET:
3425 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
3426 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
3427 case ISCSI_BOOT_TGT_NIC_ASSOC:
3428 case ISCSI_BOOT_TGT_FLAGS:
3429 rc = S_IRUGO;
3430 break;
3431 default:
3432 rc = 0;
3433 break;
3434 }
3435 return rc;
3436}
3437
3438static void qla4xxx_boot_release(void *data)
3439{
3440 struct scsi_qla_host *ha = data;
3441
3442 scsi_host_put(ha->host);
3443}
3444
3445static int get_fw_boot_info(struct scsi_qla_host *ha, uint16_t ddb_index[])
3446{
3447 dma_addr_t buf_dma;
3448 uint32_t addr, pri_addr, sec_addr;
3449 uint32_t offset;
3450 uint16_t func_num;
3451 uint8_t val;
3452 uint8_t *buf = NULL;
3453 size_t size = 13 * sizeof(uint8_t);
3454 int ret = QLA_SUCCESS;
3455
3456 func_num = PCI_FUNC(ha->pdev->devfn);
3457
Manish Rangankar0d5b36b2011-10-07 16:55:51 -07003458 ql4_printk(KERN_INFO, ha, "%s: Get FW boot info for 0x%x func %d\n",
3459 __func__, ha->pdev->device, func_num);
Manish Rangankar2a991c22011-07-25 13:48:55 -05003460
Manish Rangankar0d5b36b2011-10-07 16:55:51 -07003461 if (is_qla40XX(ha)) {
Manish Rangankar2a991c22011-07-25 13:48:55 -05003462 if (func_num == 1) {
3463 addr = NVRAM_PORT0_BOOT_MODE;
3464 pri_addr = NVRAM_PORT0_BOOT_PRI_TGT;
3465 sec_addr = NVRAM_PORT0_BOOT_SEC_TGT;
3466 } else if (func_num == 3) {
3467 addr = NVRAM_PORT1_BOOT_MODE;
3468 pri_addr = NVRAM_PORT1_BOOT_PRI_TGT;
3469 sec_addr = NVRAM_PORT1_BOOT_SEC_TGT;
3470 } else {
3471 ret = QLA_ERROR;
3472 goto exit_boot_info;
3473 }
3474
3475 /* Check Boot Mode */
3476 val = rd_nvram_byte(ha, addr);
3477 if (!(val & 0x07)) {
Manish Rangankare8fb00e2012-02-13 18:30:43 +05303478 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Adapter boot "
3479 "options : 0x%x\n", __func__, val));
Manish Rangankar2a991c22011-07-25 13:48:55 -05003480 ret = QLA_ERROR;
3481 goto exit_boot_info;
3482 }
3483
3484 /* get primary valid target index */
3485 val = rd_nvram_byte(ha, pri_addr);
3486 if (val & BIT_7)
3487 ddb_index[0] = (val & 0x7f);
Manish Rangankar2a991c22011-07-25 13:48:55 -05003488
3489 /* get secondary valid target index */
3490 val = rd_nvram_byte(ha, sec_addr);
3491 if (val & BIT_7)
3492 ddb_index[1] = (val & 0x7f);
Manish Rangankar2a991c22011-07-25 13:48:55 -05003493
3494 } else if (is_qla8022(ha)) {
3495 buf = dma_alloc_coherent(&ha->pdev->dev, size,
3496 &buf_dma, GFP_KERNEL);
3497 if (!buf) {
3498 DEBUG2(ql4_printk(KERN_ERR, ha,
3499 "%s: Unable to allocate dma buffer\n",
3500 __func__));
3501 ret = QLA_ERROR;
3502 goto exit_boot_info;
3503 }
3504
3505 if (ha->port_num == 0)
3506 offset = BOOT_PARAM_OFFSET_PORT0;
3507 else if (ha->port_num == 1)
3508 offset = BOOT_PARAM_OFFSET_PORT1;
3509 else {
3510 ret = QLA_ERROR;
3511 goto exit_boot_info_free;
3512 }
3513 addr = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_iscsi_param * 4) +
3514 offset;
3515 if (qla4xxx_get_flash(ha, buf_dma, addr,
3516 13 * sizeof(uint8_t)) != QLA_SUCCESS) {
3517 DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: Get Flash"
3518 "failed\n", ha->host_no, __func__));
3519 ret = QLA_ERROR;
3520 goto exit_boot_info_free;
3521 }
3522 /* Check Boot Mode */
3523 if (!(buf[1] & 0x07)) {
Manish Rangankare8fb00e2012-02-13 18:30:43 +05303524 DEBUG2(ql4_printk(KERN_INFO, ha, "Firmware boot options"
3525 " : 0x%x\n", buf[1]));
Manish Rangankar2a991c22011-07-25 13:48:55 -05003526 ret = QLA_ERROR;
3527 goto exit_boot_info_free;
3528 }
3529
3530 /* get primary valid target index */
3531 if (buf[2] & BIT_7)
3532 ddb_index[0] = buf[2] & 0x7f;
Manish Rangankar2a991c22011-07-25 13:48:55 -05003533
3534 /* get secondary valid target index */
3535 if (buf[11] & BIT_7)
3536 ddb_index[1] = buf[11] & 0x7f;
Manish Rangankar2a991c22011-07-25 13:48:55 -05003537 } else {
3538 ret = QLA_ERROR;
3539 goto exit_boot_info;
3540 }
3541
3542 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Primary target ID %d, Secondary"
3543 " target ID %d\n", __func__, ddb_index[0],
3544 ddb_index[1]));
3545
3546exit_boot_info_free:
3547 dma_free_coherent(&ha->pdev->dev, size, buf, buf_dma);
3548exit_boot_info:
Lalit Chandivade20e835b2012-02-13 18:30:42 +05303549 ha->pri_ddb_idx = ddb_index[0];
3550 ha->sec_ddb_idx = ddb_index[1];
Manish Rangankar2a991c22011-07-25 13:48:55 -05003551 return ret;
3552}
3553
Lalit Chandivade28deb452011-10-07 16:55:44 -07003554/**
3555 * qla4xxx_get_bidi_chap - Get a BIDI CHAP user and password
3556 * @ha: pointer to adapter structure
3557 * @username: CHAP username to be returned
3558 * @password: CHAP password to be returned
3559 *
3560 * If a boot entry has BIDI CHAP enabled then we need to set the BIDI CHAP
3561 * user and password in the sysfs entry in /sys/firmware/iscsi_boot#/.
3562 * So from the CHAP cache find the first BIDI CHAP entry and set it
3563 * to the boot record in sysfs.
3564 **/
3565static int qla4xxx_get_bidi_chap(struct scsi_qla_host *ha, char *username,
3566 char *password)
3567{
3568 int i, ret = -EINVAL;
3569 int max_chap_entries = 0;
3570 struct ql4_chap_table *chap_table;
3571
3572 if (is_qla8022(ha))
3573 max_chap_entries = (ha->hw.flt_chap_size / 2) /
3574 sizeof(struct ql4_chap_table);
3575 else
3576 max_chap_entries = MAX_CHAP_ENTRIES_40XX;
3577
3578 if (!ha->chap_list) {
3579 ql4_printk(KERN_ERR, ha, "Do not have CHAP table cache\n");
3580 return ret;
3581 }
3582
3583 mutex_lock(&ha->chap_sem);
3584 for (i = 0; i < max_chap_entries; i++) {
3585 chap_table = (struct ql4_chap_table *)ha->chap_list + i;
3586 if (chap_table->cookie !=
3587 __constant_cpu_to_le16(CHAP_VALID_COOKIE)) {
3588 continue;
3589 }
3590
3591 if (chap_table->flags & BIT_7) /* local */
3592 continue;
3593
3594 if (!(chap_table->flags & BIT_6)) /* Not BIDI */
3595 continue;
3596
3597 strncpy(password, chap_table->secret, QL4_CHAP_MAX_SECRET_LEN);
3598 strncpy(username, chap_table->name, QL4_CHAP_MAX_NAME_LEN);
3599 ret = 0;
3600 break;
3601 }
3602 mutex_unlock(&ha->chap_sem);
3603
3604 return ret;
3605}
3606
3607
Manish Rangankar2a991c22011-07-25 13:48:55 -05003608static int qla4xxx_get_boot_target(struct scsi_qla_host *ha,
3609 struct ql4_boot_session_info *boot_sess,
3610 uint16_t ddb_index)
3611{
3612 struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
3613 struct dev_db_entry *fw_ddb_entry;
3614 dma_addr_t fw_ddb_entry_dma;
3615 uint16_t idx;
3616 uint16_t options;
3617 int ret = QLA_SUCCESS;
3618
3619 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3620 &fw_ddb_entry_dma, GFP_KERNEL);
3621 if (!fw_ddb_entry) {
3622 DEBUG2(ql4_printk(KERN_ERR, ha,
3623 "%s: Unable to allocate dma buffer.\n",
3624 __func__));
3625 ret = QLA_ERROR;
3626 return ret;
3627 }
3628
3629 if (qla4xxx_bootdb_by_index(ha, fw_ddb_entry,
3630 fw_ddb_entry_dma, ddb_index)) {
Manish Rangankare8fb00e2012-02-13 18:30:43 +05303631 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: No Flash DDB found at "
3632 "index [%d]\n", __func__, ddb_index));
Manish Rangankar2a991c22011-07-25 13:48:55 -05003633 ret = QLA_ERROR;
3634 goto exit_boot_target;
3635 }
3636
3637 /* Update target name and IP from DDB */
3638 memcpy(boot_sess->target_name, fw_ddb_entry->iscsi_name,
3639 min(sizeof(boot_sess->target_name),
3640 sizeof(fw_ddb_entry->iscsi_name)));
3641
3642 options = le16_to_cpu(fw_ddb_entry->options);
3643 if (options & DDB_OPT_IPV6_DEVICE) {
3644 memcpy(&boot_conn->dest_ipaddr.ip_address,
3645 &fw_ddb_entry->ip_addr[0], IPv6_ADDR_LEN);
3646 } else {
3647 boot_conn->dest_ipaddr.ip_type = 0x1;
3648 memcpy(&boot_conn->dest_ipaddr.ip_address,
3649 &fw_ddb_entry->ip_addr[0], IP_ADDR_LEN);
3650 }
3651
3652 boot_conn->dest_port = le16_to_cpu(fw_ddb_entry->port);
3653
3654 /* update chap information */
3655 idx = __le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
3656
3657 if (BIT_7 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
3658
3659 DEBUG2(ql4_printk(KERN_INFO, ha, "Setting chap\n"));
3660
3661 ret = qla4xxx_get_chap(ha, (char *)&boot_conn->chap.
3662 target_chap_name,
3663 (char *)&boot_conn->chap.target_secret,
3664 idx);
3665 if (ret) {
3666 ql4_printk(KERN_ERR, ha, "Failed to set chap\n");
3667 ret = QLA_ERROR;
3668 goto exit_boot_target;
3669 }
3670
3671 boot_conn->chap.target_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
3672 boot_conn->chap.target_secret_length = QL4_CHAP_MAX_SECRET_LEN;
3673 }
3674
3675 if (BIT_4 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
3676
3677 DEBUG2(ql4_printk(KERN_INFO, ha, "Setting BIDI chap\n"));
3678
Lalit Chandivade28deb452011-10-07 16:55:44 -07003679 ret = qla4xxx_get_bidi_chap(ha,
3680 (char *)&boot_conn->chap.intr_chap_name,
3681 (char *)&boot_conn->chap.intr_secret);
3682
Manish Rangankar2a991c22011-07-25 13:48:55 -05003683 if (ret) {
3684 ql4_printk(KERN_ERR, ha, "Failed to set BIDI chap\n");
3685 ret = QLA_ERROR;
3686 goto exit_boot_target;
3687 }
3688
3689 boot_conn->chap.intr_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
3690 boot_conn->chap.intr_secret_length = QL4_CHAP_MAX_SECRET_LEN;
3691 }
3692
3693exit_boot_target:
3694 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3695 fw_ddb_entry, fw_ddb_entry_dma);
3696 return ret;
3697}
3698
3699static int qla4xxx_get_boot_info(struct scsi_qla_host *ha)
3700{
3701 uint16_t ddb_index[2];
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003702 int ret = QLA_ERROR;
3703 int rval;
Manish Rangankar2a991c22011-07-25 13:48:55 -05003704
3705 memset(ddb_index, 0, sizeof(ddb_index));
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003706 ddb_index[0] = 0xffff;
3707 ddb_index[1] = 0xffff;
Manish Rangankar2a991c22011-07-25 13:48:55 -05003708 ret = get_fw_boot_info(ha, ddb_index);
3709 if (ret != QLA_SUCCESS) {
Manish Rangankare8fb00e2012-02-13 18:30:43 +05303710 DEBUG2(ql4_printk(KERN_INFO, ha,
3711 "%s: No boot target configured.\n", __func__));
Manish Rangankar2a991c22011-07-25 13:48:55 -05003712 return ret;
3713 }
3714
Mike Christie13483732011-12-01 21:38:41 -06003715 if (ql4xdisablesysfsboot)
3716 return QLA_SUCCESS;
3717
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003718 if (ddb_index[0] == 0xffff)
3719 goto sec_target;
3720
3721 rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_pri_sess),
Manish Rangankar2a991c22011-07-25 13:48:55 -05003722 ddb_index[0]);
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003723 if (rval != QLA_SUCCESS) {
Manish Rangankare8fb00e2012-02-13 18:30:43 +05303724 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Primary boot target not "
3725 "configured\n", __func__));
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003726 } else
3727 ret = QLA_SUCCESS;
Manish Rangankar2a991c22011-07-25 13:48:55 -05003728
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003729sec_target:
3730 if (ddb_index[1] == 0xffff)
3731 goto exit_get_boot_info;
3732
3733 rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_sec_sess),
Manish Rangankar2a991c22011-07-25 13:48:55 -05003734 ddb_index[1]);
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003735 if (rval != QLA_SUCCESS) {
Manish Rangankare8fb00e2012-02-13 18:30:43 +05303736 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Secondary boot target not"
3737 " configured\n", __func__));
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003738 } else
3739 ret = QLA_SUCCESS;
3740
3741exit_get_boot_info:
Manish Rangankar2a991c22011-07-25 13:48:55 -05003742 return ret;
3743}
3744
3745static int qla4xxx_setup_boot_info(struct scsi_qla_host *ha)
3746{
3747 struct iscsi_boot_kobj *boot_kobj;
3748
3749 if (qla4xxx_get_boot_info(ha) != QLA_SUCCESS)
Mike Christie13483732011-12-01 21:38:41 -06003750 return QLA_ERROR;
3751
3752 if (ql4xdisablesysfsboot) {
3753 ql4_printk(KERN_INFO, ha,
3754 "%s: syfsboot disabled - driver will trigger login"
3755 "and publish session for discovery .\n", __func__);
3756 return QLA_SUCCESS;
3757 }
3758
Manish Rangankar2a991c22011-07-25 13:48:55 -05003759
3760 ha->boot_kset = iscsi_boot_create_host_kset(ha->host->host_no);
3761 if (!ha->boot_kset)
3762 goto kset_free;
3763
3764 if (!scsi_host_get(ha->host))
3765 goto kset_free;
3766 boot_kobj = iscsi_boot_create_target(ha->boot_kset, 0, ha,
3767 qla4xxx_show_boot_tgt_pri_info,
3768 qla4xxx_tgt_get_attr_visibility,
3769 qla4xxx_boot_release);
3770 if (!boot_kobj)
3771 goto put_host;
3772
3773 if (!scsi_host_get(ha->host))
3774 goto kset_free;
3775 boot_kobj = iscsi_boot_create_target(ha->boot_kset, 1, ha,
3776 qla4xxx_show_boot_tgt_sec_info,
3777 qla4xxx_tgt_get_attr_visibility,
3778 qla4xxx_boot_release);
3779 if (!boot_kobj)
3780 goto put_host;
3781
3782 if (!scsi_host_get(ha->host))
3783 goto kset_free;
3784 boot_kobj = iscsi_boot_create_initiator(ha->boot_kset, 0, ha,
3785 qla4xxx_show_boot_ini_info,
3786 qla4xxx_ini_get_attr_visibility,
3787 qla4xxx_boot_release);
3788 if (!boot_kobj)
3789 goto put_host;
3790
3791 if (!scsi_host_get(ha->host))
3792 goto kset_free;
3793 boot_kobj = iscsi_boot_create_ethernet(ha->boot_kset, 0, ha,
3794 qla4xxx_show_boot_eth_info,
3795 qla4xxx_eth_get_attr_visibility,
3796 qla4xxx_boot_release);
3797 if (!boot_kobj)
3798 goto put_host;
3799
Mike Christie13483732011-12-01 21:38:41 -06003800 return QLA_SUCCESS;
Manish Rangankar2a991c22011-07-25 13:48:55 -05003801
3802put_host:
3803 scsi_host_put(ha->host);
3804kset_free:
3805 iscsi_boot_destroy_kset(ha->boot_kset);
3806 return -ENOMEM;
3807}
3808
Lalit Chandivade45494152011-10-07 16:55:42 -07003809
3810/**
3811 * qla4xxx_create chap_list - Create CHAP list from FLASH
3812 * @ha: pointer to adapter structure
3813 *
3814 * Read flash and make a list of CHAP entries, during login when a CHAP entry
3815 * is received, it will be checked in this list. If entry exist then the CHAP
3816 * entry index is set in the DDB. If CHAP entry does not exist in this list
3817 * then a new entry is added in FLASH in CHAP table and the index obtained is
3818 * used in the DDB.
3819 **/
3820static void qla4xxx_create_chap_list(struct scsi_qla_host *ha)
3821{
3822 int rval = 0;
3823 uint8_t *chap_flash_data = NULL;
3824 uint32_t offset;
3825 dma_addr_t chap_dma;
3826 uint32_t chap_size = 0;
3827
3828 if (is_qla40XX(ha))
3829 chap_size = MAX_CHAP_ENTRIES_40XX *
3830 sizeof(struct ql4_chap_table);
3831 else /* Single region contains CHAP info for both
3832 * ports which is divided into half for each port.
3833 */
3834 chap_size = ha->hw.flt_chap_size / 2;
3835
3836 chap_flash_data = dma_alloc_coherent(&ha->pdev->dev, chap_size,
3837 &chap_dma, GFP_KERNEL);
3838 if (!chap_flash_data) {
3839 ql4_printk(KERN_ERR, ha, "No memory for chap_flash_data\n");
3840 return;
3841 }
3842 if (is_qla40XX(ha))
3843 offset = FLASH_CHAP_OFFSET;
3844 else {
3845 offset = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_region_chap << 2);
3846 if (ha->port_num == 1)
3847 offset += chap_size;
3848 }
3849
3850 rval = qla4xxx_get_flash(ha, chap_dma, offset, chap_size);
3851 if (rval != QLA_SUCCESS)
3852 goto exit_chap_list;
3853
3854 if (ha->chap_list == NULL)
3855 ha->chap_list = vmalloc(chap_size);
3856 if (ha->chap_list == NULL) {
3857 ql4_printk(KERN_ERR, ha, "No memory for ha->chap_list\n");
3858 goto exit_chap_list;
3859 }
3860
3861 memcpy(ha->chap_list, chap_flash_data, chap_size);
3862
3863exit_chap_list:
3864 dma_free_coherent(&ha->pdev->dev, chap_size,
3865 chap_flash_data, chap_dma);
Lalit Chandivade45494152011-10-07 16:55:42 -07003866}
3867
Mike Christie13483732011-12-01 21:38:41 -06003868static void qla4xxx_get_param_ddb(struct ddb_entry *ddb_entry,
3869 struct ql4_tuple_ddb *tddb)
3870{
3871 struct scsi_qla_host *ha;
3872 struct iscsi_cls_session *cls_sess;
3873 struct iscsi_cls_conn *cls_conn;
3874 struct iscsi_session *sess;
3875 struct iscsi_conn *conn;
3876
3877 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
3878 ha = ddb_entry->ha;
3879 cls_sess = ddb_entry->sess;
3880 sess = cls_sess->dd_data;
3881 cls_conn = ddb_entry->conn;
3882 conn = cls_conn->dd_data;
3883
3884 tddb->tpgt = sess->tpgt;
3885 tddb->port = conn->persistent_port;
3886 strncpy(tddb->iscsi_name, sess->targetname, ISCSI_NAME_SIZE);
3887 strncpy(tddb->ip_addr, conn->persistent_address, DDB_IPADDR_LEN);
3888}
3889
3890static void qla4xxx_convert_param_ddb(struct dev_db_entry *fw_ddb_entry,
3891 struct ql4_tuple_ddb *tddb)
3892{
3893 uint16_t options = 0;
3894
3895 tddb->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
3896 memcpy(&tddb->iscsi_name[0], &fw_ddb_entry->iscsi_name[0],
3897 min(sizeof(tddb->iscsi_name), sizeof(fw_ddb_entry->iscsi_name)));
3898
3899 options = le16_to_cpu(fw_ddb_entry->options);
3900 if (options & DDB_OPT_IPV6_DEVICE)
3901 sprintf(tddb->ip_addr, "%pI6", fw_ddb_entry->ip_addr);
3902 else
3903 sprintf(tddb->ip_addr, "%pI4", fw_ddb_entry->ip_addr);
3904
3905 tddb->port = le16_to_cpu(fw_ddb_entry->port);
3906}
3907
3908static int qla4xxx_compare_tuple_ddb(struct scsi_qla_host *ha,
3909 struct ql4_tuple_ddb *old_tddb,
3910 struct ql4_tuple_ddb *new_tddb)
3911{
3912 if (strcmp(old_tddb->iscsi_name, new_tddb->iscsi_name))
3913 return QLA_ERROR;
3914
3915 if (strcmp(old_tddb->ip_addr, new_tddb->ip_addr))
3916 return QLA_ERROR;
3917
3918 if (old_tddb->port != new_tddb->port)
3919 return QLA_ERROR;
3920
3921 DEBUG2(ql4_printk(KERN_INFO, ha,
3922 "Match Found, fw[%d,%d,%s,%s], [%d,%d,%s,%s]",
3923 old_tddb->port, old_tddb->tpgt, old_tddb->ip_addr,
3924 old_tddb->iscsi_name, new_tddb->port, new_tddb->tpgt,
3925 new_tddb->ip_addr, new_tddb->iscsi_name));
3926
3927 return QLA_SUCCESS;
3928}
3929
3930static int qla4xxx_is_session_exists(struct scsi_qla_host *ha,
3931 struct dev_db_entry *fw_ddb_entry)
3932{
3933 struct ddb_entry *ddb_entry;
3934 struct ql4_tuple_ddb *fw_tddb = NULL;
3935 struct ql4_tuple_ddb *tmp_tddb = NULL;
3936 int idx;
3937 int ret = QLA_ERROR;
3938
3939 fw_tddb = vzalloc(sizeof(*fw_tddb));
3940 if (!fw_tddb) {
3941 DEBUG2(ql4_printk(KERN_WARNING, ha,
3942 "Memory Allocation failed.\n"));
3943 ret = QLA_SUCCESS;
3944 goto exit_check;
3945 }
3946
3947 tmp_tddb = vzalloc(sizeof(*tmp_tddb));
3948 if (!tmp_tddb) {
3949 DEBUG2(ql4_printk(KERN_WARNING, ha,
3950 "Memory Allocation failed.\n"));
3951 ret = QLA_SUCCESS;
3952 goto exit_check;
3953 }
3954
3955 qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb);
3956
3957 for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
3958 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
3959 if (ddb_entry == NULL)
3960 continue;
3961
3962 qla4xxx_get_param_ddb(ddb_entry, tmp_tddb);
3963 if (!qla4xxx_compare_tuple_ddb(ha, fw_tddb, tmp_tddb)) {
3964 ret = QLA_SUCCESS; /* found */
3965 goto exit_check;
3966 }
3967 }
3968
3969exit_check:
3970 if (fw_tddb)
3971 vfree(fw_tddb);
3972 if (tmp_tddb)
3973 vfree(tmp_tddb);
3974 return ret;
3975}
3976
3977static int qla4xxx_is_flash_ddb_exists(struct scsi_qla_host *ha,
3978 struct list_head *list_nt,
3979 struct dev_db_entry *fw_ddb_entry)
3980{
3981 struct qla_ddb_index *nt_ddb_idx, *nt_ddb_idx_tmp;
3982 struct ql4_tuple_ddb *fw_tddb = NULL;
3983 struct ql4_tuple_ddb *tmp_tddb = NULL;
3984 int ret = QLA_ERROR;
3985
3986 fw_tddb = vzalloc(sizeof(*fw_tddb));
3987 if (!fw_tddb) {
3988 DEBUG2(ql4_printk(KERN_WARNING, ha,
3989 "Memory Allocation failed.\n"));
3990 ret = QLA_SUCCESS;
3991 goto exit_check;
3992 }
3993
3994 tmp_tddb = vzalloc(sizeof(*tmp_tddb));
3995 if (!tmp_tddb) {
3996 DEBUG2(ql4_printk(KERN_WARNING, ha,
3997 "Memory Allocation failed.\n"));
3998 ret = QLA_SUCCESS;
3999 goto exit_check;
4000 }
4001
4002 qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb);
4003
4004 list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
4005 qla4xxx_convert_param_ddb(&nt_ddb_idx->fw_ddb, tmp_tddb);
4006 if (!qla4xxx_compare_tuple_ddb(ha, fw_tddb, tmp_tddb)) {
4007 ret = QLA_SUCCESS; /* found */
4008 goto exit_check;
4009 }
4010 }
4011
4012exit_check:
4013 if (fw_tddb)
4014 vfree(fw_tddb);
4015 if (tmp_tddb)
4016 vfree(tmp_tddb);
4017 return ret;
4018}
4019
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004020static void qla4xxx_free_ddb_list(struct list_head *list_ddb)
Mike Christie13483732011-12-01 21:38:41 -06004021{
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004022 struct qla_ddb_index *ddb_idx, *ddb_idx_tmp;
Mike Christie13483732011-12-01 21:38:41 -06004023
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004024 list_for_each_entry_safe(ddb_idx, ddb_idx_tmp, list_ddb, list) {
4025 list_del_init(&ddb_idx->list);
4026 vfree(ddb_idx);
Mike Christie13483732011-12-01 21:38:41 -06004027 }
Mike Christie13483732011-12-01 21:38:41 -06004028}
4029
4030static struct iscsi_endpoint *qla4xxx_get_ep_fwdb(struct scsi_qla_host *ha,
4031 struct dev_db_entry *fw_ddb_entry)
4032{
4033 struct iscsi_endpoint *ep;
4034 struct sockaddr_in *addr;
4035 struct sockaddr_in6 *addr6;
4036 struct sockaddr *dst_addr;
4037 char *ip;
4038
4039 /* TODO: need to destroy on unload iscsi_endpoint*/
4040 dst_addr = vmalloc(sizeof(*dst_addr));
4041 if (!dst_addr)
4042 return NULL;
4043
4044 if (fw_ddb_entry->options & DDB_OPT_IPV6_DEVICE) {
4045 dst_addr->sa_family = AF_INET6;
4046 addr6 = (struct sockaddr_in6 *)dst_addr;
4047 ip = (char *)&addr6->sin6_addr;
4048 memcpy(ip, fw_ddb_entry->ip_addr, IPv6_ADDR_LEN);
4049 addr6->sin6_port = htons(le16_to_cpu(fw_ddb_entry->port));
4050
4051 } else {
4052 dst_addr->sa_family = AF_INET;
4053 addr = (struct sockaddr_in *)dst_addr;
4054 ip = (char *)&addr->sin_addr;
4055 memcpy(ip, fw_ddb_entry->ip_addr, IP_ADDR_LEN);
4056 addr->sin_port = htons(le16_to_cpu(fw_ddb_entry->port));
4057 }
4058
4059 ep = qla4xxx_ep_connect(ha->host, dst_addr, 0);
4060 vfree(dst_addr);
4061 return ep;
4062}
4063
4064static int qla4xxx_verify_boot_idx(struct scsi_qla_host *ha, uint16_t idx)
4065{
4066 if (ql4xdisablesysfsboot)
4067 return QLA_SUCCESS;
4068 if (idx == ha->pri_ddb_idx || idx == ha->sec_ddb_idx)
4069 return QLA_ERROR;
4070 return QLA_SUCCESS;
4071}
4072
4073static void qla4xxx_setup_flash_ddb_entry(struct scsi_qla_host *ha,
4074 struct ddb_entry *ddb_entry)
4075{
Nilesh Javalic28eaac2011-12-18 21:40:44 -08004076 uint16_t def_timeout;
4077
Mike Christie13483732011-12-01 21:38:41 -06004078 ddb_entry->ddb_type = FLASH_DDB;
4079 ddb_entry->fw_ddb_index = INVALID_ENTRY;
4080 ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
4081 ddb_entry->ha = ha;
4082 ddb_entry->unblock_sess = qla4xxx_unblock_flash_ddb;
4083 ddb_entry->ddb_change = qla4xxx_flash_ddb_change;
4084
4085 atomic_set(&ddb_entry->retry_relogin_timer, INVALID_ENTRY);
4086 atomic_set(&ddb_entry->relogin_timer, 0);
4087 atomic_set(&ddb_entry->relogin_retry_count, 0);
Nilesh Javalic28eaac2011-12-18 21:40:44 -08004088 def_timeout = le16_to_cpu(ddb_entry->fw_ddb_entry.def_timeout);
Mike Christie13483732011-12-01 21:38:41 -06004089 ddb_entry->default_relogin_timeout =
Nilesh Javalic28eaac2011-12-18 21:40:44 -08004090 (def_timeout > LOGIN_TOV) && (def_timeout < LOGIN_TOV * 10) ?
4091 def_timeout : LOGIN_TOV;
Mike Christie13483732011-12-01 21:38:41 -06004092 ddb_entry->default_time2wait =
4093 le16_to_cpu(ddb_entry->fw_ddb_entry.iscsi_def_time2wait);
4094}
4095
4096static void qla4xxx_wait_for_ip_configuration(struct scsi_qla_host *ha)
4097{
4098 uint32_t idx = 0;
4099 uint32_t ip_idx[IP_ADDR_COUNT] = {0, 1, 2, 3}; /* 4 IP interfaces */
4100 uint32_t sts[MBOX_REG_COUNT];
4101 uint32_t ip_state;
4102 unsigned long wtime;
4103 int ret;
4104
4105 wtime = jiffies + (HZ * IP_CONFIG_TOV);
4106 do {
4107 for (idx = 0; idx < IP_ADDR_COUNT; idx++) {
4108 if (ip_idx[idx] == -1)
4109 continue;
4110
4111 ret = qla4xxx_get_ip_state(ha, 0, ip_idx[idx], sts);
4112
4113 if (ret == QLA_ERROR) {
4114 ip_idx[idx] = -1;
4115 continue;
4116 }
4117
4118 ip_state = (sts[1] & IP_STATE_MASK) >> IP_STATE_SHIFT;
4119
4120 DEBUG2(ql4_printk(KERN_INFO, ha,
4121 "Waiting for IP state for idx = %d, state = 0x%x\n",
4122 ip_idx[idx], ip_state));
4123 if (ip_state == IP_ADDRSTATE_UNCONFIGURED ||
4124 ip_state == IP_ADDRSTATE_INVALID ||
4125 ip_state == IP_ADDRSTATE_PREFERRED ||
4126 ip_state == IP_ADDRSTATE_DEPRICATED ||
4127 ip_state == IP_ADDRSTATE_DISABLING)
4128 ip_idx[idx] = -1;
Mike Christie13483732011-12-01 21:38:41 -06004129 }
4130
4131 /* Break if all IP states checked */
4132 if ((ip_idx[0] == -1) &&
4133 (ip_idx[1] == -1) &&
4134 (ip_idx[2] == -1) &&
4135 (ip_idx[3] == -1))
4136 break;
4137 schedule_timeout_uninterruptible(HZ);
4138 } while (time_after(wtime, jiffies));
4139}
4140
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004141static void qla4xxx_build_st_list(struct scsi_qla_host *ha,
4142 struct list_head *list_st)
Mike Christie13483732011-12-01 21:38:41 -06004143{
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004144 struct qla_ddb_index *st_ddb_idx;
Mike Christie13483732011-12-01 21:38:41 -06004145 int max_ddbs;
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004146 int fw_idx_size;
4147 struct dev_db_entry *fw_ddb_entry;
4148 dma_addr_t fw_ddb_dma;
Mike Christie13483732011-12-01 21:38:41 -06004149 int ret;
4150 uint32_t idx = 0, next_idx = 0;
4151 uint32_t state = 0, conn_err = 0;
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004152 uint16_t conn_id = 0;
Mike Christie13483732011-12-01 21:38:41 -06004153
4154 fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
4155 &fw_ddb_dma);
4156 if (fw_ddb_entry == NULL) {
4157 DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004158 goto exit_st_list;
Mike Christie13483732011-12-01 21:38:41 -06004159 }
4160
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004161 max_ddbs = is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
4162 MAX_DEV_DB_ENTRIES;
Mike Christie13483732011-12-01 21:38:41 -06004163 fw_idx_size = sizeof(struct qla_ddb_index);
4164
4165 for (idx = 0; idx < max_ddbs; idx = next_idx) {
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004166 ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry, fw_ddb_dma,
4167 NULL, &next_idx, &state,
4168 &conn_err, NULL, &conn_id);
Mike Christie13483732011-12-01 21:38:41 -06004169 if (ret == QLA_ERROR)
4170 break;
4171
Lalit Chandivade981c9822012-02-13 18:30:41 +05304172 /* Ignore DDB if invalid state (unassigned) */
4173 if (state == DDB_DS_UNASSIGNED)
4174 goto continue_next_st;
4175
Mike Christie13483732011-12-01 21:38:41 -06004176 /* Check if ST, add to the list_st */
4177 if (strlen((char *) fw_ddb_entry->iscsi_name) != 0)
4178 goto continue_next_st;
4179
4180 st_ddb_idx = vzalloc(fw_idx_size);
4181 if (!st_ddb_idx)
4182 break;
4183
4184 st_ddb_idx->fw_ddb_idx = idx;
4185
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004186 list_add_tail(&st_ddb_idx->list, list_st);
Mike Christie13483732011-12-01 21:38:41 -06004187continue_next_st:
4188 if (next_idx == 0)
4189 break;
4190 }
4191
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004192exit_st_list:
4193 if (fw_ddb_entry)
4194 dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
4195}
4196
4197/**
4198 * qla4xxx_remove_failed_ddb - Remove inactive or failed ddb from list
4199 * @ha: pointer to adapter structure
4200 * @list_ddb: List from which failed ddb to be removed
4201 *
4202 * Iterate over the list of DDBs and find and remove DDBs that are either in
4203 * no connection active state or failed state
4204 **/
4205static void qla4xxx_remove_failed_ddb(struct scsi_qla_host *ha,
4206 struct list_head *list_ddb)
4207{
4208 struct qla_ddb_index *ddb_idx, *ddb_idx_tmp;
4209 uint32_t next_idx = 0;
4210 uint32_t state = 0, conn_err = 0;
4211 int ret;
4212
4213 list_for_each_entry_safe(ddb_idx, ddb_idx_tmp, list_ddb, list) {
4214 ret = qla4xxx_get_fwddb_entry(ha, ddb_idx->fw_ddb_idx,
4215 NULL, 0, NULL, &next_idx, &state,
4216 &conn_err, NULL, NULL);
4217 if (ret == QLA_ERROR)
4218 continue;
4219
4220 if (state == DDB_DS_NO_CONNECTION_ACTIVE ||
4221 state == DDB_DS_SESSION_FAILED) {
4222 list_del_init(&ddb_idx->list);
4223 vfree(ddb_idx);
4224 }
4225 }
4226}
4227
4228static int qla4xxx_sess_conn_setup(struct scsi_qla_host *ha,
4229 struct dev_db_entry *fw_ddb_entry,
4230 int is_reset)
4231{
4232 struct iscsi_cls_session *cls_sess;
4233 struct iscsi_session *sess;
4234 struct iscsi_cls_conn *cls_conn;
4235 struct iscsi_endpoint *ep;
4236 uint16_t cmds_max = 32;
4237 uint16_t conn_id = 0;
4238 uint32_t initial_cmdsn = 0;
4239 int ret = QLA_SUCCESS;
4240
4241 struct ddb_entry *ddb_entry = NULL;
4242
4243 /* Create session object, with INVALID_ENTRY,
4244 * the targer_id would get set when we issue the login
4245 */
4246 cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport, ha->host,
4247 cmds_max, sizeof(struct ddb_entry),
4248 sizeof(struct ql4_task_data),
4249 initial_cmdsn, INVALID_ENTRY);
4250 if (!cls_sess) {
4251 ret = QLA_ERROR;
4252 goto exit_setup;
4253 }
4254
4255 /*
4256 * so calling module_put function to decrement the
4257 * reference count.
4258 **/
4259 module_put(qla4xxx_iscsi_transport.owner);
4260 sess = cls_sess->dd_data;
4261 ddb_entry = sess->dd_data;
4262 ddb_entry->sess = cls_sess;
4263
4264 cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
4265 memcpy(&ddb_entry->fw_ddb_entry, fw_ddb_entry,
4266 sizeof(struct dev_db_entry));
4267
4268 qla4xxx_setup_flash_ddb_entry(ha, ddb_entry);
4269
4270 cls_conn = iscsi_conn_setup(cls_sess, sizeof(struct qla_conn), conn_id);
4271
4272 if (!cls_conn) {
4273 ret = QLA_ERROR;
4274 goto exit_setup;
4275 }
4276
4277 ddb_entry->conn = cls_conn;
4278
4279 /* Setup ep, for displaying attributes in sysfs */
4280 ep = qla4xxx_get_ep_fwdb(ha, fw_ddb_entry);
4281 if (ep) {
4282 ep->conn = cls_conn;
4283 cls_conn->ep = ep;
4284 } else {
4285 DEBUG2(ql4_printk(KERN_ERR, ha, "Unable to get ep\n"));
4286 ret = QLA_ERROR;
4287 goto exit_setup;
4288 }
4289
4290 /* Update sess/conn params */
4291 qla4xxx_copy_fwddb_param(ha, fw_ddb_entry, cls_sess, cls_conn);
4292
4293 if (is_reset == RESET_ADAPTER) {
4294 iscsi_block_session(cls_sess);
4295 /* Use the relogin path to discover new devices
4296 * by short-circuting the logic of setting
4297 * timer to relogin - instead set the flags
4298 * to initiate login right away.
4299 */
4300 set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
4301 set_bit(DF_RELOGIN, &ddb_entry->flags);
4302 }
4303
4304exit_setup:
4305 return ret;
4306}
4307
4308static void qla4xxx_build_nt_list(struct scsi_qla_host *ha,
4309 struct list_head *list_nt, int is_reset)
4310{
4311 struct dev_db_entry *fw_ddb_entry;
4312 dma_addr_t fw_ddb_dma;
4313 int max_ddbs;
4314 int fw_idx_size;
4315 int ret;
4316 uint32_t idx = 0, next_idx = 0;
4317 uint32_t state = 0, conn_err = 0;
4318 uint16_t conn_id = 0;
4319 struct qla_ddb_index *nt_ddb_idx;
4320
4321 fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
4322 &fw_ddb_dma);
4323 if (fw_ddb_entry == NULL) {
4324 DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
4325 goto exit_nt_list;
4326 }
4327 max_ddbs = is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
4328 MAX_DEV_DB_ENTRIES;
4329 fw_idx_size = sizeof(struct qla_ddb_index);
4330
4331 for (idx = 0; idx < max_ddbs; idx = next_idx) {
4332 ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry, fw_ddb_dma,
4333 NULL, &next_idx, &state,
4334 &conn_err, NULL, &conn_id);
4335 if (ret == QLA_ERROR)
4336 break;
4337
4338 if (qla4xxx_verify_boot_idx(ha, idx) != QLA_SUCCESS)
4339 goto continue_next_nt;
4340
4341 /* Check if NT, then add to list it */
4342 if (strlen((char *) fw_ddb_entry->iscsi_name) == 0)
4343 goto continue_next_nt;
4344
4345 if (!(state == DDB_DS_NO_CONNECTION_ACTIVE ||
4346 state == DDB_DS_SESSION_FAILED))
4347 goto continue_next_nt;
4348
4349 DEBUG2(ql4_printk(KERN_INFO, ha,
4350 "Adding DDB to session = 0x%x\n", idx));
4351 if (is_reset == INIT_ADAPTER) {
4352 nt_ddb_idx = vmalloc(fw_idx_size);
4353 if (!nt_ddb_idx)
4354 break;
4355
4356 nt_ddb_idx->fw_ddb_idx = idx;
4357
4358 memcpy(&nt_ddb_idx->fw_ddb, fw_ddb_entry,
4359 sizeof(struct dev_db_entry));
4360
4361 if (qla4xxx_is_flash_ddb_exists(ha, list_nt,
4362 fw_ddb_entry) == QLA_SUCCESS) {
4363 vfree(nt_ddb_idx);
4364 goto continue_next_nt;
4365 }
4366 list_add_tail(&nt_ddb_idx->list, list_nt);
4367 } else if (is_reset == RESET_ADAPTER) {
4368 if (qla4xxx_is_session_exists(ha, fw_ddb_entry) ==
4369 QLA_SUCCESS)
4370 goto continue_next_nt;
4371 }
4372
4373 ret = qla4xxx_sess_conn_setup(ha, fw_ddb_entry, is_reset);
4374 if (ret == QLA_ERROR)
4375 goto exit_nt_list;
4376
4377continue_next_nt:
4378 if (next_idx == 0)
4379 break;
4380 }
4381
4382exit_nt_list:
4383 if (fw_ddb_entry)
4384 dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
4385}
4386
4387/**
4388 * qla4xxx_build_ddb_list - Build ddb list and setup sessions
4389 * @ha: pointer to adapter structure
4390 * @is_reset: Is this init path or reset path
4391 *
4392 * Create a list of sendtargets (st) from firmware DDBs, issue send targets
4393 * using connection open, then create the list of normal targets (nt)
4394 * from firmware DDBs. Based on the list of nt setup session and connection
4395 * objects.
4396 **/
4397void qla4xxx_build_ddb_list(struct scsi_qla_host *ha, int is_reset)
4398{
4399 uint16_t tmo = 0;
4400 struct list_head list_st, list_nt;
4401 struct qla_ddb_index *st_ddb_idx, *st_ddb_idx_tmp;
4402 unsigned long wtime;
4403
4404 if (!test_bit(AF_LINK_UP, &ha->flags)) {
4405 set_bit(AF_BUILD_DDB_LIST, &ha->flags);
4406 ha->is_reset = is_reset;
4407 return;
4408 }
4409
4410 INIT_LIST_HEAD(&list_st);
4411 INIT_LIST_HEAD(&list_nt);
4412
4413 qla4xxx_build_st_list(ha, &list_st);
4414
Mike Christie13483732011-12-01 21:38:41 -06004415 /* Before issuing conn open mbox, ensure all IPs states are configured
4416 * Note, conn open fails if IPs are not configured
4417 */
4418 qla4xxx_wait_for_ip_configuration(ha);
4419
4420 /* Go thru the STs and fire the sendtargets by issuing conn open mbx */
4421 list_for_each_entry_safe(st_ddb_idx, st_ddb_idx_tmp, &list_st, list) {
4422 qla4xxx_conn_open(ha, st_ddb_idx->fw_ddb_idx);
4423 }
4424
4425 /* Wait to ensure all sendtargets are done for min 12 sec wait */
Nilesh Javalic28eaac2011-12-18 21:40:44 -08004426 tmo = ((ha->def_timeout > LOGIN_TOV) &&
4427 (ha->def_timeout < LOGIN_TOV * 10) ?
4428 ha->def_timeout : LOGIN_TOV);
4429
Mike Christie13483732011-12-01 21:38:41 -06004430 DEBUG2(ql4_printk(KERN_INFO, ha,
4431 "Default time to wait for build ddb %d\n", tmo));
4432
4433 wtime = jiffies + (HZ * tmo);
4434 do {
Nilesh Javalif1f2e602011-12-16 01:58:57 -08004435 if (list_empty(&list_st))
4436 break;
4437
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004438 qla4xxx_remove_failed_ddb(ha, &list_st);
Mike Christie13483732011-12-01 21:38:41 -06004439 schedule_timeout_uninterruptible(HZ / 10);
4440 } while (time_after(wtime, jiffies));
4441
4442 /* Free up the sendtargets list */
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004443 qla4xxx_free_ddb_list(&list_st);
Mike Christie13483732011-12-01 21:38:41 -06004444
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004445 qla4xxx_build_nt_list(ha, &list_nt, is_reset);
Mike Christie13483732011-12-01 21:38:41 -06004446
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004447 qla4xxx_free_ddb_list(&list_nt);
Mike Christie13483732011-12-01 21:38:41 -06004448
4449 qla4xxx_free_ddb_index(ha);
4450}
4451
David Somayajuluafaf5a22006-09-19 10:28:00 -07004452/**
4453 * qla4xxx_probe_adapter - callback function to probe HBA
4454 * @pdev: pointer to pci_dev structure
4455 * @pci_device_id: pointer to pci_device entry
4456 *
4457 * This routine will probe for Qlogic 4xxx iSCSI host adapters.
4458 * It returns zero if successful. It also initializes all data necessary for
4459 * the driver.
4460 **/
4461static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
4462 const struct pci_device_id *ent)
4463{
4464 int ret = -ENODEV, status;
4465 struct Scsi_Host *host;
4466 struct scsi_qla_host *ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004467 uint8_t init_retry_count = 0;
4468 char buf[34];
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304469 struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
Prasanna Mumbaif9880e72011-03-21 03:34:26 -07004470 uint32_t dev_state;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004471
4472 if (pci_enable_device(pdev))
4473 return -1;
4474
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004475 host = iscsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha), 0);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004476 if (host == NULL) {
4477 printk(KERN_WARNING
4478 "qla4xxx: Couldn't allocate host from scsi layer!\n");
4479 goto probe_disable_device;
4480 }
4481
4482 /* Clear our data area */
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004483 ha = to_qla_host(host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004484 memset(ha, 0, sizeof(*ha));
4485
4486 /* Save the information from PCI BIOS. */
4487 ha->pdev = pdev;
4488 ha->host = host;
4489 ha->host_no = host->host_no;
4490
Lalit Chandivade2232be02010-07-30 14:38:47 +05304491 pci_enable_pcie_error_reporting(pdev);
4492
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304493 /* Setup Runtime configurable options */
4494 if (is_qla8022(ha)) {
4495 ha->isp_ops = &qla4_8xxx_isp_ops;
4496 rwlock_init(&ha->hw_lock);
4497 ha->qdr_sn_window = -1;
4498 ha->ddr_mn_window = -1;
4499 ha->curr_window = 255;
4500 ha->func_num = PCI_FUNC(ha->pdev->devfn);
4501 nx_legacy_intr = &legacy_intr[ha->func_num];
4502 ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
4503 ha->nx_legacy_intr.tgt_status_reg =
4504 nx_legacy_intr->tgt_status_reg;
4505 ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
4506 ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
4507 } else {
4508 ha->isp_ops = &qla4xxx_isp_ops;
4509 }
4510
Lalit Chandivade2232be02010-07-30 14:38:47 +05304511 /* Set EEH reset type to fundamental if required by hba */
4512 if (is_qla8022(ha))
4513 pdev->needs_freset = 1;
4514
David Somayajuluafaf5a22006-09-19 10:28:00 -07004515 /* Configure PCI I/O space. */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304516 ret = ha->isp_ops->iospace_config(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004517 if (ret)
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304518 goto probe_failed_ioconfig;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004519
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304520 ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
David Somayajuluafaf5a22006-09-19 10:28:00 -07004521 pdev->device, pdev->irq, ha->reg);
4522
4523 qla4xxx_config_dma_addressing(ha);
4524
4525 /* Initialize lists and spinlocks. */
David Somayajuluafaf5a22006-09-19 10:28:00 -07004526 INIT_LIST_HEAD(&ha->free_srb_q);
4527
4528 mutex_init(&ha->mbox_sem);
Lalit Chandivade45494152011-10-07 16:55:42 -07004529 mutex_init(&ha->chap_sem);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304530 init_completion(&ha->mbx_intr_comp);
Vikas Chaudhary95d31262011-08-12 02:51:29 -07004531 init_completion(&ha->disable_acb_comp);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004532
4533 spin_lock_init(&ha->hardware_lock);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004534
Vikas Chaudharyff884432011-08-29 23:43:02 +05304535 /* Initialize work list */
4536 INIT_LIST_HEAD(&ha->work_list);
4537
David Somayajuluafaf5a22006-09-19 10:28:00 -07004538 /* Allocate dma buffers */
4539 if (qla4xxx_mem_alloc(ha)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304540 ql4_printk(KERN_WARNING, ha,
4541 "[ERROR] Failed to allocate memory for adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07004542
4543 ret = -ENOMEM;
4544 goto probe_failed;
4545 }
4546
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004547 host->cmd_per_lun = 3;
4548 host->max_channel = 0;
4549 host->max_lun = MAX_LUNS - 1;
4550 host->max_id = MAX_TARGETS;
4551 host->max_cmd_len = IOCB_MAX_CDB_LEN;
4552 host->can_queue = MAX_SRBS ;
4553 host->transportt = qla4xxx_scsi_transport;
4554
4555 ret = scsi_init_shared_tag_map(host, MAX_SRBS);
4556 if (ret) {
4557 ql4_printk(KERN_WARNING, ha,
4558 "%s: scsi_init_shared_tag_map failed\n", __func__);
4559 goto probe_failed;
4560 }
4561
4562 pci_set_drvdata(pdev, ha);
4563
4564 ret = scsi_add_host(host, &pdev->dev);
4565 if (ret)
4566 goto probe_failed;
4567
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304568 if (is_qla8022(ha))
4569 (void) qla4_8xxx_get_flash_info(ha);
4570
David Somayajuluafaf5a22006-09-19 10:28:00 -07004571 /*
4572 * Initialize the Host adapter request/response queues and
4573 * firmware
4574 * NOTE: interrupts enabled upon successful completion
4575 */
Mike Christie13483732011-12-01 21:38:41 -06004576 status = qla4xxx_initialize_adapter(ha, INIT_ADAPTER);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304577 while ((!test_bit(AF_ONLINE, &ha->flags)) &&
4578 init_retry_count++ < MAX_INIT_RETRIES) {
Prasanna Mumbaif9880e72011-03-21 03:34:26 -07004579
4580 if (is_qla8022(ha)) {
4581 qla4_8xxx_idc_lock(ha);
4582 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
4583 qla4_8xxx_idc_unlock(ha);
4584 if (dev_state == QLA82XX_DEV_FAILED) {
4585 ql4_printk(KERN_WARNING, ha, "%s: don't retry "
4586 "initialize adapter. H/W is in failed state\n",
4587 __func__);
4588 break;
4589 }
4590 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07004591 DEBUG2(printk("scsi: %s: retrying adapter initialization "
4592 "(%d)\n", __func__, init_retry_count));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304593
4594 if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
4595 continue;
4596
Mike Christie13483732011-12-01 21:38:41 -06004597 status = qla4xxx_initialize_adapter(ha, INIT_ADAPTER);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004598 }
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304599
4600 if (!test_bit(AF_ONLINE, &ha->flags)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304601 ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07004602
Lalit Chandivadefe998522010-12-02 22:12:36 -08004603 if (is_qla8022(ha) && ql4xdontresethba) {
4604 /* Put the device in failed state. */
4605 DEBUG2(printk(KERN_ERR "HW STATE: FAILED\n"));
4606 qla4_8xxx_idc_lock(ha);
4607 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
4608 QLA82XX_DEV_FAILED);
4609 qla4_8xxx_idc_unlock(ha);
4610 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07004611 ret = -ENODEV;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004612 goto remove_host;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004613 }
4614
David Somayajuluafaf5a22006-09-19 10:28:00 -07004615 /* Startup the kernel thread for this host adapter. */
4616 DEBUG2(printk("scsi: %s: Starting kernel thread for "
4617 "qla4xxx_dpc\n", __func__));
4618 sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
4619 ha->dpc_thread = create_singlethread_workqueue(buf);
4620 if (!ha->dpc_thread) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304621 ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07004622 ret = -ENODEV;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004623 goto remove_host;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004624 }
David Howellsc4028952006-11-22 14:57:56 +00004625 INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004626
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004627 sprintf(buf, "qla4xxx_%lu_task", ha->host_no);
4628 ha->task_wq = alloc_workqueue(buf, WQ_MEM_RECLAIM, 1);
4629 if (!ha->task_wq) {
4630 ql4_printk(KERN_WARNING, ha, "Unable to start task thread!\n");
4631 ret = -ENODEV;
4632 goto remove_host;
4633 }
4634
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304635 /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
4636 * (which is called indirectly by qla4xxx_initialize_adapter),
4637 * so that irqs will be registered after crbinit but before
4638 * mbx_intr_enable.
4639 */
4640 if (!is_qla8022(ha)) {
4641 ret = qla4xxx_request_irqs(ha);
4642 if (ret) {
4643 ql4_printk(KERN_WARNING, ha, "Failed to reserve "
4644 "interrupt %d already in use.\n", pdev->irq);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004645 goto remove_host;
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304646 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07004647 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07004648
Lalit Chandivade2232be02010-07-30 14:38:47 +05304649 pci_save_state(ha->pdev);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304650 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004651
4652 /* Start timer thread. */
4653 qla4xxx_start_timer(ha, qla4xxx_timer, 1);
4654
4655 set_bit(AF_INIT_DONE, &ha->flags);
4656
David Somayajuluafaf5a22006-09-19 10:28:00 -07004657 printk(KERN_INFO
4658 " QLogic iSCSI HBA Driver version: %s\n"
4659 " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
4660 qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
4661 ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
4662 ha->patch_number, ha->build_number);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05004663
Manish Rangankar2a991c22011-07-25 13:48:55 -05004664 if (qla4xxx_setup_boot_info(ha))
4665 ql4_printk(KERN_ERR, ha, "%s:ISCSI boot info setup failed\n",
4666 __func__);
4667
Mike Christie13483732011-12-01 21:38:41 -06004668 /* Perform the build ddb list and login to each */
4669 qla4xxx_build_ddb_list(ha, INIT_ADAPTER);
4670 iscsi_host_for_each_session(ha->host, qla4xxx_login_flash_ddb);
4671
4672 qla4xxx_create_chap_list(ha);
4673
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05004674 qla4xxx_create_ifaces(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004675 return 0;
4676
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004677remove_host:
4678 scsi_remove_host(ha->host);
4679
David Somayajuluafaf5a22006-09-19 10:28:00 -07004680probe_failed:
4681 qla4xxx_free_adapter(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304682
4683probe_failed_ioconfig:
Lalit Chandivade2232be02010-07-30 14:38:47 +05304684 pci_disable_pcie_error_reporting(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004685 scsi_host_put(ha->host);
4686
4687probe_disable_device:
4688 pci_disable_device(pdev);
4689
4690 return ret;
4691}
4692
4693/**
Karen Higgins7eece5a2011-03-21 03:34:29 -07004694 * qla4xxx_prevent_other_port_reinit - prevent other port from re-initialize
4695 * @ha: pointer to adapter structure
4696 *
4697 * Mark the other ISP-4xxx port to indicate that the driver is being removed,
4698 * so that the other port will not re-initialize while in the process of
4699 * removing the ha due to driver unload or hba hotplug.
4700 **/
4701static void qla4xxx_prevent_other_port_reinit(struct scsi_qla_host *ha)
4702{
4703 struct scsi_qla_host *other_ha = NULL;
4704 struct pci_dev *other_pdev = NULL;
4705 int fn = ISP4XXX_PCI_FN_2;
4706
4707 /*iscsi function numbers for ISP4xxx is 1 and 3*/
4708 if (PCI_FUNC(ha->pdev->devfn) & BIT_1)
4709 fn = ISP4XXX_PCI_FN_1;
4710
4711 other_pdev =
4712 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
4713 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
4714 fn));
4715
4716 /* Get other_ha if other_pdev is valid and state is enable*/
4717 if (other_pdev) {
4718 if (atomic_read(&other_pdev->enable_cnt)) {
4719 other_ha = pci_get_drvdata(other_pdev);
4720 if (other_ha) {
4721 set_bit(AF_HA_REMOVAL, &other_ha->flags);
4722 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: "
4723 "Prevent %s reinit\n", __func__,
4724 dev_name(&other_ha->pdev->dev)));
4725 }
4726 }
4727 pci_dev_put(other_pdev);
4728 }
4729}
4730
Mike Christie13483732011-12-01 21:38:41 -06004731static void qla4xxx_destroy_fw_ddb_session(struct scsi_qla_host *ha)
4732{
4733 struct ddb_entry *ddb_entry;
4734 int options;
4735 int idx;
4736
4737 for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
4738
4739 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
4740 if ((ddb_entry != NULL) &&
4741 (ddb_entry->ddb_type == FLASH_DDB)) {
4742
4743 options = LOGOUT_OPTION_CLOSE_SESSION;
4744 if (qla4xxx_session_logout_ddb(ha, ddb_entry, options)
4745 == QLA_ERROR)
4746 ql4_printk(KERN_ERR, ha, "%s: Logout failed\n",
4747 __func__);
4748
4749 qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
4750 /*
4751 * we have decremented the reference count of the driver
4752 * when we setup the session to have the driver unload
4753 * to be seamless without actually destroying the
4754 * session
4755 **/
4756 try_module_get(qla4xxx_iscsi_transport.owner);
4757 iscsi_destroy_endpoint(ddb_entry->conn->ep);
4758 qla4xxx_free_ddb(ha, ddb_entry);
4759 iscsi_session_teardown(ddb_entry->sess);
4760 }
4761 }
4762}
Karen Higgins7eece5a2011-03-21 03:34:29 -07004763/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07004764 * qla4xxx_remove_adapter - calback function to remove adapter.
4765 * @pci_dev: PCI device pointer
4766 **/
4767static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
4768{
4769 struct scsi_qla_host *ha;
4770
4771 ha = pci_get_drvdata(pdev);
4772
Karen Higgins7eece5a2011-03-21 03:34:29 -07004773 if (!is_qla8022(ha))
4774 qla4xxx_prevent_other_port_reinit(ha);
David C Somayajulubee4fe82007-05-23 18:03:32 -07004775
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05004776 /* destroy iface from sysfs */
4777 qla4xxx_destroy_ifaces(ha);
4778
Mike Christie13483732011-12-01 21:38:41 -06004779 if ((!ql4xdisablesysfsboot) && ha->boot_kset)
Manish Rangankar2a991c22011-07-25 13:48:55 -05004780 iscsi_boot_destroy_kset(ha->boot_kset);
4781
Mike Christie13483732011-12-01 21:38:41 -06004782 qla4xxx_destroy_fw_ddb_session(ha);
4783
David Somayajuluafaf5a22006-09-19 10:28:00 -07004784 scsi_remove_host(ha->host);
4785
4786 qla4xxx_free_adapter(ha);
4787
4788 scsi_host_put(ha->host);
4789
Lalit Chandivade2232be02010-07-30 14:38:47 +05304790 pci_disable_pcie_error_reporting(pdev);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304791 pci_disable_device(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004792 pci_set_drvdata(pdev, NULL);
4793}
4794
4795/**
4796 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
4797 * @ha: HA context
4798 *
4799 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
4800 * supported addressing method.
4801 */
Adrian Bunk47975472007-04-26 00:35:16 -07004802static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07004803{
4804 int retval;
4805
4806 /* Update our PCI device dma_mask for full 64 bit mask */
Yang Hongyang6a355282009-04-06 19:01:13 -07004807 if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
4808 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07004809 dev_dbg(&ha->pdev->dev,
4810 "Failed to set 64 bit PCI consistent mask; "
4811 "using 32 bit.\n");
4812 retval = pci_set_consistent_dma_mask(ha->pdev,
Yang Hongyang284901a2009-04-06 19:01:15 -07004813 DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07004814 }
4815 } else
Yang Hongyang284901a2009-04-06 19:01:15 -07004816 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07004817}
4818
4819static int qla4xxx_slave_alloc(struct scsi_device *sdev)
4820{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004821 struct iscsi_cls_session *cls_sess;
4822 struct iscsi_session *sess;
4823 struct ddb_entry *ddb;
Vikas Chaudhary8bb40332011-03-21 03:34:31 -07004824 int queue_depth = QL4_DEF_QDEPTH;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004825
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004826 cls_sess = starget_to_session(sdev->sdev_target);
4827 sess = cls_sess->dd_data;
4828 ddb = sess->dd_data;
4829
David Somayajuluafaf5a22006-09-19 10:28:00 -07004830 sdev->hostdata = ddb;
4831 sdev->tagged_supported = 1;
Vikas Chaudhary8bb40332011-03-21 03:34:31 -07004832
4833 if (ql4xmaxqdepth != 0 && ql4xmaxqdepth <= 0xffffU)
4834 queue_depth = ql4xmaxqdepth;
4835
4836 scsi_activate_tcq(sdev, queue_depth);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004837 return 0;
4838}
4839
4840static int qla4xxx_slave_configure(struct scsi_device *sdev)
4841{
4842 sdev->tagged_supported = 1;
4843 return 0;
4844}
4845
4846static void qla4xxx_slave_destroy(struct scsi_device *sdev)
4847{
4848 scsi_deactivate_tcq(sdev, 1);
4849}
4850
4851/**
4852 * qla4xxx_del_from_active_array - returns an active srb
4853 * @ha: Pointer to host adapter structure.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02004854 * @index: index into the active_array
David Somayajuluafaf5a22006-09-19 10:28:00 -07004855 *
4856 * This routine removes and returns the srb at the specified index
4857 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304858struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
4859 uint32_t index)
David Somayajuluafaf5a22006-09-19 10:28:00 -07004860{
4861 struct srb *srb = NULL;
Vikas Chaudhary53698872010-04-28 11:41:59 +05304862 struct scsi_cmnd *cmd = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004863
Vikas Chaudhary53698872010-04-28 11:41:59 +05304864 cmd = scsi_host_find_tag(ha->host, index);
4865 if (!cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07004866 return srb;
4867
Vikas Chaudhary53698872010-04-28 11:41:59 +05304868 srb = (struct srb *)CMD_SP(cmd);
4869 if (!srb)
David Somayajuluafaf5a22006-09-19 10:28:00 -07004870 return srb;
4871
4872 /* update counters */
4873 if (srb->flags & SRB_DMA_VALID) {
4874 ha->req_q_count += srb->iocb_cnt;
4875 ha->iocb_cnt -= srb->iocb_cnt;
4876 if (srb->cmd)
Vikas Chaudhary53698872010-04-28 11:41:59 +05304877 srb->cmd->host_scribble =
4878 (unsigned char *)(unsigned long) MAX_SRBS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004879 }
4880 return srb;
4881}
4882
4883/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07004884 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304885 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07004886 * @cmd: Scsi Command to wait on.
4887 *
4888 * This routine waits for the command to be returned by the Firmware
4889 * for some max time.
4890 **/
4891static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
4892 struct scsi_cmnd *cmd)
4893{
4894 int done = 0;
4895 struct srb *rp;
4896 uint32_t max_wait_time = EH_WAIT_CMD_TOV;
Lalit Chandivade2232be02010-07-30 14:38:47 +05304897 int ret = SUCCESS;
4898
4899 /* Dont wait on command if PCI error is being handled
4900 * by PCI AER driver
4901 */
4902 if (unlikely(pci_channel_offline(ha->pdev)) ||
4903 (test_bit(AF_EEH_BUSY, &ha->flags))) {
4904 ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
4905 ha->host_no, __func__);
4906 return ret;
4907 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07004908
4909 do {
4910 /* Checking to see if its returned to OS */
Vikas Chaudhary53698872010-04-28 11:41:59 +05304911 rp = (struct srb *) CMD_SP(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004912 if (rp == NULL) {
4913 done++;
4914 break;
4915 }
4916
4917 msleep(2000);
4918 } while (max_wait_time--);
4919
4920 return done;
4921}
4922
4923/**
4924 * qla4xxx_wait_for_hba_online - waits for HBA to come online
4925 * @ha: Pointer to host adapter structure
4926 **/
4927static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
4928{
4929 unsigned long wait_online;
4930
Vikas Chaudharyf581a3f2010-10-06 22:47:48 -07004931 wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004932 while (time_before(jiffies, wait_online)) {
4933
4934 if (adapter_up(ha))
4935 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004936
4937 msleep(2000);
4938 }
4939
4940 return QLA_ERROR;
4941}
4942
4943/**
Mike Christiece545032008-02-29 18:25:20 -06004944 * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02004945 * @ha: pointer to HBA
David Somayajuluafaf5a22006-09-19 10:28:00 -07004946 * @t: target id
4947 * @l: lun id
4948 *
4949 * This function waits for all outstanding commands to a lun to complete. It
4950 * returns 0 if all pending commands are returned and 1 otherwise.
4951 **/
Mike Christiece545032008-02-29 18:25:20 -06004952static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
4953 struct scsi_target *stgt,
4954 struct scsi_device *sdev)
David Somayajuluafaf5a22006-09-19 10:28:00 -07004955{
4956 int cnt;
4957 int status = 0;
4958 struct scsi_cmnd *cmd;
4959
4960 /*
Mike Christiece545032008-02-29 18:25:20 -06004961 * Waiting for all commands for the designated target or dev
4962 * in the active array
David Somayajuluafaf5a22006-09-19 10:28:00 -07004963 */
4964 for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
4965 cmd = scsi_host_find_tag(ha->host, cnt);
Mike Christiece545032008-02-29 18:25:20 -06004966 if (cmd && stgt == scsi_target(cmd->device) &&
4967 (!sdev || sdev == cmd->device)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07004968 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
4969 status++;
4970 break;
4971 }
4972 }
4973 }
4974 return status;
4975}
4976
4977/**
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304978 * qla4xxx_eh_abort - callback for abort task.
4979 * @cmd: Pointer to Linux's SCSI command structure
4980 *
4981 * This routine is called by the Linux OS to abort the specified
4982 * command.
4983 **/
4984static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
4985{
4986 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
4987 unsigned int id = cmd->device->id;
4988 unsigned int lun = cmd->device->lun;
Mike Christie92b3e5b2010-10-06 22:51:17 -07004989 unsigned long flags;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304990 struct srb *srb = NULL;
4991 int ret = SUCCESS;
4992 int wait = 0;
4993
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304994 ql4_printk(KERN_INFO, ha,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004995 "scsi%ld:%d:%d: Abort command issued cmd=%p\n",
4996 ha->host_no, id, lun, cmd);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304997
Mike Christie92b3e5b2010-10-06 22:51:17 -07004998 spin_lock_irqsave(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304999 srb = (struct srb *) CMD_SP(cmd);
Mike Christie92b3e5b2010-10-06 22:51:17 -07005000 if (!srb) {
5001 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05305002 return SUCCESS;
Mike Christie92b3e5b2010-10-06 22:51:17 -07005003 }
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05305004 kref_get(&srb->srb_ref);
Mike Christie92b3e5b2010-10-06 22:51:17 -07005005 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05305006
5007 if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
5008 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
5009 ha->host_no, id, lun));
5010 ret = FAILED;
5011 } else {
5012 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
5013 ha->host_no, id, lun));
5014 wait = 1;
5015 }
5016
5017 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
5018
5019 /* Wait for command to complete */
5020 if (wait) {
5021 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
5022 DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
5023 ha->host_no, id, lun));
5024 ret = FAILED;
5025 }
5026 }
5027
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05305028 ql4_printk(KERN_INFO, ha,
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05305029 "scsi%ld:%d:%d: Abort command - %s\n",
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005030 ha->host_no, id, lun, (ret == SUCCESS) ? "succeeded" : "failed");
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05305031
5032 return ret;
5033}
5034
5035/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07005036 * qla4xxx_eh_device_reset - callback for target reset.
5037 * @cmd: Pointer to Linux's SCSI command structure
5038 *
5039 * This routine is called by the Linux OS to reset all luns on the
5040 * specified target.
5041 **/
5042static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
5043{
5044 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
5045 struct ddb_entry *ddb_entry = cmd->device->hostdata;
David Somayajuluafaf5a22006-09-19 10:28:00 -07005046 int ret = FAILED, stat;
5047
Karen Higgins612f7342009-07-15 15:03:01 -05005048 if (!ddb_entry)
David Somayajuluafaf5a22006-09-19 10:28:00 -07005049 return ret;
5050
Mike Christiec01be6d2010-07-22 16:59:49 +05305051 ret = iscsi_block_scsi_eh(cmd);
5052 if (ret)
5053 return ret;
5054 ret = FAILED;
5055
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05305056 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07005057 "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
5058 cmd->device->channel, cmd->device->id, cmd->device->lun);
5059
5060 DEBUG2(printk(KERN_INFO
5061 "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
5062 "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
Jens Axboe242f9dc2008-09-14 05:55:09 -07005063 cmd, jiffies, cmd->request->timeout / HZ,
David Somayajuluafaf5a22006-09-19 10:28:00 -07005064 ha->dpc_flags, cmd->result, cmd->allowed));
5065
5066 /* FIXME: wait for hba to go online */
5067 stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
5068 if (stat != QLA_SUCCESS) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05305069 ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
David Somayajuluafaf5a22006-09-19 10:28:00 -07005070 goto eh_dev_reset_done;
5071 }
5072
Mike Christiece545032008-02-29 18:25:20 -06005073 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
5074 cmd->device)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05305075 ql4_printk(KERN_INFO, ha,
Mike Christiece545032008-02-29 18:25:20 -06005076 "DEVICE RESET FAILED - waiting for "
5077 "commands.\n");
5078 goto eh_dev_reset_done;
David Somayajuluafaf5a22006-09-19 10:28:00 -07005079 }
5080
David C Somayajulu9d562912008-03-19 11:23:03 -07005081 /* Send marker. */
5082 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
5083 MM_LUN_RESET) != QLA_SUCCESS)
5084 goto eh_dev_reset_done;
5085
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05305086 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07005087 "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
5088 ha->host_no, cmd->device->channel, cmd->device->id,
5089 cmd->device->lun);
5090
5091 ret = SUCCESS;
5092
5093eh_dev_reset_done:
5094
5095 return ret;
5096}
5097
5098/**
Mike Christiece545032008-02-29 18:25:20 -06005099 * qla4xxx_eh_target_reset - callback for target reset.
5100 * @cmd: Pointer to Linux's SCSI command structure
5101 *
5102 * This routine is called by the Linux OS to reset the target.
5103 **/
5104static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
5105{
5106 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
5107 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christiec01be6d2010-07-22 16:59:49 +05305108 int stat, ret;
Mike Christiece545032008-02-29 18:25:20 -06005109
5110 if (!ddb_entry)
5111 return FAILED;
5112
Mike Christiec01be6d2010-07-22 16:59:49 +05305113 ret = iscsi_block_scsi_eh(cmd);
5114 if (ret)
5115 return ret;
5116
Mike Christiece545032008-02-29 18:25:20 -06005117 starget_printk(KERN_INFO, scsi_target(cmd->device),
5118 "WARM TARGET RESET ISSUED.\n");
5119
5120 DEBUG2(printk(KERN_INFO
5121 "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
5122 "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
Jens Axboe242f9dc2008-09-14 05:55:09 -07005123 ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
Mike Christiece545032008-02-29 18:25:20 -06005124 ha->dpc_flags, cmd->result, cmd->allowed));
5125
5126 stat = qla4xxx_reset_target(ha, ddb_entry);
5127 if (stat != QLA_SUCCESS) {
5128 starget_printk(KERN_INFO, scsi_target(cmd->device),
5129 "WARM TARGET RESET FAILED.\n");
5130 return FAILED;
5131 }
5132
Mike Christiece545032008-02-29 18:25:20 -06005133 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
5134 NULL)) {
5135 starget_printk(KERN_INFO, scsi_target(cmd->device),
5136 "WARM TARGET DEVICE RESET FAILED - "
5137 "waiting for commands.\n");
5138 return FAILED;
5139 }
5140
David C Somayajulu9d562912008-03-19 11:23:03 -07005141 /* Send marker. */
5142 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
5143 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
5144 starget_printk(KERN_INFO, scsi_target(cmd->device),
5145 "WARM TARGET DEVICE RESET FAILED - "
5146 "marker iocb failed.\n");
5147 return FAILED;
5148 }
5149
Mike Christiece545032008-02-29 18:25:20 -06005150 starget_printk(KERN_INFO, scsi_target(cmd->device),
5151 "WARM TARGET RESET SUCCEEDED.\n");
5152 return SUCCESS;
5153}
5154
5155/**
Sarang Radke8a28896012011-12-06 02:34:10 -08005156 * qla4xxx_is_eh_active - check if error handler is running
5157 * @shost: Pointer to SCSI Host struct
5158 *
5159 * This routine finds that if reset host is called in EH
5160 * scenario or from some application like sg_reset
5161 **/
5162static int qla4xxx_is_eh_active(struct Scsi_Host *shost)
5163{
5164 if (shost->shost_state == SHOST_RECOVERY)
5165 return 1;
5166 return 0;
5167}
5168
5169/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07005170 * qla4xxx_eh_host_reset - kernel callback
5171 * @cmd: Pointer to Linux's SCSI command structure
5172 *
5173 * This routine is invoked by the Linux kernel to perform fatal error
5174 * recovery on the specified adapter.
5175 **/
5176static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
5177{
5178 int return_status = FAILED;
5179 struct scsi_qla_host *ha;
5180
Manish Rangankarb3a271a2011-07-25 13:48:53 -05005181 ha = to_qla_host(cmd->device->host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07005182
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05305183 if (ql4xdontresethba) {
5184 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
5185 ha->host_no, __func__));
Sarang Radke8a28896012011-12-06 02:34:10 -08005186
5187 /* Clear outstanding srb in queues */
5188 if (qla4xxx_is_eh_active(cmd->device->host))
5189 qla4xxx_abort_active_cmds(ha, DID_ABORT << 16);
5190
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05305191 return FAILED;
5192 }
5193
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05305194 ql4_printk(KERN_INFO, ha,
Karen Higginsdca05c42009-07-15 15:03:00 -05005195 "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
David Somayajuluafaf5a22006-09-19 10:28:00 -07005196 cmd->device->channel, cmd->device->id, cmd->device->lun);
5197
5198 if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
5199 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
5200 "DEAD.\n", ha->host_no, cmd->device->channel,
5201 __func__));
5202
5203 return FAILED;
5204 }
5205
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05305206 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
5207 if (is_qla8022(ha))
5208 set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
5209 else
5210 set_bit(DPC_RESET_HA, &ha->dpc_flags);
5211 }
Mike Christie50a29ae2008-03-04 13:26:53 -06005212
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05305213 if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
David Somayajuluafaf5a22006-09-19 10:28:00 -07005214 return_status = SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07005215
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05305216 ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005217 return_status == FAILED ? "FAILED" : "SUCCEEDED");
David Somayajuluafaf5a22006-09-19 10:28:00 -07005218
5219 return return_status;
5220}
5221
Vikas Chaudhary95d31262011-08-12 02:51:29 -07005222static int qla4xxx_context_reset(struct scsi_qla_host *ha)
5223{
5224 uint32_t mbox_cmd[MBOX_REG_COUNT];
5225 uint32_t mbox_sts[MBOX_REG_COUNT];
5226 struct addr_ctrl_blk_def *acb = NULL;
5227 uint32_t acb_len = sizeof(struct addr_ctrl_blk_def);
5228 int rval = QLA_SUCCESS;
5229 dma_addr_t acb_dma;
5230
5231 acb = dma_alloc_coherent(&ha->pdev->dev,
5232 sizeof(struct addr_ctrl_blk_def),
5233 &acb_dma, GFP_KERNEL);
5234 if (!acb) {
5235 ql4_printk(KERN_ERR, ha, "%s: Unable to alloc acb\n",
5236 __func__);
5237 rval = -ENOMEM;
5238 goto exit_port_reset;
5239 }
5240
5241 memset(acb, 0, acb_len);
5242
5243 rval = qla4xxx_get_acb(ha, acb_dma, PRIMARI_ACB, acb_len);
5244 if (rval != QLA_SUCCESS) {
5245 rval = -EIO;
5246 goto exit_free_acb;
5247 }
5248
5249 rval = qla4xxx_disable_acb(ha);
5250 if (rval != QLA_SUCCESS) {
5251 rval = -EIO;
5252 goto exit_free_acb;
5253 }
5254
5255 wait_for_completion_timeout(&ha->disable_acb_comp,
5256 DISABLE_ACB_TOV * HZ);
5257
5258 rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], acb_dma);
5259 if (rval != QLA_SUCCESS) {
5260 rval = -EIO;
5261 goto exit_free_acb;
5262 }
5263
5264exit_free_acb:
5265 dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk_def),
5266 acb, acb_dma);
5267exit_port_reset:
5268 DEBUG2(ql4_printk(KERN_INFO, ha, "%s %s\n", __func__,
5269 rval == QLA_SUCCESS ? "SUCCEEDED" : "FAILED"));
5270 return rval;
5271}
5272
5273static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type)
5274{
5275 struct scsi_qla_host *ha = to_qla_host(shost);
5276 int rval = QLA_SUCCESS;
5277
5278 if (ql4xdontresethba) {
5279 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Don't Reset HBA\n",
5280 __func__));
5281 rval = -EPERM;
5282 goto exit_host_reset;
5283 }
5284
5285 rval = qla4xxx_wait_for_hba_online(ha);
5286 if (rval != QLA_SUCCESS) {
5287 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Unable to reset host "
5288 "adapter\n", __func__));
5289 rval = -EIO;
5290 goto exit_host_reset;
5291 }
5292
5293 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
5294 goto recover_adapter;
5295
5296 switch (reset_type) {
5297 case SCSI_ADAPTER_RESET:
5298 set_bit(DPC_RESET_HA, &ha->dpc_flags);
5299 break;
5300 case SCSI_FIRMWARE_RESET:
5301 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
5302 if (is_qla8022(ha))
5303 /* set firmware context reset */
5304 set_bit(DPC_RESET_HA_FW_CONTEXT,
5305 &ha->dpc_flags);
5306 else {
5307 rval = qla4xxx_context_reset(ha);
5308 goto exit_host_reset;
5309 }
5310 }
5311 break;
5312 }
5313
5314recover_adapter:
5315 rval = qla4xxx_recover_adapter(ha);
5316 if (rval != QLA_SUCCESS) {
5317 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: recover adapter fail\n",
5318 __func__));
5319 rval = -EIO;
5320 }
5321
5322exit_host_reset:
5323 return rval;
5324}
5325
Lalit Chandivade2232be02010-07-30 14:38:47 +05305326/* PCI AER driver recovers from all correctable errors w/o
5327 * driver intervention. For uncorrectable errors PCI AER
5328 * driver calls the following device driver's callbacks
5329 *
5330 * - Fatal Errors - link_reset
5331 * - Non-Fatal Errors - driver's pci_error_detected() which
5332 * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
5333 *
5334 * PCI AER driver calls
5335 * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
5336 * returns RECOVERED or NEED_RESET if fw_hung
5337 * NEED_RESET - driver's slot_reset()
5338 * DISCONNECT - device is dead & cannot recover
5339 * RECOVERED - driver's pci_resume()
5340 */
5341static pci_ers_result_t
5342qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
5343{
5344 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
5345
5346 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
5347 ha->host_no, __func__, state);
5348
5349 if (!is_aer_supported(ha))
5350 return PCI_ERS_RESULT_NONE;
5351
5352 switch (state) {
5353 case pci_channel_io_normal:
5354 clear_bit(AF_EEH_BUSY, &ha->flags);
5355 return PCI_ERS_RESULT_CAN_RECOVER;
5356 case pci_channel_io_frozen:
5357 set_bit(AF_EEH_BUSY, &ha->flags);
5358 qla4xxx_mailbox_premature_completion(ha);
5359 qla4xxx_free_irqs(ha);
5360 pci_disable_device(pdev);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07005361 /* Return back all IOs */
5362 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
Lalit Chandivade2232be02010-07-30 14:38:47 +05305363 return PCI_ERS_RESULT_NEED_RESET;
5364 case pci_channel_io_perm_failure:
5365 set_bit(AF_EEH_BUSY, &ha->flags);
5366 set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
5367 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
5368 return PCI_ERS_RESULT_DISCONNECT;
5369 }
5370 return PCI_ERS_RESULT_NEED_RESET;
5371}
5372
5373/**
5374 * qla4xxx_pci_mmio_enabled() gets called if
5375 * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
5376 * and read/write to the device still works.
5377 **/
5378static pci_ers_result_t
5379qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
5380{
5381 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
5382
5383 if (!is_aer_supported(ha))
5384 return PCI_ERS_RESULT_NONE;
5385
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07005386 return PCI_ERS_RESULT_RECOVERED;
Lalit Chandivade2232be02010-07-30 14:38:47 +05305387}
5388
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07005389static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
Lalit Chandivade2232be02010-07-30 14:38:47 +05305390{
5391 uint32_t rval = QLA_ERROR;
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07005392 uint32_t ret = 0;
Lalit Chandivade2232be02010-07-30 14:38:47 +05305393 int fn;
5394 struct pci_dev *other_pdev = NULL;
5395
5396 ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
5397
5398 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
5399
5400 if (test_bit(AF_ONLINE, &ha->flags)) {
5401 clear_bit(AF_ONLINE, &ha->flags);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05005402 clear_bit(AF_LINK_UP, &ha->flags);
5403 iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
Lalit Chandivade2232be02010-07-30 14:38:47 +05305404 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
Lalit Chandivade2232be02010-07-30 14:38:47 +05305405 }
5406
5407 fn = PCI_FUNC(ha->pdev->devfn);
5408 while (fn > 0) {
5409 fn--;
5410 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
5411 "func %x\n", ha->host_no, __func__, fn);
5412 /* Get the pci device given the domain, bus,
5413 * slot/function number */
5414 other_pdev =
5415 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
5416 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
5417 fn));
5418
5419 if (!other_pdev)
5420 continue;
5421
5422 if (atomic_read(&other_pdev->enable_cnt)) {
5423 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
5424 "func in enabled state%x\n", ha->host_no,
5425 __func__, fn);
5426 pci_dev_put(other_pdev);
5427 break;
5428 }
5429 pci_dev_put(other_pdev);
5430 }
5431
5432 /* The first function on the card, the reset owner will
5433 * start & initialize the firmware. The other functions
5434 * on the card will reset the firmware context
5435 */
5436 if (!fn) {
5437 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
5438 "0x%x is the owner\n", ha->host_no, __func__,
5439 ha->pdev->devfn);
5440
5441 qla4_8xxx_idc_lock(ha);
5442 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
5443 QLA82XX_DEV_COLD);
5444
5445 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
5446 QLA82XX_IDC_VERSION);
5447
5448 qla4_8xxx_idc_unlock(ha);
5449 clear_bit(AF_FW_RECOVERY, &ha->flags);
Mike Christie13483732011-12-01 21:38:41 -06005450 rval = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
Lalit Chandivade2232be02010-07-30 14:38:47 +05305451 qla4_8xxx_idc_lock(ha);
5452
5453 if (rval != QLA_SUCCESS) {
5454 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
5455 "FAILED\n", ha->host_no, __func__);
5456 qla4_8xxx_clear_drv_active(ha);
5457 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
5458 QLA82XX_DEV_FAILED);
5459 } else {
5460 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
5461 "READY\n", ha->host_no, __func__);
5462 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
5463 QLA82XX_DEV_READY);
5464 /* Clear driver state register */
5465 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
5466 qla4_8xxx_set_drv_active(ha);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07005467 ret = qla4xxx_request_irqs(ha);
5468 if (ret) {
5469 ql4_printk(KERN_WARNING, ha, "Failed to "
5470 "reserve interrupt %d already in use.\n",
5471 ha->pdev->irq);
5472 rval = QLA_ERROR;
5473 } else {
5474 ha->isp_ops->enable_intrs(ha);
5475 rval = QLA_SUCCESS;
5476 }
Lalit Chandivade2232be02010-07-30 14:38:47 +05305477 }
5478 qla4_8xxx_idc_unlock(ha);
5479 } else {
5480 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
5481 "the reset owner\n", ha->host_no, __func__,
5482 ha->pdev->devfn);
5483 if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
5484 QLA82XX_DEV_READY)) {
5485 clear_bit(AF_FW_RECOVERY, &ha->flags);
Mike Christie13483732011-12-01 21:38:41 -06005486 rval = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07005487 if (rval == QLA_SUCCESS) {
5488 ret = qla4xxx_request_irqs(ha);
5489 if (ret) {
5490 ql4_printk(KERN_WARNING, ha, "Failed to"
5491 " reserve interrupt %d already in"
5492 " use.\n", ha->pdev->irq);
5493 rval = QLA_ERROR;
5494 } else {
5495 ha->isp_ops->enable_intrs(ha);
5496 rval = QLA_SUCCESS;
5497 }
5498 }
Lalit Chandivade2232be02010-07-30 14:38:47 +05305499 qla4_8xxx_idc_lock(ha);
5500 qla4_8xxx_set_drv_active(ha);
5501 qla4_8xxx_idc_unlock(ha);
5502 }
5503 }
5504 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
5505 return rval;
5506}
5507
5508static pci_ers_result_t
5509qla4xxx_pci_slot_reset(struct pci_dev *pdev)
5510{
5511 pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
5512 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
5513 int rc;
5514
5515 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
5516 ha->host_no, __func__);
5517
5518 if (!is_aer_supported(ha))
5519 return PCI_ERS_RESULT_NONE;
5520
5521 /* Restore the saved state of PCIe device -
5522 * BAR registers, PCI Config space, PCIX, MSI,
5523 * IOV states
5524 */
5525 pci_restore_state(pdev);
5526
5527 /* pci_restore_state() clears the saved_state flag of the device
5528 * save restored state which resets saved_state flag
5529 */
5530 pci_save_state(pdev);
5531
5532 /* Initialize device or resume if in suspended state */
5533 rc = pci_enable_device(pdev);
5534 if (rc) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005535 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Can't re-enable "
Lalit Chandivade2232be02010-07-30 14:38:47 +05305536 "device after reset\n", ha->host_no, __func__);
5537 goto exit_slot_reset;
5538 }
5539
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07005540 ha->isp_ops->disable_intrs(ha);
Lalit Chandivade2232be02010-07-30 14:38:47 +05305541
5542 if (is_qla8022(ha)) {
5543 if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
5544 ret = PCI_ERS_RESULT_RECOVERED;
5545 goto exit_slot_reset;
5546 } else
5547 goto exit_slot_reset;
5548 }
5549
5550exit_slot_reset:
5551 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
5552 "device after reset\n", ha->host_no, __func__, ret);
5553 return ret;
5554}
5555
5556static void
5557qla4xxx_pci_resume(struct pci_dev *pdev)
5558{
5559 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
5560 int ret;
5561
5562 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
5563 ha->host_no, __func__);
5564
5565 ret = qla4xxx_wait_for_hba_online(ha);
5566 if (ret != QLA_SUCCESS) {
5567 ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
5568 "resume I/O from slot/link_reset\n", ha->host_no,
5569 __func__);
5570 }
5571
5572 pci_cleanup_aer_uncorrect_error_status(pdev);
5573 clear_bit(AF_EEH_BUSY, &ha->flags);
5574}
5575
5576static struct pci_error_handlers qla4xxx_err_handler = {
5577 .error_detected = qla4xxx_pci_error_detected,
5578 .mmio_enabled = qla4xxx_pci_mmio_enabled,
5579 .slot_reset = qla4xxx_pci_slot_reset,
5580 .resume = qla4xxx_pci_resume,
5581};
5582
David Somayajuluafaf5a22006-09-19 10:28:00 -07005583static struct pci_device_id qla4xxx_pci_tbl[] = {
5584 {
5585 .vendor = PCI_VENDOR_ID_QLOGIC,
5586 .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
5587 .subvendor = PCI_ANY_ID,
5588 .subdevice = PCI_ANY_ID,
5589 },
5590 {
5591 .vendor = PCI_VENDOR_ID_QLOGIC,
5592 .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
5593 .subvendor = PCI_ANY_ID,
5594 .subdevice = PCI_ANY_ID,
5595 },
David C Somayajulud9150582006-11-15 17:38:40 -08005596 {
5597 .vendor = PCI_VENDOR_ID_QLOGIC,
5598 .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
5599 .subvendor = PCI_ANY_ID,
5600 .subdevice = PCI_ANY_ID,
5601 },
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05305602 {
5603 .vendor = PCI_VENDOR_ID_QLOGIC,
5604 .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
5605 .subvendor = PCI_ANY_ID,
5606 .subdevice = PCI_ANY_ID,
5607 },
David Somayajuluafaf5a22006-09-19 10:28:00 -07005608 {0, 0},
5609};
5610MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
5611
Adrian Bunk47975472007-04-26 00:35:16 -07005612static struct pci_driver qla4xxx_pci_driver = {
David Somayajuluafaf5a22006-09-19 10:28:00 -07005613 .name = DRIVER_NAME,
5614 .id_table = qla4xxx_pci_tbl,
5615 .probe = qla4xxx_probe_adapter,
5616 .remove = qla4xxx_remove_adapter,
Lalit Chandivade2232be02010-07-30 14:38:47 +05305617 .err_handler = &qla4xxx_err_handler,
David Somayajuluafaf5a22006-09-19 10:28:00 -07005618};
5619
5620static int __init qla4xxx_module_init(void)
5621{
5622 int ret;
5623
5624 /* Allocate cache for SRBs. */
5625 srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09005626 SLAB_HWCACHE_ALIGN, NULL);
David Somayajuluafaf5a22006-09-19 10:28:00 -07005627 if (srb_cachep == NULL) {
5628 printk(KERN_ERR
5629 "%s: Unable to allocate SRB cache..."
5630 "Failing load!\n", DRIVER_NAME);
5631 ret = -ENOMEM;
5632 goto no_srp_cache;
5633 }
5634
5635 /* Derive version string. */
5636 strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
Andrew Vasquez11010fe2006-10-06 09:54:59 -07005637 if (ql4xextended_error_logging)
David Somayajuluafaf5a22006-09-19 10:28:00 -07005638 strcat(qla4xxx_version_str, "-debug");
5639
5640 qla4xxx_scsi_transport =
5641 iscsi_register_transport(&qla4xxx_iscsi_transport);
5642 if (!qla4xxx_scsi_transport){
5643 ret = -ENODEV;
5644 goto release_srb_cache;
5645 }
5646
David Somayajuluafaf5a22006-09-19 10:28:00 -07005647 ret = pci_register_driver(&qla4xxx_pci_driver);
5648 if (ret)
5649 goto unregister_transport;
5650
5651 printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
5652 return 0;
Doug Maxey5ae16db2006-10-05 23:50:07 -05005653
David Somayajuluafaf5a22006-09-19 10:28:00 -07005654unregister_transport:
5655 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
5656release_srb_cache:
5657 kmem_cache_destroy(srb_cachep);
5658no_srp_cache:
5659 return ret;
5660}
5661
5662static void __exit qla4xxx_module_exit(void)
5663{
David Somayajuluafaf5a22006-09-19 10:28:00 -07005664 pci_unregister_driver(&qla4xxx_pci_driver);
5665 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
5666 kmem_cache_destroy(srb_cachep);
5667}
5668
5669module_init(qla4xxx_module_init);
5670module_exit(qla4xxx_module_exit);
5671
5672MODULE_AUTHOR("QLogic Corporation");
5673MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
5674MODULE_LICENSE("GPL");
5675MODULE_VERSION(QLA4XXX_DRIVER_VERSION);