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