d5f9f60609b63692ea4962d003a28137e9f4169a
[firefly-linux-kernel-4.4.55.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
10 #include <scsi/scsi_tcq.h>
11 #include <scsi/scsicam.h>
12
13 #include "ql4_def.h"
14 #include "ql4_version.h"
15 #include "ql4_glbl.h"
16 #include "ql4_dbg.h"
17 #include "ql4_inline.h"
18
19 /*
20  * Driver version
21  */
22 static char qla4xxx_version_str[40];
23
24 /*
25  * SRB allocation cache
26  */
27 static struct kmem_cache *srb_cachep;
28
29 /*
30  * Module parameter information and variables
31  */
32 int ql4xdontresethba = 0;
33 module_param(ql4xdontresethba, int, S_IRUGO | S_IWUSR);
34 MODULE_PARM_DESC(ql4xdontresethba,
35                 "Don't reset the HBA for driver recovery \n"
36                 " 0 - It will reset HBA (Default)\n"
37                 " 1 - It will NOT reset HBA");
38
39 int ql4xextended_error_logging = 0; /* 0 = off, 1 = log errors */
40 module_param(ql4xextended_error_logging, int, S_IRUGO | S_IWUSR);
41 MODULE_PARM_DESC(ql4xextended_error_logging,
42                  "Option to enable extended error logging, "
43                  "Default is 0 - no logging, 1 - debug logging");
44
45 int ql4xenablemsix = 1;
46 module_param(ql4xenablemsix, int, S_IRUGO|S_IWUSR);
47 MODULE_PARM_DESC(ql4xenablemsix,
48                 "Set to enable MSI or MSI-X interrupt mechanism.\n"
49                 " 0 = enable INTx interrupt mechanism.\n"
50                 " 1 = enable MSI-X interrupt mechanism (Default).\n"
51                 " 2 = enable MSI interrupt mechanism.");
52
53 #define QL4_DEF_QDEPTH 32
54 static int ql4xmaxqdepth = QL4_DEF_QDEPTH;
55 module_param(ql4xmaxqdepth, int, S_IRUGO | S_IWUSR);
56 MODULE_PARM_DESC(ql4xmaxqdepth,
57                 "Maximum queue depth to report for target devices.\n"
58                 " Default: 32.");
59
60 static int ql4xsess_recovery_tmo = QL4_SESS_RECOVERY_TMO;
61 module_param(ql4xsess_recovery_tmo, int, S_IRUGO);
62 MODULE_PARM_DESC(ql4xsess_recovery_tmo,
63                 "Target Session Recovery Timeout.\n"
64                 " Default: 30 sec.");
65
66 /*
67  * SCSI host template entry points
68  */
69 static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
70
71 /*
72  * iSCSI template entry points
73  */
74 static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
75                              enum iscsi_tgt_dscvr type, uint32_t enable,
76                              struct sockaddr *dst_addr);
77 static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
78                                   enum iscsi_param param, char *buf);
79 static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
80                                   enum iscsi_param param, char *buf);
81 static int qla4xxx_host_get_param(struct Scsi_Host *shost,
82                                   enum iscsi_host_param param, char *buf);
83 static int qla4xxx_iface_set_param(struct Scsi_Host *shost, char *data,
84                                    int count);
85 static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
86                                    enum iscsi_param_type param_type,
87                                    int param, char *buf);
88 static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session);
89 static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
90
91 /*
92  * SCSI host template entry points
93  */
94 static int qla4xxx_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmd);
95 static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
96 static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
97 static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
98 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
99 static int qla4xxx_slave_alloc(struct scsi_device *device);
100 static int qla4xxx_slave_configure(struct scsi_device *device);
101 static void qla4xxx_slave_destroy(struct scsi_device *sdev);
102 static void qla4xxx_scan_start(struct Scsi_Host *shost);
103 static mode_t ql4_attr_is_visible(int param_type, int param);
104
105 static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
106     QLA82XX_LEGACY_INTR_CONFIG;
107
108 static struct scsi_host_template qla4xxx_driver_template = {
109         .module                 = THIS_MODULE,
110         .name                   = DRIVER_NAME,
111         .proc_name              = DRIVER_NAME,
112         .queuecommand           = qla4xxx_queuecommand,
113
114         .eh_abort_handler       = qla4xxx_eh_abort,
115         .eh_device_reset_handler = qla4xxx_eh_device_reset,
116         .eh_target_reset_handler = qla4xxx_eh_target_reset,
117         .eh_host_reset_handler  = qla4xxx_eh_host_reset,
118         .eh_timed_out           = qla4xxx_eh_cmd_timed_out,
119
120         .slave_configure        = qla4xxx_slave_configure,
121         .slave_alloc            = qla4xxx_slave_alloc,
122         .slave_destroy          = qla4xxx_slave_destroy,
123
124         .scan_finished          = iscsi_scan_finished,
125         .scan_start             = qla4xxx_scan_start,
126
127         .this_id                = -1,
128         .cmd_per_lun            = 3,
129         .use_clustering         = ENABLE_CLUSTERING,
130         .sg_tablesize           = SG_ALL,
131
132         .max_sectors            = 0xFFFF,
133         .shost_attrs            = qla4xxx_host_attrs,
134 };
135
136 static struct iscsi_transport qla4xxx_iscsi_transport = {
137         .owner                  = THIS_MODULE,
138         .name                   = DRIVER_NAME,
139         .caps                   = CAP_FW_DB | CAP_SENDTARGETS_OFFLOAD |
140                                   CAP_DATA_PATH_OFFLOAD,
141         .tgt_dscvr              = qla4xxx_tgt_dscvr,
142         .attr_is_visible        = ql4_attr_is_visible,
143         .get_conn_param         = qla4xxx_conn_get_param,
144         .get_session_param      = qla4xxx_sess_get_param,
145         .get_host_param         = qla4xxx_host_get_param,
146         .set_iface_param        = qla4xxx_iface_set_param,
147         .session_recovery_timedout = qla4xxx_recovery_timedout,
148         .get_iface_param        = qla4xxx_get_iface_param,
149 };
150
151 static struct scsi_transport_template *qla4xxx_scsi_transport;
152
153 static mode_t ql4_attr_is_visible(int param_type, int param)
154 {
155         switch (param_type) {
156         case ISCSI_HOST_PARAM:
157                 switch (param) {
158                 case ISCSI_HOST_PARAM_HWADDRESS:
159                 case ISCSI_HOST_PARAM_IPADDRESS:
160                 case ISCSI_HOST_PARAM_INITIATOR_NAME:
161                         return S_IRUGO;
162                 default:
163                         return 0;
164                 }
165         case ISCSI_PARAM:
166                 switch (param) {
167                 case ISCSI_PARAM_CONN_ADDRESS:
168                 case ISCSI_PARAM_CONN_PORT:
169                 case ISCSI_PARAM_TARGET_NAME:
170                 case ISCSI_PARAM_TPGT:
171                 case ISCSI_PARAM_TARGET_ALIAS:
172                         return S_IRUGO;
173                 default:
174                         return 0;
175                 }
176         case ISCSI_NET_PARAM:
177                 switch (param) {
178                 case ISCSI_NET_PARAM_IPV4_ADDR:
179                 case ISCSI_NET_PARAM_IPV4_SUBNET:
180                 case ISCSI_NET_PARAM_IPV4_GW:
181                 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
182                 case ISCSI_NET_PARAM_IFACE_ENABLE:
183                 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
184                 case ISCSI_NET_PARAM_IPV6_ADDR:
185                 case ISCSI_NET_PARAM_IPV6_ROUTER:
186                 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
187                 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
188                 case ISCSI_NET_PARAM_VLAN_ID:
189                 case ISCSI_NET_PARAM_VLAN_PRIORITY:
190                 case ISCSI_NET_PARAM_VLAN_ENABLED:
191                         return S_IRUGO;
192                 default:
193                         return 0;
194                 }
195         }
196
197         return 0;
198 }
199
200 static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
201                                    enum iscsi_param_type param_type,
202                                    int param, char *buf)
203 {
204         struct Scsi_Host *shost = iscsi_iface_to_shost(iface);
205         struct scsi_qla_host *ha = to_qla_host(shost);
206         int len = -ENOSYS;
207
208         if (param_type != ISCSI_NET_PARAM)
209                 return -ENOSYS;
210
211         switch (param) {
212         case ISCSI_NET_PARAM_IPV4_ADDR:
213                 len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
214                 break;
215         case ISCSI_NET_PARAM_IPV4_SUBNET:
216                 len = sprintf(buf, "%pI4\n", &ha->ip_config.subnet_mask);
217                 break;
218         case ISCSI_NET_PARAM_IPV4_GW:
219                 len = sprintf(buf, "%pI4\n", &ha->ip_config.gateway);
220                 break;
221         case ISCSI_NET_PARAM_IFACE_ENABLE:
222                 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
223                         len = sprintf(buf, "%s\n",
224                                       (ha->ip_config.ipv4_options &
225                                        IPOPT_IPV4_PROTOCOL_ENABLE) ?
226                                       "enabled" : "disabled");
227                 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
228                         len = sprintf(buf, "%s\n",
229                                       (ha->ip_config.ipv6_options &
230                                        IPV6_OPT_IPV6_PROTOCOL_ENABLE) ?
231                                        "enabled" : "disabled");
232                 break;
233         case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
234                 len = sprintf(buf, "%s\n",
235                               (ha->ip_config.tcp_options & TCPOPT_DHCP_ENABLE) ?
236                               "dhcp" : "static");
237                 break;
238         case ISCSI_NET_PARAM_IPV6_ADDR:
239                 if (iface->iface_num == 0)
240                         len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr0);
241                 if (iface->iface_num == 1)
242                         len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr1);
243                 break;
244         case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
245                 len = sprintf(buf, "%pI6\n",
246                               &ha->ip_config.ipv6_link_local_addr);
247                 break;
248         case ISCSI_NET_PARAM_IPV6_ROUTER:
249                 len = sprintf(buf, "%pI6\n",
250                               &ha->ip_config.ipv6_default_router_addr);
251                 break;
252         case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
253                 len = sprintf(buf, "%s\n",
254                               (ha->ip_config.ipv6_addl_options &
255                                IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) ?
256                                "nd" : "static");
257                 break;
258         case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
259                 len = sprintf(buf, "%s\n",
260                               (ha->ip_config.ipv6_addl_options &
261                                IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR) ?
262                                "auto" : "static");
263                 break;
264         case ISCSI_NET_PARAM_VLAN_ID:
265                 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
266                         len = sprintf(buf, "%d\n",
267                                       (ha->ip_config.ipv4_vlan_tag &
268                                        ISCSI_MAX_VLAN_ID));
269                 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
270                         len = sprintf(buf, "%d\n",
271                                       (ha->ip_config.ipv6_vlan_tag &
272                                        ISCSI_MAX_VLAN_ID));
273                 break;
274         case ISCSI_NET_PARAM_VLAN_PRIORITY:
275                 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
276                         len = sprintf(buf, "%d\n",
277                                       ((ha->ip_config.ipv4_vlan_tag >> 13) &
278                                         ISCSI_MAX_VLAN_PRIORITY));
279                 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
280                         len = sprintf(buf, "%d\n",
281                                       ((ha->ip_config.ipv6_vlan_tag >> 13) &
282                                         ISCSI_MAX_VLAN_PRIORITY));
283                 break;
284         case ISCSI_NET_PARAM_VLAN_ENABLED:
285                 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
286                         len = sprintf(buf, "%s\n",
287                                       (ha->ip_config.ipv4_options &
288                                        IPOPT_VLAN_TAGGING_ENABLE) ?
289                                        "enabled" : "disabled");
290                 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
291                         len = sprintf(buf, "%s\n",
292                                       (ha->ip_config.ipv6_options &
293                                        IPV6_OPT_VLAN_TAGGING_ENABLE) ?
294                                        "enabled" : "disabled");
295                 break;
296         default:
297                 len = -ENOSYS;
298         }
299
300         return len;
301 }
302
303 static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
304 {
305         struct iscsi_cls_session *session;
306         struct ddb_entry *ddb_entry;
307
308         session = starget_to_session(scsi_target(sc->device));
309         ddb_entry = session->dd_data;
310
311         /* if we are not logged in then the LLD is going to clean up the cmd */
312         if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
313                 return BLK_EH_RESET_TIMER;
314         else
315                 return BLK_EH_NOT_HANDLED;
316 }
317
318 static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session)
319 {
320         struct ddb_entry *ddb_entry = session->dd_data;
321         struct scsi_qla_host *ha = ddb_entry->ha;
322
323         if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
324                 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
325
326                 DEBUG2(printk("scsi%ld: %s: ddb [%d] session recovery timeout "
327                               "of (%d) secs exhausted, marking device DEAD.\n",
328                               ha->host_no, __func__, ddb_entry->fw_ddb_index,
329                               ddb_entry->sess->recovery_tmo));
330         }
331 }
332
333 static int qla4xxx_host_get_param(struct Scsi_Host *shost,
334                                   enum iscsi_host_param param, char *buf)
335 {
336         struct scsi_qla_host *ha = to_qla_host(shost);
337         int len;
338
339         switch (param) {
340         case ISCSI_HOST_PARAM_HWADDRESS:
341                 len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
342                 break;
343         case ISCSI_HOST_PARAM_IPADDRESS:
344                 len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
345                 break;
346         case ISCSI_HOST_PARAM_INITIATOR_NAME:
347                 len = sprintf(buf, "%s\n", ha->name_string);
348                 break;
349         default:
350                 return -ENOSYS;
351         }
352
353         return len;
354 }
355
356 static void qla4xxx_create_ipv4_iface(struct scsi_qla_host *ha)
357 {
358         if (ha->iface_ipv4)
359                 return;
360
361         /* IPv4 */
362         ha->iface_ipv4 = iscsi_create_iface(ha->host,
363                                             &qla4xxx_iscsi_transport,
364                                             ISCSI_IFACE_TYPE_IPV4, 0, 0);
365         if (!ha->iface_ipv4)
366                 ql4_printk(KERN_ERR, ha, "Could not create IPv4 iSCSI "
367                            "iface0.\n");
368 }
369
370 static void qla4xxx_create_ipv6_iface(struct scsi_qla_host *ha)
371 {
372         if (!ha->iface_ipv6_0)
373                 /* IPv6 iface-0 */
374                 ha->iface_ipv6_0 = iscsi_create_iface(ha->host,
375                                                       &qla4xxx_iscsi_transport,
376                                                       ISCSI_IFACE_TYPE_IPV6, 0,
377                                                       0);
378         if (!ha->iface_ipv6_0)
379                 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
380                            "iface0.\n");
381
382         if (!ha->iface_ipv6_1)
383                 /* IPv6 iface-1 */
384                 ha->iface_ipv6_1 = iscsi_create_iface(ha->host,
385                                                       &qla4xxx_iscsi_transport,
386                                                       ISCSI_IFACE_TYPE_IPV6, 1,
387                                                       0);
388         if (!ha->iface_ipv6_1)
389                 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
390                            "iface1.\n");
391 }
392
393 static void qla4xxx_create_ifaces(struct scsi_qla_host *ha)
394 {
395         if (ha->ip_config.ipv4_options & IPOPT_IPV4_PROTOCOL_ENABLE)
396                 qla4xxx_create_ipv4_iface(ha);
397
398         if (ha->ip_config.ipv6_options & IPV6_OPT_IPV6_PROTOCOL_ENABLE)
399                 qla4xxx_create_ipv6_iface(ha);
400 }
401
402 static void qla4xxx_destroy_ipv4_iface(struct scsi_qla_host *ha)
403 {
404         if (ha->iface_ipv4) {
405                 iscsi_destroy_iface(ha->iface_ipv4);
406                 ha->iface_ipv4 = NULL;
407         }
408 }
409
410 static void qla4xxx_destroy_ipv6_iface(struct scsi_qla_host *ha)
411 {
412         if (ha->iface_ipv6_0) {
413                 iscsi_destroy_iface(ha->iface_ipv6_0);
414                 ha->iface_ipv6_0 = NULL;
415         }
416         if (ha->iface_ipv6_1) {
417                 iscsi_destroy_iface(ha->iface_ipv6_1);
418                 ha->iface_ipv6_1 = NULL;
419         }
420 }
421
422 static void qla4xxx_destroy_ifaces(struct scsi_qla_host *ha)
423 {
424         qla4xxx_destroy_ipv4_iface(ha);
425         qla4xxx_destroy_ipv6_iface(ha);
426 }
427
428 static void qla4xxx_set_ipv6(struct scsi_qla_host *ha,
429                              struct iscsi_iface_param_info *iface_param,
430                              struct addr_ctrl_blk *init_fw_cb)
431 {
432         /*
433          * iface_num 0 is valid for IPv6 Addr, linklocal, router, autocfg.
434          * iface_num 1 is valid only for IPv6 Addr.
435          */
436         switch (iface_param->param) {
437         case ISCSI_NET_PARAM_IPV6_ADDR:
438                 if (iface_param->iface_num & 0x1)
439                         /* IPv6 Addr 1 */
440                         memcpy(init_fw_cb->ipv6_addr1, iface_param->value,
441                                sizeof(init_fw_cb->ipv6_addr1));
442                 else
443                         /* IPv6 Addr 0 */
444                         memcpy(init_fw_cb->ipv6_addr0, iface_param->value,
445                                sizeof(init_fw_cb->ipv6_addr0));
446                 break;
447         case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
448                 if (iface_param->iface_num & 0x1)
449                         break;
450                 memcpy(init_fw_cb->ipv6_if_id, &iface_param->value[8],
451                        sizeof(init_fw_cb->ipv6_if_id));
452                 break;
453         case ISCSI_NET_PARAM_IPV6_ROUTER:
454                 if (iface_param->iface_num & 0x1)
455                         break;
456                 memcpy(init_fw_cb->ipv6_dflt_rtr_addr, iface_param->value,
457                        sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
458                 break;
459         case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
460                 /* Autocfg applies to even interface */
461                 if (iface_param->iface_num & 0x1)
462                         break;
463
464                 if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_DISABLE)
465                         init_fw_cb->ipv6_addtl_opts &=
466                                 cpu_to_le16(
467                                   ~IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
468                 else if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_ND_ENABLE)
469                         init_fw_cb->ipv6_addtl_opts |=
470                                 cpu_to_le16(
471                                   IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
472                 else
473                         ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
474                                    "IPv6 addr\n");
475                 break;
476         case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
477                 /* Autocfg applies to even interface */
478                 if (iface_param->iface_num & 0x1)
479                         break;
480
481                 if (iface_param->value[0] ==
482                     ISCSI_IPV6_LINKLOCAL_AUTOCFG_ENABLE)
483                         init_fw_cb->ipv6_addtl_opts |= cpu_to_le16(
484                                         IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
485                 else if (iface_param->value[0] ==
486                          ISCSI_IPV6_LINKLOCAL_AUTOCFG_DISABLE)
487                         init_fw_cb->ipv6_addtl_opts &= cpu_to_le16(
488                                        ~IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
489                 else
490                         ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
491                                    "IPv6 linklocal addr\n");
492                 break;
493         case ISCSI_NET_PARAM_IPV6_ROUTER_AUTOCFG:
494                 /* Autocfg applies to even interface */
495                 if (iface_param->iface_num & 0x1)
496                         break;
497
498                 if (iface_param->value[0] == ISCSI_IPV6_ROUTER_AUTOCFG_ENABLE)
499                         memset(init_fw_cb->ipv6_dflt_rtr_addr, 0,
500                                sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
501                 break;
502         case ISCSI_NET_PARAM_IFACE_ENABLE:
503                 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
504                         init_fw_cb->ipv6_opts |=
505                                 cpu_to_le16(IPV6_OPT_IPV6_PROTOCOL_ENABLE);
506                         qla4xxx_create_ipv6_iface(ha);
507                 } else {
508                         init_fw_cb->ipv6_opts &=
509                                 cpu_to_le16(~IPV6_OPT_IPV6_PROTOCOL_ENABLE &
510                                             0xFFFF);
511                         qla4xxx_destroy_ipv6_iface(ha);
512                 }
513                 break;
514         case ISCSI_NET_PARAM_VLAN_ID:
515                 if (iface_param->len != sizeof(init_fw_cb->ipv6_vlan_tag))
516                         break;
517                 init_fw_cb->ipv6_vlan_tag =
518                                 cpu_to_be16(*(uint16_t *)iface_param->value);
519                 break;
520         case ISCSI_NET_PARAM_VLAN_ENABLED:
521                 if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
522                         init_fw_cb->ipv6_opts |=
523                                 cpu_to_le16(IPV6_OPT_VLAN_TAGGING_ENABLE);
524                 else
525                         init_fw_cb->ipv6_opts &=
526                                 cpu_to_le16(~IPV6_OPT_VLAN_TAGGING_ENABLE);
527                 break;
528         default:
529                 ql4_printk(KERN_ERR, ha, "Unknown IPv6 param = %d\n",
530                            iface_param->param);
531                 break;
532         }
533 }
534
535 static void qla4xxx_set_ipv4(struct scsi_qla_host *ha,
536                              struct iscsi_iface_param_info *iface_param,
537                              struct addr_ctrl_blk *init_fw_cb)
538 {
539         switch (iface_param->param) {
540         case ISCSI_NET_PARAM_IPV4_ADDR:
541                 memcpy(init_fw_cb->ipv4_addr, iface_param->value,
542                        sizeof(init_fw_cb->ipv4_addr));
543                 break;
544         case ISCSI_NET_PARAM_IPV4_SUBNET:
545                 memcpy(init_fw_cb->ipv4_subnet, iface_param->value,
546                        sizeof(init_fw_cb->ipv4_subnet));
547                 break;
548         case ISCSI_NET_PARAM_IPV4_GW:
549                 memcpy(init_fw_cb->ipv4_gw_addr, iface_param->value,
550                        sizeof(init_fw_cb->ipv4_gw_addr));
551                 break;
552         case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
553                 if (iface_param->value[0] == ISCSI_BOOTPROTO_DHCP)
554                         init_fw_cb->ipv4_tcp_opts |=
555                                         cpu_to_le16(TCPOPT_DHCP_ENABLE);
556                 else if (iface_param->value[0] == ISCSI_BOOTPROTO_STATIC)
557                         init_fw_cb->ipv4_tcp_opts &=
558                                         cpu_to_le16(~TCPOPT_DHCP_ENABLE);
559                 else
560                         ql4_printk(KERN_ERR, ha, "Invalid IPv4 bootproto\n");
561                 break;
562         case ISCSI_NET_PARAM_IFACE_ENABLE:
563                 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
564                         init_fw_cb->ipv4_ip_opts |=
565                                 cpu_to_le16(IPOPT_IPV4_PROTOCOL_ENABLE);
566                         qla4xxx_create_ipv4_iface(ha);
567                 } else {
568                         init_fw_cb->ipv4_ip_opts &=
569                                 cpu_to_le16(~IPOPT_IPV4_PROTOCOL_ENABLE &
570                                             0xFFFF);
571                         qla4xxx_destroy_ipv4_iface(ha);
572                 }
573                 break;
574         case ISCSI_NET_PARAM_VLAN_ID:
575                 if (iface_param->len != sizeof(init_fw_cb->ipv4_vlan_tag))
576                         break;
577                 init_fw_cb->ipv4_vlan_tag =
578                                 cpu_to_be16(*(uint16_t *)iface_param->value);
579                 break;
580         case ISCSI_NET_PARAM_VLAN_ENABLED:
581                 if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
582                         init_fw_cb->ipv4_ip_opts |=
583                                         cpu_to_le16(IPOPT_VLAN_TAGGING_ENABLE);
584                 else
585                         init_fw_cb->ipv4_ip_opts &=
586                                         cpu_to_le16(~IPOPT_VLAN_TAGGING_ENABLE);
587                 break;
588         default:
589                 ql4_printk(KERN_ERR, ha, "Unknown IPv4 param = %d\n",
590                            iface_param->param);
591                 break;
592         }
593 }
594
595 static void
596 qla4xxx_initcb_to_acb(struct addr_ctrl_blk *init_fw_cb)
597 {
598         struct addr_ctrl_blk_def *acb;
599         acb = (struct addr_ctrl_blk_def *)init_fw_cb;
600         memset(acb->reserved1, 0, sizeof(acb->reserved1));
601         memset(acb->reserved2, 0, sizeof(acb->reserved2));
602         memset(acb->reserved3, 0, sizeof(acb->reserved3));
603         memset(acb->reserved4, 0, sizeof(acb->reserved4));
604         memset(acb->reserved5, 0, sizeof(acb->reserved5));
605         memset(acb->reserved6, 0, sizeof(acb->reserved6));
606         memset(acb->reserved7, 0, sizeof(acb->reserved7));
607         memset(acb->reserved8, 0, sizeof(acb->reserved8));
608         memset(acb->reserved9, 0, sizeof(acb->reserved9));
609         memset(acb->reserved10, 0, sizeof(acb->reserved10));
610         memset(acb->reserved11, 0, sizeof(acb->reserved11));
611         memset(acb->reserved12, 0, sizeof(acb->reserved12));
612         memset(acb->reserved13, 0, sizeof(acb->reserved13));
613         memset(acb->reserved14, 0, sizeof(acb->reserved14));
614         memset(acb->reserved15, 0, sizeof(acb->reserved15));
615 }
616
617 static int
618 qla4xxx_iface_set_param(struct Scsi_Host *shost, char *data, int count)
619 {
620         struct scsi_qla_host *ha = to_qla_host(shost);
621         int rval = 0;
622         struct iscsi_iface_param_info *iface_param = NULL;
623         struct addr_ctrl_blk *init_fw_cb = NULL;
624         dma_addr_t init_fw_cb_dma;
625         uint32_t mbox_cmd[MBOX_REG_COUNT];
626         uint32_t mbox_sts[MBOX_REG_COUNT];
627         uint32_t total_param_count;
628         uint32_t length;
629
630         init_fw_cb = dma_alloc_coherent(&ha->pdev->dev,
631                                         sizeof(struct addr_ctrl_blk),
632                                         &init_fw_cb_dma, GFP_KERNEL);
633         if (!init_fw_cb) {
634                 ql4_printk(KERN_ERR, ha, "%s: Unable to alloc init_cb\n",
635                            __func__);
636                 return -ENOMEM;
637         }
638
639         memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
640         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
641         memset(&mbox_sts, 0, sizeof(mbox_sts));
642
643         if (qla4xxx_get_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma)) {
644                 ql4_printk(KERN_ERR, ha, "%s: get ifcb failed\n", __func__);
645                 rval = -EIO;
646                 goto exit_init_fw_cb;
647         }
648
649         total_param_count = count;
650         iface_param = (struct iscsi_iface_param_info *)data;
651
652         for ( ; total_param_count != 0; total_param_count--) {
653                 length = iface_param->len;
654
655                 if (iface_param->param_type != ISCSI_NET_PARAM)
656                         continue;
657
658                 switch (iface_param->iface_type) {
659                 case ISCSI_IFACE_TYPE_IPV4:
660                         switch (iface_param->iface_num) {
661                         case 0:
662                                 qla4xxx_set_ipv4(ha, iface_param, init_fw_cb);
663                                 break;
664                         default:
665                                 /* Cannot have more than one IPv4 interface */
666                                 ql4_printk(KERN_ERR, ha, "Invalid IPv4 iface "
667                                            "number = %d\n",
668                                            iface_param->iface_num);
669                                 break;
670                         }
671                         break;
672                 case ISCSI_IFACE_TYPE_IPV6:
673                         switch (iface_param->iface_num) {
674                         case 0:
675                         case 1:
676                                 qla4xxx_set_ipv6(ha, iface_param, init_fw_cb);
677                                 break;
678                         default:
679                                 /* Cannot have more than two IPv6 interface */
680                                 ql4_printk(KERN_ERR, ha, "Invalid IPv6 iface "
681                                            "number = %d\n",
682                                            iface_param->iface_num);
683                                 break;
684                         }
685                         break;
686                 default:
687                         ql4_printk(KERN_ERR, ha, "Invalid iface type\n");
688                         break;
689                 }
690
691                 iface_param = (struct iscsi_iface_param_info *)
692                                                 ((uint8_t *)iface_param +
693                             sizeof(struct iscsi_iface_param_info) + length);
694         }
695
696         init_fw_cb->cookie = cpu_to_le32(0x11BEAD5A);
697
698         rval = qla4xxx_set_flash(ha, init_fw_cb_dma, FLASH_SEGMENT_IFCB,
699                                  sizeof(struct addr_ctrl_blk),
700                                  FLASH_OPT_RMW_COMMIT);
701         if (rval != QLA_SUCCESS) {
702                 ql4_printk(KERN_ERR, ha, "%s: set flash mbx failed\n",
703                            __func__);
704                 rval = -EIO;
705                 goto exit_init_fw_cb;
706         }
707
708         qla4xxx_disable_acb(ha);
709
710         qla4xxx_initcb_to_acb(init_fw_cb);
711
712         rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma);
713         if (rval != QLA_SUCCESS) {
714                 ql4_printk(KERN_ERR, ha, "%s: set acb mbx failed\n",
715                            __func__);
716                 rval = -EIO;
717                 goto exit_init_fw_cb;
718         }
719
720         memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
721         qla4xxx_update_local_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb,
722                                   init_fw_cb_dma);
723
724 exit_init_fw_cb:
725         dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk),
726                           init_fw_cb, init_fw_cb_dma);
727
728         return rval;
729 }
730
731 static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
732                                   enum iscsi_param param, char *buf)
733 {
734         struct ddb_entry *ddb_entry = sess->dd_data;
735         int len;
736
737         switch (param) {
738         case ISCSI_PARAM_TARGET_NAME:
739                 len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
740                                ddb_entry->iscsi_name);
741                 break;
742         case ISCSI_PARAM_TPGT:
743                 len = sprintf(buf, "%u\n", ddb_entry->tpgt);
744                 break;
745         case ISCSI_PARAM_TARGET_ALIAS:
746                 len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
747                     ddb_entry->iscsi_alias);
748                 break;
749         default:
750                 return -ENOSYS;
751         }
752
753         return len;
754 }
755
756 static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
757                                   enum iscsi_param param, char *buf)
758 {
759         struct iscsi_cls_session *session;
760         struct ddb_entry *ddb_entry;
761         int len;
762
763         session = iscsi_dev_to_session(conn->dev.parent);
764         ddb_entry = session->dd_data;
765
766         switch (param) {
767         case ISCSI_PARAM_CONN_PORT:
768                 len = sprintf(buf, "%hu\n", ddb_entry->port);
769                 break;
770         case ISCSI_PARAM_CONN_ADDRESS:
771                 /* TODO: what are the ipv6 bits */
772                 len = sprintf(buf, "%pI4\n", &ddb_entry->ip_addr);
773                 break;
774         default:
775                 return -ENOSYS;
776         }
777
778         return len;
779 }
780
781 static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
782                              enum iscsi_tgt_dscvr type, uint32_t enable,
783                              struct sockaddr *dst_addr)
784 {
785         struct scsi_qla_host *ha;
786         struct sockaddr_in *addr;
787         struct sockaddr_in6 *addr6;
788         int ret = 0;
789
790         ha = (struct scsi_qla_host *) shost->hostdata;
791
792         switch (type) {
793         case ISCSI_TGT_DSCVR_SEND_TARGETS:
794                 if (dst_addr->sa_family == AF_INET) {
795                         addr = (struct sockaddr_in *)dst_addr;
796                         if (qla4xxx_send_tgts(ha, (char *)&addr->sin_addr,
797                                               addr->sin_port) != QLA_SUCCESS)
798                                 ret = -EIO;
799                 } else if (dst_addr->sa_family == AF_INET6) {
800                         /*
801                          * TODO: fix qla4xxx_send_tgts
802                          */
803                         addr6 = (struct sockaddr_in6 *)dst_addr;
804                         if (qla4xxx_send_tgts(ha, (char *)&addr6->sin6_addr,
805                                               addr6->sin6_port) != QLA_SUCCESS)
806                                 ret = -EIO;
807                 } else
808                         ret = -ENOSYS;
809                 break;
810         default:
811                 ret = -ENOSYS;
812         }
813         return ret;
814 }
815
816 void qla4xxx_destroy_sess(struct ddb_entry *ddb_entry)
817 {
818         if (!ddb_entry->sess)
819                 return;
820
821         if (ddb_entry->conn) {
822                 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
823                 iscsi_remove_session(ddb_entry->sess);
824         }
825         iscsi_free_session(ddb_entry->sess);
826 }
827
828 int qla4xxx_add_sess(struct ddb_entry *ddb_entry)
829 {
830         int err;
831
832         ddb_entry->sess->recovery_tmo = ql4xsess_recovery_tmo;
833
834         err = iscsi_add_session(ddb_entry->sess, ddb_entry->fw_ddb_index);
835         if (err) {
836                 DEBUG2(printk(KERN_ERR "Could not add session.\n"));
837                 return err;
838         }
839
840         ddb_entry->conn = iscsi_create_conn(ddb_entry->sess, 0, 0);
841         if (!ddb_entry->conn) {
842                 iscsi_remove_session(ddb_entry->sess);
843                 DEBUG2(printk(KERN_ERR "Could not add connection.\n"));
844                 return -ENOMEM;
845         }
846
847         /* finally ready to go */
848         iscsi_unblock_session(ddb_entry->sess);
849         return 0;
850 }
851
852 struct ddb_entry *qla4xxx_alloc_sess(struct scsi_qla_host *ha)
853 {
854         struct ddb_entry *ddb_entry;
855         struct iscsi_cls_session *sess;
856
857         sess = iscsi_alloc_session(ha->host, &qla4xxx_iscsi_transport,
858                                    sizeof(struct ddb_entry));
859         if (!sess)
860                 return NULL;
861
862         ddb_entry = sess->dd_data;
863         memset(ddb_entry, 0, sizeof(*ddb_entry));
864         ddb_entry->ha = ha;
865         ddb_entry->sess = sess;
866         return ddb_entry;
867 }
868
869 static void qla4xxx_scan_start(struct Scsi_Host *shost)
870 {
871         struct scsi_qla_host *ha = shost_priv(shost);
872         struct ddb_entry *ddb_entry, *ddbtemp;
873
874         /* finish setup of sessions that were already setup in firmware */
875         list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
876                 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE)
877                         qla4xxx_add_sess(ddb_entry);
878         }
879 }
880
881 /*
882  * Timer routines
883  */
884
885 static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
886                                 unsigned long interval)
887 {
888         DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
889                      __func__, ha->host->host_no));
890         init_timer(&ha->timer);
891         ha->timer.expires = jiffies + interval * HZ;
892         ha->timer.data = (unsigned long)ha;
893         ha->timer.function = (void (*)(unsigned long))func;
894         add_timer(&ha->timer);
895         ha->timer_active = 1;
896 }
897
898 static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
899 {
900         del_timer_sync(&ha->timer);
901         ha->timer_active = 0;
902 }
903
904 /***
905  * qla4xxx_mark_device_missing - mark a device as missing.
906  * @ha: Pointer to host adapter structure.
907  * @ddb_entry: Pointer to device database entry
908  *
909  * This routine marks a device missing and close connection.
910  **/
911 void qla4xxx_mark_device_missing(struct scsi_qla_host *ha,
912                                  struct ddb_entry *ddb_entry)
913 {
914         if ((atomic_read(&ddb_entry->state) != DDB_STATE_DEAD)) {
915                 atomic_set(&ddb_entry->state, DDB_STATE_MISSING);
916                 DEBUG2(printk("scsi%ld: ddb [%d] marked MISSING\n",
917                     ha->host_no, ddb_entry->fw_ddb_index));
918         } else
919                 DEBUG2(printk("scsi%ld: ddb [%d] DEAD\n", ha->host_no,
920                     ddb_entry->fw_ddb_index))
921
922         iscsi_block_session(ddb_entry->sess);
923         iscsi_conn_error_event(ddb_entry->conn, ISCSI_ERR_CONN_FAILED);
924 }
925
926 /**
927  * qla4xxx_mark_all_devices_missing - mark all devices as missing.
928  * @ha: Pointer to host adapter structure.
929  *
930  * This routine marks a device missing and resets the relogin retry count.
931  **/
932 void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
933 {
934         struct ddb_entry *ddb_entry, *ddbtemp;
935         list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
936                 qla4xxx_mark_device_missing(ha, ddb_entry);
937         }
938 }
939
940 static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
941                                        struct ddb_entry *ddb_entry,
942                                        struct scsi_cmnd *cmd)
943 {
944         struct srb *srb;
945
946         srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
947         if (!srb)
948                 return srb;
949
950         kref_init(&srb->srb_ref);
951         srb->ha = ha;
952         srb->ddb = ddb_entry;
953         srb->cmd = cmd;
954         srb->flags = 0;
955         CMD_SP(cmd) = (void *)srb;
956
957         return srb;
958 }
959
960 static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
961 {
962         struct scsi_cmnd *cmd = srb->cmd;
963
964         if (srb->flags & SRB_DMA_VALID) {
965                 scsi_dma_unmap(cmd);
966                 srb->flags &= ~SRB_DMA_VALID;
967         }
968         CMD_SP(cmd) = NULL;
969 }
970
971 void qla4xxx_srb_compl(struct kref *ref)
972 {
973         struct srb *srb = container_of(ref, struct srb, srb_ref);
974         struct scsi_cmnd *cmd = srb->cmd;
975         struct scsi_qla_host *ha = srb->ha;
976
977         qla4xxx_srb_free_dma(ha, srb);
978
979         mempool_free(srb, ha->srb_mempool);
980
981         cmd->scsi_done(cmd);
982 }
983
984 /**
985  * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
986  * @host: scsi host
987  * @cmd: Pointer to Linux's SCSI command structure
988  *
989  * Remarks:
990  * This routine is invoked by Linux to send a SCSI command to the driver.
991  * The mid-level driver tries to ensure that queuecommand never gets
992  * invoked concurrently with itself or the interrupt handler (although
993  * the interrupt handler may call this routine as part of request-
994  * completion handling).   Unfortunely, it sometimes calls the scheduler
995  * in interrupt context which is a big NO! NO!.
996  **/
997 static int qla4xxx_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
998 {
999         struct scsi_qla_host *ha = to_qla_host(host);
1000         struct ddb_entry *ddb_entry = cmd->device->hostdata;
1001         struct iscsi_cls_session *sess = ddb_entry->sess;
1002         struct srb *srb;
1003         int rval;
1004
1005         if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1006                 if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
1007                         cmd->result = DID_NO_CONNECT << 16;
1008                 else
1009                         cmd->result = DID_REQUEUE << 16;
1010                 goto qc_fail_command;
1011         }
1012
1013         if (!sess) {
1014                 cmd->result = DID_IMM_RETRY << 16;
1015                 goto qc_fail_command;
1016         }
1017
1018         rval = iscsi_session_chkready(sess);
1019         if (rval) {
1020                 cmd->result = rval;
1021                 goto qc_fail_command;
1022         }
1023
1024         if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
1025                 if (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD) {
1026                         cmd->result = DID_NO_CONNECT << 16;
1027                         goto qc_fail_command;
1028                 }
1029                 return SCSI_MLQUEUE_TARGET_BUSY;
1030         }
1031
1032         if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1033             test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
1034             test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1035             test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
1036             test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
1037             !test_bit(AF_ONLINE, &ha->flags) ||
1038             test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
1039                 goto qc_host_busy;
1040
1041         srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd);
1042         if (!srb)
1043                 goto qc_host_busy;
1044
1045         rval = qla4xxx_send_command_to_isp(ha, srb);
1046         if (rval != QLA_SUCCESS)
1047                 goto qc_host_busy_free_sp;
1048
1049         return 0;
1050
1051 qc_host_busy_free_sp:
1052         qla4xxx_srb_free_dma(ha, srb);
1053         mempool_free(srb, ha->srb_mempool);
1054
1055 qc_host_busy:
1056         return SCSI_MLQUEUE_HOST_BUSY;
1057
1058 qc_fail_command:
1059         cmd->scsi_done(cmd);
1060
1061         return 0;
1062 }
1063
1064 /**
1065  * qla4xxx_mem_free - frees memory allocated to adapter
1066  * @ha: Pointer to host adapter structure.
1067  *
1068  * Frees memory previously allocated by qla4xxx_mem_alloc
1069  **/
1070 static void qla4xxx_mem_free(struct scsi_qla_host *ha)
1071 {
1072         if (ha->queues)
1073                 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
1074                                   ha->queues_dma);
1075
1076         ha->queues_len = 0;
1077         ha->queues = NULL;
1078         ha->queues_dma = 0;
1079         ha->request_ring = NULL;
1080         ha->request_dma = 0;
1081         ha->response_ring = NULL;
1082         ha->response_dma = 0;
1083         ha->shadow_regs = NULL;
1084         ha->shadow_regs_dma = 0;
1085
1086         /* Free srb pool. */
1087         if (ha->srb_mempool)
1088                 mempool_destroy(ha->srb_mempool);
1089
1090         ha->srb_mempool = NULL;
1091
1092         /* release io space registers  */
1093         if (is_qla8022(ha)) {
1094                 if (ha->nx_pcibase)
1095                         iounmap(
1096                             (struct device_reg_82xx __iomem *)ha->nx_pcibase);
1097         } else if (ha->reg)
1098                 iounmap(ha->reg);
1099         pci_release_regions(ha->pdev);
1100 }
1101
1102 /**
1103  * qla4xxx_mem_alloc - allocates memory for use by adapter.
1104  * @ha: Pointer to host adapter structure
1105  *
1106  * Allocates DMA memory for request and response queues. Also allocates memory
1107  * for srbs.
1108  **/
1109 static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
1110 {
1111         unsigned long align;
1112
1113         /* Allocate contiguous block of DMA memory for queues. */
1114         ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
1115                           (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
1116                           sizeof(struct shadow_regs) +
1117                           MEM_ALIGN_VALUE +
1118                           (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
1119         ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
1120                                         &ha->queues_dma, GFP_KERNEL);
1121         if (ha->queues == NULL) {
1122                 ql4_printk(KERN_WARNING, ha,
1123                     "Memory Allocation failed - queues.\n");
1124
1125                 goto mem_alloc_error_exit;
1126         }
1127         memset(ha->queues, 0, ha->queues_len);
1128
1129         /*
1130          * As per RISC alignment requirements -- the bus-address must be a
1131          * multiple of the request-ring size (in bytes).
1132          */
1133         align = 0;
1134         if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
1135                 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
1136                                            (MEM_ALIGN_VALUE - 1));
1137
1138         /* Update request and response queue pointers. */
1139         ha->request_dma = ha->queues_dma + align;
1140         ha->request_ring = (struct queue_entry *) (ha->queues + align);
1141         ha->response_dma = ha->queues_dma + align +
1142                 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
1143         ha->response_ring = (struct queue_entry *) (ha->queues + align +
1144                                                     (REQUEST_QUEUE_DEPTH *
1145                                                      QUEUE_SIZE));
1146         ha->shadow_regs_dma = ha->queues_dma + align +
1147                 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
1148                 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
1149         ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
1150                                                   (REQUEST_QUEUE_DEPTH *
1151                                                    QUEUE_SIZE) +
1152                                                   (RESPONSE_QUEUE_DEPTH *
1153                                                    QUEUE_SIZE));
1154
1155         /* Allocate memory for srb pool. */
1156         ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
1157                                          mempool_free_slab, srb_cachep);
1158         if (ha->srb_mempool == NULL) {
1159                 ql4_printk(KERN_WARNING, ha,
1160                     "Memory Allocation failed - SRB Pool.\n");
1161
1162                 goto mem_alloc_error_exit;
1163         }
1164
1165         return QLA_SUCCESS;
1166
1167 mem_alloc_error_exit:
1168         qla4xxx_mem_free(ha);
1169         return QLA_ERROR;
1170 }
1171
1172 /**
1173  * qla4_8xxx_check_fw_alive  - Check firmware health
1174  * @ha: Pointer to host adapter structure.
1175  *
1176  * Context: Interrupt
1177  **/
1178 static void qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
1179 {
1180         uint32_t fw_heartbeat_counter, halt_status;
1181
1182         fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
1183         /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
1184         if (fw_heartbeat_counter == 0xffffffff) {
1185                 DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
1186                     "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
1187                     ha->host_no, __func__));
1188                 return;
1189         }
1190
1191         if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
1192                 ha->seconds_since_last_heartbeat++;
1193                 /* FW not alive after 2 seconds */
1194                 if (ha->seconds_since_last_heartbeat == 2) {
1195                         ha->seconds_since_last_heartbeat = 0;
1196                         halt_status = qla4_8xxx_rd_32(ha,
1197                                                       QLA82XX_PEG_HALT_STATUS1);
1198
1199                         ql4_printk(KERN_INFO, ha,
1200                                    "scsi(%ld): %s, Dumping hw/fw registers:\n "
1201                                    " PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2:"
1202                                    " 0x%x,\n PEG_NET_0_PC: 0x%x, PEG_NET_1_PC:"
1203                                    " 0x%x,\n PEG_NET_2_PC: 0x%x, PEG_NET_3_PC:"
1204                                    " 0x%x,\n PEG_NET_4_PC: 0x%x\n",
1205                                    ha->host_no, __func__, halt_status,
1206                                    qla4_8xxx_rd_32(ha,
1207                                                    QLA82XX_PEG_HALT_STATUS2),
1208                                    qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_0 +
1209                                                    0x3c),
1210                                    qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_1 +
1211                                                    0x3c),
1212                                    qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_2 +
1213                                                    0x3c),
1214                                    qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_3 +
1215                                                    0x3c),
1216                                    qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_4 +
1217                                                    0x3c));
1218
1219                         /* Since we cannot change dev_state in interrupt
1220                          * context, set appropriate DPC flag then wakeup
1221                          * DPC */
1222                         if (halt_status & HALT_STATUS_UNRECOVERABLE)
1223                                 set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
1224                         else {
1225                                 printk("scsi%ld: %s: detect abort needed!\n",
1226                                     ha->host_no, __func__);
1227                                 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1228                         }
1229                         qla4xxx_wake_dpc(ha);
1230                         qla4xxx_mailbox_premature_completion(ha);
1231                 }
1232         } else
1233                 ha->seconds_since_last_heartbeat = 0;
1234
1235         ha->fw_heartbeat_counter = fw_heartbeat_counter;
1236 }
1237
1238 /**
1239  * qla4_8xxx_watchdog - Poll dev state
1240  * @ha: Pointer to host adapter structure.
1241  *
1242  * Context: Interrupt
1243  **/
1244 void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
1245 {
1246         uint32_t dev_state;
1247
1248         dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
1249
1250         /* don't poll if reset is going on */
1251         if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
1252             test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1253             test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags))) {
1254                 if (dev_state == QLA82XX_DEV_NEED_RESET &&
1255                     !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
1256                         if (!ql4xdontresethba) {
1257                                 ql4_printk(KERN_INFO, ha, "%s: HW State: "
1258                                     "NEED RESET!\n", __func__);
1259                                 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1260                                 qla4xxx_wake_dpc(ha);
1261                                 qla4xxx_mailbox_premature_completion(ha);
1262                         }
1263                 } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
1264                     !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
1265                         ql4_printk(KERN_INFO, ha, "%s: HW State: NEED QUIES!\n",
1266                             __func__);
1267                         set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
1268                         qla4xxx_wake_dpc(ha);
1269                 } else  {
1270                         /* Check firmware health */
1271                         qla4_8xxx_check_fw_alive(ha);
1272                 }
1273         }
1274 }
1275
1276 /**
1277  * qla4xxx_timer - checks every second for work to do.
1278  * @ha: Pointer to host adapter structure.
1279  **/
1280 static void qla4xxx_timer(struct scsi_qla_host *ha)
1281 {
1282         struct ddb_entry *ddb_entry, *dtemp;
1283         int start_dpc = 0;
1284         uint16_t w;
1285
1286         /* If we are in the middle of AER/EEH processing
1287          * skip any processing and reschedule the timer
1288          */
1289         if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1290                 mod_timer(&ha->timer, jiffies + HZ);
1291                 return;
1292         }
1293
1294         /* Hardware read to trigger an EEH error during mailbox waits. */
1295         if (!pci_channel_offline(ha->pdev))
1296                 pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
1297
1298         if (is_qla8022(ha)) {
1299                 qla4_8xxx_watchdog(ha);
1300         }
1301
1302         /* Search for relogin's to time-out and port down retry. */
1303         list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
1304                 /* Count down time between sending relogins */
1305                 if (adapter_up(ha) &&
1306                     !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
1307                     atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
1308                         if (atomic_read(&ddb_entry->retry_relogin_timer) !=
1309                             INVALID_ENTRY) {
1310                                 if (atomic_read(&ddb_entry->retry_relogin_timer)
1311                                                 == 0) {
1312                                         atomic_set(&ddb_entry->
1313                                                 retry_relogin_timer,
1314                                                 INVALID_ENTRY);
1315                                         set_bit(DPC_RELOGIN_DEVICE,
1316                                                 &ha->dpc_flags);
1317                                         set_bit(DF_RELOGIN, &ddb_entry->flags);
1318                                         DEBUG2(printk("scsi%ld: %s: ddb [%d]"
1319                                                       " login device\n",
1320                                                       ha->host_no, __func__,
1321                                                       ddb_entry->fw_ddb_index));
1322                                 } else
1323                                         atomic_dec(&ddb_entry->
1324                                                         retry_relogin_timer);
1325                         }
1326                 }
1327
1328                 /* Wait for relogin to timeout */
1329                 if (atomic_read(&ddb_entry->relogin_timer) &&
1330                     (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
1331                         /*
1332                          * If the relogin times out and the device is
1333                          * still NOT ONLINE then try and relogin again.
1334                          */
1335                         if (atomic_read(&ddb_entry->state) !=
1336                             DDB_STATE_ONLINE &&
1337                             ddb_entry->fw_ddb_device_state ==
1338                             DDB_DS_SESSION_FAILED) {
1339                                 /* Reset retry relogin timer */
1340                                 atomic_inc(&ddb_entry->relogin_retry_count);
1341                                 DEBUG2(printk("scsi%ld: ddb [%d] relogin"
1342                                               " timed out-retrying"
1343                                               " relogin (%d)\n",
1344                                               ha->host_no,
1345                                               ddb_entry->fw_ddb_index,
1346                                               atomic_read(&ddb_entry->
1347                                                           relogin_retry_count))
1348                                         );
1349                                 start_dpc++;
1350                                 DEBUG(printk("scsi%ld:%d:%d: ddb [%d] "
1351                                              "initiate relogin after"
1352                                              " %d seconds\n",
1353                                              ha->host_no, ddb_entry->bus,
1354                                              ddb_entry->target,
1355                                              ddb_entry->fw_ddb_index,
1356                                              ddb_entry->default_time2wait + 4)
1357                                         );
1358
1359                                 atomic_set(&ddb_entry->retry_relogin_timer,
1360                                            ddb_entry->default_time2wait + 4);
1361                         }
1362                 }
1363         }
1364
1365         if (!is_qla8022(ha)) {
1366                 /* Check for heartbeat interval. */
1367                 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
1368                     ha->heartbeat_interval != 0) {
1369                         ha->seconds_since_last_heartbeat++;
1370                         if (ha->seconds_since_last_heartbeat >
1371                             ha->heartbeat_interval + 2)
1372                                 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1373                 }
1374         }
1375
1376         /* Wakeup the dpc routine for this adapter, if needed. */
1377         if (start_dpc ||
1378              test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1379              test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
1380              test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
1381              test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
1382              test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1383              test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
1384              test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
1385              test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
1386              test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
1387              test_bit(DPC_AEN, &ha->dpc_flags)) {
1388                 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
1389                               " - dpc flags = 0x%lx\n",
1390                               ha->host_no, __func__, ha->dpc_flags));
1391                 qla4xxx_wake_dpc(ha);
1392         }
1393
1394         /* Reschedule timer thread to call us back in one second */
1395         mod_timer(&ha->timer, jiffies + HZ);
1396
1397         DEBUG2(ha->seconds_since_last_intr++);
1398 }
1399
1400 /**
1401  * qla4xxx_cmd_wait - waits for all outstanding commands to complete
1402  * @ha: Pointer to host adapter structure.
1403  *
1404  * This routine stalls the driver until all outstanding commands are returned.
1405  * Caller must release the Hardware Lock prior to calling this routine.
1406  **/
1407 static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
1408 {
1409         uint32_t index = 0;
1410         unsigned long flags;
1411         struct scsi_cmnd *cmd;
1412
1413         unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
1414
1415         DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
1416             "complete\n", WAIT_CMD_TOV));
1417
1418         while (!time_after_eq(jiffies, wtime)) {
1419                 spin_lock_irqsave(&ha->hardware_lock, flags);
1420                 /* Find a command that hasn't completed. */
1421                 for (index = 0; index < ha->host->can_queue; index++) {
1422                         cmd = scsi_host_find_tag(ha->host, index);
1423                         /*
1424                          * We cannot just check if the index is valid,
1425                          * becase if we are run from the scsi eh, then
1426                          * the scsi/block layer is going to prevent
1427                          * the tag from being released.
1428                          */
1429                         if (cmd != NULL && CMD_SP(cmd))
1430                                 break;
1431                 }
1432                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1433
1434                 /* If No Commands are pending, wait is complete */
1435                 if (index == ha->host->can_queue)
1436                         return QLA_SUCCESS;
1437
1438                 msleep(1000);
1439         }
1440         /* If we timed out on waiting for commands to come back
1441          * return ERROR. */
1442         return QLA_ERROR;
1443 }
1444
1445 int qla4xxx_hw_reset(struct scsi_qla_host *ha)
1446 {
1447         uint32_t ctrl_status;
1448         unsigned long flags = 0;
1449
1450         DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
1451
1452         if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
1453                 return QLA_ERROR;
1454
1455         spin_lock_irqsave(&ha->hardware_lock, flags);
1456
1457         /*
1458          * If the SCSI Reset Interrupt bit is set, clear it.
1459          * Otherwise, the Soft Reset won't work.
1460          */
1461         ctrl_status = readw(&ha->reg->ctrl_status);
1462         if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
1463                 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
1464
1465         /* Issue Soft Reset */
1466         writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
1467         readl(&ha->reg->ctrl_status);
1468
1469         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1470         return QLA_SUCCESS;
1471 }
1472
1473 /**
1474  * qla4xxx_soft_reset - performs soft reset.
1475  * @ha: Pointer to host adapter structure.
1476  **/
1477 int qla4xxx_soft_reset(struct scsi_qla_host *ha)
1478 {
1479         uint32_t max_wait_time;
1480         unsigned long flags = 0;
1481         int status;
1482         uint32_t ctrl_status;
1483
1484         status = qla4xxx_hw_reset(ha);
1485         if (status != QLA_SUCCESS)
1486                 return status;
1487
1488         status = QLA_ERROR;
1489         /* Wait until the Network Reset Intr bit is cleared */
1490         max_wait_time = RESET_INTR_TOV;
1491         do {
1492                 spin_lock_irqsave(&ha->hardware_lock, flags);
1493                 ctrl_status = readw(&ha->reg->ctrl_status);
1494                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1495
1496                 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
1497                         break;
1498
1499                 msleep(1000);
1500         } while ((--max_wait_time));
1501
1502         if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
1503                 DEBUG2(printk(KERN_WARNING
1504                               "scsi%ld: Network Reset Intr not cleared by "
1505                               "Network function, clearing it now!\n",
1506                               ha->host_no));
1507                 spin_lock_irqsave(&ha->hardware_lock, flags);
1508                 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
1509                 readl(&ha->reg->ctrl_status);
1510                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1511         }
1512
1513         /* Wait until the firmware tells us the Soft Reset is done */
1514         max_wait_time = SOFT_RESET_TOV;
1515         do {
1516                 spin_lock_irqsave(&ha->hardware_lock, flags);
1517                 ctrl_status = readw(&ha->reg->ctrl_status);
1518                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1519
1520                 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
1521                         status = QLA_SUCCESS;
1522                         break;
1523                 }
1524
1525                 msleep(1000);
1526         } while ((--max_wait_time));
1527
1528         /*
1529          * Also, make sure that the SCSI Reset Interrupt bit has been cleared
1530          * after the soft reset has taken place.
1531          */
1532         spin_lock_irqsave(&ha->hardware_lock, flags);
1533         ctrl_status = readw(&ha->reg->ctrl_status);
1534         if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
1535                 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
1536                 readl(&ha->reg->ctrl_status);
1537         }
1538         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1539
1540         /* If soft reset fails then most probably the bios on other
1541          * function is also enabled.
1542          * Since the initialization is sequential the other fn
1543          * wont be able to acknowledge the soft reset.
1544          * Issue a force soft reset to workaround this scenario.
1545          */
1546         if (max_wait_time == 0) {
1547                 /* Issue Force Soft Reset */
1548                 spin_lock_irqsave(&ha->hardware_lock, flags);
1549                 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
1550                 readl(&ha->reg->ctrl_status);
1551                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1552                 /* Wait until the firmware tells us the Soft Reset is done */
1553                 max_wait_time = SOFT_RESET_TOV;
1554                 do {
1555                         spin_lock_irqsave(&ha->hardware_lock, flags);
1556                         ctrl_status = readw(&ha->reg->ctrl_status);
1557                         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1558
1559                         if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
1560                                 status = QLA_SUCCESS;
1561                                 break;
1562                         }
1563
1564                         msleep(1000);
1565                 } while ((--max_wait_time));
1566         }
1567
1568         return status;
1569 }
1570
1571 /**
1572  * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
1573  * @ha: Pointer to host adapter structure.
1574  * @res: returned scsi status
1575  *
1576  * This routine is called just prior to a HARD RESET to return all
1577  * outstanding commands back to the Operating System.
1578  * Caller should make sure that the following locks are released
1579  * before this calling routine: Hardware lock, and io_request_lock.
1580  **/
1581 static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
1582 {
1583         struct srb *srb;
1584         int i;
1585         unsigned long flags;
1586
1587         spin_lock_irqsave(&ha->hardware_lock, flags);
1588         for (i = 0; i < ha->host->can_queue; i++) {
1589                 srb = qla4xxx_del_from_active_array(ha, i);
1590                 if (srb != NULL) {
1591                         srb->cmd->result = res;
1592                         kref_put(&srb->srb_ref, qla4xxx_srb_compl);
1593                 }
1594         }
1595         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1596 }
1597
1598 void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
1599 {
1600         clear_bit(AF_ONLINE, &ha->flags);
1601
1602         /* Disable the board */
1603         ql4_printk(KERN_INFO, ha, "Disabling the board\n");
1604
1605         qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
1606         qla4xxx_mark_all_devices_missing(ha);
1607         clear_bit(AF_INIT_DONE, &ha->flags);
1608 }
1609
1610 /**
1611  * qla4xxx_recover_adapter - recovers adapter after a fatal error
1612  * @ha: Pointer to host adapter structure.
1613  **/
1614 static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
1615 {
1616         int status = QLA_ERROR;
1617         uint8_t reset_chip = 0;
1618
1619         /* Stall incoming I/O until we are done */
1620         scsi_block_requests(ha->host);
1621         clear_bit(AF_ONLINE, &ha->flags);
1622
1623         DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
1624
1625         set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
1626
1627         if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
1628                 reset_chip = 1;
1629
1630         /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
1631          * do not reset adapter, jump to initialize_adapter */
1632         if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1633                 status = QLA_SUCCESS;
1634                 goto recover_ha_init_adapter;
1635         }
1636
1637         /* For the ISP-82xx adapter, issue a stop_firmware if invoked
1638          * from eh_host_reset or ioctl module */
1639         if (is_qla8022(ha) && !reset_chip &&
1640             test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
1641
1642                 DEBUG2(ql4_printk(KERN_INFO, ha,
1643                     "scsi%ld: %s - Performing stop_firmware...\n",
1644                     ha->host_no, __func__));
1645                 status = ha->isp_ops->reset_firmware(ha);
1646                 if (status == QLA_SUCCESS) {
1647                         if (!test_bit(AF_FW_RECOVERY, &ha->flags))
1648                                 qla4xxx_cmd_wait(ha);
1649                         ha->isp_ops->disable_intrs(ha);
1650                         qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1651                         qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1652                 } else {
1653                         /* If the stop_firmware fails then
1654                          * reset the entire chip */
1655                         reset_chip = 1;
1656                         clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1657                         set_bit(DPC_RESET_HA, &ha->dpc_flags);
1658                 }
1659         }
1660
1661         /* Issue full chip reset if recovering from a catastrophic error,
1662          * or if stop_firmware fails for ISP-82xx.
1663          * This is the default case for ISP-4xxx */
1664         if (!is_qla8022(ha) || reset_chip) {
1665                 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
1666                         qla4xxx_cmd_wait(ha);
1667                 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1668                 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1669                 DEBUG2(ql4_printk(KERN_INFO, ha,
1670                     "scsi%ld: %s - Performing chip reset..\n",
1671                     ha->host_no, __func__));
1672                 status = ha->isp_ops->reset_chip(ha);
1673         }
1674
1675         /* Flush any pending ddb changed AENs */
1676         qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1677
1678 recover_ha_init_adapter:
1679         /* Upon successful firmware/chip reset, re-initialize the adapter */
1680         if (status == QLA_SUCCESS) {
1681                 /* For ISP-4xxx, force function 1 to always initialize
1682                  * before function 3 to prevent both funcions from
1683                  * stepping on top of the other */
1684                 if (!is_qla8022(ha) && (ha->mac_index == 3))
1685                         ssleep(6);
1686
1687                 /* NOTE: AF_ONLINE flag set upon successful completion of
1688                  *       qla4xxx_initialize_adapter */
1689                 status = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
1690         }
1691
1692         /* Retry failed adapter initialization, if necessary
1693          * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
1694          * case to prevent ping-pong resets between functions */
1695         if (!test_bit(AF_ONLINE, &ha->flags) &&
1696             !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1697                 /* Adapter initialization failed, see if we can retry
1698                  * resetting the ha.
1699                  * Since we don't want to block the DPC for too long
1700                  * with multiple resets in the same thread,
1701                  * utilize DPC to retry */
1702                 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
1703                         ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
1704                         DEBUG2(printk("scsi%ld: recover adapter - retrying "
1705                                       "(%d) more times\n", ha->host_no,
1706                                       ha->retry_reset_ha_cnt));
1707                         set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1708                         status = QLA_ERROR;
1709                 } else {
1710                         if (ha->retry_reset_ha_cnt > 0) {
1711                                 /* Schedule another Reset HA--DPC will retry */
1712                                 ha->retry_reset_ha_cnt--;
1713                                 DEBUG2(printk("scsi%ld: recover adapter - "
1714                                               "retry remaining %d\n",
1715                                               ha->host_no,
1716                                               ha->retry_reset_ha_cnt));
1717                                 status = QLA_ERROR;
1718                         }
1719
1720                         if (ha->retry_reset_ha_cnt == 0) {
1721                                 /* Recover adapter retries have been exhausted.
1722                                  * Adapter DEAD */
1723                                 DEBUG2(printk("scsi%ld: recover adapter "
1724                                               "failed - board disabled\n",
1725                                               ha->host_no));
1726                                 qla4xxx_dead_adapter_cleanup(ha);
1727                                 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1728                                 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
1729                                 clear_bit(DPC_RESET_HA_FW_CONTEXT,
1730                                           &ha->dpc_flags);
1731                                 status = QLA_ERROR;
1732                         }
1733                 }
1734         } else {
1735                 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
1736                 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1737                 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1738         }
1739
1740         ha->adapter_error_count++;
1741
1742         if (test_bit(AF_ONLINE, &ha->flags))
1743                 ha->isp_ops->enable_intrs(ha);
1744
1745         scsi_unblock_requests(ha->host);
1746
1747         clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
1748         DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
1749             status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
1750
1751         return status;
1752 }
1753
1754 static void qla4xxx_relogin_all_devices(struct scsi_qla_host *ha)
1755 {
1756         struct ddb_entry *ddb_entry, *dtemp;
1757
1758         list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
1759                 if ((atomic_read(&ddb_entry->state) == DDB_STATE_MISSING) ||
1760                     (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD)) {
1761                         if (ddb_entry->fw_ddb_device_state ==
1762                             DDB_DS_SESSION_ACTIVE) {
1763                                 atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
1764                                 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
1765                                     " marked ONLINE\n", ha->host_no, __func__,
1766                                     ddb_entry->fw_ddb_index);
1767
1768                                 iscsi_unblock_session(ddb_entry->sess);
1769                         } else
1770                                 qla4xxx_relogin_device(ha, ddb_entry);
1771                 }
1772         }
1773 }
1774
1775 void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
1776 {
1777         if (ha->dpc_thread)
1778                 queue_work(ha->dpc_thread, &ha->dpc_work);
1779 }
1780
1781 /**
1782  * qla4xxx_do_dpc - dpc routine
1783  * @data: in our case pointer to adapter structure
1784  *
1785  * This routine is a task that is schedule by the interrupt handler
1786  * to perform the background processing for interrupts.  We put it
1787  * on a task queue that is consumed whenever the scheduler runs; that's
1788  * so you can do anything (i.e. put the process to sleep etc).  In fact,
1789  * the mid-level tries to sleep when it reaches the driver threshold
1790  * "host->can_queue". This can cause a panic if we were in our interrupt code.
1791  **/
1792 static void qla4xxx_do_dpc(struct work_struct *work)
1793 {
1794         struct scsi_qla_host *ha =
1795                 container_of(work, struct scsi_qla_host, dpc_work);
1796         struct ddb_entry *ddb_entry, *dtemp;
1797         int status = QLA_ERROR;
1798
1799         DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
1800             "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
1801             ha->host_no, __func__, ha->flags, ha->dpc_flags))
1802
1803         /* Initialization not yet finished. Don't do anything yet. */
1804         if (!test_bit(AF_INIT_DONE, &ha->flags))
1805                 return;
1806
1807         if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1808                 DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
1809                     ha->host_no, __func__, ha->flags));
1810                 return;
1811         }
1812
1813         if (is_qla8022(ha)) {
1814                 if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
1815                         qla4_8xxx_idc_lock(ha);
1816                         qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
1817                             QLA82XX_DEV_FAILED);
1818                         qla4_8xxx_idc_unlock(ha);
1819                         ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
1820                         qla4_8xxx_device_state_handler(ha);
1821                 }
1822                 if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
1823                         qla4_8xxx_need_qsnt_handler(ha);
1824                 }
1825         }
1826
1827         if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
1828             (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1829             test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1830             test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
1831                 if (ql4xdontresethba) {
1832                         DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
1833                             ha->host_no, __func__));
1834                         clear_bit(DPC_RESET_HA, &ha->dpc_flags);
1835                         clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1836                         clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1837                         goto dpc_post_reset_ha;
1838                 }
1839                 if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
1840                     test_bit(DPC_RESET_HA, &ha->dpc_flags))
1841                         qla4xxx_recover_adapter(ha);
1842
1843                 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1844                         uint8_t wait_time = RESET_INTR_TOV;
1845
1846                         while ((readw(&ha->reg->ctrl_status) &
1847                                 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
1848                                 if (--wait_time == 0)
1849                                         break;
1850                                 msleep(1000);
1851                         }
1852                         if (wait_time == 0)
1853                                 DEBUG2(printk("scsi%ld: %s: SR|FSR "
1854                                               "bit not cleared-- resetting\n",
1855                                               ha->host_no, __func__));
1856                         qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1857                         if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
1858                                 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1859                                 status = qla4xxx_recover_adapter(ha);
1860                         }
1861                         clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1862                         if (status == QLA_SUCCESS)
1863                                 ha->isp_ops->enable_intrs(ha);
1864                 }
1865         }
1866
1867 dpc_post_reset_ha:
1868         /* ---- process AEN? --- */
1869         if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
1870                 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
1871
1872         /* ---- Get DHCP IP Address? --- */
1873         if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
1874                 qla4xxx_get_dhcp_ip_address(ha);
1875
1876         /* ---- link change? --- */
1877         if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
1878                 if (!test_bit(AF_LINK_UP, &ha->flags)) {
1879                         /* ---- link down? --- */
1880                         qla4xxx_mark_all_devices_missing(ha);
1881                 } else {
1882                         /* ---- link up? --- *
1883                          * F/W will auto login to all devices ONLY ONCE after
1884                          * link up during driver initialization and runtime
1885                          * fatal error recovery.  Therefore, the driver must
1886                          * manually relogin to devices when recovering from
1887                          * connection failures, logouts, expired KATO, etc. */
1888
1889                         qla4xxx_relogin_all_devices(ha);
1890                 }
1891         }
1892
1893         /* ---- relogin device? --- */
1894         if (adapter_up(ha) &&
1895             test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
1896                 list_for_each_entry_safe(ddb_entry, dtemp,
1897                                          &ha->ddb_list, list) {
1898                         if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
1899                             atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
1900                                 qla4xxx_relogin_device(ha, ddb_entry);
1901
1902                         /*
1903                          * If mbx cmd times out there is no point
1904                          * in continuing further.
1905                          * With large no of targets this can hang
1906                          * the system.
1907                          */
1908                         if (test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
1909                                 printk(KERN_WARNING "scsi%ld: %s: "
1910                                        "need to reset hba\n",
1911                                        ha->host_no, __func__);
1912                                 break;
1913                         }
1914                 }
1915         }
1916
1917 }
1918
1919 /**
1920  * qla4xxx_free_adapter - release the adapter
1921  * @ha: pointer to adapter structure
1922  **/
1923 static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
1924 {
1925
1926         if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
1927                 /* Turn-off interrupts on the card. */
1928                 ha->isp_ops->disable_intrs(ha);
1929         }
1930
1931         /* Remove timer thread, if present */
1932         if (ha->timer_active)
1933                 qla4xxx_stop_timer(ha);
1934
1935         /* Kill the kernel thread for this host */
1936         if (ha->dpc_thread)
1937                 destroy_workqueue(ha->dpc_thread);
1938
1939         /* Put firmware in known state */
1940         ha->isp_ops->reset_firmware(ha);
1941
1942         if (is_qla8022(ha)) {
1943                 qla4_8xxx_idc_lock(ha);
1944                 qla4_8xxx_clear_drv_active(ha);
1945                 qla4_8xxx_idc_unlock(ha);
1946         }
1947
1948         /* Detach interrupts */
1949         if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
1950                 qla4xxx_free_irqs(ha);
1951
1952         /* free extra memory */
1953         qla4xxx_mem_free(ha);
1954 }
1955
1956 int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
1957 {
1958         int status = 0;
1959         uint8_t revision_id;
1960         unsigned long mem_base, mem_len, db_base, db_len;
1961         struct pci_dev *pdev = ha->pdev;
1962
1963         status = pci_request_regions(pdev, DRIVER_NAME);
1964         if (status) {
1965                 printk(KERN_WARNING
1966                     "scsi(%ld) Failed to reserve PIO regions (%s) "
1967                     "status=%d\n", ha->host_no, pci_name(pdev), status);
1968                 goto iospace_error_exit;
1969         }
1970
1971         pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
1972         DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
1973             __func__, revision_id));
1974         ha->revision_id = revision_id;
1975
1976         /* remap phys address */
1977         mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
1978         mem_len = pci_resource_len(pdev, 0);
1979         DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
1980             __func__, mem_base, mem_len));
1981
1982         /* mapping of pcibase pointer */
1983         ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
1984         if (!ha->nx_pcibase) {
1985                 printk(KERN_ERR
1986                     "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
1987                 pci_release_regions(ha->pdev);
1988                 goto iospace_error_exit;
1989         }
1990
1991         /* Mapping of IO base pointer, door bell read and write pointer */
1992
1993         /* mapping of IO base pointer */
1994         ha->qla4_8xxx_reg =
1995             (struct device_reg_82xx  __iomem *)((uint8_t *)ha->nx_pcibase +
1996             0xbc000 + (ha->pdev->devfn << 11));
1997
1998         db_base = pci_resource_start(pdev, 4);  /* doorbell is on bar 4 */
1999         db_len = pci_resource_len(pdev, 4);
2000
2001         ha->nx_db_wr_ptr = (ha->pdev->devfn == 4 ? QLA82XX_CAM_RAM_DB1 :
2002             QLA82XX_CAM_RAM_DB2);
2003
2004         return 0;
2005 iospace_error_exit:
2006         return -ENOMEM;
2007 }
2008
2009 /***
2010  * qla4xxx_iospace_config - maps registers
2011  * @ha: pointer to adapter structure
2012  *
2013  * This routines maps HBA's registers from the pci address space
2014  * into the kernel virtual address space for memory mapped i/o.
2015  **/
2016 int qla4xxx_iospace_config(struct scsi_qla_host *ha)
2017 {
2018         unsigned long pio, pio_len, pio_flags;
2019         unsigned long mmio, mmio_len, mmio_flags;
2020
2021         pio = pci_resource_start(ha->pdev, 0);
2022         pio_len = pci_resource_len(ha->pdev, 0);
2023         pio_flags = pci_resource_flags(ha->pdev, 0);
2024         if (pio_flags & IORESOURCE_IO) {
2025                 if (pio_len < MIN_IOBASE_LEN) {
2026                         ql4_printk(KERN_WARNING, ha,
2027                                 "Invalid PCI I/O region size\n");
2028                         pio = 0;
2029                 }
2030         } else {
2031                 ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
2032                 pio = 0;
2033         }
2034
2035         /* Use MMIO operations for all accesses. */
2036         mmio = pci_resource_start(ha->pdev, 1);
2037         mmio_len = pci_resource_len(ha->pdev, 1);
2038         mmio_flags = pci_resource_flags(ha->pdev, 1);
2039
2040         if (!(mmio_flags & IORESOURCE_MEM)) {
2041                 ql4_printk(KERN_ERR, ha,
2042                     "region #0 not an MMIO resource, aborting\n");
2043
2044                 goto iospace_error_exit;
2045         }
2046
2047         if (mmio_len < MIN_IOBASE_LEN) {
2048                 ql4_printk(KERN_ERR, ha,
2049                     "Invalid PCI mem region size, aborting\n");
2050                 goto iospace_error_exit;
2051         }
2052
2053         if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
2054                 ql4_printk(KERN_WARNING, ha,
2055                     "Failed to reserve PIO/MMIO regions\n");
2056
2057                 goto iospace_error_exit;
2058         }
2059
2060         ha->pio_address = pio;
2061         ha->pio_length = pio_len;
2062         ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
2063         if (!ha->reg) {
2064                 ql4_printk(KERN_ERR, ha,
2065                     "cannot remap MMIO, aborting\n");
2066
2067                 goto iospace_error_exit;
2068         }
2069
2070         return 0;
2071
2072 iospace_error_exit:
2073         return -ENOMEM;
2074 }
2075
2076 static struct isp_operations qla4xxx_isp_ops = {
2077         .iospace_config         = qla4xxx_iospace_config,
2078         .pci_config             = qla4xxx_pci_config,
2079         .disable_intrs          = qla4xxx_disable_intrs,
2080         .enable_intrs           = qla4xxx_enable_intrs,
2081         .start_firmware         = qla4xxx_start_firmware,
2082         .intr_handler           = qla4xxx_intr_handler,
2083         .interrupt_service_routine = qla4xxx_interrupt_service_routine,
2084         .reset_chip             = qla4xxx_soft_reset,
2085         .reset_firmware         = qla4xxx_hw_reset,
2086         .queue_iocb             = qla4xxx_queue_iocb,
2087         .complete_iocb          = qla4xxx_complete_iocb,
2088         .rd_shdw_req_q_out      = qla4xxx_rd_shdw_req_q_out,
2089         .rd_shdw_rsp_q_in       = qla4xxx_rd_shdw_rsp_q_in,
2090         .get_sys_info           = qla4xxx_get_sys_info,
2091 };
2092
2093 static struct isp_operations qla4_8xxx_isp_ops = {
2094         .iospace_config         = qla4_8xxx_iospace_config,
2095         .pci_config             = qla4_8xxx_pci_config,
2096         .disable_intrs          = qla4_8xxx_disable_intrs,
2097         .enable_intrs           = qla4_8xxx_enable_intrs,
2098         .start_firmware         = qla4_8xxx_load_risc,
2099         .intr_handler           = qla4_8xxx_intr_handler,
2100         .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
2101         .reset_chip             = qla4_8xxx_isp_reset,
2102         .reset_firmware         = qla4_8xxx_stop_firmware,
2103         .queue_iocb             = qla4_8xxx_queue_iocb,
2104         .complete_iocb          = qla4_8xxx_complete_iocb,
2105         .rd_shdw_req_q_out      = qla4_8xxx_rd_shdw_req_q_out,
2106         .rd_shdw_rsp_q_in       = qla4_8xxx_rd_shdw_rsp_q_in,
2107         .get_sys_info           = qla4_8xxx_get_sys_info,
2108 };
2109
2110 uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
2111 {
2112         return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
2113 }
2114
2115 uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
2116 {
2117         return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
2118 }
2119
2120 uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
2121 {
2122         return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
2123 }
2124
2125 uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
2126 {
2127         return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
2128 }
2129
2130 /**
2131  * qla4xxx_probe_adapter - callback function to probe HBA
2132  * @pdev: pointer to pci_dev structure
2133  * @pci_device_id: pointer to pci_device entry
2134  *
2135  * This routine will probe for Qlogic 4xxx iSCSI host adapters.
2136  * It returns zero if successful. It also initializes all data necessary for
2137  * the driver.
2138  **/
2139 static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
2140                                            const struct pci_device_id *ent)
2141 {
2142         int ret = -ENODEV, status;
2143         struct Scsi_Host *host;
2144         struct scsi_qla_host *ha;
2145         uint8_t init_retry_count = 0;
2146         char buf[34];
2147         struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
2148         uint32_t dev_state;
2149
2150         if (pci_enable_device(pdev))
2151                 return -1;
2152
2153         host = scsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha));
2154         if (host == NULL) {
2155                 printk(KERN_WARNING
2156                        "qla4xxx: Couldn't allocate host from scsi layer!\n");
2157                 goto probe_disable_device;
2158         }
2159
2160         /* Clear our data area */
2161         ha = (struct scsi_qla_host *) host->hostdata;
2162         memset(ha, 0, sizeof(*ha));
2163
2164         /* Save the information from PCI BIOS.  */
2165         ha->pdev = pdev;
2166         ha->host = host;
2167         ha->host_no = host->host_no;
2168
2169         pci_enable_pcie_error_reporting(pdev);
2170
2171         /* Setup Runtime configurable options */
2172         if (is_qla8022(ha)) {
2173                 ha->isp_ops = &qla4_8xxx_isp_ops;
2174                 rwlock_init(&ha->hw_lock);
2175                 ha->qdr_sn_window = -1;
2176                 ha->ddr_mn_window = -1;
2177                 ha->curr_window = 255;
2178                 ha->func_num = PCI_FUNC(ha->pdev->devfn);
2179                 nx_legacy_intr = &legacy_intr[ha->func_num];
2180                 ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
2181                 ha->nx_legacy_intr.tgt_status_reg =
2182                         nx_legacy_intr->tgt_status_reg;
2183                 ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
2184                 ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
2185         } else {
2186                 ha->isp_ops = &qla4xxx_isp_ops;
2187         }
2188
2189         /* Set EEH reset type to fundamental if required by hba */
2190         if (is_qla8022(ha))
2191                 pdev->needs_freset = 1;
2192
2193         /* Configure PCI I/O space. */
2194         ret = ha->isp_ops->iospace_config(ha);
2195         if (ret)
2196                 goto probe_failed_ioconfig;
2197
2198         ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
2199                    pdev->device, pdev->irq, ha->reg);
2200
2201         qla4xxx_config_dma_addressing(ha);
2202
2203         /* Initialize lists and spinlocks. */
2204         INIT_LIST_HEAD(&ha->ddb_list);
2205         INIT_LIST_HEAD(&ha->free_srb_q);
2206
2207         mutex_init(&ha->mbox_sem);
2208         init_completion(&ha->mbx_intr_comp);
2209
2210         spin_lock_init(&ha->hardware_lock);
2211
2212         /* Allocate dma buffers */
2213         if (qla4xxx_mem_alloc(ha)) {
2214                 ql4_printk(KERN_WARNING, ha,
2215                     "[ERROR] Failed to allocate memory for adapter\n");
2216
2217                 ret = -ENOMEM;
2218                 goto probe_failed;
2219         }
2220
2221         if (is_qla8022(ha))
2222                 (void) qla4_8xxx_get_flash_info(ha);
2223
2224         /*
2225          * Initialize the Host adapter request/response queues and
2226          * firmware
2227          * NOTE: interrupts enabled upon successful completion
2228          */
2229         status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
2230         while ((!test_bit(AF_ONLINE, &ha->flags)) &&
2231             init_retry_count++ < MAX_INIT_RETRIES) {
2232
2233                 if (is_qla8022(ha)) {
2234                         qla4_8xxx_idc_lock(ha);
2235                         dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
2236                         qla4_8xxx_idc_unlock(ha);
2237                         if (dev_state == QLA82XX_DEV_FAILED) {
2238                                 ql4_printk(KERN_WARNING, ha, "%s: don't retry "
2239                                     "initialize adapter. H/W is in failed state\n",
2240                                     __func__);
2241                                 break;
2242                         }
2243                 }
2244                 DEBUG2(printk("scsi: %s: retrying adapter initialization "
2245                               "(%d)\n", __func__, init_retry_count));
2246
2247                 if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
2248                         continue;
2249
2250                 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
2251         }
2252
2253         if (!test_bit(AF_ONLINE, &ha->flags)) {
2254                 ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
2255
2256                 if (is_qla8022(ha) && ql4xdontresethba) {
2257                         /* Put the device in failed state. */
2258                         DEBUG2(printk(KERN_ERR "HW STATE: FAILED\n"));
2259                         qla4_8xxx_idc_lock(ha);
2260                         qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2261                             QLA82XX_DEV_FAILED);
2262                         qla4_8xxx_idc_unlock(ha);
2263                 }
2264                 ret = -ENODEV;
2265                 goto probe_failed;
2266         }
2267
2268         host->cmd_per_lun = 3;
2269         host->max_channel = 0;
2270         host->max_lun = MAX_LUNS - 1;
2271         host->max_id = MAX_TARGETS;
2272         host->max_cmd_len = IOCB_MAX_CDB_LEN;
2273         host->can_queue = MAX_SRBS ;
2274         host->transportt = qla4xxx_scsi_transport;
2275
2276         ret = scsi_init_shared_tag_map(host, MAX_SRBS);
2277         if (ret) {
2278                 ql4_printk(KERN_WARNING, ha,
2279                     "scsi_init_shared_tag_map failed\n");
2280                 goto probe_failed;
2281         }
2282
2283         /* Startup the kernel thread for this host adapter. */
2284         DEBUG2(printk("scsi: %s: Starting kernel thread for "
2285                       "qla4xxx_dpc\n", __func__));
2286         sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
2287         ha->dpc_thread = create_singlethread_workqueue(buf);
2288         if (!ha->dpc_thread) {
2289                 ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
2290                 ret = -ENODEV;
2291                 goto probe_failed;
2292         }
2293         INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
2294
2295         /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
2296          * (which is called indirectly by qla4xxx_initialize_adapter),
2297          * so that irqs will be registered after crbinit but before
2298          * mbx_intr_enable.
2299          */
2300         if (!is_qla8022(ha)) {
2301                 ret = qla4xxx_request_irqs(ha);
2302                 if (ret) {
2303                         ql4_printk(KERN_WARNING, ha, "Failed to reserve "
2304                             "interrupt %d already in use.\n", pdev->irq);
2305                         goto probe_failed;
2306                 }
2307         }
2308
2309         pci_save_state(ha->pdev);
2310         ha->isp_ops->enable_intrs(ha);
2311
2312         /* Start timer thread. */
2313         qla4xxx_start_timer(ha, qla4xxx_timer, 1);
2314
2315         set_bit(AF_INIT_DONE, &ha->flags);
2316
2317         pci_set_drvdata(pdev, ha);
2318
2319         ret = scsi_add_host(host, &pdev->dev);
2320         if (ret)
2321                 goto probe_failed;
2322
2323         printk(KERN_INFO
2324                " QLogic iSCSI HBA Driver version: %s\n"
2325                "  QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
2326                qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
2327                ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
2328                ha->patch_number, ha->build_number);
2329         scsi_scan_host(host);
2330
2331         qla4xxx_create_ifaces(ha);
2332         return 0;
2333
2334 probe_failed:
2335         qla4xxx_free_adapter(ha);
2336
2337 probe_failed_ioconfig:
2338         pci_disable_pcie_error_reporting(pdev);
2339         scsi_host_put(ha->host);
2340
2341 probe_disable_device:
2342         pci_disable_device(pdev);
2343
2344         return ret;
2345 }
2346
2347 /**
2348  * qla4xxx_prevent_other_port_reinit - prevent other port from re-initialize
2349  * @ha: pointer to adapter structure
2350  *
2351  * Mark the other ISP-4xxx port to indicate that the driver is being removed,
2352  * so that the other port will not re-initialize while in the process of
2353  * removing the ha due to driver unload or hba hotplug.
2354  **/
2355 static void qla4xxx_prevent_other_port_reinit(struct scsi_qla_host *ha)
2356 {
2357         struct scsi_qla_host *other_ha = NULL;
2358         struct pci_dev *other_pdev = NULL;
2359         int fn = ISP4XXX_PCI_FN_2;
2360
2361         /*iscsi function numbers for ISP4xxx is 1 and 3*/
2362         if (PCI_FUNC(ha->pdev->devfn) & BIT_1)
2363                 fn = ISP4XXX_PCI_FN_1;
2364
2365         other_pdev =
2366                 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
2367                 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
2368                 fn));
2369
2370         /* Get other_ha if other_pdev is valid and state is enable*/
2371         if (other_pdev) {
2372                 if (atomic_read(&other_pdev->enable_cnt)) {
2373                         other_ha = pci_get_drvdata(other_pdev);
2374                         if (other_ha) {
2375                                 set_bit(AF_HA_REMOVAL, &other_ha->flags);
2376                                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: "
2377                                     "Prevent %s reinit\n", __func__,
2378                                     dev_name(&other_ha->pdev->dev)));
2379                         }
2380                 }
2381                 pci_dev_put(other_pdev);
2382         }
2383 }
2384
2385 /**
2386  * qla4xxx_remove_adapter - calback function to remove adapter.
2387  * @pci_dev: PCI device pointer
2388  **/
2389 static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
2390 {
2391         struct scsi_qla_host *ha;
2392
2393         ha = pci_get_drvdata(pdev);
2394
2395         if (!is_qla8022(ha))
2396                 qla4xxx_prevent_other_port_reinit(ha);
2397
2398         /* remove devs from iscsi_sessions to scsi_devices */
2399         qla4xxx_free_ddb_list(ha);
2400
2401         /* destroy iface from sysfs */
2402         qla4xxx_destroy_ifaces(ha);
2403
2404         scsi_remove_host(ha->host);
2405
2406         qla4xxx_free_adapter(ha);
2407
2408         scsi_host_put(ha->host);
2409
2410         pci_disable_pcie_error_reporting(pdev);
2411         pci_disable_device(pdev);
2412         pci_set_drvdata(pdev, NULL);
2413 }
2414
2415 /**
2416  * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
2417  * @ha: HA context
2418  *
2419  * At exit, the @ha's flags.enable_64bit_addressing set to indicated
2420  * supported addressing method.
2421  */
2422 static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
2423 {
2424         int retval;
2425
2426         /* Update our PCI device dma_mask for full 64 bit mask */
2427         if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
2428                 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
2429                         dev_dbg(&ha->pdev->dev,
2430                                   "Failed to set 64 bit PCI consistent mask; "
2431                                    "using 32 bit.\n");
2432                         retval = pci_set_consistent_dma_mask(ha->pdev,
2433                                                              DMA_BIT_MASK(32));
2434                 }
2435         } else
2436                 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
2437 }
2438
2439 static int qla4xxx_slave_alloc(struct scsi_device *sdev)
2440 {
2441         struct iscsi_cls_session *sess = starget_to_session(sdev->sdev_target);
2442         struct ddb_entry *ddb = sess->dd_data;
2443         int queue_depth = QL4_DEF_QDEPTH;
2444
2445         sdev->hostdata = ddb;
2446         sdev->tagged_supported = 1;
2447
2448         if (ql4xmaxqdepth != 0 && ql4xmaxqdepth <= 0xffffU)
2449                 queue_depth = ql4xmaxqdepth;
2450
2451         scsi_activate_tcq(sdev, queue_depth);
2452         return 0;
2453 }
2454
2455 static int qla4xxx_slave_configure(struct scsi_device *sdev)
2456 {
2457         sdev->tagged_supported = 1;
2458         return 0;
2459 }
2460
2461 static void qla4xxx_slave_destroy(struct scsi_device *sdev)
2462 {
2463         scsi_deactivate_tcq(sdev, 1);
2464 }
2465
2466 /**
2467  * qla4xxx_del_from_active_array - returns an active srb
2468  * @ha: Pointer to host adapter structure.
2469  * @index: index into the active_array
2470  *
2471  * This routine removes and returns the srb at the specified index
2472  **/
2473 struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
2474     uint32_t index)
2475 {
2476         struct srb *srb = NULL;
2477         struct scsi_cmnd *cmd = NULL;
2478
2479         cmd = scsi_host_find_tag(ha->host, index);
2480         if (!cmd)
2481                 return srb;
2482
2483         srb = (struct srb *)CMD_SP(cmd);
2484         if (!srb)
2485                 return srb;
2486
2487         /* update counters */
2488         if (srb->flags & SRB_DMA_VALID) {
2489                 ha->req_q_count += srb->iocb_cnt;
2490                 ha->iocb_cnt -= srb->iocb_cnt;
2491                 if (srb->cmd)
2492                         srb->cmd->host_scribble =
2493                                 (unsigned char *)(unsigned long) MAX_SRBS;
2494         }
2495         return srb;
2496 }
2497
2498 /**
2499  * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
2500  * @ha: Pointer to host adapter structure.
2501  * @cmd: Scsi Command to wait on.
2502  *
2503  * This routine waits for the command to be returned by the Firmware
2504  * for some max time.
2505  **/
2506 static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
2507                                       struct scsi_cmnd *cmd)
2508 {
2509         int done = 0;
2510         struct srb *rp;
2511         uint32_t max_wait_time = EH_WAIT_CMD_TOV;
2512         int ret = SUCCESS;
2513
2514         /* Dont wait on command if PCI error is being handled
2515          * by PCI AER driver
2516          */
2517         if (unlikely(pci_channel_offline(ha->pdev)) ||
2518             (test_bit(AF_EEH_BUSY, &ha->flags))) {
2519                 ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
2520                     ha->host_no, __func__);
2521                 return ret;
2522         }
2523
2524         do {
2525                 /* Checking to see if its returned to OS */
2526                 rp = (struct srb *) CMD_SP(cmd);
2527                 if (rp == NULL) {
2528                         done++;
2529                         break;
2530                 }
2531
2532                 msleep(2000);
2533         } while (max_wait_time--);
2534
2535         return done;
2536 }
2537
2538 /**
2539  * qla4xxx_wait_for_hba_online - waits for HBA to come online
2540  * @ha: Pointer to host adapter structure
2541  **/
2542 static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
2543 {
2544         unsigned long wait_online;
2545
2546         wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
2547         while (time_before(jiffies, wait_online)) {
2548
2549                 if (adapter_up(ha))
2550                         return QLA_SUCCESS;
2551
2552                 msleep(2000);
2553         }
2554
2555         return QLA_ERROR;
2556 }
2557
2558 /**
2559  * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
2560  * @ha: pointer to HBA
2561  * @t: target id
2562  * @l: lun id
2563  *
2564  * This function waits for all outstanding commands to a lun to complete. It
2565  * returns 0 if all pending commands are returned and 1 otherwise.
2566  **/
2567 static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
2568                                         struct scsi_target *stgt,
2569                                         struct scsi_device *sdev)
2570 {
2571         int cnt;
2572         int status = 0;
2573         struct scsi_cmnd *cmd;
2574
2575         /*
2576          * Waiting for all commands for the designated target or dev
2577          * in the active array
2578          */
2579         for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
2580                 cmd = scsi_host_find_tag(ha->host, cnt);
2581                 if (cmd && stgt == scsi_target(cmd->device) &&
2582                     (!sdev || sdev == cmd->device)) {
2583                         if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
2584                                 status++;
2585                                 break;
2586                         }
2587                 }
2588         }
2589         return status;
2590 }
2591
2592 /**
2593  * qla4xxx_eh_abort - callback for abort task.
2594  * @cmd: Pointer to Linux's SCSI command structure
2595  *
2596  * This routine is called by the Linux OS to abort the specified
2597  * command.
2598  **/
2599 static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
2600 {
2601         struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2602         unsigned int id = cmd->device->id;
2603         unsigned int lun = cmd->device->lun;
2604         unsigned long flags;
2605         struct srb *srb = NULL;
2606         int ret = SUCCESS;
2607         int wait = 0;
2608
2609         ql4_printk(KERN_INFO, ha,
2610             "scsi%ld:%d:%d: Abort command issued cmd=%p\n",
2611             ha->host_no, id, lun, cmd);
2612
2613         spin_lock_irqsave(&ha->hardware_lock, flags);
2614         srb = (struct srb *) CMD_SP(cmd);
2615         if (!srb) {
2616                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2617                 return SUCCESS;
2618         }
2619         kref_get(&srb->srb_ref);
2620         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2621
2622         if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
2623                 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
2624                     ha->host_no, id, lun));
2625                 ret = FAILED;
2626         } else {
2627                 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
2628                     ha->host_no, id, lun));
2629                 wait = 1;
2630         }
2631
2632         kref_put(&srb->srb_ref, qla4xxx_srb_compl);
2633
2634         /* Wait for command to complete */
2635         if (wait) {
2636                 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
2637                         DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
2638                             ha->host_no, id, lun));
2639                         ret = FAILED;
2640                 }
2641         }
2642
2643         ql4_printk(KERN_INFO, ha,
2644             "scsi%ld:%d:%d: Abort command - %s\n",
2645             ha->host_no, id, lun, (ret == SUCCESS) ? "succeeded" : "failed");
2646
2647         return ret;
2648 }
2649
2650 /**
2651  * qla4xxx_eh_device_reset - callback for target reset.
2652  * @cmd: Pointer to Linux's SCSI command structure
2653  *
2654  * This routine is called by the Linux OS to reset all luns on the
2655  * specified target.
2656  **/
2657 static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
2658 {
2659         struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2660         struct ddb_entry *ddb_entry = cmd->device->hostdata;
2661         int ret = FAILED, stat;
2662
2663         if (!ddb_entry)
2664                 return ret;
2665
2666         ret = iscsi_block_scsi_eh(cmd);
2667         if (ret)
2668                 return ret;
2669         ret = FAILED;
2670
2671         ql4_printk(KERN_INFO, ha,
2672                    "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
2673                    cmd->device->channel, cmd->device->id, cmd->device->lun);
2674
2675         DEBUG2(printk(KERN_INFO
2676                       "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
2677                       "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
2678                       cmd, jiffies, cmd->request->timeout / HZ,
2679                       ha->dpc_flags, cmd->result, cmd->allowed));
2680
2681         /* FIXME: wait for hba to go online */
2682         stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
2683         if (stat != QLA_SUCCESS) {
2684                 ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
2685                 goto eh_dev_reset_done;
2686         }
2687
2688         if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2689                                          cmd->device)) {
2690                 ql4_printk(KERN_INFO, ha,
2691                            "DEVICE RESET FAILED - waiting for "
2692                            "commands.\n");
2693                 goto eh_dev_reset_done;
2694         }
2695
2696         /* Send marker. */
2697         if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2698                 MM_LUN_RESET) != QLA_SUCCESS)
2699                 goto eh_dev_reset_done;
2700
2701         ql4_printk(KERN_INFO, ha,
2702                    "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
2703                    ha->host_no, cmd->device->channel, cmd->device->id,
2704                    cmd->device->lun);
2705
2706         ret = SUCCESS;
2707
2708 eh_dev_reset_done:
2709
2710         return ret;
2711 }
2712
2713 /**
2714  * qla4xxx_eh_target_reset - callback for target reset.
2715  * @cmd: Pointer to Linux's SCSI command structure
2716  *
2717  * This routine is called by the Linux OS to reset the target.
2718  **/
2719 static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
2720 {
2721         struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2722         struct ddb_entry *ddb_entry = cmd->device->hostdata;
2723         int stat, ret;
2724
2725         if (!ddb_entry)
2726                 return FAILED;
2727
2728         ret = iscsi_block_scsi_eh(cmd);
2729         if (ret)
2730                 return ret;
2731
2732         starget_printk(KERN_INFO, scsi_target(cmd->device),
2733                        "WARM TARGET RESET ISSUED.\n");
2734
2735         DEBUG2(printk(KERN_INFO
2736                       "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
2737                       "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
2738                       ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
2739                       ha->dpc_flags, cmd->result, cmd->allowed));
2740
2741         stat = qla4xxx_reset_target(ha, ddb_entry);
2742         if (stat != QLA_SUCCESS) {
2743                 starget_printk(KERN_INFO, scsi_target(cmd->device),
2744                                "WARM TARGET RESET FAILED.\n");
2745                 return FAILED;
2746         }
2747
2748         if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2749                                          NULL)) {
2750                 starget_printk(KERN_INFO, scsi_target(cmd->device),
2751                                "WARM TARGET DEVICE RESET FAILED - "
2752                                "waiting for commands.\n");
2753                 return FAILED;
2754         }
2755
2756         /* Send marker. */
2757         if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2758                 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
2759                 starget_printk(KERN_INFO, scsi_target(cmd->device),
2760                                "WARM TARGET DEVICE RESET FAILED - "
2761                                "marker iocb failed.\n");
2762                 return FAILED;
2763         }
2764
2765         starget_printk(KERN_INFO, scsi_target(cmd->device),
2766                        "WARM TARGET RESET SUCCEEDED.\n");
2767         return SUCCESS;
2768 }
2769
2770 /**
2771  * qla4xxx_eh_host_reset - kernel callback
2772  * @cmd: Pointer to Linux's SCSI command structure
2773  *
2774  * This routine is invoked by the Linux kernel to perform fatal error
2775  * recovery on the specified adapter.
2776  **/
2777 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
2778 {
2779         int return_status = FAILED;
2780         struct scsi_qla_host *ha;
2781
2782         ha = (struct scsi_qla_host *) cmd->device->host->hostdata;
2783
2784         if (ql4xdontresethba) {
2785                 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
2786                      ha->host_no, __func__));
2787                 return FAILED;
2788         }
2789
2790         ql4_printk(KERN_INFO, ha,
2791                    "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
2792                    cmd->device->channel, cmd->device->id, cmd->device->lun);
2793
2794         if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
2795                 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host.  Adapter "
2796                               "DEAD.\n", ha->host_no, cmd->device->channel,
2797                               __func__));
2798
2799                 return FAILED;
2800         }
2801
2802         if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
2803                 if (is_qla8022(ha))
2804                         set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2805                 else
2806                         set_bit(DPC_RESET_HA, &ha->dpc_flags);
2807         }
2808
2809         if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
2810                 return_status = SUCCESS;
2811
2812         ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
2813                    return_status == FAILED ? "FAILED" : "SUCCEEDED");
2814
2815         return return_status;
2816 }
2817
2818 /* PCI AER driver recovers from all correctable errors w/o
2819  * driver intervention. For uncorrectable errors PCI AER
2820  * driver calls the following device driver's callbacks
2821  *
2822  * - Fatal Errors - link_reset
2823  * - Non-Fatal Errors - driver's pci_error_detected() which
2824  * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
2825  *
2826  * PCI AER driver calls
2827  * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
2828  *               returns RECOVERED or NEED_RESET if fw_hung
2829  * NEED_RESET - driver's slot_reset()
2830  * DISCONNECT - device is dead & cannot recover
2831  * RECOVERED - driver's pci_resume()
2832  */
2833 static pci_ers_result_t
2834 qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
2835 {
2836         struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2837
2838         ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
2839             ha->host_no, __func__, state);
2840
2841         if (!is_aer_supported(ha))
2842                 return PCI_ERS_RESULT_NONE;
2843
2844         switch (state) {
2845         case pci_channel_io_normal:
2846                 clear_bit(AF_EEH_BUSY, &ha->flags);
2847                 return PCI_ERS_RESULT_CAN_RECOVER;
2848         case pci_channel_io_frozen:
2849                 set_bit(AF_EEH_BUSY, &ha->flags);
2850                 qla4xxx_mailbox_premature_completion(ha);
2851                 qla4xxx_free_irqs(ha);
2852                 pci_disable_device(pdev);
2853                 /* Return back all IOs */
2854                 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
2855                 return PCI_ERS_RESULT_NEED_RESET;
2856         case pci_channel_io_perm_failure:
2857                 set_bit(AF_EEH_BUSY, &ha->flags);
2858                 set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
2859                 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
2860                 return PCI_ERS_RESULT_DISCONNECT;
2861         }
2862         return PCI_ERS_RESULT_NEED_RESET;
2863 }
2864
2865 /**
2866  * qla4xxx_pci_mmio_enabled() gets called if
2867  * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
2868  * and read/write to the device still works.
2869  **/
2870 static pci_ers_result_t
2871 qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
2872 {
2873         struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2874
2875         if (!is_aer_supported(ha))
2876                 return PCI_ERS_RESULT_NONE;
2877
2878         return PCI_ERS_RESULT_RECOVERED;
2879 }
2880
2881 static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
2882 {
2883         uint32_t rval = QLA_ERROR;
2884         uint32_t ret = 0;
2885         int fn;
2886         struct pci_dev *other_pdev = NULL;
2887
2888         ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
2889
2890         set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2891
2892         if (test_bit(AF_ONLINE, &ha->flags)) {
2893                 clear_bit(AF_ONLINE, &ha->flags);
2894                 qla4xxx_mark_all_devices_missing(ha);
2895                 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2896         }
2897
2898         fn = PCI_FUNC(ha->pdev->devfn);
2899         while (fn > 0) {
2900                 fn--;
2901                 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
2902                     "func %x\n", ha->host_no, __func__, fn);
2903                 /* Get the pci device given the domain, bus,
2904                  * slot/function number */
2905                 other_pdev =
2906                     pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
2907                     ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
2908                     fn));
2909
2910                 if (!other_pdev)
2911                         continue;
2912
2913                 if (atomic_read(&other_pdev->enable_cnt)) {
2914                         ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
2915                             "func in enabled state%x\n", ha->host_no,
2916                             __func__, fn);
2917                         pci_dev_put(other_pdev);
2918                         break;
2919                 }
2920                 pci_dev_put(other_pdev);
2921         }
2922
2923         /* The first function on the card, the reset owner will
2924          * start & initialize the firmware. The other functions
2925          * on the card will reset the firmware context
2926          */
2927         if (!fn) {
2928                 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
2929                     "0x%x is the owner\n", ha->host_no, __func__,
2930                     ha->pdev->devfn);
2931
2932                 qla4_8xxx_idc_lock(ha);
2933                 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2934                     QLA82XX_DEV_COLD);
2935
2936                 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
2937                     QLA82XX_IDC_VERSION);
2938
2939                 qla4_8xxx_idc_unlock(ha);
2940                 clear_bit(AF_FW_RECOVERY, &ha->flags);
2941                 rval = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
2942                 qla4_8xxx_idc_lock(ha);
2943
2944                 if (rval != QLA_SUCCESS) {
2945                         ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
2946                             "FAILED\n", ha->host_no, __func__);
2947                         qla4_8xxx_clear_drv_active(ha);
2948                         qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2949                             QLA82XX_DEV_FAILED);
2950                 } else {
2951                         ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
2952                             "READY\n", ha->host_no, __func__);
2953                         qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2954                             QLA82XX_DEV_READY);
2955                         /* Clear driver state register */
2956                         qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
2957                         qla4_8xxx_set_drv_active(ha);
2958                         ret = qla4xxx_request_irqs(ha);
2959                         if (ret) {
2960                                 ql4_printk(KERN_WARNING, ha, "Failed to "
2961                                     "reserve interrupt %d already in use.\n",
2962                                     ha->pdev->irq);
2963                                 rval = QLA_ERROR;
2964                         } else {
2965                                 ha->isp_ops->enable_intrs(ha);
2966                                 rval = QLA_SUCCESS;
2967                         }
2968                 }
2969                 qla4_8xxx_idc_unlock(ha);
2970         } else {
2971                 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
2972                     "the reset owner\n", ha->host_no, __func__,
2973                     ha->pdev->devfn);
2974                 if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
2975                     QLA82XX_DEV_READY)) {
2976                         clear_bit(AF_FW_RECOVERY, &ha->flags);
2977                         rval = qla4xxx_initialize_adapter(ha,
2978                             PRESERVE_DDB_LIST);
2979                         if (rval == QLA_SUCCESS) {
2980                                 ret = qla4xxx_request_irqs(ha);
2981                                 if (ret) {
2982                                         ql4_printk(KERN_WARNING, ha, "Failed to"
2983                                             " reserve interrupt %d already in"
2984                                             " use.\n", ha->pdev->irq);
2985                                         rval = QLA_ERROR;
2986                                 } else {
2987                                         ha->isp_ops->enable_intrs(ha);
2988                                         rval = QLA_SUCCESS;
2989                                 }
2990                         }
2991                         qla4_8xxx_idc_lock(ha);
2992                         qla4_8xxx_set_drv_active(ha);
2993                         qla4_8xxx_idc_unlock(ha);
2994                 }
2995         }
2996         clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2997         return rval;
2998 }
2999
3000 static pci_ers_result_t
3001 qla4xxx_pci_slot_reset(struct pci_dev *pdev)
3002 {
3003         pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
3004         struct scsi_qla_host *ha = pci_get_drvdata(pdev);
3005         int rc;
3006
3007         ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
3008             ha->host_no, __func__);
3009
3010         if (!is_aer_supported(ha))
3011                 return PCI_ERS_RESULT_NONE;
3012
3013         /* Restore the saved state of PCIe device -
3014          * BAR registers, PCI Config space, PCIX, MSI,
3015          * IOV states
3016          */
3017         pci_restore_state(pdev);
3018
3019         /* pci_restore_state() clears the saved_state flag of the device
3020          * save restored state which resets saved_state flag
3021          */
3022         pci_save_state(pdev);
3023
3024         /* Initialize device or resume if in suspended state */
3025         rc = pci_enable_device(pdev);
3026         if (rc) {
3027                 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Can't re-enable "
3028                     "device after reset\n", ha->host_no, __func__);
3029                 goto exit_slot_reset;
3030         }
3031
3032         ha->isp_ops->disable_intrs(ha);
3033
3034         if (is_qla8022(ha)) {
3035                 if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
3036                         ret = PCI_ERS_RESULT_RECOVERED;
3037                         goto exit_slot_reset;
3038                 } else
3039                         goto exit_slot_reset;
3040         }
3041
3042 exit_slot_reset:
3043         ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
3044             "device after reset\n", ha->host_no, __func__, ret);
3045         return ret;
3046 }
3047
3048 static void
3049 qla4xxx_pci_resume(struct pci_dev *pdev)
3050 {
3051         struct scsi_qla_host *ha = pci_get_drvdata(pdev);
3052         int ret;
3053
3054         ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
3055             ha->host_no, __func__);
3056
3057         ret = qla4xxx_wait_for_hba_online(ha);
3058         if (ret != QLA_SUCCESS) {
3059                 ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
3060                     "resume I/O from slot/link_reset\n", ha->host_no,
3061                      __func__);
3062         }
3063
3064         pci_cleanup_aer_uncorrect_error_status(pdev);
3065         clear_bit(AF_EEH_BUSY, &ha->flags);
3066 }
3067
3068 static struct pci_error_handlers qla4xxx_err_handler = {
3069         .error_detected = qla4xxx_pci_error_detected,
3070         .mmio_enabled = qla4xxx_pci_mmio_enabled,
3071         .slot_reset = qla4xxx_pci_slot_reset,
3072         .resume = qla4xxx_pci_resume,
3073 };
3074
3075 static struct pci_device_id qla4xxx_pci_tbl[] = {
3076         {
3077                 .vendor         = PCI_VENDOR_ID_QLOGIC,
3078                 .device         = PCI_DEVICE_ID_QLOGIC_ISP4010,
3079                 .subvendor      = PCI_ANY_ID,
3080                 .subdevice      = PCI_ANY_ID,
3081         },
3082         {
3083                 .vendor         = PCI_VENDOR_ID_QLOGIC,
3084                 .device         = PCI_DEVICE_ID_QLOGIC_ISP4022,
3085                 .subvendor      = PCI_ANY_ID,
3086                 .subdevice      = PCI_ANY_ID,
3087         },
3088         {
3089                 .vendor         = PCI_VENDOR_ID_QLOGIC,
3090                 .device         = PCI_DEVICE_ID_QLOGIC_ISP4032,
3091                 .subvendor      = PCI_ANY_ID,
3092                 .subdevice      = PCI_ANY_ID,
3093         },
3094         {
3095                 .vendor         = PCI_VENDOR_ID_QLOGIC,
3096                 .device         = PCI_DEVICE_ID_QLOGIC_ISP8022,
3097                 .subvendor      = PCI_ANY_ID,
3098                 .subdevice      = PCI_ANY_ID,
3099         },
3100         {0, 0},
3101 };
3102 MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
3103
3104 static struct pci_driver qla4xxx_pci_driver = {
3105         .name           = DRIVER_NAME,
3106         .id_table       = qla4xxx_pci_tbl,
3107         .probe          = qla4xxx_probe_adapter,
3108         .remove         = qla4xxx_remove_adapter,
3109         .err_handler = &qla4xxx_err_handler,
3110 };
3111
3112 static int __init qla4xxx_module_init(void)
3113 {
3114         int ret;
3115
3116         /* Allocate cache for SRBs. */
3117         srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
3118                                        SLAB_HWCACHE_ALIGN, NULL);
3119         if (srb_cachep == NULL) {
3120                 printk(KERN_ERR
3121                        "%s: Unable to allocate SRB cache..."
3122                        "Failing load!\n", DRIVER_NAME);
3123                 ret = -ENOMEM;
3124                 goto no_srp_cache;
3125         }
3126
3127         /* Derive version string. */
3128         strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
3129         if (ql4xextended_error_logging)
3130                 strcat(qla4xxx_version_str, "-debug");
3131
3132         qla4xxx_scsi_transport =
3133                 iscsi_register_transport(&qla4xxx_iscsi_transport);
3134         if (!qla4xxx_scsi_transport){
3135                 ret = -ENODEV;
3136                 goto release_srb_cache;
3137         }
3138
3139         ret = pci_register_driver(&qla4xxx_pci_driver);
3140         if (ret)
3141                 goto unregister_transport;
3142
3143         printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
3144         return 0;
3145
3146 unregister_transport:
3147         iscsi_unregister_transport(&qla4xxx_iscsi_transport);
3148 release_srb_cache:
3149         kmem_cache_destroy(srb_cachep);
3150 no_srp_cache:
3151         return ret;
3152 }
3153
3154 static void __exit qla4xxx_module_exit(void)
3155 {
3156         pci_unregister_driver(&qla4xxx_pci_driver);
3157         iscsi_unregister_transport(&qla4xxx_iscsi_transport);
3158         kmem_cache_destroy(srb_cachep);
3159 }
3160
3161 module_init(qla4xxx_module_init);
3162 module_exit(qla4xxx_module_exit);
3163
3164 MODULE_AUTHOR("QLogic Corporation");
3165 MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
3166 MODULE_LICENSE("GPL");
3167 MODULE_VERSION(QLA4XXX_DRIVER_VERSION);