xref: /openbmc/linux/drivers/scsi/qla4xxx/ql4_os.c (revision c7685190)
1 /*
2  * QLogic iSCSI HBA Driver
3  * Copyright (c)  2003-2013 QLogic Corporation
4  *
5  * See LICENSE.qla4xxx for copyright and licensing details.
6  */
7 #include <linux/moduleparam.h>
8 #include <linux/slab.h>
9 #include <linux/blkdev.h>
10 #include <linux/iscsi_boot_sysfs.h>
11 #include <linux/inet.h>
12 
13 #include <scsi/scsi_tcq.h>
14 #include <scsi/scsicam.h>
15 
16 #include "ql4_def.h"
17 #include "ql4_version.h"
18 #include "ql4_glbl.h"
19 #include "ql4_dbg.h"
20 #include "ql4_inline.h"
21 #include "ql4_83xx.h"
22 
23 /*
24  * Driver version
25  */
26 static char qla4xxx_version_str[40];
27 
28 /*
29  * SRB allocation cache
30  */
31 static struct kmem_cache *srb_cachep;
32 
33 /*
34  * Module parameter information and variables
35  */
36 static int ql4xdisablesysfsboot = 1;
37 module_param(ql4xdisablesysfsboot, int, S_IRUGO | S_IWUSR);
38 MODULE_PARM_DESC(ql4xdisablesysfsboot,
39 		 " 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)");
42 
43 int ql4xdontresethba;
44 module_param(ql4xdontresethba, int, S_IRUGO | S_IWUSR);
45 MODULE_PARM_DESC(ql4xdontresethba,
46 		 " 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");
49 
50 int ql4xextended_error_logging;
51 module_param(ql4xextended_error_logging, int, S_IRUGO | S_IWUSR);
52 MODULE_PARM_DESC(ql4xextended_error_logging,
53 		 " Option to enable extended error logging.\n"
54 		 "\t\t  0 - no logging (Default)\n"
55 		 "\t\t  2 - debug logging");
56 
57 int ql4xenablemsix = 1;
58 module_param(ql4xenablemsix, int, S_IRUGO|S_IWUSR);
59 MODULE_PARM_DESC(ql4xenablemsix,
60 		 " 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.");
64 
65 #define QL4_DEF_QDEPTH 32
66 static int ql4xmaxqdepth = QL4_DEF_QDEPTH;
67 module_param(ql4xmaxqdepth, int, S_IRUGO | S_IWUSR);
68 MODULE_PARM_DESC(ql4xmaxqdepth,
69 		 " Maximum queue depth to report for target devices.\n"
70 		 "\t\t  Default: 32.");
71 
72 static int ql4xqfulltracking = 1;
73 module_param(ql4xqfulltracking, int, S_IRUGO | S_IWUSR);
74 MODULE_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 
80 static int ql4xsess_recovery_tmo = QL4_SESS_RECOVERY_TMO;
81 module_param(ql4xsess_recovery_tmo, int, S_IRUGO);
82 MODULE_PARM_DESC(ql4xsess_recovery_tmo,
83 		" Target Session Recovery Timeout.\n"
84 		"\t\t  Default: 120 sec.");
85 
86 int ql4xmdcapmask = 0;
87 module_param(ql4xmdcapmask, int, S_IRUGO);
88 MODULE_PARM_DESC(ql4xmdcapmask,
89 		 " Set the Minidump driver capture mask level.\n"
90 		 "\t\t  Default is 0 (firmware default capture mask)\n"
91 		 "\t\t  Can be set to 0x3, 0x7, 0xF, 0x1F, 0x3F, 0x7F, 0xFF");
92 
93 int ql4xenablemd = 1;
94 module_param(ql4xenablemd, int, S_IRUGO | S_IWUSR);
95 MODULE_PARM_DESC(ql4xenablemd,
96 		 " Set to enable minidump.\n"
97 		 "\t\t  0 - disable minidump\n"
98 		 "\t\t  1 - enable minidump (Default)");
99 
100 static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha);
101 /*
102  * SCSI host template entry points
103  */
104 static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
105 
106 /*
107  * iSCSI template entry points
108  */
109 static int qla4xxx_session_get_param(struct iscsi_cls_session *cls_sess,
110 				     enum iscsi_param param, char *buf);
111 static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
112 				  enum iscsi_param param, char *buf);
113 static int qla4xxx_host_get_param(struct Scsi_Host *shost,
114 				  enum iscsi_host_param param, char *buf);
115 static int qla4xxx_iface_set_param(struct Scsi_Host *shost, void *data,
116 				   uint32_t len);
117 static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
118 				   enum iscsi_param_type param_type,
119 				   int param, char *buf);
120 static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
121 static struct iscsi_endpoint *qla4xxx_ep_connect(struct Scsi_Host *shost,
122 						 struct sockaddr *dst_addr,
123 						 int non_blocking);
124 static int qla4xxx_ep_poll(struct iscsi_endpoint *ep, int timeout_ms);
125 static void qla4xxx_ep_disconnect(struct iscsi_endpoint *ep);
126 static int qla4xxx_get_ep_param(struct iscsi_endpoint *ep,
127 				enum iscsi_param param, char *buf);
128 static int qla4xxx_conn_start(struct iscsi_cls_conn *conn);
129 static struct iscsi_cls_conn *
130 qla4xxx_conn_create(struct iscsi_cls_session *cls_sess, uint32_t conn_idx);
131 static 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);
134 static void qla4xxx_conn_destroy(struct iscsi_cls_conn *conn);
135 static struct iscsi_cls_session *
136 qla4xxx_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max,
137 			uint16_t qdepth, uint32_t initial_cmdsn);
138 static void qla4xxx_session_destroy(struct iscsi_cls_session *sess);
139 static void qla4xxx_task_work(struct work_struct *wdata);
140 static int qla4xxx_alloc_pdu(struct iscsi_task *, uint8_t);
141 static int qla4xxx_task_xmit(struct iscsi_task *);
142 static void qla4xxx_task_cleanup(struct iscsi_task *);
143 static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session);
144 static void qla4xxx_conn_get_stats(struct iscsi_cls_conn *cls_conn,
145 				   struct iscsi_stats *stats);
146 static 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);
149 static int qla4xxx_get_chap_list(struct Scsi_Host *shost, uint16_t chap_tbl_idx,
150 				 uint32_t *num_entries, char *buf);
151 static int qla4xxx_delete_chap(struct Scsi_Host *shost, uint16_t chap_tbl_idx);
152 static int qla4xxx_set_chap_entry(struct Scsi_Host *shost, void  *data,
153 				  int len);
154 static int qla4xxx_get_host_stats(struct Scsi_Host *shost, char *buf, int len);
155 
156 /*
157  * SCSI host template entry points
158  */
159 static int qla4xxx_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmd);
160 static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
161 static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
162 static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
163 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
164 static int qla4xxx_slave_alloc(struct scsi_device *device);
165 static umode_t qla4_attr_is_visible(int param_type, int param);
166 static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type);
167 
168 /*
169  * iSCSI Flash DDB sysfs entry points
170  */
171 static int
172 qla4xxx_sysfs_ddb_set_param(struct iscsi_bus_flash_session *fnode_sess,
173 			    struct iscsi_bus_flash_conn *fnode_conn,
174 			    void *data, int len);
175 static int
176 qla4xxx_sysfs_ddb_get_param(struct iscsi_bus_flash_session *fnode_sess,
177 			    int param, char *buf);
178 static int qla4xxx_sysfs_ddb_add(struct Scsi_Host *shost, const char *buf,
179 				 int len);
180 static int
181 qla4xxx_sysfs_ddb_delete(struct iscsi_bus_flash_session *fnode_sess);
182 static int qla4xxx_sysfs_ddb_login(struct iscsi_bus_flash_session *fnode_sess,
183 				   struct iscsi_bus_flash_conn *fnode_conn);
184 static int qla4xxx_sysfs_ddb_logout(struct iscsi_bus_flash_session *fnode_sess,
185 				    struct iscsi_bus_flash_conn *fnode_conn);
186 static int qla4xxx_sysfs_ddb_logout_sid(struct iscsi_cls_session *cls_sess);
187 
188 static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
189     QLA82XX_LEGACY_INTR_CONFIG;
190 
191 static struct scsi_host_template qla4xxx_driver_template = {
192 	.module			= THIS_MODULE,
193 	.name			= DRIVER_NAME,
194 	.proc_name		= DRIVER_NAME,
195 	.queuecommand		= qla4xxx_queuecommand,
196 
197 	.eh_abort_handler	= qla4xxx_eh_abort,
198 	.eh_device_reset_handler = qla4xxx_eh_device_reset,
199 	.eh_target_reset_handler = qla4xxx_eh_target_reset,
200 	.eh_host_reset_handler	= qla4xxx_eh_host_reset,
201 	.eh_timed_out		= qla4xxx_eh_cmd_timed_out,
202 
203 	.slave_alloc		= qla4xxx_slave_alloc,
204 	.change_queue_depth	= scsi_change_queue_depth,
205 
206 	.this_id		= -1,
207 	.cmd_per_lun		= 3,
208 	.use_clustering		= ENABLE_CLUSTERING,
209 	.sg_tablesize		= SG_ALL,
210 
211 	.max_sectors		= 0xFFFF,
212 	.shost_attrs		= qla4xxx_host_attrs,
213 	.host_reset		= qla4xxx_host_reset,
214 	.vendor_id		= SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_QLOGIC,
215 };
216 
217 static struct iscsi_transport qla4xxx_iscsi_transport = {
218 	.owner			= THIS_MODULE,
219 	.name			= DRIVER_NAME,
220 	.caps			= CAP_TEXT_NEGO |
221 				  CAP_DATA_PATH_OFFLOAD | CAP_HDRDGST |
222 				  CAP_DATADGST | CAP_LOGIN_OFFLOAD |
223 				  CAP_MULTI_R2T,
224 	.attr_is_visible	= qla4_attr_is_visible,
225 	.create_session         = qla4xxx_session_create,
226 	.destroy_session        = qla4xxx_session_destroy,
227 	.start_conn             = qla4xxx_conn_start,
228 	.create_conn            = qla4xxx_conn_create,
229 	.bind_conn              = qla4xxx_conn_bind,
230 	.stop_conn              = iscsi_conn_stop,
231 	.destroy_conn           = qla4xxx_conn_destroy,
232 	.set_param              = iscsi_set_param,
233 	.get_conn_param		= qla4xxx_conn_get_param,
234 	.get_session_param	= qla4xxx_session_get_param,
235 	.get_ep_param           = qla4xxx_get_ep_param,
236 	.ep_connect		= qla4xxx_ep_connect,
237 	.ep_poll		= qla4xxx_ep_poll,
238 	.ep_disconnect		= qla4xxx_ep_disconnect,
239 	.get_stats		= qla4xxx_conn_get_stats,
240 	.send_pdu		= iscsi_conn_send_pdu,
241 	.xmit_task		= qla4xxx_task_xmit,
242 	.cleanup_task		= qla4xxx_task_cleanup,
243 	.alloc_pdu		= qla4xxx_alloc_pdu,
244 
245 	.get_host_param		= qla4xxx_host_get_param,
246 	.set_iface_param	= qla4xxx_iface_set_param,
247 	.get_iface_param	= qla4xxx_get_iface_param,
248 	.bsg_request		= qla4xxx_bsg_request,
249 	.send_ping		= qla4xxx_send_ping,
250 	.get_chap		= qla4xxx_get_chap_list,
251 	.delete_chap		= qla4xxx_delete_chap,
252 	.set_chap		= qla4xxx_set_chap_entry,
253 	.get_flashnode_param	= qla4xxx_sysfs_ddb_get_param,
254 	.set_flashnode_param	= qla4xxx_sysfs_ddb_set_param,
255 	.new_flashnode		= qla4xxx_sysfs_ddb_add,
256 	.del_flashnode		= qla4xxx_sysfs_ddb_delete,
257 	.login_flashnode	= qla4xxx_sysfs_ddb_login,
258 	.logout_flashnode	= qla4xxx_sysfs_ddb_logout,
259 	.logout_flashnode_sid	= qla4xxx_sysfs_ddb_logout_sid,
260 	.get_host_stats		= qla4xxx_get_host_stats,
261 };
262 
263 static struct scsi_transport_template *qla4xxx_scsi_transport;
264 
265 static int qla4xxx_send_ping(struct Scsi_Host *shost, uint32_t iface_num,
266 			     uint32_t iface_type, uint32_t payload_size,
267 			     uint32_t pid, struct sockaddr *dst_addr)
268 {
269 	struct scsi_qla_host *ha = to_qla_host(shost);
270 	struct sockaddr_in *addr;
271 	struct sockaddr_in6 *addr6;
272 	uint32_t options = 0;
273 	uint8_t ipaddr[IPv6_ADDR_LEN];
274 	int rval;
275 
276 	memset(ipaddr, 0, IPv6_ADDR_LEN);
277 	/* IPv4 to IPv4 */
278 	if ((iface_type == ISCSI_IFACE_TYPE_IPV4) &&
279 	    (dst_addr->sa_family == AF_INET)) {
280 		addr = (struct sockaddr_in *)dst_addr;
281 		memcpy(ipaddr, &addr->sin_addr.s_addr, IP_ADDR_LEN);
282 		DEBUG2(ql4_printk(KERN_INFO, ha, "%s: IPv4 Ping src: %pI4 "
283 				  "dest: %pI4\n", __func__,
284 				  &ha->ip_config.ip_address, ipaddr));
285 		rval = qla4xxx_ping_iocb(ha, options, payload_size, pid,
286 					 ipaddr);
287 		if (rval)
288 			rval = -EINVAL;
289 	} else if ((iface_type == ISCSI_IFACE_TYPE_IPV6) &&
290 		   (dst_addr->sa_family == AF_INET6)) {
291 		/* IPv6 to IPv6 */
292 		addr6 = (struct sockaddr_in6 *)dst_addr;
293 		memcpy(ipaddr, &addr6->sin6_addr.in6_u.u6_addr8, IPv6_ADDR_LEN);
294 
295 		options |= PING_IPV6_PROTOCOL_ENABLE;
296 
297 		/* Ping using LinkLocal address */
298 		if ((iface_num == 0) || (iface_num == 1)) {
299 			DEBUG2(ql4_printk(KERN_INFO, ha, "%s: LinkLocal Ping "
300 					  "src: %pI6 dest: %pI6\n", __func__,
301 					  &ha->ip_config.ipv6_link_local_addr,
302 					  ipaddr));
303 			options |= PING_IPV6_LINKLOCAL_ADDR;
304 			rval = qla4xxx_ping_iocb(ha, options, payload_size,
305 						 pid, ipaddr);
306 		} else {
307 			ql4_printk(KERN_WARNING, ha, "%s: iface num = %d "
308 				   "not supported\n", __func__, iface_num);
309 			rval = -ENOSYS;
310 			goto exit_send_ping;
311 		}
312 
313 		/*
314 		 * If ping using LinkLocal address fails, try ping using
315 		 * IPv6 address
316 		 */
317 		if (rval != QLA_SUCCESS) {
318 			options &= ~PING_IPV6_LINKLOCAL_ADDR;
319 			if (iface_num == 0) {
320 				options |= PING_IPV6_ADDR0;
321 				DEBUG2(ql4_printk(KERN_INFO, ha, "%s: IPv6 "
322 						  "Ping src: %pI6 "
323 						  "dest: %pI6\n", __func__,
324 						  &ha->ip_config.ipv6_addr0,
325 						  ipaddr));
326 			} else if (iface_num == 1) {
327 				options |= PING_IPV6_ADDR1;
328 				DEBUG2(ql4_printk(KERN_INFO, ha, "%s: IPv6 "
329 						  "Ping src: %pI6 "
330 						  "dest: %pI6\n", __func__,
331 						  &ha->ip_config.ipv6_addr1,
332 						  ipaddr));
333 			}
334 			rval = qla4xxx_ping_iocb(ha, options, payload_size,
335 						 pid, ipaddr);
336 			if (rval)
337 				rval = -EINVAL;
338 		}
339 	} else
340 		rval = -ENOSYS;
341 exit_send_ping:
342 	return rval;
343 }
344 
345 static umode_t qla4_attr_is_visible(int param_type, int param)
346 {
347 	switch (param_type) {
348 	case ISCSI_HOST_PARAM:
349 		switch (param) {
350 		case ISCSI_HOST_PARAM_HWADDRESS:
351 		case ISCSI_HOST_PARAM_IPADDRESS:
352 		case ISCSI_HOST_PARAM_INITIATOR_NAME:
353 		case ISCSI_HOST_PARAM_PORT_STATE:
354 		case ISCSI_HOST_PARAM_PORT_SPEED:
355 			return S_IRUGO;
356 		default:
357 			return 0;
358 		}
359 	case ISCSI_PARAM:
360 		switch (param) {
361 		case ISCSI_PARAM_PERSISTENT_ADDRESS:
362 		case ISCSI_PARAM_PERSISTENT_PORT:
363 		case ISCSI_PARAM_CONN_ADDRESS:
364 		case ISCSI_PARAM_CONN_PORT:
365 		case ISCSI_PARAM_TARGET_NAME:
366 		case ISCSI_PARAM_TPGT:
367 		case ISCSI_PARAM_TARGET_ALIAS:
368 		case ISCSI_PARAM_MAX_BURST:
369 		case ISCSI_PARAM_MAX_R2T:
370 		case ISCSI_PARAM_FIRST_BURST:
371 		case ISCSI_PARAM_MAX_RECV_DLENGTH:
372 		case ISCSI_PARAM_MAX_XMIT_DLENGTH:
373 		case ISCSI_PARAM_IFACE_NAME:
374 		case ISCSI_PARAM_CHAP_OUT_IDX:
375 		case ISCSI_PARAM_CHAP_IN_IDX:
376 		case ISCSI_PARAM_USERNAME:
377 		case ISCSI_PARAM_PASSWORD:
378 		case ISCSI_PARAM_USERNAME_IN:
379 		case ISCSI_PARAM_PASSWORD_IN:
380 		case ISCSI_PARAM_AUTO_SND_TGT_DISABLE:
381 		case ISCSI_PARAM_DISCOVERY_SESS:
382 		case ISCSI_PARAM_PORTAL_TYPE:
383 		case ISCSI_PARAM_CHAP_AUTH_EN:
384 		case ISCSI_PARAM_DISCOVERY_LOGOUT_EN:
385 		case ISCSI_PARAM_BIDI_CHAP_EN:
386 		case ISCSI_PARAM_DISCOVERY_AUTH_OPTIONAL:
387 		case ISCSI_PARAM_DEF_TIME2WAIT:
388 		case ISCSI_PARAM_DEF_TIME2RETAIN:
389 		case ISCSI_PARAM_HDRDGST_EN:
390 		case ISCSI_PARAM_DATADGST_EN:
391 		case ISCSI_PARAM_INITIAL_R2T_EN:
392 		case ISCSI_PARAM_IMM_DATA_EN:
393 		case ISCSI_PARAM_PDU_INORDER_EN:
394 		case ISCSI_PARAM_DATASEQ_INORDER_EN:
395 		case ISCSI_PARAM_MAX_SEGMENT_SIZE:
396 		case ISCSI_PARAM_TCP_TIMESTAMP_STAT:
397 		case ISCSI_PARAM_TCP_WSF_DISABLE:
398 		case ISCSI_PARAM_TCP_NAGLE_DISABLE:
399 		case ISCSI_PARAM_TCP_TIMER_SCALE:
400 		case ISCSI_PARAM_TCP_TIMESTAMP_EN:
401 		case ISCSI_PARAM_TCP_XMIT_WSF:
402 		case ISCSI_PARAM_TCP_RECV_WSF:
403 		case ISCSI_PARAM_IP_FRAGMENT_DISABLE:
404 		case ISCSI_PARAM_IPV4_TOS:
405 		case ISCSI_PARAM_IPV6_TC:
406 		case ISCSI_PARAM_IPV6_FLOW_LABEL:
407 		case ISCSI_PARAM_IS_FW_ASSIGNED_IPV6:
408 		case ISCSI_PARAM_KEEPALIVE_TMO:
409 		case ISCSI_PARAM_LOCAL_PORT:
410 		case ISCSI_PARAM_ISID:
411 		case ISCSI_PARAM_TSID:
412 		case ISCSI_PARAM_DEF_TASKMGMT_TMO:
413 		case ISCSI_PARAM_ERL:
414 		case ISCSI_PARAM_STATSN:
415 		case ISCSI_PARAM_EXP_STATSN:
416 		case ISCSI_PARAM_DISCOVERY_PARENT_IDX:
417 		case ISCSI_PARAM_DISCOVERY_PARENT_TYPE:
418 		case ISCSI_PARAM_LOCAL_IPADDR:
419 			return S_IRUGO;
420 		default:
421 			return 0;
422 		}
423 	case ISCSI_NET_PARAM:
424 		switch (param) {
425 		case ISCSI_NET_PARAM_IPV4_ADDR:
426 		case ISCSI_NET_PARAM_IPV4_SUBNET:
427 		case ISCSI_NET_PARAM_IPV4_GW:
428 		case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
429 		case ISCSI_NET_PARAM_IFACE_ENABLE:
430 		case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
431 		case ISCSI_NET_PARAM_IPV6_ADDR:
432 		case ISCSI_NET_PARAM_IPV6_ROUTER:
433 		case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
434 		case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
435 		case ISCSI_NET_PARAM_VLAN_ID:
436 		case ISCSI_NET_PARAM_VLAN_PRIORITY:
437 		case ISCSI_NET_PARAM_VLAN_ENABLED:
438 		case ISCSI_NET_PARAM_MTU:
439 		case ISCSI_NET_PARAM_PORT:
440 		case ISCSI_NET_PARAM_IPADDR_STATE:
441 		case ISCSI_NET_PARAM_IPV6_LINKLOCAL_STATE:
442 		case ISCSI_NET_PARAM_IPV6_ROUTER_STATE:
443 		case ISCSI_NET_PARAM_DELAYED_ACK_EN:
444 		case ISCSI_NET_PARAM_TCP_NAGLE_DISABLE:
445 		case ISCSI_NET_PARAM_TCP_WSF_DISABLE:
446 		case ISCSI_NET_PARAM_TCP_WSF:
447 		case ISCSI_NET_PARAM_TCP_TIMER_SCALE:
448 		case ISCSI_NET_PARAM_TCP_TIMESTAMP_EN:
449 		case ISCSI_NET_PARAM_CACHE_ID:
450 		case ISCSI_NET_PARAM_IPV4_DHCP_DNS_ADDR_EN:
451 		case ISCSI_NET_PARAM_IPV4_DHCP_SLP_DA_EN:
452 		case ISCSI_NET_PARAM_IPV4_TOS_EN:
453 		case ISCSI_NET_PARAM_IPV4_TOS:
454 		case ISCSI_NET_PARAM_IPV4_GRAT_ARP_EN:
455 		case ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID_EN:
456 		case ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID:
457 		case ISCSI_NET_PARAM_IPV4_DHCP_REQ_VENDOR_ID_EN:
458 		case ISCSI_NET_PARAM_IPV4_DHCP_USE_VENDOR_ID_EN:
459 		case ISCSI_NET_PARAM_IPV4_DHCP_VENDOR_ID:
460 		case ISCSI_NET_PARAM_IPV4_DHCP_LEARN_IQN_EN:
461 		case ISCSI_NET_PARAM_IPV4_FRAGMENT_DISABLE:
462 		case ISCSI_NET_PARAM_IPV4_IN_FORWARD_EN:
463 		case ISCSI_NET_PARAM_REDIRECT_EN:
464 		case ISCSI_NET_PARAM_IPV4_TTL:
465 		case ISCSI_NET_PARAM_IPV6_GRAT_NEIGHBOR_ADV_EN:
466 		case ISCSI_NET_PARAM_IPV6_MLD_EN:
467 		case ISCSI_NET_PARAM_IPV6_FLOW_LABEL:
468 		case ISCSI_NET_PARAM_IPV6_TRAFFIC_CLASS:
469 		case ISCSI_NET_PARAM_IPV6_HOP_LIMIT:
470 		case ISCSI_NET_PARAM_IPV6_ND_REACHABLE_TMO:
471 		case ISCSI_NET_PARAM_IPV6_ND_REXMIT_TIME:
472 		case ISCSI_NET_PARAM_IPV6_ND_STALE_TMO:
473 		case ISCSI_NET_PARAM_IPV6_DUP_ADDR_DETECT_CNT:
474 		case ISCSI_NET_PARAM_IPV6_RTR_ADV_LINK_MTU:
475 			return S_IRUGO;
476 		default:
477 			return 0;
478 		}
479 	case ISCSI_IFACE_PARAM:
480 		switch (param) {
481 		case ISCSI_IFACE_PARAM_DEF_TASKMGMT_TMO:
482 		case ISCSI_IFACE_PARAM_HDRDGST_EN:
483 		case ISCSI_IFACE_PARAM_DATADGST_EN:
484 		case ISCSI_IFACE_PARAM_IMM_DATA_EN:
485 		case ISCSI_IFACE_PARAM_INITIAL_R2T_EN:
486 		case ISCSI_IFACE_PARAM_DATASEQ_INORDER_EN:
487 		case ISCSI_IFACE_PARAM_PDU_INORDER_EN:
488 		case ISCSI_IFACE_PARAM_ERL:
489 		case ISCSI_IFACE_PARAM_MAX_RECV_DLENGTH:
490 		case ISCSI_IFACE_PARAM_FIRST_BURST:
491 		case ISCSI_IFACE_PARAM_MAX_R2T:
492 		case ISCSI_IFACE_PARAM_MAX_BURST:
493 		case ISCSI_IFACE_PARAM_CHAP_AUTH_EN:
494 		case ISCSI_IFACE_PARAM_BIDI_CHAP_EN:
495 		case ISCSI_IFACE_PARAM_DISCOVERY_AUTH_OPTIONAL:
496 		case ISCSI_IFACE_PARAM_DISCOVERY_LOGOUT_EN:
497 		case ISCSI_IFACE_PARAM_STRICT_LOGIN_COMP_EN:
498 		case ISCSI_IFACE_PARAM_INITIATOR_NAME:
499 			return S_IRUGO;
500 		default:
501 			return 0;
502 		}
503 	case ISCSI_FLASHNODE_PARAM:
504 		switch (param) {
505 		case ISCSI_FLASHNODE_IS_FW_ASSIGNED_IPV6:
506 		case ISCSI_FLASHNODE_PORTAL_TYPE:
507 		case ISCSI_FLASHNODE_AUTO_SND_TGT_DISABLE:
508 		case ISCSI_FLASHNODE_DISCOVERY_SESS:
509 		case ISCSI_FLASHNODE_ENTRY_EN:
510 		case ISCSI_FLASHNODE_HDR_DGST_EN:
511 		case ISCSI_FLASHNODE_DATA_DGST_EN:
512 		case ISCSI_FLASHNODE_IMM_DATA_EN:
513 		case ISCSI_FLASHNODE_INITIAL_R2T_EN:
514 		case ISCSI_FLASHNODE_DATASEQ_INORDER:
515 		case ISCSI_FLASHNODE_PDU_INORDER:
516 		case ISCSI_FLASHNODE_CHAP_AUTH_EN:
517 		case ISCSI_FLASHNODE_SNACK_REQ_EN:
518 		case ISCSI_FLASHNODE_DISCOVERY_LOGOUT_EN:
519 		case ISCSI_FLASHNODE_BIDI_CHAP_EN:
520 		case ISCSI_FLASHNODE_DISCOVERY_AUTH_OPTIONAL:
521 		case ISCSI_FLASHNODE_ERL:
522 		case ISCSI_FLASHNODE_TCP_TIMESTAMP_STAT:
523 		case ISCSI_FLASHNODE_TCP_NAGLE_DISABLE:
524 		case ISCSI_FLASHNODE_TCP_WSF_DISABLE:
525 		case ISCSI_FLASHNODE_TCP_TIMER_SCALE:
526 		case ISCSI_FLASHNODE_TCP_TIMESTAMP_EN:
527 		case ISCSI_FLASHNODE_IP_FRAG_DISABLE:
528 		case ISCSI_FLASHNODE_MAX_RECV_DLENGTH:
529 		case ISCSI_FLASHNODE_MAX_XMIT_DLENGTH:
530 		case ISCSI_FLASHNODE_FIRST_BURST:
531 		case ISCSI_FLASHNODE_DEF_TIME2WAIT:
532 		case ISCSI_FLASHNODE_DEF_TIME2RETAIN:
533 		case ISCSI_FLASHNODE_MAX_R2T:
534 		case ISCSI_FLASHNODE_KEEPALIVE_TMO:
535 		case ISCSI_FLASHNODE_ISID:
536 		case ISCSI_FLASHNODE_TSID:
537 		case ISCSI_FLASHNODE_PORT:
538 		case ISCSI_FLASHNODE_MAX_BURST:
539 		case ISCSI_FLASHNODE_DEF_TASKMGMT_TMO:
540 		case ISCSI_FLASHNODE_IPADDR:
541 		case ISCSI_FLASHNODE_ALIAS:
542 		case ISCSI_FLASHNODE_REDIRECT_IPADDR:
543 		case ISCSI_FLASHNODE_MAX_SEGMENT_SIZE:
544 		case ISCSI_FLASHNODE_LOCAL_PORT:
545 		case ISCSI_FLASHNODE_IPV4_TOS:
546 		case ISCSI_FLASHNODE_IPV6_TC:
547 		case ISCSI_FLASHNODE_IPV6_FLOW_LABEL:
548 		case ISCSI_FLASHNODE_NAME:
549 		case ISCSI_FLASHNODE_TPGT:
550 		case ISCSI_FLASHNODE_LINK_LOCAL_IPV6:
551 		case ISCSI_FLASHNODE_DISCOVERY_PARENT_IDX:
552 		case ISCSI_FLASHNODE_DISCOVERY_PARENT_TYPE:
553 		case ISCSI_FLASHNODE_TCP_XMIT_WSF:
554 		case ISCSI_FLASHNODE_TCP_RECV_WSF:
555 		case ISCSI_FLASHNODE_CHAP_OUT_IDX:
556 		case ISCSI_FLASHNODE_USERNAME:
557 		case ISCSI_FLASHNODE_PASSWORD:
558 		case ISCSI_FLASHNODE_STATSN:
559 		case ISCSI_FLASHNODE_EXP_STATSN:
560 		case ISCSI_FLASHNODE_IS_BOOT_TGT:
561 			return S_IRUGO;
562 		default:
563 			return 0;
564 		}
565 	}
566 
567 	return 0;
568 }
569 
570 /**
571  * qla4xxx_create chap_list - Create CHAP list from FLASH
572  * @ha: pointer to adapter structure
573  *
574  * Read flash and make a list of CHAP entries, during login when a CHAP entry
575  * is received, it will be checked in this list. If entry exist then the CHAP
576  * entry index is set in the DDB. If CHAP entry does not exist in this list
577  * then a new entry is added in FLASH in CHAP table and the index obtained is
578  * used in the DDB.
579  **/
580 static void qla4xxx_create_chap_list(struct scsi_qla_host *ha)
581 {
582 	int rval = 0;
583 	uint8_t *chap_flash_data = NULL;
584 	uint32_t offset;
585 	dma_addr_t chap_dma;
586 	uint32_t chap_size = 0;
587 
588 	if (is_qla40XX(ha))
589 		chap_size = MAX_CHAP_ENTRIES_40XX *
590 			    sizeof(struct ql4_chap_table);
591 	else	/* Single region contains CHAP info for both
592 		 * ports which is divided into half for each port.
593 		 */
594 		chap_size = ha->hw.flt_chap_size / 2;
595 
596 	chap_flash_data = dma_alloc_coherent(&ha->pdev->dev, chap_size,
597 					     &chap_dma, GFP_KERNEL);
598 	if (!chap_flash_data) {
599 		ql4_printk(KERN_ERR, ha, "No memory for chap_flash_data\n");
600 		return;
601 	}
602 
603 	if (is_qla40XX(ha)) {
604 		offset = FLASH_CHAP_OFFSET;
605 	} else {
606 		offset = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_region_chap << 2);
607 		if (ha->port_num == 1)
608 			offset += chap_size;
609 	}
610 
611 	rval = qla4xxx_get_flash(ha, chap_dma, offset, chap_size);
612 	if (rval != QLA_SUCCESS)
613 		goto exit_chap_list;
614 
615 	if (ha->chap_list == NULL)
616 		ha->chap_list = vmalloc(chap_size);
617 	if (ha->chap_list == NULL) {
618 		ql4_printk(KERN_ERR, ha, "No memory for ha->chap_list\n");
619 		goto exit_chap_list;
620 	}
621 
622 	memset(ha->chap_list, 0, chap_size);
623 	memcpy(ha->chap_list, chap_flash_data, chap_size);
624 
625 exit_chap_list:
626 	dma_free_coherent(&ha->pdev->dev, chap_size, chap_flash_data, chap_dma);
627 }
628 
629 static int qla4xxx_get_chap_by_index(struct scsi_qla_host *ha,
630 				     int16_t chap_index,
631 				     struct ql4_chap_table **chap_entry)
632 {
633 	int rval = QLA_ERROR;
634 	int max_chap_entries;
635 
636 	if (!ha->chap_list) {
637 		ql4_printk(KERN_ERR, ha, "CHAP table cache is empty!\n");
638 		rval = QLA_ERROR;
639 		goto exit_get_chap;
640 	}
641 
642 	if (is_qla80XX(ha))
643 		max_chap_entries = (ha->hw.flt_chap_size / 2) /
644 				   sizeof(struct ql4_chap_table);
645 	else
646 		max_chap_entries = MAX_CHAP_ENTRIES_40XX;
647 
648 	if (chap_index > max_chap_entries) {
649 		ql4_printk(KERN_ERR, ha, "Invalid Chap index\n");
650 		rval = QLA_ERROR;
651 		goto exit_get_chap;
652 	}
653 
654 	*chap_entry = (struct ql4_chap_table *)ha->chap_list + chap_index;
655 	if ((*chap_entry)->cookie !=
656 	     __constant_cpu_to_le16(CHAP_VALID_COOKIE)) {
657 		rval = QLA_ERROR;
658 		*chap_entry = NULL;
659 	} else {
660 		rval = QLA_SUCCESS;
661 	}
662 
663 exit_get_chap:
664 	return rval;
665 }
666 
667 /**
668  * qla4xxx_find_free_chap_index - Find the first free chap index
669  * @ha: pointer to adapter structure
670  * @chap_index: CHAP index to be returned
671  *
672  * Find the first free chap index available in the chap table
673  *
674  * Note: Caller should acquire the chap lock before getting here.
675  **/
676 static int qla4xxx_find_free_chap_index(struct scsi_qla_host *ha,
677 					uint16_t *chap_index)
678 {
679 	int i, rval;
680 	int free_index = -1;
681 	int max_chap_entries = 0;
682 	struct ql4_chap_table *chap_table;
683 
684 	if (is_qla80XX(ha))
685 		max_chap_entries = (ha->hw.flt_chap_size / 2) /
686 						sizeof(struct ql4_chap_table);
687 	else
688 		max_chap_entries = MAX_CHAP_ENTRIES_40XX;
689 
690 	if (!ha->chap_list) {
691 		ql4_printk(KERN_ERR, ha, "CHAP table cache is empty!\n");
692 		rval = QLA_ERROR;
693 		goto exit_find_chap;
694 	}
695 
696 	for (i = 0; i < max_chap_entries; i++) {
697 		chap_table = (struct ql4_chap_table *)ha->chap_list + i;
698 
699 		if ((chap_table->cookie !=
700 		    __constant_cpu_to_le16(CHAP_VALID_COOKIE)) &&
701 		   (i > MAX_RESRV_CHAP_IDX)) {
702 				free_index = i;
703 				break;
704 		}
705 	}
706 
707 	if (free_index != -1) {
708 		*chap_index = free_index;
709 		rval = QLA_SUCCESS;
710 	} else {
711 		rval = QLA_ERROR;
712 	}
713 
714 exit_find_chap:
715 	return rval;
716 }
717 
718 static int qla4xxx_get_chap_list(struct Scsi_Host *shost, uint16_t chap_tbl_idx,
719 				  uint32_t *num_entries, char *buf)
720 {
721 	struct scsi_qla_host *ha = to_qla_host(shost);
722 	struct ql4_chap_table *chap_table;
723 	struct iscsi_chap_rec *chap_rec;
724 	int max_chap_entries = 0;
725 	int valid_chap_entries = 0;
726 	int ret = 0, i;
727 
728 	if (is_qla80XX(ha))
729 		max_chap_entries = (ha->hw.flt_chap_size / 2) /
730 					sizeof(struct ql4_chap_table);
731 	else
732 		max_chap_entries = MAX_CHAP_ENTRIES_40XX;
733 
734 	ql4_printk(KERN_INFO, ha, "%s: num_entries = %d, CHAP idx = %d\n",
735 			__func__, *num_entries, chap_tbl_idx);
736 
737 	if (!buf) {
738 		ret = -ENOMEM;
739 		goto exit_get_chap_list;
740 	}
741 
742 	qla4xxx_create_chap_list(ha);
743 
744 	chap_rec = (struct iscsi_chap_rec *) buf;
745 	mutex_lock(&ha->chap_sem);
746 	for (i = chap_tbl_idx; i < max_chap_entries; i++) {
747 		chap_table = (struct ql4_chap_table *)ha->chap_list + i;
748 		if (chap_table->cookie !=
749 		    __constant_cpu_to_le16(CHAP_VALID_COOKIE))
750 			continue;
751 
752 		chap_rec->chap_tbl_idx = i;
753 		strlcpy(chap_rec->username, chap_table->name,
754 			ISCSI_CHAP_AUTH_NAME_MAX_LEN);
755 		strlcpy(chap_rec->password, chap_table->secret,
756 			QL4_CHAP_MAX_SECRET_LEN);
757 		chap_rec->password_length = chap_table->secret_len;
758 
759 		if (chap_table->flags & BIT_7) /* local */
760 			chap_rec->chap_type = CHAP_TYPE_OUT;
761 
762 		if (chap_table->flags & BIT_6) /* peer */
763 			chap_rec->chap_type = CHAP_TYPE_IN;
764 
765 		chap_rec++;
766 
767 		valid_chap_entries++;
768 		if (valid_chap_entries == *num_entries)
769 			break;
770 		else
771 			continue;
772 	}
773 	mutex_unlock(&ha->chap_sem);
774 
775 exit_get_chap_list:
776 	ql4_printk(KERN_INFO, ha, "%s: Valid CHAP Entries = %d\n",
777 			__func__,  valid_chap_entries);
778 	*num_entries = valid_chap_entries;
779 	return ret;
780 }
781 
782 static int __qla4xxx_is_chap_active(struct device *dev, void *data)
783 {
784 	int ret = 0;
785 	uint16_t *chap_tbl_idx = (uint16_t *) data;
786 	struct iscsi_cls_session *cls_session;
787 	struct iscsi_session *sess;
788 	struct ddb_entry *ddb_entry;
789 
790 	if (!iscsi_is_session_dev(dev))
791 		goto exit_is_chap_active;
792 
793 	cls_session = iscsi_dev_to_session(dev);
794 	sess = cls_session->dd_data;
795 	ddb_entry = sess->dd_data;
796 
797 	if (iscsi_session_chkready(cls_session))
798 		goto exit_is_chap_active;
799 
800 	if (ddb_entry->chap_tbl_idx == *chap_tbl_idx)
801 		ret = 1;
802 
803 exit_is_chap_active:
804 	return ret;
805 }
806 
807 static int qla4xxx_is_chap_active(struct Scsi_Host *shost,
808 				  uint16_t chap_tbl_idx)
809 {
810 	int ret = 0;
811 
812 	ret = device_for_each_child(&shost->shost_gendev, &chap_tbl_idx,
813 				    __qla4xxx_is_chap_active);
814 
815 	return ret;
816 }
817 
818 static int qla4xxx_delete_chap(struct Scsi_Host *shost, uint16_t chap_tbl_idx)
819 {
820 	struct scsi_qla_host *ha = to_qla_host(shost);
821 	struct ql4_chap_table *chap_table;
822 	dma_addr_t chap_dma;
823 	int max_chap_entries = 0;
824 	uint32_t offset = 0;
825 	uint32_t chap_size;
826 	int ret = 0;
827 
828 	chap_table = dma_pool_alloc(ha->chap_dma_pool, GFP_KERNEL, &chap_dma);
829 	if (chap_table == NULL)
830 		return -ENOMEM;
831 
832 	memset(chap_table, 0, sizeof(struct ql4_chap_table));
833 
834 	if (is_qla80XX(ha))
835 		max_chap_entries = (ha->hw.flt_chap_size / 2) /
836 				   sizeof(struct ql4_chap_table);
837 	else
838 		max_chap_entries = MAX_CHAP_ENTRIES_40XX;
839 
840 	if (chap_tbl_idx > max_chap_entries) {
841 		ret = -EINVAL;
842 		goto exit_delete_chap;
843 	}
844 
845 	/* Check if chap index is in use.
846 	 * If chap is in use don't delet chap entry */
847 	ret = qla4xxx_is_chap_active(shost, chap_tbl_idx);
848 	if (ret) {
849 		ql4_printk(KERN_INFO, ha, "CHAP entry %d is in use, cannot "
850 			   "delete from flash\n", chap_tbl_idx);
851 		ret = -EBUSY;
852 		goto exit_delete_chap;
853 	}
854 
855 	chap_size = sizeof(struct ql4_chap_table);
856 	if (is_qla40XX(ha))
857 		offset = FLASH_CHAP_OFFSET | (chap_tbl_idx * chap_size);
858 	else {
859 		offset = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_region_chap << 2);
860 		/* flt_chap_size is CHAP table size for both ports
861 		 * so divide it by 2 to calculate the offset for second port
862 		 */
863 		if (ha->port_num == 1)
864 			offset += (ha->hw.flt_chap_size / 2);
865 		offset += (chap_tbl_idx * chap_size);
866 	}
867 
868 	ret = qla4xxx_get_flash(ha, chap_dma, offset, chap_size);
869 	if (ret != QLA_SUCCESS) {
870 		ret = -EINVAL;
871 		goto exit_delete_chap;
872 	}
873 
874 	DEBUG2(ql4_printk(KERN_INFO, ha, "Chap Cookie: x%x\n",
875 			  __le16_to_cpu(chap_table->cookie)));
876 
877 	if (__le16_to_cpu(chap_table->cookie) != CHAP_VALID_COOKIE) {
878 		ql4_printk(KERN_ERR, ha, "No valid chap entry found\n");
879 		goto exit_delete_chap;
880 	}
881 
882 	chap_table->cookie = __constant_cpu_to_le16(0xFFFF);
883 
884 	offset = FLASH_CHAP_OFFSET |
885 			(chap_tbl_idx * sizeof(struct ql4_chap_table));
886 	ret = qla4xxx_set_flash(ha, chap_dma, offset, chap_size,
887 				FLASH_OPT_RMW_COMMIT);
888 	if (ret == QLA_SUCCESS && ha->chap_list) {
889 		mutex_lock(&ha->chap_sem);
890 		/* Update ha chap_list cache */
891 		memcpy((struct ql4_chap_table *)ha->chap_list + chap_tbl_idx,
892 			chap_table, sizeof(struct ql4_chap_table));
893 		mutex_unlock(&ha->chap_sem);
894 	}
895 	if (ret != QLA_SUCCESS)
896 		ret =  -EINVAL;
897 
898 exit_delete_chap:
899 	dma_pool_free(ha->chap_dma_pool, chap_table, chap_dma);
900 	return ret;
901 }
902 
903 /**
904  * qla4xxx_set_chap_entry - Make chap entry with given information
905  * @shost: pointer to host
906  * @data: chap info - credentials, index and type to make chap entry
907  * @len: length of data
908  *
909  * Add or update chap entry with the given information
910  **/
911 static int qla4xxx_set_chap_entry(struct Scsi_Host *shost, void *data, int len)
912 {
913 	struct scsi_qla_host *ha = to_qla_host(shost);
914 	struct iscsi_chap_rec chap_rec;
915 	struct ql4_chap_table *chap_entry = NULL;
916 	struct iscsi_param_info *param_info;
917 	struct nlattr *attr;
918 	int max_chap_entries = 0;
919 	int type;
920 	int rem = len;
921 	int rc = 0;
922 	int size;
923 
924 	memset(&chap_rec, 0, sizeof(chap_rec));
925 
926 	nla_for_each_attr(attr, data, len, rem) {
927 		param_info = nla_data(attr);
928 
929 		switch (param_info->param) {
930 		case ISCSI_CHAP_PARAM_INDEX:
931 			chap_rec.chap_tbl_idx = *(uint16_t *)param_info->value;
932 			break;
933 		case ISCSI_CHAP_PARAM_CHAP_TYPE:
934 			chap_rec.chap_type = param_info->value[0];
935 			break;
936 		case ISCSI_CHAP_PARAM_USERNAME:
937 			size = min_t(size_t, sizeof(chap_rec.username),
938 				     param_info->len);
939 			memcpy(chap_rec.username, param_info->value, size);
940 			break;
941 		case ISCSI_CHAP_PARAM_PASSWORD:
942 			size = min_t(size_t, sizeof(chap_rec.password),
943 				     param_info->len);
944 			memcpy(chap_rec.password, param_info->value, size);
945 			break;
946 		case ISCSI_CHAP_PARAM_PASSWORD_LEN:
947 			chap_rec.password_length = param_info->value[0];
948 			break;
949 		default:
950 			ql4_printk(KERN_ERR, ha,
951 				   "%s: No such sysfs attribute\n", __func__);
952 			rc = -ENOSYS;
953 			goto exit_set_chap;
954 		};
955 	}
956 
957 	if (chap_rec.chap_type == CHAP_TYPE_IN)
958 		type = BIDI_CHAP;
959 	else
960 		type = LOCAL_CHAP;
961 
962 	if (is_qla80XX(ha))
963 		max_chap_entries = (ha->hw.flt_chap_size / 2) /
964 				   sizeof(struct ql4_chap_table);
965 	else
966 		max_chap_entries = MAX_CHAP_ENTRIES_40XX;
967 
968 	mutex_lock(&ha->chap_sem);
969 	if (chap_rec.chap_tbl_idx < max_chap_entries) {
970 		rc = qla4xxx_get_chap_by_index(ha, chap_rec.chap_tbl_idx,
971 					       &chap_entry);
972 		if (!rc) {
973 			if (!(type == qla4xxx_get_chap_type(chap_entry))) {
974 				ql4_printk(KERN_INFO, ha,
975 					   "Type mismatch for CHAP entry %d\n",
976 					   chap_rec.chap_tbl_idx);
977 				rc = -EINVAL;
978 				goto exit_unlock_chap;
979 			}
980 
981 			/* If chap index is in use then don't modify it */
982 			rc = qla4xxx_is_chap_active(shost,
983 						    chap_rec.chap_tbl_idx);
984 			if (rc) {
985 				ql4_printk(KERN_INFO, ha,
986 					   "CHAP entry %d is in use\n",
987 					   chap_rec.chap_tbl_idx);
988 				rc = -EBUSY;
989 				goto exit_unlock_chap;
990 			}
991 		}
992 	} else {
993 		rc = qla4xxx_find_free_chap_index(ha, &chap_rec.chap_tbl_idx);
994 		if (rc) {
995 			ql4_printk(KERN_INFO, ha, "CHAP entry not available\n");
996 			rc = -EBUSY;
997 			goto exit_unlock_chap;
998 		}
999 	}
1000 
1001 	rc = qla4xxx_set_chap(ha, chap_rec.username, chap_rec.password,
1002 			      chap_rec.chap_tbl_idx, type);
1003 
1004 exit_unlock_chap:
1005 	mutex_unlock(&ha->chap_sem);
1006 
1007 exit_set_chap:
1008 	return rc;
1009 }
1010 
1011 
1012 static int qla4xxx_get_host_stats(struct Scsi_Host *shost, char *buf, int len)
1013 {
1014 	struct scsi_qla_host *ha = to_qla_host(shost);
1015 	struct iscsi_offload_host_stats *host_stats = NULL;
1016 	int host_stats_size;
1017 	int ret = 0;
1018 	int ddb_idx = 0;
1019 	struct ql_iscsi_stats *ql_iscsi_stats = NULL;
1020 	int stats_size;
1021 	dma_addr_t iscsi_stats_dma;
1022 
1023 	DEBUG2(ql4_printk(KERN_INFO, ha, "Func: %s\n", __func__));
1024 
1025 	host_stats_size = sizeof(struct iscsi_offload_host_stats);
1026 
1027 	if (host_stats_size != len) {
1028 		ql4_printk(KERN_INFO, ha, "%s: host_stats size mismatch expected = %d, is = %d\n",
1029 			   __func__, len, host_stats_size);
1030 		ret = -EINVAL;
1031 		goto exit_host_stats;
1032 	}
1033 	host_stats = (struct iscsi_offload_host_stats *)buf;
1034 
1035 	if (!buf) {
1036 		ret = -ENOMEM;
1037 		goto exit_host_stats;
1038 	}
1039 
1040 	stats_size = PAGE_ALIGN(sizeof(struct ql_iscsi_stats));
1041 
1042 	ql_iscsi_stats = dma_alloc_coherent(&ha->pdev->dev, stats_size,
1043 					    &iscsi_stats_dma, GFP_KERNEL);
1044 	if (!ql_iscsi_stats) {
1045 		ql4_printk(KERN_ERR, ha,
1046 			   "Unable to allocate memory for iscsi stats\n");
1047 		ret = -ENOMEM;
1048 		goto exit_host_stats;
1049 	}
1050 
1051 	ret =  qla4xxx_get_mgmt_data(ha, ddb_idx, stats_size,
1052 				     iscsi_stats_dma);
1053 	if (ret != QLA_SUCCESS) {
1054 		ql4_printk(KERN_ERR, ha,
1055 			   "Unable to retrieve iscsi stats\n");
1056 		ret = -EIO;
1057 		goto exit_host_stats;
1058 	}
1059 	host_stats->mactx_frames = le64_to_cpu(ql_iscsi_stats->mac_tx_frames);
1060 	host_stats->mactx_bytes = le64_to_cpu(ql_iscsi_stats->mac_tx_bytes);
1061 	host_stats->mactx_multicast_frames =
1062 			le64_to_cpu(ql_iscsi_stats->mac_tx_multicast_frames);
1063 	host_stats->mactx_broadcast_frames =
1064 			le64_to_cpu(ql_iscsi_stats->mac_tx_broadcast_frames);
1065 	host_stats->mactx_pause_frames =
1066 			le64_to_cpu(ql_iscsi_stats->mac_tx_pause_frames);
1067 	host_stats->mactx_control_frames =
1068 			le64_to_cpu(ql_iscsi_stats->mac_tx_control_frames);
1069 	host_stats->mactx_deferral =
1070 			le64_to_cpu(ql_iscsi_stats->mac_tx_deferral);
1071 	host_stats->mactx_excess_deferral =
1072 			le64_to_cpu(ql_iscsi_stats->mac_tx_excess_deferral);
1073 	host_stats->mactx_late_collision =
1074 			le64_to_cpu(ql_iscsi_stats->mac_tx_late_collision);
1075 	host_stats->mactx_abort	= le64_to_cpu(ql_iscsi_stats->mac_tx_abort);
1076 	host_stats->mactx_single_collision =
1077 			le64_to_cpu(ql_iscsi_stats->mac_tx_single_collision);
1078 	host_stats->mactx_multiple_collision =
1079 			le64_to_cpu(ql_iscsi_stats->mac_tx_multiple_collision);
1080 	host_stats->mactx_collision =
1081 			le64_to_cpu(ql_iscsi_stats->mac_tx_collision);
1082 	host_stats->mactx_frames_dropped =
1083 			le64_to_cpu(ql_iscsi_stats->mac_tx_frames_dropped);
1084 	host_stats->mactx_jumbo_frames =
1085 			le64_to_cpu(ql_iscsi_stats->mac_tx_jumbo_frames);
1086 	host_stats->macrx_frames = le64_to_cpu(ql_iscsi_stats->mac_rx_frames);
1087 	host_stats->macrx_bytes = le64_to_cpu(ql_iscsi_stats->mac_rx_bytes);
1088 	host_stats->macrx_unknown_control_frames =
1089 		le64_to_cpu(ql_iscsi_stats->mac_rx_unknown_control_frames);
1090 	host_stats->macrx_pause_frames =
1091 			le64_to_cpu(ql_iscsi_stats->mac_rx_pause_frames);
1092 	host_stats->macrx_control_frames =
1093 			le64_to_cpu(ql_iscsi_stats->mac_rx_control_frames);
1094 	host_stats->macrx_dribble =
1095 			le64_to_cpu(ql_iscsi_stats->mac_rx_dribble);
1096 	host_stats->macrx_frame_length_error =
1097 			le64_to_cpu(ql_iscsi_stats->mac_rx_frame_length_error);
1098 	host_stats->macrx_jabber = le64_to_cpu(ql_iscsi_stats->mac_rx_jabber);
1099 	host_stats->macrx_carrier_sense_error =
1100 		le64_to_cpu(ql_iscsi_stats->mac_rx_carrier_sense_error);
1101 	host_stats->macrx_frame_discarded =
1102 			le64_to_cpu(ql_iscsi_stats->mac_rx_frame_discarded);
1103 	host_stats->macrx_frames_dropped =
1104 			le64_to_cpu(ql_iscsi_stats->mac_rx_frames_dropped);
1105 	host_stats->mac_crc_error = le64_to_cpu(ql_iscsi_stats->mac_crc_error);
1106 	host_stats->mac_encoding_error =
1107 			le64_to_cpu(ql_iscsi_stats->mac_encoding_error);
1108 	host_stats->macrx_length_error_large =
1109 			le64_to_cpu(ql_iscsi_stats->mac_rx_length_error_large);
1110 	host_stats->macrx_length_error_small =
1111 			le64_to_cpu(ql_iscsi_stats->mac_rx_length_error_small);
1112 	host_stats->macrx_multicast_frames =
1113 			le64_to_cpu(ql_iscsi_stats->mac_rx_multicast_frames);
1114 	host_stats->macrx_broadcast_frames =
1115 			le64_to_cpu(ql_iscsi_stats->mac_rx_broadcast_frames);
1116 	host_stats->iptx_packets = le64_to_cpu(ql_iscsi_stats->ip_tx_packets);
1117 	host_stats->iptx_bytes = le64_to_cpu(ql_iscsi_stats->ip_tx_bytes);
1118 	host_stats->iptx_fragments =
1119 			le64_to_cpu(ql_iscsi_stats->ip_tx_fragments);
1120 	host_stats->iprx_packets = le64_to_cpu(ql_iscsi_stats->ip_rx_packets);
1121 	host_stats->iprx_bytes = le64_to_cpu(ql_iscsi_stats->ip_rx_bytes);
1122 	host_stats->iprx_fragments =
1123 			le64_to_cpu(ql_iscsi_stats->ip_rx_fragments);
1124 	host_stats->ip_datagram_reassembly =
1125 			le64_to_cpu(ql_iscsi_stats->ip_datagram_reassembly);
1126 	host_stats->ip_invalid_address_error =
1127 			le64_to_cpu(ql_iscsi_stats->ip_invalid_address_error);
1128 	host_stats->ip_error_packets =
1129 			le64_to_cpu(ql_iscsi_stats->ip_error_packets);
1130 	host_stats->ip_fragrx_overlap =
1131 			le64_to_cpu(ql_iscsi_stats->ip_fragrx_overlap);
1132 	host_stats->ip_fragrx_outoforder =
1133 			le64_to_cpu(ql_iscsi_stats->ip_fragrx_outoforder);
1134 	host_stats->ip_datagram_reassembly_timeout =
1135 		le64_to_cpu(ql_iscsi_stats->ip_datagram_reassembly_timeout);
1136 	host_stats->ipv6tx_packets =
1137 			le64_to_cpu(ql_iscsi_stats->ipv6_tx_packets);
1138 	host_stats->ipv6tx_bytes = le64_to_cpu(ql_iscsi_stats->ipv6_tx_bytes);
1139 	host_stats->ipv6tx_fragments =
1140 			le64_to_cpu(ql_iscsi_stats->ipv6_tx_fragments);
1141 	host_stats->ipv6rx_packets =
1142 			le64_to_cpu(ql_iscsi_stats->ipv6_rx_packets);
1143 	host_stats->ipv6rx_bytes = le64_to_cpu(ql_iscsi_stats->ipv6_rx_bytes);
1144 	host_stats->ipv6rx_fragments =
1145 			le64_to_cpu(ql_iscsi_stats->ipv6_rx_fragments);
1146 	host_stats->ipv6_datagram_reassembly =
1147 			le64_to_cpu(ql_iscsi_stats->ipv6_datagram_reassembly);
1148 	host_stats->ipv6_invalid_address_error =
1149 		le64_to_cpu(ql_iscsi_stats->ipv6_invalid_address_error);
1150 	host_stats->ipv6_error_packets =
1151 			le64_to_cpu(ql_iscsi_stats->ipv6_error_packets);
1152 	host_stats->ipv6_fragrx_overlap =
1153 			le64_to_cpu(ql_iscsi_stats->ipv6_fragrx_overlap);
1154 	host_stats->ipv6_fragrx_outoforder =
1155 			le64_to_cpu(ql_iscsi_stats->ipv6_fragrx_outoforder);
1156 	host_stats->ipv6_datagram_reassembly_timeout =
1157 		le64_to_cpu(ql_iscsi_stats->ipv6_datagram_reassembly_timeout);
1158 	host_stats->tcptx_segments =
1159 			le64_to_cpu(ql_iscsi_stats->tcp_tx_segments);
1160 	host_stats->tcptx_bytes	= le64_to_cpu(ql_iscsi_stats->tcp_tx_bytes);
1161 	host_stats->tcprx_segments =
1162 			le64_to_cpu(ql_iscsi_stats->tcp_rx_segments);
1163 	host_stats->tcprx_byte = le64_to_cpu(ql_iscsi_stats->tcp_rx_byte);
1164 	host_stats->tcp_duplicate_ack_retx =
1165 			le64_to_cpu(ql_iscsi_stats->tcp_duplicate_ack_retx);
1166 	host_stats->tcp_retx_timer_expired =
1167 			le64_to_cpu(ql_iscsi_stats->tcp_retx_timer_expired);
1168 	host_stats->tcprx_duplicate_ack	=
1169 			le64_to_cpu(ql_iscsi_stats->tcp_rx_duplicate_ack);
1170 	host_stats->tcprx_pure_ackr =
1171 			le64_to_cpu(ql_iscsi_stats->tcp_rx_pure_ackr);
1172 	host_stats->tcptx_delayed_ack =
1173 			le64_to_cpu(ql_iscsi_stats->tcp_tx_delayed_ack);
1174 	host_stats->tcptx_pure_ack =
1175 			le64_to_cpu(ql_iscsi_stats->tcp_tx_pure_ack);
1176 	host_stats->tcprx_segment_error =
1177 			le64_to_cpu(ql_iscsi_stats->tcp_rx_segment_error);
1178 	host_stats->tcprx_segment_outoforder =
1179 			le64_to_cpu(ql_iscsi_stats->tcp_rx_segment_outoforder);
1180 	host_stats->tcprx_window_probe =
1181 			le64_to_cpu(ql_iscsi_stats->tcp_rx_window_probe);
1182 	host_stats->tcprx_window_update =
1183 			le64_to_cpu(ql_iscsi_stats->tcp_rx_window_update);
1184 	host_stats->tcptx_window_probe_persist =
1185 		le64_to_cpu(ql_iscsi_stats->tcp_tx_window_probe_persist);
1186 	host_stats->ecc_error_correction =
1187 			le64_to_cpu(ql_iscsi_stats->ecc_error_correction);
1188 	host_stats->iscsi_pdu_tx = le64_to_cpu(ql_iscsi_stats->iscsi_pdu_tx);
1189 	host_stats->iscsi_data_bytes_tx =
1190 			le64_to_cpu(ql_iscsi_stats->iscsi_data_bytes_tx);
1191 	host_stats->iscsi_pdu_rx = le64_to_cpu(ql_iscsi_stats->iscsi_pdu_rx);
1192 	host_stats->iscsi_data_bytes_rx	=
1193 			le64_to_cpu(ql_iscsi_stats->iscsi_data_bytes_rx);
1194 	host_stats->iscsi_io_completed =
1195 			le64_to_cpu(ql_iscsi_stats->iscsi_io_completed);
1196 	host_stats->iscsi_unexpected_io_rx =
1197 			le64_to_cpu(ql_iscsi_stats->iscsi_unexpected_io_rx);
1198 	host_stats->iscsi_format_error =
1199 			le64_to_cpu(ql_iscsi_stats->iscsi_format_error);
1200 	host_stats->iscsi_hdr_digest_error =
1201 			le64_to_cpu(ql_iscsi_stats->iscsi_hdr_digest_error);
1202 	host_stats->iscsi_data_digest_error =
1203 			le64_to_cpu(ql_iscsi_stats->iscsi_data_digest_error);
1204 	host_stats->iscsi_sequence_error =
1205 			le64_to_cpu(ql_iscsi_stats->iscsi_sequence_error);
1206 exit_host_stats:
1207 	if (ql_iscsi_stats)
1208 		dma_free_coherent(&ha->pdev->dev, host_stats_size,
1209 				  ql_iscsi_stats, iscsi_stats_dma);
1210 
1211 	ql4_printk(KERN_INFO, ha, "%s: Get host stats done\n",
1212 		   __func__);
1213 	return ret;
1214 }
1215 
1216 static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
1217 				   enum iscsi_param_type param_type,
1218 				   int param, char *buf)
1219 {
1220 	struct Scsi_Host *shost = iscsi_iface_to_shost(iface);
1221 	struct scsi_qla_host *ha = to_qla_host(shost);
1222 	int ival;
1223 	char *pval = NULL;
1224 	int len = -ENOSYS;
1225 
1226 	if (param_type == ISCSI_NET_PARAM) {
1227 		switch (param) {
1228 		case ISCSI_NET_PARAM_IPV4_ADDR:
1229 			len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
1230 			break;
1231 		case ISCSI_NET_PARAM_IPV4_SUBNET:
1232 			len = sprintf(buf, "%pI4\n",
1233 				      &ha->ip_config.subnet_mask);
1234 			break;
1235 		case ISCSI_NET_PARAM_IPV4_GW:
1236 			len = sprintf(buf, "%pI4\n", &ha->ip_config.gateway);
1237 			break;
1238 		case ISCSI_NET_PARAM_IFACE_ENABLE:
1239 			if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
1240 				OP_STATE(ha->ip_config.ipv4_options,
1241 					 IPOPT_IPV4_PROTOCOL_ENABLE, pval);
1242 			} else {
1243 				OP_STATE(ha->ip_config.ipv6_options,
1244 					 IPV6_OPT_IPV6_PROTOCOL_ENABLE, pval);
1245 			}
1246 
1247 			len = sprintf(buf, "%s\n", pval);
1248 			break;
1249 		case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
1250 			len = sprintf(buf, "%s\n",
1251 				      (ha->ip_config.tcp_options &
1252 				       TCPOPT_DHCP_ENABLE) ?
1253 				      "dhcp" : "static");
1254 			break;
1255 		case ISCSI_NET_PARAM_IPV6_ADDR:
1256 			if (iface->iface_num == 0)
1257 				len = sprintf(buf, "%pI6\n",
1258 					      &ha->ip_config.ipv6_addr0);
1259 			if (iface->iface_num == 1)
1260 				len = sprintf(buf, "%pI6\n",
1261 					      &ha->ip_config.ipv6_addr1);
1262 			break;
1263 		case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
1264 			len = sprintf(buf, "%pI6\n",
1265 				      &ha->ip_config.ipv6_link_local_addr);
1266 			break;
1267 		case ISCSI_NET_PARAM_IPV6_ROUTER:
1268 			len = sprintf(buf, "%pI6\n",
1269 				      &ha->ip_config.ipv6_default_router_addr);
1270 			break;
1271 		case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
1272 			pval = (ha->ip_config.ipv6_addl_options &
1273 				IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) ?
1274 				"nd" : "static";
1275 
1276 			len = sprintf(buf, "%s\n", pval);
1277 			break;
1278 		case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
1279 			pval = (ha->ip_config.ipv6_addl_options &
1280 				IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR) ?
1281 				"auto" : "static";
1282 
1283 			len = sprintf(buf, "%s\n", pval);
1284 			break;
1285 		case ISCSI_NET_PARAM_VLAN_ID:
1286 			if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
1287 				ival = ha->ip_config.ipv4_vlan_tag &
1288 				       ISCSI_MAX_VLAN_ID;
1289 			else
1290 				ival = ha->ip_config.ipv6_vlan_tag &
1291 				       ISCSI_MAX_VLAN_ID;
1292 
1293 			len = sprintf(buf, "%d\n", ival);
1294 			break;
1295 		case ISCSI_NET_PARAM_VLAN_PRIORITY:
1296 			if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
1297 				ival = (ha->ip_config.ipv4_vlan_tag >> 13) &
1298 				       ISCSI_MAX_VLAN_PRIORITY;
1299 			else
1300 				ival = (ha->ip_config.ipv6_vlan_tag >> 13) &
1301 				       ISCSI_MAX_VLAN_PRIORITY;
1302 
1303 			len = sprintf(buf, "%d\n", ival);
1304 			break;
1305 		case ISCSI_NET_PARAM_VLAN_ENABLED:
1306 			if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
1307 				OP_STATE(ha->ip_config.ipv4_options,
1308 					 IPOPT_VLAN_TAGGING_ENABLE, pval);
1309 			} else {
1310 				OP_STATE(ha->ip_config.ipv6_options,
1311 					 IPV6_OPT_VLAN_TAGGING_ENABLE, pval);
1312 			}
1313 			len = sprintf(buf, "%s\n", pval);
1314 			break;
1315 		case ISCSI_NET_PARAM_MTU:
1316 			len = sprintf(buf, "%d\n", ha->ip_config.eth_mtu_size);
1317 			break;
1318 		case ISCSI_NET_PARAM_PORT:
1319 			if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
1320 				len = sprintf(buf, "%d\n",
1321 					      ha->ip_config.ipv4_port);
1322 			else
1323 				len = sprintf(buf, "%d\n",
1324 					      ha->ip_config.ipv6_port);
1325 			break;
1326 		case ISCSI_NET_PARAM_IPADDR_STATE:
1327 			if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
1328 				pval = iscsi_get_ipaddress_state_name(
1329 						ha->ip_config.ipv4_addr_state);
1330 			} else {
1331 				if (iface->iface_num == 0)
1332 					pval = iscsi_get_ipaddress_state_name(
1333 						ha->ip_config.ipv6_addr0_state);
1334 				else if (iface->iface_num == 1)
1335 					pval = iscsi_get_ipaddress_state_name(
1336 						ha->ip_config.ipv6_addr1_state);
1337 			}
1338 
1339 			len = sprintf(buf, "%s\n", pval);
1340 			break;
1341 		case ISCSI_NET_PARAM_IPV6_LINKLOCAL_STATE:
1342 			pval = iscsi_get_ipaddress_state_name(
1343 					ha->ip_config.ipv6_link_local_state);
1344 			len = sprintf(buf, "%s\n", pval);
1345 			break;
1346 		case ISCSI_NET_PARAM_IPV6_ROUTER_STATE:
1347 			pval = iscsi_get_router_state_name(
1348 				      ha->ip_config.ipv6_default_router_state);
1349 			len = sprintf(buf, "%s\n", pval);
1350 			break;
1351 		case ISCSI_NET_PARAM_DELAYED_ACK_EN:
1352 			if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
1353 				OP_STATE(~ha->ip_config.tcp_options,
1354 					 TCPOPT_DELAYED_ACK_DISABLE, pval);
1355 			} else {
1356 				OP_STATE(~ha->ip_config.ipv6_tcp_options,
1357 					 IPV6_TCPOPT_DELAYED_ACK_DISABLE, pval);
1358 			}
1359 			len = sprintf(buf, "%s\n", pval);
1360 			break;
1361 		case ISCSI_NET_PARAM_TCP_NAGLE_DISABLE:
1362 			if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
1363 				OP_STATE(~ha->ip_config.tcp_options,
1364 					 TCPOPT_NAGLE_ALGO_DISABLE, pval);
1365 			} else {
1366 				OP_STATE(~ha->ip_config.ipv6_tcp_options,
1367 					 IPV6_TCPOPT_NAGLE_ALGO_DISABLE, pval);
1368 			}
1369 			len = sprintf(buf, "%s\n", pval);
1370 			break;
1371 		case ISCSI_NET_PARAM_TCP_WSF_DISABLE:
1372 			if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
1373 				OP_STATE(~ha->ip_config.tcp_options,
1374 					 TCPOPT_WINDOW_SCALE_DISABLE, pval);
1375 			} else {
1376 				OP_STATE(~ha->ip_config.ipv6_tcp_options,
1377 					 IPV6_TCPOPT_WINDOW_SCALE_DISABLE,
1378 					 pval);
1379 			}
1380 			len = sprintf(buf, "%s\n", pval);
1381 			break;
1382 		case ISCSI_NET_PARAM_TCP_WSF:
1383 			if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
1384 				len = sprintf(buf, "%d\n",
1385 					      ha->ip_config.tcp_wsf);
1386 			else
1387 				len = sprintf(buf, "%d\n",
1388 					      ha->ip_config.ipv6_tcp_wsf);
1389 			break;
1390 		case ISCSI_NET_PARAM_TCP_TIMER_SCALE:
1391 			if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
1392 				ival = (ha->ip_config.tcp_options &
1393 					TCPOPT_TIMER_SCALE) >> 1;
1394 			else
1395 				ival = (ha->ip_config.ipv6_tcp_options &
1396 					IPV6_TCPOPT_TIMER_SCALE) >> 1;
1397 
1398 			len = sprintf(buf, "%d\n", ival);
1399 			break;
1400 		case ISCSI_NET_PARAM_TCP_TIMESTAMP_EN:
1401 			if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
1402 				OP_STATE(ha->ip_config.tcp_options,
1403 					 TCPOPT_TIMESTAMP_ENABLE, pval);
1404 			} else {
1405 				OP_STATE(ha->ip_config.ipv6_tcp_options,
1406 					 IPV6_TCPOPT_TIMESTAMP_EN, pval);
1407 			}
1408 			len = sprintf(buf, "%s\n", pval);
1409 			break;
1410 		case ISCSI_NET_PARAM_CACHE_ID:
1411 			if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
1412 				len = sprintf(buf, "%d\n",
1413 					      ha->ip_config.ipv4_cache_id);
1414 			else
1415 				len = sprintf(buf, "%d\n",
1416 					      ha->ip_config.ipv6_cache_id);
1417 			break;
1418 		case ISCSI_NET_PARAM_IPV4_DHCP_DNS_ADDR_EN:
1419 			OP_STATE(ha->ip_config.tcp_options,
1420 				 TCPOPT_DNS_SERVER_IP_EN, pval);
1421 
1422 			len = sprintf(buf, "%s\n", pval);
1423 			break;
1424 		case ISCSI_NET_PARAM_IPV4_DHCP_SLP_DA_EN:
1425 			OP_STATE(ha->ip_config.tcp_options,
1426 				 TCPOPT_SLP_DA_INFO_EN, pval);
1427 
1428 			len = sprintf(buf, "%s\n", pval);
1429 			break;
1430 		case ISCSI_NET_PARAM_IPV4_TOS_EN:
1431 			OP_STATE(ha->ip_config.ipv4_options,
1432 				 IPOPT_IPV4_TOS_EN, pval);
1433 
1434 			len = sprintf(buf, "%s\n", pval);
1435 			break;
1436 		case ISCSI_NET_PARAM_IPV4_TOS:
1437 			len = sprintf(buf, "%d\n", ha->ip_config.ipv4_tos);
1438 			break;
1439 		case ISCSI_NET_PARAM_IPV4_GRAT_ARP_EN:
1440 			OP_STATE(ha->ip_config.ipv4_options,
1441 				 IPOPT_GRAT_ARP_EN, pval);
1442 
1443 			len = sprintf(buf, "%s\n", pval);
1444 			break;
1445 		case ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID_EN:
1446 			OP_STATE(ha->ip_config.ipv4_options, IPOPT_ALT_CID_EN,
1447 				 pval);
1448 
1449 			len = sprintf(buf, "%s\n", pval);
1450 			break;
1451 		case ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID:
1452 			pval = (ha->ip_config.ipv4_alt_cid_len) ?
1453 			       (char *)ha->ip_config.ipv4_alt_cid : "";
1454 
1455 			len = sprintf(buf, "%s\n", pval);
1456 			break;
1457 		case ISCSI_NET_PARAM_IPV4_DHCP_REQ_VENDOR_ID_EN:
1458 			OP_STATE(ha->ip_config.ipv4_options,
1459 				 IPOPT_REQ_VID_EN, pval);
1460 
1461 			len = sprintf(buf, "%s\n", pval);
1462 			break;
1463 		case ISCSI_NET_PARAM_IPV4_DHCP_USE_VENDOR_ID_EN:
1464 			OP_STATE(ha->ip_config.ipv4_options,
1465 				 IPOPT_USE_VID_EN, pval);
1466 
1467 			len = sprintf(buf, "%s\n", pval);
1468 			break;
1469 		case ISCSI_NET_PARAM_IPV4_DHCP_VENDOR_ID:
1470 			pval = (ha->ip_config.ipv4_vid_len) ?
1471 			       (char *)ha->ip_config.ipv4_vid : "";
1472 
1473 			len = sprintf(buf, "%s\n", pval);
1474 			break;
1475 		case ISCSI_NET_PARAM_IPV4_DHCP_LEARN_IQN_EN:
1476 			OP_STATE(ha->ip_config.ipv4_options,
1477 				 IPOPT_LEARN_IQN_EN, pval);
1478 
1479 			len = sprintf(buf, "%s\n", pval);
1480 			break;
1481 		case ISCSI_NET_PARAM_IPV4_FRAGMENT_DISABLE:
1482 			OP_STATE(~ha->ip_config.ipv4_options,
1483 				 IPOPT_FRAGMENTATION_DISABLE, pval);
1484 
1485 			len = sprintf(buf, "%s\n", pval);
1486 			break;
1487 		case ISCSI_NET_PARAM_IPV4_IN_FORWARD_EN:
1488 			OP_STATE(ha->ip_config.ipv4_options,
1489 				 IPOPT_IN_FORWARD_EN, pval);
1490 
1491 			len = sprintf(buf, "%s\n", pval);
1492 			break;
1493 		case ISCSI_NET_PARAM_REDIRECT_EN:
1494 			if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
1495 				OP_STATE(ha->ip_config.ipv4_options,
1496 					 IPOPT_ARP_REDIRECT_EN, pval);
1497 			} else {
1498 				OP_STATE(ha->ip_config.ipv6_options,
1499 					 IPV6_OPT_REDIRECT_EN, pval);
1500 			}
1501 			len = sprintf(buf, "%s\n", pval);
1502 			break;
1503 		case ISCSI_NET_PARAM_IPV4_TTL:
1504 			len = sprintf(buf, "%d\n", ha->ip_config.ipv4_ttl);
1505 			break;
1506 		case ISCSI_NET_PARAM_IPV6_GRAT_NEIGHBOR_ADV_EN:
1507 			OP_STATE(ha->ip_config.ipv6_options,
1508 				 IPV6_OPT_GRAT_NEIGHBOR_ADV_EN, pval);
1509 
1510 			len = sprintf(buf, "%s\n", pval);
1511 			break;
1512 		case ISCSI_NET_PARAM_IPV6_MLD_EN:
1513 			OP_STATE(ha->ip_config.ipv6_addl_options,
1514 				 IPV6_ADDOPT_MLD_EN, pval);
1515 
1516 			len = sprintf(buf, "%s\n", pval);
1517 			break;
1518 		case ISCSI_NET_PARAM_IPV6_FLOW_LABEL:
1519 			len = sprintf(buf, "%u\n", ha->ip_config.ipv6_flow_lbl);
1520 			break;
1521 		case ISCSI_NET_PARAM_IPV6_TRAFFIC_CLASS:
1522 			len = sprintf(buf, "%d\n",
1523 				      ha->ip_config.ipv6_traffic_class);
1524 			break;
1525 		case ISCSI_NET_PARAM_IPV6_HOP_LIMIT:
1526 			len = sprintf(buf, "%d\n",
1527 				      ha->ip_config.ipv6_hop_limit);
1528 			break;
1529 		case ISCSI_NET_PARAM_IPV6_ND_REACHABLE_TMO:
1530 			len = sprintf(buf, "%d\n",
1531 				      ha->ip_config.ipv6_nd_reach_time);
1532 			break;
1533 		case ISCSI_NET_PARAM_IPV6_ND_REXMIT_TIME:
1534 			len = sprintf(buf, "%d\n",
1535 				      ha->ip_config.ipv6_nd_rexmit_timer);
1536 			break;
1537 		case ISCSI_NET_PARAM_IPV6_ND_STALE_TMO:
1538 			len = sprintf(buf, "%d\n",
1539 				      ha->ip_config.ipv6_nd_stale_timeout);
1540 			break;
1541 		case ISCSI_NET_PARAM_IPV6_DUP_ADDR_DETECT_CNT:
1542 			len = sprintf(buf, "%d\n",
1543 				      ha->ip_config.ipv6_dup_addr_detect_count);
1544 			break;
1545 		case ISCSI_NET_PARAM_IPV6_RTR_ADV_LINK_MTU:
1546 			len = sprintf(buf, "%d\n",
1547 				      ha->ip_config.ipv6_gw_advrt_mtu);
1548 			break;
1549 		default:
1550 			len = -ENOSYS;
1551 		}
1552 	} else if (param_type == ISCSI_IFACE_PARAM) {
1553 		switch (param) {
1554 		case ISCSI_IFACE_PARAM_DEF_TASKMGMT_TMO:
1555 			len = sprintf(buf, "%d\n", ha->ip_config.def_timeout);
1556 			break;
1557 		case ISCSI_IFACE_PARAM_HDRDGST_EN:
1558 			OP_STATE(ha->ip_config.iscsi_options,
1559 				 ISCSIOPTS_HEADER_DIGEST_EN, pval);
1560 
1561 			len = sprintf(buf, "%s\n", pval);
1562 			break;
1563 		case ISCSI_IFACE_PARAM_DATADGST_EN:
1564 			OP_STATE(ha->ip_config.iscsi_options,
1565 				 ISCSIOPTS_DATA_DIGEST_EN, pval);
1566 
1567 			len = sprintf(buf, "%s\n", pval);
1568 			break;
1569 		case ISCSI_IFACE_PARAM_IMM_DATA_EN:
1570 			OP_STATE(ha->ip_config.iscsi_options,
1571 				 ISCSIOPTS_IMMEDIATE_DATA_EN, pval);
1572 
1573 			len = sprintf(buf, "%s\n", pval);
1574 			break;
1575 		case ISCSI_IFACE_PARAM_INITIAL_R2T_EN:
1576 			OP_STATE(ha->ip_config.iscsi_options,
1577 				 ISCSIOPTS_INITIAL_R2T_EN, pval);
1578 
1579 			len = sprintf(buf, "%s\n", pval);
1580 			break;
1581 		case ISCSI_IFACE_PARAM_DATASEQ_INORDER_EN:
1582 			OP_STATE(ha->ip_config.iscsi_options,
1583 				 ISCSIOPTS_DATA_SEQ_INORDER_EN, pval);
1584 
1585 			len = sprintf(buf, "%s\n", pval);
1586 			break;
1587 		case ISCSI_IFACE_PARAM_PDU_INORDER_EN:
1588 			OP_STATE(ha->ip_config.iscsi_options,
1589 				 ISCSIOPTS_DATA_PDU_INORDER_EN, pval);
1590 
1591 			len = sprintf(buf, "%s\n", pval);
1592 			break;
1593 		case ISCSI_IFACE_PARAM_ERL:
1594 			len = sprintf(buf, "%d\n",
1595 				      (ha->ip_config.iscsi_options &
1596 				       ISCSIOPTS_ERL));
1597 			break;
1598 		case ISCSI_IFACE_PARAM_MAX_RECV_DLENGTH:
1599 			len = sprintf(buf, "%u\n",
1600 				      ha->ip_config.iscsi_max_pdu_size *
1601 				      BYTE_UNITS);
1602 			break;
1603 		case ISCSI_IFACE_PARAM_FIRST_BURST:
1604 			len = sprintf(buf, "%u\n",
1605 				      ha->ip_config.iscsi_first_burst_len *
1606 				      BYTE_UNITS);
1607 			break;
1608 		case ISCSI_IFACE_PARAM_MAX_R2T:
1609 			len = sprintf(buf, "%d\n",
1610 				      ha->ip_config.iscsi_max_outstnd_r2t);
1611 			break;
1612 		case ISCSI_IFACE_PARAM_MAX_BURST:
1613 			len = sprintf(buf, "%u\n",
1614 				      ha->ip_config.iscsi_max_burst_len *
1615 				      BYTE_UNITS);
1616 			break;
1617 		case ISCSI_IFACE_PARAM_CHAP_AUTH_EN:
1618 			OP_STATE(ha->ip_config.iscsi_options,
1619 				 ISCSIOPTS_CHAP_AUTH_EN, pval);
1620 
1621 			len = sprintf(buf, "%s\n", pval);
1622 			break;
1623 		case ISCSI_IFACE_PARAM_BIDI_CHAP_EN:
1624 			OP_STATE(ha->ip_config.iscsi_options,
1625 				 ISCSIOPTS_BIDI_CHAP_EN, pval);
1626 
1627 			len = sprintf(buf, "%s\n", pval);
1628 			break;
1629 		case ISCSI_IFACE_PARAM_DISCOVERY_AUTH_OPTIONAL:
1630 			OP_STATE(ha->ip_config.iscsi_options,
1631 				 ISCSIOPTS_DISCOVERY_AUTH_EN, pval);
1632 
1633 			len = sprintf(buf, "%s\n", pval);
1634 			break;
1635 		case ISCSI_IFACE_PARAM_DISCOVERY_LOGOUT_EN:
1636 			OP_STATE(ha->ip_config.iscsi_options,
1637 				 ISCSIOPTS_DISCOVERY_LOGOUT_EN, pval);
1638 
1639 			len = sprintf(buf, "%s\n", pval);
1640 			break;
1641 		case ISCSI_IFACE_PARAM_STRICT_LOGIN_COMP_EN:
1642 			OP_STATE(ha->ip_config.iscsi_options,
1643 				 ISCSIOPTS_STRICT_LOGIN_COMP_EN, pval);
1644 
1645 			len = sprintf(buf, "%s\n", pval);
1646 			break;
1647 		case ISCSI_IFACE_PARAM_INITIATOR_NAME:
1648 			len = sprintf(buf, "%s\n", ha->ip_config.iscsi_name);
1649 			break;
1650 		default:
1651 			len = -ENOSYS;
1652 		}
1653 	}
1654 
1655 	return len;
1656 }
1657 
1658 static struct iscsi_endpoint *
1659 qla4xxx_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
1660 		   int non_blocking)
1661 {
1662 	int ret;
1663 	struct iscsi_endpoint *ep;
1664 	struct qla_endpoint *qla_ep;
1665 	struct scsi_qla_host *ha;
1666 	struct sockaddr_in *addr;
1667 	struct sockaddr_in6 *addr6;
1668 
1669 	if (!shost) {
1670 		ret = -ENXIO;
1671 		pr_err("%s: shost is NULL\n", __func__);
1672 		return ERR_PTR(ret);
1673 	}
1674 
1675 	ha = iscsi_host_priv(shost);
1676 	ep = iscsi_create_endpoint(sizeof(struct qla_endpoint));
1677 	if (!ep) {
1678 		ret = -ENOMEM;
1679 		return ERR_PTR(ret);
1680 	}
1681 
1682 	qla_ep = ep->dd_data;
1683 	memset(qla_ep, 0, sizeof(struct qla_endpoint));
1684 	if (dst_addr->sa_family == AF_INET) {
1685 		memcpy(&qla_ep->dst_addr, dst_addr, sizeof(struct sockaddr_in));
1686 		addr = (struct sockaddr_in *)&qla_ep->dst_addr;
1687 		DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI4\n", __func__,
1688 				  (char *)&addr->sin_addr));
1689 	} else if (dst_addr->sa_family == AF_INET6) {
1690 		memcpy(&qla_ep->dst_addr, dst_addr,
1691 		       sizeof(struct sockaddr_in6));
1692 		addr6 = (struct sockaddr_in6 *)&qla_ep->dst_addr;
1693 		DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI6\n", __func__,
1694 				  (char *)&addr6->sin6_addr));
1695 	} else {
1696 		ql4_printk(KERN_WARNING, ha, "%s: Invalid endpoint\n",
1697 			   __func__);
1698 	}
1699 
1700 	qla_ep->host = shost;
1701 
1702 	return ep;
1703 }
1704 
1705 static int qla4xxx_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
1706 {
1707 	struct qla_endpoint *qla_ep;
1708 	struct scsi_qla_host *ha;
1709 	int ret = 0;
1710 
1711 	qla_ep = ep->dd_data;
1712 	ha = to_qla_host(qla_ep->host);
1713 	DEBUG2(pr_info_ratelimited("%s: host: %ld\n", __func__, ha->host_no));
1714 
1715 	if (adapter_up(ha) && !test_bit(AF_BUILD_DDB_LIST, &ha->flags))
1716 		ret = 1;
1717 
1718 	return ret;
1719 }
1720 
1721 static void qla4xxx_ep_disconnect(struct iscsi_endpoint *ep)
1722 {
1723 	struct qla_endpoint *qla_ep;
1724 	struct scsi_qla_host *ha;
1725 
1726 	qla_ep = ep->dd_data;
1727 	ha = to_qla_host(qla_ep->host);
1728 	DEBUG2(ql4_printk(KERN_INFO, ha, "%s: host: %ld\n", __func__,
1729 			  ha->host_no));
1730 	iscsi_destroy_endpoint(ep);
1731 }
1732 
1733 static int qla4xxx_get_ep_param(struct iscsi_endpoint *ep,
1734 				enum iscsi_param param,
1735 				char *buf)
1736 {
1737 	struct qla_endpoint *qla_ep = ep->dd_data;
1738 	struct sockaddr *dst_addr;
1739 	struct scsi_qla_host *ha;
1740 
1741 	if (!qla_ep)
1742 		return -ENOTCONN;
1743 
1744 	ha = to_qla_host(qla_ep->host);
1745 	DEBUG2(ql4_printk(KERN_INFO, ha, "%s: host: %ld\n", __func__,
1746 			  ha->host_no));
1747 
1748 	switch (param) {
1749 	case ISCSI_PARAM_CONN_PORT:
1750 	case ISCSI_PARAM_CONN_ADDRESS:
1751 		dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
1752 		if (!dst_addr)
1753 			return -ENOTCONN;
1754 
1755 		return iscsi_conn_get_addr_param((struct sockaddr_storage *)
1756 						 &qla_ep->dst_addr, param, buf);
1757 	default:
1758 		return -ENOSYS;
1759 	}
1760 }
1761 
1762 static void qla4xxx_conn_get_stats(struct iscsi_cls_conn *cls_conn,
1763 				   struct iscsi_stats *stats)
1764 {
1765 	struct iscsi_session *sess;
1766 	struct iscsi_cls_session *cls_sess;
1767 	struct ddb_entry *ddb_entry;
1768 	struct scsi_qla_host *ha;
1769 	struct ql_iscsi_stats *ql_iscsi_stats;
1770 	int stats_size;
1771 	int ret;
1772 	dma_addr_t iscsi_stats_dma;
1773 
1774 	cls_sess = iscsi_conn_to_session(cls_conn);
1775 	sess = cls_sess->dd_data;
1776 	ddb_entry = sess->dd_data;
1777 	ha = ddb_entry->ha;
1778 
1779 	DEBUG2(ql4_printk(KERN_INFO, ha, "%s: host: %ld\n", __func__,
1780 			  ha->host_no));
1781 	stats_size = PAGE_ALIGN(sizeof(struct ql_iscsi_stats));
1782 	/* Allocate memory */
1783 	ql_iscsi_stats = dma_alloc_coherent(&ha->pdev->dev, stats_size,
1784 					    &iscsi_stats_dma, GFP_KERNEL);
1785 	if (!ql_iscsi_stats) {
1786 		ql4_printk(KERN_ERR, ha,
1787 			   "Unable to allocate memory for iscsi stats\n");
1788 		goto exit_get_stats;
1789 	}
1790 
1791 	ret =  qla4xxx_get_mgmt_data(ha, ddb_entry->fw_ddb_index, stats_size,
1792 				     iscsi_stats_dma);
1793 	if (ret != QLA_SUCCESS) {
1794 		ql4_printk(KERN_ERR, ha,
1795 			   "Unable to retrieve iscsi stats\n");
1796 		goto free_stats;
1797 	}
1798 
1799 	/* octets */
1800 	stats->txdata_octets = le64_to_cpu(ql_iscsi_stats->tx_data_octets);
1801 	stats->rxdata_octets = le64_to_cpu(ql_iscsi_stats->rx_data_octets);
1802 	/* xmit pdus */
1803 	stats->noptx_pdus = le32_to_cpu(ql_iscsi_stats->tx_nopout_pdus);
1804 	stats->scsicmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_cmd_pdus);
1805 	stats->tmfcmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_tmf_cmd_pdus);
1806 	stats->login_pdus = le32_to_cpu(ql_iscsi_stats->tx_login_cmd_pdus);
1807 	stats->text_pdus = le32_to_cpu(ql_iscsi_stats->tx_text_cmd_pdus);
1808 	stats->dataout_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_write_pdus);
1809 	stats->logout_pdus = le32_to_cpu(ql_iscsi_stats->tx_logout_cmd_pdus);
1810 	stats->snack_pdus = le32_to_cpu(ql_iscsi_stats->tx_snack_req_pdus);
1811 	/* recv pdus */
1812 	stats->noprx_pdus = le32_to_cpu(ql_iscsi_stats->rx_nopin_pdus);
1813 	stats->scsirsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_resp_pdus);
1814 	stats->tmfrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_tmf_resp_pdus);
1815 	stats->textrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_text_resp_pdus);
1816 	stats->datain_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_read_pdus);
1817 	stats->logoutrsp_pdus =
1818 			le32_to_cpu(ql_iscsi_stats->rx_logout_resp_pdus);
1819 	stats->r2t_pdus = le32_to_cpu(ql_iscsi_stats->rx_r2t_pdus);
1820 	stats->async_pdus = le32_to_cpu(ql_iscsi_stats->rx_async_pdus);
1821 	stats->rjt_pdus = le32_to_cpu(ql_iscsi_stats->rx_reject_pdus);
1822 
1823 free_stats:
1824 	dma_free_coherent(&ha->pdev->dev, stats_size, ql_iscsi_stats,
1825 			  iscsi_stats_dma);
1826 exit_get_stats:
1827 	return;
1828 }
1829 
1830 static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
1831 {
1832 	struct iscsi_cls_session *session;
1833 	struct iscsi_session *sess;
1834 	unsigned long flags;
1835 	enum blk_eh_timer_return ret = BLK_EH_NOT_HANDLED;
1836 
1837 	session = starget_to_session(scsi_target(sc->device));
1838 	sess = session->dd_data;
1839 
1840 	spin_lock_irqsave(&session->lock, flags);
1841 	if (session->state == ISCSI_SESSION_FAILED)
1842 		ret = BLK_EH_RESET_TIMER;
1843 	spin_unlock_irqrestore(&session->lock, flags);
1844 
1845 	return ret;
1846 }
1847 
1848 static void qla4xxx_set_port_speed(struct Scsi_Host *shost)
1849 {
1850 	struct scsi_qla_host *ha = to_qla_host(shost);
1851 	struct iscsi_cls_host *ihost = shost->shost_data;
1852 	uint32_t speed = ISCSI_PORT_SPEED_UNKNOWN;
1853 
1854 	qla4xxx_get_firmware_state(ha);
1855 
1856 	switch (ha->addl_fw_state & 0x0F00) {
1857 	case FW_ADDSTATE_LINK_SPEED_10MBPS:
1858 		speed = ISCSI_PORT_SPEED_10MBPS;
1859 		break;
1860 	case FW_ADDSTATE_LINK_SPEED_100MBPS:
1861 		speed = ISCSI_PORT_SPEED_100MBPS;
1862 		break;
1863 	case FW_ADDSTATE_LINK_SPEED_1GBPS:
1864 		speed = ISCSI_PORT_SPEED_1GBPS;
1865 		break;
1866 	case FW_ADDSTATE_LINK_SPEED_10GBPS:
1867 		speed = ISCSI_PORT_SPEED_10GBPS;
1868 		break;
1869 	}
1870 	ihost->port_speed = speed;
1871 }
1872 
1873 static void qla4xxx_set_port_state(struct Scsi_Host *shost)
1874 {
1875 	struct scsi_qla_host *ha = to_qla_host(shost);
1876 	struct iscsi_cls_host *ihost = shost->shost_data;
1877 	uint32_t state = ISCSI_PORT_STATE_DOWN;
1878 
1879 	if (test_bit(AF_LINK_UP, &ha->flags))
1880 		state = ISCSI_PORT_STATE_UP;
1881 
1882 	ihost->port_state = state;
1883 }
1884 
1885 static int qla4xxx_host_get_param(struct Scsi_Host *shost,
1886 				  enum iscsi_host_param param, char *buf)
1887 {
1888 	struct scsi_qla_host *ha = to_qla_host(shost);
1889 	int len;
1890 
1891 	switch (param) {
1892 	case ISCSI_HOST_PARAM_HWADDRESS:
1893 		len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
1894 		break;
1895 	case ISCSI_HOST_PARAM_IPADDRESS:
1896 		len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
1897 		break;
1898 	case ISCSI_HOST_PARAM_INITIATOR_NAME:
1899 		len = sprintf(buf, "%s\n", ha->name_string);
1900 		break;
1901 	case ISCSI_HOST_PARAM_PORT_STATE:
1902 		qla4xxx_set_port_state(shost);
1903 		len = sprintf(buf, "%s\n", iscsi_get_port_state_name(shost));
1904 		break;
1905 	case ISCSI_HOST_PARAM_PORT_SPEED:
1906 		qla4xxx_set_port_speed(shost);
1907 		len = sprintf(buf, "%s\n", iscsi_get_port_speed_name(shost));
1908 		break;
1909 	default:
1910 		return -ENOSYS;
1911 	}
1912 
1913 	return len;
1914 }
1915 
1916 static void qla4xxx_create_ipv4_iface(struct scsi_qla_host *ha)
1917 {
1918 	if (ha->iface_ipv4)
1919 		return;
1920 
1921 	/* IPv4 */
1922 	ha->iface_ipv4 = iscsi_create_iface(ha->host,
1923 					    &qla4xxx_iscsi_transport,
1924 					    ISCSI_IFACE_TYPE_IPV4, 0, 0);
1925 	if (!ha->iface_ipv4)
1926 		ql4_printk(KERN_ERR, ha, "Could not create IPv4 iSCSI "
1927 			   "iface0.\n");
1928 }
1929 
1930 static void qla4xxx_create_ipv6_iface(struct scsi_qla_host *ha)
1931 {
1932 	if (!ha->iface_ipv6_0)
1933 		/* IPv6 iface-0 */
1934 		ha->iface_ipv6_0 = iscsi_create_iface(ha->host,
1935 						      &qla4xxx_iscsi_transport,
1936 						      ISCSI_IFACE_TYPE_IPV6, 0,
1937 						      0);
1938 	if (!ha->iface_ipv6_0)
1939 		ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
1940 			   "iface0.\n");
1941 
1942 	if (!ha->iface_ipv6_1)
1943 		/* IPv6 iface-1 */
1944 		ha->iface_ipv6_1 = iscsi_create_iface(ha->host,
1945 						      &qla4xxx_iscsi_transport,
1946 						      ISCSI_IFACE_TYPE_IPV6, 1,
1947 						      0);
1948 	if (!ha->iface_ipv6_1)
1949 		ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
1950 			   "iface1.\n");
1951 }
1952 
1953 static void qla4xxx_create_ifaces(struct scsi_qla_host *ha)
1954 {
1955 	if (ha->ip_config.ipv4_options & IPOPT_IPV4_PROTOCOL_ENABLE)
1956 		qla4xxx_create_ipv4_iface(ha);
1957 
1958 	if (ha->ip_config.ipv6_options & IPV6_OPT_IPV6_PROTOCOL_ENABLE)
1959 		qla4xxx_create_ipv6_iface(ha);
1960 }
1961 
1962 static void qla4xxx_destroy_ipv4_iface(struct scsi_qla_host *ha)
1963 {
1964 	if (ha->iface_ipv4) {
1965 		iscsi_destroy_iface(ha->iface_ipv4);
1966 		ha->iface_ipv4 = NULL;
1967 	}
1968 }
1969 
1970 static void qla4xxx_destroy_ipv6_iface(struct scsi_qla_host *ha)
1971 {
1972 	if (ha->iface_ipv6_0) {
1973 		iscsi_destroy_iface(ha->iface_ipv6_0);
1974 		ha->iface_ipv6_0 = NULL;
1975 	}
1976 	if (ha->iface_ipv6_1) {
1977 		iscsi_destroy_iface(ha->iface_ipv6_1);
1978 		ha->iface_ipv6_1 = NULL;
1979 	}
1980 }
1981 
1982 static void qla4xxx_destroy_ifaces(struct scsi_qla_host *ha)
1983 {
1984 	qla4xxx_destroy_ipv4_iface(ha);
1985 	qla4xxx_destroy_ipv6_iface(ha);
1986 }
1987 
1988 static void qla4xxx_set_ipv6(struct scsi_qla_host *ha,
1989 			     struct iscsi_iface_param_info *iface_param,
1990 			     struct addr_ctrl_blk *init_fw_cb)
1991 {
1992 	/*
1993 	 * iface_num 0 is valid for IPv6 Addr, linklocal, router, autocfg.
1994 	 * iface_num 1 is valid only for IPv6 Addr.
1995 	 */
1996 	switch (iface_param->param) {
1997 	case ISCSI_NET_PARAM_IPV6_ADDR:
1998 		if (iface_param->iface_num & 0x1)
1999 			/* IPv6 Addr 1 */
2000 			memcpy(init_fw_cb->ipv6_addr1, iface_param->value,
2001 			       sizeof(init_fw_cb->ipv6_addr1));
2002 		else
2003 			/* IPv6 Addr 0 */
2004 			memcpy(init_fw_cb->ipv6_addr0, iface_param->value,
2005 			       sizeof(init_fw_cb->ipv6_addr0));
2006 		break;
2007 	case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
2008 		if (iface_param->iface_num & 0x1)
2009 			break;
2010 		memcpy(init_fw_cb->ipv6_if_id, &iface_param->value[8],
2011 		       sizeof(init_fw_cb->ipv6_if_id));
2012 		break;
2013 	case ISCSI_NET_PARAM_IPV6_ROUTER:
2014 		if (iface_param->iface_num & 0x1)
2015 			break;
2016 		memcpy(init_fw_cb->ipv6_dflt_rtr_addr, iface_param->value,
2017 		       sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
2018 		break;
2019 	case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
2020 		/* Autocfg applies to even interface */
2021 		if (iface_param->iface_num & 0x1)
2022 			break;
2023 
2024 		if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_DISABLE)
2025 			init_fw_cb->ipv6_addtl_opts &=
2026 				cpu_to_le16(
2027 				  ~IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
2028 		else if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_ND_ENABLE)
2029 			init_fw_cb->ipv6_addtl_opts |=
2030 				cpu_to_le16(
2031 				  IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
2032 		else
2033 			ql4_printk(KERN_ERR, ha,
2034 				   "Invalid autocfg setting for IPv6 addr\n");
2035 		break;
2036 	case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
2037 		/* Autocfg applies to even interface */
2038 		if (iface_param->iface_num & 0x1)
2039 			break;
2040 
2041 		if (iface_param->value[0] ==
2042 		    ISCSI_IPV6_LINKLOCAL_AUTOCFG_ENABLE)
2043 			init_fw_cb->ipv6_addtl_opts |= cpu_to_le16(
2044 					IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
2045 		else if (iface_param->value[0] ==
2046 			 ISCSI_IPV6_LINKLOCAL_AUTOCFG_DISABLE)
2047 			init_fw_cb->ipv6_addtl_opts &= cpu_to_le16(
2048 				       ~IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
2049 		else
2050 			ql4_printk(KERN_ERR, ha,
2051 				   "Invalid autocfg setting for IPv6 linklocal addr\n");
2052 		break;
2053 	case ISCSI_NET_PARAM_IPV6_ROUTER_AUTOCFG:
2054 		/* Autocfg applies to even interface */
2055 		if (iface_param->iface_num & 0x1)
2056 			break;
2057 
2058 		if (iface_param->value[0] == ISCSI_IPV6_ROUTER_AUTOCFG_ENABLE)
2059 			memset(init_fw_cb->ipv6_dflt_rtr_addr, 0,
2060 			       sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
2061 		break;
2062 	case ISCSI_NET_PARAM_IFACE_ENABLE:
2063 		if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
2064 			init_fw_cb->ipv6_opts |=
2065 				cpu_to_le16(IPV6_OPT_IPV6_PROTOCOL_ENABLE);
2066 			qla4xxx_create_ipv6_iface(ha);
2067 		} else {
2068 			init_fw_cb->ipv6_opts &=
2069 				cpu_to_le16(~IPV6_OPT_IPV6_PROTOCOL_ENABLE &
2070 					    0xFFFF);
2071 			qla4xxx_destroy_ipv6_iface(ha);
2072 		}
2073 		break;
2074 	case ISCSI_NET_PARAM_VLAN_TAG:
2075 		if (iface_param->len != sizeof(init_fw_cb->ipv6_vlan_tag))
2076 			break;
2077 		init_fw_cb->ipv6_vlan_tag =
2078 				cpu_to_be16(*(uint16_t *)iface_param->value);
2079 		break;
2080 	case ISCSI_NET_PARAM_VLAN_ENABLED:
2081 		if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
2082 			init_fw_cb->ipv6_opts |=
2083 				cpu_to_le16(IPV6_OPT_VLAN_TAGGING_ENABLE);
2084 		else
2085 			init_fw_cb->ipv6_opts &=
2086 				cpu_to_le16(~IPV6_OPT_VLAN_TAGGING_ENABLE);
2087 		break;
2088 	case ISCSI_NET_PARAM_MTU:
2089 		init_fw_cb->eth_mtu_size =
2090 				cpu_to_le16(*(uint16_t *)iface_param->value);
2091 		break;
2092 	case ISCSI_NET_PARAM_PORT:
2093 		/* Autocfg applies to even interface */
2094 		if (iface_param->iface_num & 0x1)
2095 			break;
2096 
2097 		init_fw_cb->ipv6_port =
2098 				cpu_to_le16(*(uint16_t *)iface_param->value);
2099 		break;
2100 	case ISCSI_NET_PARAM_DELAYED_ACK_EN:
2101 		if (iface_param->iface_num & 0x1)
2102 			break;
2103 		if (iface_param->value[0] == ISCSI_NET_PARAM_DISABLE)
2104 			init_fw_cb->ipv6_tcp_opts |=
2105 				cpu_to_le16(IPV6_TCPOPT_DELAYED_ACK_DISABLE);
2106 		else
2107 			init_fw_cb->ipv6_tcp_opts &=
2108 				cpu_to_le16(~IPV6_TCPOPT_DELAYED_ACK_DISABLE &
2109 					    0xFFFF);
2110 		break;
2111 	case ISCSI_NET_PARAM_TCP_NAGLE_DISABLE:
2112 		if (iface_param->iface_num & 0x1)
2113 			break;
2114 		if (iface_param->value[0] == ISCSI_NET_PARAM_DISABLE)
2115 			init_fw_cb->ipv6_tcp_opts |=
2116 				cpu_to_le16(IPV6_TCPOPT_NAGLE_ALGO_DISABLE);
2117 		else
2118 			init_fw_cb->ipv6_tcp_opts &=
2119 				cpu_to_le16(~IPV6_TCPOPT_NAGLE_ALGO_DISABLE);
2120 		break;
2121 	case ISCSI_NET_PARAM_TCP_WSF_DISABLE:
2122 		if (iface_param->iface_num & 0x1)
2123 			break;
2124 		if (iface_param->value[0] == ISCSI_NET_PARAM_DISABLE)
2125 			init_fw_cb->ipv6_tcp_opts |=
2126 				cpu_to_le16(IPV6_TCPOPT_WINDOW_SCALE_DISABLE);
2127 		else
2128 			init_fw_cb->ipv6_tcp_opts &=
2129 				cpu_to_le16(~IPV6_TCPOPT_WINDOW_SCALE_DISABLE);
2130 		break;
2131 	case ISCSI_NET_PARAM_TCP_WSF:
2132 		if (iface_param->iface_num & 0x1)
2133 			break;
2134 		init_fw_cb->ipv6_tcp_wsf = iface_param->value[0];
2135 		break;
2136 	case ISCSI_NET_PARAM_TCP_TIMER_SCALE:
2137 		if (iface_param->iface_num & 0x1)
2138 			break;
2139 		init_fw_cb->ipv6_tcp_opts &=
2140 					cpu_to_le16(~IPV6_TCPOPT_TIMER_SCALE);
2141 		init_fw_cb->ipv6_tcp_opts |=
2142 				cpu_to_le16((iface_param->value[0] << 1) &
2143 					    IPV6_TCPOPT_TIMER_SCALE);
2144 		break;
2145 	case ISCSI_NET_PARAM_TCP_TIMESTAMP_EN:
2146 		if (iface_param->iface_num & 0x1)
2147 			break;
2148 		if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2149 			init_fw_cb->ipv6_tcp_opts |=
2150 				cpu_to_le16(IPV6_TCPOPT_TIMESTAMP_EN);
2151 		else
2152 			init_fw_cb->ipv6_tcp_opts &=
2153 				cpu_to_le16(~IPV6_TCPOPT_TIMESTAMP_EN);
2154 		break;
2155 	case ISCSI_NET_PARAM_IPV6_GRAT_NEIGHBOR_ADV_EN:
2156 		if (iface_param->iface_num & 0x1)
2157 			break;
2158 		if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2159 			init_fw_cb->ipv6_opts |=
2160 				cpu_to_le16(IPV6_OPT_GRAT_NEIGHBOR_ADV_EN);
2161 		else
2162 			init_fw_cb->ipv6_opts &=
2163 				cpu_to_le16(~IPV6_OPT_GRAT_NEIGHBOR_ADV_EN);
2164 		break;
2165 	case ISCSI_NET_PARAM_REDIRECT_EN:
2166 		if (iface_param->iface_num & 0x1)
2167 			break;
2168 		if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2169 			init_fw_cb->ipv6_opts |=
2170 				cpu_to_le16(IPV6_OPT_REDIRECT_EN);
2171 		else
2172 			init_fw_cb->ipv6_opts &=
2173 				cpu_to_le16(~IPV6_OPT_REDIRECT_EN);
2174 		break;
2175 	case ISCSI_NET_PARAM_IPV6_MLD_EN:
2176 		if (iface_param->iface_num & 0x1)
2177 			break;
2178 		if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2179 			init_fw_cb->ipv6_addtl_opts |=
2180 				cpu_to_le16(IPV6_ADDOPT_MLD_EN);
2181 		else
2182 			init_fw_cb->ipv6_addtl_opts &=
2183 				cpu_to_le16(~IPV6_ADDOPT_MLD_EN);
2184 		break;
2185 	case ISCSI_NET_PARAM_IPV6_FLOW_LABEL:
2186 		if (iface_param->iface_num & 0x1)
2187 			break;
2188 		init_fw_cb->ipv6_flow_lbl =
2189 				cpu_to_le16(*(uint16_t *)iface_param->value);
2190 		break;
2191 	case ISCSI_NET_PARAM_IPV6_TRAFFIC_CLASS:
2192 		if (iface_param->iface_num & 0x1)
2193 			break;
2194 		init_fw_cb->ipv6_traffic_class = iface_param->value[0];
2195 		break;
2196 	case ISCSI_NET_PARAM_IPV6_HOP_LIMIT:
2197 		if (iface_param->iface_num & 0x1)
2198 			break;
2199 		init_fw_cb->ipv6_hop_limit = iface_param->value[0];
2200 		break;
2201 	case ISCSI_NET_PARAM_IPV6_ND_REACHABLE_TMO:
2202 		if (iface_param->iface_num & 0x1)
2203 			break;
2204 		init_fw_cb->ipv6_nd_reach_time =
2205 				cpu_to_le32(*(uint32_t *)iface_param->value);
2206 		break;
2207 	case ISCSI_NET_PARAM_IPV6_ND_REXMIT_TIME:
2208 		if (iface_param->iface_num & 0x1)
2209 			break;
2210 		init_fw_cb->ipv6_nd_rexmit_timer =
2211 				cpu_to_le32(*(uint32_t *)iface_param->value);
2212 		break;
2213 	case ISCSI_NET_PARAM_IPV6_ND_STALE_TMO:
2214 		if (iface_param->iface_num & 0x1)
2215 			break;
2216 		init_fw_cb->ipv6_nd_stale_timeout =
2217 				cpu_to_le32(*(uint32_t *)iface_param->value);
2218 		break;
2219 	case ISCSI_NET_PARAM_IPV6_DUP_ADDR_DETECT_CNT:
2220 		if (iface_param->iface_num & 0x1)
2221 			break;
2222 		init_fw_cb->ipv6_dup_addr_detect_count = iface_param->value[0];
2223 		break;
2224 	case ISCSI_NET_PARAM_IPV6_RTR_ADV_LINK_MTU:
2225 		if (iface_param->iface_num & 0x1)
2226 			break;
2227 		init_fw_cb->ipv6_gw_advrt_mtu =
2228 				cpu_to_le32(*(uint32_t *)iface_param->value);
2229 		break;
2230 	default:
2231 		ql4_printk(KERN_ERR, ha, "Unknown IPv6 param = %d\n",
2232 			   iface_param->param);
2233 		break;
2234 	}
2235 }
2236 
2237 static void qla4xxx_set_ipv4(struct scsi_qla_host *ha,
2238 			     struct iscsi_iface_param_info *iface_param,
2239 			     struct addr_ctrl_blk *init_fw_cb)
2240 {
2241 	switch (iface_param->param) {
2242 	case ISCSI_NET_PARAM_IPV4_ADDR:
2243 		memcpy(init_fw_cb->ipv4_addr, iface_param->value,
2244 		       sizeof(init_fw_cb->ipv4_addr));
2245 		break;
2246 	case ISCSI_NET_PARAM_IPV4_SUBNET:
2247 		memcpy(init_fw_cb->ipv4_subnet,	iface_param->value,
2248 		       sizeof(init_fw_cb->ipv4_subnet));
2249 		break;
2250 	case ISCSI_NET_PARAM_IPV4_GW:
2251 		memcpy(init_fw_cb->ipv4_gw_addr, iface_param->value,
2252 		       sizeof(init_fw_cb->ipv4_gw_addr));
2253 		break;
2254 	case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
2255 		if (iface_param->value[0] == ISCSI_BOOTPROTO_DHCP)
2256 			init_fw_cb->ipv4_tcp_opts |=
2257 					cpu_to_le16(TCPOPT_DHCP_ENABLE);
2258 		else if (iface_param->value[0] == ISCSI_BOOTPROTO_STATIC)
2259 			init_fw_cb->ipv4_tcp_opts &=
2260 					cpu_to_le16(~TCPOPT_DHCP_ENABLE);
2261 		else
2262 			ql4_printk(KERN_ERR, ha, "Invalid IPv4 bootproto\n");
2263 		break;
2264 	case ISCSI_NET_PARAM_IFACE_ENABLE:
2265 		if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
2266 			init_fw_cb->ipv4_ip_opts |=
2267 				cpu_to_le16(IPOPT_IPV4_PROTOCOL_ENABLE);
2268 			qla4xxx_create_ipv4_iface(ha);
2269 		} else {
2270 			init_fw_cb->ipv4_ip_opts &=
2271 				cpu_to_le16(~IPOPT_IPV4_PROTOCOL_ENABLE &
2272 					    0xFFFF);
2273 			qla4xxx_destroy_ipv4_iface(ha);
2274 		}
2275 		break;
2276 	case ISCSI_NET_PARAM_VLAN_TAG:
2277 		if (iface_param->len != sizeof(init_fw_cb->ipv4_vlan_tag))
2278 			break;
2279 		init_fw_cb->ipv4_vlan_tag =
2280 				cpu_to_be16(*(uint16_t *)iface_param->value);
2281 		break;
2282 	case ISCSI_NET_PARAM_VLAN_ENABLED:
2283 		if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
2284 			init_fw_cb->ipv4_ip_opts |=
2285 					cpu_to_le16(IPOPT_VLAN_TAGGING_ENABLE);
2286 		else
2287 			init_fw_cb->ipv4_ip_opts &=
2288 					cpu_to_le16(~IPOPT_VLAN_TAGGING_ENABLE);
2289 		break;
2290 	case ISCSI_NET_PARAM_MTU:
2291 		init_fw_cb->eth_mtu_size =
2292 				cpu_to_le16(*(uint16_t *)iface_param->value);
2293 		break;
2294 	case ISCSI_NET_PARAM_PORT:
2295 		init_fw_cb->ipv4_port =
2296 				cpu_to_le16(*(uint16_t *)iface_param->value);
2297 		break;
2298 	case ISCSI_NET_PARAM_DELAYED_ACK_EN:
2299 		if (iface_param->iface_num & 0x1)
2300 			break;
2301 		if (iface_param->value[0] == ISCSI_NET_PARAM_DISABLE)
2302 			init_fw_cb->ipv4_tcp_opts |=
2303 				cpu_to_le16(TCPOPT_DELAYED_ACK_DISABLE);
2304 		else
2305 			init_fw_cb->ipv4_tcp_opts &=
2306 				cpu_to_le16(~TCPOPT_DELAYED_ACK_DISABLE &
2307 					    0xFFFF);
2308 		break;
2309 	case ISCSI_NET_PARAM_TCP_NAGLE_DISABLE:
2310 		if (iface_param->iface_num & 0x1)
2311 			break;
2312 		if (iface_param->value[0] == ISCSI_NET_PARAM_DISABLE)
2313 			init_fw_cb->ipv4_tcp_opts |=
2314 				cpu_to_le16(TCPOPT_NAGLE_ALGO_DISABLE);
2315 		else
2316 			init_fw_cb->ipv4_tcp_opts &=
2317 				cpu_to_le16(~TCPOPT_NAGLE_ALGO_DISABLE);
2318 		break;
2319 	case ISCSI_NET_PARAM_TCP_WSF_DISABLE:
2320 		if (iface_param->iface_num & 0x1)
2321 			break;
2322 		if (iface_param->value[0] == ISCSI_NET_PARAM_DISABLE)
2323 			init_fw_cb->ipv4_tcp_opts |=
2324 				cpu_to_le16(TCPOPT_WINDOW_SCALE_DISABLE);
2325 		else
2326 			init_fw_cb->ipv4_tcp_opts &=
2327 				cpu_to_le16(~TCPOPT_WINDOW_SCALE_DISABLE);
2328 		break;
2329 	case ISCSI_NET_PARAM_TCP_WSF:
2330 		if (iface_param->iface_num & 0x1)
2331 			break;
2332 		init_fw_cb->ipv4_tcp_wsf = iface_param->value[0];
2333 		break;
2334 	case ISCSI_NET_PARAM_TCP_TIMER_SCALE:
2335 		if (iface_param->iface_num & 0x1)
2336 			break;
2337 		init_fw_cb->ipv4_tcp_opts &= cpu_to_le16(~TCPOPT_TIMER_SCALE);
2338 		init_fw_cb->ipv4_tcp_opts |=
2339 				cpu_to_le16((iface_param->value[0] << 1) &
2340 					    TCPOPT_TIMER_SCALE);
2341 		break;
2342 	case ISCSI_NET_PARAM_TCP_TIMESTAMP_EN:
2343 		if (iface_param->iface_num & 0x1)
2344 			break;
2345 		if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2346 			init_fw_cb->ipv4_tcp_opts |=
2347 				cpu_to_le16(TCPOPT_TIMESTAMP_ENABLE);
2348 		else
2349 			init_fw_cb->ipv4_tcp_opts &=
2350 				cpu_to_le16(~TCPOPT_TIMESTAMP_ENABLE);
2351 		break;
2352 	case ISCSI_NET_PARAM_IPV4_DHCP_DNS_ADDR_EN:
2353 		if (iface_param->iface_num & 0x1)
2354 			break;
2355 		if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2356 			init_fw_cb->ipv4_tcp_opts |=
2357 				cpu_to_le16(TCPOPT_DNS_SERVER_IP_EN);
2358 		else
2359 			init_fw_cb->ipv4_tcp_opts &=
2360 				cpu_to_le16(~TCPOPT_DNS_SERVER_IP_EN);
2361 		break;
2362 	case ISCSI_NET_PARAM_IPV4_DHCP_SLP_DA_EN:
2363 		if (iface_param->iface_num & 0x1)
2364 			break;
2365 		if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2366 			init_fw_cb->ipv4_tcp_opts |=
2367 				cpu_to_le16(TCPOPT_SLP_DA_INFO_EN);
2368 		else
2369 			init_fw_cb->ipv4_tcp_opts &=
2370 				cpu_to_le16(~TCPOPT_SLP_DA_INFO_EN);
2371 		break;
2372 	case ISCSI_NET_PARAM_IPV4_TOS_EN:
2373 		if (iface_param->iface_num & 0x1)
2374 			break;
2375 		if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2376 			init_fw_cb->ipv4_ip_opts |=
2377 				cpu_to_le16(IPOPT_IPV4_TOS_EN);
2378 		else
2379 			init_fw_cb->ipv4_ip_opts &=
2380 				cpu_to_le16(~IPOPT_IPV4_TOS_EN);
2381 		break;
2382 	case ISCSI_NET_PARAM_IPV4_TOS:
2383 		if (iface_param->iface_num & 0x1)
2384 			break;
2385 		init_fw_cb->ipv4_tos = iface_param->value[0];
2386 		break;
2387 	case ISCSI_NET_PARAM_IPV4_GRAT_ARP_EN:
2388 		if (iface_param->iface_num & 0x1)
2389 			break;
2390 		if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2391 			init_fw_cb->ipv4_ip_opts |=
2392 					cpu_to_le16(IPOPT_GRAT_ARP_EN);
2393 		else
2394 			init_fw_cb->ipv4_ip_opts &=
2395 					cpu_to_le16(~IPOPT_GRAT_ARP_EN);
2396 		break;
2397 	case ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID_EN:
2398 		if (iface_param->iface_num & 0x1)
2399 			break;
2400 		if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2401 			init_fw_cb->ipv4_ip_opts |=
2402 				cpu_to_le16(IPOPT_ALT_CID_EN);
2403 		else
2404 			init_fw_cb->ipv4_ip_opts &=
2405 				cpu_to_le16(~IPOPT_ALT_CID_EN);
2406 		break;
2407 	case ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID:
2408 		if (iface_param->iface_num & 0x1)
2409 			break;
2410 		memcpy(init_fw_cb->ipv4_dhcp_alt_cid, iface_param->value,
2411 		       (sizeof(init_fw_cb->ipv4_dhcp_alt_cid) - 1));
2412 		init_fw_cb->ipv4_dhcp_alt_cid_len =
2413 					strlen(init_fw_cb->ipv4_dhcp_alt_cid);
2414 		break;
2415 	case ISCSI_NET_PARAM_IPV4_DHCP_REQ_VENDOR_ID_EN:
2416 		if (iface_param->iface_num & 0x1)
2417 			break;
2418 		if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2419 			init_fw_cb->ipv4_ip_opts |=
2420 					cpu_to_le16(IPOPT_REQ_VID_EN);
2421 		else
2422 			init_fw_cb->ipv4_ip_opts &=
2423 					cpu_to_le16(~IPOPT_REQ_VID_EN);
2424 		break;
2425 	case ISCSI_NET_PARAM_IPV4_DHCP_USE_VENDOR_ID_EN:
2426 		if (iface_param->iface_num & 0x1)
2427 			break;
2428 		if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2429 			init_fw_cb->ipv4_ip_opts |=
2430 					cpu_to_le16(IPOPT_USE_VID_EN);
2431 		else
2432 			init_fw_cb->ipv4_ip_opts &=
2433 					cpu_to_le16(~IPOPT_USE_VID_EN);
2434 		break;
2435 	case ISCSI_NET_PARAM_IPV4_DHCP_VENDOR_ID:
2436 		if (iface_param->iface_num & 0x1)
2437 			break;
2438 		memcpy(init_fw_cb->ipv4_dhcp_vid, iface_param->value,
2439 		       (sizeof(init_fw_cb->ipv4_dhcp_vid) - 1));
2440 		init_fw_cb->ipv4_dhcp_vid_len =
2441 					strlen(init_fw_cb->ipv4_dhcp_vid);
2442 		break;
2443 	case ISCSI_NET_PARAM_IPV4_DHCP_LEARN_IQN_EN:
2444 		if (iface_param->iface_num & 0x1)
2445 			break;
2446 		if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2447 			init_fw_cb->ipv4_ip_opts |=
2448 					cpu_to_le16(IPOPT_LEARN_IQN_EN);
2449 		else
2450 			init_fw_cb->ipv4_ip_opts &=
2451 					cpu_to_le16(~IPOPT_LEARN_IQN_EN);
2452 		break;
2453 	case ISCSI_NET_PARAM_IPV4_FRAGMENT_DISABLE:
2454 		if (iface_param->iface_num & 0x1)
2455 			break;
2456 		if (iface_param->value[0] == ISCSI_NET_PARAM_DISABLE)
2457 			init_fw_cb->ipv4_ip_opts |=
2458 				cpu_to_le16(IPOPT_FRAGMENTATION_DISABLE);
2459 		else
2460 			init_fw_cb->ipv4_ip_opts &=
2461 				cpu_to_le16(~IPOPT_FRAGMENTATION_DISABLE);
2462 		break;
2463 	case ISCSI_NET_PARAM_IPV4_IN_FORWARD_EN:
2464 		if (iface_param->iface_num & 0x1)
2465 			break;
2466 		if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2467 			init_fw_cb->ipv4_ip_opts |=
2468 				cpu_to_le16(IPOPT_IN_FORWARD_EN);
2469 		else
2470 			init_fw_cb->ipv4_ip_opts &=
2471 				cpu_to_le16(~IPOPT_IN_FORWARD_EN);
2472 		break;
2473 	case ISCSI_NET_PARAM_REDIRECT_EN:
2474 		if (iface_param->iface_num & 0x1)
2475 			break;
2476 		if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2477 			init_fw_cb->ipv4_ip_opts |=
2478 				cpu_to_le16(IPOPT_ARP_REDIRECT_EN);
2479 		else
2480 			init_fw_cb->ipv4_ip_opts &=
2481 				cpu_to_le16(~IPOPT_ARP_REDIRECT_EN);
2482 		break;
2483 	case ISCSI_NET_PARAM_IPV4_TTL:
2484 		if (iface_param->iface_num & 0x1)
2485 			break;
2486 		init_fw_cb->ipv4_ttl = iface_param->value[0];
2487 		break;
2488 	default:
2489 		ql4_printk(KERN_ERR, ha, "Unknown IPv4 param = %d\n",
2490 			   iface_param->param);
2491 		break;
2492 	}
2493 }
2494 
2495 static void qla4xxx_set_iscsi_param(struct scsi_qla_host *ha,
2496 				    struct iscsi_iface_param_info *iface_param,
2497 				    struct addr_ctrl_blk *init_fw_cb)
2498 {
2499 	switch (iface_param->param) {
2500 	case ISCSI_IFACE_PARAM_DEF_TASKMGMT_TMO:
2501 		if (iface_param->iface_num & 0x1)
2502 			break;
2503 		init_fw_cb->def_timeout =
2504 				cpu_to_le16(*(uint16_t *)iface_param->value);
2505 		break;
2506 	case ISCSI_IFACE_PARAM_HDRDGST_EN:
2507 		if (iface_param->iface_num & 0x1)
2508 			break;
2509 		if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2510 			init_fw_cb->iscsi_opts |=
2511 				cpu_to_le16(ISCSIOPTS_HEADER_DIGEST_EN);
2512 		else
2513 			init_fw_cb->iscsi_opts &=
2514 				cpu_to_le16(~ISCSIOPTS_HEADER_DIGEST_EN);
2515 		break;
2516 	case ISCSI_IFACE_PARAM_DATADGST_EN:
2517 		if (iface_param->iface_num & 0x1)
2518 			break;
2519 		if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2520 			init_fw_cb->iscsi_opts |=
2521 				cpu_to_le16(ISCSIOPTS_DATA_DIGEST_EN);
2522 		else
2523 			init_fw_cb->iscsi_opts &=
2524 				cpu_to_le16(~ISCSIOPTS_DATA_DIGEST_EN);
2525 		break;
2526 	case ISCSI_IFACE_PARAM_IMM_DATA_EN:
2527 		if (iface_param->iface_num & 0x1)
2528 			break;
2529 		if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2530 			init_fw_cb->iscsi_opts |=
2531 				cpu_to_le16(ISCSIOPTS_IMMEDIATE_DATA_EN);
2532 		else
2533 			init_fw_cb->iscsi_opts &=
2534 				cpu_to_le16(~ISCSIOPTS_IMMEDIATE_DATA_EN);
2535 		break;
2536 	case ISCSI_IFACE_PARAM_INITIAL_R2T_EN:
2537 		if (iface_param->iface_num & 0x1)
2538 			break;
2539 		if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2540 			init_fw_cb->iscsi_opts |=
2541 				cpu_to_le16(ISCSIOPTS_INITIAL_R2T_EN);
2542 		else
2543 			init_fw_cb->iscsi_opts &=
2544 				cpu_to_le16(~ISCSIOPTS_INITIAL_R2T_EN);
2545 		break;
2546 	case ISCSI_IFACE_PARAM_DATASEQ_INORDER_EN:
2547 		if (iface_param->iface_num & 0x1)
2548 			break;
2549 		if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2550 			init_fw_cb->iscsi_opts |=
2551 				cpu_to_le16(ISCSIOPTS_DATA_SEQ_INORDER_EN);
2552 		else
2553 			init_fw_cb->iscsi_opts &=
2554 				cpu_to_le16(~ISCSIOPTS_DATA_SEQ_INORDER_EN);
2555 		break;
2556 	case ISCSI_IFACE_PARAM_PDU_INORDER_EN:
2557 		if (iface_param->iface_num & 0x1)
2558 			break;
2559 		if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2560 			init_fw_cb->iscsi_opts |=
2561 				cpu_to_le16(ISCSIOPTS_DATA_PDU_INORDER_EN);
2562 		else
2563 			init_fw_cb->iscsi_opts &=
2564 				cpu_to_le16(~ISCSIOPTS_DATA_PDU_INORDER_EN);
2565 		break;
2566 	case ISCSI_IFACE_PARAM_ERL:
2567 		if (iface_param->iface_num & 0x1)
2568 			break;
2569 		init_fw_cb->iscsi_opts &= cpu_to_le16(~ISCSIOPTS_ERL);
2570 		init_fw_cb->iscsi_opts |= cpu_to_le16(iface_param->value[0] &
2571 						      ISCSIOPTS_ERL);
2572 		break;
2573 	case ISCSI_IFACE_PARAM_MAX_RECV_DLENGTH:
2574 		if (iface_param->iface_num & 0x1)
2575 			break;
2576 		init_fw_cb->iscsi_max_pdu_size =
2577 				cpu_to_le32(*(uint32_t *)iface_param->value) /
2578 				BYTE_UNITS;
2579 		break;
2580 	case ISCSI_IFACE_PARAM_FIRST_BURST:
2581 		if (iface_param->iface_num & 0x1)
2582 			break;
2583 		init_fw_cb->iscsi_fburst_len =
2584 				cpu_to_le32(*(uint32_t *)iface_param->value) /
2585 				BYTE_UNITS;
2586 		break;
2587 	case ISCSI_IFACE_PARAM_MAX_R2T:
2588 		if (iface_param->iface_num & 0x1)
2589 			break;
2590 		init_fw_cb->iscsi_max_outstnd_r2t =
2591 				cpu_to_le16(*(uint16_t *)iface_param->value);
2592 		break;
2593 	case ISCSI_IFACE_PARAM_MAX_BURST:
2594 		if (iface_param->iface_num & 0x1)
2595 			break;
2596 		init_fw_cb->iscsi_max_burst_len =
2597 				cpu_to_le32(*(uint32_t *)iface_param->value) /
2598 				BYTE_UNITS;
2599 		break;
2600 	case ISCSI_IFACE_PARAM_CHAP_AUTH_EN:
2601 		if (iface_param->iface_num & 0x1)
2602 			break;
2603 		if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2604 			init_fw_cb->iscsi_opts |=
2605 				cpu_to_le16(ISCSIOPTS_CHAP_AUTH_EN);
2606 		else
2607 			init_fw_cb->iscsi_opts &=
2608 				cpu_to_le16(~ISCSIOPTS_CHAP_AUTH_EN);
2609 		break;
2610 	case ISCSI_IFACE_PARAM_BIDI_CHAP_EN:
2611 		if (iface_param->iface_num & 0x1)
2612 			break;
2613 		if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2614 			init_fw_cb->iscsi_opts |=
2615 				cpu_to_le16(ISCSIOPTS_BIDI_CHAP_EN);
2616 		else
2617 			init_fw_cb->iscsi_opts &=
2618 				cpu_to_le16(~ISCSIOPTS_BIDI_CHAP_EN);
2619 		break;
2620 	case ISCSI_IFACE_PARAM_DISCOVERY_AUTH_OPTIONAL:
2621 		if (iface_param->iface_num & 0x1)
2622 			break;
2623 		if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2624 			init_fw_cb->iscsi_opts |=
2625 				cpu_to_le16(ISCSIOPTS_DISCOVERY_AUTH_EN);
2626 		else
2627 			init_fw_cb->iscsi_opts &=
2628 				cpu_to_le16(~ISCSIOPTS_DISCOVERY_AUTH_EN);
2629 		break;
2630 	case ISCSI_IFACE_PARAM_DISCOVERY_LOGOUT_EN:
2631 		if (iface_param->iface_num & 0x1)
2632 			break;
2633 		if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2634 			init_fw_cb->iscsi_opts |=
2635 				cpu_to_le16(ISCSIOPTS_DISCOVERY_LOGOUT_EN);
2636 		else
2637 			init_fw_cb->iscsi_opts &=
2638 				cpu_to_le16(~ISCSIOPTS_DISCOVERY_LOGOUT_EN);
2639 		break;
2640 	case ISCSI_IFACE_PARAM_STRICT_LOGIN_COMP_EN:
2641 		if (iface_param->iface_num & 0x1)
2642 			break;
2643 		if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2644 			init_fw_cb->iscsi_opts |=
2645 				cpu_to_le16(ISCSIOPTS_STRICT_LOGIN_COMP_EN);
2646 		else
2647 			init_fw_cb->iscsi_opts &=
2648 				cpu_to_le16(~ISCSIOPTS_STRICT_LOGIN_COMP_EN);
2649 		break;
2650 	default:
2651 		ql4_printk(KERN_ERR, ha, "Unknown iscsi param = %d\n",
2652 			   iface_param->param);
2653 		break;
2654 	}
2655 }
2656 
2657 static void
2658 qla4xxx_initcb_to_acb(struct addr_ctrl_blk *init_fw_cb)
2659 {
2660 	struct addr_ctrl_blk_def *acb;
2661 	acb = (struct addr_ctrl_blk_def *)init_fw_cb;
2662 	memset(acb->reserved1, 0, sizeof(acb->reserved1));
2663 	memset(acb->reserved2, 0, sizeof(acb->reserved2));
2664 	memset(acb->reserved3, 0, sizeof(acb->reserved3));
2665 	memset(acb->reserved4, 0, sizeof(acb->reserved4));
2666 	memset(acb->reserved5, 0, sizeof(acb->reserved5));
2667 	memset(acb->reserved6, 0, sizeof(acb->reserved6));
2668 	memset(acb->reserved7, 0, sizeof(acb->reserved7));
2669 	memset(acb->reserved8, 0, sizeof(acb->reserved8));
2670 	memset(acb->reserved9, 0, sizeof(acb->reserved9));
2671 	memset(acb->reserved10, 0, sizeof(acb->reserved10));
2672 	memset(acb->reserved11, 0, sizeof(acb->reserved11));
2673 	memset(acb->reserved12, 0, sizeof(acb->reserved12));
2674 	memset(acb->reserved13, 0, sizeof(acb->reserved13));
2675 	memset(acb->reserved14, 0, sizeof(acb->reserved14));
2676 	memset(acb->reserved15, 0, sizeof(acb->reserved15));
2677 }
2678 
2679 static int
2680 qla4xxx_iface_set_param(struct Scsi_Host *shost, void *data, uint32_t len)
2681 {
2682 	struct scsi_qla_host *ha = to_qla_host(shost);
2683 	int rval = 0;
2684 	struct iscsi_iface_param_info *iface_param = NULL;
2685 	struct addr_ctrl_blk *init_fw_cb = NULL;
2686 	dma_addr_t init_fw_cb_dma;
2687 	uint32_t mbox_cmd[MBOX_REG_COUNT];
2688 	uint32_t mbox_sts[MBOX_REG_COUNT];
2689 	uint32_t rem = len;
2690 	struct nlattr *attr;
2691 
2692 	init_fw_cb = dma_zalloc_coherent(&ha->pdev->dev,
2693 					 sizeof(struct addr_ctrl_blk),
2694 					 &init_fw_cb_dma, GFP_KERNEL);
2695 	if (!init_fw_cb) {
2696 		ql4_printk(KERN_ERR, ha, "%s: Unable to alloc init_cb\n",
2697 			   __func__);
2698 		return -ENOMEM;
2699 	}
2700 
2701 	memset(&mbox_cmd, 0, sizeof(mbox_cmd));
2702 	memset(&mbox_sts, 0, sizeof(mbox_sts));
2703 
2704 	if (qla4xxx_get_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma)) {
2705 		ql4_printk(KERN_ERR, ha, "%s: get ifcb failed\n", __func__);
2706 		rval = -EIO;
2707 		goto exit_init_fw_cb;
2708 	}
2709 
2710 	nla_for_each_attr(attr, data, len, rem) {
2711 		iface_param = nla_data(attr);
2712 
2713 		if (iface_param->param_type == ISCSI_NET_PARAM) {
2714 			switch (iface_param->iface_type) {
2715 			case ISCSI_IFACE_TYPE_IPV4:
2716 				switch (iface_param->iface_num) {
2717 				case 0:
2718 					qla4xxx_set_ipv4(ha, iface_param,
2719 							 init_fw_cb);
2720 					break;
2721 				default:
2722 				/* Cannot have more than one IPv4 interface */
2723 					ql4_printk(KERN_ERR, ha,
2724 						   "Invalid IPv4 iface number = %d\n",
2725 						   iface_param->iface_num);
2726 					break;
2727 				}
2728 				break;
2729 			case ISCSI_IFACE_TYPE_IPV6:
2730 				switch (iface_param->iface_num) {
2731 				case 0:
2732 				case 1:
2733 					qla4xxx_set_ipv6(ha, iface_param,
2734 							 init_fw_cb);
2735 					break;
2736 				default:
2737 				/* Cannot have more than two IPv6 interface */
2738 					ql4_printk(KERN_ERR, ha,
2739 						   "Invalid IPv6 iface number = %d\n",
2740 						   iface_param->iface_num);
2741 					break;
2742 				}
2743 				break;
2744 			default:
2745 				ql4_printk(KERN_ERR, ha,
2746 					   "Invalid iface type\n");
2747 				break;
2748 			}
2749 		} else if (iface_param->param_type == ISCSI_IFACE_PARAM) {
2750 				qla4xxx_set_iscsi_param(ha, iface_param,
2751 							init_fw_cb);
2752 		} else {
2753 			continue;
2754 		}
2755 	}
2756 
2757 	init_fw_cb->cookie = cpu_to_le32(0x11BEAD5A);
2758 
2759 	rval = qla4xxx_set_flash(ha, init_fw_cb_dma, FLASH_SEGMENT_IFCB,
2760 				 sizeof(struct addr_ctrl_blk),
2761 				 FLASH_OPT_RMW_COMMIT);
2762 	if (rval != QLA_SUCCESS) {
2763 		ql4_printk(KERN_ERR, ha, "%s: set flash mbx failed\n",
2764 			   __func__);
2765 		rval = -EIO;
2766 		goto exit_init_fw_cb;
2767 	}
2768 
2769 	rval = qla4xxx_disable_acb(ha);
2770 	if (rval != QLA_SUCCESS) {
2771 		ql4_printk(KERN_ERR, ha, "%s: disable acb mbx failed\n",
2772 			   __func__);
2773 		rval = -EIO;
2774 		goto exit_init_fw_cb;
2775 	}
2776 
2777 	wait_for_completion_timeout(&ha->disable_acb_comp,
2778 				    DISABLE_ACB_TOV * HZ);
2779 
2780 	qla4xxx_initcb_to_acb(init_fw_cb);
2781 
2782 	rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma);
2783 	if (rval != QLA_SUCCESS) {
2784 		ql4_printk(KERN_ERR, ha, "%s: set acb mbx failed\n",
2785 			   __func__);
2786 		rval = -EIO;
2787 		goto exit_init_fw_cb;
2788 	}
2789 
2790 	memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
2791 	qla4xxx_update_local_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb,
2792 				  init_fw_cb_dma);
2793 
2794 exit_init_fw_cb:
2795 	dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk),
2796 			  init_fw_cb, init_fw_cb_dma);
2797 
2798 	return rval;
2799 }
2800 
2801 static int qla4xxx_session_get_param(struct iscsi_cls_session *cls_sess,
2802 				     enum iscsi_param param, char *buf)
2803 {
2804 	struct iscsi_session *sess = cls_sess->dd_data;
2805 	struct ddb_entry *ddb_entry = sess->dd_data;
2806 	struct scsi_qla_host *ha = ddb_entry->ha;
2807 	struct iscsi_cls_conn *cls_conn = ddb_entry->conn;
2808 	struct ql4_chap_table chap_tbl;
2809 	int rval, len;
2810 	uint16_t idx;
2811 
2812 	memset(&chap_tbl, 0, sizeof(chap_tbl));
2813 	switch (param) {
2814 	case ISCSI_PARAM_CHAP_IN_IDX:
2815 		rval = qla4xxx_get_chap_index(ha, sess->username_in,
2816 					      sess->password_in, BIDI_CHAP,
2817 					      &idx);
2818 		if (rval)
2819 			len = sprintf(buf, "\n");
2820 		else
2821 			len = sprintf(buf, "%hu\n", idx);
2822 		break;
2823 	case ISCSI_PARAM_CHAP_OUT_IDX:
2824 		if (ddb_entry->ddb_type == FLASH_DDB) {
2825 			if (ddb_entry->chap_tbl_idx != INVALID_ENTRY) {
2826 				idx = ddb_entry->chap_tbl_idx;
2827 				rval = QLA_SUCCESS;
2828 			} else {
2829 				rval = QLA_ERROR;
2830 			}
2831 		} else {
2832 			rval = qla4xxx_get_chap_index(ha, sess->username,
2833 						      sess->password,
2834 						      LOCAL_CHAP, &idx);
2835 		}
2836 		if (rval)
2837 			len = sprintf(buf, "\n");
2838 		else
2839 			len = sprintf(buf, "%hu\n", idx);
2840 		break;
2841 	case ISCSI_PARAM_USERNAME:
2842 	case ISCSI_PARAM_PASSWORD:
2843 		/* First, populate session username and password for FLASH DDB,
2844 		 * if not already done. This happens when session login fails
2845 		 * for a FLASH DDB.
2846 		 */
2847 		if (ddb_entry->ddb_type == FLASH_DDB &&
2848 		    ddb_entry->chap_tbl_idx != INVALID_ENTRY &&
2849 		    !sess->username && !sess->password) {
2850 			idx = ddb_entry->chap_tbl_idx;
2851 			rval = qla4xxx_get_uni_chap_at_index(ha, chap_tbl.name,
2852 							    chap_tbl.secret,
2853 							    idx);
2854 			if (!rval) {
2855 				iscsi_set_param(cls_conn, ISCSI_PARAM_USERNAME,
2856 						(char *)chap_tbl.name,
2857 						strlen((char *)chap_tbl.name));
2858 				iscsi_set_param(cls_conn, ISCSI_PARAM_PASSWORD,
2859 						(char *)chap_tbl.secret,
2860 						chap_tbl.secret_len);
2861 			}
2862 		}
2863 		/* allow fall-through */
2864 	default:
2865 		return iscsi_session_get_param(cls_sess, param, buf);
2866 	}
2867 
2868 	return len;
2869 }
2870 
2871 static int qla4xxx_conn_get_param(struct iscsi_cls_conn *cls_conn,
2872 				  enum iscsi_param param, char *buf)
2873 {
2874 	struct iscsi_conn *conn;
2875 	struct qla_conn *qla_conn;
2876 	struct sockaddr *dst_addr;
2877 
2878 	conn = cls_conn->dd_data;
2879 	qla_conn = conn->dd_data;
2880 	dst_addr = (struct sockaddr *)&qla_conn->qla_ep->dst_addr;
2881 
2882 	switch (param) {
2883 	case ISCSI_PARAM_CONN_PORT:
2884 	case ISCSI_PARAM_CONN_ADDRESS:
2885 		return iscsi_conn_get_addr_param((struct sockaddr_storage *)
2886 						 dst_addr, param, buf);
2887 	default:
2888 		return iscsi_conn_get_param(cls_conn, param, buf);
2889 	}
2890 }
2891 
2892 int qla4xxx_get_ddb_index(struct scsi_qla_host *ha, uint16_t *ddb_index)
2893 {
2894 	uint32_t mbx_sts = 0;
2895 	uint16_t tmp_ddb_index;
2896 	int ret;
2897 
2898 get_ddb_index:
2899 	tmp_ddb_index = find_first_zero_bit(ha->ddb_idx_map, MAX_DDB_ENTRIES);
2900 
2901 	if (tmp_ddb_index >= MAX_DDB_ENTRIES) {
2902 		DEBUG2(ql4_printk(KERN_INFO, ha,
2903 				  "Free DDB index not available\n"));
2904 		ret = QLA_ERROR;
2905 		goto exit_get_ddb_index;
2906 	}
2907 
2908 	if (test_and_set_bit(tmp_ddb_index, ha->ddb_idx_map))
2909 		goto get_ddb_index;
2910 
2911 	DEBUG2(ql4_printk(KERN_INFO, ha,
2912 			  "Found a free DDB index at %d\n", tmp_ddb_index));
2913 	ret = qla4xxx_req_ddb_entry(ha, tmp_ddb_index, &mbx_sts);
2914 	if (ret == QLA_ERROR) {
2915 		if (mbx_sts == MBOX_STS_COMMAND_ERROR) {
2916 			ql4_printk(KERN_INFO, ha,
2917 				   "DDB index = %d not available trying next\n",
2918 				   tmp_ddb_index);
2919 			goto get_ddb_index;
2920 		}
2921 		DEBUG2(ql4_printk(KERN_INFO, ha,
2922 				  "Free FW DDB not available\n"));
2923 	}
2924 
2925 	*ddb_index = tmp_ddb_index;
2926 
2927 exit_get_ddb_index:
2928 	return ret;
2929 }
2930 
2931 static int qla4xxx_match_ipaddress(struct scsi_qla_host *ha,
2932 				   struct ddb_entry *ddb_entry,
2933 				   char *existing_ipaddr,
2934 				   char *user_ipaddr)
2935 {
2936 	uint8_t dst_ipaddr[IPv6_ADDR_LEN];
2937 	char formatted_ipaddr[DDB_IPADDR_LEN];
2938 	int status = QLA_SUCCESS, ret = 0;
2939 
2940 	if (ddb_entry->fw_ddb_entry.options & DDB_OPT_IPV6_DEVICE) {
2941 		ret = in6_pton(user_ipaddr, strlen(user_ipaddr), dst_ipaddr,
2942 			       '\0', NULL);
2943 		if (ret == 0) {
2944 			status = QLA_ERROR;
2945 			goto out_match;
2946 		}
2947 		ret = sprintf(formatted_ipaddr, "%pI6", dst_ipaddr);
2948 	} else {
2949 		ret = in4_pton(user_ipaddr, strlen(user_ipaddr), dst_ipaddr,
2950 			       '\0', NULL);
2951 		if (ret == 0) {
2952 			status = QLA_ERROR;
2953 			goto out_match;
2954 		}
2955 		ret = sprintf(formatted_ipaddr, "%pI4", dst_ipaddr);
2956 	}
2957 
2958 	if (strcmp(existing_ipaddr, formatted_ipaddr))
2959 		status = QLA_ERROR;
2960 
2961 out_match:
2962 	return status;
2963 }
2964 
2965 static int qla4xxx_match_fwdb_session(struct scsi_qla_host *ha,
2966 				      struct iscsi_cls_conn *cls_conn)
2967 {
2968 	int idx = 0, max_ddbs, rval;
2969 	struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
2970 	struct iscsi_session *sess, *existing_sess;
2971 	struct iscsi_conn *conn, *existing_conn;
2972 	struct ddb_entry *ddb_entry;
2973 
2974 	sess = cls_sess->dd_data;
2975 	conn = cls_conn->dd_data;
2976 
2977 	if (sess->targetname == NULL ||
2978 	    conn->persistent_address == NULL ||
2979 	    conn->persistent_port == 0)
2980 		return QLA_ERROR;
2981 
2982 	max_ddbs =  is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
2983 				     MAX_DEV_DB_ENTRIES;
2984 
2985 	for (idx = 0; idx < max_ddbs; idx++) {
2986 		ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
2987 		if (ddb_entry == NULL)
2988 			continue;
2989 
2990 		if (ddb_entry->ddb_type != FLASH_DDB)
2991 			continue;
2992 
2993 		existing_sess = ddb_entry->sess->dd_data;
2994 		existing_conn = ddb_entry->conn->dd_data;
2995 
2996 		if (existing_sess->targetname == NULL ||
2997 		    existing_conn->persistent_address == NULL ||
2998 		    existing_conn->persistent_port == 0)
2999 			continue;
3000 
3001 		DEBUG2(ql4_printk(KERN_INFO, ha,
3002 				  "IQN = %s User IQN = %s\n",
3003 				  existing_sess->targetname,
3004 				  sess->targetname));
3005 
3006 		DEBUG2(ql4_printk(KERN_INFO, ha,
3007 				  "IP = %s User IP = %s\n",
3008 				  existing_conn->persistent_address,
3009 				  conn->persistent_address));
3010 
3011 		DEBUG2(ql4_printk(KERN_INFO, ha,
3012 				  "Port = %d User Port = %d\n",
3013 				  existing_conn->persistent_port,
3014 				  conn->persistent_port));
3015 
3016 		if (strcmp(existing_sess->targetname, sess->targetname))
3017 			continue;
3018 		rval = qla4xxx_match_ipaddress(ha, ddb_entry,
3019 					existing_conn->persistent_address,
3020 					conn->persistent_address);
3021 		if (rval == QLA_ERROR)
3022 			continue;
3023 		if (existing_conn->persistent_port != conn->persistent_port)
3024 			continue;
3025 		break;
3026 	}
3027 
3028 	if (idx == max_ddbs)
3029 		return QLA_ERROR;
3030 
3031 	DEBUG2(ql4_printk(KERN_INFO, ha,
3032 			  "Match found in fwdb sessions\n"));
3033 	return QLA_SUCCESS;
3034 }
3035 
3036 static struct iscsi_cls_session *
3037 qla4xxx_session_create(struct iscsi_endpoint *ep,
3038 			uint16_t cmds_max, uint16_t qdepth,
3039 			uint32_t initial_cmdsn)
3040 {
3041 	struct iscsi_cls_session *cls_sess;
3042 	struct scsi_qla_host *ha;
3043 	struct qla_endpoint *qla_ep;
3044 	struct ddb_entry *ddb_entry;
3045 	uint16_t ddb_index;
3046 	struct iscsi_session *sess;
3047 	struct sockaddr *dst_addr;
3048 	int ret;
3049 
3050 	if (!ep) {
3051 		printk(KERN_ERR "qla4xxx: missing ep.\n");
3052 		return NULL;
3053 	}
3054 
3055 	qla_ep = ep->dd_data;
3056 	dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
3057 	ha = to_qla_host(qla_ep->host);
3058 	DEBUG2(ql4_printk(KERN_INFO, ha, "%s: host: %ld\n", __func__,
3059 			  ha->host_no));
3060 
3061 	ret = qla4xxx_get_ddb_index(ha, &ddb_index);
3062 	if (ret == QLA_ERROR)
3063 		return NULL;
3064 
3065 	cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport, qla_ep->host,
3066 				       cmds_max, sizeof(struct ddb_entry),
3067 				       sizeof(struct ql4_task_data),
3068 				       initial_cmdsn, ddb_index);
3069 	if (!cls_sess)
3070 		return NULL;
3071 
3072 	sess = cls_sess->dd_data;
3073 	ddb_entry = sess->dd_data;
3074 	ddb_entry->fw_ddb_index = ddb_index;
3075 	ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
3076 	ddb_entry->ha = ha;
3077 	ddb_entry->sess = cls_sess;
3078 	ddb_entry->unblock_sess = qla4xxx_unblock_ddb;
3079 	ddb_entry->ddb_change = qla4xxx_ddb_change;
3080 	clear_bit(DDB_CONN_CLOSE_FAILURE, &ddb_entry->flags);
3081 	cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
3082 	ha->fw_ddb_index_map[ddb_entry->fw_ddb_index] = ddb_entry;
3083 	ha->tot_ddbs++;
3084 
3085 	return cls_sess;
3086 }
3087 
3088 static void qla4xxx_session_destroy(struct iscsi_cls_session *cls_sess)
3089 {
3090 	struct iscsi_session *sess;
3091 	struct ddb_entry *ddb_entry;
3092 	struct scsi_qla_host *ha;
3093 	unsigned long flags, wtime;
3094 	struct dev_db_entry *fw_ddb_entry = NULL;
3095 	dma_addr_t fw_ddb_entry_dma;
3096 	uint32_t ddb_state;
3097 	int ret;
3098 
3099 	sess = cls_sess->dd_data;
3100 	ddb_entry = sess->dd_data;
3101 	ha = ddb_entry->ha;
3102 	DEBUG2(ql4_printk(KERN_INFO, ha, "%s: host: %ld\n", __func__,
3103 			  ha->host_no));
3104 
3105 	fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3106 					  &fw_ddb_entry_dma, GFP_KERNEL);
3107 	if (!fw_ddb_entry) {
3108 		ql4_printk(KERN_ERR, ha,
3109 			   "%s: Unable to allocate dma buffer\n", __func__);
3110 		goto destroy_session;
3111 	}
3112 
3113 	wtime = jiffies + (HZ * LOGOUT_TOV);
3114 	do {
3115 		ret = qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index,
3116 					      fw_ddb_entry, fw_ddb_entry_dma,
3117 					      NULL, NULL, &ddb_state, NULL,
3118 					      NULL, NULL);
3119 		if (ret == QLA_ERROR)
3120 			goto destroy_session;
3121 
3122 		if ((ddb_state == DDB_DS_NO_CONNECTION_ACTIVE) ||
3123 		    (ddb_state == DDB_DS_SESSION_FAILED))
3124 			goto destroy_session;
3125 
3126 		schedule_timeout_uninterruptible(HZ);
3127 	} while ((time_after(wtime, jiffies)));
3128 
3129 destroy_session:
3130 	qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
3131 	if (test_and_clear_bit(DDB_CONN_CLOSE_FAILURE, &ddb_entry->flags))
3132 		clear_bit(ddb_entry->fw_ddb_index, ha->ddb_idx_map);
3133 	spin_lock_irqsave(&ha->hardware_lock, flags);
3134 	qla4xxx_free_ddb(ha, ddb_entry);
3135 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
3136 
3137 	iscsi_session_teardown(cls_sess);
3138 
3139 	if (fw_ddb_entry)
3140 		dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3141 				  fw_ddb_entry, fw_ddb_entry_dma);
3142 }
3143 
3144 static struct iscsi_cls_conn *
3145 qla4xxx_conn_create(struct iscsi_cls_session *cls_sess, uint32_t conn_idx)
3146 {
3147 	struct iscsi_cls_conn *cls_conn;
3148 	struct iscsi_session *sess;
3149 	struct ddb_entry *ddb_entry;
3150 	struct scsi_qla_host *ha;
3151 
3152 	cls_conn = iscsi_conn_setup(cls_sess, sizeof(struct qla_conn),
3153 				    conn_idx);
3154 	if (!cls_conn) {
3155 		pr_info("%s: Can not create connection for conn_idx = %u\n",
3156 			__func__, conn_idx);
3157 		return NULL;
3158 	}
3159 
3160 	sess = cls_sess->dd_data;
3161 	ddb_entry = sess->dd_data;
3162 	ddb_entry->conn = cls_conn;
3163 
3164 	ha = ddb_entry->ha;
3165 	DEBUG2(ql4_printk(KERN_INFO, ha, "%s: conn_idx = %u\n", __func__,
3166 			  conn_idx));
3167 	return cls_conn;
3168 }
3169 
3170 static int qla4xxx_conn_bind(struct iscsi_cls_session *cls_session,
3171 			     struct iscsi_cls_conn *cls_conn,
3172 			     uint64_t transport_fd, int is_leading)
3173 {
3174 	struct iscsi_conn *conn;
3175 	struct qla_conn *qla_conn;
3176 	struct iscsi_endpoint *ep;
3177 	struct ddb_entry *ddb_entry;
3178 	struct scsi_qla_host *ha;
3179 	struct iscsi_session *sess;
3180 
3181 	sess = cls_session->dd_data;
3182 	ddb_entry = sess->dd_data;
3183 	ha = ddb_entry->ha;
3184 
3185 	DEBUG2(ql4_printk(KERN_INFO, ha, "%s: sid = %d, cid = %d\n", __func__,
3186 			  cls_session->sid, cls_conn->cid));
3187 
3188 	if (iscsi_conn_bind(cls_session, cls_conn, is_leading))
3189 		return -EINVAL;
3190 	ep = iscsi_lookup_endpoint(transport_fd);
3191 	conn = cls_conn->dd_data;
3192 	qla_conn = conn->dd_data;
3193 	qla_conn->qla_ep = ep->dd_data;
3194 	return 0;
3195 }
3196 
3197 static int qla4xxx_conn_start(struct iscsi_cls_conn *cls_conn)
3198 {
3199 	struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
3200 	struct iscsi_session *sess;
3201 	struct ddb_entry *ddb_entry;
3202 	struct scsi_qla_host *ha;
3203 	struct dev_db_entry *fw_ddb_entry = NULL;
3204 	dma_addr_t fw_ddb_entry_dma;
3205 	uint32_t mbx_sts = 0;
3206 	int ret = 0;
3207 	int status = QLA_SUCCESS;
3208 
3209 	sess = cls_sess->dd_data;
3210 	ddb_entry = sess->dd_data;
3211 	ha = ddb_entry->ha;
3212 	DEBUG2(ql4_printk(KERN_INFO, ha, "%s: sid = %d, cid = %d\n", __func__,
3213 			  cls_sess->sid, cls_conn->cid));
3214 
3215 	/* Check if we have  matching FW DDB, if yes then do not
3216 	 * login to this target. This could cause target to logout previous
3217 	 * connection
3218 	 */
3219 	ret = qla4xxx_match_fwdb_session(ha, cls_conn);
3220 	if (ret == QLA_SUCCESS) {
3221 		ql4_printk(KERN_INFO, ha,
3222 			   "Session already exist in FW.\n");
3223 		ret = -EEXIST;
3224 		goto exit_conn_start;
3225 	}
3226 
3227 	fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3228 					  &fw_ddb_entry_dma, GFP_KERNEL);
3229 	if (!fw_ddb_entry) {
3230 		ql4_printk(KERN_ERR, ha,
3231 			   "%s: Unable to allocate dma buffer\n", __func__);
3232 		ret = -ENOMEM;
3233 		goto exit_conn_start;
3234 	}
3235 
3236 	ret = qla4xxx_set_param_ddbentry(ha, ddb_entry, cls_conn, &mbx_sts);
3237 	if (ret) {
3238 		/* If iscsid is stopped and started then no need to do
3239 		* set param again since ddb state will be already
3240 		* active and FW does not allow set ddb to an
3241 		* active session.
3242 		*/
3243 		if (mbx_sts)
3244 			if (ddb_entry->fw_ddb_device_state ==
3245 						DDB_DS_SESSION_ACTIVE) {
3246 				ddb_entry->unblock_sess(ddb_entry->sess);
3247 				goto exit_set_param;
3248 			}
3249 
3250 		ql4_printk(KERN_ERR, ha, "%s: Failed set param for index[%d]\n",
3251 			   __func__, ddb_entry->fw_ddb_index);
3252 		goto exit_conn_start;
3253 	}
3254 
3255 	status = qla4xxx_conn_open(ha, ddb_entry->fw_ddb_index);
3256 	if (status == QLA_ERROR) {
3257 		ql4_printk(KERN_ERR, ha, "%s: Login failed: %s\n", __func__,
3258 			   sess->targetname);
3259 		ret = -EINVAL;
3260 		goto exit_conn_start;
3261 	}
3262 
3263 	if (ddb_entry->fw_ddb_device_state == DDB_DS_NO_CONNECTION_ACTIVE)
3264 		ddb_entry->fw_ddb_device_state = DDB_DS_LOGIN_IN_PROCESS;
3265 
3266 	DEBUG2(printk(KERN_INFO "%s: DDB state [%d]\n", __func__,
3267 		      ddb_entry->fw_ddb_device_state));
3268 
3269 exit_set_param:
3270 	ret = 0;
3271 
3272 exit_conn_start:
3273 	if (fw_ddb_entry)
3274 		dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3275 				  fw_ddb_entry, fw_ddb_entry_dma);
3276 	return ret;
3277 }
3278 
3279 static void qla4xxx_conn_destroy(struct iscsi_cls_conn *cls_conn)
3280 {
3281 	struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
3282 	struct iscsi_session *sess;
3283 	struct scsi_qla_host *ha;
3284 	struct ddb_entry *ddb_entry;
3285 	int options;
3286 
3287 	sess = cls_sess->dd_data;
3288 	ddb_entry = sess->dd_data;
3289 	ha = ddb_entry->ha;
3290 	DEBUG2(ql4_printk(KERN_INFO, ha, "%s: cid = %d\n", __func__,
3291 			  cls_conn->cid));
3292 
3293 	options = LOGOUT_OPTION_CLOSE_SESSION;
3294 	if (qla4xxx_session_logout_ddb(ha, ddb_entry, options) == QLA_ERROR)
3295 		ql4_printk(KERN_ERR, ha, "%s: Logout failed\n", __func__);
3296 }
3297 
3298 static void qla4xxx_task_work(struct work_struct *wdata)
3299 {
3300 	struct ql4_task_data *task_data;
3301 	struct scsi_qla_host *ha;
3302 	struct passthru_status *sts;
3303 	struct iscsi_task *task;
3304 	struct iscsi_hdr *hdr;
3305 	uint8_t *data;
3306 	uint32_t data_len;
3307 	struct iscsi_conn *conn;
3308 	int hdr_len;
3309 	itt_t itt;
3310 
3311 	task_data = container_of(wdata, struct ql4_task_data, task_work);
3312 	ha = task_data->ha;
3313 	task = task_data->task;
3314 	sts = &task_data->sts;
3315 	hdr_len = sizeof(struct iscsi_hdr);
3316 
3317 	DEBUG3(printk(KERN_INFO "Status returned\n"));
3318 	DEBUG3(qla4xxx_dump_buffer(sts, 64));
3319 	DEBUG3(printk(KERN_INFO "Response buffer"));
3320 	DEBUG3(qla4xxx_dump_buffer(task_data->resp_buffer, 64));
3321 
3322 	conn = task->conn;
3323 
3324 	switch (sts->completionStatus) {
3325 	case PASSTHRU_STATUS_COMPLETE:
3326 		hdr = (struct iscsi_hdr *)task_data->resp_buffer;
3327 		/* Assign back the itt in hdr, until we use the PREASSIGN_TAG */
3328 		itt = sts->handle;
3329 		hdr->itt = itt;
3330 		data = task_data->resp_buffer + hdr_len;
3331 		data_len = task_data->resp_len - hdr_len;
3332 		iscsi_complete_pdu(conn, hdr, data, data_len);
3333 		break;
3334 	default:
3335 		ql4_printk(KERN_ERR, ha, "Passthru failed status = 0x%x\n",
3336 			   sts->completionStatus);
3337 		break;
3338 	}
3339 	return;
3340 }
3341 
3342 static int qla4xxx_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
3343 {
3344 	struct ql4_task_data *task_data;
3345 	struct iscsi_session *sess;
3346 	struct ddb_entry *ddb_entry;
3347 	struct scsi_qla_host *ha;
3348 	int hdr_len;
3349 
3350 	sess = task->conn->session;
3351 	ddb_entry = sess->dd_data;
3352 	ha = ddb_entry->ha;
3353 	task_data = task->dd_data;
3354 	memset(task_data, 0, sizeof(struct ql4_task_data));
3355 
3356 	if (task->sc) {
3357 		ql4_printk(KERN_INFO, ha,
3358 			   "%s: SCSI Commands not implemented\n", __func__);
3359 		return -EINVAL;
3360 	}
3361 
3362 	hdr_len = sizeof(struct iscsi_hdr);
3363 	task_data->ha = ha;
3364 	task_data->task = task;
3365 
3366 	if (task->data_count) {
3367 		task_data->data_dma = dma_map_single(&ha->pdev->dev, task->data,
3368 						     task->data_count,
3369 						     PCI_DMA_TODEVICE);
3370 	}
3371 
3372 	DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
3373 		      __func__, task->conn->max_recv_dlength, hdr_len));
3374 
3375 	task_data->resp_len = task->conn->max_recv_dlength + hdr_len;
3376 	task_data->resp_buffer = dma_alloc_coherent(&ha->pdev->dev,
3377 						    task_data->resp_len,
3378 						    &task_data->resp_dma,
3379 						    GFP_ATOMIC);
3380 	if (!task_data->resp_buffer)
3381 		goto exit_alloc_pdu;
3382 
3383 	task_data->req_len = task->data_count + hdr_len;
3384 	task_data->req_buffer = dma_alloc_coherent(&ha->pdev->dev,
3385 						   task_data->req_len,
3386 						   &task_data->req_dma,
3387 						   GFP_ATOMIC);
3388 	if (!task_data->req_buffer)
3389 		goto exit_alloc_pdu;
3390 
3391 	task->hdr = task_data->req_buffer;
3392 
3393 	INIT_WORK(&task_data->task_work, qla4xxx_task_work);
3394 
3395 	return 0;
3396 
3397 exit_alloc_pdu:
3398 	if (task_data->resp_buffer)
3399 		dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
3400 				  task_data->resp_buffer, task_data->resp_dma);
3401 
3402 	if (task_data->req_buffer)
3403 		dma_free_coherent(&ha->pdev->dev, task_data->req_len,
3404 				  task_data->req_buffer, task_data->req_dma);
3405 	return -ENOMEM;
3406 }
3407 
3408 static void qla4xxx_task_cleanup(struct iscsi_task *task)
3409 {
3410 	struct ql4_task_data *task_data;
3411 	struct iscsi_session *sess;
3412 	struct ddb_entry *ddb_entry;
3413 	struct scsi_qla_host *ha;
3414 	int hdr_len;
3415 
3416 	hdr_len = sizeof(struct iscsi_hdr);
3417 	sess = task->conn->session;
3418 	ddb_entry = sess->dd_data;
3419 	ha = ddb_entry->ha;
3420 	task_data = task->dd_data;
3421 
3422 	if (task->data_count) {
3423 		dma_unmap_single(&ha->pdev->dev, task_data->data_dma,
3424 				 task->data_count, PCI_DMA_TODEVICE);
3425 	}
3426 
3427 	DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
3428 		      __func__, task->conn->max_recv_dlength, hdr_len));
3429 
3430 	dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
3431 			  task_data->resp_buffer, task_data->resp_dma);
3432 	dma_free_coherent(&ha->pdev->dev, task_data->req_len,
3433 			  task_data->req_buffer, task_data->req_dma);
3434 	return;
3435 }
3436 
3437 static int qla4xxx_task_xmit(struct iscsi_task *task)
3438 {
3439 	struct scsi_cmnd *sc = task->sc;
3440 	struct iscsi_session *sess = task->conn->session;
3441 	struct ddb_entry *ddb_entry = sess->dd_data;
3442 	struct scsi_qla_host *ha = ddb_entry->ha;
3443 
3444 	if (!sc)
3445 		return qla4xxx_send_passthru0(task);
3446 
3447 	ql4_printk(KERN_INFO, ha, "%s: scsi cmd xmit not implemented\n",
3448 		   __func__);
3449 	return -ENOSYS;
3450 }
3451 
3452 static int qla4xxx_copy_from_fwddb_param(struct iscsi_bus_flash_session *sess,
3453 					 struct iscsi_bus_flash_conn *conn,
3454 					 struct dev_db_entry *fw_ddb_entry)
3455 {
3456 	unsigned long options = 0;
3457 	int rc = 0;
3458 
3459 	options = le16_to_cpu(fw_ddb_entry->options);
3460 	conn->is_fw_assigned_ipv6 = test_bit(OPT_IS_FW_ASSIGNED_IPV6, &options);
3461 	if (test_bit(OPT_IPV6_DEVICE, &options)) {
3462 		rc = iscsi_switch_str_param(&sess->portal_type,
3463 					    PORTAL_TYPE_IPV6);
3464 		if (rc)
3465 			goto exit_copy;
3466 	} else {
3467 		rc = iscsi_switch_str_param(&sess->portal_type,
3468 					    PORTAL_TYPE_IPV4);
3469 		if (rc)
3470 			goto exit_copy;
3471 	}
3472 
3473 	sess->auto_snd_tgt_disable = test_bit(OPT_AUTO_SENDTGTS_DISABLE,
3474 					      &options);
3475 	sess->discovery_sess = test_bit(OPT_DISC_SESSION, &options);
3476 	sess->entry_state = test_bit(OPT_ENTRY_STATE, &options);
3477 
3478 	options = le16_to_cpu(fw_ddb_entry->iscsi_options);
3479 	conn->hdrdgst_en = test_bit(ISCSIOPT_HEADER_DIGEST_EN, &options);
3480 	conn->datadgst_en = test_bit(ISCSIOPT_DATA_DIGEST_EN, &options);
3481 	sess->imm_data_en = test_bit(ISCSIOPT_IMMEDIATE_DATA_EN, &options);
3482 	sess->initial_r2t_en = test_bit(ISCSIOPT_INITIAL_R2T_EN, &options);
3483 	sess->dataseq_inorder_en = test_bit(ISCSIOPT_DATA_SEQ_IN_ORDER,
3484 					    &options);
3485 	sess->pdu_inorder_en = test_bit(ISCSIOPT_DATA_PDU_IN_ORDER, &options);
3486 	sess->chap_auth_en = test_bit(ISCSIOPT_CHAP_AUTH_EN, &options);
3487 	conn->snack_req_en = test_bit(ISCSIOPT_SNACK_REQ_EN, &options);
3488 	sess->discovery_logout_en = test_bit(ISCSIOPT_DISCOVERY_LOGOUT_EN,
3489 					     &options);
3490 	sess->bidi_chap_en = test_bit(ISCSIOPT_BIDI_CHAP_EN, &options);
3491 	sess->discovery_auth_optional =
3492 			test_bit(ISCSIOPT_DISCOVERY_AUTH_OPTIONAL, &options);
3493 	if (test_bit(ISCSIOPT_ERL1, &options))
3494 		sess->erl |= BIT_1;
3495 	if (test_bit(ISCSIOPT_ERL0, &options))
3496 		sess->erl |= BIT_0;
3497 
3498 	options = le16_to_cpu(fw_ddb_entry->tcp_options);
3499 	conn->tcp_timestamp_stat = test_bit(TCPOPT_TIMESTAMP_STAT, &options);
3500 	conn->tcp_nagle_disable = test_bit(TCPOPT_NAGLE_DISABLE, &options);
3501 	conn->tcp_wsf_disable = test_bit(TCPOPT_WSF_DISABLE, &options);
3502 	if (test_bit(TCPOPT_TIMER_SCALE3, &options))
3503 		conn->tcp_timer_scale |= BIT_3;
3504 	if (test_bit(TCPOPT_TIMER_SCALE2, &options))
3505 		conn->tcp_timer_scale |= BIT_2;
3506 	if (test_bit(TCPOPT_TIMER_SCALE1, &options))
3507 		conn->tcp_timer_scale |= BIT_1;
3508 
3509 	conn->tcp_timer_scale >>= 1;
3510 	conn->tcp_timestamp_en = test_bit(TCPOPT_TIMESTAMP_EN, &options);
3511 
3512 	options = le16_to_cpu(fw_ddb_entry->ip_options);
3513 	conn->fragment_disable = test_bit(IPOPT_FRAGMENT_DISABLE, &options);
3514 
3515 	conn->max_recv_dlength = BYTE_UNITS *
3516 			  le16_to_cpu(fw_ddb_entry->iscsi_max_rcv_data_seg_len);
3517 	conn->max_xmit_dlength = BYTE_UNITS *
3518 			  le16_to_cpu(fw_ddb_entry->iscsi_max_snd_data_seg_len);
3519 	sess->first_burst = BYTE_UNITS *
3520 			       le16_to_cpu(fw_ddb_entry->iscsi_first_burst_len);
3521 	sess->max_burst = BYTE_UNITS *
3522 				 le16_to_cpu(fw_ddb_entry->iscsi_max_burst_len);
3523 	sess->max_r2t = le16_to_cpu(fw_ddb_entry->iscsi_max_outsnd_r2t);
3524 	sess->time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
3525 	sess->time2retain = le16_to_cpu(fw_ddb_entry->iscsi_def_time2retain);
3526 	sess->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
3527 	conn->max_segment_size = le16_to_cpu(fw_ddb_entry->mss);
3528 	conn->tcp_xmit_wsf = fw_ddb_entry->tcp_xmt_wsf;
3529 	conn->tcp_recv_wsf = fw_ddb_entry->tcp_rcv_wsf;
3530 	conn->ipv6_flow_label = le16_to_cpu(fw_ddb_entry->ipv6_flow_lbl);
3531 	conn->keepalive_timeout = le16_to_cpu(fw_ddb_entry->ka_timeout);
3532 	conn->local_port = le16_to_cpu(fw_ddb_entry->lcl_port);
3533 	conn->statsn = le32_to_cpu(fw_ddb_entry->stat_sn);
3534 	conn->exp_statsn = le32_to_cpu(fw_ddb_entry->exp_stat_sn);
3535 	sess->discovery_parent_idx = le16_to_cpu(fw_ddb_entry->ddb_link);
3536 	sess->discovery_parent_type = le16_to_cpu(fw_ddb_entry->ddb_link);
3537 	sess->chap_out_idx = le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
3538 	sess->tsid = le16_to_cpu(fw_ddb_entry->tsid);
3539 
3540 	sess->default_taskmgmt_timeout =
3541 				le16_to_cpu(fw_ddb_entry->def_timeout);
3542 	conn->port = le16_to_cpu(fw_ddb_entry->port);
3543 
3544 	options = le16_to_cpu(fw_ddb_entry->options);
3545 	conn->ipaddress = kzalloc(IPv6_ADDR_LEN, GFP_KERNEL);
3546 	if (!conn->ipaddress) {
3547 		rc = -ENOMEM;
3548 		goto exit_copy;
3549 	}
3550 
3551 	conn->redirect_ipaddr = kzalloc(IPv6_ADDR_LEN, GFP_KERNEL);
3552 	if (!conn->redirect_ipaddr) {
3553 		rc = -ENOMEM;
3554 		goto exit_copy;
3555 	}
3556 
3557 	memcpy(conn->ipaddress, fw_ddb_entry->ip_addr, IPv6_ADDR_LEN);
3558 	memcpy(conn->redirect_ipaddr, fw_ddb_entry->tgt_addr, IPv6_ADDR_LEN);
3559 
3560 	if (test_bit(OPT_IPV6_DEVICE, &options)) {
3561 		conn->ipv6_traffic_class = fw_ddb_entry->ipv4_tos;
3562 
3563 		conn->link_local_ipv6_addr = kmemdup(
3564 					fw_ddb_entry->link_local_ipv6_addr,
3565 					IPv6_ADDR_LEN, GFP_KERNEL);
3566 		if (!conn->link_local_ipv6_addr) {
3567 			rc = -ENOMEM;
3568 			goto exit_copy;
3569 		}
3570 	} else {
3571 		conn->ipv4_tos = fw_ddb_entry->ipv4_tos;
3572 	}
3573 
3574 	if (fw_ddb_entry->iscsi_name[0]) {
3575 		rc = iscsi_switch_str_param(&sess->targetname,
3576 					    (char *)fw_ddb_entry->iscsi_name);
3577 		if (rc)
3578 			goto exit_copy;
3579 	}
3580 
3581 	if (fw_ddb_entry->iscsi_alias[0]) {
3582 		rc = iscsi_switch_str_param(&sess->targetalias,
3583 					    (char *)fw_ddb_entry->iscsi_alias);
3584 		if (rc)
3585 			goto exit_copy;
3586 	}
3587 
3588 	COPY_ISID(sess->isid, fw_ddb_entry->isid);
3589 
3590 exit_copy:
3591 	return rc;
3592 }
3593 
3594 static int qla4xxx_copy_to_fwddb_param(struct iscsi_bus_flash_session *sess,
3595 				       struct iscsi_bus_flash_conn *conn,
3596 				       struct dev_db_entry *fw_ddb_entry)
3597 {
3598 	uint16_t options;
3599 	int rc = 0;
3600 
3601 	options = le16_to_cpu(fw_ddb_entry->options);
3602 	SET_BITVAL(conn->is_fw_assigned_ipv6,  options, BIT_11);
3603 	if (!strncmp(sess->portal_type, PORTAL_TYPE_IPV6, 4))
3604 		options |= BIT_8;
3605 	else
3606 		options &= ~BIT_8;
3607 
3608 	SET_BITVAL(sess->auto_snd_tgt_disable, options, BIT_6);
3609 	SET_BITVAL(sess->discovery_sess, options, BIT_4);
3610 	SET_BITVAL(sess->entry_state, options, BIT_3);
3611 	fw_ddb_entry->options = cpu_to_le16(options);
3612 
3613 	options = le16_to_cpu(fw_ddb_entry->iscsi_options);
3614 	SET_BITVAL(conn->hdrdgst_en, options, BIT_13);
3615 	SET_BITVAL(conn->datadgst_en, options, BIT_12);
3616 	SET_BITVAL(sess->imm_data_en, options, BIT_11);
3617 	SET_BITVAL(sess->initial_r2t_en, options, BIT_10);
3618 	SET_BITVAL(sess->dataseq_inorder_en, options, BIT_9);
3619 	SET_BITVAL(sess->pdu_inorder_en, options, BIT_8);
3620 	SET_BITVAL(sess->chap_auth_en, options, BIT_7);
3621 	SET_BITVAL(conn->snack_req_en, options, BIT_6);
3622 	SET_BITVAL(sess->discovery_logout_en, options, BIT_5);
3623 	SET_BITVAL(sess->bidi_chap_en, options, BIT_4);
3624 	SET_BITVAL(sess->discovery_auth_optional, options, BIT_3);
3625 	SET_BITVAL(sess->erl & BIT_1, options, BIT_1);
3626 	SET_BITVAL(sess->erl & BIT_0, options, BIT_0);
3627 	fw_ddb_entry->iscsi_options = cpu_to_le16(options);
3628 
3629 	options = le16_to_cpu(fw_ddb_entry->tcp_options);
3630 	SET_BITVAL(conn->tcp_timestamp_stat, options, BIT_6);
3631 	SET_BITVAL(conn->tcp_nagle_disable, options, BIT_5);
3632 	SET_BITVAL(conn->tcp_wsf_disable, options, BIT_4);
3633 	SET_BITVAL(conn->tcp_timer_scale & BIT_2, options, BIT_3);
3634 	SET_BITVAL(conn->tcp_timer_scale & BIT_1, options, BIT_2);
3635 	SET_BITVAL(conn->tcp_timer_scale & BIT_0, options, BIT_1);
3636 	SET_BITVAL(conn->tcp_timestamp_en, options, BIT_0);
3637 	fw_ddb_entry->tcp_options = cpu_to_le16(options);
3638 
3639 	options = le16_to_cpu(fw_ddb_entry->ip_options);
3640 	SET_BITVAL(conn->fragment_disable, options, BIT_4);
3641 	fw_ddb_entry->ip_options = cpu_to_le16(options);
3642 
3643 	fw_ddb_entry->iscsi_max_outsnd_r2t = cpu_to_le16(sess->max_r2t);
3644 	fw_ddb_entry->iscsi_max_rcv_data_seg_len =
3645 			       cpu_to_le16(conn->max_recv_dlength / BYTE_UNITS);
3646 	fw_ddb_entry->iscsi_max_snd_data_seg_len =
3647 			       cpu_to_le16(conn->max_xmit_dlength / BYTE_UNITS);
3648 	fw_ddb_entry->iscsi_first_burst_len =
3649 				cpu_to_le16(sess->first_burst / BYTE_UNITS);
3650 	fw_ddb_entry->iscsi_max_burst_len = cpu_to_le16(sess->max_burst /
3651 					    BYTE_UNITS);
3652 	fw_ddb_entry->iscsi_def_time2wait = cpu_to_le16(sess->time2wait);
3653 	fw_ddb_entry->iscsi_def_time2retain = cpu_to_le16(sess->time2retain);
3654 	fw_ddb_entry->tgt_portal_grp = cpu_to_le16(sess->tpgt);
3655 	fw_ddb_entry->mss = cpu_to_le16(conn->max_segment_size);
3656 	fw_ddb_entry->tcp_xmt_wsf = (uint8_t) cpu_to_le32(conn->tcp_xmit_wsf);
3657 	fw_ddb_entry->tcp_rcv_wsf = (uint8_t) cpu_to_le32(conn->tcp_recv_wsf);
3658 	fw_ddb_entry->ipv6_flow_lbl = cpu_to_le16(conn->ipv6_flow_label);
3659 	fw_ddb_entry->ka_timeout = cpu_to_le16(conn->keepalive_timeout);
3660 	fw_ddb_entry->lcl_port = cpu_to_le16(conn->local_port);
3661 	fw_ddb_entry->stat_sn = cpu_to_le32(conn->statsn);
3662 	fw_ddb_entry->exp_stat_sn = cpu_to_le32(conn->exp_statsn);
3663 	fw_ddb_entry->ddb_link = cpu_to_le16(sess->discovery_parent_idx);
3664 	fw_ddb_entry->chap_tbl_idx = cpu_to_le16(sess->chap_out_idx);
3665 	fw_ddb_entry->tsid = cpu_to_le16(sess->tsid);
3666 	fw_ddb_entry->port = cpu_to_le16(conn->port);
3667 	fw_ddb_entry->def_timeout =
3668 				cpu_to_le16(sess->default_taskmgmt_timeout);
3669 
3670 	if (!strncmp(sess->portal_type, PORTAL_TYPE_IPV6, 4))
3671 		fw_ddb_entry->ipv4_tos = conn->ipv6_traffic_class;
3672 	else
3673 		fw_ddb_entry->ipv4_tos = conn->ipv4_tos;
3674 
3675 	if (conn->ipaddress)
3676 		memcpy(fw_ddb_entry->ip_addr, conn->ipaddress,
3677 		       sizeof(fw_ddb_entry->ip_addr));
3678 
3679 	if (conn->redirect_ipaddr)
3680 		memcpy(fw_ddb_entry->tgt_addr, conn->redirect_ipaddr,
3681 		       sizeof(fw_ddb_entry->tgt_addr));
3682 
3683 	if (conn->link_local_ipv6_addr)
3684 		memcpy(fw_ddb_entry->link_local_ipv6_addr,
3685 		       conn->link_local_ipv6_addr,
3686 		       sizeof(fw_ddb_entry->link_local_ipv6_addr));
3687 
3688 	if (sess->targetname)
3689 		memcpy(fw_ddb_entry->iscsi_name, sess->targetname,
3690 		       sizeof(fw_ddb_entry->iscsi_name));
3691 
3692 	if (sess->targetalias)
3693 		memcpy(fw_ddb_entry->iscsi_alias, sess->targetalias,
3694 		       sizeof(fw_ddb_entry->iscsi_alias));
3695 
3696 	COPY_ISID(fw_ddb_entry->isid, sess->isid);
3697 
3698 	return rc;
3699 }
3700 
3701 static void qla4xxx_copy_to_sess_conn_params(struct iscsi_conn *conn,
3702 					     struct iscsi_session *sess,
3703 					     struct dev_db_entry *fw_ddb_entry)
3704 {
3705 	unsigned long options = 0;
3706 	uint16_t ddb_link;
3707 	uint16_t disc_parent;
3708 	char ip_addr[DDB_IPADDR_LEN];
3709 
3710 	options = le16_to_cpu(fw_ddb_entry->options);
3711 	conn->is_fw_assigned_ipv6 = test_bit(OPT_IS_FW_ASSIGNED_IPV6, &options);
3712 	sess->auto_snd_tgt_disable = test_bit(OPT_AUTO_SENDTGTS_DISABLE,
3713 					      &options);
3714 	sess->discovery_sess = test_bit(OPT_DISC_SESSION, &options);
3715 
3716 	options = le16_to_cpu(fw_ddb_entry->iscsi_options);
3717 	conn->hdrdgst_en = test_bit(ISCSIOPT_HEADER_DIGEST_EN, &options);
3718 	conn->datadgst_en = test_bit(ISCSIOPT_DATA_DIGEST_EN, &options);
3719 	sess->imm_data_en = test_bit(ISCSIOPT_IMMEDIATE_DATA_EN, &options);
3720 	sess->initial_r2t_en = test_bit(ISCSIOPT_INITIAL_R2T_EN, &options);
3721 	sess->dataseq_inorder_en = test_bit(ISCSIOPT_DATA_SEQ_IN_ORDER,
3722 					    &options);
3723 	sess->pdu_inorder_en = test_bit(ISCSIOPT_DATA_PDU_IN_ORDER, &options);
3724 	sess->chap_auth_en = test_bit(ISCSIOPT_CHAP_AUTH_EN, &options);
3725 	sess->discovery_logout_en = test_bit(ISCSIOPT_DISCOVERY_LOGOUT_EN,
3726 					     &options);
3727 	sess->bidi_chap_en = test_bit(ISCSIOPT_BIDI_CHAP_EN, &options);
3728 	sess->discovery_auth_optional =
3729 			test_bit(ISCSIOPT_DISCOVERY_AUTH_OPTIONAL, &options);
3730 	if (test_bit(ISCSIOPT_ERL1, &options))
3731 		sess->erl |= BIT_1;
3732 	if (test_bit(ISCSIOPT_ERL0, &options))
3733 		sess->erl |= BIT_0;
3734 
3735 	options = le16_to_cpu(fw_ddb_entry->tcp_options);
3736 	conn->tcp_timestamp_stat = test_bit(TCPOPT_TIMESTAMP_STAT, &options);
3737 	conn->tcp_nagle_disable = test_bit(TCPOPT_NAGLE_DISABLE, &options);
3738 	conn->tcp_wsf_disable = test_bit(TCPOPT_WSF_DISABLE, &options);
3739 	if (test_bit(TCPOPT_TIMER_SCALE3, &options))
3740 		conn->tcp_timer_scale |= BIT_3;
3741 	if (test_bit(TCPOPT_TIMER_SCALE2, &options))
3742 		conn->tcp_timer_scale |= BIT_2;
3743 	if (test_bit(TCPOPT_TIMER_SCALE1, &options))
3744 		conn->tcp_timer_scale |= BIT_1;
3745 
3746 	conn->tcp_timer_scale >>= 1;
3747 	conn->tcp_timestamp_en = test_bit(TCPOPT_TIMESTAMP_EN, &options);
3748 
3749 	options = le16_to_cpu(fw_ddb_entry->ip_options);
3750 	conn->fragment_disable = test_bit(IPOPT_FRAGMENT_DISABLE, &options);
3751 
3752 	conn->max_recv_dlength = BYTE_UNITS *
3753 			  le16_to_cpu(fw_ddb_entry->iscsi_max_rcv_data_seg_len);
3754 	conn->max_xmit_dlength = BYTE_UNITS *
3755 			  le16_to_cpu(fw_ddb_entry->iscsi_max_snd_data_seg_len);
3756 	sess->max_r2t = le16_to_cpu(fw_ddb_entry->iscsi_max_outsnd_r2t);
3757 	sess->first_burst = BYTE_UNITS *
3758 			       le16_to_cpu(fw_ddb_entry->iscsi_first_burst_len);
3759 	sess->max_burst = BYTE_UNITS *
3760 				 le16_to_cpu(fw_ddb_entry->iscsi_max_burst_len);
3761 	sess->time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
3762 	sess->time2retain = le16_to_cpu(fw_ddb_entry->iscsi_def_time2retain);
3763 	sess->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
3764 	conn->max_segment_size = le16_to_cpu(fw_ddb_entry->mss);
3765 	conn->tcp_xmit_wsf = fw_ddb_entry->tcp_xmt_wsf;
3766 	conn->tcp_recv_wsf = fw_ddb_entry->tcp_rcv_wsf;
3767 	conn->ipv4_tos = fw_ddb_entry->ipv4_tos;
3768 	conn->keepalive_tmo = le16_to_cpu(fw_ddb_entry->ka_timeout);
3769 	conn->local_port = le16_to_cpu(fw_ddb_entry->lcl_port);
3770 	conn->statsn = le32_to_cpu(fw_ddb_entry->stat_sn);
3771 	conn->exp_statsn = le32_to_cpu(fw_ddb_entry->exp_stat_sn);
3772 	sess->tsid = le16_to_cpu(fw_ddb_entry->tsid);
3773 	COPY_ISID(sess->isid, fw_ddb_entry->isid);
3774 
3775 	ddb_link = le16_to_cpu(fw_ddb_entry->ddb_link);
3776 	if (ddb_link == DDB_ISNS)
3777 		disc_parent = ISCSI_DISC_PARENT_ISNS;
3778 	else if (ddb_link == DDB_NO_LINK)
3779 		disc_parent = ISCSI_DISC_PARENT_UNKNOWN;
3780 	else if (ddb_link < MAX_DDB_ENTRIES)
3781 		disc_parent = ISCSI_DISC_PARENT_SENDTGT;
3782 	else
3783 		disc_parent = ISCSI_DISC_PARENT_UNKNOWN;
3784 
3785 	iscsi_set_param(conn->cls_conn, ISCSI_PARAM_DISCOVERY_PARENT_TYPE,
3786 			iscsi_get_discovery_parent_name(disc_parent), 0);
3787 
3788 	iscsi_set_param(conn->cls_conn, ISCSI_PARAM_TARGET_ALIAS,
3789 			(char *)fw_ddb_entry->iscsi_alias, 0);
3790 
3791 	options = le16_to_cpu(fw_ddb_entry->options);
3792 	if (options & DDB_OPT_IPV6_DEVICE) {
3793 		memset(ip_addr, 0, sizeof(ip_addr));
3794 		sprintf(ip_addr, "%pI6", fw_ddb_entry->link_local_ipv6_addr);
3795 		iscsi_set_param(conn->cls_conn, ISCSI_PARAM_LOCAL_IPADDR,
3796 				(char *)ip_addr, 0);
3797 	}
3798 }
3799 
3800 static void qla4xxx_copy_fwddb_param(struct scsi_qla_host *ha,
3801 				     struct dev_db_entry *fw_ddb_entry,
3802 				     struct iscsi_cls_session *cls_sess,
3803 				     struct iscsi_cls_conn *cls_conn)
3804 {
3805 	int buflen = 0;
3806 	struct iscsi_session *sess;
3807 	struct ddb_entry *ddb_entry;
3808 	struct ql4_chap_table chap_tbl;
3809 	struct iscsi_conn *conn;
3810 	char ip_addr[DDB_IPADDR_LEN];
3811 	uint16_t options = 0;
3812 
3813 	sess = cls_sess->dd_data;
3814 	ddb_entry = sess->dd_data;
3815 	conn = cls_conn->dd_data;
3816 	memset(&chap_tbl, 0, sizeof(chap_tbl));
3817 
3818 	ddb_entry->chap_tbl_idx = le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
3819 
3820 	qla4xxx_copy_to_sess_conn_params(conn, sess, fw_ddb_entry);
3821 
3822 	sess->def_taskmgmt_tmo = le16_to_cpu(fw_ddb_entry->def_timeout);
3823 	conn->persistent_port = le16_to_cpu(fw_ddb_entry->port);
3824 
3825 	memset(ip_addr, 0, sizeof(ip_addr));
3826 	options = le16_to_cpu(fw_ddb_entry->options);
3827 	if (options & DDB_OPT_IPV6_DEVICE) {
3828 		iscsi_set_param(cls_conn, ISCSI_PARAM_PORTAL_TYPE, "ipv6", 4);
3829 
3830 		memset(ip_addr, 0, sizeof(ip_addr));
3831 		sprintf(ip_addr, "%pI6", fw_ddb_entry->ip_addr);
3832 	} else {
3833 		iscsi_set_param(cls_conn, ISCSI_PARAM_PORTAL_TYPE, "ipv4", 4);
3834 		sprintf(ip_addr, "%pI4", fw_ddb_entry->ip_addr);
3835 	}
3836 
3837 	iscsi_set_param(cls_conn, ISCSI_PARAM_PERSISTENT_ADDRESS,
3838 			(char *)ip_addr, buflen);
3839 	iscsi_set_param(cls_conn, ISCSI_PARAM_TARGET_NAME,
3840 			(char *)fw_ddb_entry->iscsi_name, buflen);
3841 	iscsi_set_param(cls_conn, ISCSI_PARAM_INITIATOR_NAME,
3842 			(char *)ha->name_string, buflen);
3843 
3844 	if (ddb_entry->chap_tbl_idx != INVALID_ENTRY) {
3845 		if (!qla4xxx_get_uni_chap_at_index(ha, chap_tbl.name,
3846 						   chap_tbl.secret,
3847 						   ddb_entry->chap_tbl_idx)) {
3848 			iscsi_set_param(cls_conn, ISCSI_PARAM_USERNAME,
3849 					(char *)chap_tbl.name,
3850 					strlen((char *)chap_tbl.name));
3851 			iscsi_set_param(cls_conn, ISCSI_PARAM_PASSWORD,
3852 					(char *)chap_tbl.secret,
3853 					chap_tbl.secret_len);
3854 		}
3855 	}
3856 }
3857 
3858 void qla4xxx_update_session_conn_fwddb_param(struct scsi_qla_host *ha,
3859 					     struct ddb_entry *ddb_entry)
3860 {
3861 	struct iscsi_cls_session *cls_sess;
3862 	struct iscsi_cls_conn *cls_conn;
3863 	uint32_t ddb_state;
3864 	dma_addr_t fw_ddb_entry_dma;
3865 	struct dev_db_entry *fw_ddb_entry;
3866 
3867 	fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3868 					  &fw_ddb_entry_dma, GFP_KERNEL);
3869 	if (!fw_ddb_entry) {
3870 		ql4_printk(KERN_ERR, ha,
3871 			   "%s: Unable to allocate dma buffer\n", __func__);
3872 		goto exit_session_conn_fwddb_param;
3873 	}
3874 
3875 	if (qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index, fw_ddb_entry,
3876 				    fw_ddb_entry_dma, NULL, NULL, &ddb_state,
3877 				    NULL, NULL, NULL) == QLA_ERROR) {
3878 		DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: failed "
3879 				  "get_ddb_entry for fw_ddb_index %d\n",
3880 				  ha->host_no, __func__,
3881 				  ddb_entry->fw_ddb_index));
3882 		goto exit_session_conn_fwddb_param;
3883 	}
3884 
3885 	cls_sess = ddb_entry->sess;
3886 
3887 	cls_conn = ddb_entry->conn;
3888 
3889 	/* Update params */
3890 	qla4xxx_copy_fwddb_param(ha, fw_ddb_entry, cls_sess, cls_conn);
3891 
3892 exit_session_conn_fwddb_param:
3893 	if (fw_ddb_entry)
3894 		dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3895 				  fw_ddb_entry, fw_ddb_entry_dma);
3896 }
3897 
3898 void qla4xxx_update_session_conn_param(struct scsi_qla_host *ha,
3899 				       struct ddb_entry *ddb_entry)
3900 {
3901 	struct iscsi_cls_session *cls_sess;
3902 	struct iscsi_cls_conn *cls_conn;
3903 	struct iscsi_session *sess;
3904 	struct iscsi_conn *conn;
3905 	uint32_t ddb_state;
3906 	dma_addr_t fw_ddb_entry_dma;
3907 	struct dev_db_entry *fw_ddb_entry;
3908 
3909 	fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3910 					  &fw_ddb_entry_dma, GFP_KERNEL);
3911 	if (!fw_ddb_entry) {
3912 		ql4_printk(KERN_ERR, ha,
3913 			   "%s: Unable to allocate dma buffer\n", __func__);
3914 		goto exit_session_conn_param;
3915 	}
3916 
3917 	if (qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index, fw_ddb_entry,
3918 				    fw_ddb_entry_dma, NULL, NULL, &ddb_state,
3919 				    NULL, NULL, NULL) == QLA_ERROR) {
3920 		DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: failed "
3921 				  "get_ddb_entry for fw_ddb_index %d\n",
3922 				  ha->host_no, __func__,
3923 				  ddb_entry->fw_ddb_index));
3924 		goto exit_session_conn_param;
3925 	}
3926 
3927 	cls_sess = ddb_entry->sess;
3928 	sess = cls_sess->dd_data;
3929 
3930 	cls_conn = ddb_entry->conn;
3931 	conn = cls_conn->dd_data;
3932 
3933 	/* Update timers after login */
3934 	ddb_entry->default_relogin_timeout =
3935 		(le16_to_cpu(fw_ddb_entry->def_timeout) > LOGIN_TOV) &&
3936 		 (le16_to_cpu(fw_ddb_entry->def_timeout) < LOGIN_TOV * 10) ?
3937 		 le16_to_cpu(fw_ddb_entry->def_timeout) : LOGIN_TOV;
3938 	ddb_entry->default_time2wait =
3939 				le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
3940 
3941 	/* Update params */
3942 	ddb_entry->chap_tbl_idx = le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
3943 	qla4xxx_copy_to_sess_conn_params(conn, sess, fw_ddb_entry);
3944 
3945 	memcpy(sess->initiatorname, ha->name_string,
3946 	       min(sizeof(ha->name_string), sizeof(sess->initiatorname)));
3947 
3948 exit_session_conn_param:
3949 	if (fw_ddb_entry)
3950 		dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3951 				  fw_ddb_entry, fw_ddb_entry_dma);
3952 }
3953 
3954 /*
3955  * Timer routines
3956  */
3957 static void qla4xxx_timer(struct timer_list *t);
3958 
3959 static void qla4xxx_start_timer(struct scsi_qla_host *ha,
3960 				unsigned long interval)
3961 {
3962 	DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
3963 		     __func__, ha->host->host_no));
3964 	timer_setup(&ha->timer, qla4xxx_timer, 0);
3965 	ha->timer.expires = jiffies + interval * HZ;
3966 	add_timer(&ha->timer);
3967 	ha->timer_active = 1;
3968 }
3969 
3970 static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
3971 {
3972 	del_timer_sync(&ha->timer);
3973 	ha->timer_active = 0;
3974 }
3975 
3976 /***
3977  * qla4xxx_mark_device_missing - blocks the session
3978  * @cls_session: Pointer to the session to be blocked
3979  * @ddb_entry: Pointer to device database entry
3980  *
3981  * This routine marks a device missing and close connection.
3982  **/
3983 void qla4xxx_mark_device_missing(struct iscsi_cls_session *cls_session)
3984 {
3985 	iscsi_block_session(cls_session);
3986 }
3987 
3988 /**
3989  * qla4xxx_mark_all_devices_missing - mark all devices as missing.
3990  * @ha: Pointer to host adapter structure.
3991  *
3992  * This routine marks a device missing and resets the relogin retry count.
3993  **/
3994 void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
3995 {
3996 	iscsi_host_for_each_session(ha->host, qla4xxx_mark_device_missing);
3997 }
3998 
3999 static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
4000 				       struct ddb_entry *ddb_entry,
4001 				       struct scsi_cmnd *cmd)
4002 {
4003 	struct srb *srb;
4004 
4005 	srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
4006 	if (!srb)
4007 		return srb;
4008 
4009 	kref_init(&srb->srb_ref);
4010 	srb->ha = ha;
4011 	srb->ddb = ddb_entry;
4012 	srb->cmd = cmd;
4013 	srb->flags = 0;
4014 	CMD_SP(cmd) = (void *)srb;
4015 
4016 	return srb;
4017 }
4018 
4019 static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
4020 {
4021 	struct scsi_cmnd *cmd = srb->cmd;
4022 
4023 	if (srb->flags & SRB_DMA_VALID) {
4024 		scsi_dma_unmap(cmd);
4025 		srb->flags &= ~SRB_DMA_VALID;
4026 	}
4027 	CMD_SP(cmd) = NULL;
4028 }
4029 
4030 void qla4xxx_srb_compl(struct kref *ref)
4031 {
4032 	struct srb *srb = container_of(ref, struct srb, srb_ref);
4033 	struct scsi_cmnd *cmd = srb->cmd;
4034 	struct scsi_qla_host *ha = srb->ha;
4035 
4036 	qla4xxx_srb_free_dma(ha, srb);
4037 
4038 	mempool_free(srb, ha->srb_mempool);
4039 
4040 	cmd->scsi_done(cmd);
4041 }
4042 
4043 /**
4044  * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
4045  * @host: scsi host
4046  * @cmd: Pointer to Linux's SCSI command structure
4047  *
4048  * Remarks:
4049  * This routine is invoked by Linux to send a SCSI command to the driver.
4050  * The mid-level driver tries to ensure that queuecommand never gets
4051  * invoked concurrently with itself or the interrupt handler (although
4052  * the interrupt handler may call this routine as part of request-
4053  * completion handling).   Unfortunely, it sometimes calls the scheduler
4054  * in interrupt context which is a big NO! NO!.
4055  **/
4056 static int qla4xxx_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
4057 {
4058 	struct scsi_qla_host *ha = to_qla_host(host);
4059 	struct ddb_entry *ddb_entry = cmd->device->hostdata;
4060 	struct iscsi_cls_session *sess = ddb_entry->sess;
4061 	struct srb *srb;
4062 	int rval;
4063 
4064 	if (test_bit(AF_EEH_BUSY, &ha->flags)) {
4065 		if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
4066 			cmd->result = DID_NO_CONNECT << 16;
4067 		else
4068 			cmd->result = DID_REQUEUE << 16;
4069 		goto qc_fail_command;
4070 	}
4071 
4072 	if (!sess) {
4073 		cmd->result = DID_IMM_RETRY << 16;
4074 		goto qc_fail_command;
4075 	}
4076 
4077 	rval = iscsi_session_chkready(sess);
4078 	if (rval) {
4079 		cmd->result = rval;
4080 		goto qc_fail_command;
4081 	}
4082 
4083 	if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
4084 	    test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
4085 	    test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
4086 	    test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
4087 	    test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
4088 	    !test_bit(AF_ONLINE, &ha->flags) ||
4089 	    !test_bit(AF_LINK_UP, &ha->flags) ||
4090 	    test_bit(AF_LOOPBACK, &ha->flags) ||
4091 	    test_bit(DPC_POST_IDC_ACK, &ha->dpc_flags) ||
4092 	    test_bit(DPC_RESTORE_ACB, &ha->dpc_flags) ||
4093 	    test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
4094 		goto qc_host_busy;
4095 
4096 	srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd);
4097 	if (!srb)
4098 		goto qc_host_busy;
4099 
4100 	rval = qla4xxx_send_command_to_isp(ha, srb);
4101 	if (rval != QLA_SUCCESS)
4102 		goto qc_host_busy_free_sp;
4103 
4104 	return 0;
4105 
4106 qc_host_busy_free_sp:
4107 	qla4xxx_srb_free_dma(ha, srb);
4108 	mempool_free(srb, ha->srb_mempool);
4109 
4110 qc_host_busy:
4111 	return SCSI_MLQUEUE_HOST_BUSY;
4112 
4113 qc_fail_command:
4114 	cmd->scsi_done(cmd);
4115 
4116 	return 0;
4117 }
4118 
4119 /**
4120  * qla4xxx_mem_free - frees memory allocated to adapter
4121  * @ha: Pointer to host adapter structure.
4122  *
4123  * Frees memory previously allocated by qla4xxx_mem_alloc
4124  **/
4125 static void qla4xxx_mem_free(struct scsi_qla_host *ha)
4126 {
4127 	if (ha->queues)
4128 		dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
4129 				  ha->queues_dma);
4130 
4131 	 if (ha->fw_dump)
4132 		vfree(ha->fw_dump);
4133 
4134 	ha->queues_len = 0;
4135 	ha->queues = NULL;
4136 	ha->queues_dma = 0;
4137 	ha->request_ring = NULL;
4138 	ha->request_dma = 0;
4139 	ha->response_ring = NULL;
4140 	ha->response_dma = 0;
4141 	ha->shadow_regs = NULL;
4142 	ha->shadow_regs_dma = 0;
4143 	ha->fw_dump = NULL;
4144 	ha->fw_dump_size = 0;
4145 
4146 	/* Free srb pool. */
4147 	if (ha->srb_mempool)
4148 		mempool_destroy(ha->srb_mempool);
4149 
4150 	ha->srb_mempool = NULL;
4151 
4152 	if (ha->chap_dma_pool)
4153 		dma_pool_destroy(ha->chap_dma_pool);
4154 
4155 	if (ha->chap_list)
4156 		vfree(ha->chap_list);
4157 	ha->chap_list = NULL;
4158 
4159 	if (ha->fw_ddb_dma_pool)
4160 		dma_pool_destroy(ha->fw_ddb_dma_pool);
4161 
4162 	/* release io space registers  */
4163 	if (is_qla8022(ha)) {
4164 		if (ha->nx_pcibase)
4165 			iounmap(
4166 			    (struct device_reg_82xx __iomem *)ha->nx_pcibase);
4167 	} else if (is_qla8032(ha) || is_qla8042(ha)) {
4168 		if (ha->nx_pcibase)
4169 			iounmap(
4170 			    (struct device_reg_83xx __iomem *)ha->nx_pcibase);
4171 	} else if (ha->reg) {
4172 		iounmap(ha->reg);
4173 	}
4174 
4175 	if (ha->reset_tmplt.buff)
4176 		vfree(ha->reset_tmplt.buff);
4177 
4178 	pci_release_regions(ha->pdev);
4179 }
4180 
4181 /**
4182  * qla4xxx_mem_alloc - allocates memory for use by adapter.
4183  * @ha: Pointer to host adapter structure
4184  *
4185  * Allocates DMA memory for request and response queues. Also allocates memory
4186  * for srbs.
4187  **/
4188 static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
4189 {
4190 	unsigned long align;
4191 
4192 	/* Allocate contiguous block of DMA memory for queues. */
4193 	ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
4194 			  (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
4195 			  sizeof(struct shadow_regs) +
4196 			  MEM_ALIGN_VALUE +
4197 			  (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
4198 	ha->queues = dma_zalloc_coherent(&ha->pdev->dev, ha->queues_len,
4199 					 &ha->queues_dma, GFP_KERNEL);
4200 	if (ha->queues == NULL) {
4201 		ql4_printk(KERN_WARNING, ha,
4202 		    "Memory Allocation failed - queues.\n");
4203 
4204 		goto mem_alloc_error_exit;
4205 	}
4206 
4207 	/*
4208 	 * As per RISC alignment requirements -- the bus-address must be a
4209 	 * multiple of the request-ring size (in bytes).
4210 	 */
4211 	align = 0;
4212 	if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
4213 		align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
4214 					   (MEM_ALIGN_VALUE - 1));
4215 
4216 	/* Update request and response queue pointers. */
4217 	ha->request_dma = ha->queues_dma + align;
4218 	ha->request_ring = (struct queue_entry *) (ha->queues + align);
4219 	ha->response_dma = ha->queues_dma + align +
4220 		(REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
4221 	ha->response_ring = (struct queue_entry *) (ha->queues + align +
4222 						    (REQUEST_QUEUE_DEPTH *
4223 						     QUEUE_SIZE));
4224 	ha->shadow_regs_dma = ha->queues_dma + align +
4225 		(REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
4226 		(RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
4227 	ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
4228 						  (REQUEST_QUEUE_DEPTH *
4229 						   QUEUE_SIZE) +
4230 						  (RESPONSE_QUEUE_DEPTH *
4231 						   QUEUE_SIZE));
4232 
4233 	/* Allocate memory for srb pool. */
4234 	ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
4235 					 mempool_free_slab, srb_cachep);
4236 	if (ha->srb_mempool == NULL) {
4237 		ql4_printk(KERN_WARNING, ha,
4238 		    "Memory Allocation failed - SRB Pool.\n");
4239 
4240 		goto mem_alloc_error_exit;
4241 	}
4242 
4243 	ha->chap_dma_pool = dma_pool_create("ql4_chap", &ha->pdev->dev,
4244 					    CHAP_DMA_BLOCK_SIZE, 8, 0);
4245 
4246 	if (ha->chap_dma_pool == NULL) {
4247 		ql4_printk(KERN_WARNING, ha,
4248 		    "%s: chap_dma_pool allocation failed..\n", __func__);
4249 		goto mem_alloc_error_exit;
4250 	}
4251 
4252 	ha->fw_ddb_dma_pool = dma_pool_create("ql4_fw_ddb", &ha->pdev->dev,
4253 					      DDB_DMA_BLOCK_SIZE, 8, 0);
4254 
4255 	if (ha->fw_ddb_dma_pool == NULL) {
4256 		ql4_printk(KERN_WARNING, ha,
4257 			   "%s: fw_ddb_dma_pool allocation failed..\n",
4258 			   __func__);
4259 		goto mem_alloc_error_exit;
4260 	}
4261 
4262 	return QLA_SUCCESS;
4263 
4264 mem_alloc_error_exit:
4265 	qla4xxx_mem_free(ha);
4266 	return QLA_ERROR;
4267 }
4268 
4269 /**
4270  * qla4_8xxx_check_temp - Check the ISP82XX temperature.
4271  * @ha: adapter block pointer.
4272  *
4273  * Note: The caller should not hold the idc lock.
4274  **/
4275 static int qla4_8xxx_check_temp(struct scsi_qla_host *ha)
4276 {
4277 	uint32_t temp, temp_state, temp_val;
4278 	int status = QLA_SUCCESS;
4279 
4280 	temp = qla4_8xxx_rd_direct(ha, QLA8XXX_CRB_TEMP_STATE);
4281 
4282 	temp_state = qla82xx_get_temp_state(temp);
4283 	temp_val = qla82xx_get_temp_val(temp);
4284 
4285 	if (temp_state == QLA82XX_TEMP_PANIC) {
4286 		ql4_printk(KERN_WARNING, ha, "Device temperature %d degrees C"
4287 			   " exceeds maximum allowed. Hardware has been shut"
4288 			   " down.\n", temp_val);
4289 		status = QLA_ERROR;
4290 	} else if (temp_state == QLA82XX_TEMP_WARN) {
4291 		if (ha->temperature == QLA82XX_TEMP_NORMAL)
4292 			ql4_printk(KERN_WARNING, ha, "Device temperature %d"
4293 				   " degrees C exceeds operating range."
4294 				   " Immediate action needed.\n", temp_val);
4295 	} else {
4296 		if (ha->temperature == QLA82XX_TEMP_WARN)
4297 			ql4_printk(KERN_INFO, ha, "Device temperature is"
4298 				   " now %d degrees C in normal range.\n",
4299 				   temp_val);
4300 	}
4301 	ha->temperature = temp_state;
4302 	return status;
4303 }
4304 
4305 /**
4306  * qla4_8xxx_check_fw_alive  - Check firmware health
4307  * @ha: Pointer to host adapter structure.
4308  *
4309  * Context: Interrupt
4310  **/
4311 static int qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
4312 {
4313 	uint32_t fw_heartbeat_counter;
4314 	int status = QLA_SUCCESS;
4315 
4316 	fw_heartbeat_counter = qla4_8xxx_rd_direct(ha,
4317 						   QLA8XXX_PEG_ALIVE_COUNTER);
4318 	/* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
4319 	if (fw_heartbeat_counter == 0xffffffff) {
4320 		DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
4321 		    "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
4322 		    ha->host_no, __func__));
4323 		return status;
4324 	}
4325 
4326 	if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
4327 		ha->seconds_since_last_heartbeat++;
4328 		/* FW not alive after 2 seconds */
4329 		if (ha->seconds_since_last_heartbeat == 2) {
4330 			ha->seconds_since_last_heartbeat = 0;
4331 			qla4_8xxx_dump_peg_reg(ha);
4332 			status = QLA_ERROR;
4333 		}
4334 	} else
4335 		ha->seconds_since_last_heartbeat = 0;
4336 
4337 	ha->fw_heartbeat_counter = fw_heartbeat_counter;
4338 	return status;
4339 }
4340 
4341 static void qla4_8xxx_process_fw_error(struct scsi_qla_host *ha)
4342 {
4343 	uint32_t halt_status;
4344 	int halt_status_unrecoverable = 0;
4345 
4346 	halt_status = qla4_8xxx_rd_direct(ha, QLA8XXX_PEG_HALT_STATUS1);
4347 
4348 	if (is_qla8022(ha)) {
4349 		ql4_printk(KERN_INFO, ha, "%s: disabling pause transmit on port 0 & 1.\n",
4350 			   __func__);
4351 		qla4_82xx_wr_32(ha, QLA82XX_CRB_NIU + 0x98,
4352 				CRB_NIU_XG_PAUSE_CTL_P0 |
4353 				CRB_NIU_XG_PAUSE_CTL_P1);
4354 
4355 		if (QLA82XX_FWERROR_CODE(halt_status) == 0x67)
4356 			ql4_printk(KERN_ERR, ha, "%s: Firmware aborted with error code 0x00006700. Device is being reset\n",
4357 				   __func__);
4358 		if (halt_status & HALT_STATUS_UNRECOVERABLE)
4359 			halt_status_unrecoverable = 1;
4360 	} else if (is_qla8032(ha) || is_qla8042(ha)) {
4361 		if (halt_status & QLA83XX_HALT_STATUS_FW_RESET)
4362 			ql4_printk(KERN_ERR, ha, "%s: Firmware error detected device is being reset\n",
4363 				   __func__);
4364 		else if (halt_status & QLA83XX_HALT_STATUS_UNRECOVERABLE)
4365 			halt_status_unrecoverable = 1;
4366 	}
4367 
4368 	/*
4369 	 * Since we cannot change dev_state in interrupt context,
4370 	 * set appropriate DPC flag then wakeup DPC
4371 	 */
4372 	if (halt_status_unrecoverable) {
4373 		set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
4374 	} else {
4375 		ql4_printk(KERN_INFO, ha, "%s: detect abort needed!\n",
4376 			   __func__);
4377 		set_bit(DPC_RESET_HA, &ha->dpc_flags);
4378 	}
4379 	qla4xxx_mailbox_premature_completion(ha);
4380 	qla4xxx_wake_dpc(ha);
4381 }
4382 
4383 /**
4384  * qla4_8xxx_watchdog - Poll dev state
4385  * @ha: Pointer to host adapter structure.
4386  *
4387  * Context: Interrupt
4388  **/
4389 void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
4390 {
4391 	uint32_t dev_state;
4392 	uint32_t idc_ctrl;
4393 
4394 	if (is_qla8032(ha) &&
4395 	    (qla4_83xx_is_detached(ha) == QLA_SUCCESS))
4396 		WARN_ONCE(1, "%s: iSCSI function %d marked invisible\n",
4397 			  __func__, ha->func_num);
4398 
4399 	/* don't poll if reset is going on */
4400 	if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
4401 	    test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
4402 	    test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags))) {
4403 		dev_state = qla4_8xxx_rd_direct(ha, QLA8XXX_CRB_DEV_STATE);
4404 
4405 		if (qla4_8xxx_check_temp(ha)) {
4406 			if (is_qla8022(ha)) {
4407 				ql4_printk(KERN_INFO, ha, "disabling pause transmit on port 0 & 1.\n");
4408 				qla4_82xx_wr_32(ha, QLA82XX_CRB_NIU + 0x98,
4409 						CRB_NIU_XG_PAUSE_CTL_P0 |
4410 						CRB_NIU_XG_PAUSE_CTL_P1);
4411 			}
4412 			set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
4413 			qla4xxx_wake_dpc(ha);
4414 		} else if (dev_state == QLA8XXX_DEV_NEED_RESET &&
4415 			   !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
4416 
4417 			ql4_printk(KERN_INFO, ha, "%s: HW State: NEED RESET!\n",
4418 				   __func__);
4419 
4420 			if (is_qla8032(ha) || is_qla8042(ha)) {
4421 				idc_ctrl = qla4_83xx_rd_reg(ha,
4422 							QLA83XX_IDC_DRV_CTRL);
4423 				if (!(idc_ctrl & GRACEFUL_RESET_BIT1)) {
4424 					ql4_printk(KERN_INFO, ha, "%s: Graceful reset bit is not set\n",
4425 						   __func__);
4426 					qla4xxx_mailbox_premature_completion(
4427 									    ha);
4428 				}
4429 			}
4430 
4431 			if ((is_qla8032(ha) || is_qla8042(ha)) ||
4432 			    (is_qla8022(ha) && !ql4xdontresethba)) {
4433 				set_bit(DPC_RESET_HA, &ha->dpc_flags);
4434 				qla4xxx_wake_dpc(ha);
4435 			}
4436 		} else if (dev_state == QLA8XXX_DEV_NEED_QUIESCENT &&
4437 		    !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
4438 			ql4_printk(KERN_INFO, ha, "%s: HW State: NEED QUIES!\n",
4439 			    __func__);
4440 			set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
4441 			qla4xxx_wake_dpc(ha);
4442 		} else  {
4443 			/* Check firmware health */
4444 			if (qla4_8xxx_check_fw_alive(ha))
4445 				qla4_8xxx_process_fw_error(ha);
4446 		}
4447 	}
4448 }
4449 
4450 static void qla4xxx_check_relogin_flash_ddb(struct iscsi_cls_session *cls_sess)
4451 {
4452 	struct iscsi_session *sess;
4453 	struct ddb_entry *ddb_entry;
4454 	struct scsi_qla_host *ha;
4455 
4456 	sess = cls_sess->dd_data;
4457 	ddb_entry = sess->dd_data;
4458 	ha = ddb_entry->ha;
4459 
4460 	if (!(ddb_entry->ddb_type == FLASH_DDB))
4461 		return;
4462 
4463 	if (adapter_up(ha) && !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
4464 	    !iscsi_is_session_online(cls_sess)) {
4465 		if (atomic_read(&ddb_entry->retry_relogin_timer) !=
4466 		    INVALID_ENTRY) {
4467 			if (atomic_read(&ddb_entry->retry_relogin_timer) ==
4468 					0) {
4469 				atomic_set(&ddb_entry->retry_relogin_timer,
4470 					   INVALID_ENTRY);
4471 				set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
4472 				set_bit(DF_RELOGIN, &ddb_entry->flags);
4473 				DEBUG2(ql4_printk(KERN_INFO, ha,
4474 				       "%s: index [%d] login device\n",
4475 					__func__, ddb_entry->fw_ddb_index));
4476 			} else
4477 				atomic_dec(&ddb_entry->retry_relogin_timer);
4478 		}
4479 	}
4480 
4481 	/* Wait for relogin to timeout */
4482 	if (atomic_read(&ddb_entry->relogin_timer) &&
4483 	    (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
4484 		/*
4485 		 * If the relogin times out and the device is
4486 		 * still NOT ONLINE then try and relogin again.
4487 		 */
4488 		if (!iscsi_is_session_online(cls_sess)) {
4489 			/* Reset retry relogin timer */
4490 			atomic_inc(&ddb_entry->relogin_retry_count);
4491 			DEBUG2(ql4_printk(KERN_INFO, ha,
4492 				"%s: index[%d] relogin timed out-retrying"
4493 				" relogin (%d), retry (%d)\n", __func__,
4494 				ddb_entry->fw_ddb_index,
4495 				atomic_read(&ddb_entry->relogin_retry_count),
4496 				ddb_entry->default_time2wait + 4));
4497 			set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
4498 			atomic_set(&ddb_entry->retry_relogin_timer,
4499 				   ddb_entry->default_time2wait + 4);
4500 		}
4501 	}
4502 }
4503 
4504 /**
4505  * qla4xxx_timer - checks every second for work to do.
4506  * @ha: Pointer to host adapter structure.
4507  **/
4508 static void qla4xxx_timer(struct timer_list *t)
4509 {
4510 	struct scsi_qla_host *ha = from_timer(ha, t, timer);
4511 	int start_dpc = 0;
4512 	uint16_t w;
4513 
4514 	iscsi_host_for_each_session(ha->host, qla4xxx_check_relogin_flash_ddb);
4515 
4516 	/* If we are in the middle of AER/EEH processing
4517 	 * skip any processing and reschedule the timer
4518 	 */
4519 	if (test_bit(AF_EEH_BUSY, &ha->flags)) {
4520 		mod_timer(&ha->timer, jiffies + HZ);
4521 		return;
4522 	}
4523 
4524 	/* Hardware read to trigger an EEH error during mailbox waits. */
4525 	if (!pci_channel_offline(ha->pdev))
4526 		pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
4527 
4528 	if (is_qla80XX(ha))
4529 		qla4_8xxx_watchdog(ha);
4530 
4531 	if (is_qla40XX(ha)) {
4532 		/* Check for heartbeat interval. */
4533 		if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
4534 		    ha->heartbeat_interval != 0) {
4535 			ha->seconds_since_last_heartbeat++;
4536 			if (ha->seconds_since_last_heartbeat >
4537 			    ha->heartbeat_interval + 2)
4538 				set_bit(DPC_RESET_HA, &ha->dpc_flags);
4539 		}
4540 	}
4541 
4542 	/* Process any deferred work. */
4543 	if (!list_empty(&ha->work_list))
4544 		start_dpc++;
4545 
4546 	/* Wakeup the dpc routine for this adapter, if needed. */
4547 	if (start_dpc ||
4548 	     test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
4549 	     test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
4550 	     test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
4551 	     test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
4552 	     test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
4553 	     test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
4554 	     test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
4555 	     test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
4556 	     test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
4557 	     test_bit(DPC_SYSFS_DDB_EXPORT, &ha->dpc_flags) ||
4558 	     test_bit(DPC_AEN, &ha->dpc_flags)) {
4559 		DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
4560 			      " - dpc flags = 0x%lx\n",
4561 			      ha->host_no, __func__, ha->dpc_flags));
4562 		qla4xxx_wake_dpc(ha);
4563 	}
4564 
4565 	/* Reschedule timer thread to call us back in one second */
4566 	mod_timer(&ha->timer, jiffies + HZ);
4567 
4568 	DEBUG2(ha->seconds_since_last_intr++);
4569 }
4570 
4571 /**
4572  * qla4xxx_cmd_wait - waits for all outstanding commands to complete
4573  * @ha: Pointer to host adapter structure.
4574  *
4575  * This routine stalls the driver until all outstanding commands are returned.
4576  * Caller must release the Hardware Lock prior to calling this routine.
4577  **/
4578 static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
4579 {
4580 	uint32_t index = 0;
4581 	unsigned long flags;
4582 	struct scsi_cmnd *cmd;
4583 	unsigned long wtime;
4584 	uint32_t wtmo;
4585 
4586 	if (is_qla40XX(ha))
4587 		wtmo = WAIT_CMD_TOV;
4588 	else
4589 		wtmo = ha->nx_reset_timeout / 2;
4590 
4591 	wtime = jiffies + (wtmo * HZ);
4592 
4593 	DEBUG2(ql4_printk(KERN_INFO, ha,
4594 			  "Wait up to %u seconds for cmds to complete\n",
4595 			  wtmo));
4596 
4597 	while (!time_after_eq(jiffies, wtime)) {
4598 		spin_lock_irqsave(&ha->hardware_lock, flags);
4599 		/* Find a command that hasn't completed. */
4600 		for (index = 0; index < ha->host->can_queue; index++) {
4601 			cmd = scsi_host_find_tag(ha->host, index);
4602 			/*
4603 			 * We cannot just check if the index is valid,
4604 			 * becase if we are run from the scsi eh, then
4605 			 * the scsi/block layer is going to prevent
4606 			 * the tag from being released.
4607 			 */
4608 			if (cmd != NULL && CMD_SP(cmd))
4609 				break;
4610 		}
4611 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
4612 
4613 		/* If No Commands are pending, wait is complete */
4614 		if (index == ha->host->can_queue)
4615 			return QLA_SUCCESS;
4616 
4617 		msleep(1000);
4618 	}
4619 	/* If we timed out on waiting for commands to come back
4620 	 * return ERROR. */
4621 	return QLA_ERROR;
4622 }
4623 
4624 int qla4xxx_hw_reset(struct scsi_qla_host *ha)
4625 {
4626 	uint32_t ctrl_status;
4627 	unsigned long flags = 0;
4628 
4629 	DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
4630 
4631 	if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
4632 		return QLA_ERROR;
4633 
4634 	spin_lock_irqsave(&ha->hardware_lock, flags);
4635 
4636 	/*
4637 	 * If the SCSI Reset Interrupt bit is set, clear it.
4638 	 * Otherwise, the Soft Reset won't work.
4639 	 */
4640 	ctrl_status = readw(&ha->reg->ctrl_status);
4641 	if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
4642 		writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
4643 
4644 	/* Issue Soft Reset */
4645 	writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
4646 	readl(&ha->reg->ctrl_status);
4647 
4648 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
4649 	return QLA_SUCCESS;
4650 }
4651 
4652 /**
4653  * qla4xxx_soft_reset - performs soft reset.
4654  * @ha: Pointer to host adapter structure.
4655  **/
4656 int qla4xxx_soft_reset(struct scsi_qla_host *ha)
4657 {
4658 	uint32_t max_wait_time;
4659 	unsigned long flags = 0;
4660 	int status;
4661 	uint32_t ctrl_status;
4662 
4663 	status = qla4xxx_hw_reset(ha);
4664 	if (status != QLA_SUCCESS)
4665 		return status;
4666 
4667 	status = QLA_ERROR;
4668 	/* Wait until the Network Reset Intr bit is cleared */
4669 	max_wait_time = RESET_INTR_TOV;
4670 	do {
4671 		spin_lock_irqsave(&ha->hardware_lock, flags);
4672 		ctrl_status = readw(&ha->reg->ctrl_status);
4673 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
4674 
4675 		if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
4676 			break;
4677 
4678 		msleep(1000);
4679 	} while ((--max_wait_time));
4680 
4681 	if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
4682 		DEBUG2(printk(KERN_WARNING
4683 			      "scsi%ld: Network Reset Intr not cleared by "
4684 			      "Network function, clearing it now!\n",
4685 			      ha->host_no));
4686 		spin_lock_irqsave(&ha->hardware_lock, flags);
4687 		writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
4688 		readl(&ha->reg->ctrl_status);
4689 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
4690 	}
4691 
4692 	/* Wait until the firmware tells us the Soft Reset is done */
4693 	max_wait_time = SOFT_RESET_TOV;
4694 	do {
4695 		spin_lock_irqsave(&ha->hardware_lock, flags);
4696 		ctrl_status = readw(&ha->reg->ctrl_status);
4697 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
4698 
4699 		if ((ctrl_status & CSR_SOFT_RESET) == 0) {
4700 			status = QLA_SUCCESS;
4701 			break;
4702 		}
4703 
4704 		msleep(1000);
4705 	} while ((--max_wait_time));
4706 
4707 	/*
4708 	 * Also, make sure that the SCSI Reset Interrupt bit has been cleared
4709 	 * after the soft reset has taken place.
4710 	 */
4711 	spin_lock_irqsave(&ha->hardware_lock, flags);
4712 	ctrl_status = readw(&ha->reg->ctrl_status);
4713 	if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
4714 		writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
4715 		readl(&ha->reg->ctrl_status);
4716 	}
4717 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
4718 
4719 	/* If soft reset fails then most probably the bios on other
4720 	 * function is also enabled.
4721 	 * Since the initialization is sequential the other fn
4722 	 * wont be able to acknowledge the soft reset.
4723 	 * Issue a force soft reset to workaround this scenario.
4724 	 */
4725 	if (max_wait_time == 0) {
4726 		/* Issue Force Soft Reset */
4727 		spin_lock_irqsave(&ha->hardware_lock, flags);
4728 		writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
4729 		readl(&ha->reg->ctrl_status);
4730 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
4731 		/* Wait until the firmware tells us the Soft Reset is done */
4732 		max_wait_time = SOFT_RESET_TOV;
4733 		do {
4734 			spin_lock_irqsave(&ha->hardware_lock, flags);
4735 			ctrl_status = readw(&ha->reg->ctrl_status);
4736 			spin_unlock_irqrestore(&ha->hardware_lock, flags);
4737 
4738 			if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
4739 				status = QLA_SUCCESS;
4740 				break;
4741 			}
4742 
4743 			msleep(1000);
4744 		} while ((--max_wait_time));
4745 	}
4746 
4747 	return status;
4748 }
4749 
4750 /**
4751  * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
4752  * @ha: Pointer to host adapter structure.
4753  * @res: returned scsi status
4754  *
4755  * This routine is called just prior to a HARD RESET to return all
4756  * outstanding commands back to the Operating System.
4757  * Caller should make sure that the following locks are released
4758  * before this calling routine: Hardware lock, and io_request_lock.
4759  **/
4760 static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
4761 {
4762 	struct srb *srb;
4763 	int i;
4764 	unsigned long flags;
4765 
4766 	spin_lock_irqsave(&ha->hardware_lock, flags);
4767 	for (i = 0; i < ha->host->can_queue; i++) {
4768 		srb = qla4xxx_del_from_active_array(ha, i);
4769 		if (srb != NULL) {
4770 			srb->cmd->result = res;
4771 			kref_put(&srb->srb_ref, qla4xxx_srb_compl);
4772 		}
4773 	}
4774 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
4775 }
4776 
4777 void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
4778 {
4779 	clear_bit(AF_ONLINE, &ha->flags);
4780 
4781 	/* Disable the board */
4782 	ql4_printk(KERN_INFO, ha, "Disabling the board\n");
4783 
4784 	qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
4785 	qla4xxx_mark_all_devices_missing(ha);
4786 	clear_bit(AF_INIT_DONE, &ha->flags);
4787 }
4788 
4789 static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session)
4790 {
4791 	struct iscsi_session *sess;
4792 	struct ddb_entry *ddb_entry;
4793 
4794 	sess = cls_session->dd_data;
4795 	ddb_entry = sess->dd_data;
4796 	ddb_entry->fw_ddb_device_state = DDB_DS_SESSION_FAILED;
4797 
4798 	if (ddb_entry->ddb_type == FLASH_DDB)
4799 		iscsi_block_session(ddb_entry->sess);
4800 	else
4801 		iscsi_session_failure(cls_session->dd_data,
4802 				      ISCSI_ERR_CONN_FAILED);
4803 }
4804 
4805 /**
4806  * qla4xxx_recover_adapter - recovers adapter after a fatal error
4807  * @ha: Pointer to host adapter structure.
4808  **/
4809 static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
4810 {
4811 	int status = QLA_ERROR;
4812 	uint8_t reset_chip = 0;
4813 	uint32_t dev_state;
4814 	unsigned long wait;
4815 
4816 	/* Stall incoming I/O until we are done */
4817 	scsi_block_requests(ha->host);
4818 	clear_bit(AF_ONLINE, &ha->flags);
4819 	clear_bit(AF_LINK_UP, &ha->flags);
4820 
4821 	DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
4822 
4823 	set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
4824 
4825 	if ((is_qla8032(ha) || is_qla8042(ha)) &&
4826 	    !test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
4827 		ql4_printk(KERN_INFO, ha, "%s: disabling pause transmit on port 0 & 1.\n",
4828 			   __func__);
4829 		/* disable pause frame for ISP83xx */
4830 		qla4_83xx_disable_pause(ha);
4831 	}
4832 
4833 	iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
4834 
4835 	if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
4836 		reset_chip = 1;
4837 
4838 	/* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
4839 	 * do not reset adapter, jump to initialize_adapter */
4840 	if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
4841 		status = QLA_SUCCESS;
4842 		goto recover_ha_init_adapter;
4843 	}
4844 
4845 	/* For the ISP-8xxx adapter, issue a stop_firmware if invoked
4846 	 * from eh_host_reset or ioctl module */
4847 	if (is_qla80XX(ha) && !reset_chip &&
4848 	    test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
4849 
4850 		DEBUG2(ql4_printk(KERN_INFO, ha,
4851 		    "scsi%ld: %s - Performing stop_firmware...\n",
4852 		    ha->host_no, __func__));
4853 		status = ha->isp_ops->reset_firmware(ha);
4854 		if (status == QLA_SUCCESS) {
4855 			ha->isp_ops->disable_intrs(ha);
4856 			qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
4857 			qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
4858 		} else {
4859 			/* If the stop_firmware fails then
4860 			 * reset the entire chip */
4861 			reset_chip = 1;
4862 			clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
4863 			set_bit(DPC_RESET_HA, &ha->dpc_flags);
4864 		}
4865 	}
4866 
4867 	/* Issue full chip reset if recovering from a catastrophic error,
4868 	 * or if stop_firmware fails for ISP-8xxx.
4869 	 * This is the default case for ISP-4xxx */
4870 	if (is_qla40XX(ha) || reset_chip) {
4871 		if (is_qla40XX(ha))
4872 			goto chip_reset;
4873 
4874 		/* Check if 8XXX firmware is alive or not
4875 		 * We may have arrived here from NEED_RESET
4876 		 * detection only */
4877 		if (test_bit(AF_FW_RECOVERY, &ha->flags))
4878 			goto chip_reset;
4879 
4880 		wait = jiffies + (FW_ALIVE_WAIT_TOV * HZ);
4881 		while (time_before(jiffies, wait)) {
4882 			if (qla4_8xxx_check_fw_alive(ha)) {
4883 				qla4xxx_mailbox_premature_completion(ha);
4884 				break;
4885 			}
4886 
4887 			set_current_state(TASK_UNINTERRUPTIBLE);
4888 			schedule_timeout(HZ);
4889 		}
4890 chip_reset:
4891 		if (!test_bit(AF_FW_RECOVERY, &ha->flags))
4892 			qla4xxx_cmd_wait(ha);
4893 
4894 		qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
4895 		DEBUG2(ql4_printk(KERN_INFO, ha,
4896 		    "scsi%ld: %s - Performing chip reset..\n",
4897 		    ha->host_no, __func__));
4898 		status = ha->isp_ops->reset_chip(ha);
4899 		qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
4900 	}
4901 
4902 	/* Flush any pending ddb changed AENs */
4903 	qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
4904 
4905 recover_ha_init_adapter:
4906 	/* Upon successful firmware/chip reset, re-initialize the adapter */
4907 	if (status == QLA_SUCCESS) {
4908 		/* For ISP-4xxx, force function 1 to always initialize
4909 		 * before function 3 to prevent both funcions from
4910 		 * stepping on top of the other */
4911 		if (is_qla40XX(ha) && (ha->mac_index == 3))
4912 			ssleep(6);
4913 
4914 		/* NOTE: AF_ONLINE flag set upon successful completion of
4915 		 * qla4xxx_initialize_adapter */
4916 		status = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
4917 		if (is_qla80XX(ha) && (status == QLA_ERROR)) {
4918 			status = qla4_8xxx_check_init_adapter_retry(ha);
4919 			if (status == QLA_ERROR) {
4920 				ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Don't retry recover adapter\n",
4921 					   ha->host_no, __func__);
4922 				qla4xxx_dead_adapter_cleanup(ha);
4923 				clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
4924 				clear_bit(DPC_RESET_HA, &ha->dpc_flags);
4925 				clear_bit(DPC_RESET_HA_FW_CONTEXT,
4926 					  &ha->dpc_flags);
4927 				goto exit_recover;
4928 			}
4929 		}
4930 	}
4931 
4932 	/* Retry failed adapter initialization, if necessary
4933 	 * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
4934 	 * case to prevent ping-pong resets between functions */
4935 	if (!test_bit(AF_ONLINE, &ha->flags) &&
4936 	    !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
4937 		/* Adapter initialization failed, see if we can retry
4938 		 * resetting the ha.
4939 		 * Since we don't want to block the DPC for too long
4940 		 * with multiple resets in the same thread,
4941 		 * utilize DPC to retry */
4942 		if (is_qla80XX(ha)) {
4943 			ha->isp_ops->idc_lock(ha);
4944 			dev_state = qla4_8xxx_rd_direct(ha,
4945 							QLA8XXX_CRB_DEV_STATE);
4946 			ha->isp_ops->idc_unlock(ha);
4947 			if (dev_state == QLA8XXX_DEV_FAILED) {
4948 				ql4_printk(KERN_INFO, ha, "%s: don't retry "
4949 					   "recover adapter. H/W is in Failed "
4950 					   "state\n", __func__);
4951 				qla4xxx_dead_adapter_cleanup(ha);
4952 				clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
4953 				clear_bit(DPC_RESET_HA, &ha->dpc_flags);
4954 				clear_bit(DPC_RESET_HA_FW_CONTEXT,
4955 						&ha->dpc_flags);
4956 				status = QLA_ERROR;
4957 
4958 				goto exit_recover;
4959 			}
4960 		}
4961 
4962 		if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
4963 			ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
4964 			DEBUG2(printk("scsi%ld: recover adapter - retrying "
4965 				      "(%d) more times\n", ha->host_no,
4966 				      ha->retry_reset_ha_cnt));
4967 			set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
4968 			status = QLA_ERROR;
4969 		} else {
4970 			if (ha->retry_reset_ha_cnt > 0) {
4971 				/* Schedule another Reset HA--DPC will retry */
4972 				ha->retry_reset_ha_cnt--;
4973 				DEBUG2(printk("scsi%ld: recover adapter - "
4974 					      "retry remaining %d\n",
4975 					      ha->host_no,
4976 					      ha->retry_reset_ha_cnt));
4977 				status = QLA_ERROR;
4978 			}
4979 
4980 			if (ha->retry_reset_ha_cnt == 0) {
4981 				/* Recover adapter retries have been exhausted.
4982 				 * Adapter DEAD */
4983 				DEBUG2(printk("scsi%ld: recover adapter "
4984 					      "failed - board disabled\n",
4985 					      ha->host_no));
4986 				qla4xxx_dead_adapter_cleanup(ha);
4987 				clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
4988 				clear_bit(DPC_RESET_HA, &ha->dpc_flags);
4989 				clear_bit(DPC_RESET_HA_FW_CONTEXT,
4990 					  &ha->dpc_flags);
4991 				status = QLA_ERROR;
4992 			}
4993 		}
4994 	} else {
4995 		clear_bit(DPC_RESET_HA, &ha->dpc_flags);
4996 		clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
4997 		clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
4998 	}
4999 
5000 exit_recover:
5001 	ha->adapter_error_count++;
5002 
5003 	if (test_bit(AF_ONLINE, &ha->flags))
5004 		ha->isp_ops->enable_intrs(ha);
5005 
5006 	scsi_unblock_requests(ha->host);
5007 
5008 	clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
5009 	DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
5010 	    status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
5011 
5012 	return status;
5013 }
5014 
5015 static void qla4xxx_relogin_devices(struct iscsi_cls_session *cls_session)
5016 {
5017 	struct iscsi_session *sess;
5018 	struct ddb_entry *ddb_entry;
5019 	struct scsi_qla_host *ha;
5020 
5021 	sess = cls_session->dd_data;
5022 	ddb_entry = sess->dd_data;
5023 	ha = ddb_entry->ha;
5024 	if (!iscsi_is_session_online(cls_session)) {
5025 		if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
5026 			ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
5027 				   " unblock session\n", ha->host_no, __func__,
5028 				   ddb_entry->fw_ddb_index);
5029 			iscsi_unblock_session(ddb_entry->sess);
5030 		} else {
5031 			/* Trigger relogin */
5032 			if (ddb_entry->ddb_type == FLASH_DDB) {
5033 				if (!(test_bit(DF_RELOGIN, &ddb_entry->flags) ||
5034 				      test_bit(DF_DISABLE_RELOGIN,
5035 					       &ddb_entry->flags)))
5036 					qla4xxx_arm_relogin_timer(ddb_entry);
5037 			} else
5038 				iscsi_session_failure(cls_session->dd_data,
5039 						      ISCSI_ERR_CONN_FAILED);
5040 		}
5041 	}
5042 }
5043 
5044 int qla4xxx_unblock_flash_ddb(struct iscsi_cls_session *cls_session)
5045 {
5046 	struct iscsi_session *sess;
5047 	struct ddb_entry *ddb_entry;
5048 	struct scsi_qla_host *ha;
5049 
5050 	sess = cls_session->dd_data;
5051 	ddb_entry = sess->dd_data;
5052 	ha = ddb_entry->ha;
5053 	ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
5054 		   " unblock session\n", ha->host_no, __func__,
5055 		   ddb_entry->fw_ddb_index);
5056 
5057 	iscsi_unblock_session(ddb_entry->sess);
5058 
5059 	/* Start scan target */
5060 	if (test_bit(AF_ONLINE, &ha->flags)) {
5061 		ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
5062 			   " start scan\n", ha->host_no, __func__,
5063 			   ddb_entry->fw_ddb_index);
5064 		scsi_queue_work(ha->host, &ddb_entry->sess->scan_work);
5065 	}
5066 	return QLA_SUCCESS;
5067 }
5068 
5069 int qla4xxx_unblock_ddb(struct iscsi_cls_session *cls_session)
5070 {
5071 	struct iscsi_session *sess;
5072 	struct ddb_entry *ddb_entry;
5073 	struct scsi_qla_host *ha;
5074 	int status = QLA_SUCCESS;
5075 
5076 	sess = cls_session->dd_data;
5077 	ddb_entry = sess->dd_data;
5078 	ha = ddb_entry->ha;
5079 	ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
5080 		   " unblock user space session\n", ha->host_no, __func__,
5081 		   ddb_entry->fw_ddb_index);
5082 
5083 	if (!iscsi_is_session_online(cls_session)) {
5084 		iscsi_conn_start(ddb_entry->conn);
5085 		iscsi_conn_login_event(ddb_entry->conn,
5086 				       ISCSI_CONN_STATE_LOGGED_IN);
5087 	} else {
5088 		ql4_printk(KERN_INFO, ha,
5089 			   "scsi%ld: %s: ddb[%d] session [%d] already logged in\n",
5090 			   ha->host_no, __func__, ddb_entry->fw_ddb_index,
5091 			   cls_session->sid);
5092 		status = QLA_ERROR;
5093 	}
5094 
5095 	return status;
5096 }
5097 
5098 static void qla4xxx_relogin_all_devices(struct scsi_qla_host *ha)
5099 {
5100 	iscsi_host_for_each_session(ha->host, qla4xxx_relogin_devices);
5101 }
5102 
5103 static void qla4xxx_relogin_flash_ddb(struct iscsi_cls_session *cls_sess)
5104 {
5105 	uint16_t relogin_timer;
5106 	struct iscsi_session *sess;
5107 	struct ddb_entry *ddb_entry;
5108 	struct scsi_qla_host *ha;
5109 
5110 	sess = cls_sess->dd_data;
5111 	ddb_entry = sess->dd_data;
5112 	ha = ddb_entry->ha;
5113 
5114 	relogin_timer = max(ddb_entry->default_relogin_timeout,
5115 			    (uint16_t)RELOGIN_TOV);
5116 	atomic_set(&ddb_entry->relogin_timer, relogin_timer);
5117 
5118 	DEBUG2(ql4_printk(KERN_INFO, ha,
5119 			  "scsi%ld: Relogin index [%d]. TOV=%d\n", ha->host_no,
5120 			  ddb_entry->fw_ddb_index, relogin_timer));
5121 
5122 	qla4xxx_login_flash_ddb(cls_sess);
5123 }
5124 
5125 static void qla4xxx_dpc_relogin(struct iscsi_cls_session *cls_sess)
5126 {
5127 	struct iscsi_session *sess;
5128 	struct ddb_entry *ddb_entry;
5129 	struct scsi_qla_host *ha;
5130 
5131 	sess = cls_sess->dd_data;
5132 	ddb_entry = sess->dd_data;
5133 	ha = ddb_entry->ha;
5134 
5135 	if (!(ddb_entry->ddb_type == FLASH_DDB))
5136 		return;
5137 
5138 	if (test_bit(DF_DISABLE_RELOGIN, &ddb_entry->flags))
5139 		return;
5140 
5141 	if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
5142 	    !iscsi_is_session_online(cls_sess)) {
5143 		DEBUG2(ql4_printk(KERN_INFO, ha,
5144 				  "relogin issued\n"));
5145 		qla4xxx_relogin_flash_ddb(cls_sess);
5146 	}
5147 }
5148 
5149 void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
5150 {
5151 	if (ha->dpc_thread)
5152 		queue_work(ha->dpc_thread, &ha->dpc_work);
5153 }
5154 
5155 static struct qla4_work_evt *
5156 qla4xxx_alloc_work(struct scsi_qla_host *ha, uint32_t data_size,
5157 		   enum qla4_work_type type)
5158 {
5159 	struct qla4_work_evt *e;
5160 	uint32_t size = sizeof(struct qla4_work_evt) + data_size;
5161 
5162 	e = kzalloc(size, GFP_ATOMIC);
5163 	if (!e)
5164 		return NULL;
5165 
5166 	INIT_LIST_HEAD(&e->list);
5167 	e->type = type;
5168 	return e;
5169 }
5170 
5171 static void qla4xxx_post_work(struct scsi_qla_host *ha,
5172 			     struct qla4_work_evt *e)
5173 {
5174 	unsigned long flags;
5175 
5176 	spin_lock_irqsave(&ha->work_lock, flags);
5177 	list_add_tail(&e->list, &ha->work_list);
5178 	spin_unlock_irqrestore(&ha->work_lock, flags);
5179 	qla4xxx_wake_dpc(ha);
5180 }
5181 
5182 int qla4xxx_post_aen_work(struct scsi_qla_host *ha,
5183 			  enum iscsi_host_event_code aen_code,
5184 			  uint32_t data_size, uint8_t *data)
5185 {
5186 	struct qla4_work_evt *e;
5187 
5188 	e = qla4xxx_alloc_work(ha, data_size, QLA4_EVENT_AEN);
5189 	if (!e)
5190 		return QLA_ERROR;
5191 
5192 	e->u.aen.code = aen_code;
5193 	e->u.aen.data_size = data_size;
5194 	memcpy(e->u.aen.data, data, data_size);
5195 
5196 	qla4xxx_post_work(ha, e);
5197 
5198 	return QLA_SUCCESS;
5199 }
5200 
5201 int qla4xxx_post_ping_evt_work(struct scsi_qla_host *ha,
5202 			       uint32_t status, uint32_t pid,
5203 			       uint32_t data_size, uint8_t *data)
5204 {
5205 	struct qla4_work_evt *e;
5206 
5207 	e = qla4xxx_alloc_work(ha, data_size, QLA4_EVENT_PING_STATUS);
5208 	if (!e)
5209 		return QLA_ERROR;
5210 
5211 	e->u.ping.status = status;
5212 	e->u.ping.pid = pid;
5213 	e->u.ping.data_size = data_size;
5214 	memcpy(e->u.ping.data, data, data_size);
5215 
5216 	qla4xxx_post_work(ha, e);
5217 
5218 	return QLA_SUCCESS;
5219 }
5220 
5221 static void qla4xxx_do_work(struct scsi_qla_host *ha)
5222 {
5223 	struct qla4_work_evt *e, *tmp;
5224 	unsigned long flags;
5225 	LIST_HEAD(work);
5226 
5227 	spin_lock_irqsave(&ha->work_lock, flags);
5228 	list_splice_init(&ha->work_list, &work);
5229 	spin_unlock_irqrestore(&ha->work_lock, flags);
5230 
5231 	list_for_each_entry_safe(e, tmp, &work, list) {
5232 		list_del_init(&e->list);
5233 
5234 		switch (e->type) {
5235 		case QLA4_EVENT_AEN:
5236 			iscsi_post_host_event(ha->host_no,
5237 					      &qla4xxx_iscsi_transport,
5238 					      e->u.aen.code,
5239 					      e->u.aen.data_size,
5240 					      e->u.aen.data);
5241 			break;
5242 		case QLA4_EVENT_PING_STATUS:
5243 			iscsi_ping_comp_event(ha->host_no,
5244 					      &qla4xxx_iscsi_transport,
5245 					      e->u.ping.status,
5246 					      e->u.ping.pid,
5247 					      e->u.ping.data_size,
5248 					      e->u.ping.data);
5249 			break;
5250 		default:
5251 			ql4_printk(KERN_WARNING, ha, "event type: 0x%x not "
5252 				   "supported", e->type);
5253 		}
5254 		kfree(e);
5255 	}
5256 }
5257 
5258 /**
5259  * qla4xxx_do_dpc - dpc routine
5260  * @data: in our case pointer to adapter structure
5261  *
5262  * This routine is a task that is schedule by the interrupt handler
5263  * to perform the background processing for interrupts.  We put it
5264  * on a task queue that is consumed whenever the scheduler runs; that's
5265  * so you can do anything (i.e. put the process to sleep etc).  In fact,
5266  * the mid-level tries to sleep when it reaches the driver threshold
5267  * "host->can_queue". This can cause a panic if we were in our interrupt code.
5268  **/
5269 static void qla4xxx_do_dpc(struct work_struct *work)
5270 {
5271 	struct scsi_qla_host *ha =
5272 		container_of(work, struct scsi_qla_host, dpc_work);
5273 	int status = QLA_ERROR;
5274 
5275 	DEBUG2(ql4_printk(KERN_INFO, ha,
5276 			  "scsi%ld: %s: DPC handler waking up. flags = 0x%08lx, dpc_flags = 0x%08lx\n",
5277 			  ha->host_no, __func__, ha->flags, ha->dpc_flags));
5278 
5279 	/* Initialization not yet finished. Don't do anything yet. */
5280 	if (!test_bit(AF_INIT_DONE, &ha->flags))
5281 		return;
5282 
5283 	if (test_bit(AF_EEH_BUSY, &ha->flags)) {
5284 		DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
5285 		    ha->host_no, __func__, ha->flags));
5286 		return;
5287 	}
5288 
5289 	/* post events to application */
5290 	qla4xxx_do_work(ha);
5291 
5292 	if (is_qla80XX(ha)) {
5293 		if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
5294 			if (is_qla8032(ha) || is_qla8042(ha)) {
5295 				ql4_printk(KERN_INFO, ha, "%s: disabling pause transmit on port 0 & 1.\n",
5296 					   __func__);
5297 				/* disable pause frame for ISP83xx */
5298 				qla4_83xx_disable_pause(ha);
5299 			}
5300 
5301 			ha->isp_ops->idc_lock(ha);
5302 			qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
5303 					    QLA8XXX_DEV_FAILED);
5304 			ha->isp_ops->idc_unlock(ha);
5305 			ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
5306 			qla4_8xxx_device_state_handler(ha);
5307 		}
5308 
5309 		if (test_bit(DPC_POST_IDC_ACK, &ha->dpc_flags)) {
5310 			if (is_qla8042(ha)) {
5311 				if (ha->idc_info.info2 &
5312 				    ENABLE_INTERNAL_LOOPBACK) {
5313 					ql4_printk(KERN_INFO, ha, "%s: Disabling ACB\n",
5314 						   __func__);
5315 					status = qla4_84xx_config_acb(ha,
5316 							    ACB_CONFIG_DISABLE);
5317 					if (status != QLA_SUCCESS) {
5318 						ql4_printk(KERN_INFO, ha, "%s: ACB config failed\n",
5319 							   __func__);
5320 					}
5321 				}
5322 			}
5323 			qla4_83xx_post_idc_ack(ha);
5324 			clear_bit(DPC_POST_IDC_ACK, &ha->dpc_flags);
5325 		}
5326 
5327 		if (is_qla8042(ha) &&
5328 		    test_bit(DPC_RESTORE_ACB, &ha->dpc_flags)) {
5329 			ql4_printk(KERN_INFO, ha, "%s: Restoring ACB\n",
5330 				   __func__);
5331 			if (qla4_84xx_config_acb(ha, ACB_CONFIG_SET) !=
5332 			    QLA_SUCCESS) {
5333 				ql4_printk(KERN_INFO, ha, "%s: ACB config failed ",
5334 					   __func__);
5335 			}
5336 			clear_bit(DPC_RESTORE_ACB, &ha->dpc_flags);
5337 		}
5338 
5339 		if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
5340 			qla4_8xxx_need_qsnt_handler(ha);
5341 		}
5342 	}
5343 
5344 	if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
5345 	    (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
5346 	    test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
5347 	    test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
5348 		if ((is_qla8022(ha) && ql4xdontresethba) ||
5349 		    ((is_qla8032(ha) || is_qla8042(ha)) &&
5350 		     qla4_83xx_idc_dontreset(ha))) {
5351 			DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
5352 			    ha->host_no, __func__));
5353 			clear_bit(DPC_RESET_HA, &ha->dpc_flags);
5354 			clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
5355 			clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
5356 			goto dpc_post_reset_ha;
5357 		}
5358 		if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
5359 		    test_bit(DPC_RESET_HA, &ha->dpc_flags))
5360 			qla4xxx_recover_adapter(ha);
5361 
5362 		if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
5363 			uint8_t wait_time = RESET_INTR_TOV;
5364 
5365 			while ((readw(&ha->reg->ctrl_status) &
5366 				(CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
5367 				if (--wait_time == 0)
5368 					break;
5369 				msleep(1000);
5370 			}
5371 			if (wait_time == 0)
5372 				DEBUG2(printk("scsi%ld: %s: SR|FSR "
5373 					      "bit not cleared-- resetting\n",
5374 					      ha->host_no, __func__));
5375 			qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
5376 			if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
5377 				qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
5378 				status = qla4xxx_recover_adapter(ha);
5379 			}
5380 			clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
5381 			if (status == QLA_SUCCESS)
5382 				ha->isp_ops->enable_intrs(ha);
5383 		}
5384 	}
5385 
5386 dpc_post_reset_ha:
5387 	/* ---- process AEN? --- */
5388 	if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
5389 		qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
5390 
5391 	/* ---- Get DHCP IP Address? --- */
5392 	if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
5393 		qla4xxx_get_dhcp_ip_address(ha);
5394 
5395 	/* ---- relogin device? --- */
5396 	if (adapter_up(ha) &&
5397 	    test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
5398 		iscsi_host_for_each_session(ha->host, qla4xxx_dpc_relogin);
5399 	}
5400 
5401 	/* ---- link change? --- */
5402 	if (!test_bit(AF_LOOPBACK, &ha->flags) &&
5403 	    test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
5404 		if (!test_bit(AF_LINK_UP, &ha->flags)) {
5405 			/* ---- link down? --- */
5406 			qla4xxx_mark_all_devices_missing(ha);
5407 		} else {
5408 			/* ---- link up? --- *
5409 			 * F/W will auto login to all devices ONLY ONCE after
5410 			 * link up during driver initialization and runtime
5411 			 * fatal error recovery.  Therefore, the driver must
5412 			 * manually relogin to devices when recovering from
5413 			 * connection failures, logouts, expired KATO, etc. */
5414 			if (test_and_clear_bit(AF_BUILD_DDB_LIST, &ha->flags)) {
5415 				qla4xxx_build_ddb_list(ha, ha->is_reset);
5416 				iscsi_host_for_each_session(ha->host,
5417 						qla4xxx_login_flash_ddb);
5418 			} else
5419 				qla4xxx_relogin_all_devices(ha);
5420 		}
5421 	}
5422 	if (test_and_clear_bit(DPC_SYSFS_DDB_EXPORT, &ha->dpc_flags)) {
5423 		if (qla4xxx_sysfs_ddb_export(ha))
5424 			ql4_printk(KERN_ERR, ha, "%s: Error exporting ddb to sysfs\n",
5425 				   __func__);
5426 	}
5427 }
5428 
5429 /**
5430  * qla4xxx_free_adapter - release the adapter
5431  * @ha: pointer to adapter structure
5432  **/
5433 static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
5434 {
5435 	qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
5436 
5437 	/* Turn-off interrupts on the card. */
5438 	ha->isp_ops->disable_intrs(ha);
5439 
5440 	if (is_qla40XX(ha)) {
5441 		writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
5442 		       &ha->reg->ctrl_status);
5443 		readl(&ha->reg->ctrl_status);
5444 	} else if (is_qla8022(ha)) {
5445 		writel(0, &ha->qla4_82xx_reg->host_int);
5446 		readl(&ha->qla4_82xx_reg->host_int);
5447 	} else if (is_qla8032(ha) || is_qla8042(ha)) {
5448 		writel(0, &ha->qla4_83xx_reg->risc_intr);
5449 		readl(&ha->qla4_83xx_reg->risc_intr);
5450 	}
5451 
5452 	/* Remove timer thread, if present */
5453 	if (ha->timer_active)
5454 		qla4xxx_stop_timer(ha);
5455 
5456 	/* Kill the kernel thread for this host */
5457 	if (ha->dpc_thread)
5458 		destroy_workqueue(ha->dpc_thread);
5459 
5460 	/* Kill the kernel thread for this host */
5461 	if (ha->task_wq)
5462 		destroy_workqueue(ha->task_wq);
5463 
5464 	/* Put firmware in known state */
5465 	ha->isp_ops->reset_firmware(ha);
5466 
5467 	if (is_qla80XX(ha)) {
5468 		ha->isp_ops->idc_lock(ha);
5469 		qla4_8xxx_clear_drv_active(ha);
5470 		ha->isp_ops->idc_unlock(ha);
5471 	}
5472 
5473 	/* Detach interrupts */
5474 	qla4xxx_free_irqs(ha);
5475 
5476 	/* free extra memory */
5477 	qla4xxx_mem_free(ha);
5478 }
5479 
5480 int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
5481 {
5482 	int status = 0;
5483 	unsigned long mem_base, mem_len, db_base, db_len;
5484 	struct pci_dev *pdev = ha->pdev;
5485 
5486 	status = pci_request_regions(pdev, DRIVER_NAME);
5487 	if (status) {
5488 		printk(KERN_WARNING
5489 		    "scsi(%ld) Failed to reserve PIO regions (%s) "
5490 		    "status=%d\n", ha->host_no, pci_name(pdev), status);
5491 		goto iospace_error_exit;
5492 	}
5493 
5494 	DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
5495 	    __func__, pdev->revision));
5496 	ha->revision_id = pdev->revision;
5497 
5498 	/* remap phys address */
5499 	mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
5500 	mem_len = pci_resource_len(pdev, 0);
5501 	DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
5502 	    __func__, mem_base, mem_len));
5503 
5504 	/* mapping of pcibase pointer */
5505 	ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
5506 	if (!ha->nx_pcibase) {
5507 		printk(KERN_ERR
5508 		    "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
5509 		pci_release_regions(ha->pdev);
5510 		goto iospace_error_exit;
5511 	}
5512 
5513 	/* Mapping of IO base pointer, door bell read and write pointer */
5514 
5515 	/* mapping of IO base pointer */
5516 	if (is_qla8022(ha)) {
5517 		ha->qla4_82xx_reg = (struct device_reg_82xx  __iomem *)
5518 				    ((uint8_t *)ha->nx_pcibase + 0xbc000 +
5519 				     (ha->pdev->devfn << 11));
5520 		ha->nx_db_wr_ptr = (ha->pdev->devfn == 4 ? QLA82XX_CAM_RAM_DB1 :
5521 				    QLA82XX_CAM_RAM_DB2);
5522 	} else if (is_qla8032(ha) || is_qla8042(ha)) {
5523 		ha->qla4_83xx_reg = (struct device_reg_83xx __iomem *)
5524 				    ((uint8_t *)ha->nx_pcibase);
5525 	}
5526 
5527 	db_base = pci_resource_start(pdev, 4);  /* doorbell is on bar 4 */
5528 	db_len = pci_resource_len(pdev, 4);
5529 
5530 	return 0;
5531 iospace_error_exit:
5532 	return -ENOMEM;
5533 }
5534 
5535 /***
5536  * qla4xxx_iospace_config - maps registers
5537  * @ha: pointer to adapter structure
5538  *
5539  * This routines maps HBA's registers from the pci address space
5540  * into the kernel virtual address space for memory mapped i/o.
5541  **/
5542 int qla4xxx_iospace_config(struct scsi_qla_host *ha)
5543 {
5544 	unsigned long pio, pio_len, pio_flags;
5545 	unsigned long mmio, mmio_len, mmio_flags;
5546 
5547 	pio = pci_resource_start(ha->pdev, 0);
5548 	pio_len = pci_resource_len(ha->pdev, 0);
5549 	pio_flags = pci_resource_flags(ha->pdev, 0);
5550 	if (pio_flags & IORESOURCE_IO) {
5551 		if (pio_len < MIN_IOBASE_LEN) {
5552 			ql4_printk(KERN_WARNING, ha,
5553 				"Invalid PCI I/O region size\n");
5554 			pio = 0;
5555 		}
5556 	} else {
5557 		ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
5558 		pio = 0;
5559 	}
5560 
5561 	/* Use MMIO operations for all accesses. */
5562 	mmio = pci_resource_start(ha->pdev, 1);
5563 	mmio_len = pci_resource_len(ha->pdev, 1);
5564 	mmio_flags = pci_resource_flags(ha->pdev, 1);
5565 
5566 	if (!(mmio_flags & IORESOURCE_MEM)) {
5567 		ql4_printk(KERN_ERR, ha,
5568 		    "region #0 not an MMIO resource, aborting\n");
5569 
5570 		goto iospace_error_exit;
5571 	}
5572 
5573 	if (mmio_len < MIN_IOBASE_LEN) {
5574 		ql4_printk(KERN_ERR, ha,
5575 		    "Invalid PCI mem region size, aborting\n");
5576 		goto iospace_error_exit;
5577 	}
5578 
5579 	if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
5580 		ql4_printk(KERN_WARNING, ha,
5581 		    "Failed to reserve PIO/MMIO regions\n");
5582 
5583 		goto iospace_error_exit;
5584 	}
5585 
5586 	ha->pio_address = pio;
5587 	ha->pio_length = pio_len;
5588 	ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
5589 	if (!ha->reg) {
5590 		ql4_printk(KERN_ERR, ha,
5591 		    "cannot remap MMIO, aborting\n");
5592 
5593 		goto iospace_error_exit;
5594 	}
5595 
5596 	return 0;
5597 
5598 iospace_error_exit:
5599 	return -ENOMEM;
5600 }
5601 
5602 static struct isp_operations qla4xxx_isp_ops = {
5603 	.iospace_config         = qla4xxx_iospace_config,
5604 	.pci_config             = qla4xxx_pci_config,
5605 	.disable_intrs          = qla4xxx_disable_intrs,
5606 	.enable_intrs           = qla4xxx_enable_intrs,
5607 	.start_firmware         = qla4xxx_start_firmware,
5608 	.intr_handler           = qla4xxx_intr_handler,
5609 	.interrupt_service_routine = qla4xxx_interrupt_service_routine,
5610 	.reset_chip             = qla4xxx_soft_reset,
5611 	.reset_firmware         = qla4xxx_hw_reset,
5612 	.queue_iocb             = qla4xxx_queue_iocb,
5613 	.complete_iocb          = qla4xxx_complete_iocb,
5614 	.rd_shdw_req_q_out      = qla4xxx_rd_shdw_req_q_out,
5615 	.rd_shdw_rsp_q_in       = qla4xxx_rd_shdw_rsp_q_in,
5616 	.get_sys_info           = qla4xxx_get_sys_info,
5617 	.queue_mailbox_command	= qla4xxx_queue_mbox_cmd,
5618 	.process_mailbox_interrupt = qla4xxx_process_mbox_intr,
5619 };
5620 
5621 static struct isp_operations qla4_82xx_isp_ops = {
5622 	.iospace_config         = qla4_8xxx_iospace_config,
5623 	.pci_config             = qla4_8xxx_pci_config,
5624 	.disable_intrs          = qla4_82xx_disable_intrs,
5625 	.enable_intrs           = qla4_82xx_enable_intrs,
5626 	.start_firmware         = qla4_8xxx_load_risc,
5627 	.restart_firmware	= qla4_82xx_try_start_fw,
5628 	.intr_handler           = qla4_82xx_intr_handler,
5629 	.interrupt_service_routine = qla4_82xx_interrupt_service_routine,
5630 	.need_reset		= qla4_8xxx_need_reset,
5631 	.reset_chip             = qla4_82xx_isp_reset,
5632 	.reset_firmware         = qla4_8xxx_stop_firmware,
5633 	.queue_iocb             = qla4_82xx_queue_iocb,
5634 	.complete_iocb          = qla4_82xx_complete_iocb,
5635 	.rd_shdw_req_q_out      = qla4_82xx_rd_shdw_req_q_out,
5636 	.rd_shdw_rsp_q_in       = qla4_82xx_rd_shdw_rsp_q_in,
5637 	.get_sys_info           = qla4_8xxx_get_sys_info,
5638 	.rd_reg_direct		= qla4_82xx_rd_32,
5639 	.wr_reg_direct		= qla4_82xx_wr_32,
5640 	.rd_reg_indirect	= qla4_82xx_md_rd_32,
5641 	.wr_reg_indirect	= qla4_82xx_md_wr_32,
5642 	.idc_lock		= qla4_82xx_idc_lock,
5643 	.idc_unlock		= qla4_82xx_idc_unlock,
5644 	.rom_lock_recovery	= qla4_82xx_rom_lock_recovery,
5645 	.queue_mailbox_command	= qla4_82xx_queue_mbox_cmd,
5646 	.process_mailbox_interrupt = qla4_82xx_process_mbox_intr,
5647 };
5648 
5649 static struct isp_operations qla4_83xx_isp_ops = {
5650 	.iospace_config		= qla4_8xxx_iospace_config,
5651 	.pci_config		= qla4_8xxx_pci_config,
5652 	.disable_intrs		= qla4_83xx_disable_intrs,
5653 	.enable_intrs		= qla4_83xx_enable_intrs,
5654 	.start_firmware		= qla4_8xxx_load_risc,
5655 	.restart_firmware	= qla4_83xx_start_firmware,
5656 	.intr_handler		= qla4_83xx_intr_handler,
5657 	.interrupt_service_routine = qla4_83xx_interrupt_service_routine,
5658 	.need_reset		= qla4_8xxx_need_reset,
5659 	.reset_chip		= qla4_83xx_isp_reset,
5660 	.reset_firmware		= qla4_8xxx_stop_firmware,
5661 	.queue_iocb		= qla4_83xx_queue_iocb,
5662 	.complete_iocb		= qla4_83xx_complete_iocb,
5663 	.rd_shdw_req_q_out	= qla4xxx_rd_shdw_req_q_out,
5664 	.rd_shdw_rsp_q_in	= qla4xxx_rd_shdw_rsp_q_in,
5665 	.get_sys_info		= qla4_8xxx_get_sys_info,
5666 	.rd_reg_direct		= qla4_83xx_rd_reg,
5667 	.wr_reg_direct		= qla4_83xx_wr_reg,
5668 	.rd_reg_indirect	= qla4_83xx_rd_reg_indirect,
5669 	.wr_reg_indirect	= qla4_83xx_wr_reg_indirect,
5670 	.idc_lock		= qla4_83xx_drv_lock,
5671 	.idc_unlock		= qla4_83xx_drv_unlock,
5672 	.rom_lock_recovery	= qla4_83xx_rom_lock_recovery,
5673 	.queue_mailbox_command	= qla4_83xx_queue_mbox_cmd,
5674 	.process_mailbox_interrupt = qla4_83xx_process_mbox_intr,
5675 };
5676 
5677 uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
5678 {
5679 	return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
5680 }
5681 
5682 uint16_t qla4_82xx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
5683 {
5684 	return (uint16_t)le32_to_cpu(readl(&ha->qla4_82xx_reg->req_q_out));
5685 }
5686 
5687 uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
5688 {
5689 	return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
5690 }
5691 
5692 uint16_t qla4_82xx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
5693 {
5694 	return (uint16_t)le32_to_cpu(readl(&ha->qla4_82xx_reg->rsp_q_in));
5695 }
5696 
5697 static ssize_t qla4xxx_show_boot_eth_info(void *data, int type, char *buf)
5698 {
5699 	struct scsi_qla_host *ha = data;
5700 	char *str = buf;
5701 	int rc;
5702 
5703 	switch (type) {
5704 	case ISCSI_BOOT_ETH_FLAGS:
5705 		rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
5706 		break;
5707 	case ISCSI_BOOT_ETH_INDEX:
5708 		rc = sprintf(str, "0\n");
5709 		break;
5710 	case ISCSI_BOOT_ETH_MAC:
5711 		rc = sysfs_format_mac(str, ha->my_mac,
5712 				      MAC_ADDR_LEN);
5713 		break;
5714 	default:
5715 		rc = -ENOSYS;
5716 		break;
5717 	}
5718 	return rc;
5719 }
5720 
5721 static umode_t qla4xxx_eth_get_attr_visibility(void *data, int type)
5722 {
5723 	int rc;
5724 
5725 	switch (type) {
5726 	case ISCSI_BOOT_ETH_FLAGS:
5727 	case ISCSI_BOOT_ETH_MAC:
5728 	case ISCSI_BOOT_ETH_INDEX:
5729 		rc = S_IRUGO;
5730 		break;
5731 	default:
5732 		rc = 0;
5733 		break;
5734 	}
5735 	return rc;
5736 }
5737 
5738 static ssize_t qla4xxx_show_boot_ini_info(void *data, int type, char *buf)
5739 {
5740 	struct scsi_qla_host *ha = data;
5741 	char *str = buf;
5742 	int rc;
5743 
5744 	switch (type) {
5745 	case ISCSI_BOOT_INI_INITIATOR_NAME:
5746 		rc = sprintf(str, "%s\n", ha->name_string);
5747 		break;
5748 	default:
5749 		rc = -ENOSYS;
5750 		break;
5751 	}
5752 	return rc;
5753 }
5754 
5755 static umode_t qla4xxx_ini_get_attr_visibility(void *data, int type)
5756 {
5757 	int rc;
5758 
5759 	switch (type) {
5760 	case ISCSI_BOOT_INI_INITIATOR_NAME:
5761 		rc = S_IRUGO;
5762 		break;
5763 	default:
5764 		rc = 0;
5765 		break;
5766 	}
5767 	return rc;
5768 }
5769 
5770 static ssize_t
5771 qla4xxx_show_boot_tgt_info(struct ql4_boot_session_info *boot_sess, int type,
5772 			   char *buf)
5773 {
5774 	struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
5775 	char *str = buf;
5776 	int rc;
5777 
5778 	switch (type) {
5779 	case ISCSI_BOOT_TGT_NAME:
5780 		rc = sprintf(buf, "%s\n", (char *)&boot_sess->target_name);
5781 		break;
5782 	case ISCSI_BOOT_TGT_IP_ADDR:
5783 		if (boot_sess->conn_list[0].dest_ipaddr.ip_type == 0x1)
5784 			rc = sprintf(buf, "%pI4\n",
5785 				     &boot_conn->dest_ipaddr.ip_address);
5786 		else
5787 			rc = sprintf(str, "%pI6\n",
5788 				     &boot_conn->dest_ipaddr.ip_address);
5789 		break;
5790 	case ISCSI_BOOT_TGT_PORT:
5791 			rc = sprintf(str, "%d\n", boot_conn->dest_port);
5792 		break;
5793 	case ISCSI_BOOT_TGT_CHAP_NAME:
5794 		rc = sprintf(str,  "%.*s\n",
5795 			     boot_conn->chap.target_chap_name_length,
5796 			     (char *)&boot_conn->chap.target_chap_name);
5797 		break;
5798 	case ISCSI_BOOT_TGT_CHAP_SECRET:
5799 		rc = sprintf(str,  "%.*s\n",
5800 			     boot_conn->chap.target_secret_length,
5801 			     (char *)&boot_conn->chap.target_secret);
5802 		break;
5803 	case ISCSI_BOOT_TGT_REV_CHAP_NAME:
5804 		rc = sprintf(str,  "%.*s\n",
5805 			     boot_conn->chap.intr_chap_name_length,
5806 			     (char *)&boot_conn->chap.intr_chap_name);
5807 		break;
5808 	case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
5809 		rc = sprintf(str,  "%.*s\n",
5810 			     boot_conn->chap.intr_secret_length,
5811 			     (char *)&boot_conn->chap.intr_secret);
5812 		break;
5813 	case ISCSI_BOOT_TGT_FLAGS:
5814 		rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
5815 		break;
5816 	case ISCSI_BOOT_TGT_NIC_ASSOC:
5817 		rc = sprintf(str, "0\n");
5818 		break;
5819 	default:
5820 		rc = -ENOSYS;
5821 		break;
5822 	}
5823 	return rc;
5824 }
5825 
5826 static ssize_t qla4xxx_show_boot_tgt_pri_info(void *data, int type, char *buf)
5827 {
5828 	struct scsi_qla_host *ha = data;
5829 	struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_pri_sess);
5830 
5831 	return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
5832 }
5833 
5834 static ssize_t qla4xxx_show_boot_tgt_sec_info(void *data, int type, char *buf)
5835 {
5836 	struct scsi_qla_host *ha = data;
5837 	struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_sec_sess);
5838 
5839 	return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
5840 }
5841 
5842 static umode_t qla4xxx_tgt_get_attr_visibility(void *data, int type)
5843 {
5844 	int rc;
5845 
5846 	switch (type) {
5847 	case ISCSI_BOOT_TGT_NAME:
5848 	case ISCSI_BOOT_TGT_IP_ADDR:
5849 	case ISCSI_BOOT_TGT_PORT:
5850 	case ISCSI_BOOT_TGT_CHAP_NAME:
5851 	case ISCSI_BOOT_TGT_CHAP_SECRET:
5852 	case ISCSI_BOOT_TGT_REV_CHAP_NAME:
5853 	case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
5854 	case ISCSI_BOOT_TGT_NIC_ASSOC:
5855 	case ISCSI_BOOT_TGT_FLAGS:
5856 		rc = S_IRUGO;
5857 		break;
5858 	default:
5859 		rc = 0;
5860 		break;
5861 	}
5862 	return rc;
5863 }
5864 
5865 static void qla4xxx_boot_release(void *data)
5866 {
5867 	struct scsi_qla_host *ha = data;
5868 
5869 	scsi_host_put(ha->host);
5870 }
5871 
5872 static int get_fw_boot_info(struct scsi_qla_host *ha, uint16_t ddb_index[])
5873 {
5874 	dma_addr_t buf_dma;
5875 	uint32_t addr, pri_addr, sec_addr;
5876 	uint32_t offset;
5877 	uint16_t func_num;
5878 	uint8_t val;
5879 	uint8_t *buf = NULL;
5880 	size_t size = 13 * sizeof(uint8_t);
5881 	int ret = QLA_SUCCESS;
5882 
5883 	func_num = PCI_FUNC(ha->pdev->devfn);
5884 
5885 	ql4_printk(KERN_INFO, ha, "%s: Get FW boot info for 0x%x func %d\n",
5886 		   __func__, ha->pdev->device, func_num);
5887 
5888 	if (is_qla40XX(ha)) {
5889 		if (func_num == 1) {
5890 			addr = NVRAM_PORT0_BOOT_MODE;
5891 			pri_addr = NVRAM_PORT0_BOOT_PRI_TGT;
5892 			sec_addr = NVRAM_PORT0_BOOT_SEC_TGT;
5893 		} else if (func_num == 3) {
5894 			addr = NVRAM_PORT1_BOOT_MODE;
5895 			pri_addr = NVRAM_PORT1_BOOT_PRI_TGT;
5896 			sec_addr = NVRAM_PORT1_BOOT_SEC_TGT;
5897 		} else {
5898 			ret = QLA_ERROR;
5899 			goto exit_boot_info;
5900 		}
5901 
5902 		/* Check Boot Mode */
5903 		val = rd_nvram_byte(ha, addr);
5904 		if (!(val & 0x07)) {
5905 			DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Adapter boot "
5906 					  "options : 0x%x\n", __func__, val));
5907 			ret = QLA_ERROR;
5908 			goto exit_boot_info;
5909 		}
5910 
5911 		/* get primary valid target index */
5912 		val = rd_nvram_byte(ha, pri_addr);
5913 		if (val & BIT_7)
5914 			ddb_index[0] = (val & 0x7f);
5915 
5916 		/* get secondary valid target index */
5917 		val = rd_nvram_byte(ha, sec_addr);
5918 		if (val & BIT_7)
5919 			ddb_index[1] = (val & 0x7f);
5920 
5921 	} else if (is_qla80XX(ha)) {
5922 		buf = dma_alloc_coherent(&ha->pdev->dev, size,
5923 					 &buf_dma, GFP_KERNEL);
5924 		if (!buf) {
5925 			DEBUG2(ql4_printk(KERN_ERR, ha,
5926 					  "%s: Unable to allocate dma buffer\n",
5927 					   __func__));
5928 			ret = QLA_ERROR;
5929 			goto exit_boot_info;
5930 		}
5931 
5932 		if (ha->port_num == 0)
5933 			offset = BOOT_PARAM_OFFSET_PORT0;
5934 		else if (ha->port_num == 1)
5935 			offset = BOOT_PARAM_OFFSET_PORT1;
5936 		else {
5937 			ret = QLA_ERROR;
5938 			goto exit_boot_info_free;
5939 		}
5940 		addr = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_iscsi_param * 4) +
5941 		       offset;
5942 		if (qla4xxx_get_flash(ha, buf_dma, addr,
5943 				      13 * sizeof(uint8_t)) != QLA_SUCCESS) {
5944 			DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: Get Flash"
5945 					  " failed\n", ha->host_no, __func__));
5946 			ret = QLA_ERROR;
5947 			goto exit_boot_info_free;
5948 		}
5949 		/* Check Boot Mode */
5950 		if (!(buf[1] & 0x07)) {
5951 			DEBUG2(ql4_printk(KERN_INFO, ha, "Firmware boot options"
5952 					  " : 0x%x\n", buf[1]));
5953 			ret = QLA_ERROR;
5954 			goto exit_boot_info_free;
5955 		}
5956 
5957 		/* get primary valid target index */
5958 		if (buf[2] & BIT_7)
5959 			ddb_index[0] = buf[2] & 0x7f;
5960 
5961 		/* get secondary valid target index */
5962 		if (buf[11] & BIT_7)
5963 			ddb_index[1] = buf[11] & 0x7f;
5964 	} else {
5965 		ret = QLA_ERROR;
5966 		goto exit_boot_info;
5967 	}
5968 
5969 	DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Primary target ID %d, Secondary"
5970 			  " target ID %d\n", __func__, ddb_index[0],
5971 			  ddb_index[1]));
5972 
5973 exit_boot_info_free:
5974 	dma_free_coherent(&ha->pdev->dev, size, buf, buf_dma);
5975 exit_boot_info:
5976 	ha->pri_ddb_idx = ddb_index[0];
5977 	ha->sec_ddb_idx = ddb_index[1];
5978 	return ret;
5979 }
5980 
5981 /**
5982  * qla4xxx_get_bidi_chap - Get a BIDI CHAP user and password
5983  * @ha: pointer to adapter structure
5984  * @username: CHAP username to be returned
5985  * @password: CHAP password to be returned
5986  *
5987  * If a boot entry has BIDI CHAP enabled then we need to set the BIDI CHAP
5988  * user and password in the sysfs entry in /sys/firmware/iscsi_boot#/.
5989  * So from the CHAP cache find the first BIDI CHAP entry and set it
5990  * to the boot record in sysfs.
5991  **/
5992 static int qla4xxx_get_bidi_chap(struct scsi_qla_host *ha, char *username,
5993 			    char *password)
5994 {
5995 	int i, ret = -EINVAL;
5996 	int max_chap_entries = 0;
5997 	struct ql4_chap_table *chap_table;
5998 
5999 	if (is_qla80XX(ha))
6000 		max_chap_entries = (ha->hw.flt_chap_size / 2) /
6001 						sizeof(struct ql4_chap_table);
6002 	else
6003 		max_chap_entries = MAX_CHAP_ENTRIES_40XX;
6004 
6005 	if (!ha->chap_list) {
6006 		ql4_printk(KERN_ERR, ha, "Do not have CHAP table cache\n");
6007 		return ret;
6008 	}
6009 
6010 	mutex_lock(&ha->chap_sem);
6011 	for (i = 0; i < max_chap_entries; i++) {
6012 		chap_table = (struct ql4_chap_table *)ha->chap_list + i;
6013 		if (chap_table->cookie !=
6014 		    __constant_cpu_to_le16(CHAP_VALID_COOKIE)) {
6015 			continue;
6016 		}
6017 
6018 		if (chap_table->flags & BIT_7) /* local */
6019 			continue;
6020 
6021 		if (!(chap_table->flags & BIT_6)) /* Not BIDI */
6022 			continue;
6023 
6024 		strlcpy(password, chap_table->secret, QL4_CHAP_MAX_SECRET_LEN);
6025 		strlcpy(username, chap_table->name, QL4_CHAP_MAX_NAME_LEN);
6026 		ret = 0;
6027 		break;
6028 	}
6029 	mutex_unlock(&ha->chap_sem);
6030 
6031 	return ret;
6032 }
6033 
6034 
6035 static int qla4xxx_get_boot_target(struct scsi_qla_host *ha,
6036 				   struct ql4_boot_session_info *boot_sess,
6037 				   uint16_t ddb_index)
6038 {
6039 	struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
6040 	struct dev_db_entry *fw_ddb_entry;
6041 	dma_addr_t fw_ddb_entry_dma;
6042 	uint16_t idx;
6043 	uint16_t options;
6044 	int ret = QLA_SUCCESS;
6045 
6046 	fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
6047 					  &fw_ddb_entry_dma, GFP_KERNEL);
6048 	if (!fw_ddb_entry) {
6049 		DEBUG2(ql4_printk(KERN_ERR, ha,
6050 				  "%s: Unable to allocate dma buffer.\n",
6051 				  __func__));
6052 		ret = QLA_ERROR;
6053 		return ret;
6054 	}
6055 
6056 	if (qla4xxx_bootdb_by_index(ha, fw_ddb_entry,
6057 				   fw_ddb_entry_dma, ddb_index)) {
6058 		DEBUG2(ql4_printk(KERN_INFO, ha, "%s: No Flash DDB found at "
6059 				  "index [%d]\n", __func__, ddb_index));
6060 		ret = QLA_ERROR;
6061 		goto exit_boot_target;
6062 	}
6063 
6064 	/* Update target name and IP from DDB */
6065 	memcpy(boot_sess->target_name, fw_ddb_entry->iscsi_name,
6066 	       min(sizeof(boot_sess->target_name),
6067 		   sizeof(fw_ddb_entry->iscsi_name)));
6068 
6069 	options = le16_to_cpu(fw_ddb_entry->options);
6070 	if (options & DDB_OPT_IPV6_DEVICE) {
6071 		memcpy(&boot_conn->dest_ipaddr.ip_address,
6072 		       &fw_ddb_entry->ip_addr[0], IPv6_ADDR_LEN);
6073 	} else {
6074 		boot_conn->dest_ipaddr.ip_type = 0x1;
6075 		memcpy(&boot_conn->dest_ipaddr.ip_address,
6076 		       &fw_ddb_entry->ip_addr[0], IP_ADDR_LEN);
6077 	}
6078 
6079 	boot_conn->dest_port = le16_to_cpu(fw_ddb_entry->port);
6080 
6081 	/* update chap information */
6082 	idx = __le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
6083 
6084 	if (BIT_7 & le16_to_cpu(fw_ddb_entry->iscsi_options))	{
6085 
6086 		DEBUG2(ql4_printk(KERN_INFO, ha, "Setting chap\n"));
6087 
6088 		ret = qla4xxx_get_chap(ha, (char *)&boot_conn->chap.
6089 				       target_chap_name,
6090 				       (char *)&boot_conn->chap.target_secret,
6091 				       idx);
6092 		if (ret) {
6093 			ql4_printk(KERN_ERR, ha, "Failed to set chap\n");
6094 			ret = QLA_ERROR;
6095 			goto exit_boot_target;
6096 		}
6097 
6098 		boot_conn->chap.target_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
6099 		boot_conn->chap.target_secret_length = QL4_CHAP_MAX_SECRET_LEN;
6100 	}
6101 
6102 	if (BIT_4 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
6103 
6104 		DEBUG2(ql4_printk(KERN_INFO, ha, "Setting BIDI chap\n"));
6105 
6106 		ret = qla4xxx_get_bidi_chap(ha,
6107 				    (char *)&boot_conn->chap.intr_chap_name,
6108 				    (char *)&boot_conn->chap.intr_secret);
6109 
6110 		if (ret) {
6111 			ql4_printk(KERN_ERR, ha, "Failed to set BIDI chap\n");
6112 			ret = QLA_ERROR;
6113 			goto exit_boot_target;
6114 		}
6115 
6116 		boot_conn->chap.intr_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
6117 		boot_conn->chap.intr_secret_length = QL4_CHAP_MAX_SECRET_LEN;
6118 	}
6119 
6120 exit_boot_target:
6121 	dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
6122 			  fw_ddb_entry, fw_ddb_entry_dma);
6123 	return ret;
6124 }
6125 
6126 static int qla4xxx_get_boot_info(struct scsi_qla_host *ha)
6127 {
6128 	uint16_t ddb_index[2];
6129 	int ret = QLA_ERROR;
6130 	int rval;
6131 
6132 	memset(ddb_index, 0, sizeof(ddb_index));
6133 	ddb_index[0] = 0xffff;
6134 	ddb_index[1] = 0xffff;
6135 	ret = get_fw_boot_info(ha, ddb_index);
6136 	if (ret != QLA_SUCCESS) {
6137 		DEBUG2(ql4_printk(KERN_INFO, ha,
6138 				"%s: No boot target configured.\n", __func__));
6139 		return ret;
6140 	}
6141 
6142 	if (ql4xdisablesysfsboot)
6143 		return QLA_SUCCESS;
6144 
6145 	if (ddb_index[0] == 0xffff)
6146 		goto sec_target;
6147 
6148 	rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_pri_sess),
6149 				      ddb_index[0]);
6150 	if (rval != QLA_SUCCESS) {
6151 		DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Primary boot target not "
6152 				  "configured\n", __func__));
6153 	} else
6154 		ret = QLA_SUCCESS;
6155 
6156 sec_target:
6157 	if (ddb_index[1] == 0xffff)
6158 		goto exit_get_boot_info;
6159 
6160 	rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_sec_sess),
6161 				      ddb_index[1]);
6162 	if (rval != QLA_SUCCESS) {
6163 		DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Secondary boot target not"
6164 				  " configured\n", __func__));
6165 	} else
6166 		ret = QLA_SUCCESS;
6167 
6168 exit_get_boot_info:
6169 	return ret;
6170 }
6171 
6172 static int qla4xxx_setup_boot_info(struct scsi_qla_host *ha)
6173 {
6174 	struct iscsi_boot_kobj *boot_kobj;
6175 
6176 	if (qla4xxx_get_boot_info(ha) != QLA_SUCCESS)
6177 		return QLA_ERROR;
6178 
6179 	if (ql4xdisablesysfsboot) {
6180 		ql4_printk(KERN_INFO, ha,
6181 			   "%s: syfsboot disabled - driver will trigger login "
6182 			   "and publish session for discovery .\n", __func__);
6183 		return QLA_SUCCESS;
6184 	}
6185 
6186 
6187 	ha->boot_kset = iscsi_boot_create_host_kset(ha->host->host_no);
6188 	if (!ha->boot_kset)
6189 		goto kset_free;
6190 
6191 	if (!scsi_host_get(ha->host))
6192 		goto kset_free;
6193 	boot_kobj = iscsi_boot_create_target(ha->boot_kset, 0, ha,
6194 					     qla4xxx_show_boot_tgt_pri_info,
6195 					     qla4xxx_tgt_get_attr_visibility,
6196 					     qla4xxx_boot_release);
6197 	if (!boot_kobj)
6198 		goto put_host;
6199 
6200 	if (!scsi_host_get(ha->host))
6201 		goto kset_free;
6202 	boot_kobj = iscsi_boot_create_target(ha->boot_kset, 1, ha,
6203 					     qla4xxx_show_boot_tgt_sec_info,
6204 					     qla4xxx_tgt_get_attr_visibility,
6205 					     qla4xxx_boot_release);
6206 	if (!boot_kobj)
6207 		goto put_host;
6208 
6209 	if (!scsi_host_get(ha->host))
6210 		goto kset_free;
6211 	boot_kobj = iscsi_boot_create_initiator(ha->boot_kset, 0, ha,
6212 					       qla4xxx_show_boot_ini_info,
6213 					       qla4xxx_ini_get_attr_visibility,
6214 					       qla4xxx_boot_release);
6215 	if (!boot_kobj)
6216 		goto put_host;
6217 
6218 	if (!scsi_host_get(ha->host))
6219 		goto kset_free;
6220 	boot_kobj = iscsi_boot_create_ethernet(ha->boot_kset, 0, ha,
6221 					       qla4xxx_show_boot_eth_info,
6222 					       qla4xxx_eth_get_attr_visibility,
6223 					       qla4xxx_boot_release);
6224 	if (!boot_kobj)
6225 		goto put_host;
6226 
6227 	return QLA_SUCCESS;
6228 
6229 put_host:
6230 	scsi_host_put(ha->host);
6231 kset_free:
6232 	iscsi_boot_destroy_kset(ha->boot_kset);
6233 	return -ENOMEM;
6234 }
6235 
6236 
6237 static void qla4xxx_get_param_ddb(struct ddb_entry *ddb_entry,
6238 				  struct ql4_tuple_ddb *tddb)
6239 {
6240 	struct scsi_qla_host *ha;
6241 	struct iscsi_cls_session *cls_sess;
6242 	struct iscsi_cls_conn *cls_conn;
6243 	struct iscsi_session *sess;
6244 	struct iscsi_conn *conn;
6245 
6246 	DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
6247 	ha = ddb_entry->ha;
6248 	cls_sess = ddb_entry->sess;
6249 	sess = cls_sess->dd_data;
6250 	cls_conn = ddb_entry->conn;
6251 	conn = cls_conn->dd_data;
6252 
6253 	tddb->tpgt = sess->tpgt;
6254 	tddb->port = conn->persistent_port;
6255 	strlcpy(tddb->iscsi_name, sess->targetname, ISCSI_NAME_SIZE);
6256 	strlcpy(tddb->ip_addr, conn->persistent_address, DDB_IPADDR_LEN);
6257 }
6258 
6259 static void qla4xxx_convert_param_ddb(struct dev_db_entry *fw_ddb_entry,
6260 				      struct ql4_tuple_ddb *tddb,
6261 				      uint8_t *flash_isid)
6262 {
6263 	uint16_t options = 0;
6264 
6265 	tddb->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
6266 	memcpy(&tddb->iscsi_name[0], &fw_ddb_entry->iscsi_name[0],
6267 	       min(sizeof(tddb->iscsi_name), sizeof(fw_ddb_entry->iscsi_name)));
6268 
6269 	options = le16_to_cpu(fw_ddb_entry->options);
6270 	if (options & DDB_OPT_IPV6_DEVICE)
6271 		sprintf(tddb->ip_addr, "%pI6", fw_ddb_entry->ip_addr);
6272 	else
6273 		sprintf(tddb->ip_addr, "%pI4", fw_ddb_entry->ip_addr);
6274 
6275 	tddb->port = le16_to_cpu(fw_ddb_entry->port);
6276 
6277 	if (flash_isid == NULL)
6278 		memcpy(&tddb->isid[0], &fw_ddb_entry->isid[0],
6279 		       sizeof(tddb->isid));
6280 	else
6281 		memcpy(&tddb->isid[0], &flash_isid[0], sizeof(tddb->isid));
6282 }
6283 
6284 static int qla4xxx_compare_tuple_ddb(struct scsi_qla_host *ha,
6285 				     struct ql4_tuple_ddb *old_tddb,
6286 				     struct ql4_tuple_ddb *new_tddb,
6287 				     uint8_t is_isid_compare)
6288 {
6289 	if (strcmp(old_tddb->iscsi_name, new_tddb->iscsi_name))
6290 		return QLA_ERROR;
6291 
6292 	if (strcmp(old_tddb->ip_addr, new_tddb->ip_addr))
6293 		return QLA_ERROR;
6294 
6295 	if (old_tddb->port != new_tddb->port)
6296 		return QLA_ERROR;
6297 
6298 	/* For multi sessions, driver generates the ISID, so do not compare
6299 	 * ISID in reset path since it would be a comparison between the
6300 	 * driver generated ISID and firmware generated ISID. This could
6301 	 * lead to adding duplicated DDBs in the list as driver generated
6302 	 * ISID would not match firmware generated ISID.
6303 	 */
6304 	if (is_isid_compare) {
6305 		DEBUG2(ql4_printk(KERN_INFO, ha,
6306 			"%s: old ISID [%pmR] New ISID [%pmR]\n",
6307 			__func__, old_tddb->isid, new_tddb->isid));
6308 
6309 		if (memcmp(&old_tddb->isid[0], &new_tddb->isid[0],
6310 			   sizeof(old_tddb->isid)))
6311 			return QLA_ERROR;
6312 	}
6313 
6314 	DEBUG2(ql4_printk(KERN_INFO, ha,
6315 			  "Match Found, fw[%d,%d,%s,%s], [%d,%d,%s,%s]",
6316 			  old_tddb->port, old_tddb->tpgt, old_tddb->ip_addr,
6317 			  old_tddb->iscsi_name, new_tddb->port, new_tddb->tpgt,
6318 			  new_tddb->ip_addr, new_tddb->iscsi_name));
6319 
6320 	return QLA_SUCCESS;
6321 }
6322 
6323 static int qla4xxx_is_session_exists(struct scsi_qla_host *ha,
6324 				     struct dev_db_entry *fw_ddb_entry,
6325 				     uint32_t *index)
6326 {
6327 	struct ddb_entry *ddb_entry;
6328 	struct ql4_tuple_ddb *fw_tddb = NULL;
6329 	struct ql4_tuple_ddb *tmp_tddb = NULL;
6330 	int idx;
6331 	int ret = QLA_ERROR;
6332 
6333 	fw_tddb = vzalloc(sizeof(*fw_tddb));
6334 	if (!fw_tddb) {
6335 		DEBUG2(ql4_printk(KERN_WARNING, ha,
6336 				  "Memory Allocation failed.\n"));
6337 		ret = QLA_SUCCESS;
6338 		goto exit_check;
6339 	}
6340 
6341 	tmp_tddb = vzalloc(sizeof(*tmp_tddb));
6342 	if (!tmp_tddb) {
6343 		DEBUG2(ql4_printk(KERN_WARNING, ha,
6344 				  "Memory Allocation failed.\n"));
6345 		ret = QLA_SUCCESS;
6346 		goto exit_check;
6347 	}
6348 
6349 	qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb, NULL);
6350 
6351 	for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
6352 		ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
6353 		if (ddb_entry == NULL)
6354 			continue;
6355 
6356 		qla4xxx_get_param_ddb(ddb_entry, tmp_tddb);
6357 		if (!qla4xxx_compare_tuple_ddb(ha, fw_tddb, tmp_tddb, false)) {
6358 			ret = QLA_SUCCESS; /* found */
6359 			if (index != NULL)
6360 				*index = idx;
6361 			goto exit_check;
6362 		}
6363 	}
6364 
6365 exit_check:
6366 	if (fw_tddb)
6367 		vfree(fw_tddb);
6368 	if (tmp_tddb)
6369 		vfree(tmp_tddb);
6370 	return ret;
6371 }
6372 
6373 /**
6374  * qla4xxx_check_existing_isid - check if target with same isid exist
6375  *				 in target list
6376  * @list_nt: list of target
6377  * @isid: isid to check
6378  *
6379  * This routine return QLA_SUCCESS if target with same isid exist
6380  **/
6381 static int qla4xxx_check_existing_isid(struct list_head *list_nt, uint8_t *isid)
6382 {
6383 	struct qla_ddb_index *nt_ddb_idx, *nt_ddb_idx_tmp;
6384 	struct dev_db_entry *fw_ddb_entry;
6385 
6386 	list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
6387 		fw_ddb_entry = &nt_ddb_idx->fw_ddb;
6388 
6389 		if (memcmp(&fw_ddb_entry->isid[0], &isid[0],
6390 			   sizeof(nt_ddb_idx->fw_ddb.isid)) == 0) {
6391 			return QLA_SUCCESS;
6392 		}
6393 	}
6394 	return QLA_ERROR;
6395 }
6396 
6397 /**
6398  * qla4xxx_update_isid - compare ddbs and updated isid
6399  * @ha: Pointer to host adapter structure.
6400  * @list_nt: list of nt target
6401  * @fw_ddb_entry: firmware ddb entry
6402  *
6403  * This routine update isid if ddbs have same iqn, same isid and
6404  * different IP addr.
6405  * Return QLA_SUCCESS if isid is updated.
6406  **/
6407 static int qla4xxx_update_isid(struct scsi_qla_host *ha,
6408 			       struct list_head *list_nt,
6409 			       struct dev_db_entry *fw_ddb_entry)
6410 {
6411 	uint8_t base_value, i;
6412 
6413 	base_value = fw_ddb_entry->isid[1] & 0x1f;
6414 	for (i = 0; i < 8; i++) {
6415 		fw_ddb_entry->isid[1] = (base_value | (i << 5));
6416 		if (qla4xxx_check_existing_isid(list_nt, fw_ddb_entry->isid))
6417 			break;
6418 	}
6419 
6420 	if (!qla4xxx_check_existing_isid(list_nt, fw_ddb_entry->isid))
6421 		return QLA_ERROR;
6422 
6423 	return QLA_SUCCESS;
6424 }
6425 
6426 /**
6427  * qla4xxx_should_update_isid - check if isid need to update
6428  * @ha: Pointer to host adapter structure.
6429  * @old_tddb: ddb tuple
6430  * @new_tddb: ddb tuple
6431  *
6432  * Return QLA_SUCCESS if different IP, different PORT, same iqn,
6433  * same isid
6434  **/
6435 static int qla4xxx_should_update_isid(struct scsi_qla_host *ha,
6436 				      struct ql4_tuple_ddb *old_tddb,
6437 				      struct ql4_tuple_ddb *new_tddb)
6438 {
6439 	if (strcmp(old_tddb->ip_addr, new_tddb->ip_addr) == 0) {
6440 		/* Same ip */
6441 		if (old_tddb->port == new_tddb->port)
6442 			return QLA_ERROR;
6443 	}
6444 
6445 	if (strcmp(old_tddb->iscsi_name, new_tddb->iscsi_name))
6446 		/* different iqn */
6447 		return QLA_ERROR;
6448 
6449 	if (memcmp(&old_tddb->isid[0], &new_tddb->isid[0],
6450 		   sizeof(old_tddb->isid)))
6451 		/* different isid */
6452 		return QLA_ERROR;
6453 
6454 	return QLA_SUCCESS;
6455 }
6456 
6457 /**
6458  * qla4xxx_is_flash_ddb_exists - check if fw_ddb_entry already exists in list_nt
6459  * @ha: Pointer to host adapter structure.
6460  * @list_nt: list of nt target.
6461  * @fw_ddb_entry: firmware ddb entry.
6462  *
6463  * This routine check if fw_ddb_entry already exists in list_nt to avoid
6464  * duplicate ddb in list_nt.
6465  * Return QLA_SUCCESS if duplicate ddb exit in list_nl.
6466  * Note: This function also update isid of DDB if required.
6467  **/
6468 
6469 static int qla4xxx_is_flash_ddb_exists(struct scsi_qla_host *ha,
6470 				       struct list_head *list_nt,
6471 				       struct dev_db_entry *fw_ddb_entry)
6472 {
6473 	struct qla_ddb_index  *nt_ddb_idx, *nt_ddb_idx_tmp;
6474 	struct ql4_tuple_ddb *fw_tddb = NULL;
6475 	struct ql4_tuple_ddb *tmp_tddb = NULL;
6476 	int rval, ret = QLA_ERROR;
6477 
6478 	fw_tddb = vzalloc(sizeof(*fw_tddb));
6479 	if (!fw_tddb) {
6480 		DEBUG2(ql4_printk(KERN_WARNING, ha,
6481 				  "Memory Allocation failed.\n"));
6482 		ret = QLA_SUCCESS;
6483 		goto exit_check;
6484 	}
6485 
6486 	tmp_tddb = vzalloc(sizeof(*tmp_tddb));
6487 	if (!tmp_tddb) {
6488 		DEBUG2(ql4_printk(KERN_WARNING, ha,
6489 				  "Memory Allocation failed.\n"));
6490 		ret = QLA_SUCCESS;
6491 		goto exit_check;
6492 	}
6493 
6494 	qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb, NULL);
6495 
6496 	list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
6497 		qla4xxx_convert_param_ddb(&nt_ddb_idx->fw_ddb, tmp_tddb,
6498 					  nt_ddb_idx->flash_isid);
6499 		ret = qla4xxx_compare_tuple_ddb(ha, fw_tddb, tmp_tddb, true);
6500 		/* found duplicate ddb */
6501 		if (ret == QLA_SUCCESS)
6502 			goto exit_check;
6503 	}
6504 
6505 	list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
6506 		qla4xxx_convert_param_ddb(&nt_ddb_idx->fw_ddb, tmp_tddb, NULL);
6507 
6508 		ret = qla4xxx_should_update_isid(ha, tmp_tddb, fw_tddb);
6509 		if (ret == QLA_SUCCESS) {
6510 			rval = qla4xxx_update_isid(ha, list_nt, fw_ddb_entry);
6511 			if (rval == QLA_SUCCESS)
6512 				ret = QLA_ERROR;
6513 			else
6514 				ret = QLA_SUCCESS;
6515 
6516 			goto exit_check;
6517 		}
6518 	}
6519 
6520 exit_check:
6521 	if (fw_tddb)
6522 		vfree(fw_tddb);
6523 	if (tmp_tddb)
6524 		vfree(tmp_tddb);
6525 	return ret;
6526 }
6527 
6528 static void qla4xxx_free_ddb_list(struct list_head *list_ddb)
6529 {
6530 	struct qla_ddb_index  *ddb_idx, *ddb_idx_tmp;
6531 
6532 	list_for_each_entry_safe(ddb_idx, ddb_idx_tmp, list_ddb, list) {
6533 		list_del_init(&ddb_idx->list);
6534 		vfree(ddb_idx);
6535 	}
6536 }
6537 
6538 static struct iscsi_endpoint *qla4xxx_get_ep_fwdb(struct scsi_qla_host *ha,
6539 					struct dev_db_entry *fw_ddb_entry)
6540 {
6541 	struct iscsi_endpoint *ep;
6542 	struct sockaddr_in *addr;
6543 	struct sockaddr_in6 *addr6;
6544 	struct sockaddr *t_addr;
6545 	struct sockaddr_storage *dst_addr;
6546 	char *ip;
6547 
6548 	/* TODO: need to destroy on unload iscsi_endpoint*/
6549 	dst_addr = vmalloc(sizeof(*dst_addr));
6550 	if (!dst_addr)
6551 		return NULL;
6552 
6553 	if (fw_ddb_entry->options & DDB_OPT_IPV6_DEVICE) {
6554 		t_addr = (struct sockaddr *)dst_addr;
6555 		t_addr->sa_family = AF_INET6;
6556 		addr6 = (struct sockaddr_in6 *)dst_addr;
6557 		ip = (char *)&addr6->sin6_addr;
6558 		memcpy(ip, fw_ddb_entry->ip_addr, IPv6_ADDR_LEN);
6559 		addr6->sin6_port = htons(le16_to_cpu(fw_ddb_entry->port));
6560 
6561 	} else {
6562 		t_addr = (struct sockaddr *)dst_addr;
6563 		t_addr->sa_family = AF_INET;
6564 		addr = (struct sockaddr_in *)dst_addr;
6565 		ip = (char *)&addr->sin_addr;
6566 		memcpy(ip, fw_ddb_entry->ip_addr, IP_ADDR_LEN);
6567 		addr->sin_port = htons(le16_to_cpu(fw_ddb_entry->port));
6568 	}
6569 
6570 	ep = qla4xxx_ep_connect(ha->host, (struct sockaddr *)dst_addr, 0);
6571 	vfree(dst_addr);
6572 	return ep;
6573 }
6574 
6575 static int qla4xxx_verify_boot_idx(struct scsi_qla_host *ha, uint16_t idx)
6576 {
6577 	if (ql4xdisablesysfsboot)
6578 		return QLA_SUCCESS;
6579 	if (idx == ha->pri_ddb_idx || idx == ha->sec_ddb_idx)
6580 		return QLA_ERROR;
6581 	return QLA_SUCCESS;
6582 }
6583 
6584 static void qla4xxx_setup_flash_ddb_entry(struct scsi_qla_host *ha,
6585 					  struct ddb_entry *ddb_entry,
6586 					  uint16_t idx)
6587 {
6588 	uint16_t def_timeout;
6589 
6590 	ddb_entry->ddb_type = FLASH_DDB;
6591 	ddb_entry->fw_ddb_index = INVALID_ENTRY;
6592 	ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
6593 	ddb_entry->ha = ha;
6594 	ddb_entry->unblock_sess = qla4xxx_unblock_flash_ddb;
6595 	ddb_entry->ddb_change = qla4xxx_flash_ddb_change;
6596 	ddb_entry->chap_tbl_idx = INVALID_ENTRY;
6597 
6598 	atomic_set(&ddb_entry->retry_relogin_timer, INVALID_ENTRY);
6599 	atomic_set(&ddb_entry->relogin_timer, 0);
6600 	atomic_set(&ddb_entry->relogin_retry_count, 0);
6601 	def_timeout = le16_to_cpu(ddb_entry->fw_ddb_entry.def_timeout);
6602 	ddb_entry->default_relogin_timeout =
6603 		(def_timeout > LOGIN_TOV) && (def_timeout < LOGIN_TOV * 10) ?
6604 		def_timeout : LOGIN_TOV;
6605 	ddb_entry->default_time2wait =
6606 		le16_to_cpu(ddb_entry->fw_ddb_entry.iscsi_def_time2wait);
6607 
6608 	if (ql4xdisablesysfsboot &&
6609 	    (idx == ha->pri_ddb_idx || idx == ha->sec_ddb_idx))
6610 		set_bit(DF_BOOT_TGT, &ddb_entry->flags);
6611 }
6612 
6613 static void qla4xxx_wait_for_ip_configuration(struct scsi_qla_host *ha)
6614 {
6615 	uint32_t idx = 0;
6616 	uint32_t ip_idx[IP_ADDR_COUNT] = {0, 1, 2, 3}; /* 4 IP interfaces */
6617 	uint32_t sts[MBOX_REG_COUNT];
6618 	uint32_t ip_state;
6619 	unsigned long wtime;
6620 	int ret;
6621 
6622 	wtime = jiffies + (HZ * IP_CONFIG_TOV);
6623 	do {
6624 		for (idx = 0; idx < IP_ADDR_COUNT; idx++) {
6625 			if (ip_idx[idx] == -1)
6626 				continue;
6627 
6628 			ret = qla4xxx_get_ip_state(ha, 0, ip_idx[idx], sts);
6629 
6630 			if (ret == QLA_ERROR) {
6631 				ip_idx[idx] = -1;
6632 				continue;
6633 			}
6634 
6635 			ip_state = (sts[1] & IP_STATE_MASK) >> IP_STATE_SHIFT;
6636 
6637 			DEBUG2(ql4_printk(KERN_INFO, ha,
6638 					  "Waiting for IP state for idx = %d, state = 0x%x\n",
6639 					  ip_idx[idx], ip_state));
6640 			if (ip_state == IP_ADDRSTATE_UNCONFIGURED ||
6641 			    ip_state == IP_ADDRSTATE_INVALID ||
6642 			    ip_state == IP_ADDRSTATE_PREFERRED ||
6643 			    ip_state == IP_ADDRSTATE_DEPRICATED ||
6644 			    ip_state == IP_ADDRSTATE_DISABLING)
6645 				ip_idx[idx] = -1;
6646 		}
6647 
6648 		/* Break if all IP states checked */
6649 		if ((ip_idx[0] == -1) &&
6650 		    (ip_idx[1] == -1) &&
6651 		    (ip_idx[2] == -1) &&
6652 		    (ip_idx[3] == -1))
6653 			break;
6654 		schedule_timeout_uninterruptible(HZ);
6655 	} while (time_after(wtime, jiffies));
6656 }
6657 
6658 static int qla4xxx_cmp_fw_stentry(struct dev_db_entry *fw_ddb_entry,
6659 				  struct dev_db_entry *flash_ddb_entry)
6660 {
6661 	uint16_t options = 0;
6662 	size_t ip_len = IP_ADDR_LEN;
6663 
6664 	options = le16_to_cpu(fw_ddb_entry->options);
6665 	if (options & DDB_OPT_IPV6_DEVICE)
6666 		ip_len = IPv6_ADDR_LEN;
6667 
6668 	if (memcmp(fw_ddb_entry->ip_addr, flash_ddb_entry->ip_addr, ip_len))
6669 		return QLA_ERROR;
6670 
6671 	if (memcmp(&fw_ddb_entry->isid[0], &flash_ddb_entry->isid[0],
6672 		   sizeof(fw_ddb_entry->isid)))
6673 		return QLA_ERROR;
6674 
6675 	if (memcmp(&fw_ddb_entry->port, &flash_ddb_entry->port,
6676 		   sizeof(fw_ddb_entry->port)))
6677 		return QLA_ERROR;
6678 
6679 	return QLA_SUCCESS;
6680 }
6681 
6682 static int qla4xxx_find_flash_st_idx(struct scsi_qla_host *ha,
6683 				     struct dev_db_entry *fw_ddb_entry,
6684 				     uint32_t fw_idx, uint32_t *flash_index)
6685 {
6686 	struct dev_db_entry *flash_ddb_entry;
6687 	dma_addr_t flash_ddb_entry_dma;
6688 	uint32_t idx = 0;
6689 	int max_ddbs;
6690 	int ret = QLA_ERROR, status;
6691 
6692 	max_ddbs =  is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
6693 				     MAX_DEV_DB_ENTRIES;
6694 
6695 	flash_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
6696 					 &flash_ddb_entry_dma);
6697 	if (flash_ddb_entry == NULL || fw_ddb_entry == NULL) {
6698 		ql4_printk(KERN_ERR, ha, "Out of memory\n");
6699 		goto exit_find_st_idx;
6700 	}
6701 
6702 	status = qla4xxx_flashdb_by_index(ha, flash_ddb_entry,
6703 					  flash_ddb_entry_dma, fw_idx);
6704 	if (status == QLA_SUCCESS) {
6705 		status = qla4xxx_cmp_fw_stentry(fw_ddb_entry, flash_ddb_entry);
6706 		if (status == QLA_SUCCESS) {
6707 			*flash_index = fw_idx;
6708 			ret = QLA_SUCCESS;
6709 			goto exit_find_st_idx;
6710 		}
6711 	}
6712 
6713 	for (idx = 0; idx < max_ddbs; idx++) {
6714 		status = qla4xxx_flashdb_by_index(ha, flash_ddb_entry,
6715 						  flash_ddb_entry_dma, idx);
6716 		if (status == QLA_ERROR)
6717 			continue;
6718 
6719 		status = qla4xxx_cmp_fw_stentry(fw_ddb_entry, flash_ddb_entry);
6720 		if (status == QLA_SUCCESS) {
6721 			*flash_index = idx;
6722 			ret = QLA_SUCCESS;
6723 			goto exit_find_st_idx;
6724 		}
6725 	}
6726 
6727 	if (idx == max_ddbs)
6728 		ql4_printk(KERN_ERR, ha, "Failed to find ST [%d] in flash\n",
6729 			   fw_idx);
6730 
6731 exit_find_st_idx:
6732 	if (flash_ddb_entry)
6733 		dma_pool_free(ha->fw_ddb_dma_pool, flash_ddb_entry,
6734 			      flash_ddb_entry_dma);
6735 
6736 	return ret;
6737 }
6738 
6739 static void qla4xxx_build_st_list(struct scsi_qla_host *ha,
6740 				  struct list_head *list_st)
6741 {
6742 	struct qla_ddb_index  *st_ddb_idx;
6743 	int max_ddbs;
6744 	int fw_idx_size;
6745 	struct dev_db_entry *fw_ddb_entry;
6746 	dma_addr_t fw_ddb_dma;
6747 	int ret;
6748 	uint32_t idx = 0, next_idx = 0;
6749 	uint32_t state = 0, conn_err = 0;
6750 	uint32_t flash_index = -1;
6751 	uint16_t conn_id = 0;
6752 
6753 	fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
6754 				      &fw_ddb_dma);
6755 	if (fw_ddb_entry == NULL) {
6756 		DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
6757 		goto exit_st_list;
6758 	}
6759 
6760 	max_ddbs =  is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
6761 				     MAX_DEV_DB_ENTRIES;
6762 	fw_idx_size = sizeof(struct qla_ddb_index);
6763 
6764 	for (idx = 0; idx < max_ddbs; idx = next_idx) {
6765 		ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry, fw_ddb_dma,
6766 					      NULL, &next_idx, &state,
6767 					      &conn_err, NULL, &conn_id);
6768 		if (ret == QLA_ERROR)
6769 			break;
6770 
6771 		/* Ignore DDB if invalid state (unassigned) */
6772 		if (state == DDB_DS_UNASSIGNED)
6773 			goto continue_next_st;
6774 
6775 		/* Check if ST, add to the list_st */
6776 		if (strlen((char *) fw_ddb_entry->iscsi_name) != 0)
6777 			goto continue_next_st;
6778 
6779 		st_ddb_idx = vzalloc(fw_idx_size);
6780 		if (!st_ddb_idx)
6781 			break;
6782 
6783 		ret = qla4xxx_find_flash_st_idx(ha, fw_ddb_entry, idx,
6784 						&flash_index);
6785 		if (ret == QLA_ERROR) {
6786 			ql4_printk(KERN_ERR, ha,
6787 				   "No flash entry for ST at idx [%d]\n", idx);
6788 			st_ddb_idx->flash_ddb_idx = idx;
6789 		} else {
6790 			ql4_printk(KERN_INFO, ha,
6791 				   "ST at idx [%d] is stored at flash [%d]\n",
6792 				   idx, flash_index);
6793 			st_ddb_idx->flash_ddb_idx = flash_index;
6794 		}
6795 
6796 		st_ddb_idx->fw_ddb_idx = idx;
6797 
6798 		list_add_tail(&st_ddb_idx->list, list_st);
6799 continue_next_st:
6800 		if (next_idx == 0)
6801 			break;
6802 	}
6803 
6804 exit_st_list:
6805 	if (fw_ddb_entry)
6806 		dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
6807 }
6808 
6809 /**
6810  * qla4xxx_remove_failed_ddb - Remove inactive or failed ddb from list
6811  * @ha: pointer to adapter structure
6812  * @list_ddb: List from which failed ddb to be removed
6813  *
6814  * Iterate over the list of DDBs and find and remove DDBs that are either in
6815  * no connection active state or failed state
6816  **/
6817 static void qla4xxx_remove_failed_ddb(struct scsi_qla_host *ha,
6818 				      struct list_head *list_ddb)
6819 {
6820 	struct qla_ddb_index  *ddb_idx, *ddb_idx_tmp;
6821 	uint32_t next_idx = 0;
6822 	uint32_t state = 0, conn_err = 0;
6823 	int ret;
6824 
6825 	list_for_each_entry_safe(ddb_idx, ddb_idx_tmp, list_ddb, list) {
6826 		ret = qla4xxx_get_fwddb_entry(ha, ddb_idx->fw_ddb_idx,
6827 					      NULL, 0, NULL, &next_idx, &state,
6828 					      &conn_err, NULL, NULL);
6829 		if (ret == QLA_ERROR)
6830 			continue;
6831 
6832 		if (state == DDB_DS_NO_CONNECTION_ACTIVE ||
6833 		    state == DDB_DS_SESSION_FAILED) {
6834 			list_del_init(&ddb_idx->list);
6835 			vfree(ddb_idx);
6836 		}
6837 	}
6838 }
6839 
6840 static void qla4xxx_update_sess_disc_idx(struct scsi_qla_host *ha,
6841 					 struct ddb_entry *ddb_entry,
6842 					 struct dev_db_entry *fw_ddb_entry)
6843 {
6844 	struct iscsi_cls_session *cls_sess;
6845 	struct iscsi_session *sess;
6846 	uint32_t max_ddbs = 0;
6847 	uint16_t ddb_link = -1;
6848 
6849 	max_ddbs =  is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
6850 				     MAX_DEV_DB_ENTRIES;
6851 
6852 	cls_sess = ddb_entry->sess;
6853 	sess = cls_sess->dd_data;
6854 
6855 	ddb_link = le16_to_cpu(fw_ddb_entry->ddb_link);
6856 	if (ddb_link < max_ddbs)
6857 		sess->discovery_parent_idx = ddb_link;
6858 	else
6859 		sess->discovery_parent_idx = DDB_NO_LINK;
6860 }
6861 
6862 static int qla4xxx_sess_conn_setup(struct scsi_qla_host *ha,
6863 				   struct dev_db_entry *fw_ddb_entry,
6864 				   int is_reset, uint16_t idx)
6865 {
6866 	struct iscsi_cls_session *cls_sess;
6867 	struct iscsi_session *sess;
6868 	struct iscsi_cls_conn *cls_conn;
6869 	struct iscsi_endpoint *ep;
6870 	uint16_t cmds_max = 32;
6871 	uint16_t conn_id = 0;
6872 	uint32_t initial_cmdsn = 0;
6873 	int ret = QLA_SUCCESS;
6874 
6875 	struct ddb_entry *ddb_entry = NULL;
6876 
6877 	/* Create session object, with INVALID_ENTRY,
6878 	 * the targer_id would get set when we issue the login
6879 	 */
6880 	cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport, ha->host,
6881 				       cmds_max, sizeof(struct ddb_entry),
6882 				       sizeof(struct ql4_task_data),
6883 				       initial_cmdsn, INVALID_ENTRY);
6884 	if (!cls_sess) {
6885 		ret = QLA_ERROR;
6886 		goto exit_setup;
6887 	}
6888 
6889 	/*
6890 	 * so calling module_put function to decrement the
6891 	 * reference count.
6892 	 **/
6893 	module_put(qla4xxx_iscsi_transport.owner);
6894 	sess = cls_sess->dd_data;
6895 	ddb_entry = sess->dd_data;
6896 	ddb_entry->sess = cls_sess;
6897 
6898 	cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
6899 	memcpy(&ddb_entry->fw_ddb_entry, fw_ddb_entry,
6900 	       sizeof(struct dev_db_entry));
6901 
6902 	qla4xxx_setup_flash_ddb_entry(ha, ddb_entry, idx);
6903 
6904 	cls_conn = iscsi_conn_setup(cls_sess, sizeof(struct qla_conn), conn_id);
6905 
6906 	if (!cls_conn) {
6907 		ret = QLA_ERROR;
6908 		goto exit_setup;
6909 	}
6910 
6911 	ddb_entry->conn = cls_conn;
6912 
6913 	/* Setup ep, for displaying attributes in sysfs */
6914 	ep = qla4xxx_get_ep_fwdb(ha, fw_ddb_entry);
6915 	if (ep) {
6916 		ep->conn = cls_conn;
6917 		cls_conn->ep = ep;
6918 	} else {
6919 		DEBUG2(ql4_printk(KERN_ERR, ha, "Unable to get ep\n"));
6920 		ret = QLA_ERROR;
6921 		goto exit_setup;
6922 	}
6923 
6924 	/* Update sess/conn params */
6925 	qla4xxx_copy_fwddb_param(ha, fw_ddb_entry, cls_sess, cls_conn);
6926 	qla4xxx_update_sess_disc_idx(ha, ddb_entry, fw_ddb_entry);
6927 
6928 	if (is_reset == RESET_ADAPTER) {
6929 		iscsi_block_session(cls_sess);
6930 		/* Use the relogin path to discover new devices
6931 		 *  by short-circuting the logic of setting
6932 		 *  timer to relogin - instead set the flags
6933 		 *  to initiate login right away.
6934 		 */
6935 		set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
6936 		set_bit(DF_RELOGIN, &ddb_entry->flags);
6937 	}
6938 
6939 exit_setup:
6940 	return ret;
6941 }
6942 
6943 static void qla4xxx_update_fw_ddb_link(struct scsi_qla_host *ha,
6944 				       struct list_head *list_ddb,
6945 				       struct dev_db_entry *fw_ddb_entry)
6946 {
6947 	struct qla_ddb_index  *ddb_idx, *ddb_idx_tmp;
6948 	uint16_t ddb_link;
6949 
6950 	ddb_link = le16_to_cpu(fw_ddb_entry->ddb_link);
6951 
6952 	list_for_each_entry_safe(ddb_idx, ddb_idx_tmp, list_ddb, list) {
6953 		if (ddb_idx->fw_ddb_idx == ddb_link) {
6954 			DEBUG2(ql4_printk(KERN_INFO, ha,
6955 					  "Updating NT parent idx from [%d] to [%d]\n",
6956 					  ddb_link, ddb_idx->flash_ddb_idx));
6957 			fw_ddb_entry->ddb_link =
6958 					    cpu_to_le16(ddb_idx->flash_ddb_idx);
6959 			return;
6960 		}
6961 	}
6962 }
6963 
6964 static void qla4xxx_build_nt_list(struct scsi_qla_host *ha,
6965 				  struct list_head *list_nt,
6966 				  struct list_head *list_st,
6967 				  int is_reset)
6968 {
6969 	struct dev_db_entry *fw_ddb_entry;
6970 	struct ddb_entry *ddb_entry = NULL;
6971 	dma_addr_t fw_ddb_dma;
6972 	int max_ddbs;
6973 	int fw_idx_size;
6974 	int ret;
6975 	uint32_t idx = 0, next_idx = 0;
6976 	uint32_t state = 0, conn_err = 0;
6977 	uint32_t ddb_idx = -1;
6978 	uint16_t conn_id = 0;
6979 	uint16_t ddb_link = -1;
6980 	struct qla_ddb_index  *nt_ddb_idx;
6981 
6982 	fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
6983 				      &fw_ddb_dma);
6984 	if (fw_ddb_entry == NULL) {
6985 		DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
6986 		goto exit_nt_list;
6987 	}
6988 	max_ddbs =  is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
6989 				     MAX_DEV_DB_ENTRIES;
6990 	fw_idx_size = sizeof(struct qla_ddb_index);
6991 
6992 	for (idx = 0; idx < max_ddbs; idx = next_idx) {
6993 		ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry, fw_ddb_dma,
6994 					      NULL, &next_idx, &state,
6995 					      &conn_err, NULL, &conn_id);
6996 		if (ret == QLA_ERROR)
6997 			break;
6998 
6999 		if (qla4xxx_verify_boot_idx(ha, idx) != QLA_SUCCESS)
7000 			goto continue_next_nt;
7001 
7002 		/* Check if NT, then add to list it */
7003 		if (strlen((char *) fw_ddb_entry->iscsi_name) == 0)
7004 			goto continue_next_nt;
7005 
7006 		ddb_link = le16_to_cpu(fw_ddb_entry->ddb_link);
7007 		if (ddb_link < max_ddbs)
7008 			qla4xxx_update_fw_ddb_link(ha, list_st, fw_ddb_entry);
7009 
7010 		if (!(state == DDB_DS_NO_CONNECTION_ACTIVE ||
7011 		    state == DDB_DS_SESSION_FAILED) &&
7012 		    (is_reset == INIT_ADAPTER))
7013 			goto continue_next_nt;
7014 
7015 		DEBUG2(ql4_printk(KERN_INFO, ha,
7016 				  "Adding  DDB to session = 0x%x\n", idx));
7017 
7018 		if (is_reset == INIT_ADAPTER) {
7019 			nt_ddb_idx = vmalloc(fw_idx_size);
7020 			if (!nt_ddb_idx)
7021 				break;
7022 
7023 			nt_ddb_idx->fw_ddb_idx = idx;
7024 
7025 			/* Copy original isid as it may get updated in function
7026 			 * qla4xxx_update_isid(). We need original isid in
7027 			 * function qla4xxx_compare_tuple_ddb to find duplicate
7028 			 * target */
7029 			memcpy(&nt_ddb_idx->flash_isid[0],
7030 			       &fw_ddb_entry->isid[0],
7031 			       sizeof(nt_ddb_idx->flash_isid));
7032 
7033 			ret = qla4xxx_is_flash_ddb_exists(ha, list_nt,
7034 							  fw_ddb_entry);
7035 			if (ret == QLA_SUCCESS) {
7036 				/* free nt_ddb_idx and do not add to list_nt */
7037 				vfree(nt_ddb_idx);
7038 				goto continue_next_nt;
7039 			}
7040 
7041 			/* Copy updated isid */
7042 			memcpy(&nt_ddb_idx->fw_ddb, fw_ddb_entry,
7043 			       sizeof(struct dev_db_entry));
7044 
7045 			list_add_tail(&nt_ddb_idx->list, list_nt);
7046 		} else if (is_reset == RESET_ADAPTER) {
7047 			ret = qla4xxx_is_session_exists(ha, fw_ddb_entry,
7048 							&ddb_idx);
7049 			if (ret == QLA_SUCCESS) {
7050 				ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha,
7051 								       ddb_idx);
7052 				if (ddb_entry != NULL)
7053 					qla4xxx_update_sess_disc_idx(ha,
7054 								     ddb_entry,
7055 								  fw_ddb_entry);
7056 				goto continue_next_nt;
7057 			}
7058 		}
7059 
7060 		ret = qla4xxx_sess_conn_setup(ha, fw_ddb_entry, is_reset, idx);
7061 		if (ret == QLA_ERROR)
7062 			goto exit_nt_list;
7063 
7064 continue_next_nt:
7065 		if (next_idx == 0)
7066 			break;
7067 	}
7068 
7069 exit_nt_list:
7070 	if (fw_ddb_entry)
7071 		dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
7072 }
7073 
7074 static void qla4xxx_build_new_nt_list(struct scsi_qla_host *ha,
7075 				      struct list_head *list_nt,
7076 				      uint16_t target_id)
7077 {
7078 	struct dev_db_entry *fw_ddb_entry;
7079 	dma_addr_t fw_ddb_dma;
7080 	int max_ddbs;
7081 	int fw_idx_size;
7082 	int ret;
7083 	uint32_t idx = 0, next_idx = 0;
7084 	uint32_t state = 0, conn_err = 0;
7085 	uint16_t conn_id = 0;
7086 	struct qla_ddb_index  *nt_ddb_idx;
7087 
7088 	fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
7089 				      &fw_ddb_dma);
7090 	if (fw_ddb_entry == NULL) {
7091 		DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
7092 		goto exit_new_nt_list;
7093 	}
7094 	max_ddbs =  is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
7095 				     MAX_DEV_DB_ENTRIES;
7096 	fw_idx_size = sizeof(struct qla_ddb_index);
7097 
7098 	for (idx = 0; idx < max_ddbs; idx = next_idx) {
7099 		ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry, fw_ddb_dma,
7100 					      NULL, &next_idx, &state,
7101 					      &conn_err, NULL, &conn_id);
7102 		if (ret == QLA_ERROR)
7103 			break;
7104 
7105 		/* Check if NT, then add it to list */
7106 		if (strlen((char *)fw_ddb_entry->iscsi_name) == 0)
7107 			goto continue_next_new_nt;
7108 
7109 		if (!(state == DDB_DS_NO_CONNECTION_ACTIVE))
7110 			goto continue_next_new_nt;
7111 
7112 		DEBUG2(ql4_printk(KERN_INFO, ha,
7113 				  "Adding  DDB to session = 0x%x\n", idx));
7114 
7115 		nt_ddb_idx = vmalloc(fw_idx_size);
7116 		if (!nt_ddb_idx)
7117 			break;
7118 
7119 		nt_ddb_idx->fw_ddb_idx = idx;
7120 
7121 		ret = qla4xxx_is_session_exists(ha, fw_ddb_entry, NULL);
7122 		if (ret == QLA_SUCCESS) {
7123 			/* free nt_ddb_idx and do not add to list_nt */
7124 			vfree(nt_ddb_idx);
7125 			goto continue_next_new_nt;
7126 		}
7127 
7128 		if (target_id < max_ddbs)
7129 			fw_ddb_entry->ddb_link = cpu_to_le16(target_id);
7130 
7131 		list_add_tail(&nt_ddb_idx->list, list_nt);
7132 
7133 		ret = qla4xxx_sess_conn_setup(ha, fw_ddb_entry, RESET_ADAPTER,
7134 					      idx);
7135 		if (ret == QLA_ERROR)
7136 			goto exit_new_nt_list;
7137 
7138 continue_next_new_nt:
7139 		if (next_idx == 0)
7140 			break;
7141 	}
7142 
7143 exit_new_nt_list:
7144 	if (fw_ddb_entry)
7145 		dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
7146 }
7147 
7148 /**
7149  * qla4xxx_sysfs_ddb_is_non_persistent - check for non-persistence of ddb entry
7150  * @dev: dev associated with the sysfs entry
7151  * @data: pointer to flashnode session object
7152  *
7153  * Returns:
7154  *	1: if flashnode entry is non-persistent
7155  *	0: if flashnode entry is persistent
7156  **/
7157 static int qla4xxx_sysfs_ddb_is_non_persistent(struct device *dev, void *data)
7158 {
7159 	struct iscsi_bus_flash_session *fnode_sess;
7160 
7161 	if (!iscsi_flashnode_bus_match(dev, NULL))
7162 		return 0;
7163 
7164 	fnode_sess = iscsi_dev_to_flash_session(dev);
7165 
7166 	return (fnode_sess->flash_state == DEV_DB_NON_PERSISTENT);
7167 }
7168 
7169 /**
7170  * qla4xxx_sysfs_ddb_tgt_create - Create sysfs entry for target
7171  * @ha: pointer to host
7172  * @fw_ddb_entry: flash ddb data
7173  * @idx: target index
7174  * @user: if set then this call is made from userland else from kernel
7175  *
7176  * Returns:
7177  * On sucess: QLA_SUCCESS
7178  * On failure: QLA_ERROR
7179  *
7180  * This create separate sysfs entries for session and connection attributes of
7181  * the given fw ddb entry.
7182  * If this is invoked as a result of a userspace call then the entry is marked
7183  * as nonpersistent using flash_state field.
7184  **/
7185 static int qla4xxx_sysfs_ddb_tgt_create(struct scsi_qla_host *ha,
7186 					struct dev_db_entry *fw_ddb_entry,
7187 					uint16_t *idx, int user)
7188 {
7189 	struct iscsi_bus_flash_session *fnode_sess = NULL;
7190 	struct iscsi_bus_flash_conn *fnode_conn = NULL;
7191 	int rc = QLA_ERROR;
7192 
7193 	fnode_sess = iscsi_create_flashnode_sess(ha->host, *idx,
7194 						 &qla4xxx_iscsi_transport, 0);
7195 	if (!fnode_sess) {
7196 		ql4_printk(KERN_ERR, ha,
7197 			   "%s: Unable to create session sysfs entry for flashnode %d of host%lu\n",
7198 			   __func__, *idx, ha->host_no);
7199 		goto exit_tgt_create;
7200 	}
7201 
7202 	fnode_conn = iscsi_create_flashnode_conn(ha->host, fnode_sess,
7203 						 &qla4xxx_iscsi_transport, 0);
7204 	if (!fnode_conn) {
7205 		ql4_printk(KERN_ERR, ha,
7206 			   "%s: Unable to create conn sysfs entry for flashnode %d of host%lu\n",
7207 			   __func__, *idx, ha->host_no);
7208 		goto free_sess;
7209 	}
7210 
7211 	if (user) {
7212 		fnode_sess->flash_state = DEV_DB_NON_PERSISTENT;
7213 	} else {
7214 		fnode_sess->flash_state = DEV_DB_PERSISTENT;
7215 
7216 		if (*idx == ha->pri_ddb_idx || *idx == ha->sec_ddb_idx)
7217 			fnode_sess->is_boot_target = 1;
7218 		else
7219 			fnode_sess->is_boot_target = 0;
7220 	}
7221 
7222 	rc = qla4xxx_copy_from_fwddb_param(fnode_sess, fnode_conn,
7223 					   fw_ddb_entry);
7224 
7225 	ql4_printk(KERN_INFO, ha, "%s: sysfs entry %s created\n",
7226 		   __func__, fnode_sess->dev.kobj.name);
7227 
7228 	ql4_printk(KERN_INFO, ha, "%s: sysfs entry %s created\n",
7229 		   __func__, fnode_conn->dev.kobj.name);
7230 
7231 	return QLA_SUCCESS;
7232 
7233 free_sess:
7234 	iscsi_destroy_flashnode_sess(fnode_sess);
7235 
7236 exit_tgt_create:
7237 	return QLA_ERROR;
7238 }
7239 
7240 /**
7241  * qla4xxx_sysfs_ddb_add - Add new ddb entry in flash
7242  * @shost: pointer to host
7243  * @buf: type of ddb entry (ipv4/ipv6)
7244  * @len: length of buf
7245  *
7246  * This creates new ddb entry in the flash by finding first free index and
7247  * storing default ddb there. And then create sysfs entry for the new ddb entry.
7248  **/
7249 static int qla4xxx_sysfs_ddb_add(struct Scsi_Host *shost, const char *buf,
7250 				 int len)
7251 {
7252 	struct scsi_qla_host *ha = to_qla_host(shost);
7253 	struct dev_db_entry *fw_ddb_entry = NULL;
7254 	dma_addr_t fw_ddb_entry_dma;
7255 	struct device *dev;
7256 	uint16_t idx = 0;
7257 	uint16_t max_ddbs = 0;
7258 	uint32_t options = 0;
7259 	uint32_t rval = QLA_ERROR;
7260 
7261 	if (strncasecmp(PORTAL_TYPE_IPV4, buf, 4) &&
7262 	    strncasecmp(PORTAL_TYPE_IPV6, buf, 4)) {
7263 		DEBUG2(ql4_printk(KERN_ERR, ha, "%s: Invalid portal type\n",
7264 				  __func__));
7265 		goto exit_ddb_add;
7266 	}
7267 
7268 	max_ddbs =  is_qla40XX(ha) ? MAX_PRST_DEV_DB_ENTRIES :
7269 				     MAX_DEV_DB_ENTRIES;
7270 
7271 	fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
7272 					  &fw_ddb_entry_dma, GFP_KERNEL);
7273 	if (!fw_ddb_entry) {
7274 		DEBUG2(ql4_printk(KERN_ERR, ha,
7275 				  "%s: Unable to allocate dma buffer\n",
7276 				  __func__));
7277 		goto exit_ddb_add;
7278 	}
7279 
7280 	dev = iscsi_find_flashnode_sess(ha->host, NULL,
7281 					qla4xxx_sysfs_ddb_is_non_persistent);
7282 	if (dev) {
7283 		ql4_printk(KERN_ERR, ha,
7284 			   "%s: A non-persistent entry %s found\n",
7285 			   __func__, dev->kobj.name);
7286 		put_device(dev);
7287 		goto exit_ddb_add;
7288 	}
7289 
7290 	/* Index 0 and 1 are reserved for boot target entries */
7291 	for (idx = 2; idx < max_ddbs; idx++) {
7292 		if (qla4xxx_flashdb_by_index(ha, fw_ddb_entry,
7293 					     fw_ddb_entry_dma, idx))
7294 			break;
7295 	}
7296 
7297 	if (idx == max_ddbs)
7298 		goto exit_ddb_add;
7299 
7300 	if (!strncasecmp("ipv6", buf, 4))
7301 		options |= IPV6_DEFAULT_DDB_ENTRY;
7302 
7303 	rval = qla4xxx_get_default_ddb(ha, options, fw_ddb_entry_dma);
7304 	if (rval == QLA_ERROR)
7305 		goto exit_ddb_add;
7306 
7307 	rval = qla4xxx_sysfs_ddb_tgt_create(ha, fw_ddb_entry, &idx, 1);
7308 
7309 exit_ddb_add:
7310 	if (fw_ddb_entry)
7311 		dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
7312 				  fw_ddb_entry, fw_ddb_entry_dma);
7313 	if (rval == QLA_SUCCESS)
7314 		return idx;
7315 	else
7316 		return -EIO;
7317 }
7318 
7319 /**
7320  * qla4xxx_sysfs_ddb_apply - write the target ddb contents to Flash
7321  * @fnode_sess: pointer to session attrs of flash ddb entry
7322  * @fnode_conn: pointer to connection attrs of flash ddb entry
7323  *
7324  * This writes the contents of target ddb buffer to Flash with a valid cookie
7325  * value in order to make the ddb entry persistent.
7326  **/
7327 static int  qla4xxx_sysfs_ddb_apply(struct iscsi_bus_flash_session *fnode_sess,
7328 				    struct iscsi_bus_flash_conn *fnode_conn)
7329 {
7330 	struct Scsi_Host *shost = iscsi_flash_session_to_shost(fnode_sess);
7331 	struct scsi_qla_host *ha = to_qla_host(shost);
7332 	uint32_t dev_db_start_offset = FLASH_OFFSET_DB_INFO;
7333 	struct dev_db_entry *fw_ddb_entry = NULL;
7334 	dma_addr_t fw_ddb_entry_dma;
7335 	uint32_t options = 0;
7336 	int rval = 0;
7337 
7338 	fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
7339 					  &fw_ddb_entry_dma, GFP_KERNEL);
7340 	if (!fw_ddb_entry) {
7341 		DEBUG2(ql4_printk(KERN_ERR, ha,
7342 				  "%s: Unable to allocate dma buffer\n",
7343 				  __func__));
7344 		rval = -ENOMEM;
7345 		goto exit_ddb_apply;
7346 	}
7347 
7348 	if (!strncasecmp(fnode_sess->portal_type, PORTAL_TYPE_IPV6, 4))
7349 		options |= IPV6_DEFAULT_DDB_ENTRY;
7350 
7351 	rval = qla4xxx_get_default_ddb(ha, options, fw_ddb_entry_dma);
7352 	if (rval == QLA_ERROR)
7353 		goto exit_ddb_apply;
7354 
7355 	dev_db_start_offset += (fnode_sess->target_id *
7356 				sizeof(*fw_ddb_entry));
7357 
7358 	qla4xxx_copy_to_fwddb_param(fnode_sess, fnode_conn, fw_ddb_entry);
7359 	fw_ddb_entry->cookie = DDB_VALID_COOKIE;
7360 
7361 	rval = qla4xxx_set_flash(ha, fw_ddb_entry_dma, dev_db_start_offset,
7362 				 sizeof(*fw_ddb_entry), FLASH_OPT_RMW_COMMIT);
7363 
7364 	if (rval == QLA_SUCCESS) {
7365 		fnode_sess->flash_state = DEV_DB_PERSISTENT;
7366 		ql4_printk(KERN_INFO, ha,
7367 			   "%s: flash node %u of host %lu written to flash\n",
7368 			   __func__, fnode_sess->target_id, ha->host_no);
7369 	} else {
7370 		rval = -EIO;
7371 		ql4_printk(KERN_ERR, ha,
7372 			   "%s: Error while writing flash node %u of host %lu to flash\n",
7373 			   __func__, fnode_sess->target_id, ha->host_no);
7374 	}
7375 
7376 exit_ddb_apply:
7377 	if (fw_ddb_entry)
7378 		dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
7379 				  fw_ddb_entry, fw_ddb_entry_dma);
7380 	return rval;
7381 }
7382 
7383 static ssize_t qla4xxx_sysfs_ddb_conn_open(struct scsi_qla_host *ha,
7384 					   struct dev_db_entry *fw_ddb_entry,
7385 					   uint16_t idx)
7386 {
7387 	struct dev_db_entry *ddb_entry = NULL;
7388 	dma_addr_t ddb_entry_dma;
7389 	unsigned long wtime;
7390 	uint32_t mbx_sts = 0;
7391 	uint32_t state = 0, conn_err = 0;
7392 	uint16_t tmo = 0;
7393 	int ret = 0;
7394 
7395 	ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*ddb_entry),
7396 				       &ddb_entry_dma, GFP_KERNEL);
7397 	if (!ddb_entry) {
7398 		DEBUG2(ql4_printk(KERN_ERR, ha,
7399 				  "%s: Unable to allocate dma buffer\n",
7400 				  __func__));
7401 		return QLA_ERROR;
7402 	}
7403 
7404 	memcpy(ddb_entry, fw_ddb_entry, sizeof(*ddb_entry));
7405 
7406 	ret = qla4xxx_set_ddb_entry(ha, idx, ddb_entry_dma, &mbx_sts);
7407 	if (ret != QLA_SUCCESS) {
7408 		DEBUG2(ql4_printk(KERN_ERR, ha,
7409 				  "%s: Unable to set ddb entry for index %d\n",
7410 				  __func__, idx));
7411 		goto exit_ddb_conn_open;
7412 	}
7413 
7414 	qla4xxx_conn_open(ha, idx);
7415 
7416 	/* To ensure that sendtargets is done, wait for at least 12 secs */
7417 	tmo = ((ha->def_timeout > LOGIN_TOV) &&
7418 	       (ha->def_timeout < LOGIN_TOV * 10) ?
7419 	       ha->def_timeout : LOGIN_TOV);
7420 
7421 	DEBUG2(ql4_printk(KERN_INFO, ha,
7422 			  "Default time to wait for login to ddb %d\n", tmo));
7423 
7424 	wtime = jiffies + (HZ * tmo);
7425 	do {
7426 		ret = qla4xxx_get_fwddb_entry(ha, idx, NULL, 0, NULL,
7427 					      NULL, &state, &conn_err, NULL,
7428 					      NULL);
7429 		if (ret == QLA_ERROR)
7430 			continue;
7431 
7432 		if (state == DDB_DS_NO_CONNECTION_ACTIVE ||
7433 		    state == DDB_DS_SESSION_FAILED)
7434 			break;
7435 
7436 		schedule_timeout_uninterruptible(HZ / 10);
7437 	} while (time_after(wtime, jiffies));
7438 
7439 exit_ddb_conn_open:
7440 	if (ddb_entry)
7441 		dma_free_coherent(&ha->pdev->dev, sizeof(*ddb_entry),
7442 				  ddb_entry, ddb_entry_dma);
7443 	return ret;
7444 }
7445 
7446 static int qla4xxx_ddb_login_st(struct scsi_qla_host *ha,
7447 				struct dev_db_entry *fw_ddb_entry,
7448 				uint16_t target_id)
7449 {
7450 	struct qla_ddb_index *ddb_idx, *ddb_idx_tmp;
7451 	struct list_head list_nt;
7452 	uint16_t ddb_index;
7453 	int ret = 0;
7454 
7455 	if (test_bit(AF_ST_DISCOVERY_IN_PROGRESS, &ha->flags)) {
7456 		ql4_printk(KERN_WARNING, ha,
7457 			   "%s: A discovery already in progress!\n", __func__);
7458 		return QLA_ERROR;
7459 	}
7460 
7461 	INIT_LIST_HEAD(&list_nt);
7462 
7463 	set_bit(AF_ST_DISCOVERY_IN_PROGRESS, &ha->flags);
7464 
7465 	ret = qla4xxx_get_ddb_index(ha, &ddb_index);
7466 	if (ret == QLA_ERROR)
7467 		goto exit_login_st_clr_bit;
7468 
7469 	ret = qla4xxx_sysfs_ddb_conn_open(ha, fw_ddb_entry, ddb_index);
7470 	if (ret == QLA_ERROR)
7471 		goto exit_login_st;
7472 
7473 	qla4xxx_build_new_nt_list(ha, &list_nt, target_id);
7474 
7475 	list_for_each_entry_safe(ddb_idx, ddb_idx_tmp, &list_nt, list) {
7476 		list_del_init(&ddb_idx->list);
7477 		qla4xxx_clear_ddb_entry(ha, ddb_idx->fw_ddb_idx);
7478 		vfree(ddb_idx);
7479 	}
7480 
7481 exit_login_st:
7482 	if (qla4xxx_clear_ddb_entry(ha, ddb_index) == QLA_ERROR) {
7483 		ql4_printk(KERN_ERR, ha,
7484 			   "Unable to clear DDB index = 0x%x\n", ddb_index);
7485 	}
7486 
7487 	clear_bit(ddb_index, ha->ddb_idx_map);
7488 
7489 exit_login_st_clr_bit:
7490 	clear_bit(AF_ST_DISCOVERY_IN_PROGRESS, &ha->flags);
7491 	return ret;
7492 }
7493 
7494 static int qla4xxx_ddb_login_nt(struct scsi_qla_host *ha,
7495 				struct dev_db_entry *fw_ddb_entry,
7496 				uint16_t idx)
7497 {
7498 	int ret = QLA_ERROR;
7499 
7500 	ret = qla4xxx_is_session_exists(ha, fw_ddb_entry, NULL);
7501 	if (ret != QLA_SUCCESS)
7502 		ret = qla4xxx_sess_conn_setup(ha, fw_ddb_entry, RESET_ADAPTER,
7503 					      idx);
7504 	else
7505 		ret = -EPERM;
7506 
7507 	return ret;
7508 }
7509 
7510 /**
7511  * qla4xxx_sysfs_ddb_login - Login to the specified target
7512  * @fnode_sess: pointer to session attrs of flash ddb entry
7513  * @fnode_conn: pointer to connection attrs of flash ddb entry
7514  *
7515  * This logs in to the specified target
7516  **/
7517 static int qla4xxx_sysfs_ddb_login(struct iscsi_bus_flash_session *fnode_sess,
7518 				   struct iscsi_bus_flash_conn *fnode_conn)
7519 {
7520 	struct Scsi_Host *shost = iscsi_flash_session_to_shost(fnode_sess);
7521 	struct scsi_qla_host *ha = to_qla_host(shost);
7522 	struct dev_db_entry *fw_ddb_entry = NULL;
7523 	dma_addr_t fw_ddb_entry_dma;
7524 	uint32_t options = 0;
7525 	int ret = 0;
7526 
7527 	if (fnode_sess->flash_state == DEV_DB_NON_PERSISTENT) {
7528 		ql4_printk(KERN_ERR, ha,
7529 			   "%s: Target info is not persistent\n", __func__);
7530 		ret = -EIO;
7531 		goto exit_ddb_login;
7532 	}
7533 
7534 	fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
7535 					  &fw_ddb_entry_dma, GFP_KERNEL);
7536 	if (!fw_ddb_entry) {
7537 		DEBUG2(ql4_printk(KERN_ERR, ha,
7538 				  "%s: Unable to allocate dma buffer\n",
7539 				  __func__));
7540 		ret = -ENOMEM;
7541 		goto exit_ddb_login;
7542 	}
7543 
7544 	if (!strncasecmp(fnode_sess->portal_type, PORTAL_TYPE_IPV6, 4))
7545 		options |= IPV6_DEFAULT_DDB_ENTRY;
7546 
7547 	ret = qla4xxx_get_default_ddb(ha, options, fw_ddb_entry_dma);
7548 	if (ret == QLA_ERROR)
7549 		goto exit_ddb_login;
7550 
7551 	qla4xxx_copy_to_fwddb_param(fnode_sess, fnode_conn, fw_ddb_entry);
7552 	fw_ddb_entry->cookie = DDB_VALID_COOKIE;
7553 
7554 	if (strlen((char *)fw_ddb_entry->iscsi_name) == 0)
7555 		ret = qla4xxx_ddb_login_st(ha, fw_ddb_entry,
7556 					   fnode_sess->target_id);
7557 	else
7558 		ret = qla4xxx_ddb_login_nt(ha, fw_ddb_entry,
7559 					   fnode_sess->target_id);
7560 
7561 	if (ret > 0)
7562 		ret = -EIO;
7563 
7564 exit_ddb_login:
7565 	if (fw_ddb_entry)
7566 		dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
7567 				  fw_ddb_entry, fw_ddb_entry_dma);
7568 	return ret;
7569 }
7570 
7571 /**
7572  * qla4xxx_sysfs_ddb_logout_sid - Logout session for the specified target
7573  * @cls_sess: pointer to session to be logged out
7574  *
7575  * This performs session log out from the specified target
7576  **/
7577 static int qla4xxx_sysfs_ddb_logout_sid(struct iscsi_cls_session *cls_sess)
7578 {
7579 	struct iscsi_session *sess;
7580 	struct ddb_entry *ddb_entry = NULL;
7581 	struct scsi_qla_host *ha;
7582 	struct dev_db_entry *fw_ddb_entry = NULL;
7583 	dma_addr_t fw_ddb_entry_dma;
7584 	unsigned long flags;
7585 	unsigned long wtime;
7586 	uint32_t ddb_state;
7587 	int options;
7588 	int ret = 0;
7589 
7590 	sess = cls_sess->dd_data;
7591 	ddb_entry = sess->dd_data;
7592 	ha = ddb_entry->ha;
7593 
7594 	if (ddb_entry->ddb_type != FLASH_DDB) {
7595 		ql4_printk(KERN_ERR, ha, "%s: Not a flash node session\n",
7596 			   __func__);
7597 		ret = -ENXIO;
7598 		goto exit_ddb_logout;
7599 	}
7600 
7601 	if (test_bit(DF_BOOT_TGT, &ddb_entry->flags)) {
7602 		ql4_printk(KERN_ERR, ha,
7603 			   "%s: Logout from boot target entry is not permitted.\n",
7604 			   __func__);
7605 		ret = -EPERM;
7606 		goto exit_ddb_logout;
7607 	}
7608 
7609 	fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
7610 					  &fw_ddb_entry_dma, GFP_KERNEL);
7611 	if (!fw_ddb_entry) {
7612 		ql4_printk(KERN_ERR, ha,
7613 			   "%s: Unable to allocate dma buffer\n", __func__);
7614 		ret = -ENOMEM;
7615 		goto exit_ddb_logout;
7616 	}
7617 
7618 	if (test_and_set_bit(DF_DISABLE_RELOGIN, &ddb_entry->flags))
7619 		goto ddb_logout_init;
7620 
7621 	ret = qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index,
7622 				      fw_ddb_entry, fw_ddb_entry_dma,
7623 				      NULL, NULL, &ddb_state, NULL,
7624 				      NULL, NULL);
7625 	if (ret == QLA_ERROR)
7626 		goto ddb_logout_init;
7627 
7628 	if (ddb_state == DDB_DS_SESSION_ACTIVE)
7629 		goto ddb_logout_init;
7630 
7631 	/* wait until next relogin is triggered using DF_RELOGIN and
7632 	 * clear DF_RELOGIN to avoid invocation of further relogin
7633 	 */
7634 	wtime = jiffies + (HZ * RELOGIN_TOV);
7635 	do {
7636 		if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags))
7637 			goto ddb_logout_init;
7638 
7639 		schedule_timeout_uninterruptible(HZ);
7640 	} while ((time_after(wtime, jiffies)));
7641 
7642 ddb_logout_init:
7643 	atomic_set(&ddb_entry->retry_relogin_timer, INVALID_ENTRY);
7644 	atomic_set(&ddb_entry->relogin_timer, 0);
7645 
7646 	options = LOGOUT_OPTION_CLOSE_SESSION;
7647 	qla4xxx_session_logout_ddb(ha, ddb_entry, options);
7648 
7649 	memset(fw_ddb_entry, 0, sizeof(*fw_ddb_entry));
7650 	wtime = jiffies + (HZ * LOGOUT_TOV);
7651 	do {
7652 		ret = qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index,
7653 					      fw_ddb_entry, fw_ddb_entry_dma,
7654 					      NULL, NULL, &ddb_state, NULL,
7655 					      NULL, NULL);
7656 		if (ret == QLA_ERROR)
7657 			goto ddb_logout_clr_sess;
7658 
7659 		if ((ddb_state == DDB_DS_NO_CONNECTION_ACTIVE) ||
7660 		    (ddb_state == DDB_DS_SESSION_FAILED))
7661 			goto ddb_logout_clr_sess;
7662 
7663 		schedule_timeout_uninterruptible(HZ);
7664 	} while ((time_after(wtime, jiffies)));
7665 
7666 ddb_logout_clr_sess:
7667 	qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
7668 	/*
7669 	 * we have decremented the reference count of the driver
7670 	 * when we setup the session to have the driver unload
7671 	 * to be seamless without actually destroying the
7672 	 * session
7673 	 **/
7674 	try_module_get(qla4xxx_iscsi_transport.owner);
7675 	iscsi_destroy_endpoint(ddb_entry->conn->ep);
7676 
7677 	spin_lock_irqsave(&ha->hardware_lock, flags);
7678 	qla4xxx_free_ddb(ha, ddb_entry);
7679 	clear_bit(ddb_entry->fw_ddb_index, ha->ddb_idx_map);
7680 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
7681 
7682 	iscsi_session_teardown(ddb_entry->sess);
7683 
7684 	clear_bit(DF_DISABLE_RELOGIN, &ddb_entry->flags);
7685 	ret = QLA_SUCCESS;
7686 
7687 exit_ddb_logout:
7688 	if (fw_ddb_entry)
7689 		dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
7690 				  fw_ddb_entry, fw_ddb_entry_dma);
7691 	return ret;
7692 }
7693 
7694 /**
7695  * qla4xxx_sysfs_ddb_logout - Logout from the specified target
7696  * @fnode_sess: pointer to session attrs of flash ddb entry
7697  * @fnode_conn: pointer to connection attrs of flash ddb entry
7698  *
7699  * This performs log out from the specified target
7700  **/
7701 static int qla4xxx_sysfs_ddb_logout(struct iscsi_bus_flash_session *fnode_sess,
7702 				    struct iscsi_bus_flash_conn *fnode_conn)
7703 {
7704 	struct Scsi_Host *shost = iscsi_flash_session_to_shost(fnode_sess);
7705 	struct scsi_qla_host *ha = to_qla_host(shost);
7706 	struct ql4_tuple_ddb *flash_tddb = NULL;
7707 	struct ql4_tuple_ddb *tmp_tddb = NULL;
7708 	struct dev_db_entry *fw_ddb_entry = NULL;
7709 	struct ddb_entry *ddb_entry = NULL;
7710 	dma_addr_t fw_ddb_dma;
7711 	uint32_t next_idx = 0;
7712 	uint32_t state = 0, conn_err = 0;
7713 	uint16_t conn_id = 0;
7714 	int idx, index;
7715 	int status, ret = 0;
7716 
7717 	fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
7718 				      &fw_ddb_dma);
7719 	if (fw_ddb_entry == NULL) {
7720 		ql4_printk(KERN_ERR, ha, "%s:Out of memory\n", __func__);
7721 		ret = -ENOMEM;
7722 		goto exit_ddb_logout;
7723 	}
7724 
7725 	flash_tddb = vzalloc(sizeof(*flash_tddb));
7726 	if (!flash_tddb) {
7727 		ql4_printk(KERN_WARNING, ha,
7728 			   "%s:Memory Allocation failed.\n", __func__);
7729 		ret = -ENOMEM;
7730 		goto exit_ddb_logout;
7731 	}
7732 
7733 	tmp_tddb = vzalloc(sizeof(*tmp_tddb));
7734 	if (!tmp_tddb) {
7735 		ql4_printk(KERN_WARNING, ha,
7736 			   "%s:Memory Allocation failed.\n", __func__);
7737 		ret = -ENOMEM;
7738 		goto exit_ddb_logout;
7739 	}
7740 
7741 	if (!fnode_sess->targetname) {
7742 		ql4_printk(KERN_ERR, ha,
7743 			   "%s:Cannot logout from SendTarget entry\n",
7744 			   __func__);
7745 		ret = -EPERM;
7746 		goto exit_ddb_logout;
7747 	}
7748 
7749 	if (fnode_sess->is_boot_target) {
7750 		ql4_printk(KERN_ERR, ha,
7751 			   "%s: Logout from boot target entry is not permitted.\n",
7752 			   __func__);
7753 		ret = -EPERM;
7754 		goto exit_ddb_logout;
7755 	}
7756 
7757 	strlcpy(flash_tddb->iscsi_name, fnode_sess->targetname,
7758 		ISCSI_NAME_SIZE);
7759 
7760 	if (!strncmp(fnode_sess->portal_type, PORTAL_TYPE_IPV6, 4))
7761 		sprintf(flash_tddb->ip_addr, "%pI6", fnode_conn->ipaddress);
7762 	else
7763 		sprintf(flash_tddb->ip_addr, "%pI4", fnode_conn->ipaddress);
7764 
7765 	flash_tddb->tpgt = fnode_sess->tpgt;
7766 	flash_tddb->port = fnode_conn->port;
7767 
7768 	COPY_ISID(flash_tddb->isid, fnode_sess->isid);
7769 
7770 	for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
7771 		ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
7772 		if (ddb_entry == NULL)
7773 			continue;
7774 
7775 		if (ddb_entry->ddb_type != FLASH_DDB)
7776 			continue;
7777 
7778 		index = ddb_entry->sess->target_id;
7779 		status = qla4xxx_get_fwddb_entry(ha, index, fw_ddb_entry,
7780 						 fw_ddb_dma, NULL, &next_idx,
7781 						 &state, &conn_err, NULL,
7782 						 &conn_id);
7783 		if (status == QLA_ERROR) {
7784 			ret = -ENOMEM;
7785 			break;
7786 		}
7787 
7788 		qla4xxx_convert_param_ddb(fw_ddb_entry, tmp_tddb, NULL);
7789 
7790 		status = qla4xxx_compare_tuple_ddb(ha, flash_tddb, tmp_tddb,
7791 						   true);
7792 		if (status == QLA_SUCCESS) {
7793 			ret = qla4xxx_sysfs_ddb_logout_sid(ddb_entry->sess);
7794 			break;
7795 		}
7796 	}
7797 
7798 	if (idx == MAX_DDB_ENTRIES)
7799 		ret = -ESRCH;
7800 
7801 exit_ddb_logout:
7802 	if (flash_tddb)
7803 		vfree(flash_tddb);
7804 	if (tmp_tddb)
7805 		vfree(tmp_tddb);
7806 	if (fw_ddb_entry)
7807 		dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
7808 
7809 	return ret;
7810 }
7811 
7812 static int
7813 qla4xxx_sysfs_ddb_get_param(struct iscsi_bus_flash_session *fnode_sess,
7814 			    int param, char *buf)
7815 {
7816 	struct Scsi_Host *shost = iscsi_flash_session_to_shost(fnode_sess);
7817 	struct scsi_qla_host *ha = to_qla_host(shost);
7818 	struct iscsi_bus_flash_conn *fnode_conn;
7819 	struct ql4_chap_table chap_tbl;
7820 	struct device *dev;
7821 	int parent_type;
7822 	int rc = 0;
7823 
7824 	dev = iscsi_find_flashnode_conn(fnode_sess);
7825 	if (!dev)
7826 		return -EIO;
7827 
7828 	fnode_conn = iscsi_dev_to_flash_conn(dev);
7829 
7830 	switch (param) {
7831 	case ISCSI_FLASHNODE_IS_FW_ASSIGNED_IPV6:
7832 		rc = sprintf(buf, "%u\n", fnode_conn->is_fw_assigned_ipv6);
7833 		break;
7834 	case ISCSI_FLASHNODE_PORTAL_TYPE:
7835 		rc = sprintf(buf, "%s\n", fnode_sess->portal_type);
7836 		break;
7837 	case ISCSI_FLASHNODE_AUTO_SND_TGT_DISABLE:
7838 		rc = sprintf(buf, "%u\n", fnode_sess->auto_snd_tgt_disable);
7839 		break;
7840 	case ISCSI_FLASHNODE_DISCOVERY_SESS:
7841 		rc = sprintf(buf, "%u\n", fnode_sess->discovery_sess);
7842 		break;
7843 	case ISCSI_FLASHNODE_ENTRY_EN:
7844 		rc = sprintf(buf, "%u\n", fnode_sess->entry_state);
7845 		break;
7846 	case ISCSI_FLASHNODE_HDR_DGST_EN:
7847 		rc = sprintf(buf, "%u\n", fnode_conn->hdrdgst_en);
7848 		break;
7849 	case ISCSI_FLASHNODE_DATA_DGST_EN:
7850 		rc = sprintf(buf, "%u\n", fnode_conn->datadgst_en);
7851 		break;
7852 	case ISCSI_FLASHNODE_IMM_DATA_EN:
7853 		rc = sprintf(buf, "%u\n", fnode_sess->imm_data_en);
7854 		break;
7855 	case ISCSI_FLASHNODE_INITIAL_R2T_EN:
7856 		rc = sprintf(buf, "%u\n", fnode_sess->initial_r2t_en);
7857 		break;
7858 	case ISCSI_FLASHNODE_DATASEQ_INORDER:
7859 		rc = sprintf(buf, "%u\n", fnode_sess->dataseq_inorder_en);
7860 		break;
7861 	case ISCSI_FLASHNODE_PDU_INORDER:
7862 		rc = sprintf(buf, "%u\n", fnode_sess->pdu_inorder_en);
7863 		break;
7864 	case ISCSI_FLASHNODE_CHAP_AUTH_EN:
7865 		rc = sprintf(buf, "%u\n", fnode_sess->chap_auth_en);
7866 		break;
7867 	case ISCSI_FLASHNODE_SNACK_REQ_EN:
7868 		rc = sprintf(buf, "%u\n", fnode_conn->snack_req_en);
7869 		break;
7870 	case ISCSI_FLASHNODE_DISCOVERY_LOGOUT_EN:
7871 		rc = sprintf(buf, "%u\n", fnode_sess->discovery_logout_en);
7872 		break;
7873 	case ISCSI_FLASHNODE_BIDI_CHAP_EN:
7874 		rc = sprintf(buf, "%u\n", fnode_sess->bidi_chap_en);
7875 		break;
7876 	case ISCSI_FLASHNODE_DISCOVERY_AUTH_OPTIONAL:
7877 		rc = sprintf(buf, "%u\n", fnode_sess->discovery_auth_optional);
7878 		break;
7879 	case ISCSI_FLASHNODE_ERL:
7880 		rc = sprintf(buf, "%u\n", fnode_sess->erl);
7881 		break;
7882 	case ISCSI_FLASHNODE_TCP_TIMESTAMP_STAT:
7883 		rc = sprintf(buf, "%u\n", fnode_conn->tcp_timestamp_stat);
7884 		break;
7885 	case ISCSI_FLASHNODE_TCP_NAGLE_DISABLE:
7886 		rc = sprintf(buf, "%u\n", fnode_conn->tcp_nagle_disable);
7887 		break;
7888 	case ISCSI_FLASHNODE_TCP_WSF_DISABLE:
7889 		rc = sprintf(buf, "%u\n", fnode_conn->tcp_wsf_disable);
7890 		break;
7891 	case ISCSI_FLASHNODE_TCP_TIMER_SCALE:
7892 		rc = sprintf(buf, "%u\n", fnode_conn->tcp_timer_scale);
7893 		break;
7894 	case ISCSI_FLASHNODE_TCP_TIMESTAMP_EN:
7895 		rc = sprintf(buf, "%u\n", fnode_conn->tcp_timestamp_en);
7896 		break;
7897 	case ISCSI_FLASHNODE_IP_FRAG_DISABLE:
7898 		rc = sprintf(buf, "%u\n", fnode_conn->fragment_disable);
7899 		break;
7900 	case ISCSI_FLASHNODE_MAX_RECV_DLENGTH:
7901 		rc = sprintf(buf, "%u\n", fnode_conn->max_recv_dlength);
7902 		break;
7903 	case ISCSI_FLASHNODE_MAX_XMIT_DLENGTH:
7904 		rc = sprintf(buf, "%u\n", fnode_conn->max_xmit_dlength);
7905 		break;
7906 	case ISCSI_FLASHNODE_FIRST_BURST:
7907 		rc = sprintf(buf, "%u\n", fnode_sess->first_burst);
7908 		break;
7909 	case ISCSI_FLASHNODE_DEF_TIME2WAIT:
7910 		rc = sprintf(buf, "%u\n", fnode_sess->time2wait);
7911 		break;
7912 	case ISCSI_FLASHNODE_DEF_TIME2RETAIN:
7913 		rc = sprintf(buf, "%u\n", fnode_sess->time2retain);
7914 		break;
7915 	case ISCSI_FLASHNODE_MAX_R2T:
7916 		rc = sprintf(buf, "%u\n", fnode_sess->max_r2t);
7917 		break;
7918 	case ISCSI_FLASHNODE_KEEPALIVE_TMO:
7919 		rc = sprintf(buf, "%u\n", fnode_conn->keepalive_timeout);
7920 		break;
7921 	case ISCSI_FLASHNODE_ISID:
7922 		rc = sprintf(buf, "%pm\n", fnode_sess->isid);
7923 		break;
7924 	case ISCSI_FLASHNODE_TSID:
7925 		rc = sprintf(buf, "%u\n", fnode_sess->tsid);
7926 		break;
7927 	case ISCSI_FLASHNODE_PORT:
7928 		rc = sprintf(buf, "%d\n", fnode_conn->port);
7929 		break;
7930 	case ISCSI_FLASHNODE_MAX_BURST:
7931 		rc = sprintf(buf, "%u\n", fnode_sess->max_burst);
7932 		break;
7933 	case ISCSI_FLASHNODE_DEF_TASKMGMT_TMO:
7934 		rc = sprintf(buf, "%u\n",
7935 			     fnode_sess->default_taskmgmt_timeout);
7936 		break;
7937 	case ISCSI_FLASHNODE_IPADDR:
7938 		if (!strncmp(fnode_sess->portal_type, PORTAL_TYPE_IPV6, 4))
7939 			rc = sprintf(buf, "%pI6\n", fnode_conn->ipaddress);
7940 		else
7941 			rc = sprintf(buf, "%pI4\n", fnode_conn->ipaddress);
7942 		break;
7943 	case ISCSI_FLASHNODE_ALIAS:
7944 		if (fnode_sess->targetalias)
7945 			rc = sprintf(buf, "%s\n", fnode_sess->targetalias);
7946 		else
7947 			rc = sprintf(buf, "\n");
7948 		break;
7949 	case ISCSI_FLASHNODE_REDIRECT_IPADDR:
7950 		if (!strncmp(fnode_sess->portal_type, PORTAL_TYPE_IPV6, 4))
7951 			rc = sprintf(buf, "%pI6\n",
7952 				     fnode_conn->redirect_ipaddr);
7953 		else
7954 			rc = sprintf(buf, "%pI4\n",
7955 				     fnode_conn->redirect_ipaddr);
7956 		break;
7957 	case ISCSI_FLASHNODE_MAX_SEGMENT_SIZE:
7958 		rc = sprintf(buf, "%u\n", fnode_conn->max_segment_size);
7959 		break;
7960 	case ISCSI_FLASHNODE_LOCAL_PORT:
7961 		rc = sprintf(buf, "%u\n", fnode_conn->local_port);
7962 		break;
7963 	case ISCSI_FLASHNODE_IPV4_TOS:
7964 		rc = sprintf(buf, "%u\n", fnode_conn->ipv4_tos);
7965 		break;
7966 	case ISCSI_FLASHNODE_IPV6_TC:
7967 		if (!strncmp(fnode_sess->portal_type, PORTAL_TYPE_IPV6, 4))
7968 			rc = sprintf(buf, "%u\n",
7969 				     fnode_conn->ipv6_traffic_class);
7970 		else
7971 			rc = sprintf(buf, "\n");
7972 		break;
7973 	case ISCSI_FLASHNODE_IPV6_FLOW_LABEL:
7974 		rc = sprintf(buf, "%u\n", fnode_conn->ipv6_flow_label);
7975 		break;
7976 	case ISCSI_FLASHNODE_LINK_LOCAL_IPV6:
7977 		if (!strncmp(fnode_sess->portal_type, PORTAL_TYPE_IPV6, 4))
7978 			rc = sprintf(buf, "%pI6\n",
7979 				     fnode_conn->link_local_ipv6_addr);
7980 		else
7981 			rc = sprintf(buf, "\n");
7982 		break;
7983 	case ISCSI_FLASHNODE_DISCOVERY_PARENT_IDX:
7984 		rc = sprintf(buf, "%u\n", fnode_sess->discovery_parent_idx);
7985 		break;
7986 	case ISCSI_FLASHNODE_DISCOVERY_PARENT_TYPE:
7987 		if (fnode_sess->discovery_parent_type == DDB_ISNS)
7988 			parent_type = ISCSI_DISC_PARENT_ISNS;
7989 		else if (fnode_sess->discovery_parent_type == DDB_NO_LINK)
7990 			parent_type = ISCSI_DISC_PARENT_UNKNOWN;
7991 		else if (fnode_sess->discovery_parent_type < MAX_DDB_ENTRIES)
7992 			parent_type = ISCSI_DISC_PARENT_SENDTGT;
7993 		else
7994 			parent_type = ISCSI_DISC_PARENT_UNKNOWN;
7995 
7996 		rc = sprintf(buf, "%s\n",
7997 			     iscsi_get_discovery_parent_name(parent_type));
7998 		break;
7999 	case ISCSI_FLASHNODE_NAME:
8000 		if (fnode_sess->targetname)
8001 			rc = sprintf(buf, "%s\n", fnode_sess->targetname);
8002 		else
8003 			rc = sprintf(buf, "\n");
8004 		break;
8005 	case ISCSI_FLASHNODE_TPGT:
8006 		rc = sprintf(buf, "%u\n", fnode_sess->tpgt);
8007 		break;
8008 	case ISCSI_FLASHNODE_TCP_XMIT_WSF:
8009 		rc = sprintf(buf, "%u\n", fnode_conn->tcp_xmit_wsf);
8010 		break;
8011 	case ISCSI_FLASHNODE_TCP_RECV_WSF:
8012 		rc = sprintf(buf, "%u\n", fnode_conn->tcp_recv_wsf);
8013 		break;
8014 	case ISCSI_FLASHNODE_CHAP_OUT_IDX:
8015 		rc = sprintf(buf, "%u\n", fnode_sess->chap_out_idx);
8016 		break;
8017 	case ISCSI_FLASHNODE_USERNAME:
8018 		if (fnode_sess->chap_auth_en) {
8019 			qla4xxx_get_uni_chap_at_index(ha,
8020 						      chap_tbl.name,
8021 						      chap_tbl.secret,
8022 						      fnode_sess->chap_out_idx);
8023 			rc = sprintf(buf, "%s\n", chap_tbl.name);
8024 		} else {
8025 			rc = sprintf(buf, "\n");
8026 		}
8027 		break;
8028 	case ISCSI_FLASHNODE_PASSWORD:
8029 		if (fnode_sess->chap_auth_en) {
8030 			qla4xxx_get_uni_chap_at_index(ha,
8031 						      chap_tbl.name,
8032 						      chap_tbl.secret,
8033 						      fnode_sess->chap_out_idx);
8034 			rc = sprintf(buf, "%s\n", chap_tbl.secret);
8035 		} else {
8036 			rc = sprintf(buf, "\n");
8037 		}
8038 		break;
8039 	case ISCSI_FLASHNODE_STATSN:
8040 		rc = sprintf(buf, "%u\n", fnode_conn->statsn);
8041 		break;
8042 	case ISCSI_FLASHNODE_EXP_STATSN:
8043 		rc = sprintf(buf, "%u\n", fnode_conn->exp_statsn);
8044 		break;
8045 	case ISCSI_FLASHNODE_IS_BOOT_TGT:
8046 		rc = sprintf(buf, "%u\n", fnode_sess->is_boot_target);
8047 		break;
8048 	default:
8049 		rc = -ENOSYS;
8050 		break;
8051 	}
8052 
8053 	put_device(dev);
8054 	return rc;
8055 }
8056 
8057 /**
8058  * qla4xxx_sysfs_ddb_set_param - Set parameter for firmware DDB entry
8059  * @fnode_sess: pointer to session attrs of flash ddb entry
8060  * @fnode_conn: pointer to connection attrs of flash ddb entry
8061  * @data: Parameters and their values to update
8062  * @len: len of data
8063  *
8064  * This sets the parameter of flash ddb entry and writes them to flash
8065  **/
8066 static int
8067 qla4xxx_sysfs_ddb_set_param(struct iscsi_bus_flash_session *fnode_sess,
8068 			    struct iscsi_bus_flash_conn *fnode_conn,
8069 			    void *data, int len)
8070 {
8071 	struct Scsi_Host *shost = iscsi_flash_session_to_shost(fnode_sess);
8072 	struct scsi_qla_host *ha = to_qla_host(shost);
8073 	struct iscsi_flashnode_param_info *fnode_param;
8074 	struct ql4_chap_table chap_tbl;
8075 	struct nlattr *attr;
8076 	uint16_t chap_out_idx = INVALID_ENTRY;
8077 	int rc = QLA_ERROR;
8078 	uint32_t rem = len;
8079 
8080 	memset((void *)&chap_tbl, 0, sizeof(chap_tbl));
8081 	nla_for_each_attr(attr, data, len, rem) {
8082 		fnode_param = nla_data(attr);
8083 
8084 		switch (fnode_param->param) {
8085 		case ISCSI_FLASHNODE_IS_FW_ASSIGNED_IPV6:
8086 			fnode_conn->is_fw_assigned_ipv6 = fnode_param->value[0];
8087 			break;
8088 		case ISCSI_FLASHNODE_PORTAL_TYPE:
8089 			memcpy(fnode_sess->portal_type, fnode_param->value,
8090 			       strlen(fnode_sess->portal_type));
8091 			break;
8092 		case ISCSI_FLASHNODE_AUTO_SND_TGT_DISABLE:
8093 			fnode_sess->auto_snd_tgt_disable =
8094 							fnode_param->value[0];
8095 			break;
8096 		case ISCSI_FLASHNODE_DISCOVERY_SESS:
8097 			fnode_sess->discovery_sess = fnode_param->value[0];
8098 			break;
8099 		case ISCSI_FLASHNODE_ENTRY_EN:
8100 			fnode_sess->entry_state = fnode_param->value[0];
8101 			break;
8102 		case ISCSI_FLASHNODE_HDR_DGST_EN:
8103 			fnode_conn->hdrdgst_en = fnode_param->value[0];
8104 			break;
8105 		case ISCSI_FLASHNODE_DATA_DGST_EN:
8106 			fnode_conn->datadgst_en = fnode_param->value[0];
8107 			break;
8108 		case ISCSI_FLASHNODE_IMM_DATA_EN:
8109 			fnode_sess->imm_data_en = fnode_param->value[0];
8110 			break;
8111 		case ISCSI_FLASHNODE_INITIAL_R2T_EN:
8112 			fnode_sess->initial_r2t_en = fnode_param->value[0];
8113 			break;
8114 		case ISCSI_FLASHNODE_DATASEQ_INORDER:
8115 			fnode_sess->dataseq_inorder_en = fnode_param->value[0];
8116 			break;
8117 		case ISCSI_FLASHNODE_PDU_INORDER:
8118 			fnode_sess->pdu_inorder_en = fnode_param->value[0];
8119 			break;
8120 		case ISCSI_FLASHNODE_CHAP_AUTH_EN:
8121 			fnode_sess->chap_auth_en = fnode_param->value[0];
8122 			/* Invalidate chap index if chap auth is disabled */
8123 			if (!fnode_sess->chap_auth_en)
8124 				fnode_sess->chap_out_idx = INVALID_ENTRY;
8125 
8126 			break;
8127 		case ISCSI_FLASHNODE_SNACK_REQ_EN:
8128 			fnode_conn->snack_req_en = fnode_param->value[0];
8129 			break;
8130 		case ISCSI_FLASHNODE_DISCOVERY_LOGOUT_EN:
8131 			fnode_sess->discovery_logout_en = fnode_param->value[0];
8132 			break;
8133 		case ISCSI_FLASHNODE_BIDI_CHAP_EN:
8134 			fnode_sess->bidi_chap_en = fnode_param->value[0];
8135 			break;
8136 		case ISCSI_FLASHNODE_DISCOVERY_AUTH_OPTIONAL:
8137 			fnode_sess->discovery_auth_optional =
8138 							fnode_param->value[0];
8139 			break;
8140 		case ISCSI_FLASHNODE_ERL:
8141 			fnode_sess->erl = fnode_param->value[0];
8142 			break;
8143 		case ISCSI_FLASHNODE_TCP_TIMESTAMP_STAT:
8144 			fnode_conn->tcp_timestamp_stat = fnode_param->value[0];
8145 			break;
8146 		case ISCSI_FLASHNODE_TCP_NAGLE_DISABLE:
8147 			fnode_conn->tcp_nagle_disable = fnode_param->value[0];
8148 			break;
8149 		case ISCSI_FLASHNODE_TCP_WSF_DISABLE:
8150 			fnode_conn->tcp_wsf_disable = fnode_param->value[0];
8151 			break;
8152 		case ISCSI_FLASHNODE_TCP_TIMER_SCALE:
8153 			fnode_conn->tcp_timer_scale = fnode_param->value[0];
8154 			break;
8155 		case ISCSI_FLASHNODE_TCP_TIMESTAMP_EN:
8156 			fnode_conn->tcp_timestamp_en = fnode_param->value[0];
8157 			break;
8158 		case ISCSI_FLASHNODE_IP_FRAG_DISABLE:
8159 			fnode_conn->fragment_disable = fnode_param->value[0];
8160 			break;
8161 		case ISCSI_FLASHNODE_MAX_RECV_DLENGTH:
8162 			fnode_conn->max_recv_dlength =
8163 					*(unsigned *)fnode_param->value;
8164 			break;
8165 		case ISCSI_FLASHNODE_MAX_XMIT_DLENGTH:
8166 			fnode_conn->max_xmit_dlength =
8167 					*(unsigned *)fnode_param->value;
8168 			break;
8169 		case ISCSI_FLASHNODE_FIRST_BURST:
8170 			fnode_sess->first_burst =
8171 					*(unsigned *)fnode_param->value;
8172 			break;
8173 		case ISCSI_FLASHNODE_DEF_TIME2WAIT:
8174 			fnode_sess->time2wait = *(uint16_t *)fnode_param->value;
8175 			break;
8176 		case ISCSI_FLASHNODE_DEF_TIME2RETAIN:
8177 			fnode_sess->time2retain =
8178 						*(uint16_t *)fnode_param->value;
8179 			break;
8180 		case ISCSI_FLASHNODE_MAX_R2T:
8181 			fnode_sess->max_r2t =
8182 					*(uint16_t *)fnode_param->value;
8183 			break;
8184 		case ISCSI_FLASHNODE_KEEPALIVE_TMO:
8185 			fnode_conn->keepalive_timeout =
8186 				*(uint16_t *)fnode_param->value;
8187 			break;
8188 		case ISCSI_FLASHNODE_ISID:
8189 			memcpy(fnode_sess->isid, fnode_param->value,
8190 			       sizeof(fnode_sess->isid));
8191 			break;
8192 		case ISCSI_FLASHNODE_TSID:
8193 			fnode_sess->tsid = *(uint16_t *)fnode_param->value;
8194 			break;
8195 		case ISCSI_FLASHNODE_PORT:
8196 			fnode_conn->port = *(uint16_t *)fnode_param->value;
8197 			break;
8198 		case ISCSI_FLASHNODE_MAX_BURST:
8199 			fnode_sess->max_burst = *(unsigned *)fnode_param->value;
8200 			break;
8201 		case ISCSI_FLASHNODE_DEF_TASKMGMT_TMO:
8202 			fnode_sess->default_taskmgmt_timeout =
8203 						*(uint16_t *)fnode_param->value;
8204 			break;
8205 		case ISCSI_FLASHNODE_IPADDR:
8206 			memcpy(fnode_conn->ipaddress, fnode_param->value,
8207 			       IPv6_ADDR_LEN);
8208 			break;
8209 		case ISCSI_FLASHNODE_ALIAS:
8210 			rc = iscsi_switch_str_param(&fnode_sess->targetalias,
8211 						    (char *)fnode_param->value);
8212 			break;
8213 		case ISCSI_FLASHNODE_REDIRECT_IPADDR:
8214 			memcpy(fnode_conn->redirect_ipaddr, fnode_param->value,
8215 			       IPv6_ADDR_LEN);
8216 			break;
8217 		case ISCSI_FLASHNODE_MAX_SEGMENT_SIZE:
8218 			fnode_conn->max_segment_size =
8219 					*(unsigned *)fnode_param->value;
8220 			break;
8221 		case ISCSI_FLASHNODE_LOCAL_PORT:
8222 			fnode_conn->local_port =
8223 						*(uint16_t *)fnode_param->value;
8224 			break;
8225 		case ISCSI_FLASHNODE_IPV4_TOS:
8226 			fnode_conn->ipv4_tos = fnode_param->value[0];
8227 			break;
8228 		case ISCSI_FLASHNODE_IPV6_TC:
8229 			fnode_conn->ipv6_traffic_class = fnode_param->value[0];
8230 			break;
8231 		case ISCSI_FLASHNODE_IPV6_FLOW_LABEL:
8232 			fnode_conn->ipv6_flow_label = fnode_param->value[0];
8233 			break;
8234 		case ISCSI_FLASHNODE_NAME:
8235 			rc = iscsi_switch_str_param(&fnode_sess->targetname,
8236 						    (char *)fnode_param->value);
8237 			break;
8238 		case ISCSI_FLASHNODE_TPGT:
8239 			fnode_sess->tpgt = *(uint16_t *)fnode_param->value;
8240 			break;
8241 		case ISCSI_FLASHNODE_LINK_LOCAL_IPV6:
8242 			memcpy(fnode_conn->link_local_ipv6_addr,
8243 			       fnode_param->value, IPv6_ADDR_LEN);
8244 			break;
8245 		case ISCSI_FLASHNODE_DISCOVERY_PARENT_IDX:
8246 			fnode_sess->discovery_parent_idx =
8247 						*(uint16_t *)fnode_param->value;
8248 			break;
8249 		case ISCSI_FLASHNODE_TCP_XMIT_WSF:
8250 			fnode_conn->tcp_xmit_wsf =
8251 						*(uint8_t *)fnode_param->value;
8252 			break;
8253 		case ISCSI_FLASHNODE_TCP_RECV_WSF:
8254 			fnode_conn->tcp_recv_wsf =
8255 						*(uint8_t *)fnode_param->value;
8256 			break;
8257 		case ISCSI_FLASHNODE_STATSN:
8258 			fnode_conn->statsn = *(uint32_t *)fnode_param->value;
8259 			break;
8260 		case ISCSI_FLASHNODE_EXP_STATSN:
8261 			fnode_conn->exp_statsn =
8262 						*(uint32_t *)fnode_param->value;
8263 			break;
8264 		case ISCSI_FLASHNODE_CHAP_OUT_IDX:
8265 			chap_out_idx = *(uint16_t *)fnode_param->value;
8266 			if (!qla4xxx_get_uni_chap_at_index(ha,
8267 							   chap_tbl.name,
8268 							   chap_tbl.secret,
8269 							   chap_out_idx)) {
8270 				fnode_sess->chap_out_idx = chap_out_idx;
8271 				/* Enable chap auth if chap index is valid */
8272 				fnode_sess->chap_auth_en = QL4_PARAM_ENABLE;
8273 			}
8274 			break;
8275 		default:
8276 			ql4_printk(KERN_ERR, ha,
8277 				   "%s: No such sysfs attribute\n", __func__);
8278 			rc = -ENOSYS;
8279 			goto exit_set_param;
8280 		}
8281 	}
8282 
8283 	rc = qla4xxx_sysfs_ddb_apply(fnode_sess, fnode_conn);
8284 
8285 exit_set_param:
8286 	return rc;
8287 }
8288 
8289 /**
8290  * qla4xxx_sysfs_ddb_delete - Delete firmware DDB entry
8291  * @fnode_sess: pointer to session attrs of flash ddb entry
8292  *
8293  * This invalidates the flash ddb entry at the given index
8294  **/
8295 static int qla4xxx_sysfs_ddb_delete(struct iscsi_bus_flash_session *fnode_sess)
8296 {
8297 	struct Scsi_Host *shost = iscsi_flash_session_to_shost(fnode_sess);
8298 	struct scsi_qla_host *ha = to_qla_host(shost);
8299 	uint32_t dev_db_start_offset;
8300 	uint32_t dev_db_end_offset;
8301 	struct dev_db_entry *fw_ddb_entry = NULL;
8302 	dma_addr_t fw_ddb_entry_dma;
8303 	uint16_t *ddb_cookie = NULL;
8304 	size_t ddb_size = 0;
8305 	void *pddb = NULL;
8306 	int target_id;
8307 	int rc = 0;
8308 
8309 	if (fnode_sess->is_boot_target) {
8310 		rc = -EPERM;
8311 		DEBUG2(ql4_printk(KERN_ERR, ha,
8312 				  "%s: Deletion of boot target entry is not permitted.\n",
8313 				  __func__));
8314 		goto exit_ddb_del;
8315 	}
8316 
8317 	if (fnode_sess->flash_state == DEV_DB_NON_PERSISTENT)
8318 		goto sysfs_ddb_del;
8319 
8320 	if (is_qla40XX(ha)) {
8321 		dev_db_start_offset = FLASH_OFFSET_DB_INFO;
8322 		dev_db_end_offset = FLASH_OFFSET_DB_END;
8323 		dev_db_start_offset += (fnode_sess->target_id *
8324 				       sizeof(*fw_ddb_entry));
8325 		ddb_size = sizeof(*fw_ddb_entry);
8326 	} else {
8327 		dev_db_start_offset = FLASH_RAW_ACCESS_ADDR +
8328 				      (ha->hw.flt_region_ddb << 2);
8329 		/* flt_ddb_size is DDB table size for both ports
8330 		 * so divide it by 2 to calculate the offset for second port
8331 		 */
8332 		if (ha->port_num == 1)
8333 			dev_db_start_offset += (ha->hw.flt_ddb_size / 2);
8334 
8335 		dev_db_end_offset = dev_db_start_offset +
8336 				    (ha->hw.flt_ddb_size / 2);
8337 
8338 		dev_db_start_offset += (fnode_sess->target_id *
8339 				       sizeof(*fw_ddb_entry));
8340 		dev_db_start_offset += offsetof(struct dev_db_entry, cookie);
8341 
8342 		ddb_size = sizeof(*ddb_cookie);
8343 	}
8344 
8345 	DEBUG2(ql4_printk(KERN_ERR, ha, "%s: start offset=%u, end offset=%u\n",
8346 			  __func__, dev_db_start_offset, dev_db_end_offset));
8347 
8348 	if (dev_db_start_offset > dev_db_end_offset) {
8349 		rc = -EIO;
8350 		DEBUG2(ql4_printk(KERN_ERR, ha, "%s:Invalid DDB index %u\n",
8351 				  __func__, fnode_sess->target_id));
8352 		goto exit_ddb_del;
8353 	}
8354 
8355 	pddb = dma_alloc_coherent(&ha->pdev->dev, ddb_size,
8356 				  &fw_ddb_entry_dma, GFP_KERNEL);
8357 	if (!pddb) {
8358 		rc = -ENOMEM;
8359 		DEBUG2(ql4_printk(KERN_ERR, ha,
8360 				  "%s: Unable to allocate dma buffer\n",
8361 				  __func__));
8362 		goto exit_ddb_del;
8363 	}
8364 
8365 	if (is_qla40XX(ha)) {
8366 		fw_ddb_entry = pddb;
8367 		memset(fw_ddb_entry, 0, ddb_size);
8368 		ddb_cookie = &fw_ddb_entry->cookie;
8369 	} else {
8370 		ddb_cookie = pddb;
8371 	}
8372 
8373 	/* invalidate the cookie */
8374 	*ddb_cookie = 0xFFEE;
8375 	qla4xxx_set_flash(ha, fw_ddb_entry_dma, dev_db_start_offset,
8376 			  ddb_size, FLASH_OPT_RMW_COMMIT);
8377 
8378 sysfs_ddb_del:
8379 	target_id = fnode_sess->target_id;
8380 	iscsi_destroy_flashnode_sess(fnode_sess);
8381 	ql4_printk(KERN_INFO, ha,
8382 		   "%s: session and conn entries for flashnode %u of host %lu deleted\n",
8383 		   __func__, target_id, ha->host_no);
8384 exit_ddb_del:
8385 	if (pddb)
8386 		dma_free_coherent(&ha->pdev->dev, ddb_size, pddb,
8387 				  fw_ddb_entry_dma);
8388 	return rc;
8389 }
8390 
8391 /**
8392  * qla4xxx_sysfs_ddb_export - Create sysfs entries for firmware DDBs
8393  * @ha: pointer to adapter structure
8394  *
8395  * Export the firmware DDB for all send targets and normal targets to sysfs.
8396  **/
8397 int qla4xxx_sysfs_ddb_export(struct scsi_qla_host *ha)
8398 {
8399 	struct dev_db_entry *fw_ddb_entry = NULL;
8400 	dma_addr_t fw_ddb_entry_dma;
8401 	uint16_t max_ddbs;
8402 	uint16_t idx = 0;
8403 	int ret = QLA_SUCCESS;
8404 
8405 	fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev,
8406 					  sizeof(*fw_ddb_entry),
8407 					  &fw_ddb_entry_dma, GFP_KERNEL);
8408 	if (!fw_ddb_entry) {
8409 		DEBUG2(ql4_printk(KERN_ERR, ha,
8410 				  "%s: Unable to allocate dma buffer\n",
8411 				  __func__));
8412 		return -ENOMEM;
8413 	}
8414 
8415 	max_ddbs =  is_qla40XX(ha) ? MAX_PRST_DEV_DB_ENTRIES :
8416 				     MAX_DEV_DB_ENTRIES;
8417 
8418 	for (idx = 0; idx < max_ddbs; idx++) {
8419 		if (qla4xxx_flashdb_by_index(ha, fw_ddb_entry, fw_ddb_entry_dma,
8420 					     idx))
8421 			continue;
8422 
8423 		ret = qla4xxx_sysfs_ddb_tgt_create(ha, fw_ddb_entry, &idx, 0);
8424 		if (ret) {
8425 			ret = -EIO;
8426 			break;
8427 		}
8428 	}
8429 
8430 	dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry), fw_ddb_entry,
8431 			  fw_ddb_entry_dma);
8432 
8433 	return ret;
8434 }
8435 
8436 static void qla4xxx_sysfs_ddb_remove(struct scsi_qla_host *ha)
8437 {
8438 	iscsi_destroy_all_flashnode(ha->host);
8439 }
8440 
8441 /**
8442  * qla4xxx_build_ddb_list - Build ddb list and setup sessions
8443  * @ha: pointer to adapter structure
8444  * @is_reset: Is this init path or reset path
8445  *
8446  * Create a list of sendtargets (st) from firmware DDBs, issue send targets
8447  * using connection open, then create the list of normal targets (nt)
8448  * from firmware DDBs. Based on the list of nt setup session and connection
8449  * objects.
8450  **/
8451 void qla4xxx_build_ddb_list(struct scsi_qla_host *ha, int is_reset)
8452 {
8453 	uint16_t tmo = 0;
8454 	struct list_head list_st, list_nt;
8455 	struct qla_ddb_index  *st_ddb_idx, *st_ddb_idx_tmp;
8456 	unsigned long wtime;
8457 
8458 	if (!test_bit(AF_LINK_UP, &ha->flags)) {
8459 		set_bit(AF_BUILD_DDB_LIST, &ha->flags);
8460 		ha->is_reset = is_reset;
8461 		return;
8462 	}
8463 
8464 	INIT_LIST_HEAD(&list_st);
8465 	INIT_LIST_HEAD(&list_nt);
8466 
8467 	qla4xxx_build_st_list(ha, &list_st);
8468 
8469 	/* Before issuing conn open mbox, ensure all IPs states are configured
8470 	 * Note, conn open fails if IPs are not configured
8471 	 */
8472 	qla4xxx_wait_for_ip_configuration(ha);
8473 
8474 	/* Go thru the STs and fire the sendtargets by issuing conn open mbx */
8475 	list_for_each_entry_safe(st_ddb_idx, st_ddb_idx_tmp, &list_st, list) {
8476 		qla4xxx_conn_open(ha, st_ddb_idx->fw_ddb_idx);
8477 	}
8478 
8479 	/* Wait to ensure all sendtargets are done for min 12 sec wait */
8480 	tmo = ((ha->def_timeout > LOGIN_TOV) &&
8481 	       (ha->def_timeout < LOGIN_TOV * 10) ?
8482 	       ha->def_timeout : LOGIN_TOV);
8483 
8484 	DEBUG2(ql4_printk(KERN_INFO, ha,
8485 			  "Default time to wait for build ddb %d\n", tmo));
8486 
8487 	wtime = jiffies + (HZ * tmo);
8488 	do {
8489 		if (list_empty(&list_st))
8490 			break;
8491 
8492 		qla4xxx_remove_failed_ddb(ha, &list_st);
8493 		schedule_timeout_uninterruptible(HZ / 10);
8494 	} while (time_after(wtime, jiffies));
8495 
8496 
8497 	qla4xxx_build_nt_list(ha, &list_nt, &list_st, is_reset);
8498 
8499 	qla4xxx_free_ddb_list(&list_st);
8500 	qla4xxx_free_ddb_list(&list_nt);
8501 
8502 	qla4xxx_free_ddb_index(ha);
8503 }
8504 
8505 /**
8506  * qla4xxx_wait_login_resp_boot_tgt -  Wait for iSCSI boot target login
8507  * response.
8508  * @ha: pointer to adapter structure
8509  *
8510  * When the boot entry is normal iSCSI target then DF_BOOT_TGT flag will be
8511  * set in DDB and we will wait for login response of boot targets during
8512  * probe.
8513  **/
8514 static void qla4xxx_wait_login_resp_boot_tgt(struct scsi_qla_host *ha)
8515 {
8516 	struct ddb_entry *ddb_entry;
8517 	struct dev_db_entry *fw_ddb_entry = NULL;
8518 	dma_addr_t fw_ddb_entry_dma;
8519 	unsigned long wtime;
8520 	uint32_t ddb_state;
8521 	int max_ddbs, idx, ret;
8522 
8523 	max_ddbs =  is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
8524 				     MAX_DEV_DB_ENTRIES;
8525 
8526 	fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
8527 					  &fw_ddb_entry_dma, GFP_KERNEL);
8528 	if (!fw_ddb_entry) {
8529 		ql4_printk(KERN_ERR, ha,
8530 			   "%s: Unable to allocate dma buffer\n", __func__);
8531 		goto exit_login_resp;
8532 	}
8533 
8534 	wtime = jiffies + (HZ * BOOT_LOGIN_RESP_TOV);
8535 
8536 	for (idx = 0; idx < max_ddbs; idx++) {
8537 		ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
8538 		if (ddb_entry == NULL)
8539 			continue;
8540 
8541 		if (test_bit(DF_BOOT_TGT, &ddb_entry->flags)) {
8542 			DEBUG2(ql4_printk(KERN_INFO, ha,
8543 					  "%s: DDB index [%d]\n", __func__,
8544 					  ddb_entry->fw_ddb_index));
8545 			do {
8546 				ret = qla4xxx_get_fwddb_entry(ha,
8547 						ddb_entry->fw_ddb_index,
8548 						fw_ddb_entry, fw_ddb_entry_dma,
8549 						NULL, NULL, &ddb_state, NULL,
8550 						NULL, NULL);
8551 				if (ret == QLA_ERROR)
8552 					goto exit_login_resp;
8553 
8554 				if ((ddb_state == DDB_DS_SESSION_ACTIVE) ||
8555 				    (ddb_state == DDB_DS_SESSION_FAILED))
8556 					break;
8557 
8558 				schedule_timeout_uninterruptible(HZ);
8559 
8560 			} while ((time_after(wtime, jiffies)));
8561 
8562 			if (!time_after(wtime, jiffies)) {
8563 				DEBUG2(ql4_printk(KERN_INFO, ha,
8564 						  "%s: Login response wait timer expired\n",
8565 						  __func__));
8566 				 goto exit_login_resp;
8567 			}
8568 		}
8569 	}
8570 
8571 exit_login_resp:
8572 	if (fw_ddb_entry)
8573 		dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
8574 				  fw_ddb_entry, fw_ddb_entry_dma);
8575 }
8576 
8577 /**
8578  * qla4xxx_probe_adapter - callback function to probe HBA
8579  * @pdev: pointer to pci_dev structure
8580  * @pci_device_id: pointer to pci_device entry
8581  *
8582  * This routine will probe for Qlogic 4xxx iSCSI host adapters.
8583  * It returns zero if successful. It also initializes all data necessary for
8584  * the driver.
8585  **/
8586 static int qla4xxx_probe_adapter(struct pci_dev *pdev,
8587 				 const struct pci_device_id *ent)
8588 {
8589 	int ret = -ENODEV, status;
8590 	struct Scsi_Host *host;
8591 	struct scsi_qla_host *ha;
8592 	uint8_t init_retry_count = 0;
8593 	char buf[34];
8594 	struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
8595 	uint32_t dev_state;
8596 
8597 	if (pci_enable_device(pdev))
8598 		return -1;
8599 
8600 	host = iscsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha), 0);
8601 	if (host == NULL) {
8602 		printk(KERN_WARNING
8603 		       "qla4xxx: Couldn't allocate host from scsi layer!\n");
8604 		goto probe_disable_device;
8605 	}
8606 
8607 	/* Clear our data area */
8608 	ha = to_qla_host(host);
8609 	memset(ha, 0, sizeof(*ha));
8610 
8611 	/* Save the information from PCI BIOS.	*/
8612 	ha->pdev = pdev;
8613 	ha->host = host;
8614 	ha->host_no = host->host_no;
8615 	ha->func_num = PCI_FUNC(ha->pdev->devfn);
8616 
8617 	pci_enable_pcie_error_reporting(pdev);
8618 
8619 	/* Setup Runtime configurable options */
8620 	if (is_qla8022(ha)) {
8621 		ha->isp_ops = &qla4_82xx_isp_ops;
8622 		ha->reg_tbl = (uint32_t *) qla4_82xx_reg_tbl;
8623 		ha->qdr_sn_window = -1;
8624 		ha->ddr_mn_window = -1;
8625 		ha->curr_window = 255;
8626 		nx_legacy_intr = &legacy_intr[ha->func_num];
8627 		ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
8628 		ha->nx_legacy_intr.tgt_status_reg =
8629 			nx_legacy_intr->tgt_status_reg;
8630 		ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
8631 		ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
8632 	} else if (is_qla8032(ha) || is_qla8042(ha)) {
8633 		ha->isp_ops = &qla4_83xx_isp_ops;
8634 		ha->reg_tbl = (uint32_t *)qla4_83xx_reg_tbl;
8635 	} else {
8636 		ha->isp_ops = &qla4xxx_isp_ops;
8637 	}
8638 
8639 	if (is_qla80XX(ha)) {
8640 		rwlock_init(&ha->hw_lock);
8641 		ha->pf_bit = ha->func_num << 16;
8642 		/* Set EEH reset type to fundamental if required by hba */
8643 		pdev->needs_freset = 1;
8644 	}
8645 
8646 	/* Configure PCI I/O space. */
8647 	ret = ha->isp_ops->iospace_config(ha);
8648 	if (ret)
8649 		goto probe_failed_ioconfig;
8650 
8651 	ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
8652 		   pdev->device, pdev->irq, ha->reg);
8653 
8654 	qla4xxx_config_dma_addressing(ha);
8655 
8656 	/* Initialize lists and spinlocks. */
8657 	INIT_LIST_HEAD(&ha->free_srb_q);
8658 
8659 	mutex_init(&ha->mbox_sem);
8660 	mutex_init(&ha->chap_sem);
8661 	init_completion(&ha->mbx_intr_comp);
8662 	init_completion(&ha->disable_acb_comp);
8663 	init_completion(&ha->idc_comp);
8664 	init_completion(&ha->link_up_comp);
8665 
8666 	spin_lock_init(&ha->hardware_lock);
8667 	spin_lock_init(&ha->work_lock);
8668 
8669 	/* Initialize work list */
8670 	INIT_LIST_HEAD(&ha->work_list);
8671 
8672 	/* Allocate dma buffers */
8673 	if (qla4xxx_mem_alloc(ha)) {
8674 		ql4_printk(KERN_WARNING, ha,
8675 		    "[ERROR] Failed to allocate memory for adapter\n");
8676 
8677 		ret = -ENOMEM;
8678 		goto probe_failed;
8679 	}
8680 
8681 	host->cmd_per_lun = 3;
8682 	host->max_channel = 0;
8683 	host->max_lun = MAX_LUNS - 1;
8684 	host->max_id = MAX_TARGETS;
8685 	host->max_cmd_len = IOCB_MAX_CDB_LEN;
8686 	host->can_queue = MAX_SRBS ;
8687 	host->transportt = qla4xxx_scsi_transport;
8688 
8689 	pci_set_drvdata(pdev, ha);
8690 
8691 	ret = scsi_add_host(host, &pdev->dev);
8692 	if (ret)
8693 		goto probe_failed;
8694 
8695 	if (is_qla80XX(ha))
8696 		qla4_8xxx_get_flash_info(ha);
8697 
8698 	if (is_qla8032(ha) || is_qla8042(ha)) {
8699 		qla4_83xx_read_reset_template(ha);
8700 		/*
8701 		 * NOTE: If ql4dontresethba==1, set IDC_CTRL DONTRESET_BIT0.
8702 		 * If DONRESET_BIT0 is set, drivers should not set dev_state
8703 		 * to NEED_RESET. But if NEED_RESET is set, drivers should
8704 		 * should honor the reset.
8705 		 */
8706 		if (ql4xdontresethba == 1)
8707 			qla4_83xx_set_idc_dontreset(ha);
8708 	}
8709 
8710 	/*
8711 	 * Initialize the Host adapter request/response queues and
8712 	 * firmware
8713 	 * NOTE: interrupts enabled upon successful completion
8714 	 */
8715 	status = qla4xxx_initialize_adapter(ha, INIT_ADAPTER);
8716 
8717 	/* Dont retry adapter initialization if IRQ allocation failed */
8718 	if (is_qla80XX(ha) && (status == QLA_ERROR))
8719 		goto skip_retry_init;
8720 
8721 	while ((!test_bit(AF_ONLINE, &ha->flags)) &&
8722 	    init_retry_count++ < MAX_INIT_RETRIES) {
8723 
8724 		if (is_qla80XX(ha)) {
8725 			ha->isp_ops->idc_lock(ha);
8726 			dev_state = qla4_8xxx_rd_direct(ha,
8727 							QLA8XXX_CRB_DEV_STATE);
8728 			ha->isp_ops->idc_unlock(ha);
8729 			if (dev_state == QLA8XXX_DEV_FAILED) {
8730 				ql4_printk(KERN_WARNING, ha, "%s: don't retry "
8731 				    "initialize adapter. H/W is in failed state\n",
8732 				    __func__);
8733 				break;
8734 			}
8735 		}
8736 		DEBUG2(printk("scsi: %s: retrying adapter initialization "
8737 			      "(%d)\n", __func__, init_retry_count));
8738 
8739 		if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
8740 			continue;
8741 
8742 		status = qla4xxx_initialize_adapter(ha, INIT_ADAPTER);
8743 		if (is_qla80XX(ha) && (status == QLA_ERROR)) {
8744 			if (qla4_8xxx_check_init_adapter_retry(ha) == QLA_ERROR)
8745 				goto skip_retry_init;
8746 		}
8747 	}
8748 
8749 skip_retry_init:
8750 	if (!test_bit(AF_ONLINE, &ha->flags)) {
8751 		ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
8752 
8753 		if ((is_qla8022(ha) && ql4xdontresethba) ||
8754 		    ((is_qla8032(ha) || is_qla8042(ha)) &&
8755 		     qla4_83xx_idc_dontreset(ha))) {
8756 			/* Put the device in failed state. */
8757 			DEBUG2(printk(KERN_ERR "HW STATE: FAILED\n"));
8758 			ha->isp_ops->idc_lock(ha);
8759 			qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
8760 					    QLA8XXX_DEV_FAILED);
8761 			ha->isp_ops->idc_unlock(ha);
8762 		}
8763 		ret = -ENODEV;
8764 		goto remove_host;
8765 	}
8766 
8767 	/* Startup the kernel thread for this host adapter. */
8768 	DEBUG2(printk("scsi: %s: Starting kernel thread for "
8769 		      "qla4xxx_dpc\n", __func__));
8770 	sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
8771 	ha->dpc_thread = create_singlethread_workqueue(buf);
8772 	if (!ha->dpc_thread) {
8773 		ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
8774 		ret = -ENODEV;
8775 		goto remove_host;
8776 	}
8777 	INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
8778 
8779 	ha->task_wq = alloc_workqueue("qla4xxx_%lu_task", WQ_MEM_RECLAIM, 1,
8780 				      ha->host_no);
8781 	if (!ha->task_wq) {
8782 		ql4_printk(KERN_WARNING, ha, "Unable to start task thread!\n");
8783 		ret = -ENODEV;
8784 		goto remove_host;
8785 	}
8786 
8787 	/*
8788 	 * For ISP-8XXX, request_irqs is called in qla4_8xxx_load_risc
8789 	 * (which is called indirectly by qla4xxx_initialize_adapter),
8790 	 * so that irqs will be registered after crbinit but before
8791 	 * mbx_intr_enable.
8792 	 */
8793 	if (is_qla40XX(ha)) {
8794 		ret = qla4xxx_request_irqs(ha);
8795 		if (ret) {
8796 			ql4_printk(KERN_WARNING, ha, "Failed to reserve "
8797 			    "interrupt %d already in use.\n", pdev->irq);
8798 			goto remove_host;
8799 		}
8800 	}
8801 
8802 	pci_save_state(ha->pdev);
8803 	ha->isp_ops->enable_intrs(ha);
8804 
8805 	/* Start timer thread. */
8806 	qla4xxx_start_timer(ha, 1);
8807 
8808 	set_bit(AF_INIT_DONE, &ha->flags);
8809 
8810 	qla4_8xxx_alloc_sysfs_attr(ha);
8811 
8812 	printk(KERN_INFO
8813 	       " QLogic iSCSI HBA Driver version: %s\n"
8814 	       "  QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
8815 	       qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
8816 	       ha->host_no, ha->fw_info.fw_major, ha->fw_info.fw_minor,
8817 	       ha->fw_info.fw_patch, ha->fw_info.fw_build);
8818 
8819 	/* Set the driver version */
8820 	if (is_qla80XX(ha))
8821 		qla4_8xxx_set_param(ha, SET_DRVR_VERSION);
8822 
8823 	if (qla4xxx_setup_boot_info(ha))
8824 		ql4_printk(KERN_ERR, ha,
8825 			   "%s: No iSCSI boot target configured\n", __func__);
8826 
8827 	set_bit(DPC_SYSFS_DDB_EXPORT, &ha->dpc_flags);
8828 	/* Perform the build ddb list and login to each */
8829 	qla4xxx_build_ddb_list(ha, INIT_ADAPTER);
8830 	iscsi_host_for_each_session(ha->host, qla4xxx_login_flash_ddb);
8831 	qla4xxx_wait_login_resp_boot_tgt(ha);
8832 
8833 	qla4xxx_create_chap_list(ha);
8834 
8835 	qla4xxx_create_ifaces(ha);
8836 	return 0;
8837 
8838 remove_host:
8839 	scsi_remove_host(ha->host);
8840 
8841 probe_failed:
8842 	qla4xxx_free_adapter(ha);
8843 
8844 probe_failed_ioconfig:
8845 	pci_disable_pcie_error_reporting(pdev);
8846 	scsi_host_put(ha->host);
8847 
8848 probe_disable_device:
8849 	pci_disable_device(pdev);
8850 
8851 	return ret;
8852 }
8853 
8854 /**
8855  * qla4xxx_prevent_other_port_reinit - prevent other port from re-initialize
8856  * @ha: pointer to adapter structure
8857  *
8858  * Mark the other ISP-4xxx port to indicate that the driver is being removed,
8859  * so that the other port will not re-initialize while in the process of
8860  * removing the ha due to driver unload or hba hotplug.
8861  **/
8862 static void qla4xxx_prevent_other_port_reinit(struct scsi_qla_host *ha)
8863 {
8864 	struct scsi_qla_host *other_ha = NULL;
8865 	struct pci_dev *other_pdev = NULL;
8866 	int fn = ISP4XXX_PCI_FN_2;
8867 
8868 	/*iscsi function numbers for ISP4xxx is 1 and 3*/
8869 	if (PCI_FUNC(ha->pdev->devfn) & BIT_1)
8870 		fn = ISP4XXX_PCI_FN_1;
8871 
8872 	other_pdev =
8873 		pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
8874 		ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
8875 		fn));
8876 
8877 	/* Get other_ha if other_pdev is valid and state is enable*/
8878 	if (other_pdev) {
8879 		if (atomic_read(&other_pdev->enable_cnt)) {
8880 			other_ha = pci_get_drvdata(other_pdev);
8881 			if (other_ha) {
8882 				set_bit(AF_HA_REMOVAL, &other_ha->flags);
8883 				DEBUG2(ql4_printk(KERN_INFO, ha, "%s: "
8884 				    "Prevent %s reinit\n", __func__,
8885 				    dev_name(&other_ha->pdev->dev)));
8886 			}
8887 		}
8888 		pci_dev_put(other_pdev);
8889 	}
8890 }
8891 
8892 static void qla4xxx_destroy_ddb(struct scsi_qla_host *ha,
8893 		struct ddb_entry *ddb_entry)
8894 {
8895 	struct dev_db_entry *fw_ddb_entry = NULL;
8896 	dma_addr_t fw_ddb_entry_dma;
8897 	unsigned long wtime;
8898 	uint32_t ddb_state;
8899 	int options;
8900 	int status;
8901 
8902 	options = LOGOUT_OPTION_CLOSE_SESSION;
8903 	if (qla4xxx_session_logout_ddb(ha, ddb_entry, options) == QLA_ERROR) {
8904 		ql4_printk(KERN_ERR, ha, "%s: Logout failed\n", __func__);
8905 		goto clear_ddb;
8906 	}
8907 
8908 	fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
8909 					  &fw_ddb_entry_dma, GFP_KERNEL);
8910 	if (!fw_ddb_entry) {
8911 		ql4_printk(KERN_ERR, ha,
8912 			   "%s: Unable to allocate dma buffer\n", __func__);
8913 		goto clear_ddb;
8914 	}
8915 
8916 	wtime = jiffies + (HZ * LOGOUT_TOV);
8917 	do {
8918 		status = qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index,
8919 						 fw_ddb_entry, fw_ddb_entry_dma,
8920 						 NULL, NULL, &ddb_state, NULL,
8921 						 NULL, NULL);
8922 		if (status == QLA_ERROR)
8923 			goto free_ddb;
8924 
8925 		if ((ddb_state == DDB_DS_NO_CONNECTION_ACTIVE) ||
8926 		    (ddb_state == DDB_DS_SESSION_FAILED))
8927 			goto free_ddb;
8928 
8929 		schedule_timeout_uninterruptible(HZ);
8930 	} while ((time_after(wtime, jiffies)));
8931 
8932 free_ddb:
8933 	dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
8934 			  fw_ddb_entry, fw_ddb_entry_dma);
8935 clear_ddb:
8936 	qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
8937 }
8938 
8939 static void qla4xxx_destroy_fw_ddb_session(struct scsi_qla_host *ha)
8940 {
8941 	struct ddb_entry *ddb_entry;
8942 	int idx;
8943 
8944 	for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
8945 
8946 		ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
8947 		if ((ddb_entry != NULL) &&
8948 		    (ddb_entry->ddb_type == FLASH_DDB)) {
8949 
8950 			qla4xxx_destroy_ddb(ha, ddb_entry);
8951 			/*
8952 			 * we have decremented the reference count of the driver
8953 			 * when we setup the session to have the driver unload
8954 			 * to be seamless without actually destroying the
8955 			 * session
8956 			 **/
8957 			try_module_get(qla4xxx_iscsi_transport.owner);
8958 			iscsi_destroy_endpoint(ddb_entry->conn->ep);
8959 			qla4xxx_free_ddb(ha, ddb_entry);
8960 			iscsi_session_teardown(ddb_entry->sess);
8961 		}
8962 	}
8963 }
8964 /**
8965  * qla4xxx_remove_adapter - callback function to remove adapter.
8966  * @pci_dev: PCI device pointer
8967  **/
8968 static void qla4xxx_remove_adapter(struct pci_dev *pdev)
8969 {
8970 	struct scsi_qla_host *ha;
8971 
8972 	/*
8973 	 * If the PCI device is disabled then it means probe_adapter had
8974 	 * failed and resources already cleaned up on probe_adapter exit.
8975 	 */
8976 	if (!pci_is_enabled(pdev))
8977 		return;
8978 
8979 	ha = pci_get_drvdata(pdev);
8980 
8981 	if (is_qla40XX(ha))
8982 		qla4xxx_prevent_other_port_reinit(ha);
8983 
8984 	/* destroy iface from sysfs */
8985 	qla4xxx_destroy_ifaces(ha);
8986 
8987 	if ((!ql4xdisablesysfsboot) && ha->boot_kset)
8988 		iscsi_boot_destroy_kset(ha->boot_kset);
8989 
8990 	qla4xxx_destroy_fw_ddb_session(ha);
8991 	qla4_8xxx_free_sysfs_attr(ha);
8992 
8993 	qla4xxx_sysfs_ddb_remove(ha);
8994 	scsi_remove_host(ha->host);
8995 
8996 	qla4xxx_free_adapter(ha);
8997 
8998 	scsi_host_put(ha->host);
8999 
9000 	pci_disable_pcie_error_reporting(pdev);
9001 	pci_disable_device(pdev);
9002 }
9003 
9004 /**
9005  * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
9006  * @ha: HA context
9007  *
9008  * At exit, the @ha's flags.enable_64bit_addressing set to indicated
9009  * supported addressing method.
9010  */
9011 static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
9012 {
9013 	int retval;
9014 
9015 	/* Update our PCI device dma_mask for full 64 bit mask */
9016 	if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
9017 		if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
9018 			dev_dbg(&ha->pdev->dev,
9019 				  "Failed to set 64 bit PCI consistent mask; "
9020 				   "using 32 bit.\n");
9021 			retval = pci_set_consistent_dma_mask(ha->pdev,
9022 							     DMA_BIT_MASK(32));
9023 		}
9024 	} else
9025 		retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
9026 }
9027 
9028 static int qla4xxx_slave_alloc(struct scsi_device *sdev)
9029 {
9030 	struct iscsi_cls_session *cls_sess;
9031 	struct iscsi_session *sess;
9032 	struct ddb_entry *ddb;
9033 	int queue_depth = QL4_DEF_QDEPTH;
9034 
9035 	cls_sess = starget_to_session(sdev->sdev_target);
9036 	sess = cls_sess->dd_data;
9037 	ddb = sess->dd_data;
9038 
9039 	sdev->hostdata = ddb;
9040 
9041 	if (ql4xmaxqdepth != 0 && ql4xmaxqdepth <= 0xffffU)
9042 		queue_depth = ql4xmaxqdepth;
9043 
9044 	scsi_change_queue_depth(sdev, queue_depth);
9045 	return 0;
9046 }
9047 
9048 /**
9049  * qla4xxx_del_from_active_array - returns an active srb
9050  * @ha: Pointer to host adapter structure.
9051  * @index: index into the active_array
9052  *
9053  * This routine removes and returns the srb at the specified index
9054  **/
9055 struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
9056     uint32_t index)
9057 {
9058 	struct srb *srb = NULL;
9059 	struct scsi_cmnd *cmd = NULL;
9060 
9061 	cmd = scsi_host_find_tag(ha->host, index);
9062 	if (!cmd)
9063 		return srb;
9064 
9065 	srb = (struct srb *)CMD_SP(cmd);
9066 	if (!srb)
9067 		return srb;
9068 
9069 	/* update counters */
9070 	if (srb->flags & SRB_DMA_VALID) {
9071 		ha->iocb_cnt -= srb->iocb_cnt;
9072 		if (srb->cmd)
9073 			srb->cmd->host_scribble =
9074 				(unsigned char *)(unsigned long) MAX_SRBS;
9075 	}
9076 	return srb;
9077 }
9078 
9079 /**
9080  * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
9081  * @ha: Pointer to host adapter structure.
9082  * @cmd: Scsi Command to wait on.
9083  *
9084  * This routine waits for the command to be returned by the Firmware
9085  * for some max time.
9086  **/
9087 static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
9088 				      struct scsi_cmnd *cmd)
9089 {
9090 	int done = 0;
9091 	struct srb *rp;
9092 	uint32_t max_wait_time = EH_WAIT_CMD_TOV;
9093 	int ret = SUCCESS;
9094 
9095 	/* Dont wait on command if PCI error is being handled
9096 	 * by PCI AER driver
9097 	 */
9098 	if (unlikely(pci_channel_offline(ha->pdev)) ||
9099 	    (test_bit(AF_EEH_BUSY, &ha->flags))) {
9100 		ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
9101 		    ha->host_no, __func__);
9102 		return ret;
9103 	}
9104 
9105 	do {
9106 		/* Checking to see if its returned to OS */
9107 		rp = (struct srb *) CMD_SP(cmd);
9108 		if (rp == NULL) {
9109 			done++;
9110 			break;
9111 		}
9112 
9113 		msleep(2000);
9114 	} while (max_wait_time--);
9115 
9116 	return done;
9117 }
9118 
9119 /**
9120  * qla4xxx_wait_for_hba_online - waits for HBA to come online
9121  * @ha: Pointer to host adapter structure
9122  **/
9123 static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
9124 {
9125 	unsigned long wait_online;
9126 
9127 	wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
9128 	while (time_before(jiffies, wait_online)) {
9129 
9130 		if (adapter_up(ha))
9131 			return QLA_SUCCESS;
9132 
9133 		msleep(2000);
9134 	}
9135 
9136 	return QLA_ERROR;
9137 }
9138 
9139 /**
9140  * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
9141  * @ha: pointer to HBA
9142  * @t: target id
9143  * @l: lun id
9144  *
9145  * This function waits for all outstanding commands to a lun to complete. It
9146  * returns 0 if all pending commands are returned and 1 otherwise.
9147  **/
9148 static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
9149 					struct scsi_target *stgt,
9150 					struct scsi_device *sdev)
9151 {
9152 	int cnt;
9153 	int status = 0;
9154 	struct scsi_cmnd *cmd;
9155 
9156 	/*
9157 	 * Waiting for all commands for the designated target or dev
9158 	 * in the active array
9159 	 */
9160 	for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
9161 		cmd = scsi_host_find_tag(ha->host, cnt);
9162 		if (cmd && stgt == scsi_target(cmd->device) &&
9163 		    (!sdev || sdev == cmd->device)) {
9164 			if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
9165 				status++;
9166 				break;
9167 			}
9168 		}
9169 	}
9170 	return status;
9171 }
9172 
9173 /**
9174  * qla4xxx_eh_abort - callback for abort task.
9175  * @cmd: Pointer to Linux's SCSI command structure
9176  *
9177  * This routine is called by the Linux OS to abort the specified
9178  * command.
9179  **/
9180 static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
9181 {
9182 	struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
9183 	unsigned int id = cmd->device->id;
9184 	uint64_t lun = cmd->device->lun;
9185 	unsigned long flags;
9186 	struct srb *srb = NULL;
9187 	int ret = SUCCESS;
9188 	int wait = 0;
9189 
9190 	ql4_printk(KERN_INFO, ha, "scsi%ld:%d:%llu: Abort command issued cmd=%p, cdb=0x%x\n",
9191 		   ha->host_no, id, lun, cmd, cmd->cmnd[0]);
9192 
9193 	spin_lock_irqsave(&ha->hardware_lock, flags);
9194 	srb = (struct srb *) CMD_SP(cmd);
9195 	if (!srb) {
9196 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
9197 		ql4_printk(KERN_INFO, ha, "scsi%ld:%d:%llu: Specified command has already completed.\n",
9198 			   ha->host_no, id, lun);
9199 		return SUCCESS;
9200 	}
9201 	kref_get(&srb->srb_ref);
9202 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
9203 
9204 	if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
9205 		DEBUG3(printk("scsi%ld:%d:%llu: Abort_task mbx failed.\n",
9206 		    ha->host_no, id, lun));
9207 		ret = FAILED;
9208 	} else {
9209 		DEBUG3(printk("scsi%ld:%d:%llu: Abort_task mbx success.\n",
9210 		    ha->host_no, id, lun));
9211 		wait = 1;
9212 	}
9213 
9214 	kref_put(&srb->srb_ref, qla4xxx_srb_compl);
9215 
9216 	/* Wait for command to complete */
9217 	if (wait) {
9218 		if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
9219 			DEBUG2(printk("scsi%ld:%d:%llu: Abort handler timed out\n",
9220 			    ha->host_no, id, lun));
9221 			ret = FAILED;
9222 		}
9223 	}
9224 
9225 	ql4_printk(KERN_INFO, ha,
9226 	    "scsi%ld:%d:%llu: Abort command - %s\n",
9227 	    ha->host_no, id, lun, (ret == SUCCESS) ? "succeeded" : "failed");
9228 
9229 	return ret;
9230 }
9231 
9232 /**
9233  * qla4xxx_eh_device_reset - callback for target reset.
9234  * @cmd: Pointer to Linux's SCSI command structure
9235  *
9236  * This routine is called by the Linux OS to reset all luns on the
9237  * specified target.
9238  **/
9239 static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
9240 {
9241 	struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
9242 	struct ddb_entry *ddb_entry = cmd->device->hostdata;
9243 	int ret = FAILED, stat;
9244 
9245 	if (!ddb_entry)
9246 		return ret;
9247 
9248 	ret = iscsi_block_scsi_eh(cmd);
9249 	if (ret)
9250 		return ret;
9251 	ret = FAILED;
9252 
9253 	ql4_printk(KERN_INFO, ha,
9254 		   "scsi%ld:%d:%d:%llu: DEVICE RESET ISSUED.\n", ha->host_no,
9255 		   cmd->device->channel, cmd->device->id, cmd->device->lun);
9256 
9257 	DEBUG2(printk(KERN_INFO
9258 		      "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
9259 		      "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
9260 		      cmd, jiffies, cmd->request->timeout / HZ,
9261 		      ha->dpc_flags, cmd->result, cmd->allowed));
9262 
9263 	/* FIXME: wait for hba to go online */
9264 	stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
9265 	if (stat != QLA_SUCCESS) {
9266 		ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
9267 		goto eh_dev_reset_done;
9268 	}
9269 
9270 	if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
9271 					 cmd->device)) {
9272 		ql4_printk(KERN_INFO, ha,
9273 			   "DEVICE RESET FAILED - waiting for "
9274 			   "commands.\n");
9275 		goto eh_dev_reset_done;
9276 	}
9277 
9278 	/* Send marker. */
9279 	if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
9280 		MM_LUN_RESET) != QLA_SUCCESS)
9281 		goto eh_dev_reset_done;
9282 
9283 	ql4_printk(KERN_INFO, ha,
9284 		   "scsi(%ld:%d:%d:%llu): DEVICE RESET SUCCEEDED.\n",
9285 		   ha->host_no, cmd->device->channel, cmd->device->id,
9286 		   cmd->device->lun);
9287 
9288 	ret = SUCCESS;
9289 
9290 eh_dev_reset_done:
9291 
9292 	return ret;
9293 }
9294 
9295 /**
9296  * qla4xxx_eh_target_reset - callback for target reset.
9297  * @cmd: Pointer to Linux's SCSI command structure
9298  *
9299  * This routine is called by the Linux OS to reset the target.
9300  **/
9301 static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
9302 {
9303 	struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
9304 	struct ddb_entry *ddb_entry = cmd->device->hostdata;
9305 	int stat, ret;
9306 
9307 	if (!ddb_entry)
9308 		return FAILED;
9309 
9310 	ret = iscsi_block_scsi_eh(cmd);
9311 	if (ret)
9312 		return ret;
9313 
9314 	starget_printk(KERN_INFO, scsi_target(cmd->device),
9315 		       "WARM TARGET RESET ISSUED.\n");
9316 
9317 	DEBUG2(printk(KERN_INFO
9318 		      "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
9319 		      "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
9320 		      ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
9321 		      ha->dpc_flags, cmd->result, cmd->allowed));
9322 
9323 	stat = qla4xxx_reset_target(ha, ddb_entry);
9324 	if (stat != QLA_SUCCESS) {
9325 		starget_printk(KERN_INFO, scsi_target(cmd->device),
9326 			       "WARM TARGET RESET FAILED.\n");
9327 		return FAILED;
9328 	}
9329 
9330 	if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
9331 					 NULL)) {
9332 		starget_printk(KERN_INFO, scsi_target(cmd->device),
9333 			       "WARM TARGET DEVICE RESET FAILED - "
9334 			       "waiting for commands.\n");
9335 		return FAILED;
9336 	}
9337 
9338 	/* Send marker. */
9339 	if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
9340 		MM_TGT_WARM_RESET) != QLA_SUCCESS) {
9341 		starget_printk(KERN_INFO, scsi_target(cmd->device),
9342 			       "WARM TARGET DEVICE RESET FAILED - "
9343 			       "marker iocb failed.\n");
9344 		return FAILED;
9345 	}
9346 
9347 	starget_printk(KERN_INFO, scsi_target(cmd->device),
9348 		       "WARM TARGET RESET SUCCEEDED.\n");
9349 	return SUCCESS;
9350 }
9351 
9352 /**
9353  * qla4xxx_is_eh_active - check if error handler is running
9354  * @shost: Pointer to SCSI Host struct
9355  *
9356  * This routine finds that if reset host is called in EH
9357  * scenario or from some application like sg_reset
9358  **/
9359 static int qla4xxx_is_eh_active(struct Scsi_Host *shost)
9360 {
9361 	if (shost->shost_state == SHOST_RECOVERY)
9362 		return 1;
9363 	return 0;
9364 }
9365 
9366 /**
9367  * qla4xxx_eh_host_reset - kernel callback
9368  * @cmd: Pointer to Linux's SCSI command structure
9369  *
9370  * This routine is invoked by the Linux kernel to perform fatal error
9371  * recovery on the specified adapter.
9372  **/
9373 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
9374 {
9375 	int return_status = FAILED;
9376 	struct scsi_qla_host *ha;
9377 
9378 	ha = to_qla_host(cmd->device->host);
9379 
9380 	if ((is_qla8032(ha) || is_qla8042(ha)) && ql4xdontresethba)
9381 		qla4_83xx_set_idc_dontreset(ha);
9382 
9383 	/*
9384 	 * For ISP8324 and ISP8042, if IDC_CTRL DONTRESET_BIT0 is set by other
9385 	 * protocol drivers, we should not set device_state to NEED_RESET
9386 	 */
9387 	if (ql4xdontresethba ||
9388 	    ((is_qla8032(ha) || is_qla8042(ha)) &&
9389 	     qla4_83xx_idc_dontreset(ha))) {
9390 		DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
9391 		     ha->host_no, __func__));
9392 
9393 		/* Clear outstanding srb in queues */
9394 		if (qla4xxx_is_eh_active(cmd->device->host))
9395 			qla4xxx_abort_active_cmds(ha, DID_ABORT << 16);
9396 
9397 		return FAILED;
9398 	}
9399 
9400 	ql4_printk(KERN_INFO, ha,
9401 		   "scsi(%ld:%d:%d:%llu): HOST RESET ISSUED.\n", ha->host_no,
9402 		   cmd->device->channel, cmd->device->id, cmd->device->lun);
9403 
9404 	if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
9405 		DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host.  Adapter "
9406 			      "DEAD.\n", ha->host_no, cmd->device->channel,
9407 			      __func__));
9408 
9409 		return FAILED;
9410 	}
9411 
9412 	if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
9413 		if (is_qla80XX(ha))
9414 			set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
9415 		else
9416 			set_bit(DPC_RESET_HA, &ha->dpc_flags);
9417 	}
9418 
9419 	if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
9420 		return_status = SUCCESS;
9421 
9422 	ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
9423 		   return_status == FAILED ? "FAILED" : "SUCCEEDED");
9424 
9425 	return return_status;
9426 }
9427 
9428 static int qla4xxx_context_reset(struct scsi_qla_host *ha)
9429 {
9430 	uint32_t mbox_cmd[MBOX_REG_COUNT];
9431 	uint32_t mbox_sts[MBOX_REG_COUNT];
9432 	struct addr_ctrl_blk_def *acb = NULL;
9433 	uint32_t acb_len = sizeof(struct addr_ctrl_blk_def);
9434 	int rval = QLA_SUCCESS;
9435 	dma_addr_t acb_dma;
9436 
9437 	acb = dma_alloc_coherent(&ha->pdev->dev,
9438 				 sizeof(struct addr_ctrl_blk_def),
9439 				 &acb_dma, GFP_KERNEL);
9440 	if (!acb) {
9441 		ql4_printk(KERN_ERR, ha, "%s: Unable to alloc acb\n",
9442 			   __func__);
9443 		rval = -ENOMEM;
9444 		goto exit_port_reset;
9445 	}
9446 
9447 	memset(acb, 0, acb_len);
9448 
9449 	rval = qla4xxx_get_acb(ha, acb_dma, PRIMARI_ACB, acb_len);
9450 	if (rval != QLA_SUCCESS) {
9451 		rval = -EIO;
9452 		goto exit_free_acb;
9453 	}
9454 
9455 	rval = qla4xxx_disable_acb(ha);
9456 	if (rval != QLA_SUCCESS) {
9457 		rval = -EIO;
9458 		goto exit_free_acb;
9459 	}
9460 
9461 	wait_for_completion_timeout(&ha->disable_acb_comp,
9462 				    DISABLE_ACB_TOV * HZ);
9463 
9464 	rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], acb_dma);
9465 	if (rval != QLA_SUCCESS) {
9466 		rval = -EIO;
9467 		goto exit_free_acb;
9468 	}
9469 
9470 exit_free_acb:
9471 	dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk_def),
9472 			  acb, acb_dma);
9473 exit_port_reset:
9474 	DEBUG2(ql4_printk(KERN_INFO, ha, "%s %s\n", __func__,
9475 			  rval == QLA_SUCCESS ? "SUCCEEDED" : "FAILED"));
9476 	return rval;
9477 }
9478 
9479 static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type)
9480 {
9481 	struct scsi_qla_host *ha = to_qla_host(shost);
9482 	int rval = QLA_SUCCESS;
9483 	uint32_t idc_ctrl;
9484 
9485 	if (ql4xdontresethba) {
9486 		DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Don't Reset HBA\n",
9487 				  __func__));
9488 		rval = -EPERM;
9489 		goto exit_host_reset;
9490 	}
9491 
9492 	if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
9493 		goto recover_adapter;
9494 
9495 	switch (reset_type) {
9496 	case SCSI_ADAPTER_RESET:
9497 		set_bit(DPC_RESET_HA, &ha->dpc_flags);
9498 		break;
9499 	case SCSI_FIRMWARE_RESET:
9500 		if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
9501 			if (is_qla80XX(ha))
9502 				/* set firmware context reset */
9503 				set_bit(DPC_RESET_HA_FW_CONTEXT,
9504 					&ha->dpc_flags);
9505 			else {
9506 				rval = qla4xxx_context_reset(ha);
9507 				goto exit_host_reset;
9508 			}
9509 		}
9510 		break;
9511 	}
9512 
9513 recover_adapter:
9514 	/* For ISP8324 and ISP8042 set graceful reset bit in IDC_DRV_CTRL if
9515 	 * reset is issued by application */
9516 	if ((is_qla8032(ha) || is_qla8042(ha)) &&
9517 	    test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
9518 		idc_ctrl = qla4_83xx_rd_reg(ha, QLA83XX_IDC_DRV_CTRL);
9519 		qla4_83xx_wr_reg(ha, QLA83XX_IDC_DRV_CTRL,
9520 				 (idc_ctrl | GRACEFUL_RESET_BIT1));
9521 	}
9522 
9523 	rval = qla4xxx_recover_adapter(ha);
9524 	if (rval != QLA_SUCCESS) {
9525 		DEBUG2(ql4_printk(KERN_INFO, ha, "%s: recover adapter fail\n",
9526 				  __func__));
9527 		rval = -EIO;
9528 	}
9529 
9530 exit_host_reset:
9531 	return rval;
9532 }
9533 
9534 /* PCI AER driver recovers from all correctable errors w/o
9535  * driver intervention. For uncorrectable errors PCI AER
9536  * driver calls the following device driver's callbacks
9537  *
9538  * - Fatal Errors - link_reset
9539  * - Non-Fatal Errors - driver's error_detected() which
9540  * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
9541  *
9542  * PCI AER driver calls
9543  * CAN_RECOVER - driver's mmio_enabled(), mmio_enabled()
9544  *               returns RECOVERED or NEED_RESET if fw_hung
9545  * NEED_RESET - driver's slot_reset()
9546  * DISCONNECT - device is dead & cannot recover
9547  * RECOVERED - driver's resume()
9548  */
9549 static pci_ers_result_t
9550 qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
9551 {
9552 	struct scsi_qla_host *ha = pci_get_drvdata(pdev);
9553 
9554 	ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
9555 	    ha->host_no, __func__, state);
9556 
9557 	if (!is_aer_supported(ha))
9558 		return PCI_ERS_RESULT_NONE;
9559 
9560 	switch (state) {
9561 	case pci_channel_io_normal:
9562 		clear_bit(AF_EEH_BUSY, &ha->flags);
9563 		return PCI_ERS_RESULT_CAN_RECOVER;
9564 	case pci_channel_io_frozen:
9565 		set_bit(AF_EEH_BUSY, &ha->flags);
9566 		qla4xxx_mailbox_premature_completion(ha);
9567 		qla4xxx_free_irqs(ha);
9568 		pci_disable_device(pdev);
9569 		/* Return back all IOs */
9570 		qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
9571 		return PCI_ERS_RESULT_NEED_RESET;
9572 	case pci_channel_io_perm_failure:
9573 		set_bit(AF_EEH_BUSY, &ha->flags);
9574 		set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
9575 		qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
9576 		return PCI_ERS_RESULT_DISCONNECT;
9577 	}
9578 	return PCI_ERS_RESULT_NEED_RESET;
9579 }
9580 
9581 /**
9582  * qla4xxx_pci_mmio_enabled() gets called if
9583  * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
9584  * and read/write to the device still works.
9585  **/
9586 static pci_ers_result_t
9587 qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
9588 {
9589 	struct scsi_qla_host *ha = pci_get_drvdata(pdev);
9590 
9591 	if (!is_aer_supported(ha))
9592 		return PCI_ERS_RESULT_NONE;
9593 
9594 	return PCI_ERS_RESULT_RECOVERED;
9595 }
9596 
9597 static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
9598 {
9599 	uint32_t rval = QLA_ERROR;
9600 	int fn;
9601 	struct pci_dev *other_pdev = NULL;
9602 
9603 	ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
9604 
9605 	set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
9606 
9607 	if (test_bit(AF_ONLINE, &ha->flags)) {
9608 		clear_bit(AF_ONLINE, &ha->flags);
9609 		clear_bit(AF_LINK_UP, &ha->flags);
9610 		iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
9611 		qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
9612 	}
9613 
9614 	fn = PCI_FUNC(ha->pdev->devfn);
9615 	if (is_qla8022(ha)) {
9616 		while (fn > 0) {
9617 			fn--;
9618 			ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at func %x\n",
9619 				   ha->host_no, __func__, fn);
9620 			/* Get the pci device given the domain, bus,
9621 			 * slot/function number */
9622 			other_pdev = pci_get_domain_bus_and_slot(
9623 					   pci_domain_nr(ha->pdev->bus),
9624 					   ha->pdev->bus->number,
9625 					   PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
9626 					   fn));
9627 
9628 			if (!other_pdev)
9629 				continue;
9630 
9631 			if (atomic_read(&other_pdev->enable_cnt)) {
9632 				ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI func in enabled state%x\n",
9633 					   ha->host_no, __func__, fn);
9634 				pci_dev_put(other_pdev);
9635 				break;
9636 			}
9637 			pci_dev_put(other_pdev);
9638 		}
9639 	} else {
9640 		/* this case is meant for ISP83xx/ISP84xx only */
9641 		if (qla4_83xx_can_perform_reset(ha)) {
9642 			/* reset fn as iSCSI is going to perform the reset */
9643 			fn = 0;
9644 		}
9645 	}
9646 
9647 	/* The first function on the card, the reset owner will
9648 	 * start & initialize the firmware. The other functions
9649 	 * on the card will reset the firmware context
9650 	 */
9651 	if (!fn) {
9652 		ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
9653 		    "0x%x is the owner\n", ha->host_no, __func__,
9654 		    ha->pdev->devfn);
9655 
9656 		ha->isp_ops->idc_lock(ha);
9657 		qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
9658 				    QLA8XXX_DEV_COLD);
9659 		ha->isp_ops->idc_unlock(ha);
9660 
9661 		rval = qla4_8xxx_update_idc_reg(ha);
9662 		if (rval == QLA_ERROR) {
9663 			ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: FAILED\n",
9664 				   ha->host_no, __func__);
9665 			ha->isp_ops->idc_lock(ha);
9666 			qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
9667 					    QLA8XXX_DEV_FAILED);
9668 			ha->isp_ops->idc_unlock(ha);
9669 			goto exit_error_recovery;
9670 		}
9671 
9672 		clear_bit(AF_FW_RECOVERY, &ha->flags);
9673 		rval = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
9674 
9675 		if (rval != QLA_SUCCESS) {
9676 			ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
9677 			    "FAILED\n", ha->host_no, __func__);
9678 			qla4xxx_free_irqs(ha);
9679 			ha->isp_ops->idc_lock(ha);
9680 			qla4_8xxx_clear_drv_active(ha);
9681 			qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
9682 					    QLA8XXX_DEV_FAILED);
9683 			ha->isp_ops->idc_unlock(ha);
9684 		} else {
9685 			ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
9686 			    "READY\n", ha->host_no, __func__);
9687 			ha->isp_ops->idc_lock(ha);
9688 			qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
9689 					    QLA8XXX_DEV_READY);
9690 			/* Clear driver state register */
9691 			qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DRV_STATE, 0);
9692 			qla4_8xxx_set_drv_active(ha);
9693 			ha->isp_ops->idc_unlock(ha);
9694 			ha->isp_ops->enable_intrs(ha);
9695 		}
9696 	} else {
9697 		ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
9698 		    "the reset owner\n", ha->host_no, __func__,
9699 		    ha->pdev->devfn);
9700 		if ((qla4_8xxx_rd_direct(ha, QLA8XXX_CRB_DEV_STATE) ==
9701 		     QLA8XXX_DEV_READY)) {
9702 			clear_bit(AF_FW_RECOVERY, &ha->flags);
9703 			rval = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
9704 			if (rval == QLA_SUCCESS)
9705 				ha->isp_ops->enable_intrs(ha);
9706 			else
9707 				qla4xxx_free_irqs(ha);
9708 
9709 			ha->isp_ops->idc_lock(ha);
9710 			qla4_8xxx_set_drv_active(ha);
9711 			ha->isp_ops->idc_unlock(ha);
9712 		}
9713 	}
9714 exit_error_recovery:
9715 	clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
9716 	return rval;
9717 }
9718 
9719 static pci_ers_result_t
9720 qla4xxx_pci_slot_reset(struct pci_dev *pdev)
9721 {
9722 	pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
9723 	struct scsi_qla_host *ha = pci_get_drvdata(pdev);
9724 	int rc;
9725 
9726 	ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
9727 	    ha->host_no, __func__);
9728 
9729 	if (!is_aer_supported(ha))
9730 		return PCI_ERS_RESULT_NONE;
9731 
9732 	/* Restore the saved state of PCIe device -
9733 	 * BAR registers, PCI Config space, PCIX, MSI,
9734 	 * IOV states
9735 	 */
9736 	pci_restore_state(pdev);
9737 
9738 	/* pci_restore_state() clears the saved_state flag of the device
9739 	 * save restored state which resets saved_state flag
9740 	 */
9741 	pci_save_state(pdev);
9742 
9743 	/* Initialize device or resume if in suspended state */
9744 	rc = pci_enable_device(pdev);
9745 	if (rc) {
9746 		ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Can't re-enable "
9747 		    "device after reset\n", ha->host_no, __func__);
9748 		goto exit_slot_reset;
9749 	}
9750 
9751 	ha->isp_ops->disable_intrs(ha);
9752 
9753 	if (is_qla80XX(ha)) {
9754 		if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
9755 			ret = PCI_ERS_RESULT_RECOVERED;
9756 			goto exit_slot_reset;
9757 		} else
9758 			goto exit_slot_reset;
9759 	}
9760 
9761 exit_slot_reset:
9762 	ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
9763 	    "device after reset\n", ha->host_no, __func__, ret);
9764 	return ret;
9765 }
9766 
9767 static void
9768 qla4xxx_pci_resume(struct pci_dev *pdev)
9769 {
9770 	struct scsi_qla_host *ha = pci_get_drvdata(pdev);
9771 	int ret;
9772 
9773 	ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
9774 	    ha->host_no, __func__);
9775 
9776 	ret = qla4xxx_wait_for_hba_online(ha);
9777 	if (ret != QLA_SUCCESS) {
9778 		ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
9779 		    "resume I/O from slot/link_reset\n", ha->host_no,
9780 		     __func__);
9781 	}
9782 
9783 	pci_cleanup_aer_uncorrect_error_status(pdev);
9784 	clear_bit(AF_EEH_BUSY, &ha->flags);
9785 }
9786 
9787 static const struct pci_error_handlers qla4xxx_err_handler = {
9788 	.error_detected = qla4xxx_pci_error_detected,
9789 	.mmio_enabled = qla4xxx_pci_mmio_enabled,
9790 	.slot_reset = qla4xxx_pci_slot_reset,
9791 	.resume = qla4xxx_pci_resume,
9792 };
9793 
9794 static struct pci_device_id qla4xxx_pci_tbl[] = {
9795 	{
9796 		.vendor		= PCI_VENDOR_ID_QLOGIC,
9797 		.device		= PCI_DEVICE_ID_QLOGIC_ISP4010,
9798 		.subvendor	= PCI_ANY_ID,
9799 		.subdevice	= PCI_ANY_ID,
9800 	},
9801 	{
9802 		.vendor		= PCI_VENDOR_ID_QLOGIC,
9803 		.device		= PCI_DEVICE_ID_QLOGIC_ISP4022,
9804 		.subvendor	= PCI_ANY_ID,
9805 		.subdevice	= PCI_ANY_ID,
9806 	},
9807 	{
9808 		.vendor		= PCI_VENDOR_ID_QLOGIC,
9809 		.device		= PCI_DEVICE_ID_QLOGIC_ISP4032,
9810 		.subvendor	= PCI_ANY_ID,
9811 		.subdevice	= PCI_ANY_ID,
9812 	},
9813 	{
9814 		.vendor         = PCI_VENDOR_ID_QLOGIC,
9815 		.device         = PCI_DEVICE_ID_QLOGIC_ISP8022,
9816 		.subvendor      = PCI_ANY_ID,
9817 		.subdevice      = PCI_ANY_ID,
9818 	},
9819 	{
9820 		.vendor		= PCI_VENDOR_ID_QLOGIC,
9821 		.device		= PCI_DEVICE_ID_QLOGIC_ISP8324,
9822 		.subvendor	= PCI_ANY_ID,
9823 		.subdevice	= PCI_ANY_ID,
9824 	},
9825 	{
9826 		.vendor		= PCI_VENDOR_ID_QLOGIC,
9827 		.device		= PCI_DEVICE_ID_QLOGIC_ISP8042,
9828 		.subvendor	= PCI_ANY_ID,
9829 		.subdevice	= PCI_ANY_ID,
9830 	},
9831 	{0, 0},
9832 };
9833 MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
9834 
9835 static struct pci_driver qla4xxx_pci_driver = {
9836 	.name		= DRIVER_NAME,
9837 	.id_table	= qla4xxx_pci_tbl,
9838 	.probe		= qla4xxx_probe_adapter,
9839 	.remove		= qla4xxx_remove_adapter,
9840 	.err_handler = &qla4xxx_err_handler,
9841 };
9842 
9843 static int __init qla4xxx_module_init(void)
9844 {
9845 	int ret;
9846 
9847 	if (ql4xqfulltracking)
9848 		qla4xxx_driver_template.track_queue_depth = 1;
9849 
9850 	/* Allocate cache for SRBs. */
9851 	srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
9852 				       SLAB_HWCACHE_ALIGN, NULL);
9853 	if (srb_cachep == NULL) {
9854 		printk(KERN_ERR
9855 		       "%s: Unable to allocate SRB cache..."
9856 		       "Failing load!\n", DRIVER_NAME);
9857 		ret = -ENOMEM;
9858 		goto no_srp_cache;
9859 	}
9860 
9861 	/* Derive version string. */
9862 	strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
9863 	if (ql4xextended_error_logging)
9864 		strcat(qla4xxx_version_str, "-debug");
9865 
9866 	qla4xxx_scsi_transport =
9867 		iscsi_register_transport(&qla4xxx_iscsi_transport);
9868 	if (!qla4xxx_scsi_transport){
9869 		ret = -ENODEV;
9870 		goto release_srb_cache;
9871 	}
9872 
9873 	ret = pci_register_driver(&qla4xxx_pci_driver);
9874 	if (ret)
9875 		goto unregister_transport;
9876 
9877 	printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
9878 	return 0;
9879 
9880 unregister_transport:
9881 	iscsi_unregister_transport(&qla4xxx_iscsi_transport);
9882 release_srb_cache:
9883 	kmem_cache_destroy(srb_cachep);
9884 no_srp_cache:
9885 	return ret;
9886 }
9887 
9888 static void __exit qla4xxx_module_exit(void)
9889 {
9890 	pci_unregister_driver(&qla4xxx_pci_driver);
9891 	iscsi_unregister_transport(&qla4xxx_iscsi_transport);
9892 	kmem_cache_destroy(srb_cachep);
9893 }
9894 
9895 module_init(qla4xxx_module_init);
9896 module_exit(qla4xxx_module_exit);
9897 
9898 MODULE_AUTHOR("QLogic Corporation");
9899 MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
9900 MODULE_LICENSE("GPL");
9901 MODULE_VERSION(QLA4XXX_DRIVER_VERSION);
9902