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