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