blob: 089443add47ff360b22888e3c3b356f2092ee632 [file] [log] [blame]
David Somayajuluafaf5a22006-09-19 10:28:00 -07001/*
2 * QLogic iSCSI HBA Driver
Vikas Chaudhary7d01d062010-12-02 22:12:51 -08003 * Copyright (c) 2003-2010 QLogic Corporation
David Somayajuluafaf5a22006-09-19 10:28:00 -07004 *
5 * See LICENSE.qla4xxx for copyright and licensing details.
6 */
7#include <linux/moduleparam.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
Manish Rangankar2a991c22011-07-25 13:48:55 -05009#include <linux/blkdev.h>
10#include <linux/iscsi_boot_sysfs.h>
Mike Christie13483732011-12-01 21:38:41 -060011#include <linux/inet.h>
David Somayajuluafaf5a22006-09-19 10:28:00 -070012
13#include <scsi/scsi_tcq.h>
14#include <scsi/scsicam.h>
15
16#include "ql4_def.h"
David C Somayajulubee4fe82007-05-23 18:03:32 -070017#include "ql4_version.h"
18#include "ql4_glbl.h"
19#include "ql4_dbg.h"
20#include "ql4_inline.h"
David Somayajuluafaf5a22006-09-19 10:28:00 -070021
22/*
23 * Driver version
24 */
Adrian Bunk47975472007-04-26 00:35:16 -070025static char qla4xxx_version_str[40];
David Somayajuluafaf5a22006-09-19 10:28:00 -070026
27/*
28 * SRB allocation cache
29 */
Christoph Lametere18b8902006-12-06 20:33:20 -080030static struct kmem_cache *srb_cachep;
David Somayajuluafaf5a22006-09-19 10:28:00 -070031
32/*
33 * Module parameter information and variables
34 */
Mike Christie13483732011-12-01 21:38:41 -060035int ql4xdisablesysfsboot = 1;
36module_param(ql4xdisablesysfsboot, int, S_IRUGO | S_IWUSR);
37MODULE_PARM_DESC(ql4xdisablesysfsboot,
Karen Higginsa4e8a712012-01-11 02:44:20 -080038 " Set to disable exporting boot targets to sysfs.\n"
39 "\t\t 0 - Export boot targets\n"
40 "\t\t 1 - Do not export boot targets (Default)");
Mike Christie13483732011-12-01 21:38:41 -060041
David Somayajuluafaf5a22006-09-19 10:28:00 -070042int ql4xdontresethba = 0;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053043module_param(ql4xdontresethba, int, S_IRUGO | S_IWUSR);
David Somayajuluafaf5a22006-09-19 10:28:00 -070044MODULE_PARM_DESC(ql4xdontresethba,
Karen Higginsa4e8a712012-01-11 02:44:20 -080045 " Don't reset the HBA for driver recovery.\n"
46 "\t\t 0 - It will reset HBA (Default)\n"
47 "\t\t 1 - It will NOT reset HBA");
David Somayajuluafaf5a22006-09-19 10:28:00 -070048
Karen Higginsa4e8a712012-01-11 02:44:20 -080049int ql4xextended_error_logging;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053050module_param(ql4xextended_error_logging, int, S_IRUGO | S_IWUSR);
Andrew Vasquez11010fe2006-10-06 09:54:59 -070051MODULE_PARM_DESC(ql4xextended_error_logging,
Karen Higginsa4e8a712012-01-11 02:44:20 -080052 " Option to enable extended error logging.\n"
53 "\t\t 0 - no logging (Default)\n"
54 "\t\t 2 - debug logging");
David Somayajuluafaf5a22006-09-19 10:28:00 -070055
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053056int ql4xenablemsix = 1;
57module_param(ql4xenablemsix, int, S_IRUGO|S_IWUSR);
58MODULE_PARM_DESC(ql4xenablemsix,
Karen Higginsa4e8a712012-01-11 02:44:20 -080059 " Set to enable MSI or MSI-X interrupt mechanism.\n"
60 "\t\t 0 = enable INTx interrupt mechanism.\n"
61 "\t\t 1 = enable MSI-X interrupt mechanism (Default).\n"
62 "\t\t 2 = enable MSI interrupt mechanism.");
David C Somayajulu477ffb92007-01-22 12:26:11 -080063
Mike Christied510d962008-07-11 19:50:33 -050064#define QL4_DEF_QDEPTH 32
Vikas Chaudhary8bb40332011-03-21 03:34:31 -070065static int ql4xmaxqdepth = QL4_DEF_QDEPTH;
66module_param(ql4xmaxqdepth, int, S_IRUGO | S_IWUSR);
67MODULE_PARM_DESC(ql4xmaxqdepth,
Karen Higginsa4e8a712012-01-11 02:44:20 -080068 " Maximum queue depth to report for target devices.\n"
69 "\t\t Default: 32.");
Mike Christied510d962008-07-11 19:50:33 -050070
Vikas Chaudhary30387272011-03-21 03:34:32 -070071static int ql4xsess_recovery_tmo = QL4_SESS_RECOVERY_TMO;
72module_param(ql4xsess_recovery_tmo, int, S_IRUGO);
73MODULE_PARM_DESC(ql4xsess_recovery_tmo,
74 "Target Session Recovery Timeout.\n"
Karen Higginsa4e8a712012-01-11 02:44:20 -080075 "\t\t Default: 120 sec.");
Vikas Chaudhary30387272011-03-21 03:34:32 -070076
Manish Rangankarb3a271a2011-07-25 13:48:53 -050077static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -070078/*
79 * SCSI host template entry points
80 */
Adrian Bunk47975472007-04-26 00:35:16 -070081static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -070082
83/*
84 * iSCSI template entry points
85 */
David Somayajuluafaf5a22006-09-19 10:28:00 -070086static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
87 enum iscsi_param param, char *buf);
Mike Christieaa1e93a2007-05-30 12:57:09 -050088static int qla4xxx_host_get_param(struct Scsi_Host *shost,
89 enum iscsi_host_param param, char *buf);
Mike Christie00c31882011-10-06 03:56:59 -050090static int qla4xxx_iface_set_param(struct Scsi_Host *shost, void *data,
91 uint32_t len);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -050092static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
93 enum iscsi_param_type param_type,
94 int param, char *buf);
Mike Christie5c656af2009-07-15 15:02:59 -050095static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
Manish Rangankarb3a271a2011-07-25 13:48:53 -050096static struct iscsi_endpoint *qla4xxx_ep_connect(struct Scsi_Host *shost,
97 struct sockaddr *dst_addr,
98 int non_blocking);
99static int qla4xxx_ep_poll(struct iscsi_endpoint *ep, int timeout_ms);
100static void qla4xxx_ep_disconnect(struct iscsi_endpoint *ep);
101static int qla4xxx_get_ep_param(struct iscsi_endpoint *ep,
102 enum iscsi_param param, char *buf);
103static int qla4xxx_conn_start(struct iscsi_cls_conn *conn);
104static struct iscsi_cls_conn *
105qla4xxx_conn_create(struct iscsi_cls_session *cls_sess, uint32_t conn_idx);
106static int qla4xxx_conn_bind(struct iscsi_cls_session *cls_session,
107 struct iscsi_cls_conn *cls_conn,
108 uint64_t transport_fd, int is_leading);
109static void qla4xxx_conn_destroy(struct iscsi_cls_conn *conn);
110static struct iscsi_cls_session *
111qla4xxx_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max,
112 uint16_t qdepth, uint32_t initial_cmdsn);
113static void qla4xxx_session_destroy(struct iscsi_cls_session *sess);
114static void qla4xxx_task_work(struct work_struct *wdata);
115static int qla4xxx_alloc_pdu(struct iscsi_task *, uint8_t);
116static int qla4xxx_task_xmit(struct iscsi_task *);
117static void qla4xxx_task_cleanup(struct iscsi_task *);
118static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session);
119static void qla4xxx_conn_get_stats(struct iscsi_cls_conn *cls_conn,
120 struct iscsi_stats *stats);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700121/*
122 * SCSI host template entry points
123 */
Jeff Garzikf2812332010-11-16 02:10:29 -0500124static int qla4xxx_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmd);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530125static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700126static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
Mike Christiece545032008-02-29 18:25:20 -0600127static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700128static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
129static int qla4xxx_slave_alloc(struct scsi_device *device);
130static int qla4xxx_slave_configure(struct scsi_device *device);
131static void qla4xxx_slave_destroy(struct scsi_device *sdev);
Al Viro587a1f12011-07-23 23:11:19 -0400132static umode_t ql4_attr_is_visible(int param_type, int param);
Vikas Chaudhary95d31262011-08-12 02:51:29 -0700133static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700134
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530135static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
136 QLA82XX_LEGACY_INTR_CONFIG;
137
David Somayajuluafaf5a22006-09-19 10:28:00 -0700138static struct scsi_host_template qla4xxx_driver_template = {
139 .module = THIS_MODULE,
140 .name = DRIVER_NAME,
141 .proc_name = DRIVER_NAME,
142 .queuecommand = qla4xxx_queuecommand,
143
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530144 .eh_abort_handler = qla4xxx_eh_abort,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700145 .eh_device_reset_handler = qla4xxx_eh_device_reset,
Mike Christiece545032008-02-29 18:25:20 -0600146 .eh_target_reset_handler = qla4xxx_eh_target_reset,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700147 .eh_host_reset_handler = qla4xxx_eh_host_reset,
Mike Christie5c656af2009-07-15 15:02:59 -0500148 .eh_timed_out = qla4xxx_eh_cmd_timed_out,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700149
150 .slave_configure = qla4xxx_slave_configure,
151 .slave_alloc = qla4xxx_slave_alloc,
152 .slave_destroy = qla4xxx_slave_destroy,
153
154 .this_id = -1,
155 .cmd_per_lun = 3,
156 .use_clustering = ENABLE_CLUSTERING,
157 .sg_tablesize = SG_ALL,
158
159 .max_sectors = 0xFFFF,
Harish Zunjarrao7ad633c2011-05-17 23:17:11 -0700160 .shost_attrs = qla4xxx_host_attrs,
Vikas Chaudhary95d31262011-08-12 02:51:29 -0700161 .host_reset = qla4xxx_host_reset,
Vikas Chaudharya3559432011-07-25 13:48:51 -0500162 .vendor_id = SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_QLOGIC,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700163};
164
165static struct iscsi_transport qla4xxx_iscsi_transport = {
166 .owner = THIS_MODULE,
167 .name = DRIVER_NAME,
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500168 .caps = CAP_TEXT_NEGO |
169 CAP_DATA_PATH_OFFLOAD | CAP_HDRDGST |
170 CAP_DATADGST | CAP_LOGIN_OFFLOAD |
171 CAP_MULTI_R2T,
Mike Christie3128c6c2011-07-25 13:48:42 -0500172 .attr_is_visible = ql4_attr_is_visible,
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500173 .create_session = qla4xxx_session_create,
174 .destroy_session = qla4xxx_session_destroy,
175 .start_conn = qla4xxx_conn_start,
176 .create_conn = qla4xxx_conn_create,
177 .bind_conn = qla4xxx_conn_bind,
178 .stop_conn = iscsi_conn_stop,
179 .destroy_conn = qla4xxx_conn_destroy,
180 .set_param = iscsi_set_param,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700181 .get_conn_param = qla4xxx_conn_get_param,
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500182 .get_session_param = iscsi_session_get_param,
183 .get_ep_param = qla4xxx_get_ep_param,
184 .ep_connect = qla4xxx_ep_connect,
185 .ep_poll = qla4xxx_ep_poll,
186 .ep_disconnect = qla4xxx_ep_disconnect,
187 .get_stats = qla4xxx_conn_get_stats,
188 .send_pdu = iscsi_conn_send_pdu,
189 .xmit_task = qla4xxx_task_xmit,
190 .cleanup_task = qla4xxx_task_cleanup,
191 .alloc_pdu = qla4xxx_alloc_pdu,
192
Mike Christieaa1e93a2007-05-30 12:57:09 -0500193 .get_host_param = qla4xxx_host_get_param,
Mike Christied00efe32011-07-25 13:48:38 -0500194 .set_iface_param = qla4xxx_iface_set_param,
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500195 .get_iface_param = qla4xxx_get_iface_param,
Vikas Chaudharya3559432011-07-25 13:48:51 -0500196 .bsg_request = qla4xxx_bsg_request,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700197};
198
199static struct scsi_transport_template *qla4xxx_scsi_transport;
200
Al Viro587a1f12011-07-23 23:11:19 -0400201static umode_t ql4_attr_is_visible(int param_type, int param)
Mike Christie3128c6c2011-07-25 13:48:42 -0500202{
203 switch (param_type) {
Mike Christief27fb2e2011-07-25 13:48:45 -0500204 case ISCSI_HOST_PARAM:
205 switch (param) {
206 case ISCSI_HOST_PARAM_HWADDRESS:
207 case ISCSI_HOST_PARAM_IPADDRESS:
208 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Vikas Chaudhary3254dbe2012-01-19 03:06:56 -0800209 case ISCSI_HOST_PARAM_PORT_STATE:
210 case ISCSI_HOST_PARAM_PORT_SPEED:
Mike Christief27fb2e2011-07-25 13:48:45 -0500211 return S_IRUGO;
212 default:
213 return 0;
214 }
Mike Christie3128c6c2011-07-25 13:48:42 -0500215 case ISCSI_PARAM:
216 switch (param) {
Mike Christie590134f2011-10-17 22:42:13 -0500217 case ISCSI_PARAM_PERSISTENT_ADDRESS:
218 case ISCSI_PARAM_PERSISTENT_PORT:
Mike Christie3128c6c2011-07-25 13:48:42 -0500219 case ISCSI_PARAM_CONN_ADDRESS:
220 case ISCSI_PARAM_CONN_PORT:
Mike Christie1d063c12011-07-25 13:48:43 -0500221 case ISCSI_PARAM_TARGET_NAME:
222 case ISCSI_PARAM_TPGT:
223 case ISCSI_PARAM_TARGET_ALIAS:
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500224 case ISCSI_PARAM_MAX_BURST:
225 case ISCSI_PARAM_MAX_R2T:
226 case ISCSI_PARAM_FIRST_BURST:
227 case ISCSI_PARAM_MAX_RECV_DLENGTH:
228 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
Mike Christiede379202011-08-14 20:42:56 -0500229 case ISCSI_PARAM_IFACE_NAME:
Mike Christie3128c6c2011-07-25 13:48:42 -0500230 return S_IRUGO;
231 default:
232 return 0;
233 }
Mike Christieb78dbba2011-07-25 13:48:44 -0500234 case ISCSI_NET_PARAM:
235 switch (param) {
236 case ISCSI_NET_PARAM_IPV4_ADDR:
237 case ISCSI_NET_PARAM_IPV4_SUBNET:
238 case ISCSI_NET_PARAM_IPV4_GW:
239 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
240 case ISCSI_NET_PARAM_IFACE_ENABLE:
241 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
242 case ISCSI_NET_PARAM_IPV6_ADDR:
243 case ISCSI_NET_PARAM_IPV6_ROUTER:
244 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
245 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
Vikas Chaudhary6ac73e82011-07-25 13:48:49 -0500246 case ISCSI_NET_PARAM_VLAN_ID:
247 case ISCSI_NET_PARAM_VLAN_PRIORITY:
248 case ISCSI_NET_PARAM_VLAN_ENABLED:
Vikas Chaudhary943c1572011-08-01 03:26:13 -0700249 case ISCSI_NET_PARAM_MTU:
Vikas Chaudhary2ada7fc2011-08-01 03:26:19 -0700250 case ISCSI_NET_PARAM_PORT:
Mike Christieb78dbba2011-07-25 13:48:44 -0500251 return S_IRUGO;
252 default:
253 return 0;
254 }
Mike Christie3128c6c2011-07-25 13:48:42 -0500255 }
256
257 return 0;
258}
259
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500260static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
261 enum iscsi_param_type param_type,
262 int param, char *buf)
263{
264 struct Scsi_Host *shost = iscsi_iface_to_shost(iface);
265 struct scsi_qla_host *ha = to_qla_host(shost);
266 int len = -ENOSYS;
267
268 if (param_type != ISCSI_NET_PARAM)
269 return -ENOSYS;
270
271 switch (param) {
272 case ISCSI_NET_PARAM_IPV4_ADDR:
273 len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
274 break;
275 case ISCSI_NET_PARAM_IPV4_SUBNET:
276 len = sprintf(buf, "%pI4\n", &ha->ip_config.subnet_mask);
277 break;
278 case ISCSI_NET_PARAM_IPV4_GW:
279 len = sprintf(buf, "%pI4\n", &ha->ip_config.gateway);
280 break;
281 case ISCSI_NET_PARAM_IFACE_ENABLE:
282 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
283 len = sprintf(buf, "%s\n",
284 (ha->ip_config.ipv4_options &
285 IPOPT_IPV4_PROTOCOL_ENABLE) ?
286 "enabled" : "disabled");
287 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
288 len = sprintf(buf, "%s\n",
289 (ha->ip_config.ipv6_options &
290 IPV6_OPT_IPV6_PROTOCOL_ENABLE) ?
291 "enabled" : "disabled");
292 break;
293 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
294 len = sprintf(buf, "%s\n",
295 (ha->ip_config.tcp_options & TCPOPT_DHCP_ENABLE) ?
296 "dhcp" : "static");
297 break;
298 case ISCSI_NET_PARAM_IPV6_ADDR:
299 if (iface->iface_num == 0)
300 len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr0);
301 if (iface->iface_num == 1)
302 len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr1);
303 break;
304 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
305 len = sprintf(buf, "%pI6\n",
306 &ha->ip_config.ipv6_link_local_addr);
307 break;
308 case ISCSI_NET_PARAM_IPV6_ROUTER:
309 len = sprintf(buf, "%pI6\n",
310 &ha->ip_config.ipv6_default_router_addr);
311 break;
312 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
313 len = sprintf(buf, "%s\n",
314 (ha->ip_config.ipv6_addl_options &
315 IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) ?
316 "nd" : "static");
317 break;
318 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
319 len = sprintf(buf, "%s\n",
320 (ha->ip_config.ipv6_addl_options &
321 IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR) ?
322 "auto" : "static");
323 break;
Vikas Chaudhary6ac73e82011-07-25 13:48:49 -0500324 case ISCSI_NET_PARAM_VLAN_ID:
325 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
326 len = sprintf(buf, "%d\n",
327 (ha->ip_config.ipv4_vlan_tag &
328 ISCSI_MAX_VLAN_ID));
329 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
330 len = sprintf(buf, "%d\n",
331 (ha->ip_config.ipv6_vlan_tag &
332 ISCSI_MAX_VLAN_ID));
333 break;
334 case ISCSI_NET_PARAM_VLAN_PRIORITY:
335 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
336 len = sprintf(buf, "%d\n",
337 ((ha->ip_config.ipv4_vlan_tag >> 13) &
338 ISCSI_MAX_VLAN_PRIORITY));
339 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
340 len = sprintf(buf, "%d\n",
341 ((ha->ip_config.ipv6_vlan_tag >> 13) &
342 ISCSI_MAX_VLAN_PRIORITY));
343 break;
344 case ISCSI_NET_PARAM_VLAN_ENABLED:
345 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
346 len = sprintf(buf, "%s\n",
347 (ha->ip_config.ipv4_options &
348 IPOPT_VLAN_TAGGING_ENABLE) ?
349 "enabled" : "disabled");
350 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
351 len = sprintf(buf, "%s\n",
352 (ha->ip_config.ipv6_options &
353 IPV6_OPT_VLAN_TAGGING_ENABLE) ?
354 "enabled" : "disabled");
355 break;
Vikas Chaudhary943c1572011-08-01 03:26:13 -0700356 case ISCSI_NET_PARAM_MTU:
357 len = sprintf(buf, "%d\n", ha->ip_config.eth_mtu_size);
358 break;
Vikas Chaudhary2ada7fc2011-08-01 03:26:19 -0700359 case ISCSI_NET_PARAM_PORT:
360 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
361 len = sprintf(buf, "%d\n", ha->ip_config.ipv4_port);
362 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
363 len = sprintf(buf, "%d\n", ha->ip_config.ipv6_port);
364 break;
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500365 default:
366 len = -ENOSYS;
367 }
368
369 return len;
370}
371
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500372static struct iscsi_endpoint *
373qla4xxx_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
374 int non_blocking)
375{
376 int ret;
377 struct iscsi_endpoint *ep;
378 struct qla_endpoint *qla_ep;
379 struct scsi_qla_host *ha;
380 struct sockaddr_in *addr;
381 struct sockaddr_in6 *addr6;
382
383 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
384 if (!shost) {
385 ret = -ENXIO;
386 printk(KERN_ERR "%s: shost is NULL\n",
387 __func__);
388 return ERR_PTR(ret);
389 }
390
391 ha = iscsi_host_priv(shost);
392
393 ep = iscsi_create_endpoint(sizeof(struct qla_endpoint));
394 if (!ep) {
395 ret = -ENOMEM;
396 return ERR_PTR(ret);
397 }
398
399 qla_ep = ep->dd_data;
400 memset(qla_ep, 0, sizeof(struct qla_endpoint));
401 if (dst_addr->sa_family == AF_INET) {
402 memcpy(&qla_ep->dst_addr, dst_addr, sizeof(struct sockaddr_in));
403 addr = (struct sockaddr_in *)&qla_ep->dst_addr;
404 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI4\n", __func__,
405 (char *)&addr->sin_addr));
406 } else if (dst_addr->sa_family == AF_INET6) {
407 memcpy(&qla_ep->dst_addr, dst_addr,
408 sizeof(struct sockaddr_in6));
409 addr6 = (struct sockaddr_in6 *)&qla_ep->dst_addr;
410 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI6\n", __func__,
411 (char *)&addr6->sin6_addr));
412 }
413
414 qla_ep->host = shost;
415
416 return ep;
417}
418
419static int qla4xxx_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
420{
421 struct qla_endpoint *qla_ep;
422 struct scsi_qla_host *ha;
423 int ret = 0;
424
425 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
426 qla_ep = ep->dd_data;
427 ha = to_qla_host(qla_ep->host);
428
Mike Christie13483732011-12-01 21:38:41 -0600429 if (adapter_up(ha) && !test_bit(AF_BUILD_DDB_LIST, &ha->flags))
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500430 ret = 1;
431
432 return ret;
433}
434
435static void qla4xxx_ep_disconnect(struct iscsi_endpoint *ep)
436{
437 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
438 iscsi_destroy_endpoint(ep);
439}
440
441static int qla4xxx_get_ep_param(struct iscsi_endpoint *ep,
442 enum iscsi_param param,
443 char *buf)
444{
445 struct qla_endpoint *qla_ep = ep->dd_data;
446 struct sockaddr *dst_addr;
447
448 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
449
450 switch (param) {
451 case ISCSI_PARAM_CONN_PORT:
452 case ISCSI_PARAM_CONN_ADDRESS:
453 if (!qla_ep)
454 return -ENOTCONN;
455
456 dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
457 if (!dst_addr)
458 return -ENOTCONN;
459
460 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
461 &qla_ep->dst_addr, param, buf);
462 default:
463 return -ENOSYS;
464 }
465}
466
467static void qla4xxx_conn_get_stats(struct iscsi_cls_conn *cls_conn,
468 struct iscsi_stats *stats)
469{
470 struct iscsi_session *sess;
471 struct iscsi_cls_session *cls_sess;
472 struct ddb_entry *ddb_entry;
473 struct scsi_qla_host *ha;
474 struct ql_iscsi_stats *ql_iscsi_stats;
475 int stats_size;
476 int ret;
477 dma_addr_t iscsi_stats_dma;
478
479 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
480
481 cls_sess = iscsi_conn_to_session(cls_conn);
482 sess = cls_sess->dd_data;
483 ddb_entry = sess->dd_data;
484 ha = ddb_entry->ha;
485
486 stats_size = PAGE_ALIGN(sizeof(struct ql_iscsi_stats));
487 /* Allocate memory */
488 ql_iscsi_stats = dma_alloc_coherent(&ha->pdev->dev, stats_size,
489 &iscsi_stats_dma, GFP_KERNEL);
490 if (!ql_iscsi_stats) {
491 ql4_printk(KERN_ERR, ha,
492 "Unable to allocate memory for iscsi stats\n");
493 goto exit_get_stats;
494 }
495
496 ret = qla4xxx_get_mgmt_data(ha, ddb_entry->fw_ddb_index, stats_size,
497 iscsi_stats_dma);
498 if (ret != QLA_SUCCESS) {
499 ql4_printk(KERN_ERR, ha,
500 "Unable to retreive iscsi stats\n");
501 goto free_stats;
502 }
503
504 /* octets */
505 stats->txdata_octets = le64_to_cpu(ql_iscsi_stats->tx_data_octets);
506 stats->rxdata_octets = le64_to_cpu(ql_iscsi_stats->rx_data_octets);
507 /* xmit pdus */
508 stats->noptx_pdus = le32_to_cpu(ql_iscsi_stats->tx_nopout_pdus);
509 stats->scsicmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_cmd_pdus);
510 stats->tmfcmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_tmf_cmd_pdus);
511 stats->login_pdus = le32_to_cpu(ql_iscsi_stats->tx_login_cmd_pdus);
512 stats->text_pdus = le32_to_cpu(ql_iscsi_stats->tx_text_cmd_pdus);
513 stats->dataout_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_write_pdus);
514 stats->logout_pdus = le32_to_cpu(ql_iscsi_stats->tx_logout_cmd_pdus);
515 stats->snack_pdus = le32_to_cpu(ql_iscsi_stats->tx_snack_req_pdus);
516 /* recv pdus */
517 stats->noprx_pdus = le32_to_cpu(ql_iscsi_stats->rx_nopin_pdus);
518 stats->scsirsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_resp_pdus);
519 stats->tmfrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_tmf_resp_pdus);
520 stats->textrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_text_resp_pdus);
521 stats->datain_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_read_pdus);
522 stats->logoutrsp_pdus =
523 le32_to_cpu(ql_iscsi_stats->rx_logout_resp_pdus);
524 stats->r2t_pdus = le32_to_cpu(ql_iscsi_stats->rx_r2t_pdus);
525 stats->async_pdus = le32_to_cpu(ql_iscsi_stats->rx_async_pdus);
526 stats->rjt_pdus = le32_to_cpu(ql_iscsi_stats->rx_reject_pdus);
527
528free_stats:
529 dma_free_coherent(&ha->pdev->dev, stats_size, ql_iscsi_stats,
530 iscsi_stats_dma);
531exit_get_stats:
532 return;
533}
534
Mike Christie5c656af2009-07-15 15:02:59 -0500535static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
536{
537 struct iscsi_cls_session *session;
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500538 struct iscsi_session *sess;
539 unsigned long flags;
540 enum blk_eh_timer_return ret = BLK_EH_NOT_HANDLED;
Mike Christie5c656af2009-07-15 15:02:59 -0500541
542 session = starget_to_session(scsi_target(sc->device));
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500543 sess = session->dd_data;
Mike Christie5c656af2009-07-15 15:02:59 -0500544
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500545 spin_lock_irqsave(&session->lock, flags);
546 if (session->state == ISCSI_SESSION_FAILED)
547 ret = BLK_EH_RESET_TIMER;
548 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie5c656af2009-07-15 15:02:59 -0500549
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500550 return ret;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700551}
552
Vikas Chaudhary3254dbe2012-01-19 03:06:56 -0800553static void qla4xxx_set_port_speed(struct Scsi_Host *shost)
554{
555 struct scsi_qla_host *ha = to_qla_host(shost);
556 struct iscsi_cls_host *ihost = shost_priv(shost);
557 uint32_t speed = ISCSI_PORT_SPEED_UNKNOWN;
558
559 qla4xxx_get_firmware_state(ha);
560
561 switch (ha->addl_fw_state & 0x0F00) {
562 case FW_ADDSTATE_LINK_SPEED_10MBPS:
563 speed = ISCSI_PORT_SPEED_10MBPS;
564 break;
565 case FW_ADDSTATE_LINK_SPEED_100MBPS:
566 speed = ISCSI_PORT_SPEED_100MBPS;
567 break;
568 case FW_ADDSTATE_LINK_SPEED_1GBPS:
569 speed = ISCSI_PORT_SPEED_1GBPS;
570 break;
571 case FW_ADDSTATE_LINK_SPEED_10GBPS:
572 speed = ISCSI_PORT_SPEED_10GBPS;
573 break;
574 }
575 ihost->port_speed = speed;
576}
577
578static void qla4xxx_set_port_state(struct Scsi_Host *shost)
579{
580 struct scsi_qla_host *ha = to_qla_host(shost);
581 struct iscsi_cls_host *ihost = shost_priv(shost);
582 uint32_t state = ISCSI_PORT_STATE_DOWN;
583
584 if (test_bit(AF_LINK_UP, &ha->flags))
585 state = ISCSI_PORT_STATE_UP;
586
587 ihost->port_state = state;
588}
589
Mike Christieaa1e93a2007-05-30 12:57:09 -0500590static int qla4xxx_host_get_param(struct Scsi_Host *shost,
591 enum iscsi_host_param param, char *buf)
592{
593 struct scsi_qla_host *ha = to_qla_host(shost);
594 int len;
595
596 switch (param) {
597 case ISCSI_HOST_PARAM_HWADDRESS:
Michael Chan7ffc49a2007-12-24 21:28:09 -0800598 len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
Mike Christieaa1e93a2007-05-30 12:57:09 -0500599 break;
Mike Christie22236962007-05-30 12:57:24 -0500600 case ISCSI_HOST_PARAM_IPADDRESS:
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500601 len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
Mike Christie22236962007-05-30 12:57:24 -0500602 break;
Mike Christie8ad57812007-05-30 12:57:13 -0500603 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie22236962007-05-30 12:57:24 -0500604 len = sprintf(buf, "%s\n", ha->name_string);
Mike Christie8ad57812007-05-30 12:57:13 -0500605 break;
Vikas Chaudhary3254dbe2012-01-19 03:06:56 -0800606 case ISCSI_HOST_PARAM_PORT_STATE:
607 qla4xxx_set_port_state(shost);
608 len = sprintf(buf, "%s\n", iscsi_get_port_state_name(shost));
609 break;
610 case ISCSI_HOST_PARAM_PORT_SPEED:
611 qla4xxx_set_port_speed(shost);
612 len = sprintf(buf, "%s\n", iscsi_get_port_speed_name(shost));
613 break;
Mike Christieaa1e93a2007-05-30 12:57:09 -0500614 default:
615 return -ENOSYS;
616 }
617
618 return len;
619}
620
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500621static void qla4xxx_create_ipv4_iface(struct scsi_qla_host *ha)
622{
623 if (ha->iface_ipv4)
624 return;
625
626 /* IPv4 */
627 ha->iface_ipv4 = iscsi_create_iface(ha->host,
628 &qla4xxx_iscsi_transport,
629 ISCSI_IFACE_TYPE_IPV4, 0, 0);
630 if (!ha->iface_ipv4)
631 ql4_printk(KERN_ERR, ha, "Could not create IPv4 iSCSI "
632 "iface0.\n");
633}
634
635static void qla4xxx_create_ipv6_iface(struct scsi_qla_host *ha)
636{
637 if (!ha->iface_ipv6_0)
638 /* IPv6 iface-0 */
639 ha->iface_ipv6_0 = iscsi_create_iface(ha->host,
640 &qla4xxx_iscsi_transport,
641 ISCSI_IFACE_TYPE_IPV6, 0,
642 0);
643 if (!ha->iface_ipv6_0)
644 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
645 "iface0.\n");
646
647 if (!ha->iface_ipv6_1)
648 /* IPv6 iface-1 */
649 ha->iface_ipv6_1 = iscsi_create_iface(ha->host,
650 &qla4xxx_iscsi_transport,
651 ISCSI_IFACE_TYPE_IPV6, 1,
652 0);
653 if (!ha->iface_ipv6_1)
654 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
655 "iface1.\n");
656}
657
658static void qla4xxx_create_ifaces(struct scsi_qla_host *ha)
659{
660 if (ha->ip_config.ipv4_options & IPOPT_IPV4_PROTOCOL_ENABLE)
661 qla4xxx_create_ipv4_iface(ha);
662
663 if (ha->ip_config.ipv6_options & IPV6_OPT_IPV6_PROTOCOL_ENABLE)
664 qla4xxx_create_ipv6_iface(ha);
665}
666
667static void qla4xxx_destroy_ipv4_iface(struct scsi_qla_host *ha)
668{
669 if (ha->iface_ipv4) {
670 iscsi_destroy_iface(ha->iface_ipv4);
671 ha->iface_ipv4 = NULL;
672 }
673}
674
675static void qla4xxx_destroy_ipv6_iface(struct scsi_qla_host *ha)
676{
677 if (ha->iface_ipv6_0) {
678 iscsi_destroy_iface(ha->iface_ipv6_0);
679 ha->iface_ipv6_0 = NULL;
680 }
681 if (ha->iface_ipv6_1) {
682 iscsi_destroy_iface(ha->iface_ipv6_1);
683 ha->iface_ipv6_1 = NULL;
684 }
685}
686
687static void qla4xxx_destroy_ifaces(struct scsi_qla_host *ha)
688{
689 qla4xxx_destroy_ipv4_iface(ha);
690 qla4xxx_destroy_ipv6_iface(ha);
691}
692
Mike Christied00efe32011-07-25 13:48:38 -0500693static void qla4xxx_set_ipv6(struct scsi_qla_host *ha,
694 struct iscsi_iface_param_info *iface_param,
695 struct addr_ctrl_blk *init_fw_cb)
696{
697 /*
698 * iface_num 0 is valid for IPv6 Addr, linklocal, router, autocfg.
699 * iface_num 1 is valid only for IPv6 Addr.
700 */
701 switch (iface_param->param) {
702 case ISCSI_NET_PARAM_IPV6_ADDR:
703 if (iface_param->iface_num & 0x1)
704 /* IPv6 Addr 1 */
705 memcpy(init_fw_cb->ipv6_addr1, iface_param->value,
706 sizeof(init_fw_cb->ipv6_addr1));
707 else
708 /* IPv6 Addr 0 */
709 memcpy(init_fw_cb->ipv6_addr0, iface_param->value,
710 sizeof(init_fw_cb->ipv6_addr0));
711 break;
712 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
713 if (iface_param->iface_num & 0x1)
714 break;
715 memcpy(init_fw_cb->ipv6_if_id, &iface_param->value[8],
716 sizeof(init_fw_cb->ipv6_if_id));
717 break;
718 case ISCSI_NET_PARAM_IPV6_ROUTER:
719 if (iface_param->iface_num & 0x1)
720 break;
721 memcpy(init_fw_cb->ipv6_dflt_rtr_addr, iface_param->value,
722 sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
723 break;
724 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
725 /* Autocfg applies to even interface */
726 if (iface_param->iface_num & 0x1)
727 break;
728
729 if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_DISABLE)
730 init_fw_cb->ipv6_addtl_opts &=
731 cpu_to_le16(
732 ~IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
733 else if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_ND_ENABLE)
734 init_fw_cb->ipv6_addtl_opts |=
735 cpu_to_le16(
736 IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
737 else
738 ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
739 "IPv6 addr\n");
740 break;
741 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
742 /* Autocfg applies to even interface */
743 if (iface_param->iface_num & 0x1)
744 break;
745
746 if (iface_param->value[0] ==
747 ISCSI_IPV6_LINKLOCAL_AUTOCFG_ENABLE)
748 init_fw_cb->ipv6_addtl_opts |= cpu_to_le16(
749 IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
750 else if (iface_param->value[0] ==
751 ISCSI_IPV6_LINKLOCAL_AUTOCFG_DISABLE)
752 init_fw_cb->ipv6_addtl_opts &= cpu_to_le16(
753 ~IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
754 else
755 ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
756 "IPv6 linklocal addr\n");
757 break;
758 case ISCSI_NET_PARAM_IPV6_ROUTER_AUTOCFG:
759 /* Autocfg applies to even interface */
760 if (iface_param->iface_num & 0x1)
761 break;
762
763 if (iface_param->value[0] == ISCSI_IPV6_ROUTER_AUTOCFG_ENABLE)
764 memset(init_fw_cb->ipv6_dflt_rtr_addr, 0,
765 sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
766 break;
767 case ISCSI_NET_PARAM_IFACE_ENABLE:
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500768 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
Mike Christied00efe32011-07-25 13:48:38 -0500769 init_fw_cb->ipv6_opts |=
770 cpu_to_le16(IPV6_OPT_IPV6_PROTOCOL_ENABLE);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500771 qla4xxx_create_ipv6_iface(ha);
772 } else {
Mike Christied00efe32011-07-25 13:48:38 -0500773 init_fw_cb->ipv6_opts &=
774 cpu_to_le16(~IPV6_OPT_IPV6_PROTOCOL_ENABLE &
775 0xFFFF);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500776 qla4xxx_destroy_ipv6_iface(ha);
777 }
Mike Christied00efe32011-07-25 13:48:38 -0500778 break;
Mike Christie2d636732011-10-11 17:55:11 -0500779 case ISCSI_NET_PARAM_VLAN_TAG:
Mike Christied00efe32011-07-25 13:48:38 -0500780 if (iface_param->len != sizeof(init_fw_cb->ipv6_vlan_tag))
781 break;
Vikas Chaudhary6ac73e82011-07-25 13:48:49 -0500782 init_fw_cb->ipv6_vlan_tag =
783 cpu_to_be16(*(uint16_t *)iface_param->value);
784 break;
785 case ISCSI_NET_PARAM_VLAN_ENABLED:
786 if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
787 init_fw_cb->ipv6_opts |=
788 cpu_to_le16(IPV6_OPT_VLAN_TAGGING_ENABLE);
789 else
790 init_fw_cb->ipv6_opts &=
791 cpu_to_le16(~IPV6_OPT_VLAN_TAGGING_ENABLE);
Mike Christied00efe32011-07-25 13:48:38 -0500792 break;
Vikas Chaudhary943c1572011-08-01 03:26:13 -0700793 case ISCSI_NET_PARAM_MTU:
794 init_fw_cb->eth_mtu_size =
795 cpu_to_le16(*(uint16_t *)iface_param->value);
796 break;
Vikas Chaudhary2ada7fc2011-08-01 03:26:19 -0700797 case ISCSI_NET_PARAM_PORT:
798 /* Autocfg applies to even interface */
799 if (iface_param->iface_num & 0x1)
800 break;
801
802 init_fw_cb->ipv6_port =
803 cpu_to_le16(*(uint16_t *)iface_param->value);
804 break;
Mike Christied00efe32011-07-25 13:48:38 -0500805 default:
806 ql4_printk(KERN_ERR, ha, "Unknown IPv6 param = %d\n",
807 iface_param->param);
808 break;
809 }
810}
811
812static void qla4xxx_set_ipv4(struct scsi_qla_host *ha,
813 struct iscsi_iface_param_info *iface_param,
814 struct addr_ctrl_blk *init_fw_cb)
815{
816 switch (iface_param->param) {
817 case ISCSI_NET_PARAM_IPV4_ADDR:
818 memcpy(init_fw_cb->ipv4_addr, iface_param->value,
819 sizeof(init_fw_cb->ipv4_addr));
820 break;
821 case ISCSI_NET_PARAM_IPV4_SUBNET:
822 memcpy(init_fw_cb->ipv4_subnet, iface_param->value,
823 sizeof(init_fw_cb->ipv4_subnet));
824 break;
825 case ISCSI_NET_PARAM_IPV4_GW:
826 memcpy(init_fw_cb->ipv4_gw_addr, iface_param->value,
827 sizeof(init_fw_cb->ipv4_gw_addr));
828 break;
829 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
830 if (iface_param->value[0] == ISCSI_BOOTPROTO_DHCP)
831 init_fw_cb->ipv4_tcp_opts |=
832 cpu_to_le16(TCPOPT_DHCP_ENABLE);
833 else if (iface_param->value[0] == ISCSI_BOOTPROTO_STATIC)
834 init_fw_cb->ipv4_tcp_opts &=
835 cpu_to_le16(~TCPOPT_DHCP_ENABLE);
836 else
837 ql4_printk(KERN_ERR, ha, "Invalid IPv4 bootproto\n");
838 break;
839 case ISCSI_NET_PARAM_IFACE_ENABLE:
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500840 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
Mike Christied00efe32011-07-25 13:48:38 -0500841 init_fw_cb->ipv4_ip_opts |=
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500842 cpu_to_le16(IPOPT_IPV4_PROTOCOL_ENABLE);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500843 qla4xxx_create_ipv4_iface(ha);
844 } else {
Mike Christied00efe32011-07-25 13:48:38 -0500845 init_fw_cb->ipv4_ip_opts &=
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500846 cpu_to_le16(~IPOPT_IPV4_PROTOCOL_ENABLE &
Mike Christied00efe32011-07-25 13:48:38 -0500847 0xFFFF);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500848 qla4xxx_destroy_ipv4_iface(ha);
849 }
Mike Christied00efe32011-07-25 13:48:38 -0500850 break;
Mike Christie2d636732011-10-11 17:55:11 -0500851 case ISCSI_NET_PARAM_VLAN_TAG:
Mike Christied00efe32011-07-25 13:48:38 -0500852 if (iface_param->len != sizeof(init_fw_cb->ipv4_vlan_tag))
853 break;
Vikas Chaudhary6ac73e82011-07-25 13:48:49 -0500854 init_fw_cb->ipv4_vlan_tag =
855 cpu_to_be16(*(uint16_t *)iface_param->value);
856 break;
857 case ISCSI_NET_PARAM_VLAN_ENABLED:
858 if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
859 init_fw_cb->ipv4_ip_opts |=
860 cpu_to_le16(IPOPT_VLAN_TAGGING_ENABLE);
861 else
862 init_fw_cb->ipv4_ip_opts &=
863 cpu_to_le16(~IPOPT_VLAN_TAGGING_ENABLE);
Mike Christied00efe32011-07-25 13:48:38 -0500864 break;
Vikas Chaudhary943c1572011-08-01 03:26:13 -0700865 case ISCSI_NET_PARAM_MTU:
866 init_fw_cb->eth_mtu_size =
867 cpu_to_le16(*(uint16_t *)iface_param->value);
868 break;
Vikas Chaudhary2ada7fc2011-08-01 03:26:19 -0700869 case ISCSI_NET_PARAM_PORT:
870 init_fw_cb->ipv4_port =
871 cpu_to_le16(*(uint16_t *)iface_param->value);
872 break;
Mike Christied00efe32011-07-25 13:48:38 -0500873 default:
874 ql4_printk(KERN_ERR, ha, "Unknown IPv4 param = %d\n",
875 iface_param->param);
876 break;
877 }
878}
879
880static void
881qla4xxx_initcb_to_acb(struct addr_ctrl_blk *init_fw_cb)
882{
883 struct addr_ctrl_blk_def *acb;
884 acb = (struct addr_ctrl_blk_def *)init_fw_cb;
885 memset(acb->reserved1, 0, sizeof(acb->reserved1));
886 memset(acb->reserved2, 0, sizeof(acb->reserved2));
887 memset(acb->reserved3, 0, sizeof(acb->reserved3));
888 memset(acb->reserved4, 0, sizeof(acb->reserved4));
889 memset(acb->reserved5, 0, sizeof(acb->reserved5));
890 memset(acb->reserved6, 0, sizeof(acb->reserved6));
891 memset(acb->reserved7, 0, sizeof(acb->reserved7));
892 memset(acb->reserved8, 0, sizeof(acb->reserved8));
893 memset(acb->reserved9, 0, sizeof(acb->reserved9));
894 memset(acb->reserved10, 0, sizeof(acb->reserved10));
895 memset(acb->reserved11, 0, sizeof(acb->reserved11));
896 memset(acb->reserved12, 0, sizeof(acb->reserved12));
897 memset(acb->reserved13, 0, sizeof(acb->reserved13));
898 memset(acb->reserved14, 0, sizeof(acb->reserved14));
899 memset(acb->reserved15, 0, sizeof(acb->reserved15));
900}
901
902static int
Mike Christie00c31882011-10-06 03:56:59 -0500903qla4xxx_iface_set_param(struct Scsi_Host *shost, void *data, uint32_t len)
Mike Christied00efe32011-07-25 13:48:38 -0500904{
905 struct scsi_qla_host *ha = to_qla_host(shost);
906 int rval = 0;
907 struct iscsi_iface_param_info *iface_param = NULL;
908 struct addr_ctrl_blk *init_fw_cb = NULL;
909 dma_addr_t init_fw_cb_dma;
910 uint32_t mbox_cmd[MBOX_REG_COUNT];
911 uint32_t mbox_sts[MBOX_REG_COUNT];
Mike Christie00c31882011-10-06 03:56:59 -0500912 uint32_t rem = len;
913 struct nlattr *attr;
Mike Christied00efe32011-07-25 13:48:38 -0500914
915 init_fw_cb = dma_alloc_coherent(&ha->pdev->dev,
916 sizeof(struct addr_ctrl_blk),
917 &init_fw_cb_dma, GFP_KERNEL);
918 if (!init_fw_cb) {
919 ql4_printk(KERN_ERR, ha, "%s: Unable to alloc init_cb\n",
920 __func__);
921 return -ENOMEM;
922 }
923
924 memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
925 memset(&mbox_cmd, 0, sizeof(mbox_cmd));
926 memset(&mbox_sts, 0, sizeof(mbox_sts));
927
928 if (qla4xxx_get_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma)) {
929 ql4_printk(KERN_ERR, ha, "%s: get ifcb failed\n", __func__);
930 rval = -EIO;
931 goto exit_init_fw_cb;
932 }
933
Mike Christie00c31882011-10-06 03:56:59 -0500934 nla_for_each_attr(attr, data, len, rem) {
935 iface_param = nla_data(attr);
Mike Christied00efe32011-07-25 13:48:38 -0500936
937 if (iface_param->param_type != ISCSI_NET_PARAM)
938 continue;
939
940 switch (iface_param->iface_type) {
941 case ISCSI_IFACE_TYPE_IPV4:
942 switch (iface_param->iface_num) {
943 case 0:
944 qla4xxx_set_ipv4(ha, iface_param, init_fw_cb);
945 break;
946 default:
947 /* Cannot have more than one IPv4 interface */
948 ql4_printk(KERN_ERR, ha, "Invalid IPv4 iface "
949 "number = %d\n",
950 iface_param->iface_num);
951 break;
952 }
953 break;
954 case ISCSI_IFACE_TYPE_IPV6:
955 switch (iface_param->iface_num) {
956 case 0:
957 case 1:
958 qla4xxx_set_ipv6(ha, iface_param, init_fw_cb);
959 break;
960 default:
961 /* Cannot have more than two IPv6 interface */
962 ql4_printk(KERN_ERR, ha, "Invalid IPv6 iface "
963 "number = %d\n",
964 iface_param->iface_num);
965 break;
966 }
967 break;
968 default:
969 ql4_printk(KERN_ERR, ha, "Invalid iface type\n");
970 break;
971 }
Mike Christied00efe32011-07-25 13:48:38 -0500972 }
973
974 init_fw_cb->cookie = cpu_to_le32(0x11BEAD5A);
975
976 rval = qla4xxx_set_flash(ha, init_fw_cb_dma, FLASH_SEGMENT_IFCB,
977 sizeof(struct addr_ctrl_blk),
978 FLASH_OPT_RMW_COMMIT);
979 if (rval != QLA_SUCCESS) {
980 ql4_printk(KERN_ERR, ha, "%s: set flash mbx failed\n",
981 __func__);
982 rval = -EIO;
983 goto exit_init_fw_cb;
984 }
985
Vikas Chaudharyce505f92011-12-01 22:42:10 -0800986 rval = qla4xxx_disable_acb(ha);
987 if (rval != QLA_SUCCESS) {
988 ql4_printk(KERN_ERR, ha, "%s: disable acb mbx failed\n",
989 __func__);
990 rval = -EIO;
991 goto exit_init_fw_cb;
992 }
993
994 wait_for_completion_timeout(&ha->disable_acb_comp,
995 DISABLE_ACB_TOV * HZ);
Mike Christied00efe32011-07-25 13:48:38 -0500996
997 qla4xxx_initcb_to_acb(init_fw_cb);
998
999 rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma);
1000 if (rval != QLA_SUCCESS) {
1001 ql4_printk(KERN_ERR, ha, "%s: set acb mbx failed\n",
1002 __func__);
1003 rval = -EIO;
1004 goto exit_init_fw_cb;
1005 }
1006
1007 memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
1008 qla4xxx_update_local_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb,
1009 init_fw_cb_dma);
1010
1011exit_init_fw_cb:
1012 dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk),
1013 init_fw_cb, init_fw_cb_dma);
1014
1015 return rval;
1016}
1017
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001018static int qla4xxx_conn_get_param(struct iscsi_cls_conn *cls_conn,
David Somayajuluafaf5a22006-09-19 10:28:00 -07001019 enum iscsi_param param, char *buf)
1020{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001021 struct iscsi_conn *conn;
1022 struct qla_conn *qla_conn;
1023 struct sockaddr *dst_addr;
1024 int len = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001025
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001026 conn = cls_conn->dd_data;
1027 qla_conn = conn->dd_data;
1028 dst_addr = &qla_conn->qla_ep->dst_addr;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001029
1030 switch (param) {
1031 case ISCSI_PARAM_CONN_PORT:
David Somayajuluafaf5a22006-09-19 10:28:00 -07001032 case ISCSI_PARAM_CONN_ADDRESS:
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001033 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
1034 dst_addr, param, buf);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001035 default:
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001036 return iscsi_conn_get_param(cls_conn, param, buf);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001037 }
1038
1039 return len;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001040
David Somayajuluafaf5a22006-09-19 10:28:00 -07001041}
1042
Mike Christie13483732011-12-01 21:38:41 -06001043int qla4xxx_get_ddb_index(struct scsi_qla_host *ha, uint16_t *ddb_index)
1044{
1045 uint32_t mbx_sts = 0;
1046 uint16_t tmp_ddb_index;
1047 int ret;
1048
1049get_ddb_index:
1050 tmp_ddb_index = find_first_zero_bit(ha->ddb_idx_map, MAX_DDB_ENTRIES);
1051
1052 if (tmp_ddb_index >= MAX_DDB_ENTRIES) {
1053 DEBUG2(ql4_printk(KERN_INFO, ha,
1054 "Free DDB index not available\n"));
1055 ret = QLA_ERROR;
1056 goto exit_get_ddb_index;
1057 }
1058
1059 if (test_and_set_bit(tmp_ddb_index, ha->ddb_idx_map))
1060 goto get_ddb_index;
1061
1062 DEBUG2(ql4_printk(KERN_INFO, ha,
1063 "Found a free DDB index at %d\n", tmp_ddb_index));
1064 ret = qla4xxx_req_ddb_entry(ha, tmp_ddb_index, &mbx_sts);
1065 if (ret == QLA_ERROR) {
1066 if (mbx_sts == MBOX_STS_COMMAND_ERROR) {
1067 ql4_printk(KERN_INFO, ha,
1068 "DDB index = %d not available trying next\n",
1069 tmp_ddb_index);
1070 goto get_ddb_index;
1071 }
1072 DEBUG2(ql4_printk(KERN_INFO, ha,
1073 "Free FW DDB not available\n"));
1074 }
1075
1076 *ddb_index = tmp_ddb_index;
1077
1078exit_get_ddb_index:
1079 return ret;
1080}
1081
1082static int qla4xxx_match_ipaddress(struct scsi_qla_host *ha,
1083 struct ddb_entry *ddb_entry,
1084 char *existing_ipaddr,
1085 char *user_ipaddr)
1086{
1087 uint8_t dst_ipaddr[IPv6_ADDR_LEN];
1088 char formatted_ipaddr[DDB_IPADDR_LEN];
1089 int status = QLA_SUCCESS, ret = 0;
1090
1091 if (ddb_entry->fw_ddb_entry.options & DDB_OPT_IPV6_DEVICE) {
1092 ret = in6_pton(user_ipaddr, strlen(user_ipaddr), dst_ipaddr,
1093 '\0', NULL);
1094 if (ret == 0) {
1095 status = QLA_ERROR;
1096 goto out_match;
1097 }
1098 ret = sprintf(formatted_ipaddr, "%pI6", dst_ipaddr);
1099 } else {
1100 ret = in4_pton(user_ipaddr, strlen(user_ipaddr), dst_ipaddr,
1101 '\0', NULL);
1102 if (ret == 0) {
1103 status = QLA_ERROR;
1104 goto out_match;
1105 }
1106 ret = sprintf(formatted_ipaddr, "%pI4", dst_ipaddr);
1107 }
1108
1109 if (strcmp(existing_ipaddr, formatted_ipaddr))
1110 status = QLA_ERROR;
1111
1112out_match:
1113 return status;
1114}
1115
1116static int qla4xxx_match_fwdb_session(struct scsi_qla_host *ha,
1117 struct iscsi_cls_conn *cls_conn)
1118{
1119 int idx = 0, max_ddbs, rval;
1120 struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
1121 struct iscsi_session *sess, *existing_sess;
1122 struct iscsi_conn *conn, *existing_conn;
1123 struct ddb_entry *ddb_entry;
1124
1125 sess = cls_sess->dd_data;
1126 conn = cls_conn->dd_data;
1127
1128 if (sess->targetname == NULL ||
1129 conn->persistent_address == NULL ||
1130 conn->persistent_port == 0)
1131 return QLA_ERROR;
1132
1133 max_ddbs = is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
1134 MAX_DEV_DB_ENTRIES;
1135
1136 for (idx = 0; idx < max_ddbs; idx++) {
1137 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
1138 if (ddb_entry == NULL)
1139 continue;
1140
1141 if (ddb_entry->ddb_type != FLASH_DDB)
1142 continue;
1143
1144 existing_sess = ddb_entry->sess->dd_data;
1145 existing_conn = ddb_entry->conn->dd_data;
1146
1147 if (existing_sess->targetname == NULL ||
1148 existing_conn->persistent_address == NULL ||
1149 existing_conn->persistent_port == 0)
1150 continue;
1151
1152 DEBUG2(ql4_printk(KERN_INFO, ha,
1153 "IQN = %s User IQN = %s\n",
1154 existing_sess->targetname,
1155 sess->targetname));
1156
1157 DEBUG2(ql4_printk(KERN_INFO, ha,
1158 "IP = %s User IP = %s\n",
1159 existing_conn->persistent_address,
1160 conn->persistent_address));
1161
1162 DEBUG2(ql4_printk(KERN_INFO, ha,
1163 "Port = %d User Port = %d\n",
1164 existing_conn->persistent_port,
1165 conn->persistent_port));
1166
1167 if (strcmp(existing_sess->targetname, sess->targetname))
1168 continue;
1169 rval = qla4xxx_match_ipaddress(ha, ddb_entry,
1170 existing_conn->persistent_address,
1171 conn->persistent_address);
1172 if (rval == QLA_ERROR)
1173 continue;
1174 if (existing_conn->persistent_port != conn->persistent_port)
1175 continue;
1176 break;
1177 }
1178
1179 if (idx == max_ddbs)
1180 return QLA_ERROR;
1181
1182 DEBUG2(ql4_printk(KERN_INFO, ha,
1183 "Match found in fwdb sessions\n"));
1184 return QLA_SUCCESS;
1185}
1186
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001187static struct iscsi_cls_session *
1188qla4xxx_session_create(struct iscsi_endpoint *ep,
1189 uint16_t cmds_max, uint16_t qdepth,
1190 uint32_t initial_cmdsn)
1191{
1192 struct iscsi_cls_session *cls_sess;
1193 struct scsi_qla_host *ha;
1194 struct qla_endpoint *qla_ep;
1195 struct ddb_entry *ddb_entry;
Mike Christie13483732011-12-01 21:38:41 -06001196 uint16_t ddb_index;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001197 struct iscsi_session *sess;
1198 struct sockaddr *dst_addr;
1199 int ret;
1200
1201 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1202 if (!ep) {
1203 printk(KERN_ERR "qla4xxx: missing ep.\n");
1204 return NULL;
1205 }
1206
1207 qla_ep = ep->dd_data;
1208 dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
1209 ha = to_qla_host(qla_ep->host);
Manish Rangankar736cf362011-10-07 16:55:46 -07001210
Mike Christie13483732011-12-01 21:38:41 -06001211 ret = qla4xxx_get_ddb_index(ha, &ddb_index);
1212 if (ret == QLA_ERROR)
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001213 return NULL;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001214
1215 cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport, qla_ep->host,
1216 cmds_max, sizeof(struct ddb_entry),
1217 sizeof(struct ql4_task_data),
1218 initial_cmdsn, ddb_index);
1219 if (!cls_sess)
1220 return NULL;
1221
1222 sess = cls_sess->dd_data;
1223 ddb_entry = sess->dd_data;
1224 ddb_entry->fw_ddb_index = ddb_index;
1225 ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
1226 ddb_entry->ha = ha;
1227 ddb_entry->sess = cls_sess;
Mike Christie13483732011-12-01 21:38:41 -06001228 ddb_entry->unblock_sess = qla4xxx_unblock_ddb;
1229 ddb_entry->ddb_change = qla4xxx_ddb_change;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001230 cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
1231 ha->fw_ddb_index_map[ddb_entry->fw_ddb_index] = ddb_entry;
1232 ha->tot_ddbs++;
1233
1234 return cls_sess;
1235}
1236
1237static void qla4xxx_session_destroy(struct iscsi_cls_session *cls_sess)
1238{
1239 struct iscsi_session *sess;
1240 struct ddb_entry *ddb_entry;
1241 struct scsi_qla_host *ha;
1242 unsigned long flags;
1243
1244 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1245 sess = cls_sess->dd_data;
1246 ddb_entry = sess->dd_data;
1247 ha = ddb_entry->ha;
1248
Manish Rangankar736cf362011-10-07 16:55:46 -07001249 qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
1250
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001251 spin_lock_irqsave(&ha->hardware_lock, flags);
1252 qla4xxx_free_ddb(ha, ddb_entry);
1253 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1254 iscsi_session_teardown(cls_sess);
1255}
1256
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001257static struct iscsi_cls_conn *
1258qla4xxx_conn_create(struct iscsi_cls_session *cls_sess, uint32_t conn_idx)
1259{
1260 struct iscsi_cls_conn *cls_conn;
1261 struct iscsi_session *sess;
1262 struct ddb_entry *ddb_entry;
1263
1264 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1265 cls_conn = iscsi_conn_setup(cls_sess, sizeof(struct qla_conn),
1266 conn_idx);
Mike Christieff1d0312011-12-01 21:38:43 -06001267 if (!cls_conn)
1268 return NULL;
1269
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001270 sess = cls_sess->dd_data;
1271 ddb_entry = sess->dd_data;
1272 ddb_entry->conn = cls_conn;
1273
1274 return cls_conn;
1275}
1276
1277static int qla4xxx_conn_bind(struct iscsi_cls_session *cls_session,
1278 struct iscsi_cls_conn *cls_conn,
1279 uint64_t transport_fd, int is_leading)
1280{
1281 struct iscsi_conn *conn;
1282 struct qla_conn *qla_conn;
1283 struct iscsi_endpoint *ep;
1284
1285 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1286
1287 if (iscsi_conn_bind(cls_session, cls_conn, is_leading))
1288 return -EINVAL;
1289 ep = iscsi_lookup_endpoint(transport_fd);
1290 conn = cls_conn->dd_data;
1291 qla_conn = conn->dd_data;
1292 qla_conn->qla_ep = ep->dd_data;
1293 return 0;
1294}
1295
1296static int qla4xxx_conn_start(struct iscsi_cls_conn *cls_conn)
1297{
1298 struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
1299 struct iscsi_session *sess;
1300 struct ddb_entry *ddb_entry;
1301 struct scsi_qla_host *ha;
Mike Christie13483732011-12-01 21:38:41 -06001302 struct dev_db_entry *fw_ddb_entry = NULL;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001303 dma_addr_t fw_ddb_entry_dma;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001304 uint32_t mbx_sts = 0;
1305 int ret = 0;
1306 int status = QLA_SUCCESS;
1307
1308 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1309 sess = cls_sess->dd_data;
1310 ddb_entry = sess->dd_data;
1311 ha = ddb_entry->ha;
1312
Mike Christie13483732011-12-01 21:38:41 -06001313 /* Check if we have matching FW DDB, if yes then do not
1314 * login to this target. This could cause target to logout previous
1315 * connection
1316 */
1317 ret = qla4xxx_match_fwdb_session(ha, cls_conn);
1318 if (ret == QLA_SUCCESS) {
1319 ql4_printk(KERN_INFO, ha,
1320 "Session already exist in FW.\n");
1321 ret = -EEXIST;
1322 goto exit_conn_start;
1323 }
1324
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001325 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1326 &fw_ddb_entry_dma, GFP_KERNEL);
1327 if (!fw_ddb_entry) {
1328 ql4_printk(KERN_ERR, ha,
1329 "%s: Unable to allocate dma buffer\n", __func__);
Mike Christie13483732011-12-01 21:38:41 -06001330 ret = -ENOMEM;
1331 goto exit_conn_start;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001332 }
1333
1334 ret = qla4xxx_set_param_ddbentry(ha, ddb_entry, cls_conn, &mbx_sts);
1335 if (ret) {
1336 /* If iscsid is stopped and started then no need to do
1337 * set param again since ddb state will be already
1338 * active and FW does not allow set ddb to an
1339 * active session.
1340 */
1341 if (mbx_sts)
1342 if (ddb_entry->fw_ddb_device_state ==
Manish Rangankarf922da72011-10-07 16:55:49 -07001343 DDB_DS_SESSION_ACTIVE) {
Mike Christie13483732011-12-01 21:38:41 -06001344 ddb_entry->unblock_sess(ddb_entry->sess);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001345 goto exit_set_param;
Manish Rangankarf922da72011-10-07 16:55:49 -07001346 }
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001347
1348 ql4_printk(KERN_ERR, ha, "%s: Failed set param for index[%d]\n",
1349 __func__, ddb_entry->fw_ddb_index);
1350 goto exit_conn_start;
1351 }
1352
1353 status = qla4xxx_conn_open(ha, ddb_entry->fw_ddb_index);
1354 if (status == QLA_ERROR) {
Manish Rangankar0e7e8502011-07-25 13:48:54 -05001355 ql4_printk(KERN_ERR, ha, "%s: Login failed: %s\n", __func__,
1356 sess->targetname);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001357 ret = -EINVAL;
1358 goto exit_conn_start;
1359 }
1360
Manish Rangankar98270ab2011-10-07 16:55:47 -07001361 if (ddb_entry->fw_ddb_device_state == DDB_DS_NO_CONNECTION_ACTIVE)
1362 ddb_entry->fw_ddb_device_state = DDB_DS_LOGIN_IN_PROCESS;
1363
1364 DEBUG2(printk(KERN_INFO "%s: DDB state [%d]\n", __func__,
1365 ddb_entry->fw_ddb_device_state));
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001366
1367exit_set_param:
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001368 ret = 0;
1369
1370exit_conn_start:
Mike Christie13483732011-12-01 21:38:41 -06001371 if (fw_ddb_entry)
1372 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1373 fw_ddb_entry, fw_ddb_entry_dma);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001374 return ret;
1375}
1376
1377static void qla4xxx_conn_destroy(struct iscsi_cls_conn *cls_conn)
1378{
1379 struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
1380 struct iscsi_session *sess;
1381 struct scsi_qla_host *ha;
1382 struct ddb_entry *ddb_entry;
1383 int options;
1384
1385 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1386 sess = cls_sess->dd_data;
1387 ddb_entry = sess->dd_data;
1388 ha = ddb_entry->ha;
1389
1390 options = LOGOUT_OPTION_CLOSE_SESSION;
1391 if (qla4xxx_session_logout_ddb(ha, ddb_entry, options) == QLA_ERROR)
1392 ql4_printk(KERN_ERR, ha, "%s: Logout failed\n", __func__);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001393}
1394
1395static void qla4xxx_task_work(struct work_struct *wdata)
1396{
1397 struct ql4_task_data *task_data;
1398 struct scsi_qla_host *ha;
1399 struct passthru_status *sts;
1400 struct iscsi_task *task;
1401 struct iscsi_hdr *hdr;
1402 uint8_t *data;
1403 uint32_t data_len;
1404 struct iscsi_conn *conn;
1405 int hdr_len;
1406 itt_t itt;
1407
1408 task_data = container_of(wdata, struct ql4_task_data, task_work);
1409 ha = task_data->ha;
1410 task = task_data->task;
1411 sts = &task_data->sts;
1412 hdr_len = sizeof(struct iscsi_hdr);
1413
1414 DEBUG3(printk(KERN_INFO "Status returned\n"));
1415 DEBUG3(qla4xxx_dump_buffer(sts, 64));
1416 DEBUG3(printk(KERN_INFO "Response buffer"));
1417 DEBUG3(qla4xxx_dump_buffer(task_data->resp_buffer, 64));
1418
1419 conn = task->conn;
1420
1421 switch (sts->completionStatus) {
1422 case PASSTHRU_STATUS_COMPLETE:
1423 hdr = (struct iscsi_hdr *)task_data->resp_buffer;
1424 /* Assign back the itt in hdr, until we use the PREASSIGN_TAG */
1425 itt = sts->handle;
1426 hdr->itt = itt;
1427 data = task_data->resp_buffer + hdr_len;
1428 data_len = task_data->resp_len - hdr_len;
1429 iscsi_complete_pdu(conn, hdr, data, data_len);
1430 break;
1431 default:
1432 ql4_printk(KERN_ERR, ha, "Passthru failed status = 0x%x\n",
1433 sts->completionStatus);
1434 break;
1435 }
1436 return;
1437}
1438
1439static int qla4xxx_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
1440{
1441 struct ql4_task_data *task_data;
1442 struct iscsi_session *sess;
1443 struct ddb_entry *ddb_entry;
1444 struct scsi_qla_host *ha;
1445 int hdr_len;
1446
1447 sess = task->conn->session;
1448 ddb_entry = sess->dd_data;
1449 ha = ddb_entry->ha;
1450 task_data = task->dd_data;
1451 memset(task_data, 0, sizeof(struct ql4_task_data));
1452
1453 if (task->sc) {
1454 ql4_printk(KERN_INFO, ha,
1455 "%s: SCSI Commands not implemented\n", __func__);
1456 return -EINVAL;
1457 }
1458
1459 hdr_len = sizeof(struct iscsi_hdr);
1460 task_data->ha = ha;
1461 task_data->task = task;
1462
1463 if (task->data_count) {
1464 task_data->data_dma = dma_map_single(&ha->pdev->dev, task->data,
1465 task->data_count,
1466 PCI_DMA_TODEVICE);
1467 }
1468
1469 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
1470 __func__, task->conn->max_recv_dlength, hdr_len));
1471
Manish Rangankar69ca2162011-10-07 16:55:50 -07001472 task_data->resp_len = task->conn->max_recv_dlength + hdr_len;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001473 task_data->resp_buffer = dma_alloc_coherent(&ha->pdev->dev,
1474 task_data->resp_len,
1475 &task_data->resp_dma,
1476 GFP_ATOMIC);
1477 if (!task_data->resp_buffer)
1478 goto exit_alloc_pdu;
1479
Manish Rangankar69ca2162011-10-07 16:55:50 -07001480 task_data->req_len = task->data_count + hdr_len;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001481 task_data->req_buffer = dma_alloc_coherent(&ha->pdev->dev,
Manish Rangankar69ca2162011-10-07 16:55:50 -07001482 task_data->req_len,
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001483 &task_data->req_dma,
1484 GFP_ATOMIC);
1485 if (!task_data->req_buffer)
1486 goto exit_alloc_pdu;
1487
1488 task->hdr = task_data->req_buffer;
1489
1490 INIT_WORK(&task_data->task_work, qla4xxx_task_work);
1491
1492 return 0;
1493
1494exit_alloc_pdu:
1495 if (task_data->resp_buffer)
1496 dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
1497 task_data->resp_buffer, task_data->resp_dma);
1498
1499 if (task_data->req_buffer)
Manish Rangankar69ca2162011-10-07 16:55:50 -07001500 dma_free_coherent(&ha->pdev->dev, task_data->req_len,
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001501 task_data->req_buffer, task_data->req_dma);
1502 return -ENOMEM;
1503}
1504
1505static void qla4xxx_task_cleanup(struct iscsi_task *task)
1506{
1507 struct ql4_task_data *task_data;
1508 struct iscsi_session *sess;
1509 struct ddb_entry *ddb_entry;
1510 struct scsi_qla_host *ha;
1511 int hdr_len;
1512
1513 hdr_len = sizeof(struct iscsi_hdr);
1514 sess = task->conn->session;
1515 ddb_entry = sess->dd_data;
1516 ha = ddb_entry->ha;
1517 task_data = task->dd_data;
1518
1519 if (task->data_count) {
1520 dma_unmap_single(&ha->pdev->dev, task_data->data_dma,
1521 task->data_count, PCI_DMA_TODEVICE);
1522 }
1523
1524 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
1525 __func__, task->conn->max_recv_dlength, hdr_len));
1526
1527 dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
1528 task_data->resp_buffer, task_data->resp_dma);
Manish Rangankar69ca2162011-10-07 16:55:50 -07001529 dma_free_coherent(&ha->pdev->dev, task_data->req_len,
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001530 task_data->req_buffer, task_data->req_dma);
1531 return;
1532}
1533
1534static int qla4xxx_task_xmit(struct iscsi_task *task)
1535{
1536 struct scsi_cmnd *sc = task->sc;
1537 struct iscsi_session *sess = task->conn->session;
1538 struct ddb_entry *ddb_entry = sess->dd_data;
1539 struct scsi_qla_host *ha = ddb_entry->ha;
1540
1541 if (!sc)
1542 return qla4xxx_send_passthru0(task);
1543
1544 ql4_printk(KERN_INFO, ha, "%s: scsi cmd xmit not implemented\n",
1545 __func__);
1546 return -ENOSYS;
1547}
1548
Mike Christie13483732011-12-01 21:38:41 -06001549static void qla4xxx_copy_fwddb_param(struct scsi_qla_host *ha,
1550 struct dev_db_entry *fw_ddb_entry,
1551 struct iscsi_cls_session *cls_sess,
1552 struct iscsi_cls_conn *cls_conn)
1553{
1554 int buflen = 0;
1555 struct iscsi_session *sess;
1556 struct iscsi_conn *conn;
1557 char ip_addr[DDB_IPADDR_LEN];
1558 uint16_t options = 0;
1559
1560 sess = cls_sess->dd_data;
1561 conn = cls_conn->dd_data;
1562
1563 conn->max_recv_dlength = BYTE_UNITS *
1564 le16_to_cpu(fw_ddb_entry->iscsi_max_rcv_data_seg_len);
1565
1566 conn->max_xmit_dlength = BYTE_UNITS *
1567 le16_to_cpu(fw_ddb_entry->iscsi_max_snd_data_seg_len);
1568
1569 sess->initial_r2t_en =
1570 (BIT_10 & le16_to_cpu(fw_ddb_entry->iscsi_options));
1571
1572 sess->max_r2t = le16_to_cpu(fw_ddb_entry->iscsi_max_outsnd_r2t);
1573
1574 sess->imm_data_en = (BIT_11 & le16_to_cpu(fw_ddb_entry->iscsi_options));
1575
1576 sess->first_burst = BYTE_UNITS *
1577 le16_to_cpu(fw_ddb_entry->iscsi_first_burst_len);
1578
1579 sess->max_burst = BYTE_UNITS *
1580 le16_to_cpu(fw_ddb_entry->iscsi_max_burst_len);
1581
1582 sess->time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
1583
1584 sess->time2retain = le16_to_cpu(fw_ddb_entry->iscsi_def_time2retain);
1585
1586 conn->persistent_port = le16_to_cpu(fw_ddb_entry->port);
1587
1588 sess->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
1589
1590 options = le16_to_cpu(fw_ddb_entry->options);
1591 if (options & DDB_OPT_IPV6_DEVICE)
1592 sprintf(ip_addr, "%pI6", fw_ddb_entry->ip_addr);
1593 else
1594 sprintf(ip_addr, "%pI4", fw_ddb_entry->ip_addr);
1595
1596 iscsi_set_param(cls_conn, ISCSI_PARAM_TARGET_NAME,
1597 (char *)fw_ddb_entry->iscsi_name, buflen);
1598 iscsi_set_param(cls_conn, ISCSI_PARAM_INITIATOR_NAME,
1599 (char *)ha->name_string, buflen);
1600 iscsi_set_param(cls_conn, ISCSI_PARAM_PERSISTENT_ADDRESS,
1601 (char *)ip_addr, buflen);
Vikas Chaudhary6c1b8782012-01-19 03:06:54 -08001602 iscsi_set_param(cls_conn, ISCSI_PARAM_TARGET_ALIAS,
1603 (char *)fw_ddb_entry->iscsi_alias, buflen);
Mike Christie13483732011-12-01 21:38:41 -06001604}
1605
1606void qla4xxx_update_session_conn_fwddb_param(struct scsi_qla_host *ha,
1607 struct ddb_entry *ddb_entry)
1608{
1609 struct iscsi_cls_session *cls_sess;
1610 struct iscsi_cls_conn *cls_conn;
1611 uint32_t ddb_state;
1612 dma_addr_t fw_ddb_entry_dma;
1613 struct dev_db_entry *fw_ddb_entry;
1614
1615 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1616 &fw_ddb_entry_dma, GFP_KERNEL);
1617 if (!fw_ddb_entry) {
1618 ql4_printk(KERN_ERR, ha,
1619 "%s: Unable to allocate dma buffer\n", __func__);
1620 goto exit_session_conn_fwddb_param;
1621 }
1622
1623 if (qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index, fw_ddb_entry,
1624 fw_ddb_entry_dma, NULL, NULL, &ddb_state,
1625 NULL, NULL, NULL) == QLA_ERROR) {
1626 DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: failed "
1627 "get_ddb_entry for fw_ddb_index %d\n",
1628 ha->host_no, __func__,
1629 ddb_entry->fw_ddb_index));
1630 goto exit_session_conn_fwddb_param;
1631 }
1632
1633 cls_sess = ddb_entry->sess;
1634
1635 cls_conn = ddb_entry->conn;
1636
1637 /* Update params */
1638 qla4xxx_copy_fwddb_param(ha, fw_ddb_entry, cls_sess, cls_conn);
1639
1640exit_session_conn_fwddb_param:
1641 if (fw_ddb_entry)
1642 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1643 fw_ddb_entry, fw_ddb_entry_dma);
1644}
1645
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001646void qla4xxx_update_session_conn_param(struct scsi_qla_host *ha,
1647 struct ddb_entry *ddb_entry)
1648{
1649 struct iscsi_cls_session *cls_sess;
1650 struct iscsi_cls_conn *cls_conn;
1651 struct iscsi_session *sess;
1652 struct iscsi_conn *conn;
1653 uint32_t ddb_state;
1654 dma_addr_t fw_ddb_entry_dma;
1655 struct dev_db_entry *fw_ddb_entry;
1656
1657 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1658 &fw_ddb_entry_dma, GFP_KERNEL);
1659 if (!fw_ddb_entry) {
1660 ql4_printk(KERN_ERR, ha,
1661 "%s: Unable to allocate dma buffer\n", __func__);
Mike Christie13483732011-12-01 21:38:41 -06001662 goto exit_session_conn_param;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001663 }
1664
1665 if (qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index, fw_ddb_entry,
1666 fw_ddb_entry_dma, NULL, NULL, &ddb_state,
1667 NULL, NULL, NULL) == QLA_ERROR) {
1668 DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: failed "
1669 "get_ddb_entry for fw_ddb_index %d\n",
1670 ha->host_no, __func__,
1671 ddb_entry->fw_ddb_index));
Mike Christie13483732011-12-01 21:38:41 -06001672 goto exit_session_conn_param;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001673 }
1674
1675 cls_sess = ddb_entry->sess;
1676 sess = cls_sess->dd_data;
1677
1678 cls_conn = ddb_entry->conn;
1679 conn = cls_conn->dd_data;
1680
Mike Christie13483732011-12-01 21:38:41 -06001681 /* Update timers after login */
1682 ddb_entry->default_relogin_timeout =
Nilesh Javalic28eaac2011-12-18 21:40:44 -08001683 (le16_to_cpu(fw_ddb_entry->def_timeout) > LOGIN_TOV) &&
1684 (le16_to_cpu(fw_ddb_entry->def_timeout) < LOGIN_TOV * 10) ?
1685 le16_to_cpu(fw_ddb_entry->def_timeout) : LOGIN_TOV;
Mike Christie13483732011-12-01 21:38:41 -06001686 ddb_entry->default_time2wait =
1687 le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
1688
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001689 /* Update params */
1690 conn->max_recv_dlength = BYTE_UNITS *
1691 le16_to_cpu(fw_ddb_entry->iscsi_max_rcv_data_seg_len);
1692
1693 conn->max_xmit_dlength = BYTE_UNITS *
1694 le16_to_cpu(fw_ddb_entry->iscsi_max_snd_data_seg_len);
1695
1696 sess->initial_r2t_en =
1697 (BIT_10 & le16_to_cpu(fw_ddb_entry->iscsi_options));
1698
1699 sess->max_r2t = le16_to_cpu(fw_ddb_entry->iscsi_max_outsnd_r2t);
1700
1701 sess->imm_data_en = (BIT_11 & le16_to_cpu(fw_ddb_entry->iscsi_options));
1702
1703 sess->first_burst = BYTE_UNITS *
1704 le16_to_cpu(fw_ddb_entry->iscsi_first_burst_len);
1705
1706 sess->max_burst = BYTE_UNITS *
1707 le16_to_cpu(fw_ddb_entry->iscsi_max_burst_len);
1708
1709 sess->time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
1710
1711 sess->time2retain = le16_to_cpu(fw_ddb_entry->iscsi_def_time2retain);
1712
1713 sess->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
1714
1715 memcpy(sess->initiatorname, ha->name_string,
1716 min(sizeof(ha->name_string), sizeof(sess->initiatorname)));
Mike Christie13483732011-12-01 21:38:41 -06001717
Vikas Chaudhary6c1b8782012-01-19 03:06:54 -08001718 iscsi_set_param(cls_conn, ISCSI_PARAM_TARGET_ALIAS,
1719 (char *)fw_ddb_entry->iscsi_alias, 0);
1720
Mike Christie13483732011-12-01 21:38:41 -06001721exit_session_conn_param:
1722 if (fw_ddb_entry)
1723 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1724 fw_ddb_entry, fw_ddb_entry_dma);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001725}
1726
David Somayajuluafaf5a22006-09-19 10:28:00 -07001727/*
1728 * Timer routines
1729 */
1730
1731static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
1732 unsigned long interval)
1733{
1734 DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
1735 __func__, ha->host->host_no));
1736 init_timer(&ha->timer);
1737 ha->timer.expires = jiffies + interval * HZ;
1738 ha->timer.data = (unsigned long)ha;
1739 ha->timer.function = (void (*)(unsigned long))func;
1740 add_timer(&ha->timer);
1741 ha->timer_active = 1;
1742}
1743
1744static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
1745{
1746 del_timer_sync(&ha->timer);
1747 ha->timer_active = 0;
1748}
1749
1750/***
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001751 * qla4xxx_mark_device_missing - blocks the session
1752 * @cls_session: Pointer to the session to be blocked
David Somayajuluafaf5a22006-09-19 10:28:00 -07001753 * @ddb_entry: Pointer to device database entry
1754 *
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301755 * This routine marks a device missing and close connection.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001756 **/
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001757void qla4xxx_mark_device_missing(struct iscsi_cls_session *cls_session)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001758{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001759 iscsi_block_session(cls_session);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001760}
1761
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301762/**
1763 * qla4xxx_mark_all_devices_missing - mark all devices as missing.
1764 * @ha: Pointer to host adapter structure.
1765 *
1766 * This routine marks a device missing and resets the relogin retry count.
1767 **/
1768void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
1769{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001770 iscsi_host_for_each_session(ha->host, qla4xxx_mark_device_missing);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301771}
1772
David Somayajuluafaf5a22006-09-19 10:28:00 -07001773static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
1774 struct ddb_entry *ddb_entry,
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001775 struct scsi_cmnd *cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001776{
1777 struct srb *srb;
1778
1779 srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
1780 if (!srb)
1781 return srb;
1782
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301783 kref_init(&srb->srb_ref);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001784 srb->ha = ha;
1785 srb->ddb = ddb_entry;
1786 srb->cmd = cmd;
1787 srb->flags = 0;
Vikas Chaudhary53698872010-04-28 11:41:59 +05301788 CMD_SP(cmd) = (void *)srb;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001789
1790 return srb;
1791}
1792
1793static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
1794{
1795 struct scsi_cmnd *cmd = srb->cmd;
1796
1797 if (srb->flags & SRB_DMA_VALID) {
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +09001798 scsi_dma_unmap(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001799 srb->flags &= ~SRB_DMA_VALID;
1800 }
Vikas Chaudhary53698872010-04-28 11:41:59 +05301801 CMD_SP(cmd) = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001802}
1803
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301804void qla4xxx_srb_compl(struct kref *ref)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001805{
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301806 struct srb *srb = container_of(ref, struct srb, srb_ref);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001807 struct scsi_cmnd *cmd = srb->cmd;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301808 struct scsi_qla_host *ha = srb->ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001809
1810 qla4xxx_srb_free_dma(ha, srb);
1811
1812 mempool_free(srb, ha->srb_mempool);
1813
1814 cmd->scsi_done(cmd);
1815}
1816
1817/**
1818 * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001819 * @host: scsi host
David Somayajuluafaf5a22006-09-19 10:28:00 -07001820 * @cmd: Pointer to Linux's SCSI command structure
David Somayajuluafaf5a22006-09-19 10:28:00 -07001821 *
1822 * Remarks:
1823 * This routine is invoked by Linux to send a SCSI command to the driver.
1824 * The mid-level driver tries to ensure that queuecommand never gets
1825 * invoked concurrently with itself or the interrupt handler (although
1826 * the interrupt handler may call this routine as part of request-
1827 * completion handling). Unfortunely, it sometimes calls the scheduler
1828 * in interrupt context which is a big NO! NO!.
1829 **/
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001830static int qla4xxx_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001831{
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001832 struct scsi_qla_host *ha = to_qla_host(host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001833 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christie7fb19212008-01-31 13:36:45 -06001834 struct iscsi_cls_session *sess = ddb_entry->sess;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001835 struct srb *srb;
1836 int rval;
1837
Lalit Chandivade2232be02010-07-30 14:38:47 +05301838 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1839 if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
1840 cmd->result = DID_NO_CONNECT << 16;
1841 else
1842 cmd->result = DID_REQUEUE << 16;
1843 goto qc_fail_command;
1844 }
1845
Mike Christie7fb19212008-01-31 13:36:45 -06001846 if (!sess) {
1847 cmd->result = DID_IMM_RETRY << 16;
1848 goto qc_fail_command;
1849 }
1850
1851 rval = iscsi_session_chkready(sess);
1852 if (rval) {
1853 cmd->result = rval;
1854 goto qc_fail_command;
1855 }
1856
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301857 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1858 test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
1859 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1860 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
1861 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
1862 !test_bit(AF_ONLINE, &ha->flags) ||
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001863 !test_bit(AF_LINK_UP, &ha->flags) ||
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301864 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
David C Somayajulu477ffb92007-01-22 12:26:11 -08001865 goto qc_host_busy;
1866
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001867 srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001868 if (!srb)
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001869 goto qc_host_busy;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001870
1871 rval = qla4xxx_send_command_to_isp(ha, srb);
1872 if (rval != QLA_SUCCESS)
1873 goto qc_host_busy_free_sp;
1874
David Somayajuluafaf5a22006-09-19 10:28:00 -07001875 return 0;
1876
1877qc_host_busy_free_sp:
1878 qla4xxx_srb_free_dma(ha, srb);
1879 mempool_free(srb, ha->srb_mempool);
1880
David Somayajuluafaf5a22006-09-19 10:28:00 -07001881qc_host_busy:
1882 return SCSI_MLQUEUE_HOST_BUSY;
1883
1884qc_fail_command:
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001885 cmd->scsi_done(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001886
1887 return 0;
1888}
1889
1890/**
1891 * qla4xxx_mem_free - frees memory allocated to adapter
1892 * @ha: Pointer to host adapter structure.
1893 *
1894 * Frees memory previously allocated by qla4xxx_mem_alloc
1895 **/
1896static void qla4xxx_mem_free(struct scsi_qla_host *ha)
1897{
1898 if (ha->queues)
1899 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
1900 ha->queues_dma);
1901
1902 ha->queues_len = 0;
1903 ha->queues = NULL;
1904 ha->queues_dma = 0;
1905 ha->request_ring = NULL;
1906 ha->request_dma = 0;
1907 ha->response_ring = NULL;
1908 ha->response_dma = 0;
1909 ha->shadow_regs = NULL;
1910 ha->shadow_regs_dma = 0;
1911
1912 /* Free srb pool. */
1913 if (ha->srb_mempool)
1914 mempool_destroy(ha->srb_mempool);
1915
1916 ha->srb_mempool = NULL;
1917
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001918 if (ha->chap_dma_pool)
1919 dma_pool_destroy(ha->chap_dma_pool);
1920
Lalit Chandivade45494152011-10-07 16:55:42 -07001921 if (ha->chap_list)
1922 vfree(ha->chap_list);
1923 ha->chap_list = NULL;
1924
Mike Christie13483732011-12-01 21:38:41 -06001925 if (ha->fw_ddb_dma_pool)
1926 dma_pool_destroy(ha->fw_ddb_dma_pool);
1927
David Somayajuluafaf5a22006-09-19 10:28:00 -07001928 /* release io space registers */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301929 if (is_qla8022(ha)) {
1930 if (ha->nx_pcibase)
1931 iounmap(
1932 (struct device_reg_82xx __iomem *)ha->nx_pcibase);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301933 } else if (ha->reg)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001934 iounmap(ha->reg);
1935 pci_release_regions(ha->pdev);
1936}
1937
1938/**
1939 * qla4xxx_mem_alloc - allocates memory for use by adapter.
1940 * @ha: Pointer to host adapter structure
1941 *
1942 * Allocates DMA memory for request and response queues. Also allocates memory
1943 * for srbs.
1944 **/
1945static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
1946{
1947 unsigned long align;
1948
1949 /* Allocate contiguous block of DMA memory for queues. */
1950 ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
1951 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
1952 sizeof(struct shadow_regs) +
1953 MEM_ALIGN_VALUE +
1954 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
1955 ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
1956 &ha->queues_dma, GFP_KERNEL);
1957 if (ha->queues == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301958 ql4_printk(KERN_WARNING, ha,
1959 "Memory Allocation failed - queues.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001960
1961 goto mem_alloc_error_exit;
1962 }
1963 memset(ha->queues, 0, ha->queues_len);
1964
1965 /*
1966 * As per RISC alignment requirements -- the bus-address must be a
1967 * multiple of the request-ring size (in bytes).
1968 */
1969 align = 0;
1970 if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
1971 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
1972 (MEM_ALIGN_VALUE - 1));
1973
1974 /* Update request and response queue pointers. */
1975 ha->request_dma = ha->queues_dma + align;
1976 ha->request_ring = (struct queue_entry *) (ha->queues + align);
1977 ha->response_dma = ha->queues_dma + align +
1978 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
1979 ha->response_ring = (struct queue_entry *) (ha->queues + align +
1980 (REQUEST_QUEUE_DEPTH *
1981 QUEUE_SIZE));
1982 ha->shadow_regs_dma = ha->queues_dma + align +
1983 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
1984 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
1985 ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
1986 (REQUEST_QUEUE_DEPTH *
1987 QUEUE_SIZE) +
1988 (RESPONSE_QUEUE_DEPTH *
1989 QUEUE_SIZE));
1990
1991 /* Allocate memory for srb pool. */
1992 ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
1993 mempool_free_slab, srb_cachep);
1994 if (ha->srb_mempool == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301995 ql4_printk(KERN_WARNING, ha,
1996 "Memory Allocation failed - SRB Pool.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001997
1998 goto mem_alloc_error_exit;
1999 }
2000
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002001 ha->chap_dma_pool = dma_pool_create("ql4_chap", &ha->pdev->dev,
2002 CHAP_DMA_BLOCK_SIZE, 8, 0);
2003
2004 if (ha->chap_dma_pool == NULL) {
2005 ql4_printk(KERN_WARNING, ha,
2006 "%s: chap_dma_pool allocation failed..\n", __func__);
2007 goto mem_alloc_error_exit;
2008 }
2009
Mike Christie13483732011-12-01 21:38:41 -06002010 ha->fw_ddb_dma_pool = dma_pool_create("ql4_fw_ddb", &ha->pdev->dev,
2011 DDB_DMA_BLOCK_SIZE, 8, 0);
2012
2013 if (ha->fw_ddb_dma_pool == NULL) {
2014 ql4_printk(KERN_WARNING, ha,
2015 "%s: fw_ddb_dma_pool allocation failed..\n",
2016 __func__);
2017 goto mem_alloc_error_exit;
2018 }
2019
David Somayajuluafaf5a22006-09-19 10:28:00 -07002020 return QLA_SUCCESS;
2021
2022mem_alloc_error_exit:
2023 qla4xxx_mem_free(ha);
2024 return QLA_ERROR;
2025}
2026
2027/**
Mike Hernandez4f770832012-01-11 02:44:15 -08002028 * qla4_8xxx_check_temp - Check the ISP82XX temperature.
2029 * @ha: adapter block pointer.
2030 *
2031 * Note: The caller should not hold the idc lock.
2032 **/
2033static int qla4_8xxx_check_temp(struct scsi_qla_host *ha)
2034{
2035 uint32_t temp, temp_state, temp_val;
2036 int status = QLA_SUCCESS;
2037
2038 temp = qla4_8xxx_rd_32(ha, CRB_TEMP_STATE);
2039
2040 temp_state = qla82xx_get_temp_state(temp);
2041 temp_val = qla82xx_get_temp_val(temp);
2042
2043 if (temp_state == QLA82XX_TEMP_PANIC) {
2044 ql4_printk(KERN_WARNING, ha, "Device temperature %d degrees C"
2045 " exceeds maximum allowed. Hardware has been shut"
2046 " down.\n", temp_val);
2047 status = QLA_ERROR;
2048 } else if (temp_state == QLA82XX_TEMP_WARN) {
2049 if (ha->temperature == QLA82XX_TEMP_NORMAL)
2050 ql4_printk(KERN_WARNING, ha, "Device temperature %d"
2051 " degrees C exceeds operating range."
2052 " Immediate action needed.\n", temp_val);
2053 } else {
2054 if (ha->temperature == QLA82XX_TEMP_WARN)
2055 ql4_printk(KERN_INFO, ha, "Device temperature is"
2056 " now %d degrees C in normal range.\n",
2057 temp_val);
2058 }
2059 ha->temperature = temp_state;
2060 return status;
2061}
2062
2063/**
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302064 * qla4_8xxx_check_fw_alive - Check firmware health
2065 * @ha: Pointer to host adapter structure.
2066 *
2067 * Context: Interrupt
2068 **/
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002069static int qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302070{
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002071 uint32_t fw_heartbeat_counter;
2072 int status = QLA_SUCCESS;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302073
2074 fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
Lalit Chandivade2232be02010-07-30 14:38:47 +05302075 /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
2076 if (fw_heartbeat_counter == 0xffffffff) {
2077 DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
2078 "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
2079 ha->host_no, __func__));
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002080 return status;
Lalit Chandivade2232be02010-07-30 14:38:47 +05302081 }
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302082
2083 if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
2084 ha->seconds_since_last_heartbeat++;
2085 /* FW not alive after 2 seconds */
2086 if (ha->seconds_since_last_heartbeat == 2) {
2087 ha->seconds_since_last_heartbeat = 0;
Vikas Chaudhary68d92eb2011-05-17 23:17:05 -07002088
2089 ql4_printk(KERN_INFO, ha,
2090 "scsi(%ld): %s, Dumping hw/fw registers:\n "
2091 " PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2:"
2092 " 0x%x,\n PEG_NET_0_PC: 0x%x, PEG_NET_1_PC:"
2093 " 0x%x,\n PEG_NET_2_PC: 0x%x, PEG_NET_3_PC:"
2094 " 0x%x,\n PEG_NET_4_PC: 0x%x\n",
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002095 ha->host_no, __func__,
2096 qla4_8xxx_rd_32(ha,
2097 QLA82XX_PEG_HALT_STATUS1),
Vikas Chaudhary68d92eb2011-05-17 23:17:05 -07002098 qla4_8xxx_rd_32(ha,
2099 QLA82XX_PEG_HALT_STATUS2),
2100 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_0 +
2101 0x3c),
2102 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_1 +
2103 0x3c),
2104 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_2 +
2105 0x3c),
2106 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_3 +
2107 0x3c),
2108 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_4 +
2109 0x3c));
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002110 status = QLA_ERROR;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302111 }
Lalit Chandivade99457d72010-10-06 22:49:32 -07002112 } else
2113 ha->seconds_since_last_heartbeat = 0;
2114
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302115 ha->fw_heartbeat_counter = fw_heartbeat_counter;
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002116 return status;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302117}
2118
2119/**
2120 * qla4_8xxx_watchdog - Poll dev state
2121 * @ha: Pointer to host adapter structure.
2122 *
2123 * Context: Interrupt
2124 **/
2125void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
2126{
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002127 uint32_t dev_state, halt_status;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302128
2129 /* don't poll if reset is going on */
Lalit Chandivaded56a1f72010-12-02 22:12:45 -08002130 if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
2131 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
Vikas Chaudhary977f46a2011-05-17 23:17:08 -07002132 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags))) {
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002133 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
Mike Hernandez4f770832012-01-11 02:44:15 -08002134
2135 if (qla4_8xxx_check_temp(ha)) {
Giridhar Malavalie6bd0eb2012-01-11 02:44:16 -08002136 ql4_printk(KERN_INFO, ha, "disabling pause"
2137 " transmit on port 0 & 1.\n");
2138 qla4_8xxx_wr_32(ha, QLA82XX_CRB_NIU + 0x98,
2139 CRB_NIU_XG_PAUSE_CTL_P0 |
2140 CRB_NIU_XG_PAUSE_CTL_P1);
Mike Hernandez4f770832012-01-11 02:44:15 -08002141 set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
2142 qla4xxx_wake_dpc(ha);
2143 } else if (dev_state == QLA82XX_DEV_NEED_RESET &&
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302144 !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
Vikas Chaudhary3930b8c2010-12-02 22:12:47 -08002145 if (!ql4xdontresethba) {
2146 ql4_printk(KERN_INFO, ha, "%s: HW State: "
2147 "NEED RESET!\n", __func__);
2148 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2149 qla4xxx_wake_dpc(ha);
Vikas Chaudhary3930b8c2010-12-02 22:12:47 -08002150 }
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302151 } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
2152 !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
Vikas Chaudhary3930b8c2010-12-02 22:12:47 -08002153 ql4_printk(KERN_INFO, ha, "%s: HW State: NEED QUIES!\n",
2154 __func__);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302155 set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
2156 qla4xxx_wake_dpc(ha);
2157 } else {
2158 /* Check firmware health */
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002159 if (qla4_8xxx_check_fw_alive(ha)) {
Giridhar Malavalie6bd0eb2012-01-11 02:44:16 -08002160 ql4_printk(KERN_INFO, ha, "disabling pause"
2161 " transmit on port 0 & 1.\n");
2162 qla4_8xxx_wr_32(ha, QLA82XX_CRB_NIU + 0x98,
2163 CRB_NIU_XG_PAUSE_CTL_P0 |
2164 CRB_NIU_XG_PAUSE_CTL_P1);
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002165 halt_status = qla4_8xxx_rd_32(ha,
2166 QLA82XX_PEG_HALT_STATUS1);
2167
Nilesh Javali527c8b22012-01-11 02:44:17 -08002168 if (LSW(MSB(halt_status)) == 0x67)
2169 ql4_printk(KERN_ERR, ha, "%s:"
2170 " Firmware aborted with"
2171 " error code 0x00006700."
2172 " Device is being reset\n",
2173 __func__);
2174
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002175 /* Since we cannot change dev_state in interrupt
2176 * context, set appropriate DPC flag then wakeup
2177 * DPC */
2178 if (halt_status & HALT_STATUS_UNRECOVERABLE)
2179 set_bit(DPC_HA_UNRECOVERABLE,
2180 &ha->dpc_flags);
2181 else {
2182 ql4_printk(KERN_INFO, ha, "%s: detect "
2183 "abort needed!\n", __func__);
2184 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2185 }
2186 qla4xxx_mailbox_premature_completion(ha);
2187 qla4xxx_wake_dpc(ha);
2188 }
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302189 }
2190 }
2191}
2192
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08002193static void qla4xxx_check_relogin_flash_ddb(struct iscsi_cls_session *cls_sess)
Mike Christie13483732011-12-01 21:38:41 -06002194{
2195 struct iscsi_session *sess;
2196 struct ddb_entry *ddb_entry;
2197 struct scsi_qla_host *ha;
2198
2199 sess = cls_sess->dd_data;
2200 ddb_entry = sess->dd_data;
2201 ha = ddb_entry->ha;
2202
2203 if (!(ddb_entry->ddb_type == FLASH_DDB))
2204 return;
2205
2206 if (adapter_up(ha) && !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
2207 !iscsi_is_session_online(cls_sess)) {
2208 if (atomic_read(&ddb_entry->retry_relogin_timer) !=
2209 INVALID_ENTRY) {
2210 if (atomic_read(&ddb_entry->retry_relogin_timer) ==
2211 0) {
2212 atomic_set(&ddb_entry->retry_relogin_timer,
2213 INVALID_ENTRY);
2214 set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
2215 set_bit(DF_RELOGIN, &ddb_entry->flags);
2216 DEBUG2(ql4_printk(KERN_INFO, ha,
2217 "%s: index [%d] login device\n",
2218 __func__, ddb_entry->fw_ddb_index));
2219 } else
2220 atomic_dec(&ddb_entry->retry_relogin_timer);
2221 }
2222 }
2223
2224 /* Wait for relogin to timeout */
2225 if (atomic_read(&ddb_entry->relogin_timer) &&
2226 (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
2227 /*
2228 * If the relogin times out and the device is
2229 * still NOT ONLINE then try and relogin again.
2230 */
2231 if (!iscsi_is_session_online(cls_sess)) {
2232 /* Reset retry relogin timer */
2233 atomic_inc(&ddb_entry->relogin_retry_count);
2234 DEBUG2(ql4_printk(KERN_INFO, ha,
2235 "%s: index[%d] relogin timed out-retrying"
2236 " relogin (%d), retry (%d)\n", __func__,
2237 ddb_entry->fw_ddb_index,
2238 atomic_read(&ddb_entry->relogin_retry_count),
2239 ddb_entry->default_time2wait + 4));
2240 set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
2241 atomic_set(&ddb_entry->retry_relogin_timer,
2242 ddb_entry->default_time2wait + 4);
2243 }
2244 }
2245}
2246
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302247/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002248 * qla4xxx_timer - checks every second for work to do.
2249 * @ha: Pointer to host adapter structure.
2250 **/
2251static void qla4xxx_timer(struct scsi_qla_host *ha)
2252{
David Somayajuluafaf5a22006-09-19 10:28:00 -07002253 int start_dpc = 0;
Lalit Chandivade2232be02010-07-30 14:38:47 +05302254 uint16_t w;
2255
Mike Christie13483732011-12-01 21:38:41 -06002256 iscsi_host_for_each_session(ha->host, qla4xxx_check_relogin_flash_ddb);
2257
Lalit Chandivade2232be02010-07-30 14:38:47 +05302258 /* If we are in the middle of AER/EEH processing
2259 * skip any processing and reschedule the timer
2260 */
2261 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
2262 mod_timer(&ha->timer, jiffies + HZ);
2263 return;
2264 }
2265
2266 /* Hardware read to trigger an EEH error during mailbox waits. */
2267 if (!pci_channel_offline(ha->pdev))
2268 pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002269
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302270 if (is_qla8022(ha)) {
2271 qla4_8xxx_watchdog(ha);
2272 }
2273
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302274 if (!is_qla8022(ha)) {
2275 /* Check for heartbeat interval. */
2276 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
2277 ha->heartbeat_interval != 0) {
2278 ha->seconds_since_last_heartbeat++;
2279 if (ha->seconds_since_last_heartbeat >
2280 ha->heartbeat_interval + 2)
2281 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2282 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002283 }
2284
David Somayajuluafaf5a22006-09-19 10:28:00 -07002285 /* Wakeup the dpc routine for this adapter, if needed. */
Lalit Chandivade1b468072011-05-17 23:17:09 -07002286 if (start_dpc ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07002287 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
2288 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
2289 test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302290 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07002291 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
2292 test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05302293 test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302294 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
2295 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
Lalit Chandivade1b468072011-05-17 23:17:09 -07002296 test_bit(DPC_AEN, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002297 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
2298 " - dpc flags = 0x%lx\n",
2299 ha->host_no, __func__, ha->dpc_flags));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302300 qla4xxx_wake_dpc(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002301 }
2302
2303 /* Reschedule timer thread to call us back in one second */
2304 mod_timer(&ha->timer, jiffies + HZ);
2305
2306 DEBUG2(ha->seconds_since_last_intr++);
2307}
2308
2309/**
2310 * qla4xxx_cmd_wait - waits for all outstanding commands to complete
2311 * @ha: Pointer to host adapter structure.
2312 *
2313 * This routine stalls the driver until all outstanding commands are returned.
2314 * Caller must release the Hardware Lock prior to calling this routine.
2315 **/
2316static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
2317{
2318 uint32_t index = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002319 unsigned long flags;
2320 struct scsi_cmnd *cmd;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002321
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302322 unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
2323
2324 DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
2325 "complete\n", WAIT_CMD_TOV));
2326
2327 while (!time_after_eq(jiffies, wtime)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002328 spin_lock_irqsave(&ha->hardware_lock, flags);
2329 /* Find a command that hasn't completed. */
2330 for (index = 0; index < ha->host->can_queue; index++) {
2331 cmd = scsi_host_find_tag(ha->host, index);
Mike Christiea1e00632010-10-26 05:45:30 -07002332 /*
2333 * We cannot just check if the index is valid,
2334 * becase if we are run from the scsi eh, then
2335 * the scsi/block layer is going to prevent
2336 * the tag from being released.
2337 */
2338 if (cmd != NULL && CMD_SP(cmd))
David Somayajuluafaf5a22006-09-19 10:28:00 -07002339 break;
2340 }
2341 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2342
2343 /* If No Commands are pending, wait is complete */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302344 if (index == ha->host->can_queue)
2345 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002346
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302347 msleep(1000);
2348 }
2349 /* If we timed out on waiting for commands to come back
2350 * return ERROR. */
2351 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002352}
2353
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302354int qla4xxx_hw_reset(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002355{
David Somayajuluafaf5a22006-09-19 10:28:00 -07002356 uint32_t ctrl_status;
David C Somayajulu477ffb92007-01-22 12:26:11 -08002357 unsigned long flags = 0;
2358
2359 DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -07002360
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302361 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
2362 return QLA_ERROR;
2363
David Somayajuluafaf5a22006-09-19 10:28:00 -07002364 spin_lock_irqsave(&ha->hardware_lock, flags);
2365
2366 /*
2367 * If the SCSI Reset Interrupt bit is set, clear it.
2368 * Otherwise, the Soft Reset won't work.
2369 */
2370 ctrl_status = readw(&ha->reg->ctrl_status);
2371 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
2372 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
2373
2374 /* Issue Soft Reset */
2375 writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
2376 readl(&ha->reg->ctrl_status);
2377
2378 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302379 return QLA_SUCCESS;
David C Somayajulu477ffb92007-01-22 12:26:11 -08002380}
2381
2382/**
2383 * qla4xxx_soft_reset - performs soft reset.
2384 * @ha: Pointer to host adapter structure.
2385 **/
2386int qla4xxx_soft_reset(struct scsi_qla_host *ha)
2387{
2388 uint32_t max_wait_time;
2389 unsigned long flags = 0;
Vikas Chaudharyf931c532010-10-06 22:48:07 -07002390 int status;
David C Somayajulu477ffb92007-01-22 12:26:11 -08002391 uint32_t ctrl_status;
2392
Vikas Chaudharyf931c532010-10-06 22:48:07 -07002393 status = qla4xxx_hw_reset(ha);
2394 if (status != QLA_SUCCESS)
2395 return status;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002396
Vikas Chaudharyf931c532010-10-06 22:48:07 -07002397 status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002398 /* Wait until the Network Reset Intr bit is cleared */
2399 max_wait_time = RESET_INTR_TOV;
2400 do {
2401 spin_lock_irqsave(&ha->hardware_lock, flags);
2402 ctrl_status = readw(&ha->reg->ctrl_status);
2403 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2404
2405 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
2406 break;
2407
2408 msleep(1000);
2409 } while ((--max_wait_time));
2410
2411 if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
2412 DEBUG2(printk(KERN_WARNING
2413 "scsi%ld: Network Reset Intr not cleared by "
2414 "Network function, clearing it now!\n",
2415 ha->host_no));
2416 spin_lock_irqsave(&ha->hardware_lock, flags);
2417 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
2418 readl(&ha->reg->ctrl_status);
2419 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2420 }
2421
2422 /* Wait until the firmware tells us the Soft Reset is done */
2423 max_wait_time = SOFT_RESET_TOV;
2424 do {
2425 spin_lock_irqsave(&ha->hardware_lock, flags);
2426 ctrl_status = readw(&ha->reg->ctrl_status);
2427 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2428
2429 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
2430 status = QLA_SUCCESS;
2431 break;
2432 }
2433
2434 msleep(1000);
2435 } while ((--max_wait_time));
2436
2437 /*
2438 * Also, make sure that the SCSI Reset Interrupt bit has been cleared
2439 * after the soft reset has taken place.
2440 */
2441 spin_lock_irqsave(&ha->hardware_lock, flags);
2442 ctrl_status = readw(&ha->reg->ctrl_status);
2443 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
2444 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
2445 readl(&ha->reg->ctrl_status);
2446 }
2447 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2448
2449 /* If soft reset fails then most probably the bios on other
2450 * function is also enabled.
2451 * Since the initialization is sequential the other fn
2452 * wont be able to acknowledge the soft reset.
2453 * Issue a force soft reset to workaround this scenario.
2454 */
2455 if (max_wait_time == 0) {
2456 /* Issue Force Soft Reset */
2457 spin_lock_irqsave(&ha->hardware_lock, flags);
2458 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
2459 readl(&ha->reg->ctrl_status);
2460 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2461 /* Wait until the firmware tells us the Soft Reset is done */
2462 max_wait_time = SOFT_RESET_TOV;
2463 do {
2464 spin_lock_irqsave(&ha->hardware_lock, flags);
2465 ctrl_status = readw(&ha->reg->ctrl_status);
2466 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2467
2468 if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
2469 status = QLA_SUCCESS;
2470 break;
2471 }
2472
2473 msleep(1000);
2474 } while ((--max_wait_time));
2475 }
2476
2477 return status;
2478}
2479
2480/**
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302481 * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
David Somayajuluafaf5a22006-09-19 10:28:00 -07002482 * @ha: Pointer to host adapter structure.
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302483 * @res: returned scsi status
David Somayajuluafaf5a22006-09-19 10:28:00 -07002484 *
2485 * This routine is called just prior to a HARD RESET to return all
2486 * outstanding commands back to the Operating System.
2487 * Caller should make sure that the following locks are released
2488 * before this calling routine: Hardware lock, and io_request_lock.
2489 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302490static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002491{
2492 struct srb *srb;
2493 int i;
2494 unsigned long flags;
2495
2496 spin_lock_irqsave(&ha->hardware_lock, flags);
2497 for (i = 0; i < ha->host->can_queue; i++) {
2498 srb = qla4xxx_del_from_active_array(ha, i);
2499 if (srb != NULL) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302500 srb->cmd->result = res;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302501 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002502 }
2503 }
2504 spin_unlock_irqrestore(&ha->hardware_lock, flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002505}
2506
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302507void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
2508{
2509 clear_bit(AF_ONLINE, &ha->flags);
2510
2511 /* Disable the board */
2512 ql4_printk(KERN_INFO, ha, "Disabling the board\n");
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302513
2514 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
2515 qla4xxx_mark_all_devices_missing(ha);
2516 clear_bit(AF_INIT_DONE, &ha->flags);
2517}
2518
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002519static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session)
2520{
2521 struct iscsi_session *sess;
2522 struct ddb_entry *ddb_entry;
2523
2524 sess = cls_session->dd_data;
2525 ddb_entry = sess->dd_data;
2526 ddb_entry->fw_ddb_device_state = DDB_DS_SESSION_FAILED;
Mike Christie13483732011-12-01 21:38:41 -06002527
2528 if (ddb_entry->ddb_type == FLASH_DDB)
2529 iscsi_block_session(ddb_entry->sess);
2530 else
2531 iscsi_session_failure(cls_session->dd_data,
2532 ISCSI_ERR_CONN_FAILED);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002533}
2534
David Somayajuluafaf5a22006-09-19 10:28:00 -07002535/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002536 * qla4xxx_recover_adapter - recovers adapter after a fatal error
2537 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07002538 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302539static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002540{
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302541 int status = QLA_ERROR;
2542 uint8_t reset_chip = 0;
Sarang Radke8e0f3a62011-12-01 22:42:09 -08002543 uint32_t dev_state;
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002544 unsigned long wait;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002545
2546 /* Stall incoming I/O until we are done */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302547 scsi_block_requests(ha->host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002548 clear_bit(AF_ONLINE, &ha->flags);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002549 clear_bit(AF_LINK_UP, &ha->flags);
Mike Christie50a29ae2008-03-04 13:26:53 -06002550
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302551 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -07002552
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302553 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002554
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002555 iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
2556
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302557 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
2558 reset_chip = 1;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002559
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302560 /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
2561 * do not reset adapter, jump to initialize_adapter */
2562 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
2563 status = QLA_SUCCESS;
2564 goto recover_ha_init_adapter;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002565 }
2566
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302567 /* For the ISP-82xx adapter, issue a stop_firmware if invoked
2568 * from eh_host_reset or ioctl module */
2569 if (is_qla8022(ha) && !reset_chip &&
2570 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
2571
2572 DEBUG2(ql4_printk(KERN_INFO, ha,
2573 "scsi%ld: %s - Performing stop_firmware...\n",
2574 ha->host_no, __func__));
2575 status = ha->isp_ops->reset_firmware(ha);
2576 if (status == QLA_SUCCESS) {
Nilesh Javali2bd1e2b2010-10-06 22:49:20 -07002577 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
2578 qla4xxx_cmd_wait(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302579 ha->isp_ops->disable_intrs(ha);
2580 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2581 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
2582 } else {
2583 /* If the stop_firmware fails then
2584 * reset the entire chip */
2585 reset_chip = 1;
2586 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2587 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2588 }
2589 }
2590
2591 /* Issue full chip reset if recovering from a catastrophic error,
2592 * or if stop_firmware fails for ISP-82xx.
2593 * This is the default case for ISP-4xxx */
2594 if (!is_qla8022(ha) || reset_chip) {
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002595 if (!is_qla8022(ha))
2596 goto chip_reset;
2597
2598 /* Check if 82XX firmware is alive or not
2599 * We may have arrived here from NEED_RESET
2600 * detection only */
2601 if (test_bit(AF_FW_RECOVERY, &ha->flags))
2602 goto chip_reset;
2603
2604 wait = jiffies + (FW_ALIVE_WAIT_TOV * HZ);
2605 while (time_before(jiffies, wait)) {
2606 if (qla4_8xxx_check_fw_alive(ha)) {
2607 qla4xxx_mailbox_premature_completion(ha);
2608 break;
2609 }
2610
2611 set_current_state(TASK_UNINTERRUPTIBLE);
2612 schedule_timeout(HZ);
2613 }
2614
Nilesh Javali2bd1e2b2010-10-06 22:49:20 -07002615 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
2616 qla4xxx_cmd_wait(ha);
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002617chip_reset:
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302618 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2619 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
2620 DEBUG2(ql4_printk(KERN_INFO, ha,
2621 "scsi%ld: %s - Performing chip reset..\n",
2622 ha->host_no, __func__));
2623 status = ha->isp_ops->reset_chip(ha);
2624 }
2625
2626 /* Flush any pending ddb changed AENs */
2627 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2628
2629recover_ha_init_adapter:
2630 /* Upon successful firmware/chip reset, re-initialize the adapter */
2631 if (status == QLA_SUCCESS) {
2632 /* For ISP-4xxx, force function 1 to always initialize
2633 * before function 3 to prevent both funcions from
2634 * stepping on top of the other */
2635 if (!is_qla8022(ha) && (ha->mac_index == 3))
2636 ssleep(6);
2637
2638 /* NOTE: AF_ONLINE flag set upon successful completion of
2639 * qla4xxx_initialize_adapter */
Mike Christie13483732011-12-01 21:38:41 -06002640 status = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302641 }
2642
2643 /* Retry failed adapter initialization, if necessary
2644 * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
2645 * case to prevent ping-pong resets between functions */
2646 if (!test_bit(AF_ONLINE, &ha->flags) &&
2647 !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002648 /* Adapter initialization failed, see if we can retry
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302649 * resetting the ha.
2650 * Since we don't want to block the DPC for too long
2651 * with multiple resets in the same thread,
2652 * utilize DPC to retry */
Sarang Radke8e0f3a62011-12-01 22:42:09 -08002653 if (is_qla8022(ha)) {
2654 qla4_8xxx_idc_lock(ha);
2655 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
2656 qla4_8xxx_idc_unlock(ha);
2657 if (dev_state == QLA82XX_DEV_FAILED) {
2658 ql4_printk(KERN_INFO, ha, "%s: don't retry "
2659 "recover adapter. H/W is in Failed "
2660 "state\n", __func__);
2661 qla4xxx_dead_adapter_cleanup(ha);
2662 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
2663 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
2664 clear_bit(DPC_RESET_HA_FW_CONTEXT,
2665 &ha->dpc_flags);
2666 status = QLA_ERROR;
2667
2668 goto exit_recover;
2669 }
2670 }
2671
David Somayajuluafaf5a22006-09-19 10:28:00 -07002672 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
2673 ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
2674 DEBUG2(printk("scsi%ld: recover adapter - retrying "
2675 "(%d) more times\n", ha->host_no,
2676 ha->retry_reset_ha_cnt));
2677 set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
2678 status = QLA_ERROR;
2679 } else {
2680 if (ha->retry_reset_ha_cnt > 0) {
2681 /* Schedule another Reset HA--DPC will retry */
2682 ha->retry_reset_ha_cnt--;
2683 DEBUG2(printk("scsi%ld: recover adapter - "
2684 "retry remaining %d\n",
2685 ha->host_no,
2686 ha->retry_reset_ha_cnt));
2687 status = QLA_ERROR;
2688 }
2689
2690 if (ha->retry_reset_ha_cnt == 0) {
2691 /* Recover adapter retries have been exhausted.
2692 * Adapter DEAD */
2693 DEBUG2(printk("scsi%ld: recover adapter "
2694 "failed - board disabled\n",
2695 ha->host_no));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302696 qla4xxx_dead_adapter_cleanup(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002697 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
2698 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302699 clear_bit(DPC_RESET_HA_FW_CONTEXT,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002700 &ha->dpc_flags);
2701 status = QLA_ERROR;
2702 }
2703 }
2704 } else {
2705 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302706 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002707 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
2708 }
2709
Sarang Radke8e0f3a62011-12-01 22:42:09 -08002710exit_recover:
David Somayajuluafaf5a22006-09-19 10:28:00 -07002711 ha->adapter_error_count++;
2712
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302713 if (test_bit(AF_ONLINE, &ha->flags))
2714 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002715
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302716 scsi_unblock_requests(ha->host);
2717
2718 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2719 DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002720 status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302721
David Somayajuluafaf5a22006-09-19 10:28:00 -07002722 return status;
2723}
2724
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002725static void qla4xxx_relogin_devices(struct iscsi_cls_session *cls_session)
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07002726{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002727 struct iscsi_session *sess;
2728 struct ddb_entry *ddb_entry;
2729 struct scsi_qla_host *ha;
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07002730
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002731 sess = cls_session->dd_data;
2732 ddb_entry = sess->dd_data;
2733 ha = ddb_entry->ha;
2734 if (!iscsi_is_session_online(cls_session)) {
2735 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
2736 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
2737 " unblock session\n", ha->host_no, __func__,
2738 ddb_entry->fw_ddb_index);
2739 iscsi_unblock_session(ddb_entry->sess);
2740 } else {
2741 /* Trigger relogin */
Mike Christie13483732011-12-01 21:38:41 -06002742 if (ddb_entry->ddb_type == FLASH_DDB) {
2743 if (!test_bit(DF_RELOGIN, &ddb_entry->flags))
2744 qla4xxx_arm_relogin_timer(ddb_entry);
2745 } else
2746 iscsi_session_failure(cls_session->dd_data,
2747 ISCSI_ERR_CONN_FAILED);
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07002748 }
2749 }
2750}
2751
Mike Christie13483732011-12-01 21:38:41 -06002752int qla4xxx_unblock_flash_ddb(struct iscsi_cls_session *cls_session)
2753{
2754 struct iscsi_session *sess;
2755 struct ddb_entry *ddb_entry;
2756 struct scsi_qla_host *ha;
2757
2758 sess = cls_session->dd_data;
2759 ddb_entry = sess->dd_data;
2760 ha = ddb_entry->ha;
2761 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
2762 " unblock session\n", ha->host_no, __func__,
2763 ddb_entry->fw_ddb_index);
2764
2765 iscsi_unblock_session(ddb_entry->sess);
2766
2767 /* Start scan target */
2768 if (test_bit(AF_ONLINE, &ha->flags)) {
2769 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
2770 " start scan\n", ha->host_no, __func__,
2771 ddb_entry->fw_ddb_index);
2772 scsi_queue_work(ha->host, &ddb_entry->sess->scan_work);
2773 }
2774 return QLA_SUCCESS;
2775}
2776
2777int qla4xxx_unblock_ddb(struct iscsi_cls_session *cls_session)
2778{
2779 struct iscsi_session *sess;
2780 struct ddb_entry *ddb_entry;
2781 struct scsi_qla_host *ha;
2782
2783 sess = cls_session->dd_data;
2784 ddb_entry = sess->dd_data;
2785 ha = ddb_entry->ha;
2786 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
2787 " unblock user space session\n", ha->host_no, __func__,
2788 ddb_entry->fw_ddb_index);
2789 iscsi_conn_start(ddb_entry->conn);
2790 iscsi_conn_login_event(ddb_entry->conn,
2791 ISCSI_CONN_STATE_LOGGED_IN);
2792
2793 return QLA_SUCCESS;
2794}
2795
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002796static void qla4xxx_relogin_all_devices(struct scsi_qla_host *ha)
2797{
2798 iscsi_host_for_each_session(ha->host, qla4xxx_relogin_devices);
2799}
2800
Mike Christie13483732011-12-01 21:38:41 -06002801static void qla4xxx_relogin_flash_ddb(struct iscsi_cls_session *cls_sess)
2802{
2803 uint16_t relogin_timer;
2804 struct iscsi_session *sess;
2805 struct ddb_entry *ddb_entry;
2806 struct scsi_qla_host *ha;
2807
2808 sess = cls_sess->dd_data;
2809 ddb_entry = sess->dd_data;
2810 ha = ddb_entry->ha;
2811
2812 relogin_timer = max(ddb_entry->default_relogin_timeout,
2813 (uint16_t)RELOGIN_TOV);
2814 atomic_set(&ddb_entry->relogin_timer, relogin_timer);
2815
2816 DEBUG2(ql4_printk(KERN_INFO, ha,
2817 "scsi%ld: Relogin index [%d]. TOV=%d\n", ha->host_no,
2818 ddb_entry->fw_ddb_index, relogin_timer));
2819
2820 qla4xxx_login_flash_ddb(cls_sess);
2821}
2822
2823static void qla4xxx_dpc_relogin(struct iscsi_cls_session *cls_sess)
2824{
2825 struct iscsi_session *sess;
2826 struct ddb_entry *ddb_entry;
2827 struct scsi_qla_host *ha;
2828
2829 sess = cls_sess->dd_data;
2830 ddb_entry = sess->dd_data;
2831 ha = ddb_entry->ha;
2832
2833 if (!(ddb_entry->ddb_type == FLASH_DDB))
2834 return;
2835
2836 if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
2837 !iscsi_is_session_online(cls_sess)) {
2838 DEBUG2(ql4_printk(KERN_INFO, ha,
2839 "relogin issued\n"));
2840 qla4xxx_relogin_flash_ddb(cls_sess);
2841 }
2842}
2843
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302844void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
2845{
Lalit Chandivade1b468072011-05-17 23:17:09 -07002846 if (ha->dpc_thread)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302847 queue_work(ha->dpc_thread, &ha->dpc_work);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302848}
2849
David Somayajuluafaf5a22006-09-19 10:28:00 -07002850/**
2851 * qla4xxx_do_dpc - dpc routine
2852 * @data: in our case pointer to adapter structure
2853 *
2854 * This routine is a task that is schedule by the interrupt handler
2855 * to perform the background processing for interrupts. We put it
2856 * on a task queue that is consumed whenever the scheduler runs; that's
2857 * so you can do anything (i.e. put the process to sleep etc). In fact,
2858 * the mid-level tries to sleep when it reaches the driver threshold
2859 * "host->can_queue". This can cause a panic if we were in our interrupt code.
2860 **/
David Howellsc4028952006-11-22 14:57:56 +00002861static void qla4xxx_do_dpc(struct work_struct *work)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002862{
David Howellsc4028952006-11-22 14:57:56 +00002863 struct scsi_qla_host *ha =
2864 container_of(work, struct scsi_qla_host, dpc_work);
David C Somayajulu477ffb92007-01-22 12:26:11 -08002865 int status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002866
David C Somayajuluf26b9042006-11-15 16:41:09 -08002867 DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302868 "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
2869 ha->host_no, __func__, ha->flags, ha->dpc_flags))
David Somayajuluafaf5a22006-09-19 10:28:00 -07002870
2871 /* Initialization not yet finished. Don't do anything yet. */
2872 if (!test_bit(AF_INIT_DONE, &ha->flags))
Lalit Chandivade1b468072011-05-17 23:17:09 -07002873 return;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002874
Lalit Chandivade2232be02010-07-30 14:38:47 +05302875 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
2876 DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
2877 ha->host_no, __func__, ha->flags));
Lalit Chandivade1b468072011-05-17 23:17:09 -07002878 return;
Lalit Chandivade2232be02010-07-30 14:38:47 +05302879 }
2880
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302881 if (is_qla8022(ha)) {
2882 if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
2883 qla4_8xxx_idc_lock(ha);
2884 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2885 QLA82XX_DEV_FAILED);
2886 qla4_8xxx_idc_unlock(ha);
2887 ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
2888 qla4_8xxx_device_state_handler(ha);
2889 }
2890 if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
2891 qla4_8xxx_need_qsnt_handler(ha);
2892 }
2893 }
2894
2895 if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
2896 (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07002897 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302898 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
2899 if (ql4xdontresethba) {
2900 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
2901 ha->host_no, __func__));
2902 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
2903 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
2904 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2905 goto dpc_post_reset_ha;
2906 }
2907 if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
2908 test_bit(DPC_RESET_HA, &ha->dpc_flags))
2909 qla4xxx_recover_adapter(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002910
David C Somayajulu477ffb92007-01-22 12:26:11 -08002911 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002912 uint8_t wait_time = RESET_INTR_TOV;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002913
David Somayajuluafaf5a22006-09-19 10:28:00 -07002914 while ((readw(&ha->reg->ctrl_status) &
2915 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
2916 if (--wait_time == 0)
2917 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002918 msleep(1000);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002919 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002920 if (wait_time == 0)
2921 DEBUG2(printk("scsi%ld: %s: SR|FSR "
2922 "bit not cleared-- resetting\n",
2923 ha->host_no, __func__));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302924 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
David C Somayajulu477ffb92007-01-22 12:26:11 -08002925 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
2926 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302927 status = qla4xxx_recover_adapter(ha);
David C Somayajulu477ffb92007-01-22 12:26:11 -08002928 }
2929 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
2930 if (status == QLA_SUCCESS)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302931 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002932 }
2933 }
2934
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302935dpc_post_reset_ha:
David Somayajuluafaf5a22006-09-19 10:28:00 -07002936 /* ---- process AEN? --- */
2937 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
2938 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
2939
2940 /* ---- Get DHCP IP Address? --- */
2941 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
2942 qla4xxx_get_dhcp_ip_address(ha);
2943
Mike Christie13483732011-12-01 21:38:41 -06002944 /* ---- relogin device? --- */
2945 if (adapter_up(ha) &&
2946 test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
2947 iscsi_host_for_each_session(ha->host, qla4xxx_dpc_relogin);
2948 }
2949
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05302950 /* ---- link change? --- */
2951 if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
2952 if (!test_bit(AF_LINK_UP, &ha->flags)) {
2953 /* ---- link down? --- */
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07002954 qla4xxx_mark_all_devices_missing(ha);
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05302955 } else {
2956 /* ---- link up? --- *
2957 * F/W will auto login to all devices ONLY ONCE after
2958 * link up during driver initialization and runtime
2959 * fatal error recovery. Therefore, the driver must
2960 * manually relogin to devices when recovering from
2961 * connection failures, logouts, expired KATO, etc. */
Mike Christie13483732011-12-01 21:38:41 -06002962 if (test_and_clear_bit(AF_BUILD_DDB_LIST, &ha->flags)) {
2963 qla4xxx_build_ddb_list(ha, ha->is_reset);
2964 iscsi_host_for_each_session(ha->host,
2965 qla4xxx_login_flash_ddb);
2966 } else
2967 qla4xxx_relogin_all_devices(ha);
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05302968 }
2969 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002970}
2971
2972/**
2973 * qla4xxx_free_adapter - release the adapter
2974 * @ha: pointer to adapter structure
2975 **/
2976static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
2977{
Sarang Radke8a288962011-12-06 02:34:10 -08002978 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002979
2980 if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
2981 /* Turn-off interrupts on the card. */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302982 ha->isp_ops->disable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002983 }
2984
David Somayajuluafaf5a22006-09-19 10:28:00 -07002985 /* Remove timer thread, if present */
2986 if (ha->timer_active)
2987 qla4xxx_stop_timer(ha);
2988
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302989 /* Kill the kernel thread for this host */
2990 if (ha->dpc_thread)
2991 destroy_workqueue(ha->dpc_thread);
2992
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002993 /* Kill the kernel thread for this host */
2994 if (ha->task_wq)
2995 destroy_workqueue(ha->task_wq);
2996
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302997 /* Put firmware in known state */
2998 ha->isp_ops->reset_firmware(ha);
2999
3000 if (is_qla8022(ha)) {
3001 qla4_8xxx_idc_lock(ha);
3002 qla4_8xxx_clear_drv_active(ha);
3003 qla4_8xxx_idc_unlock(ha);
3004 }
3005
David Somayajuluafaf5a22006-09-19 10:28:00 -07003006 /* Detach interrupts */
3007 if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303008 qla4xxx_free_irqs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003009
David C Somayajulubee4fe82007-05-23 18:03:32 -07003010 /* free extra memory */
3011 qla4xxx_mem_free(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303012}
David C Somayajulubee4fe82007-05-23 18:03:32 -07003013
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303014int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
3015{
3016 int status = 0;
3017 uint8_t revision_id;
3018 unsigned long mem_base, mem_len, db_base, db_len;
3019 struct pci_dev *pdev = ha->pdev;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003020
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303021 status = pci_request_regions(pdev, DRIVER_NAME);
3022 if (status) {
3023 printk(KERN_WARNING
3024 "scsi(%ld) Failed to reserve PIO regions (%s) "
3025 "status=%d\n", ha->host_no, pci_name(pdev), status);
3026 goto iospace_error_exit;
3027 }
3028
3029 pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
3030 DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
3031 __func__, revision_id));
3032 ha->revision_id = revision_id;
3033
3034 /* remap phys address */
3035 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
3036 mem_len = pci_resource_len(pdev, 0);
3037 DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
3038 __func__, mem_base, mem_len));
3039
3040 /* mapping of pcibase pointer */
3041 ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
3042 if (!ha->nx_pcibase) {
3043 printk(KERN_ERR
3044 "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
3045 pci_release_regions(ha->pdev);
3046 goto iospace_error_exit;
3047 }
3048
3049 /* Mapping of IO base pointer, door bell read and write pointer */
3050
3051 /* mapping of IO base pointer */
3052 ha->qla4_8xxx_reg =
3053 (struct device_reg_82xx __iomem *)((uint8_t *)ha->nx_pcibase +
3054 0xbc000 + (ha->pdev->devfn << 11));
3055
3056 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
3057 db_len = pci_resource_len(pdev, 4);
3058
Shyam Sundar2657c802010-10-06 22:50:29 -07003059 ha->nx_db_wr_ptr = (ha->pdev->devfn == 4 ? QLA82XX_CAM_RAM_DB1 :
3060 QLA82XX_CAM_RAM_DB2);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303061
Shyam Sundar2657c802010-10-06 22:50:29 -07003062 return 0;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303063iospace_error_exit:
3064 return -ENOMEM;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003065}
3066
3067/***
3068 * qla4xxx_iospace_config - maps registers
3069 * @ha: pointer to adapter structure
3070 *
3071 * This routines maps HBA's registers from the pci address space
3072 * into the kernel virtual address space for memory mapped i/o.
3073 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303074int qla4xxx_iospace_config(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07003075{
3076 unsigned long pio, pio_len, pio_flags;
3077 unsigned long mmio, mmio_len, mmio_flags;
3078
3079 pio = pci_resource_start(ha->pdev, 0);
3080 pio_len = pci_resource_len(ha->pdev, 0);
3081 pio_flags = pci_resource_flags(ha->pdev, 0);
3082 if (pio_flags & IORESOURCE_IO) {
3083 if (pio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303084 ql4_printk(KERN_WARNING, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07003085 "Invalid PCI I/O region size\n");
3086 pio = 0;
3087 }
3088 } else {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303089 ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07003090 pio = 0;
3091 }
3092
3093 /* Use MMIO operations for all accesses. */
3094 mmio = pci_resource_start(ha->pdev, 1);
3095 mmio_len = pci_resource_len(ha->pdev, 1);
3096 mmio_flags = pci_resource_flags(ha->pdev, 1);
3097
3098 if (!(mmio_flags & IORESOURCE_MEM)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303099 ql4_printk(KERN_ERR, ha,
3100 "region #0 not an MMIO resource, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07003101
3102 goto iospace_error_exit;
3103 }
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303104
David Somayajuluafaf5a22006-09-19 10:28:00 -07003105 if (mmio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303106 ql4_printk(KERN_ERR, ha,
3107 "Invalid PCI mem region size, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07003108 goto iospace_error_exit;
3109 }
3110
3111 if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303112 ql4_printk(KERN_WARNING, ha,
3113 "Failed to reserve PIO/MMIO regions\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07003114
3115 goto iospace_error_exit;
3116 }
3117
3118 ha->pio_address = pio;
3119 ha->pio_length = pio_len;
3120 ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
3121 if (!ha->reg) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303122 ql4_printk(KERN_ERR, ha,
3123 "cannot remap MMIO, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07003124
3125 goto iospace_error_exit;
3126 }
3127
3128 return 0;
3129
3130iospace_error_exit:
3131 return -ENOMEM;
3132}
3133
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303134static struct isp_operations qla4xxx_isp_ops = {
3135 .iospace_config = qla4xxx_iospace_config,
3136 .pci_config = qla4xxx_pci_config,
3137 .disable_intrs = qla4xxx_disable_intrs,
3138 .enable_intrs = qla4xxx_enable_intrs,
3139 .start_firmware = qla4xxx_start_firmware,
3140 .intr_handler = qla4xxx_intr_handler,
3141 .interrupt_service_routine = qla4xxx_interrupt_service_routine,
3142 .reset_chip = qla4xxx_soft_reset,
3143 .reset_firmware = qla4xxx_hw_reset,
3144 .queue_iocb = qla4xxx_queue_iocb,
3145 .complete_iocb = qla4xxx_complete_iocb,
3146 .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
3147 .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
3148 .get_sys_info = qla4xxx_get_sys_info,
3149};
3150
3151static struct isp_operations qla4_8xxx_isp_ops = {
3152 .iospace_config = qla4_8xxx_iospace_config,
3153 .pci_config = qla4_8xxx_pci_config,
3154 .disable_intrs = qla4_8xxx_disable_intrs,
3155 .enable_intrs = qla4_8xxx_enable_intrs,
3156 .start_firmware = qla4_8xxx_load_risc,
3157 .intr_handler = qla4_8xxx_intr_handler,
3158 .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
3159 .reset_chip = qla4_8xxx_isp_reset,
3160 .reset_firmware = qla4_8xxx_stop_firmware,
3161 .queue_iocb = qla4_8xxx_queue_iocb,
3162 .complete_iocb = qla4_8xxx_complete_iocb,
3163 .rd_shdw_req_q_out = qla4_8xxx_rd_shdw_req_q_out,
3164 .rd_shdw_rsp_q_in = qla4_8xxx_rd_shdw_rsp_q_in,
3165 .get_sys_info = qla4_8xxx_get_sys_info,
3166};
3167
3168uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
3169{
3170 return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
3171}
3172
3173uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
3174{
3175 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
3176}
3177
3178uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
3179{
3180 return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
3181}
3182
3183uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
3184{
3185 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
3186}
3187
Manish Rangankar2a991c22011-07-25 13:48:55 -05003188static ssize_t qla4xxx_show_boot_eth_info(void *data, int type, char *buf)
3189{
3190 struct scsi_qla_host *ha = data;
3191 char *str = buf;
3192 int rc;
3193
3194 switch (type) {
3195 case ISCSI_BOOT_ETH_FLAGS:
3196 rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
3197 break;
3198 case ISCSI_BOOT_ETH_INDEX:
3199 rc = sprintf(str, "0\n");
3200 break;
3201 case ISCSI_BOOT_ETH_MAC:
3202 rc = sysfs_format_mac(str, ha->my_mac,
3203 MAC_ADDR_LEN);
3204 break;
3205 default:
3206 rc = -ENOSYS;
3207 break;
3208 }
3209 return rc;
3210}
3211
Al Viro587a1f12011-07-23 23:11:19 -04003212static umode_t qla4xxx_eth_get_attr_visibility(void *data, int type)
Manish Rangankar2a991c22011-07-25 13:48:55 -05003213{
3214 int rc;
3215
3216 switch (type) {
3217 case ISCSI_BOOT_ETH_FLAGS:
3218 case ISCSI_BOOT_ETH_MAC:
3219 case ISCSI_BOOT_ETH_INDEX:
3220 rc = S_IRUGO;
3221 break;
3222 default:
3223 rc = 0;
3224 break;
3225 }
3226 return rc;
3227}
3228
3229static ssize_t qla4xxx_show_boot_ini_info(void *data, int type, char *buf)
3230{
3231 struct scsi_qla_host *ha = data;
3232 char *str = buf;
3233 int rc;
3234
3235 switch (type) {
3236 case ISCSI_BOOT_INI_INITIATOR_NAME:
3237 rc = sprintf(str, "%s\n", ha->name_string);
3238 break;
3239 default:
3240 rc = -ENOSYS;
3241 break;
3242 }
3243 return rc;
3244}
3245
Al Viro587a1f12011-07-23 23:11:19 -04003246static umode_t qla4xxx_ini_get_attr_visibility(void *data, int type)
Manish Rangankar2a991c22011-07-25 13:48:55 -05003247{
3248 int rc;
3249
3250 switch (type) {
3251 case ISCSI_BOOT_INI_INITIATOR_NAME:
3252 rc = S_IRUGO;
3253 break;
3254 default:
3255 rc = 0;
3256 break;
3257 }
3258 return rc;
3259}
3260
3261static ssize_t
3262qla4xxx_show_boot_tgt_info(struct ql4_boot_session_info *boot_sess, int type,
3263 char *buf)
3264{
3265 struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
3266 char *str = buf;
3267 int rc;
3268
3269 switch (type) {
3270 case ISCSI_BOOT_TGT_NAME:
3271 rc = sprintf(buf, "%s\n", (char *)&boot_sess->target_name);
3272 break;
3273 case ISCSI_BOOT_TGT_IP_ADDR:
3274 if (boot_sess->conn_list[0].dest_ipaddr.ip_type == 0x1)
3275 rc = sprintf(buf, "%pI4\n",
3276 &boot_conn->dest_ipaddr.ip_address);
3277 else
3278 rc = sprintf(str, "%pI6\n",
3279 &boot_conn->dest_ipaddr.ip_address);
3280 break;
3281 case ISCSI_BOOT_TGT_PORT:
3282 rc = sprintf(str, "%d\n", boot_conn->dest_port);
3283 break;
3284 case ISCSI_BOOT_TGT_CHAP_NAME:
3285 rc = sprintf(str, "%.*s\n",
3286 boot_conn->chap.target_chap_name_length,
3287 (char *)&boot_conn->chap.target_chap_name);
3288 break;
3289 case ISCSI_BOOT_TGT_CHAP_SECRET:
3290 rc = sprintf(str, "%.*s\n",
3291 boot_conn->chap.target_secret_length,
3292 (char *)&boot_conn->chap.target_secret);
3293 break;
3294 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
3295 rc = sprintf(str, "%.*s\n",
3296 boot_conn->chap.intr_chap_name_length,
3297 (char *)&boot_conn->chap.intr_chap_name);
3298 break;
3299 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
3300 rc = sprintf(str, "%.*s\n",
3301 boot_conn->chap.intr_secret_length,
3302 (char *)&boot_conn->chap.intr_secret);
3303 break;
3304 case ISCSI_BOOT_TGT_FLAGS:
3305 rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
3306 break;
3307 case ISCSI_BOOT_TGT_NIC_ASSOC:
3308 rc = sprintf(str, "0\n");
3309 break;
3310 default:
3311 rc = -ENOSYS;
3312 break;
3313 }
3314 return rc;
3315}
3316
3317static ssize_t qla4xxx_show_boot_tgt_pri_info(void *data, int type, char *buf)
3318{
3319 struct scsi_qla_host *ha = data;
3320 struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_pri_sess);
3321
3322 return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
3323}
3324
3325static ssize_t qla4xxx_show_boot_tgt_sec_info(void *data, int type, char *buf)
3326{
3327 struct scsi_qla_host *ha = data;
3328 struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_sec_sess);
3329
3330 return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
3331}
3332
Al Viro587a1f12011-07-23 23:11:19 -04003333static umode_t qla4xxx_tgt_get_attr_visibility(void *data, int type)
Manish Rangankar2a991c22011-07-25 13:48:55 -05003334{
3335 int rc;
3336
3337 switch (type) {
3338 case ISCSI_BOOT_TGT_NAME:
3339 case ISCSI_BOOT_TGT_IP_ADDR:
3340 case ISCSI_BOOT_TGT_PORT:
3341 case ISCSI_BOOT_TGT_CHAP_NAME:
3342 case ISCSI_BOOT_TGT_CHAP_SECRET:
3343 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
3344 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
3345 case ISCSI_BOOT_TGT_NIC_ASSOC:
3346 case ISCSI_BOOT_TGT_FLAGS:
3347 rc = S_IRUGO;
3348 break;
3349 default:
3350 rc = 0;
3351 break;
3352 }
3353 return rc;
3354}
3355
3356static void qla4xxx_boot_release(void *data)
3357{
3358 struct scsi_qla_host *ha = data;
3359
3360 scsi_host_put(ha->host);
3361}
3362
3363static int get_fw_boot_info(struct scsi_qla_host *ha, uint16_t ddb_index[])
3364{
3365 dma_addr_t buf_dma;
3366 uint32_t addr, pri_addr, sec_addr;
3367 uint32_t offset;
3368 uint16_t func_num;
3369 uint8_t val;
3370 uint8_t *buf = NULL;
3371 size_t size = 13 * sizeof(uint8_t);
3372 int ret = QLA_SUCCESS;
3373
3374 func_num = PCI_FUNC(ha->pdev->devfn);
3375
Manish Rangankar0d5b36b2011-10-07 16:55:51 -07003376 ql4_printk(KERN_INFO, ha, "%s: Get FW boot info for 0x%x func %d\n",
3377 __func__, ha->pdev->device, func_num);
Manish Rangankar2a991c22011-07-25 13:48:55 -05003378
Manish Rangankar0d5b36b2011-10-07 16:55:51 -07003379 if (is_qla40XX(ha)) {
Manish Rangankar2a991c22011-07-25 13:48:55 -05003380 if (func_num == 1) {
3381 addr = NVRAM_PORT0_BOOT_MODE;
3382 pri_addr = NVRAM_PORT0_BOOT_PRI_TGT;
3383 sec_addr = NVRAM_PORT0_BOOT_SEC_TGT;
3384 } else if (func_num == 3) {
3385 addr = NVRAM_PORT1_BOOT_MODE;
3386 pri_addr = NVRAM_PORT1_BOOT_PRI_TGT;
3387 sec_addr = NVRAM_PORT1_BOOT_SEC_TGT;
3388 } else {
3389 ret = QLA_ERROR;
3390 goto exit_boot_info;
3391 }
3392
3393 /* Check Boot Mode */
3394 val = rd_nvram_byte(ha, addr);
3395 if (!(val & 0x07)) {
3396 DEBUG2(ql4_printk(KERN_ERR, ha,
3397 "%s: Failed Boot options : 0x%x\n",
3398 __func__, val));
3399 ret = QLA_ERROR;
3400 goto exit_boot_info;
3401 }
3402
3403 /* get primary valid target index */
3404 val = rd_nvram_byte(ha, pri_addr);
3405 if (val & BIT_7)
3406 ddb_index[0] = (val & 0x7f);
Manish Rangankar2a991c22011-07-25 13:48:55 -05003407
3408 /* get secondary valid target index */
3409 val = rd_nvram_byte(ha, sec_addr);
3410 if (val & BIT_7)
3411 ddb_index[1] = (val & 0x7f);
Manish Rangankar2a991c22011-07-25 13:48:55 -05003412
3413 } else if (is_qla8022(ha)) {
3414 buf = dma_alloc_coherent(&ha->pdev->dev, size,
3415 &buf_dma, GFP_KERNEL);
3416 if (!buf) {
3417 DEBUG2(ql4_printk(KERN_ERR, ha,
3418 "%s: Unable to allocate dma buffer\n",
3419 __func__));
3420 ret = QLA_ERROR;
3421 goto exit_boot_info;
3422 }
3423
3424 if (ha->port_num == 0)
3425 offset = BOOT_PARAM_OFFSET_PORT0;
3426 else if (ha->port_num == 1)
3427 offset = BOOT_PARAM_OFFSET_PORT1;
3428 else {
3429 ret = QLA_ERROR;
3430 goto exit_boot_info_free;
3431 }
3432 addr = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_iscsi_param * 4) +
3433 offset;
3434 if (qla4xxx_get_flash(ha, buf_dma, addr,
3435 13 * sizeof(uint8_t)) != QLA_SUCCESS) {
3436 DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: Get Flash"
3437 "failed\n", ha->host_no, __func__));
3438 ret = QLA_ERROR;
3439 goto exit_boot_info_free;
3440 }
3441 /* Check Boot Mode */
3442 if (!(buf[1] & 0x07)) {
3443 DEBUG2(ql4_printk(KERN_INFO, ha,
3444 "Failed: Boot options : 0x%x\n",
3445 buf[1]));
3446 ret = QLA_ERROR;
3447 goto exit_boot_info_free;
3448 }
3449
3450 /* get primary valid target index */
3451 if (buf[2] & BIT_7)
3452 ddb_index[0] = buf[2] & 0x7f;
Manish Rangankar2a991c22011-07-25 13:48:55 -05003453
3454 /* get secondary valid target index */
3455 if (buf[11] & BIT_7)
3456 ddb_index[1] = buf[11] & 0x7f;
Manish Rangankar2a991c22011-07-25 13:48:55 -05003457 } else {
3458 ret = QLA_ERROR;
3459 goto exit_boot_info;
3460 }
3461
3462 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Primary target ID %d, Secondary"
3463 " target ID %d\n", __func__, ddb_index[0],
3464 ddb_index[1]));
3465
Mike Christie13483732011-12-01 21:38:41 -06003466 ha->pri_ddb_idx = ddb_index[0];
3467 ha->sec_ddb_idx = ddb_index[1];
3468
Manish Rangankar2a991c22011-07-25 13:48:55 -05003469exit_boot_info_free:
3470 dma_free_coherent(&ha->pdev->dev, size, buf, buf_dma);
3471exit_boot_info:
3472 return ret;
3473}
3474
Lalit Chandivade28deb452011-10-07 16:55:44 -07003475/**
3476 * qla4xxx_get_bidi_chap - Get a BIDI CHAP user and password
3477 * @ha: pointer to adapter structure
3478 * @username: CHAP username to be returned
3479 * @password: CHAP password to be returned
3480 *
3481 * If a boot entry has BIDI CHAP enabled then we need to set the BIDI CHAP
3482 * user and password in the sysfs entry in /sys/firmware/iscsi_boot#/.
3483 * So from the CHAP cache find the first BIDI CHAP entry and set it
3484 * to the boot record in sysfs.
3485 **/
3486static int qla4xxx_get_bidi_chap(struct scsi_qla_host *ha, char *username,
3487 char *password)
3488{
3489 int i, ret = -EINVAL;
3490 int max_chap_entries = 0;
3491 struct ql4_chap_table *chap_table;
3492
3493 if (is_qla8022(ha))
3494 max_chap_entries = (ha->hw.flt_chap_size / 2) /
3495 sizeof(struct ql4_chap_table);
3496 else
3497 max_chap_entries = MAX_CHAP_ENTRIES_40XX;
3498
3499 if (!ha->chap_list) {
3500 ql4_printk(KERN_ERR, ha, "Do not have CHAP table cache\n");
3501 return ret;
3502 }
3503
3504 mutex_lock(&ha->chap_sem);
3505 for (i = 0; i < max_chap_entries; i++) {
3506 chap_table = (struct ql4_chap_table *)ha->chap_list + i;
3507 if (chap_table->cookie !=
3508 __constant_cpu_to_le16(CHAP_VALID_COOKIE)) {
3509 continue;
3510 }
3511
3512 if (chap_table->flags & BIT_7) /* local */
3513 continue;
3514
3515 if (!(chap_table->flags & BIT_6)) /* Not BIDI */
3516 continue;
3517
3518 strncpy(password, chap_table->secret, QL4_CHAP_MAX_SECRET_LEN);
3519 strncpy(username, chap_table->name, QL4_CHAP_MAX_NAME_LEN);
3520 ret = 0;
3521 break;
3522 }
3523 mutex_unlock(&ha->chap_sem);
3524
3525 return ret;
3526}
3527
3528
Manish Rangankar2a991c22011-07-25 13:48:55 -05003529static int qla4xxx_get_boot_target(struct scsi_qla_host *ha,
3530 struct ql4_boot_session_info *boot_sess,
3531 uint16_t ddb_index)
3532{
3533 struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
3534 struct dev_db_entry *fw_ddb_entry;
3535 dma_addr_t fw_ddb_entry_dma;
3536 uint16_t idx;
3537 uint16_t options;
3538 int ret = QLA_SUCCESS;
3539
3540 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3541 &fw_ddb_entry_dma, GFP_KERNEL);
3542 if (!fw_ddb_entry) {
3543 DEBUG2(ql4_printk(KERN_ERR, ha,
3544 "%s: Unable to allocate dma buffer.\n",
3545 __func__));
3546 ret = QLA_ERROR;
3547 return ret;
3548 }
3549
3550 if (qla4xxx_bootdb_by_index(ha, fw_ddb_entry,
3551 fw_ddb_entry_dma, ddb_index)) {
3552 DEBUG2(ql4_printk(KERN_ERR, ha,
3553 "%s: Flash DDB read Failed\n", __func__));
3554 ret = QLA_ERROR;
3555 goto exit_boot_target;
3556 }
3557
3558 /* Update target name and IP from DDB */
3559 memcpy(boot_sess->target_name, fw_ddb_entry->iscsi_name,
3560 min(sizeof(boot_sess->target_name),
3561 sizeof(fw_ddb_entry->iscsi_name)));
3562
3563 options = le16_to_cpu(fw_ddb_entry->options);
3564 if (options & DDB_OPT_IPV6_DEVICE) {
3565 memcpy(&boot_conn->dest_ipaddr.ip_address,
3566 &fw_ddb_entry->ip_addr[0], IPv6_ADDR_LEN);
3567 } else {
3568 boot_conn->dest_ipaddr.ip_type = 0x1;
3569 memcpy(&boot_conn->dest_ipaddr.ip_address,
3570 &fw_ddb_entry->ip_addr[0], IP_ADDR_LEN);
3571 }
3572
3573 boot_conn->dest_port = le16_to_cpu(fw_ddb_entry->port);
3574
3575 /* update chap information */
3576 idx = __le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
3577
3578 if (BIT_7 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
3579
3580 DEBUG2(ql4_printk(KERN_INFO, ha, "Setting chap\n"));
3581
3582 ret = qla4xxx_get_chap(ha, (char *)&boot_conn->chap.
3583 target_chap_name,
3584 (char *)&boot_conn->chap.target_secret,
3585 idx);
3586 if (ret) {
3587 ql4_printk(KERN_ERR, ha, "Failed to set chap\n");
3588 ret = QLA_ERROR;
3589 goto exit_boot_target;
3590 }
3591
3592 boot_conn->chap.target_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
3593 boot_conn->chap.target_secret_length = QL4_CHAP_MAX_SECRET_LEN;
3594 }
3595
3596 if (BIT_4 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
3597
3598 DEBUG2(ql4_printk(KERN_INFO, ha, "Setting BIDI chap\n"));
3599
Lalit Chandivade28deb452011-10-07 16:55:44 -07003600 ret = qla4xxx_get_bidi_chap(ha,
3601 (char *)&boot_conn->chap.intr_chap_name,
3602 (char *)&boot_conn->chap.intr_secret);
3603
Manish Rangankar2a991c22011-07-25 13:48:55 -05003604 if (ret) {
3605 ql4_printk(KERN_ERR, ha, "Failed to set BIDI chap\n");
3606 ret = QLA_ERROR;
3607 goto exit_boot_target;
3608 }
3609
3610 boot_conn->chap.intr_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
3611 boot_conn->chap.intr_secret_length = QL4_CHAP_MAX_SECRET_LEN;
3612 }
3613
3614exit_boot_target:
3615 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3616 fw_ddb_entry, fw_ddb_entry_dma);
3617 return ret;
3618}
3619
3620static int qla4xxx_get_boot_info(struct scsi_qla_host *ha)
3621{
3622 uint16_t ddb_index[2];
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003623 int ret = QLA_ERROR;
3624 int rval;
Manish Rangankar2a991c22011-07-25 13:48:55 -05003625
3626 memset(ddb_index, 0, sizeof(ddb_index));
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003627 ddb_index[0] = 0xffff;
3628 ddb_index[1] = 0xffff;
Manish Rangankar2a991c22011-07-25 13:48:55 -05003629 ret = get_fw_boot_info(ha, ddb_index);
3630 if (ret != QLA_SUCCESS) {
3631 DEBUG2(ql4_printk(KERN_ERR, ha,
3632 "%s: Failed to set boot info.\n", __func__));
3633 return ret;
3634 }
3635
Mike Christie13483732011-12-01 21:38:41 -06003636 if (ql4xdisablesysfsboot)
3637 return QLA_SUCCESS;
3638
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003639 if (ddb_index[0] == 0xffff)
3640 goto sec_target;
3641
3642 rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_pri_sess),
Manish Rangankar2a991c22011-07-25 13:48:55 -05003643 ddb_index[0]);
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003644 if (rval != QLA_SUCCESS) {
Manish Rangankar2a991c22011-07-25 13:48:55 -05003645 DEBUG2(ql4_printk(KERN_ERR, ha, "%s: Failed to get "
3646 "primary target\n", __func__));
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003647 } else
3648 ret = QLA_SUCCESS;
Manish Rangankar2a991c22011-07-25 13:48:55 -05003649
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003650sec_target:
3651 if (ddb_index[1] == 0xffff)
3652 goto exit_get_boot_info;
3653
3654 rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_sec_sess),
Manish Rangankar2a991c22011-07-25 13:48:55 -05003655 ddb_index[1]);
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003656 if (rval != QLA_SUCCESS) {
Manish Rangankar2a991c22011-07-25 13:48:55 -05003657 DEBUG2(ql4_printk(KERN_ERR, ha, "%s: Failed to get "
3658 "secondary target\n", __func__));
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003659 } else
3660 ret = QLA_SUCCESS;
3661
3662exit_get_boot_info:
Manish Rangankar2a991c22011-07-25 13:48:55 -05003663 return ret;
3664}
3665
3666static int qla4xxx_setup_boot_info(struct scsi_qla_host *ha)
3667{
3668 struct iscsi_boot_kobj *boot_kobj;
3669
3670 if (qla4xxx_get_boot_info(ha) != QLA_SUCCESS)
Mike Christie13483732011-12-01 21:38:41 -06003671 return QLA_ERROR;
3672
3673 if (ql4xdisablesysfsboot) {
3674 ql4_printk(KERN_INFO, ha,
3675 "%s: syfsboot disabled - driver will trigger login"
3676 "and publish session for discovery .\n", __func__);
3677 return QLA_SUCCESS;
3678 }
3679
Manish Rangankar2a991c22011-07-25 13:48:55 -05003680
3681 ha->boot_kset = iscsi_boot_create_host_kset(ha->host->host_no);
3682 if (!ha->boot_kset)
3683 goto kset_free;
3684
3685 if (!scsi_host_get(ha->host))
3686 goto kset_free;
3687 boot_kobj = iscsi_boot_create_target(ha->boot_kset, 0, ha,
3688 qla4xxx_show_boot_tgt_pri_info,
3689 qla4xxx_tgt_get_attr_visibility,
3690 qla4xxx_boot_release);
3691 if (!boot_kobj)
3692 goto put_host;
3693
3694 if (!scsi_host_get(ha->host))
3695 goto kset_free;
3696 boot_kobj = iscsi_boot_create_target(ha->boot_kset, 1, ha,
3697 qla4xxx_show_boot_tgt_sec_info,
3698 qla4xxx_tgt_get_attr_visibility,
3699 qla4xxx_boot_release);
3700 if (!boot_kobj)
3701 goto put_host;
3702
3703 if (!scsi_host_get(ha->host))
3704 goto kset_free;
3705 boot_kobj = iscsi_boot_create_initiator(ha->boot_kset, 0, ha,
3706 qla4xxx_show_boot_ini_info,
3707 qla4xxx_ini_get_attr_visibility,
3708 qla4xxx_boot_release);
3709 if (!boot_kobj)
3710 goto put_host;
3711
3712 if (!scsi_host_get(ha->host))
3713 goto kset_free;
3714 boot_kobj = iscsi_boot_create_ethernet(ha->boot_kset, 0, ha,
3715 qla4xxx_show_boot_eth_info,
3716 qla4xxx_eth_get_attr_visibility,
3717 qla4xxx_boot_release);
3718 if (!boot_kobj)
3719 goto put_host;
3720
Mike Christie13483732011-12-01 21:38:41 -06003721 return QLA_SUCCESS;
Manish Rangankar2a991c22011-07-25 13:48:55 -05003722
3723put_host:
3724 scsi_host_put(ha->host);
3725kset_free:
3726 iscsi_boot_destroy_kset(ha->boot_kset);
3727 return -ENOMEM;
3728}
3729
Lalit Chandivade45494152011-10-07 16:55:42 -07003730
3731/**
3732 * qla4xxx_create chap_list - Create CHAP list from FLASH
3733 * @ha: pointer to adapter structure
3734 *
3735 * Read flash and make a list of CHAP entries, during login when a CHAP entry
3736 * is received, it will be checked in this list. If entry exist then the CHAP
3737 * entry index is set in the DDB. If CHAP entry does not exist in this list
3738 * then a new entry is added in FLASH in CHAP table and the index obtained is
3739 * used in the DDB.
3740 **/
3741static void qla4xxx_create_chap_list(struct scsi_qla_host *ha)
3742{
3743 int rval = 0;
3744 uint8_t *chap_flash_data = NULL;
3745 uint32_t offset;
3746 dma_addr_t chap_dma;
3747 uint32_t chap_size = 0;
3748
3749 if (is_qla40XX(ha))
3750 chap_size = MAX_CHAP_ENTRIES_40XX *
3751 sizeof(struct ql4_chap_table);
3752 else /* Single region contains CHAP info for both
3753 * ports which is divided into half for each port.
3754 */
3755 chap_size = ha->hw.flt_chap_size / 2;
3756
3757 chap_flash_data = dma_alloc_coherent(&ha->pdev->dev, chap_size,
3758 &chap_dma, GFP_KERNEL);
3759 if (!chap_flash_data) {
3760 ql4_printk(KERN_ERR, ha, "No memory for chap_flash_data\n");
3761 return;
3762 }
3763 if (is_qla40XX(ha))
3764 offset = FLASH_CHAP_OFFSET;
3765 else {
3766 offset = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_region_chap << 2);
3767 if (ha->port_num == 1)
3768 offset += chap_size;
3769 }
3770
3771 rval = qla4xxx_get_flash(ha, chap_dma, offset, chap_size);
3772 if (rval != QLA_SUCCESS)
3773 goto exit_chap_list;
3774
3775 if (ha->chap_list == NULL)
3776 ha->chap_list = vmalloc(chap_size);
3777 if (ha->chap_list == NULL) {
3778 ql4_printk(KERN_ERR, ha, "No memory for ha->chap_list\n");
3779 goto exit_chap_list;
3780 }
3781
3782 memcpy(ha->chap_list, chap_flash_data, chap_size);
3783
3784exit_chap_list:
3785 dma_free_coherent(&ha->pdev->dev, chap_size,
3786 chap_flash_data, chap_dma);
Lalit Chandivade45494152011-10-07 16:55:42 -07003787}
3788
Mike Christie13483732011-12-01 21:38:41 -06003789static void qla4xxx_get_param_ddb(struct ddb_entry *ddb_entry,
3790 struct ql4_tuple_ddb *tddb)
3791{
3792 struct scsi_qla_host *ha;
3793 struct iscsi_cls_session *cls_sess;
3794 struct iscsi_cls_conn *cls_conn;
3795 struct iscsi_session *sess;
3796 struct iscsi_conn *conn;
3797
3798 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
3799 ha = ddb_entry->ha;
3800 cls_sess = ddb_entry->sess;
3801 sess = cls_sess->dd_data;
3802 cls_conn = ddb_entry->conn;
3803 conn = cls_conn->dd_data;
3804
3805 tddb->tpgt = sess->tpgt;
3806 tddb->port = conn->persistent_port;
3807 strncpy(tddb->iscsi_name, sess->targetname, ISCSI_NAME_SIZE);
3808 strncpy(tddb->ip_addr, conn->persistent_address, DDB_IPADDR_LEN);
3809}
3810
3811static void qla4xxx_convert_param_ddb(struct dev_db_entry *fw_ddb_entry,
3812 struct ql4_tuple_ddb *tddb)
3813{
3814 uint16_t options = 0;
3815
3816 tddb->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
3817 memcpy(&tddb->iscsi_name[0], &fw_ddb_entry->iscsi_name[0],
3818 min(sizeof(tddb->iscsi_name), sizeof(fw_ddb_entry->iscsi_name)));
3819
3820 options = le16_to_cpu(fw_ddb_entry->options);
3821 if (options & DDB_OPT_IPV6_DEVICE)
3822 sprintf(tddb->ip_addr, "%pI6", fw_ddb_entry->ip_addr);
3823 else
3824 sprintf(tddb->ip_addr, "%pI4", fw_ddb_entry->ip_addr);
3825
3826 tddb->port = le16_to_cpu(fw_ddb_entry->port);
3827}
3828
3829static int qla4xxx_compare_tuple_ddb(struct scsi_qla_host *ha,
3830 struct ql4_tuple_ddb *old_tddb,
3831 struct ql4_tuple_ddb *new_tddb)
3832{
3833 if (strcmp(old_tddb->iscsi_name, new_tddb->iscsi_name))
3834 return QLA_ERROR;
3835
3836 if (strcmp(old_tddb->ip_addr, new_tddb->ip_addr))
3837 return QLA_ERROR;
3838
3839 if (old_tddb->port != new_tddb->port)
3840 return QLA_ERROR;
3841
3842 DEBUG2(ql4_printk(KERN_INFO, ha,
3843 "Match Found, fw[%d,%d,%s,%s], [%d,%d,%s,%s]",
3844 old_tddb->port, old_tddb->tpgt, old_tddb->ip_addr,
3845 old_tddb->iscsi_name, new_tddb->port, new_tddb->tpgt,
3846 new_tddb->ip_addr, new_tddb->iscsi_name));
3847
3848 return QLA_SUCCESS;
3849}
3850
3851static int qla4xxx_is_session_exists(struct scsi_qla_host *ha,
3852 struct dev_db_entry *fw_ddb_entry)
3853{
3854 struct ddb_entry *ddb_entry;
3855 struct ql4_tuple_ddb *fw_tddb = NULL;
3856 struct ql4_tuple_ddb *tmp_tddb = NULL;
3857 int idx;
3858 int ret = QLA_ERROR;
3859
3860 fw_tddb = vzalloc(sizeof(*fw_tddb));
3861 if (!fw_tddb) {
3862 DEBUG2(ql4_printk(KERN_WARNING, ha,
3863 "Memory Allocation failed.\n"));
3864 ret = QLA_SUCCESS;
3865 goto exit_check;
3866 }
3867
3868 tmp_tddb = vzalloc(sizeof(*tmp_tddb));
3869 if (!tmp_tddb) {
3870 DEBUG2(ql4_printk(KERN_WARNING, ha,
3871 "Memory Allocation failed.\n"));
3872 ret = QLA_SUCCESS;
3873 goto exit_check;
3874 }
3875
3876 qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb);
3877
3878 for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
3879 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
3880 if (ddb_entry == NULL)
3881 continue;
3882
3883 qla4xxx_get_param_ddb(ddb_entry, tmp_tddb);
3884 if (!qla4xxx_compare_tuple_ddb(ha, fw_tddb, tmp_tddb)) {
3885 ret = QLA_SUCCESS; /* found */
3886 goto exit_check;
3887 }
3888 }
3889
3890exit_check:
3891 if (fw_tddb)
3892 vfree(fw_tddb);
3893 if (tmp_tddb)
3894 vfree(tmp_tddb);
3895 return ret;
3896}
3897
3898static int qla4xxx_is_flash_ddb_exists(struct scsi_qla_host *ha,
3899 struct list_head *list_nt,
3900 struct dev_db_entry *fw_ddb_entry)
3901{
3902 struct qla_ddb_index *nt_ddb_idx, *nt_ddb_idx_tmp;
3903 struct ql4_tuple_ddb *fw_tddb = NULL;
3904 struct ql4_tuple_ddb *tmp_tddb = NULL;
3905 int ret = QLA_ERROR;
3906
3907 fw_tddb = vzalloc(sizeof(*fw_tddb));
3908 if (!fw_tddb) {
3909 DEBUG2(ql4_printk(KERN_WARNING, ha,
3910 "Memory Allocation failed.\n"));
3911 ret = QLA_SUCCESS;
3912 goto exit_check;
3913 }
3914
3915 tmp_tddb = vzalloc(sizeof(*tmp_tddb));
3916 if (!tmp_tddb) {
3917 DEBUG2(ql4_printk(KERN_WARNING, ha,
3918 "Memory Allocation failed.\n"));
3919 ret = QLA_SUCCESS;
3920 goto exit_check;
3921 }
3922
3923 qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb);
3924
3925 list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
3926 qla4xxx_convert_param_ddb(&nt_ddb_idx->fw_ddb, tmp_tddb);
3927 if (!qla4xxx_compare_tuple_ddb(ha, fw_tddb, tmp_tddb)) {
3928 ret = QLA_SUCCESS; /* found */
3929 goto exit_check;
3930 }
3931 }
3932
3933exit_check:
3934 if (fw_tddb)
3935 vfree(fw_tddb);
3936 if (tmp_tddb)
3937 vfree(tmp_tddb);
3938 return ret;
3939}
3940
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08003941static void qla4xxx_free_ddb_list(struct list_head *list_ddb)
Mike Christie13483732011-12-01 21:38:41 -06003942{
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08003943 struct qla_ddb_index *ddb_idx, *ddb_idx_tmp;
Mike Christie13483732011-12-01 21:38:41 -06003944
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08003945 list_for_each_entry_safe(ddb_idx, ddb_idx_tmp, list_ddb, list) {
3946 list_del_init(&ddb_idx->list);
3947 vfree(ddb_idx);
Mike Christie13483732011-12-01 21:38:41 -06003948 }
Mike Christie13483732011-12-01 21:38:41 -06003949}
3950
3951static struct iscsi_endpoint *qla4xxx_get_ep_fwdb(struct scsi_qla_host *ha,
3952 struct dev_db_entry *fw_ddb_entry)
3953{
3954 struct iscsi_endpoint *ep;
3955 struct sockaddr_in *addr;
3956 struct sockaddr_in6 *addr6;
3957 struct sockaddr *dst_addr;
3958 char *ip;
3959
3960 /* TODO: need to destroy on unload iscsi_endpoint*/
3961 dst_addr = vmalloc(sizeof(*dst_addr));
3962 if (!dst_addr)
3963 return NULL;
3964
3965 if (fw_ddb_entry->options & DDB_OPT_IPV6_DEVICE) {
3966 dst_addr->sa_family = AF_INET6;
3967 addr6 = (struct sockaddr_in6 *)dst_addr;
3968 ip = (char *)&addr6->sin6_addr;
3969 memcpy(ip, fw_ddb_entry->ip_addr, IPv6_ADDR_LEN);
3970 addr6->sin6_port = htons(le16_to_cpu(fw_ddb_entry->port));
3971
3972 } else {
3973 dst_addr->sa_family = AF_INET;
3974 addr = (struct sockaddr_in *)dst_addr;
3975 ip = (char *)&addr->sin_addr;
3976 memcpy(ip, fw_ddb_entry->ip_addr, IP_ADDR_LEN);
3977 addr->sin_port = htons(le16_to_cpu(fw_ddb_entry->port));
3978 }
3979
3980 ep = qla4xxx_ep_connect(ha->host, dst_addr, 0);
3981 vfree(dst_addr);
3982 return ep;
3983}
3984
3985static int qla4xxx_verify_boot_idx(struct scsi_qla_host *ha, uint16_t idx)
3986{
3987 if (ql4xdisablesysfsboot)
3988 return QLA_SUCCESS;
3989 if (idx == ha->pri_ddb_idx || idx == ha->sec_ddb_idx)
3990 return QLA_ERROR;
3991 return QLA_SUCCESS;
3992}
3993
3994static void qla4xxx_setup_flash_ddb_entry(struct scsi_qla_host *ha,
3995 struct ddb_entry *ddb_entry)
3996{
Nilesh Javalic28eaac2011-12-18 21:40:44 -08003997 uint16_t def_timeout;
3998
Mike Christie13483732011-12-01 21:38:41 -06003999 ddb_entry->ddb_type = FLASH_DDB;
4000 ddb_entry->fw_ddb_index = INVALID_ENTRY;
4001 ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
4002 ddb_entry->ha = ha;
4003 ddb_entry->unblock_sess = qla4xxx_unblock_flash_ddb;
4004 ddb_entry->ddb_change = qla4xxx_flash_ddb_change;
4005
4006 atomic_set(&ddb_entry->retry_relogin_timer, INVALID_ENTRY);
4007 atomic_set(&ddb_entry->relogin_timer, 0);
4008 atomic_set(&ddb_entry->relogin_retry_count, 0);
Nilesh Javalic28eaac2011-12-18 21:40:44 -08004009 def_timeout = le16_to_cpu(ddb_entry->fw_ddb_entry.def_timeout);
Mike Christie13483732011-12-01 21:38:41 -06004010 ddb_entry->default_relogin_timeout =
Nilesh Javalic28eaac2011-12-18 21:40:44 -08004011 (def_timeout > LOGIN_TOV) && (def_timeout < LOGIN_TOV * 10) ?
4012 def_timeout : LOGIN_TOV;
Mike Christie13483732011-12-01 21:38:41 -06004013 ddb_entry->default_time2wait =
4014 le16_to_cpu(ddb_entry->fw_ddb_entry.iscsi_def_time2wait);
4015}
4016
4017static void qla4xxx_wait_for_ip_configuration(struct scsi_qla_host *ha)
4018{
4019 uint32_t idx = 0;
4020 uint32_t ip_idx[IP_ADDR_COUNT] = {0, 1, 2, 3}; /* 4 IP interfaces */
4021 uint32_t sts[MBOX_REG_COUNT];
4022 uint32_t ip_state;
4023 unsigned long wtime;
4024 int ret;
4025
4026 wtime = jiffies + (HZ * IP_CONFIG_TOV);
4027 do {
4028 for (idx = 0; idx < IP_ADDR_COUNT; idx++) {
4029 if (ip_idx[idx] == -1)
4030 continue;
4031
4032 ret = qla4xxx_get_ip_state(ha, 0, ip_idx[idx], sts);
4033
4034 if (ret == QLA_ERROR) {
4035 ip_idx[idx] = -1;
4036 continue;
4037 }
4038
4039 ip_state = (sts[1] & IP_STATE_MASK) >> IP_STATE_SHIFT;
4040
4041 DEBUG2(ql4_printk(KERN_INFO, ha,
4042 "Waiting for IP state for idx = %d, state = 0x%x\n",
4043 ip_idx[idx], ip_state));
4044 if (ip_state == IP_ADDRSTATE_UNCONFIGURED ||
4045 ip_state == IP_ADDRSTATE_INVALID ||
4046 ip_state == IP_ADDRSTATE_PREFERRED ||
4047 ip_state == IP_ADDRSTATE_DEPRICATED ||
4048 ip_state == IP_ADDRSTATE_DISABLING)
4049 ip_idx[idx] = -1;
Mike Christie13483732011-12-01 21:38:41 -06004050 }
4051
4052 /* Break if all IP states checked */
4053 if ((ip_idx[0] == -1) &&
4054 (ip_idx[1] == -1) &&
4055 (ip_idx[2] == -1) &&
4056 (ip_idx[3] == -1))
4057 break;
4058 schedule_timeout_uninterruptible(HZ);
4059 } while (time_after(wtime, jiffies));
4060}
4061
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004062static void qla4xxx_build_st_list(struct scsi_qla_host *ha,
4063 struct list_head *list_st)
Mike Christie13483732011-12-01 21:38:41 -06004064{
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004065 struct qla_ddb_index *st_ddb_idx;
Mike Christie13483732011-12-01 21:38:41 -06004066 int max_ddbs;
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004067 int fw_idx_size;
4068 struct dev_db_entry *fw_ddb_entry;
4069 dma_addr_t fw_ddb_dma;
Mike Christie13483732011-12-01 21:38:41 -06004070 int ret;
4071 uint32_t idx = 0, next_idx = 0;
4072 uint32_t state = 0, conn_err = 0;
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004073 uint16_t conn_id = 0;
Mike Christie13483732011-12-01 21:38:41 -06004074
4075 fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
4076 &fw_ddb_dma);
4077 if (fw_ddb_entry == NULL) {
4078 DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004079 goto exit_st_list;
Mike Christie13483732011-12-01 21:38:41 -06004080 }
4081
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004082 max_ddbs = is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
4083 MAX_DEV_DB_ENTRIES;
Mike Christie13483732011-12-01 21:38:41 -06004084 fw_idx_size = sizeof(struct qla_ddb_index);
4085
4086 for (idx = 0; idx < max_ddbs; idx = next_idx) {
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004087 ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry, fw_ddb_dma,
4088 NULL, &next_idx, &state,
4089 &conn_err, NULL, &conn_id);
Mike Christie13483732011-12-01 21:38:41 -06004090 if (ret == QLA_ERROR)
4091 break;
4092
Lalit Chandivade981c9822012-02-13 18:30:41 +05304093 /* Ignore DDB if invalid state (unassigned) */
4094 if (state == DDB_DS_UNASSIGNED)
4095 goto continue_next_st;
4096
Mike Christie13483732011-12-01 21:38:41 -06004097 /* Check if ST, add to the list_st */
4098 if (strlen((char *) fw_ddb_entry->iscsi_name) != 0)
4099 goto continue_next_st;
4100
4101 st_ddb_idx = vzalloc(fw_idx_size);
4102 if (!st_ddb_idx)
4103 break;
4104
4105 st_ddb_idx->fw_ddb_idx = idx;
4106
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004107 list_add_tail(&st_ddb_idx->list, list_st);
Mike Christie13483732011-12-01 21:38:41 -06004108continue_next_st:
4109 if (next_idx == 0)
4110 break;
4111 }
4112
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004113exit_st_list:
4114 if (fw_ddb_entry)
4115 dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
4116}
4117
4118/**
4119 * qla4xxx_remove_failed_ddb - Remove inactive or failed ddb from list
4120 * @ha: pointer to adapter structure
4121 * @list_ddb: List from which failed ddb to be removed
4122 *
4123 * Iterate over the list of DDBs and find and remove DDBs that are either in
4124 * no connection active state or failed state
4125 **/
4126static void qla4xxx_remove_failed_ddb(struct scsi_qla_host *ha,
4127 struct list_head *list_ddb)
4128{
4129 struct qla_ddb_index *ddb_idx, *ddb_idx_tmp;
4130 uint32_t next_idx = 0;
4131 uint32_t state = 0, conn_err = 0;
4132 int ret;
4133
4134 list_for_each_entry_safe(ddb_idx, ddb_idx_tmp, list_ddb, list) {
4135 ret = qla4xxx_get_fwddb_entry(ha, ddb_idx->fw_ddb_idx,
4136 NULL, 0, NULL, &next_idx, &state,
4137 &conn_err, NULL, NULL);
4138 if (ret == QLA_ERROR)
4139 continue;
4140
4141 if (state == DDB_DS_NO_CONNECTION_ACTIVE ||
4142 state == DDB_DS_SESSION_FAILED) {
4143 list_del_init(&ddb_idx->list);
4144 vfree(ddb_idx);
4145 }
4146 }
4147}
4148
4149static int qla4xxx_sess_conn_setup(struct scsi_qla_host *ha,
4150 struct dev_db_entry *fw_ddb_entry,
4151 int is_reset)
4152{
4153 struct iscsi_cls_session *cls_sess;
4154 struct iscsi_session *sess;
4155 struct iscsi_cls_conn *cls_conn;
4156 struct iscsi_endpoint *ep;
4157 uint16_t cmds_max = 32;
4158 uint16_t conn_id = 0;
4159 uint32_t initial_cmdsn = 0;
4160 int ret = QLA_SUCCESS;
4161
4162 struct ddb_entry *ddb_entry = NULL;
4163
4164 /* Create session object, with INVALID_ENTRY,
4165 * the targer_id would get set when we issue the login
4166 */
4167 cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport, ha->host,
4168 cmds_max, sizeof(struct ddb_entry),
4169 sizeof(struct ql4_task_data),
4170 initial_cmdsn, INVALID_ENTRY);
4171 if (!cls_sess) {
4172 ret = QLA_ERROR;
4173 goto exit_setup;
4174 }
4175
4176 /*
4177 * so calling module_put function to decrement the
4178 * reference count.
4179 **/
4180 module_put(qla4xxx_iscsi_transport.owner);
4181 sess = cls_sess->dd_data;
4182 ddb_entry = sess->dd_data;
4183 ddb_entry->sess = cls_sess;
4184
4185 cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
4186 memcpy(&ddb_entry->fw_ddb_entry, fw_ddb_entry,
4187 sizeof(struct dev_db_entry));
4188
4189 qla4xxx_setup_flash_ddb_entry(ha, ddb_entry);
4190
4191 cls_conn = iscsi_conn_setup(cls_sess, sizeof(struct qla_conn), conn_id);
4192
4193 if (!cls_conn) {
4194 ret = QLA_ERROR;
4195 goto exit_setup;
4196 }
4197
4198 ddb_entry->conn = cls_conn;
4199
4200 /* Setup ep, for displaying attributes in sysfs */
4201 ep = qla4xxx_get_ep_fwdb(ha, fw_ddb_entry);
4202 if (ep) {
4203 ep->conn = cls_conn;
4204 cls_conn->ep = ep;
4205 } else {
4206 DEBUG2(ql4_printk(KERN_ERR, ha, "Unable to get ep\n"));
4207 ret = QLA_ERROR;
4208 goto exit_setup;
4209 }
4210
4211 /* Update sess/conn params */
4212 qla4xxx_copy_fwddb_param(ha, fw_ddb_entry, cls_sess, cls_conn);
4213
4214 if (is_reset == RESET_ADAPTER) {
4215 iscsi_block_session(cls_sess);
4216 /* Use the relogin path to discover new devices
4217 * by short-circuting the logic of setting
4218 * timer to relogin - instead set the flags
4219 * to initiate login right away.
4220 */
4221 set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
4222 set_bit(DF_RELOGIN, &ddb_entry->flags);
4223 }
4224
4225exit_setup:
4226 return ret;
4227}
4228
4229static void qla4xxx_build_nt_list(struct scsi_qla_host *ha,
4230 struct list_head *list_nt, int is_reset)
4231{
4232 struct dev_db_entry *fw_ddb_entry;
4233 dma_addr_t fw_ddb_dma;
4234 int max_ddbs;
4235 int fw_idx_size;
4236 int ret;
4237 uint32_t idx = 0, next_idx = 0;
4238 uint32_t state = 0, conn_err = 0;
4239 uint16_t conn_id = 0;
4240 struct qla_ddb_index *nt_ddb_idx;
4241
4242 fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
4243 &fw_ddb_dma);
4244 if (fw_ddb_entry == NULL) {
4245 DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
4246 goto exit_nt_list;
4247 }
4248 max_ddbs = is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
4249 MAX_DEV_DB_ENTRIES;
4250 fw_idx_size = sizeof(struct qla_ddb_index);
4251
4252 for (idx = 0; idx < max_ddbs; idx = next_idx) {
4253 ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry, fw_ddb_dma,
4254 NULL, &next_idx, &state,
4255 &conn_err, NULL, &conn_id);
4256 if (ret == QLA_ERROR)
4257 break;
4258
4259 if (qla4xxx_verify_boot_idx(ha, idx) != QLA_SUCCESS)
4260 goto continue_next_nt;
4261
4262 /* Check if NT, then add to list it */
4263 if (strlen((char *) fw_ddb_entry->iscsi_name) == 0)
4264 goto continue_next_nt;
4265
4266 if (!(state == DDB_DS_NO_CONNECTION_ACTIVE ||
4267 state == DDB_DS_SESSION_FAILED))
4268 goto continue_next_nt;
4269
4270 DEBUG2(ql4_printk(KERN_INFO, ha,
4271 "Adding DDB to session = 0x%x\n", idx));
4272 if (is_reset == INIT_ADAPTER) {
4273 nt_ddb_idx = vmalloc(fw_idx_size);
4274 if (!nt_ddb_idx)
4275 break;
4276
4277 nt_ddb_idx->fw_ddb_idx = idx;
4278
4279 memcpy(&nt_ddb_idx->fw_ddb, fw_ddb_entry,
4280 sizeof(struct dev_db_entry));
4281
4282 if (qla4xxx_is_flash_ddb_exists(ha, list_nt,
4283 fw_ddb_entry) == QLA_SUCCESS) {
4284 vfree(nt_ddb_idx);
4285 goto continue_next_nt;
4286 }
4287 list_add_tail(&nt_ddb_idx->list, list_nt);
4288 } else if (is_reset == RESET_ADAPTER) {
4289 if (qla4xxx_is_session_exists(ha, fw_ddb_entry) ==
4290 QLA_SUCCESS)
4291 goto continue_next_nt;
4292 }
4293
4294 ret = qla4xxx_sess_conn_setup(ha, fw_ddb_entry, is_reset);
4295 if (ret == QLA_ERROR)
4296 goto exit_nt_list;
4297
4298continue_next_nt:
4299 if (next_idx == 0)
4300 break;
4301 }
4302
4303exit_nt_list:
4304 if (fw_ddb_entry)
4305 dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
4306}
4307
4308/**
4309 * qla4xxx_build_ddb_list - Build ddb list and setup sessions
4310 * @ha: pointer to adapter structure
4311 * @is_reset: Is this init path or reset path
4312 *
4313 * Create a list of sendtargets (st) from firmware DDBs, issue send targets
4314 * using connection open, then create the list of normal targets (nt)
4315 * from firmware DDBs. Based on the list of nt setup session and connection
4316 * objects.
4317 **/
4318void qla4xxx_build_ddb_list(struct scsi_qla_host *ha, int is_reset)
4319{
4320 uint16_t tmo = 0;
4321 struct list_head list_st, list_nt;
4322 struct qla_ddb_index *st_ddb_idx, *st_ddb_idx_tmp;
4323 unsigned long wtime;
4324
4325 if (!test_bit(AF_LINK_UP, &ha->flags)) {
4326 set_bit(AF_BUILD_DDB_LIST, &ha->flags);
4327 ha->is_reset = is_reset;
4328 return;
4329 }
4330
4331 INIT_LIST_HEAD(&list_st);
4332 INIT_LIST_HEAD(&list_nt);
4333
4334 qla4xxx_build_st_list(ha, &list_st);
4335
Mike Christie13483732011-12-01 21:38:41 -06004336 /* Before issuing conn open mbox, ensure all IPs states are configured
4337 * Note, conn open fails if IPs are not configured
4338 */
4339 qla4xxx_wait_for_ip_configuration(ha);
4340
4341 /* Go thru the STs and fire the sendtargets by issuing conn open mbx */
4342 list_for_each_entry_safe(st_ddb_idx, st_ddb_idx_tmp, &list_st, list) {
4343 qla4xxx_conn_open(ha, st_ddb_idx->fw_ddb_idx);
4344 }
4345
4346 /* Wait to ensure all sendtargets are done for min 12 sec wait */
Nilesh Javalic28eaac2011-12-18 21:40:44 -08004347 tmo = ((ha->def_timeout > LOGIN_TOV) &&
4348 (ha->def_timeout < LOGIN_TOV * 10) ?
4349 ha->def_timeout : LOGIN_TOV);
4350
Mike Christie13483732011-12-01 21:38:41 -06004351 DEBUG2(ql4_printk(KERN_INFO, ha,
4352 "Default time to wait for build ddb %d\n", tmo));
4353
4354 wtime = jiffies + (HZ * tmo);
4355 do {
Nilesh Javalif1f2e602011-12-16 01:58:57 -08004356 if (list_empty(&list_st))
4357 break;
4358
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004359 qla4xxx_remove_failed_ddb(ha, &list_st);
Mike Christie13483732011-12-01 21:38:41 -06004360 schedule_timeout_uninterruptible(HZ / 10);
4361 } while (time_after(wtime, jiffies));
4362
4363 /* Free up the sendtargets list */
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004364 qla4xxx_free_ddb_list(&list_st);
Mike Christie13483732011-12-01 21:38:41 -06004365
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004366 qla4xxx_build_nt_list(ha, &list_nt, is_reset);
Mike Christie13483732011-12-01 21:38:41 -06004367
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004368 qla4xxx_free_ddb_list(&list_nt);
Mike Christie13483732011-12-01 21:38:41 -06004369
4370 qla4xxx_free_ddb_index(ha);
4371}
4372
David Somayajuluafaf5a22006-09-19 10:28:00 -07004373/**
4374 * qla4xxx_probe_adapter - callback function to probe HBA
4375 * @pdev: pointer to pci_dev structure
4376 * @pci_device_id: pointer to pci_device entry
4377 *
4378 * This routine will probe for Qlogic 4xxx iSCSI host adapters.
4379 * It returns zero if successful. It also initializes all data necessary for
4380 * the driver.
4381 **/
4382static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
4383 const struct pci_device_id *ent)
4384{
4385 int ret = -ENODEV, status;
4386 struct Scsi_Host *host;
4387 struct scsi_qla_host *ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004388 uint8_t init_retry_count = 0;
4389 char buf[34];
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304390 struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
Prasanna Mumbaif9880e72011-03-21 03:34:26 -07004391 uint32_t dev_state;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004392
4393 if (pci_enable_device(pdev))
4394 return -1;
4395
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004396 host = iscsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha), 0);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004397 if (host == NULL) {
4398 printk(KERN_WARNING
4399 "qla4xxx: Couldn't allocate host from scsi layer!\n");
4400 goto probe_disable_device;
4401 }
4402
4403 /* Clear our data area */
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004404 ha = to_qla_host(host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004405 memset(ha, 0, sizeof(*ha));
4406
4407 /* Save the information from PCI BIOS. */
4408 ha->pdev = pdev;
4409 ha->host = host;
4410 ha->host_no = host->host_no;
4411
Lalit Chandivade2232be02010-07-30 14:38:47 +05304412 pci_enable_pcie_error_reporting(pdev);
4413
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304414 /* Setup Runtime configurable options */
4415 if (is_qla8022(ha)) {
4416 ha->isp_ops = &qla4_8xxx_isp_ops;
4417 rwlock_init(&ha->hw_lock);
4418 ha->qdr_sn_window = -1;
4419 ha->ddr_mn_window = -1;
4420 ha->curr_window = 255;
4421 ha->func_num = PCI_FUNC(ha->pdev->devfn);
4422 nx_legacy_intr = &legacy_intr[ha->func_num];
4423 ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
4424 ha->nx_legacy_intr.tgt_status_reg =
4425 nx_legacy_intr->tgt_status_reg;
4426 ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
4427 ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
4428 } else {
4429 ha->isp_ops = &qla4xxx_isp_ops;
4430 }
4431
Lalit Chandivade2232be02010-07-30 14:38:47 +05304432 /* Set EEH reset type to fundamental if required by hba */
4433 if (is_qla8022(ha))
4434 pdev->needs_freset = 1;
4435
David Somayajuluafaf5a22006-09-19 10:28:00 -07004436 /* Configure PCI I/O space. */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304437 ret = ha->isp_ops->iospace_config(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004438 if (ret)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304439 goto probe_failed_ioconfig;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004440
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304441 ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
David Somayajuluafaf5a22006-09-19 10:28:00 -07004442 pdev->device, pdev->irq, ha->reg);
4443
4444 qla4xxx_config_dma_addressing(ha);
4445
4446 /* Initialize lists and spinlocks. */
David Somayajuluafaf5a22006-09-19 10:28:00 -07004447 INIT_LIST_HEAD(&ha->free_srb_q);
4448
4449 mutex_init(&ha->mbox_sem);
Lalit Chandivade45494152011-10-07 16:55:42 -07004450 mutex_init(&ha->chap_sem);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304451 init_completion(&ha->mbx_intr_comp);
Vikas Chaudhary95d31262011-08-12 02:51:29 -07004452 init_completion(&ha->disable_acb_comp);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004453
4454 spin_lock_init(&ha->hardware_lock);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004455
4456 /* Allocate dma buffers */
4457 if (qla4xxx_mem_alloc(ha)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304458 ql4_printk(KERN_WARNING, ha,
4459 "[ERROR] Failed to allocate memory for adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07004460
4461 ret = -ENOMEM;
4462 goto probe_failed;
4463 }
4464
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004465 host->cmd_per_lun = 3;
4466 host->max_channel = 0;
4467 host->max_lun = MAX_LUNS - 1;
4468 host->max_id = MAX_TARGETS;
4469 host->max_cmd_len = IOCB_MAX_CDB_LEN;
4470 host->can_queue = MAX_SRBS ;
4471 host->transportt = qla4xxx_scsi_transport;
4472
4473 ret = scsi_init_shared_tag_map(host, MAX_SRBS);
4474 if (ret) {
4475 ql4_printk(KERN_WARNING, ha,
4476 "%s: scsi_init_shared_tag_map failed\n", __func__);
4477 goto probe_failed;
4478 }
4479
4480 pci_set_drvdata(pdev, ha);
4481
4482 ret = scsi_add_host(host, &pdev->dev);
4483 if (ret)
4484 goto probe_failed;
4485
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304486 if (is_qla8022(ha))
4487 (void) qla4_8xxx_get_flash_info(ha);
4488
David Somayajuluafaf5a22006-09-19 10:28:00 -07004489 /*
4490 * Initialize the Host adapter request/response queues and
4491 * firmware
4492 * NOTE: interrupts enabled upon successful completion
4493 */
Mike Christie13483732011-12-01 21:38:41 -06004494 status = qla4xxx_initialize_adapter(ha, INIT_ADAPTER);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304495 while ((!test_bit(AF_ONLINE, &ha->flags)) &&
4496 init_retry_count++ < MAX_INIT_RETRIES) {
Prasanna Mumbaif9880e72011-03-21 03:34:26 -07004497
4498 if (is_qla8022(ha)) {
4499 qla4_8xxx_idc_lock(ha);
4500 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
4501 qla4_8xxx_idc_unlock(ha);
4502 if (dev_state == QLA82XX_DEV_FAILED) {
4503 ql4_printk(KERN_WARNING, ha, "%s: don't retry "
4504 "initialize adapter. H/W is in failed state\n",
4505 __func__);
4506 break;
4507 }
4508 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07004509 DEBUG2(printk("scsi: %s: retrying adapter initialization "
4510 "(%d)\n", __func__, init_retry_count));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304511
4512 if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
4513 continue;
4514
Mike Christie13483732011-12-01 21:38:41 -06004515 status = qla4xxx_initialize_adapter(ha, INIT_ADAPTER);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004516 }
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304517
4518 if (!test_bit(AF_ONLINE, &ha->flags)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304519 ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07004520
Lalit Chandivadefe998522010-12-02 22:12:36 -08004521 if (is_qla8022(ha) && ql4xdontresethba) {
4522 /* Put the device in failed state. */
4523 DEBUG2(printk(KERN_ERR "HW STATE: FAILED\n"));
4524 qla4_8xxx_idc_lock(ha);
4525 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
4526 QLA82XX_DEV_FAILED);
4527 qla4_8xxx_idc_unlock(ha);
4528 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07004529 ret = -ENODEV;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004530 goto remove_host;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004531 }
4532
David Somayajuluafaf5a22006-09-19 10:28:00 -07004533 /* Startup the kernel thread for this host adapter. */
4534 DEBUG2(printk("scsi: %s: Starting kernel thread for "
4535 "qla4xxx_dpc\n", __func__));
4536 sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
4537 ha->dpc_thread = create_singlethread_workqueue(buf);
4538 if (!ha->dpc_thread) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304539 ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07004540 ret = -ENODEV;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004541 goto remove_host;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004542 }
David Howellsc4028952006-11-22 14:57:56 +00004543 INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004544
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004545 sprintf(buf, "qla4xxx_%lu_task", ha->host_no);
4546 ha->task_wq = alloc_workqueue(buf, WQ_MEM_RECLAIM, 1);
4547 if (!ha->task_wq) {
4548 ql4_printk(KERN_WARNING, ha, "Unable to start task thread!\n");
4549 ret = -ENODEV;
4550 goto remove_host;
4551 }
4552
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304553 /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
4554 * (which is called indirectly by qla4xxx_initialize_adapter),
4555 * so that irqs will be registered after crbinit but before
4556 * mbx_intr_enable.
4557 */
4558 if (!is_qla8022(ha)) {
4559 ret = qla4xxx_request_irqs(ha);
4560 if (ret) {
4561 ql4_printk(KERN_WARNING, ha, "Failed to reserve "
4562 "interrupt %d already in use.\n", pdev->irq);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004563 goto remove_host;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304564 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07004565 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07004566
Lalit Chandivade2232be02010-07-30 14:38:47 +05304567 pci_save_state(ha->pdev);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304568 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004569
4570 /* Start timer thread. */
4571 qla4xxx_start_timer(ha, qla4xxx_timer, 1);
4572
4573 set_bit(AF_INIT_DONE, &ha->flags);
4574
David Somayajuluafaf5a22006-09-19 10:28:00 -07004575 printk(KERN_INFO
4576 " QLogic iSCSI HBA Driver version: %s\n"
4577 " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
4578 qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
4579 ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
4580 ha->patch_number, ha->build_number);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05004581
Manish Rangankar2a991c22011-07-25 13:48:55 -05004582 if (qla4xxx_setup_boot_info(ha))
4583 ql4_printk(KERN_ERR, ha, "%s:ISCSI boot info setup failed\n",
4584 __func__);
4585
Mike Christie13483732011-12-01 21:38:41 -06004586 /* Perform the build ddb list and login to each */
4587 qla4xxx_build_ddb_list(ha, INIT_ADAPTER);
4588 iscsi_host_for_each_session(ha->host, qla4xxx_login_flash_ddb);
4589
4590 qla4xxx_create_chap_list(ha);
4591
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05004592 qla4xxx_create_ifaces(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004593 return 0;
4594
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004595remove_host:
4596 scsi_remove_host(ha->host);
4597
David Somayajuluafaf5a22006-09-19 10:28:00 -07004598probe_failed:
4599 qla4xxx_free_adapter(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304600
4601probe_failed_ioconfig:
Lalit Chandivade2232be02010-07-30 14:38:47 +05304602 pci_disable_pcie_error_reporting(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004603 scsi_host_put(ha->host);
4604
4605probe_disable_device:
4606 pci_disable_device(pdev);
4607
4608 return ret;
4609}
4610
4611/**
Karen Higgins7eece5a2011-03-21 03:34:29 -07004612 * qla4xxx_prevent_other_port_reinit - prevent other port from re-initialize
4613 * @ha: pointer to adapter structure
4614 *
4615 * Mark the other ISP-4xxx port to indicate that the driver is being removed,
4616 * so that the other port will not re-initialize while in the process of
4617 * removing the ha due to driver unload or hba hotplug.
4618 **/
4619static void qla4xxx_prevent_other_port_reinit(struct scsi_qla_host *ha)
4620{
4621 struct scsi_qla_host *other_ha = NULL;
4622 struct pci_dev *other_pdev = NULL;
4623 int fn = ISP4XXX_PCI_FN_2;
4624
4625 /*iscsi function numbers for ISP4xxx is 1 and 3*/
4626 if (PCI_FUNC(ha->pdev->devfn) & BIT_1)
4627 fn = ISP4XXX_PCI_FN_1;
4628
4629 other_pdev =
4630 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
4631 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
4632 fn));
4633
4634 /* Get other_ha if other_pdev is valid and state is enable*/
4635 if (other_pdev) {
4636 if (atomic_read(&other_pdev->enable_cnt)) {
4637 other_ha = pci_get_drvdata(other_pdev);
4638 if (other_ha) {
4639 set_bit(AF_HA_REMOVAL, &other_ha->flags);
4640 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: "
4641 "Prevent %s reinit\n", __func__,
4642 dev_name(&other_ha->pdev->dev)));
4643 }
4644 }
4645 pci_dev_put(other_pdev);
4646 }
4647}
4648
Mike Christie13483732011-12-01 21:38:41 -06004649static void qla4xxx_destroy_fw_ddb_session(struct scsi_qla_host *ha)
4650{
4651 struct ddb_entry *ddb_entry;
4652 int options;
4653 int idx;
4654
4655 for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
4656
4657 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
4658 if ((ddb_entry != NULL) &&
4659 (ddb_entry->ddb_type == FLASH_DDB)) {
4660
4661 options = LOGOUT_OPTION_CLOSE_SESSION;
4662 if (qla4xxx_session_logout_ddb(ha, ddb_entry, options)
4663 == QLA_ERROR)
4664 ql4_printk(KERN_ERR, ha, "%s: Logout failed\n",
4665 __func__);
4666
4667 qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
4668 /*
4669 * we have decremented the reference count of the driver
4670 * when we setup the session to have the driver unload
4671 * to be seamless without actually destroying the
4672 * session
4673 **/
4674 try_module_get(qla4xxx_iscsi_transport.owner);
4675 iscsi_destroy_endpoint(ddb_entry->conn->ep);
4676 qla4xxx_free_ddb(ha, ddb_entry);
4677 iscsi_session_teardown(ddb_entry->sess);
4678 }
4679 }
4680}
Karen Higgins7eece5a2011-03-21 03:34:29 -07004681/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07004682 * qla4xxx_remove_adapter - calback function to remove adapter.
4683 * @pci_dev: PCI device pointer
4684 **/
4685static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
4686{
4687 struct scsi_qla_host *ha;
4688
4689 ha = pci_get_drvdata(pdev);
4690
Karen Higgins7eece5a2011-03-21 03:34:29 -07004691 if (!is_qla8022(ha))
4692 qla4xxx_prevent_other_port_reinit(ha);
David C Somayajulubee4fe82007-05-23 18:03:32 -07004693
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05004694 /* destroy iface from sysfs */
4695 qla4xxx_destroy_ifaces(ha);
4696
Mike Christie13483732011-12-01 21:38:41 -06004697 if ((!ql4xdisablesysfsboot) && ha->boot_kset)
Manish Rangankar2a991c22011-07-25 13:48:55 -05004698 iscsi_boot_destroy_kset(ha->boot_kset);
4699
Mike Christie13483732011-12-01 21:38:41 -06004700 qla4xxx_destroy_fw_ddb_session(ha);
4701
David Somayajuluafaf5a22006-09-19 10:28:00 -07004702 scsi_remove_host(ha->host);
4703
4704 qla4xxx_free_adapter(ha);
4705
4706 scsi_host_put(ha->host);
4707
Lalit Chandivade2232be02010-07-30 14:38:47 +05304708 pci_disable_pcie_error_reporting(pdev);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304709 pci_disable_device(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004710 pci_set_drvdata(pdev, NULL);
4711}
4712
4713/**
4714 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
4715 * @ha: HA context
4716 *
4717 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
4718 * supported addressing method.
4719 */
Adrian Bunk47975472007-04-26 00:35:16 -07004720static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07004721{
4722 int retval;
4723
4724 /* Update our PCI device dma_mask for full 64 bit mask */
Yang Hongyang6a355282009-04-06 19:01:13 -07004725 if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
4726 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07004727 dev_dbg(&ha->pdev->dev,
4728 "Failed to set 64 bit PCI consistent mask; "
4729 "using 32 bit.\n");
4730 retval = pci_set_consistent_dma_mask(ha->pdev,
Yang Hongyang284901a2009-04-06 19:01:15 -07004731 DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07004732 }
4733 } else
Yang Hongyang284901a2009-04-06 19:01:15 -07004734 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07004735}
4736
4737static int qla4xxx_slave_alloc(struct scsi_device *sdev)
4738{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004739 struct iscsi_cls_session *cls_sess;
4740 struct iscsi_session *sess;
4741 struct ddb_entry *ddb;
Vikas Chaudhary8bb40332011-03-21 03:34:31 -07004742 int queue_depth = QL4_DEF_QDEPTH;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004743
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004744 cls_sess = starget_to_session(sdev->sdev_target);
4745 sess = cls_sess->dd_data;
4746 ddb = sess->dd_data;
4747
David Somayajuluafaf5a22006-09-19 10:28:00 -07004748 sdev->hostdata = ddb;
4749 sdev->tagged_supported = 1;
Vikas Chaudhary8bb40332011-03-21 03:34:31 -07004750
4751 if (ql4xmaxqdepth != 0 && ql4xmaxqdepth <= 0xffffU)
4752 queue_depth = ql4xmaxqdepth;
4753
4754 scsi_activate_tcq(sdev, queue_depth);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004755 return 0;
4756}
4757
4758static int qla4xxx_slave_configure(struct scsi_device *sdev)
4759{
4760 sdev->tagged_supported = 1;
4761 return 0;
4762}
4763
4764static void qla4xxx_slave_destroy(struct scsi_device *sdev)
4765{
4766 scsi_deactivate_tcq(sdev, 1);
4767}
4768
4769/**
4770 * qla4xxx_del_from_active_array - returns an active srb
4771 * @ha: Pointer to host adapter structure.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02004772 * @index: index into the active_array
David Somayajuluafaf5a22006-09-19 10:28:00 -07004773 *
4774 * This routine removes and returns the srb at the specified index
4775 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304776struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
4777 uint32_t index)
David Somayajuluafaf5a22006-09-19 10:28:00 -07004778{
4779 struct srb *srb = NULL;
Vikas Chaudhary53698872010-04-28 11:41:59 +05304780 struct scsi_cmnd *cmd = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004781
Vikas Chaudhary53698872010-04-28 11:41:59 +05304782 cmd = scsi_host_find_tag(ha->host, index);
4783 if (!cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07004784 return srb;
4785
Vikas Chaudhary53698872010-04-28 11:41:59 +05304786 srb = (struct srb *)CMD_SP(cmd);
4787 if (!srb)
David Somayajuluafaf5a22006-09-19 10:28:00 -07004788 return srb;
4789
4790 /* update counters */
4791 if (srb->flags & SRB_DMA_VALID) {
4792 ha->req_q_count += srb->iocb_cnt;
4793 ha->iocb_cnt -= srb->iocb_cnt;
4794 if (srb->cmd)
Vikas Chaudhary53698872010-04-28 11:41:59 +05304795 srb->cmd->host_scribble =
4796 (unsigned char *)(unsigned long) MAX_SRBS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004797 }
4798 return srb;
4799}
4800
4801/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07004802 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304803 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07004804 * @cmd: Scsi Command to wait on.
4805 *
4806 * This routine waits for the command to be returned by the Firmware
4807 * for some max time.
4808 **/
4809static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
4810 struct scsi_cmnd *cmd)
4811{
4812 int done = 0;
4813 struct srb *rp;
4814 uint32_t max_wait_time = EH_WAIT_CMD_TOV;
Lalit Chandivade2232be02010-07-30 14:38:47 +05304815 int ret = SUCCESS;
4816
4817 /* Dont wait on command if PCI error is being handled
4818 * by PCI AER driver
4819 */
4820 if (unlikely(pci_channel_offline(ha->pdev)) ||
4821 (test_bit(AF_EEH_BUSY, &ha->flags))) {
4822 ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
4823 ha->host_no, __func__);
4824 return ret;
4825 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07004826
4827 do {
4828 /* Checking to see if its returned to OS */
Vikas Chaudhary53698872010-04-28 11:41:59 +05304829 rp = (struct srb *) CMD_SP(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004830 if (rp == NULL) {
4831 done++;
4832 break;
4833 }
4834
4835 msleep(2000);
4836 } while (max_wait_time--);
4837
4838 return done;
4839}
4840
4841/**
4842 * qla4xxx_wait_for_hba_online - waits for HBA to come online
4843 * @ha: Pointer to host adapter structure
4844 **/
4845static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
4846{
4847 unsigned long wait_online;
4848
Vikas Chaudharyf581a3f2010-10-06 22:47:48 -07004849 wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004850 while (time_before(jiffies, wait_online)) {
4851
4852 if (adapter_up(ha))
4853 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004854
4855 msleep(2000);
4856 }
4857
4858 return QLA_ERROR;
4859}
4860
4861/**
Mike Christiece545032008-02-29 18:25:20 -06004862 * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02004863 * @ha: pointer to HBA
David Somayajuluafaf5a22006-09-19 10:28:00 -07004864 * @t: target id
4865 * @l: lun id
4866 *
4867 * This function waits for all outstanding commands to a lun to complete. It
4868 * returns 0 if all pending commands are returned and 1 otherwise.
4869 **/
Mike Christiece545032008-02-29 18:25:20 -06004870static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
4871 struct scsi_target *stgt,
4872 struct scsi_device *sdev)
David Somayajuluafaf5a22006-09-19 10:28:00 -07004873{
4874 int cnt;
4875 int status = 0;
4876 struct scsi_cmnd *cmd;
4877
4878 /*
Mike Christiece545032008-02-29 18:25:20 -06004879 * Waiting for all commands for the designated target or dev
4880 * in the active array
David Somayajuluafaf5a22006-09-19 10:28:00 -07004881 */
4882 for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
4883 cmd = scsi_host_find_tag(ha->host, cnt);
Mike Christiece545032008-02-29 18:25:20 -06004884 if (cmd && stgt == scsi_target(cmd->device) &&
4885 (!sdev || sdev == cmd->device)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07004886 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
4887 status++;
4888 break;
4889 }
4890 }
4891 }
4892 return status;
4893}
4894
4895/**
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304896 * qla4xxx_eh_abort - callback for abort task.
4897 * @cmd: Pointer to Linux's SCSI command structure
4898 *
4899 * This routine is called by the Linux OS to abort the specified
4900 * command.
4901 **/
4902static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
4903{
4904 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
4905 unsigned int id = cmd->device->id;
4906 unsigned int lun = cmd->device->lun;
Mike Christie92b3e5b2010-10-06 22:51:17 -07004907 unsigned long flags;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304908 struct srb *srb = NULL;
4909 int ret = SUCCESS;
4910 int wait = 0;
4911
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304912 ql4_printk(KERN_INFO, ha,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004913 "scsi%ld:%d:%d: Abort command issued cmd=%p\n",
4914 ha->host_no, id, lun, cmd);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304915
Mike Christie92b3e5b2010-10-06 22:51:17 -07004916 spin_lock_irqsave(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304917 srb = (struct srb *) CMD_SP(cmd);
Mike Christie92b3e5b2010-10-06 22:51:17 -07004918 if (!srb) {
4919 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304920 return SUCCESS;
Mike Christie92b3e5b2010-10-06 22:51:17 -07004921 }
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304922 kref_get(&srb->srb_ref);
Mike Christie92b3e5b2010-10-06 22:51:17 -07004923 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304924
4925 if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
4926 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
4927 ha->host_no, id, lun));
4928 ret = FAILED;
4929 } else {
4930 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
4931 ha->host_no, id, lun));
4932 wait = 1;
4933 }
4934
4935 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
4936
4937 /* Wait for command to complete */
4938 if (wait) {
4939 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
4940 DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
4941 ha->host_no, id, lun));
4942 ret = FAILED;
4943 }
4944 }
4945
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304946 ql4_printk(KERN_INFO, ha,
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304947 "scsi%ld:%d:%d: Abort command - %s\n",
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004948 ha->host_no, id, lun, (ret == SUCCESS) ? "succeeded" : "failed");
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304949
4950 return ret;
4951}
4952
4953/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07004954 * qla4xxx_eh_device_reset - callback for target reset.
4955 * @cmd: Pointer to Linux's SCSI command structure
4956 *
4957 * This routine is called by the Linux OS to reset all luns on the
4958 * specified target.
4959 **/
4960static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
4961{
4962 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
4963 struct ddb_entry *ddb_entry = cmd->device->hostdata;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004964 int ret = FAILED, stat;
4965
Karen Higgins612f7342009-07-15 15:03:01 -05004966 if (!ddb_entry)
David Somayajuluafaf5a22006-09-19 10:28:00 -07004967 return ret;
4968
Mike Christiec01be6d2010-07-22 16:59:49 +05304969 ret = iscsi_block_scsi_eh(cmd);
4970 if (ret)
4971 return ret;
4972 ret = FAILED;
4973
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304974 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07004975 "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
4976 cmd->device->channel, cmd->device->id, cmd->device->lun);
4977
4978 DEBUG2(printk(KERN_INFO
4979 "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
4980 "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
Jens Axboe242f9dc2008-09-14 05:55:09 -07004981 cmd, jiffies, cmd->request->timeout / HZ,
David Somayajuluafaf5a22006-09-19 10:28:00 -07004982 ha->dpc_flags, cmd->result, cmd->allowed));
4983
4984 /* FIXME: wait for hba to go online */
4985 stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
4986 if (stat != QLA_SUCCESS) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304987 ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004988 goto eh_dev_reset_done;
4989 }
4990
Mike Christiece545032008-02-29 18:25:20 -06004991 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
4992 cmd->device)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304993 ql4_printk(KERN_INFO, ha,
Mike Christiece545032008-02-29 18:25:20 -06004994 "DEVICE RESET FAILED - waiting for "
4995 "commands.\n");
4996 goto eh_dev_reset_done;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004997 }
4998
David C Somayajulu9d562912008-03-19 11:23:03 -07004999 /* Send marker. */
5000 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
5001 MM_LUN_RESET) != QLA_SUCCESS)
5002 goto eh_dev_reset_done;
5003
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05305004 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07005005 "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
5006 ha->host_no, cmd->device->channel, cmd->device->id,
5007 cmd->device->lun);
5008
5009 ret = SUCCESS;
5010
5011eh_dev_reset_done:
5012
5013 return ret;
5014}
5015
5016/**
Mike Christiece545032008-02-29 18:25:20 -06005017 * qla4xxx_eh_target_reset - callback for target reset.
5018 * @cmd: Pointer to Linux's SCSI command structure
5019 *
5020 * This routine is called by the Linux OS to reset the target.
5021 **/
5022static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
5023{
5024 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
5025 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christiec01be6d2010-07-22 16:59:49 +05305026 int stat, ret;
Mike Christiece545032008-02-29 18:25:20 -06005027
5028 if (!ddb_entry)
5029 return FAILED;
5030
Mike Christiec01be6d2010-07-22 16:59:49 +05305031 ret = iscsi_block_scsi_eh(cmd);
5032 if (ret)
5033 return ret;
5034
Mike Christiece545032008-02-29 18:25:20 -06005035 starget_printk(KERN_INFO, scsi_target(cmd->device),
5036 "WARM TARGET RESET ISSUED.\n");
5037
5038 DEBUG2(printk(KERN_INFO
5039 "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
5040 "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
Jens Axboe242f9dc2008-09-14 05:55:09 -07005041 ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
Mike Christiece545032008-02-29 18:25:20 -06005042 ha->dpc_flags, cmd->result, cmd->allowed));
5043
5044 stat = qla4xxx_reset_target(ha, ddb_entry);
5045 if (stat != QLA_SUCCESS) {
5046 starget_printk(KERN_INFO, scsi_target(cmd->device),
5047 "WARM TARGET RESET FAILED.\n");
5048 return FAILED;
5049 }
5050
Mike Christiece545032008-02-29 18:25:20 -06005051 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
5052 NULL)) {
5053 starget_printk(KERN_INFO, scsi_target(cmd->device),
5054 "WARM TARGET DEVICE RESET FAILED - "
5055 "waiting for commands.\n");
5056 return FAILED;
5057 }
5058
David C Somayajulu9d562912008-03-19 11:23:03 -07005059 /* Send marker. */
5060 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
5061 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
5062 starget_printk(KERN_INFO, scsi_target(cmd->device),
5063 "WARM TARGET DEVICE RESET FAILED - "
5064 "marker iocb failed.\n");
5065 return FAILED;
5066 }
5067
Mike Christiece545032008-02-29 18:25:20 -06005068 starget_printk(KERN_INFO, scsi_target(cmd->device),
5069 "WARM TARGET RESET SUCCEEDED.\n");
5070 return SUCCESS;
5071}
5072
5073/**
Sarang Radke8a288962011-12-06 02:34:10 -08005074 * qla4xxx_is_eh_active - check if error handler is running
5075 * @shost: Pointer to SCSI Host struct
5076 *
5077 * This routine finds that if reset host is called in EH
5078 * scenario or from some application like sg_reset
5079 **/
5080static int qla4xxx_is_eh_active(struct Scsi_Host *shost)
5081{
5082 if (shost->shost_state == SHOST_RECOVERY)
5083 return 1;
5084 return 0;
5085}
5086
5087/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07005088 * qla4xxx_eh_host_reset - kernel callback
5089 * @cmd: Pointer to Linux's SCSI command structure
5090 *
5091 * This routine is invoked by the Linux kernel to perform fatal error
5092 * recovery on the specified adapter.
5093 **/
5094static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
5095{
5096 int return_status = FAILED;
5097 struct scsi_qla_host *ha;
5098
Manish Rangankarb3a271a2011-07-25 13:48:53 -05005099 ha = to_qla_host(cmd->device->host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07005100
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05305101 if (ql4xdontresethba) {
5102 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
5103 ha->host_no, __func__));
Sarang Radke8a288962011-12-06 02:34:10 -08005104
5105 /* Clear outstanding srb in queues */
5106 if (qla4xxx_is_eh_active(cmd->device->host))
5107 qla4xxx_abort_active_cmds(ha, DID_ABORT << 16);
5108
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05305109 return FAILED;
5110 }
5111
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05305112 ql4_printk(KERN_INFO, ha,
Karen Higginsdca05c42009-07-15 15:03:00 -05005113 "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
David Somayajuluafaf5a22006-09-19 10:28:00 -07005114 cmd->device->channel, cmd->device->id, cmd->device->lun);
5115
5116 if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
5117 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
5118 "DEAD.\n", ha->host_no, cmd->device->channel,
5119 __func__));
5120
5121 return FAILED;
5122 }
5123
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05305124 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
5125 if (is_qla8022(ha))
5126 set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
5127 else
5128 set_bit(DPC_RESET_HA, &ha->dpc_flags);
5129 }
Mike Christie50a29ae2008-03-04 13:26:53 -06005130
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05305131 if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
David Somayajuluafaf5a22006-09-19 10:28:00 -07005132 return_status = SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07005133
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05305134 ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005135 return_status == FAILED ? "FAILED" : "SUCCEEDED");
David Somayajuluafaf5a22006-09-19 10:28:00 -07005136
5137 return return_status;
5138}
5139
Vikas Chaudhary95d31262011-08-12 02:51:29 -07005140static int qla4xxx_context_reset(struct scsi_qla_host *ha)
5141{
5142 uint32_t mbox_cmd[MBOX_REG_COUNT];
5143 uint32_t mbox_sts[MBOX_REG_COUNT];
5144 struct addr_ctrl_blk_def *acb = NULL;
5145 uint32_t acb_len = sizeof(struct addr_ctrl_blk_def);
5146 int rval = QLA_SUCCESS;
5147 dma_addr_t acb_dma;
5148
5149 acb = dma_alloc_coherent(&ha->pdev->dev,
5150 sizeof(struct addr_ctrl_blk_def),
5151 &acb_dma, GFP_KERNEL);
5152 if (!acb) {
5153 ql4_printk(KERN_ERR, ha, "%s: Unable to alloc acb\n",
5154 __func__);
5155 rval = -ENOMEM;
5156 goto exit_port_reset;
5157 }
5158
5159 memset(acb, 0, acb_len);
5160
5161 rval = qla4xxx_get_acb(ha, acb_dma, PRIMARI_ACB, acb_len);
5162 if (rval != QLA_SUCCESS) {
5163 rval = -EIO;
5164 goto exit_free_acb;
5165 }
5166
5167 rval = qla4xxx_disable_acb(ha);
5168 if (rval != QLA_SUCCESS) {
5169 rval = -EIO;
5170 goto exit_free_acb;
5171 }
5172
5173 wait_for_completion_timeout(&ha->disable_acb_comp,
5174 DISABLE_ACB_TOV * HZ);
5175
5176 rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], acb_dma);
5177 if (rval != QLA_SUCCESS) {
5178 rval = -EIO;
5179 goto exit_free_acb;
5180 }
5181
5182exit_free_acb:
5183 dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk_def),
5184 acb, acb_dma);
5185exit_port_reset:
5186 DEBUG2(ql4_printk(KERN_INFO, ha, "%s %s\n", __func__,
5187 rval == QLA_SUCCESS ? "SUCCEEDED" : "FAILED"));
5188 return rval;
5189}
5190
5191static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type)
5192{
5193 struct scsi_qla_host *ha = to_qla_host(shost);
5194 int rval = QLA_SUCCESS;
5195
5196 if (ql4xdontresethba) {
5197 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Don't Reset HBA\n",
5198 __func__));
5199 rval = -EPERM;
5200 goto exit_host_reset;
5201 }
5202
5203 rval = qla4xxx_wait_for_hba_online(ha);
5204 if (rval != QLA_SUCCESS) {
5205 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Unable to reset host "
5206 "adapter\n", __func__));
5207 rval = -EIO;
5208 goto exit_host_reset;
5209 }
5210
5211 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
5212 goto recover_adapter;
5213
5214 switch (reset_type) {
5215 case SCSI_ADAPTER_RESET:
5216 set_bit(DPC_RESET_HA, &ha->dpc_flags);
5217 break;
5218 case SCSI_FIRMWARE_RESET:
5219 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
5220 if (is_qla8022(ha))
5221 /* set firmware context reset */
5222 set_bit(DPC_RESET_HA_FW_CONTEXT,
5223 &ha->dpc_flags);
5224 else {
5225 rval = qla4xxx_context_reset(ha);
5226 goto exit_host_reset;
5227 }
5228 }
5229 break;
5230 }
5231
5232recover_adapter:
5233 rval = qla4xxx_recover_adapter(ha);
5234 if (rval != QLA_SUCCESS) {
5235 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: recover adapter fail\n",
5236 __func__));
5237 rval = -EIO;
5238 }
5239
5240exit_host_reset:
5241 return rval;
5242}
5243
Lalit Chandivade2232be02010-07-30 14:38:47 +05305244/* PCI AER driver recovers from all correctable errors w/o
5245 * driver intervention. For uncorrectable errors PCI AER
5246 * driver calls the following device driver's callbacks
5247 *
5248 * - Fatal Errors - link_reset
5249 * - Non-Fatal Errors - driver's pci_error_detected() which
5250 * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
5251 *
5252 * PCI AER driver calls
5253 * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
5254 * returns RECOVERED or NEED_RESET if fw_hung
5255 * NEED_RESET - driver's slot_reset()
5256 * DISCONNECT - device is dead & cannot recover
5257 * RECOVERED - driver's pci_resume()
5258 */
5259static pci_ers_result_t
5260qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
5261{
5262 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
5263
5264 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
5265 ha->host_no, __func__, state);
5266
5267 if (!is_aer_supported(ha))
5268 return PCI_ERS_RESULT_NONE;
5269
5270 switch (state) {
5271 case pci_channel_io_normal:
5272 clear_bit(AF_EEH_BUSY, &ha->flags);
5273 return PCI_ERS_RESULT_CAN_RECOVER;
5274 case pci_channel_io_frozen:
5275 set_bit(AF_EEH_BUSY, &ha->flags);
5276 qla4xxx_mailbox_premature_completion(ha);
5277 qla4xxx_free_irqs(ha);
5278 pci_disable_device(pdev);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07005279 /* Return back all IOs */
5280 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
Lalit Chandivade2232be02010-07-30 14:38:47 +05305281 return PCI_ERS_RESULT_NEED_RESET;
5282 case pci_channel_io_perm_failure:
5283 set_bit(AF_EEH_BUSY, &ha->flags);
5284 set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
5285 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
5286 return PCI_ERS_RESULT_DISCONNECT;
5287 }
5288 return PCI_ERS_RESULT_NEED_RESET;
5289}
5290
5291/**
5292 * qla4xxx_pci_mmio_enabled() gets called if
5293 * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
5294 * and read/write to the device still works.
5295 **/
5296static pci_ers_result_t
5297qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
5298{
5299 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
5300
5301 if (!is_aer_supported(ha))
5302 return PCI_ERS_RESULT_NONE;
5303
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07005304 return PCI_ERS_RESULT_RECOVERED;
Lalit Chandivade2232be02010-07-30 14:38:47 +05305305}
5306
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07005307static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
Lalit Chandivade2232be02010-07-30 14:38:47 +05305308{
5309 uint32_t rval = QLA_ERROR;
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07005310 uint32_t ret = 0;
Lalit Chandivade2232be02010-07-30 14:38:47 +05305311 int fn;
5312 struct pci_dev *other_pdev = NULL;
5313
5314 ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
5315
5316 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
5317
5318 if (test_bit(AF_ONLINE, &ha->flags)) {
5319 clear_bit(AF_ONLINE, &ha->flags);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05005320 clear_bit(AF_LINK_UP, &ha->flags);
5321 iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
Lalit Chandivade2232be02010-07-30 14:38:47 +05305322 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
Lalit Chandivade2232be02010-07-30 14:38:47 +05305323 }
5324
5325 fn = PCI_FUNC(ha->pdev->devfn);
5326 while (fn > 0) {
5327 fn--;
5328 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
5329 "func %x\n", ha->host_no, __func__, fn);
5330 /* Get the pci device given the domain, bus,
5331 * slot/function number */
5332 other_pdev =
5333 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
5334 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
5335 fn));
5336
5337 if (!other_pdev)
5338 continue;
5339
5340 if (atomic_read(&other_pdev->enable_cnt)) {
5341 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
5342 "func in enabled state%x\n", ha->host_no,
5343 __func__, fn);
5344 pci_dev_put(other_pdev);
5345 break;
5346 }
5347 pci_dev_put(other_pdev);
5348 }
5349
5350 /* The first function on the card, the reset owner will
5351 * start & initialize the firmware. The other functions
5352 * on the card will reset the firmware context
5353 */
5354 if (!fn) {
5355 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
5356 "0x%x is the owner\n", ha->host_no, __func__,
5357 ha->pdev->devfn);
5358
5359 qla4_8xxx_idc_lock(ha);
5360 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
5361 QLA82XX_DEV_COLD);
5362
5363 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
5364 QLA82XX_IDC_VERSION);
5365
5366 qla4_8xxx_idc_unlock(ha);
5367 clear_bit(AF_FW_RECOVERY, &ha->flags);
Mike Christie13483732011-12-01 21:38:41 -06005368 rval = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
Lalit Chandivade2232be02010-07-30 14:38:47 +05305369 qla4_8xxx_idc_lock(ha);
5370
5371 if (rval != QLA_SUCCESS) {
5372 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
5373 "FAILED\n", ha->host_no, __func__);
5374 qla4_8xxx_clear_drv_active(ha);
5375 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
5376 QLA82XX_DEV_FAILED);
5377 } else {
5378 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
5379 "READY\n", ha->host_no, __func__);
5380 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
5381 QLA82XX_DEV_READY);
5382 /* Clear driver state register */
5383 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
5384 qla4_8xxx_set_drv_active(ha);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07005385 ret = qla4xxx_request_irqs(ha);
5386 if (ret) {
5387 ql4_printk(KERN_WARNING, ha, "Failed to "
5388 "reserve interrupt %d already in use.\n",
5389 ha->pdev->irq);
5390 rval = QLA_ERROR;
5391 } else {
5392 ha->isp_ops->enable_intrs(ha);
5393 rval = QLA_SUCCESS;
5394 }
Lalit Chandivade2232be02010-07-30 14:38:47 +05305395 }
5396 qla4_8xxx_idc_unlock(ha);
5397 } else {
5398 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
5399 "the reset owner\n", ha->host_no, __func__,
5400 ha->pdev->devfn);
5401 if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
5402 QLA82XX_DEV_READY)) {
5403 clear_bit(AF_FW_RECOVERY, &ha->flags);
Mike Christie13483732011-12-01 21:38:41 -06005404 rval = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07005405 if (rval == QLA_SUCCESS) {
5406 ret = qla4xxx_request_irqs(ha);
5407 if (ret) {
5408 ql4_printk(KERN_WARNING, ha, "Failed to"
5409 " reserve interrupt %d already in"
5410 " use.\n", ha->pdev->irq);
5411 rval = QLA_ERROR;
5412 } else {
5413 ha->isp_ops->enable_intrs(ha);
5414 rval = QLA_SUCCESS;
5415 }
5416 }
Lalit Chandivade2232be02010-07-30 14:38:47 +05305417 qla4_8xxx_idc_lock(ha);
5418 qla4_8xxx_set_drv_active(ha);
5419 qla4_8xxx_idc_unlock(ha);
5420 }
5421 }
5422 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
5423 return rval;
5424}
5425
5426static pci_ers_result_t
5427qla4xxx_pci_slot_reset(struct pci_dev *pdev)
5428{
5429 pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
5430 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
5431 int rc;
5432
5433 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
5434 ha->host_no, __func__);
5435
5436 if (!is_aer_supported(ha))
5437 return PCI_ERS_RESULT_NONE;
5438
5439 /* Restore the saved state of PCIe device -
5440 * BAR registers, PCI Config space, PCIX, MSI,
5441 * IOV states
5442 */
5443 pci_restore_state(pdev);
5444
5445 /* pci_restore_state() clears the saved_state flag of the device
5446 * save restored state which resets saved_state flag
5447 */
5448 pci_save_state(pdev);
5449
5450 /* Initialize device or resume if in suspended state */
5451 rc = pci_enable_device(pdev);
5452 if (rc) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005453 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Can't re-enable "
Lalit Chandivade2232be02010-07-30 14:38:47 +05305454 "device after reset\n", ha->host_no, __func__);
5455 goto exit_slot_reset;
5456 }
5457
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07005458 ha->isp_ops->disable_intrs(ha);
Lalit Chandivade2232be02010-07-30 14:38:47 +05305459
5460 if (is_qla8022(ha)) {
5461 if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
5462 ret = PCI_ERS_RESULT_RECOVERED;
5463 goto exit_slot_reset;
5464 } else
5465 goto exit_slot_reset;
5466 }
5467
5468exit_slot_reset:
5469 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
5470 "device after reset\n", ha->host_no, __func__, ret);
5471 return ret;
5472}
5473
5474static void
5475qla4xxx_pci_resume(struct pci_dev *pdev)
5476{
5477 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
5478 int ret;
5479
5480 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
5481 ha->host_no, __func__);
5482
5483 ret = qla4xxx_wait_for_hba_online(ha);
5484 if (ret != QLA_SUCCESS) {
5485 ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
5486 "resume I/O from slot/link_reset\n", ha->host_no,
5487 __func__);
5488 }
5489
5490 pci_cleanup_aer_uncorrect_error_status(pdev);
5491 clear_bit(AF_EEH_BUSY, &ha->flags);
5492}
5493
5494static struct pci_error_handlers qla4xxx_err_handler = {
5495 .error_detected = qla4xxx_pci_error_detected,
5496 .mmio_enabled = qla4xxx_pci_mmio_enabled,
5497 .slot_reset = qla4xxx_pci_slot_reset,
5498 .resume = qla4xxx_pci_resume,
5499};
5500
David Somayajuluafaf5a22006-09-19 10:28:00 -07005501static struct pci_device_id qla4xxx_pci_tbl[] = {
5502 {
5503 .vendor = PCI_VENDOR_ID_QLOGIC,
5504 .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
5505 .subvendor = PCI_ANY_ID,
5506 .subdevice = PCI_ANY_ID,
5507 },
5508 {
5509 .vendor = PCI_VENDOR_ID_QLOGIC,
5510 .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
5511 .subvendor = PCI_ANY_ID,
5512 .subdevice = PCI_ANY_ID,
5513 },
David C Somayajulud9150582006-11-15 17:38:40 -08005514 {
5515 .vendor = PCI_VENDOR_ID_QLOGIC,
5516 .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
5517 .subvendor = PCI_ANY_ID,
5518 .subdevice = PCI_ANY_ID,
5519 },
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05305520 {
5521 .vendor = PCI_VENDOR_ID_QLOGIC,
5522 .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
5523 .subvendor = PCI_ANY_ID,
5524 .subdevice = PCI_ANY_ID,
5525 },
David Somayajuluafaf5a22006-09-19 10:28:00 -07005526 {0, 0},
5527};
5528MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
5529
Adrian Bunk47975472007-04-26 00:35:16 -07005530static struct pci_driver qla4xxx_pci_driver = {
David Somayajuluafaf5a22006-09-19 10:28:00 -07005531 .name = DRIVER_NAME,
5532 .id_table = qla4xxx_pci_tbl,
5533 .probe = qla4xxx_probe_adapter,
5534 .remove = qla4xxx_remove_adapter,
Lalit Chandivade2232be02010-07-30 14:38:47 +05305535 .err_handler = &qla4xxx_err_handler,
David Somayajuluafaf5a22006-09-19 10:28:00 -07005536};
5537
5538static int __init qla4xxx_module_init(void)
5539{
5540 int ret;
5541
5542 /* Allocate cache for SRBs. */
5543 srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09005544 SLAB_HWCACHE_ALIGN, NULL);
David Somayajuluafaf5a22006-09-19 10:28:00 -07005545 if (srb_cachep == NULL) {
5546 printk(KERN_ERR
5547 "%s: Unable to allocate SRB cache..."
5548 "Failing load!\n", DRIVER_NAME);
5549 ret = -ENOMEM;
5550 goto no_srp_cache;
5551 }
5552
5553 /* Derive version string. */
5554 strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
Andrew Vasquez11010fe2006-10-06 09:54:59 -07005555 if (ql4xextended_error_logging)
David Somayajuluafaf5a22006-09-19 10:28:00 -07005556 strcat(qla4xxx_version_str, "-debug");
5557
5558 qla4xxx_scsi_transport =
5559 iscsi_register_transport(&qla4xxx_iscsi_transport);
5560 if (!qla4xxx_scsi_transport){
5561 ret = -ENODEV;
5562 goto release_srb_cache;
5563 }
5564
David Somayajuluafaf5a22006-09-19 10:28:00 -07005565 ret = pci_register_driver(&qla4xxx_pci_driver);
5566 if (ret)
5567 goto unregister_transport;
5568
5569 printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
5570 return 0;
Doug Maxey5ae16db2006-10-05 23:50:07 -05005571
David Somayajuluafaf5a22006-09-19 10:28:00 -07005572unregister_transport:
5573 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
5574release_srb_cache:
5575 kmem_cache_destroy(srb_cachep);
5576no_srp_cache:
5577 return ret;
5578}
5579
5580static void __exit qla4xxx_module_exit(void)
5581{
David Somayajuluafaf5a22006-09-19 10:28:00 -07005582 pci_unregister_driver(&qla4xxx_pci_driver);
5583 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
5584 kmem_cache_destroy(srb_cachep);
5585}
5586
5587module_init(qla4xxx_module_init);
5588module_exit(qla4xxx_module_exit);
5589
5590MODULE_AUTHOR("QLogic Corporation");
5591MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
5592MODULE_LICENSE("GPL");
5593MODULE_VERSION(QLA4XXX_DRIVER_VERSION);