Merge branch 'omap_clock_fixes_3.2' of git://git.pwsan.com/linux-2.6 into fixes
[pandora-kernel.git] / drivers / scsi / qla4xxx / ql4_os.c
1 /*
2  * QLogic iSCSI HBA Driver
3  * Copyright (c)  2003-2010 QLogic Corporation
4  *
5  * See LICENSE.qla4xxx for copyright and licensing details.
6  */
7 #include <linux/moduleparam.h>
8 #include <linux/slab.h>
9 #include <linux/blkdev.h>
10 #include <linux/iscsi_boot_sysfs.h>
11
12 #include <scsi/scsi_tcq.h>
13 #include <scsi/scsicam.h>
14
15 #include "ql4_def.h"
16 #include "ql4_version.h"
17 #include "ql4_glbl.h"
18 #include "ql4_dbg.h"
19 #include "ql4_inline.h"
20
21 /*
22  * Driver version
23  */
24 static char qla4xxx_version_str[40];
25
26 /*
27  * SRB allocation cache
28  */
29 static struct kmem_cache *srb_cachep;
30
31 /*
32  * Module parameter information and variables
33  */
34 int ql4xdontresethba = 0;
35 module_param(ql4xdontresethba, int, S_IRUGO | S_IWUSR);
36 MODULE_PARM_DESC(ql4xdontresethba,
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");
40
41 int ql4xextended_error_logging = 0; /* 0 = off, 1 = log errors */
42 module_param(ql4xextended_error_logging, int, S_IRUGO | S_IWUSR);
43 MODULE_PARM_DESC(ql4xextended_error_logging,
44                  "Option to enable extended error logging, "
45                  "Default is 0 - no logging, 1 - debug logging");
46
47 int ql4xenablemsix = 1;
48 module_param(ql4xenablemsix, int, S_IRUGO|S_IWUSR);
49 MODULE_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.");
54
55 #define QL4_DEF_QDEPTH 32
56 static int ql4xmaxqdepth = QL4_DEF_QDEPTH;
57 module_param(ql4xmaxqdepth, int, S_IRUGO | S_IWUSR);
58 MODULE_PARM_DESC(ql4xmaxqdepth,
59                 "Maximum queue depth to report for target devices.\n"
60                 " Default: 32.");
61
62 static int ql4xsess_recovery_tmo = QL4_SESS_RECOVERY_TMO;
63 module_param(ql4xsess_recovery_tmo, int, S_IRUGO);
64 MODULE_PARM_DESC(ql4xsess_recovery_tmo,
65                 "Target Session Recovery Timeout.\n"
66                 " Default: 30 sec.");
67
68 static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha);
69 /*
70  * SCSI host template entry points
71  */
72 static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
73
74 /*
75  * iSCSI template entry points
76  */
77 static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
78                                   enum iscsi_param param, char *buf);
79 static int qla4xxx_host_get_param(struct Scsi_Host *shost,
80                                   enum iscsi_host_param param, char *buf);
81 static int qla4xxx_iface_set_param(struct Scsi_Host *shost, void *data,
82                                    uint32_t len);
83 static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
84                                    enum iscsi_param_type param_type,
85                                    int param, char *buf);
86 static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
87 static struct iscsi_endpoint *qla4xxx_ep_connect(struct Scsi_Host *shost,
88                                                  struct sockaddr *dst_addr,
89                                                  int non_blocking);
90 static int qla4xxx_ep_poll(struct iscsi_endpoint *ep, int timeout_ms);
91 static void qla4xxx_ep_disconnect(struct iscsi_endpoint *ep);
92 static int qla4xxx_get_ep_param(struct iscsi_endpoint *ep,
93                                 enum iscsi_param param, char *buf);
94 static int qla4xxx_conn_start(struct iscsi_cls_conn *conn);
95 static struct iscsi_cls_conn *
96 qla4xxx_conn_create(struct iscsi_cls_session *cls_sess, uint32_t conn_idx);
97 static 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);
100 static void qla4xxx_conn_destroy(struct iscsi_cls_conn *conn);
101 static struct iscsi_cls_session *
102 qla4xxx_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max,
103                         uint16_t qdepth, uint32_t initial_cmdsn);
104 static void qla4xxx_session_destroy(struct iscsi_cls_session *sess);
105 static void qla4xxx_task_work(struct work_struct *wdata);
106 static int qla4xxx_alloc_pdu(struct iscsi_task *, uint8_t);
107 static int qla4xxx_task_xmit(struct iscsi_task *);
108 static void qla4xxx_task_cleanup(struct iscsi_task *);
109 static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session);
110 static void qla4xxx_conn_get_stats(struct iscsi_cls_conn *cls_conn,
111                                    struct iscsi_stats *stats);
112 /*
113  * SCSI host template entry points
114  */
115 static int qla4xxx_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmd);
116 static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
117 static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
118 static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
119 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
120 static int qla4xxx_slave_alloc(struct scsi_device *device);
121 static int qla4xxx_slave_configure(struct scsi_device *device);
122 static void qla4xxx_slave_destroy(struct scsi_device *sdev);
123 static mode_t ql4_attr_is_visible(int param_type, int param);
124 static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type);
125
126 static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
127     QLA82XX_LEGACY_INTR_CONFIG;
128
129 static 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
135         .eh_abort_handler       = qla4xxx_eh_abort,
136         .eh_device_reset_handler = qla4xxx_eh_device_reset,
137         .eh_target_reset_handler = qla4xxx_eh_target_reset,
138         .eh_host_reset_handler  = qla4xxx_eh_host_reset,
139         .eh_timed_out           = qla4xxx_eh_cmd_timed_out,
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,
151         .shost_attrs            = qla4xxx_host_attrs,
152         .host_reset             = qla4xxx_host_reset,
153         .vendor_id              = SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_QLOGIC,
154 };
155
156 static struct iscsi_transport qla4xxx_iscsi_transport = {
157         .owner                  = THIS_MODULE,
158         .name                   = DRIVER_NAME,
159         .caps                   = CAP_TEXT_NEGO |
160                                   CAP_DATA_PATH_OFFLOAD | CAP_HDRDGST |
161                                   CAP_DATADGST | CAP_LOGIN_OFFLOAD |
162                                   CAP_MULTI_R2T,
163         .attr_is_visible        = ql4_attr_is_visible,
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,
172         .get_conn_param         = qla4xxx_conn_get_param,
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
184         .get_host_param         = qla4xxx_host_get_param,
185         .set_iface_param        = qla4xxx_iface_set_param,
186         .get_iface_param        = qla4xxx_get_iface_param,
187         .bsg_request            = qla4xxx_bsg_request,
188 };
189
190 static struct scsi_transport_template *qla4xxx_scsi_transport;
191
192 static mode_t ql4_attr_is_visible(int param_type, int param)
193 {
194         switch (param_type) {
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                 }
204         case ISCSI_PARAM:
205                 switch (param) {
206                 case ISCSI_PARAM_PERSISTENT_ADDRESS:
207                 case ISCSI_PARAM_PERSISTENT_PORT:
208                 case ISCSI_PARAM_CONN_ADDRESS:
209                 case ISCSI_PARAM_CONN_PORT:
210                 case ISCSI_PARAM_TARGET_NAME:
211                 case ISCSI_PARAM_TPGT:
212                 case ISCSI_PARAM_TARGET_ALIAS:
213                 case ISCSI_PARAM_MAX_BURST:
214                 case ISCSI_PARAM_MAX_R2T:
215                 case ISCSI_PARAM_FIRST_BURST:
216                 case ISCSI_PARAM_MAX_RECV_DLENGTH:
217                 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
218                 case ISCSI_PARAM_IFACE_NAME:
219                         return S_IRUGO;
220                 default:
221                         return 0;
222                 }
223         case ISCSI_NET_PARAM:
224                 switch (param) {
225                 case ISCSI_NET_PARAM_IPV4_ADDR:
226                 case ISCSI_NET_PARAM_IPV4_SUBNET:
227                 case ISCSI_NET_PARAM_IPV4_GW:
228                 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
229                 case ISCSI_NET_PARAM_IFACE_ENABLE:
230                 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
231                 case ISCSI_NET_PARAM_IPV6_ADDR:
232                 case ISCSI_NET_PARAM_IPV6_ROUTER:
233                 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
234                 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
235                 case ISCSI_NET_PARAM_VLAN_ID:
236                 case ISCSI_NET_PARAM_VLAN_PRIORITY:
237                 case ISCSI_NET_PARAM_VLAN_ENABLED:
238                 case ISCSI_NET_PARAM_MTU:
239                 case ISCSI_NET_PARAM_PORT:
240                         return S_IRUGO;
241                 default:
242                         return 0;
243                 }
244         }
245
246         return 0;
247 }
248
249 static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
250                                    enum iscsi_param_type param_type,
251                                    int param, char *buf)
252 {
253         struct Scsi_Host *shost = iscsi_iface_to_shost(iface);
254         struct scsi_qla_host *ha = to_qla_host(shost);
255         int len = -ENOSYS;
256
257         if (param_type != ISCSI_NET_PARAM)
258                 return -ENOSYS;
259
260         switch (param) {
261         case ISCSI_NET_PARAM_IPV4_ADDR:
262                 len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
263                 break;
264         case ISCSI_NET_PARAM_IPV4_SUBNET:
265                 len = sprintf(buf, "%pI4\n", &ha->ip_config.subnet_mask);
266                 break;
267         case ISCSI_NET_PARAM_IPV4_GW:
268                 len = sprintf(buf, "%pI4\n", &ha->ip_config.gateway);
269                 break;
270         case ISCSI_NET_PARAM_IFACE_ENABLE:
271                 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
272                         len = sprintf(buf, "%s\n",
273                                       (ha->ip_config.ipv4_options &
274                                        IPOPT_IPV4_PROTOCOL_ENABLE) ?
275                                       "enabled" : "disabled");
276                 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
277                         len = sprintf(buf, "%s\n",
278                                       (ha->ip_config.ipv6_options &
279                                        IPV6_OPT_IPV6_PROTOCOL_ENABLE) ?
280                                        "enabled" : "disabled");
281                 break;
282         case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
283                 len = sprintf(buf, "%s\n",
284                               (ha->ip_config.tcp_options & TCPOPT_DHCP_ENABLE) ?
285                               "dhcp" : "static");
286                 break;
287         case ISCSI_NET_PARAM_IPV6_ADDR:
288                 if (iface->iface_num == 0)
289                         len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr0);
290                 if (iface->iface_num == 1)
291                         len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr1);
292                 break;
293         case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
294                 len = sprintf(buf, "%pI6\n",
295                               &ha->ip_config.ipv6_link_local_addr);
296                 break;
297         case ISCSI_NET_PARAM_IPV6_ROUTER:
298                 len = sprintf(buf, "%pI6\n",
299                               &ha->ip_config.ipv6_default_router_addr);
300                 break;
301         case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
302                 len = sprintf(buf, "%s\n",
303                               (ha->ip_config.ipv6_addl_options &
304                                IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) ?
305                                "nd" : "static");
306                 break;
307         case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
308                 len = sprintf(buf, "%s\n",
309                               (ha->ip_config.ipv6_addl_options &
310                                IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR) ?
311                                "auto" : "static");
312                 break;
313         case ISCSI_NET_PARAM_VLAN_ID:
314                 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
315                         len = sprintf(buf, "%d\n",
316                                       (ha->ip_config.ipv4_vlan_tag &
317                                        ISCSI_MAX_VLAN_ID));
318                 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
319                         len = sprintf(buf, "%d\n",
320                                       (ha->ip_config.ipv6_vlan_tag &
321                                        ISCSI_MAX_VLAN_ID));
322                 break;
323         case ISCSI_NET_PARAM_VLAN_PRIORITY:
324                 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
325                         len = sprintf(buf, "%d\n",
326                                       ((ha->ip_config.ipv4_vlan_tag >> 13) &
327                                         ISCSI_MAX_VLAN_PRIORITY));
328                 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
329                         len = sprintf(buf, "%d\n",
330                                       ((ha->ip_config.ipv6_vlan_tag >> 13) &
331                                         ISCSI_MAX_VLAN_PRIORITY));
332                 break;
333         case ISCSI_NET_PARAM_VLAN_ENABLED:
334                 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
335                         len = sprintf(buf, "%s\n",
336                                       (ha->ip_config.ipv4_options &
337                                        IPOPT_VLAN_TAGGING_ENABLE) ?
338                                        "enabled" : "disabled");
339                 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
340                         len = sprintf(buf, "%s\n",
341                                       (ha->ip_config.ipv6_options &
342                                        IPV6_OPT_VLAN_TAGGING_ENABLE) ?
343                                        "enabled" : "disabled");
344                 break;
345         case ISCSI_NET_PARAM_MTU:
346                 len = sprintf(buf, "%d\n", ha->ip_config.eth_mtu_size);
347                 break;
348         case ISCSI_NET_PARAM_PORT:
349                 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
350                         len = sprintf(buf, "%d\n", ha->ip_config.ipv4_port);
351                 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
352                         len = sprintf(buf, "%d\n", ha->ip_config.ipv6_port);
353                 break;
354         default:
355                 len = -ENOSYS;
356         }
357
358         return len;
359 }
360
361 static struct iscsi_endpoint *
362 qla4xxx_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
363                    int non_blocking)
364 {
365         int ret;
366         struct iscsi_endpoint *ep;
367         struct qla_endpoint *qla_ep;
368         struct scsi_qla_host *ha;
369         struct sockaddr_in *addr;
370         struct sockaddr_in6 *addr6;
371
372         DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
373         if (!shost) {
374                 ret = -ENXIO;
375                 printk(KERN_ERR "%s: shost is NULL\n",
376                        __func__);
377                 return ERR_PTR(ret);
378         }
379
380         ha = iscsi_host_priv(shost);
381
382         ep = iscsi_create_endpoint(sizeof(struct qla_endpoint));
383         if (!ep) {
384                 ret = -ENOMEM;
385                 return ERR_PTR(ret);
386         }
387
388         qla_ep = ep->dd_data;
389         memset(qla_ep, 0, sizeof(struct qla_endpoint));
390         if (dst_addr->sa_family == AF_INET) {
391                 memcpy(&qla_ep->dst_addr, dst_addr, sizeof(struct sockaddr_in));
392                 addr = (struct sockaddr_in *)&qla_ep->dst_addr;
393                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI4\n", __func__,
394                                   (char *)&addr->sin_addr));
395         } else if (dst_addr->sa_family == AF_INET6) {
396                 memcpy(&qla_ep->dst_addr, dst_addr,
397                        sizeof(struct sockaddr_in6));
398                 addr6 = (struct sockaddr_in6 *)&qla_ep->dst_addr;
399                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI6\n", __func__,
400                                   (char *)&addr6->sin6_addr));
401         }
402
403         qla_ep->host = shost;
404
405         return ep;
406 }
407
408 static int qla4xxx_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
409 {
410         struct qla_endpoint *qla_ep;
411         struct scsi_qla_host *ha;
412         int ret = 0;
413
414         DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
415         qla_ep = ep->dd_data;
416         ha = to_qla_host(qla_ep->host);
417
418         if (adapter_up(ha))
419                 ret = 1;
420
421         return ret;
422 }
423
424 static void qla4xxx_ep_disconnect(struct iscsi_endpoint *ep)
425 {
426         DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
427         iscsi_destroy_endpoint(ep);
428 }
429
430 static int qla4xxx_get_ep_param(struct iscsi_endpoint *ep,
431                                 enum iscsi_param param,
432                                 char *buf)
433 {
434         struct qla_endpoint *qla_ep = ep->dd_data;
435         struct sockaddr *dst_addr;
436
437         DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
438
439         switch (param) {
440         case ISCSI_PARAM_CONN_PORT:
441         case ISCSI_PARAM_CONN_ADDRESS:
442                 if (!qla_ep)
443                         return -ENOTCONN;
444
445                 dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
446                 if (!dst_addr)
447                         return -ENOTCONN;
448
449                 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
450                                                  &qla_ep->dst_addr, param, buf);
451         default:
452                 return -ENOSYS;
453         }
454 }
455
456 static void qla4xxx_conn_get_stats(struct iscsi_cls_conn *cls_conn,
457                                    struct iscsi_stats *stats)
458 {
459         struct iscsi_session *sess;
460         struct iscsi_cls_session *cls_sess;
461         struct ddb_entry *ddb_entry;
462         struct scsi_qla_host *ha;
463         struct ql_iscsi_stats *ql_iscsi_stats;
464         int stats_size;
465         int ret;
466         dma_addr_t iscsi_stats_dma;
467
468         DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
469
470         cls_sess = iscsi_conn_to_session(cls_conn);
471         sess = cls_sess->dd_data;
472         ddb_entry = sess->dd_data;
473         ha = ddb_entry->ha;
474
475         stats_size = PAGE_ALIGN(sizeof(struct ql_iscsi_stats));
476         /* Allocate memory */
477         ql_iscsi_stats = dma_alloc_coherent(&ha->pdev->dev, stats_size,
478                                             &iscsi_stats_dma, GFP_KERNEL);
479         if (!ql_iscsi_stats) {
480                 ql4_printk(KERN_ERR, ha,
481                            "Unable to allocate memory for iscsi stats\n");
482                 goto exit_get_stats;
483         }
484
485         ret =  qla4xxx_get_mgmt_data(ha, ddb_entry->fw_ddb_index, stats_size,
486                                      iscsi_stats_dma);
487         if (ret != QLA_SUCCESS) {
488                 ql4_printk(KERN_ERR, ha,
489                            "Unable to retreive iscsi stats\n");
490                 goto free_stats;
491         }
492
493         /* octets */
494         stats->txdata_octets = le64_to_cpu(ql_iscsi_stats->tx_data_octets);
495         stats->rxdata_octets = le64_to_cpu(ql_iscsi_stats->rx_data_octets);
496         /* xmit pdus */
497         stats->noptx_pdus = le32_to_cpu(ql_iscsi_stats->tx_nopout_pdus);
498         stats->scsicmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_cmd_pdus);
499         stats->tmfcmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_tmf_cmd_pdus);
500         stats->login_pdus = le32_to_cpu(ql_iscsi_stats->tx_login_cmd_pdus);
501         stats->text_pdus = le32_to_cpu(ql_iscsi_stats->tx_text_cmd_pdus);
502         stats->dataout_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_write_pdus);
503         stats->logout_pdus = le32_to_cpu(ql_iscsi_stats->tx_logout_cmd_pdus);
504         stats->snack_pdus = le32_to_cpu(ql_iscsi_stats->tx_snack_req_pdus);
505         /* recv pdus */
506         stats->noprx_pdus = le32_to_cpu(ql_iscsi_stats->rx_nopin_pdus);
507         stats->scsirsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_resp_pdus);
508         stats->tmfrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_tmf_resp_pdus);
509         stats->textrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_text_resp_pdus);
510         stats->datain_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_read_pdus);
511         stats->logoutrsp_pdus =
512                         le32_to_cpu(ql_iscsi_stats->rx_logout_resp_pdus);
513         stats->r2t_pdus = le32_to_cpu(ql_iscsi_stats->rx_r2t_pdus);
514         stats->async_pdus = le32_to_cpu(ql_iscsi_stats->rx_async_pdus);
515         stats->rjt_pdus = le32_to_cpu(ql_iscsi_stats->rx_reject_pdus);
516
517 free_stats:
518         dma_free_coherent(&ha->pdev->dev, stats_size, ql_iscsi_stats,
519                           iscsi_stats_dma);
520 exit_get_stats:
521         return;
522 }
523
524 static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
525 {
526         struct iscsi_cls_session *session;
527         struct iscsi_session *sess;
528         unsigned long flags;
529         enum blk_eh_timer_return ret = BLK_EH_NOT_HANDLED;
530
531         session = starget_to_session(scsi_target(sc->device));
532         sess = session->dd_data;
533
534         spin_lock_irqsave(&session->lock, flags);
535         if (session->state == ISCSI_SESSION_FAILED)
536                 ret = BLK_EH_RESET_TIMER;
537         spin_unlock_irqrestore(&session->lock, flags);
538
539         return ret;
540 }
541
542 static int qla4xxx_host_get_param(struct Scsi_Host *shost,
543                                   enum iscsi_host_param param, char *buf)
544 {
545         struct scsi_qla_host *ha = to_qla_host(shost);
546         int len;
547
548         switch (param) {
549         case ISCSI_HOST_PARAM_HWADDRESS:
550                 len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
551                 break;
552         case ISCSI_HOST_PARAM_IPADDRESS:
553                 len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
554                 break;
555         case ISCSI_HOST_PARAM_INITIATOR_NAME:
556                 len = sprintf(buf, "%s\n", ha->name_string);
557                 break;
558         default:
559                 return -ENOSYS;
560         }
561
562         return len;
563 }
564
565 static void qla4xxx_create_ipv4_iface(struct scsi_qla_host *ha)
566 {
567         if (ha->iface_ipv4)
568                 return;
569
570         /* IPv4 */
571         ha->iface_ipv4 = iscsi_create_iface(ha->host,
572                                             &qla4xxx_iscsi_transport,
573                                             ISCSI_IFACE_TYPE_IPV4, 0, 0);
574         if (!ha->iface_ipv4)
575                 ql4_printk(KERN_ERR, ha, "Could not create IPv4 iSCSI "
576                            "iface0.\n");
577 }
578
579 static void qla4xxx_create_ipv6_iface(struct scsi_qla_host *ha)
580 {
581         if (!ha->iface_ipv6_0)
582                 /* IPv6 iface-0 */
583                 ha->iface_ipv6_0 = iscsi_create_iface(ha->host,
584                                                       &qla4xxx_iscsi_transport,
585                                                       ISCSI_IFACE_TYPE_IPV6, 0,
586                                                       0);
587         if (!ha->iface_ipv6_0)
588                 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
589                            "iface0.\n");
590
591         if (!ha->iface_ipv6_1)
592                 /* IPv6 iface-1 */
593                 ha->iface_ipv6_1 = iscsi_create_iface(ha->host,
594                                                       &qla4xxx_iscsi_transport,
595                                                       ISCSI_IFACE_TYPE_IPV6, 1,
596                                                       0);
597         if (!ha->iface_ipv6_1)
598                 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
599                            "iface1.\n");
600 }
601
602 static void qla4xxx_create_ifaces(struct scsi_qla_host *ha)
603 {
604         if (ha->ip_config.ipv4_options & IPOPT_IPV4_PROTOCOL_ENABLE)
605                 qla4xxx_create_ipv4_iface(ha);
606
607         if (ha->ip_config.ipv6_options & IPV6_OPT_IPV6_PROTOCOL_ENABLE)
608                 qla4xxx_create_ipv6_iface(ha);
609 }
610
611 static void qla4xxx_destroy_ipv4_iface(struct scsi_qla_host *ha)
612 {
613         if (ha->iface_ipv4) {
614                 iscsi_destroy_iface(ha->iface_ipv4);
615                 ha->iface_ipv4 = NULL;
616         }
617 }
618
619 static void qla4xxx_destroy_ipv6_iface(struct scsi_qla_host *ha)
620 {
621         if (ha->iface_ipv6_0) {
622                 iscsi_destroy_iface(ha->iface_ipv6_0);
623                 ha->iface_ipv6_0 = NULL;
624         }
625         if (ha->iface_ipv6_1) {
626                 iscsi_destroy_iface(ha->iface_ipv6_1);
627                 ha->iface_ipv6_1 = NULL;
628         }
629 }
630
631 static void qla4xxx_destroy_ifaces(struct scsi_qla_host *ha)
632 {
633         qla4xxx_destroy_ipv4_iface(ha);
634         qla4xxx_destroy_ipv6_iface(ha);
635 }
636
637 static void qla4xxx_set_ipv6(struct scsi_qla_host *ha,
638                              struct iscsi_iface_param_info *iface_param,
639                              struct addr_ctrl_blk *init_fw_cb)
640 {
641         /*
642          * iface_num 0 is valid for IPv6 Addr, linklocal, router, autocfg.
643          * iface_num 1 is valid only for IPv6 Addr.
644          */
645         switch (iface_param->param) {
646         case ISCSI_NET_PARAM_IPV6_ADDR:
647                 if (iface_param->iface_num & 0x1)
648                         /* IPv6 Addr 1 */
649                         memcpy(init_fw_cb->ipv6_addr1, iface_param->value,
650                                sizeof(init_fw_cb->ipv6_addr1));
651                 else
652                         /* IPv6 Addr 0 */
653                         memcpy(init_fw_cb->ipv6_addr0, iface_param->value,
654                                sizeof(init_fw_cb->ipv6_addr0));
655                 break;
656         case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
657                 if (iface_param->iface_num & 0x1)
658                         break;
659                 memcpy(init_fw_cb->ipv6_if_id, &iface_param->value[8],
660                        sizeof(init_fw_cb->ipv6_if_id));
661                 break;
662         case ISCSI_NET_PARAM_IPV6_ROUTER:
663                 if (iface_param->iface_num & 0x1)
664                         break;
665                 memcpy(init_fw_cb->ipv6_dflt_rtr_addr, iface_param->value,
666                        sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
667                 break;
668         case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
669                 /* Autocfg applies to even interface */
670                 if (iface_param->iface_num & 0x1)
671                         break;
672
673                 if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_DISABLE)
674                         init_fw_cb->ipv6_addtl_opts &=
675                                 cpu_to_le16(
676                                   ~IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
677                 else if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_ND_ENABLE)
678                         init_fw_cb->ipv6_addtl_opts |=
679                                 cpu_to_le16(
680                                   IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
681                 else
682                         ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
683                                    "IPv6 addr\n");
684                 break;
685         case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
686                 /* Autocfg applies to even interface */
687                 if (iface_param->iface_num & 0x1)
688                         break;
689
690                 if (iface_param->value[0] ==
691                     ISCSI_IPV6_LINKLOCAL_AUTOCFG_ENABLE)
692                         init_fw_cb->ipv6_addtl_opts |= cpu_to_le16(
693                                         IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
694                 else if (iface_param->value[0] ==
695                          ISCSI_IPV6_LINKLOCAL_AUTOCFG_DISABLE)
696                         init_fw_cb->ipv6_addtl_opts &= cpu_to_le16(
697                                        ~IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
698                 else
699                         ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
700                                    "IPv6 linklocal addr\n");
701                 break;
702         case ISCSI_NET_PARAM_IPV6_ROUTER_AUTOCFG:
703                 /* Autocfg applies to even interface */
704                 if (iface_param->iface_num & 0x1)
705                         break;
706
707                 if (iface_param->value[0] == ISCSI_IPV6_ROUTER_AUTOCFG_ENABLE)
708                         memset(init_fw_cb->ipv6_dflt_rtr_addr, 0,
709                                sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
710                 break;
711         case ISCSI_NET_PARAM_IFACE_ENABLE:
712                 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
713                         init_fw_cb->ipv6_opts |=
714                                 cpu_to_le16(IPV6_OPT_IPV6_PROTOCOL_ENABLE);
715                         qla4xxx_create_ipv6_iface(ha);
716                 } else {
717                         init_fw_cb->ipv6_opts &=
718                                 cpu_to_le16(~IPV6_OPT_IPV6_PROTOCOL_ENABLE &
719                                             0xFFFF);
720                         qla4xxx_destroy_ipv6_iface(ha);
721                 }
722                 break;
723         case ISCSI_NET_PARAM_VLAN_TAG:
724                 if (iface_param->len != sizeof(init_fw_cb->ipv6_vlan_tag))
725                         break;
726                 init_fw_cb->ipv6_vlan_tag =
727                                 cpu_to_be16(*(uint16_t *)iface_param->value);
728                 break;
729         case ISCSI_NET_PARAM_VLAN_ENABLED:
730                 if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
731                         init_fw_cb->ipv6_opts |=
732                                 cpu_to_le16(IPV6_OPT_VLAN_TAGGING_ENABLE);
733                 else
734                         init_fw_cb->ipv6_opts &=
735                                 cpu_to_le16(~IPV6_OPT_VLAN_TAGGING_ENABLE);
736                 break;
737         case ISCSI_NET_PARAM_MTU:
738                 init_fw_cb->eth_mtu_size =
739                                 cpu_to_le16(*(uint16_t *)iface_param->value);
740                 break;
741         case ISCSI_NET_PARAM_PORT:
742                 /* Autocfg applies to even interface */
743                 if (iface_param->iface_num & 0x1)
744                         break;
745
746                 init_fw_cb->ipv6_port =
747                                 cpu_to_le16(*(uint16_t *)iface_param->value);
748                 break;
749         default:
750                 ql4_printk(KERN_ERR, ha, "Unknown IPv6 param = %d\n",
751                            iface_param->param);
752                 break;
753         }
754 }
755
756 static void qla4xxx_set_ipv4(struct scsi_qla_host *ha,
757                              struct iscsi_iface_param_info *iface_param,
758                              struct addr_ctrl_blk *init_fw_cb)
759 {
760         switch (iface_param->param) {
761         case ISCSI_NET_PARAM_IPV4_ADDR:
762                 memcpy(init_fw_cb->ipv4_addr, iface_param->value,
763                        sizeof(init_fw_cb->ipv4_addr));
764                 break;
765         case ISCSI_NET_PARAM_IPV4_SUBNET:
766                 memcpy(init_fw_cb->ipv4_subnet, iface_param->value,
767                        sizeof(init_fw_cb->ipv4_subnet));
768                 break;
769         case ISCSI_NET_PARAM_IPV4_GW:
770                 memcpy(init_fw_cb->ipv4_gw_addr, iface_param->value,
771                        sizeof(init_fw_cb->ipv4_gw_addr));
772                 break;
773         case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
774                 if (iface_param->value[0] == ISCSI_BOOTPROTO_DHCP)
775                         init_fw_cb->ipv4_tcp_opts |=
776                                         cpu_to_le16(TCPOPT_DHCP_ENABLE);
777                 else if (iface_param->value[0] == ISCSI_BOOTPROTO_STATIC)
778                         init_fw_cb->ipv4_tcp_opts &=
779                                         cpu_to_le16(~TCPOPT_DHCP_ENABLE);
780                 else
781                         ql4_printk(KERN_ERR, ha, "Invalid IPv4 bootproto\n");
782                 break;
783         case ISCSI_NET_PARAM_IFACE_ENABLE:
784                 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
785                         init_fw_cb->ipv4_ip_opts |=
786                                 cpu_to_le16(IPOPT_IPV4_PROTOCOL_ENABLE);
787                         qla4xxx_create_ipv4_iface(ha);
788                 } else {
789                         init_fw_cb->ipv4_ip_opts &=
790                                 cpu_to_le16(~IPOPT_IPV4_PROTOCOL_ENABLE &
791                                             0xFFFF);
792                         qla4xxx_destroy_ipv4_iface(ha);
793                 }
794                 break;
795         case ISCSI_NET_PARAM_VLAN_TAG:
796                 if (iface_param->len != sizeof(init_fw_cb->ipv4_vlan_tag))
797                         break;
798                 init_fw_cb->ipv4_vlan_tag =
799                                 cpu_to_be16(*(uint16_t *)iface_param->value);
800                 break;
801         case ISCSI_NET_PARAM_VLAN_ENABLED:
802                 if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
803                         init_fw_cb->ipv4_ip_opts |=
804                                         cpu_to_le16(IPOPT_VLAN_TAGGING_ENABLE);
805                 else
806                         init_fw_cb->ipv4_ip_opts &=
807                                         cpu_to_le16(~IPOPT_VLAN_TAGGING_ENABLE);
808                 break;
809         case ISCSI_NET_PARAM_MTU:
810                 init_fw_cb->eth_mtu_size =
811                                 cpu_to_le16(*(uint16_t *)iface_param->value);
812                 break;
813         case ISCSI_NET_PARAM_PORT:
814                 init_fw_cb->ipv4_port =
815                                 cpu_to_le16(*(uint16_t *)iface_param->value);
816                 break;
817         default:
818                 ql4_printk(KERN_ERR, ha, "Unknown IPv4 param = %d\n",
819                            iface_param->param);
820                 break;
821         }
822 }
823
824 static void
825 qla4xxx_initcb_to_acb(struct addr_ctrl_blk *init_fw_cb)
826 {
827         struct addr_ctrl_blk_def *acb;
828         acb = (struct addr_ctrl_blk_def *)init_fw_cb;
829         memset(acb->reserved1, 0, sizeof(acb->reserved1));
830         memset(acb->reserved2, 0, sizeof(acb->reserved2));
831         memset(acb->reserved3, 0, sizeof(acb->reserved3));
832         memset(acb->reserved4, 0, sizeof(acb->reserved4));
833         memset(acb->reserved5, 0, sizeof(acb->reserved5));
834         memset(acb->reserved6, 0, sizeof(acb->reserved6));
835         memset(acb->reserved7, 0, sizeof(acb->reserved7));
836         memset(acb->reserved8, 0, sizeof(acb->reserved8));
837         memset(acb->reserved9, 0, sizeof(acb->reserved9));
838         memset(acb->reserved10, 0, sizeof(acb->reserved10));
839         memset(acb->reserved11, 0, sizeof(acb->reserved11));
840         memset(acb->reserved12, 0, sizeof(acb->reserved12));
841         memset(acb->reserved13, 0, sizeof(acb->reserved13));
842         memset(acb->reserved14, 0, sizeof(acb->reserved14));
843         memset(acb->reserved15, 0, sizeof(acb->reserved15));
844 }
845
846 static int
847 qla4xxx_iface_set_param(struct Scsi_Host *shost, void *data, uint32_t len)
848 {
849         struct scsi_qla_host *ha = to_qla_host(shost);
850         int rval = 0;
851         struct iscsi_iface_param_info *iface_param = NULL;
852         struct addr_ctrl_blk *init_fw_cb = NULL;
853         dma_addr_t init_fw_cb_dma;
854         uint32_t mbox_cmd[MBOX_REG_COUNT];
855         uint32_t mbox_sts[MBOX_REG_COUNT];
856         uint32_t rem = len;
857         struct nlattr *attr;
858
859         init_fw_cb = dma_alloc_coherent(&ha->pdev->dev,
860                                         sizeof(struct addr_ctrl_blk),
861                                         &init_fw_cb_dma, GFP_KERNEL);
862         if (!init_fw_cb) {
863                 ql4_printk(KERN_ERR, ha, "%s: Unable to alloc init_cb\n",
864                            __func__);
865                 return -ENOMEM;
866         }
867
868         memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
869         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
870         memset(&mbox_sts, 0, sizeof(mbox_sts));
871
872         if (qla4xxx_get_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma)) {
873                 ql4_printk(KERN_ERR, ha, "%s: get ifcb failed\n", __func__);
874                 rval = -EIO;
875                 goto exit_init_fw_cb;
876         }
877
878         nla_for_each_attr(attr, data, len, rem) {
879                 iface_param = nla_data(attr);
880
881                 if (iface_param->param_type != ISCSI_NET_PARAM)
882                         continue;
883
884                 switch (iface_param->iface_type) {
885                 case ISCSI_IFACE_TYPE_IPV4:
886                         switch (iface_param->iface_num) {
887                         case 0:
888                                 qla4xxx_set_ipv4(ha, iface_param, init_fw_cb);
889                                 break;
890                         default:
891                                 /* Cannot have more than one IPv4 interface */
892                                 ql4_printk(KERN_ERR, ha, "Invalid IPv4 iface "
893                                            "number = %d\n",
894                                            iface_param->iface_num);
895                                 break;
896                         }
897                         break;
898                 case ISCSI_IFACE_TYPE_IPV6:
899                         switch (iface_param->iface_num) {
900                         case 0:
901                         case 1:
902                                 qla4xxx_set_ipv6(ha, iface_param, init_fw_cb);
903                                 break;
904                         default:
905                                 /* Cannot have more than two IPv6 interface */
906                                 ql4_printk(KERN_ERR, ha, "Invalid IPv6 iface "
907                                            "number = %d\n",
908                                            iface_param->iface_num);
909                                 break;
910                         }
911                         break;
912                 default:
913                         ql4_printk(KERN_ERR, ha, "Invalid iface type\n");
914                         break;
915                 }
916         }
917
918         init_fw_cb->cookie = cpu_to_le32(0x11BEAD5A);
919
920         rval = qla4xxx_set_flash(ha, init_fw_cb_dma, FLASH_SEGMENT_IFCB,
921                                  sizeof(struct addr_ctrl_blk),
922                                  FLASH_OPT_RMW_COMMIT);
923         if (rval != QLA_SUCCESS) {
924                 ql4_printk(KERN_ERR, ha, "%s: set flash mbx failed\n",
925                            __func__);
926                 rval = -EIO;
927                 goto exit_init_fw_cb;
928         }
929
930         qla4xxx_disable_acb(ha);
931
932         qla4xxx_initcb_to_acb(init_fw_cb);
933
934         rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma);
935         if (rval != QLA_SUCCESS) {
936                 ql4_printk(KERN_ERR, ha, "%s: set acb mbx failed\n",
937                            __func__);
938                 rval = -EIO;
939                 goto exit_init_fw_cb;
940         }
941
942         memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
943         qla4xxx_update_local_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb,
944                                   init_fw_cb_dma);
945
946 exit_init_fw_cb:
947         dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk),
948                           init_fw_cb, init_fw_cb_dma);
949
950         return rval;
951 }
952
953 static int qla4xxx_conn_get_param(struct iscsi_cls_conn *cls_conn,
954                                   enum iscsi_param param, char *buf)
955 {
956         struct iscsi_conn *conn;
957         struct qla_conn *qla_conn;
958         struct sockaddr *dst_addr;
959         int len = 0;
960
961         conn = cls_conn->dd_data;
962         qla_conn = conn->dd_data;
963         dst_addr = &qla_conn->qla_ep->dst_addr;
964
965         switch (param) {
966         case ISCSI_PARAM_CONN_PORT:
967         case ISCSI_PARAM_CONN_ADDRESS:
968                 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
969                                                  dst_addr, param, buf);
970         default:
971                 return iscsi_conn_get_param(cls_conn, param, buf);
972         }
973
974         return len;
975
976 }
977
978 static struct iscsi_cls_session *
979 qla4xxx_session_create(struct iscsi_endpoint *ep,
980                         uint16_t cmds_max, uint16_t qdepth,
981                         uint32_t initial_cmdsn)
982 {
983         struct iscsi_cls_session *cls_sess;
984         struct scsi_qla_host *ha;
985         struct qla_endpoint *qla_ep;
986         struct ddb_entry *ddb_entry;
987         uint32_t ddb_index;
988         uint32_t mbx_sts = 0;
989         struct iscsi_session *sess;
990         struct sockaddr *dst_addr;
991         int ret;
992
993         DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
994         if (!ep) {
995                 printk(KERN_ERR "qla4xxx: missing ep.\n");
996                 return NULL;
997         }
998
999         qla_ep = ep->dd_data;
1000         dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
1001         ha = to_qla_host(qla_ep->host);
1002
1003 get_ddb_index:
1004         ddb_index = find_first_zero_bit(ha->ddb_idx_map, MAX_DDB_ENTRIES);
1005
1006         if (ddb_index >= MAX_DDB_ENTRIES) {
1007                 DEBUG2(ql4_printk(KERN_INFO, ha,
1008                                   "Free DDB index not available\n"));
1009                 return NULL;
1010         }
1011
1012         if (test_and_set_bit(ddb_index, ha->ddb_idx_map))
1013                 goto get_ddb_index;
1014
1015         DEBUG2(ql4_printk(KERN_INFO, ha,
1016                           "Found a free DDB index at %d\n", ddb_index));
1017         ret = qla4xxx_req_ddb_entry(ha, ddb_index, &mbx_sts);
1018         if (ret == QLA_ERROR) {
1019                 if (mbx_sts == MBOX_STS_COMMAND_ERROR) {
1020                         ql4_printk(KERN_INFO, ha,
1021                                    "DDB index = %d not available trying next\n",
1022                                    ddb_index);
1023                         goto get_ddb_index;
1024                 }
1025                 DEBUG2(ql4_printk(KERN_INFO, ha,
1026                                   "Free FW DDB not available\n"));
1027                 return NULL;
1028         }
1029
1030         cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport, qla_ep->host,
1031                                        cmds_max, sizeof(struct ddb_entry),
1032                                        sizeof(struct ql4_task_data),
1033                                        initial_cmdsn, ddb_index);
1034         if (!cls_sess)
1035                 return NULL;
1036
1037         sess = cls_sess->dd_data;
1038         ddb_entry = sess->dd_data;
1039         ddb_entry->fw_ddb_index = ddb_index;
1040         ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
1041         ddb_entry->ha = ha;
1042         ddb_entry->sess = cls_sess;
1043         cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
1044         ha->fw_ddb_index_map[ddb_entry->fw_ddb_index] = ddb_entry;
1045         ha->tot_ddbs++;
1046
1047         return cls_sess;
1048 }
1049
1050 static void qla4xxx_session_destroy(struct iscsi_cls_session *cls_sess)
1051 {
1052         struct iscsi_session *sess;
1053         struct ddb_entry *ddb_entry;
1054         struct scsi_qla_host *ha;
1055         unsigned long flags;
1056
1057         DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1058         sess = cls_sess->dd_data;
1059         ddb_entry = sess->dd_data;
1060         ha = ddb_entry->ha;
1061
1062         qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
1063
1064         spin_lock_irqsave(&ha->hardware_lock, flags);
1065         qla4xxx_free_ddb(ha, ddb_entry);
1066         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1067         iscsi_session_teardown(cls_sess);
1068 }
1069
1070 static struct iscsi_cls_conn *
1071 qla4xxx_conn_create(struct iscsi_cls_session *cls_sess, uint32_t conn_idx)
1072 {
1073         struct iscsi_cls_conn *cls_conn;
1074         struct iscsi_session *sess;
1075         struct ddb_entry *ddb_entry;
1076
1077         DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1078         cls_conn = iscsi_conn_setup(cls_sess, sizeof(struct qla_conn),
1079                                     conn_idx);
1080         sess = cls_sess->dd_data;
1081         ddb_entry = sess->dd_data;
1082         ddb_entry->conn = cls_conn;
1083
1084         return cls_conn;
1085 }
1086
1087 static int qla4xxx_conn_bind(struct iscsi_cls_session *cls_session,
1088                              struct iscsi_cls_conn *cls_conn,
1089                              uint64_t transport_fd, int is_leading)
1090 {
1091         struct iscsi_conn *conn;
1092         struct qla_conn *qla_conn;
1093         struct iscsi_endpoint *ep;
1094
1095         DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1096
1097         if (iscsi_conn_bind(cls_session, cls_conn, is_leading))
1098                 return -EINVAL;
1099         ep = iscsi_lookup_endpoint(transport_fd);
1100         conn = cls_conn->dd_data;
1101         qla_conn = conn->dd_data;
1102         qla_conn->qla_ep = ep->dd_data;
1103         return 0;
1104 }
1105
1106 static int qla4xxx_conn_start(struct iscsi_cls_conn *cls_conn)
1107 {
1108         struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
1109         struct iscsi_session *sess;
1110         struct ddb_entry *ddb_entry;
1111         struct scsi_qla_host *ha;
1112         struct dev_db_entry *fw_ddb_entry;
1113         dma_addr_t fw_ddb_entry_dma;
1114         uint32_t mbx_sts = 0;
1115         int ret = 0;
1116         int status = QLA_SUCCESS;
1117
1118         DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1119         sess = cls_sess->dd_data;
1120         ddb_entry = sess->dd_data;
1121         ha = ddb_entry->ha;
1122
1123         fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1124                                           &fw_ddb_entry_dma, GFP_KERNEL);
1125         if (!fw_ddb_entry) {
1126                 ql4_printk(KERN_ERR, ha,
1127                            "%s: Unable to allocate dma buffer\n", __func__);
1128                 return -ENOMEM;
1129         }
1130
1131         ret = qla4xxx_set_param_ddbentry(ha, ddb_entry, cls_conn, &mbx_sts);
1132         if (ret) {
1133                 /* If iscsid is stopped and started then no need to do
1134                 * set param again since ddb state will be already
1135                 * active and FW does not allow set ddb to an
1136                 * active session.
1137                 */
1138                 if (mbx_sts)
1139                         if (ddb_entry->fw_ddb_device_state ==
1140                                                 DDB_DS_SESSION_ACTIVE) {
1141                                 iscsi_conn_start(ddb_entry->conn);
1142                                 iscsi_conn_login_event(ddb_entry->conn,
1143                                                 ISCSI_CONN_STATE_LOGGED_IN);
1144                                 goto exit_set_param;
1145                         }
1146
1147                 ql4_printk(KERN_ERR, ha, "%s: Failed set param for index[%d]\n",
1148                            __func__, ddb_entry->fw_ddb_index);
1149                 goto exit_conn_start;
1150         }
1151
1152         status = qla4xxx_conn_open(ha, ddb_entry->fw_ddb_index);
1153         if (status == QLA_ERROR) {
1154                 ql4_printk(KERN_ERR, ha, "%s: Login failed: %s\n", __func__,
1155                            sess->targetname);
1156                 ret = -EINVAL;
1157                 goto exit_conn_start;
1158         }
1159
1160         if (ddb_entry->fw_ddb_device_state == DDB_DS_NO_CONNECTION_ACTIVE)
1161                 ddb_entry->fw_ddb_device_state = DDB_DS_LOGIN_IN_PROCESS;
1162
1163         DEBUG2(printk(KERN_INFO "%s: DDB state [%d]\n", __func__,
1164                       ddb_entry->fw_ddb_device_state));
1165
1166 exit_set_param:
1167         ret = 0;
1168
1169 exit_conn_start:
1170         dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1171                           fw_ddb_entry, fw_ddb_entry_dma);
1172         return ret;
1173 }
1174
1175 static void qla4xxx_conn_destroy(struct iscsi_cls_conn *cls_conn)
1176 {
1177         struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
1178         struct iscsi_session *sess;
1179         struct scsi_qla_host *ha;
1180         struct ddb_entry *ddb_entry;
1181         int options;
1182
1183         DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1184         sess = cls_sess->dd_data;
1185         ddb_entry = sess->dd_data;
1186         ha = ddb_entry->ha;
1187
1188         options = LOGOUT_OPTION_CLOSE_SESSION;
1189         if (qla4xxx_session_logout_ddb(ha, ddb_entry, options) == QLA_ERROR)
1190                 ql4_printk(KERN_ERR, ha, "%s: Logout failed\n", __func__);
1191 }
1192
1193 static void qla4xxx_task_work(struct work_struct *wdata)
1194 {
1195         struct ql4_task_data *task_data;
1196         struct scsi_qla_host *ha;
1197         struct passthru_status *sts;
1198         struct iscsi_task *task;
1199         struct iscsi_hdr *hdr;
1200         uint8_t *data;
1201         uint32_t data_len;
1202         struct iscsi_conn *conn;
1203         int hdr_len;
1204         itt_t itt;
1205
1206         task_data = container_of(wdata, struct ql4_task_data, task_work);
1207         ha = task_data->ha;
1208         task = task_data->task;
1209         sts = &task_data->sts;
1210         hdr_len = sizeof(struct iscsi_hdr);
1211
1212         DEBUG3(printk(KERN_INFO "Status returned\n"));
1213         DEBUG3(qla4xxx_dump_buffer(sts, 64));
1214         DEBUG3(printk(KERN_INFO "Response buffer"));
1215         DEBUG3(qla4xxx_dump_buffer(task_data->resp_buffer, 64));
1216
1217         conn = task->conn;
1218
1219         switch (sts->completionStatus) {
1220         case PASSTHRU_STATUS_COMPLETE:
1221                 hdr = (struct iscsi_hdr *)task_data->resp_buffer;
1222                 /* Assign back the itt in hdr, until we use the PREASSIGN_TAG */
1223                 itt = sts->handle;
1224                 hdr->itt = itt;
1225                 data = task_data->resp_buffer + hdr_len;
1226                 data_len = task_data->resp_len - hdr_len;
1227                 iscsi_complete_pdu(conn, hdr, data, data_len);
1228                 break;
1229         default:
1230                 ql4_printk(KERN_ERR, ha, "Passthru failed status = 0x%x\n",
1231                            sts->completionStatus);
1232                 break;
1233         }
1234         return;
1235 }
1236
1237 static int qla4xxx_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
1238 {
1239         struct ql4_task_data *task_data;
1240         struct iscsi_session *sess;
1241         struct ddb_entry *ddb_entry;
1242         struct scsi_qla_host *ha;
1243         int hdr_len;
1244
1245         sess = task->conn->session;
1246         ddb_entry = sess->dd_data;
1247         ha = ddb_entry->ha;
1248         task_data = task->dd_data;
1249         memset(task_data, 0, sizeof(struct ql4_task_data));
1250
1251         if (task->sc) {
1252                 ql4_printk(KERN_INFO, ha,
1253                            "%s: SCSI Commands not implemented\n", __func__);
1254                 return -EINVAL;
1255         }
1256
1257         hdr_len = sizeof(struct iscsi_hdr);
1258         task_data->ha = ha;
1259         task_data->task = task;
1260
1261         if (task->data_count) {
1262                 task_data->data_dma = dma_map_single(&ha->pdev->dev, task->data,
1263                                                      task->data_count,
1264                                                      PCI_DMA_TODEVICE);
1265         }
1266
1267         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
1268                       __func__, task->conn->max_recv_dlength, hdr_len));
1269
1270         task_data->resp_len = task->conn->max_recv_dlength + hdr_len;
1271         task_data->resp_buffer = dma_alloc_coherent(&ha->pdev->dev,
1272                                                     task_data->resp_len,
1273                                                     &task_data->resp_dma,
1274                                                     GFP_ATOMIC);
1275         if (!task_data->resp_buffer)
1276                 goto exit_alloc_pdu;
1277
1278         task_data->req_len = task->data_count + hdr_len;
1279         task_data->req_buffer = dma_alloc_coherent(&ha->pdev->dev,
1280                                                    task_data->req_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
1292 exit_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->req_len,
1299                                   task_data->req_buffer, task_data->req_dma);
1300         return -ENOMEM;
1301 }
1302
1303 static 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->req_len,
1328                           task_data->req_buffer, task_data->req_dma);
1329         return;
1330 }
1331
1332 static 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
1347 void 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
1412 /*
1413  * Timer routines
1414  */
1415
1416 static 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
1429 static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
1430 {
1431         del_timer_sync(&ha->timer);
1432         ha->timer_active = 0;
1433 }
1434
1435 /***
1436  * qla4xxx_mark_device_missing - blocks the session
1437  * @cls_session: Pointer to the session to be blocked
1438  * @ddb_entry: Pointer to device database entry
1439  *
1440  * This routine marks a device missing and close connection.
1441  **/
1442 void qla4xxx_mark_device_missing(struct iscsi_cls_session *cls_session)
1443 {
1444         iscsi_block_session(cls_session);
1445 }
1446
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  **/
1453 void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
1454 {
1455         iscsi_host_for_each_session(ha->host, qla4xxx_mark_device_missing);
1456 }
1457
1458 static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
1459                                        struct ddb_entry *ddb_entry,
1460                                        struct scsi_cmnd *cmd)
1461 {
1462         struct srb *srb;
1463
1464         srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
1465         if (!srb)
1466                 return srb;
1467
1468         kref_init(&srb->srb_ref);
1469         srb->ha = ha;
1470         srb->ddb = ddb_entry;
1471         srb->cmd = cmd;
1472         srb->flags = 0;
1473         CMD_SP(cmd) = (void *)srb;
1474
1475         return srb;
1476 }
1477
1478 static 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) {
1483                 scsi_dma_unmap(cmd);
1484                 srb->flags &= ~SRB_DMA_VALID;
1485         }
1486         CMD_SP(cmd) = NULL;
1487 }
1488
1489 void qla4xxx_srb_compl(struct kref *ref)
1490 {
1491         struct srb *srb = container_of(ref, struct srb, srb_ref);
1492         struct scsi_cmnd *cmd = srb->cmd;
1493         struct scsi_qla_host *ha = srb->ha;
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.
1504  * @host: scsi host
1505  * @cmd: Pointer to Linux's SCSI command structure
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  **/
1515 static int qla4xxx_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
1516 {
1517         struct scsi_qla_host *ha = to_qla_host(host);
1518         struct ddb_entry *ddb_entry = cmd->device->hostdata;
1519         struct iscsi_cls_session *sess = ddb_entry->sess;
1520         struct srb *srb;
1521         int rval;
1522
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
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
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) ||
1548             !test_bit(AF_LINK_UP, &ha->flags) ||
1549             test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
1550                 goto qc_host_busy;
1551
1552         srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd);
1553         if (!srb)
1554                 goto qc_host_busy;
1555
1556         rval = qla4xxx_send_command_to_isp(ha, srb);
1557         if (rval != QLA_SUCCESS)
1558                 goto qc_host_busy_free_sp;
1559
1560         return 0;
1561
1562 qc_host_busy_free_sp:
1563         qla4xxx_srb_free_dma(ha, srb);
1564         mempool_free(srb, ha->srb_mempool);
1565
1566 qc_host_busy:
1567         return SCSI_MLQUEUE_HOST_BUSY;
1568
1569 qc_fail_command:
1570         cmd->scsi_done(cmd);
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  **/
1581 static 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
1603         if (ha->chap_dma_pool)
1604                 dma_pool_destroy(ha->chap_dma_pool);
1605
1606         if (ha->chap_list)
1607                 vfree(ha->chap_list);
1608         ha->chap_list = NULL;
1609
1610         /* release io space registers  */
1611         if (is_qla8022(ha)) {
1612                 if (ha->nx_pcibase)
1613                         iounmap(
1614                             (struct device_reg_82xx __iomem *)ha->nx_pcibase);
1615         } else if (ha->reg)
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  **/
1627 static 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) {
1640                 ql4_printk(KERN_WARNING, ha,
1641                     "Memory Allocation failed - queues.\n");
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) {
1677                 ql4_printk(KERN_WARNING, ha,
1678                     "Memory Allocation failed - SRB Pool.\n");
1679
1680                 goto mem_alloc_error_exit;
1681         }
1682
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
1692         return QLA_SUCCESS;
1693
1694 mem_alloc_error_exit:
1695         qla4xxx_mem_free(ha);
1696         return QLA_ERROR;
1697 }
1698
1699 /**
1700  * qla4_8xxx_check_fw_alive  - Check firmware health
1701  * @ha: Pointer to host adapter structure.
1702  *
1703  * Context: Interrupt
1704  **/
1705 static 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);
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         }
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,
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));
1745
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);
1757                         qla4xxx_mailbox_premature_completion(ha);
1758                 }
1759         } else
1760                 ha->seconds_since_last_heartbeat = 0;
1761
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  **/
1771 void 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 */
1778         if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
1779             test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1780             test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags))) {
1781                 if (dev_state == QLA82XX_DEV_NEED_RESET &&
1782                     !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
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                         }
1790                 } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
1791                     !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
1792                         ql4_printk(KERN_INFO, ha, "%s: HW State: NEED QUIES!\n",
1793                             __func__);
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
1803 /**
1804  * qla4xxx_timer - checks every second for work to do.
1805  * @ha: Pointer to host adapter structure.
1806  **/
1807 static void qla4xxx_timer(struct scsi_qla_host *ha)
1808 {
1809         int start_dpc = 0;
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);
1823
1824         if (is_qla8022(ha)) {
1825                 qla4_8xxx_watchdog(ha);
1826         }
1827
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                 }
1837         }
1838
1839         /* Wakeup the dpc routine for this adapter, if needed. */
1840         if (start_dpc ||
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) ||
1844              test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
1845              test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1846              test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
1847              test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
1848              test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
1849              test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
1850              test_bit(DPC_AEN, &ha->dpc_flags)) {
1851                 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
1852                               " - dpc flags = 0x%lx\n",
1853                               ha->host_no, __func__, ha->dpc_flags));
1854                 qla4xxx_wake_dpc(ha);
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  **/
1870 static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
1871 {
1872         uint32_t index = 0;
1873         unsigned long flags;
1874         struct scsi_cmnd *cmd;
1875
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)) {
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);
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))
1893                                 break;
1894                 }
1895                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1896
1897                 /* If No Commands are pending, wait is complete */
1898                 if (index == ha->host->can_queue)
1899                         return QLA_SUCCESS;
1900
1901                 msleep(1000);
1902         }
1903         /* If we timed out on waiting for commands to come back
1904          * return ERROR. */
1905         return QLA_ERROR;
1906 }
1907
1908 int qla4xxx_hw_reset(struct scsi_qla_host *ha)
1909 {
1910         uint32_t ctrl_status;
1911         unsigned long flags = 0;
1912
1913         DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
1914
1915         if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
1916                 return QLA_ERROR;
1917
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);
1933         return QLA_SUCCESS;
1934 }
1935
1936 /**
1937  * qla4xxx_soft_reset - performs soft reset.
1938  * @ha: Pointer to host adapter structure.
1939  **/
1940 int qla4xxx_soft_reset(struct scsi_qla_host *ha)
1941 {
1942         uint32_t max_wait_time;
1943         unsigned long flags = 0;
1944         int status;
1945         uint32_t ctrl_status;
1946
1947         status = qla4xxx_hw_reset(ha);
1948         if (status != QLA_SUCCESS)
1949                 return status;
1950
1951         status = QLA_ERROR;
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
2034 /**
2035  * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
2036  * @ha: Pointer to host adapter structure.
2037  * @res: returned scsi status
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  **/
2044 static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
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) {
2054                         srb->cmd->result = res;
2055                         kref_put(&srb->srb_ref, qla4xxx_srb_compl);
2056                 }
2057         }
2058         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2059 }
2060
2061 void 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");
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
2073 static 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
2084 /**
2085  * qla4xxx_recover_adapter - recovers adapter after a fatal error
2086  * @ha: Pointer to host adapter structure.
2087  **/
2088 static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
2089 {
2090         int status = QLA_ERROR;
2091         uint8_t reset_chip = 0;
2092
2093         /* Stall incoming I/O until we are done */
2094         scsi_block_requests(ha->host);
2095         clear_bit(AF_ONLINE, &ha->flags);
2096         clear_bit(AF_LINK_UP, &ha->flags);
2097
2098         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
2099
2100         set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2101
2102         iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
2103
2104         if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
2105                 reset_chip = 1;
2106
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         }
2113
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) {
2124                         if (!test_bit(AF_FW_RECOVERY, &ha->flags))
2125                                 qla4xxx_cmd_wait(ha);
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         }
2137
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) {
2142                 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
2143                         qla4xxx_cmd_wait(ha);
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         }
2151
2152         /* Flush any pending ddb changed AENs */
2153         qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2154
2155 recover_ha_init_adapter:
2156         /* Upon successful firmware/chip reset, re-initialize the adapter */
2157         if (status == QLA_SUCCESS) {
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 */
2166                 status = qla4xxx_initialize_adapter(ha);
2167         }
2168
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)) {
2174                 /* Adapter initialization failed, see if we can retry
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 */
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));
2203                                 qla4xxx_dead_adapter_cleanup(ha);
2204                                 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
2205                                 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
2206                                 clear_bit(DPC_RESET_HA_FW_CONTEXT,
2207                                           &ha->dpc_flags);
2208                                 status = QLA_ERROR;
2209                         }
2210                 }
2211         } else {
2212                 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
2213                 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2214                 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
2215         }
2216
2217         ha->adapter_error_count++;
2218
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,
2226             status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
2227
2228         return status;
2229 }
2230
2231 static void qla4xxx_relogin_devices(struct iscsi_cls_session *cls_session)
2232 {
2233         struct iscsi_session *sess;
2234         struct ddb_entry *ddb_entry;
2235         struct scsi_qla_host *ha;
2236
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);
2250                 }
2251         }
2252 }
2253
2254 static void qla4xxx_relogin_all_devices(struct scsi_qla_host *ha)
2255 {
2256         iscsi_host_for_each_session(ha->host, qla4xxx_relogin_devices);
2257 }
2258
2259 void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
2260 {
2261         if (ha->dpc_thread)
2262                 queue_work(ha->dpc_thread, &ha->dpc_work);
2263 }
2264
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  **/
2276 static void qla4xxx_do_dpc(struct work_struct *work)
2277 {
2278         struct scsi_qla_host *ha =
2279                 container_of(work, struct scsi_qla_host, dpc_work);
2280         int status = QLA_ERROR;
2281
2282         DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
2283             "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
2284             ha->host_no, __func__, ha->flags, ha->dpc_flags))
2285
2286         /* Initialization not yet finished. Don't do anything yet. */
2287         if (!test_bit(AF_INIT_DONE, &ha->flags))
2288                 return;
2289
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));
2293                 return;
2294         }
2295
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) ||
2312             test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
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);
2325
2326                 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
2327                         uint8_t wait_time = RESET_INTR_TOV;
2328
2329                         while ((readw(&ha->reg->ctrl_status) &
2330                                 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
2331                                 if (--wait_time == 0)
2332                                         break;
2333                                 msleep(1000);
2334                         }
2335                         if (wait_time == 0)
2336                                 DEBUG2(printk("scsi%ld: %s: SR|FSR "
2337                                               "bit not cleared-- resetting\n",
2338                                               ha->host_no, __func__));
2339                         qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
2340                         if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
2341                                 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2342                                 status = qla4xxx_recover_adapter(ha);
2343                         }
2344                         clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
2345                         if (status == QLA_SUCCESS)
2346                                 ha->isp_ops->enable_intrs(ha);
2347                 }
2348         }
2349
2350 dpc_post_reset_ha:
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
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? --- */
2363                         qla4xxx_mark_all_devices_missing(ha);
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
2372                         qla4xxx_relogin_all_devices(ha);
2373                 }
2374         }
2375 }
2376
2377 /**
2378  * qla4xxx_free_adapter - release the adapter
2379  * @ha: pointer to adapter structure
2380  **/
2381 static 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. */
2386                 ha->isp_ops->disable_intrs(ha);
2387         }
2388
2389         /* Remove timer thread, if present */
2390         if (ha->timer_active)
2391                 qla4xxx_stop_timer(ha);
2392
2393         /* Kill the kernel thread for this host */
2394         if (ha->dpc_thread)
2395                 destroy_workqueue(ha->dpc_thread);
2396
2397         /* Kill the kernel thread for this host */
2398         if (ha->task_wq)
2399                 destroy_workqueue(ha->task_wq);
2400
2401         /* Put firmware in known state */
2402         ha->isp_ops->reset_firmware(ha);
2403
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         }
2409
2410         /* Detach interrupts */
2411         if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
2412                 qla4xxx_free_irqs(ha);
2413
2414         /* free extra memory */
2415         qla4xxx_mem_free(ha);
2416 }
2417
2418 int 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;
2437
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));
2443
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
2463         ha->nx_db_wr_ptr = (ha->pdev->devfn == 4 ? QLA82XX_CAM_RAM_DB1 :
2464             QLA82XX_CAM_RAM_DB2);
2465
2466         return 0;
2467 iospace_error_exit:
2468         return -ENOMEM;
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  **/
2478 int qla4xxx_iospace_config(struct scsi_qla_host *ha)
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) {
2488                         ql4_printk(KERN_WARNING, ha,
2489                                 "Invalid PCI I/O region size\n");
2490                         pio = 0;
2491                 }
2492         } else {
2493                 ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
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)) {
2503                 ql4_printk(KERN_ERR, ha,
2504                     "region #0 not an MMIO resource, aborting\n");
2505
2506                 goto iospace_error_exit;
2507         }
2508
2509         if (mmio_len < MIN_IOBASE_LEN) {
2510                 ql4_printk(KERN_ERR, ha,
2511                     "Invalid PCI mem region size, aborting\n");
2512                 goto iospace_error_exit;
2513         }
2514
2515         if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
2516                 ql4_printk(KERN_WARNING, ha,
2517                     "Failed to reserve PIO/MMIO regions\n");
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) {
2526                 ql4_printk(KERN_ERR, ha,
2527                     "cannot remap MMIO, aborting\n");
2528
2529                 goto iospace_error_exit;
2530         }
2531
2532         return 0;
2533
2534 iospace_error_exit:
2535         return -ENOMEM;
2536 }
2537
2538 static 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
2555 static 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
2572 uint16_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
2577 uint16_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
2582 uint16_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
2587 uint16_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
2592 static 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
2616 static 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
2633 static 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
2650 static 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
2665 static ssize_t
2666 qla4xxx_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
2721 static 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
2729 static 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
2737 static 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
2760 static void qla4xxx_boot_release(void *data)
2761 {
2762         struct scsi_qla_host *ha = data;
2763
2764         scsi_host_put(ha->host);
2765 }
2766
2767 static 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         ql4_printk(KERN_INFO, ha, "%s: Get FW boot info for 0x%x func %d\n",
2781                    __func__, ha->pdev->device, func_num);
2782
2783         if (is_qla40XX(ha)) {
2784                 if (func_num == 1) {
2785                         addr = NVRAM_PORT0_BOOT_MODE;
2786                         pri_addr = NVRAM_PORT0_BOOT_PRI_TGT;
2787                         sec_addr = NVRAM_PORT0_BOOT_SEC_TGT;
2788                 } else if (func_num == 3) {
2789                         addr = NVRAM_PORT1_BOOT_MODE;
2790                         pri_addr = NVRAM_PORT1_BOOT_PRI_TGT;
2791                         sec_addr = NVRAM_PORT1_BOOT_SEC_TGT;
2792                 } else {
2793                         ret = QLA_ERROR;
2794                         goto exit_boot_info;
2795                 }
2796
2797                 /* Check Boot Mode */
2798                 val = rd_nvram_byte(ha, addr);
2799                 if (!(val & 0x07)) {
2800                         DEBUG2(ql4_printk(KERN_ERR, ha,
2801                                           "%s: Failed Boot options : 0x%x\n",
2802                                           __func__, val));
2803                         ret = QLA_ERROR;
2804                         goto exit_boot_info;
2805                 }
2806
2807                 /* get primary valid target index */
2808                 val = rd_nvram_byte(ha, pri_addr);
2809                 if (val & BIT_7)
2810                         ddb_index[0] = (val & 0x7f);
2811
2812                 /* get secondary valid target index */
2813                 val = rd_nvram_byte(ha, sec_addr);
2814                 if (val & BIT_7)
2815                         ddb_index[1] = (val & 0x7f);
2816
2817         } else if (is_qla8022(ha)) {
2818                 buf = dma_alloc_coherent(&ha->pdev->dev, size,
2819                                          &buf_dma, GFP_KERNEL);
2820                 if (!buf) {
2821                         DEBUG2(ql4_printk(KERN_ERR, ha,
2822                                           "%s: Unable to allocate dma buffer\n",
2823                                            __func__));
2824                         ret = QLA_ERROR;
2825                         goto exit_boot_info;
2826                 }
2827
2828                 if (ha->port_num == 0)
2829                         offset = BOOT_PARAM_OFFSET_PORT0;
2830                 else if (ha->port_num == 1)
2831                         offset = BOOT_PARAM_OFFSET_PORT1;
2832                 else {
2833                         ret = QLA_ERROR;
2834                         goto exit_boot_info_free;
2835                 }
2836                 addr = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_iscsi_param * 4) +
2837                        offset;
2838                 if (qla4xxx_get_flash(ha, buf_dma, addr,
2839                                       13 * sizeof(uint8_t)) != QLA_SUCCESS) {
2840                         DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: Get Flash"
2841                                           "failed\n", ha->host_no, __func__));
2842                         ret = QLA_ERROR;
2843                         goto exit_boot_info_free;
2844                 }
2845                 /* Check Boot Mode */
2846                 if (!(buf[1] & 0x07)) {
2847                         DEBUG2(ql4_printk(KERN_INFO, ha,
2848                                           "Failed: Boot options : 0x%x\n",
2849                                           buf[1]));
2850                         ret = QLA_ERROR;
2851                         goto exit_boot_info_free;
2852                 }
2853
2854                 /* get primary valid target index */
2855                 if (buf[2] & BIT_7)
2856                         ddb_index[0] = buf[2] & 0x7f;
2857
2858                 /* get secondary valid target index */
2859                 if (buf[11] & BIT_7)
2860                         ddb_index[1] = buf[11] & 0x7f;
2861         } else {
2862                 ret = QLA_ERROR;
2863                 goto exit_boot_info;
2864         }
2865
2866         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Primary target ID %d, Secondary"
2867                           " target ID %d\n", __func__, ddb_index[0],
2868                           ddb_index[1]));
2869
2870 exit_boot_info_free:
2871         dma_free_coherent(&ha->pdev->dev, size, buf, buf_dma);
2872 exit_boot_info:
2873         return ret;
2874 }
2875
2876 /**
2877  * qla4xxx_get_bidi_chap - Get a BIDI CHAP user and password
2878  * @ha: pointer to adapter structure
2879  * @username: CHAP username to be returned
2880  * @password: CHAP password to be returned
2881  *
2882  * If a boot entry has BIDI CHAP enabled then we need to set the BIDI CHAP
2883  * user and password in the sysfs entry in /sys/firmware/iscsi_boot#/.
2884  * So from the CHAP cache find the first BIDI CHAP entry and set it
2885  * to the boot record in sysfs.
2886  **/
2887 static int qla4xxx_get_bidi_chap(struct scsi_qla_host *ha, char *username,
2888                             char *password)
2889 {
2890         int i, ret = -EINVAL;
2891         int max_chap_entries = 0;
2892         struct ql4_chap_table *chap_table;
2893
2894         if (is_qla8022(ha))
2895                 max_chap_entries = (ha->hw.flt_chap_size / 2) /
2896                                                 sizeof(struct ql4_chap_table);
2897         else
2898                 max_chap_entries = MAX_CHAP_ENTRIES_40XX;
2899
2900         if (!ha->chap_list) {
2901                 ql4_printk(KERN_ERR, ha, "Do not have CHAP table cache\n");
2902                 return ret;
2903         }
2904
2905         mutex_lock(&ha->chap_sem);
2906         for (i = 0; i < max_chap_entries; i++) {
2907                 chap_table = (struct ql4_chap_table *)ha->chap_list + i;
2908                 if (chap_table->cookie !=
2909                     __constant_cpu_to_le16(CHAP_VALID_COOKIE)) {
2910                         continue;
2911                 }
2912
2913                 if (chap_table->flags & BIT_7) /* local */
2914                         continue;
2915
2916                 if (!(chap_table->flags & BIT_6)) /* Not BIDI */
2917                         continue;
2918
2919                 strncpy(password, chap_table->secret, QL4_CHAP_MAX_SECRET_LEN);
2920                 strncpy(username, chap_table->name, QL4_CHAP_MAX_NAME_LEN);
2921                 ret = 0;
2922                 break;
2923         }
2924         mutex_unlock(&ha->chap_sem);
2925
2926         return ret;
2927 }
2928
2929
2930 static int qla4xxx_get_boot_target(struct scsi_qla_host *ha,
2931                                    struct ql4_boot_session_info *boot_sess,
2932                                    uint16_t ddb_index)
2933 {
2934         struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
2935         struct dev_db_entry *fw_ddb_entry;
2936         dma_addr_t fw_ddb_entry_dma;
2937         uint16_t idx;
2938         uint16_t options;
2939         int ret = QLA_SUCCESS;
2940
2941         fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
2942                                           &fw_ddb_entry_dma, GFP_KERNEL);
2943         if (!fw_ddb_entry) {
2944                 DEBUG2(ql4_printk(KERN_ERR, ha,
2945                                   "%s: Unable to allocate dma buffer.\n",
2946                                   __func__));
2947                 ret = QLA_ERROR;
2948                 return ret;
2949         }
2950
2951         if (qla4xxx_bootdb_by_index(ha, fw_ddb_entry,
2952                                    fw_ddb_entry_dma, ddb_index)) {
2953                 DEBUG2(ql4_printk(KERN_ERR, ha,
2954                                   "%s: Flash DDB read Failed\n", __func__));
2955                 ret = QLA_ERROR;
2956                 goto exit_boot_target;
2957         }
2958
2959         /* Update target name and IP from DDB */
2960         memcpy(boot_sess->target_name, fw_ddb_entry->iscsi_name,
2961                min(sizeof(boot_sess->target_name),
2962                    sizeof(fw_ddb_entry->iscsi_name)));
2963
2964         options = le16_to_cpu(fw_ddb_entry->options);
2965         if (options & DDB_OPT_IPV6_DEVICE) {
2966                 memcpy(&boot_conn->dest_ipaddr.ip_address,
2967                        &fw_ddb_entry->ip_addr[0], IPv6_ADDR_LEN);
2968         } else {
2969                 boot_conn->dest_ipaddr.ip_type = 0x1;
2970                 memcpy(&boot_conn->dest_ipaddr.ip_address,
2971                        &fw_ddb_entry->ip_addr[0], IP_ADDR_LEN);
2972         }
2973
2974         boot_conn->dest_port = le16_to_cpu(fw_ddb_entry->port);
2975
2976         /* update chap information */
2977         idx = __le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
2978
2979         if (BIT_7 & le16_to_cpu(fw_ddb_entry->iscsi_options))   {
2980
2981                 DEBUG2(ql4_printk(KERN_INFO, ha, "Setting chap\n"));
2982
2983                 ret = qla4xxx_get_chap(ha, (char *)&boot_conn->chap.
2984                                        target_chap_name,
2985                                        (char *)&boot_conn->chap.target_secret,
2986                                        idx);
2987                 if (ret) {
2988                         ql4_printk(KERN_ERR, ha, "Failed to set chap\n");
2989                         ret = QLA_ERROR;
2990                         goto exit_boot_target;
2991                 }
2992
2993                 boot_conn->chap.target_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
2994                 boot_conn->chap.target_secret_length = QL4_CHAP_MAX_SECRET_LEN;
2995         }
2996
2997         if (BIT_4 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
2998
2999                 DEBUG2(ql4_printk(KERN_INFO, ha, "Setting BIDI chap\n"));
3000
3001                 ret = qla4xxx_get_bidi_chap(ha,
3002                                     (char *)&boot_conn->chap.intr_chap_name,
3003                                     (char *)&boot_conn->chap.intr_secret);
3004
3005                 if (ret) {
3006                         ql4_printk(KERN_ERR, ha, "Failed to set BIDI chap\n");
3007                         ret = QLA_ERROR;
3008                         goto exit_boot_target;
3009                 }
3010
3011                 boot_conn->chap.intr_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
3012                 boot_conn->chap.intr_secret_length = QL4_CHAP_MAX_SECRET_LEN;
3013         }
3014
3015 exit_boot_target:
3016         dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3017                           fw_ddb_entry, fw_ddb_entry_dma);
3018         return ret;
3019 }
3020
3021 static int qla4xxx_get_boot_info(struct scsi_qla_host *ha)
3022 {
3023         uint16_t ddb_index[2];
3024         int ret = QLA_ERROR;
3025         int rval;
3026
3027         memset(ddb_index, 0, sizeof(ddb_index));
3028         ddb_index[0] = 0xffff;
3029         ddb_index[1] = 0xffff;
3030         ret = get_fw_boot_info(ha, ddb_index);
3031         if (ret != QLA_SUCCESS) {
3032                 DEBUG2(ql4_printk(KERN_ERR, ha,
3033                                   "%s: Failed to set boot info.\n", __func__));
3034                 return ret;
3035         }
3036
3037         if (ddb_index[0] == 0xffff)
3038                 goto sec_target;
3039
3040         rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_pri_sess),
3041                                       ddb_index[0]);
3042         if (rval != QLA_SUCCESS) {
3043                 DEBUG2(ql4_printk(KERN_ERR, ha, "%s: Failed to get "
3044                                   "primary target\n", __func__));
3045         } else
3046                 ret = QLA_SUCCESS;
3047
3048 sec_target:
3049         if (ddb_index[1] == 0xffff)
3050                 goto exit_get_boot_info;
3051
3052         rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_sec_sess),
3053                                       ddb_index[1]);
3054         if (rval != QLA_SUCCESS) {
3055                 DEBUG2(ql4_printk(KERN_ERR, ha, "%s: Failed to get "
3056                                   "secondary target\n", __func__));
3057         } else
3058                 ret = QLA_SUCCESS;
3059
3060 exit_get_boot_info:
3061         return ret;
3062 }
3063
3064 static int qla4xxx_setup_boot_info(struct scsi_qla_host *ha)
3065 {
3066         struct iscsi_boot_kobj *boot_kobj;
3067
3068         if (qla4xxx_get_boot_info(ha) != QLA_SUCCESS)
3069                 return 0;
3070
3071         ha->boot_kset = iscsi_boot_create_host_kset(ha->host->host_no);
3072         if (!ha->boot_kset)
3073                 goto kset_free;
3074
3075         if (!scsi_host_get(ha->host))
3076                 goto kset_free;
3077         boot_kobj = iscsi_boot_create_target(ha->boot_kset, 0, ha,
3078                                              qla4xxx_show_boot_tgt_pri_info,
3079                                              qla4xxx_tgt_get_attr_visibility,
3080                                              qla4xxx_boot_release);
3081         if (!boot_kobj)
3082                 goto put_host;
3083
3084         if (!scsi_host_get(ha->host))
3085                 goto kset_free;
3086         boot_kobj = iscsi_boot_create_target(ha->boot_kset, 1, ha,
3087                                              qla4xxx_show_boot_tgt_sec_info,
3088                                              qla4xxx_tgt_get_attr_visibility,
3089                                              qla4xxx_boot_release);
3090         if (!boot_kobj)
3091                 goto put_host;
3092
3093         if (!scsi_host_get(ha->host))
3094                 goto kset_free;
3095         boot_kobj = iscsi_boot_create_initiator(ha->boot_kset, 0, ha,
3096                                                qla4xxx_show_boot_ini_info,
3097                                                qla4xxx_ini_get_attr_visibility,
3098                                                qla4xxx_boot_release);
3099         if (!boot_kobj)
3100                 goto put_host;
3101
3102         if (!scsi_host_get(ha->host))
3103                 goto kset_free;
3104         boot_kobj = iscsi_boot_create_ethernet(ha->boot_kset, 0, ha,
3105                                                qla4xxx_show_boot_eth_info,
3106                                                qla4xxx_eth_get_attr_visibility,
3107                                                qla4xxx_boot_release);
3108         if (!boot_kobj)
3109                 goto put_host;
3110
3111         return 0;
3112
3113 put_host:
3114         scsi_host_put(ha->host);
3115 kset_free:
3116         iscsi_boot_destroy_kset(ha->boot_kset);
3117         return -ENOMEM;
3118 }
3119
3120
3121 /**
3122  * qla4xxx_create chap_list - Create CHAP list from FLASH
3123  * @ha: pointer to adapter structure
3124  *
3125  * Read flash and make a list of CHAP entries, during login when a CHAP entry
3126  * is received, it will be checked in this list. If entry exist then the CHAP
3127  * entry index is set in the DDB. If CHAP entry does not exist in this list
3128  * then a new entry is added in FLASH in CHAP table and the index obtained is
3129  * used in the DDB.
3130  **/
3131 static void qla4xxx_create_chap_list(struct scsi_qla_host *ha)
3132 {
3133         int rval = 0;
3134         uint8_t *chap_flash_data = NULL;
3135         uint32_t offset;
3136         dma_addr_t chap_dma;
3137         uint32_t chap_size = 0;
3138
3139         if (is_qla40XX(ha))
3140                 chap_size = MAX_CHAP_ENTRIES_40XX  *
3141                                         sizeof(struct ql4_chap_table);
3142         else    /* Single region contains CHAP info for both
3143                  * ports which is divided into half for each port.
3144                  */
3145                 chap_size = ha->hw.flt_chap_size / 2;
3146
3147         chap_flash_data = dma_alloc_coherent(&ha->pdev->dev, chap_size,
3148                                           &chap_dma, GFP_KERNEL);
3149         if (!chap_flash_data) {
3150                 ql4_printk(KERN_ERR, ha, "No memory for chap_flash_data\n");
3151                 return;
3152         }
3153         if (is_qla40XX(ha))
3154                 offset = FLASH_CHAP_OFFSET;
3155         else {
3156                 offset = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_region_chap << 2);
3157                 if (ha->port_num == 1)
3158                         offset += chap_size;
3159         }
3160
3161         rval = qla4xxx_get_flash(ha, chap_dma, offset, chap_size);
3162         if (rval != QLA_SUCCESS)
3163                 goto exit_chap_list;
3164
3165         if (ha->chap_list == NULL)
3166                 ha->chap_list = vmalloc(chap_size);
3167         if (ha->chap_list == NULL) {
3168                 ql4_printk(KERN_ERR, ha, "No memory for ha->chap_list\n");
3169                 goto exit_chap_list;
3170         }
3171
3172         memcpy(ha->chap_list, chap_flash_data, chap_size);
3173
3174 exit_chap_list:
3175         dma_free_coherent(&ha->pdev->dev, chap_size,
3176                         chap_flash_data, chap_dma);
3177         return;
3178 }
3179
3180 /**
3181  * qla4xxx_probe_adapter - callback function to probe HBA
3182  * @pdev: pointer to pci_dev structure
3183  * @pci_device_id: pointer to pci_device entry
3184  *
3185  * This routine will probe for Qlogic 4xxx iSCSI host adapters.
3186  * It returns zero if successful. It also initializes all data necessary for
3187  * the driver.
3188  **/
3189 static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
3190                                            const struct pci_device_id *ent)
3191 {
3192         int ret = -ENODEV, status;
3193         struct Scsi_Host *host;
3194         struct scsi_qla_host *ha;
3195         uint8_t init_retry_count = 0;
3196         char buf[34];
3197         struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
3198         uint32_t dev_state;
3199
3200         if (pci_enable_device(pdev))
3201                 return -1;
3202
3203         host = iscsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha), 0);
3204         if (host == NULL) {
3205                 printk(KERN_WARNING
3206                        "qla4xxx: Couldn't allocate host from scsi layer!\n");
3207                 goto probe_disable_device;
3208         }
3209
3210         /* Clear our data area */
3211         ha = to_qla_host(host);
3212         memset(ha, 0, sizeof(*ha));
3213
3214         /* Save the information from PCI BIOS.  */
3215         ha->pdev = pdev;
3216         ha->host = host;
3217         ha->host_no = host->host_no;
3218
3219         pci_enable_pcie_error_reporting(pdev);
3220
3221         /* Setup Runtime configurable options */
3222         if (is_qla8022(ha)) {
3223                 ha->isp_ops = &qla4_8xxx_isp_ops;
3224                 rwlock_init(&ha->hw_lock);
3225                 ha->qdr_sn_window = -1;
3226                 ha->ddr_mn_window = -1;
3227                 ha->curr_window = 255;
3228                 ha->func_num = PCI_FUNC(ha->pdev->devfn);
3229                 nx_legacy_intr = &legacy_intr[ha->func_num];
3230                 ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
3231                 ha->nx_legacy_intr.tgt_status_reg =
3232                         nx_legacy_intr->tgt_status_reg;
3233                 ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
3234                 ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
3235         } else {
3236                 ha->isp_ops = &qla4xxx_isp_ops;
3237         }
3238
3239         /* Set EEH reset type to fundamental if required by hba */
3240         if (is_qla8022(ha))
3241                 pdev->needs_freset = 1;
3242
3243         /* Configure PCI I/O space. */
3244         ret = ha->isp_ops->iospace_config(ha);
3245         if (ret)
3246                 goto probe_failed_ioconfig;
3247
3248         ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
3249                    pdev->device, pdev->irq, ha->reg);
3250
3251         qla4xxx_config_dma_addressing(ha);
3252
3253         /* Initialize lists and spinlocks. */
3254         INIT_LIST_HEAD(&ha->free_srb_q);
3255
3256         mutex_init(&ha->mbox_sem);
3257         mutex_init(&ha->chap_sem);
3258         init_completion(&ha->mbx_intr_comp);
3259         init_completion(&ha->disable_acb_comp);
3260
3261         spin_lock_init(&ha->hardware_lock);
3262
3263         /* Allocate dma buffers */
3264         if (qla4xxx_mem_alloc(ha)) {
3265                 ql4_printk(KERN_WARNING, ha,
3266                     "[ERROR] Failed to allocate memory for adapter\n");
3267
3268                 ret = -ENOMEM;
3269                 goto probe_failed;
3270         }
3271
3272         host->cmd_per_lun = 3;
3273         host->max_channel = 0;
3274         host->max_lun = MAX_LUNS - 1;
3275         host->max_id = MAX_TARGETS;
3276         host->max_cmd_len = IOCB_MAX_CDB_LEN;
3277         host->can_queue = MAX_SRBS ;
3278         host->transportt = qla4xxx_scsi_transport;
3279
3280         ret = scsi_init_shared_tag_map(host, MAX_SRBS);
3281         if (ret) {
3282                 ql4_printk(KERN_WARNING, ha,
3283                            "%s: scsi_init_shared_tag_map failed\n", __func__);
3284                 goto probe_failed;
3285         }
3286
3287         pci_set_drvdata(pdev, ha);
3288
3289         ret = scsi_add_host(host, &pdev->dev);
3290         if (ret)
3291                 goto probe_failed;
3292
3293         if (is_qla8022(ha))
3294                 (void) qla4_8xxx_get_flash_info(ha);
3295
3296         /*
3297          * Initialize the Host adapter request/response queues and
3298          * firmware
3299          * NOTE: interrupts enabled upon successful completion
3300          */
3301         status = qla4xxx_initialize_adapter(ha);
3302         while ((!test_bit(AF_ONLINE, &ha->flags)) &&
3303             init_retry_count++ < MAX_INIT_RETRIES) {
3304
3305                 if (is_qla8022(ha)) {
3306                         qla4_8xxx_idc_lock(ha);
3307                         dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
3308                         qla4_8xxx_idc_unlock(ha);
3309                         if (dev_state == QLA82XX_DEV_FAILED) {
3310                                 ql4_printk(KERN_WARNING, ha, "%s: don't retry "
3311                                     "initialize adapter. H/W is in failed state\n",
3312                                     __func__);
3313                                 break;
3314                         }
3315                 }
3316                 DEBUG2(printk("scsi: %s: retrying adapter initialization "
3317                               "(%d)\n", __func__, init_retry_count));
3318
3319                 if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
3320                         continue;
3321
3322                 status = qla4xxx_initialize_adapter(ha);
3323         }
3324
3325         if (!test_bit(AF_ONLINE, &ha->flags)) {
3326                 ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
3327
3328                 if (is_qla8022(ha) && ql4xdontresethba) {
3329                         /* Put the device in failed state. */
3330                         DEBUG2(printk(KERN_ERR "HW STATE: FAILED\n"));
3331                         qla4_8xxx_idc_lock(ha);
3332                         qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
3333                             QLA82XX_DEV_FAILED);
3334                         qla4_8xxx_idc_unlock(ha);
3335                 }
3336                 ret = -ENODEV;
3337                 goto remove_host;
3338         }
3339
3340         /* Startup the kernel thread for this host adapter. */
3341         DEBUG2(printk("scsi: %s: Starting kernel thread for "
3342                       "qla4xxx_dpc\n", __func__));
3343         sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
3344         ha->dpc_thread = create_singlethread_workqueue(buf);
3345         if (!ha->dpc_thread) {
3346                 ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
3347                 ret = -ENODEV;
3348                 goto remove_host;
3349         }
3350         INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
3351
3352         sprintf(buf, "qla4xxx_%lu_task", ha->host_no);
3353         ha->task_wq = alloc_workqueue(buf, WQ_MEM_RECLAIM, 1);
3354         if (!ha->task_wq) {
3355                 ql4_printk(KERN_WARNING, ha, "Unable to start task thread!\n");
3356                 ret = -ENODEV;
3357                 goto remove_host;
3358         }
3359
3360         /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
3361          * (which is called indirectly by qla4xxx_initialize_adapter),
3362          * so that irqs will be registered after crbinit but before
3363          * mbx_intr_enable.
3364          */
3365         if (!is_qla8022(ha)) {
3366                 ret = qla4xxx_request_irqs(ha);
3367                 if (ret) {
3368                         ql4_printk(KERN_WARNING, ha, "Failed to reserve "
3369                             "interrupt %d already in use.\n", pdev->irq);
3370                         goto remove_host;
3371                 }
3372         }
3373
3374         pci_save_state(ha->pdev);
3375         ha->isp_ops->enable_intrs(ha);
3376
3377         /* Start timer thread. */
3378         qla4xxx_start_timer(ha, qla4xxx_timer, 1);
3379
3380         set_bit(AF_INIT_DONE, &ha->flags);
3381
3382         printk(KERN_INFO
3383                " QLogic iSCSI HBA Driver version: %s\n"
3384                "  QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
3385                qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
3386                ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
3387                ha->patch_number, ha->build_number);
3388
3389         qla4xxx_create_chap_list(ha);
3390
3391         if (qla4xxx_setup_boot_info(ha))
3392                 ql4_printk(KERN_ERR, ha, "%s:ISCSI boot info setup failed\n",
3393                            __func__);
3394
3395         qla4xxx_create_ifaces(ha);
3396         return 0;
3397
3398 remove_host:
3399         scsi_remove_host(ha->host);
3400
3401 probe_failed:
3402         qla4xxx_free_adapter(ha);
3403
3404 probe_failed_ioconfig:
3405         pci_disable_pcie_error_reporting(pdev);
3406         scsi_host_put(ha->host);
3407
3408 probe_disable_device:
3409         pci_disable_device(pdev);
3410
3411         return ret;
3412 }
3413
3414 /**
3415  * qla4xxx_prevent_other_port_reinit - prevent other port from re-initialize
3416  * @ha: pointer to adapter structure
3417  *
3418  * Mark the other ISP-4xxx port to indicate that the driver is being removed,
3419  * so that the other port will not re-initialize while in the process of
3420  * removing the ha due to driver unload or hba hotplug.
3421  **/
3422 static void qla4xxx_prevent_other_port_reinit(struct scsi_qla_host *ha)
3423 {
3424         struct scsi_qla_host *other_ha = NULL;
3425         struct pci_dev *other_pdev = NULL;
3426         int fn = ISP4XXX_PCI_FN_2;
3427
3428         /*iscsi function numbers for ISP4xxx is 1 and 3*/
3429         if (PCI_FUNC(ha->pdev->devfn) & BIT_1)
3430                 fn = ISP4XXX_PCI_FN_1;
3431
3432         other_pdev =
3433                 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
3434                 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
3435                 fn));
3436
3437         /* Get other_ha if other_pdev is valid and state is enable*/
3438         if (other_pdev) {
3439                 if (atomic_read(&other_pdev->enable_cnt)) {
3440                         other_ha = pci_get_drvdata(other_pdev);
3441                         if (other_ha) {
3442                                 set_bit(AF_HA_REMOVAL, &other_ha->flags);
3443                                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: "
3444                                     "Prevent %s reinit\n", __func__,
3445                                     dev_name(&other_ha->pdev->dev)));
3446                         }
3447                 }
3448                 pci_dev_put(other_pdev);
3449         }
3450 }
3451
3452 /**
3453  * qla4xxx_remove_adapter - calback function to remove adapter.
3454  * @pci_dev: PCI device pointer
3455  **/
3456 static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
3457 {
3458         struct scsi_qla_host *ha;
3459
3460         ha = pci_get_drvdata(pdev);
3461
3462         if (!is_qla8022(ha))
3463                 qla4xxx_prevent_other_port_reinit(ha);
3464
3465         /* destroy iface from sysfs */
3466         qla4xxx_destroy_ifaces(ha);
3467
3468         if (ha->boot_kset)
3469                 iscsi_boot_destroy_kset(ha->boot_kset);
3470
3471         scsi_remove_host(ha->host);
3472
3473         qla4xxx_free_adapter(ha);
3474
3475         scsi_host_put(ha->host);
3476
3477         pci_disable_pcie_error_reporting(pdev);
3478         pci_disable_device(pdev);
3479         pci_set_drvdata(pdev, NULL);
3480 }
3481
3482 /**
3483  * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
3484  * @ha: HA context
3485  *
3486  * At exit, the @ha's flags.enable_64bit_addressing set to indicated
3487  * supported addressing method.
3488  */
3489 static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
3490 {
3491         int retval;
3492
3493         /* Update our PCI device dma_mask for full 64 bit mask */
3494         if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
3495                 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
3496                         dev_dbg(&ha->pdev->dev,
3497                                   "Failed to set 64 bit PCI consistent mask; "
3498                                    "using 32 bit.\n");
3499                         retval = pci_set_consistent_dma_mask(ha->pdev,
3500                                                              DMA_BIT_MASK(32));
3501                 }
3502         } else
3503                 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
3504 }
3505
3506 static int qla4xxx_slave_alloc(struct scsi_device *sdev)
3507 {
3508         struct iscsi_cls_session *cls_sess;
3509         struct iscsi_session *sess;
3510         struct ddb_entry *ddb;
3511         int queue_depth = QL4_DEF_QDEPTH;
3512
3513         cls_sess = starget_to_session(sdev->sdev_target);
3514         sess = cls_sess->dd_data;
3515         ddb = sess->dd_data;
3516
3517         sdev->hostdata = ddb;
3518         sdev->tagged_supported = 1;
3519
3520         if (ql4xmaxqdepth != 0 && ql4xmaxqdepth <= 0xffffU)
3521                 queue_depth = ql4xmaxqdepth;
3522
3523         scsi_activate_tcq(sdev, queue_depth);
3524         return 0;
3525 }
3526
3527 static int qla4xxx_slave_configure(struct scsi_device *sdev)
3528 {
3529         sdev->tagged_supported = 1;
3530         return 0;
3531 }
3532
3533 static void qla4xxx_slave_destroy(struct scsi_device *sdev)
3534 {
3535         scsi_deactivate_tcq(sdev, 1);
3536 }
3537
3538 /**
3539  * qla4xxx_del_from_active_array - returns an active srb
3540  * @ha: Pointer to host adapter structure.
3541  * @index: index into the active_array
3542  *
3543  * This routine removes and returns the srb at the specified index
3544  **/
3545 struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
3546     uint32_t index)
3547 {
3548         struct srb *srb = NULL;
3549         struct scsi_cmnd *cmd = NULL;
3550
3551         cmd = scsi_host_find_tag(ha->host, index);
3552         if (!cmd)
3553                 return srb;
3554
3555         srb = (struct srb *)CMD_SP(cmd);
3556         if (!srb)
3557                 return srb;
3558
3559         /* update counters */
3560         if (srb->flags & SRB_DMA_VALID) {
3561                 ha->req_q_count += srb->iocb_cnt;
3562                 ha->iocb_cnt -= srb->iocb_cnt;
3563                 if (srb->cmd)
3564                         srb->cmd->host_scribble =
3565                                 (unsigned char *)(unsigned long) MAX_SRBS;
3566         }
3567         return srb;
3568 }
3569
3570 /**
3571  * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
3572  * @ha: Pointer to host adapter structure.
3573  * @cmd: Scsi Command to wait on.
3574  *
3575  * This routine waits for the command to be returned by the Firmware
3576  * for some max time.
3577  **/
3578 static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
3579                                       struct scsi_cmnd *cmd)
3580 {
3581         int done = 0;
3582         struct srb *rp;
3583         uint32_t max_wait_time = EH_WAIT_CMD_TOV;
3584         int ret = SUCCESS;
3585
3586         /* Dont wait on command if PCI error is being handled
3587          * by PCI AER driver
3588          */
3589         if (unlikely(pci_channel_offline(ha->pdev)) ||
3590             (test_bit(AF_EEH_BUSY, &ha->flags))) {
3591                 ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
3592                     ha->host_no, __func__);
3593                 return ret;
3594         }
3595
3596         do {
3597                 /* Checking to see if its returned to OS */
3598                 rp = (struct srb *) CMD_SP(cmd);
3599                 if (rp == NULL) {
3600                         done++;
3601                         break;
3602                 }
3603
3604                 msleep(2000);
3605         } while (max_wait_time--);
3606
3607         return done;
3608 }
3609
3610 /**
3611  * qla4xxx_wait_for_hba_online - waits for HBA to come online
3612  * @ha: Pointer to host adapter structure
3613  **/
3614 static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
3615 {
3616         unsigned long wait_online;
3617
3618         wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
3619         while (time_before(jiffies, wait_online)) {
3620
3621                 if (adapter_up(ha))
3622                         return QLA_SUCCESS;
3623
3624                 msleep(2000);
3625         }
3626
3627         return QLA_ERROR;
3628 }
3629
3630 /**
3631  * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
3632  * @ha: pointer to HBA
3633  * @t: target id
3634  * @l: lun id
3635  *
3636  * This function waits for all outstanding commands to a lun to complete. It
3637  * returns 0 if all pending commands are returned and 1 otherwise.
3638  **/
3639 static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
3640                                         struct scsi_target *stgt,
3641                                         struct scsi_device *sdev)
3642 {
3643         int cnt;
3644         int status = 0;
3645         struct scsi_cmnd *cmd;
3646
3647         /*
3648          * Waiting for all commands for the designated target or dev
3649          * in the active array
3650          */
3651         for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
3652                 cmd = scsi_host_find_tag(ha->host, cnt);
3653                 if (cmd && stgt == scsi_target(cmd->device) &&
3654                     (!sdev || sdev == cmd->device)) {
3655                         if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
3656                                 status++;
3657                                 break;
3658                         }
3659                 }
3660         }
3661         return status;
3662 }
3663
3664 /**
3665  * qla4xxx_eh_abort - callback for abort task.
3666  * @cmd: Pointer to Linux's SCSI command structure
3667  *
3668  * This routine is called by the Linux OS to abort the specified
3669  * command.
3670  **/
3671 static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
3672 {
3673         struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
3674         unsigned int id = cmd->device->id;
3675         unsigned int lun = cmd->device->lun;
3676         unsigned long flags;
3677         struct srb *srb = NULL;
3678         int ret = SUCCESS;
3679         int wait = 0;
3680
3681         ql4_printk(KERN_INFO, ha,
3682             "scsi%ld:%d:%d: Abort command issued cmd=%p\n",
3683             ha->host_no, id, lun, cmd);
3684
3685         spin_lock_irqsave(&ha->hardware_lock, flags);
3686         srb = (struct srb *) CMD_SP(cmd);
3687         if (!srb) {
3688                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3689                 return SUCCESS;
3690         }
3691         kref_get(&srb->srb_ref);
3692         spin_unlock_irqrestore(&ha->hardware_lock, flags);
3693
3694         if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
3695                 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
3696                     ha->host_no, id, lun));
3697                 ret = FAILED;
3698         } else {
3699                 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
3700                     ha->host_no, id, lun));
3701                 wait = 1;
3702         }
3703
3704         kref_put(&srb->srb_ref, qla4xxx_srb_compl);
3705
3706         /* Wait for command to complete */
3707         if (wait) {
3708                 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
3709                         DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
3710                             ha->host_no, id, lun));
3711                         ret = FAILED;
3712                 }
3713         }
3714
3715         ql4_printk(KERN_INFO, ha,
3716             "scsi%ld:%d:%d: Abort command - %s\n",
3717             ha->host_no, id, lun, (ret == SUCCESS) ? "succeeded" : "failed");
3718
3719         return ret;
3720 }
3721
3722 /**
3723  * qla4xxx_eh_device_reset - callback for target reset.
3724  * @cmd: Pointer to Linux's SCSI command structure
3725  *
3726  * This routine is called by the Linux OS to reset all luns on the
3727  * specified target.
3728  **/
3729 static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
3730 {
3731         struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
3732         struct ddb_entry *ddb_entry = cmd->device->hostdata;
3733         int ret = FAILED, stat;
3734
3735         if (!ddb_entry)
3736                 return ret;
3737
3738         ret = iscsi_block_scsi_eh(cmd);
3739         if (ret)
3740                 return ret;
3741         ret = FAILED;
3742
3743         ql4_printk(KERN_INFO, ha,
3744                    "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
3745                    cmd->device->channel, cmd->device->id, cmd->device->lun);
3746
3747         DEBUG2(printk(KERN_INFO
3748                       "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
3749                       "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
3750                       cmd, jiffies, cmd->request->timeout / HZ,
3751                       ha->dpc_flags, cmd->result, cmd->allowed));
3752
3753         /* FIXME: wait for hba to go online */
3754         stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
3755         if (stat != QLA_SUCCESS) {
3756                 ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
3757                 goto eh_dev_reset_done;
3758         }
3759
3760         if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
3761                                          cmd->device)) {
3762                 ql4_printk(KERN_INFO, ha,
3763                            "DEVICE RESET FAILED - waiting for "
3764                            "commands.\n");
3765                 goto eh_dev_reset_done;
3766         }
3767
3768         /* Send marker. */
3769         if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
3770                 MM_LUN_RESET) != QLA_SUCCESS)
3771                 goto eh_dev_reset_done;
3772
3773         ql4_printk(KERN_INFO, ha,
3774                    "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
3775                    ha->host_no, cmd->device->channel, cmd->device->id,
3776                    cmd->device->lun);
3777
3778         ret = SUCCESS;
3779
3780 eh_dev_reset_done:
3781
3782         return ret;
3783 }
3784
3785 /**
3786  * qla4xxx_eh_target_reset - callback for target reset.
3787  * @cmd: Pointer to Linux's SCSI command structure
3788  *
3789  * This routine is called by the Linux OS to reset the target.
3790  **/
3791 static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
3792 {
3793         struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
3794         struct ddb_entry *ddb_entry = cmd->device->hostdata;
3795         int stat, ret;
3796
3797         if (!ddb_entry)
3798                 return FAILED;
3799
3800         ret = iscsi_block_scsi_eh(cmd);
3801         if (ret)
3802                 return ret;
3803
3804         starget_printk(KERN_INFO, scsi_target(cmd->device),
3805                        "WARM TARGET RESET ISSUED.\n");
3806
3807         DEBUG2(printk(KERN_INFO
3808                       "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
3809                       "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
3810                       ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
3811                       ha->dpc_flags, cmd->result, cmd->allowed));
3812
3813         stat = qla4xxx_reset_target(ha, ddb_entry);
3814         if (stat != QLA_SUCCESS) {
3815                 starget_printk(KERN_INFO, scsi_target(cmd->device),
3816                                "WARM TARGET RESET FAILED.\n");
3817                 return FAILED;
3818         }
3819
3820         if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
3821                                          NULL)) {
3822                 starget_printk(KERN_INFO, scsi_target(cmd->device),
3823                                "WARM TARGET DEVICE RESET FAILED - "
3824                                "waiting for commands.\n");
3825                 return FAILED;
3826         }
3827
3828         /* Send marker. */
3829         if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
3830                 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
3831                 starget_printk(KERN_INFO, scsi_target(cmd->device),
3832                                "WARM TARGET DEVICE RESET FAILED - "
3833                                "marker iocb failed.\n");
3834                 return FAILED;
3835         }
3836
3837         starget_printk(KERN_INFO, scsi_target(cmd->device),
3838                        "WARM TARGET RESET SUCCEEDED.\n");
3839         return SUCCESS;
3840 }
3841
3842 /**
3843  * qla4xxx_eh_host_reset - kernel callback
3844  * @cmd: Pointer to Linux's SCSI command structure
3845  *
3846  * This routine is invoked by the Linux kernel to perform fatal error
3847  * recovery on the specified adapter.
3848  **/
3849 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
3850 {
3851         int return_status = FAILED;
3852         struct scsi_qla_host *ha;
3853
3854         ha = to_qla_host(cmd->device->host);
3855
3856         if (ql4xdontresethba) {
3857                 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
3858                      ha->host_no, __func__));
3859                 return FAILED;
3860         }
3861
3862         ql4_printk(KERN_INFO, ha,
3863                    "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
3864                    cmd->device->channel, cmd->device->id, cmd->device->lun);
3865
3866         if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
3867                 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host.  Adapter "
3868                               "DEAD.\n", ha->host_no, cmd->device->channel,
3869                               __func__));
3870
3871                 return FAILED;
3872         }
3873
3874         if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
3875                 if (is_qla8022(ha))
3876                         set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
3877                 else
3878                         set_bit(DPC_RESET_HA, &ha->dpc_flags);
3879         }
3880
3881         if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
3882                 return_status = SUCCESS;
3883
3884         ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
3885                    return_status == FAILED ? "FAILED" : "SUCCEEDED");
3886
3887         return return_status;
3888 }
3889
3890 static int qla4xxx_context_reset(struct scsi_qla_host *ha)
3891 {
3892         uint32_t mbox_cmd[MBOX_REG_COUNT];
3893         uint32_t mbox_sts[MBOX_REG_COUNT];
3894         struct addr_ctrl_blk_def *acb = NULL;
3895         uint32_t acb_len = sizeof(struct addr_ctrl_blk_def);
3896         int rval = QLA_SUCCESS;
3897         dma_addr_t acb_dma;
3898
3899         acb = dma_alloc_coherent(&ha->pdev->dev,
3900                                  sizeof(struct addr_ctrl_blk_def),
3901                                  &acb_dma, GFP_KERNEL);
3902         if (!acb) {
3903                 ql4_printk(KERN_ERR, ha, "%s: Unable to alloc acb\n",
3904                            __func__);
3905                 rval = -ENOMEM;
3906                 goto exit_port_reset;
3907         }
3908
3909         memset(acb, 0, acb_len);
3910
3911         rval = qla4xxx_get_acb(ha, acb_dma, PRIMARI_ACB, acb_len);
3912         if (rval != QLA_SUCCESS) {
3913                 rval = -EIO;
3914                 goto exit_free_acb;
3915         }
3916
3917         rval = qla4xxx_disable_acb(ha);
3918         if (rval != QLA_SUCCESS) {
3919                 rval = -EIO;
3920                 goto exit_free_acb;
3921         }
3922
3923         wait_for_completion_timeout(&ha->disable_acb_comp,
3924                                     DISABLE_ACB_TOV * HZ);
3925
3926         rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], acb_dma);
3927         if (rval != QLA_SUCCESS) {
3928                 rval = -EIO;
3929                 goto exit_free_acb;
3930         }
3931
3932 exit_free_acb:
3933         dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk_def),
3934                           acb, acb_dma);
3935 exit_port_reset:
3936         DEBUG2(ql4_printk(KERN_INFO, ha, "%s %s\n", __func__,
3937                           rval == QLA_SUCCESS ? "SUCCEEDED" : "FAILED"));
3938         return rval;
3939 }
3940
3941 static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type)
3942 {
3943         struct scsi_qla_host *ha = to_qla_host(shost);
3944         int rval = QLA_SUCCESS;
3945
3946         if (ql4xdontresethba) {
3947                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Don't Reset HBA\n",
3948                                   __func__));
3949                 rval = -EPERM;
3950                 goto exit_host_reset;
3951         }
3952
3953         rval = qla4xxx_wait_for_hba_online(ha);
3954         if (rval != QLA_SUCCESS) {
3955                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Unable to reset host "
3956                                   "adapter\n", __func__));
3957                 rval = -EIO;
3958                 goto exit_host_reset;
3959         }
3960
3961         if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
3962                 goto recover_adapter;
3963
3964         switch (reset_type) {
3965         case SCSI_ADAPTER_RESET:
3966                 set_bit(DPC_RESET_HA, &ha->dpc_flags);
3967                 break;
3968         case SCSI_FIRMWARE_RESET:
3969                 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
3970                         if (is_qla8022(ha))
3971                                 /* set firmware context reset */
3972                                 set_bit(DPC_RESET_HA_FW_CONTEXT,
3973                                         &ha->dpc_flags);
3974                         else {
3975                                 rval = qla4xxx_context_reset(ha);
3976                                 goto exit_host_reset;
3977                         }
3978                 }
3979                 break;
3980         }
3981
3982 recover_adapter:
3983         rval = qla4xxx_recover_adapter(ha);
3984         if (rval != QLA_SUCCESS) {
3985                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: recover adapter fail\n",
3986                                   __func__));
3987                 rval = -EIO;
3988         }
3989
3990 exit_host_reset:
3991         return rval;
3992 }
3993
3994 /* PCI AER driver recovers from all correctable errors w/o
3995  * driver intervention. For uncorrectable errors PCI AER
3996  * driver calls the following device driver's callbacks
3997  *
3998  * - Fatal Errors - link_reset
3999  * - Non-Fatal Errors - driver's pci_error_detected() which
4000  * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
4001  *
4002  * PCI AER driver calls
4003  * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
4004  *               returns RECOVERED or NEED_RESET if fw_hung
4005  * NEED_RESET - driver's slot_reset()
4006  * DISCONNECT - device is dead & cannot recover
4007  * RECOVERED - driver's pci_resume()
4008  */
4009 static pci_ers_result_t
4010 qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
4011 {
4012         struct scsi_qla_host *ha = pci_get_drvdata(pdev);
4013
4014         ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
4015             ha->host_no, __func__, state);
4016
4017         if (!is_aer_supported(ha))
4018                 return PCI_ERS_RESULT_NONE;
4019
4020         switch (state) {
4021         case pci_channel_io_normal:
4022                 clear_bit(AF_EEH_BUSY, &ha->flags);
4023                 return PCI_ERS_RESULT_CAN_RECOVER;
4024         case pci_channel_io_frozen:
4025                 set_bit(AF_EEH_BUSY, &ha->flags);
4026                 qla4xxx_mailbox_premature_completion(ha);
4027                 qla4xxx_free_irqs(ha);
4028                 pci_disable_device(pdev);
4029                 /* Return back all IOs */
4030                 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
4031                 return PCI_ERS_RESULT_NEED_RESET;
4032         case pci_channel_io_perm_failure:
4033                 set_bit(AF_EEH_BUSY, &ha->flags);
4034                 set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
4035                 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
4036                 return PCI_ERS_RESULT_DISCONNECT;
4037         }
4038         return PCI_ERS_RESULT_NEED_RESET;
4039 }
4040
4041 /**
4042  * qla4xxx_pci_mmio_enabled() gets called if
4043  * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
4044  * and read/write to the device still works.
4045  **/
4046 static pci_ers_result_t
4047 qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
4048 {
4049         struct scsi_qla_host *ha = pci_get_drvdata(pdev);
4050
4051         if (!is_aer_supported(ha))
4052                 return PCI_ERS_RESULT_NONE;
4053
4054         return PCI_ERS_RESULT_RECOVERED;
4055 }
4056
4057 static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
4058 {
4059         uint32_t rval = QLA_ERROR;
4060         uint32_t ret = 0;
4061         int fn;
4062         struct pci_dev *other_pdev = NULL;
4063
4064         ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
4065
4066         set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
4067
4068         if (test_bit(AF_ONLINE, &ha->flags)) {
4069                 clear_bit(AF_ONLINE, &ha->flags);
4070                 clear_bit(AF_LINK_UP, &ha->flags);
4071                 iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
4072                 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
4073         }
4074
4075         fn = PCI_FUNC(ha->pdev->devfn);
4076         while (fn > 0) {
4077                 fn--;
4078                 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
4079                     "func %x\n", ha->host_no, __func__, fn);
4080                 /* Get the pci device given the domain, bus,
4081                  * slot/function number */
4082                 other_pdev =
4083                     pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
4084                     ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
4085                     fn));
4086
4087                 if (!other_pdev)
4088                         continue;
4089
4090                 if (atomic_read(&other_pdev->enable_cnt)) {
4091                         ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
4092                             "func in enabled state%x\n", ha->host_no,
4093                             __func__, fn);
4094                         pci_dev_put(other_pdev);
4095                         break;
4096                 }
4097                 pci_dev_put(other_pdev);
4098         }
4099
4100         /* The first function on the card, the reset owner will
4101          * start & initialize the firmware. The other functions
4102          * on the card will reset the firmware context
4103          */
4104         if (!fn) {
4105                 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
4106                     "0x%x is the owner\n", ha->host_no, __func__,
4107                     ha->pdev->devfn);
4108
4109                 qla4_8xxx_idc_lock(ha);
4110                 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
4111                     QLA82XX_DEV_COLD);
4112
4113                 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
4114                     QLA82XX_IDC_VERSION);
4115
4116                 qla4_8xxx_idc_unlock(ha);
4117                 clear_bit(AF_FW_RECOVERY, &ha->flags);
4118                 rval = qla4xxx_initialize_adapter(ha);
4119                 qla4_8xxx_idc_lock(ha);
4120
4121                 if (rval != QLA_SUCCESS) {
4122                         ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
4123                             "FAILED\n", ha->host_no, __func__);
4124                         qla4_8xxx_clear_drv_active(ha);
4125                         qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
4126                             QLA82XX_DEV_FAILED);
4127                 } else {
4128                         ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
4129                             "READY\n", ha->host_no, __func__);
4130                         qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
4131                             QLA82XX_DEV_READY);
4132                         /* Clear driver state register */
4133                         qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
4134                         qla4_8xxx_set_drv_active(ha);
4135                         ret = qla4xxx_request_irqs(ha);
4136                         if (ret) {
4137                                 ql4_printk(KERN_WARNING, ha, "Failed to "
4138                                     "reserve interrupt %d already in use.\n",
4139                                     ha->pdev->irq);
4140                                 rval = QLA_ERROR;
4141                         } else {
4142                                 ha->isp_ops->enable_intrs(ha);
4143                                 rval = QLA_SUCCESS;
4144                         }
4145                 }
4146                 qla4_8xxx_idc_unlock(ha);
4147         } else {
4148                 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
4149                     "the reset owner\n", ha->host_no, __func__,
4150                     ha->pdev->devfn);
4151                 if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
4152                     QLA82XX_DEV_READY)) {
4153                         clear_bit(AF_FW_RECOVERY, &ha->flags);
4154                         rval = qla4xxx_initialize_adapter(ha);
4155                         if (rval == QLA_SUCCESS) {
4156                                 ret = qla4xxx_request_irqs(ha);
4157                                 if (ret) {
4158                                         ql4_printk(KERN_WARNING, ha, "Failed to"
4159                                             " reserve interrupt %d already in"
4160                                             " use.\n", ha->pdev->irq);
4161                                         rval = QLA_ERROR;
4162                                 } else {
4163                                         ha->isp_ops->enable_intrs(ha);
4164                                         rval = QLA_SUCCESS;
4165                                 }
4166                         }
4167                         qla4_8xxx_idc_lock(ha);
4168                         qla4_8xxx_set_drv_active(ha);
4169                         qla4_8xxx_idc_unlock(ha);
4170                 }
4171         }
4172         clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
4173         return rval;
4174 }
4175
4176 static pci_ers_result_t
4177 qla4xxx_pci_slot_reset(struct pci_dev *pdev)
4178 {
4179         pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
4180         struct scsi_qla_host *ha = pci_get_drvdata(pdev);
4181         int rc;
4182
4183         ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
4184             ha->host_no, __func__);
4185
4186         if (!is_aer_supported(ha))
4187                 return PCI_ERS_RESULT_NONE;
4188
4189         /* Restore the saved state of PCIe device -
4190          * BAR registers, PCI Config space, PCIX, MSI,
4191          * IOV states
4192          */
4193         pci_restore_state(pdev);
4194
4195         /* pci_restore_state() clears the saved_state flag of the device
4196          * save restored state which resets saved_state flag
4197          */
4198         pci_save_state(pdev);
4199
4200         /* Initialize device or resume if in suspended state */
4201         rc = pci_enable_device(pdev);
4202         if (rc) {
4203                 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Can't re-enable "
4204                     "device after reset\n", ha->host_no, __func__);
4205                 goto exit_slot_reset;
4206         }
4207
4208         ha->isp_ops->disable_intrs(ha);
4209
4210         if (is_qla8022(ha)) {
4211                 if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
4212                         ret = PCI_ERS_RESULT_RECOVERED;
4213                         goto exit_slot_reset;
4214                 } else
4215                         goto exit_slot_reset;
4216         }
4217
4218 exit_slot_reset:
4219         ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
4220             "device after reset\n", ha->host_no, __func__, ret);
4221         return ret;
4222 }
4223
4224 static void
4225 qla4xxx_pci_resume(struct pci_dev *pdev)
4226 {
4227         struct scsi_qla_host *ha = pci_get_drvdata(pdev);
4228         int ret;
4229
4230         ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
4231             ha->host_no, __func__);
4232
4233         ret = qla4xxx_wait_for_hba_online(ha);
4234         if (ret != QLA_SUCCESS) {
4235                 ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
4236                     "resume I/O from slot/link_reset\n", ha->host_no,
4237                      __func__);
4238         }
4239
4240         pci_cleanup_aer_uncorrect_error_status(pdev);
4241         clear_bit(AF_EEH_BUSY, &ha->flags);
4242 }
4243
4244 static struct pci_error_handlers qla4xxx_err_handler = {
4245         .error_detected = qla4xxx_pci_error_detected,
4246         .mmio_enabled = qla4xxx_pci_mmio_enabled,
4247         .slot_reset = qla4xxx_pci_slot_reset,
4248         .resume = qla4xxx_pci_resume,
4249 };
4250
4251 static struct pci_device_id qla4xxx_pci_tbl[] = {
4252         {
4253                 .vendor         = PCI_VENDOR_ID_QLOGIC,
4254                 .device         = PCI_DEVICE_ID_QLOGIC_ISP4010,
4255                 .subvendor      = PCI_ANY_ID,
4256                 .subdevice      = PCI_ANY_ID,
4257         },
4258         {
4259                 .vendor         = PCI_VENDOR_ID_QLOGIC,
4260                 .device         = PCI_DEVICE_ID_QLOGIC_ISP4022,
4261                 .subvendor      = PCI_ANY_ID,
4262                 .subdevice      = PCI_ANY_ID,
4263         },
4264         {
4265                 .vendor         = PCI_VENDOR_ID_QLOGIC,
4266                 .device         = PCI_DEVICE_ID_QLOGIC_ISP4032,
4267                 .subvendor      = PCI_ANY_ID,
4268                 .subdevice      = PCI_ANY_ID,
4269         },
4270         {
4271                 .vendor         = PCI_VENDOR_ID_QLOGIC,
4272                 .device         = PCI_DEVICE_ID_QLOGIC_ISP8022,
4273                 .subvendor      = PCI_ANY_ID,
4274                 .subdevice      = PCI_ANY_ID,
4275         },
4276         {0, 0},
4277 };
4278 MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
4279
4280 static struct pci_driver qla4xxx_pci_driver = {
4281         .name           = DRIVER_NAME,
4282         .id_table       = qla4xxx_pci_tbl,
4283         .probe          = qla4xxx_probe_adapter,
4284         .remove         = qla4xxx_remove_adapter,
4285         .err_handler = &qla4xxx_err_handler,
4286 };
4287
4288 static int __init qla4xxx_module_init(void)
4289 {
4290         int ret;
4291
4292         /* Allocate cache for SRBs. */
4293         srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
4294                                        SLAB_HWCACHE_ALIGN, NULL);
4295         if (srb_cachep == NULL) {
4296                 printk(KERN_ERR
4297                        "%s: Unable to allocate SRB cache..."
4298                        "Failing load!\n", DRIVER_NAME);
4299                 ret = -ENOMEM;
4300                 goto no_srp_cache;
4301         }
4302
4303         /* Derive version string. */
4304         strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
4305         if (ql4xextended_error_logging)
4306                 strcat(qla4xxx_version_str, "-debug");
4307
4308         qla4xxx_scsi_transport =
4309                 iscsi_register_transport(&qla4xxx_iscsi_transport);
4310         if (!qla4xxx_scsi_transport){
4311                 ret = -ENODEV;
4312                 goto release_srb_cache;
4313         }
4314
4315         ret = pci_register_driver(&qla4xxx_pci_driver);
4316         if (ret)
4317                 goto unregister_transport;
4318
4319         printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
4320         return 0;
4321
4322 unregister_transport:
4323         iscsi_unregister_transport(&qla4xxx_iscsi_transport);
4324 release_srb_cache:
4325         kmem_cache_destroy(srb_cachep);
4326 no_srp_cache:
4327         return ret;
4328 }
4329
4330 static void __exit qla4xxx_module_exit(void)
4331 {
4332         pci_unregister_driver(&qla4xxx_pci_driver);
4333         iscsi_unregister_transport(&qla4xxx_iscsi_transport);
4334         kmem_cache_destroy(srb_cachep);
4335 }
4336
4337 module_init(qla4xxx_module_init);
4338 module_exit(qla4xxx_module_exit);
4339
4340 MODULE_AUTHOR("QLogic Corporation");
4341 MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
4342 MODULE_LICENSE("GPL");
4343 MODULE_VERSION(QLA4XXX_DRIVER_VERSION);