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