Version:  2.0.40 2.2.26 2.4.37 2.6.39 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15

Linux/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 = 0x1F;
 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 0x1F.\n"
 91                  "\t\t  Can be set to 0x3, 0x7, 0xF, 0x1F, 0x3F, 0x7F");
 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 int qla4xxx_slave_configure(struct scsi_device *device);
166 static void qla4xxx_slave_destroy(struct scsi_device *sdev);
167 static umode_t qla4_attr_is_visible(int param_type, int param);
168 static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type);
169 static int qla4xxx_change_queue_depth(struct scsi_device *sdev, int qdepth,
170                                       int reason);
171 
172 /*
173  * iSCSI Flash DDB sysfs entry points
174  */
175 static int
176 qla4xxx_sysfs_ddb_set_param(struct iscsi_bus_flash_session *fnode_sess,
177                             struct iscsi_bus_flash_conn *fnode_conn,
178                             void *data, int len);
179 static int
180 qla4xxx_sysfs_ddb_get_param(struct iscsi_bus_flash_session *fnode_sess,
181                             int param, char *buf);
182 static int qla4xxx_sysfs_ddb_add(struct Scsi_Host *shost, const char *buf,
183                                  int len);
184 static int
185 qla4xxx_sysfs_ddb_delete(struct iscsi_bus_flash_session *fnode_sess);
186 static int qla4xxx_sysfs_ddb_login(struct iscsi_bus_flash_session *fnode_sess,
187                                    struct iscsi_bus_flash_conn *fnode_conn);
188 static int qla4xxx_sysfs_ddb_logout(struct iscsi_bus_flash_session *fnode_sess,
189                                     struct iscsi_bus_flash_conn *fnode_conn);
190 static int qla4xxx_sysfs_ddb_logout_sid(struct iscsi_cls_session *cls_sess);
191 
192 static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
193     QLA82XX_LEGACY_INTR_CONFIG;
194 
195 static struct scsi_host_template qla4xxx_driver_template = {
196         .module                 = THIS_MODULE,
197         .name                   = DRIVER_NAME,
198         .proc_name              = DRIVER_NAME,
199         .queuecommand           = qla4xxx_queuecommand,
200 
201         .eh_abort_handler       = qla4xxx_eh_abort,
202         .eh_device_reset_handler = qla4xxx_eh_device_reset,
203         .eh_target_reset_handler = qla4xxx_eh_target_reset,
204         .eh_host_reset_handler  = qla4xxx_eh_host_reset,
205         .eh_timed_out           = qla4xxx_eh_cmd_timed_out,
206 
207         .slave_configure        = qla4xxx_slave_configure,
208         .slave_alloc            = qla4xxx_slave_alloc,
209         .slave_destroy          = qla4xxx_slave_destroy,
210         .change_queue_depth     = qla4xxx_change_queue_depth,
211 
212         .this_id                = -1,
213         .cmd_per_lun            = 3,
214         .use_clustering         = ENABLE_CLUSTERING,
215         .sg_tablesize           = SG_ALL,
216 
217         .max_sectors            = 0xFFFF,
218         .shost_attrs            = qla4xxx_host_attrs,
219         .host_reset             = qla4xxx_host_reset,
220         .vendor_id              = SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_QLOGIC,
221 };
222 
223 static struct iscsi_transport qla4xxx_iscsi_transport = {
224         .owner                  = THIS_MODULE,
225         .name                   = DRIVER_NAME,
226         .caps                   = CAP_TEXT_NEGO |
227                                   CAP_DATA_PATH_OFFLOAD | CAP_HDRDGST |
228                                   CAP_DATADGST | CAP_LOGIN_OFFLOAD |
229                                   CAP_MULTI_R2T,
230         .attr_is_visible        = qla4_attr_is_visible,
231         .create_session         = qla4xxx_session_create,
232         .destroy_session        = qla4xxx_session_destroy,
233         .start_conn             = qla4xxx_conn_start,
234         .create_conn            = qla4xxx_conn_create,
235         .bind_conn              = qla4xxx_conn_bind,
236         .stop_conn              = iscsi_conn_stop,
237         .destroy_conn           = qla4xxx_conn_destroy,
238         .set_param              = iscsi_set_param,
239         .get_conn_param         = qla4xxx_conn_get_param,
240         .get_session_param      = qla4xxx_session_get_param,
241         .get_ep_param           = qla4xxx_get_ep_param,
242         .ep_connect             = qla4xxx_ep_connect,
243         .ep_poll                = qla4xxx_ep_poll,
244         .ep_disconnect          = qla4xxx_ep_disconnect,
245         .get_stats              = qla4xxx_conn_get_stats,
246         .send_pdu               = iscsi_conn_send_pdu,
247         .xmit_task              = qla4xxx_task_xmit,
248         .cleanup_task           = qla4xxx_task_cleanup,
249         .alloc_pdu              = qla4xxx_alloc_pdu,
250 
251         .get_host_param         = qla4xxx_host_get_param,
252         .set_iface_param        = qla4xxx_iface_set_param,
253         .get_iface_param        = qla4xxx_get_iface_param,
254         .bsg_request            = qla4xxx_bsg_request,
255         .send_ping              = qla4xxx_send_ping,
256         .get_chap               = qla4xxx_get_chap_list,
257         .delete_chap            = qla4xxx_delete_chap,
258         .set_chap               = qla4xxx_set_chap_entry,
259         .get_flashnode_param    = qla4xxx_sysfs_ddb_get_param,
260         .set_flashnode_param    = qla4xxx_sysfs_ddb_set_param,
261         .new_flashnode          = qla4xxx_sysfs_ddb_add,
262         .del_flashnode          = qla4xxx_sysfs_ddb_delete,
263         .login_flashnode        = qla4xxx_sysfs_ddb_login,
264         .logout_flashnode       = qla4xxx_sysfs_ddb_logout,
265         .logout_flashnode_sid   = qla4xxx_sysfs_ddb_logout_sid,
266         .get_host_stats         = qla4xxx_get_host_stats,
267 };
268 
269 static struct scsi_transport_template *qla4xxx_scsi_transport;
270 
271 static int qla4xxx_send_ping(struct Scsi_Host *shost, uint32_t iface_num,
272                              uint32_t iface_type, uint32_t payload_size,
273                              uint32_t pid, struct sockaddr *dst_addr)
274 {
275         struct scsi_qla_host *ha = to_qla_host(shost);
276         struct sockaddr_in *addr;
277         struct sockaddr_in6 *addr6;
278         uint32_t options = 0;
279         uint8_t ipaddr[IPv6_ADDR_LEN];
280         int rval;
281 
282         memset(ipaddr, 0, IPv6_ADDR_LEN);
283         /* IPv4 to IPv4 */
284         if ((iface_type == ISCSI_IFACE_TYPE_IPV4) &&
285             (dst_addr->sa_family == AF_INET)) {
286                 addr = (struct sockaddr_in *)dst_addr;
287                 memcpy(ipaddr, &addr->sin_addr.s_addr, IP_ADDR_LEN);
288                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: IPv4 Ping src: %pI4 "
289                                   "dest: %pI4\n", __func__,
290                                   &ha->ip_config.ip_address, ipaddr));
291                 rval = qla4xxx_ping_iocb(ha, options, payload_size, pid,
292                                          ipaddr);
293                 if (rval)
294                         rval = -EINVAL;
295         } else if ((iface_type == ISCSI_IFACE_TYPE_IPV6) &&
296                    (dst_addr->sa_family == AF_INET6)) {
297                 /* IPv6 to IPv6 */
298                 addr6 = (struct sockaddr_in6 *)dst_addr;
299                 memcpy(ipaddr, &addr6->sin6_addr.in6_u.u6_addr8, IPv6_ADDR_LEN);
300 
301                 options |= PING_IPV6_PROTOCOL_ENABLE;
302 
303                 /* Ping using LinkLocal address */
304                 if ((iface_num == 0) || (iface_num == 1)) {
305                         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: LinkLocal Ping "
306                                           "src: %pI6 dest: %pI6\n", __func__,
307                                           &ha->ip_config.ipv6_link_local_addr,
308                                           ipaddr));
309                         options |= PING_IPV6_LINKLOCAL_ADDR;
310                         rval = qla4xxx_ping_iocb(ha, options, payload_size,
311                                                  pid, ipaddr);
312                 } else {
313                         ql4_printk(KERN_WARNING, ha, "%s: iface num = %d "
314                                    "not supported\n", __func__, iface_num);
315                         rval = -ENOSYS;
316                         goto exit_send_ping;
317                 }
318 
319                 /*
320                  * If ping using LinkLocal address fails, try ping using
321                  * IPv6 address
322                  */
323                 if (rval != QLA_SUCCESS) {
324                         options &= ~PING_IPV6_LINKLOCAL_ADDR;
325                         if (iface_num == 0) {
326                                 options |= PING_IPV6_ADDR0;
327                                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: IPv6 "
328                                                   "Ping src: %pI6 "
329                                                   "dest: %pI6\n", __func__,
330                                                   &ha->ip_config.ipv6_addr0,
331                                                   ipaddr));
332                         } else if (iface_num == 1) {
333                                 options |= PING_IPV6_ADDR1;
334                                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: IPv6 "
335                                                   "Ping src: %pI6 "
336                                                   "dest: %pI6\n", __func__,
337                                                   &ha->ip_config.ipv6_addr1,
338                                                   ipaddr));
339                         }
340                         rval = qla4xxx_ping_iocb(ha, options, payload_size,
341                                                  pid, ipaddr);
342                         if (rval)
343                                 rval = -EINVAL;
344                 }
345         } else
346                 rval = -ENOSYS;
347 exit_send_ping:
348         return rval;
349 }
350 
351 static umode_t qla4_attr_is_visible(int param_type, int param)
352 {
353         switch (param_type) {
354         case ISCSI_HOST_PARAM:
355                 switch (param) {
356                 case ISCSI_HOST_PARAM_HWADDRESS:
357                 case ISCSI_HOST_PARAM_IPADDRESS:
358                 case ISCSI_HOST_PARAM_INITIATOR_NAME:
359                 case ISCSI_HOST_PARAM_PORT_STATE:
360                 case ISCSI_HOST_PARAM_PORT_SPEED:
361                         return S_IRUGO;
362                 default:
363                         return 0;
364                 }
365         case ISCSI_PARAM:
366                 switch (param) {
367                 case ISCSI_PARAM_PERSISTENT_ADDRESS:
368                 case ISCSI_PARAM_PERSISTENT_PORT:
369                 case ISCSI_PARAM_CONN_ADDRESS:
370                 case ISCSI_PARAM_CONN_PORT:
371                 case ISCSI_PARAM_TARGET_NAME:
372                 case ISCSI_PARAM_TPGT:
373                 case ISCSI_PARAM_TARGET_ALIAS:
374                 case ISCSI_PARAM_MAX_BURST:
375                 case ISCSI_PARAM_MAX_R2T:
376                 case ISCSI_PARAM_FIRST_BURST:
377                 case ISCSI_PARAM_MAX_RECV_DLENGTH:
378                 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
379                 case ISCSI_PARAM_IFACE_NAME:
380                 case ISCSI_PARAM_CHAP_OUT_IDX:
381                 case ISCSI_PARAM_CHAP_IN_IDX:
382                 case ISCSI_PARAM_USERNAME:
383                 case ISCSI_PARAM_PASSWORD:
384                 case ISCSI_PARAM_USERNAME_IN:
385                 case ISCSI_PARAM_PASSWORD_IN:
386                 case ISCSI_PARAM_AUTO_SND_TGT_DISABLE:
387                 case ISCSI_PARAM_DISCOVERY_SESS:
388                 case ISCSI_PARAM_PORTAL_TYPE:
389                 case ISCSI_PARAM_CHAP_AUTH_EN:
390                 case ISCSI_PARAM_DISCOVERY_LOGOUT_EN:
391                 case ISCSI_PARAM_BIDI_CHAP_EN:
392                 case ISCSI_PARAM_DISCOVERY_AUTH_OPTIONAL:
393                 case ISCSI_PARAM_DEF_TIME2WAIT:
394                 case ISCSI_PARAM_DEF_TIME2RETAIN:
395                 case ISCSI_PARAM_HDRDGST_EN:
396                 case ISCSI_PARAM_DATADGST_EN:
397                 case ISCSI_PARAM_INITIAL_R2T_EN:
398                 case ISCSI_PARAM_IMM_DATA_EN:
399                 case ISCSI_PARAM_PDU_INORDER_EN:
400                 case ISCSI_PARAM_DATASEQ_INORDER_EN:
401                 case ISCSI_PARAM_MAX_SEGMENT_SIZE:
402                 case ISCSI_PARAM_TCP_TIMESTAMP_STAT:
403                 case ISCSI_PARAM_TCP_WSF_DISABLE:
404                 case ISCSI_PARAM_TCP_NAGLE_DISABLE:
405                 case ISCSI_PARAM_TCP_TIMER_SCALE:
406                 case ISCSI_PARAM_TCP_TIMESTAMP_EN:
407                 case ISCSI_PARAM_TCP_XMIT_WSF:
408                 case ISCSI_PARAM_TCP_RECV_WSF:
409                 case ISCSI_PARAM_IP_FRAGMENT_DISABLE:
410                 case ISCSI_PARAM_IPV4_TOS:
411                 case ISCSI_PARAM_IPV6_TC:
412                 case ISCSI_PARAM_IPV6_FLOW_LABEL:
413                 case ISCSI_PARAM_IS_FW_ASSIGNED_IPV6:
414                 case ISCSI_PARAM_KEEPALIVE_TMO:
415                 case ISCSI_PARAM_LOCAL_PORT:
416                 case ISCSI_PARAM_ISID:
417                 case ISCSI_PARAM_TSID:
418                 case ISCSI_PARAM_DEF_TASKMGMT_TMO:
419                 case ISCSI_PARAM_ERL:
420                 case ISCSI_PARAM_STATSN:
421                 case ISCSI_PARAM_EXP_STATSN:
422                 case ISCSI_PARAM_DISCOVERY_PARENT_IDX:
423                 case ISCSI_PARAM_DISCOVERY_PARENT_TYPE:
424                 case ISCSI_PARAM_LOCAL_IPADDR:
425                         return S_IRUGO;
426                 default:
427                         return 0;
428                 }
429         case ISCSI_NET_PARAM:
430                 switch (param) {
431                 case ISCSI_NET_PARAM_IPV4_ADDR:
432                 case ISCSI_NET_PARAM_IPV4_SUBNET:
433                 case ISCSI_NET_PARAM_IPV4_GW:
434                 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
435                 case ISCSI_NET_PARAM_IFACE_ENABLE:
436                 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
437                 case ISCSI_NET_PARAM_IPV6_ADDR:
438                 case ISCSI_NET_PARAM_IPV6_ROUTER:
439                 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
440                 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
441                 case ISCSI_NET_PARAM_VLAN_ID:
442                 case ISCSI_NET_PARAM_VLAN_PRIORITY:
443                 case ISCSI_NET_PARAM_VLAN_ENABLED:
444                 case ISCSI_NET_PARAM_MTU:
445                 case ISCSI_NET_PARAM_PORT:
446                 case ISCSI_NET_PARAM_IPADDR_STATE:
447                 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_STATE:
448                 case ISCSI_NET_PARAM_IPV6_ROUTER_STATE:
449                 case ISCSI_NET_PARAM_DELAYED_ACK_EN:
450                 case ISCSI_NET_PARAM_TCP_NAGLE_DISABLE:
451                 case ISCSI_NET_PARAM_TCP_WSF_DISABLE:
452                 case ISCSI_NET_PARAM_TCP_WSF:
453                 case ISCSI_NET_PARAM_TCP_TIMER_SCALE:
454                 case ISCSI_NET_PARAM_TCP_TIMESTAMP_EN:
455                 case ISCSI_NET_PARAM_CACHE_ID:
456                 case ISCSI_NET_PARAM_IPV4_DHCP_DNS_ADDR_EN:
457                 case ISCSI_NET_PARAM_IPV4_DHCP_SLP_DA_EN:
458                 case ISCSI_NET_PARAM_IPV4_TOS_EN:
459                 case ISCSI_NET_PARAM_IPV4_TOS:
460                 case ISCSI_NET_PARAM_IPV4_GRAT_ARP_EN:
461                 case ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID_EN:
462                 case ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID:
463                 case ISCSI_NET_PARAM_IPV4_DHCP_REQ_VENDOR_ID_EN:
464                 case ISCSI_NET_PARAM_IPV4_DHCP_USE_VENDOR_ID_EN:
465                 case ISCSI_NET_PARAM_IPV4_DHCP_VENDOR_ID:
466                 case ISCSI_NET_PARAM_IPV4_DHCP_LEARN_IQN_EN:
467                 case ISCSI_NET_PARAM_IPV4_FRAGMENT_DISABLE:
468                 case ISCSI_NET_PARAM_IPV4_IN_FORWARD_EN:
469                 case ISCSI_NET_PARAM_REDIRECT_EN:
470                 case ISCSI_NET_PARAM_IPV4_TTL:
471                 case ISCSI_NET_PARAM_IPV6_GRAT_NEIGHBOR_ADV_EN:
472                 case ISCSI_NET_PARAM_IPV6_MLD_EN:
473                 case ISCSI_NET_PARAM_IPV6_FLOW_LABEL:
474                 case ISCSI_NET_PARAM_IPV6_TRAFFIC_CLASS:
475                 case ISCSI_NET_PARAM_IPV6_HOP_LIMIT:
476                 case ISCSI_NET_PARAM_IPV6_ND_REACHABLE_TMO:
477                 case ISCSI_NET_PARAM_IPV6_ND_REXMIT_TIME:
478                 case ISCSI_NET_PARAM_IPV6_ND_STALE_TMO:
479                 case ISCSI_NET_PARAM_IPV6_DUP_ADDR_DETECT_CNT:
480                 case ISCSI_NET_PARAM_IPV6_RTR_ADV_LINK_MTU:
481                         return S_IRUGO;
482                 default:
483                         return 0;
484                 }
485         case ISCSI_IFACE_PARAM:
486                 switch (param) {
487                 case ISCSI_IFACE_PARAM_DEF_TASKMGMT_TMO:
488                 case ISCSI_IFACE_PARAM_HDRDGST_EN:
489                 case ISCSI_IFACE_PARAM_DATADGST_EN:
490                 case ISCSI_IFACE_PARAM_IMM_DATA_EN:
491                 case ISCSI_IFACE_PARAM_INITIAL_R2T_EN:
492                 case ISCSI_IFACE_PARAM_DATASEQ_INORDER_EN:
493                 case ISCSI_IFACE_PARAM_PDU_INORDER_EN:
494                 case ISCSI_IFACE_PARAM_ERL:
495                 case ISCSI_IFACE_PARAM_MAX_RECV_DLENGTH:
496                 case ISCSI_IFACE_PARAM_FIRST_BURST:
497                 case ISCSI_IFACE_PARAM_MAX_R2T:
498                 case ISCSI_IFACE_PARAM_MAX_BURST:
499                 case ISCSI_IFACE_PARAM_CHAP_AUTH_EN:
500                 case ISCSI_IFACE_PARAM_BIDI_CHAP_EN:
501                 case ISCSI_IFACE_PARAM_DISCOVERY_AUTH_OPTIONAL:
502                 case ISCSI_IFACE_PARAM_DISCOVERY_LOGOUT_EN:
503                 case ISCSI_IFACE_PARAM_STRICT_LOGIN_COMP_EN:
504                 case ISCSI_IFACE_PARAM_INITIATOR_NAME:
505                         return S_IRUGO;
506                 default:
507                         return 0;
508                 }
509         case ISCSI_FLASHNODE_PARAM:
510                 switch (param) {
511                 case ISCSI_FLASHNODE_IS_FW_ASSIGNED_IPV6:
512                 case ISCSI_FLASHNODE_PORTAL_TYPE:
513                 case ISCSI_FLASHNODE_AUTO_SND_TGT_DISABLE:
514                 case ISCSI_FLASHNODE_DISCOVERY_SESS:
515                 case ISCSI_FLASHNODE_ENTRY_EN:
516                 case ISCSI_FLASHNODE_HDR_DGST_EN:
517                 case ISCSI_FLASHNODE_DATA_DGST_EN:
518                 case ISCSI_FLASHNODE_IMM_DATA_EN:
519                 case ISCSI_FLASHNODE_INITIAL_R2T_EN:
520                 case ISCSI_FLASHNODE_DATASEQ_INORDER:
521                 case ISCSI_FLASHNODE_PDU_INORDER:
522                 case ISCSI_FLASHNODE_CHAP_AUTH_EN:
523                 case ISCSI_FLASHNODE_SNACK_REQ_EN:
524                 case ISCSI_FLASHNODE_DISCOVERY_LOGOUT_EN:
525                 case ISCSI_FLASHNODE_BIDI_CHAP_EN:
526                 case ISCSI_FLASHNODE_DISCOVERY_AUTH_OPTIONAL:
527                 case ISCSI_FLASHNODE_ERL:
528                 case ISCSI_FLASHNODE_TCP_TIMESTAMP_STAT:
529                 case ISCSI_FLASHNODE_TCP_NAGLE_DISABLE:
530                 case ISCSI_FLASHNODE_TCP_WSF_DISABLE:
531                 case ISCSI_FLASHNODE_TCP_TIMER_SCALE:
532                 case ISCSI_FLASHNODE_TCP_TIMESTAMP_EN:
533                 case ISCSI_FLASHNODE_IP_FRAG_DISABLE:
534                 case ISCSI_FLASHNODE_MAX_RECV_DLENGTH:
535                 case ISCSI_FLASHNODE_MAX_XMIT_DLENGTH:
536                 case ISCSI_FLASHNODE_FIRST_BURST:
537                 case ISCSI_FLASHNODE_DEF_TIME2WAIT:
538                 case ISCSI_FLASHNODE_DEF_TIME2RETAIN:
539                 case ISCSI_FLASHNODE_MAX_R2T:
540                 case ISCSI_FLASHNODE_KEEPALIVE_TMO:
541                 case ISCSI_FLASHNODE_ISID:
542                 case ISCSI_FLASHNODE_TSID:
543                 case ISCSI_FLASHNODE_PORT:
544                 case ISCSI_FLASHNODE_MAX_BURST:
545                 case ISCSI_FLASHNODE_DEF_TASKMGMT_TMO:
546                 case ISCSI_FLASHNODE_IPADDR:
547                 case ISCSI_FLASHNODE_ALIAS:
548                 case ISCSI_FLASHNODE_REDIRECT_IPADDR:
549                 case ISCSI_FLASHNODE_MAX_SEGMENT_SIZE:
550                 case ISCSI_FLASHNODE_LOCAL_PORT:
551                 case ISCSI_FLASHNODE_IPV4_TOS:
552                 case ISCSI_FLASHNODE_IPV6_TC:
553                 case ISCSI_FLASHNODE_IPV6_FLOW_LABEL:
554                 case ISCSI_FLASHNODE_NAME:
555                 case ISCSI_FLASHNODE_TPGT:
556                 case ISCSI_FLASHNODE_LINK_LOCAL_IPV6:
557                 case ISCSI_FLASHNODE_DISCOVERY_PARENT_IDX:
558                 case ISCSI_FLASHNODE_DISCOVERY_PARENT_TYPE:
559                 case ISCSI_FLASHNODE_TCP_XMIT_WSF:
560                 case ISCSI_FLASHNODE_TCP_RECV_WSF:
561                 case ISCSI_FLASHNODE_CHAP_OUT_IDX:
562                 case ISCSI_FLASHNODE_USERNAME:
563                 case ISCSI_FLASHNODE_PASSWORD:
564                 case ISCSI_FLASHNODE_STATSN:
565                 case ISCSI_FLASHNODE_EXP_STATSN:
566                 case ISCSI_FLASHNODE_IS_BOOT_TGT:
567                         return S_IRUGO;
568                 default:
569                         return 0;
570                 }
571         }
572 
573         return 0;
574 }
575 
576 /**
577  * qla4xxx_create chap_list - Create CHAP list from FLASH
578  * @ha: pointer to adapter structure
579  *
580  * Read flash and make a list of CHAP entries, during login when a CHAP entry
581  * is received, it will be checked in this list. If entry exist then the CHAP
582  * entry index is set in the DDB. If CHAP entry does not exist in this list
583  * then a new entry is added in FLASH in CHAP table and the index obtained is
584  * used in the DDB.
585  **/
586 static void qla4xxx_create_chap_list(struct scsi_qla_host *ha)
587 {
588         int rval = 0;
589         uint8_t *chap_flash_data = NULL;
590         uint32_t offset;
591         dma_addr_t chap_dma;
592         uint32_t chap_size = 0;
593 
594         if (is_qla40XX(ha))
595                 chap_size = MAX_CHAP_ENTRIES_40XX *
596                             sizeof(struct ql4_chap_table);
597         else    /* Single region contains CHAP info for both
598                  * ports which is divided into half for each port.
599                  */
600                 chap_size = ha->hw.flt_chap_size / 2;
601 
602         chap_flash_data = dma_alloc_coherent(&ha->pdev->dev, chap_size,
603                                              &chap_dma, GFP_KERNEL);
604         if (!chap_flash_data) {
605                 ql4_printk(KERN_ERR, ha, "No memory for chap_flash_data\n");
606                 return;
607         }
608 
609         if (is_qla40XX(ha)) {
610                 offset = FLASH_CHAP_OFFSET;
611         } else {
612                 offset = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_region_chap << 2);
613                 if (ha->port_num == 1)
614                         offset += chap_size;
615         }
616 
617         rval = qla4xxx_get_flash(ha, chap_dma, offset, chap_size);
618         if (rval != QLA_SUCCESS)
619                 goto exit_chap_list;
620 
621         if (ha->chap_list == NULL)
622                 ha->chap_list = vmalloc(chap_size);
623         if (ha->chap_list == NULL) {
624                 ql4_printk(KERN_ERR, ha, "No memory for ha->chap_list\n");
625                 goto exit_chap_list;
626         }
627 
628         memset(ha->chap_list, 0, chap_size);
629         memcpy(ha->chap_list, chap_flash_data, chap_size);
630 
631 exit_chap_list:
632         dma_free_coherent(&ha->pdev->dev, chap_size, chap_flash_data, chap_dma);
633 }
634 
635 static int qla4xxx_get_chap_by_index(struct scsi_qla_host *ha,
636                                      int16_t chap_index,
637                                      struct ql4_chap_table **chap_entry)
638 {
639         int rval = QLA_ERROR;
640         int max_chap_entries;
641 
642         if (!ha->chap_list) {
643                 ql4_printk(KERN_ERR, ha, "CHAP table cache is empty!\n");
644                 rval = QLA_ERROR;
645                 goto exit_get_chap;
646         }
647 
648         if (is_qla80XX(ha))
649                 max_chap_entries = (ha->hw.flt_chap_size / 2) /
650                                    sizeof(struct ql4_chap_table);
651         else
652                 max_chap_entries = MAX_CHAP_ENTRIES_40XX;
653 
654         if (chap_index > max_chap_entries) {
655                 ql4_printk(KERN_ERR, ha, "Invalid Chap index\n");
656                 rval = QLA_ERROR;
657                 goto exit_get_chap;
658         }
659 
660         *chap_entry = (struct ql4_chap_table *)ha->chap_list + chap_index;
661         if ((*chap_entry)->cookie !=
662              __constant_cpu_to_le16(CHAP_VALID_COOKIE)) {
663                 rval = QLA_ERROR;
664                 *chap_entry = NULL;
665         } else {
666                 rval = QLA_SUCCESS;
667         }
668 
669 exit_get_chap:
670         return rval;
671 }
672 
673 /**
674  * qla4xxx_find_free_chap_index - Find the first free chap index
675  * @ha: pointer to adapter structure
676  * @chap_index: CHAP index to be returned
677  *
678  * Find the first free chap index available in the chap table
679  *
680  * Note: Caller should acquire the chap lock before getting here.
681  **/
682 static int qla4xxx_find_free_chap_index(struct scsi_qla_host *ha,
683                                         uint16_t *chap_index)
684 {
685         int i, rval;
686         int free_index = -1;
687         int max_chap_entries = 0;
688         struct ql4_chap_table *chap_table;
689 
690         if (is_qla80XX(ha))
691                 max_chap_entries = (ha->hw.flt_chap_size / 2) /
692                                                 sizeof(struct ql4_chap_table);
693         else
694                 max_chap_entries = MAX_CHAP_ENTRIES_40XX;
695 
696         if (!ha->chap_list) {
697                 ql4_printk(KERN_ERR, ha, "CHAP table cache is empty!\n");
698                 rval = QLA_ERROR;
699                 goto exit_find_chap;
700         }
701 
702         for (i = 0; i < max_chap_entries; i++) {
703                 chap_table = (struct ql4_chap_table *)ha->chap_list + i;
704 
705                 if ((chap_table->cookie !=
706                     __constant_cpu_to_le16(CHAP_VALID_COOKIE)) &&
707                    (i > MAX_RESRV_CHAP_IDX)) {
708                                 free_index = i;
709                                 break;
710                 }
711         }
712 
713         if (free_index != -1) {
714                 *chap_index = free_index;
715                 rval = QLA_SUCCESS;
716         } else {
717                 rval = QLA_ERROR;
718         }
719 
720 exit_find_chap:
721         return rval;
722 }
723 
724 static int qla4xxx_get_chap_list(struct Scsi_Host *shost, uint16_t chap_tbl_idx,
725                                   uint32_t *num_entries, char *buf)
726 {
727         struct scsi_qla_host *ha = to_qla_host(shost);
728         struct ql4_chap_table *chap_table;
729         struct iscsi_chap_rec *chap_rec;
730         int max_chap_entries = 0;
731         int valid_chap_entries = 0;
732         int ret = 0, i;
733 
734         if (is_qla80XX(ha))
735                 max_chap_entries = (ha->hw.flt_chap_size / 2) /
736                                         sizeof(struct ql4_chap_table);
737         else
738                 max_chap_entries = MAX_CHAP_ENTRIES_40XX;
739 
740         ql4_printk(KERN_INFO, ha, "%s: num_entries = %d, CHAP idx = %d\n",
741                         __func__, *num_entries, chap_tbl_idx);
742 
743         if (!buf) {
744                 ret = -ENOMEM;
745                 goto exit_get_chap_list;
746         }
747 
748         qla4xxx_create_chap_list(ha);
749 
750         chap_rec = (struct iscsi_chap_rec *) buf;
751         mutex_lock(&ha->chap_sem);
752         for (i = chap_tbl_idx; i < max_chap_entries; i++) {
753                 chap_table = (struct ql4_chap_table *)ha->chap_list + i;
754                 if (chap_table->cookie !=
755                     __constant_cpu_to_le16(CHAP_VALID_COOKIE))
756                         continue;
757 
758                 chap_rec->chap_tbl_idx = i;
759                 strncpy(chap_rec->username, chap_table->name,
760                         ISCSI_CHAP_AUTH_NAME_MAX_LEN);
761                 strncpy(chap_rec->password, chap_table->secret,
762                         QL4_CHAP_MAX_SECRET_LEN);
763                 chap_rec->password_length = chap_table->secret_len;
764 
765                 if (chap_table->flags & BIT_7) /* local */
766                         chap_rec->chap_type = CHAP_TYPE_OUT;
767 
768                 if (chap_table->flags & BIT_6) /* peer */
769                         chap_rec->chap_type = CHAP_TYPE_IN;
770 
771                 chap_rec++;
772 
773                 valid_chap_entries++;
774                 if (valid_chap_entries == *num_entries)
775                         break;
776                 else
777                         continue;
778         }
779         mutex_unlock(&ha->chap_sem);
780 
781 exit_get_chap_list:
782         ql4_printk(KERN_INFO, ha, "%s: Valid CHAP Entries = %d\n",
783                         __func__,  valid_chap_entries);
784         *num_entries = valid_chap_entries;
785         return ret;
786 }
787 
788 static int __qla4xxx_is_chap_active(struct device *dev, void *data)
789 {
790         int ret = 0;
791         uint16_t *chap_tbl_idx = (uint16_t *) data;
792         struct iscsi_cls_session *cls_session;
793         struct iscsi_session *sess;
794         struct ddb_entry *ddb_entry;
795 
796         if (!iscsi_is_session_dev(dev))
797                 goto exit_is_chap_active;
798 
799         cls_session = iscsi_dev_to_session(dev);
800         sess = cls_session->dd_data;
801         ddb_entry = sess->dd_data;
802 
803         if (iscsi_session_chkready(cls_session))
804                 goto exit_is_chap_active;
805 
806         if (ddb_entry->chap_tbl_idx == *chap_tbl_idx)
807                 ret = 1;
808 
809 exit_is_chap_active:
810         return ret;
811 }
812 
813 static int qla4xxx_is_chap_active(struct Scsi_Host *shost,
814                                   uint16_t chap_tbl_idx)
815 {
816         int ret = 0;
817 
818         ret = device_for_each_child(&shost->shost_gendev, &chap_tbl_idx,
819                                     __qla4xxx_is_chap_active);
820 
821         return ret;
822 }
823 
824 static int qla4xxx_delete_chap(struct Scsi_Host *shost, uint16_t chap_tbl_idx)
825 {
826         struct scsi_qla_host *ha = to_qla_host(shost);
827         struct ql4_chap_table *chap_table;
828         dma_addr_t chap_dma;
829         int max_chap_entries = 0;
830         uint32_t offset = 0;
831         uint32_t chap_size;
832         int ret = 0;
833 
834         chap_table = dma_pool_alloc(ha->chap_dma_pool, GFP_KERNEL, &chap_dma);
835         if (chap_table == NULL)
836                 return -ENOMEM;
837 
838         memset(chap_table, 0, sizeof(struct ql4_chap_table));
839 
840         if (is_qla80XX(ha))
841                 max_chap_entries = (ha->hw.flt_chap_size / 2) /
842                                    sizeof(struct ql4_chap_table);
843         else
844                 max_chap_entries = MAX_CHAP_ENTRIES_40XX;
845 
846         if (chap_tbl_idx > max_chap_entries) {
847                 ret = -EINVAL;
848                 goto exit_delete_chap;
849         }
850 
851         /* Check if chap index is in use.
852          * If chap is in use don't delet chap entry */
853         ret = qla4xxx_is_chap_active(shost, chap_tbl_idx);
854         if (ret) {
855                 ql4_printk(KERN_INFO, ha, "CHAP entry %d is in use, cannot "
856                            "delete from flash\n", chap_tbl_idx);
857                 ret = -EBUSY;
858                 goto exit_delete_chap;
859         }
860 
861         chap_size = sizeof(struct ql4_chap_table);
862         if (is_qla40XX(ha))
863                 offset = FLASH_CHAP_OFFSET | (chap_tbl_idx * chap_size);
864         else {
865                 offset = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_region_chap << 2);
866                 /* flt_chap_size is CHAP table size for both ports
867                  * so divide it by 2 to calculate the offset for second port
868                  */
869                 if (ha->port_num == 1)
870                         offset += (ha->hw.flt_chap_size / 2);
871                 offset += (chap_tbl_idx * chap_size);
872         }
873 
874         ret = qla4xxx_get_flash(ha, chap_dma, offset, chap_size);
875         if (ret != QLA_SUCCESS) {
876                 ret = -EINVAL;
877                 goto exit_delete_chap;
878         }
879 
880         DEBUG2(ql4_printk(KERN_INFO, ha, "Chap Cookie: x%x\n",
881                           __le16_to_cpu(chap_table->cookie)));
882 
883         if (__le16_to_cpu(chap_table->cookie) != CHAP_VALID_COOKIE) {
884                 ql4_printk(KERN_ERR, ha, "No valid chap entry found\n");
885                 goto exit_delete_chap;
886         }
887 
888         chap_table->cookie = __constant_cpu_to_le16(0xFFFF);
889 
890         offset = FLASH_CHAP_OFFSET |
891                         (chap_tbl_idx * sizeof(struct ql4_chap_table));
892         ret = qla4xxx_set_flash(ha, chap_dma, offset, chap_size,
893                                 FLASH_OPT_RMW_COMMIT);
894         if (ret == QLA_SUCCESS && ha->chap_list) {
895                 mutex_lock(&ha->chap_sem);
896                 /* Update ha chap_list cache */
897                 memcpy((struct ql4_chap_table *)ha->chap_list + chap_tbl_idx,
898                         chap_table, sizeof(struct ql4_chap_table));
899                 mutex_unlock(&ha->chap_sem);
900         }
901         if (ret != QLA_SUCCESS)
902                 ret =  -EINVAL;
903 
904 exit_delete_chap:
905         dma_pool_free(ha->chap_dma_pool, chap_table, chap_dma);
906         return ret;
907 }
908 
909 /**
910  * qla4xxx_set_chap_entry - Make chap entry with given information
911  * @shost: pointer to host
912  * @data: chap info - credentials, index and type to make chap entry
913  * @len: length of data
914  *
915  * Add or update chap entry with the given information
916  **/
917 static int qla4xxx_set_chap_entry(struct Scsi_Host *shost, void *data, int len)
918 {
919         struct scsi_qla_host *ha = to_qla_host(shost);
920         struct iscsi_chap_rec chap_rec;
921         struct ql4_chap_table *chap_entry = NULL;
922         struct iscsi_param_info *param_info;
923         struct nlattr *attr;
924         int max_chap_entries = 0;
925         int type;
926         int rem = len;
927         int rc = 0;
928         int size;
929 
930         memset(&chap_rec, 0, sizeof(chap_rec));
931 
932         nla_for_each_attr(attr, data, len, rem) {
933                 param_info = nla_data(attr);
934 
935                 switch (param_info->param) {
936                 case ISCSI_CHAP_PARAM_INDEX:
937                         chap_rec.chap_tbl_idx = *(uint16_t *)param_info->value;
938                         break;
939                 case ISCSI_CHAP_PARAM_CHAP_TYPE:
940                         chap_rec.chap_type = param_info->value[0];
941                         break;
942                 case ISCSI_CHAP_PARAM_USERNAME:
943                         size = min_t(size_t, sizeof(chap_rec.username),
944                                      param_info->len);
945                         memcpy(chap_rec.username, param_info->value, size);
946                         break;
947                 case ISCSI_CHAP_PARAM_PASSWORD:
948                         size = min_t(size_t, sizeof(chap_rec.password),
949                                      param_info->len);
950                         memcpy(chap_rec.password, param_info->value, size);
951                         break;
952                 case ISCSI_CHAP_PARAM_PASSWORD_LEN:
953                         chap_rec.password_length = param_info->value[0];
954                         break;
955                 default:
956                         ql4_printk(KERN_ERR, ha,
957                                    "%s: No such sysfs attribute\n", __func__);
958                         rc = -ENOSYS;
959                         goto exit_set_chap;
960                 };
961         }
962 
963         if (chap_rec.chap_type == CHAP_TYPE_IN)
964                 type = BIDI_CHAP;
965         else
966                 type = LOCAL_CHAP;
967 
968         if (is_qla80XX(ha))
969                 max_chap_entries = (ha->hw.flt_chap_size / 2) /
970                                    sizeof(struct ql4_chap_table);
971         else
972                 max_chap_entries = MAX_CHAP_ENTRIES_40XX;
973 
974         mutex_lock(&ha->chap_sem);
975         if (chap_rec.chap_tbl_idx < max_chap_entries) {
976                 rc = qla4xxx_get_chap_by_index(ha, chap_rec.chap_tbl_idx,
977                                                &chap_entry);
978                 if (!rc) {
979                         if (!(type == qla4xxx_get_chap_type(chap_entry))) {
980                                 ql4_printk(KERN_INFO, ha,
981                                            "Type mismatch for CHAP entry %d\n",
982                                            chap_rec.chap_tbl_idx);
983                                 rc = -EINVAL;
984                                 goto exit_unlock_chap;
985                         }
986 
987                         /* If chap index is in use then don't modify it */
988                         rc = qla4xxx_is_chap_active(shost,
989                                                     chap_rec.chap_tbl_idx);
990                         if (rc) {
991                                 ql4_printk(KERN_INFO, ha,
992                                            "CHAP entry %d is in use\n",
993                                            chap_rec.chap_tbl_idx);
994                                 rc = -EBUSY;
995                                 goto exit_unlock_chap;
996                         }
997                 }
998         } else {
999                 rc = qla4xxx_find_free_chap_index(ha, &chap_rec.chap_tbl_idx);
1000                 if (rc) {
1001                         ql4_printk(KERN_INFO, ha, "CHAP entry not available\n");
1002                         rc = -EBUSY;
1003                         goto exit_unlock_chap;
1004                 }
1005         }
1006 
1007         rc = qla4xxx_set_chap(ha, chap_rec.username, chap_rec.password,
1008                               chap_rec.chap_tbl_idx, type);
1009 
1010 exit_unlock_chap:
1011         mutex_unlock(&ha->chap_sem);
1012 
1013 exit_set_chap:
1014         return rc;
1015 }
1016 
1017 
1018 static int qla4xxx_get_host_stats(struct Scsi_Host *shost, char *buf, int len)
1019 {
1020         struct scsi_qla_host *ha = to_qla_host(shost);
1021         struct iscsi_offload_host_stats *host_stats = NULL;
1022         int host_stats_size;
1023         int ret = 0;
1024         int ddb_idx = 0;
1025         struct ql_iscsi_stats *ql_iscsi_stats = NULL;
1026         int stats_size;
1027         dma_addr_t iscsi_stats_dma;
1028 
1029         DEBUG2(ql4_printk(KERN_INFO, ha, "Func: %s\n", __func__));
1030 
1031         host_stats_size = sizeof(struct iscsi_offload_host_stats);
1032 
1033         if (host_stats_size != len) {
1034                 ql4_printk(KERN_INFO, ha, "%s: host_stats size mismatch expected = %d, is = %d\n",
1035                            __func__, len, host_stats_size);
1036                 ret = -EINVAL;
1037                 goto exit_host_stats;
1038         }
1039         host_stats = (struct iscsi_offload_host_stats *)buf;
1040 
1041         if (!buf) {
1042                 ret = -ENOMEM;
1043                 goto exit_host_stats;
1044         }
1045 
1046         stats_size = PAGE_ALIGN(sizeof(struct ql_iscsi_stats));
1047 
1048         ql_iscsi_stats = dma_alloc_coherent(&ha->pdev->dev, stats_size,
1049                                             &iscsi_stats_dma, GFP_KERNEL);
1050         if (!ql_iscsi_stats) {
1051                 ql4_printk(KERN_ERR, ha,
1052                            "Unable to allocate memory for iscsi stats\n");
1053                 goto exit_host_stats;
1054         }
1055 
1056         ret =  qla4xxx_get_mgmt_data(ha, ddb_idx, stats_size,
1057                                      iscsi_stats_dma);
1058         if (ret != QLA_SUCCESS) {
1059                 ql4_printk(KERN_ERR, ha,
1060                            "Unable to retrieve iscsi stats\n");
1061                 goto exit_host_stats;
1062         }
1063         host_stats->mactx_frames = le64_to_cpu(ql_iscsi_stats->mac_tx_frames);
1064         host_stats->mactx_bytes = le64_to_cpu(ql_iscsi_stats->mac_tx_bytes);
1065         host_stats->mactx_multicast_frames =
1066                         le64_to_cpu(ql_iscsi_stats->mac_tx_multicast_frames);
1067         host_stats->mactx_broadcast_frames =
1068                         le64_to_cpu(ql_iscsi_stats->mac_tx_broadcast_frames);
1069         host_stats->mactx_pause_frames =
1070                         le64_to_cpu(ql_iscsi_stats->mac_tx_pause_frames);
1071         host_stats->mactx_control_frames =
1072                         le64_to_cpu(ql_iscsi_stats->mac_tx_control_frames);
1073         host_stats->mactx_deferral =
1074                         le64_to_cpu(ql_iscsi_stats->mac_tx_deferral);
1075         host_stats->mactx_excess_deferral =
1076                         le64_to_cpu(ql_iscsi_stats->mac_tx_excess_deferral);
1077         host_stats->mactx_late_collision =
1078                         le64_to_cpu(ql_iscsi_stats->mac_tx_late_collision);
1079         host_stats->mactx_abort = le64_to_cpu(ql_iscsi_stats->mac_tx_abort);
1080         host_stats->mactx_single_collision =
1081                         le64_to_cpu(ql_iscsi_stats->mac_tx_single_collision);
1082         host_stats->mactx_multiple_collision =
1083                         le64_to_cpu(ql_iscsi_stats->mac_tx_multiple_collision);
1084         host_stats->mactx_collision =
1085                         le64_to_cpu(ql_iscsi_stats->mac_tx_collision);
1086         host_stats->mactx_frames_dropped =
1087                         le64_to_cpu(ql_iscsi_stats->mac_tx_frames_dropped);
1088         host_stats->mactx_jumbo_frames =
1089                         le64_to_cpu(ql_iscsi_stats->mac_tx_jumbo_frames);
1090         host_stats->macrx_frames = le64_to_cpu(ql_iscsi_stats->mac_rx_frames);
1091         host_stats->macrx_bytes = le64_to_cpu(ql_iscsi_stats->mac_rx_bytes);
1092         host_stats->macrx_unknown_control_frames =
1093                 le64_to_cpu(ql_iscsi_stats->mac_rx_unknown_control_frames);
1094         host_stats->macrx_pause_frames =
1095                         le64_to_cpu(ql_iscsi_stats->mac_rx_pause_frames);
1096         host_stats->macrx_control_frames =
1097                         le64_to_cpu(ql_iscsi_stats->mac_rx_control_frames);
1098         host_stats->macrx_dribble =
1099                         le64_to_cpu(ql_iscsi_stats->mac_rx_dribble);
1100         host_stats->macrx_frame_length_error =
1101                         le64_to_cpu(ql_iscsi_stats->mac_rx_frame_length_error);
1102         host_stats->macrx_jabber = le64_to_cpu(ql_iscsi_stats->mac_rx_jabber);
1103         host_stats->macrx_carrier_sense_error =
1104                 le64_to_cpu(ql_iscsi_stats->mac_rx_carrier_sense_error);
1105         host_stats->macrx_frame_discarded =
1106                         le64_to_cpu(ql_iscsi_stats->mac_rx_frame_discarded);
1107         host_stats->macrx_frames_dropped =
1108                         le64_to_cpu(ql_iscsi_stats->mac_rx_frames_dropped);
1109         host_stats->mac_crc_error = le64_to_cpu(ql_iscsi_stats->mac_crc_error);
1110         host_stats->mac_encoding_error =
1111                         le64_to_cpu(ql_iscsi_stats->mac_encoding_error);
1112         host_stats->macrx_length_error_large =
1113                         le64_to_cpu(ql_iscsi_stats->mac_rx_length_error_large);
1114         host_stats->macrx_length_error_small =
1115                         le64_to_cpu(ql_iscsi_stats->mac_rx_length_error_small);
1116         host_stats->macrx_multicast_frames =
1117                         le64_to_cpu(ql_iscsi_stats->mac_rx_multicast_frames);
1118         host_stats->macrx_broadcast_frames =
1119                         le64_to_cpu(ql_iscsi_stats->mac_rx_broadcast_frames);
1120         host_stats->iptx_packets = le64_to_cpu(ql_iscsi_stats->ip_tx_packets);
1121         host_stats->iptx_bytes = le64_to_cpu(ql_iscsi_stats->ip_tx_bytes);
1122         host_stats->iptx_fragments =
1123                         le64_to_cpu(ql_iscsi_stats->ip_tx_fragments);
1124         host_stats->iprx_packets = le64_to_cpu(ql_iscsi_stats->ip_rx_packets);
1125         host_stats->iprx_bytes = le64_to_cpu(ql_iscsi_stats->ip_rx_bytes);
1126         host_stats->iprx_fragments =
1127                         le64_to_cpu(ql_iscsi_stats->ip_rx_fragments);
1128         host_stats->ip_datagram_reassembly =
1129                         le64_to_cpu(ql_iscsi_stats->ip_datagram_reassembly);
1130         host_stats->ip_invalid_address_error =
1131                         le64_to_cpu(ql_iscsi_stats->ip_invalid_address_error);
1132         host_stats->ip_error_packets =
1133                         le64_to_cpu(ql_iscsi_stats->ip_error_packets);
1134         host_stats->ip_fragrx_overlap =
1135                         le64_to_cpu(ql_iscsi_stats->ip_fragrx_overlap);
1136         host_stats->ip_fragrx_outoforder =
1137                         le64_to_cpu(ql_iscsi_stats->ip_fragrx_outoforder);
1138         host_stats->ip_datagram_reassembly_timeout =
1139                 le64_to_cpu(ql_iscsi_stats->ip_datagram_reassembly_timeout);
1140         host_stats->ipv6tx_packets =
1141                         le64_to_cpu(ql_iscsi_stats->ipv6_tx_packets);
1142         host_stats->ipv6tx_bytes = le64_to_cpu(ql_iscsi_stats->ipv6_tx_bytes);
1143         host_stats->ipv6tx_fragments =
1144                         le64_to_cpu(ql_iscsi_stats->ipv6_tx_fragments);
1145         host_stats->ipv6rx_packets =
1146                         le64_to_cpu(ql_iscsi_stats->ipv6_rx_packets);
1147         host_stats->ipv6rx_bytes = le64_to_cpu(ql_iscsi_stats->ipv6_rx_bytes);
1148         host_stats->ipv6rx_fragments =
1149                         le64_to_cpu(ql_iscsi_stats->ipv6_rx_fragments);
1150         host_stats->ipv6_datagram_reassembly =
1151                         le64_to_cpu(ql_iscsi_stats->ipv6_datagram_reassembly);
1152         host_stats->ipv6_invalid_address_error =
1153                 le64_to_cpu(ql_iscsi_stats->ipv6_invalid_address_error);
1154         host_stats->ipv6_error_packets =
1155                         le64_to_cpu(ql_iscsi_stats->ipv6_error_packets);
1156         host_stats->ipv6_fragrx_overlap =
1157                         le64_to_cpu(ql_iscsi_stats->ipv6_fragrx_overlap);
1158         host_stats->ipv6_fragrx_outoforder =
1159                         le64_to_cpu(ql_iscsi_stats->ipv6_fragrx_outoforder);
1160         host_stats->ipv6_datagram_reassembly_timeout =
1161                 le64_to_cpu(ql_iscsi_stats->ipv6_datagram_reassembly_timeout);
1162         host_stats->tcptx_segments =
1163                         le64_to_cpu(ql_iscsi_stats->tcp_tx_segments);
1164         host_stats->tcptx_bytes = le64_to_cpu(ql_iscsi_stats->tcp_tx_bytes);
1165         host_stats->tcprx_segments =
1166                         le64_to_cpu(ql_iscsi_stats->tcp_rx_segments);
1167         host_stats->tcprx_byte = le64_to_cpu(ql_iscsi_stats->tcp_rx_byte);
1168         host_stats->tcp_duplicate_ack_retx =
1169                         le64_to_cpu(ql_iscsi_stats->tcp_duplicate_ack_retx);
1170         host_stats->tcp_retx_timer_expired =
1171                         le64_to_cpu(ql_iscsi_stats->tcp_retx_timer_expired);
1172         host_stats->tcprx_duplicate_ack =
1173                         le64_to_cpu(ql_iscsi_stats->tcp_rx_duplicate_ack);
1174         host_stats->tcprx_pure_ackr =
1175                         le64_to_cpu(ql_iscsi_stats->tcp_rx_pure_ackr);
1176         host_stats->tcptx_delayed_ack =
1177                         le64_to_cpu(ql_iscsi_stats->tcp_tx_delayed_ack);
1178         host_stats->tcptx_pure_ack =
1179                         le64_to_cpu(ql_iscsi_stats->tcp_tx_pure_ack);
1180         host_stats->tcprx_segment_error =
1181                         le64_to_cpu(ql_iscsi_stats->tcp_rx_segment_error);
1182         host_stats->tcprx_segment_outoforder =
1183                         le64_to_cpu(ql_iscsi_stats->tcp_rx_segment_outoforder);
1184         host_stats->tcprx_window_probe =
1185                         le64_to_cpu(ql_iscsi_stats->tcp_rx_window_probe);
1186         host_stats->tcprx_window_update =
1187                         le64_to_cpu(ql_iscsi_stats->tcp_rx_window_update);
1188         host_stats->tcptx_window_probe_persist =
1189                 le64_to_cpu(ql_iscsi_stats->tcp_tx_window_probe_persist);
1190         host_stats->ecc_error_correction =
1191                         le64_to_cpu(ql_iscsi_stats->ecc_error_correction);
1192         host_stats->iscsi_pdu_tx = le64_to_cpu(ql_iscsi_stats->iscsi_pdu_tx);
1193         host_stats->iscsi_data_bytes_tx =
1194                         le64_to_cpu(ql_iscsi_stats->iscsi_data_bytes_tx);
1195         host_stats->iscsi_pdu_rx = le64_to_cpu(ql_iscsi_stats->iscsi_pdu_rx);
1196         host_stats->iscsi_data_bytes_rx =
1197                         le64_to_cpu(ql_iscsi_stats->iscsi_data_bytes_rx);
1198         host_stats->iscsi_io_completed =
1199                         le64_to_cpu(ql_iscsi_stats->iscsi_io_completed);
1200         host_stats->iscsi_unexpected_io_rx =
1201                         le64_to_cpu(ql_iscsi_stats->iscsi_unexpected_io_rx);
1202         host_stats->iscsi_format_error =
1203                         le64_to_cpu(ql_iscsi_stats->iscsi_format_error);
1204         host_stats->iscsi_hdr_digest_error =
1205                         le64_to_cpu(ql_iscsi_stats->iscsi_hdr_digest_error);
1206         host_stats->iscsi_data_digest_error =
1207                         le64_to_cpu(ql_iscsi_stats->iscsi_data_digest_error);
1208         host_stats->iscsi_sequence_error =
1209                         le64_to_cpu(ql_iscsi_stats->iscsi_sequence_error);
1210 exit_host_stats:
1211         if (ql_iscsi_stats)
1212                 dma_free_coherent(&ha->pdev->dev, host_stats_size,
1213                                   ql_iscsi_stats, iscsi_stats_dma);
1214 
1215         ql4_printk(KERN_INFO, ha, "%s: Get host stats done\n",
1216                    __func__);
1217         return ret;
1218 }
1219 
1220 static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
1221                                    enum iscsi_param_type param_type,
1222                                    int param, char *buf)
1223 {
1224         struct Scsi_Host *shost = iscsi_iface_to_shost(iface);
1225         struct scsi_qla_host *ha = to_qla_host(shost);
1226         int ival;
1227         char *pval = NULL;
1228         int len = -ENOSYS;
1229 
1230         if (param_type == ISCSI_NET_PARAM) {
1231                 switch (param) {
1232                 case ISCSI_NET_PARAM_IPV4_ADDR:
1233                         len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
1234                         break;
1235                 case ISCSI_NET_PARAM_IPV4_SUBNET:
1236                         len = sprintf(buf, "%pI4\n",
1237                                       &ha->ip_config.subnet_mask);
1238                         break;
1239                 case ISCSI_NET_PARAM_IPV4_GW:
1240                         len = sprintf(buf, "%pI4\n", &ha->ip_config.gateway);
1241                         break;
1242                 case ISCSI_NET_PARAM_IFACE_ENABLE:
1243                         if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
1244                                 OP_STATE(ha->ip_config.ipv4_options,
1245                                          IPOPT_IPV4_PROTOCOL_ENABLE, pval);
1246                         } else {
1247                                 OP_STATE(ha->ip_config.ipv6_options,
1248                                          IPV6_OPT_IPV6_PROTOCOL_ENABLE, pval);
1249                         }
1250 
1251                         len = sprintf(buf, "%s\n", pval);
1252                         break;
1253                 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
1254                         len = sprintf(buf, "%s\n",
1255                                       (ha->ip_config.tcp_options &
1256                                        TCPOPT_DHCP_ENABLE) ?
1257                                       "dhcp" : "static");
1258                         break;
1259                 case ISCSI_NET_PARAM_IPV6_ADDR:
1260                         if (iface->iface_num == 0)
1261                                 len = sprintf(buf, "%pI6\n",
1262                                               &ha->ip_config.ipv6_addr0);
1263                         if (iface->iface_num == 1)
1264                                 len = sprintf(buf, "%pI6\n",
1265                                               &ha->ip_config.ipv6_addr1);
1266                         break;
1267                 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
1268                         len = sprintf(buf, "%pI6\n",
1269                                       &ha->ip_config.ipv6_link_local_addr);
1270                         break;
1271                 case ISCSI_NET_PARAM_IPV6_ROUTER:
1272                         len = sprintf(buf, "%pI6\n",
1273                                       &ha->ip_config.ipv6_default_router_addr);
1274                         break;
1275                 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
1276                         pval = (ha->ip_config.ipv6_addl_options &
1277                                 IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) ?
1278                                 "nd" : "static";
1279 
1280                         len = sprintf(buf, "%s\n", pval);
1281                         break;
1282                 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
1283                         pval = (ha->ip_config.ipv6_addl_options &
1284                                 IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR) ?
1285                                 "auto" : "static";
1286 
1287                         len = sprintf(buf, "%s\n", pval);
1288                         break;
1289                 case ISCSI_NET_PARAM_VLAN_ID:
1290                         if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
1291                                 ival = ha->ip_config.ipv4_vlan_tag &
1292                                        ISCSI_MAX_VLAN_ID;
1293                         else
1294                                 ival = ha->ip_config.ipv6_vlan_tag &
1295                                        ISCSI_MAX_VLAN_ID;
1296 
1297                         len = sprintf(buf, "%d\n", ival);
1298                         break;
1299                 case ISCSI_NET_PARAM_VLAN_PRIORITY:
1300                         if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
1301                                 ival = (ha->ip_config.ipv4_vlan_tag >> 13) &
1302                                        ISCSI_MAX_VLAN_PRIORITY;
1303                         else
1304                                 ival = (ha->ip_config.ipv6_vlan_tag >> 13) &
1305                                        ISCSI_MAX_VLAN_PRIORITY;
1306 
1307                         len = sprintf(buf, "%d\n", ival);
1308                         break;
1309                 case ISCSI_NET_PARAM_VLAN_ENABLED:
1310                         if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
1311                                 OP_STATE(ha->ip_config.ipv4_options,
1312                                          IPOPT_VLAN_TAGGING_ENABLE, pval);
1313                         } else {
1314                                 OP_STATE(ha->ip_config.ipv6_options,
1315                                          IPV6_OPT_VLAN_TAGGING_ENABLE, pval);
1316                         }
1317                         len = sprintf(buf, "%s\n", pval);
1318                         break;
1319                 case ISCSI_NET_PARAM_MTU:
1320                         len = sprintf(buf, "%d\n", ha->ip_config.eth_mtu_size);
1321                         break;
1322                 case ISCSI_NET_PARAM_PORT:
1323                         if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
1324                                 len = sprintf(buf, "%d\n",
1325                                               ha->ip_config.ipv4_port);
1326                         else
1327                                 len = sprintf(buf, "%d\n",
1328                                               ha->ip_config.ipv6_port);
1329                         break;
1330                 case ISCSI_NET_PARAM_IPADDR_STATE:
1331                         if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
1332                                 pval = iscsi_get_ipaddress_state_name(
1333                                                 ha->ip_config.ipv4_addr_state);
1334                         } else {
1335                                 if (iface->iface_num == 0)
1336                                         pval = iscsi_get_ipaddress_state_name(
1337                                                 ha->ip_config.ipv6_addr0_state);
1338                                 else if (iface->iface_num == 1)
1339                                         pval = iscsi_get_ipaddress_state_name(
1340                                                 ha->ip_config.ipv6_addr1_state);
1341                         }
1342 
1343                         len = sprintf(buf, "%s\n", pval);
1344                         break;
1345                 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_STATE:
1346                         pval = iscsi_get_ipaddress_state_name(
1347                                         ha->ip_config.ipv6_link_local_state);
1348                         len = sprintf(buf, "%s\n", pval);
1349                         break;
1350                 case ISCSI_NET_PARAM_IPV6_ROUTER_STATE:
1351                         pval = iscsi_get_router_state_name(
1352                                       ha->ip_config.ipv6_default_router_state);
1353                         len = sprintf(buf, "%s\n", pval);
1354                         break;
1355                 case ISCSI_NET_PARAM_DELAYED_ACK_EN:
1356                         if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
1357                                 OP_STATE(~ha->ip_config.tcp_options,
1358                                          TCPOPT_DELAYED_ACK_DISABLE, pval);
1359                         } else {
1360                                 OP_STATE(~ha->ip_config.ipv6_tcp_options,
1361                                          IPV6_TCPOPT_DELAYED_ACK_DISABLE, pval);
1362                         }
1363                         len = sprintf(buf, "%s\n", pval);
1364                         break;
1365                 case ISCSI_NET_PARAM_TCP_NAGLE_DISABLE:
1366                         if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
1367                                 OP_STATE(~ha->ip_config.tcp_options,
1368                                          TCPOPT_NAGLE_ALGO_DISABLE, pval);
1369                         } else {
1370                                 OP_STATE(~ha->ip_config.ipv6_tcp_options,
1371                                          IPV6_TCPOPT_NAGLE_ALGO_DISABLE, pval);
1372                         }
1373                         len = sprintf(buf, "%s\n", pval);
1374                         break;
1375                 case ISCSI_NET_PARAM_TCP_WSF_DISABLE:
1376                         if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
1377                                 OP_STATE(~ha->ip_config.tcp_options,
1378                                          TCPOPT_WINDOW_SCALE_DISABLE, pval);
1379                         } else {
1380                                 OP_STATE(~ha->ip_config.ipv6_tcp_options,
1381                                          IPV6_TCPOPT_WINDOW_SCALE_DISABLE,
1382                                          pval);
1383                         }
1384                         len = sprintf(buf, "%s\n", pval);
1385                         break;
1386                 case ISCSI_NET_PARAM_TCP_WSF:
1387                         if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
1388                                 len = sprintf(buf, "%d\n",
1389                                               ha->ip_config.tcp_wsf);
1390                         else
1391                                 len = sprintf(buf, "%d\n",
1392                                               ha->ip_config.ipv6_tcp_wsf);
1393                         break;
1394                 case ISCSI_NET_PARAM_TCP_TIMER_SCALE:
1395                         if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
1396                                 ival = (ha->ip_config.tcp_options &
1397                                         TCPOPT_TIMER_SCALE) >> 1;
1398                         else
1399                                 ival = (ha->ip_config.ipv6_tcp_options &
1400                                         IPV6_TCPOPT_TIMER_SCALE) >> 1;
1401 
1402                         len = sprintf(buf, "%d\n", ival);
1403                         break;
1404                 case ISCSI_NET_PARAM_TCP_TIMESTAMP_EN:
1405                         if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
1406                                 OP_STATE(ha->ip_config.tcp_options,
1407                                          TCPOPT_TIMESTAMP_ENABLE, pval);
1408                         } else {
1409                                 OP_STATE(ha->ip_config.ipv6_tcp_options,
1410                                          IPV6_TCPOPT_TIMESTAMP_EN, pval);
1411                         }
1412                         len = sprintf(buf, "%s\n", pval);
1413                         break;
1414                 case ISCSI_NET_PARAM_CACHE_ID:
1415                         if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
1416                                 len = sprintf(buf, "%d\n",
1417                                               ha->ip_config.ipv4_cache_id);
1418                         else
1419                                 len = sprintf(buf, "%d\n",
1420                                               ha->ip_config.ipv6_cache_id);
1421                         break;
1422                 case ISCSI_NET_PARAM_IPV4_DHCP_DNS_ADDR_EN:
1423                         OP_STATE(ha->ip_config.tcp_options,
1424                                  TCPOPT_DNS_SERVER_IP_EN, pval);
1425 
1426                         len = sprintf(buf, "%s\n", pval);
1427                         break;
1428                 case ISCSI_NET_PARAM_IPV4_DHCP_SLP_DA_EN:
1429                         OP_STATE(ha->ip_config.tcp_options,
1430                                  TCPOPT_SLP_DA_INFO_EN, pval);
1431 
1432                         len = sprintf(buf, "%s\n", pval);
1433                         break;
1434                 case ISCSI_NET_PARAM_IPV4_TOS_EN:
1435                         OP_STATE(ha->ip_config.ipv4_options,
1436                                  IPOPT_IPV4_TOS_EN, pval);
1437 
1438                         len = sprintf(buf, "%s\n", pval);
1439                         break;
1440                 case ISCSI_NET_PARAM_IPV4_TOS:
1441                         len = sprintf(buf, "%d\n", ha->ip_config.ipv4_tos);
1442                         break;
1443                 case ISCSI_NET_PARAM_IPV4_GRAT_ARP_EN:
1444                         OP_STATE(ha->ip_config.ipv4_options,
1445                                  IPOPT_GRAT_ARP_EN, pval);
1446 
1447                         len = sprintf(buf, "%s\n", pval);
1448                         break;
1449                 case ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID_EN:
1450                         OP_STATE(ha->ip_config.ipv4_options, IPOPT_ALT_CID_EN,
1451                                  pval);
1452 
1453                         len = sprintf(buf, "%s\n", pval);
1454                         break;
1455                 case ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID:
1456                         pval = (ha->ip_config.ipv4_alt_cid_len) ?
1457                                (char *)ha->ip_config.ipv4_alt_cid : "";
1458 
1459                         len = sprintf(buf, "%s\n", pval);
1460                         break;
1461                 case ISCSI_NET_PARAM_IPV4_DHCP_REQ_VENDOR_ID_EN:
1462                         OP_STATE(ha->ip_config.ipv4_options,
1463                                  IPOPT_REQ_VID_EN, pval);
1464 
1465                         len = sprintf(buf, "%s\n", pval);
1466                         break;
1467                 case ISCSI_NET_PARAM_IPV4_DHCP_USE_VENDOR_ID_EN:
1468                         OP_STATE(ha->ip_config.ipv4_options,
1469                                  IPOPT_USE_VID_EN, pval);
1470 
1471                         len = sprintf(buf, "%s\n", pval);
1472                         break;
1473                 case ISCSI_NET_PARAM_IPV4_DHCP_VENDOR_ID:
1474                         pval = (ha->ip_config.ipv4_vid_len) ?
1475                                (char *)ha->ip_config.ipv4_vid : "";
1476 
1477                         len = sprintf(buf, "%s\n", pval);
1478                         break;
1479                 case ISCSI_NET_PARAM_IPV4_DHCP_LEARN_IQN_EN:
1480                         OP_STATE(ha->ip_config.ipv4_options,
1481                                  IPOPT_LEARN_IQN_EN, pval);
1482 
1483                         len = sprintf(buf, "%s\n", pval);
1484                         break;
1485                 case ISCSI_NET_PARAM_IPV4_FRAGMENT_DISABLE:
1486                         OP_STATE(~ha->ip_config.ipv4_options,
1487                                  IPOPT_FRAGMENTATION_DISABLE, pval);
1488 
1489                         len = sprintf(buf, "%s\n", pval);
1490                         break;
1491                 case ISCSI_NET_PARAM_IPV4_IN_FORWARD_EN:
1492                         OP_STATE(ha->ip_config.ipv4_options,
1493                                  IPOPT_IN_FORWARD_EN, pval);
1494 
1495                         len = sprintf(buf, "%s\n", pval);
1496                         break;
1497                 case ISCSI_NET_PARAM_REDIRECT_EN:
1498                         if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
1499                                 OP_STATE(ha->ip_config.ipv4_options,
1500                                          IPOPT_ARP_REDIRECT_EN, pval);
1501                         } else {
1502                                 OP_STATE(ha->ip_config.ipv6_options,
1503                                          IPV6_OPT_REDIRECT_EN, pval);
1504                         }
1505                         len = sprintf(buf, "%s\n", pval);
1506                         break;
1507                 case ISCSI_NET_PARAM_IPV4_TTL:
1508                         len = sprintf(buf, "%d\n", ha->ip_config.ipv4_ttl);
1509                         break;
1510                 case ISCSI_NET_PARAM_IPV6_GRAT_NEIGHBOR_ADV_EN:
1511                         OP_STATE(ha->ip_config.ipv6_options,
1512                                  IPV6_OPT_GRAT_NEIGHBOR_ADV_EN, pval);
1513 
1514                         len = sprintf(buf, "%s\n", pval);
1515                         break;
1516                 case ISCSI_NET_PARAM_IPV6_MLD_EN:
1517                         OP_STATE(ha->ip_config.ipv6_addl_options,
1518                                  IPV6_ADDOPT_MLD_EN, pval);
1519 
1520                         len = sprintf(buf, "%s\n", pval);
1521                         break;
1522                 case ISCSI_NET_PARAM_IPV6_FLOW_LABEL:
1523                         len = sprintf(buf, "%u\n", ha->ip_config.ipv6_flow_lbl);
1524                         break;
1525                 case ISCSI_NET_PARAM_IPV6_TRAFFIC_CLASS:
1526                         len = sprintf(buf, "%d\n",
1527                                       ha->ip_config.ipv6_traffic_class);
1528                         break;
1529                 case ISCSI_NET_PARAM_IPV6_HOP_LIMIT:
1530                         len = sprintf(buf, "%d\n",
1531                                       ha->ip_config.ipv6_hop_limit);
1532                         break;
1533                 case ISCSI_NET_PARAM_IPV6_ND_REACHABLE_TMO:
1534                         len = sprintf(buf, "%d\n",
1535                                       ha->ip_config.ipv6_nd_reach_time);
1536                         break;
1537                 case ISCSI_NET_PARAM_IPV6_ND_REXMIT_TIME:
1538                         len = sprintf(buf, "%d\n",
1539                                       ha->ip_config.ipv6_nd_rexmit_timer);
1540                         break;
1541                 case ISCSI_NET_PARAM_IPV6_ND_STALE_TMO:
1542                         len = sprintf(buf, "%d\n",
1543                                       ha->ip_config.ipv6_nd_stale_timeout);
1544                         break;
1545                 case ISCSI_NET_PARAM_IPV6_DUP_ADDR_DETECT_CNT:
1546                         len = sprintf(buf, "%d\n",
1547                                       ha->ip_config.ipv6_dup_addr_detect_count);
1548                         break;
1549                 case ISCSI_NET_PARAM_IPV6_RTR_ADV_LINK_MTU:
1550                         len = sprintf(buf, "%d\n",
1551                                       ha->ip_config.ipv6_gw_advrt_mtu);
1552                         break;
1553                 default:
1554                         len = -ENOSYS;
1555                 }
1556         } else if (param_type == ISCSI_IFACE_PARAM) {
1557                 switch (param) {
1558                 case ISCSI_IFACE_PARAM_DEF_TASKMGMT_TMO:
1559                         len = sprintf(buf, "%d\n", ha->ip_config.def_timeout);
1560                         break;
1561                 case ISCSI_IFACE_PARAM_HDRDGST_EN:
1562                         OP_STATE(ha->ip_config.iscsi_options,
1563                                  ISCSIOPTS_HEADER_DIGEST_EN, pval);
1564 
1565                         len = sprintf(buf, "%s\n", pval);
1566                         break;
1567                 case ISCSI_IFACE_PARAM_DATADGST_EN:
1568                         OP_STATE(ha->ip_config.iscsi_options,
1569                                  ISCSIOPTS_DATA_DIGEST_EN, pval);
1570 
1571                         len = sprintf(buf, "%s\n", pval);
1572                         break;
1573                 case ISCSI_IFACE_PARAM_IMM_DATA_EN:
1574                         OP_STATE(ha->ip_config.iscsi_options,
1575                                  ISCSIOPTS_IMMEDIATE_DATA_EN, pval);
1576 
1577                         len = sprintf(buf, "%s\n", pval);
1578                         break;
1579                 case ISCSI_IFACE_PARAM_INITIAL_R2T_EN:
1580                         OP_STATE(ha->ip_config.iscsi_options,
1581                                  ISCSIOPTS_INITIAL_R2T_EN, pval);
1582 
1583                         len = sprintf(buf, "%s\n", pval);
1584                         break;
1585                 case ISCSI_IFACE_PARAM_DATASEQ_INORDER_EN:
1586                         OP_STATE(ha->ip_config.iscsi_options,
1587                                  ISCSIOPTS_DATA_SEQ_INORDER_EN, pval);
1588 
1589                         len = sprintf(buf, "%s\n", pval);
1590                         break;
1591                 case ISCSI_IFACE_PARAM_PDU_INORDER_EN:
1592                         OP_STATE(ha->ip_config.iscsi_options,
1593                                  ISCSIOPTS_DATA_PDU_INORDER_EN, pval);
1594 
1595                         len = sprintf(buf, "%s\n", pval);
1596                         break;
1597                 case ISCSI_IFACE_PARAM_ERL:
1598                         len = sprintf(buf, "%d\n",
1599                                       (ha->ip_config.iscsi_options &
1600                                        ISCSIOPTS_ERL));
1601                         break;
1602                 case ISCSI_IFACE_PARAM_MAX_RECV_DLENGTH:
1603                         len = sprintf(buf, "%u\n",
1604                                       ha->ip_config.iscsi_max_pdu_size *
1605                                       BYTE_UNITS);
1606                         break;
1607                 case ISCSI_IFACE_PARAM_FIRST_BURST:
1608                         len = sprintf(buf, "%u\n",
1609                                       ha->ip_config.iscsi_first_burst_len *
1610                                       BYTE_UNITS);
1611                         break;
1612                 case ISCSI_IFACE_PARAM_MAX_R2T:
1613                         len = sprintf(buf, "%d\n",
1614                                       ha->ip_config.iscsi_max_outstnd_r2t);
1615                         break;
1616                 case ISCSI_IFACE_PARAM_MAX_BURST:
1617                         len = sprintf(buf, "%u\n",
1618                                       ha->ip_config.iscsi_max_burst_len *
1619                                       BYTE_UNITS);
1620                         break;
1621                 case ISCSI_IFACE_PARAM_CHAP_AUTH_EN:
1622                         OP_STATE(ha->ip_config.iscsi_options,
1623                                  ISCSIOPTS_CHAP_AUTH_EN, pval);
1624 
1625                         len = sprintf(buf, "%s\n", pval);
1626                         break;
1627                 case ISCSI_IFACE_PARAM_BIDI_CHAP_EN:
1628                         OP_STATE(ha->ip_config.iscsi_options,
1629                                  ISCSIOPTS_BIDI_CHAP_EN, pval);
1630 
1631                         len = sprintf(buf, "%s\n", pval);
1632                         break;
1633                 case ISCSI_IFACE_PARAM_DISCOVERY_AUTH_OPTIONAL:
1634                         OP_STATE(ha->ip_config.iscsi_options,
1635                                  ISCSIOPTS_DISCOVERY_AUTH_EN, pval);
1636 
1637                         len = sprintf(buf, "%s\n", pval);
1638                         break;
1639                 case ISCSI_IFACE_PARAM_DISCOVERY_LOGOUT_EN:
1640                         OP_STATE(ha->ip_config.iscsi_options,
1641                                  ISCSIOPTS_DISCOVERY_LOGOUT_EN, pval);
1642 
1643                         len = sprintf(buf, "%s\n", pval);
1644                         break;
1645                 case ISCSI_IFACE_PARAM_STRICT_LOGIN_COMP_EN:
1646                         OP_STATE(ha->ip_config.iscsi_options,
1647                                  ISCSIOPTS_STRICT_LOGIN_COMP_EN, pval);
1648 
1649                         len = sprintf(buf, "%s\n", pval);
1650                         break;
1651                 case ISCSI_IFACE_PARAM_INITIATOR_NAME:
1652                         len = sprintf(buf, "%s\n", ha->ip_config.iscsi_name);
1653                         break;
1654                 default:
1655                         len = -ENOSYS;
1656                 }
1657         }
1658 
1659         return len;
1660 }
1661 
1662 static struct iscsi_endpoint *
1663 qla4xxx_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
1664                    int non_blocking)
1665 {
1666         int ret;
1667         struct iscsi_endpoint *ep;
1668         struct qla_endpoint *qla_ep;
1669         struct scsi_qla_host *ha;
1670         struct sockaddr_in *addr;
1671         struct sockaddr_in6 *addr6;
1672 
1673         if (!shost) {
1674                 ret = -ENXIO;
1675                 pr_err("%s: shost is NULL\n", __func__);
1676                 return ERR_PTR(ret);
1677         }
1678 
1679         ha = iscsi_host_priv(shost);
1680         ep = iscsi_create_endpoint(sizeof(struct qla_endpoint));
1681         if (!ep) {
1682                 ret = -ENOMEM;
1683                 return ERR_PTR(ret);
1684         }
1685 
1686         qla_ep = ep->dd_data;
1687         memset(qla_ep, 0, sizeof(struct qla_endpoint));
1688         if (dst_addr->sa_family == AF_INET) {
1689                 memcpy(&qla_ep->dst_addr, dst_addr, sizeof(struct sockaddr_in));
1690                 addr = (struct sockaddr_in *)&qla_ep->dst_addr;
1691                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI4\n", __func__,
1692                                   (char *)&addr->sin_addr));
1693         } else if (dst_addr->sa_family == AF_INET6) {
1694                 memcpy(&qla_ep->dst_addr, dst_addr,
1695                        sizeof(struct sockaddr_in6));
1696                 addr6 = (struct sockaddr_in6 *)&qla_ep->dst_addr;
1697                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI6\n", __func__,
1698                                   (char *)&addr6->sin6_addr));
1699         } else {
1700                 ql4_printk(KERN_WARNING, ha, "%s: Invalid endpoint\n",
1701                            __func__);
1702         }
1703 
1704         qla_ep->host = shost;
1705 
1706         return ep;
1707 }
1708 
1709 static int qla4xxx_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
1710 {
1711         struct qla_endpoint *qla_ep;
1712         struct scsi_qla_host *ha;
1713         int ret = 0;
1714 
1715         qla_ep = ep->dd_data;
1716         ha = to_qla_host(qla_ep->host);
1717         DEBUG2(pr_info_ratelimited("%s: host: %ld\n", __func__, ha->host_no));
1718 
1719         if (adapter_up(ha) && !test_bit(AF_BUILD_DDB_LIST, &ha->flags))
1720                 ret = 1;
1721 
1722         return ret;
1723 }
1724 
1725 static void qla4xxx_ep_disconnect(struct iscsi_endpoint *ep)
1726 {
1727         struct qla_endpoint *qla_ep;
1728         struct scsi_qla_host *ha;
1729 
1730         qla_ep = ep->dd_data;
1731         ha = to_qla_host(qla_ep->host);
1732         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: host: %ld\n", __func__,
1733                           ha->host_no));
1734         iscsi_destroy_endpoint(ep);
1735 }
1736 
1737 static int qla4xxx_get_ep_param(struct iscsi_endpoint *ep,
1738                                 enum iscsi_param param,
1739                                 char *buf)
1740 {
1741         struct qla_endpoint *qla_ep = ep->dd_data;
1742         struct sockaddr *dst_addr;
1743         struct scsi_qla_host *ha;
1744 
1745         ha = to_qla_host(qla_ep->host);
1746         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: host: %ld\n", __func__,
1747                           ha->host_no));
1748 
1749         switch (param) {
1750         case ISCSI_PARAM_CONN_PORT:
1751         case ISCSI_PARAM_CONN_ADDRESS:
1752                 if (!qla_ep)
1753                         return -ENOTCONN;
1754 
1755                 dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
1756                 if (!dst_addr)
1757                         return -ENOTCONN;
1758 
1759                 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
1760                                                  &qla_ep->dst_addr, param, buf);
1761         default:
1762                 return -ENOSYS;
1763         }
1764 }
1765 
1766 static void qla4xxx_conn_get_stats(struct iscsi_cls_conn *cls_conn,
1767                                    struct iscsi_stats *stats)
1768 {
1769         struct iscsi_session *sess;
1770         struct iscsi_cls_session *cls_sess;
1771         struct ddb_entry *ddb_entry;
1772         struct scsi_qla_host *ha;
1773         struct ql_iscsi_stats *ql_iscsi_stats;
1774         int stats_size;
1775         int ret;
1776         dma_addr_t iscsi_stats_dma;
1777 
1778         cls_sess = iscsi_conn_to_session(cls_conn);
1779         sess = cls_sess->dd_data;
1780         ddb_entry = sess->dd_data;
1781         ha = ddb_entry->ha;
1782 
1783         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: host: %ld\n", __func__,
1784                           ha->host_no));
1785         stats_size = PAGE_ALIGN(sizeof(struct ql_iscsi_stats));
1786         /* Allocate memory */
1787         ql_iscsi_stats = dma_alloc_coherent(&ha->pdev->dev, stats_size,
1788                                             &iscsi_stats_dma, GFP_KERNEL);
1789         if (!ql_iscsi_stats) {
1790                 ql4_printk(KERN_ERR, ha,
1791                            "Unable to allocate memory for iscsi stats\n");
1792                 goto exit_get_stats;
1793         }
1794 
1795         ret =  qla4xxx_get_mgmt_data(ha, ddb_entry->fw_ddb_index, stats_size,
1796                                      iscsi_stats_dma);
1797         if (ret != QLA_SUCCESS) {
1798                 ql4_printk(KERN_ERR, ha,
1799                            "Unable to retrieve iscsi stats\n");
1800                 goto free_stats;
1801         }
1802 
1803         /* octets */
1804         stats->txdata_octets = le64_to_cpu(ql_iscsi_stats->tx_data_octets);
1805         stats->rxdata_octets = le64_to_cpu(ql_iscsi_stats->rx_data_octets);
1806         /* xmit pdus */
1807         stats->noptx_pdus = le32_to_cpu(ql_iscsi_stats->tx_nopout_pdus);
1808         stats->scsicmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_cmd_pdus);
1809         stats->tmfcmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_tmf_cmd_pdus);
1810         stats->login_pdus = le32_to_cpu(ql_iscsi_stats->tx_login_cmd_pdus);
1811         stats->text_pdus = le32_to_cpu(ql_iscsi_stats->tx_text_cmd_pdus);
1812         stats->dataout_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_write_pdus);
1813         stats->logout_pdus = le32_to_cpu(ql_iscsi_stats->tx_logout_cmd_pdus);
1814         stats->snack_pdus = le32_to_cpu(ql_iscsi_stats->tx_snack_req_pdus);
1815         /* recv pdus */
1816         stats->noprx_pdus = le32_to_cpu(ql_iscsi_stats->rx_nopin_pdus);
1817         stats->scsirsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_resp_pdus);
1818         stats->tmfrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_tmf_resp_pdus);
1819         stats->textrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_text_resp_pdus);
1820         stats->datain_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_read_pdus);
1821         stats->logoutrsp_pdus =
1822                         le32_to_cpu(ql_iscsi_stats->rx_logout_resp_pdus);
1823         stats->r2t_pdus = le32_to_cpu(ql_iscsi_stats->rx_r2t_pdus);
1824         stats->async_pdus = le32_to_cpu(ql_iscsi_stats->rx_async_pdus);
1825         stats->rjt_pdus = le32_to_cpu(ql_iscsi_stats->rx_reject_pdus);
1826 
1827 free_stats:
1828         dma_free_coherent(&ha->pdev->dev, stats_size, ql_iscsi_stats,
1829                           iscsi_stats_dma);
1830 exit_get_stats:
1831         return;
1832 }
1833 
1834 static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
1835 {
1836         struct iscsi_cls_session *session;
1837         struct iscsi_session *sess;
1838         unsigned long flags;
1839         enum blk_eh_timer_return ret = BLK_EH_NOT_HANDLED;
1840 
1841         session = starget_to_session(scsi_target(sc->device));
1842         sess = session->dd_data;
1843 
1844         spin_lock_irqsave(&session->lock, flags);
1845         if (session->state == ISCSI_SESSION_FAILED)
1846                 ret = BLK_EH_RESET_TIMER;
1847         spin_unlock_irqrestore(&session->lock, flags);
1848 
1849         return ret;
1850 }
1851 
1852 static void qla4xxx_set_port_speed(struct Scsi_Host *shost)
1853 {
1854         struct scsi_qla_host *ha = to_qla_host(shost);
1855         struct iscsi_cls_host *ihost = shost->shost_data;
1856         uint32_t speed = ISCSI_PORT_SPEED_UNKNOWN;
1857 
1858         qla4xxx_get_firmware_state(ha);
1859 
1860         switch (ha->addl_fw_state & 0x0F00) {
1861         case FW_ADDSTATE_LINK_SPEED_10MBPS:
1862                 speed = ISCSI_PORT_SPEED_10MBPS;
1863                 break;
1864         case FW_ADDSTATE_LINK_SPEED_100MBPS:
1865                 speed = ISCSI_PORT_SPEED_100MBPS;
1866                 break;
1867         case FW_ADDSTATE_LINK_SPEED_1GBPS:
1868                 speed = ISCSI_PORT_SPEED_1GBPS;
1869                 break;
1870         case FW_ADDSTATE_LINK_SPEED_10GBPS:
1871                 speed = ISCSI_PORT_SPEED_10GBPS;
1872                 break;
1873         }
1874         ihost->port_speed = speed;
1875 }
1876 
1877 static void qla4xxx_set_port_state(struct Scsi_Host *shost)
1878 {
1879         struct scsi_qla_host *ha = to_qla_host(shost);
1880         struct iscsi_cls_host *ihost = shost->shost_data;
1881         uint32_t state = ISCSI_PORT_STATE_DOWN;
1882 
1883         if (test_bit(AF_LINK_UP, &ha->flags))
1884                 state = ISCSI_PORT_STATE_UP;
1885 
1886         ihost->port_state = state;
1887 }
1888 
1889 static int qla4xxx_host_get_param(struct Scsi_Host *shost,
1890                                   enum iscsi_host_param param, char *buf)
1891 {
1892         struct scsi_qla_host *ha = to_qla_host(shost);
1893         int len;
1894 
1895         switch (param) {
1896         case ISCSI_HOST_PARAM_HWADDRESS:
1897                 len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
1898                 break;
1899         case ISCSI_HOST_PARAM_IPADDRESS:
1900                 len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
1901                 break;
1902         case ISCSI_HOST_PARAM_INITIATOR_NAME:
1903                 len = sprintf(buf, "%s\n", ha->name_string);
1904                 break;
1905         case ISCSI_HOST_PARAM_PORT_STATE:
1906                 qla4xxx_set_port_state(shost);
1907                 len = sprintf(buf, "%s\n", iscsi_get_port_state_name(shost));
1908                 break;
1909         case ISCSI_HOST_PARAM_PORT_SPEED:
1910                 qla4xxx_set_port_speed(shost);
1911                 len = sprintf(buf, "%s\n", iscsi_get_port_speed_name(shost));
1912                 break;
1913         default:
1914                 return -ENOSYS;
1915         }
1916 
1917         return len;
1918 }
1919 
1920 static void qla4xxx_create_ipv4_iface(struct scsi_qla_host *ha)
1921 {
1922         if (ha->iface_ipv4)
1923                 return;
1924 
1925         /* IPv4 */
1926         ha->iface_ipv4 = iscsi_create_iface(ha->host,
1927                                             &qla4xxx_iscsi_transport,
1928                                             ISCSI_IFACE_TYPE_IPV4, 0, 0);
1929         if (!ha->iface_ipv4)
1930                 ql4_printk(KERN_ERR, ha, "Could not create IPv4 iSCSI "
1931                            "iface0.\n");
1932 }
1933 
1934 static void qla4xxx_create_ipv6_iface(struct scsi_qla_host *ha)
1935 {
1936         if (!ha->iface_ipv6_0)
1937                 /* IPv6 iface-0 */
1938                 ha->iface_ipv6_0 = iscsi_create_iface(ha->host,
1939                                                       &qla4xxx_iscsi_transport,
1940                                                       ISCSI_IFACE_TYPE_IPV6, 0,
1941                                                       0);
1942         if (!ha->iface_ipv6_0)
1943                 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
1944                            "iface0.\n");
1945 
1946         if (!ha->iface_ipv6_1)
1947                 /* IPv6 iface-1 */
1948                 ha->iface_ipv6_1 = iscsi_create_iface(ha->host,
1949                                                       &qla4xxx_iscsi_transport,
1950                                                       ISCSI_IFACE_TYPE_IPV6, 1,
1951                                                       0);
1952         if (!ha->iface_ipv6_1)
1953                 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
1954                            "iface1.\n");
1955 }
1956 
1957 static void qla4xxx_create_ifaces(struct scsi_qla_host *ha)
1958 {
1959         if (ha->ip_config.ipv4_options & IPOPT_IPV4_PROTOCOL_ENABLE)
1960                 qla4xxx_create_ipv4_iface(ha);
1961 
1962         if (ha->ip_config.ipv6_options & IPV6_OPT_IPV6_PROTOCOL_ENABLE)
1963                 qla4xxx_create_ipv6_iface(ha);
1964 }
1965 
1966 static void qla4xxx_destroy_ipv4_iface(struct scsi_qla_host *ha)
1967 {
1968         if (ha->iface_ipv4) {
1969                 iscsi_destroy_iface(ha->iface_ipv4);
1970                 ha->iface_ipv4 = NULL;
1971         }
1972 }
1973 
1974 static void qla4xxx_destroy_ipv6_iface(struct scsi_qla_host *ha)
1975 {
1976         if (ha->iface_ipv6_0) {
1977                 iscsi_destroy_iface(ha->iface_ipv6_0);
1978                 ha->iface_ipv6_0 = NULL;
1979         }
1980         if (ha->iface_ipv6_1) {
1981                 iscsi_destroy_iface(ha->iface_ipv6_1);
1982                 ha->iface_ipv6_1 = NULL;
1983         }
1984 }
1985 
1986 static void qla4xxx_destroy_ifaces(struct scsi_qla_host *ha)
1987 {
1988         qla4xxx_destroy_ipv4_iface(ha);
1989         qla4xxx_destroy_ipv6_iface(ha);
1990 }
1991 
1992 static void qla4xxx_set_ipv6(struct scsi_qla_host *ha,
1993                              struct iscsi_iface_param_info *iface_param,
1994                              struct addr_ctrl_blk *init_fw_cb)
1995 {
1996         /*
1997          * iface_num 0 is valid for IPv6 Addr, linklocal, router, autocfg.
1998          * iface_num 1 is valid only for IPv6 Addr.
1999          */
2000         switch (iface_param->param) {
2001         case ISCSI_NET_PARAM_IPV6_ADDR:
2002                 if (iface_param->iface_num & 0x1)
2003                         /* IPv6 Addr 1 */
2004                         memcpy(init_fw_cb->ipv6_addr1, iface_param->value,
2005                                sizeof(init_fw_cb->ipv6_addr1));
2006                 else
2007                         /* IPv6 Addr 0 */
2008                         memcpy(init_fw_cb->ipv6_addr0, iface_param->value,
2009                                sizeof(init_fw_cb->ipv6_addr0));
2010                 break;
2011         case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
2012                 if (iface_param->iface_num & 0x1)
2013                         break;
2014                 memcpy(init_fw_cb->ipv6_if_id, &iface_param->value[8],
2015                        sizeof(init_fw_cb->ipv6_if_id));
2016                 break;
2017         case ISCSI_NET_PARAM_IPV6_ROUTER:
2018                 if (iface_param->iface_num & 0x1)
2019                         break;
2020                 memcpy(init_fw_cb->ipv6_dflt_rtr_addr, iface_param->value,
2021                        sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
2022                 break;
2023         case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
2024                 /* Autocfg applies to even interface */
2025                 if (iface_param->iface_num & 0x1)
2026                         break;
2027 
2028                 if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_DISABLE)
2029                         init_fw_cb->ipv6_addtl_opts &=
2030                                 cpu_to_le16(
2031                                   ~IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
2032                 else if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_ND_ENABLE)
2033                         init_fw_cb->ipv6_addtl_opts |=
2034                                 cpu_to_le16(
2035                                   IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
2036                 else
2037                         ql4_printk(KERN_ERR, ha,
2038                                    "Invalid autocfg setting for IPv6 addr\n");
2039                 break;
2040         case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
2041                 /* Autocfg applies to even interface */
2042                 if (iface_param->iface_num & 0x1)
2043                         break;
2044 
2045                 if (iface_param->value[0] ==
2046                     ISCSI_IPV6_LINKLOCAL_AUTOCFG_ENABLE)
2047                         init_fw_cb->ipv6_addtl_opts |= cpu_to_le16(
2048                                         IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
2049                 else if (iface_param->value[0] ==
2050                          ISCSI_IPV6_LINKLOCAL_AUTOCFG_DISABLE)
2051                         init_fw_cb->ipv6_addtl_opts &= cpu_to_le16(
2052                                        ~IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
2053                 else
2054                         ql4_printk(KERN_ERR, ha,
2055                                    "Invalid autocfg setting for IPv6 linklocal addr\n");
2056                 break;
2057         case ISCSI_NET_PARAM_IPV6_ROUTER_AUTOCFG:
2058                 /* Autocfg applies to even interface */
2059                 if (iface_param->iface_num & 0x1)
2060                         break;
2061 
2062                 if (iface_param->value[0] == ISCSI_IPV6_ROUTER_AUTOCFG_ENABLE)
2063                         memset(init_fw_cb->ipv6_dflt_rtr_addr, 0,
2064                                sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
2065                 break;
2066         case ISCSI_NET_PARAM_IFACE_ENABLE:
2067                 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
2068                         init_fw_cb->ipv6_opts |=
2069                                 cpu_to_le16(IPV6_OPT_IPV6_PROTOCOL_ENABLE);
2070                         qla4xxx_create_ipv6_iface(ha);
2071                 } else {
2072                         init_fw_cb->ipv6_opts &=
2073                                 cpu_to_le16(~IPV6_OPT_IPV6_PROTOCOL_ENABLE &
2074                                             0xFFFF);
2075                         qla4xxx_destroy_ipv6_iface(ha);
2076                 }
2077                 break;
2078         case ISCSI_NET_PARAM_VLAN_TAG:
2079                 if (iface_param->len != sizeof(init_fw_cb->ipv6_vlan_tag))
2080                         break;
2081                 init_fw_cb->ipv6_vlan_tag =
2082                                 cpu_to_be16(*(uint16_t *)iface_param->value);
2083                 break;
2084         case ISCSI_NET_PARAM_VLAN_ENABLED:
2085                 if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
2086                         init_fw_cb->ipv6_opts |=
2087                                 cpu_to_le16(IPV6_OPT_VLAN_TAGGING_ENABLE);
2088                 else
2089                         init_fw_cb->ipv6_opts &=
2090                                 cpu_to_le16(~IPV6_OPT_VLAN_TAGGING_ENABLE);
2091                 break;
2092         case ISCSI_NET_PARAM_MTU:
2093                 init_fw_cb->eth_mtu_size =
2094                                 cpu_to_le16(*(uint16_t *)iface_param->value);
2095                 break;
2096         case ISCSI_NET_PARAM_PORT:
2097                 /* Autocfg applies to even interface */
2098                 if (iface_param->iface_num & 0x1)
2099                         break;
2100 
2101                 init_fw_cb->ipv6_port =
2102                                 cpu_to_le16(*(uint16_t *)iface_param->value);
2103                 break;
2104         case ISCSI_NET_PARAM_DELAYED_ACK_EN:
2105                 if (iface_param->iface_num & 0x1)
2106                         break;
2107                 if (iface_param->value[0] == ISCSI_NET_PARAM_DISABLE)
2108                         init_fw_cb->ipv6_tcp_opts |=
2109                                 cpu_to_le16(IPV6_TCPOPT_DELAYED_ACK_DISABLE);
2110                 else
2111                         init_fw_cb->ipv6_tcp_opts &=
2112                                 cpu_to_le16(~IPV6_TCPOPT_DELAYED_ACK_DISABLE &
2113                                             0xFFFF);
2114                 break;
2115         case ISCSI_NET_PARAM_TCP_NAGLE_DISABLE:
2116                 if (iface_param->iface_num & 0x1)
2117                         break;
2118                 if (iface_param->value[0] == ISCSI_NET_PARAM_DISABLE)
2119                         init_fw_cb->ipv6_tcp_opts |=
2120                                 cpu_to_le16(IPV6_TCPOPT_NAGLE_ALGO_DISABLE);
2121                 else
2122                         init_fw_cb->ipv6_tcp_opts &=
2123                                 cpu_to_le16(~IPV6_TCPOPT_NAGLE_ALGO_DISABLE);
2124                 break;
2125         case ISCSI_NET_PARAM_TCP_WSF_DISABLE:
2126                 if (iface_param->iface_num & 0x1)
2127                         break;
2128                 if (iface_param->value[0] == ISCSI_NET_PARAM_DISABLE)
2129                         init_fw_cb->ipv6_tcp_opts |=
2130                                 cpu_to_le16(IPV6_TCPOPT_WINDOW_SCALE_DISABLE);
2131                 else
2132                         init_fw_cb->ipv6_tcp_opts &=
2133                                 cpu_to_le16(~IPV6_TCPOPT_WINDOW_SCALE_DISABLE);
2134                 break;
2135         case ISCSI_NET_PARAM_TCP_WSF:
2136                 if (iface_param->iface_num & 0x1)
2137                         break;
2138                 init_fw_cb->ipv6_tcp_wsf = iface_param->value[0];
2139                 break;
2140         case ISCSI_NET_PARAM_TCP_TIMER_SCALE:
2141                 if (iface_param->iface_num & 0x1)
2142                         break;
2143                 init_fw_cb->ipv6_tcp_opts &=
2144                                         cpu_to_le16(~IPV6_TCPOPT_TIMER_SCALE);
2145                 init_fw_cb->ipv6_tcp_opts |=
2146                                 cpu_to_le16((iface_param->value[0] << 1) &
2147                                             IPV6_TCPOPT_TIMER_SCALE);
2148                 break;
2149         case ISCSI_NET_PARAM_TCP_TIMESTAMP_EN:
2150                 if (iface_param->iface_num & 0x1)
2151                         break;
2152                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2153                         init_fw_cb->ipv6_tcp_opts |=
2154                                 cpu_to_le16(IPV6_TCPOPT_TIMESTAMP_EN);
2155                 else
2156                         init_fw_cb->ipv6_tcp_opts &=
2157                                 cpu_to_le16(~IPV6_TCPOPT_TIMESTAMP_EN);
2158                 break;
2159         case ISCSI_NET_PARAM_IPV6_GRAT_NEIGHBOR_ADV_EN:
2160                 if (iface_param->iface_num & 0x1)
2161                         break;
2162                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2163                         init_fw_cb->ipv6_opts |=
2164                                 cpu_to_le16(IPV6_OPT_GRAT_NEIGHBOR_ADV_EN);
2165                 else
2166                         init_fw_cb->ipv6_opts &=
2167                                 cpu_to_le16(~IPV6_OPT_GRAT_NEIGHBOR_ADV_EN);
2168                 break;
2169         case ISCSI_NET_PARAM_REDIRECT_EN:
2170                 if (iface_param->iface_num & 0x1)
2171                         break;
2172                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2173                         init_fw_cb->ipv6_opts |=
2174                                 cpu_to_le16(IPV6_OPT_REDIRECT_EN);
2175                 else
2176                         init_fw_cb->ipv6_opts &=
2177                                 cpu_to_le16(~IPV6_OPT_REDIRECT_EN);
2178                 break;
2179         case ISCSI_NET_PARAM_IPV6_MLD_EN:
2180                 if (iface_param->iface_num & 0x1)
2181                         break;
2182                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2183                         init_fw_cb->ipv6_addtl_opts |=
2184                                 cpu_to_le16(IPV6_ADDOPT_MLD_EN);
2185                 else
2186                         init_fw_cb->ipv6_addtl_opts &=
2187                                 cpu_to_le16(~IPV6_ADDOPT_MLD_EN);
2188                 break;
2189         case ISCSI_NET_PARAM_IPV6_FLOW_LABEL:
2190                 if (iface_param->iface_num & 0x1)
2191                         break;
2192                 init_fw_cb->ipv6_flow_lbl =
2193                                 cpu_to_le16(*(uint16_t *)iface_param->value);
2194                 break;
2195         case ISCSI_NET_PARAM_IPV6_TRAFFIC_CLASS:
2196                 if (iface_param->iface_num & 0x1)
2197                         break;
2198                 init_fw_cb->ipv6_traffic_class = iface_param->value[0];
2199                 break;
2200         case ISCSI_NET_PARAM_IPV6_HOP_LIMIT:
2201                 if (iface_param->iface_num & 0x1)
2202                         break;
2203                 init_fw_cb->ipv6_hop_limit = iface_param->value[0];
2204                 break;
2205         case ISCSI_NET_PARAM_IPV6_ND_REACHABLE_TMO:
2206                 if (iface_param->iface_num & 0x1)
2207                         break;
2208                 init_fw_cb->ipv6_nd_reach_time =
2209                                 cpu_to_le32(*(uint32_t *)iface_param->value);
2210                 break;
2211         case ISCSI_NET_PARAM_IPV6_ND_REXMIT_TIME:
2212                 if (iface_param->iface_num & 0x1)
2213                         break;
2214                 init_fw_cb->ipv6_nd_rexmit_timer =
2215                                 cpu_to_le32(*(uint32_t *)iface_param->value);
2216                 break;
2217         case ISCSI_NET_PARAM_IPV6_ND_STALE_TMO:
2218                 if (iface_param->iface_num & 0x1)
2219                         break;
2220                 init_fw_cb->ipv6_nd_stale_timeout =
2221                                 cpu_to_le32(*(uint32_t *)iface_param->value);
2222                 break;
2223         case ISCSI_NET_PARAM_IPV6_DUP_ADDR_DETECT_CNT:
2224                 if (iface_param->iface_num & 0x1)
2225                         break;
2226                 init_fw_cb->ipv6_dup_addr_detect_count = iface_param->value[0];
2227                 break;
2228         case ISCSI_NET_PARAM_IPV6_RTR_ADV_LINK_MTU:
2229                 if (iface_param->iface_num & 0x1)
2230                         break;
2231                 init_fw_cb->ipv6_gw_advrt_mtu =
2232                                 cpu_to_le32(*(uint32_t *)iface_param->value);
2233                 break;
2234         default:
2235                 ql4_printk(KERN_ERR, ha, "Unknown IPv6 param = %d\n",
2236                            iface_param->param);
2237                 break;
2238         }
2239 }
2240 
2241 static void qla4xxx_set_ipv4(struct scsi_qla_host *ha,
2242                              struct iscsi_iface_param_info *iface_param,
2243                              struct addr_ctrl_blk *init_fw_cb)
2244 {
2245         switch (iface_param->param) {
2246         case ISCSI_NET_PARAM_IPV4_ADDR:
2247                 memcpy(init_fw_cb->ipv4_addr, iface_param->value,
2248                        sizeof(init_fw_cb->ipv4_addr));
2249                 break;
2250         case ISCSI_NET_PARAM_IPV4_SUBNET:
2251                 memcpy(init_fw_cb->ipv4_subnet, iface_param->value,
2252                        sizeof(init_fw_cb->ipv4_subnet));
2253                 break;
2254         case ISCSI_NET_PARAM_IPV4_GW:
2255                 memcpy(init_fw_cb->ipv4_gw_addr, iface_param->value,
2256                        sizeof(init_fw_cb->ipv4_gw_addr));
2257                 break;
2258         case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
2259                 if (iface_param->value[0] == ISCSI_BOOTPROTO_DHCP)
2260                         init_fw_cb->ipv4_tcp_opts |=
2261                                         cpu_to_le16(TCPOPT_DHCP_ENABLE);
2262                 else if (iface_param->value[0] == ISCSI_BOOTPROTO_STATIC)
2263                         init_fw_cb->ipv4_tcp_opts &=
2264                                         cpu_to_le16(~TCPOPT_DHCP_ENABLE);
2265                 else
2266                         ql4_printk(KERN_ERR, ha, "Invalid IPv4 bootproto\n");
2267                 break;
2268         case ISCSI_NET_PARAM_IFACE_ENABLE:
2269                 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
2270                         init_fw_cb->ipv4_ip_opts |=
2271                                 cpu_to_le16(IPOPT_IPV4_PROTOCOL_ENABLE);
2272                         qla4xxx_create_ipv4_iface(ha);
2273                 } else {
2274                         init_fw_cb->ipv4_ip_opts &=
2275                                 cpu_to_le16(~IPOPT_IPV4_PROTOCOL_ENABLE &
2276                                             0xFFFF);
2277                         qla4xxx_destroy_ipv4_iface(ha);
2278                 }
2279                 break;
2280         case ISCSI_NET_PARAM_VLAN_TAG:
2281                 if (iface_param->len != sizeof(init_fw_cb->ipv4_vlan_tag))
2282                         break;
2283                 init_fw_cb->ipv4_vlan_tag =
2284                                 cpu_to_be16(*(uint16_t *)iface_param->value);
2285                 break;
2286         case ISCSI_NET_PARAM_VLAN_ENABLED:
2287                 if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
2288                         init_fw_cb->ipv4_ip_opts |=
2289                                         cpu_to_le16(IPOPT_VLAN_TAGGING_ENABLE);
2290                 else
2291                         init_fw_cb->ipv4_ip_opts &=
2292                                         cpu_to_le16(~IPOPT_VLAN_TAGGING_ENABLE);
2293                 break;
2294         case ISCSI_NET_PARAM_MTU:
2295                 init_fw_cb->eth_mtu_size =
2296                                 cpu_to_le16(*(uint16_t *)iface_param->value);
2297                 break;
2298         case ISCSI_NET_PARAM_PORT:
2299                 init_fw_cb->ipv4_port =
2300                                 cpu_to_le16(*(uint16_t *)iface_param->value);
2301                 break;
2302         case ISCSI_NET_PARAM_DELAYED_ACK_EN:
2303                 if (iface_param->iface_num & 0x1)
2304                         break;
2305                 if (iface_param->value[0] == ISCSI_NET_PARAM_DISABLE)
2306                         init_fw_cb->ipv4_tcp_opts |=
2307                                 cpu_to_le16(TCPOPT_DELAYED_ACK_DISABLE);
2308                 else
2309                         init_fw_cb->ipv4_tcp_opts &=
2310                                 cpu_to_le16(~TCPOPT_DELAYED_ACK_DISABLE &
2311                                             0xFFFF);
2312                 break;
2313         case ISCSI_NET_PARAM_TCP_NAGLE_DISABLE:
2314                 if (iface_param->iface_num & 0x1)
2315                         break;
2316                 if (iface_param->value[0] == ISCSI_NET_PARAM_DISABLE)
2317                         init_fw_cb->ipv4_tcp_opts |=
2318                                 cpu_to_le16(TCPOPT_NAGLE_ALGO_DISABLE);
2319                 else
2320                         init_fw_cb->ipv4_tcp_opts &=
2321                                 cpu_to_le16(~TCPOPT_NAGLE_ALGO_DISABLE);
2322                 break;
2323         case ISCSI_NET_PARAM_TCP_WSF_DISABLE:
2324                 if (iface_param->iface_num & 0x1)
2325                         break;
2326                 if (iface_param->value[0] == ISCSI_NET_PARAM_DISABLE)
2327                         init_fw_cb->ipv4_tcp_opts |=
2328                                 cpu_to_le16(TCPOPT_WINDOW_SCALE_DISABLE);
2329                 else
2330                         init_fw_cb->ipv4_tcp_opts &=
2331                                 cpu_to_le16(~TCPOPT_WINDOW_SCALE_DISABLE);
2332                 break;
2333         case ISCSI_NET_PARAM_TCP_WSF:
2334                 if (iface_param->iface_num & 0x1)
2335                         break;
2336                 init_fw_cb->ipv4_tcp_wsf = iface_param->value[0];
2337                 break;
2338         case ISCSI_NET_PARAM_TCP_TIMER_SCALE:
2339                 if (iface_param->iface_num & 0x1)
2340                         break;
2341                 init_fw_cb->ipv4_tcp_opts &= cpu_to_le16(~TCPOPT_TIMER_SCALE);
2342                 init_fw_cb->ipv4_tcp_opts |=
2343                                 cpu_to_le16((iface_param->value[0] << 1) &
2344                                             TCPOPT_TIMER_SCALE);
2345                 break;
2346         case ISCSI_NET_PARAM_TCP_TIMESTAMP_EN:
2347                 if (iface_param->iface_num & 0x1)
2348                         break;
2349                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2350                         init_fw_cb->ipv4_tcp_opts |=
2351                                 cpu_to_le16(TCPOPT_TIMESTAMP_ENABLE);
2352                 else
2353                         init_fw_cb->ipv4_tcp_opts &=
2354                                 cpu_to_le16(~TCPOPT_TIMESTAMP_ENABLE);
2355                 break;
2356         case ISCSI_NET_PARAM_IPV4_DHCP_DNS_ADDR_EN:
2357                 if (iface_param->iface_num & 0x1)
2358                         break;
2359                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2360                         init_fw_cb->ipv4_tcp_opts |=
2361                                 cpu_to_le16(TCPOPT_DNS_SERVER_IP_EN);
2362                 else
2363                         init_fw_cb->ipv4_tcp_opts &=
2364                                 cpu_to_le16(~TCPOPT_DNS_SERVER_IP_EN);
2365                 break;
2366         case ISCSI_NET_PARAM_IPV4_DHCP_SLP_DA_EN:
2367                 if (iface_param->iface_num & 0x1)
2368                         break;
2369                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2370                         init_fw_cb->ipv4_tcp_opts |=
2371                                 cpu_to_le16(TCPOPT_SLP_DA_INFO_EN);
2372                 else
2373                         init_fw_cb->ipv4_tcp_opts &=
2374                                 cpu_to_le16(~TCPOPT_SLP_DA_INFO_EN);
2375                 break;
2376         case ISCSI_NET_PARAM_IPV4_TOS_EN:
2377                 if (iface_param->iface_num & 0x1)
2378                         break;
2379                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2380                         init_fw_cb->ipv4_ip_opts |=
2381                                 cpu_to_le16(IPOPT_IPV4_TOS_EN);
2382                 else
2383                         init_fw_cb->ipv4_ip_opts &=
2384                                 cpu_to_le16(~IPOPT_IPV4_TOS_EN);
2385                 break;
2386         case ISCSI_NET_PARAM_IPV4_TOS:
2387                 if (iface_param->iface_num & 0x1)
2388                         break;
2389                 init_fw_cb->ipv4_tos = iface_param->value[0];
2390                 break;
2391         case ISCSI_NET_PARAM_IPV4_GRAT_ARP_EN:
2392                 if (iface_param->iface_num & 0x1)
2393                         break;
2394                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2395                         init_fw_cb->ipv4_ip_opts |=
2396                                         cpu_to_le16(IPOPT_GRAT_ARP_EN);
2397                 else
2398                         init_fw_cb->ipv4_ip_opts &=
2399                                         cpu_to_le16(~IPOPT_GRAT_ARP_EN);
2400                 break;
2401         case ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID_EN:
2402                 if (iface_param->iface_num & 0x1)
2403                         break;
2404                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2405                         init_fw_cb->ipv4_ip_opts |=
2406                                 cpu_to_le16(IPOPT_ALT_CID_EN);
2407                 else
2408                         init_fw_cb->ipv4_ip_opts &=
2409                                 cpu_to_le16(~IPOPT_ALT_CID_EN);
2410                 break;
2411         case ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID:
2412                 if (iface_param->iface_num & 0x1)
2413                         break;
2414                 memcpy(init_fw_cb->ipv4_dhcp_alt_cid, iface_param->value,
2415                        (sizeof(init_fw_cb->ipv4_dhcp_alt_cid) - 1));
2416                 init_fw_cb->ipv4_dhcp_alt_cid_len =
2417                                         strlen(init_fw_cb->ipv4_dhcp_alt_cid);
2418                 break;
2419         case ISCSI_NET_PARAM_IPV4_DHCP_REQ_VENDOR_ID_EN:
2420                 if (iface_param->iface_num & 0x1)
2421                         break;
2422                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2423                         init_fw_cb->ipv4_ip_opts |=
2424                                         cpu_to_le16(IPOPT_REQ_VID_EN);
2425                 else
2426                         init_fw_cb->ipv4_ip_opts &=
2427                                         cpu_to_le16(~IPOPT_REQ_VID_EN);
2428                 break;
2429         case ISCSI_NET_PARAM_IPV4_DHCP_USE_VENDOR_ID_EN:
2430                 if (iface_param->iface_num & 0x1)
2431                         break;
2432                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2433                         init_fw_cb->ipv4_ip_opts |=
2434                                         cpu_to_le16(IPOPT_USE_VID_EN);
2435                 else
2436                         init_fw_cb->ipv4_ip_opts &=
2437                                         cpu_to_le16(~IPOPT_USE_VID_EN);
2438                 break;
2439         case ISCSI_NET_PARAM_IPV4_DHCP_VENDOR_ID:
2440                 if (iface_param->iface_num & 0x1)
2441                         break;
2442                 memcpy(init_fw_cb->ipv4_dhcp_vid, iface_param->value,
2443                        (sizeof(init_fw_cb->ipv4_dhcp_vid) - 1));
2444                 init_fw_cb->ipv4_dhcp_vid_len =
2445                                         strlen(init_fw_cb->ipv4_dhcp_vid);
2446                 break;
2447         case ISCSI_NET_PARAM_IPV4_DHCP_LEARN_IQN_EN:
2448                 if (iface_param->iface_num & 0x1)
2449                         break;
2450                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2451                         init_fw_cb->ipv4_ip_opts |=
2452                                         cpu_to_le16(IPOPT_LEARN_IQN_EN);
2453                 else
2454                         init_fw_cb->ipv4_ip_opts &=
2455                                         cpu_to_le16(~IPOPT_LEARN_IQN_EN);
2456                 break;
2457         case ISCSI_NET_PARAM_IPV4_FRAGMENT_DISABLE:
2458                 if (iface_param->iface_num & 0x1)
2459                         break;
2460                 if (iface_param->value[0] == ISCSI_NET_PARAM_DISABLE)
2461                         init_fw_cb->ipv4_ip_opts |=
2462                                 cpu_to_le16(IPOPT_FRAGMENTATION_DISABLE);
2463                 else
2464                         init_fw_cb->ipv4_ip_opts &=
2465                                 cpu_to_le16(~IPOPT_FRAGMENTATION_DISABLE);
2466                 break;
2467         case ISCSI_NET_PARAM_IPV4_IN_FORWARD_EN:
2468                 if (iface_param->iface_num & 0x1)
2469                         break;
2470                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2471                         init_fw_cb->ipv4_ip_opts |=
2472                                 cpu_to_le16(IPOPT_IN_FORWARD_EN);
2473                 else
2474                         init_fw_cb->ipv4_ip_opts &=
2475                                 cpu_to_le16(~IPOPT_IN_FORWARD_EN);
2476                 break;
2477         case ISCSI_NET_PARAM_REDIRECT_EN:
2478                 if (iface_param->iface_num & 0x1)
2479                         break;
2480                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2481                         init_fw_cb->ipv4_ip_opts |=
2482                                 cpu_to_le16(IPOPT_ARP_REDIRECT_EN);
2483                 else
2484                         init_fw_cb->ipv4_ip_opts &=
2485                                 cpu_to_le16(~IPOPT_ARP_REDIRECT_EN);
2486                 break;
2487         case ISCSI_NET_PARAM_IPV4_TTL:
2488                 if (iface_param->iface_num & 0x1)
2489                         break;
2490                 init_fw_cb->ipv4_ttl = iface_param->value[0];
2491                 break;
2492         default:
2493                 ql4_printk(KERN_ERR, ha, "Unknown IPv4 param = %d\n",
2494                            iface_param->param);
2495                 break;
2496         }
2497 }
2498 
2499 static void qla4xxx_set_iscsi_param(struct scsi_qla_host *ha,
2500                                     struct iscsi_iface_param_info *iface_param,
2501                                     struct addr_ctrl_blk *init_fw_cb)
2502 {
2503         switch (iface_param->param) {
2504         case ISCSI_IFACE_PARAM_DEF_TASKMGMT_TMO:
2505                 if (iface_param->iface_num & 0x1)
2506                         break;
2507                 init_fw_cb->def_timeout =
2508                                 cpu_to_le16(*(uint16_t *)iface_param->value);
2509                 break;
2510         case ISCSI_IFACE_PARAM_HDRDGST_EN:
2511                 if (iface_param->iface_num & 0x1)
2512                         break;
2513                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2514                         init_fw_cb->iscsi_opts |=
2515                                 cpu_to_le16(ISCSIOPTS_HEADER_DIGEST_EN);
2516                 else
2517                         init_fw_cb->iscsi_opts &=
2518                                 cpu_to_le16(~ISCSIOPTS_HEADER_DIGEST_EN);
2519                 break;
2520         case ISCSI_IFACE_PARAM_DATADGST_EN:
2521                 if (iface_param->iface_num & 0x1)
2522                         break;
2523                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2524                         init_fw_cb->iscsi_opts |=
2525                                 cpu_to_le16(ISCSIOPTS_DATA_DIGEST_EN);
2526                 else
2527                         init_fw_cb->iscsi_opts &=
2528                                 cpu_to_le16(~ISCSIOPTS_DATA_DIGEST_EN);
2529                 break;
2530         case ISCSI_IFACE_PARAM_IMM_DATA_EN:
2531                 if (iface_param->iface_num & 0x1)
2532                         break;
2533                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2534                         init_fw_cb->iscsi_opts |=
2535                                 cpu_to_le16(ISCSIOPTS_IMMEDIATE_DATA_EN);
2536                 else
2537                         init_fw_cb->iscsi_opts &=
2538                                 cpu_to_le16(~ISCSIOPTS_IMMEDIATE_DATA_EN);
2539                 break;
2540         case ISCSI_IFACE_PARAM_INITIAL_R2T_EN:
2541                 if (iface_param->iface_num & 0x1)
2542                         break;
2543                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2544                         init_fw_cb->iscsi_opts |=
2545                                 cpu_to_le16(ISCSIOPTS_INITIAL_R2T_EN);
2546                 else
2547                         init_fw_cb->iscsi_opts &=
2548                                 cpu_to_le16(~ISCSIOPTS_INITIAL_R2T_EN);
2549                 break;
2550         case ISCSI_IFACE_PARAM_DATASEQ_INORDER_EN:
2551                 if (iface_param->iface_num & 0x1)
2552                         break;
2553                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2554                         init_fw_cb->iscsi_opts |=
2555                                 cpu_to_le16(ISCSIOPTS_DATA_SEQ_INORDER_EN);
2556                 else
2557                         init_fw_cb->iscsi_opts &=
2558                                 cpu_to_le16(~ISCSIOPTS_DATA_SEQ_INORDER_EN);
2559                 break;
2560         case ISCSI_IFACE_PARAM_PDU_INORDER_EN:
2561                 if (iface_param->iface_num & 0x1)
2562                         break;
2563                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2564                         init_fw_cb->iscsi_opts |=
2565                                 cpu_to_le16(ISCSIOPTS_DATA_PDU_INORDER_EN);
2566                 else
2567                         init_fw_cb->iscsi_opts &=
2568                                 cpu_to_le16(~ISCSIOPTS_DATA_PDU_INORDER_EN);
2569                 break;
2570         case ISCSI_IFACE_PARAM_ERL:
2571                 if (iface_param->iface_num & 0x1)
2572                         break;
2573                 init_fw_cb->iscsi_opts &= cpu_to_le16(~ISCSIOPTS_ERL);
2574                 init_fw_cb->iscsi_opts |= cpu_to_le16(iface_param->value[0] &
2575                                                       ISCSIOPTS_ERL);
2576                 break;
2577         case ISCSI_IFACE_PARAM_MAX_RECV_DLENGTH:
2578                 if (iface_param->iface_num & 0x1)
2579                         break;
2580                 init_fw_cb->iscsi_max_pdu_size =
2581                                 cpu_to_le32(*(uint32_t *)iface_param->value) /
2582                                 BYTE_UNITS;
2583                 break;
2584         case ISCSI_IFACE_PARAM_FIRST_BURST:
2585                 if (iface_param->iface_num & 0x1)
2586                         break;
2587                 init_fw_cb->iscsi_fburst_len =
2588                                 cpu_to_le32(*(uint32_t *)iface_param->value) /
2589                                 BYTE_UNITS;
2590                 break;
2591         case ISCSI_IFACE_PARAM_MAX_R2T:
2592                 if (iface_param->iface_num & 0x1)
2593                         break;
2594                 init_fw_cb->iscsi_max_outstnd_r2t =
2595                                 cpu_to_le16(*(uint16_t *)iface_param->value);
2596                 break;
2597         case ISCSI_IFACE_PARAM_MAX_BURST:
2598                 if (iface_param->iface_num & 0x1)
2599                         break;
2600                 init_fw_cb->iscsi_max_burst_len =
2601                                 cpu_to_le32(*(uint32_t *)iface_param->value) /
2602                                 BYTE_UNITS;
2603                 break;
2604         case ISCSI_IFACE_PARAM_CHAP_AUTH_EN:
2605                 if (iface_param->iface_num & 0x1)
2606                         break;
2607                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2608                         init_fw_cb->iscsi_opts |=
2609                                 cpu_to_le16(ISCSIOPTS_CHAP_AUTH_EN);
2610                 else
2611                         init_fw_cb->iscsi_opts &=
2612                                 cpu_to_le16(~ISCSIOPTS_CHAP_AUTH_EN);
2613                 break;
2614         case ISCSI_IFACE_PARAM_BIDI_CHAP_EN:
2615                 if (iface_param->iface_num & 0x1)
2616                         break;
2617                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2618                         init_fw_cb->iscsi_opts |=
2619                                 cpu_to_le16(ISCSIOPTS_BIDI_CHAP_EN);
2620                 else
2621                         init_fw_cb->iscsi_opts &=
2622                                 cpu_to_le16(~ISCSIOPTS_BIDI_CHAP_EN);
2623                 break;
2624         case ISCSI_IFACE_PARAM_DISCOVERY_AUTH_OPTIONAL:
2625                 if (iface_param->iface_num & 0x1)
2626                         break;
2627                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2628                         init_fw_cb->iscsi_opts |=
2629                                 cpu_to_le16(ISCSIOPTS_DISCOVERY_AUTH_EN);
2630                 else
2631                         init_fw_cb->iscsi_opts &=
2632                                 cpu_to_le16(~ISCSIOPTS_DISCOVERY_AUTH_EN);
2633                 break;
2634         case ISCSI_IFACE_PARAM_DISCOVERY_LOGOUT_EN:
2635                 if (iface_param->iface_num & 0x1)
2636                         break;
2637                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2638                         init_fw_cb->iscsi_opts |=
2639                                 cpu_to_le16(ISCSIOPTS_DISCOVERY_LOGOUT_EN);
2640                 else
2641                         init_fw_cb->iscsi_opts &=
2642                                 cpu_to_le16(~ISCSIOPTS_DISCOVERY_LOGOUT_EN);
2643                 break;
2644         case ISCSI_IFACE_PARAM_STRICT_LOGIN_COMP_EN:
2645                 if (iface_param->iface_num & 0x1)
2646                         break;
2647                 if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2648                         init_fw_cb->iscsi_opts |=
2649                                 cpu_to_le16(ISCSIOPTS_STRICT_LOGIN_COMP_EN);
2650                 else
2651                         init_fw_cb->iscsi_opts &=
2652                                 cpu_to_le16(~ISCSIOPTS_STRICT_LOGIN_COMP_EN);
2653                 break;
2654         default:
2655                 ql4_printk(KERN_ERR, ha, "Unknown iscsi param = %d\n",
2656                            iface_param->param);
2657                 break;
2658         }
2659 }
2660 
2661 static void
2662 qla4xxx_initcb_to_acb(struct addr_ctrl_blk *init_fw_cb)
2663 {
2664         struct addr_ctrl_blk_def *acb;
2665         acb = (struct addr_ctrl_blk_def *)init_fw_cb;
2666         memset(acb->reserved1, 0, sizeof(acb->reserved1));
2667         memset(acb->reserved2, 0, sizeof(acb->reserved2));
2668         memset(acb->reserved3, 0, sizeof(acb->reserved3));
2669         memset(acb->reserved4, 0, sizeof(acb->reserved4));
2670         memset(acb->reserved5, 0, sizeof(acb->reserved5));
2671         memset(acb->reserved6, 0, sizeof(acb->reserved6));
2672         memset(acb->reserved7, 0, sizeof(acb->reserved7));
2673         memset(acb->reserved8, 0, sizeof(acb->reserved8));
2674         memset(acb->reserved9, 0, sizeof(acb->reserved9));
2675         memset(acb->reserved10, 0, sizeof(acb->reserved10));
2676         memset(acb->reserved11, 0, sizeof(acb->reserved11));
2677         memset(acb->reserved12, 0, sizeof(acb->reserved12));
2678         memset(acb->reserved13, 0, sizeof(acb->reserved13));
2679         memset(acb->reserved14, 0, sizeof(acb->reserved14));
2680         memset(acb->reserved15, 0, sizeof(acb->reserved15));
2681 }
2682 
2683 static int
2684 qla4xxx_iface_set_param(struct Scsi_Host *shost, void *data, uint32_t len)
2685 {
2686         struct scsi_qla_host *ha = to_qla_host(shost);
2687         int rval = 0;
2688         struct iscsi_iface_param_info *iface_param = NULL;
2689         struct addr_ctrl_blk *init_fw_cb = NULL;
2690         dma_addr_t init_fw_cb_dma;
2691         uint32_t mbox_cmd[MBOX_REG_COUNT];
2692         uint32_t mbox_sts[MBOX_REG_COUNT];
2693         uint32_t rem = len;
2694         struct nlattr *attr;
2695 
2696         init_fw_cb = dma_alloc_coherent(&ha->pdev->dev,
2697                                         sizeof(struct addr_ctrl_blk),
2698                                         &init_fw_cb_dma, GFP_KERNEL);
2699         if (!init_fw_cb) {
2700                 ql4_printk(KERN_ERR, ha, "%s: Unable to alloc init_cb\n",
2701                            __func__);
2702                 return -ENOMEM;
2703         }
2704 
2705         memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
2706         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
2707         memset(&mbox_sts, 0, sizeof(mbox_sts));
2708 
2709         if (qla4xxx_get_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma)) {
2710                 ql4_printk(KERN_ERR, ha, "%s: get ifcb failed\n", __func__);
2711                 rval = -EIO;
2712                 goto exit_init_fw_cb;
2713         }
2714 
2715         nla_for_each_attr(attr, data, len, rem) {
2716                 iface_param = nla_data(attr);
2717 
2718                 if (iface_param->param_type == ISCSI_NET_PARAM) {
2719                         switch (iface_param->iface_type) {
2720                         case ISCSI_IFACE_TYPE_IPV4:
2721                                 switch (iface_param->iface_num) {
2722                                 case 0:
2723                                         qla4xxx_set_ipv4(ha, iface_param,
2724                                                          init_fw_cb);
2725                                         break;
2726                                 default:
2727                                 /* Cannot have more than one IPv4 interface */
2728                                         ql4_printk(KERN_ERR, ha,
2729                                                    "Invalid IPv4 iface number = %d\n",
2730                                                    iface_param->iface_num);
2731                                         break;
2732                                 }
2733                                 break;
2734                         case ISCSI_IFACE_TYPE_IPV6:
2735                                 switch (iface_param->iface_num) {
2736                                 case 0:
2737                                 case 1:
2738                                         qla4xxx_set_ipv6(ha, iface_param,
2739                                                          init_fw_cb);
2740                                         break;
2741                                 default:
2742                                 /* Cannot have more than two IPv6 interface */
2743                                         ql4_printk(KERN_ERR, ha,
2744                                                    "Invalid IPv6 iface number = %d\n",
2745                                                    iface_param->iface_num);
2746                                         break;
2747                                 }
2748                                 break;
2749                         default:
2750                                 ql4_printk(KERN_ERR, ha,
2751                                            "Invalid iface type\n");
2752                                 break;
2753                         }
2754                 } else if (iface_param->param_type == ISCSI_IFACE_PARAM) {
2755                                 qla4xxx_set_iscsi_param(ha, iface_param,
2756                                                         init_fw_cb);
2757                 } else {
2758                         continue;
2759                 }
2760         }
2761 
2762         init_fw_cb->cookie = cpu_to_le32(0x11BEAD5A);
2763 
2764         rval = qla4xxx_set_flash(ha, init_fw_cb_dma, FLASH_SEGMENT_IFCB,
2765                                  sizeof(struct addr_ctrl_blk),
2766                                  FLASH_OPT_RMW_COMMIT);
2767         if (rval != QLA_SUCCESS) {
2768                 ql4_printk(KERN_ERR, ha, "%s: set flash mbx failed\n",
2769                            __func__);
2770                 rval = -EIO;
2771                 goto exit_init_fw_cb;
2772         }
2773 
2774         rval = qla4xxx_disable_acb(ha);
2775         if (rval != QLA_SUCCESS) {
2776                 ql4_printk(KERN_ERR, ha, "%s: disable acb mbx failed\n",
2777                            __func__);
2778                 rval = -EIO;
2779                 goto exit_init_fw_cb;
2780         }
2781 
2782         wait_for_completion_timeout(&ha->disable_acb_comp,
2783                                     DISABLE_ACB_TOV * HZ);
2784 
2785         qla4xxx_initcb_to_acb(init_fw_cb);
2786 
2787         rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma);
2788         if (rval != QLA_SUCCESS) {
2789                 ql4_printk(KERN_ERR, ha, "%s: set acb mbx failed\n",
2790                            __func__);
2791                 rval = -EIO;
2792                 goto exit_init_fw_cb;
2793         }
2794 
2795         memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
2796         qla4xxx_update_local_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb,
2797                                   init_fw_cb_dma);
2798 
2799 exit_init_fw_cb:
2800         dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk),
2801                           init_fw_cb, init_fw_cb_dma);
2802 
2803         return rval;
2804 }
2805 
2806 static int qla4xxx_session_get_param(struct iscsi_cls_session *cls_sess,
2807                                      enum iscsi_param param, char *buf)
2808 {
2809         struct iscsi_session *sess = cls_sess->dd_data;
2810         struct ddb_entry *ddb_entry = sess->dd_data;
2811         struct scsi_qla_host *ha = ddb_entry->ha;
2812         struct iscsi_cls_conn *cls_conn = ddb_entry->conn;
2813         struct ql4_chap_table chap_tbl;
2814         int rval, len;
2815         uint16_t idx;
2816 
2817         memset(&chap_tbl, 0, sizeof(chap_tbl));
2818         switch (param) {
2819         case ISCSI_PARAM_CHAP_IN_IDX:
2820                 rval = qla4xxx_get_chap_index(ha, sess->username_in,
2821                                               sess->password_in, BIDI_CHAP,
2822                                               &idx);
2823                 if (rval)
2824                         len = sprintf(buf, "\n");
2825                 else
2826                         len = sprintf(buf, "%hu\n", idx);
2827                 break;
2828         case ISCSI_PARAM_CHAP_OUT_IDX:
2829                 if (ddb_entry->ddb_type == FLASH_DDB) {
2830                         if (ddb_entry->chap_tbl_idx != INVALID_ENTRY) {
2831                                 idx = ddb_entry->chap_tbl_idx;
2832                                 rval = QLA_SUCCESS;
2833                         } else {
2834                                 rval = QLA_ERROR;
2835                         }
2836                 } else {
2837                         rval = qla4xxx_get_chap_index(ha, sess->username,
2838                                                       sess->password,
2839                                                       LOCAL_CHAP, &idx);
2840                 }
2841                 if (rval)
2842                         len = sprintf(buf, "\n");
2843                 else
2844                         len = sprintf(buf, "%hu\n", idx);
2845                 break;
2846         case ISCSI_PARAM_USERNAME:
2847         case ISCSI_PARAM_PASSWORD:
2848                 /* First, populate session username and password for FLASH DDB,
2849                  * if not already done. This happens when session login fails
2850                  * for a FLASH DDB.
2851                  */
2852                 if (ddb_entry->ddb_type == FLASH_DDB &&
2853                     ddb_entry->chap_tbl_idx != INVALID_ENTRY &&
2854                     !sess->username && !sess->password) {
2855                         idx = ddb_entry->chap_tbl_idx;
2856                         rval = qla4xxx_get_uni_chap_at_index(ha, chap_tbl.name,
2857                                                             chap_tbl.secret,
2858                                                             idx);
2859                         if (!rval) {
2860                                 iscsi_set_param(cls_conn, ISCSI_PARAM_USERNAME,
2861                                                 (char *)chap_tbl.name,
2862                                                 strlen((char *)chap_tbl.name));
2863                                 iscsi_set_param(cls_conn, ISCSI_PARAM_PASSWORD,
2864                                                 (char *)chap_tbl.secret,
2865                                                 chap_tbl.secret_len);
2866                         }
2867                 }
2868                 /* allow fall-through */
2869         default:
2870                 return iscsi_session_get_param(cls_sess, param, buf);
2871         }
2872 
2873         return len;
2874 }
2875 
2876 static int qla4xxx_conn_get_param(struct iscsi_cls_conn *cls_conn,
2877                                   enum iscsi_param param, char *buf)
2878 {
2879         struct iscsi_conn *conn;
2880         struct qla_conn *qla_conn;
2881         struct sockaddr *dst_addr;
2882         int len = 0;
2883 
2884         conn = cls_conn->dd_data;
2885         qla_conn = conn->dd_data;
2886         dst_addr = (struct sockaddr *)&qla_conn->qla_ep->dst_addr;
2887 
2888         switch (param) {
2889         case ISCSI_PARAM_CONN_PORT:
2890         case ISCSI_PARAM_CONN_ADDRESS:
2891                 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
2892                                                  dst_addr, param, buf);
2893         default:
2894                 return iscsi_conn_get_param(cls_conn, param, buf);
2895         }
2896 
2897         return len;
2898 
2899 }
2900 
2901 int qla4xxx_get_ddb_index(struct scsi_qla_host *ha, uint16_t *ddb_index)
2902 {
2903         uint32_t mbx_sts = 0;
2904         uint16_t tmp_ddb_index;
2905         int ret;
2906 
2907 get_ddb_index:
2908         tmp_ddb_index = find_first_zero_bit(ha->ddb_idx_map, MAX_DDB_ENTRIES);
2909 
2910         if (tmp_ddb_index >= MAX_DDB_ENTRIES) {
2911                 DEBUG2(ql4_printk(KERN_INFO, ha,
2912                                   "Free DDB index not available\n"));
2913                 ret = QLA_ERROR;
2914                 goto exit_get_ddb_index;
2915         }
2916 
2917         if (test_and_set_bit(tmp_ddb_index, ha->ddb_idx_map))
2918                 goto get_ddb_index;
2919 
2920         DEBUG2(ql4_printk(KERN_INFO, ha,
2921                           "Found a free DDB index at %d\n", tmp_ddb_index));
2922         ret = qla4xxx_req_ddb_entry(ha, tmp_ddb_index, &mbx_sts);
2923         if (ret == QLA_ERROR) {
2924                 if (mbx_sts == MBOX_STS_COMMAND_ERROR) {
2925                         ql4_printk(KERN_INFO, ha,
2926                                    "DDB index = %d not available trying next\n",
2927                                    tmp_ddb_index);
2928                         goto get_ddb_index;
2929                 }
2930                 DEBUG2(ql4_printk(KERN_INFO, ha,
2931                                   "Free FW DDB not available\n"));
2932         }
2933 
2934         *ddb_index = tmp_ddb_index;
2935 
2936 exit_get_ddb_index:
2937         return ret;
2938 }
2939 
2940 static int qla4xxx_match_ipaddress(struct scsi_qla_host *ha,
2941                                    struct ddb_entry *ddb_entry,
2942                                    char *existing_ipaddr,
2943                                    char *user_ipaddr)
2944 {
2945         uint8_t dst_ipaddr[IPv6_ADDR_LEN];
2946         char formatted_ipaddr[DDB_IPADDR_LEN];
2947         int status = QLA_SUCCESS, ret = 0;
2948 
2949         if (ddb_entry->fw_ddb_entry.options & DDB_OPT_IPV6_DEVICE) {
2950                 ret = in6_pton(user_ipaddr, strlen(user_ipaddr), dst_ipaddr,
2951                                '\0', NULL);
2952                 if (ret == 0) {
2953                         status = QLA_ERROR;
2954                         goto out_match;
2955                 }
2956                 ret = sprintf(formatted_ipaddr, "%pI6", dst_ipaddr);
2957         } else {
2958                 ret = in4_pton(user_ipaddr, strlen(user_ipaddr), dst_ipaddr,
2959                                '\0', NULL);
2960                 if (ret == 0) {
2961                         status = QLA_ERROR;
2962                         goto out_match;
2963                 }
2964                 ret = sprintf(formatted_ipaddr, "%pI4", dst_ipaddr);
2965         }
2966 
2967         if (strcmp(existing_ipaddr, formatted_ipaddr))
2968                 status = QLA_ERROR;
2969 
2970 out_match:
2971         return status;
2972 }
2973 
2974 static int qla4xxx_match_fwdb_session(struct scsi_qla_host *ha,
2975                                       struct iscsi_cls_conn *cls_conn)
2976 {
2977         int idx = 0, max_ddbs, rval;
2978         struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
2979         struct iscsi_session *sess, *existing_sess;
2980         struct iscsi_conn *conn, *existing_conn;
2981         struct ddb_entry *ddb_entry;
2982 
2983         sess = cls_sess->dd_data;
2984         conn = cls_conn->dd_data;
2985 
2986         if (sess->targetname == NULL ||
2987             conn->persistent_address == NULL ||
2988             conn->persistent_port == 0)
2989                 return QLA_ERROR;
2990 
2991         max_ddbs =  is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
2992                                      MAX_DEV_DB_ENTRIES;
2993 
2994         for (idx = 0; idx < max_ddbs; idx++) {
2995                 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
2996                 if (ddb_entry == NULL)
2997                         continue;
2998 
2999                 if (ddb_entry->ddb_type != FLASH_DDB)
3000                         continue;
3001 
3002                 existing_sess = ddb_entry->sess->dd_data;
3003                 existing_conn = ddb_entry->conn->dd_data;
3004 
3005                 if (existing_sess->targetname == NULL ||
3006                     existing_conn->persistent_address == NULL ||
3007                     existing_conn->persistent_port == 0)
3008                         continue;
3009 
3010                 DEBUG2(ql4_printk(KERN_INFO, ha,
3011                                   "IQN = %s User IQN = %s\n",
3012                                   existing_sess->targetname,
3013                                   sess->targetname));
3014 
3015                 DEBUG2(ql4_printk(KERN_INFO, ha,
3016                                   "IP = %s User IP = %s\n",
3017                                   existing_conn->persistent_address,
3018                                   conn->persistent_address));
3019 
3020                 DEBUG2(ql4_printk(KERN_INFO, ha,
3021                                   "Port = %d User Port = %d\n",
3022                                   existing_conn->persistent_port,
3023                                   conn->persistent_port));
3024 
3025                 if (strcmp(existing_sess->targetname, sess->targetname))
3026                         continue;
3027                 rval = qla4xxx_match_ipaddress(ha, ddb_entry,
3028                                         existing_conn->persistent_address,
3029                                         conn->persistent_address);
3030                 if (rval == QLA_ERROR)
3031                         continue;
3032                 if (existing_conn->persistent_port != conn->persistent_port)
3033                         continue;
3034                 break;
3035         }
3036 
3037         if (idx == max_ddbs)
3038                 return QLA_ERROR;
3039 
3040         DEBUG2(ql4_printk(KERN_INFO, ha,
3041                           "Match found in fwdb sessions\n"));
3042         return QLA_SUCCESS;
3043 }
3044 
3045 static struct iscsi_cls_session *
3046 qla4xxx_session_create(struct iscsi_endpoint *ep,
3047                         uint16_t cmds_max, uint16_t qdepth,
3048                         uint32_t initial_cmdsn)
3049 {
3050         struct iscsi_cls_session *cls_sess;
3051         struct scsi_qla_host *ha;
3052         struct qla_endpoint *qla_ep;
3053         struct ddb_entry *ddb_entry;
3054         uint16_t ddb_index;
3055         struct iscsi_session *sess;
3056         struct sockaddr *dst_addr;
3057         int ret;
3058 
3059         if (!ep) {
3060                 printk(KERN_ERR "qla4xxx: missing ep.\n");
3061                 return NULL;
3062         }
3063 
3064         qla_ep = ep->dd_data;
3065         dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
3066         ha = to_qla_host(qla_ep->host);
3067         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: host: %ld\n", __func__,
3068                           ha->host_no));
3069 
3070         ret = qla4xxx_get_ddb_index(ha, &ddb_index);
3071         if (ret == QLA_ERROR)
3072                 return NULL;
3073 
3074         cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport, qla_ep->host,
3075                                        cmds_max, sizeof(struct ddb_entry),
3076                                        sizeof(struct ql4_task_data),
3077                                        initial_cmdsn, ddb_index);
3078         if (!cls_sess)
3079                 return NULL;
3080 
3081         sess = cls_sess->dd_data;
3082         ddb_entry = sess->dd_data;
3083         ddb_entry->fw_ddb_index = ddb_index;
3084         ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
3085         ddb_entry->ha = ha;
3086         ddb_entry->sess = cls_sess;
3087         ddb_entry->unblock_sess = qla4xxx_unblock_ddb;
3088         ddb_entry->ddb_change = qla4xxx_ddb_change;
3089         clear_bit(DDB_CONN_CLOSE_FAILURE, &ddb_entry->flags);
3090         cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
3091         ha->fw_ddb_index_map[ddb_entry->fw_ddb_index] = ddb_entry;
3092         ha->tot_ddbs++;
3093 
3094         return cls_sess;
3095 }
3096 
3097 static void qla4xxx_session_destroy(struct iscsi_cls_session *cls_sess)
3098 {
3099         struct iscsi_session *sess;
3100         struct ddb_entry *ddb_entry;
3101         struct scsi_qla_host *ha;
3102         unsigned long flags, wtime;
3103         struct dev_db_entry *fw_ddb_entry = NULL;
3104         dma_addr_t fw_ddb_entry_dma;
3105         uint32_t ddb_state;
3106         int ret;
3107 
3108         sess = cls_sess->dd_data;
3109         ddb_entry = sess->dd_data;
3110         ha = ddb_entry->ha;
3111         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: host: %ld\n", __func__,
3112                           ha->host_no));
3113 
3114         fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3115                                           &fw_ddb_entry_dma, GFP_KERNEL);
3116         if (!fw_ddb_entry) {
3117                 ql4_printk(KERN_ERR, ha,
3118                            "%s: Unable to allocate dma buffer\n", __func__);
3119                 goto destroy_session;
3120         }
3121 
3122         wtime = jiffies + (HZ * LOGOUT_TOV);
3123         do {
3124                 ret = qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index,
3125                                               fw_ddb_entry, fw_ddb_entry_dma,
3126                                               NULL, NULL, &ddb_state, NULL,
3127                                               NULL, NULL);
3128                 if (ret == QLA_ERROR)
3129                         goto destroy_session;
3130 
3131                 if ((ddb_state == DDB_DS_NO_CONNECTION_ACTIVE) ||
3132                     (ddb_state == DDB_DS_SESSION_FAILED))
3133                         goto destroy_session;
3134 
3135                 schedule_timeout_uninterruptible(HZ);
3136         } while ((time_after(wtime, jiffies)));
3137 
3138 destroy_session:
3139         qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
3140         if (test_and_clear_bit(DDB_CONN_CLOSE_FAILURE, &ddb_entry->flags))
3141                 clear_bit(ddb_entry->fw_ddb_index, ha->ddb_idx_map);
3142         spin_lock_irqsave(&ha->hardware_lock, flags);
3143         qla4xxx_free_ddb(ha, ddb_entry);
3144         spin_unlock_irqrestore(&ha->hardware_lock, flags);
3145 
3146         iscsi_session_teardown(cls_sess);
3147 
3148         if (fw_ddb_entry)
3149                 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3150                                   fw_ddb_entry, fw_ddb_entry_dma);
3151 }
3152 
3153 static struct iscsi_cls_conn *
3154 qla4xxx_conn_create(struct iscsi_cls_session *cls_sess, uint32_t conn_idx)
3155 {
3156         struct iscsi_cls_conn *cls_conn;
3157         struct iscsi_session *sess;
3158         struct ddb_entry *ddb_entry;
3159         struct scsi_qla_host *ha;
3160 
3161         cls_conn = iscsi_conn_setup(cls_sess, sizeof(struct qla_conn),
3162                                     conn_idx);
3163         if (!cls_conn) {
3164                 pr_info("%s: Can not create connection for conn_idx = %u\n",
3165                         __func__, conn_idx);
3166                 return NULL;
3167         }
3168 
3169         sess = cls_sess->dd_data;
3170         ddb_entry = sess->dd_data;
3171         ddb_entry->conn = cls_conn;
3172 
3173         ha = ddb_entry->ha;
3174         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: conn_idx = %u\n", __func__,
3175                           conn_idx));
3176         return cls_conn;
3177 }
3178 
3179 static int qla4xxx_conn_bind(struct iscsi_cls_session *cls_session,
3180                              struct iscsi_cls_conn *cls_conn,
3181                              uint64_t transport_fd, int is_leading)
3182 {
3183         struct iscsi_conn *conn;
3184         struct qla_conn *qla_conn;
3185         struct iscsi_endpoint *ep;
3186         struct ddb_entry *ddb_entry;
3187         struct scsi_qla_host *ha;
3188         struct iscsi_session *sess;
3189 
3190         sess = cls_session->dd_data;
3191         ddb_entry = sess->dd_data;
3192         ha = ddb_entry->ha;
3193 
3194         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: sid = %d, cid = %d\n", __func__,
3195                           cls_session->sid, cls_conn->cid));
3196 
3197         if (iscsi_conn_bind(cls_session, cls_conn, is_leading))
3198                 return -EINVAL;
3199         ep = iscsi_lookup_endpoint(transport_fd);
3200         conn = cls_conn->dd_data;
3201         qla_conn = conn->dd_data;
3202         qla_conn->qla_ep = ep->dd_data;
3203         return 0;
3204 }
3205 
3206 static int qla4xxx_conn_start(struct iscsi_cls_conn *cls_conn)
3207 {
3208         struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
3209         struct iscsi_session *sess;
3210         struct ddb_entry *ddb_entry;
3211         struct scsi_qla_host *ha;
3212         struct dev_db_entry *fw_ddb_entry = NULL;
3213         dma_addr_t fw_ddb_entry_dma;
3214         uint32_t mbx_sts = 0;
3215         int ret = 0;
3216         int status = QLA_SUCCESS;
3217 
3218         sess = cls_sess->dd_data;
3219         ddb_entry = sess->dd_data;
3220         ha = ddb_entry->ha;
3221         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: sid = %d, cid = %d\n", __func__,
3222                           cls_sess->sid, cls_conn->cid));
3223 
3224         /* Check if we have  matching FW DDB, if yes then do not
3225          * login to this target. This could cause target to logout previous
3226          * connection
3227          */
3228         ret = qla4xxx_match_fwdb_session(ha, cls_conn);
3229         if (ret == QLA_SUCCESS) {
3230                 ql4_printk(KERN_INFO, ha,
3231                            "Session already exist in FW.\n");
3232                 ret = -EEXIST;
3233                 goto exit_conn_start;
3234         }
3235 
3236         fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3237                                           &fw_ddb_entry_dma, GFP_KERNEL);
3238         if (!fw_ddb_entry) {
3239                 ql4_printk(KERN_ERR, ha,
3240                            "%s: Unable to allocate dma buffer\n", __func__);
3241                 ret = -ENOMEM;
3242                 goto exit_conn_start;
3243         }
3244 
3245         ret = qla4xxx_set_param_ddbentry(ha, ddb_entry, cls_conn, &mbx_sts);
3246         if (ret) {
3247                 /* If iscsid is stopped and started then no need to do
3248                 * set param again since ddb state will be already
3249                 * active and FW does not allow set ddb to an
3250                 * active session.
3251                 */
3252                 if (mbx_sts)
3253                         if (ddb_entry->fw_ddb_device_state ==
3254                                                 DDB_DS_SESSION_ACTIVE) {
3255                                 ddb_entry->unblock_sess(ddb_entry->sess);
3256                                 goto exit_set_param;
3257                         }
3258 
3259                 ql4_printk(KERN_ERR, ha, "%s: Failed set param for index[%d]\n",
3260                            __func__, ddb_entry->fw_ddb_index);
3261                 goto exit_conn_start;
3262         }
3263 
3264         status = qla4xxx_conn_open(ha, ddb_entry->fw_ddb_index);
3265         if (status == QLA_ERROR) {
3266                 ql4_printk(KERN_ERR, ha, "%s: Login failed: %s\n", __func__,
3267                            sess->targetname);
3268                 ret = -EINVAL;
3269                 goto exit_conn_start;
3270         }
3271 
3272         if (ddb_entry->fw_ddb_device_state == DDB_DS_NO_CONNECTION_ACTIVE)
3273                 ddb_entry->fw_ddb_device_state = DDB_DS_LOGIN_IN_PROCESS;
3274 
3275         DEBUG2(printk(KERN_INFO "%s: DDB state [%d]\n", __func__,
3276                       ddb_entry->fw_ddb_device_state));
3277 
3278 exit_set_param:
3279         ret = 0;
3280 
3281 exit_conn_start:
3282         if (fw_ddb_entry)
3283                 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3284                                   fw_ddb_entry, fw_ddb_entry_dma);
3285         return ret;
3286 }
3287 
3288 static void qla4xxx_conn_destroy(struct iscsi_cls_conn *cls_conn)
3289 {
3290         struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
3291         struct iscsi_session *sess;
3292         struct scsi_qla_host *ha;
3293         struct ddb_entry *ddb_entry;
3294         int options;
3295 
3296         sess = cls_sess->dd_data;
3297         ddb_entry = sess->dd_data;
3298         ha = ddb_entry->ha;
3299         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: cid = %d\n", __func__,
3300                           cls_conn->cid));
3301 
3302         options = LOGOUT_OPTION_CLOSE_SESSION;
3303         if (qla4xxx_session_logout_ddb(ha, ddb_entry, options) == QLA_ERROR)
3304                 ql4_printk(KERN_ERR, ha, "%s: Logout failed\n", __func__);
3305 }
3306 
3307 static void qla4xxx_task_work(struct work_struct *wdata)
3308 {
3309         struct ql4_task_data *task_data;
3310         struct scsi_qla_host *ha;
3311         struct passthru_status *sts;
3312         struct iscsi_task *task;
3313         struct iscsi_hdr *hdr;
3314         uint8_t *data;
3315         uint32_t data_len;
3316         struct iscsi_conn *conn;
3317         int hdr_len;
3318         itt_t itt;
3319 
3320         task_data = container_of(wdata, struct ql4_task_data, task_work);
3321         ha = task_data->ha;
3322         task = task_data->task;
3323         sts = &task_data->sts;
3324         hdr_len = sizeof(struct iscsi_hdr);
3325 
3326         DEBUG3(printk(KERN_INFO "Status returned\n"));
3327         DEBUG3(qla4xxx_dump_buffer(sts, 64));
3328         DEBUG3(printk(KERN_INFO "Response buffer"));
3329         DEBUG3(qla4xxx_dump_buffer(task_data->resp_buffer, 64));
3330 
3331         conn = task->conn;
3332 
3333         switch (sts->completionStatus) {
3334         case PASSTHRU_STATUS_COMPLETE:
3335                 hdr = (struct iscsi_hdr *)task_data->resp_buffer;
3336                 /* Assign back the itt in hdr, until we use the PREASSIGN_TAG */
3337                 itt = sts->handle;
3338                 hdr->itt = itt;
3339                 data = task_data->resp_buffer + hdr_len;
3340                 data_len = task_data->resp_len - hdr_len;
3341                 iscsi_complete_pdu(conn, hdr, data, data_len);
3342                 break;
3343         default:
3344                 ql4_printk(KERN_ERR, ha, "Passthru failed status = 0x%x\n",
3345                            sts->completionStatus);
3346                 break;
3347         }
3348         return;
3349 }
3350 
3351 static int qla4xxx_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
3352 {
3353         struct ql4_task_data *task_data;
3354         struct iscsi_session *sess;
3355         struct ddb_entry *ddb_entry;
3356         struct scsi_qla_host *ha;
3357         int hdr_len;
3358 
3359         sess = task->conn->session;
3360         ddb_entry = sess->dd_data;
3361         ha = ddb_entry->ha;
3362         task_data = task->dd_data;
3363         memset(task_data, 0, sizeof(struct ql4_task_data));
3364 
3365         if (task->sc) {
3366                 ql4_printk(KERN_INFO, ha,
3367                            "%s: SCSI Commands not implemented\n", __func__);
3368                 return -EINVAL;
3369         }
3370 
3371         hdr_len = sizeof(struct iscsi_hdr);
3372         task_data->ha = ha;
3373         task_data->task = task;
3374 
3375         if (task->data_count) {
3376                 task_data->data_dma = dma_map_single(&ha->pdev->dev, task->data,
3377                                                      task->data_count,
3378                                                      PCI_DMA_TODEVICE);
3379         }
3380 
3381         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
3382                       __func__, task->conn->max_recv_dlength, hdr_len));
3383 
3384         task_data->resp_len = task->conn->max_recv_dlength + hdr_len;
3385         task_data->resp_buffer = dma_alloc_coherent(&ha->pdev->dev,
3386                                                     task_data->resp_len,
3387                                                     &task_data->resp_dma,
3388                                                     GFP_ATOMIC);
3389         if (!task_data->resp_buffer)
3390                 goto exit_alloc_pdu;
3391 
3392         task_data->req_len = task->data_count + hdr_len;
3393         task_data->req_buffer = dma_alloc_coherent(&ha->pdev->dev,
3394                                                    task_data->req_len,
3395                                                    &task_data->req_dma,
3396                                                    GFP_ATOMIC);
3397         if (!task_data->req_buffer)
3398                 goto exit_alloc_pdu;
3399 
3400         task->hdr = task_data->req_buffer;
3401 
3402         INIT_WORK(&task_data->task_work, qla4xxx_task_work);
3403 
3404         return 0;
3405 
3406 exit_alloc_pdu:
3407         if (task_data->resp_buffer)
3408                 dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
3409                                   task_data->resp_buffer, task_data->resp_dma);
3410 
3411         if (task_data->req_buffer)
3412                 dma_free_coherent(&ha->pdev->dev, task_data->req_len,
3413                                   task_data->req_buffer, task_data->req_dma);
3414         return -ENOMEM;
3415 }
3416 
3417 static void qla4xxx_task_cleanup(struct iscsi_task *task)
3418 {
3419         struct ql4_task_data *task_data;
3420         struct iscsi_session *sess;
3421         struct ddb_entry *ddb_entry;
3422         struct scsi_qla_host *ha;
3423         int hdr_len;
3424 
3425         hdr_len = sizeof(struct iscsi_hdr);
3426         sess = task->conn->session;
3427         ddb_entry = sess->dd_data;
3428         ha = ddb_entry->ha;
3429         task_data = task->dd_data;
3430 
3431         if (task->data_count) {
3432                 dma_unmap_single(&ha->pdev->dev, task_data->data_dma,
3433                                  task->data_count, PCI_DMA_TODEVICE);
3434         }
3435 
3436         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
3437                       __func__, task->conn->max_recv_dlength, hdr_len));
3438 
3439         dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
3440                           task_data->resp_buffer, task_data->resp_dma);
3441         dma_free_coherent(&ha->pdev->dev, task_data->req_len,
3442                           task_data->req_buffer, task_data->req_dma);
3443         return;
3444 }
3445 
3446 static int qla4xxx_task_xmit(struct iscsi_task *task)
3447 {
3448         struct scsi_cmnd *sc = task->sc;
3449         struct iscsi_session *sess = task->conn->session;
3450         struct ddb_entry *ddb_entry = sess->dd_data;
3451         struct scsi_qla_host *ha = ddb_entry->ha;
3452 
3453         if (!sc)
3454                 return qla4xxx_send_passthru0(task);
3455 
3456         ql4_printk(KERN_INFO, ha, "%s: scsi cmd xmit not implemented\n",
3457                    __func__);
3458         return -ENOSYS;
3459 }
3460 
3461 static int qla4xxx_copy_from_fwddb_param(struct iscsi_bus_flash_session *sess,
3462                                          struct iscsi_bus_flash_conn *conn,
3463                                          struct dev_db_entry *fw_ddb_entry)
3464 {
3465         unsigned long options = 0;
3466         int rc = 0;
3467 
3468         options = le16_to_cpu(fw_ddb_entry->options);
3469         conn->is_fw_assigned_ipv6 = test_bit(OPT_IS_FW_ASSIGNED_IPV6, &options);
3470         if (test_bit(OPT_IPV6_DEVICE, &options)) {
3471                 rc = iscsi_switch_str_param(&sess->portal_type,
3472                                             PORTAL_TYPE_IPV6);
3473                 if (rc)
3474                         goto exit_copy;
3475         } else {
3476                 rc = iscsi_switch_str_param(&sess->portal_type,
3477                                             PORTAL_TYPE_IPV4);
3478                 if (rc)
3479                         goto exit_copy;
3480         }
3481 
3482         sess->auto_snd_tgt_disable = test_bit(OPT_AUTO_SENDTGTS_DISABLE,
3483                                               &options);
3484         sess->discovery_sess = test_bit(OPT_DISC_SESSION, &options);
3485         sess->entry_state = test_bit(OPT_ENTRY_STATE, &options);
3486 
3487         options = le16_to_cpu(fw_ddb_entry->iscsi_options);
3488         conn->hdrdgst_en = test_bit(ISCSIOPT_HEADER_DIGEST_EN, &options);
3489         conn->datadgst_en = test_bit(ISCSIOPT_DATA_DIGEST_EN, &options);
3490         sess->imm_data_en = test_bit(ISCSIOPT_IMMEDIATE_DATA_EN, &options);
3491         sess->initial_r2t_en = test_bit(ISCSIOPT_INITIAL_R2T_EN, &options);
3492         sess->dataseq_inorder_en = test_bit(ISCSIOPT_DATA_SEQ_IN_ORDER,
3493                                             &options);
3494         sess->pdu_inorder_en = test_bit(ISCSIOPT_DATA_PDU_IN_ORDER, &options);
3495         sess->chap_auth_en = test_bit(ISCSIOPT_CHAP_AUTH_EN, &options);
3496         conn->snack_req_en = test_bit(ISCSIOPT_SNACK_REQ_EN, &options);
3497         sess->discovery_logout_en = test_bit(ISCSIOPT_DISCOVERY_LOGOUT_EN,
3498                                              &options);
3499         sess->bidi_chap_en = test_bit(ISCSIOPT_BIDI_CHAP_EN, &options);
3500         sess->discovery_auth_optional =
3501                         test_bit(ISCSIOPT_DISCOVERY_AUTH_OPTIONAL, &options);
3502         if (test_bit(ISCSIOPT_ERL1, &options))
3503                 sess->erl |= BIT_1;
3504         if (test_bit(ISCSIOPT_ERL0, &options))
3505                 sess->erl |= BIT_0;
3506 
3507         options = le16_to_cpu(fw_ddb_entry->tcp_options);
3508         conn->tcp_timestamp_stat = test_bit(TCPOPT_TIMESTAMP_STAT, &options);
3509         conn->tcp_nagle_disable = test_bit(TCPOPT_NAGLE_DISABLE, &options);
3510         conn->tcp_wsf_disable = test_bit(TCPOPT_WSF_DISABLE, &options);
3511         if (test_bit(TCPOPT_TIMER_SCALE3, &options))
3512                 conn->tcp_timer_scale |= BIT_3;
3513         if (test_bit(TCPOPT_TIMER_SCALE2, &options))
3514                 conn->tcp_timer_scale |= BIT_2;
3515         if (test_bit(TCPOPT_TIMER_SCALE1, &options))
3516                 conn->tcp_timer_scale |= BIT_1;
3517 
3518         conn->tcp_timer_scale >>= 1;
3519         conn->tcp_timestamp_en = test_bit(TCPOPT_TIMESTAMP_EN, &options);
3520 
3521         options = le16_to_cpu(fw_ddb_entry->ip_options);
3522         conn->fragment_disable = test_bit(IPOPT_FRAGMENT_DISABLE, &options);
3523 
3524         conn->max_recv_dlength = BYTE_UNITS *
3525                           le16_to_cpu(fw_ddb_entry->iscsi_max_rcv_data_seg_len);
3526         conn->max_xmit_dlength = BYTE_UNITS *
3527                           le16_to_cpu(fw_ddb_entry->iscsi_max_snd_data_seg_len);
3528         sess->first_burst = BYTE_UNITS *
3529                                le16_to_cpu(fw_ddb_entry->iscsi_first_burst_len);
3530         sess->max_burst = BYTE_UNITS *
3531                                  le16_to_cpu(fw_ddb_entry->iscsi_max_burst_len);
3532         sess->max_r2t = le16_to_cpu(fw_ddb_entry->iscsi_max_outsnd_r2t);
3533         sess->time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
3534         sess->time2retain = le16_to_cpu(fw_ddb_entry->iscsi_def_time2retain);
3535         sess->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
3536         conn->max_segment_size = le16_to_cpu(fw_ddb_entry->mss);
3537         conn->tcp_xmit_wsf = fw_ddb_entry->tcp_xmt_wsf;
3538         conn->tcp_recv_wsf = fw_ddb_entry->tcp_rcv_wsf;
3539         conn->ipv6_flow_label = le16_to_cpu(fw_ddb_entry->ipv6_flow_lbl);
3540         conn->keepalive_timeout = le16_to_cpu(fw_ddb_entry->ka_timeout);
3541         conn->local_port = le16_to_cpu(fw_ddb_entry->lcl_port);
3542         conn->statsn = le32_to_cpu(fw_ddb_entry->stat_sn);
3543         conn->exp_statsn = le32_to_cpu(fw_ddb_entry->exp_stat_sn);
3544         sess->discovery_parent_idx = le16_to_cpu(fw_ddb_entry->ddb_link);
3545         sess->discovery_parent_type = le16_to_cpu(fw_ddb_entry->ddb_link);
3546         sess->chap_out_idx = le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
3547         sess->tsid = le16_to_cpu(fw_ddb_entry->tsid);
3548 
3549         sess->default_taskmgmt_timeout =
3550                                 le16_to_cpu(fw_ddb_entry->def_timeout);
3551         conn->port = le16_to_cpu(fw_ddb_entry->port);
3552 
3553         options = le16_to_cpu(fw_ddb_entry->options);
3554         conn->ipaddress = kzalloc(IPv6_ADDR_LEN, GFP_KERNEL);
3555         if (!conn->ipaddress) {
3556                 rc = -ENOMEM;
3557                 goto exit_copy;
3558         }
3559 
3560         conn->redirect_ipaddr = kzalloc(IPv6_ADDR_LEN, GFP_KERNEL);
3561         if (!conn->redirect_ipaddr) {
3562                 rc = -ENOMEM;
3563                 goto exit_copy;
3564         }
3565 
3566         memcpy(conn->ipaddress, fw_ddb_entry->ip_addr, IPv6_ADDR_LEN);
3567         memcpy(conn->redirect_ipaddr, fw_ddb_entry->tgt_addr, IPv6_ADDR_LEN);
3568 
3569         if (test_bit(OPT_IPV6_DEVICE, &options)) {
3570                 conn->ipv6_traffic_class = fw_ddb_entry->ipv4_tos;
3571 
3572                 conn->link_local_ipv6_addr = kzalloc(IPv6_ADDR_LEN, GFP_KERNEL);
3573                 if (!conn->link_local_ipv6_addr) {
3574                         rc = -ENOMEM;
3575                         goto exit_copy;
3576                 }
3577 
3578                 memcpy(conn->link_local_ipv6_addr,
3579                        fw_ddb_entry->link_local_ipv6_addr, IPv6_ADDR_LEN);
3580         } else {
3581                 conn->ipv4_tos = fw_ddb_entry->ipv4_tos;
3582         }
3583 
3584         if (fw_ddb_entry->iscsi_name[0]) {
3585                 rc = iscsi_switch_str_param(&sess->targetname,
3586                                             (char *)fw_ddb_entry->iscsi_name);
3587                 if (rc)
3588                         goto exit_copy;
3589         }
3590 
3591         if (fw_ddb_entry->iscsi_alias[0]) {
3592                 rc = iscsi_switch_str_param(&sess->targetalias,
3593                                             (char *)fw_ddb_entry->iscsi_alias);
3594                 if (rc)
3595                         goto exit_copy;
3596         }
3597 
3598         COPY_ISID(sess->isid, fw_ddb_entry->isid);
3599 
3600 exit_copy:
3601         return rc;
3602 }
3603 
3604 static int qla4xxx_copy_to_fwddb_param(struct iscsi_bus_flash_session *sess,
3605                                        struct iscsi_bus_flash_conn *conn,
3606                                        struct dev_db_entry *fw_ddb_entry)
3607 {
3608         uint16_t options;
3609         int rc = 0;
3610 
3611         options = le16_to_cpu(fw_ddb_entry->options);
3612         SET_BITVAL(conn->is_fw_assigned_ipv6,  options, BIT_11);
3613         if (!strncmp(sess->portal_type, PORTAL_TYPE_IPV6, 4))
3614                 options |= BIT_8;
3615         else
3616                 options &= ~BIT_8;
3617 
3618         SET_BITVAL(sess->auto_snd_tgt_disable, options, BIT_6);
3619         SET_BITVAL(sess->discovery_sess, options, BIT_4);
3620         SET_BITVAL(sess->entry_state, options, BIT_3);
3621         fw_ddb_entry->options = cpu_to_le16(options);
3622 
3623         options = le16_to_cpu(fw_ddb_entry->iscsi_options);
3624         SET_BITVAL(conn->hdrdgst_en, options, BIT_13);
3625         SET_BITVAL(conn->datadgst_en, options, BIT_12);
3626         SET_BITVAL(sess->imm_data_en, options, BIT_11);
3627         SET_BITVAL(sess->initial_r2t_en, options, BIT_10);
3628         SET_BITVAL(sess->dataseq_inorder_en, options, BIT_9);
3629         SET_BITVAL(sess->pdu_inorder_en, options, BIT_8);
3630         SET_BITVAL(sess->chap_auth_en, options, BIT_7);
3631         SET_BITVAL(conn->snack_req_en, options, BIT_6);
3632         SET_BITVAL(sess->discovery_logout_en, options, BIT_5);
3633         SET_BITVAL(sess->bidi_chap_en, options, BIT_4);
3634         SET_BITVAL(sess->discovery_auth_optional, options, BIT_3);
3635         SET_BITVAL(sess->erl & BIT_1, options, BIT_1);
3636         SET_BITVAL(sess->erl & BIT_0, options, BIT_0);
3637         fw_ddb_entry->iscsi_options = cpu_to_le16(options);
3638 
3639         options = le16_to_cpu(fw_ddb_entry->tcp_options);
3640         SET_BITVAL(conn->tcp_timestamp_stat, options, BIT_6);
3641         SET_BITVAL(conn->tcp_nagle_disable, options, BIT_5);
3642         SET_BITVAL(conn->tcp_wsf_disable, options, BIT_4);
3643         SET_BITVAL(conn->tcp_timer_scale & BIT_2, options, BIT_3);
3644         SET_BITVAL(conn->tcp_timer_scale & BIT_1, options, BIT_2);
3645         SET_BITVAL(conn->tcp_timer_scale & BIT_0, options, BIT_1);
3646         SET_BITVAL(conn->tcp_timestamp_en, options, BIT_0);
3647         fw_ddb_entry->tcp_options = cpu_to_le16(options);
3648 
3649         options = le16_to_cpu(fw_ddb_entry->ip_options);
3650         SET_BITVAL(conn->fragment_disable, options, BIT_4);
3651         fw_ddb_entry->ip_options = cpu_to_le16(options);
3652 
3653         fw_ddb_entry->iscsi_max_outsnd_r2t = cpu_to_le16(sess->max_r2t);
3654         fw_ddb_entry->iscsi_max_rcv_data_seg_len =
3655                                cpu_to_le16(conn->max_recv_dlength / BYTE_UNITS);
3656         fw_ddb_entry->iscsi_max_snd_data_seg_len =
3657                                cpu_to_le16(conn->max_xmit_dlength / BYTE_UNITS);
3658         fw_ddb_entry->iscsi_first_burst_len =
3659                                 cpu_to_le16(sess->first_burst / BYTE_UNITS);
3660         fw_ddb_entry->iscsi_max_burst_len = cpu_to_le16(sess->max_burst /
3661                                             BYTE_UNITS);
3662         fw_ddb_entry->iscsi_def_time2wait = cpu_to_le16(sess->time2wait);
3663         fw_ddb_entry->iscsi_def_time2retain = cpu_to_le16(sess->time2retain);
3664         fw_ddb_entry->tgt_portal_grp = cpu_to_le16(sess->tpgt);
3665         fw_ddb_entry->mss = cpu_to_le16(conn->max_segment_size);
3666         fw_ddb_entry->tcp_xmt_wsf = (uint8_t) cpu_to_le32(conn->tcp_xmit_wsf);
3667         fw_ddb_entry->tcp_rcv_wsf = (uint8_t) cpu_to_le32(conn->tcp_recv_wsf);
3668         fw_ddb_entry->ipv6_flow_lbl = cpu_to_le16(conn->ipv6_flow_label);
3669         fw_ddb_entry->ka_timeout = cpu_to_le16(conn->keepalive_timeout);
3670         fw_ddb_entry->lcl_port = cpu_to_le16(conn->local_port);
3671         fw_ddb_entry->stat_sn = cpu_to_le32(conn->statsn);
3672         fw_ddb_entry->exp_stat_sn = cpu_to_le32(conn->exp_statsn);
3673         fw_ddb_entry->ddb_link = cpu_to_le16(sess->discovery_parent_idx);
3674         fw_ddb_entry->chap_tbl_idx = cpu_to_le16(sess->chap_out_idx);
3675         fw_ddb_entry->tsid = cpu_to_le16(sess->tsid);
3676         fw_ddb_entry->port = cpu_to_le16(conn->port);
3677         fw_ddb_entry->def_timeout =
3678                                 cpu_to_le16(sess->default_taskmgmt_timeout);
3679 
3680         if (!strncmp(sess->portal_type, PORTAL_TYPE_IPV6, 4))
3681                 fw_ddb_entry->ipv4_tos = conn->ipv6_traffic_class;
3682         else
3683                 fw_ddb_entry->ipv4_tos = conn->ipv4_tos;
3684 
3685         if (conn->ipaddress)
3686                 memcpy(fw_ddb_entry->ip_addr, conn->ipaddress,
3687                        sizeof(fw_ddb_entry->ip_addr));
3688 
3689         if (conn->redirect_ipaddr)
3690                 memcpy(fw_ddb_entry->tgt_addr, conn->redirect_ipaddr,
3691                        sizeof(fw_ddb_entry->tgt_addr));
3692 
3693         if (conn->link_local_ipv6_addr)
3694                 memcpy(fw_ddb_entry->link_local_ipv6_addr,
3695                        conn->link_local_ipv6_addr,
3696                        sizeof(fw_ddb_entry->link_local_ipv6_addr));
3697 
3698         if (sess->targetname)
3699                 memcpy(fw_ddb_entry->iscsi_name, sess->targetname,
3700                        sizeof(fw_ddb_entry->iscsi_name));
3701 
3702         if (sess->targetalias)
3703                 memcpy(fw_ddb_entry->iscsi_alias, sess->targetalias,
3704                        sizeof(fw_ddb_entry->iscsi_alias));
3705 
3706         COPY_ISID(fw_ddb_entry->isid, sess->isid);
3707 
3708         return rc;
3709 }
3710 
3711 static void qla4xxx_copy_to_sess_conn_params(struct iscsi_conn *conn,
3712                                              struct iscsi_session *sess,
3713                                              struct dev_db_entry *fw_ddb_entry)
3714 {
3715         unsigned long options = 0;
3716         uint16_t ddb_link;
3717         uint16_t disc_parent;
3718         char ip_addr[DDB_IPADDR_LEN];
3719 
3720         options = le16_to_cpu(fw_ddb_entry->options);
3721         conn->is_fw_assigned_ipv6 = test_bit(OPT_IS_FW_ASSIGNED_IPV6, &options);
3722         sess->auto_snd_tgt_disable = test_bit(OPT_AUTO_SENDTGTS_DISABLE,
3723                                               &options);
3724         sess->discovery_sess = test_bit(OPT_DISC_SESSION, &options);
3725 
3726         options = le16_to_cpu(fw_ddb_entry->iscsi_options);
3727         conn->hdrdgst_en = test_bit(ISCSIOPT_HEADER_DIGEST_EN, &options);
3728         conn->datadgst_en = test_bit(ISCSIOPT_DATA_DIGEST_EN, &options);
3729         sess->imm_data_en = test_bit(ISCSIOPT_IMMEDIATE_DATA_EN, &options);
3730         sess->initial_r2t_en = test_bit(ISCSIOPT_INITIAL_R2T_EN, &options);
3731         sess->dataseq_inorder_en = test_bit(ISCSIOPT_DATA_SEQ_IN_ORDER,
3732                                             &options);
3733         sess->pdu_inorder_en = test_bit(ISCSIOPT_DATA_PDU_IN_ORDER, &options);
3734         sess->chap_auth_en = test_bit(ISCSIOPT_CHAP_AUTH_EN, &options);
3735         sess->discovery_logout_en = test_bit(ISCSIOPT_DISCOVERY_LOGOUT_EN,
3736                                              &options);
3737         sess->bidi_chap_en = test_bit(ISCSIOPT_BIDI_CHAP_EN, &options);
3738         sess->discovery_auth_optional =
3739                         test_bit(ISCSIOPT_DISCOVERY_AUTH_OPTIONAL, &options);
3740         if (test_bit(ISCSIOPT_ERL1, &options))
3741                 sess->erl |= BIT_1;
3742         if (test_bit(ISCSIOPT_ERL0, &options))
3743                 sess->erl |= BIT_0;
3744 
3745         options = le16_to_cpu(fw_ddb_entry->tcp_options);
3746         conn->tcp_timestamp_stat = test_bit(TCPOPT_TIMESTAMP_STAT, &options);
3747         conn->tcp_nagle_disable = test_bit(TCPOPT_NAGLE_DISABLE, &options);
3748         conn->tcp_wsf_disable = test_bit(TCPOPT_WSF_DISABLE, &options);
3749         if (test_bit(TCPOPT_TIMER_SCALE3, &options))
3750                 conn->tcp_timer_scale |= BIT_3;
3751         if (test_bit(TCPOPT_TIMER_SCALE2, &options))
3752                 conn->tcp_timer_scale |= BIT_2;
3753         if (test_bit(TCPOPT_TIMER_SCALE1, &options))
3754                 conn->tcp_timer_scale |= BIT_1;
3755 
3756         conn->tcp_timer_scale >>= 1;
3757         conn->tcp_timestamp_en = test_bit(TCPOPT_TIMESTAMP_EN, &options);
3758 
3759         options = le16_to_cpu(fw_ddb_entry->ip_options);
3760         conn->fragment_disable = test_bit(IPOPT_FRAGMENT_DISABLE, &options);
3761 
3762         conn->max_recv_dlength = BYTE_UNITS *
3763                           le16_to_cpu(fw_ddb_entry->iscsi_max_rcv_data_seg_len);
3764         conn->max_xmit_dlength = BYTE_UNITS *
3765                           le16_to_cpu(fw_ddb_entry->iscsi_max_snd_data_seg_len);
3766         sess->max_r2t = le16_to_cpu(fw_ddb_entry->iscsi_max_outsnd_r2t);
3767         sess->first_burst = BYTE_UNITS *
3768                                le16_to_cpu(fw_ddb_entry->iscsi_first_burst_len);
3769         sess->max_burst = BYTE_UNITS *
3770                                  le16_to_cpu(fw_ddb_entry->iscsi_max_burst_len);
3771         sess->time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
3772         sess->time2retain = le16_to_cpu(fw_ddb_entry->iscsi_def_time2retain);
3773         sess->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
3774         conn->max_segment_size = le16_to_cpu(fw_ddb_entry->mss);
3775         conn->tcp_xmit_wsf = fw_ddb_entry->tcp_xmt_wsf;
3776         conn->tcp_recv_wsf = fw_ddb_entry->tcp_rcv_wsf;
3777         conn->ipv4_tos = fw_ddb_entry->ipv4_tos;
3778         conn->keepalive_tmo = le16_to_cpu(fw_ddb_entry->ka_timeout);
3779         conn->local_port = le16_to_cpu(fw_ddb_entry->lcl_port);
3780         conn->statsn = le32_to_cpu(fw_ddb_entry->stat_sn);
3781         conn->exp_statsn = le32_to_cpu(fw_ddb_entry->exp_stat_sn);
3782         sess->tsid = le16_to_cpu(fw_ddb_entry->tsid);
3783         COPY_ISID(sess->isid, fw_ddb_entry->isid);
3784 
3785         ddb_link = le16_to_cpu(fw_ddb_entry->ddb_link);
3786         if (ddb_link == DDB_ISNS)
3787                 disc_parent = ISCSI_DISC_PARENT_ISNS;
3788         else if (ddb_link == DDB_NO_LINK)
3789                 disc_parent = ISCSI_DISC_PARENT_UNKNOWN;
3790         else if (ddb_link < MAX_DDB_ENTRIES)
3791                 disc_parent = ISCSI_DISC_PARENT_SENDTGT;
3792         else
3793                 disc_parent = ISCSI_DISC_PARENT_UNKNOWN;
3794 
3795         iscsi_set_param(conn->cls_conn, ISCSI_PARAM_DISCOVERY_PARENT_TYPE,
3796                         iscsi_get_discovery_parent_name(disc_parent), 0);
3797 
3798         iscsi_set_param(conn->cls_conn, ISCSI_PARAM_TARGET_ALIAS,
3799                         (char *)fw_ddb_entry->iscsi_alias, 0);
3800 
3801         options = le16_to_cpu(fw_ddb_entry->options);
3802         if (options & DDB_OPT_IPV6_DEVICE) {
3803                 memset(ip_addr, 0, sizeof(ip_addr));
3804                 sprintf(ip_addr, "%pI6", fw_ddb_entry->link_local_ipv6_addr);
3805                 iscsi_set_param(conn->cls_conn, ISCSI_PARAM_LOCAL_IPADDR,
3806                                 (char *)ip_addr, 0);
3807         }
3808 }
3809 
3810 static void qla4xxx_copy_fwddb_param(struct scsi_qla_host *ha,
3811                                      struct dev_db_entry *fw_ddb_entry,
3812                                      struct iscsi_cls_session *cls_sess,
3813                                      struct iscsi_cls_conn *cls_conn)
3814 {
3815         int buflen = 0;
3816         struct iscsi_session *sess;
3817         struct ddb_entry *ddb_entry;
3818         struct ql4_chap_table chap_tbl;
3819         struct iscsi_conn *conn;
3820         char ip_addr[DDB_IPADDR_LEN];
3821         uint16_t options = 0;
3822 
3823         sess = cls_sess->dd_data;
3824         ddb_entry = sess->dd_data;
3825         conn = cls_conn->dd_data;
3826         memset(&chap_tbl, 0, sizeof(chap_tbl));
3827 
3828         ddb_entry->chap_tbl_idx = le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
3829 
3830         qla4xxx_copy_to_sess_conn_params(conn, sess, fw_ddb_entry);
3831 
3832         sess->def_taskmgmt_tmo = le16_to_cpu(fw_ddb_entry->def_timeout);
3833         conn->persistent_port = le16_to_cpu(fw_ddb_entry->port);
3834 
3835         memset(ip_addr, 0, sizeof(ip_addr));
3836         options = le16_to_cpu(fw_ddb_entry->options);
3837         if (options & DDB_OPT_IPV6_DEVICE) {
3838                 iscsi_set_param(cls_conn, ISCSI_PARAM_PORTAL_TYPE, "ipv6", 4);
3839 
3840                 memset(ip_addr, 0, sizeof(ip_addr));
3841                 sprintf(ip_addr, "%pI6", fw_ddb_entry->ip_addr);
3842         } else {
3843                 iscsi_set_param(cls_conn, ISCSI_PARAM_PORTAL_TYPE, "ipv4", 4);
3844                 sprintf(ip_addr, "%pI4", fw_ddb_entry->ip_addr);
3845         }
3846 
3847         iscsi_set_param(cls_conn, ISCSI_PARAM_PERSISTENT_ADDRESS,
3848                         (char *)ip_addr, buflen);
3849         iscsi_set_param(cls_conn, ISCSI_PARAM_TARGET_NAME,
3850                         (char *)fw_ddb_entry->iscsi_name, buflen);
3851         iscsi_set_param(cls_conn, ISCSI_PARAM_INITIATOR_NAME,
3852                         (char *)ha->name_string, buflen);
3853 
3854         if (ddb_entry->chap_tbl_idx != INVALID_ENTRY) {
3855                 if (!qla4xxx_get_uni_chap_at_index(ha, chap_tbl.name,
3856                                                    chap_tbl.secret,
3857                                                    ddb_entry->chap_tbl_idx)) {
3858                         iscsi_set_param(cls_conn, ISCSI_PARAM_USERNAME,
3859                                         (char *)chap_tbl.name,
3860                                         strlen((char *)chap_tbl.name));
3861                         iscsi_set_param(cls_conn, ISCSI_PARAM_PASSWORD,
3862                                         (char *)chap_tbl.secret,
3863                                         chap_tbl.secret_len);
3864                 }
3865         }
3866 }
3867 
3868 void qla4xxx_update_session_conn_fwddb_param(struct scsi_qla_host *ha,
3869                                              struct ddb_entry *ddb_entry)
3870 {
3871         struct iscsi_cls_session *cls_sess;
3872         struct iscsi_cls_conn *cls_conn;
3873         uint32_t ddb_state;
3874         dma_addr_t fw_ddb_entry_dma;
3875         struct dev_db_entry *fw_ddb_entry;
3876 
3877         fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3878                                           &fw_ddb_entry_dma, GFP_KERNEL);
3879         if (!fw_ddb_entry) {
3880                 ql4_printk(KERN_ERR, ha,
3881                            "%s: Unable to allocate dma buffer\n", __func__);
3882                 goto exit_session_conn_fwddb_param;
3883         }
3884 
3885         if (qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index, fw_ddb_entry,
3886                                     fw_ddb_entry_dma, NULL, NULL, &ddb_state,
3887                                     NULL, NULL, NULL) == QLA_ERROR) {
3888                 DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: failed "
3889                                   "get_ddb_entry for fw_ddb_index %d\n",
3890                                   ha->host_no, __func__,
3891                                   ddb_entry->fw_ddb_index));
3892                 goto exit_session_conn_fwddb_param;
3893         }
3894 
3895         cls_sess = ddb_entry->sess;
3896 
3897         cls_conn = ddb_entry->conn;
3898 
3899         /* Update params */
3900         qla4xxx_copy_fwddb_param(ha, fw_ddb_entry, cls_sess, cls_conn);
3901 
3902 exit_session_conn_fwddb_param:
3903         if (fw_ddb_entry)
3904                 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3905                                   fw_ddb_entry, fw_ddb_entry_dma);
3906 }
3907 
3908 void qla4xxx_update_session_conn_param(struct scsi_qla_host *ha,
3909                                        struct ddb_entry *ddb_entry)
3910 {
3911         struct iscsi_cls_session *cls_sess;
3912         struct iscsi_cls_conn *cls_conn;
3913         struct iscsi_session *sess;
3914         struct iscsi_conn *conn;
3915         uint32_t ddb_state;
3916         dma_addr_t fw_ddb_entry_dma;
3917         struct dev_db_entry *fw_ddb_entry;
3918 
3919         fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3920                                           &fw_ddb_entry_dma, GFP_KERNEL);
3921         if (!fw_ddb_entry) {
3922                 ql4_printk(KERN_ERR, ha,
3923                            "%s: Unable to allocate dma buffer\n", __func__);
3924                 goto exit_session_conn_param;
3925         }
3926 
3927         if (qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index, fw_ddb_entry,
3928                                     fw_ddb_entry_dma, NULL, NULL, &ddb_state,
3929                                     NULL, NULL, NULL) == QLA_ERROR) {
3930                 DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: failed "
3931                                   "get_ddb_entry for fw_ddb_index %d\n",
3932                                   ha->host_no, __func__,
3933                                   ddb_entry->fw_ddb_index));
3934                 goto exit_session_conn_param;
3935         }
3936 
3937         cls_sess = ddb_entry->sess;
3938         sess = cls_sess->dd_data;
3939 
3940         cls_conn = ddb_entry->conn;
3941         conn = cls_conn->dd_data;
3942 
3943         /* Update timers after login */
3944         ddb_entry->default_relogin_timeout =
3945                 (le16_to_cpu(fw_ddb_entry->def_timeout) > LOGIN_TOV) &&
3946                  (le16_to_cpu(fw_ddb_entry->def_timeout) < LOGIN_TOV * 10) ?
3947                  le16_to_cpu(fw_ddb_entry->def_timeout) : LOGIN_TOV;
3948         ddb_entry->default_time2wait =
3949                                 le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
3950 
3951         /* Update params */
3952         ddb_entry->chap_tbl_idx = le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
3953         qla4xxx_copy_to_sess_conn_params(conn, sess, fw_ddb_entry);
3954 
3955         memcpy(sess->initiatorname, ha->name_string,
3956                min(sizeof(ha->name_string), sizeof(sess->initiatorname)));
3957 
3958 exit_session_conn_param:
3959         if (fw_ddb_entry)
3960                 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3961                                   fw_ddb_entry, fw_ddb_entry_dma);
3962 }
3963 
3964 /*
3965  * Timer routines
3966  */
3967 
3968 static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
3969                                 unsigned long interval)
3970 {
3971         DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
3972                      __func__, ha->host->host_no));
3973         init_timer(&ha->timer);
3974         ha->timer.expires = jiffies + interval * HZ;
3975         ha->timer.data = (unsigned long)ha;
3976         ha->timer.function = (void (*)(unsigned long))func;
3977         add_timer(&ha->timer);
3978         ha->timer_active = 1;
3979 }
3980 
3981 static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
3982 {
3983         del_timer_sync(&ha->timer);
3984         ha->timer_active = 0;
3985 }
3986 
3987 /***
3988  * qla4xxx_mark_device_missing - blocks the session
3989  * @cls_session: Pointer to the session to be blocked
3990  * @ddb_entry: Pointer to device database entry
3991  *
3992  * This routine marks a device missing and close connection.
3993  **/
3994 void qla4xxx_mark_device_missing(struct iscsi_cls_session *cls_session)
3995 {
3996         iscsi_block_session(cls_session);
3997 }
3998 
3999 /**
4000  * qla4xxx_mark_all_devices_missing - mark all devices as missing.
4001  * @ha: Pointer to host adapter structure.
4002  *
4003  * This routine marks a device missing and resets the relogin retry count.
4004  **/
4005 void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
4006 {
4007         iscsi_host_for_each_session(ha->host, qla4xxx_mark_device_missing);
4008 }
4009 
4010 static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
4011                                        struct ddb_entry *ddb_entry,
4012                                        struct scsi_cmnd *cmd)
4013 {
4014         struct srb *srb;
4015 
4016         srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
4017         if (!srb)
4018                 return srb;
4019 
4020         kref_init(&srb->srb_ref);
4021         srb->ha = ha;
4022         srb->ddb = ddb_entry;
4023         srb->cmd = cmd;
4024         srb->flags = 0;
4025         CMD_SP(cmd) = (void *)srb;
4026 
4027         return srb;
4028 }
4029 
4030 static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
4031 {
4032         struct scsi_cmnd *cmd = srb->cmd;
4033 
4034         if (srb->flags & SRB_DMA_VALID) {
4035                 scsi_dma_unmap(cmd);
4036                 srb->flags &= ~SRB_DMA_VALID;
4037         }
4038         CMD_SP(cmd) = NULL;
4039 }
4040 
4041 void qla4xxx_srb_compl(struct kref *ref)
4042 {
4043         struct srb *srb = container_of(ref, struct srb, srb_ref);
4044         struct scsi_cmnd *cmd = srb->cmd;
4045         struct scsi_qla_host *ha = srb->ha;
4046 
4047         qla4xxx_srb_free_dma(ha, srb);
4048 
4049         mempool_free(srb, ha->srb_mempool);
4050 
4051         cmd->scsi_done(cmd);
4052 }
4053 
4054 /**
4055  * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
4056  * @host: scsi host
4057  * @cmd: Pointer to Linux's SCSI command structure
4058  *
4059  * Remarks:
4060  * This routine is invoked by Linux to send a SCSI command to the driver.
4061  * The mid-level driver tries to ensure that queuecommand never gets
4062  * invoked concurrently with itself or the interrupt handler (although
4063  * the interrupt handler may call this routine as part of request-
4064  * completion handling).   Unfortunely, it sometimes calls the scheduler
4065  * in interrupt context which is a big NO! NO!.
4066  **/
4067 static int qla4xxx_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
4068 {
4069         struct scsi_qla_host *ha = to_qla_host(host);
4070         struct ddb_entry *ddb_entry = cmd->device->hostdata;
4071         struct iscsi_cls_session *sess = ddb_entry->sess;
4072         struct srb *srb;
4073         int rval;
4074 
4075         if (test_bit(AF_EEH_BUSY, &ha->flags)) {
4076                 if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
4077                         cmd->result = DID_NO_CONNECT << 16;
4078                 else
4079                         cmd->result = DID_REQUEUE << 16;
4080                 goto qc_fail_command;
4081         }
4082 
4083         if (!sess) {
4084                 cmd->result = DID_IMM_RETRY << 16;
4085                 goto qc_fail_command;
4086         }
4087 
4088         rval = iscsi_session_chkready(sess);
4089         if (rval) {
4090                 cmd->result = rval;
4091                 goto qc_fail_command;
4092         }
4093 
4094         if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
4095             test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
4096             test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
4097             test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
4098             test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
4099             !test_bit(AF_ONLINE, &ha->flags) ||
4100             !test_bit(AF_LINK_UP, &ha->flags) ||
4101             test_bit(AF_LOOPBACK, &ha->flags) ||
4102             test_bit(DPC_POST_IDC_ACK, &ha->dpc_flags) ||
4103             test_bit(DPC_RESTORE_ACB, &ha->dpc_flags) ||
4104             test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
4105                 goto qc_host_busy;
4106 
4107         srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd);
4108         if (!srb)
4109                 goto qc_host_busy;
4110 
4111         rval = qla4xxx_send_command_to_isp(ha, srb);
4112         if (rval != QLA_SUCCESS)
4113                 goto qc_host_busy_free_sp;
4114 
4115         return 0;
4116 
4117 qc_host_busy_free_sp:
4118         qla4xxx_srb_free_dma(ha, srb);
4119         mempool_free(srb, ha->srb_mempool);
4120 
4121 qc_host_busy:
4122         return SCSI_MLQUEUE_HOST_BUSY;
4123 
4124 qc_fail_command:
4125         cmd->scsi_done(cmd);
4126 
4127         return 0;
4128 }
4129 
4130 /**
4131  * qla4xxx_mem_free - frees memory allocated to adapter
4132  * @ha: Pointer to host adapter structure.
4133  *
4134  * Frees memory previously allocated by qla4xxx_mem_alloc
4135  **/
4136 static void qla4xxx_mem_free(struct scsi_qla_host *ha)
4137 {
4138         if (ha->queues)
4139                 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
4140                                   ha->queues_dma);
4141 
4142          if (ha->fw_dump)
4143                 vfree(ha->fw_dump);
4144 
4145         ha->queues_len = 0;
4146         ha->queues = NULL;
4147         ha->queues_dma = 0;
4148         ha->request_ring = NULL;
4149         ha->request_dma = 0;
4150         ha->response_ring = NULL;
4151         ha->response_dma = 0;
4152         ha->shadow_regs = NULL;
4153         ha->shadow_regs_dma = 0;
4154         ha->fw_dump = NULL;
4155         ha->fw_dump_size = 0;
4156 
4157         /* Free srb pool. */
4158         if (ha->srb_mempool)
4159                 mempool_destroy(ha->srb_mempool);
4160 
4161         ha->srb_mempool = NULL;
4162 
4163         if (ha->chap_dma_pool)
4164                 dma_pool_destroy(ha->chap_dma_pool);
4165 
4166         if (ha->chap_list)
4167                 vfree(ha->chap_list);
4168         ha->chap_list = NULL;
4169 
4170         if (ha->fw_ddb_dma_pool)
4171                 dma_pool_destroy(ha->fw_ddb_dma_pool);
4172 
4173         /* release io space registers  */
4174         if (is_qla8022(ha)) {
4175                 if (ha->nx_pcibase)
4176                         iounmap(
4177                             (struct device_reg_82xx __iomem *)ha->nx_pcibase);
4178         } else if (is_qla8032(ha) || is_qla8042(ha)) {
4179                 if (ha->nx_pcibase)
4180                         iounmap(
4181                             (struct device_reg_83xx __iomem *)ha->nx_pcibase);
4182         } else if (ha->reg) {
4183                 iounmap(ha->reg);
4184         }
4185 
4186         if (ha->reset_tmplt.buff)
4187                 vfree(ha->reset_tmplt.buff);
4188 
4189         pci_release_regions(ha->pdev);
4190 }
4191 
4192 /**
4193  * qla4xxx_mem_alloc - allocates memory for use by adapter.
4194  * @ha: Pointer to host adapter structure
4195  *
4196  * Allocates DMA memory for request and response queues. Also allocates memory
4197  * for srbs.
4198  **/
4199 static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
4200 {
4201         unsigned long align;
4202 
4203         /* Allocate contiguous block of DMA memory for queues. */
4204         ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
4205                           (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
4206                           sizeof(struct shadow_regs) +
4207                           MEM_ALIGN_VALUE +
4208                           (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
4209         ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
4210                                         &ha->queues_dma, GFP_KERNEL);
4211         if (ha->queues == NULL) {
4212                 ql4_printk(KERN_WARNING, ha,
4213                     "Memory Allocation failed - queues.\n");
4214 
4215                 goto mem_alloc_error_exit;
4216         }
4217         memset(ha->queues, 0, ha->queues_len);
4218 
4219         /*
4220          * As per RISC alignment requirements -- the bus-address must be a
4221          * multiple of the request-ring size (in bytes).
4222          */
4223         align = 0;
4224         if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
4225                 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
4226                                            (MEM_ALIGN_VALUE - 1));
4227 
4228         /* Update request and response queue pointers. */
4229         ha->request_dma = ha->queues_dma + align;
4230         ha->request_ring = (struct queue_entry *) (ha->queues + align);
4231         ha->response_dma = ha->queues_dma + align +
4232                 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
4233         ha->response_ring = (struct queue_entry *) (ha->queues + align +
4234                                                     (REQUEST_QUEUE_DEPTH *
4235                                                      QUEUE_SIZE));
4236         ha->shadow_regs_dma = ha->queues_dma + align +
4237                 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
4238                 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
4239         ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
4240                                                   (REQUEST_QUEUE_DEPTH *
4241                                                    QUEUE_SIZE) +
4242                                                   (RESPONSE_QUEUE_DEPTH *
4243                                                    QUEUE_SIZE));
4244 
4245         /* Allocate memory for srb pool. */
4246         ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
4247                                          mempool_free_slab, srb_cachep);
4248         if (ha->srb_mempool == NULL) {
4249                 ql4_printk(KERN_WARNING, ha,
4250                     "Memory Allocation failed - SRB Pool.\n");
4251 
4252                 goto mem_alloc_error_exit;
4253         }
4254 
4255         ha->chap_dma_pool = dma_pool_create("ql4_chap", &ha->pdev->dev,
4256                                             CHAP_DMA_BLOCK_SIZE, 8, 0);
4257 
4258         if (ha->chap_dma_pool == NULL) {
4259                 ql4_printk(KERN_WARNING, ha,
4260                     "%s: chap_dma_pool allocation failed..\n", __func__);
4261                 goto mem_alloc_error_exit;
4262         }
4263 
4264         ha->fw_ddb_dma_pool = dma_pool_create("ql4_fw_ddb", &ha->pdev->dev,
4265                                               DDB_DMA_BLOCK_SIZE, 8, 0);
4266 
4267         if (ha->fw_ddb_dma_pool == NULL) {
4268                 ql4_printk(KERN_WARNING, ha,
4269                            "%s: fw_ddb_dma_pool allocation failed..\n",
4270                            __func__);
4271                 goto mem_alloc_error_exit;
4272         }
4273 
4274         return QLA_SUCCESS;
4275 
4276 mem_alloc_error_exit:
4277         qla4xxx_mem_free(ha);
4278         return QLA_ERROR;
4279 }
4280 
4281 /**
4282  * qla4_8xxx_check_temp - Check the ISP82XX temperature.
4283  * @ha: adapter block pointer.
4284  *
4285  * Note: The caller should not hold the idc lock.
4286  **/
4287 static int qla4_8xxx_check_temp(struct scsi_qla_host *ha)
4288 {
4289         uint32_t temp, temp_state, temp_val;
4290         int status = QLA_SUCCESS;
4291 
4292         temp = qla4_8xxx_rd_direct(ha, QLA8XXX_CRB_TEMP_STATE);
4293 
4294         temp_state = qla82xx_get_temp_state(temp);
4295         temp_val = qla82xx_get_temp_val(temp);
4296 
4297         if (temp_state == QLA82XX_TEMP_PANIC) {
4298                 ql4_printk(KERN_WARNING, ha, "Device temperature %d degrees C"
4299                            " exceeds maximum allowed. Hardware has been shut"
4300                            " down.\n", temp_val);
4301                 status = QLA_ERROR;
4302         } else if (temp_state == QLA82XX_TEMP_WARN) {
4303                 if (ha->temperature == QLA82XX_TEMP_NORMAL)
4304                         ql4_printk(KERN_WARNING, ha, "Device temperature %d"
4305                                    " degrees C exceeds operating range."
4306                                    " Immediate action needed.\n", temp_val);
4307         } else {
4308                 if (ha->temperature == QLA82XX_TEMP_WARN)
4309                         ql4_printk(KERN_INFO, ha, "Device temperature is"
4310                                    " now %d degrees C in normal range.\n",
4311                                    temp_val);
4312         }
4313         ha->temperature = temp_state;
4314         return status;
4315 }
4316 
4317 /**
4318  * qla4_8xxx_check_fw_alive  - Check firmware health
4319  * @ha: Pointer to host adapter structure.
4320  *
4321  * Context: Interrupt
4322  **/
4323 static int qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
4324 {
4325         uint32_t fw_heartbeat_counter;
4326         int status = QLA_SUCCESS;
4327 
4328         fw_heartbeat_counter = qla4_8xxx_rd_direct(ha,
4329                                                    QLA8XXX_PEG_ALIVE_COUNTER);
4330         /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
4331         if (fw_heartbeat_counter == 0xffffffff) {
4332                 DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
4333                     "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
4334                     ha->host_no, __func__));
4335                 return status;
4336         }
4337 
4338         if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
4339                 ha->seconds_since_last_heartbeat++;
4340                 /* FW not alive after 2 seconds */
4341                 if (ha->seconds_since_last_heartbeat == 2) {
4342                         ha->seconds_since_last_heartbeat = 0;
4343                         qla4_8xxx_dump_peg_reg(ha);
4344                         status = QLA_ERROR;
4345                 }
4346         } else
4347                 ha->seconds_since_last_heartbeat = 0;
4348 
4349         ha->fw_heartbeat_counter = fw_heartbeat_counter;
4350         return status;
4351 }
4352 
4353 static void qla4_8xxx_process_fw_error(struct scsi_qla_host *ha)
4354 {
4355         uint32_t halt_status;
4356         int halt_status_unrecoverable = 0;
4357 
4358         halt_status = qla4_8xxx_rd_direct(ha, QLA8XXX_PEG_HALT_STATUS1);
4359 
4360         if (is_qla8022(ha)) {
4361                 ql4_printk(KERN_INFO, ha, "%s: disabling pause transmit on port 0 & 1.\n",
4362                            __func__);
4363                 qla4_82xx_wr_32(ha, QLA82XX_CRB_NIU + 0x98,
4364                                 CRB_NIU_XG_PAUSE_CTL_P0 |
4365                                 CRB_NIU_XG_PAUSE_CTL_P1);
4366 
4367                 if (QLA82XX_FWERROR_CODE(halt_status) == 0x67)
4368                         ql4_printk(KERN_ERR, ha, "%s: Firmware aborted with error code 0x00006700. Device is being reset\n",
4369                                    __func__);
4370                 if (halt_status & HALT_STATUS_UNRECOVERABLE)
4371                         halt_status_unrecoverable = 1;
4372         } else if (is_qla8032(ha) || is_qla8042(ha)) {
4373                 if (halt_status & QLA83XX_HALT_STATUS_FW_RESET)
4374                         ql4_printk(KERN_ERR, ha, "%s: Firmware error detected device is being reset\n",
4375                                    __func__);
4376                 else if (halt_status & QLA83XX_HALT_STATUS_UNRECOVERABLE)
4377                         halt_status_unrecoverable = 1;
4378         }
4379 
4380         /*
4381          * Since we cannot change dev_state in interrupt context,
4382          * set appropriate DPC flag then wakeup DPC
4383          */
4384         if (halt_status_unrecoverable) {
4385                 set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
4386         } else {
4387                 ql4_printk(KERN_INFO, ha, "%s: detect abort needed!\n",
4388                            __func__);
4389                 set_bit(DPC_RESET_HA, &ha->dpc_flags);
4390         }
4391         qla4xxx_mailbox_premature_completion(ha);
4392         qla4xxx_wake_dpc(ha);
4393 }
4394 
4395 /**
4396  * qla4_8xxx_watchdog - Poll dev state
4397  * @ha: Pointer to host adapter structure.
4398  *
4399  * Context: Interrupt
4400  **/
4401 void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
4402 {
4403         uint32_t dev_state;
4404         uint32_t idc_ctrl;
4405 
4406         if (is_qla8032(ha) &&
4407             (qla4_83xx_is_detached(ha) == QLA_SUCCESS))
4408                 WARN_ONCE(1, "%s: iSCSI function %d marked invisible\n",
4409                           __func__, ha->func_num);
4410 
4411         /* don't poll if reset is going on */
4412         if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
4413             test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
4414             test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags))) {
4415                 dev_state = qla4_8xxx_rd_direct(ha, QLA8XXX_CRB_DEV_STATE);
4416 
4417                 if (qla4_8xxx_check_temp(ha)) {
4418                         if (is_qla8022(ha)) {
4419                                 ql4_printk(KERN_INFO, ha, "disabling pause transmit on port 0 & 1.\n");
4420                                 qla4_82xx_wr_32(ha, QLA82XX_CRB_NIU + 0x98,
4421                                                 CRB_NIU_XG_PAUSE_CTL_P0 |
4422                                                 CRB_NIU_XG_PAUSE_CTL_P1);
4423                         }
4424                         set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
4425                         qla4xxx_wake_dpc(ha);
4426                 } else if (dev_state == QLA8XXX_DEV_NEED_RESET &&
4427                            !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
4428 
4429                         ql4_printk(KERN_INFO, ha, "%s: HW State: NEED RESET!\n",
4430                                    __func__);
4431 
4432                         if (is_qla8032(ha) || is_qla8042(ha)) {
4433                                 idc_ctrl = qla4_83xx_rd_reg(ha,
4434                                                         QLA83XX_IDC_DRV_CTRL);
4435                                 if (!(idc_ctrl & GRACEFUL_RESET_BIT1)) {
4436                                         ql4_printk(KERN_INFO, ha, "%s: Graceful reset bit is not set\n",
4437                                                    __func__);
4438                                         qla4xxx_mailbox_premature_completion(
4439                                                                             ha);
4440                                 }
4441                         }
4442 
4443                         if ((is_qla8032(ha) || is_qla8042(ha)) ||
4444                             (is_qla8022(ha) && !ql4xdontresethba)) {
4445                                 set_bit(DPC_RESET_HA, &ha->dpc_flags);
4446                                 qla4xxx_wake_dpc(ha);
4447                         }
4448                 } else if (dev_state == QLA8XXX_DEV_NEED_QUIESCENT &&
4449                     !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
4450                         ql4_printk(KERN_INFO, ha, "%s: HW State: NEED QUIES!\n",
4451                             __func__);
4452                         set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
4453                         qla4xxx_wake_dpc(ha);
4454                 } else  {
4455                         /* Check firmware health */
4456                         if (qla4_8xxx_check_fw_alive(ha))
4457                                 qla4_8xxx_process_fw_error(ha);
4458                 }
4459         }
4460 }
4461 
4462 static void qla4xxx_check_relogin_flash_ddb(struct iscsi_cls_session *cls_sess)
4463 {
4464         struct iscsi_session *sess;
4465         struct ddb_entry *ddb_entry;
4466         struct scsi_qla_host *ha;
4467 
4468         sess = cls_sess->dd_data;
4469         ddb_entry = sess->dd_data;
4470         ha = ddb_entry->ha;
4471 
4472         if (!(ddb_entry->ddb_type == FLASH_DDB))
4473                 return;
4474 
4475         if (adapter_up(ha) && !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
4476             !iscsi_is_session_online(cls_sess)) {
4477                 if (atomic_read(&ddb_entry->retry_relogin_timer) !=
4478                     INVALID_ENTRY) {
4479                         if (atomic_read(&ddb_entry->retry_relogin_timer) ==
4480                                         0) {
4481                                 atomic_set(&ddb_entry->retry_relogin_timer,
4482                                            INVALID_ENTRY);
4483                                 set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
4484                                 set_bit(DF_RELOGIN, &ddb_entry->flags);
4485                                 DEBUG2(ql4_printk(KERN_INFO, ha,
4486                                        "%s: index [%d] login device\n",
4487                                         __func__, ddb_entry->fw_ddb_index));
4488                         } else
4489                                 atomic_dec(&ddb_entry->retry_relogin_timer);
4490                 }
4491         }
4492 
4493         /* Wait for relogin to timeout */
4494         if (atomic_read(&ddb_entry->relogin_timer) &&
4495             (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
4496                 /*
4497                  * If the relogin times out and the device is
4498                  * still NOT ONLINE then try and relogin again.
4499                  */
4500                 if (!iscsi_is_session_online(cls_sess)) {
4501                         /* Reset retry relogin timer */
4502                         atomic_inc(&ddb_entry->relogin_retry_count);
4503                         DEBUG2(ql4_printk(KERN_INFO, ha,
4504                                 "%s: index[%d] relogin timed out-retrying"
4505                                 " relogin (%d), retry (%d)\n", __func__,
4506                                 ddb_entry->fw_ddb_index,
4507                                 atomic_read(&ddb_entry->relogin_retry_count),
4508                                 ddb_entry->default_time2wait + 4));
4509                         set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
4510                         atomic_set(&ddb_entry->retry_relogin_timer,
4511                                    ddb_entry->default_time2wait + 4);
4512                 }
4513         }
4514 }
4515 
4516 /**
4517  * qla4xxx_timer - checks every second for work to do.
4518  * @ha: Pointer to host adapter structure.
4519  **/
4520 static void qla4xxx_timer(struct scsi_qla_host *ha)
4521 {
4522         int start_dpc = 0;
4523         uint16_t w;
4524 
4525         iscsi_host_for_each_session(ha->host, qla4xxx_check_relogin_flash_ddb);
4526 
4527         /* If we are in the middle of AER/EEH processing
4528          * skip any processing and reschedule the timer
4529          */
4530         if (test_bit(AF_EEH_BUSY, &ha->flags)) {
4531                 mod_timer(&ha->timer, jiffies + HZ);
4532                 return;
4533         }
4534 
4535         /* Hardware read to trigger an EEH error during mailbox waits. */
4536         if (!pci_channel_offline(ha->pdev))
4537                 pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
4538 
4539         if (is_qla80XX(ha))
4540                 qla4_8xxx_watchdog(ha);
4541 
4542         if (is_qla40XX(ha)) {
4543                 /* Check for heartbeat interval. */
4544                 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
4545                     ha->heartbeat_interval != 0) {
4546                         ha->seconds_since_last_heartbeat++;
4547                         if (ha->seconds_since_last_heartbeat >
4548                             ha->heartbeat_interval + 2)
4549                                 set_bit(DPC_RESET_HA, &ha->dpc_flags);
4550                 }
4551         }
4552 
4553         /* Process any deferred work. */
4554         if (!list_empty(&ha->work_list))
4555                 start_dpc++;
4556 
4557         /* Wakeup the dpc routine for this adapter, if needed. */
4558         if (start_dpc ||
4559              test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
4560              test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
4561              test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
4562              test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
4563              test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
4564              test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
4565              test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
4566              test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
4567              test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
4568              test_bit(DPC_AEN, &ha->dpc_flags)) {
4569                 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
4570                               " - dpc flags = 0x%lx\n",
4571                               ha->host_no, __func__, ha->dpc_flags));
4572                 qla4xxx_wake_dpc(ha);
4573         }
4574 
4575         /* Reschedule timer thread to call us back in one second */
4576         mod_timer(&ha->timer, jiffies + HZ);
4577 
4578         DEBUG2(ha->seconds_since_last_intr++);
4579 }
4580 
4581 /**
4582  * qla4xxx_cmd_wait - waits for all outstanding commands to complete
4583  * @ha: Pointer to host adapter structure.
4584  *
4585  * This routine stalls the driver until all outstanding commands are returned.
4586  * Caller must release the Hardware Lock prior to calling this routine.
4587  **/
4588 static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
4589 {
4590         uint32_t index = 0;
4591         unsigned long flags;
4592         struct scsi_cmnd *cmd;
4593         unsigned long wtime;
4594         uint32_t wtmo;
4595 
4596         if (is_qla40XX(ha))
4597                 wtmo = WAIT_CMD_TOV;
4598         else
4599                 wtmo = ha->nx_reset_timeout / 2;
4600 
4601         wtime = jiffies + (wtmo * HZ);
4602 
4603         DEBUG2(ql4_printk(KERN_INFO, ha,
4604                           "Wait up to %u seconds for cmds to complete\n",
4605                           wtmo));
4606 
4607         while (!time_after_eq(jiffies, wtime)) {
4608                 spin_lock_irqsave(&ha->hardware_lock, flags);
4609                 /* Find a command that hasn't completed. */
4610                 for (index = 0; index < ha->host->can_queue; index++) {
4611                         cmd = scsi_host_find_tag(ha->host, index);
4612                         /*
4613                          * We cannot just check if the index is valid,
4614                          * becase if we are run from the scsi eh, then
4615                          * the scsi/block layer is going to prevent
4616                          * the tag from being released.
4617                          */
4618                         if (cmd != NULL && CMD_SP(cmd))
4619                                 break;
4620                 }
4621                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4622 
4623                 /* If No Commands are pending, wait is complete */
4624                 if (index == ha->host->can_queue)
4625                         return QLA_SUCCESS;
4626 
4627                 msleep(1000);
4628         }
4629         /* If we timed out on waiting for commands to come back
4630          * return ERROR. */
4631         return QLA_ERROR;
4632 }
4633 
4634 int qla4xxx_hw_reset(struct scsi_qla_host *ha)
4635 {
4636         uint32_t ctrl_status;
4637         unsigned long flags = 0;
4638 
4639         DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
4640 
4641         if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
4642                 return QLA_ERROR;
4643 
4644         spin_lock_irqsave(&ha->hardware_lock, flags);
4645 
4646         /*
4647          * If the SCSI Reset Interrupt bit is set, clear it.
4648          * Otherwise, the Soft Reset won't work.
4649          */
4650         ctrl_status = readw(&ha->reg->ctrl_status);
4651         if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
4652                 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
4653 
4654         /* Issue Soft Reset */
4655         writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
4656         readl(&ha->reg->ctrl_status);
4657 
4658         spin_unlock_irqrestore(&ha->hardware_lock, flags);
4659         return QLA_SUCCESS;
4660 }
4661 
4662 /**
4663  * qla4xxx_soft_reset - performs soft reset.
4664  * @ha: Pointer to host adapter structure.
4665  **/
4666 int qla4xxx_soft_reset(struct scsi_qla_host *ha)
4667 {
4668         uint32_t max_wait_time;
4669         unsigned long flags = 0;
4670         int status;
4671         uint32_t ctrl_status;
4672 
4673         status = qla4xxx_hw_reset(ha);
4674         if (status != QLA_SUCCESS)
4675                 return status;
4676 
4677         status = QLA_ERROR;
4678         /* Wait until the Network Reset Intr bit is cleared */
4679         max_wait_time = RESET_INTR_TOV;
4680         do {
4681                 spin_lock_irqsave(&ha->hardware_lock, flags);
4682                 ctrl_status = readw(&ha->reg->ctrl_status);
4683                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4684 
4685                 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
4686                         break;
4687 
4688                 msleep(1000);
4689         } while ((--max_wait_time));
4690 
4691         if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
4692                 DEBUG2(printk(KERN_WARNING
4693                               "scsi%ld: Network Reset Intr not cleared by "
4694                               "Network function, clearing it now!\n",
4695                               ha->host_no));
4696                 spin_lock_irqsave(&ha->hardware_lock, flags);
4697                 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
4698                 readl(&ha->reg->ctrl_status);
4699                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4700         }
4701 
4702         /* Wait until the firmware tells us the Soft Reset is done */
4703         max_wait_time = SOFT_RESET_TOV;
4704         do {
4705                 spin_lock_irqsave(&ha->hardware_lock, flags);
4706                 ctrl_status = readw(&ha->reg->ctrl_status);
4707                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4708 
4709                 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
4710                         status = QLA_SUCCESS;
4711                         break;
4712                 }
4713 
4714                 msleep(1000);
4715         } while ((--max_wait_time));
4716 
4717         /*
4718          * Also, make sure that the SCSI Reset Interrupt bit has been cleared
4719          * after the soft reset has taken place.
4720          */
4721         spin_lock_irqsave(&ha->hardware_lock, flags);
4722         ctrl_status = readw(&ha->reg->ctrl_status);
4723         if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
4724                 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
4725                 readl(&ha->reg->ctrl_status);
4726         }
4727         spin_unlock_irqrestore(&ha->hardware_lock, flags);
4728 
4729         /* If soft reset fails then most probably the bios on other
4730          * function is also enabled.
4731          * Since the initialization is sequential the other fn
4732          * wont be able to acknowledge the soft reset.
4733          * Issue a force soft reset to workaround this scenario.
4734          */
4735         if (max_wait_time == 0) {
4736                 /* Issue Force Soft Reset */
4737                 spin_lock_irqsave(&ha->hardware_lock, flags);
4738                 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
4739                 readl(&ha->reg->ctrl_status);
4740                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4741                 /* Wait until the firmware tells us the Soft Reset is done */
4742                 max_wait_time = SOFT_RESET_TOV;
4743                 do {
4744                         spin_lock_irqsave(&ha->hardware_lock, flags);
4745                         ctrl_status = readw(&ha->reg->ctrl_status);
4746                         spin_unlock_irqrestore(&ha->hardware_lock, flags);
4747 
4748                         if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
4749                                 status = QLA_SUCCESS;
4750                                 break;
4751                         }
4752 
4753                         msleep(1000);
4754                 } while ((--max_wait_time));
4755         }
4756 
4757         return status;
4758 }
4759 
4760 /**
4761  * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
4762  * @ha: Pointer to host adapter structure.
4763  * @res: returned scsi status
4764  *
4765  * This routine is called just prior to a HARD RESET to return all
4766  * outstanding commands back to the Operating System.
4767  * Caller should make sure that the following locks are released
4768  * before this calling routine: Hardware lock, and io_request_lock.
4769  **/
4770 static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
4771 {
4772         struct srb *srb;
4773         int i;
4774         unsigned long flags;
4775 
4776         spin_lock_irqsave(&ha->hardware_lock, flags);
4777         for (i = 0; i < ha->host->can_queue; i++) {
4778                 srb = qla4xxx_del_from_active_array(ha, i);
4779                 if (srb != NULL) {
4780                         srb->cmd->result = res;
4781                         kref_put(&srb->srb_ref, qla4xxx_srb_compl);
4782                 }
4783         }
4784         spin_unlock_irqrestore(&ha->hardware_lock, flags);
4785 }
4786 
4787 void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
4788 {
4789         clear_bit(AF_ONLINE, &ha->flags);
4790 
4791         /* Disable the board */
4792         ql4_printk(KERN_INFO, ha, "Disabling the board\n");
4793 
4794         qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
4795         qla4xxx_mark_all_devices_missing(ha);
4796         clear_bit(AF_INIT_DONE, &ha->flags);
4797 }
4798 
4799 static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session)
4800 {
4801         struct iscsi_session *sess;
4802         struct ddb_entry *ddb_entry;
4803 
4804         sess = cls_session->dd_data;
4805         ddb_entry = sess->dd_data;
4806         ddb_entry->fw_ddb_device_state = DDB_DS_SESSION_FAILED;
4807 
4808         if (ddb_entry->ddb_type == FLASH_DDB)
4809                 iscsi_block_session(ddb_entry->sess);
4810         else
4811                 iscsi_session_failure(cls_session->dd_data,
4812                                       ISCSI_ERR_CONN_FAILED);
4813 }
4814 
4815 /**
4816  * qla4xxx_recover_adapter - recovers adapter after a fatal error
4817  * @ha: Pointer to host adapter structure.
4818  **/
4819 static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
4820 {
4821         int status = QLA_ERROR;
4822         uint8_t reset_chip = 0;
4823         uint32_t dev_state;
4824         unsigned long wait;
4825 
4826         /* Stall incoming I/O until we are done */
4827         scsi_block_requests(ha->host);
4828         clear_bit(AF_ONLINE, &ha->flags);
4829         clear_bit(AF_LINK_UP, &ha->flags);
4830 
4831         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
4832 
4833         set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
4834 
4835         if ((is_qla8032(ha) || is_qla8042(ha)) &&
4836             !test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
4837                 ql4_printk(KERN_INFO, ha, "%s: disabling pause transmit on port 0 & 1.\n",
4838                            __func__);
4839                 /* disable pause frame for ISP83xx */
4840                 qla4_83xx_disable_pause(ha);
4841         }
4842 
4843         iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
4844 
4845         if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
4846                 reset_chip = 1;
4847 
4848         /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
4849          * do not reset adapter, jump to initialize_adapter */
4850         if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
4851                 status = QLA_SUCCESS;
4852                 goto recover_ha_init_adapter;
4853         }
4854 
4855         /* For the ISP-8xxx adapter, issue a stop_firmware if invoked
4856          * from eh_host_reset or ioctl module */
4857         if (is_qla80XX(ha) && !reset_chip &&
4858             test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
4859 
4860                 DEBUG2(ql4_printk(KERN_INFO, ha,
4861                     "scsi%ld: %s - Performing stop_firmware...\n",
4862                     ha->host_no, __func__));
4863                 status = ha->isp_ops->reset_firmware(ha);
4864                 if (status == QLA_SUCCESS) {
4865                         if (!test_bit(AF_FW_RECOVERY, &ha->flags))
4866                                 qla4xxx_cmd_wait(ha);
4867 
4868                         ha->isp_ops->disable_intrs(ha);
4869                         qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
4870                         qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
4871                 } else {
4872                         /* If the stop_firmware fails then
4873                          * reset the entire chip */
4874                         reset_chip = 1;
4875                         clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
4876                         set_bit(DPC_RESET_HA, &ha->dpc_flags);
4877                 }
4878         }
4879 
4880         /* Issue full chip reset if recovering from a catastrophic error,
4881          * or if stop_firmware fails for ISP-8xxx.
4882          * This is the default case for ISP-4xxx */
4883         if (is_qla40XX(ha) || reset_chip) {
4884                 if (is_qla40XX(ha))
4885                         goto chip_reset;
4886 
4887                 /* Check if 8XXX firmware is alive or not
4888                  * We may have arrived here from NEED_RESET
4889                  * detection only */
4890                 if (test_bit(AF_FW_RECOVERY, &ha->flags))
4891                         goto chip_reset;
4892 
4893                 wait = jiffies + (FW_ALIVE_WAIT_TOV * HZ);
4894                 while (time_before(jiffies, wait)) {
4895                         if (qla4_8xxx_check_fw_alive(ha)) {
4896                                 qla4xxx_mailbox_premature_completion(ha);
4897                                 break;
4898                         }
4899 
4900                         set_current_state(TASK_UNINTERRUPTIBLE);
4901                         schedule_timeout(HZ);
4902                 }
4903 chip_reset:
4904                 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
4905                         qla4xxx_cmd_wait(ha);
4906 
4907                 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
4908                 DEBUG2(ql4_printk(KERN_INFO, ha,
4909                     "scsi%ld: %s - Performing chip reset..\n",
4910                     ha->host_no, __func__));
4911                 status = ha->isp_ops->reset_chip(ha);
4912                 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
4913         }
4914 
4915         /* Flush any pending ddb changed AENs */
4916         qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
4917 
4918 recover_ha_init_adapter:
4919         /* Upon successful firmware/chip reset, re-initialize the adapter */
4920         if (status == QLA_SUCCESS) {
4921                 /* For ISP-4xxx, force function 1 to always initialize
4922                  * before function 3 to prevent both funcions from
4923                  * stepping on top of the other */
4924                 if (is_qla40XX(ha) && (ha->mac_index == 3))
4925                         ssleep(6);
4926 
4927                 /* NOTE: AF_ONLINE flag set upon successful completion of
4928                  * qla4xxx_initialize_adapter */
4929                 status = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
4930                 if (is_qla80XX(ha) && (status == QLA_ERROR)) {
4931                         status = qla4_8xxx_check_init_adapter_retry(ha);
4932                         if (status == QLA_ERROR) {
4933                                 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Don't retry recover adapter\n",
4934                                            ha->host_no, __func__);
4935                                 qla4xxx_dead_adapter_cleanup(ha);
4936                                 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
4937                                 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
4938                                 clear_bit(DPC_RESET_HA_FW_CONTEXT,
4939                                           &ha->dpc_flags);
4940                                 goto exit_recover;
4941                         }
4942                 }
4943         }
4944 
4945         /* Retry failed adapter initialization, if necessary
4946          * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
4947          * case to prevent ping-pong resets between functions */
4948         if (!test_bit(AF_ONLINE, &ha->flags) &&
4949             !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
4950                 /* Adapter initialization failed, see if we can retry
4951                  * resetting the ha.
4952                  * Since we don't want to block the DPC for too long
4953                  * with multiple resets in the same thread,
4954                  * utilize DPC to retry */
4955                 if (is_qla80XX(ha)) {
4956                         ha->isp_ops->idc_lock(ha);
4957                         dev_state = qla4_8xxx_rd_direct(ha,
4958                                                         QLA8XXX_CRB_DEV_STATE);
4959                         ha->isp_ops->idc_unlock(ha);
4960                         if (dev_state == QLA8XXX_DEV_FAILED) {
4961                                 ql4_printk(KERN_INFO, ha, "%s: don't retry "
4962                                            "recover adapter. H/W is in Failed "
4963                                            "state\n", __func__);
4964                                 qla4xxx_dead_adapter_cleanup(ha);
4965                                 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
4966                                 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
4967                                 clear_bit(DPC_RESET_HA_FW_CONTEXT,
4968                                                 &ha->dpc_flags);
4969                                 status = QLA_ERROR;
4970 
4971                                 goto exit_recover;
4972                         }
4973                 }
4974 
4975                 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
4976                         ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
4977                         DEBUG2(printk("scsi%ld: recover adapter - retrying "
4978                                       "(%d) more times\n", ha->host_no,
4979                                       ha->retry_reset_ha_cnt));
4980                         set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
4981                         status = QLA_ERROR;
4982                 } else {
4983                         if (ha->retry_reset_ha_cnt > 0) {
4984                                 /* Schedule another Reset HA--DPC will retry */
4985                                 ha->retry_reset_ha_cnt--;
4986                                 DEBUG2(printk("scsi%ld: recover adapter - "
4987                                               "retry remaining %d\n",
4988                                               ha->host_no,
4989                                               ha->retry_reset_ha_cnt));
4990                                 status = QLA_ERROR;
4991                         }
4992 
4993                         if (ha->retry_reset_ha_cnt == 0) {
4994                                 /* Recover adapter retries have been exhausted.
4995                                  * Adapter DEAD */
4996                                 DEBUG2(printk("scsi%ld: recover adapter "
4997                                               "failed - board disabled\n",
4998                                               ha->host_no));
4999                                 qla4xxx_dead_adapter_cleanup(ha);
5000                                 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
5001                                 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
5002                                 clear_bit(DPC_RESET_HA_FW_CONTEXT,
5003                                           &ha->dpc_flags);
5004                                 status = QLA_ERROR;
5005                         }
5006                 }
5007         } else {
5008                 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
5009                 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
5010                 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
5011         }
5012 
5013 exit_recover:
5014         ha->adapter_error_count++;
5015 
5016         if (test_bit(AF_ONLINE, &ha->flags))
5017                 ha->isp_ops->enable_intrs(ha);
5018 
5019         scsi_unblock_requests(ha->host);
5020 
5021         clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
5022         DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
5023             status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
5024 
5025         return status;
5026 }
5027 
5028 static void qla4xxx_relogin_devices(struct iscsi_cls_session *cls_session)
5029 {
5030         struct iscsi_session *sess;
5031         struct ddb_entry *ddb_entry;
5032         struct scsi_qla_host *ha;
5033 
5034         sess = cls_session->dd_data;
5035         ddb_entry = sess->dd_data;
5036         ha = ddb_entry->ha;
5037         if (!iscsi_is_session_online(cls_session)) {
5038                 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
5039                         ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
5040                                    " unblock session\n", ha->host_no, __func__,
5041                                    ddb_entry->fw_ddb_index);
5042                         iscsi_unblock_session(ddb_entry->sess);
5043                 } else {
5044                         /* Trigger relogin */
5045                         if (ddb_entry->ddb_type == FLASH_DDB) {
5046                                 if (!(test_bit(DF_RELOGIN, &ddb_entry->flags) ||
5047                                       test_bit(DF_DISABLE_RELOGIN,
5048                                                &ddb_entry->flags)))
5049                                         qla4xxx_arm_relogin_timer(ddb_entry);
5050                         } else
5051                                 iscsi_session_failure(cls_session->dd_data,
5052                                                       ISCSI_ERR_CONN_FAILED);
5053                 }
5054         }
5055 }
5056 
5057 int qla4xxx_unblock_flash_ddb(struct iscsi_cls_session *cls_session)
5058 {
5059         struct iscsi_session *sess;
5060         struct ddb_entry *ddb_entry;
5061         struct scsi_qla_host *ha;
5062 
5063         sess = cls_session->dd_data;
5064         ddb_entry = sess->dd_data;
5065         ha = ddb_entry->ha;
5066         ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
5067                    " unblock session\n", ha->host_no, __func__,
5068                    ddb_entry->fw_ddb_index);
5069 
5070         iscsi_unblock_session(ddb_entry->sess);
5071 
5072         /* Start scan target */
5073         if (test_bit(AF_ONLINE, &ha->flags)) {
5074                 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
5075                            " start scan\n", ha->host_no, __func__,
5076                            ddb_entry->fw_ddb_index);
5077                 scsi_queue_work(ha->host, &ddb_entry->sess->scan_work);
5078         }
5079         return QLA_SUCCESS;
5080 }
5081 
5082 int qla4xxx_unblock_ddb(struct iscsi_cls_session *cls_session)
5083 {
5084         struct iscsi_session *sess;
5085         struct ddb_entry *ddb_entry;
5086         struct scsi_qla_host *ha;
5087         int status = QLA_SUCCESS;
5088 
5089         sess = cls_session->dd_data;
5090         ddb_entry = sess->dd_data;
5091         ha = ddb_entry->ha;
5092         ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
5093                    " unblock user space session\n", ha->host_no, __func__,
5094                    ddb_entry->fw_ddb_index);
5095 
5096         if (!iscsi_is_session_online(cls_session)) {
5097                 iscsi_conn_start(ddb_entry->conn);
5098                 iscsi_conn_login_event(ddb_entry->conn,
5099                                        ISCSI_CONN_STATE_LOGGED_IN);
5100         } else {
5101                 ql4_printk(KERN_INFO, ha,
5102                            "scsi%ld: %s: ddb[%d] session [%d] already logged in\n",
5103                            ha->host_no, __func__, ddb_entry->fw_ddb_index,
5104                            cls_session->sid);
5105                 status = QLA_ERROR;
5106         }
5107 
5108         return status;
5109 }
5110 
5111 static void qla4xxx_relogin_all_devices(struct scsi_qla_host *ha)
5112 {
5113         iscsi_host_for_each_session(ha->host, qla4xxx_relogin_devices);
5114 }
5115 
5116 static void qla4xxx_relogin_flash_ddb(struct iscsi_cls_session *cls_sess)
5117 {
5118         uint16_t relogin_timer;
5119         struct iscsi_session *sess;
5120         struct ddb_entry *ddb_entry;
5121         struct scsi_qla_host *ha;
5122 
5123         sess = cls_sess->dd_data;
5124         ddb_entry = sess->dd_data;
5125         ha = ddb_entry->ha;
5126 
5127         relogin_timer = max(ddb_entry->default_relogin_timeout,
5128                             (uint16_t)RELOGIN_TOV);
5129         atomic_set(&ddb_entry->relogin_timer, relogin_timer);
5130 
5131         DEBUG2(ql4_printk(KERN_INFO, ha,
5132                           "scsi%ld: Relogin index [%d]. TOV=%d\n", ha->host_no,
5133                           ddb_entry->fw_ddb_index, relogin_timer));
5134 
5135         qla4xxx_login_flash_ddb(cls_sess);
5136 }
5137 
5138 static void qla4xxx_dpc_relogin(struct iscsi_cls_session *cls_sess)
5139 {
5140         struct iscsi_session *sess;
5141         struct ddb_entry *ddb_entry;
5142         struct scsi_qla_host *ha;
5143 
5144         sess = cls_sess->dd_data;
5145         ddb_entry = sess->dd_data;
5146         ha = ddb_entry->ha;
5147 
5148         if (!(ddb_entry->ddb_type == FLASH_DDB))
5149                 return;
5150 
5151         if (test_bit(DF_DISABLE_RELOGIN, &ddb_entry->flags))
5152                 return;
5153 
5154         if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
5155             !iscsi_is_session_online(cls_sess)) {
5156                 DEBUG2(ql4_printk(KERN_INFO, ha,
5157                                   "relogin issued\n"));
5158                 qla4xxx_relogin_flash_ddb(cls_sess);
5159         }
5160 }
5161 
5162 void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
5163 {
5164         if (ha->dpc_thread)
5165                 queue_work(ha->dpc_thread, &ha->dpc_work);
5166 }
5167 
5168 static struct qla4_work_evt *
5169 qla4xxx_alloc_work(struct scsi_qla_host *ha, uint32_t data_size,
5170                    enum qla4_work_type type)
5171 {
5172         struct qla4_work_evt *e;
5173         uint32_t size = sizeof(struct qla4_work_evt) + data_size;
5174 
5175         e = kzalloc(size, GFP_ATOMIC);
5176         if (!e)
5177                 return NULL;
5178 
5179         INIT_LIST_HEAD(&e->list);
5180         e->type = type;
5181         return e;
5182 }
5183 
5184 static void qla4xxx_post_work(struct scsi_qla_host *ha,
5185                              struct qla4_work_evt *e)
5186 {
5187         unsigned long flags;
5188 
5189         spin_lock_irqsave(&ha->work_lock, flags);
5190         list_add_tail(&e->list, &ha->work_list);
5191         spin_unlock_irqrestore(&ha->work_lock, flags);
5192         qla4xxx_wake_dpc(ha);
5193 }
5194 
5195 int qla4xxx_post_aen_work(struct scsi_qla_host *ha,
5196                           enum iscsi_host_event_code aen_code,
5197                           uint32_t data_size, uint8_t *data)
5198 {
5199         struct qla4_work_evt *e;
5200 
5201         e = qla4xxx_alloc_work(ha, data_size, QLA4_EVENT_AEN);
5202         if (!e)
5203                 return QLA_ERROR;
5204 
5205         e->u.aen.code = aen_code;
5206         e->u.aen.data_size = data_size;
5207         memcpy(e->u.aen.data, data, data_size);
5208 
5209         qla4xxx_post_work(ha, e);
5210 
5211         return QLA_SUCCESS;
5212 }
5213 
5214 int qla4xxx_post_ping_evt_work(struct scsi_qla_host *ha,
5215                                uint32_t status, uint32_t pid,
5216                                uint32_t data_size, uint8_t *data)
5217 {
5218         struct qla4_work_evt *e;
5219 
5220         e = qla4xxx_alloc_work(ha, data_size, QLA4_EVENT_PING_STATUS);
5221         if (!e)
5222                 return QLA_ERROR;
5223 
5224         e->u.ping.status = status;
5225         e->u.ping.pid = pid;
5226         e->u.ping.data_size = data_size;
5227         memcpy(e->u.ping.data, data, data_size);
5228 
5229         qla4xxx_post_work(ha, e);
5230 
5231         return QLA_SUCCESS;
5232 }
5233 
5234 static void qla4xxx_do_work(struct scsi_qla_host *ha)
5235 {
5236         struct qla4_work_evt *e, *tmp;
5237         unsigned long flags;
5238         LIST_HEAD(work);
5239 
5240         spin_lock_irqsave(&ha->work_lock, flags);
5241         list_splice_init(&ha->work_list, &work);
5242         spin_unlock_irqrestore(&ha->work_lock, flags);
5243 
5244         list_for_each_entry_safe(e, tmp, &work, list) {
5245                 list_del_init(&e->list);
5246 
5247                 switch (e->type) {
5248                 case QLA4_EVENT_AEN:
5249                         iscsi_post_host_event(ha->host_no,
5250                                               &qla4xxx_iscsi_transport,
5251                                               e->u.aen.code,
5252                                               e->u.aen.data_size,
5253                                               e->u.aen.data);
5254                         break;
5255                 case QLA4_EVENT_PING_STATUS:
5256                         iscsi_ping_comp_event(ha->host_no,
5257                                               &qla4xxx_iscsi_transport,
5258                                               e->u.ping.status,
5259                                               e->u.ping.pid,
5260                                               e->u.ping.data_size,
5261                                               e->u.ping.data);
5262                         break;
5263                 default:
5264                         ql4_printk(KERN_WARNING, ha, "event type: 0x%x not "
5265                                    "supported", e->type);
5266                 }
5267                 kfree(e);
5268         }
5269 }
5270 
5271 /**
5272  * qla4xxx_do_dpc - dpc routine
5273  * @data: in our case pointer to adapter structure
5274  *
5275  * This routine is a task that is schedule by the interrupt handler
5276  * to perform the background processing for interrupts.  We put it
5277  * on a task queue that is consumed whenever the scheduler runs; that's
5278  * so you can do anything (i.e. put the process to sleep etc).  In fact,
5279  * the mid-level tries to sleep when it reaches the driver threshold
5280  * "host->can_queue". This can cause a panic if we were in our interrupt code.
5281  **/
5282 static void qla4xxx_do_dpc(struct work_struct *work)
5283 {
5284         struct scsi_qla_host *ha =
5285                 container_of(work, struct scsi_qla_host, dpc_work);
5286         int status = QLA_ERROR;
5287 
5288         DEBUG2(ql4_printk(KERN_INFO, ha,
5289                           "scsi%ld: %s: DPC handler waking up. flags = 0x%08lx, dpc_flags = 0x%08lx\n",
5290                           ha->host_no, __func__, ha->flags, ha->dpc_flags));
5291 
5292         /* Initialization not yet finished. Don't do anything yet. */
5293         if (!test_bit(AF_INIT_DONE, &ha->flags))
5294                 return;
5295 
5296         if (test_bit(AF_EEH_BUSY, &ha->flags)) {
5297                 DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
5298                     ha->host_no, __func__, ha->flags));
5299                 return;
5300         }
5301 
5302         /* post events to application */
5303         qla4xxx_do_work(ha);
5304 
5305         if (is_qla80XX(ha)) {
5306                 if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
5307                         if (is_qla8032(ha) || is_qla8042(ha)) {
5308                                 ql4_printk(KERN_INFO, ha, "%s: disabling pause transmit on port 0 & 1.\n",
5309                                            __func__);
5310                                 /* disable pause frame for ISP83xx */
5311                                 qla4_83xx_disable_pause(ha);
5312                         }
5313 
5314                         ha->isp_ops->idc_lock(ha);
5315                         qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
5316                                             QLA8XXX_DEV_FAILED);
5317                         ha->isp_ops->idc_unlock(ha);
5318                         ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
5319                         qla4_8xxx_device_state_handler(ha);
5320                 }
5321 
5322                 if (test_bit(DPC_POST_IDC_ACK, &ha->dpc_flags)) {
5323                         if (is_qla8042(ha)) {
5324                                 if (ha->idc_info.info2 &
5325                                     ENABLE_INTERNAL_LOOPBACK) {
5326                                         ql4_printk(KERN_INFO, ha, "%s: Disabling ACB\n",
5327                                                    __func__);
5328                                         status = qla4_84xx_config_acb(ha,
5329                                                             ACB_CONFIG_DISABLE);
5330                                         if (status != QLA_SUCCESS) {
5331                                                 ql4_printk(KERN_INFO, ha, "%s: ACB config failed\n",
5332                                                            __func__);
5333                                         }
5334                                 }
5335                         }
5336                         qla4_83xx_post_idc_ack(ha);
5337                         clear_bit(DPC_POST_IDC_ACK, &ha->dpc_flags);
5338                 }
5339 
5340                 if (is_qla8042(ha) &&
5341                     test_bit(DPC_RESTORE_ACB, &ha->dpc_flags)) {
5342                         ql4_printk(KERN_INFO, ha, "%s: Restoring ACB\n",
5343                                    __func__);
5344                         if (qla4_84xx_config_acb(ha, ACB_CONFIG_SET) !=
5345                             QLA_SUCCESS) {
5346                                 ql4_printk(KERN_INFO, ha, "%s: ACB config failed ",
5347                                            __func__);
5348                         }
5349                         clear_bit(DPC_RESTORE_ACB, &ha->dpc_flags);
5350                 }
5351 
5352                 if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
5353                         qla4_8xxx_need_qsnt_handler(ha);
5354                 }
5355         }
5356 
5357         if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
5358             (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
5359             test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
5360             test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
5361                 if ((is_qla8022(ha) && ql4xdontresethba) ||
5362                     ((is_qla8032(ha) || is_qla8042(ha)) &&
5363                      qla4_83xx_idc_dontreset(ha))) {
5364                         DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
5365                             ha->host_no, __func__));
5366                         clear_bit(DPC_RESET_HA, &ha->dpc_flags);
5367                         clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
5368                         clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
5369                         goto dpc_post_reset_ha;
5370                 }
5371                 if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
5372                     test_bit(DPC_RESET_HA, &ha->dpc_flags))
5373                         qla4xxx_recover_adapter(ha);
5374 
5375                 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
5376                         uint8_t wait_time = RESET_INTR_TOV;
5377 
5378                         while ((readw(&ha->reg->ctrl_status) &
5379                                 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
5380                                 if (--wait_time == 0)
5381                                         break;
5382                                 msleep(1000);
5383                         }
5384                         if (wait_time == 0)
5385                                 DEBUG2(printk("scsi%ld: %s: SR|FSR "
5386                                               "bit not cleared-- resetting\n",
5387                                               ha->host_no, __func__));
5388                         qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
5389                         if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
5390                                 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
5391                                 status = qla4xxx_recover_adapter(ha);
5392                         }
5393                         clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
5394                         if (status == QLA_SUCCESS)
5395                                 ha->isp_ops->enable_intrs(ha);
5396                 }
5397         }
5398 
5399 dpc_post_reset_ha:
5400         /* ---- process AEN? --- */
5401         if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
5402                 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
5403 
5404         /* ---- Get DHCP IP Address? --- */
5405         if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
5406                 qla4xxx_get_dhcp_ip_address(ha);
5407 
5408         /* ---- relogin device? --- */
5409         if (adapter_up(ha) &&
5410             test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
5411                 iscsi_host_for_each_session(ha->host, qla4xxx_dpc_relogin);
5412         }
5413 
5414         /* ---- link change? --- */
5415         if (!test_bit(AF_LOOPBACK, &ha->flags) &&
5416             test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
5417                 if (!test_bit(AF_LINK_UP, &ha->flags)) {
5418                         /* ---- link down? --- */
5419                         qla4xxx_mark_all_devices_missing(ha);
5420                 } else {
5421                         /* ---- link up? --- *
5422                          * F/W will auto login to all devices ONLY ONCE after
5423                          * link up during driver initialization and runtime
5424                          * fatal error recovery.  Therefore, the driver must
5425                          * manually relogin to devices when recovering from
5426                          * connection failures, logouts, expired KATO, etc. */
5427                         if (test_and_clear_bit(AF_BUILD_DDB_LIST, &ha->flags)) {
5428                                 qla4xxx_build_ddb_list(ha, ha->is_reset);
5429                                 iscsi_host_for_each_session(ha->host,
5430                                                 qla4xxx_login_flash_ddb);
5431                         } else
5432                                 qla4xxx_relogin_all_devices(ha);
5433                 }
5434         }
5435 }
5436 
5437 /**
5438  * qla4xxx_free_adapter - release the adapter
5439  * @ha: pointer to adapter structure
5440  **/
5441 static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
5442 {
5443         qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
5444 
5445         /* Turn-off interrupts on the card. */
5446         ha->isp_ops->disable_intrs(ha);
5447 
5448         if (is_qla40XX(ha)) {
5449                 writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
5450                        &ha->reg->ctrl_status);
5451                 readl(&ha->reg->ctrl_status);
5452         } else if (is_qla8022(ha)) {
5453                 writel(0, &ha->qla4_82xx_reg->host_int);
5454                 readl(&ha->qla4_82xx_reg->host_int);
5455         } else if (is_qla8032(ha) || is_qla8042(ha)) {
5456                 writel(0, &ha->qla4_83xx_reg->risc_intr);
5457                 readl(&ha->qla4_83xx_reg->risc_intr);
5458         }
5459 
5460         /* Remove timer thread, if present */
5461         if (ha->timer_active)
5462                 qla4xxx_stop_timer(ha);
5463 
5464         /* Kill the kernel thread for this host */
5465         if (ha->dpc_thread)
5466                 destroy_workqueue(ha->dpc_thread);
5467 
5468         /* Kill the kernel thread for this host */
5469         if (ha->task_wq)
5470                 destroy_workqueue(ha->task_wq);
5471 
5472         /* Put firmware in known state */
5473         ha->isp_ops->reset_firmware(ha);
5474 
5475         if (is_qla80XX(ha)) {
5476                 ha->isp_ops->idc_lock(ha);
5477                 qla4_8xxx_clear_drv_active(ha);
5478                 ha->isp_ops->idc_unlock(ha);
5479         }
5480 
5481         /* Detach interrupts */
5482         qla4xxx_free_irqs(ha);
5483 
5484         /* free extra memory */
5485         qla4xxx_mem_free(ha);
5486 }
5487 
5488 int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
5489 {
5490         int status = 0;
5491         unsigned long mem_base, mem_len, db_base, db_len;
5492         struct pci_dev *pdev = ha->pdev;
5493 
5494         status = pci_request_regions(pdev, DRIVER_NAME);
5495         if (status) {
5496                 printk(KERN_WARNING
5497                     "scsi(%ld) Failed to reserve PIO regions (%s) "
5498                     "status=%d\n", ha->host_no, pci_name(pdev), status);
5499                 goto iospace_error_exit;
5500         }
5501 
5502         DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
5503             __func__, pdev->revision));
5504         ha->revision_id = pdev->revision;
5505 
5506         /* remap phys address */
5507         mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
5508         mem_len = pci_resource_len(pdev, 0);
5509         DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
5510             __func__, mem_base, mem_len));
5511 
5512         /* mapping of pcibase pointer */
5513         ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
5514         if (!ha->nx_pcibase) {
5515                 printk(KERN_ERR
5516                     "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
5517                 pci_release_regions(ha->pdev);
5518                 goto iospace_error_exit;
5519         }
5520 
5521         /* Mapping of IO base pointer, door bell read and write pointer */
5522 
5523         /* mapping of IO base pointer */
5524         if (is_qla8022(ha)) {
5525                 ha->qla4_82xx_reg = (struct device_reg_82xx  __iomem *)
5526                                     ((uint8_t *)ha->nx_pcibase + 0xbc000 +
5527                                      (ha->pdev->devfn << 11));
5528                 ha->nx_db_wr_ptr = (ha->pdev->devfn == 4 ? QLA82XX_CAM_RAM_DB1 :
5529                                     QLA82XX_CAM_RAM_DB2);
5530         } else if (is_qla8032(ha) || is_qla8042(ha)) {
5531                 ha->qla4_83xx_reg = (struct device_reg_83xx __iomem *)
5532                                     ((uint8_t *)ha->nx_pcibase);
5533         }
5534 
5535         db_base = pci_resource_start(pdev, 4);  /* doorbell is on bar 4 */
5536         db_len = pci_resource_len(pdev, 4);
5537 
5538         return 0;
5539 iospace_error_exit:
5540         return -ENOMEM;
5541 }
5542 
5543 /***
5544  * qla4xxx_iospace_config - maps registers
5545  * @ha: pointer to adapter structure
5546  *
5547  * This routines maps HBA's registers from the pci address space
5548  * into the kernel virtual address space for memory mapped i/o.
5549  **/
5550 int qla4xxx_iospace_config(struct scsi_qla_host *ha)
5551 {
5552         unsigned long pio, pio_len, pio_flags;
5553         unsigned long mmio, mmio_len, mmio_flags;
5554 
5555         pio = pci_resource_start(ha->pdev, 0);
5556         pio_len = pci_resource_len(ha->pdev, 0);
5557         pio_flags = pci_resource_flags(ha->pdev, 0);
5558         if (pio_flags & IORESOURCE_IO) {
5559                 if (pio_len < MIN_IOBASE_LEN) {
5560                         ql4_printk(KERN_WARNING, ha,
5561                                 "Invalid PCI I/O region size\n");
5562                         pio = 0;
5563                 }
5564         } else {
5565                 ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
5566                 pio = 0;
5567         }
5568 
5569         /* Use MMIO operations for all accesses. */
5570         mmio = pci_resource_start(ha->pdev, 1);
5571         mmio_len = pci_resource_len(ha->pdev, 1);
5572         mmio_flags = pci_resource_flags(ha->pdev, 1);
5573 
5574         if (!(mmio_flags & IORESOURCE_MEM)) {
5575                 ql4_printk(KERN_ERR, ha,
5576                     "region #0 not an MMIO resource, aborting\n");
5577 
5578                 goto iospace_error_exit;
5579         }
5580 
5581         if (mmio_len < MIN_IOBASE_LEN) {
5582                 ql4_printk(KERN_ERR, ha,
5583                     "Invalid PCI mem region size, aborting\n");
5584                 goto iospace_error_exit;
5585         }
5586 
5587         if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
5588                 ql4_printk(KERN_WARNING, ha,
5589                     "Failed to reserve PIO/MMIO regions\n");
5590 
5591                 goto iospace_error_exit;
5592         }
5593 
5594         ha->pio_address = pio;
5595         ha->pio_length = pio_len;
5596         ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
5597         if (!ha->reg) {
5598                 ql4_printk(KERN_ERR, ha,
5599                     "cannot remap MMIO, aborting\n");
5600 
5601                 goto iospace_error_exit;
5602         }
5603 
5604         return 0;
5605 
5606 iospace_error_exit:
5607         return -ENOMEM;
5608 }
5609 
5610 static struct isp_operations qla4xxx_isp_ops = {
5611         .iospace_config         = qla4xxx_iospace_config,
5612         .pci_config             = qla4xxx_pci_config,
5613         .disable_intrs          = qla4xxx_disable_intrs,
5614         .enable_intrs           = qla4xxx_enable_intrs,
5615         .start_firmware         = qla4xxx_start_firmware,
5616         .intr_handler           = qla4xxx_intr_handler,
5617         .interrupt_service_routine = qla4xxx_interrupt_service_routine,
5618         .reset_chip             = qla4xxx_soft_reset,
5619         .reset_firmware         = qla4xxx_hw_reset,
5620         .queue_iocb             = qla4xxx_queue_iocb,
5621         .complete_iocb          = qla4xxx_complete_iocb,
5622         .rd_shdw_req_q_out      = qla4xxx_rd_shdw_req_q_out,
5623         .rd_shdw_rsp_q_in       = qla4xxx_rd_shdw_rsp_q_in,
5624         .get_sys_info           = qla4xxx_get_sys_info,
5625         .queue_mailbox_command  = qla4xxx_queue_mbox_cmd,
5626         .process_mailbox_interrupt = qla4xxx_process_mbox_intr,
5627 };
5628 
5629 static struct isp_operations qla4_82xx_isp_ops = {
5630         .iospace_config         = qla4_8xxx_iospace_config,
5631         .pci_config             = qla4_8xxx_pci_config,
5632         .disable_intrs          = qla4_82xx_disable_intrs,
5633         .enable_intrs           = qla4_82xx_enable_intrs,
5634         .start_firmware         = qla4_8xxx_load_risc,
5635         .restart_firmware       = qla4_82xx_try_start_fw,
5636         .intr_handler           = qla4_82xx_intr_handler,
5637         .interrupt_service_routine = qla4_82xx_interrupt_service_routine,
5638         .need_reset             = qla4_8xxx_need_reset,
5639         .reset_chip             = qla4_82xx_isp_reset,
5640         .reset_firmware         = qla4_8xxx_stop_firmware,
5641         .queue_iocb             = qla4_82xx_queue_iocb,
5642         .complete_iocb          = qla4_82xx_complete_iocb,
5643         .rd_shdw_req_q_out      = qla4_82xx_rd_shdw_req_q_out,
5644         .rd_shdw_rsp_q_in       = qla4_82xx_rd_shdw_rsp_q_in,
5645         .get_sys_info           = qla4_8xxx_get_sys_info,
5646         .rd_reg_direct          = qla4_82xx_rd_32,
5647         .wr_reg_direct          = qla4_82xx_wr_32,
5648         .rd_reg_indirect        = qla4_82xx_md_rd_32,
5649         .wr_reg_indirect        = qla4_82xx_md_wr_32,
5650         .idc_lock               = qla4_82xx_idc_lock,
5651         .idc_unlock             = qla4_82xx_idc_unlock,
5652         .rom_lock_recovery      = qla4_82xx_rom_lock_recovery,
5653         .queue_mailbox_command  = qla4_82xx_queue_mbox_cmd,
5654         .process_mailbox_interrupt = qla4_82xx_process_mbox_intr,
5655 };
5656 
5657 static struct isp_operations qla4_83xx_isp_ops = {
5658         .iospace_config         = qla4_8xxx_iospace_config,
5659         .pci_config             = qla4_8xxx_pci_config,
5660         .disable_intrs          = qla4_83xx_disable_intrs,
5661         .enable_intrs           = qla4_83xx_enable_intrs,
5662         .start_firmware         = qla4_8xxx_load_risc,
5663         .restart_firmware       = qla4_83xx_start_firmware,
5664         .intr_handler           = qla4_83xx_intr_handler,
5665         .interrupt_service_routine = qla4_83xx_interrupt_service_routine,
5666         .need_reset             = qla4_8xxx_need_reset,
5667         .reset_chip             = qla4_83xx_isp_reset,
5668         .reset_firmware         = qla4_8xxx_stop_firmware,
5669         .queue_iocb             = qla4_83xx_queue_iocb,
5670         .complete_iocb          = qla4_83xx_complete_iocb,
5671         .rd_shdw_req_q_out      = qla4xxx_rd_shdw_req_q_out,
5672         .rd_shdw_rsp_q_in       = qla4xxx_rd_shdw_rsp_q_in,
5673         .get_sys_info           = qla4_8xxx_get_sys_info,
5674         .rd_reg_direct          = qla4_83xx_rd_reg,
5675         .wr_reg_direct          = qla4_83xx_wr_reg,
5676         .rd_reg_indirect        = qla4_83xx_rd_reg_indirect,
5677         .wr_reg_indirect        = qla4_83xx_wr_reg_indirect,
5678         .idc_lock               = qla4_83xx_drv_lock,
5679         .idc_unlock             = qla4_83xx_drv_unlock,
5680         .rom_lock_recovery      = qla4_83xx_rom_lock_recovery,
5681         .queue_mailbox_command  = qla4_83xx_queue_mbox_cmd,
5682         .process_mailbox_interrupt = qla4_83xx_process_mbox_intr,
5683 };
5684 
5685 uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
5686 {
5687         return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
5688 }
5689 
5690 uint16_t qla4_82xx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
5691 {
5692         return (uint16_t)le32_to_cpu(readl(&ha->qla4_82xx_reg->req_q_out));
5693 }
5694 
5695 uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
5696 {
5697         return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
5698 }
5699 
5700 uint16_t qla4_82xx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
5701 {
5702         return (uint16_t)le32_to_cpu(readl(&ha->qla4_82xx_reg->rsp_q_in));
5703 }
5704 
5705 static ssize_t qla4xxx_show_boot_eth_info(void *data, int type, char *buf)
5706 {
5707         struct scsi_qla_host *ha = data;
5708         char *str = buf;
5709         int rc;
5710 
5711         switch (type) {
5712         case ISCSI_BOOT_ETH_FLAGS:
5713                 rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
5714                 break;
5715         case ISCSI_BOOT_ETH_INDEX:
5716                 rc = sprintf(str, "\n");
5717                 break;
5718         case ISCSI_BOOT_ETH_MAC:
5719                 rc = sysfs_format_mac(str, ha->my_mac,
5720                                       MAC_ADDR_LEN);
5721                 break;
5722         default:
5723                 rc = -ENOSYS;
5724                 break;
5725         }
5726         return rc;
5727 }
5728 
5729 static umode_t qla4xxx_eth_get_attr_visibility(void *data, int type)
5730 {
5731         int rc;
5732 
5733         switch (type) {
5734         case ISCSI_BOOT_ETH_FLAGS:
5735         case ISCSI_BOOT_ETH_MAC:
5736         case ISCSI_BOOT_ETH_INDEX:
5737                 rc = S_IRUGO;
5738                 break;
5739         default:
5740                 rc = 0;
5741                 break;
5742         }
5743         return rc;
5744 }
5745 
5746 static ssize_t qla4xxx_show_boot_ini_info(void *data, int type, char *buf)
5747 {
5748         struct scsi_qla_host *ha = data;
5749         char *str = buf;
5750         int rc;
5751 
5752         switch (type) {
5753         case ISCSI_BOOT_INI_INITIATOR_NAME:
5754                 rc = sprintf(str, "%s\n", ha->name_string);
5755                 break;
5756         default:
5757                 rc = -ENOSYS;
5758                 break;
5759         }
5760         return rc;
5761 }
5762 
5763 static umode_t qla4xxx_ini_get_attr_visibility(void *data, int type)
5764 {
5765         int rc;
5766 
5767         switch (type) {
5768         case ISCSI_BOOT_INI_INITIATOR_NAME:
5769                 rc = S_IRUGO;
5770                 break;
5771         default:
5772                 rc = 0;
5773                 break;
5774         }
5775         return rc;
5776 }
5777 
5778 static ssize_t
5779 qla4xxx_show_boot_tgt_info(struct ql4_boot_session_info *boot_sess, int type,
5780                            char *buf)
5781 {
5782         struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
5783         char *str = buf;
5784         int rc;
5785 
5786         switch (type) {
5787         case ISCSI_BOOT_TGT_NAME:
5788                 rc = sprintf(buf, "%s\n", (char *)&boot_sess->target_name);
5789                 break;
5790         case ISCSI_BOOT_TGT_IP_ADDR:
5791                 if (boot_sess->conn_list[0].dest_ipaddr.ip_type == 0x1)
5792                         rc = sprintf(buf, "%pI4\n",
5793                                      &boot_conn->dest_ipaddr.ip_address);
5794                 else
5795                         rc = sprintf(str, "%pI6\n",
5796                                      &boot_conn->dest_ipaddr.ip_address);
5797                 break;
5798         case ISCSI_BOOT_TGT_PORT:
5799                         rc = sprintf(str, "%d\n", boot_conn->dest_port);
5800                 break;
5801         case ISCSI_BOOT_TGT_CHAP_NAME:
5802                 rc = sprintf(str,  "%.*s\n",
5803                              boot_conn->chap.target_chap_name_length,
5804                              (char *)&boot_conn->chap.target_chap_name);
5805                 break;
5806         case ISCSI_BOOT_TGT_CHAP_SECRET:
5807                 rc = sprintf(str,  "%.*s\n",
5808                              boot_conn->chap.target_secret_length,
5809                              (char *)&boot_conn->chap.target_secret);
5810                 break;
5811         case ISCSI_BOOT_TGT_REV_CHAP_NAME:
5812                 rc = sprintf(str,  "%.*s\n",
5813                              boot_conn->chap.intr_chap_name_length,
5814                              (char *)&boot_conn->chap.intr_chap_name);
5815                 break;
5816         case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
5817                 rc = sprintf(str,  "%.*s\n",
5818                              boot_conn->chap.intr_secret_length,
5819                              (char *)&boot_conn->chap.intr_secret);
5820                 break;
5821         case ISCSI_BOOT_TGT_FLAGS:
5822                 rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
5823                 break;
5824         case ISCSI_BOOT_TGT_NIC_ASSOC:
5825                 rc = sprintf(str, "\n");
5826                 break;
5827         default:
5828                 rc = -ENOSYS;
5829                 break;
5830         }
5831         return rc;
5832 }
5833 
5834 static ssize_t qla4xxx_show_boot_tgt_pri_info(void *data, int type, char *buf)
5835 {
5836         struct scsi_qla_host *ha = data;
5837         struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_pri_sess);
5838 
5839         return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
5840 }
5841 
5842 static ssize_t qla4xxx_show_boot_tgt_sec_info(void *data, int type, char *buf)
5843 {
5844         struct scsi_qla_host *ha = data;
5845         struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_sec_sess);
5846 
5847         return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
5848 }
5849 
5850 static umode_t qla4xxx_tgt_get_attr_visibility(void *data, int type)
5851 {
5852         int rc;
5853 
5854         switch (type) {
5855         case ISCSI_BOOT_TGT_NAME:
5856         case ISCSI_BOOT_TGT_IP_ADDR:
5857         case ISCSI_BOOT_TGT_PORT:
5858         case ISCSI_BOOT_TGT_CHAP_NAME:
5859         case ISCSI_BOOT_TGT_CHAP_SECRET:
5860         case ISCSI_BOOT_TGT_REV_CHAP_NAME:
5861         case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
5862         case ISCSI_BOOT_TGT_NIC_ASSOC:
5863         case ISCSI_BOOT_TGT_FLAGS:
5864                 rc = S_IRUGO;
5865                 break;
5866         default:
5867                 rc = 0;
5868                 break;
5869         }
5870         return rc;
5871 }
5872 
5873 static void qla4xxx_boot_release(void *data)
5874 {
5875         struct scsi_qla_host *ha = data;
5876 
5877         scsi_host_put(ha->host);
5878 }
5879 
5880 static int get_fw_boot_info(struct scsi_qla_host *ha, uint16_t ddb_index[])
5881 {
5882         dma_addr_t buf_dma;
5883         uint32_t addr, pri_addr, sec_addr;
5884         uint32_t offset;
5885         uint16_t func_num;
5886         uint8_t val;
5887         uint8_t *buf = NULL;
5888         size_t size = 13 * sizeof(uint8_t);
5889         int ret = QLA_SUCCESS;
5890 
5891         func_num = PCI_FUNC(ha->pdev->devfn);
5892 
5893         ql4_printk(KERN_INFO, ha, "%s: Get FW boot info for 0x%x func %d\n",
5894                    __func__, ha->pdev->device, func_num);
5895 
5896         if (is_qla40XX(ha)) {
5897                 if (func_num == 1) {
5898                         addr = NVRAM_PORT0_BOOT_MODE;
5899                         pri_addr = NVRAM_PORT0_BOOT_PRI_TGT;
5900                         sec_addr = NVRAM_PORT0_BOOT_SEC_TGT;
5901                 } else if (func_num == 3) {
5902                         addr = NVRAM_PORT1_BOOT_MODE;
5903                         pri_addr = NVRAM_PORT1_BOOT_PRI_TGT;
5904                         sec_addr = NVRAM_PORT1_BOOT_SEC_TGT;
5905                 } else {
5906                         ret = QLA_ERROR;
5907                         goto exit_boot_info;
5908                 }
5909 
5910                 /* Check Boot Mode */
5911                 val = rd_nvram_byte(ha, addr);
5912                 if (!(val & 0x07)) {
5913                         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Adapter boot "
5914                                           "options : 0x%x\n", __func__, val));
5915                         ret = QLA_ERROR;
5916                         goto exit_boot_info;
5917                 }
5918 
5919                 /* get primary valid target index */
5920                 val = rd_nvram_byte(ha, pri_addr);
5921                 if (val & BIT_7)
5922                         ddb_index[0] = (val & 0x7f);
5923 
5924                 /* get secondary valid target index */
5925                 val = rd_nvram_byte(ha, sec_addr);
5926                 if (val & BIT_7)
5927                         ddb_index[1] = (val & 0x7f);
5928 
5929         } else if (is_qla80XX(ha)) {
5930                 buf = dma_alloc_coherent(&ha->pdev->dev, size,
5931                                          &buf_dma, GFP_KERNEL);
5932                 if (!buf) {
5933                         DEBUG2(ql4_printk(KERN_ERR, ha,
5934                                           "%s: Unable to allocate dma buffer\n",
5935                                            __func__));
5936                         ret = QLA_ERROR;
5937                         goto exit_boot_info;
5938                 }
5939 
5940                 if (ha->port_num == 0)
5941                         offset = BOOT_PARAM_OFFSET_PORT0;
5942                 else if (ha->port_num == 1)
5943                         offset = BOOT_PARAM_OFFSET_PORT1;
5944                 else {
5945                         ret = QLA_ERROR;
5946                         goto exit_boot_info_free;
5947                 }
5948                 addr = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_iscsi_param * 4) +
5949                        offset;
5950                 if (qla4xxx_get_flash(ha, buf_dma, addr,
5951                                       13 * sizeof(uint8_t)) != QLA_SUCCESS) {
5952                         DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: Get Flash"
5953                                           " failed\n", ha->host_no, __func__));
5954                         ret = QLA_ERROR;
5955                         goto exit_boot_info_free;
5956                 }
5957                 /* Check Boot Mode */
5958                 if (!(buf[1] & 0x07)) {
5959                         DEBUG2(ql4_printk(KERN_INFO, ha, "Firmware boot options"
5960                                           " : 0x%x\n", buf[1]));
5961                         ret = QLA_ERROR;
5962                         goto exit_boot_info_free;
5963                 }
5964 
5965                 /* get primary valid target index */
5966                 if (buf[2] & BIT_7)
5967                         ddb_index[0] = buf[2] & 0x7f;
5968 
5969                 /* get secondary valid target index */
5970                 if (buf[11] & BIT_7)
5971                         ddb_index[1] = buf[11] & 0x7f;
5972         } else {
5973                 ret = QLA_ERROR;
5974                 goto exit_boot_info;
5975         }
5976 
5977         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Primary target ID %d, Secondary"
5978                           " target ID %d\n", __func__, ddb_index[0],
5979                           ddb_index[1]));
5980 
5981 exit_boot_info_free:
5982         dma_free_coherent(&ha->pdev->dev, size, buf, buf_dma);
5983 exit_boot_info:
5984         ha->pri_ddb_idx = ddb_index[0];
5985         ha->sec_ddb_idx = ddb_index[1];
5986         return ret;
5987 }
5988 
5989 /**
5990  * qla4xxx_get_bidi_chap - Get a BIDI CHAP user and password
5991  * @ha: pointer to adapter structure
5992  * @username: CHAP username to be returned
5993  * @password: CHAP password to be returned
5994  *
5995  * If a boot entry has BIDI CHAP enabled then we need to set the BIDI CHAP
5996  * user and password in the sysfs entry in /sys/firmware/iscsi_boot#/.
5997  * So from the CHAP cache find the first BIDI CHAP entry and set it
5998  * to the boot record in sysfs.
5999  **/
6000 static int qla4xxx_get_bidi_chap(struct scsi_qla_host *ha, char *username,
6001                             char *password)
6002 {
6003         int i, ret = -EINVAL;
6004         int max_chap_entries = 0;
6005         struct ql4_chap_table *chap_table;
6006 
6007         if (is_qla80XX(ha))
6008                 max_chap_entries = (ha->hw.flt_chap_size / 2) /
6009                                                 sizeof(struct ql4_chap_table);
6010         else
6011                 max_chap_entries = MAX_CHAP_ENTRIES_40XX;
6012 
6013         if (!ha->chap_list) {
6014                 ql4_printk(KERN_ERR, ha, "Do not have CHAP table cache\n");
6015                 return ret;
6016         }
6017 
6018         mutex_lock(&ha->chap_sem);
6019         for (i = 0; i < max_chap_entries; i++) {
6020                 chap_table = (struct ql4_chap_table *)ha->chap_list + i;
6021                 if (chap_table->cookie !=
6022                     __constant_cpu_to_le16(CHAP_VALID_COOKIE)) {
6023                         continue;
6024                 }
6025 
6026                 if (chap_table->flags & BIT_7) /* local */
6027                         continue;
6028 
6029                 if (!(chap_table->flags & BIT_6)) /* Not BIDI */
6030                         continue;
6031 
6032                 strncpy(password, chap_table->secret, QL4_CHAP_MAX_SECRET_LEN);
6033                 strncpy(username, chap_table->name, QL4_CHAP_MAX_NAME_LEN);
6034                 ret = 0;
6035                 break;
6036         }
6037         mutex_unlock(&ha->chap_sem);
6038 
6039         return ret;
6040 }
6041 
6042 
6043 static int qla4xxx_get_boot_target(struct scsi_qla_host *ha,
6044                                    struct ql4_boot_session_info *boot_sess,
6045                                    uint16_t ddb_index)
6046 {
6047         struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
6048         struct dev_db_entry *fw_ddb_entry;
6049         dma_addr_t fw_ddb_entry_dma;
6050         uint16_t idx;
6051         uint16_t options;
6052         int ret = QLA_SUCCESS;
6053 
6054         fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
6055                                           &fw_ddb_entry_dma, GFP_KERNEL);
6056         if (!fw_ddb_entry) {
6057                 DEBUG2(ql4_printk(KERN_ERR, ha,
6058                                   "%s: Unable to allocate dma buffer.\n",
6059                                   __func__));
6060                 ret = QLA_ERROR;
6061                 return ret;
6062         }
6063 
6064         if (qla4xxx_bootdb_by_index(ha, fw_ddb_entry,
6065                                    fw_ddb_entry_dma, ddb_index)) {
6066                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: No Flash DDB found at "
6067                                   "index [%d]\n", __func__, ddb_index));
6068                 ret = QLA_ERROR;
6069                 goto exit_boot_target;
6070         }
6071 
6072         /* Update target name and IP from DDB */
6073         memcpy(boot_sess->target_name, fw_ddb_entry->iscsi_name,
6074                min(sizeof(boot_sess->target_name),
6075                    sizeof(fw_ddb_entry->iscsi_name)));
6076 
6077         options = le16_to_cpu(fw_ddb_entry->options);
6078         if (options & DDB_OPT_IPV6_DEVICE) {
6079                 memcpy(&boot_conn->dest_ipaddr.ip_address,
6080                        &fw_ddb_entry->ip_addr[0], IPv6_ADDR_LEN);
6081         } else {
6082                 boot_conn->dest_ipaddr.ip_type = 0x1;
6083                 memcpy(&boot_conn->dest_ipaddr.ip_address,
6084                        &fw_ddb_entry->ip_addr[0], IP_ADDR_LEN);
6085         }
6086 
6087         boot_conn->dest_port = le16_to_cpu(fw_ddb_entry->port);
6088 
6089         /* update chap information */
6090         idx = __le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
6091 
6092         if (BIT_7 & le16_to_cpu(fw_ddb_entry->iscsi_options))   {
6093 
6094                 DEBUG2(ql4_printk(KERN_INFO, ha, "Setting chap\n"));
6095 
6096                 ret = qla4xxx_get_chap(ha, (char *)&boot_conn->chap.
6097                                        target_chap_name,
6098                                        (char *)&boot_conn->chap.target_secret,
6099                                        idx);
6100                 if (ret) {
6101                         ql4_printk(KERN_ERR, ha, "Failed to set chap\n");
6102                         ret = QLA_ERROR;
6103                         goto exit_boot_target;
6104                 }
6105 
6106                 boot_conn->chap.target_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
6107                 boot_conn->chap.target_secret_length = QL4_CHAP_MAX_SECRET_LEN;
6108         }
6109 
6110         if (BIT_4 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
6111 
6112                 DEBUG2(ql4_printk(KERN_INFO, ha, "Setting BIDI chap\n"));
6113 
6114                 ret = qla4xxx_get_bidi_chap(ha,
6115                                     (char *)&boot_conn->chap.intr_chap_name,
6116                                     (char *)&boot_conn->chap.intr_secret);
6117 
6118                 if (ret) {
6119                         ql4_printk(KERN_ERR, ha, "Failed to set BIDI chap\n");
6120                         ret = QLA_ERROR;
6121                         goto exit_boot_target;
6122                 }
6123 
6124                 boot_conn->chap.intr_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
6125                 boot_conn->chap.intr_secret_length = QL4_CHAP_MAX_SECRET_LEN;
6126         }
6127 
6128 exit_boot_target:
6129         dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
6130                           fw_ddb_entry, fw_ddb_entry_dma);
6131         return ret;
6132 }
6133 
6134 static int qla4xxx_get_boot_info(struct scsi_qla_host *ha)
6135 {
6136         uint16_t ddb_index[2];
6137         int ret = QLA_ERROR;
6138         int rval;
6139 
6140         memset(ddb_index, 0, sizeof(ddb_index));
6141         ddb_index[0] = 0xffff;
6142         ddb_index[1] = 0xffff;
6143         ret = get_fw_boot_info(ha, ddb_index);
6144         if (ret != QLA_SUCCESS) {
6145                 DEBUG2(ql4_printk(KERN_INFO, ha,
6146                                 "%s: No boot target configured.\n", __func__));
6147                 return ret;
6148         }
6149 
6150         if (ql4xdisablesysfsboot)
6151                 return QLA_SUCCESS;
6152 
6153         if (ddb_index[0] == 0xffff)
6154                 goto sec_target;
6155 
6156         rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_pri_sess),
6157                                       ddb_index[0]);
6158         if (rval != QLA_SUCCESS) {
6159                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Primary boot target not "
6160                                   "configured\n", __func__));
6161         } else
6162                 ret = QLA_SUCCESS;
6163 
6164 sec_target:
6165         if (ddb_index[1] == 0xffff)
6166                 goto exit_get_boot_info;
6167 
6168         rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_sec_sess),
6169                                       ddb_index[1]);
6170         if (rval != QLA_SUCCESS) {
6171                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Secondary boot target not"
6172                                   " configured\n", __func__));
6173         } else
6174                 ret = QLA_SUCCESS;
6175 
6176 exit_get_boot_info:
6177         return ret;
6178 }
6179 
6180 static int qla4xxx_setup_boot_info(struct scsi_qla_host *ha)
6181 {
6182         struct iscsi_boot_kobj *boot_kobj;
6183 
6184         if (qla4xxx_get_boot_info(ha) != QLA_SUCCESS)
6185                 return QLA_ERROR;
6186 
6187         if (ql4xdisablesysfsboot) {
6188                 ql4_printk(KERN_INFO, ha,
6189                            "%s: syfsboot disabled - driver will trigger login "
6190                            "and publish session for discovery .\n", __func__);
6191                 return QLA_SUCCESS;
6192         }
6193 
6194 
6195         ha->boot_kset = iscsi_boot_create_host_kset(ha->host->host_no);
6196         if (!ha->boot_kset)
6197                 goto kset_free;
6198 
6199         if (!scsi_host_get(ha->host))
6200                 goto kset_free;
6201         boot_kobj = iscsi_boot_create_target(ha->boot_kset, 0, ha,
6202                                              qla4xxx_show_boot_tgt_pri_info,
6203                                              qla4xxx_tgt_get_attr_visibility,
6204                                              qla4xxx_boot_release);
6205         if (!boot_kobj)
6206                 goto put_host;
6207 
6208         if (!scsi_host_get(ha->host))
6209                 goto kset_free;
6210         boot_kobj = iscsi_boot_create_target(ha->boot_kset, 1, ha,
6211                                              qla4xxx_show_boot_tgt_sec_info,
6212                                              qla4xxx_tgt_get_attr_visibility,
6213                                              qla4xxx_boot_release);
6214         if (!boot_kobj)
6215                 goto put_host;
6216 
6217         if (!scsi_host_get(ha->host))
6218                 goto kset_free;
6219         boot_kobj = iscsi_boot_create_initiator(ha->boot_kset, 0, ha,
6220                                                qla4xxx_show_boot_ini_info,
6221                                                qla4xxx_ini_get_attr_visibility,
6222                                                qla4xxx_boot_release);
6223         if (!boot_kobj)
6224                 goto put_host;
6225 
6226         if (!scsi_host_get(ha->host))
6227                 goto kset_free;
6228         boot_kobj = iscsi_boot_create_ethernet(ha->boot_kset, 0, ha,
6229                                                qla4xxx_show_boot_eth_info,
6230                                                qla4xxx_eth_get_attr_visibility,
6231                                                qla4xxx_boot_release);
6232         if (!boot_kobj)
6233                 goto put_host;
6234 
6235         return QLA_SUCCESS;
6236 
6237 put_host:
6238         scsi_host_put(ha->host);
6239 kset_free:
6240         iscsi_boot_destroy_kset(ha->boot_kset);
6241         return -ENOMEM;
6242 }
6243 
6244 
6245 static void qla4xxx_get_param_ddb(struct ddb_entry *ddb_entry,
6246                                   struct ql4_tuple_ddb *tddb)
6247 {
6248         struct scsi_qla_host *ha;
6249         struct iscsi_cls_session *cls_sess;
6250         struct iscsi_cls_conn *cls_conn;
6251         struct iscsi_session *sess;
6252         struct iscsi_conn *conn;
6253 
6254         DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
6255         ha = ddb_entry->ha;
6256         cls_sess = ddb_entry->sess;
6257         sess = cls_sess->dd_data;
6258         cls_conn = ddb_entry->conn;
6259         conn = cls_conn->dd_data;
6260 
6261         tddb->tpgt = sess->tpgt;
6262         tddb->port = conn->persistent_port;
6263         strncpy(tddb->iscsi_name, sess->targetname, ISCSI_NAME_SIZE);
6264         strncpy(tddb->ip_addr, conn->persistent_address, DDB_IPADDR_LEN);
6265 }
6266 
6267 static void qla4xxx_convert_param_ddb(struct dev_db_entry *fw_ddb_entry,
6268                                       struct ql4_tuple_ddb *tddb,
6269                                       uint8_t *flash_isid)
6270 {
6271         uint16_t options = 0;
6272 
6273         tddb->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
6274         memcpy(&tddb->iscsi_name[0], &fw_ddb_entry->iscsi_name[0],
6275                min(sizeof(tddb->iscsi_name), sizeof(fw_ddb_entry->iscsi_name)));
6276 
6277         options = le16_to_cpu(fw_ddb_entry->options);
6278         if (options & DDB_OPT_IPV6_DEVICE)
6279                 sprintf(tddb->ip_addr, "%pI6", fw_ddb_entry->ip_addr);
6280         else
6281                 sprintf(tddb->ip_addr, "%pI4", fw_ddb_entry->ip_addr);
6282 
6283         tddb->port = le16_to_cpu(fw_ddb_entry->port);
6284 
6285         if (flash_isid == NULL)
6286                 memcpy(&tddb->isid[0], &fw_ddb_entry->isid[0],
6287                        sizeof(tddb->isid));
6288         else
6289                 memcpy(&tddb->isid[0], &flash_isid[0], sizeof(tddb->isid));
6290 }
6291 
6292 static int qla4xxx_compare_tuple_ddb(struct scsi_qla_host *ha,
6293                                      struct ql4_tuple_ddb *old_tddb,
6294                                      struct ql4_tuple_ddb *new_tddb,
6295                                      uint8_t is_isid_compare)
6296 {
6297         if (strcmp(old_tddb->iscsi_name, new_tddb->iscsi_name))
6298                 return QLA_ERROR;
6299 
6300         if (strcmp(old_tddb->ip_addr, new_tddb->ip_addr))
6301                 return QLA_ERROR;
6302 
6303         if (old_tddb->port != new_tddb->port)
6304                 return QLA_ERROR;
6305 
6306         /* For multi sessions, driver generates the ISID, so do not compare
6307          * ISID in reset path since it would be a comparison between the
6308          * driver generated ISID and firmware generated ISID. This could
6309          * lead to adding duplicated DDBs in the list as driver generated
6310          * ISID would not match firmware generated ISID.
6311          */
6312         if (is_isid_compare) {
6313                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: old ISID [%02x%02x%02x"
6314                         "%02x%02x%02x] New ISID [%02x%02x%02x%02x%02x%02x]\n",
6315                         __func__, old_tddb->isid[5], old_tddb->isid[4],
6316                         old_tddb->isid[3], old_tddb->isid[2], old_tddb->isid[1],
6317                         old_tddb->isid[0], new_tddb->isid[5], new_tddb->isid[4],
6318                         new_tddb->isid[3], new_tddb->isid[2], new_tddb->isid[1],
6319                         new_tddb->isid[0]));
6320 
6321                 if (memcmp(&old_tddb->isid[0], &new_tddb->isid[0],
6322                            sizeof(old_tddb->isid)))
6323                         return QLA_ERROR;
6324         }
6325 
6326         DEBUG2(ql4_printk(KERN_INFO, ha,
6327                           "Match Found, fw[%d,%d,%s,%s], [%d,%d,%s,%s]",
6328                           old_tddb->port, old_tddb->tpgt, old_tddb->ip_addr,
6329                           old_tddb->iscsi_name, new_tddb->port, new_tddb->tpgt,
6330                           new_tddb->ip_addr, new_tddb->iscsi_name));
6331 
6332         return QLA_SUCCESS;
6333 }
6334 
6335 static int qla4xxx_is_session_exists(struct scsi_qla_host *ha,
6336                                      struct dev_db_entry *fw_ddb_entry,
6337                                      uint32_t *index)
6338 {
6339         struct ddb_entry *ddb_entry;
6340         struct ql4_tuple_ddb *fw_tddb = NULL;
6341         struct ql4_tuple_ddb *tmp_tddb = NULL;
6342         int idx;
6343         int ret = QLA_ERROR;
6344 
6345         fw_tddb = vzalloc(sizeof(*fw_tddb));
6346         if (!fw_tddb) {
6347                 DEBUG2(ql4_printk(KERN_WARNING, ha,
6348                                   "Memory Allocation failed.\n"));
6349                 ret = QLA_SUCCESS;
6350                 goto exit_check;
6351         }
6352 
6353         tmp_tddb = vzalloc(sizeof(*tmp_tddb));
6354         if (!tmp_tddb) {
6355                 DEBUG2(ql4_printk(KERN_WARNING, ha,
6356                                   "Memory Allocation failed.\n"));
6357                 ret = QLA_SUCCESS;
6358                 goto exit_check;
6359         }
6360 
6361         qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb, NULL);
6362 
6363         for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
6364                 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
6365                 if (ddb_entry == NULL)
6366                         continue;
6367 
6368                 qla4xxx_get_param_ddb(ddb_entry, tmp_tddb);
6369                 if (!qla4xxx_compare_tuple_ddb(ha, fw_tddb, tmp_tddb, false)) {
6370                         ret = QLA_SUCCESS; /* found */
6371                         if (index != NULL)
6372                                 *index = idx;
6373                         goto exit_check;
6374                 }
6375         }
6376 
6377 exit_check:
6378         if (fw_tddb)
6379                 vfree(fw_tddb);
6380         if (tmp_tddb)
6381                 vfree(tmp_tddb);
6382         return ret;
6383 }
6384 
6385 /**
6386  * qla4xxx_check_existing_isid - check if target with same isid exist
6387  *                               in target list
6388  * @list_nt: list of target
6389  * @isid: isid to check
6390  *
6391  * This routine return QLA_SUCCESS if target with same isid exist
6392  **/
6393 static int qla4xxx_check_existing_isid(struct list_head *list_nt, uint8_t *isid)
6394 {
6395         struct qla_ddb_index *nt_ddb_idx, *nt_ddb_idx_tmp;
6396         struct dev_db_entry *fw_ddb_entry;
6397 
6398         list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
6399                 fw_ddb_entry = &nt_ddb_idx->fw_ddb;
6400 
6401                 if (memcmp(&fw_ddb_entry->isid[0], &isid[0],
6402                            sizeof(nt_ddb_idx->fw_ddb.isid)) == 0) {
6403                         return QLA_SUCCESS;
6404                 }
6405         }
6406         return QLA_ERROR;
6407 }
6408 
6409 /**
6410  * qla4xxx_update_isid - compare ddbs and updated isid
6411  * @ha: Pointer to host adapter structure.
6412  * @list_nt: list of nt target
6413  * @fw_ddb_entry: firmware ddb entry
6414  *
6415  * This routine update isid if ddbs have same iqn, same isid and
6416  * different IP addr.
6417  * Return QLA_SUCCESS if isid is updated.
6418  **/
6419 static int qla4xxx_update_isid(struct scsi_qla_host *ha,
6420                                struct list_head *list_nt,
6421                                struct dev_db_entry *fw_ddb_entry)
6422 {
6423         uint8_t base_value, i;
6424 
6425         base_value = fw_ddb_entry->isid[1] & 0x1f;
6426         for (i = 0; i < 8; i++) {
6427                 fw_ddb_entry->isid[1] = (base_value | (i << 5));
6428                 if (qla4xxx_check_existing_isid(list_nt, fw_ddb_entry->isid))
6429                         break;
6430         }
6431 
6432         if (!qla4xxx_check_existing_isid(list_nt, fw_ddb_entry->isid))
6433                 return QLA_ERROR;
6434 
6435         return QLA_SUCCESS;
6436 }
6437 
6438 /**
6439  * qla4xxx_should_update_isid - check if isid need to update
6440  * @ha: Pointer to host adapter structure.
6441  * @old_tddb: ddb tuple
6442  * @new_tddb: ddb tuple
6443  *
6444  * Return QLA_SUCCESS if different IP, different PORT, same iqn,
6445  * same isid
6446  **/
6447 static int qla4xxx_should_update_isid(struct scsi_qla_host *ha,
6448                                       struct ql4_tuple_ddb *old_tddb,
6449                                       struct ql4_tuple_ddb *new_tddb)
6450 {
6451         if (strcmp(old_tddb->ip_addr, new_tddb->ip_addr) == 0) {
6452                 /* Same ip */
6453                 if (old_tddb->port == new_tddb->port)
6454                         return QLA_ERROR;
6455         }
6456 
6457         if (strcmp(old_tddb->iscsi_name, new_tddb->iscsi_name))
6458                 /* different iqn */
6459                 return QLA_ERROR;
6460 
6461         if (memcmp(&old_tddb->isid[0], &new_tddb->isid[0],
6462                    sizeof(old_tddb->isid)))
6463                 /* different isid */
6464                 return QLA_ERROR;
6465 
6466         return QLA_SUCCESS;
6467 }
6468 
6469 /**
6470  * qla4xxx_is_flash_ddb_exists - check if fw_ddb_entry already exists in list_nt
6471  * @ha: Pointer to host adapter structure.
6472  * @list_nt: list of nt target.
6473  * @fw_ddb_entry: firmware ddb entry.
6474  *
6475  * This routine check if fw_ddb_entry already exists in list_nt to avoid
6476  * duplicate ddb in list_nt.
6477  * Return QLA_SUCCESS if duplicate ddb exit in list_nl.
6478  * Note: This function also update isid of DDB if required.
6479  **/
6480 
6481 static int qla4xxx_is_flash_ddb_exists(struct scsi_qla_host *ha,
6482                                        struct list_head *list_nt,
6483                                        struct dev_db_entry *fw_ddb_entry)
6484 {
6485         struct qla_ddb_index  *nt_ddb_idx, *nt_ddb_idx_tmp;
6486         struct ql4_tuple_ddb *fw_tddb = NULL;
6487         struct ql4_tuple_ddb *tmp_tddb = NULL;
6488         int rval, ret = QLA_ERROR;
6489 
6490         fw_tddb = vzalloc(sizeof(*fw_tddb));
6491         if (!fw_tddb) {
6492                 DEBUG2(ql4_printk(KERN_WARNING, ha,
6493                                   "Memory Allocation failed.\n"));
6494                 ret = QLA_SUCCESS;
6495                 goto exit_check;
6496         }
6497 
6498         tmp_tddb = vzalloc(sizeof(*tmp_tddb));
6499         if (!tmp_tddb) {
6500                 DEBUG2(ql4_printk(KERN_WARNING, ha,
6501                                   "Memory Allocation failed.\n"));
6502                 ret = QLA_SUCCESS;
6503                 goto exit_check;
6504         }
6505 
6506         qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb, NULL);
6507 
6508         list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
6509                 qla4xxx_convert_param_ddb(&nt_ddb_idx->fw_ddb, tmp_tddb,
6510                                           nt_ddb_idx->flash_isid);
6511                 ret = qla4xxx_compare_tuple_ddb(ha, fw_tddb, tmp_tddb, true);
6512                 /* found duplicate ddb */
6513                 if (ret == QLA_SUCCESS)
6514                         goto exit_check;
6515         }
6516 
6517         list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
6518                 qla4xxx_convert_param_ddb(&nt_ddb_idx->fw_ddb, tmp_tddb, NULL);
6519 
6520                 ret = qla4xxx_should_update_isid(ha, tmp_tddb, fw_tddb);
6521                 if (ret == QLA_SUCCESS) {
6522                         rval = qla4xxx_update_isid(ha, list_nt, fw_ddb_entry);
6523                         if (rval == QLA_SUCCESS)
6524                                 ret = QLA_ERROR;
6525                         else
6526                                 ret = QLA_SUCCESS;
6527 
6528                         goto exit_check;
6529                 }
6530         }
6531 
6532 exit_check:
6533         if (fw_tddb)
6534                 vfree(fw_tddb);
6535         if (tmp_tddb)
6536                 vfree(tmp_tddb);
6537         return ret;
6538 }
6539 
6540 static void qla4xxx_free_ddb_list(struct list_head *list_ddb)
6541 {
6542         struct qla_ddb_index  *ddb_idx, *ddb_idx_tmp;
6543 
6544         list_for_each_entry_safe(ddb_idx, ddb_idx_tmp, list_ddb, list) {
6545                 list_del_init(&ddb_idx->list);
6546                 vfree(ddb_idx);
6547         }
6548 }
6549 
6550 static struct iscsi_endpoint *qla4xxx_get_ep_fwdb(struct scsi_qla_host *ha,
6551                                         struct dev_db_entry *fw_ddb_entry)
6552 {
6553         struct iscsi_endpoint *ep;
6554         struct sockaddr_in *addr;
6555         struct sockaddr_in6 *addr6;
6556         struct sockaddr *t_addr;
6557         struct sockaddr_storage *dst_addr;
6558         char *ip;
6559 
6560         /* TODO: need to destroy on unload iscsi_endpoint*/
6561         dst_addr = vmalloc(sizeof(*dst_addr));
6562         if (!dst_addr)
6563                 return NULL;
6564 
6565         if (fw_ddb_entry->options & DDB_OPT_IPV6_DEVICE) {
6566                 t_addr = (struct sockaddr *)dst_addr;
6567                 t_addr->sa_family = AF_INET6;
6568                 addr6 = (struct sockaddr_in6 *)dst_addr;
6569                 ip = (char *)&addr6->sin6_addr;
6570                 memcpy(ip, fw_ddb_entry->ip_addr, IPv6_ADDR_LEN);
6571                 addr6->sin6_port = htons(le16_to_cpu(fw_ddb_entry->port));
6572 
6573         } else {
6574                 t_addr = (struct sockaddr *)dst_addr;
6575                 t_addr->sa_family = AF_INET;
6576                 addr = (struct sockaddr_in *)dst_addr;
6577                 ip = (char *)&addr->sin_addr;
6578                 memcpy(ip, fw_ddb_entry->ip_addr, IP_ADDR_LEN);
6579                 addr->sin_port = htons(le16_to_cpu(fw_ddb_entry->port));
6580         }
6581 
6582         ep = qla4xxx_ep_connect(ha->host, (struct sockaddr *)dst_addr, 0);
6583         vfree(dst_addr);
6584         return ep;
6585 }
6586 
6587 static int qla4xxx_verify_boot_idx(struct scsi_qla_host *ha, uint16_t idx)
6588 {
6589         if (ql4xdisablesysfsboot)
6590                 return QLA_SUCCESS;
6591         if (idx == ha->pri_ddb_idx || idx == ha->sec_ddb_idx)
6592                 return QLA_ERROR;
6593         return QLA_SUCCESS;
6594 }
6595 
6596 static void qla4xxx_setup_flash_ddb_entry(struct scsi_qla_host *ha,
6597                                           struct ddb_entry *ddb_entry,
6598                                           uint16_t idx)
6599 {
6600         uint16_t def_timeout;
6601 
6602         ddb_entry->ddb_type = FLASH_DDB;
6603         ddb_entry->fw_ddb_index = INVALID_ENTRY;
6604         ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
6605         ddb_entry->ha = ha;
6606         ddb_entry->unblock_sess = qla4xxx_unblock_flash_ddb;
6607         ddb_entry->ddb_change = qla4xxx_flash_ddb_change;
6608         ddb_entry->chap_tbl_idx = INVALID_ENTRY;
6609 
6610         atomic_set(&ddb_entry->retry_relogin_timer, INVALID_ENTRY);
6611         atomic_set(&ddb_entry->relogin_timer, 0);
6612         atomic_set(&ddb_entry->relogin_retry_count, 0);
6613         def_timeout = le16_to_cpu(ddb_entry->fw_ddb_entry.def_timeout);
6614         ddb_entry->default_relogin_timeout =
6615                 (def_timeout > LOGIN_TOV) && (def_timeout < LOGIN_TOV * 10) ?
6616                 def_timeout : LOGIN_TOV;
6617         ddb_entry->default_time2wait =
6618                 le16_to_cpu(ddb_entry->fw_ddb_entry.iscsi_def_time2wait);
6619 
6620         if (ql4xdisablesysfsboot &&
6621             (idx == ha->pri_ddb_idx || idx == ha->sec_ddb_idx))
6622                 set_bit(DF_BOOT_TGT, &ddb_entry->flags);
6623 }
6624 
6625 static void qla4xxx_wait_for_ip_configuration(struct scsi_qla_host *ha)
6626 {
6627         uint32_t idx = 0;
6628         uint32_t ip_idx[IP_ADDR_COUNT] = {0, 1, 2, 3}; /* 4 IP interfaces */
6629         uint32_t sts[MBOX_REG_COUNT];
6630         uint32_t ip_state;
6631         unsigned long wtime;
6632         int ret;
6633 
6634         wtime = jiffies + (HZ * IP_CONFIG_TOV);
6635         do {
6636                 for (idx = 0; idx < IP_ADDR_COUNT; idx++) {
6637                         if (ip_idx[idx] == -1)
6638                                 continue;
6639 
6640                         ret = qla4xxx_get_ip_state(ha, 0, ip_idx[idx], sts);
6641 
6642                         if (ret == QLA_ERROR) {
6643                                 ip_idx[idx] = -1;
6644                                 continue;
6645                         }
6646 
6647                         ip_state = (sts[1] & IP_STATE_MASK) >> IP_STATE_SHIFT;
6648 
6649                         DEBUG2(ql4_printk(KERN_INFO, ha,
6650                                           "Waiting for IP state for idx = %d, state = 0x%x\n",
6651                                           ip_idx[idx], ip_state));
6652                         if (ip_state == IP_ADDRSTATE_UNCONFIGURED ||
6653                             ip_state == IP_ADDRSTATE_INVALID ||
6654                             ip_state == IP_ADDRSTATE_PREFERRED ||
6655                             ip_state == IP_ADDRSTATE_DEPRICATED ||
6656                             ip_state == IP_ADDRSTATE_DISABLING)
6657                                 ip_idx[idx] = -1;
6658                 }
6659 
6660                 /* Break if all IP states checked */
6661                 if ((ip_idx[0] == -1) &&
6662                     (ip_idx[1] == -1) &&
6663                     (ip_idx[2] == -1) &&
6664                     (ip_idx[3] == -1))
6665                         break;
6666                 schedule_timeout_uninterruptible(HZ);
6667         } while (time_after(wtime, jiffies));
6668 }
6669 
6670 static int qla4xxx_cmp_fw_stentry(struct dev_db_entry *fw_ddb_entry,
6671                                   struct dev_db_entry *flash_ddb_entry)
6672 {
6673         uint16_t options = 0;
6674         size_t ip_len = IP_ADDR_LEN;
6675 
6676         options = le16_to_cpu(fw_ddb_entry->options);
6677         if (options & DDB_OPT_IPV6_DEVICE)
6678                 ip_len = IPv6_ADDR_LEN;
6679 
6680         if (memcmp(fw_ddb_entry->ip_addr, flash_ddb_entry->ip_addr, ip_len))
6681                 return QLA_ERROR;
6682 
6683         if (memcmp(&fw_ddb_entry->isid[0], &flash_ddb_entry->isid[0],
6684                    sizeof(fw_ddb_entry->isid)))
6685                 return QLA_ERROR;
6686 
6687         if (memcmp(&fw_ddb_entry->port, &flash_ddb_entry->port,
6688                    sizeof(fw_ddb_entry->port)))
6689                 return QLA_ERROR;
6690 
6691         return QLA_SUCCESS;
6692 }
6693 
6694 static int qla4xxx_find_flash_st_idx(struct scsi_qla_host *ha,
6695                                      struct dev_db_entry *fw_ddb_entry,
6696                                      uint32_t fw_idx, uint32_t *flash_index)
6697 {
6698         struct dev_db_entry *flash_ddb_entry;
6699         dma_addr_t flash_ddb_entry_dma;
6700         uint32_t idx = 0;
6701         int max_ddbs;
6702         int ret = QLA_ERROR, status;
6703 
6704         max_ddbs =  is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
6705                                      MAX_DEV_DB_ENTRIES;
6706 
6707         flash_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
6708                                          &flash_ddb_entry_dma);
6709         if (flash_ddb_entry == NULL || fw_ddb_entry == NULL) {
6710                 ql4_printk(KERN_ERR, ha, "Out of memory\n");
6711                 goto exit_find_st_idx;
6712         }
6713 
6714         status = qla4xxx_flashdb_by_index(ha, flash_ddb_entry,
6715                                           flash_ddb_entry_dma, fw_idx);
6716         if (status == QLA_SUCCESS) {
6717                 status = qla4xxx_cmp_fw_stentry(fw_ddb_entry, flash_ddb_entry);
6718                 if (status == QLA_SUCCESS) {
6719                         *flash_index = fw_idx;
6720                         ret = QLA_SUCCESS;
6721                         goto exit_find_st_idx;
6722                 }
6723         }
6724 
6725         for (idx = 0; idx < max_ddbs; idx++) {
6726                 status = qla4xxx_flashdb_by_index(ha, flash_ddb_entry,
6727                                                   flash_ddb_entry_dma, idx);
6728                 if (status == QLA_ERROR)
6729                         continue;
6730 
6731                 status = qla4xxx_cmp_fw_stentry(fw_ddb_entry, flash_ddb_entry);
6732                 if (status == QLA_SUCCESS) {
6733                         *flash_index = idx;
6734                         ret = QLA_SUCCESS;
6735                         goto exit_find_st_idx;
6736                 }
6737         }
6738 
6739         if (idx == max_ddbs)
6740                 ql4_printk(KERN_ERR, ha, "Failed to find ST [%d] in flash\n",
6741                            fw_idx);
6742 
6743 exit_find_st_idx:
6744         if (flash_ddb_entry)
6745                 dma_pool_free(ha->fw_ddb_dma_pool, flash_ddb_entry,
6746                               flash_ddb_entry_dma);
6747 
6748         return ret;
6749 }
6750 
6751 static void qla4xxx_build_st_list(struct scsi_qla_host *ha,
6752                                   struct list_head *list_st)
6753 {
6754         struct qla_ddb_index  *st_ddb_idx;
6755         int max_ddbs;
6756         int fw_idx_size;
6757         struct dev_db_entry *fw_ddb_entry;
6758         dma_addr_t fw_ddb_dma;
6759         int ret;
6760         uint32_t idx = 0, next_idx = 0;
6761         uint32_t state = 0, conn_err = 0;
6762         uint32_t flash_index = -1;
6763         uint16_t conn_id = 0;
6764 
6765         fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
6766                                       &fw_ddb_dma);
6767         if (fw_ddb_entry == NULL) {
6768                 DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
6769                 goto exit_st_list;
6770         }
6771 
6772         max_ddbs =  is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
6773                                      MAX_DEV_DB_ENTRIES;
6774         fw_idx_size = sizeof(struct qla_ddb_index);
6775 
6776         for (idx = 0; idx < max_ddbs; idx = next_idx) {
6777                 ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry, fw_ddb_dma,
6778                                               NULL, &next_idx, &state,
6779                                               &conn_err, NULL, &conn_id);
6780                 if (ret == QLA_ERROR)
6781                         break;
6782 
6783                 /* Ignore DDB if invalid state (unassigned) */
6784                 if (state == DDB_DS_UNASSIGNED)
6785                         goto continue_next_st;
6786 
6787                 /* Check if ST, add to the list_st */
6788                 if (strlen((char *) fw_ddb_entry->iscsi_name) != 0)
6789                         goto continue_next_st;
6790 
6791                 st_ddb_idx = vzalloc(fw_idx_size);
6792                 if (!st_ddb_idx)
6793                         break;
6794 
6795                 ret = qla4xxx_find_flash_st_idx(ha, fw_ddb_entry, idx,
6796                                                 &flash_index);
6797                 if (ret == QLA_ERROR) {
6798                         ql4_printk(KERN_ERR, ha,
6799                                    "No flash entry for ST at idx [%d]\n", idx);
6800                         st_ddb_idx->flash_ddb_idx = idx;
6801                 } else {
6802                         ql4_printk(KERN_INFO, ha,
6803                                    "ST at idx [%d] is stored at flash [%d]\n",
6804                                    idx, flash_index);
6805                         st_ddb_idx->flash_ddb_idx = flash_index;
6806                 }
6807 
6808                 st_ddb_idx->fw_ddb_idx = idx;
6809 
6810                 list_add_tail(&st_ddb_idx->list, list_st);
6811 continue_next_st:
6812                 if (next_idx == 0)
6813                         break;
6814         }
6815 
6816 exit_st_list:
6817         if (fw_ddb_entry)
6818                 dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
6819 }
6820 
6821 /**
6822  * qla4xxx_remove_failed_ddb - Remove inactive or failed ddb from list
6823  * @ha: pointer to adapter structure
6824  * @list_ddb: List from which failed ddb to be removed
6825  *
6826  * Iterate over the list of DDBs and find and remove DDBs that are either in
6827  * no connection active state or failed state
6828  **/
6829 static void qla4xxx_remove_failed_ddb(struct scsi_qla_host *ha,
6830                                       struct list_head *list_ddb)
6831 {
6832         struct qla_ddb_index  *ddb_idx, *ddb_idx_tmp;
6833         uint32_t next_idx = 0;
6834         uint32_t state = 0, conn_err = 0;
6835         int ret;
6836 
6837         list_for_each_entry_safe(ddb_idx, ddb_idx_tmp, list_ddb, list) {
6838                 ret = qla4xxx_get_fwddb_entry(ha, ddb_idx->fw_ddb_idx,
6839                                               NULL, 0, NULL, &next_idx, &state,
6840                                               &conn_err, NULL, NULL);
6841                 if (ret == QLA_ERROR)
6842                         continue;
6843 
6844                 if (state == DDB_DS_NO_CONNECTION_ACTIVE ||
6845                     state == DDB_DS_SESSION_FAILED) {
6846                         list_del_init(&ddb_idx->list);
6847                         vfree(ddb_idx);
6848                 }
6849         }
6850 }
6851 
6852 static void qla4xxx_update_sess_disc_idx(struct scsi_qla_host *ha,
6853                                          struct ddb_entry *ddb_entry,
6854                                          struct dev_db_entry *fw_ddb_entry)
6855 {
6856         struct iscsi_cls_session *cls_sess;
6857         struct iscsi_session *sess;
6858         uint32_t max_ddbs = 0;
6859         uint16_t ddb_link = -1;
6860 
6861         max_ddbs =  is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
6862                                      MAX_DEV_DB_ENTRIES;
6863 
6864         cls_sess = ddb_entry->sess;
6865         sess = cls_sess->dd_data;
6866 
6867         ddb_link = le16_to_cpu(fw_ddb_entry->ddb_link);
6868         if (ddb_link < max_ddbs)
6869                 sess->discovery_parent_idx = ddb_link;
6870         else
6871                 sess->discovery_parent_idx = DDB_NO_LINK;
6872 }
6873 
6874 static int qla4xxx_sess_conn_setup(struct scsi_qla_host *ha,
6875                                    struct dev_db_entry *fw_ddb_entry,
6876                                    int is_reset, uint16_t idx)
6877 {
6878         struct iscsi_cls_session *cls_sess;
6879         struct iscsi_session *sess;
6880         struct iscsi_cls_conn *cls_conn;
6881         struct iscsi_endpoint *ep;
6882         uint16_t cmds_max = 32;
6883         uint16_t conn_id = 0;
6884         uint32_t initial_cmdsn = 0;
6885         int ret = QLA_SUCCESS;
6886 
6887         struct ddb_entry *ddb_entry = NULL;
6888 
6889         /* Create session object, with INVALID_ENTRY,
6890          * the targer_id would get set when we issue the login
6891          */
6892         cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport, ha->host,
6893                                        cmds_max, sizeof(struct ddb_entry),
6894                                        sizeof(struct ql4_task_data),
6895                                        initial_cmdsn, INVALID_ENTRY);
6896         if (!cls_sess) {
6897                 ret = QLA_ERROR;
6898                 goto exit_setup;
6899         }
6900 
6901         /*
6902          * so calling module_put function to decrement the
6903          * reference count.
6904          **/
6905         module_put(qla4xxx_iscsi_transport.owner);
6906         sess = cls_sess->dd_data;
6907         ddb_entry = sess->dd_data;
6908         ddb_entry->sess = cls_sess;
6909 
6910         cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
6911         memcpy(&ddb_entry->fw_ddb_entry, fw_ddb_entry,
6912                sizeof(struct dev_db_entry));
6913 
6914         qla4xxx_setup_flash_ddb_entry(ha, ddb_entry, idx);
6915 
6916         cls_conn = iscsi_conn_setup(cls_sess, sizeof(struct qla_conn), conn_id);
6917 
6918         if (!cls_conn) {
6919                 ret = QLA_ERROR;
6920                 goto exit_setup;
6921         }
6922 
6923         ddb_entry->conn = cls_conn;
6924 
6925         /* Setup ep, for displaying attributes in sysfs */
6926         ep = qla4xxx_get_ep_fwdb(ha, fw_ddb_entry);
6927         if (ep) {
6928                 ep->conn = cls_conn;
6929                 cls_conn->ep = ep;
6930         } else {
6931                 DEBUG2(ql4_printk(KERN_ERR, ha, "Unable to get ep\n"));
6932                 ret = QLA_ERROR;
6933                 goto exit_setup;
6934         }
6935 
6936         /* Update sess/conn params */
6937         qla4xxx_copy_fwddb_param(ha, fw_ddb_entry, cls_sess, cls_conn);
6938         qla4xxx_update_sess_disc_idx(ha, ddb_entry, fw_ddb_entry);
6939 
6940         if (is_reset == RESET_ADAPTER) {
6941                 iscsi_block_session(cls_sess);
6942                 /* Use the relogin path to discover new devices
6943                  *  by short-circuting the logic of setting
6944                  *  timer to relogin - instead set the flags
6945                  *  to initiate login right away.
6946                  */
6947                 set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
6948                 set_bit(DF_RELOGIN, &ddb_entry->flags);
6949         }
6950 
6951 exit_setup:
6952         return ret;
6953 }
6954 
6955 static void qla4xxx_update_fw_ddb_link(struct scsi_qla_host *ha,
6956                                        struct list_head *list_ddb,
6957                                        struct dev_db_entry *fw_ddb_entry)
6958 {
6959         struct qla_ddb_index  *ddb_idx, *ddb_idx_tmp;
6960         uint16_t ddb_link;
6961 
6962         ddb_link = le16_to_cpu(fw_ddb_entry->ddb_link);
6963 
6964         list_for_each_entry_safe(ddb_idx, ddb_idx_tmp, list_ddb, list) {
6965                 if (ddb_idx->fw_ddb_idx == ddb_link) {
6966                         DEBUG2(ql4_printk(KERN_INFO, ha,
6967                                           "Updating NT parent idx from [%d] to [%d]\n",
6968                                           ddb_link, ddb_idx->flash_ddb_idx));
6969                         fw_ddb_entry->ddb_link =
6970                                             cpu_to_le16(ddb_idx->flash_ddb_idx);
6971                         return;
6972                 }
6973         }
6974 }
6975 
6976 static void qla4xxx_build_nt_list(struct scsi_qla_host *ha,
6977                                   struct list_head *list_nt,
6978                                   struct list_head *list_st,
6979                                   int is_reset)
6980 {
6981         struct dev_db_entry *fw_ddb_entry;
6982         struct ddb_entry *ddb_entry = NULL;
6983         dma_addr_t fw_ddb_dma;
6984         int max_ddbs;
6985         int fw_idx_size;
6986         int ret;
6987         uint32_t idx = 0, next_idx = 0;
6988         uint32_t state = 0, conn_err = 0;
6989         uint32_t ddb_idx = -1;
6990         uint16_t conn_id = 0;
6991         uint16_t ddb_link = -1;
6992         struct qla_ddb_index  *nt_ddb_idx;
6993 
6994         fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
6995                                       &fw_ddb_dma);
6996         if (fw_ddb_entry == NULL) {
6997                 DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
6998                 goto exit_nt_list;
6999         }
7000         max_ddbs =  is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
7001                                      MAX_DEV_DB_ENTRIES;
7002         fw_idx_size = sizeof(struct qla_ddb_index);
7003 
7004         for (idx = 0; idx < max_ddbs; idx = next_idx) {
7005                 ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry, fw_ddb_dma,
7006                                               NULL, &next_idx, &state,
7007                                               &conn_err, NULL, &conn_id);
7008                 if (ret == QLA_ERROR)
7009                         break;
7010 
7011                 if (qla4xxx_verify_boot_idx(ha, idx) != QLA_SUCCESS)
7012                         goto continue_next_nt;
7013 
7014                 /* Check if NT, then add to list it */
7015                 if (strlen((char *) fw_ddb_entry->iscsi_name) == 0)
7016                         goto continue_next_nt;
7017 
7018                 ddb_link = le16_to_cpu(fw_ddb_entry->ddb_link);
7019                 if (ddb_link < max_ddbs)
7020                         qla4xxx_update_fw_ddb_link(ha, list_st, fw_ddb_entry);
7021 
7022                 if (!(state == DDB_DS_NO_CONNECTION_ACTIVE ||
7023                     state == DDB_DS_SESSION_FAILED) &&
7024                     (is_reset == INIT_ADAPTER))
7025                         goto continue_next_nt;
7026 
7027                 DEBUG2(ql4_printk(KERN_INFO, ha,
7028                                   "Adding  DDB to session = 0x%x\n", idx));
7029 
7030                 if (is_reset == INIT_ADAPTER) {
7031                         nt_ddb_idx = vmalloc(fw_idx_size);
7032                         if (!nt_ddb_idx)
7033                                 break;
7034 
7035                         nt_ddb_idx->fw_ddb_idx = idx;
7036 
7037                         /* Copy original isid as it may get updated in function
7038                          * qla4xxx_update_isid(). We need original isid in
7039                          * function qla4xxx_compare_tuple_ddb to find duplicate
7040                          * target */
7041                         memcpy(&nt_ddb_idx->flash_isid[0],
7042                                &fw_ddb_entry->isid[0],
7043                                sizeof(nt_ddb_idx->flash_isid));
7044 
7045                         ret = qla4xxx_is_flash_ddb_exists(ha, list_nt,
7046                                                           fw_ddb_entry);
7047                         if (ret == QLA_SUCCESS) {
7048                                 /* free nt_ddb_idx and do not add to list_nt */
7049                                 vfree(nt_ddb_idx);
7050                                 goto continue_next_nt;
7051                         }
7052 
7053                         /* Copy updated isid */
7054                         memcpy(&nt_ddb_idx->fw_ddb, fw_ddb_entry,
7055                                sizeof(struct dev_db_entry));
7056 
7057                         list_add_tail(&nt_ddb_idx->list, list_nt);
7058                 } else if (is_reset == RESET_ADAPTER) {
7059                         ret = qla4xxx_is_session_exists(ha, fw_ddb_entry,
7060                                                         &ddb_idx);
7061                         if (ret == QLA_SUCCESS) {
7062                                 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha,
7063                                                                        ddb_idx);
7064                                 if (ddb_entry != NULL)
7065                                         qla4xxx_update_sess_disc_idx(ha,
7066                                                                      ddb_entry,
7067                                                                   fw_ddb_entry);
7068                                 goto continue_next_nt;
7069                         }
7070                 }
7071 
7072                 ret = qla4xxx_sess_conn_setup(ha, fw_ddb_entry, is_reset, idx);
7073                 if (ret == QLA_ERROR)
7074                         goto exit_nt_list;
7075 
7076 continue_next_nt:
7077                 if (next_idx == 0)
7078                         break;
7079         }
7080 
7081 exit_nt_list:
7082         if (fw_ddb_entry)
7083                 dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
7084 }
7085 
7086 static void qla4xxx_build_new_nt_list(struct scsi_qla_host *ha,
7087                                       struct list_head *list_nt,
7088                                       uint16_t target_id)
7089 {
7090         struct dev_db_entry *fw_ddb_entry;
7091         dma_addr_t fw_ddb_dma;
7092         int max_ddbs;
7093         int fw_idx_size;
7094         int ret;
7095         uint32_t idx = 0, next_idx = 0;
7096         uint32_t state = 0, conn_err = 0;
7097         uint16_t conn_id = 0;