blob: ae5d512bf43b06f21bcd09abedd2786f1854f473 [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 Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-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 Chaudharyf4f5df22010-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);
Al Viro587a1f12011-07-23 23:11:19 -0400131static umode_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 Chaudharyf4f5df22010-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
Al Viro587a1f12011-07-23 23:11:19 -0400200static umode_t ql4_attr_is_visible(int param_type, int param)
Mike Christie3128c6c2011-07-25 13:48:42 -0500201{
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 =
Nilesh Javalic28eaac2011-12-18 21:40:44 -08001633 (le16_to_cpu(fw_ddb_entry->def_timeout) > LOGIN_TOV) &&
1634 (le16_to_cpu(fw_ddb_entry->def_timeout) < LOGIN_TOV * 10) ?
1635 le16_to_cpu(fw_ddb_entry->def_timeout) : LOGIN_TOV;
Mike Christie13483732011-12-01 21:38:41 -06001636 ddb_entry->default_time2wait =
1637 le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
1638
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001639 /* Update params */
1640 conn->max_recv_dlength = BYTE_UNITS *
1641 le16_to_cpu(fw_ddb_entry->iscsi_max_rcv_data_seg_len);
1642
1643 conn->max_xmit_dlength = BYTE_UNITS *
1644 le16_to_cpu(fw_ddb_entry->iscsi_max_snd_data_seg_len);
1645
1646 sess->initial_r2t_en =
1647 (BIT_10 & le16_to_cpu(fw_ddb_entry->iscsi_options));
1648
1649 sess->max_r2t = le16_to_cpu(fw_ddb_entry->iscsi_max_outsnd_r2t);
1650
1651 sess->imm_data_en = (BIT_11 & le16_to_cpu(fw_ddb_entry->iscsi_options));
1652
1653 sess->first_burst = BYTE_UNITS *
1654 le16_to_cpu(fw_ddb_entry->iscsi_first_burst_len);
1655
1656 sess->max_burst = BYTE_UNITS *
1657 le16_to_cpu(fw_ddb_entry->iscsi_max_burst_len);
1658
1659 sess->time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
1660
1661 sess->time2retain = le16_to_cpu(fw_ddb_entry->iscsi_def_time2retain);
1662
1663 sess->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
1664
1665 memcpy(sess->initiatorname, ha->name_string,
1666 min(sizeof(ha->name_string), sizeof(sess->initiatorname)));
Mike Christie13483732011-12-01 21:38:41 -06001667
1668exit_session_conn_param:
1669 if (fw_ddb_entry)
1670 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1671 fw_ddb_entry, fw_ddb_entry_dma);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001672}
1673
David Somayajuluafaf5a22006-09-19 10:28:00 -07001674/*
1675 * Timer routines
1676 */
1677
1678static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
1679 unsigned long interval)
1680{
1681 DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
1682 __func__, ha->host->host_no));
1683 init_timer(&ha->timer);
1684 ha->timer.expires = jiffies + interval * HZ;
1685 ha->timer.data = (unsigned long)ha;
1686 ha->timer.function = (void (*)(unsigned long))func;
1687 add_timer(&ha->timer);
1688 ha->timer_active = 1;
1689}
1690
1691static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
1692{
1693 del_timer_sync(&ha->timer);
1694 ha->timer_active = 0;
1695}
1696
1697/***
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001698 * qla4xxx_mark_device_missing - blocks the session
1699 * @cls_session: Pointer to the session to be blocked
David Somayajuluafaf5a22006-09-19 10:28:00 -07001700 * @ddb_entry: Pointer to device database entry
1701 *
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301702 * This routine marks a device missing and close connection.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001703 **/
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001704void qla4xxx_mark_device_missing(struct iscsi_cls_session *cls_session)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001705{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001706 iscsi_block_session(cls_session);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001707}
1708
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301709/**
1710 * qla4xxx_mark_all_devices_missing - mark all devices as missing.
1711 * @ha: Pointer to host adapter structure.
1712 *
1713 * This routine marks a device missing and resets the relogin retry count.
1714 **/
1715void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
1716{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001717 iscsi_host_for_each_session(ha->host, qla4xxx_mark_device_missing);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301718}
1719
David Somayajuluafaf5a22006-09-19 10:28:00 -07001720static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
1721 struct ddb_entry *ddb_entry,
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001722 struct scsi_cmnd *cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001723{
1724 struct srb *srb;
1725
1726 srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
1727 if (!srb)
1728 return srb;
1729
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301730 kref_init(&srb->srb_ref);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001731 srb->ha = ha;
1732 srb->ddb = ddb_entry;
1733 srb->cmd = cmd;
1734 srb->flags = 0;
Vikas Chaudhary53698872010-04-28 11:41:59 +05301735 CMD_SP(cmd) = (void *)srb;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001736
1737 return srb;
1738}
1739
1740static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
1741{
1742 struct scsi_cmnd *cmd = srb->cmd;
1743
1744 if (srb->flags & SRB_DMA_VALID) {
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +09001745 scsi_dma_unmap(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001746 srb->flags &= ~SRB_DMA_VALID;
1747 }
Vikas Chaudhary53698872010-04-28 11:41:59 +05301748 CMD_SP(cmd) = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001749}
1750
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301751void qla4xxx_srb_compl(struct kref *ref)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001752{
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301753 struct srb *srb = container_of(ref, struct srb, srb_ref);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001754 struct scsi_cmnd *cmd = srb->cmd;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301755 struct scsi_qla_host *ha = srb->ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001756
1757 qla4xxx_srb_free_dma(ha, srb);
1758
1759 mempool_free(srb, ha->srb_mempool);
1760
1761 cmd->scsi_done(cmd);
1762}
1763
1764/**
1765 * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001766 * @host: scsi host
David Somayajuluafaf5a22006-09-19 10:28:00 -07001767 * @cmd: Pointer to Linux's SCSI command structure
David Somayajuluafaf5a22006-09-19 10:28:00 -07001768 *
1769 * Remarks:
1770 * This routine is invoked by Linux to send a SCSI command to the driver.
1771 * The mid-level driver tries to ensure that queuecommand never gets
1772 * invoked concurrently with itself or the interrupt handler (although
1773 * the interrupt handler may call this routine as part of request-
1774 * completion handling). Unfortunely, it sometimes calls the scheduler
1775 * in interrupt context which is a big NO! NO!.
1776 **/
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001777static int qla4xxx_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001778{
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001779 struct scsi_qla_host *ha = to_qla_host(host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001780 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christie7fb19212008-01-31 13:36:45 -06001781 struct iscsi_cls_session *sess = ddb_entry->sess;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001782 struct srb *srb;
1783 int rval;
1784
Lalit Chandivade2232be02010-07-30 14:38:47 +05301785 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1786 if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
1787 cmd->result = DID_NO_CONNECT << 16;
1788 else
1789 cmd->result = DID_REQUEUE << 16;
1790 goto qc_fail_command;
1791 }
1792
Mike Christie7fb19212008-01-31 13:36:45 -06001793 if (!sess) {
1794 cmd->result = DID_IMM_RETRY << 16;
1795 goto qc_fail_command;
1796 }
1797
1798 rval = iscsi_session_chkready(sess);
1799 if (rval) {
1800 cmd->result = rval;
1801 goto qc_fail_command;
1802 }
1803
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301804 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1805 test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
1806 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1807 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
1808 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
1809 !test_bit(AF_ONLINE, &ha->flags) ||
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001810 !test_bit(AF_LINK_UP, &ha->flags) ||
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301811 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
David C Somayajulu477ffb92007-01-22 12:26:11 -08001812 goto qc_host_busy;
1813
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001814 srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001815 if (!srb)
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001816 goto qc_host_busy;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001817
1818 rval = qla4xxx_send_command_to_isp(ha, srb);
1819 if (rval != QLA_SUCCESS)
1820 goto qc_host_busy_free_sp;
1821
David Somayajuluafaf5a22006-09-19 10:28:00 -07001822 return 0;
1823
1824qc_host_busy_free_sp:
1825 qla4xxx_srb_free_dma(ha, srb);
1826 mempool_free(srb, ha->srb_mempool);
1827
David Somayajuluafaf5a22006-09-19 10:28:00 -07001828qc_host_busy:
1829 return SCSI_MLQUEUE_HOST_BUSY;
1830
1831qc_fail_command:
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001832 cmd->scsi_done(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001833
1834 return 0;
1835}
1836
1837/**
1838 * qla4xxx_mem_free - frees memory allocated to adapter
1839 * @ha: Pointer to host adapter structure.
1840 *
1841 * Frees memory previously allocated by qla4xxx_mem_alloc
1842 **/
1843static void qla4xxx_mem_free(struct scsi_qla_host *ha)
1844{
1845 if (ha->queues)
1846 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
1847 ha->queues_dma);
1848
1849 ha->queues_len = 0;
1850 ha->queues = NULL;
1851 ha->queues_dma = 0;
1852 ha->request_ring = NULL;
1853 ha->request_dma = 0;
1854 ha->response_ring = NULL;
1855 ha->response_dma = 0;
1856 ha->shadow_regs = NULL;
1857 ha->shadow_regs_dma = 0;
1858
1859 /* Free srb pool. */
1860 if (ha->srb_mempool)
1861 mempool_destroy(ha->srb_mempool);
1862
1863 ha->srb_mempool = NULL;
1864
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001865 if (ha->chap_dma_pool)
1866 dma_pool_destroy(ha->chap_dma_pool);
1867
Lalit Chandivade45494152011-10-07 16:55:42 -07001868 if (ha->chap_list)
1869 vfree(ha->chap_list);
1870 ha->chap_list = NULL;
1871
Mike Christie13483732011-12-01 21:38:41 -06001872 if (ha->fw_ddb_dma_pool)
1873 dma_pool_destroy(ha->fw_ddb_dma_pool);
1874
David Somayajuluafaf5a22006-09-19 10:28:00 -07001875 /* release io space registers */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301876 if (is_qla8022(ha)) {
1877 if (ha->nx_pcibase)
1878 iounmap(
1879 (struct device_reg_82xx __iomem *)ha->nx_pcibase);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301880 } else if (ha->reg)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001881 iounmap(ha->reg);
1882 pci_release_regions(ha->pdev);
1883}
1884
1885/**
1886 * qla4xxx_mem_alloc - allocates memory for use by adapter.
1887 * @ha: Pointer to host adapter structure
1888 *
1889 * Allocates DMA memory for request and response queues. Also allocates memory
1890 * for srbs.
1891 **/
1892static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
1893{
1894 unsigned long align;
1895
1896 /* Allocate contiguous block of DMA memory for queues. */
1897 ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
1898 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
1899 sizeof(struct shadow_regs) +
1900 MEM_ALIGN_VALUE +
1901 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
1902 ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
1903 &ha->queues_dma, GFP_KERNEL);
1904 if (ha->queues == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301905 ql4_printk(KERN_WARNING, ha,
1906 "Memory Allocation failed - queues.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001907
1908 goto mem_alloc_error_exit;
1909 }
1910 memset(ha->queues, 0, ha->queues_len);
1911
1912 /*
1913 * As per RISC alignment requirements -- the bus-address must be a
1914 * multiple of the request-ring size (in bytes).
1915 */
1916 align = 0;
1917 if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
1918 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
1919 (MEM_ALIGN_VALUE - 1));
1920
1921 /* Update request and response queue pointers. */
1922 ha->request_dma = ha->queues_dma + align;
1923 ha->request_ring = (struct queue_entry *) (ha->queues + align);
1924 ha->response_dma = ha->queues_dma + align +
1925 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
1926 ha->response_ring = (struct queue_entry *) (ha->queues + align +
1927 (REQUEST_QUEUE_DEPTH *
1928 QUEUE_SIZE));
1929 ha->shadow_regs_dma = ha->queues_dma + align +
1930 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
1931 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
1932 ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
1933 (REQUEST_QUEUE_DEPTH *
1934 QUEUE_SIZE) +
1935 (RESPONSE_QUEUE_DEPTH *
1936 QUEUE_SIZE));
1937
1938 /* Allocate memory for srb pool. */
1939 ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
1940 mempool_free_slab, srb_cachep);
1941 if (ha->srb_mempool == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301942 ql4_printk(KERN_WARNING, ha,
1943 "Memory Allocation failed - SRB Pool.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001944
1945 goto mem_alloc_error_exit;
1946 }
1947
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001948 ha->chap_dma_pool = dma_pool_create("ql4_chap", &ha->pdev->dev,
1949 CHAP_DMA_BLOCK_SIZE, 8, 0);
1950
1951 if (ha->chap_dma_pool == NULL) {
1952 ql4_printk(KERN_WARNING, ha,
1953 "%s: chap_dma_pool allocation failed..\n", __func__);
1954 goto mem_alloc_error_exit;
1955 }
1956
Mike Christie13483732011-12-01 21:38:41 -06001957 ha->fw_ddb_dma_pool = dma_pool_create("ql4_fw_ddb", &ha->pdev->dev,
1958 DDB_DMA_BLOCK_SIZE, 8, 0);
1959
1960 if (ha->fw_ddb_dma_pool == NULL) {
1961 ql4_printk(KERN_WARNING, ha,
1962 "%s: fw_ddb_dma_pool allocation failed..\n",
1963 __func__);
1964 goto mem_alloc_error_exit;
1965 }
1966
David Somayajuluafaf5a22006-09-19 10:28:00 -07001967 return QLA_SUCCESS;
1968
1969mem_alloc_error_exit:
1970 qla4xxx_mem_free(ha);
1971 return QLA_ERROR;
1972}
1973
1974/**
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301975 * qla4_8xxx_check_fw_alive - Check firmware health
1976 * @ha: Pointer to host adapter structure.
1977 *
1978 * Context: Interrupt
1979 **/
Shyam Sunder9ee91a32011-12-01 22:42:13 -08001980static int qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301981{
Shyam Sunder9ee91a32011-12-01 22:42:13 -08001982 uint32_t fw_heartbeat_counter;
1983 int status = QLA_SUCCESS;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301984
1985 fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
Lalit Chandivade2232be02010-07-30 14:38:47 +05301986 /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
1987 if (fw_heartbeat_counter == 0xffffffff) {
1988 DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
1989 "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
1990 ha->host_no, __func__));
Shyam Sunder9ee91a32011-12-01 22:42:13 -08001991 return status;
Lalit Chandivade2232be02010-07-30 14:38:47 +05301992 }
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301993
1994 if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
1995 ha->seconds_since_last_heartbeat++;
1996 /* FW not alive after 2 seconds */
1997 if (ha->seconds_since_last_heartbeat == 2) {
1998 ha->seconds_since_last_heartbeat = 0;
Vikas Chaudhary68d92eb2011-05-17 23:17:05 -07001999
2000 ql4_printk(KERN_INFO, ha,
2001 "scsi(%ld): %s, Dumping hw/fw registers:\n "
2002 " PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2:"
2003 " 0x%x,\n PEG_NET_0_PC: 0x%x, PEG_NET_1_PC:"
2004 " 0x%x,\n PEG_NET_2_PC: 0x%x, PEG_NET_3_PC:"
2005 " 0x%x,\n PEG_NET_4_PC: 0x%x\n",
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002006 ha->host_no, __func__,
2007 qla4_8xxx_rd_32(ha,
2008 QLA82XX_PEG_HALT_STATUS1),
Vikas Chaudhary68d92eb2011-05-17 23:17:05 -07002009 qla4_8xxx_rd_32(ha,
2010 QLA82XX_PEG_HALT_STATUS2),
2011 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_0 +
2012 0x3c),
2013 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_1 +
2014 0x3c),
2015 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_2 +
2016 0x3c),
2017 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_3 +
2018 0x3c),
2019 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_4 +
2020 0x3c));
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002021 status = QLA_ERROR;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302022 }
Lalit Chandivade99457d72010-10-06 22:49:32 -07002023 } else
2024 ha->seconds_since_last_heartbeat = 0;
2025
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302026 ha->fw_heartbeat_counter = fw_heartbeat_counter;
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002027 return status;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302028}
2029
2030/**
2031 * qla4_8xxx_watchdog - Poll dev state
2032 * @ha: Pointer to host adapter structure.
2033 *
2034 * Context: Interrupt
2035 **/
2036void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
2037{
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002038 uint32_t dev_state, halt_status;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302039
2040 /* don't poll if reset is going on */
Lalit Chandivaded56a1f72010-12-02 22:12:45 -08002041 if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
2042 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
Vikas Chaudhary977f46a2011-05-17 23:17:08 -07002043 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags))) {
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002044 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302045 if (dev_state == QLA82XX_DEV_NEED_RESET &&
2046 !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
Vikas Chaudhary3930b8c2010-12-02 22:12:47 -08002047 if (!ql4xdontresethba) {
2048 ql4_printk(KERN_INFO, ha, "%s: HW State: "
2049 "NEED RESET!\n", __func__);
2050 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2051 qla4xxx_wake_dpc(ha);
Vikas Chaudhary3930b8c2010-12-02 22:12:47 -08002052 }
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302053 } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
2054 !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
Vikas Chaudhary3930b8c2010-12-02 22:12:47 -08002055 ql4_printk(KERN_INFO, ha, "%s: HW State: NEED QUIES!\n",
2056 __func__);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302057 set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
2058 qla4xxx_wake_dpc(ha);
2059 } else {
2060 /* Check firmware health */
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002061 if (qla4_8xxx_check_fw_alive(ha)) {
2062 halt_status = qla4_8xxx_rd_32(ha,
2063 QLA82XX_PEG_HALT_STATUS1);
2064
2065 /* Since we cannot change dev_state in interrupt
2066 * context, set appropriate DPC flag then wakeup
2067 * DPC */
2068 if (halt_status & HALT_STATUS_UNRECOVERABLE)
2069 set_bit(DPC_HA_UNRECOVERABLE,
2070 &ha->dpc_flags);
2071 else {
2072 ql4_printk(KERN_INFO, ha, "%s: detect "
2073 "abort needed!\n", __func__);
2074 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2075 }
2076 qla4xxx_mailbox_premature_completion(ha);
2077 qla4xxx_wake_dpc(ha);
2078 }
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302079 }
2080 }
2081}
2082
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08002083static void qla4xxx_check_relogin_flash_ddb(struct iscsi_cls_session *cls_sess)
Mike Christie13483732011-12-01 21:38:41 -06002084{
2085 struct iscsi_session *sess;
2086 struct ddb_entry *ddb_entry;
2087 struct scsi_qla_host *ha;
2088
2089 sess = cls_sess->dd_data;
2090 ddb_entry = sess->dd_data;
2091 ha = ddb_entry->ha;
2092
2093 if (!(ddb_entry->ddb_type == FLASH_DDB))
2094 return;
2095
2096 if (adapter_up(ha) && !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
2097 !iscsi_is_session_online(cls_sess)) {
2098 if (atomic_read(&ddb_entry->retry_relogin_timer) !=
2099 INVALID_ENTRY) {
2100 if (atomic_read(&ddb_entry->retry_relogin_timer) ==
2101 0) {
2102 atomic_set(&ddb_entry->retry_relogin_timer,
2103 INVALID_ENTRY);
2104 set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
2105 set_bit(DF_RELOGIN, &ddb_entry->flags);
2106 DEBUG2(ql4_printk(KERN_INFO, ha,
2107 "%s: index [%d] login device\n",
2108 __func__, ddb_entry->fw_ddb_index));
2109 } else
2110 atomic_dec(&ddb_entry->retry_relogin_timer);
2111 }
2112 }
2113
2114 /* Wait for relogin to timeout */
2115 if (atomic_read(&ddb_entry->relogin_timer) &&
2116 (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
2117 /*
2118 * If the relogin times out and the device is
2119 * still NOT ONLINE then try and relogin again.
2120 */
2121 if (!iscsi_is_session_online(cls_sess)) {
2122 /* Reset retry relogin timer */
2123 atomic_inc(&ddb_entry->relogin_retry_count);
2124 DEBUG2(ql4_printk(KERN_INFO, ha,
2125 "%s: index[%d] relogin timed out-retrying"
2126 " relogin (%d), retry (%d)\n", __func__,
2127 ddb_entry->fw_ddb_index,
2128 atomic_read(&ddb_entry->relogin_retry_count),
2129 ddb_entry->default_time2wait + 4));
2130 set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
2131 atomic_set(&ddb_entry->retry_relogin_timer,
2132 ddb_entry->default_time2wait + 4);
2133 }
2134 }
2135}
2136
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302137/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002138 * qla4xxx_timer - checks every second for work to do.
2139 * @ha: Pointer to host adapter structure.
2140 **/
2141static void qla4xxx_timer(struct scsi_qla_host *ha)
2142{
David Somayajuluafaf5a22006-09-19 10:28:00 -07002143 int start_dpc = 0;
Lalit Chandivade2232be02010-07-30 14:38:47 +05302144 uint16_t w;
2145
Mike Christie13483732011-12-01 21:38:41 -06002146 iscsi_host_for_each_session(ha->host, qla4xxx_check_relogin_flash_ddb);
2147
Lalit Chandivade2232be02010-07-30 14:38:47 +05302148 /* If we are in the middle of AER/EEH processing
2149 * skip any processing and reschedule the timer
2150 */
2151 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
2152 mod_timer(&ha->timer, jiffies + HZ);
2153 return;
2154 }
2155
2156 /* Hardware read to trigger an EEH error during mailbox waits. */
2157 if (!pci_channel_offline(ha->pdev))
2158 pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002159
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302160 if (is_qla8022(ha)) {
2161 qla4_8xxx_watchdog(ha);
2162 }
2163
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302164 if (!is_qla8022(ha)) {
2165 /* Check for heartbeat interval. */
2166 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
2167 ha->heartbeat_interval != 0) {
2168 ha->seconds_since_last_heartbeat++;
2169 if (ha->seconds_since_last_heartbeat >
2170 ha->heartbeat_interval + 2)
2171 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2172 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002173 }
2174
David Somayajuluafaf5a22006-09-19 10:28:00 -07002175 /* Wakeup the dpc routine for this adapter, if needed. */
Lalit Chandivade1b468072011-05-17 23:17:09 -07002176 if (start_dpc ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07002177 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
2178 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
2179 test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302180 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07002181 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
2182 test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05302183 test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302184 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
2185 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
Lalit Chandivade1b468072011-05-17 23:17:09 -07002186 test_bit(DPC_AEN, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002187 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
2188 " - dpc flags = 0x%lx\n",
2189 ha->host_no, __func__, ha->dpc_flags));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302190 qla4xxx_wake_dpc(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002191 }
2192
2193 /* Reschedule timer thread to call us back in one second */
2194 mod_timer(&ha->timer, jiffies + HZ);
2195
2196 DEBUG2(ha->seconds_since_last_intr++);
2197}
2198
2199/**
2200 * qla4xxx_cmd_wait - waits for all outstanding commands to complete
2201 * @ha: Pointer to host adapter structure.
2202 *
2203 * This routine stalls the driver until all outstanding commands are returned.
2204 * Caller must release the Hardware Lock prior to calling this routine.
2205 **/
2206static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
2207{
2208 uint32_t index = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002209 unsigned long flags;
2210 struct scsi_cmnd *cmd;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002211
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302212 unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
2213
2214 DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
2215 "complete\n", WAIT_CMD_TOV));
2216
2217 while (!time_after_eq(jiffies, wtime)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002218 spin_lock_irqsave(&ha->hardware_lock, flags);
2219 /* Find a command that hasn't completed. */
2220 for (index = 0; index < ha->host->can_queue; index++) {
2221 cmd = scsi_host_find_tag(ha->host, index);
Mike Christiea1e00632010-10-26 05:45:30 -07002222 /*
2223 * We cannot just check if the index is valid,
2224 * becase if we are run from the scsi eh, then
2225 * the scsi/block layer is going to prevent
2226 * the tag from being released.
2227 */
2228 if (cmd != NULL && CMD_SP(cmd))
David Somayajuluafaf5a22006-09-19 10:28:00 -07002229 break;
2230 }
2231 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2232
2233 /* If No Commands are pending, wait is complete */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302234 if (index == ha->host->can_queue)
2235 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002236
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302237 msleep(1000);
2238 }
2239 /* If we timed out on waiting for commands to come back
2240 * return ERROR. */
2241 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002242}
2243
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302244int qla4xxx_hw_reset(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002245{
David Somayajuluafaf5a22006-09-19 10:28:00 -07002246 uint32_t ctrl_status;
David C Somayajulu477ffb92007-01-22 12:26:11 -08002247 unsigned long flags = 0;
2248
2249 DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -07002250
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302251 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
2252 return QLA_ERROR;
2253
David Somayajuluafaf5a22006-09-19 10:28:00 -07002254 spin_lock_irqsave(&ha->hardware_lock, flags);
2255
2256 /*
2257 * If the SCSI Reset Interrupt bit is set, clear it.
2258 * Otherwise, the Soft Reset won't work.
2259 */
2260 ctrl_status = readw(&ha->reg->ctrl_status);
2261 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
2262 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
2263
2264 /* Issue Soft Reset */
2265 writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
2266 readl(&ha->reg->ctrl_status);
2267
2268 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302269 return QLA_SUCCESS;
David C Somayajulu477ffb92007-01-22 12:26:11 -08002270}
2271
2272/**
2273 * qla4xxx_soft_reset - performs soft reset.
2274 * @ha: Pointer to host adapter structure.
2275 **/
2276int qla4xxx_soft_reset(struct scsi_qla_host *ha)
2277{
2278 uint32_t max_wait_time;
2279 unsigned long flags = 0;
Vikas Chaudharyf931c532010-10-06 22:48:07 -07002280 int status;
David C Somayajulu477ffb92007-01-22 12:26:11 -08002281 uint32_t ctrl_status;
2282
Vikas Chaudharyf931c532010-10-06 22:48:07 -07002283 status = qla4xxx_hw_reset(ha);
2284 if (status != QLA_SUCCESS)
2285 return status;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002286
Vikas Chaudharyf931c532010-10-06 22:48:07 -07002287 status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002288 /* Wait until the Network Reset Intr bit is cleared */
2289 max_wait_time = RESET_INTR_TOV;
2290 do {
2291 spin_lock_irqsave(&ha->hardware_lock, flags);
2292 ctrl_status = readw(&ha->reg->ctrl_status);
2293 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2294
2295 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
2296 break;
2297
2298 msleep(1000);
2299 } while ((--max_wait_time));
2300
2301 if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
2302 DEBUG2(printk(KERN_WARNING
2303 "scsi%ld: Network Reset Intr not cleared by "
2304 "Network function, clearing it now!\n",
2305 ha->host_no));
2306 spin_lock_irqsave(&ha->hardware_lock, flags);
2307 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
2308 readl(&ha->reg->ctrl_status);
2309 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2310 }
2311
2312 /* Wait until the firmware tells us the Soft Reset is done */
2313 max_wait_time = SOFT_RESET_TOV;
2314 do {
2315 spin_lock_irqsave(&ha->hardware_lock, flags);
2316 ctrl_status = readw(&ha->reg->ctrl_status);
2317 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2318
2319 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
2320 status = QLA_SUCCESS;
2321 break;
2322 }
2323
2324 msleep(1000);
2325 } while ((--max_wait_time));
2326
2327 /*
2328 * Also, make sure that the SCSI Reset Interrupt bit has been cleared
2329 * after the soft reset has taken place.
2330 */
2331 spin_lock_irqsave(&ha->hardware_lock, flags);
2332 ctrl_status = readw(&ha->reg->ctrl_status);
2333 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
2334 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
2335 readl(&ha->reg->ctrl_status);
2336 }
2337 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2338
2339 /* If soft reset fails then most probably the bios on other
2340 * function is also enabled.
2341 * Since the initialization is sequential the other fn
2342 * wont be able to acknowledge the soft reset.
2343 * Issue a force soft reset to workaround this scenario.
2344 */
2345 if (max_wait_time == 0) {
2346 /* Issue Force Soft Reset */
2347 spin_lock_irqsave(&ha->hardware_lock, flags);
2348 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
2349 readl(&ha->reg->ctrl_status);
2350 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2351 /* Wait until the firmware tells us the Soft Reset is done */
2352 max_wait_time = SOFT_RESET_TOV;
2353 do {
2354 spin_lock_irqsave(&ha->hardware_lock, flags);
2355 ctrl_status = readw(&ha->reg->ctrl_status);
2356 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2357
2358 if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
2359 status = QLA_SUCCESS;
2360 break;
2361 }
2362
2363 msleep(1000);
2364 } while ((--max_wait_time));
2365 }
2366
2367 return status;
2368}
2369
2370/**
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302371 * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
David Somayajuluafaf5a22006-09-19 10:28:00 -07002372 * @ha: Pointer to host adapter structure.
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302373 * @res: returned scsi status
David Somayajuluafaf5a22006-09-19 10:28:00 -07002374 *
2375 * This routine is called just prior to a HARD RESET to return all
2376 * outstanding commands back to the Operating System.
2377 * Caller should make sure that the following locks are released
2378 * before this calling routine: Hardware lock, and io_request_lock.
2379 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302380static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002381{
2382 struct srb *srb;
2383 int i;
2384 unsigned long flags;
2385
2386 spin_lock_irqsave(&ha->hardware_lock, flags);
2387 for (i = 0; i < ha->host->can_queue; i++) {
2388 srb = qla4xxx_del_from_active_array(ha, i);
2389 if (srb != NULL) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302390 srb->cmd->result = res;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302391 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002392 }
2393 }
2394 spin_unlock_irqrestore(&ha->hardware_lock, flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002395}
2396
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302397void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
2398{
2399 clear_bit(AF_ONLINE, &ha->flags);
2400
2401 /* Disable the board */
2402 ql4_printk(KERN_INFO, ha, "Disabling the board\n");
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302403
2404 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
2405 qla4xxx_mark_all_devices_missing(ha);
2406 clear_bit(AF_INIT_DONE, &ha->flags);
2407}
2408
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002409static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session)
2410{
2411 struct iscsi_session *sess;
2412 struct ddb_entry *ddb_entry;
2413
2414 sess = cls_session->dd_data;
2415 ddb_entry = sess->dd_data;
2416 ddb_entry->fw_ddb_device_state = DDB_DS_SESSION_FAILED;
Mike Christie13483732011-12-01 21:38:41 -06002417
2418 if (ddb_entry->ddb_type == FLASH_DDB)
2419 iscsi_block_session(ddb_entry->sess);
2420 else
2421 iscsi_session_failure(cls_session->dd_data,
2422 ISCSI_ERR_CONN_FAILED);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002423}
2424
David Somayajuluafaf5a22006-09-19 10:28:00 -07002425/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002426 * qla4xxx_recover_adapter - recovers adapter after a fatal error
2427 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07002428 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302429static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002430{
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302431 int status = QLA_ERROR;
2432 uint8_t reset_chip = 0;
Sarang Radke8e0f3a62011-12-01 22:42:09 -08002433 uint32_t dev_state;
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002434 unsigned long wait;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002435
2436 /* Stall incoming I/O until we are done */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302437 scsi_block_requests(ha->host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002438 clear_bit(AF_ONLINE, &ha->flags);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002439 clear_bit(AF_LINK_UP, &ha->flags);
Mike Christie50a29ae2008-03-04 13:26:53 -06002440
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302441 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -07002442
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302443 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002444
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002445 iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
2446
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302447 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
2448 reset_chip = 1;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002449
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302450 /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
2451 * do not reset adapter, jump to initialize_adapter */
2452 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
2453 status = QLA_SUCCESS;
2454 goto recover_ha_init_adapter;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002455 }
2456
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302457 /* For the ISP-82xx adapter, issue a stop_firmware if invoked
2458 * from eh_host_reset or ioctl module */
2459 if (is_qla8022(ha) && !reset_chip &&
2460 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
2461
2462 DEBUG2(ql4_printk(KERN_INFO, ha,
2463 "scsi%ld: %s - Performing stop_firmware...\n",
2464 ha->host_no, __func__));
2465 status = ha->isp_ops->reset_firmware(ha);
2466 if (status == QLA_SUCCESS) {
Nilesh Javali2bd1e2be2010-10-06 22:49:20 -07002467 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
2468 qla4xxx_cmd_wait(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302469 ha->isp_ops->disable_intrs(ha);
2470 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2471 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
2472 } else {
2473 /* If the stop_firmware fails then
2474 * reset the entire chip */
2475 reset_chip = 1;
2476 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2477 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2478 }
2479 }
2480
2481 /* Issue full chip reset if recovering from a catastrophic error,
2482 * or if stop_firmware fails for ISP-82xx.
2483 * This is the default case for ISP-4xxx */
2484 if (!is_qla8022(ha) || reset_chip) {
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002485 if (!is_qla8022(ha))
2486 goto chip_reset;
2487
2488 /* Check if 82XX firmware is alive or not
2489 * We may have arrived here from NEED_RESET
2490 * detection only */
2491 if (test_bit(AF_FW_RECOVERY, &ha->flags))
2492 goto chip_reset;
2493
2494 wait = jiffies + (FW_ALIVE_WAIT_TOV * HZ);
2495 while (time_before(jiffies, wait)) {
2496 if (qla4_8xxx_check_fw_alive(ha)) {
2497 qla4xxx_mailbox_premature_completion(ha);
2498 break;
2499 }
2500
2501 set_current_state(TASK_UNINTERRUPTIBLE);
2502 schedule_timeout(HZ);
2503 }
2504
Nilesh Javali2bd1e2be2010-10-06 22:49:20 -07002505 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
2506 qla4xxx_cmd_wait(ha);
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002507chip_reset:
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302508 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2509 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
2510 DEBUG2(ql4_printk(KERN_INFO, ha,
2511 "scsi%ld: %s - Performing chip reset..\n",
2512 ha->host_no, __func__));
2513 status = ha->isp_ops->reset_chip(ha);
2514 }
2515
2516 /* Flush any pending ddb changed AENs */
2517 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2518
2519recover_ha_init_adapter:
2520 /* Upon successful firmware/chip reset, re-initialize the adapter */
2521 if (status == QLA_SUCCESS) {
2522 /* For ISP-4xxx, force function 1 to always initialize
2523 * before function 3 to prevent both funcions from
2524 * stepping on top of the other */
2525 if (!is_qla8022(ha) && (ha->mac_index == 3))
2526 ssleep(6);
2527
2528 /* NOTE: AF_ONLINE flag set upon successful completion of
2529 * qla4xxx_initialize_adapter */
Mike Christie13483732011-12-01 21:38:41 -06002530 status = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302531 }
2532
2533 /* Retry failed adapter initialization, if necessary
2534 * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
2535 * case to prevent ping-pong resets between functions */
2536 if (!test_bit(AF_ONLINE, &ha->flags) &&
2537 !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002538 /* Adapter initialization failed, see if we can retry
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302539 * resetting the ha.
2540 * Since we don't want to block the DPC for too long
2541 * with multiple resets in the same thread,
2542 * utilize DPC to retry */
Sarang Radke8e0f3a62011-12-01 22:42:09 -08002543 if (is_qla8022(ha)) {
2544 qla4_8xxx_idc_lock(ha);
2545 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
2546 qla4_8xxx_idc_unlock(ha);
2547 if (dev_state == QLA82XX_DEV_FAILED) {
2548 ql4_printk(KERN_INFO, ha, "%s: don't retry "
2549 "recover adapter. H/W is in Failed "
2550 "state\n", __func__);
2551 qla4xxx_dead_adapter_cleanup(ha);
2552 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
2553 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
2554 clear_bit(DPC_RESET_HA_FW_CONTEXT,
2555 &ha->dpc_flags);
2556 status = QLA_ERROR;
2557
2558 goto exit_recover;
2559 }
2560 }
2561
David Somayajuluafaf5a22006-09-19 10:28:00 -07002562 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
2563 ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
2564 DEBUG2(printk("scsi%ld: recover adapter - retrying "
2565 "(%d) more times\n", ha->host_no,
2566 ha->retry_reset_ha_cnt));
2567 set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
2568 status = QLA_ERROR;
2569 } else {
2570 if (ha->retry_reset_ha_cnt > 0) {
2571 /* Schedule another Reset HA--DPC will retry */
2572 ha->retry_reset_ha_cnt--;
2573 DEBUG2(printk("scsi%ld: recover adapter - "
2574 "retry remaining %d\n",
2575 ha->host_no,
2576 ha->retry_reset_ha_cnt));
2577 status = QLA_ERROR;
2578 }
2579
2580 if (ha->retry_reset_ha_cnt == 0) {
2581 /* Recover adapter retries have been exhausted.
2582 * Adapter DEAD */
2583 DEBUG2(printk("scsi%ld: recover adapter "
2584 "failed - board disabled\n",
2585 ha->host_no));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302586 qla4xxx_dead_adapter_cleanup(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002587 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
2588 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302589 clear_bit(DPC_RESET_HA_FW_CONTEXT,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002590 &ha->dpc_flags);
2591 status = QLA_ERROR;
2592 }
2593 }
2594 } else {
2595 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302596 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002597 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
2598 }
2599
Sarang Radke8e0f3a62011-12-01 22:42:09 -08002600exit_recover:
David Somayajuluafaf5a22006-09-19 10:28:00 -07002601 ha->adapter_error_count++;
2602
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302603 if (test_bit(AF_ONLINE, &ha->flags))
2604 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002605
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302606 scsi_unblock_requests(ha->host);
2607
2608 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2609 DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002610 status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302611
David Somayajuluafaf5a22006-09-19 10:28:00 -07002612 return status;
2613}
2614
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002615static void qla4xxx_relogin_devices(struct iscsi_cls_session *cls_session)
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07002616{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002617 struct iscsi_session *sess;
2618 struct ddb_entry *ddb_entry;
2619 struct scsi_qla_host *ha;
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07002620
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002621 sess = cls_session->dd_data;
2622 ddb_entry = sess->dd_data;
2623 ha = ddb_entry->ha;
2624 if (!iscsi_is_session_online(cls_session)) {
2625 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
2626 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
2627 " unblock session\n", ha->host_no, __func__,
2628 ddb_entry->fw_ddb_index);
2629 iscsi_unblock_session(ddb_entry->sess);
2630 } else {
2631 /* Trigger relogin */
Mike Christie13483732011-12-01 21:38:41 -06002632 if (ddb_entry->ddb_type == FLASH_DDB) {
2633 if (!test_bit(DF_RELOGIN, &ddb_entry->flags))
2634 qla4xxx_arm_relogin_timer(ddb_entry);
2635 } else
2636 iscsi_session_failure(cls_session->dd_data,
2637 ISCSI_ERR_CONN_FAILED);
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07002638 }
2639 }
2640}
2641
Mike Christie13483732011-12-01 21:38:41 -06002642int qla4xxx_unblock_flash_ddb(struct iscsi_cls_session *cls_session)
2643{
2644 struct iscsi_session *sess;
2645 struct ddb_entry *ddb_entry;
2646 struct scsi_qla_host *ha;
2647
2648 sess = cls_session->dd_data;
2649 ddb_entry = sess->dd_data;
2650 ha = ddb_entry->ha;
2651 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
2652 " unblock session\n", ha->host_no, __func__,
2653 ddb_entry->fw_ddb_index);
2654
2655 iscsi_unblock_session(ddb_entry->sess);
2656
2657 /* Start scan target */
2658 if (test_bit(AF_ONLINE, &ha->flags)) {
2659 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
2660 " start scan\n", ha->host_no, __func__,
2661 ddb_entry->fw_ddb_index);
2662 scsi_queue_work(ha->host, &ddb_entry->sess->scan_work);
2663 }
2664 return QLA_SUCCESS;
2665}
2666
2667int qla4xxx_unblock_ddb(struct iscsi_cls_session *cls_session)
2668{
2669 struct iscsi_session *sess;
2670 struct ddb_entry *ddb_entry;
2671 struct scsi_qla_host *ha;
2672
2673 sess = cls_session->dd_data;
2674 ddb_entry = sess->dd_data;
2675 ha = ddb_entry->ha;
2676 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
2677 " unblock user space session\n", ha->host_no, __func__,
2678 ddb_entry->fw_ddb_index);
2679 iscsi_conn_start(ddb_entry->conn);
2680 iscsi_conn_login_event(ddb_entry->conn,
2681 ISCSI_CONN_STATE_LOGGED_IN);
2682
2683 return QLA_SUCCESS;
2684}
2685
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002686static void qla4xxx_relogin_all_devices(struct scsi_qla_host *ha)
2687{
2688 iscsi_host_for_each_session(ha->host, qla4xxx_relogin_devices);
2689}
2690
Mike Christie13483732011-12-01 21:38:41 -06002691static void qla4xxx_relogin_flash_ddb(struct iscsi_cls_session *cls_sess)
2692{
2693 uint16_t relogin_timer;
2694 struct iscsi_session *sess;
2695 struct ddb_entry *ddb_entry;
2696 struct scsi_qla_host *ha;
2697
2698 sess = cls_sess->dd_data;
2699 ddb_entry = sess->dd_data;
2700 ha = ddb_entry->ha;
2701
2702 relogin_timer = max(ddb_entry->default_relogin_timeout,
2703 (uint16_t)RELOGIN_TOV);
2704 atomic_set(&ddb_entry->relogin_timer, relogin_timer);
2705
2706 DEBUG2(ql4_printk(KERN_INFO, ha,
2707 "scsi%ld: Relogin index [%d]. TOV=%d\n", ha->host_no,
2708 ddb_entry->fw_ddb_index, relogin_timer));
2709
2710 qla4xxx_login_flash_ddb(cls_sess);
2711}
2712
2713static void qla4xxx_dpc_relogin(struct iscsi_cls_session *cls_sess)
2714{
2715 struct iscsi_session *sess;
2716 struct ddb_entry *ddb_entry;
2717 struct scsi_qla_host *ha;
2718
2719 sess = cls_sess->dd_data;
2720 ddb_entry = sess->dd_data;
2721 ha = ddb_entry->ha;
2722
2723 if (!(ddb_entry->ddb_type == FLASH_DDB))
2724 return;
2725
2726 if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
2727 !iscsi_is_session_online(cls_sess)) {
2728 DEBUG2(ql4_printk(KERN_INFO, ha,
2729 "relogin issued\n"));
2730 qla4xxx_relogin_flash_ddb(cls_sess);
2731 }
2732}
2733
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302734void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
2735{
Lalit Chandivade1b468072011-05-17 23:17:09 -07002736 if (ha->dpc_thread)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302737 queue_work(ha->dpc_thread, &ha->dpc_work);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302738}
2739
David Somayajuluafaf5a22006-09-19 10:28:00 -07002740/**
2741 * qla4xxx_do_dpc - dpc routine
2742 * @data: in our case pointer to adapter structure
2743 *
2744 * This routine is a task that is schedule by the interrupt handler
2745 * to perform the background processing for interrupts. We put it
2746 * on a task queue that is consumed whenever the scheduler runs; that's
2747 * so you can do anything (i.e. put the process to sleep etc). In fact,
2748 * the mid-level tries to sleep when it reaches the driver threshold
2749 * "host->can_queue". This can cause a panic if we were in our interrupt code.
2750 **/
David Howellsc4028952006-11-22 14:57:56 +00002751static void qla4xxx_do_dpc(struct work_struct *work)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002752{
David Howellsc4028952006-11-22 14:57:56 +00002753 struct scsi_qla_host *ha =
2754 container_of(work, struct scsi_qla_host, dpc_work);
David C Somayajulu477ffb92007-01-22 12:26:11 -08002755 int status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002756
David C Somayajuluf26b9042006-11-15 16:41:09 -08002757 DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302758 "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
2759 ha->host_no, __func__, ha->flags, ha->dpc_flags))
David Somayajuluafaf5a22006-09-19 10:28:00 -07002760
2761 /* Initialization not yet finished. Don't do anything yet. */
2762 if (!test_bit(AF_INIT_DONE, &ha->flags))
Lalit Chandivade1b468072011-05-17 23:17:09 -07002763 return;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002764
Lalit Chandivade2232be02010-07-30 14:38:47 +05302765 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
2766 DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
2767 ha->host_no, __func__, ha->flags));
Lalit Chandivade1b468072011-05-17 23:17:09 -07002768 return;
Lalit Chandivade2232be02010-07-30 14:38:47 +05302769 }
2770
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302771 if (is_qla8022(ha)) {
2772 if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
2773 qla4_8xxx_idc_lock(ha);
2774 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2775 QLA82XX_DEV_FAILED);
2776 qla4_8xxx_idc_unlock(ha);
2777 ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
2778 qla4_8xxx_device_state_handler(ha);
2779 }
2780 if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
2781 qla4_8xxx_need_qsnt_handler(ha);
2782 }
2783 }
2784
2785 if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
2786 (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07002787 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302788 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
2789 if (ql4xdontresethba) {
2790 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
2791 ha->host_no, __func__));
2792 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
2793 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
2794 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2795 goto dpc_post_reset_ha;
2796 }
2797 if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
2798 test_bit(DPC_RESET_HA, &ha->dpc_flags))
2799 qla4xxx_recover_adapter(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002800
David C Somayajulu477ffb92007-01-22 12:26:11 -08002801 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002802 uint8_t wait_time = RESET_INTR_TOV;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002803
David Somayajuluafaf5a22006-09-19 10:28:00 -07002804 while ((readw(&ha->reg->ctrl_status) &
2805 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
2806 if (--wait_time == 0)
2807 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002808 msleep(1000);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002809 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002810 if (wait_time == 0)
2811 DEBUG2(printk("scsi%ld: %s: SR|FSR "
2812 "bit not cleared-- resetting\n",
2813 ha->host_no, __func__));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302814 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
David C Somayajulu477ffb92007-01-22 12:26:11 -08002815 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
2816 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302817 status = qla4xxx_recover_adapter(ha);
David C Somayajulu477ffb92007-01-22 12:26:11 -08002818 }
2819 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
2820 if (status == QLA_SUCCESS)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302821 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002822 }
2823 }
2824
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302825dpc_post_reset_ha:
David Somayajuluafaf5a22006-09-19 10:28:00 -07002826 /* ---- process AEN? --- */
2827 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
2828 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
2829
2830 /* ---- Get DHCP IP Address? --- */
2831 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
2832 qla4xxx_get_dhcp_ip_address(ha);
2833
Mike Christie13483732011-12-01 21:38:41 -06002834 /* ---- relogin device? --- */
2835 if (adapter_up(ha) &&
2836 test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
2837 iscsi_host_for_each_session(ha->host, qla4xxx_dpc_relogin);
2838 }
2839
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05302840 /* ---- link change? --- */
2841 if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
2842 if (!test_bit(AF_LINK_UP, &ha->flags)) {
2843 /* ---- link down? --- */
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07002844 qla4xxx_mark_all_devices_missing(ha);
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05302845 } else {
2846 /* ---- link up? --- *
2847 * F/W will auto login to all devices ONLY ONCE after
2848 * link up during driver initialization and runtime
2849 * fatal error recovery. Therefore, the driver must
2850 * manually relogin to devices when recovering from
2851 * connection failures, logouts, expired KATO, etc. */
Mike Christie13483732011-12-01 21:38:41 -06002852 if (test_and_clear_bit(AF_BUILD_DDB_LIST, &ha->flags)) {
2853 qla4xxx_build_ddb_list(ha, ha->is_reset);
2854 iscsi_host_for_each_session(ha->host,
2855 qla4xxx_login_flash_ddb);
2856 } else
2857 qla4xxx_relogin_all_devices(ha);
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05302858 }
2859 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002860}
2861
2862/**
2863 * qla4xxx_free_adapter - release the adapter
2864 * @ha: pointer to adapter structure
2865 **/
2866static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
2867{
Sarang Radke8a288962011-12-06 02:34:10 -08002868 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002869
2870 if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
2871 /* Turn-off interrupts on the card. */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302872 ha->isp_ops->disable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002873 }
2874
David Somayajuluafaf5a22006-09-19 10:28:00 -07002875 /* Remove timer thread, if present */
2876 if (ha->timer_active)
2877 qla4xxx_stop_timer(ha);
2878
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302879 /* Kill the kernel thread for this host */
2880 if (ha->dpc_thread)
2881 destroy_workqueue(ha->dpc_thread);
2882
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002883 /* Kill the kernel thread for this host */
2884 if (ha->task_wq)
2885 destroy_workqueue(ha->task_wq);
2886
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302887 /* Put firmware in known state */
2888 ha->isp_ops->reset_firmware(ha);
2889
2890 if (is_qla8022(ha)) {
2891 qla4_8xxx_idc_lock(ha);
2892 qla4_8xxx_clear_drv_active(ha);
2893 qla4_8xxx_idc_unlock(ha);
2894 }
2895
David Somayajuluafaf5a22006-09-19 10:28:00 -07002896 /* Detach interrupts */
2897 if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302898 qla4xxx_free_irqs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002899
David C Somayajulubee4fe82007-05-23 18:03:32 -07002900 /* free extra memory */
2901 qla4xxx_mem_free(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302902}
David C Somayajulubee4fe82007-05-23 18:03:32 -07002903
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302904int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
2905{
2906 int status = 0;
2907 uint8_t revision_id;
2908 unsigned long mem_base, mem_len, db_base, db_len;
2909 struct pci_dev *pdev = ha->pdev;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002910
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302911 status = pci_request_regions(pdev, DRIVER_NAME);
2912 if (status) {
2913 printk(KERN_WARNING
2914 "scsi(%ld) Failed to reserve PIO regions (%s) "
2915 "status=%d\n", ha->host_no, pci_name(pdev), status);
2916 goto iospace_error_exit;
2917 }
2918
2919 pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
2920 DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
2921 __func__, revision_id));
2922 ha->revision_id = revision_id;
2923
2924 /* remap phys address */
2925 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
2926 mem_len = pci_resource_len(pdev, 0);
2927 DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
2928 __func__, mem_base, mem_len));
2929
2930 /* mapping of pcibase pointer */
2931 ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
2932 if (!ha->nx_pcibase) {
2933 printk(KERN_ERR
2934 "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
2935 pci_release_regions(ha->pdev);
2936 goto iospace_error_exit;
2937 }
2938
2939 /* Mapping of IO base pointer, door bell read and write pointer */
2940
2941 /* mapping of IO base pointer */
2942 ha->qla4_8xxx_reg =
2943 (struct device_reg_82xx __iomem *)((uint8_t *)ha->nx_pcibase +
2944 0xbc000 + (ha->pdev->devfn << 11));
2945
2946 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
2947 db_len = pci_resource_len(pdev, 4);
2948
Shyam Sundar2657c802010-10-06 22:50:29 -07002949 ha->nx_db_wr_ptr = (ha->pdev->devfn == 4 ? QLA82XX_CAM_RAM_DB1 :
2950 QLA82XX_CAM_RAM_DB2);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302951
Shyam Sundar2657c802010-10-06 22:50:29 -07002952 return 0;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302953iospace_error_exit:
2954 return -ENOMEM;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002955}
2956
2957/***
2958 * qla4xxx_iospace_config - maps registers
2959 * @ha: pointer to adapter structure
2960 *
2961 * This routines maps HBA's registers from the pci address space
2962 * into the kernel virtual address space for memory mapped i/o.
2963 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302964int qla4xxx_iospace_config(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002965{
2966 unsigned long pio, pio_len, pio_flags;
2967 unsigned long mmio, mmio_len, mmio_flags;
2968
2969 pio = pci_resource_start(ha->pdev, 0);
2970 pio_len = pci_resource_len(ha->pdev, 0);
2971 pio_flags = pci_resource_flags(ha->pdev, 0);
2972 if (pio_flags & IORESOURCE_IO) {
2973 if (pio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302974 ql4_printk(KERN_WARNING, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002975 "Invalid PCI I/O region size\n");
2976 pio = 0;
2977 }
2978 } else {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302979 ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002980 pio = 0;
2981 }
2982
2983 /* Use MMIO operations for all accesses. */
2984 mmio = pci_resource_start(ha->pdev, 1);
2985 mmio_len = pci_resource_len(ha->pdev, 1);
2986 mmio_flags = pci_resource_flags(ha->pdev, 1);
2987
2988 if (!(mmio_flags & IORESOURCE_MEM)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302989 ql4_printk(KERN_ERR, ha,
2990 "region #0 not an MMIO resource, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002991
2992 goto iospace_error_exit;
2993 }
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302994
David Somayajuluafaf5a22006-09-19 10:28:00 -07002995 if (mmio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302996 ql4_printk(KERN_ERR, ha,
2997 "Invalid PCI mem region size, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002998 goto iospace_error_exit;
2999 }
3000
3001 if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303002 ql4_printk(KERN_WARNING, ha,
3003 "Failed to reserve PIO/MMIO regions\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07003004
3005 goto iospace_error_exit;
3006 }
3007
3008 ha->pio_address = pio;
3009 ha->pio_length = pio_len;
3010 ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
3011 if (!ha->reg) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303012 ql4_printk(KERN_ERR, ha,
3013 "cannot remap MMIO, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07003014
3015 goto iospace_error_exit;
3016 }
3017
3018 return 0;
3019
3020iospace_error_exit:
3021 return -ENOMEM;
3022}
3023
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303024static struct isp_operations qla4xxx_isp_ops = {
3025 .iospace_config = qla4xxx_iospace_config,
3026 .pci_config = qla4xxx_pci_config,
3027 .disable_intrs = qla4xxx_disable_intrs,
3028 .enable_intrs = qla4xxx_enable_intrs,
3029 .start_firmware = qla4xxx_start_firmware,
3030 .intr_handler = qla4xxx_intr_handler,
3031 .interrupt_service_routine = qla4xxx_interrupt_service_routine,
3032 .reset_chip = qla4xxx_soft_reset,
3033 .reset_firmware = qla4xxx_hw_reset,
3034 .queue_iocb = qla4xxx_queue_iocb,
3035 .complete_iocb = qla4xxx_complete_iocb,
3036 .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
3037 .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
3038 .get_sys_info = qla4xxx_get_sys_info,
3039};
3040
3041static struct isp_operations qla4_8xxx_isp_ops = {
3042 .iospace_config = qla4_8xxx_iospace_config,
3043 .pci_config = qla4_8xxx_pci_config,
3044 .disable_intrs = qla4_8xxx_disable_intrs,
3045 .enable_intrs = qla4_8xxx_enable_intrs,
3046 .start_firmware = qla4_8xxx_load_risc,
3047 .intr_handler = qla4_8xxx_intr_handler,
3048 .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
3049 .reset_chip = qla4_8xxx_isp_reset,
3050 .reset_firmware = qla4_8xxx_stop_firmware,
3051 .queue_iocb = qla4_8xxx_queue_iocb,
3052 .complete_iocb = qla4_8xxx_complete_iocb,
3053 .rd_shdw_req_q_out = qla4_8xxx_rd_shdw_req_q_out,
3054 .rd_shdw_rsp_q_in = qla4_8xxx_rd_shdw_rsp_q_in,
3055 .get_sys_info = qla4_8xxx_get_sys_info,
3056};
3057
3058uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
3059{
3060 return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
3061}
3062
3063uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
3064{
3065 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
3066}
3067
3068uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
3069{
3070 return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
3071}
3072
3073uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
3074{
3075 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
3076}
3077
Manish Rangankar2a991c22011-07-25 13:48:55 -05003078static ssize_t qla4xxx_show_boot_eth_info(void *data, int type, char *buf)
3079{
3080 struct scsi_qla_host *ha = data;
3081 char *str = buf;
3082 int rc;
3083
3084 switch (type) {
3085 case ISCSI_BOOT_ETH_FLAGS:
3086 rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
3087 break;
3088 case ISCSI_BOOT_ETH_INDEX:
3089 rc = sprintf(str, "0\n");
3090 break;
3091 case ISCSI_BOOT_ETH_MAC:
3092 rc = sysfs_format_mac(str, ha->my_mac,
3093 MAC_ADDR_LEN);
3094 break;
3095 default:
3096 rc = -ENOSYS;
3097 break;
3098 }
3099 return rc;
3100}
3101
Al Viro587a1f12011-07-23 23:11:19 -04003102static umode_t qla4xxx_eth_get_attr_visibility(void *data, int type)
Manish Rangankar2a991c22011-07-25 13:48:55 -05003103{
3104 int rc;
3105
3106 switch (type) {
3107 case ISCSI_BOOT_ETH_FLAGS:
3108 case ISCSI_BOOT_ETH_MAC:
3109 case ISCSI_BOOT_ETH_INDEX:
3110 rc = S_IRUGO;
3111 break;
3112 default:
3113 rc = 0;
3114 break;
3115 }
3116 return rc;
3117}
3118
3119static ssize_t qla4xxx_show_boot_ini_info(void *data, int type, char *buf)
3120{
3121 struct scsi_qla_host *ha = data;
3122 char *str = buf;
3123 int rc;
3124
3125 switch (type) {
3126 case ISCSI_BOOT_INI_INITIATOR_NAME:
3127 rc = sprintf(str, "%s\n", ha->name_string);
3128 break;
3129 default:
3130 rc = -ENOSYS;
3131 break;
3132 }
3133 return rc;
3134}
3135
Al Viro587a1f12011-07-23 23:11:19 -04003136static umode_t qla4xxx_ini_get_attr_visibility(void *data, int type)
Manish Rangankar2a991c22011-07-25 13:48:55 -05003137{
3138 int rc;
3139
3140 switch (type) {
3141 case ISCSI_BOOT_INI_INITIATOR_NAME:
3142 rc = S_IRUGO;
3143 break;
3144 default:
3145 rc = 0;
3146 break;
3147 }
3148 return rc;
3149}
3150
3151static ssize_t
3152qla4xxx_show_boot_tgt_info(struct ql4_boot_session_info *boot_sess, int type,
3153 char *buf)
3154{
3155 struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
3156 char *str = buf;
3157 int rc;
3158
3159 switch (type) {
3160 case ISCSI_BOOT_TGT_NAME:
3161 rc = sprintf(buf, "%s\n", (char *)&boot_sess->target_name);
3162 break;
3163 case ISCSI_BOOT_TGT_IP_ADDR:
3164 if (boot_sess->conn_list[0].dest_ipaddr.ip_type == 0x1)
3165 rc = sprintf(buf, "%pI4\n",
3166 &boot_conn->dest_ipaddr.ip_address);
3167 else
3168 rc = sprintf(str, "%pI6\n",
3169 &boot_conn->dest_ipaddr.ip_address);
3170 break;
3171 case ISCSI_BOOT_TGT_PORT:
3172 rc = sprintf(str, "%d\n", boot_conn->dest_port);
3173 break;
3174 case ISCSI_BOOT_TGT_CHAP_NAME:
3175 rc = sprintf(str, "%.*s\n",
3176 boot_conn->chap.target_chap_name_length,
3177 (char *)&boot_conn->chap.target_chap_name);
3178 break;
3179 case ISCSI_BOOT_TGT_CHAP_SECRET:
3180 rc = sprintf(str, "%.*s\n",
3181 boot_conn->chap.target_secret_length,
3182 (char *)&boot_conn->chap.target_secret);
3183 break;
3184 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
3185 rc = sprintf(str, "%.*s\n",
3186 boot_conn->chap.intr_chap_name_length,
3187 (char *)&boot_conn->chap.intr_chap_name);
3188 break;
3189 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
3190 rc = sprintf(str, "%.*s\n",
3191 boot_conn->chap.intr_secret_length,
3192 (char *)&boot_conn->chap.intr_secret);
3193 break;
3194 case ISCSI_BOOT_TGT_FLAGS:
3195 rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
3196 break;
3197 case ISCSI_BOOT_TGT_NIC_ASSOC:
3198 rc = sprintf(str, "0\n");
3199 break;
3200 default:
3201 rc = -ENOSYS;
3202 break;
3203 }
3204 return rc;
3205}
3206
3207static ssize_t qla4xxx_show_boot_tgt_pri_info(void *data, int type, char *buf)
3208{
3209 struct scsi_qla_host *ha = data;
3210 struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_pri_sess);
3211
3212 return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
3213}
3214
3215static ssize_t qla4xxx_show_boot_tgt_sec_info(void *data, int type, char *buf)
3216{
3217 struct scsi_qla_host *ha = data;
3218 struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_sec_sess);
3219
3220 return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
3221}
3222
Al Viro587a1f12011-07-23 23:11:19 -04003223static umode_t qla4xxx_tgt_get_attr_visibility(void *data, int type)
Manish Rangankar2a991c22011-07-25 13:48:55 -05003224{
3225 int rc;
3226
3227 switch (type) {
3228 case ISCSI_BOOT_TGT_NAME:
3229 case ISCSI_BOOT_TGT_IP_ADDR:
3230 case ISCSI_BOOT_TGT_PORT:
3231 case ISCSI_BOOT_TGT_CHAP_NAME:
3232 case ISCSI_BOOT_TGT_CHAP_SECRET:
3233 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
3234 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
3235 case ISCSI_BOOT_TGT_NIC_ASSOC:
3236 case ISCSI_BOOT_TGT_FLAGS:
3237 rc = S_IRUGO;
3238 break;
3239 default:
3240 rc = 0;
3241 break;
3242 }
3243 return rc;
3244}
3245
3246static void qla4xxx_boot_release(void *data)
3247{
3248 struct scsi_qla_host *ha = data;
3249
3250 scsi_host_put(ha->host);
3251}
3252
3253static int get_fw_boot_info(struct scsi_qla_host *ha, uint16_t ddb_index[])
3254{
3255 dma_addr_t buf_dma;
3256 uint32_t addr, pri_addr, sec_addr;
3257 uint32_t offset;
3258 uint16_t func_num;
3259 uint8_t val;
3260 uint8_t *buf = NULL;
3261 size_t size = 13 * sizeof(uint8_t);
3262 int ret = QLA_SUCCESS;
3263
3264 func_num = PCI_FUNC(ha->pdev->devfn);
3265
Manish Rangankar0d5b36b2011-10-07 16:55:51 -07003266 ql4_printk(KERN_INFO, ha, "%s: Get FW boot info for 0x%x func %d\n",
3267 __func__, ha->pdev->device, func_num);
Manish Rangankar2a991c22011-07-25 13:48:55 -05003268
Manish Rangankar0d5b36b2011-10-07 16:55:51 -07003269 if (is_qla40XX(ha)) {
Manish Rangankar2a991c22011-07-25 13:48:55 -05003270 if (func_num == 1) {
3271 addr = NVRAM_PORT0_BOOT_MODE;
3272 pri_addr = NVRAM_PORT0_BOOT_PRI_TGT;
3273 sec_addr = NVRAM_PORT0_BOOT_SEC_TGT;
3274 } else if (func_num == 3) {
3275 addr = NVRAM_PORT1_BOOT_MODE;
3276 pri_addr = NVRAM_PORT1_BOOT_PRI_TGT;
3277 sec_addr = NVRAM_PORT1_BOOT_SEC_TGT;
3278 } else {
3279 ret = QLA_ERROR;
3280 goto exit_boot_info;
3281 }
3282
3283 /* Check Boot Mode */
3284 val = rd_nvram_byte(ha, addr);
3285 if (!(val & 0x07)) {
3286 DEBUG2(ql4_printk(KERN_ERR, ha,
3287 "%s: Failed Boot options : 0x%x\n",
3288 __func__, val));
3289 ret = QLA_ERROR;
3290 goto exit_boot_info;
3291 }
3292
3293 /* get primary valid target index */
3294 val = rd_nvram_byte(ha, pri_addr);
3295 if (val & BIT_7)
3296 ddb_index[0] = (val & 0x7f);
Manish Rangankar2a991c22011-07-25 13:48:55 -05003297
3298 /* get secondary valid target index */
3299 val = rd_nvram_byte(ha, sec_addr);
3300 if (val & BIT_7)
3301 ddb_index[1] = (val & 0x7f);
Manish Rangankar2a991c22011-07-25 13:48:55 -05003302
3303 } else if (is_qla8022(ha)) {
3304 buf = dma_alloc_coherent(&ha->pdev->dev, size,
3305 &buf_dma, GFP_KERNEL);
3306 if (!buf) {
3307 DEBUG2(ql4_printk(KERN_ERR, ha,
3308 "%s: Unable to allocate dma buffer\n",
3309 __func__));
3310 ret = QLA_ERROR;
3311 goto exit_boot_info;
3312 }
3313
3314 if (ha->port_num == 0)
3315 offset = BOOT_PARAM_OFFSET_PORT0;
3316 else if (ha->port_num == 1)
3317 offset = BOOT_PARAM_OFFSET_PORT1;
3318 else {
3319 ret = QLA_ERROR;
3320 goto exit_boot_info_free;
3321 }
3322 addr = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_iscsi_param * 4) +
3323 offset;
3324 if (qla4xxx_get_flash(ha, buf_dma, addr,
3325 13 * sizeof(uint8_t)) != QLA_SUCCESS) {
3326 DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: Get Flash"
3327 "failed\n", ha->host_no, __func__));
3328 ret = QLA_ERROR;
3329 goto exit_boot_info_free;
3330 }
3331 /* Check Boot Mode */
3332 if (!(buf[1] & 0x07)) {
3333 DEBUG2(ql4_printk(KERN_INFO, ha,
3334 "Failed: Boot options : 0x%x\n",
3335 buf[1]));
3336 ret = QLA_ERROR;
3337 goto exit_boot_info_free;
3338 }
3339
3340 /* get primary valid target index */
3341 if (buf[2] & BIT_7)
3342 ddb_index[0] = buf[2] & 0x7f;
Manish Rangankar2a991c22011-07-25 13:48:55 -05003343
3344 /* get secondary valid target index */
3345 if (buf[11] & BIT_7)
3346 ddb_index[1] = buf[11] & 0x7f;
Manish Rangankar2a991c22011-07-25 13:48:55 -05003347 } else {
3348 ret = QLA_ERROR;
3349 goto exit_boot_info;
3350 }
3351
3352 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Primary target ID %d, Secondary"
3353 " target ID %d\n", __func__, ddb_index[0],
3354 ddb_index[1]));
3355
Mike Christie13483732011-12-01 21:38:41 -06003356 ha->pri_ddb_idx = ddb_index[0];
3357 ha->sec_ddb_idx = ddb_index[1];
3358
Manish Rangankar2a991c22011-07-25 13:48:55 -05003359exit_boot_info_free:
3360 dma_free_coherent(&ha->pdev->dev, size, buf, buf_dma);
3361exit_boot_info:
3362 return ret;
3363}
3364
Lalit Chandivade28deb452011-10-07 16:55:44 -07003365/**
3366 * qla4xxx_get_bidi_chap - Get a BIDI CHAP user and password
3367 * @ha: pointer to adapter structure
3368 * @username: CHAP username to be returned
3369 * @password: CHAP password to be returned
3370 *
3371 * If a boot entry has BIDI CHAP enabled then we need to set the BIDI CHAP
3372 * user and password in the sysfs entry in /sys/firmware/iscsi_boot#/.
3373 * So from the CHAP cache find the first BIDI CHAP entry and set it
3374 * to the boot record in sysfs.
3375 **/
3376static int qla4xxx_get_bidi_chap(struct scsi_qla_host *ha, char *username,
3377 char *password)
3378{
3379 int i, ret = -EINVAL;
3380 int max_chap_entries = 0;
3381 struct ql4_chap_table *chap_table;
3382
3383 if (is_qla8022(ha))
3384 max_chap_entries = (ha->hw.flt_chap_size / 2) /
3385 sizeof(struct ql4_chap_table);
3386 else
3387 max_chap_entries = MAX_CHAP_ENTRIES_40XX;
3388
3389 if (!ha->chap_list) {
3390 ql4_printk(KERN_ERR, ha, "Do not have CHAP table cache\n");
3391 return ret;
3392 }
3393
3394 mutex_lock(&ha->chap_sem);
3395 for (i = 0; i < max_chap_entries; i++) {
3396 chap_table = (struct ql4_chap_table *)ha->chap_list + i;
3397 if (chap_table->cookie !=
3398 __constant_cpu_to_le16(CHAP_VALID_COOKIE)) {
3399 continue;
3400 }
3401
3402 if (chap_table->flags & BIT_7) /* local */
3403 continue;
3404
3405 if (!(chap_table->flags & BIT_6)) /* Not BIDI */
3406 continue;
3407
3408 strncpy(password, chap_table->secret, QL4_CHAP_MAX_SECRET_LEN);
3409 strncpy(username, chap_table->name, QL4_CHAP_MAX_NAME_LEN);
3410 ret = 0;
3411 break;
3412 }
3413 mutex_unlock(&ha->chap_sem);
3414
3415 return ret;
3416}
3417
3418
Manish Rangankar2a991c22011-07-25 13:48:55 -05003419static int qla4xxx_get_boot_target(struct scsi_qla_host *ha,
3420 struct ql4_boot_session_info *boot_sess,
3421 uint16_t ddb_index)
3422{
3423 struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
3424 struct dev_db_entry *fw_ddb_entry;
3425 dma_addr_t fw_ddb_entry_dma;
3426 uint16_t idx;
3427 uint16_t options;
3428 int ret = QLA_SUCCESS;
3429
3430 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3431 &fw_ddb_entry_dma, GFP_KERNEL);
3432 if (!fw_ddb_entry) {
3433 DEBUG2(ql4_printk(KERN_ERR, ha,
3434 "%s: Unable to allocate dma buffer.\n",
3435 __func__));
3436 ret = QLA_ERROR;
3437 return ret;
3438 }
3439
3440 if (qla4xxx_bootdb_by_index(ha, fw_ddb_entry,
3441 fw_ddb_entry_dma, ddb_index)) {
3442 DEBUG2(ql4_printk(KERN_ERR, ha,
3443 "%s: Flash DDB read Failed\n", __func__));
3444 ret = QLA_ERROR;
3445 goto exit_boot_target;
3446 }
3447
3448 /* Update target name and IP from DDB */
3449 memcpy(boot_sess->target_name, fw_ddb_entry->iscsi_name,
3450 min(sizeof(boot_sess->target_name),
3451 sizeof(fw_ddb_entry->iscsi_name)));
3452
3453 options = le16_to_cpu(fw_ddb_entry->options);
3454 if (options & DDB_OPT_IPV6_DEVICE) {
3455 memcpy(&boot_conn->dest_ipaddr.ip_address,
3456 &fw_ddb_entry->ip_addr[0], IPv6_ADDR_LEN);
3457 } else {
3458 boot_conn->dest_ipaddr.ip_type = 0x1;
3459 memcpy(&boot_conn->dest_ipaddr.ip_address,
3460 &fw_ddb_entry->ip_addr[0], IP_ADDR_LEN);
3461 }
3462
3463 boot_conn->dest_port = le16_to_cpu(fw_ddb_entry->port);
3464
3465 /* update chap information */
3466 idx = __le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
3467
3468 if (BIT_7 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
3469
3470 DEBUG2(ql4_printk(KERN_INFO, ha, "Setting chap\n"));
3471
3472 ret = qla4xxx_get_chap(ha, (char *)&boot_conn->chap.
3473 target_chap_name,
3474 (char *)&boot_conn->chap.target_secret,
3475 idx);
3476 if (ret) {
3477 ql4_printk(KERN_ERR, ha, "Failed to set chap\n");
3478 ret = QLA_ERROR;
3479 goto exit_boot_target;
3480 }
3481
3482 boot_conn->chap.target_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
3483 boot_conn->chap.target_secret_length = QL4_CHAP_MAX_SECRET_LEN;
3484 }
3485
3486 if (BIT_4 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
3487
3488 DEBUG2(ql4_printk(KERN_INFO, ha, "Setting BIDI chap\n"));
3489
Lalit Chandivade28deb452011-10-07 16:55:44 -07003490 ret = qla4xxx_get_bidi_chap(ha,
3491 (char *)&boot_conn->chap.intr_chap_name,
3492 (char *)&boot_conn->chap.intr_secret);
3493
Manish Rangankar2a991c22011-07-25 13:48:55 -05003494 if (ret) {
3495 ql4_printk(KERN_ERR, ha, "Failed to set BIDI chap\n");
3496 ret = QLA_ERROR;
3497 goto exit_boot_target;
3498 }
3499
3500 boot_conn->chap.intr_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
3501 boot_conn->chap.intr_secret_length = QL4_CHAP_MAX_SECRET_LEN;
3502 }
3503
3504exit_boot_target:
3505 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3506 fw_ddb_entry, fw_ddb_entry_dma);
3507 return ret;
3508}
3509
3510static int qla4xxx_get_boot_info(struct scsi_qla_host *ha)
3511{
3512 uint16_t ddb_index[2];
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003513 int ret = QLA_ERROR;
3514 int rval;
Manish Rangankar2a991c22011-07-25 13:48:55 -05003515
3516 memset(ddb_index, 0, sizeof(ddb_index));
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003517 ddb_index[0] = 0xffff;
3518 ddb_index[1] = 0xffff;
Manish Rangankar2a991c22011-07-25 13:48:55 -05003519 ret = get_fw_boot_info(ha, ddb_index);
3520 if (ret != QLA_SUCCESS) {
3521 DEBUG2(ql4_printk(KERN_ERR, ha,
3522 "%s: Failed to set boot info.\n", __func__));
3523 return ret;
3524 }
3525
Mike Christie13483732011-12-01 21:38:41 -06003526 if (ql4xdisablesysfsboot)
3527 return QLA_SUCCESS;
3528
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003529 if (ddb_index[0] == 0xffff)
3530 goto sec_target;
3531
3532 rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_pri_sess),
Manish Rangankar2a991c22011-07-25 13:48:55 -05003533 ddb_index[0]);
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003534 if (rval != QLA_SUCCESS) {
Manish Rangankar2a991c22011-07-25 13:48:55 -05003535 DEBUG2(ql4_printk(KERN_ERR, ha, "%s: Failed to get "
3536 "primary target\n", __func__));
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003537 } else
3538 ret = QLA_SUCCESS;
Manish Rangankar2a991c22011-07-25 13:48:55 -05003539
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003540sec_target:
3541 if (ddb_index[1] == 0xffff)
3542 goto exit_get_boot_info;
3543
3544 rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_sec_sess),
Manish Rangankar2a991c22011-07-25 13:48:55 -05003545 ddb_index[1]);
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003546 if (rval != QLA_SUCCESS) {
Manish Rangankar2a991c22011-07-25 13:48:55 -05003547 DEBUG2(ql4_printk(KERN_ERR, ha, "%s: Failed to get "
3548 "secondary target\n", __func__));
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003549 } else
3550 ret = QLA_SUCCESS;
3551
3552exit_get_boot_info:
Manish Rangankar2a991c22011-07-25 13:48:55 -05003553 return ret;
3554}
3555
3556static int qla4xxx_setup_boot_info(struct scsi_qla_host *ha)
3557{
3558 struct iscsi_boot_kobj *boot_kobj;
3559
3560 if (qla4xxx_get_boot_info(ha) != QLA_SUCCESS)
Mike Christie13483732011-12-01 21:38:41 -06003561 return QLA_ERROR;
3562
3563 if (ql4xdisablesysfsboot) {
3564 ql4_printk(KERN_INFO, ha,
3565 "%s: syfsboot disabled - driver will trigger login"
3566 "and publish session for discovery .\n", __func__);
3567 return QLA_SUCCESS;
3568 }
3569
Manish Rangankar2a991c22011-07-25 13:48:55 -05003570
3571 ha->boot_kset = iscsi_boot_create_host_kset(ha->host->host_no);
3572 if (!ha->boot_kset)
3573 goto kset_free;
3574
3575 if (!scsi_host_get(ha->host))
3576 goto kset_free;
3577 boot_kobj = iscsi_boot_create_target(ha->boot_kset, 0, ha,
3578 qla4xxx_show_boot_tgt_pri_info,
3579 qla4xxx_tgt_get_attr_visibility,
3580 qla4xxx_boot_release);
3581 if (!boot_kobj)
3582 goto put_host;
3583
3584 if (!scsi_host_get(ha->host))
3585 goto kset_free;
3586 boot_kobj = iscsi_boot_create_target(ha->boot_kset, 1, ha,
3587 qla4xxx_show_boot_tgt_sec_info,
3588 qla4xxx_tgt_get_attr_visibility,
3589 qla4xxx_boot_release);
3590 if (!boot_kobj)
3591 goto put_host;
3592
3593 if (!scsi_host_get(ha->host))
3594 goto kset_free;
3595 boot_kobj = iscsi_boot_create_initiator(ha->boot_kset, 0, ha,
3596 qla4xxx_show_boot_ini_info,
3597 qla4xxx_ini_get_attr_visibility,
3598 qla4xxx_boot_release);
3599 if (!boot_kobj)
3600 goto put_host;
3601
3602 if (!scsi_host_get(ha->host))
3603 goto kset_free;
3604 boot_kobj = iscsi_boot_create_ethernet(ha->boot_kset, 0, ha,
3605 qla4xxx_show_boot_eth_info,
3606 qla4xxx_eth_get_attr_visibility,
3607 qla4xxx_boot_release);
3608 if (!boot_kobj)
3609 goto put_host;
3610
Mike Christie13483732011-12-01 21:38:41 -06003611 return QLA_SUCCESS;
Manish Rangankar2a991c22011-07-25 13:48:55 -05003612
3613put_host:
3614 scsi_host_put(ha->host);
3615kset_free:
3616 iscsi_boot_destroy_kset(ha->boot_kset);
3617 return -ENOMEM;
3618}
3619
Lalit Chandivade45494152011-10-07 16:55:42 -07003620
3621/**
3622 * qla4xxx_create chap_list - Create CHAP list from FLASH
3623 * @ha: pointer to adapter structure
3624 *
3625 * Read flash and make a list of CHAP entries, during login when a CHAP entry
3626 * is received, it will be checked in this list. If entry exist then the CHAP
3627 * entry index is set in the DDB. If CHAP entry does not exist in this list
3628 * then a new entry is added in FLASH in CHAP table and the index obtained is
3629 * used in the DDB.
3630 **/
3631static void qla4xxx_create_chap_list(struct scsi_qla_host *ha)
3632{
3633 int rval = 0;
3634 uint8_t *chap_flash_data = NULL;
3635 uint32_t offset;
3636 dma_addr_t chap_dma;
3637 uint32_t chap_size = 0;
3638
3639 if (is_qla40XX(ha))
3640 chap_size = MAX_CHAP_ENTRIES_40XX *
3641 sizeof(struct ql4_chap_table);
3642 else /* Single region contains CHAP info for both
3643 * ports which is divided into half for each port.
3644 */
3645 chap_size = ha->hw.flt_chap_size / 2;
3646
3647 chap_flash_data = dma_alloc_coherent(&ha->pdev->dev, chap_size,
3648 &chap_dma, GFP_KERNEL);
3649 if (!chap_flash_data) {
3650 ql4_printk(KERN_ERR, ha, "No memory for chap_flash_data\n");
3651 return;
3652 }
3653 if (is_qla40XX(ha))
3654 offset = FLASH_CHAP_OFFSET;
3655 else {
3656 offset = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_region_chap << 2);
3657 if (ha->port_num == 1)
3658 offset += chap_size;
3659 }
3660
3661 rval = qla4xxx_get_flash(ha, chap_dma, offset, chap_size);
3662 if (rval != QLA_SUCCESS)
3663 goto exit_chap_list;
3664
3665 if (ha->chap_list == NULL)
3666 ha->chap_list = vmalloc(chap_size);
3667 if (ha->chap_list == NULL) {
3668 ql4_printk(KERN_ERR, ha, "No memory for ha->chap_list\n");
3669 goto exit_chap_list;
3670 }
3671
3672 memcpy(ha->chap_list, chap_flash_data, chap_size);
3673
3674exit_chap_list:
3675 dma_free_coherent(&ha->pdev->dev, chap_size,
3676 chap_flash_data, chap_dma);
Lalit Chandivade45494152011-10-07 16:55:42 -07003677}
3678
Mike Christie13483732011-12-01 21:38:41 -06003679static void qla4xxx_get_param_ddb(struct ddb_entry *ddb_entry,
3680 struct ql4_tuple_ddb *tddb)
3681{
3682 struct scsi_qla_host *ha;
3683 struct iscsi_cls_session *cls_sess;
3684 struct iscsi_cls_conn *cls_conn;
3685 struct iscsi_session *sess;
3686 struct iscsi_conn *conn;
3687
3688 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
3689 ha = ddb_entry->ha;
3690 cls_sess = ddb_entry->sess;
3691 sess = cls_sess->dd_data;
3692 cls_conn = ddb_entry->conn;
3693 conn = cls_conn->dd_data;
3694
3695 tddb->tpgt = sess->tpgt;
3696 tddb->port = conn->persistent_port;
3697 strncpy(tddb->iscsi_name, sess->targetname, ISCSI_NAME_SIZE);
3698 strncpy(tddb->ip_addr, conn->persistent_address, DDB_IPADDR_LEN);
3699}
3700
3701static void qla4xxx_convert_param_ddb(struct dev_db_entry *fw_ddb_entry,
3702 struct ql4_tuple_ddb *tddb)
3703{
3704 uint16_t options = 0;
3705
3706 tddb->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
3707 memcpy(&tddb->iscsi_name[0], &fw_ddb_entry->iscsi_name[0],
3708 min(sizeof(tddb->iscsi_name), sizeof(fw_ddb_entry->iscsi_name)));
3709
3710 options = le16_to_cpu(fw_ddb_entry->options);
3711 if (options & DDB_OPT_IPV6_DEVICE)
3712 sprintf(tddb->ip_addr, "%pI6", fw_ddb_entry->ip_addr);
3713 else
3714 sprintf(tddb->ip_addr, "%pI4", fw_ddb_entry->ip_addr);
3715
3716 tddb->port = le16_to_cpu(fw_ddb_entry->port);
3717}
3718
3719static int qla4xxx_compare_tuple_ddb(struct scsi_qla_host *ha,
3720 struct ql4_tuple_ddb *old_tddb,
3721 struct ql4_tuple_ddb *new_tddb)
3722{
3723 if (strcmp(old_tddb->iscsi_name, new_tddb->iscsi_name))
3724 return QLA_ERROR;
3725
3726 if (strcmp(old_tddb->ip_addr, new_tddb->ip_addr))
3727 return QLA_ERROR;
3728
3729 if (old_tddb->port != new_tddb->port)
3730 return QLA_ERROR;
3731
3732 DEBUG2(ql4_printk(KERN_INFO, ha,
3733 "Match Found, fw[%d,%d,%s,%s], [%d,%d,%s,%s]",
3734 old_tddb->port, old_tddb->tpgt, old_tddb->ip_addr,
3735 old_tddb->iscsi_name, new_tddb->port, new_tddb->tpgt,
3736 new_tddb->ip_addr, new_tddb->iscsi_name));
3737
3738 return QLA_SUCCESS;
3739}
3740
3741static int qla4xxx_is_session_exists(struct scsi_qla_host *ha,
3742 struct dev_db_entry *fw_ddb_entry)
3743{
3744 struct ddb_entry *ddb_entry;
3745 struct ql4_tuple_ddb *fw_tddb = NULL;
3746 struct ql4_tuple_ddb *tmp_tddb = NULL;
3747 int idx;
3748 int ret = QLA_ERROR;
3749
3750 fw_tddb = vzalloc(sizeof(*fw_tddb));
3751 if (!fw_tddb) {
3752 DEBUG2(ql4_printk(KERN_WARNING, ha,
3753 "Memory Allocation failed.\n"));
3754 ret = QLA_SUCCESS;
3755 goto exit_check;
3756 }
3757
3758 tmp_tddb = vzalloc(sizeof(*tmp_tddb));
3759 if (!tmp_tddb) {
3760 DEBUG2(ql4_printk(KERN_WARNING, ha,
3761 "Memory Allocation failed.\n"));
3762 ret = QLA_SUCCESS;
3763 goto exit_check;
3764 }
3765
3766 qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb);
3767
3768 for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
3769 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
3770 if (ddb_entry == NULL)
3771 continue;
3772
3773 qla4xxx_get_param_ddb(ddb_entry, tmp_tddb);
3774 if (!qla4xxx_compare_tuple_ddb(ha, fw_tddb, tmp_tddb)) {
3775 ret = QLA_SUCCESS; /* found */
3776 goto exit_check;
3777 }
3778 }
3779
3780exit_check:
3781 if (fw_tddb)
3782 vfree(fw_tddb);
3783 if (tmp_tddb)
3784 vfree(tmp_tddb);
3785 return ret;
3786}
3787
3788static int qla4xxx_is_flash_ddb_exists(struct scsi_qla_host *ha,
3789 struct list_head *list_nt,
3790 struct dev_db_entry *fw_ddb_entry)
3791{
3792 struct qla_ddb_index *nt_ddb_idx, *nt_ddb_idx_tmp;
3793 struct ql4_tuple_ddb *fw_tddb = NULL;
3794 struct ql4_tuple_ddb *tmp_tddb = NULL;
3795 int ret = QLA_ERROR;
3796
3797 fw_tddb = vzalloc(sizeof(*fw_tddb));
3798 if (!fw_tddb) {
3799 DEBUG2(ql4_printk(KERN_WARNING, ha,
3800 "Memory Allocation failed.\n"));
3801 ret = QLA_SUCCESS;
3802 goto exit_check;
3803 }
3804
3805 tmp_tddb = vzalloc(sizeof(*tmp_tddb));
3806 if (!tmp_tddb) {
3807 DEBUG2(ql4_printk(KERN_WARNING, ha,
3808 "Memory Allocation failed.\n"));
3809 ret = QLA_SUCCESS;
3810 goto exit_check;
3811 }
3812
3813 qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb);
3814
3815 list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
3816 qla4xxx_convert_param_ddb(&nt_ddb_idx->fw_ddb, tmp_tddb);
3817 if (!qla4xxx_compare_tuple_ddb(ha, fw_tddb, tmp_tddb)) {
3818 ret = QLA_SUCCESS; /* found */
3819 goto exit_check;
3820 }
3821 }
3822
3823exit_check:
3824 if (fw_tddb)
3825 vfree(fw_tddb);
3826 if (tmp_tddb)
3827 vfree(tmp_tddb);
3828 return ret;
3829}
3830
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08003831static void qla4xxx_free_ddb_list(struct list_head *list_ddb)
Mike Christie13483732011-12-01 21:38:41 -06003832{
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08003833 struct qla_ddb_index *ddb_idx, *ddb_idx_tmp;
Mike Christie13483732011-12-01 21:38:41 -06003834
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08003835 list_for_each_entry_safe(ddb_idx, ddb_idx_tmp, list_ddb, list) {
3836 list_del_init(&ddb_idx->list);
3837 vfree(ddb_idx);
Mike Christie13483732011-12-01 21:38:41 -06003838 }
Mike Christie13483732011-12-01 21:38:41 -06003839}
3840
3841static struct iscsi_endpoint *qla4xxx_get_ep_fwdb(struct scsi_qla_host *ha,
3842 struct dev_db_entry *fw_ddb_entry)
3843{
3844 struct iscsi_endpoint *ep;
3845 struct sockaddr_in *addr;
3846 struct sockaddr_in6 *addr6;
3847 struct sockaddr *dst_addr;
3848 char *ip;
3849
3850 /* TODO: need to destroy on unload iscsi_endpoint*/
3851 dst_addr = vmalloc(sizeof(*dst_addr));
3852 if (!dst_addr)
3853 return NULL;
3854
3855 if (fw_ddb_entry->options & DDB_OPT_IPV6_DEVICE) {
3856 dst_addr->sa_family = AF_INET6;
3857 addr6 = (struct sockaddr_in6 *)dst_addr;
3858 ip = (char *)&addr6->sin6_addr;
3859 memcpy(ip, fw_ddb_entry->ip_addr, IPv6_ADDR_LEN);
3860 addr6->sin6_port = htons(le16_to_cpu(fw_ddb_entry->port));
3861
3862 } else {
3863 dst_addr->sa_family = AF_INET;
3864 addr = (struct sockaddr_in *)dst_addr;
3865 ip = (char *)&addr->sin_addr;
3866 memcpy(ip, fw_ddb_entry->ip_addr, IP_ADDR_LEN);
3867 addr->sin_port = htons(le16_to_cpu(fw_ddb_entry->port));
3868 }
3869
3870 ep = qla4xxx_ep_connect(ha->host, dst_addr, 0);
3871 vfree(dst_addr);
3872 return ep;
3873}
3874
3875static int qla4xxx_verify_boot_idx(struct scsi_qla_host *ha, uint16_t idx)
3876{
3877 if (ql4xdisablesysfsboot)
3878 return QLA_SUCCESS;
3879 if (idx == ha->pri_ddb_idx || idx == ha->sec_ddb_idx)
3880 return QLA_ERROR;
3881 return QLA_SUCCESS;
3882}
3883
3884static void qla4xxx_setup_flash_ddb_entry(struct scsi_qla_host *ha,
3885 struct ddb_entry *ddb_entry)
3886{
Nilesh Javalic28eaac2011-12-18 21:40:44 -08003887 uint16_t def_timeout;
3888
Mike Christie13483732011-12-01 21:38:41 -06003889 ddb_entry->ddb_type = FLASH_DDB;
3890 ddb_entry->fw_ddb_index = INVALID_ENTRY;
3891 ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
3892 ddb_entry->ha = ha;
3893 ddb_entry->unblock_sess = qla4xxx_unblock_flash_ddb;
3894 ddb_entry->ddb_change = qla4xxx_flash_ddb_change;
3895
3896 atomic_set(&ddb_entry->retry_relogin_timer, INVALID_ENTRY);
3897 atomic_set(&ddb_entry->relogin_timer, 0);
3898 atomic_set(&ddb_entry->relogin_retry_count, 0);
Nilesh Javalic28eaac2011-12-18 21:40:44 -08003899 def_timeout = le16_to_cpu(ddb_entry->fw_ddb_entry.def_timeout);
Mike Christie13483732011-12-01 21:38:41 -06003900 ddb_entry->default_relogin_timeout =
Nilesh Javalic28eaac2011-12-18 21:40:44 -08003901 (def_timeout > LOGIN_TOV) && (def_timeout < LOGIN_TOV * 10) ?
3902 def_timeout : LOGIN_TOV;
Mike Christie13483732011-12-01 21:38:41 -06003903 ddb_entry->default_time2wait =
3904 le16_to_cpu(ddb_entry->fw_ddb_entry.iscsi_def_time2wait);
3905}
3906
3907static void qla4xxx_wait_for_ip_configuration(struct scsi_qla_host *ha)
3908{
3909 uint32_t idx = 0;
3910 uint32_t ip_idx[IP_ADDR_COUNT] = {0, 1, 2, 3}; /* 4 IP interfaces */
3911 uint32_t sts[MBOX_REG_COUNT];
3912 uint32_t ip_state;
3913 unsigned long wtime;
3914 int ret;
3915
3916 wtime = jiffies + (HZ * IP_CONFIG_TOV);
3917 do {
3918 for (idx = 0; idx < IP_ADDR_COUNT; idx++) {
3919 if (ip_idx[idx] == -1)
3920 continue;
3921
3922 ret = qla4xxx_get_ip_state(ha, 0, ip_idx[idx], sts);
3923
3924 if (ret == QLA_ERROR) {
3925 ip_idx[idx] = -1;
3926 continue;
3927 }
3928
3929 ip_state = (sts[1] & IP_STATE_MASK) >> IP_STATE_SHIFT;
3930
3931 DEBUG2(ql4_printk(KERN_INFO, ha,
3932 "Waiting for IP state for idx = %d, state = 0x%x\n",
3933 ip_idx[idx], ip_state));
3934 if (ip_state == IP_ADDRSTATE_UNCONFIGURED ||
3935 ip_state == IP_ADDRSTATE_INVALID ||
3936 ip_state == IP_ADDRSTATE_PREFERRED ||
3937 ip_state == IP_ADDRSTATE_DEPRICATED ||
3938 ip_state == IP_ADDRSTATE_DISABLING)
3939 ip_idx[idx] = -1;
Mike Christie13483732011-12-01 21:38:41 -06003940 }
3941
3942 /* Break if all IP states checked */
3943 if ((ip_idx[0] == -1) &&
3944 (ip_idx[1] == -1) &&
3945 (ip_idx[2] == -1) &&
3946 (ip_idx[3] == -1))
3947 break;
3948 schedule_timeout_uninterruptible(HZ);
3949 } while (time_after(wtime, jiffies));
3950}
3951
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08003952static void qla4xxx_build_st_list(struct scsi_qla_host *ha,
3953 struct list_head *list_st)
Mike Christie13483732011-12-01 21:38:41 -06003954{
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08003955 struct qla_ddb_index *st_ddb_idx;
Mike Christie13483732011-12-01 21:38:41 -06003956 int max_ddbs;
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08003957 int fw_idx_size;
3958 struct dev_db_entry *fw_ddb_entry;
3959 dma_addr_t fw_ddb_dma;
Mike Christie13483732011-12-01 21:38:41 -06003960 int ret;
3961 uint32_t idx = 0, next_idx = 0;
3962 uint32_t state = 0, conn_err = 0;
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08003963 uint16_t conn_id = 0;
Mike Christie13483732011-12-01 21:38:41 -06003964
3965 fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
3966 &fw_ddb_dma);
3967 if (fw_ddb_entry == NULL) {
3968 DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08003969 goto exit_st_list;
Mike Christie13483732011-12-01 21:38:41 -06003970 }
3971
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08003972 max_ddbs = is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
3973 MAX_DEV_DB_ENTRIES;
Mike Christie13483732011-12-01 21:38:41 -06003974 fw_idx_size = sizeof(struct qla_ddb_index);
3975
3976 for (idx = 0; idx < max_ddbs; idx = next_idx) {
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08003977 ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry, fw_ddb_dma,
3978 NULL, &next_idx, &state,
3979 &conn_err, NULL, &conn_id);
Mike Christie13483732011-12-01 21:38:41 -06003980 if (ret == QLA_ERROR)
3981 break;
3982
3983 if (qla4xxx_verify_boot_idx(ha, idx) != QLA_SUCCESS)
3984 goto continue_next_st;
3985
3986 /* Check if ST, add to the list_st */
3987 if (strlen((char *) fw_ddb_entry->iscsi_name) != 0)
3988 goto continue_next_st;
3989
3990 st_ddb_idx = vzalloc(fw_idx_size);
3991 if (!st_ddb_idx)
3992 break;
3993
3994 st_ddb_idx->fw_ddb_idx = idx;
3995
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08003996 list_add_tail(&st_ddb_idx->list, list_st);
Mike Christie13483732011-12-01 21:38:41 -06003997continue_next_st:
3998 if (next_idx == 0)
3999 break;
4000 }
4001
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004002exit_st_list:
4003 if (fw_ddb_entry)
4004 dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
4005}
4006
4007/**
4008 * qla4xxx_remove_failed_ddb - Remove inactive or failed ddb from list
4009 * @ha: pointer to adapter structure
4010 * @list_ddb: List from which failed ddb to be removed
4011 *
4012 * Iterate over the list of DDBs and find and remove DDBs that are either in
4013 * no connection active state or failed state
4014 **/
4015static void qla4xxx_remove_failed_ddb(struct scsi_qla_host *ha,
4016 struct list_head *list_ddb)
4017{
4018 struct qla_ddb_index *ddb_idx, *ddb_idx_tmp;
4019 uint32_t next_idx = 0;
4020 uint32_t state = 0, conn_err = 0;
4021 int ret;
4022
4023 list_for_each_entry_safe(ddb_idx, ddb_idx_tmp, list_ddb, list) {
4024 ret = qla4xxx_get_fwddb_entry(ha, ddb_idx->fw_ddb_idx,
4025 NULL, 0, NULL, &next_idx, &state,
4026 &conn_err, NULL, NULL);
4027 if (ret == QLA_ERROR)
4028 continue;
4029
4030 if (state == DDB_DS_NO_CONNECTION_ACTIVE ||
4031 state == DDB_DS_SESSION_FAILED) {
4032 list_del_init(&ddb_idx->list);
4033 vfree(ddb_idx);
4034 }
4035 }
4036}
4037
4038static int qla4xxx_sess_conn_setup(struct scsi_qla_host *ha,
4039 struct dev_db_entry *fw_ddb_entry,
4040 int is_reset)
4041{
4042 struct iscsi_cls_session *cls_sess;
4043 struct iscsi_session *sess;
4044 struct iscsi_cls_conn *cls_conn;
4045 struct iscsi_endpoint *ep;
4046 uint16_t cmds_max = 32;
4047 uint16_t conn_id = 0;
4048 uint32_t initial_cmdsn = 0;
4049 int ret = QLA_SUCCESS;
4050
4051 struct ddb_entry *ddb_entry = NULL;
4052
4053 /* Create session object, with INVALID_ENTRY,
4054 * the targer_id would get set when we issue the login
4055 */
4056 cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport, ha->host,
4057 cmds_max, sizeof(struct ddb_entry),
4058 sizeof(struct ql4_task_data),
4059 initial_cmdsn, INVALID_ENTRY);
4060 if (!cls_sess) {
4061 ret = QLA_ERROR;
4062 goto exit_setup;
4063 }
4064
4065 /*
4066 * so calling module_put function to decrement the
4067 * reference count.
4068 **/
4069 module_put(qla4xxx_iscsi_transport.owner);
4070 sess = cls_sess->dd_data;
4071 ddb_entry = sess->dd_data;
4072 ddb_entry->sess = cls_sess;
4073
4074 cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
4075 memcpy(&ddb_entry->fw_ddb_entry, fw_ddb_entry,
4076 sizeof(struct dev_db_entry));
4077
4078 qla4xxx_setup_flash_ddb_entry(ha, ddb_entry);
4079
4080 cls_conn = iscsi_conn_setup(cls_sess, sizeof(struct qla_conn), conn_id);
4081
4082 if (!cls_conn) {
4083 ret = QLA_ERROR;
4084 goto exit_setup;
4085 }
4086
4087 ddb_entry->conn = cls_conn;
4088
4089 /* Setup ep, for displaying attributes in sysfs */
4090 ep = qla4xxx_get_ep_fwdb(ha, fw_ddb_entry);
4091 if (ep) {
4092 ep->conn = cls_conn;
4093 cls_conn->ep = ep;
4094 } else {
4095 DEBUG2(ql4_printk(KERN_ERR, ha, "Unable to get ep\n"));
4096 ret = QLA_ERROR;
4097 goto exit_setup;
4098 }
4099
4100 /* Update sess/conn params */
4101 qla4xxx_copy_fwddb_param(ha, fw_ddb_entry, cls_sess, cls_conn);
4102
4103 if (is_reset == RESET_ADAPTER) {
4104 iscsi_block_session(cls_sess);
4105 /* Use the relogin path to discover new devices
4106 * by short-circuting the logic of setting
4107 * timer to relogin - instead set the flags
4108 * to initiate login right away.
4109 */
4110 set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
4111 set_bit(DF_RELOGIN, &ddb_entry->flags);
4112 }
4113
4114exit_setup:
4115 return ret;
4116}
4117
4118static void qla4xxx_build_nt_list(struct scsi_qla_host *ha,
4119 struct list_head *list_nt, int is_reset)
4120{
4121 struct dev_db_entry *fw_ddb_entry;
4122 dma_addr_t fw_ddb_dma;
4123 int max_ddbs;
4124 int fw_idx_size;
4125 int ret;
4126 uint32_t idx = 0, next_idx = 0;
4127 uint32_t state = 0, conn_err = 0;
4128 uint16_t conn_id = 0;
4129 struct qla_ddb_index *nt_ddb_idx;
4130
4131 fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
4132 &fw_ddb_dma);
4133 if (fw_ddb_entry == NULL) {
4134 DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
4135 goto exit_nt_list;
4136 }
4137 max_ddbs = is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
4138 MAX_DEV_DB_ENTRIES;
4139 fw_idx_size = sizeof(struct qla_ddb_index);
4140
4141 for (idx = 0; idx < max_ddbs; idx = next_idx) {
4142 ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry, fw_ddb_dma,
4143 NULL, &next_idx, &state,
4144 &conn_err, NULL, &conn_id);
4145 if (ret == QLA_ERROR)
4146 break;
4147
4148 if (qla4xxx_verify_boot_idx(ha, idx) != QLA_SUCCESS)
4149 goto continue_next_nt;
4150
4151 /* Check if NT, then add to list it */
4152 if (strlen((char *) fw_ddb_entry->iscsi_name) == 0)
4153 goto continue_next_nt;
4154
4155 if (!(state == DDB_DS_NO_CONNECTION_ACTIVE ||
4156 state == DDB_DS_SESSION_FAILED))
4157 goto continue_next_nt;
4158
4159 DEBUG2(ql4_printk(KERN_INFO, ha,
4160 "Adding DDB to session = 0x%x\n", idx));
4161 if (is_reset == INIT_ADAPTER) {
4162 nt_ddb_idx = vmalloc(fw_idx_size);
4163 if (!nt_ddb_idx)
4164 break;
4165
4166 nt_ddb_idx->fw_ddb_idx = idx;
4167
4168 memcpy(&nt_ddb_idx->fw_ddb, fw_ddb_entry,
4169 sizeof(struct dev_db_entry));
4170
4171 if (qla4xxx_is_flash_ddb_exists(ha, list_nt,
4172 fw_ddb_entry) == QLA_SUCCESS) {
4173 vfree(nt_ddb_idx);
4174 goto continue_next_nt;
4175 }
4176 list_add_tail(&nt_ddb_idx->list, list_nt);
4177 } else if (is_reset == RESET_ADAPTER) {
4178 if (qla4xxx_is_session_exists(ha, fw_ddb_entry) ==
4179 QLA_SUCCESS)
4180 goto continue_next_nt;
4181 }
4182
4183 ret = qla4xxx_sess_conn_setup(ha, fw_ddb_entry, is_reset);
4184 if (ret == QLA_ERROR)
4185 goto exit_nt_list;
4186
4187continue_next_nt:
4188 if (next_idx == 0)
4189 break;
4190 }
4191
4192exit_nt_list:
4193 if (fw_ddb_entry)
4194 dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
4195}
4196
4197/**
4198 * qla4xxx_build_ddb_list - Build ddb list and setup sessions
4199 * @ha: pointer to adapter structure
4200 * @is_reset: Is this init path or reset path
4201 *
4202 * Create a list of sendtargets (st) from firmware DDBs, issue send targets
4203 * using connection open, then create the list of normal targets (nt)
4204 * from firmware DDBs. Based on the list of nt setup session and connection
4205 * objects.
4206 **/
4207void qla4xxx_build_ddb_list(struct scsi_qla_host *ha, int is_reset)
4208{
4209 uint16_t tmo = 0;
4210 struct list_head list_st, list_nt;
4211 struct qla_ddb_index *st_ddb_idx, *st_ddb_idx_tmp;
4212 unsigned long wtime;
4213
4214 if (!test_bit(AF_LINK_UP, &ha->flags)) {
4215 set_bit(AF_BUILD_DDB_LIST, &ha->flags);
4216 ha->is_reset = is_reset;
4217 return;
4218 }
4219
4220 INIT_LIST_HEAD(&list_st);
4221 INIT_LIST_HEAD(&list_nt);
4222
4223 qla4xxx_build_st_list(ha, &list_st);
4224
Mike Christie13483732011-12-01 21:38:41 -06004225 /* Before issuing conn open mbox, ensure all IPs states are configured
4226 * Note, conn open fails if IPs are not configured
4227 */
4228 qla4xxx_wait_for_ip_configuration(ha);
4229
4230 /* Go thru the STs and fire the sendtargets by issuing conn open mbx */
4231 list_for_each_entry_safe(st_ddb_idx, st_ddb_idx_tmp, &list_st, list) {
4232 qla4xxx_conn_open(ha, st_ddb_idx->fw_ddb_idx);
4233 }
4234
4235 /* Wait to ensure all sendtargets are done for min 12 sec wait */
Nilesh Javalic28eaac2011-12-18 21:40:44 -08004236 tmo = ((ha->def_timeout > LOGIN_TOV) &&
4237 (ha->def_timeout < LOGIN_TOV * 10) ?
4238 ha->def_timeout : LOGIN_TOV);
4239
Mike Christie13483732011-12-01 21:38:41 -06004240 DEBUG2(ql4_printk(KERN_INFO, ha,
4241 "Default time to wait for build ddb %d\n", tmo));
4242
4243 wtime = jiffies + (HZ * tmo);
4244 do {
Nilesh Javalif1f2e602011-12-16 01:58:57 -08004245 if (list_empty(&list_st))
4246 break;
4247
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004248 qla4xxx_remove_failed_ddb(ha, &list_st);
Mike Christie13483732011-12-01 21:38:41 -06004249 schedule_timeout_uninterruptible(HZ / 10);
4250 } while (time_after(wtime, jiffies));
4251
4252 /* Free up the sendtargets list */
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004253 qla4xxx_free_ddb_list(&list_st);
Mike Christie13483732011-12-01 21:38:41 -06004254
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004255 qla4xxx_build_nt_list(ha, &list_nt, is_reset);
Mike Christie13483732011-12-01 21:38:41 -06004256
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004257 qla4xxx_free_ddb_list(&list_nt);
Mike Christie13483732011-12-01 21:38:41 -06004258
4259 qla4xxx_free_ddb_index(ha);
4260}
4261
David Somayajuluafaf5a22006-09-19 10:28:00 -07004262/**
4263 * qla4xxx_probe_adapter - callback function to probe HBA
4264 * @pdev: pointer to pci_dev structure
4265 * @pci_device_id: pointer to pci_device entry
4266 *
4267 * This routine will probe for Qlogic 4xxx iSCSI host adapters.
4268 * It returns zero if successful. It also initializes all data necessary for
4269 * the driver.
4270 **/
4271static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
4272 const struct pci_device_id *ent)
4273{
4274 int ret = -ENODEV, status;
4275 struct Scsi_Host *host;
4276 struct scsi_qla_host *ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004277 uint8_t init_retry_count = 0;
4278 char buf[34];
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304279 struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
Prasanna Mumbaif9880e72011-03-21 03:34:26 -07004280 uint32_t dev_state;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004281
4282 if (pci_enable_device(pdev))
4283 return -1;
4284
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004285 host = iscsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha), 0);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004286 if (host == NULL) {
4287 printk(KERN_WARNING
4288 "qla4xxx: Couldn't allocate host from scsi layer!\n");
4289 goto probe_disable_device;
4290 }
4291
4292 /* Clear our data area */
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004293 ha = to_qla_host(host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004294 memset(ha, 0, sizeof(*ha));
4295
4296 /* Save the information from PCI BIOS. */
4297 ha->pdev = pdev;
4298 ha->host = host;
4299 ha->host_no = host->host_no;
4300
Lalit Chandivade2232be02010-07-30 14:38:47 +05304301 pci_enable_pcie_error_reporting(pdev);
4302
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304303 /* Setup Runtime configurable options */
4304 if (is_qla8022(ha)) {
4305 ha->isp_ops = &qla4_8xxx_isp_ops;
4306 rwlock_init(&ha->hw_lock);
4307 ha->qdr_sn_window = -1;
4308 ha->ddr_mn_window = -1;
4309 ha->curr_window = 255;
4310 ha->func_num = PCI_FUNC(ha->pdev->devfn);
4311 nx_legacy_intr = &legacy_intr[ha->func_num];
4312 ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
4313 ha->nx_legacy_intr.tgt_status_reg =
4314 nx_legacy_intr->tgt_status_reg;
4315 ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
4316 ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
4317 } else {
4318 ha->isp_ops = &qla4xxx_isp_ops;
4319 }
4320
Lalit Chandivade2232be02010-07-30 14:38:47 +05304321 /* Set EEH reset type to fundamental if required by hba */
4322 if (is_qla8022(ha))
4323 pdev->needs_freset = 1;
4324
David Somayajuluafaf5a22006-09-19 10:28:00 -07004325 /* Configure PCI I/O space. */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304326 ret = ha->isp_ops->iospace_config(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004327 if (ret)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304328 goto probe_failed_ioconfig;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004329
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304330 ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
David Somayajuluafaf5a22006-09-19 10:28:00 -07004331 pdev->device, pdev->irq, ha->reg);
4332
4333 qla4xxx_config_dma_addressing(ha);
4334
4335 /* Initialize lists and spinlocks. */
David Somayajuluafaf5a22006-09-19 10:28:00 -07004336 INIT_LIST_HEAD(&ha->free_srb_q);
4337
4338 mutex_init(&ha->mbox_sem);
Lalit Chandivade45494152011-10-07 16:55:42 -07004339 mutex_init(&ha->chap_sem);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304340 init_completion(&ha->mbx_intr_comp);
Vikas Chaudhary95d31262011-08-12 02:51:29 -07004341 init_completion(&ha->disable_acb_comp);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004342
4343 spin_lock_init(&ha->hardware_lock);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004344
4345 /* Allocate dma buffers */
4346 if (qla4xxx_mem_alloc(ha)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304347 ql4_printk(KERN_WARNING, ha,
4348 "[ERROR] Failed to allocate memory for adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07004349
4350 ret = -ENOMEM;
4351 goto probe_failed;
4352 }
4353
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004354 host->cmd_per_lun = 3;
4355 host->max_channel = 0;
4356 host->max_lun = MAX_LUNS - 1;
4357 host->max_id = MAX_TARGETS;
4358 host->max_cmd_len = IOCB_MAX_CDB_LEN;
4359 host->can_queue = MAX_SRBS ;
4360 host->transportt = qla4xxx_scsi_transport;
4361
4362 ret = scsi_init_shared_tag_map(host, MAX_SRBS);
4363 if (ret) {
4364 ql4_printk(KERN_WARNING, ha,
4365 "%s: scsi_init_shared_tag_map failed\n", __func__);
4366 goto probe_failed;
4367 }
4368
4369 pci_set_drvdata(pdev, ha);
4370
4371 ret = scsi_add_host(host, &pdev->dev);
4372 if (ret)
4373 goto probe_failed;
4374
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304375 if (is_qla8022(ha))
4376 (void) qla4_8xxx_get_flash_info(ha);
4377
David Somayajuluafaf5a22006-09-19 10:28:00 -07004378 /*
4379 * Initialize the Host adapter request/response queues and
4380 * firmware
4381 * NOTE: interrupts enabled upon successful completion
4382 */
Mike Christie13483732011-12-01 21:38:41 -06004383 status = qla4xxx_initialize_adapter(ha, INIT_ADAPTER);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304384 while ((!test_bit(AF_ONLINE, &ha->flags)) &&
4385 init_retry_count++ < MAX_INIT_RETRIES) {
Prasanna Mumbaif9880e72011-03-21 03:34:26 -07004386
4387 if (is_qla8022(ha)) {
4388 qla4_8xxx_idc_lock(ha);
4389 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
4390 qla4_8xxx_idc_unlock(ha);
4391 if (dev_state == QLA82XX_DEV_FAILED) {
4392 ql4_printk(KERN_WARNING, ha, "%s: don't retry "
4393 "initialize adapter. H/W is in failed state\n",
4394 __func__);
4395 break;
4396 }
4397 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07004398 DEBUG2(printk("scsi: %s: retrying adapter initialization "
4399 "(%d)\n", __func__, init_retry_count));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304400
4401 if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
4402 continue;
4403
Mike Christie13483732011-12-01 21:38:41 -06004404 status = qla4xxx_initialize_adapter(ha, INIT_ADAPTER);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004405 }
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304406
4407 if (!test_bit(AF_ONLINE, &ha->flags)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304408 ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07004409
Lalit Chandivadefe998522010-12-02 22:12:36 -08004410 if (is_qla8022(ha) && ql4xdontresethba) {
4411 /* Put the device in failed state. */
4412 DEBUG2(printk(KERN_ERR "HW STATE: FAILED\n"));
4413 qla4_8xxx_idc_lock(ha);
4414 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
4415 QLA82XX_DEV_FAILED);
4416 qla4_8xxx_idc_unlock(ha);
4417 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07004418 ret = -ENODEV;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004419 goto remove_host;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004420 }
4421
David Somayajuluafaf5a22006-09-19 10:28:00 -07004422 /* Startup the kernel thread for this host adapter. */
4423 DEBUG2(printk("scsi: %s: Starting kernel thread for "
4424 "qla4xxx_dpc\n", __func__));
4425 sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
4426 ha->dpc_thread = create_singlethread_workqueue(buf);
4427 if (!ha->dpc_thread) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304428 ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07004429 ret = -ENODEV;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004430 goto remove_host;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004431 }
David Howellsc4028952006-11-22 14:57:56 +00004432 INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004433
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004434 sprintf(buf, "qla4xxx_%lu_task", ha->host_no);
4435 ha->task_wq = alloc_workqueue(buf, WQ_MEM_RECLAIM, 1);
4436 if (!ha->task_wq) {
4437 ql4_printk(KERN_WARNING, ha, "Unable to start task thread!\n");
4438 ret = -ENODEV;
4439 goto remove_host;
4440 }
4441
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304442 /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
4443 * (which is called indirectly by qla4xxx_initialize_adapter),
4444 * so that irqs will be registered after crbinit but before
4445 * mbx_intr_enable.
4446 */
4447 if (!is_qla8022(ha)) {
4448 ret = qla4xxx_request_irqs(ha);
4449 if (ret) {
4450 ql4_printk(KERN_WARNING, ha, "Failed to reserve "
4451 "interrupt %d already in use.\n", pdev->irq);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004452 goto remove_host;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304453 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07004454 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07004455
Lalit Chandivade2232be02010-07-30 14:38:47 +05304456 pci_save_state(ha->pdev);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304457 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004458
4459 /* Start timer thread. */
4460 qla4xxx_start_timer(ha, qla4xxx_timer, 1);
4461
4462 set_bit(AF_INIT_DONE, &ha->flags);
4463
David Somayajuluafaf5a22006-09-19 10:28:00 -07004464 printk(KERN_INFO
4465 " QLogic iSCSI HBA Driver version: %s\n"
4466 " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
4467 qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
4468 ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
4469 ha->patch_number, ha->build_number);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05004470
Manish Rangankar2a991c22011-07-25 13:48:55 -05004471 if (qla4xxx_setup_boot_info(ha))
4472 ql4_printk(KERN_ERR, ha, "%s:ISCSI boot info setup failed\n",
4473 __func__);
4474
Mike Christie13483732011-12-01 21:38:41 -06004475 /* Perform the build ddb list and login to each */
4476 qla4xxx_build_ddb_list(ha, INIT_ADAPTER);
4477 iscsi_host_for_each_session(ha->host, qla4xxx_login_flash_ddb);
4478
4479 qla4xxx_create_chap_list(ha);
4480
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05004481 qla4xxx_create_ifaces(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004482 return 0;
4483
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004484remove_host:
4485 scsi_remove_host(ha->host);
4486
David Somayajuluafaf5a22006-09-19 10:28:00 -07004487probe_failed:
4488 qla4xxx_free_adapter(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304489
4490probe_failed_ioconfig:
Lalit Chandivade2232be02010-07-30 14:38:47 +05304491 pci_disable_pcie_error_reporting(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004492 scsi_host_put(ha->host);
4493
4494probe_disable_device:
4495 pci_disable_device(pdev);
4496
4497 return ret;
4498}
4499
4500/**
Karen Higgins7eece5a2011-03-21 03:34:29 -07004501 * qla4xxx_prevent_other_port_reinit - prevent other port from re-initialize
4502 * @ha: pointer to adapter structure
4503 *
4504 * Mark the other ISP-4xxx port to indicate that the driver is being removed,
4505 * so that the other port will not re-initialize while in the process of
4506 * removing the ha due to driver unload or hba hotplug.
4507 **/
4508static void qla4xxx_prevent_other_port_reinit(struct scsi_qla_host *ha)
4509{
4510 struct scsi_qla_host *other_ha = NULL;
4511 struct pci_dev *other_pdev = NULL;
4512 int fn = ISP4XXX_PCI_FN_2;
4513
4514 /*iscsi function numbers for ISP4xxx is 1 and 3*/
4515 if (PCI_FUNC(ha->pdev->devfn) & BIT_1)
4516 fn = ISP4XXX_PCI_FN_1;
4517
4518 other_pdev =
4519 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
4520 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
4521 fn));
4522
4523 /* Get other_ha if other_pdev is valid and state is enable*/
4524 if (other_pdev) {
4525 if (atomic_read(&other_pdev->enable_cnt)) {
4526 other_ha = pci_get_drvdata(other_pdev);
4527 if (other_ha) {
4528 set_bit(AF_HA_REMOVAL, &other_ha->flags);
4529 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: "
4530 "Prevent %s reinit\n", __func__,
4531 dev_name(&other_ha->pdev->dev)));
4532 }
4533 }
4534 pci_dev_put(other_pdev);
4535 }
4536}
4537
Mike Christie13483732011-12-01 21:38:41 -06004538static void qla4xxx_destroy_fw_ddb_session(struct scsi_qla_host *ha)
4539{
4540 struct ddb_entry *ddb_entry;
4541 int options;
4542 int idx;
4543
4544 for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
4545
4546 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
4547 if ((ddb_entry != NULL) &&
4548 (ddb_entry->ddb_type == FLASH_DDB)) {
4549
4550 options = LOGOUT_OPTION_CLOSE_SESSION;
4551 if (qla4xxx_session_logout_ddb(ha, ddb_entry, options)
4552 == QLA_ERROR)
4553 ql4_printk(KERN_ERR, ha, "%s: Logout failed\n",
4554 __func__);
4555
4556 qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
4557 /*
4558 * we have decremented the reference count of the driver
4559 * when we setup the session to have the driver unload
4560 * to be seamless without actually destroying the
4561 * session
4562 **/
4563 try_module_get(qla4xxx_iscsi_transport.owner);
4564 iscsi_destroy_endpoint(ddb_entry->conn->ep);
4565 qla4xxx_free_ddb(ha, ddb_entry);
4566 iscsi_session_teardown(ddb_entry->sess);
4567 }
4568 }
4569}
Karen Higgins7eece5a2011-03-21 03:34:29 -07004570/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07004571 * qla4xxx_remove_adapter - calback function to remove adapter.
4572 * @pci_dev: PCI device pointer
4573 **/
4574static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
4575{
4576 struct scsi_qla_host *ha;
4577
4578 ha = pci_get_drvdata(pdev);
4579
Karen Higgins7eece5a2011-03-21 03:34:29 -07004580 if (!is_qla8022(ha))
4581 qla4xxx_prevent_other_port_reinit(ha);
David C Somayajulubee4fe82007-05-23 18:03:32 -07004582
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05004583 /* destroy iface from sysfs */
4584 qla4xxx_destroy_ifaces(ha);
4585
Mike Christie13483732011-12-01 21:38:41 -06004586 if ((!ql4xdisablesysfsboot) && ha->boot_kset)
Manish Rangankar2a991c22011-07-25 13:48:55 -05004587 iscsi_boot_destroy_kset(ha->boot_kset);
4588
Mike Christie13483732011-12-01 21:38:41 -06004589 qla4xxx_destroy_fw_ddb_session(ha);
4590
David Somayajuluafaf5a22006-09-19 10:28:00 -07004591 scsi_remove_host(ha->host);
4592
4593 qla4xxx_free_adapter(ha);
4594
4595 scsi_host_put(ha->host);
4596
Lalit Chandivade2232be02010-07-30 14:38:47 +05304597 pci_disable_pcie_error_reporting(pdev);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304598 pci_disable_device(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004599 pci_set_drvdata(pdev, NULL);
4600}
4601
4602/**
4603 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
4604 * @ha: HA context
4605 *
4606 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
4607 * supported addressing method.
4608 */
Adrian Bunk47975472007-04-26 00:35:16 -07004609static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07004610{
4611 int retval;
4612
4613 /* Update our PCI device dma_mask for full 64 bit mask */
Yang Hongyang6a355282009-04-06 19:01:13 -07004614 if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
4615 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07004616 dev_dbg(&ha->pdev->dev,
4617 "Failed to set 64 bit PCI consistent mask; "
4618 "using 32 bit.\n");
4619 retval = pci_set_consistent_dma_mask(ha->pdev,
Yang Hongyang284901a2009-04-06 19:01:15 -07004620 DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07004621 }
4622 } else
Yang Hongyang284901a2009-04-06 19:01:15 -07004623 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07004624}
4625
4626static int qla4xxx_slave_alloc(struct scsi_device *sdev)
4627{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004628 struct iscsi_cls_session *cls_sess;
4629 struct iscsi_session *sess;
4630 struct ddb_entry *ddb;
Vikas Chaudhary8bb40332011-03-21 03:34:31 -07004631 int queue_depth = QL4_DEF_QDEPTH;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004632
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004633 cls_sess = starget_to_session(sdev->sdev_target);
4634 sess = cls_sess->dd_data;
4635 ddb = sess->dd_data;
4636
David Somayajuluafaf5a22006-09-19 10:28:00 -07004637 sdev->hostdata = ddb;
4638 sdev->tagged_supported = 1;
Vikas Chaudhary8bb40332011-03-21 03:34:31 -07004639
4640 if (ql4xmaxqdepth != 0 && ql4xmaxqdepth <= 0xffffU)
4641 queue_depth = ql4xmaxqdepth;
4642
4643 scsi_activate_tcq(sdev, queue_depth);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004644 return 0;
4645}
4646
4647static int qla4xxx_slave_configure(struct scsi_device *sdev)
4648{
4649 sdev->tagged_supported = 1;
4650 return 0;
4651}
4652
4653static void qla4xxx_slave_destroy(struct scsi_device *sdev)
4654{
4655 scsi_deactivate_tcq(sdev, 1);
4656}
4657
4658/**
4659 * qla4xxx_del_from_active_array - returns an active srb
4660 * @ha: Pointer to host adapter structure.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02004661 * @index: index into the active_array
David Somayajuluafaf5a22006-09-19 10:28:00 -07004662 *
4663 * This routine removes and returns the srb at the specified index
4664 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304665struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
4666 uint32_t index)
David Somayajuluafaf5a22006-09-19 10:28:00 -07004667{
4668 struct srb *srb = NULL;
Vikas Chaudhary53698872010-04-28 11:41:59 +05304669 struct scsi_cmnd *cmd = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004670
Vikas Chaudhary53698872010-04-28 11:41:59 +05304671 cmd = scsi_host_find_tag(ha->host, index);
4672 if (!cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07004673 return srb;
4674
Vikas Chaudhary53698872010-04-28 11:41:59 +05304675 srb = (struct srb *)CMD_SP(cmd);
4676 if (!srb)
David Somayajuluafaf5a22006-09-19 10:28:00 -07004677 return srb;
4678
4679 /* update counters */
4680 if (srb->flags & SRB_DMA_VALID) {
4681 ha->req_q_count += srb->iocb_cnt;
4682 ha->iocb_cnt -= srb->iocb_cnt;
4683 if (srb->cmd)
Vikas Chaudhary53698872010-04-28 11:41:59 +05304684 srb->cmd->host_scribble =
4685 (unsigned char *)(unsigned long) MAX_SRBS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004686 }
4687 return srb;
4688}
4689
4690/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07004691 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304692 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07004693 * @cmd: Scsi Command to wait on.
4694 *
4695 * This routine waits for the command to be returned by the Firmware
4696 * for some max time.
4697 **/
4698static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
4699 struct scsi_cmnd *cmd)
4700{
4701 int done = 0;
4702 struct srb *rp;
4703 uint32_t max_wait_time = EH_WAIT_CMD_TOV;
Lalit Chandivade2232be02010-07-30 14:38:47 +05304704 int ret = SUCCESS;
4705
4706 /* Dont wait on command if PCI error is being handled
4707 * by PCI AER driver
4708 */
4709 if (unlikely(pci_channel_offline(ha->pdev)) ||
4710 (test_bit(AF_EEH_BUSY, &ha->flags))) {
4711 ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
4712 ha->host_no, __func__);
4713 return ret;
4714 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07004715
4716 do {
4717 /* Checking to see if its returned to OS */
Vikas Chaudhary53698872010-04-28 11:41:59 +05304718 rp = (struct srb *) CMD_SP(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004719 if (rp == NULL) {
4720 done++;
4721 break;
4722 }
4723
4724 msleep(2000);
4725 } while (max_wait_time--);
4726
4727 return done;
4728}
4729
4730/**
4731 * qla4xxx_wait_for_hba_online - waits for HBA to come online
4732 * @ha: Pointer to host adapter structure
4733 **/
4734static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
4735{
4736 unsigned long wait_online;
4737
Vikas Chaudharyf581a3f2010-10-06 22:47:48 -07004738 wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004739 while (time_before(jiffies, wait_online)) {
4740
4741 if (adapter_up(ha))
4742 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004743
4744 msleep(2000);
4745 }
4746
4747 return QLA_ERROR;
4748}
4749
4750/**
Mike Christiece545032008-02-29 18:25:20 -06004751 * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02004752 * @ha: pointer to HBA
David Somayajuluafaf5a22006-09-19 10:28:00 -07004753 * @t: target id
4754 * @l: lun id
4755 *
4756 * This function waits for all outstanding commands to a lun to complete. It
4757 * returns 0 if all pending commands are returned and 1 otherwise.
4758 **/
Mike Christiece545032008-02-29 18:25:20 -06004759static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
4760 struct scsi_target *stgt,
4761 struct scsi_device *sdev)
David Somayajuluafaf5a22006-09-19 10:28:00 -07004762{
4763 int cnt;
4764 int status = 0;
4765 struct scsi_cmnd *cmd;
4766
4767 /*
Mike Christiece545032008-02-29 18:25:20 -06004768 * Waiting for all commands for the designated target or dev
4769 * in the active array
David Somayajuluafaf5a22006-09-19 10:28:00 -07004770 */
4771 for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
4772 cmd = scsi_host_find_tag(ha->host, cnt);
Mike Christiece545032008-02-29 18:25:20 -06004773 if (cmd && stgt == scsi_target(cmd->device) &&
4774 (!sdev || sdev == cmd->device)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07004775 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
4776 status++;
4777 break;
4778 }
4779 }
4780 }
4781 return status;
4782}
4783
4784/**
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304785 * qla4xxx_eh_abort - callback for abort task.
4786 * @cmd: Pointer to Linux's SCSI command structure
4787 *
4788 * This routine is called by the Linux OS to abort the specified
4789 * command.
4790 **/
4791static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
4792{
4793 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
4794 unsigned int id = cmd->device->id;
4795 unsigned int lun = cmd->device->lun;
Mike Christie92b3e5b2010-10-06 22:51:17 -07004796 unsigned long flags;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304797 struct srb *srb = NULL;
4798 int ret = SUCCESS;
4799 int wait = 0;
4800
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304801 ql4_printk(KERN_INFO, ha,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004802 "scsi%ld:%d:%d: Abort command issued cmd=%p\n",
4803 ha->host_no, id, lun, cmd);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304804
Mike Christie92b3e5b2010-10-06 22:51:17 -07004805 spin_lock_irqsave(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304806 srb = (struct srb *) CMD_SP(cmd);
Mike Christie92b3e5b2010-10-06 22:51:17 -07004807 if (!srb) {
4808 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304809 return SUCCESS;
Mike Christie92b3e5b2010-10-06 22:51:17 -07004810 }
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304811 kref_get(&srb->srb_ref);
Mike Christie92b3e5b2010-10-06 22:51:17 -07004812 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304813
4814 if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
4815 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
4816 ha->host_no, id, lun));
4817 ret = FAILED;
4818 } else {
4819 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
4820 ha->host_no, id, lun));
4821 wait = 1;
4822 }
4823
4824 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
4825
4826 /* Wait for command to complete */
4827 if (wait) {
4828 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
4829 DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
4830 ha->host_no, id, lun));
4831 ret = FAILED;
4832 }
4833 }
4834
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304835 ql4_printk(KERN_INFO, ha,
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304836 "scsi%ld:%d:%d: Abort command - %s\n",
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004837 ha->host_no, id, lun, (ret == SUCCESS) ? "succeeded" : "failed");
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304838
4839 return ret;
4840}
4841
4842/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07004843 * qla4xxx_eh_device_reset - callback for target reset.
4844 * @cmd: Pointer to Linux's SCSI command structure
4845 *
4846 * This routine is called by the Linux OS to reset all luns on the
4847 * specified target.
4848 **/
4849static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
4850{
4851 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
4852 struct ddb_entry *ddb_entry = cmd->device->hostdata;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004853 int ret = FAILED, stat;
4854
Karen Higgins612f73482009-07-15 15:03:01 -05004855 if (!ddb_entry)
David Somayajuluafaf5a22006-09-19 10:28:00 -07004856 return ret;
4857
Mike Christiec01be6d2010-07-22 16:59:49 +05304858 ret = iscsi_block_scsi_eh(cmd);
4859 if (ret)
4860 return ret;
4861 ret = FAILED;
4862
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304863 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07004864 "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
4865 cmd->device->channel, cmd->device->id, cmd->device->lun);
4866
4867 DEBUG2(printk(KERN_INFO
4868 "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
4869 "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
Jens Axboe242f9dc2008-09-14 05:55:09 -07004870 cmd, jiffies, cmd->request->timeout / HZ,
David Somayajuluafaf5a22006-09-19 10:28:00 -07004871 ha->dpc_flags, cmd->result, cmd->allowed));
4872
4873 /* FIXME: wait for hba to go online */
4874 stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
4875 if (stat != QLA_SUCCESS) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304876 ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004877 goto eh_dev_reset_done;
4878 }
4879
Mike Christiece545032008-02-29 18:25:20 -06004880 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
4881 cmd->device)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304882 ql4_printk(KERN_INFO, ha,
Mike Christiece545032008-02-29 18:25:20 -06004883 "DEVICE RESET FAILED - waiting for "
4884 "commands.\n");
4885 goto eh_dev_reset_done;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004886 }
4887
David C Somayajulu9d562912008-03-19 11:23:03 -07004888 /* Send marker. */
4889 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
4890 MM_LUN_RESET) != QLA_SUCCESS)
4891 goto eh_dev_reset_done;
4892
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304893 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07004894 "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
4895 ha->host_no, cmd->device->channel, cmd->device->id,
4896 cmd->device->lun);
4897
4898 ret = SUCCESS;
4899
4900eh_dev_reset_done:
4901
4902 return ret;
4903}
4904
4905/**
Mike Christiece545032008-02-29 18:25:20 -06004906 * qla4xxx_eh_target_reset - callback for target reset.
4907 * @cmd: Pointer to Linux's SCSI command structure
4908 *
4909 * This routine is called by the Linux OS to reset the target.
4910 **/
4911static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
4912{
4913 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
4914 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christiec01be6d2010-07-22 16:59:49 +05304915 int stat, ret;
Mike Christiece545032008-02-29 18:25:20 -06004916
4917 if (!ddb_entry)
4918 return FAILED;
4919
Mike Christiec01be6d2010-07-22 16:59:49 +05304920 ret = iscsi_block_scsi_eh(cmd);
4921 if (ret)
4922 return ret;
4923
Mike Christiece545032008-02-29 18:25:20 -06004924 starget_printk(KERN_INFO, scsi_target(cmd->device),
4925 "WARM TARGET RESET ISSUED.\n");
4926
4927 DEBUG2(printk(KERN_INFO
4928 "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
4929 "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
Jens Axboe242f9dc2008-09-14 05:55:09 -07004930 ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
Mike Christiece545032008-02-29 18:25:20 -06004931 ha->dpc_flags, cmd->result, cmd->allowed));
4932
4933 stat = qla4xxx_reset_target(ha, ddb_entry);
4934 if (stat != QLA_SUCCESS) {
4935 starget_printk(KERN_INFO, scsi_target(cmd->device),
4936 "WARM TARGET RESET FAILED.\n");
4937 return FAILED;
4938 }
4939
Mike Christiece545032008-02-29 18:25:20 -06004940 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
4941 NULL)) {
4942 starget_printk(KERN_INFO, scsi_target(cmd->device),
4943 "WARM TARGET DEVICE RESET FAILED - "
4944 "waiting for commands.\n");
4945 return FAILED;
4946 }
4947
David C Somayajulu9d562912008-03-19 11:23:03 -07004948 /* Send marker. */
4949 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
4950 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
4951 starget_printk(KERN_INFO, scsi_target(cmd->device),
4952 "WARM TARGET DEVICE RESET FAILED - "
4953 "marker iocb failed.\n");
4954 return FAILED;
4955 }
4956
Mike Christiece545032008-02-29 18:25:20 -06004957 starget_printk(KERN_INFO, scsi_target(cmd->device),
4958 "WARM TARGET RESET SUCCEEDED.\n");
4959 return SUCCESS;
4960}
4961
4962/**
Sarang Radke8a288962011-12-06 02:34:10 -08004963 * qla4xxx_is_eh_active - check if error handler is running
4964 * @shost: Pointer to SCSI Host struct
4965 *
4966 * This routine finds that if reset host is called in EH
4967 * scenario or from some application like sg_reset
4968 **/
4969static int qla4xxx_is_eh_active(struct Scsi_Host *shost)
4970{
4971 if (shost->shost_state == SHOST_RECOVERY)
4972 return 1;
4973 return 0;
4974}
4975
4976/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07004977 * qla4xxx_eh_host_reset - kernel callback
4978 * @cmd: Pointer to Linux's SCSI command structure
4979 *
4980 * This routine is invoked by the Linux kernel to perform fatal error
4981 * recovery on the specified adapter.
4982 **/
4983static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
4984{
4985 int return_status = FAILED;
4986 struct scsi_qla_host *ha;
4987
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004988 ha = to_qla_host(cmd->device->host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004989
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304990 if (ql4xdontresethba) {
4991 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
4992 ha->host_no, __func__));
Sarang Radke8a288962011-12-06 02:34:10 -08004993
4994 /* Clear outstanding srb in queues */
4995 if (qla4xxx_is_eh_active(cmd->device->host))
4996 qla4xxx_abort_active_cmds(ha, DID_ABORT << 16);
4997
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304998 return FAILED;
4999 }
5000
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05305001 ql4_printk(KERN_INFO, ha,
Karen Higginsdca05c42009-07-15 15:03:00 -05005002 "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
David Somayajuluafaf5a22006-09-19 10:28:00 -07005003 cmd->device->channel, cmd->device->id, cmd->device->lun);
5004
5005 if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
5006 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
5007 "DEAD.\n", ha->host_no, cmd->device->channel,
5008 __func__));
5009
5010 return FAILED;
5011 }
5012
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05305013 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
5014 if (is_qla8022(ha))
5015 set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
5016 else
5017 set_bit(DPC_RESET_HA, &ha->dpc_flags);
5018 }
Mike Christie50a29ae2008-03-04 13:26:53 -06005019
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05305020 if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
David Somayajuluafaf5a22006-09-19 10:28:00 -07005021 return_status = SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07005022
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05305023 ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005024 return_status == FAILED ? "FAILED" : "SUCCEEDED");
David Somayajuluafaf5a22006-09-19 10:28:00 -07005025
5026 return return_status;
5027}
5028
Vikas Chaudhary95d31262011-08-12 02:51:29 -07005029static int qla4xxx_context_reset(struct scsi_qla_host *ha)
5030{
5031 uint32_t mbox_cmd[MBOX_REG_COUNT];
5032 uint32_t mbox_sts[MBOX_REG_COUNT];
5033 struct addr_ctrl_blk_def *acb = NULL;
5034 uint32_t acb_len = sizeof(struct addr_ctrl_blk_def);
5035 int rval = QLA_SUCCESS;
5036 dma_addr_t acb_dma;
5037
5038 acb = dma_alloc_coherent(&ha->pdev->dev,
5039 sizeof(struct addr_ctrl_blk_def),
5040 &acb_dma, GFP_KERNEL);
5041 if (!acb) {
5042 ql4_printk(KERN_ERR, ha, "%s: Unable to alloc acb\n",
5043 __func__);
5044 rval = -ENOMEM;
5045 goto exit_port_reset;
5046 }
5047
5048 memset(acb, 0, acb_len);
5049
5050 rval = qla4xxx_get_acb(ha, acb_dma, PRIMARI_ACB, acb_len);
5051 if (rval != QLA_SUCCESS) {
5052 rval = -EIO;
5053 goto exit_free_acb;
5054 }
5055
5056 rval = qla4xxx_disable_acb(ha);
5057 if (rval != QLA_SUCCESS) {
5058 rval = -EIO;
5059 goto exit_free_acb;
5060 }
5061
5062 wait_for_completion_timeout(&ha->disable_acb_comp,
5063 DISABLE_ACB_TOV * HZ);
5064
5065 rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], acb_dma);
5066 if (rval != QLA_SUCCESS) {
5067 rval = -EIO;
5068 goto exit_free_acb;
5069 }
5070
5071exit_free_acb:
5072 dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk_def),
5073 acb, acb_dma);
5074exit_port_reset:
5075 DEBUG2(ql4_printk(KERN_INFO, ha, "%s %s\n", __func__,
5076 rval == QLA_SUCCESS ? "SUCCEEDED" : "FAILED"));
5077 return rval;
5078}
5079
5080static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type)
5081{
5082 struct scsi_qla_host *ha = to_qla_host(shost);
5083 int rval = QLA_SUCCESS;
5084
5085 if (ql4xdontresethba) {
5086 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Don't Reset HBA\n",
5087 __func__));
5088 rval = -EPERM;
5089 goto exit_host_reset;
5090 }
5091
5092 rval = qla4xxx_wait_for_hba_online(ha);
5093 if (rval != QLA_SUCCESS) {
5094 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Unable to reset host "
5095 "adapter\n", __func__));
5096 rval = -EIO;
5097 goto exit_host_reset;
5098 }
5099
5100 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
5101 goto recover_adapter;
5102
5103 switch (reset_type) {
5104 case SCSI_ADAPTER_RESET:
5105 set_bit(DPC_RESET_HA, &ha->dpc_flags);
5106 break;
5107 case SCSI_FIRMWARE_RESET:
5108 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
5109 if (is_qla8022(ha))
5110 /* set firmware context reset */
5111 set_bit(DPC_RESET_HA_FW_CONTEXT,
5112 &ha->dpc_flags);
5113 else {
5114 rval = qla4xxx_context_reset(ha);
5115 goto exit_host_reset;
5116 }
5117 }
5118 break;
5119 }
5120
5121recover_adapter:
5122 rval = qla4xxx_recover_adapter(ha);
5123 if (rval != QLA_SUCCESS) {
5124 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: recover adapter fail\n",
5125 __func__));
5126 rval = -EIO;
5127 }
5128
5129exit_host_reset:
5130 return rval;
5131}
5132
Lalit Chandivade2232be02010-07-30 14:38:47 +05305133/* PCI AER driver recovers from all correctable errors w/o
5134 * driver intervention. For uncorrectable errors PCI AER
5135 * driver calls the following device driver's callbacks
5136 *
5137 * - Fatal Errors - link_reset
5138 * - Non-Fatal Errors - driver's pci_error_detected() which
5139 * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
5140 *
5141 * PCI AER driver calls
5142 * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
5143 * returns RECOVERED or NEED_RESET if fw_hung
5144 * NEED_RESET - driver's slot_reset()
5145 * DISCONNECT - device is dead & cannot recover
5146 * RECOVERED - driver's pci_resume()
5147 */
5148static pci_ers_result_t
5149qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
5150{
5151 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
5152
5153 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
5154 ha->host_no, __func__, state);
5155
5156 if (!is_aer_supported(ha))
5157 return PCI_ERS_RESULT_NONE;
5158
5159 switch (state) {
5160 case pci_channel_io_normal:
5161 clear_bit(AF_EEH_BUSY, &ha->flags);
5162 return PCI_ERS_RESULT_CAN_RECOVER;
5163 case pci_channel_io_frozen:
5164 set_bit(AF_EEH_BUSY, &ha->flags);
5165 qla4xxx_mailbox_premature_completion(ha);
5166 qla4xxx_free_irqs(ha);
5167 pci_disable_device(pdev);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07005168 /* Return back all IOs */
5169 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
Lalit Chandivade2232be02010-07-30 14:38:47 +05305170 return PCI_ERS_RESULT_NEED_RESET;
5171 case pci_channel_io_perm_failure:
5172 set_bit(AF_EEH_BUSY, &ha->flags);
5173 set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
5174 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
5175 return PCI_ERS_RESULT_DISCONNECT;
5176 }
5177 return PCI_ERS_RESULT_NEED_RESET;
5178}
5179
5180/**
5181 * qla4xxx_pci_mmio_enabled() gets called if
5182 * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
5183 * and read/write to the device still works.
5184 **/
5185static pci_ers_result_t
5186qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
5187{
5188 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
5189
5190 if (!is_aer_supported(ha))
5191 return PCI_ERS_RESULT_NONE;
5192
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07005193 return PCI_ERS_RESULT_RECOVERED;
Lalit Chandivade2232be02010-07-30 14:38:47 +05305194}
5195
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07005196static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
Lalit Chandivade2232be02010-07-30 14:38:47 +05305197{
5198 uint32_t rval = QLA_ERROR;
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07005199 uint32_t ret = 0;
Lalit Chandivade2232be02010-07-30 14:38:47 +05305200 int fn;
5201 struct pci_dev *other_pdev = NULL;
5202
5203 ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
5204
5205 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
5206
5207 if (test_bit(AF_ONLINE, &ha->flags)) {
5208 clear_bit(AF_ONLINE, &ha->flags);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05005209 clear_bit(AF_LINK_UP, &ha->flags);
5210 iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
Lalit Chandivade2232be02010-07-30 14:38:47 +05305211 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
Lalit Chandivade2232be02010-07-30 14:38:47 +05305212 }
5213
5214 fn = PCI_FUNC(ha->pdev->devfn);
5215 while (fn > 0) {
5216 fn--;
5217 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
5218 "func %x\n", ha->host_no, __func__, fn);
5219 /* Get the pci device given the domain, bus,
5220 * slot/function number */
5221 other_pdev =
5222 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
5223 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
5224 fn));
5225
5226 if (!other_pdev)
5227 continue;
5228
5229 if (atomic_read(&other_pdev->enable_cnt)) {
5230 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
5231 "func in enabled state%x\n", ha->host_no,
5232 __func__, fn);
5233 pci_dev_put(other_pdev);
5234 break;
5235 }
5236 pci_dev_put(other_pdev);
5237 }
5238
5239 /* The first function on the card, the reset owner will
5240 * start & initialize the firmware. The other functions
5241 * on the card will reset the firmware context
5242 */
5243 if (!fn) {
5244 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
5245 "0x%x is the owner\n", ha->host_no, __func__,
5246 ha->pdev->devfn);
5247
5248 qla4_8xxx_idc_lock(ha);
5249 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
5250 QLA82XX_DEV_COLD);
5251
5252 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
5253 QLA82XX_IDC_VERSION);
5254
5255 qla4_8xxx_idc_unlock(ha);
5256 clear_bit(AF_FW_RECOVERY, &ha->flags);
Mike Christie13483732011-12-01 21:38:41 -06005257 rval = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
Lalit Chandivade2232be02010-07-30 14:38:47 +05305258 qla4_8xxx_idc_lock(ha);
5259
5260 if (rval != QLA_SUCCESS) {
5261 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
5262 "FAILED\n", ha->host_no, __func__);
5263 qla4_8xxx_clear_drv_active(ha);
5264 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
5265 QLA82XX_DEV_FAILED);
5266 } else {
5267 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
5268 "READY\n", ha->host_no, __func__);
5269 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
5270 QLA82XX_DEV_READY);
5271 /* Clear driver state register */
5272 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
5273 qla4_8xxx_set_drv_active(ha);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07005274 ret = qla4xxx_request_irqs(ha);
5275 if (ret) {
5276 ql4_printk(KERN_WARNING, ha, "Failed to "
5277 "reserve interrupt %d already in use.\n",
5278 ha->pdev->irq);
5279 rval = QLA_ERROR;
5280 } else {
5281 ha->isp_ops->enable_intrs(ha);
5282 rval = QLA_SUCCESS;
5283 }
Lalit Chandivade2232be02010-07-30 14:38:47 +05305284 }
5285 qla4_8xxx_idc_unlock(ha);
5286 } else {
5287 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
5288 "the reset owner\n", ha->host_no, __func__,
5289 ha->pdev->devfn);
5290 if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
5291 QLA82XX_DEV_READY)) {
5292 clear_bit(AF_FW_RECOVERY, &ha->flags);
Mike Christie13483732011-12-01 21:38:41 -06005293 rval = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07005294 if (rval == QLA_SUCCESS) {
5295 ret = qla4xxx_request_irqs(ha);
5296 if (ret) {
5297 ql4_printk(KERN_WARNING, ha, "Failed to"
5298 " reserve interrupt %d already in"
5299 " use.\n", ha->pdev->irq);
5300 rval = QLA_ERROR;
5301 } else {
5302 ha->isp_ops->enable_intrs(ha);
5303 rval = QLA_SUCCESS;
5304 }
5305 }
Lalit Chandivade2232be02010-07-30 14:38:47 +05305306 qla4_8xxx_idc_lock(ha);
5307 qla4_8xxx_set_drv_active(ha);
5308 qla4_8xxx_idc_unlock(ha);
5309 }
5310 }
5311 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
5312 return rval;
5313}
5314
5315static pci_ers_result_t
5316qla4xxx_pci_slot_reset(struct pci_dev *pdev)
5317{
5318 pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
5319 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
5320 int rc;
5321
5322 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
5323 ha->host_no, __func__);
5324
5325 if (!is_aer_supported(ha))
5326 return PCI_ERS_RESULT_NONE;
5327
5328 /* Restore the saved state of PCIe device -
5329 * BAR registers, PCI Config space, PCIX, MSI,
5330 * IOV states
5331 */
5332 pci_restore_state(pdev);
5333
5334 /* pci_restore_state() clears the saved_state flag of the device
5335 * save restored state which resets saved_state flag
5336 */
5337 pci_save_state(pdev);
5338
5339 /* Initialize device or resume if in suspended state */
5340 rc = pci_enable_device(pdev);
5341 if (rc) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005342 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Can't re-enable "
Lalit Chandivade2232be02010-07-30 14:38:47 +05305343 "device after reset\n", ha->host_no, __func__);
5344 goto exit_slot_reset;
5345 }
5346
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07005347 ha->isp_ops->disable_intrs(ha);
Lalit Chandivade2232be02010-07-30 14:38:47 +05305348
5349 if (is_qla8022(ha)) {
5350 if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
5351 ret = PCI_ERS_RESULT_RECOVERED;
5352 goto exit_slot_reset;
5353 } else
5354 goto exit_slot_reset;
5355 }
5356
5357exit_slot_reset:
5358 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
5359 "device after reset\n", ha->host_no, __func__, ret);
5360 return ret;
5361}
5362
5363static void
5364qla4xxx_pci_resume(struct pci_dev *pdev)
5365{
5366 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
5367 int ret;
5368
5369 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
5370 ha->host_no, __func__);
5371
5372 ret = qla4xxx_wait_for_hba_online(ha);
5373 if (ret != QLA_SUCCESS) {
5374 ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
5375 "resume I/O from slot/link_reset\n", ha->host_no,
5376 __func__);
5377 }
5378
5379 pci_cleanup_aer_uncorrect_error_status(pdev);
5380 clear_bit(AF_EEH_BUSY, &ha->flags);
5381}
5382
5383static struct pci_error_handlers qla4xxx_err_handler = {
5384 .error_detected = qla4xxx_pci_error_detected,
5385 .mmio_enabled = qla4xxx_pci_mmio_enabled,
5386 .slot_reset = qla4xxx_pci_slot_reset,
5387 .resume = qla4xxx_pci_resume,
5388};
5389
David Somayajuluafaf5a22006-09-19 10:28:00 -07005390static struct pci_device_id qla4xxx_pci_tbl[] = {
5391 {
5392 .vendor = PCI_VENDOR_ID_QLOGIC,
5393 .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
5394 .subvendor = PCI_ANY_ID,
5395 .subdevice = PCI_ANY_ID,
5396 },
5397 {
5398 .vendor = PCI_VENDOR_ID_QLOGIC,
5399 .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
5400 .subvendor = PCI_ANY_ID,
5401 .subdevice = PCI_ANY_ID,
5402 },
David C Somayajulud9150582006-11-15 17:38:40 -08005403 {
5404 .vendor = PCI_VENDOR_ID_QLOGIC,
5405 .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
5406 .subvendor = PCI_ANY_ID,
5407 .subdevice = PCI_ANY_ID,
5408 },
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05305409 {
5410 .vendor = PCI_VENDOR_ID_QLOGIC,
5411 .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
5412 .subvendor = PCI_ANY_ID,
5413 .subdevice = PCI_ANY_ID,
5414 },
David Somayajuluafaf5a22006-09-19 10:28:00 -07005415 {0, 0},
5416};
5417MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
5418
Adrian Bunk47975472007-04-26 00:35:16 -07005419static struct pci_driver qla4xxx_pci_driver = {
David Somayajuluafaf5a22006-09-19 10:28:00 -07005420 .name = DRIVER_NAME,
5421 .id_table = qla4xxx_pci_tbl,
5422 .probe = qla4xxx_probe_adapter,
5423 .remove = qla4xxx_remove_adapter,
Lalit Chandivade2232be02010-07-30 14:38:47 +05305424 .err_handler = &qla4xxx_err_handler,
David Somayajuluafaf5a22006-09-19 10:28:00 -07005425};
5426
5427static int __init qla4xxx_module_init(void)
5428{
5429 int ret;
5430
5431 /* Allocate cache for SRBs. */
5432 srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09005433 SLAB_HWCACHE_ALIGN, NULL);
David Somayajuluafaf5a22006-09-19 10:28:00 -07005434 if (srb_cachep == NULL) {
5435 printk(KERN_ERR
5436 "%s: Unable to allocate SRB cache..."
5437 "Failing load!\n", DRIVER_NAME);
5438 ret = -ENOMEM;
5439 goto no_srp_cache;
5440 }
5441
5442 /* Derive version string. */
5443 strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
Andrew Vasquez11010fe2006-10-06 09:54:59 -07005444 if (ql4xextended_error_logging)
David Somayajuluafaf5a22006-09-19 10:28:00 -07005445 strcat(qla4xxx_version_str, "-debug");
5446
5447 qla4xxx_scsi_transport =
5448 iscsi_register_transport(&qla4xxx_iscsi_transport);
5449 if (!qla4xxx_scsi_transport){
5450 ret = -ENODEV;
5451 goto release_srb_cache;
5452 }
5453
David Somayajuluafaf5a22006-09-19 10:28:00 -07005454 ret = pci_register_driver(&qla4xxx_pci_driver);
5455 if (ret)
5456 goto unregister_transport;
5457
5458 printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
5459 return 0;
Doug Maxey5ae16db2006-10-05 23:50:07 -05005460
David Somayajuluafaf5a22006-09-19 10:28:00 -07005461unregister_transport:
5462 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
5463release_srb_cache:
5464 kmem_cache_destroy(srb_cachep);
5465no_srp_cache:
5466 return ret;
5467}
5468
5469static void __exit qla4xxx_module_exit(void)
5470{
David Somayajuluafaf5a22006-09-19 10:28:00 -07005471 pci_unregister_driver(&qla4xxx_pci_driver);
5472 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
5473 kmem_cache_destroy(srb_cachep);
5474}
5475
5476module_init(qla4xxx_module_init);
5477module_exit(qla4xxx_module_exit);
5478
5479MODULE_AUTHOR("QLogic Corporation");
5480MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
5481MODULE_LICENSE("GPL");
5482MODULE_VERSION(QLA4XXX_DRIVER_VERSION);