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