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