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