SCSI: qla4xxx: overflow in qla4xxx_set_chap_entry()
[opensuse:kernel.git] / drivers / scsi / qla4xxx / ql4_os.c
1 /*
2  * QLogic iSCSI HBA Driver
3  * Copyright (c)  2003-2013 QLogic Corporation
4  *
5  * See LICENSE.qla4xxx for copyright and licensing details.
6  */
7 #include <linux/moduleparam.h>
8 #include <linux/slab.h>
9 #include <linux/blkdev.h>
10 #include <linux/iscsi_boot_sysfs.h>
11 #include <linux/inet.h>
12
13 #include <scsi/scsi_tcq.h>
14 #include <scsi/scsicam.h>
15
16 #include "ql4_def.h"
17 #include "ql4_version.h"
18 #include "ql4_glbl.h"
19 #include "ql4_dbg.h"
20 #include "ql4_inline.h"
21 #include "ql4_83xx.h"
22
23 /*
24  * Driver version
25  */
26 static char qla4xxx_version_str[40];
27
28 /*
29  * SRB allocation cache
30  */
31 static struct kmem_cache *srb_cachep;
32
33 /*
34  * Module parameter information and variables
35  */
36 static int ql4xdisablesysfsboot = 1;
37 module_param(ql4xdisablesysfsboot, int, S_IRUGO | S_IWUSR);
38 MODULE_PARM_DESC(ql4xdisablesysfsboot,
39                  " Set to disable exporting boot targets to sysfs.\n"
40                  "\t\t  0 - Export boot targets\n"
41                  "\t\t  1 - Do not export boot targets (Default)");
42
43 int ql4xdontresethba;
44 module_param(ql4xdontresethba, int, S_IRUGO | S_IWUSR);
45 MODULE_PARM_DESC(ql4xdontresethba,
46                  " Don't reset the HBA for driver recovery.\n"
47                  "\t\t  0 - It will reset HBA (Default)\n"
48                  "\t\t  1 - It will NOT reset HBA");
49
50 int ql4xextended_error_logging;
51 module_param(ql4xextended_error_logging, int, S_IRUGO | S_IWUSR);
52 MODULE_PARM_DESC(ql4xextended_error_logging,
53                  " Option to enable extended error logging.\n"
54                  "\t\t  0 - no logging (Default)\n"
55                  "\t\t  2 - debug logging");
56
57 int ql4xenablemsix = 1;
58 module_param(ql4xenablemsix, int, S_IRUGO|S_IWUSR);
59 MODULE_PARM_DESC(ql4xenablemsix,
60                  " Set to enable MSI or MSI-X interrupt mechanism.\n"
61                  "\t\t  0 = enable INTx interrupt mechanism.\n"
62                  "\t\t  1 = enable MSI-X interrupt mechanism (Default).\n"
63                  "\t\t  2 = enable MSI interrupt mechanism.");
64
65 #define QL4_DEF_QDEPTH 32
66 static int ql4xmaxqdepth = QL4_DEF_QDEPTH;
67 module_param(ql4xmaxqdepth, int, S_IRUGO | S_IWUSR);
68 MODULE_PARM_DESC(ql4xmaxqdepth,
69                  " Maximum queue depth to report for target devices.\n"
70                  "\t\t  Default: 32.");
71
72 static int ql4xqfulltracking = 1;
73 module_param(ql4xqfulltracking, int, S_IRUGO | S_IWUSR);
74 MODULE_PARM_DESC(ql4xqfulltracking,
75                  " Enable or disable dynamic tracking and adjustment of\n"
76                  "\t\t scsi device queue depth.\n"
77                  "\t\t  0 - Disable.\n"
78                  "\t\t  1 - Enable. (Default)");
79
80 static int ql4xsess_recovery_tmo = QL4_SESS_RECOVERY_TMO;
81 module_param(ql4xsess_recovery_tmo, int, S_IRUGO);
82 MODULE_PARM_DESC(ql4xsess_recovery_tmo,
83                 " Target Session Recovery Timeout.\n"
84                 "\t\t  Default: 120 sec.");
85
86 int ql4xmdcapmask = 0x1F;
87 module_param(ql4xmdcapmask, int, S_IRUGO);
88 MODULE_PARM_DESC(ql4xmdcapmask,
89                  " Set the Minidump driver capture mask level.\n"
90                  "\t\t  Default is 0x1F.\n"
91                  "\t\t  Can be set to 0x3, 0x7, 0xF, 0x1F, 0x3F, 0x7F");
92
93 int ql4xenablemd = 1;
94 module_param(ql4xenablemd, int, S_IRUGO | S_IWUSR);
95 MODULE_PARM_DESC(ql4xenablemd,
96                  " Set to enable minidump.\n"
97                  "\t\t  0 - disable minidump\n"
98                  "\t\t  1 - enable minidump (Default)");
99
100 static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha);
101 /*
102  * SCSI host template entry points
103  */
104 static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
105
106 /*
107  * iSCSI template entry points
108  */
109 static int qla4xxx_session_get_param(struct iscsi_cls_session *cls_sess,
110                                      enum iscsi_param param, char *buf);
111 static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
112                                   enum iscsi_param param, char *buf);
113 static int qla4xxx_host_get_param(struct Scsi_Host *shost,
114                                   enum iscsi_host_param param, char *buf);
115 static int qla4xxx_iface_set_param(struct Scsi_Host *shost, void *data,
116                                    uint32_t len);
117 static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
118                                    enum iscsi_param_type param_type,
119                                    int param, char *buf);
120 static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
121 static struct iscsi_endpoint *qla4xxx_ep_connect(struct Scsi_Host *shost,
122                                                  struct sockaddr *dst_addr,
123                                                  int non_blocking);
124 static int qla4xxx_ep_poll(struct iscsi_endpoint *ep, int timeout_ms);
125 static void qla4xxx_ep_disconnect(struct iscsi_endpoint *ep);
126 static int qla4xxx_get_ep_param(struct iscsi_endpoint *ep,
127                                 enum iscsi_param param, char *buf);
128 static int qla4xxx_conn_start(struct iscsi_cls_conn *conn);
129 static struct iscsi_cls_conn *
130 qla4xxx_conn_create(struct iscsi_cls_session *cls_sess, uint32_t conn_idx);
131 static int qla4xxx_conn_bind(struct iscsi_cls_session *cls_session,
132                              struct iscsi_cls_conn *cls_conn,
133                              uint64_t transport_fd, int is_leading);
134 static void qla4xxx_conn_destroy(struct iscsi_cls_conn *conn);
135 static struct iscsi_cls_session *
136 qla4xxx_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max,
137                         uint16_t qdepth, uint32_t initial_cmdsn);
138 static void qla4xxx_session_destroy(struct iscsi_cls_session *sess);
139 static void qla4xxx_task_work(struct work_struct *wdata);
140 static int qla4xxx_alloc_pdu(struct iscsi_task *, uint8_t);
141 static int qla4xxx_task_xmit(struct iscsi_task *);
142 static void qla4xxx_task_cleanup(struct iscsi_task *);
143 static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session);
144 static void qla4xxx_conn_get_stats(struct iscsi_cls_conn *cls_conn,
145                                    struct iscsi_stats *stats);
146 static int qla4xxx_send_ping(struct Scsi_Host *shost, uint32_t iface_num,
147                              uint32_t iface_type, uint32_t payload_size,
148                              uint32_t pid, struct sockaddr *dst_addr);
149 static int qla4xxx_get_chap_list(struct Scsi_Host *shost, uint16_t chap_tbl_idx,
150                                  uint32_t *num_entries, char *buf);
151 static int qla4xxx_delete_chap(struct Scsi_Host *shost, uint16_t chap_tbl_idx);
152 static int qla4xxx_set_chap_entry(struct Scsi_Host *shost, void  *data,
153                                   int len);
154
155 /*
156  * SCSI host template entry points
157  */
158 static int qla4xxx_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmd);
159 static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
160 static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
161 static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
162 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
163 static int qla4xxx_slave_alloc(struct scsi_device *device);
164 static int qla4xxx_slave_configure(struct scsi_device *device);
165 static void qla4xxx_slave_destroy(struct scsi_device *sdev);
166 static umode_t qla4_attr_is_visible(int param_type, int param);
167 static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type);
168 static int qla4xxx_change_queue_depth(struct scsi_device *sdev, int qdepth,
169                                       int reason);
170
171 /*
172  * iSCSI Flash DDB sysfs entry points
173  */
174 static int
175 qla4xxx_sysfs_ddb_set_param(struct iscsi_bus_flash_session *fnode_sess,
176                             struct iscsi_bus_flash_conn *fnode_conn,
177                             void *data, int len);
178 static int
179 qla4xxx_sysfs_ddb_get_param(struct iscsi_bus_flash_session *fnode_sess,
180                             int param, char *buf);
181 static int qla4xxx_sysfs_ddb_add(struct Scsi_Host *shost, const char *buf,
182                                  int len);
183 static int
184 qla4xxx_sysfs_ddb_delete(struct iscsi_bus_flash_session *fnode_sess);
185 static int qla4xxx_sysfs_ddb_login(struct iscsi_bus_flash_session *fnode_sess,
186                                    struct iscsi_bus_flash_conn *fnode_conn);
187 static int qla4xxx_sysfs_ddb_logout(struct iscsi_bus_flash_session *fnode_sess,
188                                     struct iscsi_bus_flash_conn *fnode_conn);
189 static int qla4xxx_sysfs_ddb_logout_sid(struct iscsi_cls_session *cls_sess);
190
191 static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
192     QLA82XX_LEGACY_INTR_CONFIG;
193
194 static struct scsi_host_template qla4xxx_driver_template = {
195         .module                 = THIS_MODULE,
196         .name                   = DRIVER_NAME,
197         .proc_name              = DRIVER_NAME,
198         .queuecommand           = qla4xxx_queuecommand,
199
200         .eh_abort_handler       = qla4xxx_eh_abort,
201         .eh_device_reset_handler = qla4xxx_eh_device_reset,
202         .eh_target_reset_handler = qla4xxx_eh_target_reset,
203         .eh_host_reset_handler  = qla4xxx_eh_host_reset,
204         .eh_timed_out           = qla4xxx_eh_cmd_timed_out,
205
206         .slave_configure        = qla4xxx_slave_configure,
207         .slave_alloc            = qla4xxx_slave_alloc,
208         .slave_destroy          = qla4xxx_slave_destroy,
209         .change_queue_depth     = qla4xxx_change_queue_depth,
210
211         .this_id                = -1,
212         .cmd_per_lun            = 3,
213         .use_clustering         = ENABLE_CLUSTERING,
214         .sg_tablesize           = SG_ALL,
215
216         .max_sectors            = 0xFFFF,
217         .shost_attrs            = qla4xxx_host_attrs,
218         .host_reset             = qla4xxx_host_reset,
219         .vendor_id              = SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_QLOGIC,
220 };
221
222 static struct iscsi_transport qla4xxx_iscsi_transport = {
223         .owner                  = THIS_MODULE,
224         .name                   = DRIVER_NAME,
225         .caps                   = CAP_TEXT_NEGO |
226                                   CAP_DATA_PATH_OFFLOAD | CAP_HDRDGST |
227                                   CAP_DATADGST | CAP_LOGIN_OFFLOAD |
228                                   CAP_MULTI_R2T,
229         .attr_is_visible        = qla4_attr_is_visible,
230         .create_session         = qla4xxx_session_create,
231         .destroy_session        = qla4xxx_session_destroy,
232         .start_conn             = qla4xxx_conn_start,
233         .create_conn            = qla4xxx_conn_create,
234         .bind_conn              = qla4xxx_conn_bind,
235         .stop_conn              = iscsi_conn_stop,
236         .destroy_conn           = qla4xxx_conn_destroy,
237         .set_param              = iscsi_set_param,
238         .get_conn_param         = qla4xxx_conn_get_param,
239         .get_session_param      = qla4xxx_session_get_param,
240         .get_ep_param           = qla4xxx_get_ep_param,
241         .ep_connect             = qla4xxx_ep_connect,
242         .ep_poll                = qla4xxx_ep_poll,
243         .ep_disconnect          = qla4xxx_ep_disconnect,
244         .get_stats              = qla4xxx_conn_get_stats,
245         .send_pdu               = iscsi_conn_send_pdu,
246         .xmit_task              = qla4xxx_task_xmit,
247         .cleanup_task           = qla4xxx_task_cleanup,
248         .alloc_pdu              = qla4xxx_alloc_pdu,
249
250         .get_host_param         = qla4xxx_host_get_param,
251         .set_iface_param        = qla4xxx_iface_set_param,
252         .get_iface_param        = qla4xxx_get_iface_param,
253         .bsg_request            = qla4xxx_bsg_request,
254         .send_ping              = qla4xxx_send_ping,
255         .get_chap               = qla4xxx_get_chap_list,
256         .delete_chap            = qla4xxx_delete_chap,
257         .set_chap               = qla4xxx_set_chap_entry,
258         .get_flashnode_param    = qla4xxx_sysfs_ddb_get_param,
259         .set_flashnode_param    = qla4xxx_sysfs_ddb_set_param,
260         .new_flashnode          = qla4xxx_sysfs_ddb_add,
261         .del_flashnode          = qla4xxx_sysfs_ddb_delete,
262         .login_flashnode        = qla4xxx_sysfs_ddb_login,
263         .logout_flashnode       = qla4xxx_sysfs_ddb_logout,
264         .logout_flashnode_sid   = qla4xxx_sysfs_ddb_logout_sid,
265 };
266
267 static struct scsi_transport_template *qla4xxx_scsi_transport;
268
269 static int qla4xxx_send_ping(struct Scsi_Host *shost, uint32_t iface_num,
270                              uint32_t iface_type, uint32_t payload_size,
271                              uint32_t pid, struct sockaddr *dst_addr)
272 {
273         struct scsi_qla_host *ha = to_qla_host(shost);
274         struct sockaddr_in *addr;
275         struct sockaddr_in6 *addr6;
276         uint32_t options = 0;
277         uint8_t ipaddr[IPv6_ADDR_LEN];
278         int rval;
279
280         memset(ipaddr, 0, IPv6_ADDR_LEN);
281         /* IPv4 to IPv4 */
282         if ((iface_type == ISCSI_IFACE_TYPE_IPV4) &&
283             (dst_addr->sa_family == AF_INET)) {
284                 addr = (struct sockaddr_in *)dst_addr;
285                 memcpy(ipaddr, &addr->sin_addr.s_addr, IP_ADDR_LEN);
286                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: IPv4 Ping src: %pI4 "
287                                   "dest: %pI4\n", __func__,
288                                   &ha->ip_config.ip_address, ipaddr));
289                 rval = qla4xxx_ping_iocb(ha, options, payload_size, pid,
290                                          ipaddr);
291                 if (rval)
292                         rval = -EINVAL;
293         } else if ((iface_type == ISCSI_IFACE_TYPE_IPV6) &&
294                    (dst_addr->sa_family == AF_INET6)) {
295                 /* IPv6 to IPv6 */
296                 addr6 = (struct sockaddr_in6 *)dst_addr;
297                 memcpy(ipaddr, &addr6->sin6_addr.in6_u.u6_addr8, IPv6_ADDR_LEN);
298
299                 options |= PING_IPV6_PROTOCOL_ENABLE;
300
301                 /* Ping using LinkLocal address */
302                 if ((iface_num == 0) || (iface_num == 1)) {
303                         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: LinkLocal Ping "
304                                           "src: %pI6 dest: %pI6\n", __func__,
305                                           &ha->ip_config.ipv6_link_local_addr,
306                                           ipaddr));
307                         options |= PING_IPV6_LINKLOCAL_ADDR;
308                         rval = qla4xxx_ping_iocb(ha, options, payload_size,
309                                                  pid, ipaddr);
310                 } else {
311                         ql4_printk(KERN_WARNING, ha, "%s: iface num = %d "
312                                    "not supported\n", __func__, iface_num);
313                         rval = -ENOSYS;
314                         goto exit_send_ping;
315                 }
316
317                 /*
318                  * If ping using LinkLocal address fails, try ping using
319                  * IPv6 address
320                  */
321                 if (rval != QLA_SUCCESS) {
322                         options &= ~PING_IPV6_LINKLOCAL_ADDR;
323                         if (iface_num == 0) {
324                                 options |= PING_IPV6_ADDR0;
325                                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: IPv6 "
326                                                   "Ping src: %pI6 "
327                                                   "dest: %pI6\n", __func__,
328                                                   &ha->ip_config.ipv6_addr0,
329                                                   ipaddr));
330                         } else if (iface_num == 1) {
331                                 options |= PING_IPV6_ADDR1;
332                                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: IPv6 "
333                                                   "Ping src: %pI6 "
334                                                   "dest: %pI6\n", __func__,
335                                                   &ha->ip_config.ipv6_addr1,
336                                                   ipaddr));
337                         }
338                         rval = qla4xxx_ping_iocb(ha, options, payload_size,
339                                                  pid, ipaddr);
340                         if (rval)
341                                 rval = -EINVAL;
342                 }
343         } else
344                 rval = -ENOSYS;
345 exit_send_ping:
346         return rval;
347 }
348
349 static umode_t qla4_attr_is_visible(int param_type, int param)
350 {
351         switch (param_type) {
352         case ISCSI_HOST_PARAM:
353                 switch (param) {
354                 case ISCSI_HOST_PARAM_HWADDRESS:
355                 case ISCSI_HOST_PARAM_IPADDRESS:
356                 case ISCSI_HOST_PARAM_INITIATOR_NAME:
357                 case ISCSI_HOST_PARAM_PORT_STATE:
358                 case ISCSI_HOST_PARAM_PORT_SPEED:
359                         return S_IRUGO;
360                 default:
361                         return 0;
362                 }
363         case ISCSI_PARAM:
364                 switch (param) {
365                 case ISCSI_PARAM_PERSISTENT_ADDRESS:
366                 case ISCSI_PARAM_PERSISTENT_PORT:
367                 case ISCSI_PARAM_CONN_ADDRESS:
368                 case ISCSI_PARAM_CONN_PORT:
369                 case ISCSI_PARAM_TARGET_NAME:
370                 case ISCSI_PARAM_TPGT:
371                 case ISCSI_PARAM_TARGET_ALIAS:
372                 case ISCSI_PARAM_MAX_BURST:
373                 case ISCSI_PARAM_MAX_R2T:
374                 case ISCSI_PARAM_FIRST_BURST:
375                 case ISCSI_PARAM_MAX_RECV_DLENGTH:
376                 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
377                 case ISCSI_PARAM_IFACE_NAME:
378                 case ISCSI_PARAM_CHAP_OUT_IDX:
379                 case ISCSI_PARAM_CHAP_IN_IDX:
380                 case ISCSI_PARAM_USERNAME:
381                 case ISCSI_PARAM_PASSWORD:
382                 case ISCSI_PARAM_USERNAME_IN:
383                 case ISCSI_PARAM_PASSWORD_IN:
384                 case ISCSI_PARAM_AUTO_SND_TGT_DISABLE:
385                 case ISCSI_PARAM_DISCOVERY_SESS:
386                 case ISCSI_PARAM_PORTAL_TYPE:
387                 case ISCSI_PARAM_CHAP_AUTH_EN:
388                 case ISCSI_PARAM_DISCOVERY_LOGOUT_EN:
389                 case ISCSI_PARAM_BIDI_CHAP_EN:
390                 case ISCSI_PARAM_DISCOVERY_AUTH_OPTIONAL:
391                 case ISCSI_PARAM_DEF_TIME2WAIT:
392                 case ISCSI_PARAM_DEF_TIME2RETAIN:
393                 case ISCSI_PARAM_HDRDGST_EN:
394                 case ISCSI_PARAM_DATADGST_EN:
395                 case ISCSI_PARAM_INITIAL_R2T_EN:
396                 case ISCSI_PARAM_IMM_DATA_EN:
397                 case ISCSI_PARAM_PDU_INORDER_EN:
398                 case ISCSI_PARAM_DATASEQ_INORDER_EN:
399                 case ISCSI_PARAM_MAX_SEGMENT_SIZE:
400                 case ISCSI_PARAM_TCP_TIMESTAMP_STAT:
401                 case ISCSI_PARAM_TCP_WSF_DISABLE:
402                 case ISCSI_PARAM_TCP_NAGLE_DISABLE:
403                 case ISCSI_PARAM_TCP_TIMER_SCALE:
404                 case ISCSI_PARAM_TCP_TIMESTAMP_EN:
405                 case ISCSI_PARAM_TCP_XMIT_WSF:
406                 case ISCSI_PARAM_TCP_RECV_WSF:
407                 case ISCSI_PARAM_IP_FRAGMENT_DISABLE:
408                 case ISCSI_PARAM_IPV4_TOS:
409                 case ISCSI_PARAM_IPV6_TC:
410                 case ISCSI_PARAM_IPV6_FLOW_LABEL:
411                 case ISCSI_PARAM_IS_FW_ASSIGNED_IPV6:
412                 case ISCSI_PARAM_KEEPALIVE_TMO:
413                 case ISCSI_PARAM_LOCAL_PORT:
414                 case ISCSI_PARAM_ISID:
415                 case ISCSI_PARAM_TSID:
416                 case ISCSI_PARAM_DEF_TASKMGMT_TMO:
417                 case ISCSI_PARAM_ERL:
418                 case ISCSI_PARAM_STATSN:
419                 case ISCSI_PARAM_EXP_STATSN:
420                 case ISCSI_PARAM_DISCOVERY_PARENT_IDX:
421                 case ISCSI_PARAM_DISCOVERY_PARENT_TYPE:
422                         return S_IRUGO;
423                 default:
424                         return 0;
425                 }
426         case ISCSI_NET_PARAM:
427                 switch (param) {
428                 case ISCSI_NET_PARAM_IPV4_ADDR:
429                 case ISCSI_NET_PARAM_IPV4_SUBNET:
430                 case ISCSI_NET_PARAM_IPV4_GW:
431                 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
432                 case ISCSI_NET_PARAM_IFACE_ENABLE:
433                 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
434                 case ISCSI_NET_PARAM_IPV6_ADDR:
435                 case ISCSI_NET_PARAM_IPV6_ROUTER:
436                 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
437                 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
438                 case ISCSI_NET_PARAM_VLAN_ID:
439                 case ISCSI_NET_PARAM_VLAN_PRIORITY:
440                 case ISCSI_NET_PARAM_VLAN_ENABLED:
441                 case ISCSI_NET_PARAM_MTU:
442                 case ISCSI_NET_PARAM_PORT:
443                         return S_IRUGO;
444                 default:
445                         return 0;
446                 }
447         case ISCSI_FLASHNODE_PARAM:
448                 switch (param) {
449                 case ISCSI_FLASHNODE_IS_FW_ASSIGNED_IPV6:
450                 case ISCSI_FLASHNODE_PORTAL_TYPE:
451                 case ISCSI_FLASHNODE_AUTO_SND_TGT_DISABLE:
452                 case ISCSI_FLASHNODE_DISCOVERY_SESS:
453                 case ISCSI_FLASHNODE_ENTRY_EN:
454                 case ISCSI_FLASHNODE_HDR_DGST_EN:
455                 case ISCSI_FLASHNODE_DATA_DGST_EN:
456                 case ISCSI_FLASHNODE_IMM_DATA_EN:
457                 case ISCSI_FLASHNODE_INITIAL_R2T_EN:
458                 case ISCSI_FLASHNODE_DATASEQ_INORDER:
459                 case ISCSI_FLASHNODE_PDU_INORDER:
460                 case ISCSI_FLASHNODE_CHAP_AUTH_EN:
461                 case ISCSI_FLASHNODE_SNACK_REQ_EN:
462                 case ISCSI_FLASHNODE_DISCOVERY_LOGOUT_EN:
463                 case ISCSI_FLASHNODE_BIDI_CHAP_EN:
464                 case ISCSI_FLASHNODE_DISCOVERY_AUTH_OPTIONAL:
465                 case ISCSI_FLASHNODE_ERL:
466                 case ISCSI_FLASHNODE_TCP_TIMESTAMP_STAT:
467                 case ISCSI_FLASHNODE_TCP_NAGLE_DISABLE:
468                 case ISCSI_FLASHNODE_TCP_WSF_DISABLE:
469                 case ISCSI_FLASHNODE_TCP_TIMER_SCALE:
470                 case ISCSI_FLASHNODE_TCP_TIMESTAMP_EN:
471                 case ISCSI_FLASHNODE_IP_FRAG_DISABLE:
472                 case ISCSI_FLASHNODE_MAX_RECV_DLENGTH:
473                 case ISCSI_FLASHNODE_MAX_XMIT_DLENGTH:
474                 case ISCSI_FLASHNODE_FIRST_BURST:
475                 case ISCSI_FLASHNODE_DEF_TIME2WAIT:
476                 case ISCSI_FLASHNODE_DEF_TIME2RETAIN:
477                 case ISCSI_FLASHNODE_MAX_R2T:
478                 case ISCSI_FLASHNODE_KEEPALIVE_TMO:
479                 case ISCSI_FLASHNODE_ISID:
480                 case ISCSI_FLASHNODE_TSID:
481                 case ISCSI_FLASHNODE_PORT:
482                 case ISCSI_FLASHNODE_MAX_BURST:
483                 case ISCSI_FLASHNODE_DEF_TASKMGMT_TMO:
484                 case ISCSI_FLASHNODE_IPADDR:
485                 case ISCSI_FLASHNODE_ALIAS:
486                 case ISCSI_FLASHNODE_REDIRECT_IPADDR:
487                 case ISCSI_FLASHNODE_MAX_SEGMENT_SIZE:
488                 case ISCSI_FLASHNODE_LOCAL_PORT:
489                 case ISCSI_FLASHNODE_IPV4_TOS:
490                 case ISCSI_FLASHNODE_IPV6_TC:
491                 case ISCSI_FLASHNODE_IPV6_FLOW_LABEL:
492                 case ISCSI_FLASHNODE_NAME:
493                 case ISCSI_FLASHNODE_TPGT:
494                 case ISCSI_FLASHNODE_LINK_LOCAL_IPV6:
495                 case ISCSI_FLASHNODE_DISCOVERY_PARENT_IDX:
496                 case ISCSI_FLASHNODE_DISCOVERY_PARENT_TYPE:
497                 case ISCSI_FLASHNODE_TCP_XMIT_WSF:
498                 case ISCSI_FLASHNODE_TCP_RECV_WSF:
499                 case ISCSI_FLASHNODE_CHAP_OUT_IDX:
500                 case ISCSI_FLASHNODE_USERNAME:
501                 case ISCSI_FLASHNODE_PASSWORD:
502                 case ISCSI_FLASHNODE_STATSN:
503                 case ISCSI_FLASHNODE_EXP_STATSN:
504                 case ISCSI_FLASHNODE_IS_BOOT_TGT:
505                         return S_IRUGO;
506                 default:
507                         return 0;
508                 }
509         }
510
511         return 0;
512 }
513
514 static int qla4xxx_get_chap_by_index(struct scsi_qla_host *ha,
515                                      int16_t chap_index,
516                                      struct ql4_chap_table **chap_entry)
517 {
518         int rval = QLA_ERROR;
519         int max_chap_entries;
520
521         if (!ha->chap_list) {
522                 ql4_printk(KERN_ERR, ha, "CHAP table cache is empty!\n");
523                 rval = QLA_ERROR;
524                 goto exit_get_chap;
525         }
526
527         if (is_qla80XX(ha))
528                 max_chap_entries = (ha->hw.flt_chap_size / 2) /
529                                    sizeof(struct ql4_chap_table);
530         else
531                 max_chap_entries = MAX_CHAP_ENTRIES_40XX;
532
533         if (chap_index > max_chap_entries) {
534                 ql4_printk(KERN_ERR, ha, "Invalid Chap index\n");
535                 rval = QLA_ERROR;
536                 goto exit_get_chap;
537         }
538
539         *chap_entry = (struct ql4_chap_table *)ha->chap_list + chap_index;
540         if ((*chap_entry)->cookie !=
541              __constant_cpu_to_le16(CHAP_VALID_COOKIE)) {
542                 rval = QLA_ERROR;
543                 *chap_entry = NULL;
544         } else {
545                 rval = QLA_SUCCESS;
546         }
547
548 exit_get_chap:
549         return rval;
550 }
551
552 /**
553  * qla4xxx_find_free_chap_index - Find the first free chap index
554  * @ha: pointer to adapter structure
555  * @chap_index: CHAP index to be returned
556  *
557  * Find the first free chap index available in the chap table
558  *
559  * Note: Caller should acquire the chap lock before getting here.
560  **/
561 static int qla4xxx_find_free_chap_index(struct scsi_qla_host *ha,
562                                         uint16_t *chap_index)
563 {
564         int i, rval;
565         int free_index = -1;
566         int max_chap_entries = 0;
567         struct ql4_chap_table *chap_table;
568
569         if (is_qla80XX(ha))
570                 max_chap_entries = (ha->hw.flt_chap_size / 2) /
571                                                 sizeof(struct ql4_chap_table);
572         else
573                 max_chap_entries = MAX_CHAP_ENTRIES_40XX;
574
575         if (!ha->chap_list) {
576                 ql4_printk(KERN_ERR, ha, "CHAP table cache is empty!\n");
577                 rval = QLA_ERROR;
578                 goto exit_find_chap;
579         }
580
581         for (i = 0; i < max_chap_entries; i++) {
582                 chap_table = (struct ql4_chap_table *)ha->chap_list + i;
583
584                 if ((chap_table->cookie !=
585                     __constant_cpu_to_le16(CHAP_VALID_COOKIE)) &&
586                    (i > MAX_RESRV_CHAP_IDX)) {
587                                 free_index = i;
588                                 break;
589                 }
590         }
591
592         if (free_index != -1) {
593                 *chap_index = free_index;
594                 rval = QLA_SUCCESS;
595         } else {
596                 rval = QLA_ERROR;
597         }
598
599 exit_find_chap:
600         return rval;
601 }
602
603 static int qla4xxx_get_chap_list(struct Scsi_Host *shost, uint16_t chap_tbl_idx,
604                                   uint32_t *num_entries, char *buf)
605 {
606         struct scsi_qla_host *ha = to_qla_host(shost);
607         struct ql4_chap_table *chap_table;
608         struct iscsi_chap_rec *chap_rec;
609         int max_chap_entries = 0;
610         int valid_chap_entries = 0;
611         int ret = 0, i;
612
613         if (is_qla80XX(ha))
614                 max_chap_entries = (ha->hw.flt_chap_size / 2) /
615                                         sizeof(struct ql4_chap_table);
616         else
617                 max_chap_entries = MAX_CHAP_ENTRIES_40XX;
618
619         ql4_printk(KERN_INFO, ha, "%s: num_entries = %d, CHAP idx = %d\n",
620                         __func__, *num_entries, chap_tbl_idx);
621
622         if (!buf) {
623                 ret = -ENOMEM;
624                 goto exit_get_chap_list;
625         }
626
627         chap_rec = (struct iscsi_chap_rec *) buf;
628         mutex_lock(&ha->chap_sem);
629         for (i = chap_tbl_idx; i < max_chap_entries; i++) {
630                 chap_table = (struct ql4_chap_table *)ha->chap_list + i;
631                 if (chap_table->cookie !=
632                     __constant_cpu_to_le16(CHAP_VALID_COOKIE))
633                         continue;
634
635                 chap_rec->chap_tbl_idx = i;
636                 strncpy(chap_rec->username, chap_table->name,
637                         ISCSI_CHAP_AUTH_NAME_MAX_LEN);
638                 strncpy(chap_rec->password, chap_table->secret,
639                         QL4_CHAP_MAX_SECRET_LEN);
640                 chap_rec->password_length = chap_table->secret_len;
641
642                 if (chap_table->flags & BIT_7) /* local */
643                         chap_rec->chap_type = CHAP_TYPE_OUT;
644
645                 if (chap_table->flags & BIT_6) /* peer */
646                         chap_rec->chap_type = CHAP_TYPE_IN;
647
648                 chap_rec++;
649
650                 valid_chap_entries++;
651                 if (valid_chap_entries == *num_entries)
652                         break;
653                 else
654                         continue;
655         }
656         mutex_unlock(&ha->chap_sem);
657
658 exit_get_chap_list:
659         ql4_printk(KERN_INFO, ha, "%s: Valid CHAP Entries = %d\n",
660                         __func__,  valid_chap_entries);
661         *num_entries = valid_chap_entries;
662         return ret;
663 }
664
665 static int __qla4xxx_is_chap_active(struct device *dev, void *data)
666 {
667         int ret = 0;
668         uint16_t *chap_tbl_idx = (uint16_t *) data;
669         struct iscsi_cls_session *cls_session;
670         struct iscsi_session *sess;
671         struct ddb_entry *ddb_entry;
672
673         if (!iscsi_is_session_dev(dev))
674                 goto exit_is_chap_active;
675
676         cls_session = iscsi_dev_to_session(dev);
677         sess = cls_session->dd_data;
678         ddb_entry = sess->dd_data;
679
680         if (iscsi_session_chkready(cls_session))
681                 goto exit_is_chap_active;
682
683         if (ddb_entry->chap_tbl_idx == *chap_tbl_idx)
684                 ret = 1;
685
686 exit_is_chap_active:
687         return ret;
688 }
689
690 static int qla4xxx_is_chap_active(struct Scsi_Host *shost,
691                                   uint16_t chap_tbl_idx)
692 {
693         int ret = 0;
694
695         ret = device_for_each_child(&shost->shost_gendev, &chap_tbl_idx,
696                                     __qla4xxx_is_chap_active);
697
698         return ret;
699 }
700
701 static int qla4xxx_delete_chap(struct Scsi_Host *shost, uint16_t chap_tbl_idx)
702 {
703         struct scsi_qla_host *ha = to_qla_host(shost);
704         struct ql4_chap_table *chap_table;
705         dma_addr_t chap_dma;
706         int max_chap_entries = 0;
707         uint32_t offset = 0;
708         uint32_t chap_size;
709         int ret = 0;
710
711         chap_table = dma_pool_alloc(ha->chap_dma_pool, GFP_KERNEL, &chap_dma);
712         if (chap_table == NULL)
713                 return -ENOMEM;
714
715         memset(chap_table, 0, sizeof(struct ql4_chap_table));
716
717         if (is_qla80XX(ha))
718                 max_chap_entries = (ha->hw.flt_chap_size / 2) /
719                                    sizeof(struct ql4_chap_table);
720         else
721                 max_chap_entries = MAX_CHAP_ENTRIES_40XX;
722
723         if (chap_tbl_idx > max_chap_entries) {
724                 ret = -EINVAL;
725                 goto exit_delete_chap;
726         }
727
728         /* Check if chap index is in use.
729          * If chap is in use don't delet chap entry */
730         ret = qla4xxx_is_chap_active(shost, chap_tbl_idx);
731         if (ret) {
732                 ql4_printk(KERN_INFO, ha, "CHAP entry %d is in use, cannot "
733                            "delete from flash\n", chap_tbl_idx);
734                 ret = -EBUSY;
735                 goto exit_delete_chap;
736         }
737
738         chap_size = sizeof(struct ql4_chap_table);
739         if (is_qla40XX(ha))
740                 offset = FLASH_CHAP_OFFSET | (chap_tbl_idx * chap_size);
741         else {
742                 offset = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_region_chap << 2);
743                 /* flt_chap_size is CHAP table size for both ports
744                  * so divide it by 2 to calculate the offset for second port
745                  */
746                 if (ha->port_num == 1)
747                         offset += (ha->hw.flt_chap_size / 2);
748                 offset += (chap_tbl_idx * chap_size);
749         }
750
751         ret = qla4xxx_get_flash(ha, chap_dma, offset, chap_size);
752         if (ret != QLA_SUCCESS) {
753                 ret = -EINVAL;
754                 goto exit_delete_chap;
755         }
756
757         DEBUG2(ql4_printk(KERN_INFO, ha, "Chap Cookie: x%x\n",
758                           __le16_to_cpu(chap_table->cookie)));
759
760         if (__le16_to_cpu(chap_table->cookie) != CHAP_VALID_COOKIE) {
761                 ql4_printk(KERN_ERR, ha, "No valid chap entry found\n");
762                 goto exit_delete_chap;
763         }
764
765         chap_table->cookie = __constant_cpu_to_le16(0xFFFF);
766
767         offset = FLASH_CHAP_OFFSET |
768                         (chap_tbl_idx * sizeof(struct ql4_chap_table));
769         ret = qla4xxx_set_flash(ha, chap_dma, offset, chap_size,
770                                 FLASH_OPT_RMW_COMMIT);
771         if (ret == QLA_SUCCESS && ha->chap_list) {
772                 mutex_lock(&ha->chap_sem);
773                 /* Update ha chap_list cache */
774                 memcpy((struct ql4_chap_table *)ha->chap_list + chap_tbl_idx,
775                         chap_table, sizeof(struct ql4_chap_table));
776                 mutex_unlock(&ha->chap_sem);
777         }
778         if (ret != QLA_SUCCESS)
779                 ret =  -EINVAL;
780
781 exit_delete_chap:
782         dma_pool_free(ha->chap_dma_pool, chap_table, chap_dma);
783         return ret;
784 }
785
786 /**
787  * qla4xxx_set_chap_entry - Make chap entry with given information
788  * @shost: pointer to host
789  * @data: chap info - credentials, index and type to make chap entry
790  * @len: length of data
791  *
792  * Add or update chap entry with the given information
793  **/
794 static int qla4xxx_set_chap_entry(struct Scsi_Host *shost, void *data, int len)
795 {
796         struct scsi_qla_host *ha = to_qla_host(shost);
797         struct iscsi_chap_rec chap_rec;
798         struct ql4_chap_table *chap_entry = NULL;
799         struct iscsi_param_info *param_info;
800         struct nlattr *attr;
801         int max_chap_entries = 0;
802         int type;
803         int rem = len;
804         int rc = 0;
805         int size;
806
807         memset(&chap_rec, 0, sizeof(chap_rec));
808
809         nla_for_each_attr(attr, data, len, rem) {
810                 param_info = nla_data(attr);
811
812                 switch (param_info->param) {
813                 case ISCSI_CHAP_PARAM_INDEX:
814                         chap_rec.chap_tbl_idx = *(uint16_t *)param_info->value;
815                         break;
816                 case ISCSI_CHAP_PARAM_CHAP_TYPE:
817                         chap_rec.chap_type = param_info->value[0];
818                         break;
819                 case ISCSI_CHAP_PARAM_USERNAME:
820                         size = min_t(size_t, sizeof(chap_rec.username),
821                                      param_info->len);
822                         memcpy(chap_rec.username, param_info->value, size);
823                         break;
824                 case ISCSI_CHAP_PARAM_PASSWORD:
825                         size = min_t(size_t, sizeof(chap_rec.password),
826                                      param_info->len);
827                         memcpy(chap_rec.password, param_info->value, size);
828                         break;
829                 case ISCSI_CHAP_PARAM_PASSWORD_LEN:
830                         chap_rec.password_length = param_info->value[0];
831                         break;
832                 default:
833                         ql4_printk(KERN_ERR, ha,
834                                    "%s: No such sysfs attribute\n", __func__);
835                         rc = -ENOSYS;
836                         goto exit_set_chap;
837                 };
838         }
839
840         if (chap_rec.chap_type == CHAP_TYPE_IN)
841                 type = BIDI_CHAP;
842         else
843                 type = LOCAL_CHAP;
844
845         if (is_qla80XX(ha))
846                 max_chap_entries = (ha->hw.flt_chap_size / 2) /
847                                    sizeof(struct ql4_chap_table);
848         else
849                 max_chap_entries = MAX_CHAP_ENTRIES_40XX;
850
851         mutex_lock(&ha->chap_sem);
852         if (chap_rec.chap_tbl_idx < max_chap_entries) {
853                 rc = qla4xxx_get_chap_by_index(ha, chap_rec.chap_tbl_idx,
854                                                &chap_entry);
855                 if (!rc) {
856                         if (!(type == qla4xxx_get_chap_type(chap_entry))) {
857                                 ql4_printk(KERN_INFO, ha,
858                                            "Type mismatch for CHAP entry %d\n",
859                                            chap_rec.chap_tbl_idx);
860                                 rc = -EINVAL;
861                                 goto exit_unlock_chap;
862                         }
863
864                         /* If chap index is in use then don't modify it */
865                         rc = qla4xxx_is_chap_active(shost,
866                                                     chap_rec.chap_tbl_idx);
867                         if (rc) {
868                                 ql4_printk(KERN_INFO, ha,
869                                            "CHAP entry %d is in use\n",
870                                            chap_rec.chap_tbl_idx);
871                                 rc = -EBUSY;
872                                 goto exit_unlock_chap;
873                         }
874                 }
875         } else {
876                 rc = qla4xxx_find_free_chap_index(ha, &chap_rec.chap_tbl_idx);
877                 if (rc) {
878                         ql4_printk(KERN_INFO, ha, "CHAP entry not available\n");
879                         rc = -EBUSY;
880                         goto exit_unlock_chap;
881                 }
882         }
883
884         rc = qla4xxx_set_chap(ha, chap_rec.username, chap_rec.password,
885                               chap_rec.chap_tbl_idx, type);
886
887 exit_unlock_chap:
888         mutex_unlock(&ha->chap_sem);
889
890 exit_set_chap:
891         return rc;
892 }
893
894 static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
895                                    enum iscsi_param_type param_type,
896                                    int param, char *buf)
897 {
898         struct Scsi_Host *shost = iscsi_iface_to_shost(iface);
899         struct scsi_qla_host *ha = to_qla_host(shost);
900         int len = -ENOSYS;
901
902         if (param_type != ISCSI_NET_PARAM)
903                 return -ENOSYS;
904
905         switch (param) {
906         case ISCSI_NET_PARAM_IPV4_ADDR:
907                 len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
908                 break;
909         case ISCSI_NET_PARAM_IPV4_SUBNET:
910                 len = sprintf(buf, "%pI4\n", &ha->ip_config.subnet_mask);
911                 break;
912         case ISCSI_NET_PARAM_IPV4_GW:
913                 len = sprintf(buf, "%pI4\n", &ha->ip_config.gateway);
914                 break;
915         case ISCSI_NET_PARAM_IFACE_ENABLE:
916                 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
917                         len = sprintf(buf, "%s\n",
918                                       (ha->ip_config.ipv4_options &
919                                        IPOPT_IPV4_PROTOCOL_ENABLE) ?
920                                       "enabled" : "disabled");
921                 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
922                         len = sprintf(buf, "%s\n",
923                                       (ha->ip_config.ipv6_options &
924                                        IPV6_OPT_IPV6_PROTOCOL_ENABLE) ?
925                                        "enabled" : "disabled");
926                 break;
927         case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
928                 len = sprintf(buf, "%s\n",
929                               (ha->ip_config.tcp_options & TCPOPT_DHCP_ENABLE) ?
930                               "dhcp" : "static");
931                 break;
932         case ISCSI_NET_PARAM_IPV6_ADDR:
933                 if (iface->iface_num == 0)
934                         len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr0);
935                 if (iface->iface_num == 1)
936                         len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr1);
937                 break;
938         case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
939                 len = sprintf(buf, "%pI6\n",
940                               &ha->ip_config.ipv6_link_local_addr);
941                 break;
942         case ISCSI_NET_PARAM_IPV6_ROUTER:
943                 len = sprintf(buf, "%pI6\n",
944                               &ha->ip_config.ipv6_default_router_addr);
945                 break;
946         case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
947                 len = sprintf(buf, "%s\n",
948                               (ha->ip_config.ipv6_addl_options &
949                                IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) ?
950                                "nd" : "static");
951                 break;
952         case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
953                 len = sprintf(buf, "%s\n",
954                               (ha->ip_config.ipv6_addl_options &
955                                IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR) ?
956                                "auto" : "static");
957                 break;
958         case ISCSI_NET_PARAM_VLAN_ID:
959                 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
960                         len = sprintf(buf, "%d\n",
961                                       (ha->ip_config.ipv4_vlan_tag &
962                                        ISCSI_MAX_VLAN_ID));
963                 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
964                         len = sprintf(buf, "%d\n",
965                                       (ha->ip_config.ipv6_vlan_tag &
966                                        ISCSI_MAX_VLAN_ID));
967                 break;
968         case ISCSI_NET_PARAM_VLAN_PRIORITY:
969                 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
970                         len = sprintf(buf, "%d\n",
971                                       ((ha->ip_config.ipv4_vlan_tag >> 13) &
972                                         ISCSI_MAX_VLAN_PRIORITY));
973                 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
974                         len = sprintf(buf, "%d\n",
975                                       ((ha->ip_config.ipv6_vlan_tag >> 13) &
976                                         ISCSI_MAX_VLAN_PRIORITY));
977                 break;
978         case ISCSI_NET_PARAM_VLAN_ENABLED:
979                 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
980                         len = sprintf(buf, "%s\n",
981                                       (ha->ip_config.ipv4_options &
982                                        IPOPT_VLAN_TAGGING_ENABLE) ?
983                                        "enabled" : "disabled");
984                 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
985                         len = sprintf(buf, "%s\n",
986                                       (ha->ip_config.ipv6_options &
987                                        IPV6_OPT_VLAN_TAGGING_ENABLE) ?
988                                        "enabled" : "disabled");
989                 break;
990         case ISCSI_NET_PARAM_MTU:
991                 len = sprintf(buf, "%d\n", ha->ip_config.eth_mtu_size);
992                 break;
993         case ISCSI_NET_PARAM_PORT:
994                 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
995                         len = sprintf(buf, "%d\n", ha->ip_config.ipv4_port);
996                 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
997                         len = sprintf(buf, "%d\n", ha->ip_config.ipv6_port);
998                 break;
999         default:
1000                 len = -ENOSYS;
1001         }
1002
1003         return len;
1004 }
1005
1006 static struct iscsi_endpoint *
1007 qla4xxx_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
1008                    int non_blocking)
1009 {
1010         int ret;
1011         struct iscsi_endpoint *ep;
1012         struct qla_endpoint *qla_ep;
1013         struct scsi_qla_host *ha;
1014         struct sockaddr_in *addr;
1015         struct sockaddr_in6 *addr6;
1016
1017         DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1018         if (!shost) {
1019                 ret = -ENXIO;
1020                 printk(KERN_ERR "%s: shost is NULL\n",
1021                        __func__);
1022                 return ERR_PTR(ret);
1023         }
1024
1025         ha = iscsi_host_priv(shost);
1026
1027         ep = iscsi_create_endpoint(sizeof(struct qla_endpoint));
1028         if (!ep) {
1029                 ret = -ENOMEM;
1030                 return ERR_PTR(ret);
1031         }
1032
1033         qla_ep = ep->dd_data;
1034         memset(qla_ep, 0, sizeof(struct qla_endpoint));
1035         if (dst_addr->sa_family == AF_INET) {
1036                 memcpy(&qla_ep->dst_addr, dst_addr, sizeof(struct sockaddr_in));
1037                 addr = (struct sockaddr_in *)&qla_ep->dst_addr;
1038                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI4\n", __func__,
1039                                   (char *)&addr->sin_addr));
1040         } else if (dst_addr->sa_family == AF_INET6) {
1041                 memcpy(&qla_ep->dst_addr, dst_addr,
1042                        sizeof(struct sockaddr_in6));
1043                 addr6 = (struct sockaddr_in6 *)&qla_ep->dst_addr;
1044                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI6\n", __func__,
1045                                   (char *)&addr6->sin6_addr));
1046         }
1047
1048         qla_ep->host = shost;
1049
1050         return ep;
1051 }
1052
1053 static int qla4xxx_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
1054 {
1055         struct qla_endpoint *qla_ep;
1056         struct scsi_qla_host *ha;
1057         int ret = 0;
1058
1059         DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1060         qla_ep = ep->dd_data;
1061         ha = to_qla_host(qla_ep->host);
1062
1063         if (adapter_up(ha) && !test_bit(AF_BUILD_DDB_LIST, &ha->flags))
1064                 ret = 1;
1065
1066         return ret;
1067 }
1068
1069 static void qla4xxx_ep_disconnect(struct iscsi_endpoint *ep)
1070 {
1071         DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1072         iscsi_destroy_endpoint(ep);
1073 }
1074
1075 static int qla4xxx_get_ep_param(struct iscsi_endpoint *ep,
1076                                 enum iscsi_param param,
1077                                 char *buf)
1078 {
1079         struct qla_endpoint *qla_ep = ep->dd_data;
1080         struct sockaddr *dst_addr;
1081
1082         DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1083
1084         switch (param) {
1085         case ISCSI_PARAM_CONN_PORT:
1086         case ISCSI_PARAM_CONN_ADDRESS:
1087                 if (!qla_ep)
1088                         return -ENOTCONN;
1089
1090                 dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
1091                 if (!dst_addr)
1092                         return -ENOTCONN;
1093
1094                 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
1095                                                  &qla_ep->dst_addr, param, buf);
1096         default:
1097                 return -ENOSYS;
1098         }
1099 }
1100
1101 static void qla4xxx_conn_get_stats(struct iscsi_cls_conn *cls_conn,
1102                                    struct iscsi_stats *stats)
1103 {
1104         struct iscsi_session *sess;
1105         struct iscsi_cls_session *cls_sess;
1106         struct ddb_entry *ddb_entry;
1107         struct scsi_qla_host *ha;
1108         struct ql_iscsi_stats *ql_iscsi_stats;
1109         int stats_size;
1110         int ret;
1111         dma_addr_t iscsi_stats_dma;
1112
1113         DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1114
1115         cls_sess = iscsi_conn_to_session(cls_conn);
1116         sess = cls_sess->dd_data;
1117         ddb_entry = sess->dd_data;
1118         ha = ddb_entry->ha;
1119
1120         stats_size = PAGE_ALIGN(sizeof(struct ql_iscsi_stats));
1121         /* Allocate memory */
1122         ql_iscsi_stats = dma_alloc_coherent(&ha->pdev->dev, stats_size,
1123                                             &iscsi_stats_dma, GFP_KERNEL);
1124         if (!ql_iscsi_stats) {
1125                 ql4_printk(KERN_ERR, ha,
1126                            "Unable to allocate memory for iscsi stats\n");
1127                 goto exit_get_stats;
1128         }
1129
1130         ret =  qla4xxx_get_mgmt_data(ha, ddb_entry->fw_ddb_index, stats_size,
1131                                      iscsi_stats_dma);
1132         if (ret != QLA_SUCCESS) {
1133                 ql4_printk(KERN_ERR, ha,
1134                            "Unable to retrieve iscsi stats\n");
1135                 goto free_stats;
1136         }
1137
1138         /* octets */
1139         stats->txdata_octets = le64_to_cpu(ql_iscsi_stats->tx_data_octets);
1140         stats->rxdata_octets = le64_to_cpu(ql_iscsi_stats->rx_data_octets);
1141         /* xmit pdus */
1142         stats->noptx_pdus = le32_to_cpu(ql_iscsi_stats->tx_nopout_pdus);
1143         stats->scsicmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_cmd_pdus);
1144         stats->tmfcmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_tmf_cmd_pdus);
1145         stats->login_pdus = le32_to_cpu(ql_iscsi_stats->tx_login_cmd_pdus);
1146         stats->text_pdus = le32_to_cpu(ql_iscsi_stats->tx_text_cmd_pdus);
1147         stats->dataout_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_write_pdus);
1148         stats->logout_pdus = le32_to_cpu(ql_iscsi_stats->tx_logout_cmd_pdus);
1149         stats->snack_pdus = le32_to_cpu(ql_iscsi_stats->tx_snack_req_pdus);
1150         /* recv pdus */
1151         stats->noprx_pdus = le32_to_cpu(ql_iscsi_stats->rx_nopin_pdus);
1152         stats->scsirsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_resp_pdus);
1153         stats->tmfrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_tmf_resp_pdus);
1154         stats->textrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_text_resp_pdus);
1155         stats->datain_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_read_pdus);
1156         stats->logoutrsp_pdus =
1157                         le32_to_cpu(ql_iscsi_stats->rx_logout_resp_pdus);
1158         stats->r2t_pdus = le32_to_cpu(ql_iscsi_stats->rx_r2t_pdus);
1159         stats->async_pdus = le32_to_cpu(ql_iscsi_stats->rx_async_pdus);
1160         stats->rjt_pdus = le32_to_cpu(ql_iscsi_stats->rx_reject_pdus);
1161
1162 free_stats:
1163         dma_free_coherent(&ha->pdev->dev, stats_size, ql_iscsi_stats,
1164                           iscsi_stats_dma);
1165 exit_get_stats:
1166         return;
1167 }
1168
1169 static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
1170 {
1171         struct iscsi_cls_session *session;
1172         struct iscsi_session *sess;
1173         unsigned long flags;
1174         enum blk_eh_timer_return ret = BLK_EH_NOT_HANDLED;
1175
1176         session = starget_to_session(scsi_target(sc->device));
1177         sess = session->dd_data;
1178
1179         spin_lock_irqsave(&session->lock, flags);
1180         if (session->state == ISCSI_SESSION_FAILED)
1181                 ret = BLK_EH_RESET_TIMER;
1182         spin_unlock_irqrestore(&session->lock, flags);
1183
1184         return ret;
1185 }
1186
1187 static void qla4xxx_set_port_speed(struct Scsi_Host *shost)
1188 {
1189         struct scsi_qla_host *ha = to_qla_host(shost);
1190         struct iscsi_cls_host *ihost = shost->shost_data;
1191         uint32_t speed = ISCSI_PORT_SPEED_UNKNOWN;
1192
1193         qla4xxx_get_firmware_state(ha);
1194
1195         switch (ha->addl_fw_state & 0x0F00) {
1196         case FW_ADDSTATE_LINK_SPEED_10MBPS:
1197                 speed = ISCSI_PORT_SPEED_10MBPS;
1198                 break;
1199         case FW_ADDSTATE_LINK_SPEED_100MBPS:
1200                 speed = ISCSI_PORT_SPEED_100MBPS;
1201                 break;
1202         case FW_ADDSTATE_LINK_SPEED_1GBPS:
1203                 speed = ISCSI_PORT_SPEED_1GBPS;
1204                 break;
1205         case FW_ADDSTATE_LINK_SPEED_10GBPS:
1206                 speed = ISCSI_PORT_SPEED_10GBPS;
1207                 break;
1208         }
1209         ihost->port_speed = speed;
1210 }
1211
1212 static void qla4xxx_set_port_state(struct Scsi_Host *shost)
1213 {
1214         struct scsi_qla_host *ha = to_qla_host(shost);
1215         struct iscsi_cls_host *ihost = shost->shost_data;
1216         uint32_t state = ISCSI_PORT_STATE_DOWN;
1217
1218         if (test_bit(AF_LINK_UP, &ha->flags))
1219                 state = ISCSI_PORT_STATE_UP;
1220
1221         ihost->port_state = state;
1222 }
1223
1224 static int qla4xxx_host_get_param(struct Scsi_Host *shost,
1225                                   enum iscsi_host_param param, char *buf)
1226 {
1227         struct scsi_qla_host *ha = to_qla_host(shost);
1228         int len;
1229
1230         switch (param) {
1231         case ISCSI_HOST_PARAM_HWADDRESS:
1232                 len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
1233                 break;
1234         case ISCSI_HOST_PARAM_IPADDRESS:
1235                 len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
1236                 break;
1237         case ISCSI_HOST_PARAM_INITIATOR_NAME:
1238                 len = sprintf(buf, "%s\n", ha->name_string);
1239                 break;
1240         case ISCSI_HOST_PARAM_PORT_STATE:
1241                 qla4xxx_set_port_state(shost);
1242                 len = sprintf(buf, "%s\n", iscsi_get_port_state_name(shost));
1243                 break;
1244         case ISCSI_HOST_PARAM_PORT_SPEED:
1245                 qla4xxx_set_port_speed(shost);
1246                 len = sprintf(buf, "%s\n", iscsi_get_port_speed_name(shost));
1247                 break;
1248         default:
1249                 return -ENOSYS;
1250         }
1251
1252         return len;
1253 }
1254
1255 static void qla4xxx_create_ipv4_iface(struct scsi_qla_host *ha)
1256 {
1257         if (ha->iface_ipv4)
1258                 return;
1259
1260         /* IPv4 */
1261         ha->iface_ipv4 = iscsi_create_iface(ha->host,
1262                                             &qla4xxx_iscsi_transport,
1263                                             ISCSI_IFACE_TYPE_IPV4, 0, 0);
1264         if (!ha->iface_ipv4)
1265                 ql4_printk(KERN_ERR, ha, "Could not create IPv4 iSCSI "
1266                            "iface0.\n");
1267 }
1268
1269 static void qla4xxx_create_ipv6_iface(struct scsi_qla_host *ha)
1270 {
1271         if (!ha->iface_ipv6_0)
1272                 /* IPv6 iface-0 */
1273                 ha->iface_ipv6_0 = iscsi_create_iface(ha->host,
1274                                                       &qla4xxx_iscsi_transport,
1275                                                       ISCSI_IFACE_TYPE_IPV6, 0,
1276                                                       0);
1277         if (!ha->iface_ipv6_0)
1278                 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
1279                            "iface0.\n");
1280
1281         if (!ha->iface_ipv6_1)
1282                 /* IPv6 iface-1 */
1283                 ha->iface_ipv6_1 = iscsi_create_iface(ha->host,
1284                                                       &qla4xxx_iscsi_transport,
1285                                                       ISCSI_IFACE_TYPE_IPV6, 1,
1286                                                       0);
1287         if (!ha->iface_ipv6_1)
1288                 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
1289                            "iface1.\n");
1290 }
1291
1292 static void qla4xxx_create_ifaces(struct scsi_qla_host *ha)
1293 {
1294         if (ha->ip_config.ipv4_options & IPOPT_IPV4_PROTOCOL_ENABLE)
1295                 qla4xxx_create_ipv4_iface(ha);
1296
1297         if (ha->ip_config.ipv6_options & IPV6_OPT_IPV6_PROTOCOL_ENABLE)
1298                 qla4xxx_create_ipv6_iface(ha);
1299 }
1300
1301 static void qla4xxx_destroy_ipv4_iface(struct scsi_qla_host *ha)
1302 {
1303         if (ha->iface_ipv4) {
1304                 iscsi_destroy_iface(ha->iface_ipv4);
1305                 ha->iface_ipv4 = NULL;
1306         }
1307 }
1308
1309 static void qla4xxx_destroy_ipv6_iface(struct scsi_qla_host *ha)
1310 {
1311         if (ha->iface_ipv6_0) {
1312                 iscsi_destroy_iface(ha->iface_ipv6_0);
1313                 ha->iface_ipv6_0 = NULL;
1314         }
1315         if (ha->iface_ipv6_1) {
1316                 iscsi_destroy_iface(ha->iface_ipv6_1);
1317                 ha->iface_ipv6_1 = NULL;
1318         }
1319 }
1320
1321 static void qla4xxx_destroy_ifaces(struct scsi_qla_host *ha)
1322 {
1323         qla4xxx_destroy_ipv4_iface(ha);
1324         qla4xxx_destroy_ipv6_iface(ha);
1325 }
1326
1327 static void qla4xxx_set_ipv6(struct scsi_qla_host *ha,
1328                              struct iscsi_iface_param_info *iface_param,
1329                              struct addr_ctrl_blk *init_fw_cb)
1330 {
1331         /*
1332          * iface_num 0 is valid for IPv6 Addr, linklocal, router, autocfg.
1333          * iface_num 1 is valid only for IPv6 Addr.
1334          */
1335         switch (iface_param->param) {
1336         case ISCSI_NET_PARAM_IPV6_ADDR:
1337                 if (iface_param->iface_num & 0x1)
1338                         /* IPv6 Addr 1 */
1339                         memcpy(init_fw_cb->ipv6_addr1, iface_param->value,
1340                                sizeof(init_fw_cb->ipv6_addr1));
1341                 else
1342                         /* IPv6 Addr 0 */
1343                         memcpy(init_fw_cb->ipv6_addr0, iface_param->value,
1344                                sizeof(init_fw_cb->ipv6_addr0));
1345                 break;
1346         case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
1347                 if (iface_param->iface_num & 0x1)
1348                         break;
1349                 memcpy(init_fw_cb->ipv6_if_id, &iface_param->value[8],
1350                        sizeof(init_fw_cb->ipv6_if_id));
1351                 break;
1352         case ISCSI_NET_PARAM_IPV6_ROUTER:
1353                 if (iface_param->iface_num & 0x1)
1354                         break;
1355                 memcpy(init_fw_cb->ipv6_dflt_rtr_addr, iface_param->value,
1356                        sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
1357                 break;
1358         case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
1359                 /* Autocfg applies to even interface */
1360                 if (iface_param->iface_num & 0x1)
1361                         break;
1362
1363                 if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_DISABLE)
1364                         init_fw_cb->ipv6_addtl_opts &=
1365                                 cpu_to_le16(
1366                                   ~IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
1367                 else if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_ND_ENABLE)
1368                         init_fw_cb->ipv6_addtl_opts |=
1369                                 cpu_to_le16(
1370                                   IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
1371                 else
1372                         ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
1373                                    "IPv6 addr\n");
1374                 break;
1375         case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
1376                 /* Autocfg applies to even interface */
1377                 if (iface_param->iface_num & 0x1)
1378                         break;
1379
1380                 if (iface_param->value[0] ==
1381                     ISCSI_IPV6_LINKLOCAL_AUTOCFG_ENABLE)
1382                         init_fw_cb->ipv6_addtl_opts |= cpu_to_le16(
1383                                         IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
1384                 else if (iface_param->value[0] ==
1385                          ISCSI_IPV6_LINKLOCAL_AUTOCFG_DISABLE)
1386                         init_fw_cb->ipv6_addtl_opts &= cpu_to_le16(
1387                                        ~IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
1388                 else
1389                         ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
1390                                    "IPv6 linklocal addr\n");
1391                 break;
1392         case ISCSI_NET_PARAM_IPV6_ROUTER_AUTOCFG:
1393                 /* Autocfg applies to even interface */
1394                 if (iface_param->iface_num & 0x1)
1395                         break;
1396
1397                 if (iface_param->value[0] == ISCSI_IPV6_ROUTER_AUTOCFG_ENABLE)
1398                         memset(init_fw_cb->ipv6_dflt_rtr_addr, 0,
1399                                sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
1400                 break;
1401         case ISCSI_NET_PARAM_IFACE_ENABLE:
1402                 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
1403                         init_fw_cb->ipv6_opts |=
1404                                 cpu_to_le16(IPV6_OPT_IPV6_PROTOCOL_ENABLE);
1405                         qla4xxx_create_ipv6_iface(ha);
1406                 } else {
1407                         init_fw_cb->ipv6_opts &=
1408                                 cpu_to_le16(~IPV6_OPT_IPV6_PROTOCOL_ENABLE &
1409                                             0xFFFF);
1410                         qla4xxx_destroy_ipv6_iface(ha);
1411                 }
1412                 break;
1413         case ISCSI_NET_PARAM_VLAN_TAG:
1414                 if (iface_param->len != sizeof(init_fw_cb->ipv6_vlan_tag))
1415                         break;
1416                 init_fw_cb->ipv6_vlan_tag =
1417                                 cpu_to_be16(*(uint16_t *)iface_param->value);
1418                 break;
1419         case ISCSI_NET_PARAM_VLAN_ENABLED:
1420                 if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
1421                         init_fw_cb->ipv6_opts |=
1422                                 cpu_to_le16(IPV6_OPT_VLAN_TAGGING_ENABLE);
1423                 else
1424                         init_fw_cb->ipv6_opts &=
1425                                 cpu_to_le16(~IPV6_OPT_VLAN_TAGGING_ENABLE);
1426                 break;
1427         case ISCSI_NET_PARAM_MTU:
1428                 init_fw_cb->eth_mtu_size =
1429                                 cpu_to_le16(*(uint16_t *)iface_param->value);
1430                 break;
1431         case ISCSI_NET_PARAM_PORT:
1432                 /* Autocfg applies to even interface */
1433                 if (iface_param->iface_num & 0x1)
1434                         break;
1435
1436                 init_fw_cb->ipv6_port =
1437                                 cpu_to_le16(*(uint16_t *)iface_param->value);
1438                 break;
1439         default:
1440                 ql4_printk(KERN_ERR, ha, "Unknown IPv6 param = %d\n",
1441                            iface_param->param);
1442                 break;
1443         }
1444 }
1445
1446 static void qla4xxx_set_ipv4(struct scsi_qla_host *ha,
1447                              struct iscsi_iface_param_info *iface_param,
1448                              struct addr_ctrl_blk *init_fw_cb)
1449 {
1450         switch (iface_param->param) {
1451         case ISCSI_NET_PARAM_IPV4_ADDR:
1452                 memcpy(init_fw_cb->ipv4_addr, iface_param->value,
1453                        sizeof(init_fw_cb->ipv4_addr));
1454                 break;
1455         case ISCSI_NET_PARAM_IPV4_SUBNET:
1456                 memcpy(init_fw_cb->ipv4_subnet, iface_param->value,
1457                        sizeof(init_fw_cb->ipv4_subnet));
1458                 break;
1459         case ISCSI_NET_PARAM_IPV4_GW:
1460                 memcpy(init_fw_cb->ipv4_gw_addr, iface_param->value,
1461                        sizeof(init_fw_cb->ipv4_gw_addr));
1462                 break;
1463         case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
1464                 if (iface_param->value[0] == ISCSI_BOOTPROTO_DHCP)
1465                         init_fw_cb->ipv4_tcp_opts |=
1466                                         cpu_to_le16(TCPOPT_DHCP_ENABLE);
1467                 else if (iface_param->value[0] == ISCSI_BOOTPROTO_STATIC)
1468                         init_fw_cb->ipv4_tcp_opts &=
1469                                         cpu_to_le16(~TCPOPT_DHCP_ENABLE);
1470                 else
1471                         ql4_printk(KERN_ERR, ha, "Invalid IPv4 bootproto\n");
1472                 break;
1473         case ISCSI_NET_PARAM_IFACE_ENABLE:
1474                 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
1475                         init_fw_cb->ipv4_ip_opts |=
1476                                 cpu_to_le16(IPOPT_IPV4_PROTOCOL_ENABLE);
1477                         qla4xxx_create_ipv4_iface(ha);
1478                 } else {
1479                         init_fw_cb->ipv4_ip_opts &=
1480                                 cpu_to_le16(~IPOPT_IPV4_PROTOCOL_ENABLE &
1481                                             0xFFFF);
1482                         qla4xxx_destroy_ipv4_iface(ha);
1483                 }
1484                 break;
1485         case ISCSI_NET_PARAM_VLAN_TAG:
1486                 if (iface_param->len != sizeof(init_fw_cb->ipv4_vlan_tag))
1487                         break;
1488                 init_fw_cb->ipv4_vlan_tag =
1489                                 cpu_to_be16(*(uint16_t *)iface_param->value);
1490                 break;
1491         case ISCSI_NET_PARAM_VLAN_ENABLED:
1492                 if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
1493                         init_fw_cb->ipv4_ip_opts |=
1494                                         cpu_to_le16(IPOPT_VLAN_TAGGING_ENABLE);
1495                 else
1496                         init_fw_cb->ipv4_ip_opts &=
1497                                         cpu_to_le16(~IPOPT_VLAN_TAGGING_ENABLE);
1498                 break;
1499         case ISCSI_NET_PARAM_MTU:
1500                 init_fw_cb->eth_mtu_size =
1501                                 cpu_to_le16(*(uint16_t *)iface_param->value);
1502                 break;
1503         case ISCSI_NET_PARAM_PORT:
1504                 init_fw_cb->ipv4_port =
1505                                 cpu_to_le16(*(uint16_t *)iface_param->value);
1506                 break;
1507         default:
1508                 ql4_printk(KERN_ERR, ha, "Unknown IPv4 param = %d\n",
1509                            iface_param->param);
1510                 break;
1511         }
1512 }
1513
1514 static void
1515 qla4xxx_initcb_to_acb(struct addr_ctrl_blk *init_fw_cb)
1516 {
1517         struct addr_ctrl_blk_def *acb;
1518         acb = (struct addr_ctrl_blk_def *)init_fw_cb;
1519         memset(acb->reserved1, 0, sizeof(acb->reserved1));
1520         memset(acb->reserved2, 0, sizeof(acb->reserved2));
1521         memset(acb->reserved3, 0, sizeof(acb->reserved3));
1522         memset(acb->reserved4, 0, sizeof(acb->reserved4));
1523         memset(acb->reserved5, 0, sizeof(acb->reserved5));
1524         memset(acb->reserved6, 0, sizeof(acb->reserved6));
1525         memset(acb->reserved7, 0, sizeof(acb->reserved7));
1526         memset(acb->reserved8, 0, sizeof(acb->reserved8));
1527         memset(acb->reserved9, 0, sizeof(acb->reserved9));
1528         memset(acb->reserved10, 0, sizeof(acb->reserved10));
1529         memset(acb->reserved11, 0, sizeof(acb->reserved11));
1530         memset(acb->reserved12, 0, sizeof(acb->reserved12));
1531         memset(acb->reserved13, 0, sizeof(acb->reserved13));
1532         memset(acb->reserved14, 0, sizeof(acb->reserved14));
1533         memset(acb->reserved15, 0, sizeof(acb->reserved15));
1534 }
1535
1536 static int
1537 qla4xxx_iface_set_param(struct Scsi_Host *shost, void *data, uint32_t len)
1538 {
1539         struct scsi_qla_host *ha = to_qla_host(shost);
1540         int rval = 0;
1541         struct iscsi_iface_param_info *iface_param = NULL;
1542         struct addr_ctrl_blk *init_fw_cb = NULL;
1543         dma_addr_t init_fw_cb_dma;
1544         uint32_t mbox_cmd[MBOX_REG_COUNT];
1545         uint32_t mbox_sts[MBOX_REG_COUNT];
1546         uint32_t rem = len;
1547         struct nlattr *attr;
1548
1549         init_fw_cb = dma_alloc_coherent(&ha->pdev->dev,
1550                                         sizeof(struct addr_ctrl_blk),
1551                                         &init_fw_cb_dma, GFP_KERNEL);
1552         if (!init_fw_cb) {
1553                 ql4_printk(KERN_ERR, ha, "%s: Unable to alloc init_cb\n",
1554                            __func__);
1555                 return -ENOMEM;
1556         }
1557
1558         memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
1559         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
1560         memset(&mbox_sts, 0, sizeof(mbox_sts));
1561
1562         if (qla4xxx_get_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma)) {
1563                 ql4_printk(KERN_ERR, ha, "%s: get ifcb failed\n", __func__);
1564                 rval = -EIO;
1565                 goto exit_init_fw_cb;
1566         }
1567
1568         nla_for_each_attr(attr, data, len, rem) {
1569                 iface_param = nla_data(attr);
1570
1571                 if (iface_param->param_type != ISCSI_NET_PARAM)
1572                         continue;
1573
1574                 switch (iface_param->iface_type) {
1575                 case ISCSI_IFACE_TYPE_IPV4:
1576                         switch (iface_param->iface_num) {
1577                         case 0:
1578                                 qla4xxx_set_ipv4(ha, iface_param, init_fw_cb);
1579                                 break;
1580                         default:
1581                                 /* Cannot have more than one IPv4 interface */
1582                                 ql4_printk(KERN_ERR, ha, "Invalid IPv4 iface "
1583                                            "number = %d\n",
1584                                            iface_param->iface_num);
1585                                 break;
1586                         }
1587                         break;
1588                 case ISCSI_IFACE_TYPE_IPV6:
1589                         switch (iface_param->iface_num) {
1590                         case 0:
1591                         case 1:
1592                                 qla4xxx_set_ipv6(ha, iface_param, init_fw_cb);
1593                                 break;
1594                         default:
1595                                 /* Cannot have more than two IPv6 interface */
1596                                 ql4_printk(KERN_ERR, ha, "Invalid IPv6 iface "
1597                                            "number = %d\n",
1598                                            iface_param->iface_num);
1599                                 break;
1600                         }
1601                         break;
1602                 default:
1603                         ql4_printk(KERN_ERR, ha, "Invalid iface type\n");
1604                         break;
1605                 }
1606         }
1607
1608         init_fw_cb->cookie = cpu_to_le32(0x11BEAD5A);
1609
1610         rval = qla4xxx_set_flash(ha, init_fw_cb_dma, FLASH_SEGMENT_IFCB,
1611                                  sizeof(struct addr_ctrl_blk),
1612                                  FLASH_OPT_RMW_COMMIT);
1613         if (rval != QLA_SUCCESS) {
1614                 ql4_printk(KERN_ERR, ha, "%s: set flash mbx failed\n",
1615                            __func__);
1616                 rval = -EIO;
1617                 goto exit_init_fw_cb;
1618         }
1619
1620         rval = qla4xxx_disable_acb(ha);
1621         if (rval != QLA_SUCCESS) {
1622                 ql4_printk(KERN_ERR, ha, "%s: disable acb mbx failed\n",
1623                            __func__);
1624                 rval = -EIO;
1625                 goto exit_init_fw_cb;
1626         }
1627
1628         wait_for_completion_timeout(&ha->disable_acb_comp,
1629                                     DISABLE_ACB_TOV * HZ);
1630
1631         qla4xxx_initcb_to_acb(init_fw_cb);
1632
1633         rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma);
1634         if (rval != QLA_SUCCESS) {
1635                 ql4_printk(KERN_ERR, ha, "%s: set acb mbx failed\n",
1636                            __func__);
1637                 rval = -EIO;
1638                 goto exit_init_fw_cb;
1639         }
1640
1641         memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
1642         qla4xxx_update_local_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb,
1643                                   init_fw_cb_dma);
1644
1645 exit_init_fw_cb:
1646         dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk),
1647                           init_fw_cb, init_fw_cb_dma);
1648
1649         return rval;
1650 }
1651
1652 static int qla4xxx_session_get_param(struct iscsi_cls_session *cls_sess,
1653                                      enum iscsi_param param, char *buf)
1654 {
1655         struct iscsi_session *sess = cls_sess->dd_data;
1656         struct ddb_entry *ddb_entry = sess->dd_data;
1657         struct scsi_qla_host *ha = ddb_entry->ha;
1658         struct iscsi_cls_conn *cls_conn = ddb_entry->conn;
1659         struct ql4_chap_table chap_tbl;
1660         int rval, len;
1661         uint16_t idx;
1662
1663         memset(&chap_tbl, 0, sizeof(chap_tbl));
1664         switch (param) {
1665         case ISCSI_PARAM_CHAP_IN_IDX:
1666                 rval = qla4xxx_get_chap_index(ha, sess->username_in,
1667                                               sess->password_in, BIDI_CHAP,
1668                                               &idx);
1669                 if (rval)
1670                         len = sprintf(buf, "\n");
1671                 else
1672                         len = sprintf(buf, "%hu\n", idx);
1673                 break;
1674         case ISCSI_PARAM_CHAP_OUT_IDX:
1675                 if (ddb_entry->ddb_type == FLASH_DDB) {
1676                         if (ddb_entry->chap_tbl_idx != INVALID_ENTRY) {
1677                                 idx = ddb_entry->chap_tbl_idx;
1678                                 rval = QLA_SUCCESS;
1679                         } else {
1680                                 rval = QLA_ERROR;
1681                         }
1682                 } else {
1683                         rval = qla4xxx_get_chap_index(ha, sess->username,
1684                                                       sess->password,
1685                                                       LOCAL_CHAP, &idx);
1686                 }
1687                 if (rval)
1688                         len = sprintf(buf, "\n");
1689                 else
1690                         len = sprintf(buf, "%hu\n", idx);
1691                 break;
1692         case ISCSI_PARAM_USERNAME:
1693         case ISCSI_PARAM_PASSWORD:
1694                 /* First, populate session username and password for FLASH DDB,
1695                  * if not already done. This happens when session login fails
1696                  * for a FLASH DDB.
1697                  */
1698                 if (ddb_entry->ddb_type == FLASH_DDB &&
1699                     ddb_entry->chap_tbl_idx != INVALID_ENTRY &&
1700                     !sess->username && !sess->password) {
1701                         idx = ddb_entry->chap_tbl_idx;
1702                         rval = qla4xxx_get_uni_chap_at_index(ha, chap_tbl.name,
1703                                                             chap_tbl.secret,
1704                                                             idx);
1705                         if (!rval) {
1706                                 iscsi_set_param(cls_conn, ISCSI_PARAM_USERNAME,
1707                                                 (char *)chap_tbl.name,
1708                                                 strlen((char *)chap_tbl.name));
1709                                 iscsi_set_param(cls_conn, ISCSI_PARAM_PASSWORD,
1710                                                 (char *)chap_tbl.secret,
1711                                                 chap_tbl.secret_len);
1712                         }
1713                 }
1714                 /* allow fall-through */
1715         default:
1716                 return iscsi_session_get_param(cls_sess, param, buf);
1717         }
1718
1719         return len;
1720 }
1721
1722 static int qla4xxx_conn_get_param(struct iscsi_cls_conn *cls_conn,
1723                                   enum iscsi_param param, char *buf)
1724 {
1725         struct iscsi_conn *conn;
1726         struct qla_conn *qla_conn;
1727         struct sockaddr *dst_addr;
1728         int len = 0;
1729
1730         conn = cls_conn->dd_data;
1731         qla_conn = conn->dd_data;
1732         dst_addr = (struct sockaddr *)&qla_conn->qla_ep->dst_addr;
1733
1734         switch (param) {
1735         case ISCSI_PARAM_CONN_PORT:
1736         case ISCSI_PARAM_CONN_ADDRESS:
1737                 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
1738                                                  dst_addr, param, buf);
1739         default:
1740                 return iscsi_conn_get_param(cls_conn, param, buf);
1741         }
1742
1743         return len;
1744
1745 }
1746
1747 int qla4xxx_get_ddb_index(struct scsi_qla_host *ha, uint16_t *ddb_index)
1748 {
1749         uint32_t mbx_sts = 0;
1750         uint16_t tmp_ddb_index;
1751         int ret;
1752
1753 get_ddb_index:
1754         tmp_ddb_index = find_first_zero_bit(ha->ddb_idx_map, MAX_DDB_ENTRIES);
1755
1756         if (tmp_ddb_index >= MAX_DDB_ENTRIES) {
1757                 DEBUG2(ql4_printk(KERN_INFO, ha,
1758                                   "Free DDB index not available\n"));
1759                 ret = QLA_ERROR;
1760                 goto exit_get_ddb_index;
1761         }
1762
1763         if (test_and_set_bit(tmp_ddb_index, ha->ddb_idx_map))
1764                 goto get_ddb_index;
1765
1766         DEBUG2(ql4_printk(KERN_INFO, ha,
1767                           "Found a free DDB index at %d\n", tmp_ddb_index));
1768         ret = qla4xxx_req_ddb_entry(ha, tmp_ddb_index, &mbx_sts);
1769         if (ret == QLA_ERROR) {
1770                 if (mbx_sts == MBOX_STS_COMMAND_ERROR) {
1771                         ql4_printk(KERN_INFO, ha,
1772                                    "DDB index = %d not available trying next\n",
1773                                    tmp_ddb_index);
1774                         goto get_ddb_index;
1775                 }
1776                 DEBUG2(ql4_printk(KERN_INFO, ha,
1777                                   "Free FW DDB not available\n"));
1778         }
1779
1780         *ddb_index = tmp_ddb_index;
1781
1782 exit_get_ddb_index:
1783         return ret;
1784 }
1785
1786 static int qla4xxx_match_ipaddress(struct scsi_qla_host *ha,
1787                                    struct ddb_entry *ddb_entry,
1788                                    char *existing_ipaddr,
1789                                    char *user_ipaddr)
1790 {
1791         uint8_t dst_ipaddr[IPv6_ADDR_LEN];
1792         char formatted_ipaddr[DDB_IPADDR_LEN];
1793         int status = QLA_SUCCESS, ret = 0;
1794
1795         if (ddb_entry->fw_ddb_entry.options & DDB_OPT_IPV6_DEVICE) {
1796                 ret = in6_pton(user_ipaddr, strlen(user_ipaddr), dst_ipaddr,
1797                                '\0', NULL);
1798                 if (ret == 0) {
1799                         status = QLA_ERROR;
1800                         goto out_match;
1801                 }
1802                 ret = sprintf(formatted_ipaddr, "%pI6", dst_ipaddr);
1803         } else {
1804                 ret = in4_pton(user_ipaddr, strlen(user_ipaddr), dst_ipaddr,
1805                                '\0', NULL);
1806                 if (ret == 0) {
1807                         status = QLA_ERROR;
1808                         goto out_match;
1809                 }
1810                 ret = sprintf(formatted_ipaddr, "%pI4", dst_ipaddr);
1811         }
1812
1813         if (strcmp(existing_ipaddr, formatted_ipaddr))
1814                 status = QLA_ERROR;
1815
1816 out_match:
1817         return status;
1818 }
1819
1820 static int qla4xxx_match_fwdb_session(struct scsi_qla_host *ha,
1821                                       struct iscsi_cls_conn *cls_conn)
1822 {
1823         int idx = 0, max_ddbs, rval;
1824         struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
1825         struct iscsi_session *sess, *existing_sess;
1826         struct iscsi_conn *conn, *existing_conn;
1827         struct ddb_entry *ddb_entry;
1828
1829         sess = cls_sess->dd_data;
1830         conn = cls_conn->dd_data;
1831
1832         if (sess->targetname == NULL ||
1833             conn->persistent_address == NULL ||
1834             conn->persistent_port == 0)
1835                 return QLA_ERROR;
1836
1837         max_ddbs =  is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
1838                                      MAX_DEV_DB_ENTRIES;
1839
1840         for (idx = 0; idx < max_ddbs; idx++) {
1841                 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
1842                 if (ddb_entry == NULL)
1843                         continue;
1844
1845                 if (ddb_entry->ddb_type != FLASH_DDB)
1846                         continue;
1847
1848                 existing_sess = ddb_entry->sess->dd_data;
1849                 existing_conn = ddb_entry->conn->dd_data;
1850
1851                 if (existing_sess->targetname == NULL ||
1852                     existing_conn->persistent_address == NULL ||
1853                     existing_conn->persistent_port == 0)
1854                         continue;
1855
1856                 DEBUG2(ql4_printk(KERN_INFO, ha,
1857                                   "IQN = %s User IQN = %s\n",
1858                                   existing_sess->targetname,
1859                                   sess->targetname));
1860
1861                 DEBUG2(ql4_printk(KERN_INFO, ha,
1862                                   "IP = %s User IP = %s\n",
1863                                   existing_conn->persistent_address,
1864                                   conn->persistent_address));
1865
1866                 DEBUG2(ql4_printk(KERN_INFO, ha,
1867                                   "Port = %d User Port = %d\n",
1868                                   existing_conn->persistent_port,
1869                                   conn->persistent_port));
1870
1871                 if (strcmp(existing_sess->targetname, sess->targetname))
1872                         continue;
1873                 rval = qla4xxx_match_ipaddress(ha, ddb_entry,
1874                                         existing_conn->persistent_address,
1875                                         conn->persistent_address);
1876                 if (rval == QLA_ERROR)
1877                         continue;
1878                 if (existing_conn->persistent_port != conn->persistent_port)
1879                         continue;
1880                 break;
1881         }
1882
1883         if (idx == max_ddbs)
1884                 return QLA_ERROR;
1885
1886         DEBUG2(ql4_printk(KERN_INFO, ha,
1887                           "Match found in fwdb sessions\n"));
1888         return QLA_SUCCESS;
1889 }
1890
1891 static struct iscsi_cls_session *
1892 qla4xxx_session_create(struct iscsi_endpoint *ep,
1893                         uint16_t cmds_max, uint16_t qdepth,
1894                         uint32_t initial_cmdsn)
1895 {
1896         struct iscsi_cls_session *cls_sess;
1897         struct scsi_qla_host *ha;
1898         struct qla_endpoint *qla_ep;
1899         struct ddb_entry *ddb_entry;
1900         uint16_t ddb_index;
1901         struct iscsi_session *sess;
1902         struct sockaddr *dst_addr;
1903         int ret;
1904
1905         DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1906         if (!ep) {
1907                 printk(KERN_ERR "qla4xxx: missing ep.\n");
1908                 return NULL;
1909         }
1910
1911         qla_ep = ep->dd_data;
1912         dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
1913         ha = to_qla_host(qla_ep->host);
1914
1915         ret = qla4xxx_get_ddb_index(ha, &ddb_index);
1916         if (ret == QLA_ERROR)
1917                 return NULL;
1918
1919         cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport, qla_ep->host,
1920                                        cmds_max, sizeof(struct ddb_entry),
1921                                        sizeof(struct ql4_task_data),
1922                                        initial_cmdsn, ddb_index);
1923         if (!cls_sess)
1924                 return NULL;
1925
1926         sess = cls_sess->dd_data;
1927         ddb_entry = sess->dd_data;
1928         ddb_entry->fw_ddb_index = ddb_index;
1929         ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
1930         ddb_entry->ha = ha;
1931         ddb_entry->sess = cls_sess;
1932         ddb_entry->unblock_sess = qla4xxx_unblock_ddb;
1933         ddb_entry->ddb_change = qla4xxx_ddb_change;
1934         cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
1935         ha->fw_ddb_index_map[ddb_entry->fw_ddb_index] = ddb_entry;
1936         ha->tot_ddbs++;
1937
1938         return cls_sess;
1939 }
1940
1941 static void qla4xxx_session_destroy(struct iscsi_cls_session *cls_sess)
1942 {
1943         struct iscsi_session *sess;
1944         struct ddb_entry *ddb_entry;
1945         struct scsi_qla_host *ha;
1946         unsigned long flags, wtime;
1947         struct dev_db_entry *fw_ddb_entry = NULL;
1948         dma_addr_t fw_ddb_entry_dma;
1949         uint32_t ddb_state;
1950         int ret;
1951
1952         DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1953         sess = cls_sess->dd_data;
1954         ddb_entry = sess->dd_data;
1955         ha = ddb_entry->ha;
1956
1957         fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1958                                           &fw_ddb_entry_dma, GFP_KERNEL);
1959         if (!fw_ddb_entry) {
1960                 ql4_printk(KERN_ERR, ha,
1961                            "%s: Unable to allocate dma buffer\n", __func__);
1962                 goto destroy_session;
1963         }
1964
1965         wtime = jiffies + (HZ * LOGOUT_TOV);
1966         do {
1967                 ret = qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index,
1968                                               fw_ddb_entry, fw_ddb_entry_dma,
1969                                               NULL, NULL, &ddb_state, NULL,
1970                                               NULL, NULL);
1971                 if (ret == QLA_ERROR)
1972                         goto destroy_session;
1973
1974                 if ((ddb_state == DDB_DS_NO_CONNECTION_ACTIVE) ||
1975                     (ddb_state == DDB_DS_SESSION_FAILED))
1976                         goto destroy_session;
1977
1978                 schedule_timeout_uninterruptible(HZ);
1979         } while ((time_after(wtime, jiffies)));
1980
1981 destroy_session:
1982         qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
1983
1984         spin_lock_irqsave(&ha->hardware_lock, flags);
1985         qla4xxx_free_ddb(ha, ddb_entry);
1986         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1987
1988         iscsi_session_teardown(cls_sess);
1989
1990         if (fw_ddb_entry)
1991                 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1992                                   fw_ddb_entry, fw_ddb_entry_dma);
1993 }
1994
1995 static struct iscsi_cls_conn *
1996 qla4xxx_conn_create(struct iscsi_cls_session *cls_sess, uint32_t conn_idx)
1997 {
1998         struct iscsi_cls_conn *cls_conn;
1999         struct iscsi_session *sess;
2000         struct ddb_entry *ddb_entry;
2001
2002         DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
2003         cls_conn = iscsi_conn_setup(cls_sess, sizeof(struct qla_conn),
2004                                     conn_idx);
2005         if (!cls_conn)
2006                 return NULL;
2007
2008         sess = cls_sess->dd_data;
2009         ddb_entry = sess->dd_data;
2010         ddb_entry->conn = cls_conn;
2011
2012         return cls_conn;
2013 }
2014
2015 static int qla4xxx_conn_bind(struct iscsi_cls_session *cls_session,
2016                              struct iscsi_cls_conn *cls_conn,
2017                              uint64_t transport_fd, int is_leading)
2018 {
2019         struct iscsi_conn *conn;
2020         struct qla_conn *qla_conn;
2021         struct iscsi_endpoint *ep;
2022
2023         DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
2024
2025         if (iscsi_conn_bind(cls_session, cls_conn, is_leading))
2026                 return -EINVAL;
2027         ep = iscsi_lookup_endpoint(transport_fd);
2028         conn = cls_conn->dd_data;
2029         qla_conn = conn->dd_data;
2030         qla_conn->qla_ep = ep->dd_data;
2031         return 0;
2032 }
2033
2034 static int qla4xxx_conn_start(struct iscsi_cls_conn *cls_conn)
2035 {
2036         struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
2037         struct iscsi_session *sess;
2038         struct ddb_entry *ddb_entry;
2039         struct scsi_qla_host *ha;
2040         struct dev_db_entry *fw_ddb_entry = NULL;
2041         dma_addr_t fw_ddb_entry_dma;
2042         uint32_t mbx_sts = 0;
2043         int ret = 0;
2044         int status = QLA_SUCCESS;
2045
2046         DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
2047         sess = cls_sess->dd_data;
2048         ddb_entry = sess->dd_data;
2049         ha = ddb_entry->ha;
2050
2051         /* Check if we have  matching FW DDB, if yes then do not
2052          * login to this target. This could cause target to logout previous
2053          * connection
2054          */
2055         ret = qla4xxx_match_fwdb_session(ha, cls_conn);
2056         if (ret == QLA_SUCCESS) {
2057                 ql4_printk(KERN_INFO, ha,
2058                            "Session already exist in FW.\n");
2059                 ret = -EEXIST;
2060                 goto exit_conn_start;
2061         }
2062
2063         fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
2064                                           &fw_ddb_entry_dma, GFP_KERNEL);
2065         if (!fw_ddb_entry) {
2066                 ql4_printk(KERN_ERR, ha,
2067                            "%s: Unable to allocate dma buffer\n", __func__);
2068                 ret = -ENOMEM;
2069                 goto exit_conn_start;
2070         }
2071
2072         ret = qla4xxx_set_param_ddbentry(ha, ddb_entry, cls_conn, &mbx_sts);
2073         if (ret) {
2074                 /* If iscsid is stopped and started then no need to do
2075                 * set param again since ddb state will be already
2076                 * active and FW does not allow set ddb to an
2077                 * active session.
2078                 */
2079                 if (mbx_sts)
2080                         if (ddb_entry->fw_ddb_device_state ==
2081                                                 DDB_DS_SESSION_ACTIVE) {
2082                                 ddb_entry->unblock_sess(ddb_entry->sess);
2083                                 goto exit_set_param;
2084                         }
2085
2086                 ql4_printk(KERN_ERR, ha, "%s: Failed set param for index[%d]\n",
2087                            __func__, ddb_entry->fw_ddb_index);
2088                 goto exit_conn_start;
2089         }
2090
2091         status = qla4xxx_conn_open(ha, ddb_entry->fw_ddb_index);
2092         if (status == QLA_ERROR) {
2093                 ql4_printk(KERN_ERR, ha, "%s: Login failed: %s\n", __func__,
2094                            sess->targetname);
2095                 ret = -EINVAL;
2096                 goto exit_conn_start;
2097         }
2098
2099         if (ddb_entry->fw_ddb_device_state == DDB_DS_NO_CONNECTION_ACTIVE)
2100                 ddb_entry->fw_ddb_device_state = DDB_DS_LOGIN_IN_PROCESS;
2101
2102         DEBUG2(printk(KERN_INFO "%s: DDB state [%d]\n", __func__,
2103                       ddb_entry->fw_ddb_device_state));
2104
2105 exit_set_param:
2106         ret = 0;
2107
2108 exit_conn_start:
2109         if (fw_ddb_entry)
2110                 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
2111                                   fw_ddb_entry, fw_ddb_entry_dma);
2112         return ret;
2113 }
2114
2115 static void qla4xxx_conn_destroy(struct iscsi_cls_conn *cls_conn)
2116 {
2117         struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
2118         struct iscsi_session *sess;
2119         struct scsi_qla_host *ha;
2120         struct ddb_entry *ddb_entry;
2121         int options;
2122
2123         DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
2124         sess = cls_sess->dd_data;
2125         ddb_entry = sess->dd_data;
2126         ha = ddb_entry->ha;
2127
2128         options = LOGOUT_OPTION_CLOSE_SESSION;
2129         if (qla4xxx_session_logout_ddb(ha, ddb_entry, options) == QLA_ERROR)
2130                 ql4_printk(KERN_ERR, ha, "%s: Logout failed\n", __func__);
2131 }
2132
2133 static void qla4xxx_task_work(struct work_struct *wdata)
2134 {
2135         struct ql4_task_data *task_data;
2136         struct scsi_qla_host *ha;
2137         struct passthru_status *sts;
2138         struct iscsi_task *task;
2139         struct iscsi_hdr *hdr;
2140         uint8_t *data;
2141         uint32_t data_len;
2142         struct iscsi_conn *conn;
2143         int hdr_len;
2144         itt_t itt;
2145
2146         task_data = container_of(wdata, struct ql4_task_data, task_work);
2147         ha = task_data->ha;
2148         task = task_data->task;
2149         sts = &task_data->sts;
2150         hdr_len = sizeof(struct iscsi_hdr);
2151
2152         DEBUG3(printk(KERN_INFO "Status returned\n"));
2153         DEBUG3(qla4xxx_dump_buffer(sts, 64));
2154         DEBUG3(printk(KERN_INFO "Response buffer"));
2155         DEBUG3(qla4xxx_dump_buffer(task_data->resp_buffer, 64));
2156
2157         conn = task->conn;
2158
2159         switch (sts->completionStatus) {
2160         case PASSTHRU_STATUS_COMPLETE:
2161                 hdr = (struct iscsi_hdr *)task_data->resp_buffer;
2162                 /* Assign back the itt in hdr, until we use the PREASSIGN_TAG */
2163                 itt = sts->handle;
2164                 hdr->itt = itt;
2165                 data = task_data->resp_buffer + hdr_len;
2166                 data_len = task_data->resp_len - hdr_len;
2167                 iscsi_complete_pdu(conn, hdr, data, data_len);
2168                 break;
2169         default:
2170                 ql4_printk(KERN_ERR, ha, "Passthru failed status = 0x%x\n",
2171                            sts->completionStatus);
2172                 break;
2173         }
2174         return;
2175 }
2176
2177 static int qla4xxx_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
2178 {
2179         struct ql4_task_data *task_data;
2180         struct iscsi_session *sess;
2181         struct ddb_entry *ddb_entry;
2182         struct scsi_qla_host *ha;
2183         int hdr_len;
2184
2185         sess = task->conn->session;
2186         ddb_entry = sess->dd_data;
2187         ha = ddb_entry->ha;
2188         task_data = task->dd_data;
2189         memset(task_data, 0, sizeof(struct ql4_task_data));
2190
2191         if (task->sc) {
2192                 ql4_printk(KERN_INFO, ha,
2193                            "%s: SCSI Commands not implemented\n", __func__);
2194                 return -EINVAL;
2195         }
2196
2197         hdr_len = sizeof(struct iscsi_hdr);
2198         task_data->ha = ha;
2199         task_data->task = task;
2200
2201         if (task->data_count) {
2202                 task_data->data_dma = dma_map_single(&ha->pdev->dev, task->data,
2203                                                      task->data_count,
2204                                                      PCI_DMA_TODEVICE);
2205         }
2206
2207         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
2208                       __func__, task->conn->max_recv_dlength, hdr_len));
2209
2210         task_data->resp_len = task->conn->max_recv_dlength + hdr_len;
2211         task_data->resp_buffer = dma_alloc_coherent(&ha->pdev->dev,
2212                                                     task_data->resp_len,
2213                                                     &task_data->resp_dma,
2214                                                     GFP_ATOMIC);
2215         if (!task_data->resp_buffer)
2216                 goto exit_alloc_pdu;
2217
2218         task_data->req_len = task->data_count + hdr_len;
2219         task_data->req_buffer = dma_alloc_coherent(&ha->pdev->dev,
2220                                                    task_data->req_len,
2221                                                    &task_data->req_dma,
2222                                                    GFP_ATOMIC);
2223         if (!task_data->req_buffer)
2224                 goto exit_alloc_pdu;
2225
2226         task->hdr = task_data->req_buffer;
2227
2228         INIT_WORK(&task_data->task_work, qla4xxx_task_work);
2229
2230         return 0;
2231
2232 exit_alloc_pdu:
2233         if (task_data->resp_buffer)
2234                 dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
2235                                   task_data->resp_buffer, task_data->resp_dma);
2236
2237         if (task_data->req_buffer)
2238                 dma_free_coherent(&ha->pdev->dev, task_data->req_len,
2239                                   task_data->req_buffer, task_data->req_dma);
2240         return -ENOMEM;
2241 }
2242
2243 static void qla4xxx_task_cleanup(struct iscsi_task *task)
2244 {
2245         struct ql4_task_data *task_data;
2246         struct iscsi_session *sess;
2247         struct ddb_entry *ddb_entry;
2248         struct scsi_qla_host *ha;
2249         int hdr_len;
2250
2251         hdr_len = sizeof(struct iscsi_hdr);
2252         sess = task->conn->session;
2253         ddb_entry = sess->dd_data;
2254         ha = ddb_entry->ha;
2255         task_data = task->dd_data;
2256
2257         if (task->data_count) {
2258                 dma_unmap_single(&ha->pdev->dev, task_data->data_dma,
2259                                  task->data_count, PCI_DMA_TODEVICE);
2260         }
2261
2262         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
2263                       __func__, task->conn->max_recv_dlength, hdr_len));
2264
2265         dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
2266                           task_data->resp_buffer, task_data->resp_dma);
2267         dma_free_coherent(&ha->pdev->dev, task_data->req_len,
2268                           task_data->req_buffer, task_data->req_dma);
2269         return;
2270 }
2271
2272 static int qla4xxx_task_xmit(struct iscsi_task *task)
2273 {
2274         struct scsi_cmnd *sc = task->sc;
2275         struct iscsi_session *sess = task->conn->session;
2276         struct ddb_entry *ddb_entry = sess->dd_data;
2277         struct scsi_qla_host *ha = ddb_entry->ha;
2278
2279         if (!sc)
2280                 return qla4xxx_send_passthru0(task);
2281
2282         ql4_printk(KERN_INFO, ha, "%s: scsi cmd xmit not implemented\n",
2283                    __func__);
2284         return -ENOSYS;
2285 }
2286
2287 static int qla4xxx_copy_from_fwddb_param(struct iscsi_bus_flash_session *sess,
2288                                          struct iscsi_bus_flash_conn *conn,
2289                                          struct dev_db_entry *fw_ddb_entry)
2290 {
2291         unsigned long options = 0;
2292         int rc = 0;
2293
2294         options = le16_to_cpu(fw_ddb_entry->options);
2295         conn->is_fw_assigned_ipv6 = test_bit(OPT_IS_FW_ASSIGNED_IPV6, &options);
2296         if (test_bit(OPT_IPV6_DEVICE, &options)) {
2297                 rc = iscsi_switch_str_param(&sess->portal_type,
2298                                             PORTAL_TYPE_IPV6);
2299                 if (rc)
2300                         goto exit_copy;
2301         } else {
2302                 rc = iscsi_switch_str_param(&sess->portal_type,
2303                                             PORTAL_TYPE_IPV4);
2304                 if (rc)
2305                         goto exit_copy;
2306         }
2307
2308         sess->auto_snd_tgt_disable = test_bit(OPT_AUTO_SENDTGTS_DISABLE,
2309                                               &options);
2310         sess->discovery_sess = test_bit(OPT_DISC_SESSION, &options);
2311         sess->entry_state = test_bit(OPT_ENTRY_STATE, &options);
2312
2313         options = le16_to_cpu(fw_ddb_entry->iscsi_options);
2314         conn->hdrdgst_en = test_bit(ISCSIOPT_HEADER_DIGEST_EN, &options);
2315         conn->datadgst_en = test_bit(ISCSIOPT_DATA_DIGEST_EN, &options);
2316         sess->imm_data_en = test_bit(ISCSIOPT_IMMEDIATE_DATA_EN, &options);
2317         sess->initial_r2t_en = test_bit(ISCSIOPT_INITIAL_R2T_EN, &options);
2318         sess->dataseq_inorder_en = test_bit(ISCSIOPT_DATA_SEQ_IN_ORDER,
2319                                             &options);
2320         sess->pdu_inorder_en = test_bit(ISCSIOPT_DATA_PDU_IN_ORDER, &options);
2321         sess->chap_auth_en = test_bit(ISCSIOPT_CHAP_AUTH_EN, &options);
2322         conn->snack_req_en = test_bit(ISCSIOPT_SNACK_REQ_EN, &options);
2323         sess->discovery_logout_en = test_bit(ISCSIOPT_DISCOVERY_LOGOUT_EN,
2324                                              &options);
2325         sess->bidi_chap_en = test_bit(ISCSIOPT_BIDI_CHAP_EN, &options);
2326         sess->discovery_auth_optional =
2327                         test_bit(ISCSIOPT_DISCOVERY_AUTH_OPTIONAL, &options);
2328         if (test_bit(ISCSIOPT_ERL1, &options))
2329                 sess->erl |= BIT_1;
2330         if (test_bit(ISCSIOPT_ERL0, &options))
2331                 sess->erl |= BIT_0;
2332
2333         options = le16_to_cpu(fw_ddb_entry->tcp_options);
2334         conn->tcp_timestamp_stat = test_bit(TCPOPT_TIMESTAMP_STAT, &options);
2335         conn->tcp_nagle_disable = test_bit(TCPOPT_NAGLE_DISABLE, &options);
2336         conn->tcp_wsf_disable = test_bit(TCPOPT_WSF_DISABLE, &options);
2337         if (test_bit(TCPOPT_TIMER_SCALE3, &options))
2338                 conn->tcp_timer_scale |= BIT_3;
2339         if (test_bit(TCPOPT_TIMER_SCALE2, &options))
2340                 conn->tcp_timer_scale |= BIT_2;
2341         if (test_bit(TCPOPT_TIMER_SCALE1, &options))
2342                 conn->tcp_timer_scale |= BIT_1;
2343
2344         conn->tcp_timer_scale >>= 1;
2345         conn->tcp_timestamp_en = test_bit(TCPOPT_TIMESTAMP_EN, &options);
2346
2347         options = le16_to_cpu(fw_ddb_entry->ip_options);
2348         conn->fragment_disable = test_bit(IPOPT_FRAGMENT_DISABLE, &options);
2349
2350         conn->max_recv_dlength = BYTE_UNITS *
2351                           le16_to_cpu(fw_ddb_entry->iscsi_max_rcv_data_seg_len);
2352         conn->max_xmit_dlength = BYTE_UNITS *
2353                           le16_to_cpu(fw_ddb_entry->iscsi_max_snd_data_seg_len);
2354         sess->first_burst = BYTE_UNITS *
2355                                le16_to_cpu(fw_ddb_entry->iscsi_first_burst_len);
2356         sess->max_burst = BYTE_UNITS *
2357                                  le16_to_cpu(fw_ddb_entry->iscsi_max_burst_len);
2358         sess->max_r2t = le16_to_cpu(fw_ddb_entry->iscsi_max_outsnd_r2t);
2359         sess->time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
2360         sess->time2retain = le16_to_cpu(fw_ddb_entry->iscsi_def_time2retain);
2361         sess->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
2362         conn->max_segment_size = le16_to_cpu(fw_ddb_entry->mss);
2363         conn->tcp_xmit_wsf = fw_ddb_entry->tcp_xmt_wsf;
2364         conn->tcp_recv_wsf = fw_ddb_entry->tcp_rcv_wsf;
2365         conn->ipv6_flow_label = le16_to_cpu(fw_ddb_entry->ipv6_flow_lbl);
2366         conn->keepalive_timeout = le16_to_cpu(fw_ddb_entry->ka_timeout);
2367         conn->local_port = le16_to_cpu(fw_ddb_entry->lcl_port);
2368         conn->statsn = le32_to_cpu(fw_ddb_entry->stat_sn);
2369         conn->exp_statsn = le32_to_cpu(fw_ddb_entry->exp_stat_sn);
2370         sess->discovery_parent_idx = le16_to_cpu(fw_ddb_entry->ddb_link);
2371         sess->discovery_parent_type = le16_to_cpu(fw_ddb_entry->ddb_link);
2372         sess->chap_out_idx = le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
2373         sess->tsid = le16_to_cpu(fw_ddb_entry->tsid);
2374
2375         sess->default_taskmgmt_timeout =
2376                                 le16_to_cpu(fw_ddb_entry->def_timeout);
2377         conn->port = le16_to_cpu(fw_ddb_entry->port);
2378
2379         options = le16_to_cpu(fw_ddb_entry->options);
2380         conn->ipaddress = kzalloc(IPv6_ADDR_LEN, GFP_KERNEL);
2381         if (!conn->ipaddress) {
2382                 rc = -ENOMEM;
2383                 goto exit_copy;
2384         }
2385
2386         conn->redirect_ipaddr = kzalloc(IPv6_ADDR_LEN, GFP_KERNEL);
2387         if (!conn->redirect_ipaddr) {
2388                 rc = -ENOMEM;
2389                 goto exit_copy;
2390         }
2391
2392         memcpy(conn->ipaddress, fw_ddb_entry->ip_addr, IPv6_ADDR_LEN);
2393         memcpy(conn->redirect_ipaddr, fw_ddb_entry->tgt_addr, IPv6_ADDR_LEN);
2394
2395         if (test_bit(OPT_IPV6_DEVICE, &options)) {
2396                 conn->ipv6_traffic_class = fw_ddb_entry->ipv4_tos;
2397
2398                 conn->link_local_ipv6_addr = kzalloc(IPv6_ADDR_LEN, GFP_KERNEL);
2399                 if (!conn->link_local_ipv6_addr) {
2400                         rc = -ENOMEM;
2401                         goto exit_copy;
2402                 }
2403
2404                 memcpy(conn->link_local_ipv6_addr,
2405                        fw_ddb_entry->link_local_ipv6_addr, IPv6_ADDR_LEN);
2406         } else {
2407                 conn->ipv4_tos = fw_ddb_entry->ipv4_tos;
2408         }
2409
2410         if (fw_ddb_entry->iscsi_name[0]) {
2411                 rc = iscsi_switch_str_param(&sess->targetname,
2412                                             (char *)fw_ddb_entry->iscsi_name);
2413                 if (rc)
2414                         goto exit_copy;
2415         }
2416
2417         if (fw_ddb_entry->iscsi_alias[0]) {
2418                 rc = iscsi_switch_str_param(&sess->targetalias,
2419                                             (char *)fw_ddb_entry->iscsi_alias);
2420                 if (rc)
2421                         goto exit_copy;
2422         }
2423
2424         COPY_ISID(sess->isid, fw_ddb_entry->isid);
2425
2426 exit_copy:
2427         return rc;
2428 }
2429
2430 static int qla4xxx_copy_to_fwddb_param(struct iscsi_bus_flash_session *sess,
2431                                        struct iscsi_bus_flash_conn *conn,
2432                                        struct dev_db_entry *fw_ddb_entry)
2433 {
2434         uint16_t options;
2435         int rc = 0;
2436
2437         options = le16_to_cpu(fw_ddb_entry->options);
2438         SET_BITVAL(conn->is_fw_assigned_ipv6,  options, BIT_11);
2439         if (!strncmp(sess->portal_type, PORTAL_TYPE_IPV6, 4))
2440                 options |= BIT_8;
2441         else
2442                 options &= ~BIT_8;
2443
2444         SET_BITVAL(sess->auto_snd_tgt_disable, options, BIT_6);
2445         SET_BITVAL(sess->discovery_sess, options, BIT_4);
2446         SET_BITVAL(sess->entry_state, options, BIT_3);
2447         fw_ddb_entry->options = cpu_to_le16(options);
2448
2449         options = le16_to_cpu(fw_ddb_entry->iscsi_options);
2450         SET_BITVAL(conn->hdrdgst_en, options, BIT_13);
2451         SET_BITVAL(conn->datadgst_en, options, BIT_12);
2452         SET_BITVAL(sess->imm_data_en, options, BIT_11);
2453         SET_BITVAL(sess->initial_r2t_en, options, BIT_10);
2454         SET_BITVAL(sess->dataseq_inorder_en, options, BIT_9);
2455         SET_BITVAL(sess->pdu_inorder_en, options, BIT_8);
2456         SET_BITVAL(sess->chap_auth_en, options, BIT_7);
2457         SET_BITVAL(conn->snack_req_en, options, BIT_6);
2458         SET_BITVAL(sess->discovery_logout_en, options, BIT_5);
2459         SET_BITVAL(sess->bidi_chap_en, options, BIT_4);
2460         SET_BITVAL(sess->discovery_auth_optional, options, BIT_3);
2461         SET_BITVAL(sess->erl & BIT_1, options, BIT_1);
2462         SET_BITVAL(sess->erl & BIT_0, options, BIT_0);
2463         fw_ddb_entry->iscsi_options = cpu_to_le16(options);
2464
2465         options = le16_to_cpu(fw_ddb_entry->tcp_options);
2466         SET_BITVAL(conn->tcp_timestamp_stat, options, BIT_6);
2467         SET_BITVAL(conn->tcp_nagle_disable, options, BIT_5);
2468         SET_BITVAL(conn->tcp_wsf_disable, options, BIT_4);
2469         SET_BITVAL(conn->tcp_timer_scale & BIT_2, options, BIT_3);
2470         SET_BITVAL(conn->tcp_timer_scale & BIT_1, options, BIT_2);
2471         SET_BITVAL(conn->tcp_timer_scale & BIT_0, options, BIT_1);
2472         SET_BITVAL(conn->tcp_timestamp_en, options, BIT_0);
2473         fw_ddb_entry->tcp_options = cpu_to_le16(options);
2474
2475         options = le16_to_cpu(fw_ddb_entry->ip_options);
2476         SET_BITVAL(conn->fragment_disable, options, BIT_4);
2477         fw_ddb_entry->ip_options = cpu_to_le16(options);
2478
2479         fw_ddb_entry->iscsi_max_outsnd_r2t = cpu_to_le16(sess->max_r2t);
2480         fw_ddb_entry->iscsi_max_rcv_data_seg_len =
2481                                cpu_to_le16(conn->max_recv_dlength / BYTE_UNITS);
2482         fw_ddb_entry->iscsi_max_snd_data_seg_len =
2483                                cpu_to_le16(conn->max_xmit_dlength / BYTE_UNITS);
2484         fw_ddb_entry->iscsi_first_burst_len =
2485                                 cpu_to_le16(sess->first_burst / BYTE_UNITS);
2486         fw_ddb_entry->iscsi_max_burst_len = cpu_to_le16(sess->max_burst /
2487                                             BYTE_UNITS);
2488         fw_ddb_entry->iscsi_def_time2wait = cpu_to_le16(sess->time2wait);
2489         fw_ddb_entry->iscsi_def_time2retain = cpu_to_le16(sess->time2retain);
2490         fw_ddb_entry->tgt_portal_grp = cpu_to_le16(sess->tpgt);
2491         fw_ddb_entry->mss = cpu_to_le16(conn->max_segment_size);
2492         fw_ddb_entry->tcp_xmt_wsf = (uint8_t) cpu_to_le32(conn->tcp_xmit_wsf);
2493         fw_ddb_entry->tcp_rcv_wsf = (uint8_t) cpu_to_le32(conn->tcp_recv_wsf);
2494         fw_ddb_entry->ipv6_flow_lbl = cpu_to_le16(conn->ipv6_flow_label);
2495         fw_ddb_entry->ka_timeout = cpu_to_le16(conn->keepalive_timeout);
2496         fw_ddb_entry->lcl_port = cpu_to_le16(conn->local_port);
2497         fw_ddb_entry->stat_sn = cpu_to_le32(conn->statsn);
2498         fw_ddb_entry->exp_stat_sn = cpu_to_le32(conn->exp_statsn);
2499         fw_ddb_entry->ddb_link = cpu_to_le16(sess->discovery_parent_idx);
2500         fw_ddb_entry->chap_tbl_idx = cpu_to_le16(sess->chap_out_idx);
2501         fw_ddb_entry->tsid = cpu_to_le16(sess->tsid);
2502         fw_ddb_entry->port = cpu_to_le16(conn->port);
2503         fw_ddb_entry->def_timeout =
2504                                 cpu_to_le16(sess->default_taskmgmt_timeout);
2505
2506         if (!strncmp(sess->portal_type, PORTAL_TYPE_IPV6, 4))
2507                 fw_ddb_entry->ipv4_tos = conn->ipv6_traffic_class;
2508         else
2509                 fw_ddb_entry->ipv4_tos = conn->ipv4_tos;
2510
2511         if (conn->ipaddress)
2512                 memcpy(fw_ddb_entry->ip_addr, conn->ipaddress,
2513                        sizeof(fw_ddb_entry->ip_addr));
2514
2515         if (conn->redirect_ipaddr)
2516                 memcpy(fw_ddb_entry->tgt_addr, conn->redirect_ipaddr,
2517                        sizeof(fw_ddb_entry->tgt_addr));
2518
2519         if (conn->link_local_ipv6_addr)
2520                 memcpy(fw_ddb_entry->link_local_ipv6_addr,
2521                        conn->link_local_ipv6_addr,
2522                        sizeof(fw_ddb_entry->link_local_ipv6_addr));
2523
2524         if (sess->targetname)
2525                 memcpy(fw_ddb_entry->iscsi_name, sess->targetname,
2526                        sizeof(fw_ddb_entry->iscsi_name));
2527
2528         if (sess->targetalias)
2529                 memcpy(fw_ddb_entry->iscsi_alias, sess->targetalias,
2530                        sizeof(fw_ddb_entry->iscsi_alias));
2531
2532         COPY_ISID(fw_ddb_entry->isid, sess->isid);
2533
2534         return rc;
2535 }
2536
2537 static void qla4xxx_copy_to_sess_conn_params(struct iscsi_conn *conn,
2538                                              struct iscsi_session *sess,
2539                                              struct dev_db_entry *fw_ddb_entry)
2540 {
2541         unsigned long options = 0;
2542         uint16_t ddb_link;
2543         uint16_t disc_parent;
2544
2545         options = le16_to_cpu(fw_ddb_entry->options);
2546         conn->is_fw_assigned_ipv6 = test_bit(OPT_IS_FW_ASSIGNED_IPV6, &options);
2547         sess->auto_snd_tgt_disable = test_bit(OPT_AUTO_SENDTGTS_DISABLE,
2548                                               &options);
2549         sess->discovery_sess = test_bit(OPT_DISC_SESSION, &options);
2550
2551         options = le16_to_cpu(fw_ddb_entry->iscsi_options);
2552         conn->hdrdgst_en = test_bit(ISCSIOPT_HEADER_DIGEST_EN, &options);
2553         conn->datadgst_en = test_bit(ISCSIOPT_DATA_DIGEST_EN, &options);
2554         sess->imm_data_en = test_bit(ISCSIOPT_IMMEDIATE_DATA_EN, &options);
2555         sess->initial_r2t_en = test_bit(ISCSIOPT_INITIAL_R2T_EN, &options);
2556         sess->dataseq_inorder_en = test_bit(ISCSIOPT_DATA_SEQ_IN_ORDER,
2557                                             &options);
2558         sess->pdu_inorder_en = test_bit(ISCSIOPT_DATA_PDU_IN_ORDER, &options);
2559         sess->chap_auth_en = test_bit(ISCSIOPT_CHAP_AUTH_EN, &options);
2560         sess->discovery_logout_en = test_bit(ISCSIOPT_DISCOVERY_LOGOUT_EN,
2561                                              &options);
2562         sess->bidi_chap_en = test_bit(ISCSIOPT_BIDI_CHAP_EN, &options);
2563         sess->discovery_auth_optional =
2564                         test_bit(ISCSIOPT_DISCOVERY_AUTH_OPTIONAL, &options);
2565         if (test_bit(ISCSIOPT_ERL1, &options))
2566                 sess->erl |= BIT_1;
2567         if (test_bit(ISCSIOPT_ERL0, &options))
2568                 sess->erl |= BIT_0;
2569
2570         options = le16_to_cpu(fw_ddb_entry->tcp_options);
2571         conn->tcp_timestamp_stat = test_bit(TCPOPT_TIMESTAMP_STAT, &options);
2572         conn->tcp_nagle_disable = test_bit(TCPOPT_NAGLE_DISABLE, &options);
2573         conn->tcp_wsf_disable = test_bit(TCPOPT_WSF_DISABLE, &options);
2574         if (test_bit(TCPOPT_TIMER_SCALE3, &options))
2575                 conn->tcp_timer_scale |= BIT_3;
2576         if (test_bit(TCPOPT_TIMER_SCALE2, &options))
2577                 conn->tcp_timer_scale |= BIT_2;
2578         if (test_bit(TCPOPT_TIMER_SCALE1, &options))
2579                 conn->tcp_timer_scale |= BIT_1;
2580
2581         conn->tcp_timer_scale >>= 1;
2582         conn->tcp_timestamp_en = test_bit(TCPOPT_TIMESTAMP_EN, &options);
2583
2584         options = le16_to_cpu(fw_ddb_entry->ip_options);