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