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