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