GNU Linux-libre 4.14.332-gnu1
[releases.git] / drivers / scsi / qla4xxx / ql4_os.c
1 /*
2  * QLogic iSCSI HBA Driver
3  * Copyright (c)  2003-2013 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 #include "ql4_83xx.h"
22
23 /*
24  * Driver version
25  */
26 static char qla4xxx_version_str[40];
27
28 /*
29  * SRB allocation cache
30  */
31 static struct kmem_cache *srb_cachep;
32
33 /*
34  * Module parameter information and variables
35  */
36 static int ql4xdisablesysfsboot = 1;
37 module_param(ql4xdisablesysfsboot, int, S_IRUGO | S_IWUSR);
38 MODULE_PARM_DESC(ql4xdisablesysfsboot,
39                  " Set to disable exporting boot targets to sysfs.\n"
40                  "\t\t  0 - Export boot targets\n"
41                  "\t\t  1 - Do not export boot targets (Default)");
42
43 int ql4xdontresethba;
44 module_param(ql4xdontresethba, int, S_IRUGO | S_IWUSR);
45 MODULE_PARM_DESC(ql4xdontresethba,
46                  " Don't reset the HBA for driver recovery.\n"
47                  "\t\t  0 - It will reset HBA (Default)\n"
48                  "\t\t  1 - It will NOT reset HBA");
49
50 int ql4xextended_error_logging;
51 module_param(ql4xextended_error_logging, int, S_IRUGO | S_IWUSR);
52 MODULE_PARM_DESC(ql4xextended_error_logging,
53                  " Option to enable extended error logging.\n"
54                  "\t\t  0 - no logging (Default)\n"
55                  "\t\t  2 - debug logging");
56
57 int ql4xenablemsix = 1;
58 module_param(ql4xenablemsix, int, S_IRUGO|S_IWUSR);
59 MODULE_PARM_DESC(ql4xenablemsix,
60                  " Set to enable MSI or MSI-X interrupt mechanism.\n"
61                  "\t\t  0 = enable INTx interrupt mechanism.\n"
62                  "\t\t  1 = enable MSI-X interrupt mechanism (Default).\n"
63                  "\t\t  2 = enable MSI interrupt mechanism.");
64
65 #define QL4_DEF_QDEPTH 32
66 static int ql4xmaxqdepth = QL4_DEF_QDEPTH;
67 module_param(ql4xmaxqdepth, int, S_IRUGO | S_IWUSR);
68 MODULE_PARM_DESC(ql4xmaxqdepth,
69                  " Maximum queue depth to report for target devices.\n"
70                  "\t\t  Default: 32.");
71
72 static int ql4xqfulltracking = 1;
73 module_param(ql4xqfulltracking, int, S_IRUGO | S_IWUSR);
74 MODULE_PARM_DESC(ql4xqfulltracking,
75                  " Enable or disable dynamic tracking and adjustment of\n"
76                  "\t\t scsi device queue depth.\n"
77                  "\t\t  0 - Disable.\n"
78                  "\t\t  1 - Enable. (Default)");
79
80 static int ql4xsess_recovery_tmo = QL4_SESS_RECOVERY_TMO;
81 module_param(ql4xsess_recovery_tmo, int, S_IRUGO);
82 MODULE_PARM_DESC(ql4xsess_recovery_tmo,
83                 " Target Session Recovery Timeout.\n"
84                 "\t\t  Default: 120 sec.");
85
86 int ql4xmdcapmask = 0;
87 module_param(ql4xmdcapmask, int, S_IRUGO);
88 MODULE_PARM_DESC(ql4xmdcapmask,
89                  " Set the Minidump driver capture mask level.\n"
90                  "\t\t  Default is 0 (firmware default capture mask)\n"
91                  "\t\t  Can be set to 0x3, 0x7, 0xF, 0x1F, 0x3F, 0x7F, 0xFF");
92
93 int ql4xenablemd = 1;
94 module_param(ql4xenablemd, int, S_IRUGO | S_IWUSR);
95 MODULE_PARM_DESC(ql4xenablemd,
96                  " Set to enable minidump.\n"
97                  "\t\t  0 - disable minidump\n"
98                  "\t\t  1 - enable minidump (Default)");
99
100 static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha);
101 /*
102  * SCSI host template entry points
103  */
104 static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
105
106 /*
107  * iSCSI template entry points
108  */
109 static int qla4xxx_session_get_param(struct iscsi_cls_session *cls_sess,
110                                      enum iscsi_param param, char *buf);
111 static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
112                                   enum iscsi_param param, char *buf);
113 static int qla4xxx_host_get_param(struct Scsi_Host *shost,
114                                   enum iscsi_host_param param, char *buf);
115 static int qla4xxx_iface_set_param(struct Scsi_Host *shost, void *data,
116                                    uint32_t len);
117 static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
118                                    enum iscsi_param_type param_type,
119                                    int param, char *buf);
120 static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
121 static struct iscsi_endpoint *qla4xxx_ep_connect(struct Scsi_Host *shost,
122                                                  struct sockaddr *dst_addr,
123                                                  int non_blocking);
124 static int qla4xxx_ep_poll(struct iscsi_endpoint *ep, int timeout_ms);
125 static void qla4xxx_ep_disconnect(struct iscsi_endpoint *ep);
126 static int qla4xxx_get_ep_param(struct iscsi_endpoint *ep,
127                                 enum iscsi_param param, char *buf);
128 static int qla4xxx_conn_start(struct iscsi_cls_conn *conn);
129 static struct iscsi_cls_conn *
130 qla4xxx_conn_create(struct iscsi_cls_session *cls_sess, uint32_t conn_idx);
131 static int qla4xxx_conn_bind(struct iscsi_cls_session *cls_session,
132                              struct iscsi_cls_conn *cls_conn,
133                              uint64_t transport_fd, int is_leading);
134 static void qla4xxx_conn_destroy(struct iscsi_cls_conn *conn);
135 static struct iscsi_cls_session *
136 qla4xxx_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max,
137                         uint16_t qdepth, uint32_t initial_cmdsn);
138 static void qla4xxx_session_destroy(struct iscsi_cls_session *sess);
139 static void qla4xxx_task_work(struct work_struct *wdata);
140 static int qla4xxx_alloc_pdu(struct iscsi_task *, uint8_t);
141 static int qla4xxx_task_xmit(struct iscsi_task *);
142 static void qla4xxx_task_cleanup(struct iscsi_task *);
143 static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session);
144 static void qla4xxx_conn_get_stats(struct iscsi_cls_conn *cls_conn,
145                                    struct iscsi_stats *stats);
146 static int qla4xxx_send_ping(struct Scsi_Host *shost, uint32_t iface_num,
147                              uint32_t iface_type, uint32_t payload_size,
148                              uint32_t pid, struct sockaddr *dst_addr);
149 static int qla4xxx_get_chap_list(struct Scsi_Host *shost, uint16_t chap_tbl_idx,
150                                  uint32_t *num_entries, char *buf);
151 static int qla4xxx_delete_chap(struct Scsi_Host *shost, uint16_t chap_tbl_idx);
152 static int qla4xxx_set_chap_entry(struct Scsi_Host *shost, void  *data,
153                                   int len);
154 static int qla4xxx_get_host_stats(struct Scsi_Host *shost, char *buf, int len);
155
156 /*
157  * SCSI host template entry points
158  */
159 static int qla4xxx_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmd);
160 static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
161 static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
162 static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
163 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
164 static int qla4xxx_slave_alloc(struct scsi_device *device);
165 static umode_t qla4_attr_is_visible(int param_type, int param);
166 static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type);
167
168 /*
169  * iSCSI Flash DDB sysfs entry points
170  */
171 static int
172 qla4xxx_sysfs_ddb_set_param(struct iscsi_bus_flash_session *fnode_sess,
173                             struct iscsi_bus_flash_conn *fnode_conn,
174                             void *data, int len);
175 static int
176 qla4xxx_sysfs_ddb_get_param(struct iscsi_bus_flash_session *fnode_sess,
177                             int param, char *buf);
178 static int qla4xxx_sysfs_ddb_add(struct Scsi_Host *shost, const char *buf,
179                                  int len);
180 static int
181 qla4xxx_sysfs_ddb_delete(struct iscsi_bus_flash_session *fnode_sess);
182 static int qla4xxx_sysfs_ddb_login(struct iscsi_bus_flash_session *fnode_sess,
183                                    struct iscsi_bus_flash_conn *fnode_conn);
184 static int qla4xxx_sysfs_ddb_logout(struct iscsi_bus_flash_session *fnode_sess,
185                                     struct iscsi_bus_flash_conn *fnode_conn);
186 static int qla4xxx_sysfs_ddb_logout_sid(struct iscsi_cls_session *cls_sess);
187
188 static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
189     QLA82XX_LEGACY_INTR_CONFIG;
190
191 static struct scsi_host_template qla4xxx_driver_template = {
192         .module                 = THIS_MODULE,
193         .name                   = DRIVER_NAME,
194         .proc_name              = DRIVER_NAME,
195         .queuecommand           = qla4xxx_queuecommand,
196
197         .eh_abort_handler       = qla4xxx_eh_abort,
198         .eh_device_reset_handler = qla4xxx_eh_device_reset,
199         .eh_target_reset_handler = qla4xxx_eh_target_reset,
200         .eh_host_reset_handler  = qla4xxx_eh_host_reset,
201         .eh_timed_out           = qla4xxx_eh_cmd_timed_out,
202
203         .slave_alloc            = qla4xxx_slave_alloc,
204         .change_queue_depth     = scsi_change_queue_depth,
205
206         .this_id                = -1,
207         .cmd_per_lun            = 3,
208         .use_clustering         = ENABLE_CLUSTERING,
209         .sg_tablesize           = SG_ALL,
210
211         .max_sectors            = 0xFFFF,
212         .shost_attrs            = qla4xxx_host_attrs,
213         .host_reset             = qla4xxx_host_reset,
214         .vendor_id              = SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_QLOGIC,
215 };
216
217 static struct iscsi_transport qla4xxx_iscsi_transport = {
218         .owner                  = THIS_MODULE,
219         .name                   = DRIVER_NAME,
220         .caps                   = CAP_TEXT_NEGO |
221                                   CAP_DATA_PATH_OFFLOAD | CAP_HDRDGST |
222                                   CAP_DATADGST | CAP_LOGIN_OFFLOAD |
223                                   CAP_MULTI_R2T,
224         .attr_is_visible        = qla4_attr_is_visible,
225         .create_session         = qla4xxx_session_create,
226         .destroy_session        = qla4xxx_session_destroy,
227         .start_conn             = qla4xxx_conn_start,
228         .create_conn            = qla4xxx_conn_create,
229         .bind_conn              = qla4xxx_conn_bind,
230         .stop_conn              = iscsi_conn_stop,
231         .destroy_conn           = qla4xxx_conn_destroy,
232         .set_param              = iscsi_set_param,
233         .get_conn_param         = qla4xxx_conn_get_param,
234         .get_session_param      = qla4xxx_session_get_param,
235         .get_ep_param           = qla4xxx_get_ep_param,
236         .ep_connect             = qla4xxx_ep_connect,
237         .ep_poll                = qla4xxx_ep_poll,
238         .ep_disconnect          = qla4xxx_ep_disconnect,
239         .get_stats              = qla4xxx_conn_get_stats,
240         .send_pdu               = iscsi_conn_send_pdu,
241         .xmit_task              = qla4xxx_task_xmit,
242         .cleanup_task           = qla4xxx_task_cleanup,
243         .alloc_pdu              = qla4xxx_alloc_pdu,
244
245         .get_host_param         = qla4xxx_host_get_param,
246         .set_iface_param        = qla4xxx_iface_set_param,
247         .get_iface_param        = qla4xxx_get_iface_param,
248         .bsg_request            = qla4xxx_bsg_request,
249         .send_ping              = qla4xxx_send_ping,
250         .get_chap               = qla4xxx_get_chap_list,
251         .delete_chap            = qla4xxx_delete_chap,
252         .set_chap               = qla4xxx_set_chap_entry,
253         .get_flashnode_param    = qla4xxx_sysfs_ddb_get_param,
254         .set_flashnode_param    = qla4xxx_sysfs_ddb_set_param,
255         .new_flashnode          = qla4xxx_sysfs_ddb_add,
256         .del_flashnode          = qla4xxx_sysfs_ddb_delete,
257         .login_flashnode        = qla4xxx_sysfs_ddb_login,
258         .logout_flashnode       = qla4xxx_sysfs_ddb_logout,
259         .logout_flashnode_sid   = qla4xxx_sysfs_ddb_logout_sid,
260         .get_host_stats         = qla4xxx_get_host_stats,
261 };
262
263 static struct scsi_transport_template *qla4xxx_scsi_transport;
264
265 static int qla4xxx_isp_check_reg(struct scsi_qla_host *ha)
266 {
267         u32 reg_val = 0;
268         int rval = QLA_SUCCESS;
269
270         if (is_qla8022(ha))
271                 reg_val = readl(&ha->qla4_82xx_reg->host_status);
272         else if (is_qla8032(ha) || is_qla8042(ha))
273                 reg_val = qla4_8xxx_rd_direct(ha, QLA8XXX_PEG_ALIVE_COUNTER);
274         else
275                 reg_val = readw(&ha->reg->ctrl_status);
276
277         if (reg_val == QL4_ISP_REG_DISCONNECT)
278                 rval = QLA_ERROR;
279
280         return rval;
281 }
282
283 static int qla4xxx_send_ping(struct Scsi_Host *shost, uint32_t iface_num,
284                              uint32_t iface_type, uint32_t payload_size,
285                              uint32_t pid, struct sockaddr *dst_addr)
286 {
287         struct scsi_qla_host *ha = to_qla_host(shost);
288         struct sockaddr_in *addr;
289         struct sockaddr_in6 *addr6;
290         uint32_t options = 0;
291         uint8_t ipaddr[IPv6_ADDR_LEN];
292         int rval;
293
294         memset(ipaddr, 0, IPv6_ADDR_LEN);
295         /* IPv4 to IPv4 */
296         if ((iface_type == ISCSI_IFACE_TYPE_IPV4) &&
297             (dst_addr->sa_family == AF_INET)) {
298                 addr = (struct sockaddr_in *)dst_addr;
299                 memcpy(ipaddr, &addr->sin_addr.s_addr, IP_ADDR_LEN);
300                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: IPv4 Ping src: %pI4 "
301                                   "dest: %pI4\n", __func__,
302                                   &ha->ip_config.ip_address, ipaddr));
303                 rval = qla4xxx_ping_iocb(ha, options, payload_size, pid,
304                                          ipaddr);
305                 if (rval)
306                         rval = -EINVAL;
307         } else if ((iface_type == ISCSI_IFACE_TYPE_IPV6) &&
308                    (dst_addr->sa_family == AF_INET6)) {
309                 /* IPv6 to IPv6 */
310                 addr6 = (struct sockaddr_in6 *)dst_addr;
311                 memcpy(ipaddr, &addr6->sin6_addr.in6_u.u6_addr8, IPv6_ADDR_LEN);
312
313                 options |= PING_IPV6_PROTOCOL_ENABLE;
314
315                 /* Ping using LinkLocal address */
316                 if ((iface_num == 0) || (iface_num == 1)) {
317                         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: LinkLocal Ping "
318                                           "src: %pI6 dest: %pI6\n", __func__,
319                                           &ha->ip_config.ipv6_link_local_addr,
320                                           ipaddr));
321                         options |= PING_IPV6_LINKLOCAL_ADDR;
322                         rval = qla4xxx_ping_iocb(ha, options, payload_size,
323                                                  pid, ipaddr);
324                 } else {
325                         ql4_printk(KERN_WARNING, ha, "%s: iface num = %d "
326                                    "not supported\n", __func__, iface_num);
327                         rval = -ENOSYS;
328                         goto exit_send_ping;
329                 }
330
331                 /*
332                  * If ping using LinkLocal address fails, try ping using
333                  * IPv6 address
334                  */
335                 if (rval != QLA_SUCCESS) {
336                         options &= ~PING_IPV6_LINKLOCAL_ADDR;
337                         if (iface_num == 0) {
338                                 options |= PING_IPV6_ADDR0;
339                                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: IPv6 "
340                                                   "Ping src: %pI6 "
341                                                   "dest: %pI6\n", __func__,
342                                                   &ha->ip_config.ipv6_addr0,
343                                                   ipaddr));
344                         } else if (iface_num == 1) {
345                                 options |= PING_IPV6_ADDR1;
346                                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: IPv6 "
347                                                   "Ping src: %pI6 "
348                                                   "dest: %pI6\n", __func__,
349                                                   &ha->ip_config.ipv6_addr1,
350                                                   ipaddr));
351                         }
352                         rval = qla4xxx_ping_iocb(ha, options, payload_size,
353                                                  pid, ipaddr);
354                         if (rval)
355                                 rval = -EINVAL;
356                 }
357         } else
358                 rval = -ENOSYS;
359 exit_send_ping:
360         return rval;
361 }
362
363 static umode_t qla4_attr_is_visible(int param_type, int param)
364 {
365         switch (param_type) {
366         case ISCSI_HOST_PARAM:
367                 switch (param) {
368                 case ISCSI_HOST_PARAM_HWADDRESS:
369                 case ISCSI_HOST_PARAM_IPADDRESS:
370                 case ISCSI_HOST_PARAM_INITIATOR_NAME:
371                 case ISCSI_HOST_PARAM_PORT_STATE:
372                 case ISCSI_HOST_PARAM_PORT_SPEED:
373                         return S_IRUGO;
374                 default:
375                         return 0;
376                 }
377         case ISCSI_PARAM:
378                 switch (param) {
379                 case ISCSI_PARAM_PERSISTENT_ADDRESS:
380                 case ISCSI_PARAM_PERSISTENT_PORT:
381                 case ISCSI_PARAM_CONN_ADDRESS:
382                 case ISCSI_PARAM_CONN_PORT:
383                 case ISCSI_PARAM_TARGET_NAME:
384                 case ISCSI_PARAM_TPGT:
385                 case ISCSI_PARAM_TARGET_ALIAS:
386                 case ISCSI_PARAM_MAX_BURST:
387                 case ISCSI_PARAM_MAX_R2T:
388                 case ISCSI_PARAM_FIRST_BURST:
389                 case ISCSI_PARAM_MAX_RECV_DLENGTH:
390                 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
391                 case ISCSI_PARAM_IFACE_NAME:
392                 case ISCSI_PARAM_CHAP_OUT_IDX:
393                 case ISCSI_PARAM_CHAP_IN_IDX:
394                 case ISCSI_PARAM_USERNAME:
395                 case ISCSI_PARAM_PASSWORD:
396                 case ISCSI_PARAM_USERNAME_IN:
397                 case ISCSI_PARAM_PASSWORD_IN:
398                 case ISCSI_PARAM_AUTO_SND_TGT_DISABLE:
399                 case ISCSI_PARAM_DISCOVERY_SESS:
400                 case ISCSI_PARAM_PORTAL_TYPE:
401                 case ISCSI_PARAM_CHAP_AUTH_EN:
402                 case ISCSI_PARAM_DISCOVERY_LOGOUT_EN:
403                 case ISCSI_PARAM_BIDI_CHAP_EN:
404                 case ISCSI_PARAM_DISCOVERY_AUTH_OPTIONAL:
405                 case ISCSI_PARAM_DEF_TIME2WAIT:
406                 case ISCSI_PARAM_DEF_TIME2RETAIN:
407                 case ISCSI_PARAM_HDRDGST_EN:
408                 case ISCSI_PARAM_DATADGST_EN:
409                 case ISCSI_PARAM_INITIAL_R2T_EN:
410                 case ISCSI_PARAM_IMM_DATA_EN:
411                 case ISCSI_PARAM_PDU_INORDER_EN:
412                 case ISCSI_PARAM_DATASEQ_INORDER_EN:
413                 case ISCSI_PARAM_MAX_SEGMENT_SIZE:
414                 case ISCSI_PARAM_TCP_TIMESTAMP_STAT:
415                 case ISCSI_PARAM_TCP_WSF_DISABLE:
416                 case ISCSI_PARAM_TCP_NAGLE_DISABLE:
417                 case ISCSI_PARAM_TCP_TIMER_SCALE:
418                 case ISCSI_PARAM_TCP_TIMESTAMP_EN:
419                 case ISCSI_PARAM_TCP_XMIT_WSF:
420                 case ISCSI_PARAM_TCP_RECV_WSF:
421                 case ISCSI_PARAM_IP_FRAGMENT_DISABLE:
422                 case ISCSI_PARAM_IPV4_TOS:
423                 case ISCSI_PARAM_IPV6_TC:
424                 case ISCSI_PARAM_IPV6_FLOW_LABEL:
425                 case ISCSI_PARAM_IS_FW_ASSIGNED_IPV6:
426                 case ISCSI_PARAM_KEEPALIVE_TMO:
427                 case ISCSI_PARAM_LOCAL_PORT:
428                 case ISCSI_PARAM_ISID:
429                 case ISCSI_PARAM_TSID:
430                 case ISCSI_PARAM_DEF_TASKMGMT_TMO:
431                 case ISCSI_PARAM_ERL:
432                 case ISCSI_PARAM_STATSN:
433                 case ISCSI_PARAM_EXP_STATSN:
434                 case ISCSI_PARAM_DISCOVERY_PARENT_IDX:
435                 case ISCSI_PARAM_DISCOVERY_PARENT_TYPE:
436                 case ISCSI_PARAM_LOCAL_IPADDR:
437                         return S_IRUGO;
438                 default:
439                         return 0;
440                 }
441         case ISCSI_NET_PARAM:
442                 switch (param) {
443                 case ISCSI_NET_PARAM_IPV4_ADDR:
444                 case ISCSI_NET_PARAM_IPV4_SUBNET:
445                 case ISCSI_NET_PARAM_IPV4_GW:
446                 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
447                 case ISCSI_NET_PARAM_IFACE_ENABLE:
448                 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
449                 case ISCSI_NET_PARAM_IPV6_ADDR:
450                 case ISCSI_NET_PARAM_IPV6_ROUTER:
451                 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
452                 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
453                 case ISCSI_NET_PARAM_VLAN_ID:
454                 case ISCSI_NET_PARAM_VLAN_PRIORITY:
455                 case ISCSI_NET_PARAM_VLAN_ENABLED:
456                 case ISCSI_NET_PARAM_MTU:
457                 case ISCSI_NET_PARAM_PORT:
458                 case ISCSI_NET_PARAM_IPADDR_STATE:
459                 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_STATE:
460                 case ISCSI_NET_PARAM_IPV6_ROUTER_STATE:
461                 case ISCSI_NET_PARAM_DELAYED_ACK_EN:
462                 case ISCSI_NET_PARAM_TCP_NAGLE_DISABLE:
463                 case ISCSI_NET_PARAM_TCP_WSF_DISABLE:
464                 case ISCSI_NET_PARAM_TCP_WSF:
465                 case ISCSI_NET_PARAM_TCP_TIMER_SCALE:
466                 case ISCSI_NET_PARAM_TCP_TIMESTAMP_EN:
467                 case ISCSI_NET_PARAM_CACHE_ID:
468                 case ISCSI_NET_PARAM_IPV4_DHCP_DNS_ADDR_EN:
469                 case ISCSI_NET_PARAM_IPV4_DHCP_SLP_DA_EN:
470                 case ISCSI_NET_PARAM_IPV4_TOS_EN:
471                 case ISCSI_NET_PARAM_IPV4_TOS:
472                 case ISCSI_NET_PARAM_IPV4_GRAT_ARP_EN:
473                 case ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID_EN:
474                 case ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID:
475                 case ISCSI_NET_PARAM_IPV4_DHCP_REQ_VENDOR_ID_EN:
476                 case ISCSI_NET_PARAM_IPV4_DHCP_USE_VENDOR_ID_EN:
477                 case ISCSI_NET_PARAM_IPV4_DHCP_VENDOR_ID:
478                 case ISCSI_NET_PARAM_IPV4_DHCP_LEARN_IQN_EN:
479                 case ISCSI_NET_PARAM_IPV4_FRAGMENT_DISABLE:
480                 case ISCSI_NET_PARAM_IPV4_IN_FORWARD_EN:
481                 case ISCSI_NET_PARAM_REDIRECT_EN:
482                 case ISCSI_NET_PARAM_IPV4_TTL:
483                 case ISCSI_NET_PARAM_IPV6_GRAT_NEIGHBOR_ADV_EN:
484                 case ISCSI_NET_PARAM_IPV6_MLD_EN:
485                 case ISCSI_NET_PARAM_IPV6_FLOW_LABEL:
486                 case ISCSI_NET_PARAM_IPV6_TRAFFIC_CLASS:
487                 case ISCSI_NET_PARAM_IPV6_HOP_LIMIT:
488                 case ISCSI_NET_PARAM_IPV6_ND_REACHABLE_TMO:
489                 case ISCSI_NET_PARAM_IPV6_ND_REXMIT_TIME:
490                 case ISCSI_NET_PARAM_IPV6_ND_STALE_TMO:
491                 case ISCSI_NET_PARAM_IPV6_DUP_ADDR_DETECT_CNT:
492                 case ISCSI_NET_PARAM_IPV6_RTR_ADV_LINK_MTU:
493                         return S_IRUGO;
494                 default:
495                         return 0;
496                 }
497         case ISCSI_IFACE_PARAM:
498                 switch (param) {
499                 case ISCSI_IFACE_PARAM_DEF_TASKMGMT_TMO:
500                 case ISCSI_IFACE_PARAM_HDRDGST_EN:
501                 case ISCSI_IFACE_PARAM_DATADGST_EN:
502                 case ISCSI_IFACE_PARAM_IMM_DATA_EN:
503                 case ISCSI_IFACE_PARAM_INITIAL_R2T_EN:
504                 case ISCSI_IFACE_PARAM_DATASEQ_INORDER_EN:
505                 case ISCSI_IFACE_PARAM_PDU_INORDER_EN:
506                 case ISCSI_IFACE_PARAM_ERL:
507                 case ISCSI_IFACE_PARAM_MAX_RECV_DLENGTH:
508                 case ISCSI_IFACE_PARAM_FIRST_BURST:
509                 case ISCSI_IFACE_PARAM_MAX_R2T:
510                 case ISCSI_IFACE_PARAM_MAX_BURST:
511                 case ISCSI_IFACE_PARAM_CHAP_AUTH_EN:
512                 case ISCSI_IFACE_PARAM_BIDI_CHAP_EN:
513                 case ISCSI_IFACE_PARAM_DISCOVERY_AUTH_OPTIONAL:
514                 case ISCSI_IFACE_PARAM_DISCOVERY_LOGOUT_EN:
515                 case ISCSI_IFACE_PARAM_STRICT_LOGIN_COMP_EN:
516                 case ISCSI_IFACE_PARAM_INITIATOR_NAME:
517                         return S_IRUGO;
518                 default:
519                         return 0;
520                 }
521         case ISCSI_FLASHNODE_PARAM:
522                 switch (param) {
523                 case ISCSI_FLASHNODE_IS_FW_ASSIGNED_IPV6:
524                 case ISCSI_FLASHNODE_PORTAL_TYPE:
525                 case ISCSI_FLASHNODE_AUTO_SND_TGT_DISABLE:
526                 case ISCSI_FLASHNODE_DISCOVERY_SESS:
527                 case ISCSI_FLASHNODE_ENTRY_EN:
528                 case ISCSI_FLASHNODE_HDR_DGST_EN:
529                 case ISCSI_FLASHNODE_DATA_DGST_EN:
530                 case ISCSI_FLASHNODE_IMM_DATA_EN:
531                 case ISCSI_FLASHNODE_INITIAL_R2T_EN:
532                 case ISCSI_FLASHNODE_DATASEQ_INORDER:
533                 case ISCSI_FLASHNODE_PDU_INORDER:
534                 case ISCSI_FLASHNODE_CHAP_AUTH_EN:
535                 case ISCSI_FLASHNODE_SNACK_REQ_EN:
536                 case ISCSI_FLASHNODE_DISCOVERY_LOGOUT_EN:
537                 case ISCSI_FLASHNODE_BIDI_CHAP_EN:
538                 case ISCSI_FLASHNODE_DISCOVERY_AUTH_OPTIONAL:
539                 case ISCSI_FLASHNODE_ERL:
540                 case ISCSI_FLASHNODE_TCP_TIMESTAMP_STAT:
541                 case ISCSI_FLASHNODE_TCP_NAGLE_DISABLE:
542                 case ISCSI_FLASHNODE_TCP_WSF_DISABLE:
543                 case ISCSI_FLASHNODE_TCP_TIMER_SCALE:
544                 case ISCSI_FLASHNODE_TCP_TIMESTAMP_EN:
545                 case ISCSI_FLASHNODE_IP_FRAG_DISABLE:
546                 case ISCSI_FLASHNODE_MAX_RECV_DLENGTH:
547                 case ISCSI_FLASHNODE_MAX_XMIT_DLENGTH:
548                 case ISCSI_FLASHNODE_FIRST_BURST:
549                 case ISCSI_FLASHNODE_DEF_TIME2WAIT:
550                 case ISCSI_FLASHNODE_DEF_TIME2RETAIN:
551                 case ISCSI_FLASHNODE_MAX_R2T:
552                 case ISCSI_FLASHNODE_KEEPALIVE_TMO:
553                 case ISCSI_FLASHNODE_ISID:
554                 case ISCSI_FLASHNODE_TSID:
555                 case ISCSI_FLASHNODE_PORT:
556                 case ISCSI_FLASHNODE_MAX_BURST:
557                 case ISCSI_FLASHNODE_DEF_TASKMGMT_TMO:
558                 case ISCSI_FLASHNODE_IPADDR:
559                 case ISCSI_FLASHNODE_ALIAS:
560                 case ISCSI_FLASHNODE_REDIRECT_IPADDR:
561                 case ISCSI_FLASHNODE_MAX_SEGMENT_SIZE:
562                 case ISCSI_FLASHNODE_LOCAL_PORT:
563                 case ISCSI_FLASHNODE_IPV4_TOS:
564                 case ISCSI_FLASHNODE_IPV6_TC:
565                 case ISCSI_FLASHNODE_IPV6_FLOW_LABEL:
566                 case ISCSI_FLASHNODE_NAME:
567                 case ISCSI_FLASHNODE_TPGT:
568                 case ISCSI_FLASHNODE_LINK_LOCAL_IPV6:
569                 case ISCSI_FLASHNODE_DISCOVERY_PARENT_IDX:
570                 case ISCSI_FLASHNODE_DISCOVERY_PARENT_TYPE:
571                 case ISCSI_FLASHNODE_TCP_XMIT_WSF:
572                 case ISCSI_FLASHNODE_TCP_RECV_WSF:
573                 case ISCSI_FLASHNODE_CHAP_OUT_IDX:
574                 case ISCSI_FLASHNODE_USERNAME:
575                 case ISCSI_FLASHNODE_PASSWORD:
576                 case ISCSI_FLASHNODE_STATSN:
577                 case ISCSI_FLASHNODE_EXP_STATSN:
578                 case ISCSI_FLASHNODE_IS_BOOT_TGT:
579                         return S_IRUGO;
580                 default:
581                         return 0;
582                 }
583         }
584
585         return 0;
586 }
587
588 /**
589  * qla4xxx_create chap_list - Create CHAP list from FLASH
590  * @ha: pointer to adapter structure
591  *
592  * Read flash and make a list of CHAP entries, during login when a CHAP entry
593  * is received, it will be checked in this list. If entry exist then the CHAP
594  * entry index is set in the DDB. If CHAP entry does not exist in this list
595  * then a new entry is added in FLASH in CHAP table and the index obtained is
596  * used in the DDB.
597  **/
598 static void qla4xxx_create_chap_list(struct scsi_qla_host *ha)
599 {
600         int rval = 0;
601         uint8_t *chap_flash_data = NULL;
602         uint32_t offset;
603         dma_addr_t chap_dma;
604         uint32_t chap_size = 0;
605
606         if (is_qla40XX(ha))
607                 chap_size = MAX_CHAP_ENTRIES_40XX *
608                             sizeof(struct ql4_chap_table);
609         else    /* Single region contains CHAP info for both
610                  * ports which is divided into half for each port.
611                  */
612                 chap_size = ha->hw.flt_chap_size / 2;
613
614         chap_flash_data = dma_alloc_coherent(&ha->pdev->dev, chap_size,
615                                              &chap_dma, GFP_KERNEL);
616         if (!chap_flash_data) {
617                 ql4_printk(KERN_ERR, ha, "No memory for chap_flash_data\n");
618                 return;
619         }
620
621         if (is_qla40XX(ha)) {
622                 offset = FLASH_CHAP_OFFSET;
623         } else {
624                 offset = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_region_chap << 2);
625                 if (ha->port_num == 1)
626                         offset += chap_size;
627         }
628
629         rval = qla4xxx_get_flash(ha, chap_dma, offset, chap_size);
630         if (rval != QLA_SUCCESS)
631                 goto exit_chap_list;
632
633         if (ha->chap_list == NULL)
634                 ha->chap_list = vmalloc(chap_size);
635         if (ha->chap_list == NULL) {
636                 ql4_printk(KERN_ERR, ha, "No memory for ha->chap_list\n");
637                 goto exit_chap_list;
638         }
639
640         memset(ha->chap_list, 0, chap_size);
641         memcpy(ha->chap_list, chap_flash_data, chap_size);
642
643 exit_chap_list:
644         dma_free_coherent(&ha->pdev->dev, chap_size, chap_flash_data, chap_dma);
645 }
646
647 static int qla4xxx_get_chap_by_index(struct scsi_qla_host *ha,
648                                      int16_t chap_index,
649                                      struct ql4_chap_table **chap_entry)
650 {
651         int rval = QLA_ERROR;
652         int max_chap_entries;
653
654         if (!ha->chap_list) {
655                 ql4_printk(KERN_ERR, ha, "CHAP table cache is empty!\n");
656                 rval = QLA_ERROR;
657                 goto exit_get_chap;
658         }
659
660         if (is_qla80XX(ha))
661                 max_chap_entries = (ha->hw.flt_chap_size / 2) /
662                                    sizeof(struct ql4_chap_table);
663         else
664                 max_chap_entries = MAX_CHAP_ENTRIES_40XX;
665
666         if (chap_index > max_chap_entries) {
667                 ql4_printk(KERN_ERR, ha, "Invalid Chap index\n");
668                 rval = QLA_ERROR;
669                 goto exit_get_chap;
670         }
671
672         *chap_entry = (struct ql4_chap_table *)ha->chap_list + chap_index;
673         if ((*chap_entry)->cookie !=
674              __constant_cpu_to_le16(CHAP_VALID_COOKIE)) {
675                 rval = QLA_ERROR;
676                 *chap_entry = NULL;
677         } else {
678                 rval = QLA_SUCCESS;
679         }
680
681 exit_get_chap:
682         return rval;
683 }
684
685 /**
686  * qla4xxx_find_free_chap_index - Find the first free chap index
687  * @ha: pointer to adapter structure
688  * @chap_index: CHAP index to be returned
689  *
690  * Find the first free chap index available in the chap table
691  *
692  * Note: Caller should acquire the chap lock before getting here.
693  **/
694 static int qla4xxx_find_free_chap_index(struct scsi_qla_host *ha,
695                                         uint16_t *chap_index)
696 {
697         int i, rval;
698         int free_index = -1;
699         int max_chap_entries = 0;
700         struct ql4_chap_table *chap_table;
701
702         if (is_qla80XX(ha))
703                 max_chap_entries = (ha->hw.flt_chap_size / 2) /
704                                                 sizeof(struct ql4_chap_table);
705         else
706                 max_chap_entries = MAX_CHAP_ENTRIES_40XX;
707
708         if (!ha->chap_list) {
709                 ql4_printk(KERN_ERR, ha, "CHAP table cache is empty!\n");
710                 rval = QLA_ERROR;
711                 goto exit_find_chap;
712         }
713
714         for (i = 0; i < max_chap_entries; i++) {
715                 chap_table = (struct ql4_chap_table *)ha->chap_list + i;
716
717                 if ((chap_table->cookie !=
718                     __constant_cpu_to_le16(CHAP_VALID_COOKIE)) &&
719                    (i > MAX_RESRV_CHAP_IDX)) {
720                                 free_index = i;
721                                 break;
722                 }
723         }
724
725         if (free_index != -1) {
726                 *chap_index = free_index;
727                 rval = QLA_SUCCESS;
728         } else {
729                 rval = QLA_ERROR;
730         }
731
732 exit_find_chap:
733         return rval;
734 }
735
736 static int qla4xxx_get_chap_list(struct Scsi_Host *shost, uint16_t chap_tbl_idx,
737                                   uint32_t *num_entries, char *buf)
738 {
739         struct scsi_qla_host *ha = to_qla_host(shost);
740         struct ql4_chap_table *chap_table;
741         struct iscsi_chap_rec *chap_rec;
742         int max_chap_entries = 0;
743         int valid_chap_entries = 0;
744         int ret = 0, i;
745
746         if (is_qla80XX(ha))
747                 max_chap_entries = (ha->hw.flt_chap_size / 2) /
748                                         sizeof(struct ql4_chap_table);
749         else
750                 max_chap_entries = MAX_CHAP_ENTRIES_40XX;
751
752         ql4_printk(KERN_INFO, ha, "%s: num_entries = %d, CHAP idx = %d\n",
753                         __func__, *num_entries, chap_tbl_idx);
754
755         if (!buf) {
756                 ret = -ENOMEM;
757                 goto exit_get_chap_list;
758         }
759
760         qla4xxx_create_chap_list(ha);
761
762         chap_rec = (struct iscsi_chap_rec *) buf;
763         mutex_lock(&ha->chap_sem);
764         for (i = chap_tbl_idx; i < max_chap_entries; i++) {
765                 chap_table = (struct ql4_chap_table *)ha->chap_list + i;
766                 if (chap_table->cookie !=
767                     __constant_cpu_to_le16(CHAP_VALID_COOKIE))
768                         continue;
769
770                 chap_rec->chap_tbl_idx = i;
771                 strlcpy(chap_rec->username, chap_table->name,
772                         ISCSI_CHAP_AUTH_NAME_MAX_LEN);
773                 strlcpy(chap_rec->password, chap_table->secret,
774                         QL4_CHAP_MAX_SECRET_LEN);
775                 chap_rec->password_length = chap_table->secret_len;
776
777                 if (chap_table->flags & BIT_7) /* local */
778                         chap_rec->chap_type = CHAP_TYPE_OUT;
779
780                 if (chap_table->flags & BIT_6) /* peer */
781                         chap_rec->chap_type = CHAP_TYPE_IN;
782
783                 chap_rec++;
784
785                 valid_chap_entries++;
786                 if (valid_chap_entries == *num_entries)
787                         break;
788                 else
789                         continue;
790         }
791         mutex_unlock(&ha->chap_sem);
792
793 exit_get_chap_list:
794         ql4_printk(KERN_INFO, ha, "%s: Valid CHAP Entries = %d\n",
795                         __func__,  valid_chap_entries);
796         *num_entries = valid_chap_entries;
797         return ret;
798 }
799
800 static int __qla4xxx_is_chap_active(struct device *dev, void *data)
801 {
802         int ret = 0;
803         uint16_t *chap_tbl_idx = (uint16_t *) data;
804         struct iscsi_cls_session *cls_session;
805         struct iscsi_session *sess;
806         struct ddb_entry *ddb_entry;
807
808         if (!iscsi_is_session_dev(dev))
809                 goto exit_is_chap_active;
810
811         cls_session = iscsi_dev_to_session(dev);
812         sess = cls_session->dd_data;
813         ddb_entry = sess->dd_data;
814
815         if (iscsi_session_chkready(cls_session))
816                 goto exit_is_chap_active;
817
818         if (ddb_entry->chap_tbl_idx == *chap_tbl_idx)
819                 ret = 1;
820
821 exit_is_chap_active:
822         return ret;
823 }
824
825 static int qla4xxx_is_chap_active(struct Scsi_Host *shost,
826                                   uint16_t chap_tbl_idx)
827 {
828         int ret = 0;
829
830         ret = device_for_each_child(&shost->shost_gendev, &chap_tbl_idx,
831                                     __qla4xxx_is_chap_active);
832
833         return ret;
834 }
835
836 static int qla4xxx_delete_chap(struct Scsi_Host *shost, uint16_t chap_tbl_idx)
837 {
838         struct scsi_qla_host *ha = to_qla_host(shost);
839         struct ql4_chap_table *chap_table;
840         dma_addr_t chap_dma;
841         int max_chap_entries = 0;
842         uint32_t offset = 0;
843         uint32_t chap_size;
844         int ret = 0;
845
846         chap_table = dma_pool_alloc(ha->chap_dma_pool, GFP_KERNEL, &chap_dma);
847         if (chap_table == NULL)
848                 return -ENOMEM;
849
850         memset(chap_table, 0, sizeof(struct ql4_chap_table));
851
852         if (is_qla80XX(ha))
853                 max_chap_entries = (ha->hw.flt_chap_size / 2) /
854                                    sizeof(struct ql4_chap_table);
855         else
856                 max_chap_entries = MAX_CHAP_ENTRIES_40XX;
857
858         if (chap_tbl_idx > max_chap_entries) {
859                 ret = -EINVAL;
860                 goto exit_delete_chap;
861         }
862
863         /* Check if chap index is in use.
864          * If chap is in use don't delet chap entry */
865         ret = qla4xxx_is_chap_active(shost, chap_tbl_idx);
866         if (ret) {
867                 ql4_printk(KERN_INFO, ha, "CHAP entry %d is in use, cannot "
868                            "delete from flash\n", chap_tbl_idx);
869                 ret = -EBUSY;
870                 goto exit_delete_chap;
871         }
872
873         chap_size = sizeof(struct ql4_chap_table);
874         if (is_qla40XX(ha))
875                 offset = FLASH_CHAP_OFFSET | (chap_tbl_idx * chap_size);
876         else {
877                 offset = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_region_chap << 2);
878                 /* flt_chap_size is CHAP table size for both ports
879                  * so divide it by 2 to calculate the offset for second port
880                  */
881                 if (ha->port_num == 1)
882                         offset += (ha->hw.flt_chap_size / 2);
883                 offset += (chap_tbl_idx * chap_size);
884         }
885
886         ret = qla4xxx_get_flash(ha, chap_dma, offset, chap_size);
887         if (ret != QLA_SUCCESS) {
888                 ret = -EINVAL;
889                 goto exit_delete_chap;
890         }
891
892         DEBUG2(ql4_printk(KERN_INFO, ha, "Chap Cookie: x%x\n",
893                           __le16_to_cpu(chap_table->cookie)));
894
895         if (__le16_to_cpu(chap_table->cookie) != CHAP_VALID_COOKIE) {
896                 ql4_printk(KERN_ERR, ha, "No valid chap entry found\n");
897                 goto exit_delete_chap;
898         }
899
900         chap_table->cookie = __constant_cpu_to_le16(0xFFFF);
901
902         offset = FLASH_CHAP_OFFSET |
903                         (chap_tbl_idx * sizeof(struct ql4_chap_table));
904         ret = qla4xxx_set_flash(ha, chap_dma, offset, chap_size,
905                                 FLASH_OPT_RMW_COMMIT);
906         if (ret == QLA_SUCCESS && ha->chap_list) {
907                 mutex_lock(&ha->chap_sem);
908                 /* Update ha chap_list cache */
909                 memcpy((struct ql4_chap_table *)ha->chap_list + chap_tbl_idx,
910                         chap_table, sizeof(struct ql4_chap_table));
911                 mutex_unlock(&ha->chap_sem);
912         }
913         if (ret != QLA_SUCCESS)
914                 ret =  -EINVAL;
915
916 exit_delete_chap:
917         dma_pool_free(ha->chap_dma_pool, chap_table, chap_dma);
918         return ret;
919 }
920
921 /**
922  * qla4xxx_set_chap_entry - Make chap entry with given information
923  * @shost: pointer to host
924  * @data: chap info - credentials, index and type to make chap entry
925  * @len: length of data
926  *
927  * Add or update chap entry with the given information
928  **/
929 static int qla4xxx_set_chap_entry(struct Scsi_Host *shost, void *data, int len)
930 {
931         struct scsi_qla_host *ha = to_qla_host(shost);
932         struct iscsi_chap_rec chap_rec;
933         struct ql4_chap_table *chap_entry = NULL;
934         struct iscsi_param_info *param_info;
935         struct nlattr *attr;
936         int max_chap_entries = 0;
937         int type;
938         int rem = len;
939         int rc = 0;
940         int size;
941
942         memset(&chap_rec, 0, sizeof(chap_rec));
943
944         nla_for_each_attr(attr, data, len, rem) {
945                 if (nla_len(attr) < sizeof(*param_info)) {
946                         rc = -EINVAL;
947                         goto exit_set_chap;
948                 }
949
950                 param_info = nla_data(attr);
951
952                 switch (param_info->param) {
953                 case ISCSI_CHAP_PARAM_INDEX:
954                         chap_rec.chap_tbl_idx = *(uint16_t *)param_info->value;
955                         break;
956                 case ISCSI_CHAP_PARAM_CHAP_TYPE:
957                         chap_rec.chap_type = param_info->value[0];
958                         break;
959                 case ISCSI_CHAP_PARAM_USERNAME:
960                         size = min_t(size_t, sizeof(chap_rec.username),
961                                      param_info->len);
962                         memcpy(chap_rec.username, param_info->value, size);
963                         break;
964                 case ISCSI_CHAP_PARAM_PASSWORD:
965                         size = min_t(size_t, sizeof(chap_rec.password),
966                                      param_info->len);
967                         memcpy(chap_rec.password, param_info->value, size);
968                         break;
969                 case ISCSI_CHAP_PARAM_PASSWORD_LEN:
970                         chap_rec.password_length = param_info->value[0];
971                         break;
972                 default:
973                         ql4_printk(KERN_ERR, ha,
974                                    "%s: No such sysfs attribute\n", __func__);
975                         rc = -ENOSYS;
976                         goto exit_set_chap;
977                 };
978         }
979
980         if (chap_rec.chap_type == CHAP_TYPE_IN)
981                 type = BIDI_CHAP;
982         else
983                 type = LOCAL_CHAP;
984
985         if (is_qla80XX(ha))
986                 max_chap_entries = (ha->hw.flt_chap_size / 2) /
987                                    sizeof(struct ql4_chap_table);
988         else
989                 max_chap_entries = MAX_CHAP_ENTRIES_40XX;
990
991         mutex_lock(&ha->chap_sem);
992         if (chap_rec.chap_tbl_idx < max_chap_entries) {
993                 rc = qla4xxx_get_chap_by_index(ha, chap_rec.chap_tbl_idx,
994                                                &chap_entry);
995                 if (!rc) {
996                         if (!(type == qla4xxx_get_chap_type(chap_entry))) {
997                                 ql4_printk(KERN_INFO, ha,
998                                            "Type mismatch for CHAP entry %d\n",
999                                            chap_rec.chap_tbl_idx);
1000                                 rc = -EINVAL;
1001                                 goto exit_unlock_chap;
1002                         }
1003
1004                         /* If chap index is in use then don't modify it */
1005                         rc = qla4xxx_is_chap_active(shost,
1006                                                     chap_rec.chap_tbl_idx);
1007                         if (rc) {
1008                                 ql4_printk(KERN_INFO, ha,
1009                                            "CHAP entry %d is in use\n",
1010                                            chap_rec.chap_tbl_idx);
1011                                 rc = -EBUSY;
1012                                 goto exit_unlock_chap;
1013                         }
1014                 }
1015         } else {
1016                 rc = qla4xxx_find_free_chap_index(ha, &chap_rec.chap_tbl_idx);
1017                 if (rc) {
1018                         ql4_printk(KERN_INFO, ha, "CHAP entry not available\n");
1019                         rc = -EBUSY;
1020                         goto exit_unlock_chap;
1021                 }
1022         }
1023
1024         rc = qla4xxx_set_chap(ha, chap_rec.username, chap_rec.password,
1025                               chap_rec.chap_tbl_idx, type);
1026
1027 exit_unlock_chap:
1028         mutex_unlock(&ha->chap_sem);
1029
1030 exit_set_chap:
1031         return rc;
1032 }
1033
1034
1035 static int qla4xxx_get_host_stats(struct Scsi_Host *shost, char *buf, int len)
1036 {
1037         struct scsi_qla_host *ha = to_qla_host(shost);
1038         struct iscsi_offload_host_stats *host_stats = NULL;
1039         int host_stats_size;
1040         int ret = 0;
1041         int ddb_idx = 0;
1042         struct ql_iscsi_stats *ql_iscsi_stats = NULL;
1043         int stats_size;
1044         dma_addr_t iscsi_stats_dma;
1045
1046         DEBUG2(ql4_printk(KERN_INFO, ha, "Func: %s\n", __func__));
1047
1048         host_stats_size = sizeof(struct iscsi_offload_host_stats);
1049
1050         if (host_stats_size != len) {
1051                 ql4_printk(KERN_INFO, ha, "%s: host_stats size mismatch expected = %d, is = %d\n",
1052                            __func__, len, host_stats_size);
1053                 ret = -EINVAL;
1054                 goto exit_host_stats;
1055         }
1056         host_stats = (struct iscsi_offload_host_stats *)buf;
1057
1058         if (!buf) {
1059                 ret = -ENOMEM;
1060                 goto exit_host_stats;
1061         }
1062
1063         stats_size = PAGE_ALIGN(sizeof(struct ql_iscsi_stats));
1064
1065         ql_iscsi_stats = dma_alloc_coherent(&ha->pdev->dev, stats_size,
1066                                             &iscsi_stats_dma, GFP_KERNEL);
1067         if (!ql_iscsi_stats) {
1068                 ql4_printk(KERN_ERR, ha,
1069                            "Unable to allocate memory for iscsi stats\n");
1070                 ret = -ENOMEM;
1071                 goto exit_host_stats;
1072         }
1073
1074         ret =  qla4xxx_get_mgmt_data(ha, ddb_idx, stats_size,
1075                                      iscsi_stats_dma);
1076         if (ret != QLA_SUCCESS) {
1077                 ql4_printk(KERN_ERR, ha,
1078                            "Unable to retrieve iscsi stats\n");
1079                 ret = -EIO;
1080                 goto exit_host_stats;
1081         }
1082         host_stats->mactx_frames = le64_to_cpu(ql_iscsi_stats->mac_tx_frames);
1083         host_stats->mactx_bytes = le64_to_cpu(ql_iscsi_stats->mac_tx_bytes);
1084         host_stats->mactx_multicast_frames =
1085                         le64_to_cpu(ql_iscsi_stats->mac_tx_multicast_frames);
1086         host_stats->mactx_broadcast_frames =
1087                         le64_to_cpu(ql_iscsi_stats->mac_tx_broadcast_frames);
1088         host_stats->mactx_pause_frames =
1089                         le64_to_cpu(ql_iscsi_stats->mac_tx_pause_frames);
1090         host_stats->mactx_control_frames =
1091                         le64_to_cpu(ql_iscsi_stats->mac_tx_control_frames);
1092         host_stats->mactx_deferral =
1093                         le64_to_cpu(ql_iscsi_stats->mac_tx_deferral);
1094         host_stats->mactx_excess_deferral =
1095                         le64_to_cpu(ql_iscsi_stats->mac_tx_excess_deferral);
1096         host_stats->mactx_late_collision =
1097                         le64_to_cpu(ql_iscsi_stats->mac_tx_late_collision);
1098         host_stats->mactx_abort = le64_to_cpu(ql_iscsi_stats->mac_tx_abort);
1099         host_stats->mactx_single_collision =
1100                         le64_to_cpu(ql_iscsi_stats->mac_tx_single_collision);
1101         host_stats->mactx_multiple_collision =
1102                         le64_to_cpu(ql_iscsi_stats->mac_tx_multiple_collision);
1103         host_stats->mactx_collision =
1104                         le64_to_cpu(ql_iscsi_stats->mac_tx_collision);
1105         host_stats->mactx_frames_dropped =
1106                         le64_to_cpu(ql_iscsi_stats->mac_tx_frames_dropped);
1107         host_stats->mactx_jumbo_frames =
1108                         le64_to_cpu(ql_iscsi_stats->mac_tx_jumbo_frames);
1109         host_stats->macrx_frames = le64_to_cpu(ql_iscsi_stats->mac_rx_frames);
1110         host_stats->macrx_bytes = le64_to_cpu(ql_iscsi_stats->mac_rx_bytes);
1111         host_stats->macrx_unknown_control_frames =
1112                 le64_to_cpu(ql_iscsi_stats->mac_rx_unknown_control_frames);
1113         host_stats->macrx_pause_frames =
1114                         le64_to_cpu(ql_iscsi_stats->mac_rx_pause_frames);
1115         host_stats->macrx_control_frames =
1116                         le64_to_cpu(ql_iscsi_stats->mac_rx_control_frames);
1117         host_stats->macrx_dribble =
1118                         le64_to_cpu(ql_iscsi_stats->mac_rx_dribble);
1119         host_stats->macrx_frame_length_error =
1120                         le64_to_cpu(ql_iscsi_stats->mac_rx_frame_length_error);
1121         host_stats->macrx_jabber = le64_to_cpu(ql_iscsi_stats->mac_rx_jabber);
1122         host_stats->macrx_carrier_sense_error =
1123                 le64_to_cpu(ql_iscsi_stats->mac_rx_carrier_sense_error);
1124         host_stats->macrx_frame_discarded =
1125                         le64_to_cpu(ql_iscsi_stats->mac_rx_frame_discarded);
1126         host_stats->macrx_frames_dropped =
1127                         le64_to_cpu(ql_iscsi_stats->mac_rx_frames_dropped);
1128         host_stats->mac_crc_error = le64_to_cpu(ql_iscsi_stats->mac_crc_error);
1129         host_stats->mac_encoding_error =
1130                         le64_to_cpu(ql_iscsi_stats->mac_encoding_error);
1131         host_stats->macrx_length_error_large =
1132                         le64_to_cpu(ql_iscsi_stats->mac_rx_length_error_large);
1133         host_stats->macrx_length_error_small =
1134                         le64_to_cpu(ql_iscsi_stats->mac_rx_length_error_small);
1135         host_stats->macrx_multicast_frames =
1136                         le64_to_cpu(ql_iscsi_stats->mac_rx_multicast_frames);
1137         host_stats->macrx_broadcast_frames =
1138                         le64_to_cpu(ql_iscsi_stats->mac_rx_broadcast_frames);
1139         host_stats->iptx_packets = le64_to_cpu(ql_iscsi_stats->ip_tx_packets);
1140         host_stats->iptx_bytes = le64_to_cpu(ql_iscsi_stats->ip_tx_bytes);
1141         host_stats->iptx_fragments =
1142                         le64_to_cpu(ql_iscsi_stats->ip_tx_fragments);
1143         host_stats->iprx_packets = le64_to_cpu(ql_iscsi_stats->ip_rx_packets);
1144         host_stats->iprx_bytes = le64_to_cpu(ql_iscsi_stats->ip_rx_bytes);
1145         host_stats->iprx_fragments =
1146                         le64_to_cpu(ql_iscsi_stats->ip_rx_fragments);
1147         host_stats->ip_datagram_reassembly =
1148                         le64_to_cpu(ql_iscsi_stats->ip_datagram_reassembly);
1149         host_stats->ip_invalid_address_error =
1150                         le64_to_cpu(ql_iscsi_stats->ip_invalid_address_error);
1151         host_stats->ip_error_packets =
1152                         le64_to_cpu(ql_iscsi_stats->ip_error_packets);
1153         host_stats->ip_fragrx_overlap =
1154                         le64_to_cpu(ql_iscsi_stats->ip_fragrx_overlap);
1155         host_stats->ip_fragrx_outoforder =
1156                         le64_to_cpu(ql_iscsi_stats->ip_fragrx_outoforder);
1157         host_stats->ip_datagram_reassembly_timeout =
1158                 le64_to_cpu(ql_iscsi_stats->ip_datagram_reassembly_timeout);
1159         host_stats->ipv6tx_packets =
1160                         le64_to_cpu(ql_iscsi_stats->ipv6_tx_packets);
1161         host_stats->ipv6tx_bytes = le64_to_cpu(ql_iscsi_stats->ipv6_tx_bytes);
1162         host_stats->ipv6tx_fragments =
1163                         le64_to_cpu(ql_iscsi_stats->ipv6_tx_fragments);
1164         host_stats->ipv6rx_packets =
1165                         le64_to_cpu(ql_iscsi_stats->ipv6_rx_packets);
1166         host_stats->ipv6rx_bytes = le64_to_cpu(ql_iscsi_stats->ipv6_rx_bytes);
1167         host_stats->ipv6rx_fragments =
1168                         le64_to_cpu(ql_iscsi_stats->ipv6_rx_fragments);
1169         host_stats->ipv6_datagram_reassembly =
1170                         le64_to_cpu(ql_iscsi_stats->ipv6_datagram_reassembly);
1171         host_stats->ipv6_invalid_address_error =
1172                 le64_to_cpu(ql_iscsi_stats->ipv6_invalid_address_error);
1173         host_stats->ipv6_error_packets =
1174                         le64_to_cpu(ql_iscsi_stats->ipv6_error_packets);
1175         host_stats->ipv6_fragrx_overlap =
1176                         le64_to_cpu(ql_iscsi_stats->ipv6_fragrx_overlap);
1177         host_stats->ipv6_fragrx_outoforder =
1178                         le64_to_cpu(ql_iscsi_stats->ipv6_fragrx_outoforder);
1179         host_stats->ipv6_datagram_reassembly_timeout =
1180                 le64_to_cpu(ql_iscsi_stats->ipv6_datagram_reassembly_timeout);
1181         host_stats->tcptx_segments =
1182                         le64_to_cpu(ql_iscsi_stats->tcp_tx_segments);
1183         host_stats->tcptx_bytes = le64_to_cpu(ql_iscsi_stats->tcp_tx_bytes);
1184         host_stats->tcprx_segments =
1185                         le64_to_cpu(ql_iscsi_stats->tcp_rx_segments);
1186         host_stats->tcprx_byte = le64_to_cpu(ql_iscsi_stats->tcp_rx_byte);
1187         host_stats->tcp_duplicate_ack_retx =
1188                         le64_to_cpu(ql_iscsi_stats->tcp_duplicate_ack_retx);
1189         host_stats->tcp_retx_timer_expired =
1190                         le64_to_cpu(ql_iscsi_stats->tcp_retx_timer_expired);
1191         host_stats->tcprx_duplicate_ack =
1192                         le64_to_cpu(ql_iscsi_stats->tcp_rx_duplicate_ack);
1193         host_stats->tcprx_pure_ackr =
1194                         le64_to_cpu(ql_iscsi_stats->tcp_rx_pure_ackr);
1195         host_stats->tcptx_delayed_ack =
1196                         le64_to_cpu(ql_iscsi_stats->tcp_tx_delayed_ack);
1197         host_stats->tcptx_pure_ack =
1198                         le64_to_cpu(ql_iscsi_stats->tcp_tx_pure_ack);
1199         host_stats->tcprx_segment_error =
1200                         le64_to_cpu(ql_iscsi_stats->tcp_rx_segment_error);
1201         host_stats->tcprx_segment_outoforder =
1202                         le64_to_cpu(ql_iscsi_stats->tcp_rx_segment_outoforder);
1203         host_stats->tcprx_window_probe =
1204                         le64_to_cpu(ql_iscsi_stats->tcp_rx_window_probe);
1205         host_stats->tcprx_window_update =
1206                         le64_to_cpu(ql_iscsi_stats->tcp_rx_window_update);
1207         host_stats->tcptx_window_probe_persist =
1208                 le64_to_cpu(ql_iscsi_stats->tcp_tx_window_probe_persist);
1209         host_stats->ecc_error_correction =
1210                         le64_to_cpu(ql_iscsi_stats->ecc_error_correction);
1211         host_stats->iscsi_pdu_tx = le64_to_cpu(ql_iscsi_stats->iscsi_pdu_tx);
1212         host_stats->iscsi_data_bytes_tx =
1213                         le64_to_cpu(ql_iscsi_stats->iscsi_data_bytes_tx);
1214         host_stats->iscsi_pdu_rx = le64_to_cpu(ql_iscsi_stats->iscsi_pdu_rx);
1215         host_stats->iscsi_data_bytes_rx =
1216                         le64_to_cpu(ql_iscsi_stats->iscsi_data_bytes_rx);
1217         host_stats->iscsi_io_completed =
1218                         le64_to_cpu(ql_iscsi_stats->iscsi_io_completed);
1219         host_stats->iscsi_unexpected_io_rx =
1220                         le64_to_cpu(ql_iscsi_stats->iscsi_unexpected_io_rx);
1221         host_stats->iscsi_format_error =
1222                         le64_to_cpu(ql_iscsi_stats->iscsi_format_error);
1223         host_stats->iscsi_hdr_digest_error =
1224                         le64_to_cpu(ql_iscsi_stats->iscsi_hdr_digest_error);
1225         host_stats->iscsi_data_digest_error =
1226                         le64_to_cpu(ql_iscsi_stats->iscsi_data_digest_error);
1227         host_stats->iscsi_sequence_error =
1228                         le64_to_cpu(ql_iscsi_stats->iscsi_sequence_error);
1229 exit_host_stats:
1230         if (ql_iscsi_stats)
1231                 dma_free_coherent(&ha->pdev->dev, stats_size,
1232                                   ql_iscsi_stats, iscsi_stats_dma);
1233
1234         ql4_printk(KERN_INFO, ha, "%s: Get host stats done\n",
1235                    __func__);
1236         return ret;
1237 }
1238
1239 static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
1240                                    enum iscsi_param_type param_type,
1241                                    int param, char *buf)
1242 {
1243         struct Scsi_Host *shost = iscsi_iface_to_shost(iface);
1244         struct scsi_qla_host *ha = to_qla_host(shost);
1245         int ival;
1246         char *pval = NULL;
1247         int len = -ENOSYS;
1248
1249         if (param_type == ISCSI_NET_PARAM) {
1250                 switch (param) {
1251                 case ISCSI_NET_PARAM_IPV4_ADDR:
1252                         len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
1253                         break;
1254                 case ISCSI_NET_PARAM_IPV4_SUBNET:
1255                         len = sprintf(buf, "%pI4\n",
1256                                       &ha->ip_config.subnet_mask);
1257                         break;
1258                 case ISCSI_NET_PARAM_IPV4_GW:
1259                         len = sprintf(buf, "%pI4\n", &ha->ip_config.gateway);
1260                         break;
1261                 case ISCSI_NET_PARAM_IFACE_ENABLE:
1262                         if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
1263                                 OP_STATE(ha->ip_config.ipv4_options,
1264                                          IPOPT_IPV4_PROTOCOL_ENABLE, pval);
1265                         } else {
1266                                 OP_STATE(ha->ip_config.ipv6_options,
1267                                          IPV6_OPT_IPV6_PROTOCOL_ENABLE, pval);
1268                         }
1269
1270                         len = sprintf(buf, "%s\n", pval);
1271                         break;
1272                 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
1273                         len = sprintf(buf, "%s\n",
1274                                       (ha->ip_config.tcp_options &
1275                                        TCPOPT_DHCP_ENABLE) ?
1276                                       "dhcp" : "static");
1277                         break;
1278                 case ISCSI_NET_PARAM_IPV6_ADDR:
1279                         if (iface->iface_num == 0)
1280                                 len = sprintf(buf, "%pI6\n",
1281                                               &ha->ip_config.ipv6_addr0);
1282                         if (iface->iface_num == 1)
1283                                 len = sprintf(buf, "%pI6\n",
1284                                               &ha->ip_config.ipv6_addr1);
1285                         break;
1286                 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
1287                         len = sprintf(buf, "%pI6\n",
1288                                       &ha->ip_config.ipv6_link_local_addr);
1289                         break;
1290                 case ISCSI_NET_PARAM_IPV6_ROUTER:
1291                         len = sprintf(buf, "%pI6\n",
1292                                       &ha->ip_config.ipv6_default_router_addr);
1293                         break;
1294                 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
1295                         pval = (ha->ip_config.ipv6_addl_options &
1296                                 IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) ?
1297                                 "nd" : "static";
1298
1299                         len = sprintf(buf, "%s\n", pval);
1300                         break;
1301                 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
1302                         pval = (ha->ip_config.ipv6_addl_options &
1303                                 IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR) ?
1304                                 "auto" : "static";
1305
1306                         len = sprintf(buf, "%s\n", pval);
1307                         break;
1308                 case ISCSI_NET_PARAM_VLAN_ID:
1309                         if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
1310                                 ival = ha->ip_config.ipv4_vlan_tag &
1311                                        ISCSI_MAX_VLAN_ID;
1312                         else
1313                                 ival = ha->ip_config.ipv6_vlan_tag &
1314                                        ISCSI_MAX_VLAN_ID;
1315
1316                         len = sprintf(buf, "%d\n", ival);
1317                         break;
1318                 case ISCSI_NET_PARAM_VLAN_PRIORITY:
1319                         if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
1320                                 ival = (ha->ip_config.ipv4_vlan_tag >> 13) &
1321                                        ISCSI_MAX_VLAN_PRIORITY;
1322                         else
1323                                 ival = (ha->ip_config.ipv6_vlan_tag >> 13) &
1324                                        ISCSI_MAX_VLAN_PRIORITY;
1325
1326                         len = sprintf(buf, "%d\n", ival);
1327                         break;
1328                 case ISCSI_NET_PARAM_VLAN_ENABLED:
1329                         if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
1330                                 OP_STATE(ha->ip_config.ipv4_options,
1331                                          IPOPT_VLAN_TAGGING_ENABLE, pval);
1332                         } else {
1333                                 OP_STATE(ha->ip_config.ipv6_options,
1334                                          IPV6_OPT_VLAN_TAGGING_ENABLE, pval);
1335                         }
1336                         len = sprintf(buf, "%s\n", pval);
1337                         break;
1338                 case ISCSI_NET_PARAM_MTU:
1339                         len = sprintf(buf, "%d\n", ha->ip_config.eth_mtu_size);
1340                         break;
1341                 case ISCSI_NET_PARAM_PORT:
1342                         if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
1343                                 len = sprintf(buf, "%d\n",
1344                                               ha->ip_config.ipv4_port);
1345                         else
1346                                 len = sprintf(buf, "%d\n",
1347                                               ha->ip_config.ipv6_port);
1348                         break;
1349                 case ISCSI_NET_PARAM_IPADDR_STATE:
1350                         if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
1351                                 pval = iscsi_get_ipaddress_state_name(
1352                                                 ha->ip_config.ipv4_addr_state);
1353                         } else {
1354                                 if (iface->iface_num == 0)
1355                                         pval = iscsi_get_ipaddress_state_name(
1356                                                 ha->ip_config.ipv6_addr0_state);
1357                                 else if (iface->iface_num == 1)
1358                                         pval = iscsi_get_ipaddress_state_name(
1359                                                 ha->ip_config.ipv6_addr1_state);
1360                         }
1361
1362                         len = sprintf(buf, "%s\n", pval);
1363                         break;
1364                 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_STATE:
1365                         pval = iscsi_get_ipaddress_state_name(
1366                                         ha->ip_config.ipv6_link_local_state);
1367                         len = sprintf(buf, "%s\n", pval);
1368                         break;
1369                 case ISCSI_NET_PARAM_IPV6_ROUTER_STATE:
1370                         pval = iscsi_get_router_state_name(
1371                                       ha->ip_config.ipv6_default_router_state);
1372                         len = sprintf(buf, "%s\n", pval);
1373                         break;
1374                 case ISCSI_NET_PARAM_DELAYED_ACK_EN:
1375                         if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
1376                                 OP_STATE(~ha->ip_config.tcp_options,
1377                                          TCPOPT_DELAYED_ACK_DISABLE, pval);
1378                         } else {
1379                                 OP_STATE(~ha->ip_config.ipv6_tcp_options,
1380                                          IPV6_TCPOPT_DELAYED_ACK_DISABLE, pval);
1381                         }
1382                         len = sprintf(buf, "%s\n", pval);
1383                         break;
1384                 case ISCSI_NET_PARAM_TCP_NAGLE_DISABLE:
1385                         if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
1386                                 OP_STATE(~ha->ip_config.tcp_options,
1387                                          TCPOPT_NAGLE_ALGO_DISABLE, pval);
1388                         } else {
1389                                 OP_STATE(~ha->ip_config.ipv6_tcp_options,
1390                                          IPV6_TCPOPT_NAGLE_ALGO_DISABLE, pval);
1391                         }
1392                         len = sprintf(buf, "%s\n", pval);
1393                         break;
1394                 case ISCSI_NET_PARAM_TCP_WSF_DISABLE:
1395                         if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
1396                                 OP_STATE(~ha->ip_config.tcp_options,
1397                                          TCPOPT_WINDOW_SCALE_DISABLE, pval);
1398                         } else {
1399                                 OP_STATE(~ha->ip_config.ipv6_tcp_options,
1400                                          IPV6_TCPOPT_WINDOW_SCALE_DISABLE,
1401                                          pval);
1402                         }
1403                         len = sprintf(buf, "%s\n", pval);
1404                         break;
1405                 case ISCSI_NET_PARAM_TCP_WSF:
1406                         if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
1407                                 len = sprintf(buf, "%d\n",
1408                                               ha->ip_config.tcp_wsf);
1409                         else
1410                                 len = sprintf(buf, "%d\n",
1411                                               ha->ip_config.ipv6_tcp_wsf);
1412                         break;
1413                 case ISCSI_NET_PARAM_TCP_TIMER_SCALE:
1414                         if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
1415                                 ival = (ha->ip_config.tcp_options &
1416                                         TCPOPT_TIMER_SCALE) >> 1;
1417                         else
1418                                 ival = (ha->ip_config.ipv6_tcp_options &
1419                                         IPV6_TCPOPT_TIMER_SCALE) >> 1;
1420
1421                         len = sprintf(buf, "%d\n", ival);
1422                         break;
1423                 case ISCSI_NET_PARAM_TCP_TIMESTAMP_EN:
1424                         if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
1425                                 OP_STATE(ha->ip_config.tcp_options,
1426                                          TCPOPT_TIMESTAMP_ENABLE, pval);
1427                         } else {
1428                                 OP_STATE(ha->ip_config.ipv6_tcp_options,
1429                                          IPV6_TCPOPT_TIMESTAMP_EN, pval);
1430                         }
1431                         len = sprintf(buf, "%s\n", pval);
1432                         break;
1433                 case ISCSI_NET_PARAM_CACHE_ID:
1434                         if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
1435                                 len = sprintf(buf, "%d\n",
1436                                               ha->ip_config.ipv4_cache_id);
1437                         else
1438                                 len = sprintf(buf, "%d\n",
1439                                               ha->ip_config.ipv6_cache_id);
1440                         break;
1441                 case ISCSI_NET_PARAM_IPV4_DHCP_DNS_ADDR_EN:
1442                         OP_STATE(ha->ip_config.tcp_options,
1443                                  TCPOPT_DNS_SERVER_IP_EN, pval);
1444
1445                         len = sprintf(buf, "%s\n", pval);
1446                         break;
1447                 case ISCSI_NET_PARAM_IPV4_DHCP_SLP_DA_EN:
1448                         OP_STATE(ha->ip_config.tcp_options,
1449                                  TCPOPT_SLP_DA_INFO_EN, pval);
1450
1451                         len = sprintf(buf, "%s\n", pval);
1452                         break;
1453                 case ISCSI_NET_PARAM_IPV4_TOS_EN:
1454                         OP_STATE(ha->ip_config.ipv4_options,
1455                                  IPOPT_IPV4_TOS_EN, pval);
1456
1457                         len = sprintf(buf, "%s\n", pval);
1458                         break;
1459                 case ISCSI_NET_PARAM_IPV4_TOS:
1460                         len = sprintf(buf, "%d\n", ha->ip_config.ipv4_tos);
1461                         break;
1462                 case ISCSI_NET_PARAM_IPV4_GRAT_ARP_EN:
1463                         OP_STATE(ha->ip_config.ipv4_options,
1464                                  IPOPT_GRAT_ARP_EN, pval);
1465
1466                         len = sprintf(buf, "%s\n", pval);
1467                         break;
1468                 case ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID_EN:
1469                         OP_STATE(ha->ip_config.ipv4_options, IPOPT_ALT_CID_EN,
1470                                  pval);
1471
1472                         len = sprintf(buf, "%s\n", pval);
1473                         break;
1474                 case ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID:
1475                         pval = (ha->ip_config.ipv4_alt_cid_len) ?
1476                                (char *)ha->ip_config.ipv4_alt_cid : "";
1477
1478                         len = sprintf(buf, "%s\n", pval);
1479                         break;
1480                 case ISCSI_NET_PARAM_IPV4_DHCP_REQ_VENDOR_ID_EN:
1481                         OP_STATE(ha->ip_config.ipv4_options,
1482                                  IPOPT_REQ_VID_EN, pval);
1483
1484                         len = sprintf(buf, "%s\n", pval);
1485                         break;
1486                 case ISCSI_NET_PARAM_IPV4_DHCP_USE_VENDOR_ID_EN:
1487                         OP_STATE(ha->ip_config.ipv4_options,
1488                                  IPOPT_USE_VID_EN, pval);
1489
1490                         len = sprintf(buf, "%s\n", pval);
1491                         break;
1492                 case ISCSI_NET_PARAM_IPV4_DHCP_VENDOR_ID:
1493                         pval = (ha->ip_config.ipv4_vid_len) ?
1494                                (char *)ha->ip_config.ipv4_vid : "";
1495
1496                         len = sprintf(buf, "%s\n", pval);
1497                         break;
1498                 case ISCSI_NET_PARAM_IPV4_DHCP_LEARN_IQN_EN:
1499                         OP_STATE(ha->ip_config.ipv4_options,
1500                                  IPOPT_LEARN_IQN_EN, pval);
1501
1502                         len = sprintf(buf, "%s\n", pval);
1503                         break;
1504                 case ISCSI_NET_PARAM_IPV4_FRAGMENT_DISABLE:
1505                         OP_STATE(~ha->ip_config.ipv4_options,
1506                                  IPOPT_FRAGMENTATION_DISABLE, pval);
1507
1508                         len = sprintf(buf, "%s\n", pval);
1509                         break;
1510                 case ISCSI_NET_PARAM_IPV4_IN_FORWARD_EN:
1511                         OP_STATE(ha->ip_config.ipv4_options,
1512                                  IPOPT_IN_FORWARD_EN, pval);
1513
1514                         len = sprintf(buf, "%s\n", pval);
1515                         break;
1516                 case ISCSI_NET_PARAM_REDIRECT_EN:
1517                         if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
1518                                 OP_STATE(ha->ip_config.ipv4_options,
1519                                          IPOPT_ARP_REDIRECT_EN, pval);
1520                         } else {
1521                                 OP_STATE(ha->ip_config.ipv6_options,
1522                                          IPV6_OPT_REDIRECT_EN, pval);
1523                         }
1524                         len = sprintf(buf, "%s\n", pval);
1525                         break;
1526                 case ISCSI_NET_PARAM_IPV4_TTL:
1527                         len = sprintf(buf, "%d\n", ha->ip_config.ipv4_ttl);
1528                         break;
1529                 case ISCSI_NET_PARAM_IPV6_GRAT_NEIGHBOR_ADV_EN:
1530                         OP_STATE(ha->ip_config.ipv6_options,
1531                                  IPV6_OPT_GRAT_NEIGHBOR_ADV_EN, pval);
1532
1533                         len = sprintf(buf, "%s\n", pval);
1534                         break;
1535                 case ISCSI_NET_PARAM_IPV6_MLD_EN:
1536                         OP_STATE(ha->ip_config.ipv6_addl_options,
1537                                  IPV6_ADDOPT_MLD_EN, pval);
1538
1539                         len = sprintf(buf, "%s\n", pval);
1540                         break;
1541                 case ISCSI_NET_PARAM_IPV6_FLOW_LABEL:
1542                         len = sprintf(buf, "%u\n", ha->ip_config.ipv6_flow_lbl);
1543                         break;
1544                 case ISCSI_NET_PARAM_IPV6_TRAFFIC_CLASS:
1545                         len = sprintf(buf, "%d\n",
1546                                       ha->ip_config.ipv6_traffic_class);
1547                         break;
1548                 case ISCSI_NET_PARAM_IPV6_HOP_LIMIT:
1549                         len = sprintf(buf, "%d\n",
1550                                       ha->ip_config.ipv6_hop_limit);
1551                         break;
1552                 case ISCSI_NET_PARAM_IPV6_ND_REACHABLE_TMO:
1553                         len = sprintf(buf, "%d\n",
1554                                       ha->ip_config.ipv6_nd_reach_time);
1555                         break;
1556                 case ISCSI_NET_PARAM_IPV6_ND_REXMIT_TIME:
1557                         len = sprintf(buf, "%d\n",
1558                                       ha->ip_config.ipv6_nd_rexmit_timer);
1559                         break;
1560                 case ISCSI_NET_PARAM_IPV6_ND_STALE_TMO:
1561                         len = sprintf(buf, "%d\n",
1562                                       ha->ip_config.ipv6_nd_stale_timeout);
1563                         break;
1564                 case ISCSI_NET_PARAM_IPV6_DUP_ADDR_DETECT_CNT:
1565                         len = sprintf(buf, "%d\n",
1566                                       ha->ip_config.ipv6_dup_addr_detect_count);
1567                         break;
1568                 case ISCSI_NET_PARAM_IPV6_RTR_ADV_LINK_MTU:
1569                         len = sprintf(buf, "%d\n",
1570                                       ha->ip_config.ipv6_gw_advrt_mtu);
1571                         break;
1572                 default:
1573                         len = -ENOSYS;
1574                 }
1575         } else if (param_type == ISCSI_IFACE_PARAM) {
1576                 switch (param) {
1577                 case ISCSI_IFACE_PARAM_DEF_TASKMGMT_TMO:
1578                         len = sprintf(buf, "%d\n", ha->ip_config.def_timeout);
1579                         break;
1580                 case ISCSI_IFACE_PARAM_HDRDGST_EN:
1581                         OP_STATE(ha->ip_config.iscsi_options,
1582                                  ISCSIOPTS_HEADER_DIGEST_EN, pval);
1583
1584                         len = sprintf(buf, "%s\n", pval);
1585                         break;
1586                 case ISCSI_IFACE_PARAM_DATADGST_EN:
1587                         OP_STATE(ha->ip_config.iscsi_options,
1588                                  ISCSIOPTS_DATA_DIGEST_EN, pval);
1589
1590                         len = sprintf(buf, "%s\n", pval);
1591                         break;
1592                 case ISCSI_IFACE_PARAM_IMM_DATA_EN:
1593                         OP_STATE(ha->ip_config.iscsi_options,
1594                                  ISCSIOPTS_IMMEDIATE_DATA_EN, pval);
1595
1596                         len = sprintf(buf, "%s\n", pval);
1597                         break;
1598                 case ISCSI_IFACE_PARAM_INITIAL_R2T_EN:
1599                         OP_STATE(ha->ip_config.iscsi_options,
1600                                  ISCSIOPTS_INITIAL_R2T_EN, pval);
1601
1602                         len = sprintf(buf, "%s\n", pval);
1603                         break;
1604                 case ISCSI_IFACE_PARAM_DATASEQ_INORDER_EN:
1605                         OP_STATE(ha->ip_config.iscsi_options,
1606                                  ISCSIOPTS_DATA_SEQ_INORDER_EN, pval);
1607
1608                         len = sprintf(buf, "%s\n", pval);
1609                         break;
1610                 case ISCSI_IFACE_PARAM_PDU_INORDER_EN:
1611                         OP_STATE(ha->ip_config.iscsi_options,
1612                                  ISCSIOPTS_DATA_PDU_INORDER_EN, pval);
1613
1614                         len = sprintf(buf, "%s\n", pval);
1615                         break;
1616                 case ISCSI_IFACE_PARAM_ERL:
1617                         len = sprintf(buf, "%d\n",
1618                                       (ha->ip_config.iscsi_options &
1619                                        ISCSIOPTS_ERL));
1620                         break;
1621                 case ISCSI_IFACE_PARAM_MAX_RECV_DLENGTH:
1622                         len = sprintf(buf, "%u\n",
1623                                       ha->ip_config.iscsi_max_pdu_size *
1624                                       BYTE_UNITS);
1625                         break;
1626                 case ISCSI_IFACE_PARAM_FIRST_BURST:
1627                         len = sprintf(buf, "%u\n",
1628                                       ha->ip_config.iscsi_first_burst_len *
1629                                       BYTE_UNITS);
1630                         break;
1631                 case ISCSI_IFACE_PARAM_MAX_R2T:
1632                         len = sprintf(buf, "%d\n",
1633                                       ha->ip_config.iscsi_max_outstnd_r2t);
1634                         break;
1635                 case ISCSI_IFACE_PARAM_MAX_BURST:
1636                         len = sprintf(buf, "%u\n",
1637                                       ha->ip_config.iscsi_max_burst_len *
1638                                       BYTE_UNITS);
1639                         break;
1640                 case ISCSI_IFACE_PARAM_CHAP_AUTH_EN:
1641                         OP_STATE(ha->ip_config.iscsi_options,
1642                                  ISCSIOPTS_CHAP_AUTH_EN, pval);
1643
1644                         len = sprintf(buf, "%s\n", pval);
1645                         break;
1646                 case ISCSI_IFACE_PARAM_BIDI_CHAP_EN:
1647                         OP_STATE(ha->ip_config.iscsi_options,
1648                                  ISCSIOPTS_BIDI_CHAP_EN, pval);
1649
1650                         len = sprintf(buf, "%s\n", pval);
1651                         break;
1652                 case ISCSI_IFACE_PARAM_DISCOVERY_AUTH_OPTIONAL:
1653                         OP_STATE(ha->ip_config.iscsi_options,
1654                                  ISCSIOPTS_DISCOVERY_AUTH_EN, pval);
1655
1656                         len = sprintf(buf, "%s\n", pval);
1657                         break;
1658                 case ISCSI_IFACE_PARAM_DISCOVERY_LOGOUT_EN:
1659                         OP_STATE(ha->ip_config.iscsi_options,
1660                                  ISCSIOPTS_DISCOVERY_LOGOUT_EN, pval);
1661
1662                         len = sprintf(buf, "%s\n", pval);
1663                         break;
1664                 case ISCSI_IFACE_PARAM_STRICT_LOGIN_COMP_EN:
1665                         OP_STATE(ha->ip_config.iscsi_options,
1666                                  ISCSIOPTS_STRICT_LOGIN_COMP_EN, pval);
1667
1668                         len = sprintf(buf, "%s\n", pval);
1669                         break;
1670                 case ISCSI_IFACE_PARAM_INITIATOR_NAME:
1671                         len = sprintf(buf, "%s\n", ha->ip_config.iscsi_name);
1672                         break;
1673                 default:
1674                         len = -ENOSYS;
1675                 }
1676         }
1677
1678         return len;
1679 }
1680
1681 static struct iscsi_endpoint *
1682 qla4xxx_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
1683                    int non_blocking)
1684 {
1685         int ret;
1686         struct iscsi_endpoint *ep;
1687         struct qla_endpoint *qla_ep;
1688         struct scsi_qla_host *ha;
1689         struct sockaddr_in *addr;
1690         struct sockaddr_in6 *addr6;
1691
1692         if (!shost) {
1693                 ret = -ENXIO;
1694                 pr_err("%s: shost is NULL\n", __func__);
1695                 return ERR_PTR(ret);
1696         }
1697
1698         ha = iscsi_host_priv(shost);
1699         ep = iscsi_create_endpoint(sizeof(struct qla_endpoint));
1700         if (!ep) {
1701                 ret = -ENOMEM;
1702                 return ERR_PTR(ret);
1703         }
1704
1705         qla_ep = ep->dd_data;
1706         memset(qla_ep, 0, sizeof(struct qla_endpoint));
1707         if (dst_addr->sa_family == AF_INET) {
1708                 memcpy(&qla_ep->dst_addr, dst_addr, sizeof(struct sockaddr_in));
1709                 addr = (struct sockaddr_in *)&qla_ep->dst_addr;
1710                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI4\n", __func__,
1711                                   (char *)&addr->sin_addr));
1712         } else if (dst_addr->sa_family == AF_INET6) {
1713                 memcpy(&qla_ep->dst_addr, dst_addr,
1714                        sizeof(struct sockaddr_in6));
1715                 addr6 = (struct sockaddr_in6 *)&qla_ep->dst_addr;
1716                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI6\n", __func__,
1717                                   (char *)&addr6->sin6_addr));
1718         } else {
1719                 ql4_printk(KERN_WARNING, ha, "%s: Invalid endpoint\n",
1720                            __func__);
1721         }
1722
1723         qla_ep->host = shost;
1724
1725         return ep;
1726 }
1727
1728 static int qla4xxx_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
1729 {
1730         struct qla_endpoint *qla_ep;
1731         struct scsi_qla_host *ha;
1732         int ret = 0;
1733
1734         qla_ep = ep->dd_data;
1735         ha = to_qla_host(qla_ep->host);
1736         DEBUG2(pr_info_ratelimited("%s: host: %ld\n", __func__, ha->host_no));
1737
1738         if (adapter_up(ha) && !test_bit(AF_BUILD_DDB_LIST, &ha->flags))
1739                 ret = 1;
1740
1741         return ret;
1742 }
1743
1744 static void qla4xxx_ep_disconnect(struct iscsi_endpoint *ep)
1745 {
1746         struct qla_endpoint *qla_ep;
1747         struct scsi_qla_host *ha;
1748
1749         qla_ep = ep->dd_data;
1750         ha = to_qla_host(qla_ep->host);
1751         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: host: %ld\n", __func__,
1752                           ha->host_no));
1753         iscsi_destroy_endpoint(ep);
1754 }
1755
1756 static int qla4xxx_get_ep_param(struct iscsi_endpoint *ep,
1757                                 enum iscsi_param param,
1758                                 char *buf)
1759 {
1760         struct qla_endpoint *qla_ep = ep->dd_data;
1761         struct sockaddr *dst_addr;
1762         struct scsi_qla_host *ha;
1763
1764         if (!qla_ep)
1765                 return -ENOTCONN;
1766
1767         ha = to_qla_host(qla_ep->host);
1768         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: host: %ld\n", __func__,
1769                           ha->host_no));
1770
1771         switch (param) {
1772         case ISCSI_PARAM_CONN_PORT:
1773         case ISCSI_PARAM_CONN_ADDRESS:
1774                 dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
1775                 if (!dst_addr)
1776                         return -ENOTCONN;
1777
1778                 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
1779                                                  &qla_ep->dst_addr, param, buf);
1780         default:
1781                 return -ENOSYS;
1782         }
1783 }
1784
1785 static void qla4xxx_conn_get_stats(struct iscsi_cls_conn *cls_conn,
1786                                    struct iscsi_stats *stats)
1787 {
1788         struct iscsi_session *sess;
1789         struct iscsi_cls_session *cls_sess;
1790         struct ddb_entry *ddb_entry;
1791         struct scsi_qla_host *ha;
1792         struct ql_iscsi_stats *ql_iscsi_stats;
1793         int stats_size;
1794         int ret;
1795         dma_addr_t iscsi_stats_dma;
1796
1797         cls_sess = iscsi_conn_to_session(cls_conn);
1798         sess = cls_sess->dd_data;
1799         ddb_entry = sess->dd_data;
1800         ha = ddb_entry->ha;
1801
1802         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: host: %ld\n", __func__,
1803                           ha->host_no));
1804         stats_size = PAGE_ALIGN(sizeof(struct ql_iscsi_stats));
1805         /* Allocate memory */
1806         ql_iscsi_stats = dma_alloc_coherent(&ha->pdev->dev, stats_size,
1807                                             &iscsi_stats_dma, GFP_KERNEL);
1808         if (!ql_iscsi_stats) {
1809                 ql4_printk(KERN_ERR, ha,
1810                            "Unable to allocate memory for iscsi stats\n");
1811                 goto exit_get_stats;
1812         }
1813
1814         ret =  qla4xxx_get_mgmt_data(ha, ddb_entry->fw_ddb_index, stats_size,
1815                                      iscsi_stats_dma);
1816         if (ret != QLA_SUCCESS) {
1817                 ql4_printk(KERN_ERR, ha,
1818                            "Unable to retrieve iscsi stats\n");
1819                 goto free_stats;
1820         }
1821
1822         /* octets */
1823         stats->txdata_octets = le64_to_cpu(ql_iscsi_stats->tx_data_octets);
1824         stats->rxdata_octets = le64_to_cpu(ql_iscsi_stats->rx_data_octets);
1825         /* xmit pdus */
1826         stats->noptx_pdus = le32_to_cpu(ql_iscsi_stats->tx_nopout_pdus);
1827         stats->scsicmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_cmd_pdus);
1828         stats->tmfcmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_tmf_cmd_pdus);
1829         stats->login_pdus = le32_to_cpu(ql_iscsi_stats->tx_login_cmd_pdus);
1830         stats->text_pdus = le32_to_cpu(ql_iscsi_stats->tx_text_cmd_pdus);
1831         stats->dataout_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_write_pdus);
1832         stats->logout_pdus = le32_to_cpu(ql_iscsi_stats->tx_logout_cmd_pdus);
1833         stats->snack_pdus = le32_to_cpu(ql_iscsi_stats->tx_snack_req_pdus);
1834         /* recv pdus */
1835         stats->noprx_pdus = le32_to_cpu(ql_iscsi_stats->rx_nopin_pdus);
1836         stats->scsirsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_resp_pdus);
1837         stats->tmfrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_tmf_resp_pdus);
1838         stats->textrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_text_resp_pdus);
1839         stats->datain_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_read_pdus);
1840         stats->logoutrsp_pdus =
1841                         le32_to_cpu(ql_iscsi_stats->rx_logout_resp_pdus);
1842         stats->r2t_pdus = le32_to_cpu(ql_iscsi_stats->rx_r2t_pdus);
1843         stats->async_pdus = le32_to_cpu(ql_iscsi_stats->rx_async_pdus);
1844         stats->rjt_pdus = le32_to_cpu(ql_iscsi_stats->rx_reject_pdus);
1845
1846 free_stats:
1847         dma_free_coherent(&ha->pdev->dev, stats_size, ql_iscsi_stats,
1848                           iscsi_stats_dma);
1849 exit_get_stats:
1850         return;
1851 }
1852
1853 static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
1854 {
1855         struct iscsi_cls_session *session;
1856         struct iscsi_session *sess;
1857         unsigned long flags;
1858         enum blk_eh_timer_return ret = BLK_EH_NOT_HANDLED;
1859
1860         session = starget_to_session(scsi_target(sc->device));
1861         sess = session->dd_data;
1862
1863         spin_lock_irqsave(&session->lock, flags);
1864         if (session->state == ISCSI_SESSION_FAILED)
1865                 ret = BLK_EH_RESET_TIMER;
1866         spin_unlock_irqrestore(&session->lock, flags);
1867
1868         return ret;
1869 }
1870
1871 static void qla4xxx_set_port_speed(struct Scsi_Host *shost)
1872 {
1873         struct scsi_qla_host *ha = to_qla_host(shost);
1874         struct iscsi_cls_host *ihost = shost->shost_data;
1875         uint32_t speed = ISCSI_PORT_SPEED_UNKNOWN;
1876
1877         qla4xxx_get_firmware_state(ha);
1878
1879         switch (ha->addl_fw_state & 0x0F00) {
1880         case FW_ADDSTATE_LINK_SPEED_10MBPS:
1881                 speed = ISCSI_PORT_SPEED_10MBPS;
1882                 break;
1883         case FW_ADDSTATE_LINK_SPEED_100MBPS:
1884                 speed = ISCSI_PORT_SPEED_100MBPS;
1885                 break;
1886         case FW_ADDSTATE_LINK_SPEED_1GBPS:
1887                 speed = ISCSI_PORT_SPEED_1GBPS;
1888                 break;
1889         case FW_ADDSTATE_LINK_SPEED_10GBPS:
1890                 speed = ISCSI_PORT_SPEED_10GBPS;
1891                 break;
1892         }
1893         ihost->port_speed = speed;
1894 }
1895
1896 static void qla4xxx_set_port_state(struct Scsi_Host *shost)
1897 {
1898         struct scsi_qla_host *ha = to_qla_host(shost);
1899         struct iscsi_cls_host *ihost = shost->shost_data;
1900         uint32_t state = ISCSI_PORT_STATE_DOWN;
1901
1902         if (test_bit(AF_LINK_UP, &ha->flags))
1903                 state = ISCSI_PORT_STATE_UP;
1904
1905         ihost->port_state = state;
1906 }
1907
1908 static int qla4xxx_host_get_param(struct Scsi_Host *shost,
1909                                   enum iscsi_host_param param, char *buf)
1910 {
1911         struct scsi_qla_host *ha = to_qla_host(shost);
1912         int len;
1913
1914         switch (param) {
1915         case ISCSI_HOST_PARAM_HWADDRESS:
1916                 len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
1917                 break;
1918         case ISCSI_HOST_PARAM_IPADDRESS:
1919                 len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
1920                 break;
1921         case ISCSI_HOST_PARAM_INITIATOR_NAME:
1922                 len = sprintf(buf, "%s\n", ha->name_string);
1923                 break;
1924         case ISCSI_HOST_PARAM_PORT_STATE:
1925                 qla4xxx_set_port_state(shost);
1926                 len = sprintf(buf, "%s\n", iscsi_get_port_state_name(shost));
1927                 break;
1928         case ISCSI_HOST_PARAM_PORT_SPEED:
1929                 qla4xxx_set_port_speed(shost);
1930                 len = sprintf(buf, "%s\n", iscsi_get_port_speed_name(shost));
1931                 break;
1932         default:
1933                 return -ENOSYS;
1934         }
1935
1936         return len;
1937 }
1938
1939 static void qla4xxx_create_ipv4_iface(struct scsi_qla_host *ha)
1940 {
1941         if (ha->iface_ipv4)
1942                 return;
1943
1944         /* IPv4 */
1945         ha->iface_ipv4 = iscsi_create_iface(ha->host,
1946                                             &qla4xxx_iscsi_transport,
1947                                             ISCSI_IFACE_TYPE_IPV4, 0, 0);
1948         if (!ha->iface_ipv4)
1949                 ql4_printk(KERN_ERR, ha, "Could not create IPv4 iSCSI "
1950                            "iface0.\n");
1951 }
1952
1953 static void qla4xxx_create_ipv6_iface(struct scsi_qla_host *ha)
1954 {
1955         if (!ha->iface_ipv6_0)
1956                 /* IPv6 iface-0 */
1957                 ha->iface_ipv6_0 = iscsi_create_iface(ha->host,
1958                                                       &qla4xxx_iscsi_transport,
1959                                                       ISCSI_IFACE_TYPE_IPV6, 0,
1960                                                       0);
1961         if (!ha->iface_ipv6_0)
1962                 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
1963                            "iface0.\n");
1964
1965         if (!ha->iface_ipv6_1)
1966                 /* IPv6 iface-1 */
1967                 ha->iface_ipv6_1 = iscsi_create_iface(ha->host,
1968                                                       &qla4xxx_iscsi_transport,
1969                                                       ISCSI_IFACE_TYPE_IPV6, 1,
1970                                                       0);
1971         if (!ha->iface_ipv6_1)
1972                 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
1973                            "iface1.\n");
1974 }
1975
1976 static void qla4xxx_create_ifaces(struct scsi_qla_host *ha)
1977 {
1978         if (ha->ip_config.ipv4_options & IPOPT_IPV4_PROTOCOL_ENABLE)
1979                 qla4xxx_create_ipv4_iface(ha);
1980
1981         if (ha->ip_config.ipv6_options & IPV6_OPT_IPV6_PROTOCOL_ENABLE)
1982                 qla4xxx_create_ipv6_iface(ha);
1983 }
1984
1985 static void qla4xxx_destroy_ipv4_iface(struct scsi_qla_host *ha)
1986 {
1987         if (ha->iface_ipv4) {
1988                 iscsi_destroy_iface(ha->iface_ipv4);
1989                 ha->iface_ipv4 = NULL;
1990         }
1991 }
1992
1993 static void qla4xxx_destroy_ipv6_iface(struct scsi_qla_host *ha)
1994 {
1995         if (ha->iface_ipv6_0) {
1996                 iscsi_destroy_iface(ha->iface_ipv6_0);
1997                 ha->iface_ipv6_0 = NULL;
1998         }
1999         if (ha->iface_ipv6_1) {
2000                 iscsi_destroy_iface(ha->iface_ipv6_1);
2001                 ha->iface_ipv6_1 = NULL;
2002         }
2003 }
2004
2005 static void qla4xxx_destroy_ifaces(struct scsi_qla_host *ha)
2006 {
2007         qla4xxx_destroy_ipv4_iface(ha);
2008         qla4xxx_destroy_ipv6_iface(ha);
2009 }
2010
2011 static void qla4xxx_set_ipv6(struct scsi_qla_host *ha,
2012                              struct iscsi_iface_param_info *iface_param,
2013                              struct addr_ctrl_blk *init_fw_cb)
2014 {
2015         /*
2016          * iface_num 0 is valid for IPv6 Addr, linklocal, router, autocfg.
2017          * iface_num 1 is valid only for IPv6 Addr.
2018          */
2019         switch (iface_param->param) {
2020         case ISCSI_NET_PARAM_IPV6_ADDR:
2021                 if (iface_param->iface_num & 0x1)
2022                         /* IPv6 Addr 1 */
2023                         memcpy(init_fw_cb->ipv6_addr1, iface_param->value,
2024                                sizeof(init_fw_cb->ipv6_addr1));
2025                 else
2026                         /* IPv6 Addr 0 */
2027                         memcpy(init_fw_cb->ipv6_addr0, iface_param->value,
2028                                sizeof(init_fw_cb->ipv6_addr0));
2029                 break;
2030         case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
2031                 if (iface_param->iface_num & 0x1)
2032                         break;
2033                 memcpy(init_fw_cb->ipv6_if_id, &iface_param->value[8],
2034                        sizeof(init_fw_cb->ipv6_if_id));
2035                 break;
2036         case ISCSI_NET_PARAM_IPV6_ROUTER:
2037                 if (iface_param->iface_num & 0x1)
2038                         break;
2039                 memcpy(init_fw_cb->ipv6_dflt_rtr_addr, iface_param->value,
2040                        sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
2041                 break;
2042         case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
2043                 /* Autocfg applies to even interface */
2044                 if (iface_param->iface_num & 0x1)
2045                         break;
2046
2047                 if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_DISABLE)
2048                         init_fw_cb->ipv6_addtl_opts &=
2049                                 cpu_to_le16(
2050                                   ~IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
2051                 else if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_ND_ENABLE)
2052                         init_fw_cb->ipv6_addtl_opts |=
2053                                 cpu_to_le16(
2054                                   IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
2055                 else
2056                         ql4_printk(KERN_ERR, ha,
2057                                    "Invalid autocfg setting for IPv6 addr\n");
2058                 break;
2059         case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
2060                 /* Autocfg applies to even interface */
2061                 if (iface_param->iface_num & 0x1)
2062                         break;
2063
2064                 if (iface_param->value[0] ==
2065                     ISCSI_IPV6_LINKLOCAL_AUTOCFG_ENABLE)
2066                         init_fw_cb->ipv6_addtl_opts |= cpu_to_le16(
2067                                         IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
2068                 else if (iface_param->value[0] ==
2069                          ISCSI_IPV6_LINKLOCAL_AUTOCFG_DISABLE)
2070                         init_fw_cb->ipv6_addtl_opts &= cpu_to_le16(
2071                                        ~IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
2072                 else
2073                         ql4_printk(KERN_ERR, ha,
2074                                    "Invalid autocfg setting for IPv6 linklocal addr\n");
2075                 break;
2076         case ISCSI_NET_PARAM_IPV6_ROUTER_AUTOCFG:
2077                 /* Autocfg applies to even interface */
2078                 if (iface_param->iface_num & 0x1)
2079                         break;
2080
2081                 if (iface_param->value[0] == ISCSI_IPV6_ROUTER_AUTOCFG_ENABLE)
2082                         memset(init_fw_cb->ipv6_dflt_rtr_addr, 0,
2083                                sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
2084                 break;
2085         case ISCSI_NET_PARAM_IFACE_ENABLE:
2086                 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
2087                         init_fw_cb->ipv6_opts |=
2088                                 cpu_to_le16(IPV6_OPT_IPV6_PROTOCOL_ENABLE);
2089                         qla4xxx_create_ipv6_iface(ha);
2090                 } else {
2091                         init_fw_cb->ipv6_opts &=
2092                                 cpu_to_le16(~IPV6_OPT_IPV6_PROTOCOL_ENABLE &
2093                                             0xFFFF);
2094                         qla4xxx_destroy_ipv6_iface(ha);
2095                 }
2096                 break;
2097         case ISCSI_NET_PARAM_VLAN_TAG:
2098                 if (iface_param->len != sizeof(init_fw_cb->ipv6_vlan_tag))
2099                         break;
2100                 init_fw_cb->ipv6_vlan_tag =
2101                                 cpu_to_be16(*(uint16_t *)iface_param->value);
2102                 break;
2103         case ISCSI_NET_PARAM_VLAN_ENABLED:
2104                 if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
2105                         init_fw_cb->ipv6_opts |=
2106                                 cpu_to_le16(IPV6_OPT_VLAN_TAGGING_ENABLE);
2107                 else
2108                         init_fw_cb->ipv6_opts &=
2109                                 cpu_to_le16(~IPV6_OPT_VLAN_TAGGING_ENABLE);
2110                 break;
2111         case ISCSI_NET_PARAM_MTU:
2112                 init_fw_cb->eth_mtu_size =
2113                                 cpu_to_le16(*(uint16_t *)iface_param->value);
2114                 break;
2115         case ISCSI_NET_PARAM_PORT:
2116                 /* Autocfg applies to even interface */
2117                 if (iface_param->iface_num & 0x1)
2118                         break;
2119
2120                 init_fw_cb->ipv6_port =
2121                                 cpu_to_le16(*(uint16_t *)iface_param->value);
2122                 break;
2123         case ISCSI_NET_PARAM_DELAYED_ACK_EN:
2124                 if (iface_param->iface_num & 0x1)
2125                         break;
2126                 if (iface_param->value[0] == ISCSI_NET_PARAM_DISABLE)
2127                         init_fw_cb->ipv6_tcp_opts |=
2128                                 cpu_to_le16(IPV6_TCPOPT_DELAYED_ACK_DISABLE);
2129                 else
2130                         init_fw_cb->ipv6_tcp_opts &=
2131                                 cpu_to_le16(~IPV6_TCPOPT_DELAYED_ACK_DISABLE &
2132                                             0xFFFF);
2133                 break;
2134         case ISCSI_NET_PARAM_TCP_NAGLE_DISABLE:
2135                 if (iface_param->iface_num & 0x1)
2136                         break;
2137                 if (iface_param->value[0] == ISCSI_NET_PARAM_DISABLE)
2138                         init_fw_cb->ipv6_tcp_opts |=
2139                                 cpu_to_le16(IPV6_TCPOPT_NAGLE_ALGO_DISABLE);
2140                 else
2141                         init_fw_cb->ipv6_tcp_opts &=
2142                                 cpu_to_le16(~IPV6_TCPOPT_NAGLE_ALGO_DISABLE);
2143                 break;
2144         case ISCSI_NET_PARAM_TCP_WSF_DISABLE:
2145                 if (iface_param->iface_num & 0x1)
2146                         break;
2147                 if (iface_param->value[0] == ISCSI_NET_PARAM_DISABLE)
2148                         init_fw_cb->ipv6_tcp_opts |=
2149                                 cpu_to_le16(IPV6_TCPOPT_WINDOW_SCALE_DISABLE);
2150                 else
2151                         init_fw_cb->ipv6_tcp_opts &=
2152                                 cpu_to_le16(~IPV6_TCPOPT_WINDOW_SCALE_DISABLE);
2153                 break;
2154         case ISCSI_NET_PARAM_TCP_WSF:
2155                 if (iface_param->iface_num & 0x1)
2156                         break;
2157                 init_fw_cb->ipv6_tcp_wsf = iface_param->value[0];
2158                 break;
2159         case ISCSI_NET_PARAM_TCP_TIMER_SCALE:
2160                 if (iface_param->iface_num & 0x1)
2161                         break;
2162                 init_fw_cb->ipv6_tcp_opts &=
2163                                         cpu_to_le16(~IPV6_TCPOPT_TIMER_SCALE);
2164                 init_fw_cb->ipv6_tcp_opts |=
2165                                 cpu_to_le16((iface_param->value[0] << 1) &
2166                                             IPV6_TCPOPT_TIMER_SCALE);
2167                 break;
2168         case ISCSI_NET_PARAM_TCP_TIMESTAMP_EN:
2169                 if (iface_param->iface_num & 0x1)
2170                         break;
2171                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2172                         init_fw_cb->ipv6_tcp_opts |=
2173                                 cpu_to_le16(IPV6_TCPOPT_TIMESTAMP_EN);
2174                 else
2175                         init_fw_cb->ipv6_tcp_opts &=
2176                                 cpu_to_le16(~IPV6_TCPOPT_TIMESTAMP_EN);
2177                 break;
2178         case ISCSI_NET_PARAM_IPV6_GRAT_NEIGHBOR_ADV_EN:
2179                 if (iface_param->iface_num & 0x1)
2180                         break;
2181                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2182                         init_fw_cb->ipv6_opts |=
2183                                 cpu_to_le16(IPV6_OPT_GRAT_NEIGHBOR_ADV_EN);
2184                 else
2185                         init_fw_cb->ipv6_opts &=
2186                                 cpu_to_le16(~IPV6_OPT_GRAT_NEIGHBOR_ADV_EN);
2187                 break;
2188         case ISCSI_NET_PARAM_REDIRECT_EN:
2189                 if (iface_param->iface_num & 0x1)
2190                         break;
2191                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2192                         init_fw_cb->ipv6_opts |=
2193                                 cpu_to_le16(IPV6_OPT_REDIRECT_EN);
2194                 else
2195                         init_fw_cb->ipv6_opts &=
2196                                 cpu_to_le16(~IPV6_OPT_REDIRECT_EN);
2197                 break;
2198         case ISCSI_NET_PARAM_IPV6_MLD_EN:
2199                 if (iface_param->iface_num & 0x1)
2200                         break;
2201                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2202                         init_fw_cb->ipv6_addtl_opts |=
2203                                 cpu_to_le16(IPV6_ADDOPT_MLD_EN);
2204                 else
2205                         init_fw_cb->ipv6_addtl_opts &=
2206                                 cpu_to_le16(~IPV6_ADDOPT_MLD_EN);
2207                 break;
2208         case ISCSI_NET_PARAM_IPV6_FLOW_LABEL:
2209                 if (iface_param->iface_num & 0x1)
2210                         break;
2211                 init_fw_cb->ipv6_flow_lbl =
2212                                 cpu_to_le16(*(uint16_t *)iface_param->value);
2213                 break;
2214         case ISCSI_NET_PARAM_IPV6_TRAFFIC_CLASS:
2215                 if (iface_param->iface_num & 0x1)
2216                         break;
2217                 init_fw_cb->ipv6_traffic_class = iface_param->value[0];
2218                 break;
2219         case ISCSI_NET_PARAM_IPV6_HOP_LIMIT:
2220                 if (iface_param->iface_num & 0x1)
2221                         break;
2222                 init_fw_cb->ipv6_hop_limit = iface_param->value[0];
2223                 break;
2224         case ISCSI_NET_PARAM_IPV6_ND_REACHABLE_TMO:
2225                 if (iface_param->iface_num & 0x1)
2226                         break;
2227                 init_fw_cb->ipv6_nd_reach_time =
2228                                 cpu_to_le32(*(uint32_t *)iface_param->value);
2229                 break;
2230         case ISCSI_NET_PARAM_IPV6_ND_REXMIT_TIME:
2231                 if (iface_param->iface_num & 0x1)
2232                         break;
2233                 init_fw_cb->ipv6_nd_rexmit_timer =
2234                                 cpu_to_le32(*(uint32_t *)iface_param->value);
2235                 break;
2236         case ISCSI_NET_PARAM_IPV6_ND_STALE_TMO:
2237                 if (iface_param->iface_num & 0x1)
2238                         break;
2239                 init_fw_cb->ipv6_nd_stale_timeout =
2240                                 cpu_to_le32(*(uint32_t *)iface_param->value);
2241                 break;
2242         case ISCSI_NET_PARAM_IPV6_DUP_ADDR_DETECT_CNT:
2243                 if (iface_param->iface_num & 0x1)
2244                         break;
2245                 init_fw_cb->ipv6_dup_addr_detect_count = iface_param->value[0];
2246                 break;
2247         case ISCSI_NET_PARAM_IPV6_RTR_ADV_LINK_MTU:
2248                 if (iface_param->iface_num & 0x1)
2249                         break;
2250                 init_fw_cb->ipv6_gw_advrt_mtu =
2251                                 cpu_to_le32(*(uint32_t *)iface_param->value);
2252                 break;
2253         default:
2254                 ql4_printk(KERN_ERR, ha, "Unknown IPv6 param = %d\n",
2255                            iface_param->param);
2256                 break;
2257         }
2258 }
2259
2260 static void qla4xxx_set_ipv4(struct scsi_qla_host *ha,
2261                              struct iscsi_iface_param_info *iface_param,
2262                              struct addr_ctrl_blk *init_fw_cb)
2263 {
2264         switch (iface_param->param) {
2265         case ISCSI_NET_PARAM_IPV4_ADDR:
2266                 memcpy(init_fw_cb->ipv4_addr, iface_param->value,
2267                        sizeof(init_fw_cb->ipv4_addr));
2268                 break;
2269         case ISCSI_NET_PARAM_IPV4_SUBNET:
2270                 memcpy(init_fw_cb->ipv4_subnet, iface_param->value,
2271                        sizeof(init_fw_cb->ipv4_subnet));
2272                 break;
2273         case ISCSI_NET_PARAM_IPV4_GW:
2274                 memcpy(init_fw_cb->ipv4_gw_addr, iface_param->value,
2275                        sizeof(init_fw_cb->ipv4_gw_addr));
2276                 break;
2277         case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
2278                 if (iface_param->value[0] == ISCSI_BOOTPROTO_DHCP)
2279                         init_fw_cb->ipv4_tcp_opts |=
2280                                         cpu_to_le16(TCPOPT_DHCP_ENABLE);
2281                 else if (iface_param->value[0] == ISCSI_BOOTPROTO_STATIC)
2282                         init_fw_cb->ipv4_tcp_opts &=
2283                                         cpu_to_le16(~TCPOPT_DHCP_ENABLE);
2284                 else
2285                         ql4_printk(KERN_ERR, ha, "Invalid IPv4 bootproto\n");
2286                 break;
2287         case ISCSI_NET_PARAM_IFACE_ENABLE:
2288                 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
2289                         init_fw_cb->ipv4_ip_opts |=
2290                                 cpu_to_le16(IPOPT_IPV4_PROTOCOL_ENABLE);
2291                         qla4xxx_create_ipv4_iface(ha);
2292                 } else {
2293                         init_fw_cb->ipv4_ip_opts &=
2294                                 cpu_to_le16(~IPOPT_IPV4_PROTOCOL_ENABLE &
2295                                             0xFFFF);
2296                         qla4xxx_destroy_ipv4_iface(ha);
2297                 }
2298                 break;
2299         case ISCSI_NET_PARAM_VLAN_TAG:
2300                 if (iface_param->len != sizeof(init_fw_cb->ipv4_vlan_tag))
2301                         break;
2302                 init_fw_cb->ipv4_vlan_tag =
2303                                 cpu_to_be16(*(uint16_t *)iface_param->value);
2304                 break;
2305         case ISCSI_NET_PARAM_VLAN_ENABLED:
2306                 if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
2307                         init_fw_cb->ipv4_ip_opts |=
2308                                         cpu_to_le16(IPOPT_VLAN_TAGGING_ENABLE);
2309                 else
2310                         init_fw_cb->ipv4_ip_opts &=
2311                                         cpu_to_le16(~IPOPT_VLAN_TAGGING_ENABLE);
2312                 break;
2313         case ISCSI_NET_PARAM_MTU:
2314                 init_fw_cb->eth_mtu_size =
2315                                 cpu_to_le16(*(uint16_t *)iface_param->value);
2316                 break;
2317         case ISCSI_NET_PARAM_PORT:
2318                 init_fw_cb->ipv4_port =
2319                                 cpu_to_le16(*(uint16_t *)iface_param->value);
2320                 break;
2321         case ISCSI_NET_PARAM_DELAYED_ACK_EN:
2322                 if (iface_param->iface_num & 0x1)
2323                         break;
2324                 if (iface_param->value[0] == ISCSI_NET_PARAM_DISABLE)
2325                         init_fw_cb->ipv4_tcp_opts |=
2326                                 cpu_to_le16(TCPOPT_DELAYED_ACK_DISABLE);
2327                 else
2328                         init_fw_cb->ipv4_tcp_opts &=
2329                                 cpu_to_le16(~TCPOPT_DELAYED_ACK_DISABLE &
2330                                             0xFFFF);
2331                 break;
2332         case ISCSI_NET_PARAM_TCP_NAGLE_DISABLE:
2333                 if (iface_param->iface_num & 0x1)
2334                         break;
2335                 if (iface_param->value[0] == ISCSI_NET_PARAM_DISABLE)
2336                         init_fw_cb->ipv4_tcp_opts |=
2337                                 cpu_to_le16(TCPOPT_NAGLE_ALGO_DISABLE);
2338                 else
2339                         init_fw_cb->ipv4_tcp_opts &=
2340                                 cpu_to_le16(~TCPOPT_NAGLE_ALGO_DISABLE);
2341                 break;
2342         case ISCSI_NET_PARAM_TCP_WSF_DISABLE:
2343                 if (iface_param->iface_num & 0x1)
2344                         break;
2345                 if (iface_param->value[0] == ISCSI_NET_PARAM_DISABLE)
2346                         init_fw_cb->ipv4_tcp_opts |=
2347                                 cpu_to_le16(TCPOPT_WINDOW_SCALE_DISABLE);
2348                 else
2349                         init_fw_cb->ipv4_tcp_opts &=
2350                                 cpu_to_le16(~TCPOPT_WINDOW_SCALE_DISABLE);
2351                 break;
2352         case ISCSI_NET_PARAM_TCP_WSF:
2353                 if (iface_param->iface_num & 0x1)
2354                         break;
2355                 init_fw_cb->ipv4_tcp_wsf = iface_param->value[0];
2356                 break;
2357         case ISCSI_NET_PARAM_TCP_TIMER_SCALE:
2358                 if (iface_param->iface_num & 0x1)
2359                         break;
2360                 init_fw_cb->ipv4_tcp_opts &= cpu_to_le16(~TCPOPT_TIMER_SCALE);
2361                 init_fw_cb->ipv4_tcp_opts |=
2362                                 cpu_to_le16((iface_param->value[0] << 1) &
2363                                             TCPOPT_TIMER_SCALE);
2364                 break;
2365         case ISCSI_NET_PARAM_TCP_TIMESTAMP_EN:
2366                 if (iface_param->iface_num & 0x1)
2367                         break;
2368                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2369                         init_fw_cb->ipv4_tcp_opts |=
2370                                 cpu_to_le16(TCPOPT_TIMESTAMP_ENABLE);
2371                 else
2372                         init_fw_cb->ipv4_tcp_opts &=
2373                                 cpu_to_le16(~TCPOPT_TIMESTAMP_ENABLE);
2374                 break;
2375         case ISCSI_NET_PARAM_IPV4_DHCP_DNS_ADDR_EN:
2376                 if (iface_param->iface_num & 0x1)
2377                         break;
2378                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2379                         init_fw_cb->ipv4_tcp_opts |=
2380                                 cpu_to_le16(TCPOPT_DNS_SERVER_IP_EN);
2381                 else
2382                         init_fw_cb->ipv4_tcp_opts &=
2383                                 cpu_to_le16(~TCPOPT_DNS_SERVER_IP_EN);
2384                 break;
2385         case ISCSI_NET_PARAM_IPV4_DHCP_SLP_DA_EN:
2386                 if (iface_param->iface_num & 0x1)
2387                         break;
2388                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2389                         init_fw_cb->ipv4_tcp_opts |=
2390                                 cpu_to_le16(TCPOPT_SLP_DA_INFO_EN);
2391                 else
2392                         init_fw_cb->ipv4_tcp_opts &=
2393                                 cpu_to_le16(~TCPOPT_SLP_DA_INFO_EN);
2394                 break;
2395         case ISCSI_NET_PARAM_IPV4_TOS_EN:
2396                 if (iface_param->iface_num & 0x1)
2397                         break;
2398                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2399                         init_fw_cb->ipv4_ip_opts |=
2400                                 cpu_to_le16(IPOPT_IPV4_TOS_EN);
2401                 else
2402                         init_fw_cb->ipv4_ip_opts &=
2403                                 cpu_to_le16(~IPOPT_IPV4_TOS_EN);
2404                 break;
2405         case ISCSI_NET_PARAM_IPV4_TOS:
2406                 if (iface_param->iface_num & 0x1)
2407                         break;
2408                 init_fw_cb->ipv4_tos = iface_param->value[0];
2409                 break;
2410         case ISCSI_NET_PARAM_IPV4_GRAT_ARP_EN:
2411                 if (iface_param->iface_num & 0x1)
2412                         break;
2413                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2414                         init_fw_cb->ipv4_ip_opts |=
2415                                         cpu_to_le16(IPOPT_GRAT_ARP_EN);
2416                 else
2417                         init_fw_cb->ipv4_ip_opts &=
2418                                         cpu_to_le16(~IPOPT_GRAT_ARP_EN);
2419                 break;
2420         case ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID_EN:
2421                 if (iface_param->iface_num & 0x1)
2422                         break;
2423                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2424                         init_fw_cb->ipv4_ip_opts |=
2425                                 cpu_to_le16(IPOPT_ALT_CID_EN);
2426                 else
2427                         init_fw_cb->ipv4_ip_opts &=
2428                                 cpu_to_le16(~IPOPT_ALT_CID_EN);
2429                 break;
2430         case ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID:
2431                 if (iface_param->iface_num & 0x1)
2432                         break;
2433                 memcpy(init_fw_cb->ipv4_dhcp_alt_cid, iface_param->value,
2434                        (sizeof(init_fw_cb->ipv4_dhcp_alt_cid) - 1));
2435                 init_fw_cb->ipv4_dhcp_alt_cid_len =
2436                                         strlen(init_fw_cb->ipv4_dhcp_alt_cid);
2437                 break;
2438         case ISCSI_NET_PARAM_IPV4_DHCP_REQ_VENDOR_ID_EN:
2439                 if (iface_param->iface_num & 0x1)
2440                         break;
2441                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2442                         init_fw_cb->ipv4_ip_opts |=
2443                                         cpu_to_le16(IPOPT_REQ_VID_EN);
2444                 else
2445                         init_fw_cb->ipv4_ip_opts &=
2446                                         cpu_to_le16(~IPOPT_REQ_VID_EN);
2447                 break;
2448         case ISCSI_NET_PARAM_IPV4_DHCP_USE_VENDOR_ID_EN:
2449                 if (iface_param->iface_num & 0x1)
2450                         break;
2451                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2452                         init_fw_cb->ipv4_ip_opts |=
2453                                         cpu_to_le16(IPOPT_USE_VID_EN);
2454                 else
2455                         init_fw_cb->ipv4_ip_opts &=
2456                                         cpu_to_le16(~IPOPT_USE_VID_EN);
2457                 break;
2458         case ISCSI_NET_PARAM_IPV4_DHCP_VENDOR_ID:
2459                 if (iface_param->iface_num & 0x1)
2460                         break;
2461                 memcpy(init_fw_cb->ipv4_dhcp_vid, iface_param->value,
2462                        (sizeof(init_fw_cb->ipv4_dhcp_vid) - 1));
2463                 init_fw_cb->ipv4_dhcp_vid_len =
2464                                         strlen(init_fw_cb->ipv4_dhcp_vid);
2465                 break;
2466         case ISCSI_NET_PARAM_IPV4_DHCP_LEARN_IQN_EN:
2467                 if (iface_param->iface_num & 0x1)
2468                         break;
2469                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2470                         init_fw_cb->ipv4_ip_opts |=
2471                                         cpu_to_le16(IPOPT_LEARN_IQN_EN);
2472                 else
2473                         init_fw_cb->ipv4_ip_opts &=
2474                                         cpu_to_le16(~IPOPT_LEARN_IQN_EN);
2475                 break;
2476         case ISCSI_NET_PARAM_IPV4_FRAGMENT_DISABLE:
2477                 if (iface_param->iface_num & 0x1)
2478                         break;
2479                 if (iface_param->value[0] == ISCSI_NET_PARAM_DISABLE)
2480                         init_fw_cb->ipv4_ip_opts |=
2481                                 cpu_to_le16(IPOPT_FRAGMENTATION_DISABLE);
2482                 else
2483                         init_fw_cb->ipv4_ip_opts &=
2484                                 cpu_to_le16(~IPOPT_FRAGMENTATION_DISABLE);
2485                 break;
2486         case ISCSI_NET_PARAM_IPV4_IN_FORWARD_EN:
2487                 if (iface_param->iface_num & 0x1)
2488                         break;
2489                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2490                         init_fw_cb->ipv4_ip_opts |=
2491                                 cpu_to_le16(IPOPT_IN_FORWARD_EN);
2492                 else
2493                         init_fw_cb->ipv4_ip_opts &=
2494                                 cpu_to_le16(~IPOPT_IN_FORWARD_EN);
2495                 break;
2496         case ISCSI_NET_PARAM_REDIRECT_EN:
2497                 if (iface_param->iface_num & 0x1)
2498                         break;
2499                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2500                         init_fw_cb->ipv4_ip_opts |=
2501                                 cpu_to_le16(IPOPT_ARP_REDIRECT_EN);
2502                 else
2503                         init_fw_cb->ipv4_ip_opts &=
2504                                 cpu_to_le16(~IPOPT_ARP_REDIRECT_EN);
2505                 break;
2506         case ISCSI_NET_PARAM_IPV4_TTL:
2507                 if (iface_param->iface_num & 0x1)
2508                         break;
2509                 init_fw_cb->ipv4_ttl = iface_param->value[0];
2510                 break;
2511         default:
2512                 ql4_printk(KERN_ERR, ha, "Unknown IPv4 param = %d\n",
2513                            iface_param->param);
2514                 break;
2515         }
2516 }
2517
2518 static void qla4xxx_set_iscsi_param(struct scsi_qla_host *ha,
2519                                     struct iscsi_iface_param_info *iface_param,
2520                                     struct addr_ctrl_blk *init_fw_cb)
2521 {
2522         switch (iface_param->param) {
2523         case ISCSI_IFACE_PARAM_DEF_TASKMGMT_TMO:
2524                 if (iface_param->iface_num & 0x1)
2525                         break;
2526                 init_fw_cb->def_timeout =
2527                                 cpu_to_le16(*(uint16_t *)iface_param->value);
2528                 break;
2529         case ISCSI_IFACE_PARAM_HDRDGST_EN:
2530                 if (iface_param->iface_num & 0x1)
2531                         break;
2532                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2533                         init_fw_cb->iscsi_opts |=
2534                                 cpu_to_le16(ISCSIOPTS_HEADER_DIGEST_EN);
2535                 else
2536                         init_fw_cb->iscsi_opts &=
2537                                 cpu_to_le16(~ISCSIOPTS_HEADER_DIGEST_EN);
2538                 break;
2539         case ISCSI_IFACE_PARAM_DATADGST_EN:
2540                 if (iface_param->iface_num & 0x1)
2541                         break;
2542                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2543                         init_fw_cb->iscsi_opts |=
2544                                 cpu_to_le16(ISCSIOPTS_DATA_DIGEST_EN);
2545                 else
2546                         init_fw_cb->iscsi_opts &=
2547                                 cpu_to_le16(~ISCSIOPTS_DATA_DIGEST_EN);
2548                 break;
2549         case ISCSI_IFACE_PARAM_IMM_DATA_EN:
2550                 if (iface_param->iface_num & 0x1)
2551                         break;
2552                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2553                         init_fw_cb->iscsi_opts |=
2554                                 cpu_to_le16(ISCSIOPTS_IMMEDIATE_DATA_EN);
2555                 else
2556                         init_fw_cb->iscsi_opts &=
2557                                 cpu_to_le16(~ISCSIOPTS_IMMEDIATE_DATA_EN);
2558                 break;
2559         case ISCSI_IFACE_PARAM_INITIAL_R2T_EN:
2560                 if (iface_param->iface_num & 0x1)
2561                         break;
2562                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2563                         init_fw_cb->iscsi_opts |=
2564                                 cpu_to_le16(ISCSIOPTS_INITIAL_R2T_EN);
2565                 else
2566                         init_fw_cb->iscsi_opts &=
2567                                 cpu_to_le16(~ISCSIOPTS_INITIAL_R2T_EN);
2568                 break;
2569         case ISCSI_IFACE_PARAM_DATASEQ_INORDER_EN:
2570                 if (iface_param->iface_num & 0x1)
2571                         break;
2572                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2573                         init_fw_cb->iscsi_opts |=
2574                                 cpu_to_le16(ISCSIOPTS_DATA_SEQ_INORDER_EN);
2575                 else
2576                         init_fw_cb->iscsi_opts &=
2577                                 cpu_to_le16(~ISCSIOPTS_DATA_SEQ_INORDER_EN);
2578                 break;
2579         case ISCSI_IFACE_PARAM_PDU_INORDER_EN:
2580                 if (iface_param->iface_num & 0x1)
2581                         break;
2582                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2583                         init_fw_cb->iscsi_opts |=
2584                                 cpu_to_le16(ISCSIOPTS_DATA_PDU_INORDER_EN);
2585                 else
2586                         init_fw_cb->iscsi_opts &=
2587                                 cpu_to_le16(~ISCSIOPTS_DATA_PDU_INORDER_EN);
2588                 break;
2589         case ISCSI_IFACE_PARAM_ERL:
2590                 if (iface_param->iface_num & 0x1)
2591                         break;
2592                 init_fw_cb->iscsi_opts &= cpu_to_le16(~ISCSIOPTS_ERL);
2593                 init_fw_cb->iscsi_opts |= cpu_to_le16(iface_param->value[0] &
2594                                                       ISCSIOPTS_ERL);
2595                 break;
2596         case ISCSI_IFACE_PARAM_MAX_RECV_DLENGTH:
2597                 if (iface_param->iface_num & 0x1)
2598                         break;
2599                 init_fw_cb->iscsi_max_pdu_size =
2600                                 cpu_to_le32(*(uint32_t *)iface_param->value) /
2601                                 BYTE_UNITS;
2602                 break;
2603         case ISCSI_IFACE_PARAM_FIRST_BURST:
2604                 if (iface_param->iface_num & 0x1)
2605                         break;
2606                 init_fw_cb->iscsi_fburst_len =
2607                                 cpu_to_le32(*(uint32_t *)iface_param->value) /
2608                                 BYTE_UNITS;
2609                 break;
2610         case ISCSI_IFACE_PARAM_MAX_R2T:
2611                 if (iface_param->iface_num & 0x1)
2612                         break;
2613                 init_fw_cb->iscsi_max_outstnd_r2t =
2614                                 cpu_to_le16(*(uint16_t *)iface_param->value);
2615                 break;
2616         case ISCSI_IFACE_PARAM_MAX_BURST:
2617                 if (iface_param->iface_num & 0x1)
2618                         break;
2619                 init_fw_cb->iscsi_max_burst_len =
2620                                 cpu_to_le32(*(uint32_t *)iface_param->value) /
2621                                 BYTE_UNITS;
2622                 break;
2623         case ISCSI_IFACE_PARAM_CHAP_AUTH_EN:
2624                 if (iface_param->iface_num & 0x1)
2625                         break;
2626                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2627                         init_fw_cb->iscsi_opts |=
2628                                 cpu_to_le16(ISCSIOPTS_CHAP_AUTH_EN);
2629                 else
2630                         init_fw_cb->iscsi_opts &=
2631                                 cpu_to_le16(~ISCSIOPTS_CHAP_AUTH_EN);
2632                 break;
2633         case ISCSI_IFACE_PARAM_BIDI_CHAP_EN:
2634                 if (iface_param->iface_num & 0x1)
2635                         break;
2636                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2637                         init_fw_cb->iscsi_opts |=
2638                                 cpu_to_le16(ISCSIOPTS_BIDI_CHAP_EN);
2639                 else
2640                         init_fw_cb->iscsi_opts &=
2641                                 cpu_to_le16(~ISCSIOPTS_BIDI_CHAP_EN);
2642                 break;
2643         case ISCSI_IFACE_PARAM_DISCOVERY_AUTH_OPTIONAL:
2644                 if (iface_param->iface_num & 0x1)
2645                         break;
2646                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2647                         init_fw_cb->iscsi_opts |=
2648                                 cpu_to_le16(ISCSIOPTS_DISCOVERY_AUTH_EN);
2649                 else
2650                         init_fw_cb->iscsi_opts &=
2651                                 cpu_to_le16(~ISCSIOPTS_DISCOVERY_AUTH_EN);
2652                 break;
2653         case ISCSI_IFACE_PARAM_DISCOVERY_LOGOUT_EN:
2654                 if (iface_param->iface_num & 0x1)
2655                         break;
2656                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2657                         init_fw_cb->iscsi_opts |=
2658                                 cpu_to_le16(ISCSIOPTS_DISCOVERY_LOGOUT_EN);
2659                 else
2660                         init_fw_cb->iscsi_opts &=
2661                                 cpu_to_le16(~ISCSIOPTS_DISCOVERY_LOGOUT_EN);
2662                 break;
2663         case ISCSI_IFACE_PARAM_STRICT_LOGIN_COMP_EN:
2664                 if (iface_param->iface_num & 0x1)
2665                         break;
2666                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2667                         init_fw_cb->iscsi_opts |=
2668                                 cpu_to_le16(ISCSIOPTS_STRICT_LOGIN_COMP_EN);
2669                 else
2670                         init_fw_cb->iscsi_opts &=
2671                                 cpu_to_le16(~ISCSIOPTS_STRICT_LOGIN_COMP_EN);
2672                 break;
2673         default:
2674                 ql4_printk(KERN_ERR, ha, "Unknown iscsi param = %d\n",
2675                            iface_param->param);
2676                 break;
2677         }
2678 }
2679
2680 static void
2681 qla4xxx_initcb_to_acb(struct addr_ctrl_blk *init_fw_cb)
2682 {
2683         struct addr_ctrl_blk_def *acb;
2684         acb = (struct addr_ctrl_blk_def *)init_fw_cb;
2685         memset(acb->reserved1, 0, sizeof(acb->reserved1));
2686         memset(acb->reserved2, 0, sizeof(acb->reserved2));
2687         memset(acb->reserved3, 0, sizeof(acb->reserved3));
2688         memset(acb->reserved4, 0, sizeof(acb->reserved4));
2689         memset(acb->reserved5, 0, sizeof(acb->reserved5));
2690         memset(acb->reserved6, 0, sizeof(acb->reserved6));
2691         memset(acb->reserved7, 0, sizeof(acb->reserved7));
2692         memset(acb->reserved8, 0, sizeof(acb->reserved8));
2693         memset(acb->reserved9, 0, sizeof(acb->reserved9));
2694         memset(acb->reserved10, 0, sizeof(acb->reserved10));
2695         memset(acb->reserved11, 0, sizeof(acb->reserved11));
2696         memset(acb->reserved12, 0, sizeof(acb->reserved12));
2697         memset(acb->reserved13, 0, sizeof(acb->reserved13));
2698         memset(acb->reserved14, 0, sizeof(acb->reserved14));
2699         memset(acb->reserved15, 0, sizeof(acb->reserved15));
2700 }
2701
2702 static int
2703 qla4xxx_iface_set_param(struct Scsi_Host *shost, void *data, uint32_t len)
2704 {
2705         struct scsi_qla_host *ha = to_qla_host(shost);
2706         int rval = 0;
2707         struct iscsi_iface_param_info *iface_param = NULL;
2708         struct addr_ctrl_blk *init_fw_cb = NULL;
2709         dma_addr_t init_fw_cb_dma;
2710         uint32_t mbox_cmd[MBOX_REG_COUNT];
2711         uint32_t mbox_sts[MBOX_REG_COUNT];
2712         uint32_t rem = len;
2713         struct nlattr *attr;
2714
2715         init_fw_cb = dma_alloc_coherent(&ha->pdev->dev,
2716                                         sizeof(struct addr_ctrl_blk),
2717                                         &init_fw_cb_dma, GFP_KERNEL);
2718         if (!init_fw_cb) {
2719                 ql4_printk(KERN_ERR, ha, "%s: Unable to alloc init_cb\n",
2720                            __func__);
2721                 return -ENOMEM;
2722         }
2723
2724         memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
2725         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
2726         memset(&mbox_sts, 0, sizeof(mbox_sts));
2727
2728         if (qla4xxx_get_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma)) {
2729                 ql4_printk(KERN_ERR, ha, "%s: get ifcb failed\n", __func__);
2730                 rval = -EIO;
2731                 goto exit_init_fw_cb;
2732         }
2733
2734         nla_for_each_attr(attr, data, len, rem) {
2735                 if (nla_len(attr) < sizeof(*iface_param)) {
2736                         rval = -EINVAL;
2737                         goto exit_init_fw_cb;
2738                 }
2739
2740                 iface_param = nla_data(attr);
2741
2742                 if (iface_param->param_type == ISCSI_NET_PARAM) {
2743                         switch (iface_param->iface_type) {
2744                         case ISCSI_IFACE_TYPE_IPV4:
2745                                 switch (iface_param->iface_num) {
2746                                 case 0:
2747                                         qla4xxx_set_ipv4(ha, iface_param,
2748                                                          init_fw_cb);
2749                                         break;
2750                                 default:
2751                                 /* Cannot have more than one IPv4 interface */
2752                                         ql4_printk(KERN_ERR, ha,
2753                                                    "Invalid IPv4 iface number = %d\n",
2754                                                    iface_param->iface_num);
2755                                         break;
2756                                 }
2757                                 break;
2758                         case ISCSI_IFACE_TYPE_IPV6:
2759                                 switch (iface_param->iface_num) {
2760                                 case 0:
2761                                 case 1:
2762                                         qla4xxx_set_ipv6(ha, iface_param,
2763                                                          init_fw_cb);
2764                                         break;
2765                                 default:
2766                                 /* Cannot have more than two IPv6 interface */
2767                                         ql4_printk(KERN_ERR, ha,
2768                                                    "Invalid IPv6 iface number = %d\n",
2769                                                    iface_param->iface_num);
2770                                         break;
2771                                 }
2772                                 break;
2773                         default:
2774                                 ql4_printk(KERN_ERR, ha,
2775                                            "Invalid iface type\n");
2776                                 break;
2777                         }
2778                 } else if (iface_param->param_type == ISCSI_IFACE_PARAM) {
2779                                 qla4xxx_set_iscsi_param(ha, iface_param,
2780                                                         init_fw_cb);
2781                 } else {
2782                         continue;
2783                 }
2784         }
2785
2786         init_fw_cb->cookie = cpu_to_le32(0x11BEAD5A);
2787
2788         rval = qla4xxx_set_flash(ha, init_fw_cb_dma, FLASH_SEGMENT_IFCB,
2789                                  sizeof(struct addr_ctrl_blk),
2790                                  FLASH_OPT_RMW_COMMIT);
2791         if (rval != QLA_SUCCESS) {
2792                 ql4_printk(KERN_ERR, ha, "%s: set flash mbx failed\n",
2793                            __func__);
2794                 rval = -EIO;
2795                 goto exit_init_fw_cb;
2796         }
2797
2798         rval = qla4xxx_disable_acb(ha);
2799         if (rval != QLA_SUCCESS) {
2800                 ql4_printk(KERN_ERR, ha, "%s: disable acb mbx failed\n",
2801                            __func__);
2802                 rval = -EIO;
2803                 goto exit_init_fw_cb;
2804         }
2805
2806         wait_for_completion_timeout(&ha->disable_acb_comp,
2807                                     DISABLE_ACB_TOV * HZ);
2808
2809         qla4xxx_initcb_to_acb(init_fw_cb);
2810
2811         rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma);
2812         if (rval != QLA_SUCCESS) {
2813                 ql4_printk(KERN_ERR, ha, "%s: set acb mbx failed\n",
2814                            __func__);
2815                 rval = -EIO;
2816                 goto exit_init_fw_cb;
2817         }
2818
2819         memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
2820         qla4xxx_update_local_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb,
2821                                   init_fw_cb_dma);
2822
2823 exit_init_fw_cb:
2824         dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk),
2825                           init_fw_cb, init_fw_cb_dma);
2826
2827         return rval;
2828 }
2829
2830 static int qla4xxx_session_get_param(struct iscsi_cls_session *cls_sess,
2831                                      enum iscsi_param param, char *buf)
2832 {
2833         struct iscsi_session *sess = cls_sess->dd_data;
2834         struct ddb_entry *ddb_entry = sess->dd_data;
2835         struct scsi_qla_host *ha = ddb_entry->ha;
2836         struct iscsi_cls_conn *cls_conn = ddb_entry->conn;
2837         struct ql4_chap_table chap_tbl;
2838         int rval, len;
2839         uint16_t idx;
2840
2841         memset(&chap_tbl, 0, sizeof(chap_tbl));
2842         switch (param) {
2843         case ISCSI_PARAM_CHAP_IN_IDX:
2844                 rval = qla4xxx_get_chap_index(ha, sess->username_in,
2845                                               sess->password_in, BIDI_CHAP,
2846                                               &idx);
2847                 if (rval)
2848                         len = sprintf(buf, "\n");
2849                 else
2850                         len = sprintf(buf, "%hu\n", idx);
2851                 break;
2852         case ISCSI_PARAM_CHAP_OUT_IDX:
2853                 if (ddb_entry->ddb_type == FLASH_DDB) {
2854                         if (ddb_entry->chap_tbl_idx != INVALID_ENTRY) {
2855                                 idx = ddb_entry->chap_tbl_idx;
2856                                 rval = QLA_SUCCESS;
2857                         } else {
2858                                 rval = QLA_ERROR;
2859                         }
2860                 } else {
2861                         rval = qla4xxx_get_chap_index(ha, sess->username,
2862                                                       sess->password,
2863                                                       LOCAL_CHAP, &idx);
2864                 }
2865                 if (rval)
2866                         len = sprintf(buf, "\n");
2867                 else
2868                         len = sprintf(buf, "%hu\n", idx);
2869                 break;
2870         case ISCSI_PARAM_USERNAME:
2871         case ISCSI_PARAM_PASSWORD:
2872                 /* First, populate session username and password for FLASH DDB,
2873                  * if not already done. This happens when session login fails
2874                  * for a FLASH DDB.
2875                  */
2876                 if (ddb_entry->ddb_type == FLASH_DDB &&
2877                     ddb_entry->chap_tbl_idx != INVALID_ENTRY &&
2878                     !sess->username && !sess->password) {
2879                         idx = ddb_entry->chap_tbl_idx;
2880                         rval = qla4xxx_get_uni_chap_at_index(ha, chap_tbl.name,
2881                                                             chap_tbl.secret,
2882                                                             idx);
2883                         if (!rval) {
2884                                 iscsi_set_param(cls_conn, ISCSI_PARAM_USERNAME,
2885                                                 (char *)chap_tbl.name,
2886                                                 strlen((char *)chap_tbl.name));
2887                                 iscsi_set_param(cls_conn, ISCSI_PARAM_PASSWORD,
2888                                                 (char *)chap_tbl.secret,
2889                                                 chap_tbl.secret_len);
2890                         }
2891                 }
2892                 /* allow fall-through */
2893         default:
2894                 return iscsi_session_get_param(cls_sess, param, buf);
2895         }
2896
2897         return len;
2898 }
2899
2900 static int qla4xxx_conn_get_param(struct iscsi_cls_conn *cls_conn,
2901                                   enum iscsi_param param, char *buf)
2902 {
2903         struct iscsi_conn *conn;
2904         struct qla_conn *qla_conn;
2905         struct sockaddr *dst_addr;
2906
2907         conn = cls_conn->dd_data;
2908         qla_conn = conn->dd_data;
2909         dst_addr = (struct sockaddr *)&qla_conn->qla_ep->dst_addr;
2910
2911         switch (param) {
2912         case ISCSI_PARAM_CONN_PORT:
2913         case ISCSI_PARAM_CONN_ADDRESS:
2914                 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
2915                                                  dst_addr, param, buf);
2916         default:
2917                 return iscsi_conn_get_param(cls_conn, param, buf);
2918         }
2919 }
2920
2921 int qla4xxx_get_ddb_index(struct scsi_qla_host *ha, uint16_t *ddb_index)
2922 {
2923         uint32_t mbx_sts = 0;
2924         uint16_t tmp_ddb_index;
2925         int ret;
2926
2927 get_ddb_index:
2928         tmp_ddb_index = find_first_zero_bit(ha->ddb_idx_map, MAX_DDB_ENTRIES);
2929
2930         if (tmp_ddb_index >= MAX_DDB_ENTRIES) {
2931                 DEBUG2(ql4_printk(KERN_INFO, ha,
2932                                   "Free DDB index not available\n"));
2933                 ret = QLA_ERROR;
2934                 goto exit_get_ddb_index;
2935         }
2936
2937         if (test_and_set_bit(tmp_ddb_index, ha->ddb_idx_map))
2938                 goto get_ddb_index;
2939
2940         DEBUG2(ql4_printk(KERN_INFO, ha,
2941                           "Found a free DDB index at %d\n", tmp_ddb_index));
2942         ret = qla4xxx_req_ddb_entry(ha, tmp_ddb_index, &mbx_sts);
2943         if (ret == QLA_ERROR) {
2944                 if (mbx_sts == MBOX_STS_COMMAND_ERROR) {
2945                         ql4_printk(KERN_INFO, ha,
2946                                    "DDB index = %d not available trying next\n",
2947                                    tmp_ddb_index);
2948                         goto get_ddb_index;
2949                 }
2950                 DEBUG2(ql4_printk(KERN_INFO, ha,
2951                                   "Free FW DDB not available\n"));
2952         }
2953
2954         *ddb_index = tmp_ddb_index;
2955
2956 exit_get_ddb_index:
2957         return ret;
2958 }
2959
2960 static int qla4xxx_match_ipaddress(struct scsi_qla_host *ha,
2961                                    struct ddb_entry *ddb_entry,
2962                                    char *existing_ipaddr,
2963                                    char *user_ipaddr)
2964 {
2965         uint8_t dst_ipaddr[IPv6_ADDR_LEN];
2966         char formatted_ipaddr[DDB_IPADDR_LEN];
2967         int status = QLA_SUCCESS, ret = 0;
2968
2969         if (ddb_entry->fw_ddb_entry.options & DDB_OPT_IPV6_DEVICE) {
2970                 ret = in6_pton(user_ipaddr, strlen(user_ipaddr), dst_ipaddr,
2971                                '\0', NULL);
2972                 if (ret == 0) {
2973                         status = QLA_ERROR;
2974                         goto out_match;
2975                 }
2976                 ret = sprintf(formatted_ipaddr, "%pI6", dst_ipaddr);
2977         } else {
2978                 ret = in4_pton(user_ipaddr, strlen(user_ipaddr), dst_ipaddr,
2979                                '\0', NULL);
2980                 if (ret == 0) {
2981                         status = QLA_ERROR;
2982                         goto out_match;
2983                 }
2984                 ret = sprintf(formatted_ipaddr, "%pI4", dst_ipaddr);
2985         }
2986
2987         if (strcmp(existing_ipaddr, formatted_ipaddr))
2988                 status = QLA_ERROR;
2989
2990 out_match:
2991         return status;
2992 }
2993
2994 static int qla4xxx_match_fwdb_session(struct scsi_qla_host *ha,
2995                                       struct iscsi_cls_conn *cls_conn)
2996 {
2997         int idx = 0, max_ddbs, rval;
2998         struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
2999         struct iscsi_session *sess, *existing_sess;
3000         struct iscsi_conn *conn, *existing_conn;
3001         struct ddb_entry *ddb_entry;
3002
3003         sess = cls_sess->dd_data;
3004         conn = cls_conn->dd_data;
3005
3006         if (sess->targetname == NULL ||
3007             conn->persistent_address == NULL ||
3008             conn->persistent_port == 0)
3009                 return QLA_ERROR;
3010
3011         max_ddbs =  is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
3012                                      MAX_DEV_DB_ENTRIES;
3013
3014         for (idx = 0; idx < max_ddbs; idx++) {
3015                 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
3016                 if (ddb_entry == NULL)
3017                         continue;
3018
3019                 if (ddb_entry->ddb_type != FLASH_DDB)
3020                         continue;
3021
3022                 existing_sess = ddb_entry->sess->dd_data;
3023                 existing_conn = ddb_entry->conn->dd_data;
3024
3025                 if (existing_sess->targetname == NULL ||
3026                     existing_conn->persistent_address == NULL ||
3027                     existing_conn->persistent_port == 0)
3028                         continue;
3029
3030                 DEBUG2(ql4_printk(KERN_INFO, ha,
3031                                   "IQN = %s User IQN = %s\n",
3032                                   existing_sess->targetname,
3033                                   sess->targetname));
3034
3035                 DEBUG2(ql4_printk(KERN_INFO, ha,
3036                                   "IP = %s User IP = %s\n",
3037                                   existing_conn->persistent_address,
3038                                   conn->persistent_address));
3039
3040                 DEBUG2(ql4_printk(KERN_INFO, ha,
3041                                   "Port = %d User Port = %d\n",
3042                                   existing_conn->persistent_port,
3043                                   conn->persistent_port));
3044
3045                 if (strcmp(existing_sess->targetname, sess->targetname))
3046                         continue;
3047                 rval = qla4xxx_match_ipaddress(ha, ddb_entry,
3048                                         existing_conn->persistent_address,
3049                                         conn->persistent_address);
3050                 if (rval == QLA_ERROR)
3051                         continue;
3052                 if (existing_conn->persistent_port != conn->persistent_port)
3053                         continue;
3054                 break;
3055         }
3056
3057         if (idx == max_ddbs)
3058                 return QLA_ERROR;
3059
3060         DEBUG2(ql4_printk(KERN_INFO, ha,
3061                           "Match found in fwdb sessions\n"));
3062         return QLA_SUCCESS;
3063 }
3064
3065 static struct iscsi_cls_session *
3066 qla4xxx_session_create(struct iscsi_endpoint *ep,
3067                         uint16_t cmds_max, uint16_t qdepth,
3068                         uint32_t initial_cmdsn)
3069 {
3070         struct iscsi_cls_session *cls_sess;
3071         struct scsi_qla_host *ha;
3072         struct qla_endpoint *qla_ep;
3073         struct ddb_entry *ddb_entry;
3074         uint16_t ddb_index;
3075         struct iscsi_session *sess;
3076         struct sockaddr *dst_addr;
3077         int ret;
3078
3079         if (!ep) {
3080                 printk(KERN_ERR "qla4xxx: missing ep.\n");
3081                 return NULL;
3082         }
3083
3084         qla_ep = ep->dd_data;
3085         dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
3086         ha = to_qla_host(qla_ep->host);
3087         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: host: %ld\n", __func__,
3088                           ha->host_no));
3089
3090         ret = qla4xxx_get_ddb_index(ha, &ddb_index);
3091         if (ret == QLA_ERROR)
3092                 return NULL;
3093
3094         cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport, qla_ep->host,
3095                                        cmds_max, sizeof(struct ddb_entry),
3096                                        sizeof(struct ql4_task_data),
3097                                        initial_cmdsn, ddb_index);
3098         if (!cls_sess)
3099                 return NULL;
3100
3101         sess = cls_sess->dd_data;
3102         ddb_entry = sess->dd_data;
3103         ddb_entry->fw_ddb_index = ddb_index;
3104         ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
3105         ddb_entry->ha = ha;
3106         ddb_entry->sess = cls_sess;
3107         ddb_entry->unblock_sess = qla4xxx_unblock_ddb;
3108         ddb_entry->ddb_change = qla4xxx_ddb_change;
3109         clear_bit(DDB_CONN_CLOSE_FAILURE, &ddb_entry->flags);
3110         cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
3111         ha->fw_ddb_index_map[ddb_entry->fw_ddb_index] = ddb_entry;
3112         ha->tot_ddbs++;
3113
3114         return cls_sess;
3115 }
3116
3117 static void qla4xxx_session_destroy(struct iscsi_cls_session *cls_sess)
3118 {
3119         struct iscsi_session *sess;
3120         struct ddb_entry *ddb_entry;
3121         struct scsi_qla_host *ha;
3122         unsigned long flags, wtime;
3123         struct dev_db_entry *fw_ddb_entry = NULL;
3124         dma_addr_t fw_ddb_entry_dma;
3125         uint32_t ddb_state;
3126         int ret;
3127
3128         sess = cls_sess->dd_data;
3129         ddb_entry = sess->dd_data;
3130         ha = ddb_entry->ha;
3131         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: host: %ld\n", __func__,
3132                           ha->host_no));
3133
3134         fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3135                                           &fw_ddb_entry_dma, GFP_KERNEL);
3136         if (!fw_ddb_entry) {
3137                 ql4_printk(KERN_ERR, ha,
3138                            "%s: Unable to allocate dma buffer\n", __func__);
3139                 goto destroy_session;
3140         }
3141
3142         wtime = jiffies + (HZ * LOGOUT_TOV);
3143         do {
3144                 ret = qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index,
3145                                               fw_ddb_entry, fw_ddb_entry_dma,
3146                                               NULL, NULL, &ddb_state, NULL,
3147                                               NULL, NULL);
3148                 if (ret == QLA_ERROR)
3149                         goto destroy_session;
3150
3151                 if ((ddb_state == DDB_DS_NO_CONNECTION_ACTIVE) ||
3152                     (ddb_state == DDB_DS_SESSION_FAILED))
3153                         goto destroy_session;
3154
3155                 schedule_timeout_uninterruptible(HZ);
3156         } while ((time_after(wtime, jiffies)));
3157
3158 destroy_session:
3159         qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
3160         if (test_and_clear_bit(DDB_CONN_CLOSE_FAILURE, &ddb_entry->flags))
3161                 clear_bit(ddb_entry->fw_ddb_index, ha->ddb_idx_map);
3162         spin_lock_irqsave(&ha->hardware_lock, flags);
3163         qla4xxx_free_ddb(ha, ddb_entry);
3164         spin_unlock_irqrestore(&ha->hardware_lock, flags);
3165
3166         iscsi_session_teardown(cls_sess);
3167
3168         if (fw_ddb_entry)
3169                 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3170                                   fw_ddb_entry, fw_ddb_entry_dma);
3171 }
3172
3173 static struct iscsi_cls_conn *
3174 qla4xxx_conn_create(struct iscsi_cls_session *cls_sess, uint32_t conn_idx)
3175 {
3176         struct iscsi_cls_conn *cls_conn;
3177         struct iscsi_session *sess;
3178         struct ddb_entry *ddb_entry;
3179         struct scsi_qla_host *ha;
3180
3181         cls_conn = iscsi_conn_setup(cls_sess, sizeof(struct qla_conn),
3182                                     conn_idx);
3183         if (!cls_conn) {
3184                 pr_info("%s: Can not create connection for conn_idx = %u\n",
3185                         __func__, conn_idx);
3186                 return NULL;
3187         }
3188
3189         sess = cls_sess->dd_data;
3190         ddb_entry = sess->dd_data;
3191         ddb_entry->conn = cls_conn;
3192
3193         ha = ddb_entry->ha;
3194         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: conn_idx = %u\n", __func__,
3195                           conn_idx));
3196         return cls_conn;
3197 }
3198
3199 static int qla4xxx_conn_bind(struct iscsi_cls_session *cls_session,
3200                              struct iscsi_cls_conn *cls_conn,
3201                              uint64_t transport_fd, int is_leading)
3202 {
3203         struct iscsi_conn *conn;
3204         struct qla_conn *qla_conn;
3205         struct iscsi_endpoint *ep;
3206         struct ddb_entry *ddb_entry;
3207         struct scsi_qla_host *ha;
3208         struct iscsi_session *sess;
3209
3210         sess = cls_session->dd_data;
3211         ddb_entry = sess->dd_data;
3212         ha = ddb_entry->ha;
3213
3214         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: sid = %d, cid = %d\n", __func__,
3215                           cls_session->sid, cls_conn->cid));
3216
3217         if (iscsi_conn_bind(cls_session, cls_conn, is_leading))
3218                 return -EINVAL;
3219         ep = iscsi_lookup_endpoint(transport_fd);
3220         if (!ep)
3221                 return -EINVAL;
3222         conn = cls_conn->dd_data;
3223         qla_conn = conn->dd_data;
3224         qla_conn->qla_ep = ep->dd_data;
3225         return 0;
3226 }
3227
3228 static int qla4xxx_conn_start(struct iscsi_cls_conn *cls_conn)
3229 {
3230         struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
3231         struct iscsi_session *sess;
3232         struct ddb_entry *ddb_entry;
3233         struct scsi_qla_host *ha;
3234         struct dev_db_entry *fw_ddb_entry = NULL;
3235         dma_addr_t fw_ddb_entry_dma;
3236         uint32_t mbx_sts = 0;
3237         int ret = 0;
3238         int status = QLA_SUCCESS;
3239
3240         sess = cls_sess->dd_data;
3241         ddb_entry = sess->dd_data;
3242         ha = ddb_entry->ha;
3243         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: sid = %d, cid = %d\n", __func__,
3244                           cls_sess->sid, cls_conn->cid));
3245
3246         /* Check if we have  matching FW DDB, if yes then do not
3247          * login to this target. This could cause target to logout previous
3248          * connection
3249          */
3250         ret = qla4xxx_match_fwdb_session(ha, cls_conn);
3251         if (ret == QLA_SUCCESS) {
3252                 ql4_printk(KERN_INFO, ha,
3253                            "Session already exist in FW.\n");
3254                 ret = -EEXIST;
3255                 goto exit_conn_start;
3256         }
3257
3258         fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3259                                           &fw_ddb_entry_dma, GFP_KERNEL);
3260         if (!fw_ddb_entry) {
3261                 ql4_printk(KERN_ERR, ha,
3262                            "%s: Unable to allocate dma buffer\n", __func__);
3263                 ret = -ENOMEM;
3264                 goto exit_conn_start;
3265         }
3266
3267         ret = qla4xxx_set_param_ddbentry(ha, ddb_entry, cls_conn, &mbx_sts);
3268         if (ret) {
3269                 /* If iscsid is stopped and started then no need to do
3270                 * set param again since ddb state will be already
3271                 * active and FW does not allow set ddb to an
3272                 * active session.
3273                 */
3274                 if (mbx_sts)
3275                         if (ddb_entry->fw_ddb_device_state ==
3276                                                 DDB_DS_SESSION_ACTIVE) {
3277                                 ddb_entry->unblock_sess(ddb_entry->sess);
3278                                 goto exit_set_param;
3279                         }
3280
3281                 ql4_printk(KERN_ERR, ha, "%s: Failed set param for index[%d]\n",
3282                            __func__, ddb_entry->fw_ddb_index);
3283                 goto exit_conn_start;
3284         }
3285
3286         status = qla4xxx_conn_open(ha, ddb_entry->fw_ddb_index);
3287         if (status == QLA_ERROR) {
3288                 ql4_printk(KERN_ERR, ha, "%s: Login failed: %s\n", __func__,
3289                            sess->targetname);
3290                 ret = -EINVAL;
3291                 goto exit_conn_start;
3292         }
3293
3294         if (ddb_entry->fw_ddb_device_state == DDB_DS_NO_CONNECTION_ACTIVE)
3295                 ddb_entry->fw_ddb_device_state = DDB_DS_LOGIN_IN_PROCESS;
3296
3297         DEBUG2(printk(KERN_INFO "%s: DDB state [%d]\n", __func__,
3298                       ddb_entry->fw_ddb_device_state));
3299
3300 exit_set_param:
3301         ret = 0;
3302
3303 exit_conn_start:
3304         if (fw_ddb_entry)
3305                 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3306                                   fw_ddb_entry, fw_ddb_entry_dma);
3307         return ret;
3308 }
3309
3310 static void qla4xxx_conn_destroy(struct iscsi_cls_conn *cls_conn)
3311 {
3312         struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
3313         struct iscsi_session *sess;
3314         struct scsi_qla_host *ha;
3315         struct ddb_entry *ddb_entry;
3316         int options;
3317
3318         sess = cls_sess->dd_data;
3319         ddb_entry = sess->dd_data;
3320         ha = ddb_entry->ha;
3321         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: cid = %d\n", __func__,
3322                           cls_conn->cid));
3323
3324         options = LOGOUT_OPTION_CLOSE_SESSION;
3325         if (qla4xxx_session_logout_ddb(ha, ddb_entry, options) == QLA_ERROR)
3326                 ql4_printk(KERN_ERR, ha, "%s: Logout failed\n", __func__);
3327 }
3328
3329 static void qla4xxx_task_work(struct work_struct *wdata)
3330 {
3331         struct ql4_task_data *task_data;
3332         struct scsi_qla_host *ha;
3333         struct passthru_status *sts;
3334         struct iscsi_task *task;
3335         struct iscsi_hdr *hdr;
3336         uint8_t *data;
3337         uint32_t data_len;
3338         struct iscsi_conn *conn;
3339         int hdr_len;
3340         itt_t itt;
3341
3342         task_data = container_of(wdata, struct ql4_task_data, task_work);
3343         ha = task_data->ha;
3344         task = task_data->task;
3345         sts = &task_data->sts;
3346         hdr_len = sizeof(struct iscsi_hdr);
3347
3348         DEBUG3(printk(KERN_INFO "Status returned\n"));
3349         DEBUG3(qla4xxx_dump_buffer(sts, 64));
3350         DEBUG3(printk(KERN_INFO "Response buffer"));
3351         DEBUG3(qla4xxx_dump_buffer(task_data->resp_buffer, 64));
3352
3353         conn = task->conn;
3354
3355         switch (sts->completionStatus) {
3356         case PASSTHRU_STATUS_COMPLETE:
3357                 hdr = (struct iscsi_hdr *)task_data->resp_buffer;
3358                 /* Assign back the itt in hdr, until we use the PREASSIGN_TAG */
3359                 itt = sts->handle;
3360                 hdr->itt = itt;
3361                 data = task_data->resp_buffer + hdr_len;
3362                 data_len = task_data->resp_len - hdr_len;
3363                 iscsi_complete_pdu(conn, hdr, data, data_len);
3364                 break;
3365         default:
3366                 ql4_printk(KERN_ERR, ha, "Passthru failed status = 0x%x\n",
3367                            sts->completionStatus);
3368                 break;
3369         }
3370         return;
3371 }
3372
3373 static int qla4xxx_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
3374 {
3375         struct ql4_task_data *task_data;
3376         struct iscsi_session *sess;
3377         struct ddb_entry *ddb_entry;
3378         struct scsi_qla_host *ha;
3379         int hdr_len;
3380
3381         sess = task->conn->session;
3382         ddb_entry = sess->dd_data;
3383         ha = ddb_entry->ha;
3384         task_data = task->dd_data;
3385         memset(task_data, 0, sizeof(struct ql4_task_data));
3386
3387         if (task->sc) {
3388                 ql4_printk(KERN_INFO, ha,
3389                            "%s: SCSI Commands not implemented\n", __func__);
3390                 return -EINVAL;
3391         }
3392
3393         hdr_len = sizeof(struct iscsi_hdr);
3394         task_data->ha = ha;
3395         task_data->task = task;
3396
3397         if (task->data_count) {
3398                 task_data->data_dma = dma_map_single(&ha->pdev->dev, task->data,
3399                                                      task->data_count,
3400                                                      PCI_DMA_TODEVICE);
3401         }
3402
3403         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
3404                       __func__, task->conn->max_recv_dlength, hdr_len));
3405
3406         task_data->resp_len = task->conn->max_recv_dlength + hdr_len;
3407         task_data->resp_buffer = dma_alloc_coherent(&ha->pdev->dev,
3408                                                     task_data->resp_len,
3409                                                     &task_data->resp_dma,
3410                                                     GFP_ATOMIC);
3411         if (!task_data->resp_buffer)
3412                 goto exit_alloc_pdu;
3413
3414         task_data->req_len = task->data_count + hdr_len;
3415         task_data->req_buffer = dma_alloc_coherent(&ha->pdev->dev,
3416                                                    task_data->req_len,
3417                                                    &task_data->req_dma,
3418                                                    GFP_ATOMIC);
3419         if (!task_data->req_buffer)
3420                 goto exit_alloc_pdu;
3421
3422         task->hdr = task_data->req_buffer;
3423
3424         INIT_WORK(&task_data->task_work, qla4xxx_task_work);
3425
3426         return 0;
3427
3428 exit_alloc_pdu:
3429         if (task_data->resp_buffer)
3430                 dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
3431                                   task_data->resp_buffer, task_data->resp_dma);
3432
3433         if (task_data->req_buffer)
3434                 dma_free_coherent(&ha->pdev->dev, task_data->req_len,
3435                                   task_data->req_buffer, task_data->req_dma);
3436         return -ENOMEM;
3437 }
3438
3439 static void qla4xxx_task_cleanup(struct iscsi_task *task)
3440 {
3441         struct ql4_task_data *task_data;
3442         struct iscsi_session *sess;
3443         struct ddb_entry *ddb_entry;
3444         struct scsi_qla_host *ha;
3445         int hdr_len;
3446
3447         hdr_len = sizeof(struct iscsi_hdr);
3448         sess = task->conn->session;
3449         ddb_entry = sess->dd_data;
3450         ha = ddb_entry->ha;
3451         task_data = task->dd_data;
3452
3453         if (task->data_count) {
3454                 dma_unmap_single(&ha->pdev->dev, task_data->data_dma,
3455                                  task->data_count, PCI_DMA_TODEVICE);
3456         }
3457
3458         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
3459                       __func__, task->conn->max_recv_dlength, hdr_len));
3460
3461         dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
3462                           task_data->resp_buffer, task_data->resp_dma);
3463         dma_free_coherent(&ha->pdev->dev, task_data->req_len,
3464                           task_data->req_buffer, task_data->req_dma);
3465         return;
3466 }
3467
3468 static int qla4xxx_task_xmit(struct iscsi_task *task)
3469 {
3470         struct scsi_cmnd *sc = task->sc;
3471         struct iscsi_session *sess = task->conn->session;
3472         struct ddb_entry *ddb_entry = sess->dd_data;
3473         struct scsi_qla_host *ha = ddb_entry->ha;
3474
3475         if (!sc)
3476                 return qla4xxx_send_passthru0(task);
3477
3478         ql4_printk(KERN_INFO, ha, "%s: scsi cmd xmit not implemented\n",
3479                    __func__);
3480         return -ENOSYS;
3481 }
3482
3483 static int qla4xxx_copy_from_fwddb_param(struct iscsi_bus_flash_session *sess,
3484                                          struct iscsi_bus_flash_conn *conn,
3485                                          struct dev_db_entry *fw_ddb_entry)
3486 {
3487         unsigned long options = 0;
3488         int rc = 0;
3489
3490         options = le16_to_cpu(fw_ddb_entry->options);
3491         conn->is_fw_assigned_ipv6 = test_bit(OPT_IS_FW_ASSIGNED_IPV6, &options);
3492         if (test_bit(OPT_IPV6_DEVICE, &options)) {
3493                 rc = iscsi_switch_str_param(&sess->portal_type,
3494                                             PORTAL_TYPE_IPV6);
3495                 if (rc)
3496                         goto exit_copy;
3497         } else {
3498                 rc = iscsi_switch_str_param(&sess->portal_type,
3499                                             PORTAL_TYPE_IPV4);
3500                 if (rc)
3501                         goto exit_copy;
3502         }
3503
3504         sess->auto_snd_tgt_disable = test_bit(OPT_AUTO_SENDTGTS_DISABLE,
3505                                               &options);
3506         sess->discovery_sess = test_bit(OPT_DISC_SESSION, &options);
3507         sess->entry_state = test_bit(OPT_ENTRY_STATE, &options);
3508
3509         options = le16_to_cpu(fw_ddb_entry->iscsi_options);
3510         conn->hdrdgst_en = test_bit(ISCSIOPT_HEADER_DIGEST_EN, &options);
3511         conn->datadgst_en = test_bit(ISCSIOPT_DATA_DIGEST_EN, &options);
3512         sess->imm_data_en = test_bit(ISCSIOPT_IMMEDIATE_DATA_EN, &options);
3513         sess->initial_r2t_en = test_bit(ISCSIOPT_INITIAL_R2T_EN, &options);
3514         sess->dataseq_inorder_en = test_bit(ISCSIOPT_DATA_SEQ_IN_ORDER,
3515                                             &options);
3516         sess->pdu_inorder_en = test_bit(ISCSIOPT_DATA_PDU_IN_ORDER, &options);
3517         sess->chap_auth_en = test_bit(ISCSIOPT_CHAP_AUTH_EN, &options);
3518         conn->snack_req_en = test_bit(ISCSIOPT_SNACK_REQ_EN, &options);
3519         sess->discovery_logout_en = test_bit(ISCSIOPT_DISCOVERY_LOGOUT_EN,
3520                                              &options);
3521         sess->bidi_chap_en = test_bit(ISCSIOPT_BIDI_CHAP_EN, &options);
3522         sess->discovery_auth_optional =
3523                         test_bit(ISCSIOPT_DISCOVERY_AUTH_OPTIONAL, &options);
3524         if (test_bit(ISCSIOPT_ERL1, &options))
3525                 sess->erl |= BIT_1;
3526         if (test_bit(ISCSIOPT_ERL0, &options))
3527                 sess->erl |= BIT_0;
3528
3529         options = le16_to_cpu(fw_ddb_entry->tcp_options);
3530         conn->tcp_timestamp_stat = test_bit(TCPOPT_TIMESTAMP_STAT, &options);
3531         conn->tcp_nagle_disable = test_bit(TCPOPT_NAGLE_DISABLE, &options);
3532         conn->tcp_wsf_disable = test_bit(TCPOPT_WSF_DISABLE, &options);
3533         if (test_bit(TCPOPT_TIMER_SCALE3, &options))
3534                 conn->tcp_timer_scale |= BIT_3;
3535         if (test_bit(TCPOPT_TIMER_SCALE2, &options))
3536                 conn->tcp_timer_scale |= BIT_2;
3537         if (test_bit(TCPOPT_TIMER_SCALE1, &options))
3538                 conn->tcp_timer_scale |= BIT_1;
3539
3540         conn->tcp_timer_scale >>= 1;
3541         conn->tcp_timestamp_en = test_bit(TCPOPT_TIMESTAMP_EN, &options);
3542
3543         options = le16_to_cpu(fw_ddb_entry->ip_options);
3544         conn->fragment_disable = test_bit(IPOPT_FRAGMENT_DISABLE, &options);
3545
3546         conn->max_recv_dlength = BYTE_UNITS *
3547                           le16_to_cpu(fw_ddb_entry->iscsi_max_rcv_data_seg_len);
3548         conn->max_xmit_dlength = BYTE_UNITS *
3549                           le16_to_cpu(fw_ddb_entry->iscsi_max_snd_data_seg_len);
3550         sess->first_burst = BYTE_UNITS *
3551                                le16_to_cpu(fw_ddb_entry->iscsi_first_burst_len);
3552         sess->max_burst = BYTE_UNITS *
3553                                  le16_to_cpu(fw_ddb_entry->iscsi_max_burst_len);
3554         sess->max_r2t = le16_to_cpu(fw_ddb_entry->iscsi_max_outsnd_r2t);
3555         sess->time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
3556         sess->time2retain = le16_to_cpu(fw_ddb_entry->iscsi_def_time2retain);
3557         sess->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
3558         conn->max_segment_size = le16_to_cpu(fw_ddb_entry->mss);
3559         conn->tcp_xmit_wsf = fw_ddb_entry->tcp_xmt_wsf;
3560         conn->tcp_recv_wsf = fw_ddb_entry->tcp_rcv_wsf;
3561         conn->ipv6_flow_label = le16_to_cpu(fw_ddb_entry->ipv6_flow_lbl);
3562         conn->keepalive_timeout = le16_to_cpu(fw_ddb_entry->ka_timeout);
3563         conn->local_port = le16_to_cpu(fw_ddb_entry->lcl_port);
3564         conn->statsn = le32_to_cpu(fw_ddb_entry->stat_sn);
3565         conn->exp_statsn = le32_to_cpu(fw_ddb_entry->exp_stat_sn);
3566         sess->discovery_parent_idx = le16_to_cpu(fw_ddb_entry->ddb_link);
3567         sess->discovery_parent_type = le16_to_cpu(fw_ddb_entry->ddb_link);
3568         sess->chap_out_idx = le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
3569         sess->tsid = le16_to_cpu(fw_ddb_entry->tsid);
3570
3571         sess->default_taskmgmt_timeout =
3572                                 le16_to_cpu(fw_ddb_entry->def_timeout);
3573         conn->port = le16_to_cpu(fw_ddb_entry->port);
3574
3575         options = le16_to_cpu(fw_ddb_entry->options);
3576         conn->ipaddress = kzalloc(IPv6_ADDR_LEN, GFP_KERNEL);
3577         if (!conn->ipaddress) {
3578                 rc = -ENOMEM;
3579                 goto exit_copy;
3580         }
3581
3582         conn->redirect_ipaddr = kzalloc(IPv6_ADDR_LEN, GFP_KERNEL);
3583         if (!conn->redirect_ipaddr) {
3584                 rc = -ENOMEM;
3585                 goto exit_copy;
3586         }
3587
3588         memcpy(conn->ipaddress, fw_ddb_entry->ip_addr, IPv6_ADDR_LEN);
3589         memcpy(conn->redirect_ipaddr, fw_ddb_entry->tgt_addr, IPv6_ADDR_LEN);
3590
3591         if (test_bit(OPT_IPV6_DEVICE, &options)) {
3592                 conn->ipv6_traffic_class = fw_ddb_entry->ipv4_tos;
3593
3594                 conn->link_local_ipv6_addr = kmemdup(
3595                                         fw_ddb_entry->link_local_ipv6_addr,
3596                                         IPv6_ADDR_LEN, GFP_KERNEL);
3597                 if (!conn->link_local_ipv6_addr) {
3598                         rc = -ENOMEM;
3599                         goto exit_copy;
3600                 }
3601         } else {
3602                 conn->ipv4_tos = fw_ddb_entry->ipv4_tos;
3603         }
3604
3605         if (fw_ddb_entry->iscsi_name[0]) {
3606                 rc = iscsi_switch_str_param(&sess->targetname,
3607                                             (char *)fw_ddb_entry->iscsi_name);
3608                 if (rc)
3609                         goto exit_copy;
3610         }
3611
3612         if (fw_ddb_entry->iscsi_alias[0]) {
3613                 rc = iscsi_switch_str_param(&sess->targetalias,
3614                                             (char *)fw_ddb_entry->iscsi_alias);
3615                 if (rc)
3616                         goto exit_copy;
3617         }
3618
3619         COPY_ISID(sess->isid, fw_ddb_entry->isid);
3620
3621 exit_copy:
3622         return rc;
3623 }
3624
3625 static int qla4xxx_copy_to_fwddb_param(struct iscsi_bus_flash_session *sess,
3626                                        struct iscsi_bus_flash_conn *conn,
3627                                        struct dev_db_entry *fw_ddb_entry)
3628 {
3629         uint16_t options;
3630         int rc = 0;
3631
3632         options = le16_to_cpu(fw_ddb_entry->options);
3633         SET_BITVAL(conn->is_fw_assigned_ipv6,  options, BIT_11);
3634         if (!strncmp(sess->portal_type, PORTAL_TYPE_IPV6, 4))
3635                 options |= BIT_8;
3636         else
3637                 options &= ~BIT_8;
3638
3639         SET_BITVAL(sess->auto_snd_tgt_disable, options, BIT_6);
3640         SET_BITVAL(sess->discovery_sess, options, BIT_4);
3641         SET_BITVAL(sess->entry_state, options, BIT_3);
3642         fw_ddb_entry->options = cpu_to_le16(options);
3643
3644         options = le16_to_cpu(fw_ddb_entry->iscsi_options);
3645         SET_BITVAL(conn->hdrdgst_en, options, BIT_13);
3646         SET_BITVAL(conn->datadgst_en, options, BIT_12);
3647         SET_BITVAL(sess->imm_data_en, options, BIT_11);
3648         SET_BITVAL(sess->initial_r2t_en, options, BIT_10);
3649         SET_BITVAL(sess->dataseq_inorder_en, options, BIT_9);
3650         SET_BITVAL(sess->pdu_inorder_en, options, BIT_8);
3651         SET_BITVAL(sess->chap_auth_en, options, BIT_7);
3652         SET_BITVAL(conn->snack_req_en, options, BIT_6);
3653         SET_BITVAL(sess->discovery_logout_en, options, BIT_5);
3654         SET_BITVAL(sess->bidi_chap_en, options, BIT_4);
3655         SET_BITVAL(sess->discovery_auth_optional, options, BIT_3);
3656         SET_BITVAL(sess->erl & BIT_1, options, BIT_1);
3657         SET_BITVAL(sess->erl & BIT_0, options, BIT_0);
3658         fw_ddb_entry->iscsi_options = cpu_to_le16(options);
3659
3660         options = le16_to_cpu(fw_ddb_entry->tcp_options);
3661         SET_BITVAL(conn->tcp_timestamp_stat, options, BIT_6);
3662         SET_BITVAL(conn->tcp_nagle_disable, options, BIT_5);
3663         SET_BITVAL(conn->tcp_wsf_disable, options, BIT_4);
3664         SET_BITVAL(conn->tcp_timer_scale & BIT_2, options, BIT_3);
3665         SET_BITVAL(conn->tcp_timer_scale & BIT_1, options, BIT_2);
3666         SET_BITVAL(conn->tcp_timer_scale & BIT_0, options, BIT_1);
3667         SET_BITVAL(conn->tcp_timestamp_en, options, BIT_0);
3668         fw_ddb_entry->tcp_options = cpu_to_le16(options);
3669
3670         options = le16_to_cpu(fw_ddb_entry->ip_options);
3671         SET_BITVAL(conn->fragment_disable, options, BIT_4);
3672         fw_ddb_entry->ip_options = cpu_to_le16(options);
3673
3674         fw_ddb_entry->iscsi_max_outsnd_r2t = cpu_to_le16(sess->max_r2t);
3675         fw_ddb_entry->iscsi_max_rcv_data_seg_len =
3676                                cpu_to_le16(conn->max_recv_dlength / BYTE_UNITS);
3677         fw_ddb_entry->iscsi_max_snd_data_seg_len =
3678                                cpu_to_le16(conn->max_xmit_dlength / BYTE_UNITS);
3679         fw_ddb_entry->iscsi_first_burst_len =
3680                                 cpu_to_le16(sess->first_burst / BYTE_UNITS);
3681         fw_ddb_entry->iscsi_max_burst_len = cpu_to_le16(sess->max_burst /
3682                                             BYTE_UNITS);
3683         fw_ddb_entry->iscsi_def_time2wait = cpu_to_le16(sess->time2wait);
3684         fw_ddb_entry->iscsi_def_time2retain = cpu_to_le16(sess->time2retain);
3685         fw_ddb_entry->tgt_portal_grp = cpu_to_le16(sess->tpgt);
3686         fw_ddb_entry->mss = cpu_to_le16(conn->max_segment_size);
3687         fw_ddb_entry->tcp_xmt_wsf = (uint8_t) cpu_to_le32(conn->tcp_xmit_wsf);
3688         fw_ddb_entry->tcp_rcv_wsf = (uint8_t) cpu_to_le32(conn->tcp_recv_wsf);
3689         fw_ddb_entry->ipv6_flow_lbl = cpu_to_le16(conn->ipv6_flow_label);
3690         fw_ddb_entry->ka_timeout = cpu_to_le16(conn->keepalive_timeout);
3691         fw_ddb_entry->lcl_port = cpu_to_le16(conn->local_port);
3692         fw_ddb_entry->stat_sn = cpu_to_le32(conn->statsn);
3693         fw_ddb_entry->exp_stat_sn = cpu_to_le32(conn->exp_statsn);
3694         fw_ddb_entry->ddb_link = cpu_to_le16(sess->discovery_parent_idx);
3695         fw_ddb_entry->chap_tbl_idx = cpu_to_le16(sess->chap_out_idx);
3696         fw_ddb_entry->tsid = cpu_to_le16(sess->tsid);
3697         fw_ddb_entry->port = cpu_to_le16(conn->port);
3698         fw_ddb_entry->def_timeout =
3699                                 cpu_to_le16(sess->default_taskmgmt_timeout);
3700
3701         if (!strncmp(sess->portal_type, PORTAL_TYPE_IPV6, 4))
3702                 fw_ddb_entry->ipv4_tos = conn->ipv6_traffic_class;
3703         else
3704                 fw_ddb_entry->ipv4_tos = conn->ipv4_tos;
3705
3706         if (conn->ipaddress)
3707                 memcpy(fw_ddb_entry->ip_addr, conn->ipaddress,
3708                        sizeof(fw_ddb_entry->ip_addr));
3709
3710         if (conn->redirect_ipaddr)
3711                 memcpy(fw_ddb_entry->tgt_addr, conn->redirect_ipaddr,
3712                        sizeof(fw_ddb_entry->tgt_addr));
3713
3714         if (conn->link_local_ipv6_addr)
3715                 memcpy(fw_ddb_entry->link_local_ipv6_addr,
3716                        conn->link_local_ipv6_addr,
3717                        sizeof(fw_ddb_entry->link_local_ipv6_addr));
3718
3719         if (sess->targetname)
3720                 memcpy(fw_ddb_entry->iscsi_name, sess->targetname,
3721                        sizeof(fw_ddb_entry->iscsi_name));
3722
3723         if (sess->targetalias)
3724                 memcpy(fw_ddb_entry->iscsi_alias, sess->targetalias,
3725                        sizeof(fw_ddb_entry->iscsi_alias));
3726
3727         COPY_ISID(fw_ddb_entry->isid, sess->isid);
3728
3729         return rc;
3730 }
3731
3732 static void qla4xxx_copy_to_sess_conn_params(struct iscsi_conn *conn,
3733                                              struct iscsi_session *sess,
3734                                              struct dev_db_entry *fw_ddb_entry)
3735 {
3736         unsigned long options = 0;
3737         uint16_t ddb_link;
3738         uint16_t disc_parent;
3739         char ip_addr[DDB_IPADDR_LEN];
3740
3741         options = le16_to_cpu(fw_ddb_entry->options);
3742         conn->is_fw_assigned_ipv6 = test_bit(OPT_IS_FW_ASSIGNED_IPV6, &options);
3743         sess->auto_snd_tgt_disable = test_bit(OPT_AUTO_SENDTGTS_DISABLE,
3744                                               &options);
3745         sess->discovery_sess = test_bit(OPT_DISC_SESSION, &options);
3746
3747         options = le16_to_cpu(fw_ddb_entry->iscsi_options);
3748         conn->hdrdgst_en = test_bit(ISCSIOPT_HEADER_DIGEST_EN, &options);
3749         conn->datadgst_en = test_bit(ISCSIOPT_DATA_DIGEST_EN, &options);
3750         sess->imm_data_en = test_bit(ISCSIOPT_IMMEDIATE_DATA_EN, &options);
3751         sess->initial_r2t_en = test_bit(ISCSIOPT_INITIAL_R2T_EN, &options);
3752         sess->dataseq_inorder_en = test_bit(ISCSIOPT_DATA_SEQ_IN_ORDER,
3753                                             &options);
3754         sess->pdu_inorder_en = test_bit(ISCSIOPT_DATA_PDU_IN_ORDER, &options);
3755         sess->chap_auth_en = test_bit(ISCSIOPT_CHAP_AUTH_EN, &options);
3756         sess->discovery_logout_en = test_bit(ISCSIOPT_DISCOVERY_LOGOUT_EN,
3757                                              &options);
3758         sess->bidi_chap_en = test_bit(ISCSIOPT_BIDI_CHAP_EN, &options);
3759         sess->discovery_auth_optional =
3760                         test_bit(ISCSIOPT_DISCOVERY_AUTH_OPTIONAL, &options);
3761         if (test_bit(ISCSIOPT_ERL1, &options))
3762                 sess->erl |= BIT_1;
3763         if (test_bit(ISCSIOPT_ERL0, &options))
3764                 sess->erl |= BIT_0;
3765
3766         options = le16_to_cpu(fw_ddb_entry->tcp_options);
3767         conn->tcp_timestamp_stat = test_bit(TCPOPT_TIMESTAMP_STAT, &options);
3768         conn->tcp_nagle_disable = test_bit(TCPOPT_NAGLE_DISABLE, &options);
3769         conn->tcp_wsf_disable = test_bit(TCPOPT_WSF_DISABLE, &options);
3770         if (test_bit(TCPOPT_TIMER_SCALE3, &options))
3771                 conn->tcp_timer_scale |= BIT_3;
3772         if (test_bit(TCPOPT_TIMER_SCALE2, &options))
3773                 conn->tcp_timer_scale |= BIT_2;
3774         if (test_bit(TCPOPT_TIMER_SCALE1, &options))
3775                 conn->tcp_timer_scale |= BIT_1;
3776
3777         conn->tcp_timer_scale >>= 1;
3778         conn->tcp_timestamp_en = test_bit(TCPOPT_TIMESTAMP_EN, &options);
3779
3780         options = le16_to_cpu(fw_ddb_entry->ip_options);
3781         conn->fragment_disable = test_bit(IPOPT_FRAGMENT_DISABLE, &options);
3782
3783         conn->max_recv_dlength = BYTE_UNITS *
3784                           le16_to_cpu(fw_ddb_entry->iscsi_max_rcv_data_seg_len);
3785         conn->max_xmit_dlength = BYTE_UNITS *
3786                           le16_to_cpu(fw_ddb_entry->iscsi_max_snd_data_seg_len);
3787         sess->max_r2t = le16_to_cpu(fw_ddb_entry->iscsi_max_outsnd_r2t);
3788         sess->first_burst = BYTE_UNITS *
3789                                le16_to_cpu(fw_ddb_entry->iscsi_first_burst_len);
3790         sess->max_burst = BYTE_UNITS *
3791                                  le16_to_cpu(fw_ddb_entry->iscsi_max_burst_len);
3792         sess->time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
3793         sess->time2retain = le16_to_cpu(fw_ddb_entry->iscsi_def_time2retain);
3794         sess->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
3795         conn->max_segment_size = le16_to_cpu(fw_ddb_entry->mss);
3796         conn->tcp_xmit_wsf = fw_ddb_entry->tcp_xmt_wsf;
3797         conn->tcp_recv_wsf = fw_ddb_entry->tcp_rcv_wsf;
3798         conn->ipv4_tos = fw_ddb_entry->ipv4_tos;
3799         conn->keepalive_tmo = le16_to_cpu(fw_ddb_entry->ka_timeout);
3800         conn->local_port = le16_to_cpu(fw_ddb_entry->lcl_port);
3801         conn->statsn = le32_to_cpu(fw_ddb_entry->stat_sn);
3802         conn->exp_statsn = le32_to_cpu(fw_ddb_entry->exp_stat_sn);
3803         sess->tsid = le16_to_cpu(fw_ddb_entry->tsid);
3804         COPY_ISID(sess->isid, fw_ddb_entry->isid);
3805
3806         ddb_link = le16_to_cpu(fw_ddb_entry->ddb_link);
3807         if (ddb_link == DDB_ISNS)
3808                 disc_parent = ISCSI_DISC_PARENT_ISNS;
3809         else if (ddb_link == DDB_NO_LINK)
3810                 disc_parent = ISCSI_DISC_PARENT_UNKNOWN;
3811         else if (ddb_link < MAX_DDB_ENTRIES)
3812                 disc_parent = ISCSI_DISC_PARENT_SENDTGT;
3813         else
3814                 disc_parent = ISCSI_DISC_PARENT_UNKNOWN;
3815
3816         iscsi_set_param(conn->cls_conn, ISCSI_PARAM_DISCOVERY_PARENT_TYPE,
3817                         iscsi_get_discovery_parent_name(disc_parent), 0);
3818
3819         iscsi_set_param(conn->cls_conn, ISCSI_PARAM_TARGET_ALIAS,
3820                         (char *)fw_ddb_entry->iscsi_alias, 0);
3821
3822         options = le16_to_cpu(fw_ddb_entry->options);
3823         if (options & DDB_OPT_IPV6_DEVICE) {
3824                 memset(ip_addr, 0, sizeof(ip_addr));
3825                 sprintf(ip_addr, "%pI6", fw_ddb_entry->link_local_ipv6_addr);
3826                 iscsi_set_param(conn->cls_conn, ISCSI_PARAM_LOCAL_IPADDR,
3827                                 (char *)ip_addr, 0);
3828         }
3829 }
3830
3831 static void qla4xxx_copy_fwddb_param(struct scsi_qla_host *ha,
3832                                      struct dev_db_entry *fw_ddb_entry,
3833                                      struct iscsi_cls_session *cls_sess,
3834                                      struct iscsi_cls_conn *cls_conn)
3835 {
3836         int buflen = 0;
3837         struct iscsi_session *sess;
3838         struct ddb_entry *ddb_entry;
3839         struct ql4_chap_table chap_tbl;
3840         struct iscsi_conn *conn;
3841         char ip_addr[DDB_IPADDR_LEN];
3842         uint16_t options = 0;
3843
3844         sess = cls_sess->dd_data;
3845         ddb_entry = sess->dd_data;
3846         conn = cls_conn->dd_data;
3847         memset(&chap_tbl, 0, sizeof(chap_tbl));
3848
3849         ddb_entry->chap_tbl_idx = le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
3850
3851         qla4xxx_copy_to_sess_conn_params(conn, sess, fw_ddb_entry);
3852
3853         sess->def_taskmgmt_tmo = le16_to_cpu(fw_ddb_entry->def_timeout);
3854         conn->persistent_port = le16_to_cpu(fw_ddb_entry->port);
3855
3856         memset(ip_addr, 0, sizeof(ip_addr));
3857         options = le16_to_cpu(fw_ddb_entry->options);
3858         if (options & DDB_OPT_IPV6_DEVICE) {
3859                 iscsi_set_param(cls_conn, ISCSI_PARAM_PORTAL_TYPE, "ipv6", 4);
3860
3861                 memset(ip_addr, 0, sizeof(ip_addr));
3862                 sprintf(ip_addr, "%pI6", fw_ddb_entry->ip_addr);
3863         } else {
3864                 iscsi_set_param(cls_conn, ISCSI_PARAM_PORTAL_TYPE, "ipv4", 4);
3865                 sprintf(ip_addr, "%pI4", fw_ddb_entry->ip_addr);
3866         }
3867
3868         iscsi_set_param(cls_conn, ISCSI_PARAM_PERSISTENT_ADDRESS,
3869                         (char *)ip_addr, buflen);
3870         iscsi_set_param(cls_conn, ISCSI_PARAM_TARGET_NAME,
3871                         (char *)fw_ddb_entry->iscsi_name, buflen);
3872         iscsi_set_param(cls_conn, ISCSI_PARAM_INITIATOR_NAME,
3873                         (char *)ha->name_string, buflen);
3874
3875         if (ddb_entry->chap_tbl_idx != INVALID_ENTRY) {
3876                 if (!qla4xxx_get_uni_chap_at_index(ha, chap_tbl.name,
3877                                                    chap_tbl.secret,
3878                                                    ddb_entry->chap_tbl_idx)) {
3879                         iscsi_set_param(cls_conn, ISCSI_PARAM_USERNAME,
3880                                         (char *)chap_tbl.name,
3881                                         strlen((char *)chap_tbl.name));
3882                         iscsi_set_param(cls_conn, ISCSI_PARAM_PASSWORD,
3883                                         (char *)chap_tbl.secret,
3884                                         chap_tbl.secret_len);
3885                 }
3886         }
3887 }
3888
3889 void qla4xxx_update_session_conn_fwddb_param(struct scsi_qla_host *ha,
3890                                              struct ddb_entry *ddb_entry)
3891 {
3892         struct iscsi_cls_session *cls_sess;
3893         struct iscsi_cls_conn *cls_conn;
3894         uint32_t ddb_state;
3895         dma_addr_t fw_ddb_entry_dma;
3896         struct dev_db_entry *fw_ddb_entry;
3897
3898         fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3899                                           &fw_ddb_entry_dma, GFP_KERNEL);
3900         if (!fw_ddb_entry) {
3901                 ql4_printk(KERN_ERR, ha,
3902                            "%s: Unable to allocate dma buffer\n", __func__);
3903                 goto exit_session_conn_fwddb_param;
3904         }
3905
3906         if (qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index, fw_ddb_entry,
3907                                     fw_ddb_entry_dma, NULL, NULL, &ddb_state,
3908                                     NULL, NULL, NULL) == QLA_ERROR) {
3909                 DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: failed "
3910                                   "get_ddb_entry for fw_ddb_index %d\n",
3911                                   ha->host_no, __func__,
3912                                   ddb_entry->fw_ddb_index));
3913                 goto exit_session_conn_fwddb_param;
3914         }
3915
3916         cls_sess = ddb_entry->sess;
3917
3918         cls_conn = ddb_entry->conn;
3919
3920         /* Update params */
3921         qla4xxx_copy_fwddb_param(ha, fw_ddb_entry, cls_sess, cls_conn);
3922
3923 exit_session_conn_fwddb_param:
3924         if (fw_ddb_entry)
3925                 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3926                                   fw_ddb_entry, fw_ddb_entry_dma);
3927 }
3928
3929 void qla4xxx_update_session_conn_param(struct scsi_qla_host *ha,
3930                                        struct ddb_entry *ddb_entry)
3931 {
3932         struct iscsi_cls_session *cls_sess;
3933         struct iscsi_cls_conn *cls_conn;
3934         struct iscsi_session *sess;
3935         struct iscsi_conn *conn;
3936         uint32_t ddb_state;
3937         dma_addr_t fw_ddb_entry_dma;
3938         struct dev_db_entry *fw_ddb_entry;
3939
3940         fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3941                                           &fw_ddb_entry_dma, GFP_KERNEL);
3942         if (!fw_ddb_entry) {
3943                 ql4_printk(KERN_ERR, ha,
3944                            "%s: Unable to allocate dma buffer\n", __func__);
3945                 goto exit_session_conn_param;
3946         }
3947
3948         if (qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index, fw_ddb_entry,
3949                                     fw_ddb_entry_dma, NULL, NULL, &ddb_state,
3950                                     NULL, NULL, NULL) == QLA_ERROR) {
3951                 DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: failed "
3952                                   "get_ddb_entry for fw_ddb_index %d\n",
3953                                   ha->host_no, __func__,
3954                                   ddb_entry->fw_ddb_index));
3955                 goto exit_session_conn_param;
3956         }
3957
3958         cls_sess = ddb_entry->sess;
3959         sess = cls_sess->dd_data;
3960
3961         cls_conn = ddb_entry->conn;
3962         conn = cls_conn->dd_data;
3963
3964         /* Update timers after login */
3965         ddb_entry->default_relogin_timeout =
3966                 (le16_to_cpu(fw_ddb_entry->def_timeout) > LOGIN_TOV) &&
3967                  (le16_to_cpu(fw_ddb_entry->def_timeout) < LOGIN_TOV * 10) ?
3968                  le16_to_cpu(fw_ddb_entry->def_timeout) : LOGIN_TOV;
3969         ddb_entry->default_time2wait =
3970                                 le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
3971
3972         /* Update params */
3973         ddb_entry->chap_tbl_idx = le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
3974         qla4xxx_copy_to_sess_conn_params(conn, sess, fw_ddb_entry);
3975
3976         memcpy(sess->initiatorname, ha->name_string,
3977                min(sizeof(ha->name_string), sizeof(sess->initiatorname)));
3978
3979 exit_session_conn_param:
3980         if (fw_ddb_entry)
3981                 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3982                                   fw_ddb_entry, fw_ddb_entry_dma);
3983 }
3984
3985 /*
3986  * Timer routines
3987  */
3988
3989 static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
3990                                 unsigned long interval)
3991 {
3992         DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
3993                      __func__, ha->host->host_no));
3994         init_timer(&ha->timer);
3995         ha->timer.expires = jiffies + interval * HZ;
3996         ha->timer.data = (unsigned long)ha;
3997         ha->timer.function = (void (*)(unsigned long))func;
3998         add_timer(&ha->timer);
3999         ha->timer_active = 1;
4000 }
4001
4002 static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
4003 {
4004         del_timer_sync(&ha->timer);
4005         ha->timer_active = 0;
4006 }
4007
4008 /***
4009  * qla4xxx_mark_device_missing - blocks the session
4010  * @cls_session: Pointer to the session to be blocked
4011  * @ddb_entry: Pointer to device database entry
4012  *
4013  * This routine marks a device missing and close connection.
4014  **/
4015 void qla4xxx_mark_device_missing(struct iscsi_cls_session *cls_session)
4016 {
4017         iscsi_block_session(cls_session);
4018 }
4019
4020 /**
4021  * qla4xxx_mark_all_devices_missing - mark all devices as missing.
4022  * @ha: Pointer to host adapter structure.
4023  *
4024  * This routine marks a device missing and resets the relogin retry count.
4025  **/
4026 void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
4027 {
4028         iscsi_host_for_each_session(ha->host, qla4xxx_mark_device_missing);
4029 }
4030
4031 static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
4032                                        struct ddb_entry *ddb_entry,
4033                                        struct scsi_cmnd *cmd)
4034 {
4035         struct srb *srb;
4036
4037         srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
4038         if (!srb)
4039                 return srb;
4040
4041         kref_init(&srb->srb_ref);
4042         srb->ha = ha;
4043         srb->ddb = ddb_entry;
4044         srb->cmd = cmd;
4045         srb->flags = 0;
4046         CMD_SP(cmd) = (void *)srb;
4047
4048         return srb;
4049 }
4050
4051 static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
4052 {
4053         struct scsi_cmnd *cmd = srb->cmd;
4054
4055         if (srb->flags & SRB_DMA_VALID) {
4056                 scsi_dma_unmap(cmd);
4057                 srb->flags &= ~SRB_DMA_VALID;
4058         }
4059         CMD_SP(cmd) = NULL;
4060 }
4061
4062 void qla4xxx_srb_compl(struct kref *ref)
4063 {
4064         struct srb *srb = container_of(ref, struct srb, srb_ref);
4065         struct scsi_cmnd *cmd = srb->cmd;
4066         struct scsi_qla_host *ha = srb->ha;
4067
4068         qla4xxx_srb_free_dma(ha, srb);
4069
4070         mempool_free(srb, ha->srb_mempool);
4071
4072         cmd->scsi_done(cmd);
4073 }
4074
4075 /**
4076  * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
4077  * @host: scsi host
4078  * @cmd: Pointer to Linux's SCSI command structure
4079  *
4080  * Remarks:
4081  * This routine is invoked by Linux to send a SCSI command to the driver.
4082  * The mid-level driver tries to ensure that queuecommand never gets
4083  * invoked concurrently with itself or the interrupt handler (although
4084  * the interrupt handler may call this routine as part of request-
4085  * completion handling).   Unfortunely, it sometimes calls the scheduler
4086  * in interrupt context which is a big NO! NO!.
4087  **/
4088 static int qla4xxx_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
4089 {
4090         struct scsi_qla_host *ha = to_qla_host(host);
4091         struct ddb_entry *ddb_entry = cmd->device->hostdata;
4092         struct iscsi_cls_session *sess = ddb_entry->sess;
4093         struct srb *srb;
4094         int rval;
4095
4096         if (test_bit(AF_EEH_BUSY, &ha->flags)) {
4097                 if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
4098                         cmd->result = DID_NO_CONNECT << 16;
4099                 else
4100                         cmd->result = DID_REQUEUE << 16;
4101                 goto qc_fail_command;
4102         }
4103
4104         if (!sess) {
4105                 cmd->result = DID_IMM_RETRY << 16;
4106                 goto qc_fail_command;
4107         }
4108
4109         rval = iscsi_session_chkready(sess);
4110         if (rval) {
4111                 cmd->result = rval;
4112                 goto qc_fail_command;
4113         }
4114
4115         if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
4116             test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
4117             test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
4118             test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
4119             test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
4120             !test_bit(AF_ONLINE, &ha->flags) ||
4121             !test_bit(AF_LINK_UP, &ha->flags) ||
4122             test_bit(AF_LOOPBACK, &ha->flags) ||
4123             test_bit(DPC_POST_IDC_ACK, &ha->dpc_flags) ||
4124             test_bit(DPC_RESTORE_ACB, &ha->dpc_flags) ||
4125             test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
4126                 goto qc_host_busy;
4127
4128         srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd);
4129         if (!srb)
4130                 goto qc_host_busy;
4131
4132         rval = qla4xxx_send_command_to_isp(ha, srb);
4133         if (rval != QLA_SUCCESS)
4134                 goto qc_host_busy_free_sp;
4135
4136         return 0;
4137
4138 qc_host_busy_free_sp:
4139         qla4xxx_srb_free_dma(ha, srb);
4140         mempool_free(srb, ha->srb_mempool);
4141
4142 qc_host_busy:
4143         return SCSI_MLQUEUE_HOST_BUSY;
4144
4145 qc_fail_command:
4146         cmd->scsi_done(cmd);
4147
4148         return 0;
4149 }
4150
4151 /**
4152  * qla4xxx_mem_free - frees memory allocated to adapter
4153  * @ha: Pointer to host adapter structure.
4154  *
4155  * Frees memory previously allocated by qla4xxx_mem_alloc
4156  **/
4157 static void qla4xxx_mem_free(struct scsi_qla_host *ha)
4158 {
4159         if (ha->queues)
4160                 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
4161                                   ha->queues_dma);
4162
4163         if (ha->fw_dump)
4164                 vfree(ha->fw_dump);
4165
4166         ha->queues_len = 0;
4167         ha->queues = NULL;
4168         ha->queues_dma = 0;
4169         ha->request_ring = NULL;
4170         ha->request_dma = 0;
4171         ha->response_ring = NULL;
4172         ha->response_dma = 0;
4173         ha->shadow_regs = NULL;
4174         ha->shadow_regs_dma = 0;
4175         ha->fw_dump = NULL;
4176         ha->fw_dump_size = 0;
4177
4178         /* Free srb pool. */
4179         if (ha->srb_mempool)
4180                 mempool_destroy(ha->srb_mempool);
4181
4182         ha->srb_mempool = NULL;
4183
4184         if (ha->chap_dma_pool)
4185                 dma_pool_destroy(ha->chap_dma_pool);
4186
4187         if (ha->chap_list)
4188                 vfree(ha->chap_list);
4189         ha->chap_list = NULL;
4190
4191         if (ha->fw_ddb_dma_pool)
4192                 dma_pool_destroy(ha->fw_ddb_dma_pool);
4193
4194         /* release io space registers  */
4195         if (is_qla8022(ha)) {
4196                 if (ha->nx_pcibase)
4197                         iounmap(
4198                             (struct device_reg_82xx __iomem *)ha->nx_pcibase);
4199         } else if (is_qla8032(ha) || is_qla8042(ha)) {
4200                 if (ha->nx_pcibase)
4201                         iounmap(
4202                             (struct device_reg_83xx __iomem *)ha->nx_pcibase);
4203         } else if (ha->reg) {
4204                 iounmap(ha->reg);
4205         }
4206
4207         if (ha->reset_tmplt.buff)
4208                 vfree(ha->reset_tmplt.buff);
4209
4210         pci_release_regions(ha->pdev);
4211 }
4212
4213 /**
4214  * qla4xxx_mem_alloc - allocates memory for use by adapter.
4215  * @ha: Pointer to host adapter structure
4216  *
4217  * Allocates DMA memory for request and response queues. Also allocates memory
4218  * for srbs.
4219  **/
4220 static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
4221 {
4222         unsigned long align;
4223
4224         /* Allocate contiguous block of DMA memory for queues. */
4225         ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
4226                           (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
4227                           sizeof(struct shadow_regs) +
4228                           MEM_ALIGN_VALUE +
4229                           (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
4230         ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
4231                                         &ha->queues_dma, GFP_KERNEL);
4232         if (ha->queues == NULL) {
4233                 ql4_printk(KERN_WARNING, ha,
4234                     "Memory Allocation failed - queues.\n");
4235
4236                 goto mem_alloc_error_exit;
4237         }
4238         memset(ha->queues, 0, ha->queues_len);
4239
4240         /*
4241          * As per RISC alignment requirements -- the bus-address must be a
4242          * multiple of the request-ring size (in bytes).
4243          */
4244         align = 0;
4245         if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
4246                 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
4247                                            (MEM_ALIGN_VALUE - 1));
4248
4249         /* Update request and response queue pointers. */
4250         ha->request_dma = ha->queues_dma + align;
4251         ha->request_ring = (struct queue_entry *) (ha->queues + align);
4252         ha->response_dma = ha->queues_dma + align +
4253                 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
4254         ha->response_ring = (struct queue_entry *) (ha->queues + align +
4255                                                     (REQUEST_QUEUE_DEPTH *
4256                                                      QUEUE_SIZE));
4257         ha->shadow_regs_dma = ha->queues_dma + align +
4258                 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
4259                 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
4260         ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
4261                                                   (REQUEST_QUEUE_DEPTH *
4262                                                    QUEUE_SIZE) +
4263                                                   (RESPONSE_QUEUE_DEPTH *
4264                                                    QUEUE_SIZE));
4265
4266         /* Allocate memory for srb pool. */
4267         ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
4268                                          mempool_free_slab, srb_cachep);
4269         if (ha->srb_mempool == NULL) {
4270                 ql4_printk(KERN_WARNING, ha,
4271                     "Memory Allocation failed - SRB Pool.\n");
4272
4273                 goto mem_alloc_error_exit;
4274         }
4275
4276         ha->chap_dma_pool = dma_pool_create("ql4_chap", &ha->pdev->dev,
4277                                             CHAP_DMA_BLOCK_SIZE, 8, 0);
4278
4279         if (ha->chap_dma_pool == NULL) {
4280                 ql4_printk(KERN_WARNING, ha,
4281                     "%s: chap_dma_pool allocation failed..\n", __func__);
4282                 goto mem_alloc_error_exit;
4283         }
4284
4285         ha->fw_ddb_dma_pool = dma_pool_create("ql4_fw_ddb", &ha->pdev->dev,
4286                                               DDB_DMA_BLOCK_SIZE, 8, 0);
4287
4288         if (ha->fw_ddb_dma_pool == NULL) {
4289                 ql4_printk(KERN_WARNING, ha,
4290                            "%s: fw_ddb_dma_pool allocation failed..\n",
4291                            __func__);
4292                 goto mem_alloc_error_exit;
4293         }
4294
4295         return QLA_SUCCESS;
4296
4297 mem_alloc_error_exit:
4298         return QLA_ERROR;
4299 }
4300
4301 /**
4302  * qla4_8xxx_check_temp - Check the ISP82XX temperature.
4303  * @ha: adapter block pointer.
4304  *
4305  * Note: The caller should not hold the idc lock.
4306  **/
4307 static int qla4_8xxx_check_temp(struct scsi_qla_host *ha)
4308 {
4309         uint32_t temp, temp_state, temp_val;
4310         int status = QLA_SUCCESS;
4311
4312         temp = qla4_8xxx_rd_direct(ha, QLA8XXX_CRB_TEMP_STATE);
4313
4314         temp_state = qla82xx_get_temp_state(temp);
4315         temp_val = qla82xx_get_temp_val(temp);
4316
4317         if (temp_state == QLA82XX_TEMP_PANIC) {
4318                 ql4_printk(KERN_WARNING, ha, "Device temperature %d degrees C"
4319                            " exceeds maximum allowed. Hardware has been shut"
4320                            " down.\n", temp_val);
4321                 status = QLA_ERROR;
4322         } else if (temp_state == QLA82XX_TEMP_WARN) {
4323                 if (ha->temperature == QLA82XX_TEMP_NORMAL)
4324                         ql4_printk(KERN_WARNING, ha, "Device temperature %d"
4325                                    " degrees C exceeds operating range."
4326                                    " Immediate action needed.\n", temp_val);
4327         } else {
4328                 if (ha->temperature == QLA82XX_TEMP_WARN)
4329                         ql4_printk(KERN_INFO, ha, "Device temperature is"
4330                                    " now %d degrees C in normal range.\n",
4331                                    temp_val);
4332         }
4333         ha->temperature = temp_state;
4334         return status;
4335 }
4336
4337 /**
4338  * qla4_8xxx_check_fw_alive  - Check firmware health
4339  * @ha: Pointer to host adapter structure.
4340  *
4341  * Context: Interrupt
4342  **/
4343 static int qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
4344 {
4345         uint32_t fw_heartbeat_counter;
4346         int status = QLA_SUCCESS;
4347
4348         fw_heartbeat_counter = qla4_8xxx_rd_direct(ha,
4349                                                    QLA8XXX_PEG_ALIVE_COUNTER);
4350         /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
4351         if (fw_heartbeat_counter == 0xffffffff) {
4352                 DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
4353                     "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
4354                     ha->host_no, __func__));
4355                 return status;
4356         }
4357
4358         if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
4359                 ha->seconds_since_last_heartbeat++;
4360                 /* FW not alive after 2 seconds */
4361                 if (ha->seconds_since_last_heartbeat == 2) {
4362                         ha->seconds_since_last_heartbeat = 0;
4363                         qla4_8xxx_dump_peg_reg(ha);
4364                         status = QLA_ERROR;
4365                 }
4366         } else
4367                 ha->seconds_since_last_heartbeat = 0;
4368
4369         ha->fw_heartbeat_counter = fw_heartbeat_counter;
4370         return status;
4371 }
4372
4373 static void qla4_8xxx_process_fw_error(struct scsi_qla_host *ha)
4374 {
4375         uint32_t halt_status;
4376         int halt_status_unrecoverable = 0;
4377
4378         halt_status = qla4_8xxx_rd_direct(ha, QLA8XXX_PEG_HALT_STATUS1);
4379
4380         if (is_qla8022(ha)) {
4381                 ql4_printk(KERN_INFO, ha, "%s: disabling pause transmit on port 0 & 1.\n",
4382                            __func__);
4383                 qla4_82xx_wr_32(ha, QLA82XX_CRB_NIU + 0x98,
4384                                 CRB_NIU_XG_PAUSE_CTL_P0 |
4385                                 CRB_NIU_XG_PAUSE_CTL_P1);
4386
4387                 if (QLA82XX_FWERROR_CODE(halt_status) == 0x67)
4388                         ql4_printk(KERN_ERR, ha, "%s: Firmware aborted with error code 0x00006700. Device is being reset\n",
4389                                    __func__);
4390                 if (halt_status & HALT_STATUS_UNRECOVERABLE)
4391                         halt_status_unrecoverable = 1;
4392         } else if (is_qla8032(ha) || is_qla8042(ha)) {
4393                 if (halt_status & QLA83XX_HALT_STATUS_FW_RESET)
4394                         ql4_printk(KERN_ERR, ha, "%s: Firmware error detected device is being reset\n",
4395                                    __func__);
4396                 else if (halt_status & QLA83XX_HALT_STATUS_UNRECOVERABLE)
4397                         halt_status_unrecoverable = 1;
4398         }
4399
4400         /*
4401          * Since we cannot change dev_state in interrupt context,
4402          * set appropriate DPC flag then wakeup DPC
4403          */
4404         if (halt_status_unrecoverable) {
4405                 set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
4406         } else {
4407                 ql4_printk(KERN_INFO, ha, "%s: detect abort needed!\n",
4408                            __func__);
4409                 set_bit(DPC_RESET_HA, &ha->dpc_flags);
4410         }
4411         qla4xxx_mailbox_premature_completion(ha);
4412         qla4xxx_wake_dpc(ha);
4413 }
4414
4415 /**
4416  * qla4_8xxx_watchdog - Poll dev state
4417  * @ha: Pointer to host adapter structure.
4418  *
4419  * Context: Interrupt
4420  **/
4421 void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
4422 {
4423         uint32_t dev_state;
4424         uint32_t idc_ctrl;
4425
4426         if (is_qla8032(ha) &&
4427             (qla4_83xx_is_detached(ha) == QLA_SUCCESS))
4428                 WARN_ONCE(1, "%s: iSCSI function %d marked invisible\n",
4429                           __func__, ha->func_num);
4430
4431         /* don't poll if reset is going on */
4432         if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
4433             test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
4434             test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags))) {
4435                 dev_state = qla4_8xxx_rd_direct(ha, QLA8XXX_CRB_DEV_STATE);
4436
4437                 if (qla4_8xxx_check_temp(ha)) {
4438                         if (is_qla8022(ha)) {
4439                                 ql4_printk(KERN_INFO, ha, "disabling pause transmit on port 0 & 1.\n");
4440                                 qla4_82xx_wr_32(ha, QLA82XX_CRB_NIU + 0x98,
4441                                                 CRB_NIU_XG_PAUSE_CTL_P0 |
4442                                                 CRB_NIU_XG_PAUSE_CTL_P1);
4443                         }
4444                         set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
4445                         qla4xxx_wake_dpc(ha);
4446                 } else if (dev_state == QLA8XXX_DEV_NEED_RESET &&
4447                            !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
4448
4449                         ql4_printk(KERN_INFO, ha, "%s: HW State: NEED RESET!\n",
4450                                    __func__);
4451
4452                         if (is_qla8032(ha) || is_qla8042(ha)) {
4453                                 idc_ctrl = qla4_83xx_rd_reg(ha,
4454                                                         QLA83XX_IDC_DRV_CTRL);
4455                                 if (!(idc_ctrl & GRACEFUL_RESET_BIT1)) {
4456                                         ql4_printk(KERN_INFO, ha, "%s: Graceful reset bit is not set\n",
4457                                                    __func__);
4458                                         qla4xxx_mailbox_premature_completion(
4459                                                                             ha);
4460                                 }
4461                         }
4462
4463                         if ((is_qla8032(ha) || is_qla8042(ha)) ||
4464                             (is_qla8022(ha) && !ql4xdontresethba)) {
4465                                 set_bit(DPC_RESET_HA, &ha->dpc_flags);
4466                                 qla4xxx_wake_dpc(ha);
4467                         }
4468                 } else if (dev_state == QLA8XXX_DEV_NEED_QUIESCENT &&
4469                     !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
4470                         ql4_printk(KERN_INFO, ha, "%s: HW State: NEED QUIES!\n",
4471                             __func__);
4472                         set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
4473                         qla4xxx_wake_dpc(ha);
4474                 } else  {
4475                         /* Check firmware health */
4476                         if (qla4_8xxx_check_fw_alive(ha))
4477                                 qla4_8xxx_process_fw_error(ha);
4478                 }
4479         }
4480 }
4481
4482 static void qla4xxx_check_relogin_flash_ddb(struct iscsi_cls_session *cls_sess)
4483 {
4484         struct iscsi_session *sess;
4485         struct ddb_entry *ddb_entry;
4486         struct scsi_qla_host *ha;
4487
4488         sess = cls_sess->dd_data;
4489         ddb_entry = sess->dd_data;
4490         ha = ddb_entry->ha;
4491
4492         if (!(ddb_entry->ddb_type == FLASH_DDB))
4493                 return;
4494
4495         if (adapter_up(ha) && !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
4496             !iscsi_is_session_online(cls_sess)) {
4497                 if (atomic_read(&ddb_entry->retry_relogin_timer) !=
4498                     INVALID_ENTRY) {
4499                         if (atomic_read(&ddb_entry->retry_relogin_timer) ==
4500                                         0) {
4501                                 atomic_set(&ddb_entry->retry_relogin_timer,
4502                                            INVALID_ENTRY);
4503                                 set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
4504                                 set_bit(DF_RELOGIN, &ddb_entry->flags);
4505                                 DEBUG2(ql4_printk(KERN_INFO, ha,
4506                                        "%s: index [%d] login device\n",
4507                                         __func__, ddb_entry->fw_ddb_index));
4508                         } else
4509                                 atomic_dec(&ddb_entry->retry_relogin_timer);
4510                 }
4511         }
4512
4513         /* Wait for relogin to timeout */
4514         if (atomic_read(&ddb_entry->relogin_timer) &&
4515             (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
4516                 /*
4517                  * If the relogin times out and the device is
4518                  * still NOT ONLINE then try and relogin again.
4519                  */
4520                 if (!iscsi_is_session_online(cls_sess)) {
4521                         /* Reset retry relogin timer */
4522                         atomic_inc(&ddb_entry->relogin_retry_count);
4523                         DEBUG2(ql4_printk(KERN_INFO, ha,
4524                                 "%s: index[%d] relogin timed out-retrying"
4525                                 " relogin (%d), retry (%d)\n", __func__,
4526                                 ddb_entry->fw_ddb_index,
4527                                 atomic_read(&ddb_entry->relogin_retry_count),
4528                                 ddb_entry->default_time2wait + 4));
4529                         set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
4530                         atomic_set(&ddb_entry->retry_relogin_timer,
4531                                    ddb_entry->default_time2wait + 4);
4532                 }
4533         }
4534 }
4535
4536 /**
4537  * qla4xxx_timer - checks every second for work to do.
4538  * @ha: Pointer to host adapter structure.
4539  **/
4540 static void qla4xxx_timer(struct scsi_qla_host *ha)
4541 {
4542         int start_dpc = 0;
4543         uint16_t w;
4544
4545         iscsi_host_for_each_session(ha->host, qla4xxx_check_relogin_flash_ddb);
4546
4547         /* If we are in the middle of AER/EEH processing
4548          * skip any processing and reschedule the timer
4549          */
4550         if (test_bit(AF_EEH_BUSY, &ha->flags)) {
4551                 mod_timer(&ha->timer, jiffies + HZ);
4552                 return;
4553         }
4554
4555         /* Hardware read to trigger an EEH error during mailbox waits. */
4556         if (!pci_channel_offline(ha->pdev))
4557                 pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
4558
4559         if (is_qla80XX(ha))
4560                 qla4_8xxx_watchdog(ha);
4561
4562         if (is_qla40XX(ha)) {
4563                 /* Check for heartbeat interval. */
4564                 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
4565                     ha->heartbeat_interval != 0) {
4566                         ha->seconds_since_last_heartbeat++;
4567                         if (ha->seconds_since_last_heartbeat >
4568                             ha->heartbeat_interval + 2)
4569                                 set_bit(DPC_RESET_HA, &ha->dpc_flags);
4570                 }
4571         }
4572
4573         /* Process any deferred work. */
4574         if (!list_empty(&ha->work_list))
4575                 start_dpc++;
4576
4577         /* Wakeup the dpc routine for this adapter, if needed. */
4578         if (start_dpc ||
4579              test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
4580              test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
4581              test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
4582              test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
4583              test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
4584              test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
4585              test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
4586              test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
4587              test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
4588              test_bit(DPC_SYSFS_DDB_EXPORT, &ha->dpc_flags) ||
4589              test_bit(DPC_AEN, &ha->dpc_flags)) {
4590                 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
4591                               " - dpc flags = 0x%lx\n",
4592                               ha->host_no, __func__, ha->dpc_flags));
4593                 qla4xxx_wake_dpc(ha);
4594         }
4595
4596         /* Reschedule timer thread to call us back in one second */
4597         mod_timer(&ha->timer, jiffies + HZ);
4598
4599         DEBUG2(ha->seconds_since_last_intr++);
4600 }
4601
4602 /**
4603  * qla4xxx_cmd_wait - waits for all outstanding commands to complete
4604  * @ha: Pointer to host adapter structure.
4605  *
4606  * This routine stalls the driver until all outstanding commands are returned.
4607  * Caller must release the Hardware Lock prior to calling this routine.
4608  **/
4609 static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
4610 {
4611         uint32_t index = 0;
4612         unsigned long flags;
4613         struct scsi_cmnd *cmd;
4614         unsigned long wtime;
4615         uint32_t wtmo;
4616
4617         if (is_qla40XX(ha))
4618                 wtmo = WAIT_CMD_TOV;
4619         else
4620                 wtmo = ha->nx_reset_timeout / 2;
4621
4622         wtime = jiffies + (wtmo * HZ);
4623
4624         DEBUG2(ql4_printk(KERN_INFO, ha,
4625                           "Wait up to %u seconds for cmds to complete\n",
4626                           wtmo));
4627
4628         while (!time_after_eq(jiffies, wtime)) {
4629                 spin_lock_irqsave(&ha->hardware_lock, flags);
4630                 /* Find a command that hasn't completed. */
4631                 for (index = 0; index < ha->host->can_queue; index++) {
4632                         cmd = scsi_host_find_tag(ha->host, index);
4633                         /*
4634                          * We cannot just check if the index is valid,
4635                          * becase if we are run from the scsi eh, then
4636                          * the scsi/block layer is going to prevent
4637                          * the tag from being released.
4638                          */
4639                         if (cmd != NULL && CMD_SP(cmd))
4640                                 break;
4641                 }
4642                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4643
4644                 /* If No Commands are pending, wait is complete */
4645                 if (index == ha->host->can_queue)
4646                         return QLA_SUCCESS;
4647
4648                 msleep(1000);
4649         }
4650         /* If we timed out on waiting for commands to come back
4651          * return ERROR. */
4652         return QLA_ERROR;
4653 }
4654
4655 int qla4xxx_hw_reset(struct scsi_qla_host *ha)
4656 {
4657         uint32_t ctrl_status;
4658         unsigned long flags = 0;
4659
4660         DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
4661
4662         if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
4663                 return QLA_ERROR;
4664
4665         spin_lock_irqsave(&ha->hardware_lock, flags);
4666
4667         /*
4668          * If the SCSI Reset Interrupt bit is set, clear it.
4669          * Otherwise, the Soft Reset won't work.
4670          */
4671         ctrl_status = readw(&ha->reg->ctrl_status);
4672         if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
4673                 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
4674
4675         /* Issue Soft Reset */
4676         writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
4677         readl(&ha->reg->ctrl_status);
4678
4679         spin_unlock_irqrestore(&ha->hardware_lock, flags);
4680         return QLA_SUCCESS;
4681 }
4682
4683 /**
4684  * qla4xxx_soft_reset - performs soft reset.
4685  * @ha: Pointer to host adapter structure.
4686  **/
4687 int qla4xxx_soft_reset(struct scsi_qla_host *ha)
4688 {
4689         uint32_t max_wait_time;
4690         unsigned long flags = 0;
4691         int status;
4692         uint32_t ctrl_status;
4693
4694         status = qla4xxx_hw_reset(ha);
4695         if (status != QLA_SUCCESS)
4696                 return status;
4697
4698         status = QLA_ERROR;
4699         /* Wait until the Network Reset Intr bit is cleared */
4700         max_wait_time = RESET_INTR_TOV;
4701         do {
4702                 spin_lock_irqsave(&ha->hardware_lock, flags);
4703                 ctrl_status = readw(&ha->reg->ctrl_status);
4704                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4705
4706                 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
4707                         break;
4708
4709                 msleep(1000);
4710         } while ((--max_wait_time));
4711
4712         if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
4713                 DEBUG2(printk(KERN_WARNING
4714                               "scsi%ld: Network Reset Intr not cleared by "
4715                               "Network function, clearing it now!\n",
4716                               ha->host_no));
4717                 spin_lock_irqsave(&ha->hardware_lock, flags);
4718                 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
4719                 readl(&ha->reg->ctrl_status);
4720                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4721         }
4722
4723         /* Wait until the firmware tells us the Soft Reset is done */
4724         max_wait_time = SOFT_RESET_TOV;
4725         do {
4726                 spin_lock_irqsave(&ha->hardware_lock, flags);
4727                 ctrl_status = readw(&ha->reg->ctrl_status);
4728                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4729
4730                 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
4731                         status = QLA_SUCCESS;
4732                         break;
4733                 }
4734
4735                 msleep(1000);
4736         } while ((--max_wait_time));
4737
4738         /*
4739          * Also, make sure that the SCSI Reset Interrupt bit has been cleared
4740          * after the soft reset has taken place.
4741          */
4742         spin_lock_irqsave(&ha->hardware_lock, flags);
4743         ctrl_status = readw(&ha->reg->ctrl_status);
4744         if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
4745                 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
4746                 readl(&ha->reg->ctrl_status);
4747         }
4748         spin_unlock_irqrestore(&ha->hardware_lock, flags);
4749
4750         /* If soft reset fails then most probably the bios on other
4751          * function is also enabled.
4752          * Since the initialization is sequential the other fn
4753          * wont be able to acknowledge the soft reset.
4754          * Issue a force soft reset to workaround this scenario.
4755          */
4756         if (max_wait_time == 0) {
4757                 /* Issue Force Soft Reset */
4758                 spin_lock_irqsave(&ha->hardware_lock, flags);
4759                 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
4760                 readl(&ha->reg->ctrl_status);
4761                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4762                 /* Wait until the firmware tells us the Soft Reset is done */
4763                 max_wait_time = SOFT_RESET_TOV;
4764                 do {
4765                         spin_lock_irqsave(&ha->hardware_lock, flags);
4766                         ctrl_status = readw(&ha->reg->ctrl_status);
4767                         spin_unlock_irqrestore(&ha->hardware_lock, flags);
4768
4769                         if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
4770                                 status = QLA_SUCCESS;
4771                                 break;
4772                         }
4773
4774                         msleep(1000);
4775                 } while ((--max_wait_time));
4776         }
4777
4778         return status;
4779 }
4780
4781 /**
4782  * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
4783  * @ha: Pointer to host adapter structure.
4784  * @res: returned scsi status
4785  *
4786  * This routine is called just prior to a HARD RESET to return all
4787  * outstanding commands back to the Operating System.
4788  * Caller should make sure that the following locks are released
4789  * before this calling routine: Hardware lock, and io_request_lock.
4790  **/
4791 static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
4792 {
4793         struct srb *srb;
4794         int i;
4795         unsigned long flags;
4796
4797         spin_lock_irqsave(&ha->hardware_lock, flags);
4798         for (i = 0; i < ha->host->can_queue; i++) {
4799                 srb = qla4xxx_del_from_active_array(ha, i);
4800                 if (srb != NULL) {
4801                         srb->cmd->result = res;
4802                         kref_put(&srb->srb_ref, qla4xxx_srb_compl);
4803                 }
4804         }
4805         spin_unlock_irqrestore(&ha->hardware_lock, flags);
4806 }
4807
4808 void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
4809 {
4810         clear_bit(AF_ONLINE, &ha->flags);
4811
4812         /* Disable the board */
4813         ql4_printk(KERN_INFO, ha, "Disabling the board\n");
4814
4815         qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
4816         qla4xxx_mark_all_devices_missing(ha);
4817         clear_bit(AF_INIT_DONE, &ha->flags);
4818 }
4819
4820 static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session)
4821 {
4822         struct iscsi_session *sess;
4823         struct ddb_entry *ddb_entry;
4824
4825         sess = cls_session->dd_data;
4826         ddb_entry = sess->dd_data;
4827         ddb_entry->fw_ddb_device_state = DDB_DS_SESSION_FAILED;
4828
4829         if (ddb_entry->ddb_type == FLASH_DDB)
4830                 iscsi_block_session(ddb_entry->sess);
4831         else
4832                 iscsi_session_failure(cls_session->dd_data,
4833                                       ISCSI_ERR_CONN_FAILED);
4834 }
4835
4836 /**
4837  * qla4xxx_recover_adapter - recovers adapter after a fatal error
4838  * @ha: Pointer to host adapter structure.
4839  **/
4840 static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
4841 {
4842         int status = QLA_ERROR;
4843         uint8_t reset_chip = 0;
4844         uint32_t dev_state;
4845         unsigned long wait;
4846
4847         /* Stall incoming I/O until we are done */
4848         scsi_block_requests(ha->host);
4849         clear_bit(AF_ONLINE, &ha->flags);
4850         clear_bit(AF_LINK_UP, &ha->flags);
4851
4852         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
4853
4854         set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
4855
4856         if ((is_qla8032(ha) || is_qla8042(ha)) &&
4857             !test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
4858                 ql4_printk(KERN_INFO, ha, "%s: disabling pause transmit on port 0 & 1.\n",
4859                            __func__);
4860                 /* disable pause frame for ISP83xx */
4861                 qla4_83xx_disable_pause(ha);
4862         }
4863
4864         iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
4865
4866         if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
4867                 reset_chip = 1;
4868
4869         /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
4870          * do not reset adapter, jump to initialize_adapter */
4871         if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
4872                 status = QLA_SUCCESS;
4873                 goto recover_ha_init_adapter;
4874         }
4875
4876         /* For the ISP-8xxx adapter, issue a stop_firmware if invoked
4877          * from eh_host_reset or ioctl module */
4878         if (is_qla80XX(ha) && !reset_chip &&
4879             test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
4880
4881                 DEBUG2(ql4_printk(KERN_INFO, ha,
4882                     "scsi%ld: %s - Performing stop_firmware...\n",
4883                     ha->host_no, __func__));
4884                 status = ha->isp_ops->reset_firmware(ha);
4885                 if (status == QLA_SUCCESS) {
4886                         ha->isp_ops->disable_intrs(ha);
4887                         qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
4888                         qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
4889                 } else {
4890                         /* If the stop_firmware fails then
4891                          * reset the entire chip */
4892                         reset_chip = 1;
4893                         clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
4894                         set_bit(DPC_RESET_HA, &ha->dpc_flags);
4895                 }
4896         }
4897
4898         /* Issue full chip reset if recovering from a catastrophic error,
4899          * or if stop_firmware fails for ISP-8xxx.
4900          * This is the default case for ISP-4xxx */
4901         if (is_qla40XX(ha) || reset_chip) {
4902                 if (is_qla40XX(ha))
4903                         goto chip_reset;
4904
4905                 /* Check if 8XXX firmware is alive or not
4906                  * We may have arrived here from NEED_RESET
4907                  * detection only */
4908                 if (test_bit(AF_FW_RECOVERY, &ha->flags))
4909                         goto chip_reset;
4910
4911                 wait = jiffies + (FW_ALIVE_WAIT_TOV * HZ);
4912                 while (time_before(jiffies, wait)) {
4913                         if (qla4_8xxx_check_fw_alive(ha)) {
4914                                 qla4xxx_mailbox_premature_completion(ha);
4915                                 break;
4916                         }
4917
4918                         set_current_state(TASK_UNINTERRUPTIBLE);
4919                         schedule_timeout(HZ);
4920                 }
4921 chip_reset:
4922                 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
4923                         qla4xxx_cmd_wait(ha);
4924
4925                 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
4926                 DEBUG2(ql4_printk(KERN_INFO, ha,
4927                     "scsi%ld: %s - Performing chip reset..\n",
4928                     ha->host_no, __func__));
4929                 status = ha->isp_ops->reset_chip(ha);
4930                 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
4931         }
4932
4933         /* Flush any pending ddb changed AENs */
4934         qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
4935
4936 recover_ha_init_adapter:
4937         /* Upon successful firmware/chip reset, re-initialize the adapter */
4938         if (status == QLA_SUCCESS) {
4939                 /* For ISP-4xxx, force function 1 to always initialize
4940                  * before function 3 to prevent both funcions from
4941                  * stepping on top of the other */
4942                 if (is_qla40XX(ha) && (ha->mac_index == 3))
4943                         ssleep(6);
4944
4945                 /* NOTE: AF_ONLINE flag set upon successful completion of
4946                  * qla4xxx_initialize_adapter */
4947                 status = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
4948                 if (is_qla80XX(ha) && (status == QLA_ERROR)) {
4949                         status = qla4_8xxx_check_init_adapter_retry(ha);
4950                         if (status == QLA_ERROR) {
4951                                 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Don't retry recover adapter\n",
4952                                            ha->host_no, __func__);
4953                                 qla4xxx_dead_adapter_cleanup(ha);
4954                                 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
4955                                 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
4956                                 clear_bit(DPC_RESET_HA_FW_CONTEXT,
4957                                           &ha->dpc_flags);
4958                                 goto exit_recover;
4959                         }
4960                 }
4961         }
4962
4963         /* Retry failed adapter initialization, if necessary
4964          * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
4965          * case to prevent ping-pong resets between functions */
4966         if (!test_bit(AF_ONLINE, &ha->flags) &&
4967             !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
4968                 /* Adapter initialization failed, see if we can retry
4969                  * resetting the ha.
4970                  * Since we don't want to block the DPC for too long
4971                  * with multiple resets in the same thread,
4972                  * utilize DPC to retry */
4973                 if (is_qla80XX(ha)) {
4974                         ha->isp_ops->idc_lock(ha);
4975                         dev_state = qla4_8xxx_rd_direct(ha,
4976                                                         QLA8XXX_CRB_DEV_STATE);
4977                         ha->isp_ops->idc_unlock(ha);
4978                         if (dev_state == QLA8XXX_DEV_FAILED) {
4979                                 ql4_printk(KERN_INFO, ha, "%s: don't retry "
4980                                            "recover adapter. H/W is in Failed "
4981                                            "state\n", __func__);
4982                                 qla4xxx_dead_adapter_cleanup(ha);
4983                                 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
4984                                 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
4985                                 clear_bit(DPC_RESET_HA_FW_CONTEXT,
4986                                                 &ha->dpc_flags);
4987                                 status = QLA_ERROR;
4988
4989                                 goto exit_recover;
4990                         }
4991                 }
4992
4993                 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
4994                         ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
4995                         DEBUG2(printk("scsi%ld: recover adapter - retrying "
4996                                       "(%d) more times\n", ha->host_no,
4997                                       ha->retry_reset_ha_cnt));
4998                         set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
4999                         status = QLA_ERROR;
5000                 } else {
5001                         if (ha->retry_reset_ha_cnt > 0) {
5002                                 /* Schedule another Reset HA--DPC will retry */
5003                                 ha->retry_reset_ha_cnt--;
5004                                 DEBUG2(printk("scsi%ld: recover adapter - "
5005                                               "retry remaining %d\n",
5006                                               ha->host_no,
5007                                               ha->retry_reset_ha_cnt));
5008                                 status = QLA_ERROR;
5009                         }
5010
5011                         if (ha->retry_reset_ha_cnt == 0) {
5012                                 /* Recover adapter retries have been exhausted.
5013                                  * Adapter DEAD */
5014                                 DEBUG2(printk("scsi%ld: recover adapter "
5015                                               "failed - board disabled\n",
5016                                               ha->host_no));
5017                                 qla4xxx_dead_adapter_cleanup(ha);
5018                                 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
5019                                 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
5020                                 clear_bit(DPC_RESET_HA_FW_CONTEXT,
5021                                           &ha->dpc_flags);
5022                                 status = QLA_ERROR;
5023                         }
5024                 }
5025         } else {
5026                 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
5027                 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
5028                 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
5029         }
5030
5031 exit_recover:
5032         ha->adapter_error_count++;
5033
5034         if (test_bit(AF_ONLINE, &ha->flags))
5035                 ha->isp_ops->enable_intrs(ha);
5036
5037         scsi_unblock_requests(ha->host);
5038
5039         clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
5040         DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
5041             status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
5042
5043         return status;
5044 }
5045
5046 static void qla4xxx_relogin_devices(struct iscsi_cls_session *cls_session)
5047 {
5048         struct iscsi_session *sess;
5049         struct ddb_entry *ddb_entry;
5050         struct scsi_qla_host *ha;
5051
5052         sess = cls_session->dd_data;
5053         ddb_entry = sess->dd_data;
5054         ha = ddb_entry->ha;
5055         if (!iscsi_is_session_online(cls_session)) {
5056                 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
5057                         ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
5058                                    " unblock session\n", ha->host_no, __func__,
5059                                    ddb_entry->fw_ddb_index);
5060                         iscsi_unblock_session(ddb_entry->sess);
5061                 } else {
5062                         /* Trigger relogin */
5063                         if (ddb_entry->ddb_type == FLASH_DDB) {
5064                                 if (!(test_bit(DF_RELOGIN, &ddb_entry->flags) ||
5065                                       test_bit(DF_DISABLE_RELOGIN,
5066                                                &ddb_entry->flags)))
5067                                         qla4xxx_arm_relogin_timer(ddb_entry);
5068                         } else
5069                                 iscsi_session_failure(cls_session->dd_data,
5070                                                       ISCSI_ERR_CONN_FAILED);
5071                 }
5072         }
5073 }
5074
5075 int qla4xxx_unblock_flash_ddb(struct iscsi_cls_session *cls_session)
5076 {
5077         struct iscsi_session *sess;
5078         struct ddb_entry *ddb_entry;
5079         struct scsi_qla_host *ha;
5080
5081         sess = cls_session->dd_data;
5082         ddb_entry = sess->dd_data;
5083         ha = ddb_entry->ha;
5084         ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
5085                    " unblock session\n", ha->host_no, __func__,
5086                    ddb_entry->fw_ddb_index);
5087
5088         iscsi_unblock_session(ddb_entry->sess);
5089
5090         /* Start scan target */
5091         if (test_bit(AF_ONLINE, &ha->flags)) {
5092                 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
5093                            " start scan\n", ha->host_no, __func__,
5094                            ddb_entry->fw_ddb_index);
5095                 scsi_queue_work(ha->host, &ddb_entry->sess->scan_work);
5096         }
5097         return QLA_SUCCESS;
5098 }
5099
5100 int qla4xxx_unblock_ddb(struct iscsi_cls_session *cls_session)
5101 {
5102         struct iscsi_session *sess;
5103         struct ddb_entry *ddb_entry;
5104         struct scsi_qla_host *ha;
5105         int status = QLA_SUCCESS;
5106
5107         sess = cls_session->dd_data;
5108         ddb_entry = sess->dd_data;
5109         ha = ddb_entry->ha;
5110         ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
5111                    " unblock user space session\n", ha->host_no, __func__,
5112                    ddb_entry->fw_ddb_index);
5113
5114         if (!iscsi_is_session_online(cls_session)) {
5115                 iscsi_conn_start(ddb_entry->conn);
5116                 iscsi_conn_login_event(ddb_entry->conn,
5117                                        ISCSI_CONN_STATE_LOGGED_IN);
5118         } else {
5119                 ql4_printk(KERN_INFO, ha,
5120                            "scsi%ld: %s: ddb[%d] session [%d] already logged in\n",
5121                            ha->host_no, __func__, ddb_entry->fw_ddb_index,
5122                            cls_session->sid);
5123                 status = QLA_ERROR;
5124         }
5125
5126         return status;
5127 }
5128
5129 static void qla4xxx_relogin_all_devices(struct scsi_qla_host *ha)
5130 {
5131         iscsi_host_for_each_session(ha->host, qla4xxx_relogin_devices);
5132 }
5133
5134 static void qla4xxx_relogin_flash_ddb(struct iscsi_cls_session *cls_sess)
5135 {
5136         uint16_t relogin_timer;
5137         struct iscsi_session *sess;
5138         struct ddb_entry *ddb_entry;
5139         struct scsi_qla_host *ha;
5140
5141         sess = cls_sess->dd_data;
5142         ddb_entry = sess->dd_data;
5143         ha = ddb_entry->ha;
5144
5145         relogin_timer = max(ddb_entry->default_relogin_timeout,
5146                             (uint16_t)RELOGIN_TOV);
5147         atomic_set(&ddb_entry->relogin_timer, relogin_timer);
5148
5149         DEBUG2(ql4_printk(KERN_INFO, ha,
5150                           "scsi%ld: Relogin index [%d]. TOV=%d\n", ha->host_no,
5151                           ddb_entry->fw_ddb_index, relogin_timer));
5152
5153         qla4xxx_login_flash_ddb(cls_sess);
5154 }
5155
5156 static void qla4xxx_dpc_relogin(struct iscsi_cls_session *cls_sess)
5157 {
5158         struct iscsi_session *sess;
5159         struct ddb_entry *ddb_entry;
5160         struct scsi_qla_host *ha;
5161
5162         sess = cls_sess->dd_data;
5163         ddb_entry = sess->dd_data;
5164         ha = ddb_entry->ha;
5165
5166         if (!(ddb_entry->ddb_type == FLASH_DDB))
5167                 return;
5168
5169         if (test_bit(DF_DISABLE_RELOGIN, &ddb_entry->flags))
5170                 return;
5171
5172         if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
5173             !iscsi_is_session_online(cls_sess)) {
5174                 DEBUG2(ql4_printk(KERN_INFO, ha,
5175                                   "relogin issued\n"));
5176                 qla4xxx_relogin_flash_ddb(cls_sess);
5177         }
5178 }
5179
5180 void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
5181 {
5182         if (ha->dpc_thread)
5183                 queue_work(ha->dpc_thread, &ha->dpc_work);
5184 }
5185
5186 static struct qla4_work_evt *
5187 qla4xxx_alloc_work(struct scsi_qla_host *ha, uint32_t data_size,
5188                    enum qla4_work_type type)
5189 {
5190         struct qla4_work_evt *e;
5191         uint32_t size = sizeof(struct qla4_work_evt) + data_size;
5192
5193         e = kzalloc(size, GFP_ATOMIC);
5194         if (!e)
5195                 return NULL;
5196
5197         INIT_LIST_HEAD(&e->list);
5198         e->type = type;
5199         return e;
5200 }
5201
5202 static void qla4xxx_post_work(struct scsi_qla_host *ha,
5203                              struct qla4_work_evt *e)
5204 {
5205         unsigned long flags;
5206
5207         spin_lock_irqsave(&ha->work_lock, flags);
5208         list_add_tail(&e->list, &ha->work_list);
5209         spin_unlock_irqrestore(&ha->work_lock, flags);
5210         qla4xxx_wake_dpc(ha);
5211 }
5212
5213 int qla4xxx_post_aen_work(struct scsi_qla_host *ha,
5214                           enum iscsi_host_event_code aen_code,
5215                           uint32_t data_size, uint8_t *data)
5216 {
5217         struct qla4_work_evt *e;
5218
5219         e = qla4xxx_alloc_work(ha, data_size, QLA4_EVENT_AEN);
5220         if (!e)
5221                 return QLA_ERROR;
5222
5223         e->u.aen.code = aen_code;
5224         e->u.aen.data_size = data_size;
5225         memcpy(e->u.aen.data, data, data_size);
5226
5227         qla4xxx_post_work(ha, e);
5228
5229         return QLA_SUCCESS;
5230 }
5231
5232 int qla4xxx_post_ping_evt_work(struct scsi_qla_host *ha,
5233                                uint32_t status, uint32_t pid,
5234                                uint32_t data_size, uint8_t *data)
5235 {
5236         struct qla4_work_evt *e;
5237
5238         e = qla4xxx_alloc_work(ha, data_size, QLA4_EVENT_PING_STATUS);
5239         if (!e)
5240                 return QLA_ERROR;
5241
5242         e->u.ping.status = status;
5243         e->u.ping.pid = pid;
5244         e->u.ping.data_size = data_size;
5245         memcpy(e->u.ping.data, data, data_size);
5246
5247         qla4xxx_post_work(ha, e);
5248
5249         return QLA_SUCCESS;
5250 }
5251
5252 static void qla4xxx_do_work(struct scsi_qla_host *ha)
5253 {
5254         struct qla4_work_evt *e, *tmp;
5255         unsigned long flags;
5256         LIST_HEAD(work);
5257
5258         spin_lock_irqsave(&ha->work_lock, flags);
5259         list_splice_init(&ha->work_list, &work);
5260         spin_unlock_irqrestore(&ha->work_lock, flags);
5261
5262         list_for_each_entry_safe(e, tmp, &work, list) {
5263                 list_del_init(&e->list);
5264
5265                 switch (e->type) {
5266                 case QLA4_EVENT_AEN:
5267                         iscsi_post_host_event(ha->host_no,
5268                                               &qla4xxx_iscsi_transport,
5269                                               e->u.aen.code,
5270                                               e->u.aen.data_size,
5271                                               e->u.aen.data);
5272                         break;
5273                 case QLA4_EVENT_PING_STATUS:
5274                         iscsi_ping_comp_event(ha->host_no,
5275                                               &qla4xxx_iscsi_transport,
5276                                               e->u.ping.status,
5277                                               e->u.ping.pid,
5278                                               e->u.ping.data_size,
5279                                               e->u.ping.data);
5280                         break;
5281                 default:
5282                         ql4_printk(KERN_WARNING, ha, "event type: 0x%x not "
5283                                    "supported", e->type);
5284                 }
5285                 kfree(e);
5286         }
5287 }
5288
5289 /**
5290  * qla4xxx_do_dpc - dpc routine
5291  * @data: in our case pointer to adapter structure
5292  *
5293  * This routine is a task that is schedule by the interrupt handler
5294  * to perform the background processing for interrupts.  We put it
5295  * on a task queue that is consumed whenever the scheduler runs; that's
5296  * so you can do anything (i.e. put the process to sleep etc).  In fact,
5297  * the mid-level tries to sleep when it reaches the driver threshold
5298  * "host->can_queue". This can cause a panic if we were in our interrupt code.
5299  **/
5300 static void qla4xxx_do_dpc(struct work_struct *work)
5301 {
5302         struct scsi_qla_host *ha =
5303                 container_of(work, struct scsi_qla_host, dpc_work);
5304         int status = QLA_ERROR;
5305
5306         DEBUG2(ql4_printk(KERN_INFO, ha,
5307                           "scsi%ld: %s: DPC handler waking up. flags = 0x%08lx, dpc_flags = 0x%08lx\n",
5308                           ha->host_no, __func__, ha->flags, ha->dpc_flags));
5309
5310         /* Initialization not yet finished. Don't do anything yet. */
5311         if (!test_bit(AF_INIT_DONE, &ha->flags))
5312                 return;
5313
5314         if (test_bit(AF_EEH_BUSY, &ha->flags)) {
5315                 DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
5316                     ha->host_no, __func__, ha->flags));
5317                 return;
5318         }
5319
5320         /* post events to application */
5321         qla4xxx_do_work(ha);
5322
5323         if (is_qla80XX(ha)) {
5324                 if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
5325                         if (is_qla8032(ha) || is_qla8042(ha)) {
5326                                 ql4_printk(KERN_INFO, ha, "%s: disabling pause transmit on port 0 & 1.\n",
5327                                            __func__);
5328                                 /* disable pause frame for ISP83xx */
5329                                 qla4_83xx_disable_pause(ha);
5330                         }
5331
5332                         ha->isp_ops->idc_lock(ha);
5333                         qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
5334                                             QLA8XXX_DEV_FAILED);
5335                         ha->isp_ops->idc_unlock(ha);
5336                         ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
5337                         qla4_8xxx_device_state_handler(ha);
5338                 }
5339
5340                 if (test_bit(DPC_POST_IDC_ACK, &ha->dpc_flags)) {
5341                         if (is_qla8042(ha)) {
5342                                 if (ha->idc_info.info2 &
5343                                     ENABLE_INTERNAL_LOOPBACK) {
5344                                         ql4_printk(KERN_INFO, ha, "%s: Disabling ACB\n",
5345                                                    __func__);
5346                                         status = qla4_84xx_config_acb(ha,
5347                                                             ACB_CONFIG_DISABLE);
5348                                         if (status != QLA_SUCCESS) {
5349                                                 ql4_printk(KERN_INFO, ha, "%s: ACB config failed\n",
5350                                                            __func__);
5351                                         }
5352                                 }
5353                         }
5354                         qla4_83xx_post_idc_ack(ha);
5355                         clear_bit(DPC_POST_IDC_ACK, &ha->dpc_flags);
5356                 }
5357
5358                 if (is_qla8042(ha) &&
5359                     test_bit(DPC_RESTORE_ACB, &ha->dpc_flags)) {
5360                         ql4_printk(KERN_INFO, ha, "%s: Restoring ACB\n",
5361                                    __func__);
5362                         if (qla4_84xx_config_acb(ha, ACB_CONFIG_SET) !=
5363                             QLA_SUCCESS) {
5364                                 ql4_printk(KERN_INFO, ha, "%s: ACB config failed ",
5365                                            __func__);
5366                         }
5367                         clear_bit(DPC_RESTORE_ACB, &ha->dpc_flags);
5368                 }
5369
5370                 if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
5371                         qla4_8xxx_need_qsnt_handler(ha);
5372                 }
5373         }
5374
5375         if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
5376             (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
5377             test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
5378             test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
5379                 if ((is_qla8022(ha) && ql4xdontresethba) ||
5380                     ((is_qla8032(ha) || is_qla8042(ha)) &&
5381                      qla4_83xx_idc_dontreset(ha))) {
5382                         DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
5383                             ha->host_no, __func__));
5384                         clear_bit(DPC_RESET_HA, &ha->dpc_flags);
5385                         clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
5386                         clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
5387                         goto dpc_post_reset_ha;
5388                 }
5389                 if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
5390                     test_bit(DPC_RESET_HA, &ha->dpc_flags))
5391                         qla4xxx_recover_adapter(ha);
5392
5393                 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
5394                         uint8_t wait_time = RESET_INTR_TOV;
5395
5396                         while ((readw(&ha->reg->ctrl_status) &
5397                                 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
5398                                 if (--wait_time == 0)
5399                                         break;
5400                                 msleep(1000);
5401                         }
5402                         if (wait_time == 0)
5403                                 DEBUG2(printk("scsi%ld: %s: SR|FSR "
5404                                               "bit not cleared-- resetting\n",
5405                                               ha->host_no, __func__));
5406                         qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
5407                         if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
5408                                 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
5409                                 status = qla4xxx_recover_adapter(ha);
5410                         }
5411                         clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
5412                         if (status == QLA_SUCCESS)
5413                                 ha->isp_ops->enable_intrs(ha);
5414                 }
5415         }
5416
5417 dpc_post_reset_ha:
5418         /* ---- process AEN? --- */
5419         if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
5420                 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
5421
5422         /* ---- Get DHCP IP Address? --- */
5423         if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
5424                 qla4xxx_get_dhcp_ip_address(ha);
5425
5426         /* ---- relogin device? --- */
5427         if (adapter_up(ha) &&
5428             test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
5429                 iscsi_host_for_each_session(ha->host, qla4xxx_dpc_relogin);
5430         }
5431
5432         /* ---- link change? --- */
5433         if (!test_bit(AF_LOOPBACK, &ha->flags) &&
5434             test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
5435                 if (!test_bit(AF_LINK_UP, &ha->flags)) {
5436                         /* ---- link down? --- */
5437                         qla4xxx_mark_all_devices_missing(ha);
5438                 } else {
5439                         /* ---- link up? --- *
5440                          * F/W will auto login to all devices ONLY ONCE after
5441                          * link up during driver initialization and runtime
5442                          * fatal error recovery.  Therefore, the driver must
5443                          * manually relogin to devices when recovering from
5444                          * connection failures, logouts, expired KATO, etc. */
5445                         if (test_and_clear_bit(AF_BUILD_DDB_LIST, &ha->flags)) {
5446                                 qla4xxx_build_ddb_list(ha, ha->is_reset);
5447                                 iscsi_host_for_each_session(ha->host,
5448                                                 qla4xxx_login_flash_ddb);
5449                         } else
5450                                 qla4xxx_relogin_all_devices(ha);
5451                 }
5452         }
5453         if (test_and_clear_bit(DPC_SYSFS_DDB_EXPORT, &ha->dpc_flags)) {
5454                 if (qla4xxx_sysfs_ddb_export(ha))
5455                         ql4_printk(KERN_ERR, ha, "%s: Error exporting ddb to sysfs\n",
5456                                    __func__);
5457         }
5458 }
5459
5460 /**
5461  * qla4xxx_free_adapter - release the adapter
5462  * @ha: pointer to adapter structure
5463  **/
5464 static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
5465 {
5466         qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
5467
5468         /* Turn-off interrupts on the card. */
5469         ha->isp_ops->disable_intrs(ha);
5470
5471         if (is_qla40XX(ha)) {
5472                 writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
5473                        &ha->reg->ctrl_status);
5474                 readl(&ha->reg->ctrl_status);
5475         } else if (is_qla8022(ha)) {
5476                 writel(0, &ha->qla4_82xx_reg->host_int);
5477                 readl(&ha->qla4_82xx_reg->host_int);
5478         } else if (is_qla8032(ha) || is_qla8042(ha)) {
5479                 writel(0, &ha->qla4_83xx_reg->risc_intr);
5480                 readl(&ha->qla4_83xx_reg->risc_intr);
5481         }
5482
5483         /* Remove timer thread, if present */
5484         if (ha->timer_active)
5485                 qla4xxx_stop_timer(ha);
5486
5487         /* Kill the kernel thread for this host */
5488         if (ha->dpc_thread)
5489                 destroy_workqueue(ha->dpc_thread);
5490
5491         /* Kill the kernel thread for this host */
5492         if (ha->task_wq)
5493                 destroy_workqueue(ha->task_wq);
5494
5495         /* Put firmware in known state */
5496         ha->isp_ops->reset_firmware(ha);
5497
5498         if (is_qla80XX(ha)) {
5499                 ha->isp_ops->idc_lock(ha);
5500                 qla4_8xxx_clear_drv_active(ha);
5501                 ha->isp_ops->idc_unlock(ha);
5502         }
5503
5504         /* Detach interrupts */
5505         qla4xxx_free_irqs(ha);
5506
5507         /* free extra memory */
5508         qla4xxx_mem_free(ha);
5509 }
5510
5511 int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
5512 {
5513         int status = 0;
5514         unsigned long mem_base, mem_len, db_base, db_len;
5515         struct pci_dev *pdev = ha->pdev;
5516
5517         status = pci_request_regions(pdev, DRIVER_NAME);
5518         if (status) {
5519                 printk(KERN_WARNING
5520                     "scsi(%ld) Failed to reserve PIO regions (%s) "
5521                     "status=%d\n", ha->host_no, pci_name(pdev), status);
5522                 goto iospace_error_exit;
5523         }
5524
5525         DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
5526             __func__, pdev->revision));
5527         ha->revision_id = pdev->revision;
5528
5529         /* remap phys address */
5530         mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
5531         mem_len = pci_resource_len(pdev, 0);
5532         DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
5533             __func__, mem_base, mem_len));
5534
5535         /* mapping of pcibase pointer */
5536         ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
5537         if (!ha->nx_pcibase) {
5538                 printk(KERN_ERR
5539                     "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
5540                 pci_release_regions(ha->pdev);
5541                 goto iospace_error_exit;
5542         }
5543
5544         /* Mapping of IO base pointer, door bell read and write pointer */
5545
5546         /* mapping of IO base pointer */
5547         if (is_qla8022(ha)) {
5548                 ha->qla4_82xx_reg = (struct device_reg_82xx  __iomem *)
5549                                     ((uint8_t *)ha->nx_pcibase + 0xbc000 +
5550                                      (ha->pdev->devfn << 11));
5551                 ha->nx_db_wr_ptr = (ha->pdev->devfn == 4 ? QLA82XX_CAM_RAM_DB1 :
5552                                     QLA82XX_CAM_RAM_DB2);
5553         } else if (is_qla8032(ha) || is_qla8042(ha)) {
5554                 ha->qla4_83xx_reg = (struct device_reg_83xx __iomem *)
5555                                     ((uint8_t *)ha->nx_pcibase);
5556         }
5557
5558         db_base = pci_resource_start(pdev, 4);  /* doorbell is on bar 4 */
5559         db_len = pci_resource_len(pdev, 4);
5560
5561         return 0;
5562 iospace_error_exit:
5563         return -ENOMEM;
5564 }
5565
5566 /***
5567  * qla4xxx_iospace_config - maps registers
5568  * @ha: pointer to adapter structure
5569  *
5570  * This routines maps HBA's registers from the pci address space
5571  * into the kernel virtual address space for memory mapped i/o.
5572  **/
5573 int qla4xxx_iospace_config(struct scsi_qla_host *ha)
5574 {
5575         unsigned long pio, pio_len, pio_flags;
5576         unsigned long mmio, mmio_len, mmio_flags;
5577
5578         pio = pci_resource_start(ha->pdev, 0);
5579         pio_len = pci_resource_len(ha->pdev, 0);
5580         pio_flags = pci_resource_flags(ha->pdev, 0);
5581         if (pio_flags & IORESOURCE_IO) {
5582                 if (pio_len < MIN_IOBASE_LEN) {
5583                         ql4_printk(KERN_WARNING, ha,
5584                                 "Invalid PCI I/O region size\n");
5585                         pio = 0;
5586                 }
5587         } else {
5588                 ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
5589                 pio = 0;
5590         }
5591
5592         /* Use MMIO operations for all accesses. */
5593         mmio = pci_resource_start(ha->pdev, 1);
5594         mmio_len = pci_resource_len(ha->pdev, 1);
5595         mmio_flags = pci_resource_flags(ha->pdev, 1);
5596
5597         if (!(mmio_flags & IORESOURCE_MEM)) {
5598                 ql4_printk(KERN_ERR, ha,
5599                     "region #0 not an MMIO resource, aborting\n");
5600
5601                 goto iospace_error_exit;
5602         }
5603
5604         if (mmio_len < MIN_IOBASE_LEN) {
5605                 ql4_printk(KERN_ERR, ha,
5606                     "Invalid PCI mem region size, aborting\n");
5607                 goto iospace_error_exit;
5608         }
5609
5610         if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
5611                 ql4_printk(KERN_WARNING, ha,
5612                     "Failed to reserve PIO/MMIO regions\n");
5613
5614                 goto iospace_error_exit;
5615         }
5616
5617         ha->pio_address = pio;
5618         ha->pio_length = pio_len;
5619         ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
5620         if (!ha->reg) {
5621                 ql4_printk(KERN_ERR, ha,
5622                     "cannot remap MMIO, aborting\n");
5623
5624                 goto iospace_error_exit;
5625         }
5626
5627         return 0;
5628
5629 iospace_error_exit:
5630         return -ENOMEM;
5631 }
5632
5633 static struct isp_operations qla4xxx_isp_ops = {
5634         .iospace_config         = qla4xxx_iospace_config,
5635         .pci_config             = qla4xxx_pci_config,
5636         .disable_intrs          = qla4xxx_disable_intrs,
5637         .enable_intrs           = qla4xxx_enable_intrs,
5638         .start_firmware         = qla4xxx_start_firmware,
5639         .intr_handler           = qla4xxx_intr_handler,
5640         .interrupt_service_routine = qla4xxx_interrupt_service_routine,
5641         .reset_chip             = qla4xxx_soft_reset,
5642         .reset_firmware         = qla4xxx_hw_reset,
5643         .queue_iocb             = qla4xxx_queue_iocb,
5644         .complete_iocb          = qla4xxx_complete_iocb,
5645         .rd_shdw_req_q_out      = qla4xxx_rd_shdw_req_q_out,
5646         .rd_shdw_rsp_q_in       = qla4xxx_rd_shdw_rsp_q_in,
5647         .get_sys_info           = qla4xxx_get_sys_info,
5648         .queue_mailbox_command  = qla4xxx_queue_mbox_cmd,
5649         .process_mailbox_interrupt = qla4xxx_process_mbox_intr,
5650 };
5651
5652 static struct isp_operations qla4_82xx_isp_ops = {
5653         .iospace_config         = qla4_8xxx_iospace_config,
5654         .pci_config             = qla4_8xxx_pci_config,
5655         .disable_intrs          = qla4_82xx_disable_intrs,
5656         .enable_intrs           = qla4_82xx_enable_intrs,
5657         .start_firmware         = qla4_8xxx_load_risc,
5658         .restart_firmware       = qla4_82xx_try_start_fw,
5659         .intr_handler           = qla4_82xx_intr_handler,
5660         .interrupt_service_routine = qla4_82xx_interrupt_service_routine,
5661         .need_reset             = qla4_8xxx_need_reset,
5662         .reset_chip             = qla4_82xx_isp_reset,
5663         .reset_firmware         = qla4_8xxx_stop_firmware,
5664         .queue_iocb             = qla4_82xx_queue_iocb,
5665         .complete_iocb          = qla4_82xx_complete_iocb,
5666         .rd_shdw_req_q_out      = qla4_82xx_rd_shdw_req_q_out,
5667         .rd_shdw_rsp_q_in       = qla4_82xx_rd_shdw_rsp_q_in,
5668         .get_sys_info           = qla4_8xxx_get_sys_info,
5669         .rd_reg_direct          = qla4_82xx_rd_32,
5670         .wr_reg_direct          = qla4_82xx_wr_32,
5671         .rd_reg_indirect        = qla4_82xx_md_rd_32,
5672         .wr_reg_indirect        = qla4_82xx_md_wr_32,
5673         .idc_lock               = qla4_82xx_idc_lock,
5674         .idc_unlock             = qla4_82xx_idc_unlock,
5675         .rom_lock_recovery      = qla4_82xx_rom_lock_recovery,
5676         .queue_mailbox_command  = qla4_82xx_queue_mbox_cmd,
5677         .process_mailbox_interrupt = qla4_82xx_process_mbox_intr,
5678 };
5679
5680 static struct isp_operations qla4_83xx_isp_ops = {
5681         .iospace_config         = qla4_8xxx_iospace_config,
5682         .pci_config             = qla4_8xxx_pci_config,
5683         .disable_intrs          = qla4_83xx_disable_intrs,
5684         .enable_intrs           = qla4_83xx_enable_intrs,
5685         .start_firmware         = qla4_8xxx_load_risc,
5686         .restart_firmware       = qla4_83xx_start_firmware,
5687         .intr_handler           = qla4_83xx_intr_handler,
5688         .interrupt_service_routine = qla4_83xx_interrupt_service_routine,
5689         .need_reset             = qla4_8xxx_need_reset,
5690         .reset_chip             = qla4_83xx_isp_reset,
5691         .reset_firmware         = qla4_8xxx_stop_firmware,
5692         .queue_iocb             = qla4_83xx_queue_iocb,
5693         .complete_iocb          = qla4_83xx_complete_iocb,
5694         .rd_shdw_req_q_out      = qla4xxx_rd_shdw_req_q_out,
5695         .rd_shdw_rsp_q_in       = qla4xxx_rd_shdw_rsp_q_in,
5696         .get_sys_info           = qla4_8xxx_get_sys_info,
5697         .rd_reg_direct          = qla4_83xx_rd_reg,
5698         .wr_reg_direct          = qla4_83xx_wr_reg,
5699         .rd_reg_indirect        = qla4_83xx_rd_reg_indirect,
5700         .wr_reg_indirect        = qla4_83xx_wr_reg_indirect,
5701         .idc_lock               = qla4_83xx_drv_lock,
5702         .idc_unlock             = qla4_83xx_drv_unlock,
5703         .rom_lock_recovery      = qla4_83xx_rom_lock_recovery,
5704         .queue_mailbox_command  = qla4_83xx_queue_mbox_cmd,
5705         .process_mailbox_interrupt = qla4_83xx_process_mbox_intr,
5706 };
5707
5708 uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
5709 {
5710         return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
5711 }
5712
5713 uint16_t qla4_82xx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
5714 {
5715         return (uint16_t)le32_to_cpu(readl(&ha->qla4_82xx_reg->req_q_out));
5716 }
5717
5718 uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
5719 {
5720         return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
5721 }
5722
5723 uint16_t qla4_82xx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
5724 {
5725         return (uint16_t)le32_to_cpu(readl(&ha->qla4_82xx_reg->rsp_q_in));
5726 }
5727
5728 static ssize_t qla4xxx_show_boot_eth_info(void *data, int type, char *buf)
5729 {
5730         struct scsi_qla_host *ha = data;
5731         char *str = buf;
5732         int rc;
5733
5734         switch (type) {
5735         case ISCSI_BOOT_ETH_FLAGS:
5736                 rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
5737                 break;
5738         case ISCSI_BOOT_ETH_INDEX:
5739                 rc = sprintf(str, "0\n");
5740                 break;
5741         case ISCSI_BOOT_ETH_MAC:
5742                 rc = sysfs_format_mac(str, ha->my_mac,
5743                                       MAC_ADDR_LEN);
5744                 break;
5745         default:
5746                 rc = -ENOSYS;
5747                 break;
5748         }
5749         return rc;
5750 }
5751
5752 static umode_t qla4xxx_eth_get_attr_visibility(void *data, int type)
5753 {
5754         int rc;
5755
5756         switch (type) {
5757         case ISCSI_BOOT_ETH_FLAGS:
5758         case ISCSI_BOOT_ETH_MAC:
5759         case ISCSI_BOOT_ETH_INDEX:
5760                 rc = S_IRUGO;
5761                 break;
5762         default:
5763                 rc = 0;
5764                 break;
5765         }
5766         return rc;
5767 }
5768
5769 static ssize_t qla4xxx_show_boot_ini_info(void *data, int type, char *buf)
5770 {
5771         struct scsi_qla_host *ha = data;
5772         char *str = buf;
5773         int rc;
5774
5775         switch (type) {
5776         case ISCSI_BOOT_INI_INITIATOR_NAME:
5777                 rc = sprintf(str, "%s\n", ha->name_string);
5778                 break;
5779         default:
5780                 rc = -ENOSYS;
5781                 break;
5782         }
5783         return rc;
5784 }
5785
5786 static umode_t qla4xxx_ini_get_attr_visibility(void *data, int type)
5787 {
5788         int rc;
5789
5790         switch (type) {
5791         case ISCSI_BOOT_INI_INITIATOR_NAME:
5792                 rc = S_IRUGO;
5793                 break;
5794         default:
5795                 rc = 0;
5796                 break;
5797         }
5798         return rc;
5799 }
5800
5801 static ssize_t
5802 qla4xxx_show_boot_tgt_info(struct ql4_boot_session_info *boot_sess, int type,
5803                            char *buf)
5804 {
5805         struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
5806         char *str = buf;
5807         int rc;
5808
5809         switch (type) {
5810         case ISCSI_BOOT_TGT_NAME:
5811                 rc = sprintf(buf, "%s\n", (char *)&boot_sess->target_name);
5812                 break;
5813         case ISCSI_BOOT_TGT_IP_ADDR:
5814                 if (boot_sess->conn_list[0].dest_ipaddr.ip_type == 0x1)
5815                         rc = sprintf(buf, "%pI4\n",
5816                                      &boot_conn->dest_ipaddr.ip_address);
5817                 else
5818                         rc = sprintf(str, "%pI6\n",
5819                                      &boot_conn->dest_ipaddr.ip_address);
5820                 break;
5821         case ISCSI_BOOT_TGT_PORT:
5822                         rc = sprintf(str, "%d\n", boot_conn->dest_port);
5823                 break;
5824         case ISCSI_BOOT_TGT_CHAP_NAME:
5825                 rc = sprintf(str,  "%.*s\n",
5826                              boot_conn->chap.target_chap_name_length,
5827                              (char *)&boot_conn->chap.target_chap_name);
5828                 break;
5829         case ISCSI_BOOT_TGT_CHAP_SECRET:
5830                 rc = sprintf(str,  "%.*s\n",
5831                              boot_conn->chap.target_secret_length,
5832                              (char *)&boot_conn->chap.target_secret);
5833                 break;
5834         case ISCSI_BOOT_TGT_REV_CHAP_NAME:
5835                 rc = sprintf(str,  "%.*s\n",
5836                              boot_conn->chap.intr_chap_name_length,
5837                              (char *)&boot_conn->chap.intr_chap_name);
5838                 break;
5839         case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
5840                 rc = sprintf(str,  "%.*s\n",
5841                              boot_conn->chap.intr_secret_length,
5842                              (char *)&boot_conn->chap.intr_secret);
5843                 break;
5844         case ISCSI_BOOT_TGT_FLAGS:
5845                 rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
5846                 break;
5847         case ISCSI_BOOT_TGT_NIC_ASSOC:
5848                 rc = sprintf(str, "0\n");
5849                 break;
5850         default:
5851                 rc = -ENOSYS;
5852                 break;
5853         }
5854         return rc;
5855 }
5856
5857 static ssize_t qla4xxx_show_boot_tgt_pri_info(void *data, int type, char *buf)
5858 {
5859         struct scsi_qla_host *ha = data;
5860         struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_pri_sess);
5861
5862         return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
5863 }
5864
5865 static ssize_t qla4xxx_show_boot_tgt_sec_info(void *data, int type, char *buf)
5866 {
5867         struct scsi_qla_host *ha = data;
5868         struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_sec_sess);
5869
5870         return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
5871 }
5872
5873 static umode_t qla4xxx_tgt_get_attr_visibility(void *data, int type)
5874 {
5875         int rc;
5876
5877         switch (type) {
5878         case ISCSI_BOOT_TGT_NAME:
5879         case ISCSI_BOOT_TGT_IP_ADDR:
5880         case ISCSI_BOOT_TGT_PORT:
5881         case ISCSI_BOOT_TGT_CHAP_NAME:
5882         case ISCSI_BOOT_TGT_CHAP_SECRET:
5883         case ISCSI_BOOT_TGT_REV_CHAP_NAME:
5884         case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
5885         case ISCSI_BOOT_TGT_NIC_ASSOC:
5886         case ISCSI_BOOT_TGT_FLAGS:
5887                 rc = S_IRUGO;
5888                 break;
5889         default:
5890                 rc = 0;
5891                 break;
5892         }
5893         return rc;
5894 }
5895
5896 static void qla4xxx_boot_release(void *data)
5897 {
5898         struct scsi_qla_host *ha = data;
5899
5900         scsi_host_put(ha->host);
5901 }
5902
5903 static int get_fw_boot_info(struct scsi_qla_host *ha, uint16_t ddb_index[])
5904 {
5905         dma_addr_t buf_dma;
5906         uint32_t addr, pri_addr, sec_addr;
5907         uint32_t offset;
5908         uint16_t func_num;
5909         uint8_t val;
5910         uint8_t *buf = NULL;
5911         size_t size = 13 * sizeof(uint8_t);
5912         int ret = QLA_SUCCESS;
5913
5914         func_num = PCI_FUNC(ha->pdev->devfn);
5915
5916         ql4_printk(KERN_INFO, ha, "%s: Get FW boot info for 0x%x func %d\n",
5917                    __func__, ha->pdev->device, func_num);
5918
5919         if (is_qla40XX(ha)) {
5920                 if (func_num == 1) {
5921                         addr = NVRAM_PORT0_BOOT_MODE;
5922                         pri_addr = NVRAM_PORT0_BOOT_PRI_TGT;
5923                         sec_addr = NVRAM_PORT0_BOOT_SEC_TGT;
5924                 } else if (func_num == 3) {
5925                         addr = NVRAM_PORT1_BOOT_MODE;
5926                         pri_addr = NVRAM_PORT1_BOOT_PRI_TGT;
5927                         sec_addr = NVRAM_PORT1_BOOT_SEC_TGT;
5928                 } else {
5929                         ret = QLA_ERROR;
5930                         goto exit_boot_info;
5931                 }
5932
5933                 /* Check Boot Mode */
5934                 val = rd_nvram_byte(ha, addr);
5935                 if (!(val & 0x07)) {
5936                         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Adapter boot "
5937                                           "options : 0x%x\n", __func__, val));
5938                         ret = QLA_ERROR;
5939                         goto exit_boot_info;
5940                 }
5941
5942                 /* get primary valid target index */
5943                 val = rd_nvram_byte(ha, pri_addr);
5944                 if (val & BIT_7)
5945                         ddb_index[0] = (val & 0x7f);
5946
5947                 /* get secondary valid target index */
5948                 val = rd_nvram_byte(ha, sec_addr);
5949                 if (val & BIT_7)
5950                         ddb_index[1] = (val & 0x7f);
5951                 goto exit_boot_info;
5952         } else if (is_qla80XX(ha)) {
5953                 buf = dma_alloc_coherent(&ha->pdev->dev, size,
5954                                          &buf_dma, GFP_KERNEL);
5955                 if (!buf) {
5956                         DEBUG2(ql4_printk(KERN_ERR, ha,
5957                                           "%s: Unable to allocate dma buffer\n",
5958                                            __func__));
5959                         ret = QLA_ERROR;
5960                         goto exit_boot_info;
5961                 }
5962
5963                 if (ha->port_num == 0)
5964                         offset = BOOT_PARAM_OFFSET_PORT0;
5965                 else if (ha->port_num == 1)
5966                         offset = BOOT_PARAM_OFFSET_PORT1;
5967                 else {
5968                         ret = QLA_ERROR;
5969                         goto exit_boot_info_free;
5970                 }
5971                 addr = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_iscsi_param * 4) +
5972                        offset;
5973                 if (qla4xxx_get_flash(ha, buf_dma, addr,
5974                                       13 * sizeof(uint8_t)) != QLA_SUCCESS) {
5975                         DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: Get Flash"
5976                                           " failed\n", ha->host_no, __func__));
5977                         ret = QLA_ERROR;
5978                         goto exit_boot_info_free;
5979                 }
5980                 /* Check Boot Mode */
5981                 if (!(buf[1] & 0x07)) {
5982                         DEBUG2(ql4_printk(KERN_INFO, ha, "Firmware boot options"
5983                                           " : 0x%x\n", buf[1]));
5984                         ret = QLA_ERROR;
5985                         goto exit_boot_info_free;
5986                 }
5987
5988                 /* get primary valid target index */
5989                 if (buf[2] & BIT_7)
5990                         ddb_index[0] = buf[2] & 0x7f;
5991
5992                 /* get secondary valid target index */
5993                 if (buf[11] & BIT_7)
5994                         ddb_index[1] = buf[11] & 0x7f;
5995         } else {
5996                 ret = QLA_ERROR;
5997                 goto exit_boot_info;
5998         }
5999
6000         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Primary target ID %d, Secondary"
6001                           " target ID %d\n", __func__, ddb_index[0],
6002                           ddb_index[1]));
6003
6004 exit_boot_info_free:
6005         dma_free_coherent(&ha->pdev->dev, size, buf, buf_dma);
6006 exit_boot_info:
6007         ha->pri_ddb_idx = ddb_index[0];
6008         ha->sec_ddb_idx = ddb_index[1];
6009         return ret;
6010 }
6011
6012 /**
6013  * qla4xxx_get_bidi_chap - Get a BIDI CHAP user and password
6014  * @ha: pointer to adapter structure
6015  * @username: CHAP username to be returned
6016  * @password: CHAP password to be returned
6017  *
6018  * If a boot entry has BIDI CHAP enabled then we need to set the BIDI CHAP
6019  * user and password in the sysfs entry in /sys/firmware/iscsi_boot#/.
6020  * So from the CHAP cache find the first BIDI CHAP entry and set it
6021  * to the boot record in sysfs.
6022  **/
6023 static int qla4xxx_get_bidi_chap(struct scsi_qla_host *ha, char *username,
6024                             char *password)
6025 {
6026         int i, ret = -EINVAL;
6027         int max_chap_entries = 0;
6028         struct ql4_chap_table *chap_table;
6029
6030         if (is_qla80XX(ha))
6031                 max_chap_entries = (ha->hw.flt_chap_size / 2) /
6032                                                 sizeof(struct ql4_chap_table);
6033         else
6034                 max_chap_entries = MAX_CHAP_ENTRIES_40XX;
6035
6036         if (!ha->chap_list) {
6037                 ql4_printk(KERN_ERR, ha, "Do not have CHAP table cache\n");
6038                 return ret;
6039         }
6040
6041         mutex_lock(&ha->chap_sem);
6042         for (i = 0; i < max_chap_entries; i++) {
6043                 chap_table = (struct ql4_chap_table *)ha->chap_list + i;
6044                 if (chap_table->cookie !=
6045                     __constant_cpu_to_le16(CHAP_VALID_COOKIE)) {
6046                         continue;
6047                 }
6048
6049                 if (chap_table->flags & BIT_7) /* local */
6050                         continue;
6051
6052                 if (!(chap_table->flags & BIT_6)) /* Not BIDI */
6053                         continue;
6054
6055                 strlcpy(password, chap_table->secret, QL4_CHAP_MAX_SECRET_LEN);
6056                 strlcpy(username, chap_table->name, QL4_CHAP_MAX_NAME_LEN);
6057                 ret = 0;
6058                 break;
6059         }
6060         mutex_unlock(&ha->chap_sem);
6061
6062         return ret;
6063 }
6064
6065
6066 static int qla4xxx_get_boot_target(struct scsi_qla_host *ha,
6067                                    struct ql4_boot_session_info *boot_sess,
6068                                    uint16_t ddb_index)
6069 {
6070         struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
6071         struct dev_db_entry *fw_ddb_entry;
6072         dma_addr_t fw_ddb_entry_dma;
6073         uint16_t idx;
6074         uint16_t options;
6075         int ret = QLA_SUCCESS;
6076
6077         fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
6078                                           &fw_ddb_entry_dma, GFP_KERNEL);
6079         if (!fw_ddb_entry) {
6080                 DEBUG2(ql4_printk(KERN_ERR, ha,
6081                                   "%s: Unable to allocate dma buffer.\n",
6082                                   __func__));
6083                 ret = QLA_ERROR;
6084                 return ret;
6085         }
6086
6087         if (qla4xxx_bootdb_by_index(ha, fw_ddb_entry,
6088                                    fw_ddb_entry_dma, ddb_index)) {
6089                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: No Flash DDB found at "
6090                                   "index [%d]\n", __func__, ddb_index));
6091                 ret = QLA_ERROR;
6092                 goto exit_boot_target;
6093         }
6094
6095         /* Update target name and IP from DDB */
6096         memcpy(boot_sess->target_name, fw_ddb_entry->iscsi_name,
6097                min(sizeof(boot_sess->target_name),
6098                    sizeof(fw_ddb_entry->iscsi_name)));
6099
6100         options = le16_to_cpu(fw_ddb_entry->options);
6101         if (options & DDB_OPT_IPV6_DEVICE) {
6102                 memcpy(&boot_conn->dest_ipaddr.ip_address,
6103                        &fw_ddb_entry->ip_addr[0], IPv6_ADDR_LEN);
6104         } else {
6105                 boot_conn->dest_ipaddr.ip_type = 0x1;
6106                 memcpy(&boot_conn->dest_ipaddr.ip_address,
6107                        &fw_ddb_entry->ip_addr[0], IP_ADDR_LEN);
6108         }
6109
6110         boot_conn->dest_port = le16_to_cpu(fw_ddb_entry->port);
6111
6112         /* update chap information */
6113         idx = __le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
6114
6115         if (BIT_7 & le16_to_cpu(fw_ddb_entry->iscsi_options))   {
6116
6117                 DEBUG2(ql4_printk(KERN_INFO, ha, "Setting chap\n"));
6118
6119                 ret = qla4xxx_get_chap(ha, (char *)&boot_conn->chap.
6120                                        target_chap_name,
6121                                        (char *)&boot_conn->chap.target_secret,
6122                                        idx);
6123                 if (ret) {
6124                         ql4_printk(KERN_ERR, ha, "Failed to set chap\n");
6125                         ret = QLA_ERROR;
6126                         goto exit_boot_target;
6127                 }
6128
6129                 boot_conn->chap.target_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
6130                 boot_conn->chap.target_secret_length = QL4_CHAP_MAX_SECRET_LEN;
6131         }
6132
6133         if (BIT_4 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
6134
6135                 DEBUG2(ql4_printk(KERN_INFO, ha, "Setting BIDI chap\n"));
6136
6137                 ret = qla4xxx_get_bidi_chap(ha,
6138                                     (char *)&boot_conn->chap.intr_chap_name,
6139                                     (char *)&boot_conn->chap.intr_secret);
6140
6141                 if (ret) {
6142                         ql4_printk(KERN_ERR, ha, "Failed to set BIDI chap\n");
6143                         ret = QLA_ERROR;
6144                         goto exit_boot_target;
6145                 }
6146
6147                 boot_conn->chap.intr_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
6148                 boot_conn->chap.intr_secret_length = QL4_CHAP_MAX_SECRET_LEN;
6149         }
6150
6151 exit_boot_target:
6152         dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
6153                           fw_ddb_entry, fw_ddb_entry_dma);
6154         return ret;
6155 }
6156
6157 static int qla4xxx_get_boot_info(struct scsi_qla_host *ha)
6158 {
6159         uint16_t ddb_index[2];
6160         int ret = QLA_ERROR;
6161         int rval;
6162
6163         memset(ddb_index, 0, sizeof(ddb_index));
6164         ddb_index[0] = 0xffff;
6165         ddb_index[1] = 0xffff;
6166         ret = get_fw_boot_info(ha, ddb_index);
6167         if (ret != QLA_SUCCESS) {
6168                 DEBUG2(ql4_printk(KERN_INFO, ha,
6169                                 "%s: No boot target configured.\n", __func__));
6170                 return ret;
6171         }
6172
6173         if (ql4xdisablesysfsboot)
6174                 return QLA_SUCCESS;
6175
6176         if (ddb_index[0] == 0xffff)
6177                 goto sec_target;
6178
6179         rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_pri_sess),
6180                                       ddb_index[0]);
6181         if (rval != QLA_SUCCESS) {
6182                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Primary boot target not "
6183                                   "configured\n", __func__));
6184         } else
6185                 ret = QLA_SUCCESS;
6186
6187 sec_target:
6188         if (ddb_index[1] == 0xffff)
6189                 goto exit_get_boot_info;
6190
6191         rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_sec_sess),
6192                                       ddb_index[1]);
6193         if (rval != QLA_SUCCESS) {
6194                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Secondary boot target not"
6195                                   " configured\n", __func__));
6196         } else
6197                 ret = QLA_SUCCESS;
6198
6199 exit_get_boot_info:
6200         return ret;
6201 }
6202
6203 static int qla4xxx_setup_boot_info(struct scsi_qla_host *ha)
6204 {
6205         struct iscsi_boot_kobj *boot_kobj;
6206
6207         if (qla4xxx_get_boot_info(ha) != QLA_SUCCESS)
6208                 return QLA_ERROR;
6209
6210         if (ql4xdisablesysfsboot) {
6211                 ql4_printk(KERN_INFO, ha,
6212                            "%s: syfsboot disabled - driver will trigger login "
6213                            "and publish session for discovery .\n", __func__);
6214                 return QLA_SUCCESS;
6215         }
6216
6217
6218         ha->boot_kset = iscsi_boot_create_host_kset(ha->host->host_no);
6219         if (!ha->boot_kset)
6220                 goto kset_free;
6221
6222         if (!scsi_host_get(ha->host))
6223                 goto kset_free;
6224         boot_kobj = iscsi_boot_create_target(ha->boot_kset, 0, ha,
6225                                              qla4xxx_show_boot_tgt_pri_info,
6226                                              qla4xxx_tgt_get_attr_visibility,
6227                                              qla4xxx_boot_release);
6228         if (!boot_kobj)
6229                 goto put_host;
6230
6231         if (!scsi_host_get(ha->host))
6232                 goto kset_free;
6233         boot_kobj = iscsi_boot_create_target(ha->boot_kset, 1, ha,
6234                                              qla4xxx_show_boot_tgt_sec_info,
6235                                              qla4xxx_tgt_get_attr_visibility,
6236                                              qla4xxx_boot_release);
6237         if (!boot_kobj)
6238                 goto put_host;
6239
6240         if (!scsi_host_get(ha->host))
6241                 goto kset_free;
6242         boot_kobj = iscsi_boot_create_initiator(ha->boot_kset, 0, ha,
6243                                                qla4xxx_show_boot_ini_info,
6244                                                qla4xxx_ini_get_attr_visibility,
6245                                                qla4xxx_boot_release);
6246         if (!boot_kobj)
6247                 goto put_host;
6248
6249         if (!scsi_host_get(ha->host))
6250                 goto kset_free;
6251         boot_kobj = iscsi_boot_create_ethernet(ha->boot_kset, 0, ha,
6252                                                qla4xxx_show_boot_eth_info,
6253                                                qla4xxx_eth_get_attr_visibility,
6254                                                qla4xxx_boot_release);
6255         if (!boot_kobj)
6256                 goto put_host;
6257
6258         return QLA_SUCCESS;
6259
6260 put_host:
6261         scsi_host_put(ha->host);
6262 kset_free:
6263         iscsi_boot_destroy_kset(ha->boot_kset);
6264         return -ENOMEM;
6265 }
6266
6267
6268 static void qla4xxx_get_param_ddb(struct ddb_entry *ddb_entry,
6269                                   struct ql4_tuple_ddb *tddb)
6270 {
6271         struct scsi_qla_host *ha;
6272         struct iscsi_cls_session *cls_sess;
6273         struct iscsi_cls_conn *cls_conn;
6274         struct iscsi_session *sess;
6275         struct iscsi_conn *conn;
6276
6277         DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
6278         ha = ddb_entry->ha;
6279         cls_sess = ddb_entry->sess;
6280         sess = cls_sess->dd_data;
6281         cls_conn = ddb_entry->conn;
6282         conn = cls_conn->dd_data;
6283
6284         tddb->tpgt = sess->tpgt;
6285         tddb->port = conn->persistent_port;
6286         strlcpy(tddb->iscsi_name, sess->targetname, ISCSI_NAME_SIZE);
6287         strlcpy(tddb->ip_addr, conn->persistent_address, DDB_IPADDR_LEN);
6288 }
6289
6290 static void qla4xxx_convert_param_ddb(struct dev_db_entry *fw_ddb_entry,
6291                                       struct ql4_tuple_ddb *tddb,
6292                                       uint8_t *flash_isid)
6293 {
6294         uint16_t options = 0;
6295
6296         tddb->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
6297         memcpy(&tddb->iscsi_name[0], &fw_ddb_entry->iscsi_name[0],
6298                min(sizeof(tddb->iscsi_name), sizeof(fw_ddb_entry->iscsi_name)));
6299
6300         options = le16_to_cpu(fw_ddb_entry->options);
6301         if (options & DDB_OPT_IPV6_DEVICE)
6302                 sprintf(tddb->ip_addr, "%pI6", fw_ddb_entry->ip_addr);
6303         else
6304                 sprintf(tddb->ip_addr, "%pI4", fw_ddb_entry->ip_addr);
6305
6306         tddb->port = le16_to_cpu(fw_ddb_entry->port);
6307
6308         if (flash_isid == NULL)
6309                 memcpy(&tddb->isid[0], &fw_ddb_entry->isid[0],
6310                        sizeof(tddb->isid));
6311         else
6312                 memcpy(&tddb->isid[0], &flash_isid[0], sizeof(tddb->isid));
6313 }
6314
6315 static int qla4xxx_compare_tuple_ddb(struct scsi_qla_host *ha,
6316                                      struct ql4_tuple_ddb *old_tddb,
6317                                      struct ql4_tuple_ddb *new_tddb,
6318                                      uint8_t is_isid_compare)
6319 {
6320         if (strcmp(old_tddb->iscsi_name, new_tddb->iscsi_name))
6321                 return QLA_ERROR;
6322
6323         if (strcmp(old_tddb->ip_addr, new_tddb->ip_addr))
6324                 return QLA_ERROR;
6325
6326         if (old_tddb->port != new_tddb->port)
6327                 return QLA_ERROR;
6328
6329         /* For multi sessions, driver generates the ISID, so do not compare
6330          * ISID in reset path since it would be a comparison between the
6331          * driver generated ISID and firmware generated ISID. This could
6332          * lead to adding duplicated DDBs in the list as driver generated
6333          * ISID would not match firmware generated ISID.
6334          */
6335         if (is_isid_compare) {
6336                 DEBUG2(ql4_printk(KERN_INFO, ha,
6337                         "%s: old ISID [%pmR] New ISID [%pmR]\n",
6338                         __func__, old_tddb->isid, new_tddb->isid));
6339
6340                 if (memcmp(&old_tddb->isid[0], &new_tddb->isid[0],
6341                            sizeof(old_tddb->isid)))
6342                         return QLA_ERROR;
6343         }
6344
6345         DEBUG2(ql4_printk(KERN_INFO, ha,
6346                           "Match Found, fw[%d,%d,%s,%s], [%d,%d,%s,%s]",
6347                           old_tddb->port, old_tddb->tpgt, old_tddb->ip_addr,
6348                           old_tddb->iscsi_name, new_tddb->port, new_tddb->tpgt,
6349                           new_tddb->ip_addr, new_tddb->iscsi_name));
6350
6351         return QLA_SUCCESS;
6352 }
6353
6354 static int qla4xxx_is_session_exists(struct scsi_qla_host *ha,
6355                                      struct dev_db_entry *fw_ddb_entry,
6356                                      uint32_t *index)
6357 {
6358         struct ddb_entry *ddb_entry;
6359         struct ql4_tuple_ddb *fw_tddb = NULL;
6360         struct ql4_tuple_ddb *tmp_tddb = NULL;
6361         int idx;
6362         int ret = QLA_ERROR;
6363
6364         fw_tddb = vzalloc(sizeof(*fw_tddb));
6365         if (!fw_tddb) {
6366                 DEBUG2(ql4_printk(KERN_WARNING, ha,
6367                                   "Memory Allocation failed.\n"));
6368                 ret = QLA_SUCCESS;
6369                 goto exit_check;
6370         }
6371
6372         tmp_tddb = vzalloc(sizeof(*tmp_tddb));
6373         if (!tmp_tddb) {
6374                 DEBUG2(ql4_printk(KERN_WARNING, ha,
6375                                   "Memory Allocation failed.\n"));
6376                 ret = QLA_SUCCESS;
6377                 goto exit_check;
6378         }
6379
6380         qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb, NULL);
6381
6382         for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
6383                 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
6384                 if (ddb_entry == NULL)
6385                         continue;
6386
6387                 qla4xxx_get_param_ddb(ddb_entry, tmp_tddb);
6388                 if (!qla4xxx_compare_tuple_ddb(ha, fw_tddb, tmp_tddb, false)) {
6389                         ret = QLA_SUCCESS; /* found */
6390                         if (index != NULL)
6391                                 *index = idx;
6392                         goto exit_check;
6393                 }
6394         }
6395
6396 exit_check:
6397         if (fw_tddb)
6398                 vfree(fw_tddb);
6399         if (tmp_tddb)
6400                 vfree(tmp_tddb);
6401         return ret;
6402 }
6403
6404 /**
6405  * qla4xxx_check_existing_isid - check if target with same isid exist
6406  *                               in target list
6407  * @list_nt: list of target
6408  * @isid: isid to check
6409  *
6410  * This routine return QLA_SUCCESS if target with same isid exist
6411  **/
6412 static int qla4xxx_check_existing_isid(struct list_head *list_nt, uint8_t *isid)
6413 {
6414         struct qla_ddb_index *nt_ddb_idx, *nt_ddb_idx_tmp;
6415         struct dev_db_entry *fw_ddb_entry;
6416
6417         list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
6418                 fw_ddb_entry = &nt_ddb_idx->fw_ddb;
6419
6420                 if (memcmp(&fw_ddb_entry->isid[0], &isid[0],
6421                            sizeof(nt_ddb_idx->fw_ddb.isid)) == 0) {
6422                         return QLA_SUCCESS;
6423                 }
6424         }
6425         return QLA_ERROR;
6426 }
6427
6428 /**
6429  * qla4xxx_update_isid - compare ddbs and updated isid
6430  * @ha: Pointer to host adapter structure.
6431  * @list_nt: list of nt target
6432  * @fw_ddb_entry: firmware ddb entry
6433  *
6434  * This routine update isid if ddbs have same iqn, same isid and
6435  * different IP addr.
6436  * Return QLA_SUCCESS if isid is updated.
6437  **/
6438 static int qla4xxx_update_isid(struct scsi_qla_host *ha,
6439                                struct list_head *list_nt,
6440                                struct dev_db_entry *fw_ddb_entry)
6441 {
6442         uint8_t base_value, i;
6443
6444         base_value = fw_ddb_entry->isid[1] & 0x1f;
6445         for (i = 0; i < 8; i++) {
6446                 fw_ddb_entry->isid[1] = (base_value | (i << 5));
6447                 if (qla4xxx_check_existing_isid(list_nt, fw_ddb_entry->isid))
6448                         break;
6449         }
6450
6451         if (!qla4xxx_check_existing_isid(list_nt, fw_ddb_entry->isid))
6452                 return QLA_ERROR;
6453
6454         return QLA_SUCCESS;
6455 }
6456
6457 /**
6458  * qla4xxx_should_update_isid - check if isid need to update
6459  * @ha: Pointer to host adapter structure.
6460  * @old_tddb: ddb tuple
6461  * @new_tddb: ddb tuple
6462  *
6463  * Return QLA_SUCCESS if different IP, different PORT, same iqn,
6464  * same isid
6465  **/
6466 static int qla4xxx_should_update_isid(struct scsi_qla_host *ha,
6467                                       struct ql4_tuple_ddb *old_tddb,
6468                                       struct ql4_tuple_ddb *new_tddb)
6469 {
6470         if (strcmp(old_tddb->ip_addr, new_tddb->ip_addr) == 0) {
6471                 /* Same ip */
6472                 if (old_tddb->port == new_tddb->port)
6473                         return QLA_ERROR;
6474         }
6475
6476         if (strcmp(old_tddb->iscsi_name, new_tddb->iscsi_name))
6477                 /* different iqn */
6478                 return QLA_ERROR;
6479
6480         if (memcmp(&old_tddb->isid[0], &new_tddb->isid[0],
6481                    sizeof(old_tddb->isid)))
6482                 /* different isid */
6483                 return QLA_ERROR;
6484
6485         return QLA_SUCCESS;
6486 }
6487
6488 /**
6489  * qla4xxx_is_flash_ddb_exists - check if fw_ddb_entry already exists in list_nt
6490  * @ha: Pointer to host adapter structure.
6491  * @list_nt: list of nt target.
6492  * @fw_ddb_entry: firmware ddb entry.
6493  *
6494  * This routine check if fw_ddb_entry already exists in list_nt to avoid
6495  * duplicate ddb in list_nt.
6496  * Return QLA_SUCCESS if duplicate ddb exit in list_nl.
6497  * Note: This function also update isid of DDB if required.
6498  **/
6499
6500 static int qla4xxx_is_flash_ddb_exists(struct scsi_qla_host *ha,
6501                                        struct list_head *list_nt,
6502                                        struct dev_db_entry *fw_ddb_entry)
6503 {
6504         struct qla_ddb_index  *nt_ddb_idx, *nt_ddb_idx_tmp;
6505         struct ql4_tuple_ddb *fw_tddb = NULL;
6506         struct ql4_tuple_ddb *tmp_tddb = NULL;
6507         int rval, ret = QLA_ERROR;
6508
6509         fw_tddb = vzalloc(sizeof(*fw_tddb));
6510         if (!fw_tddb) {
6511                 DEBUG2(ql4_printk(KERN_WARNING, ha,
6512                                   "Memory Allocation failed.\n"));
6513                 ret = QLA_SUCCESS;
6514                 goto exit_check;
6515         }
6516
6517         tmp_tddb = vzalloc(sizeof(*tmp_tddb));
6518         if (!tmp_tddb) {
6519                 DEBUG2(ql4_printk(KERN_WARNING, ha,
6520                                   "Memory Allocation failed.\n"));
6521                 ret = QLA_SUCCESS;
6522                 goto exit_check;
6523         }
6524
6525         qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb, NULL);
6526
6527         list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
6528                 qla4xxx_convert_param_ddb(&nt_ddb_idx->fw_ddb, tmp_tddb,
6529                                           nt_ddb_idx->flash_isid);
6530                 ret = qla4xxx_compare_tuple_ddb(ha, fw_tddb, tmp_tddb, true);
6531                 /* found duplicate ddb */
6532                 if (ret == QLA_SUCCESS)
6533                         goto exit_check;
6534         }
6535
6536         list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
6537                 qla4xxx_convert_param_ddb(&nt_ddb_idx->fw_ddb, tmp_tddb, NULL);
6538
6539                 ret = qla4xxx_should_update_isid(ha, tmp_tddb, fw_tddb);
6540                 if (ret == QLA_SUCCESS) {
6541                         rval = qla4xxx_update_isid(ha, list_nt, fw_ddb_entry);
6542                         if (rval == QLA_SUCCESS)
6543                                 ret = QLA_ERROR;
6544                         else
6545                                 ret = QLA_SUCCESS;
6546
6547                         goto exit_check;
6548                 }
6549         }
6550
6551 exit_check:
6552         if (fw_tddb)
6553                 vfree(fw_tddb);
6554         if (tmp_tddb)
6555                 vfree(tmp_tddb);
6556         return ret;
6557 }
6558
6559 static void qla4xxx_free_ddb_list(struct list_head *list_ddb)
6560 {
6561         struct qla_ddb_index  *ddb_idx, *ddb_idx_tmp;
6562
6563         list_for_each_entry_safe(ddb_idx, ddb_idx_tmp, list_ddb, list) {
6564                 list_del_init(&ddb_idx->list);
6565                 vfree(ddb_idx);
6566         }
6567 }
6568
6569 static struct iscsi_endpoint *qla4xxx_get_ep_fwdb(struct scsi_qla_host *ha,
6570                                         struct dev_db_entry *fw_ddb_entry)
6571 {
6572         struct iscsi_endpoint *ep;
6573         struct sockaddr_in *addr;
6574         struct sockaddr_in6 *addr6;
6575         struct sockaddr *t_addr;
6576         struct sockaddr_storage *dst_addr;
6577         char *ip;
6578
6579         /* TODO: need to destroy on unload iscsi_endpoint*/
6580         dst_addr = vmalloc(sizeof(*dst_addr));
6581         if (!dst_addr)
6582                 return NULL;
6583
6584         if (fw_ddb_entry->options & DDB_OPT_IPV6_DEVICE) {
6585                 t_addr = (struct sockaddr *)dst_addr;
6586                 t_addr->sa_family = AF_INET6;
6587                 addr6 = (struct sockaddr_in6 *)dst_addr;
6588                 ip = (char *)&addr6->sin6_addr;
6589                 memcpy(ip, fw_ddb_entry->ip_addr, IPv6_ADDR_LEN);
6590                 addr6->sin6_port = htons(le16_to_cpu(fw_ddb_entry->port));
6591
6592         } else {
6593                 t_addr = (struct sockaddr *)dst_addr;
6594                 t_addr->sa_family = AF_INET;
6595                 addr = (struct sockaddr_in *)dst_addr;
6596                 ip = (char *)&addr->sin_addr;
6597                 memcpy(ip, fw_ddb_entry->ip_addr, IP_ADDR_LEN);
6598                 addr->sin_port = htons(le16_to_cpu(fw_ddb_entry->port));
6599         }
6600
6601         ep = qla4xxx_ep_connect(ha->host, (struct sockaddr *)dst_addr, 0);
6602         vfree(dst_addr);
6603         return ep;
6604 }
6605
6606 static int qla4xxx_verify_boot_idx(struct scsi_qla_host *ha, uint16_t idx)
6607 {
6608         if (ql4xdisablesysfsboot)
6609                 return QLA_SUCCESS;
6610         if (idx == ha->pri_ddb_idx || idx == ha->sec_ddb_idx)
6611                 return QLA_ERROR;
6612         return QLA_SUCCESS;
6613 }
6614
6615 static void qla4xxx_setup_flash_ddb_entry(struct scsi_qla_host *ha,
6616                                           struct ddb_entry *ddb_entry,
6617                                           uint16_t idx)
6618 {
6619         uint16_t def_timeout;
6620
6621         ddb_entry->ddb_type = FLASH_DDB;
6622         ddb_entry->fw_ddb_index = INVALID_ENTRY;
6623         ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
6624         ddb_entry->ha = ha;
6625         ddb_entry->unblock_sess = qla4xxx_unblock_flash_ddb;
6626         ddb_entry->ddb_change = qla4xxx_flash_ddb_change;
6627         ddb_entry->chap_tbl_idx = INVALID_ENTRY;
6628
6629         atomic_set(&ddb_entry->retry_relogin_timer, INVALID_ENTRY);
6630         atomic_set(&ddb_entry->relogin_timer, 0);
6631         atomic_set(&ddb_entry->relogin_retry_count, 0);
6632         def_timeout = le16_to_cpu(ddb_entry->fw_ddb_entry.def_timeout);
6633         ddb_entry->default_relogin_timeout =
6634                 (def_timeout > LOGIN_TOV) && (def_timeout < LOGIN_TOV * 10) ?
6635                 def_timeout : LOGIN_TOV;
6636         ddb_entry->default_time2wait =
6637                 le16_to_cpu(ddb_entry->fw_ddb_entry.iscsi_def_time2wait);
6638
6639         if (ql4xdisablesysfsboot &&
6640             (idx == ha->pri_ddb_idx || idx == ha->sec_ddb_idx))
6641                 set_bit(DF_BOOT_TGT, &ddb_entry->flags);
6642 }
6643
6644 static void qla4xxx_wait_for_ip_configuration(struct scsi_qla_host *ha)
6645 {
6646         uint32_t idx = 0;
6647         uint32_t ip_idx[IP_ADDR_COUNT] = {0, 1, 2, 3}; /* 4 IP interfaces */
6648         uint32_t sts[MBOX_REG_COUNT];
6649         uint32_t ip_state;
6650         unsigned long wtime;
6651         int ret;
6652
6653         wtime = jiffies + (HZ * IP_CONFIG_TOV);
6654         do {
6655                 for (idx = 0; idx < IP_ADDR_COUNT; idx++) {
6656                         if (ip_idx[idx] == -1)
6657                                 continue;
6658
6659                         ret = qla4xxx_get_ip_state(ha, 0, ip_idx[idx], sts);
6660
6661                         if (ret == QLA_ERROR) {
6662                                 ip_idx[idx] = -1;
6663                                 continue;
6664                         }
6665
6666                         ip_state = (sts[1] & IP_STATE_MASK) >> IP_STATE_SHIFT;
6667
6668                         DEBUG2(ql4_printk(KERN_INFO, ha,
6669                                           "Waiting for IP state for idx = %d, state = 0x%x\n",
6670                                           ip_idx[idx], ip_state));
6671                         if (ip_state == IP_ADDRSTATE_UNCONFIGURED ||
6672                             ip_state == IP_ADDRSTATE_INVALID ||
6673                             ip_state == IP_ADDRSTATE_PREFERRED ||
6674                             ip_state == IP_ADDRSTATE_DEPRICATED ||
6675                             ip_state == IP_ADDRSTATE_DISABLING)
6676                                 ip_idx[idx] = -1;
6677                 }
6678
6679                 /* Break if all IP states checked */
6680                 if ((ip_idx[0] == -1) &&
6681                     (ip_idx[1] == -1) &&
6682                     (ip_idx[2] == -1) &&
6683                     (ip_idx[3] == -1))
6684                         break;
6685                 schedule_timeout_uninterruptible(HZ);
6686         } while (time_after(wtime, jiffies));
6687 }
6688
6689 static int qla4xxx_cmp_fw_stentry(struct dev_db_entry *fw_ddb_entry,
6690                                   struct dev_db_entry *flash_ddb_entry)
6691 {
6692         uint16_t options = 0;
6693         size_t ip_len = IP_ADDR_LEN;
6694
6695         options = le16_to_cpu(fw_ddb_entry->options);
6696         if (options & DDB_OPT_IPV6_DEVICE)
6697                 ip_len = IPv6_ADDR_LEN;
6698
6699         if (memcmp(fw_ddb_entry->ip_addr, flash_ddb_entry->ip_addr, ip_len))
6700                 return QLA_ERROR;
6701
6702         if (memcmp(&fw_ddb_entry->isid[0], &flash_ddb_entry->isid[0],
6703                    sizeof(fw_ddb_entry->isid)))
6704                 return QLA_ERROR;
6705
6706         if (memcmp(&fw_ddb_entry->port, &flash_ddb_entry->port,
6707                    sizeof(fw_ddb_entry->port)))
6708                 return QLA_ERROR;
6709
6710         return QLA_SUCCESS;
6711 }
6712
6713 static int qla4xxx_find_flash_st_idx(struct scsi_qla_host *ha,
6714                                      struct dev_db_entry *fw_ddb_entry,
6715                                      uint32_t fw_idx, uint32_t *flash_index)
6716 {
6717         struct dev_db_entry *flash_ddb_entry;
6718         dma_addr_t flash_ddb_entry_dma;
6719         uint32_t idx = 0;
6720         int max_ddbs;
6721         int ret = QLA_ERROR, status;
6722
6723         max_ddbs =  is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
6724                                      MAX_DEV_DB_ENTRIES;
6725
6726         flash_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
6727                                          &flash_ddb_entry_dma);
6728         if (flash_ddb_entry == NULL || fw_ddb_entry == NULL) {
6729                 ql4_printk(KERN_ERR, ha, "Out of memory\n");
6730                 goto exit_find_st_idx;
6731         }
6732
6733         status = qla4xxx_flashdb_by_index(ha, flash_ddb_entry,
6734                                           flash_ddb_entry_dma, fw_idx);
6735         if (status == QLA_SUCCESS) {
6736                 status = qla4xxx_cmp_fw_stentry(fw_ddb_entry, flash_ddb_entry);
6737                 if (status == QLA_SUCCESS) {
6738                         *flash_index = fw_idx;
6739                         ret = QLA_SUCCESS;
6740                         goto exit_find_st_idx;
6741                 }
6742         }
6743
6744         for (idx = 0; idx < max_ddbs; idx++) {
6745                 status = qla4xxx_flashdb_by_index(ha, flash_ddb_entry,
6746                                                   flash_ddb_entry_dma, idx);
6747                 if (status == QLA_ERROR)
6748                         continue;
6749
6750                 status = qla4xxx_cmp_fw_stentry(fw_ddb_entry, flash_ddb_entry);
6751                 if (status == QLA_SUCCESS) {
6752                         *flash_index = idx;
6753                         ret = QLA_SUCCESS;
6754                         goto exit_find_st_idx;
6755                 }
6756         }
6757
6758         if (idx == max_ddbs)
6759                 ql4_printk(KERN_ERR, ha, "Failed to find ST [%d] in flash\n",
6760                            fw_idx);
6761
6762 exit_find_st_idx:
6763         if (flash_ddb_entry)
6764                 dma_pool_free(ha->fw_ddb_dma_pool, flash_ddb_entry,
6765                               flash_ddb_entry_dma);
6766
6767         return ret;
6768 }
6769
6770 static void qla4xxx_build_st_list(struct scsi_qla_host *ha,
6771                                   struct list_head *list_st)
6772 {
6773         struct qla_ddb_index  *st_ddb_idx;
6774         int max_ddbs;
6775         int fw_idx_size;
6776         struct dev_db_entry *fw_ddb_entry;
6777         dma_addr_t fw_ddb_dma;
6778         int ret;
6779         uint32_t idx = 0, next_idx = 0;
6780         uint32_t state = 0, conn_err = 0;
6781         uint32_t flash_index = -1;
6782         uint16_t conn_id = 0;
6783
6784         fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
6785                                       &fw_ddb_dma);
6786         if (fw_ddb_entry == NULL) {
6787                 DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
6788                 goto exit_st_list;
6789         }
6790
6791         max_ddbs =  is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
6792                                      MAX_DEV_DB_ENTRIES;
6793         fw_idx_size = sizeof(struct qla_ddb_index);
6794
6795         for (idx = 0; idx < max_ddbs; idx = next_idx) {
6796                 ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry, fw_ddb_dma,
6797                                               NULL, &next_idx, &state,
6798                                               &conn_err, NULL, &conn_id);
6799                 if (ret == QLA_ERROR)
6800                         break;
6801
6802                 /* Ignore DDB if invalid state (unassigned) */
6803                 if (state == DDB_DS_UNASSIGNED)
6804                         goto continue_next_st;
6805
6806                 /* Check if ST, add to the list_st */
6807                 if (strlen((char *) fw_ddb_entry->iscsi_name) != 0)
6808                         goto continue_next_st;
6809
6810                 st_ddb_idx = vzalloc(fw_idx_size);
6811                 if (!st_ddb_idx)
6812                         break;
6813
6814                 ret = qla4xxx_find_flash_st_idx(ha, fw_ddb_entry, idx,
6815                                                 &flash_index);
6816                 if (ret == QLA_ERROR) {
6817                         ql4_printk(KERN_ERR, ha,
6818                                    "No flash entry for ST at idx [%d]\n", idx);
6819                         st_ddb_idx->flash_ddb_idx = idx;
6820                 } else {
6821                         ql4_printk(KERN_INFO, ha,
6822                                    "ST at idx [%d] is stored at flash [%d]\n",
6823                                    idx, flash_index);
6824                         st_ddb_idx->flash_ddb_idx = flash_index;
6825                 }
6826
6827                 st_ddb_idx->fw_ddb_idx = idx;
6828
6829                 list_add_tail(&st_ddb_idx->list, list_st);
6830 continue_next_st:
6831                 if (next_idx == 0)
6832                         break;
6833         }
6834
6835 exit_st_list:
6836         if (fw_ddb_entry)
6837                 dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
6838 }
6839
6840 /**
6841  * qla4xxx_remove_failed_ddb - Remove inactive or failed ddb from list
6842  * @ha: pointer to adapter structure
6843  * @list_ddb: List from which failed ddb to be removed
6844  *
6845  * Iterate over the list of DDBs and find and remove DDBs that are either in
6846  * no connection active state or failed state
6847  **/
6848 static void qla4xxx_remove_failed_ddb(struct scsi_qla_host *ha,
6849                                       struct list_head *list_ddb)
6850 {
6851         struct qla_ddb_index  *ddb_idx, *ddb_idx_tmp;
6852         uint32_t next_idx = 0;
6853         uint32_t state = 0, conn_err = 0;
6854         int ret;
6855
6856         list_for_each_entry_safe(ddb_idx, ddb_idx_tmp, list_ddb, list) {
6857                 ret = qla4xxx_get_fwddb_entry(ha, ddb_idx->fw_ddb_idx,
6858                                               NULL, 0, NULL, &next_idx, &state,
6859                                               &conn_err, NULL, NULL);
6860                 if (ret == QLA_ERROR)
6861                         continue;
6862
6863                 if (state == DDB_DS_NO_CONNECTION_ACTIVE ||
6864                     state == DDB_DS_SESSION_FAILED) {
6865                         list_del_init(&ddb_idx->list);
6866                         vfree(ddb_idx);
6867                 }
6868         }
6869 }
6870
6871 static void qla4xxx_update_sess_disc_idx(struct scsi_qla_host *ha,
6872                                          struct ddb_entry *ddb_entry,
6873                                          struct dev_db_entry *fw_ddb_entry)
6874 {
6875         struct iscsi_cls_session *cls_sess;
6876         struct iscsi_session *sess;
6877         uint32_t max_ddbs = 0;
6878         uint16_t ddb_link = -1;
6879
6880         max_ddbs =  is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
6881                                      MAX_DEV_DB_ENTRIES;
6882
6883         cls_sess = ddb_entry->sess;
6884         sess = cls_sess->dd_data;
6885
6886         ddb_link = le16_to_cpu(fw_ddb_entry->ddb_link);
6887         if (ddb_link < max_ddbs)
6888                 sess->discovery_parent_idx = ddb_link;
6889         else
6890                 sess->discovery_parent_idx = DDB_NO_LINK;
6891 }
6892
6893 static int qla4xxx_sess_conn_setup(struct scsi_qla_host *ha,
6894                                    struct dev_db_entry *fw_ddb_entry,
6895                                    int is_reset, uint16_t idx)
6896 {
6897         struct iscsi_cls_session *cls_sess;
6898         struct iscsi_session *sess;
6899         struct iscsi_cls_conn *cls_conn;
6900         struct iscsi_endpoint *ep;
6901         uint16_t cmds_max = 32;
6902         uint16_t conn_id = 0;
6903         uint32_t initial_cmdsn = 0;
6904         int ret = QLA_SUCCESS;
6905
6906         struct ddb_entry *ddb_entry = NULL;
6907
6908         /* Create session object, with INVALID_ENTRY,
6909          * the targer_id would get set when we issue the login
6910          */
6911         cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport, ha->host,
6912                                        cmds_max, sizeof(struct ddb_entry),
6913                                        sizeof(struct ql4_task_data),
6914                                        initial_cmdsn, INVALID_ENTRY);
6915         if (!cls_sess) {
6916                 ret = QLA_ERROR;
6917                 goto exit_setup;
6918         }
6919
6920         /*
6921          * so calling module_put function to decrement the
6922          * reference count.
6923          **/
6924         module_put(qla4xxx_iscsi_transport.owner);
6925         sess = cls_sess->dd_data;
6926         ddb_entry = sess->dd_data;
6927         ddb_entry->sess = cls_sess;
6928
6929         cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
6930         memcpy(&ddb_entry->fw_ddb_entry, fw_ddb_entry,
6931                sizeof(struct dev_db_entry));
6932
6933         qla4xxx_setup_flash_ddb_entry(ha, ddb_entry, idx);
6934
6935         cls_conn = iscsi_conn_setup(cls_sess, sizeof(struct qla_conn), conn_id);
6936
6937         if (!cls_conn) {
6938                 ret = QLA_ERROR;
6939                 goto exit_setup;
6940         }
6941
6942         ddb_entry->conn = cls_conn;
6943
6944         /* Setup ep, for displaying attributes in sysfs */
6945         ep = qla4xxx_get_ep_fwdb(ha, fw_ddb_entry);
6946         if (ep) {
6947                 ep->conn = cls_conn;
6948                 cls_conn->ep = ep;
6949         } else {
6950                 DEBUG2(ql4_printk(KERN_ERR, ha, "Unable to get ep\n"));
6951                 ret = QLA_ERROR;
6952                 goto exit_setup;
6953         }
6954
6955         /* Update sess/conn params */
6956         qla4xxx_copy_fwddb_param(ha, fw_ddb_entry, cls_sess, cls_conn);
6957         qla4xxx_update_sess_disc_idx(ha, ddb_entry, fw_ddb_entry);
6958
6959         if (is_reset == RESET_ADAPTER) {
6960                 iscsi_block_session(cls_sess);
6961                 /* Use the relogin path to discover new devices
6962                  *  by short-circuting the logic of setting
6963                  *  timer to relogin - instead set the flags
6964                  *  to initiate login right away.
6965                  */
6966                 set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
6967                 set_bit(DF_RELOGIN, &ddb_entry->flags);
6968         }
6969
6970 exit_setup:
6971         return ret;
6972 }
6973
6974 static void qla4xxx_update_fw_ddb_link(struct scsi_qla_host *ha,
6975                                        struct list_head *list_ddb,
6976                                        struct dev_db_entry *fw_ddb_entry)
6977 {
6978         struct qla_ddb_index  *ddb_idx, *ddb_idx_tmp;
6979         uint16_t ddb_link;
6980
6981         ddb_link = le16_to_cpu(fw_ddb_entry->ddb_link);
6982
6983         list_for_each_entry_safe(ddb_idx, ddb_idx_tmp, list_ddb, list) {
6984                 if (ddb_idx->fw_ddb_idx == ddb_link) {
6985                         DEBUG2(ql4_printk(KERN_INFO, ha,
6986                                           "Updating NT parent idx from [%d] to [%d]\n",
6987                                           ddb_link, ddb_idx->flash_ddb_idx));
6988                         fw_ddb_entry->ddb_link =
6989                                             cpu_to_le16(ddb_idx->flash_ddb_idx);
6990                         return;
6991                 }
6992         }
6993 }
6994
6995 static void qla4xxx_build_nt_list(struct scsi_qla_host *ha,
6996                                   struct list_head *list_nt,
6997                                   struct list_head *list_st,
6998                                   int is_reset)
6999 {
7000         struct dev_db_entry *fw_ddb_entry;
7001         struct ddb_entry *ddb_entry = NULL;
7002         dma_addr_t fw_ddb_dma;
7003         int max_ddbs;
7004         int fw_idx_size;
7005         int ret;
7006         uint32_t idx = 0, next_idx = 0;
7007         uint32_t state = 0, conn_err = 0;
7008         uint32_t ddb_idx = -1;
7009         uint16_t conn_id = 0;
7010         uint16_t ddb_link = -1;
7011         struct qla_ddb_index  *nt_ddb_idx;
7012
7013         fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
7014                                       &fw_ddb_dma);
7015         if (fw_ddb_entry == NULL) {
7016                 DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
7017                 goto exit_nt_list;
7018         }
7019         max_ddbs =  is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
7020                                      MAX_DEV_DB_ENTRIES;
7021         fw_idx_size = sizeof(struct qla_ddb_index);
7022
7023         for (idx = 0; idx < max_ddbs; idx = next_idx) {
7024                 ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry, fw_ddb_dma,
7025                                               NULL, &next_idx, &state,
7026                                               &conn_err, NULL, &conn_id);
7027                 if (ret == QLA_ERROR)
7028                         break;
7029
7030                 if (qla4xxx_verify_boot_idx(ha, idx) != QLA_SUCCESS)
7031                         goto continue_next_nt;
7032
7033                 /* Check if NT, then add to list it */
7034                 if (strlen((char *) fw_ddb_entry->iscsi_name) == 0)
7035                         goto continue_next_nt;
7036
7037                 ddb_link = le16_to_cpu(fw_ddb_entry->ddb_link);
7038                 if (ddb_link < max_ddbs)
7039                         qla4xxx_update_fw_ddb_link(ha, list_st, fw_ddb_entry);
7040
7041                 if (!(state == DDB_DS_NO_CONNECTION_ACTIVE ||
7042                     state == DDB_DS_SESSION_FAILED) &&
7043                     (is_reset == INIT_ADAPTER))
7044                         goto continue_next_nt;
7045
7046                 DEBUG2(ql4_printk(KERN_INFO, ha,
7047                                   "Adding  DDB to session = 0x%x\n", idx));
7048
7049                 if (is_reset == INIT_ADAPTER) {
7050                         nt_ddb_idx = vmalloc(fw_idx_size);
7051                         if (!nt_ddb_idx)
7052                                 break;
7053
7054                         nt_ddb_idx->fw_ddb_idx = idx;
7055
7056                         /* Copy original isid as it may get updated in function
7057                          * qla4xxx_update_isid(). We need original isid in
7058                          * function qla4xxx_compare_tuple_ddb to find duplicate
7059                          * target */
7060                         memcpy(&nt_ddb_idx->flash_isid[0],
7061                                &fw_ddb_entry->isid[0],
7062                                sizeof(nt_ddb_idx->flash_isid));
7063
7064                         ret = qla4xxx_is_flash_ddb_exists(ha, list_nt,
7065                                                           fw_ddb_entry);
7066                         if (ret == QLA_SUCCESS) {
7067                                 /* free nt_ddb_idx and do not add to list_nt */
7068                                 vfree(nt_ddb_idx);
7069                                 goto continue_next_nt;
7070                         }
7071
7072                         /* Copy updated isid */
7073                         memcpy(&nt_ddb_idx->fw_ddb, fw_ddb_entry,
7074                                sizeof(struct dev_db_entry));
7075
7076                         list_add_tail(&nt_ddb_idx->list, list_nt);
7077                 } else if (is_reset == RESET_ADAPTER) {
7078                         ret = qla4xxx_is_session_exists(ha, fw_ddb_entry,
7079                                                         &ddb_idx);
7080                         if (ret == QLA_SUCCESS) {
7081                                 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha,
7082                                                                        ddb_idx);
7083                                 if (ddb_entry != NULL)
7084                                         qla4xxx_update_sess_disc_idx(ha,
7085                                                                      ddb_entry,
7086                                                                   fw_ddb_entry);
7087                                 goto continue_next_nt;
7088                         }
7089                 }
7090
7091                 ret = qla4xxx_sess_conn_setup(ha, fw_ddb_entry, is_reset, idx);
7092                 if (ret == QLA_ERROR)
7093                         goto exit_nt_list;
7094
7095 continue_next_nt:
7096                 if (next_idx == 0)
7097                         break;
7098         }
7099
7100 exit_nt_list:
7101         if (fw_ddb_entry)
7102                 dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
7103 }
7104
7105 static void qla4xxx_build_new_nt_list(struct scsi_qla_host *ha,
7106                                       struct list_head *list_nt,
7107                                       uint16_t target_id)
7108 {
7109         struct dev_db_entry *fw_ddb_entry;
7110         dma_addr_t fw_ddb_dma;
7111         int max_ddbs;
7112         int fw_idx_size;
7113         int ret;
7114         uint32_t idx = 0, next_idx = 0;
7115         uint32_t state = 0, conn_err = 0;
7116         uint16_t conn_id = 0;
7117         struct qla_ddb_index  *nt_ddb_idx;
7118
7119         fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
7120                                       &fw_ddb_dma);
7121         if (fw_ddb_entry == NULL) {
7122                 DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
7123                 goto exit_new_nt_list;
7124         }
7125         max_ddbs =  is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
7126                                      MAX_DEV_DB_ENTRIES;
7127         fw_idx_size = sizeof(struct qla_ddb_index);
7128
7129         for (idx = 0; idx < max_ddbs; idx = next_idx) {
7130                 ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry, fw_ddb_dma,
7131                                               NULL, &next_idx, &state,
7132                                               &conn_err, NULL, &conn_id);
7133                 if (ret == QLA_ERROR)
7134                         break;
7135
7136                 /* Check if NT, then add it to list */
7137                 if (strlen((char *)fw_ddb_entry->iscsi_name) == 0)
7138                         goto continue_next_new_nt;
7139
7140                 if (!(state == DDB_DS_NO_CONNECTION_ACTIVE))
7141                         goto continue_next_new_nt;
7142
7143                 DEBUG2(ql4_printk(KERN_INFO, ha,
7144                                   "Adding  DDB to session = 0x%x\n", idx));
7145
7146                 nt_ddb_idx = vmalloc(fw_idx_size);
7147                 if (!nt_ddb_idx)
7148                         break;
7149
7150                 nt_ddb_idx->fw_ddb_idx = idx;
7151
7152                 ret = qla4xxx_is_session_exists(ha, fw_ddb_entry, NULL);
7153                 if (ret == QLA_SUCCESS) {
7154                         /* free nt_ddb_idx and do not add to list_nt */
7155                         vfree(nt_ddb_idx);
7156                         goto continue_next_new_nt;
7157                 }
7158
7159                 if (target_id < max_ddbs)
7160                         fw_ddb_entry->ddb_link = cpu_to_le16(target_id);
7161
7162                 list_add_tail(&nt_ddb_idx->list, list_nt);
7163
7164                 ret = qla4xxx_sess_conn_setup(ha, fw_ddb_entry, RESET_ADAPTER,
7165                                               idx);
7166                 if (ret == QLA_ERROR)
7167                         goto exit_new_nt_list;
7168
7169 continue_next_new_nt:
7170                 if (next_idx == 0)
7171                         break;
7172         }
7173
7174 exit_new_nt_list:
7175         if (fw_ddb_entry)
7176                 dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
7177 }
7178
7179 /**
7180  * qla4xxx_sysfs_ddb_is_non_persistent - check for non-persistence of ddb entry
7181  * @dev: dev associated with the sysfs entry
7182  * @data: pointer to flashnode session object
7183  *
7184  * Returns:
7185  *      1: if flashnode entry is non-persistent
7186  *      0: if flashnode entry is persistent
7187  **/
7188 static int qla4xxx_sysfs_ddb_is_non_persistent(struct device *dev, void *data)
7189 {
7190         struct iscsi_bus_flash_session *fnode_sess;
7191
7192         if (!iscsi_flashnode_bus_match(dev, NULL))
7193                 return 0;
7194
7195         fnode_sess = iscsi_dev_to_flash_session(dev);
7196
7197         return (fnode_sess->flash_state == DEV_DB_NON_PERSISTENT);
7198 }
7199
7200 /**
7201  * qla4xxx_sysfs_ddb_tgt_create - Create sysfs entry for target
7202  * @ha: pointer to host
7203  * @fw_ddb_entry: flash ddb data
7204  * @idx: target index
7205  * @user: if set then this call is made from userland else from kernel
7206  *
7207  * Returns:
7208  * On sucess: QLA_SUCCESS
7209  * On failure: QLA_ERROR
7210  *
7211  * This create separate sysfs entries for session and connection attributes of
7212  * the given fw ddb entry.
7213  * If this is invoked as a result of a userspace call then the entry is marked
7214  * as nonpersistent using flash_state field.
7215  **/
7216 static int qla4xxx_sysfs_ddb_tgt_create(struct scsi_qla_host *ha,
7217                                         struct dev_db_entry *fw_ddb_entry,
7218                                         uint16_t *idx, int user)
7219 {
7220         struct iscsi_bus_flash_session *fnode_sess = NULL;
7221         struct iscsi_bus_flash_conn *fnode_conn = NULL;
7222         int rc = QLA_ERROR;
7223
7224         fnode_sess = iscsi_create_flashnode_sess(ha->host, *idx,
7225                                                  &qla4xxx_iscsi_transport, 0);
7226         if (!fnode_sess) {
7227                 ql4_printk(KERN_ERR, ha,
7228                            "%s: Unable to create session sysfs entry for flashnode %d of host%lu\n",
7229                            __func__, *idx, ha->host_no);
7230                 goto exit_tgt_create;
7231         }
7232
7233         fnode_conn = iscsi_create_flashnode_conn(ha->host, fnode_sess,
7234                                                  &qla4xxx_iscsi_transport, 0);
7235         if (!fnode_conn) {
7236                 ql4_printk(KERN_ERR, ha,
7237                            "%s: Unable to create conn sysfs entry for flashnode %d of host%lu\n",
7238                            __func__, *idx, ha->host_no);
7239                 goto free_sess;
7240         }
7241
7242         if (user) {
7243                 fnode_sess->flash_state = DEV_DB_NON_PERSISTENT;
7244         } else {
7245                 fnode_sess->flash_state = DEV_DB_PERSISTENT;
7246
7247                 if (*idx == ha->pri_ddb_idx || *idx == ha->sec_ddb_idx)
7248                         fnode_sess->is_boot_target = 1;
7249                 else
7250                         fnode_sess->is_boot_target = 0;
7251         }
7252
7253         rc = qla4xxx_copy_from_fwddb_param(fnode_sess, fnode_conn,
7254                                            fw_ddb_entry);
7255         if (rc)
7256                 goto free_sess;
7257
7258         ql4_printk(KERN_INFO, ha, "%s: sysfs entry %s created\n",
7259                    __func__, fnode_sess->dev.kobj.name);
7260
7261         ql4_printk(KERN_INFO, ha, "%s: sysfs entry %s created\n",
7262                    __func__, fnode_conn->dev.kobj.name);
7263
7264         return QLA_SUCCESS;
7265
7266 free_sess:
7267         iscsi_destroy_flashnode_sess(fnode_sess);
7268
7269 exit_tgt_create:
7270         return QLA_ERROR;
7271 }
7272
7273 /**
7274  * qla4xxx_sysfs_ddb_add - Add new ddb entry in flash
7275  * @shost: pointer to host
7276  * @buf: type of ddb entry (ipv4/ipv6)
7277  * @len: length of buf
7278  *
7279  * This creates new ddb entry in the flash by finding first free index and
7280  * storing default ddb there. And then create sysfs entry for the new ddb entry.
7281  **/
7282 static int qla4xxx_sysfs_ddb_add(struct Scsi_Host *shost, const char *buf,
7283                                  int len)
7284 {
7285         struct scsi_qla_host *ha = to_qla_host(shost);
7286         struct dev_db_entry *fw_ddb_entry = NULL;
7287         dma_addr_t fw_ddb_entry_dma;
7288         struct device *dev;
7289         uint16_t idx = 0;
7290         uint16_t max_ddbs = 0;
7291         uint32_t options = 0;
7292         uint32_t rval = QLA_ERROR;
7293
7294         if (strncasecmp(PORTAL_TYPE_IPV4, buf, 4) &&
7295             strncasecmp(PORTAL_TYPE_IPV6, buf, 4)) {
7296                 DEBUG2(ql4_printk(KERN_ERR, ha, "%s: Invalid portal type\n",
7297                                   __func__));
7298                 goto exit_ddb_add;
7299         }
7300
7301         max_ddbs =  is_qla40XX(ha) ? MAX_PRST_DEV_DB_ENTRIES :
7302                                      MAX_DEV_DB_ENTRIES;
7303
7304         fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
7305                                           &fw_ddb_entry_dma, GFP_KERNEL);
7306         if (!fw_ddb_entry) {
7307                 DEBUG2(ql4_printk(KERN_ERR, ha,
7308                                   "%s: Unable to allocate dma buffer\n",
7309                                   __func__));
7310                 goto exit_ddb_add;
7311         }
7312
7313         dev = iscsi_find_flashnode_sess(ha->host, NULL,
7314                                         qla4xxx_sysfs_ddb_is_non_persistent);
7315         if (dev) {
7316                 ql4_printk(KERN_ERR, ha,
7317                            "%s: A non-persistent entry %s found\n",
7318                            __func__, dev->kobj.name);
7319                 put_device(dev);
7320                 goto exit_ddb_add;
7321         }
7322
7323         /* Index 0 and 1 are reserved for boot target entries */
7324         for (idx = 2; idx < max_ddbs; idx++) {
7325                 if (qla4xxx_flashdb_by_index(ha, fw_ddb_entry,
7326                                              fw_ddb_entry_dma, idx))
7327                         break;
7328         }
7329
7330         if (idx == max_ddbs)
7331                 goto exit_ddb_add;
7332
7333         if (!strncasecmp("ipv6", buf, 4))
7334                 options |= IPV6_DEFAULT_DDB_ENTRY;
7335
7336         rval = qla4xxx_get_default_ddb(ha, options, fw_ddb_entry_dma);
7337         if (rval == QLA_ERROR)
7338                 goto exit_ddb_add;
7339
7340         rval = qla4xxx_sysfs_ddb_tgt_create(ha, fw_ddb_entry, &idx, 1);
7341
7342 exit_ddb_add:
7343         if (fw_ddb_entry)
7344                 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
7345                                   fw_ddb_entry, fw_ddb_entry_dma);
7346         if (rval == QLA_SUCCESS)
7347                 return idx;
7348         else
7349                 return -EIO;
7350 }
7351
7352 /**
7353  * qla4xxx_sysfs_ddb_apply - write the target ddb contents to Flash
7354  * @fnode_sess: pointer to session attrs of flash ddb entry
7355  * @fnode_conn: pointer to connection attrs of flash ddb entry
7356  *
7357  * This writes the contents of target ddb buffer to Flash with a valid cookie
7358  * value in order to make the ddb entry persistent.
7359  **/
7360 static int  qla4xxx_sysfs_ddb_apply(struct iscsi_bus_flash_session *fnode_sess,
7361                                     struct iscsi_bus_flash_conn *fnode_conn)
7362 {
7363         struct Scsi_Host *shost = iscsi_flash_session_to_shost(fnode_sess);
7364         struct scsi_qla_host *ha = to_qla_host(shost);
7365         uint32_t dev_db_start_offset = FLASH_OFFSET_DB_INFO;
7366         struct dev_db_entry *fw_ddb_entry = NULL;
7367         dma_addr_t fw_ddb_entry_dma;
7368         uint32_t options = 0;
7369         int rval = 0;
7370
7371         fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
7372                                           &fw_ddb_entry_dma, GFP_KERNEL);
7373         if (!fw_ddb_entry) {
7374                 DEBUG2(ql4_printk(KERN_ERR, ha,
7375                                   "%s: Unable to allocate dma buffer\n",
7376                                   __func__));
7377                 rval = -ENOMEM;
7378                 goto exit_ddb_apply;
7379         }
7380
7381         if (!strncasecmp(fnode_sess->portal_type, PORTAL_TYPE_IPV6, 4))
7382                 options |= IPV6_DEFAULT_DDB_ENTRY;
7383
7384         rval = qla4xxx_get_default_ddb(ha, options, fw_ddb_entry_dma);
7385         if (rval == QLA_ERROR)
7386                 goto exit_ddb_apply;
7387
7388         dev_db_start_offset += (fnode_sess->target_id *
7389                                 sizeof(*fw_ddb_entry));
7390
7391         qla4xxx_copy_to_fwddb_param(fnode_sess, fnode_conn, fw_ddb_entry);
7392         fw_ddb_entry->cookie = DDB_VALID_COOKIE;
7393
7394         rval = qla4xxx_set_flash(ha, fw_ddb_entry_dma, dev_db_start_offset,
7395                                  sizeof(*fw_ddb_entry), FLASH_OPT_RMW_COMMIT);
7396
7397         if (rval == QLA_SUCCESS) {
7398                 fnode_sess->flash_state = DEV_DB_PERSISTENT;
7399                 ql4_printk(KERN_INFO, ha,
7400                            "%s: flash node %u of host %lu written to flash\n",
7401                            __func__, fnode_sess->target_id, ha->host_no);
7402         } else {
7403                 rval = -EIO;
7404                 ql4_printk(KERN_ERR, ha,
7405                            "%s: Error while writing flash node %u of host %lu to flash\n",
7406                            __func__, fnode_sess->target_id, ha->host_no);
7407         }
7408
7409 exit_ddb_apply:
7410         if (fw_ddb_entry)
7411                 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
7412                                   fw_ddb_entry, fw_ddb_entry_dma);
7413         return rval;
7414 }
7415
7416 static ssize_t qla4xxx_sysfs_ddb_conn_open(struct scsi_qla_host *ha,
7417                                            struct dev_db_entry *fw_ddb_entry,
7418                                            uint16_t idx)
7419 {
7420         struct dev_db_entry *ddb_entry = NULL;
7421         dma_addr_t ddb_entry_dma;
7422         unsigned long wtime;
7423         uint32_t mbx_sts = 0;
7424         uint32_t state = 0, conn_err = 0;
7425         uint16_t tmo = 0;
7426         int ret = 0;
7427
7428         ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*ddb_entry),
7429                                        &ddb_entry_dma, GFP_KERNEL);
7430         if (!ddb_entry) {
7431                 DEBUG2(ql4_printk(KERN_ERR, ha,
7432                                   "%s: Unable to allocate dma buffer\n",
7433                                   __func__));
7434                 return QLA_ERROR;
7435         }
7436
7437         memcpy(ddb_entry, fw_ddb_entry, sizeof(*ddb_entry));
7438
7439         ret = qla4xxx_set_ddb_entry(ha, idx, ddb_entry_dma, &mbx_sts);
7440         if (ret != QLA_SUCCESS) {
7441                 DEBUG2(ql4_printk(KERN_ERR, ha,
7442                                   "%s: Unable to set ddb entry for index %d\n",
7443                                   __func__, idx));
7444                 goto exit_ddb_conn_open;
7445         }
7446
7447         qla4xxx_conn_open(ha, idx);
7448
7449         /* To ensure that sendtargets is done, wait for at least 12 secs */
7450         tmo = ((ha->def_timeout > LOGIN_TOV) &&
7451                (ha->def_timeout < LOGIN_TOV * 10) ?
7452                ha->def_timeout : LOGIN_TOV);
7453
7454         DEBUG2(ql4_printk(KERN_INFO, ha,
7455                           "Default time to wait for login to ddb %d\n", tmo));
7456
7457         wtime = jiffies + (HZ * tmo);
7458         do {
7459                 ret = qla4xxx_get_fwddb_entry(ha, idx, NULL, 0, NULL,
7460                                               NULL, &state, &conn_err, NULL,
7461                                               NULL);
7462                 if (ret == QLA_ERROR)
7463                         continue;
7464
7465                 if (state == DDB_DS_NO_CONNECTION_ACTIVE ||
7466                     state == DDB_DS_SESSION_FAILED)
7467                         break;
7468
7469                 schedule_timeout_uninterruptible(HZ / 10);
7470         } while (time_after(wtime, jiffies));
7471
7472 exit_ddb_conn_open:
7473         if (ddb_entry)
7474                 dma_free_coherent(&ha->pdev->dev, sizeof(*ddb_entry),
7475                                   ddb_entry, ddb_entry_dma);
7476         return ret;
7477 }
7478
7479 static int qla4xxx_ddb_login_st(struct scsi_qla_host *ha,
7480                                 struct dev_db_entry *fw_ddb_entry,
7481                                 uint16_t target_id)
7482 {
7483         struct qla_ddb_index *ddb_idx, *ddb_idx_tmp;
7484         struct list_head list_nt;
7485         uint16_t ddb_index;
7486         int ret = 0;
7487
7488         if (test_bit(AF_ST_DISCOVERY_IN_PROGRESS, &ha->flags)) {
7489                 ql4_printk(KERN_WARNING, ha,
7490                            "%s: A discovery already in progress!\n", __func__);
7491                 return QLA_ERROR;
7492         }
7493
7494         INIT_LIST_HEAD(&list_nt);
7495
7496         set_bit(AF_ST_DISCOVERY_IN_PROGRESS, &ha->flags);
7497
7498         ret = qla4xxx_get_ddb_index(ha, &ddb_index);
7499         if (ret == QLA_ERROR)
7500                 goto exit_login_st_clr_bit;
7501
7502         ret = qla4xxx_sysfs_ddb_conn_open(ha, fw_ddb_entry, ddb_index);
7503         if (ret == QLA_ERROR)
7504                 goto exit_login_st;
7505
7506         qla4xxx_build_new_nt_list(ha, &list_nt, target_id);
7507
7508         list_for_each_entry_safe(ddb_idx, ddb_idx_tmp, &list_nt, list) {
7509                 list_del_init(&ddb_idx->list);
7510                 qla4xxx_clear_ddb_entry(ha, ddb_idx->fw_ddb_idx);
7511                 vfree(ddb_idx);
7512         }
7513
7514 exit_login_st:
7515         if (qla4xxx_clear_ddb_entry(ha, ddb_index) == QLA_ERROR) {
7516                 ql4_printk(KERN_ERR, ha,
7517                            "Unable to clear DDB index = 0x%x\n", ddb_index);
7518         }
7519
7520         clear_bit(ddb_index, ha->ddb_idx_map);
7521
7522 exit_login_st_clr_bit:
7523         clear_bit(AF_ST_DISCOVERY_IN_PROGRESS, &ha->flags);
7524         return ret;
7525 }
7526
7527 static int qla4xxx_ddb_login_nt(struct scsi_qla_host *ha,
7528                                 struct dev_db_entry *fw_ddb_entry,
7529                                 uint16_t idx)
7530 {
7531         int ret = QLA_ERROR;
7532
7533         ret = qla4xxx_is_session_exists(ha, fw_ddb_entry, NULL);
7534         if (ret != QLA_SUCCESS)
7535                 ret = qla4xxx_sess_conn_setup(ha, fw_ddb_entry, RESET_ADAPTER,
7536                                               idx);
7537         else
7538                 ret = -EPERM;
7539
7540         return ret;
7541 }
7542
7543 /**
7544  * qla4xxx_sysfs_ddb_login - Login to the specified target
7545  * @fnode_sess: pointer to session attrs of flash ddb entry
7546  * @fnode_conn: pointer to connection attrs of flash ddb entry
7547  *
7548  * This logs in to the specified target
7549  **/
7550 static int qla4xxx_sysfs_ddb_login(struct iscsi_bus_flash_session *fnode_sess,
7551                                    struct iscsi_bus_flash_conn *fnode_conn)
7552 {
7553         struct Scsi_Host *shost = iscsi_flash_session_to_shost(fnode_sess);
7554         struct scsi_qla_host *ha = to_qla_host(shost);
7555         struct dev_db_entry *fw_ddb_entry = NULL;
7556         dma_addr_t fw_ddb_entry_dma;
7557         uint32_t options = 0;
7558         int ret = 0;
7559
7560         if (fnode_sess->flash_state == DEV_DB_NON_PERSISTENT) {
7561                 ql4_printk(KERN_ERR, ha,
7562                            "%s: Target info is not persistent\n", __func__);
7563                 ret = -EIO;
7564                 goto exit_ddb_login;
7565         }
7566
7567         fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
7568                                           &fw_ddb_entry_dma, GFP_KERNEL);
7569         if (!fw_ddb_entry) {
7570                 DEBUG2(ql4_printk(KERN_ERR, ha,
7571                                   "%s: Unable to allocate dma buffer\n",
7572                                   __func__));
7573                 ret = -ENOMEM;
7574                 goto exit_ddb_login;
7575         }
7576
7577         if (!strncasecmp(fnode_sess->portal_type, PORTAL_TYPE_IPV6, 4))
7578                 options |= IPV6_DEFAULT_DDB_ENTRY;
7579
7580         ret = qla4xxx_get_default_ddb(ha, options, fw_ddb_entry_dma);
7581         if (ret == QLA_ERROR)
7582                 goto exit_ddb_login;
7583
7584         qla4xxx_copy_to_fwddb_param(fnode_sess, fnode_conn, fw_ddb_entry);
7585         fw_ddb_entry->cookie = DDB_VALID_COOKIE;
7586
7587         if (strlen((char *)fw_ddb_entry->iscsi_name) == 0)
7588                 ret = qla4xxx_ddb_login_st(ha, fw_ddb_entry,
7589                                            fnode_sess->target_id);
7590         else
7591                 ret = qla4xxx_ddb_login_nt(ha, fw_ddb_entry,
7592                                            fnode_sess->target_id);
7593
7594         if (ret > 0)
7595                 ret = -EIO;
7596
7597 exit_ddb_login:
7598         if (fw_ddb_entry)
7599                 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
7600                                   fw_ddb_entry, fw_ddb_entry_dma);
7601         return ret;
7602 }
7603
7604 /**
7605  * qla4xxx_sysfs_ddb_logout_sid - Logout session for the specified target
7606  * @cls_sess: pointer to session to be logged out
7607  *
7608  * This performs session log out from the specified target
7609  **/
7610 static int qla4xxx_sysfs_ddb_logout_sid(struct iscsi_cls_session *cls_sess)
7611 {
7612         struct iscsi_session *sess;
7613         struct ddb_entry *ddb_entry = NULL;
7614         struct scsi_qla_host *ha;
7615         struct dev_db_entry *fw_ddb_entry = NULL;
7616         dma_addr_t fw_ddb_entry_dma;
7617         unsigned long flags;
7618         unsigned long wtime;
7619         uint32_t ddb_state;
7620         int options;
7621         int ret = 0;
7622
7623         sess = cls_sess->dd_data;
7624         ddb_entry = sess->dd_data;
7625         ha = ddb_entry->ha;
7626
7627         if (ddb_entry->ddb_type != FLASH_DDB) {
7628                 ql4_printk(KERN_ERR, ha, "%s: Not a flash node session\n",
7629                            __func__);
7630                 ret = -ENXIO;
7631                 goto exit_ddb_logout;
7632         }
7633
7634         if (test_bit(DF_BOOT_TGT, &ddb_entry->flags)) {
7635                 ql4_printk(KERN_ERR, ha,
7636                            "%s: Logout from boot target entry is not permitted.\n",
7637                            __func__);
7638                 ret = -EPERM;
7639                 goto exit_ddb_logout;
7640         }
7641
7642         fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
7643                                           &fw_ddb_entry_dma, GFP_KERNEL);
7644         if (!fw_ddb_entry) {
7645                 ql4_printk(KERN_ERR, ha,
7646                            "%s: Unable to allocate dma buffer\n", __func__);
7647                 ret = -ENOMEM;
7648                 goto exit_ddb_logout;
7649         }
7650
7651         if (test_and_set_bit(DF_DISABLE_RELOGIN, &ddb_entry->flags))
7652                 goto ddb_logout_init;
7653
7654         ret = qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index,
7655                                       fw_ddb_entry, fw_ddb_entry_dma,
7656                                       NULL, NULL, &ddb_state, NULL,
7657                                       NULL, NULL);
7658         if (ret == QLA_ERROR)
7659                 goto ddb_logout_init;
7660
7661         if (ddb_state == DDB_DS_SESSION_ACTIVE)
7662                 goto ddb_logout_init;
7663
7664         /* wait until next relogin is triggered using DF_RELOGIN and
7665          * clear DF_RELOGIN to avoid invocation of further relogin
7666          */
7667         wtime = jiffies + (HZ * RELOGIN_TOV);
7668         do {
7669                 if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags))
7670                         goto ddb_logout_init;
7671
7672                 schedule_timeout_uninterruptible(HZ);
7673         } while ((time_after(wtime, jiffies)));
7674
7675 ddb_logout_init:
7676         atomic_set(&ddb_entry->retry_relogin_timer, INVALID_ENTRY);
7677         atomic_set(&ddb_entry->relogin_timer, 0);
7678
7679         options = LOGOUT_OPTION_CLOSE_SESSION;
7680         qla4xxx_session_logout_ddb(ha, ddb_entry, options);
7681
7682         memset(fw_ddb_entry, 0, sizeof(*fw_ddb_entry));
7683         wtime = jiffies + (HZ * LOGOUT_TOV);
7684         do {
7685                 ret = qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index,
7686                                               fw_ddb_entry, fw_ddb_entry_dma,
7687                                               NULL, NULL, &ddb_state, NULL,
7688                                               NULL, NULL);
7689                 if (ret == QLA_ERROR)
7690                         goto ddb_logout_clr_sess;
7691
7692                 if ((ddb_state == DDB_DS_NO_CONNECTION_ACTIVE) ||
7693                     (ddb_state == DDB_DS_SESSION_FAILED))
7694                         goto ddb_logout_clr_sess;
7695
7696                 schedule_timeout_uninterruptible(HZ);
7697         } while ((time_after(wtime, jiffies)));
7698
7699 ddb_logout_clr_sess:
7700         qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
7701         /*
7702          * we have decremented the reference count of the driver
7703          * when we setup the session to have the driver unload
7704          * to be seamless without actually destroying the
7705          * session
7706          **/
7707         try_module_get(qla4xxx_iscsi_transport.owner);
7708         iscsi_destroy_endpoint(ddb_entry->conn->ep);
7709
7710         spin_lock_irqsave(&ha->hardware_lock, flags);
7711         qla4xxx_free_ddb(ha, ddb_entry);
7712         clear_bit(ddb_entry->fw_ddb_index, ha->ddb_idx_map);
7713         spin_unlock_irqrestore(&ha->hardware_lock, flags);
7714
7715         iscsi_session_teardown(ddb_entry->sess);
7716
7717         clear_bit(DF_DISABLE_RELOGIN, &ddb_entry->flags);
7718         ret = QLA_SUCCESS;
7719
7720 exit_ddb_logout:
7721         if (fw_ddb_entry)
7722                 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
7723                                   fw_ddb_entry, fw_ddb_entry_dma);
7724         return ret;
7725 }
7726
7727 /**
7728  * qla4xxx_sysfs_ddb_logout - Logout from the specified target
7729  * @fnode_sess: pointer to session attrs of flash ddb entry
7730  * @fnode_conn: pointer to connection attrs of flash ddb entry
7731  *
7732  * This performs log out from the specified target
7733  **/
7734 static int qla4xxx_sysfs_ddb_logout(struct iscsi_bus_flash_session *fnode_sess,
7735                                     struct iscsi_bus_flash_conn *fnode_conn)
7736 {
7737         struct Scsi_Host *shost = iscsi_flash_session_to_shost(fnode_sess);
7738         struct scsi_qla_host *ha = to_qla_host(shost);
7739         struct ql4_tuple_ddb *flash_tddb = NULL;
7740         struct ql4_tuple_ddb *tmp_tddb = NULL;
7741         struct dev_db_entry *fw_ddb_entry = NULL;
7742         struct ddb_entry *ddb_entry = NULL;
7743         dma_addr_t fw_ddb_dma;
7744         uint32_t next_idx = 0;
7745         uint32_t state = 0, conn_err = 0;
7746         uint16_t conn_id = 0;
7747         int idx, index;
7748         int status, ret = 0;
7749
7750         fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
7751                                       &fw_ddb_dma);
7752         if (fw_ddb_entry == NULL) {
7753                 ql4_printk(KERN_ERR, ha, "%s:Out of memory\n", __func__);
7754                 ret = -ENOMEM;
7755                 goto exit_ddb_logout;
7756         }
7757
7758         flash_tddb = vzalloc(sizeof(*flash_tddb));
7759         if (!flash_tddb) {
7760                 ql4_printk(KERN_WARNING, ha,
7761                            "%s:Memory Allocation failed.\n", __func__);
7762                 ret = -ENOMEM;
7763                 goto exit_ddb_logout;
7764         }
7765
7766         tmp_tddb = vzalloc(sizeof(*tmp_tddb));
7767         if (!tmp_tddb) {
7768                 ql4_printk(KERN_WARNING, ha,
7769                            "%s:Memory Allocation failed.\n", __func__);
7770                 ret = -ENOMEM;
7771                 goto exit_ddb_logout;
7772         }
7773
7774         if (!fnode_sess->targetname) {
7775                 ql4_printk(KERN_ERR, ha,
7776                            "%s:Cannot logout from SendTarget entry\n",
7777                            __func__);
7778                 ret = -EPERM;
7779                 goto exit_ddb_logout;
7780         }
7781
7782         if (fnode_sess->is_boot_target) {
7783                 ql4_printk(KERN_ERR, ha,
7784                            "%s: Logout from boot target entry is not permitted.\n",
7785                            __func__);
7786                 ret = -EPERM;
7787                 goto exit_ddb_logout;
7788         }
7789
7790         strlcpy(flash_tddb->iscsi_name, fnode_sess->targetname,
7791                 ISCSI_NAME_SIZE);
7792
7793         if (!strncmp(fnode_sess->portal_type, PORTAL_TYPE_IPV6, 4))
7794                 sprintf(flash_tddb->ip_addr, "%pI6", fnode_conn->ipaddress);
7795         else
7796                 sprintf(flash_tddb->ip_addr, "%pI4", fnode_conn->ipaddress);
7797
7798         flash_tddb->tpgt = fnode_sess->tpgt;
7799         flash_tddb->port = fnode_conn->port;
7800
7801         COPY_ISID(flash_tddb->isid, fnode_sess->isid);
7802
7803         for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
7804                 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
7805                 if (ddb_entry == NULL)
7806                         continue;
7807
7808                 if (ddb_entry->ddb_type != FLASH_DDB)
7809                         continue;
7810
7811                 index = ddb_entry->sess->target_id;
7812                 status = qla4xxx_get_fwddb_entry(ha, index, fw_ddb_entry,
7813                                                  fw_ddb_dma, NULL, &next_idx,
7814                                                  &state, &conn_err, NULL,
7815                                                  &conn_id);
7816                 if (status == QLA_ERROR) {
7817                         ret = -ENOMEM;
7818                         break;
7819                 }
7820
7821                 qla4xxx_convert_param_ddb(fw_ddb_entry, tmp_tddb, NULL);
7822
7823                 status = qla4xxx_compare_tuple_ddb(ha, flash_tddb, tmp_tddb,
7824                                                    true);
7825                 if (status == QLA_SUCCESS) {
7826                         ret = qla4xxx_sysfs_ddb_logout_sid(ddb_entry->sess);
7827                         break;
7828                 }
7829         }
7830
7831         if (idx == MAX_DDB_ENTRIES)
7832                 ret = -ESRCH;
7833
7834 exit_ddb_logout:
7835         if (flash_tddb)
7836                 vfree(flash_tddb);
7837         if (tmp_tddb)
7838                 vfree(tmp_tddb);
7839         if (fw_ddb_entry)
7840                 dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
7841
7842         return ret;
7843 }
7844
7845 static int
7846 qla4xxx_sysfs_ddb_get_param(struct iscsi_bus_flash_session *fnode_sess,
7847                             int param, char *buf)
7848 {
7849         struct Scsi_Host *shost = iscsi_flash_session_to_shost(fnode_sess);
7850         struct scsi_qla_host *ha = to_qla_host(shost);
7851         struct iscsi_bus_flash_conn *fnode_conn;
7852         struct ql4_chap_table chap_tbl;
7853         struct device *dev;
7854         int parent_type;
7855         int rc = 0;
7856
7857         dev = iscsi_find_flashnode_conn(fnode_sess);
7858         if (!dev)
7859                 return -EIO;
7860
7861         fnode_conn = iscsi_dev_to_flash_conn(dev);
7862
7863         switch (param) {
7864         case ISCSI_FLASHNODE_IS_FW_ASSIGNED_IPV6:
7865                 rc = sprintf(buf, "%u\n", fnode_conn->is_fw_assigned_ipv6);
7866                 break;
7867         case ISCSI_FLASHNODE_PORTAL_TYPE:
7868                 rc = sprintf(buf, "%s\n", fnode_sess->portal_type);
7869                 break;
7870         case ISCSI_FLASHNODE_AUTO_SND_TGT_DISABLE:
7871                 rc = sprintf(buf, "%u\n", fnode_sess->auto_snd_tgt_disable);
7872                 break;
7873         case ISCSI_FLASHNODE_DISCOVERY_SESS:
7874                 rc = sprintf(buf, "%u\n", fnode_sess->discovery_sess);
7875                 break;
7876         case ISCSI_FLASHNODE_ENTRY_EN:
7877                 rc = sprintf(buf, "%u\n", fnode_sess->entry_state);
7878                 break;
7879         case ISCSI_FLASHNODE_HDR_DGST_EN:
7880                 rc = sprintf(buf, "%u\n", fnode_conn->hdrdgst_en);
7881                 break;
7882         case ISCSI_FLASHNODE_DATA_DGST_EN:
7883                 rc = sprintf(buf, "%u\n", fnode_conn->datadgst_en);
7884                 break;
7885         case ISCSI_FLASHNODE_IMM_DATA_EN:
7886                 rc = sprintf(buf, "%u\n", fnode_sess->imm_data_en);
7887                 break;
7888         case ISCSI_FLASHNODE_INITIAL_R2T_EN:
7889                 rc = sprintf(buf, "%u\n", fnode_sess->initial_r2t_en);
7890                 break;
7891         case ISCSI_FLASHNODE_DATASEQ_INORDER:
7892                 rc = sprintf(buf, "%u\n", fnode_sess->dataseq_inorder_en);
7893                 break;
7894         case ISCSI_FLASHNODE_PDU_INORDER:
7895                 rc = sprintf(buf, "%u\n", fnode_sess->pdu_inorder_en);
7896                 break;
7897         case ISCSI_FLASHNODE_CHAP_AUTH_EN:
7898                 rc = sprintf(buf, "%u\n", fnode_sess->chap_auth_en);
7899                 break;
7900         case ISCSI_FLASHNODE_SNACK_REQ_EN:
7901                 rc = sprintf(buf, "%u\n", fnode_conn->snack_req_en);
7902                 break;
7903         case ISCSI_FLASHNODE_DISCOVERY_LOGOUT_EN:
7904                 rc = sprintf(buf, "%u\n", fnode_sess->discovery_logout_en);
7905                 break;
7906         case ISCSI_FLASHNODE_BIDI_CHAP_EN:
7907                 rc = sprintf(buf, "%u\n", fnode_sess->bidi_chap_en);
7908                 break;
7909         case ISCSI_FLASHNODE_DISCOVERY_AUTH_OPTIONAL:
7910                 rc = sprintf(buf, "%u\n", fnode_sess->discovery_auth_optional);
7911                 break;
7912         case ISCSI_FLASHNODE_ERL:
7913                 rc = sprintf(buf, "%u\n", fnode_sess->erl);
7914                 break;
7915         case ISCSI_FLASHNODE_TCP_TIMESTAMP_STAT:
7916                 rc = sprintf(buf, "%u\n", fnode_conn->tcp_timestamp_stat);
7917                 break;
7918         case ISCSI_FLASHNODE_TCP_NAGLE_DISABLE:
7919                 rc = sprintf(buf, "%u\n", fnode_conn->tcp_nagle_disable);
7920                 break;
7921         case ISCSI_FLASHNODE_TCP_WSF_DISABLE:
7922                 rc = sprintf(buf, "%u\n", fnode_conn->tcp_wsf_disable);
7923                 break;
7924         case ISCSI_FLASHNODE_TCP_TIMER_SCALE:
7925                 rc = sprintf(buf, "%u\n", fnode_conn->tcp_timer_scale);
7926                 break;
7927         case ISCSI_FLASHNODE_TCP_TIMESTAMP_EN:
7928                 rc = sprintf(buf, "%u\n", fnode_conn->tcp_timestamp_en);
7929                 break;
7930         case ISCSI_FLASHNODE_IP_FRAG_DISABLE:
7931                 rc = sprintf(buf, "%u\n", fnode_conn->fragment_disable);
7932                 break;
7933         case ISCSI_FLASHNODE_MAX_RECV_DLENGTH:
7934                 rc = sprintf(buf, "%u\n", fnode_conn->max_recv_dlength);
7935                 break;
7936         case ISCSI_FLASHNODE_MAX_XMIT_DLENGTH:
7937                 rc = sprintf(buf, "%u\n", fnode_conn->max_xmit_dlength);
7938                 break;
7939         case ISCSI_FLASHNODE_FIRST_BURST:
7940                 rc = sprintf(buf, "%u\n", fnode_sess->first_burst);
7941                 break;
7942         case ISCSI_FLASHNODE_DEF_TIME2WAIT:
7943                 rc = sprintf(buf, "%u\n", fnode_sess->time2wait);
7944                 break;
7945         case ISCSI_FLASHNODE_DEF_TIME2RETAIN:
7946                 rc = sprintf(buf, "%u\n", fnode_sess->time2retain);
7947                 break;
7948         case ISCSI_FLASHNODE_MAX_R2T:
7949                 rc = sprintf(buf, "%u\n", fnode_sess->max_r2t);
7950                 break;
7951         case ISCSI_FLASHNODE_KEEPALIVE_TMO:
7952                 rc = sprintf(buf, "%u\n", fnode_conn->keepalive_timeout);
7953                 break;
7954         case ISCSI_FLASHNODE_ISID:
7955                 rc = sprintf(buf, "%pm\n", fnode_sess->isid);
7956                 break;
7957         case ISCSI_FLASHNODE_TSID:
7958                 rc = sprintf(buf, "%u\n", fnode_sess->tsid);
7959                 break;
7960         case ISCSI_FLASHNODE_PORT:
7961                 rc = sprintf(buf, "%d\n", fnode_conn->port);
7962                 break;
7963         case ISCSI_FLASHNODE_MAX_BURST:
7964                 rc = sprintf(buf, "%u\n", fnode_sess->max_burst);
7965                 break;
7966         case ISCSI_FLASHNODE_DEF_TASKMGMT_TMO:
7967                 rc = sprintf(buf, "%u\n",
7968                              fnode_sess->default_taskmgmt_timeout);
7969                 break;
7970         case ISCSI_FLASHNODE_IPADDR:
7971                 if (!strncmp(fnode_sess->portal_type, PORTAL_TYPE_IPV6, 4))
7972                         rc = sprintf(buf, "%pI6\n", fnode_conn->ipaddress);
7973                 else
7974                         rc = sprintf(buf, "%pI4\n", fnode_conn->ipaddress);
7975                 break;
7976         case ISCSI_FLASHNODE_ALIAS:
7977                 if (fnode_sess->targetalias)
7978                         rc = sprintf(buf, "%s\n", fnode_sess->targetalias);
7979                 else
7980                         rc = sprintf(buf, "\n");
7981                 break;
7982         case ISCSI_FLASHNODE_REDIRECT_IPADDR:
7983                 if (!strncmp(fnode_sess->portal_type, PORTAL_TYPE_IPV6, 4))
7984                         rc = sprintf(buf, "%pI6\n",
7985                                      fnode_conn->redirect_ipaddr);
7986                 else
7987                         rc = sprintf(buf, "%pI4\n",
7988                                      fnode_conn->redirect_ipaddr);
7989                 break;
7990         case ISCSI_FLASHNODE_MAX_SEGMENT_SIZE:
7991                 rc = sprintf(buf, "%u\n", fnode_conn->max_segment_size);
7992                 break;
7993         case ISCSI_FLASHNODE_LOCAL_PORT:
7994                 rc = sprintf(buf, "%u\n", fnode_conn->local_port);
7995                 break;
7996         case ISCSI_FLASHNODE_IPV4_TOS:
7997                 rc = sprintf(buf, "%u\n", fnode_conn->ipv4_tos);
7998                 break;
7999         case ISCSI_FLASHNODE_IPV6_TC:
8000                 if (!strncmp(fnode_sess->portal_type, PORTAL_TYPE_IPV6, 4))
8001                         rc = sprintf(buf, "%u\n",
8002                                      fnode_conn->ipv6_traffic_class);
8003                 else
8004                         rc = sprintf(buf, "\n");
8005                 break;
8006         case ISCSI_FLASHNODE_IPV6_FLOW_LABEL:
8007                 rc = sprintf(buf, "%u\n", fnode_conn->ipv6_flow_label);
8008                 break;
8009         case ISCSI_FLASHNODE_LINK_LOCAL_IPV6:
8010                 if (!strncmp(fnode_sess->portal_type, PORTAL_TYPE_IPV6, 4))
8011                         rc = sprintf(buf, "%pI6\n",
8012                                      fnode_conn->link_local_ipv6_addr);
8013                 else
8014                         rc = sprintf(buf, "\n");
8015                 break;
8016         case ISCSI_FLASHNODE_DISCOVERY_PARENT_IDX:
8017                 rc = sprintf(buf, "%u\n", fnode_sess->discovery_parent_idx);
8018                 break;
8019         case ISCSI_FLASHNODE_DISCOVERY_PARENT_TYPE:
8020                 if (fnode_sess->discovery_parent_type == DDB_ISNS)
8021                         parent_type = ISCSI_DISC_PARENT_ISNS;
8022                 else if (fnode_sess->discovery_parent_type == DDB_NO_LINK)
8023                         parent_type = ISCSI_DISC_PARENT_UNKNOWN;
8024                 else if (fnode_sess->discovery_parent_type < MAX_DDB_ENTRIES)
8025                         parent_type = ISCSI_DISC_PARENT_SENDTGT;
8026                 else
8027                         parent_type = ISCSI_DISC_PARENT_UNKNOWN;
8028
8029                 rc = sprintf(buf, "%s\n",
8030                              iscsi_get_discovery_parent_name(parent_type));
8031                 break;
8032         case ISCSI_FLASHNODE_NAME:
8033                 if (fnode_sess->targetname)
8034                         rc = sprintf(buf, "%s\n", fnode_sess->targetname);
8035                 else
8036                         rc = sprintf(buf, "\n");
8037                 break;
8038         case ISCSI_FLASHNODE_TPGT:
8039                 rc = sprintf(buf, "%u\n", fnode_sess->tpgt);
8040                 break;
8041         case ISCSI_FLASHNODE_TCP_XMIT_WSF:
8042                 rc = sprintf(buf, "%u\n", fnode_conn->tcp_xmit_wsf);
8043                 break;
8044         case ISCSI_FLASHNODE_TCP_RECV_WSF:
8045                 rc = sprintf(buf, "%u\n", fnode_conn->tcp_recv_wsf);
8046                 break;
8047         case ISCSI_FLASHNODE_CHAP_OUT_IDX:
8048                 rc = sprintf(buf, "%u\n", fnode_sess->chap_out_idx);
8049                 break;
8050         case ISCSI_FLASHNODE_USERNAME:
8051                 if (fnode_sess->chap_auth_en) {
8052                         qla4xxx_get_uni_chap_at_index(ha,
8053                                                       chap_tbl.name,
8054                                                       chap_tbl.secret,
8055                                                       fnode_sess->chap_out_idx);
8056                         rc = sprintf(buf, "%s\n", chap_tbl.name);
8057                 } else {
8058                         rc = sprintf(buf, "\n");
8059                 }
8060                 break;
8061         case ISCSI_FLASHNODE_PASSWORD:
8062                 if (fnode_sess->chap_auth_en) {
8063                         qla4xxx_get_uni_chap_at_index(ha,
8064                                                       chap_tbl.name,
8065                                                       chap_tbl.secret,
8066                                                       fnode_sess->chap_out_idx);
8067                         rc = sprintf(buf, "%s\n", chap_tbl.secret);
8068                 } else {
8069                         rc = sprintf(buf, "\n");
8070                 }
8071                 break;
8072         case ISCSI_FLASHNODE_STATSN:
8073                 rc = sprintf(buf, "%u\n", fnode_conn->statsn);
8074                 break;
8075         case ISCSI_FLASHNODE_EXP_STATSN:
8076                 rc = sprintf(buf, "%u\n", fnode_conn->exp_statsn);
8077                 break;
8078         case ISCSI_FLASHNODE_IS_BOOT_TGT:
8079                 rc = sprintf(buf, "%u\n", fnode_sess->is_boot_target);
8080                 break;
8081         default:
8082                 rc = -ENOSYS;
8083                 break;
8084         }
8085
8086         put_device(dev);
8087         return rc;
8088 }
8089
8090 /**
8091  * qla4xxx_sysfs_ddb_set_param - Set parameter for firmware DDB entry
8092  * @fnode_sess: pointer to session attrs of flash ddb entry
8093  * @fnode_conn: pointer to connection attrs of flash ddb entry
8094  * @data: Parameters and their values to update
8095  * @len: len of data
8096  *
8097  * This sets the parameter of flash ddb entry and writes them to flash
8098  **/
8099 static int
8100 qla4xxx_sysfs_ddb_set_param(struct iscsi_bus_flash_session *fnode_sess,
8101                             struct iscsi_bus_flash_conn *fnode_conn,
8102                             void *data, int len)
8103 {
8104         struct Scsi_Host *shost = iscsi_flash_session_to_shost(fnode_sess);
8105         struct scsi_qla_host *ha = to_qla_host(shost);
8106         struct iscsi_flashnode_param_info *fnode_param;
8107         struct ql4_chap_table chap_tbl;
8108         struct nlattr *attr;
8109         uint16_t chap_out_idx = INVALID_ENTRY;
8110         int rc = QLA_ERROR;
8111         uint32_t rem = len;
8112
8113         memset((void *)&chap_tbl, 0, sizeof(chap_tbl));
8114         nla_for_each_attr(attr, data, len, rem) {
8115                 if (nla_len(attr) < sizeof(*fnode_param)) {
8116                         rc = -EINVAL;
8117                         goto exit_set_param;
8118                 }
8119
8120                 fnode_param = nla_data(attr);
8121
8122                 switch (fnode_param->param) {
8123                 case ISCSI_FLASHNODE_IS_FW_ASSIGNED_IPV6:
8124                         fnode_conn->is_fw_assigned_ipv6 = fnode_param->value[0];
8125                         break;
8126                 case ISCSI_FLASHNODE_PORTAL_TYPE:
8127                         memcpy(fnode_sess->portal_type, fnode_param->value,
8128                                strlen(fnode_sess->portal_type));
8129                         break;
8130                 case ISCSI_FLASHNODE_AUTO_SND_TGT_DISABLE:
8131                         fnode_sess->auto_snd_tgt_disable =
8132                                                         fnode_param->value[0];
8133                         break;
8134                 case ISCSI_FLASHNODE_DISCOVERY_SESS:
8135                         fnode_sess->discovery_sess = fnode_param->value[0];
8136                         break;
8137                 case ISCSI_FLASHNODE_ENTRY_EN:
8138                         fnode_sess->entry_state = fnode_param->value[0];
8139                         break;
8140                 case ISCSI_FLASHNODE_HDR_DGST_EN:
8141                         fnode_conn->hdrdgst_en = fnode_param->value[0];
8142                         break;
8143                 case ISCSI_FLASHNODE_DATA_DGST_EN:
8144                         fnode_conn->datadgst_en = fnode_param->value[0];
8145                         break;
8146                 case ISCSI_FLASHNODE_IMM_DATA_EN:
8147                         fnode_sess->imm_data_en = fnode_param->value[0];
8148                         break;
8149                 case ISCSI_FLASHNODE_INITIAL_R2T_EN:
8150                         fnode_sess->initial_r2t_en = fnode_param->value[0];
8151                         break;
8152                 case ISCSI_FLASHNODE_DATASEQ_INORDER:
8153                         fnode_sess->dataseq_inorder_en = fnode_param->value[0];
8154                         break;
8155                 case ISCSI_FLASHNODE_PDU_INORDER:
8156                         fnode_sess->pdu_inorder_en = fnode_param->value[0];
8157                         break;
8158                 case ISCSI_FLASHNODE_CHAP_AUTH_EN:
8159                         fnode_sess->chap_auth_en = fnode_param->value[0];
8160                         /* Invalidate chap index if chap auth is disabled */
8161                         if (!fnode_sess->chap_auth_en)
8162                                 fnode_sess->chap_out_idx = INVALID_ENTRY;
8163
8164                         break;
8165                 case ISCSI_FLASHNODE_SNACK_REQ_EN:
8166                         fnode_conn->snack_req_en = fnode_param->value[0];
8167                         break;
8168                 case ISCSI_FLASHNODE_DISCOVERY_LOGOUT_EN:
8169                         fnode_sess->discovery_logout_en = fnode_param->value[0];
8170                         break;
8171                 case ISCSI_FLASHNODE_BIDI_CHAP_EN:
8172                         fnode_sess->bidi_chap_en = fnode_param->value[0];
8173                         break;
8174                 case ISCSI_FLASHNODE_DISCOVERY_AUTH_OPTIONAL:
8175                         fnode_sess->discovery_auth_optional =
8176                                                         fnode_param->value[0];
8177                         break;
8178                 case ISCSI_FLASHNODE_ERL:
8179                         fnode_sess->erl = fnode_param->value[0];
8180                         break;
8181                 case ISCSI_FLASHNODE_TCP_TIMESTAMP_STAT:
8182                         fnode_conn->tcp_timestamp_stat = fnode_param->value[0];
8183                         break;
8184                 case ISCSI_FLASHNODE_TCP_NAGLE_DISABLE:
8185                         fnode_conn->tcp_nagle_disable = fnode_param->value[0];
8186                         break;
8187                 case ISCSI_FLASHNODE_TCP_WSF_DISABLE:
8188                         fnode_conn->tcp_wsf_disable = fnode_param->value[0];
8189                         break;
8190                 case ISCSI_FLASHNODE_TCP_TIMER_SCALE:
8191                         fnode_conn->tcp_timer_scale = fnode_param->value[0];
8192                         break;
8193                 case ISCSI_FLASHNODE_TCP_TIMESTAMP_EN:
8194                         fnode_conn->tcp_timestamp_en = fnode_param->value[0];
8195                         break;
8196                 case ISCSI_FLASHNODE_IP_FRAG_DISABLE:
8197                         fnode_conn->fragment_disable = fnode_param->value[0];
8198                         break;
8199                 case ISCSI_FLASHNODE_MAX_RECV_DLENGTH:
8200                         fnode_conn->max_recv_dlength =
8201                                         *(unsigned *)fnode_param->value;
8202                         break;
8203                 case ISCSI_FLASHNODE_MAX_XMIT_DLENGTH:
8204                         fnode_conn->max_xmit_dlength =
8205                                         *(unsigned *)fnode_param->value;
8206                         break;
8207                 case ISCSI_FLASHNODE_FIRST_BURST:
8208                         fnode_sess->first_burst =
8209                                         *(unsigned *)fnode_param->value;
8210                         break;
8211                 case ISCSI_FLASHNODE_DEF_TIME2WAIT:
8212                         fnode_sess->time2wait = *(uint16_t *)fnode_param->value;
8213                         break;
8214                 case ISCSI_FLASHNODE_DEF_TIME2RETAIN:
8215                         fnode_sess->time2retain =
8216                                                 *(uint16_t *)fnode_param->value;
8217                         break;
8218                 case ISCSI_FLASHNODE_MAX_R2T:
8219                         fnode_sess->max_r2t =
8220                                         *(uint16_t *)fnode_param->value;
8221                         break;
8222                 case ISCSI_FLASHNODE_KEEPALIVE_TMO:
8223                         fnode_conn->keepalive_timeout =
8224                                 *(uint16_t *)fnode_param->value;
8225                         break;
8226                 case ISCSI_FLASHNODE_ISID:
8227                         memcpy(fnode_sess->isid, fnode_param->value,
8228                                sizeof(fnode_sess->isid));
8229                         break;
8230                 case ISCSI_FLASHNODE_TSID:
8231                         fnode_sess->tsid = *(uint16_t *)fnode_param->value;
8232                         break;
8233                 case ISCSI_FLASHNODE_PORT:
8234                         fnode_conn->port = *(uint16_t *)fnode_param->value;
8235                         break;
8236                 case ISCSI_FLASHNODE_MAX_BURST:
8237                         fnode_sess->max_burst = *(unsigned *)fnode_param->value;
8238                         break;
8239                 case ISCSI_FLASHNODE_DEF_TASKMGMT_TMO:
8240                         fnode_sess->default_taskmgmt_timeout =
8241                                                 *(uint16_t *)fnode_param->value;
8242                         break;
8243                 case ISCSI_FLASHNODE_IPADDR:
8244                         memcpy(fnode_conn->ipaddress, fnode_param->value,
8245                                IPv6_ADDR_LEN);
8246                         break;
8247                 case ISCSI_FLASHNODE_ALIAS:
8248                         rc = iscsi_switch_str_param(&fnode_sess->targetalias,
8249                                                     (char *)fnode_param->value);
8250                         break;
8251                 case ISCSI_FLASHNODE_REDIRECT_IPADDR:
8252                         memcpy(fnode_conn->redirect_ipaddr, fnode_param->value,
8253                                IPv6_ADDR_LEN);
8254                         break;
8255                 case ISCSI_FLASHNODE_MAX_SEGMENT_SIZE:
8256                         fnode_conn->max_segment_size =
8257                                         *(unsigned *)fnode_param->value;
8258                         break;
8259                 case ISCSI_FLASHNODE_LOCAL_PORT:
8260                         fnode_conn->local_port =
8261                                                 *(uint16_t *)fnode_param->value;
8262                         break;
8263                 case ISCSI_FLASHNODE_IPV4_TOS:
8264                         fnode_conn->ipv4_tos = fnode_param->value[0];
8265                         break;
8266                 case ISCSI_FLASHNODE_IPV6_TC:
8267                         fnode_conn->ipv6_traffic_class = fnode_param->value[0];
8268                         break;
8269                 case ISCSI_FLASHNODE_IPV6_FLOW_LABEL:
8270                         fnode_conn->ipv6_flow_label = fnode_param->value[0];
8271                         break;
8272                 case ISCSI_FLASHNODE_NAME:
8273                         rc = iscsi_switch_str_param(&fnode_sess->targetname,
8274                                                     (char *)fnode_param->value);
8275                         break;
8276                 case ISCSI_FLASHNODE_TPGT:
8277                         fnode_sess->tpgt = *(uint16_t *)fnode_param->value;
8278                         break;
8279                 case ISCSI_FLASHNODE_LINK_LOCAL_IPV6:
8280                         memcpy(fnode_conn->link_local_ipv6_addr,
8281                                fnode_param->value, IPv6_ADDR_LEN);
8282                         break;
8283                 case ISCSI_FLASHNODE_DISCOVERY_PARENT_IDX:
8284                         fnode_sess->discovery_parent_idx =
8285                                                 *(uint16_t *)fnode_param->value;
8286                         break;
8287                 case ISCSI_FLASHNODE_TCP_XMIT_WSF:
8288                         fnode_conn->tcp_xmit_wsf =
8289                                                 *(uint8_t *)fnode_param->value;
8290                         break;
8291                 case ISCSI_FLASHNODE_TCP_RECV_WSF:
8292                         fnode_conn->tcp_recv_wsf =
8293                                                 *(uint8_t *)fnode_param->value;
8294                         break;
8295                 case ISCSI_FLASHNODE_STATSN:
8296                         fnode_conn->statsn = *(uint32_t *)fnode_param->value;
8297                         break;
8298                 case ISCSI_FLASHNODE_EXP_STATSN:
8299                         fnode_conn->exp_statsn =
8300                                                 *(uint32_t *)fnode_param->value;
8301                         break;
8302                 case ISCSI_FLASHNODE_CHAP_OUT_IDX:
8303                         chap_out_idx = *(uint16_t *)fnode_param->value;
8304                         if (!qla4xxx_get_uni_chap_at_index(ha,
8305                                                            chap_tbl.name,
8306                                                            chap_tbl.secret,
8307                                                            chap_out_idx)) {
8308                                 fnode_sess->chap_out_idx = chap_out_idx;
8309                                 /* Enable chap auth if chap index is valid */
8310                                 fnode_sess->chap_auth_en = QL4_PARAM_ENABLE;
8311                         }
8312                         break;
8313                 default:
8314                         ql4_printk(KERN_ERR, ha,
8315                                    "%s: No such sysfs attribute\n", __func__);
8316                         rc = -ENOSYS;
8317                         goto exit_set_param;
8318                 }
8319         }
8320
8321         rc = qla4xxx_sysfs_ddb_apply(fnode_sess, fnode_conn);
8322
8323 exit_set_param:
8324         return rc;
8325 }
8326
8327 /**
8328  * qla4xxx_sysfs_ddb_delete - Delete firmware DDB entry
8329  * @fnode_sess: pointer to session attrs of flash ddb entry
8330  *
8331  * This invalidates the flash ddb entry at the given index
8332  **/
8333 static int qla4xxx_sysfs_ddb_delete(struct iscsi_bus_flash_session *fnode_sess)
8334 {
8335         struct Scsi_Host *shost = iscsi_flash_session_to_shost(fnode_sess);
8336         struct scsi_qla_host *ha = to_qla_host(shost);
8337         uint32_t dev_db_start_offset;
8338         uint32_t dev_db_end_offset;
8339         struct dev_db_entry *fw_ddb_entry = NULL;
8340         dma_addr_t fw_ddb_entry_dma;
8341         uint16_t *ddb_cookie = NULL;
8342         size_t ddb_size = 0;
8343         void *pddb = NULL;
8344         int target_id;
8345         int rc = 0;
8346
8347         if (fnode_sess->is_boot_target) {
8348                 rc = -EPERM;
8349                 DEBUG2(ql4_printk(KERN_ERR, ha,
8350                                   "%s: Deletion of boot target entry is not permitted.\n",
8351                                   __func__));
8352                 goto exit_ddb_del;
8353         }
8354
8355         if (fnode_sess->flash_state == DEV_DB_NON_PERSISTENT)
8356                 goto sysfs_ddb_del;
8357
8358         if (is_qla40XX(ha)) {
8359                 dev_db_start_offset = FLASH_OFFSET_DB_INFO;
8360                 dev_db_end_offset = FLASH_OFFSET_DB_END;
8361                 dev_db_start_offset += (fnode_sess->target_id *
8362                                        sizeof(*fw_ddb_entry));
8363                 ddb_size = sizeof(*fw_ddb_entry);
8364         } else {
8365                 dev_db_start_offset = FLASH_RAW_ACCESS_ADDR +
8366                                       (ha->hw.flt_region_ddb << 2);
8367                 /* flt_ddb_size is DDB table size for both ports
8368                  * so divide it by 2 to calculate the offset for second port
8369                  */
8370                 if (ha->port_num == 1)
8371                         dev_db_start_offset += (ha->hw.flt_ddb_size / 2);
8372
8373                 dev_db_end_offset = dev_db_start_offset +
8374                                     (ha->hw.flt_ddb_size / 2);
8375
8376                 dev_db_start_offset += (fnode_sess->target_id *
8377                                        sizeof(*fw_ddb_entry));
8378                 dev_db_start_offset += offsetof(struct dev_db_entry, cookie);
8379
8380                 ddb_size = sizeof(*ddb_cookie);
8381         }
8382
8383         DEBUG2(ql4_printk(KERN_ERR, ha, "%s: start offset=%u, end offset=%u\n",
8384                           __func__, dev_db_start_offset, dev_db_end_offset));
8385
8386         if (dev_db_start_offset > dev_db_end_offset) {
8387                 rc = -EIO;
8388                 DEBUG2(ql4_printk(KERN_ERR, ha, "%s:Invalid DDB index %u\n",
8389                                   __func__, fnode_sess->target_id));
8390                 goto exit_ddb_del;
8391         }
8392
8393         pddb = dma_alloc_coherent(&ha->pdev->dev, ddb_size,
8394                                   &fw_ddb_entry_dma, GFP_KERNEL);
8395         if (!pddb) {
8396                 rc = -ENOMEM;
8397                 DEBUG2(ql4_printk(KERN_ERR, ha,
8398                                   "%s: Unable to allocate dma buffer\n",
8399                                   __func__));
8400                 goto exit_ddb_del;
8401         }
8402
8403         if (is_qla40XX(ha)) {
8404                 fw_ddb_entry = pddb;
8405                 memset(fw_ddb_entry, 0, ddb_size);
8406                 ddb_cookie = &fw_ddb_entry->cookie;
8407         } else {
8408                 ddb_cookie = pddb;
8409         }
8410
8411         /* invalidate the cookie */
8412         *ddb_cookie = 0xFFEE;
8413         qla4xxx_set_flash(ha, fw_ddb_entry_dma, dev_db_start_offset,
8414                           ddb_size, FLASH_OPT_RMW_COMMIT);
8415
8416 sysfs_ddb_del:
8417         target_id = fnode_sess->target_id;
8418         iscsi_destroy_flashnode_sess(fnode_sess);
8419         ql4_printk(KERN_INFO, ha,
8420                    "%s: session and conn entries for flashnode %u of host %lu deleted\n",
8421                    __func__, target_id, ha->host_no);
8422 exit_ddb_del:
8423         if (pddb)
8424                 dma_free_coherent(&ha->pdev->dev, ddb_size, pddb,
8425                                   fw_ddb_entry_dma);
8426         return rc;
8427 }
8428
8429 /**
8430  * qla4xxx_sysfs_ddb_export - Create sysfs entries for firmware DDBs
8431  * @ha: pointer to adapter structure
8432  *
8433  * Export the firmware DDB for all send targets and normal targets to sysfs.
8434  **/
8435 int qla4xxx_sysfs_ddb_export(struct scsi_qla_host *ha)
8436 {
8437         struct dev_db_entry *fw_ddb_entry = NULL;
8438         dma_addr_t fw_ddb_entry_dma;
8439         uint16_t max_ddbs;
8440         uint16_t idx = 0;
8441         int ret = QLA_SUCCESS;
8442
8443         fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev,
8444                                           sizeof(*fw_ddb_entry),
8445                                           &fw_ddb_entry_dma, GFP_KERNEL);
8446         if (!fw_ddb_entry) {
8447                 DEBUG2(ql4_printk(KERN_ERR, ha,
8448                                   "%s: Unable to allocate dma buffer\n",
8449                                   __func__));
8450                 return -ENOMEM;
8451         }
8452
8453         max_ddbs =  is_qla40XX(ha) ? MAX_PRST_DEV_DB_ENTRIES :
8454                                      MAX_DEV_DB_ENTRIES;
8455
8456         for (idx = 0; idx < max_ddbs; idx++) {
8457                 if (qla4xxx_flashdb_by_index(ha, fw_ddb_entry, fw_ddb_entry_dma,
8458                                              idx))
8459                         continue;
8460
8461                 ret = qla4xxx_sysfs_ddb_tgt_create(ha, fw_ddb_entry, &idx, 0);
8462                 if (ret) {
8463                         ret = -EIO;
8464                         break;
8465                 }
8466         }
8467
8468         dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry), fw_ddb_entry,
8469                           fw_ddb_entry_dma);
8470
8471         return ret;
8472 }
8473
8474 static void qla4xxx_sysfs_ddb_remove(struct scsi_qla_host *ha)
8475 {
8476         iscsi_destroy_all_flashnode(ha->host);
8477 }
8478
8479 /**
8480  * qla4xxx_build_ddb_list - Build ddb list and setup sessions
8481  * @ha: pointer to adapter structure
8482  * @is_reset: Is this init path or reset path
8483  *
8484  * Create a list of sendtargets (st) from firmware DDBs, issue send targets
8485  * using connection open, then create the list of normal targets (nt)
8486  * from firmware DDBs. Based on the list of nt setup session and connection
8487  * objects.
8488  **/
8489 void qla4xxx_build_ddb_list(struct scsi_qla_host *ha, int is_reset)
8490 {
8491         uint16_t tmo = 0;
8492         struct list_head list_st, list_nt;
8493         struct qla_ddb_index  *st_ddb_idx, *st_ddb_idx_tmp;
8494         unsigned long wtime;
8495
8496         if (!test_bit(AF_LINK_UP, &ha->flags)) {
8497                 set_bit(AF_BUILD_DDB_LIST, &ha->flags);
8498                 ha->is_reset = is_reset;
8499                 return;
8500         }
8501
8502         INIT_LIST_HEAD(&list_st);
8503         INIT_LIST_HEAD(&list_nt);
8504
8505         qla4xxx_build_st_list(ha, &list_st);
8506
8507         /* Before issuing conn open mbox, ensure all IPs states are configured
8508          * Note, conn open fails if IPs are not configured
8509          */
8510         qla4xxx_wait_for_ip_configuration(ha);
8511
8512         /* Go thru the STs and fire the sendtargets by issuing conn open mbx */
8513         list_for_each_entry_safe(st_ddb_idx, st_ddb_idx_tmp, &list_st, list) {
8514                 qla4xxx_conn_open(ha, st_ddb_idx->fw_ddb_idx);
8515         }
8516
8517         /* Wait to ensure all sendtargets are done for min 12 sec wait */
8518         tmo = ((ha->def_timeout > LOGIN_TOV) &&
8519                (ha->def_timeout < LOGIN_TOV * 10) ?
8520                ha->def_timeout : LOGIN_TOV);
8521
8522         DEBUG2(ql4_printk(KERN_INFO, ha,
8523                           "Default time to wait for build ddb %d\n", tmo));
8524
8525         wtime = jiffies + (HZ * tmo);
8526         do {
8527                 if (list_empty(&list_st))
8528                         break;
8529
8530                 qla4xxx_remove_failed_ddb(ha, &list_st);
8531                 schedule_timeout_uninterruptible(HZ / 10);
8532         } while (time_after(wtime, jiffies));
8533
8534
8535         qla4xxx_build_nt_list(ha, &list_nt, &list_st, is_reset);
8536
8537         qla4xxx_free_ddb_list(&list_st);
8538         qla4xxx_free_ddb_list(&list_nt);
8539
8540         qla4xxx_free_ddb_index(ha);
8541 }
8542
8543 /**
8544  * qla4xxx_wait_login_resp_boot_tgt -  Wait for iSCSI boot target login
8545  * response.
8546  * @ha: pointer to adapter structure
8547  *
8548  * When the boot entry is normal iSCSI target then DF_BOOT_TGT flag will be
8549  * set in DDB and we will wait for login response of boot targets during
8550  * probe.
8551  **/
8552 static void qla4xxx_wait_login_resp_boot_tgt(struct scsi_qla_host *ha)
8553 {
8554         struct ddb_entry *ddb_entry;
8555         struct dev_db_entry *fw_ddb_entry = NULL;
8556         dma_addr_t fw_ddb_entry_dma;
8557         unsigned long wtime;
8558         uint32_t ddb_state;
8559         int max_ddbs, idx, ret;
8560
8561         max_ddbs =  is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
8562                                      MAX_DEV_DB_ENTRIES;
8563
8564         fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
8565                                           &fw_ddb_entry_dma, GFP_KERNEL);
8566         if (!fw_ddb_entry) {
8567                 ql4_printk(KERN_ERR, ha,
8568                            "%s: Unable to allocate dma buffer\n", __func__);
8569                 goto exit_login_resp;
8570         }
8571
8572         wtime = jiffies + (HZ * BOOT_LOGIN_RESP_TOV);
8573
8574         for (idx = 0; idx < max_ddbs; idx++) {
8575                 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
8576                 if (ddb_entry == NULL)
8577                         continue;
8578
8579                 if (test_bit(DF_BOOT_TGT, &ddb_entry->flags)) {
8580                         DEBUG2(ql4_printk(KERN_INFO, ha,
8581                                           "%s: DDB index [%d]\n", __func__,
8582                                           ddb_entry->fw_ddb_index));
8583                         do {
8584                                 ret = qla4xxx_get_fwddb_entry(ha,
8585                                                 ddb_entry->fw_ddb_index,
8586                                                 fw_ddb_entry, fw_ddb_entry_dma,
8587                                                 NULL, NULL, &ddb_state, NULL,
8588                                                 NULL, NULL);
8589                                 if (ret == QLA_ERROR)
8590                                         goto exit_login_resp;
8591
8592                                 if ((ddb_state == DDB_DS_SESSION_ACTIVE) ||
8593                                     (ddb_state == DDB_DS_SESSION_FAILED))
8594                                         break;
8595
8596                                 schedule_timeout_uninterruptible(HZ);
8597
8598                         } while ((time_after(wtime, jiffies)));
8599
8600                         if (!time_after(wtime, jiffies)) {
8601                                 DEBUG2(ql4_printk(KERN_INFO, ha,
8602                                                   "%s: Login response wait timer expired\n",
8603                                                   __func__));
8604                                  goto exit_login_resp;
8605                         }
8606                 }
8607         }
8608
8609 exit_login_resp:
8610         if (fw_ddb_entry)
8611                 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
8612                                   fw_ddb_entry, fw_ddb_entry_dma);
8613 }
8614
8615 /**
8616  * qla4xxx_probe_adapter - callback function to probe HBA
8617  * @pdev: pointer to pci_dev structure
8618  * @pci_device_id: pointer to pci_device entry
8619  *
8620  * This routine will probe for Qlogic 4xxx iSCSI host adapters.
8621  * It returns zero if successful. It also initializes all data necessary for
8622  * the driver.
8623  **/
8624 static int qla4xxx_probe_adapter(struct pci_dev *pdev,
8625                                  const struct pci_device_id *ent)
8626 {
8627         int ret = -ENODEV, status;
8628         struct Scsi_Host *host;
8629         struct scsi_qla_host *ha;
8630         uint8_t init_retry_count = 0;
8631         char buf[34];
8632         struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
8633         uint32_t dev_state;
8634
8635         if (pci_enable_device(pdev))
8636                 return -1;
8637
8638         host = iscsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha), 0);
8639         if (host == NULL) {
8640                 printk(KERN_WARNING
8641                        "qla4xxx: Couldn't allocate host from scsi layer!\n");
8642                 goto probe_disable_device;
8643         }
8644
8645         /* Clear our data area */
8646         ha = to_qla_host(host);
8647         memset(ha, 0, sizeof(*ha));
8648
8649         /* Save the information from PCI BIOS.  */
8650         ha->pdev = pdev;
8651         ha->host = host;
8652         ha->host_no = host->host_no;
8653         ha->func_num = PCI_FUNC(ha->pdev->devfn);
8654
8655         pci_enable_pcie_error_reporting(pdev);
8656
8657         /* Setup Runtime configurable options */
8658         if (is_qla8022(ha)) {
8659                 ha->isp_ops = &qla4_82xx_isp_ops;
8660                 ha->reg_tbl = (uint32_t *) qla4_82xx_reg_tbl;
8661                 ha->qdr_sn_window = -1;
8662                 ha->ddr_mn_window = -1;
8663                 ha->curr_window = 255;
8664                 nx_legacy_intr = &legacy_intr[ha->func_num];
8665                 ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
8666                 ha->nx_legacy_intr.tgt_status_reg =
8667                         nx_legacy_intr->tgt_status_reg;
8668                 ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
8669                 ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
8670         } else if (is_qla8032(ha) || is_qla8042(ha)) {
8671                 ha->isp_ops = &qla4_83xx_isp_ops;
8672                 ha->reg_tbl = (uint32_t *)qla4_83xx_reg_tbl;
8673         } else {
8674                 ha->isp_ops = &qla4xxx_isp_ops;
8675         }
8676
8677         if (is_qla80XX(ha)) {
8678                 rwlock_init(&ha->hw_lock);
8679                 ha->pf_bit = ha->func_num << 16;
8680                 /* Set EEH reset type to fundamental if required by hba */
8681                 pdev->needs_freset = 1;
8682         }
8683
8684         /* Configure PCI I/O space. */
8685         ret = ha->isp_ops->iospace_config(ha);
8686         if (ret)
8687                 goto probe_failed_ioconfig;
8688
8689         ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
8690                    pdev->device, pdev->irq, ha->reg);
8691
8692         qla4xxx_config_dma_addressing(ha);
8693
8694         /* Initialize lists and spinlocks. */
8695         INIT_LIST_HEAD(&ha->free_srb_q);
8696
8697         mutex_init(&ha->mbox_sem);
8698         mutex_init(&ha->chap_sem);
8699         init_completion(&ha->mbx_intr_comp);
8700         init_completion(&ha->disable_acb_comp);
8701         init_completion(&ha->idc_comp);
8702         init_completion(&ha->link_up_comp);
8703
8704         spin_lock_init(&ha->hardware_lock);
8705         spin_lock_init(&ha->work_lock);
8706
8707         /* Initialize work list */
8708         INIT_LIST_HEAD(&ha->work_list);
8709
8710         /* Allocate dma buffers */
8711         if (qla4xxx_mem_alloc(ha)) {
8712                 ql4_printk(KERN_WARNING, ha,
8713                     "[ERROR] Failed to allocate memory for adapter\n");
8714
8715                 ret = -ENOMEM;
8716                 goto probe_failed;
8717         }
8718
8719         host->cmd_per_lun = 3;
8720         host->max_channel = 0;
8721         host->max_lun = MAX_LUNS - 1;
8722         host->max_id = MAX_TARGETS;
8723         host->max_cmd_len = IOCB_MAX_CDB_LEN;
8724         host->can_queue = MAX_SRBS ;
8725         host->transportt = qla4xxx_scsi_transport;
8726
8727         pci_set_drvdata(pdev, ha);
8728
8729         ret = scsi_add_host(host, &pdev->dev);
8730         if (ret)
8731                 goto probe_failed;
8732
8733         if (is_qla80XX(ha))
8734                 qla4_8xxx_get_flash_info(ha);
8735
8736         if (is_qla8032(ha) || is_qla8042(ha)) {
8737                 qla4_83xx_read_reset_template(ha);
8738                 /*
8739                  * NOTE: If ql4dontresethba==1, set IDC_CTRL DONTRESET_BIT0.
8740                  * If DONRESET_BIT0 is set, drivers should not set dev_state
8741                  * to NEED_RESET. But if NEED_RESET is set, drivers should
8742                  * should honor the reset.
8743                  */
8744                 if (ql4xdontresethba == 1)
8745                         qla4_83xx_set_idc_dontreset(ha);
8746         }
8747
8748         /*
8749          * Initialize the Host adapter request/response queues and
8750          * firmware
8751          * NOTE: interrupts enabled upon successful completion
8752          */
8753         status = qla4xxx_initialize_adapter(ha, INIT_ADAPTER);
8754
8755         /* Dont retry adapter initialization if IRQ allocation failed */
8756         if (is_qla80XX(ha) && (status == QLA_ERROR))
8757                 goto skip_retry_init;
8758
8759         while ((!test_bit(AF_ONLINE, &ha->flags)) &&
8760             init_retry_count++ < MAX_INIT_RETRIES) {
8761
8762                 if (is_qla80XX(ha)) {
8763                         ha->isp_ops->idc_lock(ha);
8764                         dev_state = qla4_8xxx_rd_direct(ha,
8765                                                         QLA8XXX_CRB_DEV_STATE);
8766                         ha->isp_ops->idc_unlock(ha);
8767                         if (dev_state == QLA8XXX_DEV_FAILED) {
8768                                 ql4_printk(KERN_WARNING, ha, "%s: don't retry "
8769                                     "initialize adapter. H/W is in failed state\n",
8770                                     __func__);
8771                                 break;
8772                         }
8773                 }
8774                 DEBUG2(printk("scsi: %s: retrying adapter initialization "
8775                               "(%d)\n", __func__, init_retry_count));
8776
8777                 if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
8778                         continue;
8779
8780                 status = qla4xxx_initialize_adapter(ha, INIT_ADAPTER);
8781                 if (is_qla80XX(ha) && (status == QLA_ERROR)) {
8782                         if (qla4_8xxx_check_init_adapter_retry(ha) == QLA_ERROR)
8783                                 goto skip_retry_init;
8784                 }
8785         }
8786
8787 skip_retry_init:
8788         if (!test_bit(AF_ONLINE, &ha->flags)) {
8789                 ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
8790
8791                 if ((is_qla8022(ha) && ql4xdontresethba) ||
8792                     ((is_qla8032(ha) || is_qla8042(ha)) &&
8793                      qla4_83xx_idc_dontreset(ha))) {
8794                         /* Put the device in failed state. */
8795                         DEBUG2(printk(KERN_ERR "HW STATE: FAILED\n"));
8796                         ha->isp_ops->idc_lock(ha);
8797                         qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
8798                                             QLA8XXX_DEV_FAILED);
8799                         ha->isp_ops->idc_unlock(ha);
8800                 }
8801                 ret = -ENODEV;
8802                 goto remove_host;
8803         }
8804
8805         /* Startup the kernel thread for this host adapter. */
8806         DEBUG2(printk("scsi: %s: Starting kernel thread for "
8807                       "qla4xxx_dpc\n", __func__));
8808         sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
8809         ha->dpc_thread = create_singlethread_workqueue(buf);
8810         if (!ha->dpc_thread) {
8811                 ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
8812                 ret = -ENODEV;
8813                 goto remove_host;
8814         }
8815         INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
8816
8817         ha->task_wq = alloc_workqueue("qla4xxx_%lu_task", WQ_MEM_RECLAIM, 1,
8818                                       ha->host_no);
8819         if (!ha->task_wq) {
8820                 ql4_printk(KERN_WARNING, ha, "Unable to start task thread!\n");
8821                 ret = -ENODEV;
8822                 goto remove_host;
8823         }
8824
8825         /*
8826          * For ISP-8XXX, request_irqs is called in qla4_8xxx_load_risc
8827          * (which is called indirectly by qla4xxx_initialize_adapter),
8828          * so that irqs will be registered after crbinit but before
8829          * mbx_intr_enable.
8830          */
8831         if (is_qla40XX(ha)) {
8832                 ret = qla4xxx_request_irqs(ha);
8833                 if (ret) {
8834                         ql4_printk(KERN_WARNING, ha, "Failed to reserve "
8835                             "interrupt %d already in use.\n", pdev->irq);
8836                         goto remove_host;
8837                 }
8838         }
8839
8840         pci_save_state(ha->pdev);
8841         ha->isp_ops->enable_intrs(ha);
8842
8843         /* Start timer thread. */
8844         qla4xxx_start_timer(ha, qla4xxx_timer, 1);
8845
8846         set_bit(AF_INIT_DONE, &ha->flags);
8847
8848         qla4_8xxx_alloc_sysfs_attr(ha);
8849
8850         printk(KERN_INFO
8851                " QLogic iSCSI HBA Driver version: %s\n"
8852                "  QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
8853                qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
8854                ha->host_no, ha->fw_info.fw_major, ha->fw_info.fw_minor,
8855                ha->fw_info.fw_patch, ha->fw_info.fw_build);
8856
8857         /* Set the driver version */
8858         if (is_qla80XX(ha))
8859                 qla4_8xxx_set_param(ha, SET_DRVR_VERSION);
8860
8861         if (qla4xxx_setup_boot_info(ha))
8862                 ql4_printk(KERN_ERR, ha,
8863                            "%s: No iSCSI boot target configured\n", __func__);
8864
8865         set_bit(DPC_SYSFS_DDB_EXPORT, &ha->dpc_flags);
8866         /* Perform the build ddb list and login to each */
8867         qla4xxx_build_ddb_list(ha, INIT_ADAPTER);
8868         iscsi_host_for_each_session(ha->host, qla4xxx_login_flash_ddb);
8869         qla4xxx_wait_login_resp_boot_tgt(ha);
8870
8871         qla4xxx_create_chap_list(ha);
8872
8873         qla4xxx_create_ifaces(ha);
8874         return 0;
8875
8876 remove_host:
8877         scsi_remove_host(ha->host);
8878
8879 probe_failed:
8880         qla4xxx_free_adapter(ha);
8881
8882 probe_failed_ioconfig:
8883         pci_disable_pcie_error_reporting(pdev);
8884         scsi_host_put(ha->host);
8885
8886 probe_disable_device:
8887         pci_disable_device(pdev);
8888
8889         return ret;
8890 }
8891
8892 /**
8893  * qla4xxx_prevent_other_port_reinit - prevent other port from re-initialize
8894  * @ha: pointer to adapter structure
8895  *
8896  * Mark the other ISP-4xxx port to indicate that the driver is being removed,
8897  * so that the other port will not re-initialize while in the process of
8898  * removing the ha due to driver unload or hba hotplug.
8899  **/
8900 static void qla4xxx_prevent_other_port_reinit(struct scsi_qla_host *ha)
8901 {
8902         struct scsi_qla_host *other_ha = NULL;
8903         struct pci_dev *other_pdev = NULL;
8904         int fn = ISP4XXX_PCI_FN_2;
8905
8906         /*iscsi function numbers for ISP4xxx is 1 and 3*/
8907         if (PCI_FUNC(ha->pdev->devfn) & BIT_1)
8908                 fn = ISP4XXX_PCI_FN_1;
8909
8910         other_pdev =
8911                 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
8912                 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
8913                 fn));
8914
8915         /* Get other_ha if other_pdev is valid and state is enable*/
8916         if (other_pdev) {
8917                 if (atomic_read(&other_pdev->enable_cnt)) {
8918                         other_ha = pci_get_drvdata(other_pdev);
8919                         if (other_ha) {
8920                                 set_bit(AF_HA_REMOVAL, &other_ha->flags);
8921                                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: "
8922                                     "Prevent %s reinit\n", __func__,
8923                                     dev_name(&other_ha->pdev->dev)));
8924                         }
8925                 }
8926                 pci_dev_put(other_pdev);
8927         }
8928 }
8929
8930 static void qla4xxx_destroy_ddb(struct scsi_qla_host *ha,
8931                 struct ddb_entry *ddb_entry)
8932 {
8933         struct dev_db_entry *fw_ddb_entry = NULL;
8934         dma_addr_t fw_ddb_entry_dma;
8935         unsigned long wtime;
8936         uint32_t ddb_state;
8937         int options;
8938         int status;
8939
8940         options = LOGOUT_OPTION_CLOSE_SESSION;
8941         if (qla4xxx_session_logout_ddb(ha, ddb_entry, options) == QLA_ERROR) {
8942                 ql4_printk(KERN_ERR, ha, "%s: Logout failed\n", __func__);
8943                 goto clear_ddb;
8944         }
8945
8946         fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
8947                                           &fw_ddb_entry_dma, GFP_KERNEL);
8948         if (!fw_ddb_entry) {
8949                 ql4_printk(KERN_ERR, ha,
8950                            "%s: Unable to allocate dma buffer\n", __func__);
8951                 goto clear_ddb;
8952         }
8953
8954         wtime = jiffies + (HZ * LOGOUT_TOV);
8955         do {
8956                 status = qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index,
8957                                                  fw_ddb_entry, fw_ddb_entry_dma,
8958                                                  NULL, NULL, &ddb_state, NULL,
8959                                                  NULL, NULL);
8960                 if (status == QLA_ERROR)
8961                         goto free_ddb;
8962
8963                 if ((ddb_state == DDB_DS_NO_CONNECTION_ACTIVE) ||
8964                     (ddb_state == DDB_DS_SESSION_FAILED))
8965                         goto free_ddb;
8966
8967                 schedule_timeout_uninterruptible(HZ);
8968         } while ((time_after(wtime, jiffies)));
8969
8970 free_ddb:
8971         dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
8972                           fw_ddb_entry, fw_ddb_entry_dma);
8973 clear_ddb:
8974         qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
8975 }
8976
8977 static void qla4xxx_destroy_fw_ddb_session(struct scsi_qla_host *ha)
8978 {
8979         struct ddb_entry *ddb_entry;
8980         int idx;
8981
8982         for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
8983
8984                 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
8985                 if ((ddb_entry != NULL) &&
8986                     (ddb_entry->ddb_type == FLASH_DDB)) {
8987
8988                         qla4xxx_destroy_ddb(ha, ddb_entry);
8989                         /*
8990                          * we have decremented the reference count of the driver
8991                          * when we setup the session to have the driver unload
8992                          * to be seamless without actually destroying the
8993                          * session
8994                          **/
8995                         try_module_get(qla4xxx_iscsi_transport.owner);
8996                         iscsi_destroy_endpoint(ddb_entry->conn->ep);
8997                         qla4xxx_free_ddb(ha, ddb_entry);
8998                         iscsi_session_teardown(ddb_entry->sess);
8999                 }
9000         }
9001 }
9002 /**
9003  * qla4xxx_remove_adapter - callback function to remove adapter.
9004  * @pci_dev: PCI device pointer
9005  **/
9006 static void qla4xxx_remove_adapter(struct pci_dev *pdev)
9007 {
9008         struct scsi_qla_host *ha;
9009
9010         /*
9011          * If the PCI device is disabled then it means probe_adapter had
9012          * failed and resources already cleaned up on probe_adapter exit.
9013          */
9014         if (!pci_is_enabled(pdev))
9015                 return;
9016
9017         ha = pci_get_drvdata(pdev);
9018
9019         if (is_qla40XX(ha))
9020                 qla4xxx_prevent_other_port_reinit(ha);
9021
9022         /* destroy iface from sysfs */
9023         qla4xxx_destroy_ifaces(ha);
9024
9025         if ((!ql4xdisablesysfsboot) && ha->boot_kset)
9026                 iscsi_boot_destroy_kset(ha->boot_kset);
9027
9028         qla4xxx_destroy_fw_ddb_session(ha);
9029         qla4_8xxx_free_sysfs_attr(ha);
9030
9031         qla4xxx_sysfs_ddb_remove(ha);
9032         scsi_remove_host(ha->host);
9033
9034         qla4xxx_free_adapter(ha);
9035
9036         scsi_host_put(ha->host);
9037
9038         pci_disable_pcie_error_reporting(pdev);
9039         pci_disable_device(pdev);
9040 }
9041
9042 /**
9043  * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
9044  * @ha: HA context
9045  *
9046  * At exit, the @ha's flags.enable_64bit_addressing set to indicated
9047  * supported addressing method.
9048  */
9049 static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
9050 {
9051         int retval;
9052
9053         /* Update our PCI device dma_mask for full 64 bit mask */
9054         if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
9055                 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
9056                         dev_dbg(&ha->pdev->dev,
9057                                   "Failed to set 64 bit PCI consistent mask; "
9058                                    "using 32 bit.\n");
9059                         retval = pci_set_consistent_dma_mask(ha->pdev,
9060                                                              DMA_BIT_MASK(32));
9061                 }
9062         } else
9063                 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
9064 }
9065
9066 static int qla4xxx_slave_alloc(struct scsi_device *sdev)
9067 {
9068         struct iscsi_cls_session *cls_sess;
9069         struct iscsi_session *sess;
9070         struct ddb_entry *ddb;
9071         int queue_depth = QL4_DEF_QDEPTH;
9072
9073         cls_sess = starget_to_session(sdev->sdev_target);
9074         sess = cls_sess->dd_data;
9075         ddb = sess->dd_data;
9076
9077         sdev->hostdata = ddb;
9078
9079         if (ql4xmaxqdepth != 0 && ql4xmaxqdepth <= 0xffffU)
9080                 queue_depth = ql4xmaxqdepth;
9081
9082         scsi_change_queue_depth(sdev, queue_depth);
9083         return 0;
9084 }
9085
9086 /**
9087  * qla4xxx_del_from_active_array - returns an active srb
9088  * @ha: Pointer to host adapter structure.
9089  * @index: index into the active_array
9090  *
9091  * This routine removes and returns the srb at the specified index
9092  **/
9093 struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
9094     uint32_t index)
9095 {
9096         struct srb *srb = NULL;
9097         struct scsi_cmnd *cmd = NULL;
9098
9099         cmd = scsi_host_find_tag(ha->host, index);
9100         if (!cmd)
9101                 return srb;
9102
9103         srb = (struct srb *)CMD_SP(cmd);
9104         if (!srb)
9105                 return srb;
9106
9107         /* update counters */
9108         if (srb->flags & SRB_DMA_VALID) {
9109                 ha->iocb_cnt -= srb->iocb_cnt;
9110                 if (srb->cmd)
9111                         srb->cmd->host_scribble =
9112                                 (unsigned char *)(unsigned long) MAX_SRBS;
9113         }
9114         return srb;
9115 }
9116
9117 /**
9118  * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
9119  * @ha: Pointer to host adapter structure.
9120  * @cmd: Scsi Command to wait on.
9121  *
9122  * This routine waits for the command to be returned by the Firmware
9123  * for some max time.
9124  **/
9125 static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
9126                                       struct scsi_cmnd *cmd)
9127 {
9128         int done = 0;
9129         struct srb *rp;
9130         uint32_t max_wait_time = EH_WAIT_CMD_TOV;
9131         int ret = SUCCESS;
9132
9133         /* Dont wait on command if PCI error is being handled
9134          * by PCI AER driver
9135          */
9136         if (unlikely(pci_channel_offline(ha->pdev)) ||
9137             (test_bit(AF_EEH_BUSY, &ha->flags))) {
9138                 ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
9139                     ha->host_no, __func__);
9140                 return ret;
9141         }
9142
9143         do {
9144                 /* Checking to see if its returned to OS */
9145                 rp = (struct srb *) CMD_SP(cmd);
9146                 if (rp == NULL) {
9147                         done++;
9148                         break;
9149                 }
9150
9151                 msleep(2000);
9152         } while (max_wait_time--);
9153
9154         return done;
9155 }
9156
9157 /**
9158  * qla4xxx_wait_for_hba_online - waits for HBA to come online
9159  * @ha: Pointer to host adapter structure
9160  **/
9161 static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
9162 {
9163         unsigned long wait_online;
9164
9165         wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
9166         while (time_before(jiffies, wait_online)) {
9167
9168                 if (adapter_up(ha))
9169                         return QLA_SUCCESS;
9170
9171                 msleep(2000);
9172         }
9173
9174         return QLA_ERROR;
9175 }
9176
9177 /**
9178  * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
9179  * @ha: pointer to HBA
9180  * @t: target id
9181  * @l: lun id
9182  *
9183  * This function waits for all outstanding commands to a lun to complete. It
9184  * returns 0 if all pending commands are returned and 1 otherwise.
9185  **/
9186 static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
9187                                         struct scsi_target *stgt,
9188                                         struct scsi_device *sdev)
9189 {
9190         int cnt;
9191         int status = 0;
9192         struct scsi_cmnd *cmd;
9193
9194         /*
9195          * Waiting for all commands for the designated target or dev
9196          * in the active array
9197          */
9198         for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
9199                 cmd = scsi_host_find_tag(ha->host, cnt);
9200                 if (cmd && stgt == scsi_target(cmd->device) &&
9201                     (!sdev || sdev == cmd->device)) {
9202                         if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
9203                                 status++;
9204                                 break;
9205                         }
9206                 }
9207         }
9208         return status;
9209 }
9210
9211 /**
9212  * qla4xxx_eh_abort - callback for abort task.
9213  * @cmd: Pointer to Linux's SCSI command structure
9214  *
9215  * This routine is called by the Linux OS to abort the specified
9216  * command.
9217  **/
9218 static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
9219 {
9220         struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
9221         unsigned int id = cmd->device->id;
9222         uint64_t lun = cmd->device->lun;
9223         unsigned long flags;
9224         struct srb *srb = NULL;
9225         int ret = SUCCESS;
9226         int wait = 0;
9227         int rval;
9228
9229         ql4_printk(KERN_INFO, ha, "scsi%ld:%d:%llu: Abort command issued cmd=%p, cdb=0x%x\n",
9230                    ha->host_no, id, lun, cmd, cmd->cmnd[0]);
9231
9232         rval = qla4xxx_isp_check_reg(ha);
9233         if (rval != QLA_SUCCESS) {
9234                 ql4_printk(KERN_INFO, ha, "PCI/Register disconnect, exiting.\n");
9235                 return FAILED;
9236         }
9237
9238         spin_lock_irqsave(&ha->hardware_lock, flags);
9239         srb = (struct srb *) CMD_SP(cmd);
9240         if (!srb) {
9241                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
9242                 ql4_printk(KERN_INFO, ha, "scsi%ld:%d:%llu: Specified command has already completed.\n",
9243                            ha->host_no, id, lun);
9244                 return SUCCESS;
9245         }
9246         kref_get(&srb->srb_ref);
9247         spin_unlock_irqrestore(&ha->hardware_lock, flags);
9248
9249         if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
9250                 DEBUG3(printk("scsi%ld:%d:%llu: Abort_task mbx failed.\n",
9251                     ha->host_no, id, lun));
9252                 ret = FAILED;
9253         } else {
9254                 DEBUG3(printk("scsi%ld:%d:%llu: Abort_task mbx success.\n",
9255                     ha->host_no, id, lun));
9256                 wait = 1;
9257         }
9258
9259         kref_put(&srb->srb_ref, qla4xxx_srb_compl);
9260
9261         /* Wait for command to complete */
9262         if (wait) {
9263                 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
9264                         DEBUG2(printk("scsi%ld:%d:%llu: Abort handler timed out\n",
9265                             ha->host_no, id, lun));
9266                         ret = FAILED;
9267                 }
9268         }
9269
9270         ql4_printk(KERN_INFO, ha,
9271             "scsi%ld:%d:%llu: Abort command - %s\n",
9272             ha->host_no, id, lun, (ret == SUCCESS) ? "succeeded" : "failed");
9273
9274         return ret;
9275 }
9276
9277 /**
9278  * qla4xxx_eh_device_reset - callback for target reset.
9279  * @cmd: Pointer to Linux's SCSI command structure
9280  *
9281  * This routine is called by the Linux OS to reset all luns on the
9282  * specified target.
9283  **/
9284 static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
9285 {
9286         struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
9287         struct ddb_entry *ddb_entry = cmd->device->hostdata;
9288         int ret = FAILED, stat;
9289         int rval;
9290
9291         if (!ddb_entry)
9292                 return ret;
9293
9294         ret = iscsi_block_scsi_eh(cmd);
9295         if (ret)
9296                 return ret;
9297         ret = FAILED;
9298
9299         ql4_printk(KERN_INFO, ha,
9300                    "scsi%ld:%d:%d:%llu: DEVICE RESET ISSUED.\n", ha->host_no,
9301                    cmd->device->channel, cmd->device->id, cmd->device->lun);
9302
9303         DEBUG2(printk(KERN_INFO
9304                       "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
9305                       "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
9306                       cmd, jiffies, cmd->request->timeout / HZ,
9307                       ha->dpc_flags, cmd->result, cmd->allowed));
9308
9309         rval = qla4xxx_isp_check_reg(ha);
9310         if (rval != QLA_SUCCESS) {
9311                 ql4_printk(KERN_INFO, ha, "PCI/Register disconnect, exiting.\n");
9312                 return FAILED;
9313         }
9314
9315         /* FIXME: wait for hba to go online */
9316         stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
9317         if (stat != QLA_SUCCESS) {
9318                 ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
9319                 goto eh_dev_reset_done;
9320         }
9321
9322         if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
9323                                          cmd->device)) {
9324                 ql4_printk(KERN_INFO, ha,
9325                            "DEVICE RESET FAILED - waiting for "
9326                            "commands.\n");
9327                 goto eh_dev_reset_done;
9328         }
9329
9330         /* Send marker. */
9331         if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
9332                 MM_LUN_RESET) != QLA_SUCCESS)
9333                 goto eh_dev_reset_done;
9334
9335         ql4_printk(KERN_INFO, ha,
9336                    "scsi(%ld:%d:%d:%llu): DEVICE RESET SUCCEEDED.\n",
9337                    ha->host_no, cmd->device->channel, cmd->device->id,
9338                    cmd->device->lun);
9339
9340         ret = SUCCESS;
9341
9342 eh_dev_reset_done:
9343
9344         return ret;
9345 }
9346
9347 /**
9348  * qla4xxx_eh_target_reset - callback for target reset.
9349  * @cmd: Pointer to Linux's SCSI command structure
9350  *
9351  * This routine is called by the Linux OS to reset the target.
9352  **/
9353 static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
9354 {
9355         struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
9356         struct ddb_entry *ddb_entry = cmd->device->hostdata;
9357         int stat, ret;
9358         int rval;
9359
9360         if (!ddb_entry)
9361                 return FAILED;
9362
9363         ret = iscsi_block_scsi_eh(cmd);
9364         if (ret)
9365                 return ret;
9366
9367         starget_printk(KERN_INFO, scsi_target(cmd->device),
9368                        "WARM TARGET RESET ISSUED.\n");
9369
9370         DEBUG2(printk(KERN_INFO
9371                       "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
9372                       "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
9373                       ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
9374                       ha->dpc_flags, cmd->result, cmd->allowed));
9375
9376         rval = qla4xxx_isp_check_reg(ha);
9377         if (rval != QLA_SUCCESS) {
9378                 ql4_printk(KERN_INFO, ha, "PCI/Register disconnect, exiting.\n");
9379                 return FAILED;
9380         }
9381
9382         stat = qla4xxx_reset_target(ha, ddb_entry);
9383         if (stat != QLA_SUCCESS) {
9384                 starget_printk(KERN_INFO, scsi_target(cmd->device),
9385                                "WARM TARGET RESET FAILED.\n");
9386                 return FAILED;
9387         }
9388
9389         if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
9390                                          NULL)) {
9391                 starget_printk(KERN_INFO, scsi_target(cmd->device),
9392                                "WARM TARGET DEVICE RESET FAILED - "
9393                                "waiting for commands.\n");
9394                 return FAILED;
9395         }
9396
9397         /* Send marker. */
9398         if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
9399                 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
9400                 starget_printk(KERN_INFO, scsi_target(cmd->device),
9401                                "WARM TARGET DEVICE RESET FAILED - "
9402                                "marker iocb failed.\n");
9403                 return FAILED;
9404         }
9405
9406         starget_printk(KERN_INFO, scsi_target(cmd->device),
9407                        "WARM TARGET RESET SUCCEEDED.\n");
9408         return SUCCESS;
9409 }
9410
9411 /**
9412  * qla4xxx_is_eh_active - check if error handler is running
9413  * @shost: Pointer to SCSI Host struct
9414  *
9415  * This routine finds that if reset host is called in EH
9416  * scenario or from some application like sg_reset
9417  **/
9418 static int qla4xxx_is_eh_active(struct Scsi_Host *shost)
9419 {
9420         if (shost->shost_state == SHOST_RECOVERY)
9421                 return 1;
9422         return 0;
9423 }
9424
9425 /**
9426  * qla4xxx_eh_host_reset - kernel callback
9427  * @cmd: Pointer to Linux's SCSI command structure
9428  *
9429  * This routine is invoked by the Linux kernel to perform fatal error
9430  * recovery on the specified adapter.
9431  **/
9432 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
9433 {
9434         int return_status = FAILED;
9435         struct scsi_qla_host *ha;
9436         int rval;
9437
9438         ha = to_qla_host(cmd->device->host);
9439
9440         rval = qla4xxx_isp_check_reg(ha);
9441         if (rval != QLA_SUCCESS) {
9442                 ql4_printk(KERN_INFO, ha, "PCI/Register disconnect, exiting.\n");
9443                 return FAILED;
9444         }
9445
9446         if ((is_qla8032(ha) || is_qla8042(ha)) && ql4xdontresethba)
9447                 qla4_83xx_set_idc_dontreset(ha);
9448
9449         /*
9450          * For ISP8324 and ISP8042, if IDC_CTRL DONTRESET_BIT0 is set by other
9451          * protocol drivers, we should not set device_state to NEED_RESET
9452          */
9453         if (ql4xdontresethba ||
9454             ((is_qla8032(ha) || is_qla8042(ha)) &&
9455              qla4_83xx_idc_dontreset(ha))) {
9456                 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
9457                      ha->host_no, __func__));
9458
9459                 /* Clear outstanding srb in queues */
9460                 if (qla4xxx_is_eh_active(cmd->device->host))
9461                         qla4xxx_abort_active_cmds(ha, DID_ABORT << 16);
9462
9463                 return FAILED;
9464         }
9465
9466         ql4_printk(KERN_INFO, ha,
9467                    "scsi(%ld:%d:%d:%llu): HOST RESET ISSUED.\n", ha->host_no,
9468                    cmd->device->channel, cmd->device->id, cmd->device->lun);
9469
9470         if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
9471                 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host.  Adapter "
9472                               "DEAD.\n", ha->host_no, cmd->device->channel,
9473                               __func__));
9474
9475                 return FAILED;
9476         }
9477
9478         if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
9479                 if (is_qla80XX(ha))
9480                         set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
9481                 else
9482                         set_bit(DPC_RESET_HA, &ha->dpc_flags);
9483         }
9484
9485         if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
9486                 return_status = SUCCESS;
9487
9488         ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
9489                    return_status == FAILED ? "FAILED" : "SUCCEEDED");
9490
9491         return return_status;
9492 }
9493
9494 static int qla4xxx_context_reset(struct scsi_qla_host *ha)
9495 {
9496         uint32_t mbox_cmd[MBOX_REG_COUNT];
9497         uint32_t mbox_sts[MBOX_REG_COUNT];
9498         struct addr_ctrl_blk_def *acb = NULL;
9499         uint32_t acb_len = sizeof(struct addr_ctrl_blk_def);
9500         int rval = QLA_SUCCESS;
9501         dma_addr_t acb_dma;
9502
9503         acb = dma_alloc_coherent(&ha->pdev->dev,
9504                                  sizeof(struct addr_ctrl_blk_def),
9505                                  &acb_dma, GFP_KERNEL);
9506         if (!acb) {
9507                 ql4_printk(KERN_ERR, ha, "%s: Unable to alloc acb\n",
9508                            __func__);
9509                 rval = -ENOMEM;
9510                 goto exit_port_reset;
9511         }
9512
9513         memset(acb, 0, acb_len);
9514
9515         rval = qla4xxx_get_acb(ha, acb_dma, PRIMARI_ACB, acb_len);
9516         if (rval != QLA_SUCCESS) {
9517                 rval = -EIO;
9518                 goto exit_free_acb;
9519         }
9520
9521         rval = qla4xxx_disable_acb(ha);
9522         if (rval != QLA_SUCCESS) {
9523                 rval = -EIO;
9524                 goto exit_free_acb;
9525         }
9526
9527         wait_for_completion_timeout(&ha->disable_acb_comp,
9528                                     DISABLE_ACB_TOV * HZ);
9529
9530         rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], acb_dma);
9531         if (rval != QLA_SUCCESS) {
9532                 rval = -EIO;
9533                 goto exit_free_acb;
9534         }
9535
9536 exit_free_acb:
9537         dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk_def),
9538                           acb, acb_dma);
9539 exit_port_reset:
9540         DEBUG2(ql4_printk(KERN_INFO, ha, "%s %s\n", __func__,
9541                           rval == QLA_SUCCESS ? "SUCCEEDED" : "FAILED"));
9542         return rval;
9543 }
9544
9545 static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type)
9546 {
9547         struct scsi_qla_host *ha = to_qla_host(shost);
9548         int rval = QLA_SUCCESS;
9549         uint32_t idc_ctrl;
9550
9551         if (ql4xdontresethba) {
9552                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Don't Reset HBA\n",
9553                                   __func__));
9554                 rval = -EPERM;
9555                 goto exit_host_reset;
9556         }
9557
9558         if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
9559                 goto recover_adapter;
9560
9561         switch (reset_type) {
9562         case SCSI_ADAPTER_RESET:
9563                 set_bit(DPC_RESET_HA, &ha->dpc_flags);
9564                 break;
9565         case SCSI_FIRMWARE_RESET:
9566                 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
9567                         if (is_qla80XX(ha))
9568                                 /* set firmware context reset */
9569                                 set_bit(DPC_RESET_HA_FW_CONTEXT,
9570                                         &ha->dpc_flags);
9571                         else {
9572                                 rval = qla4xxx_context_reset(ha);
9573                                 goto exit_host_reset;
9574                         }
9575                 }
9576                 break;
9577         }
9578
9579 recover_adapter:
9580         /* For ISP8324 and ISP8042 set graceful reset bit in IDC_DRV_CTRL if
9581          * reset is issued by application */
9582         if ((is_qla8032(ha) || is_qla8042(ha)) &&
9583             test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
9584                 idc_ctrl = qla4_83xx_rd_reg(ha, QLA83XX_IDC_DRV_CTRL);
9585                 qla4_83xx_wr_reg(ha, QLA83XX_IDC_DRV_CTRL,
9586                                  (idc_ctrl | GRACEFUL_RESET_BIT1));
9587         }
9588
9589         rval = qla4xxx_recover_adapter(ha);
9590         if (rval != QLA_SUCCESS) {
9591                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: recover adapter fail\n",
9592                                   __func__));
9593                 rval = -EIO;
9594         }
9595
9596 exit_host_reset:
9597         return rval;
9598 }
9599
9600 /* PCI AER driver recovers from all correctable errors w/o
9601  * driver intervention. For uncorrectable errors PCI AER
9602  * driver calls the following device driver's callbacks
9603  *
9604  * - Fatal Errors - link_reset
9605  * - Non-Fatal Errors - driver's error_detected() which
9606  * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
9607  *
9608  * PCI AER driver calls
9609  * CAN_RECOVER - driver's mmio_enabled(), mmio_enabled()
9610  *               returns RECOVERED or NEED_RESET if fw_hung
9611  * NEED_RESET - driver's slot_reset()
9612  * DISCONNECT - device is dead & cannot recover
9613  * RECOVERED - driver's resume()
9614  */
9615 static pci_ers_result_t
9616 qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
9617 {
9618         struct scsi_qla_host *ha = pci_get_drvdata(pdev);
9619
9620         ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
9621             ha->host_no, __func__, state);
9622
9623         if (!is_aer_supported(ha))
9624                 return PCI_ERS_RESULT_NONE;
9625
9626         switch (state) {
9627         case pci_channel_io_normal:
9628                 clear_bit(AF_EEH_BUSY, &ha->flags);
9629                 return PCI_ERS_RESULT_CAN_RECOVER;
9630         case pci_channel_io_frozen:
9631                 set_bit(AF_EEH_BUSY, &ha->flags);
9632                 qla4xxx_mailbox_premature_completion(ha);
9633                 qla4xxx_free_irqs(ha);
9634                 pci_disable_device(pdev);
9635                 /* Return back all IOs */
9636                 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
9637                 return PCI_ERS_RESULT_NEED_RESET;
9638         case pci_channel_io_perm_failure:
9639                 set_bit(AF_EEH_BUSY, &ha->flags);
9640                 set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
9641                 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
9642                 return PCI_ERS_RESULT_DISCONNECT;
9643         }
9644         return PCI_ERS_RESULT_NEED_RESET;
9645 }
9646
9647 /**
9648  * qla4xxx_pci_mmio_enabled() gets called if
9649  * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
9650  * and read/write to the device still works.
9651  **/
9652 static pci_ers_result_t
9653 qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
9654 {
9655         struct scsi_qla_host *ha = pci_get_drvdata(pdev);
9656
9657         if (!is_aer_supported(ha))
9658                 return PCI_ERS_RESULT_NONE;
9659
9660         return PCI_ERS_RESULT_RECOVERED;
9661 }
9662
9663 static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
9664 {
9665         uint32_t rval = QLA_ERROR;
9666         int fn;
9667         struct pci_dev *other_pdev = NULL;
9668
9669         ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
9670
9671         set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
9672
9673         if (test_bit(AF_ONLINE, &ha->flags)) {
9674                 clear_bit(AF_ONLINE, &ha->flags);
9675                 clear_bit(AF_LINK_UP, &ha->flags);
9676                 iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
9677                 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
9678         }
9679
9680         fn = PCI_FUNC(ha->pdev->devfn);
9681         if (is_qla8022(ha)) {
9682                 while (fn > 0) {
9683                         fn--;
9684                         ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at func %x\n",
9685                                    ha->host_no, __func__, fn);
9686                         /* Get the pci device given the domain, bus,
9687                          * slot/function number */
9688                         other_pdev = pci_get_domain_bus_and_slot(
9689                                            pci_domain_nr(ha->pdev->bus),
9690                                            ha->pdev->bus->number,
9691                                            PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
9692                                            fn));
9693
9694                         if (!other_pdev)
9695                                 continue;
9696
9697                         if (atomic_read(&other_pdev->enable_cnt)) {
9698                                 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI func in enabled state%x\n",
9699                                            ha->host_no, __func__, fn);
9700                                 pci_dev_put(other_pdev);
9701                                 break;
9702                         }
9703                         pci_dev_put(other_pdev);
9704                 }
9705         } else {
9706                 /* this case is meant for ISP83xx/ISP84xx only */
9707                 if (qla4_83xx_can_perform_reset(ha)) {
9708                         /* reset fn as iSCSI is going to perform the reset */
9709                         fn = 0;
9710                 }
9711         }
9712
9713         /* The first function on the card, the reset owner will
9714          * start & initialize the firmware. The other functions
9715          * on the card will reset the firmware context
9716          */
9717         if (!fn) {
9718                 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
9719                     "0x%x is the owner\n", ha->host_no, __func__,
9720                     ha->pdev->devfn);
9721
9722                 ha->isp_ops->idc_lock(ha);
9723                 qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
9724                                     QLA8XXX_DEV_COLD);
9725                 ha->isp_ops->idc_unlock(ha);
9726
9727                 rval = qla4_8xxx_update_idc_reg(ha);
9728                 if (rval == QLA_ERROR) {
9729                         ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: FAILED\n",
9730                                    ha->host_no, __func__);
9731                         ha->isp_ops->idc_lock(ha);
9732                         qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
9733                                             QLA8XXX_DEV_FAILED);
9734                         ha->isp_ops->idc_unlock(ha);
9735                         goto exit_error_recovery;
9736                 }
9737
9738                 clear_bit(AF_FW_RECOVERY, &ha->flags);
9739                 rval = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
9740
9741                 if (rval != QLA_SUCCESS) {
9742                         ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
9743                             "FAILED\n", ha->host_no, __func__);
9744                         qla4xxx_free_irqs(ha);
9745                         ha->isp_ops->idc_lock(ha);
9746                         qla4_8xxx_clear_drv_active(ha);
9747                         qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
9748                                             QLA8XXX_DEV_FAILED);
9749                         ha->isp_ops->idc_unlock(ha);
9750                 } else {
9751                         ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
9752                             "READY\n", ha->host_no, __func__);
9753                         ha->isp_ops->idc_lock(ha);
9754                         qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
9755                                             QLA8XXX_DEV_READY);
9756                         /* Clear driver state register */
9757                         qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DRV_STATE, 0);
9758                         qla4_8xxx_set_drv_active(ha);
9759                         ha->isp_ops->idc_unlock(ha);
9760                         ha->isp_ops->enable_intrs(ha);
9761                 }
9762         } else {
9763                 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
9764                     "the reset owner\n", ha->host_no, __func__,
9765                     ha->pdev->devfn);
9766                 if ((qla4_8xxx_rd_direct(ha, QLA8XXX_CRB_DEV_STATE) ==
9767                      QLA8XXX_DEV_READY)) {
9768                         clear_bit(AF_FW_RECOVERY, &ha->flags);
9769                         rval = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
9770                         if (rval == QLA_SUCCESS)
9771                                 ha->isp_ops->enable_intrs(ha);
9772                         else
9773                                 qla4xxx_free_irqs(ha);
9774
9775                         ha->isp_ops->idc_lock(ha);
9776                         qla4_8xxx_set_drv_active(ha);
9777                         ha->isp_ops->idc_unlock(ha);
9778                 }
9779         }
9780 exit_error_recovery:
9781         clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
9782         return rval;
9783 }
9784
9785 static pci_ers_result_t
9786 qla4xxx_pci_slot_reset(struct pci_dev *pdev)
9787 {
9788         pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
9789         struct scsi_qla_host *ha = pci_get_drvdata(pdev);
9790         int rc;
9791
9792         ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
9793             ha->host_no, __func__);
9794
9795         if (!is_aer_supported(ha))
9796                 return PCI_ERS_RESULT_NONE;
9797
9798         /* Restore the saved state of PCIe device -
9799          * BAR registers, PCI Config space, PCIX, MSI,
9800          * IOV states
9801          */
9802         pci_restore_state(pdev);
9803
9804         /* pci_restore_state() clears the saved_state flag of the device
9805          * save restored state which resets saved_state flag
9806          */
9807         pci_save_state(pdev);
9808
9809         /* Initialize device or resume if in suspended state */
9810         rc = pci_enable_device(pdev);
9811         if (rc) {
9812                 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Can't re-enable "
9813                     "device after reset\n", ha->host_no, __func__);
9814                 goto exit_slot_reset;
9815         }
9816
9817         ha->isp_ops->disable_intrs(ha);
9818
9819         if (is_qla80XX(ha)) {
9820                 if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
9821                         ret = PCI_ERS_RESULT_RECOVERED;
9822                         goto exit_slot_reset;
9823                 } else
9824                         goto exit_slot_reset;
9825         }
9826
9827 exit_slot_reset:
9828         ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
9829             "device after reset\n", ha->host_no, __func__, ret);
9830         return ret;
9831 }
9832
9833 static void
9834 qla4xxx_pci_resume(struct pci_dev *pdev)
9835 {
9836         struct scsi_qla_host *ha = pci_get_drvdata(pdev);
9837         int ret;
9838
9839         ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
9840             ha->host_no, __func__);
9841
9842         ret = qla4xxx_wait_for_hba_online(ha);
9843         if (ret != QLA_SUCCESS) {
9844                 ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
9845                     "resume I/O from slot/link_reset\n", ha->host_no,
9846                      __func__);
9847         }
9848
9849         pci_cleanup_aer_uncorrect_error_status(pdev);
9850         clear_bit(AF_EEH_BUSY, &ha->flags);
9851 }
9852
9853 static const struct pci_error_handlers qla4xxx_err_handler = {
9854         .error_detected = qla4xxx_pci_error_detected,
9855         .mmio_enabled = qla4xxx_pci_mmio_enabled,
9856         .slot_reset = qla4xxx_pci_slot_reset,
9857         .resume = qla4xxx_pci_resume,
9858 };
9859
9860 static struct pci_device_id qla4xxx_pci_tbl[] = {
9861         {
9862                 .vendor         = PCI_VENDOR_ID_QLOGIC,
9863                 .device         = PCI_DEVICE_ID_QLOGIC_ISP4010,
9864                 .subvendor      = PCI_ANY_ID,
9865                 .subdevice      = PCI_ANY_ID,
9866         },
9867         {
9868                 .vendor         = PCI_VENDOR_ID_QLOGIC,
9869                 .device         = PCI_DEVICE_ID_QLOGIC_ISP4022,
9870                 .subvendor      = PCI_ANY_ID,
9871                 .subdevice      = PCI_ANY_ID,
9872         },
9873         {
9874                 .vendor         = PCI_VENDOR_ID_QLOGIC,
9875                 .device         = PCI_DEVICE_ID_QLOGIC_ISP4032,
9876                 .subvendor      = PCI_ANY_ID,
9877                 .subdevice      = PCI_ANY_ID,
9878         },
9879         {
9880                 .vendor         = PCI_VENDOR_ID_QLOGIC,
9881                 .device         = PCI_DEVICE_ID_QLOGIC_ISP8022,
9882                 .subvendor      = PCI_ANY_ID,
9883                 .subdevice      = PCI_ANY_ID,
9884         },
9885         {
9886                 .vendor         = PCI_VENDOR_ID_QLOGIC,
9887                 .device         = PCI_DEVICE_ID_QLOGIC_ISP8324,
9888                 .subvendor      = PCI_ANY_ID,
9889                 .subdevice      = PCI_ANY_ID,
9890         },
9891         {
9892                 .vendor         = PCI_VENDOR_ID_QLOGIC,
9893                 .device         = PCI_DEVICE_ID_QLOGIC_ISP8042,
9894                 .subvendor      = PCI_ANY_ID,
9895                 .subdevice      = PCI_ANY_ID,
9896         },
9897         {0, 0},
9898 };
9899 MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
9900
9901 static struct pci_driver qla4xxx_pci_driver = {
9902         .name           = DRIVER_NAME,
9903         .id_table       = qla4xxx_pci_tbl,
9904         .probe          = qla4xxx_probe_adapter,
9905         .remove         = qla4xxx_remove_adapter,
9906         .err_handler = &qla4xxx_err_handler,
9907 };
9908
9909 static int __init qla4xxx_module_init(void)
9910 {
9911         int ret;
9912
9913         if (ql4xqfulltracking)
9914                 qla4xxx_driver_template.track_queue_depth = 1;
9915
9916         /* Allocate cache for SRBs. */
9917         srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
9918                                        SLAB_HWCACHE_ALIGN, NULL);
9919         if (srb_cachep == NULL) {
9920                 printk(KERN_ERR
9921                        "%s: Unable to allocate SRB cache..."
9922                        "Failing load!\n", DRIVER_NAME);
9923                 ret = -ENOMEM;
9924                 goto no_srp_cache;
9925         }
9926
9927         /* Derive version string. */
9928         strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
9929         if (ql4xextended_error_logging)
9930                 strcat(qla4xxx_version_str, "-debug");
9931
9932         qla4xxx_scsi_transport =
9933                 iscsi_register_transport(&qla4xxx_iscsi_transport);
9934         if (!qla4xxx_scsi_transport){
9935                 ret = -ENODEV;
9936                 goto release_srb_cache;
9937         }
9938
9939         ret = pci_register_driver(&qla4xxx_pci_driver);
9940         if (ret)
9941                 goto unregister_transport;
9942
9943         printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
9944         return 0;
9945
9946 unregister_transport:
9947         iscsi_unregister_transport(&qla4xxx_iscsi_transport);
9948 release_srb_cache:
9949         kmem_cache_destroy(srb_cachep);
9950 no_srp_cache:
9951         return ret;
9952 }
9953
9954 static void __exit qla4xxx_module_exit(void)
9955 {
9956         pci_unregister_driver(&qla4xxx_pci_driver);
9957         iscsi_unregister_transport(&qla4xxx_iscsi_transport);
9958         kmem_cache_destroy(srb_cachep);
9959 }
9960
9961 module_init(qla4xxx_module_init);
9962 module_exit(qla4xxx_module_exit);
9963
9964 MODULE_AUTHOR("QLogic Corporation");
9965 MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
9966 MODULE_LICENSE("GPL");
9967 MODULE_VERSION(QLA4XXX_DRIVER_VERSION);