]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/scsi/qla4xxx/ql4_os.c
Merge tag 'scsi-misc' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[karo-tx-linux.git] / drivers / scsi / qla4xxx / ql4_os.c
1 /*
2  * QLogic iSCSI HBA Driver
3  * Copyright (c)  2003-2012 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 = 0x1F;
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 0x1F.\n"
91                  "\t\t  Can be set to 0x3, 0x7, 0xF, 0x1F, 0x3F, 0x7F");
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
153 /*
154  * SCSI host template entry points
155  */
156 static int qla4xxx_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmd);
157 static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
158 static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
159 static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
160 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
161 static int qla4xxx_slave_alloc(struct scsi_device *device);
162 static int qla4xxx_slave_configure(struct scsi_device *device);
163 static void qla4xxx_slave_destroy(struct scsi_device *sdev);
164 static umode_t qla4_attr_is_visible(int param_type, int param);
165 static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type);
166 static int qla4xxx_change_queue_depth(struct scsi_device *sdev, int qdepth,
167                                       int reason);
168
169 static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
170     QLA82XX_LEGACY_INTR_CONFIG;
171
172 static struct scsi_host_template qla4xxx_driver_template = {
173         .module                 = THIS_MODULE,
174         .name                   = DRIVER_NAME,
175         .proc_name              = DRIVER_NAME,
176         .queuecommand           = qla4xxx_queuecommand,
177
178         .eh_abort_handler       = qla4xxx_eh_abort,
179         .eh_device_reset_handler = qla4xxx_eh_device_reset,
180         .eh_target_reset_handler = qla4xxx_eh_target_reset,
181         .eh_host_reset_handler  = qla4xxx_eh_host_reset,
182         .eh_timed_out           = qla4xxx_eh_cmd_timed_out,
183
184         .slave_configure        = qla4xxx_slave_configure,
185         .slave_alloc            = qla4xxx_slave_alloc,
186         .slave_destroy          = qla4xxx_slave_destroy,
187         .change_queue_depth     = qla4xxx_change_queue_depth,
188
189         .this_id                = -1,
190         .cmd_per_lun            = 3,
191         .use_clustering         = ENABLE_CLUSTERING,
192         .sg_tablesize           = SG_ALL,
193
194         .max_sectors            = 0xFFFF,
195         .shost_attrs            = qla4xxx_host_attrs,
196         .host_reset             = qla4xxx_host_reset,
197         .vendor_id              = SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_QLOGIC,
198 };
199
200 static struct iscsi_transport qla4xxx_iscsi_transport = {
201         .owner                  = THIS_MODULE,
202         .name                   = DRIVER_NAME,
203         .caps                   = CAP_TEXT_NEGO |
204                                   CAP_DATA_PATH_OFFLOAD | CAP_HDRDGST |
205                                   CAP_DATADGST | CAP_LOGIN_OFFLOAD |
206                                   CAP_MULTI_R2T,
207         .attr_is_visible        = qla4_attr_is_visible,
208         .create_session         = qla4xxx_session_create,
209         .destroy_session        = qla4xxx_session_destroy,
210         .start_conn             = qla4xxx_conn_start,
211         .create_conn            = qla4xxx_conn_create,
212         .bind_conn              = qla4xxx_conn_bind,
213         .stop_conn              = iscsi_conn_stop,
214         .destroy_conn           = qla4xxx_conn_destroy,
215         .set_param              = iscsi_set_param,
216         .get_conn_param         = qla4xxx_conn_get_param,
217         .get_session_param      = qla4xxx_session_get_param,
218         .get_ep_param           = qla4xxx_get_ep_param,
219         .ep_connect             = qla4xxx_ep_connect,
220         .ep_poll                = qla4xxx_ep_poll,
221         .ep_disconnect          = qla4xxx_ep_disconnect,
222         .get_stats              = qla4xxx_conn_get_stats,
223         .send_pdu               = iscsi_conn_send_pdu,
224         .xmit_task              = qla4xxx_task_xmit,
225         .cleanup_task           = qla4xxx_task_cleanup,
226         .alloc_pdu              = qla4xxx_alloc_pdu,
227
228         .get_host_param         = qla4xxx_host_get_param,
229         .set_iface_param        = qla4xxx_iface_set_param,
230         .get_iface_param        = qla4xxx_get_iface_param,
231         .bsg_request            = qla4xxx_bsg_request,
232         .send_ping              = qla4xxx_send_ping,
233         .get_chap               = qla4xxx_get_chap_list,
234         .delete_chap            = qla4xxx_delete_chap,
235 };
236
237 static struct scsi_transport_template *qla4xxx_scsi_transport;
238
239 static int qla4xxx_send_ping(struct Scsi_Host *shost, uint32_t iface_num,
240                              uint32_t iface_type, uint32_t payload_size,
241                              uint32_t pid, struct sockaddr *dst_addr)
242 {
243         struct scsi_qla_host *ha = to_qla_host(shost);
244         struct sockaddr_in *addr;
245         struct sockaddr_in6 *addr6;
246         uint32_t options = 0;
247         uint8_t ipaddr[IPv6_ADDR_LEN];
248         int rval;
249
250         memset(ipaddr, 0, IPv6_ADDR_LEN);
251         /* IPv4 to IPv4 */
252         if ((iface_type == ISCSI_IFACE_TYPE_IPV4) &&
253             (dst_addr->sa_family == AF_INET)) {
254                 addr = (struct sockaddr_in *)dst_addr;
255                 memcpy(ipaddr, &addr->sin_addr.s_addr, IP_ADDR_LEN);
256                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: IPv4 Ping src: %pI4 "
257                                   "dest: %pI4\n", __func__,
258                                   &ha->ip_config.ip_address, ipaddr));
259                 rval = qla4xxx_ping_iocb(ha, options, payload_size, pid,
260                                          ipaddr);
261                 if (rval)
262                         rval = -EINVAL;
263         } else if ((iface_type == ISCSI_IFACE_TYPE_IPV6) &&
264                    (dst_addr->sa_family == AF_INET6)) {
265                 /* IPv6 to IPv6 */
266                 addr6 = (struct sockaddr_in6 *)dst_addr;
267                 memcpy(ipaddr, &addr6->sin6_addr.in6_u.u6_addr8, IPv6_ADDR_LEN);
268
269                 options |= PING_IPV6_PROTOCOL_ENABLE;
270
271                 /* Ping using LinkLocal address */
272                 if ((iface_num == 0) || (iface_num == 1)) {
273                         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: LinkLocal Ping "
274                                           "src: %pI6 dest: %pI6\n", __func__,
275                                           &ha->ip_config.ipv6_link_local_addr,
276                                           ipaddr));
277                         options |= PING_IPV6_LINKLOCAL_ADDR;
278                         rval = qla4xxx_ping_iocb(ha, options, payload_size,
279                                                  pid, ipaddr);
280                 } else {
281                         ql4_printk(KERN_WARNING, ha, "%s: iface num = %d "
282                                    "not supported\n", __func__, iface_num);
283                         rval = -ENOSYS;
284                         goto exit_send_ping;
285                 }
286
287                 /*
288                  * If ping using LinkLocal address fails, try ping using
289                  * IPv6 address
290                  */
291                 if (rval != QLA_SUCCESS) {
292                         options &= ~PING_IPV6_LINKLOCAL_ADDR;
293                         if (iface_num == 0) {
294                                 options |= PING_IPV6_ADDR0;
295                                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: IPv6 "
296                                                   "Ping src: %pI6 "
297                                                   "dest: %pI6\n", __func__,
298                                                   &ha->ip_config.ipv6_addr0,
299                                                   ipaddr));
300                         } else if (iface_num == 1) {
301                                 options |= PING_IPV6_ADDR1;
302                                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: IPv6 "
303                                                   "Ping src: %pI6 "
304                                                   "dest: %pI6\n", __func__,
305                                                   &ha->ip_config.ipv6_addr1,
306                                                   ipaddr));
307                         }
308                         rval = qla4xxx_ping_iocb(ha, options, payload_size,
309                                                  pid, ipaddr);
310                         if (rval)
311                                 rval = -EINVAL;
312                 }
313         } else
314                 rval = -ENOSYS;
315 exit_send_ping:
316         return rval;
317 }
318
319 static umode_t qla4_attr_is_visible(int param_type, int param)
320 {
321         switch (param_type) {
322         case ISCSI_HOST_PARAM:
323                 switch (param) {
324                 case ISCSI_HOST_PARAM_HWADDRESS:
325                 case ISCSI_HOST_PARAM_IPADDRESS:
326                 case ISCSI_HOST_PARAM_INITIATOR_NAME:
327                 case ISCSI_HOST_PARAM_PORT_STATE:
328                 case ISCSI_HOST_PARAM_PORT_SPEED:
329                         return S_IRUGO;
330                 default:
331                         return 0;
332                 }
333         case ISCSI_PARAM:
334                 switch (param) {
335                 case ISCSI_PARAM_PERSISTENT_ADDRESS:
336                 case ISCSI_PARAM_PERSISTENT_PORT:
337                 case ISCSI_PARAM_CONN_ADDRESS:
338                 case ISCSI_PARAM_CONN_PORT:
339                 case ISCSI_PARAM_TARGET_NAME:
340                 case ISCSI_PARAM_TPGT:
341                 case ISCSI_PARAM_TARGET_ALIAS:
342                 case ISCSI_PARAM_MAX_BURST:
343                 case ISCSI_PARAM_MAX_R2T:
344                 case ISCSI_PARAM_FIRST_BURST:
345                 case ISCSI_PARAM_MAX_RECV_DLENGTH:
346                 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
347                 case ISCSI_PARAM_IFACE_NAME:
348                 case ISCSI_PARAM_CHAP_OUT_IDX:
349                 case ISCSI_PARAM_CHAP_IN_IDX:
350                 case ISCSI_PARAM_USERNAME:
351                 case ISCSI_PARAM_PASSWORD:
352                 case ISCSI_PARAM_USERNAME_IN:
353                 case ISCSI_PARAM_PASSWORD_IN:
354                         return S_IRUGO;
355                 default:
356                         return 0;
357                 }
358         case ISCSI_NET_PARAM:
359                 switch (param) {
360                 case ISCSI_NET_PARAM_IPV4_ADDR:
361                 case ISCSI_NET_PARAM_IPV4_SUBNET:
362                 case ISCSI_NET_PARAM_IPV4_GW:
363                 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
364                 case ISCSI_NET_PARAM_IFACE_ENABLE:
365                 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
366                 case ISCSI_NET_PARAM_IPV6_ADDR:
367                 case ISCSI_NET_PARAM_IPV6_ROUTER:
368                 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
369                 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
370                 case ISCSI_NET_PARAM_VLAN_ID:
371                 case ISCSI_NET_PARAM_VLAN_PRIORITY:
372                 case ISCSI_NET_PARAM_VLAN_ENABLED:
373                 case ISCSI_NET_PARAM_MTU:
374                 case ISCSI_NET_PARAM_PORT:
375                         return S_IRUGO;
376                 default:
377                         return 0;
378                 }
379         }
380
381         return 0;
382 }
383
384 static int qla4xxx_get_chap_list(struct Scsi_Host *shost, uint16_t chap_tbl_idx,
385                                   uint32_t *num_entries, char *buf)
386 {
387         struct scsi_qla_host *ha = to_qla_host(shost);
388         struct ql4_chap_table *chap_table;
389         struct iscsi_chap_rec *chap_rec;
390         int max_chap_entries = 0;
391         int valid_chap_entries = 0;
392         int ret = 0, i;
393
394         if (is_qla8022(ha))
395                 max_chap_entries = (ha->hw.flt_chap_size / 2) /
396                                         sizeof(struct ql4_chap_table);
397         else
398                 max_chap_entries = MAX_CHAP_ENTRIES_40XX;
399
400         ql4_printk(KERN_INFO, ha, "%s: num_entries = %d, CHAP idx = %d\n",
401                         __func__, *num_entries, chap_tbl_idx);
402
403         if (!buf) {
404                 ret = -ENOMEM;
405                 goto exit_get_chap_list;
406         }
407
408         chap_rec = (struct iscsi_chap_rec *) buf;
409         mutex_lock(&ha->chap_sem);
410         for (i = chap_tbl_idx; i < max_chap_entries; i++) {
411                 chap_table = (struct ql4_chap_table *)ha->chap_list + i;
412                 if (chap_table->cookie !=
413                     __constant_cpu_to_le16(CHAP_VALID_COOKIE))
414                         continue;
415
416                 chap_rec->chap_tbl_idx = i;
417                 strncpy(chap_rec->username, chap_table->name,
418                         ISCSI_CHAP_AUTH_NAME_MAX_LEN);
419                 strncpy(chap_rec->password, chap_table->secret,
420                         QL4_CHAP_MAX_SECRET_LEN);
421                 chap_rec->password_length = chap_table->secret_len;
422
423                 if (chap_table->flags & BIT_7) /* local */
424                         chap_rec->chap_type = CHAP_TYPE_OUT;
425
426                 if (chap_table->flags & BIT_6) /* peer */
427                         chap_rec->chap_type = CHAP_TYPE_IN;
428
429                 chap_rec++;
430
431                 valid_chap_entries++;
432                 if (valid_chap_entries == *num_entries)
433                         break;
434                 else
435                         continue;
436         }
437         mutex_unlock(&ha->chap_sem);
438
439 exit_get_chap_list:
440         ql4_printk(KERN_INFO, ha, "%s: Valid CHAP Entries = %d\n",
441                         __func__,  valid_chap_entries);
442         *num_entries = valid_chap_entries;
443         return ret;
444 }
445
446 static int __qla4xxx_is_chap_active(struct device *dev, void *data)
447 {
448         int ret = 0;
449         uint16_t *chap_tbl_idx = (uint16_t *) data;
450         struct iscsi_cls_session *cls_session;
451         struct iscsi_session *sess;
452         struct ddb_entry *ddb_entry;
453
454         if (!iscsi_is_session_dev(dev))
455                 goto exit_is_chap_active;
456
457         cls_session = iscsi_dev_to_session(dev);
458         sess = cls_session->dd_data;
459         ddb_entry = sess->dd_data;
460
461         if (iscsi_session_chkready(cls_session))
462                 goto exit_is_chap_active;
463
464         if (ddb_entry->chap_tbl_idx == *chap_tbl_idx)
465                 ret = 1;
466
467 exit_is_chap_active:
468         return ret;
469 }
470
471 static int qla4xxx_is_chap_active(struct Scsi_Host *shost,
472                                   uint16_t chap_tbl_idx)
473 {
474         int ret = 0;
475
476         ret = device_for_each_child(&shost->shost_gendev, &chap_tbl_idx,
477                                     __qla4xxx_is_chap_active);
478
479         return ret;
480 }
481
482 static int qla4xxx_delete_chap(struct Scsi_Host *shost, uint16_t chap_tbl_idx)
483 {
484         struct scsi_qla_host *ha = to_qla_host(shost);
485         struct ql4_chap_table *chap_table;
486         dma_addr_t chap_dma;
487         int max_chap_entries = 0;
488         uint32_t offset = 0;
489         uint32_t chap_size;
490         int ret = 0;
491
492         chap_table = dma_pool_alloc(ha->chap_dma_pool, GFP_KERNEL, &chap_dma);
493         if (chap_table == NULL)
494                 return -ENOMEM;
495
496         memset(chap_table, 0, sizeof(struct ql4_chap_table));
497
498         if (is_qla8022(ha))
499                 max_chap_entries = (ha->hw.flt_chap_size / 2) /
500                                    sizeof(struct ql4_chap_table);
501         else
502                 max_chap_entries = MAX_CHAP_ENTRIES_40XX;
503
504         if (chap_tbl_idx > max_chap_entries) {
505                 ret = -EINVAL;
506                 goto exit_delete_chap;
507         }
508
509         /* Check if chap index is in use.
510          * If chap is in use don't delet chap entry */
511         ret = qla4xxx_is_chap_active(shost, chap_tbl_idx);
512         if (ret) {
513                 ql4_printk(KERN_INFO, ha, "CHAP entry %d is in use, cannot "
514                            "delete from flash\n", chap_tbl_idx);
515                 ret = -EBUSY;
516                 goto exit_delete_chap;
517         }
518
519         chap_size = sizeof(struct ql4_chap_table);
520         if (is_qla40XX(ha))
521                 offset = FLASH_CHAP_OFFSET | (chap_tbl_idx * chap_size);
522         else {
523                 offset = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_region_chap << 2);
524                 /* flt_chap_size is CHAP table size for both ports
525                  * so divide it by 2 to calculate the offset for second port
526                  */
527                 if (ha->port_num == 1)
528                         offset += (ha->hw.flt_chap_size / 2);
529                 offset += (chap_tbl_idx * chap_size);
530         }
531
532         ret = qla4xxx_get_flash(ha, chap_dma, offset, chap_size);
533         if (ret != QLA_SUCCESS) {
534                 ret = -EINVAL;
535                 goto exit_delete_chap;
536         }
537
538         DEBUG2(ql4_printk(KERN_INFO, ha, "Chap Cookie: x%x\n",
539                           __le16_to_cpu(chap_table->cookie)));
540
541         if (__le16_to_cpu(chap_table->cookie) != CHAP_VALID_COOKIE) {
542                 ql4_printk(KERN_ERR, ha, "No valid chap entry found\n");
543                 goto exit_delete_chap;
544         }
545
546         chap_table->cookie = __constant_cpu_to_le16(0xFFFF);
547
548         offset = FLASH_CHAP_OFFSET |
549                         (chap_tbl_idx * sizeof(struct ql4_chap_table));
550         ret = qla4xxx_set_flash(ha, chap_dma, offset, chap_size,
551                                 FLASH_OPT_RMW_COMMIT);
552         if (ret == QLA_SUCCESS && ha->chap_list) {
553                 mutex_lock(&ha->chap_sem);
554                 /* Update ha chap_list cache */
555                 memcpy((struct ql4_chap_table *)ha->chap_list + chap_tbl_idx,
556                         chap_table, sizeof(struct ql4_chap_table));
557                 mutex_unlock(&ha->chap_sem);
558         }
559         if (ret != QLA_SUCCESS)
560                 ret =  -EINVAL;
561
562 exit_delete_chap:
563         dma_pool_free(ha->chap_dma_pool, chap_table, chap_dma);
564         return ret;
565 }
566
567 static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
568                                    enum iscsi_param_type param_type,
569                                    int param, char *buf)
570 {
571         struct Scsi_Host *shost = iscsi_iface_to_shost(iface);
572         struct scsi_qla_host *ha = to_qla_host(shost);
573         int len = -ENOSYS;
574
575         if (param_type != ISCSI_NET_PARAM)
576                 return -ENOSYS;
577
578         switch (param) {
579         case ISCSI_NET_PARAM_IPV4_ADDR:
580                 len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
581                 break;
582         case ISCSI_NET_PARAM_IPV4_SUBNET:
583                 len = sprintf(buf, "%pI4\n", &ha->ip_config.subnet_mask);
584                 break;
585         case ISCSI_NET_PARAM_IPV4_GW:
586                 len = sprintf(buf, "%pI4\n", &ha->ip_config.gateway);
587                 break;
588         case ISCSI_NET_PARAM_IFACE_ENABLE:
589                 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
590                         len = sprintf(buf, "%s\n",
591                                       (ha->ip_config.ipv4_options &
592                                        IPOPT_IPV4_PROTOCOL_ENABLE) ?
593                                       "enabled" : "disabled");
594                 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
595                         len = sprintf(buf, "%s\n",
596                                       (ha->ip_config.ipv6_options &
597                                        IPV6_OPT_IPV6_PROTOCOL_ENABLE) ?
598                                        "enabled" : "disabled");
599                 break;
600         case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
601                 len = sprintf(buf, "%s\n",
602                               (ha->ip_config.tcp_options & TCPOPT_DHCP_ENABLE) ?
603                               "dhcp" : "static");
604                 break;
605         case ISCSI_NET_PARAM_IPV6_ADDR:
606                 if (iface->iface_num == 0)
607                         len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr0);
608                 if (iface->iface_num == 1)
609                         len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr1);
610                 break;
611         case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
612                 len = sprintf(buf, "%pI6\n",
613                               &ha->ip_config.ipv6_link_local_addr);
614                 break;
615         case ISCSI_NET_PARAM_IPV6_ROUTER:
616                 len = sprintf(buf, "%pI6\n",
617                               &ha->ip_config.ipv6_default_router_addr);
618                 break;
619         case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
620                 len = sprintf(buf, "%s\n",
621                               (ha->ip_config.ipv6_addl_options &
622                                IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) ?
623                                "nd" : "static");
624                 break;
625         case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
626                 len = sprintf(buf, "%s\n",
627                               (ha->ip_config.ipv6_addl_options &
628                                IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR) ?
629                                "auto" : "static");
630                 break;
631         case ISCSI_NET_PARAM_VLAN_ID:
632                 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
633                         len = sprintf(buf, "%d\n",
634                                       (ha->ip_config.ipv4_vlan_tag &
635                                        ISCSI_MAX_VLAN_ID));
636                 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
637                         len = sprintf(buf, "%d\n",
638                                       (ha->ip_config.ipv6_vlan_tag &
639                                        ISCSI_MAX_VLAN_ID));
640                 break;
641         case ISCSI_NET_PARAM_VLAN_PRIORITY:
642                 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
643                         len = sprintf(buf, "%d\n",
644                                       ((ha->ip_config.ipv4_vlan_tag >> 13) &
645                                         ISCSI_MAX_VLAN_PRIORITY));
646                 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
647                         len = sprintf(buf, "%d\n",
648                                       ((ha->ip_config.ipv6_vlan_tag >> 13) &
649                                         ISCSI_MAX_VLAN_PRIORITY));
650                 break;
651         case ISCSI_NET_PARAM_VLAN_ENABLED:
652                 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
653                         len = sprintf(buf, "%s\n",
654                                       (ha->ip_config.ipv4_options &
655                                        IPOPT_VLAN_TAGGING_ENABLE) ?
656                                        "enabled" : "disabled");
657                 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
658                         len = sprintf(buf, "%s\n",
659                                       (ha->ip_config.ipv6_options &
660                                        IPV6_OPT_VLAN_TAGGING_ENABLE) ?
661                                        "enabled" : "disabled");
662                 break;
663         case ISCSI_NET_PARAM_MTU:
664                 len = sprintf(buf, "%d\n", ha->ip_config.eth_mtu_size);
665                 break;
666         case ISCSI_NET_PARAM_PORT:
667                 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
668                         len = sprintf(buf, "%d\n", ha->ip_config.ipv4_port);
669                 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
670                         len = sprintf(buf, "%d\n", ha->ip_config.ipv6_port);
671                 break;
672         default:
673                 len = -ENOSYS;
674         }
675
676         return len;
677 }
678
679 static struct iscsi_endpoint *
680 qla4xxx_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
681                    int non_blocking)
682 {
683         int ret;
684         struct iscsi_endpoint *ep;
685         struct qla_endpoint *qla_ep;
686         struct scsi_qla_host *ha;
687         struct sockaddr_in *addr;
688         struct sockaddr_in6 *addr6;
689
690         DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
691         if (!shost) {
692                 ret = -ENXIO;
693                 printk(KERN_ERR "%s: shost is NULL\n",
694                        __func__);
695                 return ERR_PTR(ret);
696         }
697
698         ha = iscsi_host_priv(shost);
699
700         ep = iscsi_create_endpoint(sizeof(struct qla_endpoint));
701         if (!ep) {
702                 ret = -ENOMEM;
703                 return ERR_PTR(ret);
704         }
705
706         qla_ep = ep->dd_data;
707         memset(qla_ep, 0, sizeof(struct qla_endpoint));
708         if (dst_addr->sa_family == AF_INET) {
709                 memcpy(&qla_ep->dst_addr, dst_addr, sizeof(struct sockaddr_in));
710                 addr = (struct sockaddr_in *)&qla_ep->dst_addr;
711                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI4\n", __func__,
712                                   (char *)&addr->sin_addr));
713         } else if (dst_addr->sa_family == AF_INET6) {
714                 memcpy(&qla_ep->dst_addr, dst_addr,
715                        sizeof(struct sockaddr_in6));
716                 addr6 = (struct sockaddr_in6 *)&qla_ep->dst_addr;
717                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI6\n", __func__,
718                                   (char *)&addr6->sin6_addr));
719         }
720
721         qla_ep->host = shost;
722
723         return ep;
724 }
725
726 static int qla4xxx_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
727 {
728         struct qla_endpoint *qla_ep;
729         struct scsi_qla_host *ha;
730         int ret = 0;
731
732         DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
733         qla_ep = ep->dd_data;
734         ha = to_qla_host(qla_ep->host);
735
736         if (adapter_up(ha) && !test_bit(AF_BUILD_DDB_LIST, &ha->flags))
737                 ret = 1;
738
739         return ret;
740 }
741
742 static void qla4xxx_ep_disconnect(struct iscsi_endpoint *ep)
743 {
744         DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
745         iscsi_destroy_endpoint(ep);
746 }
747
748 static int qla4xxx_get_ep_param(struct iscsi_endpoint *ep,
749                                 enum iscsi_param param,
750                                 char *buf)
751 {
752         struct qla_endpoint *qla_ep = ep->dd_data;
753         struct sockaddr *dst_addr;
754
755         DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
756
757         switch (param) {
758         case ISCSI_PARAM_CONN_PORT:
759         case ISCSI_PARAM_CONN_ADDRESS:
760                 if (!qla_ep)
761                         return -ENOTCONN;
762
763                 dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
764                 if (!dst_addr)
765                         return -ENOTCONN;
766
767                 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
768                                                  &qla_ep->dst_addr, param, buf);
769         default:
770                 return -ENOSYS;
771         }
772 }
773
774 static void qla4xxx_conn_get_stats(struct iscsi_cls_conn *cls_conn,
775                                    struct iscsi_stats *stats)
776 {
777         struct iscsi_session *sess;
778         struct iscsi_cls_session *cls_sess;
779         struct ddb_entry *ddb_entry;
780         struct scsi_qla_host *ha;
781         struct ql_iscsi_stats *ql_iscsi_stats;
782         int stats_size;
783         int ret;
784         dma_addr_t iscsi_stats_dma;
785
786         DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
787
788         cls_sess = iscsi_conn_to_session(cls_conn);
789         sess = cls_sess->dd_data;
790         ddb_entry = sess->dd_data;
791         ha = ddb_entry->ha;
792
793         stats_size = PAGE_ALIGN(sizeof(struct ql_iscsi_stats));
794         /* Allocate memory */
795         ql_iscsi_stats = dma_alloc_coherent(&ha->pdev->dev, stats_size,
796                                             &iscsi_stats_dma, GFP_KERNEL);
797         if (!ql_iscsi_stats) {
798                 ql4_printk(KERN_ERR, ha,
799                            "Unable to allocate memory for iscsi stats\n");
800                 goto exit_get_stats;
801         }
802
803         ret =  qla4xxx_get_mgmt_data(ha, ddb_entry->fw_ddb_index, stats_size,
804                                      iscsi_stats_dma);
805         if (ret != QLA_SUCCESS) {
806                 ql4_printk(KERN_ERR, ha,
807                            "Unable to retrieve iscsi stats\n");
808                 goto free_stats;
809         }
810
811         /* octets */
812         stats->txdata_octets = le64_to_cpu(ql_iscsi_stats->tx_data_octets);
813         stats->rxdata_octets = le64_to_cpu(ql_iscsi_stats->rx_data_octets);
814         /* xmit pdus */
815         stats->noptx_pdus = le32_to_cpu(ql_iscsi_stats->tx_nopout_pdus);
816         stats->scsicmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_cmd_pdus);
817         stats->tmfcmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_tmf_cmd_pdus);
818         stats->login_pdus = le32_to_cpu(ql_iscsi_stats->tx_login_cmd_pdus);
819         stats->text_pdus = le32_to_cpu(ql_iscsi_stats->tx_text_cmd_pdus);
820         stats->dataout_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_write_pdus);
821         stats->logout_pdus = le32_to_cpu(ql_iscsi_stats->tx_logout_cmd_pdus);
822         stats->snack_pdus = le32_to_cpu(ql_iscsi_stats->tx_snack_req_pdus);
823         /* recv pdus */
824         stats->noprx_pdus = le32_to_cpu(ql_iscsi_stats->rx_nopin_pdus);
825         stats->scsirsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_resp_pdus);
826         stats->tmfrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_tmf_resp_pdus);
827         stats->textrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_text_resp_pdus);
828         stats->datain_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_read_pdus);
829         stats->logoutrsp_pdus =
830                         le32_to_cpu(ql_iscsi_stats->rx_logout_resp_pdus);
831         stats->r2t_pdus = le32_to_cpu(ql_iscsi_stats->rx_r2t_pdus);
832         stats->async_pdus = le32_to_cpu(ql_iscsi_stats->rx_async_pdus);
833         stats->rjt_pdus = le32_to_cpu(ql_iscsi_stats->rx_reject_pdus);
834
835 free_stats:
836         dma_free_coherent(&ha->pdev->dev, stats_size, ql_iscsi_stats,
837                           iscsi_stats_dma);
838 exit_get_stats:
839         return;
840 }
841
842 static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
843 {
844         struct iscsi_cls_session *session;
845         struct iscsi_session *sess;
846         unsigned long flags;
847         enum blk_eh_timer_return ret = BLK_EH_NOT_HANDLED;
848
849         session = starget_to_session(scsi_target(sc->device));
850         sess = session->dd_data;
851
852         spin_lock_irqsave(&session->lock, flags);
853         if (session->state == ISCSI_SESSION_FAILED)
854                 ret = BLK_EH_RESET_TIMER;
855         spin_unlock_irqrestore(&session->lock, flags);
856
857         return ret;
858 }
859
860 static void qla4xxx_set_port_speed(struct Scsi_Host *shost)
861 {
862         struct scsi_qla_host *ha = to_qla_host(shost);
863         struct iscsi_cls_host *ihost = shost->shost_data;
864         uint32_t speed = ISCSI_PORT_SPEED_UNKNOWN;
865
866         qla4xxx_get_firmware_state(ha);
867
868         switch (ha->addl_fw_state & 0x0F00) {
869         case FW_ADDSTATE_LINK_SPEED_10MBPS:
870                 speed = ISCSI_PORT_SPEED_10MBPS;
871                 break;
872         case FW_ADDSTATE_LINK_SPEED_100MBPS:
873                 speed = ISCSI_PORT_SPEED_100MBPS;
874                 break;
875         case FW_ADDSTATE_LINK_SPEED_1GBPS:
876                 speed = ISCSI_PORT_SPEED_1GBPS;
877                 break;
878         case FW_ADDSTATE_LINK_SPEED_10GBPS:
879                 speed = ISCSI_PORT_SPEED_10GBPS;
880                 break;
881         }
882         ihost->port_speed = speed;
883 }
884
885 static void qla4xxx_set_port_state(struct Scsi_Host *shost)
886 {
887         struct scsi_qla_host *ha = to_qla_host(shost);
888         struct iscsi_cls_host *ihost = shost->shost_data;
889         uint32_t state = ISCSI_PORT_STATE_DOWN;
890
891         if (test_bit(AF_LINK_UP, &ha->flags))
892                 state = ISCSI_PORT_STATE_UP;
893
894         ihost->port_state = state;
895 }
896
897 static int qla4xxx_host_get_param(struct Scsi_Host *shost,
898                                   enum iscsi_host_param param, char *buf)
899 {
900         struct scsi_qla_host *ha = to_qla_host(shost);
901         int len;
902
903         switch (param) {
904         case ISCSI_HOST_PARAM_HWADDRESS:
905                 len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
906                 break;
907         case ISCSI_HOST_PARAM_IPADDRESS:
908                 len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
909                 break;
910         case ISCSI_HOST_PARAM_INITIATOR_NAME:
911                 len = sprintf(buf, "%s\n", ha->name_string);
912                 break;
913         case ISCSI_HOST_PARAM_PORT_STATE:
914                 qla4xxx_set_port_state(shost);
915                 len = sprintf(buf, "%s\n", iscsi_get_port_state_name(shost));
916                 break;
917         case ISCSI_HOST_PARAM_PORT_SPEED:
918                 qla4xxx_set_port_speed(shost);
919                 len = sprintf(buf, "%s\n", iscsi_get_port_speed_name(shost));
920                 break;
921         default:
922                 return -ENOSYS;
923         }
924
925         return len;
926 }
927
928 static void qla4xxx_create_ipv4_iface(struct scsi_qla_host *ha)
929 {
930         if (ha->iface_ipv4)
931                 return;
932
933         /* IPv4 */
934         ha->iface_ipv4 = iscsi_create_iface(ha->host,
935                                             &qla4xxx_iscsi_transport,
936                                             ISCSI_IFACE_TYPE_IPV4, 0, 0);
937         if (!ha->iface_ipv4)
938                 ql4_printk(KERN_ERR, ha, "Could not create IPv4 iSCSI "
939                            "iface0.\n");
940 }
941
942 static void qla4xxx_create_ipv6_iface(struct scsi_qla_host *ha)
943 {
944         if (!ha->iface_ipv6_0)
945                 /* IPv6 iface-0 */
946                 ha->iface_ipv6_0 = iscsi_create_iface(ha->host,
947                                                       &qla4xxx_iscsi_transport,
948                                                       ISCSI_IFACE_TYPE_IPV6, 0,
949                                                       0);
950         if (!ha->iface_ipv6_0)
951                 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
952                            "iface0.\n");
953
954         if (!ha->iface_ipv6_1)
955                 /* IPv6 iface-1 */
956                 ha->iface_ipv6_1 = iscsi_create_iface(ha->host,
957                                                       &qla4xxx_iscsi_transport,
958                                                       ISCSI_IFACE_TYPE_IPV6, 1,
959                                                       0);
960         if (!ha->iface_ipv6_1)
961                 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
962                            "iface1.\n");
963 }
964
965 static void qla4xxx_create_ifaces(struct scsi_qla_host *ha)
966 {
967         if (ha->ip_config.ipv4_options & IPOPT_IPV4_PROTOCOL_ENABLE)
968                 qla4xxx_create_ipv4_iface(ha);
969
970         if (ha->ip_config.ipv6_options & IPV6_OPT_IPV6_PROTOCOL_ENABLE)
971                 qla4xxx_create_ipv6_iface(ha);
972 }
973
974 static void qla4xxx_destroy_ipv4_iface(struct scsi_qla_host *ha)
975 {
976         if (ha->iface_ipv4) {
977                 iscsi_destroy_iface(ha->iface_ipv4);
978                 ha->iface_ipv4 = NULL;
979         }
980 }
981
982 static void qla4xxx_destroy_ipv6_iface(struct scsi_qla_host *ha)
983 {
984         if (ha->iface_ipv6_0) {
985                 iscsi_destroy_iface(ha->iface_ipv6_0);
986                 ha->iface_ipv6_0 = NULL;
987         }
988         if (ha->iface_ipv6_1) {
989                 iscsi_destroy_iface(ha->iface_ipv6_1);
990                 ha->iface_ipv6_1 = NULL;
991         }
992 }
993
994 static void qla4xxx_destroy_ifaces(struct scsi_qla_host *ha)
995 {
996         qla4xxx_destroy_ipv4_iface(ha);
997         qla4xxx_destroy_ipv6_iface(ha);
998 }
999
1000 static void qla4xxx_set_ipv6(struct scsi_qla_host *ha,
1001                              struct iscsi_iface_param_info *iface_param,
1002                              struct addr_ctrl_blk *init_fw_cb)
1003 {
1004         /*
1005          * iface_num 0 is valid for IPv6 Addr, linklocal, router, autocfg.
1006          * iface_num 1 is valid only for IPv6 Addr.
1007          */
1008         switch (iface_param->param) {
1009         case ISCSI_NET_PARAM_IPV6_ADDR:
1010                 if (iface_param->iface_num & 0x1)
1011                         /* IPv6 Addr 1 */
1012                         memcpy(init_fw_cb->ipv6_addr1, iface_param->value,
1013                                sizeof(init_fw_cb->ipv6_addr1));
1014                 else
1015                         /* IPv6 Addr 0 */
1016                         memcpy(init_fw_cb->ipv6_addr0, iface_param->value,
1017                                sizeof(init_fw_cb->ipv6_addr0));
1018                 break;
1019         case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
1020                 if (iface_param->iface_num & 0x1)
1021                         break;
1022                 memcpy(init_fw_cb->ipv6_if_id, &iface_param->value[8],
1023                        sizeof(init_fw_cb->ipv6_if_id));
1024                 break;
1025         case ISCSI_NET_PARAM_IPV6_ROUTER:
1026                 if (iface_param->iface_num & 0x1)
1027                         break;
1028                 memcpy(init_fw_cb->ipv6_dflt_rtr_addr, iface_param->value,
1029                        sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
1030                 break;
1031         case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
1032                 /* Autocfg applies to even interface */
1033                 if (iface_param->iface_num & 0x1)
1034                         break;
1035
1036                 if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_DISABLE)
1037                         init_fw_cb->ipv6_addtl_opts &=
1038                                 cpu_to_le16(
1039                                   ~IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
1040                 else if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_ND_ENABLE)
1041                         init_fw_cb->ipv6_addtl_opts |=
1042                                 cpu_to_le16(
1043                                   IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
1044                 else
1045                         ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
1046                                    "IPv6 addr\n");
1047                 break;
1048         case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
1049                 /* Autocfg applies to even interface */
1050                 if (iface_param->iface_num & 0x1)
1051                         break;
1052
1053                 if (iface_param->value[0] ==
1054                     ISCSI_IPV6_LINKLOCAL_AUTOCFG_ENABLE)
1055                         init_fw_cb->ipv6_addtl_opts |= cpu_to_le16(
1056                                         IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
1057                 else if (iface_param->value[0] ==
1058                          ISCSI_IPV6_LINKLOCAL_AUTOCFG_DISABLE)
1059                         init_fw_cb->ipv6_addtl_opts &= cpu_to_le16(
1060                                        ~IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
1061                 else
1062                         ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
1063                                    "IPv6 linklocal addr\n");
1064                 break;
1065         case ISCSI_NET_PARAM_IPV6_ROUTER_AUTOCFG:
1066                 /* Autocfg applies to even interface */
1067                 if (iface_param->iface_num & 0x1)
1068                         break;
1069
1070                 if (iface_param->value[0] == ISCSI_IPV6_ROUTER_AUTOCFG_ENABLE)
1071                         memset(init_fw_cb->ipv6_dflt_rtr_addr, 0,
1072                                sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
1073                 break;
1074         case ISCSI_NET_PARAM_IFACE_ENABLE:
1075                 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
1076                         init_fw_cb->ipv6_opts |=
1077                                 cpu_to_le16(IPV6_OPT_IPV6_PROTOCOL_ENABLE);
1078                         qla4xxx_create_ipv6_iface(ha);
1079                 } else {
1080                         init_fw_cb->ipv6_opts &=
1081                                 cpu_to_le16(~IPV6_OPT_IPV6_PROTOCOL_ENABLE &
1082                                             0xFFFF);
1083                         qla4xxx_destroy_ipv6_iface(ha);
1084                 }
1085                 break;
1086         case ISCSI_NET_PARAM_VLAN_TAG:
1087                 if (iface_param->len != sizeof(init_fw_cb->ipv6_vlan_tag))
1088                         break;
1089                 init_fw_cb->ipv6_vlan_tag =
1090                                 cpu_to_be16(*(uint16_t *)iface_param->value);
1091                 break;
1092         case ISCSI_NET_PARAM_VLAN_ENABLED:
1093                 if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
1094                         init_fw_cb->ipv6_opts |=
1095                                 cpu_to_le16(IPV6_OPT_VLAN_TAGGING_ENABLE);
1096                 else
1097                         init_fw_cb->ipv6_opts &=
1098                                 cpu_to_le16(~IPV6_OPT_VLAN_TAGGING_ENABLE);
1099                 break;
1100         case ISCSI_NET_PARAM_MTU:
1101                 init_fw_cb->eth_mtu_size =
1102                                 cpu_to_le16(*(uint16_t *)iface_param->value);
1103                 break;
1104         case ISCSI_NET_PARAM_PORT:
1105                 /* Autocfg applies to even interface */
1106                 if (iface_param->iface_num & 0x1)
1107                         break;
1108
1109                 init_fw_cb->ipv6_port =
1110                                 cpu_to_le16(*(uint16_t *)iface_param->value);
1111                 break;
1112         default:
1113                 ql4_printk(KERN_ERR, ha, "Unknown IPv6 param = %d\n",
1114                            iface_param->param);
1115                 break;
1116         }
1117 }
1118
1119 static void qla4xxx_set_ipv4(struct scsi_qla_host *ha,
1120                              struct iscsi_iface_param_info *iface_param,
1121                              struct addr_ctrl_blk *init_fw_cb)
1122 {
1123         switch (iface_param->param) {
1124         case ISCSI_NET_PARAM_IPV4_ADDR:
1125                 memcpy(init_fw_cb->ipv4_addr, iface_param->value,
1126                        sizeof(init_fw_cb->ipv4_addr));
1127                 break;
1128         case ISCSI_NET_PARAM_IPV4_SUBNET:
1129                 memcpy(init_fw_cb->ipv4_subnet, iface_param->value,
1130                        sizeof(init_fw_cb->ipv4_subnet));
1131                 break;
1132         case ISCSI_NET_PARAM_IPV4_GW:
1133                 memcpy(init_fw_cb->ipv4_gw_addr, iface_param->value,
1134                        sizeof(init_fw_cb->ipv4_gw_addr));
1135                 break;
1136         case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
1137                 if (iface_param->value[0] == ISCSI_BOOTPROTO_DHCP)
1138                         init_fw_cb->ipv4_tcp_opts |=
1139                                         cpu_to_le16(TCPOPT_DHCP_ENABLE);
1140                 else if (iface_param->value[0] == ISCSI_BOOTPROTO_STATIC)
1141                         init_fw_cb->ipv4_tcp_opts &=
1142                                         cpu_to_le16(~TCPOPT_DHCP_ENABLE);
1143                 else
1144                         ql4_printk(KERN_ERR, ha, "Invalid IPv4 bootproto\n");
1145                 break;
1146         case ISCSI_NET_PARAM_IFACE_ENABLE:
1147                 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
1148                         init_fw_cb->ipv4_ip_opts |=
1149                                 cpu_to_le16(IPOPT_IPV4_PROTOCOL_ENABLE);
1150                         qla4xxx_create_ipv4_iface(ha);
1151                 } else {
1152                         init_fw_cb->ipv4_ip_opts &=
1153                                 cpu_to_le16(~IPOPT_IPV4_PROTOCOL_ENABLE &
1154                                             0xFFFF);
1155                         qla4xxx_destroy_ipv4_iface(ha);
1156                 }
1157                 break;
1158         case ISCSI_NET_PARAM_VLAN_TAG:
1159                 if (iface_param->len != sizeof(init_fw_cb->ipv4_vlan_tag))
1160                         break;
1161                 init_fw_cb->ipv4_vlan_tag =
1162                                 cpu_to_be16(*(uint16_t *)iface_param->value);
1163                 break;
1164         case ISCSI_NET_PARAM_VLAN_ENABLED:
1165                 if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
1166                         init_fw_cb->ipv4_ip_opts |=
1167                                         cpu_to_le16(IPOPT_VLAN_TAGGING_ENABLE);
1168                 else
1169                         init_fw_cb->ipv4_ip_opts &=
1170                                         cpu_to_le16(~IPOPT_VLAN_TAGGING_ENABLE);
1171                 break;
1172         case ISCSI_NET_PARAM_MTU:
1173                 init_fw_cb->eth_mtu_size =
1174                                 cpu_to_le16(*(uint16_t *)iface_param->value);
1175                 break;
1176         case ISCSI_NET_PARAM_PORT:
1177                 init_fw_cb->ipv4_port =
1178                                 cpu_to_le16(*(uint16_t *)iface_param->value);
1179                 break;
1180         default:
1181                 ql4_printk(KERN_ERR, ha, "Unknown IPv4 param = %d\n",
1182                            iface_param->param);
1183                 break;
1184         }
1185 }
1186
1187 static void
1188 qla4xxx_initcb_to_acb(struct addr_ctrl_blk *init_fw_cb)
1189 {
1190         struct addr_ctrl_blk_def *acb;
1191         acb = (struct addr_ctrl_blk_def *)init_fw_cb;
1192         memset(acb->reserved1, 0, sizeof(acb->reserved1));
1193         memset(acb->reserved2, 0, sizeof(acb->reserved2));
1194         memset(acb->reserved3, 0, sizeof(acb->reserved3));
1195         memset(acb->reserved4, 0, sizeof(acb->reserved4));
1196         memset(acb->reserved5, 0, sizeof(acb->reserved5));
1197         memset(acb->reserved6, 0, sizeof(acb->reserved6));
1198         memset(acb->reserved7, 0, sizeof(acb->reserved7));
1199         memset(acb->reserved8, 0, sizeof(acb->reserved8));
1200         memset(acb->reserved9, 0, sizeof(acb->reserved9));
1201         memset(acb->reserved10, 0, sizeof(acb->reserved10));
1202         memset(acb->reserved11, 0, sizeof(acb->reserved11));
1203         memset(acb->reserved12, 0, sizeof(acb->reserved12));
1204         memset(acb->reserved13, 0, sizeof(acb->reserved13));
1205         memset(acb->reserved14, 0, sizeof(acb->reserved14));
1206         memset(acb->reserved15, 0, sizeof(acb->reserved15));
1207 }
1208
1209 static int
1210 qla4xxx_iface_set_param(struct Scsi_Host *shost, void *data, uint32_t len)
1211 {
1212         struct scsi_qla_host *ha = to_qla_host(shost);
1213         int rval = 0;
1214         struct iscsi_iface_param_info *iface_param = NULL;
1215         struct addr_ctrl_blk *init_fw_cb = NULL;
1216         dma_addr_t init_fw_cb_dma;
1217         uint32_t mbox_cmd[MBOX_REG_COUNT];
1218         uint32_t mbox_sts[MBOX_REG_COUNT];
1219         uint32_t rem = len;
1220         struct nlattr *attr;
1221
1222         init_fw_cb = dma_alloc_coherent(&ha->pdev->dev,
1223                                         sizeof(struct addr_ctrl_blk),
1224                                         &init_fw_cb_dma, GFP_KERNEL);
1225         if (!init_fw_cb) {
1226                 ql4_printk(KERN_ERR, ha, "%s: Unable to alloc init_cb\n",
1227                            __func__);
1228                 return -ENOMEM;
1229         }
1230
1231         memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
1232         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
1233         memset(&mbox_sts, 0, sizeof(mbox_sts));
1234
1235         if (qla4xxx_get_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma)) {
1236                 ql4_printk(KERN_ERR, ha, "%s: get ifcb failed\n", __func__);
1237                 rval = -EIO;
1238                 goto exit_init_fw_cb;
1239         }
1240
1241         nla_for_each_attr(attr, data, len, rem) {
1242                 iface_param = nla_data(attr);
1243
1244                 if (iface_param->param_type != ISCSI_NET_PARAM)
1245                         continue;
1246
1247                 switch (iface_param->iface_type) {
1248                 case ISCSI_IFACE_TYPE_IPV4:
1249                         switch (iface_param->iface_num) {
1250                         case 0:
1251                                 qla4xxx_set_ipv4(ha, iface_param, init_fw_cb);
1252                                 break;
1253                         default:
1254                                 /* Cannot have more than one IPv4 interface */
1255                                 ql4_printk(KERN_ERR, ha, "Invalid IPv4 iface "
1256                                            "number = %d\n",
1257                                            iface_param->iface_num);
1258                                 break;
1259                         }
1260                         break;
1261                 case ISCSI_IFACE_TYPE_IPV6:
1262                         switch (iface_param->iface_num) {
1263                         case 0:
1264                         case 1:
1265                                 qla4xxx_set_ipv6(ha, iface_param, init_fw_cb);
1266                                 break;
1267                         default:
1268                                 /* Cannot have more than two IPv6 interface */
1269                                 ql4_printk(KERN_ERR, ha, "Invalid IPv6 iface "
1270                                            "number = %d\n",
1271                                            iface_param->iface_num);
1272                                 break;
1273                         }
1274                         break;
1275                 default:
1276                         ql4_printk(KERN_ERR, ha, "Invalid iface type\n");
1277                         break;
1278                 }
1279         }
1280
1281         init_fw_cb->cookie = cpu_to_le32(0x11BEAD5A);
1282
1283         rval = qla4xxx_set_flash(ha, init_fw_cb_dma, FLASH_SEGMENT_IFCB,
1284                                  sizeof(struct addr_ctrl_blk),
1285                                  FLASH_OPT_RMW_COMMIT);
1286         if (rval != QLA_SUCCESS) {
1287                 ql4_printk(KERN_ERR, ha, "%s: set flash mbx failed\n",
1288                            __func__);
1289                 rval = -EIO;
1290                 goto exit_init_fw_cb;
1291         }
1292
1293         rval = qla4xxx_disable_acb(ha);
1294         if (rval != QLA_SUCCESS) {
1295                 ql4_printk(KERN_ERR, ha, "%s: disable acb mbx failed\n",
1296                            __func__);
1297                 rval = -EIO;
1298                 goto exit_init_fw_cb;
1299         }
1300
1301         wait_for_completion_timeout(&ha->disable_acb_comp,
1302                                     DISABLE_ACB_TOV * HZ);
1303
1304         qla4xxx_initcb_to_acb(init_fw_cb);
1305
1306         rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma);
1307         if (rval != QLA_SUCCESS) {
1308                 ql4_printk(KERN_ERR, ha, "%s: set acb mbx failed\n",
1309                            __func__);
1310                 rval = -EIO;
1311                 goto exit_init_fw_cb;
1312         }
1313
1314         memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
1315         qla4xxx_update_local_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb,
1316                                   init_fw_cb_dma);
1317
1318 exit_init_fw_cb:
1319         dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk),
1320                           init_fw_cb, init_fw_cb_dma);
1321
1322         return rval;
1323 }
1324
1325 static int qla4xxx_session_get_param(struct iscsi_cls_session *cls_sess,
1326                                      enum iscsi_param param, char *buf)
1327 {
1328         struct iscsi_session *sess = cls_sess->dd_data;
1329         struct ddb_entry *ddb_entry = sess->dd_data;
1330         struct scsi_qla_host *ha = ddb_entry->ha;
1331         int rval, len;
1332         uint16_t idx;
1333
1334         switch (param) {
1335         case ISCSI_PARAM_CHAP_IN_IDX:
1336                 rval = qla4xxx_get_chap_index(ha, sess->username_in,
1337                                               sess->password_in, BIDI_CHAP,
1338                                               &idx);
1339                 if (rval)
1340                         len = sprintf(buf, "\n");
1341                 else
1342                         len = sprintf(buf, "%hu\n", idx);
1343                 break;
1344         case ISCSI_PARAM_CHAP_OUT_IDX:
1345                 rval = qla4xxx_get_chap_index(ha, sess->username,
1346                                               sess->password, LOCAL_CHAP,
1347                                               &idx);
1348                 if (rval)
1349                         len = sprintf(buf, "\n");
1350                 else
1351                         len = sprintf(buf, "%hu\n", idx);
1352                 break;
1353         default:
1354                 return iscsi_session_get_param(cls_sess, param, buf);
1355         }
1356
1357         return len;
1358 }
1359
1360 static int qla4xxx_conn_get_param(struct iscsi_cls_conn *cls_conn,
1361                                   enum iscsi_param param, char *buf)
1362 {
1363         struct iscsi_conn *conn;
1364         struct qla_conn *qla_conn;
1365         struct sockaddr *dst_addr;
1366         int len = 0;
1367
1368         conn = cls_conn->dd_data;
1369         qla_conn = conn->dd_data;
1370         dst_addr = (struct sockaddr *)&qla_conn->qla_ep->dst_addr;
1371
1372         switch (param) {
1373         case ISCSI_PARAM_CONN_PORT:
1374         case ISCSI_PARAM_CONN_ADDRESS:
1375                 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
1376                                                  dst_addr, param, buf);
1377         default:
1378                 return iscsi_conn_get_param(cls_conn, param, buf);
1379         }
1380
1381         return len;
1382
1383 }
1384
1385 int qla4xxx_get_ddb_index(struct scsi_qla_host *ha, uint16_t *ddb_index)
1386 {
1387         uint32_t mbx_sts = 0;
1388         uint16_t tmp_ddb_index;
1389         int ret;
1390
1391 get_ddb_index:
1392         tmp_ddb_index = find_first_zero_bit(ha->ddb_idx_map, MAX_DDB_ENTRIES);
1393
1394         if (tmp_ddb_index >= MAX_DDB_ENTRIES) {
1395                 DEBUG2(ql4_printk(KERN_INFO, ha,
1396                                   "Free DDB index not available\n"));
1397                 ret = QLA_ERROR;
1398                 goto exit_get_ddb_index;
1399         }
1400
1401         if (test_and_set_bit(tmp_ddb_index, ha->ddb_idx_map))
1402                 goto get_ddb_index;
1403
1404         DEBUG2(ql4_printk(KERN_INFO, ha,
1405                           "Found a free DDB index at %d\n", tmp_ddb_index));
1406         ret = qla4xxx_req_ddb_entry(ha, tmp_ddb_index, &mbx_sts);
1407         if (ret == QLA_ERROR) {
1408                 if (mbx_sts == MBOX_STS_COMMAND_ERROR) {
1409                         ql4_printk(KERN_INFO, ha,
1410                                    "DDB index = %d not available trying next\n",
1411                                    tmp_ddb_index);
1412                         goto get_ddb_index;
1413                 }
1414                 DEBUG2(ql4_printk(KERN_INFO, ha,
1415                                   "Free FW DDB not available\n"));
1416         }
1417
1418         *ddb_index = tmp_ddb_index;
1419
1420 exit_get_ddb_index:
1421         return ret;
1422 }
1423
1424 static int qla4xxx_match_ipaddress(struct scsi_qla_host *ha,
1425                                    struct ddb_entry *ddb_entry,
1426                                    char *existing_ipaddr,
1427                                    char *user_ipaddr)
1428 {
1429         uint8_t dst_ipaddr[IPv6_ADDR_LEN];
1430         char formatted_ipaddr[DDB_IPADDR_LEN];
1431         int status = QLA_SUCCESS, ret = 0;
1432
1433         if (ddb_entry->fw_ddb_entry.options & DDB_OPT_IPV6_DEVICE) {
1434                 ret = in6_pton(user_ipaddr, strlen(user_ipaddr), dst_ipaddr,
1435                                '\0', NULL);
1436                 if (ret == 0) {
1437                         status = QLA_ERROR;
1438                         goto out_match;
1439                 }
1440                 ret = sprintf(formatted_ipaddr, "%pI6", dst_ipaddr);
1441         } else {
1442                 ret = in4_pton(user_ipaddr, strlen(user_ipaddr), dst_ipaddr,
1443                                '\0', NULL);
1444                 if (ret == 0) {
1445                         status = QLA_ERROR;
1446                         goto out_match;
1447                 }
1448                 ret = sprintf(formatted_ipaddr, "%pI4", dst_ipaddr);
1449         }
1450
1451         if (strcmp(existing_ipaddr, formatted_ipaddr))
1452                 status = QLA_ERROR;
1453
1454 out_match:
1455         return status;
1456 }
1457
1458 static int qla4xxx_match_fwdb_session(struct scsi_qla_host *ha,
1459                                       struct iscsi_cls_conn *cls_conn)
1460 {
1461         int idx = 0, max_ddbs, rval;
1462         struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
1463         struct iscsi_session *sess, *existing_sess;
1464         struct iscsi_conn *conn, *existing_conn;
1465         struct ddb_entry *ddb_entry;
1466
1467         sess = cls_sess->dd_data;
1468         conn = cls_conn->dd_data;
1469
1470         if (sess->targetname == NULL ||
1471             conn->persistent_address == NULL ||
1472             conn->persistent_port == 0)
1473                 return QLA_ERROR;
1474
1475         max_ddbs =  is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
1476                                      MAX_DEV_DB_ENTRIES;
1477
1478         for (idx = 0; idx < max_ddbs; idx++) {
1479                 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
1480                 if (ddb_entry == NULL)
1481                         continue;
1482
1483                 if (ddb_entry->ddb_type != FLASH_DDB)
1484                         continue;
1485
1486                 existing_sess = ddb_entry->sess->dd_data;
1487                 existing_conn = ddb_entry->conn->dd_data;
1488
1489                 if (existing_sess->targetname == NULL ||
1490                     existing_conn->persistent_address == NULL ||
1491                     existing_conn->persistent_port == 0)
1492                         continue;
1493
1494                 DEBUG2(ql4_printk(KERN_INFO, ha,
1495                                   "IQN = %s User IQN = %s\n",
1496                                   existing_sess->targetname,
1497                                   sess->targetname));
1498
1499                 DEBUG2(ql4_printk(KERN_INFO, ha,
1500                                   "IP = %s User IP = %s\n",
1501                                   existing_conn->persistent_address,
1502                                   conn->persistent_address));
1503
1504                 DEBUG2(ql4_printk(KERN_INFO, ha,
1505                                   "Port = %d User Port = %d\n",
1506                                   existing_conn->persistent_port,
1507                                   conn->persistent_port));
1508
1509                 if (strcmp(existing_sess->targetname, sess->targetname))
1510                         continue;
1511                 rval = qla4xxx_match_ipaddress(ha, ddb_entry,
1512                                         existing_conn->persistent_address,
1513                                         conn->persistent_address);
1514                 if (rval == QLA_ERROR)
1515                         continue;
1516                 if (existing_conn->persistent_port != conn->persistent_port)
1517                         continue;
1518                 break;
1519         }
1520
1521         if (idx == max_ddbs)
1522                 return QLA_ERROR;
1523
1524         DEBUG2(ql4_printk(KERN_INFO, ha,
1525                           "Match found in fwdb sessions\n"));
1526         return QLA_SUCCESS;
1527 }
1528
1529 static struct iscsi_cls_session *
1530 qla4xxx_session_create(struct iscsi_endpoint *ep,
1531                         uint16_t cmds_max, uint16_t qdepth,
1532                         uint32_t initial_cmdsn)
1533 {
1534         struct iscsi_cls_session *cls_sess;
1535         struct scsi_qla_host *ha;
1536         struct qla_endpoint *qla_ep;
1537         struct ddb_entry *ddb_entry;
1538         uint16_t ddb_index;
1539         struct iscsi_session *sess;
1540         struct sockaddr *dst_addr;
1541         int ret;
1542
1543         DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1544         if (!ep) {
1545                 printk(KERN_ERR "qla4xxx: missing ep.\n");
1546                 return NULL;
1547         }
1548
1549         qla_ep = ep->dd_data;
1550         dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
1551         ha = to_qla_host(qla_ep->host);
1552
1553         ret = qla4xxx_get_ddb_index(ha, &ddb_index);
1554         if (ret == QLA_ERROR)
1555                 return NULL;
1556
1557         cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport, qla_ep->host,
1558                                        cmds_max, sizeof(struct ddb_entry),
1559                                        sizeof(struct ql4_task_data),
1560                                        initial_cmdsn, ddb_index);
1561         if (!cls_sess)
1562                 return NULL;
1563
1564         sess = cls_sess->dd_data;
1565         ddb_entry = sess->dd_data;
1566         ddb_entry->fw_ddb_index = ddb_index;
1567         ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
1568         ddb_entry->ha = ha;
1569         ddb_entry->sess = cls_sess;
1570         ddb_entry->unblock_sess = qla4xxx_unblock_ddb;
1571         ddb_entry->ddb_change = qla4xxx_ddb_change;
1572         cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
1573         ha->fw_ddb_index_map[ddb_entry->fw_ddb_index] = ddb_entry;
1574         ha->tot_ddbs++;
1575
1576         return cls_sess;
1577 }
1578
1579 static void qla4xxx_session_destroy(struct iscsi_cls_session *cls_sess)
1580 {
1581         struct iscsi_session *sess;
1582         struct ddb_entry *ddb_entry;
1583         struct scsi_qla_host *ha;
1584         unsigned long flags, wtime;
1585         struct dev_db_entry *fw_ddb_entry = NULL;
1586         dma_addr_t fw_ddb_entry_dma;
1587         uint32_t ddb_state;
1588         int ret;
1589
1590         DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1591         sess = cls_sess->dd_data;
1592         ddb_entry = sess->dd_data;
1593         ha = ddb_entry->ha;
1594
1595         fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1596                                           &fw_ddb_entry_dma, GFP_KERNEL);
1597         if (!fw_ddb_entry) {
1598                 ql4_printk(KERN_ERR, ha,
1599                            "%s: Unable to allocate dma buffer\n", __func__);
1600                 goto destroy_session;
1601         }
1602
1603         wtime = jiffies + (HZ * LOGOUT_TOV);
1604         do {
1605                 ret = qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index,
1606                                               fw_ddb_entry, fw_ddb_entry_dma,
1607                                               NULL, NULL, &ddb_state, NULL,
1608                                               NULL, NULL);
1609                 if (ret == QLA_ERROR)
1610                         goto destroy_session;
1611
1612                 if ((ddb_state == DDB_DS_NO_CONNECTION_ACTIVE) ||
1613                     (ddb_state == DDB_DS_SESSION_FAILED))
1614                         goto destroy_session;
1615
1616                 schedule_timeout_uninterruptible(HZ);
1617         } while ((time_after(wtime, jiffies)));
1618
1619 destroy_session:
1620         qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
1621
1622         spin_lock_irqsave(&ha->hardware_lock, flags);
1623         qla4xxx_free_ddb(ha, ddb_entry);
1624         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1625
1626         iscsi_session_teardown(cls_sess);
1627
1628         if (fw_ddb_entry)
1629                 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1630                                   fw_ddb_entry, fw_ddb_entry_dma);
1631 }
1632
1633 static struct iscsi_cls_conn *
1634 qla4xxx_conn_create(struct iscsi_cls_session *cls_sess, uint32_t conn_idx)
1635 {
1636         struct iscsi_cls_conn *cls_conn;
1637         struct iscsi_session *sess;
1638         struct ddb_entry *ddb_entry;
1639
1640         DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1641         cls_conn = iscsi_conn_setup(cls_sess, sizeof(struct qla_conn),
1642                                     conn_idx);
1643         if (!cls_conn)
1644                 return NULL;
1645
1646         sess = cls_sess->dd_data;
1647         ddb_entry = sess->dd_data;
1648         ddb_entry->conn = cls_conn;
1649
1650         return cls_conn;
1651 }
1652
1653 static int qla4xxx_conn_bind(struct iscsi_cls_session *cls_session,
1654                              struct iscsi_cls_conn *cls_conn,
1655                              uint64_t transport_fd, int is_leading)
1656 {
1657         struct iscsi_conn *conn;
1658         struct qla_conn *qla_conn;
1659         struct iscsi_endpoint *ep;
1660
1661         DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1662
1663         if (iscsi_conn_bind(cls_session, cls_conn, is_leading))
1664                 return -EINVAL;
1665         ep = iscsi_lookup_endpoint(transport_fd);
1666         conn = cls_conn->dd_data;
1667         qla_conn = conn->dd_data;
1668         qla_conn->qla_ep = ep->dd_data;
1669         return 0;
1670 }
1671
1672 static int qla4xxx_conn_start(struct iscsi_cls_conn *cls_conn)
1673 {
1674         struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
1675         struct iscsi_session *sess;
1676         struct ddb_entry *ddb_entry;
1677         struct scsi_qla_host *ha;
1678         struct dev_db_entry *fw_ddb_entry = NULL;
1679         dma_addr_t fw_ddb_entry_dma;
1680         uint32_t mbx_sts = 0;
1681         int ret = 0;
1682         int status = QLA_SUCCESS;
1683
1684         DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1685         sess = cls_sess->dd_data;
1686         ddb_entry = sess->dd_data;
1687         ha = ddb_entry->ha;
1688
1689         /* Check if we have  matching FW DDB, if yes then do not
1690          * login to this target. This could cause target to logout previous
1691          * connection
1692          */
1693         ret = qla4xxx_match_fwdb_session(ha, cls_conn);
1694         if (ret == QLA_SUCCESS) {
1695                 ql4_printk(KERN_INFO, ha,
1696                            "Session already exist in FW.\n");
1697                 ret = -EEXIST;
1698                 goto exit_conn_start;
1699         }
1700
1701         fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1702                                           &fw_ddb_entry_dma, GFP_KERNEL);
1703         if (!fw_ddb_entry) {
1704                 ql4_printk(KERN_ERR, ha,
1705                            "%s: Unable to allocate dma buffer\n", __func__);
1706                 ret = -ENOMEM;
1707                 goto exit_conn_start;
1708         }
1709
1710         ret = qla4xxx_set_param_ddbentry(ha, ddb_entry, cls_conn, &mbx_sts);
1711         if (ret) {
1712                 /* If iscsid is stopped and started then no need to do
1713                 * set param again since ddb state will be already
1714                 * active and FW does not allow set ddb to an
1715                 * active session.
1716                 */
1717                 if (mbx_sts)
1718                         if (ddb_entry->fw_ddb_device_state ==
1719                                                 DDB_DS_SESSION_ACTIVE) {
1720                                 ddb_entry->unblock_sess(ddb_entry->sess);
1721                                 goto exit_set_param;
1722                         }
1723
1724                 ql4_printk(KERN_ERR, ha, "%s: Failed set param for index[%d]\n",
1725                            __func__, ddb_entry->fw_ddb_index);
1726                 goto exit_conn_start;
1727         }
1728
1729         status = qla4xxx_conn_open(ha, ddb_entry->fw_ddb_index);
1730         if (status == QLA_ERROR) {
1731                 ql4_printk(KERN_ERR, ha, "%s: Login failed: %s\n", __func__,
1732                            sess->targetname);
1733                 ret = -EINVAL;
1734                 goto exit_conn_start;
1735         }
1736
1737         if (ddb_entry->fw_ddb_device_state == DDB_DS_NO_CONNECTION_ACTIVE)
1738                 ddb_entry->fw_ddb_device_state = DDB_DS_LOGIN_IN_PROCESS;
1739
1740         DEBUG2(printk(KERN_INFO "%s: DDB state [%d]\n", __func__,
1741                       ddb_entry->fw_ddb_device_state));
1742
1743 exit_set_param:
1744         ret = 0;
1745
1746 exit_conn_start:
1747         if (fw_ddb_entry)
1748                 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1749                                   fw_ddb_entry, fw_ddb_entry_dma);
1750         return ret;
1751 }
1752
1753 static void qla4xxx_conn_destroy(struct iscsi_cls_conn *cls_conn)
1754 {
1755         struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
1756         struct iscsi_session *sess;
1757         struct scsi_qla_host *ha;
1758         struct ddb_entry *ddb_entry;
1759         int options;
1760
1761         DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1762         sess = cls_sess->dd_data;
1763         ddb_entry = sess->dd_data;
1764         ha = ddb_entry->ha;
1765
1766         options = LOGOUT_OPTION_CLOSE_SESSION;
1767         if (qla4xxx_session_logout_ddb(ha, ddb_entry, options) == QLA_ERROR)
1768                 ql4_printk(KERN_ERR, ha, "%s: Logout failed\n", __func__);
1769 }
1770
1771 static void qla4xxx_task_work(struct work_struct *wdata)
1772 {
1773         struct ql4_task_data *task_data;
1774         struct scsi_qla_host *ha;
1775         struct passthru_status *sts;
1776         struct iscsi_task *task;
1777         struct iscsi_hdr *hdr;
1778         uint8_t *data;
1779         uint32_t data_len;
1780         struct iscsi_conn *conn;
1781         int hdr_len;
1782         itt_t itt;
1783
1784         task_data = container_of(wdata, struct ql4_task_data, task_work);
1785         ha = task_data->ha;
1786         task = task_data->task;
1787         sts = &task_data->sts;
1788         hdr_len = sizeof(struct iscsi_hdr);
1789
1790         DEBUG3(printk(KERN_INFO "Status returned\n"));
1791         DEBUG3(qla4xxx_dump_buffer(sts, 64));
1792         DEBUG3(printk(KERN_INFO "Response buffer"));
1793         DEBUG3(qla4xxx_dump_buffer(task_data->resp_buffer, 64));
1794
1795         conn = task->conn;
1796
1797         switch (sts->completionStatus) {
1798         case PASSTHRU_STATUS_COMPLETE:
1799                 hdr = (struct iscsi_hdr *)task_data->resp_buffer;
1800                 /* Assign back the itt in hdr, until we use the PREASSIGN_TAG */
1801                 itt = sts->handle;
1802                 hdr->itt = itt;
1803                 data = task_data->resp_buffer + hdr_len;
1804                 data_len = task_data->resp_len - hdr_len;
1805                 iscsi_complete_pdu(conn, hdr, data, data_len);
1806                 break;
1807         default:
1808                 ql4_printk(KERN_ERR, ha, "Passthru failed status = 0x%x\n",
1809                            sts->completionStatus);
1810                 break;
1811         }
1812         return;
1813 }
1814
1815 static int qla4xxx_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
1816 {
1817         struct ql4_task_data *task_data;
1818         struct iscsi_session *sess;
1819         struct ddb_entry *ddb_entry;
1820         struct scsi_qla_host *ha;
1821         int hdr_len;
1822
1823         sess = task->conn->session;
1824         ddb_entry = sess->dd_data;
1825         ha = ddb_entry->ha;
1826         task_data = task->dd_data;
1827         memset(task_data, 0, sizeof(struct ql4_task_data));
1828
1829         if (task->sc) {
1830                 ql4_printk(KERN_INFO, ha,
1831                            "%s: SCSI Commands not implemented\n", __func__);
1832                 return -EINVAL;
1833         }
1834
1835         hdr_len = sizeof(struct iscsi_hdr);
1836         task_data->ha = ha;
1837         task_data->task = task;
1838
1839         if (task->data_count) {
1840                 task_data->data_dma = dma_map_single(&ha->pdev->dev, task->data,
1841                                                      task->data_count,
1842                                                      PCI_DMA_TODEVICE);
1843         }
1844
1845         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
1846                       __func__, task->conn->max_recv_dlength, hdr_len));
1847
1848         task_data->resp_len = task->conn->max_recv_dlength + hdr_len;
1849         task_data->resp_buffer = dma_alloc_coherent(&ha->pdev->dev,
1850                                                     task_data->resp_len,
1851                                                     &task_data->resp_dma,
1852                                                     GFP_ATOMIC);
1853         if (!task_data->resp_buffer)
1854                 goto exit_alloc_pdu;
1855
1856         task_data->req_len = task->data_count + hdr_len;
1857         task_data->req_buffer = dma_alloc_coherent(&ha->pdev->dev,
1858                                                    task_data->req_len,
1859                                                    &task_data->req_dma,
1860                                                    GFP_ATOMIC);
1861         if (!task_data->req_buffer)
1862                 goto exit_alloc_pdu;
1863
1864         task->hdr = task_data->req_buffer;
1865
1866         INIT_WORK(&task_data->task_work, qla4xxx_task_work);
1867
1868         return 0;
1869
1870 exit_alloc_pdu:
1871         if (task_data->resp_buffer)
1872                 dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
1873                                   task_data->resp_buffer, task_data->resp_dma);
1874
1875         if (task_data->req_buffer)
1876                 dma_free_coherent(&ha->pdev->dev, task_data->req_len,
1877                                   task_data->req_buffer, task_data->req_dma);
1878         return -ENOMEM;
1879 }
1880
1881 static void qla4xxx_task_cleanup(struct iscsi_task *task)
1882 {
1883         struct ql4_task_data *task_data;
1884         struct iscsi_session *sess;
1885         struct ddb_entry *ddb_entry;
1886         struct scsi_qla_host *ha;
1887         int hdr_len;
1888
1889         hdr_len = sizeof(struct iscsi_hdr);
1890         sess = task->conn->session;
1891         ddb_entry = sess->dd_data;
1892         ha = ddb_entry->ha;
1893         task_data = task->dd_data;
1894
1895         if (task->data_count) {
1896                 dma_unmap_single(&ha->pdev->dev, task_data->data_dma,
1897                                  task->data_count, PCI_DMA_TODEVICE);
1898         }
1899
1900         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
1901                       __func__, task->conn->max_recv_dlength, hdr_len));
1902
1903         dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
1904                           task_data->resp_buffer, task_data->resp_dma);
1905         dma_free_coherent(&ha->pdev->dev, task_data->req_len,
1906                           task_data->req_buffer, task_data->req_dma);
1907         return;
1908 }
1909
1910 static int qla4xxx_task_xmit(struct iscsi_task *task)
1911 {
1912         struct scsi_cmnd *sc = task->sc;
1913         struct iscsi_session *sess = task->conn->session;
1914         struct ddb_entry *ddb_entry = sess->dd_data;
1915         struct scsi_qla_host *ha = ddb_entry->ha;
1916
1917         if (!sc)
1918                 return qla4xxx_send_passthru0(task);
1919
1920         ql4_printk(KERN_INFO, ha, "%s: scsi cmd xmit not implemented\n",
1921                    __func__);
1922         return -ENOSYS;
1923 }
1924
1925 static void qla4xxx_copy_fwddb_param(struct scsi_qla_host *ha,
1926                                      struct dev_db_entry *fw_ddb_entry,
1927                                      struct iscsi_cls_session *cls_sess,
1928                                      struct iscsi_cls_conn *cls_conn)
1929 {
1930         int buflen = 0;
1931         struct iscsi_session *sess;
1932         struct ddb_entry *ddb_entry;
1933         struct iscsi_conn *conn;
1934         char ip_addr[DDB_IPADDR_LEN];
1935         uint16_t options = 0;
1936
1937         sess = cls_sess->dd_data;
1938         ddb_entry = sess->dd_data;
1939         conn = cls_conn->dd_data;
1940
1941         ddb_entry->chap_tbl_idx = le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
1942
1943         conn->max_recv_dlength = BYTE_UNITS *
1944                           le16_to_cpu(fw_ddb_entry->iscsi_max_rcv_data_seg_len);
1945
1946         conn->max_xmit_dlength = BYTE_UNITS *
1947                           le16_to_cpu(fw_ddb_entry->iscsi_max_snd_data_seg_len);
1948
1949         sess->initial_r2t_en =
1950                             (BIT_10 & le16_to_cpu(fw_ddb_entry->iscsi_options));
1951
1952         sess->max_r2t = le16_to_cpu(fw_ddb_entry->iscsi_max_outsnd_r2t);
1953
1954         sess->imm_data_en = (BIT_11 & le16_to_cpu(fw_ddb_entry->iscsi_options));
1955
1956         sess->first_burst = BYTE_UNITS *
1957                                le16_to_cpu(fw_ddb_entry->iscsi_first_burst_len);
1958
1959         sess->max_burst = BYTE_UNITS *
1960                                  le16_to_cpu(fw_ddb_entry->iscsi_max_burst_len);
1961
1962         sess->time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
1963
1964         sess->time2retain = le16_to_cpu(fw_ddb_entry->iscsi_def_time2retain);
1965
1966         conn->persistent_port = le16_to_cpu(fw_ddb_entry->port);
1967
1968         sess->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
1969
1970         options = le16_to_cpu(fw_ddb_entry->options);
1971         if (options & DDB_OPT_IPV6_DEVICE)
1972                 sprintf(ip_addr, "%pI6", fw_ddb_entry->ip_addr);
1973         else
1974                 sprintf(ip_addr, "%pI4", fw_ddb_entry->ip_addr);
1975
1976         iscsi_set_param(cls_conn, ISCSI_PARAM_TARGET_NAME,
1977                         (char *)fw_ddb_entry->iscsi_name, buflen);
1978         iscsi_set_param(cls_conn, ISCSI_PARAM_INITIATOR_NAME,
1979                         (char *)ha->name_string, buflen);
1980         iscsi_set_param(cls_conn, ISCSI_PARAM_PERSISTENT_ADDRESS,
1981                         (char *)ip_addr, buflen);
1982         iscsi_set_param(cls_conn, ISCSI_PARAM_TARGET_ALIAS,
1983                         (char *)fw_ddb_entry->iscsi_alias, buflen);
1984 }
1985
1986 void qla4xxx_update_session_conn_fwddb_param(struct scsi_qla_host *ha,
1987                                              struct ddb_entry *ddb_entry)
1988 {
1989         struct iscsi_cls_session *cls_sess;
1990         struct iscsi_cls_conn *cls_conn;
1991         uint32_t ddb_state;
1992         dma_addr_t fw_ddb_entry_dma;
1993         struct dev_db_entry *fw_ddb_entry;
1994
1995         fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1996                                           &fw_ddb_entry_dma, GFP_KERNEL);
1997         if (!fw_ddb_entry) {
1998                 ql4_printk(KERN_ERR, ha,
1999                            "%s: Unable to allocate dma buffer\n", __func__);
2000                 goto exit_session_conn_fwddb_param;
2001         }
2002
2003         if (qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index, fw_ddb_entry,
2004                                     fw_ddb_entry_dma, NULL, NULL, &ddb_state,
2005                                     NULL, NULL, NULL) == QLA_ERROR) {
2006                 DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: failed "
2007                                   "get_ddb_entry for fw_ddb_index %d\n",
2008                                   ha->host_no, __func__,
2009                                   ddb_entry->fw_ddb_index));
2010                 goto exit_session_conn_fwddb_param;
2011         }
2012
2013         cls_sess = ddb_entry->sess;
2014
2015         cls_conn = ddb_entry->conn;
2016
2017         /* Update params */
2018         qla4xxx_copy_fwddb_param(ha, fw_ddb_entry, cls_sess, cls_conn);
2019
2020 exit_session_conn_fwddb_param:
2021         if (fw_ddb_entry)
2022                 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
2023                                   fw_ddb_entry, fw_ddb_entry_dma);
2024 }
2025
2026 void qla4xxx_update_session_conn_param(struct scsi_qla_host *ha,
2027                                        struct ddb_entry *ddb_entry)
2028 {
2029         struct iscsi_cls_session *cls_sess;
2030         struct iscsi_cls_conn *cls_conn;
2031         struct iscsi_session *sess;
2032         struct iscsi_conn *conn;
2033         uint32_t ddb_state;
2034         dma_addr_t fw_ddb_entry_dma;
2035         struct dev_db_entry *fw_ddb_entry;
2036
2037         fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
2038                                           &fw_ddb_entry_dma, GFP_KERNEL);
2039         if (!fw_ddb_entry) {
2040                 ql4_printk(KERN_ERR, ha,
2041                            "%s: Unable to allocate dma buffer\n", __func__);
2042                 goto exit_session_conn_param;
2043         }
2044
2045         if (qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index, fw_ddb_entry,
2046                                     fw_ddb_entry_dma, NULL, NULL, &ddb_state,
2047                                     NULL, NULL, NULL) == QLA_ERROR) {
2048                 DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: failed "
2049                                   "get_ddb_entry for fw_ddb_index %d\n",
2050                                   ha->host_no, __func__,
2051                                   ddb_entry->fw_ddb_index));
2052                 goto exit_session_conn_param;
2053         }
2054
2055         cls_sess = ddb_entry->sess;
2056         sess = cls_sess->dd_data;
2057
2058         cls_conn = ddb_entry->conn;
2059         conn = cls_conn->dd_data;
2060
2061         /* Update timers after login */
2062         ddb_entry->default_relogin_timeout =
2063                 (le16_to_cpu(fw_ddb_entry->def_timeout) > LOGIN_TOV) &&
2064                  (le16_to_cpu(fw_ddb_entry->def_timeout) < LOGIN_TOV * 10) ?
2065                  le16_to_cpu(fw_ddb_entry->def_timeout) : LOGIN_TOV;
2066         ddb_entry->default_time2wait =
2067                                 le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
2068
2069         /* Update params */
2070         ddb_entry->chap_tbl_idx = le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
2071         conn->max_recv_dlength = BYTE_UNITS *
2072                           le16_to_cpu(fw_ddb_entry->iscsi_max_rcv_data_seg_len);
2073
2074         conn->max_xmit_dlength = BYTE_UNITS *
2075                           le16_to_cpu(fw_ddb_entry->iscsi_max_snd_data_seg_len);
2076
2077         sess->initial_r2t_en =
2078                             (BIT_10 & le16_to_cpu(fw_ddb_entry->iscsi_options));
2079
2080         sess->max_r2t = le16_to_cpu(fw_ddb_entry->iscsi_max_outsnd_r2t);
2081
2082         sess->imm_data_en = (BIT_11 & le16_to_cpu(fw_ddb_entry->iscsi_options));
2083
2084         sess->first_burst = BYTE_UNITS *
2085                                le16_to_cpu(fw_ddb_entry->iscsi_first_burst_len);
2086
2087         sess->max_burst = BYTE_UNITS *
2088                                  le16_to_cpu(fw_ddb_entry->iscsi_max_burst_len);
2089
2090         sess->time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
2091
2092         sess->time2retain = le16_to_cpu(fw_ddb_entry->iscsi_def_time2retain);
2093
2094         sess->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
2095
2096         memcpy(sess->initiatorname, ha->name_string,
2097                min(sizeof(ha->name_string), sizeof(sess->initiatorname)));
2098
2099         iscsi_set_param(cls_conn, ISCSI_PARAM_TARGET_ALIAS,
2100                         (char *)fw_ddb_entry->iscsi_alias, 0);
2101
2102 exit_session_conn_param:
2103         if (fw_ddb_entry)
2104                 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
2105                                   fw_ddb_entry, fw_ddb_entry_dma);
2106 }
2107
2108 /*
2109  * Timer routines
2110  */
2111
2112 static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
2113                                 unsigned long interval)
2114 {
2115         DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
2116                      __func__, ha->host->host_no));
2117         init_timer(&ha->timer);
2118         ha->timer.expires = jiffies + interval * HZ;
2119         ha->timer.data = (unsigned long)ha;
2120         ha->timer.function = (void (*)(unsigned long))func;
2121         add_timer(&ha->timer);
2122         ha->timer_active = 1;
2123 }
2124
2125 static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
2126 {
2127         del_timer_sync(&ha->timer);
2128         ha->timer_active = 0;
2129 }
2130
2131 /***
2132  * qla4xxx_mark_device_missing - blocks the session
2133  * @cls_session: Pointer to the session to be blocked
2134  * @ddb_entry: Pointer to device database entry
2135  *
2136  * This routine marks a device missing and close connection.
2137  **/
2138 void qla4xxx_mark_device_missing(struct iscsi_cls_session *cls_session)
2139 {
2140         iscsi_block_session(cls_session);
2141 }
2142
2143 /**
2144  * qla4xxx_mark_all_devices_missing - mark all devices as missing.
2145  * @ha: Pointer to host adapter structure.
2146  *
2147  * This routine marks a device missing and resets the relogin retry count.
2148  **/
2149 void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
2150 {
2151         iscsi_host_for_each_session(ha->host, qla4xxx_mark_device_missing);
2152 }
2153
2154 static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
2155                                        struct ddb_entry *ddb_entry,
2156                                        struct scsi_cmnd *cmd)
2157 {
2158         struct srb *srb;
2159
2160         srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
2161         if (!srb)
2162                 return srb;
2163
2164         kref_init(&srb->srb_ref);
2165         srb->ha = ha;
2166         srb->ddb = ddb_entry;
2167         srb->cmd = cmd;
2168         srb->flags = 0;
2169         CMD_SP(cmd) = (void *)srb;
2170
2171         return srb;
2172 }
2173
2174 static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
2175 {
2176         struct scsi_cmnd *cmd = srb->cmd;
2177
2178         if (srb->flags & SRB_DMA_VALID) {
2179                 scsi_dma_unmap(cmd);
2180                 srb->flags &= ~SRB_DMA_VALID;
2181         }
2182         CMD_SP(cmd) = NULL;
2183 }
2184
2185 void qla4xxx_srb_compl(struct kref *ref)
2186 {
2187         struct srb *srb = container_of(ref, struct srb, srb_ref);
2188         struct scsi_cmnd *cmd = srb->cmd;
2189         struct scsi_qla_host *ha = srb->ha;
2190
2191         qla4xxx_srb_free_dma(ha, srb);
2192
2193         mempool_free(srb, ha->srb_mempool);
2194
2195         cmd->scsi_done(cmd);
2196 }
2197
2198 /**
2199  * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
2200  * @host: scsi host
2201  * @cmd: Pointer to Linux's SCSI command structure
2202  *
2203  * Remarks:
2204  * This routine is invoked by Linux to send a SCSI command to the driver.
2205  * The mid-level driver tries to ensure that queuecommand never gets
2206  * invoked concurrently with itself or the interrupt handler (although
2207  * the interrupt handler may call this routine as part of request-
2208  * completion handling).   Unfortunely, it sometimes calls the scheduler
2209  * in interrupt context which is a big NO! NO!.
2210  **/
2211 static int qla4xxx_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
2212 {
2213         struct scsi_qla_host *ha = to_qla_host(host);
2214         struct ddb_entry *ddb_entry = cmd->device->hostdata;
2215         struct iscsi_cls_session *sess = ddb_entry->sess;
2216         struct srb *srb;
2217         int rval;
2218
2219         if (test_bit(AF_EEH_BUSY, &ha->flags)) {
2220                 if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
2221                         cmd->result = DID_NO_CONNECT << 16;
2222                 else
2223                         cmd->result = DID_REQUEUE << 16;
2224                 goto qc_fail_command;
2225         }
2226
2227         if (!sess) {
2228                 cmd->result = DID_IMM_RETRY << 16;
2229                 goto qc_fail_command;
2230         }
2231
2232         rval = iscsi_session_chkready(sess);
2233         if (rval) {
2234                 cmd->result = rval;
2235                 goto qc_fail_command;
2236         }
2237
2238         if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
2239             test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
2240             test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
2241             test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
2242             test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
2243             !test_bit(AF_ONLINE, &ha->flags) ||
2244             !test_bit(AF_LINK_UP, &ha->flags) ||
2245             test_bit(AF_LOOPBACK, &ha->flags) ||
2246             test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
2247                 goto qc_host_busy;
2248
2249         srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd);
2250         if (!srb)
2251                 goto qc_host_busy;
2252
2253         rval = qla4xxx_send_command_to_isp(ha, srb);
2254         if (rval != QLA_SUCCESS)
2255                 goto qc_host_busy_free_sp;
2256
2257         return 0;
2258
2259 qc_host_busy_free_sp:
2260         qla4xxx_srb_free_dma(ha, srb);
2261         mempool_free(srb, ha->srb_mempool);
2262
2263 qc_host_busy:
2264         return SCSI_MLQUEUE_HOST_BUSY;
2265
2266 qc_fail_command:
2267         cmd->scsi_done(cmd);
2268
2269         return 0;
2270 }
2271
2272 /**
2273  * qla4xxx_mem_free - frees memory allocated to adapter
2274  * @ha: Pointer to host adapter structure.
2275  *
2276  * Frees memory previously allocated by qla4xxx_mem_alloc
2277  **/
2278 static void qla4xxx_mem_free(struct scsi_qla_host *ha)
2279 {
2280         if (ha->queues)
2281                 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
2282                                   ha->queues_dma);
2283
2284          if (ha->fw_dump)
2285                 vfree(ha->fw_dump);
2286
2287         ha->queues_len = 0;
2288         ha->queues = NULL;
2289         ha->queues_dma = 0;
2290         ha->request_ring = NULL;
2291         ha->request_dma = 0;
2292         ha->response_ring = NULL;
2293         ha->response_dma = 0;
2294         ha->shadow_regs = NULL;
2295         ha->shadow_regs_dma = 0;
2296         ha->fw_dump = NULL;
2297         ha->fw_dump_size = 0;
2298
2299         /* Free srb pool. */
2300         if (ha->srb_mempool)
2301                 mempool_destroy(ha->srb_mempool);
2302
2303         ha->srb_mempool = NULL;
2304
2305         if (ha->chap_dma_pool)
2306                 dma_pool_destroy(ha->chap_dma_pool);
2307
2308         if (ha->chap_list)
2309                 vfree(ha->chap_list);
2310         ha->chap_list = NULL;
2311
2312         if (ha->fw_ddb_dma_pool)
2313                 dma_pool_destroy(ha->fw_ddb_dma_pool);
2314
2315         /* release io space registers  */
2316         if (is_qla8022(ha)) {
2317                 if (ha->nx_pcibase)
2318                         iounmap(
2319                             (struct device_reg_82xx __iomem *)ha->nx_pcibase);
2320         } else if (is_qla8032(ha)) {
2321                 if (ha->nx_pcibase)
2322                         iounmap(
2323                             (struct device_reg_83xx __iomem *)ha->nx_pcibase);
2324         } else if (ha->reg) {
2325                 iounmap(ha->reg);
2326         }
2327
2328         if (ha->reset_tmplt.buff)
2329                 vfree(ha->reset_tmplt.buff);
2330
2331         pci_release_regions(ha->pdev);
2332 }
2333
2334 /**
2335  * qla4xxx_mem_alloc - allocates memory for use by adapter.
2336  * @ha: Pointer to host adapter structure
2337  *
2338  * Allocates DMA memory for request and response queues. Also allocates memory
2339  * for srbs.
2340  **/
2341 static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
2342 {
2343         unsigned long align;
2344
2345         /* Allocate contiguous block of DMA memory for queues. */
2346         ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
2347                           (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
2348                           sizeof(struct shadow_regs) +
2349                           MEM_ALIGN_VALUE +
2350                           (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
2351         ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
2352                                         &ha->queues_dma, GFP_KERNEL);
2353         if (ha->queues == NULL) {
2354                 ql4_printk(KERN_WARNING, ha,
2355                     "Memory Allocation failed - queues.\n");
2356
2357                 goto mem_alloc_error_exit;
2358         }
2359         memset(ha->queues, 0, ha->queues_len);
2360
2361         /*
2362          * As per RISC alignment requirements -- the bus-address must be a
2363          * multiple of the request-ring size (in bytes).
2364          */
2365         align = 0;
2366         if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
2367                 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
2368                                            (MEM_ALIGN_VALUE - 1));
2369
2370         /* Update request and response queue pointers. */
2371         ha->request_dma = ha->queues_dma + align;
2372         ha->request_ring = (struct queue_entry *) (ha->queues + align);
2373         ha->response_dma = ha->queues_dma + align +
2374                 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
2375         ha->response_ring = (struct queue_entry *) (ha->queues + align +
2376                                                     (REQUEST_QUEUE_DEPTH *
2377                                                      QUEUE_SIZE));
2378         ha->shadow_regs_dma = ha->queues_dma + align +
2379                 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
2380                 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
2381         ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
2382                                                   (REQUEST_QUEUE_DEPTH *
2383                                                    QUEUE_SIZE) +
2384                                                   (RESPONSE_QUEUE_DEPTH *
2385                                                    QUEUE_SIZE));
2386
2387         /* Allocate memory for srb pool. */
2388         ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
2389                                          mempool_free_slab, srb_cachep);
2390         if (ha->srb_mempool == NULL) {
2391                 ql4_printk(KERN_WARNING, ha,
2392                     "Memory Allocation failed - SRB Pool.\n");
2393
2394                 goto mem_alloc_error_exit;
2395         }
2396
2397         ha->chap_dma_pool = dma_pool_create("ql4_chap", &ha->pdev->dev,
2398                                             CHAP_DMA_BLOCK_SIZE, 8, 0);
2399
2400         if (ha->chap_dma_pool == NULL) {
2401                 ql4_printk(KERN_WARNING, ha,
2402                     "%s: chap_dma_pool allocation failed..\n", __func__);
2403                 goto mem_alloc_error_exit;
2404         }
2405
2406         ha->fw_ddb_dma_pool = dma_pool_create("ql4_fw_ddb", &ha->pdev->dev,
2407                                               DDB_DMA_BLOCK_SIZE, 8, 0);
2408
2409         if (ha->fw_ddb_dma_pool == NULL) {
2410                 ql4_printk(KERN_WARNING, ha,
2411                            "%s: fw_ddb_dma_pool allocation failed..\n",
2412                            __func__);
2413                 goto mem_alloc_error_exit;
2414         }
2415
2416         return QLA_SUCCESS;
2417
2418 mem_alloc_error_exit:
2419         qla4xxx_mem_free(ha);
2420         return QLA_ERROR;
2421 }
2422
2423 /**
2424  * qla4_8xxx_check_temp - Check the ISP82XX temperature.
2425  * @ha: adapter block pointer.
2426  *
2427  * Note: The caller should not hold the idc lock.
2428  **/
2429 static int qla4_8xxx_check_temp(struct scsi_qla_host *ha)
2430 {
2431         uint32_t temp, temp_state, temp_val;
2432         int status = QLA_SUCCESS;
2433
2434         temp = qla4_8xxx_rd_direct(ha, QLA8XXX_CRB_TEMP_STATE);
2435
2436         temp_state = qla82xx_get_temp_state(temp);
2437         temp_val = qla82xx_get_temp_val(temp);
2438
2439         if (temp_state == QLA82XX_TEMP_PANIC) {
2440                 ql4_printk(KERN_WARNING, ha, "Device temperature %d degrees C"
2441                            " exceeds maximum allowed. Hardware has been shut"
2442                            " down.\n", temp_val);
2443                 status = QLA_ERROR;
2444         } else if (temp_state == QLA82XX_TEMP_WARN) {
2445                 if (ha->temperature == QLA82XX_TEMP_NORMAL)
2446                         ql4_printk(KERN_WARNING, ha, "Device temperature %d"
2447                                    " degrees C exceeds operating range."
2448                                    " Immediate action needed.\n", temp_val);
2449         } else {
2450                 if (ha->temperature == QLA82XX_TEMP_WARN)
2451                         ql4_printk(KERN_INFO, ha, "Device temperature is"
2452                                    " now %d degrees C in normal range.\n",
2453                                    temp_val);
2454         }
2455         ha->temperature = temp_state;
2456         return status;
2457 }
2458
2459 /**
2460  * qla4_8xxx_check_fw_alive  - Check firmware health
2461  * @ha: Pointer to host adapter structure.
2462  *
2463  * Context: Interrupt
2464  **/
2465 static int qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
2466 {
2467         uint32_t fw_heartbeat_counter;
2468         int status = QLA_SUCCESS;
2469
2470         fw_heartbeat_counter = qla4_8xxx_rd_direct(ha,
2471                                                    QLA8XXX_PEG_ALIVE_COUNTER);
2472         /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
2473         if (fw_heartbeat_counter == 0xffffffff) {
2474                 DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
2475                     "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
2476                     ha->host_no, __func__));
2477                 return status;
2478         }
2479
2480         if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
2481                 ha->seconds_since_last_heartbeat++;
2482                 /* FW not alive after 2 seconds */
2483                 if (ha->seconds_since_last_heartbeat == 2) {
2484                         ha->seconds_since_last_heartbeat = 0;
2485                         qla4_8xxx_dump_peg_reg(ha);
2486                         status = QLA_ERROR;
2487                 }
2488         } else
2489                 ha->seconds_since_last_heartbeat = 0;
2490
2491         ha->fw_heartbeat_counter = fw_heartbeat_counter;
2492         return status;
2493 }
2494
2495 static void qla4_8xxx_process_fw_error(struct scsi_qla_host *ha)
2496 {
2497         uint32_t halt_status;
2498         int halt_status_unrecoverable = 0;
2499
2500         halt_status = qla4_8xxx_rd_direct(ha, QLA8XXX_PEG_HALT_STATUS1);
2501
2502         if (is_qla8022(ha)) {
2503                 ql4_printk(KERN_INFO, ha, "%s: disabling pause transmit on port 0 & 1.\n",
2504                            __func__);
2505                 qla4_82xx_wr_32(ha, QLA82XX_CRB_NIU + 0x98,
2506                                 CRB_NIU_XG_PAUSE_CTL_P0 |
2507                                 CRB_NIU_XG_PAUSE_CTL_P1);
2508
2509                 if (QLA82XX_FWERROR_CODE(halt_status) == 0x67)
2510                         ql4_printk(KERN_ERR, ha, "%s: Firmware aborted with error code 0x00006700. Device is being reset\n",
2511                                    __func__);
2512                 if (halt_status & HALT_STATUS_UNRECOVERABLE)
2513                         halt_status_unrecoverable = 1;
2514         } else if (is_qla8032(ha)) {
2515                 if (halt_status & QLA83XX_HALT_STATUS_FW_RESET)
2516                         ql4_printk(KERN_ERR, ha, "%s: Firmware error detected device is being reset\n",
2517                                    __func__);
2518                 else if (halt_status & QLA83XX_HALT_STATUS_UNRECOVERABLE)
2519                         halt_status_unrecoverable = 1;
2520         }
2521
2522         /*
2523          * Since we cannot change dev_state in interrupt context,
2524          * set appropriate DPC flag then wakeup DPC
2525          */
2526         if (halt_status_unrecoverable) {
2527                 set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
2528         } else {
2529                 ql4_printk(KERN_INFO, ha, "%s: detect abort needed!\n",
2530                            __func__);
2531                 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2532         }
2533         qla4xxx_mailbox_premature_completion(ha);
2534         qla4xxx_wake_dpc(ha);
2535 }
2536
2537 /**
2538  * qla4_8xxx_watchdog - Poll dev state
2539  * @ha: Pointer to host adapter structure.
2540  *
2541  * Context: Interrupt
2542  **/
2543 void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
2544 {
2545         uint32_t dev_state;
2546
2547         /* don't poll if reset is going on */
2548         if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
2549             test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
2550             test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags))) {
2551                 dev_state = qla4_8xxx_rd_direct(ha, QLA8XXX_CRB_DEV_STATE);
2552
2553                 if (qla4_8xxx_check_temp(ha)) {
2554                         if (is_qla8022(ha)) {
2555                                 ql4_printk(KERN_INFO, ha, "disabling pause transmit on port 0 & 1.\n");
2556                                 qla4_82xx_wr_32(ha, QLA82XX_CRB_NIU + 0x98,
2557                                                 CRB_NIU_XG_PAUSE_CTL_P0 |
2558                                                 CRB_NIU_XG_PAUSE_CTL_P1);
2559                         }
2560                         set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
2561                         qla4xxx_wake_dpc(ha);
2562                 } else if (dev_state == QLA8XXX_DEV_NEED_RESET &&
2563                            !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
2564                         if (is_qla8032(ha) ||
2565                             (is_qla8022(ha) && !ql4xdontresethba)) {
2566                                 ql4_printk(KERN_INFO, ha, "%s: HW State: "
2567                                     "NEED RESET!\n", __func__);
2568                                 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2569                                 qla4xxx_wake_dpc(ha);
2570                         }
2571                 } else if (dev_state == QLA8XXX_DEV_NEED_QUIESCENT &&
2572                     !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
2573                         ql4_printk(KERN_INFO, ha, "%s: HW State: NEED QUIES!\n",
2574                             __func__);
2575                         set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
2576                         qla4xxx_wake_dpc(ha);
2577                 } else  {
2578                         /* Check firmware health */
2579                         if (qla4_8xxx_check_fw_alive(ha))
2580                                 qla4_8xxx_process_fw_error(ha);
2581                 }
2582         }
2583 }
2584
2585 static void qla4xxx_check_relogin_flash_ddb(struct iscsi_cls_session *cls_sess)
2586 {
2587         struct iscsi_session *sess;
2588         struct ddb_entry *ddb_entry;
2589         struct scsi_qla_host *ha;
2590
2591         sess = cls_sess->dd_data;
2592         ddb_entry = sess->dd_data;
2593         ha = ddb_entry->ha;
2594
2595         if (!(ddb_entry->ddb_type == FLASH_DDB))
2596                 return;
2597
2598         if (adapter_up(ha) && !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
2599             !iscsi_is_session_online(cls_sess)) {
2600                 if (atomic_read(&ddb_entry->retry_relogin_timer) !=
2601                     INVALID_ENTRY) {
2602                         if (atomic_read(&ddb_entry->retry_relogin_timer) ==
2603                                         0) {
2604                                 atomic_set(&ddb_entry->retry_relogin_timer,
2605                                            INVALID_ENTRY);
2606                                 set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
2607                                 set_bit(DF_RELOGIN, &ddb_entry->flags);
2608                                 DEBUG2(ql4_printk(KERN_INFO, ha,
2609                                        "%s: index [%d] login device\n",
2610                                         __func__, ddb_entry->fw_ddb_index));
2611                         } else
2612                                 atomic_dec(&ddb_entry->retry_relogin_timer);
2613                 }
2614         }
2615
2616         /* Wait for relogin to timeout */
2617         if (atomic_read(&ddb_entry->relogin_timer) &&
2618             (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
2619                 /*
2620                  * If the relogin times out and the device is
2621                  * still NOT ONLINE then try and relogin again.
2622                  */
2623                 if (!iscsi_is_session_online(cls_sess)) {
2624                         /* Reset retry relogin timer */
2625                         atomic_inc(&ddb_entry->relogin_retry_count);
2626                         DEBUG2(ql4_printk(KERN_INFO, ha,
2627                                 "%s: index[%d] relogin timed out-retrying"
2628                                 " relogin (%d), retry (%d)\n", __func__,
2629                                 ddb_entry->fw_ddb_index,
2630                                 atomic_read(&ddb_entry->relogin_retry_count),
2631                                 ddb_entry->default_time2wait + 4));
2632                         set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
2633                         atomic_set(&ddb_entry->retry_relogin_timer,
2634                                    ddb_entry->default_time2wait + 4);
2635                 }
2636         }
2637 }
2638
2639 /**
2640  * qla4xxx_timer - checks every second for work to do.
2641  * @ha: Pointer to host adapter structure.
2642  **/
2643 static void qla4xxx_timer(struct scsi_qla_host *ha)
2644 {
2645         int start_dpc = 0;
2646         uint16_t w;
2647
2648         iscsi_host_for_each_session(ha->host, qla4xxx_check_relogin_flash_ddb);
2649
2650         /* If we are in the middle of AER/EEH processing
2651          * skip any processing and reschedule the timer
2652          */
2653         if (test_bit(AF_EEH_BUSY, &ha->flags)) {
2654                 mod_timer(&ha->timer, jiffies + HZ);
2655                 return;
2656         }
2657
2658         /* Hardware read to trigger an EEH error during mailbox waits. */
2659         if (!pci_channel_offline(ha->pdev))
2660                 pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
2661
2662         if (is_qla80XX(ha))
2663                 qla4_8xxx_watchdog(ha);
2664
2665         if (is_qla40XX(ha)) {
2666                 /* Check for heartbeat interval. */
2667                 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
2668                     ha->heartbeat_interval != 0) {
2669                         ha->seconds_since_last_heartbeat++;
2670                         if (ha->seconds_since_last_heartbeat >
2671                             ha->heartbeat_interval + 2)
2672                                 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2673                 }
2674         }
2675
2676         /* Process any deferred work. */
2677         if (!list_empty(&ha->work_list))
2678                 start_dpc++;
2679
2680         /* Wakeup the dpc routine for this adapter, if needed. */
2681         if (start_dpc ||
2682              test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
2683              test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
2684              test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
2685              test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
2686              test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
2687              test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
2688              test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
2689              test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
2690              test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
2691              test_bit(DPC_AEN, &ha->dpc_flags)) {
2692                 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
2693                               " - dpc flags = 0x%lx\n",
2694                               ha->host_no, __func__, ha->dpc_flags));
2695                 qla4xxx_wake_dpc(ha);
2696         }
2697
2698         /* Reschedule timer thread to call us back in one second */
2699         mod_timer(&ha->timer, jiffies + HZ);
2700
2701         DEBUG2(ha->seconds_since_last_intr++);
2702 }
2703
2704 /**
2705  * qla4xxx_cmd_wait - waits for all outstanding commands to complete
2706  * @ha: Pointer to host adapter structure.
2707  *
2708  * This routine stalls the driver until all outstanding commands are returned.
2709  * Caller must release the Hardware Lock prior to calling this routine.
2710  **/
2711 static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
2712 {
2713         uint32_t index = 0;
2714         unsigned long flags;
2715         struct scsi_cmnd *cmd;
2716
2717         unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
2718
2719         DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
2720             "complete\n", WAIT_CMD_TOV));
2721
2722         while (!time_after_eq(jiffies, wtime)) {
2723                 spin_lock_irqsave(&ha->hardware_lock, flags);
2724                 /* Find a command that hasn't completed. */
2725                 for (index = 0; index < ha->host->can_queue; index++) {
2726                         cmd = scsi_host_find_tag(ha->host, index);
2727                         /*
2728                          * We cannot just check if the index is valid,
2729                          * becase if we are run from the scsi eh, then
2730                          * the scsi/block layer is going to prevent
2731                          * the tag from being released.
2732                          */
2733                         if (cmd != NULL && CMD_SP(cmd))
2734                                 break;
2735                 }
2736                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2737
2738                 /* If No Commands are pending, wait is complete */
2739                 if (index == ha->host->can_queue)
2740                         return QLA_SUCCESS;
2741
2742                 msleep(1000);
2743         }
2744         /* If we timed out on waiting for commands to come back
2745          * return ERROR. */
2746         return QLA_ERROR;
2747 }
2748
2749 int qla4xxx_hw_reset(struct scsi_qla_host *ha)
2750 {
2751         uint32_t ctrl_status;
2752         unsigned long flags = 0;
2753
2754         DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
2755
2756         if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
2757                 return QLA_ERROR;
2758
2759         spin_lock_irqsave(&ha->hardware_lock, flags);
2760
2761         /*
2762          * If the SCSI Reset Interrupt bit is set, clear it.
2763          * Otherwise, the Soft Reset won't work.
2764          */
2765         ctrl_status = readw(&ha->reg->ctrl_status);
2766         if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
2767                 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
2768
2769         /* Issue Soft Reset */
2770         writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
2771         readl(&ha->reg->ctrl_status);
2772
2773         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2774         return QLA_SUCCESS;
2775 }
2776
2777 /**
2778  * qla4xxx_soft_reset - performs soft reset.
2779  * @ha: Pointer to host adapter structure.
2780  **/
2781 int qla4xxx_soft_reset(struct scsi_qla_host *ha)
2782 {
2783         uint32_t max_wait_time;
2784         unsigned long flags = 0;
2785         int status;
2786         uint32_t ctrl_status;
2787
2788         status = qla4xxx_hw_reset(ha);
2789         if (status != QLA_SUCCESS)
2790                 return status;
2791
2792         status = QLA_ERROR;
2793         /* Wait until the Network Reset Intr bit is cleared */
2794         max_wait_time = RESET_INTR_TOV;
2795         do {
2796                 spin_lock_irqsave(&ha->hardware_lock, flags);
2797                 ctrl_status = readw(&ha->reg->ctrl_status);
2798                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2799
2800                 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
2801                         break;
2802
2803                 msleep(1000);
2804         } while ((--max_wait_time));
2805
2806         if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
2807                 DEBUG2(printk(KERN_WARNING
2808                               "scsi%ld: Network Reset Intr not cleared by "
2809                               "Network function, clearing it now!\n",
2810                               ha->host_no));
2811                 spin_lock_irqsave(&ha->hardware_lock, flags);
2812                 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
2813                 readl(&ha->reg->ctrl_status);
2814                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2815         }
2816
2817         /* Wait until the firmware tells us the Soft Reset is done */
2818         max_wait_time = SOFT_RESET_TOV;
2819         do {
2820                 spin_lock_irqsave(&ha->hardware_lock, flags);
2821                 ctrl_status = readw(&ha->reg->ctrl_status);
2822                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2823
2824                 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
2825                         status = QLA_SUCCESS;
2826                         break;
2827                 }
2828
2829                 msleep(1000);
2830         } while ((--max_wait_time));
2831
2832         /*
2833          * Also, make sure that the SCSI Reset Interrupt bit has been cleared
2834          * after the soft reset has taken place.
2835          */
2836         spin_lock_irqsave(&ha->hardware_lock, flags);
2837         ctrl_status = readw(&ha->reg->ctrl_status);
2838         if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
2839                 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
2840                 readl(&ha->reg->ctrl_status);
2841         }
2842         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2843
2844         /* If soft reset fails then most probably the bios on other
2845          * function is also enabled.
2846          * Since the initialization is sequential the other fn
2847          * wont be able to acknowledge the soft reset.
2848          * Issue a force soft reset to workaround this scenario.
2849          */
2850         if (max_wait_time == 0) {
2851                 /* Issue Force Soft Reset */
2852                 spin_lock_irqsave(&ha->hardware_lock, flags);
2853                 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
2854                 readl(&ha->reg->ctrl_status);
2855                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2856                 /* Wait until the firmware tells us the Soft Reset is done */
2857                 max_wait_time = SOFT_RESET_TOV;
2858                 do {
2859                         spin_lock_irqsave(&ha->hardware_lock, flags);
2860                         ctrl_status = readw(&ha->reg->ctrl_status);
2861                         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2862
2863                         if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
2864                                 status = QLA_SUCCESS;
2865                                 break;
2866                         }
2867
2868                         msleep(1000);
2869                 } while ((--max_wait_time));
2870         }
2871
2872         return status;
2873 }
2874
2875 /**
2876  * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
2877  * @ha: Pointer to host adapter structure.
2878  * @res: returned scsi status
2879  *
2880  * This routine is called just prior to a HARD RESET to return all
2881  * outstanding commands back to the Operating System.
2882  * Caller should make sure that the following locks are released
2883  * before this calling routine: Hardware lock, and io_request_lock.
2884  **/
2885 static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
2886 {
2887         struct srb *srb;
2888         int i;
2889         unsigned long flags;
2890
2891         spin_lock_irqsave(&ha->hardware_lock, flags);
2892         for (i = 0; i < ha->host->can_queue; i++) {
2893                 srb = qla4xxx_del_from_active_array(ha, i);
2894                 if (srb != NULL) {
2895                         srb->cmd->result = res;
2896                         kref_put(&srb->srb_ref, qla4xxx_srb_compl);
2897                 }
2898         }
2899         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2900 }
2901
2902 void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
2903 {
2904         clear_bit(AF_ONLINE, &ha->flags);
2905
2906         /* Disable the board */
2907         ql4_printk(KERN_INFO, ha, "Disabling the board\n");
2908
2909         qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
2910         qla4xxx_mark_all_devices_missing(ha);
2911         clear_bit(AF_INIT_DONE, &ha->flags);
2912 }
2913
2914 static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session)
2915 {
2916         struct iscsi_session *sess;
2917         struct ddb_entry *ddb_entry;
2918
2919         sess = cls_session->dd_data;
2920         ddb_entry = sess->dd_data;
2921         ddb_entry->fw_ddb_device_state = DDB_DS_SESSION_FAILED;
2922
2923         if (ddb_entry->ddb_type == FLASH_DDB)
2924                 iscsi_block_session(ddb_entry->sess);
2925         else
2926                 iscsi_session_failure(cls_session->dd_data,
2927                                       ISCSI_ERR_CONN_FAILED);
2928 }
2929
2930 /**
2931  * qla4xxx_recover_adapter - recovers adapter after a fatal error
2932  * @ha: Pointer to host adapter structure.
2933  **/
2934 static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
2935 {
2936         int status = QLA_ERROR;
2937         uint8_t reset_chip = 0;
2938         uint32_t dev_state;
2939         unsigned long wait;
2940
2941         /* Stall incoming I/O until we are done */
2942         scsi_block_requests(ha->host);
2943         clear_bit(AF_ONLINE, &ha->flags);
2944         clear_bit(AF_LINK_UP, &ha->flags);
2945
2946         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
2947
2948         set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2949
2950         if (is_qla8032(ha) &&
2951             !test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
2952                 ql4_printk(KERN_INFO, ha, "%s: disabling pause transmit on port 0 & 1.\n",
2953                            __func__);
2954                 /* disable pause frame for ISP83xx */
2955                 qla4_83xx_disable_pause(ha);
2956         }
2957
2958         iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
2959
2960         if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
2961                 reset_chip = 1;
2962
2963         /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
2964          * do not reset adapter, jump to initialize_adapter */
2965         if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
2966                 status = QLA_SUCCESS;
2967                 goto recover_ha_init_adapter;
2968         }
2969
2970         /* For the ISP-8xxx adapter, issue a stop_firmware if invoked
2971          * from eh_host_reset or ioctl module */
2972         if (is_qla80XX(ha) && !reset_chip &&
2973             test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
2974
2975                 DEBUG2(ql4_printk(KERN_INFO, ha,
2976                     "scsi%ld: %s - Performing stop_firmware...\n",
2977                     ha->host_no, __func__));
2978                 status = ha->isp_ops->reset_firmware(ha);
2979                 if (status == QLA_SUCCESS) {
2980                         if (!test_bit(AF_FW_RECOVERY, &ha->flags))
2981                                 qla4xxx_cmd_wait(ha);
2982
2983                         ha->isp_ops->disable_intrs(ha);
2984                         qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2985                         qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
2986                 } else {
2987                         /* If the stop_firmware fails then
2988                          * reset the entire chip */
2989                         reset_chip = 1;
2990                         clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2991                         set_bit(DPC_RESET_HA, &ha->dpc_flags);
2992                 }
2993         }
2994
2995         /* Issue full chip reset if recovering from a catastrophic error,
2996          * or if stop_firmware fails for ISP-8xxx.
2997          * This is the default case for ISP-4xxx */
2998         if (is_qla40XX(ha) || reset_chip) {
2999                 if (is_qla40XX(ha))
3000                         goto chip_reset;
3001
3002                 /* Check if 8XXX firmware is alive or not
3003                  * We may have arrived here from NEED_RESET
3004                  * detection only */
3005                 if (test_bit(AF_FW_RECOVERY, &ha->flags))
3006                         goto chip_reset;
3007
3008                 wait = jiffies + (FW_ALIVE_WAIT_TOV * HZ);
3009                 while (time_before(jiffies, wait)) {
3010                         if (qla4_8xxx_check_fw_alive(ha)) {
3011                                 qla4xxx_mailbox_premature_completion(ha);
3012                                 break;
3013                         }
3014
3015                         set_current_state(TASK_UNINTERRUPTIBLE);
3016                         schedule_timeout(HZ);
3017                 }
3018 chip_reset:
3019                 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
3020                         qla4xxx_cmd_wait(ha);
3021
3022                 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
3023                 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
3024                 DEBUG2(ql4_printk(KERN_INFO, ha,
3025                     "scsi%ld: %s - Performing chip reset..\n",
3026                     ha->host_no, __func__));
3027                 status = ha->isp_ops->reset_chip(ha);
3028         }
3029
3030         /* Flush any pending ddb changed AENs */
3031         qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
3032
3033 recover_ha_init_adapter:
3034         /* Upon successful firmware/chip reset, re-initialize the adapter */
3035         if (status == QLA_SUCCESS) {
3036                 /* For ISP-4xxx, force function 1 to always initialize
3037                  * before function 3 to prevent both funcions from
3038                  * stepping on top of the other */
3039                 if (is_qla40XX(ha) && (ha->mac_index == 3))
3040                         ssleep(6);
3041
3042                 /* NOTE: AF_ONLINE flag set upon successful completion of
3043                  *       qla4xxx_initialize_adapter */
3044                 status = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
3045         }
3046
3047         /* Retry failed adapter initialization, if necessary
3048          * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
3049          * case to prevent ping-pong resets between functions */
3050         if (!test_bit(AF_ONLINE, &ha->flags) &&
3051             !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
3052                 /* Adapter initialization failed, see if we can retry
3053                  * resetting the ha.
3054                  * Since we don't want to block the DPC for too long
3055                  * with multiple resets in the same thread,
3056                  * utilize DPC to retry */
3057                 if (is_qla80XX(ha)) {
3058                         ha->isp_ops->idc_lock(ha);
3059                         dev_state = qla4_8xxx_rd_direct(ha,
3060                                                         QLA8XXX_CRB_DEV_STATE);
3061                         ha->isp_ops->idc_unlock(ha);
3062                         if (dev_state == QLA8XXX_DEV_FAILED) {
3063                                 ql4_printk(KERN_INFO, ha, "%s: don't retry "
3064                                            "recover adapter. H/W is in Failed "
3065                                            "state\n", __func__);
3066                                 qla4xxx_dead_adapter_cleanup(ha);
3067                                 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
3068                                 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
3069                                 clear_bit(DPC_RESET_HA_FW_CONTEXT,
3070                                                 &ha->dpc_flags);
3071                                 status = QLA_ERROR;
3072
3073                                 goto exit_recover;
3074                         }
3075                 }
3076
3077                 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
3078                         ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
3079                         DEBUG2(printk("scsi%ld: recover adapter - retrying "
3080                                       "(%d) more times\n", ha->host_no,
3081                                       ha->retry_reset_ha_cnt));
3082                         set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
3083                         status = QLA_ERROR;
3084                 } else {
3085                         if (ha->retry_reset_ha_cnt > 0) {
3086                                 /* Schedule another Reset HA--DPC will retry */
3087                                 ha->retry_reset_ha_cnt--;
3088                                 DEBUG2(printk("scsi%ld: recover adapter - "
3089                                               "retry remaining %d\n",
3090                                               ha->host_no,
3091                                               ha->retry_reset_ha_cnt));
3092                                 status = QLA_ERROR;
3093                         }
3094
3095                         if (ha->retry_reset_ha_cnt == 0) {
3096                                 /* Recover adapter retries have been exhausted.
3097                                  * Adapter DEAD */
3098                                 DEBUG2(printk("scsi%ld: recover adapter "
3099                                               "failed - board disabled\n",
3100                                               ha->host_no));
3101                                 qla4xxx_dead_adapter_cleanup(ha);
3102                                 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
3103                                 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
3104                                 clear_bit(DPC_RESET_HA_FW_CONTEXT,
3105                                           &ha->dpc_flags);
3106                                 status = QLA_ERROR;
3107                         }
3108                 }
3109         } else {
3110                 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
3111                 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
3112                 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
3113         }
3114
3115 exit_recover:
3116         ha->adapter_error_count++;
3117
3118         if (test_bit(AF_ONLINE, &ha->flags))
3119                 ha->isp_ops->enable_intrs(ha);
3120
3121         scsi_unblock_requests(ha->host);
3122
3123         clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
3124         DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
3125             status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
3126
3127         return status;
3128 }
3129
3130 static void qla4xxx_relogin_devices(struct iscsi_cls_session *cls_session)
3131 {
3132         struct iscsi_session *sess;
3133         struct ddb_entry *ddb_entry;
3134         struct scsi_qla_host *ha;
3135
3136         sess = cls_session->dd_data;
3137         ddb_entry = sess->dd_data;
3138         ha = ddb_entry->ha;
3139         if (!iscsi_is_session_online(cls_session)) {
3140                 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
3141                         ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
3142                                    " unblock session\n", ha->host_no, __func__,
3143                                    ddb_entry->fw_ddb_index);
3144                         iscsi_unblock_session(ddb_entry->sess);
3145                 } else {
3146                         /* Trigger relogin */
3147                         if (ddb_entry->ddb_type == FLASH_DDB) {
3148                                 if (!test_bit(DF_RELOGIN, &ddb_entry->flags))
3149                                         qla4xxx_arm_relogin_timer(ddb_entry);
3150                         } else
3151                                 iscsi_session_failure(cls_session->dd_data,
3152                                                       ISCSI_ERR_CONN_FAILED);
3153                 }
3154         }
3155 }
3156
3157 int qla4xxx_unblock_flash_ddb(struct iscsi_cls_session *cls_session)
3158 {
3159         struct iscsi_session *sess;
3160         struct ddb_entry *ddb_entry;
3161         struct scsi_qla_host *ha;
3162
3163         sess = cls_session->dd_data;
3164         ddb_entry = sess->dd_data;
3165         ha = ddb_entry->ha;
3166         ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
3167                    " unblock session\n", ha->host_no, __func__,
3168                    ddb_entry->fw_ddb_index);
3169
3170         iscsi_unblock_session(ddb_entry->sess);
3171
3172         /* Start scan target */
3173         if (test_bit(AF_ONLINE, &ha->flags)) {
3174                 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
3175                            " start scan\n", ha->host_no, __func__,
3176                            ddb_entry->fw_ddb_index);
3177                 scsi_queue_work(ha->host, &ddb_entry->sess->scan_work);
3178         }
3179         return QLA_SUCCESS;
3180 }
3181
3182 int qla4xxx_unblock_ddb(struct iscsi_cls_session *cls_session)
3183 {
3184         struct iscsi_session *sess;
3185         struct ddb_entry *ddb_entry;
3186         struct scsi_qla_host *ha;
3187         int status = QLA_SUCCESS;
3188
3189         sess = cls_session->dd_data;
3190         ddb_entry = sess->dd_data;
3191         ha = ddb_entry->ha;
3192         ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
3193                    " unblock user space session\n", ha->host_no, __func__,
3194                    ddb_entry->fw_ddb_index);
3195
3196         if (!iscsi_is_session_online(cls_session)) {
3197                 iscsi_conn_start(ddb_entry->conn);
3198                 iscsi_conn_login_event(ddb_entry->conn,
3199                                        ISCSI_CONN_STATE_LOGGED_IN);
3200         } else {
3201                 ql4_printk(KERN_INFO, ha,
3202                            "scsi%ld: %s: ddb[%d] session [%d] already logged in\n",
3203                            ha->host_no, __func__, ddb_entry->fw_ddb_index,
3204                            cls_session->sid);
3205                 status = QLA_ERROR;
3206         }
3207
3208         return status;
3209 }
3210
3211 static void qla4xxx_relogin_all_devices(struct scsi_qla_host *ha)
3212 {
3213         iscsi_host_for_each_session(ha->host, qla4xxx_relogin_devices);
3214 }
3215
3216 static void qla4xxx_relogin_flash_ddb(struct iscsi_cls_session *cls_sess)
3217 {
3218         uint16_t relogin_timer;
3219         struct iscsi_session *sess;
3220         struct ddb_entry *ddb_entry;
3221         struct scsi_qla_host *ha;
3222
3223         sess = cls_sess->dd_data;
3224         ddb_entry = sess->dd_data;
3225         ha = ddb_entry->ha;
3226
3227         relogin_timer = max(ddb_entry->default_relogin_timeout,
3228                             (uint16_t)RELOGIN_TOV);
3229         atomic_set(&ddb_entry->relogin_timer, relogin_timer);
3230
3231         DEBUG2(ql4_printk(KERN_INFO, ha,
3232                           "scsi%ld: Relogin index [%d]. TOV=%d\n", ha->host_no,
3233                           ddb_entry->fw_ddb_index, relogin_timer));
3234
3235         qla4xxx_login_flash_ddb(cls_sess);
3236 }
3237
3238 static void qla4xxx_dpc_relogin(struct iscsi_cls_session *cls_sess)
3239 {
3240         struct iscsi_session *sess;
3241         struct ddb_entry *ddb_entry;
3242         struct scsi_qla_host *ha;
3243
3244         sess = cls_sess->dd_data;
3245         ddb_entry = sess->dd_data;
3246         ha = ddb_entry->ha;
3247
3248         if (!(ddb_entry->ddb_type == FLASH_DDB))
3249                 return;
3250
3251         if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
3252             !iscsi_is_session_online(cls_sess)) {
3253                 DEBUG2(ql4_printk(KERN_INFO, ha,
3254                                   "relogin issued\n"));
3255                 qla4xxx_relogin_flash_ddb(cls_sess);
3256         }
3257 }
3258
3259 void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
3260 {
3261         if (ha->dpc_thread)
3262                 queue_work(ha->dpc_thread, &ha->dpc_work);
3263 }
3264
3265 static struct qla4_work_evt *
3266 qla4xxx_alloc_work(struct scsi_qla_host *ha, uint32_t data_size,
3267                    enum qla4_work_type type)
3268 {
3269         struct qla4_work_evt *e;
3270         uint32_t size = sizeof(struct qla4_work_evt) + data_size;
3271
3272         e = kzalloc(size, GFP_ATOMIC);
3273         if (!e)
3274                 return NULL;
3275
3276         INIT_LIST_HEAD(&e->list);
3277         e->type = type;
3278         return e;
3279 }
3280
3281 static void qla4xxx_post_work(struct scsi_qla_host *ha,
3282                              struct qla4_work_evt *e)
3283 {
3284         unsigned long flags;
3285
3286         spin_lock_irqsave(&ha->work_lock, flags);
3287         list_add_tail(&e->list, &ha->work_list);
3288         spin_unlock_irqrestore(&ha->work_lock, flags);
3289         qla4xxx_wake_dpc(ha);
3290 }
3291
3292 int qla4xxx_post_aen_work(struct scsi_qla_host *ha,
3293                           enum iscsi_host_event_code aen_code,
3294                           uint32_t data_size, uint8_t *data)
3295 {
3296         struct qla4_work_evt *e;
3297
3298         e = qla4xxx_alloc_work(ha, data_size, QLA4_EVENT_AEN);
3299         if (!e)
3300                 return QLA_ERROR;
3301
3302         e->u.aen.code = aen_code;
3303         e->u.aen.data_size = data_size;
3304         memcpy(e->u.aen.data, data, data_size);
3305
3306         qla4xxx_post_work(ha, e);
3307
3308         return QLA_SUCCESS;
3309 }
3310
3311 int qla4xxx_post_ping_evt_work(struct scsi_qla_host *ha,
3312                                uint32_t status, uint32_t pid,
3313                                uint32_t data_size, uint8_t *data)
3314 {
3315         struct qla4_work_evt *e;
3316
3317         e = qla4xxx_alloc_work(ha, data_size, QLA4_EVENT_PING_STATUS);
3318         if (!e)
3319                 return QLA_ERROR;
3320
3321         e->u.ping.status = status;
3322         e->u.ping.pid = pid;
3323         e->u.ping.data_size = data_size;
3324         memcpy(e->u.ping.data, data, data_size);
3325
3326         qla4xxx_post_work(ha, e);
3327
3328         return QLA_SUCCESS;
3329 }
3330
3331 static void qla4xxx_do_work(struct scsi_qla_host *ha)
3332 {
3333         struct qla4_work_evt *e, *tmp;
3334         unsigned long flags;
3335         LIST_HEAD(work);
3336
3337         spin_lock_irqsave(&ha->work_lock, flags);
3338         list_splice_init(&ha->work_list, &work);
3339         spin_unlock_irqrestore(&ha->work_lock, flags);
3340
3341         list_for_each_entry_safe(e, tmp, &work, list) {
3342                 list_del_init(&e->list);
3343
3344                 switch (e->type) {
3345                 case QLA4_EVENT_AEN:
3346                         iscsi_post_host_event(ha->host_no,
3347                                               &qla4xxx_iscsi_transport,
3348                                               e->u.aen.code,
3349                                               e->u.aen.data_size,
3350                                               e->u.aen.data);
3351                         break;
3352                 case QLA4_EVENT_PING_STATUS:
3353                         iscsi_ping_comp_event(ha->host_no,
3354                                               &qla4xxx_iscsi_transport,
3355                                               e->u.ping.status,
3356                                               e->u.ping.pid,
3357                                               e->u.ping.data_size,
3358                                               e->u.ping.data);
3359                         break;
3360                 default:
3361                         ql4_printk(KERN_WARNING, ha, "event type: 0x%x not "
3362                                    "supported", e->type);
3363                 }
3364                 kfree(e);
3365         }
3366 }
3367
3368 /**
3369  * qla4xxx_do_dpc - dpc routine
3370  * @data: in our case pointer to adapter structure
3371  *
3372  * This routine is a task that is schedule by the interrupt handler
3373  * to perform the background processing for interrupts.  We put it
3374  * on a task queue that is consumed whenever the scheduler runs; that's
3375  * so you can do anything (i.e. put the process to sleep etc).  In fact,
3376  * the mid-level tries to sleep when it reaches the driver threshold
3377  * "host->can_queue". This can cause a panic if we were in our interrupt code.
3378  **/
3379 static void qla4xxx_do_dpc(struct work_struct *work)
3380 {
3381         struct scsi_qla_host *ha =
3382                 container_of(work, struct scsi_qla_host, dpc_work);
3383         int status = QLA_ERROR;
3384
3385         DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
3386             "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
3387             ha->host_no, __func__, ha->flags, ha->dpc_flags))
3388
3389         /* Initialization not yet finished. Don't do anything yet. */
3390         if (!test_bit(AF_INIT_DONE, &ha->flags))
3391                 return;
3392
3393         if (test_bit(AF_EEH_BUSY, &ha->flags)) {
3394                 DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
3395                     ha->host_no, __func__, ha->flags));
3396                 return;
3397         }
3398
3399         /* post events to application */
3400         qla4xxx_do_work(ha);
3401
3402         if (is_qla80XX(ha)) {
3403                 if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
3404                         if (is_qla8032(ha)) {
3405                                 ql4_printk(KERN_INFO, ha, "%s: disabling pause transmit on port 0 & 1.\n",
3406                                            __func__);
3407                                 /* disable pause frame for ISP83xx */
3408                                 qla4_83xx_disable_pause(ha);
3409                         }
3410
3411                         ha->isp_ops->idc_lock(ha);
3412                         qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
3413                                             QLA8XXX_DEV_FAILED);
3414                         ha->isp_ops->idc_unlock(ha);
3415                         ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
3416                         qla4_8xxx_device_state_handler(ha);
3417                 }
3418
3419                 if (test_and_clear_bit(DPC_POST_IDC_ACK, &ha->dpc_flags))
3420                         qla4_83xx_post_idc_ack(ha);
3421
3422                 if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
3423                         qla4_8xxx_need_qsnt_handler(ha);
3424                 }
3425         }
3426
3427         if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
3428             (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
3429             test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
3430             test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
3431                 if ((is_qla8022(ha) && ql4xdontresethba) ||
3432                     (is_qla8032(ha) && qla4_83xx_idc_dontreset(ha))) {
3433                         DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
3434                             ha->host_no, __func__));
3435                         clear_bit(DPC_RESET_HA, &ha->dpc_flags);
3436                         clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
3437                         clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
3438                         goto dpc_post_reset_ha;
3439                 }
3440                 if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
3441                     test_bit(DPC_RESET_HA, &ha->dpc_flags))
3442                         qla4xxx_recover_adapter(ha);
3443
3444                 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
3445                         uint8_t wait_time = RESET_INTR_TOV;
3446
3447                         while ((readw(&ha->reg->ctrl_status) &
3448                                 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
3449                                 if (--wait_time == 0)
3450                                         break;
3451                                 msleep(1000);
3452                         }
3453                         if (wait_time == 0)
3454                                 DEBUG2(printk("scsi%ld: %s: SR|FSR "
3455                                               "bit not cleared-- resetting\n",
3456                                               ha->host_no, __func__));
3457                         qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
3458                         if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
3459                                 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
3460                                 status = qla4xxx_recover_adapter(ha);
3461                         }
3462                         clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
3463                         if (status == QLA_SUCCESS)
3464                                 ha->isp_ops->enable_intrs(ha);
3465                 }
3466         }
3467
3468 dpc_post_reset_ha:
3469         /* ---- process AEN? --- */
3470         if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
3471                 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
3472
3473         /* ---- Get DHCP IP Address? --- */
3474         if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
3475                 qla4xxx_get_dhcp_ip_address(ha);
3476
3477         /* ---- relogin device? --- */
3478         if (adapter_up(ha) &&
3479             test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
3480                 iscsi_host_for_each_session(ha->host, qla4xxx_dpc_relogin);
3481         }
3482
3483         /* ---- link change? --- */
3484         if (!test_bit(AF_LOOPBACK, &ha->flags) &&
3485             test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
3486                 if (!test_bit(AF_LINK_UP, &ha->flags)) {
3487                         /* ---- link down? --- */
3488                         qla4xxx_mark_all_devices_missing(ha);
3489                 } else {
3490                         /* ---- link up? --- *
3491                          * F/W will auto login to all devices ONLY ONCE after
3492                          * link up during driver initialization and runtime
3493                          * fatal error recovery.  Therefore, the driver must
3494                          * manually relogin to devices when recovering from
3495                          * connection failures, logouts, expired KATO, etc. */
3496                         if (test_and_clear_bit(AF_BUILD_DDB_LIST, &ha->flags)) {
3497                                 qla4xxx_build_ddb_list(ha, ha->is_reset);
3498                                 iscsi_host_for_each_session(ha->host,
3499                                                 qla4xxx_login_flash_ddb);
3500                         } else
3501                                 qla4xxx_relogin_all_devices(ha);
3502                 }
3503         }
3504 }
3505
3506 /**
3507  * qla4xxx_free_adapter - release the adapter
3508  * @ha: pointer to adapter structure
3509  **/
3510 static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
3511 {
3512         qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
3513
3514         /* Turn-off interrupts on the card. */
3515         ha->isp_ops->disable_intrs(ha);
3516
3517         if (is_qla40XX(ha)) {
3518                 writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
3519                        &ha->reg->ctrl_status);
3520                 readl(&ha->reg->ctrl_status);
3521         } else if (is_qla8022(ha)) {
3522                 writel(0, &ha->qla4_82xx_reg->host_int);
3523                 readl(&ha->qla4_82xx_reg->host_int);
3524         } else if (is_qla8032(ha)) {
3525                 writel(0, &ha->qla4_83xx_reg->risc_intr);
3526                 readl(&ha->qla4_83xx_reg->risc_intr);
3527         }
3528
3529         /* Remove timer thread, if present */
3530         if (ha->timer_active)
3531                 qla4xxx_stop_timer(ha);
3532
3533         /* Kill the kernel thread for this host */
3534         if (ha->dpc_thread)
3535                 destroy_workqueue(ha->dpc_thread);
3536
3537         /* Kill the kernel thread for this host */
3538         if (ha->task_wq)
3539                 destroy_workqueue(ha->task_wq);
3540
3541         /* Put firmware in known state */
3542         ha->isp_ops->reset_firmware(ha);
3543
3544         if (is_qla80XX(ha)) {
3545                 ha->isp_ops->idc_lock(ha);
3546                 qla4_8xxx_clear_drv_active(ha);
3547                 ha->isp_ops->idc_unlock(ha);
3548         }
3549
3550         /* Detach interrupts */
3551         qla4xxx_free_irqs(ha);
3552
3553         /* free extra memory */
3554         qla4xxx_mem_free(ha);
3555 }
3556
3557 int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
3558 {
3559         int status = 0;
3560         unsigned long mem_base, mem_len, db_base, db_len;
3561         struct pci_dev *pdev = ha->pdev;
3562
3563         status = pci_request_regions(pdev, DRIVER_NAME);
3564         if (status) {
3565                 printk(KERN_WARNING
3566                     "scsi(%ld) Failed to reserve PIO regions (%s) "
3567                     "status=%d\n", ha->host_no, pci_name(pdev), status);
3568                 goto iospace_error_exit;
3569         }
3570
3571         DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
3572             __func__, pdev->revision));
3573         ha->revision_id = pdev->revision;
3574
3575         /* remap phys address */
3576         mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
3577         mem_len = pci_resource_len(pdev, 0);
3578         DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
3579             __func__, mem_base, mem_len));
3580
3581         /* mapping of pcibase pointer */
3582         ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
3583         if (!ha->nx_pcibase) {
3584                 printk(KERN_ERR
3585                     "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
3586                 pci_release_regions(ha->pdev);
3587                 goto iospace_error_exit;
3588         }
3589
3590         /* Mapping of IO base pointer, door bell read and write pointer */
3591
3592         /* mapping of IO base pointer */
3593         if (is_qla8022(ha)) {
3594                 ha->qla4_82xx_reg = (struct device_reg_82xx  __iomem *)
3595                                     ((uint8_t *)ha->nx_pcibase + 0xbc000 +
3596                                      (ha->pdev->devfn << 11));
3597                 ha->nx_db_wr_ptr = (ha->pdev->devfn == 4 ? QLA82XX_CAM_RAM_DB1 :
3598                                     QLA82XX_CAM_RAM_DB2);
3599         } else if (is_qla8032(ha)) {
3600                 ha->qla4_83xx_reg = (struct device_reg_83xx __iomem *)
3601                                     ((uint8_t *)ha->nx_pcibase);
3602         }
3603
3604         db_base = pci_resource_start(pdev, 4);  /* doorbell is on bar 4 */
3605         db_len = pci_resource_len(pdev, 4);
3606
3607         return 0;
3608 iospace_error_exit:
3609         return -ENOMEM;
3610 }
3611
3612 /***
3613  * qla4xxx_iospace_config - maps registers
3614  * @ha: pointer to adapter structure
3615  *
3616  * This routines maps HBA's registers from the pci address space
3617  * into the kernel virtual address space for memory mapped i/o.
3618  **/
3619 int qla4xxx_iospace_config(struct scsi_qla_host *ha)
3620 {
3621         unsigned long pio, pio_len, pio_flags;
3622         unsigned long mmio, mmio_len, mmio_flags;
3623
3624         pio = pci_resource_start(ha->pdev, 0);
3625         pio_len = pci_resource_len(ha->pdev, 0);
3626         pio_flags = pci_resource_flags(ha->pdev, 0);
3627         if (pio_flags & IORESOURCE_IO) {
3628                 if (pio_len < MIN_IOBASE_LEN) {
3629                         ql4_printk(KERN_WARNING, ha,
3630                                 "Invalid PCI I/O region size\n");
3631                         pio = 0;
3632                 }
3633         } else {
3634                 ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
3635                 pio = 0;
3636         }
3637
3638         /* Use MMIO operations for all accesses. */
3639         mmio = pci_resource_start(ha->pdev, 1);
3640         mmio_len = pci_resource_len(ha->pdev, 1);
3641         mmio_flags = pci_resource_flags(ha->pdev, 1);
3642
3643         if (!(mmio_flags & IORESOURCE_MEM)) {
3644                 ql4_printk(KERN_ERR, ha,
3645                     "region #0 not an MMIO resource, aborting\n");
3646
3647                 goto iospace_error_exit;
3648         }
3649
3650         if (mmio_len < MIN_IOBASE_LEN) {
3651                 ql4_printk(KERN_ERR, ha,
3652                     "Invalid PCI mem region size, aborting\n");
3653                 goto iospace_error_exit;
3654         }
3655
3656         if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
3657                 ql4_printk(KERN_WARNING, ha,
3658                     "Failed to reserve PIO/MMIO regions\n");
3659
3660                 goto iospace_error_exit;
3661         }
3662
3663         ha->pio_address = pio;
3664         ha->pio_length = pio_len;
3665         ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
3666         if (!ha->reg) {
3667                 ql4_printk(KERN_ERR, ha,
3668                     "cannot remap MMIO, aborting\n");
3669
3670                 goto iospace_error_exit;
3671         }
3672
3673         return 0;
3674
3675 iospace_error_exit:
3676         return -ENOMEM;
3677 }
3678
3679 static struct isp_operations qla4xxx_isp_ops = {
3680         .iospace_config         = qla4xxx_iospace_config,
3681         .pci_config             = qla4xxx_pci_config,
3682         .disable_intrs          = qla4xxx_disable_intrs,
3683         .enable_intrs           = qla4xxx_enable_intrs,
3684         .start_firmware         = qla4xxx_start_firmware,
3685         .intr_handler           = qla4xxx_intr_handler,
3686         .interrupt_service_routine = qla4xxx_interrupt_service_routine,
3687         .reset_chip             = qla4xxx_soft_reset,
3688         .reset_firmware         = qla4xxx_hw_reset,
3689         .queue_iocb             = qla4xxx_queue_iocb,
3690         .complete_iocb          = qla4xxx_complete_iocb,
3691         .rd_shdw_req_q_out      = qla4xxx_rd_shdw_req_q_out,
3692         .rd_shdw_rsp_q_in       = qla4xxx_rd_shdw_rsp_q_in,
3693         .get_sys_info           = qla4xxx_get_sys_info,
3694         .queue_mailbox_command  = qla4xxx_queue_mbox_cmd,
3695         .process_mailbox_interrupt = qla4xxx_process_mbox_intr,
3696 };
3697
3698 static struct isp_operations qla4_82xx_isp_ops = {
3699         .iospace_config         = qla4_8xxx_iospace_config,
3700         .pci_config             = qla4_8xxx_pci_config,
3701         .disable_intrs          = qla4_82xx_disable_intrs,
3702         .enable_intrs           = qla4_82xx_enable_intrs,
3703         .start_firmware         = qla4_8xxx_load_risc,
3704         .restart_firmware       = qla4_82xx_try_start_fw,
3705         .intr_handler           = qla4_82xx_intr_handler,
3706         .interrupt_service_routine = qla4_82xx_interrupt_service_routine,
3707         .need_reset             = qla4_8xxx_need_reset,
3708         .reset_chip             = qla4_82xx_isp_reset,
3709         .reset_firmware         = qla4_8xxx_stop_firmware,
3710         .queue_iocb             = qla4_82xx_queue_iocb,
3711         .complete_iocb          = qla4_82xx_complete_iocb,
3712         .rd_shdw_req_q_out      = qla4_82xx_rd_shdw_req_q_out,
3713         .rd_shdw_rsp_q_in       = qla4_82xx_rd_shdw_rsp_q_in,
3714         .get_sys_info           = qla4_8xxx_get_sys_info,
3715         .rd_reg_direct          = qla4_82xx_rd_32,
3716         .wr_reg_direct          = qla4_82xx_wr_32,
3717         .rd_reg_indirect        = qla4_82xx_md_rd_32,
3718         .wr_reg_indirect        = qla4_82xx_md_wr_32,
3719         .idc_lock               = qla4_82xx_idc_lock,
3720         .idc_unlock             = qla4_82xx_idc_unlock,
3721         .rom_lock_recovery      = qla4_82xx_rom_lock_recovery,
3722         .queue_mailbox_command  = qla4_82xx_queue_mbox_cmd,
3723         .process_mailbox_interrupt = qla4_82xx_process_mbox_intr,
3724 };
3725
3726 static struct isp_operations qla4_83xx_isp_ops = {
3727         .iospace_config         = qla4_8xxx_iospace_config,
3728         .pci_config             = qla4_8xxx_pci_config,
3729         .disable_intrs          = qla4_83xx_disable_intrs,
3730         .enable_intrs           = qla4_83xx_enable_intrs,
3731         .start_firmware         = qla4_8xxx_load_risc,
3732         .restart_firmware       = qla4_83xx_start_firmware,
3733         .intr_handler           = qla4_83xx_intr_handler,
3734         .interrupt_service_routine = qla4_83xx_interrupt_service_routine,
3735         .need_reset             = qla4_8xxx_need_reset,
3736         .reset_chip             = qla4_83xx_isp_reset,
3737         .reset_firmware         = qla4_8xxx_stop_firmware,
3738         .queue_iocb             = qla4_83xx_queue_iocb,
3739         .complete_iocb          = qla4_83xx_complete_iocb,
3740         .rd_shdw_req_q_out      = qla4_83xx_rd_shdw_req_q_out,
3741         .rd_shdw_rsp_q_in       = qla4_83xx_rd_shdw_rsp_q_in,
3742         .get_sys_info           = qla4_8xxx_get_sys_info,
3743         .rd_reg_direct          = qla4_83xx_rd_reg,
3744         .wr_reg_direct          = qla4_83xx_wr_reg,
3745         .rd_reg_indirect        = qla4_83xx_rd_reg_indirect,
3746         .wr_reg_indirect        = qla4_83xx_wr_reg_indirect,
3747         .idc_lock               = qla4_83xx_drv_lock,
3748         .idc_unlock             = qla4_83xx_drv_unlock,
3749         .rom_lock_recovery      = qla4_83xx_rom_lock_recovery,
3750         .queue_mailbox_command  = qla4_83xx_queue_mbox_cmd,
3751         .process_mailbox_interrupt = qla4_83xx_process_mbox_intr,
3752 };
3753
3754 uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
3755 {
3756         return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
3757 }
3758
3759 uint16_t qla4_82xx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
3760 {
3761         return (uint16_t)le32_to_cpu(readl(&ha->qla4_82xx_reg->req_q_out));
3762 }
3763
3764 uint16_t qla4_83xx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
3765 {
3766         return (uint16_t)le32_to_cpu(readl(&ha->qla4_83xx_reg->req_q_out));
3767 }
3768
3769 uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
3770 {
3771         return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
3772 }
3773
3774 uint16_t qla4_82xx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
3775 {
3776         return (uint16_t)le32_to_cpu(readl(&ha->qla4_82xx_reg->rsp_q_in));
3777 }
3778
3779 uint16_t qla4_83xx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
3780 {
3781         return (uint16_t)le32_to_cpu(readl(&ha->qla4_83xx_reg->rsp_q_in));
3782 }
3783
3784 static ssize_t qla4xxx_show_boot_eth_info(void *data, int type, char *buf)
3785 {
3786         struct scsi_qla_host *ha = data;
3787         char *str = buf;
3788         int rc;
3789
3790         switch (type) {
3791         case ISCSI_BOOT_ETH_FLAGS:
3792                 rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
3793                 break;
3794         case ISCSI_BOOT_ETH_INDEX:
3795                 rc = sprintf(str, "0\n");
3796                 break;
3797         case ISCSI_BOOT_ETH_MAC:
3798                 rc = sysfs_format_mac(str, ha->my_mac,
3799                                       MAC_ADDR_LEN);
3800                 break;
3801         default:
3802                 rc = -ENOSYS;
3803                 break;
3804         }
3805         return rc;
3806 }
3807
3808 static umode_t qla4xxx_eth_get_attr_visibility(void *data, int type)
3809 {
3810         int rc;
3811
3812         switch (type) {
3813         case ISCSI_BOOT_ETH_FLAGS:
3814         case ISCSI_BOOT_ETH_MAC:
3815         case ISCSI_BOOT_ETH_INDEX:
3816                 rc = S_IRUGO;
3817                 break;
3818         default:
3819                 rc = 0;
3820                 break;
3821         }
3822         return rc;
3823 }
3824
3825 static ssize_t qla4xxx_show_boot_ini_info(void *data, int type, char *buf)
3826 {
3827         struct scsi_qla_host *ha = data;
3828         char *str = buf;
3829         int rc;
3830
3831         switch (type) {
3832         case ISCSI_BOOT_INI_INITIATOR_NAME:
3833                 rc = sprintf(str, "%s\n", ha->name_string);
3834                 break;
3835         default:
3836                 rc = -ENOSYS;
3837                 break;
3838         }
3839         return rc;
3840 }
3841
3842 static umode_t qla4xxx_ini_get_attr_visibility(void *data, int type)
3843 {
3844         int rc;
3845
3846         switch (type) {
3847         case ISCSI_BOOT_INI_INITIATOR_NAME:
3848                 rc = S_IRUGO;
3849                 break;
3850         default:
3851                 rc = 0;
3852                 break;
3853         }
3854         return rc;
3855 }
3856
3857 static ssize_t
3858 qla4xxx_show_boot_tgt_info(struct ql4_boot_session_info *boot_sess, int type,
3859                            char *buf)
3860 {
3861         struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
3862         char *str = buf;
3863         int rc;
3864
3865         switch (type) {
3866         case ISCSI_BOOT_TGT_NAME:
3867                 rc = sprintf(buf, "%s\n", (char *)&boot_sess->target_name);
3868                 break;
3869         case ISCSI_BOOT_TGT_IP_ADDR:
3870                 if (boot_sess->conn_list[0].dest_ipaddr.ip_type == 0x1)
3871                         rc = sprintf(buf, "%pI4\n",
3872                                      &boot_conn->dest_ipaddr.ip_address);
3873                 else
3874                         rc = sprintf(str, "%pI6\n",
3875                                      &boot_conn->dest_ipaddr.ip_address);
3876                 break;
3877         case ISCSI_BOOT_TGT_PORT:
3878                         rc = sprintf(str, "%d\n", boot_conn->dest_port);
3879                 break;
3880         case ISCSI_BOOT_TGT_CHAP_NAME:
3881                 rc = sprintf(str,  "%.*s\n",
3882                              boot_conn->chap.target_chap_name_length,
3883                              (char *)&boot_conn->chap.target_chap_name);
3884                 break;
3885         case ISCSI_BOOT_TGT_CHAP_SECRET:
3886                 rc = sprintf(str,  "%.*s\n",
3887                              boot_conn->chap.target_secret_length,
3888                              (char *)&boot_conn->chap.target_secret);
3889                 break;
3890         case ISCSI_BOOT_TGT_REV_CHAP_NAME:
3891                 rc = sprintf(str,  "%.*s\n",
3892                              boot_conn->chap.intr_chap_name_length,
3893                              (char *)&boot_conn->chap.intr_chap_name);
3894                 break;
3895         case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
3896                 rc = sprintf(str,  "%.*s\n",
3897                              boot_conn->chap.intr_secret_length,
3898                              (char *)&boot_conn->chap.intr_secret);
3899                 break;
3900         case ISCSI_BOOT_TGT_FLAGS:
3901                 rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
3902                 break;
3903         case ISCSI_BOOT_TGT_NIC_ASSOC:
3904                 rc = sprintf(str, "0\n");
3905                 break;
3906         default:
3907                 rc = -ENOSYS;
3908                 break;
3909         }
3910         return rc;
3911 }
3912
3913 static ssize_t qla4xxx_show_boot_tgt_pri_info(void *data, int type, char *buf)
3914 {
3915         struct scsi_qla_host *ha = data;
3916         struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_pri_sess);
3917
3918         return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
3919 }
3920
3921 static ssize_t qla4xxx_show_boot_tgt_sec_info(void *data, int type, char *buf)
3922 {
3923         struct scsi_qla_host *ha = data;
3924         struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_sec_sess);
3925
3926         return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
3927 }
3928
3929 static umode_t qla4xxx_tgt_get_attr_visibility(void *data, int type)
3930 {
3931         int rc;
3932
3933         switch (type) {
3934         case ISCSI_BOOT_TGT_NAME:
3935         case ISCSI_BOOT_TGT_IP_ADDR:
3936         case ISCSI_BOOT_TGT_PORT:
3937         case ISCSI_BOOT_TGT_CHAP_NAME:
3938         case ISCSI_BOOT_TGT_CHAP_SECRET:
3939         case ISCSI_BOOT_TGT_REV_CHAP_NAME:
3940         case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
3941         case ISCSI_BOOT_TGT_NIC_ASSOC:
3942         case ISCSI_BOOT_TGT_FLAGS:
3943                 rc = S_IRUGO;
3944                 break;
3945         default:
3946                 rc = 0;
3947                 break;
3948         }
3949         return rc;
3950 }
3951
3952 static void qla4xxx_boot_release(void *data)
3953 {
3954         struct scsi_qla_host *ha = data;
3955
3956         scsi_host_put(ha->host);
3957 }
3958
3959 static int get_fw_boot_info(struct scsi_qla_host *ha, uint16_t ddb_index[])
3960 {
3961         dma_addr_t buf_dma;
3962         uint32_t addr, pri_addr, sec_addr;
3963         uint32_t offset;
3964         uint16_t func_num;
3965         uint8_t val;
3966         uint8_t *buf = NULL;
3967         size_t size = 13 * sizeof(uint8_t);
3968         int ret = QLA_SUCCESS;
3969
3970         func_num = PCI_FUNC(ha->pdev->devfn);
3971
3972         ql4_printk(KERN_INFO, ha, "%s: Get FW boot info for 0x%x func %d\n",
3973                    __func__, ha->pdev->device, func_num);
3974
3975         if (is_qla40XX(ha)) {
3976                 if (func_num == 1) {
3977                         addr = NVRAM_PORT0_BOOT_MODE;
3978                         pri_addr = NVRAM_PORT0_BOOT_PRI_TGT;
3979                         sec_addr = NVRAM_PORT0_BOOT_SEC_TGT;
3980                 } else if (func_num == 3) {
3981                         addr = NVRAM_PORT1_BOOT_MODE;
3982                         pri_addr = NVRAM_PORT1_BOOT_PRI_TGT;
3983                         sec_addr = NVRAM_PORT1_BOOT_SEC_TGT;
3984                 } else {
3985                         ret = QLA_ERROR;
3986                         goto exit_boot_info;
3987                 }
3988
3989                 /* Check Boot Mode */
3990                 val = rd_nvram_byte(ha, addr);
3991                 if (!(val & 0x07)) {
3992                         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Adapter boot "
3993                                           "options : 0x%x\n", __func__, val));
3994                         ret = QLA_ERROR;
3995                         goto exit_boot_info;
3996                 }
3997
3998                 /* get primary valid target index */
3999                 val = rd_nvram_byte(ha, pri_addr);
4000                 if (val & BIT_7)
4001                         ddb_index[0] = (val & 0x7f);
4002
4003                 /* get secondary valid target index */
4004                 val = rd_nvram_byte(ha, sec_addr);
4005                 if (val & BIT_7)
4006                         ddb_index[1] = (val & 0x7f);
4007
4008         } else if (is_qla8022(ha)) {
4009                 buf = dma_alloc_coherent(&ha->pdev->dev, size,
4010                                          &buf_dma, GFP_KERNEL);
4011                 if (!buf) {
4012                         DEBUG2(ql4_printk(KERN_ERR, ha,
4013                                           "%s: Unable to allocate dma buffer\n",
4014                                            __func__));
4015                         ret = QLA_ERROR;
4016                         goto exit_boot_info;
4017                 }
4018
4019                 if (ha->port_num == 0)
4020                         offset = BOOT_PARAM_OFFSET_PORT0;
4021                 else if (ha->port_num == 1)
4022                         offset = BOOT_PARAM_OFFSET_PORT1;
4023                 else {
4024                         ret = QLA_ERROR;
4025                         goto exit_boot_info_free;
4026                 }
4027                 addr = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_iscsi_param * 4) +
4028                        offset;
4029                 if (qla4xxx_get_flash(ha, buf_dma, addr,
4030                                       13 * sizeof(uint8_t)) != QLA_SUCCESS) {
4031                         DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: Get Flash"
4032                                           " failed\n", ha->host_no, __func__));
4033                         ret = QLA_ERROR;
4034                         goto exit_boot_info_free;
4035                 }
4036                 /* Check Boot Mode */
4037                 if (!(buf[1] & 0x07)) {
4038                         DEBUG2(ql4_printk(KERN_INFO, ha, "Firmware boot options"
4039                                           " : 0x%x\n", buf[1]));
4040                         ret = QLA_ERROR;
4041                         goto exit_boot_info_free;
4042                 }
4043
4044                 /* get primary valid target index */
4045                 if (buf[2] & BIT_7)
4046                         ddb_index[0] = buf[2] & 0x7f;
4047
4048                 /* get secondary valid target index */
4049                 if (buf[11] & BIT_7)
4050                         ddb_index[1] = buf[11] & 0x7f;
4051         } else {
4052                 ret = QLA_ERROR;
4053                 goto exit_boot_info;
4054         }
4055
4056         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Primary target ID %d, Secondary"
4057                           " target ID %d\n", __func__, ddb_index[0],
4058                           ddb_index[1]));
4059
4060 exit_boot_info_free:
4061         dma_free_coherent(&ha->pdev->dev, size, buf, buf_dma);
4062 exit_boot_info:
4063         ha->pri_ddb_idx = ddb_index[0];
4064         ha->sec_ddb_idx = ddb_index[1];
4065         return ret;
4066 }
4067
4068 /**
4069  * qla4xxx_get_bidi_chap - Get a BIDI CHAP user and password
4070  * @ha: pointer to adapter structure
4071  * @username: CHAP username to be returned
4072  * @password: CHAP password to be returned
4073  *
4074  * If a boot entry has BIDI CHAP enabled then we need to set the BIDI CHAP
4075  * user and password in the sysfs entry in /sys/firmware/iscsi_boot#/.
4076  * So from the CHAP cache find the first BIDI CHAP entry and set it
4077  * to the boot record in sysfs.
4078  **/
4079 static int qla4xxx_get_bidi_chap(struct scsi_qla_host *ha, char *username,
4080                             char *password)
4081 {
4082         int i, ret = -EINVAL;
4083         int max_chap_entries = 0;
4084         struct ql4_chap_table *chap_table;
4085
4086         if (is_qla8022(ha))
4087                 max_chap_entries = (ha->hw.flt_chap_size / 2) /
4088                                                 sizeof(struct ql4_chap_table);
4089         else
4090                 max_chap_entries = MAX_CHAP_ENTRIES_40XX;
4091
4092         if (!ha->chap_list) {
4093                 ql4_printk(KERN_ERR, ha, "Do not have CHAP table cache\n");
4094                 return ret;
4095         }
4096
4097         mutex_lock(&ha->chap_sem);
4098         for (i = 0; i < max_chap_entries; i++) {
4099                 chap_table = (struct ql4_chap_table *)ha->chap_list + i;
4100                 if (chap_table->cookie !=
4101                     __constant_cpu_to_le16(CHAP_VALID_COOKIE)) {
4102                         continue;
4103                 }
4104
4105                 if (chap_table->flags & BIT_7) /* local */
4106                         continue;
4107
4108                 if (!(chap_table->flags & BIT_6)) /* Not BIDI */
4109                         continue;
4110
4111                 strncpy(password, chap_table->secret, QL4_CHAP_MAX_SECRET_LEN);
4112                 strncpy(username, chap_table->name, QL4_CHAP_MAX_NAME_LEN);
4113                 ret = 0;
4114                 break;
4115         }
4116         mutex_unlock(&ha->chap_sem);
4117
4118         return ret;
4119 }
4120
4121
4122 static int qla4xxx_get_boot_target(struct scsi_qla_host *ha,
4123                                    struct ql4_boot_session_info *boot_sess,
4124                                    uint16_t ddb_index)
4125 {
4126         struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
4127         struct dev_db_entry *fw_ddb_entry;
4128         dma_addr_t fw_ddb_entry_dma;
4129         uint16_t idx;
4130         uint16_t options;
4131         int ret = QLA_SUCCESS;
4132
4133         fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
4134                                           &fw_ddb_entry_dma, GFP_KERNEL);
4135         if (!fw_ddb_entry) {
4136                 DEBUG2(ql4_printk(KERN_ERR, ha,
4137                                   "%s: Unable to allocate dma buffer.\n",
4138                                   __func__));
4139                 ret = QLA_ERROR;
4140                 return ret;
4141         }
4142
4143         if (qla4xxx_bootdb_by_index(ha, fw_ddb_entry,
4144                                    fw_ddb_entry_dma, ddb_index)) {
4145                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: No Flash DDB found at "
4146                                   "index [%d]\n", __func__, ddb_index));
4147                 ret = QLA_ERROR;
4148                 goto exit_boot_target;
4149         }
4150
4151         /* Update target name and IP from DDB */
4152         memcpy(boot_sess->target_name, fw_ddb_entry->iscsi_name,
4153                min(sizeof(boot_sess->target_name),
4154                    sizeof(fw_ddb_entry->iscsi_name)));
4155
4156         options = le16_to_cpu(fw_ddb_entry->options);
4157         if (options & DDB_OPT_IPV6_DEVICE) {
4158                 memcpy(&boot_conn->dest_ipaddr.ip_address,
4159                        &fw_ddb_entry->ip_addr[0], IPv6_ADDR_LEN);
4160         } else {
4161                 boot_conn->dest_ipaddr.ip_type = 0x1;
4162                 memcpy(&boot_conn->dest_ipaddr.ip_address,
4163                        &fw_ddb_entry->ip_addr[0], IP_ADDR_LEN);
4164         }
4165
4166         boot_conn->dest_port = le16_to_cpu(fw_ddb_entry->port);
4167
4168         /* update chap information */
4169         idx = __le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
4170
4171         if (BIT_7 & le16_to_cpu(fw_ddb_entry->iscsi_options))   {
4172
4173                 DEBUG2(ql4_printk(KERN_INFO, ha, "Setting chap\n"));
4174
4175                 ret = qla4xxx_get_chap(ha, (char *)&boot_conn->chap.
4176                                        target_chap_name,
4177                                        (char *)&boot_conn->chap.target_secret,
4178                                        idx);
4179                 if (ret) {
4180                         ql4_printk(KERN_ERR, ha, "Failed to set chap\n");
4181                         ret = QLA_ERROR;
4182                         goto exit_boot_target;
4183                 }
4184
4185                 boot_conn->chap.target_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
4186                 boot_conn->chap.target_secret_length = QL4_CHAP_MAX_SECRET_LEN;
4187         }
4188
4189         if (BIT_4 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
4190
4191                 DEBUG2(ql4_printk(KERN_INFO, ha, "Setting BIDI chap\n"));
4192
4193                 ret = qla4xxx_get_bidi_chap(ha,
4194                                     (char *)&boot_conn->chap.intr_chap_name,
4195                                     (char *)&boot_conn->chap.intr_secret);
4196
4197                 if (ret) {
4198                         ql4_printk(KERN_ERR, ha, "Failed to set BIDI chap\n");
4199                         ret = QLA_ERROR;
4200                         goto exit_boot_target;
4201                 }
4202
4203                 boot_conn->chap.intr_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
4204                 boot_conn->chap.intr_secret_length = QL4_CHAP_MAX_SECRET_LEN;
4205         }
4206
4207 exit_boot_target:
4208         dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
4209                           fw_ddb_entry, fw_ddb_entry_dma);
4210         return ret;
4211 }
4212
4213 static int qla4xxx_get_boot_info(struct scsi_qla_host *ha)
4214 {
4215         uint16_t ddb_index[2];
4216         int ret = QLA_ERROR;
4217         int rval;
4218
4219         memset(ddb_index, 0, sizeof(ddb_index));
4220         ddb_index[0] = 0xffff;
4221         ddb_index[1] = 0xffff;
4222         ret = get_fw_boot_info(ha, ddb_index);
4223         if (ret != QLA_SUCCESS) {
4224                 DEBUG2(ql4_printk(KERN_INFO, ha,
4225                                 "%s: No boot target configured.\n", __func__));
4226                 return ret;
4227         }
4228
4229         if (ql4xdisablesysfsboot)
4230                 return QLA_SUCCESS;
4231
4232         if (ddb_index[0] == 0xffff)
4233                 goto sec_target;
4234
4235         rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_pri_sess),
4236                                       ddb_index[0]);
4237         if (rval != QLA_SUCCESS) {
4238                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Primary boot target not "
4239                                   "configured\n", __func__));
4240         } else
4241                 ret = QLA_SUCCESS;
4242
4243 sec_target:
4244         if (ddb_index[1] == 0xffff)
4245                 goto exit_get_boot_info;
4246
4247         rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_sec_sess),
4248                                       ddb_index[1]);
4249         if (rval != QLA_SUCCESS) {
4250                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Secondary boot target not"
4251                                   " configured\n", __func__));
4252         } else
4253                 ret = QLA_SUCCESS;
4254
4255 exit_get_boot_info:
4256         return ret;
4257 }
4258
4259 static int qla4xxx_setup_boot_info(struct scsi_qla_host *ha)
4260 {
4261         struct iscsi_boot_kobj *boot_kobj;
4262
4263         if (qla4xxx_get_boot_info(ha) != QLA_SUCCESS)
4264                 return QLA_ERROR;
4265
4266         if (ql4xdisablesysfsboot) {
4267                 ql4_printk(KERN_INFO, ha,
4268                            "%s: syfsboot disabled - driver will trigger login "
4269                            "and publish session for discovery .\n", __func__);
4270                 return QLA_SUCCESS;
4271         }
4272
4273
4274         ha->boot_kset = iscsi_boot_create_host_kset(ha->host->host_no);
4275         if (!ha->boot_kset)
4276                 goto kset_free;
4277
4278         if (!scsi_host_get(ha->host))
4279                 goto kset_free;
4280         boot_kobj = iscsi_boot_create_target(ha->boot_kset, 0, ha,
4281                                              qla4xxx_show_boot_tgt_pri_info,
4282                                              qla4xxx_tgt_get_attr_visibility,
4283                                              qla4xxx_boot_release);
4284         if (!boot_kobj)
4285                 goto put_host;
4286
4287         if (!scsi_host_get(ha->host))
4288                 goto kset_free;
4289         boot_kobj = iscsi_boot_create_target(ha->boot_kset, 1, ha,
4290                                              qla4xxx_show_boot_tgt_sec_info,
4291                                              qla4xxx_tgt_get_attr_visibility,
4292                                              qla4xxx_boot_release);
4293         if (!boot_kobj)
4294                 goto put_host;
4295
4296         if (!scsi_host_get(ha->host))
4297                 goto kset_free;
4298         boot_kobj = iscsi_boot_create_initiator(ha->boot_kset, 0, ha,
4299                                                qla4xxx_show_boot_ini_info,
4300                                                qla4xxx_ini_get_attr_visibility,
4301                                                qla4xxx_boot_release);
4302         if (!boot_kobj)
4303                 goto put_host;
4304
4305         if (!scsi_host_get(ha->host))
4306                 goto kset_free;
4307         boot_kobj = iscsi_boot_create_ethernet(ha->boot_kset, 0, ha,
4308                                                qla4xxx_show_boot_eth_info,
4309                                                qla4xxx_eth_get_attr_visibility,
4310                                                qla4xxx_boot_release);
4311         if (!boot_kobj)
4312                 goto put_host;
4313
4314         return QLA_SUCCESS;
4315
4316 put_host:
4317         scsi_host_put(ha->host);
4318 kset_free:
4319         iscsi_boot_destroy_kset(ha->boot_kset);
4320         return -ENOMEM;
4321 }
4322
4323
4324 /**
4325  * qla4xxx_create chap_list - Create CHAP list from FLASH
4326  * @ha: pointer to adapter structure
4327  *
4328  * Read flash and make a list of CHAP entries, during login when a CHAP entry
4329  * is received, it will be checked in this list. If entry exist then the CHAP
4330  * entry index is set in the DDB. If CHAP entry does not exist in this list
4331  * then a new entry is added in FLASH in CHAP table and the index obtained is
4332  * used in the DDB.
4333  **/
4334 static void qla4xxx_create_chap_list(struct scsi_qla_host *ha)
4335 {
4336         int rval = 0;
4337         uint8_t *chap_flash_data = NULL;
4338         uint32_t offset;
4339         dma_addr_t chap_dma;
4340         uint32_t chap_size = 0;
4341
4342         if (is_qla40XX(ha))
4343                 chap_size = MAX_CHAP_ENTRIES_40XX  *
4344                                         sizeof(struct ql4_chap_table);
4345         else    /* Single region contains CHAP info for both
4346                  * ports which is divided into half for each port.
4347                  */
4348                 chap_size = ha->hw.flt_chap_size / 2;
4349
4350         chap_flash_data = dma_alloc_coherent(&ha->pdev->dev, chap_size,
4351                                           &chap_dma, GFP_KERNEL);
4352         if (!chap_flash_data) {
4353                 ql4_printk(KERN_ERR, ha, "No memory for chap_flash_data\n");
4354                 return;
4355         }
4356         if (is_qla40XX(ha))
4357                 offset = FLASH_CHAP_OFFSET;
4358         else {
4359                 offset = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_region_chap << 2);
4360                 if (ha->port_num == 1)
4361                         offset += chap_size;
4362         }
4363
4364         rval = qla4xxx_get_flash(ha, chap_dma, offset, chap_size);
4365         if (rval != QLA_SUCCESS)
4366                 goto exit_chap_list;
4367
4368         if (ha->chap_list == NULL)
4369                 ha->chap_list = vmalloc(chap_size);
4370         if (ha->chap_list == NULL) {
4371                 ql4_printk(KERN_ERR, ha, "No memory for ha->chap_list\n");
4372                 goto exit_chap_list;
4373         }
4374
4375         memcpy(ha->chap_list, chap_flash_data, chap_size);
4376
4377 exit_chap_list:
4378         dma_free_coherent(&ha->pdev->dev, chap_size,
4379                         chap_flash_data, chap_dma);
4380 }
4381
4382 static void qla4xxx_get_param_ddb(struct ddb_entry *ddb_entry,
4383                                   struct ql4_tuple_ddb *tddb)
4384 {
4385         struct scsi_qla_host *ha;
4386         struct iscsi_cls_session *cls_sess;
4387         struct iscsi_cls_conn *cls_conn;
4388         struct iscsi_session *sess;
4389         struct iscsi_conn *conn;
4390
4391         DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
4392         ha = ddb_entry->ha;
4393         cls_sess = ddb_entry->sess;
4394         sess = cls_sess->dd_data;
4395         cls_conn = ddb_entry->conn;
4396         conn = cls_conn->dd_data;
4397
4398         tddb->tpgt = sess->tpgt;
4399         tddb->port = conn->persistent_port;
4400         strncpy(tddb->iscsi_name, sess->targetname, ISCSI_NAME_SIZE);
4401         strncpy(tddb->ip_addr, conn->persistent_address, DDB_IPADDR_LEN);
4402 }
4403
4404 static void qla4xxx_convert_param_ddb(struct dev_db_entry *fw_ddb_entry,
4405                                       struct ql4_tuple_ddb *tddb,
4406                                       uint8_t *flash_isid)
4407 {
4408         uint16_t options = 0;
4409
4410         tddb->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
4411         memcpy(&tddb->iscsi_name[0], &fw_ddb_entry->iscsi_name[0],
4412                min(sizeof(tddb->iscsi_name), sizeof(fw_ddb_entry->iscsi_name)));
4413
4414         options = le16_to_cpu(fw_ddb_entry->options);
4415         if (options & DDB_OPT_IPV6_DEVICE)
4416                 sprintf(tddb->ip_addr, "%pI6", fw_ddb_entry->ip_addr);
4417         else
4418                 sprintf(tddb->ip_addr, "%pI4", fw_ddb_entry->ip_addr);
4419
4420         tddb->port = le16_to_cpu(fw_ddb_entry->port);
4421
4422         if (flash_isid == NULL)
4423                 memcpy(&tddb->isid[0], &fw_ddb_entry->isid[0],
4424                        sizeof(tddb->isid));
4425         else
4426                 memcpy(&tddb->isid[0], &flash_isid[0], sizeof(tddb->isid));
4427 }
4428
4429 static int qla4xxx_compare_tuple_ddb(struct scsi_qla_host *ha,
4430                                      struct ql4_tuple_ddb *old_tddb,
4431                                      struct ql4_tuple_ddb *new_tddb,
4432                                      uint8_t is_isid_compare)
4433 {
4434         if (strcmp(old_tddb->iscsi_name, new_tddb->iscsi_name))
4435                 return QLA_ERROR;
4436
4437         if (strcmp(old_tddb->ip_addr, new_tddb->ip_addr))
4438                 return QLA_ERROR;
4439
4440         if (old_tddb->port != new_tddb->port)
4441                 return QLA_ERROR;
4442
4443         /* For multi sessions, driver generates the ISID, so do not compare
4444          * ISID in reset path since it would be a comparison between the
4445          * driver generated ISID and firmware generated ISID. This could
4446          * lead to adding duplicated DDBs in the list as driver generated
4447          * ISID would not match firmware generated ISID.
4448          */
4449         if (is_isid_compare) {
4450                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: old ISID [%02x%02x%02x"
4451                         "%02x%02x%02x] New ISID [%02x%02x%02x%02x%02x%02x]\n",
4452                         __func__, old_tddb->isid[5], old_tddb->isid[4],
4453                         old_tddb->isid[3], old_tddb->isid[2], old_tddb->isid[1],
4454                         old_tddb->isid[0], new_tddb->isid[5], new_tddb->isid[4],
4455                         new_tddb->isid[3], new_tddb->isid[2], new_tddb->isid[1],
4456                         new_tddb->isid[0]));
4457
4458                 if (memcmp(&old_tddb->isid[0], &new_tddb->isid[0],
4459                            sizeof(old_tddb->isid)))
4460                         return QLA_ERROR;
4461         }
4462
4463         DEBUG2(ql4_printk(KERN_INFO, ha,
4464                           "Match Found, fw[%d,%d,%s,%s], [%d,%d,%s,%s]",
4465                           old_tddb->port, old_tddb->tpgt, old_tddb->ip_addr,
4466                           old_tddb->iscsi_name, new_tddb->port, new_tddb->tpgt,
4467                           new_tddb->ip_addr, new_tddb->iscsi_name));
4468
4469         return QLA_SUCCESS;
4470 }
4471
4472 static int qla4xxx_is_session_exists(struct scsi_qla_host *ha,
4473                                      struct dev_db_entry *fw_ddb_entry)
4474 {
4475         struct ddb_entry *ddb_entry;
4476         struct ql4_tuple_ddb *fw_tddb = NULL;
4477         struct ql4_tuple_ddb *tmp_tddb = NULL;
4478         int idx;
4479         int ret = QLA_ERROR;
4480
4481         fw_tddb = vzalloc(sizeof(*fw_tddb));
4482         if (!fw_tddb) {
4483                 DEBUG2(ql4_printk(KERN_WARNING, ha,
4484                                   "Memory Allocation failed.\n"));
4485                 ret = QLA_SUCCESS;
4486                 goto exit_check;
4487         }
4488
4489         tmp_tddb = vzalloc(sizeof(*tmp_tddb));
4490         if (!tmp_tddb) {
4491                 DEBUG2(ql4_printk(KERN_WARNING, ha,
4492                                   "Memory Allocation failed.\n"));
4493                 ret = QLA_SUCCESS;
4494                 goto exit_check;
4495         }
4496
4497         qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb, NULL);
4498
4499         for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
4500                 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
4501                 if (ddb_entry == NULL)
4502                         continue;
4503
4504                 qla4xxx_get_param_ddb(ddb_entry, tmp_tddb);
4505                 if (!qla4xxx_compare_tuple_ddb(ha, fw_tddb, tmp_tddb, false)) {
4506                         ret = QLA_SUCCESS; /* found */
4507                         goto exit_check;
4508                 }
4509         }
4510
4511 exit_check:
4512         if (fw_tddb)
4513                 vfree(fw_tddb);
4514         if (tmp_tddb)
4515                 vfree(tmp_tddb);
4516         return ret;
4517 }
4518
4519 /**
4520  * qla4xxx_check_existing_isid - check if target with same isid exist
4521  *                               in target list
4522  * @list_nt: list of target
4523  * @isid: isid to check
4524  *
4525  * This routine return QLA_SUCCESS if target with same isid exist
4526  **/
4527 static int qla4xxx_check_existing_isid(struct list_head *list_nt, uint8_t *isid)
4528 {
4529         struct qla_ddb_index *nt_ddb_idx, *nt_ddb_idx_tmp;
4530         struct dev_db_entry *fw_ddb_entry;
4531
4532         list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
4533                 fw_ddb_entry = &nt_ddb_idx->fw_ddb;
4534
4535                 if (memcmp(&fw_ddb_entry->isid[0], &isid[0],
4536                            sizeof(nt_ddb_idx->fw_ddb.isid)) == 0) {
4537                         return QLA_SUCCESS;
4538                 }
4539         }
4540         return QLA_ERROR;
4541 }
4542
4543 /**
4544  * qla4xxx_update_isid - compare ddbs and updated isid
4545  * @ha: Pointer to host adapter structure.
4546  * @list_nt: list of nt target
4547  * @fw_ddb_entry: firmware ddb entry
4548  *
4549  * This routine update isid if ddbs have same iqn, same isid and
4550  * different IP addr.
4551  * Return QLA_SUCCESS if isid is updated.
4552  **/
4553 static int qla4xxx_update_isid(struct scsi_qla_host *ha,
4554                                struct list_head *list_nt,
4555                                struct dev_db_entry *fw_ddb_entry)
4556 {
4557         uint8_t base_value, i;
4558
4559         base_value = fw_ddb_entry->isid[1] & 0x1f;
4560         for (i = 0; i < 8; i++) {
4561                 fw_ddb_entry->isid[1] = (base_value | (i << 5));
4562                 if (qla4xxx_check_existing_isid(list_nt, fw_ddb_entry->isid))
4563                         break;
4564         }
4565
4566         if (!qla4xxx_check_existing_isid(list_nt, fw_ddb_entry->isid))
4567                 return QLA_ERROR;
4568
4569         return QLA_SUCCESS;
4570 }
4571
4572 /**
4573  * qla4xxx_should_update_isid - check if isid need to update
4574  * @ha: Pointer to host adapter structure.
4575  * @old_tddb: ddb tuple
4576  * @new_tddb: ddb tuple
4577  *
4578  * Return QLA_SUCCESS if different IP, different PORT, same iqn,
4579  * same isid
4580  **/
4581 static int qla4xxx_should_update_isid(struct scsi_qla_host *ha,
4582                                       struct ql4_tuple_ddb *old_tddb,
4583                                       struct ql4_tuple_ddb *new_tddb)
4584 {
4585         if (strcmp(old_tddb->ip_addr, new_tddb->ip_addr) == 0) {
4586                 /* Same ip */
4587                 if (old_tddb->port == new_tddb->port)
4588                         return QLA_ERROR;
4589         }
4590
4591         if (strcmp(old_tddb->iscsi_name, new_tddb->iscsi_name))
4592                 /* different iqn */
4593                 return QLA_ERROR;
4594
4595         if (memcmp(&old_tddb->isid[0], &new_tddb->isid[0],
4596                    sizeof(old_tddb->isid)))
4597                 /* different isid */
4598                 return QLA_ERROR;
4599
4600         return QLA_SUCCESS;
4601 }
4602
4603 /**
4604  * qla4xxx_is_flash_ddb_exists - check if fw_ddb_entry already exists in list_nt
4605  * @ha: Pointer to host adapter structure.
4606  * @list_nt: list of nt target.
4607  * @fw_ddb_entry: firmware ddb entry.
4608  *
4609  * This routine check if fw_ddb_entry already exists in list_nt to avoid
4610  * duplicate ddb in list_nt.
4611  * Return QLA_SUCCESS if duplicate ddb exit in list_nl.
4612  * Note: This function also update isid of DDB if required.
4613  **/
4614
4615 static int qla4xxx_is_flash_ddb_exists(struct scsi_qla_host *ha,
4616                                        struct list_head *list_nt,
4617                                        struct dev_db_entry *fw_ddb_entry)
4618 {
4619         struct qla_ddb_index  *nt_ddb_idx, *nt_ddb_idx_tmp;
4620         struct ql4_tuple_ddb *fw_tddb = NULL;
4621         struct ql4_tuple_ddb *tmp_tddb = NULL;
4622         int rval, ret = QLA_ERROR;
4623
4624         fw_tddb = vzalloc(sizeof(*fw_tddb));
4625         if (!fw_tddb) {
4626                 DEBUG2(ql4_printk(KERN_WARNING, ha,
4627                                   "Memory Allocation failed.\n"));
4628                 ret = QLA_SUCCESS;
4629                 goto exit_check;
4630         }
4631
4632         tmp_tddb = vzalloc(sizeof(*tmp_tddb));
4633         if (!tmp_tddb) {
4634                 DEBUG2(ql4_printk(KERN_WARNING, ha,
4635                                   "Memory Allocation failed.\n"));
4636                 ret = QLA_SUCCESS;
4637                 goto exit_check;
4638         }
4639
4640         qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb, NULL);
4641
4642         list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
4643                 qla4xxx_convert_param_ddb(&nt_ddb_idx->fw_ddb, tmp_tddb,
4644                                           nt_ddb_idx->flash_isid);
4645                 ret = qla4xxx_compare_tuple_ddb(ha, fw_tddb, tmp_tddb, true);
4646                 /* found duplicate ddb */
4647                 if (ret == QLA_SUCCESS)
4648                         goto exit_check;
4649         }
4650
4651         list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
4652                 qla4xxx_convert_param_ddb(&nt_ddb_idx->fw_ddb, tmp_tddb, NULL);
4653
4654                 ret = qla4xxx_should_update_isid(ha, tmp_tddb, fw_tddb);
4655                 if (ret == QLA_SUCCESS) {
4656                         rval = qla4xxx_update_isid(ha, list_nt, fw_ddb_entry);
4657                         if (rval == QLA_SUCCESS)
4658                                 ret = QLA_ERROR;
4659                         else
4660                                 ret = QLA_SUCCESS;
4661
4662                         goto exit_check;
4663                 }
4664         }
4665
4666 exit_check:
4667         if (fw_tddb)
4668                 vfree(fw_tddb);
4669         if (tmp_tddb)
4670                 vfree(tmp_tddb);
4671         return ret;
4672 }
4673
4674 static void qla4xxx_free_ddb_list(struct list_head *list_ddb)
4675 {
4676         struct qla_ddb_index  *ddb_idx, *ddb_idx_tmp;
4677
4678         list_for_each_entry_safe(ddb_idx, ddb_idx_tmp, list_ddb, list) {
4679                 list_del_init(&ddb_idx->list);
4680                 vfree(ddb_idx);
4681         }
4682 }
4683
4684 static struct iscsi_endpoint *qla4xxx_get_ep_fwdb(struct scsi_qla_host *ha,
4685                                         struct dev_db_entry *fw_ddb_entry)
4686 {
4687         struct iscsi_endpoint *ep;
4688         struct sockaddr_in *addr;
4689         struct sockaddr_in6 *addr6;
4690         struct sockaddr *t_addr;
4691         struct sockaddr_storage *dst_addr;
4692         char *ip;
4693
4694         /* TODO: need to destroy on unload iscsi_endpoint*/
4695         dst_addr = vmalloc(sizeof(*dst_addr));
4696         if (!dst_addr)
4697                 return NULL;
4698
4699         if (fw_ddb_entry->options & DDB_OPT_IPV6_DEVICE) {
4700                 t_addr = (struct sockaddr *)dst_addr;
4701                 t_addr->sa_family = AF_INET6;
4702                 addr6 = (struct sockaddr_in6 *)dst_addr;
4703                 ip = (char *)&addr6->sin6_addr;
4704                 memcpy(ip, fw_ddb_entry->ip_addr, IPv6_ADDR_LEN);
4705                 addr6->sin6_port = htons(le16_to_cpu(fw_ddb_entry->port));
4706
4707         } else {
4708                 t_addr = (struct sockaddr *)dst_addr;
4709                 t_addr->sa_family = AF_INET;
4710                 addr = (struct sockaddr_in *)dst_addr;
4711                 ip = (char *)&addr->sin_addr;
4712                 memcpy(ip, fw_ddb_entry->ip_addr, IP_ADDR_LEN);
4713                 addr->sin_port = htons(le16_to_cpu(fw_ddb_entry->port));
4714         }
4715
4716         ep = qla4xxx_ep_connect(ha->host, (struct sockaddr *)dst_addr, 0);
4717         vfree(dst_addr);
4718         return ep;
4719 }
4720
4721 static int qla4xxx_verify_boot_idx(struct scsi_qla_host *ha, uint16_t idx)
4722 {
4723         if (ql4xdisablesysfsboot)
4724                 return QLA_SUCCESS;
4725         if (idx == ha->pri_ddb_idx || idx == ha->sec_ddb_idx)
4726                 return QLA_ERROR;
4727         return QLA_SUCCESS;
4728 }
4729
4730 static void qla4xxx_setup_flash_ddb_entry(struct scsi_qla_host *ha,
4731                                           struct ddb_entry *ddb_entry,
4732                                           uint16_t idx)
4733 {
4734         uint16_t def_timeout;
4735
4736         ddb_entry->ddb_type = FLASH_DDB;
4737         ddb_entry->fw_ddb_index = INVALID_ENTRY;
4738         ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
4739         ddb_entry->ha = ha;
4740         ddb_entry->unblock_sess = qla4xxx_unblock_flash_ddb;
4741         ddb_entry->ddb_change = qla4xxx_flash_ddb_change;
4742
4743         atomic_set(&ddb_entry->retry_relogin_timer, INVALID_ENTRY);
4744         atomic_set(&ddb_entry->relogin_timer, 0);
4745         atomic_set(&ddb_entry->relogin_retry_count, 0);
4746         def_timeout = le16_to_cpu(ddb_entry->fw_ddb_entry.def_timeout);
4747         ddb_entry->default_relogin_timeout =
4748                 (def_timeout > LOGIN_TOV) && (def_timeout < LOGIN_TOV * 10) ?
4749                 def_timeout : LOGIN_TOV;
4750         ddb_entry->default_time2wait =
4751                 le16_to_cpu(ddb_entry->fw_ddb_entry.iscsi_def_time2wait);
4752
4753         if (ql4xdisablesysfsboot &&
4754             (idx == ha->pri_ddb_idx || idx == ha->sec_ddb_idx))
4755                 set_bit(DF_BOOT_TGT, &ddb_entry->flags);
4756 }
4757
4758 static void qla4xxx_wait_for_ip_configuration(struct scsi_qla_host *ha)
4759 {
4760         uint32_t idx = 0;
4761         uint32_t ip_idx[IP_ADDR_COUNT] = {0, 1, 2, 3}; /* 4 IP interfaces */
4762         uint32_t sts[MBOX_REG_COUNT];
4763         uint32_t ip_state;
4764         unsigned long wtime;
4765         int ret;
4766
4767         wtime = jiffies + (HZ * IP_CONFIG_TOV);
4768         do {
4769                 for (idx = 0; idx < IP_ADDR_COUNT; idx++) {
4770                         if (ip_idx[idx] == -1)
4771                                 continue;
4772
4773                         ret = qla4xxx_get_ip_state(ha, 0, ip_idx[idx], sts);
4774
4775                         if (ret == QLA_ERROR) {
4776                                 ip_idx[idx] = -1;
4777                                 continue;
4778                         }
4779
4780                         ip_state = (sts[1] & IP_STATE_MASK) >> IP_STATE_SHIFT;
4781
4782                         DEBUG2(ql4_printk(KERN_INFO, ha,
4783                                           "Waiting for IP state for idx = %d, state = 0x%x\n",
4784                                           ip_idx[idx], ip_state));
4785                         if (ip_state == IP_ADDRSTATE_UNCONFIGURED ||
4786                             ip_state == IP_ADDRSTATE_INVALID ||
4787                             ip_state == IP_ADDRSTATE_PREFERRED ||
4788                             ip_state == IP_ADDRSTATE_DEPRICATED ||
4789                             ip_state == IP_ADDRSTATE_DISABLING)
4790                                 ip_idx[idx] = -1;
4791                 }
4792
4793                 /* Break if all IP states checked */
4794                 if ((ip_idx[0] == -1) &&
4795                     (ip_idx[1] == -1) &&
4796                     (ip_idx[2] == -1) &&
4797                     (ip_idx[3] == -1))
4798                         break;
4799                 schedule_timeout_uninterruptible(HZ);
4800         } while (time_after(wtime, jiffies));
4801 }
4802
4803 static void qla4xxx_build_st_list(struct scsi_qla_host *ha,
4804                                   struct list_head *list_st)
4805 {
4806         struct qla_ddb_index  *st_ddb_idx;
4807         int max_ddbs;
4808         int fw_idx_size;
4809         struct dev_db_entry *fw_ddb_entry;
4810         dma_addr_t fw_ddb_dma;
4811         int ret;
4812         uint32_t idx = 0, next_idx = 0;
4813         uint32_t state = 0, conn_err = 0;
4814         uint16_t conn_id = 0;
4815
4816         fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
4817                                       &fw_ddb_dma);
4818         if (fw_ddb_entry == NULL) {
4819                 DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
4820                 goto exit_st_list;
4821         }
4822
4823         max_ddbs =  is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
4824                                      MAX_DEV_DB_ENTRIES;
4825         fw_idx_size = sizeof(struct qla_ddb_index);
4826
4827         for (idx = 0; idx < max_ddbs; idx = next_idx) {
4828                 ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry, fw_ddb_dma,
4829                                               NULL, &next_idx, &state,
4830                                               &conn_err, NULL, &conn_id);
4831                 if (ret == QLA_ERROR)
4832                         break;
4833
4834                 /* Ignore DDB if invalid state (unassigned) */
4835                 if (state == DDB_DS_UNASSIGNED)
4836                         goto continue_next_st;
4837
4838                 /* Check if ST, add to the list_st */
4839                 if (strlen((char *) fw_ddb_entry->iscsi_name) != 0)
4840                         goto continue_next_st;
4841
4842                 st_ddb_idx = vzalloc(fw_idx_size);
4843                 if (!st_ddb_idx)
4844                         break;
4845
4846                 st_ddb_idx->fw_ddb_idx = idx;
4847
4848                 list_add_tail(&st_ddb_idx->list, list_st);
4849 continue_next_st:
4850                 if (next_idx == 0)
4851                         break;
4852         }
4853
4854 exit_st_list:
4855         if (fw_ddb_entry)
4856                 dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
4857 }
4858
4859 /**
4860  * qla4xxx_remove_failed_ddb - Remove inactive or failed ddb from list
4861  * @ha: pointer to adapter structure
4862  * @list_ddb: List from which failed ddb to be removed
4863  *
4864  * Iterate over the list of DDBs and find and remove DDBs that are either in
4865  * no connection active state or failed state
4866  **/
4867 static void qla4xxx_remove_failed_ddb(struct scsi_qla_host *ha,
4868                                       struct list_head *list_ddb)
4869 {
4870         struct qla_ddb_index  *ddb_idx, *ddb_idx_tmp;
4871         uint32_t next_idx = 0;
4872         uint32_t state = 0, conn_err = 0;
4873         int ret;
4874
4875         list_for_each_entry_safe(ddb_idx, ddb_idx_tmp, list_ddb, list) {
4876                 ret = qla4xxx_get_fwddb_entry(ha, ddb_idx->fw_ddb_idx,
4877                                               NULL, 0, NULL, &next_idx, &state,
4878                                               &conn_err, NULL, NULL);
4879                 if (ret == QLA_ERROR)
4880                         continue;
4881
4882                 if (state == DDB_DS_NO_CONNECTION_ACTIVE ||
4883                     state == DDB_DS_SESSION_FAILED) {
4884                         list_del_init(&ddb_idx->list);
4885                         vfree(ddb_idx);
4886                 }
4887         }
4888 }
4889
4890 static int qla4xxx_sess_conn_setup(struct scsi_qla_host *ha,
4891                                    struct dev_db_entry *fw_ddb_entry,
4892                                    int is_reset, uint16_t idx)
4893 {
4894         struct iscsi_cls_session *cls_sess;
4895         struct iscsi_session *sess;
4896         struct iscsi_cls_conn *cls_conn;
4897         struct iscsi_endpoint *ep;
4898         uint16_t cmds_max = 32;
4899         uint16_t conn_id = 0;
4900         uint32_t initial_cmdsn = 0;
4901         int ret = QLA_SUCCESS;
4902
4903         struct ddb_entry *ddb_entry = NULL;
4904
4905         /* Create session object, with INVALID_ENTRY,
4906          * the targer_id would get set when we issue the login
4907          */
4908         cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport, ha->host,
4909                                        cmds_max, sizeof(struct ddb_entry),
4910                                        sizeof(struct ql4_task_data),
4911                                        initial_cmdsn, INVALID_ENTRY);
4912         if (!cls_sess) {
4913                 ret = QLA_ERROR;
4914                 goto exit_setup;
4915         }
4916
4917         /*
4918          * so calling module_put function to decrement the
4919          * reference count.
4920          **/
4921         module_put(qla4xxx_iscsi_transport.owner);
4922         sess = cls_sess->dd_data;
4923         ddb_entry = sess->dd_data;
4924         ddb_entry->sess = cls_sess;
4925
4926         cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
4927         memcpy(&ddb_entry->fw_ddb_entry, fw_ddb_entry,
4928                sizeof(struct dev_db_entry));
4929
4930         qla4xxx_setup_flash_ddb_entry(ha, ddb_entry, idx);
4931
4932         cls_conn = iscsi_conn_setup(cls_sess, sizeof(struct qla_conn), conn_id);
4933
4934         if (!cls_conn) {
4935                 ret = QLA_ERROR;
4936                 goto exit_setup;
4937         }
4938
4939         ddb_entry->conn = cls_conn;
4940
4941         /* Setup ep, for displaying attributes in sysfs */
4942         ep = qla4xxx_get_ep_fwdb(ha, fw_ddb_entry);
4943         if (ep) {
4944                 ep->conn = cls_conn;
4945                 cls_conn->ep = ep;
4946         } else {
4947                 DEBUG2(ql4_printk(KERN_ERR, ha, "Unable to get ep\n"));
4948                 ret = QLA_ERROR;
4949                 goto exit_setup;
4950         }
4951
4952         /* Update sess/conn params */
4953         qla4xxx_copy_fwddb_param(ha, fw_ddb_entry, cls_sess, cls_conn);
4954
4955         if (is_reset == RESET_ADAPTER) {
4956                 iscsi_block_session(cls_sess);
4957                 /* Use the relogin path to discover new devices
4958                  *  by short-circuting the logic of setting
4959                  *  timer to relogin - instead set the flags
4960                  *  to initiate login right away.
4961                  */
4962                 set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
4963                 set_bit(DF_RELOGIN, &ddb_entry->flags);
4964         }
4965
4966 exit_setup:
4967         return ret;
4968 }
4969
4970 static void qla4xxx_build_nt_list(struct scsi_qla_host *ha,
4971                                   struct list_head *list_nt, int is_reset)
4972 {
4973         struct dev_db_entry *fw_ddb_entry;
4974         dma_addr_t fw_ddb_dma;
4975         int max_ddbs;
4976         int fw_idx_size;
4977         int ret;
4978         uint32_t idx = 0, next_idx = 0;
4979         uint32_t state = 0, conn_err = 0;
4980         uint16_t conn_id = 0;
4981         struct qla_ddb_index  *nt_ddb_idx;
4982
4983         fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
4984                                       &fw_ddb_dma);
4985         if (fw_ddb_entry == NULL) {
4986                 DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
4987                 goto exit_nt_list;
4988         }
4989         max_ddbs =  is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
4990                                      MAX_DEV_DB_ENTRIES;
4991         fw_idx_size = sizeof(struct qla_ddb_index);
4992
4993         for (idx = 0; idx < max_ddbs; idx = next_idx) {
4994                 ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry, fw_ddb_dma,
4995                                               NULL, &next_idx, &state,
4996                                               &conn_err, NULL, &conn_id);
4997                 if (ret == QLA_ERROR)
4998                         break;
4999
5000                 if (qla4xxx_verify_boot_idx(ha, idx) != QLA_SUCCESS)
5001                         goto continue_next_nt;
5002
5003                 /* Check if NT, then add to list it */
5004                 if (strlen((char *) fw_ddb_entry->iscsi_name) == 0)
5005                         goto continue_next_nt;
5006
5007                 if (!(state == DDB_DS_NO_CONNECTION_ACTIVE ||
5008                     state == DDB_DS_SESSION_FAILED))
5009                         goto continue_next_nt;
5010
5011                 DEBUG2(ql4_printk(KERN_INFO, ha,
5012                                   "Adding  DDB to session = 0x%x\n", idx));
5013                 if (is_reset == INIT_ADAPTER) {
5014                         nt_ddb_idx = vmalloc(fw_idx_size);
5015                         if (!nt_ddb_idx)
5016                                 break;
5017
5018                         nt_ddb_idx->fw_ddb_idx = idx;
5019
5020                         /* Copy original isid as it may get updated in function
5021                          * qla4xxx_update_isid(). We need original isid in
5022                          * function qla4xxx_compare_tuple_ddb to find duplicate
5023                          * target */
5024                         memcpy(&nt_ddb_idx->flash_isid[0],
5025                                &fw_ddb_entry->isid[0],
5026                                sizeof(nt_ddb_idx->flash_isid));
5027
5028                         ret = qla4xxx_is_flash_ddb_exists(ha, list_nt,
5029                                                           fw_ddb_entry);
5030                         if (ret == QLA_SUCCESS) {
5031                                 /* free nt_ddb_idx and do not add to list_nt */
5032                                 vfree(nt_ddb_idx);
5033                                 goto continue_next_nt;
5034                         }
5035
5036                         /* Copy updated isid */
5037                         memcpy(&nt_ddb_idx->fw_ddb, fw_ddb_entry,
5038                                sizeof(struct dev_db_entry));
5039
5040                         list_add_tail(&nt_ddb_idx->list, list_nt);
5041                 } else if (is_reset == RESET_ADAPTER) {
5042                         if (qla4xxx_is_session_exists(ha, fw_ddb_entry) ==
5043                                                                 QLA_SUCCESS)
5044                                 goto continue_next_nt;
5045                 }
5046
5047                 ret = qla4xxx_sess_conn_setup(ha, fw_ddb_entry, is_reset, idx);
5048                 if (ret == QLA_ERROR)
5049                         goto exit_nt_list;
5050
5051 continue_next_nt:
5052                 if (next_idx == 0)
5053                         break;
5054         }
5055
5056 exit_nt_list:
5057         if (fw_ddb_entry)
5058                 dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
5059 }
5060
5061 /**
5062  * qla4xxx_build_ddb_list - Build ddb list and setup sessions
5063  * @ha: pointer to adapter structure
5064  * @is_reset: Is this init path or reset path
5065  *
5066  * Create a list of sendtargets (st) from firmware DDBs, issue send targets
5067  * using connection open, then create the list of normal targets (nt)
5068  * from firmware DDBs. Based on the list of nt setup session and connection
5069  * objects.
5070  **/
5071 void qla4xxx_build_ddb_list(struct scsi_qla_host *ha, int is_reset)
5072 {
5073         uint16_t tmo = 0;
5074         struct list_head list_st, list_nt;
5075         struct qla_ddb_index  *st_ddb_idx, *st_ddb_idx_tmp;
5076         unsigned long wtime;
5077
5078         if (!test_bit(AF_LINK_UP, &ha->flags)) {
5079                 set_bit(AF_BUILD_DDB_LIST, &ha->flags);
5080                 ha->is_reset = is_reset;
5081                 return;
5082         }
5083
5084         INIT_LIST_HEAD(&list_st);
5085         INIT_LIST_HEAD(&list_nt);
5086
5087         qla4xxx_build_st_list(ha, &list_st);
5088
5089         /* Before issuing conn open mbox, ensure all IPs states are configured
5090          * Note, conn open fails if IPs are not configured
5091          */
5092         qla4xxx_wait_for_ip_configuration(ha);
5093
5094         /* Go thru the STs and fire the sendtargets by issuing conn open mbx */
5095         list_for_each_entry_safe(st_ddb_idx, st_ddb_idx_tmp, &list_st, list) {
5096                 qla4xxx_conn_open(ha, st_ddb_idx->fw_ddb_idx);
5097         }
5098
5099         /* Wait to ensure all sendtargets are done for min 12 sec wait */
5100         tmo = ((ha->def_timeout > LOGIN_TOV) &&
5101                (ha->def_timeout < LOGIN_TOV * 10) ?
5102                ha->def_timeout : LOGIN_TOV);
5103
5104         DEBUG2(ql4_printk(KERN_INFO, ha,
5105                           "Default time to wait for build ddb %d\n", tmo));
5106
5107         wtime = jiffies + (HZ * tmo);
5108         do {
5109                 if (list_empty(&list_st))
5110                         break;
5111
5112                 qla4xxx_remove_failed_ddb(ha, &list_st);
5113                 schedule_timeout_uninterruptible(HZ / 10);
5114         } while (time_after(wtime, jiffies));
5115
5116         /* Free up the sendtargets list */
5117         qla4xxx_free_ddb_list(&list_st);
5118
5119         qla4xxx_build_nt_list(ha, &list_nt, is_reset);
5120
5121         qla4xxx_free_ddb_list(&list_nt);
5122
5123         qla4xxx_free_ddb_index(ha);
5124 }
5125
5126 /**
5127  * qla4xxx_wait_login_resp_boot_tgt -  Wait for iSCSI boot target login
5128  * response.
5129  * @ha: pointer to adapter structure
5130  *
5131  * When the boot entry is normal iSCSI target then DF_BOOT_TGT flag will be
5132  * set in DDB and we will wait for login response of boot targets during
5133  * probe.
5134  **/
5135 static void qla4xxx_wait_login_resp_boot_tgt(struct scsi_qla_host *ha)
5136 {
5137         struct ddb_entry *ddb_entry;
5138         struct dev_db_entry *fw_ddb_entry = NULL;
5139         dma_addr_t fw_ddb_entry_dma;
5140         unsigned long wtime;
5141         uint32_t ddb_state;
5142         int max_ddbs, idx, ret;
5143
5144         max_ddbs =  is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
5145                                      MAX_DEV_DB_ENTRIES;
5146
5147         fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
5148                                           &fw_ddb_entry_dma, GFP_KERNEL);
5149         if (!fw_ddb_entry) {
5150                 ql4_printk(KERN_ERR, ha,
5151                            "%s: Unable to allocate dma buffer\n", __func__);
5152                 goto exit_login_resp;
5153         }
5154
5155         wtime = jiffies + (HZ * BOOT_LOGIN_RESP_TOV);
5156
5157         for (idx = 0; idx < max_ddbs; idx++) {
5158                 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
5159                 if (ddb_entry == NULL)
5160                         continue;
5161
5162                 if (test_bit(DF_BOOT_TGT, &ddb_entry->flags)) {
5163                         DEBUG2(ql4_printk(KERN_INFO, ha,
5164                                           "%s: DDB index [%d]\n", __func__,
5165                                           ddb_entry->fw_ddb_index));
5166                         do {
5167                                 ret = qla4xxx_get_fwddb_entry(ha,
5168                                                 ddb_entry->fw_ddb_index,
5169                                                 fw_ddb_entry, fw_ddb_entry_dma,
5170                                                 NULL, NULL, &ddb_state, NULL,
5171                                                 NULL, NULL);
5172                                 if (ret == QLA_ERROR)
5173                                         goto exit_login_resp;
5174
5175                                 if ((ddb_state == DDB_DS_SESSION_ACTIVE) ||
5176                                     (ddb_state == DDB_DS_SESSION_FAILED))
5177                                         break;
5178
5179                                 schedule_timeout_uninterruptible(HZ);
5180
5181                         } while ((time_after(wtime, jiffies)));
5182
5183                         if (!time_after(wtime, jiffies)) {
5184                                 DEBUG2(ql4_printk(KERN_INFO, ha,
5185                                                   "%s: Login response wait timer expired\n",
5186                                                   __func__));
5187                                  goto exit_login_resp;
5188                         }
5189                 }
5190         }
5191
5192 exit_login_resp:
5193         if (fw_ddb_entry)
5194                 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
5195                                   fw_ddb_entry, fw_ddb_entry_dma);
5196 }
5197
5198 /**
5199  * qla4xxx_probe_adapter - callback function to probe HBA
5200  * @pdev: pointer to pci_dev structure
5201  * @pci_device_id: pointer to pci_device entry
5202  *
5203  * This routine will probe for Qlogic 4xxx iSCSI host adapters.
5204  * It returns zero if successful. It also initializes all data necessary for
5205  * the driver.
5206  **/
5207 static int qla4xxx_probe_adapter(struct pci_dev *pdev,
5208                                  const struct pci_device_id *ent)
5209 {
5210         int ret = -ENODEV, status;
5211         struct Scsi_Host *host;
5212         struct scsi_qla_host *ha;
5213         uint8_t init_retry_count = 0;
5214         char buf[34];
5215         struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
5216         uint32_t dev_state;
5217
5218         if (pci_enable_device(pdev))
5219                 return -1;
5220
5221         host = iscsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha), 0);
5222         if (host == NULL) {
5223                 printk(KERN_WARNING
5224                        "qla4xxx: Couldn't allocate host from scsi layer!\n");
5225                 goto probe_disable_device;
5226         }
5227
5228         /* Clear our data area */
5229         ha = to_qla_host(host);
5230         memset(ha, 0, sizeof(*ha));
5231
5232         /* Save the information from PCI BIOS.  */
5233         ha->pdev = pdev;
5234         ha->host = host;
5235         ha->host_no = host->host_no;
5236         ha->func_num = PCI_FUNC(ha->pdev->devfn);
5237
5238         pci_enable_pcie_error_reporting(pdev);
5239
5240         /* Setup Runtime configurable options */
5241         if (is_qla8022(ha)) {
5242                 ha->isp_ops = &qla4_82xx_isp_ops;
5243                 ha->reg_tbl = (uint32_t *) qla4_82xx_reg_tbl;
5244                 ha->qdr_sn_window = -1;
5245                 ha->ddr_mn_window = -1;
5246                 ha->curr_window = 255;
5247                 nx_legacy_intr = &legacy_intr[ha->func_num];
5248                 ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
5249                 ha->nx_legacy_intr.tgt_status_reg =
5250                         nx_legacy_intr->tgt_status_reg;
5251                 ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
5252                 ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
5253         } else if (is_qla8032(ha)) {
5254                 ha->isp_ops = &qla4_83xx_isp_ops;
5255                 ha->reg_tbl = (uint32_t *)qla4_83xx_reg_tbl;
5256         } else {
5257                 ha->isp_ops = &qla4xxx_isp_ops;
5258         }
5259
5260         if (is_qla80XX(ha)) {
5261                 rwlock_init(&ha->hw_lock);
5262                 ha->pf_bit = ha->func_num << 16;
5263                 /* Set EEH reset type to fundamental if required by hba */
5264                 pdev->needs_freset = 1;
5265         }
5266
5267         /* Configure PCI I/O space. */
5268         ret = ha->isp_ops->iospace_config(ha);
5269         if (ret)
5270                 goto probe_failed_ioconfig;
5271
5272         ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
5273                    pdev->device, pdev->irq, ha->reg);
5274
5275         qla4xxx_config_dma_addressing(ha);
5276
5277         /* Initialize lists and spinlocks. */
5278         INIT_LIST_HEAD(&ha->free_srb_q);
5279
5280         mutex_init(&ha->mbox_sem);
5281         mutex_init(&ha->chap_sem);
5282         init_completion(&ha->mbx_intr_comp);
5283         init_completion(&ha->disable_acb_comp);
5284
5285         spin_lock_init(&ha->hardware_lock);
5286         spin_lock_init(&ha->work_lock);
5287
5288         /* Initialize work list */
5289         INIT_LIST_HEAD(&ha->work_list);
5290
5291         /* Allocate dma buffers */
5292         if (qla4xxx_mem_alloc(ha)) {
5293                 ql4_printk(KERN_WARNING, ha,
5294                     "[ERROR] Failed to allocate memory for adapter\n");
5295
5296                 ret = -ENOMEM;
5297                 goto probe_failed;
5298         }
5299
5300         host->cmd_per_lun = 3;
5301         host->max_channel = 0;
5302         host->max_lun = MAX_LUNS - 1;
5303         host->max_id = MAX_TARGETS;
5304         host->max_cmd_len = IOCB_MAX_CDB_LEN;
5305         host->can_queue = MAX_SRBS ;
5306         host->transportt = qla4xxx_scsi_transport;
5307
5308         ret = scsi_init_shared_tag_map(host, MAX_SRBS);
5309         if (ret) {
5310                 ql4_printk(KERN_WARNING, ha,
5311                            "%s: scsi_init_shared_tag_map failed\n", __func__);
5312                 goto probe_failed;
5313         }
5314
5315         pci_set_drvdata(pdev, ha);
5316
5317         ret = scsi_add_host(host, &pdev->dev);
5318         if (ret)
5319                 goto probe_failed;
5320
5321         if (is_qla80XX(ha))
5322                 qla4_8xxx_get_flash_info(ha);
5323
5324         if (is_qla8032(ha)) {
5325                 qla4_83xx_read_reset_template(ha);
5326                 /*
5327                  * NOTE: If ql4dontresethba==1, set IDC_CTRL DONTRESET_BIT0.
5328                  * If DONRESET_BIT0 is set, drivers should not set dev_state
5329                  * to NEED_RESET. But if NEED_RESET is set, drivers should
5330                  * should honor the reset.
5331                  */
5332                 if (ql4xdontresethba == 1)
5333                         qla4_83xx_set_idc_dontreset(ha);
5334         }
5335
5336         /*
5337          * Initialize the Host adapter request/response queues and
5338          * firmware
5339          * NOTE: interrupts enabled upon successful completion
5340          */
5341         status = qla4xxx_initialize_adapter(ha, INIT_ADAPTER);
5342
5343         /* Dont retry adapter initialization if IRQ allocation failed */
5344         if (!test_bit(AF_IRQ_ATTACHED, &ha->flags))
5345                 goto skip_retry_init;
5346
5347         while ((!test_bit(AF_ONLINE, &ha->flags)) &&
5348             init_retry_count++ < MAX_INIT_RETRIES) {
5349
5350                 if (is_qla80XX(ha)) {
5351                         ha->isp_ops->idc_lock(ha);
5352                         dev_state = qla4_8xxx_rd_direct(ha,
5353                                                         QLA8XXX_CRB_DEV_STATE);
5354                         ha->isp_ops->idc_unlock(ha);
5355                         if (dev_state == QLA8XXX_DEV_FAILED) {
5356                                 ql4_printk(KERN_WARNING, ha, "%s: don't retry "
5357                                     "initialize adapter. H/W is in failed state\n",
5358                                     __func__);
5359                                 break;
5360                         }
5361                 }
5362                 DEBUG2(printk("scsi: %s: retrying adapter initialization "
5363                               "(%d)\n", __func__, init_retry_count));
5364
5365                 if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
5366                         continue;
5367
5368                 status = qla4xxx_initialize_adapter(ha, INIT_ADAPTER);
5369         }
5370
5371 skip_retry_init:
5372         if (!test_bit(AF_ONLINE, &ha->flags)) {
5373                 ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
5374
5375                 if ((is_qla8022(ha) && ql4xdontresethba) ||
5376                     (is_qla8032(ha) && qla4_83xx_idc_dontreset(ha))) {
5377                         /* Put the device in failed state. */
5378                         DEBUG2(printk(KERN_ERR "HW STATE: FAILED\n"));
5379                         ha->isp_ops->idc_lock(ha);
5380                         qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
5381                                             QLA8XXX_DEV_FAILED);
5382                         ha->isp_ops->idc_unlock(ha);
5383                 }
5384                 ret = -ENODEV;
5385                 goto remove_host;
5386         }
5387
5388         /* Startup the kernel thread for this host adapter. */
5389         DEBUG2(printk("scsi: %s: Starting kernel thread for "
5390                       "qla4xxx_dpc\n", __func__));
5391         sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
5392         ha->dpc_thread = create_singlethread_workqueue(buf);
5393         if (!ha->dpc_thread) {
5394                 ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
5395                 ret = -ENODEV;
5396                 goto remove_host;
5397         }
5398         INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
5399
5400         sprintf(buf, "qla4xxx_%lu_task", ha->host_no);
5401         ha->task_wq = alloc_workqueue(buf, WQ_MEM_RECLAIM, 1);
5402         if (!ha->task_wq) {
5403                 ql4_printk(KERN_WARNING, ha, "Unable to start task thread!\n");
5404                 ret = -ENODEV;
5405                 goto remove_host;
5406         }
5407
5408         /*
5409          * For ISP-8XXX, request_irqs is called in qla4_8xxx_load_risc
5410          * (which is called indirectly by qla4xxx_initialize_adapter),
5411          * so that irqs will be registered after crbinit but before
5412          * mbx_intr_enable.
5413          */
5414         if (is_qla40XX(ha)) {
5415                 ret = qla4xxx_request_irqs(ha);
5416                 if (ret) {
5417                         ql4_printk(KERN_WARNING, ha, "Failed to reserve "
5418                             "interrupt %d already in use.\n", pdev->irq);
5419                         goto remove_host;
5420                 }
5421         }
5422
5423         pci_save_state(ha->pdev);
5424         ha->isp_ops->enable_intrs(ha);
5425
5426         /* Start timer thread. */
5427         qla4xxx_start_timer(ha, qla4xxx_timer, 1);
5428
5429         set_bit(AF_INIT_DONE, &ha->flags);
5430
5431         qla4_8xxx_alloc_sysfs_attr(ha);
5432
5433         printk(KERN_INFO
5434                " QLogic iSCSI HBA Driver version: %s\n"
5435                "  QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
5436                qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
5437                ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
5438                ha->patch_number, ha->build_number);
5439
5440         /* Set the driver version */
5441         if (is_qla80XX(ha))
5442                 qla4_8xxx_set_param(ha, SET_DRVR_VERSION);
5443
5444         if (qla4xxx_setup_boot_info(ha))
5445                 ql4_printk(KERN_ERR, ha,
5446                            "%s: No iSCSI boot target configured\n", __func__);
5447
5448                 /* Perform the build ddb list and login to each */
5449         qla4xxx_build_ddb_list(ha, INIT_ADAPTER);
5450         iscsi_host_for_each_session(ha->host, qla4xxx_login_flash_ddb);
5451         qla4xxx_wait_login_resp_boot_tgt(ha);
5452
5453         qla4xxx_create_chap_list(ha);
5454
5455         qla4xxx_create_ifaces(ha);
5456         return 0;
5457
5458 remove_host:
5459         scsi_remove_host(ha->host);
5460
5461 probe_failed:
5462         qla4xxx_free_adapter(ha);
5463
5464 probe_failed_ioconfig:
5465         pci_disable_pcie_error_reporting(pdev);
5466         scsi_host_put(ha->host);
5467
5468 probe_disable_device:
5469         pci_disable_device(pdev);
5470
5471         return ret;
5472 }
5473
5474 /**
5475  * qla4xxx_prevent_other_port_reinit - prevent other port from re-initialize
5476  * @ha: pointer to adapter structure
5477  *
5478  * Mark the other ISP-4xxx port to indicate that the driver is being removed,
5479  * so that the other port will not re-initialize while in the process of
5480  * removing the ha due to driver unload or hba hotplug.
5481  **/
5482 static void qla4xxx_prevent_other_port_reinit(struct scsi_qla_host *ha)
5483 {
5484         struct scsi_qla_host *other_ha = NULL;
5485         struct pci_dev *other_pdev = NULL;
5486         int fn = ISP4XXX_PCI_FN_2;
5487
5488         /*iscsi function numbers for ISP4xxx is 1 and 3*/
5489         if (PCI_FUNC(ha->pdev->devfn) & BIT_1)
5490                 fn = ISP4XXX_PCI_FN_1;
5491
5492         other_pdev =
5493                 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
5494                 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
5495                 fn));
5496
5497         /* Get other_ha if other_pdev is valid and state is enable*/
5498         if (other_pdev) {
5499                 if (atomic_read(&other_pdev->enable_cnt)) {
5500                         other_ha = pci_get_drvdata(other_pdev);
5501                         if (other_ha) {
5502                                 set_bit(AF_HA_REMOVAL, &other_ha->flags);
5503                                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: "
5504                                     "Prevent %s reinit\n", __func__,
5505                                     dev_name(&other_ha->pdev->dev)));
5506                         }
5507                 }
5508                 pci_dev_put(other_pdev);
5509         }
5510 }
5511
5512 static void qla4xxx_destroy_fw_ddb_session(struct scsi_qla_host *ha)
5513 {
5514         struct ddb_entry *ddb_entry;
5515         int options;
5516         int idx;
5517
5518         for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
5519
5520                 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
5521                 if ((ddb_entry != NULL) &&
5522                     (ddb_entry->ddb_type == FLASH_DDB)) {
5523
5524                         options = LOGOUT_OPTION_CLOSE_SESSION;
5525                         if (qla4xxx_session_logout_ddb(ha, ddb_entry, options)
5526                             == QLA_ERROR)
5527                                 ql4_printk(KERN_ERR, ha, "%s: Logout failed\n",
5528                                            __func__);
5529
5530                         qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
5531                         /*
5532                          * we have decremented the reference count of the driver
5533                          * when we setup the session to have the driver unload
5534                          * to be seamless without actually destroying the
5535                          * session
5536                          **/
5537                         try_module_get(qla4xxx_iscsi_transport.owner);
5538                         iscsi_destroy_endpoint(ddb_entry->conn->ep);
5539                         qla4xxx_free_ddb(ha, ddb_entry);
5540                         iscsi_session_teardown(ddb_entry->sess);
5541                 }
5542         }
5543 }
5544 /**
5545  * qla4xxx_remove_adapter - callback function to remove adapter.
5546  * @pci_dev: PCI device pointer
5547  **/
5548 static void qla4xxx_remove_adapter(struct pci_dev *pdev)
5549 {
5550         struct scsi_qla_host *ha;
5551
5552         /*
5553          * If the PCI device is disabled then it means probe_adapter had
5554          * failed and resources already cleaned up on probe_adapter exit.
5555          */
5556         if (!pci_is_enabled(pdev))
5557                 return;
5558
5559         ha = pci_get_drvdata(pdev);
5560
5561         if (is_qla40XX(ha))
5562                 qla4xxx_prevent_other_port_reinit(ha);
5563
5564         /* destroy iface from sysfs */
5565         qla4xxx_destroy_ifaces(ha);
5566
5567         if ((!ql4xdisablesysfsboot) && ha->boot_kset)
5568                 iscsi_boot_destroy_kset(ha->boot_kset);
5569
5570         qla4xxx_destroy_fw_ddb_session(ha);
5571         qla4_8xxx_free_sysfs_attr(ha);
5572
5573         scsi_remove_host(ha->host);
5574
5575         qla4xxx_free_adapter(ha);
5576
5577         scsi_host_put(ha->host);
5578
5579         pci_disable_pcie_error_reporting(pdev);
5580         pci_disable_device(pdev);
5581         pci_set_drvdata(pdev, NULL);
5582 }
5583
5584 /**
5585  * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
5586  * @ha: HA context
5587  *
5588  * At exit, the @ha's flags.enable_64bit_addressing set to indicated
5589  * supported addressing method.
5590  */
5591 static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
5592 {
5593         int retval;
5594
5595         /* Update our PCI device dma_mask for full 64 bit mask */
5596         if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
5597                 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
5598                         dev_dbg(&ha->pdev->dev,
5599                                   "Failed to set 64 bit PCI consistent mask; "
5600                                    "using 32 bit.\n");
5601                         retval = pci_set_consistent_dma_mask(ha->pdev,
5602                                                              DMA_BIT_MASK(32));
5603                 }
5604         } else
5605                 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
5606 }
5607
5608 static int qla4xxx_slave_alloc(struct scsi_device *sdev)
5609 {
5610         struct iscsi_cls_session *cls_sess;
5611         struct iscsi_session *sess;
5612         struct ddb_entry *ddb;
5613         int queue_depth = QL4_DEF_QDEPTH;
5614
5615         cls_sess = starget_to_session(sdev->sdev_target);
5616         sess = cls_sess->dd_data;
5617         ddb = sess->dd_data;
5618
5619         sdev->hostdata = ddb;
5620         sdev->tagged_supported = 1;
5621
5622         if (ql4xmaxqdepth != 0 && ql4xmaxqdepth <= 0xffffU)
5623                 queue_depth = ql4xmaxqdepth;
5624
5625         scsi_activate_tcq(sdev, queue_depth);
5626         return 0;
5627 }
5628
5629 static int qla4xxx_slave_configure(struct scsi_device *sdev)
5630 {
5631         sdev->tagged_supported = 1;
5632         return 0;
5633 }
5634
5635 static void qla4xxx_slave_destroy(struct scsi_device *sdev)
5636 {
5637         scsi_deactivate_tcq(sdev, 1);
5638 }
5639
5640 static int qla4xxx_change_queue_depth(struct scsi_device *sdev, int qdepth,
5641                                       int reason)
5642 {
5643         if (!ql4xqfulltracking)
5644                 return -EOPNOTSUPP;
5645
5646         return iscsi_change_queue_depth(sdev, qdepth, reason);
5647 }
5648
5649 /**
5650  * qla4xxx_del_from_active_array - returns an active srb
5651  * @ha: Pointer to host adapter structure.
5652  * @index: index into the active_array
5653  *
5654  * This routine removes and returns the srb at the specified index
5655  **/
5656 struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
5657     uint32_t index)
5658 {
5659         struct srb *srb = NULL;
5660         struct scsi_cmnd *cmd = NULL;
5661
5662         cmd = scsi_host_find_tag(ha->host, index);
5663         if (!cmd)
5664                 return srb;
5665
5666         srb = (struct srb *)CMD_SP(cmd);
5667         if (!srb)
5668                 return srb;
5669
5670         /* update counters */
5671         if (srb->flags & SRB_DMA_VALID) {
5672                 ha->req_q_count += srb->iocb_cnt;
5673                 ha->iocb_cnt -= srb->iocb_cnt;
5674                 if (srb->cmd)
5675                         srb->cmd->host_scribble =
5676                                 (unsigned char *)(unsigned long) MAX_SRBS;
5677         }
5678         return srb;
5679 }
5680
5681 /**
5682  * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
5683  * @ha: Pointer to host adapter structure.
5684  * @cmd: Scsi Command to wait on.
5685  *
5686  * This routine waits for the command to be returned by the Firmware
5687  * for some max time.
5688  **/
5689 static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
5690                                       struct scsi_cmnd *cmd)
5691 {
5692         int done = 0;
5693         struct srb *rp;
5694         uint32_t max_wait_time = EH_WAIT_CMD_TOV;
5695         int ret = SUCCESS;
5696
5697         /* Dont wait on command if PCI error is being handled
5698          * by PCI AER driver
5699          */
5700         if (unlikely(pci_channel_offline(ha->pdev)) ||
5701             (test_bit(AF_EEH_BUSY, &ha->flags))) {
5702                 ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
5703                     ha->host_no, __func__);
5704                 return ret;
5705         }
5706
5707         do {
5708                 /* Checking to see if its returned to OS */
5709                 rp = (struct srb *) CMD_SP(cmd);
5710                 if (rp == NULL) {
5711                         done++;
5712                         break;
5713                 }
5714
5715                 msleep(2000);
5716         } while (max_wait_time--);
5717
5718         return done;
5719 }
5720
5721 /**
5722  * qla4xxx_wait_for_hba_online - waits for HBA to come online
5723  * @ha: Pointer to host adapter structure
5724  **/
5725 static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
5726 {
5727         unsigned long wait_online;
5728
5729         wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
5730         while (time_before(jiffies, wait_online)) {
5731
5732                 if (adapter_up(ha))
5733                         return QLA_SUCCESS;
5734
5735                 msleep(2000);
5736         }
5737
5738         return QLA_ERROR;
5739 }
5740
5741 /**
5742  * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
5743  * @ha: pointer to HBA
5744  * @t: target id
5745  * @l: lun id
5746  *
5747  * This function waits for all outstanding commands to a lun to complete. It
5748  * returns 0 if all pending commands are returned and 1 otherwise.
5749  **/
5750 static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
5751                                         struct scsi_target *stgt,
5752                                         struct scsi_device *sdev)
5753 {
5754         int cnt;
5755         int status = 0;
5756         struct scsi_cmnd *cmd;
5757
5758         /*
5759          * Waiting for all commands for the designated target or dev
5760          * in the active array
5761          */
5762         for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
5763                 cmd = scsi_host_find_tag(ha->host, cnt);
5764                 if (cmd && stgt == scsi_target(cmd->device) &&
5765                     (!sdev || sdev == cmd->device)) {
5766                         if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
5767                                 status++;
5768                                 break;
5769                         }
5770                 }
5771         }
5772         return status;
5773 }
5774
5775 /**
5776  * qla4xxx_eh_abort - callback for abort task.
5777  * @cmd: Pointer to Linux's SCSI command structure
5778  *
5779  * This routine is called by the Linux OS to abort the specified
5780  * command.
5781  **/
5782 static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
5783 {
5784         struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
5785         unsigned int id = cmd->device->id;
5786         unsigned int lun = cmd->device->lun;
5787         unsigned long flags;
5788         struct srb *srb = NULL;
5789         int ret = SUCCESS;
5790         int wait = 0;
5791
5792         ql4_printk(KERN_INFO, ha,
5793             "scsi%ld:%d:%d: Abort command issued cmd=%p\n",
5794             ha->host_no, id, lun, cmd);
5795
5796         spin_lock_irqsave(&ha->hardware_lock, flags);
5797         srb = (struct srb *) CMD_SP(cmd);
5798         if (!srb) {
5799                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5800                 return SUCCESS;
5801         }
5802         kref_get(&srb->srb_ref);
5803         spin_unlock_irqrestore(&ha->hardware_lock, flags);
5804
5805         if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
5806                 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
5807                     ha->host_no, id, lun));
5808                 ret = FAILED;
5809         } else {
5810                 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
5811                     ha->host_no, id, lun));
5812                 wait = 1;
5813         }
5814
5815         kref_put(&srb->srb_ref, qla4xxx_srb_compl);
5816
5817         /* Wait for command to complete */
5818         if (wait) {
5819                 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
5820                         DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
5821                             ha->host_no, id, lun));
5822                         ret = FAILED;
5823                 }
5824         }
5825
5826         ql4_printk(KERN_INFO, ha,
5827             "scsi%ld:%d:%d: Abort command - %s\n",
5828             ha->host_no, id, lun, (ret == SUCCESS) ? "succeeded" : "failed");
5829
5830         return ret;
5831 }
5832
5833 /**
5834  * qla4xxx_eh_device_reset - callback for target reset.
5835  * @cmd: Pointer to Linux's SCSI command structure
5836  *
5837  * This routine is called by the Linux OS to reset all luns on the
5838  * specified target.
5839  **/
5840 static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
5841 {
5842         struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
5843         struct ddb_entry *ddb_entry = cmd->device->hostdata;
5844         int ret = FAILED, stat;
5845
5846         if (!ddb_entry)
5847                 return ret;
5848
5849         ret = iscsi_block_scsi_eh(cmd);
5850         if (ret)
5851                 return ret;
5852         ret = FAILED;
5853
5854         ql4_printk(KERN_INFO, ha,
5855                    "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
5856                    cmd->device->channel, cmd->device->id, cmd->device->lun);
5857
5858         DEBUG2(printk(KERN_INFO
5859                       "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
5860                       "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
5861                       cmd, jiffies, cmd->request->timeout / HZ,
5862                       ha->dpc_flags, cmd->result, cmd->allowed));
5863
5864         /* FIXME: wait for hba to go online */
5865         stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
5866         if (stat != QLA_SUCCESS) {
5867                 ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
5868                 goto eh_dev_reset_done;
5869         }
5870
5871         if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
5872                                          cmd->device)) {
5873                 ql4_printk(KERN_INFO, ha,
5874                            "DEVICE RESET FAILED - waiting for "
5875                            "commands.\n");
5876                 goto eh_dev_reset_done;
5877         }
5878
5879         /* Send marker. */
5880         if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
5881                 MM_LUN_RESET) != QLA_SUCCESS)
5882                 goto eh_dev_reset_done;
5883
5884         ql4_printk(KERN_INFO, ha,
5885                    "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
5886                    ha->host_no, cmd->device->channel, cmd->device->id,
5887                    cmd->device->lun);
5888
5889         ret = SUCCESS;
5890
5891 eh_dev_reset_done:
5892
5893         return ret;
5894 }
5895
5896 /**
5897  * qla4xxx_eh_target_reset - callback for target reset.
5898  * @cmd: Pointer to Linux's SCSI command structure
5899  *
5900  * This routine is called by the Linux OS to reset the target.
5901  **/
5902 static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
5903 {
5904         struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
5905         struct ddb_entry *ddb_entry = cmd->device->hostdata;
5906         int stat, ret;
5907
5908         if (!ddb_entry)
5909                 return FAILED;
5910
5911         ret = iscsi_block_scsi_eh(cmd);
5912         if (ret)
5913                 return ret;
5914
5915         starget_printk(KERN_INFO, scsi_target(cmd->device),
5916                        "WARM TARGET RESET ISSUED.\n");
5917
5918         DEBUG2(printk(KERN_INFO
5919                       "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
5920                       "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
5921                       ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
5922                       ha->dpc_flags, cmd->result, cmd->allowed));
5923
5924         stat = qla4xxx_reset_target(ha, ddb_entry);
5925         if (stat != QLA_SUCCESS) {
5926                 starget_printk(KERN_INFO, scsi_target(cmd->device),
5927                                "WARM TARGET RESET FAILED.\n");
5928                 return FAILED;
5929         }
5930
5931         if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
5932                                          NULL)) {
5933                 starget_printk(KERN_INFO, scsi_target(cmd->device),
5934                                "WARM TARGET DEVICE RESET FAILED - "
5935                                "waiting for commands.\n");
5936                 return FAILED;
5937         }
5938
5939         /* Send marker. */
5940         if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
5941                 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
5942                 starget_printk(KERN_INFO, scsi_target(cmd->device),
5943                                "WARM TARGET DEVICE RESET FAILED - "
5944                                "marker iocb failed.\n");
5945                 return FAILED;
5946         }
5947
5948         starget_printk(KERN_INFO, scsi_target(cmd->device),
5949                        "WARM TARGET RESET SUCCEEDED.\n");
5950         return SUCCESS;
5951 }
5952
5953 /**
5954  * qla4xxx_is_eh_active - check if error handler is running
5955  * @shost: Pointer to SCSI Host struct
5956  *
5957  * This routine finds that if reset host is called in EH
5958  * scenario or from some application like sg_reset
5959  **/
5960 static int qla4xxx_is_eh_active(struct Scsi_Host *shost)
5961 {
5962         if (shost->shost_state == SHOST_RECOVERY)
5963                 return 1;
5964         return 0;
5965 }
5966
5967 /**
5968  * qla4xxx_eh_host_reset - kernel callback
5969  * @cmd: Pointer to Linux's SCSI command structure
5970  *
5971  * This routine is invoked by the Linux kernel to perform fatal error
5972  * recovery on the specified adapter.
5973  **/
5974 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
5975 {
5976         int return_status = FAILED;
5977         struct scsi_qla_host *ha;
5978
5979         ha = to_qla_host(cmd->device->host);
5980
5981         if (is_qla8032(ha) && ql4xdontresethba)
5982                 qla4_83xx_set_idc_dontreset(ha);
5983
5984         /*
5985          * For ISP8324, if IDC_CTRL DONTRESET_BIT0 is set by other
5986          * protocol drivers, we should not set device_state to
5987          * NEED_RESET
5988          */
5989         if (ql4xdontresethba ||
5990             (is_qla8032(ha) && qla4_83xx_idc_dontreset(ha))) {
5991                 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
5992                      ha->host_no, __func__));
5993
5994                 /* Clear outstanding srb in queues */
5995                 if (qla4xxx_is_eh_active(cmd->device->host))
5996                         qla4xxx_abort_active_cmds(ha, DID_ABORT << 16);
5997
5998                 return FAILED;
5999         }
6000
6001         ql4_printk(KERN_INFO, ha,
6002                    "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
6003                    cmd->device->channel, cmd->device->id, cmd->device->lun);
6004
6005         if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
6006                 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host.  Adapter "
6007                               "DEAD.\n", ha->host_no, cmd->device->channel,
6008                               __func__));
6009
6010                 return FAILED;
6011         }
6012
6013         if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
6014                 if (is_qla80XX(ha))
6015                         set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
6016                 else
6017                         set_bit(DPC_RESET_HA, &ha->dpc_flags);
6018         }
6019
6020         if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
6021                 return_status = SUCCESS;
6022
6023         ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
6024                    return_status == FAILED ? "FAILED" : "SUCCEEDED");
6025
6026         return return_status;
6027 }
6028
6029 static int qla4xxx_context_reset(struct scsi_qla_host *ha)
6030 {
6031         uint32_t mbox_cmd[MBOX_REG_COUNT];
6032         uint32_t mbox_sts[MBOX_REG_COUNT];
6033         struct addr_ctrl_blk_def *acb = NULL;
6034         uint32_t acb_len = sizeof(struct addr_ctrl_blk_def);
6035         int rval = QLA_SUCCESS;
6036         dma_addr_t acb_dma;
6037
6038         acb = dma_alloc_coherent(&ha->pdev->dev,
6039                                  sizeof(struct addr_ctrl_blk_def),
6040                                  &acb_dma, GFP_KERNEL);
6041         if (!acb) {
6042                 ql4_printk(KERN_ERR, ha, "%s: Unable to alloc acb\n",
6043                            __func__);
6044                 rval = -ENOMEM;
6045                 goto exit_port_reset;
6046         }
6047
6048         memset(acb, 0, acb_len);
6049
6050         rval = qla4xxx_get_acb(ha, acb_dma, PRIMARI_ACB, acb_len);
6051         if (rval != QLA_SUCCESS) {
6052                 rval = -EIO;
6053                 goto exit_free_acb;
6054         }
6055
6056         rval = qla4xxx_disable_acb(ha);
6057         if (rval != QLA_SUCCESS) {
6058                 rval = -EIO;
6059                 goto exit_free_acb;
6060         }
6061
6062         wait_for_completion_timeout(&ha->disable_acb_comp,
6063                                     DISABLE_ACB_TOV * HZ);
6064
6065         rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], acb_dma);
6066         if (rval != QLA_SUCCESS) {
6067                 rval = -EIO;
6068                 goto exit_free_acb;
6069         }
6070
6071 exit_free_acb:
6072         dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk_def),
6073                           acb, acb_dma);
6074 exit_port_reset:
6075         DEBUG2(ql4_printk(KERN_INFO, ha, "%s %s\n", __func__,
6076                           rval == QLA_SUCCESS ? "SUCCEEDED" : "FAILED"));
6077         return rval;
6078 }
6079
6080 static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type)
6081 {
6082         struct scsi_qla_host *ha = to_qla_host(shost);
6083         int rval = QLA_SUCCESS;
6084
6085         if (ql4xdontresethba) {
6086                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Don't Reset HBA\n",
6087                                   __func__));
6088                 rval = -EPERM;
6089                 goto exit_host_reset;
6090         }
6091
6092         if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
6093                 goto recover_adapter;
6094
6095         switch (reset_type) {
6096         case SCSI_ADAPTER_RESET:
6097                 set_bit(DPC_RESET_HA, &ha->dpc_flags);
6098                 break;
6099         case SCSI_FIRMWARE_RESET:
6100                 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
6101                         if (is_qla80XX(ha))
6102                                 /* set firmware context reset */
6103                                 set_bit(DPC_RESET_HA_FW_CONTEXT,
6104                                         &ha->dpc_flags);
6105                         else {
6106                                 rval = qla4xxx_context_reset(ha);
6107                                 goto exit_host_reset;
6108                         }
6109                 }
6110                 break;
6111         }
6112
6113 recover_adapter:
6114         rval = qla4xxx_recover_adapter(ha);
6115         if (rval != QLA_SUCCESS) {
6116                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: recover adapter fail\n",
6117                                   __func__));
6118                 rval = -EIO;
6119         }
6120
6121 exit_host_reset:
6122         return rval;
6123 }
6124
6125 /* PCI AER driver recovers from all correctable errors w/o
6126  * driver intervention. For uncorrectable errors PCI AER
6127  * driver calls the following device driver's callbacks
6128  *
6129  * - Fatal Errors - link_reset
6130  * - Non-Fatal Errors - driver's pci_error_detected() which
6131  * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
6132  *
6133  * PCI AER driver calls
6134  * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
6135  *               returns RECOVERED or NEED_RESET if fw_hung
6136  * NEED_RESET - driver's slot_reset()
6137  * DISCONNECT - device is dead & cannot recover
6138  * RECOVERED - driver's pci_resume()
6139  */
6140 static pci_ers_result_t
6141 qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
6142 {
6143         struct scsi_qla_host *ha = pci_get_drvdata(pdev);
6144
6145         ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
6146             ha->host_no, __func__, state);
6147
6148         if (!is_aer_supported(ha))
6149                 return PCI_ERS_RESULT_NONE;
6150
6151         switch (state) {
6152         case pci_channel_io_normal:
6153                 clear_bit(AF_EEH_BUSY, &ha->flags);
6154                 return PCI_ERS_RESULT_CAN_RECOVER;
6155         case pci_channel_io_frozen:
6156                 set_bit(AF_EEH_BUSY, &ha->flags);
6157                 qla4xxx_mailbox_premature_completion(ha);
6158                 qla4xxx_free_irqs(ha);
6159                 pci_disable_device(pdev);
6160                 /* Return back all IOs */
6161                 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
6162                 return PCI_ERS_RESULT_NEED_RESET;
6163         case pci_channel_io_perm_failure:
6164                 set_bit(AF_EEH_BUSY, &ha->flags);
6165                 set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
6166                 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
6167                 return PCI_ERS_RESULT_DISCONNECT;
6168         }
6169         return PCI_ERS_RESULT_NEED_RESET;
6170 }
6171
6172 /**
6173  * qla4xxx_pci_mmio_enabled() gets called if
6174  * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
6175  * and read/write to the device still works.
6176  **/
6177 static pci_ers_result_t
6178 qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
6179 {
6180         struct scsi_qla_host *ha = pci_get_drvdata(pdev);
6181
6182         if (!is_aer_supported(ha))
6183                 return PCI_ERS_RESULT_NONE;
6184
6185         return PCI_ERS_RESULT_RECOVERED;
6186 }
6187
6188 static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
6189 {
6190         uint32_t rval = QLA_ERROR;
6191         int fn;
6192         struct pci_dev *other_pdev = NULL;
6193
6194         ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
6195
6196         set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
6197
6198         if (test_bit(AF_ONLINE, &ha->flags)) {
6199                 clear_bit(AF_ONLINE, &ha->flags);
6200                 clear_bit(AF_LINK_UP, &ha->flags);
6201                 iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
6202                 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
6203         }
6204
6205         fn = PCI_FUNC(ha->pdev->devfn);
6206         while (fn > 0) {
6207                 fn--;
6208                 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
6209                     "func %x\n", ha->host_no, __func__, fn);
6210                 /* Get the pci device given the domain, bus,
6211                  * slot/function number */
6212                 other_pdev =
6213                     pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
6214                     ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
6215                     fn));
6216
6217                 if (!other_pdev)
6218                         continue;
6219
6220                 if (atomic_read(&other_pdev->enable_cnt)) {
6221                         ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
6222                             "func in enabled state%x\n", ha->host_no,
6223                             __func__, fn);
6224                         pci_dev_put(other_pdev);
6225                         break;
6226                 }
6227                 pci_dev_put(other_pdev);
6228         }
6229
6230         /* The first function on the card, the reset owner will
6231          * start & initialize the firmware. The other functions
6232          * on the card will reset the firmware context
6233          */
6234         if (!fn) {
6235                 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
6236                     "0x%x is the owner\n", ha->host_no, __func__,
6237                     ha->pdev->devfn);
6238
6239                 ha->isp_ops->idc_lock(ha);
6240                 qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
6241                                     QLA8XXX_DEV_COLD);
6242                 ha->isp_ops->idc_unlock(ha);
6243
6244                 rval = qla4_8xxx_update_idc_reg(ha);
6245                 if (rval == QLA_ERROR) {
6246                         ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: FAILED\n",
6247                                    ha->host_no, __func__);
6248                         ha->isp_ops->idc_lock(ha);
6249                         qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
6250                                             QLA8XXX_DEV_FAILED);
6251                         ha->isp_ops->idc_unlock(ha);
6252                         goto exit_error_recovery;
6253                 }
6254
6255                 clear_bit(AF_FW_RECOVERY, &ha->flags);
6256                 rval = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
6257
6258                 if (rval != QLA_SUCCESS) {
6259                         ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
6260                             "FAILED\n", ha->host_no, __func__);
6261                         ha->isp_ops->idc_lock(ha);
6262                         qla4_8xxx_clear_drv_active(ha);
6263                         qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
6264                                             QLA8XXX_DEV_FAILED);
6265                         ha->isp_ops->idc_unlock(ha);
6266                 } else {
6267                         ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
6268                             "READY\n", ha->host_no, __func__);
6269                         ha->isp_ops->idc_lock(ha);
6270                         qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
6271                                             QLA8XXX_DEV_READY);
6272                         /* Clear driver state register */
6273                         qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DRV_STATE, 0);
6274                         qla4_8xxx_set_drv_active(ha);
6275                         ha->isp_ops->idc_unlock(ha);
6276                         ha->isp_ops->enable_intrs(ha);
6277                 }
6278         } else {
6279                 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
6280                     "the reset owner\n", ha->host_no, __func__,
6281                     ha->pdev->devfn);
6282                 if ((qla4_8xxx_rd_direct(ha, QLA8XXX_CRB_DEV_STATE) ==
6283                      QLA8XXX_DEV_READY)) {
6284                         clear_bit(AF_FW_RECOVERY, &ha->flags);
6285                         rval = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
6286                         if (rval == QLA_SUCCESS)
6287                                 ha->isp_ops->enable_intrs(ha);
6288
6289                         ha->isp_ops->idc_lock(ha);
6290                         qla4_8xxx_set_drv_active(ha);
6291                         ha->isp_ops->idc_unlock(ha);
6292                 }
6293         }
6294 exit_error_recovery:
6295         clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
6296         return rval;
6297 }
6298
6299 static pci_ers_result_t
6300 qla4xxx_pci_slot_reset(struct pci_dev *pdev)
6301 {
6302         pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
6303         struct scsi_qla_host *ha = pci_get_drvdata(pdev);
6304         int rc;
6305
6306         ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
6307             ha->host_no, __func__);
6308
6309         if (!is_aer_supported(ha))
6310                 return PCI_ERS_RESULT_NONE;
6311
6312         /* Restore the saved state of PCIe device -
6313          * BAR registers, PCI Config space, PCIX, MSI,
6314          * IOV states
6315          */
6316         pci_restore_state(pdev);
6317
6318         /* pci_restore_state() clears the saved_state flag of the device
6319          * save restored state which resets saved_state flag
6320          */
6321         pci_save_state(pdev);
6322
6323         /* Initialize device or resume if in suspended state */
6324         rc = pci_enable_device(pdev);
6325         if (rc) {
6326                 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Can't re-enable "
6327                     "device after reset\n", ha->host_no, __func__);
6328                 goto exit_slot_reset;
6329         }
6330
6331         ha->isp_ops->disable_intrs(ha);
6332
6333         if (is_qla80XX(ha)) {
6334                 if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
6335                         ret = PCI_ERS_RESULT_RECOVERED;
6336                         goto exit_slot_reset;
6337                 } else
6338                         goto exit_slot_reset;
6339         }
6340
6341 exit_slot_reset:
6342         ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
6343             "device after reset\n", ha->host_no, __func__, ret);
6344         return ret;
6345 }
6346
6347 static void
6348 qla4xxx_pci_resume(struct pci_dev *pdev)
6349 {
6350         struct scsi_qla_host *ha = pci_get_drvdata(pdev);
6351         int ret;
6352
6353         ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
6354             ha->host_no, __func__);
6355
6356         ret = qla4xxx_wait_for_hba_online(ha);
6357         if (ret != QLA_SUCCESS) {
6358                 ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
6359                     "resume I/O from slot/link_reset\n", ha->host_no,
6360                      __func__);
6361         }
6362
6363         pci_cleanup_aer_uncorrect_error_status(pdev);
6364         clear_bit(AF_EEH_BUSY, &ha->flags);
6365 }
6366
6367 static const struct pci_error_handlers qla4xxx_err_handler = {
6368         .error_detected = qla4xxx_pci_error_detected,
6369         .mmio_enabled = qla4xxx_pci_mmio_enabled,
6370         .slot_reset = qla4xxx_pci_slot_reset,
6371         .resume = qla4xxx_pci_resume,
6372 };
6373
6374 static struct pci_device_id qla4xxx_pci_tbl[] = {
6375         {
6376                 .vendor         = PCI_VENDOR_ID_QLOGIC,
6377                 .device         = PCI_DEVICE_ID_QLOGIC_ISP4010,
6378                 .subvendor      = PCI_ANY_ID,
6379                 .subdevice      = PCI_ANY_ID,
6380         },
6381         {
6382                 .vendor         = PCI_VENDOR_ID_QLOGIC,
6383                 .device         = PCI_DEVICE_ID_QLOGIC_ISP4022,
6384                 .subvendor      = PCI_ANY_ID,
6385                 .subdevice      = PCI_ANY_ID,
6386         },
6387         {
6388                 .vendor         = PCI_VENDOR_ID_QLOGIC,
6389                 .device         = PCI_DEVICE_ID_QLOGIC_ISP4032,
6390                 .subvendor      = PCI_ANY_ID,
6391                 .subdevice      = PCI_ANY_ID,
6392         },
6393         {
6394                 .vendor         = PCI_VENDOR_ID_QLOGIC,
6395                 .device         = PCI_DEVICE_ID_QLOGIC_ISP8022,
6396                 .subvendor      = PCI_ANY_ID,
6397                 .subdevice      = PCI_ANY_ID,
6398         },
6399         {
6400                 .vendor         = PCI_VENDOR_ID_QLOGIC,
6401                 .device         = PCI_DEVICE_ID_QLOGIC_ISP8324,
6402                 .subvendor      = PCI_ANY_ID,
6403                 .subdevice      = PCI_ANY_ID,
6404         },
6405         {0, 0},
6406 };
6407 MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
6408
6409 static struct pci_driver qla4xxx_pci_driver = {
6410         .name           = DRIVER_NAME,
6411         .id_table       = qla4xxx_pci_tbl,
6412         .probe          = qla4xxx_probe_adapter,
6413         .remove         = qla4xxx_remove_adapter,
6414         .err_handler = &qla4xxx_err_handler,
6415 };
6416
6417 static int __init qla4xxx_module_init(void)
6418 {
6419         int ret;
6420
6421         /* Allocate cache for SRBs. */
6422         srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
6423                                        SLAB_HWCACHE_ALIGN, NULL);
6424         if (srb_cachep == NULL) {
6425                 printk(KERN_ERR
6426                        "%s: Unable to allocate SRB cache..."
6427                        "Failing load!\n", DRIVER_NAME);
6428                 ret = -ENOMEM;
6429                 goto no_srp_cache;
6430         }
6431
6432         /* Derive version string. */
6433         strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
6434         if (ql4xextended_error_logging)
6435                 strcat(qla4xxx_version_str, "-debug");
6436
6437         qla4xxx_scsi_transport =
6438                 iscsi_register_transport(&qla4xxx_iscsi_transport);
6439         if (!qla4xxx_scsi_transport){
6440                 ret = -ENODEV;
6441                 goto release_srb_cache;
6442         }
6443
6444         ret = pci_register_driver(&qla4xxx_pci_driver);
6445         if (ret)
6446                 goto unregister_transport;
6447
6448         printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
6449         return 0;
6450
6451 unregister_transport:
6452         iscsi_unregister_transport(&qla4xxx_iscsi_transport);
6453 release_srb_cache:
6454         kmem_cache_destroy(srb_cachep);
6455 no_srp_cache:
6456         return ret;
6457 }
6458
6459 static void __exit qla4xxx_module_exit(void)
6460 {
6461         pci_unregister_driver(&qla4xxx_pci_driver);
6462         iscsi_unregister_transport(&qla4xxx_iscsi_transport);
6463         kmem_cache_destroy(srb_cachep);
6464 }
6465
6466 module_init(qla4xxx_module_init);
6467 module_exit(qla4xxx_module_exit);
6468
6469 MODULE_AUTHOR("QLogic Corporation");
6470 MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
6471 MODULE_LICENSE("GPL");
6472 MODULE_VERSION(QLA4XXX_DRIVER_VERSION);