blob: bea78a7f6bc161a5898e51dfa37296dccb904f2a [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 Chaudharyf4f5df232010-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 Chaudharyf4f5df232010-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 Chaudharyf4f5df232010-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 Chaudharyf4f5df232010-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);
Vikas Chaudhary95d31262011-08-12 02:51:29 -0700124static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700125
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530126static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
127 QLA82XX_LEGACY_INTR_CONFIG;
128
David Somayajuluafaf5a22006-09-19 10:28:00 -0700129static struct scsi_host_template qla4xxx_driver_template = {
130 .module = THIS_MODULE,
131 .name = DRIVER_NAME,
132 .proc_name = DRIVER_NAME,
133 .queuecommand = qla4xxx_queuecommand,
134
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530135 .eh_abort_handler = qla4xxx_eh_abort,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700136 .eh_device_reset_handler = qla4xxx_eh_device_reset,
Mike Christiece545032008-02-29 18:25:20 -0600137 .eh_target_reset_handler = qla4xxx_eh_target_reset,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700138 .eh_host_reset_handler = qla4xxx_eh_host_reset,
Mike Christie5c656af2009-07-15 15:02:59 -0500139 .eh_timed_out = qla4xxx_eh_cmd_timed_out,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700140
141 .slave_configure = qla4xxx_slave_configure,
142 .slave_alloc = qla4xxx_slave_alloc,
143 .slave_destroy = qla4xxx_slave_destroy,
144
145 .this_id = -1,
146 .cmd_per_lun = 3,
147 .use_clustering = ENABLE_CLUSTERING,
148 .sg_tablesize = SG_ALL,
149
150 .max_sectors = 0xFFFF,
Harish Zunjarrao7ad633c2011-05-17 23:17:11 -0700151 .shost_attrs = qla4xxx_host_attrs,
Vikas Chaudhary95d31262011-08-12 02:51:29 -0700152 .host_reset = qla4xxx_host_reset,
Vikas Chaudharya3559432011-07-25 13:48:51 -0500153 .vendor_id = SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_QLOGIC,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700154};
155
156static struct iscsi_transport qla4xxx_iscsi_transport = {
157 .owner = THIS_MODULE,
158 .name = DRIVER_NAME,
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500159 .caps = CAP_TEXT_NEGO |
160 CAP_DATA_PATH_OFFLOAD | CAP_HDRDGST |
161 CAP_DATADGST | CAP_LOGIN_OFFLOAD |
162 CAP_MULTI_R2T,
Mike Christie3128c6c2011-07-25 13:48:42 -0500163 .attr_is_visible = ql4_attr_is_visible,
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500164 .create_session = qla4xxx_session_create,
165 .destroy_session = qla4xxx_session_destroy,
166 .start_conn = qla4xxx_conn_start,
167 .create_conn = qla4xxx_conn_create,
168 .bind_conn = qla4xxx_conn_bind,
169 .stop_conn = iscsi_conn_stop,
170 .destroy_conn = qla4xxx_conn_destroy,
171 .set_param = iscsi_set_param,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700172 .get_conn_param = qla4xxx_conn_get_param,
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500173 .get_session_param = iscsi_session_get_param,
174 .get_ep_param = qla4xxx_get_ep_param,
175 .ep_connect = qla4xxx_ep_connect,
176 .ep_poll = qla4xxx_ep_poll,
177 .ep_disconnect = qla4xxx_ep_disconnect,
178 .get_stats = qla4xxx_conn_get_stats,
179 .send_pdu = iscsi_conn_send_pdu,
180 .xmit_task = qla4xxx_task_xmit,
181 .cleanup_task = qla4xxx_task_cleanup,
182 .alloc_pdu = qla4xxx_alloc_pdu,
183
Mike Christieaa1e93a2007-05-30 12:57:09 -0500184 .get_host_param = qla4xxx_host_get_param,
Mike Christied00efe32011-07-25 13:48:38 -0500185 .set_iface_param = qla4xxx_iface_set_param,
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500186 .get_iface_param = qla4xxx_get_iface_param,
Vikas Chaudharya3559432011-07-25 13:48:51 -0500187 .bsg_request = qla4xxx_bsg_request,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700188};
189
190static struct scsi_transport_template *qla4xxx_scsi_transport;
191
Mike Christie3128c6c2011-07-25 13:48:42 -0500192static mode_t ql4_attr_is_visible(int param_type, int param)
193{
194 switch (param_type) {
Mike Christief27fb2e2011-07-25 13:48:45 -0500195 case ISCSI_HOST_PARAM:
196 switch (param) {
197 case ISCSI_HOST_PARAM_HWADDRESS:
198 case ISCSI_HOST_PARAM_IPADDRESS:
199 case ISCSI_HOST_PARAM_INITIATOR_NAME:
200 return S_IRUGO;
201 default:
202 return 0;
203 }
Mike Christie3128c6c2011-07-25 13:48:42 -0500204 case ISCSI_PARAM:
205 switch (param) {
206 case ISCSI_PARAM_CONN_ADDRESS:
207 case ISCSI_PARAM_CONN_PORT:
Mike Christie1d063c12011-07-25 13:48:43 -0500208 case ISCSI_PARAM_TARGET_NAME:
209 case ISCSI_PARAM_TPGT:
210 case ISCSI_PARAM_TARGET_ALIAS:
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500211 case ISCSI_PARAM_MAX_BURST:
212 case ISCSI_PARAM_MAX_R2T:
213 case ISCSI_PARAM_FIRST_BURST:
214 case ISCSI_PARAM_MAX_RECV_DLENGTH:
215 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
Mike Christiede379202011-08-14 20:42:56 -0500216 case ISCSI_PARAM_IFACE_NAME:
Mike Christie3128c6c2011-07-25 13:48:42 -0500217 return S_IRUGO;
218 default:
219 return 0;
220 }
Mike Christieb78dbba2011-07-25 13:48:44 -0500221 case ISCSI_NET_PARAM:
222 switch (param) {
223 case ISCSI_NET_PARAM_IPV4_ADDR:
224 case ISCSI_NET_PARAM_IPV4_SUBNET:
225 case ISCSI_NET_PARAM_IPV4_GW:
226 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
227 case ISCSI_NET_PARAM_IFACE_ENABLE:
228 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
229 case ISCSI_NET_PARAM_IPV6_ADDR:
230 case ISCSI_NET_PARAM_IPV6_ROUTER:
231 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
232 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
Vikas Chaudhary6ac73e82011-07-25 13:48:49 -0500233 case ISCSI_NET_PARAM_VLAN_ID:
234 case ISCSI_NET_PARAM_VLAN_PRIORITY:
235 case ISCSI_NET_PARAM_VLAN_ENABLED:
Vikas Chaudhary943c1572011-08-01 03:26:13 -0700236 case ISCSI_NET_PARAM_MTU:
Vikas Chaudhary2ada7fc2011-08-01 03:26:19 -0700237 case ISCSI_NET_PARAM_PORT:
Mike Christieb78dbba2011-07-25 13:48:44 -0500238 return S_IRUGO;
239 default:
240 return 0;
241 }
Mike Christie3128c6c2011-07-25 13:48:42 -0500242 }
243
244 return 0;
245}
246
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500247static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
248 enum iscsi_param_type param_type,
249 int param, char *buf)
250{
251 struct Scsi_Host *shost = iscsi_iface_to_shost(iface);
252 struct scsi_qla_host *ha = to_qla_host(shost);
253 int len = -ENOSYS;
254
255 if (param_type != ISCSI_NET_PARAM)
256 return -ENOSYS;
257
258 switch (param) {
259 case ISCSI_NET_PARAM_IPV4_ADDR:
260 len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
261 break;
262 case ISCSI_NET_PARAM_IPV4_SUBNET:
263 len = sprintf(buf, "%pI4\n", &ha->ip_config.subnet_mask);
264 break;
265 case ISCSI_NET_PARAM_IPV4_GW:
266 len = sprintf(buf, "%pI4\n", &ha->ip_config.gateway);
267 break;
268 case ISCSI_NET_PARAM_IFACE_ENABLE:
269 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
270 len = sprintf(buf, "%s\n",
271 (ha->ip_config.ipv4_options &
272 IPOPT_IPV4_PROTOCOL_ENABLE) ?
273 "enabled" : "disabled");
274 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
275 len = sprintf(buf, "%s\n",
276 (ha->ip_config.ipv6_options &
277 IPV6_OPT_IPV6_PROTOCOL_ENABLE) ?
278 "enabled" : "disabled");
279 break;
280 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
281 len = sprintf(buf, "%s\n",
282 (ha->ip_config.tcp_options & TCPOPT_DHCP_ENABLE) ?
283 "dhcp" : "static");
284 break;
285 case ISCSI_NET_PARAM_IPV6_ADDR:
286 if (iface->iface_num == 0)
287 len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr0);
288 if (iface->iface_num == 1)
289 len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr1);
290 break;
291 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
292 len = sprintf(buf, "%pI6\n",
293 &ha->ip_config.ipv6_link_local_addr);
294 break;
295 case ISCSI_NET_PARAM_IPV6_ROUTER:
296 len = sprintf(buf, "%pI6\n",
297 &ha->ip_config.ipv6_default_router_addr);
298 break;
299 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
300 len = sprintf(buf, "%s\n",
301 (ha->ip_config.ipv6_addl_options &
302 IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) ?
303 "nd" : "static");
304 break;
305 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
306 len = sprintf(buf, "%s\n",
307 (ha->ip_config.ipv6_addl_options &
308 IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR) ?
309 "auto" : "static");
310 break;
Vikas Chaudhary6ac73e82011-07-25 13:48:49 -0500311 case ISCSI_NET_PARAM_VLAN_ID:
312 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
313 len = sprintf(buf, "%d\n",
314 (ha->ip_config.ipv4_vlan_tag &
315 ISCSI_MAX_VLAN_ID));
316 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
317 len = sprintf(buf, "%d\n",
318 (ha->ip_config.ipv6_vlan_tag &
319 ISCSI_MAX_VLAN_ID));
320 break;
321 case ISCSI_NET_PARAM_VLAN_PRIORITY:
322 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
323 len = sprintf(buf, "%d\n",
324 ((ha->ip_config.ipv4_vlan_tag >> 13) &
325 ISCSI_MAX_VLAN_PRIORITY));
326 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
327 len = sprintf(buf, "%d\n",
328 ((ha->ip_config.ipv6_vlan_tag >> 13) &
329 ISCSI_MAX_VLAN_PRIORITY));
330 break;
331 case ISCSI_NET_PARAM_VLAN_ENABLED:
332 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
333 len = sprintf(buf, "%s\n",
334 (ha->ip_config.ipv4_options &
335 IPOPT_VLAN_TAGGING_ENABLE) ?
336 "enabled" : "disabled");
337 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
338 len = sprintf(buf, "%s\n",
339 (ha->ip_config.ipv6_options &
340 IPV6_OPT_VLAN_TAGGING_ENABLE) ?
341 "enabled" : "disabled");
342 break;
Vikas Chaudhary943c1572011-08-01 03:26:13 -0700343 case ISCSI_NET_PARAM_MTU:
344 len = sprintf(buf, "%d\n", ha->ip_config.eth_mtu_size);
345 break;
Vikas Chaudhary2ada7fc2011-08-01 03:26:19 -0700346 case ISCSI_NET_PARAM_PORT:
347 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
348 len = sprintf(buf, "%d\n", ha->ip_config.ipv4_port);
349 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
350 len = sprintf(buf, "%d\n", ha->ip_config.ipv6_port);
351 break;
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500352 default:
353 len = -ENOSYS;
354 }
355
356 return len;
357}
358
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500359static struct iscsi_endpoint *
360qla4xxx_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
361 int non_blocking)
362{
363 int ret;
364 struct iscsi_endpoint *ep;
365 struct qla_endpoint *qla_ep;
366 struct scsi_qla_host *ha;
367 struct sockaddr_in *addr;
368 struct sockaddr_in6 *addr6;
369
370 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
371 if (!shost) {
372 ret = -ENXIO;
373 printk(KERN_ERR "%s: shost is NULL\n",
374 __func__);
375 return ERR_PTR(ret);
376 }
377
378 ha = iscsi_host_priv(shost);
379
380 ep = iscsi_create_endpoint(sizeof(struct qla_endpoint));
381 if (!ep) {
382 ret = -ENOMEM;
383 return ERR_PTR(ret);
384 }
385
386 qla_ep = ep->dd_data;
387 memset(qla_ep, 0, sizeof(struct qla_endpoint));
388 if (dst_addr->sa_family == AF_INET) {
389 memcpy(&qla_ep->dst_addr, dst_addr, sizeof(struct sockaddr_in));
390 addr = (struct sockaddr_in *)&qla_ep->dst_addr;
391 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI4\n", __func__,
392 (char *)&addr->sin_addr));
393 } else if (dst_addr->sa_family == AF_INET6) {
394 memcpy(&qla_ep->dst_addr, dst_addr,
395 sizeof(struct sockaddr_in6));
396 addr6 = (struct sockaddr_in6 *)&qla_ep->dst_addr;
397 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI6\n", __func__,
398 (char *)&addr6->sin6_addr));
399 }
400
401 qla_ep->host = shost;
402
403 return ep;
404}
405
406static int qla4xxx_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
407{
408 struct qla_endpoint *qla_ep;
409 struct scsi_qla_host *ha;
410 int ret = 0;
411
412 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
413 qla_ep = ep->dd_data;
414 ha = to_qla_host(qla_ep->host);
415
416 if (adapter_up(ha))
417 ret = 1;
418
419 return ret;
420}
421
422static void qla4xxx_ep_disconnect(struct iscsi_endpoint *ep)
423{
424 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
425 iscsi_destroy_endpoint(ep);
426}
427
428static int qla4xxx_get_ep_param(struct iscsi_endpoint *ep,
429 enum iscsi_param param,
430 char *buf)
431{
432 struct qla_endpoint *qla_ep = ep->dd_data;
433 struct sockaddr *dst_addr;
434
435 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
436
437 switch (param) {
438 case ISCSI_PARAM_CONN_PORT:
439 case ISCSI_PARAM_CONN_ADDRESS:
440 if (!qla_ep)
441 return -ENOTCONN;
442
443 dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
444 if (!dst_addr)
445 return -ENOTCONN;
446
447 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
448 &qla_ep->dst_addr, param, buf);
449 default:
450 return -ENOSYS;
451 }
452}
453
454static void qla4xxx_conn_get_stats(struct iscsi_cls_conn *cls_conn,
455 struct iscsi_stats *stats)
456{
457 struct iscsi_session *sess;
458 struct iscsi_cls_session *cls_sess;
459 struct ddb_entry *ddb_entry;
460 struct scsi_qla_host *ha;
461 struct ql_iscsi_stats *ql_iscsi_stats;
462 int stats_size;
463 int ret;
464 dma_addr_t iscsi_stats_dma;
465
466 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
467
468 cls_sess = iscsi_conn_to_session(cls_conn);
469 sess = cls_sess->dd_data;
470 ddb_entry = sess->dd_data;
471 ha = ddb_entry->ha;
472
473 stats_size = PAGE_ALIGN(sizeof(struct ql_iscsi_stats));
474 /* Allocate memory */
475 ql_iscsi_stats = dma_alloc_coherent(&ha->pdev->dev, stats_size,
476 &iscsi_stats_dma, GFP_KERNEL);
477 if (!ql_iscsi_stats) {
478 ql4_printk(KERN_ERR, ha,
479 "Unable to allocate memory for iscsi stats\n");
480 goto exit_get_stats;
481 }
482
483 ret = qla4xxx_get_mgmt_data(ha, ddb_entry->fw_ddb_index, stats_size,
484 iscsi_stats_dma);
485 if (ret != QLA_SUCCESS) {
486 ql4_printk(KERN_ERR, ha,
487 "Unable to retreive iscsi stats\n");
488 goto free_stats;
489 }
490
491 /* octets */
492 stats->txdata_octets = le64_to_cpu(ql_iscsi_stats->tx_data_octets);
493 stats->rxdata_octets = le64_to_cpu(ql_iscsi_stats->rx_data_octets);
494 /* xmit pdus */
495 stats->noptx_pdus = le32_to_cpu(ql_iscsi_stats->tx_nopout_pdus);
496 stats->scsicmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_cmd_pdus);
497 stats->tmfcmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_tmf_cmd_pdus);
498 stats->login_pdus = le32_to_cpu(ql_iscsi_stats->tx_login_cmd_pdus);
499 stats->text_pdus = le32_to_cpu(ql_iscsi_stats->tx_text_cmd_pdus);
500 stats->dataout_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_write_pdus);
501 stats->logout_pdus = le32_to_cpu(ql_iscsi_stats->tx_logout_cmd_pdus);
502 stats->snack_pdus = le32_to_cpu(ql_iscsi_stats->tx_snack_req_pdus);
503 /* recv pdus */
504 stats->noprx_pdus = le32_to_cpu(ql_iscsi_stats->rx_nopin_pdus);
505 stats->scsirsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_resp_pdus);
506 stats->tmfrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_tmf_resp_pdus);
507 stats->textrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_text_resp_pdus);
508 stats->datain_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_read_pdus);
509 stats->logoutrsp_pdus =
510 le32_to_cpu(ql_iscsi_stats->rx_logout_resp_pdus);
511 stats->r2t_pdus = le32_to_cpu(ql_iscsi_stats->rx_r2t_pdus);
512 stats->async_pdus = le32_to_cpu(ql_iscsi_stats->rx_async_pdus);
513 stats->rjt_pdus = le32_to_cpu(ql_iscsi_stats->rx_reject_pdus);
514
515free_stats:
516 dma_free_coherent(&ha->pdev->dev, stats_size, ql_iscsi_stats,
517 iscsi_stats_dma);
518exit_get_stats:
519 return;
520}
521
Mike Christie5c656af2009-07-15 15:02:59 -0500522static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
523{
524 struct iscsi_cls_session *session;
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500525 struct iscsi_session *sess;
526 unsigned long flags;
527 enum blk_eh_timer_return ret = BLK_EH_NOT_HANDLED;
Mike Christie5c656af2009-07-15 15:02:59 -0500528
529 session = starget_to_session(scsi_target(sc->device));
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500530 sess = session->dd_data;
Mike Christie5c656af2009-07-15 15:02:59 -0500531
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500532 spin_lock_irqsave(&session->lock, flags);
533 if (session->state == ISCSI_SESSION_FAILED)
534 ret = BLK_EH_RESET_TIMER;
535 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie5c656af2009-07-15 15:02:59 -0500536
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500537 return ret;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700538}
539
Mike Christieaa1e93a2007-05-30 12:57:09 -0500540static int qla4xxx_host_get_param(struct Scsi_Host *shost,
541 enum iscsi_host_param param, char *buf)
542{
543 struct scsi_qla_host *ha = to_qla_host(shost);
544 int len;
545
546 switch (param) {
547 case ISCSI_HOST_PARAM_HWADDRESS:
Michael Chan7ffc49a2007-12-24 21:28:09 -0800548 len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
Mike Christieaa1e93a2007-05-30 12:57:09 -0500549 break;
Mike Christie22236962007-05-30 12:57:24 -0500550 case ISCSI_HOST_PARAM_IPADDRESS:
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500551 len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
Mike Christie22236962007-05-30 12:57:24 -0500552 break;
Mike Christie8ad57812007-05-30 12:57:13 -0500553 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie22236962007-05-30 12:57:24 -0500554 len = sprintf(buf, "%s\n", ha->name_string);
Mike Christie8ad57812007-05-30 12:57:13 -0500555 break;
Mike Christieaa1e93a2007-05-30 12:57:09 -0500556 default:
557 return -ENOSYS;
558 }
559
560 return len;
561}
562
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500563static void qla4xxx_create_ipv4_iface(struct scsi_qla_host *ha)
564{
565 if (ha->iface_ipv4)
566 return;
567
568 /* IPv4 */
569 ha->iface_ipv4 = iscsi_create_iface(ha->host,
570 &qla4xxx_iscsi_transport,
571 ISCSI_IFACE_TYPE_IPV4, 0, 0);
572 if (!ha->iface_ipv4)
573 ql4_printk(KERN_ERR, ha, "Could not create IPv4 iSCSI "
574 "iface0.\n");
575}
576
577static void qla4xxx_create_ipv6_iface(struct scsi_qla_host *ha)
578{
579 if (!ha->iface_ipv6_0)
580 /* IPv6 iface-0 */
581 ha->iface_ipv6_0 = iscsi_create_iface(ha->host,
582 &qla4xxx_iscsi_transport,
583 ISCSI_IFACE_TYPE_IPV6, 0,
584 0);
585 if (!ha->iface_ipv6_0)
586 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
587 "iface0.\n");
588
589 if (!ha->iface_ipv6_1)
590 /* IPv6 iface-1 */
591 ha->iface_ipv6_1 = iscsi_create_iface(ha->host,
592 &qla4xxx_iscsi_transport,
593 ISCSI_IFACE_TYPE_IPV6, 1,
594 0);
595 if (!ha->iface_ipv6_1)
596 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
597 "iface1.\n");
598}
599
600static void qla4xxx_create_ifaces(struct scsi_qla_host *ha)
601{
602 if (ha->ip_config.ipv4_options & IPOPT_IPV4_PROTOCOL_ENABLE)
603 qla4xxx_create_ipv4_iface(ha);
604
605 if (ha->ip_config.ipv6_options & IPV6_OPT_IPV6_PROTOCOL_ENABLE)
606 qla4xxx_create_ipv6_iface(ha);
607}
608
609static void qla4xxx_destroy_ipv4_iface(struct scsi_qla_host *ha)
610{
611 if (ha->iface_ipv4) {
612 iscsi_destroy_iface(ha->iface_ipv4);
613 ha->iface_ipv4 = NULL;
614 }
615}
616
617static void qla4xxx_destroy_ipv6_iface(struct scsi_qla_host *ha)
618{
619 if (ha->iface_ipv6_0) {
620 iscsi_destroy_iface(ha->iface_ipv6_0);
621 ha->iface_ipv6_0 = NULL;
622 }
623 if (ha->iface_ipv6_1) {
624 iscsi_destroy_iface(ha->iface_ipv6_1);
625 ha->iface_ipv6_1 = NULL;
626 }
627}
628
629static void qla4xxx_destroy_ifaces(struct scsi_qla_host *ha)
630{
631 qla4xxx_destroy_ipv4_iface(ha);
632 qla4xxx_destroy_ipv6_iface(ha);
633}
634
Mike Christied00efe32011-07-25 13:48:38 -0500635static void qla4xxx_set_ipv6(struct scsi_qla_host *ha,
636 struct iscsi_iface_param_info *iface_param,
637 struct addr_ctrl_blk *init_fw_cb)
638{
639 /*
640 * iface_num 0 is valid for IPv6 Addr, linklocal, router, autocfg.
641 * iface_num 1 is valid only for IPv6 Addr.
642 */
643 switch (iface_param->param) {
644 case ISCSI_NET_PARAM_IPV6_ADDR:
645 if (iface_param->iface_num & 0x1)
646 /* IPv6 Addr 1 */
647 memcpy(init_fw_cb->ipv6_addr1, iface_param->value,
648 sizeof(init_fw_cb->ipv6_addr1));
649 else
650 /* IPv6 Addr 0 */
651 memcpy(init_fw_cb->ipv6_addr0, iface_param->value,
652 sizeof(init_fw_cb->ipv6_addr0));
653 break;
654 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
655 if (iface_param->iface_num & 0x1)
656 break;
657 memcpy(init_fw_cb->ipv6_if_id, &iface_param->value[8],
658 sizeof(init_fw_cb->ipv6_if_id));
659 break;
660 case ISCSI_NET_PARAM_IPV6_ROUTER:
661 if (iface_param->iface_num & 0x1)
662 break;
663 memcpy(init_fw_cb->ipv6_dflt_rtr_addr, iface_param->value,
664 sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
665 break;
666 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
667 /* Autocfg applies to even interface */
668 if (iface_param->iface_num & 0x1)
669 break;
670
671 if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_DISABLE)
672 init_fw_cb->ipv6_addtl_opts &=
673 cpu_to_le16(
674 ~IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
675 else if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_ND_ENABLE)
676 init_fw_cb->ipv6_addtl_opts |=
677 cpu_to_le16(
678 IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
679 else
680 ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
681 "IPv6 addr\n");
682 break;
683 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
684 /* Autocfg applies to even interface */
685 if (iface_param->iface_num & 0x1)
686 break;
687
688 if (iface_param->value[0] ==
689 ISCSI_IPV6_LINKLOCAL_AUTOCFG_ENABLE)
690 init_fw_cb->ipv6_addtl_opts |= cpu_to_le16(
691 IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
692 else if (iface_param->value[0] ==
693 ISCSI_IPV6_LINKLOCAL_AUTOCFG_DISABLE)
694 init_fw_cb->ipv6_addtl_opts &= cpu_to_le16(
695 ~IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
696 else
697 ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
698 "IPv6 linklocal addr\n");
699 break;
700 case ISCSI_NET_PARAM_IPV6_ROUTER_AUTOCFG:
701 /* Autocfg applies to even interface */
702 if (iface_param->iface_num & 0x1)
703 break;
704
705 if (iface_param->value[0] == ISCSI_IPV6_ROUTER_AUTOCFG_ENABLE)
706 memset(init_fw_cb->ipv6_dflt_rtr_addr, 0,
707 sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
708 break;
709 case ISCSI_NET_PARAM_IFACE_ENABLE:
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500710 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
Mike Christied00efe32011-07-25 13:48:38 -0500711 init_fw_cb->ipv6_opts |=
712 cpu_to_le16(IPV6_OPT_IPV6_PROTOCOL_ENABLE);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500713 qla4xxx_create_ipv6_iface(ha);
714 } else {
Mike Christied00efe32011-07-25 13:48:38 -0500715 init_fw_cb->ipv6_opts &=
716 cpu_to_le16(~IPV6_OPT_IPV6_PROTOCOL_ENABLE &
717 0xFFFF);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500718 qla4xxx_destroy_ipv6_iface(ha);
719 }
Mike Christied00efe32011-07-25 13:48:38 -0500720 break;
721 case ISCSI_NET_PARAM_VLAN_ID:
722 if (iface_param->len != sizeof(init_fw_cb->ipv6_vlan_tag))
723 break;
Vikas Chaudhary6ac73e82011-07-25 13:48:49 -0500724 init_fw_cb->ipv6_vlan_tag =
725 cpu_to_be16(*(uint16_t *)iface_param->value);
726 break;
727 case ISCSI_NET_PARAM_VLAN_ENABLED:
728 if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
729 init_fw_cb->ipv6_opts |=
730 cpu_to_le16(IPV6_OPT_VLAN_TAGGING_ENABLE);
731 else
732 init_fw_cb->ipv6_opts &=
733 cpu_to_le16(~IPV6_OPT_VLAN_TAGGING_ENABLE);
Mike Christied00efe32011-07-25 13:48:38 -0500734 break;
Vikas Chaudhary943c1572011-08-01 03:26:13 -0700735 case ISCSI_NET_PARAM_MTU:
736 init_fw_cb->eth_mtu_size =
737 cpu_to_le16(*(uint16_t *)iface_param->value);
738 break;
Vikas Chaudhary2ada7fc2011-08-01 03:26:19 -0700739 case ISCSI_NET_PARAM_PORT:
740 /* Autocfg applies to even interface */
741 if (iface_param->iface_num & 0x1)
742 break;
743
744 init_fw_cb->ipv6_port =
745 cpu_to_le16(*(uint16_t *)iface_param->value);
746 break;
Mike Christied00efe32011-07-25 13:48:38 -0500747 default:
748 ql4_printk(KERN_ERR, ha, "Unknown IPv6 param = %d\n",
749 iface_param->param);
750 break;
751 }
752}
753
754static void qla4xxx_set_ipv4(struct scsi_qla_host *ha,
755 struct iscsi_iface_param_info *iface_param,
756 struct addr_ctrl_blk *init_fw_cb)
757{
758 switch (iface_param->param) {
759 case ISCSI_NET_PARAM_IPV4_ADDR:
760 memcpy(init_fw_cb->ipv4_addr, iface_param->value,
761 sizeof(init_fw_cb->ipv4_addr));
762 break;
763 case ISCSI_NET_PARAM_IPV4_SUBNET:
764 memcpy(init_fw_cb->ipv4_subnet, iface_param->value,
765 sizeof(init_fw_cb->ipv4_subnet));
766 break;
767 case ISCSI_NET_PARAM_IPV4_GW:
768 memcpy(init_fw_cb->ipv4_gw_addr, iface_param->value,
769 sizeof(init_fw_cb->ipv4_gw_addr));
770 break;
771 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
772 if (iface_param->value[0] == ISCSI_BOOTPROTO_DHCP)
773 init_fw_cb->ipv4_tcp_opts |=
774 cpu_to_le16(TCPOPT_DHCP_ENABLE);
775 else if (iface_param->value[0] == ISCSI_BOOTPROTO_STATIC)
776 init_fw_cb->ipv4_tcp_opts &=
777 cpu_to_le16(~TCPOPT_DHCP_ENABLE);
778 else
779 ql4_printk(KERN_ERR, ha, "Invalid IPv4 bootproto\n");
780 break;
781 case ISCSI_NET_PARAM_IFACE_ENABLE:
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500782 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
Mike Christied00efe32011-07-25 13:48:38 -0500783 init_fw_cb->ipv4_ip_opts |=
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500784 cpu_to_le16(IPOPT_IPV4_PROTOCOL_ENABLE);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500785 qla4xxx_create_ipv4_iface(ha);
786 } else {
Mike Christied00efe32011-07-25 13:48:38 -0500787 init_fw_cb->ipv4_ip_opts &=
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500788 cpu_to_le16(~IPOPT_IPV4_PROTOCOL_ENABLE &
Mike Christied00efe32011-07-25 13:48:38 -0500789 0xFFFF);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500790 qla4xxx_destroy_ipv4_iface(ha);
791 }
Mike Christied00efe32011-07-25 13:48:38 -0500792 break;
793 case ISCSI_NET_PARAM_VLAN_ID:
794 if (iface_param->len != sizeof(init_fw_cb->ipv4_vlan_tag))
795 break;
Vikas Chaudhary6ac73e82011-07-25 13:48:49 -0500796 init_fw_cb->ipv4_vlan_tag =
797 cpu_to_be16(*(uint16_t *)iface_param->value);
798 break;
799 case ISCSI_NET_PARAM_VLAN_ENABLED:
800 if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
801 init_fw_cb->ipv4_ip_opts |=
802 cpu_to_le16(IPOPT_VLAN_TAGGING_ENABLE);
803 else
804 init_fw_cb->ipv4_ip_opts &=
805 cpu_to_le16(~IPOPT_VLAN_TAGGING_ENABLE);
Mike Christied00efe32011-07-25 13:48:38 -0500806 break;
Vikas Chaudhary943c1572011-08-01 03:26:13 -0700807 case ISCSI_NET_PARAM_MTU:
808 init_fw_cb->eth_mtu_size =
809 cpu_to_le16(*(uint16_t *)iface_param->value);
810 break;
Vikas Chaudhary2ada7fc2011-08-01 03:26:19 -0700811 case ISCSI_NET_PARAM_PORT:
812 init_fw_cb->ipv4_port =
813 cpu_to_le16(*(uint16_t *)iface_param->value);
814 break;
Mike Christied00efe32011-07-25 13:48:38 -0500815 default:
816 ql4_printk(KERN_ERR, ha, "Unknown IPv4 param = %d\n",
817 iface_param->param);
818 break;
819 }
820}
821
822static void
823qla4xxx_initcb_to_acb(struct addr_ctrl_blk *init_fw_cb)
824{
825 struct addr_ctrl_blk_def *acb;
826 acb = (struct addr_ctrl_blk_def *)init_fw_cb;
827 memset(acb->reserved1, 0, sizeof(acb->reserved1));
828 memset(acb->reserved2, 0, sizeof(acb->reserved2));
829 memset(acb->reserved3, 0, sizeof(acb->reserved3));
830 memset(acb->reserved4, 0, sizeof(acb->reserved4));
831 memset(acb->reserved5, 0, sizeof(acb->reserved5));
832 memset(acb->reserved6, 0, sizeof(acb->reserved6));
833 memset(acb->reserved7, 0, sizeof(acb->reserved7));
834 memset(acb->reserved8, 0, sizeof(acb->reserved8));
835 memset(acb->reserved9, 0, sizeof(acb->reserved9));
836 memset(acb->reserved10, 0, sizeof(acb->reserved10));
837 memset(acb->reserved11, 0, sizeof(acb->reserved11));
838 memset(acb->reserved12, 0, sizeof(acb->reserved12));
839 memset(acb->reserved13, 0, sizeof(acb->reserved13));
840 memset(acb->reserved14, 0, sizeof(acb->reserved14));
841 memset(acb->reserved15, 0, sizeof(acb->reserved15));
842}
843
844static int
845qla4xxx_iface_set_param(struct Scsi_Host *shost, char *data, int count)
846{
847 struct scsi_qla_host *ha = to_qla_host(shost);
848 int rval = 0;
849 struct iscsi_iface_param_info *iface_param = NULL;
850 struct addr_ctrl_blk *init_fw_cb = NULL;
851 dma_addr_t init_fw_cb_dma;
852 uint32_t mbox_cmd[MBOX_REG_COUNT];
853 uint32_t mbox_sts[MBOX_REG_COUNT];
854 uint32_t total_param_count;
855 uint32_t length;
856
857 init_fw_cb = dma_alloc_coherent(&ha->pdev->dev,
858 sizeof(struct addr_ctrl_blk),
859 &init_fw_cb_dma, GFP_KERNEL);
860 if (!init_fw_cb) {
861 ql4_printk(KERN_ERR, ha, "%s: Unable to alloc init_cb\n",
862 __func__);
863 return -ENOMEM;
864 }
865
866 memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
867 memset(&mbox_cmd, 0, sizeof(mbox_cmd));
868 memset(&mbox_sts, 0, sizeof(mbox_sts));
869
870 if (qla4xxx_get_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma)) {
871 ql4_printk(KERN_ERR, ha, "%s: get ifcb failed\n", __func__);
872 rval = -EIO;
873 goto exit_init_fw_cb;
874 }
875
876 total_param_count = count;
877 iface_param = (struct iscsi_iface_param_info *)data;
878
879 for ( ; total_param_count != 0; total_param_count--) {
880 length = iface_param->len;
881
882 if (iface_param->param_type != ISCSI_NET_PARAM)
883 continue;
884
885 switch (iface_param->iface_type) {
886 case ISCSI_IFACE_TYPE_IPV4:
887 switch (iface_param->iface_num) {
888 case 0:
889 qla4xxx_set_ipv4(ha, iface_param, init_fw_cb);
890 break;
891 default:
892 /* Cannot have more than one IPv4 interface */
893 ql4_printk(KERN_ERR, ha, "Invalid IPv4 iface "
894 "number = %d\n",
895 iface_param->iface_num);
896 break;
897 }
898 break;
899 case ISCSI_IFACE_TYPE_IPV6:
900 switch (iface_param->iface_num) {
901 case 0:
902 case 1:
903 qla4xxx_set_ipv6(ha, iface_param, init_fw_cb);
904 break;
905 default:
906 /* Cannot have more than two IPv6 interface */
907 ql4_printk(KERN_ERR, ha, "Invalid IPv6 iface "
908 "number = %d\n",
909 iface_param->iface_num);
910 break;
911 }
912 break;
913 default:
914 ql4_printk(KERN_ERR, ha, "Invalid iface type\n");
915 break;
916 }
917
918 iface_param = (struct iscsi_iface_param_info *)
919 ((uint8_t *)iface_param +
920 sizeof(struct iscsi_iface_param_info) + length);
921 }
922
923 init_fw_cb->cookie = cpu_to_le32(0x11BEAD5A);
924
925 rval = qla4xxx_set_flash(ha, init_fw_cb_dma, FLASH_SEGMENT_IFCB,
926 sizeof(struct addr_ctrl_blk),
927 FLASH_OPT_RMW_COMMIT);
928 if (rval != QLA_SUCCESS) {
929 ql4_printk(KERN_ERR, ha, "%s: set flash mbx failed\n",
930 __func__);
931 rval = -EIO;
932 goto exit_init_fw_cb;
933 }
934
935 qla4xxx_disable_acb(ha);
936
937 qla4xxx_initcb_to_acb(init_fw_cb);
938
939 rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma);
940 if (rval != QLA_SUCCESS) {
941 ql4_printk(KERN_ERR, ha, "%s: set acb mbx failed\n",
942 __func__);
943 rval = -EIO;
944 goto exit_init_fw_cb;
945 }
946
947 memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
948 qla4xxx_update_local_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb,
949 init_fw_cb_dma);
950
951exit_init_fw_cb:
952 dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk),
953 init_fw_cb, init_fw_cb_dma);
954
955 return rval;
956}
957
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500958static int qla4xxx_conn_get_param(struct iscsi_cls_conn *cls_conn,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700959 enum iscsi_param param, char *buf)
960{
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500961 struct iscsi_conn *conn;
962 struct qla_conn *qla_conn;
963 struct sockaddr *dst_addr;
964 int len = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700965
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500966 conn = cls_conn->dd_data;
967 qla_conn = conn->dd_data;
968 dst_addr = &qla_conn->qla_ep->dst_addr;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700969
970 switch (param) {
971 case ISCSI_PARAM_CONN_PORT:
David Somayajuluafaf5a22006-09-19 10:28:00 -0700972 case ISCSI_PARAM_CONN_ADDRESS:
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500973 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
974 dst_addr, param, buf);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700975 default:
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500976 return iscsi_conn_get_param(cls_conn, param, buf);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700977 }
978
979 return len;
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500980
David Somayajuluafaf5a22006-09-19 10:28:00 -0700981}
982
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500983static struct iscsi_cls_session *
984qla4xxx_session_create(struct iscsi_endpoint *ep,
985 uint16_t cmds_max, uint16_t qdepth,
986 uint32_t initial_cmdsn)
987{
988 struct iscsi_cls_session *cls_sess;
989 struct scsi_qla_host *ha;
990 struct qla_endpoint *qla_ep;
991 struct ddb_entry *ddb_entry;
992 uint32_t ddb_index;
993 uint32_t mbx_sts = 0;
994 struct iscsi_session *sess;
995 struct sockaddr *dst_addr;
996 int ret;
997
998 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
999 if (!ep) {
1000 printk(KERN_ERR "qla4xxx: missing ep.\n");
1001 return NULL;
1002 }
1003
1004 qla_ep = ep->dd_data;
1005 dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
1006 ha = to_qla_host(qla_ep->host);
Manish Rangankar736cf362011-10-07 16:55:46 -07001007
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001008get_ddb_index:
1009 ddb_index = find_first_zero_bit(ha->ddb_idx_map, MAX_DDB_ENTRIES);
1010
1011 if (ddb_index >= MAX_DDB_ENTRIES) {
1012 DEBUG2(ql4_printk(KERN_INFO, ha,
1013 "Free DDB index not available\n"));
1014 return NULL;
1015 }
1016
1017 if (test_and_set_bit(ddb_index, ha->ddb_idx_map))
1018 goto get_ddb_index;
1019
1020 DEBUG2(ql4_printk(KERN_INFO, ha,
1021 "Found a free DDB index at %d\n", ddb_index));
1022 ret = qla4xxx_req_ddb_entry(ha, ddb_index, &mbx_sts);
1023 if (ret == QLA_ERROR) {
1024 if (mbx_sts == MBOX_STS_COMMAND_ERROR) {
1025 ql4_printk(KERN_INFO, ha,
1026 "DDB index = %d not available trying next\n",
1027 ddb_index);
1028 goto get_ddb_index;
1029 }
1030 DEBUG2(ql4_printk(KERN_INFO, ha,
1031 "Free FW DDB not available\n"));
1032 return NULL;
1033 }
1034
1035 cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport, qla_ep->host,
1036 cmds_max, sizeof(struct ddb_entry),
1037 sizeof(struct ql4_task_data),
1038 initial_cmdsn, ddb_index);
1039 if (!cls_sess)
1040 return NULL;
1041
1042 sess = cls_sess->dd_data;
1043 ddb_entry = sess->dd_data;
1044 ddb_entry->fw_ddb_index = ddb_index;
1045 ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
1046 ddb_entry->ha = ha;
1047 ddb_entry->sess = cls_sess;
1048 cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
1049 ha->fw_ddb_index_map[ddb_entry->fw_ddb_index] = ddb_entry;
1050 ha->tot_ddbs++;
1051
1052 return cls_sess;
1053}
1054
1055static void qla4xxx_session_destroy(struct iscsi_cls_session *cls_sess)
1056{
1057 struct iscsi_session *sess;
1058 struct ddb_entry *ddb_entry;
1059 struct scsi_qla_host *ha;
1060 unsigned long flags;
1061
1062 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1063 sess = cls_sess->dd_data;
1064 ddb_entry = sess->dd_data;
1065 ha = ddb_entry->ha;
1066
Manish Rangankar736cf362011-10-07 16:55:46 -07001067 qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
1068
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001069 spin_lock_irqsave(&ha->hardware_lock, flags);
1070 qla4xxx_free_ddb(ha, ddb_entry);
1071 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1072 iscsi_session_teardown(cls_sess);
1073}
1074
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001075static struct iscsi_cls_conn *
1076qla4xxx_conn_create(struct iscsi_cls_session *cls_sess, uint32_t conn_idx)
1077{
1078 struct iscsi_cls_conn *cls_conn;
1079 struct iscsi_session *sess;
1080 struct ddb_entry *ddb_entry;
1081
1082 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1083 cls_conn = iscsi_conn_setup(cls_sess, sizeof(struct qla_conn),
1084 conn_idx);
1085 sess = cls_sess->dd_data;
1086 ddb_entry = sess->dd_data;
1087 ddb_entry->conn = cls_conn;
1088
1089 return cls_conn;
1090}
1091
1092static int qla4xxx_conn_bind(struct iscsi_cls_session *cls_session,
1093 struct iscsi_cls_conn *cls_conn,
1094 uint64_t transport_fd, int is_leading)
1095{
1096 struct iscsi_conn *conn;
1097 struct qla_conn *qla_conn;
1098 struct iscsi_endpoint *ep;
1099
1100 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1101
1102 if (iscsi_conn_bind(cls_session, cls_conn, is_leading))
1103 return -EINVAL;
1104 ep = iscsi_lookup_endpoint(transport_fd);
1105 conn = cls_conn->dd_data;
1106 qla_conn = conn->dd_data;
1107 qla_conn->qla_ep = ep->dd_data;
1108 return 0;
1109}
1110
1111static int qla4xxx_conn_start(struct iscsi_cls_conn *cls_conn)
1112{
1113 struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
1114 struct iscsi_session *sess;
1115 struct ddb_entry *ddb_entry;
1116 struct scsi_qla_host *ha;
1117 struct dev_db_entry *fw_ddb_entry;
1118 dma_addr_t fw_ddb_entry_dma;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001119 uint32_t mbx_sts = 0;
1120 int ret = 0;
1121 int status = QLA_SUCCESS;
1122
1123 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1124 sess = cls_sess->dd_data;
1125 ddb_entry = sess->dd_data;
1126 ha = ddb_entry->ha;
1127
1128 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1129 &fw_ddb_entry_dma, GFP_KERNEL);
1130 if (!fw_ddb_entry) {
1131 ql4_printk(KERN_ERR, ha,
1132 "%s: Unable to allocate dma buffer\n", __func__);
1133 return -ENOMEM;
1134 }
1135
1136 ret = qla4xxx_set_param_ddbentry(ha, ddb_entry, cls_conn, &mbx_sts);
1137 if (ret) {
1138 /* If iscsid is stopped and started then no need to do
1139 * set param again since ddb state will be already
1140 * active and FW does not allow set ddb to an
1141 * active session.
1142 */
1143 if (mbx_sts)
1144 if (ddb_entry->fw_ddb_device_state ==
1145 DDB_DS_SESSION_ACTIVE)
1146 goto exit_set_param;
1147
1148 ql4_printk(KERN_ERR, ha, "%s: Failed set param for index[%d]\n",
1149 __func__, ddb_entry->fw_ddb_index);
1150 goto exit_conn_start;
1151 }
1152
1153 status = qla4xxx_conn_open(ha, ddb_entry->fw_ddb_index);
1154 if (status == QLA_ERROR) {
Manish Rangankar0e7e8502011-07-25 13:48:54 -05001155 ql4_printk(KERN_ERR, ha, "%s: Login failed: %s\n", __func__,
1156 sess->targetname);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001157 ret = -EINVAL;
1158 goto exit_conn_start;
1159 }
1160
1161 ddb_entry->fw_ddb_device_state = DDB_DS_LOGIN_IN_PROCESS;
1162
1163exit_set_param:
1164 iscsi_conn_start(cls_conn);
1165 ret = 0;
1166
1167exit_conn_start:
1168 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1169 fw_ddb_entry, fw_ddb_entry_dma);
1170 return ret;
1171}
1172
1173static void qla4xxx_conn_destroy(struct iscsi_cls_conn *cls_conn)
1174{
1175 struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
1176 struct iscsi_session *sess;
1177 struct scsi_qla_host *ha;
1178 struct ddb_entry *ddb_entry;
1179 int options;
1180
1181 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1182 sess = cls_sess->dd_data;
1183 ddb_entry = sess->dd_data;
1184 ha = ddb_entry->ha;
1185
1186 options = LOGOUT_OPTION_CLOSE_SESSION;
1187 if (qla4xxx_session_logout_ddb(ha, ddb_entry, options) == QLA_ERROR)
1188 ql4_printk(KERN_ERR, ha, "%s: Logout failed\n", __func__);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001189}
1190
1191static void qla4xxx_task_work(struct work_struct *wdata)
1192{
1193 struct ql4_task_data *task_data;
1194 struct scsi_qla_host *ha;
1195 struct passthru_status *sts;
1196 struct iscsi_task *task;
1197 struct iscsi_hdr *hdr;
1198 uint8_t *data;
1199 uint32_t data_len;
1200 struct iscsi_conn *conn;
1201 int hdr_len;
1202 itt_t itt;
1203
1204 task_data = container_of(wdata, struct ql4_task_data, task_work);
1205 ha = task_data->ha;
1206 task = task_data->task;
1207 sts = &task_data->sts;
1208 hdr_len = sizeof(struct iscsi_hdr);
1209
1210 DEBUG3(printk(KERN_INFO "Status returned\n"));
1211 DEBUG3(qla4xxx_dump_buffer(sts, 64));
1212 DEBUG3(printk(KERN_INFO "Response buffer"));
1213 DEBUG3(qla4xxx_dump_buffer(task_data->resp_buffer, 64));
1214
1215 conn = task->conn;
1216
1217 switch (sts->completionStatus) {
1218 case PASSTHRU_STATUS_COMPLETE:
1219 hdr = (struct iscsi_hdr *)task_data->resp_buffer;
1220 /* Assign back the itt in hdr, until we use the PREASSIGN_TAG */
1221 itt = sts->handle;
1222 hdr->itt = itt;
1223 data = task_data->resp_buffer + hdr_len;
1224 data_len = task_data->resp_len - hdr_len;
1225 iscsi_complete_pdu(conn, hdr, data, data_len);
1226 break;
1227 default:
1228 ql4_printk(KERN_ERR, ha, "Passthru failed status = 0x%x\n",
1229 sts->completionStatus);
1230 break;
1231 }
1232 return;
1233}
1234
1235static int qla4xxx_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
1236{
1237 struct ql4_task_data *task_data;
1238 struct iscsi_session *sess;
1239 struct ddb_entry *ddb_entry;
1240 struct scsi_qla_host *ha;
1241 int hdr_len;
1242
1243 sess = task->conn->session;
1244 ddb_entry = sess->dd_data;
1245 ha = ddb_entry->ha;
1246 task_data = task->dd_data;
1247 memset(task_data, 0, sizeof(struct ql4_task_data));
1248
1249 if (task->sc) {
1250 ql4_printk(KERN_INFO, ha,
1251 "%s: SCSI Commands not implemented\n", __func__);
1252 return -EINVAL;
1253 }
1254
1255 hdr_len = sizeof(struct iscsi_hdr);
1256 task_data->ha = ha;
1257 task_data->task = task;
1258
1259 if (task->data_count) {
1260 task_data->data_dma = dma_map_single(&ha->pdev->dev, task->data,
1261 task->data_count,
1262 PCI_DMA_TODEVICE);
1263 }
1264
1265 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
1266 __func__, task->conn->max_recv_dlength, hdr_len));
1267
1268 task_data->resp_len = task->conn->max_recv_dlength;
1269 task_data->resp_buffer = dma_alloc_coherent(&ha->pdev->dev,
1270 task_data->resp_len,
1271 &task_data->resp_dma,
1272 GFP_ATOMIC);
1273 if (!task_data->resp_buffer)
1274 goto exit_alloc_pdu;
1275
1276 task_data->req_buffer = dma_alloc_coherent(&ha->pdev->dev,
1277 task->data_count + hdr_len,
1278 &task_data->req_dma,
1279 GFP_ATOMIC);
1280 if (!task_data->req_buffer)
1281 goto exit_alloc_pdu;
1282
1283 task->hdr = task_data->req_buffer;
1284
1285 INIT_WORK(&task_data->task_work, qla4xxx_task_work);
1286
1287 return 0;
1288
1289exit_alloc_pdu:
1290 if (task_data->resp_buffer)
1291 dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
1292 task_data->resp_buffer, task_data->resp_dma);
1293
1294 if (task_data->req_buffer)
1295 dma_free_coherent(&ha->pdev->dev, task->data_count + hdr_len,
1296 task_data->req_buffer, task_data->req_dma);
1297 return -ENOMEM;
1298}
1299
1300static void qla4xxx_task_cleanup(struct iscsi_task *task)
1301{
1302 struct ql4_task_data *task_data;
1303 struct iscsi_session *sess;
1304 struct ddb_entry *ddb_entry;
1305 struct scsi_qla_host *ha;
1306 int hdr_len;
1307
1308 hdr_len = sizeof(struct iscsi_hdr);
1309 sess = task->conn->session;
1310 ddb_entry = sess->dd_data;
1311 ha = ddb_entry->ha;
1312 task_data = task->dd_data;
1313
1314 if (task->data_count) {
1315 dma_unmap_single(&ha->pdev->dev, task_data->data_dma,
1316 task->data_count, PCI_DMA_TODEVICE);
1317 }
1318
1319 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
1320 __func__, task->conn->max_recv_dlength, hdr_len));
1321
1322 dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
1323 task_data->resp_buffer, task_data->resp_dma);
1324 dma_free_coherent(&ha->pdev->dev, task->data_count + hdr_len,
1325 task_data->req_buffer, task_data->req_dma);
1326 return;
1327}
1328
1329static int qla4xxx_task_xmit(struct iscsi_task *task)
1330{
1331 struct scsi_cmnd *sc = task->sc;
1332 struct iscsi_session *sess = task->conn->session;
1333 struct ddb_entry *ddb_entry = sess->dd_data;
1334 struct scsi_qla_host *ha = ddb_entry->ha;
1335
1336 if (!sc)
1337 return qla4xxx_send_passthru0(task);
1338
1339 ql4_printk(KERN_INFO, ha, "%s: scsi cmd xmit not implemented\n",
1340 __func__);
1341 return -ENOSYS;
1342}
1343
1344void qla4xxx_update_session_conn_param(struct scsi_qla_host *ha,
1345 struct ddb_entry *ddb_entry)
1346{
1347 struct iscsi_cls_session *cls_sess;
1348 struct iscsi_cls_conn *cls_conn;
1349 struct iscsi_session *sess;
1350 struct iscsi_conn *conn;
1351 uint32_t ddb_state;
1352 dma_addr_t fw_ddb_entry_dma;
1353 struct dev_db_entry *fw_ddb_entry;
1354
1355 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1356 &fw_ddb_entry_dma, GFP_KERNEL);
1357 if (!fw_ddb_entry) {
1358 ql4_printk(KERN_ERR, ha,
1359 "%s: Unable to allocate dma buffer\n", __func__);
1360 return;
1361 }
1362
1363 if (qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index, fw_ddb_entry,
1364 fw_ddb_entry_dma, NULL, NULL, &ddb_state,
1365 NULL, NULL, NULL) == QLA_ERROR) {
1366 DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: failed "
1367 "get_ddb_entry for fw_ddb_index %d\n",
1368 ha->host_no, __func__,
1369 ddb_entry->fw_ddb_index));
1370 return;
1371 }
1372
1373 cls_sess = ddb_entry->sess;
1374 sess = cls_sess->dd_data;
1375
1376 cls_conn = ddb_entry->conn;
1377 conn = cls_conn->dd_data;
1378
1379 /* Update params */
1380 conn->max_recv_dlength = BYTE_UNITS *
1381 le16_to_cpu(fw_ddb_entry->iscsi_max_rcv_data_seg_len);
1382
1383 conn->max_xmit_dlength = BYTE_UNITS *
1384 le16_to_cpu(fw_ddb_entry->iscsi_max_snd_data_seg_len);
1385
1386 sess->initial_r2t_en =
1387 (BIT_10 & le16_to_cpu(fw_ddb_entry->iscsi_options));
1388
1389 sess->max_r2t = le16_to_cpu(fw_ddb_entry->iscsi_max_outsnd_r2t);
1390
1391 sess->imm_data_en = (BIT_11 & le16_to_cpu(fw_ddb_entry->iscsi_options));
1392
1393 sess->first_burst = BYTE_UNITS *
1394 le16_to_cpu(fw_ddb_entry->iscsi_first_burst_len);
1395
1396 sess->max_burst = BYTE_UNITS *
1397 le16_to_cpu(fw_ddb_entry->iscsi_max_burst_len);
1398
1399 sess->time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
1400
1401 sess->time2retain = le16_to_cpu(fw_ddb_entry->iscsi_def_time2retain);
1402
1403 sess->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
1404
1405 memcpy(sess->initiatorname, ha->name_string,
1406 min(sizeof(ha->name_string), sizeof(sess->initiatorname)));
1407}
1408
David Somayajuluafaf5a22006-09-19 10:28:00 -07001409/*
1410 * Timer routines
1411 */
1412
1413static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
1414 unsigned long interval)
1415{
1416 DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
1417 __func__, ha->host->host_no));
1418 init_timer(&ha->timer);
1419 ha->timer.expires = jiffies + interval * HZ;
1420 ha->timer.data = (unsigned long)ha;
1421 ha->timer.function = (void (*)(unsigned long))func;
1422 add_timer(&ha->timer);
1423 ha->timer_active = 1;
1424}
1425
1426static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
1427{
1428 del_timer_sync(&ha->timer);
1429 ha->timer_active = 0;
1430}
1431
1432/***
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001433 * qla4xxx_mark_device_missing - blocks the session
1434 * @cls_session: Pointer to the session to be blocked
David Somayajuluafaf5a22006-09-19 10:28:00 -07001435 * @ddb_entry: Pointer to device database entry
1436 *
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301437 * This routine marks a device missing and close connection.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001438 **/
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001439void qla4xxx_mark_device_missing(struct iscsi_cls_session *cls_session)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001440{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001441 iscsi_block_session(cls_session);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001442}
1443
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301444/**
1445 * qla4xxx_mark_all_devices_missing - mark all devices as missing.
1446 * @ha: Pointer to host adapter structure.
1447 *
1448 * This routine marks a device missing and resets the relogin retry count.
1449 **/
1450void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
1451{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001452 iscsi_host_for_each_session(ha->host, qla4xxx_mark_device_missing);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301453}
1454
David Somayajuluafaf5a22006-09-19 10:28:00 -07001455static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
1456 struct ddb_entry *ddb_entry,
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001457 struct scsi_cmnd *cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001458{
1459 struct srb *srb;
1460
1461 srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
1462 if (!srb)
1463 return srb;
1464
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301465 kref_init(&srb->srb_ref);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001466 srb->ha = ha;
1467 srb->ddb = ddb_entry;
1468 srb->cmd = cmd;
1469 srb->flags = 0;
Vikas Chaudhary53698872010-04-28 11:41:59 +05301470 CMD_SP(cmd) = (void *)srb;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001471
1472 return srb;
1473}
1474
1475static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
1476{
1477 struct scsi_cmnd *cmd = srb->cmd;
1478
1479 if (srb->flags & SRB_DMA_VALID) {
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +09001480 scsi_dma_unmap(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001481 srb->flags &= ~SRB_DMA_VALID;
1482 }
Vikas Chaudhary53698872010-04-28 11:41:59 +05301483 CMD_SP(cmd) = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001484}
1485
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301486void qla4xxx_srb_compl(struct kref *ref)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001487{
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301488 struct srb *srb = container_of(ref, struct srb, srb_ref);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001489 struct scsi_cmnd *cmd = srb->cmd;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301490 struct scsi_qla_host *ha = srb->ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001491
1492 qla4xxx_srb_free_dma(ha, srb);
1493
1494 mempool_free(srb, ha->srb_mempool);
1495
1496 cmd->scsi_done(cmd);
1497}
1498
1499/**
1500 * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001501 * @host: scsi host
David Somayajuluafaf5a22006-09-19 10:28:00 -07001502 * @cmd: Pointer to Linux's SCSI command structure
David Somayajuluafaf5a22006-09-19 10:28:00 -07001503 *
1504 * Remarks:
1505 * This routine is invoked by Linux to send a SCSI command to the driver.
1506 * The mid-level driver tries to ensure that queuecommand never gets
1507 * invoked concurrently with itself or the interrupt handler (although
1508 * the interrupt handler may call this routine as part of request-
1509 * completion handling). Unfortunely, it sometimes calls the scheduler
1510 * in interrupt context which is a big NO! NO!.
1511 **/
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001512static int qla4xxx_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001513{
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001514 struct scsi_qla_host *ha = to_qla_host(host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001515 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christie7fb19212008-01-31 13:36:45 -06001516 struct iscsi_cls_session *sess = ddb_entry->sess;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001517 struct srb *srb;
1518 int rval;
1519
Lalit Chandivade2232be02010-07-30 14:38:47 +05301520 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1521 if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
1522 cmd->result = DID_NO_CONNECT << 16;
1523 else
1524 cmd->result = DID_REQUEUE << 16;
1525 goto qc_fail_command;
1526 }
1527
Mike Christie7fb19212008-01-31 13:36:45 -06001528 if (!sess) {
1529 cmd->result = DID_IMM_RETRY << 16;
1530 goto qc_fail_command;
1531 }
1532
1533 rval = iscsi_session_chkready(sess);
1534 if (rval) {
1535 cmd->result = rval;
1536 goto qc_fail_command;
1537 }
1538
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301539 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1540 test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
1541 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1542 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
1543 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
1544 !test_bit(AF_ONLINE, &ha->flags) ||
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001545 !test_bit(AF_LINK_UP, &ha->flags) ||
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301546 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
David C Somayajulu477ffb92007-01-22 12:26:11 -08001547 goto qc_host_busy;
1548
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001549 srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001550 if (!srb)
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001551 goto qc_host_busy;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001552
1553 rval = qla4xxx_send_command_to_isp(ha, srb);
1554 if (rval != QLA_SUCCESS)
1555 goto qc_host_busy_free_sp;
1556
David Somayajuluafaf5a22006-09-19 10:28:00 -07001557 return 0;
1558
1559qc_host_busy_free_sp:
1560 qla4xxx_srb_free_dma(ha, srb);
1561 mempool_free(srb, ha->srb_mempool);
1562
David Somayajuluafaf5a22006-09-19 10:28:00 -07001563qc_host_busy:
1564 return SCSI_MLQUEUE_HOST_BUSY;
1565
1566qc_fail_command:
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001567 cmd->scsi_done(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001568
1569 return 0;
1570}
1571
1572/**
1573 * qla4xxx_mem_free - frees memory allocated to adapter
1574 * @ha: Pointer to host adapter structure.
1575 *
1576 * Frees memory previously allocated by qla4xxx_mem_alloc
1577 **/
1578static void qla4xxx_mem_free(struct scsi_qla_host *ha)
1579{
1580 if (ha->queues)
1581 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
1582 ha->queues_dma);
1583
1584 ha->queues_len = 0;
1585 ha->queues = NULL;
1586 ha->queues_dma = 0;
1587 ha->request_ring = NULL;
1588 ha->request_dma = 0;
1589 ha->response_ring = NULL;
1590 ha->response_dma = 0;
1591 ha->shadow_regs = NULL;
1592 ha->shadow_regs_dma = 0;
1593
1594 /* Free srb pool. */
1595 if (ha->srb_mempool)
1596 mempool_destroy(ha->srb_mempool);
1597
1598 ha->srb_mempool = NULL;
1599
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001600 if (ha->chap_dma_pool)
1601 dma_pool_destroy(ha->chap_dma_pool);
1602
Lalit Chandivade45494152011-10-07 16:55:42 -07001603 if (ha->chap_list)
1604 vfree(ha->chap_list);
1605 ha->chap_list = NULL;
1606
David Somayajuluafaf5a22006-09-19 10:28:00 -07001607 /* release io space registers */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301608 if (is_qla8022(ha)) {
1609 if (ha->nx_pcibase)
1610 iounmap(
1611 (struct device_reg_82xx __iomem *)ha->nx_pcibase);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301612 } else if (ha->reg)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001613 iounmap(ha->reg);
1614 pci_release_regions(ha->pdev);
1615}
1616
1617/**
1618 * qla4xxx_mem_alloc - allocates memory for use by adapter.
1619 * @ha: Pointer to host adapter structure
1620 *
1621 * Allocates DMA memory for request and response queues. Also allocates memory
1622 * for srbs.
1623 **/
1624static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
1625{
1626 unsigned long align;
1627
1628 /* Allocate contiguous block of DMA memory for queues. */
1629 ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
1630 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
1631 sizeof(struct shadow_regs) +
1632 MEM_ALIGN_VALUE +
1633 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
1634 ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
1635 &ha->queues_dma, GFP_KERNEL);
1636 if (ha->queues == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301637 ql4_printk(KERN_WARNING, ha,
1638 "Memory Allocation failed - queues.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001639
1640 goto mem_alloc_error_exit;
1641 }
1642 memset(ha->queues, 0, ha->queues_len);
1643
1644 /*
1645 * As per RISC alignment requirements -- the bus-address must be a
1646 * multiple of the request-ring size (in bytes).
1647 */
1648 align = 0;
1649 if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
1650 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
1651 (MEM_ALIGN_VALUE - 1));
1652
1653 /* Update request and response queue pointers. */
1654 ha->request_dma = ha->queues_dma + align;
1655 ha->request_ring = (struct queue_entry *) (ha->queues + align);
1656 ha->response_dma = ha->queues_dma + align +
1657 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
1658 ha->response_ring = (struct queue_entry *) (ha->queues + align +
1659 (REQUEST_QUEUE_DEPTH *
1660 QUEUE_SIZE));
1661 ha->shadow_regs_dma = ha->queues_dma + align +
1662 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
1663 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
1664 ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
1665 (REQUEST_QUEUE_DEPTH *
1666 QUEUE_SIZE) +
1667 (RESPONSE_QUEUE_DEPTH *
1668 QUEUE_SIZE));
1669
1670 /* Allocate memory for srb pool. */
1671 ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
1672 mempool_free_slab, srb_cachep);
1673 if (ha->srb_mempool == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301674 ql4_printk(KERN_WARNING, ha,
1675 "Memory Allocation failed - SRB Pool.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001676
1677 goto mem_alloc_error_exit;
1678 }
1679
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001680 ha->chap_dma_pool = dma_pool_create("ql4_chap", &ha->pdev->dev,
1681 CHAP_DMA_BLOCK_SIZE, 8, 0);
1682
1683 if (ha->chap_dma_pool == NULL) {
1684 ql4_printk(KERN_WARNING, ha,
1685 "%s: chap_dma_pool allocation failed..\n", __func__);
1686 goto mem_alloc_error_exit;
1687 }
1688
David Somayajuluafaf5a22006-09-19 10:28:00 -07001689 return QLA_SUCCESS;
1690
1691mem_alloc_error_exit:
1692 qla4xxx_mem_free(ha);
1693 return QLA_ERROR;
1694}
1695
1696/**
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301697 * qla4_8xxx_check_fw_alive - Check firmware health
1698 * @ha: Pointer to host adapter structure.
1699 *
1700 * Context: Interrupt
1701 **/
1702static void qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
1703{
1704 uint32_t fw_heartbeat_counter, halt_status;
1705
1706 fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
Lalit Chandivade2232be02010-07-30 14:38:47 +05301707 /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
1708 if (fw_heartbeat_counter == 0xffffffff) {
1709 DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
1710 "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
1711 ha->host_no, __func__));
1712 return;
1713 }
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301714
1715 if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
1716 ha->seconds_since_last_heartbeat++;
1717 /* FW not alive after 2 seconds */
1718 if (ha->seconds_since_last_heartbeat == 2) {
1719 ha->seconds_since_last_heartbeat = 0;
1720 halt_status = qla4_8xxx_rd_32(ha,
Vikas Chaudhary68d92eb2011-05-17 23:17:05 -07001721 QLA82XX_PEG_HALT_STATUS1);
1722
1723 ql4_printk(KERN_INFO, ha,
1724 "scsi(%ld): %s, Dumping hw/fw registers:\n "
1725 " PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2:"
1726 " 0x%x,\n PEG_NET_0_PC: 0x%x, PEG_NET_1_PC:"
1727 " 0x%x,\n PEG_NET_2_PC: 0x%x, PEG_NET_3_PC:"
1728 " 0x%x,\n PEG_NET_4_PC: 0x%x\n",
1729 ha->host_no, __func__, halt_status,
1730 qla4_8xxx_rd_32(ha,
1731 QLA82XX_PEG_HALT_STATUS2),
1732 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_0 +
1733 0x3c),
1734 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_1 +
1735 0x3c),
1736 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_2 +
1737 0x3c),
1738 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_3 +
1739 0x3c),
1740 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_4 +
1741 0x3c));
Nilesh Javali21033632010-07-30 14:28:07 +05301742
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301743 /* Since we cannot change dev_state in interrupt
1744 * context, set appropriate DPC flag then wakeup
1745 * DPC */
1746 if (halt_status & HALT_STATUS_UNRECOVERABLE)
1747 set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
1748 else {
1749 printk("scsi%ld: %s: detect abort needed!\n",
1750 ha->host_no, __func__);
1751 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1752 }
1753 qla4xxx_wake_dpc(ha);
Nilesh Javali21033632010-07-30 14:28:07 +05301754 qla4xxx_mailbox_premature_completion(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301755 }
Lalit Chandivade99457d72010-10-06 22:49:32 -07001756 } else
1757 ha->seconds_since_last_heartbeat = 0;
1758
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301759 ha->fw_heartbeat_counter = fw_heartbeat_counter;
1760}
1761
1762/**
1763 * qla4_8xxx_watchdog - Poll dev state
1764 * @ha: Pointer to host adapter structure.
1765 *
1766 * Context: Interrupt
1767 **/
1768void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
1769{
1770 uint32_t dev_state;
1771
1772 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
1773
1774 /* don't poll if reset is going on */
Lalit Chandivaded56a1f72010-12-02 22:12:45 -08001775 if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
1776 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
Vikas Chaudhary977f46a2011-05-17 23:17:08 -07001777 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags))) {
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301778 if (dev_state == QLA82XX_DEV_NEED_RESET &&
1779 !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
Vikas Chaudhary3930b8c2010-12-02 22:12:47 -08001780 if (!ql4xdontresethba) {
1781 ql4_printk(KERN_INFO, ha, "%s: HW State: "
1782 "NEED RESET!\n", __func__);
1783 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1784 qla4xxx_wake_dpc(ha);
1785 qla4xxx_mailbox_premature_completion(ha);
1786 }
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301787 } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
1788 !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
Vikas Chaudhary3930b8c2010-12-02 22:12:47 -08001789 ql4_printk(KERN_INFO, ha, "%s: HW State: NEED QUIES!\n",
1790 __func__);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301791 set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
1792 qla4xxx_wake_dpc(ha);
1793 } else {
1794 /* Check firmware health */
1795 qla4_8xxx_check_fw_alive(ha);
1796 }
1797 }
1798}
1799
1800/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07001801 * qla4xxx_timer - checks every second for work to do.
1802 * @ha: Pointer to host adapter structure.
1803 **/
1804static void qla4xxx_timer(struct scsi_qla_host *ha)
1805{
David Somayajuluafaf5a22006-09-19 10:28:00 -07001806 int start_dpc = 0;
Lalit Chandivade2232be02010-07-30 14:38:47 +05301807 uint16_t w;
1808
1809 /* If we are in the middle of AER/EEH processing
1810 * skip any processing and reschedule the timer
1811 */
1812 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1813 mod_timer(&ha->timer, jiffies + HZ);
1814 return;
1815 }
1816
1817 /* Hardware read to trigger an EEH error during mailbox waits. */
1818 if (!pci_channel_offline(ha->pdev))
1819 pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001820
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301821 if (is_qla8022(ha)) {
1822 qla4_8xxx_watchdog(ha);
1823 }
1824
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301825 if (!is_qla8022(ha)) {
1826 /* Check for heartbeat interval. */
1827 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
1828 ha->heartbeat_interval != 0) {
1829 ha->seconds_since_last_heartbeat++;
1830 if (ha->seconds_since_last_heartbeat >
1831 ha->heartbeat_interval + 2)
1832 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1833 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001834 }
1835
David Somayajuluafaf5a22006-09-19 10:28:00 -07001836 /* Wakeup the dpc routine for this adapter, if needed. */
Lalit Chandivade1b468072011-05-17 23:17:09 -07001837 if (start_dpc ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07001838 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1839 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
1840 test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301841 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07001842 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1843 test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
Vikas Chaudhary065aa1b42010-04-28 11:38:11 +05301844 test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301845 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
1846 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
Lalit Chandivade1b468072011-05-17 23:17:09 -07001847 test_bit(DPC_AEN, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001848 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
1849 " - dpc flags = 0x%lx\n",
1850 ha->host_no, __func__, ha->dpc_flags));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301851 qla4xxx_wake_dpc(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001852 }
1853
1854 /* Reschedule timer thread to call us back in one second */
1855 mod_timer(&ha->timer, jiffies + HZ);
1856
1857 DEBUG2(ha->seconds_since_last_intr++);
1858}
1859
1860/**
1861 * qla4xxx_cmd_wait - waits for all outstanding commands to complete
1862 * @ha: Pointer to host adapter structure.
1863 *
1864 * This routine stalls the driver until all outstanding commands are returned.
1865 * Caller must release the Hardware Lock prior to calling this routine.
1866 **/
1867static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
1868{
1869 uint32_t index = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001870 unsigned long flags;
1871 struct scsi_cmnd *cmd;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001872
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301873 unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
1874
1875 DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
1876 "complete\n", WAIT_CMD_TOV));
1877
1878 while (!time_after_eq(jiffies, wtime)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001879 spin_lock_irqsave(&ha->hardware_lock, flags);
1880 /* Find a command that hasn't completed. */
1881 for (index = 0; index < ha->host->can_queue; index++) {
1882 cmd = scsi_host_find_tag(ha->host, index);
Mike Christiea1e00632010-10-26 05:45:30 -07001883 /*
1884 * We cannot just check if the index is valid,
1885 * becase if we are run from the scsi eh, then
1886 * the scsi/block layer is going to prevent
1887 * the tag from being released.
1888 */
1889 if (cmd != NULL && CMD_SP(cmd))
David Somayajuluafaf5a22006-09-19 10:28:00 -07001890 break;
1891 }
1892 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1893
1894 /* If No Commands are pending, wait is complete */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301895 if (index == ha->host->can_queue)
1896 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001897
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301898 msleep(1000);
1899 }
1900 /* If we timed out on waiting for commands to come back
1901 * return ERROR. */
1902 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001903}
1904
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301905int qla4xxx_hw_reset(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001906{
David Somayajuluafaf5a22006-09-19 10:28:00 -07001907 uint32_t ctrl_status;
David C Somayajulu477ffb92007-01-22 12:26:11 -08001908 unsigned long flags = 0;
1909
1910 DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001911
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301912 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
1913 return QLA_ERROR;
1914
David Somayajuluafaf5a22006-09-19 10:28:00 -07001915 spin_lock_irqsave(&ha->hardware_lock, flags);
1916
1917 /*
1918 * If the SCSI Reset Interrupt bit is set, clear it.
1919 * Otherwise, the Soft Reset won't work.
1920 */
1921 ctrl_status = readw(&ha->reg->ctrl_status);
1922 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
1923 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
1924
1925 /* Issue Soft Reset */
1926 writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
1927 readl(&ha->reg->ctrl_status);
1928
1929 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301930 return QLA_SUCCESS;
David C Somayajulu477ffb92007-01-22 12:26:11 -08001931}
1932
1933/**
1934 * qla4xxx_soft_reset - performs soft reset.
1935 * @ha: Pointer to host adapter structure.
1936 **/
1937int qla4xxx_soft_reset(struct scsi_qla_host *ha)
1938{
1939 uint32_t max_wait_time;
1940 unsigned long flags = 0;
Vikas Chaudharyf931c532010-10-06 22:48:07 -07001941 int status;
David C Somayajulu477ffb92007-01-22 12:26:11 -08001942 uint32_t ctrl_status;
1943
Vikas Chaudharyf931c532010-10-06 22:48:07 -07001944 status = qla4xxx_hw_reset(ha);
1945 if (status != QLA_SUCCESS)
1946 return status;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001947
Vikas Chaudharyf931c532010-10-06 22:48:07 -07001948 status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001949 /* Wait until the Network Reset Intr bit is cleared */
1950 max_wait_time = RESET_INTR_TOV;
1951 do {
1952 spin_lock_irqsave(&ha->hardware_lock, flags);
1953 ctrl_status = readw(&ha->reg->ctrl_status);
1954 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1955
1956 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
1957 break;
1958
1959 msleep(1000);
1960 } while ((--max_wait_time));
1961
1962 if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
1963 DEBUG2(printk(KERN_WARNING
1964 "scsi%ld: Network Reset Intr not cleared by "
1965 "Network function, clearing it now!\n",
1966 ha->host_no));
1967 spin_lock_irqsave(&ha->hardware_lock, flags);
1968 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
1969 readl(&ha->reg->ctrl_status);
1970 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1971 }
1972
1973 /* Wait until the firmware tells us the Soft Reset is done */
1974 max_wait_time = SOFT_RESET_TOV;
1975 do {
1976 spin_lock_irqsave(&ha->hardware_lock, flags);
1977 ctrl_status = readw(&ha->reg->ctrl_status);
1978 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1979
1980 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
1981 status = QLA_SUCCESS;
1982 break;
1983 }
1984
1985 msleep(1000);
1986 } while ((--max_wait_time));
1987
1988 /*
1989 * Also, make sure that the SCSI Reset Interrupt bit has been cleared
1990 * after the soft reset has taken place.
1991 */
1992 spin_lock_irqsave(&ha->hardware_lock, flags);
1993 ctrl_status = readw(&ha->reg->ctrl_status);
1994 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
1995 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
1996 readl(&ha->reg->ctrl_status);
1997 }
1998 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1999
2000 /* If soft reset fails then most probably the bios on other
2001 * function is also enabled.
2002 * Since the initialization is sequential the other fn
2003 * wont be able to acknowledge the soft reset.
2004 * Issue a force soft reset to workaround this scenario.
2005 */
2006 if (max_wait_time == 0) {
2007 /* Issue Force Soft Reset */
2008 spin_lock_irqsave(&ha->hardware_lock, flags);
2009 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
2010 readl(&ha->reg->ctrl_status);
2011 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2012 /* Wait until the firmware tells us the Soft Reset is done */
2013 max_wait_time = SOFT_RESET_TOV;
2014 do {
2015 spin_lock_irqsave(&ha->hardware_lock, flags);
2016 ctrl_status = readw(&ha->reg->ctrl_status);
2017 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2018
2019 if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
2020 status = QLA_SUCCESS;
2021 break;
2022 }
2023
2024 msleep(1000);
2025 } while ((--max_wait_time));
2026 }
2027
2028 return status;
2029}
2030
2031/**
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302032 * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
David Somayajuluafaf5a22006-09-19 10:28:00 -07002033 * @ha: Pointer to host adapter structure.
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302034 * @res: returned scsi status
David Somayajuluafaf5a22006-09-19 10:28:00 -07002035 *
2036 * This routine is called just prior to a HARD RESET to return all
2037 * outstanding commands back to the Operating System.
2038 * Caller should make sure that the following locks are released
2039 * before this calling routine: Hardware lock, and io_request_lock.
2040 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302041static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002042{
2043 struct srb *srb;
2044 int i;
2045 unsigned long flags;
2046
2047 spin_lock_irqsave(&ha->hardware_lock, flags);
2048 for (i = 0; i < ha->host->can_queue; i++) {
2049 srb = qla4xxx_del_from_active_array(ha, i);
2050 if (srb != NULL) {
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302051 srb->cmd->result = res;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302052 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002053 }
2054 }
2055 spin_unlock_irqrestore(&ha->hardware_lock, flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002056}
2057
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302058void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
2059{
2060 clear_bit(AF_ONLINE, &ha->flags);
2061
2062 /* Disable the board */
2063 ql4_printk(KERN_INFO, ha, "Disabling the board\n");
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302064
2065 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
2066 qla4xxx_mark_all_devices_missing(ha);
2067 clear_bit(AF_INIT_DONE, &ha->flags);
2068}
2069
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002070static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session)
2071{
2072 struct iscsi_session *sess;
2073 struct ddb_entry *ddb_entry;
2074
2075 sess = cls_session->dd_data;
2076 ddb_entry = sess->dd_data;
2077 ddb_entry->fw_ddb_device_state = DDB_DS_SESSION_FAILED;
2078 iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_CONN_FAILED);
2079}
2080
David Somayajuluafaf5a22006-09-19 10:28:00 -07002081/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002082 * qla4xxx_recover_adapter - recovers adapter after a fatal error
2083 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07002084 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302085static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002086{
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302087 int status = QLA_ERROR;
2088 uint8_t reset_chip = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002089
2090 /* Stall incoming I/O until we are done */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302091 scsi_block_requests(ha->host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002092 clear_bit(AF_ONLINE, &ha->flags);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002093 clear_bit(AF_LINK_UP, &ha->flags);
Mike Christie50a29ae2008-03-04 13:26:53 -06002094
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302095 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -07002096
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302097 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002098
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002099 iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
2100
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302101 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
2102 reset_chip = 1;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002103
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302104 /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
2105 * do not reset adapter, jump to initialize_adapter */
2106 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
2107 status = QLA_SUCCESS;
2108 goto recover_ha_init_adapter;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002109 }
2110
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302111 /* For the ISP-82xx adapter, issue a stop_firmware if invoked
2112 * from eh_host_reset or ioctl module */
2113 if (is_qla8022(ha) && !reset_chip &&
2114 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
2115
2116 DEBUG2(ql4_printk(KERN_INFO, ha,
2117 "scsi%ld: %s - Performing stop_firmware...\n",
2118 ha->host_no, __func__));
2119 status = ha->isp_ops->reset_firmware(ha);
2120 if (status == QLA_SUCCESS) {
Nilesh Javali2bd1e2b2010-10-06 22:49:20 -07002121 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
2122 qla4xxx_cmd_wait(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302123 ha->isp_ops->disable_intrs(ha);
2124 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2125 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
2126 } else {
2127 /* If the stop_firmware fails then
2128 * reset the entire chip */
2129 reset_chip = 1;
2130 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2131 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2132 }
2133 }
2134
2135 /* Issue full chip reset if recovering from a catastrophic error,
2136 * or if stop_firmware fails for ISP-82xx.
2137 * This is the default case for ISP-4xxx */
2138 if (!is_qla8022(ha) || reset_chip) {
Nilesh Javali2bd1e2b2010-10-06 22:49:20 -07002139 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
2140 qla4xxx_cmd_wait(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302141 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2142 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
2143 DEBUG2(ql4_printk(KERN_INFO, ha,
2144 "scsi%ld: %s - Performing chip reset..\n",
2145 ha->host_no, __func__));
2146 status = ha->isp_ops->reset_chip(ha);
2147 }
2148
2149 /* Flush any pending ddb changed AENs */
2150 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2151
2152recover_ha_init_adapter:
2153 /* Upon successful firmware/chip reset, re-initialize the adapter */
2154 if (status == QLA_SUCCESS) {
2155 /* For ISP-4xxx, force function 1 to always initialize
2156 * before function 3 to prevent both funcions from
2157 * stepping on top of the other */
2158 if (!is_qla8022(ha) && (ha->mac_index == 3))
2159 ssleep(6);
2160
2161 /* NOTE: AF_ONLINE flag set upon successful completion of
2162 * qla4xxx_initialize_adapter */
Manish Rangankar0e7e8502011-07-25 13:48:54 -05002163 status = qla4xxx_initialize_adapter(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302164 }
2165
2166 /* Retry failed adapter initialization, if necessary
2167 * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
2168 * case to prevent ping-pong resets between functions */
2169 if (!test_bit(AF_ONLINE, &ha->flags) &&
2170 !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002171 /* Adapter initialization failed, see if we can retry
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302172 * resetting the ha.
2173 * Since we don't want to block the DPC for too long
2174 * with multiple resets in the same thread,
2175 * utilize DPC to retry */
David Somayajuluafaf5a22006-09-19 10:28:00 -07002176 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
2177 ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
2178 DEBUG2(printk("scsi%ld: recover adapter - retrying "
2179 "(%d) more times\n", ha->host_no,
2180 ha->retry_reset_ha_cnt));
2181 set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
2182 status = QLA_ERROR;
2183 } else {
2184 if (ha->retry_reset_ha_cnt > 0) {
2185 /* Schedule another Reset HA--DPC will retry */
2186 ha->retry_reset_ha_cnt--;
2187 DEBUG2(printk("scsi%ld: recover adapter - "
2188 "retry remaining %d\n",
2189 ha->host_no,
2190 ha->retry_reset_ha_cnt));
2191 status = QLA_ERROR;
2192 }
2193
2194 if (ha->retry_reset_ha_cnt == 0) {
2195 /* Recover adapter retries have been exhausted.
2196 * Adapter DEAD */
2197 DEBUG2(printk("scsi%ld: recover adapter "
2198 "failed - board disabled\n",
2199 ha->host_no));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302200 qla4xxx_dead_adapter_cleanup(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002201 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
2202 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302203 clear_bit(DPC_RESET_HA_FW_CONTEXT,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002204 &ha->dpc_flags);
2205 status = QLA_ERROR;
2206 }
2207 }
2208 } else {
2209 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302210 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002211 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
2212 }
2213
2214 ha->adapter_error_count++;
2215
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302216 if (test_bit(AF_ONLINE, &ha->flags))
2217 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002218
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302219 scsi_unblock_requests(ha->host);
2220
2221 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2222 DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002223 status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302224
David Somayajuluafaf5a22006-09-19 10:28:00 -07002225 return status;
2226}
2227
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002228static void qla4xxx_relogin_devices(struct iscsi_cls_session *cls_session)
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07002229{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002230 struct iscsi_session *sess;
2231 struct ddb_entry *ddb_entry;
2232 struct scsi_qla_host *ha;
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07002233
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002234 sess = cls_session->dd_data;
2235 ddb_entry = sess->dd_data;
2236 ha = ddb_entry->ha;
2237 if (!iscsi_is_session_online(cls_session)) {
2238 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
2239 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
2240 " unblock session\n", ha->host_no, __func__,
2241 ddb_entry->fw_ddb_index);
2242 iscsi_unblock_session(ddb_entry->sess);
2243 } else {
2244 /* Trigger relogin */
2245 iscsi_session_failure(cls_session->dd_data,
2246 ISCSI_ERR_CONN_FAILED);
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07002247 }
2248 }
2249}
2250
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002251static void qla4xxx_relogin_all_devices(struct scsi_qla_host *ha)
2252{
2253 iscsi_host_for_each_session(ha->host, qla4xxx_relogin_devices);
2254}
2255
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302256void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
2257{
Lalit Chandivade1b468072011-05-17 23:17:09 -07002258 if (ha->dpc_thread)
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302259 queue_work(ha->dpc_thread, &ha->dpc_work);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302260}
2261
David Somayajuluafaf5a22006-09-19 10:28:00 -07002262/**
2263 * qla4xxx_do_dpc - dpc routine
2264 * @data: in our case pointer to adapter structure
2265 *
2266 * This routine is a task that is schedule by the interrupt handler
2267 * to perform the background processing for interrupts. We put it
2268 * on a task queue that is consumed whenever the scheduler runs; that's
2269 * so you can do anything (i.e. put the process to sleep etc). In fact,
2270 * the mid-level tries to sleep when it reaches the driver threshold
2271 * "host->can_queue". This can cause a panic if we were in our interrupt code.
2272 **/
David Howellsc4028952006-11-22 14:57:56 +00002273static void qla4xxx_do_dpc(struct work_struct *work)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002274{
David Howellsc4028952006-11-22 14:57:56 +00002275 struct scsi_qla_host *ha =
2276 container_of(work, struct scsi_qla_host, dpc_work);
David C Somayajulu477ffb92007-01-22 12:26:11 -08002277 int status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002278
David C Somayajuluf26b9042006-11-15 16:41:09 -08002279 DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302280 "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
2281 ha->host_no, __func__, ha->flags, ha->dpc_flags))
David Somayajuluafaf5a22006-09-19 10:28:00 -07002282
2283 /* Initialization not yet finished. Don't do anything yet. */
2284 if (!test_bit(AF_INIT_DONE, &ha->flags))
Lalit Chandivade1b468072011-05-17 23:17:09 -07002285 return;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002286
Lalit Chandivade2232be02010-07-30 14:38:47 +05302287 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
2288 DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
2289 ha->host_no, __func__, ha->flags));
Lalit Chandivade1b468072011-05-17 23:17:09 -07002290 return;
Lalit Chandivade2232be02010-07-30 14:38:47 +05302291 }
2292
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302293 if (is_qla8022(ha)) {
2294 if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
2295 qla4_8xxx_idc_lock(ha);
2296 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2297 QLA82XX_DEV_FAILED);
2298 qla4_8xxx_idc_unlock(ha);
2299 ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
2300 qla4_8xxx_device_state_handler(ha);
2301 }
2302 if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
2303 qla4_8xxx_need_qsnt_handler(ha);
2304 }
2305 }
2306
2307 if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
2308 (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07002309 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302310 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
2311 if (ql4xdontresethba) {
2312 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
2313 ha->host_no, __func__));
2314 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
2315 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
2316 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2317 goto dpc_post_reset_ha;
2318 }
2319 if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
2320 test_bit(DPC_RESET_HA, &ha->dpc_flags))
2321 qla4xxx_recover_adapter(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002322
David C Somayajulu477ffb92007-01-22 12:26:11 -08002323 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002324 uint8_t wait_time = RESET_INTR_TOV;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002325
David Somayajuluafaf5a22006-09-19 10:28:00 -07002326 while ((readw(&ha->reg->ctrl_status) &
2327 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
2328 if (--wait_time == 0)
2329 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002330 msleep(1000);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002331 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002332 if (wait_time == 0)
2333 DEBUG2(printk("scsi%ld: %s: SR|FSR "
2334 "bit not cleared-- resetting\n",
2335 ha->host_no, __func__));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302336 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
David C Somayajulu477ffb92007-01-22 12:26:11 -08002337 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
2338 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302339 status = qla4xxx_recover_adapter(ha);
David C Somayajulu477ffb92007-01-22 12:26:11 -08002340 }
2341 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
2342 if (status == QLA_SUCCESS)
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302343 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002344 }
2345 }
2346
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302347dpc_post_reset_ha:
David Somayajuluafaf5a22006-09-19 10:28:00 -07002348 /* ---- process AEN? --- */
2349 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
2350 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
2351
2352 /* ---- Get DHCP IP Address? --- */
2353 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
2354 qla4xxx_get_dhcp_ip_address(ha);
2355
Vikas Chaudhary065aa1b42010-04-28 11:38:11 +05302356 /* ---- link change? --- */
2357 if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
2358 if (!test_bit(AF_LINK_UP, &ha->flags)) {
2359 /* ---- link down? --- */
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07002360 qla4xxx_mark_all_devices_missing(ha);
Vikas Chaudhary065aa1b42010-04-28 11:38:11 +05302361 } else {
2362 /* ---- link up? --- *
2363 * F/W will auto login to all devices ONLY ONCE after
2364 * link up during driver initialization and runtime
2365 * fatal error recovery. Therefore, the driver must
2366 * manually relogin to devices when recovering from
2367 * connection failures, logouts, expired KATO, etc. */
2368
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07002369 qla4xxx_relogin_all_devices(ha);
Vikas Chaudhary065aa1b42010-04-28 11:38:11 +05302370 }
2371 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002372}
2373
2374/**
2375 * qla4xxx_free_adapter - release the adapter
2376 * @ha: pointer to adapter structure
2377 **/
2378static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
2379{
2380
2381 if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
2382 /* Turn-off interrupts on the card. */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302383 ha->isp_ops->disable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002384 }
2385
David Somayajuluafaf5a22006-09-19 10:28:00 -07002386 /* Remove timer thread, if present */
2387 if (ha->timer_active)
2388 qla4xxx_stop_timer(ha);
2389
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302390 /* Kill the kernel thread for this host */
2391 if (ha->dpc_thread)
2392 destroy_workqueue(ha->dpc_thread);
2393
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002394 /* Kill the kernel thread for this host */
2395 if (ha->task_wq)
2396 destroy_workqueue(ha->task_wq);
2397
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302398 /* Put firmware in known state */
2399 ha->isp_ops->reset_firmware(ha);
2400
2401 if (is_qla8022(ha)) {
2402 qla4_8xxx_idc_lock(ha);
2403 qla4_8xxx_clear_drv_active(ha);
2404 qla4_8xxx_idc_unlock(ha);
2405 }
2406
David Somayajuluafaf5a22006-09-19 10:28:00 -07002407 /* Detach interrupts */
2408 if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302409 qla4xxx_free_irqs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002410
David C Somayajulubee4fe82007-05-23 18:03:32 -07002411 /* free extra memory */
2412 qla4xxx_mem_free(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302413}
David C Somayajulubee4fe82007-05-23 18:03:32 -07002414
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302415int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
2416{
2417 int status = 0;
2418 uint8_t revision_id;
2419 unsigned long mem_base, mem_len, db_base, db_len;
2420 struct pci_dev *pdev = ha->pdev;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002421
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302422 status = pci_request_regions(pdev, DRIVER_NAME);
2423 if (status) {
2424 printk(KERN_WARNING
2425 "scsi(%ld) Failed to reserve PIO regions (%s) "
2426 "status=%d\n", ha->host_no, pci_name(pdev), status);
2427 goto iospace_error_exit;
2428 }
2429
2430 pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
2431 DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
2432 __func__, revision_id));
2433 ha->revision_id = revision_id;
2434
2435 /* remap phys address */
2436 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
2437 mem_len = pci_resource_len(pdev, 0);
2438 DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
2439 __func__, mem_base, mem_len));
2440
2441 /* mapping of pcibase pointer */
2442 ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
2443 if (!ha->nx_pcibase) {
2444 printk(KERN_ERR
2445 "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
2446 pci_release_regions(ha->pdev);
2447 goto iospace_error_exit;
2448 }
2449
2450 /* Mapping of IO base pointer, door bell read and write pointer */
2451
2452 /* mapping of IO base pointer */
2453 ha->qla4_8xxx_reg =
2454 (struct device_reg_82xx __iomem *)((uint8_t *)ha->nx_pcibase +
2455 0xbc000 + (ha->pdev->devfn << 11));
2456
2457 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
2458 db_len = pci_resource_len(pdev, 4);
2459
Shyam Sundar2657c802010-10-06 22:50:29 -07002460 ha->nx_db_wr_ptr = (ha->pdev->devfn == 4 ? QLA82XX_CAM_RAM_DB1 :
2461 QLA82XX_CAM_RAM_DB2);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302462
Shyam Sundar2657c802010-10-06 22:50:29 -07002463 return 0;
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302464iospace_error_exit:
2465 return -ENOMEM;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002466}
2467
2468/***
2469 * qla4xxx_iospace_config - maps registers
2470 * @ha: pointer to adapter structure
2471 *
2472 * This routines maps HBA's registers from the pci address space
2473 * into the kernel virtual address space for memory mapped i/o.
2474 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302475int qla4xxx_iospace_config(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002476{
2477 unsigned long pio, pio_len, pio_flags;
2478 unsigned long mmio, mmio_len, mmio_flags;
2479
2480 pio = pci_resource_start(ha->pdev, 0);
2481 pio_len = pci_resource_len(ha->pdev, 0);
2482 pio_flags = pci_resource_flags(ha->pdev, 0);
2483 if (pio_flags & IORESOURCE_IO) {
2484 if (pio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302485 ql4_printk(KERN_WARNING, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002486 "Invalid PCI I/O region size\n");
2487 pio = 0;
2488 }
2489 } else {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302490 ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002491 pio = 0;
2492 }
2493
2494 /* Use MMIO operations for all accesses. */
2495 mmio = pci_resource_start(ha->pdev, 1);
2496 mmio_len = pci_resource_len(ha->pdev, 1);
2497 mmio_flags = pci_resource_flags(ha->pdev, 1);
2498
2499 if (!(mmio_flags & IORESOURCE_MEM)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302500 ql4_printk(KERN_ERR, ha,
2501 "region #0 not an MMIO resource, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002502
2503 goto iospace_error_exit;
2504 }
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302505
David Somayajuluafaf5a22006-09-19 10:28:00 -07002506 if (mmio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302507 ql4_printk(KERN_ERR, ha,
2508 "Invalid PCI mem region size, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002509 goto iospace_error_exit;
2510 }
2511
2512 if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302513 ql4_printk(KERN_WARNING, ha,
2514 "Failed to reserve PIO/MMIO regions\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002515
2516 goto iospace_error_exit;
2517 }
2518
2519 ha->pio_address = pio;
2520 ha->pio_length = pio_len;
2521 ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
2522 if (!ha->reg) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302523 ql4_printk(KERN_ERR, ha,
2524 "cannot remap MMIO, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002525
2526 goto iospace_error_exit;
2527 }
2528
2529 return 0;
2530
2531iospace_error_exit:
2532 return -ENOMEM;
2533}
2534
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302535static struct isp_operations qla4xxx_isp_ops = {
2536 .iospace_config = qla4xxx_iospace_config,
2537 .pci_config = qla4xxx_pci_config,
2538 .disable_intrs = qla4xxx_disable_intrs,
2539 .enable_intrs = qla4xxx_enable_intrs,
2540 .start_firmware = qla4xxx_start_firmware,
2541 .intr_handler = qla4xxx_intr_handler,
2542 .interrupt_service_routine = qla4xxx_interrupt_service_routine,
2543 .reset_chip = qla4xxx_soft_reset,
2544 .reset_firmware = qla4xxx_hw_reset,
2545 .queue_iocb = qla4xxx_queue_iocb,
2546 .complete_iocb = qla4xxx_complete_iocb,
2547 .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
2548 .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
2549 .get_sys_info = qla4xxx_get_sys_info,
2550};
2551
2552static struct isp_operations qla4_8xxx_isp_ops = {
2553 .iospace_config = qla4_8xxx_iospace_config,
2554 .pci_config = qla4_8xxx_pci_config,
2555 .disable_intrs = qla4_8xxx_disable_intrs,
2556 .enable_intrs = qla4_8xxx_enable_intrs,
2557 .start_firmware = qla4_8xxx_load_risc,
2558 .intr_handler = qla4_8xxx_intr_handler,
2559 .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
2560 .reset_chip = qla4_8xxx_isp_reset,
2561 .reset_firmware = qla4_8xxx_stop_firmware,
2562 .queue_iocb = qla4_8xxx_queue_iocb,
2563 .complete_iocb = qla4_8xxx_complete_iocb,
2564 .rd_shdw_req_q_out = qla4_8xxx_rd_shdw_req_q_out,
2565 .rd_shdw_rsp_q_in = qla4_8xxx_rd_shdw_rsp_q_in,
2566 .get_sys_info = qla4_8xxx_get_sys_info,
2567};
2568
2569uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
2570{
2571 return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
2572}
2573
2574uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
2575{
2576 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
2577}
2578
2579uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
2580{
2581 return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
2582}
2583
2584uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
2585{
2586 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
2587}
2588
Manish Rangankar2a991c22011-07-25 13:48:55 -05002589static ssize_t qla4xxx_show_boot_eth_info(void *data, int type, char *buf)
2590{
2591 struct scsi_qla_host *ha = data;
2592 char *str = buf;
2593 int rc;
2594
2595 switch (type) {
2596 case ISCSI_BOOT_ETH_FLAGS:
2597 rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
2598 break;
2599 case ISCSI_BOOT_ETH_INDEX:
2600 rc = sprintf(str, "0\n");
2601 break;
2602 case ISCSI_BOOT_ETH_MAC:
2603 rc = sysfs_format_mac(str, ha->my_mac,
2604 MAC_ADDR_LEN);
2605 break;
2606 default:
2607 rc = -ENOSYS;
2608 break;
2609 }
2610 return rc;
2611}
2612
2613static mode_t qla4xxx_eth_get_attr_visibility(void *data, int type)
2614{
2615 int rc;
2616
2617 switch (type) {
2618 case ISCSI_BOOT_ETH_FLAGS:
2619 case ISCSI_BOOT_ETH_MAC:
2620 case ISCSI_BOOT_ETH_INDEX:
2621 rc = S_IRUGO;
2622 break;
2623 default:
2624 rc = 0;
2625 break;
2626 }
2627 return rc;
2628}
2629
2630static ssize_t qla4xxx_show_boot_ini_info(void *data, int type, char *buf)
2631{
2632 struct scsi_qla_host *ha = data;
2633 char *str = buf;
2634 int rc;
2635
2636 switch (type) {
2637 case ISCSI_BOOT_INI_INITIATOR_NAME:
2638 rc = sprintf(str, "%s\n", ha->name_string);
2639 break;
2640 default:
2641 rc = -ENOSYS;
2642 break;
2643 }
2644 return rc;
2645}
2646
2647static mode_t qla4xxx_ini_get_attr_visibility(void *data, int type)
2648{
2649 int rc;
2650
2651 switch (type) {
2652 case ISCSI_BOOT_INI_INITIATOR_NAME:
2653 rc = S_IRUGO;
2654 break;
2655 default:
2656 rc = 0;
2657 break;
2658 }
2659 return rc;
2660}
2661
2662static ssize_t
2663qla4xxx_show_boot_tgt_info(struct ql4_boot_session_info *boot_sess, int type,
2664 char *buf)
2665{
2666 struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
2667 char *str = buf;
2668 int rc;
2669
2670 switch (type) {
2671 case ISCSI_BOOT_TGT_NAME:
2672 rc = sprintf(buf, "%s\n", (char *)&boot_sess->target_name);
2673 break;
2674 case ISCSI_BOOT_TGT_IP_ADDR:
2675 if (boot_sess->conn_list[0].dest_ipaddr.ip_type == 0x1)
2676 rc = sprintf(buf, "%pI4\n",
2677 &boot_conn->dest_ipaddr.ip_address);
2678 else
2679 rc = sprintf(str, "%pI6\n",
2680 &boot_conn->dest_ipaddr.ip_address);
2681 break;
2682 case ISCSI_BOOT_TGT_PORT:
2683 rc = sprintf(str, "%d\n", boot_conn->dest_port);
2684 break;
2685 case ISCSI_BOOT_TGT_CHAP_NAME:
2686 rc = sprintf(str, "%.*s\n",
2687 boot_conn->chap.target_chap_name_length,
2688 (char *)&boot_conn->chap.target_chap_name);
2689 break;
2690 case ISCSI_BOOT_TGT_CHAP_SECRET:
2691 rc = sprintf(str, "%.*s\n",
2692 boot_conn->chap.target_secret_length,
2693 (char *)&boot_conn->chap.target_secret);
2694 break;
2695 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
2696 rc = sprintf(str, "%.*s\n",
2697 boot_conn->chap.intr_chap_name_length,
2698 (char *)&boot_conn->chap.intr_chap_name);
2699 break;
2700 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
2701 rc = sprintf(str, "%.*s\n",
2702 boot_conn->chap.intr_secret_length,
2703 (char *)&boot_conn->chap.intr_secret);
2704 break;
2705 case ISCSI_BOOT_TGT_FLAGS:
2706 rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
2707 break;
2708 case ISCSI_BOOT_TGT_NIC_ASSOC:
2709 rc = sprintf(str, "0\n");
2710 break;
2711 default:
2712 rc = -ENOSYS;
2713 break;
2714 }
2715 return rc;
2716}
2717
2718static ssize_t qla4xxx_show_boot_tgt_pri_info(void *data, int type, char *buf)
2719{
2720 struct scsi_qla_host *ha = data;
2721 struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_pri_sess);
2722
2723 return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
2724}
2725
2726static ssize_t qla4xxx_show_boot_tgt_sec_info(void *data, int type, char *buf)
2727{
2728 struct scsi_qla_host *ha = data;
2729 struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_sec_sess);
2730
2731 return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
2732}
2733
2734static mode_t qla4xxx_tgt_get_attr_visibility(void *data, int type)
2735{
2736 int rc;
2737
2738 switch (type) {
2739 case ISCSI_BOOT_TGT_NAME:
2740 case ISCSI_BOOT_TGT_IP_ADDR:
2741 case ISCSI_BOOT_TGT_PORT:
2742 case ISCSI_BOOT_TGT_CHAP_NAME:
2743 case ISCSI_BOOT_TGT_CHAP_SECRET:
2744 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
2745 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
2746 case ISCSI_BOOT_TGT_NIC_ASSOC:
2747 case ISCSI_BOOT_TGT_FLAGS:
2748 rc = S_IRUGO;
2749 break;
2750 default:
2751 rc = 0;
2752 break;
2753 }
2754 return rc;
2755}
2756
2757static void qla4xxx_boot_release(void *data)
2758{
2759 struct scsi_qla_host *ha = data;
2760
2761 scsi_host_put(ha->host);
2762}
2763
2764static int get_fw_boot_info(struct scsi_qla_host *ha, uint16_t ddb_index[])
2765{
2766 dma_addr_t buf_dma;
2767 uint32_t addr, pri_addr, sec_addr;
2768 uint32_t offset;
2769 uint16_t func_num;
2770 uint8_t val;
2771 uint8_t *buf = NULL;
2772 size_t size = 13 * sizeof(uint8_t);
2773 int ret = QLA_SUCCESS;
2774
2775 func_num = PCI_FUNC(ha->pdev->devfn);
2776
2777 DEBUG2(ql4_printk(KERN_INFO, ha,
2778 "%s: Get FW boot info for 0x%x func %d\n", __func__,
2779 (is_qla4032(ha) ? PCI_DEVICE_ID_QLOGIC_ISP4032 :
2780 PCI_DEVICE_ID_QLOGIC_ISP8022), func_num));
2781
2782 if (is_qla4032(ha)) {
2783 if (func_num == 1) {
2784 addr = NVRAM_PORT0_BOOT_MODE;
2785 pri_addr = NVRAM_PORT0_BOOT_PRI_TGT;
2786 sec_addr = NVRAM_PORT0_BOOT_SEC_TGT;
2787 } else if (func_num == 3) {
2788 addr = NVRAM_PORT1_BOOT_MODE;
2789 pri_addr = NVRAM_PORT1_BOOT_PRI_TGT;
2790 sec_addr = NVRAM_PORT1_BOOT_SEC_TGT;
2791 } else {
2792 ret = QLA_ERROR;
2793 goto exit_boot_info;
2794 }
2795
2796 /* Check Boot Mode */
2797 val = rd_nvram_byte(ha, addr);
2798 if (!(val & 0x07)) {
2799 DEBUG2(ql4_printk(KERN_ERR, ha,
2800 "%s: Failed Boot options : 0x%x\n",
2801 __func__, val));
2802 ret = QLA_ERROR;
2803 goto exit_boot_info;
2804 }
2805
2806 /* get primary valid target index */
2807 val = rd_nvram_byte(ha, pri_addr);
2808 if (val & BIT_7)
2809 ddb_index[0] = (val & 0x7f);
Manish Rangankar2a991c22011-07-25 13:48:55 -05002810
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);
Manish Rangankar2a991c22011-07-25 13:48:55 -05002815
2816 } else if (is_qla8022(ha)) {
2817 buf = dma_alloc_coherent(&ha->pdev->dev, size,
2818 &buf_dma, GFP_KERNEL);
2819 if (!buf) {
2820 DEBUG2(ql4_printk(KERN_ERR, ha,
2821 "%s: Unable to allocate dma buffer\n",
2822 __func__));
2823 ret = QLA_ERROR;
2824 goto exit_boot_info;
2825 }
2826
2827 if (ha->port_num == 0)
2828 offset = BOOT_PARAM_OFFSET_PORT0;
2829 else if (ha->port_num == 1)
2830 offset = BOOT_PARAM_OFFSET_PORT1;
2831 else {
2832 ret = QLA_ERROR;
2833 goto exit_boot_info_free;
2834 }
2835 addr = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_iscsi_param * 4) +
2836 offset;
2837 if (qla4xxx_get_flash(ha, buf_dma, addr,
2838 13 * sizeof(uint8_t)) != QLA_SUCCESS) {
2839 DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: Get Flash"
2840 "failed\n", ha->host_no, __func__));
2841 ret = QLA_ERROR;
2842 goto exit_boot_info_free;
2843 }
2844 /* Check Boot Mode */
2845 if (!(buf[1] & 0x07)) {
2846 DEBUG2(ql4_printk(KERN_INFO, ha,
2847 "Failed: Boot options : 0x%x\n",
2848 buf[1]));
2849 ret = QLA_ERROR;
2850 goto exit_boot_info_free;
2851 }
2852
2853 /* get primary valid target index */
2854 if (buf[2] & BIT_7)
2855 ddb_index[0] = buf[2] & 0x7f;
Manish Rangankar2a991c22011-07-25 13:48:55 -05002856
2857 /* get secondary valid target index */
2858 if (buf[11] & BIT_7)
2859 ddb_index[1] = buf[11] & 0x7f;
Manish Rangankar2a991c22011-07-25 13:48:55 -05002860 } else {
2861 ret = QLA_ERROR;
2862 goto exit_boot_info;
2863 }
2864
2865 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Primary target ID %d, Secondary"
2866 " target ID %d\n", __func__, ddb_index[0],
2867 ddb_index[1]));
2868
2869exit_boot_info_free:
2870 dma_free_coherent(&ha->pdev->dev, size, buf, buf_dma);
2871exit_boot_info:
2872 return ret;
2873}
2874
Lalit Chandivade28deb452011-10-07 16:55:44 -07002875/**
2876 * qla4xxx_get_bidi_chap - Get a BIDI CHAP user and password
2877 * @ha: pointer to adapter structure
2878 * @username: CHAP username to be returned
2879 * @password: CHAP password to be returned
2880 *
2881 * If a boot entry has BIDI CHAP enabled then we need to set the BIDI CHAP
2882 * user and password in the sysfs entry in /sys/firmware/iscsi_boot#/.
2883 * So from the CHAP cache find the first BIDI CHAP entry and set it
2884 * to the boot record in sysfs.
2885 **/
2886static int qla4xxx_get_bidi_chap(struct scsi_qla_host *ha, char *username,
2887 char *password)
2888{
2889 int i, ret = -EINVAL;
2890 int max_chap_entries = 0;
2891 struct ql4_chap_table *chap_table;
2892
2893 if (is_qla8022(ha))
2894 max_chap_entries = (ha->hw.flt_chap_size / 2) /
2895 sizeof(struct ql4_chap_table);
2896 else
2897 max_chap_entries = MAX_CHAP_ENTRIES_40XX;
2898
2899 if (!ha->chap_list) {
2900 ql4_printk(KERN_ERR, ha, "Do not have CHAP table cache\n");
2901 return ret;
2902 }
2903
2904 mutex_lock(&ha->chap_sem);
2905 for (i = 0; i < max_chap_entries; i++) {
2906 chap_table = (struct ql4_chap_table *)ha->chap_list + i;
2907 if (chap_table->cookie !=
2908 __constant_cpu_to_le16(CHAP_VALID_COOKIE)) {
2909 continue;
2910 }
2911
2912 if (chap_table->flags & BIT_7) /* local */
2913 continue;
2914
2915 if (!(chap_table->flags & BIT_6)) /* Not BIDI */
2916 continue;
2917
2918 strncpy(password, chap_table->secret, QL4_CHAP_MAX_SECRET_LEN);
2919 strncpy(username, chap_table->name, QL4_CHAP_MAX_NAME_LEN);
2920 ret = 0;
2921 break;
2922 }
2923 mutex_unlock(&ha->chap_sem);
2924
2925 return ret;
2926}
2927
2928
Manish Rangankar2a991c22011-07-25 13:48:55 -05002929static int qla4xxx_get_boot_target(struct scsi_qla_host *ha,
2930 struct ql4_boot_session_info *boot_sess,
2931 uint16_t ddb_index)
2932{
2933 struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
2934 struct dev_db_entry *fw_ddb_entry;
2935 dma_addr_t fw_ddb_entry_dma;
2936 uint16_t idx;
2937 uint16_t options;
2938 int ret = QLA_SUCCESS;
2939
2940 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
2941 &fw_ddb_entry_dma, GFP_KERNEL);
2942 if (!fw_ddb_entry) {
2943 DEBUG2(ql4_printk(KERN_ERR, ha,
2944 "%s: Unable to allocate dma buffer.\n",
2945 __func__));
2946 ret = QLA_ERROR;
2947 return ret;
2948 }
2949
2950 if (qla4xxx_bootdb_by_index(ha, fw_ddb_entry,
2951 fw_ddb_entry_dma, ddb_index)) {
2952 DEBUG2(ql4_printk(KERN_ERR, ha,
2953 "%s: Flash DDB read Failed\n", __func__));
2954 ret = QLA_ERROR;
2955 goto exit_boot_target;
2956 }
2957
2958 /* Update target name and IP from DDB */
2959 memcpy(boot_sess->target_name, fw_ddb_entry->iscsi_name,
2960 min(sizeof(boot_sess->target_name),
2961 sizeof(fw_ddb_entry->iscsi_name)));
2962
2963 options = le16_to_cpu(fw_ddb_entry->options);
2964 if (options & DDB_OPT_IPV6_DEVICE) {
2965 memcpy(&boot_conn->dest_ipaddr.ip_address,
2966 &fw_ddb_entry->ip_addr[0], IPv6_ADDR_LEN);
2967 } else {
2968 boot_conn->dest_ipaddr.ip_type = 0x1;
2969 memcpy(&boot_conn->dest_ipaddr.ip_address,
2970 &fw_ddb_entry->ip_addr[0], IP_ADDR_LEN);
2971 }
2972
2973 boot_conn->dest_port = le16_to_cpu(fw_ddb_entry->port);
2974
2975 /* update chap information */
2976 idx = __le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
2977
2978 if (BIT_7 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
2979
2980 DEBUG2(ql4_printk(KERN_INFO, ha, "Setting chap\n"));
2981
2982 ret = qla4xxx_get_chap(ha, (char *)&boot_conn->chap.
2983 target_chap_name,
2984 (char *)&boot_conn->chap.target_secret,
2985 idx);
2986 if (ret) {
2987 ql4_printk(KERN_ERR, ha, "Failed to set chap\n");
2988 ret = QLA_ERROR;
2989 goto exit_boot_target;
2990 }
2991
2992 boot_conn->chap.target_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
2993 boot_conn->chap.target_secret_length = QL4_CHAP_MAX_SECRET_LEN;
2994 }
2995
2996 if (BIT_4 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
2997
2998 DEBUG2(ql4_printk(KERN_INFO, ha, "Setting BIDI chap\n"));
2999
Lalit Chandivade28deb452011-10-07 16:55:44 -07003000 ret = qla4xxx_get_bidi_chap(ha,
3001 (char *)&boot_conn->chap.intr_chap_name,
3002 (char *)&boot_conn->chap.intr_secret);
3003
Manish Rangankar2a991c22011-07-25 13:48:55 -05003004 if (ret) {
3005 ql4_printk(KERN_ERR, ha, "Failed to set BIDI chap\n");
3006 ret = QLA_ERROR;
3007 goto exit_boot_target;
3008 }
3009
3010 boot_conn->chap.intr_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
3011 boot_conn->chap.intr_secret_length = QL4_CHAP_MAX_SECRET_LEN;
3012 }
3013
3014exit_boot_target:
3015 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3016 fw_ddb_entry, fw_ddb_entry_dma);
3017 return ret;
3018}
3019
3020static int qla4xxx_get_boot_info(struct scsi_qla_host *ha)
3021{
3022 uint16_t ddb_index[2];
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003023 int ret = QLA_ERROR;
3024 int rval;
Manish Rangankar2a991c22011-07-25 13:48:55 -05003025
3026 memset(ddb_index, 0, sizeof(ddb_index));
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003027 ddb_index[0] = 0xffff;
3028 ddb_index[1] = 0xffff;
Manish Rangankar2a991c22011-07-25 13:48:55 -05003029 ret = get_fw_boot_info(ha, ddb_index);
3030 if (ret != QLA_SUCCESS) {
3031 DEBUG2(ql4_printk(KERN_ERR, ha,
3032 "%s: Failed to set boot info.\n", __func__));
3033 return ret;
3034 }
3035
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003036 if (ddb_index[0] == 0xffff)
3037 goto sec_target;
3038
3039 rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_pri_sess),
Manish Rangankar2a991c22011-07-25 13:48:55 -05003040 ddb_index[0]);
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003041 if (rval != QLA_SUCCESS) {
Manish Rangankar2a991c22011-07-25 13:48:55 -05003042 DEBUG2(ql4_printk(KERN_ERR, ha, "%s: Failed to get "
3043 "primary target\n", __func__));
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003044 } else
3045 ret = QLA_SUCCESS;
Manish Rangankar2a991c22011-07-25 13:48:55 -05003046
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003047sec_target:
3048 if (ddb_index[1] == 0xffff)
3049 goto exit_get_boot_info;
3050
3051 rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_sec_sess),
Manish Rangankar2a991c22011-07-25 13:48:55 -05003052 ddb_index[1]);
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003053 if (rval != QLA_SUCCESS) {
Manish Rangankar2a991c22011-07-25 13:48:55 -05003054 DEBUG2(ql4_printk(KERN_ERR, ha, "%s: Failed to get "
3055 "secondary target\n", __func__));
Lalit Chandivade8de5b952011-10-07 16:55:43 -07003056 } else
3057 ret = QLA_SUCCESS;
3058
3059exit_get_boot_info:
Manish Rangankar2a991c22011-07-25 13:48:55 -05003060 return ret;
3061}
3062
3063static int qla4xxx_setup_boot_info(struct scsi_qla_host *ha)
3064{
3065 struct iscsi_boot_kobj *boot_kobj;
3066
3067 if (qla4xxx_get_boot_info(ha) != QLA_SUCCESS)
3068 return 0;
3069
3070 ha->boot_kset = iscsi_boot_create_host_kset(ha->host->host_no);
3071 if (!ha->boot_kset)
3072 goto kset_free;
3073
3074 if (!scsi_host_get(ha->host))
3075 goto kset_free;
3076 boot_kobj = iscsi_boot_create_target(ha->boot_kset, 0, ha,
3077 qla4xxx_show_boot_tgt_pri_info,
3078 qla4xxx_tgt_get_attr_visibility,
3079 qla4xxx_boot_release);
3080 if (!boot_kobj)
3081 goto put_host;
3082
3083 if (!scsi_host_get(ha->host))
3084 goto kset_free;
3085 boot_kobj = iscsi_boot_create_target(ha->boot_kset, 1, ha,
3086 qla4xxx_show_boot_tgt_sec_info,
3087 qla4xxx_tgt_get_attr_visibility,
3088 qla4xxx_boot_release);
3089 if (!boot_kobj)
3090 goto put_host;
3091
3092 if (!scsi_host_get(ha->host))
3093 goto kset_free;
3094 boot_kobj = iscsi_boot_create_initiator(ha->boot_kset, 0, ha,
3095 qla4xxx_show_boot_ini_info,
3096 qla4xxx_ini_get_attr_visibility,
3097 qla4xxx_boot_release);
3098 if (!boot_kobj)
3099 goto put_host;
3100
3101 if (!scsi_host_get(ha->host))
3102 goto kset_free;
3103 boot_kobj = iscsi_boot_create_ethernet(ha->boot_kset, 0, ha,
3104 qla4xxx_show_boot_eth_info,
3105 qla4xxx_eth_get_attr_visibility,
3106 qla4xxx_boot_release);
3107 if (!boot_kobj)
3108 goto put_host;
3109
3110 return 0;
3111
3112put_host:
3113 scsi_host_put(ha->host);
3114kset_free:
3115 iscsi_boot_destroy_kset(ha->boot_kset);
3116 return -ENOMEM;
3117}
3118
Lalit Chandivade45494152011-10-07 16:55:42 -07003119
3120/**
3121 * qla4xxx_create chap_list - Create CHAP list from FLASH
3122 * @ha: pointer to adapter structure
3123 *
3124 * Read flash and make a list of CHAP entries, during login when a CHAP entry
3125 * is received, it will be checked in this list. If entry exist then the CHAP
3126 * entry index is set in the DDB. If CHAP entry does not exist in this list
3127 * then a new entry is added in FLASH in CHAP table and the index obtained is
3128 * used in the DDB.
3129 **/
3130static void qla4xxx_create_chap_list(struct scsi_qla_host *ha)
3131{
3132 int rval = 0;
3133 uint8_t *chap_flash_data = NULL;
3134 uint32_t offset;
3135 dma_addr_t chap_dma;
3136 uint32_t chap_size = 0;
3137
3138 if (is_qla40XX(ha))
3139 chap_size = MAX_CHAP_ENTRIES_40XX *
3140 sizeof(struct ql4_chap_table);
3141 else /* Single region contains CHAP info for both
3142 * ports which is divided into half for each port.
3143 */
3144 chap_size = ha->hw.flt_chap_size / 2;
3145
3146 chap_flash_data = dma_alloc_coherent(&ha->pdev->dev, chap_size,
3147 &chap_dma, GFP_KERNEL);
3148 if (!chap_flash_data) {
3149 ql4_printk(KERN_ERR, ha, "No memory for chap_flash_data\n");
3150 return;
3151 }
3152 if (is_qla40XX(ha))
3153 offset = FLASH_CHAP_OFFSET;
3154 else {
3155 offset = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_region_chap << 2);
3156 if (ha->port_num == 1)
3157 offset += chap_size;
3158 }
3159
3160 rval = qla4xxx_get_flash(ha, chap_dma, offset, chap_size);
3161 if (rval != QLA_SUCCESS)
3162 goto exit_chap_list;
3163
3164 if (ha->chap_list == NULL)
3165 ha->chap_list = vmalloc(chap_size);
3166 if (ha->chap_list == NULL) {
3167 ql4_printk(KERN_ERR, ha, "No memory for ha->chap_list\n");
3168 goto exit_chap_list;
3169 }
3170
3171 memcpy(ha->chap_list, chap_flash_data, chap_size);
3172
3173exit_chap_list:
3174 dma_free_coherent(&ha->pdev->dev, chap_size,
3175 chap_flash_data, chap_dma);
3176 return;
3177}
3178
David Somayajuluafaf5a22006-09-19 10:28:00 -07003179/**
3180 * qla4xxx_probe_adapter - callback function to probe HBA
3181 * @pdev: pointer to pci_dev structure
3182 * @pci_device_id: pointer to pci_device entry
3183 *
3184 * This routine will probe for Qlogic 4xxx iSCSI host adapters.
3185 * It returns zero if successful. It also initializes all data necessary for
3186 * the driver.
3187 **/
3188static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
3189 const struct pci_device_id *ent)
3190{
3191 int ret = -ENODEV, status;
3192 struct Scsi_Host *host;
3193 struct scsi_qla_host *ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003194 uint8_t init_retry_count = 0;
3195 char buf[34];
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303196 struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
Prasanna Mumbaif9880e72011-03-21 03:34:26 -07003197 uint32_t dev_state;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003198
3199 if (pci_enable_device(pdev))
3200 return -1;
3201
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003202 host = iscsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha), 0);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003203 if (host == NULL) {
3204 printk(KERN_WARNING
3205 "qla4xxx: Couldn't allocate host from scsi layer!\n");
3206 goto probe_disable_device;
3207 }
3208
3209 /* Clear our data area */
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003210 ha = to_qla_host(host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003211 memset(ha, 0, sizeof(*ha));
3212
3213 /* Save the information from PCI BIOS. */
3214 ha->pdev = pdev;
3215 ha->host = host;
3216 ha->host_no = host->host_no;
3217
Lalit Chandivade2232be02010-07-30 14:38:47 +05303218 pci_enable_pcie_error_reporting(pdev);
3219
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303220 /* Setup Runtime configurable options */
3221 if (is_qla8022(ha)) {
3222 ha->isp_ops = &qla4_8xxx_isp_ops;
3223 rwlock_init(&ha->hw_lock);
3224 ha->qdr_sn_window = -1;
3225 ha->ddr_mn_window = -1;
3226 ha->curr_window = 255;
3227 ha->func_num = PCI_FUNC(ha->pdev->devfn);
3228 nx_legacy_intr = &legacy_intr[ha->func_num];
3229 ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
3230 ha->nx_legacy_intr.tgt_status_reg =
3231 nx_legacy_intr->tgt_status_reg;
3232 ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
3233 ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
3234 } else {
3235 ha->isp_ops = &qla4xxx_isp_ops;
3236 }
3237
Lalit Chandivade2232be02010-07-30 14:38:47 +05303238 /* Set EEH reset type to fundamental if required by hba */
3239 if (is_qla8022(ha))
3240 pdev->needs_freset = 1;
3241
David Somayajuluafaf5a22006-09-19 10:28:00 -07003242 /* Configure PCI I/O space. */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303243 ret = ha->isp_ops->iospace_config(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003244 if (ret)
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303245 goto probe_failed_ioconfig;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003246
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303247 ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
David Somayajuluafaf5a22006-09-19 10:28:00 -07003248 pdev->device, pdev->irq, ha->reg);
3249
3250 qla4xxx_config_dma_addressing(ha);
3251
3252 /* Initialize lists and spinlocks. */
David Somayajuluafaf5a22006-09-19 10:28:00 -07003253 INIT_LIST_HEAD(&ha->free_srb_q);
3254
3255 mutex_init(&ha->mbox_sem);
Lalit Chandivade45494152011-10-07 16:55:42 -07003256 mutex_init(&ha->chap_sem);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303257 init_completion(&ha->mbx_intr_comp);
Vikas Chaudhary95d31262011-08-12 02:51:29 -07003258 init_completion(&ha->disable_acb_comp);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003259
3260 spin_lock_init(&ha->hardware_lock);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003261
3262 /* Allocate dma buffers */
3263 if (qla4xxx_mem_alloc(ha)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303264 ql4_printk(KERN_WARNING, ha,
3265 "[ERROR] Failed to allocate memory for adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07003266
3267 ret = -ENOMEM;
3268 goto probe_failed;
3269 }
3270
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003271 host->cmd_per_lun = 3;
3272 host->max_channel = 0;
3273 host->max_lun = MAX_LUNS - 1;
3274 host->max_id = MAX_TARGETS;
3275 host->max_cmd_len = IOCB_MAX_CDB_LEN;
3276 host->can_queue = MAX_SRBS ;
3277 host->transportt = qla4xxx_scsi_transport;
3278
3279 ret = scsi_init_shared_tag_map(host, MAX_SRBS);
3280 if (ret) {
3281 ql4_printk(KERN_WARNING, ha,
3282 "%s: scsi_init_shared_tag_map failed\n", __func__);
3283 goto probe_failed;
3284 }
3285
3286 pci_set_drvdata(pdev, ha);
3287
3288 ret = scsi_add_host(host, &pdev->dev);
3289 if (ret)
3290 goto probe_failed;
3291
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303292 if (is_qla8022(ha))
3293 (void) qla4_8xxx_get_flash_info(ha);
3294
David Somayajuluafaf5a22006-09-19 10:28:00 -07003295 /*
3296 * Initialize the Host adapter request/response queues and
3297 * firmware
3298 * NOTE: interrupts enabled upon successful completion
3299 */
Manish Rangankar0e7e8502011-07-25 13:48:54 -05003300 status = qla4xxx_initialize_adapter(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303301 while ((!test_bit(AF_ONLINE, &ha->flags)) &&
3302 init_retry_count++ < MAX_INIT_RETRIES) {
Prasanna Mumbaif9880e72011-03-21 03:34:26 -07003303
3304 if (is_qla8022(ha)) {
3305 qla4_8xxx_idc_lock(ha);
3306 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
3307 qla4_8xxx_idc_unlock(ha);
3308 if (dev_state == QLA82XX_DEV_FAILED) {
3309 ql4_printk(KERN_WARNING, ha, "%s: don't retry "
3310 "initialize adapter. H/W is in failed state\n",
3311 __func__);
3312 break;
3313 }
3314 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07003315 DEBUG2(printk("scsi: %s: retrying adapter initialization "
3316 "(%d)\n", __func__, init_retry_count));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303317
3318 if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
3319 continue;
3320
Manish Rangankar0e7e8502011-07-25 13:48:54 -05003321 status = qla4xxx_initialize_adapter(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003322 }
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303323
3324 if (!test_bit(AF_ONLINE, &ha->flags)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303325 ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07003326
Lalit Chandivadefe998522010-12-02 22:12:36 -08003327 if (is_qla8022(ha) && ql4xdontresethba) {
3328 /* Put the device in failed state. */
3329 DEBUG2(printk(KERN_ERR "HW STATE: FAILED\n"));
3330 qla4_8xxx_idc_lock(ha);
3331 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
3332 QLA82XX_DEV_FAILED);
3333 qla4_8xxx_idc_unlock(ha);
3334 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07003335 ret = -ENODEV;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003336 goto remove_host;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003337 }
3338
David Somayajuluafaf5a22006-09-19 10:28:00 -07003339 /* Startup the kernel thread for this host adapter. */
3340 DEBUG2(printk("scsi: %s: Starting kernel thread for "
3341 "qla4xxx_dpc\n", __func__));
3342 sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
3343 ha->dpc_thread = create_singlethread_workqueue(buf);
3344 if (!ha->dpc_thread) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303345 ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07003346 ret = -ENODEV;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003347 goto remove_host;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003348 }
David Howellsc4028952006-11-22 14:57:56 +00003349 INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003350
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003351 sprintf(buf, "qla4xxx_%lu_task", ha->host_no);
3352 ha->task_wq = alloc_workqueue(buf, WQ_MEM_RECLAIM, 1);
3353 if (!ha->task_wq) {
3354 ql4_printk(KERN_WARNING, ha, "Unable to start task thread!\n");
3355 ret = -ENODEV;
3356 goto remove_host;
3357 }
3358
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303359 /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
3360 * (which is called indirectly by qla4xxx_initialize_adapter),
3361 * so that irqs will be registered after crbinit but before
3362 * mbx_intr_enable.
3363 */
3364 if (!is_qla8022(ha)) {
3365 ret = qla4xxx_request_irqs(ha);
3366 if (ret) {
3367 ql4_printk(KERN_WARNING, ha, "Failed to reserve "
3368 "interrupt %d already in use.\n", pdev->irq);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003369 goto remove_host;
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303370 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07003371 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07003372
Lalit Chandivade2232be02010-07-30 14:38:47 +05303373 pci_save_state(ha->pdev);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303374 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003375
3376 /* Start timer thread. */
3377 qla4xxx_start_timer(ha, qla4xxx_timer, 1);
3378
3379 set_bit(AF_INIT_DONE, &ha->flags);
3380
David Somayajuluafaf5a22006-09-19 10:28:00 -07003381 printk(KERN_INFO
3382 " QLogic iSCSI HBA Driver version: %s\n"
3383 " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
3384 qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
3385 ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
3386 ha->patch_number, ha->build_number);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05003387
Lalit Chandivade45494152011-10-07 16:55:42 -07003388 qla4xxx_create_chap_list(ha);
3389
Manish Rangankar2a991c22011-07-25 13:48:55 -05003390 if (qla4xxx_setup_boot_info(ha))
3391 ql4_printk(KERN_ERR, ha, "%s:ISCSI boot info setup failed\n",
3392 __func__);
3393
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05003394 qla4xxx_create_ifaces(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003395 return 0;
3396
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003397remove_host:
3398 scsi_remove_host(ha->host);
3399
David Somayajuluafaf5a22006-09-19 10:28:00 -07003400probe_failed:
3401 qla4xxx_free_adapter(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303402
3403probe_failed_ioconfig:
Lalit Chandivade2232be02010-07-30 14:38:47 +05303404 pci_disable_pcie_error_reporting(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003405 scsi_host_put(ha->host);
3406
3407probe_disable_device:
3408 pci_disable_device(pdev);
3409
3410 return ret;
3411}
3412
3413/**
Karen Higgins7eece5a2011-03-21 03:34:29 -07003414 * qla4xxx_prevent_other_port_reinit - prevent other port from re-initialize
3415 * @ha: pointer to adapter structure
3416 *
3417 * Mark the other ISP-4xxx port to indicate that the driver is being removed,
3418 * so that the other port will not re-initialize while in the process of
3419 * removing the ha due to driver unload or hba hotplug.
3420 **/
3421static void qla4xxx_prevent_other_port_reinit(struct scsi_qla_host *ha)
3422{
3423 struct scsi_qla_host *other_ha = NULL;
3424 struct pci_dev *other_pdev = NULL;
3425 int fn = ISP4XXX_PCI_FN_2;
3426
3427 /*iscsi function numbers for ISP4xxx is 1 and 3*/
3428 if (PCI_FUNC(ha->pdev->devfn) & BIT_1)
3429 fn = ISP4XXX_PCI_FN_1;
3430
3431 other_pdev =
3432 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
3433 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
3434 fn));
3435
3436 /* Get other_ha if other_pdev is valid and state is enable*/
3437 if (other_pdev) {
3438 if (atomic_read(&other_pdev->enable_cnt)) {
3439 other_ha = pci_get_drvdata(other_pdev);
3440 if (other_ha) {
3441 set_bit(AF_HA_REMOVAL, &other_ha->flags);
3442 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: "
3443 "Prevent %s reinit\n", __func__,
3444 dev_name(&other_ha->pdev->dev)));
3445 }
3446 }
3447 pci_dev_put(other_pdev);
3448 }
3449}
3450
3451/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07003452 * qla4xxx_remove_adapter - calback function to remove adapter.
3453 * @pci_dev: PCI device pointer
3454 **/
3455static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
3456{
3457 struct scsi_qla_host *ha;
3458
3459 ha = pci_get_drvdata(pdev);
3460
Karen Higgins7eece5a2011-03-21 03:34:29 -07003461 if (!is_qla8022(ha))
3462 qla4xxx_prevent_other_port_reinit(ha);
David C Somayajulubee4fe82007-05-23 18:03:32 -07003463
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05003464 /* destroy iface from sysfs */
3465 qla4xxx_destroy_ifaces(ha);
3466
Manish Rangankar2a991c22011-07-25 13:48:55 -05003467 if (ha->boot_kset)
3468 iscsi_boot_destroy_kset(ha->boot_kset);
3469
David Somayajuluafaf5a22006-09-19 10:28:00 -07003470 scsi_remove_host(ha->host);
3471
3472 qla4xxx_free_adapter(ha);
3473
3474 scsi_host_put(ha->host);
3475
Lalit Chandivade2232be02010-07-30 14:38:47 +05303476 pci_disable_pcie_error_reporting(pdev);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303477 pci_disable_device(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003478 pci_set_drvdata(pdev, NULL);
3479}
3480
3481/**
3482 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
3483 * @ha: HA context
3484 *
3485 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
3486 * supported addressing method.
3487 */
Adrian Bunk47975472007-04-26 00:35:16 -07003488static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07003489{
3490 int retval;
3491
3492 /* Update our PCI device dma_mask for full 64 bit mask */
Yang Hongyang6a355282009-04-06 19:01:13 -07003493 if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
3494 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07003495 dev_dbg(&ha->pdev->dev,
3496 "Failed to set 64 bit PCI consistent mask; "
3497 "using 32 bit.\n");
3498 retval = pci_set_consistent_dma_mask(ha->pdev,
Yang Hongyang284901a2009-04-06 19:01:15 -07003499 DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07003500 }
3501 } else
Yang Hongyang284901a2009-04-06 19:01:15 -07003502 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07003503}
3504
3505static int qla4xxx_slave_alloc(struct scsi_device *sdev)
3506{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003507 struct iscsi_cls_session *cls_sess;
3508 struct iscsi_session *sess;
3509 struct ddb_entry *ddb;
Vikas Chaudhary8bb40332011-03-21 03:34:31 -07003510 int queue_depth = QL4_DEF_QDEPTH;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003511
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003512 cls_sess = starget_to_session(sdev->sdev_target);
3513 sess = cls_sess->dd_data;
3514 ddb = sess->dd_data;
3515
David Somayajuluafaf5a22006-09-19 10:28:00 -07003516 sdev->hostdata = ddb;
3517 sdev->tagged_supported = 1;
Vikas Chaudhary8bb40332011-03-21 03:34:31 -07003518
3519 if (ql4xmaxqdepth != 0 && ql4xmaxqdepth <= 0xffffU)
3520 queue_depth = ql4xmaxqdepth;
3521
3522 scsi_activate_tcq(sdev, queue_depth);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003523 return 0;
3524}
3525
3526static int qla4xxx_slave_configure(struct scsi_device *sdev)
3527{
3528 sdev->tagged_supported = 1;
3529 return 0;
3530}
3531
3532static void qla4xxx_slave_destroy(struct scsi_device *sdev)
3533{
3534 scsi_deactivate_tcq(sdev, 1);
3535}
3536
3537/**
3538 * qla4xxx_del_from_active_array - returns an active srb
3539 * @ha: Pointer to host adapter structure.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02003540 * @index: index into the active_array
David Somayajuluafaf5a22006-09-19 10:28:00 -07003541 *
3542 * This routine removes and returns the srb at the specified index
3543 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303544struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
3545 uint32_t index)
David Somayajuluafaf5a22006-09-19 10:28:00 -07003546{
3547 struct srb *srb = NULL;
Vikas Chaudhary53698872010-04-28 11:41:59 +05303548 struct scsi_cmnd *cmd = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003549
Vikas Chaudhary53698872010-04-28 11:41:59 +05303550 cmd = scsi_host_find_tag(ha->host, index);
3551 if (!cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07003552 return srb;
3553
Vikas Chaudhary53698872010-04-28 11:41:59 +05303554 srb = (struct srb *)CMD_SP(cmd);
3555 if (!srb)
David Somayajuluafaf5a22006-09-19 10:28:00 -07003556 return srb;
3557
3558 /* update counters */
3559 if (srb->flags & SRB_DMA_VALID) {
3560 ha->req_q_count += srb->iocb_cnt;
3561 ha->iocb_cnt -= srb->iocb_cnt;
3562 if (srb->cmd)
Vikas Chaudhary53698872010-04-28 11:41:59 +05303563 srb->cmd->host_scribble =
3564 (unsigned char *)(unsigned long) MAX_SRBS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003565 }
3566 return srb;
3567}
3568
3569/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07003570 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05303571 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07003572 * @cmd: Scsi Command to wait on.
3573 *
3574 * This routine waits for the command to be returned by the Firmware
3575 * for some max time.
3576 **/
3577static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
3578 struct scsi_cmnd *cmd)
3579{
3580 int done = 0;
3581 struct srb *rp;
3582 uint32_t max_wait_time = EH_WAIT_CMD_TOV;
Lalit Chandivade2232be02010-07-30 14:38:47 +05303583 int ret = SUCCESS;
3584
3585 /* Dont wait on command if PCI error is being handled
3586 * by PCI AER driver
3587 */
3588 if (unlikely(pci_channel_offline(ha->pdev)) ||
3589 (test_bit(AF_EEH_BUSY, &ha->flags))) {
3590 ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
3591 ha->host_no, __func__);
3592 return ret;
3593 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07003594
3595 do {
3596 /* Checking to see if its returned to OS */
Vikas Chaudhary53698872010-04-28 11:41:59 +05303597 rp = (struct srb *) CMD_SP(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003598 if (rp == NULL) {
3599 done++;
3600 break;
3601 }
3602
3603 msleep(2000);
3604 } while (max_wait_time--);
3605
3606 return done;
3607}
3608
3609/**
3610 * qla4xxx_wait_for_hba_online - waits for HBA to come online
3611 * @ha: Pointer to host adapter structure
3612 **/
3613static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
3614{
3615 unsigned long wait_online;
3616
Vikas Chaudharyf581a3f2010-10-06 22:47:48 -07003617 wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003618 while (time_before(jiffies, wait_online)) {
3619
3620 if (adapter_up(ha))
3621 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003622
3623 msleep(2000);
3624 }
3625
3626 return QLA_ERROR;
3627}
3628
3629/**
Mike Christiece545032008-02-29 18:25:20 -06003630 * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02003631 * @ha: pointer to HBA
David Somayajuluafaf5a22006-09-19 10:28:00 -07003632 * @t: target id
3633 * @l: lun id
3634 *
3635 * This function waits for all outstanding commands to a lun to complete. It
3636 * returns 0 if all pending commands are returned and 1 otherwise.
3637 **/
Mike Christiece545032008-02-29 18:25:20 -06003638static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
3639 struct scsi_target *stgt,
3640 struct scsi_device *sdev)
David Somayajuluafaf5a22006-09-19 10:28:00 -07003641{
3642 int cnt;
3643 int status = 0;
3644 struct scsi_cmnd *cmd;
3645
3646 /*
Mike Christiece545032008-02-29 18:25:20 -06003647 * Waiting for all commands for the designated target or dev
3648 * in the active array
David Somayajuluafaf5a22006-09-19 10:28:00 -07003649 */
3650 for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
3651 cmd = scsi_host_find_tag(ha->host, cnt);
Mike Christiece545032008-02-29 18:25:20 -06003652 if (cmd && stgt == scsi_target(cmd->device) &&
3653 (!sdev || sdev == cmd->device)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07003654 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
3655 status++;
3656 break;
3657 }
3658 }
3659 }
3660 return status;
3661}
3662
3663/**
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05303664 * qla4xxx_eh_abort - callback for abort task.
3665 * @cmd: Pointer to Linux's SCSI command structure
3666 *
3667 * This routine is called by the Linux OS to abort the specified
3668 * command.
3669 **/
3670static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
3671{
3672 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
3673 unsigned int id = cmd->device->id;
3674 unsigned int lun = cmd->device->lun;
Mike Christie92b3e5b2010-10-06 22:51:17 -07003675 unsigned long flags;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05303676 struct srb *srb = NULL;
3677 int ret = SUCCESS;
3678 int wait = 0;
3679
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303680 ql4_printk(KERN_INFO, ha,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04003681 "scsi%ld:%d:%d: Abort command issued cmd=%p\n",
3682 ha->host_no, id, lun, cmd);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05303683
Mike Christie92b3e5b2010-10-06 22:51:17 -07003684 spin_lock_irqsave(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05303685 srb = (struct srb *) CMD_SP(cmd);
Mike Christie92b3e5b2010-10-06 22:51:17 -07003686 if (!srb) {
3687 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05303688 return SUCCESS;
Mike Christie92b3e5b2010-10-06 22:51:17 -07003689 }
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05303690 kref_get(&srb->srb_ref);
Mike Christie92b3e5b2010-10-06 22:51:17 -07003691 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05303692
3693 if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
3694 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
3695 ha->host_no, id, lun));
3696 ret = FAILED;
3697 } else {
3698 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
3699 ha->host_no, id, lun));
3700 wait = 1;
3701 }
3702
3703 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
3704
3705 /* Wait for command to complete */
3706 if (wait) {
3707 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
3708 DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
3709 ha->host_no, id, lun));
3710 ret = FAILED;
3711 }
3712 }
3713
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303714 ql4_printk(KERN_INFO, ha,
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05303715 "scsi%ld:%d:%d: Abort command - %s\n",
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003716 ha->host_no, id, lun, (ret == SUCCESS) ? "succeeded" : "failed");
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05303717
3718 return ret;
3719}
3720
3721/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07003722 * qla4xxx_eh_device_reset - callback for target reset.
3723 * @cmd: Pointer to Linux's SCSI command structure
3724 *
3725 * This routine is called by the Linux OS to reset all luns on the
3726 * specified target.
3727 **/
3728static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
3729{
3730 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
3731 struct ddb_entry *ddb_entry = cmd->device->hostdata;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003732 int ret = FAILED, stat;
3733
Karen Higgins612f7342009-07-15 15:03:01 -05003734 if (!ddb_entry)
David Somayajuluafaf5a22006-09-19 10:28:00 -07003735 return ret;
3736
Mike Christiec01be6d2010-07-22 16:59:49 +05303737 ret = iscsi_block_scsi_eh(cmd);
3738 if (ret)
3739 return ret;
3740 ret = FAILED;
3741
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303742 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07003743 "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
3744 cmd->device->channel, cmd->device->id, cmd->device->lun);
3745
3746 DEBUG2(printk(KERN_INFO
3747 "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
3748 "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
Jens Axboe242f9dc2008-09-14 05:55:09 -07003749 cmd, jiffies, cmd->request->timeout / HZ,
David Somayajuluafaf5a22006-09-19 10:28:00 -07003750 ha->dpc_flags, cmd->result, cmd->allowed));
3751
3752 /* FIXME: wait for hba to go online */
3753 stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
3754 if (stat != QLA_SUCCESS) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303755 ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003756 goto eh_dev_reset_done;
3757 }
3758
Mike Christiece545032008-02-29 18:25:20 -06003759 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
3760 cmd->device)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303761 ql4_printk(KERN_INFO, ha,
Mike Christiece545032008-02-29 18:25:20 -06003762 "DEVICE RESET FAILED - waiting for "
3763 "commands.\n");
3764 goto eh_dev_reset_done;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003765 }
3766
David C Somayajulu9d562912008-03-19 11:23:03 -07003767 /* Send marker. */
3768 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
3769 MM_LUN_RESET) != QLA_SUCCESS)
3770 goto eh_dev_reset_done;
3771
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303772 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07003773 "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
3774 ha->host_no, cmd->device->channel, cmd->device->id,
3775 cmd->device->lun);
3776
3777 ret = SUCCESS;
3778
3779eh_dev_reset_done:
3780
3781 return ret;
3782}
3783
3784/**
Mike Christiece545032008-02-29 18:25:20 -06003785 * qla4xxx_eh_target_reset - callback for target reset.
3786 * @cmd: Pointer to Linux's SCSI command structure
3787 *
3788 * This routine is called by the Linux OS to reset the target.
3789 **/
3790static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
3791{
3792 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
3793 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christiec01be6d2010-07-22 16:59:49 +05303794 int stat, ret;
Mike Christiece545032008-02-29 18:25:20 -06003795
3796 if (!ddb_entry)
3797 return FAILED;
3798
Mike Christiec01be6d2010-07-22 16:59:49 +05303799 ret = iscsi_block_scsi_eh(cmd);
3800 if (ret)
3801 return ret;
3802
Mike Christiece545032008-02-29 18:25:20 -06003803 starget_printk(KERN_INFO, scsi_target(cmd->device),
3804 "WARM TARGET RESET ISSUED.\n");
3805
3806 DEBUG2(printk(KERN_INFO
3807 "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
3808 "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
Jens Axboe242f9dc2008-09-14 05:55:09 -07003809 ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
Mike Christiece545032008-02-29 18:25:20 -06003810 ha->dpc_flags, cmd->result, cmd->allowed));
3811
3812 stat = qla4xxx_reset_target(ha, ddb_entry);
3813 if (stat != QLA_SUCCESS) {
3814 starget_printk(KERN_INFO, scsi_target(cmd->device),
3815 "WARM TARGET RESET FAILED.\n");
3816 return FAILED;
3817 }
3818
Mike Christiece545032008-02-29 18:25:20 -06003819 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
3820 NULL)) {
3821 starget_printk(KERN_INFO, scsi_target(cmd->device),
3822 "WARM TARGET DEVICE RESET FAILED - "
3823 "waiting for commands.\n");
3824 return FAILED;
3825 }
3826
David C Somayajulu9d562912008-03-19 11:23:03 -07003827 /* Send marker. */
3828 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
3829 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
3830 starget_printk(KERN_INFO, scsi_target(cmd->device),
3831 "WARM TARGET DEVICE RESET FAILED - "
3832 "marker iocb failed.\n");
3833 return FAILED;
3834 }
3835
Mike Christiece545032008-02-29 18:25:20 -06003836 starget_printk(KERN_INFO, scsi_target(cmd->device),
3837 "WARM TARGET RESET SUCCEEDED.\n");
3838 return SUCCESS;
3839}
3840
3841/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07003842 * qla4xxx_eh_host_reset - kernel callback
3843 * @cmd: Pointer to Linux's SCSI command structure
3844 *
3845 * This routine is invoked by the Linux kernel to perform fatal error
3846 * recovery on the specified adapter.
3847 **/
3848static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
3849{
3850 int return_status = FAILED;
3851 struct scsi_qla_host *ha;
3852
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003853 ha = to_qla_host(cmd->device->host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003854
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303855 if (ql4xdontresethba) {
3856 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
3857 ha->host_no, __func__));
3858 return FAILED;
3859 }
3860
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303861 ql4_printk(KERN_INFO, ha,
Karen Higginsdca05c42009-07-15 15:03:00 -05003862 "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
David Somayajuluafaf5a22006-09-19 10:28:00 -07003863 cmd->device->channel, cmd->device->id, cmd->device->lun);
3864
3865 if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
3866 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
3867 "DEAD.\n", ha->host_no, cmd->device->channel,
3868 __func__));
3869
3870 return FAILED;
3871 }
3872
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303873 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
3874 if (is_qla8022(ha))
3875 set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
3876 else
3877 set_bit(DPC_RESET_HA, &ha->dpc_flags);
3878 }
Mike Christie50a29ae2008-03-04 13:26:53 -06003879
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303880 if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
David Somayajuluafaf5a22006-09-19 10:28:00 -07003881 return_status = SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003882
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303883 ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003884 return_status == FAILED ? "FAILED" : "SUCCEEDED");
David Somayajuluafaf5a22006-09-19 10:28:00 -07003885
3886 return return_status;
3887}
3888
Vikas Chaudhary95d31262011-08-12 02:51:29 -07003889static int qla4xxx_context_reset(struct scsi_qla_host *ha)
3890{
3891 uint32_t mbox_cmd[MBOX_REG_COUNT];
3892 uint32_t mbox_sts[MBOX_REG_COUNT];
3893 struct addr_ctrl_blk_def *acb = NULL;
3894 uint32_t acb_len = sizeof(struct addr_ctrl_blk_def);
3895 int rval = QLA_SUCCESS;
3896 dma_addr_t acb_dma;
3897
3898 acb = dma_alloc_coherent(&ha->pdev->dev,
3899 sizeof(struct addr_ctrl_blk_def),
3900 &acb_dma, GFP_KERNEL);
3901 if (!acb) {
3902 ql4_printk(KERN_ERR, ha, "%s: Unable to alloc acb\n",
3903 __func__);
3904 rval = -ENOMEM;
3905 goto exit_port_reset;
3906 }
3907
3908 memset(acb, 0, acb_len);
3909
3910 rval = qla4xxx_get_acb(ha, acb_dma, PRIMARI_ACB, acb_len);
3911 if (rval != QLA_SUCCESS) {
3912 rval = -EIO;
3913 goto exit_free_acb;
3914 }
3915
3916 rval = qla4xxx_disable_acb(ha);
3917 if (rval != QLA_SUCCESS) {
3918 rval = -EIO;
3919 goto exit_free_acb;
3920 }
3921
3922 wait_for_completion_timeout(&ha->disable_acb_comp,
3923 DISABLE_ACB_TOV * HZ);
3924
3925 rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], acb_dma);
3926 if (rval != QLA_SUCCESS) {
3927 rval = -EIO;
3928 goto exit_free_acb;
3929 }
3930
3931exit_free_acb:
3932 dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk_def),
3933 acb, acb_dma);
3934exit_port_reset:
3935 DEBUG2(ql4_printk(KERN_INFO, ha, "%s %s\n", __func__,
3936 rval == QLA_SUCCESS ? "SUCCEEDED" : "FAILED"));
3937 return rval;
3938}
3939
3940static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type)
3941{
3942 struct scsi_qla_host *ha = to_qla_host(shost);
3943 int rval = QLA_SUCCESS;
3944
3945 if (ql4xdontresethba) {
3946 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Don't Reset HBA\n",
3947 __func__));
3948 rval = -EPERM;
3949 goto exit_host_reset;
3950 }
3951
3952 rval = qla4xxx_wait_for_hba_online(ha);
3953 if (rval != QLA_SUCCESS) {
3954 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Unable to reset host "
3955 "adapter\n", __func__));
3956 rval = -EIO;
3957 goto exit_host_reset;
3958 }
3959
3960 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
3961 goto recover_adapter;
3962
3963 switch (reset_type) {
3964 case SCSI_ADAPTER_RESET:
3965 set_bit(DPC_RESET_HA, &ha->dpc_flags);
3966 break;
3967 case SCSI_FIRMWARE_RESET:
3968 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
3969 if (is_qla8022(ha))
3970 /* set firmware context reset */
3971 set_bit(DPC_RESET_HA_FW_CONTEXT,
3972 &ha->dpc_flags);
3973 else {
3974 rval = qla4xxx_context_reset(ha);
3975 goto exit_host_reset;
3976 }
3977 }
3978 break;
3979 }
3980
3981recover_adapter:
3982 rval = qla4xxx_recover_adapter(ha);
3983 if (rval != QLA_SUCCESS) {
3984 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: recover adapter fail\n",
3985 __func__));
3986 rval = -EIO;
3987 }
3988
3989exit_host_reset:
3990 return rval;
3991}
3992
Lalit Chandivade2232be02010-07-30 14:38:47 +05303993/* PCI AER driver recovers from all correctable errors w/o
3994 * driver intervention. For uncorrectable errors PCI AER
3995 * driver calls the following device driver's callbacks
3996 *
3997 * - Fatal Errors - link_reset
3998 * - Non-Fatal Errors - driver's pci_error_detected() which
3999 * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
4000 *
4001 * PCI AER driver calls
4002 * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
4003 * returns RECOVERED or NEED_RESET if fw_hung
4004 * NEED_RESET - driver's slot_reset()
4005 * DISCONNECT - device is dead & cannot recover
4006 * RECOVERED - driver's pci_resume()
4007 */
4008static pci_ers_result_t
4009qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
4010{
4011 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
4012
4013 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
4014 ha->host_no, __func__, state);
4015
4016 if (!is_aer_supported(ha))
4017 return PCI_ERS_RESULT_NONE;
4018
4019 switch (state) {
4020 case pci_channel_io_normal:
4021 clear_bit(AF_EEH_BUSY, &ha->flags);
4022 return PCI_ERS_RESULT_CAN_RECOVER;
4023 case pci_channel_io_frozen:
4024 set_bit(AF_EEH_BUSY, &ha->flags);
4025 qla4xxx_mailbox_premature_completion(ha);
4026 qla4xxx_free_irqs(ha);
4027 pci_disable_device(pdev);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07004028 /* Return back all IOs */
4029 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
Lalit Chandivade2232be02010-07-30 14:38:47 +05304030 return PCI_ERS_RESULT_NEED_RESET;
4031 case pci_channel_io_perm_failure:
4032 set_bit(AF_EEH_BUSY, &ha->flags);
4033 set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
4034 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
4035 return PCI_ERS_RESULT_DISCONNECT;
4036 }
4037 return PCI_ERS_RESULT_NEED_RESET;
4038}
4039
4040/**
4041 * qla4xxx_pci_mmio_enabled() gets called if
4042 * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
4043 * and read/write to the device still works.
4044 **/
4045static pci_ers_result_t
4046qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
4047{
4048 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
4049
4050 if (!is_aer_supported(ha))
4051 return PCI_ERS_RESULT_NONE;
4052
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07004053 return PCI_ERS_RESULT_RECOVERED;
Lalit Chandivade2232be02010-07-30 14:38:47 +05304054}
4055
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07004056static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
Lalit Chandivade2232be02010-07-30 14:38:47 +05304057{
4058 uint32_t rval = QLA_ERROR;
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07004059 uint32_t ret = 0;
Lalit Chandivade2232be02010-07-30 14:38:47 +05304060 int fn;
4061 struct pci_dev *other_pdev = NULL;
4062
4063 ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
4064
4065 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
4066
4067 if (test_bit(AF_ONLINE, &ha->flags)) {
4068 clear_bit(AF_ONLINE, &ha->flags);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05004069 clear_bit(AF_LINK_UP, &ha->flags);
4070 iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
Lalit Chandivade2232be02010-07-30 14:38:47 +05304071 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
Lalit Chandivade2232be02010-07-30 14:38:47 +05304072 }
4073
4074 fn = PCI_FUNC(ha->pdev->devfn);
4075 while (fn > 0) {
4076 fn--;
4077 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
4078 "func %x\n", ha->host_no, __func__, fn);
4079 /* Get the pci device given the domain, bus,
4080 * slot/function number */
4081 other_pdev =
4082 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
4083 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
4084 fn));
4085
4086 if (!other_pdev)
4087 continue;
4088
4089 if (atomic_read(&other_pdev->enable_cnt)) {
4090 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
4091 "func in enabled state%x\n", ha->host_no,
4092 __func__, fn);
4093 pci_dev_put(other_pdev);
4094 break;
4095 }
4096 pci_dev_put(other_pdev);
4097 }
4098
4099 /* The first function on the card, the reset owner will
4100 * start & initialize the firmware. The other functions
4101 * on the card will reset the firmware context
4102 */
4103 if (!fn) {
4104 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
4105 "0x%x is the owner\n", ha->host_no, __func__,
4106 ha->pdev->devfn);
4107
4108 qla4_8xxx_idc_lock(ha);
4109 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
4110 QLA82XX_DEV_COLD);
4111
4112 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
4113 QLA82XX_IDC_VERSION);
4114
4115 qla4_8xxx_idc_unlock(ha);
4116 clear_bit(AF_FW_RECOVERY, &ha->flags);
Manish Rangankar0e7e8502011-07-25 13:48:54 -05004117 rval = qla4xxx_initialize_adapter(ha);
Lalit Chandivade2232be02010-07-30 14:38:47 +05304118 qla4_8xxx_idc_lock(ha);
4119
4120 if (rval != QLA_SUCCESS) {
4121 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
4122 "FAILED\n", ha->host_no, __func__);
4123 qla4_8xxx_clear_drv_active(ha);
4124 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
4125 QLA82XX_DEV_FAILED);
4126 } else {
4127 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
4128 "READY\n", ha->host_no, __func__);
4129 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
4130 QLA82XX_DEV_READY);
4131 /* Clear driver state register */
4132 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
4133 qla4_8xxx_set_drv_active(ha);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07004134 ret = qla4xxx_request_irqs(ha);
4135 if (ret) {
4136 ql4_printk(KERN_WARNING, ha, "Failed to "
4137 "reserve interrupt %d already in use.\n",
4138 ha->pdev->irq);
4139 rval = QLA_ERROR;
4140 } else {
4141 ha->isp_ops->enable_intrs(ha);
4142 rval = QLA_SUCCESS;
4143 }
Lalit Chandivade2232be02010-07-30 14:38:47 +05304144 }
4145 qla4_8xxx_idc_unlock(ha);
4146 } else {
4147 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
4148 "the reset owner\n", ha->host_no, __func__,
4149 ha->pdev->devfn);
4150 if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
4151 QLA82XX_DEV_READY)) {
4152 clear_bit(AF_FW_RECOVERY, &ha->flags);
Manish Rangankar0e7e8502011-07-25 13:48:54 -05004153 rval = qla4xxx_initialize_adapter(ha);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07004154 if (rval == QLA_SUCCESS) {
4155 ret = qla4xxx_request_irqs(ha);
4156 if (ret) {
4157 ql4_printk(KERN_WARNING, ha, "Failed to"
4158 " reserve interrupt %d already in"
4159 " use.\n", ha->pdev->irq);
4160 rval = QLA_ERROR;
4161 } else {
4162 ha->isp_ops->enable_intrs(ha);
4163 rval = QLA_SUCCESS;
4164 }
4165 }
Lalit Chandivade2232be02010-07-30 14:38:47 +05304166 qla4_8xxx_idc_lock(ha);
4167 qla4_8xxx_set_drv_active(ha);
4168 qla4_8xxx_idc_unlock(ha);
4169 }
4170 }
4171 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
4172 return rval;
4173}
4174
4175static pci_ers_result_t
4176qla4xxx_pci_slot_reset(struct pci_dev *pdev)
4177{
4178 pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
4179 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
4180 int rc;
4181
4182 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
4183 ha->host_no, __func__);
4184
4185 if (!is_aer_supported(ha))
4186 return PCI_ERS_RESULT_NONE;
4187
4188 /* Restore the saved state of PCIe device -
4189 * BAR registers, PCI Config space, PCIX, MSI,
4190 * IOV states
4191 */
4192 pci_restore_state(pdev);
4193
4194 /* pci_restore_state() clears the saved_state flag of the device
4195 * save restored state which resets saved_state flag
4196 */
4197 pci_save_state(pdev);
4198
4199 /* Initialize device or resume if in suspended state */
4200 rc = pci_enable_device(pdev);
4201 if (rc) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004202 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Can't re-enable "
Lalit Chandivade2232be02010-07-30 14:38:47 +05304203 "device after reset\n", ha->host_no, __func__);
4204 goto exit_slot_reset;
4205 }
4206
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07004207 ha->isp_ops->disable_intrs(ha);
Lalit Chandivade2232be02010-07-30 14:38:47 +05304208
4209 if (is_qla8022(ha)) {
4210 if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
4211 ret = PCI_ERS_RESULT_RECOVERED;
4212 goto exit_slot_reset;
4213 } else
4214 goto exit_slot_reset;
4215 }
4216
4217exit_slot_reset:
4218 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
4219 "device after reset\n", ha->host_no, __func__, ret);
4220 return ret;
4221}
4222
4223static void
4224qla4xxx_pci_resume(struct pci_dev *pdev)
4225{
4226 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
4227 int ret;
4228
4229 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
4230 ha->host_no, __func__);
4231
4232 ret = qla4xxx_wait_for_hba_online(ha);
4233 if (ret != QLA_SUCCESS) {
4234 ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
4235 "resume I/O from slot/link_reset\n", ha->host_no,
4236 __func__);
4237 }
4238
4239 pci_cleanup_aer_uncorrect_error_status(pdev);
4240 clear_bit(AF_EEH_BUSY, &ha->flags);
4241}
4242
4243static struct pci_error_handlers qla4xxx_err_handler = {
4244 .error_detected = qla4xxx_pci_error_detected,
4245 .mmio_enabled = qla4xxx_pci_mmio_enabled,
4246 .slot_reset = qla4xxx_pci_slot_reset,
4247 .resume = qla4xxx_pci_resume,
4248};
4249
David Somayajuluafaf5a22006-09-19 10:28:00 -07004250static struct pci_device_id qla4xxx_pci_tbl[] = {
4251 {
4252 .vendor = PCI_VENDOR_ID_QLOGIC,
4253 .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
4254 .subvendor = PCI_ANY_ID,
4255 .subdevice = PCI_ANY_ID,
4256 },
4257 {
4258 .vendor = PCI_VENDOR_ID_QLOGIC,
4259 .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
4260 .subvendor = PCI_ANY_ID,
4261 .subdevice = PCI_ANY_ID,
4262 },
David C Somayajulud9150582006-11-15 17:38:40 -08004263 {
4264 .vendor = PCI_VENDOR_ID_QLOGIC,
4265 .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
4266 .subvendor = PCI_ANY_ID,
4267 .subdevice = PCI_ANY_ID,
4268 },
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304269 {
4270 .vendor = PCI_VENDOR_ID_QLOGIC,
4271 .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
4272 .subvendor = PCI_ANY_ID,
4273 .subdevice = PCI_ANY_ID,
4274 },
David Somayajuluafaf5a22006-09-19 10:28:00 -07004275 {0, 0},
4276};
4277MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
4278
Adrian Bunk47975472007-04-26 00:35:16 -07004279static struct pci_driver qla4xxx_pci_driver = {
David Somayajuluafaf5a22006-09-19 10:28:00 -07004280 .name = DRIVER_NAME,
4281 .id_table = qla4xxx_pci_tbl,
4282 .probe = qla4xxx_probe_adapter,
4283 .remove = qla4xxx_remove_adapter,
Lalit Chandivade2232be02010-07-30 14:38:47 +05304284 .err_handler = &qla4xxx_err_handler,
David Somayajuluafaf5a22006-09-19 10:28:00 -07004285};
4286
4287static int __init qla4xxx_module_init(void)
4288{
4289 int ret;
4290
4291 /* Allocate cache for SRBs. */
4292 srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09004293 SLAB_HWCACHE_ALIGN, NULL);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004294 if (srb_cachep == NULL) {
4295 printk(KERN_ERR
4296 "%s: Unable to allocate SRB cache..."
4297 "Failing load!\n", DRIVER_NAME);
4298 ret = -ENOMEM;
4299 goto no_srp_cache;
4300 }
4301
4302 /* Derive version string. */
4303 strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
Andrew Vasquez11010fe2006-10-06 09:54:59 -07004304 if (ql4xextended_error_logging)
David Somayajuluafaf5a22006-09-19 10:28:00 -07004305 strcat(qla4xxx_version_str, "-debug");
4306
4307 qla4xxx_scsi_transport =
4308 iscsi_register_transport(&qla4xxx_iscsi_transport);
4309 if (!qla4xxx_scsi_transport){
4310 ret = -ENODEV;
4311 goto release_srb_cache;
4312 }
4313
David Somayajuluafaf5a22006-09-19 10:28:00 -07004314 ret = pci_register_driver(&qla4xxx_pci_driver);
4315 if (ret)
4316 goto unregister_transport;
4317
4318 printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
4319 return 0;
Doug Maxey5ae16db2006-10-05 23:50:07 -05004320
David Somayajuluafaf5a22006-09-19 10:28:00 -07004321unregister_transport:
4322 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
4323release_srb_cache:
4324 kmem_cache_destroy(srb_cachep);
4325no_srp_cache:
4326 return ret;
4327}
4328
4329static void __exit qla4xxx_module_exit(void)
4330{
David Somayajuluafaf5a22006-09-19 10:28:00 -07004331 pci_unregister_driver(&qla4xxx_pci_driver);
4332 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
4333 kmem_cache_destroy(srb_cachep);
4334}
4335
4336module_init(qla4xxx_module_init);
4337module_exit(qla4xxx_module_exit);
4338
4339MODULE_AUTHOR("QLogic Corporation");
4340MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
4341MODULE_LICENSE("GPL");
4342MODULE_VERSION(QLA4XXX_DRIVER_VERSION);