]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/scsi/qla4xxx/ql4_os.c
[SCSI] qla4xxx: added support to show multiple iface in sysfs
[mirror_ubuntu-bionic-kernel.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>
afaf5a2d
DS
9
10#include <scsi/scsi_tcq.h>
11#include <scsi/scsicam.h>
12
13#include "ql4_def.h"
bee4fe8e
DS
14#include "ql4_version.h"
15#include "ql4_glbl.h"
16#include "ql4_dbg.h"
17#include "ql4_inline.h"
afaf5a2d
DS
18
19/*
20 * Driver version
21 */
47975477 22static char qla4xxx_version_str[40];
afaf5a2d
DS
23
24/*
25 * SRB allocation cache
26 */
e18b890b 27static struct kmem_cache *srb_cachep;
afaf5a2d
DS
28
29/*
30 * Module parameter information and variables
31 */
afaf5a2d 32int ql4xdontresethba = 0;
f4f5df23 33module_param(ql4xdontresethba, int, S_IRUGO | S_IWUSR);
afaf5a2d 34MODULE_PARM_DESC(ql4xdontresethba,
f4f5df23
VC
35 "Don't reset the HBA for driver recovery \n"
36 " 0 - It will reset HBA (Default)\n"
37 " 1 - It will NOT reset HBA");
afaf5a2d 38
11010fec 39int ql4xextended_error_logging = 0; /* 0 = off, 1 = log errors */
f4f5df23 40module_param(ql4xextended_error_logging, int, S_IRUGO | S_IWUSR);
11010fec 41MODULE_PARM_DESC(ql4xextended_error_logging,
afaf5a2d
DS
42 "Option to enable extended error logging, "
43 "Default is 0 - no logging, 1 - debug logging");
44
f4f5df23
VC
45int ql4xenablemsix = 1;
46module_param(ql4xenablemsix, int, S_IRUGO|S_IWUSR);
47MODULE_PARM_DESC(ql4xenablemsix,
48 "Set to enable MSI or MSI-X interrupt mechanism.\n"
49 " 0 = enable INTx interrupt mechanism.\n"
50 " 1 = enable MSI-X interrupt mechanism (Default).\n"
51 " 2 = enable MSI interrupt mechanism.");
477ffb9d 52
d510d965 53#define QL4_DEF_QDEPTH 32
8bb4033d
VC
54static int ql4xmaxqdepth = QL4_DEF_QDEPTH;
55module_param(ql4xmaxqdepth, int, S_IRUGO | S_IWUSR);
56MODULE_PARM_DESC(ql4xmaxqdepth,
57 "Maximum queue depth to report for target devices.\n"
58 " Default: 32.");
d510d965 59
3038727c
VC
60static int ql4xsess_recovery_tmo = QL4_SESS_RECOVERY_TMO;
61module_param(ql4xsess_recovery_tmo, int, S_IRUGO);
62MODULE_PARM_DESC(ql4xsess_recovery_tmo,
63 "Target Session Recovery Timeout.\n"
64 " Default: 30 sec.");
65
afaf5a2d
DS
66/*
67 * SCSI host template entry points
68 */
47975477 69static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
afaf5a2d
DS
70
71/*
72 * iSCSI template entry points
73 */
2174a04e
MC
74static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
75 enum iscsi_tgt_dscvr type, uint32_t enable,
76 struct sockaddr *dst_addr);
afaf5a2d
DS
77static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
78 enum iscsi_param param, char *buf);
79static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
80 enum iscsi_param param, char *buf);
aa1e93a2
MC
81static int qla4xxx_host_get_param(struct Scsi_Host *shost,
82 enum iscsi_host_param param, char *buf);
d00efe3f
MC
83static int qla4xxx_iface_set_param(struct Scsi_Host *shost, char *data,
84 int count);
ed1086e0
VC
85static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
86 enum iscsi_param_type param_type,
87 int param, char *buf);
afaf5a2d 88static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session);
5c656af7 89static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
afaf5a2d
DS
90
91/*
92 * SCSI host template entry points
93 */
f281233d 94static int qla4xxx_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmd);
09a0f719 95static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
afaf5a2d 96static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
ce545039 97static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
afaf5a2d
DS
98static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
99static int qla4xxx_slave_alloc(struct scsi_device *device);
100static int qla4xxx_slave_configure(struct scsi_device *device);
101static void qla4xxx_slave_destroy(struct scsi_device *sdev);
024f801f 102static void qla4xxx_scan_start(struct Scsi_Host *shost);
afaf5a2d 103
f4f5df23
VC
104static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
105 QLA82XX_LEGACY_INTR_CONFIG;
106
afaf5a2d
DS
107static struct scsi_host_template qla4xxx_driver_template = {
108 .module = THIS_MODULE,
109 .name = DRIVER_NAME,
110 .proc_name = DRIVER_NAME,
111 .queuecommand = qla4xxx_queuecommand,
112
09a0f719 113 .eh_abort_handler = qla4xxx_eh_abort,
afaf5a2d 114 .eh_device_reset_handler = qla4xxx_eh_device_reset,
ce545039 115 .eh_target_reset_handler = qla4xxx_eh_target_reset,
afaf5a2d 116 .eh_host_reset_handler = qla4xxx_eh_host_reset,
5c656af7 117 .eh_timed_out = qla4xxx_eh_cmd_timed_out,
afaf5a2d
DS
118
119 .slave_configure = qla4xxx_slave_configure,
120 .slave_alloc = qla4xxx_slave_alloc,
121 .slave_destroy = qla4xxx_slave_destroy,
122
921601b7 123 .scan_finished = iscsi_scan_finished,
024f801f 124 .scan_start = qla4xxx_scan_start,
921601b7 125
afaf5a2d
DS
126 .this_id = -1,
127 .cmd_per_lun = 3,
128 .use_clustering = ENABLE_CLUSTERING,
129 .sg_tablesize = SG_ALL,
130
131 .max_sectors = 0xFFFF,
7ad633c0 132 .shost_attrs = qla4xxx_host_attrs,
afaf5a2d
DS
133};
134
135static struct iscsi_transport qla4xxx_iscsi_transport = {
136 .owner = THIS_MODULE,
137 .name = DRIVER_NAME,
d8196ed2
MC
138 .caps = CAP_FW_DB | CAP_SENDTARGETS_OFFLOAD |
139 CAP_DATA_PATH_OFFLOAD,
aa1e93a2 140 .param_mask = ISCSI_CONN_PORT | ISCSI_CONN_ADDRESS |
3b2bef1f
VC
141 ISCSI_TARGET_NAME | ISCSI_TPGT |
142 ISCSI_TARGET_ALIAS,
8ad5781a 143 .host_param_mask = ISCSI_HOST_HWADDRESS |
22236961 144 ISCSI_HOST_IPADDRESS |
8ad5781a 145 ISCSI_HOST_INITIATOR_NAME,
ed1086e0
VC
146 .iface_param_mask = ISCSI_NET_IPV4_ADDR |
147 ISCSI_NET_IPV4_SUBNET |
148 ISCSI_NET_IPV4_GW |
149 ISCSI_NET_IPV4_BOOTPROTO |
150 ISCSI_NET_IFACE_ENABLE |
151 ISCSI_NET_IPV6_LINKLOCAL |
152 ISCSI_NET_IPV6_ADDR |
153 ISCSI_NET_IPV6_ROUTER |
154 ISCSI_NET_IPV6_ADDR_AUTOCFG |
155 ISCSI_NET_IPV6_LINKLOCAL_AUTOCFG |
156 ISCSI_NET_IFACE_ENABLE,
afaf5a2d
DS
157 .tgt_dscvr = qla4xxx_tgt_dscvr,
158 .get_conn_param = qla4xxx_conn_get_param,
159 .get_session_param = qla4xxx_sess_get_param,
aa1e93a2 160 .get_host_param = qla4xxx_host_get_param,
d00efe3f 161 .set_iface_param = qla4xxx_iface_set_param,
afaf5a2d 162 .session_recovery_timedout = qla4xxx_recovery_timedout,
ed1086e0 163 .get_iface_param = qla4xxx_get_iface_param,
afaf5a2d
DS
164};
165
166static struct scsi_transport_template *qla4xxx_scsi_transport;
167
ed1086e0
VC
168static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
169 enum iscsi_param_type param_type,
170 int param, char *buf)
171{
172 struct Scsi_Host *shost = iscsi_iface_to_shost(iface);
173 struct scsi_qla_host *ha = to_qla_host(shost);
174 int len = -ENOSYS;
175
176 if (param_type != ISCSI_NET_PARAM)
177 return -ENOSYS;
178
179 switch (param) {
180 case ISCSI_NET_PARAM_IPV4_ADDR:
181 len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
182 break;
183 case ISCSI_NET_PARAM_IPV4_SUBNET:
184 len = sprintf(buf, "%pI4\n", &ha->ip_config.subnet_mask);
185 break;
186 case ISCSI_NET_PARAM_IPV4_GW:
187 len = sprintf(buf, "%pI4\n", &ha->ip_config.gateway);
188 break;
189 case ISCSI_NET_PARAM_IFACE_ENABLE:
190 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
191 len = sprintf(buf, "%s\n",
192 (ha->ip_config.ipv4_options &
193 IPOPT_IPV4_PROTOCOL_ENABLE) ?
194 "enabled" : "disabled");
195 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
196 len = sprintf(buf, "%s\n",
197 (ha->ip_config.ipv6_options &
198 IPV6_OPT_IPV6_PROTOCOL_ENABLE) ?
199 "enabled" : "disabled");
200 break;
201 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
202 len = sprintf(buf, "%s\n",
203 (ha->ip_config.tcp_options & TCPOPT_DHCP_ENABLE) ?
204 "dhcp" : "static");
205 break;
206 case ISCSI_NET_PARAM_IPV6_ADDR:
207 if (iface->iface_num == 0)
208 len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr0);
209 if (iface->iface_num == 1)
210 len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr1);
211 break;
212 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
213 len = sprintf(buf, "%pI6\n",
214 &ha->ip_config.ipv6_link_local_addr);
215 break;
216 case ISCSI_NET_PARAM_IPV6_ROUTER:
217 len = sprintf(buf, "%pI6\n",
218 &ha->ip_config.ipv6_default_router_addr);
219 break;
220 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
221 len = sprintf(buf, "%s\n",
222 (ha->ip_config.ipv6_addl_options &
223 IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) ?
224 "nd" : "static");
225 break;
226 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
227 len = sprintf(buf, "%s\n",
228 (ha->ip_config.ipv6_addl_options &
229 IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR) ?
230 "auto" : "static");
231 break;
232 default:
233 len = -ENOSYS;
234 }
235
236 return len;
237}
238
5c656af7
MC
239static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
240{
241 struct iscsi_cls_session *session;
242 struct ddb_entry *ddb_entry;
243
244 session = starget_to_session(scsi_target(sc->device));
245 ddb_entry = session->dd_data;
246
247 /* if we are not logged in then the LLD is going to clean up the cmd */
248 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
249 return BLK_EH_RESET_TIMER;
250 else
251 return BLK_EH_NOT_HANDLED;
252}
253
afaf5a2d
DS
254static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session)
255{
256 struct ddb_entry *ddb_entry = session->dd_data;
257 struct scsi_qla_host *ha = ddb_entry->ha;
258
568d303b
MC
259 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
260 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
261
3013cea8 262 DEBUG2(printk("scsi%ld: %s: ddb [%d] session recovery timeout "
568d303b
MC
263 "of (%d) secs exhausted, marking device DEAD.\n",
264 ha->host_no, __func__, ddb_entry->fw_ddb_index,
3038727c 265 ddb_entry->sess->recovery_tmo));
568d303b 266 }
afaf5a2d
DS
267}
268
aa1e93a2
MC
269static int qla4xxx_host_get_param(struct Scsi_Host *shost,
270 enum iscsi_host_param param, char *buf)
271{
272 struct scsi_qla_host *ha = to_qla_host(shost);
273 int len;
274
275 switch (param) {
276 case ISCSI_HOST_PARAM_HWADDRESS:
7ffc49a6 277 len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
8ad5781a 278 break;
22236961 279 case ISCSI_HOST_PARAM_IPADDRESS:
2bab08fc 280 len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
22236961 281 break;
8ad5781a 282 case ISCSI_HOST_PARAM_INITIATOR_NAME:
22236961 283 len = sprintf(buf, "%s\n", ha->name_string);
aa1e93a2
MC
284 break;
285 default:
286 return -ENOSYS;
287 }
288
289 return len;
290}
291
ed1086e0
VC
292static void qla4xxx_create_ipv4_iface(struct scsi_qla_host *ha)
293{
294 if (ha->iface_ipv4)
295 return;
296
297 /* IPv4 */
298 ha->iface_ipv4 = iscsi_create_iface(ha->host,
299 &qla4xxx_iscsi_transport,
300 ISCSI_IFACE_TYPE_IPV4, 0, 0);
301 if (!ha->iface_ipv4)
302 ql4_printk(KERN_ERR, ha, "Could not create IPv4 iSCSI "
303 "iface0.\n");
304}
305
306static void qla4xxx_create_ipv6_iface(struct scsi_qla_host *ha)
307{
308 if (!ha->iface_ipv6_0)
309 /* IPv6 iface-0 */
310 ha->iface_ipv6_0 = iscsi_create_iface(ha->host,
311 &qla4xxx_iscsi_transport,
312 ISCSI_IFACE_TYPE_IPV6, 0,
313 0);
314 if (!ha->iface_ipv6_0)
315 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
316 "iface0.\n");
317
318 if (!ha->iface_ipv6_1)
319 /* IPv6 iface-1 */
320 ha->iface_ipv6_1 = iscsi_create_iface(ha->host,
321 &qla4xxx_iscsi_transport,
322 ISCSI_IFACE_TYPE_IPV6, 1,
323 0);
324 if (!ha->iface_ipv6_1)
325 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
326 "iface1.\n");
327}
328
329static void qla4xxx_create_ifaces(struct scsi_qla_host *ha)
330{
331 if (ha->ip_config.ipv4_options & IPOPT_IPV4_PROTOCOL_ENABLE)
332 qla4xxx_create_ipv4_iface(ha);
333
334 if (ha->ip_config.ipv6_options & IPV6_OPT_IPV6_PROTOCOL_ENABLE)
335 qla4xxx_create_ipv6_iface(ha);
336}
337
338static void qla4xxx_destroy_ipv4_iface(struct scsi_qla_host *ha)
339{
340 if (ha->iface_ipv4) {
341 iscsi_destroy_iface(ha->iface_ipv4);
342 ha->iface_ipv4 = NULL;
343 }
344}
345
346static void qla4xxx_destroy_ipv6_iface(struct scsi_qla_host *ha)
347{
348 if (ha->iface_ipv6_0) {
349 iscsi_destroy_iface(ha->iface_ipv6_0);
350 ha->iface_ipv6_0 = NULL;
351 }
352 if (ha->iface_ipv6_1) {
353 iscsi_destroy_iface(ha->iface_ipv6_1);
354 ha->iface_ipv6_1 = NULL;
355 }
356}
357
358static void qla4xxx_destroy_ifaces(struct scsi_qla_host *ha)
359{
360 qla4xxx_destroy_ipv4_iface(ha);
361 qla4xxx_destroy_ipv6_iface(ha);
362}
363
d00efe3f
MC
364static void qla4xxx_set_ipv6(struct scsi_qla_host *ha,
365 struct iscsi_iface_param_info *iface_param,
366 struct addr_ctrl_blk *init_fw_cb)
367{
368 /*
369 * iface_num 0 is valid for IPv6 Addr, linklocal, router, autocfg.
370 * iface_num 1 is valid only for IPv6 Addr.
371 */
372 switch (iface_param->param) {
373 case ISCSI_NET_PARAM_IPV6_ADDR:
374 if (iface_param->iface_num & 0x1)
375 /* IPv6 Addr 1 */
376 memcpy(init_fw_cb->ipv6_addr1, iface_param->value,
377 sizeof(init_fw_cb->ipv6_addr1));
378 else
379 /* IPv6 Addr 0 */
380 memcpy(init_fw_cb->ipv6_addr0, iface_param->value,
381 sizeof(init_fw_cb->ipv6_addr0));
382 break;
383 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
384 if (iface_param->iface_num & 0x1)
385 break;
386 memcpy(init_fw_cb->ipv6_if_id, &iface_param->value[8],
387 sizeof(init_fw_cb->ipv6_if_id));
388 break;
389 case ISCSI_NET_PARAM_IPV6_ROUTER:
390 if (iface_param->iface_num & 0x1)
391 break;
392 memcpy(init_fw_cb->ipv6_dflt_rtr_addr, iface_param->value,
393 sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
394 break;
395 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
396 /* Autocfg applies to even interface */
397 if (iface_param->iface_num & 0x1)
398 break;
399
400 if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_DISABLE)
401 init_fw_cb->ipv6_addtl_opts &=
402 cpu_to_le16(
403 ~IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
404 else if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_ND_ENABLE)
405 init_fw_cb->ipv6_addtl_opts |=
406 cpu_to_le16(
407 IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
408 else
409 ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
410 "IPv6 addr\n");
411 break;
412 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
413 /* Autocfg applies to even interface */
414 if (iface_param->iface_num & 0x1)
415 break;
416
417 if (iface_param->value[0] ==
418 ISCSI_IPV6_LINKLOCAL_AUTOCFG_ENABLE)
419 init_fw_cb->ipv6_addtl_opts |= cpu_to_le16(
420 IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
421 else if (iface_param->value[0] ==
422 ISCSI_IPV6_LINKLOCAL_AUTOCFG_DISABLE)
423 init_fw_cb->ipv6_addtl_opts &= cpu_to_le16(
424 ~IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
425 else
426 ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
427 "IPv6 linklocal addr\n");
428 break;
429 case ISCSI_NET_PARAM_IPV6_ROUTER_AUTOCFG:
430 /* Autocfg applies to even interface */
431 if (iface_param->iface_num & 0x1)
432 break;
433
434 if (iface_param->value[0] == ISCSI_IPV6_ROUTER_AUTOCFG_ENABLE)
435 memset(init_fw_cb->ipv6_dflt_rtr_addr, 0,
436 sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
437 break;
438 case ISCSI_NET_PARAM_IFACE_ENABLE:
ed1086e0 439 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
d00efe3f
MC
440 init_fw_cb->ipv6_opts |=
441 cpu_to_le16(IPV6_OPT_IPV6_PROTOCOL_ENABLE);
ed1086e0
VC
442 qla4xxx_create_ipv6_iface(ha);
443 } else {
d00efe3f
MC
444 init_fw_cb->ipv6_opts &=
445 cpu_to_le16(~IPV6_OPT_IPV6_PROTOCOL_ENABLE &
446 0xFFFF);
ed1086e0
VC
447 qla4xxx_destroy_ipv6_iface(ha);
448 }
d00efe3f
MC
449 break;
450 case ISCSI_NET_PARAM_VLAN_ID:
451 if (iface_param->len != sizeof(init_fw_cb->ipv6_vlan_tag))
452 break;
453 init_fw_cb->ipv6_vlan_tag = *(uint16_t *)iface_param->value;
454 break;
455 default:
456 ql4_printk(KERN_ERR, ha, "Unknown IPv6 param = %d\n",
457 iface_param->param);
458 break;
459 }
460}
461
462static void qla4xxx_set_ipv4(struct scsi_qla_host *ha,
463 struct iscsi_iface_param_info *iface_param,
464 struct addr_ctrl_blk *init_fw_cb)
465{
466 switch (iface_param->param) {
467 case ISCSI_NET_PARAM_IPV4_ADDR:
468 memcpy(init_fw_cb->ipv4_addr, iface_param->value,
469 sizeof(init_fw_cb->ipv4_addr));
470 break;
471 case ISCSI_NET_PARAM_IPV4_SUBNET:
472 memcpy(init_fw_cb->ipv4_subnet, iface_param->value,
473 sizeof(init_fw_cb->ipv4_subnet));
474 break;
475 case ISCSI_NET_PARAM_IPV4_GW:
476 memcpy(init_fw_cb->ipv4_gw_addr, iface_param->value,
477 sizeof(init_fw_cb->ipv4_gw_addr));
478 break;
479 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
480 if (iface_param->value[0] == ISCSI_BOOTPROTO_DHCP)
481 init_fw_cb->ipv4_tcp_opts |=
482 cpu_to_le16(TCPOPT_DHCP_ENABLE);
483 else if (iface_param->value[0] == ISCSI_BOOTPROTO_STATIC)
484 init_fw_cb->ipv4_tcp_opts &=
485 cpu_to_le16(~TCPOPT_DHCP_ENABLE);
486 else
487 ql4_printk(KERN_ERR, ha, "Invalid IPv4 bootproto\n");
488 break;
489 case ISCSI_NET_PARAM_IFACE_ENABLE:
ed1086e0 490 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
d00efe3f 491 init_fw_cb->ipv4_ip_opts |=
2bab08fc 492 cpu_to_le16(IPOPT_IPV4_PROTOCOL_ENABLE);
ed1086e0
VC
493 qla4xxx_create_ipv4_iface(ha);
494 } else {
d00efe3f 495 init_fw_cb->ipv4_ip_opts &=
2bab08fc 496 cpu_to_le16(~IPOPT_IPV4_PROTOCOL_ENABLE &
d00efe3f 497 0xFFFF);
ed1086e0
VC
498 qla4xxx_destroy_ipv4_iface(ha);
499 }
d00efe3f
MC
500 break;
501 case ISCSI_NET_PARAM_VLAN_ID:
502 if (iface_param->len != sizeof(init_fw_cb->ipv4_vlan_tag))
503 break;
504 init_fw_cb->ipv4_vlan_tag = *(uint16_t *)iface_param->value;
505 break;
506 default:
507 ql4_printk(KERN_ERR, ha, "Unknown IPv4 param = %d\n",
508 iface_param->param);
509 break;
510 }
511}
512
513static void
514qla4xxx_initcb_to_acb(struct addr_ctrl_blk *init_fw_cb)
515{
516 struct addr_ctrl_blk_def *acb;
517 acb = (struct addr_ctrl_blk_def *)init_fw_cb;
518 memset(acb->reserved1, 0, sizeof(acb->reserved1));
519 memset(acb->reserved2, 0, sizeof(acb->reserved2));
520 memset(acb->reserved3, 0, sizeof(acb->reserved3));
521 memset(acb->reserved4, 0, sizeof(acb->reserved4));
522 memset(acb->reserved5, 0, sizeof(acb->reserved5));
523 memset(acb->reserved6, 0, sizeof(acb->reserved6));
524 memset(acb->reserved7, 0, sizeof(acb->reserved7));
525 memset(acb->reserved8, 0, sizeof(acb->reserved8));
526 memset(acb->reserved9, 0, sizeof(acb->reserved9));
527 memset(acb->reserved10, 0, sizeof(acb->reserved10));
528 memset(acb->reserved11, 0, sizeof(acb->reserved11));
529 memset(acb->reserved12, 0, sizeof(acb->reserved12));
530 memset(acb->reserved13, 0, sizeof(acb->reserved13));
531 memset(acb->reserved14, 0, sizeof(acb->reserved14));
532 memset(acb->reserved15, 0, sizeof(acb->reserved15));
533}
534
535static int
536qla4xxx_iface_set_param(struct Scsi_Host *shost, char *data, int count)
537{
538 struct scsi_qla_host *ha = to_qla_host(shost);
539 int rval = 0;
540 struct iscsi_iface_param_info *iface_param = NULL;
541 struct addr_ctrl_blk *init_fw_cb = NULL;
542 dma_addr_t init_fw_cb_dma;
543 uint32_t mbox_cmd[MBOX_REG_COUNT];
544 uint32_t mbox_sts[MBOX_REG_COUNT];
545 uint32_t total_param_count;
546 uint32_t length;
547
548 init_fw_cb = dma_alloc_coherent(&ha->pdev->dev,
549 sizeof(struct addr_ctrl_blk),
550 &init_fw_cb_dma, GFP_KERNEL);
551 if (!init_fw_cb) {
552 ql4_printk(KERN_ERR, ha, "%s: Unable to alloc init_cb\n",
553 __func__);
554 return -ENOMEM;
555 }
556
557 memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
558 memset(&mbox_cmd, 0, sizeof(mbox_cmd));
559 memset(&mbox_sts, 0, sizeof(mbox_sts));
560
561 if (qla4xxx_get_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma)) {
562 ql4_printk(KERN_ERR, ha, "%s: get ifcb failed\n", __func__);
563 rval = -EIO;
564 goto exit_init_fw_cb;
565 }
566
567 total_param_count = count;
568 iface_param = (struct iscsi_iface_param_info *)data;
569
570 for ( ; total_param_count != 0; total_param_count--) {
571 length = iface_param->len;
572
573 if (iface_param->param_type != ISCSI_NET_PARAM)
574 continue;
575
576 switch (iface_param->iface_type) {
577 case ISCSI_IFACE_TYPE_IPV4:
578 switch (iface_param->iface_num) {
579 case 0:
580 qla4xxx_set_ipv4(ha, iface_param, init_fw_cb);
581 break;
582 default:
583 /* Cannot have more than one IPv4 interface */
584 ql4_printk(KERN_ERR, ha, "Invalid IPv4 iface "
585 "number = %d\n",
586 iface_param->iface_num);
587 break;
588 }
589 break;
590 case ISCSI_IFACE_TYPE_IPV6:
591 switch (iface_param->iface_num) {
592 case 0:
593 case 1:
594 qla4xxx_set_ipv6(ha, iface_param, init_fw_cb);
595 break;
596 default:
597 /* Cannot have more than two IPv6 interface */
598 ql4_printk(KERN_ERR, ha, "Invalid IPv6 iface "
599 "number = %d\n",
600 iface_param->iface_num);
601 break;
602 }
603 break;
604 default:
605 ql4_printk(KERN_ERR, ha, "Invalid iface type\n");
606 break;
607 }
608
609 iface_param = (struct iscsi_iface_param_info *)
610 ((uint8_t *)iface_param +
611 sizeof(struct iscsi_iface_param_info) + length);
612 }
613
614 init_fw_cb->cookie = cpu_to_le32(0x11BEAD5A);
615
616 rval = qla4xxx_set_flash(ha, init_fw_cb_dma, FLASH_SEGMENT_IFCB,
617 sizeof(struct addr_ctrl_blk),
618 FLASH_OPT_RMW_COMMIT);
619 if (rval != QLA_SUCCESS) {
620 ql4_printk(KERN_ERR, ha, "%s: set flash mbx failed\n",
621 __func__);
622 rval = -EIO;
623 goto exit_init_fw_cb;
624 }
625
626 qla4xxx_disable_acb(ha);
627
628 qla4xxx_initcb_to_acb(init_fw_cb);
629
630 rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma);
631 if (rval != QLA_SUCCESS) {
632 ql4_printk(KERN_ERR, ha, "%s: set acb mbx failed\n",
633 __func__);
634 rval = -EIO;
635 goto exit_init_fw_cb;
636 }
637
638 memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
639 qla4xxx_update_local_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb,
640 init_fw_cb_dma);
641
642exit_init_fw_cb:
643 dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk),
644 init_fw_cb, init_fw_cb_dma);
645
646 return rval;
647}
648
afaf5a2d
DS
649static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
650 enum iscsi_param param, char *buf)
651{
652 struct ddb_entry *ddb_entry = sess->dd_data;
653 int len;
654
655 switch (param) {
656 case ISCSI_PARAM_TARGET_NAME:
657 len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
658 ddb_entry->iscsi_name);
659 break;
660 case ISCSI_PARAM_TPGT:
661 len = sprintf(buf, "%u\n", ddb_entry->tpgt);
662 break;
3b2bef1f
VC
663 case ISCSI_PARAM_TARGET_ALIAS:
664 len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
665 ddb_entry->iscsi_alias);
666 break;
afaf5a2d
DS
667 default:
668 return -ENOSYS;
669 }
670
671 return len;
672}
673
674static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
675 enum iscsi_param param, char *buf)
676{
677 struct iscsi_cls_session *session;
678 struct ddb_entry *ddb_entry;
679 int len;
680
681 session = iscsi_dev_to_session(conn->dev.parent);
682 ddb_entry = session->dd_data;
683
684 switch (param) {
685 case ISCSI_PARAM_CONN_PORT:
686 len = sprintf(buf, "%hu\n", ddb_entry->port);
687 break;
688 case ISCSI_PARAM_CONN_ADDRESS:
689 /* TODO: what are the ipv6 bits */
63779436 690 len = sprintf(buf, "%pI4\n", &ddb_entry->ip_addr);
afaf5a2d
DS
691 break;
692 default:
693 return -ENOSYS;
694 }
695
696 return len;
697}
698
2174a04e
MC
699static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
700 enum iscsi_tgt_dscvr type, uint32_t enable,
701 struct sockaddr *dst_addr)
afaf5a2d
DS
702{
703 struct scsi_qla_host *ha;
afaf5a2d
DS
704 struct sockaddr_in *addr;
705 struct sockaddr_in6 *addr6;
706 int ret = 0;
707
afaf5a2d
DS
708 ha = (struct scsi_qla_host *) shost->hostdata;
709
710 switch (type) {
711 case ISCSI_TGT_DSCVR_SEND_TARGETS:
712 if (dst_addr->sa_family == AF_INET) {
713 addr = (struct sockaddr_in *)dst_addr;
714 if (qla4xxx_send_tgts(ha, (char *)&addr->sin_addr,
715 addr->sin_port) != QLA_SUCCESS)
716 ret = -EIO;
717 } else if (dst_addr->sa_family == AF_INET6) {
718 /*
719 * TODO: fix qla4xxx_send_tgts
720 */
721 addr6 = (struct sockaddr_in6 *)dst_addr;
722 if (qla4xxx_send_tgts(ha, (char *)&addr6->sin6_addr,
723 addr6->sin6_port) != QLA_SUCCESS)
724 ret = -EIO;
725 } else
726 ret = -ENOSYS;
727 break;
728 default:
729 ret = -ENOSYS;
730 }
afaf5a2d
DS
731 return ret;
732}
733
734void qla4xxx_destroy_sess(struct ddb_entry *ddb_entry)
735{
736 if (!ddb_entry->sess)
737 return;
738
739 if (ddb_entry->conn) {
26974789 740 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
afaf5a2d
DS
741 iscsi_remove_session(ddb_entry->sess);
742 }
743 iscsi_free_session(ddb_entry->sess);
744}
745
746int qla4xxx_add_sess(struct ddb_entry *ddb_entry)
747{
748 int err;
749
3038727c 750 ddb_entry->sess->recovery_tmo = ql4xsess_recovery_tmo;
3013cea8 751
afaf5a2d
DS
752 err = iscsi_add_session(ddb_entry->sess, ddb_entry->fw_ddb_index);
753 if (err) {
754 DEBUG2(printk(KERN_ERR "Could not add session.\n"));
755 return err;
756 }
757
5d91e209 758 ddb_entry->conn = iscsi_create_conn(ddb_entry->sess, 0, 0);
afaf5a2d
DS
759 if (!ddb_entry->conn) {
760 iscsi_remove_session(ddb_entry->sess);
761 DEBUG2(printk(KERN_ERR "Could not add connection.\n"));
762 return -ENOMEM;
763 }
b635930d
MC
764
765 /* finally ready to go */
766 iscsi_unblock_session(ddb_entry->sess);
afaf5a2d
DS
767 return 0;
768}
769
770struct ddb_entry *qla4xxx_alloc_sess(struct scsi_qla_host *ha)
771{
772 struct ddb_entry *ddb_entry;
773 struct iscsi_cls_session *sess;
774
5d91e209
MC
775 sess = iscsi_alloc_session(ha->host, &qla4xxx_iscsi_transport,
776 sizeof(struct ddb_entry));
afaf5a2d
DS
777 if (!sess)
778 return NULL;
779
780 ddb_entry = sess->dd_data;
781 memset(ddb_entry, 0, sizeof(*ddb_entry));
782 ddb_entry->ha = ha;
783 ddb_entry->sess = sess;
784 return ddb_entry;
785}
786
024f801f
MC
787static void qla4xxx_scan_start(struct Scsi_Host *shost)
788{
789 struct scsi_qla_host *ha = shost_priv(shost);
790 struct ddb_entry *ddb_entry, *ddbtemp;
791
792 /* finish setup of sessions that were already setup in firmware */
793 list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
794 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE)
795 qla4xxx_add_sess(ddb_entry);
796 }
797}
798
afaf5a2d
DS
799/*
800 * Timer routines
801 */
802
803static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
804 unsigned long interval)
805{
806 DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
807 __func__, ha->host->host_no));
808 init_timer(&ha->timer);
809 ha->timer.expires = jiffies + interval * HZ;
810 ha->timer.data = (unsigned long)ha;
811 ha->timer.function = (void (*)(unsigned long))func;
812 add_timer(&ha->timer);
813 ha->timer_active = 1;
814}
815
816static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
817{
818 del_timer_sync(&ha->timer);
819 ha->timer_active = 0;
820}
821
822/***
823 * qla4xxx_mark_device_missing - mark a device as missing.
824 * @ha: Pointer to host adapter structure.
825 * @ddb_entry: Pointer to device database entry
826 *
f4f5df23 827 * This routine marks a device missing and close connection.
afaf5a2d
DS
828 **/
829void qla4xxx_mark_device_missing(struct scsi_qla_host *ha,
830 struct ddb_entry *ddb_entry)
831{
f4f5df23
VC
832 if ((atomic_read(&ddb_entry->state) != DDB_STATE_DEAD)) {
833 atomic_set(&ddb_entry->state, DDB_STATE_MISSING);
834 DEBUG2(printk("scsi%ld: ddb [%d] marked MISSING\n",
835 ha->host_no, ddb_entry->fw_ddb_index));
836 } else
837 DEBUG2(printk("scsi%ld: ddb [%d] DEAD\n", ha->host_no,
838 ddb_entry->fw_ddb_index))
839
b635930d 840 iscsi_block_session(ddb_entry->sess);
e5bd7b54 841 iscsi_conn_error_event(ddb_entry->conn, ISCSI_ERR_CONN_FAILED);
afaf5a2d
DS
842}
843
f4f5df23
VC
844/**
845 * qla4xxx_mark_all_devices_missing - mark all devices as missing.
846 * @ha: Pointer to host adapter structure.
847 *
848 * This routine marks a device missing and resets the relogin retry count.
849 **/
850void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
851{
852 struct ddb_entry *ddb_entry, *ddbtemp;
853 list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
854 qla4xxx_mark_device_missing(ha, ddb_entry);
855 }
856}
857
afaf5a2d
DS
858static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
859 struct ddb_entry *ddb_entry,
8f0722ca 860 struct scsi_cmnd *cmd)
afaf5a2d
DS
861{
862 struct srb *srb;
863
864 srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
865 if (!srb)
866 return srb;
867
09a0f719 868 kref_init(&srb->srb_ref);
afaf5a2d
DS
869 srb->ha = ha;
870 srb->ddb = ddb_entry;
871 srb->cmd = cmd;
872 srb->flags = 0;
5369887a 873 CMD_SP(cmd) = (void *)srb;
afaf5a2d
DS
874
875 return srb;
876}
877
878static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
879{
880 struct scsi_cmnd *cmd = srb->cmd;
881
882 if (srb->flags & SRB_DMA_VALID) {
5f7186c8 883 scsi_dma_unmap(cmd);
afaf5a2d
DS
884 srb->flags &= ~SRB_DMA_VALID;
885 }
5369887a 886 CMD_SP(cmd) = NULL;
afaf5a2d
DS
887}
888
09a0f719 889void qla4xxx_srb_compl(struct kref *ref)
afaf5a2d 890{
09a0f719 891 struct srb *srb = container_of(ref, struct srb, srb_ref);
afaf5a2d 892 struct scsi_cmnd *cmd = srb->cmd;
09a0f719 893 struct scsi_qla_host *ha = srb->ha;
afaf5a2d
DS
894
895 qla4xxx_srb_free_dma(ha, srb);
896
897 mempool_free(srb, ha->srb_mempool);
898
899 cmd->scsi_done(cmd);
900}
901
902/**
903 * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
8f0722ca 904 * @host: scsi host
afaf5a2d 905 * @cmd: Pointer to Linux's SCSI command structure
afaf5a2d
DS
906 *
907 * Remarks:
908 * This routine is invoked by Linux to send a SCSI command to the driver.
909 * The mid-level driver tries to ensure that queuecommand never gets
910 * invoked concurrently with itself or the interrupt handler (although
911 * the interrupt handler may call this routine as part of request-
912 * completion handling). Unfortunely, it sometimes calls the scheduler
913 * in interrupt context which is a big NO! NO!.
914 **/
8f0722ca 915static int qla4xxx_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
afaf5a2d 916{
8f0722ca 917 struct scsi_qla_host *ha = to_qla_host(host);
afaf5a2d 918 struct ddb_entry *ddb_entry = cmd->device->hostdata;
7fb1921b 919 struct iscsi_cls_session *sess = ddb_entry->sess;
afaf5a2d
DS
920 struct srb *srb;
921 int rval;
922
2232be0d
LC
923 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
924 if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
925 cmd->result = DID_NO_CONNECT << 16;
926 else
927 cmd->result = DID_REQUEUE << 16;
928 goto qc_fail_command;
929 }
930
7fb1921b
MC
931 if (!sess) {
932 cmd->result = DID_IMM_RETRY << 16;
933 goto qc_fail_command;
934 }
935
936 rval = iscsi_session_chkready(sess);
937 if (rval) {
938 cmd->result = rval;
939 goto qc_fail_command;
940 }
941
afaf5a2d
DS
942 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
943 if (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD) {
944 cmd->result = DID_NO_CONNECT << 16;
945 goto qc_fail_command;
946 }
c5e98e91 947 return SCSI_MLQUEUE_TARGET_BUSY;
afaf5a2d
DS
948 }
949
f4f5df23
VC
950 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
951 test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
952 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
953 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
954 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
955 !test_bit(AF_ONLINE, &ha->flags) ||
956 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
477ffb9d
DS
957 goto qc_host_busy;
958
8f0722ca 959 srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd);
afaf5a2d 960 if (!srb)
8f0722ca 961 goto qc_host_busy;
afaf5a2d
DS
962
963 rval = qla4xxx_send_command_to_isp(ha, srb);
964 if (rval != QLA_SUCCESS)
965 goto qc_host_busy_free_sp;
966
afaf5a2d
DS
967 return 0;
968
969qc_host_busy_free_sp:
970 qla4xxx_srb_free_dma(ha, srb);
971 mempool_free(srb, ha->srb_mempool);
972
afaf5a2d
DS
973qc_host_busy:
974 return SCSI_MLQUEUE_HOST_BUSY;
975
976qc_fail_command:
8f0722ca 977 cmd->scsi_done(cmd);
afaf5a2d
DS
978
979 return 0;
980}
981
982/**
983 * qla4xxx_mem_free - frees memory allocated to adapter
984 * @ha: Pointer to host adapter structure.
985 *
986 * Frees memory previously allocated by qla4xxx_mem_alloc
987 **/
988static void qla4xxx_mem_free(struct scsi_qla_host *ha)
989{
990 if (ha->queues)
991 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
992 ha->queues_dma);
993
994 ha->queues_len = 0;
995 ha->queues = NULL;
996 ha->queues_dma = 0;
997 ha->request_ring = NULL;
998 ha->request_dma = 0;
999 ha->response_ring = NULL;
1000 ha->response_dma = 0;
1001 ha->shadow_regs = NULL;
1002 ha->shadow_regs_dma = 0;
1003
1004 /* Free srb pool. */
1005 if (ha->srb_mempool)
1006 mempool_destroy(ha->srb_mempool);
1007
1008 ha->srb_mempool = NULL;
1009
1010 /* release io space registers */
f4f5df23
VC
1011 if (is_qla8022(ha)) {
1012 if (ha->nx_pcibase)
1013 iounmap(
1014 (struct device_reg_82xx __iomem *)ha->nx_pcibase);
f4f5df23 1015 } else if (ha->reg)
afaf5a2d
DS
1016 iounmap(ha->reg);
1017 pci_release_regions(ha->pdev);
1018}
1019
1020/**
1021 * qla4xxx_mem_alloc - allocates memory for use by adapter.
1022 * @ha: Pointer to host adapter structure
1023 *
1024 * Allocates DMA memory for request and response queues. Also allocates memory
1025 * for srbs.
1026 **/
1027static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
1028{
1029 unsigned long align;
1030
1031 /* Allocate contiguous block of DMA memory for queues. */
1032 ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
1033 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
1034 sizeof(struct shadow_regs) +
1035 MEM_ALIGN_VALUE +
1036 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
1037 ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
1038 &ha->queues_dma, GFP_KERNEL);
1039 if (ha->queues == NULL) {
c2660df3
VC
1040 ql4_printk(KERN_WARNING, ha,
1041 "Memory Allocation failed - queues.\n");
afaf5a2d
DS
1042
1043 goto mem_alloc_error_exit;
1044 }
1045 memset(ha->queues, 0, ha->queues_len);
1046
1047 /*
1048 * As per RISC alignment requirements -- the bus-address must be a
1049 * multiple of the request-ring size (in bytes).
1050 */
1051 align = 0;
1052 if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
1053 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
1054 (MEM_ALIGN_VALUE - 1));
1055
1056 /* Update request and response queue pointers. */
1057 ha->request_dma = ha->queues_dma + align;
1058 ha->request_ring = (struct queue_entry *) (ha->queues + align);
1059 ha->response_dma = ha->queues_dma + align +
1060 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
1061 ha->response_ring = (struct queue_entry *) (ha->queues + align +
1062 (REQUEST_QUEUE_DEPTH *
1063 QUEUE_SIZE));
1064 ha->shadow_regs_dma = ha->queues_dma + align +
1065 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
1066 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
1067 ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
1068 (REQUEST_QUEUE_DEPTH *
1069 QUEUE_SIZE) +
1070 (RESPONSE_QUEUE_DEPTH *
1071 QUEUE_SIZE));
1072
1073 /* Allocate memory for srb pool. */
1074 ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
1075 mempool_free_slab, srb_cachep);
1076 if (ha->srb_mempool == NULL) {
c2660df3
VC
1077 ql4_printk(KERN_WARNING, ha,
1078 "Memory Allocation failed - SRB Pool.\n");
afaf5a2d
DS
1079
1080 goto mem_alloc_error_exit;
1081 }
1082
1083 return QLA_SUCCESS;
1084
1085mem_alloc_error_exit:
1086 qla4xxx_mem_free(ha);
1087 return QLA_ERROR;
1088}
1089
f4f5df23
VC
1090/**
1091 * qla4_8xxx_check_fw_alive - Check firmware health
1092 * @ha: Pointer to host adapter structure.
1093 *
1094 * Context: Interrupt
1095 **/
1096static void qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
1097{
1098 uint32_t fw_heartbeat_counter, halt_status;
1099
1100 fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
2232be0d
LC
1101 /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
1102 if (fw_heartbeat_counter == 0xffffffff) {
1103 DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
1104 "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
1105 ha->host_no, __func__));
1106 return;
1107 }
f4f5df23
VC
1108
1109 if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
1110 ha->seconds_since_last_heartbeat++;
1111 /* FW not alive after 2 seconds */
1112 if (ha->seconds_since_last_heartbeat == 2) {
1113 ha->seconds_since_last_heartbeat = 0;
1114 halt_status = qla4_8xxx_rd_32(ha,
68d92ebf
VC
1115 QLA82XX_PEG_HALT_STATUS1);
1116
1117 ql4_printk(KERN_INFO, ha,
1118 "scsi(%ld): %s, Dumping hw/fw registers:\n "
1119 " PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2:"
1120 " 0x%x,\n PEG_NET_0_PC: 0x%x, PEG_NET_1_PC:"
1121 " 0x%x,\n PEG_NET_2_PC: 0x%x, PEG_NET_3_PC:"
1122 " 0x%x,\n PEG_NET_4_PC: 0x%x\n",
1123 ha->host_no, __func__, halt_status,
1124 qla4_8xxx_rd_32(ha,
1125 QLA82XX_PEG_HALT_STATUS2),
1126 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_0 +
1127 0x3c),
1128 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_1 +
1129 0x3c),
1130 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_2 +
1131 0x3c),
1132 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_3 +
1133 0x3c),
1134 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_4 +
1135 0x3c));
21033639 1136
f4f5df23
VC
1137 /* Since we cannot change dev_state in interrupt
1138 * context, set appropriate DPC flag then wakeup
1139 * DPC */
1140 if (halt_status & HALT_STATUS_UNRECOVERABLE)
1141 set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
1142 else {
1143 printk("scsi%ld: %s: detect abort needed!\n",
1144 ha->host_no, __func__);
1145 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1146 }
1147 qla4xxx_wake_dpc(ha);
21033639 1148 qla4xxx_mailbox_premature_completion(ha);
f4f5df23 1149 }
99457d75
LC
1150 } else
1151 ha->seconds_since_last_heartbeat = 0;
1152
f4f5df23
VC
1153 ha->fw_heartbeat_counter = fw_heartbeat_counter;
1154}
1155
1156/**
1157 * qla4_8xxx_watchdog - Poll dev state
1158 * @ha: Pointer to host adapter structure.
1159 *
1160 * Context: Interrupt
1161 **/
1162void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
1163{
1164 uint32_t dev_state;
1165
1166 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
1167
1168 /* don't poll if reset is going on */
d56a1f7b
LC
1169 if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
1170 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
977f46a4 1171 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags))) {
f4f5df23
VC
1172 if (dev_state == QLA82XX_DEV_NEED_RESET &&
1173 !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
3930b8c1
VC
1174 if (!ql4xdontresethba) {
1175 ql4_printk(KERN_INFO, ha, "%s: HW State: "
1176 "NEED RESET!\n", __func__);
1177 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1178 qla4xxx_wake_dpc(ha);
1179 qla4xxx_mailbox_premature_completion(ha);
1180 }
f4f5df23
VC
1181 } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
1182 !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
3930b8c1
VC
1183 ql4_printk(KERN_INFO, ha, "%s: HW State: NEED QUIES!\n",
1184 __func__);
f4f5df23
VC
1185 set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
1186 qla4xxx_wake_dpc(ha);
1187 } else {
1188 /* Check firmware health */
1189 qla4_8xxx_check_fw_alive(ha);
1190 }
1191 }
1192}
1193
afaf5a2d
DS
1194/**
1195 * qla4xxx_timer - checks every second for work to do.
1196 * @ha: Pointer to host adapter structure.
1197 **/
1198static void qla4xxx_timer(struct scsi_qla_host *ha)
1199{
1200 struct ddb_entry *ddb_entry, *dtemp;
1201 int start_dpc = 0;
2232be0d
LC
1202 uint16_t w;
1203
1204 /* If we are in the middle of AER/EEH processing
1205 * skip any processing and reschedule the timer
1206 */
1207 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1208 mod_timer(&ha->timer, jiffies + HZ);
1209 return;
1210 }
1211
1212 /* Hardware read to trigger an EEH error during mailbox waits. */
1213 if (!pci_channel_offline(ha->pdev))
1214 pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
afaf5a2d 1215
f4f5df23
VC
1216 if (is_qla8022(ha)) {
1217 qla4_8xxx_watchdog(ha);
1218 }
1219
afaf5a2d
DS
1220 /* Search for relogin's to time-out and port down retry. */
1221 list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
1222 /* Count down time between sending relogins */
1223 if (adapter_up(ha) &&
1224 !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
1225 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
1226 if (atomic_read(&ddb_entry->retry_relogin_timer) !=
1227 INVALID_ENTRY) {
1228 if (atomic_read(&ddb_entry->retry_relogin_timer)
1229 == 0) {
1230 atomic_set(&ddb_entry->
1231 retry_relogin_timer,
1232 INVALID_ENTRY);
1233 set_bit(DPC_RELOGIN_DEVICE,
1234 &ha->dpc_flags);
1235 set_bit(DF_RELOGIN, &ddb_entry->flags);
f4f5df23 1236 DEBUG2(printk("scsi%ld: %s: ddb [%d]"
afaf5a2d
DS
1237 " login device\n",
1238 ha->host_no, __func__,
1239 ddb_entry->fw_ddb_index));
1240 } else
1241 atomic_dec(&ddb_entry->
1242 retry_relogin_timer);
1243 }
1244 }
1245
1246 /* Wait for relogin to timeout */
1247 if (atomic_read(&ddb_entry->relogin_timer) &&
1248 (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
1249 /*
1250 * If the relogin times out and the device is
1251 * still NOT ONLINE then try and relogin again.
1252 */
1253 if (atomic_read(&ddb_entry->state) !=
1254 DDB_STATE_ONLINE &&
1255 ddb_entry->fw_ddb_device_state ==
1256 DDB_DS_SESSION_FAILED) {
1257 /* Reset retry relogin timer */
1258 atomic_inc(&ddb_entry->relogin_retry_count);
f4f5df23 1259 DEBUG2(printk("scsi%ld: ddb [%d] relogin"
afaf5a2d
DS
1260 " timed out-retrying"
1261 " relogin (%d)\n",
1262 ha->host_no,
1263 ddb_entry->fw_ddb_index,
1264 atomic_read(&ddb_entry->
1265 relogin_retry_count))
1266 );
1267 start_dpc++;
f4f5df23 1268 DEBUG(printk("scsi%ld:%d:%d: ddb [%d] "
cd09b2c3 1269 "initiate relogin after"
afaf5a2d
DS
1270 " %d seconds\n",
1271 ha->host_no, ddb_entry->bus,
1272 ddb_entry->target,
1273 ddb_entry->fw_ddb_index,
1274 ddb_entry->default_time2wait + 4)
1275 );
1276
1277 atomic_set(&ddb_entry->retry_relogin_timer,
1278 ddb_entry->default_time2wait + 4);
1279 }
1280 }
1281 }
1282
f4f5df23
VC
1283 if (!is_qla8022(ha)) {
1284 /* Check for heartbeat interval. */
1285 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
1286 ha->heartbeat_interval != 0) {
1287 ha->seconds_since_last_heartbeat++;
1288 if (ha->seconds_since_last_heartbeat >
1289 ha->heartbeat_interval + 2)
1290 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1291 }
afaf5a2d
DS
1292 }
1293
afaf5a2d 1294 /* Wakeup the dpc routine for this adapter, if needed. */
1b46807e 1295 if (start_dpc ||
afaf5a2d
DS
1296 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1297 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
1298 test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
f4f5df23 1299 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
afaf5a2d
DS
1300 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1301 test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
065aa1b4 1302 test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
f4f5df23
VC
1303 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
1304 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
1b46807e 1305 test_bit(DPC_AEN, &ha->dpc_flags)) {
afaf5a2d
DS
1306 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
1307 " - dpc flags = 0x%lx\n",
1308 ha->host_no, __func__, ha->dpc_flags));
f4f5df23 1309 qla4xxx_wake_dpc(ha);
afaf5a2d
DS
1310 }
1311
1312 /* Reschedule timer thread to call us back in one second */
1313 mod_timer(&ha->timer, jiffies + HZ);
1314
1315 DEBUG2(ha->seconds_since_last_intr++);
1316}
1317
1318/**
1319 * qla4xxx_cmd_wait - waits for all outstanding commands to complete
1320 * @ha: Pointer to host adapter structure.
1321 *
1322 * This routine stalls the driver until all outstanding commands are returned.
1323 * Caller must release the Hardware Lock prior to calling this routine.
1324 **/
1325static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
1326{
1327 uint32_t index = 0;
afaf5a2d
DS
1328 unsigned long flags;
1329 struct scsi_cmnd *cmd;
afaf5a2d 1330
f4f5df23
VC
1331 unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
1332
1333 DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
1334 "complete\n", WAIT_CMD_TOV));
1335
1336 while (!time_after_eq(jiffies, wtime)) {
afaf5a2d
DS
1337 spin_lock_irqsave(&ha->hardware_lock, flags);
1338 /* Find a command that hasn't completed. */
1339 for (index = 0; index < ha->host->can_queue; index++) {
1340 cmd = scsi_host_find_tag(ha->host, index);
a1e0063d
MC
1341 /*
1342 * We cannot just check if the index is valid,
1343 * becase if we are run from the scsi eh, then
1344 * the scsi/block layer is going to prevent
1345 * the tag from being released.
1346 */
1347 if (cmd != NULL && CMD_SP(cmd))
afaf5a2d
DS
1348 break;
1349 }
1350 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1351
1352 /* If No Commands are pending, wait is complete */
f4f5df23
VC
1353 if (index == ha->host->can_queue)
1354 return QLA_SUCCESS;
afaf5a2d 1355
f4f5df23
VC
1356 msleep(1000);
1357 }
1358 /* If we timed out on waiting for commands to come back
1359 * return ERROR. */
1360 return QLA_ERROR;
afaf5a2d
DS
1361}
1362
f4f5df23 1363int qla4xxx_hw_reset(struct scsi_qla_host *ha)
afaf5a2d 1364{
afaf5a2d 1365 uint32_t ctrl_status;
477ffb9d
DS
1366 unsigned long flags = 0;
1367
1368 DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
afaf5a2d 1369
f4f5df23
VC
1370 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
1371 return QLA_ERROR;
1372
afaf5a2d
DS
1373 spin_lock_irqsave(&ha->hardware_lock, flags);
1374
1375 /*
1376 * If the SCSI Reset Interrupt bit is set, clear it.
1377 * Otherwise, the Soft Reset won't work.
1378 */
1379 ctrl_status = readw(&ha->reg->ctrl_status);
1380 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
1381 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
1382
1383 /* Issue Soft Reset */
1384 writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
1385 readl(&ha->reg->ctrl_status);
1386
1387 spin_unlock_irqrestore(&ha->hardware_lock, flags);
f4f5df23 1388 return QLA_SUCCESS;
477ffb9d
DS
1389}
1390
1391/**
1392 * qla4xxx_soft_reset - performs soft reset.
1393 * @ha: Pointer to host adapter structure.
1394 **/
1395int qla4xxx_soft_reset(struct scsi_qla_host *ha)
1396{
1397 uint32_t max_wait_time;
1398 unsigned long flags = 0;
f931c534 1399 int status;
477ffb9d
DS
1400 uint32_t ctrl_status;
1401
f931c534
VC
1402 status = qla4xxx_hw_reset(ha);
1403 if (status != QLA_SUCCESS)
1404 return status;
afaf5a2d 1405
f931c534 1406 status = QLA_ERROR;
afaf5a2d
DS
1407 /* Wait until the Network Reset Intr bit is cleared */
1408 max_wait_time = RESET_INTR_TOV;
1409 do {
1410 spin_lock_irqsave(&ha->hardware_lock, flags);
1411 ctrl_status = readw(&ha->reg->ctrl_status);
1412 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1413
1414 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
1415 break;
1416
1417 msleep(1000);
1418 } while ((--max_wait_time));
1419
1420 if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
1421 DEBUG2(printk(KERN_WARNING
1422 "scsi%ld: Network Reset Intr not cleared by "
1423 "Network function, clearing it now!\n",
1424 ha->host_no));
1425 spin_lock_irqsave(&ha->hardware_lock, flags);
1426 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
1427 readl(&ha->reg->ctrl_status);
1428 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1429 }
1430
1431 /* Wait until the firmware tells us the Soft Reset is done */
1432 max_wait_time = SOFT_RESET_TOV;
1433 do {
1434 spin_lock_irqsave(&ha->hardware_lock, flags);
1435 ctrl_status = readw(&ha->reg->ctrl_status);
1436 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1437
1438 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
1439 status = QLA_SUCCESS;
1440 break;
1441 }
1442
1443 msleep(1000);
1444 } while ((--max_wait_time));
1445
1446 /*
1447 * Also, make sure that the SCSI Reset Interrupt bit has been cleared
1448 * after the soft reset has taken place.
1449 */
1450 spin_lock_irqsave(&ha->hardware_lock, flags);
1451 ctrl_status = readw(&ha->reg->ctrl_status);
1452 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
1453 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
1454 readl(&ha->reg->ctrl_status);
1455 }
1456 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1457
1458 /* If soft reset fails then most probably the bios on other
1459 * function is also enabled.
1460 * Since the initialization is sequential the other fn
1461 * wont be able to acknowledge the soft reset.
1462 * Issue a force soft reset to workaround this scenario.
1463 */
1464 if (max_wait_time == 0) {
1465 /* Issue Force Soft Reset */
1466 spin_lock_irqsave(&ha->hardware_lock, flags);
1467 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
1468 readl(&ha->reg->ctrl_status);
1469 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1470 /* Wait until the firmware tells us the Soft Reset is done */
1471 max_wait_time = SOFT_RESET_TOV;
1472 do {
1473 spin_lock_irqsave(&ha->hardware_lock, flags);
1474 ctrl_status = readw(&ha->reg->ctrl_status);
1475 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1476
1477 if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
1478 status = QLA_SUCCESS;
1479 break;
1480 }
1481
1482 msleep(1000);
1483 } while ((--max_wait_time));
1484 }
1485
1486 return status;
1487}
1488
afaf5a2d 1489/**
f4f5df23 1490 * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
afaf5a2d 1491 * @ha: Pointer to host adapter structure.
f4f5df23 1492 * @res: returned scsi status
afaf5a2d
DS
1493 *
1494 * This routine is called just prior to a HARD RESET to return all
1495 * outstanding commands back to the Operating System.
1496 * Caller should make sure that the following locks are released
1497 * before this calling routine: Hardware lock, and io_request_lock.
1498 **/
f4f5df23 1499static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
afaf5a2d
DS
1500{
1501 struct srb *srb;
1502 int i;
1503 unsigned long flags;
1504
1505 spin_lock_irqsave(&ha->hardware_lock, flags);
1506 for (i = 0; i < ha->host->can_queue; i++) {
1507 srb = qla4xxx_del_from_active_array(ha, i);
1508 if (srb != NULL) {
f4f5df23 1509 srb->cmd->result = res;
09a0f719 1510 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
afaf5a2d
DS
1511 }
1512 }
1513 spin_unlock_irqrestore(&ha->hardware_lock, flags);
afaf5a2d
DS
1514}
1515
f4f5df23
VC
1516void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
1517{
1518 clear_bit(AF_ONLINE, &ha->flags);
1519
1520 /* Disable the board */
1521 ql4_printk(KERN_INFO, ha, "Disabling the board\n");
f4f5df23
VC
1522
1523 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
1524 qla4xxx_mark_all_devices_missing(ha);
1525 clear_bit(AF_INIT_DONE, &ha->flags);
1526}
1527
afaf5a2d
DS
1528/**
1529 * qla4xxx_recover_adapter - recovers adapter after a fatal error
1530 * @ha: Pointer to host adapter structure.
afaf5a2d 1531 **/
f4f5df23 1532static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
afaf5a2d 1533{
f4f5df23
VC
1534 int status = QLA_ERROR;
1535 uint8_t reset_chip = 0;
afaf5a2d
DS
1536
1537 /* Stall incoming I/O until we are done */
f4f5df23 1538 scsi_block_requests(ha->host);
afaf5a2d 1539 clear_bit(AF_ONLINE, &ha->flags);
50a29aec 1540
f4f5df23 1541 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
afaf5a2d 1542
f4f5df23 1543 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
afaf5a2d 1544
f4f5df23
VC
1545 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
1546 reset_chip = 1;
afaf5a2d 1547
f4f5df23
VC
1548 /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
1549 * do not reset adapter, jump to initialize_adapter */
1550 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1551 status = QLA_SUCCESS;
1552 goto recover_ha_init_adapter;
1553 }
afaf5a2d 1554
f4f5df23
VC
1555 /* For the ISP-82xx adapter, issue a stop_firmware if invoked
1556 * from eh_host_reset or ioctl module */
1557 if (is_qla8022(ha) && !reset_chip &&
1558 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
1559
1560 DEBUG2(ql4_printk(KERN_INFO, ha,
1561 "scsi%ld: %s - Performing stop_firmware...\n",
1562 ha->host_no, __func__));
1563 status = ha->isp_ops->reset_firmware(ha);
1564 if (status == QLA_SUCCESS) {
2bd1e2be
NJ
1565 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
1566 qla4xxx_cmd_wait(ha);
f4f5df23
VC
1567 ha->isp_ops->disable_intrs(ha);
1568 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1569 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1570 } else {
1571 /* If the stop_firmware fails then
1572 * reset the entire chip */
1573 reset_chip = 1;
1574 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1575 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1576 }
1577 }
dca05c4c 1578
f4f5df23
VC
1579 /* Issue full chip reset if recovering from a catastrophic error,
1580 * or if stop_firmware fails for ISP-82xx.
1581 * This is the default case for ISP-4xxx */
1582 if (!is_qla8022(ha) || reset_chip) {
2bd1e2be
NJ
1583 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
1584 qla4xxx_cmd_wait(ha);
f4f5df23
VC
1585 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1586 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1587 DEBUG2(ql4_printk(KERN_INFO, ha,
1588 "scsi%ld: %s - Performing chip reset..\n",
1589 ha->host_no, __func__));
1590 status = ha->isp_ops->reset_chip(ha);
1591 }
afaf5a2d
DS
1592
1593 /* Flush any pending ddb changed AENs */
1594 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1595
f4f5df23
VC
1596recover_ha_init_adapter:
1597 /* Upon successful firmware/chip reset, re-initialize the adapter */
afaf5a2d 1598 if (status == QLA_SUCCESS) {
f4f5df23
VC
1599 /* For ISP-4xxx, force function 1 to always initialize
1600 * before function 3 to prevent both funcions from
1601 * stepping on top of the other */
1602 if (!is_qla8022(ha) && (ha->mac_index == 3))
1603 ssleep(6);
1604
1605 /* NOTE: AF_ONLINE flag set upon successful completion of
1606 * qla4xxx_initialize_adapter */
1607 status = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
afaf5a2d
DS
1608 }
1609
f4f5df23
VC
1610 /* Retry failed adapter initialization, if necessary
1611 * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
1612 * case to prevent ping-pong resets between functions */
1613 if (!test_bit(AF_ONLINE, &ha->flags) &&
1614 !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
afaf5a2d 1615 /* Adapter initialization failed, see if we can retry
f4f5df23
VC
1616 * resetting the ha.
1617 * Since we don't want to block the DPC for too long
1618 * with multiple resets in the same thread,
1619 * utilize DPC to retry */
afaf5a2d
DS
1620 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
1621 ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
1622 DEBUG2(printk("scsi%ld: recover adapter - retrying "
1623 "(%d) more times\n", ha->host_no,
1624 ha->retry_reset_ha_cnt));
1625 set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1626 status = QLA_ERROR;
1627 } else {
1628 if (ha->retry_reset_ha_cnt > 0) {
1629 /* Schedule another Reset HA--DPC will retry */
1630 ha->retry_reset_ha_cnt--;
1631 DEBUG2(printk("scsi%ld: recover adapter - "
1632 "retry remaining %d\n",
1633 ha->host_no,
1634 ha->retry_reset_ha_cnt));
1635 status = QLA_ERROR;
1636 }
1637
1638 if (ha->retry_reset_ha_cnt == 0) {
1639 /* Recover adapter retries have been exhausted.
1640 * Adapter DEAD */
1641 DEBUG2(printk("scsi%ld: recover adapter "
1642 "failed - board disabled\n",
1643 ha->host_no));
f4f5df23 1644 qla4xxx_dead_adapter_cleanup(ha);
afaf5a2d
DS
1645 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1646 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
f4f5df23 1647 clear_bit(DPC_RESET_HA_FW_CONTEXT,
afaf5a2d
DS
1648 &ha->dpc_flags);
1649 status = QLA_ERROR;
1650 }
1651 }
1652 } else {
1653 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
f4f5df23 1654 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
afaf5a2d
DS
1655 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1656 }
1657
1658 ha->adapter_error_count++;
1659
f4f5df23
VC
1660 if (test_bit(AF_ONLINE, &ha->flags))
1661 ha->isp_ops->enable_intrs(ha);
1662
1663 scsi_unblock_requests(ha->host);
1664
1665 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
1666 DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
25985edc 1667 status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
afaf5a2d 1668
afaf5a2d
DS
1669 return status;
1670}
1671
2d7924e6
VC
1672static void qla4xxx_relogin_all_devices(struct scsi_qla_host *ha)
1673{
1674 struct ddb_entry *ddb_entry, *dtemp;
1675
1676 list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
1677 if ((atomic_read(&ddb_entry->state) == DDB_STATE_MISSING) ||
1678 (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD)) {
1679 if (ddb_entry->fw_ddb_device_state ==
1680 DDB_DS_SESSION_ACTIVE) {
1681 atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
1682 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
1683 " marked ONLINE\n", ha->host_no, __func__,
1684 ddb_entry->fw_ddb_index);
1685
1686 iscsi_unblock_session(ddb_entry->sess);
1687 } else
1688 qla4xxx_relogin_device(ha, ddb_entry);
1689 }
1690 }
1691}
1692
f4f5df23
VC
1693void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
1694{
1b46807e 1695 if (ha->dpc_thread)
f4f5df23 1696 queue_work(ha->dpc_thread, &ha->dpc_work);
f4f5df23
VC
1697}
1698
afaf5a2d
DS
1699/**
1700 * qla4xxx_do_dpc - dpc routine
1701 * @data: in our case pointer to adapter structure
1702 *
1703 * This routine is a task that is schedule by the interrupt handler
1704 * to perform the background processing for interrupts. We put it
1705 * on a task queue that is consumed whenever the scheduler runs; that's
1706 * so you can do anything (i.e. put the process to sleep etc). In fact,
1707 * the mid-level tries to sleep when it reaches the driver threshold
1708 * "host->can_queue". This can cause a panic if we were in our interrupt code.
1709 **/
c4028958 1710static void qla4xxx_do_dpc(struct work_struct *work)
afaf5a2d 1711{
c4028958
DH
1712 struct scsi_qla_host *ha =
1713 container_of(work, struct scsi_qla_host, dpc_work);
afaf5a2d 1714 struct ddb_entry *ddb_entry, *dtemp;
477ffb9d 1715 int status = QLA_ERROR;
afaf5a2d 1716
f26b9044 1717 DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
f4f5df23
VC
1718 "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
1719 ha->host_no, __func__, ha->flags, ha->dpc_flags))
afaf5a2d
DS
1720
1721 /* Initialization not yet finished. Don't do anything yet. */
1722 if (!test_bit(AF_INIT_DONE, &ha->flags))
1b46807e 1723 return;
afaf5a2d 1724
2232be0d
LC
1725 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1726 DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
1727 ha->host_no, __func__, ha->flags));
1b46807e 1728 return;
2232be0d
LC
1729 }
1730
f4f5df23
VC
1731 if (is_qla8022(ha)) {
1732 if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
1733 qla4_8xxx_idc_lock(ha);
1734 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
1735 QLA82XX_DEV_FAILED);
1736 qla4_8xxx_idc_unlock(ha);
1737 ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
1738 qla4_8xxx_device_state_handler(ha);
1739 }
1740 if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
1741 qla4_8xxx_need_qsnt_handler(ha);
1742 }
1743 }
1744
1745 if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
1746 (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
afaf5a2d 1747 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
f4f5df23
VC
1748 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
1749 if (ql4xdontresethba) {
1750 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
1751 ha->host_no, __func__));
1752 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
1753 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1754 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1755 goto dpc_post_reset_ha;
1756 }
1757 if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
1758 test_bit(DPC_RESET_HA, &ha->dpc_flags))
1759 qla4xxx_recover_adapter(ha);
afaf5a2d 1760
477ffb9d 1761 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
afaf5a2d 1762 uint8_t wait_time = RESET_INTR_TOV;
afaf5a2d 1763
afaf5a2d
DS
1764 while ((readw(&ha->reg->ctrl_status) &
1765 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
1766 if (--wait_time == 0)
1767 break;
afaf5a2d 1768 msleep(1000);
afaf5a2d 1769 }
afaf5a2d
DS
1770 if (wait_time == 0)
1771 DEBUG2(printk("scsi%ld: %s: SR|FSR "
1772 "bit not cleared-- resetting\n",
1773 ha->host_no, __func__));
f4f5df23 1774 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
477ffb9d
DS
1775 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
1776 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
f4f5df23 1777 status = qla4xxx_recover_adapter(ha);
477ffb9d
DS
1778 }
1779 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1780 if (status == QLA_SUCCESS)
f4f5df23 1781 ha->isp_ops->enable_intrs(ha);
afaf5a2d
DS
1782 }
1783 }
1784
f4f5df23 1785dpc_post_reset_ha:
afaf5a2d
DS
1786 /* ---- process AEN? --- */
1787 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
1788 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
1789
1790 /* ---- Get DHCP IP Address? --- */
1791 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
1792 qla4xxx_get_dhcp_ip_address(ha);
1793
065aa1b4
VC
1794 /* ---- link change? --- */
1795 if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
1796 if (!test_bit(AF_LINK_UP, &ha->flags)) {
1797 /* ---- link down? --- */
2d7924e6 1798 qla4xxx_mark_all_devices_missing(ha);
065aa1b4
VC
1799 } else {
1800 /* ---- link up? --- *
1801 * F/W will auto login to all devices ONLY ONCE after
1802 * link up during driver initialization and runtime
1803 * fatal error recovery. Therefore, the driver must
1804 * manually relogin to devices when recovering from
1805 * connection failures, logouts, expired KATO, etc. */
1806
2d7924e6 1807 qla4xxx_relogin_all_devices(ha);
065aa1b4
VC
1808 }
1809 }
1810
afaf5a2d
DS
1811 /* ---- relogin device? --- */
1812 if (adapter_up(ha) &&
1813 test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
1814 list_for_each_entry_safe(ddb_entry, dtemp,
1815 &ha->ddb_list, list) {
1816 if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
1817 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
1818 qla4xxx_relogin_device(ha, ddb_entry);
1819
1820 /*
1821 * If mbx cmd times out there is no point
1822 * in continuing further.
1823 * With large no of targets this can hang
1824 * the system.
1825 */
1826 if (test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
1827 printk(KERN_WARNING "scsi%ld: %s: "
1828 "need to reset hba\n",
1829 ha->host_no, __func__);
1830 break;
1831 }
1832 }
1833 }
0753b487 1834
afaf5a2d
DS
1835}
1836
1837/**
1838 * qla4xxx_free_adapter - release the adapter
1839 * @ha: pointer to adapter structure
1840 **/
1841static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
1842{
1843
1844 if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
1845 /* Turn-off interrupts on the card. */
f4f5df23 1846 ha->isp_ops->disable_intrs(ha);
afaf5a2d
DS
1847 }
1848
f4f5df23
VC
1849 /* Remove timer thread, if present */
1850 if (ha->timer_active)
1851 qla4xxx_stop_timer(ha);
1852
afaf5a2d
DS
1853 /* Kill the kernel thread for this host */
1854 if (ha->dpc_thread)
1855 destroy_workqueue(ha->dpc_thread);
1856
f4f5df23
VC
1857 /* Put firmware in known state */
1858 ha->isp_ops->reset_firmware(ha);
afaf5a2d 1859
f4f5df23
VC
1860 if (is_qla8022(ha)) {
1861 qla4_8xxx_idc_lock(ha);
1862 qla4_8xxx_clear_drv_active(ha);
1863 qla4_8xxx_idc_unlock(ha);
1864 }
afaf5a2d 1865
afaf5a2d
DS
1866 /* Detach interrupts */
1867 if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
f4f5df23 1868 qla4xxx_free_irqs(ha);
afaf5a2d 1869
bee4fe8e
DS
1870 /* free extra memory */
1871 qla4xxx_mem_free(ha);
f4f5df23
VC
1872}
1873
1874int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
1875{
1876 int status = 0;
1877 uint8_t revision_id;
1878 unsigned long mem_base, mem_len, db_base, db_len;
1879 struct pci_dev *pdev = ha->pdev;
1880
1881 status = pci_request_regions(pdev, DRIVER_NAME);
1882 if (status) {
1883 printk(KERN_WARNING
1884 "scsi(%ld) Failed to reserve PIO regions (%s) "
1885 "status=%d\n", ha->host_no, pci_name(pdev), status);
1886 goto iospace_error_exit;
1887 }
1888
1889 pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
1890 DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
1891 __func__, revision_id));
1892 ha->revision_id = revision_id;
bee4fe8e 1893
f4f5df23
VC
1894 /* remap phys address */
1895 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
1896 mem_len = pci_resource_len(pdev, 0);
1897 DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
1898 __func__, mem_base, mem_len));
afaf5a2d 1899
f4f5df23
VC
1900 /* mapping of pcibase pointer */
1901 ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
1902 if (!ha->nx_pcibase) {
1903 printk(KERN_ERR
1904 "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
1905 pci_release_regions(ha->pdev);
1906 goto iospace_error_exit;
1907 }
1908
1909 /* Mapping of IO base pointer, door bell read and write pointer */
1910
1911 /* mapping of IO base pointer */
1912 ha->qla4_8xxx_reg =
1913 (struct device_reg_82xx __iomem *)((uint8_t *)ha->nx_pcibase +
1914 0xbc000 + (ha->pdev->devfn << 11));
1915
1916 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
1917 db_len = pci_resource_len(pdev, 4);
1918
2657c800
SS
1919 ha->nx_db_wr_ptr = (ha->pdev->devfn == 4 ? QLA82XX_CAM_RAM_DB1 :
1920 QLA82XX_CAM_RAM_DB2);
f4f5df23 1921
2657c800 1922 return 0;
f4f5df23
VC
1923iospace_error_exit:
1924 return -ENOMEM;
afaf5a2d
DS
1925}
1926
1927/***
1928 * qla4xxx_iospace_config - maps registers
1929 * @ha: pointer to adapter structure
1930 *
1931 * This routines maps HBA's registers from the pci address space
1932 * into the kernel virtual address space for memory mapped i/o.
1933 **/
f4f5df23 1934int qla4xxx_iospace_config(struct scsi_qla_host *ha)
afaf5a2d
DS
1935{
1936 unsigned long pio, pio_len, pio_flags;
1937 unsigned long mmio, mmio_len, mmio_flags;
1938
1939 pio = pci_resource_start(ha->pdev, 0);
1940 pio_len = pci_resource_len(ha->pdev, 0);
1941 pio_flags = pci_resource_flags(ha->pdev, 0);
1942 if (pio_flags & IORESOURCE_IO) {
1943 if (pio_len < MIN_IOBASE_LEN) {
c2660df3 1944 ql4_printk(KERN_WARNING, ha,
afaf5a2d
DS
1945 "Invalid PCI I/O region size\n");
1946 pio = 0;
1947 }
1948 } else {
c2660df3 1949 ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
afaf5a2d
DS
1950 pio = 0;
1951 }
1952
1953 /* Use MMIO operations for all accesses. */
1954 mmio = pci_resource_start(ha->pdev, 1);
1955 mmio_len = pci_resource_len(ha->pdev, 1);
1956 mmio_flags = pci_resource_flags(ha->pdev, 1);
1957
1958 if (!(mmio_flags & IORESOURCE_MEM)) {
c2660df3
VC
1959 ql4_printk(KERN_ERR, ha,
1960 "region #0 not an MMIO resource, aborting\n");
afaf5a2d
DS
1961
1962 goto iospace_error_exit;
1963 }
c2660df3 1964
afaf5a2d 1965 if (mmio_len < MIN_IOBASE_LEN) {
c2660df3
VC
1966 ql4_printk(KERN_ERR, ha,
1967 "Invalid PCI mem region size, aborting\n");
afaf5a2d
DS
1968 goto iospace_error_exit;
1969 }
1970
1971 if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
c2660df3
VC
1972 ql4_printk(KERN_WARNING, ha,
1973 "Failed to reserve PIO/MMIO regions\n");
afaf5a2d
DS
1974
1975 goto iospace_error_exit;
1976 }
1977
1978 ha->pio_address = pio;
1979 ha->pio_length = pio_len;
1980 ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
1981 if (!ha->reg) {
c2660df3
VC
1982 ql4_printk(KERN_ERR, ha,
1983 "cannot remap MMIO, aborting\n");
afaf5a2d
DS
1984
1985 goto iospace_error_exit;
1986 }
1987
1988 return 0;
1989
1990iospace_error_exit:
1991 return -ENOMEM;
1992}
1993
f4f5df23
VC
1994static struct isp_operations qla4xxx_isp_ops = {
1995 .iospace_config = qla4xxx_iospace_config,
1996 .pci_config = qla4xxx_pci_config,
1997 .disable_intrs = qla4xxx_disable_intrs,
1998 .enable_intrs = qla4xxx_enable_intrs,
1999 .start_firmware = qla4xxx_start_firmware,
2000 .intr_handler = qla4xxx_intr_handler,
2001 .interrupt_service_routine = qla4xxx_interrupt_service_routine,
2002 .reset_chip = qla4xxx_soft_reset,
2003 .reset_firmware = qla4xxx_hw_reset,
2004 .queue_iocb = qla4xxx_queue_iocb,
2005 .complete_iocb = qla4xxx_complete_iocb,
2006 .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
2007 .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
2008 .get_sys_info = qla4xxx_get_sys_info,
2009};
2010
2011static struct isp_operations qla4_8xxx_isp_ops = {
2012 .iospace_config = qla4_8xxx_iospace_config,
2013 .pci_config = qla4_8xxx_pci_config,
2014 .disable_intrs = qla4_8xxx_disable_intrs,
2015 .enable_intrs = qla4_8xxx_enable_intrs,
2016 .start_firmware = qla4_8xxx_load_risc,
2017 .intr_handler = qla4_8xxx_intr_handler,
2018 .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
2019 .reset_chip = qla4_8xxx_isp_reset,
2020 .reset_firmware = qla4_8xxx_stop_firmware,
2021 .queue_iocb = qla4_8xxx_queue_iocb,
2022 .complete_iocb = qla4_8xxx_complete_iocb,
2023 .rd_shdw_req_q_out = qla4_8xxx_rd_shdw_req_q_out,
2024 .rd_shdw_rsp_q_in = qla4_8xxx_rd_shdw_rsp_q_in,
2025 .get_sys_info = qla4_8xxx_get_sys_info,
2026};
2027
2028uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
2029{
2030 return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
2031}
2032
2033uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
2034{
2035 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
2036}
2037
2038uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
2039{
2040 return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
2041}
2042
2043uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
2044{
2045 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
2046}
2047
afaf5a2d
DS
2048/**
2049 * qla4xxx_probe_adapter - callback function to probe HBA
2050 * @pdev: pointer to pci_dev structure
2051 * @pci_device_id: pointer to pci_device entry
2052 *
2053 * This routine will probe for Qlogic 4xxx iSCSI host adapters.
2054 * It returns zero if successful. It also initializes all data necessary for
2055 * the driver.
2056 **/
2057static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
2058 const struct pci_device_id *ent)
2059{
2060 int ret = -ENODEV, status;
2061 struct Scsi_Host *host;
2062 struct scsi_qla_host *ha;
afaf5a2d
DS
2063 uint8_t init_retry_count = 0;
2064 char buf[34];
f4f5df23 2065 struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
f9880e76 2066 uint32_t dev_state;
afaf5a2d
DS
2067
2068 if (pci_enable_device(pdev))
2069 return -1;
2070
2071 host = scsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha));
2072 if (host == NULL) {
2073 printk(KERN_WARNING
2074 "qla4xxx: Couldn't allocate host from scsi layer!\n");
2075 goto probe_disable_device;
2076 }
2077
2078 /* Clear our data area */
2079 ha = (struct scsi_qla_host *) host->hostdata;
2080 memset(ha, 0, sizeof(*ha));
2081
2082 /* Save the information from PCI BIOS. */
2083 ha->pdev = pdev;
2084 ha->host = host;
2085 ha->host_no = host->host_no;
2086
2232be0d
LC
2087 pci_enable_pcie_error_reporting(pdev);
2088
f4f5df23
VC
2089 /* Setup Runtime configurable options */
2090 if (is_qla8022(ha)) {
2091 ha->isp_ops = &qla4_8xxx_isp_ops;
2092 rwlock_init(&ha->hw_lock);
2093 ha->qdr_sn_window = -1;
2094 ha->ddr_mn_window = -1;
2095 ha->curr_window = 255;
2096 ha->func_num = PCI_FUNC(ha->pdev->devfn);
2097 nx_legacy_intr = &legacy_intr[ha->func_num];
2098 ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
2099 ha->nx_legacy_intr.tgt_status_reg =
2100 nx_legacy_intr->tgt_status_reg;
2101 ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
2102 ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
2103 } else {
2104 ha->isp_ops = &qla4xxx_isp_ops;
2105 }
2106
2232be0d
LC
2107 /* Set EEH reset type to fundamental if required by hba */
2108 if (is_qla8022(ha))
2109 pdev->needs_freset = 1;
2110
afaf5a2d 2111 /* Configure PCI I/O space. */
f4f5df23 2112 ret = ha->isp_ops->iospace_config(ha);
afaf5a2d 2113 if (ret)
f4f5df23 2114 goto probe_failed_ioconfig;
afaf5a2d 2115
c2660df3 2116 ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
afaf5a2d
DS
2117 pdev->device, pdev->irq, ha->reg);
2118
2119 qla4xxx_config_dma_addressing(ha);
2120
2121 /* Initialize lists and spinlocks. */
2122 INIT_LIST_HEAD(&ha->ddb_list);
2123 INIT_LIST_HEAD(&ha->free_srb_q);
2124
2125 mutex_init(&ha->mbox_sem);
f4f5df23 2126 init_completion(&ha->mbx_intr_comp);
afaf5a2d
DS
2127
2128 spin_lock_init(&ha->hardware_lock);
afaf5a2d
DS
2129
2130 /* Allocate dma buffers */
2131 if (qla4xxx_mem_alloc(ha)) {
c2660df3
VC
2132 ql4_printk(KERN_WARNING, ha,
2133 "[ERROR] Failed to allocate memory for adapter\n");
afaf5a2d
DS
2134
2135 ret = -ENOMEM;
2136 goto probe_failed;
2137 }
2138
f4f5df23
VC
2139 if (is_qla8022(ha))
2140 (void) qla4_8xxx_get_flash_info(ha);
2141
afaf5a2d
DS
2142 /*
2143 * Initialize the Host adapter request/response queues and
2144 * firmware
2145 * NOTE: interrupts enabled upon successful completion
2146 */
2147 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
f4f5df23
VC
2148 while ((!test_bit(AF_ONLINE, &ha->flags)) &&
2149 init_retry_count++ < MAX_INIT_RETRIES) {
f9880e76
PM
2150
2151 if (is_qla8022(ha)) {
2152 qla4_8xxx_idc_lock(ha);
2153 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
2154 qla4_8xxx_idc_unlock(ha);
2155 if (dev_state == QLA82XX_DEV_FAILED) {
2156 ql4_printk(KERN_WARNING, ha, "%s: don't retry "
2157 "initialize adapter. H/W is in failed state\n",
2158 __func__);
2159 break;
2160 }
2161 }
afaf5a2d
DS
2162 DEBUG2(printk("scsi: %s: retrying adapter initialization "
2163 "(%d)\n", __func__, init_retry_count));
f4f5df23
VC
2164
2165 if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
2166 continue;
2167
afaf5a2d
DS
2168 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
2169 }
f4f5df23
VC
2170
2171 if (!test_bit(AF_ONLINE, &ha->flags)) {
c2660df3 2172 ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
afaf5a2d 2173
fe998527
LC
2174 if (is_qla8022(ha) && ql4xdontresethba) {
2175 /* Put the device in failed state. */
2176 DEBUG2(printk(KERN_ERR "HW STATE: FAILED\n"));
2177 qla4_8xxx_idc_lock(ha);
2178 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2179 QLA82XX_DEV_FAILED);
2180 qla4_8xxx_idc_unlock(ha);
2181 }
afaf5a2d
DS
2182 ret = -ENODEV;
2183 goto probe_failed;
2184 }
2185
2186 host->cmd_per_lun = 3;
2187 host->max_channel = 0;
2188 host->max_lun = MAX_LUNS - 1;
2189 host->max_id = MAX_TARGETS;
2190 host->max_cmd_len = IOCB_MAX_CDB_LEN;
2191 host->can_queue = MAX_SRBS ;
2192 host->transportt = qla4xxx_scsi_transport;
2193
2194 ret = scsi_init_shared_tag_map(host, MAX_SRBS);
2195 if (ret) {
c2660df3
VC
2196 ql4_printk(KERN_WARNING, ha,
2197 "scsi_init_shared_tag_map failed\n");
2198 goto probe_failed;
afaf5a2d
DS
2199 }
2200
2201 /* Startup the kernel thread for this host adapter. */
2202 DEBUG2(printk("scsi: %s: Starting kernel thread for "
2203 "qla4xxx_dpc\n", __func__));
2204 sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
2205 ha->dpc_thread = create_singlethread_workqueue(buf);
2206 if (!ha->dpc_thread) {
c2660df3 2207 ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
afaf5a2d
DS
2208 ret = -ENODEV;
2209 goto probe_failed;
2210 }
c4028958 2211 INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
afaf5a2d 2212
f4f5df23
VC
2213 /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
2214 * (which is called indirectly by qla4xxx_initialize_adapter),
2215 * so that irqs will be registered after crbinit but before
2216 * mbx_intr_enable.
2217 */
2218 if (!is_qla8022(ha)) {
2219 ret = qla4xxx_request_irqs(ha);
2220 if (ret) {
2221 ql4_printk(KERN_WARNING, ha, "Failed to reserve "
2222 "interrupt %d already in use.\n", pdev->irq);
2223 goto probe_failed;
2224 }
afaf5a2d 2225 }
afaf5a2d 2226
2232be0d 2227 pci_save_state(ha->pdev);
f4f5df23 2228 ha->isp_ops->enable_intrs(ha);
afaf5a2d
DS
2229
2230 /* Start timer thread. */
2231 qla4xxx_start_timer(ha, qla4xxx_timer, 1);
2232
2233 set_bit(AF_INIT_DONE, &ha->flags);
2234
2235 pci_set_drvdata(pdev, ha);
2236
2237 ret = scsi_add_host(host, &pdev->dev);
2238 if (ret)
2239 goto probe_failed;
2240
afaf5a2d
DS
2241 printk(KERN_INFO
2242 " QLogic iSCSI HBA Driver version: %s\n"
2243 " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
2244 qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
2245 ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
2246 ha->patch_number, ha->build_number);
921601b7 2247 scsi_scan_host(host);
ed1086e0
VC
2248
2249 qla4xxx_create_ifaces(ha);
afaf5a2d
DS
2250 return 0;
2251
afaf5a2d
DS
2252probe_failed:
2253 qla4xxx_free_adapter(ha);
f4f5df23
VC
2254
2255probe_failed_ioconfig:
2232be0d 2256 pci_disable_pcie_error_reporting(pdev);
afaf5a2d
DS
2257 scsi_host_put(ha->host);
2258
2259probe_disable_device:
2260 pci_disable_device(pdev);
2261
2262 return ret;
2263}
2264
7eece5a0
KH
2265/**
2266 * qla4xxx_prevent_other_port_reinit - prevent other port from re-initialize
2267 * @ha: pointer to adapter structure
2268 *
2269 * Mark the other ISP-4xxx port to indicate that the driver is being removed,
2270 * so that the other port will not re-initialize while in the process of
2271 * removing the ha due to driver unload or hba hotplug.
2272 **/
2273static void qla4xxx_prevent_other_port_reinit(struct scsi_qla_host *ha)
2274{
2275 struct scsi_qla_host *other_ha = NULL;
2276 struct pci_dev *other_pdev = NULL;
2277 int fn = ISP4XXX_PCI_FN_2;
2278
2279 /*iscsi function numbers for ISP4xxx is 1 and 3*/
2280 if (PCI_FUNC(ha->pdev->devfn) & BIT_1)
2281 fn = ISP4XXX_PCI_FN_1;
2282
2283 other_pdev =
2284 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
2285 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
2286 fn));
2287
2288 /* Get other_ha if other_pdev is valid and state is enable*/
2289 if (other_pdev) {
2290 if (atomic_read(&other_pdev->enable_cnt)) {
2291 other_ha = pci_get_drvdata(other_pdev);
2292 if (other_ha) {
2293 set_bit(AF_HA_REMOVAL, &other_ha->flags);
2294 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: "
2295 "Prevent %s reinit\n", __func__,
2296 dev_name(&other_ha->pdev->dev)));
2297 }
2298 }
2299 pci_dev_put(other_pdev);
2300 }
2301}
2302
afaf5a2d
DS
2303/**
2304 * qla4xxx_remove_adapter - calback function to remove adapter.
2305 * @pci_dev: PCI device pointer
2306 **/
2307static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
2308{
2309 struct scsi_qla_host *ha;
2310
2311 ha = pci_get_drvdata(pdev);
2312
7eece5a0
KH
2313 if (!is_qla8022(ha))
2314 qla4xxx_prevent_other_port_reinit(ha);
bee4fe8e 2315
afaf5a2d
DS
2316 /* remove devs from iscsi_sessions to scsi_devices */
2317 qla4xxx_free_ddb_list(ha);
2318
ed1086e0
VC
2319 /* destroy iface from sysfs */
2320 qla4xxx_destroy_ifaces(ha);
2321
afaf5a2d
DS
2322 scsi_remove_host(ha->host);
2323
2324 qla4xxx_free_adapter(ha);
2325
2326 scsi_host_put(ha->host);
2327
2232be0d 2328 pci_disable_pcie_error_reporting(pdev);
f4f5df23 2329 pci_disable_device(pdev);
afaf5a2d
DS
2330 pci_set_drvdata(pdev, NULL);
2331}
2332
2333/**
2334 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
2335 * @ha: HA context
2336 *
2337 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
2338 * supported addressing method.
2339 */
47975477 2340static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
afaf5a2d
DS
2341{
2342 int retval;
2343
2344 /* Update our PCI device dma_mask for full 64 bit mask */
6a35528a
YH
2345 if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
2346 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
afaf5a2d
DS
2347 dev_dbg(&ha->pdev->dev,
2348 "Failed to set 64 bit PCI consistent mask; "
2349 "using 32 bit.\n");
2350 retval = pci_set_consistent_dma_mask(ha->pdev,
284901a9 2351 DMA_BIT_MASK(32));
afaf5a2d
DS
2352 }
2353 } else
284901a9 2354 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
afaf5a2d
DS
2355}
2356
2357static int qla4xxx_slave_alloc(struct scsi_device *sdev)
2358{
2359 struct iscsi_cls_session *sess = starget_to_session(sdev->sdev_target);
2360 struct ddb_entry *ddb = sess->dd_data;
8bb4033d 2361 int queue_depth = QL4_DEF_QDEPTH;
afaf5a2d
DS
2362
2363 sdev->hostdata = ddb;
2364 sdev->tagged_supported = 1;
8bb4033d
VC
2365
2366 if (ql4xmaxqdepth != 0 && ql4xmaxqdepth <= 0xffffU)
2367 queue_depth = ql4xmaxqdepth;
2368
2369 scsi_activate_tcq(sdev, queue_depth);
afaf5a2d
DS
2370 return 0;
2371}
2372
2373static int qla4xxx_slave_configure(struct scsi_device *sdev)
2374{
2375 sdev->tagged_supported = 1;
2376 return 0;
2377}
2378
2379static void qla4xxx_slave_destroy(struct scsi_device *sdev)
2380{
2381 scsi_deactivate_tcq(sdev, 1);
2382}
2383
2384/**
2385 * qla4xxx_del_from_active_array - returns an active srb
2386 * @ha: Pointer to host adapter structure.
fd589a8f 2387 * @index: index into the active_array
afaf5a2d
DS
2388 *
2389 * This routine removes and returns the srb at the specified index
2390 **/
f4f5df23
VC
2391struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
2392 uint32_t index)
afaf5a2d
DS
2393{
2394 struct srb *srb = NULL;
5369887a 2395 struct scsi_cmnd *cmd = NULL;
afaf5a2d 2396
5369887a
VC
2397 cmd = scsi_host_find_tag(ha->host, index);
2398 if (!cmd)
afaf5a2d
DS
2399 return srb;
2400
5369887a
VC
2401 srb = (struct srb *)CMD_SP(cmd);
2402 if (!srb)
afaf5a2d
DS
2403 return srb;
2404
2405 /* update counters */
2406 if (srb->flags & SRB_DMA_VALID) {
2407 ha->req_q_count += srb->iocb_cnt;
2408 ha->iocb_cnt -= srb->iocb_cnt;
2409 if (srb->cmd)
5369887a
VC
2410 srb->cmd->host_scribble =
2411 (unsigned char *)(unsigned long) MAX_SRBS;
afaf5a2d
DS
2412 }
2413 return srb;
2414}
2415
afaf5a2d
DS
2416/**
2417 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
09a0f719 2418 * @ha: Pointer to host adapter structure.
afaf5a2d
DS
2419 * @cmd: Scsi Command to wait on.
2420 *
2421 * This routine waits for the command to be returned by the Firmware
2422 * for some max time.
2423 **/
2424static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
2425 struct scsi_cmnd *cmd)
2426{
2427 int done = 0;
2428 struct srb *rp;
2429 uint32_t max_wait_time = EH_WAIT_CMD_TOV;
2232be0d
LC
2430 int ret = SUCCESS;
2431
2432 /* Dont wait on command if PCI error is being handled
2433 * by PCI AER driver
2434 */
2435 if (unlikely(pci_channel_offline(ha->pdev)) ||
2436 (test_bit(AF_EEH_BUSY, &ha->flags))) {
2437 ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
2438 ha->host_no, __func__);
2439 return ret;
2440 }
afaf5a2d
DS
2441
2442 do {
2443 /* Checking to see if its returned to OS */
5369887a 2444 rp = (struct srb *) CMD_SP(cmd);
afaf5a2d
DS
2445 if (rp == NULL) {
2446 done++;
2447 break;
2448 }
2449
2450 msleep(2000);
2451 } while (max_wait_time--);
2452
2453 return done;
2454}
2455
2456/**
2457 * qla4xxx_wait_for_hba_online - waits for HBA to come online
2458 * @ha: Pointer to host adapter structure
2459 **/
2460static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
2461{
2462 unsigned long wait_online;
2463
f581a3f7 2464 wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
afaf5a2d
DS
2465 while (time_before(jiffies, wait_online)) {
2466
2467 if (adapter_up(ha))
2468 return QLA_SUCCESS;
afaf5a2d
DS
2469
2470 msleep(2000);
2471 }
2472
2473 return QLA_ERROR;
2474}
2475
2476/**
ce545039 2477 * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
fd589a8f 2478 * @ha: pointer to HBA
afaf5a2d
DS
2479 * @t: target id
2480 * @l: lun id
2481 *
2482 * This function waits for all outstanding commands to a lun to complete. It
2483 * returns 0 if all pending commands are returned and 1 otherwise.
2484 **/
ce545039
MC
2485static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
2486 struct scsi_target *stgt,
2487 struct scsi_device *sdev)
afaf5a2d
DS
2488{
2489 int cnt;
2490 int status = 0;
2491 struct scsi_cmnd *cmd;
2492
2493 /*
ce545039
MC
2494 * Waiting for all commands for the designated target or dev
2495 * in the active array
afaf5a2d
DS
2496 */
2497 for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
2498 cmd = scsi_host_find_tag(ha->host, cnt);
ce545039
MC
2499 if (cmd && stgt == scsi_target(cmd->device) &&
2500 (!sdev || sdev == cmd->device)) {
afaf5a2d
DS
2501 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
2502 status++;
2503 break;
2504 }
2505 }
2506 }
2507 return status;
2508}
2509
09a0f719
VC
2510/**
2511 * qla4xxx_eh_abort - callback for abort task.
2512 * @cmd: Pointer to Linux's SCSI command structure
2513 *
2514 * This routine is called by the Linux OS to abort the specified
2515 * command.
2516 **/
2517static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
2518{
2519 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2520 unsigned int id = cmd->device->id;
2521 unsigned int lun = cmd->device->lun;
92b3e5bb 2522 unsigned long flags;
09a0f719
VC
2523 struct srb *srb = NULL;
2524 int ret = SUCCESS;
2525 int wait = 0;
2526
c2660df3 2527 ql4_printk(KERN_INFO, ha,
5cd049a5
CH
2528 "scsi%ld:%d:%d: Abort command issued cmd=%p\n",
2529 ha->host_no, id, lun, cmd);
09a0f719 2530
92b3e5bb 2531 spin_lock_irqsave(&ha->hardware_lock, flags);
09a0f719 2532 srb = (struct srb *) CMD_SP(cmd);
92b3e5bb
MC
2533 if (!srb) {
2534 spin_unlock_irqrestore(&ha->hardware_lock, flags);
09a0f719 2535 return SUCCESS;
92b3e5bb 2536 }
09a0f719 2537 kref_get(&srb->srb_ref);
92b3e5bb 2538 spin_unlock_irqrestore(&ha->hardware_lock, flags);
09a0f719
VC
2539
2540 if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
2541 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
2542 ha->host_no, id, lun));
2543 ret = FAILED;
2544 } else {
2545 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
2546 ha->host_no, id, lun));
2547 wait = 1;
2548 }
2549
2550 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
2551
2552 /* Wait for command to complete */
2553 if (wait) {
2554 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
2555 DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
2556 ha->host_no, id, lun));
2557 ret = FAILED;
2558 }
2559 }
2560
c2660df3 2561 ql4_printk(KERN_INFO, ha,
09a0f719 2562 "scsi%ld:%d:%d: Abort command - %s\n",
25985edc 2563 ha->host_no, id, lun, (ret == SUCCESS) ? "succeeded" : "failed");
09a0f719
VC
2564
2565 return ret;
2566}
2567
afaf5a2d
DS
2568/**
2569 * qla4xxx_eh_device_reset - callback for target reset.
2570 * @cmd: Pointer to Linux's SCSI command structure
2571 *
2572 * This routine is called by the Linux OS to reset all luns on the
2573 * specified target.
2574 **/
2575static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
2576{
2577 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2578 struct ddb_entry *ddb_entry = cmd->device->hostdata;
afaf5a2d
DS
2579 int ret = FAILED, stat;
2580
612f7348 2581 if (!ddb_entry)
afaf5a2d
DS
2582 return ret;
2583
c01be6dc
MC
2584 ret = iscsi_block_scsi_eh(cmd);
2585 if (ret)
2586 return ret;
2587 ret = FAILED;
2588
c2660df3 2589 ql4_printk(KERN_INFO, ha,
afaf5a2d
DS
2590 "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
2591 cmd->device->channel, cmd->device->id, cmd->device->lun);
2592
2593 DEBUG2(printk(KERN_INFO
2594 "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
2595 "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
242f9dcb 2596 cmd, jiffies, cmd->request->timeout / HZ,
afaf5a2d
DS
2597 ha->dpc_flags, cmd->result, cmd->allowed));
2598
2599 /* FIXME: wait for hba to go online */
2600 stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
2601 if (stat != QLA_SUCCESS) {
c2660df3 2602 ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
afaf5a2d
DS
2603 goto eh_dev_reset_done;
2604 }
2605
ce545039
MC
2606 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2607 cmd->device)) {
c2660df3 2608 ql4_printk(KERN_INFO, ha,
ce545039
MC
2609 "DEVICE RESET FAILED - waiting for "
2610 "commands.\n");
2611 goto eh_dev_reset_done;
afaf5a2d
DS
2612 }
2613
9d562913
DS
2614 /* Send marker. */
2615 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2616 MM_LUN_RESET) != QLA_SUCCESS)
2617 goto eh_dev_reset_done;
2618
c2660df3 2619 ql4_printk(KERN_INFO, ha,
afaf5a2d
DS
2620 "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
2621 ha->host_no, cmd->device->channel, cmd->device->id,
2622 cmd->device->lun);
2623
2624 ret = SUCCESS;
2625
2626eh_dev_reset_done:
2627
2628 return ret;
2629}
2630
ce545039
MC
2631/**
2632 * qla4xxx_eh_target_reset - callback for target reset.
2633 * @cmd: Pointer to Linux's SCSI command structure
2634 *
2635 * This routine is called by the Linux OS to reset the target.
2636 **/
2637static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
2638{
2639 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2640 struct ddb_entry *ddb_entry = cmd->device->hostdata;
c01be6dc 2641 int stat, ret;
ce545039
MC
2642
2643 if (!ddb_entry)
2644 return FAILED;
2645
c01be6dc
MC
2646 ret = iscsi_block_scsi_eh(cmd);
2647 if (ret)
2648 return ret;
2649
ce545039
MC
2650 starget_printk(KERN_INFO, scsi_target(cmd->device),
2651 "WARM TARGET RESET ISSUED.\n");
2652
2653 DEBUG2(printk(KERN_INFO
2654 "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
2655 "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
242f9dcb 2656 ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
ce545039
MC
2657 ha->dpc_flags, cmd->result, cmd->allowed));
2658
2659 stat = qla4xxx_reset_target(ha, ddb_entry);
2660 if (stat != QLA_SUCCESS) {
2661 starget_printk(KERN_INFO, scsi_target(cmd->device),
2662 "WARM TARGET RESET FAILED.\n");
2663 return FAILED;
2664 }
2665
ce545039
MC
2666 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2667 NULL)) {
2668 starget_printk(KERN_INFO, scsi_target(cmd->device),
2669 "WARM TARGET DEVICE RESET FAILED - "
2670 "waiting for commands.\n");
2671 return FAILED;
2672 }
2673
9d562913
DS
2674 /* Send marker. */
2675 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2676 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
2677 starget_printk(KERN_INFO, scsi_target(cmd->device),
2678 "WARM TARGET DEVICE RESET FAILED - "
2679 "marker iocb failed.\n");
2680 return FAILED;
2681 }
2682
ce545039
MC
2683 starget_printk(KERN_INFO, scsi_target(cmd->device),
2684 "WARM TARGET RESET SUCCEEDED.\n");
2685 return SUCCESS;
2686}
2687
afaf5a2d
DS
2688/**
2689 * qla4xxx_eh_host_reset - kernel callback
2690 * @cmd: Pointer to Linux's SCSI command structure
2691 *
2692 * This routine is invoked by the Linux kernel to perform fatal error
2693 * recovery on the specified adapter.
2694 **/
2695static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
2696{
2697 int return_status = FAILED;
2698 struct scsi_qla_host *ha;
2699
2700 ha = (struct scsi_qla_host *) cmd->device->host->hostdata;
2701
f4f5df23
VC
2702 if (ql4xdontresethba) {
2703 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
2704 ha->host_no, __func__));
2705 return FAILED;
2706 }
2707
c2660df3 2708 ql4_printk(KERN_INFO, ha,
dca05c4c 2709 "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
afaf5a2d
DS
2710 cmd->device->channel, cmd->device->id, cmd->device->lun);
2711
2712 if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
2713 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
2714 "DEAD.\n", ha->host_no, cmd->device->channel,
2715 __func__));
2716
2717 return FAILED;
2718 }
2719
f4f5df23
VC
2720 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
2721 if (is_qla8022(ha))
2722 set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2723 else
2724 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2725 }
50a29aec 2726
f4f5df23 2727 if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
afaf5a2d 2728 return_status = SUCCESS;
afaf5a2d 2729
c2660df3 2730 ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
25985edc 2731 return_status == FAILED ? "FAILED" : "SUCCEEDED");
afaf5a2d
DS
2732
2733 return return_status;
2734}
2735
2232be0d
LC
2736/* PCI AER driver recovers from all correctable errors w/o
2737 * driver intervention. For uncorrectable errors PCI AER
2738 * driver calls the following device driver's callbacks
2739 *
2740 * - Fatal Errors - link_reset
2741 * - Non-Fatal Errors - driver's pci_error_detected() which
2742 * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
2743 *
2744 * PCI AER driver calls
2745 * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
2746 * returns RECOVERED or NEED_RESET if fw_hung
2747 * NEED_RESET - driver's slot_reset()
2748 * DISCONNECT - device is dead & cannot recover
2749 * RECOVERED - driver's pci_resume()
2750 */
2751static pci_ers_result_t
2752qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
2753{
2754 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2755
2756 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
2757 ha->host_no, __func__, state);
2758
2759 if (!is_aer_supported(ha))
2760 return PCI_ERS_RESULT_NONE;
2761
2762 switch (state) {
2763 case pci_channel_io_normal:
2764 clear_bit(AF_EEH_BUSY, &ha->flags);
2765 return PCI_ERS_RESULT_CAN_RECOVER;
2766 case pci_channel_io_frozen:
2767 set_bit(AF_EEH_BUSY, &ha->flags);
2768 qla4xxx_mailbox_premature_completion(ha);
2769 qla4xxx_free_irqs(ha);
2770 pci_disable_device(pdev);
7b3595df
VC
2771 /* Return back all IOs */
2772 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
2232be0d
LC
2773 return PCI_ERS_RESULT_NEED_RESET;
2774 case pci_channel_io_perm_failure:
2775 set_bit(AF_EEH_BUSY, &ha->flags);
2776 set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
2777 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
2778 return PCI_ERS_RESULT_DISCONNECT;
2779 }
2780 return PCI_ERS_RESULT_NEED_RESET;
2781}
2782
2783/**
2784 * qla4xxx_pci_mmio_enabled() gets called if
2785 * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
2786 * and read/write to the device still works.
2787 **/
2788static pci_ers_result_t
2789qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
2790{
2791 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2792
2793 if (!is_aer_supported(ha))
2794 return PCI_ERS_RESULT_NONE;
2795
7b3595df 2796 return PCI_ERS_RESULT_RECOVERED;
2232be0d
LC
2797}
2798
7b3595df 2799static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
2232be0d
LC
2800{
2801 uint32_t rval = QLA_ERROR;
7b3595df 2802 uint32_t ret = 0;
2232be0d
LC
2803 int fn;
2804 struct pci_dev *other_pdev = NULL;
2805
2806 ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
2807
2808 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2809
2810 if (test_bit(AF_ONLINE, &ha->flags)) {
2811 clear_bit(AF_ONLINE, &ha->flags);
2812 qla4xxx_mark_all_devices_missing(ha);
2813 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2232be0d
LC
2814 }
2815
2816 fn = PCI_FUNC(ha->pdev->devfn);
2817 while (fn > 0) {
2818 fn--;
2819 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
2820 "func %x\n", ha->host_no, __func__, fn);
2821 /* Get the pci device given the domain, bus,
2822 * slot/function number */
2823 other_pdev =
2824 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
2825 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
2826 fn));
2827
2828 if (!other_pdev)
2829 continue;
2830
2831 if (atomic_read(&other_pdev->enable_cnt)) {
2832 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
2833 "func in enabled state%x\n", ha->host_no,
2834 __func__, fn);
2835 pci_dev_put(other_pdev);
2836 break;
2837 }
2838 pci_dev_put(other_pdev);
2839 }
2840
2841 /* The first function on the card, the reset owner will
2842 * start & initialize the firmware. The other functions
2843 * on the card will reset the firmware context
2844 */
2845 if (!fn) {
2846 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
2847 "0x%x is the owner\n", ha->host_no, __func__,
2848 ha->pdev->devfn);
2849
2850 qla4_8xxx_idc_lock(ha);
2851 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2852 QLA82XX_DEV_COLD);
2853
2854 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
2855 QLA82XX_IDC_VERSION);
2856
2857 qla4_8xxx_idc_unlock(ha);
2858 clear_bit(AF_FW_RECOVERY, &ha->flags);
2859 rval = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
2860 qla4_8xxx_idc_lock(ha);
2861
2862 if (rval != QLA_SUCCESS) {
2863 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
2864 "FAILED\n", ha->host_no, __func__);
2865 qla4_8xxx_clear_drv_active(ha);
2866 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2867 QLA82XX_DEV_FAILED);
2868 } else {
2869 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
2870 "READY\n", ha->host_no, __func__);
2871 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2872 QLA82XX_DEV_READY);
2873 /* Clear driver state register */
2874 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
2875 qla4_8xxx_set_drv_active(ha);
7b3595df
VC
2876 ret = qla4xxx_request_irqs(ha);
2877 if (ret) {
2878 ql4_printk(KERN_WARNING, ha, "Failed to "
2879 "reserve interrupt %d already in use.\n",
2880 ha->pdev->irq);
2881 rval = QLA_ERROR;
2882 } else {
2883 ha->isp_ops->enable_intrs(ha);
2884 rval = QLA_SUCCESS;
2885 }
2232be0d
LC
2886 }
2887 qla4_8xxx_idc_unlock(ha);
2888 } else {
2889 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
2890 "the reset owner\n", ha->host_no, __func__,
2891 ha->pdev->devfn);
2892 if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
2893 QLA82XX_DEV_READY)) {
2894 clear_bit(AF_FW_RECOVERY, &ha->flags);
2895 rval = qla4xxx_initialize_adapter(ha,
2896 PRESERVE_DDB_LIST);
7b3595df
VC
2897 if (rval == QLA_SUCCESS) {
2898 ret = qla4xxx_request_irqs(ha);
2899 if (ret) {
2900 ql4_printk(KERN_WARNING, ha, "Failed to"
2901 " reserve interrupt %d already in"
2902 " use.\n", ha->pdev->irq);
2903 rval = QLA_ERROR;
2904 } else {
2905 ha->isp_ops->enable_intrs(ha);
2906 rval = QLA_SUCCESS;
2907 }
2908 }
2232be0d
LC
2909 qla4_8xxx_idc_lock(ha);
2910 qla4_8xxx_set_drv_active(ha);
2911 qla4_8xxx_idc_unlock(ha);
2912 }
2913 }
2914 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2915 return rval;
2916}
2917
2918static pci_ers_result_t
2919qla4xxx_pci_slot_reset(struct pci_dev *pdev)
2920{
2921 pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
2922 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2923 int rc;
2924
2925 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
2926 ha->host_no, __func__);
2927
2928 if (!is_aer_supported(ha))
2929 return PCI_ERS_RESULT_NONE;
2930
2931 /* Restore the saved state of PCIe device -
2932 * BAR registers, PCI Config space, PCIX, MSI,
2933 * IOV states
2934 */
2935 pci_restore_state(pdev);
2936
2937 /* pci_restore_state() clears the saved_state flag of the device
2938 * save restored state which resets saved_state flag
2939 */
2940 pci_save_state(pdev);
2941
2942 /* Initialize device or resume if in suspended state */
2943 rc = pci_enable_device(pdev);
2944 if (rc) {
25985edc 2945 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Can't re-enable "
2232be0d
LC
2946 "device after reset\n", ha->host_no, __func__);
2947 goto exit_slot_reset;
2948 }
2949
7b3595df 2950 ha->isp_ops->disable_intrs(ha);
2232be0d
LC
2951
2952 if (is_qla8022(ha)) {
2953 if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
2954 ret = PCI_ERS_RESULT_RECOVERED;
2955 goto exit_slot_reset;
2956 } else
2957 goto exit_slot_reset;
2958 }
2959
2960exit_slot_reset:
2961 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
2962 "device after reset\n", ha->host_no, __func__, ret);
2963 return ret;
2964}
2965
2966static void
2967qla4xxx_pci_resume(struct pci_dev *pdev)
2968{
2969 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2970 int ret;
2971
2972 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
2973 ha->host_no, __func__);
2974
2975 ret = qla4xxx_wait_for_hba_online(ha);
2976 if (ret != QLA_SUCCESS) {
2977 ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
2978 "resume I/O from slot/link_reset\n", ha->host_no,
2979 __func__);
2980 }
2981
2982 pci_cleanup_aer_uncorrect_error_status(pdev);
2983 clear_bit(AF_EEH_BUSY, &ha->flags);
2984}
2985
2986static struct pci_error_handlers qla4xxx_err_handler = {
2987 .error_detected = qla4xxx_pci_error_detected,
2988 .mmio_enabled = qla4xxx_pci_mmio_enabled,
2989 .slot_reset = qla4xxx_pci_slot_reset,
2990 .resume = qla4xxx_pci_resume,
2991};
2992
afaf5a2d
DS
2993static struct pci_device_id qla4xxx_pci_tbl[] = {
2994 {
2995 .vendor = PCI_VENDOR_ID_QLOGIC,
2996 .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
2997 .subvendor = PCI_ANY_ID,
2998 .subdevice = PCI_ANY_ID,
2999 },
3000 {
3001 .vendor = PCI_VENDOR_ID_QLOGIC,
3002 .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
3003 .subvendor = PCI_ANY_ID,
3004 .subdevice = PCI_ANY_ID,
3005 },
d915058f
DS
3006 {
3007 .vendor = PCI_VENDOR_ID_QLOGIC,
3008 .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
3009 .subvendor = PCI_ANY_ID,
3010 .subdevice = PCI_ANY_ID,
3011 },
f4f5df23
VC
3012 {
3013 .vendor = PCI_VENDOR_ID_QLOGIC,
3014 .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
3015 .subvendor = PCI_ANY_ID,
3016 .subdevice = PCI_ANY_ID,
3017 },
afaf5a2d
DS
3018 {0, 0},
3019};
3020MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
3021
47975477 3022static struct pci_driver qla4xxx_pci_driver = {
afaf5a2d
DS
3023 .name = DRIVER_NAME,
3024 .id_table = qla4xxx_pci_tbl,
3025 .probe = qla4xxx_probe_adapter,
3026 .remove = qla4xxx_remove_adapter,
2232be0d 3027 .err_handler = &qla4xxx_err_handler,
afaf5a2d
DS
3028};
3029
3030static int __init qla4xxx_module_init(void)
3031{
3032 int ret;
3033
3034 /* Allocate cache for SRBs. */
3035 srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
20c2df83 3036 SLAB_HWCACHE_ALIGN, NULL);
afaf5a2d
DS
3037 if (srb_cachep == NULL) {
3038 printk(KERN_ERR
3039 "%s: Unable to allocate SRB cache..."
3040 "Failing load!\n", DRIVER_NAME);
3041 ret = -ENOMEM;
3042 goto no_srp_cache;
3043 }
3044
3045 /* Derive version string. */
3046 strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
11010fec 3047 if (ql4xextended_error_logging)
afaf5a2d
DS
3048 strcat(qla4xxx_version_str, "-debug");
3049
3050 qla4xxx_scsi_transport =
3051 iscsi_register_transport(&qla4xxx_iscsi_transport);
3052 if (!qla4xxx_scsi_transport){
3053 ret = -ENODEV;
3054 goto release_srb_cache;
3055 }
3056
afaf5a2d
DS
3057 ret = pci_register_driver(&qla4xxx_pci_driver);
3058 if (ret)
3059 goto unregister_transport;
3060
3061 printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
3062 return 0;
5ae16db3 3063
afaf5a2d
DS
3064unregister_transport:
3065 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
3066release_srb_cache:
3067 kmem_cache_destroy(srb_cachep);
3068no_srp_cache:
3069 return ret;
3070}
3071
3072static void __exit qla4xxx_module_exit(void)
3073{
3074 pci_unregister_driver(&qla4xxx_pci_driver);
3075 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
3076 kmem_cache_destroy(srb_cachep);
3077}
3078
3079module_init(qla4xxx_module_init);
3080module_exit(qla4xxx_module_exit);
3081
3082MODULE_AUTHOR("QLogic Corporation");
3083MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
3084MODULE_LICENSE("GPL");
3085MODULE_VERSION(QLA4XXX_DRIVER_VERSION);