blob: 3339cc1e3390b0fb43a3761cba41e51c4b7d9477 [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)) {
Manish Rangankare8fb00e2012-02-13 18:30:43 +05303396 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Adapter boot "
3397 "options : 0x%x\n", __func__, val));
Manish Rangankar2a991c22011-07-25 13:48:55 -05003398 ret = QLA_ERROR;
3399 goto exit_boot_info;
3400 }
3401
3402 /* get primary valid target index */
3403 val = rd_nvram_byte(ha, pri_addr);
3404 if (val & BIT_7)
3405 ddb_index[0] = (val & 0x7f);
Manish Rangankar2a991c22011-07-25 13:48:55 -05003406
3407 /* get secondary valid target index */
3408 val = rd_nvram_byte(ha, sec_addr);
3409 if (val & BIT_7)
3410 ddb_index[1] = (val & 0x7f);
Manish Rangankar2a991c22011-07-25 13:48:55 -05003411
3412 } else if (is_qla8022(ha)) {
3413 buf = dma_alloc_coherent(&ha->pdev->dev, size,
3414 &buf_dma, GFP_KERNEL);
3415 if (!buf) {
3416 DEBUG2(ql4_printk(KERN_ERR, ha,
3417 "%s: Unable to allocate dma buffer\n",
3418 __func__));
3419 ret = QLA_ERROR;
3420 goto exit_boot_info;
3421 }
3422
3423 if (ha->port_num == 0)
3424 offset = BOOT_PARAM_OFFSET_PORT0;
3425 else if (ha->port_num == 1)
3426 offset = BOOT_PARAM_OFFSET_PORT1;
3427 else {
3428 ret = QLA_ERROR;
3429 goto exit_boot_info_free;
3430 }
3431 addr = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_iscsi_param * 4) +
3432 offset;
3433 if (qla4xxx_get_flash(ha, buf_dma, addr,
3434 13 * sizeof(uint8_t)) != QLA_SUCCESS) {
3435 DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: Get Flash"
3436 "failed\n", ha->host_no, __func__));
3437 ret = QLA_ERROR;
3438 goto exit_boot_info_free;
3439 }
3440 /* Check Boot Mode */
3441 if (!(buf[1] & 0x07)) {
Manish Rangankare8fb00e2012-02-13 18:30:43 +05303442 DEBUG2(ql4_printk(KERN_INFO, ha, "Firmware boot options"
3443 " : 0x%x\n", buf[1]));
Manish Rangankar2a991c22011-07-25 13:48:55 -05003444 ret = QLA_ERROR;
3445 goto exit_boot_info_free;
3446 }
3447
3448 /* get primary valid target index */
3449 if (buf[2] & BIT_7)
3450 ddb_index[0] = buf[2] & 0x7f;
Manish Rangankar2a991c22011-07-25 13:48:55 -05003451
3452 /* get secondary valid target index */
3453 if (buf[11] & BIT_7)
3454 ddb_index[1] = buf[11] & 0x7f;
Manish Rangankar2a991c22011-07-25 13:48:55 -05003455 } else {
3456 ret = QLA_ERROR;
3457 goto exit_boot_info;
3458 }
3459
3460 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Primary target ID %d, Secondary"
3461 " target ID %d\n", __func__, ddb_index[0],
3462 ddb_index[1]));
3463
3464exit_boot_info_free:
3465 dma_free_coherent(&ha->pdev->dev, size, buf, buf_dma);
3466exit_boot_info:
Lalit Chandivade20e835b2012-02-13 18:30:42 +05303467 ha->pri_ddb_idx = ddb_index[0];
3468 ha->sec_ddb_idx = ddb_index[1];
Manish Rangankar2a991c22011-07-25 13:48:55 -05003469 return ret;
3470}
3471
Lalit Chandivade28deb452011-10-07 16:55:44 -07003472/**
3473 * qla4xxx_get_bidi_chap - Get a BIDI CHAP user and password
3474 * @ha: pointer to adapter structure
3475 * @username: CHAP username to be returned
3476 * @password: CHAP password to be returned
3477 *
3478 * If a boot entry has BIDI CHAP enabled then we need to set the BIDI CHAP
3479 * user and password in the sysfs entry in /sys/firmware/iscsi_boot#/.
3480 * So from the CHAP cache find the first BIDI CHAP entry and set it
3481 * to the boot record in sysfs.
3482 **/
3483static int qla4xxx_get_bidi_chap(struct scsi_qla_host *ha, char *username,
3484 char *password)
3485{
3486 int i, ret = -EINVAL;
3487 int max_chap_entries = 0;
3488 struct ql4_chap_table *chap_table;
3489
3490 if (is_qla8022(ha))
3491 max_chap_entries = (ha->hw.flt_chap_size / 2) /
3492 sizeof(struct ql4_chap_table);
3493 else
3494 max_chap_entries = MAX_CHAP_ENTRIES_40XX;
3495
3496 if (!ha->chap_list) {
3497 ql4_printk(KERN_ERR, ha, "Do not have CHAP table cache\n");
3498 return ret;
3499 }
3500
3501 mutex_lock(&ha->chap_sem);
3502 for (i = 0; i < max_chap_entries; i++) {
3503 chap_table = (struct ql4_chap_table *)ha->chap_list + i;
3504 if (chap_table->cookie !=
3505 __constant_cpu_to_le16(CHAP_VALID_COOKIE)) {
3506 continue;
3507 }
3508
3509 if (chap_table->flags & BIT_7) /* local */
3510 continue;
3511
3512 if (!(chap_table->flags & BIT_6)) /* Not BIDI */
3513 continue;
3514
3515 strncpy(password, chap_table->secret, QL4_CHAP_MAX_SECRET_LEN);
3516 strncpy(username, chap_table->name, QL4_CHAP_MAX_NAME_LEN);
3517 ret = 0;
3518 break;
3519 }
3520 mutex_unlock(&ha->chap_sem);
3521
3522 return ret;
3523}
3524
3525
Manish Rangankar2a991c22011-07-25 13:48:55 -05003526static int qla4xxx_get_boot_target(struct scsi_qla_host *ha,
3527 struct ql4_boot_session_info *boot_sess,
3528 uint16_t ddb_index)
3529{
3530 struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
3531 struct dev_db_entry *fw_ddb_entry;
3532 dma_addr_t fw_ddb_entry_dma;
3533 uint16_t idx;
3534 uint16_t options;
3535 int ret = QLA_SUCCESS;
3536
3537 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3538 &fw_ddb_entry_dma, GFP_KERNEL);
3539 if (!fw_ddb_entry) {
3540 DEBUG2(ql4_printk(KERN_ERR, ha,
3541 "%s: Unable to allocate dma buffer.\n",
3542 __func__));
3543 ret = QLA_ERROR;
3544 return ret;
3545 }
3546
3547 if (qla4xxx_bootdb_by_index(ha, fw_ddb_entry,
3548 fw_ddb_entry_dma, ddb_index)) {
Manish Rangankare8fb00e2012-02-13 18:30:43 +05303549 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: No Flash DDB found at "
3550 "index [%d]\n", __func__, ddb_index));
Manish Rangankar2a991c22011-07-25 13:48:55 -05003551 ret = QLA_ERROR;
3552 goto exit_boot_target;
3553 }
3554
3555 /* Update target name and IP from DDB */
3556 memcpy(boot_sess->target_name, fw_ddb_entry->iscsi_name,
3557 min(sizeof(boot_sess->target_name),
3558 sizeof(fw_ddb_entry->iscsi_name)));
3559
3560 options = le16_to_cpu(fw_ddb_entry->options);
3561 if (options & DDB_OPT_IPV6_DEVICE) {
3562 memcpy(&boot_conn->dest_ipaddr.ip_address,
3563 &fw_ddb_entry->ip_addr[0], IPv6_ADDR_LEN);
3564 } else {
3565 boot_conn->dest_ipaddr.ip_type = 0x1;
3566 memcpy(&boot_conn->dest_ipaddr.ip_address,
3567 &fw_ddb_entry->ip_addr[0], IP_ADDR_LEN);
3568 }
3569
3570 boot_conn->dest_port = le16_to_cpu(fw_ddb_entry->port);
3571
3572 /* update chap information */
3573 idx = __le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
3574
3575 if (BIT_7 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
3576
3577 DEBUG2(ql4_printk(KERN_INFO, ha, "Setting chap\n"));
3578
3579 ret = qla4xxx_get_chap(ha, (char *)&boot_conn->chap.
3580 target_chap_name,
3581 (char *)&boot_conn->chap.target_secret,
3582 idx);
3583 if (ret) {
3584 ql4_printk(KERN_ERR, ha, "Failed to set chap\n");
3585 ret = QLA_ERROR;
3586 goto exit_boot_target;
3587 }
3588
3589 boot_conn->chap.target_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
3590 boot_conn->chap.target_secret_length = QL4_CHAP_MAX_SECRET_LEN;
3591 }
3592
3593 if (BIT_4 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
3594
3595 DEBUG2(ql4_printk(KERN_INFO, ha, "Setting BIDI chap\n"));
3596
Lalit Chandivade28deb452011-10-07 16:55:44 -07003597 ret = qla4xxx_get_bidi_chap(ha,
3598 (char *)&boot_conn->chap.intr_chap_name,
3599 (char *)&boot_conn->chap.intr_secret);
3600
Manish Rangankar2a991c22011-07-25 13:48:55 -05003601 if (ret) {
3602 ql4_printk(KERN_ERR, ha, "Failed to set BIDI chap\n");
3603 ret = QLA_ERROR;
3604 goto exit_boot_target;
3605 }
3606
3607 boot_conn->chap.intr_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
3608 boot_conn->chap.intr_secret_length = QL4_CHAP_MAX_SECRET_LEN;
3609 }
3610
3611exit_boot_target:
3612 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3613 fw_ddb_entry, fw_ddb_entry_dma);
3614 return ret;
3615}
3616
3617static int qla4xxx_get_boot_info(struct scsi_qla_host *ha)
3618{
3619 uint16_t ddb_index[2];
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003620 int ret = QLA_ERROR;
3621 int rval;
Manish Rangankar2a991c22011-07-25 13:48:55 -05003622
3623 memset(ddb_index, 0, sizeof(ddb_index));
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003624 ddb_index[0] = 0xffff;
3625 ddb_index[1] = 0xffff;
Manish Rangankar2a991c22011-07-25 13:48:55 -05003626 ret = get_fw_boot_info(ha, ddb_index);
3627 if (ret != QLA_SUCCESS) {
Manish Rangankare8fb00e2012-02-13 18:30:43 +05303628 DEBUG2(ql4_printk(KERN_INFO, ha,
3629 "%s: No boot target configured.\n", __func__));
Manish Rangankar2a991c22011-07-25 13:48:55 -05003630 return ret;
3631 }
3632
Mike Christie13483732011-12-01 21:38:41 -06003633 if (ql4xdisablesysfsboot)
3634 return QLA_SUCCESS;
3635
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003636 if (ddb_index[0] == 0xffff)
3637 goto sec_target;
3638
3639 rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_pri_sess),
Manish Rangankar2a991c22011-07-25 13:48:55 -05003640 ddb_index[0]);
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003641 if (rval != QLA_SUCCESS) {
Manish Rangankare8fb00e2012-02-13 18:30:43 +05303642 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Primary boot target not "
3643 "configured\n", __func__));
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003644 } else
3645 ret = QLA_SUCCESS;
Manish Rangankar2a991c22011-07-25 13:48:55 -05003646
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003647sec_target:
3648 if (ddb_index[1] == 0xffff)
3649 goto exit_get_boot_info;
3650
3651 rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_sec_sess),
Manish Rangankar2a991c22011-07-25 13:48:55 -05003652 ddb_index[1]);
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003653 if (rval != QLA_SUCCESS) {
Manish Rangankare8fb00e2012-02-13 18:30:43 +05303654 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Secondary boot target not"
3655 " configured\n", __func__));
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003656 } else
3657 ret = QLA_SUCCESS;
3658
3659exit_get_boot_info:
Manish Rangankar2a991c22011-07-25 13:48:55 -05003660 return ret;
3661}
3662
3663static int qla4xxx_setup_boot_info(struct scsi_qla_host *ha)
3664{
3665 struct iscsi_boot_kobj *boot_kobj;
3666
3667 if (qla4xxx_get_boot_info(ha) != QLA_SUCCESS)
Mike Christie13483732011-12-01 21:38:41 -06003668 return QLA_ERROR;
3669
3670 if (ql4xdisablesysfsboot) {
3671 ql4_printk(KERN_INFO, ha,
3672 "%s: syfsboot disabled - driver will trigger login"
3673 "and publish session for discovery .\n", __func__);
3674 return QLA_SUCCESS;
3675 }
3676
Manish Rangankar2a991c22011-07-25 13:48:55 -05003677
3678 ha->boot_kset = iscsi_boot_create_host_kset(ha->host->host_no);
3679 if (!ha->boot_kset)
3680 goto kset_free;
3681
3682 if (!scsi_host_get(ha->host))
3683 goto kset_free;
3684 boot_kobj = iscsi_boot_create_target(ha->boot_kset, 0, ha,
3685 qla4xxx_show_boot_tgt_pri_info,
3686 qla4xxx_tgt_get_attr_visibility,
3687 qla4xxx_boot_release);
3688 if (!boot_kobj)
3689 goto put_host;
3690
3691 if (!scsi_host_get(ha->host))
3692 goto kset_free;
3693 boot_kobj = iscsi_boot_create_target(ha->boot_kset, 1, ha,
3694 qla4xxx_show_boot_tgt_sec_info,
3695 qla4xxx_tgt_get_attr_visibility,
3696 qla4xxx_boot_release);
3697 if (!boot_kobj)
3698 goto put_host;
3699
3700 if (!scsi_host_get(ha->host))
3701 goto kset_free;
3702 boot_kobj = iscsi_boot_create_initiator(ha->boot_kset, 0, ha,
3703 qla4xxx_show_boot_ini_info,
3704 qla4xxx_ini_get_attr_visibility,
3705 qla4xxx_boot_release);
3706 if (!boot_kobj)
3707 goto put_host;
3708
3709 if (!scsi_host_get(ha->host))
3710 goto kset_free;
3711 boot_kobj = iscsi_boot_create_ethernet(ha->boot_kset, 0, ha,
3712 qla4xxx_show_boot_eth_info,
3713 qla4xxx_eth_get_attr_visibility,
3714 qla4xxx_boot_release);
3715 if (!boot_kobj)
3716 goto put_host;
3717
Mike Christie13483732011-12-01 21:38:41 -06003718 return QLA_SUCCESS;
Manish Rangankar2a991c22011-07-25 13:48:55 -05003719
3720put_host:
3721 scsi_host_put(ha->host);
3722kset_free:
3723 iscsi_boot_destroy_kset(ha->boot_kset);
3724 return -ENOMEM;
3725}
3726
Lalit Chandivade45494152011-10-07 16:55:42 -07003727
3728/**
3729 * qla4xxx_create chap_list - Create CHAP list from FLASH
3730 * @ha: pointer to adapter structure
3731 *
3732 * Read flash and make a list of CHAP entries, during login when a CHAP entry
3733 * is received, it will be checked in this list. If entry exist then the CHAP
3734 * entry index is set in the DDB. If CHAP entry does not exist in this list
3735 * then a new entry is added in FLASH in CHAP table and the index obtained is
3736 * used in the DDB.
3737 **/
3738static void qla4xxx_create_chap_list(struct scsi_qla_host *ha)
3739{
3740 int rval = 0;
3741 uint8_t *chap_flash_data = NULL;
3742 uint32_t offset;
3743 dma_addr_t chap_dma;
3744 uint32_t chap_size = 0;
3745
3746 if (is_qla40XX(ha))
3747 chap_size = MAX_CHAP_ENTRIES_40XX *
3748 sizeof(struct ql4_chap_table);
3749 else /* Single region contains CHAP info for both
3750 * ports which is divided into half for each port.
3751 */
3752 chap_size = ha->hw.flt_chap_size / 2;
3753
3754 chap_flash_data = dma_alloc_coherent(&ha->pdev->dev, chap_size,
3755 &chap_dma, GFP_KERNEL);
3756 if (!chap_flash_data) {
3757 ql4_printk(KERN_ERR, ha, "No memory for chap_flash_data\n");
3758 return;
3759 }
3760 if (is_qla40XX(ha))
3761 offset = FLASH_CHAP_OFFSET;
3762 else {
3763 offset = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_region_chap << 2);
3764 if (ha->port_num == 1)
3765 offset += chap_size;
3766 }
3767
3768 rval = qla4xxx_get_flash(ha, chap_dma, offset, chap_size);
3769 if (rval != QLA_SUCCESS)
3770 goto exit_chap_list;
3771
3772 if (ha->chap_list == NULL)
3773 ha->chap_list = vmalloc(chap_size);
3774 if (ha->chap_list == NULL) {
3775 ql4_printk(KERN_ERR, ha, "No memory for ha->chap_list\n");
3776 goto exit_chap_list;
3777 }
3778
3779 memcpy(ha->chap_list, chap_flash_data, chap_size);
3780
3781exit_chap_list:
3782 dma_free_coherent(&ha->pdev->dev, chap_size,
3783 chap_flash_data, chap_dma);
Lalit Chandivade45494152011-10-07 16:55:42 -07003784}
3785
Mike Christie13483732011-12-01 21:38:41 -06003786static void qla4xxx_get_param_ddb(struct ddb_entry *ddb_entry,
3787 struct ql4_tuple_ddb *tddb)
3788{
3789 struct scsi_qla_host *ha;
3790 struct iscsi_cls_session *cls_sess;
3791 struct iscsi_cls_conn *cls_conn;
3792 struct iscsi_session *sess;
3793 struct iscsi_conn *conn;
3794
3795 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
3796 ha = ddb_entry->ha;
3797 cls_sess = ddb_entry->sess;
3798 sess = cls_sess->dd_data;
3799 cls_conn = ddb_entry->conn;
3800 conn = cls_conn->dd_data;
3801
3802 tddb->tpgt = sess->tpgt;
3803 tddb->port = conn->persistent_port;
3804 strncpy(tddb->iscsi_name, sess->targetname, ISCSI_NAME_SIZE);
3805 strncpy(tddb->ip_addr, conn->persistent_address, DDB_IPADDR_LEN);
3806}
3807
3808static void qla4xxx_convert_param_ddb(struct dev_db_entry *fw_ddb_entry,
3809 struct ql4_tuple_ddb *tddb)
3810{
3811 uint16_t options = 0;
3812
3813 tddb->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
3814 memcpy(&tddb->iscsi_name[0], &fw_ddb_entry->iscsi_name[0],
3815 min(sizeof(tddb->iscsi_name), sizeof(fw_ddb_entry->iscsi_name)));
3816
3817 options = le16_to_cpu(fw_ddb_entry->options);
3818 if (options & DDB_OPT_IPV6_DEVICE)
3819 sprintf(tddb->ip_addr, "%pI6", fw_ddb_entry->ip_addr);
3820 else
3821 sprintf(tddb->ip_addr, "%pI4", fw_ddb_entry->ip_addr);
3822
3823 tddb->port = le16_to_cpu(fw_ddb_entry->port);
3824}
3825
3826static int qla4xxx_compare_tuple_ddb(struct scsi_qla_host *ha,
3827 struct ql4_tuple_ddb *old_tddb,
3828 struct ql4_tuple_ddb *new_tddb)
3829{
3830 if (strcmp(old_tddb->iscsi_name, new_tddb->iscsi_name))
3831 return QLA_ERROR;
3832
3833 if (strcmp(old_tddb->ip_addr, new_tddb->ip_addr))
3834 return QLA_ERROR;
3835
3836 if (old_tddb->port != new_tddb->port)
3837 return QLA_ERROR;
3838
3839 DEBUG2(ql4_printk(KERN_INFO, ha,
3840 "Match Found, fw[%d,%d,%s,%s], [%d,%d,%s,%s]",
3841 old_tddb->port, old_tddb->tpgt, old_tddb->ip_addr,
3842 old_tddb->iscsi_name, new_tddb->port, new_tddb->tpgt,
3843 new_tddb->ip_addr, new_tddb->iscsi_name));
3844
3845 return QLA_SUCCESS;
3846}
3847
3848static int qla4xxx_is_session_exists(struct scsi_qla_host *ha,
3849 struct dev_db_entry *fw_ddb_entry)
3850{
3851 struct ddb_entry *ddb_entry;
3852 struct ql4_tuple_ddb *fw_tddb = NULL;
3853 struct ql4_tuple_ddb *tmp_tddb = NULL;
3854 int idx;
3855 int ret = QLA_ERROR;
3856
3857 fw_tddb = vzalloc(sizeof(*fw_tddb));
3858 if (!fw_tddb) {
3859 DEBUG2(ql4_printk(KERN_WARNING, ha,
3860 "Memory Allocation failed.\n"));
3861 ret = QLA_SUCCESS;
3862 goto exit_check;
3863 }
3864
3865 tmp_tddb = vzalloc(sizeof(*tmp_tddb));
3866 if (!tmp_tddb) {
3867 DEBUG2(ql4_printk(KERN_WARNING, ha,
3868 "Memory Allocation failed.\n"));
3869 ret = QLA_SUCCESS;
3870 goto exit_check;
3871 }
3872
3873 qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb);
3874
3875 for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
3876 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
3877 if (ddb_entry == NULL)
3878 continue;
3879
3880 qla4xxx_get_param_ddb(ddb_entry, tmp_tddb);
3881 if (!qla4xxx_compare_tuple_ddb(ha, fw_tddb, tmp_tddb)) {
3882 ret = QLA_SUCCESS; /* found */
3883 goto exit_check;
3884 }
3885 }
3886
3887exit_check:
3888 if (fw_tddb)
3889 vfree(fw_tddb);
3890 if (tmp_tddb)
3891 vfree(tmp_tddb);
3892 return ret;
3893}
3894
3895static int qla4xxx_is_flash_ddb_exists(struct scsi_qla_host *ha,
3896 struct list_head *list_nt,
3897 struct dev_db_entry *fw_ddb_entry)
3898{
3899 struct qla_ddb_index *nt_ddb_idx, *nt_ddb_idx_tmp;
3900 struct ql4_tuple_ddb *fw_tddb = NULL;
3901 struct ql4_tuple_ddb *tmp_tddb = NULL;
3902 int ret = QLA_ERROR;
3903
3904 fw_tddb = vzalloc(sizeof(*fw_tddb));
3905 if (!fw_tddb) {
3906 DEBUG2(ql4_printk(KERN_WARNING, ha,
3907 "Memory Allocation failed.\n"));
3908 ret = QLA_SUCCESS;
3909 goto exit_check;
3910 }
3911
3912 tmp_tddb = vzalloc(sizeof(*tmp_tddb));
3913 if (!tmp_tddb) {
3914 DEBUG2(ql4_printk(KERN_WARNING, ha,
3915 "Memory Allocation failed.\n"));
3916 ret = QLA_SUCCESS;
3917 goto exit_check;
3918 }
3919
3920 qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb);
3921
3922 list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
3923 qla4xxx_convert_param_ddb(&nt_ddb_idx->fw_ddb, tmp_tddb);
3924 if (!qla4xxx_compare_tuple_ddb(ha, fw_tddb, tmp_tddb)) {
3925 ret = QLA_SUCCESS; /* found */
3926 goto exit_check;
3927 }
3928 }
3929
3930exit_check:
3931 if (fw_tddb)
3932 vfree(fw_tddb);
3933 if (tmp_tddb)
3934 vfree(tmp_tddb);
3935 return ret;
3936}
3937
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08003938static void qla4xxx_free_ddb_list(struct list_head *list_ddb)
Mike Christie13483732011-12-01 21:38:41 -06003939{
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08003940 struct qla_ddb_index *ddb_idx, *ddb_idx_tmp;
Mike Christie13483732011-12-01 21:38:41 -06003941
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08003942 list_for_each_entry_safe(ddb_idx, ddb_idx_tmp, list_ddb, list) {
3943 list_del_init(&ddb_idx->list);
3944 vfree(ddb_idx);
Mike Christie13483732011-12-01 21:38:41 -06003945 }
Mike Christie13483732011-12-01 21:38:41 -06003946}
3947
3948static struct iscsi_endpoint *qla4xxx_get_ep_fwdb(struct scsi_qla_host *ha,
3949 struct dev_db_entry *fw_ddb_entry)
3950{
3951 struct iscsi_endpoint *ep;
3952 struct sockaddr_in *addr;
3953 struct sockaddr_in6 *addr6;
3954 struct sockaddr *dst_addr;
3955 char *ip;
3956
3957 /* TODO: need to destroy on unload iscsi_endpoint*/
3958 dst_addr = vmalloc(sizeof(*dst_addr));
3959 if (!dst_addr)
3960 return NULL;
3961
3962 if (fw_ddb_entry->options & DDB_OPT_IPV6_DEVICE) {
3963 dst_addr->sa_family = AF_INET6;
3964 addr6 = (struct sockaddr_in6 *)dst_addr;
3965 ip = (char *)&addr6->sin6_addr;
3966 memcpy(ip, fw_ddb_entry->ip_addr, IPv6_ADDR_LEN);
3967 addr6->sin6_port = htons(le16_to_cpu(fw_ddb_entry->port));
3968
3969 } else {
3970 dst_addr->sa_family = AF_INET;
3971 addr = (struct sockaddr_in *)dst_addr;
3972 ip = (char *)&addr->sin_addr;
3973 memcpy(ip, fw_ddb_entry->ip_addr, IP_ADDR_LEN);
3974 addr->sin_port = htons(le16_to_cpu(fw_ddb_entry->port));
3975 }
3976
3977 ep = qla4xxx_ep_connect(ha->host, dst_addr, 0);
3978 vfree(dst_addr);
3979 return ep;
3980}
3981
3982static int qla4xxx_verify_boot_idx(struct scsi_qla_host *ha, uint16_t idx)
3983{
3984 if (ql4xdisablesysfsboot)
3985 return QLA_SUCCESS;
3986 if (idx == ha->pri_ddb_idx || idx == ha->sec_ddb_idx)
3987 return QLA_ERROR;
3988 return QLA_SUCCESS;
3989}
3990
3991static void qla4xxx_setup_flash_ddb_entry(struct scsi_qla_host *ha,
3992 struct ddb_entry *ddb_entry)
3993{
Nilesh Javalic28eaac2011-12-18 21:40:44 -08003994 uint16_t def_timeout;
3995
Mike Christie13483732011-12-01 21:38:41 -06003996 ddb_entry->ddb_type = FLASH_DDB;
3997 ddb_entry->fw_ddb_index = INVALID_ENTRY;
3998 ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
3999 ddb_entry->ha = ha;
4000 ddb_entry->unblock_sess = qla4xxx_unblock_flash_ddb;
4001 ddb_entry->ddb_change = qla4xxx_flash_ddb_change;
4002
4003 atomic_set(&ddb_entry->retry_relogin_timer, INVALID_ENTRY);
4004 atomic_set(&ddb_entry->relogin_timer, 0);
4005 atomic_set(&ddb_entry->relogin_retry_count, 0);
Nilesh Javalic28eaac2011-12-18 21:40:44 -08004006 def_timeout = le16_to_cpu(ddb_entry->fw_ddb_entry.def_timeout);
Mike Christie13483732011-12-01 21:38:41 -06004007 ddb_entry->default_relogin_timeout =
Nilesh Javalic28eaac2011-12-18 21:40:44 -08004008 (def_timeout > LOGIN_TOV) && (def_timeout < LOGIN_TOV * 10) ?
4009 def_timeout : LOGIN_TOV;
Mike Christie13483732011-12-01 21:38:41 -06004010 ddb_entry->default_time2wait =
4011 le16_to_cpu(ddb_entry->fw_ddb_entry.iscsi_def_time2wait);
4012}
4013
4014static void qla4xxx_wait_for_ip_configuration(struct scsi_qla_host *ha)
4015{
4016 uint32_t idx = 0;
4017 uint32_t ip_idx[IP_ADDR_COUNT] = {0, 1, 2, 3}; /* 4 IP interfaces */
4018 uint32_t sts[MBOX_REG_COUNT];
4019 uint32_t ip_state;
4020 unsigned long wtime;
4021 int ret;
4022
4023 wtime = jiffies + (HZ * IP_CONFIG_TOV);
4024 do {
4025 for (idx = 0; idx < IP_ADDR_COUNT; idx++) {
4026 if (ip_idx[idx] == -1)
4027 continue;
4028
4029 ret = qla4xxx_get_ip_state(ha, 0, ip_idx[idx], sts);
4030
4031 if (ret == QLA_ERROR) {
4032 ip_idx[idx] = -1;
4033 continue;
4034 }
4035
4036 ip_state = (sts[1] & IP_STATE_MASK) >> IP_STATE_SHIFT;
4037
4038 DEBUG2(ql4_printk(KERN_INFO, ha,
4039 "Waiting for IP state for idx = %d, state = 0x%x\n",
4040 ip_idx[idx], ip_state));
4041 if (ip_state == IP_ADDRSTATE_UNCONFIGURED ||
4042 ip_state == IP_ADDRSTATE_INVALID ||
4043 ip_state == IP_ADDRSTATE_PREFERRED ||
4044 ip_state == IP_ADDRSTATE_DEPRICATED ||
4045 ip_state == IP_ADDRSTATE_DISABLING)
4046 ip_idx[idx] = -1;
Mike Christie13483732011-12-01 21:38:41 -06004047 }
4048
4049 /* Break if all IP states checked */
4050 if ((ip_idx[0] == -1) &&
4051 (ip_idx[1] == -1) &&
4052 (ip_idx[2] == -1) &&
4053 (ip_idx[3] == -1))
4054 break;
4055 schedule_timeout_uninterruptible(HZ);
4056 } while (time_after(wtime, jiffies));
4057}
4058
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004059static void qla4xxx_build_st_list(struct scsi_qla_host *ha,
4060 struct list_head *list_st)
Mike Christie13483732011-12-01 21:38:41 -06004061{
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004062 struct qla_ddb_index *st_ddb_idx;
Mike Christie13483732011-12-01 21:38:41 -06004063 int max_ddbs;
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004064 int fw_idx_size;
4065 struct dev_db_entry *fw_ddb_entry;
4066 dma_addr_t fw_ddb_dma;
Mike Christie13483732011-12-01 21:38:41 -06004067 int ret;
4068 uint32_t idx = 0, next_idx = 0;
4069 uint32_t state = 0, conn_err = 0;
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004070 uint16_t conn_id = 0;
Mike Christie13483732011-12-01 21:38:41 -06004071
4072 fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
4073 &fw_ddb_dma);
4074 if (fw_ddb_entry == NULL) {
4075 DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004076 goto exit_st_list;
Mike Christie13483732011-12-01 21:38:41 -06004077 }
4078
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004079 max_ddbs = is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
4080 MAX_DEV_DB_ENTRIES;
Mike Christie13483732011-12-01 21:38:41 -06004081 fw_idx_size = sizeof(struct qla_ddb_index);
4082
4083 for (idx = 0; idx < max_ddbs; idx = next_idx) {
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004084 ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry, fw_ddb_dma,
4085 NULL, &next_idx, &state,
4086 &conn_err, NULL, &conn_id);
Mike Christie13483732011-12-01 21:38:41 -06004087 if (ret == QLA_ERROR)
4088 break;
4089
Lalit Chandivade981c9822012-02-13 18:30:41 +05304090 /* Ignore DDB if invalid state (unassigned) */
4091 if (state == DDB_DS_UNASSIGNED)
4092 goto continue_next_st;
4093
Mike Christie13483732011-12-01 21:38:41 -06004094 /* Check if ST, add to the list_st */
4095 if (strlen((char *) fw_ddb_entry->iscsi_name) != 0)
4096 goto continue_next_st;
4097
4098 st_ddb_idx = vzalloc(fw_idx_size);
4099 if (!st_ddb_idx)
4100 break;
4101
4102 st_ddb_idx->fw_ddb_idx = idx;
4103
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004104 list_add_tail(&st_ddb_idx->list, list_st);
Mike Christie13483732011-12-01 21:38:41 -06004105continue_next_st:
4106 if (next_idx == 0)
4107 break;
4108 }
4109
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004110exit_st_list:
4111 if (fw_ddb_entry)
4112 dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
4113}
4114
4115/**
4116 * qla4xxx_remove_failed_ddb - Remove inactive or failed ddb from list
4117 * @ha: pointer to adapter structure
4118 * @list_ddb: List from which failed ddb to be removed
4119 *
4120 * Iterate over the list of DDBs and find and remove DDBs that are either in
4121 * no connection active state or failed state
4122 **/
4123static void qla4xxx_remove_failed_ddb(struct scsi_qla_host *ha,
4124 struct list_head *list_ddb)
4125{
4126 struct qla_ddb_index *ddb_idx, *ddb_idx_tmp;
4127 uint32_t next_idx = 0;
4128 uint32_t state = 0, conn_err = 0;
4129 int ret;
4130
4131 list_for_each_entry_safe(ddb_idx, ddb_idx_tmp, list_ddb, list) {
4132 ret = qla4xxx_get_fwddb_entry(ha, ddb_idx->fw_ddb_idx,
4133 NULL, 0, NULL, &next_idx, &state,
4134 &conn_err, NULL, NULL);
4135 if (ret == QLA_ERROR)
4136 continue;
4137
4138 if (state == DDB_DS_NO_CONNECTION_ACTIVE ||
4139 state == DDB_DS_SESSION_FAILED) {
4140 list_del_init(&ddb_idx->list);
4141 vfree(ddb_idx);
4142 }
4143 }
4144}
4145
4146static int qla4xxx_sess_conn_setup(struct scsi_qla_host *ha,
4147 struct dev_db_entry *fw_ddb_entry,
4148 int is_reset)
4149{
4150 struct iscsi_cls_session *cls_sess;
4151 struct iscsi_session *sess;
4152 struct iscsi_cls_conn *cls_conn;
4153 struct iscsi_endpoint *ep;
4154 uint16_t cmds_max = 32;
4155 uint16_t conn_id = 0;
4156 uint32_t initial_cmdsn = 0;
4157 int ret = QLA_SUCCESS;
4158
4159 struct ddb_entry *ddb_entry = NULL;
4160
4161 /* Create session object, with INVALID_ENTRY,
4162 * the targer_id would get set when we issue the login
4163 */
4164 cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport, ha->host,
4165 cmds_max, sizeof(struct ddb_entry),
4166 sizeof(struct ql4_task_data),
4167 initial_cmdsn, INVALID_ENTRY);
4168 if (!cls_sess) {
4169 ret = QLA_ERROR;
4170 goto exit_setup;
4171 }
4172
4173 /*
4174 * so calling module_put function to decrement the
4175 * reference count.
4176 **/
4177 module_put(qla4xxx_iscsi_transport.owner);
4178 sess = cls_sess->dd_data;
4179 ddb_entry = sess->dd_data;
4180 ddb_entry->sess = cls_sess;
4181
4182 cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
4183 memcpy(&ddb_entry->fw_ddb_entry, fw_ddb_entry,
4184 sizeof(struct dev_db_entry));
4185
4186 qla4xxx_setup_flash_ddb_entry(ha, ddb_entry);
4187
4188 cls_conn = iscsi_conn_setup(cls_sess, sizeof(struct qla_conn), conn_id);
4189
4190 if (!cls_conn) {
4191 ret = QLA_ERROR;
4192 goto exit_setup;
4193 }
4194
4195 ddb_entry->conn = cls_conn;
4196
4197 /* Setup ep, for displaying attributes in sysfs */
4198 ep = qla4xxx_get_ep_fwdb(ha, fw_ddb_entry);
4199 if (ep) {
4200 ep->conn = cls_conn;
4201 cls_conn->ep = ep;
4202 } else {
4203 DEBUG2(ql4_printk(KERN_ERR, ha, "Unable to get ep\n"));
4204 ret = QLA_ERROR;
4205 goto exit_setup;
4206 }
4207
4208 /* Update sess/conn params */
4209 qla4xxx_copy_fwddb_param(ha, fw_ddb_entry, cls_sess, cls_conn);
4210
4211 if (is_reset == RESET_ADAPTER) {
4212 iscsi_block_session(cls_sess);
4213 /* Use the relogin path to discover new devices
4214 * by short-circuting the logic of setting
4215 * timer to relogin - instead set the flags
4216 * to initiate login right away.
4217 */
4218 set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
4219 set_bit(DF_RELOGIN, &ddb_entry->flags);
4220 }
4221
4222exit_setup:
4223 return ret;
4224}
4225
4226static void qla4xxx_build_nt_list(struct scsi_qla_host *ha,
4227 struct list_head *list_nt, int is_reset)
4228{
4229 struct dev_db_entry *fw_ddb_entry;
4230 dma_addr_t fw_ddb_dma;
4231 int max_ddbs;
4232 int fw_idx_size;
4233 int ret;
4234 uint32_t idx = 0, next_idx = 0;
4235 uint32_t state = 0, conn_err = 0;
4236 uint16_t conn_id = 0;
4237 struct qla_ddb_index *nt_ddb_idx;
4238
4239 fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
4240 &fw_ddb_dma);
4241 if (fw_ddb_entry == NULL) {
4242 DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
4243 goto exit_nt_list;
4244 }
4245 max_ddbs = is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
4246 MAX_DEV_DB_ENTRIES;
4247 fw_idx_size = sizeof(struct qla_ddb_index);
4248
4249 for (idx = 0; idx < max_ddbs; idx = next_idx) {
4250 ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry, fw_ddb_dma,
4251 NULL, &next_idx, &state,
4252 &conn_err, NULL, &conn_id);
4253 if (ret == QLA_ERROR)
4254 break;
4255
4256 if (qla4xxx_verify_boot_idx(ha, idx) != QLA_SUCCESS)
4257 goto continue_next_nt;
4258
4259 /* Check if NT, then add to list it */
4260 if (strlen((char *) fw_ddb_entry->iscsi_name) == 0)
4261 goto continue_next_nt;
4262
4263 if (!(state == DDB_DS_NO_CONNECTION_ACTIVE ||
4264 state == DDB_DS_SESSION_FAILED))
4265 goto continue_next_nt;
4266
4267 DEBUG2(ql4_printk(KERN_INFO, ha,
4268 "Adding DDB to session = 0x%x\n", idx));
4269 if (is_reset == INIT_ADAPTER) {
4270 nt_ddb_idx = vmalloc(fw_idx_size);
4271 if (!nt_ddb_idx)
4272 break;
4273
4274 nt_ddb_idx->fw_ddb_idx = idx;
4275
4276 memcpy(&nt_ddb_idx->fw_ddb, fw_ddb_entry,
4277 sizeof(struct dev_db_entry));
4278
4279 if (qla4xxx_is_flash_ddb_exists(ha, list_nt,
4280 fw_ddb_entry) == QLA_SUCCESS) {
4281 vfree(nt_ddb_idx);
4282 goto continue_next_nt;
4283 }
4284 list_add_tail(&nt_ddb_idx->list, list_nt);
4285 } else if (is_reset == RESET_ADAPTER) {
4286 if (qla4xxx_is_session_exists(ha, fw_ddb_entry) ==
4287 QLA_SUCCESS)
4288 goto continue_next_nt;
4289 }
4290
4291 ret = qla4xxx_sess_conn_setup(ha, fw_ddb_entry, is_reset);
4292 if (ret == QLA_ERROR)
4293 goto exit_nt_list;
4294
4295continue_next_nt:
4296 if (next_idx == 0)
4297 break;
4298 }
4299
4300exit_nt_list:
4301 if (fw_ddb_entry)
4302 dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
4303}
4304
4305/**
4306 * qla4xxx_build_ddb_list - Build ddb list and setup sessions
4307 * @ha: pointer to adapter structure
4308 * @is_reset: Is this init path or reset path
4309 *
4310 * Create a list of sendtargets (st) from firmware DDBs, issue send targets
4311 * using connection open, then create the list of normal targets (nt)
4312 * from firmware DDBs. Based on the list of nt setup session and connection
4313 * objects.
4314 **/
4315void qla4xxx_build_ddb_list(struct scsi_qla_host *ha, int is_reset)
4316{
4317 uint16_t tmo = 0;
4318 struct list_head list_st, list_nt;
4319 struct qla_ddb_index *st_ddb_idx, *st_ddb_idx_tmp;
4320 unsigned long wtime;
4321
4322 if (!test_bit(AF_LINK_UP, &ha->flags)) {
4323 set_bit(AF_BUILD_DDB_LIST, &ha->flags);
4324 ha->is_reset = is_reset;
4325 return;
4326 }
4327
4328 INIT_LIST_HEAD(&list_st);
4329 INIT_LIST_HEAD(&list_nt);
4330
4331 qla4xxx_build_st_list(ha, &list_st);
4332
Mike Christie13483732011-12-01 21:38:41 -06004333 /* Before issuing conn open mbox, ensure all IPs states are configured
4334 * Note, conn open fails if IPs are not configured
4335 */
4336 qla4xxx_wait_for_ip_configuration(ha);
4337
4338 /* Go thru the STs and fire the sendtargets by issuing conn open mbx */
4339 list_for_each_entry_safe(st_ddb_idx, st_ddb_idx_tmp, &list_st, list) {
4340 qla4xxx_conn_open(ha, st_ddb_idx->fw_ddb_idx);
4341 }
4342
4343 /* Wait to ensure all sendtargets are done for min 12 sec wait */
Nilesh Javalic28eaac2011-12-18 21:40:44 -08004344 tmo = ((ha->def_timeout > LOGIN_TOV) &&
4345 (ha->def_timeout < LOGIN_TOV * 10) ?
4346 ha->def_timeout : LOGIN_TOV);
4347
Mike Christie13483732011-12-01 21:38:41 -06004348 DEBUG2(ql4_printk(KERN_INFO, ha,
4349 "Default time to wait for build ddb %d\n", tmo));
4350
4351 wtime = jiffies + (HZ * tmo);
4352 do {
Nilesh Javalif1f2e602011-12-16 01:58:57 -08004353 if (list_empty(&list_st))
4354 break;
4355
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004356 qla4xxx_remove_failed_ddb(ha, &list_st);
Mike Christie13483732011-12-01 21:38:41 -06004357 schedule_timeout_uninterruptible(HZ / 10);
4358 } while (time_after(wtime, jiffies));
4359
4360 /* Free up the sendtargets list */
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004361 qla4xxx_free_ddb_list(&list_st);
Mike Christie13483732011-12-01 21:38:41 -06004362
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004363 qla4xxx_build_nt_list(ha, &list_nt, is_reset);
Mike Christie13483732011-12-01 21:38:41 -06004364
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004365 qla4xxx_free_ddb_list(&list_nt);
Mike Christie13483732011-12-01 21:38:41 -06004366
4367 qla4xxx_free_ddb_index(ha);
4368}
4369
David Somayajuluafaf5a22006-09-19 10:28:00 -07004370/**
4371 * qla4xxx_probe_adapter - callback function to probe HBA
4372 * @pdev: pointer to pci_dev structure
4373 * @pci_device_id: pointer to pci_device entry
4374 *
4375 * This routine will probe for Qlogic 4xxx iSCSI host adapters.
4376 * It returns zero if successful. It also initializes all data necessary for
4377 * the driver.
4378 **/
4379static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
4380 const struct pci_device_id *ent)
4381{
4382 int ret = -ENODEV, status;
4383 struct Scsi_Host *host;
4384 struct scsi_qla_host *ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004385 uint8_t init_retry_count = 0;
4386 char buf[34];
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304387 struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
Prasanna Mumbaif9880e72011-03-21 03:34:26 -07004388 uint32_t dev_state;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004389
4390 if (pci_enable_device(pdev))
4391 return -1;
4392
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004393 host = iscsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha), 0);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004394 if (host == NULL) {
4395 printk(KERN_WARNING
4396 "qla4xxx: Couldn't allocate host from scsi layer!\n");
4397 goto probe_disable_device;
4398 }
4399
4400 /* Clear our data area */
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004401 ha = to_qla_host(host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004402 memset(ha, 0, sizeof(*ha));
4403
4404 /* Save the information from PCI BIOS. */
4405 ha->pdev = pdev;
4406 ha->host = host;
4407 ha->host_no = host->host_no;
4408
Lalit Chandivade2232be02010-07-30 14:38:47 +05304409 pci_enable_pcie_error_reporting(pdev);
4410
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304411 /* Setup Runtime configurable options */
4412 if (is_qla8022(ha)) {
4413 ha->isp_ops = &qla4_8xxx_isp_ops;
4414 rwlock_init(&ha->hw_lock);
4415 ha->qdr_sn_window = -1;
4416 ha->ddr_mn_window = -1;
4417 ha->curr_window = 255;
4418 ha->func_num = PCI_FUNC(ha->pdev->devfn);
4419 nx_legacy_intr = &legacy_intr[ha->func_num];
4420 ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
4421 ha->nx_legacy_intr.tgt_status_reg =
4422 nx_legacy_intr->tgt_status_reg;
4423 ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
4424 ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
4425 } else {
4426 ha->isp_ops = &qla4xxx_isp_ops;
4427 }
4428
Lalit Chandivade2232be02010-07-30 14:38:47 +05304429 /* Set EEH reset type to fundamental if required by hba */
4430 if (is_qla8022(ha))
4431 pdev->needs_freset = 1;
4432
David Somayajuluafaf5a22006-09-19 10:28:00 -07004433 /* Configure PCI I/O space. */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304434 ret = ha->isp_ops->iospace_config(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004435 if (ret)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304436 goto probe_failed_ioconfig;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004437
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304438 ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
David Somayajuluafaf5a22006-09-19 10:28:00 -07004439 pdev->device, pdev->irq, ha->reg);
4440
4441 qla4xxx_config_dma_addressing(ha);
4442
4443 /* Initialize lists and spinlocks. */
David Somayajuluafaf5a22006-09-19 10:28:00 -07004444 INIT_LIST_HEAD(&ha->free_srb_q);
4445
4446 mutex_init(&ha->mbox_sem);
Lalit Chandivade45494152011-10-07 16:55:42 -07004447 mutex_init(&ha->chap_sem);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304448 init_completion(&ha->mbx_intr_comp);
Vikas Chaudhary95d31262011-08-12 02:51:29 -07004449 init_completion(&ha->disable_acb_comp);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004450
4451 spin_lock_init(&ha->hardware_lock);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004452
4453 /* Allocate dma buffers */
4454 if (qla4xxx_mem_alloc(ha)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304455 ql4_printk(KERN_WARNING, ha,
4456 "[ERROR] Failed to allocate memory for adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07004457
4458 ret = -ENOMEM;
4459 goto probe_failed;
4460 }
4461
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004462 host->cmd_per_lun = 3;
4463 host->max_channel = 0;
4464 host->max_lun = MAX_LUNS - 1;
4465 host->max_id = MAX_TARGETS;
4466 host->max_cmd_len = IOCB_MAX_CDB_LEN;
4467 host->can_queue = MAX_SRBS ;
4468 host->transportt = qla4xxx_scsi_transport;
4469
4470 ret = scsi_init_shared_tag_map(host, MAX_SRBS);
4471 if (ret) {
4472 ql4_printk(KERN_WARNING, ha,
4473 "%s: scsi_init_shared_tag_map failed\n", __func__);
4474 goto probe_failed;
4475 }
4476
4477 pci_set_drvdata(pdev, ha);
4478
4479 ret = scsi_add_host(host, &pdev->dev);
4480 if (ret)
4481 goto probe_failed;
4482
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304483 if (is_qla8022(ha))
4484 (void) qla4_8xxx_get_flash_info(ha);
4485
David Somayajuluafaf5a22006-09-19 10:28:00 -07004486 /*
4487 * Initialize the Host adapter request/response queues and
4488 * firmware
4489 * NOTE: interrupts enabled upon successful completion
4490 */
Mike Christie13483732011-12-01 21:38:41 -06004491 status = qla4xxx_initialize_adapter(ha, INIT_ADAPTER);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304492 while ((!test_bit(AF_ONLINE, &ha->flags)) &&
4493 init_retry_count++ < MAX_INIT_RETRIES) {
Prasanna Mumbaif9880e72011-03-21 03:34:26 -07004494
4495 if (is_qla8022(ha)) {
4496 qla4_8xxx_idc_lock(ha);
4497 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
4498 qla4_8xxx_idc_unlock(ha);
4499 if (dev_state == QLA82XX_DEV_FAILED) {
4500 ql4_printk(KERN_WARNING, ha, "%s: don't retry "
4501 "initialize adapter. H/W is in failed state\n",
4502 __func__);
4503 break;
4504 }
4505 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07004506 DEBUG2(printk("scsi: %s: retrying adapter initialization "
4507 "(%d)\n", __func__, init_retry_count));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304508
4509 if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
4510 continue;
4511
Mike Christie13483732011-12-01 21:38:41 -06004512 status = qla4xxx_initialize_adapter(ha, INIT_ADAPTER);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004513 }
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304514
4515 if (!test_bit(AF_ONLINE, &ha->flags)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304516 ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07004517
Lalit Chandivadefe998522010-12-02 22:12:36 -08004518 if (is_qla8022(ha) && ql4xdontresethba) {
4519 /* Put the device in failed state. */
4520 DEBUG2(printk(KERN_ERR "HW STATE: FAILED\n"));
4521 qla4_8xxx_idc_lock(ha);
4522 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
4523 QLA82XX_DEV_FAILED);
4524 qla4_8xxx_idc_unlock(ha);
4525 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07004526 ret = -ENODEV;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004527 goto remove_host;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004528 }
4529
David Somayajuluafaf5a22006-09-19 10:28:00 -07004530 /* Startup the kernel thread for this host adapter. */
4531 DEBUG2(printk("scsi: %s: Starting kernel thread for "
4532 "qla4xxx_dpc\n", __func__));
4533 sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
4534 ha->dpc_thread = create_singlethread_workqueue(buf);
4535 if (!ha->dpc_thread) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304536 ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07004537 ret = -ENODEV;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004538 goto remove_host;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004539 }
David Howellsc4028952006-11-22 14:57:56 +00004540 INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004541
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004542 sprintf(buf, "qla4xxx_%lu_task", ha->host_no);
4543 ha->task_wq = alloc_workqueue(buf, WQ_MEM_RECLAIM, 1);
4544 if (!ha->task_wq) {
4545 ql4_printk(KERN_WARNING, ha, "Unable to start task thread!\n");
4546 ret = -ENODEV;
4547 goto remove_host;
4548 }
4549
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304550 /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
4551 * (which is called indirectly by qla4xxx_initialize_adapter),
4552 * so that irqs will be registered after crbinit but before
4553 * mbx_intr_enable.
4554 */
4555 if (!is_qla8022(ha)) {
4556 ret = qla4xxx_request_irqs(ha);
4557 if (ret) {
4558 ql4_printk(KERN_WARNING, ha, "Failed to reserve "
4559 "interrupt %d already in use.\n", pdev->irq);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004560 goto remove_host;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304561 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07004562 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07004563
Lalit Chandivade2232be02010-07-30 14:38:47 +05304564 pci_save_state(ha->pdev);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304565 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004566
4567 /* Start timer thread. */
4568 qla4xxx_start_timer(ha, qla4xxx_timer, 1);
4569
4570 set_bit(AF_INIT_DONE, &ha->flags);
4571
David Somayajuluafaf5a22006-09-19 10:28:00 -07004572 printk(KERN_INFO
4573 " QLogic iSCSI HBA Driver version: %s\n"
4574 " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
4575 qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
4576 ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
4577 ha->patch_number, ha->build_number);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05004578
Manish Rangankar2a991c22011-07-25 13:48:55 -05004579 if (qla4xxx_setup_boot_info(ha))
4580 ql4_printk(KERN_ERR, ha, "%s:ISCSI boot info setup failed\n",
4581 __func__);
4582
Mike Christie13483732011-12-01 21:38:41 -06004583 /* Perform the build ddb list and login to each */
4584 qla4xxx_build_ddb_list(ha, INIT_ADAPTER);
4585 iscsi_host_for_each_session(ha->host, qla4xxx_login_flash_ddb);
4586
4587 qla4xxx_create_chap_list(ha);
4588
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05004589 qla4xxx_create_ifaces(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004590 return 0;
4591
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004592remove_host:
4593 scsi_remove_host(ha->host);
4594
David Somayajuluafaf5a22006-09-19 10:28:00 -07004595probe_failed:
4596 qla4xxx_free_adapter(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304597
4598probe_failed_ioconfig:
Lalit Chandivade2232be02010-07-30 14:38:47 +05304599 pci_disable_pcie_error_reporting(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004600 scsi_host_put(ha->host);
4601
4602probe_disable_device:
4603 pci_disable_device(pdev);
4604
4605 return ret;
4606}
4607
4608/**
Karen Higgins7eece5a2011-03-21 03:34:29 -07004609 * qla4xxx_prevent_other_port_reinit - prevent other port from re-initialize
4610 * @ha: pointer to adapter structure
4611 *
4612 * Mark the other ISP-4xxx port to indicate that the driver is being removed,
4613 * so that the other port will not re-initialize while in the process of
4614 * removing the ha due to driver unload or hba hotplug.
4615 **/
4616static void qla4xxx_prevent_other_port_reinit(struct scsi_qla_host *ha)
4617{
4618 struct scsi_qla_host *other_ha = NULL;
4619 struct pci_dev *other_pdev = NULL;
4620 int fn = ISP4XXX_PCI_FN_2;
4621
4622 /*iscsi function numbers for ISP4xxx is 1 and 3*/
4623 if (PCI_FUNC(ha->pdev->devfn) & BIT_1)
4624 fn = ISP4XXX_PCI_FN_1;
4625
4626 other_pdev =
4627 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
4628 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
4629 fn));
4630
4631 /* Get other_ha if other_pdev is valid and state is enable*/
4632 if (other_pdev) {
4633 if (atomic_read(&other_pdev->enable_cnt)) {
4634 other_ha = pci_get_drvdata(other_pdev);
4635 if (other_ha) {
4636 set_bit(AF_HA_REMOVAL, &other_ha->flags);
4637 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: "
4638 "Prevent %s reinit\n", __func__,
4639 dev_name(&other_ha->pdev->dev)));
4640 }
4641 }
4642 pci_dev_put(other_pdev);
4643 }
4644}
4645
Mike Christie13483732011-12-01 21:38:41 -06004646static void qla4xxx_destroy_fw_ddb_session(struct scsi_qla_host *ha)
4647{
4648 struct ddb_entry *ddb_entry;
4649 int options;
4650 int idx;
4651
4652 for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
4653
4654 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
4655 if ((ddb_entry != NULL) &&
4656 (ddb_entry->ddb_type == FLASH_DDB)) {
4657
4658 options = LOGOUT_OPTION_CLOSE_SESSION;
4659 if (qla4xxx_session_logout_ddb(ha, ddb_entry, options)
4660 == QLA_ERROR)
4661 ql4_printk(KERN_ERR, ha, "%s: Logout failed\n",
4662 __func__);
4663
4664 qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
4665 /*
4666 * we have decremented the reference count of the driver
4667 * when we setup the session to have the driver unload
4668 * to be seamless without actually destroying the
4669 * session
4670 **/
4671 try_module_get(qla4xxx_iscsi_transport.owner);
4672 iscsi_destroy_endpoint(ddb_entry->conn->ep);
4673 qla4xxx_free_ddb(ha, ddb_entry);
4674 iscsi_session_teardown(ddb_entry->sess);
4675 }
4676 }
4677}
Karen Higgins7eece5a2011-03-21 03:34:29 -07004678/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07004679 * qla4xxx_remove_adapter - calback function to remove adapter.
4680 * @pci_dev: PCI device pointer
4681 **/
4682static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
4683{
4684 struct scsi_qla_host *ha;
4685
4686 ha = pci_get_drvdata(pdev);
4687
Karen Higgins7eece5a2011-03-21 03:34:29 -07004688 if (!is_qla8022(ha))
4689 qla4xxx_prevent_other_port_reinit(ha);
David C Somayajulubee4fe82007-05-23 18:03:32 -07004690
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05004691 /* destroy iface from sysfs */
4692 qla4xxx_destroy_ifaces(ha);
4693
Mike Christie13483732011-12-01 21:38:41 -06004694 if ((!ql4xdisablesysfsboot) && ha->boot_kset)
Manish Rangankar2a991c22011-07-25 13:48:55 -05004695 iscsi_boot_destroy_kset(ha->boot_kset);
4696
Mike Christie13483732011-12-01 21:38:41 -06004697 qla4xxx_destroy_fw_ddb_session(ha);
4698
David Somayajuluafaf5a22006-09-19 10:28:00 -07004699 scsi_remove_host(ha->host);
4700
4701 qla4xxx_free_adapter(ha);
4702
4703 scsi_host_put(ha->host);
4704
Lalit Chandivade2232be02010-07-30 14:38:47 +05304705 pci_disable_pcie_error_reporting(pdev);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304706 pci_disable_device(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004707 pci_set_drvdata(pdev, NULL);
4708}
4709
4710/**
4711 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
4712 * @ha: HA context
4713 *
4714 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
4715 * supported addressing method.
4716 */
Adrian Bunk47975472007-04-26 00:35:16 -07004717static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07004718{
4719 int retval;
4720
4721 /* Update our PCI device dma_mask for full 64 bit mask */
Yang Hongyang6a355282009-04-06 19:01:13 -07004722 if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
4723 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07004724 dev_dbg(&ha->pdev->dev,
4725 "Failed to set 64 bit PCI consistent mask; "
4726 "using 32 bit.\n");
4727 retval = pci_set_consistent_dma_mask(ha->pdev,
Yang Hongyang284901a2009-04-06 19:01:15 -07004728 DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07004729 }
4730 } else
Yang Hongyang284901a2009-04-06 19:01:15 -07004731 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07004732}
4733
4734static int qla4xxx_slave_alloc(struct scsi_device *sdev)
4735{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004736 struct iscsi_cls_session *cls_sess;
4737 struct iscsi_session *sess;
4738 struct ddb_entry *ddb;
Vikas Chaudhary8bb40332011-03-21 03:34:31 -07004739 int queue_depth = QL4_DEF_QDEPTH;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004740
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004741 cls_sess = starget_to_session(sdev->sdev_target);
4742 sess = cls_sess->dd_data;
4743 ddb = sess->dd_data;
4744
David Somayajuluafaf5a22006-09-19 10:28:00 -07004745 sdev->hostdata = ddb;
4746 sdev->tagged_supported = 1;
Vikas Chaudhary8bb40332011-03-21 03:34:31 -07004747
4748 if (ql4xmaxqdepth != 0 && ql4xmaxqdepth <= 0xffffU)
4749 queue_depth = ql4xmaxqdepth;
4750
4751 scsi_activate_tcq(sdev, queue_depth);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004752 return 0;
4753}
4754
4755static int qla4xxx_slave_configure(struct scsi_device *sdev)
4756{
4757 sdev->tagged_supported = 1;
4758 return 0;
4759}
4760
4761static void qla4xxx_slave_destroy(struct scsi_device *sdev)
4762{
4763 scsi_deactivate_tcq(sdev, 1);
4764}
4765
4766/**
4767 * qla4xxx_del_from_active_array - returns an active srb
4768 * @ha: Pointer to host adapter structure.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02004769 * @index: index into the active_array
David Somayajuluafaf5a22006-09-19 10:28:00 -07004770 *
4771 * This routine removes and returns the srb at the specified index
4772 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304773struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
4774 uint32_t index)
David Somayajuluafaf5a22006-09-19 10:28:00 -07004775{
4776 struct srb *srb = NULL;
Vikas Chaudhary53698872010-04-28 11:41:59 +05304777 struct scsi_cmnd *cmd = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004778
Vikas Chaudhary53698872010-04-28 11:41:59 +05304779 cmd = scsi_host_find_tag(ha->host, index);
4780 if (!cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07004781 return srb;
4782
Vikas Chaudhary53698872010-04-28 11:41:59 +05304783 srb = (struct srb *)CMD_SP(cmd);
4784 if (!srb)
David Somayajuluafaf5a22006-09-19 10:28:00 -07004785 return srb;
4786
4787 /* update counters */
4788 if (srb->flags & SRB_DMA_VALID) {
4789 ha->req_q_count += srb->iocb_cnt;
4790 ha->iocb_cnt -= srb->iocb_cnt;
4791 if (srb->cmd)
Vikas Chaudhary53698872010-04-28 11:41:59 +05304792 srb->cmd->host_scribble =
4793 (unsigned char *)(unsigned long) MAX_SRBS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004794 }
4795 return srb;
4796}
4797
4798/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07004799 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304800 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07004801 * @cmd: Scsi Command to wait on.
4802 *
4803 * This routine waits for the command to be returned by the Firmware
4804 * for some max time.
4805 **/
4806static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
4807 struct scsi_cmnd *cmd)
4808{
4809 int done = 0;
4810 struct srb *rp;
4811 uint32_t max_wait_time = EH_WAIT_CMD_TOV;
Lalit Chandivade2232be02010-07-30 14:38:47 +05304812 int ret = SUCCESS;
4813
4814 /* Dont wait on command if PCI error is being handled
4815 * by PCI AER driver
4816 */
4817 if (unlikely(pci_channel_offline(ha->pdev)) ||
4818 (test_bit(AF_EEH_BUSY, &ha->flags))) {
4819 ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
4820 ha->host_no, __func__);
4821 return ret;
4822 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07004823
4824 do {
4825 /* Checking to see if its returned to OS */
Vikas Chaudhary53698872010-04-28 11:41:59 +05304826 rp = (struct srb *) CMD_SP(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004827 if (rp == NULL) {
4828 done++;
4829 break;
4830 }
4831
4832 msleep(2000);
4833 } while (max_wait_time--);
4834
4835 return done;
4836}
4837
4838/**
4839 * qla4xxx_wait_for_hba_online - waits for HBA to come online
4840 * @ha: Pointer to host adapter structure
4841 **/
4842static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
4843{
4844 unsigned long wait_online;
4845
Vikas Chaudharyf581a3f2010-10-06 22:47:48 -07004846 wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004847 while (time_before(jiffies, wait_online)) {
4848
4849 if (adapter_up(ha))
4850 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004851
4852 msleep(2000);
4853 }
4854
4855 return QLA_ERROR;
4856}
4857
4858/**
Mike Christiece545032008-02-29 18:25:20 -06004859 * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02004860 * @ha: pointer to HBA
David Somayajuluafaf5a22006-09-19 10:28:00 -07004861 * @t: target id
4862 * @l: lun id
4863 *
4864 * This function waits for all outstanding commands to a lun to complete. It
4865 * returns 0 if all pending commands are returned and 1 otherwise.
4866 **/
Mike Christiece545032008-02-29 18:25:20 -06004867static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
4868 struct scsi_target *stgt,
4869 struct scsi_device *sdev)
David Somayajuluafaf5a22006-09-19 10:28:00 -07004870{
4871 int cnt;
4872 int status = 0;
4873 struct scsi_cmnd *cmd;
4874
4875 /*
Mike Christiece545032008-02-29 18:25:20 -06004876 * Waiting for all commands for the designated target or dev
4877 * in the active array
David Somayajuluafaf5a22006-09-19 10:28:00 -07004878 */
4879 for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
4880 cmd = scsi_host_find_tag(ha->host, cnt);
Mike Christiece545032008-02-29 18:25:20 -06004881 if (cmd && stgt == scsi_target(cmd->device) &&
4882 (!sdev || sdev == cmd->device)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07004883 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
4884 status++;
4885 break;
4886 }
4887 }
4888 }
4889 return status;
4890}
4891
4892/**
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304893 * qla4xxx_eh_abort - callback for abort task.
4894 * @cmd: Pointer to Linux's SCSI command structure
4895 *
4896 * This routine is called by the Linux OS to abort the specified
4897 * command.
4898 **/
4899static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
4900{
4901 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
4902 unsigned int id = cmd->device->id;
4903 unsigned int lun = cmd->device->lun;
Mike Christie92b3e5b2010-10-06 22:51:17 -07004904 unsigned long flags;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304905 struct srb *srb = NULL;
4906 int ret = SUCCESS;
4907 int wait = 0;
4908
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304909 ql4_printk(KERN_INFO, ha,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004910 "scsi%ld:%d:%d: Abort command issued cmd=%p\n",
4911 ha->host_no, id, lun, cmd);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304912
Mike Christie92b3e5b2010-10-06 22:51:17 -07004913 spin_lock_irqsave(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304914 srb = (struct srb *) CMD_SP(cmd);
Mike Christie92b3e5b2010-10-06 22:51:17 -07004915 if (!srb) {
4916 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304917 return SUCCESS;
Mike Christie92b3e5b2010-10-06 22:51:17 -07004918 }
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304919 kref_get(&srb->srb_ref);
Mike Christie92b3e5b2010-10-06 22:51:17 -07004920 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304921
4922 if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
4923 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
4924 ha->host_no, id, lun));
4925 ret = FAILED;
4926 } else {
4927 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
4928 ha->host_no, id, lun));
4929 wait = 1;
4930 }
4931
4932 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
4933
4934 /* Wait for command to complete */
4935 if (wait) {
4936 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
4937 DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
4938 ha->host_no, id, lun));
4939 ret = FAILED;
4940 }
4941 }
4942
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304943 ql4_printk(KERN_INFO, ha,
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304944 "scsi%ld:%d:%d: Abort command - %s\n",
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004945 ha->host_no, id, lun, (ret == SUCCESS) ? "succeeded" : "failed");
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05304946
4947 return ret;
4948}
4949
4950/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07004951 * qla4xxx_eh_device_reset - callback for target reset.
4952 * @cmd: Pointer to Linux's SCSI command structure
4953 *
4954 * This routine is called by the Linux OS to reset all luns on the
4955 * specified target.
4956 **/
4957static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
4958{
4959 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
4960 struct ddb_entry *ddb_entry = cmd->device->hostdata;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004961 int ret = FAILED, stat;
4962
Karen Higgins612f7342009-07-15 15:03:01 -05004963 if (!ddb_entry)
David Somayajuluafaf5a22006-09-19 10:28:00 -07004964 return ret;
4965
Mike Christiec01be6d2010-07-22 16:59:49 +05304966 ret = iscsi_block_scsi_eh(cmd);
4967 if (ret)
4968 return ret;
4969 ret = FAILED;
4970
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304971 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07004972 "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
4973 cmd->device->channel, cmd->device->id, cmd->device->lun);
4974
4975 DEBUG2(printk(KERN_INFO
4976 "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
4977 "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
Jens Axboe242f9dc2008-09-14 05:55:09 -07004978 cmd, jiffies, cmd->request->timeout / HZ,
David Somayajuluafaf5a22006-09-19 10:28:00 -07004979 ha->dpc_flags, cmd->result, cmd->allowed));
4980
4981 /* FIXME: wait for hba to go online */
4982 stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
4983 if (stat != QLA_SUCCESS) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304984 ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004985 goto eh_dev_reset_done;
4986 }
4987
Mike Christiece545032008-02-29 18:25:20 -06004988 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
4989 cmd->device)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304990 ql4_printk(KERN_INFO, ha,
Mike Christiece545032008-02-29 18:25:20 -06004991 "DEVICE RESET FAILED - waiting for "
4992 "commands.\n");
4993 goto eh_dev_reset_done;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004994 }
4995
David C Somayajulu9d562912008-03-19 11:23:03 -07004996 /* Send marker. */
4997 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
4998 MM_LUN_RESET) != QLA_SUCCESS)
4999 goto eh_dev_reset_done;
5000
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05305001 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07005002 "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
5003 ha->host_no, cmd->device->channel, cmd->device->id,
5004 cmd->device->lun);
5005
5006 ret = SUCCESS;
5007
5008eh_dev_reset_done:
5009
5010 return ret;
5011}
5012
5013/**
Mike Christiece545032008-02-29 18:25:20 -06005014 * qla4xxx_eh_target_reset - callback for target reset.
5015 * @cmd: Pointer to Linux's SCSI command structure
5016 *
5017 * This routine is called by the Linux OS to reset the target.
5018 **/
5019static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
5020{
5021 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
5022 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christiec01be6d2010-07-22 16:59:49 +05305023 int stat, ret;
Mike Christiece545032008-02-29 18:25:20 -06005024
5025 if (!ddb_entry)
5026 return FAILED;
5027
Mike Christiec01be6d2010-07-22 16:59:49 +05305028 ret = iscsi_block_scsi_eh(cmd);
5029 if (ret)
5030 return ret;
5031
Mike Christiece545032008-02-29 18:25:20 -06005032 starget_printk(KERN_INFO, scsi_target(cmd->device),
5033 "WARM TARGET RESET ISSUED.\n");
5034
5035 DEBUG2(printk(KERN_INFO
5036 "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
5037 "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
Jens Axboe242f9dc2008-09-14 05:55:09 -07005038 ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
Mike Christiece545032008-02-29 18:25:20 -06005039 ha->dpc_flags, cmd->result, cmd->allowed));
5040
5041 stat = qla4xxx_reset_target(ha, ddb_entry);
5042 if (stat != QLA_SUCCESS) {
5043 starget_printk(KERN_INFO, scsi_target(cmd->device),
5044 "WARM TARGET RESET FAILED.\n");
5045 return FAILED;
5046 }
5047
Mike Christiece545032008-02-29 18:25:20 -06005048 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
5049 NULL)) {
5050 starget_printk(KERN_INFO, scsi_target(cmd->device),
5051 "WARM TARGET DEVICE RESET FAILED - "
5052 "waiting for commands.\n");
5053 return FAILED;
5054 }
5055
David C Somayajulu9d562912008-03-19 11:23:03 -07005056 /* Send marker. */
5057 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
5058 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
5059 starget_printk(KERN_INFO, scsi_target(cmd->device),
5060 "WARM TARGET DEVICE RESET FAILED - "
5061 "marker iocb failed.\n");
5062 return FAILED;
5063 }
5064
Mike Christiece545032008-02-29 18:25:20 -06005065 starget_printk(KERN_INFO, scsi_target(cmd->device),
5066 "WARM TARGET RESET SUCCEEDED.\n");
5067 return SUCCESS;
5068}
5069
5070/**
Sarang Radke8a288962011-12-06 02:34:10 -08005071 * qla4xxx_is_eh_active - check if error handler is running
5072 * @shost: Pointer to SCSI Host struct
5073 *
5074 * This routine finds that if reset host is called in EH
5075 * scenario or from some application like sg_reset
5076 **/
5077static int qla4xxx_is_eh_active(struct Scsi_Host *shost)
5078{
5079 if (shost->shost_state == SHOST_RECOVERY)
5080 return 1;
5081 return 0;
5082}
5083
5084/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07005085 * qla4xxx_eh_host_reset - kernel callback
5086 * @cmd: Pointer to Linux's SCSI command structure
5087 *
5088 * This routine is invoked by the Linux kernel to perform fatal error
5089 * recovery on the specified adapter.
5090 **/
5091static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
5092{
5093 int return_status = FAILED;
5094 struct scsi_qla_host *ha;
5095
Manish Rangankarb3a271a2011-07-25 13:48:53 -05005096 ha = to_qla_host(cmd->device->host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07005097
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05305098 if (ql4xdontresethba) {
5099 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
5100 ha->host_no, __func__));
Sarang Radke8a288962011-12-06 02:34:10 -08005101
5102 /* Clear outstanding srb in queues */
5103 if (qla4xxx_is_eh_active(cmd->device->host))
5104 qla4xxx_abort_active_cmds(ha, DID_ABORT << 16);
5105
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05305106 return FAILED;
5107 }
5108
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05305109 ql4_printk(KERN_INFO, ha,
Karen Higginsdca05c42009-07-15 15:03:00 -05005110 "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
David Somayajuluafaf5a22006-09-19 10:28:00 -07005111 cmd->device->channel, cmd->device->id, cmd->device->lun);
5112
5113 if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
5114 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
5115 "DEAD.\n", ha->host_no, cmd->device->channel,
5116 __func__));
5117
5118 return FAILED;
5119 }
5120
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05305121 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
5122 if (is_qla8022(ha))
5123 set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
5124 else
5125 set_bit(DPC_RESET_HA, &ha->dpc_flags);
5126 }
Mike Christie50a29ae2008-03-04 13:26:53 -06005127
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05305128 if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
David Somayajuluafaf5a22006-09-19 10:28:00 -07005129 return_status = SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07005130
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05305131 ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005132 return_status == FAILED ? "FAILED" : "SUCCEEDED");
David Somayajuluafaf5a22006-09-19 10:28:00 -07005133
5134 return return_status;
5135}
5136
Vikas Chaudhary95d31262011-08-12 02:51:29 -07005137static int qla4xxx_context_reset(struct scsi_qla_host *ha)
5138{
5139 uint32_t mbox_cmd[MBOX_REG_COUNT];
5140 uint32_t mbox_sts[MBOX_REG_COUNT];
5141 struct addr_ctrl_blk_def *acb = NULL;
5142 uint32_t acb_len = sizeof(struct addr_ctrl_blk_def);
5143 int rval = QLA_SUCCESS;
5144 dma_addr_t acb_dma;
5145
5146 acb = dma_alloc_coherent(&ha->pdev->dev,
5147 sizeof(struct addr_ctrl_blk_def),
5148 &acb_dma, GFP_KERNEL);
5149 if (!acb) {
5150 ql4_printk(KERN_ERR, ha, "%s: Unable to alloc acb\n",
5151 __func__);
5152 rval = -ENOMEM;
5153 goto exit_port_reset;
5154 }
5155
5156 memset(acb, 0, acb_len);
5157
5158 rval = qla4xxx_get_acb(ha, acb_dma, PRIMARI_ACB, acb_len);
5159 if (rval != QLA_SUCCESS) {
5160 rval = -EIO;
5161 goto exit_free_acb;
5162 }
5163
5164 rval = qla4xxx_disable_acb(ha);
5165 if (rval != QLA_SUCCESS) {
5166 rval = -EIO;
5167 goto exit_free_acb;
5168 }
5169
5170 wait_for_completion_timeout(&ha->disable_acb_comp,
5171 DISABLE_ACB_TOV * HZ);
5172
5173 rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], acb_dma);
5174 if (rval != QLA_SUCCESS) {
5175 rval = -EIO;
5176 goto exit_free_acb;
5177 }
5178
5179exit_free_acb:
5180 dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk_def),
5181 acb, acb_dma);
5182exit_port_reset:
5183 DEBUG2(ql4_printk(KERN_INFO, ha, "%s %s\n", __func__,
5184 rval == QLA_SUCCESS ? "SUCCEEDED" : "FAILED"));
5185 return rval;
5186}
5187
5188static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type)
5189{
5190 struct scsi_qla_host *ha = to_qla_host(shost);
5191 int rval = QLA_SUCCESS;
5192
5193 if (ql4xdontresethba) {
5194 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Don't Reset HBA\n",
5195 __func__));
5196 rval = -EPERM;
5197 goto exit_host_reset;
5198 }
5199
5200 rval = qla4xxx_wait_for_hba_online(ha);
5201 if (rval != QLA_SUCCESS) {
5202 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Unable to reset host "
5203 "adapter\n", __func__));
5204 rval = -EIO;
5205 goto exit_host_reset;
5206 }
5207
5208 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
5209 goto recover_adapter;
5210
5211 switch (reset_type) {
5212 case SCSI_ADAPTER_RESET:
5213 set_bit(DPC_RESET_HA, &ha->dpc_flags);
5214 break;
5215 case SCSI_FIRMWARE_RESET:
5216 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
5217 if (is_qla8022(ha))
5218 /* set firmware context reset */
5219 set_bit(DPC_RESET_HA_FW_CONTEXT,
5220 &ha->dpc_flags);
5221 else {
5222 rval = qla4xxx_context_reset(ha);
5223 goto exit_host_reset;
5224 }
5225 }
5226 break;
5227 }
5228
5229recover_adapter:
5230 rval = qla4xxx_recover_adapter(ha);
5231 if (rval != QLA_SUCCESS) {
5232 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: recover adapter fail\n",
5233 __func__));
5234 rval = -EIO;
5235 }
5236
5237exit_host_reset:
5238 return rval;
5239}
5240
Lalit Chandivade2232be02010-07-30 14:38:47 +05305241/* PCI AER driver recovers from all correctable errors w/o
5242 * driver intervention. For uncorrectable errors PCI AER
5243 * driver calls the following device driver's callbacks
5244 *
5245 * - Fatal Errors - link_reset
5246 * - Non-Fatal Errors - driver's pci_error_detected() which
5247 * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
5248 *
5249 * PCI AER driver calls
5250 * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
5251 * returns RECOVERED or NEED_RESET if fw_hung
5252 * NEED_RESET - driver's slot_reset()
5253 * DISCONNECT - device is dead & cannot recover
5254 * RECOVERED - driver's pci_resume()
5255 */
5256static pci_ers_result_t
5257qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
5258{
5259 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
5260
5261 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
5262 ha->host_no, __func__, state);
5263
5264 if (!is_aer_supported(ha))
5265 return PCI_ERS_RESULT_NONE;
5266
5267 switch (state) {
5268 case pci_channel_io_normal:
5269 clear_bit(AF_EEH_BUSY, &ha->flags);
5270 return PCI_ERS_RESULT_CAN_RECOVER;
5271 case pci_channel_io_frozen:
5272 set_bit(AF_EEH_BUSY, &ha->flags);
5273 qla4xxx_mailbox_premature_completion(ha);
5274 qla4xxx_free_irqs(ha);
5275 pci_disable_device(pdev);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07005276 /* Return back all IOs */
5277 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
Lalit Chandivade2232be02010-07-30 14:38:47 +05305278 return PCI_ERS_RESULT_NEED_RESET;
5279 case pci_channel_io_perm_failure:
5280 set_bit(AF_EEH_BUSY, &ha->flags);
5281 set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
5282 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
5283 return PCI_ERS_RESULT_DISCONNECT;
5284 }
5285 return PCI_ERS_RESULT_NEED_RESET;
5286}
5287
5288/**
5289 * qla4xxx_pci_mmio_enabled() gets called if
5290 * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
5291 * and read/write to the device still works.
5292 **/
5293static pci_ers_result_t
5294qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
5295{
5296 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
5297
5298 if (!is_aer_supported(ha))
5299 return PCI_ERS_RESULT_NONE;
5300
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07005301 return PCI_ERS_RESULT_RECOVERED;
Lalit Chandivade2232be02010-07-30 14:38:47 +05305302}
5303
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07005304static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
Lalit Chandivade2232be02010-07-30 14:38:47 +05305305{
5306 uint32_t rval = QLA_ERROR;
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07005307 uint32_t ret = 0;
Lalit Chandivade2232be02010-07-30 14:38:47 +05305308 int fn;
5309 struct pci_dev *other_pdev = NULL;
5310
5311 ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
5312
5313 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
5314
5315 if (test_bit(AF_ONLINE, &ha->flags)) {
5316 clear_bit(AF_ONLINE, &ha->flags);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05005317 clear_bit(AF_LINK_UP, &ha->flags);
5318 iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
Lalit Chandivade2232be02010-07-30 14:38:47 +05305319 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
Lalit Chandivade2232be02010-07-30 14:38:47 +05305320 }
5321
5322 fn = PCI_FUNC(ha->pdev->devfn);
5323 while (fn > 0) {
5324 fn--;
5325 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
5326 "func %x\n", ha->host_no, __func__, fn);
5327 /* Get the pci device given the domain, bus,
5328 * slot/function number */
5329 other_pdev =
5330 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
5331 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
5332 fn));
5333
5334 if (!other_pdev)
5335 continue;
5336
5337 if (atomic_read(&other_pdev->enable_cnt)) {
5338 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
5339 "func in enabled state%x\n", ha->host_no,
5340 __func__, fn);
5341 pci_dev_put(other_pdev);
5342 break;
5343 }
5344 pci_dev_put(other_pdev);
5345 }
5346
5347 /* The first function on the card, the reset owner will
5348 * start & initialize the firmware. The other functions
5349 * on the card will reset the firmware context
5350 */
5351 if (!fn) {
5352 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
5353 "0x%x is the owner\n", ha->host_no, __func__,
5354 ha->pdev->devfn);
5355
5356 qla4_8xxx_idc_lock(ha);
5357 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
5358 QLA82XX_DEV_COLD);
5359
5360 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
5361 QLA82XX_IDC_VERSION);
5362
5363 qla4_8xxx_idc_unlock(ha);
5364 clear_bit(AF_FW_RECOVERY, &ha->flags);
Mike Christie13483732011-12-01 21:38:41 -06005365 rval = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
Lalit Chandivade2232be02010-07-30 14:38:47 +05305366 qla4_8xxx_idc_lock(ha);
5367
5368 if (rval != QLA_SUCCESS) {
5369 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
5370 "FAILED\n", ha->host_no, __func__);
5371 qla4_8xxx_clear_drv_active(ha);
5372 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
5373 QLA82XX_DEV_FAILED);
5374 } else {
5375 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
5376 "READY\n", ha->host_no, __func__);
5377 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
5378 QLA82XX_DEV_READY);
5379 /* Clear driver state register */
5380 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
5381 qla4_8xxx_set_drv_active(ha);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07005382 ret = qla4xxx_request_irqs(ha);
5383 if (ret) {
5384 ql4_printk(KERN_WARNING, ha, "Failed to "
5385 "reserve interrupt %d already in use.\n",
5386 ha->pdev->irq);
5387 rval = QLA_ERROR;
5388 } else {
5389 ha->isp_ops->enable_intrs(ha);
5390 rval = QLA_SUCCESS;
5391 }
Lalit Chandivade2232be02010-07-30 14:38:47 +05305392 }
5393 qla4_8xxx_idc_unlock(ha);
5394 } else {
5395 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
5396 "the reset owner\n", ha->host_no, __func__,
5397 ha->pdev->devfn);
5398 if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
5399 QLA82XX_DEV_READY)) {
5400 clear_bit(AF_FW_RECOVERY, &ha->flags);
Mike Christie13483732011-12-01 21:38:41 -06005401 rval = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07005402 if (rval == QLA_SUCCESS) {
5403 ret = qla4xxx_request_irqs(ha);
5404 if (ret) {
5405 ql4_printk(KERN_WARNING, ha, "Failed to"
5406 " reserve interrupt %d already in"
5407 " use.\n", ha->pdev->irq);
5408 rval = QLA_ERROR;
5409 } else {
5410 ha->isp_ops->enable_intrs(ha);
5411 rval = QLA_SUCCESS;
5412 }
5413 }
Lalit Chandivade2232be02010-07-30 14:38:47 +05305414 qla4_8xxx_idc_lock(ha);
5415 qla4_8xxx_set_drv_active(ha);
5416 qla4_8xxx_idc_unlock(ha);
5417 }
5418 }
5419 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
5420 return rval;
5421}
5422
5423static pci_ers_result_t
5424qla4xxx_pci_slot_reset(struct pci_dev *pdev)
5425{
5426 pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
5427 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
5428 int rc;
5429
5430 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
5431 ha->host_no, __func__);
5432
5433 if (!is_aer_supported(ha))
5434 return PCI_ERS_RESULT_NONE;
5435
5436 /* Restore the saved state of PCIe device -
5437 * BAR registers, PCI Config space, PCIX, MSI,
5438 * IOV states
5439 */
5440 pci_restore_state(pdev);
5441
5442 /* pci_restore_state() clears the saved_state flag of the device
5443 * save restored state which resets saved_state flag
5444 */
5445 pci_save_state(pdev);
5446
5447 /* Initialize device or resume if in suspended state */
5448 rc = pci_enable_device(pdev);
5449 if (rc) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005450 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Can't re-enable "
Lalit Chandivade2232be02010-07-30 14:38:47 +05305451 "device after reset\n", ha->host_no, __func__);
5452 goto exit_slot_reset;
5453 }
5454
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07005455 ha->isp_ops->disable_intrs(ha);
Lalit Chandivade2232be02010-07-30 14:38:47 +05305456
5457 if (is_qla8022(ha)) {
5458 if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
5459 ret = PCI_ERS_RESULT_RECOVERED;
5460 goto exit_slot_reset;
5461 } else
5462 goto exit_slot_reset;
5463 }
5464
5465exit_slot_reset:
5466 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
5467 "device after reset\n", ha->host_no, __func__, ret);
5468 return ret;
5469}
5470
5471static void
5472qla4xxx_pci_resume(struct pci_dev *pdev)
5473{
5474 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
5475 int ret;
5476
5477 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
5478 ha->host_no, __func__);
5479
5480 ret = qla4xxx_wait_for_hba_online(ha);
5481 if (ret != QLA_SUCCESS) {
5482 ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
5483 "resume I/O from slot/link_reset\n", ha->host_no,
5484 __func__);
5485 }
5486
5487 pci_cleanup_aer_uncorrect_error_status(pdev);
5488 clear_bit(AF_EEH_BUSY, &ha->flags);
5489}
5490
5491static struct pci_error_handlers qla4xxx_err_handler = {
5492 .error_detected = qla4xxx_pci_error_detected,
5493 .mmio_enabled = qla4xxx_pci_mmio_enabled,
5494 .slot_reset = qla4xxx_pci_slot_reset,
5495 .resume = qla4xxx_pci_resume,
5496};
5497
David Somayajuluafaf5a22006-09-19 10:28:00 -07005498static struct pci_device_id qla4xxx_pci_tbl[] = {
5499 {
5500 .vendor = PCI_VENDOR_ID_QLOGIC,
5501 .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
5502 .subvendor = PCI_ANY_ID,
5503 .subdevice = PCI_ANY_ID,
5504 },
5505 {
5506 .vendor = PCI_VENDOR_ID_QLOGIC,
5507 .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
5508 .subvendor = PCI_ANY_ID,
5509 .subdevice = PCI_ANY_ID,
5510 },
David C Somayajulud9150582006-11-15 17:38:40 -08005511 {
5512 .vendor = PCI_VENDOR_ID_QLOGIC,
5513 .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
5514 .subvendor = PCI_ANY_ID,
5515 .subdevice = PCI_ANY_ID,
5516 },
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05305517 {
5518 .vendor = PCI_VENDOR_ID_QLOGIC,
5519 .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
5520 .subvendor = PCI_ANY_ID,
5521 .subdevice = PCI_ANY_ID,
5522 },
David Somayajuluafaf5a22006-09-19 10:28:00 -07005523 {0, 0},
5524};
5525MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
5526
Adrian Bunk47975472007-04-26 00:35:16 -07005527static struct pci_driver qla4xxx_pci_driver = {
David Somayajuluafaf5a22006-09-19 10:28:00 -07005528 .name = DRIVER_NAME,
5529 .id_table = qla4xxx_pci_tbl,
5530 .probe = qla4xxx_probe_adapter,
5531 .remove = qla4xxx_remove_adapter,
Lalit Chandivade2232be02010-07-30 14:38:47 +05305532 .err_handler = &qla4xxx_err_handler,
David Somayajuluafaf5a22006-09-19 10:28:00 -07005533};
5534
5535static int __init qla4xxx_module_init(void)
5536{
5537 int ret;
5538
5539 /* Allocate cache for SRBs. */
5540 srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09005541 SLAB_HWCACHE_ALIGN, NULL);
David Somayajuluafaf5a22006-09-19 10:28:00 -07005542 if (srb_cachep == NULL) {
5543 printk(KERN_ERR
5544 "%s: Unable to allocate SRB cache..."
5545 "Failing load!\n", DRIVER_NAME);
5546 ret = -ENOMEM;
5547 goto no_srp_cache;
5548 }
5549
5550 /* Derive version string. */
5551 strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
Andrew Vasquez11010fe2006-10-06 09:54:59 -07005552 if (ql4xextended_error_logging)
David Somayajuluafaf5a22006-09-19 10:28:00 -07005553 strcat(qla4xxx_version_str, "-debug");
5554
5555 qla4xxx_scsi_transport =
5556 iscsi_register_transport(&qla4xxx_iscsi_transport);
5557 if (!qla4xxx_scsi_transport){
5558 ret = -ENODEV;
5559 goto release_srb_cache;
5560 }
5561
David Somayajuluafaf5a22006-09-19 10:28:00 -07005562 ret = pci_register_driver(&qla4xxx_pci_driver);
5563 if (ret)
5564 goto unregister_transport;
5565
5566 printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
5567 return 0;
Doug Maxey5ae16db2006-10-05 23:50:07 -05005568
David Somayajuluafaf5a22006-09-19 10:28:00 -07005569unregister_transport:
5570 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
5571release_srb_cache:
5572 kmem_cache_destroy(srb_cachep);
5573no_srp_cache:
5574 return ret;
5575}
5576
5577static void __exit qla4xxx_module_exit(void)
5578{
David Somayajuluafaf5a22006-09-19 10:28:00 -07005579 pci_unregister_driver(&qla4xxx_pci_driver);
5580 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
5581 kmem_cache_destroy(srb_cachep);
5582}
5583
5584module_init(qla4xxx_module_init);
5585module_exit(qla4xxx_module_exit);
5586
5587MODULE_AUTHOR("QLogic Corporation");
5588MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
5589MODULE_LICENSE("GPL");
5590MODULE_VERSION(QLA4XXX_DRIVER_VERSION);