blob: 064d67282f59dbf954b12d60267573b667739af1 [file] [log] [blame]
David Somayajuluafaf5a22006-09-19 10:28:00 -07001/*
2 * QLogic iSCSI HBA Driver
Vikas Chaudhary7d01d062010-12-02 22:12:51 -08003 * Copyright (c) 2003-2010 QLogic Corporation
David Somayajuluafaf5a22006-09-19 10:28:00 -07004 *
5 * See LICENSE.qla4xxx for copyright and licensing details.
6 */
7#include <linux/moduleparam.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
Manish Rangankar2a991c22011-07-25 13:48:55 -05009#include <linux/blkdev.h>
10#include <linux/iscsi_boot_sysfs.h>
Mike Christie13483732011-12-01 21:38:41 -060011#include <linux/inet.h>
David Somayajuluafaf5a22006-09-19 10:28:00 -070012
13#include <scsi/scsi_tcq.h>
14#include <scsi/scsicam.h>
15
16#include "ql4_def.h"
David C Somayajulubee4fe82007-05-23 18:03:32 -070017#include "ql4_version.h"
18#include "ql4_glbl.h"
19#include "ql4_dbg.h"
20#include "ql4_inline.h"
David Somayajuluafaf5a22006-09-19 10:28:00 -070021
22/*
23 * Driver version
24 */
Adrian Bunk47975472007-04-26 00:35:16 -070025static char qla4xxx_version_str[40];
David Somayajuluafaf5a22006-09-19 10:28:00 -070026
27/*
28 * SRB allocation cache
29 */
Christoph Lametere18b8902006-12-06 20:33:20 -080030static struct kmem_cache *srb_cachep;
David Somayajuluafaf5a22006-09-19 10:28:00 -070031
32/*
33 * Module parameter information and variables
34 */
Mike Christie13483732011-12-01 21:38:41 -060035int ql4xdisablesysfsboot = 1;
36module_param(ql4xdisablesysfsboot, int, S_IRUGO | S_IWUSR);
37MODULE_PARM_DESC(ql4xdisablesysfsboot,
38 "Set to disable exporting boot targets to sysfs\n"
39 " 0 - Export boot targets\n"
40 " 1 - Do not export boot targets (Default)");
41
David Somayajuluafaf5a22006-09-19 10:28:00 -070042int ql4xdontresethba = 0;
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +053043module_param(ql4xdontresethba, int, S_IRUGO | S_IWUSR);
David Somayajuluafaf5a22006-09-19 10:28:00 -070044MODULE_PARM_DESC(ql4xdontresethba,
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +053045 "Don't reset the HBA for driver recovery \n"
46 " 0 - It will reset HBA (Default)\n"
47 " 1 - It will NOT reset HBA");
David Somayajuluafaf5a22006-09-19 10:28:00 -070048
Andrew Vasquez11010fe2006-10-06 09:54:59 -070049int ql4xextended_error_logging = 0; /* 0 = off, 1 = log errors */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +053050module_param(ql4xextended_error_logging, int, S_IRUGO | S_IWUSR);
Andrew Vasquez11010fe2006-10-06 09:54:59 -070051MODULE_PARM_DESC(ql4xextended_error_logging,
David Somayajuluafaf5a22006-09-19 10:28:00 -070052 "Option to enable extended error logging, "
53 "Default is 0 - no logging, 1 - debug logging");
54
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +053055int ql4xenablemsix = 1;
56module_param(ql4xenablemsix, int, S_IRUGO|S_IWUSR);
57MODULE_PARM_DESC(ql4xenablemsix,
58 "Set to enable MSI or MSI-X interrupt mechanism.\n"
59 " 0 = enable INTx interrupt mechanism.\n"
60 " 1 = enable MSI-X interrupt mechanism (Default).\n"
61 " 2 = enable MSI interrupt mechanism.");
David C Somayajulu477ffb92007-01-22 12:26:11 -080062
Mike Christied510d962008-07-11 19:50:33 -050063#define QL4_DEF_QDEPTH 32
Vikas Chaudhary8bb40332011-03-21 03:34:31 -070064static int ql4xmaxqdepth = QL4_DEF_QDEPTH;
65module_param(ql4xmaxqdepth, int, S_IRUGO | S_IWUSR);
66MODULE_PARM_DESC(ql4xmaxqdepth,
67 "Maximum queue depth to report for target devices.\n"
68 " Default: 32.");
Mike Christied510d962008-07-11 19:50:33 -050069
Vikas Chaudhary30387272011-03-21 03:34:32 -070070static int ql4xsess_recovery_tmo = QL4_SESS_RECOVERY_TMO;
71module_param(ql4xsess_recovery_tmo, int, S_IRUGO);
72MODULE_PARM_DESC(ql4xsess_recovery_tmo,
73 "Target Session Recovery Timeout.\n"
Mike Christie13483732011-12-01 21:38:41 -060074 " Default: 120 sec.");
Vikas Chaudhary30387272011-03-21 03:34:32 -070075
Manish Rangankarb3a271a2011-07-25 13:48:53 -050076static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -070077/*
78 * SCSI host template entry points
79 */
Adrian Bunk47975472007-04-26 00:35:16 -070080static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -070081
82/*
83 * iSCSI template entry points
84 */
David Somayajuluafaf5a22006-09-19 10:28:00 -070085static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
86 enum iscsi_param param, char *buf);
Mike Christieaa1e93a2007-05-30 12:57:09 -050087static int qla4xxx_host_get_param(struct Scsi_Host *shost,
88 enum iscsi_host_param param, char *buf);
Mike Christie00c31882011-10-06 03:56:59 -050089static int qla4xxx_iface_set_param(struct Scsi_Host *shost, void *data,
90 uint32_t len);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -050091static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
92 enum iscsi_param_type param_type,
93 int param, char *buf);
Mike Christie5c656af2009-07-15 15:02:59 -050094static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
Manish Rangankarb3a271a2011-07-25 13:48:53 -050095static struct iscsi_endpoint *qla4xxx_ep_connect(struct Scsi_Host *shost,
96 struct sockaddr *dst_addr,
97 int non_blocking);
98static int qla4xxx_ep_poll(struct iscsi_endpoint *ep, int timeout_ms);
99static void qla4xxx_ep_disconnect(struct iscsi_endpoint *ep);
100static int qla4xxx_get_ep_param(struct iscsi_endpoint *ep,
101 enum iscsi_param param, char *buf);
102static int qla4xxx_conn_start(struct iscsi_cls_conn *conn);
103static struct iscsi_cls_conn *
104qla4xxx_conn_create(struct iscsi_cls_session *cls_sess, uint32_t conn_idx);
105static int qla4xxx_conn_bind(struct iscsi_cls_session *cls_session,
106 struct iscsi_cls_conn *cls_conn,
107 uint64_t transport_fd, int is_leading);
108static void qla4xxx_conn_destroy(struct iscsi_cls_conn *conn);
109static struct iscsi_cls_session *
110qla4xxx_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max,
111 uint16_t qdepth, uint32_t initial_cmdsn);
112static void qla4xxx_session_destroy(struct iscsi_cls_session *sess);
113static void qla4xxx_task_work(struct work_struct *wdata);
114static int qla4xxx_alloc_pdu(struct iscsi_task *, uint8_t);
115static int qla4xxx_task_xmit(struct iscsi_task *);
116static void qla4xxx_task_cleanup(struct iscsi_task *);
117static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session);
118static void qla4xxx_conn_get_stats(struct iscsi_cls_conn *cls_conn,
119 struct iscsi_stats *stats);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700120/*
121 * SCSI host template entry points
122 */
Jeff Garzikf2812332010-11-16 02:10:29 -0500123static int qla4xxx_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmd);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530124static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700125static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
Mike Christiece545032008-02-29 18:25:20 -0600126static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700127static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
128static int qla4xxx_slave_alloc(struct scsi_device *device);
129static int qla4xxx_slave_configure(struct scsi_device *device);
130static void qla4xxx_slave_destroy(struct scsi_device *sdev);
Mike Christie3128c6c2011-07-25 13:48:42 -0500131static mode_t ql4_attr_is_visible(int param_type, int param);
Vikas Chaudhary95d31262011-08-12 02:51:29 -0700132static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700133
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530134static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
135 QLA82XX_LEGACY_INTR_CONFIG;
136
David Somayajuluafaf5a22006-09-19 10:28:00 -0700137static struct scsi_host_template qla4xxx_driver_template = {
138 .module = THIS_MODULE,
139 .name = DRIVER_NAME,
140 .proc_name = DRIVER_NAME,
141 .queuecommand = qla4xxx_queuecommand,
142
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530143 .eh_abort_handler = qla4xxx_eh_abort,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700144 .eh_device_reset_handler = qla4xxx_eh_device_reset,
Mike Christiece545032008-02-29 18:25:20 -0600145 .eh_target_reset_handler = qla4xxx_eh_target_reset,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700146 .eh_host_reset_handler = qla4xxx_eh_host_reset,
Mike Christie5c656af2009-07-15 15:02:59 -0500147 .eh_timed_out = qla4xxx_eh_cmd_timed_out,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700148
149 .slave_configure = qla4xxx_slave_configure,
150 .slave_alloc = qla4xxx_slave_alloc,
151 .slave_destroy = qla4xxx_slave_destroy,
152
153 .this_id = -1,
154 .cmd_per_lun = 3,
155 .use_clustering = ENABLE_CLUSTERING,
156 .sg_tablesize = SG_ALL,
157
158 .max_sectors = 0xFFFF,
Harish Zunjarrao7ad633c2011-05-17 23:17:11 -0700159 .shost_attrs = qla4xxx_host_attrs,
Vikas Chaudhary95d31262011-08-12 02:51:29 -0700160 .host_reset = qla4xxx_host_reset,
Vikas Chaudharya3559432011-07-25 13:48:51 -0500161 .vendor_id = SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_QLOGIC,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700162};
163
164static struct iscsi_transport qla4xxx_iscsi_transport = {
165 .owner = THIS_MODULE,
166 .name = DRIVER_NAME,
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500167 .caps = CAP_TEXT_NEGO |
168 CAP_DATA_PATH_OFFLOAD | CAP_HDRDGST |
169 CAP_DATADGST | CAP_LOGIN_OFFLOAD |
170 CAP_MULTI_R2T,
Mike Christie3128c6c2011-07-25 13:48:42 -0500171 .attr_is_visible = ql4_attr_is_visible,
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500172 .create_session = qla4xxx_session_create,
173 .destroy_session = qla4xxx_session_destroy,
174 .start_conn = qla4xxx_conn_start,
175 .create_conn = qla4xxx_conn_create,
176 .bind_conn = qla4xxx_conn_bind,
177 .stop_conn = iscsi_conn_stop,
178 .destroy_conn = qla4xxx_conn_destroy,
179 .set_param = iscsi_set_param,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700180 .get_conn_param = qla4xxx_conn_get_param,
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500181 .get_session_param = iscsi_session_get_param,
182 .get_ep_param = qla4xxx_get_ep_param,
183 .ep_connect = qla4xxx_ep_connect,
184 .ep_poll = qla4xxx_ep_poll,
185 .ep_disconnect = qla4xxx_ep_disconnect,
186 .get_stats = qla4xxx_conn_get_stats,
187 .send_pdu = iscsi_conn_send_pdu,
188 .xmit_task = qla4xxx_task_xmit,
189 .cleanup_task = qla4xxx_task_cleanup,
190 .alloc_pdu = qla4xxx_alloc_pdu,
191
Mike Christieaa1e93a2007-05-30 12:57:09 -0500192 .get_host_param = qla4xxx_host_get_param,
Mike Christied00efe32011-07-25 13:48:38 -0500193 .set_iface_param = qla4xxx_iface_set_param,
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500194 .get_iface_param = qla4xxx_get_iface_param,
Vikas Chaudharya3559432011-07-25 13:48:51 -0500195 .bsg_request = qla4xxx_bsg_request,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700196};
197
198static struct scsi_transport_template *qla4xxx_scsi_transport;
199
Mike Christie3128c6c2011-07-25 13:48:42 -0500200static mode_t ql4_attr_is_visible(int param_type, int param)
201{
202 switch (param_type) {
Mike Christief27fb2e2011-07-25 13:48:45 -0500203 case ISCSI_HOST_PARAM:
204 switch (param) {
205 case ISCSI_HOST_PARAM_HWADDRESS:
206 case ISCSI_HOST_PARAM_IPADDRESS:
207 case ISCSI_HOST_PARAM_INITIATOR_NAME:
208 return S_IRUGO;
209 default:
210 return 0;
211 }
Mike Christie3128c6c2011-07-25 13:48:42 -0500212 case ISCSI_PARAM:
213 switch (param) {
Mike Christie590134f2011-10-17 22:42:13 -0500214 case ISCSI_PARAM_PERSISTENT_ADDRESS:
215 case ISCSI_PARAM_PERSISTENT_PORT:
Mike Christie3128c6c2011-07-25 13:48:42 -0500216 case ISCSI_PARAM_CONN_ADDRESS:
217 case ISCSI_PARAM_CONN_PORT:
Mike Christie1d063c12011-07-25 13:48:43 -0500218 case ISCSI_PARAM_TARGET_NAME:
219 case ISCSI_PARAM_TPGT:
220 case ISCSI_PARAM_TARGET_ALIAS:
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500221 case ISCSI_PARAM_MAX_BURST:
222 case ISCSI_PARAM_MAX_R2T:
223 case ISCSI_PARAM_FIRST_BURST:
224 case ISCSI_PARAM_MAX_RECV_DLENGTH:
225 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
Mike Christiede379202011-08-14 20:42:56 -0500226 case ISCSI_PARAM_IFACE_NAME:
Mike Christie3128c6c2011-07-25 13:48:42 -0500227 return S_IRUGO;
228 default:
229 return 0;
230 }
Mike Christieb78dbba2011-07-25 13:48:44 -0500231 case ISCSI_NET_PARAM:
232 switch (param) {
233 case ISCSI_NET_PARAM_IPV4_ADDR:
234 case ISCSI_NET_PARAM_IPV4_SUBNET:
235 case ISCSI_NET_PARAM_IPV4_GW:
236 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
237 case ISCSI_NET_PARAM_IFACE_ENABLE:
238 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
239 case ISCSI_NET_PARAM_IPV6_ADDR:
240 case ISCSI_NET_PARAM_IPV6_ROUTER:
241 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
242 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
Vikas Chaudhary6ac73e82011-07-25 13:48:49 -0500243 case ISCSI_NET_PARAM_VLAN_ID:
244 case ISCSI_NET_PARAM_VLAN_PRIORITY:
245 case ISCSI_NET_PARAM_VLAN_ENABLED:
Vikas Chaudhary943c1572011-08-01 03:26:13 -0700246 case ISCSI_NET_PARAM_MTU:
Vikas Chaudhary2ada7fc2011-08-01 03:26:19 -0700247 case ISCSI_NET_PARAM_PORT:
Mike Christieb78dbba2011-07-25 13:48:44 -0500248 return S_IRUGO;
249 default:
250 return 0;
251 }
Mike Christie3128c6c2011-07-25 13:48:42 -0500252 }
253
254 return 0;
255}
256
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500257static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
258 enum iscsi_param_type param_type,
259 int param, char *buf)
260{
261 struct Scsi_Host *shost = iscsi_iface_to_shost(iface);
262 struct scsi_qla_host *ha = to_qla_host(shost);
263 int len = -ENOSYS;
264
265 if (param_type != ISCSI_NET_PARAM)
266 return -ENOSYS;
267
268 switch (param) {
269 case ISCSI_NET_PARAM_IPV4_ADDR:
270 len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
271 break;
272 case ISCSI_NET_PARAM_IPV4_SUBNET:
273 len = sprintf(buf, "%pI4\n", &ha->ip_config.subnet_mask);
274 break;
275 case ISCSI_NET_PARAM_IPV4_GW:
276 len = sprintf(buf, "%pI4\n", &ha->ip_config.gateway);
277 break;
278 case ISCSI_NET_PARAM_IFACE_ENABLE:
279 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
280 len = sprintf(buf, "%s\n",
281 (ha->ip_config.ipv4_options &
282 IPOPT_IPV4_PROTOCOL_ENABLE) ?
283 "enabled" : "disabled");
284 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
285 len = sprintf(buf, "%s\n",
286 (ha->ip_config.ipv6_options &
287 IPV6_OPT_IPV6_PROTOCOL_ENABLE) ?
288 "enabled" : "disabled");
289 break;
290 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
291 len = sprintf(buf, "%s\n",
292 (ha->ip_config.tcp_options & TCPOPT_DHCP_ENABLE) ?
293 "dhcp" : "static");
294 break;
295 case ISCSI_NET_PARAM_IPV6_ADDR:
296 if (iface->iface_num == 0)
297 len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr0);
298 if (iface->iface_num == 1)
299 len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr1);
300 break;
301 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
302 len = sprintf(buf, "%pI6\n",
303 &ha->ip_config.ipv6_link_local_addr);
304 break;
305 case ISCSI_NET_PARAM_IPV6_ROUTER:
306 len = sprintf(buf, "%pI6\n",
307 &ha->ip_config.ipv6_default_router_addr);
308 break;
309 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
310 len = sprintf(buf, "%s\n",
311 (ha->ip_config.ipv6_addl_options &
312 IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) ?
313 "nd" : "static");
314 break;
315 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
316 len = sprintf(buf, "%s\n",
317 (ha->ip_config.ipv6_addl_options &
318 IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR) ?
319 "auto" : "static");
320 break;
Vikas Chaudhary6ac73e82011-07-25 13:48:49 -0500321 case ISCSI_NET_PARAM_VLAN_ID:
322 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
323 len = sprintf(buf, "%d\n",
324 (ha->ip_config.ipv4_vlan_tag &
325 ISCSI_MAX_VLAN_ID));
326 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
327 len = sprintf(buf, "%d\n",
328 (ha->ip_config.ipv6_vlan_tag &
329 ISCSI_MAX_VLAN_ID));
330 break;
331 case ISCSI_NET_PARAM_VLAN_PRIORITY:
332 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
333 len = sprintf(buf, "%d\n",
334 ((ha->ip_config.ipv4_vlan_tag >> 13) &
335 ISCSI_MAX_VLAN_PRIORITY));
336 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
337 len = sprintf(buf, "%d\n",
338 ((ha->ip_config.ipv6_vlan_tag >> 13) &
339 ISCSI_MAX_VLAN_PRIORITY));
340 break;
341 case ISCSI_NET_PARAM_VLAN_ENABLED:
342 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
343 len = sprintf(buf, "%s\n",
344 (ha->ip_config.ipv4_options &
345 IPOPT_VLAN_TAGGING_ENABLE) ?
346 "enabled" : "disabled");
347 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
348 len = sprintf(buf, "%s\n",
349 (ha->ip_config.ipv6_options &
350 IPV6_OPT_VLAN_TAGGING_ENABLE) ?
351 "enabled" : "disabled");
352 break;
Vikas Chaudhary943c1572011-08-01 03:26:13 -0700353 case ISCSI_NET_PARAM_MTU:
354 len = sprintf(buf, "%d\n", ha->ip_config.eth_mtu_size);
355 break;
Vikas Chaudhary2ada7fc2011-08-01 03:26:19 -0700356 case ISCSI_NET_PARAM_PORT:
357 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
358 len = sprintf(buf, "%d\n", ha->ip_config.ipv4_port);
359 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
360 len = sprintf(buf, "%d\n", ha->ip_config.ipv6_port);
361 break;
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500362 default:
363 len = -ENOSYS;
364 }
365
366 return len;
367}
368
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500369static struct iscsi_endpoint *
370qla4xxx_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
371 int non_blocking)
372{
373 int ret;
374 struct iscsi_endpoint *ep;
375 struct qla_endpoint *qla_ep;
376 struct scsi_qla_host *ha;
377 struct sockaddr_in *addr;
378 struct sockaddr_in6 *addr6;
379
380 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
381 if (!shost) {
382 ret = -ENXIO;
383 printk(KERN_ERR "%s: shost is NULL\n",
384 __func__);
385 return ERR_PTR(ret);
386 }
387
388 ha = iscsi_host_priv(shost);
389
390 ep = iscsi_create_endpoint(sizeof(struct qla_endpoint));
391 if (!ep) {
392 ret = -ENOMEM;
393 return ERR_PTR(ret);
394 }
395
396 qla_ep = ep->dd_data;
397 memset(qla_ep, 0, sizeof(struct qla_endpoint));
398 if (dst_addr->sa_family == AF_INET) {
399 memcpy(&qla_ep->dst_addr, dst_addr, sizeof(struct sockaddr_in));
400 addr = (struct sockaddr_in *)&qla_ep->dst_addr;
401 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI4\n", __func__,
402 (char *)&addr->sin_addr));
403 } else if (dst_addr->sa_family == AF_INET6) {
404 memcpy(&qla_ep->dst_addr, dst_addr,
405 sizeof(struct sockaddr_in6));
406 addr6 = (struct sockaddr_in6 *)&qla_ep->dst_addr;
407 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI6\n", __func__,
408 (char *)&addr6->sin6_addr));
409 }
410
411 qla_ep->host = shost;
412
413 return ep;
414}
415
416static int qla4xxx_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
417{
418 struct qla_endpoint *qla_ep;
419 struct scsi_qla_host *ha;
420 int ret = 0;
421
422 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
423 qla_ep = ep->dd_data;
424 ha = to_qla_host(qla_ep->host);
425
Mike Christie13483732011-12-01 21:38:41 -0600426 if (adapter_up(ha) && !test_bit(AF_BUILD_DDB_LIST, &ha->flags))
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500427 ret = 1;
428
429 return ret;
430}
431
432static void qla4xxx_ep_disconnect(struct iscsi_endpoint *ep)
433{
434 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
435 iscsi_destroy_endpoint(ep);
436}
437
438static int qla4xxx_get_ep_param(struct iscsi_endpoint *ep,
439 enum iscsi_param param,
440 char *buf)
441{
442 struct qla_endpoint *qla_ep = ep->dd_data;
443 struct sockaddr *dst_addr;
444
445 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
446
447 switch (param) {
448 case ISCSI_PARAM_CONN_PORT:
449 case ISCSI_PARAM_CONN_ADDRESS:
450 if (!qla_ep)
451 return -ENOTCONN;
452
453 dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
454 if (!dst_addr)
455 return -ENOTCONN;
456
457 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
458 &qla_ep->dst_addr, param, buf);
459 default:
460 return -ENOSYS;
461 }
462}
463
464static void qla4xxx_conn_get_stats(struct iscsi_cls_conn *cls_conn,
465 struct iscsi_stats *stats)
466{
467 struct iscsi_session *sess;
468 struct iscsi_cls_session *cls_sess;
469 struct ddb_entry *ddb_entry;
470 struct scsi_qla_host *ha;
471 struct ql_iscsi_stats *ql_iscsi_stats;
472 int stats_size;
473 int ret;
474 dma_addr_t iscsi_stats_dma;
475
476 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
477
478 cls_sess = iscsi_conn_to_session(cls_conn);
479 sess = cls_sess->dd_data;
480 ddb_entry = sess->dd_data;
481 ha = ddb_entry->ha;
482
483 stats_size = PAGE_ALIGN(sizeof(struct ql_iscsi_stats));
484 /* Allocate memory */
485 ql_iscsi_stats = dma_alloc_coherent(&ha->pdev->dev, stats_size,
486 &iscsi_stats_dma, GFP_KERNEL);
487 if (!ql_iscsi_stats) {
488 ql4_printk(KERN_ERR, ha,
489 "Unable to allocate memory for iscsi stats\n");
490 goto exit_get_stats;
491 }
492
493 ret = qla4xxx_get_mgmt_data(ha, ddb_entry->fw_ddb_index, stats_size,
494 iscsi_stats_dma);
495 if (ret != QLA_SUCCESS) {
496 ql4_printk(KERN_ERR, ha,
497 "Unable to retreive iscsi stats\n");
498 goto free_stats;
499 }
500
501 /* octets */
502 stats->txdata_octets = le64_to_cpu(ql_iscsi_stats->tx_data_octets);
503 stats->rxdata_octets = le64_to_cpu(ql_iscsi_stats->rx_data_octets);
504 /* xmit pdus */
505 stats->noptx_pdus = le32_to_cpu(ql_iscsi_stats->tx_nopout_pdus);
506 stats->scsicmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_cmd_pdus);
507 stats->tmfcmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_tmf_cmd_pdus);
508 stats->login_pdus = le32_to_cpu(ql_iscsi_stats->tx_login_cmd_pdus);
509 stats->text_pdus = le32_to_cpu(ql_iscsi_stats->tx_text_cmd_pdus);
510 stats->dataout_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_write_pdus);
511 stats->logout_pdus = le32_to_cpu(ql_iscsi_stats->tx_logout_cmd_pdus);
512 stats->snack_pdus = le32_to_cpu(ql_iscsi_stats->tx_snack_req_pdus);
513 /* recv pdus */
514 stats->noprx_pdus = le32_to_cpu(ql_iscsi_stats->rx_nopin_pdus);
515 stats->scsirsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_resp_pdus);
516 stats->tmfrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_tmf_resp_pdus);
517 stats->textrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_text_resp_pdus);
518 stats->datain_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_read_pdus);
519 stats->logoutrsp_pdus =
520 le32_to_cpu(ql_iscsi_stats->rx_logout_resp_pdus);
521 stats->r2t_pdus = le32_to_cpu(ql_iscsi_stats->rx_r2t_pdus);
522 stats->async_pdus = le32_to_cpu(ql_iscsi_stats->rx_async_pdus);
523 stats->rjt_pdus = le32_to_cpu(ql_iscsi_stats->rx_reject_pdus);
524
525free_stats:
526 dma_free_coherent(&ha->pdev->dev, stats_size, ql_iscsi_stats,
527 iscsi_stats_dma);
528exit_get_stats:
529 return;
530}
531
Mike Christie5c656af2009-07-15 15:02:59 -0500532static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
533{
534 struct iscsi_cls_session *session;
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500535 struct iscsi_session *sess;
536 unsigned long flags;
537 enum blk_eh_timer_return ret = BLK_EH_NOT_HANDLED;
Mike Christie5c656af2009-07-15 15:02:59 -0500538
539 session = starget_to_session(scsi_target(sc->device));
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500540 sess = session->dd_data;
Mike Christie5c656af2009-07-15 15:02:59 -0500541
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500542 spin_lock_irqsave(&session->lock, flags);
543 if (session->state == ISCSI_SESSION_FAILED)
544 ret = BLK_EH_RESET_TIMER;
545 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie5c656af2009-07-15 15:02:59 -0500546
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500547 return ret;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700548}
549
Mike Christieaa1e93a2007-05-30 12:57:09 -0500550static int qla4xxx_host_get_param(struct Scsi_Host *shost,
551 enum iscsi_host_param param, char *buf)
552{
553 struct scsi_qla_host *ha = to_qla_host(shost);
554 int len;
555
556 switch (param) {
557 case ISCSI_HOST_PARAM_HWADDRESS:
Michael Chan7ffc49a2007-12-24 21:28:09 -0800558 len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
Mike Christieaa1e93a2007-05-30 12:57:09 -0500559 break;
Mike Christie22236962007-05-30 12:57:24 -0500560 case ISCSI_HOST_PARAM_IPADDRESS:
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500561 len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
Mike Christie22236962007-05-30 12:57:24 -0500562 break;
Mike Christie8ad57812007-05-30 12:57:13 -0500563 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie22236962007-05-30 12:57:24 -0500564 len = sprintf(buf, "%s\n", ha->name_string);
Mike Christie8ad57812007-05-30 12:57:13 -0500565 break;
Mike Christieaa1e93a2007-05-30 12:57:09 -0500566 default:
567 return -ENOSYS;
568 }
569
570 return len;
571}
572
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500573static void qla4xxx_create_ipv4_iface(struct scsi_qla_host *ha)
574{
575 if (ha->iface_ipv4)
576 return;
577
578 /* IPv4 */
579 ha->iface_ipv4 = iscsi_create_iface(ha->host,
580 &qla4xxx_iscsi_transport,
581 ISCSI_IFACE_TYPE_IPV4, 0, 0);
582 if (!ha->iface_ipv4)
583 ql4_printk(KERN_ERR, ha, "Could not create IPv4 iSCSI "
584 "iface0.\n");
585}
586
587static void qla4xxx_create_ipv6_iface(struct scsi_qla_host *ha)
588{
589 if (!ha->iface_ipv6_0)
590 /* IPv6 iface-0 */
591 ha->iface_ipv6_0 = iscsi_create_iface(ha->host,
592 &qla4xxx_iscsi_transport,
593 ISCSI_IFACE_TYPE_IPV6, 0,
594 0);
595 if (!ha->iface_ipv6_0)
596 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
597 "iface0.\n");
598
599 if (!ha->iface_ipv6_1)
600 /* IPv6 iface-1 */
601 ha->iface_ipv6_1 = iscsi_create_iface(ha->host,
602 &qla4xxx_iscsi_transport,
603 ISCSI_IFACE_TYPE_IPV6, 1,
604 0);
605 if (!ha->iface_ipv6_1)
606 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
607 "iface1.\n");
608}
609
610static void qla4xxx_create_ifaces(struct scsi_qla_host *ha)
611{
612 if (ha->ip_config.ipv4_options & IPOPT_IPV4_PROTOCOL_ENABLE)
613 qla4xxx_create_ipv4_iface(ha);
614
615 if (ha->ip_config.ipv6_options & IPV6_OPT_IPV6_PROTOCOL_ENABLE)
616 qla4xxx_create_ipv6_iface(ha);
617}
618
619static void qla4xxx_destroy_ipv4_iface(struct scsi_qla_host *ha)
620{
621 if (ha->iface_ipv4) {
622 iscsi_destroy_iface(ha->iface_ipv4);
623 ha->iface_ipv4 = NULL;
624 }
625}
626
627static void qla4xxx_destroy_ipv6_iface(struct scsi_qla_host *ha)
628{
629 if (ha->iface_ipv6_0) {
630 iscsi_destroy_iface(ha->iface_ipv6_0);
631 ha->iface_ipv6_0 = NULL;
632 }
633 if (ha->iface_ipv6_1) {
634 iscsi_destroy_iface(ha->iface_ipv6_1);
635 ha->iface_ipv6_1 = NULL;
636 }
637}
638
639static void qla4xxx_destroy_ifaces(struct scsi_qla_host *ha)
640{
641 qla4xxx_destroy_ipv4_iface(ha);
642 qla4xxx_destroy_ipv6_iface(ha);
643}
644
Mike Christied00efe32011-07-25 13:48:38 -0500645static void qla4xxx_set_ipv6(struct scsi_qla_host *ha,
646 struct iscsi_iface_param_info *iface_param,
647 struct addr_ctrl_blk *init_fw_cb)
648{
649 /*
650 * iface_num 0 is valid for IPv6 Addr, linklocal, router, autocfg.
651 * iface_num 1 is valid only for IPv6 Addr.
652 */
653 switch (iface_param->param) {
654 case ISCSI_NET_PARAM_IPV6_ADDR:
655 if (iface_param->iface_num & 0x1)
656 /* IPv6 Addr 1 */
657 memcpy(init_fw_cb->ipv6_addr1, iface_param->value,
658 sizeof(init_fw_cb->ipv6_addr1));
659 else
660 /* IPv6 Addr 0 */
661 memcpy(init_fw_cb->ipv6_addr0, iface_param->value,
662 sizeof(init_fw_cb->ipv6_addr0));
663 break;
664 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
665 if (iface_param->iface_num & 0x1)
666 break;
667 memcpy(init_fw_cb->ipv6_if_id, &iface_param->value[8],
668 sizeof(init_fw_cb->ipv6_if_id));
669 break;
670 case ISCSI_NET_PARAM_IPV6_ROUTER:
671 if (iface_param->iface_num & 0x1)
672 break;
673 memcpy(init_fw_cb->ipv6_dflt_rtr_addr, iface_param->value,
674 sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
675 break;
676 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
677 /* Autocfg applies to even interface */
678 if (iface_param->iface_num & 0x1)
679 break;
680
681 if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_DISABLE)
682 init_fw_cb->ipv6_addtl_opts &=
683 cpu_to_le16(
684 ~IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
685 else if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_ND_ENABLE)
686 init_fw_cb->ipv6_addtl_opts |=
687 cpu_to_le16(
688 IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
689 else
690 ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
691 "IPv6 addr\n");
692 break;
693 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
694 /* Autocfg applies to even interface */
695 if (iface_param->iface_num & 0x1)
696 break;
697
698 if (iface_param->value[0] ==
699 ISCSI_IPV6_LINKLOCAL_AUTOCFG_ENABLE)
700 init_fw_cb->ipv6_addtl_opts |= cpu_to_le16(
701 IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
702 else if (iface_param->value[0] ==
703 ISCSI_IPV6_LINKLOCAL_AUTOCFG_DISABLE)
704 init_fw_cb->ipv6_addtl_opts &= cpu_to_le16(
705 ~IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
706 else
707 ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
708 "IPv6 linklocal addr\n");
709 break;
710 case ISCSI_NET_PARAM_IPV6_ROUTER_AUTOCFG:
711 /* Autocfg applies to even interface */
712 if (iface_param->iface_num & 0x1)
713 break;
714
715 if (iface_param->value[0] == ISCSI_IPV6_ROUTER_AUTOCFG_ENABLE)
716 memset(init_fw_cb->ipv6_dflt_rtr_addr, 0,
717 sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
718 break;
719 case ISCSI_NET_PARAM_IFACE_ENABLE:
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500720 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
Mike Christied00efe32011-07-25 13:48:38 -0500721 init_fw_cb->ipv6_opts |=
722 cpu_to_le16(IPV6_OPT_IPV6_PROTOCOL_ENABLE);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500723 qla4xxx_create_ipv6_iface(ha);
724 } else {
Mike Christied00efe32011-07-25 13:48:38 -0500725 init_fw_cb->ipv6_opts &=
726 cpu_to_le16(~IPV6_OPT_IPV6_PROTOCOL_ENABLE &
727 0xFFFF);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500728 qla4xxx_destroy_ipv6_iface(ha);
729 }
Mike Christied00efe32011-07-25 13:48:38 -0500730 break;
Mike Christie2d636732011-10-11 17:55:11 -0500731 case ISCSI_NET_PARAM_VLAN_TAG:
Mike Christied00efe32011-07-25 13:48:38 -0500732 if (iface_param->len != sizeof(init_fw_cb->ipv6_vlan_tag))
733 break;
Vikas Chaudhary6ac73e82011-07-25 13:48:49 -0500734 init_fw_cb->ipv6_vlan_tag =
735 cpu_to_be16(*(uint16_t *)iface_param->value);
736 break;
737 case ISCSI_NET_PARAM_VLAN_ENABLED:
738 if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
739 init_fw_cb->ipv6_opts |=
740 cpu_to_le16(IPV6_OPT_VLAN_TAGGING_ENABLE);
741 else
742 init_fw_cb->ipv6_opts &=
743 cpu_to_le16(~IPV6_OPT_VLAN_TAGGING_ENABLE);
Mike Christied00efe32011-07-25 13:48:38 -0500744 break;
Vikas Chaudhary943c1572011-08-01 03:26:13 -0700745 case ISCSI_NET_PARAM_MTU:
746 init_fw_cb->eth_mtu_size =
747 cpu_to_le16(*(uint16_t *)iface_param->value);
748 break;
Vikas Chaudhary2ada7fc2011-08-01 03:26:19 -0700749 case ISCSI_NET_PARAM_PORT:
750 /* Autocfg applies to even interface */
751 if (iface_param->iface_num & 0x1)
752 break;
753
754 init_fw_cb->ipv6_port =
755 cpu_to_le16(*(uint16_t *)iface_param->value);
756 break;
Mike Christied00efe32011-07-25 13:48:38 -0500757 default:
758 ql4_printk(KERN_ERR, ha, "Unknown IPv6 param = %d\n",
759 iface_param->param);
760 break;
761 }
762}
763
764static void qla4xxx_set_ipv4(struct scsi_qla_host *ha,
765 struct iscsi_iface_param_info *iface_param,
766 struct addr_ctrl_blk *init_fw_cb)
767{
768 switch (iface_param->param) {
769 case ISCSI_NET_PARAM_IPV4_ADDR:
770 memcpy(init_fw_cb->ipv4_addr, iface_param->value,
771 sizeof(init_fw_cb->ipv4_addr));
772 break;
773 case ISCSI_NET_PARAM_IPV4_SUBNET:
774 memcpy(init_fw_cb->ipv4_subnet, iface_param->value,
775 sizeof(init_fw_cb->ipv4_subnet));
776 break;
777 case ISCSI_NET_PARAM_IPV4_GW:
778 memcpy(init_fw_cb->ipv4_gw_addr, iface_param->value,
779 sizeof(init_fw_cb->ipv4_gw_addr));
780 break;
781 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
782 if (iface_param->value[0] == ISCSI_BOOTPROTO_DHCP)
783 init_fw_cb->ipv4_tcp_opts |=
784 cpu_to_le16(TCPOPT_DHCP_ENABLE);
785 else if (iface_param->value[0] == ISCSI_BOOTPROTO_STATIC)
786 init_fw_cb->ipv4_tcp_opts &=
787 cpu_to_le16(~TCPOPT_DHCP_ENABLE);
788 else
789 ql4_printk(KERN_ERR, ha, "Invalid IPv4 bootproto\n");
790 break;
791 case ISCSI_NET_PARAM_IFACE_ENABLE:
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500792 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
Mike Christied00efe32011-07-25 13:48:38 -0500793 init_fw_cb->ipv4_ip_opts |=
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500794 cpu_to_le16(IPOPT_IPV4_PROTOCOL_ENABLE);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500795 qla4xxx_create_ipv4_iface(ha);
796 } else {
Mike Christied00efe32011-07-25 13:48:38 -0500797 init_fw_cb->ipv4_ip_opts &=
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500798 cpu_to_le16(~IPOPT_IPV4_PROTOCOL_ENABLE &
Mike Christied00efe32011-07-25 13:48:38 -0500799 0xFFFF);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500800 qla4xxx_destroy_ipv4_iface(ha);
801 }
Mike Christied00efe32011-07-25 13:48:38 -0500802 break;
Mike Christie2d636732011-10-11 17:55:11 -0500803 case ISCSI_NET_PARAM_VLAN_TAG:
Mike Christied00efe32011-07-25 13:48:38 -0500804 if (iface_param->len != sizeof(init_fw_cb->ipv4_vlan_tag))
805 break;
Vikas Chaudhary6ac73e82011-07-25 13:48:49 -0500806 init_fw_cb->ipv4_vlan_tag =
807 cpu_to_be16(*(uint16_t *)iface_param->value);
808 break;
809 case ISCSI_NET_PARAM_VLAN_ENABLED:
810 if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
811 init_fw_cb->ipv4_ip_opts |=
812 cpu_to_le16(IPOPT_VLAN_TAGGING_ENABLE);
813 else
814 init_fw_cb->ipv4_ip_opts &=
815 cpu_to_le16(~IPOPT_VLAN_TAGGING_ENABLE);
Mike Christied00efe32011-07-25 13:48:38 -0500816 break;
Vikas Chaudhary943c1572011-08-01 03:26:13 -0700817 case ISCSI_NET_PARAM_MTU:
818 init_fw_cb->eth_mtu_size =
819 cpu_to_le16(*(uint16_t *)iface_param->value);
820 break;
Vikas Chaudhary2ada7fc2011-08-01 03:26:19 -0700821 case ISCSI_NET_PARAM_PORT:
822 init_fw_cb->ipv4_port =
823 cpu_to_le16(*(uint16_t *)iface_param->value);
824 break;
Mike Christied00efe32011-07-25 13:48:38 -0500825 default:
826 ql4_printk(KERN_ERR, ha, "Unknown IPv4 param = %d\n",
827 iface_param->param);
828 break;
829 }
830}
831
832static void
833qla4xxx_initcb_to_acb(struct addr_ctrl_blk *init_fw_cb)
834{
835 struct addr_ctrl_blk_def *acb;
836 acb = (struct addr_ctrl_blk_def *)init_fw_cb;
837 memset(acb->reserved1, 0, sizeof(acb->reserved1));
838 memset(acb->reserved2, 0, sizeof(acb->reserved2));
839 memset(acb->reserved3, 0, sizeof(acb->reserved3));
840 memset(acb->reserved4, 0, sizeof(acb->reserved4));
841 memset(acb->reserved5, 0, sizeof(acb->reserved5));
842 memset(acb->reserved6, 0, sizeof(acb->reserved6));
843 memset(acb->reserved7, 0, sizeof(acb->reserved7));
844 memset(acb->reserved8, 0, sizeof(acb->reserved8));
845 memset(acb->reserved9, 0, sizeof(acb->reserved9));
846 memset(acb->reserved10, 0, sizeof(acb->reserved10));
847 memset(acb->reserved11, 0, sizeof(acb->reserved11));
848 memset(acb->reserved12, 0, sizeof(acb->reserved12));
849 memset(acb->reserved13, 0, sizeof(acb->reserved13));
850 memset(acb->reserved14, 0, sizeof(acb->reserved14));
851 memset(acb->reserved15, 0, sizeof(acb->reserved15));
852}
853
854static int
Mike Christie00c31882011-10-06 03:56:59 -0500855qla4xxx_iface_set_param(struct Scsi_Host *shost, void *data, uint32_t len)
Mike Christied00efe32011-07-25 13:48:38 -0500856{
857 struct scsi_qla_host *ha = to_qla_host(shost);
858 int rval = 0;
859 struct iscsi_iface_param_info *iface_param = NULL;
860 struct addr_ctrl_blk *init_fw_cb = NULL;
861 dma_addr_t init_fw_cb_dma;
862 uint32_t mbox_cmd[MBOX_REG_COUNT];
863 uint32_t mbox_sts[MBOX_REG_COUNT];
Mike Christie00c31882011-10-06 03:56:59 -0500864 uint32_t rem = len;
865 struct nlattr *attr;
Mike Christied00efe32011-07-25 13:48:38 -0500866
867 init_fw_cb = dma_alloc_coherent(&ha->pdev->dev,
868 sizeof(struct addr_ctrl_blk),
869 &init_fw_cb_dma, GFP_KERNEL);
870 if (!init_fw_cb) {
871 ql4_printk(KERN_ERR, ha, "%s: Unable to alloc init_cb\n",
872 __func__);
873 return -ENOMEM;
874 }
875
876 memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
877 memset(&mbox_cmd, 0, sizeof(mbox_cmd));
878 memset(&mbox_sts, 0, sizeof(mbox_sts));
879
880 if (qla4xxx_get_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma)) {
881 ql4_printk(KERN_ERR, ha, "%s: get ifcb failed\n", __func__);
882 rval = -EIO;
883 goto exit_init_fw_cb;
884 }
885
Mike Christie00c31882011-10-06 03:56:59 -0500886 nla_for_each_attr(attr, data, len, rem) {
887 iface_param = nla_data(attr);
Mike Christied00efe32011-07-25 13:48:38 -0500888
889 if (iface_param->param_type != ISCSI_NET_PARAM)
890 continue;
891
892 switch (iface_param->iface_type) {
893 case ISCSI_IFACE_TYPE_IPV4:
894 switch (iface_param->iface_num) {
895 case 0:
896 qla4xxx_set_ipv4(ha, iface_param, init_fw_cb);
897 break;
898 default:
899 /* Cannot have more than one IPv4 interface */
900 ql4_printk(KERN_ERR, ha, "Invalid IPv4 iface "
901 "number = %d\n",
902 iface_param->iface_num);
903 break;
904 }
905 break;
906 case ISCSI_IFACE_TYPE_IPV6:
907 switch (iface_param->iface_num) {
908 case 0:
909 case 1:
910 qla4xxx_set_ipv6(ha, iface_param, init_fw_cb);
911 break;
912 default:
913 /* Cannot have more than two IPv6 interface */
914 ql4_printk(KERN_ERR, ha, "Invalid IPv6 iface "
915 "number = %d\n",
916 iface_param->iface_num);
917 break;
918 }
919 break;
920 default:
921 ql4_printk(KERN_ERR, ha, "Invalid iface type\n");
922 break;
923 }
Mike Christied00efe32011-07-25 13:48:38 -0500924 }
925
926 init_fw_cb->cookie = cpu_to_le32(0x11BEAD5A);
927
928 rval = qla4xxx_set_flash(ha, init_fw_cb_dma, FLASH_SEGMENT_IFCB,
929 sizeof(struct addr_ctrl_blk),
930 FLASH_OPT_RMW_COMMIT);
931 if (rval != QLA_SUCCESS) {
932 ql4_printk(KERN_ERR, ha, "%s: set flash mbx failed\n",
933 __func__);
934 rval = -EIO;
935 goto exit_init_fw_cb;
936 }
937
938 qla4xxx_disable_acb(ha);
939
940 qla4xxx_initcb_to_acb(init_fw_cb);
941
942 rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma);
943 if (rval != QLA_SUCCESS) {
944 ql4_printk(KERN_ERR, ha, "%s: set acb mbx failed\n",
945 __func__);
946 rval = -EIO;
947 goto exit_init_fw_cb;
948 }
949
950 memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
951 qla4xxx_update_local_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb,
952 init_fw_cb_dma);
953
954exit_init_fw_cb:
955 dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk),
956 init_fw_cb, init_fw_cb_dma);
957
958 return rval;
959}
960
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500961static int qla4xxx_conn_get_param(struct iscsi_cls_conn *cls_conn,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700962 enum iscsi_param param, char *buf)
963{
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500964 struct iscsi_conn *conn;
965 struct qla_conn *qla_conn;
966 struct sockaddr *dst_addr;
967 int len = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700968
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500969 conn = cls_conn->dd_data;
970 qla_conn = conn->dd_data;
971 dst_addr = &qla_conn->qla_ep->dst_addr;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700972
973 switch (param) {
974 case ISCSI_PARAM_CONN_PORT:
David Somayajuluafaf5a22006-09-19 10:28:00 -0700975 case ISCSI_PARAM_CONN_ADDRESS:
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500976 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
977 dst_addr, param, buf);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700978 default:
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500979 return iscsi_conn_get_param(cls_conn, param, buf);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700980 }
981
982 return len;
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500983
David Somayajuluafaf5a22006-09-19 10:28:00 -0700984}
985
Mike Christie13483732011-12-01 21:38:41 -0600986int qla4xxx_get_ddb_index(struct scsi_qla_host *ha, uint16_t *ddb_index)
987{
988 uint32_t mbx_sts = 0;
989 uint16_t tmp_ddb_index;
990 int ret;
991
992get_ddb_index:
993 tmp_ddb_index = find_first_zero_bit(ha->ddb_idx_map, MAX_DDB_ENTRIES);
994
995 if (tmp_ddb_index >= MAX_DDB_ENTRIES) {
996 DEBUG2(ql4_printk(KERN_INFO, ha,
997 "Free DDB index not available\n"));
998 ret = QLA_ERROR;
999 goto exit_get_ddb_index;
1000 }
1001
1002 if (test_and_set_bit(tmp_ddb_index, ha->ddb_idx_map))
1003 goto get_ddb_index;
1004
1005 DEBUG2(ql4_printk(KERN_INFO, ha,
1006 "Found a free DDB index at %d\n", tmp_ddb_index));
1007 ret = qla4xxx_req_ddb_entry(ha, tmp_ddb_index, &mbx_sts);
1008 if (ret == QLA_ERROR) {
1009 if (mbx_sts == MBOX_STS_COMMAND_ERROR) {
1010 ql4_printk(KERN_INFO, ha,
1011 "DDB index = %d not available trying next\n",
1012 tmp_ddb_index);
1013 goto get_ddb_index;
1014 }
1015 DEBUG2(ql4_printk(KERN_INFO, ha,
1016 "Free FW DDB not available\n"));
1017 }
1018
1019 *ddb_index = tmp_ddb_index;
1020
1021exit_get_ddb_index:
1022 return ret;
1023}
1024
1025static int qla4xxx_match_ipaddress(struct scsi_qla_host *ha,
1026 struct ddb_entry *ddb_entry,
1027 char *existing_ipaddr,
1028 char *user_ipaddr)
1029{
1030 uint8_t dst_ipaddr[IPv6_ADDR_LEN];
1031 char formatted_ipaddr[DDB_IPADDR_LEN];
1032 int status = QLA_SUCCESS, ret = 0;
1033
1034 if (ddb_entry->fw_ddb_entry.options & DDB_OPT_IPV6_DEVICE) {
1035 ret = in6_pton(user_ipaddr, strlen(user_ipaddr), dst_ipaddr,
1036 '\0', NULL);
1037 if (ret == 0) {
1038 status = QLA_ERROR;
1039 goto out_match;
1040 }
1041 ret = sprintf(formatted_ipaddr, "%pI6", dst_ipaddr);
1042 } else {
1043 ret = in4_pton(user_ipaddr, strlen(user_ipaddr), dst_ipaddr,
1044 '\0', NULL);
1045 if (ret == 0) {
1046 status = QLA_ERROR;
1047 goto out_match;
1048 }
1049 ret = sprintf(formatted_ipaddr, "%pI4", dst_ipaddr);
1050 }
1051
1052 if (strcmp(existing_ipaddr, formatted_ipaddr))
1053 status = QLA_ERROR;
1054
1055out_match:
1056 return status;
1057}
1058
1059static int qla4xxx_match_fwdb_session(struct scsi_qla_host *ha,
1060 struct iscsi_cls_conn *cls_conn)
1061{
1062 int idx = 0, max_ddbs, rval;
1063 struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
1064 struct iscsi_session *sess, *existing_sess;
1065 struct iscsi_conn *conn, *existing_conn;
1066 struct ddb_entry *ddb_entry;
1067
1068 sess = cls_sess->dd_data;
1069 conn = cls_conn->dd_data;
1070
1071 if (sess->targetname == NULL ||
1072 conn->persistent_address == NULL ||
1073 conn->persistent_port == 0)
1074 return QLA_ERROR;
1075
1076 max_ddbs = is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
1077 MAX_DEV_DB_ENTRIES;
1078
1079 for (idx = 0; idx < max_ddbs; idx++) {
1080 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
1081 if (ddb_entry == NULL)
1082 continue;
1083
1084 if (ddb_entry->ddb_type != FLASH_DDB)
1085 continue;
1086
1087 existing_sess = ddb_entry->sess->dd_data;
1088 existing_conn = ddb_entry->conn->dd_data;
1089
1090 if (existing_sess->targetname == NULL ||
1091 existing_conn->persistent_address == NULL ||
1092 existing_conn->persistent_port == 0)
1093 continue;
1094
1095 DEBUG2(ql4_printk(KERN_INFO, ha,
1096 "IQN = %s User IQN = %s\n",
1097 existing_sess->targetname,
1098 sess->targetname));
1099
1100 DEBUG2(ql4_printk(KERN_INFO, ha,
1101 "IP = %s User IP = %s\n",
1102 existing_conn->persistent_address,
1103 conn->persistent_address));
1104
1105 DEBUG2(ql4_printk(KERN_INFO, ha,
1106 "Port = %d User Port = %d\n",
1107 existing_conn->persistent_port,
1108 conn->persistent_port));
1109
1110 if (strcmp(existing_sess->targetname, sess->targetname))
1111 continue;
1112 rval = qla4xxx_match_ipaddress(ha, ddb_entry,
1113 existing_conn->persistent_address,
1114 conn->persistent_address);
1115 if (rval == QLA_ERROR)
1116 continue;
1117 if (existing_conn->persistent_port != conn->persistent_port)
1118 continue;
1119 break;
1120 }
1121
1122 if (idx == max_ddbs)
1123 return QLA_ERROR;
1124
1125 DEBUG2(ql4_printk(KERN_INFO, ha,
1126 "Match found in fwdb sessions\n"));
1127 return QLA_SUCCESS;
1128}
1129
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001130static struct iscsi_cls_session *
1131qla4xxx_session_create(struct iscsi_endpoint *ep,
1132 uint16_t cmds_max, uint16_t qdepth,
1133 uint32_t initial_cmdsn)
1134{
1135 struct iscsi_cls_session *cls_sess;
1136 struct scsi_qla_host *ha;
1137 struct qla_endpoint *qla_ep;
1138 struct ddb_entry *ddb_entry;
Mike Christie13483732011-12-01 21:38:41 -06001139 uint16_t ddb_index;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001140 struct iscsi_session *sess;
1141 struct sockaddr *dst_addr;
1142 int ret;
1143
1144 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1145 if (!ep) {
1146 printk(KERN_ERR "qla4xxx: missing ep.\n");
1147 return NULL;
1148 }
1149
1150 qla_ep = ep->dd_data;
1151 dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
1152 ha = to_qla_host(qla_ep->host);
Manish Rangankar736cf362011-10-07 16:55:46 -07001153
Mike Christie13483732011-12-01 21:38:41 -06001154 ret = qla4xxx_get_ddb_index(ha, &ddb_index);
1155 if (ret == QLA_ERROR)
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001156 return NULL;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001157
1158 cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport, qla_ep->host,
1159 cmds_max, sizeof(struct ddb_entry),
1160 sizeof(struct ql4_task_data),
1161 initial_cmdsn, ddb_index);
1162 if (!cls_sess)
1163 return NULL;
1164
1165 sess = cls_sess->dd_data;
1166 ddb_entry = sess->dd_data;
1167 ddb_entry->fw_ddb_index = ddb_index;
1168 ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
1169 ddb_entry->ha = ha;
1170 ddb_entry->sess = cls_sess;
Mike Christie13483732011-12-01 21:38:41 -06001171 ddb_entry->unblock_sess = qla4xxx_unblock_ddb;
1172 ddb_entry->ddb_change = qla4xxx_ddb_change;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001173 cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
1174 ha->fw_ddb_index_map[ddb_entry->fw_ddb_index] = ddb_entry;
1175 ha->tot_ddbs++;
1176
1177 return cls_sess;
1178}
1179
1180static void qla4xxx_session_destroy(struct iscsi_cls_session *cls_sess)
1181{
1182 struct iscsi_session *sess;
1183 struct ddb_entry *ddb_entry;
1184 struct scsi_qla_host *ha;
1185 unsigned long flags;
1186
1187 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1188 sess = cls_sess->dd_data;
1189 ddb_entry = sess->dd_data;
1190 ha = ddb_entry->ha;
1191
Manish Rangankar736cf362011-10-07 16:55:46 -07001192 qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
1193
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001194 spin_lock_irqsave(&ha->hardware_lock, flags);
1195 qla4xxx_free_ddb(ha, ddb_entry);
1196 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1197 iscsi_session_teardown(cls_sess);
1198}
1199
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001200static struct iscsi_cls_conn *
1201qla4xxx_conn_create(struct iscsi_cls_session *cls_sess, uint32_t conn_idx)
1202{
1203 struct iscsi_cls_conn *cls_conn;
1204 struct iscsi_session *sess;
1205 struct ddb_entry *ddb_entry;
1206
1207 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1208 cls_conn = iscsi_conn_setup(cls_sess, sizeof(struct qla_conn),
1209 conn_idx);
Mike Christieff1d0312011-12-01 21:38:43 -06001210 if (!cls_conn)
1211 return NULL;
1212
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001213 sess = cls_sess->dd_data;
1214 ddb_entry = sess->dd_data;
1215 ddb_entry->conn = cls_conn;
1216
1217 return cls_conn;
1218}
1219
1220static int qla4xxx_conn_bind(struct iscsi_cls_session *cls_session,
1221 struct iscsi_cls_conn *cls_conn,
1222 uint64_t transport_fd, int is_leading)
1223{
1224 struct iscsi_conn *conn;
1225 struct qla_conn *qla_conn;
1226 struct iscsi_endpoint *ep;
1227
1228 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1229
1230 if (iscsi_conn_bind(cls_session, cls_conn, is_leading))
1231 return -EINVAL;
1232 ep = iscsi_lookup_endpoint(transport_fd);
1233 conn = cls_conn->dd_data;
1234 qla_conn = conn->dd_data;
1235 qla_conn->qla_ep = ep->dd_data;
1236 return 0;
1237}
1238
1239static int qla4xxx_conn_start(struct iscsi_cls_conn *cls_conn)
1240{
1241 struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
1242 struct iscsi_session *sess;
1243 struct ddb_entry *ddb_entry;
1244 struct scsi_qla_host *ha;
Mike Christie13483732011-12-01 21:38:41 -06001245 struct dev_db_entry *fw_ddb_entry = NULL;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001246 dma_addr_t fw_ddb_entry_dma;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001247 uint32_t mbx_sts = 0;
1248 int ret = 0;
1249 int status = QLA_SUCCESS;
1250
1251 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1252 sess = cls_sess->dd_data;
1253 ddb_entry = sess->dd_data;
1254 ha = ddb_entry->ha;
1255
Mike Christie13483732011-12-01 21:38:41 -06001256 /* Check if we have matching FW DDB, if yes then do not
1257 * login to this target. This could cause target to logout previous
1258 * connection
1259 */
1260 ret = qla4xxx_match_fwdb_session(ha, cls_conn);
1261 if (ret == QLA_SUCCESS) {
1262 ql4_printk(KERN_INFO, ha,
1263 "Session already exist in FW.\n");
1264 ret = -EEXIST;
1265 goto exit_conn_start;
1266 }
1267
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001268 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1269 &fw_ddb_entry_dma, GFP_KERNEL);
1270 if (!fw_ddb_entry) {
1271 ql4_printk(KERN_ERR, ha,
1272 "%s: Unable to allocate dma buffer\n", __func__);
Mike Christie13483732011-12-01 21:38:41 -06001273 ret = -ENOMEM;
1274 goto exit_conn_start;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001275 }
1276
1277 ret = qla4xxx_set_param_ddbentry(ha, ddb_entry, cls_conn, &mbx_sts);
1278 if (ret) {
1279 /* If iscsid is stopped and started then no need to do
1280 * set param again since ddb state will be already
1281 * active and FW does not allow set ddb to an
1282 * active session.
1283 */
1284 if (mbx_sts)
1285 if (ddb_entry->fw_ddb_device_state ==
Manish Rangankarf922da72011-10-07 16:55:49 -07001286 DDB_DS_SESSION_ACTIVE) {
Mike Christie13483732011-12-01 21:38:41 -06001287 ddb_entry->unblock_sess(ddb_entry->sess);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001288 goto exit_set_param;
Manish Rangankarf922da72011-10-07 16:55:49 -07001289 }
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001290
1291 ql4_printk(KERN_ERR, ha, "%s: Failed set param for index[%d]\n",
1292 __func__, ddb_entry->fw_ddb_index);
1293 goto exit_conn_start;
1294 }
1295
1296 status = qla4xxx_conn_open(ha, ddb_entry->fw_ddb_index);
1297 if (status == QLA_ERROR) {
Manish Rangankar0e7e8502011-07-25 13:48:54 -05001298 ql4_printk(KERN_ERR, ha, "%s: Login failed: %s\n", __func__,
1299 sess->targetname);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001300 ret = -EINVAL;
1301 goto exit_conn_start;
1302 }
1303
Manish Rangankar98270ab2011-10-07 16:55:47 -07001304 if (ddb_entry->fw_ddb_device_state == DDB_DS_NO_CONNECTION_ACTIVE)
1305 ddb_entry->fw_ddb_device_state = DDB_DS_LOGIN_IN_PROCESS;
1306
1307 DEBUG2(printk(KERN_INFO "%s: DDB state [%d]\n", __func__,
1308 ddb_entry->fw_ddb_device_state));
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001309
1310exit_set_param:
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001311 ret = 0;
1312
1313exit_conn_start:
Mike Christie13483732011-12-01 21:38:41 -06001314 if (fw_ddb_entry)
1315 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1316 fw_ddb_entry, fw_ddb_entry_dma);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001317 return ret;
1318}
1319
1320static void qla4xxx_conn_destroy(struct iscsi_cls_conn *cls_conn)
1321{
1322 struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
1323 struct iscsi_session *sess;
1324 struct scsi_qla_host *ha;
1325 struct ddb_entry *ddb_entry;
1326 int options;
1327
1328 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1329 sess = cls_sess->dd_data;
1330 ddb_entry = sess->dd_data;
1331 ha = ddb_entry->ha;
1332
1333 options = LOGOUT_OPTION_CLOSE_SESSION;
1334 if (qla4xxx_session_logout_ddb(ha, ddb_entry, options) == QLA_ERROR)
1335 ql4_printk(KERN_ERR, ha, "%s: Logout failed\n", __func__);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001336}
1337
1338static void qla4xxx_task_work(struct work_struct *wdata)
1339{
1340 struct ql4_task_data *task_data;
1341 struct scsi_qla_host *ha;
1342 struct passthru_status *sts;
1343 struct iscsi_task *task;
1344 struct iscsi_hdr *hdr;
1345 uint8_t *data;
1346 uint32_t data_len;
1347 struct iscsi_conn *conn;
1348 int hdr_len;
1349 itt_t itt;
1350
1351 task_data = container_of(wdata, struct ql4_task_data, task_work);
1352 ha = task_data->ha;
1353 task = task_data->task;
1354 sts = &task_data->sts;
1355 hdr_len = sizeof(struct iscsi_hdr);
1356
1357 DEBUG3(printk(KERN_INFO "Status returned\n"));
1358 DEBUG3(qla4xxx_dump_buffer(sts, 64));
1359 DEBUG3(printk(KERN_INFO "Response buffer"));
1360 DEBUG3(qla4xxx_dump_buffer(task_data->resp_buffer, 64));
1361
1362 conn = task->conn;
1363
1364 switch (sts->completionStatus) {
1365 case PASSTHRU_STATUS_COMPLETE:
1366 hdr = (struct iscsi_hdr *)task_data->resp_buffer;
1367 /* Assign back the itt in hdr, until we use the PREASSIGN_TAG */
1368 itt = sts->handle;
1369 hdr->itt = itt;
1370 data = task_data->resp_buffer + hdr_len;
1371 data_len = task_data->resp_len - hdr_len;
1372 iscsi_complete_pdu(conn, hdr, data, data_len);
1373 break;
1374 default:
1375 ql4_printk(KERN_ERR, ha, "Passthru failed status = 0x%x\n",
1376 sts->completionStatus);
1377 break;
1378 }
1379 return;
1380}
1381
1382static int qla4xxx_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
1383{
1384 struct ql4_task_data *task_data;
1385 struct iscsi_session *sess;
1386 struct ddb_entry *ddb_entry;
1387 struct scsi_qla_host *ha;
1388 int hdr_len;
1389
1390 sess = task->conn->session;
1391 ddb_entry = sess->dd_data;
1392 ha = ddb_entry->ha;
1393 task_data = task->dd_data;
1394 memset(task_data, 0, sizeof(struct ql4_task_data));
1395
1396 if (task->sc) {
1397 ql4_printk(KERN_INFO, ha,
1398 "%s: SCSI Commands not implemented\n", __func__);
1399 return -EINVAL;
1400 }
1401
1402 hdr_len = sizeof(struct iscsi_hdr);
1403 task_data->ha = ha;
1404 task_data->task = task;
1405
1406 if (task->data_count) {
1407 task_data->data_dma = dma_map_single(&ha->pdev->dev, task->data,
1408 task->data_count,
1409 PCI_DMA_TODEVICE);
1410 }
1411
1412 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
1413 __func__, task->conn->max_recv_dlength, hdr_len));
1414
Manish Rangankar69ca2162011-10-07 16:55:50 -07001415 task_data->resp_len = task->conn->max_recv_dlength + hdr_len;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001416 task_data->resp_buffer = dma_alloc_coherent(&ha->pdev->dev,
1417 task_data->resp_len,
1418 &task_data->resp_dma,
1419 GFP_ATOMIC);
1420 if (!task_data->resp_buffer)
1421 goto exit_alloc_pdu;
1422
Manish Rangankar69ca2162011-10-07 16:55:50 -07001423 task_data->req_len = task->data_count + hdr_len;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001424 task_data->req_buffer = dma_alloc_coherent(&ha->pdev->dev,
Manish Rangankar69ca2162011-10-07 16:55:50 -07001425 task_data->req_len,
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001426 &task_data->req_dma,
1427 GFP_ATOMIC);
1428 if (!task_data->req_buffer)
1429 goto exit_alloc_pdu;
1430
1431 task->hdr = task_data->req_buffer;
1432
1433 INIT_WORK(&task_data->task_work, qla4xxx_task_work);
1434
1435 return 0;
1436
1437exit_alloc_pdu:
1438 if (task_data->resp_buffer)
1439 dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
1440 task_data->resp_buffer, task_data->resp_dma);
1441
1442 if (task_data->req_buffer)
Manish Rangankar69ca2162011-10-07 16:55:50 -07001443 dma_free_coherent(&ha->pdev->dev, task_data->req_len,
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001444 task_data->req_buffer, task_data->req_dma);
1445 return -ENOMEM;
1446}
1447
1448static void qla4xxx_task_cleanup(struct iscsi_task *task)
1449{
1450 struct ql4_task_data *task_data;
1451 struct iscsi_session *sess;
1452 struct ddb_entry *ddb_entry;
1453 struct scsi_qla_host *ha;
1454 int hdr_len;
1455
1456 hdr_len = sizeof(struct iscsi_hdr);
1457 sess = task->conn->session;
1458 ddb_entry = sess->dd_data;
1459 ha = ddb_entry->ha;
1460 task_data = task->dd_data;
1461
1462 if (task->data_count) {
1463 dma_unmap_single(&ha->pdev->dev, task_data->data_dma,
1464 task->data_count, PCI_DMA_TODEVICE);
1465 }
1466
1467 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
1468 __func__, task->conn->max_recv_dlength, hdr_len));
1469
1470 dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
1471 task_data->resp_buffer, task_data->resp_dma);
Manish Rangankar69ca2162011-10-07 16:55:50 -07001472 dma_free_coherent(&ha->pdev->dev, task_data->req_len,
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001473 task_data->req_buffer, task_data->req_dma);
1474 return;
1475}
1476
1477static int qla4xxx_task_xmit(struct iscsi_task *task)
1478{
1479 struct scsi_cmnd *sc = task->sc;
1480 struct iscsi_session *sess = task->conn->session;
1481 struct ddb_entry *ddb_entry = sess->dd_data;
1482 struct scsi_qla_host *ha = ddb_entry->ha;
1483
1484 if (!sc)
1485 return qla4xxx_send_passthru0(task);
1486
1487 ql4_printk(KERN_INFO, ha, "%s: scsi cmd xmit not implemented\n",
1488 __func__);
1489 return -ENOSYS;
1490}
1491
Mike Christie13483732011-12-01 21:38:41 -06001492static void qla4xxx_copy_fwddb_param(struct scsi_qla_host *ha,
1493 struct dev_db_entry *fw_ddb_entry,
1494 struct iscsi_cls_session *cls_sess,
1495 struct iscsi_cls_conn *cls_conn)
1496{
1497 int buflen = 0;
1498 struct iscsi_session *sess;
1499 struct iscsi_conn *conn;
1500 char ip_addr[DDB_IPADDR_LEN];
1501 uint16_t options = 0;
1502
1503 sess = cls_sess->dd_data;
1504 conn = cls_conn->dd_data;
1505
1506 conn->max_recv_dlength = BYTE_UNITS *
1507 le16_to_cpu(fw_ddb_entry->iscsi_max_rcv_data_seg_len);
1508
1509 conn->max_xmit_dlength = BYTE_UNITS *
1510 le16_to_cpu(fw_ddb_entry->iscsi_max_snd_data_seg_len);
1511
1512 sess->initial_r2t_en =
1513 (BIT_10 & le16_to_cpu(fw_ddb_entry->iscsi_options));
1514
1515 sess->max_r2t = le16_to_cpu(fw_ddb_entry->iscsi_max_outsnd_r2t);
1516
1517 sess->imm_data_en = (BIT_11 & le16_to_cpu(fw_ddb_entry->iscsi_options));
1518
1519 sess->first_burst = BYTE_UNITS *
1520 le16_to_cpu(fw_ddb_entry->iscsi_first_burst_len);
1521
1522 sess->max_burst = BYTE_UNITS *
1523 le16_to_cpu(fw_ddb_entry->iscsi_max_burst_len);
1524
1525 sess->time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
1526
1527 sess->time2retain = le16_to_cpu(fw_ddb_entry->iscsi_def_time2retain);
1528
1529 conn->persistent_port = le16_to_cpu(fw_ddb_entry->port);
1530
1531 sess->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
1532
1533 options = le16_to_cpu(fw_ddb_entry->options);
1534 if (options & DDB_OPT_IPV6_DEVICE)
1535 sprintf(ip_addr, "%pI6", fw_ddb_entry->ip_addr);
1536 else
1537 sprintf(ip_addr, "%pI4", fw_ddb_entry->ip_addr);
1538
1539 iscsi_set_param(cls_conn, ISCSI_PARAM_TARGET_NAME,
1540 (char *)fw_ddb_entry->iscsi_name, buflen);
1541 iscsi_set_param(cls_conn, ISCSI_PARAM_INITIATOR_NAME,
1542 (char *)ha->name_string, buflen);
1543 iscsi_set_param(cls_conn, ISCSI_PARAM_PERSISTENT_ADDRESS,
1544 (char *)ip_addr, buflen);
1545}
1546
1547void qla4xxx_update_session_conn_fwddb_param(struct scsi_qla_host *ha,
1548 struct ddb_entry *ddb_entry)
1549{
1550 struct iscsi_cls_session *cls_sess;
1551 struct iscsi_cls_conn *cls_conn;
1552 uint32_t ddb_state;
1553 dma_addr_t fw_ddb_entry_dma;
1554 struct dev_db_entry *fw_ddb_entry;
1555
1556 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1557 &fw_ddb_entry_dma, GFP_KERNEL);
1558 if (!fw_ddb_entry) {
1559 ql4_printk(KERN_ERR, ha,
1560 "%s: Unable to allocate dma buffer\n", __func__);
1561 goto exit_session_conn_fwddb_param;
1562 }
1563
1564 if (qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index, fw_ddb_entry,
1565 fw_ddb_entry_dma, NULL, NULL, &ddb_state,
1566 NULL, NULL, NULL) == QLA_ERROR) {
1567 DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: failed "
1568 "get_ddb_entry for fw_ddb_index %d\n",
1569 ha->host_no, __func__,
1570 ddb_entry->fw_ddb_index));
1571 goto exit_session_conn_fwddb_param;
1572 }
1573
1574 cls_sess = ddb_entry->sess;
1575
1576 cls_conn = ddb_entry->conn;
1577
1578 /* Update params */
1579 qla4xxx_copy_fwddb_param(ha, fw_ddb_entry, cls_sess, cls_conn);
1580
1581exit_session_conn_fwddb_param:
1582 if (fw_ddb_entry)
1583 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1584 fw_ddb_entry, fw_ddb_entry_dma);
1585}
1586
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001587void qla4xxx_update_session_conn_param(struct scsi_qla_host *ha,
1588 struct ddb_entry *ddb_entry)
1589{
1590 struct iscsi_cls_session *cls_sess;
1591 struct iscsi_cls_conn *cls_conn;
1592 struct iscsi_session *sess;
1593 struct iscsi_conn *conn;
1594 uint32_t ddb_state;
1595 dma_addr_t fw_ddb_entry_dma;
1596 struct dev_db_entry *fw_ddb_entry;
1597
1598 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1599 &fw_ddb_entry_dma, GFP_KERNEL);
1600 if (!fw_ddb_entry) {
1601 ql4_printk(KERN_ERR, ha,
1602 "%s: Unable to allocate dma buffer\n", __func__);
Mike Christie13483732011-12-01 21:38:41 -06001603 goto exit_session_conn_param;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001604 }
1605
1606 if (qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index, fw_ddb_entry,
1607 fw_ddb_entry_dma, NULL, NULL, &ddb_state,
1608 NULL, NULL, NULL) == QLA_ERROR) {
1609 DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: failed "
1610 "get_ddb_entry for fw_ddb_index %d\n",
1611 ha->host_no, __func__,
1612 ddb_entry->fw_ddb_index));
Mike Christie13483732011-12-01 21:38:41 -06001613 goto exit_session_conn_param;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001614 }
1615
1616 cls_sess = ddb_entry->sess;
1617 sess = cls_sess->dd_data;
1618
1619 cls_conn = ddb_entry->conn;
1620 conn = cls_conn->dd_data;
1621
Mike Christie13483732011-12-01 21:38:41 -06001622 /* Update timers after login */
1623 ddb_entry->default_relogin_timeout =
1624 le16_to_cpu(fw_ddb_entry->def_timeout);
1625 ddb_entry->default_time2wait =
1626 le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
1627
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001628 /* Update params */
1629 conn->max_recv_dlength = BYTE_UNITS *
1630 le16_to_cpu(fw_ddb_entry->iscsi_max_rcv_data_seg_len);
1631
1632 conn->max_xmit_dlength = BYTE_UNITS *
1633 le16_to_cpu(fw_ddb_entry->iscsi_max_snd_data_seg_len);
1634
1635 sess->initial_r2t_en =
1636 (BIT_10 & le16_to_cpu(fw_ddb_entry->iscsi_options));
1637
1638 sess->max_r2t = le16_to_cpu(fw_ddb_entry->iscsi_max_outsnd_r2t);
1639
1640 sess->imm_data_en = (BIT_11 & le16_to_cpu(fw_ddb_entry->iscsi_options));
1641
1642 sess->first_burst = BYTE_UNITS *
1643 le16_to_cpu(fw_ddb_entry->iscsi_first_burst_len);
1644
1645 sess->max_burst = BYTE_UNITS *
1646 le16_to_cpu(fw_ddb_entry->iscsi_max_burst_len);
1647
1648 sess->time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
1649
1650 sess->time2retain = le16_to_cpu(fw_ddb_entry->iscsi_def_time2retain);
1651
1652 sess->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
1653
1654 memcpy(sess->initiatorname, ha->name_string,
1655 min(sizeof(ha->name_string), sizeof(sess->initiatorname)));
Mike Christie13483732011-12-01 21:38:41 -06001656
1657exit_session_conn_param:
1658 if (fw_ddb_entry)
1659 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1660 fw_ddb_entry, fw_ddb_entry_dma);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001661}
1662
David Somayajuluafaf5a22006-09-19 10:28:00 -07001663/*
1664 * Timer routines
1665 */
1666
1667static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
1668 unsigned long interval)
1669{
1670 DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
1671 __func__, ha->host->host_no));
1672 init_timer(&ha->timer);
1673 ha->timer.expires = jiffies + interval * HZ;
1674 ha->timer.data = (unsigned long)ha;
1675 ha->timer.function = (void (*)(unsigned long))func;
1676 add_timer(&ha->timer);
1677 ha->timer_active = 1;
1678}
1679
1680static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
1681{
1682 del_timer_sync(&ha->timer);
1683 ha->timer_active = 0;
1684}
1685
1686/***
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001687 * qla4xxx_mark_device_missing - blocks the session
1688 * @cls_session: Pointer to the session to be blocked
David Somayajuluafaf5a22006-09-19 10:28:00 -07001689 * @ddb_entry: Pointer to device database entry
1690 *
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301691 * This routine marks a device missing and close connection.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001692 **/
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001693void qla4xxx_mark_device_missing(struct iscsi_cls_session *cls_session)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001694{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001695 iscsi_block_session(cls_session);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001696}
1697
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301698/**
1699 * qla4xxx_mark_all_devices_missing - mark all devices as missing.
1700 * @ha: Pointer to host adapter structure.
1701 *
1702 * This routine marks a device missing and resets the relogin retry count.
1703 **/
1704void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
1705{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001706 iscsi_host_for_each_session(ha->host, qla4xxx_mark_device_missing);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301707}
1708
David Somayajuluafaf5a22006-09-19 10:28:00 -07001709static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
1710 struct ddb_entry *ddb_entry,
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001711 struct scsi_cmnd *cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001712{
1713 struct srb *srb;
1714
1715 srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
1716 if (!srb)
1717 return srb;
1718
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301719 kref_init(&srb->srb_ref);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001720 srb->ha = ha;
1721 srb->ddb = ddb_entry;
1722 srb->cmd = cmd;
1723 srb->flags = 0;
Vikas Chaudhary53698872010-04-28 11:41:59 +05301724 CMD_SP(cmd) = (void *)srb;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001725
1726 return srb;
1727}
1728
1729static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
1730{
1731 struct scsi_cmnd *cmd = srb->cmd;
1732
1733 if (srb->flags & SRB_DMA_VALID) {
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +09001734 scsi_dma_unmap(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001735 srb->flags &= ~SRB_DMA_VALID;
1736 }
Vikas Chaudhary53698872010-04-28 11:41:59 +05301737 CMD_SP(cmd) = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001738}
1739
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301740void qla4xxx_srb_compl(struct kref *ref)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001741{
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301742 struct srb *srb = container_of(ref, struct srb, srb_ref);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001743 struct scsi_cmnd *cmd = srb->cmd;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301744 struct scsi_qla_host *ha = srb->ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001745
1746 qla4xxx_srb_free_dma(ha, srb);
1747
1748 mempool_free(srb, ha->srb_mempool);
1749
1750 cmd->scsi_done(cmd);
1751}
1752
1753/**
1754 * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001755 * @host: scsi host
David Somayajuluafaf5a22006-09-19 10:28:00 -07001756 * @cmd: Pointer to Linux's SCSI command structure
David Somayajuluafaf5a22006-09-19 10:28:00 -07001757 *
1758 * Remarks:
1759 * This routine is invoked by Linux to send a SCSI command to the driver.
1760 * The mid-level driver tries to ensure that queuecommand never gets
1761 * invoked concurrently with itself or the interrupt handler (although
1762 * the interrupt handler may call this routine as part of request-
1763 * completion handling). Unfortunely, it sometimes calls the scheduler
1764 * in interrupt context which is a big NO! NO!.
1765 **/
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001766static int qla4xxx_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001767{
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001768 struct scsi_qla_host *ha = to_qla_host(host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001769 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christie7fb19212008-01-31 13:36:45 -06001770 struct iscsi_cls_session *sess = ddb_entry->sess;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001771 struct srb *srb;
1772 int rval;
1773
Lalit Chandivade2232be02010-07-30 14:38:47 +05301774 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1775 if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
1776 cmd->result = DID_NO_CONNECT << 16;
1777 else
1778 cmd->result = DID_REQUEUE << 16;
1779 goto qc_fail_command;
1780 }
1781
Mike Christie7fb19212008-01-31 13:36:45 -06001782 if (!sess) {
1783 cmd->result = DID_IMM_RETRY << 16;
1784 goto qc_fail_command;
1785 }
1786
1787 rval = iscsi_session_chkready(sess);
1788 if (rval) {
1789 cmd->result = rval;
1790 goto qc_fail_command;
1791 }
1792
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301793 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1794 test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
1795 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1796 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
1797 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
1798 !test_bit(AF_ONLINE, &ha->flags) ||
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001799 !test_bit(AF_LINK_UP, &ha->flags) ||
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301800 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
David C Somayajulu477ffb92007-01-22 12:26:11 -08001801 goto qc_host_busy;
1802
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001803 srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001804 if (!srb)
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001805 goto qc_host_busy;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001806
1807 rval = qla4xxx_send_command_to_isp(ha, srb);
1808 if (rval != QLA_SUCCESS)
1809 goto qc_host_busy_free_sp;
1810
David Somayajuluafaf5a22006-09-19 10:28:00 -07001811 return 0;
1812
1813qc_host_busy_free_sp:
1814 qla4xxx_srb_free_dma(ha, srb);
1815 mempool_free(srb, ha->srb_mempool);
1816
David Somayajuluafaf5a22006-09-19 10:28:00 -07001817qc_host_busy:
1818 return SCSI_MLQUEUE_HOST_BUSY;
1819
1820qc_fail_command:
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001821 cmd->scsi_done(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001822
1823 return 0;
1824}
1825
1826/**
1827 * qla4xxx_mem_free - frees memory allocated to adapter
1828 * @ha: Pointer to host adapter structure.
1829 *
1830 * Frees memory previously allocated by qla4xxx_mem_alloc
1831 **/
1832static void qla4xxx_mem_free(struct scsi_qla_host *ha)
1833{
1834 if (ha->queues)
1835 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
1836 ha->queues_dma);
1837
1838 ha->queues_len = 0;
1839 ha->queues = NULL;
1840 ha->queues_dma = 0;
1841 ha->request_ring = NULL;
1842 ha->request_dma = 0;
1843 ha->response_ring = NULL;
1844 ha->response_dma = 0;
1845 ha->shadow_regs = NULL;
1846 ha->shadow_regs_dma = 0;
1847
1848 /* Free srb pool. */
1849 if (ha->srb_mempool)
1850 mempool_destroy(ha->srb_mempool);
1851
1852 ha->srb_mempool = NULL;
1853
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001854 if (ha->chap_dma_pool)
1855 dma_pool_destroy(ha->chap_dma_pool);
1856
Lalit Chandivade45494152011-10-07 16:55:42 -07001857 if (ha->chap_list)
1858 vfree(ha->chap_list);
1859 ha->chap_list = NULL;
1860
Mike Christie13483732011-12-01 21:38:41 -06001861 if (ha->fw_ddb_dma_pool)
1862 dma_pool_destroy(ha->fw_ddb_dma_pool);
1863
David Somayajuluafaf5a22006-09-19 10:28:00 -07001864 /* release io space registers */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301865 if (is_qla8022(ha)) {
1866 if (ha->nx_pcibase)
1867 iounmap(
1868 (struct device_reg_82xx __iomem *)ha->nx_pcibase);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301869 } else if (ha->reg)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001870 iounmap(ha->reg);
1871 pci_release_regions(ha->pdev);
1872}
1873
1874/**
1875 * qla4xxx_mem_alloc - allocates memory for use by adapter.
1876 * @ha: Pointer to host adapter structure
1877 *
1878 * Allocates DMA memory for request and response queues. Also allocates memory
1879 * for srbs.
1880 **/
1881static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
1882{
1883 unsigned long align;
1884
1885 /* Allocate contiguous block of DMA memory for queues. */
1886 ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
1887 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
1888 sizeof(struct shadow_regs) +
1889 MEM_ALIGN_VALUE +
1890 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
1891 ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
1892 &ha->queues_dma, GFP_KERNEL);
1893 if (ha->queues == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301894 ql4_printk(KERN_WARNING, ha,
1895 "Memory Allocation failed - queues.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001896
1897 goto mem_alloc_error_exit;
1898 }
1899 memset(ha->queues, 0, ha->queues_len);
1900
1901 /*
1902 * As per RISC alignment requirements -- the bus-address must be a
1903 * multiple of the request-ring size (in bytes).
1904 */
1905 align = 0;
1906 if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
1907 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
1908 (MEM_ALIGN_VALUE - 1));
1909
1910 /* Update request and response queue pointers. */
1911 ha->request_dma = ha->queues_dma + align;
1912 ha->request_ring = (struct queue_entry *) (ha->queues + align);
1913 ha->response_dma = ha->queues_dma + align +
1914 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
1915 ha->response_ring = (struct queue_entry *) (ha->queues + align +
1916 (REQUEST_QUEUE_DEPTH *
1917 QUEUE_SIZE));
1918 ha->shadow_regs_dma = ha->queues_dma + align +
1919 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
1920 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
1921 ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
1922 (REQUEST_QUEUE_DEPTH *
1923 QUEUE_SIZE) +
1924 (RESPONSE_QUEUE_DEPTH *
1925 QUEUE_SIZE));
1926
1927 /* Allocate memory for srb pool. */
1928 ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
1929 mempool_free_slab, srb_cachep);
1930 if (ha->srb_mempool == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301931 ql4_printk(KERN_WARNING, ha,
1932 "Memory Allocation failed - SRB Pool.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001933
1934 goto mem_alloc_error_exit;
1935 }
1936
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001937 ha->chap_dma_pool = dma_pool_create("ql4_chap", &ha->pdev->dev,
1938 CHAP_DMA_BLOCK_SIZE, 8, 0);
1939
1940 if (ha->chap_dma_pool == NULL) {
1941 ql4_printk(KERN_WARNING, ha,
1942 "%s: chap_dma_pool allocation failed..\n", __func__);
1943 goto mem_alloc_error_exit;
1944 }
1945
Mike Christie13483732011-12-01 21:38:41 -06001946 ha->fw_ddb_dma_pool = dma_pool_create("ql4_fw_ddb", &ha->pdev->dev,
1947 DDB_DMA_BLOCK_SIZE, 8, 0);
1948
1949 if (ha->fw_ddb_dma_pool == NULL) {
1950 ql4_printk(KERN_WARNING, ha,
1951 "%s: fw_ddb_dma_pool allocation failed..\n",
1952 __func__);
1953 goto mem_alloc_error_exit;
1954 }
1955
David Somayajuluafaf5a22006-09-19 10:28:00 -07001956 return QLA_SUCCESS;
1957
1958mem_alloc_error_exit:
1959 qla4xxx_mem_free(ha);
1960 return QLA_ERROR;
1961}
1962
1963/**
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301964 * qla4_8xxx_check_fw_alive - Check firmware health
1965 * @ha: Pointer to host adapter structure.
1966 *
1967 * Context: Interrupt
1968 **/
1969static void qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
1970{
1971 uint32_t fw_heartbeat_counter, halt_status;
1972
1973 fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
Lalit Chandivade2232be02010-07-30 14:38:47 +05301974 /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
1975 if (fw_heartbeat_counter == 0xffffffff) {
1976 DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
1977 "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
1978 ha->host_no, __func__));
1979 return;
1980 }
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301981
1982 if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
1983 ha->seconds_since_last_heartbeat++;
1984 /* FW not alive after 2 seconds */
1985 if (ha->seconds_since_last_heartbeat == 2) {
1986 ha->seconds_since_last_heartbeat = 0;
1987 halt_status = qla4_8xxx_rd_32(ha,
Vikas Chaudhary68d92eb2011-05-17 23:17:05 -07001988 QLA82XX_PEG_HALT_STATUS1);
1989
1990 ql4_printk(KERN_INFO, ha,
1991 "scsi(%ld): %s, Dumping hw/fw registers:\n "
1992 " PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2:"
1993 " 0x%x,\n PEG_NET_0_PC: 0x%x, PEG_NET_1_PC:"
1994 " 0x%x,\n PEG_NET_2_PC: 0x%x, PEG_NET_3_PC:"
1995 " 0x%x,\n PEG_NET_4_PC: 0x%x\n",
1996 ha->host_no, __func__, halt_status,
1997 qla4_8xxx_rd_32(ha,
1998 QLA82XX_PEG_HALT_STATUS2),
1999 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_0 +
2000 0x3c),
2001 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_1 +
2002 0x3c),
2003 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_2 +
2004 0x3c),
2005 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_3 +
2006 0x3c),
2007 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_4 +
2008 0x3c));
Nilesh Javali21033632010-07-30 14:28:07 +05302009
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302010 /* Since we cannot change dev_state in interrupt
2011 * context, set appropriate DPC flag then wakeup
2012 * DPC */
2013 if (halt_status & HALT_STATUS_UNRECOVERABLE)
2014 set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
2015 else {
2016 printk("scsi%ld: %s: detect abort needed!\n",
2017 ha->host_no, __func__);
2018 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2019 }
2020 qla4xxx_wake_dpc(ha);
Nilesh Javali21033632010-07-30 14:28:07 +05302021 qla4xxx_mailbox_premature_completion(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302022 }
Lalit Chandivade99457d72010-10-06 22:49:32 -07002023 } else
2024 ha->seconds_since_last_heartbeat = 0;
2025
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302026 ha->fw_heartbeat_counter = fw_heartbeat_counter;
2027}
2028
2029/**
2030 * qla4_8xxx_watchdog - Poll dev state
2031 * @ha: Pointer to host adapter structure.
2032 *
2033 * Context: Interrupt
2034 **/
2035void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
2036{
2037 uint32_t dev_state;
2038
2039 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
2040
2041 /* don't poll if reset is going on */
Lalit Chandivaded56a1f72010-12-02 22:12:45 -08002042 if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
2043 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
Vikas Chaudhary977f46a2011-05-17 23:17:08 -07002044 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags))) {
Vikas Chaudharyf4f5df232010-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);
2052 qla4xxx_mailbox_premature_completion(ha);
2053 }
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302054 } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
2055 !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
Vikas Chaudhary3930b8c2010-12-02 22:12:47 -08002056 ql4_printk(KERN_INFO, ha, "%s: HW State: NEED QUIES!\n",
2057 __func__);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302058 set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
2059 qla4xxx_wake_dpc(ha);
2060 } else {
2061 /* Check firmware health */
2062 qla4_8xxx_check_fw_alive(ha);
2063 }
2064 }
2065}
2066
Mike Christie13483732011-12-01 21:38:41 -06002067void qla4xxx_check_relogin_flash_ddb(struct iscsi_cls_session *cls_sess)
2068{
2069 struct iscsi_session *sess;
2070 struct ddb_entry *ddb_entry;
2071 struct scsi_qla_host *ha;
2072
2073 sess = cls_sess->dd_data;
2074 ddb_entry = sess->dd_data;
2075 ha = ddb_entry->ha;
2076
2077 if (!(ddb_entry->ddb_type == FLASH_DDB))
2078 return;
2079
2080 if (adapter_up(ha) && !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
2081 !iscsi_is_session_online(cls_sess)) {
2082 if (atomic_read(&ddb_entry->retry_relogin_timer) !=
2083 INVALID_ENTRY) {
2084 if (atomic_read(&ddb_entry->retry_relogin_timer) ==
2085 0) {
2086 atomic_set(&ddb_entry->retry_relogin_timer,
2087 INVALID_ENTRY);
2088 set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
2089 set_bit(DF_RELOGIN, &ddb_entry->flags);
2090 DEBUG2(ql4_printk(KERN_INFO, ha,
2091 "%s: index [%d] login device\n",
2092 __func__, ddb_entry->fw_ddb_index));
2093 } else
2094 atomic_dec(&ddb_entry->retry_relogin_timer);
2095 }
2096 }
2097
2098 /* Wait for relogin to timeout */
2099 if (atomic_read(&ddb_entry->relogin_timer) &&
2100 (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
2101 /*
2102 * If the relogin times out and the device is
2103 * still NOT ONLINE then try and relogin again.
2104 */
2105 if (!iscsi_is_session_online(cls_sess)) {
2106 /* Reset retry relogin timer */
2107 atomic_inc(&ddb_entry->relogin_retry_count);
2108 DEBUG2(ql4_printk(KERN_INFO, ha,
2109 "%s: index[%d] relogin timed out-retrying"
2110 " relogin (%d), retry (%d)\n", __func__,
2111 ddb_entry->fw_ddb_index,
2112 atomic_read(&ddb_entry->relogin_retry_count),
2113 ddb_entry->default_time2wait + 4));
2114 set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
2115 atomic_set(&ddb_entry->retry_relogin_timer,
2116 ddb_entry->default_time2wait + 4);
2117 }
2118 }
2119}
2120
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302121/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002122 * qla4xxx_timer - checks every second for work to do.
2123 * @ha: Pointer to host adapter structure.
2124 **/
2125static void qla4xxx_timer(struct scsi_qla_host *ha)
2126{
David Somayajuluafaf5a22006-09-19 10:28:00 -07002127 int start_dpc = 0;
Lalit Chandivade2232be02010-07-30 14:38:47 +05302128 uint16_t w;
2129
Mike Christie13483732011-12-01 21:38:41 -06002130 iscsi_host_for_each_session(ha->host, qla4xxx_check_relogin_flash_ddb);
2131
Lalit Chandivade2232be02010-07-30 14:38:47 +05302132 /* If we are in the middle of AER/EEH processing
2133 * skip any processing and reschedule the timer
2134 */
2135 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
2136 mod_timer(&ha->timer, jiffies + HZ);
2137 return;
2138 }
2139
2140 /* Hardware read to trigger an EEH error during mailbox waits. */
2141 if (!pci_channel_offline(ha->pdev))
2142 pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002143
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302144 if (is_qla8022(ha)) {
2145 qla4_8xxx_watchdog(ha);
2146 }
2147
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302148 if (!is_qla8022(ha)) {
2149 /* Check for heartbeat interval. */
2150 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
2151 ha->heartbeat_interval != 0) {
2152 ha->seconds_since_last_heartbeat++;
2153 if (ha->seconds_since_last_heartbeat >
2154 ha->heartbeat_interval + 2)
2155 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2156 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002157 }
2158
David Somayajuluafaf5a22006-09-19 10:28:00 -07002159 /* Wakeup the dpc routine for this adapter, if needed. */
Lalit Chandivade1b468072011-05-17 23:17:09 -07002160 if (start_dpc ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07002161 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
2162 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
2163 test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302164 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07002165 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
2166 test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05302167 test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302168 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
2169 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
Lalit Chandivade1b468072011-05-17 23:17:09 -07002170 test_bit(DPC_AEN, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002171 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
2172 " - dpc flags = 0x%lx\n",
2173 ha->host_no, __func__, ha->dpc_flags));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302174 qla4xxx_wake_dpc(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002175 }
2176
2177 /* Reschedule timer thread to call us back in one second */
2178 mod_timer(&ha->timer, jiffies + HZ);
2179
2180 DEBUG2(ha->seconds_since_last_intr++);
2181}
2182
2183/**
2184 * qla4xxx_cmd_wait - waits for all outstanding commands to complete
2185 * @ha: Pointer to host adapter structure.
2186 *
2187 * This routine stalls the driver until all outstanding commands are returned.
2188 * Caller must release the Hardware Lock prior to calling this routine.
2189 **/
2190static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
2191{
2192 uint32_t index = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002193 unsigned long flags;
2194 struct scsi_cmnd *cmd;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002195
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302196 unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
2197
2198 DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
2199 "complete\n", WAIT_CMD_TOV));
2200
2201 while (!time_after_eq(jiffies, wtime)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002202 spin_lock_irqsave(&ha->hardware_lock, flags);
2203 /* Find a command that hasn't completed. */
2204 for (index = 0; index < ha->host->can_queue; index++) {
2205 cmd = scsi_host_find_tag(ha->host, index);
Mike Christiea1e00632010-10-26 05:45:30 -07002206 /*
2207 * We cannot just check if the index is valid,
2208 * becase if we are run from the scsi eh, then
2209 * the scsi/block layer is going to prevent
2210 * the tag from being released.
2211 */
2212 if (cmd != NULL && CMD_SP(cmd))
David Somayajuluafaf5a22006-09-19 10:28:00 -07002213 break;
2214 }
2215 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2216
2217 /* If No Commands are pending, wait is complete */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302218 if (index == ha->host->can_queue)
2219 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002220
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302221 msleep(1000);
2222 }
2223 /* If we timed out on waiting for commands to come back
2224 * return ERROR. */
2225 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002226}
2227
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302228int qla4xxx_hw_reset(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002229{
David Somayajuluafaf5a22006-09-19 10:28:00 -07002230 uint32_t ctrl_status;
David C Somayajulu477ffb92007-01-22 12:26:11 -08002231 unsigned long flags = 0;
2232
2233 DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -07002234
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302235 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
2236 return QLA_ERROR;
2237
David Somayajuluafaf5a22006-09-19 10:28:00 -07002238 spin_lock_irqsave(&ha->hardware_lock, flags);
2239
2240 /*
2241 * If the SCSI Reset Interrupt bit is set, clear it.
2242 * Otherwise, the Soft Reset won't work.
2243 */
2244 ctrl_status = readw(&ha->reg->ctrl_status);
2245 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
2246 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
2247
2248 /* Issue Soft Reset */
2249 writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
2250 readl(&ha->reg->ctrl_status);
2251
2252 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302253 return QLA_SUCCESS;
David C Somayajulu477ffb92007-01-22 12:26:11 -08002254}
2255
2256/**
2257 * qla4xxx_soft_reset - performs soft reset.
2258 * @ha: Pointer to host adapter structure.
2259 **/
2260int qla4xxx_soft_reset(struct scsi_qla_host *ha)
2261{
2262 uint32_t max_wait_time;
2263 unsigned long flags = 0;
Vikas Chaudharyf931c532010-10-06 22:48:07 -07002264 int status;
David C Somayajulu477ffb92007-01-22 12:26:11 -08002265 uint32_t ctrl_status;
2266
Vikas Chaudharyf931c532010-10-06 22:48:07 -07002267 status = qla4xxx_hw_reset(ha);
2268 if (status != QLA_SUCCESS)
2269 return status;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002270
Vikas Chaudharyf931c532010-10-06 22:48:07 -07002271 status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002272 /* Wait until the Network Reset Intr bit is cleared */
2273 max_wait_time = RESET_INTR_TOV;
2274 do {
2275 spin_lock_irqsave(&ha->hardware_lock, flags);
2276 ctrl_status = readw(&ha->reg->ctrl_status);
2277 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2278
2279 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
2280 break;
2281
2282 msleep(1000);
2283 } while ((--max_wait_time));
2284
2285 if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
2286 DEBUG2(printk(KERN_WARNING
2287 "scsi%ld: Network Reset Intr not cleared by "
2288 "Network function, clearing it now!\n",
2289 ha->host_no));
2290 spin_lock_irqsave(&ha->hardware_lock, flags);
2291 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
2292 readl(&ha->reg->ctrl_status);
2293 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2294 }
2295
2296 /* Wait until the firmware tells us the Soft Reset is done */
2297 max_wait_time = SOFT_RESET_TOV;
2298 do {
2299 spin_lock_irqsave(&ha->hardware_lock, flags);
2300 ctrl_status = readw(&ha->reg->ctrl_status);
2301 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2302
2303 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
2304 status = QLA_SUCCESS;
2305 break;
2306 }
2307
2308 msleep(1000);
2309 } while ((--max_wait_time));
2310
2311 /*
2312 * Also, make sure that the SCSI Reset Interrupt bit has been cleared
2313 * after the soft reset has taken place.
2314 */
2315 spin_lock_irqsave(&ha->hardware_lock, flags);
2316 ctrl_status = readw(&ha->reg->ctrl_status);
2317 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
2318 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
2319 readl(&ha->reg->ctrl_status);
2320 }
2321 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2322
2323 /* If soft reset fails then most probably the bios on other
2324 * function is also enabled.
2325 * Since the initialization is sequential the other fn
2326 * wont be able to acknowledge the soft reset.
2327 * Issue a force soft reset to workaround this scenario.
2328 */
2329 if (max_wait_time == 0) {
2330 /* Issue Force Soft Reset */
2331 spin_lock_irqsave(&ha->hardware_lock, flags);
2332 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
2333 readl(&ha->reg->ctrl_status);
2334 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2335 /* Wait until the firmware tells us the Soft Reset is done */
2336 max_wait_time = SOFT_RESET_TOV;
2337 do {
2338 spin_lock_irqsave(&ha->hardware_lock, flags);
2339 ctrl_status = readw(&ha->reg->ctrl_status);
2340 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2341
2342 if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
2343 status = QLA_SUCCESS;
2344 break;
2345 }
2346
2347 msleep(1000);
2348 } while ((--max_wait_time));
2349 }
2350
2351 return status;
2352}
2353
2354/**
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302355 * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
David Somayajuluafaf5a22006-09-19 10:28:00 -07002356 * @ha: Pointer to host adapter structure.
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302357 * @res: returned scsi status
David Somayajuluafaf5a22006-09-19 10:28:00 -07002358 *
2359 * This routine is called just prior to a HARD RESET to return all
2360 * outstanding commands back to the Operating System.
2361 * Caller should make sure that the following locks are released
2362 * before this calling routine: Hardware lock, and io_request_lock.
2363 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302364static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002365{
2366 struct srb *srb;
2367 int i;
2368 unsigned long flags;
2369
2370 spin_lock_irqsave(&ha->hardware_lock, flags);
2371 for (i = 0; i < ha->host->can_queue; i++) {
2372 srb = qla4xxx_del_from_active_array(ha, i);
2373 if (srb != NULL) {
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302374 srb->cmd->result = res;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302375 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002376 }
2377 }
2378 spin_unlock_irqrestore(&ha->hardware_lock, flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002379}
2380
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302381void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
2382{
2383 clear_bit(AF_ONLINE, &ha->flags);
2384
2385 /* Disable the board */
2386 ql4_printk(KERN_INFO, ha, "Disabling the board\n");
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302387
2388 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
2389 qla4xxx_mark_all_devices_missing(ha);
2390 clear_bit(AF_INIT_DONE, &ha->flags);
2391}
2392
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002393static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session)
2394{
2395 struct iscsi_session *sess;
2396 struct ddb_entry *ddb_entry;
2397
2398 sess = cls_session->dd_data;
2399 ddb_entry = sess->dd_data;
2400 ddb_entry->fw_ddb_device_state = DDB_DS_SESSION_FAILED;
Mike Christie13483732011-12-01 21:38:41 -06002401
2402 if (ddb_entry->ddb_type == FLASH_DDB)
2403 iscsi_block_session(ddb_entry->sess);
2404 else
2405 iscsi_session_failure(cls_session->dd_data,
2406 ISCSI_ERR_CONN_FAILED);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002407}
2408
David Somayajuluafaf5a22006-09-19 10:28:00 -07002409/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002410 * qla4xxx_recover_adapter - recovers adapter after a fatal error
2411 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07002412 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302413static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002414{
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302415 int status = QLA_ERROR;
2416 uint8_t reset_chip = 0;
Sarang Radke8e0f3a62011-12-01 22:42:09 -08002417 uint32_t dev_state;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002418
2419 /* Stall incoming I/O until we are done */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302420 scsi_block_requests(ha->host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002421 clear_bit(AF_ONLINE, &ha->flags);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002422 clear_bit(AF_LINK_UP, &ha->flags);
Mike Christie50a29ae2008-03-04 13:26:53 -06002423
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302424 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -07002425
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302426 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002427
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002428 iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
2429
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302430 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
2431 reset_chip = 1;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002432
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302433 /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
2434 * do not reset adapter, jump to initialize_adapter */
2435 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
2436 status = QLA_SUCCESS;
2437 goto recover_ha_init_adapter;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002438 }
2439
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302440 /* For the ISP-82xx adapter, issue a stop_firmware if invoked
2441 * from eh_host_reset or ioctl module */
2442 if (is_qla8022(ha) && !reset_chip &&
2443 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
2444
2445 DEBUG2(ql4_printk(KERN_INFO, ha,
2446 "scsi%ld: %s - Performing stop_firmware...\n",
2447 ha->host_no, __func__));
2448 status = ha->isp_ops->reset_firmware(ha);
2449 if (status == QLA_SUCCESS) {
Nilesh Javali2bd1e2b2010-10-06 22:49:20 -07002450 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
2451 qla4xxx_cmd_wait(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302452 ha->isp_ops->disable_intrs(ha);
2453 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2454 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
2455 } else {
2456 /* If the stop_firmware fails then
2457 * reset the entire chip */
2458 reset_chip = 1;
2459 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2460 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2461 }
2462 }
2463
2464 /* Issue full chip reset if recovering from a catastrophic error,
2465 * or if stop_firmware fails for ISP-82xx.
2466 * This is the default case for ISP-4xxx */
2467 if (!is_qla8022(ha) || reset_chip) {
Nilesh Javali2bd1e2b2010-10-06 22:49:20 -07002468 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
2469 qla4xxx_cmd_wait(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302470 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2471 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
2472 DEBUG2(ql4_printk(KERN_INFO, ha,
2473 "scsi%ld: %s - Performing chip reset..\n",
2474 ha->host_no, __func__));
2475 status = ha->isp_ops->reset_chip(ha);
2476 }
2477
2478 /* Flush any pending ddb changed AENs */
2479 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2480
2481recover_ha_init_adapter:
2482 /* Upon successful firmware/chip reset, re-initialize the adapter */
2483 if (status == QLA_SUCCESS) {
2484 /* For ISP-4xxx, force function 1 to always initialize
2485 * before function 3 to prevent both funcions from
2486 * stepping on top of the other */
2487 if (!is_qla8022(ha) && (ha->mac_index == 3))
2488 ssleep(6);
2489
2490 /* NOTE: AF_ONLINE flag set upon successful completion of
2491 * qla4xxx_initialize_adapter */
Mike Christie13483732011-12-01 21:38:41 -06002492 status = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302493 }
2494
2495 /* Retry failed adapter initialization, if necessary
2496 * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
2497 * case to prevent ping-pong resets between functions */
2498 if (!test_bit(AF_ONLINE, &ha->flags) &&
2499 !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002500 /* Adapter initialization failed, see if we can retry
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302501 * resetting the ha.
2502 * Since we don't want to block the DPC for too long
2503 * with multiple resets in the same thread,
2504 * utilize DPC to retry */
Sarang Radke8e0f3a62011-12-01 22:42:09 -08002505 if (is_qla8022(ha)) {
2506 qla4_8xxx_idc_lock(ha);
2507 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
2508 qla4_8xxx_idc_unlock(ha);
2509 if (dev_state == QLA82XX_DEV_FAILED) {
2510 ql4_printk(KERN_INFO, ha, "%s: don't retry "
2511 "recover adapter. H/W is in Failed "
2512 "state\n", __func__);
2513 qla4xxx_dead_adapter_cleanup(ha);
2514 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
2515 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
2516 clear_bit(DPC_RESET_HA_FW_CONTEXT,
2517 &ha->dpc_flags);
2518 status = QLA_ERROR;
2519
2520 goto exit_recover;
2521 }
2522 }
2523
David Somayajuluafaf5a22006-09-19 10:28:00 -07002524 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
2525 ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
2526 DEBUG2(printk("scsi%ld: recover adapter - retrying "
2527 "(%d) more times\n", ha->host_no,
2528 ha->retry_reset_ha_cnt));
2529 set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
2530 status = QLA_ERROR;
2531 } else {
2532 if (ha->retry_reset_ha_cnt > 0) {
2533 /* Schedule another Reset HA--DPC will retry */
2534 ha->retry_reset_ha_cnt--;
2535 DEBUG2(printk("scsi%ld: recover adapter - "
2536 "retry remaining %d\n",
2537 ha->host_no,
2538 ha->retry_reset_ha_cnt));
2539 status = QLA_ERROR;
2540 }
2541
2542 if (ha->retry_reset_ha_cnt == 0) {
2543 /* Recover adapter retries have been exhausted.
2544 * Adapter DEAD */
2545 DEBUG2(printk("scsi%ld: recover adapter "
2546 "failed - board disabled\n",
2547 ha->host_no));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302548 qla4xxx_dead_adapter_cleanup(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002549 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
2550 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302551 clear_bit(DPC_RESET_HA_FW_CONTEXT,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002552 &ha->dpc_flags);
2553 status = QLA_ERROR;
2554 }
2555 }
2556 } else {
2557 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302558 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002559 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
2560 }
2561
Sarang Radke8e0f3a62011-12-01 22:42:09 -08002562exit_recover:
David Somayajuluafaf5a22006-09-19 10:28:00 -07002563 ha->adapter_error_count++;
2564
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302565 if (test_bit(AF_ONLINE, &ha->flags))
2566 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002567
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302568 scsi_unblock_requests(ha->host);
2569
2570 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2571 DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002572 status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302573
David Somayajuluafaf5a22006-09-19 10:28:00 -07002574 return status;
2575}
2576
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002577static void qla4xxx_relogin_devices(struct iscsi_cls_session *cls_session)
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07002578{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002579 struct iscsi_session *sess;
2580 struct ddb_entry *ddb_entry;
2581 struct scsi_qla_host *ha;
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07002582
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002583 sess = cls_session->dd_data;
2584 ddb_entry = sess->dd_data;
2585 ha = ddb_entry->ha;
2586 if (!iscsi_is_session_online(cls_session)) {
2587 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
2588 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
2589 " unblock session\n", ha->host_no, __func__,
2590 ddb_entry->fw_ddb_index);
2591 iscsi_unblock_session(ddb_entry->sess);
2592 } else {
2593 /* Trigger relogin */
Mike Christie13483732011-12-01 21:38:41 -06002594 if (ddb_entry->ddb_type == FLASH_DDB) {
2595 if (!test_bit(DF_RELOGIN, &ddb_entry->flags))
2596 qla4xxx_arm_relogin_timer(ddb_entry);
2597 } else
2598 iscsi_session_failure(cls_session->dd_data,
2599 ISCSI_ERR_CONN_FAILED);
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07002600 }
2601 }
2602}
2603
Mike Christie13483732011-12-01 21:38:41 -06002604int qla4xxx_unblock_flash_ddb(struct iscsi_cls_session *cls_session)
2605{
2606 struct iscsi_session *sess;
2607 struct ddb_entry *ddb_entry;
2608 struct scsi_qla_host *ha;
2609
2610 sess = cls_session->dd_data;
2611 ddb_entry = sess->dd_data;
2612 ha = ddb_entry->ha;
2613 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
2614 " unblock session\n", ha->host_no, __func__,
2615 ddb_entry->fw_ddb_index);
2616
2617 iscsi_unblock_session(ddb_entry->sess);
2618
2619 /* Start scan target */
2620 if (test_bit(AF_ONLINE, &ha->flags)) {
2621 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
2622 " start scan\n", ha->host_no, __func__,
2623 ddb_entry->fw_ddb_index);
2624 scsi_queue_work(ha->host, &ddb_entry->sess->scan_work);
2625 }
2626 return QLA_SUCCESS;
2627}
2628
2629int qla4xxx_unblock_ddb(struct iscsi_cls_session *cls_session)
2630{
2631 struct iscsi_session *sess;
2632 struct ddb_entry *ddb_entry;
2633 struct scsi_qla_host *ha;
2634
2635 sess = cls_session->dd_data;
2636 ddb_entry = sess->dd_data;
2637 ha = ddb_entry->ha;
2638 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
2639 " unblock user space session\n", ha->host_no, __func__,
2640 ddb_entry->fw_ddb_index);
2641 iscsi_conn_start(ddb_entry->conn);
2642 iscsi_conn_login_event(ddb_entry->conn,
2643 ISCSI_CONN_STATE_LOGGED_IN);
2644
2645 return QLA_SUCCESS;
2646}
2647
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002648static void qla4xxx_relogin_all_devices(struct scsi_qla_host *ha)
2649{
2650 iscsi_host_for_each_session(ha->host, qla4xxx_relogin_devices);
2651}
2652
Mike Christie13483732011-12-01 21:38:41 -06002653static void qla4xxx_relogin_flash_ddb(struct iscsi_cls_session *cls_sess)
2654{
2655 uint16_t relogin_timer;
2656 struct iscsi_session *sess;
2657 struct ddb_entry *ddb_entry;
2658 struct scsi_qla_host *ha;
2659
2660 sess = cls_sess->dd_data;
2661 ddb_entry = sess->dd_data;
2662 ha = ddb_entry->ha;
2663
2664 relogin_timer = max(ddb_entry->default_relogin_timeout,
2665 (uint16_t)RELOGIN_TOV);
2666 atomic_set(&ddb_entry->relogin_timer, relogin_timer);
2667
2668 DEBUG2(ql4_printk(KERN_INFO, ha,
2669 "scsi%ld: Relogin index [%d]. TOV=%d\n", ha->host_no,
2670 ddb_entry->fw_ddb_index, relogin_timer));
2671
2672 qla4xxx_login_flash_ddb(cls_sess);
2673}
2674
2675static void qla4xxx_dpc_relogin(struct iscsi_cls_session *cls_sess)
2676{
2677 struct iscsi_session *sess;
2678 struct ddb_entry *ddb_entry;
2679 struct scsi_qla_host *ha;
2680
2681 sess = cls_sess->dd_data;
2682 ddb_entry = sess->dd_data;
2683 ha = ddb_entry->ha;
2684
2685 if (!(ddb_entry->ddb_type == FLASH_DDB))
2686 return;
2687
2688 if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
2689 !iscsi_is_session_online(cls_sess)) {
2690 DEBUG2(ql4_printk(KERN_INFO, ha,
2691 "relogin issued\n"));
2692 qla4xxx_relogin_flash_ddb(cls_sess);
2693 }
2694}
2695
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302696void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
2697{
Lalit Chandivade1b468072011-05-17 23:17:09 -07002698 if (ha->dpc_thread)
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302699 queue_work(ha->dpc_thread, &ha->dpc_work);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302700}
2701
David Somayajuluafaf5a22006-09-19 10:28:00 -07002702/**
2703 * qla4xxx_do_dpc - dpc routine
2704 * @data: in our case pointer to adapter structure
2705 *
2706 * This routine is a task that is schedule by the interrupt handler
2707 * to perform the background processing for interrupts. We put it
2708 * on a task queue that is consumed whenever the scheduler runs; that's
2709 * so you can do anything (i.e. put the process to sleep etc). In fact,
2710 * the mid-level tries to sleep when it reaches the driver threshold
2711 * "host->can_queue". This can cause a panic if we were in our interrupt code.
2712 **/
David Howellsc4028952006-11-22 14:57:56 +00002713static void qla4xxx_do_dpc(struct work_struct *work)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002714{
David Howellsc4028952006-11-22 14:57:56 +00002715 struct scsi_qla_host *ha =
2716 container_of(work, struct scsi_qla_host, dpc_work);
David C Somayajulu477ffb92007-01-22 12:26:11 -08002717 int status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002718
David C Somayajuluf26b9042006-11-15 16:41:09 -08002719 DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302720 "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
2721 ha->host_no, __func__, ha->flags, ha->dpc_flags))
David Somayajuluafaf5a22006-09-19 10:28:00 -07002722
2723 /* Initialization not yet finished. Don't do anything yet. */
2724 if (!test_bit(AF_INIT_DONE, &ha->flags))
Lalit Chandivade1b468072011-05-17 23:17:09 -07002725 return;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002726
Lalit Chandivade2232be02010-07-30 14:38:47 +05302727 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
2728 DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
2729 ha->host_no, __func__, ha->flags));
Lalit Chandivade1b468072011-05-17 23:17:09 -07002730 return;
Lalit Chandivade2232be02010-07-30 14:38:47 +05302731 }
2732
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302733 if (is_qla8022(ha)) {
2734 if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
2735 qla4_8xxx_idc_lock(ha);
2736 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2737 QLA82XX_DEV_FAILED);
2738 qla4_8xxx_idc_unlock(ha);
2739 ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
2740 qla4_8xxx_device_state_handler(ha);
2741 }
2742 if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
2743 qla4_8xxx_need_qsnt_handler(ha);
2744 }
2745 }
2746
2747 if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
2748 (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07002749 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302750 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
2751 if (ql4xdontresethba) {
2752 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
2753 ha->host_no, __func__));
2754 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
2755 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
2756 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2757 goto dpc_post_reset_ha;
2758 }
2759 if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
2760 test_bit(DPC_RESET_HA, &ha->dpc_flags))
2761 qla4xxx_recover_adapter(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002762
David C Somayajulu477ffb92007-01-22 12:26:11 -08002763 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002764 uint8_t wait_time = RESET_INTR_TOV;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002765
David Somayajuluafaf5a22006-09-19 10:28:00 -07002766 while ((readw(&ha->reg->ctrl_status) &
2767 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
2768 if (--wait_time == 0)
2769 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002770 msleep(1000);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002771 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002772 if (wait_time == 0)
2773 DEBUG2(printk("scsi%ld: %s: SR|FSR "
2774 "bit not cleared-- resetting\n",
2775 ha->host_no, __func__));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302776 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
David C Somayajulu477ffb92007-01-22 12:26:11 -08002777 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
2778 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302779 status = qla4xxx_recover_adapter(ha);
David C Somayajulu477ffb92007-01-22 12:26:11 -08002780 }
2781 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
2782 if (status == QLA_SUCCESS)
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302783 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002784 }
2785 }
2786
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302787dpc_post_reset_ha:
David Somayajuluafaf5a22006-09-19 10:28:00 -07002788 /* ---- process AEN? --- */
2789 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
2790 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
2791
2792 /* ---- Get DHCP IP Address? --- */
2793 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
2794 qla4xxx_get_dhcp_ip_address(ha);
2795
Mike Christie13483732011-12-01 21:38:41 -06002796 /* ---- relogin device? --- */
2797 if (adapter_up(ha) &&
2798 test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
2799 iscsi_host_for_each_session(ha->host, qla4xxx_dpc_relogin);
2800 }
2801
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05302802 /* ---- link change? --- */
2803 if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
2804 if (!test_bit(AF_LINK_UP, &ha->flags)) {
2805 /* ---- link down? --- */
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07002806 qla4xxx_mark_all_devices_missing(ha);
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05302807 } else {
2808 /* ---- link up? --- *
2809 * F/W will auto login to all devices ONLY ONCE after
2810 * link up during driver initialization and runtime
2811 * fatal error recovery. Therefore, the driver must
2812 * manually relogin to devices when recovering from
2813 * connection failures, logouts, expired KATO, etc. */
Mike Christie13483732011-12-01 21:38:41 -06002814 if (test_and_clear_bit(AF_BUILD_DDB_LIST, &ha->flags)) {
2815 qla4xxx_build_ddb_list(ha, ha->is_reset);
2816 iscsi_host_for_each_session(ha->host,
2817 qla4xxx_login_flash_ddb);
2818 } else
2819 qla4xxx_relogin_all_devices(ha);
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05302820 }
2821 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002822}
2823
2824/**
2825 * qla4xxx_free_adapter - release the adapter
2826 * @ha: pointer to adapter structure
2827 **/
2828static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
2829{
Sarang Radke8a288962011-12-06 02:34:10 -08002830 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002831
2832 if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
2833 /* Turn-off interrupts on the card. */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302834 ha->isp_ops->disable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002835 }
2836
David Somayajuluafaf5a22006-09-19 10:28:00 -07002837 /* Remove timer thread, if present */
2838 if (ha->timer_active)
2839 qla4xxx_stop_timer(ha);
2840
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302841 /* Kill the kernel thread for this host */
2842 if (ha->dpc_thread)
2843 destroy_workqueue(ha->dpc_thread);
2844
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002845 /* Kill the kernel thread for this host */
2846 if (ha->task_wq)
2847 destroy_workqueue(ha->task_wq);
2848
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302849 /* Put firmware in known state */
2850 ha->isp_ops->reset_firmware(ha);
2851
2852 if (is_qla8022(ha)) {
2853 qla4_8xxx_idc_lock(ha);
2854 qla4_8xxx_clear_drv_active(ha);
2855 qla4_8xxx_idc_unlock(ha);
2856 }
2857
David Somayajuluafaf5a22006-09-19 10:28:00 -07002858 /* Detach interrupts */
2859 if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302860 qla4xxx_free_irqs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002861
David C Somayajulubee4fe82007-05-23 18:03:32 -07002862 /* free extra memory */
2863 qla4xxx_mem_free(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302864}
David C Somayajulubee4fe82007-05-23 18:03:32 -07002865
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302866int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
2867{
2868 int status = 0;
2869 uint8_t revision_id;
2870 unsigned long mem_base, mem_len, db_base, db_len;
2871 struct pci_dev *pdev = ha->pdev;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002872
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302873 status = pci_request_regions(pdev, DRIVER_NAME);
2874 if (status) {
2875 printk(KERN_WARNING
2876 "scsi(%ld) Failed to reserve PIO regions (%s) "
2877 "status=%d\n", ha->host_no, pci_name(pdev), status);
2878 goto iospace_error_exit;
2879 }
2880
2881 pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
2882 DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
2883 __func__, revision_id));
2884 ha->revision_id = revision_id;
2885
2886 /* remap phys address */
2887 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
2888 mem_len = pci_resource_len(pdev, 0);
2889 DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
2890 __func__, mem_base, mem_len));
2891
2892 /* mapping of pcibase pointer */
2893 ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
2894 if (!ha->nx_pcibase) {
2895 printk(KERN_ERR
2896 "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
2897 pci_release_regions(ha->pdev);
2898 goto iospace_error_exit;
2899 }
2900
2901 /* Mapping of IO base pointer, door bell read and write pointer */
2902
2903 /* mapping of IO base pointer */
2904 ha->qla4_8xxx_reg =
2905 (struct device_reg_82xx __iomem *)((uint8_t *)ha->nx_pcibase +
2906 0xbc000 + (ha->pdev->devfn << 11));
2907
2908 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
2909 db_len = pci_resource_len(pdev, 4);
2910
Shyam Sundar2657c802010-10-06 22:50:29 -07002911 ha->nx_db_wr_ptr = (ha->pdev->devfn == 4 ? QLA82XX_CAM_RAM_DB1 :
2912 QLA82XX_CAM_RAM_DB2);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302913
Shyam Sundar2657c802010-10-06 22:50:29 -07002914 return 0;
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302915iospace_error_exit:
2916 return -ENOMEM;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002917}
2918
2919/***
2920 * qla4xxx_iospace_config - maps registers
2921 * @ha: pointer to adapter structure
2922 *
2923 * This routines maps HBA's registers from the pci address space
2924 * into the kernel virtual address space for memory mapped i/o.
2925 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302926int qla4xxx_iospace_config(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002927{
2928 unsigned long pio, pio_len, pio_flags;
2929 unsigned long mmio, mmio_len, mmio_flags;
2930
2931 pio = pci_resource_start(ha->pdev, 0);
2932 pio_len = pci_resource_len(ha->pdev, 0);
2933 pio_flags = pci_resource_flags(ha->pdev, 0);
2934 if (pio_flags & IORESOURCE_IO) {
2935 if (pio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302936 ql4_printk(KERN_WARNING, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002937 "Invalid PCI I/O region size\n");
2938 pio = 0;
2939 }
2940 } else {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302941 ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002942 pio = 0;
2943 }
2944
2945 /* Use MMIO operations for all accesses. */
2946 mmio = pci_resource_start(ha->pdev, 1);
2947 mmio_len = pci_resource_len(ha->pdev, 1);
2948 mmio_flags = pci_resource_flags(ha->pdev, 1);
2949
2950 if (!(mmio_flags & IORESOURCE_MEM)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302951 ql4_printk(KERN_ERR, ha,
2952 "region #0 not an MMIO resource, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002953
2954 goto iospace_error_exit;
2955 }
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302956
David Somayajuluafaf5a22006-09-19 10:28:00 -07002957 if (mmio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302958 ql4_printk(KERN_ERR, ha,
2959 "Invalid PCI mem region size, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002960 goto iospace_error_exit;
2961 }
2962
2963 if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302964 ql4_printk(KERN_WARNING, ha,
2965 "Failed to reserve PIO/MMIO regions\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002966
2967 goto iospace_error_exit;
2968 }
2969
2970 ha->pio_address = pio;
2971 ha->pio_length = pio_len;
2972 ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
2973 if (!ha->reg) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302974 ql4_printk(KERN_ERR, ha,
2975 "cannot remap MMIO, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002976
2977 goto iospace_error_exit;
2978 }
2979
2980 return 0;
2981
2982iospace_error_exit:
2983 return -ENOMEM;
2984}
2985
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302986static struct isp_operations qla4xxx_isp_ops = {
2987 .iospace_config = qla4xxx_iospace_config,
2988 .pci_config = qla4xxx_pci_config,
2989 .disable_intrs = qla4xxx_disable_intrs,
2990 .enable_intrs = qla4xxx_enable_intrs,
2991 .start_firmware = qla4xxx_start_firmware,
2992 .intr_handler = qla4xxx_intr_handler,
2993 .interrupt_service_routine = qla4xxx_interrupt_service_routine,
2994 .reset_chip = qla4xxx_soft_reset,
2995 .reset_firmware = qla4xxx_hw_reset,
2996 .queue_iocb = qla4xxx_queue_iocb,
2997 .complete_iocb = qla4xxx_complete_iocb,
2998 .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
2999 .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
3000 .get_sys_info = qla4xxx_get_sys_info,
3001};
3002
3003static struct isp_operations qla4_8xxx_isp_ops = {
3004 .iospace_config = qla4_8xxx_iospace_config,
3005 .pci_config = qla4_8xxx_pci_config,
3006 .disable_intrs = qla4_8xxx_disable_intrs,
3007 .enable_intrs = qla4_8xxx_enable_intrs,
3008 .start_firmware = qla4_8xxx_load_risc,
3009 .intr_handler = qla4_8xxx_intr_handler,
3010 .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
3011 .reset_chip = qla4_8xxx_isp_reset,
3012 .reset_firmware = qla4_8xxx_stop_firmware,
3013 .queue_iocb = qla4_8xxx_queue_iocb,
3014 .complete_iocb = qla4_8xxx_complete_iocb,
3015 .rd_shdw_req_q_out = qla4_8xxx_rd_shdw_req_q_out,
3016 .rd_shdw_rsp_q_in = qla4_8xxx_rd_shdw_rsp_q_in,
3017 .get_sys_info = qla4_8xxx_get_sys_info,
3018};
3019
3020uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
3021{
3022 return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
3023}
3024
3025uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
3026{
3027 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
3028}
3029
3030uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
3031{
3032 return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
3033}
3034
3035uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
3036{
3037 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
3038}
3039
Manish Rangankar2a991c22011-07-25 13:48:55 -05003040static ssize_t qla4xxx_show_boot_eth_info(void *data, int type, char *buf)
3041{
3042 struct scsi_qla_host *ha = data;
3043 char *str = buf;
3044 int rc;
3045
3046 switch (type) {
3047 case ISCSI_BOOT_ETH_FLAGS:
3048 rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
3049 break;
3050 case ISCSI_BOOT_ETH_INDEX:
3051 rc = sprintf(str, "0\n");
3052 break;
3053 case ISCSI_BOOT_ETH_MAC:
3054 rc = sysfs_format_mac(str, ha->my_mac,
3055 MAC_ADDR_LEN);
3056 break;
3057 default:
3058 rc = -ENOSYS;
3059 break;
3060 }
3061 return rc;
3062}
3063
3064static mode_t qla4xxx_eth_get_attr_visibility(void *data, int type)
3065{
3066 int rc;
3067
3068 switch (type) {
3069 case ISCSI_BOOT_ETH_FLAGS:
3070 case ISCSI_BOOT_ETH_MAC:
3071 case ISCSI_BOOT_ETH_INDEX:
3072 rc = S_IRUGO;
3073 break;
3074 default:
3075 rc = 0;
3076 break;
3077 }
3078 return rc;
3079}
3080
3081static ssize_t qla4xxx_show_boot_ini_info(void *data, int type, char *buf)
3082{
3083 struct scsi_qla_host *ha = data;
3084 char *str = buf;
3085 int rc;
3086
3087 switch (type) {
3088 case ISCSI_BOOT_INI_INITIATOR_NAME:
3089 rc = sprintf(str, "%s\n", ha->name_string);
3090 break;
3091 default:
3092 rc = -ENOSYS;
3093 break;
3094 }
3095 return rc;
3096}
3097
3098static mode_t qla4xxx_ini_get_attr_visibility(void *data, int type)
3099{
3100 int rc;
3101
3102 switch (type) {
3103 case ISCSI_BOOT_INI_INITIATOR_NAME:
3104 rc = S_IRUGO;
3105 break;
3106 default:
3107 rc = 0;
3108 break;
3109 }
3110 return rc;
3111}
3112
3113static ssize_t
3114qla4xxx_show_boot_tgt_info(struct ql4_boot_session_info *boot_sess, int type,
3115 char *buf)
3116{
3117 struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
3118 char *str = buf;
3119 int rc;
3120
3121 switch (type) {
3122 case ISCSI_BOOT_TGT_NAME:
3123 rc = sprintf(buf, "%s\n", (char *)&boot_sess->target_name);
3124 break;
3125 case ISCSI_BOOT_TGT_IP_ADDR:
3126 if (boot_sess->conn_list[0].dest_ipaddr.ip_type == 0x1)
3127 rc = sprintf(buf, "%pI4\n",
3128 &boot_conn->dest_ipaddr.ip_address);
3129 else
3130 rc = sprintf(str, "%pI6\n",
3131 &boot_conn->dest_ipaddr.ip_address);
3132 break;
3133 case ISCSI_BOOT_TGT_PORT:
3134 rc = sprintf(str, "%d\n", boot_conn->dest_port);
3135 break;
3136 case ISCSI_BOOT_TGT_CHAP_NAME:
3137 rc = sprintf(str, "%.*s\n",
3138 boot_conn->chap.target_chap_name_length,
3139 (char *)&boot_conn->chap.target_chap_name);
3140 break;
3141 case ISCSI_BOOT_TGT_CHAP_SECRET:
3142 rc = sprintf(str, "%.*s\n",
3143 boot_conn->chap.target_secret_length,
3144 (char *)&boot_conn->chap.target_secret);
3145 break;
3146 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
3147 rc = sprintf(str, "%.*s\n",
3148 boot_conn->chap.intr_chap_name_length,
3149 (char *)&boot_conn->chap.intr_chap_name);
3150 break;
3151 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
3152 rc = sprintf(str, "%.*s\n",
3153 boot_conn->chap.intr_secret_length,
3154 (char *)&boot_conn->chap.intr_secret);
3155 break;
3156 case ISCSI_BOOT_TGT_FLAGS:
3157 rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
3158 break;
3159 case ISCSI_BOOT_TGT_NIC_ASSOC:
3160 rc = sprintf(str, "0\n");
3161 break;
3162 default:
3163 rc = -ENOSYS;
3164 break;
3165 }
3166 return rc;
3167}
3168
3169static ssize_t qla4xxx_show_boot_tgt_pri_info(void *data, int type, char *buf)
3170{
3171 struct scsi_qla_host *ha = data;
3172 struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_pri_sess);
3173
3174 return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
3175}
3176
3177static ssize_t qla4xxx_show_boot_tgt_sec_info(void *data, int type, char *buf)
3178{
3179 struct scsi_qla_host *ha = data;
3180 struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_sec_sess);
3181
3182 return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
3183}
3184
3185static mode_t qla4xxx_tgt_get_attr_visibility(void *data, int type)
3186{
3187 int rc;
3188
3189 switch (type) {
3190 case ISCSI_BOOT_TGT_NAME:
3191 case ISCSI_BOOT_TGT_IP_ADDR:
3192 case ISCSI_BOOT_TGT_PORT:
3193 case ISCSI_BOOT_TGT_CHAP_NAME:
3194 case ISCSI_BOOT_TGT_CHAP_SECRET:
3195 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
3196 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
3197 case ISCSI_BOOT_TGT_NIC_ASSOC:
3198 case ISCSI_BOOT_TGT_FLAGS:
3199 rc = S_IRUGO;
3200 break;
3201 default:
3202 rc = 0;
3203 break;
3204 }
3205 return rc;
3206}
3207
3208static void qla4xxx_boot_release(void *data)
3209{
3210 struct scsi_qla_host *ha = data;
3211
3212 scsi_host_put(ha->host);
3213}
3214
3215static int get_fw_boot_info(struct scsi_qla_host *ha, uint16_t ddb_index[])
3216{
3217 dma_addr_t buf_dma;
3218 uint32_t addr, pri_addr, sec_addr;
3219 uint32_t offset;
3220 uint16_t func_num;
3221 uint8_t val;
3222 uint8_t *buf = NULL;
3223 size_t size = 13 * sizeof(uint8_t);
3224 int ret = QLA_SUCCESS;
3225
3226 func_num = PCI_FUNC(ha->pdev->devfn);
3227
Manish Rangankar0d5b36b2011-10-07 16:55:51 -07003228 ql4_printk(KERN_INFO, ha, "%s: Get FW boot info for 0x%x func %d\n",
3229 __func__, ha->pdev->device, func_num);
Manish Rangankar2a991c22011-07-25 13:48:55 -05003230
Manish Rangankar0d5b36b2011-10-07 16:55:51 -07003231 if (is_qla40XX(ha)) {
Manish Rangankar2a991c22011-07-25 13:48:55 -05003232 if (func_num == 1) {
3233 addr = NVRAM_PORT0_BOOT_MODE;
3234 pri_addr = NVRAM_PORT0_BOOT_PRI_TGT;
3235 sec_addr = NVRAM_PORT0_BOOT_SEC_TGT;
3236 } else if (func_num == 3) {
3237 addr = NVRAM_PORT1_BOOT_MODE;
3238 pri_addr = NVRAM_PORT1_BOOT_PRI_TGT;
3239 sec_addr = NVRAM_PORT1_BOOT_SEC_TGT;
3240 } else {
3241 ret = QLA_ERROR;
3242 goto exit_boot_info;
3243 }
3244
3245 /* Check Boot Mode */
3246 val = rd_nvram_byte(ha, addr);
3247 if (!(val & 0x07)) {
3248 DEBUG2(ql4_printk(KERN_ERR, ha,
3249 "%s: Failed Boot options : 0x%x\n",
3250 __func__, val));
3251 ret = QLA_ERROR;
3252 goto exit_boot_info;
3253 }
3254
3255 /* get primary valid target index */
3256 val = rd_nvram_byte(ha, pri_addr);
3257 if (val & BIT_7)
3258 ddb_index[0] = (val & 0x7f);
Manish Rangankar2a991c22011-07-25 13:48:55 -05003259
3260 /* get secondary valid target index */
3261 val = rd_nvram_byte(ha, sec_addr);
3262 if (val & BIT_7)
3263 ddb_index[1] = (val & 0x7f);
Manish Rangankar2a991c22011-07-25 13:48:55 -05003264
3265 } else if (is_qla8022(ha)) {
3266 buf = dma_alloc_coherent(&ha->pdev->dev, size,
3267 &buf_dma, GFP_KERNEL);
3268 if (!buf) {
3269 DEBUG2(ql4_printk(KERN_ERR, ha,
3270 "%s: Unable to allocate dma buffer\n",
3271 __func__));
3272 ret = QLA_ERROR;
3273 goto exit_boot_info;
3274 }
3275
3276 if (ha->port_num == 0)
3277 offset = BOOT_PARAM_OFFSET_PORT0;
3278 else if (ha->port_num == 1)
3279 offset = BOOT_PARAM_OFFSET_PORT1;
3280 else {
3281 ret = QLA_ERROR;
3282 goto exit_boot_info_free;
3283 }
3284 addr = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_iscsi_param * 4) +
3285 offset;
3286 if (qla4xxx_get_flash(ha, buf_dma, addr,
3287 13 * sizeof(uint8_t)) != QLA_SUCCESS) {
3288 DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: Get Flash"
3289 "failed\n", ha->host_no, __func__));
3290 ret = QLA_ERROR;
3291 goto exit_boot_info_free;
3292 }
3293 /* Check Boot Mode */
3294 if (!(buf[1] & 0x07)) {
3295 DEBUG2(ql4_printk(KERN_INFO, ha,
3296 "Failed: Boot options : 0x%x\n",
3297 buf[1]));
3298 ret = QLA_ERROR;
3299 goto exit_boot_info_free;
3300 }
3301
3302 /* get primary valid target index */
3303 if (buf[2] & BIT_7)
3304 ddb_index[0] = buf[2] & 0x7f;
Manish Rangankar2a991c22011-07-25 13:48:55 -05003305
3306 /* get secondary valid target index */
3307 if (buf[11] & BIT_7)
3308 ddb_index[1] = buf[11] & 0x7f;
Manish Rangankar2a991c22011-07-25 13:48:55 -05003309 } else {
3310 ret = QLA_ERROR;
3311 goto exit_boot_info;
3312 }
3313
3314 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Primary target ID %d, Secondary"
3315 " target ID %d\n", __func__, ddb_index[0],
3316 ddb_index[1]));
3317
Mike Christie13483732011-12-01 21:38:41 -06003318 ha->pri_ddb_idx = ddb_index[0];
3319 ha->sec_ddb_idx = ddb_index[1];
3320
Manish Rangankar2a991c22011-07-25 13:48:55 -05003321exit_boot_info_free:
3322 dma_free_coherent(&ha->pdev->dev, size, buf, buf_dma);
3323exit_boot_info:
3324 return ret;
3325}
3326
Lalit Chandivade28deb452011-10-07 16:55:44 -07003327/**
3328 * qla4xxx_get_bidi_chap - Get a BIDI CHAP user and password
3329 * @ha: pointer to adapter structure
3330 * @username: CHAP username to be returned
3331 * @password: CHAP password to be returned
3332 *
3333 * If a boot entry has BIDI CHAP enabled then we need to set the BIDI CHAP
3334 * user and password in the sysfs entry in /sys/firmware/iscsi_boot#/.
3335 * So from the CHAP cache find the first BIDI CHAP entry and set it
3336 * to the boot record in sysfs.
3337 **/
3338static int qla4xxx_get_bidi_chap(struct scsi_qla_host *ha, char *username,
3339 char *password)
3340{
3341 int i, ret = -EINVAL;
3342 int max_chap_entries = 0;
3343 struct ql4_chap_table *chap_table;
3344
3345 if (is_qla8022(ha))
3346 max_chap_entries = (ha->hw.flt_chap_size / 2) /
3347 sizeof(struct ql4_chap_table);
3348 else
3349 max_chap_entries = MAX_CHAP_ENTRIES_40XX;
3350
3351 if (!ha->chap_list) {
3352 ql4_printk(KERN_ERR, ha, "Do not have CHAP table cache\n");
3353 return ret;
3354 }
3355
3356 mutex_lock(&ha->chap_sem);
3357 for (i = 0; i < max_chap_entries; i++) {
3358 chap_table = (struct ql4_chap_table *)ha->chap_list + i;
3359 if (chap_table->cookie !=
3360 __constant_cpu_to_le16(CHAP_VALID_COOKIE)) {
3361 continue;
3362 }
3363
3364 if (chap_table->flags & BIT_7) /* local */
3365 continue;
3366
3367 if (!(chap_table->flags & BIT_6)) /* Not BIDI */
3368 continue;
3369
3370 strncpy(password, chap_table->secret, QL4_CHAP_MAX_SECRET_LEN);
3371 strncpy(username, chap_table->name, QL4_CHAP_MAX_NAME_LEN);
3372 ret = 0;
3373 break;
3374 }
3375 mutex_unlock(&ha->chap_sem);
3376
3377 return ret;
3378}
3379
3380
Manish Rangankar2a991c22011-07-25 13:48:55 -05003381static int qla4xxx_get_boot_target(struct scsi_qla_host *ha,
3382 struct ql4_boot_session_info *boot_sess,
3383 uint16_t ddb_index)
3384{
3385 struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
3386 struct dev_db_entry *fw_ddb_entry;
3387 dma_addr_t fw_ddb_entry_dma;
3388 uint16_t idx;
3389 uint16_t options;
3390 int ret = QLA_SUCCESS;
3391
3392 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3393 &fw_ddb_entry_dma, GFP_KERNEL);
3394 if (!fw_ddb_entry) {
3395 DEBUG2(ql4_printk(KERN_ERR, ha,
3396 "%s: Unable to allocate dma buffer.\n",
3397 __func__));
3398 ret = QLA_ERROR;
3399 return ret;
3400 }
3401
3402 if (qla4xxx_bootdb_by_index(ha, fw_ddb_entry,
3403 fw_ddb_entry_dma, ddb_index)) {
3404 DEBUG2(ql4_printk(KERN_ERR, ha,
3405 "%s: Flash DDB read Failed\n", __func__));
3406 ret = QLA_ERROR;
3407 goto exit_boot_target;
3408 }
3409
3410 /* Update target name and IP from DDB */
3411 memcpy(boot_sess->target_name, fw_ddb_entry->iscsi_name,
3412 min(sizeof(boot_sess->target_name),
3413 sizeof(fw_ddb_entry->iscsi_name)));
3414
3415 options = le16_to_cpu(fw_ddb_entry->options);
3416 if (options & DDB_OPT_IPV6_DEVICE) {
3417 memcpy(&boot_conn->dest_ipaddr.ip_address,
3418 &fw_ddb_entry->ip_addr[0], IPv6_ADDR_LEN);
3419 } else {
3420 boot_conn->dest_ipaddr.ip_type = 0x1;
3421 memcpy(&boot_conn->dest_ipaddr.ip_address,
3422 &fw_ddb_entry->ip_addr[0], IP_ADDR_LEN);
3423 }
3424
3425 boot_conn->dest_port = le16_to_cpu(fw_ddb_entry->port);
3426
3427 /* update chap information */
3428 idx = __le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
3429
3430 if (BIT_7 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
3431
3432 DEBUG2(ql4_printk(KERN_INFO, ha, "Setting chap\n"));
3433
3434 ret = qla4xxx_get_chap(ha, (char *)&boot_conn->chap.
3435 target_chap_name,
3436 (char *)&boot_conn->chap.target_secret,
3437 idx);
3438 if (ret) {
3439 ql4_printk(KERN_ERR, ha, "Failed to set chap\n");
3440 ret = QLA_ERROR;
3441 goto exit_boot_target;
3442 }
3443
3444 boot_conn->chap.target_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
3445 boot_conn->chap.target_secret_length = QL4_CHAP_MAX_SECRET_LEN;
3446 }
3447
3448 if (BIT_4 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
3449
3450 DEBUG2(ql4_printk(KERN_INFO, ha, "Setting BIDI chap\n"));
3451
Lalit Chandivade28deb452011-10-07 16:55:44 -07003452 ret = qla4xxx_get_bidi_chap(ha,
3453 (char *)&boot_conn->chap.intr_chap_name,
3454 (char *)&boot_conn->chap.intr_secret);
3455
Manish Rangankar2a991c22011-07-25 13:48:55 -05003456 if (ret) {
3457 ql4_printk(KERN_ERR, ha, "Failed to set BIDI chap\n");
3458 ret = QLA_ERROR;
3459 goto exit_boot_target;
3460 }
3461
3462 boot_conn->chap.intr_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
3463 boot_conn->chap.intr_secret_length = QL4_CHAP_MAX_SECRET_LEN;
3464 }
3465
3466exit_boot_target:
3467 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3468 fw_ddb_entry, fw_ddb_entry_dma);
3469 return ret;
3470}
3471
3472static int qla4xxx_get_boot_info(struct scsi_qla_host *ha)
3473{
3474 uint16_t ddb_index[2];
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003475 int ret = QLA_ERROR;
3476 int rval;
Manish Rangankar2a991c22011-07-25 13:48:55 -05003477
3478 memset(ddb_index, 0, sizeof(ddb_index));
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003479 ddb_index[0] = 0xffff;
3480 ddb_index[1] = 0xffff;
Manish Rangankar2a991c22011-07-25 13:48:55 -05003481 ret = get_fw_boot_info(ha, ddb_index);
3482 if (ret != QLA_SUCCESS) {
3483 DEBUG2(ql4_printk(KERN_ERR, ha,
3484 "%s: Failed to set boot info.\n", __func__));
3485 return ret;
3486 }
3487
Mike Christie13483732011-12-01 21:38:41 -06003488 if (ql4xdisablesysfsboot)
3489 return QLA_SUCCESS;
3490
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003491 if (ddb_index[0] == 0xffff)
3492 goto sec_target;
3493
3494 rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_pri_sess),
Manish Rangankar2a991c22011-07-25 13:48:55 -05003495 ddb_index[0]);
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003496 if (rval != QLA_SUCCESS) {
Manish Rangankar2a991c22011-07-25 13:48:55 -05003497 DEBUG2(ql4_printk(KERN_ERR, ha, "%s: Failed to get "
3498 "primary target\n", __func__));
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003499 } else
3500 ret = QLA_SUCCESS;
Manish Rangankar2a991c22011-07-25 13:48:55 -05003501
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003502sec_target:
3503 if (ddb_index[1] == 0xffff)
3504 goto exit_get_boot_info;
3505
3506 rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_sec_sess),
Manish Rangankar2a991c22011-07-25 13:48:55 -05003507 ddb_index[1]);
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003508 if (rval != QLA_SUCCESS) {
Manish Rangankar2a991c22011-07-25 13:48:55 -05003509 DEBUG2(ql4_printk(KERN_ERR, ha, "%s: Failed to get "
3510 "secondary target\n", __func__));
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003511 } else
3512 ret = QLA_SUCCESS;
3513
3514exit_get_boot_info:
Manish Rangankar2a991c22011-07-25 13:48:55 -05003515 return ret;
3516}
3517
3518static int qla4xxx_setup_boot_info(struct scsi_qla_host *ha)
3519{
3520 struct iscsi_boot_kobj *boot_kobj;
3521
3522 if (qla4xxx_get_boot_info(ha) != QLA_SUCCESS)
Mike Christie13483732011-12-01 21:38:41 -06003523 return QLA_ERROR;
3524
3525 if (ql4xdisablesysfsboot) {
3526 ql4_printk(KERN_INFO, ha,
3527 "%s: syfsboot disabled - driver will trigger login"
3528 "and publish session for discovery .\n", __func__);
3529 return QLA_SUCCESS;
3530 }
3531
Manish Rangankar2a991c22011-07-25 13:48:55 -05003532
3533 ha->boot_kset = iscsi_boot_create_host_kset(ha->host->host_no);
3534 if (!ha->boot_kset)
3535 goto kset_free;
3536
3537 if (!scsi_host_get(ha->host))
3538 goto kset_free;
3539 boot_kobj = iscsi_boot_create_target(ha->boot_kset, 0, ha,
3540 qla4xxx_show_boot_tgt_pri_info,
3541 qla4xxx_tgt_get_attr_visibility,
3542 qla4xxx_boot_release);
3543 if (!boot_kobj)
3544 goto put_host;
3545
3546 if (!scsi_host_get(ha->host))
3547 goto kset_free;
3548 boot_kobj = iscsi_boot_create_target(ha->boot_kset, 1, ha,
3549 qla4xxx_show_boot_tgt_sec_info,
3550 qla4xxx_tgt_get_attr_visibility,
3551 qla4xxx_boot_release);
3552 if (!boot_kobj)
3553 goto put_host;
3554
3555 if (!scsi_host_get(ha->host))
3556 goto kset_free;
3557 boot_kobj = iscsi_boot_create_initiator(ha->boot_kset, 0, ha,
3558 qla4xxx_show_boot_ini_info,
3559 qla4xxx_ini_get_attr_visibility,
3560 qla4xxx_boot_release);
3561 if (!boot_kobj)
3562 goto put_host;
3563
3564 if (!scsi_host_get(ha->host))
3565 goto kset_free;
3566 boot_kobj = iscsi_boot_create_ethernet(ha->boot_kset, 0, ha,
3567 qla4xxx_show_boot_eth_info,
3568 qla4xxx_eth_get_attr_visibility,
3569 qla4xxx_boot_release);
3570 if (!boot_kobj)
3571 goto put_host;
3572
Mike Christie13483732011-12-01 21:38:41 -06003573 return QLA_SUCCESS;
Manish Rangankar2a991c22011-07-25 13:48:55 -05003574
3575put_host:
3576 scsi_host_put(ha->host);
3577kset_free:
3578 iscsi_boot_destroy_kset(ha->boot_kset);
3579 return -ENOMEM;
3580}
3581
Lalit Chandivade45494152011-10-07 16:55:42 -07003582
3583/**
3584 * qla4xxx_create chap_list - Create CHAP list from FLASH
3585 * @ha: pointer to adapter structure
3586 *
3587 * Read flash and make a list of CHAP entries, during login when a CHAP entry
3588 * is received, it will be checked in this list. If entry exist then the CHAP
3589 * entry index is set in the DDB. If CHAP entry does not exist in this list
3590 * then a new entry is added in FLASH in CHAP table and the index obtained is
3591 * used in the DDB.
3592 **/
3593static void qla4xxx_create_chap_list(struct scsi_qla_host *ha)
3594{
3595 int rval = 0;
3596 uint8_t *chap_flash_data = NULL;
3597 uint32_t offset;
3598 dma_addr_t chap_dma;
3599 uint32_t chap_size = 0;
3600
3601 if (is_qla40XX(ha))
3602 chap_size = MAX_CHAP_ENTRIES_40XX *
3603 sizeof(struct ql4_chap_table);
3604 else /* Single region contains CHAP info for both
3605 * ports which is divided into half for each port.
3606 */
3607 chap_size = ha->hw.flt_chap_size / 2;
3608
3609 chap_flash_data = dma_alloc_coherent(&ha->pdev->dev, chap_size,
3610 &chap_dma, GFP_KERNEL);
3611 if (!chap_flash_data) {
3612 ql4_printk(KERN_ERR, ha, "No memory for chap_flash_data\n");
3613 return;
3614 }
3615 if (is_qla40XX(ha))
3616 offset = FLASH_CHAP_OFFSET;
3617 else {
3618 offset = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_region_chap << 2);
3619 if (ha->port_num == 1)
3620 offset += chap_size;
3621 }
3622
3623 rval = qla4xxx_get_flash(ha, chap_dma, offset, chap_size);
3624 if (rval != QLA_SUCCESS)
3625 goto exit_chap_list;
3626
3627 if (ha->chap_list == NULL)
3628 ha->chap_list = vmalloc(chap_size);
3629 if (ha->chap_list == NULL) {
3630 ql4_printk(KERN_ERR, ha, "No memory for ha->chap_list\n");
3631 goto exit_chap_list;
3632 }
3633
3634 memcpy(ha->chap_list, chap_flash_data, chap_size);
3635
3636exit_chap_list:
3637 dma_free_coherent(&ha->pdev->dev, chap_size,
3638 chap_flash_data, chap_dma);
Lalit Chandivade45494152011-10-07 16:55:42 -07003639}
3640
Mike Christie13483732011-12-01 21:38:41 -06003641static void qla4xxx_get_param_ddb(struct ddb_entry *ddb_entry,
3642 struct ql4_tuple_ddb *tddb)
3643{
3644 struct scsi_qla_host *ha;
3645 struct iscsi_cls_session *cls_sess;
3646 struct iscsi_cls_conn *cls_conn;
3647 struct iscsi_session *sess;
3648 struct iscsi_conn *conn;
3649
3650 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
3651 ha = ddb_entry->ha;
3652 cls_sess = ddb_entry->sess;
3653 sess = cls_sess->dd_data;
3654 cls_conn = ddb_entry->conn;
3655 conn = cls_conn->dd_data;
3656
3657 tddb->tpgt = sess->tpgt;
3658 tddb->port = conn->persistent_port;
3659 strncpy(tddb->iscsi_name, sess->targetname, ISCSI_NAME_SIZE);
3660 strncpy(tddb->ip_addr, conn->persistent_address, DDB_IPADDR_LEN);
3661}
3662
3663static void qla4xxx_convert_param_ddb(struct dev_db_entry *fw_ddb_entry,
3664 struct ql4_tuple_ddb *tddb)
3665{
3666 uint16_t options = 0;
3667
3668 tddb->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
3669 memcpy(&tddb->iscsi_name[0], &fw_ddb_entry->iscsi_name[0],
3670 min(sizeof(tddb->iscsi_name), sizeof(fw_ddb_entry->iscsi_name)));
3671
3672 options = le16_to_cpu(fw_ddb_entry->options);
3673 if (options & DDB_OPT_IPV6_DEVICE)
3674 sprintf(tddb->ip_addr, "%pI6", fw_ddb_entry->ip_addr);
3675 else
3676 sprintf(tddb->ip_addr, "%pI4", fw_ddb_entry->ip_addr);
3677
3678 tddb->port = le16_to_cpu(fw_ddb_entry->port);
3679}
3680
3681static int qla4xxx_compare_tuple_ddb(struct scsi_qla_host *ha,
3682 struct ql4_tuple_ddb *old_tddb,
3683 struct ql4_tuple_ddb *new_tddb)
3684{
3685 if (strcmp(old_tddb->iscsi_name, new_tddb->iscsi_name))
3686 return QLA_ERROR;
3687
3688 if (strcmp(old_tddb->ip_addr, new_tddb->ip_addr))
3689 return QLA_ERROR;
3690
3691 if (old_tddb->port != new_tddb->port)
3692 return QLA_ERROR;
3693
3694 DEBUG2(ql4_printk(KERN_INFO, ha,
3695 "Match Found, fw[%d,%d,%s,%s], [%d,%d,%s,%s]",
3696 old_tddb->port, old_tddb->tpgt, old_tddb->ip_addr,
3697 old_tddb->iscsi_name, new_tddb->port, new_tddb->tpgt,
3698 new_tddb->ip_addr, new_tddb->iscsi_name));
3699
3700 return QLA_SUCCESS;
3701}
3702
3703static int qla4xxx_is_session_exists(struct scsi_qla_host *ha,
3704 struct dev_db_entry *fw_ddb_entry)
3705{
3706 struct ddb_entry *ddb_entry;
3707 struct ql4_tuple_ddb *fw_tddb = NULL;
3708 struct ql4_tuple_ddb *tmp_tddb = NULL;
3709 int idx;
3710 int ret = QLA_ERROR;
3711
3712 fw_tddb = vzalloc(sizeof(*fw_tddb));
3713 if (!fw_tddb) {
3714 DEBUG2(ql4_printk(KERN_WARNING, ha,
3715 "Memory Allocation failed.\n"));
3716 ret = QLA_SUCCESS;
3717 goto exit_check;
3718 }
3719
3720 tmp_tddb = vzalloc(sizeof(*tmp_tddb));
3721 if (!tmp_tddb) {
3722 DEBUG2(ql4_printk(KERN_WARNING, ha,
3723 "Memory Allocation failed.\n"));
3724 ret = QLA_SUCCESS;
3725 goto exit_check;
3726 }
3727
3728 qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb);
3729
3730 for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
3731 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
3732 if (ddb_entry == NULL)
3733 continue;
3734
3735 qla4xxx_get_param_ddb(ddb_entry, tmp_tddb);
3736 if (!qla4xxx_compare_tuple_ddb(ha, fw_tddb, tmp_tddb)) {
3737 ret = QLA_SUCCESS; /* found */
3738 goto exit_check;
3739 }
3740 }
3741
3742exit_check:
3743 if (fw_tddb)
3744 vfree(fw_tddb);
3745 if (tmp_tddb)
3746 vfree(tmp_tddb);
3747 return ret;
3748}
3749
3750static int qla4xxx_is_flash_ddb_exists(struct scsi_qla_host *ha,
3751 struct list_head *list_nt,
3752 struct dev_db_entry *fw_ddb_entry)
3753{
3754 struct qla_ddb_index *nt_ddb_idx, *nt_ddb_idx_tmp;
3755 struct ql4_tuple_ddb *fw_tddb = NULL;
3756 struct ql4_tuple_ddb *tmp_tddb = NULL;
3757 int ret = QLA_ERROR;
3758
3759 fw_tddb = vzalloc(sizeof(*fw_tddb));
3760 if (!fw_tddb) {
3761 DEBUG2(ql4_printk(KERN_WARNING, ha,
3762 "Memory Allocation failed.\n"));
3763 ret = QLA_SUCCESS;
3764 goto exit_check;
3765 }
3766
3767 tmp_tddb = vzalloc(sizeof(*tmp_tddb));
3768 if (!tmp_tddb) {
3769 DEBUG2(ql4_printk(KERN_WARNING, ha,
3770 "Memory Allocation failed.\n"));
3771 ret = QLA_SUCCESS;
3772 goto exit_check;
3773 }
3774
3775 qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb);
3776
3777 list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
3778 qla4xxx_convert_param_ddb(&nt_ddb_idx->fw_ddb, tmp_tddb);
3779 if (!qla4xxx_compare_tuple_ddb(ha, fw_tddb, tmp_tddb)) {
3780 ret = QLA_SUCCESS; /* found */
3781 goto exit_check;
3782 }
3783 }
3784
3785exit_check:
3786 if (fw_tddb)
3787 vfree(fw_tddb);
3788 if (tmp_tddb)
3789 vfree(tmp_tddb);
3790 return ret;
3791}
3792
3793static void qla4xxx_free_nt_list(struct list_head *list_nt)
3794{
3795 struct qla_ddb_index *nt_ddb_idx, *nt_ddb_idx_tmp;
3796
3797 /* Free up the normaltargets list */
3798 list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
3799 list_del_init(&nt_ddb_idx->list);
3800 vfree(nt_ddb_idx);
3801 }
3802
3803}
3804
3805static struct iscsi_endpoint *qla4xxx_get_ep_fwdb(struct scsi_qla_host *ha,
3806 struct dev_db_entry *fw_ddb_entry)
3807{
3808 struct iscsi_endpoint *ep;
3809 struct sockaddr_in *addr;
3810 struct sockaddr_in6 *addr6;
3811 struct sockaddr *dst_addr;
3812 char *ip;
3813
3814 /* TODO: need to destroy on unload iscsi_endpoint*/
3815 dst_addr = vmalloc(sizeof(*dst_addr));
3816 if (!dst_addr)
3817 return NULL;
3818
3819 if (fw_ddb_entry->options & DDB_OPT_IPV6_DEVICE) {
3820 dst_addr->sa_family = AF_INET6;
3821 addr6 = (struct sockaddr_in6 *)dst_addr;
3822 ip = (char *)&addr6->sin6_addr;
3823 memcpy(ip, fw_ddb_entry->ip_addr, IPv6_ADDR_LEN);
3824 addr6->sin6_port = htons(le16_to_cpu(fw_ddb_entry->port));
3825
3826 } else {
3827 dst_addr->sa_family = AF_INET;
3828 addr = (struct sockaddr_in *)dst_addr;
3829 ip = (char *)&addr->sin_addr;
3830 memcpy(ip, fw_ddb_entry->ip_addr, IP_ADDR_LEN);
3831 addr->sin_port = htons(le16_to_cpu(fw_ddb_entry->port));
3832 }
3833
3834 ep = qla4xxx_ep_connect(ha->host, dst_addr, 0);
3835 vfree(dst_addr);
3836 return ep;
3837}
3838
3839static int qla4xxx_verify_boot_idx(struct scsi_qla_host *ha, uint16_t idx)
3840{
3841 if (ql4xdisablesysfsboot)
3842 return QLA_SUCCESS;
3843 if (idx == ha->pri_ddb_idx || idx == ha->sec_ddb_idx)
3844 return QLA_ERROR;
3845 return QLA_SUCCESS;
3846}
3847
3848static void qla4xxx_setup_flash_ddb_entry(struct scsi_qla_host *ha,
3849 struct ddb_entry *ddb_entry)
3850{
3851 ddb_entry->ddb_type = FLASH_DDB;
3852 ddb_entry->fw_ddb_index = INVALID_ENTRY;
3853 ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
3854 ddb_entry->ha = ha;
3855 ddb_entry->unblock_sess = qla4xxx_unblock_flash_ddb;
3856 ddb_entry->ddb_change = qla4xxx_flash_ddb_change;
3857
3858 atomic_set(&ddb_entry->retry_relogin_timer, INVALID_ENTRY);
3859 atomic_set(&ddb_entry->relogin_timer, 0);
3860 atomic_set(&ddb_entry->relogin_retry_count, 0);
3861
3862 ddb_entry->default_relogin_timeout =
3863 le16_to_cpu(ddb_entry->fw_ddb_entry.def_timeout);
3864 ddb_entry->default_time2wait =
3865 le16_to_cpu(ddb_entry->fw_ddb_entry.iscsi_def_time2wait);
3866}
3867
3868static void qla4xxx_wait_for_ip_configuration(struct scsi_qla_host *ha)
3869{
3870 uint32_t idx = 0;
3871 uint32_t ip_idx[IP_ADDR_COUNT] = {0, 1, 2, 3}; /* 4 IP interfaces */
3872 uint32_t sts[MBOX_REG_COUNT];
3873 uint32_t ip_state;
3874 unsigned long wtime;
3875 int ret;
3876
3877 wtime = jiffies + (HZ * IP_CONFIG_TOV);
3878 do {
3879 for (idx = 0; idx < IP_ADDR_COUNT; idx++) {
3880 if (ip_idx[idx] == -1)
3881 continue;
3882
3883 ret = qla4xxx_get_ip_state(ha, 0, ip_idx[idx], sts);
3884
3885 if (ret == QLA_ERROR) {
3886 ip_idx[idx] = -1;
3887 continue;
3888 }
3889
3890 ip_state = (sts[1] & IP_STATE_MASK) >> IP_STATE_SHIFT;
3891
3892 DEBUG2(ql4_printk(KERN_INFO, ha,
3893 "Waiting for IP state for idx = %d, state = 0x%x\n",
3894 ip_idx[idx], ip_state));
3895 if (ip_state == IP_ADDRSTATE_UNCONFIGURED ||
3896 ip_state == IP_ADDRSTATE_INVALID ||
3897 ip_state == IP_ADDRSTATE_PREFERRED ||
3898 ip_state == IP_ADDRSTATE_DEPRICATED ||
3899 ip_state == IP_ADDRSTATE_DISABLING)
3900 ip_idx[idx] = -1;
3901
3902 }
3903
3904 /* Break if all IP states checked */
3905 if ((ip_idx[0] == -1) &&
3906 (ip_idx[1] == -1) &&
3907 (ip_idx[2] == -1) &&
3908 (ip_idx[3] == -1))
3909 break;
3910 schedule_timeout_uninterruptible(HZ);
3911 } while (time_after(wtime, jiffies));
3912}
3913
3914void qla4xxx_build_ddb_list(struct scsi_qla_host *ha, int is_reset)
3915{
3916 int max_ddbs;
3917 int ret;
3918 uint32_t idx = 0, next_idx = 0;
3919 uint32_t state = 0, conn_err = 0;
3920 uint16_t conn_id;
3921 struct dev_db_entry *fw_ddb_entry;
3922 struct ddb_entry *ddb_entry = NULL;
3923 dma_addr_t fw_ddb_dma;
3924 struct iscsi_cls_session *cls_sess;
3925 struct iscsi_session *sess;
3926 struct iscsi_cls_conn *cls_conn;
3927 struct iscsi_endpoint *ep;
3928 uint16_t cmds_max = 32, tmo = 0;
3929 uint32_t initial_cmdsn = 0;
3930 struct list_head list_st, list_nt; /* List of sendtargets */
3931 struct qla_ddb_index *st_ddb_idx, *st_ddb_idx_tmp;
3932 int fw_idx_size;
3933 unsigned long wtime;
3934 struct qla_ddb_index *nt_ddb_idx;
3935
3936 if (!test_bit(AF_LINK_UP, &ha->flags)) {
3937 set_bit(AF_BUILD_DDB_LIST, &ha->flags);
3938 ha->is_reset = is_reset;
3939 return;
3940 }
3941 max_ddbs = is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
3942 MAX_DEV_DB_ENTRIES;
3943
3944 fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
3945 &fw_ddb_dma);
3946 if (fw_ddb_entry == NULL) {
3947 DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
3948 goto exit_ddb_list;
3949 }
3950
3951 INIT_LIST_HEAD(&list_st);
3952 INIT_LIST_HEAD(&list_nt);
3953 fw_idx_size = sizeof(struct qla_ddb_index);
3954
3955 for (idx = 0; idx < max_ddbs; idx = next_idx) {
3956 ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry,
3957 fw_ddb_dma, NULL,
3958 &next_idx, &state, &conn_err,
3959 NULL, &conn_id);
3960 if (ret == QLA_ERROR)
3961 break;
3962
3963 if (qla4xxx_verify_boot_idx(ha, idx) != QLA_SUCCESS)
3964 goto continue_next_st;
3965
3966 /* Check if ST, add to the list_st */
3967 if (strlen((char *) fw_ddb_entry->iscsi_name) != 0)
3968 goto continue_next_st;
3969
3970 st_ddb_idx = vzalloc(fw_idx_size);
3971 if (!st_ddb_idx)
3972 break;
3973
3974 st_ddb_idx->fw_ddb_idx = idx;
3975
3976 list_add_tail(&st_ddb_idx->list, &list_st);
3977continue_next_st:
3978 if (next_idx == 0)
3979 break;
3980 }
3981
3982 /* Before issuing conn open mbox, ensure all IPs states are configured
3983 * Note, conn open fails if IPs are not configured
3984 */
3985 qla4xxx_wait_for_ip_configuration(ha);
3986
3987 /* Go thru the STs and fire the sendtargets by issuing conn open mbx */
3988 list_for_each_entry_safe(st_ddb_idx, st_ddb_idx_tmp, &list_st, list) {
3989 qla4xxx_conn_open(ha, st_ddb_idx->fw_ddb_idx);
3990 }
3991
3992 /* Wait to ensure all sendtargets are done for min 12 sec wait */
3993 tmo = ((ha->def_timeout < LOGIN_TOV) ? LOGIN_TOV : ha->def_timeout);
3994 DEBUG2(ql4_printk(KERN_INFO, ha,
3995 "Default time to wait for build ddb %d\n", tmo));
3996
3997 wtime = jiffies + (HZ * tmo);
3998 do {
3999 list_for_each_entry_safe(st_ddb_idx, st_ddb_idx_tmp, &list_st,
4000 list) {
4001 ret = qla4xxx_get_fwddb_entry(ha,
4002 st_ddb_idx->fw_ddb_idx,
4003 NULL, 0, NULL, &next_idx,
4004 &state, &conn_err, NULL,
4005 NULL);
4006 if (ret == QLA_ERROR)
4007 continue;
4008
4009 if (state == DDB_DS_NO_CONNECTION_ACTIVE ||
4010 state == DDB_DS_SESSION_FAILED) {
4011 list_del_init(&st_ddb_idx->list);
4012 vfree(st_ddb_idx);
4013 }
4014 }
4015 schedule_timeout_uninterruptible(HZ / 10);
4016 } while (time_after(wtime, jiffies));
4017
4018 /* Free up the sendtargets list */
4019 list_for_each_entry_safe(st_ddb_idx, st_ddb_idx_tmp, &list_st, list) {
4020 list_del_init(&st_ddb_idx->list);
4021 vfree(st_ddb_idx);
4022 }
4023
4024 for (idx = 0; idx < max_ddbs; idx = next_idx) {
4025 ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry,
4026 fw_ddb_dma, NULL,
4027 &next_idx, &state, &conn_err,
4028 NULL, &conn_id);
4029 if (ret == QLA_ERROR)
4030 break;
4031
4032 if (qla4xxx_verify_boot_idx(ha, idx) != QLA_SUCCESS)
4033 goto continue_next_nt;
4034
4035 /* Check if NT, then add to list it */
4036 if (strlen((char *) fw_ddb_entry->iscsi_name) == 0)
4037 goto continue_next_nt;
4038
4039 if (state == DDB_DS_NO_CONNECTION_ACTIVE ||
4040 state == DDB_DS_SESSION_FAILED) {
4041 DEBUG2(ql4_printk(KERN_INFO, ha,
4042 "Adding DDB to session = 0x%x\n",
4043 idx));
4044 if (is_reset == INIT_ADAPTER) {
4045 nt_ddb_idx = vmalloc(fw_idx_size);
4046 if (!nt_ddb_idx)
4047 break;
4048
4049 nt_ddb_idx->fw_ddb_idx = idx;
4050
4051 memcpy(&nt_ddb_idx->fw_ddb, fw_ddb_entry,
4052 sizeof(struct dev_db_entry));
4053
4054 if (qla4xxx_is_flash_ddb_exists(ha, &list_nt,
4055 fw_ddb_entry) == QLA_SUCCESS) {
4056 vfree(nt_ddb_idx);
4057 goto continue_next_nt;
4058 }
4059 list_add_tail(&nt_ddb_idx->list, &list_nt);
4060 } else if (is_reset == RESET_ADAPTER) {
4061 if (qla4xxx_is_session_exists(ha,
4062 fw_ddb_entry) == QLA_SUCCESS)
4063 goto continue_next_nt;
4064 }
4065
4066 /* Create session object, with INVALID_ENTRY,
4067 * the targer_id would get set when we issue the login
4068 */
4069 cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport,
4070 ha->host, cmds_max,
4071 sizeof(struct ddb_entry),
4072 sizeof(struct ql4_task_data),
4073 initial_cmdsn, INVALID_ENTRY);
4074 if (!cls_sess)
4075 goto exit_ddb_list;
4076
4077 /*
4078 * iscsi_session_setup increments the driver reference
4079 * count which wouldn't let the driver to be unloaded.
4080 * so calling module_put function to decrement the
4081 * reference count.
4082 **/
4083 module_put(qla4xxx_iscsi_transport.owner);
4084 sess = cls_sess->dd_data;
4085 ddb_entry = sess->dd_data;
4086 ddb_entry->sess = cls_sess;
4087
4088 cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
4089 memcpy(&ddb_entry->fw_ddb_entry, fw_ddb_entry,
4090 sizeof(struct dev_db_entry));
4091
4092 qla4xxx_setup_flash_ddb_entry(ha, ddb_entry);
4093
4094 cls_conn = iscsi_conn_setup(cls_sess,
4095 sizeof(struct qla_conn),
4096 conn_id);
4097 if (!cls_conn)
4098 goto exit_ddb_list;
4099
4100 ddb_entry->conn = cls_conn;
4101
4102 /* Setup ep, for displaying attributes in sysfs */
4103 ep = qla4xxx_get_ep_fwdb(ha, fw_ddb_entry);
4104 if (ep) {
4105 ep->conn = cls_conn;
4106 cls_conn->ep = ep;
4107 } else {
4108 DEBUG2(ql4_printk(KERN_ERR, ha,
4109 "Unable to get ep\n"));
4110 }
4111
4112 /* Update sess/conn params */
4113 qla4xxx_copy_fwddb_param(ha, fw_ddb_entry, cls_sess,
4114 cls_conn);
4115
4116 if (is_reset == RESET_ADAPTER) {
4117 iscsi_block_session(cls_sess);
4118 /* Use the relogin path to discover new devices
4119 * by short-circuting the logic of setting
4120 * timer to relogin - instead set the flags
4121 * to initiate login right away.
4122 */
4123 set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
4124 set_bit(DF_RELOGIN, &ddb_entry->flags);
4125 }
4126 }
4127continue_next_nt:
4128 if (next_idx == 0)
4129 break;
4130 }
4131exit_ddb_list:
4132 qla4xxx_free_nt_list(&list_nt);
4133 if (fw_ddb_entry)
4134 dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
4135
4136 qla4xxx_free_ddb_index(ha);
4137}
4138
4139
David Somayajuluafaf5a22006-09-19 10:28:00 -07004140/**
4141 * qla4xxx_probe_adapter - callback function to probe HBA
4142 * @pdev: pointer to pci_dev structure
4143 * @pci_device_id: pointer to pci_device entry
4144 *
4145 * This routine will probe for Qlogic 4xxx iSCSI host adapters.
4146 * It returns zero if successful. It also initializes all data necessary for
4147 * the driver.
4148 **/
4149static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
4150 const struct pci_device_id *ent)
4151{
4152 int ret = -ENODEV, status;
4153 struct Scsi_Host *host;
4154 struct scsi_qla_host *ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004155 uint8_t init_retry_count = 0;
4156 char buf[34];
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304157 struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
Prasanna Mumbaif9880e72011-03-21 03:34:26 -07004158 uint32_t dev_state;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004159
4160 if (pci_enable_device(pdev))
4161 return -1;
4162
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004163 host = iscsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha), 0);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004164 if (host == NULL) {
4165 printk(KERN_WARNING
4166 "qla4xxx: Couldn't allocate host from scsi layer!\n");
4167 goto probe_disable_device;
4168 }
4169
4170 /* Clear our data area */
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004171 ha = to_qla_host(host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004172 memset(ha, 0, sizeof(*ha));
4173
4174 /* Save the information from PCI BIOS. */
4175 ha->pdev = pdev;
4176 ha->host = host;
4177 ha->host_no = host->host_no;
4178
Lalit Chandivade2232be02010-07-30 14:38:47 +05304179 pci_enable_pcie_error_reporting(pdev);
4180
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304181 /* Setup Runtime configurable options */
4182 if (is_qla8022(ha)) {
4183 ha->isp_ops = &qla4_8xxx_isp_ops;
4184 rwlock_init(&ha->hw_lock);
4185 ha->qdr_sn_window = -1;
4186 ha->ddr_mn_window = -1;
4187 ha->curr_window = 255;
4188 ha->func_num = PCI_FUNC(ha->pdev->devfn);
4189 nx_legacy_intr = &legacy_intr[ha->func_num];
4190 ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
4191 ha->nx_legacy_intr.tgt_status_reg =
4192 nx_legacy_intr->tgt_status_reg;
4193 ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
4194 ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
4195 } else {
4196 ha->isp_ops = &qla4xxx_isp_ops;
4197 }
4198
Lalit Chandivade2232be02010-07-30 14:38:47 +05304199 /* Set EEH reset type to fundamental if required by hba */
4200 if (is_qla8022(ha))
4201 pdev->needs_freset = 1;
4202
David Somayajuluafaf5a22006-09-19 10:28:00 -07004203 /* Configure PCI I/O space. */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304204 ret = ha->isp_ops->iospace_config(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004205 if (ret)
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304206 goto probe_failed_ioconfig;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004207
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304208 ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
David Somayajuluafaf5a22006-09-19 10:28:00 -07004209 pdev->device, pdev->irq, ha->reg);
4210
4211 qla4xxx_config_dma_addressing(ha);
4212
4213 /* Initialize lists and spinlocks. */
David Somayajuluafaf5a22006-09-19 10:28:00 -07004214 INIT_LIST_HEAD(&ha->free_srb_q);
4215
4216 mutex_init(&ha->mbox_sem);
Lalit Chandivade45494152011-10-07 16:55:42 -07004217 mutex_init(&ha->chap_sem);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304218 init_completion(&ha->mbx_intr_comp);
Vikas Chaudhary95d31262011-08-12 02:51:29 -07004219 init_completion(&ha->disable_acb_comp);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004220
4221 spin_lock_init(&ha->hardware_lock);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004222
4223 /* Allocate dma buffers */
4224 if (qla4xxx_mem_alloc(ha)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304225 ql4_printk(KERN_WARNING, ha,
4226 "[ERROR] Failed to allocate memory for adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07004227
4228 ret = -ENOMEM;
4229 goto probe_failed;
4230 }
4231
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004232 host->cmd_per_lun = 3;
4233 host->max_channel = 0;
4234 host->max_lun = MAX_LUNS - 1;
4235 host->max_id = MAX_TARGETS;
4236 host->max_cmd_len = IOCB_MAX_CDB_LEN;
4237 host->can_queue = MAX_SRBS ;
4238 host->transportt = qla4xxx_scsi_transport;
4239
4240 ret = scsi_init_shared_tag_map(host, MAX_SRBS);
4241 if (ret) {
4242 ql4_printk(KERN_WARNING, ha,
4243 "%s: scsi_init_shared_tag_map failed\n", __func__);
4244 goto probe_failed;
4245 }
4246
4247 pci_set_drvdata(pdev, ha);
4248
4249 ret = scsi_add_host(host, &pdev->dev);
4250 if (ret)
4251 goto probe_failed;
4252
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304253 if (is_qla8022(ha))
4254 (void) qla4_8xxx_get_flash_info(ha);
4255
David Somayajuluafaf5a22006-09-19 10:28:00 -07004256 /*
4257 * Initialize the Host adapter request/response queues and
4258 * firmware
4259 * NOTE: interrupts enabled upon successful completion
4260 */
Mike Christie13483732011-12-01 21:38:41 -06004261 status = qla4xxx_initialize_adapter(ha, INIT_ADAPTER);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304262 while ((!test_bit(AF_ONLINE, &ha->flags)) &&
4263 init_retry_count++ < MAX_INIT_RETRIES) {
Prasanna Mumbaif9880e72011-03-21 03:34:26 -07004264
4265 if (is_qla8022(ha)) {
4266 qla4_8xxx_idc_lock(ha);
4267 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
4268 qla4_8xxx_idc_unlock(ha);
4269 if (dev_state == QLA82XX_DEV_FAILED) {
4270 ql4_printk(KERN_WARNING, ha, "%s: don't retry "
4271 "initialize adapter. H/W is in failed state\n",
4272 __func__);
4273 break;
4274 }
4275 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07004276 DEBUG2(printk("scsi: %s: retrying adapter initialization "
4277 "(%d)\n", __func__, init_retry_count));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304278
4279 if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
4280 continue;
4281
Mike Christie13483732011-12-01 21:38:41 -06004282 status = qla4xxx_initialize_adapter(ha, INIT_ADAPTER);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004283 }
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304284
4285 if (!test_bit(AF_ONLINE, &ha->flags)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304286 ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07004287
Lalit Chandivadefe998522010-12-02 22:12:36 -08004288 if (is_qla8022(ha) && ql4xdontresethba) {
4289 /* Put the device in failed state. */
4290 DEBUG2(printk(KERN_ERR "HW STATE: FAILED\n"));
4291 qla4_8xxx_idc_lock(ha);
4292 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
4293 QLA82XX_DEV_FAILED);
4294 qla4_8xxx_idc_unlock(ha);
4295 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07004296 ret = -ENODEV;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004297 goto remove_host;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004298 }
4299
David Somayajuluafaf5a22006-09-19 10:28:00 -07004300 /* Startup the kernel thread for this host adapter. */
4301 DEBUG2(printk("scsi: %s: Starting kernel thread for "
4302 "qla4xxx_dpc\n", __func__));
4303 sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
4304 ha->dpc_thread = create_singlethread_workqueue(buf);
4305 if (!ha->dpc_thread) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304306 ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07004307 ret = -ENODEV;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004308 goto remove_host;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004309 }
David Howellsc4028952006-11-22 14:57:56 +00004310 INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004311
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004312 sprintf(buf, "qla4xxx_%lu_task", ha->host_no);
4313 ha->task_wq = alloc_workqueue(buf, WQ_MEM_RECLAIM, 1);
4314 if (!ha->task_wq) {
4315 ql4_printk(KERN_WARNING, ha, "Unable to start task thread!\n");
4316 ret = -ENODEV;
4317 goto remove_host;
4318 }
4319
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304320 /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
4321 * (which is called indirectly by qla4xxx_initialize_adapter),
4322 * so that irqs will be registered after crbinit but before
4323 * mbx_intr_enable.
4324 */
4325 if (!is_qla8022(ha)) {
4326 ret = qla4xxx_request_irqs(ha);
4327 if (ret) {
4328 ql4_printk(KERN_WARNING, ha, "Failed to reserve "
4329 "interrupt %d already in use.\n", pdev->irq);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004330 goto remove_host;
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304331 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07004332 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07004333
Lalit Chandivade2232be02010-07-30 14:38:47 +05304334 pci_save_state(ha->pdev);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304335 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004336
4337 /* Start timer thread. */
4338 qla4xxx_start_timer(ha, qla4xxx_timer, 1);
4339
4340 set_bit(AF_INIT_DONE, &ha->flags);
4341
David Somayajuluafaf5a22006-09-19 10:28:00 -07004342 printk(KERN_INFO
4343 " QLogic iSCSI HBA Driver version: %s\n"
4344 " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
4345 qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
4346 ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
4347 ha->patch_number, ha->build_number);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05004348
Manish Rangankar2a991c22011-07-25 13:48:55 -05004349 if (qla4xxx_setup_boot_info(ha))
4350 ql4_printk(KERN_ERR, ha, "%s:ISCSI boot info setup failed\n",
4351 __func__);
4352
Mike Christie13483732011-12-01 21:38:41 -06004353 /* Perform the build ddb list and login to each */
4354 qla4xxx_build_ddb_list(ha, INIT_ADAPTER);
4355 iscsi_host_for_each_session(ha->host, qla4xxx_login_flash_ddb);
4356
4357 qla4xxx_create_chap_list(ha);
4358
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05004359 qla4xxx_create_ifaces(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004360 return 0;
4361
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004362remove_host:
4363 scsi_remove_host(ha->host);
4364
David Somayajuluafaf5a22006-09-19 10:28:00 -07004365probe_failed:
4366 qla4xxx_free_adapter(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304367
4368probe_failed_ioconfig:
Lalit Chandivade2232be02010-07-30 14:38:47 +05304369 pci_disable_pcie_error_reporting(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004370 scsi_host_put(ha->host);
4371
4372probe_disable_device:
4373 pci_disable_device(pdev);
4374
4375 return ret;
4376}
4377
4378/**
Karen Higgins7eece5a2011-03-21 03:34:29 -07004379 * qla4xxx_prevent_other_port_reinit - prevent other port from re-initialize
4380 * @ha: pointer to adapter structure
4381 *
4382 * Mark the other ISP-4xxx port to indicate that the driver is being removed,
4383 * so that the other port will not re-initialize while in the process of
4384 * removing the ha due to driver unload or hba hotplug.
4385 **/
4386static void qla4xxx_prevent_other_port_reinit(struct scsi_qla_host *ha)
4387{
4388 struct scsi_qla_host *other_ha = NULL;
4389 struct pci_dev *other_pdev = NULL;
4390 int fn = ISP4XXX_PCI_FN_2;
4391
4392 /*iscsi function numbers for ISP4xxx is 1 and 3*/
4393 if (PCI_FUNC(ha->pdev->devfn) & BIT_1)
4394 fn = ISP4XXX_PCI_FN_1;
4395
4396 other_pdev =
4397 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
4398 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
4399 fn));
4400
4401 /* Get other_ha if other_pdev is valid and state is enable*/
4402 if (other_pdev) {
4403 if (atomic_read(&other_pdev->enable_cnt)) {
4404 other_ha = pci_get_drvdata(other_pdev);
4405 if (other_ha) {
4406 set_bit(AF_HA_REMOVAL, &other_ha->flags);
4407 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: "
4408 "Prevent %s reinit\n", __func__,
4409 dev_name(&other_ha->pdev->dev)));
4410 }
4411 }
4412 pci_dev_put(other_pdev);
4413 }
4414}
4415
Mike Christie13483732011-12-01 21:38:41 -06004416static void qla4xxx_destroy_fw_ddb_session(struct scsi_qla_host *ha)
4417{
4418 struct ddb_entry *ddb_entry;
4419 int options;
4420 int idx;
4421
4422 for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
4423
4424 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
4425 if ((ddb_entry != NULL) &&
4426 (ddb_entry->ddb_type == FLASH_DDB)) {
4427
4428 options = LOGOUT_OPTION_CLOSE_SESSION;
4429 if (qla4xxx_session_logout_ddb(ha, ddb_entry, options)
4430 == QLA_ERROR)
4431 ql4_printk(KERN_ERR, ha, "%s: Logout failed\n",
4432 __func__);
4433
4434 qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
4435 /*
4436 * we have decremented the reference count of the driver
4437 * when we setup the session to have the driver unload
4438 * to be seamless without actually destroying the
4439 * session
4440 **/
4441 try_module_get(qla4xxx_iscsi_transport.owner);
4442 iscsi_destroy_endpoint(ddb_entry->conn->ep);
4443 qla4xxx_free_ddb(ha, ddb_entry);
4444 iscsi_session_teardown(ddb_entry->sess);
4445 }
4446 }
4447}
Karen Higgins7eece5a2011-03-21 03:34:29 -07004448/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07004449 * qla4xxx_remove_adapter - calback function to remove adapter.
4450 * @pci_dev: PCI device pointer
4451 **/
4452static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
4453{
4454 struct scsi_qla_host *ha;
4455
4456 ha = pci_get_drvdata(pdev);
4457
Karen Higgins7eece5a2011-03-21 03:34:29 -07004458 if (!is_qla8022(ha))
4459 qla4xxx_prevent_other_port_reinit(ha);
David C Somayajulubee4fe82007-05-23 18:03:32 -07004460
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05004461 /* destroy iface from sysfs */
4462 qla4xxx_destroy_ifaces(ha);
4463
Mike Christie13483732011-12-01 21:38:41 -06004464 if ((!ql4xdisablesysfsboot) && ha->boot_kset)
Manish Rangankar2a991c22011-07-25 13:48:55 -05004465 iscsi_boot_destroy_kset(ha->boot_kset);
4466
Mike Christie13483732011-12-01 21:38:41 -06004467 qla4xxx_destroy_fw_ddb_session(ha);
4468
David Somayajuluafaf5a22006-09-19 10:28:00 -07004469 scsi_remove_host(ha->host);
4470
4471 qla4xxx_free_adapter(ha);
4472
4473 scsi_host_put(ha->host);
4474
Lalit Chandivade2232be02010-07-30 14:38:47 +05304475 pci_disable_pcie_error_reporting(pdev);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304476 pci_disable_device(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004477 pci_set_drvdata(pdev, NULL);
4478}
4479
4480/**
4481 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
4482 * @ha: HA context
4483 *
4484 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
4485 * supported addressing method.
4486 */
Adrian Bunk47975472007-04-26 00:35:16 -07004487static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07004488{
4489 int retval;
4490
4491 /* Update our PCI device dma_mask for full 64 bit mask */
Yang Hongyang6a355282009-04-06 19:01:13 -07004492 if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
4493 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07004494 dev_dbg(&ha->pdev->dev,
4495 "Failed to set 64 bit PCI consistent mask; "
4496 "using 32 bit.\n");
4497 retval = pci_set_consistent_dma_mask(ha->pdev,
Yang Hongyang284901a2009-04-06 19:01:15 -07004498 DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07004499 }
4500 } else
Yang Hongyang284901a2009-04-06 19:01:15 -07004501 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07004502}
4503
4504static int qla4xxx_slave_alloc(struct scsi_device *sdev)
4505{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004506 struct iscsi_cls_session *cls_sess;
4507 struct iscsi_session *sess;
4508 struct ddb_entry *ddb;
Vikas Chaudhary8bb40332011-03-21 03:34:31 -07004509 int queue_depth = QL4_DEF_QDEPTH;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004510
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004511 cls_sess = starget_to_session(sdev->sdev_target);
4512 sess = cls_sess->dd_data;
4513 ddb = sess->dd_data;
4514
David Somayajuluafaf5a22006-09-19 10:28:00 -07004515 sdev->hostdata = ddb;
4516 sdev->tagged_supported = 1;
Vikas Chaudhary8bb40332011-03-21 03:34:31 -07004517
4518 if (ql4xmaxqdepth != 0 && ql4xmaxqdepth <= 0xffffU)
4519 queue_depth = ql4xmaxqdepth;
4520
4521 scsi_activate_tcq(sdev, queue_depth);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004522 return 0;
4523}
4524
4525static int qla4xxx_slave_configure(struct scsi_device *sdev)
4526{
4527 sdev->tagged_supported = 1;
4528 return 0;
4529}
4530
4531static void qla4xxx_slave_destroy(struct scsi_device *sdev)
4532{
4533 scsi_deactivate_tcq(sdev, 1);
4534}
4535
4536/**
4537 * qla4xxx_del_from_active_array - returns an active srb
4538 * @ha: Pointer to host adapter structure.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02004539 * @index: index into the active_array
David Somayajuluafaf5a22006-09-19 10:28:00 -07004540 *
4541 * This routine removes and returns the srb at the specified index
4542 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304543struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
4544 uint32_t index)
David Somayajuluafaf5a22006-09-19 10:28:00 -07004545{
4546 struct srb *srb = NULL;
Vikas Chaudhary53698872010-04-28 11:41:59 +05304547 struct scsi_cmnd *cmd = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004548
Vikas Chaudhary53698872010-04-28 11:41:59 +05304549 cmd = scsi_host_find_tag(ha->host, index);
4550 if (!cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07004551 return srb;
4552
Vikas Chaudhary53698872010-04-28 11:41:59 +05304553 srb = (struct srb *)CMD_SP(cmd);
4554 if (!srb)
David Somayajuluafaf5a22006-09-19 10:28:00 -07004555 return srb;
4556
4557 /* update counters */
4558 if (srb->flags & SRB_DMA_VALID) {
4559 ha->req_q_count += srb->iocb_cnt;
4560 ha->iocb_cnt -= srb->iocb_cnt;
4561 if (srb->cmd)
Vikas Chaudhary53698872010-04-28 11:41:59 +05304562 srb->cmd->host_scribble =
4563 (unsigned char *)(unsigned long) MAX_SRBS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004564 }
4565 return srb;
4566}
4567
4568/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07004569 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304570 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07004571 * @cmd: Scsi Command to wait on.
4572 *
4573 * This routine waits for the command to be returned by the Firmware
4574 * for some max time.
4575 **/
4576static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
4577 struct scsi_cmnd *cmd)
4578{
4579 int done = 0;
4580 struct srb *rp;
4581 uint32_t max_wait_time = EH_WAIT_CMD_TOV;
Lalit Chandivade2232be02010-07-30 14:38:47 +05304582 int ret = SUCCESS;
4583
4584 /* Dont wait on command if PCI error is being handled
4585 * by PCI AER driver
4586 */
4587 if (unlikely(pci_channel_offline(ha->pdev)) ||
4588 (test_bit(AF_EEH_BUSY, &ha->flags))) {
4589 ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
4590 ha->host_no, __func__);
4591 return ret;
4592 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07004593
4594 do {
4595 /* Checking to see if its returned to OS */
Vikas Chaudhary53698872010-04-28 11:41:59 +05304596 rp = (struct srb *) CMD_SP(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004597 if (rp == NULL) {
4598 done++;
4599 break;
4600 }
4601
4602 msleep(2000);
4603 } while (max_wait_time--);
4604
4605 return done;
4606}
4607
4608/**
4609 * qla4xxx_wait_for_hba_online - waits for HBA to come online
4610 * @ha: Pointer to host adapter structure
4611 **/
4612static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
4613{
4614 unsigned long wait_online;
4615
Vikas Chaudharyf581a3f2010-10-06 22:47:48 -07004616 wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004617 while (time_before(jiffies, wait_online)) {
4618
4619 if (adapter_up(ha))
4620 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004621
4622 msleep(2000);
4623 }
4624
4625 return QLA_ERROR;
4626}
4627
4628/**
Mike Christiece545032008-02-29 18:25:20 -06004629 * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02004630 * @ha: pointer to HBA
David Somayajuluafaf5a22006-09-19 10:28:00 -07004631 * @t: target id
4632 * @l: lun id
4633 *
4634 * This function waits for all outstanding commands to a lun to complete. It
4635 * returns 0 if all pending commands are returned and 1 otherwise.
4636 **/
Mike Christiece545032008-02-29 18:25:20 -06004637static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
4638 struct scsi_target *stgt,
4639 struct scsi_device *sdev)
David Somayajuluafaf5a22006-09-19 10:28:00 -07004640{
4641 int cnt;
4642 int status = 0;
4643 struct scsi_cmnd *cmd;
4644
4645 /*
Mike Christiece545032008-02-29 18:25:20 -06004646 * Waiting for all commands for the designated target or dev
4647 * in the active array
David Somayajuluafaf5a22006-09-19 10:28:00 -07004648 */
4649 for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
4650 cmd = scsi_host_find_tag(ha->host, cnt);
Mike Christiece545032008-02-29 18:25:20 -06004651 if (cmd && stgt == scsi_target(cmd->device) &&
4652 (!sdev || sdev == cmd->device)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07004653 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
4654 status++;
4655 break;
4656 }
4657 }
4658 }
4659 return status;
4660}
4661
4662/**
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304663 * qla4xxx_eh_abort - callback for abort task.
4664 * @cmd: Pointer to Linux's SCSI command structure
4665 *
4666 * This routine is called by the Linux OS to abort the specified
4667 * command.
4668 **/
4669static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
4670{
4671 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
4672 unsigned int id = cmd->device->id;
4673 unsigned int lun = cmd->device->lun;
Mike Christie92b3e5b2010-10-06 22:51:17 -07004674 unsigned long flags;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304675 struct srb *srb = NULL;
4676 int ret = SUCCESS;
4677 int wait = 0;
4678
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304679 ql4_printk(KERN_INFO, ha,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004680 "scsi%ld:%d:%d: Abort command issued cmd=%p\n",
4681 ha->host_no, id, lun, cmd);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304682
Mike Christie92b3e5b2010-10-06 22:51:17 -07004683 spin_lock_irqsave(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304684 srb = (struct srb *) CMD_SP(cmd);
Mike Christie92b3e5b2010-10-06 22:51:17 -07004685 if (!srb) {
4686 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304687 return SUCCESS;
Mike Christie92b3e5b2010-10-06 22:51:17 -07004688 }
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304689 kref_get(&srb->srb_ref);
Mike Christie92b3e5b2010-10-06 22:51:17 -07004690 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304691
4692 if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
4693 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
4694 ha->host_no, id, lun));
4695 ret = FAILED;
4696 } else {
4697 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
4698 ha->host_no, id, lun));
4699 wait = 1;
4700 }
4701
4702 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
4703
4704 /* Wait for command to complete */
4705 if (wait) {
4706 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
4707 DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
4708 ha->host_no, id, lun));
4709 ret = FAILED;
4710 }
4711 }
4712
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304713 ql4_printk(KERN_INFO, ha,
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304714 "scsi%ld:%d:%d: Abort command - %s\n",
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004715 ha->host_no, id, lun, (ret == SUCCESS) ? "succeeded" : "failed");
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304716
4717 return ret;
4718}
4719
4720/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07004721 * qla4xxx_eh_device_reset - callback for target reset.
4722 * @cmd: Pointer to Linux's SCSI command structure
4723 *
4724 * This routine is called by the Linux OS to reset all luns on the
4725 * specified target.
4726 **/
4727static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
4728{
4729 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
4730 struct ddb_entry *ddb_entry = cmd->device->hostdata;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004731 int ret = FAILED, stat;
4732
Karen Higgins612f7342009-07-15 15:03:01 -05004733 if (!ddb_entry)
David Somayajuluafaf5a22006-09-19 10:28:00 -07004734 return ret;
4735
Mike Christiec01be6d2010-07-22 16:59:49 +05304736 ret = iscsi_block_scsi_eh(cmd);
4737 if (ret)
4738 return ret;
4739 ret = FAILED;
4740
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304741 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07004742 "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
4743 cmd->device->channel, cmd->device->id, cmd->device->lun);
4744
4745 DEBUG2(printk(KERN_INFO
4746 "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
4747 "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
Jens Axboe242f9dc2008-09-14 05:55:09 -07004748 cmd, jiffies, cmd->request->timeout / HZ,
David Somayajuluafaf5a22006-09-19 10:28:00 -07004749 ha->dpc_flags, cmd->result, cmd->allowed));
4750
4751 /* FIXME: wait for hba to go online */
4752 stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
4753 if (stat != QLA_SUCCESS) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304754 ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004755 goto eh_dev_reset_done;
4756 }
4757
Mike Christiece545032008-02-29 18:25:20 -06004758 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
4759 cmd->device)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304760 ql4_printk(KERN_INFO, ha,
Mike Christiece545032008-02-29 18:25:20 -06004761 "DEVICE RESET FAILED - waiting for "
4762 "commands.\n");
4763 goto eh_dev_reset_done;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004764 }
4765
David C Somayajulu9d562912008-03-19 11:23:03 -07004766 /* Send marker. */
4767 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
4768 MM_LUN_RESET) != QLA_SUCCESS)
4769 goto eh_dev_reset_done;
4770
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304771 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07004772 "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
4773 ha->host_no, cmd->device->channel, cmd->device->id,
4774 cmd->device->lun);
4775
4776 ret = SUCCESS;
4777
4778eh_dev_reset_done:
4779
4780 return ret;
4781}
4782
4783/**
Mike Christiece545032008-02-29 18:25:20 -06004784 * qla4xxx_eh_target_reset - callback for target reset.
4785 * @cmd: Pointer to Linux's SCSI command structure
4786 *
4787 * This routine is called by the Linux OS to reset the target.
4788 **/
4789static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
4790{
4791 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
4792 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christiec01be6d2010-07-22 16:59:49 +05304793 int stat, ret;
Mike Christiece545032008-02-29 18:25:20 -06004794
4795 if (!ddb_entry)
4796 return FAILED;
4797
Mike Christiec01be6d2010-07-22 16:59:49 +05304798 ret = iscsi_block_scsi_eh(cmd);
4799 if (ret)
4800 return ret;
4801
Mike Christiece545032008-02-29 18:25:20 -06004802 starget_printk(KERN_INFO, scsi_target(cmd->device),
4803 "WARM TARGET RESET ISSUED.\n");
4804
4805 DEBUG2(printk(KERN_INFO
4806 "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
4807 "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
Jens Axboe242f9dc2008-09-14 05:55:09 -07004808 ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
Mike Christiece545032008-02-29 18:25:20 -06004809 ha->dpc_flags, cmd->result, cmd->allowed));
4810
4811 stat = qla4xxx_reset_target(ha, ddb_entry);
4812 if (stat != QLA_SUCCESS) {
4813 starget_printk(KERN_INFO, scsi_target(cmd->device),
4814 "WARM TARGET RESET FAILED.\n");
4815 return FAILED;
4816 }
4817
Mike Christiece545032008-02-29 18:25:20 -06004818 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
4819 NULL)) {
4820 starget_printk(KERN_INFO, scsi_target(cmd->device),
4821 "WARM TARGET DEVICE RESET FAILED - "
4822 "waiting for commands.\n");
4823 return FAILED;
4824 }
4825
David C Somayajulu9d562912008-03-19 11:23:03 -07004826 /* Send marker. */
4827 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
4828 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
4829 starget_printk(KERN_INFO, scsi_target(cmd->device),
4830 "WARM TARGET DEVICE RESET FAILED - "
4831 "marker iocb failed.\n");
4832 return FAILED;
4833 }
4834
Mike Christiece545032008-02-29 18:25:20 -06004835 starget_printk(KERN_INFO, scsi_target(cmd->device),
4836 "WARM TARGET RESET SUCCEEDED.\n");
4837 return SUCCESS;
4838}
4839
4840/**
Sarang Radke8a288962011-12-06 02:34:10 -08004841 * qla4xxx_is_eh_active - check if error handler is running
4842 * @shost: Pointer to SCSI Host struct
4843 *
4844 * This routine finds that if reset host is called in EH
4845 * scenario or from some application like sg_reset
4846 **/
4847static int qla4xxx_is_eh_active(struct Scsi_Host *shost)
4848{
4849 if (shost->shost_state == SHOST_RECOVERY)
4850 return 1;
4851 return 0;
4852}
4853
4854/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07004855 * qla4xxx_eh_host_reset - kernel callback
4856 * @cmd: Pointer to Linux's SCSI command structure
4857 *
4858 * This routine is invoked by the Linux kernel to perform fatal error
4859 * recovery on the specified adapter.
4860 **/
4861static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
4862{
4863 int return_status = FAILED;
4864 struct scsi_qla_host *ha;
4865
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004866 ha = to_qla_host(cmd->device->host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004867
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304868 if (ql4xdontresethba) {
4869 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
4870 ha->host_no, __func__));
Sarang Radke8a288962011-12-06 02:34:10 -08004871
4872 /* Clear outstanding srb in queues */
4873 if (qla4xxx_is_eh_active(cmd->device->host))
4874 qla4xxx_abort_active_cmds(ha, DID_ABORT << 16);
4875
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304876 return FAILED;
4877 }
4878
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304879 ql4_printk(KERN_INFO, ha,
Karen Higginsdca05c42009-07-15 15:03:00 -05004880 "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
David Somayajuluafaf5a22006-09-19 10:28:00 -07004881 cmd->device->channel, cmd->device->id, cmd->device->lun);
4882
4883 if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
4884 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
4885 "DEAD.\n", ha->host_no, cmd->device->channel,
4886 __func__));
4887
4888 return FAILED;
4889 }
4890
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304891 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
4892 if (is_qla8022(ha))
4893 set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
4894 else
4895 set_bit(DPC_RESET_HA, &ha->dpc_flags);
4896 }
Mike Christie50a29ae2008-03-04 13:26:53 -06004897
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304898 if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
David Somayajuluafaf5a22006-09-19 10:28:00 -07004899 return_status = SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004900
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304901 ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004902 return_status == FAILED ? "FAILED" : "SUCCEEDED");
David Somayajuluafaf5a22006-09-19 10:28:00 -07004903
4904 return return_status;
4905}
4906
Vikas Chaudhary95d31262011-08-12 02:51:29 -07004907static int qla4xxx_context_reset(struct scsi_qla_host *ha)
4908{
4909 uint32_t mbox_cmd[MBOX_REG_COUNT];
4910 uint32_t mbox_sts[MBOX_REG_COUNT];
4911 struct addr_ctrl_blk_def *acb = NULL;
4912 uint32_t acb_len = sizeof(struct addr_ctrl_blk_def);
4913 int rval = QLA_SUCCESS;
4914 dma_addr_t acb_dma;
4915
4916 acb = dma_alloc_coherent(&ha->pdev->dev,
4917 sizeof(struct addr_ctrl_blk_def),
4918 &acb_dma, GFP_KERNEL);
4919 if (!acb) {
4920 ql4_printk(KERN_ERR, ha, "%s: Unable to alloc acb\n",
4921 __func__);
4922 rval = -ENOMEM;
4923 goto exit_port_reset;
4924 }
4925
4926 memset(acb, 0, acb_len);
4927
4928 rval = qla4xxx_get_acb(ha, acb_dma, PRIMARI_ACB, acb_len);
4929 if (rval != QLA_SUCCESS) {
4930 rval = -EIO;
4931 goto exit_free_acb;
4932 }
4933
4934 rval = qla4xxx_disable_acb(ha);
4935 if (rval != QLA_SUCCESS) {
4936 rval = -EIO;
4937 goto exit_free_acb;
4938 }
4939
4940 wait_for_completion_timeout(&ha->disable_acb_comp,
4941 DISABLE_ACB_TOV * HZ);
4942
4943 rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], acb_dma);
4944 if (rval != QLA_SUCCESS) {
4945 rval = -EIO;
4946 goto exit_free_acb;
4947 }
4948
4949exit_free_acb:
4950 dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk_def),
4951 acb, acb_dma);
4952exit_port_reset:
4953 DEBUG2(ql4_printk(KERN_INFO, ha, "%s %s\n", __func__,
4954 rval == QLA_SUCCESS ? "SUCCEEDED" : "FAILED"));
4955 return rval;
4956}
4957
4958static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type)
4959{
4960 struct scsi_qla_host *ha = to_qla_host(shost);
4961 int rval = QLA_SUCCESS;
4962
4963 if (ql4xdontresethba) {
4964 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Don't Reset HBA\n",
4965 __func__));
4966 rval = -EPERM;
4967 goto exit_host_reset;
4968 }
4969
4970 rval = qla4xxx_wait_for_hba_online(ha);
4971 if (rval != QLA_SUCCESS) {
4972 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Unable to reset host "
4973 "adapter\n", __func__));
4974 rval = -EIO;
4975 goto exit_host_reset;
4976 }
4977
4978 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
4979 goto recover_adapter;
4980
4981 switch (reset_type) {
4982 case SCSI_ADAPTER_RESET:
4983 set_bit(DPC_RESET_HA, &ha->dpc_flags);
4984 break;
4985 case SCSI_FIRMWARE_RESET:
4986 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
4987 if (is_qla8022(ha))
4988 /* set firmware context reset */
4989 set_bit(DPC_RESET_HA_FW_CONTEXT,
4990 &ha->dpc_flags);
4991 else {
4992 rval = qla4xxx_context_reset(ha);
4993 goto exit_host_reset;
4994 }
4995 }
4996 break;
4997 }
4998
4999recover_adapter:
5000 rval = qla4xxx_recover_adapter(ha);
5001 if (rval != QLA_SUCCESS) {
5002 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: recover adapter fail\n",
5003 __func__));
5004 rval = -EIO;
5005 }
5006
5007exit_host_reset:
5008 return rval;
5009}
5010
Lalit Chandivade2232be02010-07-30 14:38:47 +05305011/* PCI AER driver recovers from all correctable errors w/o
5012 * driver intervention. For uncorrectable errors PCI AER
5013 * driver calls the following device driver's callbacks
5014 *
5015 * - Fatal Errors - link_reset
5016 * - Non-Fatal Errors - driver's pci_error_detected() which
5017 * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
5018 *
5019 * PCI AER driver calls
5020 * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
5021 * returns RECOVERED or NEED_RESET if fw_hung
5022 * NEED_RESET - driver's slot_reset()
5023 * DISCONNECT - device is dead & cannot recover
5024 * RECOVERED - driver's pci_resume()
5025 */
5026static pci_ers_result_t
5027qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
5028{
5029 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
5030
5031 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
5032 ha->host_no, __func__, state);
5033
5034 if (!is_aer_supported(ha))
5035 return PCI_ERS_RESULT_NONE;
5036
5037 switch (state) {
5038 case pci_channel_io_normal:
5039 clear_bit(AF_EEH_BUSY, &ha->flags);
5040 return PCI_ERS_RESULT_CAN_RECOVER;
5041 case pci_channel_io_frozen:
5042 set_bit(AF_EEH_BUSY, &ha->flags);
5043 qla4xxx_mailbox_premature_completion(ha);
5044 qla4xxx_free_irqs(ha);
5045 pci_disable_device(pdev);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07005046 /* Return back all IOs */
5047 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
Lalit Chandivade2232be02010-07-30 14:38:47 +05305048 return PCI_ERS_RESULT_NEED_RESET;
5049 case pci_channel_io_perm_failure:
5050 set_bit(AF_EEH_BUSY, &ha->flags);
5051 set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
5052 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
5053 return PCI_ERS_RESULT_DISCONNECT;
5054 }
5055 return PCI_ERS_RESULT_NEED_RESET;
5056}
5057
5058/**
5059 * qla4xxx_pci_mmio_enabled() gets called if
5060 * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
5061 * and read/write to the device still works.
5062 **/
5063static pci_ers_result_t
5064qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
5065{
5066 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
5067
5068 if (!is_aer_supported(ha))
5069 return PCI_ERS_RESULT_NONE;
5070
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07005071 return PCI_ERS_RESULT_RECOVERED;
Lalit Chandivade2232be02010-07-30 14:38:47 +05305072}
5073
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07005074static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
Lalit Chandivade2232be02010-07-30 14:38:47 +05305075{
5076 uint32_t rval = QLA_ERROR;
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07005077 uint32_t ret = 0;
Lalit Chandivade2232be02010-07-30 14:38:47 +05305078 int fn;
5079 struct pci_dev *other_pdev = NULL;
5080
5081 ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
5082
5083 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
5084
5085 if (test_bit(AF_ONLINE, &ha->flags)) {
5086 clear_bit(AF_ONLINE, &ha->flags);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05005087 clear_bit(AF_LINK_UP, &ha->flags);
5088 iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
Lalit Chandivade2232be02010-07-30 14:38:47 +05305089 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
Lalit Chandivade2232be02010-07-30 14:38:47 +05305090 }
5091
5092 fn = PCI_FUNC(ha->pdev->devfn);
5093 while (fn > 0) {
5094 fn--;
5095 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
5096 "func %x\n", ha->host_no, __func__, fn);
5097 /* Get the pci device given the domain, bus,
5098 * slot/function number */
5099 other_pdev =
5100 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
5101 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
5102 fn));
5103
5104 if (!other_pdev)
5105 continue;
5106
5107 if (atomic_read(&other_pdev->enable_cnt)) {
5108 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
5109 "func in enabled state%x\n", ha->host_no,
5110 __func__, fn);
5111 pci_dev_put(other_pdev);
5112 break;
5113 }
5114 pci_dev_put(other_pdev);
5115 }
5116
5117 /* The first function on the card, the reset owner will
5118 * start & initialize the firmware. The other functions
5119 * on the card will reset the firmware context
5120 */
5121 if (!fn) {
5122 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
5123 "0x%x is the owner\n", ha->host_no, __func__,
5124 ha->pdev->devfn);
5125
5126 qla4_8xxx_idc_lock(ha);
5127 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
5128 QLA82XX_DEV_COLD);
5129
5130 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
5131 QLA82XX_IDC_VERSION);
5132
5133 qla4_8xxx_idc_unlock(ha);
5134 clear_bit(AF_FW_RECOVERY, &ha->flags);
Mike Christie13483732011-12-01 21:38:41 -06005135 rval = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
Lalit Chandivade2232be02010-07-30 14:38:47 +05305136 qla4_8xxx_idc_lock(ha);
5137
5138 if (rval != QLA_SUCCESS) {
5139 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
5140 "FAILED\n", ha->host_no, __func__);
5141 qla4_8xxx_clear_drv_active(ha);
5142 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
5143 QLA82XX_DEV_FAILED);
5144 } else {
5145 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
5146 "READY\n", ha->host_no, __func__);
5147 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
5148 QLA82XX_DEV_READY);
5149 /* Clear driver state register */
5150 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
5151 qla4_8xxx_set_drv_active(ha);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07005152 ret = qla4xxx_request_irqs(ha);
5153 if (ret) {
5154 ql4_printk(KERN_WARNING, ha, "Failed to "
5155 "reserve interrupt %d already in use.\n",
5156 ha->pdev->irq);
5157 rval = QLA_ERROR;
5158 } else {
5159 ha->isp_ops->enable_intrs(ha);
5160 rval = QLA_SUCCESS;
5161 }
Lalit Chandivade2232be02010-07-30 14:38:47 +05305162 }
5163 qla4_8xxx_idc_unlock(ha);
5164 } else {
5165 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
5166 "the reset owner\n", ha->host_no, __func__,
5167 ha->pdev->devfn);
5168 if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
5169 QLA82XX_DEV_READY)) {
5170 clear_bit(AF_FW_RECOVERY, &ha->flags);
Mike Christie13483732011-12-01 21:38:41 -06005171 rval = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07005172 if (rval == QLA_SUCCESS) {
5173 ret = qla4xxx_request_irqs(ha);
5174 if (ret) {
5175 ql4_printk(KERN_WARNING, ha, "Failed to"
5176 " reserve interrupt %d already in"
5177 " use.\n", ha->pdev->irq);
5178 rval = QLA_ERROR;
5179 } else {
5180 ha->isp_ops->enable_intrs(ha);
5181 rval = QLA_SUCCESS;
5182 }
5183 }
Lalit Chandivade2232be02010-07-30 14:38:47 +05305184 qla4_8xxx_idc_lock(ha);
5185 qla4_8xxx_set_drv_active(ha);
5186 qla4_8xxx_idc_unlock(ha);
5187 }
5188 }
5189 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
5190 return rval;
5191}
5192
5193static pci_ers_result_t
5194qla4xxx_pci_slot_reset(struct pci_dev *pdev)
5195{
5196 pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
5197 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
5198 int rc;
5199
5200 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
5201 ha->host_no, __func__);
5202
5203 if (!is_aer_supported(ha))
5204 return PCI_ERS_RESULT_NONE;
5205
5206 /* Restore the saved state of PCIe device -
5207 * BAR registers, PCI Config space, PCIX, MSI,
5208 * IOV states
5209 */
5210 pci_restore_state(pdev);
5211
5212 /* pci_restore_state() clears the saved_state flag of the device
5213 * save restored state which resets saved_state flag
5214 */
5215 pci_save_state(pdev);
5216
5217 /* Initialize device or resume if in suspended state */
5218 rc = pci_enable_device(pdev);
5219 if (rc) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005220 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Can't re-enable "
Lalit Chandivade2232be02010-07-30 14:38:47 +05305221 "device after reset\n", ha->host_no, __func__);
5222 goto exit_slot_reset;
5223 }
5224
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07005225 ha->isp_ops->disable_intrs(ha);
Lalit Chandivade2232be02010-07-30 14:38:47 +05305226
5227 if (is_qla8022(ha)) {
5228 if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
5229 ret = PCI_ERS_RESULT_RECOVERED;
5230 goto exit_slot_reset;
5231 } else
5232 goto exit_slot_reset;
5233 }
5234
5235exit_slot_reset:
5236 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
5237 "device after reset\n", ha->host_no, __func__, ret);
5238 return ret;
5239}
5240
5241static void
5242qla4xxx_pci_resume(struct pci_dev *pdev)
5243{
5244 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
5245 int ret;
5246
5247 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
5248 ha->host_no, __func__);
5249
5250 ret = qla4xxx_wait_for_hba_online(ha);
5251 if (ret != QLA_SUCCESS) {
5252 ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
5253 "resume I/O from slot/link_reset\n", ha->host_no,
5254 __func__);
5255 }
5256
5257 pci_cleanup_aer_uncorrect_error_status(pdev);
5258 clear_bit(AF_EEH_BUSY, &ha->flags);
5259}
5260
5261static struct pci_error_handlers qla4xxx_err_handler = {
5262 .error_detected = qla4xxx_pci_error_detected,
5263 .mmio_enabled = qla4xxx_pci_mmio_enabled,
5264 .slot_reset = qla4xxx_pci_slot_reset,
5265 .resume = qla4xxx_pci_resume,
5266};
5267
David Somayajuluafaf5a22006-09-19 10:28:00 -07005268static struct pci_device_id qla4xxx_pci_tbl[] = {
5269 {
5270 .vendor = PCI_VENDOR_ID_QLOGIC,
5271 .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
5272 .subvendor = PCI_ANY_ID,
5273 .subdevice = PCI_ANY_ID,
5274 },
5275 {
5276 .vendor = PCI_VENDOR_ID_QLOGIC,
5277 .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
5278 .subvendor = PCI_ANY_ID,
5279 .subdevice = PCI_ANY_ID,
5280 },
David C Somayajulud9150582006-11-15 17:38:40 -08005281 {
5282 .vendor = PCI_VENDOR_ID_QLOGIC,
5283 .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
5284 .subvendor = PCI_ANY_ID,
5285 .subdevice = PCI_ANY_ID,
5286 },
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05305287 {
5288 .vendor = PCI_VENDOR_ID_QLOGIC,
5289 .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
5290 .subvendor = PCI_ANY_ID,
5291 .subdevice = PCI_ANY_ID,
5292 },
David Somayajuluafaf5a22006-09-19 10:28:00 -07005293 {0, 0},
5294};
5295MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
5296
Adrian Bunk47975472007-04-26 00:35:16 -07005297static struct pci_driver qla4xxx_pci_driver = {
David Somayajuluafaf5a22006-09-19 10:28:00 -07005298 .name = DRIVER_NAME,
5299 .id_table = qla4xxx_pci_tbl,
5300 .probe = qla4xxx_probe_adapter,
5301 .remove = qla4xxx_remove_adapter,
Lalit Chandivade2232be02010-07-30 14:38:47 +05305302 .err_handler = &qla4xxx_err_handler,
David Somayajuluafaf5a22006-09-19 10:28:00 -07005303};
5304
5305static int __init qla4xxx_module_init(void)
5306{
5307 int ret;
5308
5309 /* Allocate cache for SRBs. */
5310 srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09005311 SLAB_HWCACHE_ALIGN, NULL);
David Somayajuluafaf5a22006-09-19 10:28:00 -07005312 if (srb_cachep == NULL) {
5313 printk(KERN_ERR
5314 "%s: Unable to allocate SRB cache..."
5315 "Failing load!\n", DRIVER_NAME);
5316 ret = -ENOMEM;
5317 goto no_srp_cache;
5318 }
5319
5320 /* Derive version string. */
5321 strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
Andrew Vasquez11010fe2006-10-06 09:54:59 -07005322 if (ql4xextended_error_logging)
David Somayajuluafaf5a22006-09-19 10:28:00 -07005323 strcat(qla4xxx_version_str, "-debug");
5324
5325 qla4xxx_scsi_transport =
5326 iscsi_register_transport(&qla4xxx_iscsi_transport);
5327 if (!qla4xxx_scsi_transport){
5328 ret = -ENODEV;
5329 goto release_srb_cache;
5330 }
5331
David Somayajuluafaf5a22006-09-19 10:28:00 -07005332 ret = pci_register_driver(&qla4xxx_pci_driver);
5333 if (ret)
5334 goto unregister_transport;
5335
5336 printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
5337 return 0;
Doug Maxey5ae16db2006-10-05 23:50:07 -05005338
David Somayajuluafaf5a22006-09-19 10:28:00 -07005339unregister_transport:
5340 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
5341release_srb_cache:
5342 kmem_cache_destroy(srb_cachep);
5343no_srp_cache:
5344 return ret;
5345}
5346
5347static void __exit qla4xxx_module_exit(void)
5348{
David Somayajuluafaf5a22006-09-19 10:28:00 -07005349 pci_unregister_driver(&qla4xxx_pci_driver);
5350 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
5351 kmem_cache_destroy(srb_cachep);
5352}
5353
5354module_init(qla4xxx_module_init);
5355module_exit(qla4xxx_module_exit);
5356
5357MODULE_AUTHOR("QLogic Corporation");
5358MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
5359MODULE_LICENSE("GPL");
5360MODULE_VERSION(QLA4XXX_DRIVER_VERSION);