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