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