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