OSDN Git Service

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