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