b75590af8ed309051579c73caaf08dc25c73b091
[firefly-linux-kernel-4.4.55.git] / drivers / scsi / qla4xxx / ql4_os.c
1 /*
2  * QLogic iSCSI HBA Driver
3  * Copyright (c)  2003-2010 QLogic Corporation
4  *
5  * See LICENSE.qla4xxx for copyright and licensing details.
6  */
7 #include <linux/moduleparam.h>
8 #include <linux/slab.h>
9 #include <linux/blkdev.h>
10 #include <linux/iscsi_boot_sysfs.h>
11 #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) > LOGIN_TOV) &&
1634                  (le16_to_cpu(fw_ddb_entry->def_timeout) < LOGIN_TOV * 10) ?
1635                  le16_to_cpu(fw_ddb_entry->def_timeout) : LOGIN_TOV;
1636         ddb_entry->default_time2wait =
1637                                 le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
1638
1639         /* Update params */
1640         conn->max_recv_dlength = BYTE_UNITS *
1641                           le16_to_cpu(fw_ddb_entry->iscsi_max_rcv_data_seg_len);
1642
1643         conn->max_xmit_dlength = BYTE_UNITS *
1644                           le16_to_cpu(fw_ddb_entry->iscsi_max_snd_data_seg_len);
1645
1646         sess->initial_r2t_en =
1647                             (BIT_10 & le16_to_cpu(fw_ddb_entry->iscsi_options));
1648
1649         sess->max_r2t = le16_to_cpu(fw_ddb_entry->iscsi_max_outsnd_r2t);
1650
1651         sess->imm_data_en = (BIT_11 & le16_to_cpu(fw_ddb_entry->iscsi_options));
1652
1653         sess->first_burst = BYTE_UNITS *
1654                                le16_to_cpu(fw_ddb_entry->iscsi_first_burst_len);
1655
1656         sess->max_burst = BYTE_UNITS *
1657                                  le16_to_cpu(fw_ddb_entry->iscsi_max_burst_len);
1658
1659         sess->time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
1660
1661         sess->time2retain = le16_to_cpu(fw_ddb_entry->iscsi_def_time2retain);
1662
1663         sess->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
1664
1665         memcpy(sess->initiatorname, ha->name_string,
1666                min(sizeof(ha->name_string), sizeof(sess->initiatorname)));
1667
1668 exit_session_conn_param:
1669         if (fw_ddb_entry)
1670                 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1671                                   fw_ddb_entry, fw_ddb_entry_dma);
1672 }
1673
1674 /*
1675  * Timer routines
1676  */
1677
1678 static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
1679                                 unsigned long interval)
1680 {
1681         DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
1682                      __func__, ha->host->host_no));
1683         init_timer(&ha->timer);
1684         ha->timer.expires = jiffies + interval * HZ;
1685         ha->timer.data = (unsigned long)ha;
1686         ha->timer.function = (void (*)(unsigned long))func;
1687         add_timer(&ha->timer);
1688         ha->timer_active = 1;
1689 }
1690
1691 static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
1692 {
1693         del_timer_sync(&ha->timer);
1694         ha->timer_active = 0;
1695 }
1696
1697 /***
1698  * qla4xxx_mark_device_missing - blocks the session
1699  * @cls_session: Pointer to the session to be blocked
1700  * @ddb_entry: Pointer to device database entry
1701  *
1702  * This routine marks a device missing and close connection.
1703  **/
1704 void qla4xxx_mark_device_missing(struct iscsi_cls_session *cls_session)
1705 {
1706         iscsi_block_session(cls_session);
1707 }
1708
1709 /**
1710  * qla4xxx_mark_all_devices_missing - mark all devices as missing.
1711  * @ha: Pointer to host adapter structure.
1712  *
1713  * This routine marks a device missing and resets the relogin retry count.
1714  **/
1715 void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
1716 {
1717         iscsi_host_for_each_session(ha->host, qla4xxx_mark_device_missing);
1718 }
1719
1720 static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
1721                                        struct ddb_entry *ddb_entry,
1722                                        struct scsi_cmnd *cmd)
1723 {
1724         struct srb *srb;
1725
1726         srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
1727         if (!srb)
1728                 return srb;
1729
1730         kref_init(&srb->srb_ref);
1731         srb->ha = ha;
1732         srb->ddb = ddb_entry;
1733         srb->cmd = cmd;
1734         srb->flags = 0;
1735         CMD_SP(cmd) = (void *)srb;
1736
1737         return srb;
1738 }
1739
1740 static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
1741 {
1742         struct scsi_cmnd *cmd = srb->cmd;
1743
1744         if (srb->flags & SRB_DMA_VALID) {
1745                 scsi_dma_unmap(cmd);
1746                 srb->flags &= ~SRB_DMA_VALID;
1747         }
1748         CMD_SP(cmd) = NULL;
1749 }
1750
1751 void qla4xxx_srb_compl(struct kref *ref)
1752 {
1753         struct srb *srb = container_of(ref, struct srb, srb_ref);
1754         struct scsi_cmnd *cmd = srb->cmd;
1755         struct scsi_qla_host *ha = srb->ha;
1756
1757         qla4xxx_srb_free_dma(ha, srb);
1758
1759         mempool_free(srb, ha->srb_mempool);
1760
1761         cmd->scsi_done(cmd);
1762 }
1763
1764 /**
1765  * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
1766  * @host: scsi host
1767  * @cmd: Pointer to Linux's SCSI command structure
1768  *
1769  * Remarks:
1770  * This routine is invoked by Linux to send a SCSI command to the driver.
1771  * The mid-level driver tries to ensure that queuecommand never gets
1772  * invoked concurrently with itself or the interrupt handler (although
1773  * the interrupt handler may call this routine as part of request-
1774  * completion handling).   Unfortunely, it sometimes calls the scheduler
1775  * in interrupt context which is a big NO! NO!.
1776  **/
1777 static int qla4xxx_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
1778 {
1779         struct scsi_qla_host *ha = to_qla_host(host);
1780         struct ddb_entry *ddb_entry = cmd->device->hostdata;
1781         struct iscsi_cls_session *sess = ddb_entry->sess;
1782         struct srb *srb;
1783         int rval;
1784
1785         if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1786                 if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
1787                         cmd->result = DID_NO_CONNECT << 16;
1788                 else
1789                         cmd->result = DID_REQUEUE << 16;
1790                 goto qc_fail_command;
1791         }
1792
1793         if (!sess) {
1794                 cmd->result = DID_IMM_RETRY << 16;
1795                 goto qc_fail_command;
1796         }
1797
1798         rval = iscsi_session_chkready(sess);
1799         if (rval) {
1800                 cmd->result = rval;
1801                 goto qc_fail_command;
1802         }
1803
1804         if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1805             test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
1806             test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1807             test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
1808             test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
1809             !test_bit(AF_ONLINE, &ha->flags) ||
1810             !test_bit(AF_LINK_UP, &ha->flags) ||
1811             test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
1812                 goto qc_host_busy;
1813
1814         srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd);
1815         if (!srb)
1816                 goto qc_host_busy;
1817
1818         rval = qla4xxx_send_command_to_isp(ha, srb);
1819         if (rval != QLA_SUCCESS)
1820                 goto qc_host_busy_free_sp;
1821
1822         return 0;
1823
1824 qc_host_busy_free_sp:
1825         qla4xxx_srb_free_dma(ha, srb);
1826         mempool_free(srb, ha->srb_mempool);
1827
1828 qc_host_busy:
1829         return SCSI_MLQUEUE_HOST_BUSY;
1830
1831 qc_fail_command:
1832         cmd->scsi_done(cmd);
1833
1834         return 0;
1835 }
1836
1837 /**
1838  * qla4xxx_mem_free - frees memory allocated to adapter
1839  * @ha: Pointer to host adapter structure.
1840  *
1841  * Frees memory previously allocated by qla4xxx_mem_alloc
1842  **/
1843 static void qla4xxx_mem_free(struct scsi_qla_host *ha)
1844 {
1845         if (ha->queues)
1846                 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
1847                                   ha->queues_dma);
1848
1849         ha->queues_len = 0;
1850         ha->queues = NULL;
1851         ha->queues_dma = 0;
1852         ha->request_ring = NULL;
1853         ha->request_dma = 0;
1854         ha->response_ring = NULL;
1855         ha->response_dma = 0;
1856         ha->shadow_regs = NULL;
1857         ha->shadow_regs_dma = 0;
1858
1859         /* Free srb pool. */
1860         if (ha->srb_mempool)
1861                 mempool_destroy(ha->srb_mempool);
1862
1863         ha->srb_mempool = NULL;
1864
1865         if (ha->chap_dma_pool)
1866                 dma_pool_destroy(ha->chap_dma_pool);
1867
1868         if (ha->chap_list)
1869                 vfree(ha->chap_list);
1870         ha->chap_list = NULL;
1871
1872         if (ha->fw_ddb_dma_pool)
1873                 dma_pool_destroy(ha->fw_ddb_dma_pool);
1874
1875         /* release io space registers  */
1876         if (is_qla8022(ha)) {
1877                 if (ha->nx_pcibase)
1878                         iounmap(
1879                             (struct device_reg_82xx __iomem *)ha->nx_pcibase);
1880         } else if (ha->reg)
1881                 iounmap(ha->reg);
1882         pci_release_regions(ha->pdev);
1883 }
1884
1885 /**
1886  * qla4xxx_mem_alloc - allocates memory for use by adapter.
1887  * @ha: Pointer to host adapter structure
1888  *
1889  * Allocates DMA memory for request and response queues. Also allocates memory
1890  * for srbs.
1891  **/
1892 static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
1893 {
1894         unsigned long align;
1895
1896         /* Allocate contiguous block of DMA memory for queues. */
1897         ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
1898                           (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
1899                           sizeof(struct shadow_regs) +
1900                           MEM_ALIGN_VALUE +
1901                           (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
1902         ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
1903                                         &ha->queues_dma, GFP_KERNEL);
1904         if (ha->queues == NULL) {
1905                 ql4_printk(KERN_WARNING, ha,
1906                     "Memory Allocation failed - queues.\n");
1907
1908                 goto mem_alloc_error_exit;
1909         }
1910         memset(ha->queues, 0, ha->queues_len);
1911
1912         /*
1913          * As per RISC alignment requirements -- the bus-address must be a
1914          * multiple of the request-ring size (in bytes).
1915          */
1916         align = 0;
1917         if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
1918                 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
1919                                            (MEM_ALIGN_VALUE - 1));
1920
1921         /* Update request and response queue pointers. */
1922         ha->request_dma = ha->queues_dma + align;
1923         ha->request_ring = (struct queue_entry *) (ha->queues + align);
1924         ha->response_dma = ha->queues_dma + align +
1925                 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
1926         ha->response_ring = (struct queue_entry *) (ha->queues + align +
1927                                                     (REQUEST_QUEUE_DEPTH *
1928                                                      QUEUE_SIZE));
1929         ha->shadow_regs_dma = ha->queues_dma + align +
1930                 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
1931                 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
1932         ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
1933                                                   (REQUEST_QUEUE_DEPTH *
1934                                                    QUEUE_SIZE) +
1935                                                   (RESPONSE_QUEUE_DEPTH *
1936                                                    QUEUE_SIZE));
1937
1938         /* Allocate memory for srb pool. */
1939         ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
1940                                          mempool_free_slab, srb_cachep);
1941         if (ha->srb_mempool == NULL) {
1942                 ql4_printk(KERN_WARNING, ha,
1943                     "Memory Allocation failed - SRB Pool.\n");
1944
1945                 goto mem_alloc_error_exit;
1946         }
1947
1948         ha->chap_dma_pool = dma_pool_create("ql4_chap", &ha->pdev->dev,
1949                                             CHAP_DMA_BLOCK_SIZE, 8, 0);
1950
1951         if (ha->chap_dma_pool == NULL) {
1952                 ql4_printk(KERN_WARNING, ha,
1953                     "%s: chap_dma_pool allocation failed..\n", __func__);
1954                 goto mem_alloc_error_exit;
1955         }
1956
1957         ha->fw_ddb_dma_pool = dma_pool_create("ql4_fw_ddb", &ha->pdev->dev,
1958                                               DDB_DMA_BLOCK_SIZE, 8, 0);
1959
1960         if (ha->fw_ddb_dma_pool == NULL) {
1961                 ql4_printk(KERN_WARNING, ha,
1962                            "%s: fw_ddb_dma_pool allocation failed..\n",
1963                            __func__);
1964                 goto mem_alloc_error_exit;
1965         }
1966
1967         return QLA_SUCCESS;
1968
1969 mem_alloc_error_exit:
1970         qla4xxx_mem_free(ha);
1971         return QLA_ERROR;
1972 }
1973
1974 /**
1975  * qla4_8xxx_check_temp - Check the ISP82XX temperature.
1976  * @ha: adapter block pointer.
1977  *
1978  * Note: The caller should not hold the idc lock.
1979  **/
1980 static int qla4_8xxx_check_temp(struct scsi_qla_host *ha)
1981 {
1982         uint32_t temp, temp_state, temp_val;
1983         int status = QLA_SUCCESS;
1984
1985         temp = qla4_8xxx_rd_32(ha, CRB_TEMP_STATE);
1986
1987         temp_state = qla82xx_get_temp_state(temp);
1988         temp_val = qla82xx_get_temp_val(temp);
1989
1990         if (temp_state == QLA82XX_TEMP_PANIC) {
1991                 ql4_printk(KERN_WARNING, ha, "Device temperature %d degrees C"
1992                            " exceeds maximum allowed. Hardware has been shut"
1993                            " down.\n", temp_val);
1994                 status = QLA_ERROR;
1995         } else if (temp_state == QLA82XX_TEMP_WARN) {
1996                 if (ha->temperature == QLA82XX_TEMP_NORMAL)
1997                         ql4_printk(KERN_WARNING, ha, "Device temperature %d"
1998                                    " degrees C exceeds operating range."
1999                                    " Immediate action needed.\n", temp_val);
2000         } else {
2001                 if (ha->temperature == QLA82XX_TEMP_WARN)
2002                         ql4_printk(KERN_INFO, ha, "Device temperature is"
2003                                    " now %d degrees C in normal range.\n",
2004                                    temp_val);
2005         }
2006         ha->temperature = temp_state;
2007         return status;
2008 }
2009
2010 /**
2011  * qla4_8xxx_check_fw_alive  - Check firmware health
2012  * @ha: Pointer to host adapter structure.
2013  *
2014  * Context: Interrupt
2015  **/
2016 static int qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
2017 {
2018         uint32_t fw_heartbeat_counter;
2019         int status = QLA_SUCCESS;
2020
2021         fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
2022         /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
2023         if (fw_heartbeat_counter == 0xffffffff) {
2024                 DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
2025                     "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
2026                     ha->host_no, __func__));
2027                 return status;
2028         }
2029
2030         if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
2031                 ha->seconds_since_last_heartbeat++;
2032                 /* FW not alive after 2 seconds */
2033                 if (ha->seconds_since_last_heartbeat == 2) {
2034                         ha->seconds_since_last_heartbeat = 0;
2035
2036                         ql4_printk(KERN_INFO, ha,
2037                                    "scsi(%ld): %s, Dumping hw/fw registers:\n "
2038                                    " PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2:"
2039                                    " 0x%x,\n PEG_NET_0_PC: 0x%x, PEG_NET_1_PC:"
2040                                    " 0x%x,\n PEG_NET_2_PC: 0x%x, PEG_NET_3_PC:"
2041                                    " 0x%x,\n PEG_NET_4_PC: 0x%x\n",
2042                                    ha->host_no, __func__,
2043                                    qla4_8xxx_rd_32(ha,
2044                                                    QLA82XX_PEG_HALT_STATUS1),
2045                                    qla4_8xxx_rd_32(ha,
2046                                                    QLA82XX_PEG_HALT_STATUS2),
2047                                    qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_0 +
2048                                                    0x3c),
2049                                    qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_1 +
2050                                                    0x3c),
2051                                    qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_2 +
2052                                                    0x3c),
2053                                    qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_3 +
2054                                                    0x3c),
2055                                    qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_4 +
2056                                                    0x3c));
2057                         status = QLA_ERROR;
2058                 }
2059         } else
2060                 ha->seconds_since_last_heartbeat = 0;
2061
2062         ha->fw_heartbeat_counter = fw_heartbeat_counter;
2063         return status;
2064 }
2065
2066 /**
2067  * qla4_8xxx_watchdog - Poll dev state
2068  * @ha: Pointer to host adapter structure.
2069  *
2070  * Context: Interrupt
2071  **/
2072 void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
2073 {
2074         uint32_t dev_state, halt_status;
2075
2076         /* don't poll if reset is going on */
2077         if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
2078             test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
2079             test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags))) {
2080                 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
2081
2082                 if (qla4_8xxx_check_temp(ha)) {
2083                         ql4_printk(KERN_INFO, ha, "disabling pause"
2084                                    " transmit on port 0 & 1.\n");
2085                         qla4_8xxx_wr_32(ha, QLA82XX_CRB_NIU + 0x98,
2086                                         CRB_NIU_XG_PAUSE_CTL_P0 |
2087                                         CRB_NIU_XG_PAUSE_CTL_P1);
2088                         set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
2089                         qla4xxx_wake_dpc(ha);
2090                 } else if (dev_state == QLA82XX_DEV_NEED_RESET &&
2091                     !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
2092                         if (!ql4xdontresethba) {
2093                                 ql4_printk(KERN_INFO, ha, "%s: HW State: "
2094                                     "NEED RESET!\n", __func__);
2095                                 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2096                                 qla4xxx_wake_dpc(ha);
2097                         }
2098                 } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
2099                     !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
2100                         ql4_printk(KERN_INFO, ha, "%s: HW State: NEED QUIES!\n",
2101                             __func__);
2102                         set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
2103                         qla4xxx_wake_dpc(ha);
2104                 } else  {
2105                         /* Check firmware health */
2106                         if (qla4_8xxx_check_fw_alive(ha)) {
2107                                 ql4_printk(KERN_INFO, ha, "disabling pause"
2108                                            " transmit on port 0 & 1.\n");
2109                                 qla4_8xxx_wr_32(ha, QLA82XX_CRB_NIU + 0x98,
2110                                                 CRB_NIU_XG_PAUSE_CTL_P0 |
2111                                                 CRB_NIU_XG_PAUSE_CTL_P1);
2112                                 halt_status = qla4_8xxx_rd_32(ha,
2113                                                 QLA82XX_PEG_HALT_STATUS1);
2114
2115                                 if (LSW(MSB(halt_status)) == 0x67)
2116                                         ql4_printk(KERN_ERR, ha, "%s:"
2117                                                    " Firmware aborted with"
2118                                                    " error code 0x00006700."
2119                                                    " Device is being reset\n",
2120                                                    __func__);
2121
2122                                 /* Since we cannot change dev_state in interrupt
2123                                  * context, set appropriate DPC flag then wakeup
2124                                  * DPC */
2125                                 if (halt_status & HALT_STATUS_UNRECOVERABLE)
2126                                         set_bit(DPC_HA_UNRECOVERABLE,
2127                                                 &ha->dpc_flags);
2128                                 else {
2129                                         ql4_printk(KERN_INFO, ha, "%s: detect "
2130                                                    "abort needed!\n", __func__);
2131                                         set_bit(DPC_RESET_HA, &ha->dpc_flags);
2132                                 }
2133                                 qla4xxx_mailbox_premature_completion(ha);
2134                                 qla4xxx_wake_dpc(ha);
2135                         }
2136                 }
2137         }
2138 }
2139
2140 static void qla4xxx_check_relogin_flash_ddb(struct iscsi_cls_session *cls_sess)
2141 {
2142         struct iscsi_session *sess;
2143         struct ddb_entry *ddb_entry;
2144         struct scsi_qla_host *ha;
2145
2146         sess = cls_sess->dd_data;
2147         ddb_entry = sess->dd_data;
2148         ha = ddb_entry->ha;
2149
2150         if (!(ddb_entry->ddb_type == FLASH_DDB))
2151                 return;
2152
2153         if (adapter_up(ha) && !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
2154             !iscsi_is_session_online(cls_sess)) {
2155                 if (atomic_read(&ddb_entry->retry_relogin_timer) !=
2156                     INVALID_ENTRY) {
2157                         if (atomic_read(&ddb_entry->retry_relogin_timer) ==
2158                                         0) {
2159                                 atomic_set(&ddb_entry->retry_relogin_timer,
2160                                            INVALID_ENTRY);
2161                                 set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
2162                                 set_bit(DF_RELOGIN, &ddb_entry->flags);
2163                                 DEBUG2(ql4_printk(KERN_INFO, ha,
2164                                        "%s: index [%d] login device\n",
2165                                         __func__, ddb_entry->fw_ddb_index));
2166                         } else
2167                                 atomic_dec(&ddb_entry->retry_relogin_timer);
2168                 }
2169         }
2170
2171         /* Wait for relogin to timeout */
2172         if (atomic_read(&ddb_entry->relogin_timer) &&
2173             (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
2174                 /*
2175                  * If the relogin times out and the device is
2176                  * still NOT ONLINE then try and relogin again.
2177                  */
2178                 if (!iscsi_is_session_online(cls_sess)) {
2179                         /* Reset retry relogin timer */
2180                         atomic_inc(&ddb_entry->relogin_retry_count);
2181                         DEBUG2(ql4_printk(KERN_INFO, ha,
2182                                 "%s: index[%d] relogin timed out-retrying"
2183                                 " relogin (%d), retry (%d)\n", __func__,
2184                                 ddb_entry->fw_ddb_index,
2185                                 atomic_read(&ddb_entry->relogin_retry_count),
2186                                 ddb_entry->default_time2wait + 4));
2187                         set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
2188                         atomic_set(&ddb_entry->retry_relogin_timer,
2189                                    ddb_entry->default_time2wait + 4);
2190                 }
2191         }
2192 }
2193
2194 /**
2195  * qla4xxx_timer - checks every second for work to do.
2196  * @ha: Pointer to host adapter structure.
2197  **/
2198 static void qla4xxx_timer(struct scsi_qla_host *ha)
2199 {
2200         int start_dpc = 0;
2201         uint16_t w;
2202
2203         iscsi_host_for_each_session(ha->host, qla4xxx_check_relogin_flash_ddb);
2204
2205         /* If we are in the middle of AER/EEH processing
2206          * skip any processing and reschedule the timer
2207          */
2208         if (test_bit(AF_EEH_BUSY, &ha->flags)) {
2209                 mod_timer(&ha->timer, jiffies + HZ);
2210                 return;
2211         }
2212
2213         /* Hardware read to trigger an EEH error during mailbox waits. */
2214         if (!pci_channel_offline(ha->pdev))
2215                 pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
2216
2217         if (is_qla8022(ha)) {
2218                 qla4_8xxx_watchdog(ha);
2219         }
2220
2221         if (!is_qla8022(ha)) {
2222                 /* Check for heartbeat interval. */
2223                 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
2224                     ha->heartbeat_interval != 0) {
2225                         ha->seconds_since_last_heartbeat++;
2226                         if (ha->seconds_since_last_heartbeat >
2227                             ha->heartbeat_interval + 2)
2228                                 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2229                 }
2230         }
2231
2232         /* Wakeup the dpc routine for this adapter, if needed. */
2233         if (start_dpc ||
2234              test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
2235              test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
2236              test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
2237              test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
2238              test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
2239              test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
2240              test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
2241              test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
2242              test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
2243              test_bit(DPC_AEN, &ha->dpc_flags)) {
2244                 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
2245                               " - dpc flags = 0x%lx\n",
2246                               ha->host_no, __func__, ha->dpc_flags));
2247                 qla4xxx_wake_dpc(ha);
2248         }
2249
2250         /* Reschedule timer thread to call us back in one second */
2251         mod_timer(&ha->timer, jiffies + HZ);
2252
2253         DEBUG2(ha->seconds_since_last_intr++);
2254 }
2255
2256 /**
2257  * qla4xxx_cmd_wait - waits for all outstanding commands to complete
2258  * @ha: Pointer to host adapter structure.
2259  *
2260  * This routine stalls the driver until all outstanding commands are returned.
2261  * Caller must release the Hardware Lock prior to calling this routine.
2262  **/
2263 static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
2264 {
2265         uint32_t index = 0;
2266         unsigned long flags;
2267         struct scsi_cmnd *cmd;
2268
2269         unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
2270
2271         DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
2272             "complete\n", WAIT_CMD_TOV));
2273
2274         while (!time_after_eq(jiffies, wtime)) {
2275                 spin_lock_irqsave(&ha->hardware_lock, flags);
2276                 /* Find a command that hasn't completed. */
2277                 for (index = 0; index < ha->host->can_queue; index++) {
2278                         cmd = scsi_host_find_tag(ha->host, index);
2279                         /*
2280                          * We cannot just check if the index is valid,
2281                          * becase if we are run from the scsi eh, then
2282                          * the scsi/block layer is going to prevent
2283                          * the tag from being released.
2284                          */
2285                         if (cmd != NULL && CMD_SP(cmd))
2286                                 break;
2287                 }
2288                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2289
2290                 /* If No Commands are pending, wait is complete */
2291                 if (index == ha->host->can_queue)
2292                         return QLA_SUCCESS;
2293
2294                 msleep(1000);
2295         }
2296         /* If we timed out on waiting for commands to come back
2297          * return ERROR. */
2298         return QLA_ERROR;
2299 }
2300
2301 int qla4xxx_hw_reset(struct scsi_qla_host *ha)
2302 {
2303         uint32_t ctrl_status;
2304         unsigned long flags = 0;
2305
2306         DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
2307
2308         if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
2309                 return QLA_ERROR;
2310
2311         spin_lock_irqsave(&ha->hardware_lock, flags);
2312
2313         /*
2314          * If the SCSI Reset Interrupt bit is set, clear it.
2315          * Otherwise, the Soft Reset won't work.
2316          */
2317         ctrl_status = readw(&ha->reg->ctrl_status);
2318         if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
2319                 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
2320
2321         /* Issue Soft Reset */
2322         writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
2323         readl(&ha->reg->ctrl_status);
2324
2325         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2326         return QLA_SUCCESS;
2327 }
2328
2329 /**
2330  * qla4xxx_soft_reset - performs soft reset.
2331  * @ha: Pointer to host adapter structure.
2332  **/
2333 int qla4xxx_soft_reset(struct scsi_qla_host *ha)
2334 {
2335         uint32_t max_wait_time;
2336         unsigned long flags = 0;
2337         int status;
2338         uint32_t ctrl_status;
2339
2340         status = qla4xxx_hw_reset(ha);
2341         if (status != QLA_SUCCESS)
2342                 return status;
2343
2344         status = QLA_ERROR;
2345         /* Wait until the Network Reset Intr bit is cleared */
2346         max_wait_time = RESET_INTR_TOV;
2347         do {
2348                 spin_lock_irqsave(&ha->hardware_lock, flags);
2349                 ctrl_status = readw(&ha->reg->ctrl_status);
2350                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2351
2352                 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
2353                         break;
2354
2355                 msleep(1000);
2356         } while ((--max_wait_time));
2357
2358         if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
2359                 DEBUG2(printk(KERN_WARNING
2360                               "scsi%ld: Network Reset Intr not cleared by "
2361                               "Network function, clearing it now!\n",
2362                               ha->host_no));
2363                 spin_lock_irqsave(&ha->hardware_lock, flags);
2364                 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
2365                 readl(&ha->reg->ctrl_status);
2366                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2367         }
2368
2369         /* Wait until the firmware tells us the Soft Reset is done */
2370         max_wait_time = SOFT_RESET_TOV;
2371         do {
2372                 spin_lock_irqsave(&ha->hardware_lock, flags);
2373                 ctrl_status = readw(&ha->reg->ctrl_status);
2374                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2375
2376                 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
2377                         status = QLA_SUCCESS;
2378                         break;
2379                 }
2380
2381                 msleep(1000);
2382         } while ((--max_wait_time));
2383
2384         /*
2385          * Also, make sure that the SCSI Reset Interrupt bit has been cleared
2386          * after the soft reset has taken place.
2387          */
2388         spin_lock_irqsave(&ha->hardware_lock, flags);
2389         ctrl_status = readw(&ha->reg->ctrl_status);
2390         if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
2391                 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
2392                 readl(&ha->reg->ctrl_status);
2393         }
2394         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2395
2396         /* If soft reset fails then most probably the bios on other
2397          * function is also enabled.
2398          * Since the initialization is sequential the other fn
2399          * wont be able to acknowledge the soft reset.
2400          * Issue a force soft reset to workaround this scenario.
2401          */
2402         if (max_wait_time == 0) {
2403                 /* Issue Force Soft Reset */
2404                 spin_lock_irqsave(&ha->hardware_lock, flags);
2405                 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
2406                 readl(&ha->reg->ctrl_status);
2407                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2408                 /* Wait until the firmware tells us the Soft Reset is done */
2409                 max_wait_time = SOFT_RESET_TOV;
2410                 do {
2411                         spin_lock_irqsave(&ha->hardware_lock, flags);
2412                         ctrl_status = readw(&ha->reg->ctrl_status);
2413                         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2414
2415                         if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
2416                                 status = QLA_SUCCESS;
2417                                 break;
2418                         }
2419
2420                         msleep(1000);
2421                 } while ((--max_wait_time));
2422         }
2423
2424         return status;
2425 }
2426
2427 /**
2428  * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
2429  * @ha: Pointer to host adapter structure.
2430  * @res: returned scsi status
2431  *
2432  * This routine is called just prior to a HARD RESET to return all
2433  * outstanding commands back to the Operating System.
2434  * Caller should make sure that the following locks are released
2435  * before this calling routine: Hardware lock, and io_request_lock.
2436  **/
2437 static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
2438 {
2439         struct srb *srb;
2440         int i;
2441         unsigned long flags;
2442
2443         spin_lock_irqsave(&ha->hardware_lock, flags);
2444         for (i = 0; i < ha->host->can_queue; i++) {
2445                 srb = qla4xxx_del_from_active_array(ha, i);
2446                 if (srb != NULL) {
2447                         srb->cmd->result = res;
2448                         kref_put(&srb->srb_ref, qla4xxx_srb_compl);
2449                 }
2450         }
2451         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2452 }
2453
2454 void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
2455 {
2456         clear_bit(AF_ONLINE, &ha->flags);
2457
2458         /* Disable the board */
2459         ql4_printk(KERN_INFO, ha, "Disabling the board\n");
2460
2461         qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
2462         qla4xxx_mark_all_devices_missing(ha);
2463         clear_bit(AF_INIT_DONE, &ha->flags);
2464 }
2465
2466 static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session)
2467 {
2468         struct iscsi_session *sess;
2469         struct ddb_entry *ddb_entry;
2470
2471         sess = cls_session->dd_data;
2472         ddb_entry = sess->dd_data;
2473         ddb_entry->fw_ddb_device_state = DDB_DS_SESSION_FAILED;
2474
2475         if (ddb_entry->ddb_type == FLASH_DDB)
2476                 iscsi_block_session(ddb_entry->sess);
2477         else
2478                 iscsi_session_failure(cls_session->dd_data,
2479                                       ISCSI_ERR_CONN_FAILED);
2480 }
2481
2482 /**
2483  * qla4xxx_recover_adapter - recovers adapter after a fatal error
2484  * @ha: Pointer to host adapter structure.
2485  **/
2486 static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
2487 {
2488         int status = QLA_ERROR;
2489         uint8_t reset_chip = 0;
2490         uint32_t dev_state;
2491         unsigned long wait;
2492
2493         /* Stall incoming I/O until we are done */
2494         scsi_block_requests(ha->host);
2495         clear_bit(AF_ONLINE, &ha->flags);
2496         clear_bit(AF_LINK_UP, &ha->flags);
2497
2498         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
2499
2500         set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2501
2502         iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
2503
2504         if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
2505                 reset_chip = 1;
2506
2507         /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
2508          * do not reset adapter, jump to initialize_adapter */
2509         if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
2510                 status = QLA_SUCCESS;
2511                 goto recover_ha_init_adapter;
2512         }
2513
2514         /* For the ISP-82xx adapter, issue a stop_firmware if invoked
2515          * from eh_host_reset or ioctl module */
2516         if (is_qla8022(ha) && !reset_chip &&
2517             test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
2518
2519                 DEBUG2(ql4_printk(KERN_INFO, ha,
2520                     "scsi%ld: %s - Performing stop_firmware...\n",
2521                     ha->host_no, __func__));
2522                 status = ha->isp_ops->reset_firmware(ha);
2523                 if (status == QLA_SUCCESS) {
2524                         if (!test_bit(AF_FW_RECOVERY, &ha->flags))
2525                                 qla4xxx_cmd_wait(ha);
2526                         ha->isp_ops->disable_intrs(ha);
2527                         qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2528                         qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
2529                 } else {
2530                         /* If the stop_firmware fails then
2531                          * reset the entire chip */
2532                         reset_chip = 1;
2533                         clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2534                         set_bit(DPC_RESET_HA, &ha->dpc_flags);
2535                 }
2536         }
2537
2538         /* Issue full chip reset if recovering from a catastrophic error,
2539          * or if stop_firmware fails for ISP-82xx.
2540          * This is the default case for ISP-4xxx */
2541         if (!is_qla8022(ha) || reset_chip) {
2542                 if (!is_qla8022(ha))
2543                         goto chip_reset;
2544
2545                 /* Check if 82XX firmware is alive or not
2546                  * We may have arrived here from NEED_RESET
2547                  * detection only */
2548                 if (test_bit(AF_FW_RECOVERY, &ha->flags))
2549                         goto chip_reset;
2550
2551                 wait = jiffies + (FW_ALIVE_WAIT_TOV * HZ);
2552                 while (time_before(jiffies, wait)) {
2553                         if (qla4_8xxx_check_fw_alive(ha)) {
2554                                 qla4xxx_mailbox_premature_completion(ha);
2555                                 break;
2556                         }
2557
2558                         set_current_state(TASK_UNINTERRUPTIBLE);
2559                         schedule_timeout(HZ);
2560                 }
2561
2562                 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
2563                         qla4xxx_cmd_wait(ha);
2564 chip_reset:
2565                 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2566                 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
2567                 DEBUG2(ql4_printk(KERN_INFO, ha,
2568                     "scsi%ld: %s - Performing chip reset..\n",
2569                     ha->host_no, __func__));
2570                 status = ha->isp_ops->reset_chip(ha);
2571         }
2572
2573         /* Flush any pending ddb changed AENs */
2574         qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2575
2576 recover_ha_init_adapter:
2577         /* Upon successful firmware/chip reset, re-initialize the adapter */
2578         if (status == QLA_SUCCESS) {
2579                 /* For ISP-4xxx, force function 1 to always initialize
2580                  * before function 3 to prevent both funcions from
2581                  * stepping on top of the other */
2582                 if (!is_qla8022(ha) && (ha->mac_index == 3))
2583                         ssleep(6);
2584
2585                 /* NOTE: AF_ONLINE flag set upon successful completion of
2586                  *       qla4xxx_initialize_adapter */
2587                 status = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
2588         }
2589
2590         /* Retry failed adapter initialization, if necessary
2591          * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
2592          * case to prevent ping-pong resets between functions */
2593         if (!test_bit(AF_ONLINE, &ha->flags) &&
2594             !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
2595                 /* Adapter initialization failed, see if we can retry
2596                  * resetting the ha.
2597                  * Since we don't want to block the DPC for too long
2598                  * with multiple resets in the same thread,
2599                  * utilize DPC to retry */
2600                 if (is_qla8022(ha)) {
2601                         qla4_8xxx_idc_lock(ha);
2602                         dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
2603                         qla4_8xxx_idc_unlock(ha);
2604                         if (dev_state == QLA82XX_DEV_FAILED) {
2605                                 ql4_printk(KERN_INFO, ha, "%s: don't retry "
2606                                            "recover adapter. H/W is in Failed "
2607                                            "state\n", __func__);
2608                                 qla4xxx_dead_adapter_cleanup(ha);
2609                                 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
2610                                 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
2611                                 clear_bit(DPC_RESET_HA_FW_CONTEXT,
2612                                                 &ha->dpc_flags);
2613                                 status = QLA_ERROR;
2614
2615                                 goto exit_recover;
2616                         }
2617                 }
2618
2619                 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
2620                         ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
2621                         DEBUG2(printk("scsi%ld: recover adapter - retrying "
2622                                       "(%d) more times\n", ha->host_no,
2623                                       ha->retry_reset_ha_cnt));
2624                         set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
2625                         status = QLA_ERROR;
2626                 } else {
2627                         if (ha->retry_reset_ha_cnt > 0) {
2628                                 /* Schedule another Reset HA--DPC will retry */
2629                                 ha->retry_reset_ha_cnt--;
2630                                 DEBUG2(printk("scsi%ld: recover adapter - "
2631                                               "retry remaining %d\n",
2632                                               ha->host_no,
2633                                               ha->retry_reset_ha_cnt));
2634                                 status = QLA_ERROR;
2635                         }
2636
2637                         if (ha->retry_reset_ha_cnt == 0) {
2638                                 /* Recover adapter retries have been exhausted.
2639                                  * Adapter DEAD */
2640                                 DEBUG2(printk("scsi%ld: recover adapter "
2641                                               "failed - board disabled\n",
2642                                               ha->host_no));
2643                                 qla4xxx_dead_adapter_cleanup(ha);
2644                                 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
2645                                 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
2646                                 clear_bit(DPC_RESET_HA_FW_CONTEXT,
2647                                           &ha->dpc_flags);
2648                                 status = QLA_ERROR;
2649                         }
2650                 }
2651         } else {
2652                 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
2653                 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2654                 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
2655         }
2656
2657 exit_recover:
2658         ha->adapter_error_count++;
2659
2660         if (test_bit(AF_ONLINE, &ha->flags))
2661                 ha->isp_ops->enable_intrs(ha);
2662
2663         scsi_unblock_requests(ha->host);
2664
2665         clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2666         DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
2667             status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
2668
2669         return status;
2670 }
2671
2672 static void qla4xxx_relogin_devices(struct iscsi_cls_session *cls_session)
2673 {
2674         struct iscsi_session *sess;
2675         struct ddb_entry *ddb_entry;
2676         struct scsi_qla_host *ha;
2677
2678         sess = cls_session->dd_data;
2679         ddb_entry = sess->dd_data;
2680         ha = ddb_entry->ha;
2681         if (!iscsi_is_session_online(cls_session)) {
2682                 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
2683                         ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
2684                                    " unblock session\n", ha->host_no, __func__,
2685                                    ddb_entry->fw_ddb_index);
2686                         iscsi_unblock_session(ddb_entry->sess);
2687                 } else {
2688                         /* Trigger relogin */
2689                         if (ddb_entry->ddb_type == FLASH_DDB) {
2690                                 if (!test_bit(DF_RELOGIN, &ddb_entry->flags))
2691                                         qla4xxx_arm_relogin_timer(ddb_entry);
2692                         } else
2693                                 iscsi_session_failure(cls_session->dd_data,
2694                                                       ISCSI_ERR_CONN_FAILED);
2695                 }
2696         }
2697 }
2698
2699 int qla4xxx_unblock_flash_ddb(struct iscsi_cls_session *cls_session)
2700 {
2701         struct iscsi_session *sess;
2702         struct ddb_entry *ddb_entry;
2703         struct scsi_qla_host *ha;
2704
2705         sess = cls_session->dd_data;
2706         ddb_entry = sess->dd_data;
2707         ha = ddb_entry->ha;
2708         ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
2709                    " unblock session\n", ha->host_no, __func__,
2710                    ddb_entry->fw_ddb_index);
2711
2712         iscsi_unblock_session(ddb_entry->sess);
2713
2714         /* Start scan target */
2715         if (test_bit(AF_ONLINE, &ha->flags)) {
2716                 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
2717                            " start scan\n", ha->host_no, __func__,
2718                            ddb_entry->fw_ddb_index);
2719                 scsi_queue_work(ha->host, &ddb_entry->sess->scan_work);
2720         }
2721         return QLA_SUCCESS;
2722 }
2723
2724 int qla4xxx_unblock_ddb(struct iscsi_cls_session *cls_session)
2725 {
2726         struct iscsi_session *sess;
2727         struct ddb_entry *ddb_entry;
2728         struct scsi_qla_host *ha;
2729
2730         sess = cls_session->dd_data;
2731         ddb_entry = sess->dd_data;
2732         ha = ddb_entry->ha;
2733         ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
2734                    " unblock user space session\n", ha->host_no, __func__,
2735                    ddb_entry->fw_ddb_index);
2736         iscsi_conn_start(ddb_entry->conn);
2737         iscsi_conn_login_event(ddb_entry->conn,
2738                                ISCSI_CONN_STATE_LOGGED_IN);
2739
2740         return QLA_SUCCESS;
2741 }
2742
2743 static void qla4xxx_relogin_all_devices(struct scsi_qla_host *ha)
2744 {
2745         iscsi_host_for_each_session(ha->host, qla4xxx_relogin_devices);
2746 }
2747
2748 static void qla4xxx_relogin_flash_ddb(struct iscsi_cls_session *cls_sess)
2749 {
2750         uint16_t relogin_timer;
2751         struct iscsi_session *sess;
2752         struct ddb_entry *ddb_entry;
2753         struct scsi_qla_host *ha;
2754
2755         sess = cls_sess->dd_data;
2756         ddb_entry = sess->dd_data;
2757         ha = ddb_entry->ha;
2758
2759         relogin_timer = max(ddb_entry->default_relogin_timeout,
2760                             (uint16_t)RELOGIN_TOV);
2761         atomic_set(&ddb_entry->relogin_timer, relogin_timer);
2762
2763         DEBUG2(ql4_printk(KERN_INFO, ha,
2764                           "scsi%ld: Relogin index [%d]. TOV=%d\n", ha->host_no,
2765                           ddb_entry->fw_ddb_index, relogin_timer));
2766
2767         qla4xxx_login_flash_ddb(cls_sess);
2768 }
2769
2770 static void qla4xxx_dpc_relogin(struct iscsi_cls_session *cls_sess)
2771 {
2772         struct iscsi_session *sess;
2773         struct ddb_entry *ddb_entry;
2774         struct scsi_qla_host *ha;
2775
2776         sess = cls_sess->dd_data;
2777         ddb_entry = sess->dd_data;
2778         ha = ddb_entry->ha;
2779
2780         if (!(ddb_entry->ddb_type == FLASH_DDB))
2781                 return;
2782
2783         if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
2784             !iscsi_is_session_online(cls_sess)) {
2785                 DEBUG2(ql4_printk(KERN_INFO, ha,
2786                                   "relogin issued\n"));
2787                 qla4xxx_relogin_flash_ddb(cls_sess);
2788         }
2789 }
2790
2791 void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
2792 {
2793         if (ha->dpc_thread)
2794                 queue_work(ha->dpc_thread, &ha->dpc_work);
2795 }
2796
2797 /**
2798  * qla4xxx_do_dpc - dpc routine
2799  * @data: in our case pointer to adapter structure
2800  *
2801  * This routine is a task that is schedule by the interrupt handler
2802  * to perform the background processing for interrupts.  We put it
2803  * on a task queue that is consumed whenever the scheduler runs; that's
2804  * so you can do anything (i.e. put the process to sleep etc).  In fact,
2805  * the mid-level tries to sleep when it reaches the driver threshold
2806  * "host->can_queue". This can cause a panic if we were in our interrupt code.
2807  **/
2808 static void qla4xxx_do_dpc(struct work_struct *work)
2809 {
2810         struct scsi_qla_host *ha =
2811                 container_of(work, struct scsi_qla_host, dpc_work);
2812         int status = QLA_ERROR;
2813
2814         DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
2815             "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
2816             ha->host_no, __func__, ha->flags, ha->dpc_flags))
2817
2818         /* Initialization not yet finished. Don't do anything yet. */
2819         if (!test_bit(AF_INIT_DONE, &ha->flags))
2820                 return;
2821
2822         if (test_bit(AF_EEH_BUSY, &ha->flags)) {
2823                 DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
2824                     ha->host_no, __func__, ha->flags));
2825                 return;
2826         }
2827
2828         if (is_qla8022(ha)) {
2829                 if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
2830                         qla4_8xxx_idc_lock(ha);
2831                         qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2832                             QLA82XX_DEV_FAILED);
2833                         qla4_8xxx_idc_unlock(ha);
2834                         ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
2835                         qla4_8xxx_device_state_handler(ha);
2836                 }
2837                 if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
2838                         qla4_8xxx_need_qsnt_handler(ha);
2839                 }
2840         }
2841
2842         if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
2843             (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
2844             test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
2845             test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
2846                 if (ql4xdontresethba) {
2847                         DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
2848                             ha->host_no, __func__));
2849                         clear_bit(DPC_RESET_HA, &ha->dpc_flags);
2850                         clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
2851                         clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2852                         goto dpc_post_reset_ha;
2853                 }
2854                 if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
2855                     test_bit(DPC_RESET_HA, &ha->dpc_flags))
2856                         qla4xxx_recover_adapter(ha);
2857
2858                 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
2859                         uint8_t wait_time = RESET_INTR_TOV;
2860
2861                         while ((readw(&ha->reg->ctrl_status) &
2862                                 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
2863                                 if (--wait_time == 0)
2864                                         break;
2865                                 msleep(1000);
2866                         }
2867                         if (wait_time == 0)
2868                                 DEBUG2(printk("scsi%ld: %s: SR|FSR "
2869                                               "bit not cleared-- resetting\n",
2870                                               ha->host_no, __func__));
2871                         qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
2872                         if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
2873                                 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2874                                 status = qla4xxx_recover_adapter(ha);
2875                         }
2876                         clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
2877                         if (status == QLA_SUCCESS)
2878                                 ha->isp_ops->enable_intrs(ha);
2879                 }
2880         }
2881
2882 dpc_post_reset_ha:
2883         /* ---- process AEN? --- */
2884         if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
2885                 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
2886
2887         /* ---- Get DHCP IP Address? --- */
2888         if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
2889                 qla4xxx_get_dhcp_ip_address(ha);
2890
2891         /* ---- relogin device? --- */
2892         if (adapter_up(ha) &&
2893             test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
2894                 iscsi_host_for_each_session(ha->host, qla4xxx_dpc_relogin);
2895         }
2896
2897         /* ---- link change? --- */
2898         if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
2899                 if (!test_bit(AF_LINK_UP, &ha->flags)) {
2900                         /* ---- link down? --- */
2901                         qla4xxx_mark_all_devices_missing(ha);
2902                 } else {
2903                         /* ---- link up? --- *
2904                          * F/W will auto login to all devices ONLY ONCE after
2905                          * link up during driver initialization and runtime
2906                          * fatal error recovery.  Therefore, the driver must
2907                          * manually relogin to devices when recovering from
2908                          * connection failures, logouts, expired KATO, etc. */
2909                         if (test_and_clear_bit(AF_BUILD_DDB_LIST, &ha->flags)) {
2910                                 qla4xxx_build_ddb_list(ha, ha->is_reset);
2911                                 iscsi_host_for_each_session(ha->host,
2912                                                 qla4xxx_login_flash_ddb);
2913                         } else
2914                                 qla4xxx_relogin_all_devices(ha);
2915                 }
2916         }
2917 }
2918
2919 /**
2920  * qla4xxx_free_adapter - release the adapter
2921  * @ha: pointer to adapter structure
2922  **/
2923 static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
2924 {
2925         qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
2926
2927         if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
2928                 /* Turn-off interrupts on the card. */
2929                 ha->isp_ops->disable_intrs(ha);
2930         }
2931
2932         /* Remove timer thread, if present */
2933         if (ha->timer_active)
2934                 qla4xxx_stop_timer(ha);
2935
2936         /* Kill the kernel thread for this host */
2937         if (ha->dpc_thread)
2938                 destroy_workqueue(ha->dpc_thread);
2939
2940         /* Kill the kernel thread for this host */
2941         if (ha->task_wq)
2942                 destroy_workqueue(ha->task_wq);
2943
2944         /* Put firmware in known state */
2945         ha->isp_ops->reset_firmware(ha);
2946
2947         if (is_qla8022(ha)) {
2948                 qla4_8xxx_idc_lock(ha);
2949                 qla4_8xxx_clear_drv_active(ha);
2950                 qla4_8xxx_idc_unlock(ha);
2951         }
2952
2953         /* Detach interrupts */
2954         if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
2955                 qla4xxx_free_irqs(ha);
2956
2957         /* free extra memory */
2958         qla4xxx_mem_free(ha);
2959 }
2960
2961 int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
2962 {
2963         int status = 0;
2964         uint8_t revision_id;
2965         unsigned long mem_base, mem_len, db_base, db_len;
2966         struct pci_dev *pdev = ha->pdev;
2967
2968         status = pci_request_regions(pdev, DRIVER_NAME);
2969         if (status) {
2970                 printk(KERN_WARNING
2971                     "scsi(%ld) Failed to reserve PIO regions (%s) "
2972                     "status=%d\n", ha->host_no, pci_name(pdev), status);
2973                 goto iospace_error_exit;
2974         }
2975
2976         pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
2977         DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
2978             __func__, revision_id));
2979         ha->revision_id = revision_id;
2980
2981         /* remap phys address */
2982         mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
2983         mem_len = pci_resource_len(pdev, 0);
2984         DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
2985             __func__, mem_base, mem_len));
2986
2987         /* mapping of pcibase pointer */
2988         ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
2989         if (!ha->nx_pcibase) {
2990                 printk(KERN_ERR
2991                     "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
2992                 pci_release_regions(ha->pdev);
2993                 goto iospace_error_exit;
2994         }
2995
2996         /* Mapping of IO base pointer, door bell read and write pointer */
2997
2998         /* mapping of IO base pointer */
2999         ha->qla4_8xxx_reg =
3000             (struct device_reg_82xx  __iomem *)((uint8_t *)ha->nx_pcibase +
3001             0xbc000 + (ha->pdev->devfn << 11));
3002
3003         db_base = pci_resource_start(pdev, 4);  /* doorbell is on bar 4 */
3004         db_len = pci_resource_len(pdev, 4);
3005
3006         ha->nx_db_wr_ptr = (ha->pdev->devfn == 4 ? QLA82XX_CAM_RAM_DB1 :
3007             QLA82XX_CAM_RAM_DB2);
3008
3009         return 0;
3010 iospace_error_exit:
3011         return -ENOMEM;
3012 }
3013
3014 /***
3015  * qla4xxx_iospace_config - maps registers
3016  * @ha: pointer to adapter structure
3017  *
3018  * This routines maps HBA's registers from the pci address space
3019  * into the kernel virtual address space for memory mapped i/o.
3020  **/
3021 int qla4xxx_iospace_config(struct scsi_qla_host *ha)
3022 {
3023         unsigned long pio, pio_len, pio_flags;
3024         unsigned long mmio, mmio_len, mmio_flags;
3025
3026         pio = pci_resource_start(ha->pdev, 0);
3027         pio_len = pci_resource_len(ha->pdev, 0);
3028         pio_flags = pci_resource_flags(ha->pdev, 0);
3029         if (pio_flags & IORESOURCE_IO) {
3030                 if (pio_len < MIN_IOBASE_LEN) {
3031                         ql4_printk(KERN_WARNING, ha,
3032                                 "Invalid PCI I/O region size\n");
3033                         pio = 0;
3034                 }
3035         } else {
3036                 ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
3037                 pio = 0;
3038         }
3039
3040         /* Use MMIO operations for all accesses. */
3041         mmio = pci_resource_start(ha->pdev, 1);
3042         mmio_len = pci_resource_len(ha->pdev, 1);
3043         mmio_flags = pci_resource_flags(ha->pdev, 1);
3044
3045         if (!(mmio_flags & IORESOURCE_MEM)) {
3046                 ql4_printk(KERN_ERR, ha,
3047                     "region #0 not an MMIO resource, aborting\n");
3048
3049                 goto iospace_error_exit;
3050         }
3051
3052         if (mmio_len < MIN_IOBASE_LEN) {
3053                 ql4_printk(KERN_ERR, ha,
3054                     "Invalid PCI mem region size, aborting\n");
3055                 goto iospace_error_exit;
3056         }
3057
3058         if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
3059                 ql4_printk(KERN_WARNING, ha,
3060                     "Failed to reserve PIO/MMIO regions\n");
3061
3062                 goto iospace_error_exit;
3063         }
3064
3065         ha->pio_address = pio;
3066         ha->pio_length = pio_len;
3067         ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
3068         if (!ha->reg) {
3069                 ql4_printk(KERN_ERR, ha,
3070                     "cannot remap MMIO, aborting\n");
3071
3072                 goto iospace_error_exit;
3073         }
3074
3075         return 0;
3076
3077 iospace_error_exit:
3078         return -ENOMEM;
3079 }
3080
3081 static struct isp_operations qla4xxx_isp_ops = {
3082         .iospace_config         = qla4xxx_iospace_config,
3083         .pci_config             = qla4xxx_pci_config,
3084         .disable_intrs          = qla4xxx_disable_intrs,
3085         .enable_intrs           = qla4xxx_enable_intrs,
3086         .start_firmware         = qla4xxx_start_firmware,
3087         .intr_handler           = qla4xxx_intr_handler,
3088         .interrupt_service_routine = qla4xxx_interrupt_service_routine,
3089         .reset_chip             = qla4xxx_soft_reset,
3090         .reset_firmware         = qla4xxx_hw_reset,
3091         .queue_iocb             = qla4xxx_queue_iocb,
3092         .complete_iocb          = qla4xxx_complete_iocb,
3093         .rd_shdw_req_q_out      = qla4xxx_rd_shdw_req_q_out,
3094         .rd_shdw_rsp_q_in       = qla4xxx_rd_shdw_rsp_q_in,
3095         .get_sys_info           = qla4xxx_get_sys_info,
3096 };
3097
3098 static struct isp_operations qla4_8xxx_isp_ops = {
3099         .iospace_config         = qla4_8xxx_iospace_config,
3100         .pci_config             = qla4_8xxx_pci_config,
3101         .disable_intrs          = qla4_8xxx_disable_intrs,
3102         .enable_intrs           = qla4_8xxx_enable_intrs,
3103         .start_firmware         = qla4_8xxx_load_risc,
3104         .intr_handler           = qla4_8xxx_intr_handler,
3105         .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
3106         .reset_chip             = qla4_8xxx_isp_reset,
3107         .reset_firmware         = qla4_8xxx_stop_firmware,
3108         .queue_iocb             = qla4_8xxx_queue_iocb,
3109         .complete_iocb          = qla4_8xxx_complete_iocb,
3110         .rd_shdw_req_q_out      = qla4_8xxx_rd_shdw_req_q_out,
3111         .rd_shdw_rsp_q_in       = qla4_8xxx_rd_shdw_rsp_q_in,
3112         .get_sys_info           = qla4_8xxx_get_sys_info,
3113 };
3114
3115 uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
3116 {
3117         return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
3118 }
3119
3120 uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
3121 {
3122         return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
3123 }
3124
3125 uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
3126 {
3127         return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
3128 }
3129
3130 uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
3131 {
3132         return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
3133 }
3134
3135 static ssize_t qla4xxx_show_boot_eth_info(void *data, int type, char *buf)
3136 {
3137         struct scsi_qla_host *ha = data;
3138         char *str = buf;
3139         int rc;
3140
3141         switch (type) {
3142         case ISCSI_BOOT_ETH_FLAGS:
3143                 rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
3144                 break;
3145         case ISCSI_BOOT_ETH_INDEX:
3146                 rc = sprintf(str, "0\n");
3147                 break;
3148         case ISCSI_BOOT_ETH_MAC:
3149                 rc = sysfs_format_mac(str, ha->my_mac,
3150                                       MAC_ADDR_LEN);
3151                 break;
3152         default:
3153                 rc = -ENOSYS;
3154                 break;
3155         }
3156         return rc;
3157 }
3158
3159 static umode_t qla4xxx_eth_get_attr_visibility(void *data, int type)
3160 {
3161         int rc;
3162
3163         switch (type) {
3164         case ISCSI_BOOT_ETH_FLAGS:
3165         case ISCSI_BOOT_ETH_MAC:
3166         case ISCSI_BOOT_ETH_INDEX:
3167                 rc = S_IRUGO;
3168                 break;
3169         default:
3170                 rc = 0;
3171                 break;
3172         }
3173         return rc;
3174 }
3175
3176 static ssize_t qla4xxx_show_boot_ini_info(void *data, int type, char *buf)
3177 {
3178         struct scsi_qla_host *ha = data;
3179         char *str = buf;
3180         int rc;
3181
3182         switch (type) {
3183         case ISCSI_BOOT_INI_INITIATOR_NAME:
3184                 rc = sprintf(str, "%s\n", ha->name_string);
3185                 break;
3186         default:
3187                 rc = -ENOSYS;
3188                 break;
3189         }
3190         return rc;
3191 }
3192
3193 static umode_t qla4xxx_ini_get_attr_visibility(void *data, int type)
3194 {
3195         int rc;
3196
3197         switch (type) {
3198         case ISCSI_BOOT_INI_INITIATOR_NAME:
3199                 rc = S_IRUGO;
3200                 break;
3201         default:
3202                 rc = 0;
3203                 break;
3204         }
3205         return rc;
3206 }
3207
3208 static ssize_t
3209 qla4xxx_show_boot_tgt_info(struct ql4_boot_session_info *boot_sess, int type,
3210                            char *buf)
3211 {
3212         struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
3213         char *str = buf;
3214         int rc;
3215
3216         switch (type) {
3217         case ISCSI_BOOT_TGT_NAME:
3218                 rc = sprintf(buf, "%s\n", (char *)&boot_sess->target_name);
3219                 break;
3220         case ISCSI_BOOT_TGT_IP_ADDR:
3221                 if (boot_sess->conn_list[0].dest_ipaddr.ip_type == 0x1)
3222                         rc = sprintf(buf, "%pI4\n",
3223                                      &boot_conn->dest_ipaddr.ip_address);
3224                 else
3225                         rc = sprintf(str, "%pI6\n",
3226                                      &boot_conn->dest_ipaddr.ip_address);
3227                 break;
3228         case ISCSI_BOOT_TGT_PORT:
3229                         rc = sprintf(str, "%d\n", boot_conn->dest_port);
3230                 break;
3231         case ISCSI_BOOT_TGT_CHAP_NAME:
3232                 rc = sprintf(str,  "%.*s\n",
3233                              boot_conn->chap.target_chap_name_length,
3234                              (char *)&boot_conn->chap.target_chap_name);
3235                 break;
3236         case ISCSI_BOOT_TGT_CHAP_SECRET:
3237                 rc = sprintf(str,  "%.*s\n",
3238                              boot_conn->chap.target_secret_length,
3239                              (char *)&boot_conn->chap.target_secret);
3240                 break;
3241         case ISCSI_BOOT_TGT_REV_CHAP_NAME:
3242                 rc = sprintf(str,  "%.*s\n",
3243                              boot_conn->chap.intr_chap_name_length,
3244                              (char *)&boot_conn->chap.intr_chap_name);
3245                 break;
3246         case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
3247                 rc = sprintf(str,  "%.*s\n",
3248                              boot_conn->chap.intr_secret_length,
3249                              (char *)&boot_conn->chap.intr_secret);
3250                 break;
3251         case ISCSI_BOOT_TGT_FLAGS:
3252                 rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
3253                 break;
3254         case ISCSI_BOOT_TGT_NIC_ASSOC:
3255                 rc = sprintf(str, "0\n");
3256                 break;
3257         default:
3258                 rc = -ENOSYS;
3259                 break;
3260         }
3261         return rc;
3262 }
3263
3264 static ssize_t qla4xxx_show_boot_tgt_pri_info(void *data, int type, char *buf)
3265 {
3266         struct scsi_qla_host *ha = data;
3267         struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_pri_sess);
3268
3269         return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
3270 }
3271
3272 static ssize_t qla4xxx_show_boot_tgt_sec_info(void *data, int type, char *buf)
3273 {
3274         struct scsi_qla_host *ha = data;
3275         struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_sec_sess);
3276
3277         return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
3278 }
3279
3280 static umode_t qla4xxx_tgt_get_attr_visibility(void *data, int type)
3281 {
3282         int rc;
3283
3284         switch (type) {
3285         case ISCSI_BOOT_TGT_NAME:
3286         case ISCSI_BOOT_TGT_IP_ADDR:
3287         case ISCSI_BOOT_TGT_PORT:
3288         case ISCSI_BOOT_TGT_CHAP_NAME:
3289         case ISCSI_BOOT_TGT_CHAP_SECRET:
3290         case ISCSI_BOOT_TGT_REV_CHAP_NAME:
3291         case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
3292         case ISCSI_BOOT_TGT_NIC_ASSOC:
3293         case ISCSI_BOOT_TGT_FLAGS:
3294                 rc = S_IRUGO;
3295                 break;
3296         default:
3297                 rc = 0;
3298                 break;
3299         }
3300         return rc;
3301 }
3302
3303 static void qla4xxx_boot_release(void *data)
3304 {
3305         struct scsi_qla_host *ha = data;
3306
3307         scsi_host_put(ha->host);
3308 }
3309
3310 static int get_fw_boot_info(struct scsi_qla_host *ha, uint16_t ddb_index[])
3311 {
3312         dma_addr_t buf_dma;
3313         uint32_t addr, pri_addr, sec_addr;
3314         uint32_t offset;
3315         uint16_t func_num;
3316         uint8_t val;
3317         uint8_t *buf = NULL;
3318         size_t size = 13 * sizeof(uint8_t);
3319         int ret = QLA_SUCCESS;
3320
3321         func_num = PCI_FUNC(ha->pdev->devfn);
3322
3323         ql4_printk(KERN_INFO, ha, "%s: Get FW boot info for 0x%x func %d\n",
3324                    __func__, ha->pdev->device, func_num);
3325
3326         if (is_qla40XX(ha)) {
3327                 if (func_num == 1) {
3328                         addr = NVRAM_PORT0_BOOT_MODE;
3329                         pri_addr = NVRAM_PORT0_BOOT_PRI_TGT;
3330                         sec_addr = NVRAM_PORT0_BOOT_SEC_TGT;
3331                 } else if (func_num == 3) {
3332                         addr = NVRAM_PORT1_BOOT_MODE;
3333                         pri_addr = NVRAM_PORT1_BOOT_PRI_TGT;
3334                         sec_addr = NVRAM_PORT1_BOOT_SEC_TGT;
3335                 } else {
3336                         ret = QLA_ERROR;
3337                         goto exit_boot_info;
3338                 }
3339
3340                 /* Check Boot Mode */
3341                 val = rd_nvram_byte(ha, addr);
3342                 if (!(val & 0x07)) {
3343                         DEBUG2(ql4_printk(KERN_ERR, ha,
3344                                           "%s: Failed Boot options : 0x%x\n",
3345                                           __func__, val));
3346                         ret = QLA_ERROR;
3347                         goto exit_boot_info;
3348                 }
3349
3350                 /* get primary valid target index */
3351                 val = rd_nvram_byte(ha, pri_addr);
3352                 if (val & BIT_7)
3353                         ddb_index[0] = (val & 0x7f);
3354
3355                 /* get secondary valid target index */
3356                 val = rd_nvram_byte(ha, sec_addr);
3357                 if (val & BIT_7)
3358                         ddb_index[1] = (val & 0x7f);
3359
3360         } else if (is_qla8022(ha)) {
3361                 buf = dma_alloc_coherent(&ha->pdev->dev, size,
3362                                          &buf_dma, GFP_KERNEL);
3363                 if (!buf) {
3364                         DEBUG2(ql4_printk(KERN_ERR, ha,
3365                                           "%s: Unable to allocate dma buffer\n",
3366                                            __func__));
3367                         ret = QLA_ERROR;
3368                         goto exit_boot_info;
3369                 }
3370
3371                 if (ha->port_num == 0)
3372                         offset = BOOT_PARAM_OFFSET_PORT0;
3373                 else if (ha->port_num == 1)
3374                         offset = BOOT_PARAM_OFFSET_PORT1;
3375                 else {
3376                         ret = QLA_ERROR;
3377                         goto exit_boot_info_free;
3378                 }
3379                 addr = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_iscsi_param * 4) +
3380                        offset;
3381                 if (qla4xxx_get_flash(ha, buf_dma, addr,
3382                                       13 * sizeof(uint8_t)) != QLA_SUCCESS) {
3383                         DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: Get Flash"
3384                                           "failed\n", ha->host_no, __func__));
3385                         ret = QLA_ERROR;
3386                         goto exit_boot_info_free;
3387                 }
3388                 /* Check Boot Mode */
3389                 if (!(buf[1] & 0x07)) {
3390                         DEBUG2(ql4_printk(KERN_INFO, ha,
3391                                           "Failed: Boot options : 0x%x\n",
3392                                           buf[1]));
3393                         ret = QLA_ERROR;
3394                         goto exit_boot_info_free;
3395                 }
3396
3397                 /* get primary valid target index */
3398                 if (buf[2] & BIT_7)
3399                         ddb_index[0] = buf[2] & 0x7f;
3400
3401                 /* get secondary valid target index */
3402                 if (buf[11] & BIT_7)
3403                         ddb_index[1] = buf[11] & 0x7f;
3404         } else {
3405                 ret = QLA_ERROR;
3406                 goto exit_boot_info;
3407         }
3408
3409         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Primary target ID %d, Secondary"
3410                           " target ID %d\n", __func__, ddb_index[0],
3411                           ddb_index[1]));
3412
3413         ha->pri_ddb_idx = ddb_index[0];
3414         ha->sec_ddb_idx = ddb_index[1];
3415
3416 exit_boot_info_free:
3417         dma_free_coherent(&ha->pdev->dev, size, buf, buf_dma);
3418 exit_boot_info:
3419         return ret;
3420 }
3421
3422 /**
3423  * qla4xxx_get_bidi_chap - Get a BIDI CHAP user and password
3424  * @ha: pointer to adapter structure
3425  * @username: CHAP username to be returned
3426  * @password: CHAP password to be returned
3427  *
3428  * If a boot entry has BIDI CHAP enabled then we need to set the BIDI CHAP
3429  * user and password in the sysfs entry in /sys/firmware/iscsi_boot#/.
3430  * So from the CHAP cache find the first BIDI CHAP entry and set it
3431  * to the boot record in sysfs.
3432  **/
3433 static int qla4xxx_get_bidi_chap(struct scsi_qla_host *ha, char *username,
3434                             char *password)
3435 {
3436         int i, ret = -EINVAL;
3437         int max_chap_entries = 0;
3438         struct ql4_chap_table *chap_table;
3439
3440         if (is_qla8022(ha))
3441                 max_chap_entries = (ha->hw.flt_chap_size / 2) /
3442                                                 sizeof(struct ql4_chap_table);
3443         else
3444                 max_chap_entries = MAX_CHAP_ENTRIES_40XX;
3445
3446         if (!ha->chap_list) {
3447                 ql4_printk(KERN_ERR, ha, "Do not have CHAP table cache\n");
3448                 return ret;
3449         }
3450
3451         mutex_lock(&ha->chap_sem);
3452         for (i = 0; i < max_chap_entries; i++) {
3453                 chap_table = (struct ql4_chap_table *)ha->chap_list + i;
3454                 if (chap_table->cookie !=
3455                     __constant_cpu_to_le16(CHAP_VALID_COOKIE)) {
3456                         continue;
3457                 }
3458
3459                 if (chap_table->flags & BIT_7) /* local */
3460                         continue;
3461
3462                 if (!(chap_table->flags & BIT_6)) /* Not BIDI */
3463                         continue;
3464
3465                 strncpy(password, chap_table->secret, QL4_CHAP_MAX_SECRET_LEN);
3466                 strncpy(username, chap_table->name, QL4_CHAP_MAX_NAME_LEN);
3467                 ret = 0;
3468                 break;
3469         }
3470         mutex_unlock(&ha->chap_sem);
3471
3472         return ret;
3473 }
3474
3475
3476 static int qla4xxx_get_boot_target(struct scsi_qla_host *ha,
3477                                    struct ql4_boot_session_info *boot_sess,
3478                                    uint16_t ddb_index)
3479 {
3480         struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
3481         struct dev_db_entry *fw_ddb_entry;
3482         dma_addr_t fw_ddb_entry_dma;
3483         uint16_t idx;
3484         uint16_t options;
3485         int ret = QLA_SUCCESS;
3486
3487         fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3488                                           &fw_ddb_entry_dma, GFP_KERNEL);
3489         if (!fw_ddb_entry) {
3490                 DEBUG2(ql4_printk(KERN_ERR, ha,
3491                                   "%s: Unable to allocate dma buffer.\n",
3492                                   __func__));
3493                 ret = QLA_ERROR;
3494                 return ret;
3495         }
3496
3497         if (qla4xxx_bootdb_by_index(ha, fw_ddb_entry,
3498                                    fw_ddb_entry_dma, ddb_index)) {
3499                 DEBUG2(ql4_printk(KERN_ERR, ha,
3500                                   "%s: Flash DDB read Failed\n", __func__));
3501                 ret = QLA_ERROR;
3502                 goto exit_boot_target;
3503         }
3504
3505         /* Update target name and IP from DDB */
3506         memcpy(boot_sess->target_name, fw_ddb_entry->iscsi_name,
3507                min(sizeof(boot_sess->target_name),
3508                    sizeof(fw_ddb_entry->iscsi_name)));
3509
3510         options = le16_to_cpu(fw_ddb_entry->options);
3511         if (options & DDB_OPT_IPV6_DEVICE) {
3512                 memcpy(&boot_conn->dest_ipaddr.ip_address,
3513                        &fw_ddb_entry->ip_addr[0], IPv6_ADDR_LEN);
3514         } else {
3515                 boot_conn->dest_ipaddr.ip_type = 0x1;
3516                 memcpy(&boot_conn->dest_ipaddr.ip_address,
3517                        &fw_ddb_entry->ip_addr[0], IP_ADDR_LEN);
3518         }
3519
3520         boot_conn->dest_port = le16_to_cpu(fw_ddb_entry->port);
3521
3522         /* update chap information */
3523         idx = __le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
3524
3525         if (BIT_7 & le16_to_cpu(fw_ddb_entry->iscsi_options))   {
3526
3527                 DEBUG2(ql4_printk(KERN_INFO, ha, "Setting chap\n"));
3528
3529                 ret = qla4xxx_get_chap(ha, (char *)&boot_conn->chap.
3530                                        target_chap_name,
3531                                        (char *)&boot_conn->chap.target_secret,
3532                                        idx);
3533                 if (ret) {
3534                         ql4_printk(KERN_ERR, ha, "Failed to set chap\n");
3535                         ret = QLA_ERROR;
3536                         goto exit_boot_target;
3537                 }
3538
3539                 boot_conn->chap.target_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
3540                 boot_conn->chap.target_secret_length = QL4_CHAP_MAX_SECRET_LEN;
3541         }
3542
3543         if (BIT_4 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
3544
3545                 DEBUG2(ql4_printk(KERN_INFO, ha, "Setting BIDI chap\n"));
3546
3547                 ret = qla4xxx_get_bidi_chap(ha,
3548                                     (char *)&boot_conn->chap.intr_chap_name,
3549                                     (char *)&boot_conn->chap.intr_secret);
3550
3551                 if (ret) {
3552                         ql4_printk(KERN_ERR, ha, "Failed to set BIDI chap\n");
3553                         ret = QLA_ERROR;
3554                         goto exit_boot_target;
3555                 }
3556
3557                 boot_conn->chap.intr_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
3558                 boot_conn->chap.intr_secret_length = QL4_CHAP_MAX_SECRET_LEN;
3559         }
3560
3561 exit_boot_target:
3562         dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3563                           fw_ddb_entry, fw_ddb_entry_dma);
3564         return ret;
3565 }
3566
3567 static int qla4xxx_get_boot_info(struct scsi_qla_host *ha)
3568 {
3569         uint16_t ddb_index[2];
3570         int ret = QLA_ERROR;
3571         int rval;
3572
3573         memset(ddb_index, 0, sizeof(ddb_index));
3574         ddb_index[0] = 0xffff;
3575         ddb_index[1] = 0xffff;
3576         ret = get_fw_boot_info(ha, ddb_index);
3577         if (ret != QLA_SUCCESS) {
3578                 DEBUG2(ql4_printk(KERN_ERR, ha,
3579                                   "%s: Failed to set boot info.\n", __func__));
3580                 return ret;
3581         }
3582
3583         if (ql4xdisablesysfsboot)
3584                 return QLA_SUCCESS;
3585
3586         if (ddb_index[0] == 0xffff)
3587                 goto sec_target;
3588
3589         rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_pri_sess),
3590                                       ddb_index[0]);
3591         if (rval != QLA_SUCCESS) {
3592                 DEBUG2(ql4_printk(KERN_ERR, ha, "%s: Failed to get "
3593                                   "primary target\n", __func__));
3594         } else
3595                 ret = QLA_SUCCESS;
3596
3597 sec_target:
3598         if (ddb_index[1] == 0xffff)
3599                 goto exit_get_boot_info;
3600
3601         rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_sec_sess),
3602                                       ddb_index[1]);
3603         if (rval != QLA_SUCCESS) {
3604                 DEBUG2(ql4_printk(KERN_ERR, ha, "%s: Failed to get "
3605                                   "secondary target\n", __func__));
3606         } else
3607                 ret = QLA_SUCCESS;
3608
3609 exit_get_boot_info:
3610         return ret;
3611 }
3612
3613 static int qla4xxx_setup_boot_info(struct scsi_qla_host *ha)
3614 {
3615         struct iscsi_boot_kobj *boot_kobj;
3616
3617         if (qla4xxx_get_boot_info(ha) != QLA_SUCCESS)
3618                 return QLA_ERROR;
3619
3620         if (ql4xdisablesysfsboot) {
3621                 ql4_printk(KERN_INFO, ha,
3622                            "%s: syfsboot disabled - driver will trigger login"
3623                            "and publish session for discovery .\n", __func__);
3624                 return QLA_SUCCESS;
3625         }
3626
3627
3628         ha->boot_kset = iscsi_boot_create_host_kset(ha->host->host_no);
3629         if (!ha->boot_kset)
3630                 goto kset_free;
3631
3632         if (!scsi_host_get(ha->host))
3633                 goto kset_free;
3634         boot_kobj = iscsi_boot_create_target(ha->boot_kset, 0, ha,
3635                                              qla4xxx_show_boot_tgt_pri_info,
3636                                              qla4xxx_tgt_get_attr_visibility,
3637                                              qla4xxx_boot_release);
3638         if (!boot_kobj)
3639                 goto put_host;
3640
3641         if (!scsi_host_get(ha->host))
3642                 goto kset_free;
3643         boot_kobj = iscsi_boot_create_target(ha->boot_kset, 1, ha,
3644                                              qla4xxx_show_boot_tgt_sec_info,
3645                                              qla4xxx_tgt_get_attr_visibility,
3646                                              qla4xxx_boot_release);
3647         if (!boot_kobj)
3648                 goto put_host;
3649
3650         if (!scsi_host_get(ha->host))
3651                 goto kset_free;
3652         boot_kobj = iscsi_boot_create_initiator(ha->boot_kset, 0, ha,
3653                                                qla4xxx_show_boot_ini_info,
3654                                                qla4xxx_ini_get_attr_visibility,
3655                                                qla4xxx_boot_release);
3656         if (!boot_kobj)
3657                 goto put_host;
3658
3659         if (!scsi_host_get(ha->host))
3660                 goto kset_free;
3661         boot_kobj = iscsi_boot_create_ethernet(ha->boot_kset, 0, ha,
3662                                                qla4xxx_show_boot_eth_info,
3663                                                qla4xxx_eth_get_attr_visibility,
3664                                                qla4xxx_boot_release);
3665         if (!boot_kobj)
3666                 goto put_host;
3667
3668         return QLA_SUCCESS;
3669
3670 put_host:
3671         scsi_host_put(ha->host);
3672 kset_free:
3673         iscsi_boot_destroy_kset(ha->boot_kset);
3674         return -ENOMEM;
3675 }
3676
3677
3678 /**
3679  * qla4xxx_create chap_list - Create CHAP list from FLASH
3680  * @ha: pointer to adapter structure
3681  *
3682  * Read flash and make a list of CHAP entries, during login when a CHAP entry
3683  * is received, it will be checked in this list. If entry exist then the CHAP
3684  * entry index is set in the DDB. If CHAP entry does not exist in this list
3685  * then a new entry is added in FLASH in CHAP table and the index obtained is
3686  * used in the DDB.
3687  **/
3688 static void qla4xxx_create_chap_list(struct scsi_qla_host *ha)
3689 {
3690         int rval = 0;
3691         uint8_t *chap_flash_data = NULL;
3692         uint32_t offset;
3693         dma_addr_t chap_dma;
3694         uint32_t chap_size = 0;
3695
3696         if (is_qla40XX(ha))
3697                 chap_size = MAX_CHAP_ENTRIES_40XX  *
3698                                         sizeof(struct ql4_chap_table);
3699         else    /* Single region contains CHAP info for both
3700                  * ports which is divided into half for each port.
3701                  */
3702                 chap_size = ha->hw.flt_chap_size / 2;
3703
3704         chap_flash_data = dma_alloc_coherent(&ha->pdev->dev, chap_size,
3705                                           &chap_dma, GFP_KERNEL);
3706         if (!chap_flash_data) {
3707                 ql4_printk(KERN_ERR, ha, "No memory for chap_flash_data\n");
3708                 return;
3709         }
3710         if (is_qla40XX(ha))
3711                 offset = FLASH_CHAP_OFFSET;
3712         else {
3713                 offset = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_region_chap << 2);
3714                 if (ha->port_num == 1)
3715                         offset += chap_size;
3716         }
3717
3718         rval = qla4xxx_get_flash(ha, chap_dma, offset, chap_size);
3719         if (rval != QLA_SUCCESS)
3720                 goto exit_chap_list;
3721
3722         if (ha->chap_list == NULL)
3723                 ha->chap_list = vmalloc(chap_size);
3724         if (ha->chap_list == NULL) {
3725                 ql4_printk(KERN_ERR, ha, "No memory for ha->chap_list\n");
3726                 goto exit_chap_list;
3727         }
3728
3729         memcpy(ha->chap_list, chap_flash_data, chap_size);
3730
3731 exit_chap_list:
3732         dma_free_coherent(&ha->pdev->dev, chap_size,
3733                         chap_flash_data, chap_dma);
3734 }
3735
3736 static void qla4xxx_get_param_ddb(struct ddb_entry *ddb_entry,
3737                                   struct ql4_tuple_ddb *tddb)
3738 {
3739         struct scsi_qla_host *ha;
3740         struct iscsi_cls_session *cls_sess;
3741         struct iscsi_cls_conn *cls_conn;
3742         struct iscsi_session *sess;
3743         struct iscsi_conn *conn;
3744
3745         DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
3746         ha = ddb_entry->ha;
3747         cls_sess = ddb_entry->sess;
3748         sess = cls_sess->dd_data;
3749         cls_conn = ddb_entry->conn;
3750         conn = cls_conn->dd_data;
3751
3752         tddb->tpgt = sess->tpgt;
3753         tddb->port = conn->persistent_port;
3754         strncpy(tddb->iscsi_name, sess->targetname, ISCSI_NAME_SIZE);
3755         strncpy(tddb->ip_addr, conn->persistent_address, DDB_IPADDR_LEN);
3756 }
3757
3758 static void qla4xxx_convert_param_ddb(struct dev_db_entry *fw_ddb_entry,
3759                                       struct ql4_tuple_ddb *tddb)
3760 {
3761         uint16_t options = 0;
3762
3763         tddb->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
3764         memcpy(&tddb->iscsi_name[0], &fw_ddb_entry->iscsi_name[0],
3765                min(sizeof(tddb->iscsi_name), sizeof(fw_ddb_entry->iscsi_name)));
3766
3767         options = le16_to_cpu(fw_ddb_entry->options);
3768         if (options & DDB_OPT_IPV6_DEVICE)
3769                 sprintf(tddb->ip_addr, "%pI6", fw_ddb_entry->ip_addr);
3770         else
3771                 sprintf(tddb->ip_addr, "%pI4", fw_ddb_entry->ip_addr);
3772
3773         tddb->port = le16_to_cpu(fw_ddb_entry->port);
3774 }
3775
3776 static int qla4xxx_compare_tuple_ddb(struct scsi_qla_host *ha,
3777                                      struct ql4_tuple_ddb *old_tddb,
3778                                      struct ql4_tuple_ddb *new_tddb)
3779 {
3780         if (strcmp(old_tddb->iscsi_name, new_tddb->iscsi_name))
3781                 return QLA_ERROR;
3782
3783         if (strcmp(old_tddb->ip_addr, new_tddb->ip_addr))
3784                 return QLA_ERROR;
3785
3786         if (old_tddb->port != new_tddb->port)
3787                 return QLA_ERROR;
3788
3789         DEBUG2(ql4_printk(KERN_INFO, ha,
3790                           "Match Found, fw[%d,%d,%s,%s], [%d,%d,%s,%s]",
3791                           old_tddb->port, old_tddb->tpgt, old_tddb->ip_addr,
3792                           old_tddb->iscsi_name, new_tddb->port, new_tddb->tpgt,
3793                           new_tddb->ip_addr, new_tddb->iscsi_name));
3794
3795         return QLA_SUCCESS;
3796 }
3797
3798 static int qla4xxx_is_session_exists(struct scsi_qla_host *ha,
3799                                      struct dev_db_entry *fw_ddb_entry)
3800 {
3801         struct ddb_entry *ddb_entry;
3802         struct ql4_tuple_ddb *fw_tddb = NULL;
3803         struct ql4_tuple_ddb *tmp_tddb = NULL;
3804         int idx;
3805         int ret = QLA_ERROR;
3806
3807         fw_tddb = vzalloc(sizeof(*fw_tddb));
3808         if (!fw_tddb) {
3809                 DEBUG2(ql4_printk(KERN_WARNING, ha,
3810                                   "Memory Allocation failed.\n"));
3811                 ret = QLA_SUCCESS;
3812                 goto exit_check;
3813         }
3814
3815         tmp_tddb = vzalloc(sizeof(*tmp_tddb));
3816         if (!tmp_tddb) {
3817                 DEBUG2(ql4_printk(KERN_WARNING, ha,
3818                                   "Memory Allocation failed.\n"));
3819                 ret = QLA_SUCCESS;
3820                 goto exit_check;
3821         }
3822
3823         qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb);
3824
3825         for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
3826                 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
3827                 if (ddb_entry == NULL)
3828                         continue;
3829
3830                 qla4xxx_get_param_ddb(ddb_entry, tmp_tddb);
3831                 if (!qla4xxx_compare_tuple_ddb(ha, fw_tddb, tmp_tddb)) {
3832                         ret = QLA_SUCCESS; /* found */
3833                         goto exit_check;
3834                 }
3835         }
3836
3837 exit_check:
3838         if (fw_tddb)
3839                 vfree(fw_tddb);
3840         if (tmp_tddb)
3841                 vfree(tmp_tddb);
3842         return ret;
3843 }
3844
3845 static int qla4xxx_is_flash_ddb_exists(struct scsi_qla_host *ha,
3846                                        struct list_head *list_nt,
3847                                        struct dev_db_entry *fw_ddb_entry)
3848 {
3849         struct qla_ddb_index  *nt_ddb_idx, *nt_ddb_idx_tmp;
3850         struct ql4_tuple_ddb *fw_tddb = NULL;
3851         struct ql4_tuple_ddb *tmp_tddb = NULL;
3852         int ret = QLA_ERROR;
3853
3854         fw_tddb = vzalloc(sizeof(*fw_tddb));
3855         if (!fw_tddb) {
3856                 DEBUG2(ql4_printk(KERN_WARNING, ha,
3857                                   "Memory Allocation failed.\n"));
3858                 ret = QLA_SUCCESS;
3859                 goto exit_check;
3860         }
3861
3862         tmp_tddb = vzalloc(sizeof(*tmp_tddb));
3863         if (!tmp_tddb) {
3864                 DEBUG2(ql4_printk(KERN_WARNING, ha,
3865                                   "Memory Allocation failed.\n"));
3866                 ret = QLA_SUCCESS;
3867                 goto exit_check;
3868         }
3869
3870         qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb);
3871
3872         list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
3873                 qla4xxx_convert_param_ddb(&nt_ddb_idx->fw_ddb, tmp_tddb);
3874                 if (!qla4xxx_compare_tuple_ddb(ha, fw_tddb, tmp_tddb)) {
3875                         ret = QLA_SUCCESS; /* found */
3876                         goto exit_check;
3877                 }
3878         }
3879
3880 exit_check:
3881         if (fw_tddb)
3882                 vfree(fw_tddb);
3883         if (tmp_tddb)
3884                 vfree(tmp_tddb);
3885         return ret;
3886 }
3887
3888 static void qla4xxx_free_ddb_list(struct list_head *list_ddb)
3889 {
3890         struct qla_ddb_index  *ddb_idx, *ddb_idx_tmp;
3891
3892         list_for_each_entry_safe(ddb_idx, ddb_idx_tmp, list_ddb, list) {
3893                 list_del_init(&ddb_idx->list);
3894                 vfree(ddb_idx);
3895         }
3896 }
3897
3898 static struct iscsi_endpoint *qla4xxx_get_ep_fwdb(struct scsi_qla_host *ha,
3899                                         struct dev_db_entry *fw_ddb_entry)
3900 {
3901         struct iscsi_endpoint *ep;
3902         struct sockaddr_in *addr;
3903         struct sockaddr_in6 *addr6;
3904         struct sockaddr *dst_addr;
3905         char *ip;
3906
3907         /* TODO: need to destroy on unload iscsi_endpoint*/
3908         dst_addr = vmalloc(sizeof(*dst_addr));
3909         if (!dst_addr)
3910                 return NULL;
3911
3912         if (fw_ddb_entry->options & DDB_OPT_IPV6_DEVICE) {
3913                 dst_addr->sa_family = AF_INET6;
3914                 addr6 = (struct sockaddr_in6 *)dst_addr;
3915                 ip = (char *)&addr6->sin6_addr;
3916                 memcpy(ip, fw_ddb_entry->ip_addr, IPv6_ADDR_LEN);
3917                 addr6->sin6_port = htons(le16_to_cpu(fw_ddb_entry->port));
3918
3919         } else {
3920                 dst_addr->sa_family = AF_INET;
3921                 addr = (struct sockaddr_in *)dst_addr;
3922                 ip = (char *)&addr->sin_addr;
3923                 memcpy(ip, fw_ddb_entry->ip_addr, IP_ADDR_LEN);
3924                 addr->sin_port = htons(le16_to_cpu(fw_ddb_entry->port));
3925         }
3926
3927         ep = qla4xxx_ep_connect(ha->host, dst_addr, 0);
3928         vfree(dst_addr);
3929         return ep;
3930 }
3931
3932 static int qla4xxx_verify_boot_idx(struct scsi_qla_host *ha, uint16_t idx)
3933 {
3934         if (ql4xdisablesysfsboot)
3935                 return QLA_SUCCESS;
3936         if (idx == ha->pri_ddb_idx || idx == ha->sec_ddb_idx)
3937                 return QLA_ERROR;
3938         return QLA_SUCCESS;
3939 }
3940
3941 static void qla4xxx_setup_flash_ddb_entry(struct scsi_qla_host *ha,
3942                                           struct ddb_entry *ddb_entry)
3943 {
3944         uint16_t def_timeout;
3945
3946         ddb_entry->ddb_type = FLASH_DDB;
3947         ddb_entry->fw_ddb_index = INVALID_ENTRY;
3948         ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
3949         ddb_entry->ha = ha;
3950         ddb_entry->unblock_sess = qla4xxx_unblock_flash_ddb;
3951         ddb_entry->ddb_change = qla4xxx_flash_ddb_change;
3952
3953         atomic_set(&ddb_entry->retry_relogin_timer, INVALID_ENTRY);
3954         atomic_set(&ddb_entry->relogin_timer, 0);
3955         atomic_set(&ddb_entry->relogin_retry_count, 0);
3956         def_timeout = le16_to_cpu(ddb_entry->fw_ddb_entry.def_timeout);
3957         ddb_entry->default_relogin_timeout =
3958                 (def_timeout > LOGIN_TOV) && (def_timeout < LOGIN_TOV * 10) ?
3959                 def_timeout : LOGIN_TOV;
3960         ddb_entry->default_time2wait =
3961                 le16_to_cpu(ddb_entry->fw_ddb_entry.iscsi_def_time2wait);
3962 }
3963
3964 static void qla4xxx_wait_for_ip_configuration(struct scsi_qla_host *ha)
3965 {
3966         uint32_t idx = 0;
3967         uint32_t ip_idx[IP_ADDR_COUNT] = {0, 1, 2, 3}; /* 4 IP interfaces */
3968         uint32_t sts[MBOX_REG_COUNT];
3969         uint32_t ip_state;
3970         unsigned long wtime;
3971         int ret;
3972
3973         wtime = jiffies + (HZ * IP_CONFIG_TOV);
3974         do {
3975                 for (idx = 0; idx < IP_ADDR_COUNT; idx++) {
3976                         if (ip_idx[idx] == -1)
3977                                 continue;
3978
3979                         ret = qla4xxx_get_ip_state(ha, 0, ip_idx[idx], sts);
3980
3981                         if (ret == QLA_ERROR) {
3982                                 ip_idx[idx] = -1;
3983                                 continue;
3984                         }
3985
3986                         ip_state = (sts[1] & IP_STATE_MASK) >> IP_STATE_SHIFT;
3987
3988                         DEBUG2(ql4_printk(KERN_INFO, ha,
3989                                           "Waiting for IP state for idx = %d, state = 0x%x\n",
3990                                           ip_idx[idx], ip_state));
3991                         if (ip_state == IP_ADDRSTATE_UNCONFIGURED ||
3992                             ip_state == IP_ADDRSTATE_INVALID ||
3993                             ip_state == IP_ADDRSTATE_PREFERRED ||
3994                             ip_state == IP_ADDRSTATE_DEPRICATED ||
3995                             ip_state == IP_ADDRSTATE_DISABLING)
3996                                 ip_idx[idx] = -1;
3997                 }
3998
3999                 /* Break if all IP states checked */
4000                 if ((ip_idx[0] == -1) &&
4001                     (ip_idx[1] == -1) &&
4002                     (ip_idx[2] == -1) &&
4003                     (ip_idx[3] == -1))
4004                         break;
4005                 schedule_timeout_uninterruptible(HZ);
4006         } while (time_after(wtime, jiffies));
4007 }
4008
4009 static void qla4xxx_build_st_list(struct scsi_qla_host *ha,
4010                                   struct list_head *list_st)
4011 {
4012         struct qla_ddb_index  *st_ddb_idx;
4013         int max_ddbs;
4014         int fw_idx_size;
4015         struct dev_db_entry *fw_ddb_entry;
4016         dma_addr_t fw_ddb_dma;
4017         int ret;
4018         uint32_t idx = 0, next_idx = 0;
4019         uint32_t state = 0, conn_err = 0;
4020         uint16_t conn_id = 0;
4021
4022         fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
4023                                       &fw_ddb_dma);
4024         if (fw_ddb_entry == NULL) {
4025                 DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
4026                 goto exit_st_list;
4027         }
4028
4029         max_ddbs =  is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
4030                                      MAX_DEV_DB_ENTRIES;
4031         fw_idx_size = sizeof(struct qla_ddb_index);
4032
4033         for (idx = 0; idx < max_ddbs; idx = next_idx) {
4034                 ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry, fw_ddb_dma,
4035                                               NULL, &next_idx, &state,
4036                                               &conn_err, NULL, &conn_id);
4037                 if (ret == QLA_ERROR)
4038                         break;
4039
4040                 /* Check if ST, add to the list_st */
4041                 if (strlen((char *) fw_ddb_entry->iscsi_name) != 0)
4042                         goto continue_next_st;
4043
4044                 st_ddb_idx = vzalloc(fw_idx_size);
4045                 if (!st_ddb_idx)
4046                         break;
4047
4048                 st_ddb_idx->fw_ddb_idx = idx;
4049
4050                 list_add_tail(&st_ddb_idx->list, list_st);
4051 continue_next_st:
4052                 if (next_idx == 0)
4053                         break;
4054         }
4055
4056 exit_st_list:
4057         if (fw_ddb_entry)
4058                 dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
4059 }
4060
4061 /**
4062  * qla4xxx_remove_failed_ddb - Remove inactive or failed ddb from list
4063  * @ha: pointer to adapter structure
4064  * @list_ddb: List from which failed ddb to be removed
4065  *
4066  * Iterate over the list of DDBs and find and remove DDBs that are either in
4067  * no connection active state or failed state
4068  **/
4069 static void qla4xxx_remove_failed_ddb(struct scsi_qla_host *ha,
4070                                       struct list_head *list_ddb)
4071 {
4072         struct qla_ddb_index  *ddb_idx, *ddb_idx_tmp;
4073         uint32_t next_idx = 0;
4074         uint32_t state = 0, conn_err = 0;
4075         int ret;
4076
4077         list_for_each_entry_safe(ddb_idx, ddb_idx_tmp, list_ddb, list) {
4078                 ret = qla4xxx_get_fwddb_entry(ha, ddb_idx->fw_ddb_idx,
4079                                               NULL, 0, NULL, &next_idx, &state,
4080                                               &conn_err, NULL, NULL);
4081                 if (ret == QLA_ERROR)
4082                         continue;
4083
4084                 if (state == DDB_DS_NO_CONNECTION_ACTIVE ||
4085                     state == DDB_DS_SESSION_FAILED) {
4086                         list_del_init(&ddb_idx->list);
4087                         vfree(ddb_idx);
4088                 }
4089         }
4090 }
4091
4092 static int qla4xxx_sess_conn_setup(struct scsi_qla_host *ha,
4093                                    struct dev_db_entry *fw_ddb_entry,
4094                                    int is_reset)
4095 {
4096         struct iscsi_cls_session *cls_sess;
4097         struct iscsi_session *sess;
4098         struct iscsi_cls_conn *cls_conn;
4099         struct iscsi_endpoint *ep;
4100         uint16_t cmds_max = 32;
4101         uint16_t conn_id = 0;
4102         uint32_t initial_cmdsn = 0;
4103         int ret = QLA_SUCCESS;
4104
4105         struct ddb_entry *ddb_entry = NULL;
4106
4107         /* Create session object, with INVALID_ENTRY,
4108          * the targer_id would get set when we issue the login
4109          */
4110         cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport, ha->host,
4111                                        cmds_max, sizeof(struct ddb_entry),
4112                                        sizeof(struct ql4_task_data),
4113                                        initial_cmdsn, INVALID_ENTRY);
4114         if (!cls_sess) {
4115                 ret = QLA_ERROR;
4116                 goto exit_setup;
4117         }
4118
4119         /*
4120          * so calling module_put function to decrement the
4121          * reference count.
4122          **/
4123         module_put(qla4xxx_iscsi_transport.owner);
4124         sess = cls_sess->dd_data;
4125         ddb_entry = sess->dd_data;
4126         ddb_entry->sess = cls_sess;
4127
4128         cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
4129         memcpy(&ddb_entry->fw_ddb_entry, fw_ddb_entry,
4130                sizeof(struct dev_db_entry));
4131
4132         qla4xxx_setup_flash_ddb_entry(ha, ddb_entry);
4133
4134         cls_conn = iscsi_conn_setup(cls_sess, sizeof(struct qla_conn), conn_id);
4135
4136         if (!cls_conn) {
4137                 ret = QLA_ERROR;
4138                 goto exit_setup;
4139         }
4140
4141         ddb_entry->conn = cls_conn;
4142
4143         /* Setup ep, for displaying attributes in sysfs */
4144         ep = qla4xxx_get_ep_fwdb(ha, fw_ddb_entry);
4145         if (ep) {
4146                 ep->conn = cls_conn;
4147                 cls_conn->ep = ep;
4148         } else {
4149                 DEBUG2(ql4_printk(KERN_ERR, ha, "Unable to get ep\n"));
4150                 ret = QLA_ERROR;
4151                 goto exit_setup;
4152         }
4153
4154         /* Update sess/conn params */
4155         qla4xxx_copy_fwddb_param(ha, fw_ddb_entry, cls_sess, cls_conn);
4156
4157         if (is_reset == RESET_ADAPTER) {
4158                 iscsi_block_session(cls_sess);
4159                 /* Use the relogin path to discover new devices
4160                  *  by short-circuting the logic of setting
4161                  *  timer to relogin - instead set the flags
4162                  *  to initiate login right away.
4163                  */
4164                 set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
4165                 set_bit(DF_RELOGIN, &ddb_entry->flags);
4166         }
4167
4168 exit_setup:
4169         return ret;
4170 }
4171
4172 static void qla4xxx_build_nt_list(struct scsi_qla_host *ha,
4173                                   struct list_head *list_nt, int is_reset)
4174 {
4175         struct dev_db_entry *fw_ddb_entry;
4176         dma_addr_t fw_ddb_dma;
4177         int max_ddbs;
4178         int fw_idx_size;
4179         int ret;
4180         uint32_t idx = 0, next_idx = 0;
4181         uint32_t state = 0, conn_err = 0;
4182         uint16_t conn_id = 0;
4183         struct qla_ddb_index  *nt_ddb_idx;
4184
4185         fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
4186                                       &fw_ddb_dma);
4187         if (fw_ddb_entry == NULL) {
4188                 DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
4189                 goto exit_nt_list;
4190         }
4191         max_ddbs =  is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
4192                                      MAX_DEV_DB_ENTRIES;
4193         fw_idx_size = sizeof(struct qla_ddb_index);
4194
4195         for (idx = 0; idx < max_ddbs; idx = next_idx) {
4196                 ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry, fw_ddb_dma,
4197                                               NULL, &next_idx, &state,
4198                                               &conn_err, NULL, &conn_id);
4199                 if (ret == QLA_ERROR)
4200                         break;
4201
4202                 if (qla4xxx_verify_boot_idx(ha, idx) != QLA_SUCCESS)
4203                         goto continue_next_nt;
4204
4205                 /* Check if NT, then add to list it */
4206                 if (strlen((char *) fw_ddb_entry->iscsi_name) == 0)
4207                         goto continue_next_nt;
4208
4209                 if (!(state == DDB_DS_NO_CONNECTION_ACTIVE ||
4210                     state == DDB_DS_SESSION_FAILED))
4211                         goto continue_next_nt;
4212
4213                 DEBUG2(ql4_printk(KERN_INFO, ha,
4214                                   "Adding  DDB to session = 0x%x\n", idx));
4215                 if (is_reset == INIT_ADAPTER) {
4216                         nt_ddb_idx = vmalloc(fw_idx_size);
4217                         if (!nt_ddb_idx)
4218                                 break;
4219
4220                         nt_ddb_idx->fw_ddb_idx = idx;
4221
4222                         memcpy(&nt_ddb_idx->fw_ddb, fw_ddb_entry,
4223                                sizeof(struct dev_db_entry));
4224
4225                         if (qla4xxx_is_flash_ddb_exists(ha, list_nt,
4226                                         fw_ddb_entry) == QLA_SUCCESS) {
4227                                 vfree(nt_ddb_idx);
4228                                 goto continue_next_nt;
4229                         }
4230                         list_add_tail(&nt_ddb_idx->list, list_nt);
4231                 } else if (is_reset == RESET_ADAPTER) {
4232                         if (qla4xxx_is_session_exists(ha, fw_ddb_entry) ==
4233                                                                 QLA_SUCCESS)
4234                                 goto continue_next_nt;
4235                 }
4236
4237                 ret = qla4xxx_sess_conn_setup(ha, fw_ddb_entry, is_reset);
4238                 if (ret == QLA_ERROR)
4239                         goto exit_nt_list;
4240
4241 continue_next_nt:
4242                 if (next_idx == 0)
4243                         break;
4244         }
4245
4246 exit_nt_list:
4247         if (fw_ddb_entry)
4248                 dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
4249 }
4250
4251 /**
4252  * qla4xxx_build_ddb_list - Build ddb list and setup sessions
4253  * @ha: pointer to adapter structure
4254  * @is_reset: Is this init path or reset path
4255  *
4256  * Create a list of sendtargets (st) from firmware DDBs, issue send targets
4257  * using connection open, then create the list of normal targets (nt)
4258  * from firmware DDBs. Based on the list of nt setup session and connection
4259  * objects.
4260  **/
4261 void qla4xxx_build_ddb_list(struct scsi_qla_host *ha, int is_reset)
4262 {
4263         uint16_t tmo = 0;
4264         struct list_head list_st, list_nt;
4265         struct qla_ddb_index  *st_ddb_idx, *st_ddb_idx_tmp;
4266         unsigned long wtime;
4267
4268         if (!test_bit(AF_LINK_UP, &ha->flags)) {
4269                 set_bit(AF_BUILD_DDB_LIST, &ha->flags);
4270                 ha->is_reset = is_reset;
4271                 return;
4272         }
4273
4274         INIT_LIST_HEAD(&list_st);
4275         INIT_LIST_HEAD(&list_nt);
4276
4277         qla4xxx_build_st_list(ha, &list_st);
4278
4279         /* Before issuing conn open mbox, ensure all IPs states are configured
4280          * Note, conn open fails if IPs are not configured
4281          */
4282         qla4xxx_wait_for_ip_configuration(ha);
4283
4284         /* Go thru the STs and fire the sendtargets by issuing conn open mbx */
4285         list_for_each_entry_safe(st_ddb_idx, st_ddb_idx_tmp, &list_st, list) {
4286                 qla4xxx_conn_open(ha, st_ddb_idx->fw_ddb_idx);
4287         }
4288
4289         /* Wait to ensure all sendtargets are done for min 12 sec wait */
4290         tmo = ((ha->def_timeout > LOGIN_TOV) &&
4291                (ha->def_timeout < LOGIN_TOV * 10) ?
4292                ha->def_timeout : LOGIN_TOV);
4293
4294         DEBUG2(ql4_printk(KERN_INFO, ha,
4295                           "Default time to wait for build ddb %d\n", tmo));
4296
4297         wtime = jiffies + (HZ * tmo);
4298         do {
4299                 if (list_empty(&list_st))
4300                         break;
4301
4302                 qla4xxx_remove_failed_ddb(ha, &list_st);
4303                 schedule_timeout_uninterruptible(HZ / 10);
4304         } while (time_after(wtime, jiffies));
4305
4306         /* Free up the sendtargets list */
4307         qla4xxx_free_ddb_list(&list_st);
4308
4309         qla4xxx_build_nt_list(ha, &list_nt, is_reset);
4310
4311         qla4xxx_free_ddb_list(&list_nt);
4312
4313         qla4xxx_free_ddb_index(ha);
4314 }
4315
4316 /**
4317  * qla4xxx_probe_adapter - callback function to probe HBA
4318  * @pdev: pointer to pci_dev structure
4319  * @pci_device_id: pointer to pci_device entry
4320  *
4321  * This routine will probe for Qlogic 4xxx iSCSI host adapters.
4322  * It returns zero if successful. It also initializes all data necessary for
4323  * the driver.
4324  **/
4325 static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
4326                                            const struct pci_device_id *ent)
4327 {
4328         int ret = -ENODEV, status;
4329         struct Scsi_Host *host;
4330         struct scsi_qla_host *ha;
4331         uint8_t init_retry_count = 0;
4332         char buf[34];
4333         struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
4334         uint32_t dev_state;
4335
4336         if (pci_enable_device(pdev))
4337                 return -1;
4338
4339         host = iscsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha), 0);
4340         if (host == NULL) {
4341                 printk(KERN_WARNING
4342                        "qla4xxx: Couldn't allocate host from scsi layer!\n");
4343                 goto probe_disable_device;
4344         }
4345
4346         /* Clear our data area */
4347         ha = to_qla_host(host);
4348         memset(ha, 0, sizeof(*ha));
4349
4350         /* Save the information from PCI BIOS.  */
4351         ha->pdev = pdev;
4352         ha->host = host;
4353         ha->host_no = host->host_no;
4354
4355         pci_enable_pcie_error_reporting(pdev);
4356
4357         /* Setup Runtime configurable options */
4358         if (is_qla8022(ha)) {
4359                 ha->isp_ops = &qla4_8xxx_isp_ops;
4360                 rwlock_init(&ha->hw_lock);
4361                 ha->qdr_sn_window = -1;
4362                 ha->ddr_mn_window = -1;
4363                 ha->curr_window = 255;
4364                 ha->func_num = PCI_FUNC(ha->pdev->devfn);
4365                 nx_legacy_intr = &legacy_intr[ha->func_num];
4366                 ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
4367                 ha->nx_legacy_intr.tgt_status_reg =
4368                         nx_legacy_intr->tgt_status_reg;
4369                 ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
4370                 ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
4371         } else {
4372                 ha->isp_ops = &qla4xxx_isp_ops;
4373         }
4374
4375         /* Set EEH reset type to fundamental if required by hba */
4376         if (is_qla8022(ha))
4377                 pdev->needs_freset = 1;
4378
4379         /* Configure PCI I/O space. */
4380         ret = ha->isp_ops->iospace_config(ha);
4381         if (ret)
4382                 goto probe_failed_ioconfig;
4383
4384         ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
4385                    pdev->device, pdev->irq, ha->reg);
4386
4387         qla4xxx_config_dma_addressing(ha);
4388
4389         /* Initialize lists and spinlocks. */
4390         INIT_LIST_HEAD(&ha->free_srb_q);
4391
4392         mutex_init(&ha->mbox_sem);
4393         mutex_init(&ha->chap_sem);
4394         init_completion(&ha->mbx_intr_comp);
4395         init_completion(&ha->disable_acb_comp);
4396
4397         spin_lock_init(&ha->hardware_lock);
4398
4399         /* Allocate dma buffers */
4400         if (qla4xxx_mem_alloc(ha)) {
4401                 ql4_printk(KERN_WARNING, ha,
4402                     "[ERROR] Failed to allocate memory for adapter\n");
4403
4404                 ret = -ENOMEM;
4405                 goto probe_failed;
4406         }
4407
4408         host->cmd_per_lun = 3;
4409         host->max_channel = 0;
4410         host->max_lun = MAX_LUNS - 1;
4411         host->max_id = MAX_TARGETS;
4412         host->max_cmd_len = IOCB_MAX_CDB_LEN;
4413         host->can_queue = MAX_SRBS ;
4414         host->transportt = qla4xxx_scsi_transport;
4415
4416         ret = scsi_init_shared_tag_map(host, MAX_SRBS);
4417         if (ret) {
4418                 ql4_printk(KERN_WARNING, ha,
4419                            "%s: scsi_init_shared_tag_map failed\n", __func__);
4420                 goto probe_failed;
4421         }
4422
4423         pci_set_drvdata(pdev, ha);
4424
4425         ret = scsi_add_host(host, &pdev->dev);
4426         if (ret)
4427                 goto probe_failed;
4428
4429         if (is_qla8022(ha))
4430                 (void) qla4_8xxx_get_flash_info(ha);
4431
4432         /*
4433          * Initialize the Host adapter request/response queues and
4434          * firmware
4435          * NOTE: interrupts enabled upon successful completion
4436          */
4437         status = qla4xxx_initialize_adapter(ha, INIT_ADAPTER);
4438         while ((!test_bit(AF_ONLINE, &ha->flags)) &&
4439             init_retry_count++ < MAX_INIT_RETRIES) {
4440
4441                 if (is_qla8022(ha)) {
4442                         qla4_8xxx_idc_lock(ha);
4443                         dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
4444                         qla4_8xxx_idc_unlock(ha);
4445                         if (dev_state == QLA82XX_DEV_FAILED) {
4446                                 ql4_printk(KERN_WARNING, ha, "%s: don't retry "
4447                                     "initialize adapter. H/W is in failed state\n",
4448                                     __func__);
4449                                 break;
4450                         }
4451                 }
4452                 DEBUG2(printk("scsi: %s: retrying adapter initialization "
4453                               "(%d)\n", __func__, init_retry_count));
4454
4455                 if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
4456                         continue;
4457
4458                 status = qla4xxx_initialize_adapter(ha, INIT_ADAPTER);
4459         }
4460
4461         if (!test_bit(AF_ONLINE, &ha->flags)) {
4462                 ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
4463
4464                 if (is_qla8022(ha) && ql4xdontresethba) {
4465                         /* Put the device in failed state. */
4466                         DEBUG2(printk(KERN_ERR "HW STATE: FAILED\n"));
4467                         qla4_8xxx_idc_lock(ha);
4468                         qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
4469                             QLA82XX_DEV_FAILED);
4470                         qla4_8xxx_idc_unlock(ha);
4471                 }
4472                 ret = -ENODEV;
4473                 goto remove_host;
4474         }
4475
4476         /* Startup the kernel thread for this host adapter. */
4477         DEBUG2(printk("scsi: %s: Starting kernel thread for "
4478                       "qla4xxx_dpc\n", __func__));
4479         sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
4480         ha->dpc_thread = create_singlethread_workqueue(buf);
4481         if (!ha->dpc_thread) {
4482                 ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
4483                 ret = -ENODEV;
4484                 goto remove_host;
4485         }
4486         INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
4487
4488         sprintf(buf, "qla4xxx_%lu_task", ha->host_no);
4489         ha->task_wq = alloc_workqueue(buf, WQ_MEM_RECLAIM, 1);
4490         if (!ha->task_wq) {
4491                 ql4_printk(KERN_WARNING, ha, "Unable to start task thread!\n");
4492                 ret = -ENODEV;
4493                 goto remove_host;
4494         }
4495
4496         /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
4497          * (which is called indirectly by qla4xxx_initialize_adapter),
4498          * so that irqs will be registered after crbinit but before
4499          * mbx_intr_enable.
4500          */
4501         if (!is_qla8022(ha)) {
4502                 ret = qla4xxx_request_irqs(ha);
4503                 if (ret) {
4504                         ql4_printk(KERN_WARNING, ha, "Failed to reserve "
4505                             "interrupt %d already in use.\n", pdev->irq);
4506                         goto remove_host;
4507                 }
4508         }
4509
4510         pci_save_state(ha->pdev);
4511         ha->isp_ops->enable_intrs(ha);
4512
4513         /* Start timer thread. */
4514         qla4xxx_start_timer(ha, qla4xxx_timer, 1);
4515
4516         set_bit(AF_INIT_DONE, &ha->flags);
4517
4518         printk(KERN_INFO
4519                " QLogic iSCSI HBA Driver version: %s\n"
4520                "  QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
4521                qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
4522                ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
4523                ha->patch_number, ha->build_number);
4524
4525         if (qla4xxx_setup_boot_info(ha))
4526                 ql4_printk(KERN_ERR, ha, "%s:ISCSI boot info setup failed\n",
4527                            __func__);
4528
4529                 /* Perform the build ddb list and login to each */
4530         qla4xxx_build_ddb_list(ha, INIT_ADAPTER);
4531         iscsi_host_for_each_session(ha->host, qla4xxx_login_flash_ddb);
4532
4533         qla4xxx_create_chap_list(ha);
4534
4535         qla4xxx_create_ifaces(ha);
4536         return 0;
4537
4538 remove_host:
4539         scsi_remove_host(ha->host);
4540
4541 probe_failed:
4542         qla4xxx_free_adapter(ha);
4543
4544 probe_failed_ioconfig:
4545         pci_disable_pcie_error_reporting(pdev);
4546         scsi_host_put(ha->host);
4547
4548 probe_disable_device:
4549         pci_disable_device(pdev);
4550
4551         return ret;
4552 }
4553
4554 /**
4555  * qla4xxx_prevent_other_port_reinit - prevent other port from re-initialize
4556  * @ha: pointer to adapter structure
4557  *
4558  * Mark the other ISP-4xxx port to indicate that the driver is being removed,
4559  * so that the other port will not re-initialize while in the process of
4560  * removing the ha due to driver unload or hba hotplug.
4561  **/
4562 static void qla4xxx_prevent_other_port_reinit(struct scsi_qla_host *ha)
4563 {
4564         struct scsi_qla_host *other_ha = NULL;
4565         struct pci_dev *other_pdev = NULL;
4566         int fn = ISP4XXX_PCI_FN_2;
4567
4568         /*iscsi function numbers for ISP4xxx is 1 and 3*/
4569         if (PCI_FUNC(ha->pdev->devfn) & BIT_1)
4570                 fn = ISP4XXX_PCI_FN_1;
4571
4572         other_pdev =
4573                 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
4574                 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
4575                 fn));
4576
4577         /* Get other_ha if other_pdev is valid and state is enable*/
4578         if (other_pdev) {
4579                 if (atomic_read(&other_pdev->enable_cnt)) {
4580                         other_ha = pci_get_drvdata(other_pdev);
4581                         if (other_ha) {
4582                                 set_bit(AF_HA_REMOVAL, &other_ha->flags);
4583                                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: "
4584                                     "Prevent %s reinit\n", __func__,
4585                                     dev_name(&other_ha->pdev->dev)));
4586                         }
4587                 }
4588                 pci_dev_put(other_pdev);
4589         }
4590 }
4591
4592 static void qla4xxx_destroy_fw_ddb_session(struct scsi_qla_host *ha)
4593 {
4594         struct ddb_entry *ddb_entry;
4595         int options;
4596         int idx;
4597
4598         for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
4599
4600                 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
4601                 if ((ddb_entry != NULL) &&
4602                     (ddb_entry->ddb_type == FLASH_DDB)) {
4603
4604                         options = LOGOUT_OPTION_CLOSE_SESSION;
4605                         if (qla4xxx_session_logout_ddb(ha, ddb_entry, options)
4606                             == QLA_ERROR)
4607                                 ql4_printk(KERN_ERR, ha, "%s: Logout failed\n",
4608                                            __func__);
4609
4610                         qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
4611                         /*
4612                          * we have decremented the reference count of the driver
4613                          * when we setup the session to have the driver unload
4614                          * to be seamless without actually destroying the
4615                          * session
4616                          **/
4617                         try_module_get(qla4xxx_iscsi_transport.owner);
4618                         iscsi_destroy_endpoint(ddb_entry->conn->ep);
4619                         qla4xxx_free_ddb(ha, ddb_entry);
4620                         iscsi_session_teardown(ddb_entry->sess);
4621                 }
4622         }
4623 }
4624 /**
4625  * qla4xxx_remove_adapter - calback function to remove adapter.
4626  * @pci_dev: PCI device pointer
4627  **/
4628 static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
4629 {
4630         struct scsi_qla_host *ha;
4631
4632         ha = pci_get_drvdata(pdev);
4633
4634         if (!is_qla8022(ha))
4635                 qla4xxx_prevent_other_port_reinit(ha);
4636
4637         /* destroy iface from sysfs */
4638         qla4xxx_destroy_ifaces(ha);
4639
4640         if ((!ql4xdisablesysfsboot) && ha->boot_kset)
4641                 iscsi_boot_destroy_kset(ha->boot_kset);
4642
4643         qla4xxx_destroy_fw_ddb_session(ha);
4644
4645         scsi_remove_host(ha->host);
4646
4647         qla4xxx_free_adapter(ha);
4648
4649         scsi_host_put(ha->host);
4650
4651         pci_disable_pcie_error_reporting(pdev);
4652         pci_disable_device(pdev);
4653         pci_set_drvdata(pdev, NULL);
4654 }
4655
4656 /**
4657  * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
4658  * @ha: HA context
4659  *
4660  * At exit, the @ha's flags.enable_64bit_addressing set to indicated
4661  * supported addressing method.
4662  */
4663 static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
4664 {
4665         int retval;
4666
4667         /* Update our PCI device dma_mask for full 64 bit mask */
4668         if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
4669                 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
4670                         dev_dbg(&ha->pdev->dev,
4671                                   "Failed to set 64 bit PCI consistent mask; "
4672                                    "using 32 bit.\n");
4673                         retval = pci_set_consistent_dma_mask(ha->pdev,
4674                                                              DMA_BIT_MASK(32));
4675                 }
4676         } else
4677                 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
4678 }
4679
4680 static int qla4xxx_slave_alloc(struct scsi_device *sdev)
4681 {
4682         struct iscsi_cls_session *cls_sess;
4683         struct iscsi_session *sess;
4684         struct ddb_entry *ddb;
4685         int queue_depth = QL4_DEF_QDEPTH;
4686
4687         cls_sess = starget_to_session(sdev->sdev_target);
4688         sess = cls_sess->dd_data;
4689         ddb = sess->dd_data;
4690
4691         sdev->hostdata = ddb;
4692         sdev->tagged_supported = 1;
4693
4694         if (ql4xmaxqdepth != 0 && ql4xmaxqdepth <= 0xffffU)
4695                 queue_depth = ql4xmaxqdepth;
4696
4697         scsi_activate_tcq(sdev, queue_depth);
4698         return 0;
4699 }
4700
4701 static int qla4xxx_slave_configure(struct scsi_device *sdev)
4702 {
4703         sdev->tagged_supported = 1;
4704         return 0;
4705 }
4706
4707 static void qla4xxx_slave_destroy(struct scsi_device *sdev)
4708 {
4709         scsi_deactivate_tcq(sdev, 1);
4710 }
4711
4712 /**
4713  * qla4xxx_del_from_active_array - returns an active srb
4714  * @ha: Pointer to host adapter structure.
4715  * @index: index into the active_array
4716  *
4717  * This routine removes and returns the srb at the specified index
4718  **/
4719 struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
4720     uint32_t index)
4721 {
4722         struct srb *srb = NULL;
4723         struct scsi_cmnd *cmd = NULL;
4724
4725         cmd = scsi_host_find_tag(ha->host, index);
4726         if (!cmd)
4727                 return srb;
4728
4729         srb = (struct srb *)CMD_SP(cmd);
4730         if (!srb)
4731                 return srb;
4732
4733         /* update counters */
4734         if (srb->flags & SRB_DMA_VALID) {
4735                 ha->req_q_count += srb->iocb_cnt;
4736                 ha->iocb_cnt -= srb->iocb_cnt;
4737                 if (srb->cmd)
4738                         srb->cmd->host_scribble =
4739                                 (unsigned char *)(unsigned long) MAX_SRBS;
4740         }
4741         return srb;
4742 }
4743
4744 /**
4745  * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
4746  * @ha: Pointer to host adapter structure.
4747  * @cmd: Scsi Command to wait on.
4748  *
4749  * This routine waits for the command to be returned by the Firmware
4750  * for some max time.
4751  **/
4752 static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
4753                                       struct scsi_cmnd *cmd)
4754 {
4755         int done = 0;
4756         struct srb *rp;
4757         uint32_t max_wait_time = EH_WAIT_CMD_TOV;
4758         int ret = SUCCESS;
4759
4760         /* Dont wait on command if PCI error is being handled
4761          * by PCI AER driver
4762          */
4763         if (unlikely(pci_channel_offline(ha->pdev)) ||
4764             (test_bit(AF_EEH_BUSY, &ha->flags))) {
4765                 ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
4766                     ha->host_no, __func__);
4767                 return ret;
4768         }
4769
4770         do {
4771                 /* Checking to see if its returned to OS */
4772                 rp = (struct srb *) CMD_SP(cmd);
4773                 if (rp == NULL) {
4774                         done++;
4775                         break;
4776                 }
4777
4778                 msleep(2000);
4779         } while (max_wait_time--);
4780
4781         return done;
4782 }
4783
4784 /**
4785  * qla4xxx_wait_for_hba_online - waits for HBA to come online
4786  * @ha: Pointer to host adapter structure
4787  **/
4788 static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
4789 {
4790         unsigned long wait_online;
4791
4792         wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
4793         while (time_before(jiffies, wait_online)) {
4794
4795                 if (adapter_up(ha))
4796                         return QLA_SUCCESS;
4797
4798                 msleep(2000);
4799         }
4800
4801         return QLA_ERROR;
4802 }
4803
4804 /**
4805  * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
4806  * @ha: pointer to HBA
4807  * @t: target id
4808  * @l: lun id
4809  *
4810  * This function waits for all outstanding commands to a lun to complete. It
4811  * returns 0 if all pending commands are returned and 1 otherwise.
4812  **/
4813 static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
4814                                         struct scsi_target *stgt,
4815                                         struct scsi_device *sdev)
4816 {
4817         int cnt;
4818         int status = 0;
4819         struct scsi_cmnd *cmd;
4820
4821         /*
4822          * Waiting for all commands for the designated target or dev
4823          * in the active array
4824          */
4825         for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
4826                 cmd = scsi_host_find_tag(ha->host, cnt);
4827                 if (cmd && stgt == scsi_target(cmd->device) &&
4828                     (!sdev || sdev == cmd->device)) {
4829                         if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
4830                                 status++;
4831                                 break;
4832                         }
4833                 }
4834         }
4835         return status;
4836 }
4837
4838 /**
4839  * qla4xxx_eh_abort - callback for abort task.
4840  * @cmd: Pointer to Linux's SCSI command structure
4841  *
4842  * This routine is called by the Linux OS to abort the specified
4843  * command.
4844  **/
4845 static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
4846 {
4847         struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
4848         unsigned int id = cmd->device->id;
4849         unsigned int lun = cmd->device->lun;
4850         unsigned long flags;
4851         struct srb *srb = NULL;
4852         int ret = SUCCESS;
4853         int wait = 0;
4854
4855         ql4_printk(KERN_INFO, ha,
4856             "scsi%ld:%d:%d: Abort command issued cmd=%p\n",
4857             ha->host_no, id, lun, cmd);
4858
4859         spin_lock_irqsave(&ha->hardware_lock, flags);
4860         srb = (struct srb *) CMD_SP(cmd);
4861         if (!srb) {
4862                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4863                 return SUCCESS;
4864         }
4865         kref_get(&srb->srb_ref);
4866         spin_unlock_irqrestore(&ha->hardware_lock, flags);
4867
4868         if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
4869                 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
4870                     ha->host_no, id, lun));
4871                 ret = FAILED;
4872         } else {
4873                 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
4874                     ha->host_no, id, lun));
4875                 wait = 1;
4876         }
4877
4878         kref_put(&srb->srb_ref, qla4xxx_srb_compl);
4879
4880         /* Wait for command to complete */
4881         if (wait) {
4882                 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
4883                         DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
4884                             ha->host_no, id, lun));
4885                         ret = FAILED;
4886                 }
4887         }
4888
4889         ql4_printk(KERN_INFO, ha,
4890             "scsi%ld:%d:%d: Abort command - %s\n",
4891             ha->host_no, id, lun, (ret == SUCCESS) ? "succeeded" : "failed");
4892
4893         return ret;
4894 }
4895
4896 /**
4897  * qla4xxx_eh_device_reset - callback for target reset.
4898  * @cmd: Pointer to Linux's SCSI command structure
4899  *
4900  * This routine is called by the Linux OS to reset all luns on the
4901  * specified target.
4902  **/
4903 static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
4904 {
4905         struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
4906         struct ddb_entry *ddb_entry = cmd->device->hostdata;
4907         int ret = FAILED, stat;
4908
4909         if (!ddb_entry)
4910                 return ret;
4911
4912         ret = iscsi_block_scsi_eh(cmd);
4913         if (ret)
4914                 return ret;
4915         ret = FAILED;
4916
4917         ql4_printk(KERN_INFO, ha,
4918                    "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
4919                    cmd->device->channel, cmd->device->id, cmd->device->lun);
4920
4921         DEBUG2(printk(KERN_INFO
4922                       "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
4923                       "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
4924                       cmd, jiffies, cmd->request->timeout / HZ,
4925                       ha->dpc_flags, cmd->result, cmd->allowed));
4926
4927         /* FIXME: wait for hba to go online */
4928         stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
4929         if (stat != QLA_SUCCESS) {
4930                 ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
4931                 goto eh_dev_reset_done;
4932         }
4933
4934         if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
4935                                          cmd->device)) {
4936                 ql4_printk(KERN_INFO, ha,
4937                            "DEVICE RESET FAILED - waiting for "
4938                            "commands.\n");
4939                 goto eh_dev_reset_done;
4940         }
4941
4942         /* Send marker. */
4943         if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
4944                 MM_LUN_RESET) != QLA_SUCCESS)
4945                 goto eh_dev_reset_done;
4946
4947         ql4_printk(KERN_INFO, ha,
4948                    "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
4949                    ha->host_no, cmd->device->channel, cmd->device->id,
4950                    cmd->device->lun);
4951
4952         ret = SUCCESS;
4953
4954 eh_dev_reset_done:
4955
4956         return ret;
4957 }
4958
4959 /**
4960  * qla4xxx_eh_target_reset - callback for target reset.
4961  * @cmd: Pointer to Linux's SCSI command structure
4962  *
4963  * This routine is called by the Linux OS to reset the target.
4964  **/
4965 static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
4966 {
4967         struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
4968         struct ddb_entry *ddb_entry = cmd->device->hostdata;
4969         int stat, ret;
4970
4971         if (!ddb_entry)
4972                 return FAILED;
4973
4974         ret = iscsi_block_scsi_eh(cmd);
4975         if (ret)
4976                 return ret;
4977
4978         starget_printk(KERN_INFO, scsi_target(cmd->device),
4979                        "WARM TARGET RESET ISSUED.\n");
4980
4981         DEBUG2(printk(KERN_INFO
4982                       "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
4983                       "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
4984                       ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
4985                       ha->dpc_flags, cmd->result, cmd->allowed));
4986
4987         stat = qla4xxx_reset_target(ha, ddb_entry);
4988         if (stat != QLA_SUCCESS) {
4989                 starget_printk(KERN_INFO, scsi_target(cmd->device),
4990                                "WARM TARGET RESET FAILED.\n");
4991                 return FAILED;
4992         }
4993
4994         if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
4995                                          NULL)) {
4996                 starget_printk(KERN_INFO, scsi_target(cmd->device),
4997                                "WARM TARGET DEVICE RESET FAILED - "
4998                                "waiting for commands.\n");
4999                 return FAILED;
5000         }
5001
5002         /* Send marker. */
5003         if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
5004                 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
5005                 starget_printk(KERN_INFO, scsi_target(cmd->device),
5006                                "WARM TARGET DEVICE RESET FAILED - "
5007                                "marker iocb failed.\n");
5008                 return FAILED;
5009         }
5010
5011         starget_printk(KERN_INFO, scsi_target(cmd->device),
5012                        "WARM TARGET RESET SUCCEEDED.\n");
5013         return SUCCESS;
5014 }
5015
5016 /**
5017  * qla4xxx_is_eh_active - check if error handler is running
5018  * @shost: Pointer to SCSI Host struct
5019  *
5020  * This routine finds that if reset host is called in EH
5021  * scenario or from some application like sg_reset
5022  **/
5023 static int qla4xxx_is_eh_active(struct Scsi_Host *shost)
5024 {
5025         if (shost->shost_state == SHOST_RECOVERY)
5026                 return 1;
5027         return 0;
5028 }
5029
5030 /**
5031  * qla4xxx_eh_host_reset - kernel callback
5032  * @cmd: Pointer to Linux's SCSI command structure
5033  *
5034  * This routine is invoked by the Linux kernel to perform fatal error
5035  * recovery on the specified adapter.
5036  **/
5037 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
5038 {
5039         int return_status = FAILED;
5040         struct scsi_qla_host *ha;
5041
5042         ha = to_qla_host(cmd->device->host);
5043
5044         if (ql4xdontresethba) {
5045                 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
5046                      ha->host_no, __func__));
5047
5048                 /* Clear outstanding srb in queues */
5049                 if (qla4xxx_is_eh_active(cmd->device->host))
5050                         qla4xxx_abort_active_cmds(ha, DID_ABORT << 16);
5051
5052                 return FAILED;
5053         }
5054
5055         ql4_printk(KERN_INFO, ha,
5056                    "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
5057                    cmd->device->channel, cmd->device->id, cmd->device->lun);
5058
5059         if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
5060                 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host.  Adapter "
5061                               "DEAD.\n", ha->host_no, cmd->device->channel,
5062                               __func__));
5063
5064                 return FAILED;
5065         }
5066
5067         if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
5068                 if (is_qla8022(ha))
5069                         set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
5070                 else
5071                         set_bit(DPC_RESET_HA, &ha->dpc_flags);
5072         }
5073
5074         if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
5075                 return_status = SUCCESS;
5076
5077         ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
5078                    return_status == FAILED ? "FAILED" : "SUCCEEDED");
5079
5080         return return_status;
5081 }
5082
5083 static int qla4xxx_context_reset(struct scsi_qla_host *ha)
5084 {
5085         uint32_t mbox_cmd[MBOX_REG_COUNT];
5086         uint32_t mbox_sts[MBOX_REG_COUNT];
5087         struct addr_ctrl_blk_def *acb = NULL;
5088         uint32_t acb_len = sizeof(struct addr_ctrl_blk_def);
5089         int rval = QLA_SUCCESS;
5090         dma_addr_t acb_dma;
5091
5092         acb = dma_alloc_coherent(&ha->pdev->dev,
5093                                  sizeof(struct addr_ctrl_blk_def),
5094                                  &acb_dma, GFP_KERNEL);
5095         if (!acb) {
5096                 ql4_printk(KERN_ERR, ha, "%s: Unable to alloc acb\n",
5097                            __func__);
5098                 rval = -ENOMEM;
5099                 goto exit_port_reset;
5100         }
5101
5102         memset(acb, 0, acb_len);
5103
5104         rval = qla4xxx_get_acb(ha, acb_dma, PRIMARI_ACB, acb_len);
5105         if (rval != QLA_SUCCESS) {
5106                 rval = -EIO;
5107                 goto exit_free_acb;
5108         }
5109
5110         rval = qla4xxx_disable_acb(ha);
5111         if (rval != QLA_SUCCESS) {
5112                 rval = -EIO;
5113                 goto exit_free_acb;
5114         }
5115
5116         wait_for_completion_timeout(&ha->disable_acb_comp,
5117                                     DISABLE_ACB_TOV * HZ);
5118
5119         rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], acb_dma);
5120         if (rval != QLA_SUCCESS) {
5121                 rval = -EIO;
5122                 goto exit_free_acb;
5123         }
5124
5125 exit_free_acb:
5126         dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk_def),
5127                           acb, acb_dma);
5128 exit_port_reset:
5129         DEBUG2(ql4_printk(KERN_INFO, ha, "%s %s\n", __func__,
5130                           rval == QLA_SUCCESS ? "SUCCEEDED" : "FAILED"));
5131         return rval;
5132 }
5133
5134 static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type)
5135 {
5136         struct scsi_qla_host *ha = to_qla_host(shost);
5137         int rval = QLA_SUCCESS;
5138
5139         if (ql4xdontresethba) {
5140                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Don't Reset HBA\n",
5141                                   __func__));
5142                 rval = -EPERM;
5143                 goto exit_host_reset;
5144         }
5145
5146         rval = qla4xxx_wait_for_hba_online(ha);
5147         if (rval != QLA_SUCCESS) {
5148                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Unable to reset host "
5149                                   "adapter\n", __func__));
5150                 rval = -EIO;
5151                 goto exit_host_reset;
5152         }
5153
5154         if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
5155                 goto recover_adapter;
5156
5157         switch (reset_type) {
5158         case SCSI_ADAPTER_RESET:
5159                 set_bit(DPC_RESET_HA, &ha->dpc_flags);
5160                 break;
5161         case SCSI_FIRMWARE_RESET:
5162                 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
5163                         if (is_qla8022(ha))
5164                                 /* set firmware context reset */
5165                                 set_bit(DPC_RESET_HA_FW_CONTEXT,
5166                                         &ha->dpc_flags);
5167                         else {
5168                                 rval = qla4xxx_context_reset(ha);
5169                                 goto exit_host_reset;
5170                         }
5171                 }
5172                 break;
5173         }
5174
5175 recover_adapter:
5176         rval = qla4xxx_recover_adapter(ha);
5177         if (rval != QLA_SUCCESS) {
5178                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: recover adapter fail\n",
5179                                   __func__));
5180                 rval = -EIO;
5181         }
5182
5183 exit_host_reset:
5184         return rval;
5185 }
5186
5187 /* PCI AER driver recovers from all correctable errors w/o
5188  * driver intervention. For uncorrectable errors PCI AER
5189  * driver calls the following device driver's callbacks
5190  *
5191  * - Fatal Errors - link_reset
5192  * - Non-Fatal Errors - driver's pci_error_detected() which
5193  * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
5194  *
5195  * PCI AER driver calls
5196  * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
5197  *               returns RECOVERED or NEED_RESET if fw_hung
5198  * NEED_RESET - driver's slot_reset()
5199  * DISCONNECT - device is dead & cannot recover
5200  * RECOVERED - driver's pci_resume()
5201  */
5202 static pci_ers_result_t
5203 qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
5204 {
5205         struct scsi_qla_host *ha = pci_get_drvdata(pdev);
5206
5207         ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
5208             ha->host_no, __func__, state);
5209
5210         if (!is_aer_supported(ha))
5211                 return PCI_ERS_RESULT_NONE;
5212
5213         switch (state) {
5214         case pci_channel_io_normal:
5215                 clear_bit(AF_EEH_BUSY, &ha->flags);
5216                 return PCI_ERS_RESULT_CAN_RECOVER;
5217         case pci_channel_io_frozen:
5218                 set_bit(AF_EEH_BUSY, &ha->flags);
5219                 qla4xxx_mailbox_premature_completion(ha);
5220                 qla4xxx_free_irqs(ha);
5221                 pci_disable_device(pdev);
5222                 /* Return back all IOs */
5223                 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
5224                 return PCI_ERS_RESULT_NEED_RESET;
5225         case pci_channel_io_perm_failure:
5226                 set_bit(AF_EEH_BUSY, &ha->flags);
5227                 set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
5228                 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
5229                 return PCI_ERS_RESULT_DISCONNECT;
5230         }
5231         return PCI_ERS_RESULT_NEED_RESET;
5232 }
5233
5234 /**
5235  * qla4xxx_pci_mmio_enabled() gets called if
5236  * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
5237  * and read/write to the device still works.
5238  **/
5239 static pci_ers_result_t
5240 qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
5241 {
5242         struct scsi_qla_host *ha = pci_get_drvdata(pdev);
5243
5244         if (!is_aer_supported(ha))
5245                 return PCI_ERS_RESULT_NONE;
5246
5247         return PCI_ERS_RESULT_RECOVERED;
5248 }
5249
5250 static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
5251 {
5252         uint32_t rval = QLA_ERROR;
5253         uint32_t ret = 0;
5254         int fn;
5255         struct pci_dev *other_pdev = NULL;
5256
5257         ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
5258
5259         set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
5260
5261         if (test_bit(AF_ONLINE, &ha->flags)) {
5262                 clear_bit(AF_ONLINE, &ha->flags);
5263                 clear_bit(AF_LINK_UP, &ha->flags);
5264                 iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
5265                 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
5266         }
5267
5268         fn = PCI_FUNC(ha->pdev->devfn);
5269         while (fn > 0) {
5270                 fn--;
5271                 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
5272                     "func %x\n", ha->host_no, __func__, fn);
5273                 /* Get the pci device given the domain, bus,
5274                  * slot/function number */
5275                 other_pdev =
5276                     pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
5277                     ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
5278                     fn));
5279
5280                 if (!other_pdev)
5281                         continue;
5282
5283                 if (atomic_read(&other_pdev->enable_cnt)) {
5284                         ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
5285                             "func in enabled state%x\n", ha->host_no,
5286                             __func__, fn);
5287                         pci_dev_put(other_pdev);
5288                         break;
5289                 }
5290                 pci_dev_put(other_pdev);
5291         }
5292
5293         /* The first function on the card, the reset owner will
5294          * start & initialize the firmware. The other functions
5295          * on the card will reset the firmware context
5296          */
5297         if (!fn) {
5298                 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
5299                     "0x%x is the owner\n", ha->host_no, __func__,
5300                     ha->pdev->devfn);
5301
5302                 qla4_8xxx_idc_lock(ha);
5303                 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
5304                     QLA82XX_DEV_COLD);
5305
5306                 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
5307                     QLA82XX_IDC_VERSION);
5308
5309                 qla4_8xxx_idc_unlock(ha);
5310                 clear_bit(AF_FW_RECOVERY, &ha->flags);
5311                 rval = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
5312                 qla4_8xxx_idc_lock(ha);
5313
5314                 if (rval != QLA_SUCCESS) {
5315                         ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
5316                             "FAILED\n", ha->host_no, __func__);
5317                         qla4_8xxx_clear_drv_active(ha);
5318                         qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
5319                             QLA82XX_DEV_FAILED);
5320                 } else {
5321                         ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
5322                             "READY\n", ha->host_no, __func__);
5323                         qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
5324                             QLA82XX_DEV_READY);
5325                         /* Clear driver state register */
5326                         qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
5327                         qla4_8xxx_set_drv_active(ha);
5328                         ret = qla4xxx_request_irqs(ha);
5329                         if (ret) {
5330                                 ql4_printk(KERN_WARNING, ha, "Failed to "
5331                                     "reserve interrupt %d already in use.\n",
5332                                     ha->pdev->irq);
5333                                 rval = QLA_ERROR;
5334                         } else {
5335                                 ha->isp_ops->enable_intrs(ha);
5336                                 rval = QLA_SUCCESS;
5337                         }
5338                 }
5339                 qla4_8xxx_idc_unlock(ha);
5340         } else {
5341                 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
5342                     "the reset owner\n", ha->host_no, __func__,
5343                     ha->pdev->devfn);
5344                 if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
5345                     QLA82XX_DEV_READY)) {
5346                         clear_bit(AF_FW_RECOVERY, &ha->flags);
5347                         rval = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
5348                         if (rval == QLA_SUCCESS) {
5349                                 ret = qla4xxx_request_irqs(ha);
5350                                 if (ret) {
5351                                         ql4_printk(KERN_WARNING, ha, "Failed to"
5352                                             " reserve interrupt %d already in"
5353                                             " use.\n", ha->pdev->irq);
5354                                         rval = QLA_ERROR;
5355                                 } else {
5356                                         ha->isp_ops->enable_intrs(ha);
5357                                         rval = QLA_SUCCESS;
5358                                 }
5359                         }
5360                         qla4_8xxx_idc_lock(ha);
5361                         qla4_8xxx_set_drv_active(ha);
5362                         qla4_8xxx_idc_unlock(ha);
5363                 }
5364         }
5365         clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
5366         return rval;
5367 }
5368
5369 static pci_ers_result_t
5370 qla4xxx_pci_slot_reset(struct pci_dev *pdev)
5371 {
5372         pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
5373         struct scsi_qla_host *ha = pci_get_drvdata(pdev);
5374         int rc;
5375
5376         ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
5377             ha->host_no, __func__);
5378
5379         if (!is_aer_supported(ha))
5380                 return PCI_ERS_RESULT_NONE;
5381
5382         /* Restore the saved state of PCIe device -
5383          * BAR registers, PCI Config space, PCIX, MSI,
5384          * IOV states
5385          */
5386         pci_restore_state(pdev);
5387
5388         /* pci_restore_state() clears the saved_state flag of the device
5389          * save restored state which resets saved_state flag
5390          */
5391         pci_save_state(pdev);
5392
5393         /* Initialize device or resume if in suspended state */
5394         rc = pci_enable_device(pdev);
5395         if (rc) {
5396                 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Can't re-enable "
5397                     "device after reset\n", ha->host_no, __func__);
5398                 goto exit_slot_reset;
5399         }
5400
5401         ha->isp_ops->disable_intrs(ha);
5402
5403         if (is_qla8022(ha)) {
5404                 if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
5405                         ret = PCI_ERS_RESULT_RECOVERED;
5406                         goto exit_slot_reset;
5407                 } else
5408                         goto exit_slot_reset;
5409         }
5410
5411 exit_slot_reset:
5412         ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
5413             "device after reset\n", ha->host_no, __func__, ret);
5414         return ret;
5415 }
5416
5417 static void
5418 qla4xxx_pci_resume(struct pci_dev *pdev)
5419 {
5420         struct scsi_qla_host *ha = pci_get_drvdata(pdev);
5421         int ret;
5422
5423         ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
5424             ha->host_no, __func__);
5425
5426         ret = qla4xxx_wait_for_hba_online(ha);
5427         if (ret != QLA_SUCCESS) {
5428                 ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
5429                     "resume I/O from slot/link_reset\n", ha->host_no,
5430                      __func__);
5431         }
5432
5433         pci_cleanup_aer_uncorrect_error_status(pdev);
5434         clear_bit(AF_EEH_BUSY, &ha->flags);
5435 }
5436
5437 static struct pci_error_handlers qla4xxx_err_handler = {
5438         .error_detected = qla4xxx_pci_error_detected,
5439         .mmio_enabled = qla4xxx_pci_mmio_enabled,
5440         .slot_reset = qla4xxx_pci_slot_reset,
5441         .resume = qla4xxx_pci_resume,
5442 };
5443
5444 static struct pci_device_id qla4xxx_pci_tbl[] = {
5445         {
5446                 .vendor         = PCI_VENDOR_ID_QLOGIC,
5447                 .device         = PCI_DEVICE_ID_QLOGIC_ISP4010,
5448                 .subvendor      = PCI_ANY_ID,
5449                 .subdevice      = PCI_ANY_ID,
5450         },
5451         {
5452                 .vendor         = PCI_VENDOR_ID_QLOGIC,
5453                 .device         = PCI_DEVICE_ID_QLOGIC_ISP4022,
5454                 .subvendor      = PCI_ANY_ID,
5455                 .subdevice      = PCI_ANY_ID,
5456         },
5457         {
5458                 .vendor         = PCI_VENDOR_ID_QLOGIC,
5459                 .device         = PCI_DEVICE_ID_QLOGIC_ISP4032,
5460                 .subvendor      = PCI_ANY_ID,
5461                 .subdevice      = PCI_ANY_ID,
5462         },
5463         {
5464                 .vendor         = PCI_VENDOR_ID_QLOGIC,
5465                 .device         = PCI_DEVICE_ID_QLOGIC_ISP8022,
5466                 .subvendor      = PCI_ANY_ID,
5467                 .subdevice      = PCI_ANY_ID,
5468         },
5469         {0, 0},
5470 };
5471 MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
5472
5473 static struct pci_driver qla4xxx_pci_driver = {
5474         .name           = DRIVER_NAME,
5475         .id_table       = qla4xxx_pci_tbl,
5476         .probe          = qla4xxx_probe_adapter,
5477         .remove         = qla4xxx_remove_adapter,
5478         .err_handler = &qla4xxx_err_handler,
5479 };
5480
5481 static int __init qla4xxx_module_init(void)
5482 {
5483         int ret;
5484
5485         /* Allocate cache for SRBs. */
5486         srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
5487                                        SLAB_HWCACHE_ALIGN, NULL);
5488         if (srb_cachep == NULL) {
5489                 printk(KERN_ERR
5490                        "%s: Unable to allocate SRB cache..."
5491                        "Failing load!\n", DRIVER_NAME);
5492                 ret = -ENOMEM;
5493                 goto no_srp_cache;
5494         }
5495
5496         /* Derive version string. */
5497         strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
5498         if (ql4xextended_error_logging)
5499                 strcat(qla4xxx_version_str, "-debug");
5500
5501         qla4xxx_scsi_transport =
5502                 iscsi_register_transport(&qla4xxx_iscsi_transport);
5503         if (!qla4xxx_scsi_transport){
5504                 ret = -ENODEV;
5505                 goto release_srb_cache;
5506         }
5507
5508         ret = pci_register_driver(&qla4xxx_pci_driver);
5509         if (ret)
5510                 goto unregister_transport;
5511
5512         printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
5513         return 0;
5514
5515 unregister_transport:
5516         iscsi_unregister_transport(&qla4xxx_iscsi_transport);
5517 release_srb_cache:
5518         kmem_cache_destroy(srb_cachep);
5519 no_srp_cache:
5520         return ret;
5521 }
5522
5523 static void __exit qla4xxx_module_exit(void)
5524 {
5525         pci_unregister_driver(&qla4xxx_pci_driver);
5526         iscsi_unregister_transport(&qla4xxx_iscsi_transport);
5527         kmem_cache_destroy(srb_cachep);
5528 }
5529
5530 module_init(qla4xxx_module_init);
5531 module_exit(qla4xxx_module_exit);
5532
5533 MODULE_AUTHOR("QLogic Corporation");
5534 MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
5535 MODULE_LICENSE("GPL");
5536 MODULE_VERSION(QLA4XXX_DRIVER_VERSION);