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