blob: 15355f95f56037d2f3e8102d208b081fac23f2a8 [file] [log] [blame]
David Somayajuluafaf5a22006-09-19 10:28:00 -07001/*
2 * QLogic iSCSI HBA Driver
Vikas Chaudhary7d01d062010-12-02 22:12:51 -08003 * Copyright (c) 2003-2010 QLogic Corporation
David Somayajuluafaf5a22006-09-19 10:28:00 -07004 *
5 * See LICENSE.qla4xxx for copyright and licensing details.
6 */
7#include <linux/moduleparam.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
Manish Rangankar2a991c22011-07-25 13:48:55 -05009#include <linux/blkdev.h>
10#include <linux/iscsi_boot_sysfs.h>
David Somayajuluafaf5a22006-09-19 10:28:00 -070011
12#include <scsi/scsi_tcq.h>
13#include <scsi/scsicam.h>
14
15#include "ql4_def.h"
David C Somayajulubee4fe82007-05-23 18:03:32 -070016#include "ql4_version.h"
17#include "ql4_glbl.h"
18#include "ql4_dbg.h"
19#include "ql4_inline.h"
David Somayajuluafaf5a22006-09-19 10:28:00 -070020
21/*
22 * Driver version
23 */
Adrian Bunk47975472007-04-26 00:35:16 -070024static char qla4xxx_version_str[40];
David Somayajuluafaf5a22006-09-19 10:28:00 -070025
26/*
27 * SRB allocation cache
28 */
Christoph Lametere18b8902006-12-06 20:33:20 -080029static struct kmem_cache *srb_cachep;
David Somayajuluafaf5a22006-09-19 10:28:00 -070030
31/*
32 * Module parameter information and variables
33 */
David Somayajuluafaf5a22006-09-19 10:28:00 -070034int ql4xdontresethba = 0;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053035module_param(ql4xdontresethba, int, S_IRUGO | S_IWUSR);
David Somayajuluafaf5a22006-09-19 10:28:00 -070036MODULE_PARM_DESC(ql4xdontresethba,
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053037 "Don't reset the HBA for driver recovery \n"
38 " 0 - It will reset HBA (Default)\n"
39 " 1 - It will NOT reset HBA");
David Somayajuluafaf5a22006-09-19 10:28:00 -070040
Andrew Vasquez11010fe2006-10-06 09:54:59 -070041int ql4xextended_error_logging = 0; /* 0 = off, 1 = log errors */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053042module_param(ql4xextended_error_logging, int, S_IRUGO | S_IWUSR);
Andrew Vasquez11010fe2006-10-06 09:54:59 -070043MODULE_PARM_DESC(ql4xextended_error_logging,
David Somayajuluafaf5a22006-09-19 10:28:00 -070044 "Option to enable extended error logging, "
45 "Default is 0 - no logging, 1 - debug logging");
46
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053047int ql4xenablemsix = 1;
48module_param(ql4xenablemsix, int, S_IRUGO|S_IWUSR);
49MODULE_PARM_DESC(ql4xenablemsix,
50 "Set to enable MSI or MSI-X interrupt mechanism.\n"
51 " 0 = enable INTx interrupt mechanism.\n"
52 " 1 = enable MSI-X interrupt mechanism (Default).\n"
53 " 2 = enable MSI interrupt mechanism.");
David C Somayajulu477ffb92007-01-22 12:26:11 -080054
Mike Christied510d962008-07-11 19:50:33 -050055#define QL4_DEF_QDEPTH 32
Vikas Chaudhary8bb40332011-03-21 03:34:31 -070056static int ql4xmaxqdepth = QL4_DEF_QDEPTH;
57module_param(ql4xmaxqdepth, int, S_IRUGO | S_IWUSR);
58MODULE_PARM_DESC(ql4xmaxqdepth,
59 "Maximum queue depth to report for target devices.\n"
60 " Default: 32.");
Mike Christied510d962008-07-11 19:50:33 -050061
Vikas Chaudhary30387272011-03-21 03:34:32 -070062static int ql4xsess_recovery_tmo = QL4_SESS_RECOVERY_TMO;
63module_param(ql4xsess_recovery_tmo, int, S_IRUGO);
64MODULE_PARM_DESC(ql4xsess_recovery_tmo,
65 "Target Session Recovery Timeout.\n"
66 " Default: 30 sec.");
67
Manish Rangankarb3a271a2011-07-25 13:48:53 -050068static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -070069/*
70 * SCSI host template entry points
71 */
Adrian Bunk47975472007-04-26 00:35:16 -070072static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -070073
74/*
75 * iSCSI template entry points
76 */
David Somayajuluafaf5a22006-09-19 10:28:00 -070077static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
78 enum iscsi_param param, char *buf);
Mike Christieaa1e93a2007-05-30 12:57:09 -050079static int qla4xxx_host_get_param(struct Scsi_Host *shost,
80 enum iscsi_host_param param, char *buf);
Mike Christied00efe32011-07-25 13:48:38 -050081static int qla4xxx_iface_set_param(struct Scsi_Host *shost, char *data,
82 int count);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -050083static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
84 enum iscsi_param_type param_type,
85 int param, char *buf);
Mike Christie5c656af2009-07-15 15:02:59 -050086static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
Manish Rangankarb3a271a2011-07-25 13:48:53 -050087static struct iscsi_endpoint *qla4xxx_ep_connect(struct Scsi_Host *shost,
88 struct sockaddr *dst_addr,
89 int non_blocking);
90static int qla4xxx_ep_poll(struct iscsi_endpoint *ep, int timeout_ms);
91static void qla4xxx_ep_disconnect(struct iscsi_endpoint *ep);
92static int qla4xxx_get_ep_param(struct iscsi_endpoint *ep,
93 enum iscsi_param param, char *buf);
94static int qla4xxx_conn_start(struct iscsi_cls_conn *conn);
95static struct iscsi_cls_conn *
96qla4xxx_conn_create(struct iscsi_cls_session *cls_sess, uint32_t conn_idx);
97static int qla4xxx_conn_bind(struct iscsi_cls_session *cls_session,
98 struct iscsi_cls_conn *cls_conn,
99 uint64_t transport_fd, int is_leading);
100static void qla4xxx_conn_destroy(struct iscsi_cls_conn *conn);
101static struct iscsi_cls_session *
102qla4xxx_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max,
103 uint16_t qdepth, uint32_t initial_cmdsn);
104static void qla4xxx_session_destroy(struct iscsi_cls_session *sess);
105static void qla4xxx_task_work(struct work_struct *wdata);
106static int qla4xxx_alloc_pdu(struct iscsi_task *, uint8_t);
107static int qla4xxx_task_xmit(struct iscsi_task *);
108static void qla4xxx_task_cleanup(struct iscsi_task *);
109static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session);
110static void qla4xxx_conn_get_stats(struct iscsi_cls_conn *cls_conn,
111 struct iscsi_stats *stats);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700112/*
113 * SCSI host template entry points
114 */
Jeff Garzikf2812332010-11-16 02:10:29 -0500115static int qla4xxx_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmd);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530116static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700117static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
Mike Christiece545032008-02-29 18:25:20 -0600118static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700119static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
120static int qla4xxx_slave_alloc(struct scsi_device *device);
121static int qla4xxx_slave_configure(struct scsi_device *device);
122static void qla4xxx_slave_destroy(struct scsi_device *sdev);
Mike Christie3128c6c2011-07-25 13:48:42 -0500123static mode_t ql4_attr_is_visible(int param_type, int param);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700124
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530125static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
126 QLA82XX_LEGACY_INTR_CONFIG;
127
David Somayajuluafaf5a22006-09-19 10:28:00 -0700128static struct scsi_host_template qla4xxx_driver_template = {
129 .module = THIS_MODULE,
130 .name = DRIVER_NAME,
131 .proc_name = DRIVER_NAME,
132 .queuecommand = qla4xxx_queuecommand,
133
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530134 .eh_abort_handler = qla4xxx_eh_abort,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700135 .eh_device_reset_handler = qla4xxx_eh_device_reset,
Mike Christiece545032008-02-29 18:25:20 -0600136 .eh_target_reset_handler = qla4xxx_eh_target_reset,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700137 .eh_host_reset_handler = qla4xxx_eh_host_reset,
Mike Christie5c656af2009-07-15 15:02:59 -0500138 .eh_timed_out = qla4xxx_eh_cmd_timed_out,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700139
140 .slave_configure = qla4xxx_slave_configure,
141 .slave_alloc = qla4xxx_slave_alloc,
142 .slave_destroy = qla4xxx_slave_destroy,
143
144 .this_id = -1,
145 .cmd_per_lun = 3,
146 .use_clustering = ENABLE_CLUSTERING,
147 .sg_tablesize = SG_ALL,
148
149 .max_sectors = 0xFFFF,
Harish Zunjarrao7ad633c2011-05-17 23:17:11 -0700150 .shost_attrs = qla4xxx_host_attrs,
Vikas Chaudharya3559432011-07-25 13:48:51 -0500151 .vendor_id = SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_QLOGIC,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700152};
153
154static struct iscsi_transport qla4xxx_iscsi_transport = {
155 .owner = THIS_MODULE,
156 .name = DRIVER_NAME,
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500157 .caps = CAP_TEXT_NEGO |
158 CAP_DATA_PATH_OFFLOAD | CAP_HDRDGST |
159 CAP_DATADGST | CAP_LOGIN_OFFLOAD |
160 CAP_MULTI_R2T,
Mike Christie3128c6c2011-07-25 13:48:42 -0500161 .attr_is_visible = ql4_attr_is_visible,
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500162 .create_session = qla4xxx_session_create,
163 .destroy_session = qla4xxx_session_destroy,
164 .start_conn = qla4xxx_conn_start,
165 .create_conn = qla4xxx_conn_create,
166 .bind_conn = qla4xxx_conn_bind,
167 .stop_conn = iscsi_conn_stop,
168 .destroy_conn = qla4xxx_conn_destroy,
169 .set_param = iscsi_set_param,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700170 .get_conn_param = qla4xxx_conn_get_param,
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500171 .get_session_param = iscsi_session_get_param,
172 .get_ep_param = qla4xxx_get_ep_param,
173 .ep_connect = qla4xxx_ep_connect,
174 .ep_poll = qla4xxx_ep_poll,
175 .ep_disconnect = qla4xxx_ep_disconnect,
176 .get_stats = qla4xxx_conn_get_stats,
177 .send_pdu = iscsi_conn_send_pdu,
178 .xmit_task = qla4xxx_task_xmit,
179 .cleanup_task = qla4xxx_task_cleanup,
180 .alloc_pdu = qla4xxx_alloc_pdu,
181
Mike Christieaa1e93a2007-05-30 12:57:09 -0500182 .get_host_param = qla4xxx_host_get_param,
Mike Christied00efe32011-07-25 13:48:38 -0500183 .set_iface_param = qla4xxx_iface_set_param,
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500184 .get_iface_param = qla4xxx_get_iface_param,
Vikas Chaudharya3559432011-07-25 13:48:51 -0500185 .bsg_request = qla4xxx_bsg_request,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700186};
187
188static struct scsi_transport_template *qla4xxx_scsi_transport;
189
Mike Christie3128c6c2011-07-25 13:48:42 -0500190static mode_t ql4_attr_is_visible(int param_type, int param)
191{
192 switch (param_type) {
Mike Christief27fb2e2011-07-25 13:48:45 -0500193 case ISCSI_HOST_PARAM:
194 switch (param) {
195 case ISCSI_HOST_PARAM_HWADDRESS:
196 case ISCSI_HOST_PARAM_IPADDRESS:
197 case ISCSI_HOST_PARAM_INITIATOR_NAME:
198 return S_IRUGO;
199 default:
200 return 0;
201 }
Mike Christie3128c6c2011-07-25 13:48:42 -0500202 case ISCSI_PARAM:
203 switch (param) {
204 case ISCSI_PARAM_CONN_ADDRESS:
205 case ISCSI_PARAM_CONN_PORT:
Mike Christie1d063c12011-07-25 13:48:43 -0500206 case ISCSI_PARAM_TARGET_NAME:
207 case ISCSI_PARAM_TPGT:
208 case ISCSI_PARAM_TARGET_ALIAS:
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500209 case ISCSI_PARAM_MAX_BURST:
210 case ISCSI_PARAM_MAX_R2T:
211 case ISCSI_PARAM_FIRST_BURST:
212 case ISCSI_PARAM_MAX_RECV_DLENGTH:
213 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
Mike Christie3128c6c2011-07-25 13:48:42 -0500214 return S_IRUGO;
215 default:
216 return 0;
217 }
Mike Christieb78dbba2011-07-25 13:48:44 -0500218 case ISCSI_NET_PARAM:
219 switch (param) {
220 case ISCSI_NET_PARAM_IPV4_ADDR:
221 case ISCSI_NET_PARAM_IPV4_SUBNET:
222 case ISCSI_NET_PARAM_IPV4_GW:
223 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
224 case ISCSI_NET_PARAM_IFACE_ENABLE:
225 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
226 case ISCSI_NET_PARAM_IPV6_ADDR:
227 case ISCSI_NET_PARAM_IPV6_ROUTER:
228 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
229 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
Vikas Chaudhary6ac73e82011-07-25 13:48:49 -0500230 case ISCSI_NET_PARAM_VLAN_ID:
231 case ISCSI_NET_PARAM_VLAN_PRIORITY:
232 case ISCSI_NET_PARAM_VLAN_ENABLED:
Mike Christieb78dbba2011-07-25 13:48:44 -0500233 return S_IRUGO;
234 default:
235 return 0;
236 }
Mike Christie3128c6c2011-07-25 13:48:42 -0500237 }
238
239 return 0;
240}
241
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500242static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
243 enum iscsi_param_type param_type,
244 int param, char *buf)
245{
246 struct Scsi_Host *shost = iscsi_iface_to_shost(iface);
247 struct scsi_qla_host *ha = to_qla_host(shost);
248 int len = -ENOSYS;
249
250 if (param_type != ISCSI_NET_PARAM)
251 return -ENOSYS;
252
253 switch (param) {
254 case ISCSI_NET_PARAM_IPV4_ADDR:
255 len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
256 break;
257 case ISCSI_NET_PARAM_IPV4_SUBNET:
258 len = sprintf(buf, "%pI4\n", &ha->ip_config.subnet_mask);
259 break;
260 case ISCSI_NET_PARAM_IPV4_GW:
261 len = sprintf(buf, "%pI4\n", &ha->ip_config.gateway);
262 break;
263 case ISCSI_NET_PARAM_IFACE_ENABLE:
264 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
265 len = sprintf(buf, "%s\n",
266 (ha->ip_config.ipv4_options &
267 IPOPT_IPV4_PROTOCOL_ENABLE) ?
268 "enabled" : "disabled");
269 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
270 len = sprintf(buf, "%s\n",
271 (ha->ip_config.ipv6_options &
272 IPV6_OPT_IPV6_PROTOCOL_ENABLE) ?
273 "enabled" : "disabled");
274 break;
275 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
276 len = sprintf(buf, "%s\n",
277 (ha->ip_config.tcp_options & TCPOPT_DHCP_ENABLE) ?
278 "dhcp" : "static");
279 break;
280 case ISCSI_NET_PARAM_IPV6_ADDR:
281 if (iface->iface_num == 0)
282 len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr0);
283 if (iface->iface_num == 1)
284 len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr1);
285 break;
286 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
287 len = sprintf(buf, "%pI6\n",
288 &ha->ip_config.ipv6_link_local_addr);
289 break;
290 case ISCSI_NET_PARAM_IPV6_ROUTER:
291 len = sprintf(buf, "%pI6\n",
292 &ha->ip_config.ipv6_default_router_addr);
293 break;
294 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
295 len = sprintf(buf, "%s\n",
296 (ha->ip_config.ipv6_addl_options &
297 IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) ?
298 "nd" : "static");
299 break;
300 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
301 len = sprintf(buf, "%s\n",
302 (ha->ip_config.ipv6_addl_options &
303 IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR) ?
304 "auto" : "static");
305 break;
Vikas Chaudhary6ac73e82011-07-25 13:48:49 -0500306 case ISCSI_NET_PARAM_VLAN_ID:
307 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
308 len = sprintf(buf, "%d\n",
309 (ha->ip_config.ipv4_vlan_tag &
310 ISCSI_MAX_VLAN_ID));
311 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
312 len = sprintf(buf, "%d\n",
313 (ha->ip_config.ipv6_vlan_tag &
314 ISCSI_MAX_VLAN_ID));
315 break;
316 case ISCSI_NET_PARAM_VLAN_PRIORITY:
317 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
318 len = sprintf(buf, "%d\n",
319 ((ha->ip_config.ipv4_vlan_tag >> 13) &
320 ISCSI_MAX_VLAN_PRIORITY));
321 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
322 len = sprintf(buf, "%d\n",
323 ((ha->ip_config.ipv6_vlan_tag >> 13) &
324 ISCSI_MAX_VLAN_PRIORITY));
325 break;
326 case ISCSI_NET_PARAM_VLAN_ENABLED:
327 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
328 len = sprintf(buf, "%s\n",
329 (ha->ip_config.ipv4_options &
330 IPOPT_VLAN_TAGGING_ENABLE) ?
331 "enabled" : "disabled");
332 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
333 len = sprintf(buf, "%s\n",
334 (ha->ip_config.ipv6_options &
335 IPV6_OPT_VLAN_TAGGING_ENABLE) ?
336 "enabled" : "disabled");
337 break;
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500338 default:
339 len = -ENOSYS;
340 }
341
342 return len;
343}
344
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500345static struct iscsi_endpoint *
346qla4xxx_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
347 int non_blocking)
348{
349 int ret;
350 struct iscsi_endpoint *ep;
351 struct qla_endpoint *qla_ep;
352 struct scsi_qla_host *ha;
353 struct sockaddr_in *addr;
354 struct sockaddr_in6 *addr6;
355
356 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
357 if (!shost) {
358 ret = -ENXIO;
359 printk(KERN_ERR "%s: shost is NULL\n",
360 __func__);
361 return ERR_PTR(ret);
362 }
363
364 ha = iscsi_host_priv(shost);
365
366 ep = iscsi_create_endpoint(sizeof(struct qla_endpoint));
367 if (!ep) {
368 ret = -ENOMEM;
369 return ERR_PTR(ret);
370 }
371
372 qla_ep = ep->dd_data;
373 memset(qla_ep, 0, sizeof(struct qla_endpoint));
374 if (dst_addr->sa_family == AF_INET) {
375 memcpy(&qla_ep->dst_addr, dst_addr, sizeof(struct sockaddr_in));
376 addr = (struct sockaddr_in *)&qla_ep->dst_addr;
377 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI4\n", __func__,
378 (char *)&addr->sin_addr));
379 } else if (dst_addr->sa_family == AF_INET6) {
380 memcpy(&qla_ep->dst_addr, dst_addr,
381 sizeof(struct sockaddr_in6));
382 addr6 = (struct sockaddr_in6 *)&qla_ep->dst_addr;
383 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI6\n", __func__,
384 (char *)&addr6->sin6_addr));
385 }
386
387 qla_ep->host = shost;
388
389 return ep;
390}
391
392static int qla4xxx_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
393{
394 struct qla_endpoint *qla_ep;
395 struct scsi_qla_host *ha;
396 int ret = 0;
397
398 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
399 qla_ep = ep->dd_data;
400 ha = to_qla_host(qla_ep->host);
401
402 if (adapter_up(ha))
403 ret = 1;
404
405 return ret;
406}
407
408static void qla4xxx_ep_disconnect(struct iscsi_endpoint *ep)
409{
410 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
411 iscsi_destroy_endpoint(ep);
412}
413
414static int qla4xxx_get_ep_param(struct iscsi_endpoint *ep,
415 enum iscsi_param param,
416 char *buf)
417{
418 struct qla_endpoint *qla_ep = ep->dd_data;
419 struct sockaddr *dst_addr;
420
421 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
422
423 switch (param) {
424 case ISCSI_PARAM_CONN_PORT:
425 case ISCSI_PARAM_CONN_ADDRESS:
426 if (!qla_ep)
427 return -ENOTCONN;
428
429 dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
430 if (!dst_addr)
431 return -ENOTCONN;
432
433 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
434 &qla_ep->dst_addr, param, buf);
435 default:
436 return -ENOSYS;
437 }
438}
439
440static void qla4xxx_conn_get_stats(struct iscsi_cls_conn *cls_conn,
441 struct iscsi_stats *stats)
442{
443 struct iscsi_session *sess;
444 struct iscsi_cls_session *cls_sess;
445 struct ddb_entry *ddb_entry;
446 struct scsi_qla_host *ha;
447 struct ql_iscsi_stats *ql_iscsi_stats;
448 int stats_size;
449 int ret;
450 dma_addr_t iscsi_stats_dma;
451
452 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
453
454 cls_sess = iscsi_conn_to_session(cls_conn);
455 sess = cls_sess->dd_data;
456 ddb_entry = sess->dd_data;
457 ha = ddb_entry->ha;
458
459 stats_size = PAGE_ALIGN(sizeof(struct ql_iscsi_stats));
460 /* Allocate memory */
461 ql_iscsi_stats = dma_alloc_coherent(&ha->pdev->dev, stats_size,
462 &iscsi_stats_dma, GFP_KERNEL);
463 if (!ql_iscsi_stats) {
464 ql4_printk(KERN_ERR, ha,
465 "Unable to allocate memory for iscsi stats\n");
466 goto exit_get_stats;
467 }
468
469 ret = qla4xxx_get_mgmt_data(ha, ddb_entry->fw_ddb_index, stats_size,
470 iscsi_stats_dma);
471 if (ret != QLA_SUCCESS) {
472 ql4_printk(KERN_ERR, ha,
473 "Unable to retreive iscsi stats\n");
474 goto free_stats;
475 }
476
477 /* octets */
478 stats->txdata_octets = le64_to_cpu(ql_iscsi_stats->tx_data_octets);
479 stats->rxdata_octets = le64_to_cpu(ql_iscsi_stats->rx_data_octets);
480 /* xmit pdus */
481 stats->noptx_pdus = le32_to_cpu(ql_iscsi_stats->tx_nopout_pdus);
482 stats->scsicmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_cmd_pdus);
483 stats->tmfcmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_tmf_cmd_pdus);
484 stats->login_pdus = le32_to_cpu(ql_iscsi_stats->tx_login_cmd_pdus);
485 stats->text_pdus = le32_to_cpu(ql_iscsi_stats->tx_text_cmd_pdus);
486 stats->dataout_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_write_pdus);
487 stats->logout_pdus = le32_to_cpu(ql_iscsi_stats->tx_logout_cmd_pdus);
488 stats->snack_pdus = le32_to_cpu(ql_iscsi_stats->tx_snack_req_pdus);
489 /* recv pdus */
490 stats->noprx_pdus = le32_to_cpu(ql_iscsi_stats->rx_nopin_pdus);
491 stats->scsirsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_resp_pdus);
492 stats->tmfrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_tmf_resp_pdus);
493 stats->textrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_text_resp_pdus);
494 stats->datain_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_read_pdus);
495 stats->logoutrsp_pdus =
496 le32_to_cpu(ql_iscsi_stats->rx_logout_resp_pdus);
497 stats->r2t_pdus = le32_to_cpu(ql_iscsi_stats->rx_r2t_pdus);
498 stats->async_pdus = le32_to_cpu(ql_iscsi_stats->rx_async_pdus);
499 stats->rjt_pdus = le32_to_cpu(ql_iscsi_stats->rx_reject_pdus);
500
501free_stats:
502 dma_free_coherent(&ha->pdev->dev, stats_size, ql_iscsi_stats,
503 iscsi_stats_dma);
504exit_get_stats:
505 return;
506}
507
Mike Christie5c656af2009-07-15 15:02:59 -0500508static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
509{
510 struct iscsi_cls_session *session;
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500511 struct iscsi_session *sess;
512 unsigned long flags;
513 enum blk_eh_timer_return ret = BLK_EH_NOT_HANDLED;
Mike Christie5c656af2009-07-15 15:02:59 -0500514
515 session = starget_to_session(scsi_target(sc->device));
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500516 sess = session->dd_data;
Mike Christie5c656af2009-07-15 15:02:59 -0500517
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500518 spin_lock_irqsave(&session->lock, flags);
519 if (session->state == ISCSI_SESSION_FAILED)
520 ret = BLK_EH_RESET_TIMER;
521 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie5c656af2009-07-15 15:02:59 -0500522
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500523 return ret;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700524}
525
Mike Christieaa1e93a2007-05-30 12:57:09 -0500526static int qla4xxx_host_get_param(struct Scsi_Host *shost,
527 enum iscsi_host_param param, char *buf)
528{
529 struct scsi_qla_host *ha = to_qla_host(shost);
530 int len;
531
532 switch (param) {
533 case ISCSI_HOST_PARAM_HWADDRESS:
Michael Chan7ffc49a2007-12-24 21:28:09 -0800534 len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
Mike Christieaa1e93a2007-05-30 12:57:09 -0500535 break;
Mike Christie22236962007-05-30 12:57:24 -0500536 case ISCSI_HOST_PARAM_IPADDRESS:
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500537 len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
Mike Christie22236962007-05-30 12:57:24 -0500538 break;
Mike Christie8ad57812007-05-30 12:57:13 -0500539 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie22236962007-05-30 12:57:24 -0500540 len = sprintf(buf, "%s\n", ha->name_string);
Mike Christie8ad57812007-05-30 12:57:13 -0500541 break;
Mike Christieaa1e93a2007-05-30 12:57:09 -0500542 default:
543 return -ENOSYS;
544 }
545
546 return len;
547}
548
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500549static void qla4xxx_create_ipv4_iface(struct scsi_qla_host *ha)
550{
551 if (ha->iface_ipv4)
552 return;
553
554 /* IPv4 */
555 ha->iface_ipv4 = iscsi_create_iface(ha->host,
556 &qla4xxx_iscsi_transport,
557 ISCSI_IFACE_TYPE_IPV4, 0, 0);
558 if (!ha->iface_ipv4)
559 ql4_printk(KERN_ERR, ha, "Could not create IPv4 iSCSI "
560 "iface0.\n");
561}
562
563static void qla4xxx_create_ipv6_iface(struct scsi_qla_host *ha)
564{
565 if (!ha->iface_ipv6_0)
566 /* IPv6 iface-0 */
567 ha->iface_ipv6_0 = iscsi_create_iface(ha->host,
568 &qla4xxx_iscsi_transport,
569 ISCSI_IFACE_TYPE_IPV6, 0,
570 0);
571 if (!ha->iface_ipv6_0)
572 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
573 "iface0.\n");
574
575 if (!ha->iface_ipv6_1)
576 /* IPv6 iface-1 */
577 ha->iface_ipv6_1 = iscsi_create_iface(ha->host,
578 &qla4xxx_iscsi_transport,
579 ISCSI_IFACE_TYPE_IPV6, 1,
580 0);
581 if (!ha->iface_ipv6_1)
582 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
583 "iface1.\n");
584}
585
586static void qla4xxx_create_ifaces(struct scsi_qla_host *ha)
587{
588 if (ha->ip_config.ipv4_options & IPOPT_IPV4_PROTOCOL_ENABLE)
589 qla4xxx_create_ipv4_iface(ha);
590
591 if (ha->ip_config.ipv6_options & IPV6_OPT_IPV6_PROTOCOL_ENABLE)
592 qla4xxx_create_ipv6_iface(ha);
593}
594
595static void qla4xxx_destroy_ipv4_iface(struct scsi_qla_host *ha)
596{
597 if (ha->iface_ipv4) {
598 iscsi_destroy_iface(ha->iface_ipv4);
599 ha->iface_ipv4 = NULL;
600 }
601}
602
603static void qla4xxx_destroy_ipv6_iface(struct scsi_qla_host *ha)
604{
605 if (ha->iface_ipv6_0) {
606 iscsi_destroy_iface(ha->iface_ipv6_0);
607 ha->iface_ipv6_0 = NULL;
608 }
609 if (ha->iface_ipv6_1) {
610 iscsi_destroy_iface(ha->iface_ipv6_1);
611 ha->iface_ipv6_1 = NULL;
612 }
613}
614
615static void qla4xxx_destroy_ifaces(struct scsi_qla_host *ha)
616{
617 qla4xxx_destroy_ipv4_iface(ha);
618 qla4xxx_destroy_ipv6_iface(ha);
619}
620
Mike Christied00efe32011-07-25 13:48:38 -0500621static void qla4xxx_set_ipv6(struct scsi_qla_host *ha,
622 struct iscsi_iface_param_info *iface_param,
623 struct addr_ctrl_blk *init_fw_cb)
624{
625 /*
626 * iface_num 0 is valid for IPv6 Addr, linklocal, router, autocfg.
627 * iface_num 1 is valid only for IPv6 Addr.
628 */
629 switch (iface_param->param) {
630 case ISCSI_NET_PARAM_IPV6_ADDR:
631 if (iface_param->iface_num & 0x1)
632 /* IPv6 Addr 1 */
633 memcpy(init_fw_cb->ipv6_addr1, iface_param->value,
634 sizeof(init_fw_cb->ipv6_addr1));
635 else
636 /* IPv6 Addr 0 */
637 memcpy(init_fw_cb->ipv6_addr0, iface_param->value,
638 sizeof(init_fw_cb->ipv6_addr0));
639 break;
640 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
641 if (iface_param->iface_num & 0x1)
642 break;
643 memcpy(init_fw_cb->ipv6_if_id, &iface_param->value[8],
644 sizeof(init_fw_cb->ipv6_if_id));
645 break;
646 case ISCSI_NET_PARAM_IPV6_ROUTER:
647 if (iface_param->iface_num & 0x1)
648 break;
649 memcpy(init_fw_cb->ipv6_dflt_rtr_addr, iface_param->value,
650 sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
651 break;
652 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
653 /* Autocfg applies to even interface */
654 if (iface_param->iface_num & 0x1)
655 break;
656
657 if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_DISABLE)
658 init_fw_cb->ipv6_addtl_opts &=
659 cpu_to_le16(
660 ~IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
661 else if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_ND_ENABLE)
662 init_fw_cb->ipv6_addtl_opts |=
663 cpu_to_le16(
664 IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
665 else
666 ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
667 "IPv6 addr\n");
668 break;
669 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
670 /* Autocfg applies to even interface */
671 if (iface_param->iface_num & 0x1)
672 break;
673
674 if (iface_param->value[0] ==
675 ISCSI_IPV6_LINKLOCAL_AUTOCFG_ENABLE)
676 init_fw_cb->ipv6_addtl_opts |= cpu_to_le16(
677 IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
678 else if (iface_param->value[0] ==
679 ISCSI_IPV6_LINKLOCAL_AUTOCFG_DISABLE)
680 init_fw_cb->ipv6_addtl_opts &= cpu_to_le16(
681 ~IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
682 else
683 ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
684 "IPv6 linklocal addr\n");
685 break;
686 case ISCSI_NET_PARAM_IPV6_ROUTER_AUTOCFG:
687 /* Autocfg applies to even interface */
688 if (iface_param->iface_num & 0x1)
689 break;
690
691 if (iface_param->value[0] == ISCSI_IPV6_ROUTER_AUTOCFG_ENABLE)
692 memset(init_fw_cb->ipv6_dflt_rtr_addr, 0,
693 sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
694 break;
695 case ISCSI_NET_PARAM_IFACE_ENABLE:
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500696 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
Mike Christied00efe32011-07-25 13:48:38 -0500697 init_fw_cb->ipv6_opts |=
698 cpu_to_le16(IPV6_OPT_IPV6_PROTOCOL_ENABLE);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500699 qla4xxx_create_ipv6_iface(ha);
700 } else {
Mike Christied00efe32011-07-25 13:48:38 -0500701 init_fw_cb->ipv6_opts &=
702 cpu_to_le16(~IPV6_OPT_IPV6_PROTOCOL_ENABLE &
703 0xFFFF);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500704 qla4xxx_destroy_ipv6_iface(ha);
705 }
Mike Christied00efe32011-07-25 13:48:38 -0500706 break;
707 case ISCSI_NET_PARAM_VLAN_ID:
708 if (iface_param->len != sizeof(init_fw_cb->ipv6_vlan_tag))
709 break;
Vikas Chaudhary6ac73e82011-07-25 13:48:49 -0500710 init_fw_cb->ipv6_vlan_tag =
711 cpu_to_be16(*(uint16_t *)iface_param->value);
712 break;
713 case ISCSI_NET_PARAM_VLAN_ENABLED:
714 if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
715 init_fw_cb->ipv6_opts |=
716 cpu_to_le16(IPV6_OPT_VLAN_TAGGING_ENABLE);
717 else
718 init_fw_cb->ipv6_opts &=
719 cpu_to_le16(~IPV6_OPT_VLAN_TAGGING_ENABLE);
Mike Christied00efe32011-07-25 13:48:38 -0500720 break;
721 default:
722 ql4_printk(KERN_ERR, ha, "Unknown IPv6 param = %d\n",
723 iface_param->param);
724 break;
725 }
726}
727
728static void qla4xxx_set_ipv4(struct scsi_qla_host *ha,
729 struct iscsi_iface_param_info *iface_param,
730 struct addr_ctrl_blk *init_fw_cb)
731{
732 switch (iface_param->param) {
733 case ISCSI_NET_PARAM_IPV4_ADDR:
734 memcpy(init_fw_cb->ipv4_addr, iface_param->value,
735 sizeof(init_fw_cb->ipv4_addr));
736 break;
737 case ISCSI_NET_PARAM_IPV4_SUBNET:
738 memcpy(init_fw_cb->ipv4_subnet, iface_param->value,
739 sizeof(init_fw_cb->ipv4_subnet));
740 break;
741 case ISCSI_NET_PARAM_IPV4_GW:
742 memcpy(init_fw_cb->ipv4_gw_addr, iface_param->value,
743 sizeof(init_fw_cb->ipv4_gw_addr));
744 break;
745 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
746 if (iface_param->value[0] == ISCSI_BOOTPROTO_DHCP)
747 init_fw_cb->ipv4_tcp_opts |=
748 cpu_to_le16(TCPOPT_DHCP_ENABLE);
749 else if (iface_param->value[0] == ISCSI_BOOTPROTO_STATIC)
750 init_fw_cb->ipv4_tcp_opts &=
751 cpu_to_le16(~TCPOPT_DHCP_ENABLE);
752 else
753 ql4_printk(KERN_ERR, ha, "Invalid IPv4 bootproto\n");
754 break;
755 case ISCSI_NET_PARAM_IFACE_ENABLE:
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500756 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
Mike Christied00efe32011-07-25 13:48:38 -0500757 init_fw_cb->ipv4_ip_opts |=
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500758 cpu_to_le16(IPOPT_IPV4_PROTOCOL_ENABLE);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500759 qla4xxx_create_ipv4_iface(ha);
760 } else {
Mike Christied00efe32011-07-25 13:48:38 -0500761 init_fw_cb->ipv4_ip_opts &=
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500762 cpu_to_le16(~IPOPT_IPV4_PROTOCOL_ENABLE &
Mike Christied00efe32011-07-25 13:48:38 -0500763 0xFFFF);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500764 qla4xxx_destroy_ipv4_iface(ha);
765 }
Mike Christied00efe32011-07-25 13:48:38 -0500766 break;
767 case ISCSI_NET_PARAM_VLAN_ID:
768 if (iface_param->len != sizeof(init_fw_cb->ipv4_vlan_tag))
769 break;
Vikas Chaudhary6ac73e82011-07-25 13:48:49 -0500770 init_fw_cb->ipv4_vlan_tag =
771 cpu_to_be16(*(uint16_t *)iface_param->value);
772 break;
773 case ISCSI_NET_PARAM_VLAN_ENABLED:
774 if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
775 init_fw_cb->ipv4_ip_opts |=
776 cpu_to_le16(IPOPT_VLAN_TAGGING_ENABLE);
777 else
778 init_fw_cb->ipv4_ip_opts &=
779 cpu_to_le16(~IPOPT_VLAN_TAGGING_ENABLE);
Mike Christied00efe32011-07-25 13:48:38 -0500780 break;
781 default:
782 ql4_printk(KERN_ERR, ha, "Unknown IPv4 param = %d\n",
783 iface_param->param);
784 break;
785 }
786}
787
788static void
789qla4xxx_initcb_to_acb(struct addr_ctrl_blk *init_fw_cb)
790{
791 struct addr_ctrl_blk_def *acb;
792 acb = (struct addr_ctrl_blk_def *)init_fw_cb;
793 memset(acb->reserved1, 0, sizeof(acb->reserved1));
794 memset(acb->reserved2, 0, sizeof(acb->reserved2));
795 memset(acb->reserved3, 0, sizeof(acb->reserved3));
796 memset(acb->reserved4, 0, sizeof(acb->reserved4));
797 memset(acb->reserved5, 0, sizeof(acb->reserved5));
798 memset(acb->reserved6, 0, sizeof(acb->reserved6));
799 memset(acb->reserved7, 0, sizeof(acb->reserved7));
800 memset(acb->reserved8, 0, sizeof(acb->reserved8));
801 memset(acb->reserved9, 0, sizeof(acb->reserved9));
802 memset(acb->reserved10, 0, sizeof(acb->reserved10));
803 memset(acb->reserved11, 0, sizeof(acb->reserved11));
804 memset(acb->reserved12, 0, sizeof(acb->reserved12));
805 memset(acb->reserved13, 0, sizeof(acb->reserved13));
806 memset(acb->reserved14, 0, sizeof(acb->reserved14));
807 memset(acb->reserved15, 0, sizeof(acb->reserved15));
808}
809
810static int
811qla4xxx_iface_set_param(struct Scsi_Host *shost, char *data, int count)
812{
813 struct scsi_qla_host *ha = to_qla_host(shost);
814 int rval = 0;
815 struct iscsi_iface_param_info *iface_param = NULL;
816 struct addr_ctrl_blk *init_fw_cb = NULL;
817 dma_addr_t init_fw_cb_dma;
818 uint32_t mbox_cmd[MBOX_REG_COUNT];
819 uint32_t mbox_sts[MBOX_REG_COUNT];
820 uint32_t total_param_count;
821 uint32_t length;
822
823 init_fw_cb = dma_alloc_coherent(&ha->pdev->dev,
824 sizeof(struct addr_ctrl_blk),
825 &init_fw_cb_dma, GFP_KERNEL);
826 if (!init_fw_cb) {
827 ql4_printk(KERN_ERR, ha, "%s: Unable to alloc init_cb\n",
828 __func__);
829 return -ENOMEM;
830 }
831
832 memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
833 memset(&mbox_cmd, 0, sizeof(mbox_cmd));
834 memset(&mbox_sts, 0, sizeof(mbox_sts));
835
836 if (qla4xxx_get_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma)) {
837 ql4_printk(KERN_ERR, ha, "%s: get ifcb failed\n", __func__);
838 rval = -EIO;
839 goto exit_init_fw_cb;
840 }
841
842 total_param_count = count;
843 iface_param = (struct iscsi_iface_param_info *)data;
844
845 for ( ; total_param_count != 0; total_param_count--) {
846 length = iface_param->len;
847
848 if (iface_param->param_type != ISCSI_NET_PARAM)
849 continue;
850
851 switch (iface_param->iface_type) {
852 case ISCSI_IFACE_TYPE_IPV4:
853 switch (iface_param->iface_num) {
854 case 0:
855 qla4xxx_set_ipv4(ha, iface_param, init_fw_cb);
856 break;
857 default:
858 /* Cannot have more than one IPv4 interface */
859 ql4_printk(KERN_ERR, ha, "Invalid IPv4 iface "
860 "number = %d\n",
861 iface_param->iface_num);
862 break;
863 }
864 break;
865 case ISCSI_IFACE_TYPE_IPV6:
866 switch (iface_param->iface_num) {
867 case 0:
868 case 1:
869 qla4xxx_set_ipv6(ha, iface_param, init_fw_cb);
870 break;
871 default:
872 /* Cannot have more than two IPv6 interface */
873 ql4_printk(KERN_ERR, ha, "Invalid IPv6 iface "
874 "number = %d\n",
875 iface_param->iface_num);
876 break;
877 }
878 break;
879 default:
880 ql4_printk(KERN_ERR, ha, "Invalid iface type\n");
881 break;
882 }
883
884 iface_param = (struct iscsi_iface_param_info *)
885 ((uint8_t *)iface_param +
886 sizeof(struct iscsi_iface_param_info) + length);
887 }
888
889 init_fw_cb->cookie = cpu_to_le32(0x11BEAD5A);
890
891 rval = qla4xxx_set_flash(ha, init_fw_cb_dma, FLASH_SEGMENT_IFCB,
892 sizeof(struct addr_ctrl_blk),
893 FLASH_OPT_RMW_COMMIT);
894 if (rval != QLA_SUCCESS) {
895 ql4_printk(KERN_ERR, ha, "%s: set flash mbx failed\n",
896 __func__);
897 rval = -EIO;
898 goto exit_init_fw_cb;
899 }
900
901 qla4xxx_disable_acb(ha);
902
903 qla4xxx_initcb_to_acb(init_fw_cb);
904
905 rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma);
906 if (rval != QLA_SUCCESS) {
907 ql4_printk(KERN_ERR, ha, "%s: set acb mbx failed\n",
908 __func__);
909 rval = -EIO;
910 goto exit_init_fw_cb;
911 }
912
913 memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
914 qla4xxx_update_local_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb,
915 init_fw_cb_dma);
916
917exit_init_fw_cb:
918 dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk),
919 init_fw_cb, init_fw_cb_dma);
920
921 return rval;
922}
923
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500924static int qla4xxx_conn_get_param(struct iscsi_cls_conn *cls_conn,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700925 enum iscsi_param param, char *buf)
926{
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500927 struct iscsi_conn *conn;
928 struct qla_conn *qla_conn;
929 struct sockaddr *dst_addr;
930 int len = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700931
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500932 conn = cls_conn->dd_data;
933 qla_conn = conn->dd_data;
934 dst_addr = &qla_conn->qla_ep->dst_addr;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700935
936 switch (param) {
937 case ISCSI_PARAM_CONN_PORT:
David Somayajuluafaf5a22006-09-19 10:28:00 -0700938 case ISCSI_PARAM_CONN_ADDRESS:
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500939 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
940 dst_addr, param, buf);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700941 default:
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500942 return iscsi_conn_get_param(cls_conn, param, buf);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700943 }
944
945 return len;
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500946
David Somayajuluafaf5a22006-09-19 10:28:00 -0700947}
948
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500949static struct iscsi_cls_session *
950qla4xxx_session_create(struct iscsi_endpoint *ep,
951 uint16_t cmds_max, uint16_t qdepth,
952 uint32_t initial_cmdsn)
953{
954 struct iscsi_cls_session *cls_sess;
955 struct scsi_qla_host *ha;
956 struct qla_endpoint *qla_ep;
957 struct ddb_entry *ddb_entry;
958 uint32_t ddb_index;
959 uint32_t mbx_sts = 0;
960 struct iscsi_session *sess;
961 struct sockaddr *dst_addr;
962 int ret;
963
964 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
965 if (!ep) {
966 printk(KERN_ERR "qla4xxx: missing ep.\n");
967 return NULL;
968 }
969
970 qla_ep = ep->dd_data;
971 dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
972 ha = to_qla_host(qla_ep->host);
973get_ddb_index:
974 ddb_index = find_first_zero_bit(ha->ddb_idx_map, MAX_DDB_ENTRIES);
975
976 if (ddb_index >= MAX_DDB_ENTRIES) {
977 DEBUG2(ql4_printk(KERN_INFO, ha,
978 "Free DDB index not available\n"));
979 return NULL;
980 }
981
982 if (test_and_set_bit(ddb_index, ha->ddb_idx_map))
983 goto get_ddb_index;
984
985 DEBUG2(ql4_printk(KERN_INFO, ha,
986 "Found a free DDB index at %d\n", ddb_index));
987 ret = qla4xxx_req_ddb_entry(ha, ddb_index, &mbx_sts);
988 if (ret == QLA_ERROR) {
989 if (mbx_sts == MBOX_STS_COMMAND_ERROR) {
990 ql4_printk(KERN_INFO, ha,
991 "DDB index = %d not available trying next\n",
992 ddb_index);
993 goto get_ddb_index;
994 }
995 DEBUG2(ql4_printk(KERN_INFO, ha,
996 "Free FW DDB not available\n"));
997 return NULL;
998 }
999
1000 cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport, qla_ep->host,
1001 cmds_max, sizeof(struct ddb_entry),
1002 sizeof(struct ql4_task_data),
1003 initial_cmdsn, ddb_index);
1004 if (!cls_sess)
1005 return NULL;
1006
1007 sess = cls_sess->dd_data;
1008 ddb_entry = sess->dd_data;
1009 ddb_entry->fw_ddb_index = ddb_index;
1010 ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
1011 ddb_entry->ha = ha;
1012 ddb_entry->sess = cls_sess;
1013 cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
1014 ha->fw_ddb_index_map[ddb_entry->fw_ddb_index] = ddb_entry;
1015 ha->tot_ddbs++;
1016
1017 return cls_sess;
1018}
1019
1020static void qla4xxx_session_destroy(struct iscsi_cls_session *cls_sess)
1021{
1022 struct iscsi_session *sess;
1023 struct ddb_entry *ddb_entry;
1024 struct scsi_qla_host *ha;
1025 unsigned long flags;
1026
1027 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1028 sess = cls_sess->dd_data;
1029 ddb_entry = sess->dd_data;
1030 ha = ddb_entry->ha;
1031
1032 spin_lock_irqsave(&ha->hardware_lock, flags);
1033 qla4xxx_free_ddb(ha, ddb_entry);
1034 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1035 iscsi_session_teardown(cls_sess);
1036}
1037
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001038static struct iscsi_cls_conn *
1039qla4xxx_conn_create(struct iscsi_cls_session *cls_sess, uint32_t conn_idx)
1040{
1041 struct iscsi_cls_conn *cls_conn;
1042 struct iscsi_session *sess;
1043 struct ddb_entry *ddb_entry;
1044
1045 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1046 cls_conn = iscsi_conn_setup(cls_sess, sizeof(struct qla_conn),
1047 conn_idx);
1048 sess = cls_sess->dd_data;
1049 ddb_entry = sess->dd_data;
1050 ddb_entry->conn = cls_conn;
1051
1052 return cls_conn;
1053}
1054
1055static int qla4xxx_conn_bind(struct iscsi_cls_session *cls_session,
1056 struct iscsi_cls_conn *cls_conn,
1057 uint64_t transport_fd, int is_leading)
1058{
1059 struct iscsi_conn *conn;
1060 struct qla_conn *qla_conn;
1061 struct iscsi_endpoint *ep;
1062
1063 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1064
1065 if (iscsi_conn_bind(cls_session, cls_conn, is_leading))
1066 return -EINVAL;
1067 ep = iscsi_lookup_endpoint(transport_fd);
1068 conn = cls_conn->dd_data;
1069 qla_conn = conn->dd_data;
1070 qla_conn->qla_ep = ep->dd_data;
1071 return 0;
1072}
1073
1074static int qla4xxx_conn_start(struct iscsi_cls_conn *cls_conn)
1075{
1076 struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
1077 struct iscsi_session *sess;
1078 struct ddb_entry *ddb_entry;
1079 struct scsi_qla_host *ha;
1080 struct dev_db_entry *fw_ddb_entry;
1081 dma_addr_t fw_ddb_entry_dma;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001082 uint32_t mbx_sts = 0;
1083 int ret = 0;
1084 int status = QLA_SUCCESS;
1085
1086 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1087 sess = cls_sess->dd_data;
1088 ddb_entry = sess->dd_data;
1089 ha = ddb_entry->ha;
1090
1091 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1092 &fw_ddb_entry_dma, GFP_KERNEL);
1093 if (!fw_ddb_entry) {
1094 ql4_printk(KERN_ERR, ha,
1095 "%s: Unable to allocate dma buffer\n", __func__);
1096 return -ENOMEM;
1097 }
1098
1099 ret = qla4xxx_set_param_ddbentry(ha, ddb_entry, cls_conn, &mbx_sts);
1100 if (ret) {
1101 /* If iscsid is stopped and started then no need to do
1102 * set param again since ddb state will be already
1103 * active and FW does not allow set ddb to an
1104 * active session.
1105 */
1106 if (mbx_sts)
1107 if (ddb_entry->fw_ddb_device_state ==
1108 DDB_DS_SESSION_ACTIVE)
1109 goto exit_set_param;
1110
1111 ql4_printk(KERN_ERR, ha, "%s: Failed set param for index[%d]\n",
1112 __func__, ddb_entry->fw_ddb_index);
1113 goto exit_conn_start;
1114 }
1115
1116 status = qla4xxx_conn_open(ha, ddb_entry->fw_ddb_index);
1117 if (status == QLA_ERROR) {
Manish Rangankar0e7e8502011-07-25 13:48:54 -05001118 ql4_printk(KERN_ERR, ha, "%s: Login failed: %s\n", __func__,
1119 sess->targetname);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001120 ret = -EINVAL;
1121 goto exit_conn_start;
1122 }
1123
1124 ddb_entry->fw_ddb_device_state = DDB_DS_LOGIN_IN_PROCESS;
1125
1126exit_set_param:
1127 iscsi_conn_start(cls_conn);
1128 ret = 0;
1129
1130exit_conn_start:
1131 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1132 fw_ddb_entry, fw_ddb_entry_dma);
1133 return ret;
1134}
1135
1136static void qla4xxx_conn_destroy(struct iscsi_cls_conn *cls_conn)
1137{
1138 struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
1139 struct iscsi_session *sess;
1140 struct scsi_qla_host *ha;
1141 struct ddb_entry *ddb_entry;
1142 int options;
1143
1144 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1145 sess = cls_sess->dd_data;
1146 ddb_entry = sess->dd_data;
1147 ha = ddb_entry->ha;
1148
1149 options = LOGOUT_OPTION_CLOSE_SESSION;
1150 if (qla4xxx_session_logout_ddb(ha, ddb_entry, options) == QLA_ERROR)
1151 ql4_printk(KERN_ERR, ha, "%s: Logout failed\n", __func__);
1152 else
1153 qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
1154
1155 /*
1156 * Clear the DDB bit so that next login can use the bit
1157 * if FW is not clearing the DDB entry then set DDB will fail anyways
1158 */
1159 clear_bit(ddb_entry->fw_ddb_index, ha->ddb_idx_map);
1160}
1161
1162static void qla4xxx_task_work(struct work_struct *wdata)
1163{
1164 struct ql4_task_data *task_data;
1165 struct scsi_qla_host *ha;
1166 struct passthru_status *sts;
1167 struct iscsi_task *task;
1168 struct iscsi_hdr *hdr;
1169 uint8_t *data;
1170 uint32_t data_len;
1171 struct iscsi_conn *conn;
1172 int hdr_len;
1173 itt_t itt;
1174
1175 task_data = container_of(wdata, struct ql4_task_data, task_work);
1176 ha = task_data->ha;
1177 task = task_data->task;
1178 sts = &task_data->sts;
1179 hdr_len = sizeof(struct iscsi_hdr);
1180
1181 DEBUG3(printk(KERN_INFO "Status returned\n"));
1182 DEBUG3(qla4xxx_dump_buffer(sts, 64));
1183 DEBUG3(printk(KERN_INFO "Response buffer"));
1184 DEBUG3(qla4xxx_dump_buffer(task_data->resp_buffer, 64));
1185
1186 conn = task->conn;
1187
1188 switch (sts->completionStatus) {
1189 case PASSTHRU_STATUS_COMPLETE:
1190 hdr = (struct iscsi_hdr *)task_data->resp_buffer;
1191 /* Assign back the itt in hdr, until we use the PREASSIGN_TAG */
1192 itt = sts->handle;
1193 hdr->itt = itt;
1194 data = task_data->resp_buffer + hdr_len;
1195 data_len = task_data->resp_len - hdr_len;
1196 iscsi_complete_pdu(conn, hdr, data, data_len);
1197 break;
1198 default:
1199 ql4_printk(KERN_ERR, ha, "Passthru failed status = 0x%x\n",
1200 sts->completionStatus);
1201 break;
1202 }
1203 return;
1204}
1205
1206static int qla4xxx_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
1207{
1208 struct ql4_task_data *task_data;
1209 struct iscsi_session *sess;
1210 struct ddb_entry *ddb_entry;
1211 struct scsi_qla_host *ha;
1212 int hdr_len;
1213
1214 sess = task->conn->session;
1215 ddb_entry = sess->dd_data;
1216 ha = ddb_entry->ha;
1217 task_data = task->dd_data;
1218 memset(task_data, 0, sizeof(struct ql4_task_data));
1219
1220 if (task->sc) {
1221 ql4_printk(KERN_INFO, ha,
1222 "%s: SCSI Commands not implemented\n", __func__);
1223 return -EINVAL;
1224 }
1225
1226 hdr_len = sizeof(struct iscsi_hdr);
1227 task_data->ha = ha;
1228 task_data->task = task;
1229
1230 if (task->data_count) {
1231 task_data->data_dma = dma_map_single(&ha->pdev->dev, task->data,
1232 task->data_count,
1233 PCI_DMA_TODEVICE);
1234 }
1235
1236 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
1237 __func__, task->conn->max_recv_dlength, hdr_len));
1238
1239 task_data->resp_len = task->conn->max_recv_dlength;
1240 task_data->resp_buffer = dma_alloc_coherent(&ha->pdev->dev,
1241 task_data->resp_len,
1242 &task_data->resp_dma,
1243 GFP_ATOMIC);
1244 if (!task_data->resp_buffer)
1245 goto exit_alloc_pdu;
1246
1247 task_data->req_buffer = dma_alloc_coherent(&ha->pdev->dev,
1248 task->data_count + hdr_len,
1249 &task_data->req_dma,
1250 GFP_ATOMIC);
1251 if (!task_data->req_buffer)
1252 goto exit_alloc_pdu;
1253
1254 task->hdr = task_data->req_buffer;
1255
1256 INIT_WORK(&task_data->task_work, qla4xxx_task_work);
1257
1258 return 0;
1259
1260exit_alloc_pdu:
1261 if (task_data->resp_buffer)
1262 dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
1263 task_data->resp_buffer, task_data->resp_dma);
1264
1265 if (task_data->req_buffer)
1266 dma_free_coherent(&ha->pdev->dev, task->data_count + hdr_len,
1267 task_data->req_buffer, task_data->req_dma);
1268 return -ENOMEM;
1269}
1270
1271static void qla4xxx_task_cleanup(struct iscsi_task *task)
1272{
1273 struct ql4_task_data *task_data;
1274 struct iscsi_session *sess;
1275 struct ddb_entry *ddb_entry;
1276 struct scsi_qla_host *ha;
1277 int hdr_len;
1278
1279 hdr_len = sizeof(struct iscsi_hdr);
1280 sess = task->conn->session;
1281 ddb_entry = sess->dd_data;
1282 ha = ddb_entry->ha;
1283 task_data = task->dd_data;
1284
1285 if (task->data_count) {
1286 dma_unmap_single(&ha->pdev->dev, task_data->data_dma,
1287 task->data_count, PCI_DMA_TODEVICE);
1288 }
1289
1290 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
1291 __func__, task->conn->max_recv_dlength, hdr_len));
1292
1293 dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
1294 task_data->resp_buffer, task_data->resp_dma);
1295 dma_free_coherent(&ha->pdev->dev, task->data_count + hdr_len,
1296 task_data->req_buffer, task_data->req_dma);
1297 return;
1298}
1299
1300static int qla4xxx_task_xmit(struct iscsi_task *task)
1301{
1302 struct scsi_cmnd *sc = task->sc;
1303 struct iscsi_session *sess = task->conn->session;
1304 struct ddb_entry *ddb_entry = sess->dd_data;
1305 struct scsi_qla_host *ha = ddb_entry->ha;
1306
1307 if (!sc)
1308 return qla4xxx_send_passthru0(task);
1309
1310 ql4_printk(KERN_INFO, ha, "%s: scsi cmd xmit not implemented\n",
1311 __func__);
1312 return -ENOSYS;
1313}
1314
1315void qla4xxx_update_session_conn_param(struct scsi_qla_host *ha,
1316 struct ddb_entry *ddb_entry)
1317{
1318 struct iscsi_cls_session *cls_sess;
1319 struct iscsi_cls_conn *cls_conn;
1320 struct iscsi_session *sess;
1321 struct iscsi_conn *conn;
1322 uint32_t ddb_state;
1323 dma_addr_t fw_ddb_entry_dma;
1324 struct dev_db_entry *fw_ddb_entry;
1325
1326 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1327 &fw_ddb_entry_dma, GFP_KERNEL);
1328 if (!fw_ddb_entry) {
1329 ql4_printk(KERN_ERR, ha,
1330 "%s: Unable to allocate dma buffer\n", __func__);
1331 return;
1332 }
1333
1334 if (qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index, fw_ddb_entry,
1335 fw_ddb_entry_dma, NULL, NULL, &ddb_state,
1336 NULL, NULL, NULL) == QLA_ERROR) {
1337 DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: failed "
1338 "get_ddb_entry for fw_ddb_index %d\n",
1339 ha->host_no, __func__,
1340 ddb_entry->fw_ddb_index));
1341 return;
1342 }
1343
1344 cls_sess = ddb_entry->sess;
1345 sess = cls_sess->dd_data;
1346
1347 cls_conn = ddb_entry->conn;
1348 conn = cls_conn->dd_data;
1349
1350 /* Update params */
1351 conn->max_recv_dlength = BYTE_UNITS *
1352 le16_to_cpu(fw_ddb_entry->iscsi_max_rcv_data_seg_len);
1353
1354 conn->max_xmit_dlength = BYTE_UNITS *
1355 le16_to_cpu(fw_ddb_entry->iscsi_max_snd_data_seg_len);
1356
1357 sess->initial_r2t_en =
1358 (BIT_10 & le16_to_cpu(fw_ddb_entry->iscsi_options));
1359
1360 sess->max_r2t = le16_to_cpu(fw_ddb_entry->iscsi_max_outsnd_r2t);
1361
1362 sess->imm_data_en = (BIT_11 & le16_to_cpu(fw_ddb_entry->iscsi_options));
1363
1364 sess->first_burst = BYTE_UNITS *
1365 le16_to_cpu(fw_ddb_entry->iscsi_first_burst_len);
1366
1367 sess->max_burst = BYTE_UNITS *
1368 le16_to_cpu(fw_ddb_entry->iscsi_max_burst_len);
1369
1370 sess->time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
1371
1372 sess->time2retain = le16_to_cpu(fw_ddb_entry->iscsi_def_time2retain);
1373
1374 sess->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
1375
1376 memcpy(sess->initiatorname, ha->name_string,
1377 min(sizeof(ha->name_string), sizeof(sess->initiatorname)));
1378}
1379
David Somayajuluafaf5a22006-09-19 10:28:00 -07001380/*
1381 * Timer routines
1382 */
1383
1384static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
1385 unsigned long interval)
1386{
1387 DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
1388 __func__, ha->host->host_no));
1389 init_timer(&ha->timer);
1390 ha->timer.expires = jiffies + interval * HZ;
1391 ha->timer.data = (unsigned long)ha;
1392 ha->timer.function = (void (*)(unsigned long))func;
1393 add_timer(&ha->timer);
1394 ha->timer_active = 1;
1395}
1396
1397static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
1398{
1399 del_timer_sync(&ha->timer);
1400 ha->timer_active = 0;
1401}
1402
1403/***
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001404 * qla4xxx_mark_device_missing - blocks the session
1405 * @cls_session: Pointer to the session to be blocked
David Somayajuluafaf5a22006-09-19 10:28:00 -07001406 * @ddb_entry: Pointer to device database entry
1407 *
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301408 * This routine marks a device missing and close connection.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001409 **/
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001410void qla4xxx_mark_device_missing(struct iscsi_cls_session *cls_session)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001411{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001412 iscsi_block_session(cls_session);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001413}
1414
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301415/**
1416 * qla4xxx_mark_all_devices_missing - mark all devices as missing.
1417 * @ha: Pointer to host adapter structure.
1418 *
1419 * This routine marks a device missing and resets the relogin retry count.
1420 **/
1421void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
1422{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001423 iscsi_host_for_each_session(ha->host, qla4xxx_mark_device_missing);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301424}
1425
David Somayajuluafaf5a22006-09-19 10:28:00 -07001426static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
1427 struct ddb_entry *ddb_entry,
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001428 struct scsi_cmnd *cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001429{
1430 struct srb *srb;
1431
1432 srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
1433 if (!srb)
1434 return srb;
1435
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301436 kref_init(&srb->srb_ref);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001437 srb->ha = ha;
1438 srb->ddb = ddb_entry;
1439 srb->cmd = cmd;
1440 srb->flags = 0;
Vikas Chaudhary53698872010-04-28 11:41:59 +05301441 CMD_SP(cmd) = (void *)srb;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001442
1443 return srb;
1444}
1445
1446static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
1447{
1448 struct scsi_cmnd *cmd = srb->cmd;
1449
1450 if (srb->flags & SRB_DMA_VALID) {
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +09001451 scsi_dma_unmap(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001452 srb->flags &= ~SRB_DMA_VALID;
1453 }
Vikas Chaudhary53698872010-04-28 11:41:59 +05301454 CMD_SP(cmd) = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001455}
1456
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301457void qla4xxx_srb_compl(struct kref *ref)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001458{
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301459 struct srb *srb = container_of(ref, struct srb, srb_ref);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001460 struct scsi_cmnd *cmd = srb->cmd;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05301461 struct scsi_qla_host *ha = srb->ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001462
1463 qla4xxx_srb_free_dma(ha, srb);
1464
1465 mempool_free(srb, ha->srb_mempool);
1466
1467 cmd->scsi_done(cmd);
1468}
1469
1470/**
1471 * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001472 * @host: scsi host
David Somayajuluafaf5a22006-09-19 10:28:00 -07001473 * @cmd: Pointer to Linux's SCSI command structure
David Somayajuluafaf5a22006-09-19 10:28:00 -07001474 *
1475 * Remarks:
1476 * This routine is invoked by Linux to send a SCSI command to the driver.
1477 * The mid-level driver tries to ensure that queuecommand never gets
1478 * invoked concurrently with itself or the interrupt handler (although
1479 * the interrupt handler may call this routine as part of request-
1480 * completion handling). Unfortunely, it sometimes calls the scheduler
1481 * in interrupt context which is a big NO! NO!.
1482 **/
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001483static int qla4xxx_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001484{
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001485 struct scsi_qla_host *ha = to_qla_host(host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001486 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christie7fb19212008-01-31 13:36:45 -06001487 struct iscsi_cls_session *sess = ddb_entry->sess;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001488 struct srb *srb;
1489 int rval;
1490
Lalit Chandivade2232be02010-07-30 14:38:47 +05301491 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1492 if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
1493 cmd->result = DID_NO_CONNECT << 16;
1494 else
1495 cmd->result = DID_REQUEUE << 16;
1496 goto qc_fail_command;
1497 }
1498
Mike Christie7fb19212008-01-31 13:36:45 -06001499 if (!sess) {
1500 cmd->result = DID_IMM_RETRY << 16;
1501 goto qc_fail_command;
1502 }
1503
1504 rval = iscsi_session_chkready(sess);
1505 if (rval) {
1506 cmd->result = rval;
1507 goto qc_fail_command;
1508 }
1509
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301510 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1511 test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
1512 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1513 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
1514 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
1515 !test_bit(AF_ONLINE, &ha->flags) ||
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001516 !test_bit(AF_LINK_UP, &ha->flags) ||
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301517 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
David C Somayajulu477ffb92007-01-22 12:26:11 -08001518 goto qc_host_busy;
1519
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001520 srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001521 if (!srb)
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001522 goto qc_host_busy;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001523
1524 rval = qla4xxx_send_command_to_isp(ha, srb);
1525 if (rval != QLA_SUCCESS)
1526 goto qc_host_busy_free_sp;
1527
David Somayajuluafaf5a22006-09-19 10:28:00 -07001528 return 0;
1529
1530qc_host_busy_free_sp:
1531 qla4xxx_srb_free_dma(ha, srb);
1532 mempool_free(srb, ha->srb_mempool);
1533
David Somayajuluafaf5a22006-09-19 10:28:00 -07001534qc_host_busy:
1535 return SCSI_MLQUEUE_HOST_BUSY;
1536
1537qc_fail_command:
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07001538 cmd->scsi_done(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001539
1540 return 0;
1541}
1542
1543/**
1544 * qla4xxx_mem_free - frees memory allocated to adapter
1545 * @ha: Pointer to host adapter structure.
1546 *
1547 * Frees memory previously allocated by qla4xxx_mem_alloc
1548 **/
1549static void qla4xxx_mem_free(struct scsi_qla_host *ha)
1550{
1551 if (ha->queues)
1552 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
1553 ha->queues_dma);
1554
1555 ha->queues_len = 0;
1556 ha->queues = NULL;
1557 ha->queues_dma = 0;
1558 ha->request_ring = NULL;
1559 ha->request_dma = 0;
1560 ha->response_ring = NULL;
1561 ha->response_dma = 0;
1562 ha->shadow_regs = NULL;
1563 ha->shadow_regs_dma = 0;
1564
1565 /* Free srb pool. */
1566 if (ha->srb_mempool)
1567 mempool_destroy(ha->srb_mempool);
1568
1569 ha->srb_mempool = NULL;
1570
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001571 if (ha->chap_dma_pool)
1572 dma_pool_destroy(ha->chap_dma_pool);
1573
David Somayajuluafaf5a22006-09-19 10:28:00 -07001574 /* release io space registers */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301575 if (is_qla8022(ha)) {
1576 if (ha->nx_pcibase)
1577 iounmap(
1578 (struct device_reg_82xx __iomem *)ha->nx_pcibase);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301579 } else if (ha->reg)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001580 iounmap(ha->reg);
1581 pci_release_regions(ha->pdev);
1582}
1583
1584/**
1585 * qla4xxx_mem_alloc - allocates memory for use by adapter.
1586 * @ha: Pointer to host adapter structure
1587 *
1588 * Allocates DMA memory for request and response queues. Also allocates memory
1589 * for srbs.
1590 **/
1591static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
1592{
1593 unsigned long align;
1594
1595 /* Allocate contiguous block of DMA memory for queues. */
1596 ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
1597 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
1598 sizeof(struct shadow_regs) +
1599 MEM_ALIGN_VALUE +
1600 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
1601 ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
1602 &ha->queues_dma, GFP_KERNEL);
1603 if (ha->queues == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301604 ql4_printk(KERN_WARNING, ha,
1605 "Memory Allocation failed - queues.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001606
1607 goto mem_alloc_error_exit;
1608 }
1609 memset(ha->queues, 0, ha->queues_len);
1610
1611 /*
1612 * As per RISC alignment requirements -- the bus-address must be a
1613 * multiple of the request-ring size (in bytes).
1614 */
1615 align = 0;
1616 if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
1617 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
1618 (MEM_ALIGN_VALUE - 1));
1619
1620 /* Update request and response queue pointers. */
1621 ha->request_dma = ha->queues_dma + align;
1622 ha->request_ring = (struct queue_entry *) (ha->queues + align);
1623 ha->response_dma = ha->queues_dma + align +
1624 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
1625 ha->response_ring = (struct queue_entry *) (ha->queues + align +
1626 (REQUEST_QUEUE_DEPTH *
1627 QUEUE_SIZE));
1628 ha->shadow_regs_dma = ha->queues_dma + align +
1629 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
1630 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
1631 ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
1632 (REQUEST_QUEUE_DEPTH *
1633 QUEUE_SIZE) +
1634 (RESPONSE_QUEUE_DEPTH *
1635 QUEUE_SIZE));
1636
1637 /* Allocate memory for srb pool. */
1638 ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
1639 mempool_free_slab, srb_cachep);
1640 if (ha->srb_mempool == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301641 ql4_printk(KERN_WARNING, ha,
1642 "Memory Allocation failed - SRB Pool.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001643
1644 goto mem_alloc_error_exit;
1645 }
1646
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001647 ha->chap_dma_pool = dma_pool_create("ql4_chap", &ha->pdev->dev,
1648 CHAP_DMA_BLOCK_SIZE, 8, 0);
1649
1650 if (ha->chap_dma_pool == NULL) {
1651 ql4_printk(KERN_WARNING, ha,
1652 "%s: chap_dma_pool allocation failed..\n", __func__);
1653 goto mem_alloc_error_exit;
1654 }
1655
David Somayajuluafaf5a22006-09-19 10:28:00 -07001656 return QLA_SUCCESS;
1657
1658mem_alloc_error_exit:
1659 qla4xxx_mem_free(ha);
1660 return QLA_ERROR;
1661}
1662
1663/**
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301664 * qla4_8xxx_check_fw_alive - Check firmware health
1665 * @ha: Pointer to host adapter structure.
1666 *
1667 * Context: Interrupt
1668 **/
1669static void qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
1670{
1671 uint32_t fw_heartbeat_counter, halt_status;
1672
1673 fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
Lalit Chandivade2232be02010-07-30 14:38:47 +05301674 /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
1675 if (fw_heartbeat_counter == 0xffffffff) {
1676 DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
1677 "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
1678 ha->host_no, __func__));
1679 return;
1680 }
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301681
1682 if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
1683 ha->seconds_since_last_heartbeat++;
1684 /* FW not alive after 2 seconds */
1685 if (ha->seconds_since_last_heartbeat == 2) {
1686 ha->seconds_since_last_heartbeat = 0;
1687 halt_status = qla4_8xxx_rd_32(ha,
Vikas Chaudhary68d92eb2011-05-17 23:17:05 -07001688 QLA82XX_PEG_HALT_STATUS1);
1689
1690 ql4_printk(KERN_INFO, ha,
1691 "scsi(%ld): %s, Dumping hw/fw registers:\n "
1692 " PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2:"
1693 " 0x%x,\n PEG_NET_0_PC: 0x%x, PEG_NET_1_PC:"
1694 " 0x%x,\n PEG_NET_2_PC: 0x%x, PEG_NET_3_PC:"
1695 " 0x%x,\n PEG_NET_4_PC: 0x%x\n",
1696 ha->host_no, __func__, halt_status,
1697 qla4_8xxx_rd_32(ha,
1698 QLA82XX_PEG_HALT_STATUS2),
1699 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_0 +
1700 0x3c),
1701 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_1 +
1702 0x3c),
1703 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_2 +
1704 0x3c),
1705 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_3 +
1706 0x3c),
1707 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_4 +
1708 0x3c));
Nilesh Javali21033632010-07-30 14:28:07 +05301709
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301710 /* Since we cannot change dev_state in interrupt
1711 * context, set appropriate DPC flag then wakeup
1712 * DPC */
1713 if (halt_status & HALT_STATUS_UNRECOVERABLE)
1714 set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
1715 else {
1716 printk("scsi%ld: %s: detect abort needed!\n",
1717 ha->host_no, __func__);
1718 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1719 }
1720 qla4xxx_wake_dpc(ha);
Nilesh Javali21033632010-07-30 14:28:07 +05301721 qla4xxx_mailbox_premature_completion(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301722 }
Lalit Chandivade99457d72010-10-06 22:49:32 -07001723 } else
1724 ha->seconds_since_last_heartbeat = 0;
1725
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301726 ha->fw_heartbeat_counter = fw_heartbeat_counter;
1727}
1728
1729/**
1730 * qla4_8xxx_watchdog - Poll dev state
1731 * @ha: Pointer to host adapter structure.
1732 *
1733 * Context: Interrupt
1734 **/
1735void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
1736{
1737 uint32_t dev_state;
1738
1739 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
1740
1741 /* don't poll if reset is going on */
Lalit Chandivaded56a1f72010-12-02 22:12:45 -08001742 if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
1743 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
Vikas Chaudhary977f46a2011-05-17 23:17:08 -07001744 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags))) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301745 if (dev_state == QLA82XX_DEV_NEED_RESET &&
1746 !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
Vikas Chaudhary3930b8c2010-12-02 22:12:47 -08001747 if (!ql4xdontresethba) {
1748 ql4_printk(KERN_INFO, ha, "%s: HW State: "
1749 "NEED RESET!\n", __func__);
1750 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1751 qla4xxx_wake_dpc(ha);
1752 qla4xxx_mailbox_premature_completion(ha);
1753 }
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301754 } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
1755 !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
Vikas Chaudhary3930b8c2010-12-02 22:12:47 -08001756 ql4_printk(KERN_INFO, ha, "%s: HW State: NEED QUIES!\n",
1757 __func__);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301758 set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
1759 qla4xxx_wake_dpc(ha);
1760 } else {
1761 /* Check firmware health */
1762 qla4_8xxx_check_fw_alive(ha);
1763 }
1764 }
1765}
1766
1767/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07001768 * qla4xxx_timer - checks every second for work to do.
1769 * @ha: Pointer to host adapter structure.
1770 **/
1771static void qla4xxx_timer(struct scsi_qla_host *ha)
1772{
David Somayajuluafaf5a22006-09-19 10:28:00 -07001773 int start_dpc = 0;
Lalit Chandivade2232be02010-07-30 14:38:47 +05301774 uint16_t w;
1775
1776 /* If we are in the middle of AER/EEH processing
1777 * skip any processing and reschedule the timer
1778 */
1779 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1780 mod_timer(&ha->timer, jiffies + HZ);
1781 return;
1782 }
1783
1784 /* Hardware read to trigger an EEH error during mailbox waits. */
1785 if (!pci_channel_offline(ha->pdev))
1786 pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001787
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301788 if (is_qla8022(ha)) {
1789 qla4_8xxx_watchdog(ha);
1790 }
1791
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301792 if (!is_qla8022(ha)) {
1793 /* Check for heartbeat interval. */
1794 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
1795 ha->heartbeat_interval != 0) {
1796 ha->seconds_since_last_heartbeat++;
1797 if (ha->seconds_since_last_heartbeat >
1798 ha->heartbeat_interval + 2)
1799 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1800 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001801 }
1802
David Somayajuluafaf5a22006-09-19 10:28:00 -07001803 /* Wakeup the dpc routine for this adapter, if needed. */
Lalit Chandivade1b468072011-05-17 23:17:09 -07001804 if (start_dpc ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07001805 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1806 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
1807 test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301808 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07001809 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1810 test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301811 test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301812 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
1813 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
Lalit Chandivade1b468072011-05-17 23:17:09 -07001814 test_bit(DPC_AEN, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001815 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
1816 " - dpc flags = 0x%lx\n",
1817 ha->host_no, __func__, ha->dpc_flags));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301818 qla4xxx_wake_dpc(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001819 }
1820
1821 /* Reschedule timer thread to call us back in one second */
1822 mod_timer(&ha->timer, jiffies + HZ);
1823
1824 DEBUG2(ha->seconds_since_last_intr++);
1825}
1826
1827/**
1828 * qla4xxx_cmd_wait - waits for all outstanding commands to complete
1829 * @ha: Pointer to host adapter structure.
1830 *
1831 * This routine stalls the driver until all outstanding commands are returned.
1832 * Caller must release the Hardware Lock prior to calling this routine.
1833 **/
1834static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
1835{
1836 uint32_t index = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001837 unsigned long flags;
1838 struct scsi_cmnd *cmd;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001839
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301840 unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
1841
1842 DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
1843 "complete\n", WAIT_CMD_TOV));
1844
1845 while (!time_after_eq(jiffies, wtime)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001846 spin_lock_irqsave(&ha->hardware_lock, flags);
1847 /* Find a command that hasn't completed. */
1848 for (index = 0; index < ha->host->can_queue; index++) {
1849 cmd = scsi_host_find_tag(ha->host, index);
Mike Christiea1e00632010-10-26 05:45:30 -07001850 /*
1851 * We cannot just check if the index is valid,
1852 * becase if we are run from the scsi eh, then
1853 * the scsi/block layer is going to prevent
1854 * the tag from being released.
1855 */
1856 if (cmd != NULL && CMD_SP(cmd))
David Somayajuluafaf5a22006-09-19 10:28:00 -07001857 break;
1858 }
1859 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1860
1861 /* If No Commands are pending, wait is complete */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301862 if (index == ha->host->can_queue)
1863 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001864
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301865 msleep(1000);
1866 }
1867 /* If we timed out on waiting for commands to come back
1868 * return ERROR. */
1869 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001870}
1871
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301872int qla4xxx_hw_reset(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001873{
David Somayajuluafaf5a22006-09-19 10:28:00 -07001874 uint32_t ctrl_status;
David C Somayajulu477ffb92007-01-22 12:26:11 -08001875 unsigned long flags = 0;
1876
1877 DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001878
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301879 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
1880 return QLA_ERROR;
1881
David Somayajuluafaf5a22006-09-19 10:28:00 -07001882 spin_lock_irqsave(&ha->hardware_lock, flags);
1883
1884 /*
1885 * If the SCSI Reset Interrupt bit is set, clear it.
1886 * Otherwise, the Soft Reset won't work.
1887 */
1888 ctrl_status = readw(&ha->reg->ctrl_status);
1889 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
1890 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
1891
1892 /* Issue Soft Reset */
1893 writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
1894 readl(&ha->reg->ctrl_status);
1895
1896 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301897 return QLA_SUCCESS;
David C Somayajulu477ffb92007-01-22 12:26:11 -08001898}
1899
1900/**
1901 * qla4xxx_soft_reset - performs soft reset.
1902 * @ha: Pointer to host adapter structure.
1903 **/
1904int qla4xxx_soft_reset(struct scsi_qla_host *ha)
1905{
1906 uint32_t max_wait_time;
1907 unsigned long flags = 0;
Vikas Chaudharyf931c532010-10-06 22:48:07 -07001908 int status;
David C Somayajulu477ffb92007-01-22 12:26:11 -08001909 uint32_t ctrl_status;
1910
Vikas Chaudharyf931c532010-10-06 22:48:07 -07001911 status = qla4xxx_hw_reset(ha);
1912 if (status != QLA_SUCCESS)
1913 return status;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001914
Vikas Chaudharyf931c532010-10-06 22:48:07 -07001915 status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001916 /* Wait until the Network Reset Intr bit is cleared */
1917 max_wait_time = RESET_INTR_TOV;
1918 do {
1919 spin_lock_irqsave(&ha->hardware_lock, flags);
1920 ctrl_status = readw(&ha->reg->ctrl_status);
1921 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1922
1923 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
1924 break;
1925
1926 msleep(1000);
1927 } while ((--max_wait_time));
1928
1929 if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
1930 DEBUG2(printk(KERN_WARNING
1931 "scsi%ld: Network Reset Intr not cleared by "
1932 "Network function, clearing it now!\n",
1933 ha->host_no));
1934 spin_lock_irqsave(&ha->hardware_lock, flags);
1935 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
1936 readl(&ha->reg->ctrl_status);
1937 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1938 }
1939
1940 /* Wait until the firmware tells us the Soft Reset is done */
1941 max_wait_time = SOFT_RESET_TOV;
1942 do {
1943 spin_lock_irqsave(&ha->hardware_lock, flags);
1944 ctrl_status = readw(&ha->reg->ctrl_status);
1945 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1946
1947 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
1948 status = QLA_SUCCESS;
1949 break;
1950 }
1951
1952 msleep(1000);
1953 } while ((--max_wait_time));
1954
1955 /*
1956 * Also, make sure that the SCSI Reset Interrupt bit has been cleared
1957 * after the soft reset has taken place.
1958 */
1959 spin_lock_irqsave(&ha->hardware_lock, flags);
1960 ctrl_status = readw(&ha->reg->ctrl_status);
1961 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
1962 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
1963 readl(&ha->reg->ctrl_status);
1964 }
1965 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1966
1967 /* If soft reset fails then most probably the bios on other
1968 * function is also enabled.
1969 * Since the initialization is sequential the other fn
1970 * wont be able to acknowledge the soft reset.
1971 * Issue a force soft reset to workaround this scenario.
1972 */
1973 if (max_wait_time == 0) {
1974 /* Issue Force Soft Reset */
1975 spin_lock_irqsave(&ha->hardware_lock, flags);
1976 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
1977 readl(&ha->reg->ctrl_status);
1978 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1979 /* Wait until the firmware tells us the Soft Reset is done */
1980 max_wait_time = SOFT_RESET_TOV;
1981 do {
1982 spin_lock_irqsave(&ha->hardware_lock, flags);
1983 ctrl_status = readw(&ha->reg->ctrl_status);
1984 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1985
1986 if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
1987 status = QLA_SUCCESS;
1988 break;
1989 }
1990
1991 msleep(1000);
1992 } while ((--max_wait_time));
1993 }
1994
1995 return status;
1996}
1997
1998/**
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301999 * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
David Somayajuluafaf5a22006-09-19 10:28:00 -07002000 * @ha: Pointer to host adapter structure.
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302001 * @res: returned scsi status
David Somayajuluafaf5a22006-09-19 10:28:00 -07002002 *
2003 * This routine is called just prior to a HARD RESET to return all
2004 * outstanding commands back to the Operating System.
2005 * Caller should make sure that the following locks are released
2006 * before this calling routine: Hardware lock, and io_request_lock.
2007 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302008static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002009{
2010 struct srb *srb;
2011 int i;
2012 unsigned long flags;
2013
2014 spin_lock_irqsave(&ha->hardware_lock, flags);
2015 for (i = 0; i < ha->host->can_queue; i++) {
2016 srb = qla4xxx_del_from_active_array(ha, i);
2017 if (srb != NULL) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302018 srb->cmd->result = res;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302019 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002020 }
2021 }
2022 spin_unlock_irqrestore(&ha->hardware_lock, flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002023}
2024
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302025void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
2026{
2027 clear_bit(AF_ONLINE, &ha->flags);
2028
2029 /* Disable the board */
2030 ql4_printk(KERN_INFO, ha, "Disabling the board\n");
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302031
2032 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
2033 qla4xxx_mark_all_devices_missing(ha);
2034 clear_bit(AF_INIT_DONE, &ha->flags);
2035}
2036
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002037static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session)
2038{
2039 struct iscsi_session *sess;
2040 struct ddb_entry *ddb_entry;
2041
2042 sess = cls_session->dd_data;
2043 ddb_entry = sess->dd_data;
2044 ddb_entry->fw_ddb_device_state = DDB_DS_SESSION_FAILED;
2045 iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_CONN_FAILED);
2046}
2047
David Somayajuluafaf5a22006-09-19 10:28:00 -07002048/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002049 * qla4xxx_recover_adapter - recovers adapter after a fatal error
2050 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07002051 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302052static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002053{
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302054 int status = QLA_ERROR;
2055 uint8_t reset_chip = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002056
2057 /* Stall incoming I/O until we are done */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302058 scsi_block_requests(ha->host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002059 clear_bit(AF_ONLINE, &ha->flags);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002060 clear_bit(AF_LINK_UP, &ha->flags);
Mike Christie50a29ae2008-03-04 13:26:53 -06002061
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302062 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -07002063
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302064 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002065
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002066 iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
2067
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302068 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
2069 reset_chip = 1;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002070
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302071 /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
2072 * do not reset adapter, jump to initialize_adapter */
2073 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
2074 status = QLA_SUCCESS;
2075 goto recover_ha_init_adapter;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002076 }
2077
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302078 /* For the ISP-82xx adapter, issue a stop_firmware if invoked
2079 * from eh_host_reset or ioctl module */
2080 if (is_qla8022(ha) && !reset_chip &&
2081 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
2082
2083 DEBUG2(ql4_printk(KERN_INFO, ha,
2084 "scsi%ld: %s - Performing stop_firmware...\n",
2085 ha->host_no, __func__));
2086 status = ha->isp_ops->reset_firmware(ha);
2087 if (status == QLA_SUCCESS) {
Nilesh Javali2bd1e2b2010-10-06 22:49:20 -07002088 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
2089 qla4xxx_cmd_wait(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302090 ha->isp_ops->disable_intrs(ha);
2091 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2092 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
2093 } else {
2094 /* If the stop_firmware fails then
2095 * reset the entire chip */
2096 reset_chip = 1;
2097 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2098 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2099 }
2100 }
2101
2102 /* Issue full chip reset if recovering from a catastrophic error,
2103 * or if stop_firmware fails for ISP-82xx.
2104 * This is the default case for ISP-4xxx */
2105 if (!is_qla8022(ha) || reset_chip) {
Nilesh Javali2bd1e2b2010-10-06 22:49:20 -07002106 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
2107 qla4xxx_cmd_wait(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302108 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2109 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
2110 DEBUG2(ql4_printk(KERN_INFO, ha,
2111 "scsi%ld: %s - Performing chip reset..\n",
2112 ha->host_no, __func__));
2113 status = ha->isp_ops->reset_chip(ha);
2114 }
2115
2116 /* Flush any pending ddb changed AENs */
2117 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2118
2119recover_ha_init_adapter:
2120 /* Upon successful firmware/chip reset, re-initialize the adapter */
2121 if (status == QLA_SUCCESS) {
2122 /* For ISP-4xxx, force function 1 to always initialize
2123 * before function 3 to prevent both funcions from
2124 * stepping on top of the other */
2125 if (!is_qla8022(ha) && (ha->mac_index == 3))
2126 ssleep(6);
2127
2128 /* NOTE: AF_ONLINE flag set upon successful completion of
2129 * qla4xxx_initialize_adapter */
Manish Rangankar0e7e8502011-07-25 13:48:54 -05002130 status = qla4xxx_initialize_adapter(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302131 }
2132
2133 /* Retry failed adapter initialization, if necessary
2134 * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
2135 * case to prevent ping-pong resets between functions */
2136 if (!test_bit(AF_ONLINE, &ha->flags) &&
2137 !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002138 /* Adapter initialization failed, see if we can retry
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302139 * resetting the ha.
2140 * Since we don't want to block the DPC for too long
2141 * with multiple resets in the same thread,
2142 * utilize DPC to retry */
David Somayajuluafaf5a22006-09-19 10:28:00 -07002143 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
2144 ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
2145 DEBUG2(printk("scsi%ld: recover adapter - retrying "
2146 "(%d) more times\n", ha->host_no,
2147 ha->retry_reset_ha_cnt));
2148 set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
2149 status = QLA_ERROR;
2150 } else {
2151 if (ha->retry_reset_ha_cnt > 0) {
2152 /* Schedule another Reset HA--DPC will retry */
2153 ha->retry_reset_ha_cnt--;
2154 DEBUG2(printk("scsi%ld: recover adapter - "
2155 "retry remaining %d\n",
2156 ha->host_no,
2157 ha->retry_reset_ha_cnt));
2158 status = QLA_ERROR;
2159 }
2160
2161 if (ha->retry_reset_ha_cnt == 0) {
2162 /* Recover adapter retries have been exhausted.
2163 * Adapter DEAD */
2164 DEBUG2(printk("scsi%ld: recover adapter "
2165 "failed - board disabled\n",
2166 ha->host_no));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302167 qla4xxx_dead_adapter_cleanup(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002168 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
2169 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302170 clear_bit(DPC_RESET_HA_FW_CONTEXT,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002171 &ha->dpc_flags);
2172 status = QLA_ERROR;
2173 }
2174 }
2175 } else {
2176 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302177 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002178 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
2179 }
2180
2181 ha->adapter_error_count++;
2182
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302183 if (test_bit(AF_ONLINE, &ha->flags))
2184 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002185
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302186 scsi_unblock_requests(ha->host);
2187
2188 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2189 DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002190 status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302191
David Somayajuluafaf5a22006-09-19 10:28:00 -07002192 return status;
2193}
2194
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002195static void qla4xxx_relogin_devices(struct iscsi_cls_session *cls_session)
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07002196{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002197 struct iscsi_session *sess;
2198 struct ddb_entry *ddb_entry;
2199 struct scsi_qla_host *ha;
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07002200
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002201 sess = cls_session->dd_data;
2202 ddb_entry = sess->dd_data;
2203 ha = ddb_entry->ha;
2204 if (!iscsi_is_session_online(cls_session)) {
2205 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
2206 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
2207 " unblock session\n", ha->host_no, __func__,
2208 ddb_entry->fw_ddb_index);
2209 iscsi_unblock_session(ddb_entry->sess);
2210 } else {
2211 /* Trigger relogin */
2212 iscsi_session_failure(cls_session->dd_data,
2213 ISCSI_ERR_CONN_FAILED);
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07002214 }
2215 }
2216}
2217
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002218static void qla4xxx_relogin_all_devices(struct scsi_qla_host *ha)
2219{
2220 iscsi_host_for_each_session(ha->host, qla4xxx_relogin_devices);
2221}
2222
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302223void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
2224{
Lalit Chandivade1b468072011-05-17 23:17:09 -07002225 if (ha->dpc_thread)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302226 queue_work(ha->dpc_thread, &ha->dpc_work);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302227}
2228
David Somayajuluafaf5a22006-09-19 10:28:00 -07002229/**
2230 * qla4xxx_do_dpc - dpc routine
2231 * @data: in our case pointer to adapter structure
2232 *
2233 * This routine is a task that is schedule by the interrupt handler
2234 * to perform the background processing for interrupts. We put it
2235 * on a task queue that is consumed whenever the scheduler runs; that's
2236 * so you can do anything (i.e. put the process to sleep etc). In fact,
2237 * the mid-level tries to sleep when it reaches the driver threshold
2238 * "host->can_queue". This can cause a panic if we were in our interrupt code.
2239 **/
David Howellsc4028952006-11-22 14:57:56 +00002240static void qla4xxx_do_dpc(struct work_struct *work)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002241{
David Howellsc4028952006-11-22 14:57:56 +00002242 struct scsi_qla_host *ha =
2243 container_of(work, struct scsi_qla_host, dpc_work);
David C Somayajulu477ffb92007-01-22 12:26:11 -08002244 int status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002245
David C Somayajuluf26b9042006-11-15 16:41:09 -08002246 DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302247 "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
2248 ha->host_no, __func__, ha->flags, ha->dpc_flags))
David Somayajuluafaf5a22006-09-19 10:28:00 -07002249
2250 /* Initialization not yet finished. Don't do anything yet. */
2251 if (!test_bit(AF_INIT_DONE, &ha->flags))
Lalit Chandivade1b468072011-05-17 23:17:09 -07002252 return;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002253
Lalit Chandivade2232be02010-07-30 14:38:47 +05302254 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
2255 DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
2256 ha->host_no, __func__, ha->flags));
Lalit Chandivade1b468072011-05-17 23:17:09 -07002257 return;
Lalit Chandivade2232be02010-07-30 14:38:47 +05302258 }
2259
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302260 if (is_qla8022(ha)) {
2261 if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
2262 qla4_8xxx_idc_lock(ha);
2263 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2264 QLA82XX_DEV_FAILED);
2265 qla4_8xxx_idc_unlock(ha);
2266 ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
2267 qla4_8xxx_device_state_handler(ha);
2268 }
2269 if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
2270 qla4_8xxx_need_qsnt_handler(ha);
2271 }
2272 }
2273
2274 if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
2275 (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07002276 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302277 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
2278 if (ql4xdontresethba) {
2279 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
2280 ha->host_no, __func__));
2281 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
2282 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
2283 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2284 goto dpc_post_reset_ha;
2285 }
2286 if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
2287 test_bit(DPC_RESET_HA, &ha->dpc_flags))
2288 qla4xxx_recover_adapter(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002289
David C Somayajulu477ffb92007-01-22 12:26:11 -08002290 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002291 uint8_t wait_time = RESET_INTR_TOV;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002292
David Somayajuluafaf5a22006-09-19 10:28:00 -07002293 while ((readw(&ha->reg->ctrl_status) &
2294 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
2295 if (--wait_time == 0)
2296 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002297 msleep(1000);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002298 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002299 if (wait_time == 0)
2300 DEBUG2(printk("scsi%ld: %s: SR|FSR "
2301 "bit not cleared-- resetting\n",
2302 ha->host_no, __func__));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302303 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
David C Somayajulu477ffb92007-01-22 12:26:11 -08002304 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
2305 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302306 status = qla4xxx_recover_adapter(ha);
David C Somayajulu477ffb92007-01-22 12:26:11 -08002307 }
2308 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
2309 if (status == QLA_SUCCESS)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302310 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002311 }
2312 }
2313
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302314dpc_post_reset_ha:
David Somayajuluafaf5a22006-09-19 10:28:00 -07002315 /* ---- process AEN? --- */
2316 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
2317 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
2318
2319 /* ---- Get DHCP IP Address? --- */
2320 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
2321 qla4xxx_get_dhcp_ip_address(ha);
2322
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05302323 /* ---- link change? --- */
2324 if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
2325 if (!test_bit(AF_LINK_UP, &ha->flags)) {
2326 /* ---- link down? --- */
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07002327 qla4xxx_mark_all_devices_missing(ha);
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05302328 } else {
2329 /* ---- link up? --- *
2330 * F/W will auto login to all devices ONLY ONCE after
2331 * link up during driver initialization and runtime
2332 * fatal error recovery. Therefore, the driver must
2333 * manually relogin to devices when recovering from
2334 * connection failures, logouts, expired KATO, etc. */
2335
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07002336 qla4xxx_relogin_all_devices(ha);
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05302337 }
2338 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002339}
2340
2341/**
2342 * qla4xxx_free_adapter - release the adapter
2343 * @ha: pointer to adapter structure
2344 **/
2345static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
2346{
2347
2348 if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
2349 /* Turn-off interrupts on the card. */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302350 ha->isp_ops->disable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002351 }
2352
David Somayajuluafaf5a22006-09-19 10:28:00 -07002353 /* Remove timer thread, if present */
2354 if (ha->timer_active)
2355 qla4xxx_stop_timer(ha);
2356
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302357 /* Kill the kernel thread for this host */
2358 if (ha->dpc_thread)
2359 destroy_workqueue(ha->dpc_thread);
2360
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002361 /* Kill the kernel thread for this host */
2362 if (ha->task_wq)
2363 destroy_workqueue(ha->task_wq);
2364
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302365 /* Put firmware in known state */
2366 ha->isp_ops->reset_firmware(ha);
2367
2368 if (is_qla8022(ha)) {
2369 qla4_8xxx_idc_lock(ha);
2370 qla4_8xxx_clear_drv_active(ha);
2371 qla4_8xxx_idc_unlock(ha);
2372 }
2373
David Somayajuluafaf5a22006-09-19 10:28:00 -07002374 /* Detach interrupts */
2375 if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302376 qla4xxx_free_irqs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002377
David C Somayajulubee4fe82007-05-23 18:03:32 -07002378 /* free extra memory */
2379 qla4xxx_mem_free(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302380}
David C Somayajulubee4fe82007-05-23 18:03:32 -07002381
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302382int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
2383{
2384 int status = 0;
2385 uint8_t revision_id;
2386 unsigned long mem_base, mem_len, db_base, db_len;
2387 struct pci_dev *pdev = ha->pdev;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002388
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302389 status = pci_request_regions(pdev, DRIVER_NAME);
2390 if (status) {
2391 printk(KERN_WARNING
2392 "scsi(%ld) Failed to reserve PIO regions (%s) "
2393 "status=%d\n", ha->host_no, pci_name(pdev), status);
2394 goto iospace_error_exit;
2395 }
2396
2397 pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
2398 DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
2399 __func__, revision_id));
2400 ha->revision_id = revision_id;
2401
2402 /* remap phys address */
2403 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
2404 mem_len = pci_resource_len(pdev, 0);
2405 DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
2406 __func__, mem_base, mem_len));
2407
2408 /* mapping of pcibase pointer */
2409 ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
2410 if (!ha->nx_pcibase) {
2411 printk(KERN_ERR
2412 "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
2413 pci_release_regions(ha->pdev);
2414 goto iospace_error_exit;
2415 }
2416
2417 /* Mapping of IO base pointer, door bell read and write pointer */
2418
2419 /* mapping of IO base pointer */
2420 ha->qla4_8xxx_reg =
2421 (struct device_reg_82xx __iomem *)((uint8_t *)ha->nx_pcibase +
2422 0xbc000 + (ha->pdev->devfn << 11));
2423
2424 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
2425 db_len = pci_resource_len(pdev, 4);
2426
Shyam Sundar2657c802010-10-06 22:50:29 -07002427 ha->nx_db_wr_ptr = (ha->pdev->devfn == 4 ? QLA82XX_CAM_RAM_DB1 :
2428 QLA82XX_CAM_RAM_DB2);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302429
Shyam Sundar2657c802010-10-06 22:50:29 -07002430 return 0;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302431iospace_error_exit:
2432 return -ENOMEM;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002433}
2434
2435/***
2436 * qla4xxx_iospace_config - maps registers
2437 * @ha: pointer to adapter structure
2438 *
2439 * This routines maps HBA's registers from the pci address space
2440 * into the kernel virtual address space for memory mapped i/o.
2441 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302442int qla4xxx_iospace_config(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002443{
2444 unsigned long pio, pio_len, pio_flags;
2445 unsigned long mmio, mmio_len, mmio_flags;
2446
2447 pio = pci_resource_start(ha->pdev, 0);
2448 pio_len = pci_resource_len(ha->pdev, 0);
2449 pio_flags = pci_resource_flags(ha->pdev, 0);
2450 if (pio_flags & IORESOURCE_IO) {
2451 if (pio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302452 ql4_printk(KERN_WARNING, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07002453 "Invalid PCI I/O region size\n");
2454 pio = 0;
2455 }
2456 } else {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302457 ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002458 pio = 0;
2459 }
2460
2461 /* Use MMIO operations for all accesses. */
2462 mmio = pci_resource_start(ha->pdev, 1);
2463 mmio_len = pci_resource_len(ha->pdev, 1);
2464 mmio_flags = pci_resource_flags(ha->pdev, 1);
2465
2466 if (!(mmio_flags & IORESOURCE_MEM)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302467 ql4_printk(KERN_ERR, ha,
2468 "region #0 not an MMIO resource, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002469
2470 goto iospace_error_exit;
2471 }
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302472
David Somayajuluafaf5a22006-09-19 10:28:00 -07002473 if (mmio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302474 ql4_printk(KERN_ERR, ha,
2475 "Invalid PCI mem region size, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002476 goto iospace_error_exit;
2477 }
2478
2479 if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302480 ql4_printk(KERN_WARNING, ha,
2481 "Failed to reserve PIO/MMIO regions\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002482
2483 goto iospace_error_exit;
2484 }
2485
2486 ha->pio_address = pio;
2487 ha->pio_length = pio_len;
2488 ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
2489 if (!ha->reg) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302490 ql4_printk(KERN_ERR, ha,
2491 "cannot remap MMIO, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002492
2493 goto iospace_error_exit;
2494 }
2495
2496 return 0;
2497
2498iospace_error_exit:
2499 return -ENOMEM;
2500}
2501
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302502static struct isp_operations qla4xxx_isp_ops = {
2503 .iospace_config = qla4xxx_iospace_config,
2504 .pci_config = qla4xxx_pci_config,
2505 .disable_intrs = qla4xxx_disable_intrs,
2506 .enable_intrs = qla4xxx_enable_intrs,
2507 .start_firmware = qla4xxx_start_firmware,
2508 .intr_handler = qla4xxx_intr_handler,
2509 .interrupt_service_routine = qla4xxx_interrupt_service_routine,
2510 .reset_chip = qla4xxx_soft_reset,
2511 .reset_firmware = qla4xxx_hw_reset,
2512 .queue_iocb = qla4xxx_queue_iocb,
2513 .complete_iocb = qla4xxx_complete_iocb,
2514 .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
2515 .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
2516 .get_sys_info = qla4xxx_get_sys_info,
2517};
2518
2519static struct isp_operations qla4_8xxx_isp_ops = {
2520 .iospace_config = qla4_8xxx_iospace_config,
2521 .pci_config = qla4_8xxx_pci_config,
2522 .disable_intrs = qla4_8xxx_disable_intrs,
2523 .enable_intrs = qla4_8xxx_enable_intrs,
2524 .start_firmware = qla4_8xxx_load_risc,
2525 .intr_handler = qla4_8xxx_intr_handler,
2526 .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
2527 .reset_chip = qla4_8xxx_isp_reset,
2528 .reset_firmware = qla4_8xxx_stop_firmware,
2529 .queue_iocb = qla4_8xxx_queue_iocb,
2530 .complete_iocb = qla4_8xxx_complete_iocb,
2531 .rd_shdw_req_q_out = qla4_8xxx_rd_shdw_req_q_out,
2532 .rd_shdw_rsp_q_in = qla4_8xxx_rd_shdw_rsp_q_in,
2533 .get_sys_info = qla4_8xxx_get_sys_info,
2534};
2535
2536uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
2537{
2538 return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
2539}
2540
2541uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
2542{
2543 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
2544}
2545
2546uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
2547{
2548 return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
2549}
2550
2551uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
2552{
2553 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
2554}
2555
Manish Rangankar2a991c22011-07-25 13:48:55 -05002556static ssize_t qla4xxx_show_boot_eth_info(void *data, int type, char *buf)
2557{
2558 struct scsi_qla_host *ha = data;
2559 char *str = buf;
2560 int rc;
2561
2562 switch (type) {
2563 case ISCSI_BOOT_ETH_FLAGS:
2564 rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
2565 break;
2566 case ISCSI_BOOT_ETH_INDEX:
2567 rc = sprintf(str, "0\n");
2568 break;
2569 case ISCSI_BOOT_ETH_MAC:
2570 rc = sysfs_format_mac(str, ha->my_mac,
2571 MAC_ADDR_LEN);
2572 break;
2573 default:
2574 rc = -ENOSYS;
2575 break;
2576 }
2577 return rc;
2578}
2579
2580static mode_t qla4xxx_eth_get_attr_visibility(void *data, int type)
2581{
2582 int rc;
2583
2584 switch (type) {
2585 case ISCSI_BOOT_ETH_FLAGS:
2586 case ISCSI_BOOT_ETH_MAC:
2587 case ISCSI_BOOT_ETH_INDEX:
2588 rc = S_IRUGO;
2589 break;
2590 default:
2591 rc = 0;
2592 break;
2593 }
2594 return rc;
2595}
2596
2597static ssize_t qla4xxx_show_boot_ini_info(void *data, int type, char *buf)
2598{
2599 struct scsi_qla_host *ha = data;
2600 char *str = buf;
2601 int rc;
2602
2603 switch (type) {
2604 case ISCSI_BOOT_INI_INITIATOR_NAME:
2605 rc = sprintf(str, "%s\n", ha->name_string);
2606 break;
2607 default:
2608 rc = -ENOSYS;
2609 break;
2610 }
2611 return rc;
2612}
2613
2614static mode_t qla4xxx_ini_get_attr_visibility(void *data, int type)
2615{
2616 int rc;
2617
2618 switch (type) {
2619 case ISCSI_BOOT_INI_INITIATOR_NAME:
2620 rc = S_IRUGO;
2621 break;
2622 default:
2623 rc = 0;
2624 break;
2625 }
2626 return rc;
2627}
2628
2629static ssize_t
2630qla4xxx_show_boot_tgt_info(struct ql4_boot_session_info *boot_sess, int type,
2631 char *buf)
2632{
2633 struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
2634 char *str = buf;
2635 int rc;
2636
2637 switch (type) {
2638 case ISCSI_BOOT_TGT_NAME:
2639 rc = sprintf(buf, "%s\n", (char *)&boot_sess->target_name);
2640 break;
2641 case ISCSI_BOOT_TGT_IP_ADDR:
2642 if (boot_sess->conn_list[0].dest_ipaddr.ip_type == 0x1)
2643 rc = sprintf(buf, "%pI4\n",
2644 &boot_conn->dest_ipaddr.ip_address);
2645 else
2646 rc = sprintf(str, "%pI6\n",
2647 &boot_conn->dest_ipaddr.ip_address);
2648 break;
2649 case ISCSI_BOOT_TGT_PORT:
2650 rc = sprintf(str, "%d\n", boot_conn->dest_port);
2651 break;
2652 case ISCSI_BOOT_TGT_CHAP_NAME:
2653 rc = sprintf(str, "%.*s\n",
2654 boot_conn->chap.target_chap_name_length,
2655 (char *)&boot_conn->chap.target_chap_name);
2656 break;
2657 case ISCSI_BOOT_TGT_CHAP_SECRET:
2658 rc = sprintf(str, "%.*s\n",
2659 boot_conn->chap.target_secret_length,
2660 (char *)&boot_conn->chap.target_secret);
2661 break;
2662 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
2663 rc = sprintf(str, "%.*s\n",
2664 boot_conn->chap.intr_chap_name_length,
2665 (char *)&boot_conn->chap.intr_chap_name);
2666 break;
2667 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
2668 rc = sprintf(str, "%.*s\n",
2669 boot_conn->chap.intr_secret_length,
2670 (char *)&boot_conn->chap.intr_secret);
2671 break;
2672 case ISCSI_BOOT_TGT_FLAGS:
2673 rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
2674 break;
2675 case ISCSI_BOOT_TGT_NIC_ASSOC:
2676 rc = sprintf(str, "0\n");
2677 break;
2678 default:
2679 rc = -ENOSYS;
2680 break;
2681 }
2682 return rc;
2683}
2684
2685static ssize_t qla4xxx_show_boot_tgt_pri_info(void *data, int type, char *buf)
2686{
2687 struct scsi_qla_host *ha = data;
2688 struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_pri_sess);
2689
2690 return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
2691}
2692
2693static ssize_t qla4xxx_show_boot_tgt_sec_info(void *data, int type, char *buf)
2694{
2695 struct scsi_qla_host *ha = data;
2696 struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_sec_sess);
2697
2698 return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
2699}
2700
2701static mode_t qla4xxx_tgt_get_attr_visibility(void *data, int type)
2702{
2703 int rc;
2704
2705 switch (type) {
2706 case ISCSI_BOOT_TGT_NAME:
2707 case ISCSI_BOOT_TGT_IP_ADDR:
2708 case ISCSI_BOOT_TGT_PORT:
2709 case ISCSI_BOOT_TGT_CHAP_NAME:
2710 case ISCSI_BOOT_TGT_CHAP_SECRET:
2711 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
2712 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
2713 case ISCSI_BOOT_TGT_NIC_ASSOC:
2714 case ISCSI_BOOT_TGT_FLAGS:
2715 rc = S_IRUGO;
2716 break;
2717 default:
2718 rc = 0;
2719 break;
2720 }
2721 return rc;
2722}
2723
2724static void qla4xxx_boot_release(void *data)
2725{
2726 struct scsi_qla_host *ha = data;
2727
2728 scsi_host_put(ha->host);
2729}
2730
2731static int get_fw_boot_info(struct scsi_qla_host *ha, uint16_t ddb_index[])
2732{
2733 dma_addr_t buf_dma;
2734 uint32_t addr, pri_addr, sec_addr;
2735 uint32_t offset;
2736 uint16_t func_num;
2737 uint8_t val;
2738 uint8_t *buf = NULL;
2739 size_t size = 13 * sizeof(uint8_t);
2740 int ret = QLA_SUCCESS;
2741
2742 func_num = PCI_FUNC(ha->pdev->devfn);
2743
2744 DEBUG2(ql4_printk(KERN_INFO, ha,
2745 "%s: Get FW boot info for 0x%x func %d\n", __func__,
2746 (is_qla4032(ha) ? PCI_DEVICE_ID_QLOGIC_ISP4032 :
2747 PCI_DEVICE_ID_QLOGIC_ISP8022), func_num));
2748
2749 if (is_qla4032(ha)) {
2750 if (func_num == 1) {
2751 addr = NVRAM_PORT0_BOOT_MODE;
2752 pri_addr = NVRAM_PORT0_BOOT_PRI_TGT;
2753 sec_addr = NVRAM_PORT0_BOOT_SEC_TGT;
2754 } else if (func_num == 3) {
2755 addr = NVRAM_PORT1_BOOT_MODE;
2756 pri_addr = NVRAM_PORT1_BOOT_PRI_TGT;
2757 sec_addr = NVRAM_PORT1_BOOT_SEC_TGT;
2758 } else {
2759 ret = QLA_ERROR;
2760 goto exit_boot_info;
2761 }
2762
2763 /* Check Boot Mode */
2764 val = rd_nvram_byte(ha, addr);
2765 if (!(val & 0x07)) {
2766 DEBUG2(ql4_printk(KERN_ERR, ha,
2767 "%s: Failed Boot options : 0x%x\n",
2768 __func__, val));
2769 ret = QLA_ERROR;
2770 goto exit_boot_info;
2771 }
2772
2773 /* get primary valid target index */
2774 val = rd_nvram_byte(ha, pri_addr);
2775 if (val & BIT_7)
2776 ddb_index[0] = (val & 0x7f);
2777 else
2778 ddb_index[0] = 0;
2779
2780 /* get secondary valid target index */
2781 val = rd_nvram_byte(ha, sec_addr);
2782 if (val & BIT_7)
2783 ddb_index[1] = (val & 0x7f);
2784 else
2785 ddb_index[1] = 1;
2786
2787 } else if (is_qla8022(ha)) {
2788 buf = dma_alloc_coherent(&ha->pdev->dev, size,
2789 &buf_dma, GFP_KERNEL);
2790 if (!buf) {
2791 DEBUG2(ql4_printk(KERN_ERR, ha,
2792 "%s: Unable to allocate dma buffer\n",
2793 __func__));
2794 ret = QLA_ERROR;
2795 goto exit_boot_info;
2796 }
2797
2798 if (ha->port_num == 0)
2799 offset = BOOT_PARAM_OFFSET_PORT0;
2800 else if (ha->port_num == 1)
2801 offset = BOOT_PARAM_OFFSET_PORT1;
2802 else {
2803 ret = QLA_ERROR;
2804 goto exit_boot_info_free;
2805 }
2806 addr = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_iscsi_param * 4) +
2807 offset;
2808 if (qla4xxx_get_flash(ha, buf_dma, addr,
2809 13 * sizeof(uint8_t)) != QLA_SUCCESS) {
2810 DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: Get Flash"
2811 "failed\n", ha->host_no, __func__));
2812 ret = QLA_ERROR;
2813 goto exit_boot_info_free;
2814 }
2815 /* Check Boot Mode */
2816 if (!(buf[1] & 0x07)) {
2817 DEBUG2(ql4_printk(KERN_INFO, ha,
2818 "Failed: Boot options : 0x%x\n",
2819 buf[1]));
2820 ret = QLA_ERROR;
2821 goto exit_boot_info_free;
2822 }
2823
2824 /* get primary valid target index */
2825 if (buf[2] & BIT_7)
2826 ddb_index[0] = buf[2] & 0x7f;
2827 else
2828 ddb_index[0] = 0;
2829
2830 /* get secondary valid target index */
2831 if (buf[11] & BIT_7)
2832 ddb_index[1] = buf[11] & 0x7f;
2833 else
2834 ddb_index[1] = 1;
2835
2836 } else {
2837 ret = QLA_ERROR;
2838 goto exit_boot_info;
2839 }
2840
2841 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Primary target ID %d, Secondary"
2842 " target ID %d\n", __func__, ddb_index[0],
2843 ddb_index[1]));
2844
2845exit_boot_info_free:
2846 dma_free_coherent(&ha->pdev->dev, size, buf, buf_dma);
2847exit_boot_info:
2848 return ret;
2849}
2850
2851static int qla4xxx_get_boot_target(struct scsi_qla_host *ha,
2852 struct ql4_boot_session_info *boot_sess,
2853 uint16_t ddb_index)
2854{
2855 struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
2856 struct dev_db_entry *fw_ddb_entry;
2857 dma_addr_t fw_ddb_entry_dma;
2858 uint16_t idx;
2859 uint16_t options;
2860 int ret = QLA_SUCCESS;
2861
2862 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
2863 &fw_ddb_entry_dma, GFP_KERNEL);
2864 if (!fw_ddb_entry) {
2865 DEBUG2(ql4_printk(KERN_ERR, ha,
2866 "%s: Unable to allocate dma buffer.\n",
2867 __func__));
2868 ret = QLA_ERROR;
2869 return ret;
2870 }
2871
2872 if (qla4xxx_bootdb_by_index(ha, fw_ddb_entry,
2873 fw_ddb_entry_dma, ddb_index)) {
2874 DEBUG2(ql4_printk(KERN_ERR, ha,
2875 "%s: Flash DDB read Failed\n", __func__));
2876 ret = QLA_ERROR;
2877 goto exit_boot_target;
2878 }
2879
2880 /* Update target name and IP from DDB */
2881 memcpy(boot_sess->target_name, fw_ddb_entry->iscsi_name,
2882 min(sizeof(boot_sess->target_name),
2883 sizeof(fw_ddb_entry->iscsi_name)));
2884
2885 options = le16_to_cpu(fw_ddb_entry->options);
2886 if (options & DDB_OPT_IPV6_DEVICE) {
2887 memcpy(&boot_conn->dest_ipaddr.ip_address,
2888 &fw_ddb_entry->ip_addr[0], IPv6_ADDR_LEN);
2889 } else {
2890 boot_conn->dest_ipaddr.ip_type = 0x1;
2891 memcpy(&boot_conn->dest_ipaddr.ip_address,
2892 &fw_ddb_entry->ip_addr[0], IP_ADDR_LEN);
2893 }
2894
2895 boot_conn->dest_port = le16_to_cpu(fw_ddb_entry->port);
2896
2897 /* update chap information */
2898 idx = __le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
2899
2900 if (BIT_7 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
2901
2902 DEBUG2(ql4_printk(KERN_INFO, ha, "Setting chap\n"));
2903
2904 ret = qla4xxx_get_chap(ha, (char *)&boot_conn->chap.
2905 target_chap_name,
2906 (char *)&boot_conn->chap.target_secret,
2907 idx);
2908 if (ret) {
2909 ql4_printk(KERN_ERR, ha, "Failed to set chap\n");
2910 ret = QLA_ERROR;
2911 goto exit_boot_target;
2912 }
2913
2914 boot_conn->chap.target_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
2915 boot_conn->chap.target_secret_length = QL4_CHAP_MAX_SECRET_LEN;
2916 }
2917
2918 if (BIT_4 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
2919
2920 DEBUG2(ql4_printk(KERN_INFO, ha, "Setting BIDI chap\n"));
2921
2922 ret = qla4xxx_get_chap(ha, (char *)&boot_conn->chap.
2923 intr_chap_name,
2924 (char *)&boot_conn->chap.intr_secret,
2925 (idx + 1));
2926 if (ret) {
2927 ql4_printk(KERN_ERR, ha, "Failed to set BIDI chap\n");
2928 ret = QLA_ERROR;
2929 goto exit_boot_target;
2930 }
2931
2932 boot_conn->chap.intr_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
2933 boot_conn->chap.intr_secret_length = QL4_CHAP_MAX_SECRET_LEN;
2934 }
2935
2936exit_boot_target:
2937 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
2938 fw_ddb_entry, fw_ddb_entry_dma);
2939 return ret;
2940}
2941
2942static int qla4xxx_get_boot_info(struct scsi_qla_host *ha)
2943{
2944 uint16_t ddb_index[2];
2945 int ret = QLA_SUCCESS;
2946
2947 memset(ddb_index, 0, sizeof(ddb_index));
2948 ret = get_fw_boot_info(ha, ddb_index);
2949 if (ret != QLA_SUCCESS) {
2950 DEBUG2(ql4_printk(KERN_ERR, ha,
2951 "%s: Failed to set boot info.\n", __func__));
2952 return ret;
2953 }
2954
2955 ret = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_pri_sess),
2956 ddb_index[0]);
2957 if (ret != QLA_SUCCESS) {
2958 DEBUG2(ql4_printk(KERN_ERR, ha, "%s: Failed to get "
2959 "primary target\n", __func__));
2960 }
2961
2962 ret = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_sec_sess),
2963 ddb_index[1]);
2964 if (ret != QLA_SUCCESS) {
2965 DEBUG2(ql4_printk(KERN_ERR, ha, "%s: Failed to get "
2966 "secondary target\n", __func__));
2967 }
2968 return ret;
2969}
2970
2971static int qla4xxx_setup_boot_info(struct scsi_qla_host *ha)
2972{
2973 struct iscsi_boot_kobj *boot_kobj;
2974
2975 if (qla4xxx_get_boot_info(ha) != QLA_SUCCESS)
2976 return 0;
2977
2978 ha->boot_kset = iscsi_boot_create_host_kset(ha->host->host_no);
2979 if (!ha->boot_kset)
2980 goto kset_free;
2981
2982 if (!scsi_host_get(ha->host))
2983 goto kset_free;
2984 boot_kobj = iscsi_boot_create_target(ha->boot_kset, 0, ha,
2985 qla4xxx_show_boot_tgt_pri_info,
2986 qla4xxx_tgt_get_attr_visibility,
2987 qla4xxx_boot_release);
2988 if (!boot_kobj)
2989 goto put_host;
2990
2991 if (!scsi_host_get(ha->host))
2992 goto kset_free;
2993 boot_kobj = iscsi_boot_create_target(ha->boot_kset, 1, ha,
2994 qla4xxx_show_boot_tgt_sec_info,
2995 qla4xxx_tgt_get_attr_visibility,
2996 qla4xxx_boot_release);
2997 if (!boot_kobj)
2998 goto put_host;
2999
3000 if (!scsi_host_get(ha->host))
3001 goto kset_free;
3002 boot_kobj = iscsi_boot_create_initiator(ha->boot_kset, 0, ha,
3003 qla4xxx_show_boot_ini_info,
3004 qla4xxx_ini_get_attr_visibility,
3005 qla4xxx_boot_release);
3006 if (!boot_kobj)
3007 goto put_host;
3008
3009 if (!scsi_host_get(ha->host))
3010 goto kset_free;
3011 boot_kobj = iscsi_boot_create_ethernet(ha->boot_kset, 0, ha,
3012 qla4xxx_show_boot_eth_info,
3013 qla4xxx_eth_get_attr_visibility,
3014 qla4xxx_boot_release);
3015 if (!boot_kobj)
3016 goto put_host;
3017
3018 return 0;
3019
3020put_host:
3021 scsi_host_put(ha->host);
3022kset_free:
3023 iscsi_boot_destroy_kset(ha->boot_kset);
3024 return -ENOMEM;
3025}
3026
David Somayajuluafaf5a22006-09-19 10:28:00 -07003027/**
3028 * qla4xxx_probe_adapter - callback function to probe HBA
3029 * @pdev: pointer to pci_dev structure
3030 * @pci_device_id: pointer to pci_device entry
3031 *
3032 * This routine will probe for Qlogic 4xxx iSCSI host adapters.
3033 * It returns zero if successful. It also initializes all data necessary for
3034 * the driver.
3035 **/
3036static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
3037 const struct pci_device_id *ent)
3038{
3039 int ret = -ENODEV, status;
3040 struct Scsi_Host *host;
3041 struct scsi_qla_host *ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003042 uint8_t init_retry_count = 0;
3043 char buf[34];
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303044 struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
Prasanna Mumbaif9880e72011-03-21 03:34:26 -07003045 uint32_t dev_state;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003046
3047 if (pci_enable_device(pdev))
3048 return -1;
3049
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003050 host = iscsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha), 0);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003051 if (host == NULL) {
3052 printk(KERN_WARNING
3053 "qla4xxx: Couldn't allocate host from scsi layer!\n");
3054 goto probe_disable_device;
3055 }
3056
3057 /* Clear our data area */
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003058 ha = to_qla_host(host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003059 memset(ha, 0, sizeof(*ha));
3060
3061 /* Save the information from PCI BIOS. */
3062 ha->pdev = pdev;
3063 ha->host = host;
3064 ha->host_no = host->host_no;
3065
Lalit Chandivade2232be02010-07-30 14:38:47 +05303066 pci_enable_pcie_error_reporting(pdev);
3067
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303068 /* Setup Runtime configurable options */
3069 if (is_qla8022(ha)) {
3070 ha->isp_ops = &qla4_8xxx_isp_ops;
3071 rwlock_init(&ha->hw_lock);
3072 ha->qdr_sn_window = -1;
3073 ha->ddr_mn_window = -1;
3074 ha->curr_window = 255;
3075 ha->func_num = PCI_FUNC(ha->pdev->devfn);
3076 nx_legacy_intr = &legacy_intr[ha->func_num];
3077 ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
3078 ha->nx_legacy_intr.tgt_status_reg =
3079 nx_legacy_intr->tgt_status_reg;
3080 ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
3081 ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
3082 } else {
3083 ha->isp_ops = &qla4xxx_isp_ops;
3084 }
3085
Lalit Chandivade2232be02010-07-30 14:38:47 +05303086 /* Set EEH reset type to fundamental if required by hba */
3087 if (is_qla8022(ha))
3088 pdev->needs_freset = 1;
3089
David Somayajuluafaf5a22006-09-19 10:28:00 -07003090 /* Configure PCI I/O space. */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303091 ret = ha->isp_ops->iospace_config(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003092 if (ret)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303093 goto probe_failed_ioconfig;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003094
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303095 ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
David Somayajuluafaf5a22006-09-19 10:28:00 -07003096 pdev->device, pdev->irq, ha->reg);
3097
3098 qla4xxx_config_dma_addressing(ha);
3099
3100 /* Initialize lists and spinlocks. */
David Somayajuluafaf5a22006-09-19 10:28:00 -07003101 INIT_LIST_HEAD(&ha->free_srb_q);
3102
3103 mutex_init(&ha->mbox_sem);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303104 init_completion(&ha->mbx_intr_comp);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003105
3106 spin_lock_init(&ha->hardware_lock);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003107
3108 /* Allocate dma buffers */
3109 if (qla4xxx_mem_alloc(ha)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303110 ql4_printk(KERN_WARNING, ha,
3111 "[ERROR] Failed to allocate memory for adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07003112
3113 ret = -ENOMEM;
3114 goto probe_failed;
3115 }
3116
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003117 host->cmd_per_lun = 3;
3118 host->max_channel = 0;
3119 host->max_lun = MAX_LUNS - 1;
3120 host->max_id = MAX_TARGETS;
3121 host->max_cmd_len = IOCB_MAX_CDB_LEN;
3122 host->can_queue = MAX_SRBS ;
3123 host->transportt = qla4xxx_scsi_transport;
3124
3125 ret = scsi_init_shared_tag_map(host, MAX_SRBS);
3126 if (ret) {
3127 ql4_printk(KERN_WARNING, ha,
3128 "%s: scsi_init_shared_tag_map failed\n", __func__);
3129 goto probe_failed;
3130 }
3131
3132 pci_set_drvdata(pdev, ha);
3133
3134 ret = scsi_add_host(host, &pdev->dev);
3135 if (ret)
3136 goto probe_failed;
3137
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303138 if (is_qla8022(ha))
3139 (void) qla4_8xxx_get_flash_info(ha);
3140
David Somayajuluafaf5a22006-09-19 10:28:00 -07003141 /*
3142 * Initialize the Host adapter request/response queues and
3143 * firmware
3144 * NOTE: interrupts enabled upon successful completion
3145 */
Manish Rangankar0e7e8502011-07-25 13:48:54 -05003146 status = qla4xxx_initialize_adapter(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303147 while ((!test_bit(AF_ONLINE, &ha->flags)) &&
3148 init_retry_count++ < MAX_INIT_RETRIES) {
Prasanna Mumbaif9880e72011-03-21 03:34:26 -07003149
3150 if (is_qla8022(ha)) {
3151 qla4_8xxx_idc_lock(ha);
3152 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
3153 qla4_8xxx_idc_unlock(ha);
3154 if (dev_state == QLA82XX_DEV_FAILED) {
3155 ql4_printk(KERN_WARNING, ha, "%s: don't retry "
3156 "initialize adapter. H/W is in failed state\n",
3157 __func__);
3158 break;
3159 }
3160 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07003161 DEBUG2(printk("scsi: %s: retrying adapter initialization "
3162 "(%d)\n", __func__, init_retry_count));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303163
3164 if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
3165 continue;
3166
Manish Rangankar0e7e8502011-07-25 13:48:54 -05003167 status = qla4xxx_initialize_adapter(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003168 }
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303169
3170 if (!test_bit(AF_ONLINE, &ha->flags)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303171 ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07003172
Lalit Chandivadefe998522010-12-02 22:12:36 -08003173 if (is_qla8022(ha) && ql4xdontresethba) {
3174 /* Put the device in failed state. */
3175 DEBUG2(printk(KERN_ERR "HW STATE: FAILED\n"));
3176 qla4_8xxx_idc_lock(ha);
3177 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
3178 QLA82XX_DEV_FAILED);
3179 qla4_8xxx_idc_unlock(ha);
3180 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07003181 ret = -ENODEV;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003182 goto remove_host;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003183 }
3184
David Somayajuluafaf5a22006-09-19 10:28:00 -07003185 /* Startup the kernel thread for this host adapter. */
3186 DEBUG2(printk("scsi: %s: Starting kernel thread for "
3187 "qla4xxx_dpc\n", __func__));
3188 sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
3189 ha->dpc_thread = create_singlethread_workqueue(buf);
3190 if (!ha->dpc_thread) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303191 ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07003192 ret = -ENODEV;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003193 goto remove_host;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003194 }
David Howellsc4028952006-11-22 14:57:56 +00003195 INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003196
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003197 sprintf(buf, "qla4xxx_%lu_task", ha->host_no);
3198 ha->task_wq = alloc_workqueue(buf, WQ_MEM_RECLAIM, 1);
3199 if (!ha->task_wq) {
3200 ql4_printk(KERN_WARNING, ha, "Unable to start task thread!\n");
3201 ret = -ENODEV;
3202 goto remove_host;
3203 }
3204
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303205 /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
3206 * (which is called indirectly by qla4xxx_initialize_adapter),
3207 * so that irqs will be registered after crbinit but before
3208 * mbx_intr_enable.
3209 */
3210 if (!is_qla8022(ha)) {
3211 ret = qla4xxx_request_irqs(ha);
3212 if (ret) {
3213 ql4_printk(KERN_WARNING, ha, "Failed to reserve "
3214 "interrupt %d already in use.\n", pdev->irq);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003215 goto remove_host;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303216 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07003217 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07003218
Lalit Chandivade2232be02010-07-30 14:38:47 +05303219 pci_save_state(ha->pdev);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303220 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003221
3222 /* Start timer thread. */
3223 qla4xxx_start_timer(ha, qla4xxx_timer, 1);
3224
3225 set_bit(AF_INIT_DONE, &ha->flags);
3226
David Somayajuluafaf5a22006-09-19 10:28:00 -07003227 printk(KERN_INFO
3228 " QLogic iSCSI HBA Driver version: %s\n"
3229 " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
3230 qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
3231 ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
3232 ha->patch_number, ha->build_number);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05003233
Manish Rangankar2a991c22011-07-25 13:48:55 -05003234 if (qla4xxx_setup_boot_info(ha))
3235 ql4_printk(KERN_ERR, ha, "%s:ISCSI boot info setup failed\n",
3236 __func__);
3237
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05003238 qla4xxx_create_ifaces(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003239 return 0;
3240
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003241remove_host:
3242 scsi_remove_host(ha->host);
3243
David Somayajuluafaf5a22006-09-19 10:28:00 -07003244probe_failed:
3245 qla4xxx_free_adapter(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303246
3247probe_failed_ioconfig:
Lalit Chandivade2232be02010-07-30 14:38:47 +05303248 pci_disable_pcie_error_reporting(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003249 scsi_host_put(ha->host);
3250
3251probe_disable_device:
3252 pci_disable_device(pdev);
3253
3254 return ret;
3255}
3256
3257/**
Karen Higgins7eece5a2011-03-21 03:34:29 -07003258 * qla4xxx_prevent_other_port_reinit - prevent other port from re-initialize
3259 * @ha: pointer to adapter structure
3260 *
3261 * Mark the other ISP-4xxx port to indicate that the driver is being removed,
3262 * so that the other port will not re-initialize while in the process of
3263 * removing the ha due to driver unload or hba hotplug.
3264 **/
3265static void qla4xxx_prevent_other_port_reinit(struct scsi_qla_host *ha)
3266{
3267 struct scsi_qla_host *other_ha = NULL;
3268 struct pci_dev *other_pdev = NULL;
3269 int fn = ISP4XXX_PCI_FN_2;
3270
3271 /*iscsi function numbers for ISP4xxx is 1 and 3*/
3272 if (PCI_FUNC(ha->pdev->devfn) & BIT_1)
3273 fn = ISP4XXX_PCI_FN_1;
3274
3275 other_pdev =
3276 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
3277 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
3278 fn));
3279
3280 /* Get other_ha if other_pdev is valid and state is enable*/
3281 if (other_pdev) {
3282 if (atomic_read(&other_pdev->enable_cnt)) {
3283 other_ha = pci_get_drvdata(other_pdev);
3284 if (other_ha) {
3285 set_bit(AF_HA_REMOVAL, &other_ha->flags);
3286 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: "
3287 "Prevent %s reinit\n", __func__,
3288 dev_name(&other_ha->pdev->dev)));
3289 }
3290 }
3291 pci_dev_put(other_pdev);
3292 }
3293}
3294
3295/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07003296 * qla4xxx_remove_adapter - calback function to remove adapter.
3297 * @pci_dev: PCI device pointer
3298 **/
3299static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
3300{
3301 struct scsi_qla_host *ha;
3302
3303 ha = pci_get_drvdata(pdev);
3304
Karen Higgins7eece5a2011-03-21 03:34:29 -07003305 if (!is_qla8022(ha))
3306 qla4xxx_prevent_other_port_reinit(ha);
David C Somayajulubee4fe82007-05-23 18:03:32 -07003307
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05003308 /* destroy iface from sysfs */
3309 qla4xxx_destroy_ifaces(ha);
3310
Manish Rangankar2a991c22011-07-25 13:48:55 -05003311 if (ha->boot_kset)
3312 iscsi_boot_destroy_kset(ha->boot_kset);
3313
David Somayajuluafaf5a22006-09-19 10:28:00 -07003314 scsi_remove_host(ha->host);
3315
3316 qla4xxx_free_adapter(ha);
3317
3318 scsi_host_put(ha->host);
3319
Lalit Chandivade2232be02010-07-30 14:38:47 +05303320 pci_disable_pcie_error_reporting(pdev);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303321 pci_disable_device(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003322 pci_set_drvdata(pdev, NULL);
3323}
3324
3325/**
3326 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
3327 * @ha: HA context
3328 *
3329 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
3330 * supported addressing method.
3331 */
Adrian Bunk47975472007-04-26 00:35:16 -07003332static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07003333{
3334 int retval;
3335
3336 /* Update our PCI device dma_mask for full 64 bit mask */
Yang Hongyang6a355282009-04-06 19:01:13 -07003337 if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
3338 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07003339 dev_dbg(&ha->pdev->dev,
3340 "Failed to set 64 bit PCI consistent mask; "
3341 "using 32 bit.\n");
3342 retval = pci_set_consistent_dma_mask(ha->pdev,
Yang Hongyang284901a2009-04-06 19:01:15 -07003343 DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07003344 }
3345 } else
Yang Hongyang284901a2009-04-06 19:01:15 -07003346 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07003347}
3348
3349static int qla4xxx_slave_alloc(struct scsi_device *sdev)
3350{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003351 struct iscsi_cls_session *cls_sess;
3352 struct iscsi_session *sess;
3353 struct ddb_entry *ddb;
Vikas Chaudhary8bb40332011-03-21 03:34:31 -07003354 int queue_depth = QL4_DEF_QDEPTH;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003355
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003356 cls_sess = starget_to_session(sdev->sdev_target);
3357 sess = cls_sess->dd_data;
3358 ddb = sess->dd_data;
3359
David Somayajuluafaf5a22006-09-19 10:28:00 -07003360 sdev->hostdata = ddb;
3361 sdev->tagged_supported = 1;
Vikas Chaudhary8bb40332011-03-21 03:34:31 -07003362
3363 if (ql4xmaxqdepth != 0 && ql4xmaxqdepth <= 0xffffU)
3364 queue_depth = ql4xmaxqdepth;
3365
3366 scsi_activate_tcq(sdev, queue_depth);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003367 return 0;
3368}
3369
3370static int qla4xxx_slave_configure(struct scsi_device *sdev)
3371{
3372 sdev->tagged_supported = 1;
3373 return 0;
3374}
3375
3376static void qla4xxx_slave_destroy(struct scsi_device *sdev)
3377{
3378 scsi_deactivate_tcq(sdev, 1);
3379}
3380
3381/**
3382 * qla4xxx_del_from_active_array - returns an active srb
3383 * @ha: Pointer to host adapter structure.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02003384 * @index: index into the active_array
David Somayajuluafaf5a22006-09-19 10:28:00 -07003385 *
3386 * This routine removes and returns the srb at the specified index
3387 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303388struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
3389 uint32_t index)
David Somayajuluafaf5a22006-09-19 10:28:00 -07003390{
3391 struct srb *srb = NULL;
Vikas Chaudhary53698872010-04-28 11:41:59 +05303392 struct scsi_cmnd *cmd = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003393
Vikas Chaudhary53698872010-04-28 11:41:59 +05303394 cmd = scsi_host_find_tag(ha->host, index);
3395 if (!cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07003396 return srb;
3397
Vikas Chaudhary53698872010-04-28 11:41:59 +05303398 srb = (struct srb *)CMD_SP(cmd);
3399 if (!srb)
David Somayajuluafaf5a22006-09-19 10:28:00 -07003400 return srb;
3401
3402 /* update counters */
3403 if (srb->flags & SRB_DMA_VALID) {
3404 ha->req_q_count += srb->iocb_cnt;
3405 ha->iocb_cnt -= srb->iocb_cnt;
3406 if (srb->cmd)
Vikas Chaudhary53698872010-04-28 11:41:59 +05303407 srb->cmd->host_scribble =
3408 (unsigned char *)(unsigned long) MAX_SRBS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003409 }
3410 return srb;
3411}
3412
3413/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07003414 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05303415 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07003416 * @cmd: Scsi Command to wait on.
3417 *
3418 * This routine waits for the command to be returned by the Firmware
3419 * for some max time.
3420 **/
3421static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
3422 struct scsi_cmnd *cmd)
3423{
3424 int done = 0;
3425 struct srb *rp;
3426 uint32_t max_wait_time = EH_WAIT_CMD_TOV;
Lalit Chandivade2232be02010-07-30 14:38:47 +05303427 int ret = SUCCESS;
3428
3429 /* Dont wait on command if PCI error is being handled
3430 * by PCI AER driver
3431 */
3432 if (unlikely(pci_channel_offline(ha->pdev)) ||
3433 (test_bit(AF_EEH_BUSY, &ha->flags))) {
3434 ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
3435 ha->host_no, __func__);
3436 return ret;
3437 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07003438
3439 do {
3440 /* Checking to see if its returned to OS */
Vikas Chaudhary53698872010-04-28 11:41:59 +05303441 rp = (struct srb *) CMD_SP(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003442 if (rp == NULL) {
3443 done++;
3444 break;
3445 }
3446
3447 msleep(2000);
3448 } while (max_wait_time--);
3449
3450 return done;
3451}
3452
3453/**
3454 * qla4xxx_wait_for_hba_online - waits for HBA to come online
3455 * @ha: Pointer to host adapter structure
3456 **/
3457static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
3458{
3459 unsigned long wait_online;
3460
Vikas Chaudharyf581a3f2010-10-06 22:47:48 -07003461 wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003462 while (time_before(jiffies, wait_online)) {
3463
3464 if (adapter_up(ha))
3465 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003466
3467 msleep(2000);
3468 }
3469
3470 return QLA_ERROR;
3471}
3472
3473/**
Mike Christiece545032008-02-29 18:25:20 -06003474 * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02003475 * @ha: pointer to HBA
David Somayajuluafaf5a22006-09-19 10:28:00 -07003476 * @t: target id
3477 * @l: lun id
3478 *
3479 * This function waits for all outstanding commands to a lun to complete. It
3480 * returns 0 if all pending commands are returned and 1 otherwise.
3481 **/
Mike Christiece545032008-02-29 18:25:20 -06003482static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
3483 struct scsi_target *stgt,
3484 struct scsi_device *sdev)
David Somayajuluafaf5a22006-09-19 10:28:00 -07003485{
3486 int cnt;
3487 int status = 0;
3488 struct scsi_cmnd *cmd;
3489
3490 /*
Mike Christiece545032008-02-29 18:25:20 -06003491 * Waiting for all commands for the designated target or dev
3492 * in the active array
David Somayajuluafaf5a22006-09-19 10:28:00 -07003493 */
3494 for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
3495 cmd = scsi_host_find_tag(ha->host, cnt);
Mike Christiece545032008-02-29 18:25:20 -06003496 if (cmd && stgt == scsi_target(cmd->device) &&
3497 (!sdev || sdev == cmd->device)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07003498 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
3499 status++;
3500 break;
3501 }
3502 }
3503 }
3504 return status;
3505}
3506
3507/**
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05303508 * qla4xxx_eh_abort - callback for abort task.
3509 * @cmd: Pointer to Linux's SCSI command structure
3510 *
3511 * This routine is called by the Linux OS to abort the specified
3512 * command.
3513 **/
3514static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
3515{
3516 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
3517 unsigned int id = cmd->device->id;
3518 unsigned int lun = cmd->device->lun;
Mike Christie92b3e5b2010-10-06 22:51:17 -07003519 unsigned long flags;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05303520 struct srb *srb = NULL;
3521 int ret = SUCCESS;
3522 int wait = 0;
3523
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303524 ql4_printk(KERN_INFO, ha,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04003525 "scsi%ld:%d:%d: Abort command issued cmd=%p\n",
3526 ha->host_no, id, lun, cmd);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05303527
Mike Christie92b3e5b2010-10-06 22:51:17 -07003528 spin_lock_irqsave(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05303529 srb = (struct srb *) CMD_SP(cmd);
Mike Christie92b3e5b2010-10-06 22:51:17 -07003530 if (!srb) {
3531 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05303532 return SUCCESS;
Mike Christie92b3e5b2010-10-06 22:51:17 -07003533 }
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05303534 kref_get(&srb->srb_ref);
Mike Christie92b3e5b2010-10-06 22:51:17 -07003535 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05303536
3537 if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
3538 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
3539 ha->host_no, id, lun));
3540 ret = FAILED;
3541 } else {
3542 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
3543 ha->host_no, id, lun));
3544 wait = 1;
3545 }
3546
3547 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
3548
3549 /* Wait for command to complete */
3550 if (wait) {
3551 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
3552 DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
3553 ha->host_no, id, lun));
3554 ret = FAILED;
3555 }
3556 }
3557
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303558 ql4_printk(KERN_INFO, ha,
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05303559 "scsi%ld:%d:%d: Abort command - %s\n",
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003560 ha->host_no, id, lun, (ret == SUCCESS) ? "succeeded" : "failed");
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05303561
3562 return ret;
3563}
3564
3565/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07003566 * qla4xxx_eh_device_reset - callback for target reset.
3567 * @cmd: Pointer to Linux's SCSI command structure
3568 *
3569 * This routine is called by the Linux OS to reset all luns on the
3570 * specified target.
3571 **/
3572static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
3573{
3574 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
3575 struct ddb_entry *ddb_entry = cmd->device->hostdata;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003576 int ret = FAILED, stat;
3577
Karen Higgins612f7342009-07-15 15:03:01 -05003578 if (!ddb_entry)
David Somayajuluafaf5a22006-09-19 10:28:00 -07003579 return ret;
3580
Mike Christiec01be6d2010-07-22 16:59:49 +05303581 ret = iscsi_block_scsi_eh(cmd);
3582 if (ret)
3583 return ret;
3584 ret = FAILED;
3585
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303586 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07003587 "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
3588 cmd->device->channel, cmd->device->id, cmd->device->lun);
3589
3590 DEBUG2(printk(KERN_INFO
3591 "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
3592 "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
Jens Axboe242f9dc2008-09-14 05:55:09 -07003593 cmd, jiffies, cmd->request->timeout / HZ,
David Somayajuluafaf5a22006-09-19 10:28:00 -07003594 ha->dpc_flags, cmd->result, cmd->allowed));
3595
3596 /* FIXME: wait for hba to go online */
3597 stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
3598 if (stat != QLA_SUCCESS) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303599 ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003600 goto eh_dev_reset_done;
3601 }
3602
Mike Christiece545032008-02-29 18:25:20 -06003603 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
3604 cmd->device)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303605 ql4_printk(KERN_INFO, ha,
Mike Christiece545032008-02-29 18:25:20 -06003606 "DEVICE RESET FAILED - waiting for "
3607 "commands.\n");
3608 goto eh_dev_reset_done;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003609 }
3610
David C Somayajulu9d562912008-03-19 11:23:03 -07003611 /* Send marker. */
3612 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
3613 MM_LUN_RESET) != QLA_SUCCESS)
3614 goto eh_dev_reset_done;
3615
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303616 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07003617 "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
3618 ha->host_no, cmd->device->channel, cmd->device->id,
3619 cmd->device->lun);
3620
3621 ret = SUCCESS;
3622
3623eh_dev_reset_done:
3624
3625 return ret;
3626}
3627
3628/**
Mike Christiece545032008-02-29 18:25:20 -06003629 * qla4xxx_eh_target_reset - callback for target reset.
3630 * @cmd: Pointer to Linux's SCSI command structure
3631 *
3632 * This routine is called by the Linux OS to reset the target.
3633 **/
3634static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
3635{
3636 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
3637 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christiec01be6d2010-07-22 16:59:49 +05303638 int stat, ret;
Mike Christiece545032008-02-29 18:25:20 -06003639
3640 if (!ddb_entry)
3641 return FAILED;
3642
Mike Christiec01be6d2010-07-22 16:59:49 +05303643 ret = iscsi_block_scsi_eh(cmd);
3644 if (ret)
3645 return ret;
3646
Mike Christiece545032008-02-29 18:25:20 -06003647 starget_printk(KERN_INFO, scsi_target(cmd->device),
3648 "WARM TARGET RESET ISSUED.\n");
3649
3650 DEBUG2(printk(KERN_INFO
3651 "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
3652 "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
Jens Axboe242f9dc2008-09-14 05:55:09 -07003653 ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
Mike Christiece545032008-02-29 18:25:20 -06003654 ha->dpc_flags, cmd->result, cmd->allowed));
3655
3656 stat = qla4xxx_reset_target(ha, ddb_entry);
3657 if (stat != QLA_SUCCESS) {
3658 starget_printk(KERN_INFO, scsi_target(cmd->device),
3659 "WARM TARGET RESET FAILED.\n");
3660 return FAILED;
3661 }
3662
Mike Christiece545032008-02-29 18:25:20 -06003663 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
3664 NULL)) {
3665 starget_printk(KERN_INFO, scsi_target(cmd->device),
3666 "WARM TARGET DEVICE RESET FAILED - "
3667 "waiting for commands.\n");
3668 return FAILED;
3669 }
3670
David C Somayajulu9d562912008-03-19 11:23:03 -07003671 /* Send marker. */
3672 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
3673 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
3674 starget_printk(KERN_INFO, scsi_target(cmd->device),
3675 "WARM TARGET DEVICE RESET FAILED - "
3676 "marker iocb failed.\n");
3677 return FAILED;
3678 }
3679
Mike Christiece545032008-02-29 18:25:20 -06003680 starget_printk(KERN_INFO, scsi_target(cmd->device),
3681 "WARM TARGET RESET SUCCEEDED.\n");
3682 return SUCCESS;
3683}
3684
3685/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07003686 * qla4xxx_eh_host_reset - kernel callback
3687 * @cmd: Pointer to Linux's SCSI command structure
3688 *
3689 * This routine is invoked by the Linux kernel to perform fatal error
3690 * recovery on the specified adapter.
3691 **/
3692static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
3693{
3694 int return_status = FAILED;
3695 struct scsi_qla_host *ha;
3696
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003697 ha = to_qla_host(cmd->device->host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003698
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303699 if (ql4xdontresethba) {
3700 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
3701 ha->host_no, __func__));
3702 return FAILED;
3703 }
3704
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303705 ql4_printk(KERN_INFO, ha,
Karen Higginsdca05c42009-07-15 15:03:00 -05003706 "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
David Somayajuluafaf5a22006-09-19 10:28:00 -07003707 cmd->device->channel, cmd->device->id, cmd->device->lun);
3708
3709 if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
3710 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
3711 "DEAD.\n", ha->host_no, cmd->device->channel,
3712 __func__));
3713
3714 return FAILED;
3715 }
3716
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303717 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
3718 if (is_qla8022(ha))
3719 set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
3720 else
3721 set_bit(DPC_RESET_HA, &ha->dpc_flags);
3722 }
Mike Christie50a29ae2008-03-04 13:26:53 -06003723
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303724 if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
David Somayajuluafaf5a22006-09-19 10:28:00 -07003725 return_status = SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003726
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303727 ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003728 return_status == FAILED ? "FAILED" : "SUCCEEDED");
David Somayajuluafaf5a22006-09-19 10:28:00 -07003729
3730 return return_status;
3731}
3732
Lalit Chandivade2232be02010-07-30 14:38:47 +05303733/* PCI AER driver recovers from all correctable errors w/o
3734 * driver intervention. For uncorrectable errors PCI AER
3735 * driver calls the following device driver's callbacks
3736 *
3737 * - Fatal Errors - link_reset
3738 * - Non-Fatal Errors - driver's pci_error_detected() which
3739 * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
3740 *
3741 * PCI AER driver calls
3742 * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
3743 * returns RECOVERED or NEED_RESET if fw_hung
3744 * NEED_RESET - driver's slot_reset()
3745 * DISCONNECT - device is dead & cannot recover
3746 * RECOVERED - driver's pci_resume()
3747 */
3748static pci_ers_result_t
3749qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
3750{
3751 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
3752
3753 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
3754 ha->host_no, __func__, state);
3755
3756 if (!is_aer_supported(ha))
3757 return PCI_ERS_RESULT_NONE;
3758
3759 switch (state) {
3760 case pci_channel_io_normal:
3761 clear_bit(AF_EEH_BUSY, &ha->flags);
3762 return PCI_ERS_RESULT_CAN_RECOVER;
3763 case pci_channel_io_frozen:
3764 set_bit(AF_EEH_BUSY, &ha->flags);
3765 qla4xxx_mailbox_premature_completion(ha);
3766 qla4xxx_free_irqs(ha);
3767 pci_disable_device(pdev);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07003768 /* Return back all IOs */
3769 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
Lalit Chandivade2232be02010-07-30 14:38:47 +05303770 return PCI_ERS_RESULT_NEED_RESET;
3771 case pci_channel_io_perm_failure:
3772 set_bit(AF_EEH_BUSY, &ha->flags);
3773 set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
3774 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
3775 return PCI_ERS_RESULT_DISCONNECT;
3776 }
3777 return PCI_ERS_RESULT_NEED_RESET;
3778}
3779
3780/**
3781 * qla4xxx_pci_mmio_enabled() gets called if
3782 * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
3783 * and read/write to the device still works.
3784 **/
3785static pci_ers_result_t
3786qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
3787{
3788 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
3789
3790 if (!is_aer_supported(ha))
3791 return PCI_ERS_RESULT_NONE;
3792
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07003793 return PCI_ERS_RESULT_RECOVERED;
Lalit Chandivade2232be02010-07-30 14:38:47 +05303794}
3795
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07003796static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
Lalit Chandivade2232be02010-07-30 14:38:47 +05303797{
3798 uint32_t rval = QLA_ERROR;
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07003799 uint32_t ret = 0;
Lalit Chandivade2232be02010-07-30 14:38:47 +05303800 int fn;
3801 struct pci_dev *other_pdev = NULL;
3802
3803 ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
3804
3805 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
3806
3807 if (test_bit(AF_ONLINE, &ha->flags)) {
3808 clear_bit(AF_ONLINE, &ha->flags);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003809 clear_bit(AF_LINK_UP, &ha->flags);
3810 iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
Lalit Chandivade2232be02010-07-30 14:38:47 +05303811 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
Lalit Chandivade2232be02010-07-30 14:38:47 +05303812 }
3813
3814 fn = PCI_FUNC(ha->pdev->devfn);
3815 while (fn > 0) {
3816 fn--;
3817 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
3818 "func %x\n", ha->host_no, __func__, fn);
3819 /* Get the pci device given the domain, bus,
3820 * slot/function number */
3821 other_pdev =
3822 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
3823 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
3824 fn));
3825
3826 if (!other_pdev)
3827 continue;
3828
3829 if (atomic_read(&other_pdev->enable_cnt)) {
3830 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
3831 "func in enabled state%x\n", ha->host_no,
3832 __func__, fn);
3833 pci_dev_put(other_pdev);
3834 break;
3835 }
3836 pci_dev_put(other_pdev);
3837 }
3838
3839 /* The first function on the card, the reset owner will
3840 * start & initialize the firmware. The other functions
3841 * on the card will reset the firmware context
3842 */
3843 if (!fn) {
3844 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
3845 "0x%x is the owner\n", ha->host_no, __func__,
3846 ha->pdev->devfn);
3847
3848 qla4_8xxx_idc_lock(ha);
3849 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
3850 QLA82XX_DEV_COLD);
3851
3852 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
3853 QLA82XX_IDC_VERSION);
3854
3855 qla4_8xxx_idc_unlock(ha);
3856 clear_bit(AF_FW_RECOVERY, &ha->flags);
Manish Rangankar0e7e8502011-07-25 13:48:54 -05003857 rval = qla4xxx_initialize_adapter(ha);
Lalit Chandivade2232be02010-07-30 14:38:47 +05303858 qla4_8xxx_idc_lock(ha);
3859
3860 if (rval != QLA_SUCCESS) {
3861 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
3862 "FAILED\n", ha->host_no, __func__);
3863 qla4_8xxx_clear_drv_active(ha);
3864 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
3865 QLA82XX_DEV_FAILED);
3866 } else {
3867 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
3868 "READY\n", ha->host_no, __func__);
3869 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
3870 QLA82XX_DEV_READY);
3871 /* Clear driver state register */
3872 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
3873 qla4_8xxx_set_drv_active(ha);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07003874 ret = qla4xxx_request_irqs(ha);
3875 if (ret) {
3876 ql4_printk(KERN_WARNING, ha, "Failed to "
3877 "reserve interrupt %d already in use.\n",
3878 ha->pdev->irq);
3879 rval = QLA_ERROR;
3880 } else {
3881 ha->isp_ops->enable_intrs(ha);
3882 rval = QLA_SUCCESS;
3883 }
Lalit Chandivade2232be02010-07-30 14:38:47 +05303884 }
3885 qla4_8xxx_idc_unlock(ha);
3886 } else {
3887 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
3888 "the reset owner\n", ha->host_no, __func__,
3889 ha->pdev->devfn);
3890 if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
3891 QLA82XX_DEV_READY)) {
3892 clear_bit(AF_FW_RECOVERY, &ha->flags);
Manish Rangankar0e7e8502011-07-25 13:48:54 -05003893 rval = qla4xxx_initialize_adapter(ha);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07003894 if (rval == QLA_SUCCESS) {
3895 ret = qla4xxx_request_irqs(ha);
3896 if (ret) {
3897 ql4_printk(KERN_WARNING, ha, "Failed to"
3898 " reserve interrupt %d already in"
3899 " use.\n", ha->pdev->irq);
3900 rval = QLA_ERROR;
3901 } else {
3902 ha->isp_ops->enable_intrs(ha);
3903 rval = QLA_SUCCESS;
3904 }
3905 }
Lalit Chandivade2232be02010-07-30 14:38:47 +05303906 qla4_8xxx_idc_lock(ha);
3907 qla4_8xxx_set_drv_active(ha);
3908 qla4_8xxx_idc_unlock(ha);
3909 }
3910 }
3911 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
3912 return rval;
3913}
3914
3915static pci_ers_result_t
3916qla4xxx_pci_slot_reset(struct pci_dev *pdev)
3917{
3918 pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
3919 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
3920 int rc;
3921
3922 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
3923 ha->host_no, __func__);
3924
3925 if (!is_aer_supported(ha))
3926 return PCI_ERS_RESULT_NONE;
3927
3928 /* Restore the saved state of PCIe device -
3929 * BAR registers, PCI Config space, PCIX, MSI,
3930 * IOV states
3931 */
3932 pci_restore_state(pdev);
3933
3934 /* pci_restore_state() clears the saved_state flag of the device
3935 * save restored state which resets saved_state flag
3936 */
3937 pci_save_state(pdev);
3938
3939 /* Initialize device or resume if in suspended state */
3940 rc = pci_enable_device(pdev);
3941 if (rc) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003942 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Can't re-enable "
Lalit Chandivade2232be02010-07-30 14:38:47 +05303943 "device after reset\n", ha->host_no, __func__);
3944 goto exit_slot_reset;
3945 }
3946
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07003947 ha->isp_ops->disable_intrs(ha);
Lalit Chandivade2232be02010-07-30 14:38:47 +05303948
3949 if (is_qla8022(ha)) {
3950 if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
3951 ret = PCI_ERS_RESULT_RECOVERED;
3952 goto exit_slot_reset;
3953 } else
3954 goto exit_slot_reset;
3955 }
3956
3957exit_slot_reset:
3958 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
3959 "device after reset\n", ha->host_no, __func__, ret);
3960 return ret;
3961}
3962
3963static void
3964qla4xxx_pci_resume(struct pci_dev *pdev)
3965{
3966 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
3967 int ret;
3968
3969 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
3970 ha->host_no, __func__);
3971
3972 ret = qla4xxx_wait_for_hba_online(ha);
3973 if (ret != QLA_SUCCESS) {
3974 ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
3975 "resume I/O from slot/link_reset\n", ha->host_no,
3976 __func__);
3977 }
3978
3979 pci_cleanup_aer_uncorrect_error_status(pdev);
3980 clear_bit(AF_EEH_BUSY, &ha->flags);
3981}
3982
3983static struct pci_error_handlers qla4xxx_err_handler = {
3984 .error_detected = qla4xxx_pci_error_detected,
3985 .mmio_enabled = qla4xxx_pci_mmio_enabled,
3986 .slot_reset = qla4xxx_pci_slot_reset,
3987 .resume = qla4xxx_pci_resume,
3988};
3989
David Somayajuluafaf5a22006-09-19 10:28:00 -07003990static struct pci_device_id qla4xxx_pci_tbl[] = {
3991 {
3992 .vendor = PCI_VENDOR_ID_QLOGIC,
3993 .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
3994 .subvendor = PCI_ANY_ID,
3995 .subdevice = PCI_ANY_ID,
3996 },
3997 {
3998 .vendor = PCI_VENDOR_ID_QLOGIC,
3999 .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
4000 .subvendor = PCI_ANY_ID,
4001 .subdevice = PCI_ANY_ID,
4002 },
David C Somayajulud9150582006-11-15 17:38:40 -08004003 {
4004 .vendor = PCI_VENDOR_ID_QLOGIC,
4005 .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
4006 .subvendor = PCI_ANY_ID,
4007 .subdevice = PCI_ANY_ID,
4008 },
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304009 {
4010 .vendor = PCI_VENDOR_ID_QLOGIC,
4011 .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
4012 .subvendor = PCI_ANY_ID,
4013 .subdevice = PCI_ANY_ID,
4014 },
David Somayajuluafaf5a22006-09-19 10:28:00 -07004015 {0, 0},
4016};
4017MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
4018
Adrian Bunk47975472007-04-26 00:35:16 -07004019static struct pci_driver qla4xxx_pci_driver = {
David Somayajuluafaf5a22006-09-19 10:28:00 -07004020 .name = DRIVER_NAME,
4021 .id_table = qla4xxx_pci_tbl,
4022 .probe = qla4xxx_probe_adapter,
4023 .remove = qla4xxx_remove_adapter,
Lalit Chandivade2232be02010-07-30 14:38:47 +05304024 .err_handler = &qla4xxx_err_handler,
David Somayajuluafaf5a22006-09-19 10:28:00 -07004025};
4026
4027static int __init qla4xxx_module_init(void)
4028{
4029 int ret;
4030
4031 /* Allocate cache for SRBs. */
4032 srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09004033 SLAB_HWCACHE_ALIGN, NULL);
David Somayajuluafaf5a22006-09-19 10:28:00 -07004034 if (srb_cachep == NULL) {
4035 printk(KERN_ERR
4036 "%s: Unable to allocate SRB cache..."
4037 "Failing load!\n", DRIVER_NAME);
4038 ret = -ENOMEM;
4039 goto no_srp_cache;
4040 }
4041
4042 /* Derive version string. */
4043 strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
Andrew Vasquez11010fe2006-10-06 09:54:59 -07004044 if (ql4xextended_error_logging)
David Somayajuluafaf5a22006-09-19 10:28:00 -07004045 strcat(qla4xxx_version_str, "-debug");
4046
4047 qla4xxx_scsi_transport =
4048 iscsi_register_transport(&qla4xxx_iscsi_transport);
4049 if (!qla4xxx_scsi_transport){
4050 ret = -ENODEV;
4051 goto release_srb_cache;
4052 }
4053
David Somayajuluafaf5a22006-09-19 10:28:00 -07004054 ret = pci_register_driver(&qla4xxx_pci_driver);
4055 if (ret)
4056 goto unregister_transport;
4057
4058 printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
4059 return 0;
Doug Maxey5ae16db2006-10-05 23:50:07 -05004060
David Somayajuluafaf5a22006-09-19 10:28:00 -07004061unregister_transport:
4062 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
4063release_srb_cache:
4064 kmem_cache_destroy(srb_cachep);
4065no_srp_cache:
4066 return ret;
4067}
4068
4069static void __exit qla4xxx_module_exit(void)
4070{
David Somayajuluafaf5a22006-09-19 10:28:00 -07004071 pci_unregister_driver(&qla4xxx_pci_driver);
4072 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
4073 kmem_cache_destroy(srb_cachep);
4074}
4075
4076module_init(qla4xxx_module_init);
4077module_exit(qla4xxx_module_exit);
4078
4079MODULE_AUTHOR("QLogic Corporation");
4080MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
4081MODULE_LICENSE("GPL");
4082MODULE_VERSION(QLA4XXX_DRIVER_VERSION);