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