blob: 489d7cd5e0cee0c0d29973f24d83499fbb93b5f7 [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>
David Somayajuluafaf5a22006-09-19 10:28:00 -070011
12#include <scsi/scsi_tcq.h>
13#include <scsi/scsicam.h>
14
15#include "ql4_def.h"
David C Somayajulubee4fe82007-05-23 18:03:32 -070016#include "ql4_version.h"
17#include "ql4_glbl.h"
18#include "ql4_dbg.h"
19#include "ql4_inline.h"
David Somayajuluafaf5a22006-09-19 10:28:00 -070020
21/*
22 * Driver version
23 */
Adrian Bunk47975472007-04-26 00:35:16 -070024static char qla4xxx_version_str[40];
David Somayajuluafaf5a22006-09-19 10:28:00 -070025
26/*
27 * SRB allocation cache
28 */
Christoph Lametere18b8902006-12-06 20:33:20 -080029static struct kmem_cache *srb_cachep;
David Somayajuluafaf5a22006-09-19 10:28:00 -070030
31/*
32 * Module parameter information and variables
33 */
David Somayajuluafaf5a22006-09-19 10:28:00 -070034int ql4xdontresethba = 0;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053035module_param(ql4xdontresethba, int, S_IRUGO | S_IWUSR);
David Somayajuluafaf5a22006-09-19 10:28:00 -070036MODULE_PARM_DESC(ql4xdontresethba,
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053037 "Don't reset the HBA for driver recovery \n"
38 " 0 - It will reset HBA (Default)\n"
39 " 1 - It will NOT reset HBA");
David Somayajuluafaf5a22006-09-19 10:28:00 -070040
Andrew Vasquez11010fe2006-10-06 09:54:59 -070041int ql4xextended_error_logging = 0; /* 0 = off, 1 = log errors */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053042module_param(ql4xextended_error_logging, int, S_IRUGO | S_IWUSR);
Andrew Vasquez11010fe2006-10-06 09:54:59 -070043MODULE_PARM_DESC(ql4xextended_error_logging,
David Somayajuluafaf5a22006-09-19 10:28:00 -070044 "Option to enable extended error logging, "
45 "Default is 0 - no logging, 1 - debug logging");
46
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053047int ql4xenablemsix = 1;
48module_param(ql4xenablemsix, int, S_IRUGO|S_IWUSR);
49MODULE_PARM_DESC(ql4xenablemsix,
50 "Set to enable MSI or MSI-X interrupt mechanism.\n"
51 " 0 = enable INTx interrupt mechanism.\n"
52 " 1 = enable MSI-X interrupt mechanism (Default).\n"
53 " 2 = enable MSI interrupt mechanism.");
David C Somayajulu477ffb92007-01-22 12:26:11 -080054
Mike Christied510d962008-07-11 19:50:33 -050055#define QL4_DEF_QDEPTH 32
Vikas Chaudhary8bb40332011-03-21 03:34:31 -070056static int ql4xmaxqdepth = QL4_DEF_QDEPTH;
57module_param(ql4xmaxqdepth, int, S_IRUGO | S_IWUSR);
58MODULE_PARM_DESC(ql4xmaxqdepth,
59 "Maximum queue depth to report for target devices.\n"
60 " Default: 32.");
Mike Christied510d962008-07-11 19:50:33 -050061
Vikas Chaudhary30387272011-03-21 03:34:32 -070062static int ql4xsess_recovery_tmo = QL4_SESS_RECOVERY_TMO;
63module_param(ql4xsess_recovery_tmo, int, S_IRUGO);
64MODULE_PARM_DESC(ql4xsess_recovery_tmo,
65 "Target Session Recovery Timeout.\n"
66 " Default: 30 sec.");
67
Manish Rangankarb3a271a2011-07-25 13:48:53 -050068static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -070069/*
70 * SCSI host template entry points
71 */
Adrian Bunk47975472007-04-26 00:35:16 -070072static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -070073
74/*
75 * iSCSI template entry points
76 */
David Somayajuluafaf5a22006-09-19 10:28:00 -070077static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
78 enum iscsi_param param, char *buf);
Mike Christieaa1e93a2007-05-30 12:57:09 -050079static int qla4xxx_host_get_param(struct Scsi_Host *shost,
80 enum iscsi_host_param param, char *buf);
Mike Christied00efe32011-07-25 13:48:38 -050081static int qla4xxx_iface_set_param(struct Scsi_Host *shost, char *data,
82 int count);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -050083static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
84 enum iscsi_param_type param_type,
85 int param, char *buf);
Mike Christie5c656af2009-07-15 15:02:59 -050086static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
Manish Rangankarb3a271a2011-07-25 13:48:53 -050087static struct iscsi_endpoint *qla4xxx_ep_connect(struct Scsi_Host *shost,
88 struct sockaddr *dst_addr,
89 int non_blocking);
90static int qla4xxx_ep_poll(struct iscsi_endpoint *ep, int timeout_ms);
91static void qla4xxx_ep_disconnect(struct iscsi_endpoint *ep);
92static int qla4xxx_get_ep_param(struct iscsi_endpoint *ep,
93 enum iscsi_param param, char *buf);
94static int qla4xxx_conn_start(struct iscsi_cls_conn *conn);
95static struct iscsi_cls_conn *
96qla4xxx_conn_create(struct iscsi_cls_session *cls_sess, uint32_t conn_idx);
97static int qla4xxx_conn_bind(struct iscsi_cls_session *cls_session,
98 struct iscsi_cls_conn *cls_conn,
99 uint64_t transport_fd, int is_leading);
100static void qla4xxx_conn_destroy(struct iscsi_cls_conn *conn);
101static struct iscsi_cls_session *
102qla4xxx_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max,
103 uint16_t qdepth, uint32_t initial_cmdsn);
104static void qla4xxx_session_destroy(struct iscsi_cls_session *sess);
105static void qla4xxx_task_work(struct work_struct *wdata);
106static int qla4xxx_alloc_pdu(struct iscsi_task *, uint8_t);
107static int qla4xxx_task_xmit(struct iscsi_task *);
108static void qla4xxx_task_cleanup(struct iscsi_task *);
109static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session);
110static void qla4xxx_conn_get_stats(struct iscsi_cls_conn *cls_conn,
111 struct iscsi_stats *stats);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700112/*
113 * SCSI host template entry points
114 */
Jeff Garzikf2812332010-11-16 02:10:29 -0500115static int qla4xxx_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmd);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530116static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700117static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
Mike Christiece545032008-02-29 18:25:20 -0600118static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700119static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
120static int qla4xxx_slave_alloc(struct scsi_device *device);
121static int qla4xxx_slave_configure(struct scsi_device *device);
122static void qla4xxx_slave_destroy(struct scsi_device *sdev);
Mike Christie3128c6c2011-07-25 13:48:42 -0500123static mode_t ql4_attr_is_visible(int param_type, int param);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700124
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530125static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
126 QLA82XX_LEGACY_INTR_CONFIG;
127
David Somayajuluafaf5a22006-09-19 10:28:00 -0700128static struct scsi_host_template qla4xxx_driver_template = {
129 .module = THIS_MODULE,
130 .name = DRIVER_NAME,
131 .proc_name = DRIVER_NAME,
132 .queuecommand = qla4xxx_queuecommand,
133
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530134 .eh_abort_handler = qla4xxx_eh_abort,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700135 .eh_device_reset_handler = qla4xxx_eh_device_reset,
Mike Christiece545032008-02-29 18:25:20 -0600136 .eh_target_reset_handler = qla4xxx_eh_target_reset,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700137 .eh_host_reset_handler = qla4xxx_eh_host_reset,
Mike Christie5c656af2009-07-15 15:02:59 -0500138 .eh_timed_out = qla4xxx_eh_cmd_timed_out,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700139
140 .slave_configure = qla4xxx_slave_configure,
141 .slave_alloc = qla4xxx_slave_alloc,
142 .slave_destroy = qla4xxx_slave_destroy,
143
144 .this_id = -1,
145 .cmd_per_lun = 3,
146 .use_clustering = ENABLE_CLUSTERING,
147 .sg_tablesize = SG_ALL,
148
149 .max_sectors = 0xFFFF,
Harish Zunjarrao7ad633c2011-05-17 23:17:11 -0700150 .shost_attrs = qla4xxx_host_attrs,
Vikas Chaudharya3559432011-07-25 13:48:51 -0500151 .vendor_id = SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_QLOGIC,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700152};
153
154static struct iscsi_transport qla4xxx_iscsi_transport = {
155 .owner = THIS_MODULE,
156 .name = DRIVER_NAME,
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500157 .caps = CAP_TEXT_NEGO |
158 CAP_DATA_PATH_OFFLOAD | CAP_HDRDGST |
159 CAP_DATADGST | CAP_LOGIN_OFFLOAD |
160 CAP_MULTI_R2T,
Mike Christie3128c6c2011-07-25 13:48:42 -0500161 .attr_is_visible = ql4_attr_is_visible,
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500162 .create_session = qla4xxx_session_create,
163 .destroy_session = qla4xxx_session_destroy,
164 .start_conn = qla4xxx_conn_start,
165 .create_conn = qla4xxx_conn_create,
166 .bind_conn = qla4xxx_conn_bind,
167 .stop_conn = iscsi_conn_stop,
168 .destroy_conn = qla4xxx_conn_destroy,
169 .set_param = iscsi_set_param,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700170 .get_conn_param = qla4xxx_conn_get_param,
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500171 .get_session_param = iscsi_session_get_param,
172 .get_ep_param = qla4xxx_get_ep_param,
173 .ep_connect = qla4xxx_ep_connect,
174 .ep_poll = qla4xxx_ep_poll,
175 .ep_disconnect = qla4xxx_ep_disconnect,
176 .get_stats = qla4xxx_conn_get_stats,
177 .send_pdu = iscsi_conn_send_pdu,
178 .xmit_task = qla4xxx_task_xmit,
179 .cleanup_task = qla4xxx_task_cleanup,
180 .alloc_pdu = qla4xxx_alloc_pdu,
181
Mike Christieaa1e93a2007-05-30 12:57:09 -0500182 .get_host_param = qla4xxx_host_get_param,
Mike Christied00efe32011-07-25 13:48:38 -0500183 .set_iface_param = qla4xxx_iface_set_param,
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500184 .get_iface_param = qla4xxx_get_iface_param,
Vikas Chaudharya3559432011-07-25 13:48:51 -0500185 .bsg_request = qla4xxx_bsg_request,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700186};
187
188static struct scsi_transport_template *qla4xxx_scsi_transport;
189
Mike Christie3128c6c2011-07-25 13:48:42 -0500190static mode_t ql4_attr_is_visible(int param_type, int param)
191{
192 switch (param_type) {
Mike Christief27fb2e2011-07-25 13:48:45 -0500193 case ISCSI_HOST_PARAM:
194 switch (param) {
195 case ISCSI_HOST_PARAM_HWADDRESS:
196 case ISCSI_HOST_PARAM_IPADDRESS:
197 case ISCSI_HOST_PARAM_INITIATOR_NAME:
198 return S_IRUGO;
199 default:
200 return 0;
201 }
Mike Christie3128c6c2011-07-25 13:48:42 -0500202 case ISCSI_PARAM:
203 switch (param) {
204 case ISCSI_PARAM_CONN_ADDRESS:
205 case ISCSI_PARAM_CONN_PORT:
Mike Christie1d063c12011-07-25 13:48:43 -0500206 case ISCSI_PARAM_TARGET_NAME:
207 case ISCSI_PARAM_TPGT:
208 case ISCSI_PARAM_TARGET_ALIAS:
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500209 case ISCSI_PARAM_MAX_BURST:
210 case ISCSI_PARAM_MAX_R2T:
211 case ISCSI_PARAM_FIRST_BURST:
212 case ISCSI_PARAM_MAX_RECV_DLENGTH:
213 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
Mike Christie3128c6c2011-07-25 13:48:42 -0500214 return S_IRUGO;
215 default:
216 return 0;
217 }
Mike Christieb78dbba2011-07-25 13:48:44 -0500218 case ISCSI_NET_PARAM:
219 switch (param) {
220 case ISCSI_NET_PARAM_IPV4_ADDR:
221 case ISCSI_NET_PARAM_IPV4_SUBNET:
222 case ISCSI_NET_PARAM_IPV4_GW:
223 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
224 case ISCSI_NET_PARAM_IFACE_ENABLE:
225 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
226 case ISCSI_NET_PARAM_IPV6_ADDR:
227 case ISCSI_NET_PARAM_IPV6_ROUTER:
228 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
229 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
Vikas Chaudhary6ac73e82011-07-25 13:48:49 -0500230 case ISCSI_NET_PARAM_VLAN_ID:
231 case ISCSI_NET_PARAM_VLAN_PRIORITY:
232 case ISCSI_NET_PARAM_VLAN_ENABLED:
Vikas Chaudhary943c1572011-08-01 03:26:13 -0700233 case ISCSI_NET_PARAM_MTU:
Vikas Chaudhary2ada7fc2011-08-01 03:26:19 -0700234 case ISCSI_NET_PARAM_PORT:
Mike Christieb78dbba2011-07-25 13:48:44 -0500235 return S_IRUGO;
236 default:
237 return 0;
238 }
Mike Christie3128c6c2011-07-25 13:48:42 -0500239 }
240
241 return 0;
242}
243
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500244static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
245 enum iscsi_param_type param_type,
246 int param, char *buf)
247{
248 struct Scsi_Host *shost = iscsi_iface_to_shost(iface);
249 struct scsi_qla_host *ha = to_qla_host(shost);
250 int len = -ENOSYS;
251
252 if (param_type != ISCSI_NET_PARAM)
253 return -ENOSYS;
254
255 switch (param) {
256 case ISCSI_NET_PARAM_IPV4_ADDR:
257 len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
258 break;
259 case ISCSI_NET_PARAM_IPV4_SUBNET:
260 len = sprintf(buf, "%pI4\n", &ha->ip_config.subnet_mask);
261 break;
262 case ISCSI_NET_PARAM_IPV4_GW:
263 len = sprintf(buf, "%pI4\n", &ha->ip_config.gateway);
264 break;
265 case ISCSI_NET_PARAM_IFACE_ENABLE:
266 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
267 len = sprintf(buf, "%s\n",
268 (ha->ip_config.ipv4_options &
269 IPOPT_IPV4_PROTOCOL_ENABLE) ?
270 "enabled" : "disabled");
271 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
272 len = sprintf(buf, "%s\n",
273 (ha->ip_config.ipv6_options &
274 IPV6_OPT_IPV6_PROTOCOL_ENABLE) ?
275 "enabled" : "disabled");
276 break;
277 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
278 len = sprintf(buf, "%s\n",
279 (ha->ip_config.tcp_options & TCPOPT_DHCP_ENABLE) ?
280 "dhcp" : "static");
281 break;
282 case ISCSI_NET_PARAM_IPV6_ADDR:
283 if (iface->iface_num == 0)
284 len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr0);
285 if (iface->iface_num == 1)
286 len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr1);
287 break;
288 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
289 len = sprintf(buf, "%pI6\n",
290 &ha->ip_config.ipv6_link_local_addr);
291 break;
292 case ISCSI_NET_PARAM_IPV6_ROUTER:
293 len = sprintf(buf, "%pI6\n",
294 &ha->ip_config.ipv6_default_router_addr);
295 break;
296 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
297 len = sprintf(buf, "%s\n",
298 (ha->ip_config.ipv6_addl_options &
299 IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) ?
300 "nd" : "static");
301 break;
302 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
303 len = sprintf(buf, "%s\n",
304 (ha->ip_config.ipv6_addl_options &
305 IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR) ?
306 "auto" : "static");
307 break;
Vikas Chaudhary6ac73e82011-07-25 13:48:49 -0500308 case ISCSI_NET_PARAM_VLAN_ID:
309 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
310 len = sprintf(buf, "%d\n",
311 (ha->ip_config.ipv4_vlan_tag &
312 ISCSI_MAX_VLAN_ID));
313 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
314 len = sprintf(buf, "%d\n",
315 (ha->ip_config.ipv6_vlan_tag &
316 ISCSI_MAX_VLAN_ID));
317 break;
318 case ISCSI_NET_PARAM_VLAN_PRIORITY:
319 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
320 len = sprintf(buf, "%d\n",
321 ((ha->ip_config.ipv4_vlan_tag >> 13) &
322 ISCSI_MAX_VLAN_PRIORITY));
323 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
324 len = sprintf(buf, "%d\n",
325 ((ha->ip_config.ipv6_vlan_tag >> 13) &
326 ISCSI_MAX_VLAN_PRIORITY));
327 break;
328 case ISCSI_NET_PARAM_VLAN_ENABLED:
329 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
330 len = sprintf(buf, "%s\n",
331 (ha->ip_config.ipv4_options &
332 IPOPT_VLAN_TAGGING_ENABLE) ?
333 "enabled" : "disabled");
334 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
335 len = sprintf(buf, "%s\n",
336 (ha->ip_config.ipv6_options &
337 IPV6_OPT_VLAN_TAGGING_ENABLE) ?
338 "enabled" : "disabled");
339 break;
Vikas Chaudhary943c1572011-08-01 03:26:13 -0700340 case ISCSI_NET_PARAM_MTU:
341 len = sprintf(buf, "%d\n", ha->ip_config.eth_mtu_size);
342 break;
Vikas Chaudhary2ada7fc2011-08-01 03:26:19 -0700343 case ISCSI_NET_PARAM_PORT:
344 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
345 len = sprintf(buf, "%d\n", ha->ip_config.ipv4_port);
346 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
347 len = sprintf(buf, "%d\n", ha->ip_config.ipv6_port);
348 break;
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500349 default:
350 len = -ENOSYS;
351 }
352
353 return len;
354}
355
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500356static struct iscsi_endpoint *
357qla4xxx_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
358 int non_blocking)
359{
360 int ret;
361 struct iscsi_endpoint *ep;
362 struct qla_endpoint *qla_ep;
363 struct scsi_qla_host *ha;
364 struct sockaddr_in *addr;
365 struct sockaddr_in6 *addr6;
366
367 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
368 if (!shost) {
369 ret = -ENXIO;
370 printk(KERN_ERR "%s: shost is NULL\n",
371 __func__);
372 return ERR_PTR(ret);
373 }
374
375 ha = iscsi_host_priv(shost);
376
377 ep = iscsi_create_endpoint(sizeof(struct qla_endpoint));
378 if (!ep) {
379 ret = -ENOMEM;
380 return ERR_PTR(ret);
381 }
382
383 qla_ep = ep->dd_data;
384 memset(qla_ep, 0, sizeof(struct qla_endpoint));
385 if (dst_addr->sa_family == AF_INET) {
386 memcpy(&qla_ep->dst_addr, dst_addr, sizeof(struct sockaddr_in));
387 addr = (struct sockaddr_in *)&qla_ep->dst_addr;
388 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI4\n", __func__,
389 (char *)&addr->sin_addr));
390 } else if (dst_addr->sa_family == AF_INET6) {
391 memcpy(&qla_ep->dst_addr, dst_addr,
392 sizeof(struct sockaddr_in6));
393 addr6 = (struct sockaddr_in6 *)&qla_ep->dst_addr;
394 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI6\n", __func__,
395 (char *)&addr6->sin6_addr));
396 }
397
398 qla_ep->host = shost;
399
400 return ep;
401}
402
403static int qla4xxx_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
404{
405 struct qla_endpoint *qla_ep;
406 struct scsi_qla_host *ha;
407 int ret = 0;
408
409 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
410 qla_ep = ep->dd_data;
411 ha = to_qla_host(qla_ep->host);
412
413 if (adapter_up(ha))
414 ret = 1;
415
416 return ret;
417}
418
419static void qla4xxx_ep_disconnect(struct iscsi_endpoint *ep)
420{
421 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
422 iscsi_destroy_endpoint(ep);
423}
424
425static int qla4xxx_get_ep_param(struct iscsi_endpoint *ep,
426 enum iscsi_param param,
427 char *buf)
428{
429 struct qla_endpoint *qla_ep = ep->dd_data;
430 struct sockaddr *dst_addr;
431
432 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
433
434 switch (param) {
435 case ISCSI_PARAM_CONN_PORT:
436 case ISCSI_PARAM_CONN_ADDRESS:
437 if (!qla_ep)
438 return -ENOTCONN;
439
440 dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
441 if (!dst_addr)
442 return -ENOTCONN;
443
444 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
445 &qla_ep->dst_addr, param, buf);
446 default:
447 return -ENOSYS;
448 }
449}
450
451static void qla4xxx_conn_get_stats(struct iscsi_cls_conn *cls_conn,
452 struct iscsi_stats *stats)
453{
454 struct iscsi_session *sess;
455 struct iscsi_cls_session *cls_sess;
456 struct ddb_entry *ddb_entry;
457 struct scsi_qla_host *ha;
458 struct ql_iscsi_stats *ql_iscsi_stats;
459 int stats_size;
460 int ret;
461 dma_addr_t iscsi_stats_dma;
462
463 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
464
465 cls_sess = iscsi_conn_to_session(cls_conn);
466 sess = cls_sess->dd_data;
467 ddb_entry = sess->dd_data;
468 ha = ddb_entry->ha;
469
470 stats_size = PAGE_ALIGN(sizeof(struct ql_iscsi_stats));
471 /* Allocate memory */
472 ql_iscsi_stats = dma_alloc_coherent(&ha->pdev->dev, stats_size,
473 &iscsi_stats_dma, GFP_KERNEL);
474 if (!ql_iscsi_stats) {
475 ql4_printk(KERN_ERR, ha,
476 "Unable to allocate memory for iscsi stats\n");
477 goto exit_get_stats;
478 }
479
480 ret = qla4xxx_get_mgmt_data(ha, ddb_entry->fw_ddb_index, stats_size,
481 iscsi_stats_dma);
482 if (ret != QLA_SUCCESS) {
483 ql4_printk(KERN_ERR, ha,
484 "Unable to retreive iscsi stats\n");
485 goto free_stats;
486 }
487
488 /* octets */
489 stats->txdata_octets = le64_to_cpu(ql_iscsi_stats->tx_data_octets);
490 stats->rxdata_octets = le64_to_cpu(ql_iscsi_stats->rx_data_octets);
491 /* xmit pdus */
492 stats->noptx_pdus = le32_to_cpu(ql_iscsi_stats->tx_nopout_pdus);
493 stats->scsicmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_cmd_pdus);
494 stats->tmfcmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_tmf_cmd_pdus);
495 stats->login_pdus = le32_to_cpu(ql_iscsi_stats->tx_login_cmd_pdus);
496 stats->text_pdus = le32_to_cpu(ql_iscsi_stats->tx_text_cmd_pdus);
497 stats->dataout_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_write_pdus);
498 stats->logout_pdus = le32_to_cpu(ql_iscsi_stats->tx_logout_cmd_pdus);
499 stats->snack_pdus = le32_to_cpu(ql_iscsi_stats->tx_snack_req_pdus);
500 /* recv pdus */
501 stats->noprx_pdus = le32_to_cpu(ql_iscsi_stats->rx_nopin_pdus);
502 stats->scsirsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_resp_pdus);
503 stats->tmfrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_tmf_resp_pdus);
504 stats->textrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_text_resp_pdus);
505 stats->datain_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_read_pdus);
506 stats->logoutrsp_pdus =
507 le32_to_cpu(ql_iscsi_stats->rx_logout_resp_pdus);
508 stats->r2t_pdus = le32_to_cpu(ql_iscsi_stats->rx_r2t_pdus);
509 stats->async_pdus = le32_to_cpu(ql_iscsi_stats->rx_async_pdus);
510 stats->rjt_pdus = le32_to_cpu(ql_iscsi_stats->rx_reject_pdus);
511
512free_stats:
513 dma_free_coherent(&ha->pdev->dev, stats_size, ql_iscsi_stats,
514 iscsi_stats_dma);
515exit_get_stats:
516 return;
517}
518
Mike Christie5c656af2009-07-15 15:02:59 -0500519static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
520{
521 struct iscsi_cls_session *session;
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500522 struct iscsi_session *sess;
523 unsigned long flags;
524 enum blk_eh_timer_return ret = BLK_EH_NOT_HANDLED;
Mike Christie5c656af2009-07-15 15:02:59 -0500525
526 session = starget_to_session(scsi_target(sc->device));
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500527 sess = session->dd_data;
Mike Christie5c656af2009-07-15 15:02:59 -0500528
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500529 spin_lock_irqsave(&session->lock, flags);
530 if (session->state == ISCSI_SESSION_FAILED)
531 ret = BLK_EH_RESET_TIMER;
532 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie5c656af2009-07-15 15:02:59 -0500533
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500534 return ret;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700535}
536
Mike Christieaa1e93a2007-05-30 12:57:09 -0500537static int qla4xxx_host_get_param(struct Scsi_Host *shost,
538 enum iscsi_host_param param, char *buf)
539{
540 struct scsi_qla_host *ha = to_qla_host(shost);
541 int len;
542
543 switch (param) {
544 case ISCSI_HOST_PARAM_HWADDRESS:
Michael Chan7ffc49a2007-12-24 21:28:09 -0800545 len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
Mike Christieaa1e93a2007-05-30 12:57:09 -0500546 break;
Mike Christie22236962007-05-30 12:57:24 -0500547 case ISCSI_HOST_PARAM_IPADDRESS:
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500548 len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
Mike Christie22236962007-05-30 12:57:24 -0500549 break;
Mike Christie8ad57812007-05-30 12:57:13 -0500550 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie22236962007-05-30 12:57:24 -0500551 len = sprintf(buf, "%s\n", ha->name_string);
Mike Christie8ad57812007-05-30 12:57:13 -0500552 break;
Mike Christieaa1e93a2007-05-30 12:57:09 -0500553 default:
554 return -ENOSYS;
555 }
556
557 return len;
558}
559
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500560static void qla4xxx_create_ipv4_iface(struct scsi_qla_host *ha)
561{
562 if (ha->iface_ipv4)
563 return;
564
565 /* IPv4 */
566 ha->iface_ipv4 = iscsi_create_iface(ha->host,
567 &qla4xxx_iscsi_transport,
568 ISCSI_IFACE_TYPE_IPV4, 0, 0);
569 if (!ha->iface_ipv4)
570 ql4_printk(KERN_ERR, ha, "Could not create IPv4 iSCSI "
571 "iface0.\n");
572}
573
574static void qla4xxx_create_ipv6_iface(struct scsi_qla_host *ha)
575{
576 if (!ha->iface_ipv6_0)
577 /* IPv6 iface-0 */
578 ha->iface_ipv6_0 = iscsi_create_iface(ha->host,
579 &qla4xxx_iscsi_transport,
580 ISCSI_IFACE_TYPE_IPV6, 0,
581 0);
582 if (!ha->iface_ipv6_0)
583 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
584 "iface0.\n");
585
586 if (!ha->iface_ipv6_1)
587 /* IPv6 iface-1 */
588 ha->iface_ipv6_1 = iscsi_create_iface(ha->host,
589 &qla4xxx_iscsi_transport,
590 ISCSI_IFACE_TYPE_IPV6, 1,
591 0);
592 if (!ha->iface_ipv6_1)
593 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
594 "iface1.\n");
595}
596
597static void qla4xxx_create_ifaces(struct scsi_qla_host *ha)
598{
599 if (ha->ip_config.ipv4_options & IPOPT_IPV4_PROTOCOL_ENABLE)
600 qla4xxx_create_ipv4_iface(ha);
601
602 if (ha->ip_config.ipv6_options & IPV6_OPT_IPV6_PROTOCOL_ENABLE)
603 qla4xxx_create_ipv6_iface(ha);
604}
605
606static void qla4xxx_destroy_ipv4_iface(struct scsi_qla_host *ha)
607{
608 if (ha->iface_ipv4) {
609 iscsi_destroy_iface(ha->iface_ipv4);
610 ha->iface_ipv4 = NULL;
611 }
612}
613
614static void qla4xxx_destroy_ipv6_iface(struct scsi_qla_host *ha)
615{
616 if (ha->iface_ipv6_0) {
617 iscsi_destroy_iface(ha->iface_ipv6_0);
618 ha->iface_ipv6_0 = NULL;
619 }
620 if (ha->iface_ipv6_1) {
621 iscsi_destroy_iface(ha->iface_ipv6_1);
622 ha->iface_ipv6_1 = NULL;
623 }
624}
625
626static void qla4xxx_destroy_ifaces(struct scsi_qla_host *ha)
627{
628 qla4xxx_destroy_ipv4_iface(ha);
629 qla4xxx_destroy_ipv6_iface(ha);
630}
631
Mike Christied00efe32011-07-25 13:48:38 -0500632static void qla4xxx_set_ipv6(struct scsi_qla_host *ha,
633 struct iscsi_iface_param_info *iface_param,
634 struct addr_ctrl_blk *init_fw_cb)
635{
636 /*
637 * iface_num 0 is valid for IPv6 Addr, linklocal, router, autocfg.
638 * iface_num 1 is valid only for IPv6 Addr.
639 */
640 switch (iface_param->param) {
641 case ISCSI_NET_PARAM_IPV6_ADDR:
642 if (iface_param->iface_num & 0x1)
643 /* IPv6 Addr 1 */
644 memcpy(init_fw_cb->ipv6_addr1, iface_param->value,
645 sizeof(init_fw_cb->ipv6_addr1));
646 else
647 /* IPv6 Addr 0 */
648 memcpy(init_fw_cb->ipv6_addr0, iface_param->value,
649 sizeof(init_fw_cb->ipv6_addr0));
650 break;
651 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
652 if (iface_param->iface_num & 0x1)
653 break;
654 memcpy(init_fw_cb->ipv6_if_id, &iface_param->value[8],
655 sizeof(init_fw_cb->ipv6_if_id));
656 break;
657 case ISCSI_NET_PARAM_IPV6_ROUTER:
658 if (iface_param->iface_num & 0x1)
659 break;
660 memcpy(init_fw_cb->ipv6_dflt_rtr_addr, iface_param->value,
661 sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
662 break;
663 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
664 /* Autocfg applies to even interface */
665 if (iface_param->iface_num & 0x1)
666 break;
667
668 if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_DISABLE)
669 init_fw_cb->ipv6_addtl_opts &=
670 cpu_to_le16(
671 ~IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
672 else if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_ND_ENABLE)
673 init_fw_cb->ipv6_addtl_opts |=
674 cpu_to_le16(
675 IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
676 else
677 ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
678 "IPv6 addr\n");
679 break;
680 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
681 /* Autocfg applies to even interface */
682 if (iface_param->iface_num & 0x1)
683 break;
684
685 if (iface_param->value[0] ==
686 ISCSI_IPV6_LINKLOCAL_AUTOCFG_ENABLE)
687 init_fw_cb->ipv6_addtl_opts |= cpu_to_le16(
688 IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
689 else if (iface_param->value[0] ==
690 ISCSI_IPV6_LINKLOCAL_AUTOCFG_DISABLE)
691 init_fw_cb->ipv6_addtl_opts &= cpu_to_le16(
692 ~IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
693 else
694 ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
695 "IPv6 linklocal addr\n");
696 break;
697 case ISCSI_NET_PARAM_IPV6_ROUTER_AUTOCFG:
698 /* Autocfg applies to even interface */
699 if (iface_param->iface_num & 0x1)
700 break;
701
702 if (iface_param->value[0] == ISCSI_IPV6_ROUTER_AUTOCFG_ENABLE)
703 memset(init_fw_cb->ipv6_dflt_rtr_addr, 0,
704 sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
705 break;
706 case ISCSI_NET_PARAM_IFACE_ENABLE:
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500707 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
Mike Christied00efe32011-07-25 13:48:38 -0500708 init_fw_cb->ipv6_opts |=
709 cpu_to_le16(IPV6_OPT_IPV6_PROTOCOL_ENABLE);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500710 qla4xxx_create_ipv6_iface(ha);
711 } else {
Mike Christied00efe32011-07-25 13:48:38 -0500712 init_fw_cb->ipv6_opts &=
713 cpu_to_le16(~IPV6_OPT_IPV6_PROTOCOL_ENABLE &
714 0xFFFF);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500715 qla4xxx_destroy_ipv6_iface(ha);
716 }
Mike Christied00efe32011-07-25 13:48:38 -0500717 break;
718 case ISCSI_NET_PARAM_VLAN_ID:
719 if (iface_param->len != sizeof(init_fw_cb->ipv6_vlan_tag))
720 break;
Vikas Chaudhary6ac73e82011-07-25 13:48:49 -0500721 init_fw_cb->ipv6_vlan_tag =
722 cpu_to_be16(*(uint16_t *)iface_param->value);
723 break;
724 case ISCSI_NET_PARAM_VLAN_ENABLED:
725 if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
726 init_fw_cb->ipv6_opts |=
727 cpu_to_le16(IPV6_OPT_VLAN_TAGGING_ENABLE);
728 else
729 init_fw_cb->ipv6_opts &=
730 cpu_to_le16(~IPV6_OPT_VLAN_TAGGING_ENABLE);
Mike Christied00efe32011-07-25 13:48:38 -0500731 break;
Vikas Chaudhary943c1572011-08-01 03:26:13 -0700732 case ISCSI_NET_PARAM_MTU:
733 init_fw_cb->eth_mtu_size =
734 cpu_to_le16(*(uint16_t *)iface_param->value);
735 break;
Vikas Chaudhary2ada7fc2011-08-01 03:26:19 -0700736 case ISCSI_NET_PARAM_PORT:
737 /* Autocfg applies to even interface */
738 if (iface_param->iface_num & 0x1)
739 break;
740
741 init_fw_cb->ipv6_port =
742 cpu_to_le16(*(uint16_t *)iface_param->value);
743 break;
Mike Christied00efe32011-07-25 13:48:38 -0500744 default:
745 ql4_printk(KERN_ERR, ha, "Unknown IPv6 param = %d\n",
746 iface_param->param);
747 break;
748 }
749}
750
751static void qla4xxx_set_ipv4(struct scsi_qla_host *ha,
752 struct iscsi_iface_param_info *iface_param,
753 struct addr_ctrl_blk *init_fw_cb)
754{
755 switch (iface_param->param) {
756 case ISCSI_NET_PARAM_IPV4_ADDR:
757 memcpy(init_fw_cb->ipv4_addr, iface_param->value,
758 sizeof(init_fw_cb->ipv4_addr));
759 break;
760 case ISCSI_NET_PARAM_IPV4_SUBNET:
761 memcpy(init_fw_cb->ipv4_subnet, iface_param->value,
762 sizeof(init_fw_cb->ipv4_subnet));
763 break;
764 case ISCSI_NET_PARAM_IPV4_GW:
765 memcpy(init_fw_cb->ipv4_gw_addr, iface_param->value,
766 sizeof(init_fw_cb->ipv4_gw_addr));
767 break;
768 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
769 if (iface_param->value[0] == ISCSI_BOOTPROTO_DHCP)
770 init_fw_cb->ipv4_tcp_opts |=
771 cpu_to_le16(TCPOPT_DHCP_ENABLE);
772 else if (iface_param->value[0] == ISCSI_BOOTPROTO_STATIC)
773 init_fw_cb->ipv4_tcp_opts &=
774 cpu_to_le16(~TCPOPT_DHCP_ENABLE);
775 else
776 ql4_printk(KERN_ERR, ha, "Invalid IPv4 bootproto\n");
777 break;
778 case ISCSI_NET_PARAM_IFACE_ENABLE:
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500779 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
Mike Christied00efe32011-07-25 13:48:38 -0500780 init_fw_cb->ipv4_ip_opts |=
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500781 cpu_to_le16(IPOPT_IPV4_PROTOCOL_ENABLE);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500782 qla4xxx_create_ipv4_iface(ha);
783 } else {
Mike Christied00efe32011-07-25 13:48:38 -0500784 init_fw_cb->ipv4_ip_opts &=
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500785 cpu_to_le16(~IPOPT_IPV4_PROTOCOL_ENABLE &
Mike Christied00efe32011-07-25 13:48:38 -0500786 0xFFFF);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500787 qla4xxx_destroy_ipv4_iface(ha);
788 }
Mike Christied00efe32011-07-25 13:48:38 -0500789 break;
790 case ISCSI_NET_PARAM_VLAN_ID:
791 if (iface_param->len != sizeof(init_fw_cb->ipv4_vlan_tag))
792 break;
Vikas Chaudhary6ac73e82011-07-25 13:48:49 -0500793 init_fw_cb->ipv4_vlan_tag =
794 cpu_to_be16(*(uint16_t *)iface_param->value);
795 break;
796 case ISCSI_NET_PARAM_VLAN_ENABLED:
797 if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
798 init_fw_cb->ipv4_ip_opts |=
799 cpu_to_le16(IPOPT_VLAN_TAGGING_ENABLE);
800 else
801 init_fw_cb->ipv4_ip_opts &=
802 cpu_to_le16(~IPOPT_VLAN_TAGGING_ENABLE);
Mike Christied00efe32011-07-25 13:48:38 -0500803 break;
Vikas Chaudhary943c1572011-08-01 03:26:13 -0700804 case ISCSI_NET_PARAM_MTU:
805 init_fw_cb->eth_mtu_size =
806 cpu_to_le16(*(uint16_t *)iface_param->value);
807 break;
Vikas Chaudhary2ada7fc2011-08-01 03:26:19 -0700808 case ISCSI_NET_PARAM_PORT:
809 init_fw_cb->ipv4_port =
810 cpu_to_le16(*(uint16_t *)iface_param->value);
811 break;
Mike Christied00efe32011-07-25 13:48:38 -0500812 default:
813 ql4_printk(KERN_ERR, ha, "Unknown IPv4 param = %d\n",
814 iface_param->param);
815 break;
816 }
817}
818
819static void
820qla4xxx_initcb_to_acb(struct addr_ctrl_blk *init_fw_cb)
821{
822 struct addr_ctrl_blk_def *acb;
823 acb = (struct addr_ctrl_blk_def *)init_fw_cb;
824 memset(acb->reserved1, 0, sizeof(acb->reserved1));
825 memset(acb->reserved2, 0, sizeof(acb->reserved2));
826 memset(acb->reserved3, 0, sizeof(acb->reserved3));
827 memset(acb->reserved4, 0, sizeof(acb->reserved4));
828 memset(acb->reserved5, 0, sizeof(acb->reserved5));
829 memset(acb->reserved6, 0, sizeof(acb->reserved6));
830 memset(acb->reserved7, 0, sizeof(acb->reserved7));
831 memset(acb->reserved8, 0, sizeof(acb->reserved8));
832 memset(acb->reserved9, 0, sizeof(acb->reserved9));
833 memset(acb->reserved10, 0, sizeof(acb->reserved10));
834 memset(acb->reserved11, 0, sizeof(acb->reserved11));
835 memset(acb->reserved12, 0, sizeof(acb->reserved12));
836 memset(acb->reserved13, 0, sizeof(acb->reserved13));
837 memset(acb->reserved14, 0, sizeof(acb->reserved14));
838 memset(acb->reserved15, 0, sizeof(acb->reserved15));
839}
840
841static int
842qla4xxx_iface_set_param(struct Scsi_Host *shost, char *data, int count)
843{
844 struct scsi_qla_host *ha = to_qla_host(shost);
845 int rval = 0;
846 struct iscsi_iface_param_info *iface_param = NULL;
847 struct addr_ctrl_blk *init_fw_cb = NULL;
848 dma_addr_t init_fw_cb_dma;
849 uint32_t mbox_cmd[MBOX_REG_COUNT];
850 uint32_t mbox_sts[MBOX_REG_COUNT];
851 uint32_t total_param_count;
852 uint32_t length;
853
854 init_fw_cb = dma_alloc_coherent(&ha->pdev->dev,
855 sizeof(struct addr_ctrl_blk),
856 &init_fw_cb_dma, GFP_KERNEL);
857 if (!init_fw_cb) {
858 ql4_printk(KERN_ERR, ha, "%s: Unable to alloc init_cb\n",
859 __func__);
860 return -ENOMEM;
861 }
862
863 memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
864 memset(&mbox_cmd, 0, sizeof(mbox_cmd));
865 memset(&mbox_sts, 0, sizeof(mbox_sts));
866
867 if (qla4xxx_get_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma)) {
868 ql4_printk(KERN_ERR, ha, "%s: get ifcb failed\n", __func__);
869 rval = -EIO;
870 goto exit_init_fw_cb;
871 }
872
873 total_param_count = count;
874 iface_param = (struct iscsi_iface_param_info *)data;
875
876 for ( ; total_param_count != 0; total_param_count--) {
877 length = iface_param->len;
878
879 if (iface_param->param_type != ISCSI_NET_PARAM)
880 continue;
881
882 switch (iface_param->iface_type) {
883 case ISCSI_IFACE_TYPE_IPV4:
884 switch (iface_param->iface_num) {
885 case 0:
886 qla4xxx_set_ipv4(ha, iface_param, init_fw_cb);
887 break;
888 default:
889 /* Cannot have more than one IPv4 interface */
890 ql4_printk(KERN_ERR, ha, "Invalid IPv4 iface "
891 "number = %d\n",
892 iface_param->iface_num);
893 break;
894 }
895 break;
896 case ISCSI_IFACE_TYPE_IPV6:
897 switch (iface_param->iface_num) {
898 case 0:
899 case 1:
900 qla4xxx_set_ipv6(ha, iface_param, init_fw_cb);
901 break;
902 default:
903 /* Cannot have more than two IPv6 interface */
904 ql4_printk(KERN_ERR, ha, "Invalid IPv6 iface "
905 "number = %d\n",
906 iface_param->iface_num);
907 break;
908 }
909 break;
910 default:
911 ql4_printk(KERN_ERR, ha, "Invalid iface type\n");
912 break;
913 }
914
915 iface_param = (struct iscsi_iface_param_info *)
916 ((uint8_t *)iface_param +
917 sizeof(struct iscsi_iface_param_info) + length);
918 }
919
920 init_fw_cb->cookie = cpu_to_le32(0x11BEAD5A);
921
922 rval = qla4xxx_set_flash(ha, init_fw_cb_dma, FLASH_SEGMENT_IFCB,
923 sizeof(struct addr_ctrl_blk),
924 FLASH_OPT_RMW_COMMIT);
925 if (rval != QLA_SUCCESS) {
926 ql4_printk(KERN_ERR, ha, "%s: set flash mbx failed\n",
927 __func__);
928 rval = -EIO;
929 goto exit_init_fw_cb;
930 }
931
932 qla4xxx_disable_acb(ha);
933
934 qla4xxx_initcb_to_acb(init_fw_cb);
935
936 rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma);
937 if (rval != QLA_SUCCESS) {
938 ql4_printk(KERN_ERR, ha, "%s: set acb mbx failed\n",
939 __func__);
940 rval = -EIO;
941 goto exit_init_fw_cb;
942 }
943
944 memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
945 qla4xxx_update_local_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb,
946 init_fw_cb_dma);
947
948exit_init_fw_cb:
949 dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk),
950 init_fw_cb, init_fw_cb_dma);
951
952 return rval;
953}
954
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500955static int qla4xxx_conn_get_param(struct iscsi_cls_conn *cls_conn,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700956 enum iscsi_param param, char *buf)
957{
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500958 struct iscsi_conn *conn;
959 struct qla_conn *qla_conn;
960 struct sockaddr *dst_addr;
961 int len = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700962
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500963 conn = cls_conn->dd_data;
964 qla_conn = conn->dd_data;
965 dst_addr = &qla_conn->qla_ep->dst_addr;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700966
967 switch (param) {
968 case ISCSI_PARAM_CONN_PORT:
David Somayajuluafaf5a22006-09-19 10:28:00 -0700969 case ISCSI_PARAM_CONN_ADDRESS:
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500970 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
971 dst_addr, param, buf);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700972 default:
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500973 return iscsi_conn_get_param(cls_conn, param, buf);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700974 }
975
976 return len;
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500977
David Somayajuluafaf5a22006-09-19 10:28:00 -0700978}
979
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500980static struct iscsi_cls_session *
981qla4xxx_session_create(struct iscsi_endpoint *ep,
982 uint16_t cmds_max, uint16_t qdepth,
983 uint32_t initial_cmdsn)
984{
985 struct iscsi_cls_session *cls_sess;
986 struct scsi_qla_host *ha;
987 struct qla_endpoint *qla_ep;
988 struct ddb_entry *ddb_entry;
989 uint32_t ddb_index;
990 uint32_t mbx_sts = 0;
991 struct iscsi_session *sess;
992 struct sockaddr *dst_addr;
993 int ret;
994
995 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
996 if (!ep) {
997 printk(KERN_ERR "qla4xxx: missing ep.\n");
998 return NULL;
999 }
1000
1001 qla_ep = ep->dd_data;
1002 dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
1003 ha = to_qla_host(qla_ep->host);
1004get_ddb_index:
1005 ddb_index = find_first_zero_bit(ha->ddb_idx_map, MAX_DDB_ENTRIES);
1006
1007 if (ddb_index >= MAX_DDB_ENTRIES) {
1008 DEBUG2(ql4_printk(KERN_INFO, ha,
1009 "Free DDB index not available\n"));
1010 return NULL;
1011 }
1012
1013 if (test_and_set_bit(ddb_index, ha->ddb_idx_map))
1014 goto get_ddb_index;
1015
1016 DEBUG2(ql4_printk(KERN_INFO, ha,
1017 "Found a free DDB index at %d\n", ddb_index));
1018 ret = qla4xxx_req_ddb_entry(ha, ddb_index, &mbx_sts);
1019 if (ret == QLA_ERROR) {
1020 if (mbx_sts == MBOX_STS_COMMAND_ERROR) {
1021 ql4_printk(KERN_INFO, ha,
1022 "DDB index = %d not available trying next\n",
1023 ddb_index);
1024 goto get_ddb_index;
1025 }
1026 DEBUG2(ql4_printk(KERN_INFO, ha,
1027 "Free FW DDB not available\n"));
1028 return NULL;
1029 }
1030
1031 cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport, qla_ep->host,
1032 cmds_max, sizeof(struct ddb_entry),
1033 sizeof(struct ql4_task_data),
1034 initial_cmdsn, ddb_index);
1035 if (!cls_sess)
1036 return NULL;
1037
1038 sess = cls_sess->dd_data;
1039 ddb_entry = sess->dd_data;
1040 ddb_entry->fw_ddb_index = ddb_index;
1041 ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
1042 ddb_entry->ha = ha;
1043 ddb_entry->sess = cls_sess;
1044 cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
1045 ha->fw_ddb_index_map[ddb_entry->fw_ddb_index] = ddb_entry;
1046 ha->tot_ddbs++;
1047
1048 return cls_sess;
1049}
1050
1051static void qla4xxx_session_destroy(struct iscsi_cls_session *cls_sess)
1052{
1053 struct iscsi_session *sess;
1054 struct ddb_entry *ddb_entry;
1055 struct scsi_qla_host *ha;
1056 unsigned long flags;
1057
1058 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1059 sess = cls_sess->dd_data;
1060 ddb_entry = sess->dd_data;
1061 ha = ddb_entry->ha;
1062
1063 spin_lock_irqsave(&ha->hardware_lock, flags);
1064 qla4xxx_free_ddb(ha, ddb_entry);
1065 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1066 iscsi_session_teardown(cls_sess);
1067}
1068
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001069static struct iscsi_cls_conn *
1070qla4xxx_conn_create(struct iscsi_cls_session *cls_sess, uint32_t conn_idx)
1071{
1072 struct iscsi_cls_conn *cls_conn;
1073 struct iscsi_session *sess;
1074 struct ddb_entry *ddb_entry;
1075
1076 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1077 cls_conn = iscsi_conn_setup(cls_sess, sizeof(struct qla_conn),
1078 conn_idx);
1079 sess = cls_sess->dd_data;
1080 ddb_entry = sess->dd_data;
1081 ddb_entry->conn = cls_conn;
1082
1083 return cls_conn;
1084}
1085
1086static int qla4xxx_conn_bind(struct iscsi_cls_session *cls_session,
1087 struct iscsi_cls_conn *cls_conn,
1088 uint64_t transport_fd, int is_leading)
1089{
1090 struct iscsi_conn *conn;
1091 struct qla_conn *qla_conn;
1092 struct iscsi_endpoint *ep;
1093
1094 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1095
1096 if (iscsi_conn_bind(cls_session, cls_conn, is_leading))
1097 return -EINVAL;
1098 ep = iscsi_lookup_endpoint(transport_fd);
1099 conn = cls_conn->dd_data;
1100 qla_conn = conn->dd_data;
1101 qla_conn->qla_ep = ep->dd_data;
1102 return 0;
1103}
1104
1105static int qla4xxx_conn_start(struct iscsi_cls_conn *cls_conn)
1106{
1107 struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
1108 struct iscsi_session *sess;
1109 struct ddb_entry *ddb_entry;
1110 struct scsi_qla_host *ha;
1111 struct dev_db_entry *fw_ddb_entry;
1112 dma_addr_t fw_ddb_entry_dma;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001113 uint32_t mbx_sts = 0;
1114 int ret = 0;
1115 int status = QLA_SUCCESS;
1116
1117 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1118 sess = cls_sess->dd_data;
1119 ddb_entry = sess->dd_data;
1120 ha = ddb_entry->ha;
1121
1122 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1123 &fw_ddb_entry_dma, GFP_KERNEL);
1124 if (!fw_ddb_entry) {
1125 ql4_printk(KERN_ERR, ha,
1126 "%s: Unable to allocate dma buffer\n", __func__);
1127 return -ENOMEM;
1128 }
1129
1130 ret = qla4xxx_set_param_ddbentry(ha, ddb_entry, cls_conn, &mbx_sts);
1131 if (ret) {
1132 /* If iscsid is stopped and started then no need to do
1133 * set param again since ddb state will be already
1134 * active and FW does not allow set ddb to an
1135 * active session.
1136 */
1137 if (mbx_sts)
1138 if (ddb_entry->fw_ddb_device_state ==
1139 DDB_DS_SESSION_ACTIVE)
1140 goto exit_set_param;
1141
1142 ql4_printk(KERN_ERR, ha, "%s: Failed set param for index[%d]\n",
1143 __func__, ddb_entry->fw_ddb_index);
1144 goto exit_conn_start;
1145 }
1146
1147 status = qla4xxx_conn_open(ha, ddb_entry->fw_ddb_index);
1148 if (status == QLA_ERROR) {
Manish Rangankar0e7e8502011-07-25 13:48:54 -05001149 ql4_printk(KERN_ERR, ha, "%s: Login failed: %s\n", __func__,
1150 sess->targetname);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001151 ret = -EINVAL;
1152 goto exit_conn_start;
1153 }
1154
1155 ddb_entry->fw_ddb_device_state = DDB_DS_LOGIN_IN_PROCESS;
1156
1157exit_set_param:
1158 iscsi_conn_start(cls_conn);
1159 ret = 0;
1160
1161exit_conn_start:
1162 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1163 fw_ddb_entry, fw_ddb_entry_dma);
1164 return ret;
1165}
1166
1167static void qla4xxx_conn_destroy(struct iscsi_cls_conn *cls_conn)
1168{
1169 struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
1170 struct iscsi_session *sess;
1171 struct scsi_qla_host *ha;
1172 struct ddb_entry *ddb_entry;
1173 int options;
1174
1175 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1176 sess = cls_sess->dd_data;
1177 ddb_entry = sess->dd_data;
1178 ha = ddb_entry->ha;
1179
1180 options = LOGOUT_OPTION_CLOSE_SESSION;
1181 if (qla4xxx_session_logout_ddb(ha, ddb_entry, options) == QLA_ERROR)
1182 ql4_printk(KERN_ERR, ha, "%s: Logout failed\n", __func__);
1183 else
1184 qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
1185
1186 /*
1187 * Clear the DDB bit so that next login can use the bit
1188 * if FW is not clearing the DDB entry then set DDB will fail anyways
1189 */
1190 clear_bit(ddb_entry->fw_ddb_index, ha->ddb_idx_map);
1191}
1192
1193static void qla4xxx_task_work(struct work_struct *wdata)
1194{
1195 struct ql4_task_data *task_data;
1196 struct scsi_qla_host *ha;
1197 struct passthru_status *sts;
1198 struct iscsi_task *task;
1199 struct iscsi_hdr *hdr;
1200 uint8_t *data;
1201 uint32_t data_len;
1202 struct iscsi_conn *conn;
1203 int hdr_len;
1204 itt_t itt;
1205
1206 task_data = container_of(wdata, struct ql4_task_data, task_work);
1207 ha = task_data->ha;
1208 task = task_data->task;
1209 sts = &task_data->sts;
1210 hdr_len = sizeof(struct iscsi_hdr);
1211
1212 DEBUG3(printk(KERN_INFO "Status returned\n"));
1213 DEBUG3(qla4xxx_dump_buffer(sts, 64));
1214 DEBUG3(printk(KERN_INFO "Response buffer"));
1215 DEBUG3(qla4xxx_dump_buffer(task_data->resp_buffer, 64));
1216
1217 conn = task->conn;
1218
1219 switch (sts->completionStatus) {
1220 case PASSTHRU_STATUS_COMPLETE:
1221 hdr = (struct iscsi_hdr *)task_data->resp_buffer;
1222 /* Assign back the itt in hdr, until we use the PREASSIGN_TAG */
1223 itt = sts->handle;
1224 hdr->itt = itt;
1225 data = task_data->resp_buffer + hdr_len;
1226 data_len = task_data->resp_len - hdr_len;
1227 iscsi_complete_pdu(conn, hdr, data, data_len);
1228 break;
1229 default:
1230 ql4_printk(KERN_ERR, ha, "Passthru failed status = 0x%x\n",
1231 sts->completionStatus);
1232 break;
1233 }
1234 return;
1235}
1236
1237static int qla4xxx_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
1238{
1239 struct ql4_task_data *task_data;
1240 struct iscsi_session *sess;
1241 struct ddb_entry *ddb_entry;
1242 struct scsi_qla_host *ha;
1243 int hdr_len;
1244
1245 sess = task->conn->session;
1246 ddb_entry = sess->dd_data;
1247 ha = ddb_entry->ha;
1248 task_data = task->dd_data;
1249 memset(task_data, 0, sizeof(struct ql4_task_data));
1250
1251 if (task->sc) {
1252 ql4_printk(KERN_INFO, ha,
1253 "%s: SCSI Commands not implemented\n", __func__);
1254 return -EINVAL;
1255 }
1256
1257 hdr_len = sizeof(struct iscsi_hdr);
1258 task_data->ha = ha;
1259 task_data->task = task;
1260
1261 if (task->data_count) {
1262 task_data->data_dma = dma_map_single(&ha->pdev->dev, task->data,
1263 task->data_count,
1264 PCI_DMA_TODEVICE);
1265 }
1266
1267 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
1268 __func__, task->conn->max_recv_dlength, hdr_len));
1269
1270 task_data->resp_len = task->conn->max_recv_dlength;
1271 task_data->resp_buffer = dma_alloc_coherent(&ha->pdev->dev,
1272 task_data->resp_len,
1273 &task_data->resp_dma,
1274 GFP_ATOMIC);
1275 if (!task_data->resp_buffer)
1276 goto exit_alloc_pdu;
1277
1278 task_data->req_buffer = dma_alloc_coherent(&ha->pdev->dev,
1279 task->data_count + hdr_len,
1280 &task_data->req_dma,
1281 GFP_ATOMIC);
1282 if (!task_data->req_buffer)
1283 goto exit_alloc_pdu;
1284
1285 task->hdr = task_data->req_buffer;
1286
1287 INIT_WORK(&task_data->task_work, qla4xxx_task_work);
1288
1289 return 0;
1290
1291exit_alloc_pdu:
1292 if (task_data->resp_buffer)
1293 dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
1294 task_data->resp_buffer, task_data->resp_dma);
1295
1296 if (task_data->req_buffer)
1297 dma_free_coherent(&ha->pdev->dev, task->data_count + hdr_len,
1298 task_data->req_buffer, task_data->req_dma);
1299 return -ENOMEM;
1300}
1301
1302static void qla4xxx_task_cleanup(struct iscsi_task *task)
1303{
1304 struct ql4_task_data *task_data;
1305 struct iscsi_session *sess;
1306 struct ddb_entry *ddb_entry;
1307 struct scsi_qla_host *ha;
1308 int hdr_len;
1309
1310 hdr_len = sizeof(struct iscsi_hdr);
1311 sess = task->conn->session;
1312 ddb_entry = sess->dd_data;
1313 ha = ddb_entry->ha;
1314 task_data = task->dd_data;
1315
1316 if (task->data_count) {
1317 dma_unmap_single(&ha->pdev->dev, task_data->data_dma,
1318 task->data_count, PCI_DMA_TODEVICE);
1319 }
1320
1321 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
1322 __func__, task->conn->max_recv_dlength, hdr_len));
1323
1324 dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
1325 task_data->resp_buffer, task_data->resp_dma);
1326 dma_free_coherent(&ha->pdev->dev, task->data_count + hdr_len,
1327 task_data->req_buffer, task_data->req_dma);
1328 return;
1329}
1330
1331static int qla4xxx_task_xmit(struct iscsi_task *task)
1332{
1333 struct scsi_cmnd *sc = task->sc;
1334 struct iscsi_session *sess = task->conn->session;
1335 struct ddb_entry *ddb_entry = sess->dd_data;
1336 struct scsi_qla_host *ha = ddb_entry->ha;
1337
1338 if (!sc)
1339 return qla4xxx_send_passthru0(task);
1340
1341 ql4_printk(KERN_INFO, ha, "%s: scsi cmd xmit not implemented\n",
1342 __func__);
1343 return -ENOSYS;
1344}
1345
1346void qla4xxx_update_session_conn_param(struct scsi_qla_host *ha,
1347 struct ddb_entry *ddb_entry)
1348{
1349 struct iscsi_cls_session *cls_sess;
1350 struct iscsi_cls_conn *cls_conn;
1351 struct iscsi_session *sess;
1352 struct iscsi_conn *conn;
1353 uint32_t ddb_state;
1354 dma_addr_t fw_ddb_entry_dma;
1355 struct dev_db_entry *fw_ddb_entry;
1356
1357 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1358 &fw_ddb_entry_dma, GFP_KERNEL);
1359 if (!fw_ddb_entry) {
1360 ql4_printk(KERN_ERR, ha,
1361 "%s: Unable to allocate dma buffer\n", __func__);
1362 return;
1363 }
1364
1365 if (qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index, fw_ddb_entry,
1366 fw_ddb_entry_dma, NULL, NULL, &ddb_state,
1367 NULL, NULL, NULL) == QLA_ERROR) {
1368 DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: failed "
1369 "get_ddb_entry for fw_ddb_index %d\n",
1370 ha->host_no, __func__,
1371 ddb_entry->fw_ddb_index));
1372 return;
1373 }
1374
1375 cls_sess = ddb_entry->sess;
1376 sess = cls_sess->dd_data;
1377
1378 cls_conn = ddb_entry->conn;
1379 conn = cls_conn->dd_data;
1380
1381 /* Update params */
1382 conn->max_recv_dlength = BYTE_UNITS *
1383 le16_to_cpu(fw_ddb_entry->iscsi_max_rcv_data_seg_len);
1384
1385 conn->max_xmit_dlength = BYTE_UNITS *
1386 le16_to_cpu(fw_ddb_entry->iscsi_max_snd_data_seg_len);
1387
1388 sess->initial_r2t_en =
1389 (BIT_10 & le16_to_cpu(fw_ddb_entry->iscsi_options));
1390
1391 sess->max_r2t = le16_to_cpu(fw_ddb_entry->iscsi_max_outsnd_r2t);
1392
1393 sess->imm_data_en = (BIT_11 & le16_to_cpu(fw_ddb_entry->iscsi_options));
1394
1395 sess->first_burst = BYTE_UNITS *
1396 le16_to_cpu(fw_ddb_entry->iscsi_first_burst_len);
1397
1398 sess->max_burst = BYTE_UNITS *
1399 le16_to_cpu(fw_ddb_entry->iscsi_max_burst_len);
1400
1401 sess->time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
1402
1403 sess->time2retain = le16_to_cpu(fw_ddb_entry->iscsi_def_time2retain);
1404
1405 sess->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
1406
1407 memcpy(sess->initiatorname, ha->name_string,
1408 min(sizeof(ha->name_string), sizeof(sess->initiatorname)));
1409}
1410
David Somayajuluafaf5a22006-09-19 10:28:00 -07001411/*
1412 * Timer routines
1413 */
1414
1415static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
1416 unsigned long interval)
1417{
1418 DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
1419 __func__, ha->host->host_no));
1420 init_timer(&ha->timer);
1421 ha->timer.expires = jiffies + interval * HZ;
1422 ha->timer.data = (unsigned long)ha;
1423 ha->timer.function = (void (*)(unsigned long))func;
1424 add_timer(&ha->timer);
1425 ha->timer_active = 1;
1426}
1427
1428static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
1429{
1430 del_timer_sync(&ha->timer);
1431 ha->timer_active = 0;
1432}
1433
1434/***
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001435 * qla4xxx_mark_device_missing - blocks the session
1436 * @cls_session: Pointer to the session to be blocked
David Somayajuluafaf5a22006-09-19 10:28:00 -07001437 * @ddb_entry: Pointer to device database entry
1438 *
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301439 * This routine marks a device missing and close connection.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001440 **/
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001441void qla4xxx_mark_device_missing(struct iscsi_cls_session *cls_session)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001442{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001443 iscsi_block_session(cls_session);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001444}
1445
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301446/**
1447 * qla4xxx_mark_all_devices_missing - mark all devices as missing.
1448 * @ha: Pointer to host adapter structure.
1449 *
1450 * This routine marks a device missing and resets the relogin retry count.
1451 **/
1452void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
1453{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001454 iscsi_host_for_each_session(ha->host, qla4xxx_mark_device_missing);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301455}
1456
David Somayajuluafaf5a22006-09-19 10:28:00 -07001457static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
1458 struct ddb_entry *ddb_entry,
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001459 struct scsi_cmnd *cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001460{
1461 struct srb *srb;
1462
1463 srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
1464 if (!srb)
1465 return srb;
1466
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301467 kref_init(&srb->srb_ref);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001468 srb->ha = ha;
1469 srb->ddb = ddb_entry;
1470 srb->cmd = cmd;
1471 srb->flags = 0;
Vikas Chaudhary53698872010-04-28 11:41:59 +05301472 CMD_SP(cmd) = (void *)srb;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001473
1474 return srb;
1475}
1476
1477static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
1478{
1479 struct scsi_cmnd *cmd = srb->cmd;
1480
1481 if (srb->flags & SRB_DMA_VALID) {
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +09001482 scsi_dma_unmap(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001483 srb->flags &= ~SRB_DMA_VALID;
1484 }
Vikas Chaudhary53698872010-04-28 11:41:59 +05301485 CMD_SP(cmd) = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001486}
1487
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301488void qla4xxx_srb_compl(struct kref *ref)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001489{
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301490 struct srb *srb = container_of(ref, struct srb, srb_ref);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001491 struct scsi_cmnd *cmd = srb->cmd;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301492 struct scsi_qla_host *ha = srb->ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001493
1494 qla4xxx_srb_free_dma(ha, srb);
1495
1496 mempool_free(srb, ha->srb_mempool);
1497
1498 cmd->scsi_done(cmd);
1499}
1500
1501/**
1502 * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001503 * @host: scsi host
David Somayajuluafaf5a22006-09-19 10:28:00 -07001504 * @cmd: Pointer to Linux's SCSI command structure
David Somayajuluafaf5a22006-09-19 10:28:00 -07001505 *
1506 * Remarks:
1507 * This routine is invoked by Linux to send a SCSI command to the driver.
1508 * The mid-level driver tries to ensure that queuecommand never gets
1509 * invoked concurrently with itself or the interrupt handler (although
1510 * the interrupt handler may call this routine as part of request-
1511 * completion handling). Unfortunely, it sometimes calls the scheduler
1512 * in interrupt context which is a big NO! NO!.
1513 **/
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001514static int qla4xxx_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001515{
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001516 struct scsi_qla_host *ha = to_qla_host(host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001517 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christie7fb19212008-01-31 13:36:45 -06001518 struct iscsi_cls_session *sess = ddb_entry->sess;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001519 struct srb *srb;
1520 int rval;
1521
Lalit Chandivade2232be02010-07-30 14:38:47 +05301522 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1523 if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
1524 cmd->result = DID_NO_CONNECT << 16;
1525 else
1526 cmd->result = DID_REQUEUE << 16;
1527 goto qc_fail_command;
1528 }
1529
Mike Christie7fb19212008-01-31 13:36:45 -06001530 if (!sess) {
1531 cmd->result = DID_IMM_RETRY << 16;
1532 goto qc_fail_command;
1533 }
1534
1535 rval = iscsi_session_chkready(sess);
1536 if (rval) {
1537 cmd->result = rval;
1538 goto qc_fail_command;
1539 }
1540
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301541 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1542 test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
1543 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1544 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
1545 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
1546 !test_bit(AF_ONLINE, &ha->flags) ||
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001547 !test_bit(AF_LINK_UP, &ha->flags) ||
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301548 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
David C Somayajulu477ffb92007-01-22 12:26:11 -08001549 goto qc_host_busy;
1550
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001551 srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001552 if (!srb)
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001553 goto qc_host_busy;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001554
1555 rval = qla4xxx_send_command_to_isp(ha, srb);
1556 if (rval != QLA_SUCCESS)
1557 goto qc_host_busy_free_sp;
1558
David Somayajuluafaf5a22006-09-19 10:28:00 -07001559 return 0;
1560
1561qc_host_busy_free_sp:
1562 qla4xxx_srb_free_dma(ha, srb);
1563 mempool_free(srb, ha->srb_mempool);
1564
David Somayajuluafaf5a22006-09-19 10:28:00 -07001565qc_host_busy:
1566 return SCSI_MLQUEUE_HOST_BUSY;
1567
1568qc_fail_command:
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001569 cmd->scsi_done(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001570
1571 return 0;
1572}
1573
1574/**
1575 * qla4xxx_mem_free - frees memory allocated to adapter
1576 * @ha: Pointer to host adapter structure.
1577 *
1578 * Frees memory previously allocated by qla4xxx_mem_alloc
1579 **/
1580static void qla4xxx_mem_free(struct scsi_qla_host *ha)
1581{
1582 if (ha->queues)
1583 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
1584 ha->queues_dma);
1585
1586 ha->queues_len = 0;
1587 ha->queues = NULL;
1588 ha->queues_dma = 0;
1589 ha->request_ring = NULL;
1590 ha->request_dma = 0;
1591 ha->response_ring = NULL;
1592 ha->response_dma = 0;
1593 ha->shadow_regs = NULL;
1594 ha->shadow_regs_dma = 0;
1595
1596 /* Free srb pool. */
1597 if (ha->srb_mempool)
1598 mempool_destroy(ha->srb_mempool);
1599
1600 ha->srb_mempool = NULL;
1601
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001602 if (ha->chap_dma_pool)
1603 dma_pool_destroy(ha->chap_dma_pool);
1604
David Somayajuluafaf5a22006-09-19 10:28:00 -07001605 /* release io space registers */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301606 if (is_qla8022(ha)) {
1607 if (ha->nx_pcibase)
1608 iounmap(
1609 (struct device_reg_82xx __iomem *)ha->nx_pcibase);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301610 } else if (ha->reg)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001611 iounmap(ha->reg);
1612 pci_release_regions(ha->pdev);
1613}
1614
1615/**
1616 * qla4xxx_mem_alloc - allocates memory for use by adapter.
1617 * @ha: Pointer to host adapter structure
1618 *
1619 * Allocates DMA memory for request and response queues. Also allocates memory
1620 * for srbs.
1621 **/
1622static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
1623{
1624 unsigned long align;
1625
1626 /* Allocate contiguous block of DMA memory for queues. */
1627 ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
1628 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
1629 sizeof(struct shadow_regs) +
1630 MEM_ALIGN_VALUE +
1631 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
1632 ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
1633 &ha->queues_dma, GFP_KERNEL);
1634 if (ha->queues == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301635 ql4_printk(KERN_WARNING, ha,
1636 "Memory Allocation failed - queues.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001637
1638 goto mem_alloc_error_exit;
1639 }
1640 memset(ha->queues, 0, ha->queues_len);
1641
1642 /*
1643 * As per RISC alignment requirements -- the bus-address must be a
1644 * multiple of the request-ring size (in bytes).
1645 */
1646 align = 0;
1647 if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
1648 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
1649 (MEM_ALIGN_VALUE - 1));
1650
1651 /* Update request and response queue pointers. */
1652 ha->request_dma = ha->queues_dma + align;
1653 ha->request_ring = (struct queue_entry *) (ha->queues + align);
1654 ha->response_dma = ha->queues_dma + align +
1655 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
1656 ha->response_ring = (struct queue_entry *) (ha->queues + align +
1657 (REQUEST_QUEUE_DEPTH *
1658 QUEUE_SIZE));
1659 ha->shadow_regs_dma = ha->queues_dma + align +
1660 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
1661 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
1662 ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
1663 (REQUEST_QUEUE_DEPTH *
1664 QUEUE_SIZE) +
1665 (RESPONSE_QUEUE_DEPTH *
1666 QUEUE_SIZE));
1667
1668 /* Allocate memory for srb pool. */
1669 ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
1670 mempool_free_slab, srb_cachep);
1671 if (ha->srb_mempool == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301672 ql4_printk(KERN_WARNING, ha,
1673 "Memory Allocation failed - SRB Pool.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001674
1675 goto mem_alloc_error_exit;
1676 }
1677
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001678 ha->chap_dma_pool = dma_pool_create("ql4_chap", &ha->pdev->dev,
1679 CHAP_DMA_BLOCK_SIZE, 8, 0);
1680
1681 if (ha->chap_dma_pool == NULL) {
1682 ql4_printk(KERN_WARNING, ha,
1683 "%s: chap_dma_pool allocation failed..\n", __func__);
1684 goto mem_alloc_error_exit;
1685 }
1686
David Somayajuluafaf5a22006-09-19 10:28:00 -07001687 return QLA_SUCCESS;
1688
1689mem_alloc_error_exit:
1690 qla4xxx_mem_free(ha);
1691 return QLA_ERROR;
1692}
1693
1694/**
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301695 * qla4_8xxx_check_fw_alive - Check firmware health
1696 * @ha: Pointer to host adapter structure.
1697 *
1698 * Context: Interrupt
1699 **/
1700static void qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
1701{
1702 uint32_t fw_heartbeat_counter, halt_status;
1703
1704 fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
Lalit Chandivade2232be02010-07-30 14:38:47 +05301705 /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
1706 if (fw_heartbeat_counter == 0xffffffff) {
1707 DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
1708 "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
1709 ha->host_no, __func__));
1710 return;
1711 }
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301712
1713 if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
1714 ha->seconds_since_last_heartbeat++;
1715 /* FW not alive after 2 seconds */
1716 if (ha->seconds_since_last_heartbeat == 2) {
1717 ha->seconds_since_last_heartbeat = 0;
1718 halt_status = qla4_8xxx_rd_32(ha,
Vikas Chaudhary68d92eb2011-05-17 23:17:05 -07001719 QLA82XX_PEG_HALT_STATUS1);
1720
1721 ql4_printk(KERN_INFO, ha,
1722 "scsi(%ld): %s, Dumping hw/fw registers:\n "
1723 " PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2:"
1724 " 0x%x,\n PEG_NET_0_PC: 0x%x, PEG_NET_1_PC:"
1725 " 0x%x,\n PEG_NET_2_PC: 0x%x, PEG_NET_3_PC:"
1726 " 0x%x,\n PEG_NET_4_PC: 0x%x\n",
1727 ha->host_no, __func__, halt_status,
1728 qla4_8xxx_rd_32(ha,
1729 QLA82XX_PEG_HALT_STATUS2),
1730 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_0 +
1731 0x3c),
1732 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_1 +
1733 0x3c),
1734 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_2 +
1735 0x3c),
1736 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_3 +
1737 0x3c),
1738 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_4 +
1739 0x3c));
Nilesh Javali21033632010-07-30 14:28:07 +05301740
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301741 /* Since we cannot change dev_state in interrupt
1742 * context, set appropriate DPC flag then wakeup
1743 * DPC */
1744 if (halt_status & HALT_STATUS_UNRECOVERABLE)
1745 set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
1746 else {
1747 printk("scsi%ld: %s: detect abort needed!\n",
1748 ha->host_no, __func__);
1749 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1750 }
1751 qla4xxx_wake_dpc(ha);
Nilesh Javali21033632010-07-30 14:28:07 +05301752 qla4xxx_mailbox_premature_completion(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301753 }
Lalit Chandivade99457d72010-10-06 22:49:32 -07001754 } else
1755 ha->seconds_since_last_heartbeat = 0;
1756
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301757 ha->fw_heartbeat_counter = fw_heartbeat_counter;
1758}
1759
1760/**
1761 * qla4_8xxx_watchdog - Poll dev state
1762 * @ha: Pointer to host adapter structure.
1763 *
1764 * Context: Interrupt
1765 **/
1766void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
1767{
1768 uint32_t dev_state;
1769
1770 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
1771
1772 /* don't poll if reset is going on */
Lalit Chandivaded56a1f72010-12-02 22:12:45 -08001773 if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
1774 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
Vikas Chaudhary977f46a2011-05-17 23:17:08 -07001775 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags))) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301776 if (dev_state == QLA82XX_DEV_NEED_RESET &&
1777 !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
Vikas Chaudhary3930b8c2010-12-02 22:12:47 -08001778 if (!ql4xdontresethba) {
1779 ql4_printk(KERN_INFO, ha, "%s: HW State: "
1780 "NEED RESET!\n", __func__);
1781 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1782 qla4xxx_wake_dpc(ha);
1783 qla4xxx_mailbox_premature_completion(ha);
1784 }
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301785 } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
1786 !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
Vikas Chaudhary3930b8c2010-12-02 22:12:47 -08001787 ql4_printk(KERN_INFO, ha, "%s: HW State: NEED QUIES!\n",
1788 __func__);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301789 set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
1790 qla4xxx_wake_dpc(ha);
1791 } else {
1792 /* Check firmware health */
1793 qla4_8xxx_check_fw_alive(ha);
1794 }
1795 }
1796}
1797
1798/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07001799 * qla4xxx_timer - checks every second for work to do.
1800 * @ha: Pointer to host adapter structure.
1801 **/
1802static void qla4xxx_timer(struct scsi_qla_host *ha)
1803{
David Somayajuluafaf5a22006-09-19 10:28:00 -07001804 int start_dpc = 0;
Lalit Chandivade2232be02010-07-30 14:38:47 +05301805 uint16_t w;
1806
1807 /* If we are in the middle of AER/EEH processing
1808 * skip any processing and reschedule the timer
1809 */
1810 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1811 mod_timer(&ha->timer, jiffies + HZ);
1812 return;
1813 }
1814
1815 /* Hardware read to trigger an EEH error during mailbox waits. */
1816 if (!pci_channel_offline(ha->pdev))
1817 pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001818
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301819 if (is_qla8022(ha)) {
1820 qla4_8xxx_watchdog(ha);
1821 }
1822
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301823 if (!is_qla8022(ha)) {
1824 /* Check for heartbeat interval. */
1825 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
1826 ha->heartbeat_interval != 0) {
1827 ha->seconds_since_last_heartbeat++;
1828 if (ha->seconds_since_last_heartbeat >
1829 ha->heartbeat_interval + 2)
1830 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1831 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001832 }
1833
David Somayajuluafaf5a22006-09-19 10:28:00 -07001834 /* Wakeup the dpc routine for this adapter, if needed. */
Lalit Chandivade1b468072011-05-17 23:17:09 -07001835 if (start_dpc ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07001836 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1837 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
1838 test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301839 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07001840 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1841 test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301842 test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301843 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
1844 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
Lalit Chandivade1b468072011-05-17 23:17:09 -07001845 test_bit(DPC_AEN, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001846 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
1847 " - dpc flags = 0x%lx\n",
1848 ha->host_no, __func__, ha->dpc_flags));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301849 qla4xxx_wake_dpc(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001850 }
1851
1852 /* Reschedule timer thread to call us back in one second */
1853 mod_timer(&ha->timer, jiffies + HZ);
1854
1855 DEBUG2(ha->seconds_since_last_intr++);
1856}
1857
1858/**
1859 * qla4xxx_cmd_wait - waits for all outstanding commands to complete
1860 * @ha: Pointer to host adapter structure.
1861 *
1862 * This routine stalls the driver until all outstanding commands are returned.
1863 * Caller must release the Hardware Lock prior to calling this routine.
1864 **/
1865static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
1866{
1867 uint32_t index = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001868 unsigned long flags;
1869 struct scsi_cmnd *cmd;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001870
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301871 unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
1872
1873 DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
1874 "complete\n", WAIT_CMD_TOV));
1875
1876 while (!time_after_eq(jiffies, wtime)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001877 spin_lock_irqsave(&ha->hardware_lock, flags);
1878 /* Find a command that hasn't completed. */
1879 for (index = 0; index < ha->host->can_queue; index++) {
1880 cmd = scsi_host_find_tag(ha->host, index);
Mike Christiea1e00632010-10-26 05:45:30 -07001881 /*
1882 * We cannot just check if the index is valid,
1883 * becase if we are run from the scsi eh, then
1884 * the scsi/block layer is going to prevent
1885 * the tag from being released.
1886 */
1887 if (cmd != NULL && CMD_SP(cmd))
David Somayajuluafaf5a22006-09-19 10:28:00 -07001888 break;
1889 }
1890 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1891
1892 /* If No Commands are pending, wait is complete */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301893 if (index == ha->host->can_queue)
1894 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001895
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301896 msleep(1000);
1897 }
1898 /* If we timed out on waiting for commands to come back
1899 * return ERROR. */
1900 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001901}
1902
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301903int qla4xxx_hw_reset(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001904{
David Somayajuluafaf5a22006-09-19 10:28:00 -07001905 uint32_t ctrl_status;
David C Somayajulu477ffb92007-01-22 12:26:11 -08001906 unsigned long flags = 0;
1907
1908 DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001909
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301910 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
1911 return QLA_ERROR;
1912
David Somayajuluafaf5a22006-09-19 10:28:00 -07001913 spin_lock_irqsave(&ha->hardware_lock, flags);
1914
1915 /*
1916 * If the SCSI Reset Interrupt bit is set, clear it.
1917 * Otherwise, the Soft Reset won't work.
1918 */
1919 ctrl_status = readw(&ha->reg->ctrl_status);
1920 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
1921 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
1922
1923 /* Issue Soft Reset */
1924 writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
1925 readl(&ha->reg->ctrl_status);
1926
1927 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301928 return QLA_SUCCESS;
David C Somayajulu477ffb92007-01-22 12:26:11 -08001929}
1930
1931/**
1932 * qla4xxx_soft_reset - performs soft reset.
1933 * @ha: Pointer to host adapter structure.
1934 **/
1935int qla4xxx_soft_reset(struct scsi_qla_host *ha)
1936{
1937 uint32_t max_wait_time;
1938 unsigned long flags = 0;
Vikas Chaudharyf931c532010-10-06 22:48:07 -07001939 int status;
David C Somayajulu477ffb92007-01-22 12:26:11 -08001940 uint32_t ctrl_status;
1941
Vikas Chaudharyf931c532010-10-06 22:48:07 -07001942 status = qla4xxx_hw_reset(ha);
1943 if (status != QLA_SUCCESS)
1944 return status;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001945
Vikas Chaudharyf931c532010-10-06 22:48:07 -07001946 status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001947 /* Wait until the Network Reset Intr bit is cleared */
1948 max_wait_time = RESET_INTR_TOV;
1949 do {
1950 spin_lock_irqsave(&ha->hardware_lock, flags);
1951 ctrl_status = readw(&ha->reg->ctrl_status);
1952 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1953
1954 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
1955 break;
1956
1957 msleep(1000);
1958 } while ((--max_wait_time));
1959
1960 if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
1961 DEBUG2(printk(KERN_WARNING
1962 "scsi%ld: Network Reset Intr not cleared by "
1963 "Network function, clearing it now!\n",
1964 ha->host_no));
1965 spin_lock_irqsave(&ha->hardware_lock, flags);
1966 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
1967 readl(&ha->reg->ctrl_status);
1968 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1969 }
1970
1971 /* Wait until the firmware tells us the Soft Reset is done */
1972 max_wait_time = SOFT_RESET_TOV;
1973 do {
1974 spin_lock_irqsave(&ha->hardware_lock, flags);
1975 ctrl_status = readw(&ha->reg->ctrl_status);
1976 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1977
1978 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
1979 status = QLA_SUCCESS;
1980 break;
1981 }
1982
1983 msleep(1000);
1984 } while ((--max_wait_time));
1985
1986 /*
1987 * Also, make sure that the SCSI Reset Interrupt bit has been cleared
1988 * after the soft reset has taken place.
1989 */
1990 spin_lock_irqsave(&ha->hardware_lock, flags);
1991 ctrl_status = readw(&ha->reg->ctrl_status);
1992 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
1993 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
1994 readl(&ha->reg->ctrl_status);
1995 }
1996 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1997
1998 /* If soft reset fails then most probably the bios on other
1999 * function is also enabled.
2000 * Since the initialization is sequential the other fn
2001 * wont be able to acknowledge the soft reset.
2002 * Issue a force soft reset to workaround this scenario.
2003 */
2004 if (max_wait_time == 0) {
2005 /* Issue Force Soft Reset */
2006 spin_lock_irqsave(&ha->hardware_lock, flags);
2007 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
2008 readl(&ha->reg->ctrl_status);
2009 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2010 /* Wait until the firmware tells us the Soft Reset is done */
2011 max_wait_time = SOFT_RESET_TOV;
2012 do {
2013 spin_lock_irqsave(&ha->hardware_lock, flags);
2014 ctrl_status = readw(&ha->reg->ctrl_status);
2015 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2016
2017 if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
2018 status = QLA_SUCCESS;
2019 break;
2020 }
2021
2022 msleep(1000);
2023 } while ((--max_wait_time));
2024 }
2025
2026 return status;
2027}
2028
2029/**
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302030 * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
David Somayajuluafaf5a22006-09-19 10:28:00 -07002031 * @ha: Pointer to host adapter structure.
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302032 * @res: returned scsi status
David Somayajuluafaf5a22006-09-19 10:28:00 -07002033 *
2034 * This routine is called just prior to a HARD RESET to return all
2035 * outstanding commands back to the Operating System.
2036 * Caller should make sure that the following locks are released
2037 * before this calling routine: Hardware lock, and io_request_lock.
2038 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302039static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002040{
2041 struct srb *srb;
2042 int i;
2043 unsigned long flags;
2044
2045 spin_lock_irqsave(&ha->hardware_lock, flags);
2046 for (i = 0; i < ha->host->can_queue; i++) {
2047 srb = qla4xxx_del_from_active_array(ha, i);
2048 if (srb != NULL) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302049 srb->cmd->result = res;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302050 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002051 }
2052 }
2053 spin_unlock_irqrestore(&ha->hardware_lock, flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002054}
2055
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302056void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
2057{
2058 clear_bit(AF_ONLINE, &ha->flags);
2059
2060 /* Disable the board */
2061 ql4_printk(KERN_INFO, ha, "Disabling the board\n");
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302062
2063 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
2064 qla4xxx_mark_all_devices_missing(ha);
2065 clear_bit(AF_INIT_DONE, &ha->flags);
2066}
2067
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002068static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session)
2069{
2070 struct iscsi_session *sess;
2071 struct ddb_entry *ddb_entry;
2072
2073 sess = cls_session->dd_data;
2074 ddb_entry = sess->dd_data;
2075 ddb_entry->fw_ddb_device_state = DDB_DS_SESSION_FAILED;
2076 iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_CONN_FAILED);
2077}
2078
David Somayajuluafaf5a22006-09-19 10:28:00 -07002079/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002080 * qla4xxx_recover_adapter - recovers adapter after a fatal error
2081 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07002082 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302083static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002084{
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302085 int status = QLA_ERROR;
2086 uint8_t reset_chip = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002087
2088 /* Stall incoming I/O until we are done */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302089 scsi_block_requests(ha->host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002090 clear_bit(AF_ONLINE, &ha->flags);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002091 clear_bit(AF_LINK_UP, &ha->flags);
Mike Christie50a29ae2008-03-04 13:26:53 -06002092
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302093 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -07002094
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302095 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002096
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002097 iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
2098
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302099 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
2100 reset_chip = 1;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002101
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302102 /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
2103 * do not reset adapter, jump to initialize_adapter */
2104 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
2105 status = QLA_SUCCESS;
2106 goto recover_ha_init_adapter;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002107 }
2108
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302109 /* For the ISP-82xx adapter, issue a stop_firmware if invoked
2110 * from eh_host_reset or ioctl module */
2111 if (is_qla8022(ha) && !reset_chip &&
2112 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
2113
2114 DEBUG2(ql4_printk(KERN_INFO, ha,
2115 "scsi%ld: %s - Performing stop_firmware...\n",
2116 ha->host_no, __func__));
2117 status = ha->isp_ops->reset_firmware(ha);
2118 if (status == QLA_SUCCESS) {
Nilesh Javali2bd1e2be2010-10-06 22:49:20 -07002119 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
2120 qla4xxx_cmd_wait(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302121 ha->isp_ops->disable_intrs(ha);
2122 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2123 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
2124 } else {
2125 /* If the stop_firmware fails then
2126 * reset the entire chip */
2127 reset_chip = 1;
2128 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2129 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2130 }
2131 }
2132
2133 /* Issue full chip reset if recovering from a catastrophic error,
2134 * or if stop_firmware fails for ISP-82xx.
2135 * This is the default case for ISP-4xxx */
2136 if (!is_qla8022(ha) || reset_chip) {
Nilesh Javali2bd1e2be2010-10-06 22:49:20 -07002137 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
2138 qla4xxx_cmd_wait(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302139 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2140 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
2141 DEBUG2(ql4_printk(KERN_INFO, ha,
2142 "scsi%ld: %s - Performing chip reset..\n",
2143 ha->host_no, __func__));
2144 status = ha->isp_ops->reset_chip(ha);
2145 }
2146
2147 /* Flush any pending ddb changed AENs */
2148 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2149
2150recover_ha_init_adapter:
2151 /* Upon successful firmware/chip reset, re-initialize the adapter */
2152 if (status == QLA_SUCCESS) {
2153 /* For ISP-4xxx, force function 1 to always initialize
2154 * before function 3 to prevent both funcions from
2155 * stepping on top of the other */
2156 if (!is_qla8022(ha) && (ha->mac_index == 3))
2157 ssleep(6);
2158
2159 /* NOTE: AF_ONLINE flag set upon successful completion of
2160 * qla4xxx_initialize_adapter */
Manish Rangankar0e7e8502011-07-25 13:48:54 -05002161 status = qla4xxx_initialize_adapter(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302162 }
2163
2164 /* Retry failed adapter initialization, if necessary
2165 * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
2166 * case to prevent ping-pong resets between functions */
2167 if (!test_bit(AF_ONLINE, &ha->flags) &&
2168 !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002169 /* Adapter initialization failed, see if we can retry
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302170 * resetting the ha.
2171 * Since we don't want to block the DPC for too long
2172 * with multiple resets in the same thread,
2173 * utilize DPC to retry */
David Somayajuluafaf5a22006-09-19 10:28:00 -07002174 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
2175 ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
2176 DEBUG2(printk("scsi%ld: recover adapter - retrying "
2177 "(%d) more times\n", ha->host_no,
2178 ha->retry_reset_ha_cnt));
2179 set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
2180 status = QLA_ERROR;
2181 } else {
2182 if (ha->retry_reset_ha_cnt > 0) {
2183 /* Schedule another Reset HA--DPC will retry */
2184 ha->retry_reset_ha_cnt--;
2185 DEBUG2(printk("scsi%ld: recover adapter - "
2186 "retry remaining %d\n",
2187 ha->host_no,
2188 ha->retry_reset_ha_cnt));
2189 status = QLA_ERROR;
2190 }
2191
2192 if (ha->retry_reset_ha_cnt == 0) {
2193 /* Recover adapter retries have been exhausted.
2194 * Adapter DEAD */
2195 DEBUG2(printk("scsi%ld: recover adapter "
2196 "failed - board disabled\n",
2197 ha->host_no));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302198 qla4xxx_dead_adapter_cleanup(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002199 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
2200 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302201 clear_bit(DPC_RESET_HA_FW_CONTEXT,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002202 &ha->dpc_flags);
2203 status = QLA_ERROR;
2204 }
2205 }
2206 } else {
2207 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302208 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002209 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
2210 }
2211
2212 ha->adapter_error_count++;
2213
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302214 if (test_bit(AF_ONLINE, &ha->flags))
2215 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002216
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302217 scsi_unblock_requests(ha->host);
2218
2219 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2220 DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002221 status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302222
David Somayajuluafaf5a22006-09-19 10:28:00 -07002223 return status;
2224}
2225
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002226static void qla4xxx_relogin_devices(struct iscsi_cls_session *cls_session)
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07002227{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002228 struct iscsi_session *sess;
2229 struct ddb_entry *ddb_entry;
2230 struct scsi_qla_host *ha;
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07002231
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002232 sess = cls_session->dd_data;
2233 ddb_entry = sess->dd_data;
2234 ha = ddb_entry->ha;
2235 if (!iscsi_is_session_online(cls_session)) {
2236 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
2237 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
2238 " unblock session\n", ha->host_no, __func__,
2239 ddb_entry->fw_ddb_index);
2240 iscsi_unblock_session(ddb_entry->sess);
2241 } else {
2242 /* Trigger relogin */
2243 iscsi_session_failure(cls_session->dd_data,
2244 ISCSI_ERR_CONN_FAILED);
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07002245 }
2246 }
2247}
2248
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002249static void qla4xxx_relogin_all_devices(struct scsi_qla_host *ha)
2250{
2251 iscsi_host_for_each_session(ha->host, qla4xxx_relogin_devices);
2252}
2253
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302254void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
2255{
Lalit Chandivade1b468072011-05-17 23:17:09 -07002256 if (ha->dpc_thread)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302257 queue_work(ha->dpc_thread, &ha->dpc_work);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302258}
2259
David Somayajuluafaf5a22006-09-19 10:28:00 -07002260/**
2261 * qla4xxx_do_dpc - dpc routine
2262 * @data: in our case pointer to adapter structure
2263 *
2264 * This routine is a task that is schedule by the interrupt handler
2265 * to perform the background processing for interrupts. We put it
2266 * on a task queue that is consumed whenever the scheduler runs; that's
2267 * so you can do anything (i.e. put the process to sleep etc). In fact,
2268 * the mid-level tries to sleep when it reaches the driver threshold
2269 * "host->can_queue". This can cause a panic if we were in our interrupt code.
2270 **/
David Howellsc4028952006-11-22 14:57:56 +00002271static void qla4xxx_do_dpc(struct work_struct *work)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002272{
David Howellsc4028952006-11-22 14:57:56 +00002273 struct scsi_qla_host *ha =
2274 container_of(work, struct scsi_qla_host, dpc_work);
David C Somayajulu477ffb92007-01-22 12:26:11 -08002275 int status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002276
David C Somayajuluf26b9042006-11-15 16:41:09 -08002277 DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302278 "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
2279 ha->host_no, __func__, ha->flags, ha->dpc_flags))
David Somayajuluafaf5a22006-09-19 10:28:00 -07002280
2281 /* Initialization not yet finished. Don't do anything yet. */
2282 if (!test_bit(AF_INIT_DONE, &ha->flags))
Lalit Chandivade1b468072011-05-17 23:17:09 -07002283 return;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002284
Lalit Chandivade2232be02010-07-30 14:38:47 +05302285 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
2286 DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
2287 ha->host_no, __func__, ha->flags));
Lalit Chandivade1b468072011-05-17 23:17:09 -07002288 return;
Lalit Chandivade2232be02010-07-30 14:38:47 +05302289 }
2290
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302291 if (is_qla8022(ha)) {
2292 if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
2293 qla4_8xxx_idc_lock(ha);
2294 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2295 QLA82XX_DEV_FAILED);
2296 qla4_8xxx_idc_unlock(ha);
2297 ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
2298 qla4_8xxx_device_state_handler(ha);
2299 }
2300 if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
2301 qla4_8xxx_need_qsnt_handler(ha);
2302 }
2303 }
2304
2305 if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
2306 (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07002307 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302308 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
2309 if (ql4xdontresethba) {
2310 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
2311 ha->host_no, __func__));
2312 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
2313 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
2314 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2315 goto dpc_post_reset_ha;
2316 }
2317 if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
2318 test_bit(DPC_RESET_HA, &ha->dpc_flags))
2319 qla4xxx_recover_adapter(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002320
David C Somayajulu477ffb92007-01-22 12:26:11 -08002321 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002322 uint8_t wait_time = RESET_INTR_TOV;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002323
David Somayajuluafaf5a22006-09-19 10:28:00 -07002324 while ((readw(&ha->reg->ctrl_status) &
2325 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
2326 if (--wait_time == 0)
2327 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002328 msleep(1000);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002329 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002330 if (wait_time == 0)
2331 DEBUG2(printk("scsi%ld: %s: SR|FSR "
2332 "bit not cleared-- resetting\n",
2333 ha->host_no, __func__));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302334 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
David C Somayajulu477ffb92007-01-22 12:26:11 -08002335 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
2336 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302337 status = qla4xxx_recover_adapter(ha);
David C Somayajulu477ffb92007-01-22 12:26:11 -08002338 }
2339 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
2340 if (status == QLA_SUCCESS)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302341 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002342 }
2343 }
2344
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302345dpc_post_reset_ha:
David Somayajuluafaf5a22006-09-19 10:28:00 -07002346 /* ---- process AEN? --- */
2347 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
2348 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
2349
2350 /* ---- Get DHCP IP Address? --- */
2351 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
2352 qla4xxx_get_dhcp_ip_address(ha);
2353
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05302354 /* ---- link change? --- */
2355 if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
2356 if (!test_bit(AF_LINK_UP, &ha->flags)) {
2357 /* ---- link down? --- */
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07002358 qla4xxx_mark_all_devices_missing(ha);
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05302359 } else {
2360 /* ---- link up? --- *
2361 * F/W will auto login to all devices ONLY ONCE after
2362 * link up during driver initialization and runtime
2363 * fatal error recovery. Therefore, the driver must
2364 * manually relogin to devices when recovering from
2365 * connection failures, logouts, expired KATO, etc. */
2366
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07002367 qla4xxx_relogin_all_devices(ha);
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05302368 }
2369 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002370}
2371
2372/**
2373 * qla4xxx_free_adapter - release the adapter
2374 * @ha: pointer to adapter structure
2375 **/
2376static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
2377{
2378
2379 if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
2380 /* Turn-off interrupts on the card. */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302381 ha->isp_ops->disable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002382 }
2383
David Somayajuluafaf5a22006-09-19 10:28:00 -07002384 /* Remove timer thread, if present */
2385 if (ha->timer_active)
2386 qla4xxx_stop_timer(ha);
2387
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302388 /* Kill the kernel thread for this host */
2389 if (ha->dpc_thread)
2390 destroy_workqueue(ha->dpc_thread);
2391
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002392 /* Kill the kernel thread for this host */
2393 if (ha->task_wq)
2394 destroy_workqueue(ha->task_wq);
2395
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302396 /* Put firmware in known state */
2397 ha->isp_ops->reset_firmware(ha);
2398
2399 if (is_qla8022(ha)) {
2400 qla4_8xxx_idc_lock(ha);
2401 qla4_8xxx_clear_drv_active(ha);
2402 qla4_8xxx_idc_unlock(ha);
2403 }
2404
David Somayajuluafaf5a22006-09-19 10:28:00 -07002405 /* Detach interrupts */
2406 if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302407 qla4xxx_free_irqs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002408
David C Somayajulubee4fe82007-05-23 18:03:32 -07002409 /* free extra memory */
2410 qla4xxx_mem_free(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302411}
David C Somayajulubee4fe82007-05-23 18:03:32 -07002412
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302413int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
2414{
2415 int status = 0;
2416 uint8_t revision_id;
2417 unsigned long mem_base, mem_len, db_base, db_len;
2418 struct pci_dev *pdev = ha->pdev;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002419
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302420 status = pci_request_regions(pdev, DRIVER_NAME);
2421 if (status) {
2422 printk(KERN_WARNING
2423 "scsi(%ld) Failed to reserve PIO regions (%s) "
2424 "status=%d\n", ha->host_no, pci_name(pdev), status);
2425 goto iospace_error_exit;
2426 }
2427
2428 pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
2429 DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
2430 __func__, revision_id));
2431 ha->revision_id = revision_id;
2432
2433 /* remap phys address */
2434 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
2435 mem_len = pci_resource_len(pdev, 0);
2436 DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
2437 __func__, mem_base, mem_len));
2438
2439 /* mapping of pcibase pointer */
2440 ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
2441 if (!ha->nx_pcibase) {
2442 printk(KERN_ERR
2443 "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
2444 pci_release_regions(ha->pdev);
2445 goto iospace_error_exit;
2446 }
2447
2448 /* Mapping of IO base pointer, door bell read and write pointer */
2449
2450 /* mapping of IO base pointer */
2451 ha->qla4_8xxx_reg =
2452 (struct device_reg_82xx __iomem *)((uint8_t *)ha->nx_pcibase +
2453 0xbc000 + (ha->pdev->devfn << 11));
2454
2455 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
2456 db_len = pci_resource_len(pdev, 4);
2457
Shyam Sundar2657c802010-10-06 22:50:29 -07002458 ha->nx_db_wr_ptr = (ha->pdev->devfn == 4 ? QLA82XX_CAM_RAM_DB1 :
2459 QLA82XX_CAM_RAM_DB2);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302460
Shyam Sundar2657c802010-10-06 22:50:29 -07002461 return 0;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302462iospace_error_exit:
2463 return -ENOMEM;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002464}
2465
2466/***
2467 * qla4xxx_iospace_config - maps registers
2468 * @ha: pointer to adapter structure
2469 *
2470 * This routines maps HBA's registers from the pci address space
2471 * into the kernel virtual address space for memory mapped i/o.
2472 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302473int qla4xxx_iospace_config(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002474{
2475 unsigned long pio, pio_len, pio_flags;
2476 unsigned long mmio, mmio_len, mmio_flags;
2477
2478 pio = pci_resource_start(ha->pdev, 0);
2479 pio_len = pci_resource_len(ha->pdev, 0);
2480 pio_flags = pci_resource_flags(ha->pdev, 0);
2481 if (pio_flags & IORESOURCE_IO) {
2482 if (pio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302483 ql4_printk(KERN_WARNING, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002484 "Invalid PCI I/O region size\n");
2485 pio = 0;
2486 }
2487 } else {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302488 ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002489 pio = 0;
2490 }
2491
2492 /* Use MMIO operations for all accesses. */
2493 mmio = pci_resource_start(ha->pdev, 1);
2494 mmio_len = pci_resource_len(ha->pdev, 1);
2495 mmio_flags = pci_resource_flags(ha->pdev, 1);
2496
2497 if (!(mmio_flags & IORESOURCE_MEM)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302498 ql4_printk(KERN_ERR, ha,
2499 "region #0 not an MMIO resource, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002500
2501 goto iospace_error_exit;
2502 }
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302503
David Somayajuluafaf5a22006-09-19 10:28:00 -07002504 if (mmio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302505 ql4_printk(KERN_ERR, ha,
2506 "Invalid PCI mem region size, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002507 goto iospace_error_exit;
2508 }
2509
2510 if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302511 ql4_printk(KERN_WARNING, ha,
2512 "Failed to reserve PIO/MMIO regions\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002513
2514 goto iospace_error_exit;
2515 }
2516
2517 ha->pio_address = pio;
2518 ha->pio_length = pio_len;
2519 ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
2520 if (!ha->reg) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302521 ql4_printk(KERN_ERR, ha,
2522 "cannot remap MMIO, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002523
2524 goto iospace_error_exit;
2525 }
2526
2527 return 0;
2528
2529iospace_error_exit:
2530 return -ENOMEM;
2531}
2532
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302533static struct isp_operations qla4xxx_isp_ops = {
2534 .iospace_config = qla4xxx_iospace_config,
2535 .pci_config = qla4xxx_pci_config,
2536 .disable_intrs = qla4xxx_disable_intrs,
2537 .enable_intrs = qla4xxx_enable_intrs,
2538 .start_firmware = qla4xxx_start_firmware,
2539 .intr_handler = qla4xxx_intr_handler,
2540 .interrupt_service_routine = qla4xxx_interrupt_service_routine,
2541 .reset_chip = qla4xxx_soft_reset,
2542 .reset_firmware = qla4xxx_hw_reset,
2543 .queue_iocb = qla4xxx_queue_iocb,
2544 .complete_iocb = qla4xxx_complete_iocb,
2545 .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
2546 .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
2547 .get_sys_info = qla4xxx_get_sys_info,
2548};
2549
2550static struct isp_operations qla4_8xxx_isp_ops = {
2551 .iospace_config = qla4_8xxx_iospace_config,
2552 .pci_config = qla4_8xxx_pci_config,
2553 .disable_intrs = qla4_8xxx_disable_intrs,
2554 .enable_intrs = qla4_8xxx_enable_intrs,
2555 .start_firmware = qla4_8xxx_load_risc,
2556 .intr_handler = qla4_8xxx_intr_handler,
2557 .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
2558 .reset_chip = qla4_8xxx_isp_reset,
2559 .reset_firmware = qla4_8xxx_stop_firmware,
2560 .queue_iocb = qla4_8xxx_queue_iocb,
2561 .complete_iocb = qla4_8xxx_complete_iocb,
2562 .rd_shdw_req_q_out = qla4_8xxx_rd_shdw_req_q_out,
2563 .rd_shdw_rsp_q_in = qla4_8xxx_rd_shdw_rsp_q_in,
2564 .get_sys_info = qla4_8xxx_get_sys_info,
2565};
2566
2567uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
2568{
2569 return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
2570}
2571
2572uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
2573{
2574 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
2575}
2576
2577uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
2578{
2579 return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
2580}
2581
2582uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
2583{
2584 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
2585}
2586
Manish Rangankar2a991c22011-07-25 13:48:55 -05002587static ssize_t qla4xxx_show_boot_eth_info(void *data, int type, char *buf)
2588{
2589 struct scsi_qla_host *ha = data;
2590 char *str = buf;
2591 int rc;
2592
2593 switch (type) {
2594 case ISCSI_BOOT_ETH_FLAGS:
2595 rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
2596 break;
2597 case ISCSI_BOOT_ETH_INDEX:
2598 rc = sprintf(str, "0\n");
2599 break;
2600 case ISCSI_BOOT_ETH_MAC:
2601 rc = sysfs_format_mac(str, ha->my_mac,
2602 MAC_ADDR_LEN);
2603 break;
2604 default:
2605 rc = -ENOSYS;
2606 break;
2607 }
2608 return rc;
2609}
2610
2611static mode_t qla4xxx_eth_get_attr_visibility(void *data, int type)
2612{
2613 int rc;
2614
2615 switch (type) {
2616 case ISCSI_BOOT_ETH_FLAGS:
2617 case ISCSI_BOOT_ETH_MAC:
2618 case ISCSI_BOOT_ETH_INDEX:
2619 rc = S_IRUGO;
2620 break;
2621 default:
2622 rc = 0;
2623 break;
2624 }
2625 return rc;
2626}
2627
2628static ssize_t qla4xxx_show_boot_ini_info(void *data, int type, char *buf)
2629{
2630 struct scsi_qla_host *ha = data;
2631 char *str = buf;
2632 int rc;
2633
2634 switch (type) {
2635 case ISCSI_BOOT_INI_INITIATOR_NAME:
2636 rc = sprintf(str, "%s\n", ha->name_string);
2637 break;
2638 default:
2639 rc = -ENOSYS;
2640 break;
2641 }
2642 return rc;
2643}
2644
2645static mode_t qla4xxx_ini_get_attr_visibility(void *data, int type)
2646{
2647 int rc;
2648
2649 switch (type) {
2650 case ISCSI_BOOT_INI_INITIATOR_NAME:
2651 rc = S_IRUGO;
2652 break;
2653 default:
2654 rc = 0;
2655 break;
2656 }
2657 return rc;
2658}
2659
2660static ssize_t
2661qla4xxx_show_boot_tgt_info(struct ql4_boot_session_info *boot_sess, int type,
2662 char *buf)
2663{
2664 struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
2665 char *str = buf;
2666 int rc;
2667
2668 switch (type) {
2669 case ISCSI_BOOT_TGT_NAME:
2670 rc = sprintf(buf, "%s\n", (char *)&boot_sess->target_name);
2671 break;
2672 case ISCSI_BOOT_TGT_IP_ADDR:
2673 if (boot_sess->conn_list[0].dest_ipaddr.ip_type == 0x1)
2674 rc = sprintf(buf, "%pI4\n",
2675 &boot_conn->dest_ipaddr.ip_address);
2676 else
2677 rc = sprintf(str, "%pI6\n",
2678 &boot_conn->dest_ipaddr.ip_address);
2679 break;
2680 case ISCSI_BOOT_TGT_PORT:
2681 rc = sprintf(str, "%d\n", boot_conn->dest_port);
2682 break;
2683 case ISCSI_BOOT_TGT_CHAP_NAME:
2684 rc = sprintf(str, "%.*s\n",
2685 boot_conn->chap.target_chap_name_length,
2686 (char *)&boot_conn->chap.target_chap_name);
2687 break;
2688 case ISCSI_BOOT_TGT_CHAP_SECRET:
2689 rc = sprintf(str, "%.*s\n",
2690 boot_conn->chap.target_secret_length,
2691 (char *)&boot_conn->chap.target_secret);
2692 break;
2693 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
2694 rc = sprintf(str, "%.*s\n",
2695 boot_conn->chap.intr_chap_name_length,
2696 (char *)&boot_conn->chap.intr_chap_name);
2697 break;
2698 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
2699 rc = sprintf(str, "%.*s\n",
2700 boot_conn->chap.intr_secret_length,
2701 (char *)&boot_conn->chap.intr_secret);
2702 break;
2703 case ISCSI_BOOT_TGT_FLAGS:
2704 rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
2705 break;
2706 case ISCSI_BOOT_TGT_NIC_ASSOC:
2707 rc = sprintf(str, "0\n");
2708 break;
2709 default:
2710 rc = -ENOSYS;
2711 break;
2712 }
2713 return rc;
2714}
2715
2716static ssize_t qla4xxx_show_boot_tgt_pri_info(void *data, int type, char *buf)
2717{
2718 struct scsi_qla_host *ha = data;
2719 struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_pri_sess);
2720
2721 return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
2722}
2723
2724static ssize_t qla4xxx_show_boot_tgt_sec_info(void *data, int type, char *buf)
2725{
2726 struct scsi_qla_host *ha = data;
2727 struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_sec_sess);
2728
2729 return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
2730}
2731
2732static mode_t qla4xxx_tgt_get_attr_visibility(void *data, int type)
2733{
2734 int rc;
2735
2736 switch (type) {
2737 case ISCSI_BOOT_TGT_NAME:
2738 case ISCSI_BOOT_TGT_IP_ADDR:
2739 case ISCSI_BOOT_TGT_PORT:
2740 case ISCSI_BOOT_TGT_CHAP_NAME:
2741 case ISCSI_BOOT_TGT_CHAP_SECRET:
2742 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
2743 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
2744 case ISCSI_BOOT_TGT_NIC_ASSOC:
2745 case ISCSI_BOOT_TGT_FLAGS:
2746 rc = S_IRUGO;
2747 break;
2748 default:
2749 rc = 0;
2750 break;
2751 }
2752 return rc;
2753}
2754
2755static void qla4xxx_boot_release(void *data)
2756{
2757 struct scsi_qla_host *ha = data;
2758
2759 scsi_host_put(ha->host);
2760}
2761
2762static int get_fw_boot_info(struct scsi_qla_host *ha, uint16_t ddb_index[])
2763{
2764 dma_addr_t buf_dma;
2765 uint32_t addr, pri_addr, sec_addr;
2766 uint32_t offset;
2767 uint16_t func_num;
2768 uint8_t val;
2769 uint8_t *buf = NULL;
2770 size_t size = 13 * sizeof(uint8_t);
2771 int ret = QLA_SUCCESS;
2772
2773 func_num = PCI_FUNC(ha->pdev->devfn);
2774
2775 DEBUG2(ql4_printk(KERN_INFO, ha,
2776 "%s: Get FW boot info for 0x%x func %d\n", __func__,
2777 (is_qla4032(ha) ? PCI_DEVICE_ID_QLOGIC_ISP4032 :
2778 PCI_DEVICE_ID_QLOGIC_ISP8022), func_num));
2779
2780 if (is_qla4032(ha)) {
2781 if (func_num == 1) {
2782 addr = NVRAM_PORT0_BOOT_MODE;
2783 pri_addr = NVRAM_PORT0_BOOT_PRI_TGT;
2784 sec_addr = NVRAM_PORT0_BOOT_SEC_TGT;
2785 } else if (func_num == 3) {
2786 addr = NVRAM_PORT1_BOOT_MODE;
2787 pri_addr = NVRAM_PORT1_BOOT_PRI_TGT;
2788 sec_addr = NVRAM_PORT1_BOOT_SEC_TGT;
2789 } else {
2790 ret = QLA_ERROR;
2791 goto exit_boot_info;
2792 }
2793
2794 /* Check Boot Mode */
2795 val = rd_nvram_byte(ha, addr);
2796 if (!(val & 0x07)) {
2797 DEBUG2(ql4_printk(KERN_ERR, ha,
2798 "%s: Failed Boot options : 0x%x\n",
2799 __func__, val));
2800 ret = QLA_ERROR;
2801 goto exit_boot_info;
2802 }
2803
2804 /* get primary valid target index */
2805 val = rd_nvram_byte(ha, pri_addr);
2806 if (val & BIT_7)
2807 ddb_index[0] = (val & 0x7f);
2808 else
2809 ddb_index[0] = 0;
2810
2811 /* get secondary valid target index */
2812 val = rd_nvram_byte(ha, sec_addr);
2813 if (val & BIT_7)
2814 ddb_index[1] = (val & 0x7f);
2815 else
2816 ddb_index[1] = 1;
2817
2818 } else if (is_qla8022(ha)) {
2819 buf = dma_alloc_coherent(&ha->pdev->dev, size,
2820 &buf_dma, GFP_KERNEL);
2821 if (!buf) {
2822 DEBUG2(ql4_printk(KERN_ERR, ha,
2823 "%s: Unable to allocate dma buffer\n",
2824 __func__));
2825 ret = QLA_ERROR;
2826 goto exit_boot_info;
2827 }
2828
2829 if (ha->port_num == 0)
2830 offset = BOOT_PARAM_OFFSET_PORT0;
2831 else if (ha->port_num == 1)
2832 offset = BOOT_PARAM_OFFSET_PORT1;
2833 else {
2834 ret = QLA_ERROR;
2835 goto exit_boot_info_free;
2836 }
2837 addr = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_iscsi_param * 4) +
2838 offset;
2839 if (qla4xxx_get_flash(ha, buf_dma, addr,
2840 13 * sizeof(uint8_t)) != QLA_SUCCESS) {
2841 DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: Get Flash"
2842 "failed\n", ha->host_no, __func__));
2843 ret = QLA_ERROR;
2844 goto exit_boot_info_free;
2845 }
2846 /* Check Boot Mode */
2847 if (!(buf[1] & 0x07)) {
2848 DEBUG2(ql4_printk(KERN_INFO, ha,
2849 "Failed: Boot options : 0x%x\n",
2850 buf[1]));
2851 ret = QLA_ERROR;
2852 goto exit_boot_info_free;
2853 }
2854
2855 /* get primary valid target index */
2856 if (buf[2] & BIT_7)
2857 ddb_index[0] = buf[2] & 0x7f;
2858 else
2859 ddb_index[0] = 0;
2860
2861 /* get secondary valid target index */
2862 if (buf[11] & BIT_7)
2863 ddb_index[1] = buf[11] & 0x7f;
2864 else
2865 ddb_index[1] = 1;
2866
2867 } else {
2868 ret = QLA_ERROR;
2869 goto exit_boot_info;
2870 }
2871
2872 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Primary target ID %d, Secondary"
2873 " target ID %d\n", __func__, ddb_index[0],
2874 ddb_index[1]));
2875
2876exit_boot_info_free:
2877 dma_free_coherent(&ha->pdev->dev, size, buf, buf_dma);
2878exit_boot_info:
2879 return ret;
2880}
2881
2882static int qla4xxx_get_boot_target(struct scsi_qla_host *ha,
2883 struct ql4_boot_session_info *boot_sess,
2884 uint16_t ddb_index)
2885{
2886 struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
2887 struct dev_db_entry *fw_ddb_entry;
2888 dma_addr_t fw_ddb_entry_dma;
2889 uint16_t idx;
2890 uint16_t options;
2891 int ret = QLA_SUCCESS;
2892
2893 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
2894 &fw_ddb_entry_dma, GFP_KERNEL);
2895 if (!fw_ddb_entry) {
2896 DEBUG2(ql4_printk(KERN_ERR, ha,
2897 "%s: Unable to allocate dma buffer.\n",
2898 __func__));
2899 ret = QLA_ERROR;
2900 return ret;
2901 }
2902
2903 if (qla4xxx_bootdb_by_index(ha, fw_ddb_entry,
2904 fw_ddb_entry_dma, ddb_index)) {
2905 DEBUG2(ql4_printk(KERN_ERR, ha,
2906 "%s: Flash DDB read Failed\n", __func__));
2907 ret = QLA_ERROR;
2908 goto exit_boot_target;
2909 }
2910
2911 /* Update target name and IP from DDB */
2912 memcpy(boot_sess->target_name, fw_ddb_entry->iscsi_name,
2913 min(sizeof(boot_sess->target_name),
2914 sizeof(fw_ddb_entry->iscsi_name)));
2915
2916 options = le16_to_cpu(fw_ddb_entry->options);
2917 if (options & DDB_OPT_IPV6_DEVICE) {
2918 memcpy(&boot_conn->dest_ipaddr.ip_address,
2919 &fw_ddb_entry->ip_addr[0], IPv6_ADDR_LEN);
2920 } else {
2921 boot_conn->dest_ipaddr.ip_type = 0x1;
2922 memcpy(&boot_conn->dest_ipaddr.ip_address,
2923 &fw_ddb_entry->ip_addr[0], IP_ADDR_LEN);
2924 }
2925
2926 boot_conn->dest_port = le16_to_cpu(fw_ddb_entry->port);
2927
2928 /* update chap information */
2929 idx = __le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
2930
2931 if (BIT_7 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
2932
2933 DEBUG2(ql4_printk(KERN_INFO, ha, "Setting chap\n"));
2934
2935 ret = qla4xxx_get_chap(ha, (char *)&boot_conn->chap.
2936 target_chap_name,
2937 (char *)&boot_conn->chap.target_secret,
2938 idx);
2939 if (ret) {
2940 ql4_printk(KERN_ERR, ha, "Failed to set chap\n");
2941 ret = QLA_ERROR;
2942 goto exit_boot_target;
2943 }
2944
2945 boot_conn->chap.target_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
2946 boot_conn->chap.target_secret_length = QL4_CHAP_MAX_SECRET_LEN;
2947 }
2948
2949 if (BIT_4 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
2950
2951 DEBUG2(ql4_printk(KERN_INFO, ha, "Setting BIDI chap\n"));
2952
2953 ret = qla4xxx_get_chap(ha, (char *)&boot_conn->chap.
2954 intr_chap_name,
2955 (char *)&boot_conn->chap.intr_secret,
2956 (idx + 1));
2957 if (ret) {
2958 ql4_printk(KERN_ERR, ha, "Failed to set BIDI chap\n");
2959 ret = QLA_ERROR;
2960 goto exit_boot_target;
2961 }
2962
2963 boot_conn->chap.intr_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
2964 boot_conn->chap.intr_secret_length = QL4_CHAP_MAX_SECRET_LEN;
2965 }
2966
2967exit_boot_target:
2968 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
2969 fw_ddb_entry, fw_ddb_entry_dma);
2970 return ret;
2971}
2972
2973static int qla4xxx_get_boot_info(struct scsi_qla_host *ha)
2974{
2975 uint16_t ddb_index[2];
2976 int ret = QLA_SUCCESS;
2977
2978 memset(ddb_index, 0, sizeof(ddb_index));
2979 ret = get_fw_boot_info(ha, ddb_index);
2980 if (ret != QLA_SUCCESS) {
2981 DEBUG2(ql4_printk(KERN_ERR, ha,
2982 "%s: Failed to set boot info.\n", __func__));
2983 return ret;
2984 }
2985
2986 ret = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_pri_sess),
2987 ddb_index[0]);
2988 if (ret != QLA_SUCCESS) {
2989 DEBUG2(ql4_printk(KERN_ERR, ha, "%s: Failed to get "
2990 "primary target\n", __func__));
2991 }
2992
2993 ret = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_sec_sess),
2994 ddb_index[1]);
2995 if (ret != QLA_SUCCESS) {
2996 DEBUG2(ql4_printk(KERN_ERR, ha, "%s: Failed to get "
2997 "secondary target\n", __func__));
2998 }
2999 return ret;
3000}
3001
3002static int qla4xxx_setup_boot_info(struct scsi_qla_host *ha)
3003{
3004 struct iscsi_boot_kobj *boot_kobj;
3005
3006 if (qla4xxx_get_boot_info(ha) != QLA_SUCCESS)
3007 return 0;
3008
3009 ha->boot_kset = iscsi_boot_create_host_kset(ha->host->host_no);
3010 if (!ha->boot_kset)
3011 goto kset_free;
3012
3013 if (!scsi_host_get(ha->host))
3014 goto kset_free;
3015 boot_kobj = iscsi_boot_create_target(ha->boot_kset, 0, ha,
3016 qla4xxx_show_boot_tgt_pri_info,
3017 qla4xxx_tgt_get_attr_visibility,
3018 qla4xxx_boot_release);
3019 if (!boot_kobj)
3020 goto put_host;
3021
3022 if (!scsi_host_get(ha->host))
3023 goto kset_free;
3024 boot_kobj = iscsi_boot_create_target(ha->boot_kset, 1, ha,
3025 qla4xxx_show_boot_tgt_sec_info,
3026 qla4xxx_tgt_get_attr_visibility,
3027 qla4xxx_boot_release);
3028 if (!boot_kobj)
3029 goto put_host;
3030
3031 if (!scsi_host_get(ha->host))
3032 goto kset_free;
3033 boot_kobj = iscsi_boot_create_initiator(ha->boot_kset, 0, ha,
3034 qla4xxx_show_boot_ini_info,
3035 qla4xxx_ini_get_attr_visibility,
3036 qla4xxx_boot_release);
3037 if (!boot_kobj)
3038 goto put_host;
3039
3040 if (!scsi_host_get(ha->host))
3041 goto kset_free;
3042 boot_kobj = iscsi_boot_create_ethernet(ha->boot_kset, 0, ha,
3043 qla4xxx_show_boot_eth_info,
3044 qla4xxx_eth_get_attr_visibility,
3045 qla4xxx_boot_release);
3046 if (!boot_kobj)
3047 goto put_host;
3048
3049 return 0;
3050
3051put_host:
3052 scsi_host_put(ha->host);
3053kset_free:
3054 iscsi_boot_destroy_kset(ha->boot_kset);
3055 return -ENOMEM;
3056}
3057
David Somayajuluafaf5a22006-09-19 10:28:00 -07003058/**
3059 * qla4xxx_probe_adapter - callback function to probe HBA
3060 * @pdev: pointer to pci_dev structure
3061 * @pci_device_id: pointer to pci_device entry
3062 *
3063 * This routine will probe for Qlogic 4xxx iSCSI host adapters.
3064 * It returns zero if successful. It also initializes all data necessary for
3065 * the driver.
3066 **/
3067static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
3068 const struct pci_device_id *ent)
3069{
3070 int ret = -ENODEV, status;
3071 struct Scsi_Host *host;
3072 struct scsi_qla_host *ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003073 uint8_t init_retry_count = 0;
3074 char buf[34];
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303075 struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
Prasanna Mumbaif9880e72011-03-21 03:34:26 -07003076 uint32_t dev_state;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003077
3078 if (pci_enable_device(pdev))
3079 return -1;
3080
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003081 host = iscsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha), 0);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003082 if (host == NULL) {
3083 printk(KERN_WARNING
3084 "qla4xxx: Couldn't allocate host from scsi layer!\n");
3085 goto probe_disable_device;
3086 }
3087
3088 /* Clear our data area */
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003089 ha = to_qla_host(host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003090 memset(ha, 0, sizeof(*ha));
3091
3092 /* Save the information from PCI BIOS. */
3093 ha->pdev = pdev;
3094 ha->host = host;
3095 ha->host_no = host->host_no;
3096
Lalit Chandivade2232be02010-07-30 14:38:47 +05303097 pci_enable_pcie_error_reporting(pdev);
3098
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303099 /* Setup Runtime configurable options */
3100 if (is_qla8022(ha)) {
3101 ha->isp_ops = &qla4_8xxx_isp_ops;
3102 rwlock_init(&ha->hw_lock);
3103 ha->qdr_sn_window = -1;
3104 ha->ddr_mn_window = -1;
3105 ha->curr_window = 255;
3106 ha->func_num = PCI_FUNC(ha->pdev->devfn);
3107 nx_legacy_intr = &legacy_intr[ha->func_num];
3108 ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
3109 ha->nx_legacy_intr.tgt_status_reg =
3110 nx_legacy_intr->tgt_status_reg;
3111 ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
3112 ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
3113 } else {
3114 ha->isp_ops = &qla4xxx_isp_ops;
3115 }
3116
Lalit Chandivade2232be02010-07-30 14:38:47 +05303117 /* Set EEH reset type to fundamental if required by hba */
3118 if (is_qla8022(ha))
3119 pdev->needs_freset = 1;
3120
David Somayajuluafaf5a22006-09-19 10:28:00 -07003121 /* Configure PCI I/O space. */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303122 ret = ha->isp_ops->iospace_config(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003123 if (ret)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303124 goto probe_failed_ioconfig;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003125
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303126 ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
David Somayajuluafaf5a22006-09-19 10:28:00 -07003127 pdev->device, pdev->irq, ha->reg);
3128
3129 qla4xxx_config_dma_addressing(ha);
3130
3131 /* Initialize lists and spinlocks. */
David Somayajuluafaf5a22006-09-19 10:28:00 -07003132 INIT_LIST_HEAD(&ha->free_srb_q);
3133
3134 mutex_init(&ha->mbox_sem);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303135 init_completion(&ha->mbx_intr_comp);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003136
3137 spin_lock_init(&ha->hardware_lock);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003138
3139 /* Allocate dma buffers */
3140 if (qla4xxx_mem_alloc(ha)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303141 ql4_printk(KERN_WARNING, ha,
3142 "[ERROR] Failed to allocate memory for adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07003143
3144 ret = -ENOMEM;
3145 goto probe_failed;
3146 }
3147
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003148 host->cmd_per_lun = 3;
3149 host->max_channel = 0;
3150 host->max_lun = MAX_LUNS - 1;
3151 host->max_id = MAX_TARGETS;
3152 host->max_cmd_len = IOCB_MAX_CDB_LEN;
3153 host->can_queue = MAX_SRBS ;
3154 host->transportt = qla4xxx_scsi_transport;
3155
3156 ret = scsi_init_shared_tag_map(host, MAX_SRBS);
3157 if (ret) {
3158 ql4_printk(KERN_WARNING, ha,
3159 "%s: scsi_init_shared_tag_map failed\n", __func__);
3160 goto probe_failed;
3161 }
3162
3163 pci_set_drvdata(pdev, ha);
3164
3165 ret = scsi_add_host(host, &pdev->dev);
3166 if (ret)
3167 goto probe_failed;
3168
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303169 if (is_qla8022(ha))
3170 (void) qla4_8xxx_get_flash_info(ha);
3171
David Somayajuluafaf5a22006-09-19 10:28:00 -07003172 /*
3173 * Initialize the Host adapter request/response queues and
3174 * firmware
3175 * NOTE: interrupts enabled upon successful completion
3176 */
Manish Rangankar0e7e8502011-07-25 13:48:54 -05003177 status = qla4xxx_initialize_adapter(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303178 while ((!test_bit(AF_ONLINE, &ha->flags)) &&
3179 init_retry_count++ < MAX_INIT_RETRIES) {
Prasanna Mumbaif9880e72011-03-21 03:34:26 -07003180
3181 if (is_qla8022(ha)) {
3182 qla4_8xxx_idc_lock(ha);
3183 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
3184 qla4_8xxx_idc_unlock(ha);
3185 if (dev_state == QLA82XX_DEV_FAILED) {
3186 ql4_printk(KERN_WARNING, ha, "%s: don't retry "
3187 "initialize adapter. H/W is in failed state\n",
3188 __func__);
3189 break;
3190 }
3191 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07003192 DEBUG2(printk("scsi: %s: retrying adapter initialization "
3193 "(%d)\n", __func__, init_retry_count));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303194
3195 if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
3196 continue;
3197
Manish Rangankar0e7e8502011-07-25 13:48:54 -05003198 status = qla4xxx_initialize_adapter(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003199 }
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303200
3201 if (!test_bit(AF_ONLINE, &ha->flags)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303202 ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07003203
Lalit Chandivadefe998522010-12-02 22:12:36 -08003204 if (is_qla8022(ha) && ql4xdontresethba) {
3205 /* Put the device in failed state. */
3206 DEBUG2(printk(KERN_ERR "HW STATE: FAILED\n"));
3207 qla4_8xxx_idc_lock(ha);
3208 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
3209 QLA82XX_DEV_FAILED);
3210 qla4_8xxx_idc_unlock(ha);
3211 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07003212 ret = -ENODEV;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003213 goto remove_host;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003214 }
3215
David Somayajuluafaf5a22006-09-19 10:28:00 -07003216 /* Startup the kernel thread for this host adapter. */
3217 DEBUG2(printk("scsi: %s: Starting kernel thread for "
3218 "qla4xxx_dpc\n", __func__));
3219 sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
3220 ha->dpc_thread = create_singlethread_workqueue(buf);
3221 if (!ha->dpc_thread) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303222 ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07003223 ret = -ENODEV;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003224 goto remove_host;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003225 }
David Howellsc4028952006-11-22 14:57:56 +00003226 INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003227
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003228 sprintf(buf, "qla4xxx_%lu_task", ha->host_no);
3229 ha->task_wq = alloc_workqueue(buf, WQ_MEM_RECLAIM, 1);
3230 if (!ha->task_wq) {
3231 ql4_printk(KERN_WARNING, ha, "Unable to start task thread!\n");
3232 ret = -ENODEV;
3233 goto remove_host;
3234 }
3235
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303236 /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
3237 * (which is called indirectly by qla4xxx_initialize_adapter),
3238 * so that irqs will be registered after crbinit but before
3239 * mbx_intr_enable.
3240 */
3241 if (!is_qla8022(ha)) {
3242 ret = qla4xxx_request_irqs(ha);
3243 if (ret) {
3244 ql4_printk(KERN_WARNING, ha, "Failed to reserve "
3245 "interrupt %d already in use.\n", pdev->irq);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003246 goto remove_host;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303247 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07003248 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07003249
Lalit Chandivade2232be02010-07-30 14:38:47 +05303250 pci_save_state(ha->pdev);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303251 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003252
3253 /* Start timer thread. */
3254 qla4xxx_start_timer(ha, qla4xxx_timer, 1);
3255
3256 set_bit(AF_INIT_DONE, &ha->flags);
3257
David Somayajuluafaf5a22006-09-19 10:28:00 -07003258 printk(KERN_INFO
3259 " QLogic iSCSI HBA Driver version: %s\n"
3260 " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
3261 qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
3262 ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
3263 ha->patch_number, ha->build_number);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05003264
Manish Rangankar2a991c22011-07-25 13:48:55 -05003265 if (qla4xxx_setup_boot_info(ha))
3266 ql4_printk(KERN_ERR, ha, "%s:ISCSI boot info setup failed\n",
3267 __func__);
3268
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05003269 qla4xxx_create_ifaces(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003270 return 0;
3271
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003272remove_host:
3273 scsi_remove_host(ha->host);
3274
David Somayajuluafaf5a22006-09-19 10:28:00 -07003275probe_failed:
3276 qla4xxx_free_adapter(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303277
3278probe_failed_ioconfig:
Lalit Chandivade2232be02010-07-30 14:38:47 +05303279 pci_disable_pcie_error_reporting(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003280 scsi_host_put(ha->host);
3281
3282probe_disable_device:
3283 pci_disable_device(pdev);
3284
3285 return ret;
3286}
3287
3288/**
Karen Higgins7eece5a2011-03-21 03:34:29 -07003289 * qla4xxx_prevent_other_port_reinit - prevent other port from re-initialize
3290 * @ha: pointer to adapter structure
3291 *
3292 * Mark the other ISP-4xxx port to indicate that the driver is being removed,
3293 * so that the other port will not re-initialize while in the process of
3294 * removing the ha due to driver unload or hba hotplug.
3295 **/
3296static void qla4xxx_prevent_other_port_reinit(struct scsi_qla_host *ha)
3297{
3298 struct scsi_qla_host *other_ha = NULL;
3299 struct pci_dev *other_pdev = NULL;
3300 int fn = ISP4XXX_PCI_FN_2;
3301
3302 /*iscsi function numbers for ISP4xxx is 1 and 3*/
3303 if (PCI_FUNC(ha->pdev->devfn) & BIT_1)
3304 fn = ISP4XXX_PCI_FN_1;
3305
3306 other_pdev =
3307 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
3308 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
3309 fn));
3310
3311 /* Get other_ha if other_pdev is valid and state is enable*/
3312 if (other_pdev) {
3313 if (atomic_read(&other_pdev->enable_cnt)) {
3314 other_ha = pci_get_drvdata(other_pdev);
3315 if (other_ha) {
3316 set_bit(AF_HA_REMOVAL, &other_ha->flags);
3317 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: "
3318 "Prevent %s reinit\n", __func__,
3319 dev_name(&other_ha->pdev->dev)));
3320 }
3321 }
3322 pci_dev_put(other_pdev);
3323 }
3324}
3325
3326/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07003327 * qla4xxx_remove_adapter - calback function to remove adapter.
3328 * @pci_dev: PCI device pointer
3329 **/
3330static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
3331{
3332 struct scsi_qla_host *ha;
3333
3334 ha = pci_get_drvdata(pdev);
3335
Karen Higgins7eece5a2011-03-21 03:34:29 -07003336 if (!is_qla8022(ha))
3337 qla4xxx_prevent_other_port_reinit(ha);
David C Somayajulubee4fe82007-05-23 18:03:32 -07003338
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05003339 /* destroy iface from sysfs */
3340 qla4xxx_destroy_ifaces(ha);
3341
Manish Rangankar2a991c22011-07-25 13:48:55 -05003342 if (ha->boot_kset)
3343 iscsi_boot_destroy_kset(ha->boot_kset);
3344
David Somayajuluafaf5a22006-09-19 10:28:00 -07003345 scsi_remove_host(ha->host);
3346
3347 qla4xxx_free_adapter(ha);
3348
3349 scsi_host_put(ha->host);
3350
Lalit Chandivade2232be02010-07-30 14:38:47 +05303351 pci_disable_pcie_error_reporting(pdev);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303352 pci_disable_device(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003353 pci_set_drvdata(pdev, NULL);
3354}
3355
3356/**
3357 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
3358 * @ha: HA context
3359 *
3360 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
3361 * supported addressing method.
3362 */
Adrian Bunk47975472007-04-26 00:35:16 -07003363static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07003364{
3365 int retval;
3366
3367 /* Update our PCI device dma_mask for full 64 bit mask */
Yang Hongyang6a355282009-04-06 19:01:13 -07003368 if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
3369 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07003370 dev_dbg(&ha->pdev->dev,
3371 "Failed to set 64 bit PCI consistent mask; "
3372 "using 32 bit.\n");
3373 retval = pci_set_consistent_dma_mask(ha->pdev,
Yang Hongyang284901a2009-04-06 19:01:15 -07003374 DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07003375 }
3376 } else
Yang Hongyang284901a2009-04-06 19:01:15 -07003377 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07003378}
3379
3380static int qla4xxx_slave_alloc(struct scsi_device *sdev)
3381{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003382 struct iscsi_cls_session *cls_sess;
3383 struct iscsi_session *sess;
3384 struct ddb_entry *ddb;
Vikas Chaudhary8bb40332011-03-21 03:34:31 -07003385 int queue_depth = QL4_DEF_QDEPTH;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003386
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003387 cls_sess = starget_to_session(sdev->sdev_target);
3388 sess = cls_sess->dd_data;
3389 ddb = sess->dd_data;
3390
David Somayajuluafaf5a22006-09-19 10:28:00 -07003391 sdev->hostdata = ddb;
3392 sdev->tagged_supported = 1;
Vikas Chaudhary8bb40332011-03-21 03:34:31 -07003393
3394 if (ql4xmaxqdepth != 0 && ql4xmaxqdepth <= 0xffffU)
3395 queue_depth = ql4xmaxqdepth;
3396
3397 scsi_activate_tcq(sdev, queue_depth);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003398 return 0;
3399}
3400
3401static int qla4xxx_slave_configure(struct scsi_device *sdev)
3402{
3403 sdev->tagged_supported = 1;
3404 return 0;
3405}
3406
3407static void qla4xxx_slave_destroy(struct scsi_device *sdev)
3408{
3409 scsi_deactivate_tcq(sdev, 1);
3410}
3411
3412/**
3413 * qla4xxx_del_from_active_array - returns an active srb
3414 * @ha: Pointer to host adapter structure.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02003415 * @index: index into the active_array
David Somayajuluafaf5a22006-09-19 10:28:00 -07003416 *
3417 * This routine removes and returns the srb at the specified index
3418 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303419struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
3420 uint32_t index)
David Somayajuluafaf5a22006-09-19 10:28:00 -07003421{
3422 struct srb *srb = NULL;
Vikas Chaudhary53698872010-04-28 11:41:59 +05303423 struct scsi_cmnd *cmd = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003424
Vikas Chaudhary53698872010-04-28 11:41:59 +05303425 cmd = scsi_host_find_tag(ha->host, index);
3426 if (!cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07003427 return srb;
3428
Vikas Chaudhary53698872010-04-28 11:41:59 +05303429 srb = (struct srb *)CMD_SP(cmd);
3430 if (!srb)
David Somayajuluafaf5a22006-09-19 10:28:00 -07003431 return srb;
3432
3433 /* update counters */
3434 if (srb->flags & SRB_DMA_VALID) {
3435 ha->req_q_count += srb->iocb_cnt;
3436 ha->iocb_cnt -= srb->iocb_cnt;
3437 if (srb->cmd)
Vikas Chaudhary53698872010-04-28 11:41:59 +05303438 srb->cmd->host_scribble =
3439 (unsigned char *)(unsigned long) MAX_SRBS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003440 }
3441 return srb;
3442}
3443
3444/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07003445 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05303446 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07003447 * @cmd: Scsi Command to wait on.
3448 *
3449 * This routine waits for the command to be returned by the Firmware
3450 * for some max time.
3451 **/
3452static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
3453 struct scsi_cmnd *cmd)
3454{
3455 int done = 0;
3456 struct srb *rp;
3457 uint32_t max_wait_time = EH_WAIT_CMD_TOV;
Lalit Chandivade2232be02010-07-30 14:38:47 +05303458 int ret = SUCCESS;
3459
3460 /* Dont wait on command if PCI error is being handled
3461 * by PCI AER driver
3462 */
3463 if (unlikely(pci_channel_offline(ha->pdev)) ||
3464 (test_bit(AF_EEH_BUSY, &ha->flags))) {
3465 ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
3466 ha->host_no, __func__);
3467 return ret;
3468 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07003469
3470 do {
3471 /* Checking to see if its returned to OS */
Vikas Chaudhary53698872010-04-28 11:41:59 +05303472 rp = (struct srb *) CMD_SP(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003473 if (rp == NULL) {
3474 done++;
3475 break;
3476 }
3477
3478 msleep(2000);
3479 } while (max_wait_time--);
3480
3481 return done;
3482}
3483
3484/**
3485 * qla4xxx_wait_for_hba_online - waits for HBA to come online
3486 * @ha: Pointer to host adapter structure
3487 **/
3488static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
3489{
3490 unsigned long wait_online;
3491
Vikas Chaudharyf581a3f2010-10-06 22:47:48 -07003492 wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003493 while (time_before(jiffies, wait_online)) {
3494
3495 if (adapter_up(ha))
3496 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003497
3498 msleep(2000);
3499 }
3500
3501 return QLA_ERROR;
3502}
3503
3504/**
Mike Christiece545032008-02-29 18:25:20 -06003505 * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02003506 * @ha: pointer to HBA
David Somayajuluafaf5a22006-09-19 10:28:00 -07003507 * @t: target id
3508 * @l: lun id
3509 *
3510 * This function waits for all outstanding commands to a lun to complete. It
3511 * returns 0 if all pending commands are returned and 1 otherwise.
3512 **/
Mike Christiece545032008-02-29 18:25:20 -06003513static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
3514 struct scsi_target *stgt,
3515 struct scsi_device *sdev)
David Somayajuluafaf5a22006-09-19 10:28:00 -07003516{
3517 int cnt;
3518 int status = 0;
3519 struct scsi_cmnd *cmd;
3520
3521 /*
Mike Christiece545032008-02-29 18:25:20 -06003522 * Waiting for all commands for the designated target or dev
3523 * in the active array
David Somayajuluafaf5a22006-09-19 10:28:00 -07003524 */
3525 for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
3526 cmd = scsi_host_find_tag(ha->host, cnt);
Mike Christiece545032008-02-29 18:25:20 -06003527 if (cmd && stgt == scsi_target(cmd->device) &&
3528 (!sdev || sdev == cmd->device)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07003529 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
3530 status++;
3531 break;
3532 }
3533 }
3534 }
3535 return status;
3536}
3537
3538/**
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05303539 * qla4xxx_eh_abort - callback for abort task.
3540 * @cmd: Pointer to Linux's SCSI command structure
3541 *
3542 * This routine is called by the Linux OS to abort the specified
3543 * command.
3544 **/
3545static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
3546{
3547 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
3548 unsigned int id = cmd->device->id;
3549 unsigned int lun = cmd->device->lun;
Mike Christie92b3e5b2010-10-06 22:51:17 -07003550 unsigned long flags;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05303551 struct srb *srb = NULL;
3552 int ret = SUCCESS;
3553 int wait = 0;
3554
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303555 ql4_printk(KERN_INFO, ha,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04003556 "scsi%ld:%d:%d: Abort command issued cmd=%p\n",
3557 ha->host_no, id, lun, cmd);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05303558
Mike Christie92b3e5b2010-10-06 22:51:17 -07003559 spin_lock_irqsave(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05303560 srb = (struct srb *) CMD_SP(cmd);
Mike Christie92b3e5b2010-10-06 22:51:17 -07003561 if (!srb) {
3562 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05303563 return SUCCESS;
Mike Christie92b3e5b2010-10-06 22:51:17 -07003564 }
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05303565 kref_get(&srb->srb_ref);
Mike Christie92b3e5b2010-10-06 22:51:17 -07003566 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05303567
3568 if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
3569 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
3570 ha->host_no, id, lun));
3571 ret = FAILED;
3572 } else {
3573 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
3574 ha->host_no, id, lun));
3575 wait = 1;
3576 }
3577
3578 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
3579
3580 /* Wait for command to complete */
3581 if (wait) {
3582 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
3583 DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
3584 ha->host_no, id, lun));
3585 ret = FAILED;
3586 }
3587 }
3588
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303589 ql4_printk(KERN_INFO, ha,
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05303590 "scsi%ld:%d:%d: Abort command - %s\n",
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003591 ha->host_no, id, lun, (ret == SUCCESS) ? "succeeded" : "failed");
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05303592
3593 return ret;
3594}
3595
3596/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07003597 * qla4xxx_eh_device_reset - callback for target reset.
3598 * @cmd: Pointer to Linux's SCSI command structure
3599 *
3600 * This routine is called by the Linux OS to reset all luns on the
3601 * specified target.
3602 **/
3603static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
3604{
3605 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
3606 struct ddb_entry *ddb_entry = cmd->device->hostdata;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003607 int ret = FAILED, stat;
3608
Karen Higgins612f73482009-07-15 15:03:01 -05003609 if (!ddb_entry)
David Somayajuluafaf5a22006-09-19 10:28:00 -07003610 return ret;
3611
Mike Christiec01be6d2010-07-22 16:59:49 +05303612 ret = iscsi_block_scsi_eh(cmd);
3613 if (ret)
3614 return ret;
3615 ret = FAILED;
3616
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303617 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07003618 "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
3619 cmd->device->channel, cmd->device->id, cmd->device->lun);
3620
3621 DEBUG2(printk(KERN_INFO
3622 "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
3623 "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
Jens Axboe242f9dc2008-09-14 05:55:09 -07003624 cmd, jiffies, cmd->request->timeout / HZ,
David Somayajuluafaf5a22006-09-19 10:28:00 -07003625 ha->dpc_flags, cmd->result, cmd->allowed));
3626
3627 /* FIXME: wait for hba to go online */
3628 stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
3629 if (stat != QLA_SUCCESS) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303630 ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003631 goto eh_dev_reset_done;
3632 }
3633
Mike Christiece545032008-02-29 18:25:20 -06003634 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
3635 cmd->device)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303636 ql4_printk(KERN_INFO, ha,
Mike Christiece545032008-02-29 18:25:20 -06003637 "DEVICE RESET FAILED - waiting for "
3638 "commands.\n");
3639 goto eh_dev_reset_done;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003640 }
3641
David C Somayajulu9d562912008-03-19 11:23:03 -07003642 /* Send marker. */
3643 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
3644 MM_LUN_RESET) != QLA_SUCCESS)
3645 goto eh_dev_reset_done;
3646
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303647 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07003648 "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
3649 ha->host_no, cmd->device->channel, cmd->device->id,
3650 cmd->device->lun);
3651
3652 ret = SUCCESS;
3653
3654eh_dev_reset_done:
3655
3656 return ret;
3657}
3658
3659/**
Mike Christiece545032008-02-29 18:25:20 -06003660 * qla4xxx_eh_target_reset - callback for target reset.
3661 * @cmd: Pointer to Linux's SCSI command structure
3662 *
3663 * This routine is called by the Linux OS to reset the target.
3664 **/
3665static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
3666{
3667 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
3668 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christiec01be6d2010-07-22 16:59:49 +05303669 int stat, ret;
Mike Christiece545032008-02-29 18:25:20 -06003670
3671 if (!ddb_entry)
3672 return FAILED;
3673
Mike Christiec01be6d2010-07-22 16:59:49 +05303674 ret = iscsi_block_scsi_eh(cmd);
3675 if (ret)
3676 return ret;
3677
Mike Christiece545032008-02-29 18:25:20 -06003678 starget_printk(KERN_INFO, scsi_target(cmd->device),
3679 "WARM TARGET RESET ISSUED.\n");
3680
3681 DEBUG2(printk(KERN_INFO
3682 "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
3683 "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
Jens Axboe242f9dc2008-09-14 05:55:09 -07003684 ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
Mike Christiece545032008-02-29 18:25:20 -06003685 ha->dpc_flags, cmd->result, cmd->allowed));
3686
3687 stat = qla4xxx_reset_target(ha, ddb_entry);
3688 if (stat != QLA_SUCCESS) {
3689 starget_printk(KERN_INFO, scsi_target(cmd->device),
3690 "WARM TARGET RESET FAILED.\n");
3691 return FAILED;
3692 }
3693
Mike Christiece545032008-02-29 18:25:20 -06003694 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
3695 NULL)) {
3696 starget_printk(KERN_INFO, scsi_target(cmd->device),
3697 "WARM TARGET DEVICE RESET FAILED - "
3698 "waiting for commands.\n");
3699 return FAILED;
3700 }
3701
David C Somayajulu9d562912008-03-19 11:23:03 -07003702 /* Send marker. */
3703 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
3704 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
3705 starget_printk(KERN_INFO, scsi_target(cmd->device),
3706 "WARM TARGET DEVICE RESET FAILED - "
3707 "marker iocb failed.\n");
3708 return FAILED;
3709 }
3710
Mike Christiece545032008-02-29 18:25:20 -06003711 starget_printk(KERN_INFO, scsi_target(cmd->device),
3712 "WARM TARGET RESET SUCCEEDED.\n");
3713 return SUCCESS;
3714}
3715
3716/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07003717 * qla4xxx_eh_host_reset - kernel callback
3718 * @cmd: Pointer to Linux's SCSI command structure
3719 *
3720 * This routine is invoked by the Linux kernel to perform fatal error
3721 * recovery on the specified adapter.
3722 **/
3723static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
3724{
3725 int return_status = FAILED;
3726 struct scsi_qla_host *ha;
3727
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003728 ha = to_qla_host(cmd->device->host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003729
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303730 if (ql4xdontresethba) {
3731 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
3732 ha->host_no, __func__));
3733 return FAILED;
3734 }
3735
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303736 ql4_printk(KERN_INFO, ha,
Karen Higginsdca05c42009-07-15 15:03:00 -05003737 "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
David Somayajuluafaf5a22006-09-19 10:28:00 -07003738 cmd->device->channel, cmd->device->id, cmd->device->lun);
3739
3740 if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
3741 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
3742 "DEAD.\n", ha->host_no, cmd->device->channel,
3743 __func__));
3744
3745 return FAILED;
3746 }
3747
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303748 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
3749 if (is_qla8022(ha))
3750 set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
3751 else
3752 set_bit(DPC_RESET_HA, &ha->dpc_flags);
3753 }
Mike Christie50a29ae2008-03-04 13:26:53 -06003754
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303755 if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
David Somayajuluafaf5a22006-09-19 10:28:00 -07003756 return_status = SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003757
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303758 ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003759 return_status == FAILED ? "FAILED" : "SUCCEEDED");
David Somayajuluafaf5a22006-09-19 10:28:00 -07003760
3761 return return_status;
3762}
3763
Lalit Chandivade2232be02010-07-30 14:38:47 +05303764/* PCI AER driver recovers from all correctable errors w/o
3765 * driver intervention. For uncorrectable errors PCI AER
3766 * driver calls the following device driver's callbacks
3767 *
3768 * - Fatal Errors - link_reset
3769 * - Non-Fatal Errors - driver's pci_error_detected() which
3770 * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
3771 *
3772 * PCI AER driver calls
3773 * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
3774 * returns RECOVERED or NEED_RESET if fw_hung
3775 * NEED_RESET - driver's slot_reset()
3776 * DISCONNECT - device is dead & cannot recover
3777 * RECOVERED - driver's pci_resume()
3778 */
3779static pci_ers_result_t
3780qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
3781{
3782 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
3783
3784 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
3785 ha->host_no, __func__, state);
3786
3787 if (!is_aer_supported(ha))
3788 return PCI_ERS_RESULT_NONE;
3789
3790 switch (state) {
3791 case pci_channel_io_normal:
3792 clear_bit(AF_EEH_BUSY, &ha->flags);
3793 return PCI_ERS_RESULT_CAN_RECOVER;
3794 case pci_channel_io_frozen:
3795 set_bit(AF_EEH_BUSY, &ha->flags);
3796 qla4xxx_mailbox_premature_completion(ha);
3797 qla4xxx_free_irqs(ha);
3798 pci_disable_device(pdev);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07003799 /* Return back all IOs */
3800 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
Lalit Chandivade2232be02010-07-30 14:38:47 +05303801 return PCI_ERS_RESULT_NEED_RESET;
3802 case pci_channel_io_perm_failure:
3803 set_bit(AF_EEH_BUSY, &ha->flags);
3804 set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
3805 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
3806 return PCI_ERS_RESULT_DISCONNECT;
3807 }
3808 return PCI_ERS_RESULT_NEED_RESET;
3809}
3810
3811/**
3812 * qla4xxx_pci_mmio_enabled() gets called if
3813 * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
3814 * and read/write to the device still works.
3815 **/
3816static pci_ers_result_t
3817qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
3818{
3819 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
3820
3821 if (!is_aer_supported(ha))
3822 return PCI_ERS_RESULT_NONE;
3823
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07003824 return PCI_ERS_RESULT_RECOVERED;
Lalit Chandivade2232be02010-07-30 14:38:47 +05303825}
3826
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07003827static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
Lalit Chandivade2232be02010-07-30 14:38:47 +05303828{
3829 uint32_t rval = QLA_ERROR;
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07003830 uint32_t ret = 0;
Lalit Chandivade2232be02010-07-30 14:38:47 +05303831 int fn;
3832 struct pci_dev *other_pdev = NULL;
3833
3834 ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
3835
3836 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
3837
3838 if (test_bit(AF_ONLINE, &ha->flags)) {
3839 clear_bit(AF_ONLINE, &ha->flags);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003840 clear_bit(AF_LINK_UP, &ha->flags);
3841 iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
Lalit Chandivade2232be02010-07-30 14:38:47 +05303842 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
Lalit Chandivade2232be02010-07-30 14:38:47 +05303843 }
3844
3845 fn = PCI_FUNC(ha->pdev->devfn);
3846 while (fn > 0) {
3847 fn--;
3848 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
3849 "func %x\n", ha->host_no, __func__, fn);
3850 /* Get the pci device given the domain, bus,
3851 * slot/function number */
3852 other_pdev =
3853 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
3854 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
3855 fn));
3856
3857 if (!other_pdev)
3858 continue;
3859
3860 if (atomic_read(&other_pdev->enable_cnt)) {
3861 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
3862 "func in enabled state%x\n", ha->host_no,
3863 __func__, fn);
3864 pci_dev_put(other_pdev);
3865 break;
3866 }
3867 pci_dev_put(other_pdev);
3868 }
3869
3870 /* The first function on the card, the reset owner will
3871 * start & initialize the firmware. The other functions
3872 * on the card will reset the firmware context
3873 */
3874 if (!fn) {
3875 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
3876 "0x%x is the owner\n", ha->host_no, __func__,
3877 ha->pdev->devfn);
3878
3879 qla4_8xxx_idc_lock(ha);
3880 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
3881 QLA82XX_DEV_COLD);
3882
3883 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
3884 QLA82XX_IDC_VERSION);
3885
3886 qla4_8xxx_idc_unlock(ha);
3887 clear_bit(AF_FW_RECOVERY, &ha->flags);
Manish Rangankar0e7e8502011-07-25 13:48:54 -05003888 rval = qla4xxx_initialize_adapter(ha);
Lalit Chandivade2232be02010-07-30 14:38:47 +05303889 qla4_8xxx_idc_lock(ha);
3890
3891 if (rval != QLA_SUCCESS) {
3892 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
3893 "FAILED\n", ha->host_no, __func__);
3894 qla4_8xxx_clear_drv_active(ha);
3895 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
3896 QLA82XX_DEV_FAILED);
3897 } else {
3898 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
3899 "READY\n", ha->host_no, __func__);
3900 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
3901 QLA82XX_DEV_READY);
3902 /* Clear driver state register */
3903 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
3904 qla4_8xxx_set_drv_active(ha);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07003905 ret = qla4xxx_request_irqs(ha);
3906 if (ret) {
3907 ql4_printk(KERN_WARNING, ha, "Failed to "
3908 "reserve interrupt %d already in use.\n",
3909 ha->pdev->irq);
3910 rval = QLA_ERROR;
3911 } else {
3912 ha->isp_ops->enable_intrs(ha);
3913 rval = QLA_SUCCESS;
3914 }
Lalit Chandivade2232be02010-07-30 14:38:47 +05303915 }
3916 qla4_8xxx_idc_unlock(ha);
3917 } else {
3918 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
3919 "the reset owner\n", ha->host_no, __func__,
3920 ha->pdev->devfn);
3921 if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
3922 QLA82XX_DEV_READY)) {
3923 clear_bit(AF_FW_RECOVERY, &ha->flags);
Manish Rangankar0e7e8502011-07-25 13:48:54 -05003924 rval = qla4xxx_initialize_adapter(ha);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07003925 if (rval == QLA_SUCCESS) {
3926 ret = qla4xxx_request_irqs(ha);
3927 if (ret) {
3928 ql4_printk(KERN_WARNING, ha, "Failed to"
3929 " reserve interrupt %d already in"
3930 " use.\n", ha->pdev->irq);
3931 rval = QLA_ERROR;
3932 } else {
3933 ha->isp_ops->enable_intrs(ha);
3934 rval = QLA_SUCCESS;
3935 }
3936 }
Lalit Chandivade2232be02010-07-30 14:38:47 +05303937 qla4_8xxx_idc_lock(ha);
3938 qla4_8xxx_set_drv_active(ha);
3939 qla4_8xxx_idc_unlock(ha);
3940 }
3941 }
3942 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
3943 return rval;
3944}
3945
3946static pci_ers_result_t
3947qla4xxx_pci_slot_reset(struct pci_dev *pdev)
3948{
3949 pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
3950 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
3951 int rc;
3952
3953 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
3954 ha->host_no, __func__);
3955
3956 if (!is_aer_supported(ha))
3957 return PCI_ERS_RESULT_NONE;
3958
3959 /* Restore the saved state of PCIe device -
3960 * BAR registers, PCI Config space, PCIX, MSI,
3961 * IOV states
3962 */
3963 pci_restore_state(pdev);
3964
3965 /* pci_restore_state() clears the saved_state flag of the device
3966 * save restored state which resets saved_state flag
3967 */
3968 pci_save_state(pdev);
3969
3970 /* Initialize device or resume if in suspended state */
3971 rc = pci_enable_device(pdev);
3972 if (rc) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003973 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Can't re-enable "
Lalit Chandivade2232be02010-07-30 14:38:47 +05303974 "device after reset\n", ha->host_no, __func__);
3975 goto exit_slot_reset;
3976 }
3977
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07003978 ha->isp_ops->disable_intrs(ha);
Lalit Chandivade2232be02010-07-30 14:38:47 +05303979
3980 if (is_qla8022(ha)) {
3981 if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
3982 ret = PCI_ERS_RESULT_RECOVERED;
3983 goto exit_slot_reset;
3984 } else
3985 goto exit_slot_reset;
3986 }
3987
3988exit_slot_reset:
3989 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
3990 "device after reset\n", ha->host_no, __func__, ret);
3991 return ret;
3992}
3993
3994static void
3995qla4xxx_pci_resume(struct pci_dev *pdev)
3996{
3997 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
3998 int ret;
3999
4000 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
4001 ha->host_no, __func__);
4002
4003 ret = qla4xxx_wait_for_hba_online(ha);
4004 if (ret != QLA_SUCCESS) {
4005 ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
4006 "resume I/O from slot/link_reset\n", ha->host_no,
4007 __func__);
4008 }
4009
4010 pci_cleanup_aer_uncorrect_error_status(pdev);
4011 clear_bit(AF_EEH_BUSY, &ha->flags);
4012}
4013
4014static struct pci_error_handlers qla4xxx_err_handler = {
4015 .error_detected = qla4xxx_pci_error_detected,
4016 .mmio_enabled = qla4xxx_pci_mmio_enabled,
4017 .slot_reset = qla4xxx_pci_slot_reset,
4018 .resume = qla4xxx_pci_resume,
4019};
4020
David Somayajuluafaf5a22006-09-19 10:28:00 -07004021static struct pci_device_id qla4xxx_pci_tbl[] = {
4022 {
4023 .vendor = PCI_VENDOR_ID_QLOGIC,
4024 .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
4025 .subvendor = PCI_ANY_ID,
4026 .subdevice = PCI_ANY_ID,
4027 },
4028 {
4029 .vendor = PCI_VENDOR_ID_QLOGIC,
4030 .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
4031 .subvendor = PCI_ANY_ID,
4032 .subdevice = PCI_ANY_ID,
4033 },
David C Somayajulud9150582006-11-15 17:38:40 -08004034 {
4035 .vendor = PCI_VENDOR_ID_QLOGIC,
4036 .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
4037 .subvendor = PCI_ANY_ID,
4038 .subdevice = PCI_ANY_ID,
4039 },
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304040 {
4041 .vendor = PCI_VENDOR_ID_QLOGIC,
4042 .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
4043 .subvendor = PCI_ANY_ID,
4044 .subdevice = PCI_ANY_ID,
4045 },
David Somayajuluafaf5a22006-09-19 10:28:00 -07004046 {0, 0},
4047};
4048MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
4049
Adrian Bunk47975472007-04-26 00:35:16 -07004050static struct pci_driver qla4xxx_pci_driver = {
David Somayajuluafaf5a22006-09-19 10:28:00 -07004051 .name = DRIVER_NAME,
4052 .id_table = qla4xxx_pci_tbl,
4053 .probe = qla4xxx_probe_adapter,
4054 .remove = qla4xxx_remove_adapter,
Lalit Chandivade2232be02010-07-30 14:38:47 +05304055 .err_handler = &qla4xxx_err_handler,
David Somayajuluafaf5a22006-09-19 10:28:00 -07004056};
4057
4058static int __init qla4xxx_module_init(void)
4059{
4060 int ret;
4061
4062 /* Allocate cache for SRBs. */
4063 srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09004064 SLAB_HWCACHE_ALIGN, NULL);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004065 if (srb_cachep == NULL) {
4066 printk(KERN_ERR
4067 "%s: Unable to allocate SRB cache..."
4068 "Failing load!\n", DRIVER_NAME);
4069 ret = -ENOMEM;
4070 goto no_srp_cache;
4071 }
4072
4073 /* Derive version string. */
4074 strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
Andrew Vasquez11010fe2006-10-06 09:54:59 -07004075 if (ql4xextended_error_logging)
David Somayajuluafaf5a22006-09-19 10:28:00 -07004076 strcat(qla4xxx_version_str, "-debug");
4077
4078 qla4xxx_scsi_transport =
4079 iscsi_register_transport(&qla4xxx_iscsi_transport);
4080 if (!qla4xxx_scsi_transport){
4081 ret = -ENODEV;
4082 goto release_srb_cache;
4083 }
4084
David Somayajuluafaf5a22006-09-19 10:28:00 -07004085 ret = pci_register_driver(&qla4xxx_pci_driver);
4086 if (ret)
4087 goto unregister_transport;
4088
4089 printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
4090 return 0;
Doug Maxey5ae16db2006-10-05 23:50:07 -05004091
David Somayajuluafaf5a22006-09-19 10:28:00 -07004092unregister_transport:
4093 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
4094release_srb_cache:
4095 kmem_cache_destroy(srb_cachep);
4096no_srp_cache:
4097 return ret;
4098}
4099
4100static void __exit qla4xxx_module_exit(void)
4101{
David Somayajuluafaf5a22006-09-19 10:28:00 -07004102 pci_unregister_driver(&qla4xxx_pci_driver);
4103 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
4104 kmem_cache_destroy(srb_cachep);
4105}
4106
4107module_init(qla4xxx_module_init);
4108module_exit(qla4xxx_module_exit);
4109
4110MODULE_AUTHOR("QLogic Corporation");
4111MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
4112MODULE_LICENSE("GPL");
4113MODULE_VERSION(QLA4XXX_DRIVER_VERSION);