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