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