Version:  2.0.40 2.2.26 2.4.37 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8

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