blob: ade1ba6b345181617d435eeb55961a62a456a7ba [file] [log] [blame]
David Somayajuluafaf5a22006-09-19 10:28:00 -07001/*
2 * QLogic iSCSI HBA Driver
Vikas Chaudharyc68cdbf2012-08-22 07:55:09 -04003 * Copyright (c) 2003-2012 QLogic Corporation
David Somayajuluafaf5a22006-09-19 10:28:00 -07004 *
5 * See LICENSE.qla4xxx for copyright and licensing details.
6 */
7#include <linux/moduleparam.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
Manish Rangankar2a991c22011-07-25 13:48:55 -05009#include <linux/blkdev.h>
10#include <linux/iscsi_boot_sysfs.h>
Mike Christie13483732011-12-01 21:38:41 -060011#include <linux/inet.h>
David Somayajuluafaf5a22006-09-19 10:28:00 -070012
13#include <scsi/scsi_tcq.h>
14#include <scsi/scsicam.h>
15
16#include "ql4_def.h"
David C Somayajulubee4fe82007-05-23 18:03:32 -070017#include "ql4_version.h"
18#include "ql4_glbl.h"
19#include "ql4_dbg.h"
20#include "ql4_inline.h"
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -040021#include "ql4_83xx.h"
David Somayajuluafaf5a22006-09-19 10:28:00 -070022
23/*
24 * Driver version
25 */
Adrian Bunk47975472007-04-26 00:35:16 -070026static char qla4xxx_version_str[40];
David Somayajuluafaf5a22006-09-19 10:28:00 -070027
28/*
29 * SRB allocation cache
30 */
Christoph Lametere18b8902006-12-06 20:33:20 -080031static struct kmem_cache *srb_cachep;
David Somayajuluafaf5a22006-09-19 10:28:00 -070032
33/*
34 * Module parameter information and variables
35 */
Vikas Chaudharya7380a62012-02-27 03:08:56 -080036static int ql4xdisablesysfsboot = 1;
Mike Christie13483732011-12-01 21:38:41 -060037module_param(ql4xdisablesysfsboot, int, S_IRUGO | S_IWUSR);
38MODULE_PARM_DESC(ql4xdisablesysfsboot,
Karen Higginsa4e8a712012-01-11 02:44:20 -080039 " Set to disable exporting boot targets to sysfs.\n"
40 "\t\t 0 - Export boot targets\n"
41 "\t\t 1 - Do not export boot targets (Default)");
Mike Christie13483732011-12-01 21:38:41 -060042
Vikas Chaudhary3573bfb2012-02-27 03:08:57 -080043int ql4xdontresethba;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053044module_param(ql4xdontresethba, int, S_IRUGO | S_IWUSR);
David Somayajuluafaf5a22006-09-19 10:28:00 -070045MODULE_PARM_DESC(ql4xdontresethba,
Karen Higginsa4e8a712012-01-11 02:44:20 -080046 " Don't reset the HBA for driver recovery.\n"
47 "\t\t 0 - It will reset HBA (Default)\n"
48 "\t\t 1 - It will NOT reset HBA");
David Somayajuluafaf5a22006-09-19 10:28:00 -070049
Karen Higginsa4e8a712012-01-11 02:44:20 -080050int ql4xextended_error_logging;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053051module_param(ql4xextended_error_logging, int, S_IRUGO | S_IWUSR);
Andrew Vasquez11010fe2006-10-06 09:54:59 -070052MODULE_PARM_DESC(ql4xextended_error_logging,
Karen Higginsa4e8a712012-01-11 02:44:20 -080053 " Option to enable extended error logging.\n"
54 "\t\t 0 - no logging (Default)\n"
55 "\t\t 2 - debug logging");
David Somayajuluafaf5a22006-09-19 10:28:00 -070056
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053057int ql4xenablemsix = 1;
58module_param(ql4xenablemsix, int, S_IRUGO|S_IWUSR);
59MODULE_PARM_DESC(ql4xenablemsix,
Karen Higginsa4e8a712012-01-11 02:44:20 -080060 " Set to enable MSI or MSI-X interrupt mechanism.\n"
61 "\t\t 0 = enable INTx interrupt mechanism.\n"
62 "\t\t 1 = enable MSI-X interrupt mechanism (Default).\n"
63 "\t\t 2 = enable MSI interrupt mechanism.");
David C Somayajulu477ffb92007-01-22 12:26:11 -080064
Mike Christied510d962008-07-11 19:50:33 -050065#define QL4_DEF_QDEPTH 32
Vikas Chaudhary8bb40332011-03-21 03:34:31 -070066static int ql4xmaxqdepth = QL4_DEF_QDEPTH;
67module_param(ql4xmaxqdepth, int, S_IRUGO | S_IWUSR);
68MODULE_PARM_DESC(ql4xmaxqdepth,
Karen Higginsa4e8a712012-01-11 02:44:20 -080069 " Maximum queue depth to report for target devices.\n"
70 "\t\t Default: 32.");
Mike Christied510d962008-07-11 19:50:33 -050071
Tej Parkashf7b4aa632012-04-30 04:12:19 -070072static int ql4xqfulltracking = 1;
73module_param(ql4xqfulltracking, int, S_IRUGO | S_IWUSR);
74MODULE_PARM_DESC(ql4xqfulltracking,
75 " Enable or disable dynamic tracking and adjustment of\n"
76 "\t\t scsi device queue depth.\n"
77 "\t\t 0 - Disable.\n"
78 "\t\t 1 - Enable. (Default)");
79
Vikas Chaudhary30387272011-03-21 03:34:32 -070080static int ql4xsess_recovery_tmo = QL4_SESS_RECOVERY_TMO;
81module_param(ql4xsess_recovery_tmo, int, S_IRUGO);
82MODULE_PARM_DESC(ql4xsess_recovery_tmo,
Vikas Chaudhary3573bfb2012-02-27 03:08:57 -080083 " Target Session Recovery Timeout.\n"
Karen Higginsa4e8a712012-01-11 02:44:20 -080084 "\t\t Default: 120 sec.");
Vikas Chaudhary30387272011-03-21 03:34:32 -070085
Tej Parkash068237c82012-05-18 04:41:44 -040086int ql4xmdcapmask = 0x1F;
87module_param(ql4xmdcapmask, int, S_IRUGO);
88MODULE_PARM_DESC(ql4xmdcapmask,
89 " Set the Minidump driver capture mask level.\n"
90 "\t\t Default is 0x1F.\n"
91 "\t\t Can be set to 0x3, 0x7, 0xF, 0x1F, 0x3F, 0x7F");
92
93int ql4xenablemd = 1;
94module_param(ql4xenablemd, int, S_IRUGO | S_IWUSR);
95MODULE_PARM_DESC(ql4xenablemd,
96 " Set to enable minidump.\n"
97 "\t\t 0 - disable minidump\n"
98 "\t\t 1 - enable minidump (Default)");
99
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500100static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700101/*
102 * SCSI host template entry points
103 */
Adrian Bunk47975472007-04-26 00:35:16 -0700104static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700105
106/*
107 * iSCSI template entry points
108 */
Mike Christiefca9f042012-02-27 03:08:54 -0800109static int qla4xxx_session_get_param(struct iscsi_cls_session *cls_sess,
110 enum iscsi_param param, char *buf);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700111static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
112 enum iscsi_param param, char *buf);
Mike Christieaa1e93a2007-05-30 12:57:09 -0500113static int qla4xxx_host_get_param(struct Scsi_Host *shost,
114 enum iscsi_host_param param, char *buf);
Mike Christie00c31882011-10-06 03:56:59 -0500115static int qla4xxx_iface_set_param(struct Scsi_Host *shost, void *data,
116 uint32_t len);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500117static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
118 enum iscsi_param_type param_type,
119 int param, char *buf);
Mike Christie5c656af2009-07-15 15:02:59 -0500120static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500121static struct iscsi_endpoint *qla4xxx_ep_connect(struct Scsi_Host *shost,
122 struct sockaddr *dst_addr,
123 int non_blocking);
124static int qla4xxx_ep_poll(struct iscsi_endpoint *ep, int timeout_ms);
125static void qla4xxx_ep_disconnect(struct iscsi_endpoint *ep);
126static int qla4xxx_get_ep_param(struct iscsi_endpoint *ep,
127 enum iscsi_param param, char *buf);
128static int qla4xxx_conn_start(struct iscsi_cls_conn *conn);
129static struct iscsi_cls_conn *
130qla4xxx_conn_create(struct iscsi_cls_session *cls_sess, uint32_t conn_idx);
131static int qla4xxx_conn_bind(struct iscsi_cls_session *cls_session,
132 struct iscsi_cls_conn *cls_conn,
133 uint64_t transport_fd, int is_leading);
134static void qla4xxx_conn_destroy(struct iscsi_cls_conn *conn);
135static struct iscsi_cls_session *
136qla4xxx_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max,
137 uint16_t qdepth, uint32_t initial_cmdsn);
138static void qla4xxx_session_destroy(struct iscsi_cls_session *sess);
139static void qla4xxx_task_work(struct work_struct *wdata);
140static int qla4xxx_alloc_pdu(struct iscsi_task *, uint8_t);
141static int qla4xxx_task_xmit(struct iscsi_task *);
142static void qla4xxx_task_cleanup(struct iscsi_task *);
143static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session);
144static void qla4xxx_conn_get_stats(struct iscsi_cls_conn *cls_conn,
145 struct iscsi_stats *stats);
Vikas Chaudharyc0b9d3f2012-02-13 18:30:49 +0530146static int qla4xxx_send_ping(struct Scsi_Host *shost, uint32_t iface_num,
147 uint32_t iface_type, uint32_t payload_size,
148 uint32_t pid, struct sockaddr *dst_addr);
Nilesh Javali376738a2012-02-27 03:08:52 -0800149static int qla4xxx_get_chap_list(struct Scsi_Host *shost, uint16_t chap_tbl_idx,
150 uint32_t *num_entries, char *buf);
151static int qla4xxx_delete_chap(struct Scsi_Host *shost, uint16_t chap_tbl_idx);
Vikas Chaudharyc0b9d3f2012-02-13 18:30:49 +0530152
David Somayajuluafaf5a22006-09-19 10:28:00 -0700153/*
154 * SCSI host template entry points
155 */
Jeff Garzikf2812332010-11-16 02:10:29 -0500156static int qla4xxx_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmd);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530157static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700158static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
Mike Christiece545032008-02-29 18:25:20 -0600159static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700160static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
161static int qla4xxx_slave_alloc(struct scsi_device *device);
162static int qla4xxx_slave_configure(struct scsi_device *device);
163static void qla4xxx_slave_destroy(struct scsi_device *sdev);
Vikas Chaudhary5e9bcec2012-08-22 07:55:01 -0400164static umode_t qla4_attr_is_visible(int param_type, int param);
Vikas Chaudhary95d31262011-08-12 02:51:29 -0700165static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type);
Tej Parkashf7b4aa632012-04-30 04:12:19 -0700166static int qla4xxx_change_queue_depth(struct scsi_device *sdev, int qdepth,
167 int reason);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700168
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530169static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
170 QLA82XX_LEGACY_INTR_CONFIG;
171
David Somayajuluafaf5a22006-09-19 10:28:00 -0700172static struct scsi_host_template qla4xxx_driver_template = {
173 .module = THIS_MODULE,
174 .name = DRIVER_NAME,
175 .proc_name = DRIVER_NAME,
176 .queuecommand = qla4xxx_queuecommand,
177
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530178 .eh_abort_handler = qla4xxx_eh_abort,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700179 .eh_device_reset_handler = qla4xxx_eh_device_reset,
Mike Christiece545032008-02-29 18:25:20 -0600180 .eh_target_reset_handler = qla4xxx_eh_target_reset,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700181 .eh_host_reset_handler = qla4xxx_eh_host_reset,
Mike Christie5c656af2009-07-15 15:02:59 -0500182 .eh_timed_out = qla4xxx_eh_cmd_timed_out,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700183
184 .slave_configure = qla4xxx_slave_configure,
185 .slave_alloc = qla4xxx_slave_alloc,
186 .slave_destroy = qla4xxx_slave_destroy,
Tej Parkashf7b4aa632012-04-30 04:12:19 -0700187 .change_queue_depth = qla4xxx_change_queue_depth,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700188
189 .this_id = -1,
190 .cmd_per_lun = 3,
191 .use_clustering = ENABLE_CLUSTERING,
192 .sg_tablesize = SG_ALL,
193
194 .max_sectors = 0xFFFF,
Harish Zunjarrao7ad633c2011-05-17 23:17:11 -0700195 .shost_attrs = qla4xxx_host_attrs,
Vikas Chaudhary95d31262011-08-12 02:51:29 -0700196 .host_reset = qla4xxx_host_reset,
Vikas Chaudharya3559432011-07-25 13:48:51 -0500197 .vendor_id = SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_QLOGIC,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700198};
199
200static struct iscsi_transport qla4xxx_iscsi_transport = {
201 .owner = THIS_MODULE,
202 .name = DRIVER_NAME,
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500203 .caps = CAP_TEXT_NEGO |
204 CAP_DATA_PATH_OFFLOAD | CAP_HDRDGST |
205 CAP_DATADGST | CAP_LOGIN_OFFLOAD |
206 CAP_MULTI_R2T,
Vikas Chaudhary5e9bcec2012-08-22 07:55:01 -0400207 .attr_is_visible = qla4_attr_is_visible,
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500208 .create_session = qla4xxx_session_create,
209 .destroy_session = qla4xxx_session_destroy,
210 .start_conn = qla4xxx_conn_start,
211 .create_conn = qla4xxx_conn_create,
212 .bind_conn = qla4xxx_conn_bind,
213 .stop_conn = iscsi_conn_stop,
214 .destroy_conn = qla4xxx_conn_destroy,
215 .set_param = iscsi_set_param,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700216 .get_conn_param = qla4xxx_conn_get_param,
Mike Christiefca9f042012-02-27 03:08:54 -0800217 .get_session_param = qla4xxx_session_get_param,
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500218 .get_ep_param = qla4xxx_get_ep_param,
219 .ep_connect = qla4xxx_ep_connect,
220 .ep_poll = qla4xxx_ep_poll,
221 .ep_disconnect = qla4xxx_ep_disconnect,
222 .get_stats = qla4xxx_conn_get_stats,
223 .send_pdu = iscsi_conn_send_pdu,
224 .xmit_task = qla4xxx_task_xmit,
225 .cleanup_task = qla4xxx_task_cleanup,
226 .alloc_pdu = qla4xxx_alloc_pdu,
227
Mike Christieaa1e93a2007-05-30 12:57:09 -0500228 .get_host_param = qla4xxx_host_get_param,
Mike Christied00efe32011-07-25 13:48:38 -0500229 .set_iface_param = qla4xxx_iface_set_param,
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500230 .get_iface_param = qla4xxx_get_iface_param,
Vikas Chaudharya3559432011-07-25 13:48:51 -0500231 .bsg_request = qla4xxx_bsg_request,
Vikas Chaudharyc0b9d3f2012-02-13 18:30:49 +0530232 .send_ping = qla4xxx_send_ping,
Nilesh Javali376738a2012-02-27 03:08:52 -0800233 .get_chap = qla4xxx_get_chap_list,
234 .delete_chap = qla4xxx_delete_chap,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700235};
236
237static struct scsi_transport_template *qla4xxx_scsi_transport;
238
Vikas Chaudharyc0b9d3f2012-02-13 18:30:49 +0530239static int qla4xxx_send_ping(struct Scsi_Host *shost, uint32_t iface_num,
240 uint32_t iface_type, uint32_t payload_size,
241 uint32_t pid, struct sockaddr *dst_addr)
242{
243 struct scsi_qla_host *ha = to_qla_host(shost);
244 struct sockaddr_in *addr;
245 struct sockaddr_in6 *addr6;
246 uint32_t options = 0;
247 uint8_t ipaddr[IPv6_ADDR_LEN];
248 int rval;
249
250 memset(ipaddr, 0, IPv6_ADDR_LEN);
251 /* IPv4 to IPv4 */
252 if ((iface_type == ISCSI_IFACE_TYPE_IPV4) &&
253 (dst_addr->sa_family == AF_INET)) {
254 addr = (struct sockaddr_in *)dst_addr;
255 memcpy(ipaddr, &addr->sin_addr.s_addr, IP_ADDR_LEN);
256 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: IPv4 Ping src: %pI4 "
257 "dest: %pI4\n", __func__,
258 &ha->ip_config.ip_address, ipaddr));
259 rval = qla4xxx_ping_iocb(ha, options, payload_size, pid,
260 ipaddr);
261 if (rval)
262 rval = -EINVAL;
263 } else if ((iface_type == ISCSI_IFACE_TYPE_IPV6) &&
264 (dst_addr->sa_family == AF_INET6)) {
265 /* IPv6 to IPv6 */
266 addr6 = (struct sockaddr_in6 *)dst_addr;
267 memcpy(ipaddr, &addr6->sin6_addr.in6_u.u6_addr8, IPv6_ADDR_LEN);
268
269 options |= PING_IPV6_PROTOCOL_ENABLE;
270
271 /* Ping using LinkLocal address */
272 if ((iface_num == 0) || (iface_num == 1)) {
273 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: LinkLocal Ping "
274 "src: %pI6 dest: %pI6\n", __func__,
275 &ha->ip_config.ipv6_link_local_addr,
276 ipaddr));
277 options |= PING_IPV6_LINKLOCAL_ADDR;
278 rval = qla4xxx_ping_iocb(ha, options, payload_size,
279 pid, ipaddr);
280 } else {
281 ql4_printk(KERN_WARNING, ha, "%s: iface num = %d "
282 "not supported\n", __func__, iface_num);
283 rval = -ENOSYS;
284 goto exit_send_ping;
285 }
286
287 /*
288 * If ping using LinkLocal address fails, try ping using
289 * IPv6 address
290 */
291 if (rval != QLA_SUCCESS) {
292 options &= ~PING_IPV6_LINKLOCAL_ADDR;
293 if (iface_num == 0) {
294 options |= PING_IPV6_ADDR0;
295 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: IPv6 "
296 "Ping src: %pI6 "
297 "dest: %pI6\n", __func__,
298 &ha->ip_config.ipv6_addr0,
299 ipaddr));
300 } else if (iface_num == 1) {
301 options |= PING_IPV6_ADDR1;
302 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: IPv6 "
303 "Ping src: %pI6 "
304 "dest: %pI6\n", __func__,
305 &ha->ip_config.ipv6_addr1,
306 ipaddr));
307 }
308 rval = qla4xxx_ping_iocb(ha, options, payload_size,
309 pid, ipaddr);
310 if (rval)
311 rval = -EINVAL;
312 }
313 } else
314 rval = -ENOSYS;
315exit_send_ping:
316 return rval;
317}
318
Vikas Chaudhary5e9bcec2012-08-22 07:55:01 -0400319static umode_t qla4_attr_is_visible(int param_type, int param)
Mike Christie3128c6c2011-07-25 13:48:42 -0500320{
321 switch (param_type) {
Mike Christief27fb2e2011-07-25 13:48:45 -0500322 case ISCSI_HOST_PARAM:
323 switch (param) {
324 case ISCSI_HOST_PARAM_HWADDRESS:
325 case ISCSI_HOST_PARAM_IPADDRESS:
326 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Vikas Chaudhary3254dbe2012-01-19 03:06:56 -0800327 case ISCSI_HOST_PARAM_PORT_STATE:
328 case ISCSI_HOST_PARAM_PORT_SPEED:
Mike Christief27fb2e2011-07-25 13:48:45 -0500329 return S_IRUGO;
330 default:
331 return 0;
332 }
Mike Christie3128c6c2011-07-25 13:48:42 -0500333 case ISCSI_PARAM:
334 switch (param) {
Mike Christie590134f2011-10-17 22:42:13 -0500335 case ISCSI_PARAM_PERSISTENT_ADDRESS:
336 case ISCSI_PARAM_PERSISTENT_PORT:
Mike Christie3128c6c2011-07-25 13:48:42 -0500337 case ISCSI_PARAM_CONN_ADDRESS:
338 case ISCSI_PARAM_CONN_PORT:
Mike Christie1d063c12011-07-25 13:48:43 -0500339 case ISCSI_PARAM_TARGET_NAME:
340 case ISCSI_PARAM_TPGT:
341 case ISCSI_PARAM_TARGET_ALIAS:
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500342 case ISCSI_PARAM_MAX_BURST:
343 case ISCSI_PARAM_MAX_R2T:
344 case ISCSI_PARAM_FIRST_BURST:
345 case ISCSI_PARAM_MAX_RECV_DLENGTH:
346 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
Mike Christiede379202011-08-14 20:42:56 -0500347 case ISCSI_PARAM_IFACE_NAME:
Mike Christiefca9f042012-02-27 03:08:54 -0800348 case ISCSI_PARAM_CHAP_OUT_IDX:
349 case ISCSI_PARAM_CHAP_IN_IDX:
350 case ISCSI_PARAM_USERNAME:
351 case ISCSI_PARAM_PASSWORD:
352 case ISCSI_PARAM_USERNAME_IN:
353 case ISCSI_PARAM_PASSWORD_IN:
Mike Christie3128c6c2011-07-25 13:48:42 -0500354 return S_IRUGO;
355 default:
356 return 0;
357 }
Mike Christieb78dbba2011-07-25 13:48:44 -0500358 case ISCSI_NET_PARAM:
359 switch (param) {
360 case ISCSI_NET_PARAM_IPV4_ADDR:
361 case ISCSI_NET_PARAM_IPV4_SUBNET:
362 case ISCSI_NET_PARAM_IPV4_GW:
363 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
364 case ISCSI_NET_PARAM_IFACE_ENABLE:
365 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
366 case ISCSI_NET_PARAM_IPV6_ADDR:
367 case ISCSI_NET_PARAM_IPV6_ROUTER:
368 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
369 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
Vikas Chaudhary6ac73e82011-07-25 13:48:49 -0500370 case ISCSI_NET_PARAM_VLAN_ID:
371 case ISCSI_NET_PARAM_VLAN_PRIORITY:
372 case ISCSI_NET_PARAM_VLAN_ENABLED:
Vikas Chaudhary943c1572011-08-01 03:26:13 -0700373 case ISCSI_NET_PARAM_MTU:
Vikas Chaudhary2ada7fc2011-08-01 03:26:19 -0700374 case ISCSI_NET_PARAM_PORT:
Mike Christieb78dbba2011-07-25 13:48:44 -0500375 return S_IRUGO;
376 default:
377 return 0;
378 }
Mike Christie3128c6c2011-07-25 13:48:42 -0500379 }
380
381 return 0;
382}
383
Nilesh Javali376738a2012-02-27 03:08:52 -0800384static int qla4xxx_get_chap_list(struct Scsi_Host *shost, uint16_t chap_tbl_idx,
385 uint32_t *num_entries, char *buf)
386{
387 struct scsi_qla_host *ha = to_qla_host(shost);
388 struct ql4_chap_table *chap_table;
389 struct iscsi_chap_rec *chap_rec;
390 int max_chap_entries = 0;
391 int valid_chap_entries = 0;
392 int ret = 0, i;
393
394 if (is_qla8022(ha))
395 max_chap_entries = (ha->hw.flt_chap_size / 2) /
396 sizeof(struct ql4_chap_table);
397 else
398 max_chap_entries = MAX_CHAP_ENTRIES_40XX;
399
400 ql4_printk(KERN_INFO, ha, "%s: num_entries = %d, CHAP idx = %d\n",
401 __func__, *num_entries, chap_tbl_idx);
402
403 if (!buf) {
404 ret = -ENOMEM;
405 goto exit_get_chap_list;
406 }
407
408 chap_rec = (struct iscsi_chap_rec *) buf;
409 mutex_lock(&ha->chap_sem);
410 for (i = chap_tbl_idx; i < max_chap_entries; i++) {
411 chap_table = (struct ql4_chap_table *)ha->chap_list + i;
412 if (chap_table->cookie !=
413 __constant_cpu_to_le16(CHAP_VALID_COOKIE))
414 continue;
415
416 chap_rec->chap_tbl_idx = i;
417 strncpy(chap_rec->username, chap_table->name,
418 ISCSI_CHAP_AUTH_NAME_MAX_LEN);
419 strncpy(chap_rec->password, chap_table->secret,
420 QL4_CHAP_MAX_SECRET_LEN);
421 chap_rec->password_length = chap_table->secret_len;
422
423 if (chap_table->flags & BIT_7) /* local */
424 chap_rec->chap_type = CHAP_TYPE_OUT;
425
426 if (chap_table->flags & BIT_6) /* peer */
427 chap_rec->chap_type = CHAP_TYPE_IN;
428
429 chap_rec++;
430
431 valid_chap_entries++;
432 if (valid_chap_entries == *num_entries)
433 break;
434 else
435 continue;
436 }
437 mutex_unlock(&ha->chap_sem);
438
439exit_get_chap_list:
440 ql4_printk(KERN_INFO, ha, "%s: Valid CHAP Entries = %d\n",
441 __func__, valid_chap_entries);
442 *num_entries = valid_chap_entries;
443 return ret;
444}
445
446static int __qla4xxx_is_chap_active(struct device *dev, void *data)
447{
448 int ret = 0;
449 uint16_t *chap_tbl_idx = (uint16_t *) data;
450 struct iscsi_cls_session *cls_session;
451 struct iscsi_session *sess;
452 struct ddb_entry *ddb_entry;
453
454 if (!iscsi_is_session_dev(dev))
455 goto exit_is_chap_active;
456
457 cls_session = iscsi_dev_to_session(dev);
458 sess = cls_session->dd_data;
459 ddb_entry = sess->dd_data;
460
461 if (iscsi_session_chkready(cls_session))
462 goto exit_is_chap_active;
463
464 if (ddb_entry->chap_tbl_idx == *chap_tbl_idx)
465 ret = 1;
466
467exit_is_chap_active:
468 return ret;
469}
470
471static int qla4xxx_is_chap_active(struct Scsi_Host *shost,
472 uint16_t chap_tbl_idx)
473{
474 int ret = 0;
475
476 ret = device_for_each_child(&shost->shost_gendev, &chap_tbl_idx,
477 __qla4xxx_is_chap_active);
478
479 return ret;
480}
481
482static int qla4xxx_delete_chap(struct Scsi_Host *shost, uint16_t chap_tbl_idx)
483{
484 struct scsi_qla_host *ha = to_qla_host(shost);
485 struct ql4_chap_table *chap_table;
486 dma_addr_t chap_dma;
487 int max_chap_entries = 0;
488 uint32_t offset = 0;
489 uint32_t chap_size;
490 int ret = 0;
491
492 chap_table = dma_pool_alloc(ha->chap_dma_pool, GFP_KERNEL, &chap_dma);
493 if (chap_table == NULL)
494 return -ENOMEM;
495
496 memset(chap_table, 0, sizeof(struct ql4_chap_table));
497
498 if (is_qla8022(ha))
499 max_chap_entries = (ha->hw.flt_chap_size / 2) /
500 sizeof(struct ql4_chap_table);
501 else
502 max_chap_entries = MAX_CHAP_ENTRIES_40XX;
503
504 if (chap_tbl_idx > max_chap_entries) {
505 ret = -EINVAL;
506 goto exit_delete_chap;
507 }
508
509 /* Check if chap index is in use.
510 * If chap is in use don't delet chap entry */
511 ret = qla4xxx_is_chap_active(shost, chap_tbl_idx);
512 if (ret) {
513 ql4_printk(KERN_INFO, ha, "CHAP entry %d is in use, cannot "
514 "delete from flash\n", chap_tbl_idx);
515 ret = -EBUSY;
516 goto exit_delete_chap;
517 }
518
519 chap_size = sizeof(struct ql4_chap_table);
520 if (is_qla40XX(ha))
521 offset = FLASH_CHAP_OFFSET | (chap_tbl_idx * chap_size);
522 else {
523 offset = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_region_chap << 2);
524 /* flt_chap_size is CHAP table size for both ports
525 * so divide it by 2 to calculate the offset for second port
526 */
527 if (ha->port_num == 1)
528 offset += (ha->hw.flt_chap_size / 2);
529 offset += (chap_tbl_idx * chap_size);
530 }
531
532 ret = qla4xxx_get_flash(ha, chap_dma, offset, chap_size);
533 if (ret != QLA_SUCCESS) {
534 ret = -EINVAL;
535 goto exit_delete_chap;
536 }
537
538 DEBUG2(ql4_printk(KERN_INFO, ha, "Chap Cookie: x%x\n",
539 __le16_to_cpu(chap_table->cookie)));
540
541 if (__le16_to_cpu(chap_table->cookie) != CHAP_VALID_COOKIE) {
542 ql4_printk(KERN_ERR, ha, "No valid chap entry found\n");
543 goto exit_delete_chap;
544 }
545
546 chap_table->cookie = __constant_cpu_to_le16(0xFFFF);
547
548 offset = FLASH_CHAP_OFFSET |
549 (chap_tbl_idx * sizeof(struct ql4_chap_table));
550 ret = qla4xxx_set_flash(ha, chap_dma, offset, chap_size,
551 FLASH_OPT_RMW_COMMIT);
552 if (ret == QLA_SUCCESS && ha->chap_list) {
553 mutex_lock(&ha->chap_sem);
554 /* Update ha chap_list cache */
555 memcpy((struct ql4_chap_table *)ha->chap_list + chap_tbl_idx,
556 chap_table, sizeof(struct ql4_chap_table));
557 mutex_unlock(&ha->chap_sem);
558 }
559 if (ret != QLA_SUCCESS)
560 ret = -EINVAL;
561
562exit_delete_chap:
563 dma_pool_free(ha->chap_dma_pool, chap_table, chap_dma);
564 return ret;
565}
566
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500567static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
568 enum iscsi_param_type param_type,
569 int param, char *buf)
570{
571 struct Scsi_Host *shost = iscsi_iface_to_shost(iface);
572 struct scsi_qla_host *ha = to_qla_host(shost);
573 int len = -ENOSYS;
574
575 if (param_type != ISCSI_NET_PARAM)
576 return -ENOSYS;
577
578 switch (param) {
579 case ISCSI_NET_PARAM_IPV4_ADDR:
580 len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
581 break;
582 case ISCSI_NET_PARAM_IPV4_SUBNET:
583 len = sprintf(buf, "%pI4\n", &ha->ip_config.subnet_mask);
584 break;
585 case ISCSI_NET_PARAM_IPV4_GW:
586 len = sprintf(buf, "%pI4\n", &ha->ip_config.gateway);
587 break;
588 case ISCSI_NET_PARAM_IFACE_ENABLE:
589 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
590 len = sprintf(buf, "%s\n",
591 (ha->ip_config.ipv4_options &
592 IPOPT_IPV4_PROTOCOL_ENABLE) ?
593 "enabled" : "disabled");
594 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
595 len = sprintf(buf, "%s\n",
596 (ha->ip_config.ipv6_options &
597 IPV6_OPT_IPV6_PROTOCOL_ENABLE) ?
598 "enabled" : "disabled");
599 break;
600 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
601 len = sprintf(buf, "%s\n",
602 (ha->ip_config.tcp_options & TCPOPT_DHCP_ENABLE) ?
603 "dhcp" : "static");
604 break;
605 case ISCSI_NET_PARAM_IPV6_ADDR:
606 if (iface->iface_num == 0)
607 len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr0);
608 if (iface->iface_num == 1)
609 len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr1);
610 break;
611 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
612 len = sprintf(buf, "%pI6\n",
613 &ha->ip_config.ipv6_link_local_addr);
614 break;
615 case ISCSI_NET_PARAM_IPV6_ROUTER:
616 len = sprintf(buf, "%pI6\n",
617 &ha->ip_config.ipv6_default_router_addr);
618 break;
619 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
620 len = sprintf(buf, "%s\n",
621 (ha->ip_config.ipv6_addl_options &
622 IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) ?
623 "nd" : "static");
624 break;
625 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
626 len = sprintf(buf, "%s\n",
627 (ha->ip_config.ipv6_addl_options &
628 IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR) ?
629 "auto" : "static");
630 break;
Vikas Chaudhary6ac73e82011-07-25 13:48:49 -0500631 case ISCSI_NET_PARAM_VLAN_ID:
632 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
633 len = sprintf(buf, "%d\n",
634 (ha->ip_config.ipv4_vlan_tag &
635 ISCSI_MAX_VLAN_ID));
636 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
637 len = sprintf(buf, "%d\n",
638 (ha->ip_config.ipv6_vlan_tag &
639 ISCSI_MAX_VLAN_ID));
640 break;
641 case ISCSI_NET_PARAM_VLAN_PRIORITY:
642 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
643 len = sprintf(buf, "%d\n",
644 ((ha->ip_config.ipv4_vlan_tag >> 13) &
645 ISCSI_MAX_VLAN_PRIORITY));
646 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
647 len = sprintf(buf, "%d\n",
648 ((ha->ip_config.ipv6_vlan_tag >> 13) &
649 ISCSI_MAX_VLAN_PRIORITY));
650 break;
651 case ISCSI_NET_PARAM_VLAN_ENABLED:
652 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
653 len = sprintf(buf, "%s\n",
654 (ha->ip_config.ipv4_options &
655 IPOPT_VLAN_TAGGING_ENABLE) ?
656 "enabled" : "disabled");
657 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
658 len = sprintf(buf, "%s\n",
659 (ha->ip_config.ipv6_options &
660 IPV6_OPT_VLAN_TAGGING_ENABLE) ?
661 "enabled" : "disabled");
662 break;
Vikas Chaudhary943c1572011-08-01 03:26:13 -0700663 case ISCSI_NET_PARAM_MTU:
664 len = sprintf(buf, "%d\n", ha->ip_config.eth_mtu_size);
665 break;
Vikas Chaudhary2ada7fc2011-08-01 03:26:19 -0700666 case ISCSI_NET_PARAM_PORT:
667 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
668 len = sprintf(buf, "%d\n", ha->ip_config.ipv4_port);
669 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
670 len = sprintf(buf, "%d\n", ha->ip_config.ipv6_port);
671 break;
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500672 default:
673 len = -ENOSYS;
674 }
675
676 return len;
677}
678
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500679static struct iscsi_endpoint *
680qla4xxx_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
681 int non_blocking)
682{
683 int ret;
684 struct iscsi_endpoint *ep;
685 struct qla_endpoint *qla_ep;
686 struct scsi_qla_host *ha;
687 struct sockaddr_in *addr;
688 struct sockaddr_in6 *addr6;
689
690 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
691 if (!shost) {
692 ret = -ENXIO;
693 printk(KERN_ERR "%s: shost is NULL\n",
694 __func__);
695 return ERR_PTR(ret);
696 }
697
698 ha = iscsi_host_priv(shost);
699
700 ep = iscsi_create_endpoint(sizeof(struct qla_endpoint));
701 if (!ep) {
702 ret = -ENOMEM;
703 return ERR_PTR(ret);
704 }
705
706 qla_ep = ep->dd_data;
707 memset(qla_ep, 0, sizeof(struct qla_endpoint));
708 if (dst_addr->sa_family == AF_INET) {
709 memcpy(&qla_ep->dst_addr, dst_addr, sizeof(struct sockaddr_in));
710 addr = (struct sockaddr_in *)&qla_ep->dst_addr;
711 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI4\n", __func__,
712 (char *)&addr->sin_addr));
713 } else if (dst_addr->sa_family == AF_INET6) {
714 memcpy(&qla_ep->dst_addr, dst_addr,
715 sizeof(struct sockaddr_in6));
716 addr6 = (struct sockaddr_in6 *)&qla_ep->dst_addr;
717 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI6\n", __func__,
718 (char *)&addr6->sin6_addr));
719 }
720
721 qla_ep->host = shost;
722
723 return ep;
724}
725
726static int qla4xxx_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
727{
728 struct qla_endpoint *qla_ep;
729 struct scsi_qla_host *ha;
730 int ret = 0;
731
732 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
733 qla_ep = ep->dd_data;
734 ha = to_qla_host(qla_ep->host);
735
Mike Christie13483732011-12-01 21:38:41 -0600736 if (adapter_up(ha) && !test_bit(AF_BUILD_DDB_LIST, &ha->flags))
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500737 ret = 1;
738
739 return ret;
740}
741
742static void qla4xxx_ep_disconnect(struct iscsi_endpoint *ep)
743{
744 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
745 iscsi_destroy_endpoint(ep);
746}
747
748static int qla4xxx_get_ep_param(struct iscsi_endpoint *ep,
749 enum iscsi_param param,
750 char *buf)
751{
752 struct qla_endpoint *qla_ep = ep->dd_data;
753 struct sockaddr *dst_addr;
754
755 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
756
757 switch (param) {
758 case ISCSI_PARAM_CONN_PORT:
759 case ISCSI_PARAM_CONN_ADDRESS:
760 if (!qla_ep)
761 return -ENOTCONN;
762
763 dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
764 if (!dst_addr)
765 return -ENOTCONN;
766
767 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
768 &qla_ep->dst_addr, param, buf);
769 default:
770 return -ENOSYS;
771 }
772}
773
774static void qla4xxx_conn_get_stats(struct iscsi_cls_conn *cls_conn,
775 struct iscsi_stats *stats)
776{
777 struct iscsi_session *sess;
778 struct iscsi_cls_session *cls_sess;
779 struct ddb_entry *ddb_entry;
780 struct scsi_qla_host *ha;
781 struct ql_iscsi_stats *ql_iscsi_stats;
782 int stats_size;
783 int ret;
784 dma_addr_t iscsi_stats_dma;
785
786 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
787
788 cls_sess = iscsi_conn_to_session(cls_conn);
789 sess = cls_sess->dd_data;
790 ddb_entry = sess->dd_data;
791 ha = ddb_entry->ha;
792
793 stats_size = PAGE_ALIGN(sizeof(struct ql_iscsi_stats));
794 /* Allocate memory */
795 ql_iscsi_stats = dma_alloc_coherent(&ha->pdev->dev, stats_size,
796 &iscsi_stats_dma, GFP_KERNEL);
797 if (!ql_iscsi_stats) {
798 ql4_printk(KERN_ERR, ha,
799 "Unable to allocate memory for iscsi stats\n");
800 goto exit_get_stats;
801 }
802
803 ret = qla4xxx_get_mgmt_data(ha, ddb_entry->fw_ddb_index, stats_size,
804 iscsi_stats_dma);
805 if (ret != QLA_SUCCESS) {
806 ql4_printk(KERN_ERR, ha,
Masanari Iida59e13d42012-04-25 00:24:16 +0900807 "Unable to retrieve iscsi stats\n");
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500808 goto free_stats;
809 }
810
811 /* octets */
812 stats->txdata_octets = le64_to_cpu(ql_iscsi_stats->tx_data_octets);
813 stats->rxdata_octets = le64_to_cpu(ql_iscsi_stats->rx_data_octets);
814 /* xmit pdus */
815 stats->noptx_pdus = le32_to_cpu(ql_iscsi_stats->tx_nopout_pdus);
816 stats->scsicmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_cmd_pdus);
817 stats->tmfcmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_tmf_cmd_pdus);
818 stats->login_pdus = le32_to_cpu(ql_iscsi_stats->tx_login_cmd_pdus);
819 stats->text_pdus = le32_to_cpu(ql_iscsi_stats->tx_text_cmd_pdus);
820 stats->dataout_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_write_pdus);
821 stats->logout_pdus = le32_to_cpu(ql_iscsi_stats->tx_logout_cmd_pdus);
822 stats->snack_pdus = le32_to_cpu(ql_iscsi_stats->tx_snack_req_pdus);
823 /* recv pdus */
824 stats->noprx_pdus = le32_to_cpu(ql_iscsi_stats->rx_nopin_pdus);
825 stats->scsirsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_resp_pdus);
826 stats->tmfrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_tmf_resp_pdus);
827 stats->textrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_text_resp_pdus);
828 stats->datain_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_read_pdus);
829 stats->logoutrsp_pdus =
830 le32_to_cpu(ql_iscsi_stats->rx_logout_resp_pdus);
831 stats->r2t_pdus = le32_to_cpu(ql_iscsi_stats->rx_r2t_pdus);
832 stats->async_pdus = le32_to_cpu(ql_iscsi_stats->rx_async_pdus);
833 stats->rjt_pdus = le32_to_cpu(ql_iscsi_stats->rx_reject_pdus);
834
835free_stats:
836 dma_free_coherent(&ha->pdev->dev, stats_size, ql_iscsi_stats,
837 iscsi_stats_dma);
838exit_get_stats:
839 return;
840}
841
Mike Christie5c656af2009-07-15 15:02:59 -0500842static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
843{
844 struct iscsi_cls_session *session;
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500845 struct iscsi_session *sess;
846 unsigned long flags;
847 enum blk_eh_timer_return ret = BLK_EH_NOT_HANDLED;
Mike Christie5c656af2009-07-15 15:02:59 -0500848
849 session = starget_to_session(scsi_target(sc->device));
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500850 sess = session->dd_data;
Mike Christie5c656af2009-07-15 15:02:59 -0500851
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500852 spin_lock_irqsave(&session->lock, flags);
853 if (session->state == ISCSI_SESSION_FAILED)
854 ret = BLK_EH_RESET_TIMER;
855 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie5c656af2009-07-15 15:02:59 -0500856
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500857 return ret;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700858}
859
Vikas Chaudhary3254dbe2012-01-19 03:06:56 -0800860static void qla4xxx_set_port_speed(struct Scsi_Host *shost)
861{
862 struct scsi_qla_host *ha = to_qla_host(shost);
Vikas Chaudharye16d1662012-03-02 01:55:31 -0800863 struct iscsi_cls_host *ihost = shost->shost_data;
Vikas Chaudhary3254dbe2012-01-19 03:06:56 -0800864 uint32_t speed = ISCSI_PORT_SPEED_UNKNOWN;
865
866 qla4xxx_get_firmware_state(ha);
867
868 switch (ha->addl_fw_state & 0x0F00) {
869 case FW_ADDSTATE_LINK_SPEED_10MBPS:
870 speed = ISCSI_PORT_SPEED_10MBPS;
871 break;
872 case FW_ADDSTATE_LINK_SPEED_100MBPS:
873 speed = ISCSI_PORT_SPEED_100MBPS;
874 break;
875 case FW_ADDSTATE_LINK_SPEED_1GBPS:
876 speed = ISCSI_PORT_SPEED_1GBPS;
877 break;
878 case FW_ADDSTATE_LINK_SPEED_10GBPS:
879 speed = ISCSI_PORT_SPEED_10GBPS;
880 break;
881 }
882 ihost->port_speed = speed;
883}
884
885static void qla4xxx_set_port_state(struct Scsi_Host *shost)
886{
887 struct scsi_qla_host *ha = to_qla_host(shost);
Vikas Chaudharye16d1662012-03-02 01:55:31 -0800888 struct iscsi_cls_host *ihost = shost->shost_data;
Vikas Chaudhary3254dbe2012-01-19 03:06:56 -0800889 uint32_t state = ISCSI_PORT_STATE_DOWN;
890
891 if (test_bit(AF_LINK_UP, &ha->flags))
892 state = ISCSI_PORT_STATE_UP;
893
894 ihost->port_state = state;
895}
896
Mike Christieaa1e93a2007-05-30 12:57:09 -0500897static int qla4xxx_host_get_param(struct Scsi_Host *shost,
898 enum iscsi_host_param param, char *buf)
899{
900 struct scsi_qla_host *ha = to_qla_host(shost);
901 int len;
902
903 switch (param) {
904 case ISCSI_HOST_PARAM_HWADDRESS:
Michael Chan7ffc49a2007-12-24 21:28:09 -0800905 len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
Mike Christieaa1e93a2007-05-30 12:57:09 -0500906 break;
Mike Christie22236962007-05-30 12:57:24 -0500907 case ISCSI_HOST_PARAM_IPADDRESS:
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500908 len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
Mike Christie22236962007-05-30 12:57:24 -0500909 break;
Mike Christie8ad57812007-05-30 12:57:13 -0500910 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie22236962007-05-30 12:57:24 -0500911 len = sprintf(buf, "%s\n", ha->name_string);
Mike Christie8ad57812007-05-30 12:57:13 -0500912 break;
Vikas Chaudhary3254dbe2012-01-19 03:06:56 -0800913 case ISCSI_HOST_PARAM_PORT_STATE:
914 qla4xxx_set_port_state(shost);
915 len = sprintf(buf, "%s\n", iscsi_get_port_state_name(shost));
916 break;
917 case ISCSI_HOST_PARAM_PORT_SPEED:
918 qla4xxx_set_port_speed(shost);
919 len = sprintf(buf, "%s\n", iscsi_get_port_speed_name(shost));
920 break;
Mike Christieaa1e93a2007-05-30 12:57:09 -0500921 default:
922 return -ENOSYS;
923 }
924
925 return len;
926}
927
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500928static void qla4xxx_create_ipv4_iface(struct scsi_qla_host *ha)
929{
930 if (ha->iface_ipv4)
931 return;
932
933 /* IPv4 */
934 ha->iface_ipv4 = iscsi_create_iface(ha->host,
935 &qla4xxx_iscsi_transport,
936 ISCSI_IFACE_TYPE_IPV4, 0, 0);
937 if (!ha->iface_ipv4)
938 ql4_printk(KERN_ERR, ha, "Could not create IPv4 iSCSI "
939 "iface0.\n");
940}
941
942static void qla4xxx_create_ipv6_iface(struct scsi_qla_host *ha)
943{
944 if (!ha->iface_ipv6_0)
945 /* IPv6 iface-0 */
946 ha->iface_ipv6_0 = iscsi_create_iface(ha->host,
947 &qla4xxx_iscsi_transport,
948 ISCSI_IFACE_TYPE_IPV6, 0,
949 0);
950 if (!ha->iface_ipv6_0)
951 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
952 "iface0.\n");
953
954 if (!ha->iface_ipv6_1)
955 /* IPv6 iface-1 */
956 ha->iface_ipv6_1 = iscsi_create_iface(ha->host,
957 &qla4xxx_iscsi_transport,
958 ISCSI_IFACE_TYPE_IPV6, 1,
959 0);
960 if (!ha->iface_ipv6_1)
961 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
962 "iface1.\n");
963}
964
965static void qla4xxx_create_ifaces(struct scsi_qla_host *ha)
966{
967 if (ha->ip_config.ipv4_options & IPOPT_IPV4_PROTOCOL_ENABLE)
968 qla4xxx_create_ipv4_iface(ha);
969
970 if (ha->ip_config.ipv6_options & IPV6_OPT_IPV6_PROTOCOL_ENABLE)
971 qla4xxx_create_ipv6_iface(ha);
972}
973
974static void qla4xxx_destroy_ipv4_iface(struct scsi_qla_host *ha)
975{
976 if (ha->iface_ipv4) {
977 iscsi_destroy_iface(ha->iface_ipv4);
978 ha->iface_ipv4 = NULL;
979 }
980}
981
982static void qla4xxx_destroy_ipv6_iface(struct scsi_qla_host *ha)
983{
984 if (ha->iface_ipv6_0) {
985 iscsi_destroy_iface(ha->iface_ipv6_0);
986 ha->iface_ipv6_0 = NULL;
987 }
988 if (ha->iface_ipv6_1) {
989 iscsi_destroy_iface(ha->iface_ipv6_1);
990 ha->iface_ipv6_1 = NULL;
991 }
992}
993
994static void qla4xxx_destroy_ifaces(struct scsi_qla_host *ha)
995{
996 qla4xxx_destroy_ipv4_iface(ha);
997 qla4xxx_destroy_ipv6_iface(ha);
998}
999
Mike Christied00efe32011-07-25 13:48:38 -05001000static void qla4xxx_set_ipv6(struct scsi_qla_host *ha,
1001 struct iscsi_iface_param_info *iface_param,
1002 struct addr_ctrl_blk *init_fw_cb)
1003{
1004 /*
1005 * iface_num 0 is valid for IPv6 Addr, linklocal, router, autocfg.
1006 * iface_num 1 is valid only for IPv6 Addr.
1007 */
1008 switch (iface_param->param) {
1009 case ISCSI_NET_PARAM_IPV6_ADDR:
1010 if (iface_param->iface_num & 0x1)
1011 /* IPv6 Addr 1 */
1012 memcpy(init_fw_cb->ipv6_addr1, iface_param->value,
1013 sizeof(init_fw_cb->ipv6_addr1));
1014 else
1015 /* IPv6 Addr 0 */
1016 memcpy(init_fw_cb->ipv6_addr0, iface_param->value,
1017 sizeof(init_fw_cb->ipv6_addr0));
1018 break;
1019 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
1020 if (iface_param->iface_num & 0x1)
1021 break;
1022 memcpy(init_fw_cb->ipv6_if_id, &iface_param->value[8],
1023 sizeof(init_fw_cb->ipv6_if_id));
1024 break;
1025 case ISCSI_NET_PARAM_IPV6_ROUTER:
1026 if (iface_param->iface_num & 0x1)
1027 break;
1028 memcpy(init_fw_cb->ipv6_dflt_rtr_addr, iface_param->value,
1029 sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
1030 break;
1031 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
1032 /* Autocfg applies to even interface */
1033 if (iface_param->iface_num & 0x1)
1034 break;
1035
1036 if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_DISABLE)
1037 init_fw_cb->ipv6_addtl_opts &=
1038 cpu_to_le16(
1039 ~IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
1040 else if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_ND_ENABLE)
1041 init_fw_cb->ipv6_addtl_opts |=
1042 cpu_to_le16(
1043 IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
1044 else
1045 ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
1046 "IPv6 addr\n");
1047 break;
1048 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
1049 /* Autocfg applies to even interface */
1050 if (iface_param->iface_num & 0x1)
1051 break;
1052
1053 if (iface_param->value[0] ==
1054 ISCSI_IPV6_LINKLOCAL_AUTOCFG_ENABLE)
1055 init_fw_cb->ipv6_addtl_opts |= cpu_to_le16(
1056 IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
1057 else if (iface_param->value[0] ==
1058 ISCSI_IPV6_LINKLOCAL_AUTOCFG_DISABLE)
1059 init_fw_cb->ipv6_addtl_opts &= cpu_to_le16(
1060 ~IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
1061 else
1062 ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
1063 "IPv6 linklocal addr\n");
1064 break;
1065 case ISCSI_NET_PARAM_IPV6_ROUTER_AUTOCFG:
1066 /* Autocfg applies to even interface */
1067 if (iface_param->iface_num & 0x1)
1068 break;
1069
1070 if (iface_param->value[0] == ISCSI_IPV6_ROUTER_AUTOCFG_ENABLE)
1071 memset(init_fw_cb->ipv6_dflt_rtr_addr, 0,
1072 sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
1073 break;
1074 case ISCSI_NET_PARAM_IFACE_ENABLE:
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05001075 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
Mike Christied00efe32011-07-25 13:48:38 -05001076 init_fw_cb->ipv6_opts |=
1077 cpu_to_le16(IPV6_OPT_IPV6_PROTOCOL_ENABLE);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05001078 qla4xxx_create_ipv6_iface(ha);
1079 } else {
Mike Christied00efe32011-07-25 13:48:38 -05001080 init_fw_cb->ipv6_opts &=
1081 cpu_to_le16(~IPV6_OPT_IPV6_PROTOCOL_ENABLE &
1082 0xFFFF);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05001083 qla4xxx_destroy_ipv6_iface(ha);
1084 }
Mike Christied00efe32011-07-25 13:48:38 -05001085 break;
Mike Christie2d636732011-10-11 17:55:11 -05001086 case ISCSI_NET_PARAM_VLAN_TAG:
Mike Christied00efe32011-07-25 13:48:38 -05001087 if (iface_param->len != sizeof(init_fw_cb->ipv6_vlan_tag))
1088 break;
Vikas Chaudhary6ac73e82011-07-25 13:48:49 -05001089 init_fw_cb->ipv6_vlan_tag =
1090 cpu_to_be16(*(uint16_t *)iface_param->value);
1091 break;
1092 case ISCSI_NET_PARAM_VLAN_ENABLED:
1093 if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
1094 init_fw_cb->ipv6_opts |=
1095 cpu_to_le16(IPV6_OPT_VLAN_TAGGING_ENABLE);
1096 else
1097 init_fw_cb->ipv6_opts &=
1098 cpu_to_le16(~IPV6_OPT_VLAN_TAGGING_ENABLE);
Mike Christied00efe32011-07-25 13:48:38 -05001099 break;
Vikas Chaudhary943c1572011-08-01 03:26:13 -07001100 case ISCSI_NET_PARAM_MTU:
1101 init_fw_cb->eth_mtu_size =
1102 cpu_to_le16(*(uint16_t *)iface_param->value);
1103 break;
Vikas Chaudhary2ada7fc2011-08-01 03:26:19 -07001104 case ISCSI_NET_PARAM_PORT:
1105 /* Autocfg applies to even interface */
1106 if (iface_param->iface_num & 0x1)
1107 break;
1108
1109 init_fw_cb->ipv6_port =
1110 cpu_to_le16(*(uint16_t *)iface_param->value);
1111 break;
Mike Christied00efe32011-07-25 13:48:38 -05001112 default:
1113 ql4_printk(KERN_ERR, ha, "Unknown IPv6 param = %d\n",
1114 iface_param->param);
1115 break;
1116 }
1117}
1118
1119static void qla4xxx_set_ipv4(struct scsi_qla_host *ha,
1120 struct iscsi_iface_param_info *iface_param,
1121 struct addr_ctrl_blk *init_fw_cb)
1122{
1123 switch (iface_param->param) {
1124 case ISCSI_NET_PARAM_IPV4_ADDR:
1125 memcpy(init_fw_cb->ipv4_addr, iface_param->value,
1126 sizeof(init_fw_cb->ipv4_addr));
1127 break;
1128 case ISCSI_NET_PARAM_IPV4_SUBNET:
1129 memcpy(init_fw_cb->ipv4_subnet, iface_param->value,
1130 sizeof(init_fw_cb->ipv4_subnet));
1131 break;
1132 case ISCSI_NET_PARAM_IPV4_GW:
1133 memcpy(init_fw_cb->ipv4_gw_addr, iface_param->value,
1134 sizeof(init_fw_cb->ipv4_gw_addr));
1135 break;
1136 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
1137 if (iface_param->value[0] == ISCSI_BOOTPROTO_DHCP)
1138 init_fw_cb->ipv4_tcp_opts |=
1139 cpu_to_le16(TCPOPT_DHCP_ENABLE);
1140 else if (iface_param->value[0] == ISCSI_BOOTPROTO_STATIC)
1141 init_fw_cb->ipv4_tcp_opts &=
1142 cpu_to_le16(~TCPOPT_DHCP_ENABLE);
1143 else
1144 ql4_printk(KERN_ERR, ha, "Invalid IPv4 bootproto\n");
1145 break;
1146 case ISCSI_NET_PARAM_IFACE_ENABLE:
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05001147 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
Mike Christied00efe32011-07-25 13:48:38 -05001148 init_fw_cb->ipv4_ip_opts |=
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -05001149 cpu_to_le16(IPOPT_IPV4_PROTOCOL_ENABLE);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05001150 qla4xxx_create_ipv4_iface(ha);
1151 } else {
Mike Christied00efe32011-07-25 13:48:38 -05001152 init_fw_cb->ipv4_ip_opts &=
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -05001153 cpu_to_le16(~IPOPT_IPV4_PROTOCOL_ENABLE &
Mike Christied00efe32011-07-25 13:48:38 -05001154 0xFFFF);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05001155 qla4xxx_destroy_ipv4_iface(ha);
1156 }
Mike Christied00efe32011-07-25 13:48:38 -05001157 break;
Mike Christie2d636732011-10-11 17:55:11 -05001158 case ISCSI_NET_PARAM_VLAN_TAG:
Mike Christied00efe32011-07-25 13:48:38 -05001159 if (iface_param->len != sizeof(init_fw_cb->ipv4_vlan_tag))
1160 break;
Vikas Chaudhary6ac73e82011-07-25 13:48:49 -05001161 init_fw_cb->ipv4_vlan_tag =
1162 cpu_to_be16(*(uint16_t *)iface_param->value);
1163 break;
1164 case ISCSI_NET_PARAM_VLAN_ENABLED:
1165 if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
1166 init_fw_cb->ipv4_ip_opts |=
1167 cpu_to_le16(IPOPT_VLAN_TAGGING_ENABLE);
1168 else
1169 init_fw_cb->ipv4_ip_opts &=
1170 cpu_to_le16(~IPOPT_VLAN_TAGGING_ENABLE);
Mike Christied00efe32011-07-25 13:48:38 -05001171 break;
Vikas Chaudhary943c1572011-08-01 03:26:13 -07001172 case ISCSI_NET_PARAM_MTU:
1173 init_fw_cb->eth_mtu_size =
1174 cpu_to_le16(*(uint16_t *)iface_param->value);
1175 break;
Vikas Chaudhary2ada7fc2011-08-01 03:26:19 -07001176 case ISCSI_NET_PARAM_PORT:
1177 init_fw_cb->ipv4_port =
1178 cpu_to_le16(*(uint16_t *)iface_param->value);
1179 break;
Mike Christied00efe32011-07-25 13:48:38 -05001180 default:
1181 ql4_printk(KERN_ERR, ha, "Unknown IPv4 param = %d\n",
1182 iface_param->param);
1183 break;
1184 }
1185}
1186
1187static void
1188qla4xxx_initcb_to_acb(struct addr_ctrl_blk *init_fw_cb)
1189{
1190 struct addr_ctrl_blk_def *acb;
1191 acb = (struct addr_ctrl_blk_def *)init_fw_cb;
1192 memset(acb->reserved1, 0, sizeof(acb->reserved1));
1193 memset(acb->reserved2, 0, sizeof(acb->reserved2));
1194 memset(acb->reserved3, 0, sizeof(acb->reserved3));
1195 memset(acb->reserved4, 0, sizeof(acb->reserved4));
1196 memset(acb->reserved5, 0, sizeof(acb->reserved5));
1197 memset(acb->reserved6, 0, sizeof(acb->reserved6));
1198 memset(acb->reserved7, 0, sizeof(acb->reserved7));
1199 memset(acb->reserved8, 0, sizeof(acb->reserved8));
1200 memset(acb->reserved9, 0, sizeof(acb->reserved9));
1201 memset(acb->reserved10, 0, sizeof(acb->reserved10));
1202 memset(acb->reserved11, 0, sizeof(acb->reserved11));
1203 memset(acb->reserved12, 0, sizeof(acb->reserved12));
1204 memset(acb->reserved13, 0, sizeof(acb->reserved13));
1205 memset(acb->reserved14, 0, sizeof(acb->reserved14));
1206 memset(acb->reserved15, 0, sizeof(acb->reserved15));
1207}
1208
1209static int
Mike Christie00c31882011-10-06 03:56:59 -05001210qla4xxx_iface_set_param(struct Scsi_Host *shost, void *data, uint32_t len)
Mike Christied00efe32011-07-25 13:48:38 -05001211{
1212 struct scsi_qla_host *ha = to_qla_host(shost);
1213 int rval = 0;
1214 struct iscsi_iface_param_info *iface_param = NULL;
1215 struct addr_ctrl_blk *init_fw_cb = NULL;
1216 dma_addr_t init_fw_cb_dma;
1217 uint32_t mbox_cmd[MBOX_REG_COUNT];
1218 uint32_t mbox_sts[MBOX_REG_COUNT];
Mike Christie00c31882011-10-06 03:56:59 -05001219 uint32_t rem = len;
1220 struct nlattr *attr;
Mike Christied00efe32011-07-25 13:48:38 -05001221
1222 init_fw_cb = dma_alloc_coherent(&ha->pdev->dev,
1223 sizeof(struct addr_ctrl_blk),
1224 &init_fw_cb_dma, GFP_KERNEL);
1225 if (!init_fw_cb) {
1226 ql4_printk(KERN_ERR, ha, "%s: Unable to alloc init_cb\n",
1227 __func__);
1228 return -ENOMEM;
1229 }
1230
1231 memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
1232 memset(&mbox_cmd, 0, sizeof(mbox_cmd));
1233 memset(&mbox_sts, 0, sizeof(mbox_sts));
1234
1235 if (qla4xxx_get_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma)) {
1236 ql4_printk(KERN_ERR, ha, "%s: get ifcb failed\n", __func__);
1237 rval = -EIO;
1238 goto exit_init_fw_cb;
1239 }
1240
Mike Christie00c31882011-10-06 03:56:59 -05001241 nla_for_each_attr(attr, data, len, rem) {
1242 iface_param = nla_data(attr);
Mike Christied00efe32011-07-25 13:48:38 -05001243
1244 if (iface_param->param_type != ISCSI_NET_PARAM)
1245 continue;
1246
1247 switch (iface_param->iface_type) {
1248 case ISCSI_IFACE_TYPE_IPV4:
1249 switch (iface_param->iface_num) {
1250 case 0:
1251 qla4xxx_set_ipv4(ha, iface_param, init_fw_cb);
1252 break;
1253 default:
1254 /* Cannot have more than one IPv4 interface */
1255 ql4_printk(KERN_ERR, ha, "Invalid IPv4 iface "
1256 "number = %d\n",
1257 iface_param->iface_num);
1258 break;
1259 }
1260 break;
1261 case ISCSI_IFACE_TYPE_IPV6:
1262 switch (iface_param->iface_num) {
1263 case 0:
1264 case 1:
1265 qla4xxx_set_ipv6(ha, iface_param, init_fw_cb);
1266 break;
1267 default:
1268 /* Cannot have more than two IPv6 interface */
1269 ql4_printk(KERN_ERR, ha, "Invalid IPv6 iface "
1270 "number = %d\n",
1271 iface_param->iface_num);
1272 break;
1273 }
1274 break;
1275 default:
1276 ql4_printk(KERN_ERR, ha, "Invalid iface type\n");
1277 break;
1278 }
Mike Christied00efe32011-07-25 13:48:38 -05001279 }
1280
1281 init_fw_cb->cookie = cpu_to_le32(0x11BEAD5A);
1282
1283 rval = qla4xxx_set_flash(ha, init_fw_cb_dma, FLASH_SEGMENT_IFCB,
1284 sizeof(struct addr_ctrl_blk),
1285 FLASH_OPT_RMW_COMMIT);
1286 if (rval != QLA_SUCCESS) {
1287 ql4_printk(KERN_ERR, ha, "%s: set flash mbx failed\n",
1288 __func__);
1289 rval = -EIO;
1290 goto exit_init_fw_cb;
1291 }
1292
Vikas Chaudharyce505f92011-12-01 22:42:10 -08001293 rval = qla4xxx_disable_acb(ha);
1294 if (rval != QLA_SUCCESS) {
1295 ql4_printk(KERN_ERR, ha, "%s: disable acb mbx failed\n",
1296 __func__);
1297 rval = -EIO;
1298 goto exit_init_fw_cb;
1299 }
1300
1301 wait_for_completion_timeout(&ha->disable_acb_comp,
1302 DISABLE_ACB_TOV * HZ);
Mike Christied00efe32011-07-25 13:48:38 -05001303
1304 qla4xxx_initcb_to_acb(init_fw_cb);
1305
1306 rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma);
1307 if (rval != QLA_SUCCESS) {
1308 ql4_printk(KERN_ERR, ha, "%s: set acb mbx failed\n",
1309 __func__);
1310 rval = -EIO;
1311 goto exit_init_fw_cb;
1312 }
1313
1314 memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
1315 qla4xxx_update_local_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb,
1316 init_fw_cb_dma);
1317
1318exit_init_fw_cb:
1319 dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk),
1320 init_fw_cb, init_fw_cb_dma);
1321
1322 return rval;
1323}
1324
Mike Christiefca9f042012-02-27 03:08:54 -08001325static int qla4xxx_session_get_param(struct iscsi_cls_session *cls_sess,
1326 enum iscsi_param param, char *buf)
1327{
1328 struct iscsi_session *sess = cls_sess->dd_data;
1329 struct ddb_entry *ddb_entry = sess->dd_data;
1330 struct scsi_qla_host *ha = ddb_entry->ha;
1331 int rval, len;
1332 uint16_t idx;
1333
1334 switch (param) {
1335 case ISCSI_PARAM_CHAP_IN_IDX:
1336 rval = qla4xxx_get_chap_index(ha, sess->username_in,
1337 sess->password_in, BIDI_CHAP,
1338 &idx);
1339 if (rval)
Manish Rangankarc7a5e0d2013-01-20 23:51:04 -05001340 len = sprintf(buf, "\n");
1341 else
1342 len = sprintf(buf, "%hu\n", idx);
Mike Christiefca9f042012-02-27 03:08:54 -08001343 break;
1344 case ISCSI_PARAM_CHAP_OUT_IDX:
1345 rval = qla4xxx_get_chap_index(ha, sess->username,
1346 sess->password, LOCAL_CHAP,
1347 &idx);
1348 if (rval)
Manish Rangankarc7a5e0d2013-01-20 23:51:04 -05001349 len = sprintf(buf, "\n");
1350 else
1351 len = sprintf(buf, "%hu\n", idx);
Mike Christiefca9f042012-02-27 03:08:54 -08001352 break;
1353 default:
1354 return iscsi_session_get_param(cls_sess, param, buf);
1355 }
1356
1357 return len;
1358}
1359
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001360static int qla4xxx_conn_get_param(struct iscsi_cls_conn *cls_conn,
David Somayajuluafaf5a22006-09-19 10:28:00 -07001361 enum iscsi_param param, char *buf)
1362{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001363 struct iscsi_conn *conn;
1364 struct qla_conn *qla_conn;
1365 struct sockaddr *dst_addr;
1366 int len = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001367
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001368 conn = cls_conn->dd_data;
1369 qla_conn = conn->dd_data;
Manish Rangankard46bdeb2012-08-07 07:57:13 -04001370 dst_addr = (struct sockaddr *)&qla_conn->qla_ep->dst_addr;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001371
1372 switch (param) {
1373 case ISCSI_PARAM_CONN_PORT:
David Somayajuluafaf5a22006-09-19 10:28:00 -07001374 case ISCSI_PARAM_CONN_ADDRESS:
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001375 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
1376 dst_addr, param, buf);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001377 default:
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001378 return iscsi_conn_get_param(cls_conn, param, buf);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001379 }
1380
1381 return len;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001382
David Somayajuluafaf5a22006-09-19 10:28:00 -07001383}
1384
Mike Christie13483732011-12-01 21:38:41 -06001385int qla4xxx_get_ddb_index(struct scsi_qla_host *ha, uint16_t *ddb_index)
1386{
1387 uint32_t mbx_sts = 0;
1388 uint16_t tmp_ddb_index;
1389 int ret;
1390
1391get_ddb_index:
1392 tmp_ddb_index = find_first_zero_bit(ha->ddb_idx_map, MAX_DDB_ENTRIES);
1393
1394 if (tmp_ddb_index >= MAX_DDB_ENTRIES) {
1395 DEBUG2(ql4_printk(KERN_INFO, ha,
1396 "Free DDB index not available\n"));
1397 ret = QLA_ERROR;
1398 goto exit_get_ddb_index;
1399 }
1400
1401 if (test_and_set_bit(tmp_ddb_index, ha->ddb_idx_map))
1402 goto get_ddb_index;
1403
1404 DEBUG2(ql4_printk(KERN_INFO, ha,
1405 "Found a free DDB index at %d\n", tmp_ddb_index));
1406 ret = qla4xxx_req_ddb_entry(ha, tmp_ddb_index, &mbx_sts);
1407 if (ret == QLA_ERROR) {
1408 if (mbx_sts == MBOX_STS_COMMAND_ERROR) {
1409 ql4_printk(KERN_INFO, ha,
1410 "DDB index = %d not available trying next\n",
1411 tmp_ddb_index);
1412 goto get_ddb_index;
1413 }
1414 DEBUG2(ql4_printk(KERN_INFO, ha,
1415 "Free FW DDB not available\n"));
1416 }
1417
1418 *ddb_index = tmp_ddb_index;
1419
1420exit_get_ddb_index:
1421 return ret;
1422}
1423
1424static int qla4xxx_match_ipaddress(struct scsi_qla_host *ha,
1425 struct ddb_entry *ddb_entry,
1426 char *existing_ipaddr,
1427 char *user_ipaddr)
1428{
1429 uint8_t dst_ipaddr[IPv6_ADDR_LEN];
1430 char formatted_ipaddr[DDB_IPADDR_LEN];
1431 int status = QLA_SUCCESS, ret = 0;
1432
1433 if (ddb_entry->fw_ddb_entry.options & DDB_OPT_IPV6_DEVICE) {
1434 ret = in6_pton(user_ipaddr, strlen(user_ipaddr), dst_ipaddr,
1435 '\0', NULL);
1436 if (ret == 0) {
1437 status = QLA_ERROR;
1438 goto out_match;
1439 }
1440 ret = sprintf(formatted_ipaddr, "%pI6", dst_ipaddr);
1441 } else {
1442 ret = in4_pton(user_ipaddr, strlen(user_ipaddr), dst_ipaddr,
1443 '\0', NULL);
1444 if (ret == 0) {
1445 status = QLA_ERROR;
1446 goto out_match;
1447 }
1448 ret = sprintf(formatted_ipaddr, "%pI4", dst_ipaddr);
1449 }
1450
1451 if (strcmp(existing_ipaddr, formatted_ipaddr))
1452 status = QLA_ERROR;
1453
1454out_match:
1455 return status;
1456}
1457
1458static int qla4xxx_match_fwdb_session(struct scsi_qla_host *ha,
1459 struct iscsi_cls_conn *cls_conn)
1460{
1461 int idx = 0, max_ddbs, rval;
1462 struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
1463 struct iscsi_session *sess, *existing_sess;
1464 struct iscsi_conn *conn, *existing_conn;
1465 struct ddb_entry *ddb_entry;
1466
1467 sess = cls_sess->dd_data;
1468 conn = cls_conn->dd_data;
1469
1470 if (sess->targetname == NULL ||
1471 conn->persistent_address == NULL ||
1472 conn->persistent_port == 0)
1473 return QLA_ERROR;
1474
1475 max_ddbs = is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
1476 MAX_DEV_DB_ENTRIES;
1477
1478 for (idx = 0; idx < max_ddbs; idx++) {
1479 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
1480 if (ddb_entry == NULL)
1481 continue;
1482
1483 if (ddb_entry->ddb_type != FLASH_DDB)
1484 continue;
1485
1486 existing_sess = ddb_entry->sess->dd_data;
1487 existing_conn = ddb_entry->conn->dd_data;
1488
1489 if (existing_sess->targetname == NULL ||
1490 existing_conn->persistent_address == NULL ||
1491 existing_conn->persistent_port == 0)
1492 continue;
1493
1494 DEBUG2(ql4_printk(KERN_INFO, ha,
1495 "IQN = %s User IQN = %s\n",
1496 existing_sess->targetname,
1497 sess->targetname));
1498
1499 DEBUG2(ql4_printk(KERN_INFO, ha,
1500 "IP = %s User IP = %s\n",
1501 existing_conn->persistent_address,
1502 conn->persistent_address));
1503
1504 DEBUG2(ql4_printk(KERN_INFO, ha,
1505 "Port = %d User Port = %d\n",
1506 existing_conn->persistent_port,
1507 conn->persistent_port));
1508
1509 if (strcmp(existing_sess->targetname, sess->targetname))
1510 continue;
1511 rval = qla4xxx_match_ipaddress(ha, ddb_entry,
1512 existing_conn->persistent_address,
1513 conn->persistent_address);
1514 if (rval == QLA_ERROR)
1515 continue;
1516 if (existing_conn->persistent_port != conn->persistent_port)
1517 continue;
1518 break;
1519 }
1520
1521 if (idx == max_ddbs)
1522 return QLA_ERROR;
1523
1524 DEBUG2(ql4_printk(KERN_INFO, ha,
1525 "Match found in fwdb sessions\n"));
1526 return QLA_SUCCESS;
1527}
1528
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001529static struct iscsi_cls_session *
1530qla4xxx_session_create(struct iscsi_endpoint *ep,
1531 uint16_t cmds_max, uint16_t qdepth,
1532 uint32_t initial_cmdsn)
1533{
1534 struct iscsi_cls_session *cls_sess;
1535 struct scsi_qla_host *ha;
1536 struct qla_endpoint *qla_ep;
1537 struct ddb_entry *ddb_entry;
Mike Christie13483732011-12-01 21:38:41 -06001538 uint16_t ddb_index;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001539 struct iscsi_session *sess;
1540 struct sockaddr *dst_addr;
1541 int ret;
1542
1543 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1544 if (!ep) {
1545 printk(KERN_ERR "qla4xxx: missing ep.\n");
1546 return NULL;
1547 }
1548
1549 qla_ep = ep->dd_data;
1550 dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
1551 ha = to_qla_host(qla_ep->host);
Manish Rangankar736cf362011-10-07 16:55:46 -07001552
Mike Christie13483732011-12-01 21:38:41 -06001553 ret = qla4xxx_get_ddb_index(ha, &ddb_index);
1554 if (ret == QLA_ERROR)
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001555 return NULL;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001556
1557 cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport, qla_ep->host,
1558 cmds_max, sizeof(struct ddb_entry),
1559 sizeof(struct ql4_task_data),
1560 initial_cmdsn, ddb_index);
1561 if (!cls_sess)
1562 return NULL;
1563
1564 sess = cls_sess->dd_data;
1565 ddb_entry = sess->dd_data;
1566 ddb_entry->fw_ddb_index = ddb_index;
1567 ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
1568 ddb_entry->ha = ha;
1569 ddb_entry->sess = cls_sess;
Mike Christie13483732011-12-01 21:38:41 -06001570 ddb_entry->unblock_sess = qla4xxx_unblock_ddb;
1571 ddb_entry->ddb_change = qla4xxx_ddb_change;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001572 cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
1573 ha->fw_ddb_index_map[ddb_entry->fw_ddb_index] = ddb_entry;
1574 ha->tot_ddbs++;
1575
1576 return cls_sess;
1577}
1578
1579static void qla4xxx_session_destroy(struct iscsi_cls_session *cls_sess)
1580{
1581 struct iscsi_session *sess;
1582 struct ddb_entry *ddb_entry;
1583 struct scsi_qla_host *ha;
Manish Rangankar90599b62012-04-23 22:32:34 -07001584 unsigned long flags, wtime;
1585 struct dev_db_entry *fw_ddb_entry = NULL;
1586 dma_addr_t fw_ddb_entry_dma;
1587 uint32_t ddb_state;
1588 int ret;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001589
1590 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1591 sess = cls_sess->dd_data;
1592 ddb_entry = sess->dd_data;
1593 ha = ddb_entry->ha;
1594
Manish Rangankar90599b62012-04-23 22:32:34 -07001595 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1596 &fw_ddb_entry_dma, GFP_KERNEL);
1597 if (!fw_ddb_entry) {
1598 ql4_printk(KERN_ERR, ha,
1599 "%s: Unable to allocate dma buffer\n", __func__);
1600 goto destroy_session;
1601 }
1602
1603 wtime = jiffies + (HZ * LOGOUT_TOV);
1604 do {
1605 ret = qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index,
1606 fw_ddb_entry, fw_ddb_entry_dma,
1607 NULL, NULL, &ddb_state, NULL,
1608 NULL, NULL);
1609 if (ret == QLA_ERROR)
1610 goto destroy_session;
1611
1612 if ((ddb_state == DDB_DS_NO_CONNECTION_ACTIVE) ||
1613 (ddb_state == DDB_DS_SESSION_FAILED))
1614 goto destroy_session;
1615
1616 schedule_timeout_uninterruptible(HZ);
1617 } while ((time_after(wtime, jiffies)));
1618
1619destroy_session:
Manish Rangankar736cf362011-10-07 16:55:46 -07001620 qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
1621
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001622 spin_lock_irqsave(&ha->hardware_lock, flags);
1623 qla4xxx_free_ddb(ha, ddb_entry);
1624 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Manish Rangankar90599b62012-04-23 22:32:34 -07001625
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001626 iscsi_session_teardown(cls_sess);
Manish Rangankar90599b62012-04-23 22:32:34 -07001627
1628 if (fw_ddb_entry)
1629 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1630 fw_ddb_entry, fw_ddb_entry_dma);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001631}
1632
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001633static struct iscsi_cls_conn *
1634qla4xxx_conn_create(struct iscsi_cls_session *cls_sess, uint32_t conn_idx)
1635{
1636 struct iscsi_cls_conn *cls_conn;
1637 struct iscsi_session *sess;
1638 struct ddb_entry *ddb_entry;
1639
1640 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1641 cls_conn = iscsi_conn_setup(cls_sess, sizeof(struct qla_conn),
1642 conn_idx);
Mike Christieff1d0312011-12-01 21:38:43 -06001643 if (!cls_conn)
1644 return NULL;
1645
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001646 sess = cls_sess->dd_data;
1647 ddb_entry = sess->dd_data;
1648 ddb_entry->conn = cls_conn;
1649
1650 return cls_conn;
1651}
1652
1653static int qla4xxx_conn_bind(struct iscsi_cls_session *cls_session,
1654 struct iscsi_cls_conn *cls_conn,
1655 uint64_t transport_fd, int is_leading)
1656{
1657 struct iscsi_conn *conn;
1658 struct qla_conn *qla_conn;
1659 struct iscsi_endpoint *ep;
1660
1661 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1662
1663 if (iscsi_conn_bind(cls_session, cls_conn, is_leading))
1664 return -EINVAL;
1665 ep = iscsi_lookup_endpoint(transport_fd);
1666 conn = cls_conn->dd_data;
1667 qla_conn = conn->dd_data;
1668 qla_conn->qla_ep = ep->dd_data;
1669 return 0;
1670}
1671
1672static int qla4xxx_conn_start(struct iscsi_cls_conn *cls_conn)
1673{
1674 struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
1675 struct iscsi_session *sess;
1676 struct ddb_entry *ddb_entry;
1677 struct scsi_qla_host *ha;
Mike Christie13483732011-12-01 21:38:41 -06001678 struct dev_db_entry *fw_ddb_entry = NULL;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001679 dma_addr_t fw_ddb_entry_dma;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001680 uint32_t mbx_sts = 0;
1681 int ret = 0;
1682 int status = QLA_SUCCESS;
1683
1684 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1685 sess = cls_sess->dd_data;
1686 ddb_entry = sess->dd_data;
1687 ha = ddb_entry->ha;
1688
Mike Christie13483732011-12-01 21:38:41 -06001689 /* Check if we have matching FW DDB, if yes then do not
1690 * login to this target. This could cause target to logout previous
1691 * connection
1692 */
1693 ret = qla4xxx_match_fwdb_session(ha, cls_conn);
1694 if (ret == QLA_SUCCESS) {
1695 ql4_printk(KERN_INFO, ha,
1696 "Session already exist in FW.\n");
1697 ret = -EEXIST;
1698 goto exit_conn_start;
1699 }
1700
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001701 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1702 &fw_ddb_entry_dma, GFP_KERNEL);
1703 if (!fw_ddb_entry) {
1704 ql4_printk(KERN_ERR, ha,
1705 "%s: Unable to allocate dma buffer\n", __func__);
Mike Christie13483732011-12-01 21:38:41 -06001706 ret = -ENOMEM;
1707 goto exit_conn_start;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001708 }
1709
1710 ret = qla4xxx_set_param_ddbentry(ha, ddb_entry, cls_conn, &mbx_sts);
1711 if (ret) {
1712 /* If iscsid is stopped and started then no need to do
1713 * set param again since ddb state will be already
1714 * active and FW does not allow set ddb to an
1715 * active session.
1716 */
1717 if (mbx_sts)
1718 if (ddb_entry->fw_ddb_device_state ==
Manish Rangankarf922da72011-10-07 16:55:49 -07001719 DDB_DS_SESSION_ACTIVE) {
Mike Christie13483732011-12-01 21:38:41 -06001720 ddb_entry->unblock_sess(ddb_entry->sess);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001721 goto exit_set_param;
Manish Rangankarf922da72011-10-07 16:55:49 -07001722 }
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001723
1724 ql4_printk(KERN_ERR, ha, "%s: Failed set param for index[%d]\n",
1725 __func__, ddb_entry->fw_ddb_index);
1726 goto exit_conn_start;
1727 }
1728
1729 status = qla4xxx_conn_open(ha, ddb_entry->fw_ddb_index);
1730 if (status == QLA_ERROR) {
Manish Rangankar0e7e8502011-07-25 13:48:54 -05001731 ql4_printk(KERN_ERR, ha, "%s: Login failed: %s\n", __func__,
1732 sess->targetname);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001733 ret = -EINVAL;
1734 goto exit_conn_start;
1735 }
1736
Manish Rangankar98270ab2011-10-07 16:55:47 -07001737 if (ddb_entry->fw_ddb_device_state == DDB_DS_NO_CONNECTION_ACTIVE)
1738 ddb_entry->fw_ddb_device_state = DDB_DS_LOGIN_IN_PROCESS;
1739
1740 DEBUG2(printk(KERN_INFO "%s: DDB state [%d]\n", __func__,
1741 ddb_entry->fw_ddb_device_state));
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001742
1743exit_set_param:
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001744 ret = 0;
1745
1746exit_conn_start:
Mike Christie13483732011-12-01 21:38:41 -06001747 if (fw_ddb_entry)
1748 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1749 fw_ddb_entry, fw_ddb_entry_dma);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001750 return ret;
1751}
1752
1753static void qla4xxx_conn_destroy(struct iscsi_cls_conn *cls_conn)
1754{
1755 struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
1756 struct iscsi_session *sess;
1757 struct scsi_qla_host *ha;
1758 struct ddb_entry *ddb_entry;
1759 int options;
1760
1761 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1762 sess = cls_sess->dd_data;
1763 ddb_entry = sess->dd_data;
1764 ha = ddb_entry->ha;
1765
1766 options = LOGOUT_OPTION_CLOSE_SESSION;
1767 if (qla4xxx_session_logout_ddb(ha, ddb_entry, options) == QLA_ERROR)
1768 ql4_printk(KERN_ERR, ha, "%s: Logout failed\n", __func__);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001769}
1770
1771static void qla4xxx_task_work(struct work_struct *wdata)
1772{
1773 struct ql4_task_data *task_data;
1774 struct scsi_qla_host *ha;
1775 struct passthru_status *sts;
1776 struct iscsi_task *task;
1777 struct iscsi_hdr *hdr;
1778 uint8_t *data;
1779 uint32_t data_len;
1780 struct iscsi_conn *conn;
1781 int hdr_len;
1782 itt_t itt;
1783
1784 task_data = container_of(wdata, struct ql4_task_data, task_work);
1785 ha = task_data->ha;
1786 task = task_data->task;
1787 sts = &task_data->sts;
1788 hdr_len = sizeof(struct iscsi_hdr);
1789
1790 DEBUG3(printk(KERN_INFO "Status returned\n"));
1791 DEBUG3(qla4xxx_dump_buffer(sts, 64));
1792 DEBUG3(printk(KERN_INFO "Response buffer"));
1793 DEBUG3(qla4xxx_dump_buffer(task_data->resp_buffer, 64));
1794
1795 conn = task->conn;
1796
1797 switch (sts->completionStatus) {
1798 case PASSTHRU_STATUS_COMPLETE:
1799 hdr = (struct iscsi_hdr *)task_data->resp_buffer;
1800 /* Assign back the itt in hdr, until we use the PREASSIGN_TAG */
1801 itt = sts->handle;
1802 hdr->itt = itt;
1803 data = task_data->resp_buffer + hdr_len;
1804 data_len = task_data->resp_len - hdr_len;
1805 iscsi_complete_pdu(conn, hdr, data, data_len);
1806 break;
1807 default:
1808 ql4_printk(KERN_ERR, ha, "Passthru failed status = 0x%x\n",
1809 sts->completionStatus);
1810 break;
1811 }
1812 return;
1813}
1814
1815static int qla4xxx_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
1816{
1817 struct ql4_task_data *task_data;
1818 struct iscsi_session *sess;
1819 struct ddb_entry *ddb_entry;
1820 struct scsi_qla_host *ha;
1821 int hdr_len;
1822
1823 sess = task->conn->session;
1824 ddb_entry = sess->dd_data;
1825 ha = ddb_entry->ha;
1826 task_data = task->dd_data;
1827 memset(task_data, 0, sizeof(struct ql4_task_data));
1828
1829 if (task->sc) {
1830 ql4_printk(KERN_INFO, ha,
1831 "%s: SCSI Commands not implemented\n", __func__);
1832 return -EINVAL;
1833 }
1834
1835 hdr_len = sizeof(struct iscsi_hdr);
1836 task_data->ha = ha;
1837 task_data->task = task;
1838
1839 if (task->data_count) {
1840 task_data->data_dma = dma_map_single(&ha->pdev->dev, task->data,
1841 task->data_count,
1842 PCI_DMA_TODEVICE);
1843 }
1844
1845 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
1846 __func__, task->conn->max_recv_dlength, hdr_len));
1847
Manish Rangankar69ca2162011-10-07 16:55:50 -07001848 task_data->resp_len = task->conn->max_recv_dlength + hdr_len;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001849 task_data->resp_buffer = dma_alloc_coherent(&ha->pdev->dev,
1850 task_data->resp_len,
1851 &task_data->resp_dma,
1852 GFP_ATOMIC);
1853 if (!task_data->resp_buffer)
1854 goto exit_alloc_pdu;
1855
Manish Rangankar69ca2162011-10-07 16:55:50 -07001856 task_data->req_len = task->data_count + hdr_len;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001857 task_data->req_buffer = dma_alloc_coherent(&ha->pdev->dev,
Manish Rangankar69ca2162011-10-07 16:55:50 -07001858 task_data->req_len,
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001859 &task_data->req_dma,
1860 GFP_ATOMIC);
1861 if (!task_data->req_buffer)
1862 goto exit_alloc_pdu;
1863
1864 task->hdr = task_data->req_buffer;
1865
1866 INIT_WORK(&task_data->task_work, qla4xxx_task_work);
1867
1868 return 0;
1869
1870exit_alloc_pdu:
1871 if (task_data->resp_buffer)
1872 dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
1873 task_data->resp_buffer, task_data->resp_dma);
1874
1875 if (task_data->req_buffer)
Manish Rangankar69ca2162011-10-07 16:55:50 -07001876 dma_free_coherent(&ha->pdev->dev, task_data->req_len,
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001877 task_data->req_buffer, task_data->req_dma);
1878 return -ENOMEM;
1879}
1880
1881static void qla4xxx_task_cleanup(struct iscsi_task *task)
1882{
1883 struct ql4_task_data *task_data;
1884 struct iscsi_session *sess;
1885 struct ddb_entry *ddb_entry;
1886 struct scsi_qla_host *ha;
1887 int hdr_len;
1888
1889 hdr_len = sizeof(struct iscsi_hdr);
1890 sess = task->conn->session;
1891 ddb_entry = sess->dd_data;
1892 ha = ddb_entry->ha;
1893 task_data = task->dd_data;
1894
1895 if (task->data_count) {
1896 dma_unmap_single(&ha->pdev->dev, task_data->data_dma,
1897 task->data_count, PCI_DMA_TODEVICE);
1898 }
1899
1900 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
1901 __func__, task->conn->max_recv_dlength, hdr_len));
1902
1903 dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
1904 task_data->resp_buffer, task_data->resp_dma);
Manish Rangankar69ca2162011-10-07 16:55:50 -07001905 dma_free_coherent(&ha->pdev->dev, task_data->req_len,
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001906 task_data->req_buffer, task_data->req_dma);
1907 return;
1908}
1909
1910static int qla4xxx_task_xmit(struct iscsi_task *task)
1911{
1912 struct scsi_cmnd *sc = task->sc;
1913 struct iscsi_session *sess = task->conn->session;
1914 struct ddb_entry *ddb_entry = sess->dd_data;
1915 struct scsi_qla_host *ha = ddb_entry->ha;
1916
1917 if (!sc)
1918 return qla4xxx_send_passthru0(task);
1919
1920 ql4_printk(KERN_INFO, ha, "%s: scsi cmd xmit not implemented\n",
1921 __func__);
1922 return -ENOSYS;
1923}
1924
Mike Christie13483732011-12-01 21:38:41 -06001925static void qla4xxx_copy_fwddb_param(struct scsi_qla_host *ha,
1926 struct dev_db_entry *fw_ddb_entry,
1927 struct iscsi_cls_session *cls_sess,
1928 struct iscsi_cls_conn *cls_conn)
1929{
1930 int buflen = 0;
1931 struct iscsi_session *sess;
Nilesh Javali376738a2012-02-27 03:08:52 -08001932 struct ddb_entry *ddb_entry;
Mike Christie13483732011-12-01 21:38:41 -06001933 struct iscsi_conn *conn;
1934 char ip_addr[DDB_IPADDR_LEN];
1935 uint16_t options = 0;
1936
1937 sess = cls_sess->dd_data;
Nilesh Javali376738a2012-02-27 03:08:52 -08001938 ddb_entry = sess->dd_data;
Mike Christie13483732011-12-01 21:38:41 -06001939 conn = cls_conn->dd_data;
1940
Nilesh Javali376738a2012-02-27 03:08:52 -08001941 ddb_entry->chap_tbl_idx = le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
1942
Mike Christie13483732011-12-01 21:38:41 -06001943 conn->max_recv_dlength = BYTE_UNITS *
1944 le16_to_cpu(fw_ddb_entry->iscsi_max_rcv_data_seg_len);
1945
1946 conn->max_xmit_dlength = BYTE_UNITS *
1947 le16_to_cpu(fw_ddb_entry->iscsi_max_snd_data_seg_len);
1948
1949 sess->initial_r2t_en =
1950 (BIT_10 & le16_to_cpu(fw_ddb_entry->iscsi_options));
1951
1952 sess->max_r2t = le16_to_cpu(fw_ddb_entry->iscsi_max_outsnd_r2t);
1953
1954 sess->imm_data_en = (BIT_11 & le16_to_cpu(fw_ddb_entry->iscsi_options));
1955
1956 sess->first_burst = BYTE_UNITS *
1957 le16_to_cpu(fw_ddb_entry->iscsi_first_burst_len);
1958
1959 sess->max_burst = BYTE_UNITS *
1960 le16_to_cpu(fw_ddb_entry->iscsi_max_burst_len);
1961
1962 sess->time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
1963
1964 sess->time2retain = le16_to_cpu(fw_ddb_entry->iscsi_def_time2retain);
1965
1966 conn->persistent_port = le16_to_cpu(fw_ddb_entry->port);
1967
1968 sess->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
1969
1970 options = le16_to_cpu(fw_ddb_entry->options);
1971 if (options & DDB_OPT_IPV6_DEVICE)
1972 sprintf(ip_addr, "%pI6", fw_ddb_entry->ip_addr);
1973 else
1974 sprintf(ip_addr, "%pI4", fw_ddb_entry->ip_addr);
1975
1976 iscsi_set_param(cls_conn, ISCSI_PARAM_TARGET_NAME,
1977 (char *)fw_ddb_entry->iscsi_name, buflen);
1978 iscsi_set_param(cls_conn, ISCSI_PARAM_INITIATOR_NAME,
1979 (char *)ha->name_string, buflen);
1980 iscsi_set_param(cls_conn, ISCSI_PARAM_PERSISTENT_ADDRESS,
1981 (char *)ip_addr, buflen);
Vikas Chaudhary6c1b8782012-01-19 03:06:54 -08001982 iscsi_set_param(cls_conn, ISCSI_PARAM_TARGET_ALIAS,
1983 (char *)fw_ddb_entry->iscsi_alias, buflen);
Mike Christie13483732011-12-01 21:38:41 -06001984}
1985
1986void qla4xxx_update_session_conn_fwddb_param(struct scsi_qla_host *ha,
1987 struct ddb_entry *ddb_entry)
1988{
1989 struct iscsi_cls_session *cls_sess;
1990 struct iscsi_cls_conn *cls_conn;
1991 uint32_t ddb_state;
1992 dma_addr_t fw_ddb_entry_dma;
1993 struct dev_db_entry *fw_ddb_entry;
1994
1995 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1996 &fw_ddb_entry_dma, GFP_KERNEL);
1997 if (!fw_ddb_entry) {
1998 ql4_printk(KERN_ERR, ha,
1999 "%s: Unable to allocate dma buffer\n", __func__);
2000 goto exit_session_conn_fwddb_param;
2001 }
2002
2003 if (qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index, fw_ddb_entry,
2004 fw_ddb_entry_dma, NULL, NULL, &ddb_state,
2005 NULL, NULL, NULL) == QLA_ERROR) {
2006 DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: failed "
2007 "get_ddb_entry for fw_ddb_index %d\n",
2008 ha->host_no, __func__,
2009 ddb_entry->fw_ddb_index));
2010 goto exit_session_conn_fwddb_param;
2011 }
2012
2013 cls_sess = ddb_entry->sess;
2014
2015 cls_conn = ddb_entry->conn;
2016
2017 /* Update params */
2018 qla4xxx_copy_fwddb_param(ha, fw_ddb_entry, cls_sess, cls_conn);
2019
2020exit_session_conn_fwddb_param:
2021 if (fw_ddb_entry)
2022 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
2023 fw_ddb_entry, fw_ddb_entry_dma);
2024}
2025
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002026void qla4xxx_update_session_conn_param(struct scsi_qla_host *ha,
2027 struct ddb_entry *ddb_entry)
2028{
2029 struct iscsi_cls_session *cls_sess;
2030 struct iscsi_cls_conn *cls_conn;
2031 struct iscsi_session *sess;
2032 struct iscsi_conn *conn;
2033 uint32_t ddb_state;
2034 dma_addr_t fw_ddb_entry_dma;
2035 struct dev_db_entry *fw_ddb_entry;
2036
2037 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
2038 &fw_ddb_entry_dma, GFP_KERNEL);
2039 if (!fw_ddb_entry) {
2040 ql4_printk(KERN_ERR, ha,
2041 "%s: Unable to allocate dma buffer\n", __func__);
Mike Christie13483732011-12-01 21:38:41 -06002042 goto exit_session_conn_param;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002043 }
2044
2045 if (qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index, fw_ddb_entry,
2046 fw_ddb_entry_dma, NULL, NULL, &ddb_state,
2047 NULL, NULL, NULL) == QLA_ERROR) {
2048 DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: failed "
2049 "get_ddb_entry for fw_ddb_index %d\n",
2050 ha->host_no, __func__,
2051 ddb_entry->fw_ddb_index));
Mike Christie13483732011-12-01 21:38:41 -06002052 goto exit_session_conn_param;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002053 }
2054
2055 cls_sess = ddb_entry->sess;
2056 sess = cls_sess->dd_data;
2057
2058 cls_conn = ddb_entry->conn;
2059 conn = cls_conn->dd_data;
2060
Mike Christie13483732011-12-01 21:38:41 -06002061 /* Update timers after login */
2062 ddb_entry->default_relogin_timeout =
Nilesh Javalic28eaac2011-12-18 21:40:44 -08002063 (le16_to_cpu(fw_ddb_entry->def_timeout) > LOGIN_TOV) &&
2064 (le16_to_cpu(fw_ddb_entry->def_timeout) < LOGIN_TOV * 10) ?
2065 le16_to_cpu(fw_ddb_entry->def_timeout) : LOGIN_TOV;
Mike Christie13483732011-12-01 21:38:41 -06002066 ddb_entry->default_time2wait =
2067 le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
2068
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002069 /* Update params */
Nilesh Javali376738a2012-02-27 03:08:52 -08002070 ddb_entry->chap_tbl_idx = le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002071 conn->max_recv_dlength = BYTE_UNITS *
2072 le16_to_cpu(fw_ddb_entry->iscsi_max_rcv_data_seg_len);
2073
2074 conn->max_xmit_dlength = BYTE_UNITS *
2075 le16_to_cpu(fw_ddb_entry->iscsi_max_snd_data_seg_len);
2076
2077 sess->initial_r2t_en =
2078 (BIT_10 & le16_to_cpu(fw_ddb_entry->iscsi_options));
2079
2080 sess->max_r2t = le16_to_cpu(fw_ddb_entry->iscsi_max_outsnd_r2t);
2081
2082 sess->imm_data_en = (BIT_11 & le16_to_cpu(fw_ddb_entry->iscsi_options));
2083
2084 sess->first_burst = BYTE_UNITS *
2085 le16_to_cpu(fw_ddb_entry->iscsi_first_burst_len);
2086
2087 sess->max_burst = BYTE_UNITS *
2088 le16_to_cpu(fw_ddb_entry->iscsi_max_burst_len);
2089
2090 sess->time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
2091
2092 sess->time2retain = le16_to_cpu(fw_ddb_entry->iscsi_def_time2retain);
2093
2094 sess->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
2095
2096 memcpy(sess->initiatorname, ha->name_string,
2097 min(sizeof(ha->name_string), sizeof(sess->initiatorname)));
Mike Christie13483732011-12-01 21:38:41 -06002098
Vikas Chaudhary6c1b8782012-01-19 03:06:54 -08002099 iscsi_set_param(cls_conn, ISCSI_PARAM_TARGET_ALIAS,
2100 (char *)fw_ddb_entry->iscsi_alias, 0);
2101
Mike Christie13483732011-12-01 21:38:41 -06002102exit_session_conn_param:
2103 if (fw_ddb_entry)
2104 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
2105 fw_ddb_entry, fw_ddb_entry_dma);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002106}
2107
David Somayajuluafaf5a22006-09-19 10:28:00 -07002108/*
2109 * Timer routines
2110 */
2111
2112static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
2113 unsigned long interval)
2114{
2115 DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
2116 __func__, ha->host->host_no));
2117 init_timer(&ha->timer);
2118 ha->timer.expires = jiffies + interval * HZ;
2119 ha->timer.data = (unsigned long)ha;
2120 ha->timer.function = (void (*)(unsigned long))func;
2121 add_timer(&ha->timer);
2122 ha->timer_active = 1;
2123}
2124
2125static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
2126{
2127 del_timer_sync(&ha->timer);
2128 ha->timer_active = 0;
2129}
2130
2131/***
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002132 * qla4xxx_mark_device_missing - blocks the session
2133 * @cls_session: Pointer to the session to be blocked
David Somayajuluafaf5a22006-09-19 10:28:00 -07002134 * @ddb_entry: Pointer to device database entry
2135 *
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302136 * This routine marks a device missing and close connection.
David Somayajuluafaf5a22006-09-19 10:28:00 -07002137 **/
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002138void qla4xxx_mark_device_missing(struct iscsi_cls_session *cls_session)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002139{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002140 iscsi_block_session(cls_session);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002141}
2142
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302143/**
2144 * qla4xxx_mark_all_devices_missing - mark all devices as missing.
2145 * @ha: Pointer to host adapter structure.
2146 *
2147 * This routine marks a device missing and resets the relogin retry count.
2148 **/
2149void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
2150{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002151 iscsi_host_for_each_session(ha->host, qla4xxx_mark_device_missing);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302152}
2153
David Somayajuluafaf5a22006-09-19 10:28:00 -07002154static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
2155 struct ddb_entry *ddb_entry,
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07002156 struct scsi_cmnd *cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002157{
2158 struct srb *srb;
2159
2160 srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
2161 if (!srb)
2162 return srb;
2163
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302164 kref_init(&srb->srb_ref);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002165 srb->ha = ha;
2166 srb->ddb = ddb_entry;
2167 srb->cmd = cmd;
2168 srb->flags = 0;
Vikas Chaudhary53698872010-04-28 11:41:59 +05302169 CMD_SP(cmd) = (void *)srb;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002170
2171 return srb;
2172}
2173
2174static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
2175{
2176 struct scsi_cmnd *cmd = srb->cmd;
2177
2178 if (srb->flags & SRB_DMA_VALID) {
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +09002179 scsi_dma_unmap(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002180 srb->flags &= ~SRB_DMA_VALID;
2181 }
Vikas Chaudhary53698872010-04-28 11:41:59 +05302182 CMD_SP(cmd) = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002183}
2184
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302185void qla4xxx_srb_compl(struct kref *ref)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002186{
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302187 struct srb *srb = container_of(ref, struct srb, srb_ref);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002188 struct scsi_cmnd *cmd = srb->cmd;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302189 struct scsi_qla_host *ha = srb->ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002190
2191 qla4xxx_srb_free_dma(ha, srb);
2192
2193 mempool_free(srb, ha->srb_mempool);
2194
2195 cmd->scsi_done(cmd);
2196}
2197
2198/**
2199 * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07002200 * @host: scsi host
David Somayajuluafaf5a22006-09-19 10:28:00 -07002201 * @cmd: Pointer to Linux's SCSI command structure
David Somayajuluafaf5a22006-09-19 10:28:00 -07002202 *
2203 * Remarks:
2204 * This routine is invoked by Linux to send a SCSI command to the driver.
2205 * The mid-level driver tries to ensure that queuecommand never gets
2206 * invoked concurrently with itself or the interrupt handler (although
2207 * the interrupt handler may call this routine as part of request-
2208 * completion handling). Unfortunely, it sometimes calls the scheduler
2209 * in interrupt context which is a big NO! NO!.
2210 **/
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07002211static int qla4xxx_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002212{
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07002213 struct scsi_qla_host *ha = to_qla_host(host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002214 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christie7fb19212008-01-31 13:36:45 -06002215 struct iscsi_cls_session *sess = ddb_entry->sess;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002216 struct srb *srb;
2217 int rval;
2218
Lalit Chandivade2232be02010-07-30 14:38:47 +05302219 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
2220 if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
2221 cmd->result = DID_NO_CONNECT << 16;
2222 else
2223 cmd->result = DID_REQUEUE << 16;
2224 goto qc_fail_command;
2225 }
2226
Mike Christie7fb19212008-01-31 13:36:45 -06002227 if (!sess) {
2228 cmd->result = DID_IMM_RETRY << 16;
2229 goto qc_fail_command;
2230 }
2231
2232 rval = iscsi_session_chkready(sess);
2233 if (rval) {
2234 cmd->result = rval;
2235 goto qc_fail_command;
2236 }
2237
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302238 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
2239 test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
2240 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
2241 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
2242 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
2243 !test_bit(AF_ONLINE, &ha->flags) ||
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002244 !test_bit(AF_LINK_UP, &ha->flags) ||
Nilesh Javali026fbd32013-01-20 23:50:58 -05002245 test_bit(AF_LOOPBACK, &ha->flags) ||
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302246 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
David C Somayajulu477ffb92007-01-22 12:26:11 -08002247 goto qc_host_busy;
2248
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07002249 srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002250 if (!srb)
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07002251 goto qc_host_busy;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002252
2253 rval = qla4xxx_send_command_to_isp(ha, srb);
2254 if (rval != QLA_SUCCESS)
2255 goto qc_host_busy_free_sp;
2256
David Somayajuluafaf5a22006-09-19 10:28:00 -07002257 return 0;
2258
2259qc_host_busy_free_sp:
2260 qla4xxx_srb_free_dma(ha, srb);
2261 mempool_free(srb, ha->srb_mempool);
2262
David Somayajuluafaf5a22006-09-19 10:28:00 -07002263qc_host_busy:
2264 return SCSI_MLQUEUE_HOST_BUSY;
2265
2266qc_fail_command:
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07002267 cmd->scsi_done(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002268
2269 return 0;
2270}
2271
2272/**
2273 * qla4xxx_mem_free - frees memory allocated to adapter
2274 * @ha: Pointer to host adapter structure.
2275 *
2276 * Frees memory previously allocated by qla4xxx_mem_alloc
2277 **/
2278static void qla4xxx_mem_free(struct scsi_qla_host *ha)
2279{
2280 if (ha->queues)
2281 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
2282 ha->queues_dma);
2283
Tej Parkash068237c82012-05-18 04:41:44 -04002284 if (ha->fw_dump)
2285 vfree(ha->fw_dump);
2286
David Somayajuluafaf5a22006-09-19 10:28:00 -07002287 ha->queues_len = 0;
2288 ha->queues = NULL;
2289 ha->queues_dma = 0;
2290 ha->request_ring = NULL;
2291 ha->request_dma = 0;
2292 ha->response_ring = NULL;
2293 ha->response_dma = 0;
2294 ha->shadow_regs = NULL;
2295 ha->shadow_regs_dma = 0;
Tej Parkash068237c82012-05-18 04:41:44 -04002296 ha->fw_dump = NULL;
2297 ha->fw_dump_size = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002298
2299 /* Free srb pool. */
2300 if (ha->srb_mempool)
2301 mempool_destroy(ha->srb_mempool);
2302
2303 ha->srb_mempool = NULL;
2304
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002305 if (ha->chap_dma_pool)
2306 dma_pool_destroy(ha->chap_dma_pool);
2307
Lalit Chandivade45494152011-10-07 16:55:42 -07002308 if (ha->chap_list)
2309 vfree(ha->chap_list);
2310 ha->chap_list = NULL;
2311
Mike Christie13483732011-12-01 21:38:41 -06002312 if (ha->fw_ddb_dma_pool)
2313 dma_pool_destroy(ha->fw_ddb_dma_pool);
2314
David Somayajuluafaf5a22006-09-19 10:28:00 -07002315 /* release io space registers */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302316 if (is_qla8022(ha)) {
2317 if (ha->nx_pcibase)
2318 iounmap(
2319 (struct device_reg_82xx __iomem *)ha->nx_pcibase);
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04002320 } else if (is_qla8032(ha)) {
2321 if (ha->nx_pcibase)
2322 iounmap(
2323 (struct device_reg_83xx __iomem *)ha->nx_pcibase);
2324 } else if (ha->reg) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002325 iounmap(ha->reg);
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04002326 }
2327
2328 if (ha->reset_tmplt.buff)
2329 vfree(ha->reset_tmplt.buff);
2330
David Somayajuluafaf5a22006-09-19 10:28:00 -07002331 pci_release_regions(ha->pdev);
2332}
2333
2334/**
2335 * qla4xxx_mem_alloc - allocates memory for use by adapter.
2336 * @ha: Pointer to host adapter structure
2337 *
2338 * Allocates DMA memory for request and response queues. Also allocates memory
2339 * for srbs.
2340 **/
2341static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
2342{
2343 unsigned long align;
2344
2345 /* Allocate contiguous block of DMA memory for queues. */
2346 ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
2347 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
2348 sizeof(struct shadow_regs) +
2349 MEM_ALIGN_VALUE +
2350 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
2351 ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
2352 &ha->queues_dma, GFP_KERNEL);
2353 if (ha->queues == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302354 ql4_printk(KERN_WARNING, ha,
2355 "Memory Allocation failed - queues.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002356
2357 goto mem_alloc_error_exit;
2358 }
2359 memset(ha->queues, 0, ha->queues_len);
2360
2361 /*
2362 * As per RISC alignment requirements -- the bus-address must be a
2363 * multiple of the request-ring size (in bytes).
2364 */
2365 align = 0;
2366 if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
2367 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
2368 (MEM_ALIGN_VALUE - 1));
2369
2370 /* Update request and response queue pointers. */
2371 ha->request_dma = ha->queues_dma + align;
2372 ha->request_ring = (struct queue_entry *) (ha->queues + align);
2373 ha->response_dma = ha->queues_dma + align +
2374 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
2375 ha->response_ring = (struct queue_entry *) (ha->queues + align +
2376 (REQUEST_QUEUE_DEPTH *
2377 QUEUE_SIZE));
2378 ha->shadow_regs_dma = ha->queues_dma + align +
2379 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
2380 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
2381 ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
2382 (REQUEST_QUEUE_DEPTH *
2383 QUEUE_SIZE) +
2384 (RESPONSE_QUEUE_DEPTH *
2385 QUEUE_SIZE));
2386
2387 /* Allocate memory for srb pool. */
2388 ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
2389 mempool_free_slab, srb_cachep);
2390 if (ha->srb_mempool == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302391 ql4_printk(KERN_WARNING, ha,
2392 "Memory Allocation failed - SRB Pool.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002393
2394 goto mem_alloc_error_exit;
2395 }
2396
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002397 ha->chap_dma_pool = dma_pool_create("ql4_chap", &ha->pdev->dev,
2398 CHAP_DMA_BLOCK_SIZE, 8, 0);
2399
2400 if (ha->chap_dma_pool == NULL) {
2401 ql4_printk(KERN_WARNING, ha,
2402 "%s: chap_dma_pool allocation failed..\n", __func__);
2403 goto mem_alloc_error_exit;
2404 }
2405
Mike Christie13483732011-12-01 21:38:41 -06002406 ha->fw_ddb_dma_pool = dma_pool_create("ql4_fw_ddb", &ha->pdev->dev,
2407 DDB_DMA_BLOCK_SIZE, 8, 0);
2408
2409 if (ha->fw_ddb_dma_pool == NULL) {
2410 ql4_printk(KERN_WARNING, ha,
2411 "%s: fw_ddb_dma_pool allocation failed..\n",
2412 __func__);
2413 goto mem_alloc_error_exit;
2414 }
2415
David Somayajuluafaf5a22006-09-19 10:28:00 -07002416 return QLA_SUCCESS;
2417
2418mem_alloc_error_exit:
2419 qla4xxx_mem_free(ha);
2420 return QLA_ERROR;
2421}
2422
2423/**
Mike Hernandez4f770832012-01-11 02:44:15 -08002424 * qla4_8xxx_check_temp - Check the ISP82XX temperature.
2425 * @ha: adapter block pointer.
2426 *
2427 * Note: The caller should not hold the idc lock.
2428 **/
2429static int qla4_8xxx_check_temp(struct scsi_qla_host *ha)
2430{
2431 uint32_t temp, temp_state, temp_val;
2432 int status = QLA_SUCCESS;
2433
Vikas Chaudhary33693c72012-08-22 07:55:04 -04002434 temp = qla4_8xxx_rd_direct(ha, QLA8XXX_CRB_TEMP_STATE);
Mike Hernandez4f770832012-01-11 02:44:15 -08002435
2436 temp_state = qla82xx_get_temp_state(temp);
2437 temp_val = qla82xx_get_temp_val(temp);
2438
2439 if (temp_state == QLA82XX_TEMP_PANIC) {
2440 ql4_printk(KERN_WARNING, ha, "Device temperature %d degrees C"
2441 " exceeds maximum allowed. Hardware has been shut"
2442 " down.\n", temp_val);
2443 status = QLA_ERROR;
2444 } else if (temp_state == QLA82XX_TEMP_WARN) {
2445 if (ha->temperature == QLA82XX_TEMP_NORMAL)
2446 ql4_printk(KERN_WARNING, ha, "Device temperature %d"
2447 " degrees C exceeds operating range."
2448 " Immediate action needed.\n", temp_val);
2449 } else {
2450 if (ha->temperature == QLA82XX_TEMP_WARN)
2451 ql4_printk(KERN_INFO, ha, "Device temperature is"
2452 " now %d degrees C in normal range.\n",
2453 temp_val);
2454 }
2455 ha->temperature = temp_state;
2456 return status;
2457}
2458
2459/**
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302460 * qla4_8xxx_check_fw_alive - Check firmware health
2461 * @ha: Pointer to host adapter structure.
2462 *
2463 * Context: Interrupt
2464 **/
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002465static int qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302466{
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002467 uint32_t fw_heartbeat_counter;
2468 int status = QLA_SUCCESS;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302469
Vikas Chaudhary33693c72012-08-22 07:55:04 -04002470 fw_heartbeat_counter = qla4_8xxx_rd_direct(ha,
2471 QLA8XXX_PEG_ALIVE_COUNTER);
Lalit Chandivade2232be02010-07-30 14:38:47 +05302472 /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
2473 if (fw_heartbeat_counter == 0xffffffff) {
2474 DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
2475 "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
2476 ha->host_no, __func__));
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002477 return status;
Lalit Chandivade2232be02010-07-30 14:38:47 +05302478 }
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302479
2480 if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
2481 ha->seconds_since_last_heartbeat++;
2482 /* FW not alive after 2 seconds */
2483 if (ha->seconds_since_last_heartbeat == 2) {
2484 ha->seconds_since_last_heartbeat = 0;
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04002485 qla4_8xxx_dump_peg_reg(ha);
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002486 status = QLA_ERROR;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302487 }
Lalit Chandivade99457d72010-10-06 22:49:32 -07002488 } else
2489 ha->seconds_since_last_heartbeat = 0;
2490
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302491 ha->fw_heartbeat_counter = fw_heartbeat_counter;
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002492 return status;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302493}
2494
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04002495static void qla4_8xxx_process_fw_error(struct scsi_qla_host *ha)
2496{
2497 uint32_t halt_status;
2498 int halt_status_unrecoverable = 0;
2499
2500 halt_status = qla4_8xxx_rd_direct(ha, QLA8XXX_PEG_HALT_STATUS1);
2501
2502 if (is_qla8022(ha)) {
2503 ql4_printk(KERN_INFO, ha, "%s: disabling pause transmit on port 0 & 1.\n",
2504 __func__);
2505 qla4_82xx_wr_32(ha, QLA82XX_CRB_NIU + 0x98,
2506 CRB_NIU_XG_PAUSE_CTL_P0 |
2507 CRB_NIU_XG_PAUSE_CTL_P1);
2508
2509 if (QLA82XX_FWERROR_CODE(halt_status) == 0x67)
2510 ql4_printk(KERN_ERR, ha, "%s: Firmware aborted with error code 0x00006700. Device is being reset\n",
2511 __func__);
2512 if (halt_status & HALT_STATUS_UNRECOVERABLE)
2513 halt_status_unrecoverable = 1;
2514 } else if (is_qla8032(ha)) {
2515 if (halt_status & QLA83XX_HALT_STATUS_FW_RESET)
2516 ql4_printk(KERN_ERR, ha, "%s: Firmware error detected device is being reset\n",
2517 __func__);
2518 else if (halt_status & QLA83XX_HALT_STATUS_UNRECOVERABLE)
2519 halt_status_unrecoverable = 1;
2520 }
2521
2522 /*
2523 * Since we cannot change dev_state in interrupt context,
2524 * set appropriate DPC flag then wakeup DPC
2525 */
2526 if (halt_status_unrecoverable) {
2527 set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
2528 } else {
2529 ql4_printk(KERN_INFO, ha, "%s: detect abort needed!\n",
2530 __func__);
2531 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2532 }
2533 qla4xxx_mailbox_premature_completion(ha);
2534 qla4xxx_wake_dpc(ha);
2535}
2536
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302537/**
2538 * qla4_8xxx_watchdog - Poll dev state
2539 * @ha: Pointer to host adapter structure.
2540 *
2541 * Context: Interrupt
2542 **/
2543void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
2544{
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04002545 uint32_t dev_state;
Vikas Chaudhary6cf94122013-03-07 05:43:10 -05002546 uint32_t idc_ctrl;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302547
2548 /* don't poll if reset is going on */
Lalit Chandivaded56a1f72010-12-02 22:12:45 -08002549 if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
2550 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
Vikas Chaudhary977f46a2011-05-17 23:17:08 -07002551 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags))) {
Vikas Chaudhary33693c72012-08-22 07:55:04 -04002552 dev_state = qla4_8xxx_rd_direct(ha, QLA8XXX_CRB_DEV_STATE);
Mike Hernandez4f770832012-01-11 02:44:15 -08002553
2554 if (qla4_8xxx_check_temp(ha)) {
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04002555 if (is_qla8022(ha)) {
2556 ql4_printk(KERN_INFO, ha, "disabling pause transmit on port 0 & 1.\n");
2557 qla4_82xx_wr_32(ha, QLA82XX_CRB_NIU + 0x98,
2558 CRB_NIU_XG_PAUSE_CTL_P0 |
2559 CRB_NIU_XG_PAUSE_CTL_P1);
2560 }
Mike Hernandez4f770832012-01-11 02:44:15 -08002561 set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
2562 qla4xxx_wake_dpc(ha);
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04002563 } else if (dev_state == QLA8XXX_DEV_NEED_RESET &&
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04002564 !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
Vikas Chaudhary6cf94122013-03-07 05:43:10 -05002565
2566 ql4_printk(KERN_INFO, ha, "%s: HW State: NEED RESET!\n",
2567 __func__);
2568
2569 if (is_qla8032(ha)) {
2570 idc_ctrl = qla4_83xx_rd_reg(ha,
2571 QLA83XX_IDC_DRV_CTRL);
2572 if (!(idc_ctrl & GRACEFUL_RESET_BIT1)) {
2573 ql4_printk(KERN_INFO, ha, "%s: Graceful reset bit is not set\n",
2574 __func__);
2575 qla4xxx_mailbox_premature_completion(
2576 ha);
2577 }
2578 }
2579
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04002580 if (is_qla8032(ha) ||
2581 (is_qla8022(ha) && !ql4xdontresethba)) {
Vikas Chaudhary3930b8c2010-12-02 22:12:47 -08002582 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2583 qla4xxx_wake_dpc(ha);
Vikas Chaudhary3930b8c2010-12-02 22:12:47 -08002584 }
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04002585 } else if (dev_state == QLA8XXX_DEV_NEED_QUIESCENT &&
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302586 !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
Vikas Chaudhary3930b8c2010-12-02 22:12:47 -08002587 ql4_printk(KERN_INFO, ha, "%s: HW State: NEED QUIES!\n",
2588 __func__);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302589 set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
2590 qla4xxx_wake_dpc(ha);
2591 } else {
2592 /* Check firmware health */
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04002593 if (qla4_8xxx_check_fw_alive(ha))
2594 qla4_8xxx_process_fw_error(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302595 }
2596 }
2597}
2598
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08002599static void qla4xxx_check_relogin_flash_ddb(struct iscsi_cls_session *cls_sess)
Mike Christie13483732011-12-01 21:38:41 -06002600{
2601 struct iscsi_session *sess;
2602 struct ddb_entry *ddb_entry;
2603 struct scsi_qla_host *ha;
2604
2605 sess = cls_sess->dd_data;
2606 ddb_entry = sess->dd_data;
2607 ha = ddb_entry->ha;
2608
2609 if (!(ddb_entry->ddb_type == FLASH_DDB))
2610 return;
2611
2612 if (adapter_up(ha) && !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
2613 !iscsi_is_session_online(cls_sess)) {
2614 if (atomic_read(&ddb_entry->retry_relogin_timer) !=
2615 INVALID_ENTRY) {
2616 if (atomic_read(&ddb_entry->retry_relogin_timer) ==
2617 0) {
2618 atomic_set(&ddb_entry->retry_relogin_timer,
2619 INVALID_ENTRY);
2620 set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
2621 set_bit(DF_RELOGIN, &ddb_entry->flags);
2622 DEBUG2(ql4_printk(KERN_INFO, ha,
2623 "%s: index [%d] login device\n",
2624 __func__, ddb_entry->fw_ddb_index));
2625 } else
2626 atomic_dec(&ddb_entry->retry_relogin_timer);
2627 }
2628 }
2629
2630 /* Wait for relogin to timeout */
2631 if (atomic_read(&ddb_entry->relogin_timer) &&
2632 (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
2633 /*
2634 * If the relogin times out and the device is
2635 * still NOT ONLINE then try and relogin again.
2636 */
2637 if (!iscsi_is_session_online(cls_sess)) {
2638 /* Reset retry relogin timer */
2639 atomic_inc(&ddb_entry->relogin_retry_count);
2640 DEBUG2(ql4_printk(KERN_INFO, ha,
2641 "%s: index[%d] relogin timed out-retrying"
2642 " relogin (%d), retry (%d)\n", __func__,
2643 ddb_entry->fw_ddb_index,
2644 atomic_read(&ddb_entry->relogin_retry_count),
2645 ddb_entry->default_time2wait + 4));
2646 set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
2647 atomic_set(&ddb_entry->retry_relogin_timer,
2648 ddb_entry->default_time2wait + 4);
2649 }
2650 }
2651}
2652
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302653/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002654 * qla4xxx_timer - checks every second for work to do.
2655 * @ha: Pointer to host adapter structure.
2656 **/
2657static void qla4xxx_timer(struct scsi_qla_host *ha)
2658{
David Somayajuluafaf5a22006-09-19 10:28:00 -07002659 int start_dpc = 0;
Lalit Chandivade2232be02010-07-30 14:38:47 +05302660 uint16_t w;
2661
Mike Christie13483732011-12-01 21:38:41 -06002662 iscsi_host_for_each_session(ha->host, qla4xxx_check_relogin_flash_ddb);
2663
Lalit Chandivade2232be02010-07-30 14:38:47 +05302664 /* If we are in the middle of AER/EEH processing
2665 * skip any processing and reschedule the timer
2666 */
2667 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
2668 mod_timer(&ha->timer, jiffies + HZ);
2669 return;
2670 }
2671
2672 /* Hardware read to trigger an EEH error during mailbox waits. */
2673 if (!pci_channel_offline(ha->pdev))
2674 pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002675
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04002676 if (is_qla80XX(ha))
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302677 qla4_8xxx_watchdog(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302678
Vikas Chaudharyee996a62012-08-22 07:55:05 -04002679 if (is_qla40XX(ha)) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302680 /* Check for heartbeat interval. */
2681 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
2682 ha->heartbeat_interval != 0) {
2683 ha->seconds_since_last_heartbeat++;
2684 if (ha->seconds_since_last_heartbeat >
2685 ha->heartbeat_interval + 2)
2686 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2687 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07002688 }
2689
Vikas Chaudharyff884432011-08-29 23:43:02 +05302690 /* Process any deferred work. */
2691 if (!list_empty(&ha->work_list))
2692 start_dpc++;
2693
David Somayajuluafaf5a22006-09-19 10:28:00 -07002694 /* Wakeup the dpc routine for this adapter, if needed. */
Lalit Chandivade1b468072011-05-17 23:17:09 -07002695 if (start_dpc ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07002696 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
2697 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
2698 test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302699 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07002700 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
2701 test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05302702 test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302703 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
2704 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
Lalit Chandivade1b468072011-05-17 23:17:09 -07002705 test_bit(DPC_AEN, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002706 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
2707 " - dpc flags = 0x%lx\n",
2708 ha->host_no, __func__, ha->dpc_flags));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302709 qla4xxx_wake_dpc(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002710 }
2711
2712 /* Reschedule timer thread to call us back in one second */
2713 mod_timer(&ha->timer, jiffies + HZ);
2714
2715 DEBUG2(ha->seconds_since_last_intr++);
2716}
2717
2718/**
2719 * qla4xxx_cmd_wait - waits for all outstanding commands to complete
2720 * @ha: Pointer to host adapter structure.
2721 *
2722 * This routine stalls the driver until all outstanding commands are returned.
2723 * Caller must release the Hardware Lock prior to calling this routine.
2724 **/
2725static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
2726{
2727 uint32_t index = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002728 unsigned long flags;
2729 struct scsi_cmnd *cmd;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002730
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302731 unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
2732
2733 DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
2734 "complete\n", WAIT_CMD_TOV));
2735
2736 while (!time_after_eq(jiffies, wtime)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002737 spin_lock_irqsave(&ha->hardware_lock, flags);
2738 /* Find a command that hasn't completed. */
2739 for (index = 0; index < ha->host->can_queue; index++) {
2740 cmd = scsi_host_find_tag(ha->host, index);
Mike Christiea1e00632010-10-26 05:45:30 -07002741 /*
2742 * We cannot just check if the index is valid,
2743 * becase if we are run from the scsi eh, then
2744 * the scsi/block layer is going to prevent
2745 * the tag from being released.
2746 */
2747 if (cmd != NULL && CMD_SP(cmd))
David Somayajuluafaf5a22006-09-19 10:28:00 -07002748 break;
2749 }
2750 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2751
2752 /* If No Commands are pending, wait is complete */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302753 if (index == ha->host->can_queue)
2754 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002755
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302756 msleep(1000);
2757 }
2758 /* If we timed out on waiting for commands to come back
2759 * return ERROR. */
2760 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002761}
2762
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302763int qla4xxx_hw_reset(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002764{
David Somayajuluafaf5a22006-09-19 10:28:00 -07002765 uint32_t ctrl_status;
David C Somayajulu477ffb92007-01-22 12:26:11 -08002766 unsigned long flags = 0;
2767
2768 DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -07002769
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302770 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
2771 return QLA_ERROR;
2772
David Somayajuluafaf5a22006-09-19 10:28:00 -07002773 spin_lock_irqsave(&ha->hardware_lock, flags);
2774
2775 /*
2776 * If the SCSI Reset Interrupt bit is set, clear it.
2777 * Otherwise, the Soft Reset won't work.
2778 */
2779 ctrl_status = readw(&ha->reg->ctrl_status);
2780 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
2781 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
2782
2783 /* Issue Soft Reset */
2784 writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
2785 readl(&ha->reg->ctrl_status);
2786
2787 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302788 return QLA_SUCCESS;
David C Somayajulu477ffb92007-01-22 12:26:11 -08002789}
2790
2791/**
2792 * qla4xxx_soft_reset - performs soft reset.
2793 * @ha: Pointer to host adapter structure.
2794 **/
2795int qla4xxx_soft_reset(struct scsi_qla_host *ha)
2796{
2797 uint32_t max_wait_time;
2798 unsigned long flags = 0;
Vikas Chaudharyf931c532010-10-06 22:48:07 -07002799 int status;
David C Somayajulu477ffb92007-01-22 12:26:11 -08002800 uint32_t ctrl_status;
2801
Vikas Chaudharyf931c532010-10-06 22:48:07 -07002802 status = qla4xxx_hw_reset(ha);
2803 if (status != QLA_SUCCESS)
2804 return status;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002805
Vikas Chaudharyf931c532010-10-06 22:48:07 -07002806 status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002807 /* Wait until the Network Reset Intr bit is cleared */
2808 max_wait_time = RESET_INTR_TOV;
2809 do {
2810 spin_lock_irqsave(&ha->hardware_lock, flags);
2811 ctrl_status = readw(&ha->reg->ctrl_status);
2812 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2813
2814 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
2815 break;
2816
2817 msleep(1000);
2818 } while ((--max_wait_time));
2819
2820 if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
2821 DEBUG2(printk(KERN_WARNING
2822 "scsi%ld: Network Reset Intr not cleared by "
2823 "Network function, clearing it now!\n",
2824 ha->host_no));
2825 spin_lock_irqsave(&ha->hardware_lock, flags);
2826 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
2827 readl(&ha->reg->ctrl_status);
2828 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2829 }
2830
2831 /* Wait until the firmware tells us the Soft Reset is done */
2832 max_wait_time = SOFT_RESET_TOV;
2833 do {
2834 spin_lock_irqsave(&ha->hardware_lock, flags);
2835 ctrl_status = readw(&ha->reg->ctrl_status);
2836 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2837
2838 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
2839 status = QLA_SUCCESS;
2840 break;
2841 }
2842
2843 msleep(1000);
2844 } while ((--max_wait_time));
2845
2846 /*
2847 * Also, make sure that the SCSI Reset Interrupt bit has been cleared
2848 * after the soft reset has taken place.
2849 */
2850 spin_lock_irqsave(&ha->hardware_lock, flags);
2851 ctrl_status = readw(&ha->reg->ctrl_status);
2852 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
2853 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
2854 readl(&ha->reg->ctrl_status);
2855 }
2856 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2857
2858 /* If soft reset fails then most probably the bios on other
2859 * function is also enabled.
2860 * Since the initialization is sequential the other fn
2861 * wont be able to acknowledge the soft reset.
2862 * Issue a force soft reset to workaround this scenario.
2863 */
2864 if (max_wait_time == 0) {
2865 /* Issue Force Soft Reset */
2866 spin_lock_irqsave(&ha->hardware_lock, flags);
2867 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
2868 readl(&ha->reg->ctrl_status);
2869 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2870 /* Wait until the firmware tells us the Soft Reset is done */
2871 max_wait_time = SOFT_RESET_TOV;
2872 do {
2873 spin_lock_irqsave(&ha->hardware_lock, flags);
2874 ctrl_status = readw(&ha->reg->ctrl_status);
2875 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2876
2877 if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
2878 status = QLA_SUCCESS;
2879 break;
2880 }
2881
2882 msleep(1000);
2883 } while ((--max_wait_time));
2884 }
2885
2886 return status;
2887}
2888
2889/**
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302890 * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
David Somayajuluafaf5a22006-09-19 10:28:00 -07002891 * @ha: Pointer to host adapter structure.
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302892 * @res: returned scsi status
David Somayajuluafaf5a22006-09-19 10:28:00 -07002893 *
2894 * This routine is called just prior to a HARD RESET to return all
2895 * outstanding commands back to the Operating System.
2896 * Caller should make sure that the following locks are released
2897 * before this calling routine: Hardware lock, and io_request_lock.
2898 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302899static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002900{
2901 struct srb *srb;
2902 int i;
2903 unsigned long flags;
2904
2905 spin_lock_irqsave(&ha->hardware_lock, flags);
2906 for (i = 0; i < ha->host->can_queue; i++) {
2907 srb = qla4xxx_del_from_active_array(ha, i);
2908 if (srb != NULL) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302909 srb->cmd->result = res;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302910 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002911 }
2912 }
2913 spin_unlock_irqrestore(&ha->hardware_lock, flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002914}
2915
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302916void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
2917{
2918 clear_bit(AF_ONLINE, &ha->flags);
2919
2920 /* Disable the board */
2921 ql4_printk(KERN_INFO, ha, "Disabling the board\n");
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302922
2923 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
2924 qla4xxx_mark_all_devices_missing(ha);
2925 clear_bit(AF_INIT_DONE, &ha->flags);
2926}
2927
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002928static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session)
2929{
2930 struct iscsi_session *sess;
2931 struct ddb_entry *ddb_entry;
2932
2933 sess = cls_session->dd_data;
2934 ddb_entry = sess->dd_data;
2935 ddb_entry->fw_ddb_device_state = DDB_DS_SESSION_FAILED;
Mike Christie13483732011-12-01 21:38:41 -06002936
2937 if (ddb_entry->ddb_type == FLASH_DDB)
2938 iscsi_block_session(ddb_entry->sess);
2939 else
2940 iscsi_session_failure(cls_session->dd_data,
2941 ISCSI_ERR_CONN_FAILED);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002942}
2943
David Somayajuluafaf5a22006-09-19 10:28:00 -07002944/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07002945 * qla4xxx_recover_adapter - recovers adapter after a fatal error
2946 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07002947 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302948static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002949{
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302950 int status = QLA_ERROR;
2951 uint8_t reset_chip = 0;
Sarang Radke8e0f3a62011-12-01 22:42:09 -08002952 uint32_t dev_state;
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002953 unsigned long wait;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002954
2955 /* Stall incoming I/O until we are done */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302956 scsi_block_requests(ha->host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002957 clear_bit(AF_ONLINE, &ha->flags);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002958 clear_bit(AF_LINK_UP, &ha->flags);
Mike Christie50a29ae2008-03-04 13:26:53 -06002959
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302960 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -07002961
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302962 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002963
Tej Parkash546fef22012-09-20 07:35:12 -04002964 if (is_qla8032(ha) &&
2965 !test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
2966 ql4_printk(KERN_INFO, ha, "%s: disabling pause transmit on port 0 & 1.\n",
2967 __func__);
2968 /* disable pause frame for ISP83xx */
2969 qla4_83xx_disable_pause(ha);
2970 }
2971
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002972 iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
2973
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302974 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
2975 reset_chip = 1;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002976
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302977 /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
2978 * do not reset adapter, jump to initialize_adapter */
2979 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
2980 status = QLA_SUCCESS;
2981 goto recover_ha_init_adapter;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002982 }
2983
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04002984 /* For the ISP-8xxx adapter, issue a stop_firmware if invoked
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302985 * from eh_host_reset or ioctl module */
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04002986 if (is_qla80XX(ha) && !reset_chip &&
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302987 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
2988
2989 DEBUG2(ql4_printk(KERN_INFO, ha,
2990 "scsi%ld: %s - Performing stop_firmware...\n",
2991 ha->host_no, __func__));
2992 status = ha->isp_ops->reset_firmware(ha);
2993 if (status == QLA_SUCCESS) {
Nilesh Javali2bd1e2be2010-10-06 22:49:20 -07002994 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
2995 qla4xxx_cmd_wait(ha);
Vikas Chaudhary5c19b922012-11-23 06:58:38 -05002996
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302997 ha->isp_ops->disable_intrs(ha);
2998 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2999 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
3000 } else {
3001 /* If the stop_firmware fails then
3002 * reset the entire chip */
3003 reset_chip = 1;
3004 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
3005 set_bit(DPC_RESET_HA, &ha->dpc_flags);
3006 }
3007 }
3008
3009 /* Issue full chip reset if recovering from a catastrophic error,
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04003010 * or if stop_firmware fails for ISP-8xxx.
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303011 * This is the default case for ISP-4xxx */
Vikas Chaudharyee996a62012-08-22 07:55:05 -04003012 if (is_qla40XX(ha) || reset_chip) {
3013 if (is_qla40XX(ha))
Shyam Sunder9ee91a32011-12-01 22:42:13 -08003014 goto chip_reset;
3015
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04003016 /* Check if 8XXX firmware is alive or not
Shyam Sunder9ee91a32011-12-01 22:42:13 -08003017 * We may have arrived here from NEED_RESET
3018 * detection only */
3019 if (test_bit(AF_FW_RECOVERY, &ha->flags))
3020 goto chip_reset;
3021
3022 wait = jiffies + (FW_ALIVE_WAIT_TOV * HZ);
3023 while (time_before(jiffies, wait)) {
3024 if (qla4_8xxx_check_fw_alive(ha)) {
3025 qla4xxx_mailbox_premature_completion(ha);
3026 break;
3027 }
3028
3029 set_current_state(TASK_UNINTERRUPTIBLE);
3030 schedule_timeout(HZ);
3031 }
Vikas Chaudharyda106212012-08-22 07:45:26 -04003032chip_reset:
Nilesh Javali2bd1e2be2010-10-06 22:49:20 -07003033 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
3034 qla4xxx_cmd_wait(ha);
Vikas Chaudharyda106212012-08-22 07:45:26 -04003035
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303036 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
3037 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
3038 DEBUG2(ql4_printk(KERN_INFO, ha,
3039 "scsi%ld: %s - Performing chip reset..\n",
3040 ha->host_no, __func__));
3041 status = ha->isp_ops->reset_chip(ha);
3042 }
3043
3044 /* Flush any pending ddb changed AENs */
3045 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
3046
3047recover_ha_init_adapter:
3048 /* Upon successful firmware/chip reset, re-initialize the adapter */
3049 if (status == QLA_SUCCESS) {
3050 /* For ISP-4xxx, force function 1 to always initialize
3051 * before function 3 to prevent both funcions from
3052 * stepping on top of the other */
Vikas Chaudharyee996a62012-08-22 07:55:05 -04003053 if (is_qla40XX(ha) && (ha->mac_index == 3))
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303054 ssleep(6);
3055
3056 /* NOTE: AF_ONLINE flag set upon successful completion of
3057 * qla4xxx_initialize_adapter */
Mike Christie13483732011-12-01 21:38:41 -06003058 status = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303059 }
3060
3061 /* Retry failed adapter initialization, if necessary
3062 * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
3063 * case to prevent ping-pong resets between functions */
3064 if (!test_bit(AF_ONLINE, &ha->flags) &&
3065 !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07003066 /* Adapter initialization failed, see if we can retry
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303067 * resetting the ha.
3068 * Since we don't want to block the DPC for too long
3069 * with multiple resets in the same thread,
3070 * utilize DPC to retry */
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04003071 if (is_qla80XX(ha)) {
Vikas Chaudhary33693c72012-08-22 07:55:04 -04003072 ha->isp_ops->idc_lock(ha);
3073 dev_state = qla4_8xxx_rd_direct(ha,
3074 QLA8XXX_CRB_DEV_STATE);
3075 ha->isp_ops->idc_unlock(ha);
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04003076 if (dev_state == QLA8XXX_DEV_FAILED) {
Sarang Radke8e0f3a62011-12-01 22:42:09 -08003077 ql4_printk(KERN_INFO, ha, "%s: don't retry "
3078 "recover adapter. H/W is in Failed "
3079 "state\n", __func__);
3080 qla4xxx_dead_adapter_cleanup(ha);
3081 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
3082 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
3083 clear_bit(DPC_RESET_HA_FW_CONTEXT,
3084 &ha->dpc_flags);
3085 status = QLA_ERROR;
3086
3087 goto exit_recover;
3088 }
3089 }
3090
David Somayajuluafaf5a22006-09-19 10:28:00 -07003091 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
3092 ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
3093 DEBUG2(printk("scsi%ld: recover adapter - retrying "
3094 "(%d) more times\n", ha->host_no,
3095 ha->retry_reset_ha_cnt));
3096 set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
3097 status = QLA_ERROR;
3098 } else {
3099 if (ha->retry_reset_ha_cnt > 0) {
3100 /* Schedule another Reset HA--DPC will retry */
3101 ha->retry_reset_ha_cnt--;
3102 DEBUG2(printk("scsi%ld: recover adapter - "
3103 "retry remaining %d\n",
3104 ha->host_no,
3105 ha->retry_reset_ha_cnt));
3106 status = QLA_ERROR;
3107 }
3108
3109 if (ha->retry_reset_ha_cnt == 0) {
3110 /* Recover adapter retries have been exhausted.
3111 * Adapter DEAD */
3112 DEBUG2(printk("scsi%ld: recover adapter "
3113 "failed - board disabled\n",
3114 ha->host_no));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303115 qla4xxx_dead_adapter_cleanup(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003116 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
3117 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303118 clear_bit(DPC_RESET_HA_FW_CONTEXT,
David Somayajuluafaf5a22006-09-19 10:28:00 -07003119 &ha->dpc_flags);
3120 status = QLA_ERROR;
3121 }
3122 }
3123 } else {
3124 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303125 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003126 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
3127 }
3128
Sarang Radke8e0f3a62011-12-01 22:42:09 -08003129exit_recover:
David Somayajuluafaf5a22006-09-19 10:28:00 -07003130 ha->adapter_error_count++;
3131
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303132 if (test_bit(AF_ONLINE, &ha->flags))
3133 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003134
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303135 scsi_unblock_requests(ha->host);
3136
3137 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
3138 DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003139 status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303140
David Somayajuluafaf5a22006-09-19 10:28:00 -07003141 return status;
3142}
3143
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003144static void qla4xxx_relogin_devices(struct iscsi_cls_session *cls_session)
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07003145{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003146 struct iscsi_session *sess;
3147 struct ddb_entry *ddb_entry;
3148 struct scsi_qla_host *ha;
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07003149
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003150 sess = cls_session->dd_data;
3151 ddb_entry = sess->dd_data;
3152 ha = ddb_entry->ha;
3153 if (!iscsi_is_session_online(cls_session)) {
3154 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
3155 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
3156 " unblock session\n", ha->host_no, __func__,
3157 ddb_entry->fw_ddb_index);
3158 iscsi_unblock_session(ddb_entry->sess);
3159 } else {
3160 /* Trigger relogin */
Mike Christie13483732011-12-01 21:38:41 -06003161 if (ddb_entry->ddb_type == FLASH_DDB) {
3162 if (!test_bit(DF_RELOGIN, &ddb_entry->flags))
3163 qla4xxx_arm_relogin_timer(ddb_entry);
3164 } else
3165 iscsi_session_failure(cls_session->dd_data,
3166 ISCSI_ERR_CONN_FAILED);
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07003167 }
3168 }
3169}
3170
Mike Christie13483732011-12-01 21:38:41 -06003171int qla4xxx_unblock_flash_ddb(struct iscsi_cls_session *cls_session)
3172{
3173 struct iscsi_session *sess;
3174 struct ddb_entry *ddb_entry;
3175 struct scsi_qla_host *ha;
3176
3177 sess = cls_session->dd_data;
3178 ddb_entry = sess->dd_data;
3179 ha = ddb_entry->ha;
3180 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
3181 " unblock session\n", ha->host_no, __func__,
3182 ddb_entry->fw_ddb_index);
3183
3184 iscsi_unblock_session(ddb_entry->sess);
3185
3186 /* Start scan target */
3187 if (test_bit(AF_ONLINE, &ha->flags)) {
3188 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
3189 " start scan\n", ha->host_no, __func__,
3190 ddb_entry->fw_ddb_index);
3191 scsi_queue_work(ha->host, &ddb_entry->sess->scan_work);
3192 }
3193 return QLA_SUCCESS;
3194}
3195
3196int qla4xxx_unblock_ddb(struct iscsi_cls_session *cls_session)
3197{
3198 struct iscsi_session *sess;
3199 struct ddb_entry *ddb_entry;
3200 struct scsi_qla_host *ha;
Manish Rangankar80c53e62012-08-07 07:57:15 -04003201 int status = QLA_SUCCESS;
Mike Christie13483732011-12-01 21:38:41 -06003202
3203 sess = cls_session->dd_data;
3204 ddb_entry = sess->dd_data;
3205 ha = ddb_entry->ha;
3206 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
3207 " unblock user space session\n", ha->host_no, __func__,
3208 ddb_entry->fw_ddb_index);
Mike Christie13483732011-12-01 21:38:41 -06003209
Manish Rangankar80c53e62012-08-07 07:57:15 -04003210 if (!iscsi_is_session_online(cls_session)) {
3211 iscsi_conn_start(ddb_entry->conn);
3212 iscsi_conn_login_event(ddb_entry->conn,
3213 ISCSI_CONN_STATE_LOGGED_IN);
3214 } else {
3215 ql4_printk(KERN_INFO, ha,
3216 "scsi%ld: %s: ddb[%d] session [%d] already logged in\n",
3217 ha->host_no, __func__, ddb_entry->fw_ddb_index,
3218 cls_session->sid);
3219 status = QLA_ERROR;
3220 }
3221
3222 return status;
Mike Christie13483732011-12-01 21:38:41 -06003223}
3224
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003225static void qla4xxx_relogin_all_devices(struct scsi_qla_host *ha)
3226{
3227 iscsi_host_for_each_session(ha->host, qla4xxx_relogin_devices);
3228}
3229
Mike Christie13483732011-12-01 21:38:41 -06003230static void qla4xxx_relogin_flash_ddb(struct iscsi_cls_session *cls_sess)
3231{
3232 uint16_t relogin_timer;
3233 struct iscsi_session *sess;
3234 struct ddb_entry *ddb_entry;
3235 struct scsi_qla_host *ha;
3236
3237 sess = cls_sess->dd_data;
3238 ddb_entry = sess->dd_data;
3239 ha = ddb_entry->ha;
3240
3241 relogin_timer = max(ddb_entry->default_relogin_timeout,
3242 (uint16_t)RELOGIN_TOV);
3243 atomic_set(&ddb_entry->relogin_timer, relogin_timer);
3244
3245 DEBUG2(ql4_printk(KERN_INFO, ha,
3246 "scsi%ld: Relogin index [%d]. TOV=%d\n", ha->host_no,
3247 ddb_entry->fw_ddb_index, relogin_timer));
3248
3249 qla4xxx_login_flash_ddb(cls_sess);
3250}
3251
3252static void qla4xxx_dpc_relogin(struct iscsi_cls_session *cls_sess)
3253{
3254 struct iscsi_session *sess;
3255 struct ddb_entry *ddb_entry;
3256 struct scsi_qla_host *ha;
3257
3258 sess = cls_sess->dd_data;
3259 ddb_entry = sess->dd_data;
3260 ha = ddb_entry->ha;
3261
3262 if (!(ddb_entry->ddb_type == FLASH_DDB))
3263 return;
3264
3265 if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
3266 !iscsi_is_session_online(cls_sess)) {
3267 DEBUG2(ql4_printk(KERN_INFO, ha,
3268 "relogin issued\n"));
3269 qla4xxx_relogin_flash_ddb(cls_sess);
3270 }
3271}
3272
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303273void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
3274{
Lalit Chandivade1b468072011-05-17 23:17:09 -07003275 if (ha->dpc_thread)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303276 queue_work(ha->dpc_thread, &ha->dpc_work);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303277}
3278
Vikas Chaudharyff884432011-08-29 23:43:02 +05303279static struct qla4_work_evt *
3280qla4xxx_alloc_work(struct scsi_qla_host *ha, uint32_t data_size,
3281 enum qla4_work_type type)
3282{
3283 struct qla4_work_evt *e;
3284 uint32_t size = sizeof(struct qla4_work_evt) + data_size;
3285
3286 e = kzalloc(size, GFP_ATOMIC);
3287 if (!e)
3288 return NULL;
3289
3290 INIT_LIST_HEAD(&e->list);
3291 e->type = type;
3292 return e;
3293}
3294
3295static void qla4xxx_post_work(struct scsi_qla_host *ha,
3296 struct qla4_work_evt *e)
3297{
3298 unsigned long flags;
3299
3300 spin_lock_irqsave(&ha->work_lock, flags);
3301 list_add_tail(&e->list, &ha->work_list);
3302 spin_unlock_irqrestore(&ha->work_lock, flags);
3303 qla4xxx_wake_dpc(ha);
3304}
3305
3306int qla4xxx_post_aen_work(struct scsi_qla_host *ha,
3307 enum iscsi_host_event_code aen_code,
3308 uint32_t data_size, uint8_t *data)
3309{
3310 struct qla4_work_evt *e;
3311
3312 e = qla4xxx_alloc_work(ha, data_size, QLA4_EVENT_AEN);
3313 if (!e)
3314 return QLA_ERROR;
3315
3316 e->u.aen.code = aen_code;
3317 e->u.aen.data_size = data_size;
3318 memcpy(e->u.aen.data, data, data_size);
3319
3320 qla4xxx_post_work(ha, e);
3321
3322 return QLA_SUCCESS;
3323}
3324
Vikas Chaudharyc0b9d3f2012-02-13 18:30:49 +05303325int qla4xxx_post_ping_evt_work(struct scsi_qla_host *ha,
3326 uint32_t status, uint32_t pid,
3327 uint32_t data_size, uint8_t *data)
3328{
3329 struct qla4_work_evt *e;
3330
3331 e = qla4xxx_alloc_work(ha, data_size, QLA4_EVENT_PING_STATUS);
3332 if (!e)
3333 return QLA_ERROR;
3334
3335 e->u.ping.status = status;
3336 e->u.ping.pid = pid;
3337 e->u.ping.data_size = data_size;
3338 memcpy(e->u.ping.data, data, data_size);
3339
3340 qla4xxx_post_work(ha, e);
3341
3342 return QLA_SUCCESS;
3343}
3344
Vikas Chaudharya7380a62012-02-27 03:08:56 -08003345static void qla4xxx_do_work(struct scsi_qla_host *ha)
Vikas Chaudharyff884432011-08-29 23:43:02 +05303346{
3347 struct qla4_work_evt *e, *tmp;
3348 unsigned long flags;
3349 LIST_HEAD(work);
3350
3351 spin_lock_irqsave(&ha->work_lock, flags);
3352 list_splice_init(&ha->work_list, &work);
3353 spin_unlock_irqrestore(&ha->work_lock, flags);
3354
3355 list_for_each_entry_safe(e, tmp, &work, list) {
3356 list_del_init(&e->list);
3357
3358 switch (e->type) {
3359 case QLA4_EVENT_AEN:
3360 iscsi_post_host_event(ha->host_no,
3361 &qla4xxx_iscsi_transport,
3362 e->u.aen.code,
3363 e->u.aen.data_size,
3364 e->u.aen.data);
3365 break;
Vikas Chaudharyc0b9d3f2012-02-13 18:30:49 +05303366 case QLA4_EVENT_PING_STATUS:
3367 iscsi_ping_comp_event(ha->host_no,
3368 &qla4xxx_iscsi_transport,
3369 e->u.ping.status,
3370 e->u.ping.pid,
3371 e->u.ping.data_size,
3372 e->u.ping.data);
3373 break;
Vikas Chaudharyff884432011-08-29 23:43:02 +05303374 default:
3375 ql4_printk(KERN_WARNING, ha, "event type: 0x%x not "
3376 "supported", e->type);
3377 }
3378 kfree(e);
3379 }
3380}
3381
David Somayajuluafaf5a22006-09-19 10:28:00 -07003382/**
3383 * qla4xxx_do_dpc - dpc routine
3384 * @data: in our case pointer to adapter structure
3385 *
3386 * This routine is a task that is schedule by the interrupt handler
3387 * to perform the background processing for interrupts. We put it
3388 * on a task queue that is consumed whenever the scheduler runs; that's
3389 * so you can do anything (i.e. put the process to sleep etc). In fact,
3390 * the mid-level tries to sleep when it reaches the driver threshold
3391 * "host->can_queue". This can cause a panic if we were in our interrupt code.
3392 **/
David Howellsc4028952006-11-22 14:57:56 +00003393static void qla4xxx_do_dpc(struct work_struct *work)
David Somayajuluafaf5a22006-09-19 10:28:00 -07003394{
David Howellsc4028952006-11-22 14:57:56 +00003395 struct scsi_qla_host *ha =
3396 container_of(work, struct scsi_qla_host, dpc_work);
David C Somayajulu477ffb92007-01-22 12:26:11 -08003397 int status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003398
David C Somayajuluf26b9042006-11-15 16:41:09 -08003399 DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303400 "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
3401 ha->host_no, __func__, ha->flags, ha->dpc_flags))
David Somayajuluafaf5a22006-09-19 10:28:00 -07003402
3403 /* Initialization not yet finished. Don't do anything yet. */
3404 if (!test_bit(AF_INIT_DONE, &ha->flags))
Lalit Chandivade1b468072011-05-17 23:17:09 -07003405 return;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003406
Lalit Chandivade2232be02010-07-30 14:38:47 +05303407 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
3408 DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
3409 ha->host_no, __func__, ha->flags));
Lalit Chandivade1b468072011-05-17 23:17:09 -07003410 return;
Lalit Chandivade2232be02010-07-30 14:38:47 +05303411 }
3412
Vikas Chaudharyff884432011-08-29 23:43:02 +05303413 /* post events to application */
3414 qla4xxx_do_work(ha);
3415
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04003416 if (is_qla80XX(ha)) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303417 if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
Tej Parkash546fef22012-09-20 07:35:12 -04003418 if (is_qla8032(ha)) {
3419 ql4_printk(KERN_INFO, ha, "%s: disabling pause transmit on port 0 & 1.\n",
3420 __func__);
3421 /* disable pause frame for ISP83xx */
3422 qla4_83xx_disable_pause(ha);
3423 }
3424
Vikas Chaudhary33693c72012-08-22 07:55:04 -04003425 ha->isp_ops->idc_lock(ha);
3426 qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
3427 QLA8XXX_DEV_FAILED);
3428 ha->isp_ops->idc_unlock(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303429 ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
3430 qla4_8xxx_device_state_handler(ha);
3431 }
Nilesh Javali320a61d2012-09-20 07:35:10 -04003432
3433 if (test_and_clear_bit(DPC_POST_IDC_ACK, &ha->dpc_flags))
3434 qla4_83xx_post_idc_ack(ha);
3435
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303436 if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
3437 qla4_8xxx_need_qsnt_handler(ha);
3438 }
3439 }
3440
3441 if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
3442 (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07003443 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303444 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04003445 if ((is_qla8022(ha) && ql4xdontresethba) ||
3446 (is_qla8032(ha) && qla4_83xx_idc_dontreset(ha))) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303447 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
3448 ha->host_no, __func__));
3449 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
3450 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
3451 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
3452 goto dpc_post_reset_ha;
3453 }
3454 if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
3455 test_bit(DPC_RESET_HA, &ha->dpc_flags))
3456 qla4xxx_recover_adapter(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003457
David C Somayajulu477ffb92007-01-22 12:26:11 -08003458 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07003459 uint8_t wait_time = RESET_INTR_TOV;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003460
David Somayajuluafaf5a22006-09-19 10:28:00 -07003461 while ((readw(&ha->reg->ctrl_status) &
3462 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
3463 if (--wait_time == 0)
3464 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003465 msleep(1000);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003466 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07003467 if (wait_time == 0)
3468 DEBUG2(printk("scsi%ld: %s: SR|FSR "
3469 "bit not cleared-- resetting\n",
3470 ha->host_no, __func__));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303471 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
David C Somayajulu477ffb92007-01-22 12:26:11 -08003472 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
3473 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303474 status = qla4xxx_recover_adapter(ha);
David C Somayajulu477ffb92007-01-22 12:26:11 -08003475 }
3476 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
3477 if (status == QLA_SUCCESS)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303478 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003479 }
3480 }
3481
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303482dpc_post_reset_ha:
David Somayajuluafaf5a22006-09-19 10:28:00 -07003483 /* ---- process AEN? --- */
3484 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
3485 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
3486
3487 /* ---- Get DHCP IP Address? --- */
3488 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
3489 qla4xxx_get_dhcp_ip_address(ha);
3490
Mike Christie13483732011-12-01 21:38:41 -06003491 /* ---- relogin device? --- */
3492 if (adapter_up(ha) &&
3493 test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
3494 iscsi_host_for_each_session(ha->host, qla4xxx_dpc_relogin);
3495 }
3496
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05303497 /* ---- link change? --- */
Nilesh Javali026fbd32013-01-20 23:50:58 -05003498 if (!test_bit(AF_LOOPBACK, &ha->flags) &&
3499 test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05303500 if (!test_bit(AF_LINK_UP, &ha->flags)) {
3501 /* ---- link down? --- */
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07003502 qla4xxx_mark_all_devices_missing(ha);
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05303503 } else {
3504 /* ---- link up? --- *
3505 * F/W will auto login to all devices ONLY ONCE after
3506 * link up during driver initialization and runtime
3507 * fatal error recovery. Therefore, the driver must
3508 * manually relogin to devices when recovering from
3509 * connection failures, logouts, expired KATO, etc. */
Mike Christie13483732011-12-01 21:38:41 -06003510 if (test_and_clear_bit(AF_BUILD_DDB_LIST, &ha->flags)) {
3511 qla4xxx_build_ddb_list(ha, ha->is_reset);
3512 iscsi_host_for_each_session(ha->host,
3513 qla4xxx_login_flash_ddb);
3514 } else
3515 qla4xxx_relogin_all_devices(ha);
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05303516 }
3517 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07003518}
3519
3520/**
3521 * qla4xxx_free_adapter - release the adapter
3522 * @ha: pointer to adapter structure
3523 **/
3524static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
3525{
Sarang Radke8a288962011-12-06 02:34:10 -08003526 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003527
Vikas Chaudhary5c19b922012-11-23 06:58:38 -05003528 /* Turn-off interrupts on the card. */
3529 ha->isp_ops->disable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003530
Vikas Chaudharyd9e62e52012-08-22 07:45:25 -04003531 if (is_qla40XX(ha)) {
3532 writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
3533 &ha->reg->ctrl_status);
3534 readl(&ha->reg->ctrl_status);
3535 } else if (is_qla8022(ha)) {
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -04003536 writel(0, &ha->qla4_82xx_reg->host_int);
3537 readl(&ha->qla4_82xx_reg->host_int);
Vikas Chaudharyfbd81072012-09-20 07:35:03 -04003538 } else if (is_qla8032(ha)) {
3539 writel(0, &ha->qla4_83xx_reg->risc_intr);
3540 readl(&ha->qla4_83xx_reg->risc_intr);
Vikas Chaudharyd9e62e52012-08-22 07:45:25 -04003541 }
3542
David Somayajuluafaf5a22006-09-19 10:28:00 -07003543 /* Remove timer thread, if present */
3544 if (ha->timer_active)
3545 qla4xxx_stop_timer(ha);
3546
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303547 /* Kill the kernel thread for this host */
3548 if (ha->dpc_thread)
3549 destroy_workqueue(ha->dpc_thread);
3550
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003551 /* Kill the kernel thread for this host */
3552 if (ha->task_wq)
3553 destroy_workqueue(ha->task_wq);
3554
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303555 /* Put firmware in known state */
3556 ha->isp_ops->reset_firmware(ha);
3557
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04003558 if (is_qla80XX(ha)) {
Vikas Chaudhary33693c72012-08-22 07:55:04 -04003559 ha->isp_ops->idc_lock(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303560 qla4_8xxx_clear_drv_active(ha);
Vikas Chaudhary33693c72012-08-22 07:55:04 -04003561 ha->isp_ops->idc_unlock(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303562 }
3563
David Somayajuluafaf5a22006-09-19 10:28:00 -07003564 /* Detach interrupts */
Vikas Chaudhary5c19b922012-11-23 06:58:38 -05003565 qla4xxx_free_irqs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003566
David C Somayajulubee4fe82007-05-23 18:03:32 -07003567 /* free extra memory */
3568 qla4xxx_mem_free(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303569}
David C Somayajulubee4fe82007-05-23 18:03:32 -07003570
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303571int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
3572{
3573 int status = 0;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303574 unsigned long mem_base, mem_len, db_base, db_len;
3575 struct pci_dev *pdev = ha->pdev;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003576
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303577 status = pci_request_regions(pdev, DRIVER_NAME);
3578 if (status) {
3579 printk(KERN_WARNING
3580 "scsi(%ld) Failed to reserve PIO regions (%s) "
3581 "status=%d\n", ha->host_no, pci_name(pdev), status);
3582 goto iospace_error_exit;
3583 }
3584
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303585 DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
Sergei Shtylyov7d7311c2012-03-14 22:04:30 +03003586 __func__, pdev->revision));
3587 ha->revision_id = pdev->revision;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303588
3589 /* remap phys address */
3590 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
3591 mem_len = pci_resource_len(pdev, 0);
3592 DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
3593 __func__, mem_base, mem_len));
3594
3595 /* mapping of pcibase pointer */
3596 ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
3597 if (!ha->nx_pcibase) {
3598 printk(KERN_ERR
3599 "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
3600 pci_release_regions(ha->pdev);
3601 goto iospace_error_exit;
3602 }
3603
3604 /* Mapping of IO base pointer, door bell read and write pointer */
3605
3606 /* mapping of IO base pointer */
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04003607 if (is_qla8022(ha)) {
3608 ha->qla4_82xx_reg = (struct device_reg_82xx __iomem *)
3609 ((uint8_t *)ha->nx_pcibase + 0xbc000 +
3610 (ha->pdev->devfn << 11));
3611 ha->nx_db_wr_ptr = (ha->pdev->devfn == 4 ? QLA82XX_CAM_RAM_DB1 :
3612 QLA82XX_CAM_RAM_DB2);
3613 } else if (is_qla8032(ha)) {
3614 ha->qla4_83xx_reg = (struct device_reg_83xx __iomem *)
3615 ((uint8_t *)ha->nx_pcibase);
3616 }
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303617
3618 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
3619 db_len = pci_resource_len(pdev, 4);
3620
Shyam Sundar2657c802010-10-06 22:50:29 -07003621 return 0;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303622iospace_error_exit:
3623 return -ENOMEM;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003624}
3625
3626/***
3627 * qla4xxx_iospace_config - maps registers
3628 * @ha: pointer to adapter structure
3629 *
3630 * This routines maps HBA's registers from the pci address space
3631 * into the kernel virtual address space for memory mapped i/o.
3632 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303633int qla4xxx_iospace_config(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07003634{
3635 unsigned long pio, pio_len, pio_flags;
3636 unsigned long mmio, mmio_len, mmio_flags;
3637
3638 pio = pci_resource_start(ha->pdev, 0);
3639 pio_len = pci_resource_len(ha->pdev, 0);
3640 pio_flags = pci_resource_flags(ha->pdev, 0);
3641 if (pio_flags & IORESOURCE_IO) {
3642 if (pio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303643 ql4_printk(KERN_WARNING, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07003644 "Invalid PCI I/O region size\n");
3645 pio = 0;
3646 }
3647 } else {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303648 ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07003649 pio = 0;
3650 }
3651
3652 /* Use MMIO operations for all accesses. */
3653 mmio = pci_resource_start(ha->pdev, 1);
3654 mmio_len = pci_resource_len(ha->pdev, 1);
3655 mmio_flags = pci_resource_flags(ha->pdev, 1);
3656
3657 if (!(mmio_flags & IORESOURCE_MEM)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303658 ql4_printk(KERN_ERR, ha,
3659 "region #0 not an MMIO resource, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07003660
3661 goto iospace_error_exit;
3662 }
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303663
David Somayajuluafaf5a22006-09-19 10:28:00 -07003664 if (mmio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303665 ql4_printk(KERN_ERR, ha,
3666 "Invalid PCI mem region size, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07003667 goto iospace_error_exit;
3668 }
3669
3670 if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303671 ql4_printk(KERN_WARNING, ha,
3672 "Failed to reserve PIO/MMIO regions\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07003673
3674 goto iospace_error_exit;
3675 }
3676
3677 ha->pio_address = pio;
3678 ha->pio_length = pio_len;
3679 ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
3680 if (!ha->reg) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05303681 ql4_printk(KERN_ERR, ha,
3682 "cannot remap MMIO, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07003683
3684 goto iospace_error_exit;
3685 }
3686
3687 return 0;
3688
3689iospace_error_exit:
3690 return -ENOMEM;
3691}
3692
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303693static struct isp_operations qla4xxx_isp_ops = {
3694 .iospace_config = qla4xxx_iospace_config,
3695 .pci_config = qla4xxx_pci_config,
3696 .disable_intrs = qla4xxx_disable_intrs,
3697 .enable_intrs = qla4xxx_enable_intrs,
3698 .start_firmware = qla4xxx_start_firmware,
3699 .intr_handler = qla4xxx_intr_handler,
3700 .interrupt_service_routine = qla4xxx_interrupt_service_routine,
3701 .reset_chip = qla4xxx_soft_reset,
3702 .reset_firmware = qla4xxx_hw_reset,
3703 .queue_iocb = qla4xxx_queue_iocb,
3704 .complete_iocb = qla4xxx_complete_iocb,
3705 .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
3706 .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
3707 .get_sys_info = qla4xxx_get_sys_info,
Vikas Chaudhary33693c72012-08-22 07:55:04 -04003708 .queue_mailbox_command = qla4xxx_queue_mbox_cmd,
3709 .process_mailbox_interrupt = qla4xxx_process_mbox_intr,
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303710};
3711
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -04003712static struct isp_operations qla4_82xx_isp_ops = {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303713 .iospace_config = qla4_8xxx_iospace_config,
3714 .pci_config = qla4_8xxx_pci_config,
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04003715 .disable_intrs = qla4_82xx_disable_intrs,
3716 .enable_intrs = qla4_82xx_enable_intrs,
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303717 .start_firmware = qla4_8xxx_load_risc,
Vikas Chaudhary33693c72012-08-22 07:55:04 -04003718 .restart_firmware = qla4_82xx_try_start_fw,
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04003719 .intr_handler = qla4_82xx_intr_handler,
3720 .interrupt_service_routine = qla4_82xx_interrupt_service_routine,
Vikas Chaudhary33693c72012-08-22 07:55:04 -04003721 .need_reset = qla4_8xxx_need_reset,
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04003722 .reset_chip = qla4_82xx_isp_reset,
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303723 .reset_firmware = qla4_8xxx_stop_firmware,
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04003724 .queue_iocb = qla4_82xx_queue_iocb,
3725 .complete_iocb = qla4_82xx_complete_iocb,
3726 .rd_shdw_req_q_out = qla4_82xx_rd_shdw_req_q_out,
3727 .rd_shdw_rsp_q_in = qla4_82xx_rd_shdw_rsp_q_in,
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303728 .get_sys_info = qla4_8xxx_get_sys_info,
Vikas Chaudhary33693c72012-08-22 07:55:04 -04003729 .rd_reg_direct = qla4_82xx_rd_32,
3730 .wr_reg_direct = qla4_82xx_wr_32,
3731 .rd_reg_indirect = qla4_82xx_md_rd_32,
3732 .wr_reg_indirect = qla4_82xx_md_wr_32,
3733 .idc_lock = qla4_82xx_idc_lock,
3734 .idc_unlock = qla4_82xx_idc_unlock,
3735 .rom_lock_recovery = qla4_82xx_rom_lock_recovery,
3736 .queue_mailbox_command = qla4_82xx_queue_mbox_cmd,
3737 .process_mailbox_interrupt = qla4_82xx_process_mbox_intr,
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303738};
3739
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04003740static struct isp_operations qla4_83xx_isp_ops = {
3741 .iospace_config = qla4_8xxx_iospace_config,
3742 .pci_config = qla4_8xxx_pci_config,
3743 .disable_intrs = qla4_83xx_disable_intrs,
3744 .enable_intrs = qla4_83xx_enable_intrs,
3745 .start_firmware = qla4_8xxx_load_risc,
3746 .restart_firmware = qla4_83xx_start_firmware,
3747 .intr_handler = qla4_83xx_intr_handler,
3748 .interrupt_service_routine = qla4_83xx_interrupt_service_routine,
3749 .need_reset = qla4_8xxx_need_reset,
3750 .reset_chip = qla4_83xx_isp_reset,
3751 .reset_firmware = qla4_8xxx_stop_firmware,
3752 .queue_iocb = qla4_83xx_queue_iocb,
3753 .complete_iocb = qla4_83xx_complete_iocb,
3754 .rd_shdw_req_q_out = qla4_83xx_rd_shdw_req_q_out,
3755 .rd_shdw_rsp_q_in = qla4_83xx_rd_shdw_rsp_q_in,
3756 .get_sys_info = qla4_8xxx_get_sys_info,
3757 .rd_reg_direct = qla4_83xx_rd_reg,
3758 .wr_reg_direct = qla4_83xx_wr_reg,
3759 .rd_reg_indirect = qla4_83xx_rd_reg_indirect,
3760 .wr_reg_indirect = qla4_83xx_wr_reg_indirect,
3761 .idc_lock = qla4_83xx_drv_lock,
3762 .idc_unlock = qla4_83xx_drv_unlock,
3763 .rom_lock_recovery = qla4_83xx_rom_lock_recovery,
3764 .queue_mailbox_command = qla4_83xx_queue_mbox_cmd,
3765 .process_mailbox_interrupt = qla4_83xx_process_mbox_intr,
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303766};
3767
3768uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
3769{
3770 return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
3771}
3772
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04003773uint16_t qla4_82xx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303774{
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -04003775 return (uint16_t)le32_to_cpu(readl(&ha->qla4_82xx_reg->req_q_out));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303776}
3777
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04003778uint16_t qla4_83xx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
3779{
3780 return (uint16_t)le32_to_cpu(readl(&ha->qla4_83xx_reg->req_q_out));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303781}
3782
3783uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
3784{
3785 return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
3786}
3787
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04003788uint16_t qla4_82xx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303789{
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -04003790 return (uint16_t)le32_to_cpu(readl(&ha->qla4_82xx_reg->rsp_q_in));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303791}
3792
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04003793uint16_t qla4_83xx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
3794{
3795 return (uint16_t)le32_to_cpu(readl(&ha->qla4_83xx_reg->rsp_q_in));
David Somayajuluafaf5a22006-09-19 10:28:00 -07003796}
3797
Manish Rangankar2a991c22011-07-25 13:48:55 -05003798static ssize_t qla4xxx_show_boot_eth_info(void *data, int type, char *buf)
3799{
3800 struct scsi_qla_host *ha = data;
3801 char *str = buf;
3802 int rc;
3803
3804 switch (type) {
3805 case ISCSI_BOOT_ETH_FLAGS:
3806 rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
3807 break;
3808 case ISCSI_BOOT_ETH_INDEX:
3809 rc = sprintf(str, "0\n");
3810 break;
3811 case ISCSI_BOOT_ETH_MAC:
3812 rc = sysfs_format_mac(str, ha->my_mac,
3813 MAC_ADDR_LEN);
3814 break;
3815 default:
3816 rc = -ENOSYS;
3817 break;
3818 }
3819 return rc;
3820}
3821
Al Viro587a1f12011-07-23 23:11:19 -04003822static umode_t qla4xxx_eth_get_attr_visibility(void *data, int type)
Manish Rangankar2a991c22011-07-25 13:48:55 -05003823{
3824 int rc;
3825
3826 switch (type) {
3827 case ISCSI_BOOT_ETH_FLAGS:
3828 case ISCSI_BOOT_ETH_MAC:
3829 case ISCSI_BOOT_ETH_INDEX:
3830 rc = S_IRUGO;
3831 break;
3832 default:
3833 rc = 0;
3834 break;
3835 }
3836 return rc;
3837}
3838
3839static ssize_t qla4xxx_show_boot_ini_info(void *data, int type, char *buf)
3840{
3841 struct scsi_qla_host *ha = data;
3842 char *str = buf;
3843 int rc;
3844
3845 switch (type) {
3846 case ISCSI_BOOT_INI_INITIATOR_NAME:
3847 rc = sprintf(str, "%s\n", ha->name_string);
3848 break;
3849 default:
3850 rc = -ENOSYS;
3851 break;
3852 }
3853 return rc;
3854}
3855
Al Viro587a1f12011-07-23 23:11:19 -04003856static umode_t qla4xxx_ini_get_attr_visibility(void *data, int type)
Manish Rangankar2a991c22011-07-25 13:48:55 -05003857{
3858 int rc;
3859
3860 switch (type) {
3861 case ISCSI_BOOT_INI_INITIATOR_NAME:
3862 rc = S_IRUGO;
3863 break;
3864 default:
3865 rc = 0;
3866 break;
3867 }
3868 return rc;
3869}
3870
3871static ssize_t
3872qla4xxx_show_boot_tgt_info(struct ql4_boot_session_info *boot_sess, int type,
3873 char *buf)
3874{
3875 struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
3876 char *str = buf;
3877 int rc;
3878
3879 switch (type) {
3880 case ISCSI_BOOT_TGT_NAME:
3881 rc = sprintf(buf, "%s\n", (char *)&boot_sess->target_name);
3882 break;
3883 case ISCSI_BOOT_TGT_IP_ADDR:
3884 if (boot_sess->conn_list[0].dest_ipaddr.ip_type == 0x1)
3885 rc = sprintf(buf, "%pI4\n",
3886 &boot_conn->dest_ipaddr.ip_address);
3887 else
3888 rc = sprintf(str, "%pI6\n",
3889 &boot_conn->dest_ipaddr.ip_address);
3890 break;
3891 case ISCSI_BOOT_TGT_PORT:
3892 rc = sprintf(str, "%d\n", boot_conn->dest_port);
3893 break;
3894 case ISCSI_BOOT_TGT_CHAP_NAME:
3895 rc = sprintf(str, "%.*s\n",
3896 boot_conn->chap.target_chap_name_length,
3897 (char *)&boot_conn->chap.target_chap_name);
3898 break;
3899 case ISCSI_BOOT_TGT_CHAP_SECRET:
3900 rc = sprintf(str, "%.*s\n",
3901 boot_conn->chap.target_secret_length,
3902 (char *)&boot_conn->chap.target_secret);
3903 break;
3904 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
3905 rc = sprintf(str, "%.*s\n",
3906 boot_conn->chap.intr_chap_name_length,
3907 (char *)&boot_conn->chap.intr_chap_name);
3908 break;
3909 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
3910 rc = sprintf(str, "%.*s\n",
3911 boot_conn->chap.intr_secret_length,
3912 (char *)&boot_conn->chap.intr_secret);
3913 break;
3914 case ISCSI_BOOT_TGT_FLAGS:
3915 rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
3916 break;
3917 case ISCSI_BOOT_TGT_NIC_ASSOC:
3918 rc = sprintf(str, "0\n");
3919 break;
3920 default:
3921 rc = -ENOSYS;
3922 break;
3923 }
3924 return rc;
3925}
3926
3927static ssize_t qla4xxx_show_boot_tgt_pri_info(void *data, int type, char *buf)
3928{
3929 struct scsi_qla_host *ha = data;
3930 struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_pri_sess);
3931
3932 return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
3933}
3934
3935static ssize_t qla4xxx_show_boot_tgt_sec_info(void *data, int type, char *buf)
3936{
3937 struct scsi_qla_host *ha = data;
3938 struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_sec_sess);
3939
3940 return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
3941}
3942
Al Viro587a1f12011-07-23 23:11:19 -04003943static umode_t qla4xxx_tgt_get_attr_visibility(void *data, int type)
Manish Rangankar2a991c22011-07-25 13:48:55 -05003944{
3945 int rc;
3946
3947 switch (type) {
3948 case ISCSI_BOOT_TGT_NAME:
3949 case ISCSI_BOOT_TGT_IP_ADDR:
3950 case ISCSI_BOOT_TGT_PORT:
3951 case ISCSI_BOOT_TGT_CHAP_NAME:
3952 case ISCSI_BOOT_TGT_CHAP_SECRET:
3953 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
3954 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
3955 case ISCSI_BOOT_TGT_NIC_ASSOC:
3956 case ISCSI_BOOT_TGT_FLAGS:
3957 rc = S_IRUGO;
3958 break;
3959 default:
3960 rc = 0;
3961 break;
3962 }
3963 return rc;
3964}
3965
3966static void qla4xxx_boot_release(void *data)
3967{
3968 struct scsi_qla_host *ha = data;
3969
3970 scsi_host_put(ha->host);
3971}
3972
3973static int get_fw_boot_info(struct scsi_qla_host *ha, uint16_t ddb_index[])
3974{
3975 dma_addr_t buf_dma;
3976 uint32_t addr, pri_addr, sec_addr;
3977 uint32_t offset;
3978 uint16_t func_num;
3979 uint8_t val;
3980 uint8_t *buf = NULL;
3981 size_t size = 13 * sizeof(uint8_t);
3982 int ret = QLA_SUCCESS;
3983
3984 func_num = PCI_FUNC(ha->pdev->devfn);
3985
Manish Rangankar0d5b36b2011-10-07 16:55:51 -07003986 ql4_printk(KERN_INFO, ha, "%s: Get FW boot info for 0x%x func %d\n",
3987 __func__, ha->pdev->device, func_num);
Manish Rangankar2a991c22011-07-25 13:48:55 -05003988
Manish Rangankar0d5b36b2011-10-07 16:55:51 -07003989 if (is_qla40XX(ha)) {
Manish Rangankar2a991c22011-07-25 13:48:55 -05003990 if (func_num == 1) {
3991 addr = NVRAM_PORT0_BOOT_MODE;
3992 pri_addr = NVRAM_PORT0_BOOT_PRI_TGT;
3993 sec_addr = NVRAM_PORT0_BOOT_SEC_TGT;
3994 } else if (func_num == 3) {
3995 addr = NVRAM_PORT1_BOOT_MODE;
3996 pri_addr = NVRAM_PORT1_BOOT_PRI_TGT;
3997 sec_addr = NVRAM_PORT1_BOOT_SEC_TGT;
3998 } else {
3999 ret = QLA_ERROR;
4000 goto exit_boot_info;
4001 }
4002
4003 /* Check Boot Mode */
4004 val = rd_nvram_byte(ha, addr);
4005 if (!(val & 0x07)) {
Manish Rangankare8fb00e2012-02-13 18:30:43 +05304006 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Adapter boot "
4007 "options : 0x%x\n", __func__, val));
Manish Rangankar2a991c22011-07-25 13:48:55 -05004008 ret = QLA_ERROR;
4009 goto exit_boot_info;
4010 }
4011
4012 /* get primary valid target index */
4013 val = rd_nvram_byte(ha, pri_addr);
4014 if (val & BIT_7)
4015 ddb_index[0] = (val & 0x7f);
Manish Rangankar2a991c22011-07-25 13:48:55 -05004016
4017 /* get secondary valid target index */
4018 val = rd_nvram_byte(ha, sec_addr);
4019 if (val & BIT_7)
4020 ddb_index[1] = (val & 0x7f);
Manish Rangankar2a991c22011-07-25 13:48:55 -05004021
Vikas Chaudhary3e788fb2013-03-07 05:43:08 -05004022 } else if (is_qla80XX(ha)) {
Manish Rangankar2a991c22011-07-25 13:48:55 -05004023 buf = dma_alloc_coherent(&ha->pdev->dev, size,
4024 &buf_dma, GFP_KERNEL);
4025 if (!buf) {
4026 DEBUG2(ql4_printk(KERN_ERR, ha,
4027 "%s: Unable to allocate dma buffer\n",
4028 __func__));
4029 ret = QLA_ERROR;
4030 goto exit_boot_info;
4031 }
4032
4033 if (ha->port_num == 0)
4034 offset = BOOT_PARAM_OFFSET_PORT0;
4035 else if (ha->port_num == 1)
4036 offset = BOOT_PARAM_OFFSET_PORT1;
4037 else {
4038 ret = QLA_ERROR;
4039 goto exit_boot_info_free;
4040 }
4041 addr = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_iscsi_param * 4) +
4042 offset;
4043 if (qla4xxx_get_flash(ha, buf_dma, addr,
4044 13 * sizeof(uint8_t)) != QLA_SUCCESS) {
4045 DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: Get Flash"
Petr Uzel0bd7f842012-02-24 16:32:59 +01004046 " failed\n", ha->host_no, __func__));
Manish Rangankar2a991c22011-07-25 13:48:55 -05004047 ret = QLA_ERROR;
4048 goto exit_boot_info_free;
4049 }
4050 /* Check Boot Mode */
4051 if (!(buf[1] & 0x07)) {
Manish Rangankare8fb00e2012-02-13 18:30:43 +05304052 DEBUG2(ql4_printk(KERN_INFO, ha, "Firmware boot options"
4053 " : 0x%x\n", buf[1]));
Manish Rangankar2a991c22011-07-25 13:48:55 -05004054 ret = QLA_ERROR;
4055 goto exit_boot_info_free;
4056 }
4057
4058 /* get primary valid target index */
4059 if (buf[2] & BIT_7)
4060 ddb_index[0] = buf[2] & 0x7f;
Manish Rangankar2a991c22011-07-25 13:48:55 -05004061
4062 /* get secondary valid target index */
4063 if (buf[11] & BIT_7)
4064 ddb_index[1] = buf[11] & 0x7f;
Manish Rangankar2a991c22011-07-25 13:48:55 -05004065 } else {
4066 ret = QLA_ERROR;
4067 goto exit_boot_info;
4068 }
4069
4070 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Primary target ID %d, Secondary"
4071 " target ID %d\n", __func__, ddb_index[0],
4072 ddb_index[1]));
4073
4074exit_boot_info_free:
4075 dma_free_coherent(&ha->pdev->dev, size, buf, buf_dma);
4076exit_boot_info:
Lalit Chandivade20e835b2012-02-13 18:30:42 +05304077 ha->pri_ddb_idx = ddb_index[0];
4078 ha->sec_ddb_idx = ddb_index[1];
Manish Rangankar2a991c22011-07-25 13:48:55 -05004079 return ret;
4080}
4081
Lalit Chandivade28deb452011-10-07 16:55:44 -07004082/**
4083 * qla4xxx_get_bidi_chap - Get a BIDI CHAP user and password
4084 * @ha: pointer to adapter structure
4085 * @username: CHAP username to be returned
4086 * @password: CHAP password to be returned
4087 *
4088 * If a boot entry has BIDI CHAP enabled then we need to set the BIDI CHAP
4089 * user and password in the sysfs entry in /sys/firmware/iscsi_boot#/.
4090 * So from the CHAP cache find the first BIDI CHAP entry and set it
4091 * to the boot record in sysfs.
4092 **/
4093static int qla4xxx_get_bidi_chap(struct scsi_qla_host *ha, char *username,
4094 char *password)
4095{
4096 int i, ret = -EINVAL;
4097 int max_chap_entries = 0;
4098 struct ql4_chap_table *chap_table;
4099
4100 if (is_qla8022(ha))
4101 max_chap_entries = (ha->hw.flt_chap_size / 2) /
4102 sizeof(struct ql4_chap_table);
4103 else
4104 max_chap_entries = MAX_CHAP_ENTRIES_40XX;
4105
4106 if (!ha->chap_list) {
4107 ql4_printk(KERN_ERR, ha, "Do not have CHAP table cache\n");
4108 return ret;
4109 }
4110
4111 mutex_lock(&ha->chap_sem);
4112 for (i = 0; i < max_chap_entries; i++) {
4113 chap_table = (struct ql4_chap_table *)ha->chap_list + i;
4114 if (chap_table->cookie !=
4115 __constant_cpu_to_le16(CHAP_VALID_COOKIE)) {
4116 continue;
4117 }
4118
4119 if (chap_table->flags & BIT_7) /* local */
4120 continue;
4121
4122 if (!(chap_table->flags & BIT_6)) /* Not BIDI */
4123 continue;
4124
4125 strncpy(password, chap_table->secret, QL4_CHAP_MAX_SECRET_LEN);
4126 strncpy(username, chap_table->name, QL4_CHAP_MAX_NAME_LEN);
4127 ret = 0;
4128 break;
4129 }
4130 mutex_unlock(&ha->chap_sem);
4131
4132 return ret;
4133}
4134
4135
Manish Rangankar2a991c22011-07-25 13:48:55 -05004136static int qla4xxx_get_boot_target(struct scsi_qla_host *ha,
4137 struct ql4_boot_session_info *boot_sess,
4138 uint16_t ddb_index)
4139{
4140 struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
4141 struct dev_db_entry *fw_ddb_entry;
4142 dma_addr_t fw_ddb_entry_dma;
4143 uint16_t idx;
4144 uint16_t options;
4145 int ret = QLA_SUCCESS;
4146
4147 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
4148 &fw_ddb_entry_dma, GFP_KERNEL);
4149 if (!fw_ddb_entry) {
4150 DEBUG2(ql4_printk(KERN_ERR, ha,
4151 "%s: Unable to allocate dma buffer.\n",
4152 __func__));
4153 ret = QLA_ERROR;
4154 return ret;
4155 }
4156
4157 if (qla4xxx_bootdb_by_index(ha, fw_ddb_entry,
4158 fw_ddb_entry_dma, ddb_index)) {
Manish Rangankare8fb00e2012-02-13 18:30:43 +05304159 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: No Flash DDB found at "
4160 "index [%d]\n", __func__, ddb_index));
Manish Rangankar2a991c22011-07-25 13:48:55 -05004161 ret = QLA_ERROR;
4162 goto exit_boot_target;
4163 }
4164
4165 /* Update target name and IP from DDB */
4166 memcpy(boot_sess->target_name, fw_ddb_entry->iscsi_name,
4167 min(sizeof(boot_sess->target_name),
4168 sizeof(fw_ddb_entry->iscsi_name)));
4169
4170 options = le16_to_cpu(fw_ddb_entry->options);
4171 if (options & DDB_OPT_IPV6_DEVICE) {
4172 memcpy(&boot_conn->dest_ipaddr.ip_address,
4173 &fw_ddb_entry->ip_addr[0], IPv6_ADDR_LEN);
4174 } else {
4175 boot_conn->dest_ipaddr.ip_type = 0x1;
4176 memcpy(&boot_conn->dest_ipaddr.ip_address,
4177 &fw_ddb_entry->ip_addr[0], IP_ADDR_LEN);
4178 }
4179
4180 boot_conn->dest_port = le16_to_cpu(fw_ddb_entry->port);
4181
4182 /* update chap information */
4183 idx = __le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
4184
4185 if (BIT_7 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
4186
4187 DEBUG2(ql4_printk(KERN_INFO, ha, "Setting chap\n"));
4188
4189 ret = qla4xxx_get_chap(ha, (char *)&boot_conn->chap.
4190 target_chap_name,
4191 (char *)&boot_conn->chap.target_secret,
4192 idx);
4193 if (ret) {
4194 ql4_printk(KERN_ERR, ha, "Failed to set chap\n");
4195 ret = QLA_ERROR;
4196 goto exit_boot_target;
4197 }
4198
4199 boot_conn->chap.target_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
4200 boot_conn->chap.target_secret_length = QL4_CHAP_MAX_SECRET_LEN;
4201 }
4202
4203 if (BIT_4 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
4204
4205 DEBUG2(ql4_printk(KERN_INFO, ha, "Setting BIDI chap\n"));
4206
Lalit Chandivade28deb452011-10-07 16:55:44 -07004207 ret = qla4xxx_get_bidi_chap(ha,
4208 (char *)&boot_conn->chap.intr_chap_name,
4209 (char *)&boot_conn->chap.intr_secret);
4210
Manish Rangankar2a991c22011-07-25 13:48:55 -05004211 if (ret) {
4212 ql4_printk(KERN_ERR, ha, "Failed to set BIDI chap\n");
4213 ret = QLA_ERROR;
4214 goto exit_boot_target;
4215 }
4216
4217 boot_conn->chap.intr_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
4218 boot_conn->chap.intr_secret_length = QL4_CHAP_MAX_SECRET_LEN;
4219 }
4220
4221exit_boot_target:
4222 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
4223 fw_ddb_entry, fw_ddb_entry_dma);
4224 return ret;
4225}
4226
4227static int qla4xxx_get_boot_info(struct scsi_qla_host *ha)
4228{
4229 uint16_t ddb_index[2];
Lalit Chandivade8de5b952011-10-07 16:55:43 -07004230 int ret = QLA_ERROR;
4231 int rval;
Manish Rangankar2a991c22011-07-25 13:48:55 -05004232
4233 memset(ddb_index, 0, sizeof(ddb_index));
Lalit Chandivade8de5b952011-10-07 16:55:43 -07004234 ddb_index[0] = 0xffff;
4235 ddb_index[1] = 0xffff;
Manish Rangankar2a991c22011-07-25 13:48:55 -05004236 ret = get_fw_boot_info(ha, ddb_index);
4237 if (ret != QLA_SUCCESS) {
Manish Rangankare8fb00e2012-02-13 18:30:43 +05304238 DEBUG2(ql4_printk(KERN_INFO, ha,
4239 "%s: No boot target configured.\n", __func__));
Manish Rangankar2a991c22011-07-25 13:48:55 -05004240 return ret;
4241 }
4242
Mike Christie13483732011-12-01 21:38:41 -06004243 if (ql4xdisablesysfsboot)
4244 return QLA_SUCCESS;
4245
Lalit Chandivade8de5b952011-10-07 16:55:43 -07004246 if (ddb_index[0] == 0xffff)
4247 goto sec_target;
4248
4249 rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_pri_sess),
Manish Rangankar2a991c22011-07-25 13:48:55 -05004250 ddb_index[0]);
Lalit Chandivade8de5b952011-10-07 16:55:43 -07004251 if (rval != QLA_SUCCESS) {
Manish Rangankare8fb00e2012-02-13 18:30:43 +05304252 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Primary boot target not "
4253 "configured\n", __func__));
Lalit Chandivade8de5b952011-10-07 16:55:43 -07004254 } else
4255 ret = QLA_SUCCESS;
Manish Rangankar2a991c22011-07-25 13:48:55 -05004256
Lalit Chandivade8de5b952011-10-07 16:55:43 -07004257sec_target:
4258 if (ddb_index[1] == 0xffff)
4259 goto exit_get_boot_info;
4260
4261 rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_sec_sess),
Manish Rangankar2a991c22011-07-25 13:48:55 -05004262 ddb_index[1]);
Lalit Chandivade8de5b952011-10-07 16:55:43 -07004263 if (rval != QLA_SUCCESS) {
Manish Rangankare8fb00e2012-02-13 18:30:43 +05304264 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Secondary boot target not"
4265 " configured\n", __func__));
Lalit Chandivade8de5b952011-10-07 16:55:43 -07004266 } else
4267 ret = QLA_SUCCESS;
4268
4269exit_get_boot_info:
Manish Rangankar2a991c22011-07-25 13:48:55 -05004270 return ret;
4271}
4272
4273static int qla4xxx_setup_boot_info(struct scsi_qla_host *ha)
4274{
4275 struct iscsi_boot_kobj *boot_kobj;
4276
4277 if (qla4xxx_get_boot_info(ha) != QLA_SUCCESS)
Mike Christie13483732011-12-01 21:38:41 -06004278 return QLA_ERROR;
4279
4280 if (ql4xdisablesysfsboot) {
4281 ql4_printk(KERN_INFO, ha,
Petr Uzel0bd7f842012-02-24 16:32:59 +01004282 "%s: syfsboot disabled - driver will trigger login "
Mike Christie13483732011-12-01 21:38:41 -06004283 "and publish session for discovery .\n", __func__);
4284 return QLA_SUCCESS;
4285 }
4286
Manish Rangankar2a991c22011-07-25 13:48:55 -05004287
4288 ha->boot_kset = iscsi_boot_create_host_kset(ha->host->host_no);
4289 if (!ha->boot_kset)
4290 goto kset_free;
4291
4292 if (!scsi_host_get(ha->host))
4293 goto kset_free;
4294 boot_kobj = iscsi_boot_create_target(ha->boot_kset, 0, ha,
4295 qla4xxx_show_boot_tgt_pri_info,
4296 qla4xxx_tgt_get_attr_visibility,
4297 qla4xxx_boot_release);
4298 if (!boot_kobj)
4299 goto put_host;
4300
4301 if (!scsi_host_get(ha->host))
4302 goto kset_free;
4303 boot_kobj = iscsi_boot_create_target(ha->boot_kset, 1, ha,
4304 qla4xxx_show_boot_tgt_sec_info,
4305 qla4xxx_tgt_get_attr_visibility,
4306 qla4xxx_boot_release);
4307 if (!boot_kobj)
4308 goto put_host;
4309
4310 if (!scsi_host_get(ha->host))
4311 goto kset_free;
4312 boot_kobj = iscsi_boot_create_initiator(ha->boot_kset, 0, ha,
4313 qla4xxx_show_boot_ini_info,
4314 qla4xxx_ini_get_attr_visibility,
4315 qla4xxx_boot_release);
4316 if (!boot_kobj)
4317 goto put_host;
4318
4319 if (!scsi_host_get(ha->host))
4320 goto kset_free;
4321 boot_kobj = iscsi_boot_create_ethernet(ha->boot_kset, 0, ha,
4322 qla4xxx_show_boot_eth_info,
4323 qla4xxx_eth_get_attr_visibility,
4324 qla4xxx_boot_release);
4325 if (!boot_kobj)
4326 goto put_host;
4327
Mike Christie13483732011-12-01 21:38:41 -06004328 return QLA_SUCCESS;
Manish Rangankar2a991c22011-07-25 13:48:55 -05004329
4330put_host:
4331 scsi_host_put(ha->host);
4332kset_free:
4333 iscsi_boot_destroy_kset(ha->boot_kset);
4334 return -ENOMEM;
4335}
4336
Lalit Chandivade45494152011-10-07 16:55:42 -07004337
4338/**
4339 * qla4xxx_create chap_list - Create CHAP list from FLASH
4340 * @ha: pointer to adapter structure
4341 *
4342 * Read flash and make a list of CHAP entries, during login when a CHAP entry
4343 * is received, it will be checked in this list. If entry exist then the CHAP
4344 * entry index is set in the DDB. If CHAP entry does not exist in this list
4345 * then a new entry is added in FLASH in CHAP table and the index obtained is
4346 * used in the DDB.
4347 **/
4348static void qla4xxx_create_chap_list(struct scsi_qla_host *ha)
4349{
4350 int rval = 0;
4351 uint8_t *chap_flash_data = NULL;
4352 uint32_t offset;
4353 dma_addr_t chap_dma;
4354 uint32_t chap_size = 0;
4355
4356 if (is_qla40XX(ha))
4357 chap_size = MAX_CHAP_ENTRIES_40XX *
4358 sizeof(struct ql4_chap_table);
4359 else /* Single region contains CHAP info for both
4360 * ports which is divided into half for each port.
4361 */
4362 chap_size = ha->hw.flt_chap_size / 2;
4363
4364 chap_flash_data = dma_alloc_coherent(&ha->pdev->dev, chap_size,
4365 &chap_dma, GFP_KERNEL);
4366 if (!chap_flash_data) {
4367 ql4_printk(KERN_ERR, ha, "No memory for chap_flash_data\n");
4368 return;
4369 }
4370 if (is_qla40XX(ha))
4371 offset = FLASH_CHAP_OFFSET;
4372 else {
4373 offset = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_region_chap << 2);
4374 if (ha->port_num == 1)
4375 offset += chap_size;
4376 }
4377
4378 rval = qla4xxx_get_flash(ha, chap_dma, offset, chap_size);
4379 if (rval != QLA_SUCCESS)
4380 goto exit_chap_list;
4381
4382 if (ha->chap_list == NULL)
4383 ha->chap_list = vmalloc(chap_size);
4384 if (ha->chap_list == NULL) {
4385 ql4_printk(KERN_ERR, ha, "No memory for ha->chap_list\n");
4386 goto exit_chap_list;
4387 }
4388
4389 memcpy(ha->chap_list, chap_flash_data, chap_size);
4390
4391exit_chap_list:
4392 dma_free_coherent(&ha->pdev->dev, chap_size,
4393 chap_flash_data, chap_dma);
Lalit Chandivade45494152011-10-07 16:55:42 -07004394}
4395
Mike Christie13483732011-12-01 21:38:41 -06004396static void qla4xxx_get_param_ddb(struct ddb_entry *ddb_entry,
4397 struct ql4_tuple_ddb *tddb)
4398{
4399 struct scsi_qla_host *ha;
4400 struct iscsi_cls_session *cls_sess;
4401 struct iscsi_cls_conn *cls_conn;
4402 struct iscsi_session *sess;
4403 struct iscsi_conn *conn;
4404
4405 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
4406 ha = ddb_entry->ha;
4407 cls_sess = ddb_entry->sess;
4408 sess = cls_sess->dd_data;
4409 cls_conn = ddb_entry->conn;
4410 conn = cls_conn->dd_data;
4411
4412 tddb->tpgt = sess->tpgt;
4413 tddb->port = conn->persistent_port;
4414 strncpy(tddb->iscsi_name, sess->targetname, ISCSI_NAME_SIZE);
4415 strncpy(tddb->ip_addr, conn->persistent_address, DDB_IPADDR_LEN);
4416}
4417
4418static void qla4xxx_convert_param_ddb(struct dev_db_entry *fw_ddb_entry,
Vikas Chaudhary1cb78d72012-06-14 06:35:48 -04004419 struct ql4_tuple_ddb *tddb,
4420 uint8_t *flash_isid)
Mike Christie13483732011-12-01 21:38:41 -06004421{
4422 uint16_t options = 0;
4423
4424 tddb->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
4425 memcpy(&tddb->iscsi_name[0], &fw_ddb_entry->iscsi_name[0],
4426 min(sizeof(tddb->iscsi_name), sizeof(fw_ddb_entry->iscsi_name)));
4427
4428 options = le16_to_cpu(fw_ddb_entry->options);
4429 if (options & DDB_OPT_IPV6_DEVICE)
4430 sprintf(tddb->ip_addr, "%pI6", fw_ddb_entry->ip_addr);
4431 else
4432 sprintf(tddb->ip_addr, "%pI4", fw_ddb_entry->ip_addr);
4433
4434 tddb->port = le16_to_cpu(fw_ddb_entry->port);
Vikas Chaudhary1cb78d72012-06-14 06:35:48 -04004435
4436 if (flash_isid == NULL)
4437 memcpy(&tddb->isid[0], &fw_ddb_entry->isid[0],
4438 sizeof(tddb->isid));
4439 else
4440 memcpy(&tddb->isid[0], &flash_isid[0], sizeof(tddb->isid));
Mike Christie13483732011-12-01 21:38:41 -06004441}
4442
4443static int qla4xxx_compare_tuple_ddb(struct scsi_qla_host *ha,
4444 struct ql4_tuple_ddb *old_tddb,
Manish Rangankar173269e2012-02-27 03:08:55 -08004445 struct ql4_tuple_ddb *new_tddb,
4446 uint8_t is_isid_compare)
Mike Christie13483732011-12-01 21:38:41 -06004447{
4448 if (strcmp(old_tddb->iscsi_name, new_tddb->iscsi_name))
4449 return QLA_ERROR;
4450
4451 if (strcmp(old_tddb->ip_addr, new_tddb->ip_addr))
4452 return QLA_ERROR;
4453
4454 if (old_tddb->port != new_tddb->port)
4455 return QLA_ERROR;
4456
Manish Rangankar173269e2012-02-27 03:08:55 -08004457 /* For multi sessions, driver generates the ISID, so do not compare
Masanari Iida59e13d42012-04-25 00:24:16 +09004458 * ISID in reset path since it would be a comparison between the
Manish Rangankar173269e2012-02-27 03:08:55 -08004459 * driver generated ISID and firmware generated ISID. This could
4460 * lead to adding duplicated DDBs in the list as driver generated
4461 * ISID would not match firmware generated ISID.
4462 */
4463 if (is_isid_compare) {
4464 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: old ISID [%02x%02x%02x"
4465 "%02x%02x%02x] New ISID [%02x%02x%02x%02x%02x%02x]\n",
4466 __func__, old_tddb->isid[5], old_tddb->isid[4],
4467 old_tddb->isid[3], old_tddb->isid[2], old_tddb->isid[1],
4468 old_tddb->isid[0], new_tddb->isid[5], new_tddb->isid[4],
4469 new_tddb->isid[3], new_tddb->isid[2], new_tddb->isid[1],
4470 new_tddb->isid[0]));
4471
4472 if (memcmp(&old_tddb->isid[0], &new_tddb->isid[0],
4473 sizeof(old_tddb->isid)))
4474 return QLA_ERROR;
4475 }
4476
Mike Christie13483732011-12-01 21:38:41 -06004477 DEBUG2(ql4_printk(KERN_INFO, ha,
4478 "Match Found, fw[%d,%d,%s,%s], [%d,%d,%s,%s]",
4479 old_tddb->port, old_tddb->tpgt, old_tddb->ip_addr,
4480 old_tddb->iscsi_name, new_tddb->port, new_tddb->tpgt,
4481 new_tddb->ip_addr, new_tddb->iscsi_name));
4482
4483 return QLA_SUCCESS;
4484}
4485
4486static int qla4xxx_is_session_exists(struct scsi_qla_host *ha,
4487 struct dev_db_entry *fw_ddb_entry)
4488{
4489 struct ddb_entry *ddb_entry;
4490 struct ql4_tuple_ddb *fw_tddb = NULL;
4491 struct ql4_tuple_ddb *tmp_tddb = NULL;
4492 int idx;
4493 int ret = QLA_ERROR;
4494
4495 fw_tddb = vzalloc(sizeof(*fw_tddb));
4496 if (!fw_tddb) {
4497 DEBUG2(ql4_printk(KERN_WARNING, ha,
4498 "Memory Allocation failed.\n"));
4499 ret = QLA_SUCCESS;
4500 goto exit_check;
4501 }
4502
4503 tmp_tddb = vzalloc(sizeof(*tmp_tddb));
4504 if (!tmp_tddb) {
4505 DEBUG2(ql4_printk(KERN_WARNING, ha,
4506 "Memory Allocation failed.\n"));
4507 ret = QLA_SUCCESS;
4508 goto exit_check;
4509 }
4510
Vikas Chaudhary1cb78d72012-06-14 06:35:48 -04004511 qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb, NULL);
Mike Christie13483732011-12-01 21:38:41 -06004512
4513 for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
4514 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
4515 if (ddb_entry == NULL)
4516 continue;
4517
4518 qla4xxx_get_param_ddb(ddb_entry, tmp_tddb);
Manish Rangankar173269e2012-02-27 03:08:55 -08004519 if (!qla4xxx_compare_tuple_ddb(ha, fw_tddb, tmp_tddb, false)) {
Mike Christie13483732011-12-01 21:38:41 -06004520 ret = QLA_SUCCESS; /* found */
4521 goto exit_check;
4522 }
4523 }
4524
4525exit_check:
4526 if (fw_tddb)
4527 vfree(fw_tddb);
4528 if (tmp_tddb)
4529 vfree(tmp_tddb);
4530 return ret;
4531}
4532
Vikas Chaudhary1cb78d72012-06-14 06:35:48 -04004533/**
4534 * qla4xxx_check_existing_isid - check if target with same isid exist
4535 * in target list
4536 * @list_nt: list of target
4537 * @isid: isid to check
4538 *
4539 * This routine return QLA_SUCCESS if target with same isid exist
4540 **/
4541static int qla4xxx_check_existing_isid(struct list_head *list_nt, uint8_t *isid)
4542{
4543 struct qla_ddb_index *nt_ddb_idx, *nt_ddb_idx_tmp;
4544 struct dev_db_entry *fw_ddb_entry;
4545
4546 list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
4547 fw_ddb_entry = &nt_ddb_idx->fw_ddb;
4548
4549 if (memcmp(&fw_ddb_entry->isid[0], &isid[0],
4550 sizeof(nt_ddb_idx->fw_ddb.isid)) == 0) {
4551 return QLA_SUCCESS;
4552 }
4553 }
4554 return QLA_ERROR;
4555}
4556
4557/**
4558 * qla4xxx_update_isid - compare ddbs and updated isid
4559 * @ha: Pointer to host adapter structure.
4560 * @list_nt: list of nt target
4561 * @fw_ddb_entry: firmware ddb entry
4562 *
4563 * This routine update isid if ddbs have same iqn, same isid and
4564 * different IP addr.
4565 * Return QLA_SUCCESS if isid is updated.
4566 **/
4567static int qla4xxx_update_isid(struct scsi_qla_host *ha,
4568 struct list_head *list_nt,
4569 struct dev_db_entry *fw_ddb_entry)
4570{
4571 uint8_t base_value, i;
4572
4573 base_value = fw_ddb_entry->isid[1] & 0x1f;
4574 for (i = 0; i < 8; i++) {
4575 fw_ddb_entry->isid[1] = (base_value | (i << 5));
4576 if (qla4xxx_check_existing_isid(list_nt, fw_ddb_entry->isid))
4577 break;
4578 }
4579
4580 if (!qla4xxx_check_existing_isid(list_nt, fw_ddb_entry->isid))
4581 return QLA_ERROR;
4582
4583 return QLA_SUCCESS;
4584}
4585
4586/**
4587 * qla4xxx_should_update_isid - check if isid need to update
4588 * @ha: Pointer to host adapter structure.
4589 * @old_tddb: ddb tuple
4590 * @new_tddb: ddb tuple
4591 *
4592 * Return QLA_SUCCESS if different IP, different PORT, same iqn,
4593 * same isid
4594 **/
4595static int qla4xxx_should_update_isid(struct scsi_qla_host *ha,
4596 struct ql4_tuple_ddb *old_tddb,
4597 struct ql4_tuple_ddb *new_tddb)
4598{
4599 if (strcmp(old_tddb->ip_addr, new_tddb->ip_addr) == 0) {
4600 /* Same ip */
4601 if (old_tddb->port == new_tddb->port)
4602 return QLA_ERROR;
4603 }
4604
4605 if (strcmp(old_tddb->iscsi_name, new_tddb->iscsi_name))
4606 /* different iqn */
4607 return QLA_ERROR;
4608
4609 if (memcmp(&old_tddb->isid[0], &new_tddb->isid[0],
4610 sizeof(old_tddb->isid)))
4611 /* different isid */
4612 return QLA_ERROR;
4613
4614 return QLA_SUCCESS;
4615}
4616
4617/**
4618 * qla4xxx_is_flash_ddb_exists - check if fw_ddb_entry already exists in list_nt
4619 * @ha: Pointer to host adapter structure.
4620 * @list_nt: list of nt target.
4621 * @fw_ddb_entry: firmware ddb entry.
4622 *
4623 * This routine check if fw_ddb_entry already exists in list_nt to avoid
4624 * duplicate ddb in list_nt.
4625 * Return QLA_SUCCESS if duplicate ddb exit in list_nl.
4626 * Note: This function also update isid of DDB if required.
4627 **/
4628
Mike Christie13483732011-12-01 21:38:41 -06004629static int qla4xxx_is_flash_ddb_exists(struct scsi_qla_host *ha,
4630 struct list_head *list_nt,
4631 struct dev_db_entry *fw_ddb_entry)
4632{
4633 struct qla_ddb_index *nt_ddb_idx, *nt_ddb_idx_tmp;
4634 struct ql4_tuple_ddb *fw_tddb = NULL;
4635 struct ql4_tuple_ddb *tmp_tddb = NULL;
Vikas Chaudhary1cb78d72012-06-14 06:35:48 -04004636 int rval, ret = QLA_ERROR;
Mike Christie13483732011-12-01 21:38:41 -06004637
4638 fw_tddb = vzalloc(sizeof(*fw_tddb));
4639 if (!fw_tddb) {
4640 DEBUG2(ql4_printk(KERN_WARNING, ha,
4641 "Memory Allocation failed.\n"));
4642 ret = QLA_SUCCESS;
4643 goto exit_check;
4644 }
4645
4646 tmp_tddb = vzalloc(sizeof(*tmp_tddb));
4647 if (!tmp_tddb) {
4648 DEBUG2(ql4_printk(KERN_WARNING, ha,
4649 "Memory Allocation failed.\n"));
4650 ret = QLA_SUCCESS;
4651 goto exit_check;
4652 }
4653
Vikas Chaudhary1cb78d72012-06-14 06:35:48 -04004654 qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb, NULL);
Mike Christie13483732011-12-01 21:38:41 -06004655
4656 list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
Vikas Chaudhary1cb78d72012-06-14 06:35:48 -04004657 qla4xxx_convert_param_ddb(&nt_ddb_idx->fw_ddb, tmp_tddb,
4658 nt_ddb_idx->flash_isid);
4659 ret = qla4xxx_compare_tuple_ddb(ha, fw_tddb, tmp_tddb, true);
4660 /* found duplicate ddb */
4661 if (ret == QLA_SUCCESS)
4662 goto exit_check;
4663 }
4664
4665 list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
4666 qla4xxx_convert_param_ddb(&nt_ddb_idx->fw_ddb, tmp_tddb, NULL);
4667
4668 ret = qla4xxx_should_update_isid(ha, tmp_tddb, fw_tddb);
4669 if (ret == QLA_SUCCESS) {
4670 rval = qla4xxx_update_isid(ha, list_nt, fw_ddb_entry);
4671 if (rval == QLA_SUCCESS)
4672 ret = QLA_ERROR;
4673 else
4674 ret = QLA_SUCCESS;
4675
Mike Christie13483732011-12-01 21:38:41 -06004676 goto exit_check;
4677 }
4678 }
4679
4680exit_check:
4681 if (fw_tddb)
4682 vfree(fw_tddb);
4683 if (tmp_tddb)
4684 vfree(tmp_tddb);
4685 return ret;
4686}
4687
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004688static void qla4xxx_free_ddb_list(struct list_head *list_ddb)
Mike Christie13483732011-12-01 21:38:41 -06004689{
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004690 struct qla_ddb_index *ddb_idx, *ddb_idx_tmp;
Mike Christie13483732011-12-01 21:38:41 -06004691
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004692 list_for_each_entry_safe(ddb_idx, ddb_idx_tmp, list_ddb, list) {
4693 list_del_init(&ddb_idx->list);
4694 vfree(ddb_idx);
Mike Christie13483732011-12-01 21:38:41 -06004695 }
Mike Christie13483732011-12-01 21:38:41 -06004696}
4697
4698static struct iscsi_endpoint *qla4xxx_get_ep_fwdb(struct scsi_qla_host *ha,
4699 struct dev_db_entry *fw_ddb_entry)
4700{
4701 struct iscsi_endpoint *ep;
4702 struct sockaddr_in *addr;
4703 struct sockaddr_in6 *addr6;
Manish Rangankar3dd48492012-11-23 06:58:40 -05004704 struct sockaddr *t_addr;
4705 struct sockaddr_storage *dst_addr;
Mike Christie13483732011-12-01 21:38:41 -06004706 char *ip;
4707
4708 /* TODO: need to destroy on unload iscsi_endpoint*/
4709 dst_addr = vmalloc(sizeof(*dst_addr));
4710 if (!dst_addr)
4711 return NULL;
4712
4713 if (fw_ddb_entry->options & DDB_OPT_IPV6_DEVICE) {
Manish Rangankar3dd48492012-11-23 06:58:40 -05004714 t_addr = (struct sockaddr *)dst_addr;
4715 t_addr->sa_family = AF_INET6;
Mike Christie13483732011-12-01 21:38:41 -06004716 addr6 = (struct sockaddr_in6 *)dst_addr;
4717 ip = (char *)&addr6->sin6_addr;
4718 memcpy(ip, fw_ddb_entry->ip_addr, IPv6_ADDR_LEN);
4719 addr6->sin6_port = htons(le16_to_cpu(fw_ddb_entry->port));
4720
4721 } else {
Manish Rangankar3dd48492012-11-23 06:58:40 -05004722 t_addr = (struct sockaddr *)dst_addr;
4723 t_addr->sa_family = AF_INET;
Mike Christie13483732011-12-01 21:38:41 -06004724 addr = (struct sockaddr_in *)dst_addr;
4725 ip = (char *)&addr->sin_addr;
4726 memcpy(ip, fw_ddb_entry->ip_addr, IP_ADDR_LEN);
4727 addr->sin_port = htons(le16_to_cpu(fw_ddb_entry->port));
4728 }
4729
Manish Rangankar3dd48492012-11-23 06:58:40 -05004730 ep = qla4xxx_ep_connect(ha->host, (struct sockaddr *)dst_addr, 0);
Mike Christie13483732011-12-01 21:38:41 -06004731 vfree(dst_addr);
4732 return ep;
4733}
4734
4735static int qla4xxx_verify_boot_idx(struct scsi_qla_host *ha, uint16_t idx)
4736{
4737 if (ql4xdisablesysfsboot)
4738 return QLA_SUCCESS;
4739 if (idx == ha->pri_ddb_idx || idx == ha->sec_ddb_idx)
4740 return QLA_ERROR;
4741 return QLA_SUCCESS;
4742}
4743
4744static void qla4xxx_setup_flash_ddb_entry(struct scsi_qla_host *ha,
Manish Rangankar1dc8ed52013-01-20 23:51:03 -05004745 struct ddb_entry *ddb_entry,
4746 uint16_t idx)
Mike Christie13483732011-12-01 21:38:41 -06004747{
Nilesh Javalic28eaac2011-12-18 21:40:44 -08004748 uint16_t def_timeout;
4749
Mike Christie13483732011-12-01 21:38:41 -06004750 ddb_entry->ddb_type = FLASH_DDB;
4751 ddb_entry->fw_ddb_index = INVALID_ENTRY;
4752 ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
4753 ddb_entry->ha = ha;
4754 ddb_entry->unblock_sess = qla4xxx_unblock_flash_ddb;
4755 ddb_entry->ddb_change = qla4xxx_flash_ddb_change;
4756
4757 atomic_set(&ddb_entry->retry_relogin_timer, INVALID_ENTRY);
4758 atomic_set(&ddb_entry->relogin_timer, 0);
4759 atomic_set(&ddb_entry->relogin_retry_count, 0);
Nilesh Javalic28eaac2011-12-18 21:40:44 -08004760 def_timeout = le16_to_cpu(ddb_entry->fw_ddb_entry.def_timeout);
Mike Christie13483732011-12-01 21:38:41 -06004761 ddb_entry->default_relogin_timeout =
Nilesh Javalic28eaac2011-12-18 21:40:44 -08004762 (def_timeout > LOGIN_TOV) && (def_timeout < LOGIN_TOV * 10) ?
4763 def_timeout : LOGIN_TOV;
Mike Christie13483732011-12-01 21:38:41 -06004764 ddb_entry->default_time2wait =
4765 le16_to_cpu(ddb_entry->fw_ddb_entry.iscsi_def_time2wait);
Manish Rangankar1dc8ed52013-01-20 23:51:03 -05004766
4767 if (ql4xdisablesysfsboot &&
4768 (idx == ha->pri_ddb_idx || idx == ha->sec_ddb_idx))
4769 set_bit(DF_BOOT_TGT, &ddb_entry->flags);
Mike Christie13483732011-12-01 21:38:41 -06004770}
4771
4772static void qla4xxx_wait_for_ip_configuration(struct scsi_qla_host *ha)
4773{
4774 uint32_t idx = 0;
4775 uint32_t ip_idx[IP_ADDR_COUNT] = {0, 1, 2, 3}; /* 4 IP interfaces */
4776 uint32_t sts[MBOX_REG_COUNT];
4777 uint32_t ip_state;
4778 unsigned long wtime;
4779 int ret;
4780
4781 wtime = jiffies + (HZ * IP_CONFIG_TOV);
4782 do {
4783 for (idx = 0; idx < IP_ADDR_COUNT; idx++) {
4784 if (ip_idx[idx] == -1)
4785 continue;
4786
4787 ret = qla4xxx_get_ip_state(ha, 0, ip_idx[idx], sts);
4788
4789 if (ret == QLA_ERROR) {
4790 ip_idx[idx] = -1;
4791 continue;
4792 }
4793
4794 ip_state = (sts[1] & IP_STATE_MASK) >> IP_STATE_SHIFT;
4795
4796 DEBUG2(ql4_printk(KERN_INFO, ha,
4797 "Waiting for IP state for idx = %d, state = 0x%x\n",
4798 ip_idx[idx], ip_state));
4799 if (ip_state == IP_ADDRSTATE_UNCONFIGURED ||
4800 ip_state == IP_ADDRSTATE_INVALID ||
4801 ip_state == IP_ADDRSTATE_PREFERRED ||
4802 ip_state == IP_ADDRSTATE_DEPRICATED ||
4803 ip_state == IP_ADDRSTATE_DISABLING)
4804 ip_idx[idx] = -1;
Mike Christie13483732011-12-01 21:38:41 -06004805 }
4806
4807 /* Break if all IP states checked */
4808 if ((ip_idx[0] == -1) &&
4809 (ip_idx[1] == -1) &&
4810 (ip_idx[2] == -1) &&
4811 (ip_idx[3] == -1))
4812 break;
4813 schedule_timeout_uninterruptible(HZ);
4814 } while (time_after(wtime, jiffies));
4815}
4816
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004817static void qla4xxx_build_st_list(struct scsi_qla_host *ha,
4818 struct list_head *list_st)
Mike Christie13483732011-12-01 21:38:41 -06004819{
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004820 struct qla_ddb_index *st_ddb_idx;
Mike Christie13483732011-12-01 21:38:41 -06004821 int max_ddbs;
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004822 int fw_idx_size;
4823 struct dev_db_entry *fw_ddb_entry;
4824 dma_addr_t fw_ddb_dma;
Mike Christie13483732011-12-01 21:38:41 -06004825 int ret;
4826 uint32_t idx = 0, next_idx = 0;
4827 uint32_t state = 0, conn_err = 0;
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004828 uint16_t conn_id = 0;
Mike Christie13483732011-12-01 21:38:41 -06004829
4830 fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
4831 &fw_ddb_dma);
4832 if (fw_ddb_entry == NULL) {
4833 DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004834 goto exit_st_list;
Mike Christie13483732011-12-01 21:38:41 -06004835 }
4836
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004837 max_ddbs = is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
4838 MAX_DEV_DB_ENTRIES;
Mike Christie13483732011-12-01 21:38:41 -06004839 fw_idx_size = sizeof(struct qla_ddb_index);
4840
4841 for (idx = 0; idx < max_ddbs; idx = next_idx) {
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004842 ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry, fw_ddb_dma,
4843 NULL, &next_idx, &state,
4844 &conn_err, NULL, &conn_id);
Mike Christie13483732011-12-01 21:38:41 -06004845 if (ret == QLA_ERROR)
4846 break;
4847
Lalit Chandivade981c9822012-02-13 18:30:41 +05304848 /* Ignore DDB if invalid state (unassigned) */
4849 if (state == DDB_DS_UNASSIGNED)
4850 goto continue_next_st;
4851
Mike Christie13483732011-12-01 21:38:41 -06004852 /* Check if ST, add to the list_st */
4853 if (strlen((char *) fw_ddb_entry->iscsi_name) != 0)
4854 goto continue_next_st;
4855
4856 st_ddb_idx = vzalloc(fw_idx_size);
4857 if (!st_ddb_idx)
4858 break;
4859
4860 st_ddb_idx->fw_ddb_idx = idx;
4861
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004862 list_add_tail(&st_ddb_idx->list, list_st);
Mike Christie13483732011-12-01 21:38:41 -06004863continue_next_st:
4864 if (next_idx == 0)
4865 break;
4866 }
4867
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004868exit_st_list:
4869 if (fw_ddb_entry)
4870 dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
4871}
4872
4873/**
4874 * qla4xxx_remove_failed_ddb - Remove inactive or failed ddb from list
4875 * @ha: pointer to adapter structure
4876 * @list_ddb: List from which failed ddb to be removed
4877 *
4878 * Iterate over the list of DDBs and find and remove DDBs that are either in
4879 * no connection active state or failed state
4880 **/
4881static void qla4xxx_remove_failed_ddb(struct scsi_qla_host *ha,
4882 struct list_head *list_ddb)
4883{
4884 struct qla_ddb_index *ddb_idx, *ddb_idx_tmp;
4885 uint32_t next_idx = 0;
4886 uint32_t state = 0, conn_err = 0;
4887 int ret;
4888
4889 list_for_each_entry_safe(ddb_idx, ddb_idx_tmp, list_ddb, list) {
4890 ret = qla4xxx_get_fwddb_entry(ha, ddb_idx->fw_ddb_idx,
4891 NULL, 0, NULL, &next_idx, &state,
4892 &conn_err, NULL, NULL);
4893 if (ret == QLA_ERROR)
4894 continue;
4895
4896 if (state == DDB_DS_NO_CONNECTION_ACTIVE ||
4897 state == DDB_DS_SESSION_FAILED) {
4898 list_del_init(&ddb_idx->list);
4899 vfree(ddb_idx);
4900 }
4901 }
4902}
4903
4904static int qla4xxx_sess_conn_setup(struct scsi_qla_host *ha,
4905 struct dev_db_entry *fw_ddb_entry,
Manish Rangankar1dc8ed52013-01-20 23:51:03 -05004906 int is_reset, uint16_t idx)
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004907{
4908 struct iscsi_cls_session *cls_sess;
4909 struct iscsi_session *sess;
4910 struct iscsi_cls_conn *cls_conn;
4911 struct iscsi_endpoint *ep;
4912 uint16_t cmds_max = 32;
4913 uint16_t conn_id = 0;
4914 uint32_t initial_cmdsn = 0;
4915 int ret = QLA_SUCCESS;
4916
4917 struct ddb_entry *ddb_entry = NULL;
4918
4919 /* Create session object, with INVALID_ENTRY,
4920 * the targer_id would get set when we issue the login
4921 */
4922 cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport, ha->host,
4923 cmds_max, sizeof(struct ddb_entry),
4924 sizeof(struct ql4_task_data),
4925 initial_cmdsn, INVALID_ENTRY);
4926 if (!cls_sess) {
4927 ret = QLA_ERROR;
4928 goto exit_setup;
4929 }
4930
4931 /*
4932 * so calling module_put function to decrement the
4933 * reference count.
4934 **/
4935 module_put(qla4xxx_iscsi_transport.owner);
4936 sess = cls_sess->dd_data;
4937 ddb_entry = sess->dd_data;
4938 ddb_entry->sess = cls_sess;
4939
4940 cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
4941 memcpy(&ddb_entry->fw_ddb_entry, fw_ddb_entry,
4942 sizeof(struct dev_db_entry));
4943
Manish Rangankar1dc8ed52013-01-20 23:51:03 -05004944 qla4xxx_setup_flash_ddb_entry(ha, ddb_entry, idx);
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08004945
4946 cls_conn = iscsi_conn_setup(cls_sess, sizeof(struct qla_conn), conn_id);
4947
4948 if (!cls_conn) {
4949 ret = QLA_ERROR;
4950 goto exit_setup;
4951 }
4952
4953 ddb_entry->conn = cls_conn;
4954
4955 /* Setup ep, for displaying attributes in sysfs */
4956 ep = qla4xxx_get_ep_fwdb(ha, fw_ddb_entry);
4957 if (ep) {
4958 ep->conn = cls_conn;
4959 cls_conn->ep = ep;
4960 } else {
4961 DEBUG2(ql4_printk(KERN_ERR, ha, "Unable to get ep\n"));
4962 ret = QLA_ERROR;
4963 goto exit_setup;
4964 }
4965
4966 /* Update sess/conn params */
4967 qla4xxx_copy_fwddb_param(ha, fw_ddb_entry, cls_sess, cls_conn);
4968
4969 if (is_reset == RESET_ADAPTER) {
4970 iscsi_block_session(cls_sess);
4971 /* Use the relogin path to discover new devices
4972 * by short-circuting the logic of setting
4973 * timer to relogin - instead set the flags
4974 * to initiate login right away.
4975 */
4976 set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
4977 set_bit(DF_RELOGIN, &ddb_entry->flags);
4978 }
4979
4980exit_setup:
4981 return ret;
4982}
4983
4984static void qla4xxx_build_nt_list(struct scsi_qla_host *ha,
4985 struct list_head *list_nt, int is_reset)
4986{
4987 struct dev_db_entry *fw_ddb_entry;
4988 dma_addr_t fw_ddb_dma;
4989 int max_ddbs;
4990 int fw_idx_size;
4991 int ret;
4992 uint32_t idx = 0, next_idx = 0;
4993 uint32_t state = 0, conn_err = 0;
4994 uint16_t conn_id = 0;
4995 struct qla_ddb_index *nt_ddb_idx;
4996
4997 fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
4998 &fw_ddb_dma);
4999 if (fw_ddb_entry == NULL) {
5000 DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
5001 goto exit_nt_list;
5002 }
5003 max_ddbs = is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
5004 MAX_DEV_DB_ENTRIES;
5005 fw_idx_size = sizeof(struct qla_ddb_index);
5006
5007 for (idx = 0; idx < max_ddbs; idx = next_idx) {
5008 ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry, fw_ddb_dma,
5009 NULL, &next_idx, &state,
5010 &conn_err, NULL, &conn_id);
5011 if (ret == QLA_ERROR)
5012 break;
5013
5014 if (qla4xxx_verify_boot_idx(ha, idx) != QLA_SUCCESS)
5015 goto continue_next_nt;
5016
5017 /* Check if NT, then add to list it */
5018 if (strlen((char *) fw_ddb_entry->iscsi_name) == 0)
5019 goto continue_next_nt;
5020
5021 if (!(state == DDB_DS_NO_CONNECTION_ACTIVE ||
5022 state == DDB_DS_SESSION_FAILED))
5023 goto continue_next_nt;
5024
5025 DEBUG2(ql4_printk(KERN_INFO, ha,
5026 "Adding DDB to session = 0x%x\n", idx));
5027 if (is_reset == INIT_ADAPTER) {
5028 nt_ddb_idx = vmalloc(fw_idx_size);
5029 if (!nt_ddb_idx)
5030 break;
5031
5032 nt_ddb_idx->fw_ddb_idx = idx;
5033
Vikas Chaudhary1cb78d72012-06-14 06:35:48 -04005034 /* Copy original isid as it may get updated in function
5035 * qla4xxx_update_isid(). We need original isid in
5036 * function qla4xxx_compare_tuple_ddb to find duplicate
5037 * target */
5038 memcpy(&nt_ddb_idx->flash_isid[0],
5039 &fw_ddb_entry->isid[0],
5040 sizeof(nt_ddb_idx->flash_isid));
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08005041
Vikas Chaudhary1cb78d72012-06-14 06:35:48 -04005042 ret = qla4xxx_is_flash_ddb_exists(ha, list_nt,
5043 fw_ddb_entry);
5044 if (ret == QLA_SUCCESS) {
5045 /* free nt_ddb_idx and do not add to list_nt */
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08005046 vfree(nt_ddb_idx);
5047 goto continue_next_nt;
5048 }
Vikas Chaudhary1cb78d72012-06-14 06:35:48 -04005049
5050 /* Copy updated isid */
5051 memcpy(&nt_ddb_idx->fw_ddb, fw_ddb_entry,
5052 sizeof(struct dev_db_entry));
5053
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08005054 list_add_tail(&nt_ddb_idx->list, list_nt);
5055 } else if (is_reset == RESET_ADAPTER) {
5056 if (qla4xxx_is_session_exists(ha, fw_ddb_entry) ==
5057 QLA_SUCCESS)
5058 goto continue_next_nt;
5059 }
5060
Manish Rangankar1dc8ed52013-01-20 23:51:03 -05005061 ret = qla4xxx_sess_conn_setup(ha, fw_ddb_entry, is_reset, idx);
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08005062 if (ret == QLA_ERROR)
5063 goto exit_nt_list;
5064
5065continue_next_nt:
5066 if (next_idx == 0)
5067 break;
5068 }
5069
5070exit_nt_list:
5071 if (fw_ddb_entry)
5072 dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
5073}
5074
5075/**
5076 * qla4xxx_build_ddb_list - Build ddb list and setup sessions
5077 * @ha: pointer to adapter structure
5078 * @is_reset: Is this init path or reset path
5079 *
5080 * Create a list of sendtargets (st) from firmware DDBs, issue send targets
5081 * using connection open, then create the list of normal targets (nt)
5082 * from firmware DDBs. Based on the list of nt setup session and connection
5083 * objects.
5084 **/
5085void qla4xxx_build_ddb_list(struct scsi_qla_host *ha, int is_reset)
5086{
5087 uint16_t tmo = 0;
5088 struct list_head list_st, list_nt;
5089 struct qla_ddb_index *st_ddb_idx, *st_ddb_idx_tmp;
5090 unsigned long wtime;
5091
5092 if (!test_bit(AF_LINK_UP, &ha->flags)) {
5093 set_bit(AF_BUILD_DDB_LIST, &ha->flags);
5094 ha->is_reset = is_reset;
5095 return;
5096 }
5097
5098 INIT_LIST_HEAD(&list_st);
5099 INIT_LIST_HEAD(&list_nt);
5100
5101 qla4xxx_build_st_list(ha, &list_st);
5102
Mike Christie13483732011-12-01 21:38:41 -06005103 /* Before issuing conn open mbox, ensure all IPs states are configured
5104 * Note, conn open fails if IPs are not configured
5105 */
5106 qla4xxx_wait_for_ip_configuration(ha);
5107
5108 /* Go thru the STs and fire the sendtargets by issuing conn open mbx */
5109 list_for_each_entry_safe(st_ddb_idx, st_ddb_idx_tmp, &list_st, list) {
5110 qla4xxx_conn_open(ha, st_ddb_idx->fw_ddb_idx);
5111 }
5112
5113 /* Wait to ensure all sendtargets are done for min 12 sec wait */
Nilesh Javalic28eaac2011-12-18 21:40:44 -08005114 tmo = ((ha->def_timeout > LOGIN_TOV) &&
5115 (ha->def_timeout < LOGIN_TOV * 10) ?
5116 ha->def_timeout : LOGIN_TOV);
5117
Mike Christie13483732011-12-01 21:38:41 -06005118 DEBUG2(ql4_printk(KERN_INFO, ha,
5119 "Default time to wait for build ddb %d\n", tmo));
5120
5121 wtime = jiffies + (HZ * tmo);
5122 do {
Nilesh Javalif1f2e602011-12-16 01:58:57 -08005123 if (list_empty(&list_st))
5124 break;
5125
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08005126 qla4xxx_remove_failed_ddb(ha, &list_st);
Mike Christie13483732011-12-01 21:38:41 -06005127 schedule_timeout_uninterruptible(HZ / 10);
5128 } while (time_after(wtime, jiffies));
5129
5130 /* Free up the sendtargets list */
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08005131 qla4xxx_free_ddb_list(&list_st);
Mike Christie13483732011-12-01 21:38:41 -06005132
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08005133 qla4xxx_build_nt_list(ha, &list_nt, is_reset);
Mike Christie13483732011-12-01 21:38:41 -06005134
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08005135 qla4xxx_free_ddb_list(&list_nt);
Mike Christie13483732011-12-01 21:38:41 -06005136
5137 qla4xxx_free_ddb_index(ha);
5138}
5139
David Somayajuluafaf5a22006-09-19 10:28:00 -07005140/**
Manish Rangankar1dc8ed52013-01-20 23:51:03 -05005141 * qla4xxx_wait_login_resp_boot_tgt - Wait for iSCSI boot target login
5142 * response.
5143 * @ha: pointer to adapter structure
5144 *
5145 * When the boot entry is normal iSCSI target then DF_BOOT_TGT flag will be
5146 * set in DDB and we will wait for login response of boot targets during
5147 * probe.
5148 **/
5149static void qla4xxx_wait_login_resp_boot_tgt(struct scsi_qla_host *ha)
5150{
5151 struct ddb_entry *ddb_entry;
5152 struct dev_db_entry *fw_ddb_entry = NULL;
5153 dma_addr_t fw_ddb_entry_dma;
5154 unsigned long wtime;
5155 uint32_t ddb_state;
5156 int max_ddbs, idx, ret;
5157
5158 max_ddbs = is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
5159 MAX_DEV_DB_ENTRIES;
5160
5161 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
5162 &fw_ddb_entry_dma, GFP_KERNEL);
5163 if (!fw_ddb_entry) {
5164 ql4_printk(KERN_ERR, ha,
5165 "%s: Unable to allocate dma buffer\n", __func__);
5166 goto exit_login_resp;
5167 }
5168
5169 wtime = jiffies + (HZ * BOOT_LOGIN_RESP_TOV);
5170
5171 for (idx = 0; idx < max_ddbs; idx++) {
5172 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
5173 if (ddb_entry == NULL)
5174 continue;
5175
5176 if (test_bit(DF_BOOT_TGT, &ddb_entry->flags)) {
5177 DEBUG2(ql4_printk(KERN_INFO, ha,
5178 "%s: DDB index [%d]\n", __func__,
5179 ddb_entry->fw_ddb_index));
5180 do {
5181 ret = qla4xxx_get_fwddb_entry(ha,
5182 ddb_entry->fw_ddb_index,
5183 fw_ddb_entry, fw_ddb_entry_dma,
5184 NULL, NULL, &ddb_state, NULL,
5185 NULL, NULL);
5186 if (ret == QLA_ERROR)
5187 goto exit_login_resp;
5188
5189 if ((ddb_state == DDB_DS_SESSION_ACTIVE) ||
5190 (ddb_state == DDB_DS_SESSION_FAILED))
5191 break;
5192
5193 schedule_timeout_uninterruptible(HZ);
5194
5195 } while ((time_after(wtime, jiffies)));
5196
5197 if (!time_after(wtime, jiffies)) {
5198 DEBUG2(ql4_printk(KERN_INFO, ha,
5199 "%s: Login response wait timer expired\n",
5200 __func__));
5201 goto exit_login_resp;
5202 }
5203 }
5204 }
5205
5206exit_login_resp:
5207 if (fw_ddb_entry)
5208 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
5209 fw_ddb_entry, fw_ddb_entry_dma);
5210}
5211
5212/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07005213 * qla4xxx_probe_adapter - callback function to probe HBA
5214 * @pdev: pointer to pci_dev structure
5215 * @pci_device_id: pointer to pci_device entry
5216 *
5217 * This routine will probe for Qlogic 4xxx iSCSI host adapters.
5218 * It returns zero if successful. It also initializes all data necessary for
5219 * the driver.
5220 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005221static int qla4xxx_probe_adapter(struct pci_dev *pdev,
5222 const struct pci_device_id *ent)
David Somayajuluafaf5a22006-09-19 10:28:00 -07005223{
5224 int ret = -ENODEV, status;
5225 struct Scsi_Host *host;
5226 struct scsi_qla_host *ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -07005227 uint8_t init_retry_count = 0;
5228 char buf[34];
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05305229 struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
Prasanna Mumbaif9880e72011-03-21 03:34:26 -07005230 uint32_t dev_state;
David Somayajuluafaf5a22006-09-19 10:28:00 -07005231
5232 if (pci_enable_device(pdev))
5233 return -1;
5234
Manish Rangankarb3a271a2011-07-25 13:48:53 -05005235 host = iscsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha), 0);
David Somayajuluafaf5a22006-09-19 10:28:00 -07005236 if (host == NULL) {
5237 printk(KERN_WARNING
5238 "qla4xxx: Couldn't allocate host from scsi layer!\n");
5239 goto probe_disable_device;
5240 }
5241
5242 /* Clear our data area */
Manish Rangankarb3a271a2011-07-25 13:48:53 -05005243 ha = to_qla_host(host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07005244 memset(ha, 0, sizeof(*ha));
5245
5246 /* Save the information from PCI BIOS. */
5247 ha->pdev = pdev;
5248 ha->host = host;
5249 ha->host_no = host->host_no;
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04005250 ha->func_num = PCI_FUNC(ha->pdev->devfn);
David Somayajuluafaf5a22006-09-19 10:28:00 -07005251
Lalit Chandivade2232be02010-07-30 14:38:47 +05305252 pci_enable_pcie_error_reporting(pdev);
5253
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05305254 /* Setup Runtime configurable options */
5255 if (is_qla8022(ha)) {
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -04005256 ha->isp_ops = &qla4_82xx_isp_ops;
Vikas Chaudhary33693c72012-08-22 07:55:04 -04005257 ha->reg_tbl = (uint32_t *) qla4_82xx_reg_tbl;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05305258 ha->qdr_sn_window = -1;
5259 ha->ddr_mn_window = -1;
5260 ha->curr_window = 255;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05305261 nx_legacy_intr = &legacy_intr[ha->func_num];
5262 ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
5263 ha->nx_legacy_intr.tgt_status_reg =
5264 nx_legacy_intr->tgt_status_reg;
5265 ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
5266 ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04005267 } else if (is_qla8032(ha)) {
5268 ha->isp_ops = &qla4_83xx_isp_ops;
5269 ha->reg_tbl = (uint32_t *)qla4_83xx_reg_tbl;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05305270 } else {
5271 ha->isp_ops = &qla4xxx_isp_ops;
5272 }
5273
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04005274 if (is_qla80XX(ha)) {
5275 rwlock_init(&ha->hw_lock);
5276 ha->pf_bit = ha->func_num << 16;
5277 /* Set EEH reset type to fundamental if required by hba */
Lalit Chandivade2232be02010-07-30 14:38:47 +05305278 pdev->needs_freset = 1;
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04005279 }
Lalit Chandivade2232be02010-07-30 14:38:47 +05305280
David Somayajuluafaf5a22006-09-19 10:28:00 -07005281 /* Configure PCI I/O space. */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05305282 ret = ha->isp_ops->iospace_config(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07005283 if (ret)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05305284 goto probe_failed_ioconfig;
David Somayajuluafaf5a22006-09-19 10:28:00 -07005285
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05305286 ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
David Somayajuluafaf5a22006-09-19 10:28:00 -07005287 pdev->device, pdev->irq, ha->reg);
5288
5289 qla4xxx_config_dma_addressing(ha);
5290
5291 /* Initialize lists and spinlocks. */
David Somayajuluafaf5a22006-09-19 10:28:00 -07005292 INIT_LIST_HEAD(&ha->free_srb_q);
5293
5294 mutex_init(&ha->mbox_sem);
Lalit Chandivade45494152011-10-07 16:55:42 -07005295 mutex_init(&ha->chap_sem);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05305296 init_completion(&ha->mbx_intr_comp);
Vikas Chaudhary95d31262011-08-12 02:51:29 -07005297 init_completion(&ha->disable_acb_comp);
David Somayajuluafaf5a22006-09-19 10:28:00 -07005298
5299 spin_lock_init(&ha->hardware_lock);
Vikas Chaudhary8e9157c2012-08-22 07:45:24 -04005300 spin_lock_init(&ha->work_lock);
David Somayajuluafaf5a22006-09-19 10:28:00 -07005301
Vikas Chaudharyff884432011-08-29 23:43:02 +05305302 /* Initialize work list */
5303 INIT_LIST_HEAD(&ha->work_list);
5304
David Somayajuluafaf5a22006-09-19 10:28:00 -07005305 /* Allocate dma buffers */
5306 if (qla4xxx_mem_alloc(ha)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05305307 ql4_printk(KERN_WARNING, ha,
5308 "[ERROR] Failed to allocate memory for adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07005309
5310 ret = -ENOMEM;
5311 goto probe_failed;
5312 }
5313
Manish Rangankarb3a271a2011-07-25 13:48:53 -05005314 host->cmd_per_lun = 3;
5315 host->max_channel = 0;
5316 host->max_lun = MAX_LUNS - 1;
5317 host->max_id = MAX_TARGETS;
5318 host->max_cmd_len = IOCB_MAX_CDB_LEN;
5319 host->can_queue = MAX_SRBS ;
5320 host->transportt = qla4xxx_scsi_transport;
5321
5322 ret = scsi_init_shared_tag_map(host, MAX_SRBS);
5323 if (ret) {
5324 ql4_printk(KERN_WARNING, ha,
5325 "%s: scsi_init_shared_tag_map failed\n", __func__);
5326 goto probe_failed;
5327 }
5328
5329 pci_set_drvdata(pdev, ha);
5330
5331 ret = scsi_add_host(host, &pdev->dev);
5332 if (ret)
5333 goto probe_failed;
5334
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04005335 if (is_qla80XX(ha))
5336 qla4_8xxx_get_flash_info(ha);
5337
5338 if (is_qla8032(ha)) {
5339 qla4_83xx_read_reset_template(ha);
5340 /*
5341 * NOTE: If ql4dontresethba==1, set IDC_CTRL DONTRESET_BIT0.
5342 * If DONRESET_BIT0 is set, drivers should not set dev_state
5343 * to NEED_RESET. But if NEED_RESET is set, drivers should
5344 * should honor the reset.
5345 */
5346 if (ql4xdontresethba == 1)
5347 qla4_83xx_set_idc_dontreset(ha);
5348 }
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05305349
David Somayajuluafaf5a22006-09-19 10:28:00 -07005350 /*
5351 * Initialize the Host adapter request/response queues and
5352 * firmware
5353 * NOTE: interrupts enabled upon successful completion
5354 */
Mike Christie13483732011-12-01 21:38:41 -06005355 status = qla4xxx_initialize_adapter(ha, INIT_ADAPTER);
Poornima Vonti7977f822012-09-20 07:35:07 -04005356
5357 /* Dont retry adapter initialization if IRQ allocation failed */
5358 if (!test_bit(AF_IRQ_ATTACHED, &ha->flags))
5359 goto skip_retry_init;
5360
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05305361 while ((!test_bit(AF_ONLINE, &ha->flags)) &&
5362 init_retry_count++ < MAX_INIT_RETRIES) {
Prasanna Mumbaif9880e72011-03-21 03:34:26 -07005363
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04005364 if (is_qla80XX(ha)) {
Vikas Chaudhary33693c72012-08-22 07:55:04 -04005365 ha->isp_ops->idc_lock(ha);
5366 dev_state = qla4_8xxx_rd_direct(ha,
Vikas Chaudharye951aca2012-12-29 02:24:52 -05005367 QLA8XXX_CRB_DEV_STATE);
Vikas Chaudhary33693c72012-08-22 07:55:04 -04005368 ha->isp_ops->idc_unlock(ha);
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04005369 if (dev_state == QLA8XXX_DEV_FAILED) {
Prasanna Mumbaif9880e72011-03-21 03:34:26 -07005370 ql4_printk(KERN_WARNING, ha, "%s: don't retry "
5371 "initialize adapter. H/W is in failed state\n",
5372 __func__);
5373 break;
5374 }
5375 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07005376 DEBUG2(printk("scsi: %s: retrying adapter initialization "
5377 "(%d)\n", __func__, init_retry_count));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05305378
5379 if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
5380 continue;
5381
Mike Christie13483732011-12-01 21:38:41 -06005382 status = qla4xxx_initialize_adapter(ha, INIT_ADAPTER);
David Somayajuluafaf5a22006-09-19 10:28:00 -07005383 }
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05305384
Poornima Vonti7977f822012-09-20 07:35:07 -04005385skip_retry_init:
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05305386 if (!test_bit(AF_ONLINE, &ha->flags)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05305387 ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07005388
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04005389 if ((is_qla8022(ha) && ql4xdontresethba) ||
5390 (is_qla8032(ha) && qla4_83xx_idc_dontreset(ha))) {
Lalit Chandivadefe998522010-12-02 22:12:36 -08005391 /* Put the device in failed state. */
5392 DEBUG2(printk(KERN_ERR "HW STATE: FAILED\n"));
Vikas Chaudhary33693c72012-08-22 07:55:04 -04005393 ha->isp_ops->idc_lock(ha);
5394 qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
5395 QLA8XXX_DEV_FAILED);
5396 ha->isp_ops->idc_unlock(ha);
Lalit Chandivadefe998522010-12-02 22:12:36 -08005397 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07005398 ret = -ENODEV;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05005399 goto remove_host;
David Somayajuluafaf5a22006-09-19 10:28:00 -07005400 }
5401
David Somayajuluafaf5a22006-09-19 10:28:00 -07005402 /* Startup the kernel thread for this host adapter. */
5403 DEBUG2(printk("scsi: %s: Starting kernel thread for "
5404 "qla4xxx_dpc\n", __func__));
5405 sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
5406 ha->dpc_thread = create_singlethread_workqueue(buf);
5407 if (!ha->dpc_thread) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05305408 ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07005409 ret = -ENODEV;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05005410 goto remove_host;
David Somayajuluafaf5a22006-09-19 10:28:00 -07005411 }
David Howellsc4028952006-11-22 14:57:56 +00005412 INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
David Somayajuluafaf5a22006-09-19 10:28:00 -07005413
Manish Rangankarb3a271a2011-07-25 13:48:53 -05005414 sprintf(buf, "qla4xxx_%lu_task", ha->host_no);
5415 ha->task_wq = alloc_workqueue(buf, WQ_MEM_RECLAIM, 1);
5416 if (!ha->task_wq) {
5417 ql4_printk(KERN_WARNING, ha, "Unable to start task thread!\n");
5418 ret = -ENODEV;
5419 goto remove_host;
5420 }
5421
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04005422 /*
5423 * For ISP-8XXX, request_irqs is called in qla4_8xxx_load_risc
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05305424 * (which is called indirectly by qla4xxx_initialize_adapter),
5425 * so that irqs will be registered after crbinit but before
5426 * mbx_intr_enable.
5427 */
Vikas Chaudharyee996a62012-08-22 07:55:05 -04005428 if (is_qla40XX(ha)) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05305429 ret = qla4xxx_request_irqs(ha);
5430 if (ret) {
5431 ql4_printk(KERN_WARNING, ha, "Failed to reserve "
5432 "interrupt %d already in use.\n", pdev->irq);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05005433 goto remove_host;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05305434 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07005435 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07005436
Lalit Chandivade2232be02010-07-30 14:38:47 +05305437 pci_save_state(ha->pdev);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05305438 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07005439
5440 /* Start timer thread. */
5441 qla4xxx_start_timer(ha, qla4xxx_timer, 1);
5442
5443 set_bit(AF_INIT_DONE, &ha->flags);
5444
Tej Parkash068237c82012-05-18 04:41:44 -04005445 qla4_8xxx_alloc_sysfs_attr(ha);
5446
David Somayajuluafaf5a22006-09-19 10:28:00 -07005447 printk(KERN_INFO
5448 " QLogic iSCSI HBA Driver version: %s\n"
5449 " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
5450 qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
5451 ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
5452 ha->patch_number, ha->build_number);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05005453
Manish Dusanecfb27872012-09-20 07:35:01 -04005454 /* Set the driver version */
5455 if (is_qla80XX(ha))
5456 qla4_8xxx_set_param(ha, SET_DRVR_VERSION);
5457
Manish Rangankar2a991c22011-07-25 13:48:55 -05005458 if (qla4xxx_setup_boot_info(ha))
Vikas Chaudhary3573bfb2012-02-27 03:08:57 -08005459 ql4_printk(KERN_ERR, ha,
5460 "%s: No iSCSI boot target configured\n", __func__);
Manish Rangankar2a991c22011-07-25 13:48:55 -05005461
Mike Christie13483732011-12-01 21:38:41 -06005462 /* Perform the build ddb list and login to each */
5463 qla4xxx_build_ddb_list(ha, INIT_ADAPTER);
5464 iscsi_host_for_each_session(ha->host, qla4xxx_login_flash_ddb);
Manish Rangankar1dc8ed52013-01-20 23:51:03 -05005465 qla4xxx_wait_login_resp_boot_tgt(ha);
Mike Christie13483732011-12-01 21:38:41 -06005466
5467 qla4xxx_create_chap_list(ha);
5468
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05005469 qla4xxx_create_ifaces(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07005470 return 0;
5471
Manish Rangankarb3a271a2011-07-25 13:48:53 -05005472remove_host:
5473 scsi_remove_host(ha->host);
5474
David Somayajuluafaf5a22006-09-19 10:28:00 -07005475probe_failed:
5476 qla4xxx_free_adapter(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05305477
5478probe_failed_ioconfig:
Lalit Chandivade2232be02010-07-30 14:38:47 +05305479 pci_disable_pcie_error_reporting(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07005480 scsi_host_put(ha->host);
5481
5482probe_disable_device:
5483 pci_disable_device(pdev);
5484
5485 return ret;
5486}
5487
5488/**
Karen Higgins7eece5a2011-03-21 03:34:29 -07005489 * qla4xxx_prevent_other_port_reinit - prevent other port from re-initialize
5490 * @ha: pointer to adapter structure
5491 *
5492 * Mark the other ISP-4xxx port to indicate that the driver is being removed,
5493 * so that the other port will not re-initialize while in the process of
5494 * removing the ha due to driver unload or hba hotplug.
5495 **/
5496static void qla4xxx_prevent_other_port_reinit(struct scsi_qla_host *ha)
5497{
5498 struct scsi_qla_host *other_ha = NULL;
5499 struct pci_dev *other_pdev = NULL;
5500 int fn = ISP4XXX_PCI_FN_2;
5501
5502 /*iscsi function numbers for ISP4xxx is 1 and 3*/
5503 if (PCI_FUNC(ha->pdev->devfn) & BIT_1)
5504 fn = ISP4XXX_PCI_FN_1;
5505
5506 other_pdev =
5507 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
5508 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
5509 fn));
5510
5511 /* Get other_ha if other_pdev is valid and state is enable*/
5512 if (other_pdev) {
5513 if (atomic_read(&other_pdev->enable_cnt)) {
5514 other_ha = pci_get_drvdata(other_pdev);
5515 if (other_ha) {
5516 set_bit(AF_HA_REMOVAL, &other_ha->flags);
5517 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: "
5518 "Prevent %s reinit\n", __func__,
5519 dev_name(&other_ha->pdev->dev)));
5520 }
5521 }
5522 pci_dev_put(other_pdev);
5523 }
5524}
5525
Mike Christie13483732011-12-01 21:38:41 -06005526static void qla4xxx_destroy_fw_ddb_session(struct scsi_qla_host *ha)
5527{
5528 struct ddb_entry *ddb_entry;
5529 int options;
5530 int idx;
5531
5532 for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
5533
5534 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
5535 if ((ddb_entry != NULL) &&
5536 (ddb_entry->ddb_type == FLASH_DDB)) {
5537
5538 options = LOGOUT_OPTION_CLOSE_SESSION;
5539 if (qla4xxx_session_logout_ddb(ha, ddb_entry, options)
5540 == QLA_ERROR)
5541 ql4_printk(KERN_ERR, ha, "%s: Logout failed\n",
5542 __func__);
5543
5544 qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
5545 /*
5546 * we have decremented the reference count of the driver
5547 * when we setup the session to have the driver unload
5548 * to be seamless without actually destroying the
5549 * session
5550 **/
5551 try_module_get(qla4xxx_iscsi_transport.owner);
5552 iscsi_destroy_endpoint(ddb_entry->conn->ep);
5553 qla4xxx_free_ddb(ha, ddb_entry);
5554 iscsi_session_teardown(ddb_entry->sess);
5555 }
5556 }
5557}
Karen Higgins7eece5a2011-03-21 03:34:29 -07005558/**
Masanari Iida59e13d42012-04-25 00:24:16 +09005559 * qla4xxx_remove_adapter - callback function to remove adapter.
David Somayajuluafaf5a22006-09-19 10:28:00 -07005560 * @pci_dev: PCI device pointer
5561 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005562static void qla4xxx_remove_adapter(struct pci_dev *pdev)
David Somayajuluafaf5a22006-09-19 10:28:00 -07005563{
5564 struct scsi_qla_host *ha;
5565
Vikas Chaudharyf8b07512012-09-20 07:35:08 -04005566 /*
5567 * If the PCI device is disabled then it means probe_adapter had
5568 * failed and resources already cleaned up on probe_adapter exit.
5569 */
5570 if (!pci_is_enabled(pdev))
5571 return;
5572
David Somayajuluafaf5a22006-09-19 10:28:00 -07005573 ha = pci_get_drvdata(pdev);
5574
Vikas Chaudharyee996a62012-08-22 07:55:05 -04005575 if (is_qla40XX(ha))
Karen Higgins7eece5a2011-03-21 03:34:29 -07005576 qla4xxx_prevent_other_port_reinit(ha);
David C Somayajulubee4fe82007-05-23 18:03:32 -07005577
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05005578 /* destroy iface from sysfs */
5579 qla4xxx_destroy_ifaces(ha);
5580
Mike Christie13483732011-12-01 21:38:41 -06005581 if ((!ql4xdisablesysfsboot) && ha->boot_kset)
Manish Rangankar2a991c22011-07-25 13:48:55 -05005582 iscsi_boot_destroy_kset(ha->boot_kset);
5583
Mike Christie13483732011-12-01 21:38:41 -06005584 qla4xxx_destroy_fw_ddb_session(ha);
Tej Parkash068237c82012-05-18 04:41:44 -04005585 qla4_8xxx_free_sysfs_attr(ha);
Mike Christie13483732011-12-01 21:38:41 -06005586
David Somayajuluafaf5a22006-09-19 10:28:00 -07005587 scsi_remove_host(ha->host);
5588
5589 qla4xxx_free_adapter(ha);
5590
5591 scsi_host_put(ha->host);
5592
Lalit Chandivade2232be02010-07-30 14:38:47 +05305593 pci_disable_pcie_error_reporting(pdev);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05305594 pci_disable_device(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07005595 pci_set_drvdata(pdev, NULL);
5596}
5597
5598/**
5599 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
5600 * @ha: HA context
5601 *
5602 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
5603 * supported addressing method.
5604 */
Adrian Bunk47975472007-04-26 00:35:16 -07005605static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07005606{
5607 int retval;
5608
5609 /* Update our PCI device dma_mask for full 64 bit mask */
Yang Hongyang6a355282009-04-06 19:01:13 -07005610 if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
5611 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07005612 dev_dbg(&ha->pdev->dev,
5613 "Failed to set 64 bit PCI consistent mask; "
5614 "using 32 bit.\n");
5615 retval = pci_set_consistent_dma_mask(ha->pdev,
Yang Hongyang284901a2009-04-06 19:01:15 -07005616 DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07005617 }
5618 } else
Yang Hongyang284901a2009-04-06 19:01:15 -07005619 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07005620}
5621
5622static int qla4xxx_slave_alloc(struct scsi_device *sdev)
5623{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05005624 struct iscsi_cls_session *cls_sess;
5625 struct iscsi_session *sess;
5626 struct ddb_entry *ddb;
Vikas Chaudhary8bb40332011-03-21 03:34:31 -07005627 int queue_depth = QL4_DEF_QDEPTH;
David Somayajuluafaf5a22006-09-19 10:28:00 -07005628
Manish Rangankarb3a271a2011-07-25 13:48:53 -05005629 cls_sess = starget_to_session(sdev->sdev_target);
5630 sess = cls_sess->dd_data;
5631 ddb = sess->dd_data;
5632
David Somayajuluafaf5a22006-09-19 10:28:00 -07005633 sdev->hostdata = ddb;
5634 sdev->tagged_supported = 1;
Vikas Chaudhary8bb40332011-03-21 03:34:31 -07005635
5636 if (ql4xmaxqdepth != 0 && ql4xmaxqdepth <= 0xffffU)
5637 queue_depth = ql4xmaxqdepth;
5638
5639 scsi_activate_tcq(sdev, queue_depth);
David Somayajuluafaf5a22006-09-19 10:28:00 -07005640 return 0;
5641}
5642
5643static int qla4xxx_slave_configure(struct scsi_device *sdev)
5644{
5645 sdev->tagged_supported = 1;
5646 return 0;
5647}
5648
5649static void qla4xxx_slave_destroy(struct scsi_device *sdev)
5650{
5651 scsi_deactivate_tcq(sdev, 1);
5652}
5653
Tej Parkashf7b4aa632012-04-30 04:12:19 -07005654static int qla4xxx_change_queue_depth(struct scsi_device *sdev, int qdepth,
5655 int reason)
5656{
5657 if (!ql4xqfulltracking)
5658 return -EOPNOTSUPP;
5659
5660 return iscsi_change_queue_depth(sdev, qdepth, reason);
5661}
5662
David Somayajuluafaf5a22006-09-19 10:28:00 -07005663/**
5664 * qla4xxx_del_from_active_array - returns an active srb
5665 * @ha: Pointer to host adapter structure.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02005666 * @index: index into the active_array
David Somayajuluafaf5a22006-09-19 10:28:00 -07005667 *
5668 * This routine removes and returns the srb at the specified index
5669 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05305670struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
5671 uint32_t index)
David Somayajuluafaf5a22006-09-19 10:28:00 -07005672{
5673 struct srb *srb = NULL;
Vikas Chaudhary53698872010-04-28 11:41:59 +05305674 struct scsi_cmnd *cmd = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -07005675
Vikas Chaudhary53698872010-04-28 11:41:59 +05305676 cmd = scsi_host_find_tag(ha->host, index);
5677 if (!cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07005678 return srb;
5679
Vikas Chaudhary53698872010-04-28 11:41:59 +05305680 srb = (struct srb *)CMD_SP(cmd);
5681 if (!srb)
David Somayajuluafaf5a22006-09-19 10:28:00 -07005682 return srb;
5683
5684 /* update counters */
5685 if (srb->flags & SRB_DMA_VALID) {
5686 ha->req_q_count += srb->iocb_cnt;
5687 ha->iocb_cnt -= srb->iocb_cnt;
5688 if (srb->cmd)
Vikas Chaudhary53698872010-04-28 11:41:59 +05305689 srb->cmd->host_scribble =
5690 (unsigned char *)(unsigned long) MAX_SRBS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07005691 }
5692 return srb;
5693}
5694
5695/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07005696 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05305697 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07005698 * @cmd: Scsi Command to wait on.
5699 *
5700 * This routine waits for the command to be returned by the Firmware
5701 * for some max time.
5702 **/
5703static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
5704 struct scsi_cmnd *cmd)
5705{
5706 int done = 0;
5707 struct srb *rp;
5708 uint32_t max_wait_time = EH_WAIT_CMD_TOV;
Lalit Chandivade2232be02010-07-30 14:38:47 +05305709 int ret = SUCCESS;
5710
5711 /* Dont wait on command if PCI error is being handled
5712 * by PCI AER driver
5713 */
5714 if (unlikely(pci_channel_offline(ha->pdev)) ||
5715 (test_bit(AF_EEH_BUSY, &ha->flags))) {
5716 ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
5717 ha->host_no, __func__);
5718 return ret;
5719 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07005720
5721 do {
5722 /* Checking to see if its returned to OS */
Vikas Chaudhary53698872010-04-28 11:41:59 +05305723 rp = (struct srb *) CMD_SP(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07005724 if (rp == NULL) {
5725 done++;
5726 break;
5727 }
5728
5729 msleep(2000);
5730 } while (max_wait_time--);
5731
5732 return done;
5733}
5734
5735/**
5736 * qla4xxx_wait_for_hba_online - waits for HBA to come online
5737 * @ha: Pointer to host adapter structure
5738 **/
5739static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
5740{
5741 unsigned long wait_online;
5742
Vikas Chaudharyf581a3f2010-10-06 22:47:48 -07005743 wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
David Somayajuluafaf5a22006-09-19 10:28:00 -07005744 while (time_before(jiffies, wait_online)) {
5745
5746 if (adapter_up(ha))
5747 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07005748
5749 msleep(2000);
5750 }
5751
5752 return QLA_ERROR;
5753}
5754
5755/**
Mike Christiece545032008-02-29 18:25:20 -06005756 * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02005757 * @ha: pointer to HBA
David Somayajuluafaf5a22006-09-19 10:28:00 -07005758 * @t: target id
5759 * @l: lun id
5760 *
5761 * This function waits for all outstanding commands to a lun to complete. It
5762 * returns 0 if all pending commands are returned and 1 otherwise.
5763 **/
Mike Christiece545032008-02-29 18:25:20 -06005764static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
5765 struct scsi_target *stgt,
5766 struct scsi_device *sdev)
David Somayajuluafaf5a22006-09-19 10:28:00 -07005767{
5768 int cnt;
5769 int status = 0;
5770 struct scsi_cmnd *cmd;
5771
5772 /*
Mike Christiece545032008-02-29 18:25:20 -06005773 * Waiting for all commands for the designated target or dev
5774 * in the active array
David Somayajuluafaf5a22006-09-19 10:28:00 -07005775 */
5776 for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
5777 cmd = scsi_host_find_tag(ha->host, cnt);
Mike Christiece545032008-02-29 18:25:20 -06005778 if (cmd && stgt == scsi_target(cmd->device) &&
5779 (!sdev || sdev == cmd->device)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07005780 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
5781 status++;
5782 break;
5783 }
5784 }
5785 }
5786 return status;
5787}
5788
5789/**
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05305790 * qla4xxx_eh_abort - callback for abort task.
5791 * @cmd: Pointer to Linux's SCSI command structure
5792 *
5793 * This routine is called by the Linux OS to abort the specified
5794 * command.
5795 **/
5796static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
5797{
5798 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
5799 unsigned int id = cmd->device->id;
5800 unsigned int lun = cmd->device->lun;
Mike Christie92b3e5b2010-10-06 22:51:17 -07005801 unsigned long flags;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05305802 struct srb *srb = NULL;
5803 int ret = SUCCESS;
5804 int wait = 0;
5805
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05305806 ql4_printk(KERN_INFO, ha,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04005807 "scsi%ld:%d:%d: Abort command issued cmd=%p\n",
5808 ha->host_no, id, lun, cmd);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05305809
Mike Christie92b3e5b2010-10-06 22:51:17 -07005810 spin_lock_irqsave(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05305811 srb = (struct srb *) CMD_SP(cmd);
Mike Christie92b3e5b2010-10-06 22:51:17 -07005812 if (!srb) {
5813 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05305814 return SUCCESS;
Mike Christie92b3e5b2010-10-06 22:51:17 -07005815 }
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05305816 kref_get(&srb->srb_ref);
Mike Christie92b3e5b2010-10-06 22:51:17 -07005817 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05305818
5819 if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
5820 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
5821 ha->host_no, id, lun));
5822 ret = FAILED;
5823 } else {
5824 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
5825 ha->host_no, id, lun));
5826 wait = 1;
5827 }
5828
5829 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
5830
5831 /* Wait for command to complete */
5832 if (wait) {
5833 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
5834 DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
5835 ha->host_no, id, lun));
5836 ret = FAILED;
5837 }
5838 }
5839
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05305840 ql4_printk(KERN_INFO, ha,
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05305841 "scsi%ld:%d:%d: Abort command - %s\n",
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005842 ha->host_no, id, lun, (ret == SUCCESS) ? "succeeded" : "failed");
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05305843
5844 return ret;
5845}
5846
5847/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07005848 * qla4xxx_eh_device_reset - callback for target reset.
5849 * @cmd: Pointer to Linux's SCSI command structure
5850 *
5851 * This routine is called by the Linux OS to reset all luns on the
5852 * specified target.
5853 **/
5854static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
5855{
5856 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
5857 struct ddb_entry *ddb_entry = cmd->device->hostdata;
David Somayajuluafaf5a22006-09-19 10:28:00 -07005858 int ret = FAILED, stat;
5859
Karen Higgins612f73482009-07-15 15:03:01 -05005860 if (!ddb_entry)
David Somayajuluafaf5a22006-09-19 10:28:00 -07005861 return ret;
5862
Mike Christiec01be6d2010-07-22 16:59:49 +05305863 ret = iscsi_block_scsi_eh(cmd);
5864 if (ret)
5865 return ret;
5866 ret = FAILED;
5867
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05305868 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07005869 "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
5870 cmd->device->channel, cmd->device->id, cmd->device->lun);
5871
5872 DEBUG2(printk(KERN_INFO
5873 "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
5874 "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
Jens Axboe242f9dc2008-09-14 05:55:09 -07005875 cmd, jiffies, cmd->request->timeout / HZ,
David Somayajuluafaf5a22006-09-19 10:28:00 -07005876 ha->dpc_flags, cmd->result, cmd->allowed));
5877
5878 /* FIXME: wait for hba to go online */
5879 stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
5880 if (stat != QLA_SUCCESS) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05305881 ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
David Somayajuluafaf5a22006-09-19 10:28:00 -07005882 goto eh_dev_reset_done;
5883 }
5884
Mike Christiece545032008-02-29 18:25:20 -06005885 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
5886 cmd->device)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05305887 ql4_printk(KERN_INFO, ha,
Mike Christiece545032008-02-29 18:25:20 -06005888 "DEVICE RESET FAILED - waiting for "
5889 "commands.\n");
5890 goto eh_dev_reset_done;
David Somayajuluafaf5a22006-09-19 10:28:00 -07005891 }
5892
David C Somayajulu9d562912008-03-19 11:23:03 -07005893 /* Send marker. */
5894 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
5895 MM_LUN_RESET) != QLA_SUCCESS)
5896 goto eh_dev_reset_done;
5897
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05305898 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07005899 "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
5900 ha->host_no, cmd->device->channel, cmd->device->id,
5901 cmd->device->lun);
5902
5903 ret = SUCCESS;
5904
5905eh_dev_reset_done:
5906
5907 return ret;
5908}
5909
5910/**
Mike Christiece545032008-02-29 18:25:20 -06005911 * qla4xxx_eh_target_reset - callback for target reset.
5912 * @cmd: Pointer to Linux's SCSI command structure
5913 *
5914 * This routine is called by the Linux OS to reset the target.
5915 **/
5916static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
5917{
5918 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
5919 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christiec01be6d2010-07-22 16:59:49 +05305920 int stat, ret;
Mike Christiece545032008-02-29 18:25:20 -06005921
5922 if (!ddb_entry)
5923 return FAILED;
5924
Mike Christiec01be6d2010-07-22 16:59:49 +05305925 ret = iscsi_block_scsi_eh(cmd);
5926 if (ret)
5927 return ret;
5928
Mike Christiece545032008-02-29 18:25:20 -06005929 starget_printk(KERN_INFO, scsi_target(cmd->device),
5930 "WARM TARGET RESET ISSUED.\n");
5931
5932 DEBUG2(printk(KERN_INFO
5933 "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
5934 "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
Jens Axboe242f9dc2008-09-14 05:55:09 -07005935 ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
Mike Christiece545032008-02-29 18:25:20 -06005936 ha->dpc_flags, cmd->result, cmd->allowed));
5937
5938 stat = qla4xxx_reset_target(ha, ddb_entry);
5939 if (stat != QLA_SUCCESS) {
5940 starget_printk(KERN_INFO, scsi_target(cmd->device),
5941 "WARM TARGET RESET FAILED.\n");
5942 return FAILED;
5943 }
5944
Mike Christiece545032008-02-29 18:25:20 -06005945 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
5946 NULL)) {
5947 starget_printk(KERN_INFO, scsi_target(cmd->device),
5948 "WARM TARGET DEVICE RESET FAILED - "
5949 "waiting for commands.\n");
5950 return FAILED;
5951 }
5952
David C Somayajulu9d562912008-03-19 11:23:03 -07005953 /* Send marker. */
5954 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
5955 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
5956 starget_printk(KERN_INFO, scsi_target(cmd->device),
5957 "WARM TARGET DEVICE RESET FAILED - "
5958 "marker iocb failed.\n");
5959 return FAILED;
5960 }
5961
Mike Christiece545032008-02-29 18:25:20 -06005962 starget_printk(KERN_INFO, scsi_target(cmd->device),
5963 "WARM TARGET RESET SUCCEEDED.\n");
5964 return SUCCESS;
5965}
5966
5967/**
Sarang Radke8a288962011-12-06 02:34:10 -08005968 * qla4xxx_is_eh_active - check if error handler is running
5969 * @shost: Pointer to SCSI Host struct
5970 *
5971 * This routine finds that if reset host is called in EH
5972 * scenario or from some application like sg_reset
5973 **/
5974static int qla4xxx_is_eh_active(struct Scsi_Host *shost)
5975{
5976 if (shost->shost_state == SHOST_RECOVERY)
5977 return 1;
5978 return 0;
5979}
5980
5981/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07005982 * qla4xxx_eh_host_reset - kernel callback
5983 * @cmd: Pointer to Linux's SCSI command structure
5984 *
5985 * This routine is invoked by the Linux kernel to perform fatal error
5986 * recovery on the specified adapter.
5987 **/
5988static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
5989{
5990 int return_status = FAILED;
5991 struct scsi_qla_host *ha;
5992
Manish Rangankarb3a271a2011-07-25 13:48:53 -05005993 ha = to_qla_host(cmd->device->host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07005994
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04005995 if (is_qla8032(ha) && ql4xdontresethba)
5996 qla4_83xx_set_idc_dontreset(ha);
5997
5998 /*
5999 * For ISP8324, if IDC_CTRL DONTRESET_BIT0 is set by other
6000 * protocol drivers, we should not set device_state to
6001 * NEED_RESET
6002 */
6003 if (ql4xdontresethba ||
6004 (is_qla8032(ha) && qla4_83xx_idc_dontreset(ha))) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05306005 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
6006 ha->host_no, __func__));
Sarang Radke8a288962011-12-06 02:34:10 -08006007
6008 /* Clear outstanding srb in queues */
6009 if (qla4xxx_is_eh_active(cmd->device->host))
6010 qla4xxx_abort_active_cmds(ha, DID_ABORT << 16);
6011
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05306012 return FAILED;
6013 }
6014
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05306015 ql4_printk(KERN_INFO, ha,
Karen Higginsdca05c42009-07-15 15:03:00 -05006016 "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
David Somayajuluafaf5a22006-09-19 10:28:00 -07006017 cmd->device->channel, cmd->device->id, cmd->device->lun);
6018
6019 if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
6020 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
6021 "DEAD.\n", ha->host_no, cmd->device->channel,
6022 __func__));
6023
6024 return FAILED;
6025 }
6026
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05306027 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04006028 if (is_qla80XX(ha))
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05306029 set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
6030 else
6031 set_bit(DPC_RESET_HA, &ha->dpc_flags);
6032 }
Mike Christie50a29ae2008-03-04 13:26:53 -06006033
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05306034 if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
David Somayajuluafaf5a22006-09-19 10:28:00 -07006035 return_status = SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07006036
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05306037 ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006038 return_status == FAILED ? "FAILED" : "SUCCEEDED");
David Somayajuluafaf5a22006-09-19 10:28:00 -07006039
6040 return return_status;
6041}
6042
Vikas Chaudhary95d31262011-08-12 02:51:29 -07006043static int qla4xxx_context_reset(struct scsi_qla_host *ha)
6044{
6045 uint32_t mbox_cmd[MBOX_REG_COUNT];
6046 uint32_t mbox_sts[MBOX_REG_COUNT];
6047 struct addr_ctrl_blk_def *acb = NULL;
6048 uint32_t acb_len = sizeof(struct addr_ctrl_blk_def);
6049 int rval = QLA_SUCCESS;
6050 dma_addr_t acb_dma;
6051
6052 acb = dma_alloc_coherent(&ha->pdev->dev,
6053 sizeof(struct addr_ctrl_blk_def),
6054 &acb_dma, GFP_KERNEL);
6055 if (!acb) {
6056 ql4_printk(KERN_ERR, ha, "%s: Unable to alloc acb\n",
6057 __func__);
6058 rval = -ENOMEM;
6059 goto exit_port_reset;
6060 }
6061
6062 memset(acb, 0, acb_len);
6063
6064 rval = qla4xxx_get_acb(ha, acb_dma, PRIMARI_ACB, acb_len);
6065 if (rval != QLA_SUCCESS) {
6066 rval = -EIO;
6067 goto exit_free_acb;
6068 }
6069
6070 rval = qla4xxx_disable_acb(ha);
6071 if (rval != QLA_SUCCESS) {
6072 rval = -EIO;
6073 goto exit_free_acb;
6074 }
6075
6076 wait_for_completion_timeout(&ha->disable_acb_comp,
6077 DISABLE_ACB_TOV * HZ);
6078
6079 rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], acb_dma);
6080 if (rval != QLA_SUCCESS) {
6081 rval = -EIO;
6082 goto exit_free_acb;
6083 }
6084
6085exit_free_acb:
6086 dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk_def),
6087 acb, acb_dma);
6088exit_port_reset:
6089 DEBUG2(ql4_printk(KERN_INFO, ha, "%s %s\n", __func__,
6090 rval == QLA_SUCCESS ? "SUCCEEDED" : "FAILED"));
6091 return rval;
6092}
6093
6094static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type)
6095{
6096 struct scsi_qla_host *ha = to_qla_host(shost);
6097 int rval = QLA_SUCCESS;
Vikas Chaudharyebd777d2013-03-07 05:43:09 -05006098 uint32_t idc_ctrl;
Vikas Chaudhary95d31262011-08-12 02:51:29 -07006099
6100 if (ql4xdontresethba) {
6101 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Don't Reset HBA\n",
6102 __func__));
6103 rval = -EPERM;
6104 goto exit_host_reset;
6105 }
6106
Vikas Chaudhary95d31262011-08-12 02:51:29 -07006107 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
6108 goto recover_adapter;
6109
6110 switch (reset_type) {
6111 case SCSI_ADAPTER_RESET:
6112 set_bit(DPC_RESET_HA, &ha->dpc_flags);
6113 break;
6114 case SCSI_FIRMWARE_RESET:
6115 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04006116 if (is_qla80XX(ha))
Vikas Chaudhary95d31262011-08-12 02:51:29 -07006117 /* set firmware context reset */
6118 set_bit(DPC_RESET_HA_FW_CONTEXT,
6119 &ha->dpc_flags);
6120 else {
6121 rval = qla4xxx_context_reset(ha);
6122 goto exit_host_reset;
6123 }
6124 }
6125 break;
6126 }
6127
6128recover_adapter:
Vikas Chaudharyebd777d2013-03-07 05:43:09 -05006129 /* For ISP83XX set graceful reset bit in IDC_DRV_CTRL if
6130 * reset is issued by application */
6131 if (is_qla8032(ha) && test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
6132 idc_ctrl = qla4_83xx_rd_reg(ha, QLA83XX_IDC_DRV_CTRL);
6133 qla4_83xx_wr_reg(ha, QLA83XX_IDC_DRV_CTRL,
6134 (idc_ctrl | GRACEFUL_RESET_BIT1));
6135 }
6136
Vikas Chaudhary95d31262011-08-12 02:51:29 -07006137 rval = qla4xxx_recover_adapter(ha);
6138 if (rval != QLA_SUCCESS) {
6139 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: recover adapter fail\n",
6140 __func__));
6141 rval = -EIO;
6142 }
6143
6144exit_host_reset:
6145 return rval;
6146}
6147
Lalit Chandivade2232be02010-07-30 14:38:47 +05306148/* PCI AER driver recovers from all correctable errors w/o
6149 * driver intervention. For uncorrectable errors PCI AER
6150 * driver calls the following device driver's callbacks
6151 *
6152 * - Fatal Errors - link_reset
6153 * - Non-Fatal Errors - driver's pci_error_detected() which
6154 * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
6155 *
6156 * PCI AER driver calls
6157 * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
6158 * returns RECOVERED or NEED_RESET if fw_hung
6159 * NEED_RESET - driver's slot_reset()
6160 * DISCONNECT - device is dead & cannot recover
6161 * RECOVERED - driver's pci_resume()
6162 */
6163static pci_ers_result_t
6164qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
6165{
6166 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
6167
6168 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
6169 ha->host_no, __func__, state);
6170
6171 if (!is_aer_supported(ha))
6172 return PCI_ERS_RESULT_NONE;
6173
6174 switch (state) {
6175 case pci_channel_io_normal:
6176 clear_bit(AF_EEH_BUSY, &ha->flags);
6177 return PCI_ERS_RESULT_CAN_RECOVER;
6178 case pci_channel_io_frozen:
6179 set_bit(AF_EEH_BUSY, &ha->flags);
6180 qla4xxx_mailbox_premature_completion(ha);
6181 qla4xxx_free_irqs(ha);
6182 pci_disable_device(pdev);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07006183 /* Return back all IOs */
6184 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
Lalit Chandivade2232be02010-07-30 14:38:47 +05306185 return PCI_ERS_RESULT_NEED_RESET;
6186 case pci_channel_io_perm_failure:
6187 set_bit(AF_EEH_BUSY, &ha->flags);
6188 set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
6189 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
6190 return PCI_ERS_RESULT_DISCONNECT;
6191 }
6192 return PCI_ERS_RESULT_NEED_RESET;
6193}
6194
6195/**
6196 * qla4xxx_pci_mmio_enabled() gets called if
6197 * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
6198 * and read/write to the device still works.
6199 **/
6200static pci_ers_result_t
6201qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
6202{
6203 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
6204
6205 if (!is_aer_supported(ha))
6206 return PCI_ERS_RESULT_NONE;
6207
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07006208 return PCI_ERS_RESULT_RECOVERED;
Lalit Chandivade2232be02010-07-30 14:38:47 +05306209}
6210
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07006211static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
Lalit Chandivade2232be02010-07-30 14:38:47 +05306212{
6213 uint32_t rval = QLA_ERROR;
6214 int fn;
6215 struct pci_dev *other_pdev = NULL;
6216
6217 ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
6218
6219 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
6220
6221 if (test_bit(AF_ONLINE, &ha->flags)) {
6222 clear_bit(AF_ONLINE, &ha->flags);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05006223 clear_bit(AF_LINK_UP, &ha->flags);
6224 iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
Lalit Chandivade2232be02010-07-30 14:38:47 +05306225 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
Lalit Chandivade2232be02010-07-30 14:38:47 +05306226 }
6227
6228 fn = PCI_FUNC(ha->pdev->devfn);
6229 while (fn > 0) {
6230 fn--;
6231 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
6232 "func %x\n", ha->host_no, __func__, fn);
6233 /* Get the pci device given the domain, bus,
6234 * slot/function number */
6235 other_pdev =
6236 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
6237 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
6238 fn));
6239
6240 if (!other_pdev)
6241 continue;
6242
6243 if (atomic_read(&other_pdev->enable_cnt)) {
6244 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
6245 "func in enabled state%x\n", ha->host_no,
6246 __func__, fn);
6247 pci_dev_put(other_pdev);
6248 break;
6249 }
6250 pci_dev_put(other_pdev);
6251 }
6252
6253 /* The first function on the card, the reset owner will
6254 * start & initialize the firmware. The other functions
6255 * on the card will reset the firmware context
6256 */
6257 if (!fn) {
6258 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
6259 "0x%x is the owner\n", ha->host_no, __func__,
6260 ha->pdev->devfn);
6261
Vikas Chaudhary33693c72012-08-22 07:55:04 -04006262 ha->isp_ops->idc_lock(ha);
6263 qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
6264 QLA8XXX_DEV_COLD);
Vikas Chaudhary33693c72012-08-22 07:55:04 -04006265 ha->isp_ops->idc_unlock(ha);
Lalit Chandivade2232be02010-07-30 14:38:47 +05306266
Vikas Chaudhary39c95822012-09-20 07:35:05 -04006267 rval = qla4_8xxx_update_idc_reg(ha);
6268 if (rval == QLA_ERROR) {
6269 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: FAILED\n",
6270 ha->host_no, __func__);
6271 ha->isp_ops->idc_lock(ha);
6272 qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
6273 QLA8XXX_DEV_FAILED);
6274 ha->isp_ops->idc_unlock(ha);
6275 goto exit_error_recovery;
6276 }
Lalit Chandivade2232be02010-07-30 14:38:47 +05306277
Lalit Chandivade2232be02010-07-30 14:38:47 +05306278 clear_bit(AF_FW_RECOVERY, &ha->flags);
Mike Christie13483732011-12-01 21:38:41 -06006279 rval = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
Lalit Chandivade2232be02010-07-30 14:38:47 +05306280
6281 if (rval != QLA_SUCCESS) {
6282 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
6283 "FAILED\n", ha->host_no, __func__);
Poornima Vonti82761902012-09-20 07:35:04 -04006284 ha->isp_ops->idc_lock(ha);
Lalit Chandivade2232be02010-07-30 14:38:47 +05306285 qla4_8xxx_clear_drv_active(ha);
Vikas Chaudhary33693c72012-08-22 07:55:04 -04006286 qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
6287 QLA8XXX_DEV_FAILED);
Poornima Vonti82761902012-09-20 07:35:04 -04006288 ha->isp_ops->idc_unlock(ha);
Lalit Chandivade2232be02010-07-30 14:38:47 +05306289 } else {
6290 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
6291 "READY\n", ha->host_no, __func__);
Poornima Vonti82761902012-09-20 07:35:04 -04006292 ha->isp_ops->idc_lock(ha);
Vikas Chaudhary33693c72012-08-22 07:55:04 -04006293 qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
6294 QLA8XXX_DEV_READY);
Lalit Chandivade2232be02010-07-30 14:38:47 +05306295 /* Clear driver state register */
Vikas Chaudhary33693c72012-08-22 07:55:04 -04006296 qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DRV_STATE, 0);
Lalit Chandivade2232be02010-07-30 14:38:47 +05306297 qla4_8xxx_set_drv_active(ha);
Poornima Vonti82761902012-09-20 07:35:04 -04006298 ha->isp_ops->idc_unlock(ha);
Poornima Vonti137257d2013-01-20 23:51:01 -05006299 ha->isp_ops->enable_intrs(ha);
Lalit Chandivade2232be02010-07-30 14:38:47 +05306300 }
Lalit Chandivade2232be02010-07-30 14:38:47 +05306301 } else {
6302 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
6303 "the reset owner\n", ha->host_no, __func__,
6304 ha->pdev->devfn);
Vikas Chaudhary33693c72012-08-22 07:55:04 -04006305 if ((qla4_8xxx_rd_direct(ha, QLA8XXX_CRB_DEV_STATE) ==
6306 QLA8XXX_DEV_READY)) {
Lalit Chandivade2232be02010-07-30 14:38:47 +05306307 clear_bit(AF_FW_RECOVERY, &ha->flags);
Mike Christie13483732011-12-01 21:38:41 -06006308 rval = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
Poornima Vonti137257d2013-01-20 23:51:01 -05006309 if (rval == QLA_SUCCESS)
6310 ha->isp_ops->enable_intrs(ha);
6311
Vikas Chaudhary33693c72012-08-22 07:55:04 -04006312 ha->isp_ops->idc_lock(ha);
Lalit Chandivade2232be02010-07-30 14:38:47 +05306313 qla4_8xxx_set_drv_active(ha);
Vikas Chaudhary33693c72012-08-22 07:55:04 -04006314 ha->isp_ops->idc_unlock(ha);
Lalit Chandivade2232be02010-07-30 14:38:47 +05306315 }
6316 }
Vikas Chaudhary39c95822012-09-20 07:35:05 -04006317exit_error_recovery:
Lalit Chandivade2232be02010-07-30 14:38:47 +05306318 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
6319 return rval;
6320}
6321
6322static pci_ers_result_t
6323qla4xxx_pci_slot_reset(struct pci_dev *pdev)
6324{
6325 pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
6326 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
6327 int rc;
6328
6329 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
6330 ha->host_no, __func__);
6331
6332 if (!is_aer_supported(ha))
6333 return PCI_ERS_RESULT_NONE;
6334
6335 /* Restore the saved state of PCIe device -
6336 * BAR registers, PCI Config space, PCIX, MSI,
6337 * IOV states
6338 */
6339 pci_restore_state(pdev);
6340
6341 /* pci_restore_state() clears the saved_state flag of the device
6342 * save restored state which resets saved_state flag
6343 */
6344 pci_save_state(pdev);
6345
6346 /* Initialize device or resume if in suspended state */
6347 rc = pci_enable_device(pdev);
6348 if (rc) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006349 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Can't re-enable "
Lalit Chandivade2232be02010-07-30 14:38:47 +05306350 "device after reset\n", ha->host_no, __func__);
6351 goto exit_slot_reset;
6352 }
6353
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07006354 ha->isp_ops->disable_intrs(ha);
Lalit Chandivade2232be02010-07-30 14:38:47 +05306355
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04006356 if (is_qla80XX(ha)) {
Lalit Chandivade2232be02010-07-30 14:38:47 +05306357 if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
6358 ret = PCI_ERS_RESULT_RECOVERED;
6359 goto exit_slot_reset;
6360 } else
6361 goto exit_slot_reset;
6362 }
6363
6364exit_slot_reset:
6365 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
6366 "device after reset\n", ha->host_no, __func__, ret);
6367 return ret;
6368}
6369
6370static void
6371qla4xxx_pci_resume(struct pci_dev *pdev)
6372{
6373 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
6374 int ret;
6375
6376 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
6377 ha->host_no, __func__);
6378
6379 ret = qla4xxx_wait_for_hba_online(ha);
6380 if (ret != QLA_SUCCESS) {
6381 ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
6382 "resume I/O from slot/link_reset\n", ha->host_no,
6383 __func__);
6384 }
6385
6386 pci_cleanup_aer_uncorrect_error_status(pdev);
6387 clear_bit(AF_EEH_BUSY, &ha->flags);
6388}
6389
Stephen Hemmingera55b2d22012-09-07 09:33:16 -07006390static const struct pci_error_handlers qla4xxx_err_handler = {
Lalit Chandivade2232be02010-07-30 14:38:47 +05306391 .error_detected = qla4xxx_pci_error_detected,
6392 .mmio_enabled = qla4xxx_pci_mmio_enabled,
6393 .slot_reset = qla4xxx_pci_slot_reset,
6394 .resume = qla4xxx_pci_resume,
6395};
6396
David Somayajuluafaf5a22006-09-19 10:28:00 -07006397static struct pci_device_id qla4xxx_pci_tbl[] = {
6398 {
6399 .vendor = PCI_VENDOR_ID_QLOGIC,
6400 .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
6401 .subvendor = PCI_ANY_ID,
6402 .subdevice = PCI_ANY_ID,
6403 },
6404 {
6405 .vendor = PCI_VENDOR_ID_QLOGIC,
6406 .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
6407 .subvendor = PCI_ANY_ID,
6408 .subdevice = PCI_ANY_ID,
6409 },
David C Somayajulud9150582006-11-15 17:38:40 -08006410 {
6411 .vendor = PCI_VENDOR_ID_QLOGIC,
6412 .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
6413 .subvendor = PCI_ANY_ID,
6414 .subdevice = PCI_ANY_ID,
6415 },
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05306416 {
6417 .vendor = PCI_VENDOR_ID_QLOGIC,
6418 .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
6419 .subvendor = PCI_ANY_ID,
6420 .subdevice = PCI_ANY_ID,
6421 },
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04006422 {
6423 .vendor = PCI_VENDOR_ID_QLOGIC,
6424 .device = PCI_DEVICE_ID_QLOGIC_ISP8324,
6425 .subvendor = PCI_ANY_ID,
6426 .subdevice = PCI_ANY_ID,
6427 },
David Somayajuluafaf5a22006-09-19 10:28:00 -07006428 {0, 0},
6429};
6430MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
6431
Adrian Bunk47975472007-04-26 00:35:16 -07006432static struct pci_driver qla4xxx_pci_driver = {
David Somayajuluafaf5a22006-09-19 10:28:00 -07006433 .name = DRIVER_NAME,
6434 .id_table = qla4xxx_pci_tbl,
6435 .probe = qla4xxx_probe_adapter,
6436 .remove = qla4xxx_remove_adapter,
Lalit Chandivade2232be02010-07-30 14:38:47 +05306437 .err_handler = &qla4xxx_err_handler,
David Somayajuluafaf5a22006-09-19 10:28:00 -07006438};
6439
6440static int __init qla4xxx_module_init(void)
6441{
6442 int ret;
6443
6444 /* Allocate cache for SRBs. */
6445 srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09006446 SLAB_HWCACHE_ALIGN, NULL);
David Somayajuluafaf5a22006-09-19 10:28:00 -07006447 if (srb_cachep == NULL) {
6448 printk(KERN_ERR
6449 "%s: Unable to allocate SRB cache..."
6450 "Failing load!\n", DRIVER_NAME);
6451 ret = -ENOMEM;
6452 goto no_srp_cache;
6453 }
6454
6455 /* Derive version string. */
6456 strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
Andrew Vasquez11010fe2006-10-06 09:54:59 -07006457 if (ql4xextended_error_logging)
David Somayajuluafaf5a22006-09-19 10:28:00 -07006458 strcat(qla4xxx_version_str, "-debug");
6459
6460 qla4xxx_scsi_transport =
6461 iscsi_register_transport(&qla4xxx_iscsi_transport);
6462 if (!qla4xxx_scsi_transport){
6463 ret = -ENODEV;
6464 goto release_srb_cache;
6465 }
6466
David Somayajuluafaf5a22006-09-19 10:28:00 -07006467 ret = pci_register_driver(&qla4xxx_pci_driver);
6468 if (ret)
6469 goto unregister_transport;
6470
6471 printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
6472 return 0;
Doug Maxey5ae16db2006-10-05 23:50:07 -05006473
David Somayajuluafaf5a22006-09-19 10:28:00 -07006474unregister_transport:
6475 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
6476release_srb_cache:
6477 kmem_cache_destroy(srb_cachep);
6478no_srp_cache:
6479 return ret;
6480}
6481
6482static void __exit qla4xxx_module_exit(void)
6483{
David Somayajuluafaf5a22006-09-19 10:28:00 -07006484 pci_unregister_driver(&qla4xxx_pci_driver);
6485 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
6486 kmem_cache_destroy(srb_cachep);
6487}
6488
6489module_init(qla4xxx_module_init);
6490module_exit(qla4xxx_module_exit);
6491
6492MODULE_AUTHOR("QLogic Corporation");
6493MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
6494MODULE_LICENSE("GPL");
6495MODULE_VERSION(QLA4XXX_DRIVER_VERSION);