1 /*
2  * QLogic iSCSI HBA Driver
3  * Copyright (c)  2003-2010 QLogic Corporation
4  *
5  * See LICENSE.qla4xxx for copyright and licensing details.
6  */
7 #include <linux/moduleparam.h>
8 #include <linux/slab.h>
9 #include <linux/blkdev.h>
10 #include <linux/iscsi_boot_sysfs.h>
11 #include <linux/inet.h>
12 
13 #include <scsi/scsi_tcq.h>
14 #include <scsi/scsicam.h>
15 
16 #include "ql4_def.h"
17 #include "ql4_version.h"
18 #include "ql4_glbl.h"
19 #include "ql4_dbg.h"
20 #include "ql4_inline.h"
21 
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 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 = 0;
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 ql4xsess_recovery_tmo = QL4_SESS_RECOVERY_TMO;
72 module_param(ql4xsess_recovery_tmo, int, S_IRUGO);
73 MODULE_PARM_DESC(ql4xsess_recovery_tmo,
74 		"Target Session Recovery Timeout.\n"
75 		"\t\t  Default: 120 sec.");
76 
77 static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha);
78 /*
79  * SCSI host template entry points
80  */
81 static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
82 
83 /*
84  * iSCSI template entry points
85  */
86 static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
87 				  enum iscsi_param param, char *buf);
88 static int qla4xxx_host_get_param(struct Scsi_Host *shost,
89 				  enum iscsi_host_param param, char *buf);
90 static int qla4xxx_iface_set_param(struct Scsi_Host *shost, void *data,
91 				   uint32_t len);
92 static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
93 				   enum iscsi_param_type param_type,
94 				   int param, char *buf);
95 static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
96 static struct iscsi_endpoint *qla4xxx_ep_connect(struct Scsi_Host *shost,
97 						 struct sockaddr *dst_addr,
98 						 int non_blocking);
99 static int qla4xxx_ep_poll(struct iscsi_endpoint *ep, int timeout_ms);
100 static void qla4xxx_ep_disconnect(struct iscsi_endpoint *ep);
101 static int qla4xxx_get_ep_param(struct iscsi_endpoint *ep,
102 				enum iscsi_param param, char *buf);
103 static int qla4xxx_conn_start(struct iscsi_cls_conn *conn);
104 static struct iscsi_cls_conn *
105 qla4xxx_conn_create(struct iscsi_cls_session *cls_sess, uint32_t conn_idx);
106 static int qla4xxx_conn_bind(struct iscsi_cls_session *cls_session,
107 			     struct iscsi_cls_conn *cls_conn,
108 			     uint64_t transport_fd, int is_leading);
109 static void qla4xxx_conn_destroy(struct iscsi_cls_conn *conn);
110 static struct iscsi_cls_session *
111 qla4xxx_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max,
112 			uint16_t qdepth, uint32_t initial_cmdsn);
113 static void qla4xxx_session_destroy(struct iscsi_cls_session *sess);
114 static void qla4xxx_task_work(struct work_struct *wdata);
115 static int qla4xxx_alloc_pdu(struct iscsi_task *, uint8_t);
116 static int qla4xxx_task_xmit(struct iscsi_task *);
117 static void qla4xxx_task_cleanup(struct iscsi_task *);
118 static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session);
119 static void qla4xxx_conn_get_stats(struct iscsi_cls_conn *cls_conn,
120 				   struct iscsi_stats *stats);
121 /*
122  * SCSI host template entry points
123  */
124 static int qla4xxx_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmd);
125 static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
126 static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
127 static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
128 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
129 static int qla4xxx_slave_alloc(struct scsi_device *device);
130 static int qla4xxx_slave_configure(struct scsi_device *device);
131 static void qla4xxx_slave_destroy(struct scsi_device *sdev);
132 static umode_t ql4_attr_is_visible(int param_type, int param);
133 static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type);
134 
135 static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
136     QLA82XX_LEGACY_INTR_CONFIG;
137 
138 static struct scsi_host_template qla4xxx_driver_template = {
139 	.module			= THIS_MODULE,
140 	.name			= DRIVER_NAME,
141 	.proc_name		= DRIVER_NAME,
142 	.queuecommand		= qla4xxx_queuecommand,
143 
144 	.eh_abort_handler	= qla4xxx_eh_abort,
145 	.eh_device_reset_handler = qla4xxx_eh_device_reset,
146 	.eh_target_reset_handler = qla4xxx_eh_target_reset,
147 	.eh_host_reset_handler	= qla4xxx_eh_host_reset,
148 	.eh_timed_out		= qla4xxx_eh_cmd_timed_out,
149 
150 	.slave_configure	= qla4xxx_slave_configure,
151 	.slave_alloc		= qla4xxx_slave_alloc,
152 	.slave_destroy		= qla4xxx_slave_destroy,
153 
154 	.this_id		= -1,
155 	.cmd_per_lun		= 3,
156 	.use_clustering		= ENABLE_CLUSTERING,
157 	.sg_tablesize		= SG_ALL,
158 
159 	.max_sectors		= 0xFFFF,
160 	.shost_attrs		= qla4xxx_host_attrs,
161 	.host_reset		= qla4xxx_host_reset,
162 	.vendor_id		= SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_QLOGIC,
163 };
164 
165 static struct iscsi_transport qla4xxx_iscsi_transport = {
166 	.owner			= THIS_MODULE,
167 	.name			= DRIVER_NAME,
168 	.caps			= CAP_TEXT_NEGO |
169 				  CAP_DATA_PATH_OFFLOAD | CAP_HDRDGST |
170 				  CAP_DATADGST | CAP_LOGIN_OFFLOAD |
171 				  CAP_MULTI_R2T,
172 	.attr_is_visible	= ql4_attr_is_visible,
173 	.create_session         = qla4xxx_session_create,
174 	.destroy_session        = qla4xxx_session_destroy,
175 	.start_conn             = qla4xxx_conn_start,
176 	.create_conn            = qla4xxx_conn_create,
177 	.bind_conn              = qla4xxx_conn_bind,
178 	.stop_conn              = iscsi_conn_stop,
179 	.destroy_conn           = qla4xxx_conn_destroy,
180 	.set_param              = iscsi_set_param,
181 	.get_conn_param		= qla4xxx_conn_get_param,
182 	.get_session_param	= iscsi_session_get_param,
183 	.get_ep_param           = qla4xxx_get_ep_param,
184 	.ep_connect		= qla4xxx_ep_connect,
185 	.ep_poll		= qla4xxx_ep_poll,
186 	.ep_disconnect		= qla4xxx_ep_disconnect,
187 	.get_stats		= qla4xxx_conn_get_stats,
188 	.send_pdu		= iscsi_conn_send_pdu,
189 	.xmit_task		= qla4xxx_task_xmit,
190 	.cleanup_task		= qla4xxx_task_cleanup,
191 	.alloc_pdu		= qla4xxx_alloc_pdu,
192 
193 	.get_host_param		= qla4xxx_host_get_param,
194 	.set_iface_param	= qla4xxx_iface_set_param,
195 	.get_iface_param	= qla4xxx_get_iface_param,
196 	.bsg_request		= qla4xxx_bsg_request,
197 };
198 
199 static struct scsi_transport_template *qla4xxx_scsi_transport;
200 
ql4_attr_is_visible(int param_type,int param)201 static umode_t ql4_attr_is_visible(int param_type, int param)
202 {
203 	switch (param_type) {
204 	case ISCSI_HOST_PARAM:
205 		switch (param) {
206 		case ISCSI_HOST_PARAM_HWADDRESS:
207 		case ISCSI_HOST_PARAM_IPADDRESS:
208 		case ISCSI_HOST_PARAM_INITIATOR_NAME:
209 			return S_IRUGO;
210 		default:
211 			return 0;
212 		}
213 	case ISCSI_PARAM:
214 		switch (param) {
215 		case ISCSI_PARAM_PERSISTENT_ADDRESS:
216 		case ISCSI_PARAM_PERSISTENT_PORT:
217 		case ISCSI_PARAM_CONN_ADDRESS:
218 		case ISCSI_PARAM_CONN_PORT:
219 		case ISCSI_PARAM_TARGET_NAME:
220 		case ISCSI_PARAM_TPGT:
221 		case ISCSI_PARAM_TARGET_ALIAS:
222 		case ISCSI_PARAM_MAX_BURST:
223 		case ISCSI_PARAM_MAX_R2T:
224 		case ISCSI_PARAM_FIRST_BURST:
225 		case ISCSI_PARAM_MAX_RECV_DLENGTH:
226 		case ISCSI_PARAM_MAX_XMIT_DLENGTH:
227 		case ISCSI_PARAM_IFACE_NAME:
228 			return S_IRUGO;
229 		default:
230 			return 0;
231 		}
232 	case ISCSI_NET_PARAM:
233 		switch (param) {
234 		case ISCSI_NET_PARAM_IPV4_ADDR:
235 		case ISCSI_NET_PARAM_IPV4_SUBNET:
236 		case ISCSI_NET_PARAM_IPV4_GW:
237 		case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
238 		case ISCSI_NET_PARAM_IFACE_ENABLE:
239 		case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
240 		case ISCSI_NET_PARAM_IPV6_ADDR:
241 		case ISCSI_NET_PARAM_IPV6_ROUTER:
242 		case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
243 		case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
244 		case ISCSI_NET_PARAM_VLAN_ID:
245 		case ISCSI_NET_PARAM_VLAN_PRIORITY:
246 		case ISCSI_NET_PARAM_VLAN_ENABLED:
247 		case ISCSI_NET_PARAM_MTU:
248 		case ISCSI_NET_PARAM_PORT:
249 			return S_IRUGO;
250 		default:
251 			return 0;
252 		}
253 	}
254 
255 	return 0;
256 }
257 
qla4xxx_get_iface_param(struct iscsi_iface * iface,enum iscsi_param_type param_type,int param,char * buf)258 static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
259 				   enum iscsi_param_type param_type,
260 				   int param, char *buf)
261 {
262 	struct Scsi_Host *shost = iscsi_iface_to_shost(iface);
263 	struct scsi_qla_host *ha = to_qla_host(shost);
264 	int len = -ENOSYS;
265 
266 	if (param_type != ISCSI_NET_PARAM)
267 		return -ENOSYS;
268 
269 	switch (param) {
270 	case ISCSI_NET_PARAM_IPV4_ADDR:
271 		len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
272 		break;
273 	case ISCSI_NET_PARAM_IPV4_SUBNET:
274 		len = sprintf(buf, "%pI4\n", &ha->ip_config.subnet_mask);
275 		break;
276 	case ISCSI_NET_PARAM_IPV4_GW:
277 		len = sprintf(buf, "%pI4\n", &ha->ip_config.gateway);
278 		break;
279 	case ISCSI_NET_PARAM_IFACE_ENABLE:
280 		if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
281 			len = sprintf(buf, "%s\n",
282 				      (ha->ip_config.ipv4_options &
283 				       IPOPT_IPV4_PROTOCOL_ENABLE) ?
284 				      "enabled" : "disabled");
285 		else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
286 			len = sprintf(buf, "%s\n",
287 				      (ha->ip_config.ipv6_options &
288 				       IPV6_OPT_IPV6_PROTOCOL_ENABLE) ?
289 				       "enabled" : "disabled");
290 		break;
291 	case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
292 		len = sprintf(buf, "%s\n",
293 			      (ha->ip_config.tcp_options & TCPOPT_DHCP_ENABLE) ?
294 			      "dhcp" : "static");
295 		break;
296 	case ISCSI_NET_PARAM_IPV6_ADDR:
297 		if (iface->iface_num == 0)
298 			len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr0);
299 		if (iface->iface_num == 1)
300 			len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr1);
301 		break;
302 	case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
303 		len = sprintf(buf, "%pI6\n",
304 			      &ha->ip_config.ipv6_link_local_addr);
305 		break;
306 	case ISCSI_NET_PARAM_IPV6_ROUTER:
307 		len = sprintf(buf, "%pI6\n",
308 			      &ha->ip_config.ipv6_default_router_addr);
309 		break;
310 	case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
311 		len = sprintf(buf, "%s\n",
312 			      (ha->ip_config.ipv6_addl_options &
313 			       IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) ?
314 			       "nd" : "static");
315 		break;
316 	case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
317 		len = sprintf(buf, "%s\n",
318 			      (ha->ip_config.ipv6_addl_options &
319 			       IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR) ?
320 			       "auto" : "static");
321 		break;
322 	case ISCSI_NET_PARAM_VLAN_ID:
323 		if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
324 			len = sprintf(buf, "%d\n",
325 				      (ha->ip_config.ipv4_vlan_tag &
326 				       ISCSI_MAX_VLAN_ID));
327 		else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
328 			len = sprintf(buf, "%d\n",
329 				      (ha->ip_config.ipv6_vlan_tag &
330 				       ISCSI_MAX_VLAN_ID));
331 		break;
332 	case ISCSI_NET_PARAM_VLAN_PRIORITY:
333 		if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
334 			len = sprintf(buf, "%d\n",
335 				      ((ha->ip_config.ipv4_vlan_tag >> 13) &
336 					ISCSI_MAX_VLAN_PRIORITY));
337 		else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
338 			len = sprintf(buf, "%d\n",
339 				      ((ha->ip_config.ipv6_vlan_tag >> 13) &
340 					ISCSI_MAX_VLAN_PRIORITY));
341 		break;
342 	case ISCSI_NET_PARAM_VLAN_ENABLED:
343 		if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
344 			len = sprintf(buf, "%s\n",
345 				      (ha->ip_config.ipv4_options &
346 				       IPOPT_VLAN_TAGGING_ENABLE) ?
347 				       "enabled" : "disabled");
348 		else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
349 			len = sprintf(buf, "%s\n",
350 				      (ha->ip_config.ipv6_options &
351 				       IPV6_OPT_VLAN_TAGGING_ENABLE) ?
352 				       "enabled" : "disabled");
353 		break;
354 	case ISCSI_NET_PARAM_MTU:
355 		len = sprintf(buf, "%d\n", ha->ip_config.eth_mtu_size);
356 		break;
357 	case ISCSI_NET_PARAM_PORT:
358 		if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
359 			len = sprintf(buf, "%d\n", ha->ip_config.ipv4_port);
360 		else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
361 			len = sprintf(buf, "%d\n", ha->ip_config.ipv6_port);
362 		break;
363 	default:
364 		len = -ENOSYS;
365 	}
366 
367 	return len;
368 }
369 
370 static struct iscsi_endpoint *
qla4xxx_ep_connect(struct Scsi_Host * shost,struct sockaddr * dst_addr,int non_blocking)371 qla4xxx_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
372 		   int non_blocking)
373 {
374 	int ret;
375 	struct iscsi_endpoint *ep;
376 	struct qla_endpoint *qla_ep;
377 	struct scsi_qla_host *ha;
378 	struct sockaddr_in *addr;
379 	struct sockaddr_in6 *addr6;
380 
381 	DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
382 	if (!shost) {
383 		ret = -ENXIO;
384 		printk(KERN_ERR "%s: shost is NULL\n",
385 		       __func__);
386 		return ERR_PTR(ret);
387 	}
388 
389 	ha = iscsi_host_priv(shost);
390 
391 	ep = iscsi_create_endpoint(sizeof(struct qla_endpoint));
392 	if (!ep) {
393 		ret = -ENOMEM;
394 		return ERR_PTR(ret);
395 	}
396 
397 	qla_ep = ep->dd_data;
398 	memset(qla_ep, 0, sizeof(struct qla_endpoint));
399 	if (dst_addr->sa_family == AF_INET) {
400 		memcpy(&qla_ep->dst_addr, dst_addr, sizeof(struct sockaddr_in));
401 		addr = (struct sockaddr_in *)&qla_ep->dst_addr;
402 		DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI4\n", __func__,
403 				  (char *)&addr->sin_addr));
404 	} else if (dst_addr->sa_family == AF_INET6) {
405 		memcpy(&qla_ep->dst_addr, dst_addr,
406 		       sizeof(struct sockaddr_in6));
407 		addr6 = (struct sockaddr_in6 *)&qla_ep->dst_addr;
408 		DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI6\n", __func__,
409 				  (char *)&addr6->sin6_addr));
410 	}
411 
412 	qla_ep->host = shost;
413 
414 	return ep;
415 }
416 
qla4xxx_ep_poll(struct iscsi_endpoint * ep,int timeout_ms)417 static int qla4xxx_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
418 {
419 	struct qla_endpoint *qla_ep;
420 	struct scsi_qla_host *ha;
421 	int ret = 0;
422 
423 	DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
424 	qla_ep = ep->dd_data;
425 	ha = to_qla_host(qla_ep->host);
426 
427 	if (adapter_up(ha) && !test_bit(AF_BUILD_DDB_LIST, &ha->flags))
428 		ret = 1;
429 
430 	return ret;
431 }
432 
qla4xxx_ep_disconnect(struct iscsi_endpoint * ep)433 static void qla4xxx_ep_disconnect(struct iscsi_endpoint *ep)
434 {
435 	DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
436 	iscsi_destroy_endpoint(ep);
437 }
438 
qla4xxx_get_ep_param(struct iscsi_endpoint * ep,enum iscsi_param param,char * buf)439 static int qla4xxx_get_ep_param(struct iscsi_endpoint *ep,
440 				enum iscsi_param param,
441 				char *buf)
442 {
443 	struct qla_endpoint *qla_ep = ep->dd_data;
444 	struct sockaddr *dst_addr;
445 
446 	DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
447 
448 	switch (param) {
449 	case ISCSI_PARAM_CONN_PORT:
450 	case ISCSI_PARAM_CONN_ADDRESS:
451 		if (!qla_ep)
452 			return -ENOTCONN;
453 
454 		dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
455 		if (!dst_addr)
456 			return -ENOTCONN;
457 
458 		return iscsi_conn_get_addr_param((struct sockaddr_storage *)
459 						 &qla_ep->dst_addr, param, buf);
460 	default:
461 		return -ENOSYS;
462 	}
463 }
464 
qla4xxx_conn_get_stats(struct iscsi_cls_conn * cls_conn,struct iscsi_stats * stats)465 static void qla4xxx_conn_get_stats(struct iscsi_cls_conn *cls_conn,
466 				   struct iscsi_stats *stats)
467 {
468 	struct iscsi_session *sess;
469 	struct iscsi_cls_session *cls_sess;
470 	struct ddb_entry *ddb_entry;
471 	struct scsi_qla_host *ha;
472 	struct ql_iscsi_stats *ql_iscsi_stats;
473 	int stats_size;
474 	int ret;
475 	dma_addr_t iscsi_stats_dma;
476 
477 	DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
478 
479 	cls_sess = iscsi_conn_to_session(cls_conn);
480 	sess = cls_sess->dd_data;
481 	ddb_entry = sess->dd_data;
482 	ha = ddb_entry->ha;
483 
484 	stats_size = PAGE_ALIGN(sizeof(struct ql_iscsi_stats));
485 	/* Allocate memory */
486 	ql_iscsi_stats = dma_alloc_coherent(&ha->pdev->dev, stats_size,
487 					    &iscsi_stats_dma, GFP_KERNEL);
488 	if (!ql_iscsi_stats) {
489 		ql4_printk(KERN_ERR, ha,
490 			   "Unable to allocate memory for iscsi stats\n");
491 		goto exit_get_stats;
492 	}
493 
494 	ret =  qla4xxx_get_mgmt_data(ha, ddb_entry->fw_ddb_index, stats_size,
495 				     iscsi_stats_dma);
496 	if (ret != QLA_SUCCESS) {
497 		ql4_printk(KERN_ERR, ha,
498 			   "Unable to retreive iscsi stats\n");
499 		goto free_stats;
500 	}
501 
502 	/* octets */
503 	stats->txdata_octets = le64_to_cpu(ql_iscsi_stats->tx_data_octets);
504 	stats->rxdata_octets = le64_to_cpu(ql_iscsi_stats->rx_data_octets);
505 	/* xmit pdus */
506 	stats->noptx_pdus = le32_to_cpu(ql_iscsi_stats->tx_nopout_pdus);
507 	stats->scsicmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_cmd_pdus);
508 	stats->tmfcmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_tmf_cmd_pdus);
509 	stats->login_pdus = le32_to_cpu(ql_iscsi_stats->tx_login_cmd_pdus);
510 	stats->text_pdus = le32_to_cpu(ql_iscsi_stats->tx_text_cmd_pdus);
511 	stats->dataout_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_write_pdus);
512 	stats->logout_pdus = le32_to_cpu(ql_iscsi_stats->tx_logout_cmd_pdus);
513 	stats->snack_pdus = le32_to_cpu(ql_iscsi_stats->tx_snack_req_pdus);
514 	/* recv pdus */
515 	stats->noprx_pdus = le32_to_cpu(ql_iscsi_stats->rx_nopin_pdus);
516 	stats->scsirsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_resp_pdus);
517 	stats->tmfrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_tmf_resp_pdus);
518 	stats->textrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_text_resp_pdus);
519 	stats->datain_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_read_pdus);
520 	stats->logoutrsp_pdus =
521 			le32_to_cpu(ql_iscsi_stats->rx_logout_resp_pdus);
522 	stats->r2t_pdus = le32_to_cpu(ql_iscsi_stats->rx_r2t_pdus);
523 	stats->async_pdus = le32_to_cpu(ql_iscsi_stats->rx_async_pdus);
524 	stats->rjt_pdus = le32_to_cpu(ql_iscsi_stats->rx_reject_pdus);
525 
526 free_stats:
527 	dma_free_coherent(&ha->pdev->dev, stats_size, ql_iscsi_stats,
528 			  iscsi_stats_dma);
529 exit_get_stats:
530 	return;
531 }
532 
qla4xxx_eh_cmd_timed_out(struct scsi_cmnd * sc)533 static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
534 {
535 	struct iscsi_cls_session *session;
536 	struct iscsi_session *sess;
537 	unsigned long flags;
538 	enum blk_eh_timer_return ret = BLK_EH_NOT_HANDLED;
539 
540 	session = starget_to_session(scsi_target(sc->device));
541 	sess = session->dd_data;
542 
543 	spin_lock_irqsave(&session->lock, flags);
544 	if (session->state == ISCSI_SESSION_FAILED)
545 		ret = BLK_EH_RESET_TIMER;
546 	spin_unlock_irqrestore(&session->lock, flags);
547 
548 	return ret;
549 }
550 
qla4xxx_host_get_param(struct Scsi_Host * shost,enum iscsi_host_param param,char * buf)551 static int qla4xxx_host_get_param(struct Scsi_Host *shost,
552 				  enum iscsi_host_param param, char *buf)
553 {
554 	struct scsi_qla_host *ha = to_qla_host(shost);
555 	int len;
556 
557 	switch (param) {
558 	case ISCSI_HOST_PARAM_HWADDRESS:
559 		len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
560 		break;
561 	case ISCSI_HOST_PARAM_IPADDRESS:
562 		len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
563 		break;
564 	case ISCSI_HOST_PARAM_INITIATOR_NAME:
565 		len = sprintf(buf, "%s\n", ha->name_string);
566 		break;
567 	default:
568 		return -ENOSYS;
569 	}
570 
571 	return len;
572 }
573 
qla4xxx_create_ipv4_iface(struct scsi_qla_host * ha)574 static void qla4xxx_create_ipv4_iface(struct scsi_qla_host *ha)
575 {
576 	if (ha->iface_ipv4)
577 		return;
578 
579 	/* IPv4 */
580 	ha->iface_ipv4 = iscsi_create_iface(ha->host,
581 					    &qla4xxx_iscsi_transport,
582 					    ISCSI_IFACE_TYPE_IPV4, 0, 0);
583 	if (!ha->iface_ipv4)
584 		ql4_printk(KERN_ERR, ha, "Could not create IPv4 iSCSI "
585 			   "iface0.\n");
586 }
587 
qla4xxx_create_ipv6_iface(struct scsi_qla_host * ha)588 static void qla4xxx_create_ipv6_iface(struct scsi_qla_host *ha)
589 {
590 	if (!ha->iface_ipv6_0)
591 		/* IPv6 iface-0 */
592 		ha->iface_ipv6_0 = iscsi_create_iface(ha->host,
593 						      &qla4xxx_iscsi_transport,
594 						      ISCSI_IFACE_TYPE_IPV6, 0,
595 						      0);
596 	if (!ha->iface_ipv6_0)
597 		ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
598 			   "iface0.\n");
599 
600 	if (!ha->iface_ipv6_1)
601 		/* IPv6 iface-1 */
602 		ha->iface_ipv6_1 = iscsi_create_iface(ha->host,
603 						      &qla4xxx_iscsi_transport,
604 						      ISCSI_IFACE_TYPE_IPV6, 1,
605 						      0);
606 	if (!ha->iface_ipv6_1)
607 		ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
608 			   "iface1.\n");
609 }
610 
qla4xxx_create_ifaces(struct scsi_qla_host * ha)611 static void qla4xxx_create_ifaces(struct scsi_qla_host *ha)
612 {
613 	if (ha->ip_config.ipv4_options & IPOPT_IPV4_PROTOCOL_ENABLE)
614 		qla4xxx_create_ipv4_iface(ha);
615 
616 	if (ha->ip_config.ipv6_options & IPV6_OPT_IPV6_PROTOCOL_ENABLE)
617 		qla4xxx_create_ipv6_iface(ha);
618 }
619 
qla4xxx_destroy_ipv4_iface(struct scsi_qla_host * ha)620 static void qla4xxx_destroy_ipv4_iface(struct scsi_qla_host *ha)
621 {
622 	if (ha->iface_ipv4) {
623 		iscsi_destroy_iface(ha->iface_ipv4);
624 		ha->iface_ipv4 = NULL;
625 	}
626 }
627 
qla4xxx_destroy_ipv6_iface(struct scsi_qla_host * ha)628 static void qla4xxx_destroy_ipv6_iface(struct scsi_qla_host *ha)
629 {
630 	if (ha->iface_ipv6_0) {
631 		iscsi_destroy_iface(ha->iface_ipv6_0);
632 		ha->iface_ipv6_0 = NULL;
633 	}
634 	if (ha->iface_ipv6_1) {
635 		iscsi_destroy_iface(ha->iface_ipv6_1);
636 		ha->iface_ipv6_1 = NULL;
637 	}
638 }
639 
qla4xxx_destroy_ifaces(struct scsi_qla_host * ha)640 static void qla4xxx_destroy_ifaces(struct scsi_qla_host *ha)
641 {
642 	qla4xxx_destroy_ipv4_iface(ha);
643 	qla4xxx_destroy_ipv6_iface(ha);
644 }
645 
qla4xxx_set_ipv6(struct scsi_qla_host * ha,struct iscsi_iface_param_info * iface_param,struct addr_ctrl_blk * init_fw_cb)646 static void qla4xxx_set_ipv6(struct scsi_qla_host *ha,
647 			     struct iscsi_iface_param_info *iface_param,
648 			     struct addr_ctrl_blk *init_fw_cb)
649 {
650 	/*
651 	 * iface_num 0 is valid for IPv6 Addr, linklocal, router, autocfg.
652 	 * iface_num 1 is valid only for IPv6 Addr.
653 	 */
654 	switch (iface_param->param) {
655 	case ISCSI_NET_PARAM_IPV6_ADDR:
656 		if (iface_param->iface_num & 0x1)
657 			/* IPv6 Addr 1 */
658 			memcpy(init_fw_cb->ipv6_addr1, iface_param->value,
659 			       sizeof(init_fw_cb->ipv6_addr1));
660 		else
661 			/* IPv6 Addr 0 */
662 			memcpy(init_fw_cb->ipv6_addr0, iface_param->value,
663 			       sizeof(init_fw_cb->ipv6_addr0));
664 		break;
665 	case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
666 		if (iface_param->iface_num & 0x1)
667 			break;
668 		memcpy(init_fw_cb->ipv6_if_id, &iface_param->value[8],
669 		       sizeof(init_fw_cb->ipv6_if_id));
670 		break;
671 	case ISCSI_NET_PARAM_IPV6_ROUTER:
672 		if (iface_param->iface_num & 0x1)
673 			break;
674 		memcpy(init_fw_cb->ipv6_dflt_rtr_addr, iface_param->value,
675 		       sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
676 		break;
677 	case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
678 		/* Autocfg applies to even interface */
679 		if (iface_param->iface_num & 0x1)
680 			break;
681 
682 		if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_DISABLE)
683 			init_fw_cb->ipv6_addtl_opts &=
684 				cpu_to_le16(
685 				  ~IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
686 		else if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_ND_ENABLE)
687 			init_fw_cb->ipv6_addtl_opts |=
688 				cpu_to_le16(
689 				  IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
690 		else
691 			ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
692 				   "IPv6 addr\n");
693 		break;
694 	case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
695 		/* Autocfg applies to even interface */
696 		if (iface_param->iface_num & 0x1)
697 			break;
698 
699 		if (iface_param->value[0] ==
700 		    ISCSI_IPV6_LINKLOCAL_AUTOCFG_ENABLE)
701 			init_fw_cb->ipv6_addtl_opts |= cpu_to_le16(
702 					IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
703 		else if (iface_param->value[0] ==
704 			 ISCSI_IPV6_LINKLOCAL_AUTOCFG_DISABLE)
705 			init_fw_cb->ipv6_addtl_opts &= cpu_to_le16(
706 				       ~IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
707 		else
708 			ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
709 				   "IPv6 linklocal addr\n");
710 		break;
711 	case ISCSI_NET_PARAM_IPV6_ROUTER_AUTOCFG:
712 		/* Autocfg applies to even interface */
713 		if (iface_param->iface_num & 0x1)
714 			break;
715 
716 		if (iface_param->value[0] == ISCSI_IPV6_ROUTER_AUTOCFG_ENABLE)
717 			memset(init_fw_cb->ipv6_dflt_rtr_addr, 0,
718 			       sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
719 		break;
720 	case ISCSI_NET_PARAM_IFACE_ENABLE:
721 		if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
722 			init_fw_cb->ipv6_opts |=
723 				cpu_to_le16(IPV6_OPT_IPV6_PROTOCOL_ENABLE);
724 			qla4xxx_create_ipv6_iface(ha);
725 		} else {
726 			init_fw_cb->ipv6_opts &=
727 				cpu_to_le16(~IPV6_OPT_IPV6_PROTOCOL_ENABLE &
728 					    0xFFFF);
729 			qla4xxx_destroy_ipv6_iface(ha);
730 		}
731 		break;
732 	case ISCSI_NET_PARAM_VLAN_TAG:
733 		if (iface_param->len != sizeof(init_fw_cb->ipv6_vlan_tag))
734 			break;
735 		init_fw_cb->ipv6_vlan_tag =
736 				cpu_to_be16(*(uint16_t *)iface_param->value);
737 		break;
738 	case ISCSI_NET_PARAM_VLAN_ENABLED:
739 		if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
740 			init_fw_cb->ipv6_opts |=
741 				cpu_to_le16(IPV6_OPT_VLAN_TAGGING_ENABLE);
742 		else
743 			init_fw_cb->ipv6_opts &=
744 				cpu_to_le16(~IPV6_OPT_VLAN_TAGGING_ENABLE);
745 		break;
746 	case ISCSI_NET_PARAM_MTU:
747 		init_fw_cb->eth_mtu_size =
748 				cpu_to_le16(*(uint16_t *)iface_param->value);
749 		break;
750 	case ISCSI_NET_PARAM_PORT:
751 		/* Autocfg applies to even interface */
752 		if (iface_param->iface_num & 0x1)
753 			break;
754 
755 		init_fw_cb->ipv6_port =
756 				cpu_to_le16(*(uint16_t *)iface_param->value);
757 		break;
758 	default:
759 		ql4_printk(KERN_ERR, ha, "Unknown IPv6 param = %d\n",
760 			   iface_param->param);
761 		break;
762 	}
763 }
764 
qla4xxx_set_ipv4(struct scsi_qla_host * ha,struct iscsi_iface_param_info * iface_param,struct addr_ctrl_blk * init_fw_cb)765 static void qla4xxx_set_ipv4(struct scsi_qla_host *ha,
766 			     struct iscsi_iface_param_info *iface_param,
767 			     struct addr_ctrl_blk *init_fw_cb)
768 {
769 	switch (iface_param->param) {
770 	case ISCSI_NET_PARAM_IPV4_ADDR:
771 		memcpy(init_fw_cb->ipv4_addr, iface_param->value,
772 		       sizeof(init_fw_cb->ipv4_addr));
773 		break;
774 	case ISCSI_NET_PARAM_IPV4_SUBNET:
775 		memcpy(init_fw_cb->ipv4_subnet,	iface_param->value,
776 		       sizeof(init_fw_cb->ipv4_subnet));
777 		break;
778 	case ISCSI_NET_PARAM_IPV4_GW:
779 		memcpy(init_fw_cb->ipv4_gw_addr, iface_param->value,
780 		       sizeof(init_fw_cb->ipv4_gw_addr));
781 		break;
782 	case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
783 		if (iface_param->value[0] == ISCSI_BOOTPROTO_DHCP)
784 			init_fw_cb->ipv4_tcp_opts |=
785 					cpu_to_le16(TCPOPT_DHCP_ENABLE);
786 		else if (iface_param->value[0] == ISCSI_BOOTPROTO_STATIC)
787 			init_fw_cb->ipv4_tcp_opts &=
788 					cpu_to_le16(~TCPOPT_DHCP_ENABLE);
789 		else
790 			ql4_printk(KERN_ERR, ha, "Invalid IPv4 bootproto\n");
791 		break;
792 	case ISCSI_NET_PARAM_IFACE_ENABLE:
793 		if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
794 			init_fw_cb->ipv4_ip_opts |=
795 				cpu_to_le16(IPOPT_IPV4_PROTOCOL_ENABLE);
796 			qla4xxx_create_ipv4_iface(ha);
797 		} else {
798 			init_fw_cb->ipv4_ip_opts &=
799 				cpu_to_le16(~IPOPT_IPV4_PROTOCOL_ENABLE &
800 					    0xFFFF);
801 			qla4xxx_destroy_ipv4_iface(ha);
802 		}
803 		break;
804 	case ISCSI_NET_PARAM_VLAN_TAG:
805 		if (iface_param->len != sizeof(init_fw_cb->ipv4_vlan_tag))
806 			break;
807 		init_fw_cb->ipv4_vlan_tag =
808 				cpu_to_be16(*(uint16_t *)iface_param->value);
809 		break;
810 	case ISCSI_NET_PARAM_VLAN_ENABLED:
811 		if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
812 			init_fw_cb->ipv4_ip_opts |=
813 					cpu_to_le16(IPOPT_VLAN_TAGGING_ENABLE);
814 		else
815 			init_fw_cb->ipv4_ip_opts &=
816 					cpu_to_le16(~IPOPT_VLAN_TAGGING_ENABLE);
817 		break;
818 	case ISCSI_NET_PARAM_MTU:
819 		init_fw_cb->eth_mtu_size =
820 				cpu_to_le16(*(uint16_t *)iface_param->value);
821 		break;
822 	case ISCSI_NET_PARAM_PORT:
823 		init_fw_cb->ipv4_port =
824 				cpu_to_le16(*(uint16_t *)iface_param->value);
825 		break;
826 	default:
827 		ql4_printk(KERN_ERR, ha, "Unknown IPv4 param = %d\n",
828 			   iface_param->param);
829 		break;
830 	}
831 }
832 
833 static void
qla4xxx_initcb_to_acb(struct addr_ctrl_blk * init_fw_cb)834 qla4xxx_initcb_to_acb(struct addr_ctrl_blk *init_fw_cb)
835 {
836 	struct addr_ctrl_blk_def *acb;
837 	acb = (struct addr_ctrl_blk_def *)init_fw_cb;
838 	memset(acb->reserved1, 0, sizeof(acb->reserved1));
839 	memset(acb->reserved2, 0, sizeof(acb->reserved2));
840 	memset(acb->reserved3, 0, sizeof(acb->reserved3));
841 	memset(acb->reserved4, 0, sizeof(acb->reserved4));
842 	memset(acb->reserved5, 0, sizeof(acb->reserved5));
843 	memset(acb->reserved6, 0, sizeof(acb->reserved6));
844 	memset(acb->reserved7, 0, sizeof(acb->reserved7));
845 	memset(acb->reserved8, 0, sizeof(acb->reserved8));
846 	memset(acb->reserved9, 0, sizeof(acb->reserved9));
847 	memset(acb->reserved10, 0, sizeof(acb->reserved10));
848 	memset(acb->reserved11, 0, sizeof(acb->reserved11));
849 	memset(acb->reserved12, 0, sizeof(acb->reserved12));
850 	memset(acb->reserved13, 0, sizeof(acb->reserved13));
851 	memset(acb->reserved14, 0, sizeof(acb->reserved14));
852 	memset(acb->reserved15, 0, sizeof(acb->reserved15));
853 }
854 
855 static int
qla4xxx_iface_set_param(struct Scsi_Host * shost,void * data,uint32_t len)856 qla4xxx_iface_set_param(struct Scsi_Host *shost, void *data, uint32_t len)
857 {
858 	struct scsi_qla_host *ha = to_qla_host(shost);
859 	int rval = 0;
860 	struct iscsi_iface_param_info *iface_param = NULL;
861 	struct addr_ctrl_blk *init_fw_cb = NULL;
862 	dma_addr_t init_fw_cb_dma;
863 	uint32_t mbox_cmd[MBOX_REG_COUNT];
864 	uint32_t mbox_sts[MBOX_REG_COUNT];
865 	uint32_t rem = len;
866 	struct nlattr *attr;
867 
868 	init_fw_cb = dma_alloc_coherent(&ha->pdev->dev,
869 					sizeof(struct addr_ctrl_blk),
870 					&init_fw_cb_dma, GFP_KERNEL);
871 	if (!init_fw_cb) {
872 		ql4_printk(KERN_ERR, ha, "%s: Unable to alloc init_cb\n",
873 			   __func__);
874 		return -ENOMEM;
875 	}
876 
877 	memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
878 	memset(&mbox_cmd, 0, sizeof(mbox_cmd));
879 	memset(&mbox_sts, 0, sizeof(mbox_sts));
880 
881 	if (qla4xxx_get_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma)) {
882 		ql4_printk(KERN_ERR, ha, "%s: get ifcb failed\n", __func__);
883 		rval = -EIO;
884 		goto exit_init_fw_cb;
885 	}
886 
887 	nla_for_each_attr(attr, data, len, rem) {
888 		iface_param = nla_data(attr);
889 
890 		if (iface_param->param_type != ISCSI_NET_PARAM)
891 			continue;
892 
893 		switch (iface_param->iface_type) {
894 		case ISCSI_IFACE_TYPE_IPV4:
895 			switch (iface_param->iface_num) {
896 			case 0:
897 				qla4xxx_set_ipv4(ha, iface_param, init_fw_cb);
898 				break;
899 			default:
900 				/* Cannot have more than one IPv4 interface */
901 				ql4_printk(KERN_ERR, ha, "Invalid IPv4 iface "
902 					   "number = %d\n",
903 					   iface_param->iface_num);
904 				break;
905 			}
906 			break;
907 		case ISCSI_IFACE_TYPE_IPV6:
908 			switch (iface_param->iface_num) {
909 			case 0:
910 			case 1:
911 				qla4xxx_set_ipv6(ha, iface_param, init_fw_cb);
912 				break;
913 			default:
914 				/* Cannot have more than two IPv6 interface */
915 				ql4_printk(KERN_ERR, ha, "Invalid IPv6 iface "
916 					   "number = %d\n",
917 					   iface_param->iface_num);
918 				break;
919 			}
920 			break;
921 		default:
922 			ql4_printk(KERN_ERR, ha, "Invalid iface type\n");
923 			break;
924 		}
925 	}
926 
927 	init_fw_cb->cookie = cpu_to_le32(0x11BEAD5A);
928 
929 	rval = qla4xxx_set_flash(ha, init_fw_cb_dma, FLASH_SEGMENT_IFCB,
930 				 sizeof(struct addr_ctrl_blk),
931 				 FLASH_OPT_RMW_COMMIT);
932 	if (rval != QLA_SUCCESS) {
933 		ql4_printk(KERN_ERR, ha, "%s: set flash mbx failed\n",
934 			   __func__);
935 		rval = -EIO;
936 		goto exit_init_fw_cb;
937 	}
938 
939 	rval = qla4xxx_disable_acb(ha);
940 	if (rval != QLA_SUCCESS) {
941 		ql4_printk(KERN_ERR, ha, "%s: disable acb mbx failed\n",
942 			   __func__);
943 		rval = -EIO;
944 		goto exit_init_fw_cb;
945 	}
946 
947 	wait_for_completion_timeout(&ha->disable_acb_comp,
948 				    DISABLE_ACB_TOV * HZ);
949 
950 	qla4xxx_initcb_to_acb(init_fw_cb);
951 
952 	rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma);
953 	if (rval != QLA_SUCCESS) {
954 		ql4_printk(KERN_ERR, ha, "%s: set acb mbx failed\n",
955 			   __func__);
956 		rval = -EIO;
957 		goto exit_init_fw_cb;
958 	}
959 
960 	memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
961 	qla4xxx_update_local_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb,
962 				  init_fw_cb_dma);
963 
964 exit_init_fw_cb:
965 	dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk),
966 			  init_fw_cb, init_fw_cb_dma);
967 
968 	return rval;
969 }
970 
qla4xxx_conn_get_param(struct iscsi_cls_conn * cls_conn,enum iscsi_param param,char * buf)971 static int qla4xxx_conn_get_param(struct iscsi_cls_conn *cls_conn,
972 				  enum iscsi_param param, char *buf)
973 {
974 	struct iscsi_conn *conn;
975 	struct qla_conn *qla_conn;
976 	struct sockaddr *dst_addr;
977 	int len = 0;
978 
979 	conn = cls_conn->dd_data;
980 	qla_conn = conn->dd_data;
981 	dst_addr = &qla_conn->qla_ep->dst_addr;
982 
983 	switch (param) {
984 	case ISCSI_PARAM_CONN_PORT:
985 	case ISCSI_PARAM_CONN_ADDRESS:
986 		return iscsi_conn_get_addr_param((struct sockaddr_storage *)
987 						 dst_addr, param, buf);
988 	default:
989 		return iscsi_conn_get_param(cls_conn, param, buf);
990 	}
991 
992 	return len;
993 
994 }
995 
qla4xxx_get_ddb_index(struct scsi_qla_host * ha,uint16_t * ddb_index)996 int qla4xxx_get_ddb_index(struct scsi_qla_host *ha, uint16_t *ddb_index)
997 {
998 	uint32_t mbx_sts = 0;
999 	uint16_t tmp_ddb_index;
1000 	int ret;
1001 
1002 get_ddb_index:
1003 	tmp_ddb_index = find_first_zero_bit(ha->ddb_idx_map, MAX_DDB_ENTRIES);
1004 
1005 	if (tmp_ddb_index >= MAX_DDB_ENTRIES) {
1006 		DEBUG2(ql4_printk(KERN_INFO, ha,
1007 				  "Free DDB index not available\n"));
1008 		ret = QLA_ERROR;
1009 		goto exit_get_ddb_index;
1010 	}
1011 
1012 	if (test_and_set_bit(tmp_ddb_index, ha->ddb_idx_map))
1013 		goto get_ddb_index;
1014 
1015 	DEBUG2(ql4_printk(KERN_INFO, ha,
1016 			  "Found a free DDB index at %d\n", tmp_ddb_index));
1017 	ret = qla4xxx_req_ddb_entry(ha, tmp_ddb_index, &mbx_sts);
1018 	if (ret == QLA_ERROR) {
1019 		if (mbx_sts == MBOX_STS_COMMAND_ERROR) {
1020 			ql4_printk(KERN_INFO, ha,
1021 				   "DDB index = %d not available trying next\n",
1022 				   tmp_ddb_index);
1023 			goto get_ddb_index;
1024 		}
1025 		DEBUG2(ql4_printk(KERN_INFO, ha,
1026 				  "Free FW DDB not available\n"));
1027 	}
1028 
1029 	*ddb_index = tmp_ddb_index;
1030 
1031 exit_get_ddb_index:
1032 	return ret;
1033 }
1034 
qla4xxx_match_ipaddress(struct scsi_qla_host * ha,struct ddb_entry * ddb_entry,char * existing_ipaddr,char * user_ipaddr)1035 static int qla4xxx_match_ipaddress(struct scsi_qla_host *ha,
1036 				   struct ddb_entry *ddb_entry,
1037 				   char *existing_ipaddr,
1038 				   char *user_ipaddr)
1039 {
1040 	uint8_t dst_ipaddr[IPv6_ADDR_LEN];
1041 	char formatted_ipaddr[DDB_IPADDR_LEN];
1042 	int status = QLA_SUCCESS, ret = 0;
1043 
1044 	if (ddb_entry->fw_ddb_entry.options & DDB_OPT_IPV6_DEVICE) {
1045 		ret = in6_pton(user_ipaddr, strlen(user_ipaddr), dst_ipaddr,
1046 			       '\0', NULL);
1047 		if (ret == 0) {
1048 			status = QLA_ERROR;
1049 			goto out_match;
1050 		}
1051 		ret = sprintf(formatted_ipaddr, "%pI6", dst_ipaddr);
1052 	} else {
1053 		ret = in4_pton(user_ipaddr, strlen(user_ipaddr), dst_ipaddr,
1054 			       '\0', NULL);
1055 		if (ret == 0) {
1056 			status = QLA_ERROR;
1057 			goto out_match;
1058 		}
1059 		ret = sprintf(formatted_ipaddr, "%pI4", dst_ipaddr);
1060 	}
1061 
1062 	if (strcmp(existing_ipaddr, formatted_ipaddr))
1063 		status = QLA_ERROR;
1064 
1065 out_match:
1066 	return status;
1067 }
1068 
qla4xxx_match_fwdb_session(struct scsi_qla_host * ha,struct iscsi_cls_conn * cls_conn)1069 static int qla4xxx_match_fwdb_session(struct scsi_qla_host *ha,
1070 				      struct iscsi_cls_conn *cls_conn)
1071 {
1072 	int idx = 0, max_ddbs, rval;
1073 	struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
1074 	struct iscsi_session *sess, *existing_sess;
1075 	struct iscsi_conn *conn, *existing_conn;
1076 	struct ddb_entry *ddb_entry;
1077 
1078 	sess = cls_sess->dd_data;
1079 	conn = cls_conn->dd_data;
1080 
1081 	if (sess->targetname == NULL ||
1082 	    conn->persistent_address == NULL ||
1083 	    conn->persistent_port == 0)
1084 		return QLA_ERROR;
1085 
1086 	max_ddbs =  is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
1087 				     MAX_DEV_DB_ENTRIES;
1088 
1089 	for (idx = 0; idx < max_ddbs; idx++) {
1090 		ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
1091 		if (ddb_entry == NULL)
1092 			continue;
1093 
1094 		if (ddb_entry->ddb_type != FLASH_DDB)
1095 			continue;
1096 
1097 		existing_sess = ddb_entry->sess->dd_data;
1098 		existing_conn = ddb_entry->conn->dd_data;
1099 
1100 		if (existing_sess->targetname == NULL ||
1101 		    existing_conn->persistent_address == NULL ||
1102 		    existing_conn->persistent_port == 0)
1103 			continue;
1104 
1105 		DEBUG2(ql4_printk(KERN_INFO, ha,
1106 				  "IQN = %s User IQN = %s\n",
1107 				  existing_sess->targetname,
1108 				  sess->targetname));
1109 
1110 		DEBUG2(ql4_printk(KERN_INFO, ha,
1111 				  "IP = %s User IP = %s\n",
1112 				  existing_conn->persistent_address,
1113 				  conn->persistent_address));
1114 
1115 		DEBUG2(ql4_printk(KERN_INFO, ha,
1116 				  "Port = %d User Port = %d\n",
1117 				  existing_conn->persistent_port,
1118 				  conn->persistent_port));
1119 
1120 		if (strcmp(existing_sess->targetname, sess->targetname))
1121 			continue;
1122 		rval = qla4xxx_match_ipaddress(ha, ddb_entry,
1123 					existing_conn->persistent_address,
1124 					conn->persistent_address);
1125 		if (rval == QLA_ERROR)
1126 			continue;
1127 		if (existing_conn->persistent_port != conn->persistent_port)
1128 			continue;
1129 		break;
1130 	}
1131 
1132 	if (idx == max_ddbs)
1133 		return QLA_ERROR;
1134 
1135 	DEBUG2(ql4_printk(KERN_INFO, ha,
1136 			  "Match found in fwdb sessions\n"));
1137 	return QLA_SUCCESS;
1138 }
1139 
1140 static struct iscsi_cls_session *
qla4xxx_session_create(struct iscsi_endpoint * ep,uint16_t cmds_max,uint16_t qdepth,uint32_t initial_cmdsn)1141 qla4xxx_session_create(struct iscsi_endpoint *ep,
1142 			uint16_t cmds_max, uint16_t qdepth,
1143 			uint32_t initial_cmdsn)
1144 {
1145 	struct iscsi_cls_session *cls_sess;
1146 	struct scsi_qla_host *ha;
1147 	struct qla_endpoint *qla_ep;
1148 	struct ddb_entry *ddb_entry;
1149 	uint16_t ddb_index;
1150 	struct iscsi_session *sess;
1151 	struct sockaddr *dst_addr;
1152 	int ret;
1153 
1154 	DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1155 	if (!ep) {
1156 		printk(KERN_ERR "qla4xxx: missing ep.\n");
1157 		return NULL;
1158 	}
1159 
1160 	qla_ep = ep->dd_data;
1161 	dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
1162 	ha = to_qla_host(qla_ep->host);
1163 
1164 	ret = qla4xxx_get_ddb_index(ha, &ddb_index);
1165 	if (ret == QLA_ERROR)
1166 		return NULL;
1167 
1168 	cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport, qla_ep->host,
1169 				       cmds_max, sizeof(struct ddb_entry),
1170 				       sizeof(struct ql4_task_data),
1171 				       initial_cmdsn, ddb_index);
1172 	if (!cls_sess)
1173 		return NULL;
1174 
1175 	sess = cls_sess->dd_data;
1176 	ddb_entry = sess->dd_data;
1177 	ddb_entry->fw_ddb_index = ddb_index;
1178 	ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
1179 	ddb_entry->ha = ha;
1180 	ddb_entry->sess = cls_sess;
1181 	ddb_entry->unblock_sess = qla4xxx_unblock_ddb;
1182 	ddb_entry->ddb_change = qla4xxx_ddb_change;
1183 	cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
1184 	ha->fw_ddb_index_map[ddb_entry->fw_ddb_index] = ddb_entry;
1185 	ha->tot_ddbs++;
1186 
1187 	return cls_sess;
1188 }
1189 
qla4xxx_session_destroy(struct iscsi_cls_session * cls_sess)1190 static void qla4xxx_session_destroy(struct iscsi_cls_session *cls_sess)
1191 {
1192 	struct iscsi_session *sess;
1193 	struct ddb_entry *ddb_entry;
1194 	struct scsi_qla_host *ha;
1195 	unsigned long flags;
1196 
1197 	DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1198 	sess = cls_sess->dd_data;
1199 	ddb_entry = sess->dd_data;
1200 	ha = ddb_entry->ha;
1201 
1202 	qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
1203 
1204 	spin_lock_irqsave(&ha->hardware_lock, flags);
1205 	qla4xxx_free_ddb(ha, ddb_entry);
1206 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
1207 	iscsi_session_teardown(cls_sess);
1208 }
1209 
1210 static struct iscsi_cls_conn *
qla4xxx_conn_create(struct iscsi_cls_session * cls_sess,uint32_t conn_idx)1211 qla4xxx_conn_create(struct iscsi_cls_session *cls_sess, uint32_t conn_idx)
1212 {
1213 	struct iscsi_cls_conn *cls_conn;
1214 	struct iscsi_session *sess;
1215 	struct ddb_entry *ddb_entry;
1216 
1217 	DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1218 	cls_conn = iscsi_conn_setup(cls_sess, sizeof(struct qla_conn),
1219 				    conn_idx);
1220 	if (!cls_conn)
1221 		return NULL;
1222 
1223 	sess = cls_sess->dd_data;
1224 	ddb_entry = sess->dd_data;
1225 	ddb_entry->conn = cls_conn;
1226 
1227 	return cls_conn;
1228 }
1229 
qla4xxx_conn_bind(struct iscsi_cls_session * cls_session,struct iscsi_cls_conn * cls_conn,uint64_t transport_fd,int is_leading)1230 static int qla4xxx_conn_bind(struct iscsi_cls_session *cls_session,
1231 			     struct iscsi_cls_conn *cls_conn,
1232 			     uint64_t transport_fd, int is_leading)
1233 {
1234 	struct iscsi_conn *conn;
1235 	struct qla_conn *qla_conn;
1236 	struct iscsi_endpoint *ep;
1237 
1238 	DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1239 
1240 	if (iscsi_conn_bind(cls_session, cls_conn, is_leading))
1241 		return -EINVAL;
1242 	ep = iscsi_lookup_endpoint(transport_fd);
1243 	conn = cls_conn->dd_data;
1244 	qla_conn = conn->dd_data;
1245 	qla_conn->qla_ep = ep->dd_data;
1246 	return 0;
1247 }
1248 
qla4xxx_conn_start(struct iscsi_cls_conn * cls_conn)1249 static int qla4xxx_conn_start(struct iscsi_cls_conn *cls_conn)
1250 {
1251 	struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
1252 	struct iscsi_session *sess;
1253 	struct ddb_entry *ddb_entry;
1254 	struct scsi_qla_host *ha;
1255 	struct dev_db_entry *fw_ddb_entry = NULL;
1256 	dma_addr_t fw_ddb_entry_dma;
1257 	uint32_t mbx_sts = 0;
1258 	int ret = 0;
1259 	int status = QLA_SUCCESS;
1260 
1261 	DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1262 	sess = cls_sess->dd_data;
1263 	ddb_entry = sess->dd_data;
1264 	ha = ddb_entry->ha;
1265 
1266 	/* Check if we have  matching FW DDB, if yes then do not
1267 	 * login to this target. This could cause target to logout previous
1268 	 * connection
1269 	 */
1270 	ret = qla4xxx_match_fwdb_session(ha, cls_conn);
1271 	if (ret == QLA_SUCCESS) {
1272 		ql4_printk(KERN_INFO, ha,
1273 			   "Session already exist in FW.\n");
1274 		ret = -EEXIST;
1275 		goto exit_conn_start;
1276 	}
1277 
1278 	fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1279 					  &fw_ddb_entry_dma, GFP_KERNEL);
1280 	if (!fw_ddb_entry) {
1281 		ql4_printk(KERN_ERR, ha,
1282 			   "%s: Unable to allocate dma buffer\n", __func__);
1283 		ret = -ENOMEM;
1284 		goto exit_conn_start;
1285 	}
1286 
1287 	ret = qla4xxx_set_param_ddbentry(ha, ddb_entry, cls_conn, &mbx_sts);
1288 	if (ret) {
1289 		/* If iscsid is stopped and started then no need to do
1290 		* set param again since ddb state will be already
1291 		* active and FW does not allow set ddb to an
1292 		* active session.
1293 		*/
1294 		if (mbx_sts)
1295 			if (ddb_entry->fw_ddb_device_state ==
1296 						DDB_DS_SESSION_ACTIVE) {
1297 				ddb_entry->unblock_sess(ddb_entry->sess);
1298 				goto exit_set_param;
1299 			}
1300 
1301 		ql4_printk(KERN_ERR, ha, "%s: Failed set param for index[%d]\n",
1302 			   __func__, ddb_entry->fw_ddb_index);
1303 		goto exit_conn_start;
1304 	}
1305 
1306 	status = qla4xxx_conn_open(ha, ddb_entry->fw_ddb_index);
1307 	if (status == QLA_ERROR) {
1308 		ql4_printk(KERN_ERR, ha, "%s: Login failed: %s\n", __func__,
1309 			   sess->targetname);
1310 		ret = -EINVAL;
1311 		goto exit_conn_start;
1312 	}
1313 
1314 	if (ddb_entry->fw_ddb_device_state == DDB_DS_NO_CONNECTION_ACTIVE)
1315 		ddb_entry->fw_ddb_device_state = DDB_DS_LOGIN_IN_PROCESS;
1316 
1317 	DEBUG2(printk(KERN_INFO "%s: DDB state [%d]\n", __func__,
1318 		      ddb_entry->fw_ddb_device_state));
1319 
1320 exit_set_param:
1321 	ret = 0;
1322 
1323 exit_conn_start:
1324 	if (fw_ddb_entry)
1325 		dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1326 				  fw_ddb_entry, fw_ddb_entry_dma);
1327 	return ret;
1328 }
1329 
qla4xxx_conn_destroy(struct iscsi_cls_conn * cls_conn)1330 static void qla4xxx_conn_destroy(struct iscsi_cls_conn *cls_conn)
1331 {
1332 	struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
1333 	struct iscsi_session *sess;
1334 	struct scsi_qla_host *ha;
1335 	struct ddb_entry *ddb_entry;
1336 	int options;
1337 
1338 	DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1339 	sess = cls_sess->dd_data;
1340 	ddb_entry = sess->dd_data;
1341 	ha = ddb_entry->ha;
1342 
1343 	options = LOGOUT_OPTION_CLOSE_SESSION;
1344 	if (qla4xxx_session_logout_ddb(ha, ddb_entry, options) == QLA_ERROR)
1345 		ql4_printk(KERN_ERR, ha, "%s: Logout failed\n", __func__);
1346 }
1347 
qla4xxx_task_work(struct work_struct * wdata)1348 static void qla4xxx_task_work(struct work_struct *wdata)
1349 {
1350 	struct ql4_task_data *task_data;
1351 	struct scsi_qla_host *ha;
1352 	struct passthru_status *sts;
1353 	struct iscsi_task *task;
1354 	struct iscsi_hdr *hdr;
1355 	uint8_t *data;
1356 	uint32_t data_len;
1357 	struct iscsi_conn *conn;
1358 	int hdr_len;
1359 	itt_t itt;
1360 
1361 	task_data = container_of(wdata, struct ql4_task_data, task_work);
1362 	ha = task_data->ha;
1363 	task = task_data->task;
1364 	sts = &task_data->sts;
1365 	hdr_len = sizeof(struct iscsi_hdr);
1366 
1367 	DEBUG3(printk(KERN_INFO "Status returned\n"));
1368 	DEBUG3(qla4xxx_dump_buffer(sts, 64));
1369 	DEBUG3(printk(KERN_INFO "Response buffer"));
1370 	DEBUG3(qla4xxx_dump_buffer(task_data->resp_buffer, 64));
1371 
1372 	conn = task->conn;
1373 
1374 	switch (sts->completionStatus) {
1375 	case PASSTHRU_STATUS_COMPLETE:
1376 		hdr = (struct iscsi_hdr *)task_data->resp_buffer;
1377 		/* Assign back the itt in hdr, until we use the PREASSIGN_TAG */
1378 		itt = sts->handle;
1379 		hdr->itt = itt;
1380 		data = task_data->resp_buffer + hdr_len;
1381 		data_len = task_data->resp_len - hdr_len;
1382 		iscsi_complete_pdu(conn, hdr, data, data_len);
1383 		break;
1384 	default:
1385 		ql4_printk(KERN_ERR, ha, "Passthru failed status = 0x%x\n",
1386 			   sts->completionStatus);
1387 		break;
1388 	}
1389 	return;
1390 }
1391 
qla4xxx_alloc_pdu(struct iscsi_task * task,uint8_t opcode)1392 static int qla4xxx_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
1393 {
1394 	struct ql4_task_data *task_data;
1395 	struct iscsi_session *sess;
1396 	struct ddb_entry *ddb_entry;
1397 	struct scsi_qla_host *ha;
1398 	int hdr_len;
1399 
1400 	sess = task->conn->session;
1401 	ddb_entry = sess->dd_data;
1402 	ha = ddb_entry->ha;
1403 	task_data = task->dd_data;
1404 	memset(task_data, 0, sizeof(struct ql4_task_data));
1405 
1406 	if (task->sc) {
1407 		ql4_printk(KERN_INFO, ha,
1408 			   "%s: SCSI Commands not implemented\n", __func__);
1409 		return -EINVAL;
1410 	}
1411 
1412 	hdr_len = sizeof(struct iscsi_hdr);
1413 	task_data->ha = ha;
1414 	task_data->task = task;
1415 
1416 	if (task->data_count) {
1417 		task_data->data_dma = dma_map_single(&ha->pdev->dev, task->data,
1418 						     task->data_count,
1419 						     PCI_DMA_TODEVICE);
1420 	}
1421 
1422 	DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
1423 		      __func__, task->conn->max_recv_dlength, hdr_len));
1424 
1425 	task_data->resp_len = task->conn->max_recv_dlength + hdr_len;
1426 	task_data->resp_buffer = dma_alloc_coherent(&ha->pdev->dev,
1427 						    task_data->resp_len,
1428 						    &task_data->resp_dma,
1429 						    GFP_ATOMIC);
1430 	if (!task_data->resp_buffer)
1431 		goto exit_alloc_pdu;
1432 
1433 	task_data->req_len = task->data_count + hdr_len;
1434 	task_data->req_buffer = dma_alloc_coherent(&ha->pdev->dev,
1435 						   task_data->req_len,
1436 						   &task_data->req_dma,
1437 						   GFP_ATOMIC);
1438 	if (!task_data->req_buffer)
1439 		goto exit_alloc_pdu;
1440 
1441 	task->hdr = task_data->req_buffer;
1442 
1443 	INIT_WORK(&task_data->task_work, qla4xxx_task_work);
1444 
1445 	return 0;
1446 
1447 exit_alloc_pdu:
1448 	if (task_data->resp_buffer)
1449 		dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
1450 				  task_data->resp_buffer, task_data->resp_dma);
1451 
1452 	if (task_data->req_buffer)
1453 		dma_free_coherent(&ha->pdev->dev, task_data->req_len,
1454 				  task_data->req_buffer, task_data->req_dma);
1455 	return -ENOMEM;
1456 }
1457 
qla4xxx_task_cleanup(struct iscsi_task * task)1458 static void qla4xxx_task_cleanup(struct iscsi_task *task)
1459 {
1460 	struct ql4_task_data *task_data;
1461 	struct iscsi_session *sess;
1462 	struct ddb_entry *ddb_entry;
1463 	struct scsi_qla_host *ha;
1464 	int hdr_len;
1465 
1466 	hdr_len = sizeof(struct iscsi_hdr);
1467 	sess = task->conn->session;
1468 	ddb_entry = sess->dd_data;
1469 	ha = ddb_entry->ha;
1470 	task_data = task->dd_data;
1471 
1472 	if (task->data_count) {
1473 		dma_unmap_single(&ha->pdev->dev, task_data->data_dma,
1474 				 task->data_count, PCI_DMA_TODEVICE);
1475 	}
1476 
1477 	DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
1478 		      __func__, task->conn->max_recv_dlength, hdr_len));
1479 
1480 	dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
1481 			  task_data->resp_buffer, task_data->resp_dma);
1482 	dma_free_coherent(&ha->pdev->dev, task_data->req_len,
1483 			  task_data->req_buffer, task_data->req_dma);
1484 	return;
1485 }
1486 
qla4xxx_task_xmit(struct iscsi_task * task)1487 static int qla4xxx_task_xmit(struct iscsi_task *task)
1488 {
1489 	struct scsi_cmnd *sc = task->sc;
1490 	struct iscsi_session *sess = task->conn->session;
1491 	struct ddb_entry *ddb_entry = sess->dd_data;
1492 	struct scsi_qla_host *ha = ddb_entry->ha;
1493 
1494 	if (!sc)
1495 		return qla4xxx_send_passthru0(task);
1496 
1497 	ql4_printk(KERN_INFO, ha, "%s: scsi cmd xmit not implemented\n",
1498 		   __func__);
1499 	return -ENOSYS;
1500 }
1501 
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)1502 static void qla4xxx_copy_fwddb_param(struct scsi_qla_host *ha,
1503 				     struct dev_db_entry *fw_ddb_entry,
1504 				     struct iscsi_cls_session *cls_sess,
1505 				     struct iscsi_cls_conn *cls_conn)
1506 {
1507 	int buflen = 0;
1508 	struct iscsi_session *sess;
1509 	struct iscsi_conn *conn;
1510 	char ip_addr[DDB_IPADDR_LEN];
1511 	uint16_t options = 0;
1512 
1513 	sess = cls_sess->dd_data;
1514 	conn = cls_conn->dd_data;
1515 
1516 	conn->max_recv_dlength = BYTE_UNITS *
1517 			  le16_to_cpu(fw_ddb_entry->iscsi_max_rcv_data_seg_len);
1518 
1519 	conn->max_xmit_dlength = BYTE_UNITS *
1520 			  le16_to_cpu(fw_ddb_entry->iscsi_max_snd_data_seg_len);
1521 
1522 	sess->initial_r2t_en =
1523 			    (BIT_10 & le16_to_cpu(fw_ddb_entry->iscsi_options));
1524 
1525 	sess->max_r2t = le16_to_cpu(fw_ddb_entry->iscsi_max_outsnd_r2t);
1526 
1527 	sess->imm_data_en = (BIT_11 & le16_to_cpu(fw_ddb_entry->iscsi_options));
1528 
1529 	sess->first_burst = BYTE_UNITS *
1530 			       le16_to_cpu(fw_ddb_entry->iscsi_first_burst_len);
1531 
1532 	sess->max_burst = BYTE_UNITS *
1533 				 le16_to_cpu(fw_ddb_entry->iscsi_max_burst_len);
1534 
1535 	sess->time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
1536 
1537 	sess->time2retain = le16_to_cpu(fw_ddb_entry->iscsi_def_time2retain);
1538 
1539 	conn->persistent_port = le16_to_cpu(fw_ddb_entry->port);
1540 
1541 	sess->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
1542 
1543 	options = le16_to_cpu(fw_ddb_entry->options);
1544 	if (options & DDB_OPT_IPV6_DEVICE)
1545 		sprintf(ip_addr, "%pI6", fw_ddb_entry->ip_addr);
1546 	else
1547 		sprintf(ip_addr, "%pI4", fw_ddb_entry->ip_addr);
1548 
1549 	iscsi_set_param(cls_conn, ISCSI_PARAM_TARGET_NAME,
1550 			(char *)fw_ddb_entry->iscsi_name, buflen);
1551 	iscsi_set_param(cls_conn, ISCSI_PARAM_INITIATOR_NAME,
1552 			(char *)ha->name_string, buflen);
1553 	iscsi_set_param(cls_conn, ISCSI_PARAM_PERSISTENT_ADDRESS,
1554 			(char *)ip_addr, buflen);
1555 }
1556 
qla4xxx_update_session_conn_fwddb_param(struct scsi_qla_host * ha,struct ddb_entry * ddb_entry)1557 void qla4xxx_update_session_conn_fwddb_param(struct scsi_qla_host *ha,
1558 					     struct ddb_entry *ddb_entry)
1559 {
1560 	struct iscsi_cls_session *cls_sess;
1561 	struct iscsi_cls_conn *cls_conn;
1562 	uint32_t ddb_state;
1563 	dma_addr_t fw_ddb_entry_dma;
1564 	struct dev_db_entry *fw_ddb_entry;
1565 
1566 	fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1567 					  &fw_ddb_entry_dma, GFP_KERNEL);
1568 	if (!fw_ddb_entry) {
1569 		ql4_printk(KERN_ERR, ha,
1570 			   "%s: Unable to allocate dma buffer\n", __func__);
1571 		goto exit_session_conn_fwddb_param;
1572 	}
1573 
1574 	if (qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index, fw_ddb_entry,
1575 				    fw_ddb_entry_dma, NULL, NULL, &ddb_state,
1576 				    NULL, NULL, NULL) == QLA_ERROR) {
1577 		DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: failed "
1578 				  "get_ddb_entry for fw_ddb_index %d\n",
1579 				  ha->host_no, __func__,
1580 				  ddb_entry->fw_ddb_index));
1581 		goto exit_session_conn_fwddb_param;
1582 	}
1583 
1584 	cls_sess = ddb_entry->sess;
1585 
1586 	cls_conn = ddb_entry->conn;
1587 
1588 	/* Update params */
1589 	qla4xxx_copy_fwddb_param(ha, fw_ddb_entry, cls_sess, cls_conn);
1590 
1591 exit_session_conn_fwddb_param:
1592 	if (fw_ddb_entry)
1593 		dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1594 				  fw_ddb_entry, fw_ddb_entry_dma);
1595 }
1596 
qla4xxx_update_session_conn_param(struct scsi_qla_host * ha,struct ddb_entry * ddb_entry)1597 void qla4xxx_update_session_conn_param(struct scsi_qla_host *ha,
1598 				       struct ddb_entry *ddb_entry)
1599 {
1600 	struct iscsi_cls_session *cls_sess;
1601 	struct iscsi_cls_conn *cls_conn;
1602 	struct iscsi_session *sess;
1603 	struct iscsi_conn *conn;
1604 	uint32_t ddb_state;
1605 	dma_addr_t fw_ddb_entry_dma;
1606 	struct dev_db_entry *fw_ddb_entry;
1607 
1608 	fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1609 					  &fw_ddb_entry_dma, GFP_KERNEL);
1610 	if (!fw_ddb_entry) {
1611 		ql4_printk(KERN_ERR, ha,
1612 			   "%s: Unable to allocate dma buffer\n", __func__);
1613 		goto exit_session_conn_param;
1614 	}
1615 
1616 	if (qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index, fw_ddb_entry,
1617 				    fw_ddb_entry_dma, NULL, NULL, &ddb_state,
1618 				    NULL, NULL, NULL) == QLA_ERROR) {
1619 		DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: failed "
1620 				  "get_ddb_entry for fw_ddb_index %d\n",
1621 				  ha->host_no, __func__,
1622 				  ddb_entry->fw_ddb_index));
1623 		goto exit_session_conn_param;
1624 	}
1625 
1626 	cls_sess = ddb_entry->sess;
1627 	sess = cls_sess->dd_data;
1628 
1629 	cls_conn = ddb_entry->conn;
1630 	conn = cls_conn->dd_data;
1631 
1632 	/* Update timers after login */
1633 	ddb_entry->default_relogin_timeout =
1634 		(le16_to_cpu(fw_ddb_entry->def_timeout) > LOGIN_TOV) &&
1635 		 (le16_to_cpu(fw_ddb_entry->def_timeout) < LOGIN_TOV * 10) ?
1636 		 le16_to_cpu(fw_ddb_entry->def_timeout) : LOGIN_TOV;
1637 	ddb_entry->default_time2wait =
1638 				le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
1639 
1640 	/* Update params */
1641 	conn->max_recv_dlength = BYTE_UNITS *
1642 			  le16_to_cpu(fw_ddb_entry->iscsi_max_rcv_data_seg_len);
1643 
1644 	conn->max_xmit_dlength = BYTE_UNITS *
1645 			  le16_to_cpu(fw_ddb_entry->iscsi_max_snd_data_seg_len);
1646 
1647 	sess->initial_r2t_en =
1648 			    (BIT_10 & le16_to_cpu(fw_ddb_entry->iscsi_options));
1649 
1650 	sess->max_r2t = le16_to_cpu(fw_ddb_entry->iscsi_max_outsnd_r2t);
1651 
1652 	sess->imm_data_en = (BIT_11 & le16_to_cpu(fw_ddb_entry->iscsi_options));
1653 
1654 	sess->first_burst = BYTE_UNITS *
1655 			       le16_to_cpu(fw_ddb_entry->iscsi_first_burst_len);
1656 
1657 	sess->max_burst = BYTE_UNITS *
1658 				 le16_to_cpu(fw_ddb_entry->iscsi_max_burst_len);
1659 
1660 	sess->time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
1661 
1662 	sess->time2retain = le16_to_cpu(fw_ddb_entry->iscsi_def_time2retain);
1663 
1664 	sess->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
1665 
1666 	memcpy(sess->initiatorname, ha->name_string,
1667 	       min(sizeof(ha->name_string), sizeof(sess->initiatorname)));
1668 
1669 exit_session_conn_param:
1670 	if (fw_ddb_entry)
1671 		dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1672 				  fw_ddb_entry, fw_ddb_entry_dma);
1673 }
1674 
1675 /*
1676  * Timer routines
1677  */
1678 
qla4xxx_start_timer(struct scsi_qla_host * ha,void * func,unsigned long interval)1679 static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
1680 				unsigned long interval)
1681 {
1682 	DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
1683 		     __func__, ha->host->host_no));
1684 	init_timer(&ha->timer);
1685 	ha->timer.expires = jiffies + interval * HZ;
1686 	ha->timer.data = (unsigned long)ha;
1687 	ha->timer.function = (void (*)(unsigned long))func;
1688 	add_timer(&ha->timer);
1689 	ha->timer_active = 1;
1690 }
1691 
qla4xxx_stop_timer(struct scsi_qla_host * ha)1692 static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
1693 {
1694 	del_timer_sync(&ha->timer);
1695 	ha->timer_active = 0;
1696 }
1697 
1698 /***
1699  * qla4xxx_mark_device_missing - blocks the session
1700  * @cls_session: Pointer to the session to be blocked
1701  * @ddb_entry: Pointer to device database entry
1702  *
1703  * This routine marks a device missing and close connection.
1704  **/
qla4xxx_mark_device_missing(struct iscsi_cls_session * cls_session)1705 void qla4xxx_mark_device_missing(struct iscsi_cls_session *cls_session)
1706 {
1707 	iscsi_block_session(cls_session);
1708 }
1709 
1710 /**
1711  * qla4xxx_mark_all_devices_missing - mark all devices as missing.
1712  * @ha: Pointer to host adapter structure.
1713  *
1714  * This routine marks a device missing and resets the relogin retry count.
1715  **/
qla4xxx_mark_all_devices_missing(struct scsi_qla_host * ha)1716 void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
1717 {
1718 	iscsi_host_for_each_session(ha->host, qla4xxx_mark_device_missing);
1719 }
1720 
qla4xxx_get_new_srb(struct scsi_qla_host * ha,struct ddb_entry * ddb_entry,struct scsi_cmnd * cmd)1721 static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
1722 				       struct ddb_entry *ddb_entry,
1723 				       struct scsi_cmnd *cmd)
1724 {
1725 	struct srb *srb;
1726 
1727 	srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
1728 	if (!srb)
1729 		return srb;
1730 
1731 	kref_init(&srb->srb_ref);
1732 	srb->ha = ha;
1733 	srb->ddb = ddb_entry;
1734 	srb->cmd = cmd;
1735 	srb->flags = 0;
1736 	CMD_SP(cmd) = (void *)srb;
1737 
1738 	return srb;
1739 }
1740 
qla4xxx_srb_free_dma(struct scsi_qla_host * ha,struct srb * srb)1741 static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
1742 {
1743 	struct scsi_cmnd *cmd = srb->cmd;
1744 
1745 	if (srb->flags & SRB_DMA_VALID) {
1746 		scsi_dma_unmap(cmd);
1747 		srb->flags &= ~SRB_DMA_VALID;
1748 	}
1749 	CMD_SP(cmd) = NULL;
1750 }
1751 
qla4xxx_srb_compl(struct kref * ref)1752 void qla4xxx_srb_compl(struct kref *ref)
1753 {
1754 	struct srb *srb = container_of(ref, struct srb, srb_ref);
1755 	struct scsi_cmnd *cmd = srb->cmd;
1756 	struct scsi_qla_host *ha = srb->ha;
1757 
1758 	qla4xxx_srb_free_dma(ha, srb);
1759 
1760 	mempool_free(srb, ha->srb_mempool);
1761 
1762 	cmd->scsi_done(cmd);
1763 }
1764 
1765 /**
1766  * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
1767  * @host: scsi host
1768  * @cmd: Pointer to Linux's SCSI command structure
1769  *
1770  * Remarks:
1771  * This routine is invoked by Linux to send a SCSI command to the driver.
1772  * The mid-level driver tries to ensure that queuecommand never gets
1773  * invoked concurrently with itself or the interrupt handler (although
1774  * the interrupt handler may call this routine as part of request-
1775  * completion handling).   Unfortunely, it sometimes calls the scheduler
1776  * in interrupt context which is a big NO! NO!.
1777  **/
qla4xxx_queuecommand(struct Scsi_Host * host,struct scsi_cmnd * cmd)1778 static int qla4xxx_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
1779 {
1780 	struct scsi_qla_host *ha = to_qla_host(host);
1781 	struct ddb_entry *ddb_entry = cmd->device->hostdata;
1782 	struct iscsi_cls_session *sess = ddb_entry->sess;
1783 	struct srb *srb;
1784 	int rval;
1785 
1786 	if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1787 		if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
1788 			cmd->result = DID_NO_CONNECT << 16;
1789 		else
1790 			cmd->result = DID_REQUEUE << 16;
1791 		goto qc_fail_command;
1792 	}
1793 
1794 	if (!sess) {
1795 		cmd->result = DID_IMM_RETRY << 16;
1796 		goto qc_fail_command;
1797 	}
1798 
1799 	rval = iscsi_session_chkready(sess);
1800 	if (rval) {
1801 		cmd->result = rval;
1802 		goto qc_fail_command;
1803 	}
1804 
1805 	if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1806 	    test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
1807 	    test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1808 	    test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
1809 	    test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
1810 	    !test_bit(AF_ONLINE, &ha->flags) ||
1811 	    !test_bit(AF_LINK_UP, &ha->flags) ||
1812 	    test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
1813 		goto qc_host_busy;
1814 
1815 	srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd);
1816 	if (!srb)
1817 		goto qc_host_busy;
1818 
1819 	rval = qla4xxx_send_command_to_isp(ha, srb);
1820 	if (rval != QLA_SUCCESS)
1821 		goto qc_host_busy_free_sp;
1822 
1823 	return 0;
1824 
1825 qc_host_busy_free_sp:
1826 	qla4xxx_srb_free_dma(ha, srb);
1827 	mempool_free(srb, ha->srb_mempool);
1828 
1829 qc_host_busy:
1830 	return SCSI_MLQUEUE_HOST_BUSY;
1831 
1832 qc_fail_command:
1833 	cmd->scsi_done(cmd);
1834 
1835 	return 0;
1836 }
1837 
1838 /**
1839  * qla4xxx_mem_free - frees memory allocated to adapter
1840  * @ha: Pointer to host adapter structure.
1841  *
1842  * Frees memory previously allocated by qla4xxx_mem_alloc
1843  **/
qla4xxx_mem_free(struct scsi_qla_host * ha)1844 static void qla4xxx_mem_free(struct scsi_qla_host *ha)
1845 {
1846 	if (ha->queues)
1847 		dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
1848 				  ha->queues_dma);
1849 
1850 	ha->queues_len = 0;
1851 	ha->queues = NULL;
1852 	ha->queues_dma = 0;
1853 	ha->request_ring = NULL;
1854 	ha->request_dma = 0;
1855 	ha->response_ring = NULL;
1856 	ha->response_dma = 0;
1857 	ha->shadow_regs = NULL;
1858 	ha->shadow_regs_dma = 0;
1859 
1860 	/* Free srb pool. */
1861 	if (ha->srb_mempool)
1862 		mempool_destroy(ha->srb_mempool);
1863 
1864 	ha->srb_mempool = NULL;
1865 
1866 	if (ha->chap_dma_pool)
1867 		dma_pool_destroy(ha->chap_dma_pool);
1868 
1869 	if (ha->chap_list)
1870 		vfree(ha->chap_list);
1871 	ha->chap_list = NULL;
1872 
1873 	if (ha->fw_ddb_dma_pool)
1874 		dma_pool_destroy(ha->fw_ddb_dma_pool);
1875 
1876 	/* release io space registers  */
1877 	if (is_qla8022(ha)) {
1878 		if (ha->nx_pcibase)
1879 			iounmap(
1880 			    (struct device_reg_82xx __iomem *)ha->nx_pcibase);
1881 	} else if (ha->reg)
1882 		iounmap(ha->reg);
1883 	pci_release_regions(ha->pdev);
1884 }
1885 
1886 /**
1887  * qla4xxx_mem_alloc - allocates memory for use by adapter.
1888  * @ha: Pointer to host adapter structure
1889  *
1890  * Allocates DMA memory for request and response queues. Also allocates memory
1891  * for srbs.
1892  **/
qla4xxx_mem_alloc(struct scsi_qla_host * ha)1893 static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
1894 {
1895 	unsigned long align;
1896 
1897 	/* Allocate contiguous block of DMA memory for queues. */
1898 	ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
1899 			  (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
1900 			  sizeof(struct shadow_regs) +
1901 			  MEM_ALIGN_VALUE +
1902 			  (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
1903 	ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
1904 					&ha->queues_dma, GFP_KERNEL);
1905 	if (ha->queues == NULL) {
1906 		ql4_printk(KERN_WARNING, ha,
1907 		    "Memory Allocation failed - queues.\n");
1908 
1909 		goto mem_alloc_error_exit;
1910 	}
1911 	memset(ha->queues, 0, ha->queues_len);
1912 
1913 	/*
1914 	 * As per RISC alignment requirements -- the bus-address must be a
1915 	 * multiple of the request-ring size (in bytes).
1916 	 */
1917 	align = 0;
1918 	if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
1919 		align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
1920 					   (MEM_ALIGN_VALUE - 1));
1921 
1922 	/* Update request and response queue pointers. */
1923 	ha->request_dma = ha->queues_dma + align;
1924 	ha->request_ring = (struct queue_entry *) (ha->queues + align);
1925 	ha->response_dma = ha->queues_dma + align +
1926 		(REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
1927 	ha->response_ring = (struct queue_entry *) (ha->queues + align +
1928 						    (REQUEST_QUEUE_DEPTH *
1929 						     QUEUE_SIZE));
1930 	ha->shadow_regs_dma = ha->queues_dma + align +
1931 		(REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
1932 		(RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
1933 	ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
1934 						  (REQUEST_QUEUE_DEPTH *
1935 						   QUEUE_SIZE) +
1936 						  (RESPONSE_QUEUE_DEPTH *
1937 						   QUEUE_SIZE));
1938 
1939 	/* Allocate memory for srb pool. */
1940 	ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
1941 					 mempool_free_slab, srb_cachep);
1942 	if (ha->srb_mempool == NULL) {
1943 		ql4_printk(KERN_WARNING, ha,
1944 		    "Memory Allocation failed - SRB Pool.\n");
1945 
1946 		goto mem_alloc_error_exit;
1947 	}
1948 
1949 	ha->chap_dma_pool = dma_pool_create("ql4_chap", &ha->pdev->dev,
1950 					    CHAP_DMA_BLOCK_SIZE, 8, 0);
1951 
1952 	if (ha->chap_dma_pool == NULL) {
1953 		ql4_printk(KERN_WARNING, ha,
1954 		    "%s: chap_dma_pool allocation failed..\n", __func__);
1955 		goto mem_alloc_error_exit;
1956 	}
1957 
1958 	ha->fw_ddb_dma_pool = dma_pool_create("ql4_fw_ddb", &ha->pdev->dev,
1959 					      DDB_DMA_BLOCK_SIZE, 8, 0);
1960 
1961 	if (ha->fw_ddb_dma_pool == NULL) {
1962 		ql4_printk(KERN_WARNING, ha,
1963 			   "%s: fw_ddb_dma_pool allocation failed..\n",
1964 			   __func__);
1965 		goto mem_alloc_error_exit;
1966 	}
1967 
1968 	return QLA_SUCCESS;
1969 
1970 mem_alloc_error_exit:
1971 	qla4xxx_mem_free(ha);
1972 	return QLA_ERROR;
1973 }
1974 
1975 /**
1976  * qla4_8xxx_check_temp - Check the ISP82XX temperature.
1977  * @ha: adapter block pointer.
1978  *
1979  * Note: The caller should not hold the idc lock.
1980  **/
qla4_8xxx_check_temp(struct scsi_qla_host * ha)1981 static int qla4_8xxx_check_temp(struct scsi_qla_host *ha)
1982 {
1983 	uint32_t temp, temp_state, temp_val;
1984 	int status = QLA_SUCCESS;
1985 
1986 	temp = qla4_8xxx_rd_32(ha, CRB_TEMP_STATE);
1987 
1988 	temp_state = qla82xx_get_temp_state(temp);
1989 	temp_val = qla82xx_get_temp_val(temp);
1990 
1991 	if (temp_state == QLA82XX_TEMP_PANIC) {
1992 		ql4_printk(KERN_WARNING, ha, "Device temperature %d degrees C"
1993 			   " exceeds maximum allowed. Hardware has been shut"
1994 			   " down.\n", temp_val);
1995 		status = QLA_ERROR;
1996 	} else if (temp_state == QLA82XX_TEMP_WARN) {
1997 		if (ha->temperature == QLA82XX_TEMP_NORMAL)
1998 			ql4_printk(KERN_WARNING, ha, "Device temperature %d"
1999 				   " degrees C exceeds operating range."
2000 				   " Immediate action needed.\n", temp_val);
2001 	} else {
2002 		if (ha->temperature == QLA82XX_TEMP_WARN)
2003 			ql4_printk(KERN_INFO, ha, "Device temperature is"
2004 				   " now %d degrees C in normal range.\n",
2005 				   temp_val);
2006 	}
2007 	ha->temperature = temp_state;
2008 	return status;
2009 }
2010 
2011 /**
2012  * qla4_8xxx_check_fw_alive  - Check firmware health
2013  * @ha: Pointer to host adapter structure.
2014  *
2015  * Context: Interrupt
2016  **/
qla4_8xxx_check_fw_alive(struct scsi_qla_host * ha)2017 static int qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
2018 {
2019 	uint32_t fw_heartbeat_counter;
2020 	int status = QLA_SUCCESS;
2021 
2022 	fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
2023 	/* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
2024 	if (fw_heartbeat_counter == 0xffffffff) {
2025 		DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
2026 		    "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
2027 		    ha->host_no, __func__));
2028 		return status;
2029 	}
2030 
2031 	if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
2032 		ha->seconds_since_last_heartbeat++;
2033 		/* FW not alive after 2 seconds */
2034 		if (ha->seconds_since_last_heartbeat == 2) {
2035 			ha->seconds_since_last_heartbeat = 0;
2036 
2037 			ql4_printk(KERN_INFO, ha,
2038 				   "scsi(%ld): %s, Dumping hw/fw registers:\n "
2039 				   " PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2:"
2040 				   " 0x%x,\n PEG_NET_0_PC: 0x%x, PEG_NET_1_PC:"
2041 				   " 0x%x,\n PEG_NET_2_PC: 0x%x, PEG_NET_3_PC:"
2042 				   " 0x%x,\n PEG_NET_4_PC: 0x%x\n",
2043 				   ha->host_no, __func__,
2044 				   qla4_8xxx_rd_32(ha,
2045 						   QLA82XX_PEG_HALT_STATUS1),
2046 				   qla4_8xxx_rd_32(ha,
2047 						   QLA82XX_PEG_HALT_STATUS2),
2048 				   qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_0 +
2049 						   0x3c),
2050 				   qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_1 +
2051 						   0x3c),
2052 				   qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_2 +
2053 						   0x3c),
2054 				   qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_3 +
2055 						   0x3c),
2056 				   qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_4 +
2057 						   0x3c));
2058 			status = QLA_ERROR;
2059 		}
2060 	} else
2061 		ha->seconds_since_last_heartbeat = 0;
2062 
2063 	ha->fw_heartbeat_counter = fw_heartbeat_counter;
2064 	return status;
2065 }
2066 
2067 /**
2068  * qla4_8xxx_watchdog - Poll dev state
2069  * @ha: Pointer to host adapter structure.
2070  *
2071  * Context: Interrupt
2072  **/
qla4_8xxx_watchdog(struct scsi_qla_host * ha)2073 void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
2074 {
2075 	uint32_t dev_state, halt_status;
2076 
2077 	/* don't poll if reset is going on */
2078 	if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
2079 	    test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
2080 	    test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags))) {
2081 		dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
2082 
2083 		if (qla4_8xxx_check_temp(ha)) {
2084 			ql4_printk(KERN_INFO, ha, "disabling pause"
2085 				   " transmit on port 0 & 1.\n");
2086 			qla4_8xxx_wr_32(ha, QLA82XX_CRB_NIU + 0x98,
2087 					CRB_NIU_XG_PAUSE_CTL_P0 |
2088 					CRB_NIU_XG_PAUSE_CTL_P1);
2089 			set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
2090 			qla4xxx_wake_dpc(ha);
2091 		} else if (dev_state == QLA82XX_DEV_NEED_RESET &&
2092 		    !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
2093 			if (!ql4xdontresethba) {
2094 				ql4_printk(KERN_INFO, ha, "%s: HW State: "
2095 				    "NEED RESET!\n", __func__);
2096 				set_bit(DPC_RESET_HA, &ha->dpc_flags);
2097 				qla4xxx_wake_dpc(ha);
2098 			}
2099 		} else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
2100 		    !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
2101 			ql4_printk(KERN_INFO, ha, "%s: HW State: NEED QUIES!\n",
2102 			    __func__);
2103 			set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
2104 			qla4xxx_wake_dpc(ha);
2105 		} else  {
2106 			/* Check firmware health */
2107 			if (qla4_8xxx_check_fw_alive(ha)) {
2108 				ql4_printk(KERN_INFO, ha, "disabling pause"
2109 					   " transmit on port 0 & 1.\n");
2110 				qla4_8xxx_wr_32(ha, QLA82XX_CRB_NIU + 0x98,
2111 						CRB_NIU_XG_PAUSE_CTL_P0 |
2112 						CRB_NIU_XG_PAUSE_CTL_P1);
2113 				halt_status = qla4_8xxx_rd_32(ha,
2114 						QLA82XX_PEG_HALT_STATUS1);
2115 
2116 				if (LSW(MSB(halt_status)) == 0x67)
2117 					ql4_printk(KERN_ERR, ha, "%s:"
2118 						   " Firmware aborted with"
2119 						   " error code 0x00006700."
2120 						   " Device is being reset\n",
2121 						   __func__);
2122 
2123 				/* Since we cannot change dev_state in interrupt
2124 				 * context, set appropriate DPC flag then wakeup
2125 				 * DPC */
2126 				if (halt_status & HALT_STATUS_UNRECOVERABLE)
2127 					set_bit(DPC_HA_UNRECOVERABLE,
2128 						&ha->dpc_flags);
2129 				else {
2130 					ql4_printk(KERN_INFO, ha, "%s: detect "
2131 						   "abort needed!\n", __func__);
2132 					set_bit(DPC_RESET_HA, &ha->dpc_flags);
2133 				}
2134 				qla4xxx_mailbox_premature_completion(ha);
2135 				qla4xxx_wake_dpc(ha);
2136 			}
2137 		}
2138 	}
2139 }
2140 
qla4xxx_check_relogin_flash_ddb(struct iscsi_cls_session * cls_sess)2141 static void qla4xxx_check_relogin_flash_ddb(struct iscsi_cls_session *cls_sess)
2142 {
2143 	struct iscsi_session *sess;
2144 	struct ddb_entry *ddb_entry;
2145 	struct scsi_qla_host *ha;
2146 
2147 	sess = cls_sess->dd_data;
2148 	ddb_entry = sess->dd_data;
2149 	ha = ddb_entry->ha;
2150 
2151 	if (!(ddb_entry->ddb_type == FLASH_DDB))
2152 		return;
2153 
2154 	if (adapter_up(ha) && !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
2155 	    !iscsi_is_session_online(cls_sess)) {
2156 		if (atomic_read(&ddb_entry->retry_relogin_timer) !=
2157 		    INVALID_ENTRY) {
2158 			if (atomic_read(&ddb_entry->retry_relogin_timer) ==
2159 					0) {
2160 				atomic_set(&ddb_entry->retry_relogin_timer,
2161 					   INVALID_ENTRY);
2162 				set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
2163 				set_bit(DF_RELOGIN, &ddb_entry->flags);
2164 				DEBUG2(ql4_printk(KERN_INFO, ha,
2165 				       "%s: index [%d] login device\n",
2166 					__func__, ddb_entry->fw_ddb_index));
2167 			} else
2168 				atomic_dec(&ddb_entry->retry_relogin_timer);
2169 		}
2170 	}
2171 
2172 	/* Wait for relogin to timeout */
2173 	if (atomic_read(&ddb_entry->relogin_timer) &&
2174 	    (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
2175 		/*
2176 		 * If the relogin times out and the device is
2177 		 * still NOT ONLINE then try and relogin again.
2178 		 */
2179 		if (!iscsi_is_session_online(cls_sess)) {
2180 			/* Reset retry relogin timer */
2181 			atomic_inc(&ddb_entry->relogin_retry_count);
2182 			DEBUG2(ql4_printk(KERN_INFO, ha,
2183 				"%s: index[%d] relogin timed out-retrying"
2184 				" relogin (%d), retry (%d)\n", __func__,
2185 				ddb_entry->fw_ddb_index,
2186 				atomic_read(&ddb_entry->relogin_retry_count),
2187 				ddb_entry->default_time2wait + 4));
2188 			set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
2189 			atomic_set(&ddb_entry->retry_relogin_timer,
2190 				   ddb_entry->default_time2wait + 4);
2191 		}
2192 	}
2193 }
2194 
2195 /**
2196  * qla4xxx_timer - checks every second for work to do.
2197  * @ha: Pointer to host adapter structure.
2198  **/
qla4xxx_timer(struct scsi_qla_host * ha)2199 static void qla4xxx_timer(struct scsi_qla_host *ha)
2200 {
2201 	int start_dpc = 0;
2202 	uint16_t w;
2203 
2204 	iscsi_host_for_each_session(ha->host, qla4xxx_check_relogin_flash_ddb);
2205 
2206 	/* If we are in the middle of AER/EEH processing
2207 	 * skip any processing and reschedule the timer
2208 	 */
2209 	if (test_bit(AF_EEH_BUSY, &ha->flags)) {
2210 		mod_timer(&ha->timer, jiffies + HZ);
2211 		return;
2212 	}
2213 
2214 	/* Hardware read to trigger an EEH error during mailbox waits. */
2215 	if (!pci_channel_offline(ha->pdev))
2216 		pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
2217 
2218 	if (is_qla8022(ha)) {
2219 		qla4_8xxx_watchdog(ha);
2220 	}
2221 
2222 	if (!is_qla8022(ha)) {
2223 		/* Check for heartbeat interval. */
2224 		if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
2225 		    ha->heartbeat_interval != 0) {
2226 			ha->seconds_since_last_heartbeat++;
2227 			if (ha->seconds_since_last_heartbeat >
2228 			    ha->heartbeat_interval + 2)
2229 				set_bit(DPC_RESET_HA, &ha->dpc_flags);
2230 		}
2231 	}
2232 
2233 	/* Wakeup the dpc routine for this adapter, if needed. */
2234 	if (start_dpc ||
2235 	     test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
2236 	     test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
2237 	     test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
2238 	     test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
2239 	     test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
2240 	     test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
2241 	     test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
2242 	     test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
2243 	     test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
2244 	     test_bit(DPC_AEN, &ha->dpc_flags)) {
2245 		DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
2246 			      " - dpc flags = 0x%lx\n",
2247 			      ha->host_no, __func__, ha->dpc_flags));
2248 		qla4xxx_wake_dpc(ha);
2249 	}
2250 
2251 	/* Reschedule timer thread to call us back in one second */
2252 	mod_timer(&ha->timer, jiffies + HZ);
2253 
2254 	DEBUG2(ha->seconds_since_last_intr++);
2255 }
2256 
2257 /**
2258  * qla4xxx_cmd_wait - waits for all outstanding commands to complete
2259  * @ha: Pointer to host adapter structure.
2260  *
2261  * This routine stalls the driver until all outstanding commands are returned.
2262  * Caller must release the Hardware Lock prior to calling this routine.
2263  **/
qla4xxx_cmd_wait(struct scsi_qla_host * ha)2264 static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
2265 {
2266 	uint32_t index = 0;
2267 	unsigned long flags;
2268 	struct scsi_cmnd *cmd;
2269 
2270 	unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
2271 
2272 	DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
2273 	    "complete\n", WAIT_CMD_TOV));
2274 
2275 	while (!time_after_eq(jiffies, wtime)) {
2276 		spin_lock_irqsave(&ha->hardware_lock, flags);
2277 		/* Find a command that hasn't completed. */
2278 		for (index = 0; index < ha->host->can_queue; index++) {
2279 			cmd = scsi_host_find_tag(ha->host, index);
2280 			/*
2281 			 * We cannot just check if the index is valid,
2282 			 * becase if we are run from the scsi eh, then
2283 			 * the scsi/block layer is going to prevent
2284 			 * the tag from being released.
2285 			 */
2286 			if (cmd != NULL && CMD_SP(cmd))
2287 				break;
2288 		}
2289 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
2290 
2291 		/* If No Commands are pending, wait is complete */
2292 		if (index == ha->host->can_queue)
2293 			return QLA_SUCCESS;
2294 
2295 		msleep(1000);
2296 	}
2297 	/* If we timed out on waiting for commands to come back
2298 	 * return ERROR. */
2299 	return QLA_ERROR;
2300 }
2301 
qla4xxx_hw_reset(struct scsi_qla_host * ha)2302 int qla4xxx_hw_reset(struct scsi_qla_host *ha)
2303 {
2304 	uint32_t ctrl_status;
2305 	unsigned long flags = 0;
2306 
2307 	DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
2308 
2309 	if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
2310 		return QLA_ERROR;
2311 
2312 	spin_lock_irqsave(&ha->hardware_lock, flags);
2313 
2314 	/*
2315 	 * If the SCSI Reset Interrupt bit is set, clear it.
2316 	 * Otherwise, the Soft Reset won't work.
2317 	 */
2318 	ctrl_status = readw(&ha->reg->ctrl_status);
2319 	if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
2320 		writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
2321 
2322 	/* Issue Soft Reset */
2323 	writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
2324 	readl(&ha->reg->ctrl_status);
2325 
2326 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
2327 	return QLA_SUCCESS;
2328 }
2329 
2330 /**
2331  * qla4xxx_soft_reset - performs soft reset.
2332  * @ha: Pointer to host adapter structure.
2333  **/
qla4xxx_soft_reset(struct scsi_qla_host * ha)2334 int qla4xxx_soft_reset(struct scsi_qla_host *ha)
2335 {
2336 	uint32_t max_wait_time;
2337 	unsigned long flags = 0;
2338 	int status;
2339 	uint32_t ctrl_status;
2340 
2341 	status = qla4xxx_hw_reset(ha);
2342 	if (status != QLA_SUCCESS)
2343 		return status;
2344 
2345 	status = QLA_ERROR;
2346 	/* Wait until the Network Reset Intr bit is cleared */
2347 	max_wait_time = RESET_INTR_TOV;
2348 	do {
2349 		spin_lock_irqsave(&ha->hardware_lock, flags);
2350 		ctrl_status = readw(&ha->reg->ctrl_status);
2351 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
2352 
2353 		if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
2354 			break;
2355 
2356 		msleep(1000);
2357 	} while ((--max_wait_time));
2358 
2359 	if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
2360 		DEBUG2(printk(KERN_WARNING
2361 			      "scsi%ld: Network Reset Intr not cleared by "
2362 			      "Network function, clearing it now!\n",
2363 			      ha->host_no));
2364 		spin_lock_irqsave(&ha->hardware_lock, flags);
2365 		writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
2366 		readl(&ha->reg->ctrl_status);
2367 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
2368 	}
2369 
2370 	/* Wait until the firmware tells us the Soft Reset is done */
2371 	max_wait_time = SOFT_RESET_TOV;
2372 	do {
2373 		spin_lock_irqsave(&ha->hardware_lock, flags);
2374 		ctrl_status = readw(&ha->reg->ctrl_status);
2375 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
2376 
2377 		if ((ctrl_status & CSR_SOFT_RESET) == 0) {
2378 			status = QLA_SUCCESS;
2379 			break;
2380 		}
2381 
2382 		msleep(1000);
2383 	} while ((--max_wait_time));
2384 
2385 	/*
2386 	 * Also, make sure that the SCSI Reset Interrupt bit has been cleared
2387 	 * after the soft reset has taken place.
2388 	 */
2389 	spin_lock_irqsave(&ha->hardware_lock, flags);
2390 	ctrl_status = readw(&ha->reg->ctrl_status);
2391 	if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
2392 		writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
2393 		readl(&ha->reg->ctrl_status);
2394 	}
2395 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
2396 
2397 	/* If soft reset fails then most probably the bios on other
2398 	 * function is also enabled.
2399 	 * Since the initialization is sequential the other fn
2400 	 * wont be able to acknowledge the soft reset.
2401 	 * Issue a force soft reset to workaround this scenario.
2402 	 */
2403 	if (max_wait_time == 0) {
2404 		/* Issue Force Soft Reset */
2405 		spin_lock_irqsave(&ha->hardware_lock, flags);
2406 		writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
2407 		readl(&ha->reg->ctrl_status);
2408 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
2409 		/* Wait until the firmware tells us the Soft Reset is done */
2410 		max_wait_time = SOFT_RESET_TOV;
2411 		do {
2412 			spin_lock_irqsave(&ha->hardware_lock, flags);
2413 			ctrl_status = readw(&ha->reg->ctrl_status);
2414 			spin_unlock_irqrestore(&ha->hardware_lock, flags);
2415 
2416 			if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
2417 				status = QLA_SUCCESS;
2418 				break;
2419 			}
2420 
2421 			msleep(1000);
2422 		} while ((--max_wait_time));
2423 	}
2424 
2425 	return status;
2426 }
2427 
2428 /**
2429  * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
2430  * @ha: Pointer to host adapter structure.
2431  * @res: returned scsi status
2432  *
2433  * This routine is called just prior to a HARD RESET to return all
2434  * outstanding commands back to the Operating System.
2435  * Caller should make sure that the following locks are released
2436  * before this calling routine: Hardware lock, and io_request_lock.
2437  **/
qla4xxx_abort_active_cmds(struct scsi_qla_host * ha,int res)2438 static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
2439 {
2440 	struct srb *srb;
2441 	int i;
2442 	unsigned long flags;
2443 
2444 	spin_lock_irqsave(&ha->hardware_lock, flags);
2445 	for (i = 0; i < ha->host->can_queue; i++) {
2446 		srb = qla4xxx_del_from_active_array(ha, i);
2447 		if (srb != NULL) {
2448 			srb->cmd->result = res;
2449 			kref_put(&srb->srb_ref, qla4xxx_srb_compl);
2450 		}
2451 	}
2452 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
2453 }
2454 
qla4xxx_dead_adapter_cleanup(struct scsi_qla_host * ha)2455 void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
2456 {
2457 	clear_bit(AF_ONLINE, &ha->flags);
2458 
2459 	/* Disable the board */
2460 	ql4_printk(KERN_INFO, ha, "Disabling the board\n");
2461 
2462 	qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
2463 	qla4xxx_mark_all_devices_missing(ha);
2464 	clear_bit(AF_INIT_DONE, &ha->flags);
2465 }
2466 
qla4xxx_fail_session(struct iscsi_cls_session * cls_session)2467 static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session)
2468 {
2469 	struct iscsi_session *sess;
2470 	struct ddb_entry *ddb_entry;
2471 
2472 	sess = cls_session->dd_data;
2473 	ddb_entry = sess->dd_data;
2474 	ddb_entry->fw_ddb_device_state = DDB_DS_SESSION_FAILED;
2475 
2476 	if (ddb_entry->ddb_type == FLASH_DDB)
2477 		iscsi_block_session(ddb_entry->sess);
2478 	else
2479 		iscsi_session_failure(cls_session->dd_data,
2480 				      ISCSI_ERR_CONN_FAILED);
2481 }
2482 
2483 /**
2484  * qla4xxx_recover_adapter - recovers adapter after a fatal error
2485  * @ha: Pointer to host adapter structure.
2486  **/
qla4xxx_recover_adapter(struct scsi_qla_host * ha)2487 static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
2488 {
2489 	int status = QLA_ERROR;
2490 	uint8_t reset_chip = 0;
2491 	uint32_t dev_state;
2492 	unsigned long wait;
2493 
2494 	/* Stall incoming I/O until we are done */
2495 	scsi_block_requests(ha->host);
2496 	clear_bit(AF_ONLINE, &ha->flags);
2497 	clear_bit(AF_LINK_UP, &ha->flags);
2498 
2499 	DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
2500 
2501 	set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2502 
2503 	iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
2504 
2505 	if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
2506 		reset_chip = 1;
2507 
2508 	/* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
2509 	 * do not reset adapter, jump to initialize_adapter */
2510 	if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
2511 		status = QLA_SUCCESS;
2512 		goto recover_ha_init_adapter;
2513 	}
2514 
2515 	/* For the ISP-82xx adapter, issue a stop_firmware if invoked
2516 	 * from eh_host_reset or ioctl module */
2517 	if (is_qla8022(ha) && !reset_chip &&
2518 	    test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
2519 
2520 		DEBUG2(ql4_printk(KERN_INFO, ha,
2521 		    "scsi%ld: %s - Performing stop_firmware...\n",
2522 		    ha->host_no, __func__));
2523 		status = ha->isp_ops->reset_firmware(ha);
2524 		if (status == QLA_SUCCESS) {
2525 			if (!test_bit(AF_FW_RECOVERY, &ha->flags))
2526 				qla4xxx_cmd_wait(ha);
2527 			ha->isp_ops->disable_intrs(ha);
2528 			qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2529 			qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
2530 		} else {
2531 			/* If the stop_firmware fails then
2532 			 * reset the entire chip */
2533 			reset_chip = 1;
2534 			clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2535 			set_bit(DPC_RESET_HA, &ha->dpc_flags);
2536 		}
2537 	}
2538 
2539 	/* Issue full chip reset if recovering from a catastrophic error,
2540 	 * or if stop_firmware fails for ISP-82xx.
2541 	 * This is the default case for ISP-4xxx */
2542 	if (!is_qla8022(ha) || reset_chip) {
2543 		if (!is_qla8022(ha))
2544 			goto chip_reset;
2545 
2546 		/* Check if 82XX firmware is alive or not
2547 		 * We may have arrived here from NEED_RESET
2548 		 * detection only */
2549 		if (test_bit(AF_FW_RECOVERY, &ha->flags))
2550 			goto chip_reset;
2551 
2552 		wait = jiffies + (FW_ALIVE_WAIT_TOV * HZ);
2553 		while (time_before(jiffies, wait)) {
2554 			if (qla4_8xxx_check_fw_alive(ha)) {
2555 				qla4xxx_mailbox_premature_completion(ha);
2556 				break;
2557 			}
2558 
2559 			set_current_state(TASK_UNINTERRUPTIBLE);
2560 			schedule_timeout(HZ);
2561 		}
2562 
2563 		if (!test_bit(AF_FW_RECOVERY, &ha->flags))
2564 			qla4xxx_cmd_wait(ha);
2565 chip_reset:
2566 		qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2567 		qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
2568 		DEBUG2(ql4_printk(KERN_INFO, ha,
2569 		    "scsi%ld: %s - Performing chip reset..\n",
2570 		    ha->host_no, __func__));
2571 		status = ha->isp_ops->reset_chip(ha);
2572 	}
2573 
2574 	/* Flush any pending ddb changed AENs */
2575 	qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2576 
2577 recover_ha_init_adapter:
2578 	/* Upon successful firmware/chip reset, re-initialize the adapter */
2579 	if (status == QLA_SUCCESS) {
2580 		/* For ISP-4xxx, force function 1 to always initialize
2581 		 * before function 3 to prevent both funcions from
2582 		 * stepping on top of the other */
2583 		if (!is_qla8022(ha) && (ha->mac_index == 3))
2584 			ssleep(6);
2585 
2586 		/* NOTE: AF_ONLINE flag set upon successful completion of
2587 		 *       qla4xxx_initialize_adapter */
2588 		status = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
2589 	}
2590 
2591 	/* Retry failed adapter initialization, if necessary
2592 	 * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
2593 	 * case to prevent ping-pong resets between functions */
2594 	if (!test_bit(AF_ONLINE, &ha->flags) &&
2595 	    !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
2596 		/* Adapter initialization failed, see if we can retry
2597 		 * resetting the ha.
2598 		 * Since we don't want to block the DPC for too long
2599 		 * with multiple resets in the same thread,
2600 		 * utilize DPC to retry */
2601 		if (is_qla8022(ha)) {
2602 			qla4_8xxx_idc_lock(ha);
2603 			dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
2604 			qla4_8xxx_idc_unlock(ha);
2605 			if (dev_state == QLA82XX_DEV_FAILED) {
2606 				ql4_printk(KERN_INFO, ha, "%s: don't retry "
2607 					   "recover adapter. H/W is in Failed "
2608 					   "state\n", __func__);
2609 				qla4xxx_dead_adapter_cleanup(ha);
2610 				clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
2611 				clear_bit(DPC_RESET_HA, &ha->dpc_flags);
2612 				clear_bit(DPC_RESET_HA_FW_CONTEXT,
2613 						&ha->dpc_flags);
2614 				status = QLA_ERROR;
2615 
2616 				goto exit_recover;
2617 			}
2618 		}
2619 
2620 		if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
2621 			ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
2622 			DEBUG2(printk("scsi%ld: recover adapter - retrying "
2623 				      "(%d) more times\n", ha->host_no,
2624 				      ha->retry_reset_ha_cnt));
2625 			set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
2626 			status = QLA_ERROR;
2627 		} else {
2628 			if (ha->retry_reset_ha_cnt > 0) {
2629 				/* Schedule another Reset HA--DPC will retry */
2630 				ha->retry_reset_ha_cnt--;
2631 				DEBUG2(printk("scsi%ld: recover adapter - "
2632 					      "retry remaining %d\n",
2633 					      ha->host_no,
2634 					      ha->retry_reset_ha_cnt));
2635 				status = QLA_ERROR;
2636 			}
2637 
2638 			if (ha->retry_reset_ha_cnt == 0) {
2639 				/* Recover adapter retries have been exhausted.
2640 				 * Adapter DEAD */
2641 				DEBUG2(printk("scsi%ld: recover adapter "
2642 					      "failed - board disabled\n",
2643 					      ha->host_no));
2644 				qla4xxx_dead_adapter_cleanup(ha);
2645 				clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
2646 				clear_bit(DPC_RESET_HA, &ha->dpc_flags);
2647 				clear_bit(DPC_RESET_HA_FW_CONTEXT,
2648 					  &ha->dpc_flags);
2649 				status = QLA_ERROR;
2650 			}
2651 		}
2652 	} else {
2653 		clear_bit(DPC_RESET_HA, &ha->dpc_flags);
2654 		clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2655 		clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
2656 	}
2657 
2658 exit_recover:
2659 	ha->adapter_error_count++;
2660 
2661 	if (test_bit(AF_ONLINE, &ha->flags))
2662 		ha->isp_ops->enable_intrs(ha);
2663 
2664 	scsi_unblock_requests(ha->host);
2665 
2666 	clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2667 	DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
2668 	    status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
2669 
2670 	return status;
2671 }
2672 
qla4xxx_relogin_devices(struct iscsi_cls_session * cls_session)2673 static void qla4xxx_relogin_devices(struct iscsi_cls_session *cls_session)
2674 {
2675 	struct iscsi_session *sess;
2676 	struct ddb_entry *ddb_entry;
2677 	struct scsi_qla_host *ha;
2678 
2679 	sess = cls_session->dd_data;
2680 	ddb_entry = sess->dd_data;
2681 	ha = ddb_entry->ha;
2682 	if (!iscsi_is_session_online(cls_session)) {
2683 		if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
2684 			ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
2685 				   " unblock session\n", ha->host_no, __func__,
2686 				   ddb_entry->fw_ddb_index);
2687 			iscsi_unblock_session(ddb_entry->sess);
2688 		} else {
2689 			/* Trigger relogin */
2690 			if (ddb_entry->ddb_type == FLASH_DDB) {
2691 				if (!test_bit(DF_RELOGIN, &ddb_entry->flags))
2692 					qla4xxx_arm_relogin_timer(ddb_entry);
2693 			} else
2694 				iscsi_session_failure(cls_session->dd_data,
2695 						      ISCSI_ERR_CONN_FAILED);
2696 		}
2697 	}
2698 }
2699 
qla4xxx_unblock_flash_ddb(struct iscsi_cls_session * cls_session)2700 int qla4xxx_unblock_flash_ddb(struct iscsi_cls_session *cls_session)
2701 {
2702 	struct iscsi_session *sess;
2703 	struct ddb_entry *ddb_entry;
2704 	struct scsi_qla_host *ha;
2705 
2706 	sess = cls_session->dd_data;
2707 	ddb_entry = sess->dd_data;
2708 	ha = ddb_entry->ha;
2709 	ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
2710 		   " unblock session\n", ha->host_no, __func__,
2711 		   ddb_entry->fw_ddb_index);
2712 
2713 	iscsi_unblock_session(ddb_entry->sess);
2714 
2715 	/* Start scan target */
2716 	if (test_bit(AF_ONLINE, &ha->flags)) {
2717 		ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
2718 			   " start scan\n", ha->host_no, __func__,
2719 			   ddb_entry->fw_ddb_index);
2720 		scsi_queue_work(ha->host, &ddb_entry->sess->scan_work);
2721 	}
2722 	return QLA_SUCCESS;
2723 }
2724 
qla4xxx_unblock_ddb(struct iscsi_cls_session * cls_session)2725 int qla4xxx_unblock_ddb(struct iscsi_cls_session *cls_session)
2726 {
2727 	struct iscsi_session *sess;
2728 	struct ddb_entry *ddb_entry;
2729 	struct scsi_qla_host *ha;
2730 
2731 	sess = cls_session->dd_data;
2732 	ddb_entry = sess->dd_data;
2733 	ha = ddb_entry->ha;
2734 	ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
2735 		   " unblock user space session\n", ha->host_no, __func__,
2736 		   ddb_entry->fw_ddb_index);
2737 	iscsi_conn_start(ddb_entry->conn);
2738 	iscsi_conn_login_event(ddb_entry->conn,
2739 			       ISCSI_CONN_STATE_LOGGED_IN);
2740 
2741 	return QLA_SUCCESS;
2742 }
2743 
qla4xxx_relogin_all_devices(struct scsi_qla_host * ha)2744 static void qla4xxx_relogin_all_devices(struct scsi_qla_host *ha)
2745 {
2746 	iscsi_host_for_each_session(ha->host, qla4xxx_relogin_devices);
2747 }
2748 
qla4xxx_relogin_flash_ddb(struct iscsi_cls_session * cls_sess)2749 static void qla4xxx_relogin_flash_ddb(struct iscsi_cls_session *cls_sess)
2750 {
2751 	uint16_t relogin_timer;
2752 	struct iscsi_session *sess;
2753 	struct ddb_entry *ddb_entry;
2754 	struct scsi_qla_host *ha;
2755 
2756 	sess = cls_sess->dd_data;
2757 	ddb_entry = sess->dd_data;
2758 	ha = ddb_entry->ha;
2759 
2760 	relogin_timer = max(ddb_entry->default_relogin_timeout,
2761 			    (uint16_t)RELOGIN_TOV);
2762 	atomic_set(&ddb_entry->relogin_timer, relogin_timer);
2763 
2764 	DEBUG2(ql4_printk(KERN_INFO, ha,
2765 			  "scsi%ld: Relogin index [%d]. TOV=%d\n", ha->host_no,
2766 			  ddb_entry->fw_ddb_index, relogin_timer));
2767 
2768 	qla4xxx_login_flash_ddb(cls_sess);
2769 }
2770 
qla4xxx_dpc_relogin(struct iscsi_cls_session * cls_sess)2771 static void qla4xxx_dpc_relogin(struct iscsi_cls_session *cls_sess)
2772 {
2773 	struct iscsi_session *sess;
2774 	struct ddb_entry *ddb_entry;
2775 	struct scsi_qla_host *ha;
2776 
2777 	sess = cls_sess->dd_data;
2778 	ddb_entry = sess->dd_data;
2779 	ha = ddb_entry->ha;
2780 
2781 	if (!(ddb_entry->ddb_type == FLASH_DDB))
2782 		return;
2783 
2784 	if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
2785 	    !iscsi_is_session_online(cls_sess)) {
2786 		DEBUG2(ql4_printk(KERN_INFO, ha,
2787 				  "relogin issued\n"));
2788 		qla4xxx_relogin_flash_ddb(cls_sess);
2789 	}
2790 }
2791 
qla4xxx_wake_dpc(struct scsi_qla_host * ha)2792 void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
2793 {
2794 	if (ha->dpc_thread)
2795 		queue_work(ha->dpc_thread, &ha->dpc_work);
2796 }
2797 
2798 /**
2799  * qla4xxx_do_dpc - dpc routine
2800  * @data: in our case pointer to adapter structure
2801  *
2802  * This routine is a task that is schedule by the interrupt handler
2803  * to perform the background processing for interrupts.  We put it
2804  * on a task queue that is consumed whenever the scheduler runs; that's
2805  * so you can do anything (i.e. put the process to sleep etc).  In fact,
2806  * the mid-level tries to sleep when it reaches the driver threshold
2807  * "host->can_queue". This can cause a panic if we were in our interrupt code.
2808  **/
qla4xxx_do_dpc(struct work_struct * work)2809 static void qla4xxx_do_dpc(struct work_struct *work)
2810 {
2811 	struct scsi_qla_host *ha =
2812 		container_of(work, struct scsi_qla_host, dpc_work);
2813 	int status = QLA_ERROR;
2814 
2815 	DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
2816 	    "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
2817 	    ha->host_no, __func__, ha->flags, ha->dpc_flags))
2818 
2819 	/* Initialization not yet finished. Don't do anything yet. */
2820 	if (!test_bit(AF_INIT_DONE, &ha->flags))
2821 		return;
2822 
2823 	if (test_bit(AF_EEH_BUSY, &ha->flags)) {
2824 		DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
2825 		    ha->host_no, __func__, ha->flags));
2826 		return;
2827 	}
2828 
2829 	if (is_qla8022(ha)) {
2830 		if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
2831 			qla4_8xxx_idc_lock(ha);
2832 			qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2833 			    QLA82XX_DEV_FAILED);
2834 			qla4_8xxx_idc_unlock(ha);
2835 			ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
2836 			qla4_8xxx_device_state_handler(ha);
2837 		}
2838 		if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
2839 			qla4_8xxx_need_qsnt_handler(ha);
2840 		}
2841 	}
2842 
2843 	if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
2844 	    (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
2845 	    test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
2846 	    test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
2847 		if (ql4xdontresethba) {
2848 			DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
2849 			    ha->host_no, __func__));
2850 			clear_bit(DPC_RESET_HA, &ha->dpc_flags);
2851 			clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
2852 			clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2853 			goto dpc_post_reset_ha;
2854 		}
2855 		if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
2856 		    test_bit(DPC_RESET_HA, &ha->dpc_flags))
2857 			qla4xxx_recover_adapter(ha);
2858 
2859 		if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
2860 			uint8_t wait_time = RESET_INTR_TOV;
2861 
2862 			while ((readw(&ha->reg->ctrl_status) &
2863 				(CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
2864 				if (--wait_time == 0)
2865 					break;
2866 				msleep(1000);
2867 			}
2868 			if (wait_time == 0)
2869 				DEBUG2(printk("scsi%ld: %s: SR|FSR "
2870 					      "bit not cleared-- resetting\n",
2871 					      ha->host_no, __func__));
2872 			qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
2873 			if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
2874 				qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2875 				status = qla4xxx_recover_adapter(ha);
2876 			}
2877 			clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
2878 			if (status == QLA_SUCCESS)
2879 				ha->isp_ops->enable_intrs(ha);
2880 		}
2881 	}
2882 
2883 dpc_post_reset_ha:
2884 	/* ---- process AEN? --- */
2885 	if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
2886 		qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
2887 
2888 	/* ---- Get DHCP IP Address? --- */
2889 	if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
2890 		qla4xxx_get_dhcp_ip_address(ha);
2891 
2892 	/* ---- relogin device? --- */
2893 	if (adapter_up(ha) &&
2894 	    test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
2895 		iscsi_host_for_each_session(ha->host, qla4xxx_dpc_relogin);
2896 	}
2897 
2898 	/* ---- link change? --- */
2899 	if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
2900 		if (!test_bit(AF_LINK_UP, &ha->flags)) {
2901 			/* ---- link down? --- */
2902 			qla4xxx_mark_all_devices_missing(ha);
2903 		} else {
2904 			/* ---- link up? --- *
2905 			 * F/W will auto login to all devices ONLY ONCE after
2906 			 * link up during driver initialization and runtime
2907 			 * fatal error recovery.  Therefore, the driver must
2908 			 * manually relogin to devices when recovering from
2909 			 * connection failures, logouts, expired KATO, etc. */
2910 			if (test_and_clear_bit(AF_BUILD_DDB_LIST, &ha->flags)) {
2911 				qla4xxx_build_ddb_list(ha, ha->is_reset);
2912 				iscsi_host_for_each_session(ha->host,
2913 						qla4xxx_login_flash_ddb);
2914 			} else
2915 				qla4xxx_relogin_all_devices(ha);
2916 		}
2917 	}
2918 }
2919 
2920 /**
2921  * qla4xxx_free_adapter - release the adapter
2922  * @ha: pointer to adapter structure
2923  **/
qla4xxx_free_adapter(struct scsi_qla_host * ha)2924 static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
2925 {
2926 	qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
2927 
2928 	if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
2929 		/* Turn-off interrupts on the card. */
2930 		ha->isp_ops->disable_intrs(ha);
2931 	}
2932 
2933 	/* Remove timer thread, if present */
2934 	if (ha->timer_active)
2935 		qla4xxx_stop_timer(ha);
2936 
2937 	/* Kill the kernel thread for this host */
2938 	if (ha->dpc_thread)
2939 		destroy_workqueue(ha->dpc_thread);
2940 
2941 	/* Kill the kernel thread for this host */
2942 	if (ha->task_wq)
2943 		destroy_workqueue(ha->task_wq);
2944 
2945 	/* Put firmware in known state */
2946 	ha->isp_ops->reset_firmware(ha);
2947 
2948 	if (is_qla8022(ha)) {
2949 		qla4_8xxx_idc_lock(ha);
2950 		qla4_8xxx_clear_drv_active(ha);
2951 		qla4_8xxx_idc_unlock(ha);
2952 	}
2953 
2954 	/* Detach interrupts */
2955 	if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
2956 		qla4xxx_free_irqs(ha);
2957 
2958 	/* free extra memory */
2959 	qla4xxx_mem_free(ha);
2960 }
2961 
qla4_8xxx_iospace_config(struct scsi_qla_host * ha)2962 int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
2963 {
2964 	int status = 0;
2965 	uint8_t revision_id;
2966 	unsigned long mem_base, mem_len, db_base, db_len;
2967 	struct pci_dev *pdev = ha->pdev;
2968 
2969 	status = pci_request_regions(pdev, DRIVER_NAME);
2970 	if (status) {
2971 		printk(KERN_WARNING
2972 		    "scsi(%ld) Failed to reserve PIO regions (%s) "
2973 		    "status=%d\n", ha->host_no, pci_name(pdev), status);
2974 		goto iospace_error_exit;
2975 	}
2976 
2977 	pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
2978 	DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
2979 	    __func__, revision_id));
2980 	ha->revision_id = revision_id;
2981 
2982 	/* remap phys address */
2983 	mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
2984 	mem_len = pci_resource_len(pdev, 0);
2985 	DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
2986 	    __func__, mem_base, mem_len));
2987 
2988 	/* mapping of pcibase pointer */
2989 	ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
2990 	if (!ha->nx_pcibase) {
2991 		printk(KERN_ERR
2992 		    "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
2993 		pci_release_regions(ha->pdev);
2994 		goto iospace_error_exit;
2995 	}
2996 
2997 	/* Mapping of IO base pointer, door bell read and write pointer */
2998 
2999 	/* mapping of IO base pointer */
3000 	ha->qla4_8xxx_reg =
3001 	    (struct device_reg_82xx  __iomem *)((uint8_t *)ha->nx_pcibase +
3002 	    0xbc000 + (ha->pdev->devfn << 11));
3003 
3004 	db_base = pci_resource_start(pdev, 4);  /* doorbell is on bar 4 */
3005 	db_len = pci_resource_len(pdev, 4);
3006 
3007 	ha->nx_db_wr_ptr = (ha->pdev->devfn == 4 ? QLA82XX_CAM_RAM_DB1 :
3008 	    QLA82XX_CAM_RAM_DB2);
3009 
3010 	return 0;
3011 iospace_error_exit:
3012 	return -ENOMEM;
3013 }
3014 
3015 /***
3016  * qla4xxx_iospace_config - maps registers
3017  * @ha: pointer to adapter structure
3018  *
3019  * This routines maps HBA's registers from the pci address space
3020  * into the kernel virtual address space for memory mapped i/o.
3021  **/
qla4xxx_iospace_config(struct scsi_qla_host * ha)3022 int qla4xxx_iospace_config(struct scsi_qla_host *ha)
3023 {
3024 	unsigned long pio, pio_len, pio_flags;
3025 	unsigned long mmio, mmio_len, mmio_flags;
3026 
3027 	pio = pci_resource_start(ha->pdev, 0);
3028 	pio_len = pci_resource_len(ha->pdev, 0);
3029 	pio_flags = pci_resource_flags(ha->pdev, 0);
3030 	if (pio_flags & IORESOURCE_IO) {
3031 		if (pio_len < MIN_IOBASE_LEN) {
3032 			ql4_printk(KERN_WARNING, ha,
3033 				"Invalid PCI I/O region size\n");
3034 			pio = 0;
3035 		}
3036 	} else {
3037 		ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
3038 		pio = 0;
3039 	}
3040 
3041 	/* Use MMIO operations for all accesses. */
3042 	mmio = pci_resource_start(ha->pdev, 1);
3043 	mmio_len = pci_resource_len(ha->pdev, 1);
3044 	mmio_flags = pci_resource_flags(ha->pdev, 1);
3045 
3046 	if (!(mmio_flags & IORESOURCE_MEM)) {
3047 		ql4_printk(KERN_ERR, ha,
3048 		    "region #0 not an MMIO resource, aborting\n");
3049 
3050 		goto iospace_error_exit;
3051 	}
3052 
3053 	if (mmio_len < MIN_IOBASE_LEN) {
3054 		ql4_printk(KERN_ERR, ha,
3055 		    "Invalid PCI mem region size, aborting\n");
3056 		goto iospace_error_exit;
3057 	}
3058 
3059 	if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
3060 		ql4_printk(KERN_WARNING, ha,
3061 		    "Failed to reserve PIO/MMIO regions\n");
3062 
3063 		goto iospace_error_exit;
3064 	}
3065 
3066 	ha->pio_address = pio;
3067 	ha->pio_length = pio_len;
3068 	ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
3069 	if (!ha->reg) {
3070 		ql4_printk(KERN_ERR, ha,
3071 		    "cannot remap MMIO, aborting\n");
3072 
3073 		goto iospace_error_exit;
3074 	}
3075 
3076 	return 0;
3077 
3078 iospace_error_exit:
3079 	return -ENOMEM;
3080 }
3081 
3082 static struct isp_operations qla4xxx_isp_ops = {
3083 	.iospace_config         = qla4xxx_iospace_config,
3084 	.pci_config             = qla4xxx_pci_config,
3085 	.disable_intrs          = qla4xxx_disable_intrs,
3086 	.enable_intrs           = qla4xxx_enable_intrs,
3087 	.start_firmware         = qla4xxx_start_firmware,
3088 	.intr_handler           = qla4xxx_intr_handler,
3089 	.interrupt_service_routine = qla4xxx_interrupt_service_routine,
3090 	.reset_chip             = qla4xxx_soft_reset,
3091 	.reset_firmware         = qla4xxx_hw_reset,
3092 	.queue_iocb             = qla4xxx_queue_iocb,
3093 	.complete_iocb          = qla4xxx_complete_iocb,
3094 	.rd_shdw_req_q_out      = qla4xxx_rd_shdw_req_q_out,
3095 	.rd_shdw_rsp_q_in       = qla4xxx_rd_shdw_rsp_q_in,
3096 	.get_sys_info           = qla4xxx_get_sys_info,
3097 };
3098 
3099 static struct isp_operations qla4_8xxx_isp_ops = {
3100 	.iospace_config         = qla4_8xxx_iospace_config,
3101 	.pci_config             = qla4_8xxx_pci_config,
3102 	.disable_intrs          = qla4_8xxx_disable_intrs,
3103 	.enable_intrs           = qla4_8xxx_enable_intrs,
3104 	.start_firmware         = qla4_8xxx_load_risc,
3105 	.intr_handler           = qla4_8xxx_intr_handler,
3106 	.interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
3107 	.reset_chip             = qla4_8xxx_isp_reset,
3108 	.reset_firmware         = qla4_8xxx_stop_firmware,
3109 	.queue_iocb             = qla4_8xxx_queue_iocb,
3110 	.complete_iocb          = qla4_8xxx_complete_iocb,
3111 	.rd_shdw_req_q_out      = qla4_8xxx_rd_shdw_req_q_out,
3112 	.rd_shdw_rsp_q_in       = qla4_8xxx_rd_shdw_rsp_q_in,
3113 	.get_sys_info           = qla4_8xxx_get_sys_info,
3114 };
3115 
qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host * ha)3116 uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
3117 {
3118 	return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
3119 }
3120 
qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host * ha)3121 uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
3122 {
3123 	return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
3124 }
3125 
qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host * ha)3126 uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
3127 {
3128 	return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
3129 }
3130 
qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host * ha)3131 uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
3132 {
3133 	return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
3134 }
3135 
qla4xxx_show_boot_eth_info(void * data,int type,char * buf)3136 static ssize_t qla4xxx_show_boot_eth_info(void *data, int type, char *buf)
3137 {
3138 	struct scsi_qla_host *ha = data;
3139 	char *str = buf;
3140 	int rc;
3141 
3142 	switch (type) {
3143 	case ISCSI_BOOT_ETH_FLAGS:
3144 		rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
3145 		break;
3146 	case ISCSI_BOOT_ETH_INDEX:
3147 		rc = sprintf(str, "0\n");
3148 		break;
3149 	case ISCSI_BOOT_ETH_MAC:
3150 		rc = sysfs_format_mac(str, ha->my_mac,
3151 				      MAC_ADDR_LEN);
3152 		break;
3153 	default:
3154 		rc = -ENOSYS;
3155 		break;
3156 	}
3157 	return rc;
3158 }
3159 
qla4xxx_eth_get_attr_visibility(void * data,int type)3160 static umode_t qla4xxx_eth_get_attr_visibility(void *data, int type)
3161 {
3162 	int rc;
3163 
3164 	switch (type) {
3165 	case ISCSI_BOOT_ETH_FLAGS:
3166 	case ISCSI_BOOT_ETH_MAC:
3167 	case ISCSI_BOOT_ETH_INDEX:
3168 		rc = S_IRUGO;
3169 		break;
3170 	default:
3171 		rc = 0;
3172 		break;
3173 	}
3174 	return rc;
3175 }
3176 
qla4xxx_show_boot_ini_info(void * data,int type,char * buf)3177 static ssize_t qla4xxx_show_boot_ini_info(void *data, int type, char *buf)
3178 {
3179 	struct scsi_qla_host *ha = data;
3180 	char *str = buf;
3181 	int rc;
3182 
3183 	switch (type) {
3184 	case ISCSI_BOOT_INI_INITIATOR_NAME:
3185 		rc = sprintf(str, "%s\n", ha->name_string);
3186 		break;
3187 	default:
3188 		rc = -ENOSYS;
3189 		break;
3190 	}
3191 	return rc;
3192 }
3193 
qla4xxx_ini_get_attr_visibility(void * data,int type)3194 static umode_t qla4xxx_ini_get_attr_visibility(void *data, int type)
3195 {
3196 	int rc;
3197 
3198 	switch (type) {
3199 	case ISCSI_BOOT_INI_INITIATOR_NAME:
3200 		rc = S_IRUGO;
3201 		break;
3202 	default:
3203 		rc = 0;
3204 		break;
3205 	}
3206 	return rc;
3207 }
3208 
3209 static ssize_t
qla4xxx_show_boot_tgt_info(struct ql4_boot_session_info * boot_sess,int type,char * buf)3210 qla4xxx_show_boot_tgt_info(struct ql4_boot_session_info *boot_sess, int type,
3211 			   char *buf)
3212 {
3213 	struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
3214 	char *str = buf;
3215 	int rc;
3216 
3217 	switch (type) {
3218 	case ISCSI_BOOT_TGT_NAME:
3219 		rc = sprintf(buf, "%s\n", (char *)&boot_sess->target_name);
3220 		break;
3221 	case ISCSI_BOOT_TGT_IP_ADDR:
3222 		if (boot_sess->conn_list[0].dest_ipaddr.ip_type == 0x1)
3223 			rc = sprintf(buf, "%pI4\n",
3224 				     &boot_conn->dest_ipaddr.ip_address);
3225 		else
3226 			rc = sprintf(str, "%pI6\n",
3227 				     &boot_conn->dest_ipaddr.ip_address);
3228 		break;
3229 	case ISCSI_BOOT_TGT_PORT:
3230 			rc = sprintf(str, "%d\n", boot_conn->dest_port);
3231 		break;
3232 	case ISCSI_BOOT_TGT_CHAP_NAME:
3233 		rc = sprintf(str,  "%.*s\n",
3234 			     boot_conn->chap.target_chap_name_length,
3235 			     (char *)&boot_conn->chap.target_chap_name);
3236 		break;
3237 	case ISCSI_BOOT_TGT_CHAP_SECRET:
3238 		rc = sprintf(str,  "%.*s\n",
3239 			     boot_conn->chap.target_secret_length,
3240 			     (char *)&boot_conn->chap.target_secret);
3241 		break;
3242 	case ISCSI_BOOT_TGT_REV_CHAP_NAME:
3243 		rc = sprintf(str,  "%.*s\n",
3244 			     boot_conn->chap.intr_chap_name_length,
3245 			     (char *)&boot_conn->chap.intr_chap_name);
3246 		break;
3247 	case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
3248 		rc = sprintf(str,  "%.*s\n",
3249 			     boot_conn->chap.intr_secret_length,
3250 			     (char *)&boot_conn->chap.intr_secret);
3251 		break;
3252 	case ISCSI_BOOT_TGT_FLAGS:
3253 		rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
3254 		break;
3255 	case ISCSI_BOOT_TGT_NIC_ASSOC:
3256 		rc = sprintf(str, "0\n");
3257 		break;
3258 	default:
3259 		rc = -ENOSYS;
3260 		break;
3261 	}
3262 	return rc;
3263 }
3264 
qla4xxx_show_boot_tgt_pri_info(void * data,int type,char * buf)3265 static ssize_t qla4xxx_show_boot_tgt_pri_info(void *data, int type, char *buf)
3266 {
3267 	struct scsi_qla_host *ha = data;
3268 	struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_pri_sess);
3269 
3270 	return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
3271 }
3272 
qla4xxx_show_boot_tgt_sec_info(void * data,int type,char * buf)3273 static ssize_t qla4xxx_show_boot_tgt_sec_info(void *data, int type, char *buf)
3274 {
3275 	struct scsi_qla_host *ha = data;
3276 	struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_sec_sess);
3277 
3278 	return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
3279 }
3280 
qla4xxx_tgt_get_attr_visibility(void * data,int type)3281 static umode_t qla4xxx_tgt_get_attr_visibility(void *data, int type)
3282 {
3283 	int rc;
3284 
3285 	switch (type) {
3286 	case ISCSI_BOOT_TGT_NAME:
3287 	case ISCSI_BOOT_TGT_IP_ADDR:
3288 	case ISCSI_BOOT_TGT_PORT:
3289 	case ISCSI_BOOT_TGT_CHAP_NAME:
3290 	case ISCSI_BOOT_TGT_CHAP_SECRET:
3291 	case ISCSI_BOOT_TGT_REV_CHAP_NAME:
3292 	case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
3293 	case ISCSI_BOOT_TGT_NIC_ASSOC:
3294 	case ISCSI_BOOT_TGT_FLAGS:
3295 		rc = S_IRUGO;
3296 		break;
3297 	default:
3298 		rc = 0;
3299 		break;
3300 	}
3301 	return rc;
3302 }
3303 
qla4xxx_boot_release(void * data)3304 static void qla4xxx_boot_release(void *data)
3305 {
3306 	struct scsi_qla_host *ha = data;
3307 
3308 	scsi_host_put(ha->host);
3309 }
3310 
get_fw_boot_info(struct scsi_qla_host * ha,uint16_t ddb_index[])3311 static int get_fw_boot_info(struct scsi_qla_host *ha, uint16_t ddb_index[])
3312 {
3313 	dma_addr_t buf_dma;
3314 	uint32_t addr, pri_addr, sec_addr;
3315 	uint32_t offset;
3316 	uint16_t func_num;
3317 	uint8_t val;
3318 	uint8_t *buf = NULL;
3319 	size_t size = 13 * sizeof(uint8_t);
3320 	int ret = QLA_SUCCESS;
3321 
3322 	func_num = PCI_FUNC(ha->pdev->devfn);
3323 
3324 	ql4_printk(KERN_INFO, ha, "%s: Get FW boot info for 0x%x func %d\n",
3325 		   __func__, ha->pdev->device, func_num);
3326 
3327 	if (is_qla40XX(ha)) {
3328 		if (func_num == 1) {
3329 			addr = NVRAM_PORT0_BOOT_MODE;
3330 			pri_addr = NVRAM_PORT0_BOOT_PRI_TGT;
3331 			sec_addr = NVRAM_PORT0_BOOT_SEC_TGT;
3332 		} else if (func_num == 3) {
3333 			addr = NVRAM_PORT1_BOOT_MODE;
3334 			pri_addr = NVRAM_PORT1_BOOT_PRI_TGT;
3335 			sec_addr = NVRAM_PORT1_BOOT_SEC_TGT;
3336 		} else {
3337 			ret = QLA_ERROR;
3338 			goto exit_boot_info;
3339 		}
3340 
3341 		/* Check Boot Mode */
3342 		val = rd_nvram_byte(ha, addr);
3343 		if (!(val & 0x07)) {
3344 			DEBUG2(ql4_printk(KERN_ERR, ha,
3345 					  "%s: Failed Boot options : 0x%x\n",
3346 					  __func__, val));
3347 			ret = QLA_ERROR;
3348 			goto exit_boot_info;
3349 		}
3350 
3351 		/* get primary valid target index */
3352 		val = rd_nvram_byte(ha, pri_addr);
3353 		if (val & BIT_7)
3354 			ddb_index[0] = (val & 0x7f);
3355 
3356 		/* get secondary valid target index */
3357 		val = rd_nvram_byte(ha, sec_addr);
3358 		if (val & BIT_7)
3359 			ddb_index[1] = (val & 0x7f);
3360 
3361 	} else if (is_qla8022(ha)) {
3362 		buf = dma_alloc_coherent(&ha->pdev->dev, size,
3363 					 &buf_dma, GFP_KERNEL);
3364 		if (!buf) {
3365 			DEBUG2(ql4_printk(KERN_ERR, ha,
3366 					  "%s: Unable to allocate dma buffer\n",
3367 					   __func__));
3368 			ret = QLA_ERROR;
3369 			goto exit_boot_info;
3370 		}
3371 
3372 		if (ha->port_num == 0)
3373 			offset = BOOT_PARAM_OFFSET_PORT0;
3374 		else if (ha->port_num == 1)
3375 			offset = BOOT_PARAM_OFFSET_PORT1;
3376 		else {
3377 			ret = QLA_ERROR;
3378 			goto exit_boot_info_free;
3379 		}
3380 		addr = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_iscsi_param * 4) +
3381 		       offset;
3382 		if (qla4xxx_get_flash(ha, buf_dma, addr,
3383 				      13 * sizeof(uint8_t)) != QLA_SUCCESS) {
3384 			DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: Get Flash"
3385 					  "failed\n", ha->host_no, __func__));
3386 			ret = QLA_ERROR;
3387 			goto exit_boot_info_free;
3388 		}
3389 		/* Check Boot Mode */
3390 		if (!(buf[1] & 0x07)) {
3391 			DEBUG2(ql4_printk(KERN_INFO, ha,
3392 					  "Failed: Boot options : 0x%x\n",
3393 					  buf[1]));
3394 			ret = QLA_ERROR;
3395 			goto exit_boot_info_free;
3396 		}
3397 
3398 		/* get primary valid target index */
3399 		if (buf[2] & BIT_7)
3400 			ddb_index[0] = buf[2] & 0x7f;
3401 
3402 		/* get secondary valid target index */
3403 		if (buf[11] & BIT_7)
3404 			ddb_index[1] = buf[11] & 0x7f;
3405 	} else {
3406 		ret = QLA_ERROR;
3407 		goto exit_boot_info;
3408 	}
3409 
3410 	DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Primary target ID %d, Secondary"
3411 			  " target ID %d\n", __func__, ddb_index[0],
3412 			  ddb_index[1]));
3413 
3414 	ha->pri_ddb_idx = ddb_index[0];
3415 	ha->sec_ddb_idx = ddb_index[1];
3416 
3417 exit_boot_info_free:
3418 	dma_free_coherent(&ha->pdev->dev, size, buf, buf_dma);
3419 exit_boot_info:
3420 	return ret;
3421 }
3422 
3423 /**
3424  * qla4xxx_get_bidi_chap - Get a BIDI CHAP user and password
3425  * @ha: pointer to adapter structure
3426  * @username: CHAP username to be returned
3427  * @password: CHAP password to be returned
3428  *
3429  * If a boot entry has BIDI CHAP enabled then we need to set the BIDI CHAP
3430  * user and password in the sysfs entry in /sys/firmware/iscsi_boot#/.
3431  * So from the CHAP cache find the first BIDI CHAP entry and set it
3432  * to the boot record in sysfs.
3433  **/
qla4xxx_get_bidi_chap(struct scsi_qla_host * ha,char * username,char * password)3434 static int qla4xxx_get_bidi_chap(struct scsi_qla_host *ha, char *username,
3435 			    char *password)
3436 {
3437 	int i, ret = -EINVAL;
3438 	int max_chap_entries = 0;
3439 	struct ql4_chap_table *chap_table;
3440 
3441 	if (is_qla8022(ha))
3442 		max_chap_entries = (ha->hw.flt_chap_size / 2) /
3443 						sizeof(struct ql4_chap_table);
3444 	else
3445 		max_chap_entries = MAX_CHAP_ENTRIES_40XX;
3446 
3447 	if (!ha->chap_list) {
3448 		ql4_printk(KERN_ERR, ha, "Do not have CHAP table cache\n");
3449 		return ret;
3450 	}
3451 
3452 	mutex_lock(&ha->chap_sem);
3453 	for (i = 0; i < max_chap_entries; i++) {
3454 		chap_table = (struct ql4_chap_table *)ha->chap_list + i;
3455 		if (chap_table->cookie !=
3456 		    __constant_cpu_to_le16(CHAP_VALID_COOKIE)) {
3457 			continue;
3458 		}
3459 
3460 		if (chap_table->flags & BIT_7) /* local */
3461 			continue;
3462 
3463 		if (!(chap_table->flags & BIT_6)) /* Not BIDI */
3464 			continue;
3465 
3466 		strncpy(password, chap_table->secret, QL4_CHAP_MAX_SECRET_LEN);
3467 		strncpy(username, chap_table->name, QL4_CHAP_MAX_NAME_LEN);
3468 		ret = 0;
3469 		break;
3470 	}
3471 	mutex_unlock(&ha->chap_sem);
3472 
3473 	return ret;
3474 }
3475 
3476 
qla4xxx_get_boot_target(struct scsi_qla_host * ha,struct ql4_boot_session_info * boot_sess,uint16_t ddb_index)3477 static int qla4xxx_get_boot_target(struct scsi_qla_host *ha,
3478 				   struct ql4_boot_session_info *boot_sess,
3479 				   uint16_t ddb_index)
3480 {
3481 	struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
3482 	struct dev_db_entry *fw_ddb_entry;
3483 	dma_addr_t fw_ddb_entry_dma;
3484 	uint16_t idx;
3485 	uint16_t options;
3486 	int ret = QLA_SUCCESS;
3487 
3488 	fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3489 					  &fw_ddb_entry_dma, GFP_KERNEL);
3490 	if (!fw_ddb_entry) {
3491 		DEBUG2(ql4_printk(KERN_ERR, ha,
3492 				  "%s: Unable to allocate dma buffer.\n",
3493 				  __func__));
3494 		ret = QLA_ERROR;
3495 		return ret;
3496 	}
3497 
3498 	if (qla4xxx_bootdb_by_index(ha, fw_ddb_entry,
3499 				   fw_ddb_entry_dma, ddb_index)) {
3500 		DEBUG2(ql4_printk(KERN_ERR, ha,
3501 				  "%s: Flash DDB read Failed\n", __func__));
3502 		ret = QLA_ERROR;
3503 		goto exit_boot_target;
3504 	}
3505 
3506 	/* Update target name and IP from DDB */
3507 	memcpy(boot_sess->target_name, fw_ddb_entry->iscsi_name,
3508 	       min(sizeof(boot_sess->target_name),
3509 		   sizeof(fw_ddb_entry->iscsi_name)));
3510 
3511 	options = le16_to_cpu(fw_ddb_entry->options);
3512 	if (options & DDB_OPT_IPV6_DEVICE) {
3513 		memcpy(&boot_conn->dest_ipaddr.ip_address,
3514 		       &fw_ddb_entry->ip_addr[0], IPv6_ADDR_LEN);
3515 	} else {
3516 		boot_conn->dest_ipaddr.ip_type = 0x1;
3517 		memcpy(&boot_conn->dest_ipaddr.ip_address,
3518 		       &fw_ddb_entry->ip_addr[0], IP_ADDR_LEN);
3519 	}
3520 
3521 	boot_conn->dest_port = le16_to_cpu(fw_ddb_entry->port);
3522 
3523 	/* update chap information */
3524 	idx = __le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
3525 
3526 	if (BIT_7 & le16_to_cpu(fw_ddb_entry->iscsi_options))	{
3527 
3528 		DEBUG2(ql4_printk(KERN_INFO, ha, "Setting chap\n"));
3529 
3530 		ret = qla4xxx_get_chap(ha, (char *)&boot_conn->chap.
3531 				       target_chap_name,
3532 				       (char *)&boot_conn->chap.target_secret,
3533 				       idx);
3534 		if (ret) {
3535 			ql4_printk(KERN_ERR, ha, "Failed to set chap\n");
3536 			ret = QLA_ERROR;
3537 			goto exit_boot_target;
3538 		}
3539 
3540 		boot_conn->chap.target_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
3541 		boot_conn->chap.target_secret_length = QL4_CHAP_MAX_SECRET_LEN;
3542 	}
3543 
3544 	if (BIT_4 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
3545 
3546 		DEBUG2(ql4_printk(KERN_INFO, ha, "Setting BIDI chap\n"));
3547 
3548 		ret = qla4xxx_get_bidi_chap(ha,
3549 				    (char *)&boot_conn->chap.intr_chap_name,
3550 				    (char *)&boot_conn->chap.intr_secret);
3551 
3552 		if (ret) {
3553 			ql4_printk(KERN_ERR, ha, "Failed to set BIDI chap\n");
3554 			ret = QLA_ERROR;
3555 			goto exit_boot_target;
3556 		}
3557 
3558 		boot_conn->chap.intr_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
3559 		boot_conn->chap.intr_secret_length = QL4_CHAP_MAX_SECRET_LEN;
3560 	}
3561 
3562 exit_boot_target:
3563 	dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3564 			  fw_ddb_entry, fw_ddb_entry_dma);
3565 	return ret;
3566 }
3567 
qla4xxx_get_boot_info(struct scsi_qla_host * ha)3568 static int qla4xxx_get_boot_info(struct scsi_qla_host *ha)
3569 {
3570 	uint16_t ddb_index[2];
3571 	int ret = QLA_ERROR;
3572 	int rval;
3573 
3574 	memset(ddb_index, 0, sizeof(ddb_index));
3575 	ddb_index[0] = 0xffff;
3576 	ddb_index[1] = 0xffff;
3577 	ret = get_fw_boot_info(ha, ddb_index);
3578 	if (ret != QLA_SUCCESS) {
3579 		DEBUG2(ql4_printk(KERN_ERR, ha,
3580 				  "%s: Failed to set boot info.\n", __func__));
3581 		return ret;
3582 	}
3583 
3584 	if (ql4xdisablesysfsboot)
3585 		return QLA_SUCCESS;
3586 
3587 	if (ddb_index[0] == 0xffff)
3588 		goto sec_target;
3589 
3590 	rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_pri_sess),
3591 				      ddb_index[0]);
3592 	if (rval != QLA_SUCCESS) {
3593 		DEBUG2(ql4_printk(KERN_ERR, ha, "%s: Failed to get "
3594 				  "primary target\n", __func__));
3595 	} else
3596 		ret = QLA_SUCCESS;
3597 
3598 sec_target:
3599 	if (ddb_index[1] == 0xffff)
3600 		goto exit_get_boot_info;
3601 
3602 	rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_sec_sess),
3603 				      ddb_index[1]);
3604 	if (rval != QLA_SUCCESS) {
3605 		DEBUG2(ql4_printk(KERN_ERR, ha, "%s: Failed to get "
3606 				  "secondary target\n", __func__));
3607 	} else
3608 		ret = QLA_SUCCESS;
3609 
3610 exit_get_boot_info:
3611 	return ret;
3612 }
3613 
qla4xxx_setup_boot_info(struct scsi_qla_host * ha)3614 static int qla4xxx_setup_boot_info(struct scsi_qla_host *ha)
3615 {
3616 	struct iscsi_boot_kobj *boot_kobj;
3617 
3618 	if (qla4xxx_get_boot_info(ha) != QLA_SUCCESS)
3619 		return QLA_ERROR;
3620 
3621 	if (ql4xdisablesysfsboot) {
3622 		ql4_printk(KERN_INFO, ha,
3623 			   "%s: syfsboot disabled - driver will trigger login"
3624 			   "and publish session for discovery .\n", __func__);
3625 		return QLA_SUCCESS;
3626 	}
3627 
3628 
3629 	ha->boot_kset = iscsi_boot_create_host_kset(ha->host->host_no);
3630 	if (!ha->boot_kset)
3631 		goto kset_free;
3632 
3633 	if (!scsi_host_get(ha->host))
3634 		goto kset_free;
3635 	boot_kobj = iscsi_boot_create_target(ha->boot_kset, 0, ha,
3636 					     qla4xxx_show_boot_tgt_pri_info,
3637 					     qla4xxx_tgt_get_attr_visibility,
3638 					     qla4xxx_boot_release);
3639 	if (!boot_kobj)
3640 		goto put_host;
3641 
3642 	if (!scsi_host_get(ha->host))
3643 		goto kset_free;
3644 	boot_kobj = iscsi_boot_create_target(ha->boot_kset, 1, ha,
3645 					     qla4xxx_show_boot_tgt_sec_info,
3646 					     qla4xxx_tgt_get_attr_visibility,
3647 					     qla4xxx_boot_release);
3648 	if (!boot_kobj)
3649 		goto put_host;
3650 
3651 	if (!scsi_host_get(ha->host))
3652 		goto kset_free;
3653 	boot_kobj = iscsi_boot_create_initiator(ha->boot_kset, 0, ha,
3654 					       qla4xxx_show_boot_ini_info,
3655 					       qla4xxx_ini_get_attr_visibility,
3656 					       qla4xxx_boot_release);
3657 	if (!boot_kobj)
3658 		goto put_host;
3659 
3660 	if (!scsi_host_get(ha->host))
3661 		goto kset_free;
3662 	boot_kobj = iscsi_boot_create_ethernet(ha->boot_kset, 0, ha,
3663 					       qla4xxx_show_boot_eth_info,
3664 					       qla4xxx_eth_get_attr_visibility,
3665 					       qla4xxx_boot_release);
3666 	if (!boot_kobj)
3667 		goto put_host;
3668 
3669 	return QLA_SUCCESS;
3670 
3671 put_host:
3672 	scsi_host_put(ha->host);
3673 kset_free:
3674 	iscsi_boot_destroy_kset(ha->boot_kset);
3675 	return -ENOMEM;
3676 }
3677 
3678 
3679 /**
3680  * qla4xxx_create chap_list - Create CHAP list from FLASH
3681  * @ha: pointer to adapter structure
3682  *
3683  * Read flash and make a list of CHAP entries, during login when a CHAP entry
3684  * is received, it will be checked in this list. If entry exist then the CHAP
3685  * entry index is set in the DDB. If CHAP entry does not exist in this list
3686  * then a new entry is added in FLASH in CHAP table and the index obtained is
3687  * used in the DDB.
3688  **/
qla4xxx_create_chap_list(struct scsi_qla_host * ha)3689 static void qla4xxx_create_chap_list(struct scsi_qla_host *ha)
3690 {
3691 	int rval = 0;
3692 	uint8_t *chap_flash_data = NULL;
3693 	uint32_t offset;
3694 	dma_addr_t chap_dma;
3695 	uint32_t chap_size = 0;
3696 
3697 	if (is_qla40XX(ha))
3698 		chap_size = MAX_CHAP_ENTRIES_40XX  *
3699 					sizeof(struct ql4_chap_table);
3700 	else	/* Single region contains CHAP info for both
3701 		 * ports which is divided into half for each port.
3702 		 */
3703 		chap_size = ha->hw.flt_chap_size / 2;
3704 
3705 	chap_flash_data = dma_alloc_coherent(&ha->pdev->dev, chap_size,
3706 					  &chap_dma, GFP_KERNEL);
3707 	if (!chap_flash_data) {
3708 		ql4_printk(KERN_ERR, ha, "No memory for chap_flash_data\n");
3709 		return;
3710 	}
3711 	if (is_qla40XX(ha))
3712 		offset = FLASH_CHAP_OFFSET;
3713 	else {
3714 		offset = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_region_chap << 2);
3715 		if (ha->port_num == 1)
3716 			offset += chap_size;
3717 	}
3718 
3719 	rval = qla4xxx_get_flash(ha, chap_dma, offset, chap_size);
3720 	if (rval != QLA_SUCCESS)
3721 		goto exit_chap_list;
3722 
3723 	if (ha->chap_list == NULL)
3724 		ha->chap_list = vmalloc(chap_size);
3725 	if (ha->chap_list == NULL) {
3726 		ql4_printk(KERN_ERR, ha, "No memory for ha->chap_list\n");
3727 		goto exit_chap_list;
3728 	}
3729 
3730 	memcpy(ha->chap_list, chap_flash_data, chap_size);
3731 
3732 exit_chap_list:
3733 	dma_free_coherent(&ha->pdev->dev, chap_size,
3734 			chap_flash_data, chap_dma);
3735 }
3736 
qla4xxx_get_param_ddb(struct ddb_entry * ddb_entry,struct ql4_tuple_ddb * tddb)3737 static void qla4xxx_get_param_ddb(struct ddb_entry *ddb_entry,
3738 				  struct ql4_tuple_ddb *tddb)
3739 {
3740 	struct scsi_qla_host *ha;
3741 	struct iscsi_cls_session *cls_sess;
3742 	struct iscsi_cls_conn *cls_conn;
3743 	struct iscsi_session *sess;
3744 	struct iscsi_conn *conn;
3745 
3746 	DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
3747 	ha = ddb_entry->ha;
3748 	cls_sess = ddb_entry->sess;
3749 	sess = cls_sess->dd_data;
3750 	cls_conn = ddb_entry->conn;
3751 	conn = cls_conn->dd_data;
3752 
3753 	tddb->tpgt = sess->tpgt;
3754 	tddb->port = conn->persistent_port;
3755 	strncpy(tddb->iscsi_name, sess->targetname, ISCSI_NAME_SIZE);
3756 	strncpy(tddb->ip_addr, conn->persistent_address, DDB_IPADDR_LEN);
3757 }
3758 
qla4xxx_convert_param_ddb(struct dev_db_entry * fw_ddb_entry,struct ql4_tuple_ddb * tddb)3759 static void qla4xxx_convert_param_ddb(struct dev_db_entry *fw_ddb_entry,
3760 				      struct ql4_tuple_ddb *tddb)
3761 {
3762 	uint16_t options = 0;
3763 
3764 	tddb->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
3765 	memcpy(&tddb->iscsi_name[0], &fw_ddb_entry->iscsi_name[0],
3766 	       min(sizeof(tddb->iscsi_name), sizeof(fw_ddb_entry->iscsi_name)));
3767 
3768 	options = le16_to_cpu(fw_ddb_entry->options);
3769 	if (options & DDB_OPT_IPV6_DEVICE)
3770 		sprintf(tddb->ip_addr, "%pI6", fw_ddb_entry->ip_addr);
3771 	else
3772 		sprintf(tddb->ip_addr, "%pI4", fw_ddb_entry->ip_addr);
3773 
3774 	tddb->port = le16_to_cpu(fw_ddb_entry->port);
3775 }
3776 
qla4xxx_compare_tuple_ddb(struct scsi_qla_host * ha,struct ql4_tuple_ddb * old_tddb,struct ql4_tuple_ddb * new_tddb)3777 static int qla4xxx_compare_tuple_ddb(struct scsi_qla_host *ha,
3778 				     struct ql4_tuple_ddb *old_tddb,
3779 				     struct ql4_tuple_ddb *new_tddb)
3780 {
3781 	if (strcmp(old_tddb->iscsi_name, new_tddb->iscsi_name))
3782 		return QLA_ERROR;
3783 
3784 	if (strcmp(old_tddb->ip_addr, new_tddb->ip_addr))
3785 		return QLA_ERROR;
3786 
3787 	if (old_tddb->port != new_tddb->port)
3788 		return QLA_ERROR;
3789 
3790 	DEBUG2(ql4_printk(KERN_INFO, ha,
3791 			  "Match Found, fw[%d,%d,%s,%s], [%d,%d,%s,%s]",
3792 			  old_tddb->port, old_tddb->tpgt, old_tddb->ip_addr,
3793 			  old_tddb->iscsi_name, new_tddb->port, new_tddb->tpgt,
3794 			  new_tddb->ip_addr, new_tddb->iscsi_name));
3795 
3796 	return QLA_SUCCESS;
3797 }
3798 
qla4xxx_is_session_exists(struct scsi_qla_host * ha,struct dev_db_entry * fw_ddb_entry)3799 static int qla4xxx_is_session_exists(struct scsi_qla_host *ha,
3800 				     struct dev_db_entry *fw_ddb_entry)
3801 {
3802 	struct ddb_entry *ddb_entry;
3803 	struct ql4_tuple_ddb *fw_tddb = NULL;
3804 	struct ql4_tuple_ddb *tmp_tddb = NULL;
3805 	int idx;
3806 	int ret = QLA_ERROR;
3807 
3808 	fw_tddb = vzalloc(sizeof(*fw_tddb));
3809 	if (!fw_tddb) {
3810 		DEBUG2(ql4_printk(KERN_WARNING, ha,
3811 				  "Memory Allocation failed.\n"));
3812 		ret = QLA_SUCCESS;
3813 		goto exit_check;
3814 	}
3815 
3816 	tmp_tddb = vzalloc(sizeof(*tmp_tddb));
3817 	if (!tmp_tddb) {
3818 		DEBUG2(ql4_printk(KERN_WARNING, ha,
3819 				  "Memory Allocation failed.\n"));
3820 		ret = QLA_SUCCESS;
3821 		goto exit_check;
3822 	}
3823 
3824 	qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb);
3825 
3826 	for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
3827 		ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
3828 		if (ddb_entry == NULL)
3829 			continue;
3830 
3831 		qla4xxx_get_param_ddb(ddb_entry, tmp_tddb);
3832 		if (!qla4xxx_compare_tuple_ddb(ha, fw_tddb, tmp_tddb)) {
3833 			ret = QLA_SUCCESS; /* found */
3834 			goto exit_check;
3835 		}
3836 	}
3837 
3838 exit_check:
3839 	if (fw_tddb)
3840 		vfree(fw_tddb);
3841 	if (tmp_tddb)
3842 		vfree(tmp_tddb);
3843 	return ret;
3844 }
3845 
qla4xxx_is_flash_ddb_exists(struct scsi_qla_host * ha,struct list_head * list_nt,struct dev_db_entry * fw_ddb_entry)3846 static int qla4xxx_is_flash_ddb_exists(struct scsi_qla_host *ha,
3847 				       struct list_head *list_nt,
3848 				       struct dev_db_entry *fw_ddb_entry)
3849 {
3850 	struct qla_ddb_index  *nt_ddb_idx, *nt_ddb_idx_tmp;
3851 	struct ql4_tuple_ddb *fw_tddb = NULL;
3852 	struct ql4_tuple_ddb *tmp_tddb = NULL;
3853 	int ret = QLA_ERROR;
3854 
3855 	fw_tddb = vzalloc(sizeof(*fw_tddb));
3856 	if (!fw_tddb) {
3857 		DEBUG2(ql4_printk(KERN_WARNING, ha,
3858 				  "Memory Allocation failed.\n"));
3859 		ret = QLA_SUCCESS;
3860 		goto exit_check;
3861 	}
3862 
3863 	tmp_tddb = vzalloc(sizeof(*tmp_tddb));
3864 	if (!tmp_tddb) {
3865 		DEBUG2(ql4_printk(KERN_WARNING, ha,
3866 				  "Memory Allocation failed.\n"));
3867 		ret = QLA_SUCCESS;
3868 		goto exit_check;
3869 	}
3870 
3871 	qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb);
3872 
3873 	list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
3874 		qla4xxx_convert_param_ddb(&nt_ddb_idx->fw_ddb, tmp_tddb);
3875 		if (!qla4xxx_compare_tuple_ddb(ha, fw_tddb, tmp_tddb)) {
3876 			ret = QLA_SUCCESS; /* found */
3877 			goto exit_check;
3878 		}
3879 	}
3880 
3881 exit_check:
3882 	if (fw_tddb)
3883 		vfree(fw_tddb);
3884 	if (tmp_tddb)
3885 		vfree(tmp_tddb);
3886 	return ret;
3887 }
3888 
qla4xxx_free_ddb_list(struct list_head * list_ddb)3889 static void qla4xxx_free_ddb_list(struct list_head *list_ddb)
3890 {
3891 	struct qla_ddb_index  *ddb_idx, *ddb_idx_tmp;
3892 
3893 	list_for_each_entry_safe(ddb_idx, ddb_idx_tmp, list_ddb, list) {
3894 		list_del_init(&ddb_idx->list);
3895 		vfree(ddb_idx);
3896 	}
3897 }
3898 
qla4xxx_get_ep_fwdb(struct scsi_qla_host * ha,struct dev_db_entry * fw_ddb_entry)3899 static struct iscsi_endpoint *qla4xxx_get_ep_fwdb(struct scsi_qla_host *ha,
3900 					struct dev_db_entry *fw_ddb_entry)
3901 {
3902 	struct iscsi_endpoint *ep;
3903 	struct sockaddr_in *addr;
3904 	struct sockaddr_in6 *addr6;
3905 	struct sockaddr *dst_addr;
3906 	char *ip;
3907 
3908 	/* TODO: need to destroy on unload iscsi_endpoint*/
3909 	dst_addr = vmalloc(sizeof(*dst_addr));
3910 	if (!dst_addr)
3911 		return NULL;
3912 
3913 	if (fw_ddb_entry->options & DDB_OPT_IPV6_DEVICE) {
3914 		dst_addr->sa_family = AF_INET6;
3915 		addr6 = (struct sockaddr_in6 *)dst_addr;
3916 		ip = (char *)&addr6->sin6_addr;
3917 		memcpy(ip, fw_ddb_entry->ip_addr, IPv6_ADDR_LEN);
3918 		addr6->sin6_port = htons(le16_to_cpu(fw_ddb_entry->port));
3919 
3920 	} else {
3921 		dst_addr->sa_family = AF_INET;
3922 		addr = (struct sockaddr_in *)dst_addr;
3923 		ip = (char *)&addr->sin_addr;
3924 		memcpy(ip, fw_ddb_entry->ip_addr, IP_ADDR_LEN);
3925 		addr->sin_port = htons(le16_to_cpu(fw_ddb_entry->port));
3926 	}
3927 
3928 	ep = qla4xxx_ep_connect(ha->host, dst_addr, 0);
3929 	vfree(dst_addr);
3930 	return ep;
3931 }
3932 
qla4xxx_verify_boot_idx(struct scsi_qla_host * ha,uint16_t idx)3933 static int qla4xxx_verify_boot_idx(struct scsi_qla_host *ha, uint16_t idx)
3934 {
3935 	if (ql4xdisablesysfsboot)
3936 		return QLA_SUCCESS;
3937 	if (idx == ha->pri_ddb_idx || idx == ha->sec_ddb_idx)
3938 		return QLA_ERROR;
3939 	return QLA_SUCCESS;
3940 }
3941 
qla4xxx_setup_flash_ddb_entry(struct scsi_qla_host * ha,struct ddb_entry * ddb_entry)3942 static void qla4xxx_setup_flash_ddb_entry(struct scsi_qla_host *ha,
3943 					  struct ddb_entry *ddb_entry)
3944 {
3945 	uint16_t def_timeout;
3946 
3947 	ddb_entry->ddb_type = FLASH_DDB;
3948 	ddb_entry->fw_ddb_index = INVALID_ENTRY;
3949 	ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
3950 	ddb_entry->ha = ha;
3951 	ddb_entry->unblock_sess = qla4xxx_unblock_flash_ddb;
3952 	ddb_entry->ddb_change = qla4xxx_flash_ddb_change;
3953 
3954 	atomic_set(&ddb_entry->retry_relogin_timer, INVALID_ENTRY);
3955 	atomic_set(&ddb_entry->relogin_timer, 0);
3956 	atomic_set(&ddb_entry->relogin_retry_count, 0);
3957 	def_timeout = le16_to_cpu(ddb_entry->fw_ddb_entry.def_timeout);
3958 	ddb_entry->default_relogin_timeout =
3959 		(def_timeout > LOGIN_TOV) && (def_timeout < LOGIN_TOV * 10) ?
3960 		def_timeout : LOGIN_TOV;
3961 	ddb_entry->default_time2wait =
3962 		le16_to_cpu(ddb_entry->fw_ddb_entry.iscsi_def_time2wait);
3963 }
3964 
qla4xxx_wait_for_ip_configuration(struct scsi_qla_host * ha)3965 static void qla4xxx_wait_for_ip_configuration(struct scsi_qla_host *ha)
3966 {
3967 	uint32_t idx = 0;
3968 	uint32_t ip_idx[IP_ADDR_COUNT] = {0, 1, 2, 3}; /* 4 IP interfaces */
3969 	uint32_t sts[MBOX_REG_COUNT];
3970 	uint32_t ip_state;
3971 	unsigned long wtime;
3972 	int ret;
3973 
3974 	wtime = jiffies + (HZ * IP_CONFIG_TOV);
3975 	do {
3976 		for (idx = 0; idx < IP_ADDR_COUNT; idx++) {
3977 			if (ip_idx[idx] == -1)
3978 				continue;
3979 
3980 			ret = qla4xxx_get_ip_state(ha, 0, ip_idx[idx], sts);
3981 
3982 			if (ret == QLA_ERROR) {
3983 				ip_idx[idx] = -1;
3984 				continue;
3985 			}
3986 
3987 			ip_state = (sts[1] & IP_STATE_MASK) >> IP_STATE_SHIFT;
3988 
3989 			DEBUG2(ql4_printk(KERN_INFO, ha,
3990 					  "Waiting for IP state for idx = %d, state = 0x%x\n",
3991 					  ip_idx[idx], ip_state));
3992 			if (ip_state == IP_ADDRSTATE_UNCONFIGURED ||
3993 			    ip_state == IP_ADDRSTATE_INVALID ||
3994 			    ip_state == IP_ADDRSTATE_PREFERRED ||
3995 			    ip_state == IP_ADDRSTATE_DEPRICATED ||
3996 			    ip_state == IP_ADDRSTATE_DISABLING)
3997 				ip_idx[idx] = -1;
3998 		}
3999 
4000 		/* Break if all IP states checked */
4001 		if ((ip_idx[0] == -1) &&
4002 		    (ip_idx[1] == -1) &&
4003 		    (ip_idx[2] == -1) &&
4004 		    (ip_idx[3] == -1))
4005 			break;
4006 		schedule_timeout_uninterruptible(HZ);
4007 	} while (time_after(wtime, jiffies));
4008 }
4009 
qla4xxx_build_st_list(struct scsi_qla_host * ha,struct list_head * list_st)4010 static void qla4xxx_build_st_list(struct scsi_qla_host *ha,
4011 				  struct list_head *list_st)
4012 {
4013 	struct qla_ddb_index  *st_ddb_idx;
4014 	int max_ddbs;
4015 	int fw_idx_size;
4016 	struct dev_db_entry *fw_ddb_entry;
4017 	dma_addr_t fw_ddb_dma;
4018 	int ret;
4019 	uint32_t idx = 0, next_idx = 0;
4020 	uint32_t state = 0, conn_err = 0;
4021 	uint16_t conn_id = 0;
4022 
4023 	fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
4024 				      &fw_ddb_dma);
4025 	if (fw_ddb_entry == NULL) {
4026 		DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
4027 		goto exit_st_list;
4028 	}
4029 
4030 	max_ddbs =  is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
4031 				     MAX_DEV_DB_ENTRIES;
4032 	fw_idx_size = sizeof(struct qla_ddb_index);
4033 
4034 	for (idx = 0; idx < max_ddbs; idx = next_idx) {
4035 		ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry, fw_ddb_dma,
4036 					      NULL, &next_idx, &state,
4037 					      &conn_err, NULL, &conn_id);
4038 		if (ret == QLA_ERROR)
4039 			break;
4040 
4041 		/* Check if ST, add to the list_st */
4042 		if (strlen((char *) fw_ddb_entry->iscsi_name) != 0)
4043 			goto continue_next_st;
4044 
4045 		st_ddb_idx = vzalloc(fw_idx_size);
4046 		if (!st_ddb_idx)
4047 			break;
4048 
4049 		st_ddb_idx->fw_ddb_idx = idx;
4050 
4051 		list_add_tail(&st_ddb_idx->list, list_st);
4052 continue_next_st:
4053 		if (next_idx == 0)
4054 			break;
4055 	}
4056 
4057 exit_st_list:
4058 	if (fw_ddb_entry)
4059 		dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
4060 }
4061 
4062 /**
4063  * qla4xxx_remove_failed_ddb - Remove inactive or failed ddb from list
4064  * @ha: pointer to adapter structure
4065  * @list_ddb: List from which failed ddb to be removed
4066  *
4067  * Iterate over the list of DDBs and find and remove DDBs that are either in
4068  * no connection active state or failed state
4069  **/
qla4xxx_remove_failed_ddb(struct scsi_qla_host * ha,struct list_head * list_ddb)4070 static void qla4xxx_remove_failed_ddb(struct scsi_qla_host *ha,
4071 				      struct list_head *list_ddb)
4072 {
4073 	struct qla_ddb_index  *ddb_idx, *ddb_idx_tmp;
4074 	uint32_t next_idx = 0;
4075 	uint32_t state = 0, conn_err = 0;
4076 	int ret;
4077 
4078 	list_for_each_entry_safe(ddb_idx, ddb_idx_tmp, list_ddb, list) {
4079 		ret = qla4xxx_get_fwddb_entry(ha, ddb_idx->fw_ddb_idx,
4080 					      NULL, 0, NULL, &next_idx, &state,
4081 					      &conn_err, NULL, NULL);
4082 		if (ret == QLA_ERROR)
4083 			continue;
4084 
4085 		if (state == DDB_DS_NO_CONNECTION_ACTIVE ||
4086 		    state == DDB_DS_SESSION_FAILED) {
4087 			list_del_init(&ddb_idx->list);
4088 			vfree(ddb_idx);
4089 		}
4090 	}
4091 }
4092 
qla4xxx_sess_conn_setup(struct scsi_qla_host * ha,struct dev_db_entry * fw_ddb_entry,int is_reset)4093 static int qla4xxx_sess_conn_setup(struct scsi_qla_host *ha,
4094 				   struct dev_db_entry *fw_ddb_entry,
4095 				   int is_reset)
4096 {
4097 	struct iscsi_cls_session *cls_sess;
4098 	struct iscsi_session *sess;
4099 	struct iscsi_cls_conn *cls_conn;
4100 	struct iscsi_endpoint *ep;
4101 	uint16_t cmds_max = 32;
4102 	uint16_t conn_id = 0;
4103 	uint32_t initial_cmdsn = 0;
4104 	int ret = QLA_SUCCESS;
4105 
4106 	struct ddb_entry *ddb_entry = NULL;
4107 
4108 	/* Create session object, with INVALID_ENTRY,
4109 	 * the targer_id would get set when we issue the login
4110 	 */
4111 	cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport, ha->host,
4112 				       cmds_max, sizeof(struct ddb_entry),
4113 				       sizeof(struct ql4_task_data),
4114 				       initial_cmdsn, INVALID_ENTRY);
4115 	if (!cls_sess) {
4116 		ret = QLA_ERROR;
4117 		goto exit_setup;
4118 	}
4119 
4120 	/*
4121 	 * so calling module_put function to decrement the
4122 	 * reference count.
4123 	 **/
4124 	module_put(qla4xxx_iscsi_transport.owner);
4125 	sess = cls_sess->dd_data;
4126 	ddb_entry = sess->dd_data;
4127 	ddb_entry->sess = cls_sess;
4128 
4129 	cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
4130 	memcpy(&ddb_entry->fw_ddb_entry, fw_ddb_entry,
4131 	       sizeof(struct dev_db_entry));
4132 
4133 	qla4xxx_setup_flash_ddb_entry(ha, ddb_entry);
4134 
4135 	cls_conn = iscsi_conn_setup(cls_sess, sizeof(struct qla_conn), conn_id);
4136 
4137 	if (!cls_conn) {
4138 		ret = QLA_ERROR;
4139 		goto exit_setup;
4140 	}
4141 
4142 	ddb_entry->conn = cls_conn;
4143 
4144 	/* Setup ep, for displaying attributes in sysfs */
4145 	ep = qla4xxx_get_ep_fwdb(ha, fw_ddb_entry);
4146 	if (ep) {
4147 		ep->conn = cls_conn;
4148 		cls_conn->ep = ep;
4149 	} else {
4150 		DEBUG2(ql4_printk(KERN_ERR, ha, "Unable to get ep\n"));
4151 		ret = QLA_ERROR;
4152 		goto exit_setup;
4153 	}
4154 
4155 	/* Update sess/conn params */
4156 	qla4xxx_copy_fwddb_param(ha, fw_ddb_entry, cls_sess, cls_conn);
4157 
4158 	if (is_reset == RESET_ADAPTER) {
4159 		iscsi_block_session(cls_sess);
4160 		/* Use the relogin path to discover new devices
4161 		 *  by short-circuting the logic of setting
4162 		 *  timer to relogin - instead set the flags
4163 		 *  to initiate login right away.
4164 		 */
4165 		set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
4166 		set_bit(DF_RELOGIN, &ddb_entry->flags);
4167 	}
4168 
4169 exit_setup:
4170 	return ret;
4171 }
4172 
qla4xxx_build_nt_list(struct scsi_qla_host * ha,struct list_head * list_nt,int is_reset)4173 static void qla4xxx_build_nt_list(struct scsi_qla_host *ha,
4174 				  struct list_head *list_nt, int is_reset)
4175 {
4176 	struct dev_db_entry *fw_ddb_entry;
4177 	dma_addr_t fw_ddb_dma;
4178 	int max_ddbs;
4179 	int fw_idx_size;
4180 	int ret;
4181 	uint32_t idx = 0, next_idx = 0;
4182 	uint32_t state = 0, conn_err = 0;
4183 	uint16_t conn_id = 0;
4184 	struct qla_ddb_index  *nt_ddb_idx;
4185 
4186 	fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
4187 				      &fw_ddb_dma);
4188 	if (fw_ddb_entry == NULL) {
4189 		DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
4190 		goto exit_nt_list;
4191 	}
4192 	max_ddbs =  is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
4193 				     MAX_DEV_DB_ENTRIES;
4194 	fw_idx_size = sizeof(struct qla_ddb_index);
4195 
4196 	for (idx = 0; idx < max_ddbs; idx = next_idx) {
4197 		ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry, fw_ddb_dma,
4198 					      NULL, &next_idx, &state,
4199 					      &conn_err, NULL, &conn_id);
4200 		if (ret == QLA_ERROR)
4201 			break;
4202 
4203 		if (qla4xxx_verify_boot_idx(ha, idx) != QLA_SUCCESS)
4204 			goto continue_next_nt;
4205 
4206 		/* Check if NT, then add to list it */
4207 		if (strlen((char *) fw_ddb_entry->iscsi_name) == 0)
4208 			goto continue_next_nt;
4209 
4210 		if (!(state == DDB_DS_NO_CONNECTION_ACTIVE ||
4211 		    state == DDB_DS_SESSION_FAILED))
4212 			goto continue_next_nt;
4213 
4214 		DEBUG2(ql4_printk(KERN_INFO, ha,
4215 				  "Adding  DDB to session = 0x%x\n", idx));
4216 		if (is_reset == INIT_ADAPTER) {
4217 			nt_ddb_idx = vmalloc(fw_idx_size);
4218 			if (!nt_ddb_idx)
4219 				break;
4220 
4221 			nt_ddb_idx->fw_ddb_idx = idx;
4222 
4223 			memcpy(&nt_ddb_idx->fw_ddb, fw_ddb_entry,
4224 			       sizeof(struct dev_db_entry));
4225 
4226 			if (qla4xxx_is_flash_ddb_exists(ha, list_nt,
4227 					fw_ddb_entry) == QLA_SUCCESS) {
4228 				vfree(nt_ddb_idx);
4229 				goto continue_next_nt;
4230 			}
4231 			list_add_tail(&nt_ddb_idx->list, list_nt);
4232 		} else if (is_reset == RESET_ADAPTER) {
4233 			if (qla4xxx_is_session_exists(ha, fw_ddb_entry) ==
4234 								QLA_SUCCESS)
4235 				goto continue_next_nt;
4236 		}
4237 
4238 		ret = qla4xxx_sess_conn_setup(ha, fw_ddb_entry, is_reset);
4239 		if (ret == QLA_ERROR)
4240 			goto exit_nt_list;
4241 
4242 continue_next_nt:
4243 		if (next_idx == 0)
4244 			break;
4245 	}
4246 
4247 exit_nt_list:
4248 	if (fw_ddb_entry)
4249 		dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
4250 }
4251 
4252 /**
4253  * qla4xxx_build_ddb_list - Build ddb list and setup sessions
4254  * @ha: pointer to adapter structure
4255  * @is_reset: Is this init path or reset path
4256  *
4257  * Create a list of sendtargets (st) from firmware DDBs, issue send targets
4258  * using connection open, then create the list of normal targets (nt)
4259  * from firmware DDBs. Based on the list of nt setup session and connection
4260  * objects.
4261  **/
qla4xxx_build_ddb_list(struct scsi_qla_host * ha,int is_reset)4262 void qla4xxx_build_ddb_list(struct scsi_qla_host *ha, int is_reset)
4263 {
4264 	uint16_t tmo = 0;
4265 	struct list_head list_st, list_nt;
4266 	struct qla_ddb_index  *st_ddb_idx, *st_ddb_idx_tmp;
4267 	unsigned long wtime;
4268 
4269 	if (!test_bit(AF_LINK_UP, &ha->flags)) {
4270 		set_bit(AF_BUILD_DDB_LIST, &ha->flags);
4271 		ha->is_reset = is_reset;
4272 		return;
4273 	}
4274 
4275 	INIT_LIST_HEAD(&list_st);
4276 	INIT_LIST_HEAD(&list_nt);
4277 
4278 	qla4xxx_build_st_list(ha, &list_st);
4279 
4280 	/* Before issuing conn open mbox, ensure all IPs states are configured
4281 	 * Note, conn open fails if IPs are not configured
4282 	 */
4283 	qla4xxx_wait_for_ip_configuration(ha);
4284 
4285 	/* Go thru the STs and fire the sendtargets by issuing conn open mbx */
4286 	list_for_each_entry_safe(st_ddb_idx, st_ddb_idx_tmp, &list_st, list) {
4287 		qla4xxx_conn_open(ha, st_ddb_idx->fw_ddb_idx);
4288 	}
4289 
4290 	/* Wait to ensure all sendtargets are done for min 12 sec wait */
4291 	tmo = ((ha->def_timeout > LOGIN_TOV) &&
4292 	       (ha->def_timeout < LOGIN_TOV * 10) ?
4293 	       ha->def_timeout : LOGIN_TOV);
4294 
4295 	DEBUG2(ql4_printk(KERN_INFO, ha,
4296 			  "Default time to wait for build ddb %d\n", tmo));
4297 
4298 	wtime = jiffies + (HZ * tmo);
4299 	do {
4300 		if (list_empty(&list_st))
4301 			break;
4302 
4303 		qla4xxx_remove_failed_ddb(ha, &list_st);
4304 		schedule_timeout_uninterruptible(HZ / 10);
4305 	} while (time_after(wtime, jiffies));
4306 
4307 	/* Free up the sendtargets list */
4308 	qla4xxx_free_ddb_list(&list_st);
4309 
4310 	qla4xxx_build_nt_list(ha, &list_nt, is_reset);
4311 
4312 	qla4xxx_free_ddb_list(&list_nt);
4313 
4314 	qla4xxx_free_ddb_index(ha);
4315 }
4316 
4317 /**
4318  * qla4xxx_probe_adapter - callback function to probe HBA
4319  * @pdev: pointer to pci_dev structure
4320  * @pci_device_id: pointer to pci_device entry
4321  *
4322  * This routine will probe for Qlogic 4xxx iSCSI host adapters.
4323  * It returns zero if successful. It also initializes all data necessary for
4324  * the driver.
4325  **/
qla4xxx_probe_adapter(struct pci_dev * pdev,const struct pci_device_id * ent)4326 static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
4327 					   const struct pci_device_id *ent)
4328 {
4329 	int ret = -ENODEV, status;
4330 	struct Scsi_Host *host;
4331 	struct scsi_qla_host *ha;
4332 	uint8_t init_retry_count = 0;
4333 	char buf[34];
4334 	struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
4335 	uint32_t dev_state;
4336 
4337 	if (pci_enable_device(pdev))
4338 		return -1;
4339 
4340 	host = iscsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha), 0);
4341 	if (host == NULL) {
4342 		printk(KERN_WARNING
4343 		       "qla4xxx: Couldn't allocate host from scsi layer!\n");
4344 		goto probe_disable_device;
4345 	}
4346 
4347 	/* Clear our data area */
4348 	ha = to_qla_host(host);
4349 	memset(ha, 0, sizeof(*ha));
4350 
4351 	/* Save the information from PCI BIOS.	*/
4352 	ha->pdev = pdev;
4353 	ha->host = host;
4354 	ha->host_no = host->host_no;
4355 
4356 	pci_enable_pcie_error_reporting(pdev);
4357 
4358 	/* Setup Runtime configurable options */
4359 	if (is_qla8022(ha)) {
4360 		ha->isp_ops = &qla4_8xxx_isp_ops;
4361 		rwlock_init(&ha->hw_lock);
4362 		ha->qdr_sn_window = -1;
4363 		ha->ddr_mn_window = -1;
4364 		ha->curr_window = 255;
4365 		ha->func_num = PCI_FUNC(ha->pdev->devfn);
4366 		nx_legacy_intr = &legacy_intr[ha->func_num];
4367 		ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
4368 		ha->nx_legacy_intr.tgt_status_reg =
4369 			nx_legacy_intr->tgt_status_reg;
4370 		ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
4371 		ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
4372 	} else {
4373 		ha->isp_ops = &qla4xxx_isp_ops;
4374 	}
4375 
4376 	/* Set EEH reset type to fundamental if required by hba */
4377 	if (is_qla8022(ha))
4378 		pdev->needs_freset = 1;
4379 
4380 	/* Configure PCI I/O space. */
4381 	ret = ha->isp_ops->iospace_config(ha);
4382 	if (ret)
4383 		goto probe_failed_ioconfig;
4384 
4385 	ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
4386 		   pdev->device, pdev->irq, ha->reg);
4387 
4388 	qla4xxx_config_dma_addressing(ha);
4389 
4390 	/* Initialize lists and spinlocks. */
4391 	INIT_LIST_HEAD(&ha->free_srb_q);
4392 
4393 	mutex_init(&ha->mbox_sem);
4394 	mutex_init(&ha->chap_sem);
4395 	init_completion(&ha->mbx_intr_comp);
4396 	init_completion(&ha->disable_acb_comp);
4397 
4398 	spin_lock_init(&ha->hardware_lock);
4399 
4400 	/* Allocate dma buffers */
4401 	if (qla4xxx_mem_alloc(ha)) {
4402 		ql4_printk(KERN_WARNING, ha,
4403 		    "[ERROR] Failed to allocate memory for adapter\n");
4404 
4405 		ret = -ENOMEM;
4406 		goto probe_failed;
4407 	}
4408 
4409 	host->cmd_per_lun = 3;
4410 	host->max_channel = 0;
4411 	host->max_lun = MAX_LUNS - 1;
4412 	host->max_id = MAX_TARGETS;
4413 	host->max_cmd_len = IOCB_MAX_CDB_LEN;
4414 	host->can_queue = MAX_SRBS ;
4415 	host->transportt = qla4xxx_scsi_transport;
4416 
4417 	ret = scsi_init_shared_tag_map(host, MAX_SRBS);
4418 	if (ret) {
4419 		ql4_printk(KERN_WARNING, ha,
4420 			   "%s: scsi_init_shared_tag_map failed\n", __func__);
4421 		goto probe_failed;
4422 	}
4423 
4424 	pci_set_drvdata(pdev, ha);
4425 
4426 	ret = scsi_add_host(host, &pdev->dev);
4427 	if (ret)
4428 		goto probe_failed;
4429 
4430 	if (is_qla8022(ha))
4431 		(void) qla4_8xxx_get_flash_info(ha);
4432 
4433 	/*
4434 	 * Initialize the Host adapter request/response queues and
4435 	 * firmware
4436 	 * NOTE: interrupts enabled upon successful completion
4437 	 */
4438 	status = qla4xxx_initialize_adapter(ha, INIT_ADAPTER);
4439 	while ((!test_bit(AF_ONLINE, &ha->flags)) &&
4440 	    init_retry_count++ < MAX_INIT_RETRIES) {
4441 
4442 		if (is_qla8022(ha)) {
4443 			qla4_8xxx_idc_lock(ha);
4444 			dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
4445 			qla4_8xxx_idc_unlock(ha);
4446 			if (dev_state == QLA82XX_DEV_FAILED) {
4447 				ql4_printk(KERN_WARNING, ha, "%s: don't retry "
4448 				    "initialize adapter. H/W is in failed state\n",
4449 				    __func__);
4450 				break;
4451 			}
4452 		}
4453 		DEBUG2(printk("scsi: %s: retrying adapter initialization "
4454 			      "(%d)\n", __func__, init_retry_count));
4455 
4456 		if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
4457 			continue;
4458 
4459 		status = qla4xxx_initialize_adapter(ha, INIT_ADAPTER);
4460 	}
4461 
4462 	if (!test_bit(AF_ONLINE, &ha->flags)) {
4463 		ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
4464 
4465 		if (is_qla8022(ha) && ql4xdontresethba) {
4466 			/* Put the device in failed state. */
4467 			DEBUG2(printk(KERN_ERR "HW STATE: FAILED\n"));
4468 			qla4_8xxx_idc_lock(ha);
4469 			qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
4470 			    QLA82XX_DEV_FAILED);
4471 			qla4_8xxx_idc_unlock(ha);
4472 		}
4473 		ret = -ENODEV;
4474 		goto remove_host;
4475 	}
4476 
4477 	/* Startup the kernel thread for this host adapter. */
4478 	DEBUG2(printk("scsi: %s: Starting kernel thread for "
4479 		      "qla4xxx_dpc\n", __func__));
4480 	sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
4481 	ha->dpc_thread = create_singlethread_workqueue(buf);
4482 	if (!ha->dpc_thread) {
4483 		ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
4484 		ret = -ENODEV;
4485 		goto remove_host;
4486 	}
4487 	INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
4488 
4489 	sprintf(buf, "qla4xxx_%lu_task", ha->host_no);
4490 	ha->task_wq = alloc_workqueue(buf, WQ_MEM_RECLAIM, 1);
4491 	if (!ha->task_wq) {
4492 		ql4_printk(KERN_WARNING, ha, "Unable to start task thread!\n");
4493 		ret = -ENODEV;
4494 		goto remove_host;
4495 	}
4496 
4497 	/* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
4498 	 * (which is called indirectly by qla4xxx_initialize_adapter),
4499 	 * so that irqs will be registered after crbinit but before
4500 	 * mbx_intr_enable.
4501 	 */
4502 	if (!is_qla8022(ha)) {
4503 		ret = qla4xxx_request_irqs(ha);
4504 		if (ret) {
4505 			ql4_printk(KERN_WARNING, ha, "Failed to reserve "
4506 			    "interrupt %d already in use.\n", pdev->irq);
4507 			goto remove_host;
4508 		}
4509 	}
4510 
4511 	pci_save_state(ha->pdev);
4512 	ha->isp_ops->enable_intrs(ha);
4513 
4514 	/* Start timer thread. */
4515 	qla4xxx_start_timer(ha, qla4xxx_timer, 1);
4516 
4517 	set_bit(AF_INIT_DONE, &ha->flags);
4518 
4519 	printk(KERN_INFO
4520 	       " QLogic iSCSI HBA Driver version: %s\n"
4521 	       "  QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
4522 	       qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
4523 	       ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
4524 	       ha->patch_number, ha->build_number);
4525 
4526 	if (qla4xxx_setup_boot_info(ha))
4527 		ql4_printk(KERN_ERR, ha, "%s:ISCSI boot info setup failed\n",
4528 			   __func__);
4529 
4530 		/* Perform the build ddb list and login to each */
4531 	qla4xxx_build_ddb_list(ha, INIT_ADAPTER);
4532 	iscsi_host_for_each_session(ha->host, qla4xxx_login_flash_ddb);
4533 
4534 	qla4xxx_create_chap_list(ha);
4535 
4536 	qla4xxx_create_ifaces(ha);
4537 	return 0;
4538 
4539 remove_host:
4540 	scsi_remove_host(ha->host);
4541 
4542 probe_failed:
4543 	qla4xxx_free_adapter(ha);
4544 
4545 probe_failed_ioconfig:
4546 	pci_disable_pcie_error_reporting(pdev);
4547 	scsi_host_put(ha->host);
4548 
4549 probe_disable_device:
4550 	pci_disable_device(pdev);
4551 
4552 	return ret;
4553 }
4554 
4555 /**
4556  * qla4xxx_prevent_other_port_reinit - prevent other port from re-initialize
4557  * @ha: pointer to adapter structure
4558  *
4559  * Mark the other ISP-4xxx port to indicate that the driver is being removed,
4560  * so that the other port will not re-initialize while in the process of
4561  * removing the ha due to driver unload or hba hotplug.
4562  **/
qla4xxx_prevent_other_port_reinit(struct scsi_qla_host * ha)4563 static void qla4xxx_prevent_other_port_reinit(struct scsi_qla_host *ha)
4564 {
4565 	struct scsi_qla_host *other_ha = NULL;
4566 	struct pci_dev *other_pdev = NULL;
4567 	int fn = ISP4XXX_PCI_FN_2;
4568 
4569 	/*iscsi function numbers for ISP4xxx is 1 and 3*/
4570 	if (PCI_FUNC(ha->pdev->devfn) & BIT_1)
4571 		fn = ISP4XXX_PCI_FN_1;
4572 
4573 	other_pdev =
4574 		pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
4575 		ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
4576 		fn));
4577 
4578 	/* Get other_ha if other_pdev is valid and state is enable*/
4579 	if (other_pdev) {
4580 		if (atomic_read(&other_pdev->enable_cnt)) {
4581 			other_ha = pci_get_drvdata(other_pdev);
4582 			if (other_ha) {
4583 				set_bit(AF_HA_REMOVAL, &other_ha->flags);
4584 				DEBUG2(ql4_printk(KERN_INFO, ha, "%s: "
4585 				    "Prevent %s reinit\n", __func__,
4586 				    dev_name(&other_ha->pdev->dev)));
4587 			}
4588 		}
4589 		pci_dev_put(other_pdev);
4590 	}
4591 }
4592 
qla4xxx_destroy_fw_ddb_session(struct scsi_qla_host * ha)4593 static void qla4xxx_destroy_fw_ddb_session(struct scsi_qla_host *ha)
4594 {
4595 	struct ddb_entry *ddb_entry;
4596 	int options;
4597 	int idx;
4598 
4599 	for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
4600 
4601 		ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
4602 		if ((ddb_entry != NULL) &&
4603 		    (ddb_entry->ddb_type == FLASH_DDB)) {
4604 
4605 			options = LOGOUT_OPTION_CLOSE_SESSION;
4606 			if (qla4xxx_session_logout_ddb(ha, ddb_entry, options)
4607 			    == QLA_ERROR)
4608 				ql4_printk(KERN_ERR, ha, "%s: Logout failed\n",
4609 					   __func__);
4610 
4611 			qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
4612 			/*
4613 			 * we have decremented the reference count of the driver
4614 			 * when we setup the session to have the driver unload
4615 			 * to be seamless without actually destroying the
4616 			 * session
4617 			 **/
4618 			try_module_get(qla4xxx_iscsi_transport.owner);
4619 			iscsi_destroy_endpoint(ddb_entry->conn->ep);
4620 			qla4xxx_free_ddb(ha, ddb_entry);
4621 			iscsi_session_teardown(ddb_entry->sess);
4622 		}
4623 	}
4624 }
4625 /**
4626  * qla4xxx_remove_adapter - calback function to remove adapter.
4627  * @pci_dev: PCI device pointer
4628  **/
qla4xxx_remove_adapter(struct pci_dev * pdev)4629 static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
4630 {
4631 	struct scsi_qla_host *ha;
4632 
4633 	ha = pci_get_drvdata(pdev);
4634 
4635 	if (!is_qla8022(ha))
4636 		qla4xxx_prevent_other_port_reinit(ha);
4637 
4638 	/* destroy iface from sysfs */
4639 	qla4xxx_destroy_ifaces(ha);
4640 
4641 	if ((!ql4xdisablesysfsboot) && ha->boot_kset)
4642 		iscsi_boot_destroy_kset(ha->boot_kset);
4643 
4644 	qla4xxx_destroy_fw_ddb_session(ha);
4645 
4646 	scsi_remove_host(ha->host);
4647 
4648 	qla4xxx_free_adapter(ha);
4649 
4650 	scsi_host_put(ha->host);
4651 
4652 	pci_disable_pcie_error_reporting(pdev);
4653 	pci_disable_device(pdev);
4654 	pci_set_drvdata(pdev, NULL);
4655 }
4656 
4657 /**
4658  * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
4659  * @ha: HA context
4660  *
4661  * At exit, the @ha's flags.enable_64bit_addressing set to indicated
4662  * supported addressing method.
4663  */
qla4xxx_config_dma_addressing(struct scsi_qla_host * ha)4664 static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
4665 {
4666 	int retval;
4667 
4668 	/* Update our PCI device dma_mask for full 64 bit mask */
4669 	if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
4670 		if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
4671 			dev_dbg(&ha->pdev->dev,
4672 				  "Failed to set 64 bit PCI consistent mask; "
4673 				   "using 32 bit.\n");
4674 			retval = pci_set_consistent_dma_mask(ha->pdev,
4675 							     DMA_BIT_MASK(32));
4676 		}
4677 	} else
4678 		retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
4679 }
4680 
qla4xxx_slave_alloc(struct scsi_device * sdev)4681 static int qla4xxx_slave_alloc(struct scsi_device *sdev)
4682 {
4683 	struct iscsi_cls_session *cls_sess;
4684 	struct iscsi_session *sess;
4685 	struct ddb_entry *ddb;
4686 	int queue_depth = QL4_DEF_QDEPTH;
4687 
4688 	cls_sess = starget_to_session(sdev->sdev_target);
4689 	sess = cls_sess->dd_data;
4690 	ddb = sess->dd_data;
4691 
4692 	sdev->hostdata = ddb;
4693 	sdev->tagged_supported = 1;
4694 
4695 	if (ql4xmaxqdepth != 0 && ql4xmaxqdepth <= 0xffffU)
4696 		queue_depth = ql4xmaxqdepth;
4697 
4698 	scsi_activate_tcq(sdev, queue_depth);
4699 	return 0;
4700 }
4701 
qla4xxx_slave_configure(struct scsi_device * sdev)4702 static int qla4xxx_slave_configure(struct scsi_device *sdev)
4703 {
4704 	sdev->tagged_supported = 1;
4705 	return 0;
4706 }
4707 
qla4xxx_slave_destroy(struct scsi_device * sdev)4708 static void qla4xxx_slave_destroy(struct scsi_device *sdev)
4709 {
4710 	scsi_deactivate_tcq(sdev, 1);
4711 }
4712 
4713 /**
4714  * qla4xxx_del_from_active_array - returns an active srb
4715  * @ha: Pointer to host adapter structure.
4716  * @index: index into the active_array
4717  *
4718  * This routine removes and returns the srb at the specified index
4719  **/
qla4xxx_del_from_active_array(struct scsi_qla_host * ha,uint32_t index)4720 struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
4721     uint32_t index)
4722 {
4723 	struct srb *srb = NULL;
4724 	struct scsi_cmnd *cmd = NULL;
4725 
4726 	cmd = scsi_host_find_tag(ha->host, index);
4727 	if (!cmd)
4728 		return srb;
4729 
4730 	srb = (struct srb *)CMD_SP(cmd);
4731 	if (!srb)
4732 		return srb;
4733 
4734 	/* update counters */
4735 	if (srb->flags & SRB_DMA_VALID) {
4736 		ha->req_q_count += srb->iocb_cnt;
4737 		ha->iocb_cnt -= srb->iocb_cnt;
4738 		if (srb->cmd)
4739 			srb->cmd->host_scribble =
4740 				(unsigned char *)(unsigned long) MAX_SRBS;
4741 	}
4742 	return srb;
4743 }
4744 
4745 /**
4746  * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
4747  * @ha: Pointer to host adapter structure.
4748  * @cmd: Scsi Command to wait on.
4749  *
4750  * This routine waits for the command to be returned by the Firmware
4751  * for some max time.
4752  **/
qla4xxx_eh_wait_on_command(struct scsi_qla_host * ha,struct scsi_cmnd * cmd)4753 static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
4754 				      struct scsi_cmnd *cmd)
4755 {
4756 	int done = 0;
4757 	struct srb *rp;
4758 	uint32_t max_wait_time = EH_WAIT_CMD_TOV;
4759 	int ret = SUCCESS;
4760 
4761 	/* Dont wait on command if PCI error is being handled
4762 	 * by PCI AER driver
4763 	 */
4764 	if (unlikely(pci_channel_offline(ha->pdev)) ||
4765 	    (test_bit(AF_EEH_BUSY, &ha->flags))) {
4766 		ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
4767 		    ha->host_no, __func__);
4768 		return ret;
4769 	}
4770 
4771 	do {
4772 		/* Checking to see if its returned to OS */
4773 		rp = (struct srb *) CMD_SP(cmd);
4774 		if (rp == NULL) {
4775 			done++;
4776 			break;
4777 		}
4778 
4779 		msleep(2000);
4780 	} while (max_wait_time--);
4781 
4782 	return done;
4783 }
4784 
4785 /**
4786  * qla4xxx_wait_for_hba_online - waits for HBA to come online
4787  * @ha: Pointer to host adapter structure
4788  **/
qla4xxx_wait_for_hba_online(struct scsi_qla_host * ha)4789 static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
4790 {
4791 	unsigned long wait_online;
4792 
4793 	wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
4794 	while (time_before(jiffies, wait_online)) {
4795 
4796 		if (adapter_up(ha))
4797 			return QLA_SUCCESS;
4798 
4799 		msleep(2000);
4800 	}
4801 
4802 	return QLA_ERROR;
4803 }
4804 
4805 /**
4806  * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
4807  * @ha: pointer to HBA
4808  * @t: target id
4809  * @l: lun id
4810  *
4811  * This function waits for all outstanding commands to a lun to complete. It
4812  * returns 0 if all pending commands are returned and 1 otherwise.
4813  **/
qla4xxx_eh_wait_for_commands(struct scsi_qla_host * ha,struct scsi_target * stgt,struct scsi_device * sdev)4814 static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
4815 					struct scsi_target *stgt,
4816 					struct scsi_device *sdev)
4817 {
4818 	int cnt;
4819 	int status = 0;
4820 	struct scsi_cmnd *cmd;
4821 
4822 	/*
4823 	 * Waiting for all commands for the designated target or dev
4824 	 * in the active array
4825 	 */
4826 	for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
4827 		cmd = scsi_host_find_tag(ha->host, cnt);
4828 		if (cmd && stgt == scsi_target(cmd->device) &&
4829 		    (!sdev || sdev == cmd->device)) {
4830 			if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
4831 				status++;
4832 				break;
4833 			}
4834 		}
4835 	}
4836 	return status;
4837 }
4838 
4839 /**
4840  * qla4xxx_eh_abort - callback for abort task.
4841  * @cmd: Pointer to Linux's SCSI command structure
4842  *
4843  * This routine is called by the Linux OS to abort the specified
4844  * command.
4845  **/
qla4xxx_eh_abort(struct scsi_cmnd * cmd)4846 static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
4847 {
4848 	struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
4849 	unsigned int id = cmd->device->id;
4850 	unsigned int lun = cmd->device->lun;
4851 	unsigned long flags;
4852 	struct srb *srb = NULL;
4853 	int ret = SUCCESS;
4854 	int wait = 0;
4855 
4856 	ql4_printk(KERN_INFO, ha,
4857 	    "scsi%ld:%d:%d: Abort command issued cmd=%p\n",
4858 	    ha->host_no, id, lun, cmd);
4859 
4860 	spin_lock_irqsave(&ha->hardware_lock, flags);
4861 	srb = (struct srb *) CMD_SP(cmd);
4862 	if (!srb) {
4863 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
4864 		return SUCCESS;
4865 	}
4866 	kref_get(&srb->srb_ref);
4867 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
4868 
4869 	if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
4870 		DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
4871 		    ha->host_no, id, lun));
4872 		ret = FAILED;
4873 	} else {
4874 		DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
4875 		    ha->host_no, id, lun));
4876 		wait = 1;
4877 	}
4878 
4879 	kref_put(&srb->srb_ref, qla4xxx_srb_compl);
4880 
4881 	/* Wait for command to complete */
4882 	if (wait) {
4883 		if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
4884 			DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
4885 			    ha->host_no, id, lun));
4886 			ret = FAILED;
4887 		}
4888 	}
4889 
4890 	ql4_printk(KERN_INFO, ha,
4891 	    "scsi%ld:%d:%d: Abort command - %s\n",
4892 	    ha->host_no, id, lun, (ret == SUCCESS) ? "succeeded" : "failed");
4893 
4894 	return ret;
4895 }
4896 
4897 /**
4898  * qla4xxx_eh_device_reset - callback for target reset.
4899  * @cmd: Pointer to Linux's SCSI command structure
4900  *
4901  * This routine is called by the Linux OS to reset all luns on the
4902  * specified target.
4903  **/
qla4xxx_eh_device_reset(struct scsi_cmnd * cmd)4904 static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
4905 {
4906 	struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
4907 	struct ddb_entry *ddb_entry = cmd->device->hostdata;
4908 	int ret = FAILED, stat;
4909 
4910 	if (!ddb_entry)
4911 		return ret;
4912 
4913 	ret = iscsi_block_scsi_eh(cmd);
4914 	if (ret)
4915 		return ret;
4916 	ret = FAILED;
4917 
4918 	ql4_printk(KERN_INFO, ha,
4919 		   "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
4920 		   cmd->device->channel, cmd->device->id, cmd->device->lun);
4921 
4922 	DEBUG2(printk(KERN_INFO
4923 		      "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
4924 		      "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
4925 		      cmd, jiffies, cmd->request->timeout / HZ,
4926 		      ha->dpc_flags, cmd->result, cmd->allowed));
4927 
4928 	/* FIXME: wait for hba to go online */
4929 	stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
4930 	if (stat != QLA_SUCCESS) {
4931 		ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
4932 		goto eh_dev_reset_done;
4933 	}
4934 
4935 	if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
4936 					 cmd->device)) {
4937 		ql4_printk(KERN_INFO, ha,
4938 			   "DEVICE RESET FAILED - waiting for "
4939 			   "commands.\n");
4940 		goto eh_dev_reset_done;
4941 	}
4942 
4943 	/* Send marker. */
4944 	if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
4945 		MM_LUN_RESET) != QLA_SUCCESS)
4946 		goto eh_dev_reset_done;
4947 
4948 	ql4_printk(KERN_INFO, ha,
4949 		   "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
4950 		   ha->host_no, cmd->device->channel, cmd->device->id,
4951 		   cmd->device->lun);
4952 
4953 	ret = SUCCESS;
4954 
4955 eh_dev_reset_done:
4956 
4957 	return ret;
4958 }
4959 
4960 /**
4961  * qla4xxx_eh_target_reset - callback for target reset.
4962  * @cmd: Pointer to Linux's SCSI command structure
4963  *
4964  * This routine is called by the Linux OS to reset the target.
4965  **/
qla4xxx_eh_target_reset(struct scsi_cmnd * cmd)4966 static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
4967 {
4968 	struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
4969 	struct ddb_entry *ddb_entry = cmd->device->hostdata;
4970 	int stat, ret;
4971 
4972 	if (!ddb_entry)
4973 		return FAILED;
4974 
4975 	ret = iscsi_block_scsi_eh(cmd);
4976 	if (ret)
4977 		return ret;
4978 
4979 	starget_printk(KERN_INFO, scsi_target(cmd->device),
4980 		       "WARM TARGET RESET ISSUED.\n");
4981 
4982 	DEBUG2(printk(KERN_INFO
4983 		      "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
4984 		      "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
4985 		      ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
4986 		      ha->dpc_flags, cmd->result, cmd->allowed));
4987 
4988 	stat = qla4xxx_reset_target(ha, ddb_entry);
4989 	if (stat != QLA_SUCCESS) {
4990 		starget_printk(KERN_INFO, scsi_target(cmd->device),
4991 			       "WARM TARGET RESET FAILED.\n");
4992 		return FAILED;
4993 	}
4994 
4995 	if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
4996 					 NULL)) {
4997 		starget_printk(KERN_INFO, scsi_target(cmd->device),
4998 			       "WARM TARGET DEVICE RESET FAILED - "
4999 			       "waiting for commands.\n");
5000 		return FAILED;
5001 	}
5002 
5003 	/* Send marker. */
5004 	if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
5005 		MM_TGT_WARM_RESET) != QLA_SUCCESS) {
5006 		starget_printk(KERN_INFO, scsi_target(cmd->device),
5007 			       "WARM TARGET DEVICE RESET FAILED - "
5008 			       "marker iocb failed.\n");
5009 		return FAILED;
5010 	}
5011 
5012 	starget_printk(KERN_INFO, scsi_target(cmd->device),
5013 		       "WARM TARGET RESET SUCCEEDED.\n");
5014 	return SUCCESS;
5015 }
5016 
5017 /**
5018  * qla4xxx_is_eh_active - check if error handler is running
5019  * @shost: Pointer to SCSI Host struct
5020  *
5021  * This routine finds that if reset host is called in EH
5022  * scenario or from some application like sg_reset
5023  **/
qla4xxx_is_eh_active(struct Scsi_Host * shost)5024 static int qla4xxx_is_eh_active(struct Scsi_Host *shost)
5025 {
5026 	if (shost->shost_state == SHOST_RECOVERY)
5027 		return 1;
5028 	return 0;
5029 }
5030 
5031 /**
5032  * qla4xxx_eh_host_reset - kernel callback
5033  * @cmd: Pointer to Linux's SCSI command structure
5034  *
5035  * This routine is invoked by the Linux kernel to perform fatal error
5036  * recovery on the specified adapter.
5037  **/
qla4xxx_eh_host_reset(struct scsi_cmnd * cmd)5038 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
5039 {
5040 	int return_status = FAILED;
5041 	struct scsi_qla_host *ha;
5042 
5043 	ha = to_qla_host(cmd->device->host);
5044 
5045 	if (ql4xdontresethba) {
5046 		DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
5047 		     ha->host_no, __func__));
5048 
5049 		/* Clear outstanding srb in queues */
5050 		if (qla4xxx_is_eh_active(cmd->device->host))
5051 			qla4xxx_abort_active_cmds(ha, DID_ABORT << 16);
5052 
5053 		return FAILED;
5054 	}
5055 
5056 	ql4_printk(KERN_INFO, ha,
5057 		   "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
5058 		   cmd->device->channel, cmd->device->id, cmd->device->lun);
5059 
5060 	if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
5061 		DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host.  Adapter "
5062 			      "DEAD.\n", ha->host_no, cmd->device->channel,
5063 			      __func__));
5064 
5065 		return FAILED;
5066 	}
5067 
5068 	if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
5069 		if (is_qla8022(ha))
5070 			set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
5071 		else
5072 			set_bit(DPC_RESET_HA, &ha->dpc_flags);
5073 	}
5074 
5075 	if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
5076 		return_status = SUCCESS;
5077 
5078 	ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
5079 		   return_status == FAILED ? "FAILED" : "SUCCEEDED");
5080 
5081 	return return_status;
5082 }
5083 
qla4xxx_context_reset(struct scsi_qla_host * ha)5084 static int qla4xxx_context_reset(struct scsi_qla_host *ha)
5085 {
5086 	uint32_t mbox_cmd[MBOX_REG_COUNT];
5087 	uint32_t mbox_sts[MBOX_REG_COUNT];
5088 	struct addr_ctrl_blk_def *acb = NULL;
5089 	uint32_t acb_len = sizeof(struct addr_ctrl_blk_def);
5090 	int rval = QLA_SUCCESS;
5091 	dma_addr_t acb_dma;
5092 
5093 	acb = dma_alloc_coherent(&ha->pdev->dev,
5094 				 sizeof(struct addr_ctrl_blk_def),
5095 				 &acb_dma, GFP_KERNEL);
5096 	if (!acb) {
5097 		ql4_printk(KERN_ERR, ha, "%s: Unable to alloc acb\n",
5098 			   __func__);
5099 		rval = -ENOMEM;
5100 		goto exit_port_reset;
5101 	}
5102 
5103 	memset(acb, 0, acb_len);
5104 
5105 	rval = qla4xxx_get_acb(ha, acb_dma, PRIMARI_ACB, acb_len);
5106 	if (rval != QLA_SUCCESS) {
5107 		rval = -EIO;
5108 		goto exit_free_acb;
5109 	}
5110 
5111 	rval = qla4xxx_disable_acb(ha);
5112 	if (rval != QLA_SUCCESS) {
5113 		rval = -EIO;
5114 		goto exit_free_acb;
5115 	}
5116 
5117 	wait_for_completion_timeout(&ha->disable_acb_comp,
5118 				    DISABLE_ACB_TOV * HZ);
5119 
5120 	rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], acb_dma);
5121 	if (rval != QLA_SUCCESS) {
5122 		rval = -EIO;
5123 		goto exit_free_acb;
5124 	}
5125 
5126 exit_free_acb:
5127 	dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk_def),
5128 			  acb, acb_dma);
5129 exit_port_reset:
5130 	DEBUG2(ql4_printk(KERN_INFO, ha, "%s %s\n", __func__,
5131 			  rval == QLA_SUCCESS ? "SUCCEEDED" : "FAILED"));
5132 	return rval;
5133 }
5134 
qla4xxx_host_reset(struct Scsi_Host * shost,int reset_type)5135 static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type)
5136 {
5137 	struct scsi_qla_host *ha = to_qla_host(shost);
5138 	int rval = QLA_SUCCESS;
5139 
5140 	if (ql4xdontresethba) {
5141 		DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Don't Reset HBA\n",
5142 				  __func__));
5143 		rval = -EPERM;
5144 		goto exit_host_reset;
5145 	}
5146 
5147 	rval = qla4xxx_wait_for_hba_online(ha);
5148 	if (rval != QLA_SUCCESS) {
5149 		DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Unable to reset host "
5150 				  "adapter\n", __func__));
5151 		rval = -EIO;
5152 		goto exit_host_reset;
5153 	}
5154 
5155 	if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
5156 		goto recover_adapter;
5157 
5158 	switch (reset_type) {
5159 	case SCSI_ADAPTER_RESET:
5160 		set_bit(DPC_RESET_HA, &ha->dpc_flags);
5161 		break;
5162 	case SCSI_FIRMWARE_RESET:
5163 		if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
5164 			if (is_qla8022(ha))
5165 				/* set firmware context reset */
5166 				set_bit(DPC_RESET_HA_FW_CONTEXT,
5167 					&ha->dpc_flags);
5168 			else {
5169 				rval = qla4xxx_context_reset(ha);
5170 				goto exit_host_reset;
5171 			}
5172 		}
5173 		break;
5174 	}
5175 
5176 recover_adapter:
5177 	rval = qla4xxx_recover_adapter(ha);
5178 	if (rval != QLA_SUCCESS) {
5179 		DEBUG2(ql4_printk(KERN_INFO, ha, "%s: recover adapter fail\n",
5180 				  __func__));
5181 		rval = -EIO;
5182 	}
5183 
5184 exit_host_reset:
5185 	return rval;
5186 }
5187 
5188 /* PCI AER driver recovers from all correctable errors w/o
5189  * driver intervention. For uncorrectable errors PCI AER
5190  * driver calls the following device driver's callbacks
5191  *
5192  * - Fatal Errors - link_reset
5193  * - Non-Fatal Errors - driver's pci_error_detected() which
5194  * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
5195  *
5196  * PCI AER driver calls
5197  * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
5198  *               returns RECOVERED or NEED_RESET if fw_hung
5199  * NEED_RESET - driver's slot_reset()
5200  * DISCONNECT - device is dead & cannot recover
5201  * RECOVERED - driver's pci_resume()
5202  */
5203 static pci_ers_result_t
qla4xxx_pci_error_detected(struct pci_dev * pdev,pci_channel_state_t state)5204 qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
5205 {
5206 	struct scsi_qla_host *ha = pci_get_drvdata(pdev);
5207 
5208 	ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
5209 	    ha->host_no, __func__, state);
5210 
5211 	if (!is_aer_supported(ha))
5212 		return PCI_ERS_RESULT_NONE;
5213 
5214 	switch (state) {
5215 	case pci_channel_io_normal:
5216 		clear_bit(AF_EEH_BUSY, &ha->flags);
5217 		return PCI_ERS_RESULT_CAN_RECOVER;
5218 	case pci_channel_io_frozen:
5219 		set_bit(AF_EEH_BUSY, &ha->flags);
5220 		qla4xxx_mailbox_premature_completion(ha);
5221 		qla4xxx_free_irqs(ha);
5222 		pci_disable_device(pdev);
5223 		/* Return back all IOs */
5224 		qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
5225 		return PCI_ERS_RESULT_NEED_RESET;
5226 	case pci_channel_io_perm_failure:
5227 		set_bit(AF_EEH_BUSY, &ha->flags);
5228 		set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
5229 		qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
5230 		return PCI_ERS_RESULT_DISCONNECT;
5231 	}
5232 	return PCI_ERS_RESULT_NEED_RESET;
5233 }
5234 
5235 /**
5236  * qla4xxx_pci_mmio_enabled() gets called if
5237  * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
5238  * and read/write to the device still works.
5239  **/
5240 static pci_ers_result_t
qla4xxx_pci_mmio_enabled(struct pci_dev * pdev)5241 qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
5242 {
5243 	struct scsi_qla_host *ha = pci_get_drvdata(pdev);
5244 
5245 	if (!is_aer_supported(ha))
5246 		return PCI_ERS_RESULT_NONE;
5247 
5248 	return PCI_ERS_RESULT_RECOVERED;
5249 }
5250 
qla4_8xxx_error_recovery(struct scsi_qla_host * ha)5251 static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
5252 {
5253 	uint32_t rval = QLA_ERROR;
5254 	uint32_t ret = 0;
5255 	int fn;
5256 	struct pci_dev *other_pdev = NULL;
5257 
5258 	ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
5259 
5260 	set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
5261 
5262 	if (test_bit(AF_ONLINE, &ha->flags)) {
5263 		clear_bit(AF_ONLINE, &ha->flags);
5264 		clear_bit(AF_LINK_UP, &ha->flags);
5265 		iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
5266 		qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
5267 	}
5268 
5269 	fn = PCI_FUNC(ha->pdev->devfn);
5270 	while (fn > 0) {
5271 		fn--;
5272 		ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
5273 		    "func %x\n", ha->host_no, __func__, fn);
5274 		/* Get the pci device given the domain, bus,
5275 		 * slot/function number */
5276 		other_pdev =
5277 		    pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
5278 		    ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
5279 		    fn));
5280 
5281 		if (!other_pdev)
5282 			continue;
5283 
5284 		if (atomic_read(&other_pdev->enable_cnt)) {
5285 			ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
5286 			    "func in enabled state%x\n", ha->host_no,
5287 			    __func__, fn);
5288 			pci_dev_put(other_pdev);
5289 			break;
5290 		}
5291 		pci_dev_put(other_pdev);
5292 	}
5293 
5294 	/* The first function on the card, the reset owner will
5295 	 * start & initialize the firmware. The other functions
5296 	 * on the card will reset the firmware context
5297 	 */
5298 	if (!fn) {
5299 		ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
5300 		    "0x%x is the owner\n", ha->host_no, __func__,
5301 		    ha->pdev->devfn);
5302 
5303 		qla4_8xxx_idc_lock(ha);
5304 		qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
5305 		    QLA82XX_DEV_COLD);
5306 
5307 		qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
5308 		    QLA82XX_IDC_VERSION);
5309 
5310 		qla4_8xxx_idc_unlock(ha);
5311 		clear_bit(AF_FW_RECOVERY, &ha->flags);
5312 		rval = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
5313 		qla4_8xxx_idc_lock(ha);
5314 
5315 		if (rval != QLA_SUCCESS) {
5316 			ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
5317 			    "FAILED\n", ha->host_no, __func__);
5318 			qla4_8xxx_clear_drv_active(ha);
5319 			qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
5320 			    QLA82XX_DEV_FAILED);
5321 		} else {
5322 			ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
5323 			    "READY\n", ha->host_no, __func__);
5324 			qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
5325 			    QLA82XX_DEV_READY);
5326 			/* Clear driver state register */
5327 			qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
5328 			qla4_8xxx_set_drv_active(ha);
5329 			ret = qla4xxx_request_irqs(ha);
5330 			if (ret) {
5331 				ql4_printk(KERN_WARNING, ha, "Failed to "
5332 				    "reserve interrupt %d already in use.\n",
5333 				    ha->pdev->irq);
5334 				rval = QLA_ERROR;
5335 			} else {
5336 				ha->isp_ops->enable_intrs(ha);
5337 				rval = QLA_SUCCESS;
5338 			}
5339 		}
5340 		qla4_8xxx_idc_unlock(ha);
5341 	} else {
5342 		ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
5343 		    "the reset owner\n", ha->host_no, __func__,
5344 		    ha->pdev->devfn);
5345 		if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
5346 		    QLA82XX_DEV_READY)) {
5347 			clear_bit(AF_FW_RECOVERY, &ha->flags);
5348 			rval = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
5349 			if (rval == QLA_SUCCESS) {
5350 				ret = qla4xxx_request_irqs(ha);
5351 				if (ret) {
5352 					ql4_printk(KERN_WARNING, ha, "Failed to"
5353 					    " reserve interrupt %d already in"
5354 					    " use.\n", ha->pdev->irq);
5355 					rval = QLA_ERROR;
5356 				} else {
5357 					ha->isp_ops->enable_intrs(ha);
5358 					rval = QLA_SUCCESS;
5359 				}
5360 			}
5361 			qla4_8xxx_idc_lock(ha);
5362 			qla4_8xxx_set_drv_active(ha);
5363 			qla4_8xxx_idc_unlock(ha);
5364 		}
5365 	}
5366 	clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
5367 	return rval;
5368 }
5369 
5370 static pci_ers_result_t
qla4xxx_pci_slot_reset(struct pci_dev * pdev)5371 qla4xxx_pci_slot_reset(struct pci_dev *pdev)
5372 {
5373 	pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
5374 	struct scsi_qla_host *ha = pci_get_drvdata(pdev);
5375 	int rc;
5376 
5377 	ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
5378 	    ha->host_no, __func__);
5379 
5380 	if (!is_aer_supported(ha))
5381 		return PCI_ERS_RESULT_NONE;
5382 
5383 	/* Restore the saved state of PCIe device -
5384 	 * BAR registers, PCI Config space, PCIX, MSI,
5385 	 * IOV states
5386 	 */
5387 	pci_restore_state(pdev);
5388 
5389 	/* pci_restore_state() clears the saved_state flag of the device
5390 	 * save restored state which resets saved_state flag
5391 	 */
5392 	pci_save_state(pdev);
5393 
5394 	/* Initialize device or resume if in suspended state */
5395 	rc = pci_enable_device(pdev);
5396 	if (rc) {
5397 		ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Can't re-enable "
5398 		    "device after reset\n", ha->host_no, __func__);
5399 		goto exit_slot_reset;
5400 	}
5401 
5402 	ha->isp_ops->disable_intrs(ha);
5403 
5404 	if (is_qla8022(ha)) {
5405 		if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
5406 			ret = PCI_ERS_RESULT_RECOVERED;
5407 			goto exit_slot_reset;
5408 		} else
5409 			goto exit_slot_reset;
5410 	}
5411 
5412 exit_slot_reset:
5413 	ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
5414 	    "device after reset\n", ha->host_no, __func__, ret);
5415 	return ret;
5416 }
5417 
5418 static void
qla4xxx_pci_resume(struct pci_dev * pdev)5419 qla4xxx_pci_resume(struct pci_dev *pdev)
5420 {
5421 	struct scsi_qla_host *ha = pci_get_drvdata(pdev);
5422 	int ret;
5423 
5424 	ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
5425 	    ha->host_no, __func__);
5426 
5427 	ret = qla4xxx_wait_for_hba_online(ha);
5428 	if (ret != QLA_SUCCESS) {
5429 		ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
5430 		    "resume I/O from slot/link_reset\n", ha->host_no,
5431 		     __func__);
5432 	}
5433 
5434 	pci_cleanup_aer_uncorrect_error_status(pdev);
5435 	clear_bit(AF_EEH_BUSY, &ha->flags);
5436 }
5437 
5438 static struct pci_error_handlers qla4xxx_err_handler = {
5439 	.error_detected = qla4xxx_pci_error_detected,
5440 	.mmio_enabled = qla4xxx_pci_mmio_enabled,
5441 	.slot_reset = qla4xxx_pci_slot_reset,
5442 	.resume = qla4xxx_pci_resume,
5443 };
5444 
5445 static struct pci_device_id qla4xxx_pci_tbl[] = {
5446 	{
5447 		.vendor		= PCI_VENDOR_ID_QLOGIC,
5448 		.device		= PCI_DEVICE_ID_QLOGIC_ISP4010,
5449 		.subvendor	= PCI_ANY_ID,
5450 		.subdevice	= PCI_ANY_ID,
5451 	},
5452 	{
5453 		.vendor		= PCI_VENDOR_ID_QLOGIC,
5454 		.device		= PCI_DEVICE_ID_QLOGIC_ISP4022,
5455 		.subvendor	= PCI_ANY_ID,
5456 		.subdevice	= PCI_ANY_ID,
5457 	},
5458 	{
5459 		.vendor		= PCI_VENDOR_ID_QLOGIC,
5460 		.device		= PCI_DEVICE_ID_QLOGIC_ISP4032,
5461 		.subvendor	= PCI_ANY_ID,
5462 		.subdevice	= PCI_ANY_ID,
5463 	},
5464 	{
5465 		.vendor         = PCI_VENDOR_ID_QLOGIC,
5466 		.device         = PCI_DEVICE_ID_QLOGIC_ISP8022,
5467 		.subvendor      = PCI_ANY_ID,
5468 		.subdevice      = PCI_ANY_ID,
5469 	},
5470 	{0, 0},
5471 };
5472 MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
5473 
5474 static struct pci_driver qla4xxx_pci_driver = {
5475 	.name		= DRIVER_NAME,
5476 	.id_table	= qla4xxx_pci_tbl,
5477 	.probe		= qla4xxx_probe_adapter,
5478 	.remove		= qla4xxx_remove_adapter,
5479 	.err_handler = &qla4xxx_err_handler,
5480 };
5481 
qla4xxx_module_init(void)5482 static int __init qla4xxx_module_init(void)
5483 {
5484 	int ret;
5485 
5486 	/* Allocate cache for SRBs. */
5487 	srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
5488 				       SLAB_HWCACHE_ALIGN, NULL);
5489 	if (srb_cachep == NULL) {
5490 		printk(KERN_ERR
5491 		       "%s: Unable to allocate SRB cache..."
5492 		       "Failing load!\n", DRIVER_NAME);
5493 		ret = -ENOMEM;
5494 		goto no_srp_cache;
5495 	}
5496 
5497 	/* Derive version string. */
5498 	strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
5499 	if (ql4xextended_error_logging)
5500 		strcat(qla4xxx_version_str, "-debug");
5501 
5502 	qla4xxx_scsi_transport =
5503 		iscsi_register_transport(&qla4xxx_iscsi_transport);
5504 	if (!qla4xxx_scsi_transport){
5505 		ret = -ENODEV;
5506 		goto release_srb_cache;
5507 	}
5508 
5509 	ret = pci_register_driver(&qla4xxx_pci_driver);
5510 	if (ret)
5511 		goto unregister_transport;
5512 
5513 	printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
5514 	return 0;
5515 
5516 unregister_transport:
5517 	iscsi_unregister_transport(&qla4xxx_iscsi_transport);
5518 release_srb_cache:
5519 	kmem_cache_destroy(srb_cachep);
5520 no_srp_cache:
5521 	return ret;
5522 }
5523 
qla4xxx_module_exit(void)5524 static void __exit qla4xxx_module_exit(void)
5525 {
5526 	pci_unregister_driver(&qla4xxx_pci_driver);
5527 	iscsi_unregister_transport(&qla4xxx_iscsi_transport);
5528 	kmem_cache_destroy(srb_cachep);
5529 }
5530 
5531 module_init(qla4xxx_module_init);
5532 module_exit(qla4xxx_module_exit);
5533 
5534 MODULE_AUTHOR("QLogic Corporation");
5535 MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
5536 MODULE_LICENSE("GPL");
5537 MODULE_VERSION(QLA4XXX_DRIVER_VERSION);
5538