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