blob: 0c9ee9378094e0fd1682d9baf20465346049a54b [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,
38 "Set to disable exporting boot targets to sysfs\n"
39 " 0 - Export boot targets\n"
40 " 1 - Do not export boot targets (Default)");
41
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,
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +053045 "Don't reset the HBA for driver recovery \n"
46 " 0 - It will reset HBA (Default)\n"
47 " 1 - It will NOT reset HBA");
David Somayajuluafaf5a22006-09-19 10:28:00 -070048
Andrew Vasquez11010fe2006-10-06 09:54:59 -070049int ql4xextended_error_logging = 0; /* 0 = off, 1 = log errors */
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,
David Somayajuluafaf5a22006-09-19 10:28:00 -070052 "Option to enable extended error logging, "
53 "Default is 0 - no logging, 1 - debug logging");
54
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +053055int ql4xenablemsix = 1;
56module_param(ql4xenablemsix, int, S_IRUGO|S_IWUSR);
57MODULE_PARM_DESC(ql4xenablemsix,
58 "Set to enable MSI or MSI-X interrupt mechanism.\n"
59 " 0 = enable INTx interrupt mechanism.\n"
60 " 1 = enable MSI-X interrupt mechanism (Default).\n"
61 " 2 = enable MSI interrupt mechanism.");
David C Somayajulu477ffb92007-01-22 12:26:11 -080062
Mike Christied510d962008-07-11 19:50:33 -050063#define QL4_DEF_QDEPTH 32
Vikas Chaudhary8bb40332011-03-21 03:34:31 -070064static int ql4xmaxqdepth = QL4_DEF_QDEPTH;
65module_param(ql4xmaxqdepth, int, S_IRUGO | S_IWUSR);
66MODULE_PARM_DESC(ql4xmaxqdepth,
67 "Maximum queue depth to report for target devices.\n"
68 " Default: 32.");
Mike Christied510d962008-07-11 19:50:33 -050069
Vikas Chaudhary30387272011-03-21 03:34:32 -070070static int ql4xsess_recovery_tmo = QL4_SESS_RECOVERY_TMO;
71module_param(ql4xsess_recovery_tmo, int, S_IRUGO);
72MODULE_PARM_DESC(ql4xsess_recovery_tmo,
73 "Target Session Recovery Timeout.\n"
Mike Christie13483732011-12-01 21:38:41 -060074 " Default: 120 sec.");
Vikas Chaudhary30387272011-03-21 03:34:32 -070075
Manish Rangankarb3a271a2011-07-25 13:48:53 -050076static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -070077/*
78 * SCSI host template entry points
79 */
Adrian Bunk47975472007-04-26 00:35:16 -070080static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -070081
82/*
83 * iSCSI template entry points
84 */
David Somayajuluafaf5a22006-09-19 10:28:00 -070085static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
86 enum iscsi_param param, char *buf);
Mike Christieaa1e93a2007-05-30 12:57:09 -050087static int qla4xxx_host_get_param(struct Scsi_Host *shost,
88 enum iscsi_host_param param, char *buf);
Mike Christie00c31882011-10-06 03:56:59 -050089static int qla4xxx_iface_set_param(struct Scsi_Host *shost, void *data,
90 uint32_t len);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -050091static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
92 enum iscsi_param_type param_type,
93 int param, char *buf);
Mike Christie5c656af2009-07-15 15:02:59 -050094static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
Manish Rangankarb3a271a2011-07-25 13:48:53 -050095static struct iscsi_endpoint *qla4xxx_ep_connect(struct Scsi_Host *shost,
96 struct sockaddr *dst_addr,
97 int non_blocking);
98static int qla4xxx_ep_poll(struct iscsi_endpoint *ep, int timeout_ms);
99static void qla4xxx_ep_disconnect(struct iscsi_endpoint *ep);
100static int qla4xxx_get_ep_param(struct iscsi_endpoint *ep,
101 enum iscsi_param param, char *buf);
102static int qla4xxx_conn_start(struct iscsi_cls_conn *conn);
103static struct iscsi_cls_conn *
104qla4xxx_conn_create(struct iscsi_cls_session *cls_sess, uint32_t conn_idx);
105static int qla4xxx_conn_bind(struct iscsi_cls_session *cls_session,
106 struct iscsi_cls_conn *cls_conn,
107 uint64_t transport_fd, int is_leading);
108static void qla4xxx_conn_destroy(struct iscsi_cls_conn *conn);
109static struct iscsi_cls_session *
110qla4xxx_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max,
111 uint16_t qdepth, uint32_t initial_cmdsn);
112static void qla4xxx_session_destroy(struct iscsi_cls_session *sess);
113static void qla4xxx_task_work(struct work_struct *wdata);
114static int qla4xxx_alloc_pdu(struct iscsi_task *, uint8_t);
115static int qla4xxx_task_xmit(struct iscsi_task *);
116static void qla4xxx_task_cleanup(struct iscsi_task *);
117static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session);
118static void qla4xxx_conn_get_stats(struct iscsi_cls_conn *cls_conn,
119 struct iscsi_stats *stats);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700120/*
121 * SCSI host template entry points
122 */
Jeff Garzikf2812332010-11-16 02:10:29 -0500123static int qla4xxx_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmd);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530124static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700125static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
Mike Christiece545032008-02-29 18:25:20 -0600126static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700127static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
128static int qla4xxx_slave_alloc(struct scsi_device *device);
129static int qla4xxx_slave_configure(struct scsi_device *device);
130static void qla4xxx_slave_destroy(struct scsi_device *sdev);
Mike Christie3128c6c2011-07-25 13:48:42 -0500131static mode_t ql4_attr_is_visible(int param_type, int param);
Vikas Chaudhary95d31262011-08-12 02:51:29 -0700132static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700133
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530134static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
135 QLA82XX_LEGACY_INTR_CONFIG;
136
David Somayajuluafaf5a22006-09-19 10:28:00 -0700137static struct scsi_host_template qla4xxx_driver_template = {
138 .module = THIS_MODULE,
139 .name = DRIVER_NAME,
140 .proc_name = DRIVER_NAME,
141 .queuecommand = qla4xxx_queuecommand,
142
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530143 .eh_abort_handler = qla4xxx_eh_abort,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700144 .eh_device_reset_handler = qla4xxx_eh_device_reset,
Mike Christiece545032008-02-29 18:25:20 -0600145 .eh_target_reset_handler = qla4xxx_eh_target_reset,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700146 .eh_host_reset_handler = qla4xxx_eh_host_reset,
Mike Christie5c656af2009-07-15 15:02:59 -0500147 .eh_timed_out = qla4xxx_eh_cmd_timed_out,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700148
149 .slave_configure = qla4xxx_slave_configure,
150 .slave_alloc = qla4xxx_slave_alloc,
151 .slave_destroy = qla4xxx_slave_destroy,
152
153 .this_id = -1,
154 .cmd_per_lun = 3,
155 .use_clustering = ENABLE_CLUSTERING,
156 .sg_tablesize = SG_ALL,
157
158 .max_sectors = 0xFFFF,
Harish Zunjarrao7ad633c2011-05-17 23:17:11 -0700159 .shost_attrs = qla4xxx_host_attrs,
Vikas Chaudhary95d31262011-08-12 02:51:29 -0700160 .host_reset = qla4xxx_host_reset,
Vikas Chaudharya3559432011-07-25 13:48:51 -0500161 .vendor_id = SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_QLOGIC,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700162};
163
164static struct iscsi_transport qla4xxx_iscsi_transport = {
165 .owner = THIS_MODULE,
166 .name = DRIVER_NAME,
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500167 .caps = CAP_TEXT_NEGO |
168 CAP_DATA_PATH_OFFLOAD | CAP_HDRDGST |
169 CAP_DATADGST | CAP_LOGIN_OFFLOAD |
170 CAP_MULTI_R2T,
Mike Christie3128c6c2011-07-25 13:48:42 -0500171 .attr_is_visible = ql4_attr_is_visible,
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500172 .create_session = qla4xxx_session_create,
173 .destroy_session = qla4xxx_session_destroy,
174 .start_conn = qla4xxx_conn_start,
175 .create_conn = qla4xxx_conn_create,
176 .bind_conn = qla4xxx_conn_bind,
177 .stop_conn = iscsi_conn_stop,
178 .destroy_conn = qla4xxx_conn_destroy,
179 .set_param = iscsi_set_param,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700180 .get_conn_param = qla4xxx_conn_get_param,
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500181 .get_session_param = iscsi_session_get_param,
182 .get_ep_param = qla4xxx_get_ep_param,
183 .ep_connect = qla4xxx_ep_connect,
184 .ep_poll = qla4xxx_ep_poll,
185 .ep_disconnect = qla4xxx_ep_disconnect,
186 .get_stats = qla4xxx_conn_get_stats,
187 .send_pdu = iscsi_conn_send_pdu,
188 .xmit_task = qla4xxx_task_xmit,
189 .cleanup_task = qla4xxx_task_cleanup,
190 .alloc_pdu = qla4xxx_alloc_pdu,
191
Mike Christieaa1e93a2007-05-30 12:57:09 -0500192 .get_host_param = qla4xxx_host_get_param,
Mike Christied00efe32011-07-25 13:48:38 -0500193 .set_iface_param = qla4xxx_iface_set_param,
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500194 .get_iface_param = qla4xxx_get_iface_param,
Vikas Chaudharya3559432011-07-25 13:48:51 -0500195 .bsg_request = qla4xxx_bsg_request,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700196};
197
198static struct scsi_transport_template *qla4xxx_scsi_transport;
199
Mike Christie3128c6c2011-07-25 13:48:42 -0500200static mode_t ql4_attr_is_visible(int param_type, int param)
201{
202 switch (param_type) {
Mike Christief27fb2e2011-07-25 13:48:45 -0500203 case ISCSI_HOST_PARAM:
204 switch (param) {
205 case ISCSI_HOST_PARAM_HWADDRESS:
206 case ISCSI_HOST_PARAM_IPADDRESS:
207 case ISCSI_HOST_PARAM_INITIATOR_NAME:
208 return S_IRUGO;
209 default:
210 return 0;
211 }
Mike Christie3128c6c2011-07-25 13:48:42 -0500212 case ISCSI_PARAM:
213 switch (param) {
Mike Christie590134f2011-10-17 22:42:13 -0500214 case ISCSI_PARAM_PERSISTENT_ADDRESS:
215 case ISCSI_PARAM_PERSISTENT_PORT:
Mike Christie3128c6c2011-07-25 13:48:42 -0500216 case ISCSI_PARAM_CONN_ADDRESS:
217 case ISCSI_PARAM_CONN_PORT:
Mike Christie1d063c12011-07-25 13:48:43 -0500218 case ISCSI_PARAM_TARGET_NAME:
219 case ISCSI_PARAM_TPGT:
220 case ISCSI_PARAM_TARGET_ALIAS:
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500221 case ISCSI_PARAM_MAX_BURST:
222 case ISCSI_PARAM_MAX_R2T:
223 case ISCSI_PARAM_FIRST_BURST:
224 case ISCSI_PARAM_MAX_RECV_DLENGTH:
225 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
Mike Christiede379202011-08-14 20:42:56 -0500226 case ISCSI_PARAM_IFACE_NAME:
Mike Christie3128c6c2011-07-25 13:48:42 -0500227 return S_IRUGO;
228 default:
229 return 0;
230 }
Mike Christieb78dbba2011-07-25 13:48:44 -0500231 case ISCSI_NET_PARAM:
232 switch (param) {
233 case ISCSI_NET_PARAM_IPV4_ADDR:
234 case ISCSI_NET_PARAM_IPV4_SUBNET:
235 case ISCSI_NET_PARAM_IPV4_GW:
236 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
237 case ISCSI_NET_PARAM_IFACE_ENABLE:
238 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
239 case ISCSI_NET_PARAM_IPV6_ADDR:
240 case ISCSI_NET_PARAM_IPV6_ROUTER:
241 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
242 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
Vikas Chaudhary6ac73e82011-07-25 13:48:49 -0500243 case ISCSI_NET_PARAM_VLAN_ID:
244 case ISCSI_NET_PARAM_VLAN_PRIORITY:
245 case ISCSI_NET_PARAM_VLAN_ENABLED:
Vikas Chaudhary943c1572011-08-01 03:26:13 -0700246 case ISCSI_NET_PARAM_MTU:
Vikas Chaudhary2ada7fc2011-08-01 03:26:19 -0700247 case ISCSI_NET_PARAM_PORT:
Mike Christieb78dbba2011-07-25 13:48:44 -0500248 return S_IRUGO;
249 default:
250 return 0;
251 }
Mike Christie3128c6c2011-07-25 13:48:42 -0500252 }
253
254 return 0;
255}
256
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500257static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
258 enum iscsi_param_type param_type,
259 int param, char *buf)
260{
261 struct Scsi_Host *shost = iscsi_iface_to_shost(iface);
262 struct scsi_qla_host *ha = to_qla_host(shost);
263 int len = -ENOSYS;
264
265 if (param_type != ISCSI_NET_PARAM)
266 return -ENOSYS;
267
268 switch (param) {
269 case ISCSI_NET_PARAM_IPV4_ADDR:
270 len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
271 break;
272 case ISCSI_NET_PARAM_IPV4_SUBNET:
273 len = sprintf(buf, "%pI4\n", &ha->ip_config.subnet_mask);
274 break;
275 case ISCSI_NET_PARAM_IPV4_GW:
276 len = sprintf(buf, "%pI4\n", &ha->ip_config.gateway);
277 break;
278 case ISCSI_NET_PARAM_IFACE_ENABLE:
279 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
280 len = sprintf(buf, "%s\n",
281 (ha->ip_config.ipv4_options &
282 IPOPT_IPV4_PROTOCOL_ENABLE) ?
283 "enabled" : "disabled");
284 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
285 len = sprintf(buf, "%s\n",
286 (ha->ip_config.ipv6_options &
287 IPV6_OPT_IPV6_PROTOCOL_ENABLE) ?
288 "enabled" : "disabled");
289 break;
290 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
291 len = sprintf(buf, "%s\n",
292 (ha->ip_config.tcp_options & TCPOPT_DHCP_ENABLE) ?
293 "dhcp" : "static");
294 break;
295 case ISCSI_NET_PARAM_IPV6_ADDR:
296 if (iface->iface_num == 0)
297 len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr0);
298 if (iface->iface_num == 1)
299 len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr1);
300 break;
301 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
302 len = sprintf(buf, "%pI6\n",
303 &ha->ip_config.ipv6_link_local_addr);
304 break;
305 case ISCSI_NET_PARAM_IPV6_ROUTER:
306 len = sprintf(buf, "%pI6\n",
307 &ha->ip_config.ipv6_default_router_addr);
308 break;
309 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
310 len = sprintf(buf, "%s\n",
311 (ha->ip_config.ipv6_addl_options &
312 IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) ?
313 "nd" : "static");
314 break;
315 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
316 len = sprintf(buf, "%s\n",
317 (ha->ip_config.ipv6_addl_options &
318 IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR) ?
319 "auto" : "static");
320 break;
Vikas Chaudhary6ac73e82011-07-25 13:48:49 -0500321 case ISCSI_NET_PARAM_VLAN_ID:
322 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
323 len = sprintf(buf, "%d\n",
324 (ha->ip_config.ipv4_vlan_tag &
325 ISCSI_MAX_VLAN_ID));
326 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
327 len = sprintf(buf, "%d\n",
328 (ha->ip_config.ipv6_vlan_tag &
329 ISCSI_MAX_VLAN_ID));
330 break;
331 case ISCSI_NET_PARAM_VLAN_PRIORITY:
332 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
333 len = sprintf(buf, "%d\n",
334 ((ha->ip_config.ipv4_vlan_tag >> 13) &
335 ISCSI_MAX_VLAN_PRIORITY));
336 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
337 len = sprintf(buf, "%d\n",
338 ((ha->ip_config.ipv6_vlan_tag >> 13) &
339 ISCSI_MAX_VLAN_PRIORITY));
340 break;
341 case ISCSI_NET_PARAM_VLAN_ENABLED:
342 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
343 len = sprintf(buf, "%s\n",
344 (ha->ip_config.ipv4_options &
345 IPOPT_VLAN_TAGGING_ENABLE) ?
346 "enabled" : "disabled");
347 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
348 len = sprintf(buf, "%s\n",
349 (ha->ip_config.ipv6_options &
350 IPV6_OPT_VLAN_TAGGING_ENABLE) ?
351 "enabled" : "disabled");
352 break;
Vikas Chaudhary943c1572011-08-01 03:26:13 -0700353 case ISCSI_NET_PARAM_MTU:
354 len = sprintf(buf, "%d\n", ha->ip_config.eth_mtu_size);
355 break;
Vikas Chaudhary2ada7fc2011-08-01 03:26:19 -0700356 case ISCSI_NET_PARAM_PORT:
357 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
358 len = sprintf(buf, "%d\n", ha->ip_config.ipv4_port);
359 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
360 len = sprintf(buf, "%d\n", ha->ip_config.ipv6_port);
361 break;
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500362 default:
363 len = -ENOSYS;
364 }
365
366 return len;
367}
368
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500369static struct iscsi_endpoint *
370qla4xxx_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
371 int non_blocking)
372{
373 int ret;
374 struct iscsi_endpoint *ep;
375 struct qla_endpoint *qla_ep;
376 struct scsi_qla_host *ha;
377 struct sockaddr_in *addr;
378 struct sockaddr_in6 *addr6;
379
380 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
381 if (!shost) {
382 ret = -ENXIO;
383 printk(KERN_ERR "%s: shost is NULL\n",
384 __func__);
385 return ERR_PTR(ret);
386 }
387
388 ha = iscsi_host_priv(shost);
389
390 ep = iscsi_create_endpoint(sizeof(struct qla_endpoint));
391 if (!ep) {
392 ret = -ENOMEM;
393 return ERR_PTR(ret);
394 }
395
396 qla_ep = ep->dd_data;
397 memset(qla_ep, 0, sizeof(struct qla_endpoint));
398 if (dst_addr->sa_family == AF_INET) {
399 memcpy(&qla_ep->dst_addr, dst_addr, sizeof(struct sockaddr_in));
400 addr = (struct sockaddr_in *)&qla_ep->dst_addr;
401 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI4\n", __func__,
402 (char *)&addr->sin_addr));
403 } else if (dst_addr->sa_family == AF_INET6) {
404 memcpy(&qla_ep->dst_addr, dst_addr,
405 sizeof(struct sockaddr_in6));
406 addr6 = (struct sockaddr_in6 *)&qla_ep->dst_addr;
407 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI6\n", __func__,
408 (char *)&addr6->sin6_addr));
409 }
410
411 qla_ep->host = shost;
412
413 return ep;
414}
415
416static int qla4xxx_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
417{
418 struct qla_endpoint *qla_ep;
419 struct scsi_qla_host *ha;
420 int ret = 0;
421
422 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
423 qla_ep = ep->dd_data;
424 ha = to_qla_host(qla_ep->host);
425
Mike Christie13483732011-12-01 21:38:41 -0600426 if (adapter_up(ha) && !test_bit(AF_BUILD_DDB_LIST, &ha->flags))
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500427 ret = 1;
428
429 return ret;
430}
431
432static void qla4xxx_ep_disconnect(struct iscsi_endpoint *ep)
433{
434 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
435 iscsi_destroy_endpoint(ep);
436}
437
438static int qla4xxx_get_ep_param(struct iscsi_endpoint *ep,
439 enum iscsi_param param,
440 char *buf)
441{
442 struct qla_endpoint *qla_ep = ep->dd_data;
443 struct sockaddr *dst_addr;
444
445 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
446
447 switch (param) {
448 case ISCSI_PARAM_CONN_PORT:
449 case ISCSI_PARAM_CONN_ADDRESS:
450 if (!qla_ep)
451 return -ENOTCONN;
452
453 dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
454 if (!dst_addr)
455 return -ENOTCONN;
456
457 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
458 &qla_ep->dst_addr, param, buf);
459 default:
460 return -ENOSYS;
461 }
462}
463
464static void qla4xxx_conn_get_stats(struct iscsi_cls_conn *cls_conn,
465 struct iscsi_stats *stats)
466{
467 struct iscsi_session *sess;
468 struct iscsi_cls_session *cls_sess;
469 struct ddb_entry *ddb_entry;
470 struct scsi_qla_host *ha;
471 struct ql_iscsi_stats *ql_iscsi_stats;
472 int stats_size;
473 int ret;
474 dma_addr_t iscsi_stats_dma;
475
476 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
477
478 cls_sess = iscsi_conn_to_session(cls_conn);
479 sess = cls_sess->dd_data;
480 ddb_entry = sess->dd_data;
481 ha = ddb_entry->ha;
482
483 stats_size = PAGE_ALIGN(sizeof(struct ql_iscsi_stats));
484 /* Allocate memory */
485 ql_iscsi_stats = dma_alloc_coherent(&ha->pdev->dev, stats_size,
486 &iscsi_stats_dma, GFP_KERNEL);
487 if (!ql_iscsi_stats) {
488 ql4_printk(KERN_ERR, ha,
489 "Unable to allocate memory for iscsi stats\n");
490 goto exit_get_stats;
491 }
492
493 ret = qla4xxx_get_mgmt_data(ha, ddb_entry->fw_ddb_index, stats_size,
494 iscsi_stats_dma);
495 if (ret != QLA_SUCCESS) {
496 ql4_printk(KERN_ERR, ha,
497 "Unable to retreive iscsi stats\n");
498 goto free_stats;
499 }
500
501 /* octets */
502 stats->txdata_octets = le64_to_cpu(ql_iscsi_stats->tx_data_octets);
503 stats->rxdata_octets = le64_to_cpu(ql_iscsi_stats->rx_data_octets);
504 /* xmit pdus */
505 stats->noptx_pdus = le32_to_cpu(ql_iscsi_stats->tx_nopout_pdus);
506 stats->scsicmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_cmd_pdus);
507 stats->tmfcmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_tmf_cmd_pdus);
508 stats->login_pdus = le32_to_cpu(ql_iscsi_stats->tx_login_cmd_pdus);
509 stats->text_pdus = le32_to_cpu(ql_iscsi_stats->tx_text_cmd_pdus);
510 stats->dataout_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_write_pdus);
511 stats->logout_pdus = le32_to_cpu(ql_iscsi_stats->tx_logout_cmd_pdus);
512 stats->snack_pdus = le32_to_cpu(ql_iscsi_stats->tx_snack_req_pdus);
513 /* recv pdus */
514 stats->noprx_pdus = le32_to_cpu(ql_iscsi_stats->rx_nopin_pdus);
515 stats->scsirsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_resp_pdus);
516 stats->tmfrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_tmf_resp_pdus);
517 stats->textrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_text_resp_pdus);
518 stats->datain_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_read_pdus);
519 stats->logoutrsp_pdus =
520 le32_to_cpu(ql_iscsi_stats->rx_logout_resp_pdus);
521 stats->r2t_pdus = le32_to_cpu(ql_iscsi_stats->rx_r2t_pdus);
522 stats->async_pdus = le32_to_cpu(ql_iscsi_stats->rx_async_pdus);
523 stats->rjt_pdus = le32_to_cpu(ql_iscsi_stats->rx_reject_pdus);
524
525free_stats:
526 dma_free_coherent(&ha->pdev->dev, stats_size, ql_iscsi_stats,
527 iscsi_stats_dma);
528exit_get_stats:
529 return;
530}
531
Mike Christie5c656af2009-07-15 15:02:59 -0500532static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
533{
534 struct iscsi_cls_session *session;
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500535 struct iscsi_session *sess;
536 unsigned long flags;
537 enum blk_eh_timer_return ret = BLK_EH_NOT_HANDLED;
Mike Christie5c656af2009-07-15 15:02:59 -0500538
539 session = starget_to_session(scsi_target(sc->device));
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500540 sess = session->dd_data;
Mike Christie5c656af2009-07-15 15:02:59 -0500541
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500542 spin_lock_irqsave(&session->lock, flags);
543 if (session->state == ISCSI_SESSION_FAILED)
544 ret = BLK_EH_RESET_TIMER;
545 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie5c656af2009-07-15 15:02:59 -0500546
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500547 return ret;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700548}
549
Mike Christieaa1e93a2007-05-30 12:57:09 -0500550static int qla4xxx_host_get_param(struct Scsi_Host *shost,
551 enum iscsi_host_param param, char *buf)
552{
553 struct scsi_qla_host *ha = to_qla_host(shost);
554 int len;
555
556 switch (param) {
557 case ISCSI_HOST_PARAM_HWADDRESS:
Michael Chan7ffc49a2007-12-24 21:28:09 -0800558 len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
Mike Christieaa1e93a2007-05-30 12:57:09 -0500559 break;
Mike Christie22236962007-05-30 12:57:24 -0500560 case ISCSI_HOST_PARAM_IPADDRESS:
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500561 len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
Mike Christie22236962007-05-30 12:57:24 -0500562 break;
Mike Christie8ad57812007-05-30 12:57:13 -0500563 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie22236962007-05-30 12:57:24 -0500564 len = sprintf(buf, "%s\n", ha->name_string);
Mike Christie8ad57812007-05-30 12:57:13 -0500565 break;
Mike Christieaa1e93a2007-05-30 12:57:09 -0500566 default:
567 return -ENOSYS;
568 }
569
570 return len;
571}
572
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500573static void qla4xxx_create_ipv4_iface(struct scsi_qla_host *ha)
574{
575 if (ha->iface_ipv4)
576 return;
577
578 /* IPv4 */
579 ha->iface_ipv4 = iscsi_create_iface(ha->host,
580 &qla4xxx_iscsi_transport,
581 ISCSI_IFACE_TYPE_IPV4, 0, 0);
582 if (!ha->iface_ipv4)
583 ql4_printk(KERN_ERR, ha, "Could not create IPv4 iSCSI "
584 "iface0.\n");
585}
586
587static void qla4xxx_create_ipv6_iface(struct scsi_qla_host *ha)
588{
589 if (!ha->iface_ipv6_0)
590 /* IPv6 iface-0 */
591 ha->iface_ipv6_0 = iscsi_create_iface(ha->host,
592 &qla4xxx_iscsi_transport,
593 ISCSI_IFACE_TYPE_IPV6, 0,
594 0);
595 if (!ha->iface_ipv6_0)
596 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
597 "iface0.\n");
598
599 if (!ha->iface_ipv6_1)
600 /* IPv6 iface-1 */
601 ha->iface_ipv6_1 = iscsi_create_iface(ha->host,
602 &qla4xxx_iscsi_transport,
603 ISCSI_IFACE_TYPE_IPV6, 1,
604 0);
605 if (!ha->iface_ipv6_1)
606 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
607 "iface1.\n");
608}
609
610static void qla4xxx_create_ifaces(struct scsi_qla_host *ha)
611{
612 if (ha->ip_config.ipv4_options & IPOPT_IPV4_PROTOCOL_ENABLE)
613 qla4xxx_create_ipv4_iface(ha);
614
615 if (ha->ip_config.ipv6_options & IPV6_OPT_IPV6_PROTOCOL_ENABLE)
616 qla4xxx_create_ipv6_iface(ha);
617}
618
619static void qla4xxx_destroy_ipv4_iface(struct scsi_qla_host *ha)
620{
621 if (ha->iface_ipv4) {
622 iscsi_destroy_iface(ha->iface_ipv4);
623 ha->iface_ipv4 = NULL;
624 }
625}
626
627static void qla4xxx_destroy_ipv6_iface(struct scsi_qla_host *ha)
628{
629 if (ha->iface_ipv6_0) {
630 iscsi_destroy_iface(ha->iface_ipv6_0);
631 ha->iface_ipv6_0 = NULL;
632 }
633 if (ha->iface_ipv6_1) {
634 iscsi_destroy_iface(ha->iface_ipv6_1);
635 ha->iface_ipv6_1 = NULL;
636 }
637}
638
639static void qla4xxx_destroy_ifaces(struct scsi_qla_host *ha)
640{
641 qla4xxx_destroy_ipv4_iface(ha);
642 qla4xxx_destroy_ipv6_iface(ha);
643}
644
Mike Christied00efe32011-07-25 13:48:38 -0500645static void qla4xxx_set_ipv6(struct scsi_qla_host *ha,
646 struct iscsi_iface_param_info *iface_param,
647 struct addr_ctrl_blk *init_fw_cb)
648{
649 /*
650 * iface_num 0 is valid for IPv6 Addr, linklocal, router, autocfg.
651 * iface_num 1 is valid only for IPv6 Addr.
652 */
653 switch (iface_param->param) {
654 case ISCSI_NET_PARAM_IPV6_ADDR:
655 if (iface_param->iface_num & 0x1)
656 /* IPv6 Addr 1 */
657 memcpy(init_fw_cb->ipv6_addr1, iface_param->value,
658 sizeof(init_fw_cb->ipv6_addr1));
659 else
660 /* IPv6 Addr 0 */
661 memcpy(init_fw_cb->ipv6_addr0, iface_param->value,
662 sizeof(init_fw_cb->ipv6_addr0));
663 break;
664 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
665 if (iface_param->iface_num & 0x1)
666 break;
667 memcpy(init_fw_cb->ipv6_if_id, &iface_param->value[8],
668 sizeof(init_fw_cb->ipv6_if_id));
669 break;
670 case ISCSI_NET_PARAM_IPV6_ROUTER:
671 if (iface_param->iface_num & 0x1)
672 break;
673 memcpy(init_fw_cb->ipv6_dflt_rtr_addr, iface_param->value,
674 sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
675 break;
676 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
677 /* Autocfg applies to even interface */
678 if (iface_param->iface_num & 0x1)
679 break;
680
681 if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_DISABLE)
682 init_fw_cb->ipv6_addtl_opts &=
683 cpu_to_le16(
684 ~IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
685 else if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_ND_ENABLE)
686 init_fw_cb->ipv6_addtl_opts |=
687 cpu_to_le16(
688 IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
689 else
690 ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
691 "IPv6 addr\n");
692 break;
693 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
694 /* Autocfg applies to even interface */
695 if (iface_param->iface_num & 0x1)
696 break;
697
698 if (iface_param->value[0] ==
699 ISCSI_IPV6_LINKLOCAL_AUTOCFG_ENABLE)
700 init_fw_cb->ipv6_addtl_opts |= cpu_to_le16(
701 IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
702 else if (iface_param->value[0] ==
703 ISCSI_IPV6_LINKLOCAL_AUTOCFG_DISABLE)
704 init_fw_cb->ipv6_addtl_opts &= cpu_to_le16(
705 ~IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
706 else
707 ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
708 "IPv6 linklocal addr\n");
709 break;
710 case ISCSI_NET_PARAM_IPV6_ROUTER_AUTOCFG:
711 /* Autocfg applies to even interface */
712 if (iface_param->iface_num & 0x1)
713 break;
714
715 if (iface_param->value[0] == ISCSI_IPV6_ROUTER_AUTOCFG_ENABLE)
716 memset(init_fw_cb->ipv6_dflt_rtr_addr, 0,
717 sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
718 break;
719 case ISCSI_NET_PARAM_IFACE_ENABLE:
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500720 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
Mike Christied00efe32011-07-25 13:48:38 -0500721 init_fw_cb->ipv6_opts |=
722 cpu_to_le16(IPV6_OPT_IPV6_PROTOCOL_ENABLE);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500723 qla4xxx_create_ipv6_iface(ha);
724 } else {
Mike Christied00efe32011-07-25 13:48:38 -0500725 init_fw_cb->ipv6_opts &=
726 cpu_to_le16(~IPV6_OPT_IPV6_PROTOCOL_ENABLE &
727 0xFFFF);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500728 qla4xxx_destroy_ipv6_iface(ha);
729 }
Mike Christied00efe32011-07-25 13:48:38 -0500730 break;
Mike Christie2d636732011-10-11 17:55:11 -0500731 case ISCSI_NET_PARAM_VLAN_TAG:
Mike Christied00efe32011-07-25 13:48:38 -0500732 if (iface_param->len != sizeof(init_fw_cb->ipv6_vlan_tag))
733 break;
Vikas Chaudhary6ac73e82011-07-25 13:48:49 -0500734 init_fw_cb->ipv6_vlan_tag =
735 cpu_to_be16(*(uint16_t *)iface_param->value);
736 break;
737 case ISCSI_NET_PARAM_VLAN_ENABLED:
738 if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
739 init_fw_cb->ipv6_opts |=
740 cpu_to_le16(IPV6_OPT_VLAN_TAGGING_ENABLE);
741 else
742 init_fw_cb->ipv6_opts &=
743 cpu_to_le16(~IPV6_OPT_VLAN_TAGGING_ENABLE);
Mike Christied00efe32011-07-25 13:48:38 -0500744 break;
Vikas Chaudhary943c1572011-08-01 03:26:13 -0700745 case ISCSI_NET_PARAM_MTU:
746 init_fw_cb->eth_mtu_size =
747 cpu_to_le16(*(uint16_t *)iface_param->value);
748 break;
Vikas Chaudhary2ada7fc2011-08-01 03:26:19 -0700749 case ISCSI_NET_PARAM_PORT:
750 /* Autocfg applies to even interface */
751 if (iface_param->iface_num & 0x1)
752 break;
753
754 init_fw_cb->ipv6_port =
755 cpu_to_le16(*(uint16_t *)iface_param->value);
756 break;
Mike Christied00efe32011-07-25 13:48:38 -0500757 default:
758 ql4_printk(KERN_ERR, ha, "Unknown IPv6 param = %d\n",
759 iface_param->param);
760 break;
761 }
762}
763
764static void qla4xxx_set_ipv4(struct scsi_qla_host *ha,
765 struct iscsi_iface_param_info *iface_param,
766 struct addr_ctrl_blk *init_fw_cb)
767{
768 switch (iface_param->param) {
769 case ISCSI_NET_PARAM_IPV4_ADDR:
770 memcpy(init_fw_cb->ipv4_addr, iface_param->value,
771 sizeof(init_fw_cb->ipv4_addr));
772 break;
773 case ISCSI_NET_PARAM_IPV4_SUBNET:
774 memcpy(init_fw_cb->ipv4_subnet, iface_param->value,
775 sizeof(init_fw_cb->ipv4_subnet));
776 break;
777 case ISCSI_NET_PARAM_IPV4_GW:
778 memcpy(init_fw_cb->ipv4_gw_addr, iface_param->value,
779 sizeof(init_fw_cb->ipv4_gw_addr));
780 break;
781 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
782 if (iface_param->value[0] == ISCSI_BOOTPROTO_DHCP)
783 init_fw_cb->ipv4_tcp_opts |=
784 cpu_to_le16(TCPOPT_DHCP_ENABLE);
785 else if (iface_param->value[0] == ISCSI_BOOTPROTO_STATIC)
786 init_fw_cb->ipv4_tcp_opts &=
787 cpu_to_le16(~TCPOPT_DHCP_ENABLE);
788 else
789 ql4_printk(KERN_ERR, ha, "Invalid IPv4 bootproto\n");
790 break;
791 case ISCSI_NET_PARAM_IFACE_ENABLE:
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500792 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
Mike Christied00efe32011-07-25 13:48:38 -0500793 init_fw_cb->ipv4_ip_opts |=
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500794 cpu_to_le16(IPOPT_IPV4_PROTOCOL_ENABLE);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500795 qla4xxx_create_ipv4_iface(ha);
796 } else {
Mike Christied00efe32011-07-25 13:48:38 -0500797 init_fw_cb->ipv4_ip_opts &=
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500798 cpu_to_le16(~IPOPT_IPV4_PROTOCOL_ENABLE &
Mike Christied00efe32011-07-25 13:48:38 -0500799 0xFFFF);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500800 qla4xxx_destroy_ipv4_iface(ha);
801 }
Mike Christied00efe32011-07-25 13:48:38 -0500802 break;
Mike Christie2d636732011-10-11 17:55:11 -0500803 case ISCSI_NET_PARAM_VLAN_TAG:
Mike Christied00efe32011-07-25 13:48:38 -0500804 if (iface_param->len != sizeof(init_fw_cb->ipv4_vlan_tag))
805 break;
Vikas Chaudhary6ac73e82011-07-25 13:48:49 -0500806 init_fw_cb->ipv4_vlan_tag =
807 cpu_to_be16(*(uint16_t *)iface_param->value);
808 break;
809 case ISCSI_NET_PARAM_VLAN_ENABLED:
810 if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
811 init_fw_cb->ipv4_ip_opts |=
812 cpu_to_le16(IPOPT_VLAN_TAGGING_ENABLE);
813 else
814 init_fw_cb->ipv4_ip_opts &=
815 cpu_to_le16(~IPOPT_VLAN_TAGGING_ENABLE);
Mike Christied00efe32011-07-25 13:48:38 -0500816 break;
Vikas Chaudhary943c1572011-08-01 03:26:13 -0700817 case ISCSI_NET_PARAM_MTU:
818 init_fw_cb->eth_mtu_size =
819 cpu_to_le16(*(uint16_t *)iface_param->value);
820 break;
Vikas Chaudhary2ada7fc2011-08-01 03:26:19 -0700821 case ISCSI_NET_PARAM_PORT:
822 init_fw_cb->ipv4_port =
823 cpu_to_le16(*(uint16_t *)iface_param->value);
824 break;
Mike Christied00efe32011-07-25 13:48:38 -0500825 default:
826 ql4_printk(KERN_ERR, ha, "Unknown IPv4 param = %d\n",
827 iface_param->param);
828 break;
829 }
830}
831
832static void
833qla4xxx_initcb_to_acb(struct addr_ctrl_blk *init_fw_cb)
834{
835 struct addr_ctrl_blk_def *acb;
836 acb = (struct addr_ctrl_blk_def *)init_fw_cb;
837 memset(acb->reserved1, 0, sizeof(acb->reserved1));
838 memset(acb->reserved2, 0, sizeof(acb->reserved2));
839 memset(acb->reserved3, 0, sizeof(acb->reserved3));
840 memset(acb->reserved4, 0, sizeof(acb->reserved4));
841 memset(acb->reserved5, 0, sizeof(acb->reserved5));
842 memset(acb->reserved6, 0, sizeof(acb->reserved6));
843 memset(acb->reserved7, 0, sizeof(acb->reserved7));
844 memset(acb->reserved8, 0, sizeof(acb->reserved8));
845 memset(acb->reserved9, 0, sizeof(acb->reserved9));
846 memset(acb->reserved10, 0, sizeof(acb->reserved10));
847 memset(acb->reserved11, 0, sizeof(acb->reserved11));
848 memset(acb->reserved12, 0, sizeof(acb->reserved12));
849 memset(acb->reserved13, 0, sizeof(acb->reserved13));
850 memset(acb->reserved14, 0, sizeof(acb->reserved14));
851 memset(acb->reserved15, 0, sizeof(acb->reserved15));
852}
853
854static int
Mike Christie00c31882011-10-06 03:56:59 -0500855qla4xxx_iface_set_param(struct Scsi_Host *shost, void *data, uint32_t len)
Mike Christied00efe32011-07-25 13:48:38 -0500856{
857 struct scsi_qla_host *ha = to_qla_host(shost);
858 int rval = 0;
859 struct iscsi_iface_param_info *iface_param = NULL;
860 struct addr_ctrl_blk *init_fw_cb = NULL;
861 dma_addr_t init_fw_cb_dma;
862 uint32_t mbox_cmd[MBOX_REG_COUNT];
863 uint32_t mbox_sts[MBOX_REG_COUNT];
Mike Christie00c31882011-10-06 03:56:59 -0500864 uint32_t rem = len;
865 struct nlattr *attr;
Mike Christied00efe32011-07-25 13:48:38 -0500866
867 init_fw_cb = dma_alloc_coherent(&ha->pdev->dev,
868 sizeof(struct addr_ctrl_blk),
869 &init_fw_cb_dma, GFP_KERNEL);
870 if (!init_fw_cb) {
871 ql4_printk(KERN_ERR, ha, "%s: Unable to alloc init_cb\n",
872 __func__);
873 return -ENOMEM;
874 }
875
876 memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
877 memset(&mbox_cmd, 0, sizeof(mbox_cmd));
878 memset(&mbox_sts, 0, sizeof(mbox_sts));
879
880 if (qla4xxx_get_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma)) {
881 ql4_printk(KERN_ERR, ha, "%s: get ifcb failed\n", __func__);
882 rval = -EIO;
883 goto exit_init_fw_cb;
884 }
885
Mike Christie00c31882011-10-06 03:56:59 -0500886 nla_for_each_attr(attr, data, len, rem) {
887 iface_param = nla_data(attr);
Mike Christied00efe32011-07-25 13:48:38 -0500888
889 if (iface_param->param_type != ISCSI_NET_PARAM)
890 continue;
891
892 switch (iface_param->iface_type) {
893 case ISCSI_IFACE_TYPE_IPV4:
894 switch (iface_param->iface_num) {
895 case 0:
896 qla4xxx_set_ipv4(ha, iface_param, init_fw_cb);
897 break;
898 default:
899 /* Cannot have more than one IPv4 interface */
900 ql4_printk(KERN_ERR, ha, "Invalid IPv4 iface "
901 "number = %d\n",
902 iface_param->iface_num);
903 break;
904 }
905 break;
906 case ISCSI_IFACE_TYPE_IPV6:
907 switch (iface_param->iface_num) {
908 case 0:
909 case 1:
910 qla4xxx_set_ipv6(ha, iface_param, init_fw_cb);
911 break;
912 default:
913 /* Cannot have more than two IPv6 interface */
914 ql4_printk(KERN_ERR, ha, "Invalid IPv6 iface "
915 "number = %d\n",
916 iface_param->iface_num);
917 break;
918 }
919 break;
920 default:
921 ql4_printk(KERN_ERR, ha, "Invalid iface type\n");
922 break;
923 }
Mike Christied00efe32011-07-25 13:48:38 -0500924 }
925
926 init_fw_cb->cookie = cpu_to_le32(0x11BEAD5A);
927
928 rval = qla4xxx_set_flash(ha, init_fw_cb_dma, FLASH_SEGMENT_IFCB,
929 sizeof(struct addr_ctrl_blk),
930 FLASH_OPT_RMW_COMMIT);
931 if (rval != QLA_SUCCESS) {
932 ql4_printk(KERN_ERR, ha, "%s: set flash mbx failed\n",
933 __func__);
934 rval = -EIO;
935 goto exit_init_fw_cb;
936 }
937
Vikas Chaudharyce505f92011-12-01 22:42:10 -0800938 rval = qla4xxx_disable_acb(ha);
939 if (rval != QLA_SUCCESS) {
940 ql4_printk(KERN_ERR, ha, "%s: disable acb mbx failed\n",
941 __func__);
942 rval = -EIO;
943 goto exit_init_fw_cb;
944 }
945
946 wait_for_completion_timeout(&ha->disable_acb_comp,
947 DISABLE_ACB_TOV * HZ);
Mike Christied00efe32011-07-25 13:48:38 -0500948
949 qla4xxx_initcb_to_acb(init_fw_cb);
950
951 rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma);
952 if (rval != QLA_SUCCESS) {
953 ql4_printk(KERN_ERR, ha, "%s: set acb mbx failed\n",
954 __func__);
955 rval = -EIO;
956 goto exit_init_fw_cb;
957 }
958
959 memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
960 qla4xxx_update_local_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb,
961 init_fw_cb_dma);
962
963exit_init_fw_cb:
964 dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk),
965 init_fw_cb, init_fw_cb_dma);
966
967 return rval;
968}
969
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500970static int qla4xxx_conn_get_param(struct iscsi_cls_conn *cls_conn,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700971 enum iscsi_param param, char *buf)
972{
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500973 struct iscsi_conn *conn;
974 struct qla_conn *qla_conn;
975 struct sockaddr *dst_addr;
976 int len = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700977
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500978 conn = cls_conn->dd_data;
979 qla_conn = conn->dd_data;
980 dst_addr = &qla_conn->qla_ep->dst_addr;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700981
982 switch (param) {
983 case ISCSI_PARAM_CONN_PORT:
David Somayajuluafaf5a22006-09-19 10:28:00 -0700984 case ISCSI_PARAM_CONN_ADDRESS:
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500985 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
986 dst_addr, param, buf);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700987 default:
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500988 return iscsi_conn_get_param(cls_conn, param, buf);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700989 }
990
991 return len;
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500992
David Somayajuluafaf5a22006-09-19 10:28:00 -0700993}
994
Mike Christie13483732011-12-01 21:38:41 -0600995int qla4xxx_get_ddb_index(struct scsi_qla_host *ha, uint16_t *ddb_index)
996{
997 uint32_t mbx_sts = 0;
998 uint16_t tmp_ddb_index;
999 int ret;
1000
1001get_ddb_index:
1002 tmp_ddb_index = find_first_zero_bit(ha->ddb_idx_map, MAX_DDB_ENTRIES);
1003
1004 if (tmp_ddb_index >= MAX_DDB_ENTRIES) {
1005 DEBUG2(ql4_printk(KERN_INFO, ha,
1006 "Free DDB index not available\n"));
1007 ret = QLA_ERROR;
1008 goto exit_get_ddb_index;
1009 }
1010
1011 if (test_and_set_bit(tmp_ddb_index, ha->ddb_idx_map))
1012 goto get_ddb_index;
1013
1014 DEBUG2(ql4_printk(KERN_INFO, ha,
1015 "Found a free DDB index at %d\n", tmp_ddb_index));
1016 ret = qla4xxx_req_ddb_entry(ha, tmp_ddb_index, &mbx_sts);
1017 if (ret == QLA_ERROR) {
1018 if (mbx_sts == MBOX_STS_COMMAND_ERROR) {
1019 ql4_printk(KERN_INFO, ha,
1020 "DDB index = %d not available trying next\n",
1021 tmp_ddb_index);
1022 goto get_ddb_index;
1023 }
1024 DEBUG2(ql4_printk(KERN_INFO, ha,
1025 "Free FW DDB not available\n"));
1026 }
1027
1028 *ddb_index = tmp_ddb_index;
1029
1030exit_get_ddb_index:
1031 return ret;
1032}
1033
1034static int qla4xxx_match_ipaddress(struct scsi_qla_host *ha,
1035 struct ddb_entry *ddb_entry,
1036 char *existing_ipaddr,
1037 char *user_ipaddr)
1038{
1039 uint8_t dst_ipaddr[IPv6_ADDR_LEN];
1040 char formatted_ipaddr[DDB_IPADDR_LEN];
1041 int status = QLA_SUCCESS, ret = 0;
1042
1043 if (ddb_entry->fw_ddb_entry.options & DDB_OPT_IPV6_DEVICE) {
1044 ret = in6_pton(user_ipaddr, strlen(user_ipaddr), dst_ipaddr,
1045 '\0', NULL);
1046 if (ret == 0) {
1047 status = QLA_ERROR;
1048 goto out_match;
1049 }
1050 ret = sprintf(formatted_ipaddr, "%pI6", dst_ipaddr);
1051 } else {
1052 ret = in4_pton(user_ipaddr, strlen(user_ipaddr), dst_ipaddr,
1053 '\0', NULL);
1054 if (ret == 0) {
1055 status = QLA_ERROR;
1056 goto out_match;
1057 }
1058 ret = sprintf(formatted_ipaddr, "%pI4", dst_ipaddr);
1059 }
1060
1061 if (strcmp(existing_ipaddr, formatted_ipaddr))
1062 status = QLA_ERROR;
1063
1064out_match:
1065 return status;
1066}
1067
1068static int qla4xxx_match_fwdb_session(struct scsi_qla_host *ha,
1069 struct iscsi_cls_conn *cls_conn)
1070{
1071 int idx = 0, max_ddbs, rval;
1072 struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
1073 struct iscsi_session *sess, *existing_sess;
1074 struct iscsi_conn *conn, *existing_conn;
1075 struct ddb_entry *ddb_entry;
1076
1077 sess = cls_sess->dd_data;
1078 conn = cls_conn->dd_data;
1079
1080 if (sess->targetname == NULL ||
1081 conn->persistent_address == NULL ||
1082 conn->persistent_port == 0)
1083 return QLA_ERROR;
1084
1085 max_ddbs = is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
1086 MAX_DEV_DB_ENTRIES;
1087
1088 for (idx = 0; idx < max_ddbs; idx++) {
1089 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
1090 if (ddb_entry == NULL)
1091 continue;
1092
1093 if (ddb_entry->ddb_type != FLASH_DDB)
1094 continue;
1095
1096 existing_sess = ddb_entry->sess->dd_data;
1097 existing_conn = ddb_entry->conn->dd_data;
1098
1099 if (existing_sess->targetname == NULL ||
1100 existing_conn->persistent_address == NULL ||
1101 existing_conn->persistent_port == 0)
1102 continue;
1103
1104 DEBUG2(ql4_printk(KERN_INFO, ha,
1105 "IQN = %s User IQN = %s\n",
1106 existing_sess->targetname,
1107 sess->targetname));
1108
1109 DEBUG2(ql4_printk(KERN_INFO, ha,
1110 "IP = %s User IP = %s\n",
1111 existing_conn->persistent_address,
1112 conn->persistent_address));
1113
1114 DEBUG2(ql4_printk(KERN_INFO, ha,
1115 "Port = %d User Port = %d\n",
1116 existing_conn->persistent_port,
1117 conn->persistent_port));
1118
1119 if (strcmp(existing_sess->targetname, sess->targetname))
1120 continue;
1121 rval = qla4xxx_match_ipaddress(ha, ddb_entry,
1122 existing_conn->persistent_address,
1123 conn->persistent_address);
1124 if (rval == QLA_ERROR)
1125 continue;
1126 if (existing_conn->persistent_port != conn->persistent_port)
1127 continue;
1128 break;
1129 }
1130
1131 if (idx == max_ddbs)
1132 return QLA_ERROR;
1133
1134 DEBUG2(ql4_printk(KERN_INFO, ha,
1135 "Match found in fwdb sessions\n"));
1136 return QLA_SUCCESS;
1137}
1138
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001139static struct iscsi_cls_session *
1140qla4xxx_session_create(struct iscsi_endpoint *ep,
1141 uint16_t cmds_max, uint16_t qdepth,
1142 uint32_t initial_cmdsn)
1143{
1144 struct iscsi_cls_session *cls_sess;
1145 struct scsi_qla_host *ha;
1146 struct qla_endpoint *qla_ep;
1147 struct ddb_entry *ddb_entry;
Mike Christie13483732011-12-01 21:38:41 -06001148 uint16_t ddb_index;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001149 struct iscsi_session *sess;
1150 struct sockaddr *dst_addr;
1151 int ret;
1152
1153 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1154 if (!ep) {
1155 printk(KERN_ERR "qla4xxx: missing ep.\n");
1156 return NULL;
1157 }
1158
1159 qla_ep = ep->dd_data;
1160 dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
1161 ha = to_qla_host(qla_ep->host);
Manish Rangankar736cf362011-10-07 16:55:46 -07001162
Mike Christie13483732011-12-01 21:38:41 -06001163 ret = qla4xxx_get_ddb_index(ha, &ddb_index);
1164 if (ret == QLA_ERROR)
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001165 return NULL;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001166
1167 cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport, qla_ep->host,
1168 cmds_max, sizeof(struct ddb_entry),
1169 sizeof(struct ql4_task_data),
1170 initial_cmdsn, ddb_index);
1171 if (!cls_sess)
1172 return NULL;
1173
1174 sess = cls_sess->dd_data;
1175 ddb_entry = sess->dd_data;
1176 ddb_entry->fw_ddb_index = ddb_index;
1177 ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
1178 ddb_entry->ha = ha;
1179 ddb_entry->sess = cls_sess;
Mike Christie13483732011-12-01 21:38:41 -06001180 ddb_entry->unblock_sess = qla4xxx_unblock_ddb;
1181 ddb_entry->ddb_change = qla4xxx_ddb_change;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001182 cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
1183 ha->fw_ddb_index_map[ddb_entry->fw_ddb_index] = ddb_entry;
1184 ha->tot_ddbs++;
1185
1186 return cls_sess;
1187}
1188
1189static void qla4xxx_session_destroy(struct iscsi_cls_session *cls_sess)
1190{
1191 struct iscsi_session *sess;
1192 struct ddb_entry *ddb_entry;
1193 struct scsi_qla_host *ha;
1194 unsigned long flags;
1195
1196 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1197 sess = cls_sess->dd_data;
1198 ddb_entry = sess->dd_data;
1199 ha = ddb_entry->ha;
1200
Manish Rangankar736cf362011-10-07 16:55:46 -07001201 qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
1202
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001203 spin_lock_irqsave(&ha->hardware_lock, flags);
1204 qla4xxx_free_ddb(ha, ddb_entry);
1205 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1206 iscsi_session_teardown(cls_sess);
1207}
1208
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001209static struct iscsi_cls_conn *
1210qla4xxx_conn_create(struct iscsi_cls_session *cls_sess, uint32_t conn_idx)
1211{
1212 struct iscsi_cls_conn *cls_conn;
1213 struct iscsi_session *sess;
1214 struct ddb_entry *ddb_entry;
1215
1216 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1217 cls_conn = iscsi_conn_setup(cls_sess, sizeof(struct qla_conn),
1218 conn_idx);
Mike Christieff1d0312011-12-01 21:38:43 -06001219 if (!cls_conn)
1220 return NULL;
1221
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001222 sess = cls_sess->dd_data;
1223 ddb_entry = sess->dd_data;
1224 ddb_entry->conn = cls_conn;
1225
1226 return cls_conn;
1227}
1228
1229static int qla4xxx_conn_bind(struct iscsi_cls_session *cls_session,
1230 struct iscsi_cls_conn *cls_conn,
1231 uint64_t transport_fd, int is_leading)
1232{
1233 struct iscsi_conn *conn;
1234 struct qla_conn *qla_conn;
1235 struct iscsi_endpoint *ep;
1236
1237 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1238
1239 if (iscsi_conn_bind(cls_session, cls_conn, is_leading))
1240 return -EINVAL;
1241 ep = iscsi_lookup_endpoint(transport_fd);
1242 conn = cls_conn->dd_data;
1243 qla_conn = conn->dd_data;
1244 qla_conn->qla_ep = ep->dd_data;
1245 return 0;
1246}
1247
1248static int qla4xxx_conn_start(struct iscsi_cls_conn *cls_conn)
1249{
1250 struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
1251 struct iscsi_session *sess;
1252 struct ddb_entry *ddb_entry;
1253 struct scsi_qla_host *ha;
Mike Christie13483732011-12-01 21:38:41 -06001254 struct dev_db_entry *fw_ddb_entry = NULL;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001255 dma_addr_t fw_ddb_entry_dma;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001256 uint32_t mbx_sts = 0;
1257 int ret = 0;
1258 int status = QLA_SUCCESS;
1259
1260 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1261 sess = cls_sess->dd_data;
1262 ddb_entry = sess->dd_data;
1263 ha = ddb_entry->ha;
1264
Mike Christie13483732011-12-01 21:38:41 -06001265 /* Check if we have matching FW DDB, if yes then do not
1266 * login to this target. This could cause target to logout previous
1267 * connection
1268 */
1269 ret = qla4xxx_match_fwdb_session(ha, cls_conn);
1270 if (ret == QLA_SUCCESS) {
1271 ql4_printk(KERN_INFO, ha,
1272 "Session already exist in FW.\n");
1273 ret = -EEXIST;
1274 goto exit_conn_start;
1275 }
1276
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001277 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1278 &fw_ddb_entry_dma, GFP_KERNEL);
1279 if (!fw_ddb_entry) {
1280 ql4_printk(KERN_ERR, ha,
1281 "%s: Unable to allocate dma buffer\n", __func__);
Mike Christie13483732011-12-01 21:38:41 -06001282 ret = -ENOMEM;
1283 goto exit_conn_start;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001284 }
1285
1286 ret = qla4xxx_set_param_ddbentry(ha, ddb_entry, cls_conn, &mbx_sts);
1287 if (ret) {
1288 /* If iscsid is stopped and started then no need to do
1289 * set param again since ddb state will be already
1290 * active and FW does not allow set ddb to an
1291 * active session.
1292 */
1293 if (mbx_sts)
1294 if (ddb_entry->fw_ddb_device_state ==
Manish Rangankarf922da72011-10-07 16:55:49 -07001295 DDB_DS_SESSION_ACTIVE) {
Mike Christie13483732011-12-01 21:38:41 -06001296 ddb_entry->unblock_sess(ddb_entry->sess);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001297 goto exit_set_param;
Manish Rangankarf922da72011-10-07 16:55:49 -07001298 }
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001299
1300 ql4_printk(KERN_ERR, ha, "%s: Failed set param for index[%d]\n",
1301 __func__, ddb_entry->fw_ddb_index);
1302 goto exit_conn_start;
1303 }
1304
1305 status = qla4xxx_conn_open(ha, ddb_entry->fw_ddb_index);
1306 if (status == QLA_ERROR) {
Manish Rangankar0e7e8502011-07-25 13:48:54 -05001307 ql4_printk(KERN_ERR, ha, "%s: Login failed: %s\n", __func__,
1308 sess->targetname);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001309 ret = -EINVAL;
1310 goto exit_conn_start;
1311 }
1312
Manish Rangankar98270ab2011-10-07 16:55:47 -07001313 if (ddb_entry->fw_ddb_device_state == DDB_DS_NO_CONNECTION_ACTIVE)
1314 ddb_entry->fw_ddb_device_state = DDB_DS_LOGIN_IN_PROCESS;
1315
1316 DEBUG2(printk(KERN_INFO "%s: DDB state [%d]\n", __func__,
1317 ddb_entry->fw_ddb_device_state));
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001318
1319exit_set_param:
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001320 ret = 0;
1321
1322exit_conn_start:
Mike Christie13483732011-12-01 21:38:41 -06001323 if (fw_ddb_entry)
1324 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1325 fw_ddb_entry, fw_ddb_entry_dma);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001326 return ret;
1327}
1328
1329static void qla4xxx_conn_destroy(struct iscsi_cls_conn *cls_conn)
1330{
1331 struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
1332 struct iscsi_session *sess;
1333 struct scsi_qla_host *ha;
1334 struct ddb_entry *ddb_entry;
1335 int options;
1336
1337 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1338 sess = cls_sess->dd_data;
1339 ddb_entry = sess->dd_data;
1340 ha = ddb_entry->ha;
1341
1342 options = LOGOUT_OPTION_CLOSE_SESSION;
1343 if (qla4xxx_session_logout_ddb(ha, ddb_entry, options) == QLA_ERROR)
1344 ql4_printk(KERN_ERR, ha, "%s: Logout failed\n", __func__);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001345}
1346
1347static void qla4xxx_task_work(struct work_struct *wdata)
1348{
1349 struct ql4_task_data *task_data;
1350 struct scsi_qla_host *ha;
1351 struct passthru_status *sts;
1352 struct iscsi_task *task;
1353 struct iscsi_hdr *hdr;
1354 uint8_t *data;
1355 uint32_t data_len;
1356 struct iscsi_conn *conn;
1357 int hdr_len;
1358 itt_t itt;
1359
1360 task_data = container_of(wdata, struct ql4_task_data, task_work);
1361 ha = task_data->ha;
1362 task = task_data->task;
1363 sts = &task_data->sts;
1364 hdr_len = sizeof(struct iscsi_hdr);
1365
1366 DEBUG3(printk(KERN_INFO "Status returned\n"));
1367 DEBUG3(qla4xxx_dump_buffer(sts, 64));
1368 DEBUG3(printk(KERN_INFO "Response buffer"));
1369 DEBUG3(qla4xxx_dump_buffer(task_data->resp_buffer, 64));
1370
1371 conn = task->conn;
1372
1373 switch (sts->completionStatus) {
1374 case PASSTHRU_STATUS_COMPLETE:
1375 hdr = (struct iscsi_hdr *)task_data->resp_buffer;
1376 /* Assign back the itt in hdr, until we use the PREASSIGN_TAG */
1377 itt = sts->handle;
1378 hdr->itt = itt;
1379 data = task_data->resp_buffer + hdr_len;
1380 data_len = task_data->resp_len - hdr_len;
1381 iscsi_complete_pdu(conn, hdr, data, data_len);
1382 break;
1383 default:
1384 ql4_printk(KERN_ERR, ha, "Passthru failed status = 0x%x\n",
1385 sts->completionStatus);
1386 break;
1387 }
1388 return;
1389}
1390
1391static int qla4xxx_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
1392{
1393 struct ql4_task_data *task_data;
1394 struct iscsi_session *sess;
1395 struct ddb_entry *ddb_entry;
1396 struct scsi_qla_host *ha;
1397 int hdr_len;
1398
1399 sess = task->conn->session;
1400 ddb_entry = sess->dd_data;
1401 ha = ddb_entry->ha;
1402 task_data = task->dd_data;
1403 memset(task_data, 0, sizeof(struct ql4_task_data));
1404
1405 if (task->sc) {
1406 ql4_printk(KERN_INFO, ha,
1407 "%s: SCSI Commands not implemented\n", __func__);
1408 return -EINVAL;
1409 }
1410
1411 hdr_len = sizeof(struct iscsi_hdr);
1412 task_data->ha = ha;
1413 task_data->task = task;
1414
1415 if (task->data_count) {
1416 task_data->data_dma = dma_map_single(&ha->pdev->dev, task->data,
1417 task->data_count,
1418 PCI_DMA_TODEVICE);
1419 }
1420
1421 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
1422 __func__, task->conn->max_recv_dlength, hdr_len));
1423
Manish Rangankar69ca2162011-10-07 16:55:50 -07001424 task_data->resp_len = task->conn->max_recv_dlength + hdr_len;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001425 task_data->resp_buffer = dma_alloc_coherent(&ha->pdev->dev,
1426 task_data->resp_len,
1427 &task_data->resp_dma,
1428 GFP_ATOMIC);
1429 if (!task_data->resp_buffer)
1430 goto exit_alloc_pdu;
1431
Manish Rangankar69ca2162011-10-07 16:55:50 -07001432 task_data->req_len = task->data_count + hdr_len;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001433 task_data->req_buffer = dma_alloc_coherent(&ha->pdev->dev,
Manish Rangankar69ca2162011-10-07 16:55:50 -07001434 task_data->req_len,
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001435 &task_data->req_dma,
1436 GFP_ATOMIC);
1437 if (!task_data->req_buffer)
1438 goto exit_alloc_pdu;
1439
1440 task->hdr = task_data->req_buffer;
1441
1442 INIT_WORK(&task_data->task_work, qla4xxx_task_work);
1443
1444 return 0;
1445
1446exit_alloc_pdu:
1447 if (task_data->resp_buffer)
1448 dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
1449 task_data->resp_buffer, task_data->resp_dma);
1450
1451 if (task_data->req_buffer)
Manish Rangankar69ca2162011-10-07 16:55:50 -07001452 dma_free_coherent(&ha->pdev->dev, task_data->req_len,
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001453 task_data->req_buffer, task_data->req_dma);
1454 return -ENOMEM;
1455}
1456
1457static void qla4xxx_task_cleanup(struct iscsi_task *task)
1458{
1459 struct ql4_task_data *task_data;
1460 struct iscsi_session *sess;
1461 struct ddb_entry *ddb_entry;
1462 struct scsi_qla_host *ha;
1463 int hdr_len;
1464
1465 hdr_len = sizeof(struct iscsi_hdr);
1466 sess = task->conn->session;
1467 ddb_entry = sess->dd_data;
1468 ha = ddb_entry->ha;
1469 task_data = task->dd_data;
1470
1471 if (task->data_count) {
1472 dma_unmap_single(&ha->pdev->dev, task_data->data_dma,
1473 task->data_count, PCI_DMA_TODEVICE);
1474 }
1475
1476 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
1477 __func__, task->conn->max_recv_dlength, hdr_len));
1478
1479 dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
1480 task_data->resp_buffer, task_data->resp_dma);
Manish Rangankar69ca2162011-10-07 16:55:50 -07001481 dma_free_coherent(&ha->pdev->dev, task_data->req_len,
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001482 task_data->req_buffer, task_data->req_dma);
1483 return;
1484}
1485
1486static int qla4xxx_task_xmit(struct iscsi_task *task)
1487{
1488 struct scsi_cmnd *sc = task->sc;
1489 struct iscsi_session *sess = task->conn->session;
1490 struct ddb_entry *ddb_entry = sess->dd_data;
1491 struct scsi_qla_host *ha = ddb_entry->ha;
1492
1493 if (!sc)
1494 return qla4xxx_send_passthru0(task);
1495
1496 ql4_printk(KERN_INFO, ha, "%s: scsi cmd xmit not implemented\n",
1497 __func__);
1498 return -ENOSYS;
1499}
1500
Mike Christie13483732011-12-01 21:38:41 -06001501static void qla4xxx_copy_fwddb_param(struct scsi_qla_host *ha,
1502 struct dev_db_entry *fw_ddb_entry,
1503 struct iscsi_cls_session *cls_sess,
1504 struct iscsi_cls_conn *cls_conn)
1505{
1506 int buflen = 0;
1507 struct iscsi_session *sess;
1508 struct iscsi_conn *conn;
1509 char ip_addr[DDB_IPADDR_LEN];
1510 uint16_t options = 0;
1511
1512 sess = cls_sess->dd_data;
1513 conn = cls_conn->dd_data;
1514
1515 conn->max_recv_dlength = BYTE_UNITS *
1516 le16_to_cpu(fw_ddb_entry->iscsi_max_rcv_data_seg_len);
1517
1518 conn->max_xmit_dlength = BYTE_UNITS *
1519 le16_to_cpu(fw_ddb_entry->iscsi_max_snd_data_seg_len);
1520
1521 sess->initial_r2t_en =
1522 (BIT_10 & le16_to_cpu(fw_ddb_entry->iscsi_options));
1523
1524 sess->max_r2t = le16_to_cpu(fw_ddb_entry->iscsi_max_outsnd_r2t);
1525
1526 sess->imm_data_en = (BIT_11 & le16_to_cpu(fw_ddb_entry->iscsi_options));
1527
1528 sess->first_burst = BYTE_UNITS *
1529 le16_to_cpu(fw_ddb_entry->iscsi_first_burst_len);
1530
1531 sess->max_burst = BYTE_UNITS *
1532 le16_to_cpu(fw_ddb_entry->iscsi_max_burst_len);
1533
1534 sess->time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
1535
1536 sess->time2retain = le16_to_cpu(fw_ddb_entry->iscsi_def_time2retain);
1537
1538 conn->persistent_port = le16_to_cpu(fw_ddb_entry->port);
1539
1540 sess->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
1541
1542 options = le16_to_cpu(fw_ddb_entry->options);
1543 if (options & DDB_OPT_IPV6_DEVICE)
1544 sprintf(ip_addr, "%pI6", fw_ddb_entry->ip_addr);
1545 else
1546 sprintf(ip_addr, "%pI4", fw_ddb_entry->ip_addr);
1547
1548 iscsi_set_param(cls_conn, ISCSI_PARAM_TARGET_NAME,
1549 (char *)fw_ddb_entry->iscsi_name, buflen);
1550 iscsi_set_param(cls_conn, ISCSI_PARAM_INITIATOR_NAME,
1551 (char *)ha->name_string, buflen);
1552 iscsi_set_param(cls_conn, ISCSI_PARAM_PERSISTENT_ADDRESS,
1553 (char *)ip_addr, buflen);
1554}
1555
1556void qla4xxx_update_session_conn_fwddb_param(struct scsi_qla_host *ha,
1557 struct ddb_entry *ddb_entry)
1558{
1559 struct iscsi_cls_session *cls_sess;
1560 struct iscsi_cls_conn *cls_conn;
1561 uint32_t ddb_state;
1562 dma_addr_t fw_ddb_entry_dma;
1563 struct dev_db_entry *fw_ddb_entry;
1564
1565 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1566 &fw_ddb_entry_dma, GFP_KERNEL);
1567 if (!fw_ddb_entry) {
1568 ql4_printk(KERN_ERR, ha,
1569 "%s: Unable to allocate dma buffer\n", __func__);
1570 goto exit_session_conn_fwddb_param;
1571 }
1572
1573 if (qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index, fw_ddb_entry,
1574 fw_ddb_entry_dma, NULL, NULL, &ddb_state,
1575 NULL, NULL, NULL) == QLA_ERROR) {
1576 DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: failed "
1577 "get_ddb_entry for fw_ddb_index %d\n",
1578 ha->host_no, __func__,
1579 ddb_entry->fw_ddb_index));
1580 goto exit_session_conn_fwddb_param;
1581 }
1582
1583 cls_sess = ddb_entry->sess;
1584
1585 cls_conn = ddb_entry->conn;
1586
1587 /* Update params */
1588 qla4xxx_copy_fwddb_param(ha, fw_ddb_entry, cls_sess, cls_conn);
1589
1590exit_session_conn_fwddb_param:
1591 if (fw_ddb_entry)
1592 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1593 fw_ddb_entry, fw_ddb_entry_dma);
1594}
1595
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001596void qla4xxx_update_session_conn_param(struct scsi_qla_host *ha,
1597 struct ddb_entry *ddb_entry)
1598{
1599 struct iscsi_cls_session *cls_sess;
1600 struct iscsi_cls_conn *cls_conn;
1601 struct iscsi_session *sess;
1602 struct iscsi_conn *conn;
1603 uint32_t ddb_state;
1604 dma_addr_t fw_ddb_entry_dma;
1605 struct dev_db_entry *fw_ddb_entry;
1606
1607 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1608 &fw_ddb_entry_dma, GFP_KERNEL);
1609 if (!fw_ddb_entry) {
1610 ql4_printk(KERN_ERR, ha,
1611 "%s: Unable to allocate dma buffer\n", __func__);
Mike Christie13483732011-12-01 21:38:41 -06001612 goto exit_session_conn_param;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001613 }
1614
1615 if (qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index, fw_ddb_entry,
1616 fw_ddb_entry_dma, NULL, NULL, &ddb_state,
1617 NULL, NULL, NULL) == QLA_ERROR) {
1618 DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: failed "
1619 "get_ddb_entry for fw_ddb_index %d\n",
1620 ha->host_no, __func__,
1621 ddb_entry->fw_ddb_index));
Mike Christie13483732011-12-01 21:38:41 -06001622 goto exit_session_conn_param;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001623 }
1624
1625 cls_sess = ddb_entry->sess;
1626 sess = cls_sess->dd_data;
1627
1628 cls_conn = ddb_entry->conn;
1629 conn = cls_conn->dd_data;
1630
Mike Christie13483732011-12-01 21:38:41 -06001631 /* Update timers after login */
1632 ddb_entry->default_relogin_timeout =
1633 le16_to_cpu(fw_ddb_entry->def_timeout);
1634 ddb_entry->default_time2wait =
1635 le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
1636
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001637 /* Update params */
1638 conn->max_recv_dlength = BYTE_UNITS *
1639 le16_to_cpu(fw_ddb_entry->iscsi_max_rcv_data_seg_len);
1640
1641 conn->max_xmit_dlength = BYTE_UNITS *
1642 le16_to_cpu(fw_ddb_entry->iscsi_max_snd_data_seg_len);
1643
1644 sess->initial_r2t_en =
1645 (BIT_10 & le16_to_cpu(fw_ddb_entry->iscsi_options));
1646
1647 sess->max_r2t = le16_to_cpu(fw_ddb_entry->iscsi_max_outsnd_r2t);
1648
1649 sess->imm_data_en = (BIT_11 & le16_to_cpu(fw_ddb_entry->iscsi_options));
1650
1651 sess->first_burst = BYTE_UNITS *
1652 le16_to_cpu(fw_ddb_entry->iscsi_first_burst_len);
1653
1654 sess->max_burst = BYTE_UNITS *
1655 le16_to_cpu(fw_ddb_entry->iscsi_max_burst_len);
1656
1657 sess->time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
1658
1659 sess->time2retain = le16_to_cpu(fw_ddb_entry->iscsi_def_time2retain);
1660
1661 sess->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
1662
1663 memcpy(sess->initiatorname, ha->name_string,
1664 min(sizeof(ha->name_string), sizeof(sess->initiatorname)));
Mike Christie13483732011-12-01 21:38:41 -06001665
1666exit_session_conn_param:
1667 if (fw_ddb_entry)
1668 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1669 fw_ddb_entry, fw_ddb_entry_dma);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001670}
1671
David Somayajuluafaf5a22006-09-19 10:28:00 -07001672/*
1673 * Timer routines
1674 */
1675
1676static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
1677 unsigned long interval)
1678{
1679 DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
1680 __func__, ha->host->host_no));
1681 init_timer(&ha->timer);
1682 ha->timer.expires = jiffies + interval * HZ;
1683 ha->timer.data = (unsigned long)ha;
1684 ha->timer.function = (void (*)(unsigned long))func;
1685 add_timer(&ha->timer);
1686 ha->timer_active = 1;
1687}
1688
1689static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
1690{
1691 del_timer_sync(&ha->timer);
1692 ha->timer_active = 0;
1693}
1694
1695/***
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001696 * qla4xxx_mark_device_missing - blocks the session
1697 * @cls_session: Pointer to the session to be blocked
David Somayajuluafaf5a22006-09-19 10:28:00 -07001698 * @ddb_entry: Pointer to device database entry
1699 *
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301700 * This routine marks a device missing and close connection.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001701 **/
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001702void qla4xxx_mark_device_missing(struct iscsi_cls_session *cls_session)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001703{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001704 iscsi_block_session(cls_session);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001705}
1706
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301707/**
1708 * qla4xxx_mark_all_devices_missing - mark all devices as missing.
1709 * @ha: Pointer to host adapter structure.
1710 *
1711 * This routine marks a device missing and resets the relogin retry count.
1712 **/
1713void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
1714{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001715 iscsi_host_for_each_session(ha->host, qla4xxx_mark_device_missing);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301716}
1717
David Somayajuluafaf5a22006-09-19 10:28:00 -07001718static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
1719 struct ddb_entry *ddb_entry,
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001720 struct scsi_cmnd *cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001721{
1722 struct srb *srb;
1723
1724 srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
1725 if (!srb)
1726 return srb;
1727
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301728 kref_init(&srb->srb_ref);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001729 srb->ha = ha;
1730 srb->ddb = ddb_entry;
1731 srb->cmd = cmd;
1732 srb->flags = 0;
Vikas Chaudhary53698872010-04-28 11:41:59 +05301733 CMD_SP(cmd) = (void *)srb;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001734
1735 return srb;
1736}
1737
1738static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
1739{
1740 struct scsi_cmnd *cmd = srb->cmd;
1741
1742 if (srb->flags & SRB_DMA_VALID) {
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +09001743 scsi_dma_unmap(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001744 srb->flags &= ~SRB_DMA_VALID;
1745 }
Vikas Chaudhary53698872010-04-28 11:41:59 +05301746 CMD_SP(cmd) = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001747}
1748
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301749void qla4xxx_srb_compl(struct kref *ref)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001750{
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301751 struct srb *srb = container_of(ref, struct srb, srb_ref);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001752 struct scsi_cmnd *cmd = srb->cmd;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301753 struct scsi_qla_host *ha = srb->ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001754
1755 qla4xxx_srb_free_dma(ha, srb);
1756
1757 mempool_free(srb, ha->srb_mempool);
1758
1759 cmd->scsi_done(cmd);
1760}
1761
1762/**
1763 * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001764 * @host: scsi host
David Somayajuluafaf5a22006-09-19 10:28:00 -07001765 * @cmd: Pointer to Linux's SCSI command structure
David Somayajuluafaf5a22006-09-19 10:28:00 -07001766 *
1767 * Remarks:
1768 * This routine is invoked by Linux to send a SCSI command to the driver.
1769 * The mid-level driver tries to ensure that queuecommand never gets
1770 * invoked concurrently with itself or the interrupt handler (although
1771 * the interrupt handler may call this routine as part of request-
1772 * completion handling). Unfortunely, it sometimes calls the scheduler
1773 * in interrupt context which is a big NO! NO!.
1774 **/
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001775static int qla4xxx_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001776{
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001777 struct scsi_qla_host *ha = to_qla_host(host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001778 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christie7fb19212008-01-31 13:36:45 -06001779 struct iscsi_cls_session *sess = ddb_entry->sess;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001780 struct srb *srb;
1781 int rval;
1782
Lalit Chandivade2232be02010-07-30 14:38:47 +05301783 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1784 if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
1785 cmd->result = DID_NO_CONNECT << 16;
1786 else
1787 cmd->result = DID_REQUEUE << 16;
1788 goto qc_fail_command;
1789 }
1790
Mike Christie7fb19212008-01-31 13:36:45 -06001791 if (!sess) {
1792 cmd->result = DID_IMM_RETRY << 16;
1793 goto qc_fail_command;
1794 }
1795
1796 rval = iscsi_session_chkready(sess);
1797 if (rval) {
1798 cmd->result = rval;
1799 goto qc_fail_command;
1800 }
1801
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301802 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1803 test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
1804 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1805 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
1806 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
1807 !test_bit(AF_ONLINE, &ha->flags) ||
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001808 !test_bit(AF_LINK_UP, &ha->flags) ||
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301809 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
David C Somayajulu477ffb92007-01-22 12:26:11 -08001810 goto qc_host_busy;
1811
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001812 srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001813 if (!srb)
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001814 goto qc_host_busy;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001815
1816 rval = qla4xxx_send_command_to_isp(ha, srb);
1817 if (rval != QLA_SUCCESS)
1818 goto qc_host_busy_free_sp;
1819
David Somayajuluafaf5a22006-09-19 10:28:00 -07001820 return 0;
1821
1822qc_host_busy_free_sp:
1823 qla4xxx_srb_free_dma(ha, srb);
1824 mempool_free(srb, ha->srb_mempool);
1825
David Somayajuluafaf5a22006-09-19 10:28:00 -07001826qc_host_busy:
1827 return SCSI_MLQUEUE_HOST_BUSY;
1828
1829qc_fail_command:
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001830 cmd->scsi_done(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001831
1832 return 0;
1833}
1834
1835/**
1836 * qla4xxx_mem_free - frees memory allocated to adapter
1837 * @ha: Pointer to host adapter structure.
1838 *
1839 * Frees memory previously allocated by qla4xxx_mem_alloc
1840 **/
1841static void qla4xxx_mem_free(struct scsi_qla_host *ha)
1842{
1843 if (ha->queues)
1844 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
1845 ha->queues_dma);
1846
1847 ha->queues_len = 0;
1848 ha->queues = NULL;
1849 ha->queues_dma = 0;
1850 ha->request_ring = NULL;
1851 ha->request_dma = 0;
1852 ha->response_ring = NULL;
1853 ha->response_dma = 0;
1854 ha->shadow_regs = NULL;
1855 ha->shadow_regs_dma = 0;
1856
1857 /* Free srb pool. */
1858 if (ha->srb_mempool)
1859 mempool_destroy(ha->srb_mempool);
1860
1861 ha->srb_mempool = NULL;
1862
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001863 if (ha->chap_dma_pool)
1864 dma_pool_destroy(ha->chap_dma_pool);
1865
Lalit Chandivade45494152011-10-07 16:55:42 -07001866 if (ha->chap_list)
1867 vfree(ha->chap_list);
1868 ha->chap_list = NULL;
1869
Mike Christie13483732011-12-01 21:38:41 -06001870 if (ha->fw_ddb_dma_pool)
1871 dma_pool_destroy(ha->fw_ddb_dma_pool);
1872
David Somayajuluafaf5a22006-09-19 10:28:00 -07001873 /* release io space registers */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301874 if (is_qla8022(ha)) {
1875 if (ha->nx_pcibase)
1876 iounmap(
1877 (struct device_reg_82xx __iomem *)ha->nx_pcibase);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301878 } else if (ha->reg)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001879 iounmap(ha->reg);
1880 pci_release_regions(ha->pdev);
1881}
1882
1883/**
1884 * qla4xxx_mem_alloc - allocates memory for use by adapter.
1885 * @ha: Pointer to host adapter structure
1886 *
1887 * Allocates DMA memory for request and response queues. Also allocates memory
1888 * for srbs.
1889 **/
1890static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
1891{
1892 unsigned long align;
1893
1894 /* Allocate contiguous block of DMA memory for queues. */
1895 ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
1896 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
1897 sizeof(struct shadow_regs) +
1898 MEM_ALIGN_VALUE +
1899 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
1900 ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
1901 &ha->queues_dma, GFP_KERNEL);
1902 if (ha->queues == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301903 ql4_printk(KERN_WARNING, ha,
1904 "Memory Allocation failed - queues.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001905
1906 goto mem_alloc_error_exit;
1907 }
1908 memset(ha->queues, 0, ha->queues_len);
1909
1910 /*
1911 * As per RISC alignment requirements -- the bus-address must be a
1912 * multiple of the request-ring size (in bytes).
1913 */
1914 align = 0;
1915 if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
1916 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
1917 (MEM_ALIGN_VALUE - 1));
1918
1919 /* Update request and response queue pointers. */
1920 ha->request_dma = ha->queues_dma + align;
1921 ha->request_ring = (struct queue_entry *) (ha->queues + align);
1922 ha->response_dma = ha->queues_dma + align +
1923 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
1924 ha->response_ring = (struct queue_entry *) (ha->queues + align +
1925 (REQUEST_QUEUE_DEPTH *
1926 QUEUE_SIZE));
1927 ha->shadow_regs_dma = ha->queues_dma + align +
1928 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
1929 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
1930 ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
1931 (REQUEST_QUEUE_DEPTH *
1932 QUEUE_SIZE) +
1933 (RESPONSE_QUEUE_DEPTH *
1934 QUEUE_SIZE));
1935
1936 /* Allocate memory for srb pool. */
1937 ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
1938 mempool_free_slab, srb_cachep);
1939 if (ha->srb_mempool == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301940 ql4_printk(KERN_WARNING, ha,
1941 "Memory Allocation failed - SRB Pool.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001942
1943 goto mem_alloc_error_exit;
1944 }
1945
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001946 ha->chap_dma_pool = dma_pool_create("ql4_chap", &ha->pdev->dev,
1947 CHAP_DMA_BLOCK_SIZE, 8, 0);
1948
1949 if (ha->chap_dma_pool == NULL) {
1950 ql4_printk(KERN_WARNING, ha,
1951 "%s: chap_dma_pool allocation failed..\n", __func__);
1952 goto mem_alloc_error_exit;
1953 }
1954
Mike Christie13483732011-12-01 21:38:41 -06001955 ha->fw_ddb_dma_pool = dma_pool_create("ql4_fw_ddb", &ha->pdev->dev,
1956 DDB_DMA_BLOCK_SIZE, 8, 0);
1957
1958 if (ha->fw_ddb_dma_pool == NULL) {
1959 ql4_printk(KERN_WARNING, ha,
1960 "%s: fw_ddb_dma_pool allocation failed..\n",
1961 __func__);
1962 goto mem_alloc_error_exit;
1963 }
1964
David Somayajuluafaf5a22006-09-19 10:28:00 -07001965 return QLA_SUCCESS;
1966
1967mem_alloc_error_exit:
1968 qla4xxx_mem_free(ha);
1969 return QLA_ERROR;
1970}
1971
1972/**
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301973 * qla4_8xxx_check_fw_alive - Check firmware health
1974 * @ha: Pointer to host adapter structure.
1975 *
1976 * Context: Interrupt
1977 **/
1978static void qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
1979{
1980 uint32_t fw_heartbeat_counter, halt_status;
1981
1982 fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
Lalit Chandivade2232be02010-07-30 14:38:47 +05301983 /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
1984 if (fw_heartbeat_counter == 0xffffffff) {
1985 DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
1986 "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
1987 ha->host_no, __func__));
1988 return;
1989 }
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301990
1991 if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
1992 ha->seconds_since_last_heartbeat++;
1993 /* FW not alive after 2 seconds */
1994 if (ha->seconds_since_last_heartbeat == 2) {
1995 ha->seconds_since_last_heartbeat = 0;
1996 halt_status = qla4_8xxx_rd_32(ha,
Vikas Chaudhary68d92eb2011-05-17 23:17:05 -07001997 QLA82XX_PEG_HALT_STATUS1);
1998
1999 ql4_printk(KERN_INFO, ha,
2000 "scsi(%ld): %s, Dumping hw/fw registers:\n "
2001 " PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2:"
2002 " 0x%x,\n PEG_NET_0_PC: 0x%x, PEG_NET_1_PC:"
2003 " 0x%x,\n PEG_NET_2_PC: 0x%x, PEG_NET_3_PC:"
2004 " 0x%x,\n PEG_NET_4_PC: 0x%x\n",
2005 ha->host_no, __func__, halt_status,
2006 qla4_8xxx_rd_32(ha,
2007 QLA82XX_PEG_HALT_STATUS2),
2008 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_0 +
2009 0x3c),
2010 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_1 +
2011 0x3c),
2012 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_2 +
2013 0x3c),
2014 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_3 +
2015 0x3c),
2016 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_4 +
2017 0x3c));
Nilesh Javali21033632010-07-30 14:28:07 +05302018
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302019 /* Since we cannot change dev_state in interrupt
2020 * context, set appropriate DPC flag then wakeup
2021 * DPC */
2022 if (halt_status & HALT_STATUS_UNRECOVERABLE)
2023 set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
2024 else {
2025 printk("scsi%ld: %s: detect abort needed!\n",
2026 ha->host_no, __func__);
2027 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2028 }
2029 qla4xxx_wake_dpc(ha);
Nilesh Javali21033632010-07-30 14:28:07 +05302030 qla4xxx_mailbox_premature_completion(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302031 }
Lalit Chandivade99457d72010-10-06 22:49:32 -07002032 } else
2033 ha->seconds_since_last_heartbeat = 0;
2034
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302035 ha->fw_heartbeat_counter = fw_heartbeat_counter;
2036}
2037
2038/**
2039 * qla4_8xxx_watchdog - Poll dev state
2040 * @ha: Pointer to host adapter structure.
2041 *
2042 * Context: Interrupt
2043 **/
2044void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
2045{
2046 uint32_t dev_state;
2047
2048 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
2049
2050 /* don't poll if reset is going on */
Lalit Chandivaded56a1f72010-12-02 22:12:45 -08002051 if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
2052 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
Vikas Chaudhary977f46a2011-05-17 23:17:08 -07002053 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags))) {
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302054 if (dev_state == QLA82XX_DEV_NEED_RESET &&
2055 !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
Vikas Chaudhary3930b8c2010-12-02 22:12:47 -08002056 if (!ql4xdontresethba) {
2057 ql4_printk(KERN_INFO, ha, "%s: HW State: "
2058 "NEED RESET!\n", __func__);
2059 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2060 qla4xxx_wake_dpc(ha);
2061 qla4xxx_mailbox_premature_completion(ha);
2062 }
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302063 } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
2064 !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
Vikas Chaudhary3930b8c2010-12-02 22:12:47 -08002065 ql4_printk(KERN_INFO, ha, "%s: HW State: NEED QUIES!\n",
2066 __func__);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302067 set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
2068 qla4xxx_wake_dpc(ha);
2069 } else {
2070 /* Check firmware health */
2071 qla4_8xxx_check_fw_alive(ha);
2072 }
2073 }
2074}
2075
Mike Christie13483732011-12-01 21:38:41 -06002076void qla4xxx_check_relogin_flash_ddb(struct iscsi_cls_session *cls_sess)
2077{
2078 struct iscsi_session *sess;
2079 struct ddb_entry *ddb_entry;
2080 struct scsi_qla_host *ha;
2081
2082 sess = cls_sess->dd_data;
2083 ddb_entry = sess->dd_data;
2084 ha = ddb_entry->ha;
2085
2086 if (!(ddb_entry->ddb_type == FLASH_DDB))
2087 return;
2088
2089 if (adapter_up(ha) && !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
2090 !iscsi_is_session_online(cls_sess)) {
2091 if (atomic_read(&ddb_entry->retry_relogin_timer) !=
2092 INVALID_ENTRY) {
2093 if (atomic_read(&ddb_entry->retry_relogin_timer) ==
2094 0) {
2095 atomic_set(&ddb_entry->retry_relogin_timer,
2096 INVALID_ENTRY);
2097 set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
2098 set_bit(DF_RELOGIN, &ddb_entry->flags);
2099 DEBUG2(ql4_printk(KERN_INFO, ha,
2100 "%s: index [%d] login device\n",
2101 __func__, ddb_entry->fw_ddb_index));
2102 } else
2103 atomic_dec(&ddb_entry->retry_relogin_timer);
2104 }
2105 }
2106
2107 /* Wait for relogin to timeout */
2108 if (atomic_read(&ddb_entry->relogin_timer) &&
2109 (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
2110 /*
2111 * If the relogin times out and the device is
2112 * still NOT ONLINE then try and relogin again.
2113 */
2114 if (!iscsi_is_session_online(cls_sess)) {
2115 /* Reset retry relogin timer */
2116 atomic_inc(&ddb_entry->relogin_retry_count);
2117 DEBUG2(ql4_printk(KERN_INFO, ha,
2118 "%s: index[%d] relogin timed out-retrying"
2119 " relogin (%d), retry (%d)\n", __func__,
2120 ddb_entry->fw_ddb_index,
2121 atomic_read(&ddb_entry->relogin_retry_count),
2122 ddb_entry->default_time2wait + 4));
2123 set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
2124 atomic_set(&ddb_entry->retry_relogin_timer,
2125 ddb_entry->default_time2wait + 4);
2126 }
2127 }
2128}
2129
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302130/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002131 * qla4xxx_timer - checks every second for work to do.
2132 * @ha: Pointer to host adapter structure.
2133 **/
2134static void qla4xxx_timer(struct scsi_qla_host *ha)
2135{
David Somayajuluafaf5a22006-09-19 10:28:00 -07002136 int start_dpc = 0;
Lalit Chandivade2232be02010-07-30 14:38:47 +05302137 uint16_t w;
2138
Mike Christie13483732011-12-01 21:38:41 -06002139 iscsi_host_for_each_session(ha->host, qla4xxx_check_relogin_flash_ddb);
2140
Lalit Chandivade2232be02010-07-30 14:38:47 +05302141 /* If we are in the middle of AER/EEH processing
2142 * skip any processing and reschedule the timer
2143 */
2144 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
2145 mod_timer(&ha->timer, jiffies + HZ);
2146 return;
2147 }
2148
2149 /* Hardware read to trigger an EEH error during mailbox waits. */
2150 if (!pci_channel_offline(ha->pdev))
2151 pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002152
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302153 if (is_qla8022(ha)) {
2154 qla4_8xxx_watchdog(ha);
2155 }
2156
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302157 if (!is_qla8022(ha)) {
2158 /* Check for heartbeat interval. */
2159 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
2160 ha->heartbeat_interval != 0) {
2161 ha->seconds_since_last_heartbeat++;
2162 if (ha->seconds_since_last_heartbeat >
2163 ha->heartbeat_interval + 2)
2164 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2165 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002166 }
2167
David Somayajuluafaf5a22006-09-19 10:28:00 -07002168 /* Wakeup the dpc routine for this adapter, if needed. */
Lalit Chandivade1b468072011-05-17 23:17:09 -07002169 if (start_dpc ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07002170 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
2171 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
2172 test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302173 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07002174 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
2175 test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05302176 test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302177 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
2178 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
Lalit Chandivade1b468072011-05-17 23:17:09 -07002179 test_bit(DPC_AEN, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002180 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
2181 " - dpc flags = 0x%lx\n",
2182 ha->host_no, __func__, ha->dpc_flags));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302183 qla4xxx_wake_dpc(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002184 }
2185
2186 /* Reschedule timer thread to call us back in one second */
2187 mod_timer(&ha->timer, jiffies + HZ);
2188
2189 DEBUG2(ha->seconds_since_last_intr++);
2190}
2191
2192/**
2193 * qla4xxx_cmd_wait - waits for all outstanding commands to complete
2194 * @ha: Pointer to host adapter structure.
2195 *
2196 * This routine stalls the driver until all outstanding commands are returned.
2197 * Caller must release the Hardware Lock prior to calling this routine.
2198 **/
2199static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
2200{
2201 uint32_t index = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002202 unsigned long flags;
2203 struct scsi_cmnd *cmd;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002204
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302205 unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
2206
2207 DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
2208 "complete\n", WAIT_CMD_TOV));
2209
2210 while (!time_after_eq(jiffies, wtime)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002211 spin_lock_irqsave(&ha->hardware_lock, flags);
2212 /* Find a command that hasn't completed. */
2213 for (index = 0; index < ha->host->can_queue; index++) {
2214 cmd = scsi_host_find_tag(ha->host, index);
Mike Christiea1e00632010-10-26 05:45:30 -07002215 /*
2216 * We cannot just check if the index is valid,
2217 * becase if we are run from the scsi eh, then
2218 * the scsi/block layer is going to prevent
2219 * the tag from being released.
2220 */
2221 if (cmd != NULL && CMD_SP(cmd))
David Somayajuluafaf5a22006-09-19 10:28:00 -07002222 break;
2223 }
2224 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2225
2226 /* If No Commands are pending, wait is complete */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302227 if (index == ha->host->can_queue)
2228 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002229
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302230 msleep(1000);
2231 }
2232 /* If we timed out on waiting for commands to come back
2233 * return ERROR. */
2234 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002235}
2236
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302237int qla4xxx_hw_reset(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002238{
David Somayajuluafaf5a22006-09-19 10:28:00 -07002239 uint32_t ctrl_status;
David C Somayajulu477ffb92007-01-22 12:26:11 -08002240 unsigned long flags = 0;
2241
2242 DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -07002243
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302244 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
2245 return QLA_ERROR;
2246
David Somayajuluafaf5a22006-09-19 10:28:00 -07002247 spin_lock_irqsave(&ha->hardware_lock, flags);
2248
2249 /*
2250 * If the SCSI Reset Interrupt bit is set, clear it.
2251 * Otherwise, the Soft Reset won't work.
2252 */
2253 ctrl_status = readw(&ha->reg->ctrl_status);
2254 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
2255 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
2256
2257 /* Issue Soft Reset */
2258 writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
2259 readl(&ha->reg->ctrl_status);
2260
2261 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302262 return QLA_SUCCESS;
David C Somayajulu477ffb92007-01-22 12:26:11 -08002263}
2264
2265/**
2266 * qla4xxx_soft_reset - performs soft reset.
2267 * @ha: Pointer to host adapter structure.
2268 **/
2269int qla4xxx_soft_reset(struct scsi_qla_host *ha)
2270{
2271 uint32_t max_wait_time;
2272 unsigned long flags = 0;
Vikas Chaudharyf931c532010-10-06 22:48:07 -07002273 int status;
David C Somayajulu477ffb92007-01-22 12:26:11 -08002274 uint32_t ctrl_status;
2275
Vikas Chaudharyf931c532010-10-06 22:48:07 -07002276 status = qla4xxx_hw_reset(ha);
2277 if (status != QLA_SUCCESS)
2278 return status;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002279
Vikas Chaudharyf931c532010-10-06 22:48:07 -07002280 status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002281 /* Wait until the Network Reset Intr bit is cleared */
2282 max_wait_time = RESET_INTR_TOV;
2283 do {
2284 spin_lock_irqsave(&ha->hardware_lock, flags);
2285 ctrl_status = readw(&ha->reg->ctrl_status);
2286 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2287
2288 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
2289 break;
2290
2291 msleep(1000);
2292 } while ((--max_wait_time));
2293
2294 if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
2295 DEBUG2(printk(KERN_WARNING
2296 "scsi%ld: Network Reset Intr not cleared by "
2297 "Network function, clearing it now!\n",
2298 ha->host_no));
2299 spin_lock_irqsave(&ha->hardware_lock, flags);
2300 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
2301 readl(&ha->reg->ctrl_status);
2302 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2303 }
2304
2305 /* Wait until the firmware tells us the Soft Reset is done */
2306 max_wait_time = SOFT_RESET_TOV;
2307 do {
2308 spin_lock_irqsave(&ha->hardware_lock, flags);
2309 ctrl_status = readw(&ha->reg->ctrl_status);
2310 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2311
2312 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
2313 status = QLA_SUCCESS;
2314 break;
2315 }
2316
2317 msleep(1000);
2318 } while ((--max_wait_time));
2319
2320 /*
2321 * Also, make sure that the SCSI Reset Interrupt bit has been cleared
2322 * after the soft reset has taken place.
2323 */
2324 spin_lock_irqsave(&ha->hardware_lock, flags);
2325 ctrl_status = readw(&ha->reg->ctrl_status);
2326 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
2327 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
2328 readl(&ha->reg->ctrl_status);
2329 }
2330 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2331
2332 /* If soft reset fails then most probably the bios on other
2333 * function is also enabled.
2334 * Since the initialization is sequential the other fn
2335 * wont be able to acknowledge the soft reset.
2336 * Issue a force soft reset to workaround this scenario.
2337 */
2338 if (max_wait_time == 0) {
2339 /* Issue Force Soft Reset */
2340 spin_lock_irqsave(&ha->hardware_lock, flags);
2341 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
2342 readl(&ha->reg->ctrl_status);
2343 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2344 /* Wait until the firmware tells us the Soft Reset is done */
2345 max_wait_time = SOFT_RESET_TOV;
2346 do {
2347 spin_lock_irqsave(&ha->hardware_lock, flags);
2348 ctrl_status = readw(&ha->reg->ctrl_status);
2349 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2350
2351 if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
2352 status = QLA_SUCCESS;
2353 break;
2354 }
2355
2356 msleep(1000);
2357 } while ((--max_wait_time));
2358 }
2359
2360 return status;
2361}
2362
2363/**
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302364 * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
David Somayajuluafaf5a22006-09-19 10:28:00 -07002365 * @ha: Pointer to host adapter structure.
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302366 * @res: returned scsi status
David Somayajuluafaf5a22006-09-19 10:28:00 -07002367 *
2368 * This routine is called just prior to a HARD RESET to return all
2369 * outstanding commands back to the Operating System.
2370 * Caller should make sure that the following locks are released
2371 * before this calling routine: Hardware lock, and io_request_lock.
2372 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302373static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002374{
2375 struct srb *srb;
2376 int i;
2377 unsigned long flags;
2378
2379 spin_lock_irqsave(&ha->hardware_lock, flags);
2380 for (i = 0; i < ha->host->can_queue; i++) {
2381 srb = qla4xxx_del_from_active_array(ha, i);
2382 if (srb != NULL) {
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302383 srb->cmd->result = res;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302384 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002385 }
2386 }
2387 spin_unlock_irqrestore(&ha->hardware_lock, flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002388}
2389
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302390void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
2391{
2392 clear_bit(AF_ONLINE, &ha->flags);
2393
2394 /* Disable the board */
2395 ql4_printk(KERN_INFO, ha, "Disabling the board\n");
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302396
2397 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
2398 qla4xxx_mark_all_devices_missing(ha);
2399 clear_bit(AF_INIT_DONE, &ha->flags);
2400}
2401
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002402static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session)
2403{
2404 struct iscsi_session *sess;
2405 struct ddb_entry *ddb_entry;
2406
2407 sess = cls_session->dd_data;
2408 ddb_entry = sess->dd_data;
2409 ddb_entry->fw_ddb_device_state = DDB_DS_SESSION_FAILED;
Mike Christie13483732011-12-01 21:38:41 -06002410
2411 if (ddb_entry->ddb_type == FLASH_DDB)
2412 iscsi_block_session(ddb_entry->sess);
2413 else
2414 iscsi_session_failure(cls_session->dd_data,
2415 ISCSI_ERR_CONN_FAILED);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002416}
2417
David Somayajuluafaf5a22006-09-19 10:28:00 -07002418/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002419 * qla4xxx_recover_adapter - recovers adapter after a fatal error
2420 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07002421 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302422static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002423{
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302424 int status = QLA_ERROR;
2425 uint8_t reset_chip = 0;
Sarang Radke8e0f3a62011-12-01 22:42:09 -08002426 uint32_t dev_state;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002427
2428 /* Stall incoming I/O until we are done */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302429 scsi_block_requests(ha->host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002430 clear_bit(AF_ONLINE, &ha->flags);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002431 clear_bit(AF_LINK_UP, &ha->flags);
Mike Christie50a29ae2008-03-04 13:26:53 -06002432
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302433 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -07002434
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302435 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002436
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002437 iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
2438
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302439 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
2440 reset_chip = 1;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002441
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302442 /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
2443 * do not reset adapter, jump to initialize_adapter */
2444 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
2445 status = QLA_SUCCESS;
2446 goto recover_ha_init_adapter;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002447 }
2448
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302449 /* For the ISP-82xx adapter, issue a stop_firmware if invoked
2450 * from eh_host_reset or ioctl module */
2451 if (is_qla8022(ha) && !reset_chip &&
2452 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
2453
2454 DEBUG2(ql4_printk(KERN_INFO, ha,
2455 "scsi%ld: %s - Performing stop_firmware...\n",
2456 ha->host_no, __func__));
2457 status = ha->isp_ops->reset_firmware(ha);
2458 if (status == QLA_SUCCESS) {
Nilesh Javali2bd1e2b2010-10-06 22:49:20 -07002459 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
2460 qla4xxx_cmd_wait(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302461 ha->isp_ops->disable_intrs(ha);
2462 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2463 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
2464 } else {
2465 /* If the stop_firmware fails then
2466 * reset the entire chip */
2467 reset_chip = 1;
2468 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2469 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2470 }
2471 }
2472
2473 /* Issue full chip reset if recovering from a catastrophic error,
2474 * or if stop_firmware fails for ISP-82xx.
2475 * This is the default case for ISP-4xxx */
2476 if (!is_qla8022(ha) || reset_chip) {
Nilesh Javali2bd1e2b2010-10-06 22:49:20 -07002477 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
2478 qla4xxx_cmd_wait(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302479 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2480 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
2481 DEBUG2(ql4_printk(KERN_INFO, ha,
2482 "scsi%ld: %s - Performing chip reset..\n",
2483 ha->host_no, __func__));
2484 status = ha->isp_ops->reset_chip(ha);
2485 }
2486
2487 /* Flush any pending ddb changed AENs */
2488 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2489
2490recover_ha_init_adapter:
2491 /* Upon successful firmware/chip reset, re-initialize the adapter */
2492 if (status == QLA_SUCCESS) {
2493 /* For ISP-4xxx, force function 1 to always initialize
2494 * before function 3 to prevent both funcions from
2495 * stepping on top of the other */
2496 if (!is_qla8022(ha) && (ha->mac_index == 3))
2497 ssleep(6);
2498
2499 /* NOTE: AF_ONLINE flag set upon successful completion of
2500 * qla4xxx_initialize_adapter */
Mike Christie13483732011-12-01 21:38:41 -06002501 status = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302502 }
2503
2504 /* Retry failed adapter initialization, if necessary
2505 * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
2506 * case to prevent ping-pong resets between functions */
2507 if (!test_bit(AF_ONLINE, &ha->flags) &&
2508 !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002509 /* Adapter initialization failed, see if we can retry
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302510 * resetting the ha.
2511 * Since we don't want to block the DPC for too long
2512 * with multiple resets in the same thread,
2513 * utilize DPC to retry */
Sarang Radke8e0f3a62011-12-01 22:42:09 -08002514 if (is_qla8022(ha)) {
2515 qla4_8xxx_idc_lock(ha);
2516 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
2517 qla4_8xxx_idc_unlock(ha);
2518 if (dev_state == QLA82XX_DEV_FAILED) {
2519 ql4_printk(KERN_INFO, ha, "%s: don't retry "
2520 "recover adapter. H/W is in Failed "
2521 "state\n", __func__);
2522 qla4xxx_dead_adapter_cleanup(ha);
2523 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
2524 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
2525 clear_bit(DPC_RESET_HA_FW_CONTEXT,
2526 &ha->dpc_flags);
2527 status = QLA_ERROR;
2528
2529 goto exit_recover;
2530 }
2531 }
2532
David Somayajuluafaf5a22006-09-19 10:28:00 -07002533 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
2534 ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
2535 DEBUG2(printk("scsi%ld: recover adapter - retrying "
2536 "(%d) more times\n", ha->host_no,
2537 ha->retry_reset_ha_cnt));
2538 set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
2539 status = QLA_ERROR;
2540 } else {
2541 if (ha->retry_reset_ha_cnt > 0) {
2542 /* Schedule another Reset HA--DPC will retry */
2543 ha->retry_reset_ha_cnt--;
2544 DEBUG2(printk("scsi%ld: recover adapter - "
2545 "retry remaining %d\n",
2546 ha->host_no,
2547 ha->retry_reset_ha_cnt));
2548 status = QLA_ERROR;
2549 }
2550
2551 if (ha->retry_reset_ha_cnt == 0) {
2552 /* Recover adapter retries have been exhausted.
2553 * Adapter DEAD */
2554 DEBUG2(printk("scsi%ld: recover adapter "
2555 "failed - board disabled\n",
2556 ha->host_no));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302557 qla4xxx_dead_adapter_cleanup(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002558 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
2559 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302560 clear_bit(DPC_RESET_HA_FW_CONTEXT,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002561 &ha->dpc_flags);
2562 status = QLA_ERROR;
2563 }
2564 }
2565 } else {
2566 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302567 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002568 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
2569 }
2570
Sarang Radke8e0f3a62011-12-01 22:42:09 -08002571exit_recover:
David Somayajuluafaf5a22006-09-19 10:28:00 -07002572 ha->adapter_error_count++;
2573
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302574 if (test_bit(AF_ONLINE, &ha->flags))
2575 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002576
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302577 scsi_unblock_requests(ha->host);
2578
2579 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2580 DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002581 status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302582
David Somayajuluafaf5a22006-09-19 10:28:00 -07002583 return status;
2584}
2585
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002586static void qla4xxx_relogin_devices(struct iscsi_cls_session *cls_session)
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07002587{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002588 struct iscsi_session *sess;
2589 struct ddb_entry *ddb_entry;
2590 struct scsi_qla_host *ha;
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07002591
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002592 sess = cls_session->dd_data;
2593 ddb_entry = sess->dd_data;
2594 ha = ddb_entry->ha;
2595 if (!iscsi_is_session_online(cls_session)) {
2596 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
2597 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
2598 " unblock session\n", ha->host_no, __func__,
2599 ddb_entry->fw_ddb_index);
2600 iscsi_unblock_session(ddb_entry->sess);
2601 } else {
2602 /* Trigger relogin */
Mike Christie13483732011-12-01 21:38:41 -06002603 if (ddb_entry->ddb_type == FLASH_DDB) {
2604 if (!test_bit(DF_RELOGIN, &ddb_entry->flags))
2605 qla4xxx_arm_relogin_timer(ddb_entry);
2606 } else
2607 iscsi_session_failure(cls_session->dd_data,
2608 ISCSI_ERR_CONN_FAILED);
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07002609 }
2610 }
2611}
2612
Mike Christie13483732011-12-01 21:38:41 -06002613int qla4xxx_unblock_flash_ddb(struct iscsi_cls_session *cls_session)
2614{
2615 struct iscsi_session *sess;
2616 struct ddb_entry *ddb_entry;
2617 struct scsi_qla_host *ha;
2618
2619 sess = cls_session->dd_data;
2620 ddb_entry = sess->dd_data;
2621 ha = ddb_entry->ha;
2622 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
2623 " unblock session\n", ha->host_no, __func__,
2624 ddb_entry->fw_ddb_index);
2625
2626 iscsi_unblock_session(ddb_entry->sess);
2627
2628 /* Start scan target */
2629 if (test_bit(AF_ONLINE, &ha->flags)) {
2630 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
2631 " start scan\n", ha->host_no, __func__,
2632 ddb_entry->fw_ddb_index);
2633 scsi_queue_work(ha->host, &ddb_entry->sess->scan_work);
2634 }
2635 return QLA_SUCCESS;
2636}
2637
2638int qla4xxx_unblock_ddb(struct iscsi_cls_session *cls_session)
2639{
2640 struct iscsi_session *sess;
2641 struct ddb_entry *ddb_entry;
2642 struct scsi_qla_host *ha;
2643
2644 sess = cls_session->dd_data;
2645 ddb_entry = sess->dd_data;
2646 ha = ddb_entry->ha;
2647 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
2648 " unblock user space session\n", ha->host_no, __func__,
2649 ddb_entry->fw_ddb_index);
2650 iscsi_conn_start(ddb_entry->conn);
2651 iscsi_conn_login_event(ddb_entry->conn,
2652 ISCSI_CONN_STATE_LOGGED_IN);
2653
2654 return QLA_SUCCESS;
2655}
2656
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002657static void qla4xxx_relogin_all_devices(struct scsi_qla_host *ha)
2658{
2659 iscsi_host_for_each_session(ha->host, qla4xxx_relogin_devices);
2660}
2661
Mike Christie13483732011-12-01 21:38:41 -06002662static void qla4xxx_relogin_flash_ddb(struct iscsi_cls_session *cls_sess)
2663{
2664 uint16_t relogin_timer;
2665 struct iscsi_session *sess;
2666 struct ddb_entry *ddb_entry;
2667 struct scsi_qla_host *ha;
2668
2669 sess = cls_sess->dd_data;
2670 ddb_entry = sess->dd_data;
2671 ha = ddb_entry->ha;
2672
2673 relogin_timer = max(ddb_entry->default_relogin_timeout,
2674 (uint16_t)RELOGIN_TOV);
2675 atomic_set(&ddb_entry->relogin_timer, relogin_timer);
2676
2677 DEBUG2(ql4_printk(KERN_INFO, ha,
2678 "scsi%ld: Relogin index [%d]. TOV=%d\n", ha->host_no,
2679 ddb_entry->fw_ddb_index, relogin_timer));
2680
2681 qla4xxx_login_flash_ddb(cls_sess);
2682}
2683
2684static void qla4xxx_dpc_relogin(struct iscsi_cls_session *cls_sess)
2685{
2686 struct iscsi_session *sess;
2687 struct ddb_entry *ddb_entry;
2688 struct scsi_qla_host *ha;
2689
2690 sess = cls_sess->dd_data;
2691 ddb_entry = sess->dd_data;
2692 ha = ddb_entry->ha;
2693
2694 if (!(ddb_entry->ddb_type == FLASH_DDB))
2695 return;
2696
2697 if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
2698 !iscsi_is_session_online(cls_sess)) {
2699 DEBUG2(ql4_printk(KERN_INFO, ha,
2700 "relogin issued\n"));
2701 qla4xxx_relogin_flash_ddb(cls_sess);
2702 }
2703}
2704
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302705void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
2706{
Lalit Chandivade1b468072011-05-17 23:17:09 -07002707 if (ha->dpc_thread)
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302708 queue_work(ha->dpc_thread, &ha->dpc_work);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302709}
2710
David Somayajuluafaf5a22006-09-19 10:28:00 -07002711/**
2712 * qla4xxx_do_dpc - dpc routine
2713 * @data: in our case pointer to adapter structure
2714 *
2715 * This routine is a task that is schedule by the interrupt handler
2716 * to perform the background processing for interrupts. We put it
2717 * on a task queue that is consumed whenever the scheduler runs; that's
2718 * so you can do anything (i.e. put the process to sleep etc). In fact,
2719 * the mid-level tries to sleep when it reaches the driver threshold
2720 * "host->can_queue". This can cause a panic if we were in our interrupt code.
2721 **/
David Howellsc4028952006-11-22 14:57:56 +00002722static void qla4xxx_do_dpc(struct work_struct *work)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002723{
David Howellsc4028952006-11-22 14:57:56 +00002724 struct scsi_qla_host *ha =
2725 container_of(work, struct scsi_qla_host, dpc_work);
David C Somayajulu477ffb92007-01-22 12:26:11 -08002726 int status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002727
David C Somayajuluf26b9042006-11-15 16:41:09 -08002728 DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302729 "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
2730 ha->host_no, __func__, ha->flags, ha->dpc_flags))
David Somayajuluafaf5a22006-09-19 10:28:00 -07002731
2732 /* Initialization not yet finished. Don't do anything yet. */
2733 if (!test_bit(AF_INIT_DONE, &ha->flags))
Lalit Chandivade1b468072011-05-17 23:17:09 -07002734 return;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002735
Lalit Chandivade2232be02010-07-30 14:38:47 +05302736 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
2737 DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
2738 ha->host_no, __func__, ha->flags));
Lalit Chandivade1b468072011-05-17 23:17:09 -07002739 return;
Lalit Chandivade2232be02010-07-30 14:38:47 +05302740 }
2741
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302742 if (is_qla8022(ha)) {
2743 if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
2744 qla4_8xxx_idc_lock(ha);
2745 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2746 QLA82XX_DEV_FAILED);
2747 qla4_8xxx_idc_unlock(ha);
2748 ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
2749 qla4_8xxx_device_state_handler(ha);
2750 }
2751 if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
2752 qla4_8xxx_need_qsnt_handler(ha);
2753 }
2754 }
2755
2756 if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
2757 (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07002758 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302759 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
2760 if (ql4xdontresethba) {
2761 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
2762 ha->host_no, __func__));
2763 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
2764 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
2765 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2766 goto dpc_post_reset_ha;
2767 }
2768 if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
2769 test_bit(DPC_RESET_HA, &ha->dpc_flags))
2770 qla4xxx_recover_adapter(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002771
David C Somayajulu477ffb92007-01-22 12:26:11 -08002772 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002773 uint8_t wait_time = RESET_INTR_TOV;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002774
David Somayajuluafaf5a22006-09-19 10:28:00 -07002775 while ((readw(&ha->reg->ctrl_status) &
2776 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
2777 if (--wait_time == 0)
2778 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002779 msleep(1000);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002780 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002781 if (wait_time == 0)
2782 DEBUG2(printk("scsi%ld: %s: SR|FSR "
2783 "bit not cleared-- resetting\n",
2784 ha->host_no, __func__));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302785 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
David C Somayajulu477ffb92007-01-22 12:26:11 -08002786 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
2787 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302788 status = qla4xxx_recover_adapter(ha);
David C Somayajulu477ffb92007-01-22 12:26:11 -08002789 }
2790 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
2791 if (status == QLA_SUCCESS)
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302792 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002793 }
2794 }
2795
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302796dpc_post_reset_ha:
David Somayajuluafaf5a22006-09-19 10:28:00 -07002797 /* ---- process AEN? --- */
2798 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
2799 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
2800
2801 /* ---- Get DHCP IP Address? --- */
2802 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
2803 qla4xxx_get_dhcp_ip_address(ha);
2804
Mike Christie13483732011-12-01 21:38:41 -06002805 /* ---- relogin device? --- */
2806 if (adapter_up(ha) &&
2807 test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
2808 iscsi_host_for_each_session(ha->host, qla4xxx_dpc_relogin);
2809 }
2810
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05302811 /* ---- link change? --- */
2812 if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
2813 if (!test_bit(AF_LINK_UP, &ha->flags)) {
2814 /* ---- link down? --- */
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07002815 qla4xxx_mark_all_devices_missing(ha);
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05302816 } else {
2817 /* ---- link up? --- *
2818 * F/W will auto login to all devices ONLY ONCE after
2819 * link up during driver initialization and runtime
2820 * fatal error recovery. Therefore, the driver must
2821 * manually relogin to devices when recovering from
2822 * connection failures, logouts, expired KATO, etc. */
Mike Christie13483732011-12-01 21:38:41 -06002823 if (test_and_clear_bit(AF_BUILD_DDB_LIST, &ha->flags)) {
2824 qla4xxx_build_ddb_list(ha, ha->is_reset);
2825 iscsi_host_for_each_session(ha->host,
2826 qla4xxx_login_flash_ddb);
2827 } else
2828 qla4xxx_relogin_all_devices(ha);
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05302829 }
2830 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002831}
2832
2833/**
2834 * qla4xxx_free_adapter - release the adapter
2835 * @ha: pointer to adapter structure
2836 **/
2837static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
2838{
Sarang Radke8a288962011-12-06 02:34:10 -08002839 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002840
2841 if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
2842 /* Turn-off interrupts on the card. */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302843 ha->isp_ops->disable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002844 }
2845
David Somayajuluafaf5a22006-09-19 10:28:00 -07002846 /* Remove timer thread, if present */
2847 if (ha->timer_active)
2848 qla4xxx_stop_timer(ha);
2849
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302850 /* Kill the kernel thread for this host */
2851 if (ha->dpc_thread)
2852 destroy_workqueue(ha->dpc_thread);
2853
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002854 /* Kill the kernel thread for this host */
2855 if (ha->task_wq)
2856 destroy_workqueue(ha->task_wq);
2857
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302858 /* Put firmware in known state */
2859 ha->isp_ops->reset_firmware(ha);
2860
2861 if (is_qla8022(ha)) {
2862 qla4_8xxx_idc_lock(ha);
2863 qla4_8xxx_clear_drv_active(ha);
2864 qla4_8xxx_idc_unlock(ha);
2865 }
2866
David Somayajuluafaf5a22006-09-19 10:28:00 -07002867 /* Detach interrupts */
2868 if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302869 qla4xxx_free_irqs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002870
David C Somayajulubee4fe82007-05-23 18:03:32 -07002871 /* free extra memory */
2872 qla4xxx_mem_free(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302873}
David C Somayajulubee4fe82007-05-23 18:03:32 -07002874
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302875int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
2876{
2877 int status = 0;
2878 uint8_t revision_id;
2879 unsigned long mem_base, mem_len, db_base, db_len;
2880 struct pci_dev *pdev = ha->pdev;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002881
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302882 status = pci_request_regions(pdev, DRIVER_NAME);
2883 if (status) {
2884 printk(KERN_WARNING
2885 "scsi(%ld) Failed to reserve PIO regions (%s) "
2886 "status=%d\n", ha->host_no, pci_name(pdev), status);
2887 goto iospace_error_exit;
2888 }
2889
2890 pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
2891 DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
2892 __func__, revision_id));
2893 ha->revision_id = revision_id;
2894
2895 /* remap phys address */
2896 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
2897 mem_len = pci_resource_len(pdev, 0);
2898 DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
2899 __func__, mem_base, mem_len));
2900
2901 /* mapping of pcibase pointer */
2902 ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
2903 if (!ha->nx_pcibase) {
2904 printk(KERN_ERR
2905 "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
2906 pci_release_regions(ha->pdev);
2907 goto iospace_error_exit;
2908 }
2909
2910 /* Mapping of IO base pointer, door bell read and write pointer */
2911
2912 /* mapping of IO base pointer */
2913 ha->qla4_8xxx_reg =
2914 (struct device_reg_82xx __iomem *)((uint8_t *)ha->nx_pcibase +
2915 0xbc000 + (ha->pdev->devfn << 11));
2916
2917 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
2918 db_len = pci_resource_len(pdev, 4);
2919
Shyam Sundar2657c802010-10-06 22:50:29 -07002920 ha->nx_db_wr_ptr = (ha->pdev->devfn == 4 ? QLA82XX_CAM_RAM_DB1 :
2921 QLA82XX_CAM_RAM_DB2);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302922
Shyam Sundar2657c802010-10-06 22:50:29 -07002923 return 0;
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302924iospace_error_exit:
2925 return -ENOMEM;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002926}
2927
2928/***
2929 * qla4xxx_iospace_config - maps registers
2930 * @ha: pointer to adapter structure
2931 *
2932 * This routines maps HBA's registers from the pci address space
2933 * into the kernel virtual address space for memory mapped i/o.
2934 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302935int qla4xxx_iospace_config(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002936{
2937 unsigned long pio, pio_len, pio_flags;
2938 unsigned long mmio, mmio_len, mmio_flags;
2939
2940 pio = pci_resource_start(ha->pdev, 0);
2941 pio_len = pci_resource_len(ha->pdev, 0);
2942 pio_flags = pci_resource_flags(ha->pdev, 0);
2943 if (pio_flags & IORESOURCE_IO) {
2944 if (pio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302945 ql4_printk(KERN_WARNING, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002946 "Invalid PCI I/O region size\n");
2947 pio = 0;
2948 }
2949 } else {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302950 ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002951 pio = 0;
2952 }
2953
2954 /* Use MMIO operations for all accesses. */
2955 mmio = pci_resource_start(ha->pdev, 1);
2956 mmio_len = pci_resource_len(ha->pdev, 1);
2957 mmio_flags = pci_resource_flags(ha->pdev, 1);
2958
2959 if (!(mmio_flags & IORESOURCE_MEM)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302960 ql4_printk(KERN_ERR, ha,
2961 "region #0 not an MMIO resource, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002962
2963 goto iospace_error_exit;
2964 }
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302965
David Somayajuluafaf5a22006-09-19 10:28:00 -07002966 if (mmio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302967 ql4_printk(KERN_ERR, ha,
2968 "Invalid PCI mem region size, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002969 goto iospace_error_exit;
2970 }
2971
2972 if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302973 ql4_printk(KERN_WARNING, ha,
2974 "Failed to reserve PIO/MMIO regions\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002975
2976 goto iospace_error_exit;
2977 }
2978
2979 ha->pio_address = pio;
2980 ha->pio_length = pio_len;
2981 ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
2982 if (!ha->reg) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302983 ql4_printk(KERN_ERR, ha,
2984 "cannot remap MMIO, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002985
2986 goto iospace_error_exit;
2987 }
2988
2989 return 0;
2990
2991iospace_error_exit:
2992 return -ENOMEM;
2993}
2994
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302995static struct isp_operations qla4xxx_isp_ops = {
2996 .iospace_config = qla4xxx_iospace_config,
2997 .pci_config = qla4xxx_pci_config,
2998 .disable_intrs = qla4xxx_disable_intrs,
2999 .enable_intrs = qla4xxx_enable_intrs,
3000 .start_firmware = qla4xxx_start_firmware,
3001 .intr_handler = qla4xxx_intr_handler,
3002 .interrupt_service_routine = qla4xxx_interrupt_service_routine,
3003 .reset_chip = qla4xxx_soft_reset,
3004 .reset_firmware = qla4xxx_hw_reset,
3005 .queue_iocb = qla4xxx_queue_iocb,
3006 .complete_iocb = qla4xxx_complete_iocb,
3007 .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
3008 .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
3009 .get_sys_info = qla4xxx_get_sys_info,
3010};
3011
3012static struct isp_operations qla4_8xxx_isp_ops = {
3013 .iospace_config = qla4_8xxx_iospace_config,
3014 .pci_config = qla4_8xxx_pci_config,
3015 .disable_intrs = qla4_8xxx_disable_intrs,
3016 .enable_intrs = qla4_8xxx_enable_intrs,
3017 .start_firmware = qla4_8xxx_load_risc,
3018 .intr_handler = qla4_8xxx_intr_handler,
3019 .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
3020 .reset_chip = qla4_8xxx_isp_reset,
3021 .reset_firmware = qla4_8xxx_stop_firmware,
3022 .queue_iocb = qla4_8xxx_queue_iocb,
3023 .complete_iocb = qla4_8xxx_complete_iocb,
3024 .rd_shdw_req_q_out = qla4_8xxx_rd_shdw_req_q_out,
3025 .rd_shdw_rsp_q_in = qla4_8xxx_rd_shdw_rsp_q_in,
3026 .get_sys_info = qla4_8xxx_get_sys_info,
3027};
3028
3029uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
3030{
3031 return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
3032}
3033
3034uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
3035{
3036 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
3037}
3038
3039uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
3040{
3041 return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
3042}
3043
3044uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
3045{
3046 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
3047}
3048
Manish Rangankar2a991c22011-07-25 13:48:55 -05003049static ssize_t qla4xxx_show_boot_eth_info(void *data, int type, char *buf)
3050{
3051 struct scsi_qla_host *ha = data;
3052 char *str = buf;
3053 int rc;
3054
3055 switch (type) {
3056 case ISCSI_BOOT_ETH_FLAGS:
3057 rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
3058 break;
3059 case ISCSI_BOOT_ETH_INDEX:
3060 rc = sprintf(str, "0\n");
3061 break;
3062 case ISCSI_BOOT_ETH_MAC:
3063 rc = sysfs_format_mac(str, ha->my_mac,
3064 MAC_ADDR_LEN);
3065 break;
3066 default:
3067 rc = -ENOSYS;
3068 break;
3069 }
3070 return rc;
3071}
3072
3073static mode_t qla4xxx_eth_get_attr_visibility(void *data, int type)
3074{
3075 int rc;
3076
3077 switch (type) {
3078 case ISCSI_BOOT_ETH_FLAGS:
3079 case ISCSI_BOOT_ETH_MAC:
3080 case ISCSI_BOOT_ETH_INDEX:
3081 rc = S_IRUGO;
3082 break;
3083 default:
3084 rc = 0;
3085 break;
3086 }
3087 return rc;
3088}
3089
3090static ssize_t qla4xxx_show_boot_ini_info(void *data, int type, char *buf)
3091{
3092 struct scsi_qla_host *ha = data;
3093 char *str = buf;
3094 int rc;
3095
3096 switch (type) {
3097 case ISCSI_BOOT_INI_INITIATOR_NAME:
3098 rc = sprintf(str, "%s\n", ha->name_string);
3099 break;
3100 default:
3101 rc = -ENOSYS;
3102 break;
3103 }
3104 return rc;
3105}
3106
3107static mode_t qla4xxx_ini_get_attr_visibility(void *data, int type)
3108{
3109 int rc;
3110
3111 switch (type) {
3112 case ISCSI_BOOT_INI_INITIATOR_NAME:
3113 rc = S_IRUGO;
3114 break;
3115 default:
3116 rc = 0;
3117 break;
3118 }
3119 return rc;
3120}
3121
3122static ssize_t
3123qla4xxx_show_boot_tgt_info(struct ql4_boot_session_info *boot_sess, int type,
3124 char *buf)
3125{
3126 struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
3127 char *str = buf;
3128 int rc;
3129
3130 switch (type) {
3131 case ISCSI_BOOT_TGT_NAME:
3132 rc = sprintf(buf, "%s\n", (char *)&boot_sess->target_name);
3133 break;
3134 case ISCSI_BOOT_TGT_IP_ADDR:
3135 if (boot_sess->conn_list[0].dest_ipaddr.ip_type == 0x1)
3136 rc = sprintf(buf, "%pI4\n",
3137 &boot_conn->dest_ipaddr.ip_address);
3138 else
3139 rc = sprintf(str, "%pI6\n",
3140 &boot_conn->dest_ipaddr.ip_address);
3141 break;
3142 case ISCSI_BOOT_TGT_PORT:
3143 rc = sprintf(str, "%d\n", boot_conn->dest_port);
3144 break;
3145 case ISCSI_BOOT_TGT_CHAP_NAME:
3146 rc = sprintf(str, "%.*s\n",
3147 boot_conn->chap.target_chap_name_length,
3148 (char *)&boot_conn->chap.target_chap_name);
3149 break;
3150 case ISCSI_BOOT_TGT_CHAP_SECRET:
3151 rc = sprintf(str, "%.*s\n",
3152 boot_conn->chap.target_secret_length,
3153 (char *)&boot_conn->chap.target_secret);
3154 break;
3155 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
3156 rc = sprintf(str, "%.*s\n",
3157 boot_conn->chap.intr_chap_name_length,
3158 (char *)&boot_conn->chap.intr_chap_name);
3159 break;
3160 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
3161 rc = sprintf(str, "%.*s\n",
3162 boot_conn->chap.intr_secret_length,
3163 (char *)&boot_conn->chap.intr_secret);
3164 break;
3165 case ISCSI_BOOT_TGT_FLAGS:
3166 rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
3167 break;
3168 case ISCSI_BOOT_TGT_NIC_ASSOC:
3169 rc = sprintf(str, "0\n");
3170 break;
3171 default:
3172 rc = -ENOSYS;
3173 break;
3174 }
3175 return rc;
3176}
3177
3178static ssize_t qla4xxx_show_boot_tgt_pri_info(void *data, int type, char *buf)
3179{
3180 struct scsi_qla_host *ha = data;
3181 struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_pri_sess);
3182
3183 return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
3184}
3185
3186static ssize_t qla4xxx_show_boot_tgt_sec_info(void *data, int type, char *buf)
3187{
3188 struct scsi_qla_host *ha = data;
3189 struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_sec_sess);
3190
3191 return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
3192}
3193
3194static mode_t qla4xxx_tgt_get_attr_visibility(void *data, int type)
3195{
3196 int rc;
3197
3198 switch (type) {
3199 case ISCSI_BOOT_TGT_NAME:
3200 case ISCSI_BOOT_TGT_IP_ADDR:
3201 case ISCSI_BOOT_TGT_PORT:
3202 case ISCSI_BOOT_TGT_CHAP_NAME:
3203 case ISCSI_BOOT_TGT_CHAP_SECRET:
3204 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
3205 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
3206 case ISCSI_BOOT_TGT_NIC_ASSOC:
3207 case ISCSI_BOOT_TGT_FLAGS:
3208 rc = S_IRUGO;
3209 break;
3210 default:
3211 rc = 0;
3212 break;
3213 }
3214 return rc;
3215}
3216
3217static void qla4xxx_boot_release(void *data)
3218{
3219 struct scsi_qla_host *ha = data;
3220
3221 scsi_host_put(ha->host);
3222}
3223
3224static int get_fw_boot_info(struct scsi_qla_host *ha, uint16_t ddb_index[])
3225{
3226 dma_addr_t buf_dma;
3227 uint32_t addr, pri_addr, sec_addr;
3228 uint32_t offset;
3229 uint16_t func_num;
3230 uint8_t val;
3231 uint8_t *buf = NULL;
3232 size_t size = 13 * sizeof(uint8_t);
3233 int ret = QLA_SUCCESS;
3234
3235 func_num = PCI_FUNC(ha->pdev->devfn);
3236
Manish Rangankar0d5b36b2011-10-07 16:55:51 -07003237 ql4_printk(KERN_INFO, ha, "%s: Get FW boot info for 0x%x func %d\n",
3238 __func__, ha->pdev->device, func_num);
Manish Rangankar2a991c22011-07-25 13:48:55 -05003239
Manish Rangankar0d5b36b2011-10-07 16:55:51 -07003240 if (is_qla40XX(ha)) {
Manish Rangankar2a991c22011-07-25 13:48:55 -05003241 if (func_num == 1) {
3242 addr = NVRAM_PORT0_BOOT_MODE;
3243 pri_addr = NVRAM_PORT0_BOOT_PRI_TGT;
3244 sec_addr = NVRAM_PORT0_BOOT_SEC_TGT;
3245 } else if (func_num == 3) {
3246 addr = NVRAM_PORT1_BOOT_MODE;
3247 pri_addr = NVRAM_PORT1_BOOT_PRI_TGT;
3248 sec_addr = NVRAM_PORT1_BOOT_SEC_TGT;
3249 } else {
3250 ret = QLA_ERROR;
3251 goto exit_boot_info;
3252 }
3253
3254 /* Check Boot Mode */
3255 val = rd_nvram_byte(ha, addr);
3256 if (!(val & 0x07)) {
3257 DEBUG2(ql4_printk(KERN_ERR, ha,
3258 "%s: Failed Boot options : 0x%x\n",
3259 __func__, val));
3260 ret = QLA_ERROR;
3261 goto exit_boot_info;
3262 }
3263
3264 /* get primary valid target index */
3265 val = rd_nvram_byte(ha, pri_addr);
3266 if (val & BIT_7)
3267 ddb_index[0] = (val & 0x7f);
Manish Rangankar2a991c22011-07-25 13:48:55 -05003268
3269 /* get secondary valid target index */
3270 val = rd_nvram_byte(ha, sec_addr);
3271 if (val & BIT_7)
3272 ddb_index[1] = (val & 0x7f);
Manish Rangankar2a991c22011-07-25 13:48:55 -05003273
3274 } else if (is_qla8022(ha)) {
3275 buf = dma_alloc_coherent(&ha->pdev->dev, size,
3276 &buf_dma, GFP_KERNEL);
3277 if (!buf) {
3278 DEBUG2(ql4_printk(KERN_ERR, ha,
3279 "%s: Unable to allocate dma buffer\n",
3280 __func__));
3281 ret = QLA_ERROR;
3282 goto exit_boot_info;
3283 }
3284
3285 if (ha->port_num == 0)
3286 offset = BOOT_PARAM_OFFSET_PORT0;
3287 else if (ha->port_num == 1)
3288 offset = BOOT_PARAM_OFFSET_PORT1;
3289 else {
3290 ret = QLA_ERROR;
3291 goto exit_boot_info_free;
3292 }
3293 addr = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_iscsi_param * 4) +
3294 offset;
3295 if (qla4xxx_get_flash(ha, buf_dma, addr,
3296 13 * sizeof(uint8_t)) != QLA_SUCCESS) {
3297 DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: Get Flash"
3298 "failed\n", ha->host_no, __func__));
3299 ret = QLA_ERROR;
3300 goto exit_boot_info_free;
3301 }
3302 /* Check Boot Mode */
3303 if (!(buf[1] & 0x07)) {
3304 DEBUG2(ql4_printk(KERN_INFO, ha,
3305 "Failed: Boot options : 0x%x\n",
3306 buf[1]));
3307 ret = QLA_ERROR;
3308 goto exit_boot_info_free;
3309 }
3310
3311 /* get primary valid target index */
3312 if (buf[2] & BIT_7)
3313 ddb_index[0] = buf[2] & 0x7f;
Manish Rangankar2a991c22011-07-25 13:48:55 -05003314
3315 /* get secondary valid target index */
3316 if (buf[11] & BIT_7)
3317 ddb_index[1] = buf[11] & 0x7f;
Manish Rangankar2a991c22011-07-25 13:48:55 -05003318 } else {
3319 ret = QLA_ERROR;
3320 goto exit_boot_info;
3321 }
3322
3323 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Primary target ID %d, Secondary"
3324 " target ID %d\n", __func__, ddb_index[0],
3325 ddb_index[1]));
3326
Mike Christie13483732011-12-01 21:38:41 -06003327 ha->pri_ddb_idx = ddb_index[0];
3328 ha->sec_ddb_idx = ddb_index[1];
3329
Manish Rangankar2a991c22011-07-25 13:48:55 -05003330exit_boot_info_free:
3331 dma_free_coherent(&ha->pdev->dev, size, buf, buf_dma);
3332exit_boot_info:
3333 return ret;
3334}
3335
Lalit Chandivade28deb452011-10-07 16:55:44 -07003336/**
3337 * qla4xxx_get_bidi_chap - Get a BIDI CHAP user and password
3338 * @ha: pointer to adapter structure
3339 * @username: CHAP username to be returned
3340 * @password: CHAP password to be returned
3341 *
3342 * If a boot entry has BIDI CHAP enabled then we need to set the BIDI CHAP
3343 * user and password in the sysfs entry in /sys/firmware/iscsi_boot#/.
3344 * So from the CHAP cache find the first BIDI CHAP entry and set it
3345 * to the boot record in sysfs.
3346 **/
3347static int qla4xxx_get_bidi_chap(struct scsi_qla_host *ha, char *username,
3348 char *password)
3349{
3350 int i, ret = -EINVAL;
3351 int max_chap_entries = 0;
3352 struct ql4_chap_table *chap_table;
3353
3354 if (is_qla8022(ha))
3355 max_chap_entries = (ha->hw.flt_chap_size / 2) /
3356 sizeof(struct ql4_chap_table);
3357 else
3358 max_chap_entries = MAX_CHAP_ENTRIES_40XX;
3359
3360 if (!ha->chap_list) {
3361 ql4_printk(KERN_ERR, ha, "Do not have CHAP table cache\n");
3362 return ret;
3363 }
3364
3365 mutex_lock(&ha->chap_sem);
3366 for (i = 0; i < max_chap_entries; i++) {
3367 chap_table = (struct ql4_chap_table *)ha->chap_list + i;
3368 if (chap_table->cookie !=
3369 __constant_cpu_to_le16(CHAP_VALID_COOKIE)) {
3370 continue;
3371 }
3372
3373 if (chap_table->flags & BIT_7) /* local */
3374 continue;
3375
3376 if (!(chap_table->flags & BIT_6)) /* Not BIDI */
3377 continue;
3378
3379 strncpy(password, chap_table->secret, QL4_CHAP_MAX_SECRET_LEN);
3380 strncpy(username, chap_table->name, QL4_CHAP_MAX_NAME_LEN);
3381 ret = 0;
3382 break;
3383 }
3384 mutex_unlock(&ha->chap_sem);
3385
3386 return ret;
3387}
3388
3389
Manish Rangankar2a991c22011-07-25 13:48:55 -05003390static int qla4xxx_get_boot_target(struct scsi_qla_host *ha,
3391 struct ql4_boot_session_info *boot_sess,
3392 uint16_t ddb_index)
3393{
3394 struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
3395 struct dev_db_entry *fw_ddb_entry;
3396 dma_addr_t fw_ddb_entry_dma;
3397 uint16_t idx;
3398 uint16_t options;
3399 int ret = QLA_SUCCESS;
3400
3401 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3402 &fw_ddb_entry_dma, GFP_KERNEL);
3403 if (!fw_ddb_entry) {
3404 DEBUG2(ql4_printk(KERN_ERR, ha,
3405 "%s: Unable to allocate dma buffer.\n",
3406 __func__));
3407 ret = QLA_ERROR;
3408 return ret;
3409 }
3410
3411 if (qla4xxx_bootdb_by_index(ha, fw_ddb_entry,
3412 fw_ddb_entry_dma, ddb_index)) {
3413 DEBUG2(ql4_printk(KERN_ERR, ha,
3414 "%s: Flash DDB read Failed\n", __func__));
3415 ret = QLA_ERROR;
3416 goto exit_boot_target;
3417 }
3418
3419 /* Update target name and IP from DDB */
3420 memcpy(boot_sess->target_name, fw_ddb_entry->iscsi_name,
3421 min(sizeof(boot_sess->target_name),
3422 sizeof(fw_ddb_entry->iscsi_name)));
3423
3424 options = le16_to_cpu(fw_ddb_entry->options);
3425 if (options & DDB_OPT_IPV6_DEVICE) {
3426 memcpy(&boot_conn->dest_ipaddr.ip_address,
3427 &fw_ddb_entry->ip_addr[0], IPv6_ADDR_LEN);
3428 } else {
3429 boot_conn->dest_ipaddr.ip_type = 0x1;
3430 memcpy(&boot_conn->dest_ipaddr.ip_address,
3431 &fw_ddb_entry->ip_addr[0], IP_ADDR_LEN);
3432 }
3433
3434 boot_conn->dest_port = le16_to_cpu(fw_ddb_entry->port);
3435
3436 /* update chap information */
3437 idx = __le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
3438
3439 if (BIT_7 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
3440
3441 DEBUG2(ql4_printk(KERN_INFO, ha, "Setting chap\n"));
3442
3443 ret = qla4xxx_get_chap(ha, (char *)&boot_conn->chap.
3444 target_chap_name,
3445 (char *)&boot_conn->chap.target_secret,
3446 idx);
3447 if (ret) {
3448 ql4_printk(KERN_ERR, ha, "Failed to set chap\n");
3449 ret = QLA_ERROR;
3450 goto exit_boot_target;
3451 }
3452
3453 boot_conn->chap.target_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
3454 boot_conn->chap.target_secret_length = QL4_CHAP_MAX_SECRET_LEN;
3455 }
3456
3457 if (BIT_4 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
3458
3459 DEBUG2(ql4_printk(KERN_INFO, ha, "Setting BIDI chap\n"));
3460
Lalit Chandivade28deb452011-10-07 16:55:44 -07003461 ret = qla4xxx_get_bidi_chap(ha,
3462 (char *)&boot_conn->chap.intr_chap_name,
3463 (char *)&boot_conn->chap.intr_secret);
3464
Manish Rangankar2a991c22011-07-25 13:48:55 -05003465 if (ret) {
3466 ql4_printk(KERN_ERR, ha, "Failed to set BIDI chap\n");
3467 ret = QLA_ERROR;
3468 goto exit_boot_target;
3469 }
3470
3471 boot_conn->chap.intr_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
3472 boot_conn->chap.intr_secret_length = QL4_CHAP_MAX_SECRET_LEN;
3473 }
3474
3475exit_boot_target:
3476 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3477 fw_ddb_entry, fw_ddb_entry_dma);
3478 return ret;
3479}
3480
3481static int qla4xxx_get_boot_info(struct scsi_qla_host *ha)
3482{
3483 uint16_t ddb_index[2];
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003484 int ret = QLA_ERROR;
3485 int rval;
Manish Rangankar2a991c22011-07-25 13:48:55 -05003486
3487 memset(ddb_index, 0, sizeof(ddb_index));
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003488 ddb_index[0] = 0xffff;
3489 ddb_index[1] = 0xffff;
Manish Rangankar2a991c22011-07-25 13:48:55 -05003490 ret = get_fw_boot_info(ha, ddb_index);
3491 if (ret != QLA_SUCCESS) {
3492 DEBUG2(ql4_printk(KERN_ERR, ha,
3493 "%s: Failed to set boot info.\n", __func__));
3494 return ret;
3495 }
3496
Mike Christie13483732011-12-01 21:38:41 -06003497 if (ql4xdisablesysfsboot)
3498 return QLA_SUCCESS;
3499
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003500 if (ddb_index[0] == 0xffff)
3501 goto sec_target;
3502
3503 rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_pri_sess),
Manish Rangankar2a991c22011-07-25 13:48:55 -05003504 ddb_index[0]);
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003505 if (rval != QLA_SUCCESS) {
Manish Rangankar2a991c22011-07-25 13:48:55 -05003506 DEBUG2(ql4_printk(KERN_ERR, ha, "%s: Failed to get "
3507 "primary target\n", __func__));
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003508 } else
3509 ret = QLA_SUCCESS;
Manish Rangankar2a991c22011-07-25 13:48:55 -05003510
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003511sec_target:
3512 if (ddb_index[1] == 0xffff)
3513 goto exit_get_boot_info;
3514
3515 rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_sec_sess),
Manish Rangankar2a991c22011-07-25 13:48:55 -05003516 ddb_index[1]);
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003517 if (rval != QLA_SUCCESS) {
Manish Rangankar2a991c22011-07-25 13:48:55 -05003518 DEBUG2(ql4_printk(KERN_ERR, ha, "%s: Failed to get "
3519 "secondary target\n", __func__));
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003520 } else
3521 ret = QLA_SUCCESS;
3522
3523exit_get_boot_info:
Manish Rangankar2a991c22011-07-25 13:48:55 -05003524 return ret;
3525}
3526
3527static int qla4xxx_setup_boot_info(struct scsi_qla_host *ha)
3528{
3529 struct iscsi_boot_kobj *boot_kobj;
3530
3531 if (qla4xxx_get_boot_info(ha) != QLA_SUCCESS)
Mike Christie13483732011-12-01 21:38:41 -06003532 return QLA_ERROR;
3533
3534 if (ql4xdisablesysfsboot) {
3535 ql4_printk(KERN_INFO, ha,
3536 "%s: syfsboot disabled - driver will trigger login"
3537 "and publish session for discovery .\n", __func__);
3538 return QLA_SUCCESS;
3539 }
3540
Manish Rangankar2a991c22011-07-25 13:48:55 -05003541
3542 ha->boot_kset = iscsi_boot_create_host_kset(ha->host->host_no);
3543 if (!ha->boot_kset)
3544 goto kset_free;
3545
3546 if (!scsi_host_get(ha->host))
3547 goto kset_free;
3548 boot_kobj = iscsi_boot_create_target(ha->boot_kset, 0, ha,
3549 qla4xxx_show_boot_tgt_pri_info,
3550 qla4xxx_tgt_get_attr_visibility,
3551 qla4xxx_boot_release);
3552 if (!boot_kobj)
3553 goto put_host;
3554
3555 if (!scsi_host_get(ha->host))
3556 goto kset_free;
3557 boot_kobj = iscsi_boot_create_target(ha->boot_kset, 1, ha,
3558 qla4xxx_show_boot_tgt_sec_info,
3559 qla4xxx_tgt_get_attr_visibility,
3560 qla4xxx_boot_release);
3561 if (!boot_kobj)
3562 goto put_host;
3563
3564 if (!scsi_host_get(ha->host))
3565 goto kset_free;
3566 boot_kobj = iscsi_boot_create_initiator(ha->boot_kset, 0, ha,
3567 qla4xxx_show_boot_ini_info,
3568 qla4xxx_ini_get_attr_visibility,
3569 qla4xxx_boot_release);
3570 if (!boot_kobj)
3571 goto put_host;
3572
3573 if (!scsi_host_get(ha->host))
3574 goto kset_free;
3575 boot_kobj = iscsi_boot_create_ethernet(ha->boot_kset, 0, ha,
3576 qla4xxx_show_boot_eth_info,
3577 qla4xxx_eth_get_attr_visibility,
3578 qla4xxx_boot_release);
3579 if (!boot_kobj)
3580 goto put_host;
3581
Mike Christie13483732011-12-01 21:38:41 -06003582 return QLA_SUCCESS;
Manish Rangankar2a991c22011-07-25 13:48:55 -05003583
3584put_host:
3585 scsi_host_put(ha->host);
3586kset_free:
3587 iscsi_boot_destroy_kset(ha->boot_kset);
3588 return -ENOMEM;
3589}
3590
Lalit Chandivade45494152011-10-07 16:55:42 -07003591
3592/**
3593 * qla4xxx_create chap_list - Create CHAP list from FLASH
3594 * @ha: pointer to adapter structure
3595 *
3596 * Read flash and make a list of CHAP entries, during login when a CHAP entry
3597 * is received, it will be checked in this list. If entry exist then the CHAP
3598 * entry index is set in the DDB. If CHAP entry does not exist in this list
3599 * then a new entry is added in FLASH in CHAP table and the index obtained is
3600 * used in the DDB.
3601 **/
3602static void qla4xxx_create_chap_list(struct scsi_qla_host *ha)
3603{
3604 int rval = 0;
3605 uint8_t *chap_flash_data = NULL;
3606 uint32_t offset;
3607 dma_addr_t chap_dma;
3608 uint32_t chap_size = 0;
3609
3610 if (is_qla40XX(ha))
3611 chap_size = MAX_CHAP_ENTRIES_40XX *
3612 sizeof(struct ql4_chap_table);
3613 else /* Single region contains CHAP info for both
3614 * ports which is divided into half for each port.
3615 */
3616 chap_size = ha->hw.flt_chap_size / 2;
3617
3618 chap_flash_data = dma_alloc_coherent(&ha->pdev->dev, chap_size,
3619 &chap_dma, GFP_KERNEL);
3620 if (!chap_flash_data) {
3621 ql4_printk(KERN_ERR, ha, "No memory for chap_flash_data\n");
3622 return;
3623 }
3624 if (is_qla40XX(ha))
3625 offset = FLASH_CHAP_OFFSET;
3626 else {
3627 offset = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_region_chap << 2);
3628 if (ha->port_num == 1)
3629 offset += chap_size;
3630 }
3631
3632 rval = qla4xxx_get_flash(ha, chap_dma, offset, chap_size);
3633 if (rval != QLA_SUCCESS)
3634 goto exit_chap_list;
3635
3636 if (ha->chap_list == NULL)
3637 ha->chap_list = vmalloc(chap_size);
3638 if (ha->chap_list == NULL) {
3639 ql4_printk(KERN_ERR, ha, "No memory for ha->chap_list\n");
3640 goto exit_chap_list;
3641 }
3642
3643 memcpy(ha->chap_list, chap_flash_data, chap_size);
3644
3645exit_chap_list:
3646 dma_free_coherent(&ha->pdev->dev, chap_size,
3647 chap_flash_data, chap_dma);
Lalit Chandivade45494152011-10-07 16:55:42 -07003648}
3649
Mike Christie13483732011-12-01 21:38:41 -06003650static void qla4xxx_get_param_ddb(struct ddb_entry *ddb_entry,
3651 struct ql4_tuple_ddb *tddb)
3652{
3653 struct scsi_qla_host *ha;
3654 struct iscsi_cls_session *cls_sess;
3655 struct iscsi_cls_conn *cls_conn;
3656 struct iscsi_session *sess;
3657 struct iscsi_conn *conn;
3658
3659 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
3660 ha = ddb_entry->ha;
3661 cls_sess = ddb_entry->sess;
3662 sess = cls_sess->dd_data;
3663 cls_conn = ddb_entry->conn;
3664 conn = cls_conn->dd_data;
3665
3666 tddb->tpgt = sess->tpgt;
3667 tddb->port = conn->persistent_port;
3668 strncpy(tddb->iscsi_name, sess->targetname, ISCSI_NAME_SIZE);
3669 strncpy(tddb->ip_addr, conn->persistent_address, DDB_IPADDR_LEN);
3670}
3671
3672static void qla4xxx_convert_param_ddb(struct dev_db_entry *fw_ddb_entry,
3673 struct ql4_tuple_ddb *tddb)
3674{
3675 uint16_t options = 0;
3676
3677 tddb->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
3678 memcpy(&tddb->iscsi_name[0], &fw_ddb_entry->iscsi_name[0],
3679 min(sizeof(tddb->iscsi_name), sizeof(fw_ddb_entry->iscsi_name)));
3680
3681 options = le16_to_cpu(fw_ddb_entry->options);
3682 if (options & DDB_OPT_IPV6_DEVICE)
3683 sprintf(tddb->ip_addr, "%pI6", fw_ddb_entry->ip_addr);
3684 else
3685 sprintf(tddb->ip_addr, "%pI4", fw_ddb_entry->ip_addr);
3686
3687 tddb->port = le16_to_cpu(fw_ddb_entry->port);
3688}
3689
3690static int qla4xxx_compare_tuple_ddb(struct scsi_qla_host *ha,
3691 struct ql4_tuple_ddb *old_tddb,
3692 struct ql4_tuple_ddb *new_tddb)
3693{
3694 if (strcmp(old_tddb->iscsi_name, new_tddb->iscsi_name))
3695 return QLA_ERROR;
3696
3697 if (strcmp(old_tddb->ip_addr, new_tddb->ip_addr))
3698 return QLA_ERROR;
3699
3700 if (old_tddb->port != new_tddb->port)
3701 return QLA_ERROR;
3702
3703 DEBUG2(ql4_printk(KERN_INFO, ha,
3704 "Match Found, fw[%d,%d,%s,%s], [%d,%d,%s,%s]",
3705 old_tddb->port, old_tddb->tpgt, old_tddb->ip_addr,
3706 old_tddb->iscsi_name, new_tddb->port, new_tddb->tpgt,
3707 new_tddb->ip_addr, new_tddb->iscsi_name));
3708
3709 return QLA_SUCCESS;
3710}
3711
3712static int qla4xxx_is_session_exists(struct scsi_qla_host *ha,
3713 struct dev_db_entry *fw_ddb_entry)
3714{
3715 struct ddb_entry *ddb_entry;
3716 struct ql4_tuple_ddb *fw_tddb = NULL;
3717 struct ql4_tuple_ddb *tmp_tddb = NULL;
3718 int idx;
3719 int ret = QLA_ERROR;
3720
3721 fw_tddb = vzalloc(sizeof(*fw_tddb));
3722 if (!fw_tddb) {
3723 DEBUG2(ql4_printk(KERN_WARNING, ha,
3724 "Memory Allocation failed.\n"));
3725 ret = QLA_SUCCESS;
3726 goto exit_check;
3727 }
3728
3729 tmp_tddb = vzalloc(sizeof(*tmp_tddb));
3730 if (!tmp_tddb) {
3731 DEBUG2(ql4_printk(KERN_WARNING, ha,
3732 "Memory Allocation failed.\n"));
3733 ret = QLA_SUCCESS;
3734 goto exit_check;
3735 }
3736
3737 qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb);
3738
3739 for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
3740 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
3741 if (ddb_entry == NULL)
3742 continue;
3743
3744 qla4xxx_get_param_ddb(ddb_entry, tmp_tddb);
3745 if (!qla4xxx_compare_tuple_ddb(ha, fw_tddb, tmp_tddb)) {
3746 ret = QLA_SUCCESS; /* found */
3747 goto exit_check;
3748 }
3749 }
3750
3751exit_check:
3752 if (fw_tddb)
3753 vfree(fw_tddb);
3754 if (tmp_tddb)
3755 vfree(tmp_tddb);
3756 return ret;
3757}
3758
3759static int qla4xxx_is_flash_ddb_exists(struct scsi_qla_host *ha,
3760 struct list_head *list_nt,
3761 struct dev_db_entry *fw_ddb_entry)
3762{
3763 struct qla_ddb_index *nt_ddb_idx, *nt_ddb_idx_tmp;
3764 struct ql4_tuple_ddb *fw_tddb = NULL;
3765 struct ql4_tuple_ddb *tmp_tddb = NULL;
3766 int ret = QLA_ERROR;
3767
3768 fw_tddb = vzalloc(sizeof(*fw_tddb));
3769 if (!fw_tddb) {
3770 DEBUG2(ql4_printk(KERN_WARNING, ha,
3771 "Memory Allocation failed.\n"));
3772 ret = QLA_SUCCESS;
3773 goto exit_check;
3774 }
3775
3776 tmp_tddb = vzalloc(sizeof(*tmp_tddb));
3777 if (!tmp_tddb) {
3778 DEBUG2(ql4_printk(KERN_WARNING, ha,
3779 "Memory Allocation failed.\n"));
3780 ret = QLA_SUCCESS;
3781 goto exit_check;
3782 }
3783
3784 qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb);
3785
3786 list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
3787 qla4xxx_convert_param_ddb(&nt_ddb_idx->fw_ddb, tmp_tddb);
3788 if (!qla4xxx_compare_tuple_ddb(ha, fw_tddb, tmp_tddb)) {
3789 ret = QLA_SUCCESS; /* found */
3790 goto exit_check;
3791 }
3792 }
3793
3794exit_check:
3795 if (fw_tddb)
3796 vfree(fw_tddb);
3797 if (tmp_tddb)
3798 vfree(tmp_tddb);
3799 return ret;
3800}
3801
3802static void qla4xxx_free_nt_list(struct list_head *list_nt)
3803{
3804 struct qla_ddb_index *nt_ddb_idx, *nt_ddb_idx_tmp;
3805
3806 /* Free up the normaltargets list */
3807 list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
3808 list_del_init(&nt_ddb_idx->list);
3809 vfree(nt_ddb_idx);
3810 }
3811
3812}
3813
3814static struct iscsi_endpoint *qla4xxx_get_ep_fwdb(struct scsi_qla_host *ha,
3815 struct dev_db_entry *fw_ddb_entry)
3816{
3817 struct iscsi_endpoint *ep;
3818 struct sockaddr_in *addr;
3819 struct sockaddr_in6 *addr6;
3820 struct sockaddr *dst_addr;
3821 char *ip;
3822
3823 /* TODO: need to destroy on unload iscsi_endpoint*/
3824 dst_addr = vmalloc(sizeof(*dst_addr));
3825 if (!dst_addr)
3826 return NULL;
3827
3828 if (fw_ddb_entry->options & DDB_OPT_IPV6_DEVICE) {
3829 dst_addr->sa_family = AF_INET6;
3830 addr6 = (struct sockaddr_in6 *)dst_addr;
3831 ip = (char *)&addr6->sin6_addr;
3832 memcpy(ip, fw_ddb_entry->ip_addr, IPv6_ADDR_LEN);
3833 addr6->sin6_port = htons(le16_to_cpu(fw_ddb_entry->port));
3834
3835 } else {
3836 dst_addr->sa_family = AF_INET;
3837 addr = (struct sockaddr_in *)dst_addr;
3838 ip = (char *)&addr->sin_addr;
3839 memcpy(ip, fw_ddb_entry->ip_addr, IP_ADDR_LEN);
3840 addr->sin_port = htons(le16_to_cpu(fw_ddb_entry->port));
3841 }
3842
3843 ep = qla4xxx_ep_connect(ha->host, dst_addr, 0);
3844 vfree(dst_addr);
3845 return ep;
3846}
3847
3848static int qla4xxx_verify_boot_idx(struct scsi_qla_host *ha, uint16_t idx)
3849{
3850 if (ql4xdisablesysfsboot)
3851 return QLA_SUCCESS;
3852 if (idx == ha->pri_ddb_idx || idx == ha->sec_ddb_idx)
3853 return QLA_ERROR;
3854 return QLA_SUCCESS;
3855}
3856
3857static void qla4xxx_setup_flash_ddb_entry(struct scsi_qla_host *ha,
3858 struct ddb_entry *ddb_entry)
3859{
3860 ddb_entry->ddb_type = FLASH_DDB;
3861 ddb_entry->fw_ddb_index = INVALID_ENTRY;
3862 ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
3863 ddb_entry->ha = ha;
3864 ddb_entry->unblock_sess = qla4xxx_unblock_flash_ddb;
3865 ddb_entry->ddb_change = qla4xxx_flash_ddb_change;
3866
3867 atomic_set(&ddb_entry->retry_relogin_timer, INVALID_ENTRY);
3868 atomic_set(&ddb_entry->relogin_timer, 0);
3869 atomic_set(&ddb_entry->relogin_retry_count, 0);
3870
3871 ddb_entry->default_relogin_timeout =
3872 le16_to_cpu(ddb_entry->fw_ddb_entry.def_timeout);
3873 ddb_entry->default_time2wait =
3874 le16_to_cpu(ddb_entry->fw_ddb_entry.iscsi_def_time2wait);
3875}
3876
3877static void qla4xxx_wait_for_ip_configuration(struct scsi_qla_host *ha)
3878{
3879 uint32_t idx = 0;
3880 uint32_t ip_idx[IP_ADDR_COUNT] = {0, 1, 2, 3}; /* 4 IP interfaces */
3881 uint32_t sts[MBOX_REG_COUNT];
3882 uint32_t ip_state;
3883 unsigned long wtime;
3884 int ret;
3885
3886 wtime = jiffies + (HZ * IP_CONFIG_TOV);
3887 do {
3888 for (idx = 0; idx < IP_ADDR_COUNT; idx++) {
3889 if (ip_idx[idx] == -1)
3890 continue;
3891
3892 ret = qla4xxx_get_ip_state(ha, 0, ip_idx[idx], sts);
3893
3894 if (ret == QLA_ERROR) {
3895 ip_idx[idx] = -1;
3896 continue;
3897 }
3898
3899 ip_state = (sts[1] & IP_STATE_MASK) >> IP_STATE_SHIFT;
3900
3901 DEBUG2(ql4_printk(KERN_INFO, ha,
3902 "Waiting for IP state for idx = %d, state = 0x%x\n",
3903 ip_idx[idx], ip_state));
3904 if (ip_state == IP_ADDRSTATE_UNCONFIGURED ||
3905 ip_state == IP_ADDRSTATE_INVALID ||
3906 ip_state == IP_ADDRSTATE_PREFERRED ||
3907 ip_state == IP_ADDRSTATE_DEPRICATED ||
3908 ip_state == IP_ADDRSTATE_DISABLING)
3909 ip_idx[idx] = -1;
3910
3911 }
3912
3913 /* Break if all IP states checked */
3914 if ((ip_idx[0] == -1) &&
3915 (ip_idx[1] == -1) &&
3916 (ip_idx[2] == -1) &&
3917 (ip_idx[3] == -1))
3918 break;
3919 schedule_timeout_uninterruptible(HZ);
3920 } while (time_after(wtime, jiffies));
3921}
3922
3923void qla4xxx_build_ddb_list(struct scsi_qla_host *ha, int is_reset)
3924{
3925 int max_ddbs;
3926 int ret;
3927 uint32_t idx = 0, next_idx = 0;
3928 uint32_t state = 0, conn_err = 0;
3929 uint16_t conn_id;
3930 struct dev_db_entry *fw_ddb_entry;
3931 struct ddb_entry *ddb_entry = NULL;
3932 dma_addr_t fw_ddb_dma;
3933 struct iscsi_cls_session *cls_sess;
3934 struct iscsi_session *sess;
3935 struct iscsi_cls_conn *cls_conn;
3936 struct iscsi_endpoint *ep;
3937 uint16_t cmds_max = 32, tmo = 0;
3938 uint32_t initial_cmdsn = 0;
3939 struct list_head list_st, list_nt; /* List of sendtargets */
3940 struct qla_ddb_index *st_ddb_idx, *st_ddb_idx_tmp;
3941 int fw_idx_size;
3942 unsigned long wtime;
3943 struct qla_ddb_index *nt_ddb_idx;
3944
3945 if (!test_bit(AF_LINK_UP, &ha->flags)) {
3946 set_bit(AF_BUILD_DDB_LIST, &ha->flags);
3947 ha->is_reset = is_reset;
3948 return;
3949 }
3950 max_ddbs = is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
3951 MAX_DEV_DB_ENTRIES;
3952
3953 fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
3954 &fw_ddb_dma);
3955 if (fw_ddb_entry == NULL) {
3956 DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
3957 goto exit_ddb_list;
3958 }
3959
3960 INIT_LIST_HEAD(&list_st);
3961 INIT_LIST_HEAD(&list_nt);
3962 fw_idx_size = sizeof(struct qla_ddb_index);
3963
3964 for (idx = 0; idx < max_ddbs; idx = next_idx) {
3965 ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry,
3966 fw_ddb_dma, NULL,
3967 &next_idx, &state, &conn_err,
3968 NULL, &conn_id);
3969 if (ret == QLA_ERROR)
3970 break;
3971
3972 if (qla4xxx_verify_boot_idx(ha, idx) != QLA_SUCCESS)
3973 goto continue_next_st;
3974
3975 /* Check if ST, add to the list_st */
3976 if (strlen((char *) fw_ddb_entry->iscsi_name) != 0)
3977 goto continue_next_st;
3978
3979 st_ddb_idx = vzalloc(fw_idx_size);
3980 if (!st_ddb_idx)
3981 break;
3982
3983 st_ddb_idx->fw_ddb_idx = idx;
3984
3985 list_add_tail(&st_ddb_idx->list, &list_st);
3986continue_next_st:
3987 if (next_idx == 0)
3988 break;
3989 }
3990
3991 /* Before issuing conn open mbox, ensure all IPs states are configured
3992 * Note, conn open fails if IPs are not configured
3993 */
3994 qla4xxx_wait_for_ip_configuration(ha);
3995
3996 /* Go thru the STs and fire the sendtargets by issuing conn open mbx */
3997 list_for_each_entry_safe(st_ddb_idx, st_ddb_idx_tmp, &list_st, list) {
3998 qla4xxx_conn_open(ha, st_ddb_idx->fw_ddb_idx);
3999 }
4000
4001 /* Wait to ensure all sendtargets are done for min 12 sec wait */
4002 tmo = ((ha->def_timeout < LOGIN_TOV) ? LOGIN_TOV : ha->def_timeout);
4003 DEBUG2(ql4_printk(KERN_INFO, ha,
4004 "Default time to wait for build ddb %d\n", tmo));
4005
4006 wtime = jiffies + (HZ * tmo);
4007 do {
4008 list_for_each_entry_safe(st_ddb_idx, st_ddb_idx_tmp, &list_st,
4009 list) {
4010 ret = qla4xxx_get_fwddb_entry(ha,
4011 st_ddb_idx->fw_ddb_idx,
4012 NULL, 0, NULL, &next_idx,
4013 &state, &conn_err, NULL,
4014 NULL);
4015 if (ret == QLA_ERROR)
4016 continue;
4017
4018 if (state == DDB_DS_NO_CONNECTION_ACTIVE ||
4019 state == DDB_DS_SESSION_FAILED) {
4020 list_del_init(&st_ddb_idx->list);
4021 vfree(st_ddb_idx);
4022 }
4023 }
4024 schedule_timeout_uninterruptible(HZ / 10);
4025 } while (time_after(wtime, jiffies));
4026
4027 /* Free up the sendtargets list */
4028 list_for_each_entry_safe(st_ddb_idx, st_ddb_idx_tmp, &list_st, list) {
4029 list_del_init(&st_ddb_idx->list);
4030 vfree(st_ddb_idx);
4031 }
4032
4033 for (idx = 0; idx < max_ddbs; idx = next_idx) {
4034 ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry,
4035 fw_ddb_dma, NULL,
4036 &next_idx, &state, &conn_err,
4037 NULL, &conn_id);
4038 if (ret == QLA_ERROR)
4039 break;
4040
4041 if (qla4xxx_verify_boot_idx(ha, idx) != QLA_SUCCESS)
4042 goto continue_next_nt;
4043
4044 /* Check if NT, then add to list it */
4045 if (strlen((char *) fw_ddb_entry->iscsi_name) == 0)
4046 goto continue_next_nt;
4047
4048 if (state == DDB_DS_NO_CONNECTION_ACTIVE ||
4049 state == DDB_DS_SESSION_FAILED) {
4050 DEBUG2(ql4_printk(KERN_INFO, ha,
4051 "Adding DDB to session = 0x%x\n",
4052 idx));
4053 if (is_reset == INIT_ADAPTER) {
4054 nt_ddb_idx = vmalloc(fw_idx_size);
4055 if (!nt_ddb_idx)
4056 break;
4057
4058 nt_ddb_idx->fw_ddb_idx = idx;
4059
4060 memcpy(&nt_ddb_idx->fw_ddb, fw_ddb_entry,
4061 sizeof(struct dev_db_entry));
4062
4063 if (qla4xxx_is_flash_ddb_exists(ha, &list_nt,
4064 fw_ddb_entry) == QLA_SUCCESS) {
4065 vfree(nt_ddb_idx);
4066 goto continue_next_nt;
4067 }
4068 list_add_tail(&nt_ddb_idx->list, &list_nt);
4069 } else if (is_reset == RESET_ADAPTER) {
4070 if (qla4xxx_is_session_exists(ha,
4071 fw_ddb_entry) == QLA_SUCCESS)
4072 goto continue_next_nt;
4073 }
4074
4075 /* Create session object, with INVALID_ENTRY,
4076 * the targer_id would get set when we issue the login
4077 */
4078 cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport,
4079 ha->host, cmds_max,
4080 sizeof(struct ddb_entry),
4081 sizeof(struct ql4_task_data),
4082 initial_cmdsn, INVALID_ENTRY);
4083 if (!cls_sess)
4084 goto exit_ddb_list;
4085
4086 /*
4087 * iscsi_session_setup increments the driver reference
4088 * count which wouldn't let the driver to be unloaded.
4089 * so calling module_put function to decrement the
4090 * reference count.
4091 **/
4092 module_put(qla4xxx_iscsi_transport.owner);
4093 sess = cls_sess->dd_data;
4094 ddb_entry = sess->dd_data;
4095 ddb_entry->sess = cls_sess;
4096
4097 cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
4098 memcpy(&ddb_entry->fw_ddb_entry, fw_ddb_entry,
4099 sizeof(struct dev_db_entry));
4100
4101 qla4xxx_setup_flash_ddb_entry(ha, ddb_entry);
4102
4103 cls_conn = iscsi_conn_setup(cls_sess,
4104 sizeof(struct qla_conn),
4105 conn_id);
4106 if (!cls_conn)
4107 goto exit_ddb_list;
4108
4109 ddb_entry->conn = cls_conn;
4110
4111 /* Setup ep, for displaying attributes in sysfs */
4112 ep = qla4xxx_get_ep_fwdb(ha, fw_ddb_entry);
4113 if (ep) {
4114 ep->conn = cls_conn;
4115 cls_conn->ep = ep;
4116 } else {
4117 DEBUG2(ql4_printk(KERN_ERR, ha,
4118 "Unable to get ep\n"));
4119 }
4120
4121 /* Update sess/conn params */
4122 qla4xxx_copy_fwddb_param(ha, fw_ddb_entry, cls_sess,
4123 cls_conn);
4124
4125 if (is_reset == RESET_ADAPTER) {
4126 iscsi_block_session(cls_sess);
4127 /* Use the relogin path to discover new devices
4128 * by short-circuting the logic of setting
4129 * timer to relogin - instead set the flags
4130 * to initiate login right away.
4131 */
4132 set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
4133 set_bit(DF_RELOGIN, &ddb_entry->flags);
4134 }
4135 }
4136continue_next_nt:
4137 if (next_idx == 0)
4138 break;
4139 }
4140exit_ddb_list:
4141 qla4xxx_free_nt_list(&list_nt);
4142 if (fw_ddb_entry)
4143 dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
4144
4145 qla4xxx_free_ddb_index(ha);
4146}
4147
4148
David Somayajuluafaf5a22006-09-19 10:28:00 -07004149/**
4150 * qla4xxx_probe_adapter - callback function to probe HBA
4151 * @pdev: pointer to pci_dev structure
4152 * @pci_device_id: pointer to pci_device entry
4153 *
4154 * This routine will probe for Qlogic 4xxx iSCSI host adapters.
4155 * It returns zero if successful. It also initializes all data necessary for
4156 * the driver.
4157 **/
4158static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
4159 const struct pci_device_id *ent)
4160{
4161 int ret = -ENODEV, status;
4162 struct Scsi_Host *host;
4163 struct scsi_qla_host *ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004164 uint8_t init_retry_count = 0;
4165 char buf[34];
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304166 struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
Prasanna Mumbaif9880e72011-03-21 03:34:26 -07004167 uint32_t dev_state;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004168
4169 if (pci_enable_device(pdev))
4170 return -1;
4171
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004172 host = iscsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha), 0);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004173 if (host == NULL) {
4174 printk(KERN_WARNING
4175 "qla4xxx: Couldn't allocate host from scsi layer!\n");
4176 goto probe_disable_device;
4177 }
4178
4179 /* Clear our data area */
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004180 ha = to_qla_host(host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004181 memset(ha, 0, sizeof(*ha));
4182
4183 /* Save the information from PCI BIOS. */
4184 ha->pdev = pdev;
4185 ha->host = host;
4186 ha->host_no = host->host_no;
4187
Lalit Chandivade2232be02010-07-30 14:38:47 +05304188 pci_enable_pcie_error_reporting(pdev);
4189
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304190 /* Setup Runtime configurable options */
4191 if (is_qla8022(ha)) {
4192 ha->isp_ops = &qla4_8xxx_isp_ops;
4193 rwlock_init(&ha->hw_lock);
4194 ha->qdr_sn_window = -1;
4195 ha->ddr_mn_window = -1;
4196 ha->curr_window = 255;
4197 ha->func_num = PCI_FUNC(ha->pdev->devfn);
4198 nx_legacy_intr = &legacy_intr[ha->func_num];
4199 ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
4200 ha->nx_legacy_intr.tgt_status_reg =
4201 nx_legacy_intr->tgt_status_reg;
4202 ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
4203 ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
4204 } else {
4205 ha->isp_ops = &qla4xxx_isp_ops;
4206 }
4207
Lalit Chandivade2232be02010-07-30 14:38:47 +05304208 /* Set EEH reset type to fundamental if required by hba */
4209 if (is_qla8022(ha))
4210 pdev->needs_freset = 1;
4211
David Somayajuluafaf5a22006-09-19 10:28:00 -07004212 /* Configure PCI I/O space. */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304213 ret = ha->isp_ops->iospace_config(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004214 if (ret)
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304215 goto probe_failed_ioconfig;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004216
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304217 ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
David Somayajuluafaf5a22006-09-19 10:28:00 -07004218 pdev->device, pdev->irq, ha->reg);
4219
4220 qla4xxx_config_dma_addressing(ha);
4221
4222 /* Initialize lists and spinlocks. */
David Somayajuluafaf5a22006-09-19 10:28:00 -07004223 INIT_LIST_HEAD(&ha->free_srb_q);
4224
4225 mutex_init(&ha->mbox_sem);
Lalit Chandivade45494152011-10-07 16:55:42 -07004226 mutex_init(&ha->chap_sem);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304227 init_completion(&ha->mbx_intr_comp);
Vikas Chaudhary95d31262011-08-12 02:51:29 -07004228 init_completion(&ha->disable_acb_comp);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004229
4230 spin_lock_init(&ha->hardware_lock);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004231
4232 /* Allocate dma buffers */
4233 if (qla4xxx_mem_alloc(ha)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304234 ql4_printk(KERN_WARNING, ha,
4235 "[ERROR] Failed to allocate memory for adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07004236
4237 ret = -ENOMEM;
4238 goto probe_failed;
4239 }
4240
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004241 host->cmd_per_lun = 3;
4242 host->max_channel = 0;
4243 host->max_lun = MAX_LUNS - 1;
4244 host->max_id = MAX_TARGETS;
4245 host->max_cmd_len = IOCB_MAX_CDB_LEN;
4246 host->can_queue = MAX_SRBS ;
4247 host->transportt = qla4xxx_scsi_transport;
4248
4249 ret = scsi_init_shared_tag_map(host, MAX_SRBS);
4250 if (ret) {
4251 ql4_printk(KERN_WARNING, ha,
4252 "%s: scsi_init_shared_tag_map failed\n", __func__);
4253 goto probe_failed;
4254 }
4255
4256 pci_set_drvdata(pdev, ha);
4257
4258 ret = scsi_add_host(host, &pdev->dev);
4259 if (ret)
4260 goto probe_failed;
4261
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304262 if (is_qla8022(ha))
4263 (void) qla4_8xxx_get_flash_info(ha);
4264
David Somayajuluafaf5a22006-09-19 10:28:00 -07004265 /*
4266 * Initialize the Host adapter request/response queues and
4267 * firmware
4268 * NOTE: interrupts enabled upon successful completion
4269 */
Mike Christie13483732011-12-01 21:38:41 -06004270 status = qla4xxx_initialize_adapter(ha, INIT_ADAPTER);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304271 while ((!test_bit(AF_ONLINE, &ha->flags)) &&
4272 init_retry_count++ < MAX_INIT_RETRIES) {
Prasanna Mumbaif9880e72011-03-21 03:34:26 -07004273
4274 if (is_qla8022(ha)) {
4275 qla4_8xxx_idc_lock(ha);
4276 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
4277 qla4_8xxx_idc_unlock(ha);
4278 if (dev_state == QLA82XX_DEV_FAILED) {
4279 ql4_printk(KERN_WARNING, ha, "%s: don't retry "
4280 "initialize adapter. H/W is in failed state\n",
4281 __func__);
4282 break;
4283 }
4284 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07004285 DEBUG2(printk("scsi: %s: retrying adapter initialization "
4286 "(%d)\n", __func__, init_retry_count));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304287
4288 if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
4289 continue;
4290
Mike Christie13483732011-12-01 21:38:41 -06004291 status = qla4xxx_initialize_adapter(ha, INIT_ADAPTER);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004292 }
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304293
4294 if (!test_bit(AF_ONLINE, &ha->flags)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304295 ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07004296
Lalit Chandivadefe998522010-12-02 22:12:36 -08004297 if (is_qla8022(ha) && ql4xdontresethba) {
4298 /* Put the device in failed state. */
4299 DEBUG2(printk(KERN_ERR "HW STATE: FAILED\n"));
4300 qla4_8xxx_idc_lock(ha);
4301 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
4302 QLA82XX_DEV_FAILED);
4303 qla4_8xxx_idc_unlock(ha);
4304 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07004305 ret = -ENODEV;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004306 goto remove_host;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004307 }
4308
David Somayajuluafaf5a22006-09-19 10:28:00 -07004309 /* Startup the kernel thread for this host adapter. */
4310 DEBUG2(printk("scsi: %s: Starting kernel thread for "
4311 "qla4xxx_dpc\n", __func__));
4312 sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
4313 ha->dpc_thread = create_singlethread_workqueue(buf);
4314 if (!ha->dpc_thread) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304315 ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07004316 ret = -ENODEV;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004317 goto remove_host;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004318 }
David Howellsc4028952006-11-22 14:57:56 +00004319 INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004320
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004321 sprintf(buf, "qla4xxx_%lu_task", ha->host_no);
4322 ha->task_wq = alloc_workqueue(buf, WQ_MEM_RECLAIM, 1);
4323 if (!ha->task_wq) {
4324 ql4_printk(KERN_WARNING, ha, "Unable to start task thread!\n");
4325 ret = -ENODEV;
4326 goto remove_host;
4327 }
4328
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304329 /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
4330 * (which is called indirectly by qla4xxx_initialize_adapter),
4331 * so that irqs will be registered after crbinit but before
4332 * mbx_intr_enable.
4333 */
4334 if (!is_qla8022(ha)) {
4335 ret = qla4xxx_request_irqs(ha);
4336 if (ret) {
4337 ql4_printk(KERN_WARNING, ha, "Failed to reserve "
4338 "interrupt %d already in use.\n", pdev->irq);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004339 goto remove_host;
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304340 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07004341 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07004342
Lalit Chandivade2232be02010-07-30 14:38:47 +05304343 pci_save_state(ha->pdev);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304344 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004345
4346 /* Start timer thread. */
4347 qla4xxx_start_timer(ha, qla4xxx_timer, 1);
4348
4349 set_bit(AF_INIT_DONE, &ha->flags);
4350
David Somayajuluafaf5a22006-09-19 10:28:00 -07004351 printk(KERN_INFO
4352 " QLogic iSCSI HBA Driver version: %s\n"
4353 " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
4354 qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
4355 ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
4356 ha->patch_number, ha->build_number);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05004357
Manish Rangankar2a991c22011-07-25 13:48:55 -05004358 if (qla4xxx_setup_boot_info(ha))
4359 ql4_printk(KERN_ERR, ha, "%s:ISCSI boot info setup failed\n",
4360 __func__);
4361
Mike Christie13483732011-12-01 21:38:41 -06004362 /* Perform the build ddb list and login to each */
4363 qla4xxx_build_ddb_list(ha, INIT_ADAPTER);
4364 iscsi_host_for_each_session(ha->host, qla4xxx_login_flash_ddb);
4365
4366 qla4xxx_create_chap_list(ha);
4367
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05004368 qla4xxx_create_ifaces(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004369 return 0;
4370
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004371remove_host:
4372 scsi_remove_host(ha->host);
4373
David Somayajuluafaf5a22006-09-19 10:28:00 -07004374probe_failed:
4375 qla4xxx_free_adapter(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304376
4377probe_failed_ioconfig:
Lalit Chandivade2232be02010-07-30 14:38:47 +05304378 pci_disable_pcie_error_reporting(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004379 scsi_host_put(ha->host);
4380
4381probe_disable_device:
4382 pci_disable_device(pdev);
4383
4384 return ret;
4385}
4386
4387/**
Karen Higgins7eece5a2011-03-21 03:34:29 -07004388 * qla4xxx_prevent_other_port_reinit - prevent other port from re-initialize
4389 * @ha: pointer to adapter structure
4390 *
4391 * Mark the other ISP-4xxx port to indicate that the driver is being removed,
4392 * so that the other port will not re-initialize while in the process of
4393 * removing the ha due to driver unload or hba hotplug.
4394 **/
4395static void qla4xxx_prevent_other_port_reinit(struct scsi_qla_host *ha)
4396{
4397 struct scsi_qla_host *other_ha = NULL;
4398 struct pci_dev *other_pdev = NULL;
4399 int fn = ISP4XXX_PCI_FN_2;
4400
4401 /*iscsi function numbers for ISP4xxx is 1 and 3*/
4402 if (PCI_FUNC(ha->pdev->devfn) & BIT_1)
4403 fn = ISP4XXX_PCI_FN_1;
4404
4405 other_pdev =
4406 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
4407 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
4408 fn));
4409
4410 /* Get other_ha if other_pdev is valid and state is enable*/
4411 if (other_pdev) {
4412 if (atomic_read(&other_pdev->enable_cnt)) {
4413 other_ha = pci_get_drvdata(other_pdev);
4414 if (other_ha) {
4415 set_bit(AF_HA_REMOVAL, &other_ha->flags);
4416 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: "
4417 "Prevent %s reinit\n", __func__,
4418 dev_name(&other_ha->pdev->dev)));
4419 }
4420 }
4421 pci_dev_put(other_pdev);
4422 }
4423}
4424
Mike Christie13483732011-12-01 21:38:41 -06004425static void qla4xxx_destroy_fw_ddb_session(struct scsi_qla_host *ha)
4426{
4427 struct ddb_entry *ddb_entry;
4428 int options;
4429 int idx;
4430
4431 for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
4432
4433 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
4434 if ((ddb_entry != NULL) &&
4435 (ddb_entry->ddb_type == FLASH_DDB)) {
4436
4437 options = LOGOUT_OPTION_CLOSE_SESSION;
4438 if (qla4xxx_session_logout_ddb(ha, ddb_entry, options)
4439 == QLA_ERROR)
4440 ql4_printk(KERN_ERR, ha, "%s: Logout failed\n",
4441 __func__);
4442
4443 qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
4444 /*
4445 * we have decremented the reference count of the driver
4446 * when we setup the session to have the driver unload
4447 * to be seamless without actually destroying the
4448 * session
4449 **/
4450 try_module_get(qla4xxx_iscsi_transport.owner);
4451 iscsi_destroy_endpoint(ddb_entry->conn->ep);
4452 qla4xxx_free_ddb(ha, ddb_entry);
4453 iscsi_session_teardown(ddb_entry->sess);
4454 }
4455 }
4456}
Karen Higgins7eece5a2011-03-21 03:34:29 -07004457/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07004458 * qla4xxx_remove_adapter - calback function to remove adapter.
4459 * @pci_dev: PCI device pointer
4460 **/
4461static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
4462{
4463 struct scsi_qla_host *ha;
4464
4465 ha = pci_get_drvdata(pdev);
4466
Karen Higgins7eece5a2011-03-21 03:34:29 -07004467 if (!is_qla8022(ha))
4468 qla4xxx_prevent_other_port_reinit(ha);
David C Somayajulubee4fe82007-05-23 18:03:32 -07004469
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05004470 /* destroy iface from sysfs */
4471 qla4xxx_destroy_ifaces(ha);
4472
Mike Christie13483732011-12-01 21:38:41 -06004473 if ((!ql4xdisablesysfsboot) && ha->boot_kset)
Manish Rangankar2a991c22011-07-25 13:48:55 -05004474 iscsi_boot_destroy_kset(ha->boot_kset);
4475
Mike Christie13483732011-12-01 21:38:41 -06004476 qla4xxx_destroy_fw_ddb_session(ha);
4477
David Somayajuluafaf5a22006-09-19 10:28:00 -07004478 scsi_remove_host(ha->host);
4479
4480 qla4xxx_free_adapter(ha);
4481
4482 scsi_host_put(ha->host);
4483
Lalit Chandivade2232be02010-07-30 14:38:47 +05304484 pci_disable_pcie_error_reporting(pdev);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304485 pci_disable_device(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004486 pci_set_drvdata(pdev, NULL);
4487}
4488
4489/**
4490 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
4491 * @ha: HA context
4492 *
4493 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
4494 * supported addressing method.
4495 */
Adrian Bunk47975472007-04-26 00:35:16 -07004496static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07004497{
4498 int retval;
4499
4500 /* Update our PCI device dma_mask for full 64 bit mask */
Yang Hongyang6a355282009-04-06 19:01:13 -07004501 if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
4502 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07004503 dev_dbg(&ha->pdev->dev,
4504 "Failed to set 64 bit PCI consistent mask; "
4505 "using 32 bit.\n");
4506 retval = pci_set_consistent_dma_mask(ha->pdev,
Yang Hongyang284901a2009-04-06 19:01:15 -07004507 DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07004508 }
4509 } else
Yang Hongyang284901a2009-04-06 19:01:15 -07004510 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07004511}
4512
4513static int qla4xxx_slave_alloc(struct scsi_device *sdev)
4514{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004515 struct iscsi_cls_session *cls_sess;
4516 struct iscsi_session *sess;
4517 struct ddb_entry *ddb;
Vikas Chaudhary8bb40332011-03-21 03:34:31 -07004518 int queue_depth = QL4_DEF_QDEPTH;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004519
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004520 cls_sess = starget_to_session(sdev->sdev_target);
4521 sess = cls_sess->dd_data;
4522 ddb = sess->dd_data;
4523
David Somayajuluafaf5a22006-09-19 10:28:00 -07004524 sdev->hostdata = ddb;
4525 sdev->tagged_supported = 1;
Vikas Chaudhary8bb40332011-03-21 03:34:31 -07004526
4527 if (ql4xmaxqdepth != 0 && ql4xmaxqdepth <= 0xffffU)
4528 queue_depth = ql4xmaxqdepth;
4529
4530 scsi_activate_tcq(sdev, queue_depth);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004531 return 0;
4532}
4533
4534static int qla4xxx_slave_configure(struct scsi_device *sdev)
4535{
4536 sdev->tagged_supported = 1;
4537 return 0;
4538}
4539
4540static void qla4xxx_slave_destroy(struct scsi_device *sdev)
4541{
4542 scsi_deactivate_tcq(sdev, 1);
4543}
4544
4545/**
4546 * qla4xxx_del_from_active_array - returns an active srb
4547 * @ha: Pointer to host adapter structure.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02004548 * @index: index into the active_array
David Somayajuluafaf5a22006-09-19 10:28:00 -07004549 *
4550 * This routine removes and returns the srb at the specified index
4551 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304552struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
4553 uint32_t index)
David Somayajuluafaf5a22006-09-19 10:28:00 -07004554{
4555 struct srb *srb = NULL;
Vikas Chaudhary53698872010-04-28 11:41:59 +05304556 struct scsi_cmnd *cmd = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004557
Vikas Chaudhary53698872010-04-28 11:41:59 +05304558 cmd = scsi_host_find_tag(ha->host, index);
4559 if (!cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07004560 return srb;
4561
Vikas Chaudhary53698872010-04-28 11:41:59 +05304562 srb = (struct srb *)CMD_SP(cmd);
4563 if (!srb)
David Somayajuluafaf5a22006-09-19 10:28:00 -07004564 return srb;
4565
4566 /* update counters */
4567 if (srb->flags & SRB_DMA_VALID) {
4568 ha->req_q_count += srb->iocb_cnt;
4569 ha->iocb_cnt -= srb->iocb_cnt;
4570 if (srb->cmd)
Vikas Chaudhary53698872010-04-28 11:41:59 +05304571 srb->cmd->host_scribble =
4572 (unsigned char *)(unsigned long) MAX_SRBS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004573 }
4574 return srb;
4575}
4576
4577/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07004578 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304579 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07004580 * @cmd: Scsi Command to wait on.
4581 *
4582 * This routine waits for the command to be returned by the Firmware
4583 * for some max time.
4584 **/
4585static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
4586 struct scsi_cmnd *cmd)
4587{
4588 int done = 0;
4589 struct srb *rp;
4590 uint32_t max_wait_time = EH_WAIT_CMD_TOV;
Lalit Chandivade2232be02010-07-30 14:38:47 +05304591 int ret = SUCCESS;
4592
4593 /* Dont wait on command if PCI error is being handled
4594 * by PCI AER driver
4595 */
4596 if (unlikely(pci_channel_offline(ha->pdev)) ||
4597 (test_bit(AF_EEH_BUSY, &ha->flags))) {
4598 ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
4599 ha->host_no, __func__);
4600 return ret;
4601 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07004602
4603 do {
4604 /* Checking to see if its returned to OS */
Vikas Chaudhary53698872010-04-28 11:41:59 +05304605 rp = (struct srb *) CMD_SP(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004606 if (rp == NULL) {
4607 done++;
4608 break;
4609 }
4610
4611 msleep(2000);
4612 } while (max_wait_time--);
4613
4614 return done;
4615}
4616
4617/**
4618 * qla4xxx_wait_for_hba_online - waits for HBA to come online
4619 * @ha: Pointer to host adapter structure
4620 **/
4621static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
4622{
4623 unsigned long wait_online;
4624
Vikas Chaudharyf581a3f2010-10-06 22:47:48 -07004625 wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004626 while (time_before(jiffies, wait_online)) {
4627
4628 if (adapter_up(ha))
4629 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004630
4631 msleep(2000);
4632 }
4633
4634 return QLA_ERROR;
4635}
4636
4637/**
Mike Christiece545032008-02-29 18:25:20 -06004638 * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02004639 * @ha: pointer to HBA
David Somayajuluafaf5a22006-09-19 10:28:00 -07004640 * @t: target id
4641 * @l: lun id
4642 *
4643 * This function waits for all outstanding commands to a lun to complete. It
4644 * returns 0 if all pending commands are returned and 1 otherwise.
4645 **/
Mike Christiece545032008-02-29 18:25:20 -06004646static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
4647 struct scsi_target *stgt,
4648 struct scsi_device *sdev)
David Somayajuluafaf5a22006-09-19 10:28:00 -07004649{
4650 int cnt;
4651 int status = 0;
4652 struct scsi_cmnd *cmd;
4653
4654 /*
Mike Christiece545032008-02-29 18:25:20 -06004655 * Waiting for all commands for the designated target or dev
4656 * in the active array
David Somayajuluafaf5a22006-09-19 10:28:00 -07004657 */
4658 for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
4659 cmd = scsi_host_find_tag(ha->host, cnt);
Mike Christiece545032008-02-29 18:25:20 -06004660 if (cmd && stgt == scsi_target(cmd->device) &&
4661 (!sdev || sdev == cmd->device)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07004662 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
4663 status++;
4664 break;
4665 }
4666 }
4667 }
4668 return status;
4669}
4670
4671/**
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304672 * qla4xxx_eh_abort - callback for abort task.
4673 * @cmd: Pointer to Linux's SCSI command structure
4674 *
4675 * This routine is called by the Linux OS to abort the specified
4676 * command.
4677 **/
4678static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
4679{
4680 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
4681 unsigned int id = cmd->device->id;
4682 unsigned int lun = cmd->device->lun;
Mike Christie92b3e5b2010-10-06 22:51:17 -07004683 unsigned long flags;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304684 struct srb *srb = NULL;
4685 int ret = SUCCESS;
4686 int wait = 0;
4687
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304688 ql4_printk(KERN_INFO, ha,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004689 "scsi%ld:%d:%d: Abort command issued cmd=%p\n",
4690 ha->host_no, id, lun, cmd);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304691
Mike Christie92b3e5b2010-10-06 22:51:17 -07004692 spin_lock_irqsave(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304693 srb = (struct srb *) CMD_SP(cmd);
Mike Christie92b3e5b2010-10-06 22:51:17 -07004694 if (!srb) {
4695 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304696 return SUCCESS;
Mike Christie92b3e5b2010-10-06 22:51:17 -07004697 }
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304698 kref_get(&srb->srb_ref);
Mike Christie92b3e5b2010-10-06 22:51:17 -07004699 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304700
4701 if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
4702 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
4703 ha->host_no, id, lun));
4704 ret = FAILED;
4705 } else {
4706 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
4707 ha->host_no, id, lun));
4708 wait = 1;
4709 }
4710
4711 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
4712
4713 /* Wait for command to complete */
4714 if (wait) {
4715 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
4716 DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
4717 ha->host_no, id, lun));
4718 ret = FAILED;
4719 }
4720 }
4721
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304722 ql4_printk(KERN_INFO, ha,
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304723 "scsi%ld:%d:%d: Abort command - %s\n",
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004724 ha->host_no, id, lun, (ret == SUCCESS) ? "succeeded" : "failed");
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304725
4726 return ret;
4727}
4728
4729/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07004730 * qla4xxx_eh_device_reset - callback for target reset.
4731 * @cmd: Pointer to Linux's SCSI command structure
4732 *
4733 * This routine is called by the Linux OS to reset all luns on the
4734 * specified target.
4735 **/
4736static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
4737{
4738 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
4739 struct ddb_entry *ddb_entry = cmd->device->hostdata;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004740 int ret = FAILED, stat;
4741
Karen Higgins612f7342009-07-15 15:03:01 -05004742 if (!ddb_entry)
David Somayajuluafaf5a22006-09-19 10:28:00 -07004743 return ret;
4744
Mike Christiec01be6d2010-07-22 16:59:49 +05304745 ret = iscsi_block_scsi_eh(cmd);
4746 if (ret)
4747 return ret;
4748 ret = FAILED;
4749
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304750 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07004751 "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
4752 cmd->device->channel, cmd->device->id, cmd->device->lun);
4753
4754 DEBUG2(printk(KERN_INFO
4755 "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
4756 "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
Jens Axboe242f9dc2008-09-14 05:55:09 -07004757 cmd, jiffies, cmd->request->timeout / HZ,
David Somayajuluafaf5a22006-09-19 10:28:00 -07004758 ha->dpc_flags, cmd->result, cmd->allowed));
4759
4760 /* FIXME: wait for hba to go online */
4761 stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
4762 if (stat != QLA_SUCCESS) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304763 ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004764 goto eh_dev_reset_done;
4765 }
4766
Mike Christiece545032008-02-29 18:25:20 -06004767 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
4768 cmd->device)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304769 ql4_printk(KERN_INFO, ha,
Mike Christiece545032008-02-29 18:25:20 -06004770 "DEVICE RESET FAILED - waiting for "
4771 "commands.\n");
4772 goto eh_dev_reset_done;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004773 }
4774
David C Somayajulu9d562912008-03-19 11:23:03 -07004775 /* Send marker. */
4776 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
4777 MM_LUN_RESET) != QLA_SUCCESS)
4778 goto eh_dev_reset_done;
4779
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304780 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07004781 "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
4782 ha->host_no, cmd->device->channel, cmd->device->id,
4783 cmd->device->lun);
4784
4785 ret = SUCCESS;
4786
4787eh_dev_reset_done:
4788
4789 return ret;
4790}
4791
4792/**
Mike Christiece545032008-02-29 18:25:20 -06004793 * qla4xxx_eh_target_reset - callback for target reset.
4794 * @cmd: Pointer to Linux's SCSI command structure
4795 *
4796 * This routine is called by the Linux OS to reset the target.
4797 **/
4798static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
4799{
4800 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
4801 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christiec01be6d2010-07-22 16:59:49 +05304802 int stat, ret;
Mike Christiece545032008-02-29 18:25:20 -06004803
4804 if (!ddb_entry)
4805 return FAILED;
4806
Mike Christiec01be6d2010-07-22 16:59:49 +05304807 ret = iscsi_block_scsi_eh(cmd);
4808 if (ret)
4809 return ret;
4810
Mike Christiece545032008-02-29 18:25:20 -06004811 starget_printk(KERN_INFO, scsi_target(cmd->device),
4812 "WARM TARGET RESET ISSUED.\n");
4813
4814 DEBUG2(printk(KERN_INFO
4815 "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
4816 "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
Jens Axboe242f9dc2008-09-14 05:55:09 -07004817 ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
Mike Christiece545032008-02-29 18:25:20 -06004818 ha->dpc_flags, cmd->result, cmd->allowed));
4819
4820 stat = qla4xxx_reset_target(ha, ddb_entry);
4821 if (stat != QLA_SUCCESS) {
4822 starget_printk(KERN_INFO, scsi_target(cmd->device),
4823 "WARM TARGET RESET FAILED.\n");
4824 return FAILED;
4825 }
4826
Mike Christiece545032008-02-29 18:25:20 -06004827 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
4828 NULL)) {
4829 starget_printk(KERN_INFO, scsi_target(cmd->device),
4830 "WARM TARGET DEVICE RESET FAILED - "
4831 "waiting for commands.\n");
4832 return FAILED;
4833 }
4834
David C Somayajulu9d562912008-03-19 11:23:03 -07004835 /* Send marker. */
4836 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
4837 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
4838 starget_printk(KERN_INFO, scsi_target(cmd->device),
4839 "WARM TARGET DEVICE RESET FAILED - "
4840 "marker iocb failed.\n");
4841 return FAILED;
4842 }
4843
Mike Christiece545032008-02-29 18:25:20 -06004844 starget_printk(KERN_INFO, scsi_target(cmd->device),
4845 "WARM TARGET RESET SUCCEEDED.\n");
4846 return SUCCESS;
4847}
4848
4849/**
Sarang Radke8a288962011-12-06 02:34:10 -08004850 * qla4xxx_is_eh_active - check if error handler is running
4851 * @shost: Pointer to SCSI Host struct
4852 *
4853 * This routine finds that if reset host is called in EH
4854 * scenario or from some application like sg_reset
4855 **/
4856static int qla4xxx_is_eh_active(struct Scsi_Host *shost)
4857{
4858 if (shost->shost_state == SHOST_RECOVERY)
4859 return 1;
4860 return 0;
4861}
4862
4863/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07004864 * qla4xxx_eh_host_reset - kernel callback
4865 * @cmd: Pointer to Linux's SCSI command structure
4866 *
4867 * This routine is invoked by the Linux kernel to perform fatal error
4868 * recovery on the specified adapter.
4869 **/
4870static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
4871{
4872 int return_status = FAILED;
4873 struct scsi_qla_host *ha;
4874
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004875 ha = to_qla_host(cmd->device->host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004876
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304877 if (ql4xdontresethba) {
4878 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
4879 ha->host_no, __func__));
Sarang Radke8a288962011-12-06 02:34:10 -08004880
4881 /* Clear outstanding srb in queues */
4882 if (qla4xxx_is_eh_active(cmd->device->host))
4883 qla4xxx_abort_active_cmds(ha, DID_ABORT << 16);
4884
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304885 return FAILED;
4886 }
4887
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304888 ql4_printk(KERN_INFO, ha,
Karen Higginsdca05c42009-07-15 15:03:00 -05004889 "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
David Somayajuluafaf5a22006-09-19 10:28:00 -07004890 cmd->device->channel, cmd->device->id, cmd->device->lun);
4891
4892 if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
4893 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
4894 "DEAD.\n", ha->host_no, cmd->device->channel,
4895 __func__));
4896
4897 return FAILED;
4898 }
4899
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304900 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
4901 if (is_qla8022(ha))
4902 set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
4903 else
4904 set_bit(DPC_RESET_HA, &ha->dpc_flags);
4905 }
Mike Christie50a29ae2008-03-04 13:26:53 -06004906
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304907 if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
David Somayajuluafaf5a22006-09-19 10:28:00 -07004908 return_status = SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004909
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304910 ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004911 return_status == FAILED ? "FAILED" : "SUCCEEDED");
David Somayajuluafaf5a22006-09-19 10:28:00 -07004912
4913 return return_status;
4914}
4915
Vikas Chaudhary95d31262011-08-12 02:51:29 -07004916static int qla4xxx_context_reset(struct scsi_qla_host *ha)
4917{
4918 uint32_t mbox_cmd[MBOX_REG_COUNT];
4919 uint32_t mbox_sts[MBOX_REG_COUNT];
4920 struct addr_ctrl_blk_def *acb = NULL;
4921 uint32_t acb_len = sizeof(struct addr_ctrl_blk_def);
4922 int rval = QLA_SUCCESS;
4923 dma_addr_t acb_dma;
4924
4925 acb = dma_alloc_coherent(&ha->pdev->dev,
4926 sizeof(struct addr_ctrl_blk_def),
4927 &acb_dma, GFP_KERNEL);
4928 if (!acb) {
4929 ql4_printk(KERN_ERR, ha, "%s: Unable to alloc acb\n",
4930 __func__);
4931 rval = -ENOMEM;
4932 goto exit_port_reset;
4933 }
4934
4935 memset(acb, 0, acb_len);
4936
4937 rval = qla4xxx_get_acb(ha, acb_dma, PRIMARI_ACB, acb_len);
4938 if (rval != QLA_SUCCESS) {
4939 rval = -EIO;
4940 goto exit_free_acb;
4941 }
4942
4943 rval = qla4xxx_disable_acb(ha);
4944 if (rval != QLA_SUCCESS) {
4945 rval = -EIO;
4946 goto exit_free_acb;
4947 }
4948
4949 wait_for_completion_timeout(&ha->disable_acb_comp,
4950 DISABLE_ACB_TOV * HZ);
4951
4952 rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], acb_dma);
4953 if (rval != QLA_SUCCESS) {
4954 rval = -EIO;
4955 goto exit_free_acb;
4956 }
4957
4958exit_free_acb:
4959 dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk_def),
4960 acb, acb_dma);
4961exit_port_reset:
4962 DEBUG2(ql4_printk(KERN_INFO, ha, "%s %s\n", __func__,
4963 rval == QLA_SUCCESS ? "SUCCEEDED" : "FAILED"));
4964 return rval;
4965}
4966
4967static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type)
4968{
4969 struct scsi_qla_host *ha = to_qla_host(shost);
4970 int rval = QLA_SUCCESS;
4971
4972 if (ql4xdontresethba) {
4973 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Don't Reset HBA\n",
4974 __func__));
4975 rval = -EPERM;
4976 goto exit_host_reset;
4977 }
4978
4979 rval = qla4xxx_wait_for_hba_online(ha);
4980 if (rval != QLA_SUCCESS) {
4981 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Unable to reset host "
4982 "adapter\n", __func__));
4983 rval = -EIO;
4984 goto exit_host_reset;
4985 }
4986
4987 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
4988 goto recover_adapter;
4989
4990 switch (reset_type) {
4991 case SCSI_ADAPTER_RESET:
4992 set_bit(DPC_RESET_HA, &ha->dpc_flags);
4993 break;
4994 case SCSI_FIRMWARE_RESET:
4995 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
4996 if (is_qla8022(ha))
4997 /* set firmware context reset */
4998 set_bit(DPC_RESET_HA_FW_CONTEXT,
4999 &ha->dpc_flags);
5000 else {
5001 rval = qla4xxx_context_reset(ha);
5002 goto exit_host_reset;
5003 }
5004 }
5005 break;
5006 }
5007
5008recover_adapter:
5009 rval = qla4xxx_recover_adapter(ha);
5010 if (rval != QLA_SUCCESS) {
5011 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: recover adapter fail\n",
5012 __func__));
5013 rval = -EIO;
5014 }
5015
5016exit_host_reset:
5017 return rval;
5018}
5019
Lalit Chandivade2232be02010-07-30 14:38:47 +05305020/* PCI AER driver recovers from all correctable errors w/o
5021 * driver intervention. For uncorrectable errors PCI AER
5022 * driver calls the following device driver's callbacks
5023 *
5024 * - Fatal Errors - link_reset
5025 * - Non-Fatal Errors - driver's pci_error_detected() which
5026 * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
5027 *
5028 * PCI AER driver calls
5029 * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
5030 * returns RECOVERED or NEED_RESET if fw_hung
5031 * NEED_RESET - driver's slot_reset()
5032 * DISCONNECT - device is dead & cannot recover
5033 * RECOVERED - driver's pci_resume()
5034 */
5035static pci_ers_result_t
5036qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
5037{
5038 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
5039
5040 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
5041 ha->host_no, __func__, state);
5042
5043 if (!is_aer_supported(ha))
5044 return PCI_ERS_RESULT_NONE;
5045
5046 switch (state) {
5047 case pci_channel_io_normal:
5048 clear_bit(AF_EEH_BUSY, &ha->flags);
5049 return PCI_ERS_RESULT_CAN_RECOVER;
5050 case pci_channel_io_frozen:
5051 set_bit(AF_EEH_BUSY, &ha->flags);
5052 qla4xxx_mailbox_premature_completion(ha);
5053 qla4xxx_free_irqs(ha);
5054 pci_disable_device(pdev);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07005055 /* Return back all IOs */
5056 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
Lalit Chandivade2232be02010-07-30 14:38:47 +05305057 return PCI_ERS_RESULT_NEED_RESET;
5058 case pci_channel_io_perm_failure:
5059 set_bit(AF_EEH_BUSY, &ha->flags);
5060 set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
5061 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
5062 return PCI_ERS_RESULT_DISCONNECT;
5063 }
5064 return PCI_ERS_RESULT_NEED_RESET;
5065}
5066
5067/**
5068 * qla4xxx_pci_mmio_enabled() gets called if
5069 * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
5070 * and read/write to the device still works.
5071 **/
5072static pci_ers_result_t
5073qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
5074{
5075 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
5076
5077 if (!is_aer_supported(ha))
5078 return PCI_ERS_RESULT_NONE;
5079
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07005080 return PCI_ERS_RESULT_RECOVERED;
Lalit Chandivade2232be02010-07-30 14:38:47 +05305081}
5082
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07005083static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
Lalit Chandivade2232be02010-07-30 14:38:47 +05305084{
5085 uint32_t rval = QLA_ERROR;
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07005086 uint32_t ret = 0;
Lalit Chandivade2232be02010-07-30 14:38:47 +05305087 int fn;
5088 struct pci_dev *other_pdev = NULL;
5089
5090 ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
5091
5092 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
5093
5094 if (test_bit(AF_ONLINE, &ha->flags)) {
5095 clear_bit(AF_ONLINE, &ha->flags);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05005096 clear_bit(AF_LINK_UP, &ha->flags);
5097 iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
Lalit Chandivade2232be02010-07-30 14:38:47 +05305098 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
Lalit Chandivade2232be02010-07-30 14:38:47 +05305099 }
5100
5101 fn = PCI_FUNC(ha->pdev->devfn);
5102 while (fn > 0) {
5103 fn--;
5104 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
5105 "func %x\n", ha->host_no, __func__, fn);
5106 /* Get the pci device given the domain, bus,
5107 * slot/function number */
5108 other_pdev =
5109 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
5110 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
5111 fn));
5112
5113 if (!other_pdev)
5114 continue;
5115
5116 if (atomic_read(&other_pdev->enable_cnt)) {
5117 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
5118 "func in enabled state%x\n", ha->host_no,
5119 __func__, fn);
5120 pci_dev_put(other_pdev);
5121 break;
5122 }
5123 pci_dev_put(other_pdev);
5124 }
5125
5126 /* The first function on the card, the reset owner will
5127 * start & initialize the firmware. The other functions
5128 * on the card will reset the firmware context
5129 */
5130 if (!fn) {
5131 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
5132 "0x%x is the owner\n", ha->host_no, __func__,
5133 ha->pdev->devfn);
5134
5135 qla4_8xxx_idc_lock(ha);
5136 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
5137 QLA82XX_DEV_COLD);
5138
5139 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
5140 QLA82XX_IDC_VERSION);
5141
5142 qla4_8xxx_idc_unlock(ha);
5143 clear_bit(AF_FW_RECOVERY, &ha->flags);
Mike Christie13483732011-12-01 21:38:41 -06005144 rval = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
Lalit Chandivade2232be02010-07-30 14:38:47 +05305145 qla4_8xxx_idc_lock(ha);
5146
5147 if (rval != QLA_SUCCESS) {
5148 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
5149 "FAILED\n", ha->host_no, __func__);
5150 qla4_8xxx_clear_drv_active(ha);
5151 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
5152 QLA82XX_DEV_FAILED);
5153 } else {
5154 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
5155 "READY\n", ha->host_no, __func__);
5156 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
5157 QLA82XX_DEV_READY);
5158 /* Clear driver state register */
5159 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
5160 qla4_8xxx_set_drv_active(ha);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07005161 ret = qla4xxx_request_irqs(ha);
5162 if (ret) {
5163 ql4_printk(KERN_WARNING, ha, "Failed to "
5164 "reserve interrupt %d already in use.\n",
5165 ha->pdev->irq);
5166 rval = QLA_ERROR;
5167 } else {
5168 ha->isp_ops->enable_intrs(ha);
5169 rval = QLA_SUCCESS;
5170 }
Lalit Chandivade2232be02010-07-30 14:38:47 +05305171 }
5172 qla4_8xxx_idc_unlock(ha);
5173 } else {
5174 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
5175 "the reset owner\n", ha->host_no, __func__,
5176 ha->pdev->devfn);
5177 if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
5178 QLA82XX_DEV_READY)) {
5179 clear_bit(AF_FW_RECOVERY, &ha->flags);
Mike Christie13483732011-12-01 21:38:41 -06005180 rval = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07005181 if (rval == QLA_SUCCESS) {
5182 ret = qla4xxx_request_irqs(ha);
5183 if (ret) {
5184 ql4_printk(KERN_WARNING, ha, "Failed to"
5185 " reserve interrupt %d already in"
5186 " use.\n", ha->pdev->irq);
5187 rval = QLA_ERROR;
5188 } else {
5189 ha->isp_ops->enable_intrs(ha);
5190 rval = QLA_SUCCESS;
5191 }
5192 }
Lalit Chandivade2232be02010-07-30 14:38:47 +05305193 qla4_8xxx_idc_lock(ha);
5194 qla4_8xxx_set_drv_active(ha);
5195 qla4_8xxx_idc_unlock(ha);
5196 }
5197 }
5198 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
5199 return rval;
5200}
5201
5202static pci_ers_result_t
5203qla4xxx_pci_slot_reset(struct pci_dev *pdev)
5204{
5205 pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
5206 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
5207 int rc;
5208
5209 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
5210 ha->host_no, __func__);
5211
5212 if (!is_aer_supported(ha))
5213 return PCI_ERS_RESULT_NONE;
5214
5215 /* Restore the saved state of PCIe device -
5216 * BAR registers, PCI Config space, PCIX, MSI,
5217 * IOV states
5218 */
5219 pci_restore_state(pdev);
5220
5221 /* pci_restore_state() clears the saved_state flag of the device
5222 * save restored state which resets saved_state flag
5223 */
5224 pci_save_state(pdev);
5225
5226 /* Initialize device or resume if in suspended state */
5227 rc = pci_enable_device(pdev);
5228 if (rc) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005229 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Can't re-enable "
Lalit Chandivade2232be02010-07-30 14:38:47 +05305230 "device after reset\n", ha->host_no, __func__);
5231 goto exit_slot_reset;
5232 }
5233
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07005234 ha->isp_ops->disable_intrs(ha);
Lalit Chandivade2232be02010-07-30 14:38:47 +05305235
5236 if (is_qla8022(ha)) {
5237 if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
5238 ret = PCI_ERS_RESULT_RECOVERED;
5239 goto exit_slot_reset;
5240 } else
5241 goto exit_slot_reset;
5242 }
5243
5244exit_slot_reset:
5245 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
5246 "device after reset\n", ha->host_no, __func__, ret);
5247 return ret;
5248}
5249
5250static void
5251qla4xxx_pci_resume(struct pci_dev *pdev)
5252{
5253 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
5254 int ret;
5255
5256 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
5257 ha->host_no, __func__);
5258
5259 ret = qla4xxx_wait_for_hba_online(ha);
5260 if (ret != QLA_SUCCESS) {
5261 ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
5262 "resume I/O from slot/link_reset\n", ha->host_no,
5263 __func__);
5264 }
5265
5266 pci_cleanup_aer_uncorrect_error_status(pdev);
5267 clear_bit(AF_EEH_BUSY, &ha->flags);
5268}
5269
5270static struct pci_error_handlers qla4xxx_err_handler = {
5271 .error_detected = qla4xxx_pci_error_detected,
5272 .mmio_enabled = qla4xxx_pci_mmio_enabled,
5273 .slot_reset = qla4xxx_pci_slot_reset,
5274 .resume = qla4xxx_pci_resume,
5275};
5276
David Somayajuluafaf5a22006-09-19 10:28:00 -07005277static struct pci_device_id qla4xxx_pci_tbl[] = {
5278 {
5279 .vendor = PCI_VENDOR_ID_QLOGIC,
5280 .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
5281 .subvendor = PCI_ANY_ID,
5282 .subdevice = PCI_ANY_ID,
5283 },
5284 {
5285 .vendor = PCI_VENDOR_ID_QLOGIC,
5286 .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
5287 .subvendor = PCI_ANY_ID,
5288 .subdevice = PCI_ANY_ID,
5289 },
David C Somayajulud9150582006-11-15 17:38:40 -08005290 {
5291 .vendor = PCI_VENDOR_ID_QLOGIC,
5292 .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
5293 .subvendor = PCI_ANY_ID,
5294 .subdevice = PCI_ANY_ID,
5295 },
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05305296 {
5297 .vendor = PCI_VENDOR_ID_QLOGIC,
5298 .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
5299 .subvendor = PCI_ANY_ID,
5300 .subdevice = PCI_ANY_ID,
5301 },
David Somayajuluafaf5a22006-09-19 10:28:00 -07005302 {0, 0},
5303};
5304MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
5305
Adrian Bunk47975472007-04-26 00:35:16 -07005306static struct pci_driver qla4xxx_pci_driver = {
David Somayajuluafaf5a22006-09-19 10:28:00 -07005307 .name = DRIVER_NAME,
5308 .id_table = qla4xxx_pci_tbl,
5309 .probe = qla4xxx_probe_adapter,
5310 .remove = qla4xxx_remove_adapter,
Lalit Chandivade2232be02010-07-30 14:38:47 +05305311 .err_handler = &qla4xxx_err_handler,
David Somayajuluafaf5a22006-09-19 10:28:00 -07005312};
5313
5314static int __init qla4xxx_module_init(void)
5315{
5316 int ret;
5317
5318 /* Allocate cache for SRBs. */
5319 srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09005320 SLAB_HWCACHE_ALIGN, NULL);
David Somayajuluafaf5a22006-09-19 10:28:00 -07005321 if (srb_cachep == NULL) {
5322 printk(KERN_ERR
5323 "%s: Unable to allocate SRB cache..."
5324 "Failing load!\n", DRIVER_NAME);
5325 ret = -ENOMEM;
5326 goto no_srp_cache;
5327 }
5328
5329 /* Derive version string. */
5330 strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
Andrew Vasquez11010fe2006-10-06 09:54:59 -07005331 if (ql4xextended_error_logging)
David Somayajuluafaf5a22006-09-19 10:28:00 -07005332 strcat(qla4xxx_version_str, "-debug");
5333
5334 qla4xxx_scsi_transport =
5335 iscsi_register_transport(&qla4xxx_iscsi_transport);
5336 if (!qla4xxx_scsi_transport){
5337 ret = -ENODEV;
5338 goto release_srb_cache;
5339 }
5340
David Somayajuluafaf5a22006-09-19 10:28:00 -07005341 ret = pci_register_driver(&qla4xxx_pci_driver);
5342 if (ret)
5343 goto unregister_transport;
5344
5345 printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
5346 return 0;
Doug Maxey5ae16db2006-10-05 23:50:07 -05005347
David Somayajuluafaf5a22006-09-19 10:28:00 -07005348unregister_transport:
5349 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
5350release_srb_cache:
5351 kmem_cache_destroy(srb_cachep);
5352no_srp_cache:
5353 return ret;
5354}
5355
5356static void __exit qla4xxx_module_exit(void)
5357{
David Somayajuluafaf5a22006-09-19 10:28:00 -07005358 pci_unregister_driver(&qla4xxx_pci_driver);
5359 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
5360 kmem_cache_destroy(srb_cachep);
5361}
5362
5363module_init(qla4xxx_module_init);
5364module_exit(qla4xxx_module_exit);
5365
5366MODULE_AUTHOR("QLogic Corporation");
5367MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
5368MODULE_LICENSE("GPL");
5369MODULE_VERSION(QLA4XXX_DRIVER_VERSION);