[SCSI] qla4xxx: Added new "struct ipaddress_config"
[linux-2.6.git] / drivers / scsi / qla4xxx / ql4_init.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
8 #include <scsi/iscsi_if.h>
9 #include "ql4_def.h"
10 #include "ql4_glbl.h"
11 #include "ql4_dbg.h"
12 #include "ql4_inline.h"
13
14 static struct ddb_entry *qla4xxx_alloc_ddb(struct scsi_qla_host *ha,
15                                            uint32_t fw_ddb_index);
16
17 static void ql4xxx_set_mac_number(struct scsi_qla_host *ha)
18 {
19         uint32_t value;
20         uint8_t func_number;
21         unsigned long flags;
22
23         /* Get the function number */
24         spin_lock_irqsave(&ha->hardware_lock, flags);
25         value = readw(&ha->reg->ctrl_status);
26         spin_unlock_irqrestore(&ha->hardware_lock, flags);
27
28         func_number = (uint8_t) ((value >> 4) & 0x30);
29         switch (value & ISP_CONTROL_FN_MASK) {
30         case ISP_CONTROL_FN0_SCSI:
31                 ha->mac_index = 1;
32                 break;
33         case ISP_CONTROL_FN1_SCSI:
34                 ha->mac_index = 3;
35                 break;
36         default:
37                 DEBUG2(printk("scsi%ld: %s: Invalid function number, "
38                               "ispControlStatus = 0x%x\n", ha->host_no,
39                               __func__, value));
40                 break;
41         }
42         DEBUG2(printk("scsi%ld: %s: mac_index %d.\n", ha->host_no, __func__,
43                       ha->mac_index));
44 }
45
46 /**
47  * qla4xxx_free_ddb - deallocate ddb
48  * @ha: pointer to host adapter structure.
49  * @ddb_entry: pointer to device database entry
50  *
51  * This routine deallocates and unlinks the specified ddb_entry from the
52  * adapter's
53  **/
54 void qla4xxx_free_ddb(struct scsi_qla_host *ha,
55     struct ddb_entry *ddb_entry)
56 {
57         /* Remove device entry from list */
58         list_del_init(&ddb_entry->list);
59
60         /* Remove device pointer from index mapping arrays */
61         ha->fw_ddb_index_map[ddb_entry->fw_ddb_index] =
62                 (struct ddb_entry *) INVALID_ENTRY;
63         ha->tot_ddbs--;
64
65         /* Free memory and scsi-ml struct for device entry */
66         qla4xxx_destroy_sess(ddb_entry);
67 }
68
69 /**
70  * qla4xxx_free_ddb_list - deallocate all ddbs
71  * @ha: pointer to host adapter structure.
72  *
73  * This routine deallocates and removes all devices on the sppecified adapter.
74  **/
75 void qla4xxx_free_ddb_list(struct scsi_qla_host *ha)
76 {
77         struct list_head *ptr;
78         struct ddb_entry *ddb_entry;
79
80         while (!list_empty(&ha->ddb_list)) {
81                 ptr = ha->ddb_list.next;
82                 /* Free memory for device entry and remove */
83                 ddb_entry = list_entry(ptr, struct ddb_entry, list);
84                 qla4xxx_free_ddb(ha, ddb_entry);
85         }
86 }
87
88 /**
89  * qla4xxx_init_response_q_entries() - Initializes response queue entries.
90  * @ha: HA context
91  *
92  * Beginning of request ring has initialization control block already built
93  * by nvram config routine.
94  **/
95 static void qla4xxx_init_response_q_entries(struct scsi_qla_host *ha)
96 {
97         uint16_t cnt;
98         struct response *pkt;
99
100         pkt = (struct response *)ha->response_ptr;
101         for (cnt = 0; cnt < RESPONSE_QUEUE_DEPTH; cnt++) {
102                 pkt->signature = RESPONSE_PROCESSED;
103                 pkt++;
104         }
105 }
106
107 /**
108  * qla4xxx_init_rings - initialize hw queues
109  * @ha: pointer to host adapter structure.
110  *
111  * This routine initializes the internal queues for the specified adapter.
112  * The QLA4010 requires us to restart the queues at index 0.
113  * The QLA4000 doesn't care, so just default to QLA4010's requirement.
114  **/
115 int qla4xxx_init_rings(struct scsi_qla_host *ha)
116 {
117         unsigned long flags = 0;
118
119         /* Initialize request queue. */
120         spin_lock_irqsave(&ha->hardware_lock, flags);
121         ha->request_out = 0;
122         ha->request_in = 0;
123         ha->request_ptr = &ha->request_ring[ha->request_in];
124         ha->req_q_count = REQUEST_QUEUE_DEPTH;
125
126         /* Initialize response queue. */
127         ha->response_in = 0;
128         ha->response_out = 0;
129         ha->response_ptr = &ha->response_ring[ha->response_out];
130
131         if (is_qla8022(ha)) {
132                 writel(0,
133                     (unsigned long  __iomem *)&ha->qla4_8xxx_reg->req_q_out);
134                 writel(0,
135                     (unsigned long  __iomem *)&ha->qla4_8xxx_reg->rsp_q_in);
136                 writel(0,
137                     (unsigned long  __iomem *)&ha->qla4_8xxx_reg->rsp_q_out);
138         } else {
139                 /*
140                  * Initialize DMA Shadow registers.  The firmware is really
141                  * supposed to take care of this, but on some uniprocessor
142                  * systems, the shadow registers aren't cleared-- causing
143                  * the interrupt_handler to think there are responses to be
144                  * processed when there aren't.
145                  */
146                 ha->shadow_regs->req_q_out = __constant_cpu_to_le32(0);
147                 ha->shadow_regs->rsp_q_in = __constant_cpu_to_le32(0);
148                 wmb();
149
150                 writel(0, &ha->reg->req_q_in);
151                 writel(0, &ha->reg->rsp_q_out);
152                 readl(&ha->reg->rsp_q_out);
153         }
154
155         qla4xxx_init_response_q_entries(ha);
156
157         spin_unlock_irqrestore(&ha->hardware_lock, flags);
158
159         return QLA_SUCCESS;
160 }
161
162 /**
163  * qla4xxx_get_sys_info - validate adapter MAC address(es)
164  * @ha: pointer to host adapter structure.
165  *
166  **/
167 int qla4xxx_get_sys_info(struct scsi_qla_host *ha)
168 {
169         struct flash_sys_info *sys_info;
170         dma_addr_t sys_info_dma;
171         int status = QLA_ERROR;
172
173         sys_info = dma_alloc_coherent(&ha->pdev->dev, sizeof(*sys_info),
174                                       &sys_info_dma, GFP_KERNEL);
175         if (sys_info == NULL) {
176                 DEBUG2(printk("scsi%ld: %s: Unable to allocate dma buffer.\n",
177                               ha->host_no, __func__));
178
179                 goto exit_get_sys_info_no_free;
180         }
181         memset(sys_info, 0, sizeof(*sys_info));
182
183         /* Get flash sys info */
184         if (qla4xxx_get_flash(ha, sys_info_dma, FLASH_OFFSET_SYS_INFO,
185                               sizeof(*sys_info)) != QLA_SUCCESS) {
186                 DEBUG2(printk("scsi%ld: %s: get_flash FLASH_OFFSET_SYS_INFO "
187                               "failed\n", ha->host_no, __func__));
188
189                 goto exit_get_sys_info;
190         }
191
192         /* Save M.A.C. address & serial_number */
193         memcpy(ha->my_mac, &sys_info->physAddr[0].address[0],
194                min(sizeof(ha->my_mac),
195                    sizeof(sys_info->physAddr[0].address)));
196         memcpy(ha->serial_number, &sys_info->acSerialNumber,
197                min(sizeof(ha->serial_number),
198                    sizeof(sys_info->acSerialNumber)));
199
200         status = QLA_SUCCESS;
201
202 exit_get_sys_info:
203         dma_free_coherent(&ha->pdev->dev, sizeof(*sys_info), sys_info,
204                           sys_info_dma);
205
206 exit_get_sys_info_no_free:
207         return status;
208 }
209
210 /**
211  * qla4xxx_init_local_data - initialize adapter specific local data
212  * @ha: pointer to host adapter structure.
213  *
214  **/
215 static int qla4xxx_init_local_data(struct scsi_qla_host *ha)
216 {
217         /* Initialize aen queue */
218         ha->aen_q_count = MAX_AEN_ENTRIES;
219
220         return qla4xxx_get_firmware_status(ha);
221 }
222
223 static uint8_t
224 qla4xxx_wait_for_ip_config(struct scsi_qla_host *ha)
225 {
226         uint8_t ipv4_wait = 0;
227         uint8_t ipv6_wait = 0;
228         int8_t ip_address[IPv6_ADDR_LEN] = {0} ;
229
230         /* If both IPv4 & IPv6 are enabled, possibly only one
231          * IP address may be acquired, so check to see if we
232          * need to wait for another */
233         if (is_ipv4_enabled(ha) && is_ipv6_enabled(ha)) {
234                 if (((ha->addl_fw_state & FW_ADDSTATE_DHCPv4_ENABLED) != 0) &&
235                     ((ha->addl_fw_state &
236                                     FW_ADDSTATE_DHCPv4_LEASE_ACQUIRED) == 0)) {
237                         ipv4_wait = 1;
238                 }
239                 if (((ha->ip_config.ipv6_addl_options &
240                       IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) != 0) &&
241                     ((ha->ip_config.ipv6_link_local_state ==
242                       IP_ADDRSTATE_ACQUIRING) ||
243                      (ha->ip_config.ipv6_addr0_state ==
244                       IP_ADDRSTATE_ACQUIRING) ||
245                      (ha->ip_config.ipv6_addr1_state ==
246                       IP_ADDRSTATE_ACQUIRING))) {
247
248                         ipv6_wait = 1;
249
250                         if ((ha->ip_config.ipv6_link_local_state ==
251                              IP_ADDRSTATE_PREFERRED) ||
252                             (ha->ip_config.ipv6_addr0_state ==
253                              IP_ADDRSTATE_PREFERRED) ||
254                             (ha->ip_config.ipv6_addr1_state ==
255                              IP_ADDRSTATE_PREFERRED)) {
256                                 DEBUG2(printk(KERN_INFO "scsi%ld: %s: "
257                                               "Preferred IP configured."
258                                               " Don't wait!\n", ha->host_no,
259                                               __func__));
260                                 ipv6_wait = 0;
261                         }
262                         if (memcmp(&ha->ip_config.ipv6_default_router_addr,
263                                    ip_address, IPv6_ADDR_LEN) == 0) {
264                                 DEBUG2(printk(KERN_INFO "scsi%ld: %s: "
265                                               "No Router configured. "
266                                               "Don't wait!\n", ha->host_no,
267                                               __func__));
268                                 ipv6_wait = 0;
269                         }
270                         if ((ha->ip_config.ipv6_default_router_state ==
271                              IPV6_RTRSTATE_MANUAL) &&
272                             (ha->ip_config.ipv6_link_local_state ==
273                              IP_ADDRSTATE_TENTATIVE) &&
274                             (memcmp(&ha->ip_config.ipv6_link_local_addr,
275                              &ha->ip_config.ipv6_default_router_addr, 4) ==
276                              0)) {
277                                 DEBUG2(printk("scsi%ld: %s: LinkLocal Router & "
278                                         "IP configured. Don't wait!\n",
279                                         ha->host_no, __func__));
280                                 ipv6_wait = 0;
281                         }
282                 }
283                 if (ipv4_wait || ipv6_wait) {
284                         DEBUG2(printk("scsi%ld: %s: Wait for additional "
285                                       "IP(s) \"", ha->host_no, __func__));
286                         if (ipv4_wait)
287                                 DEBUG2(printk("IPv4 "));
288                         if (ha->ip_config.ipv6_link_local_state ==
289                             IP_ADDRSTATE_ACQUIRING)
290                                 DEBUG2(printk("IPv6LinkLocal "));
291                         if (ha->ip_config.ipv6_addr0_state ==
292                             IP_ADDRSTATE_ACQUIRING)
293                                 DEBUG2(printk("IPv6Addr0 "));
294                         if (ha->ip_config.ipv6_addr1_state ==
295                             IP_ADDRSTATE_ACQUIRING)
296                                 DEBUG2(printk("IPv6Addr1 "));
297                         DEBUG2(printk("\"\n"));
298                 }
299         }
300
301         return ipv4_wait|ipv6_wait;
302 }
303
304 static int qla4xxx_fw_ready(struct scsi_qla_host *ha)
305 {
306         uint32_t timeout_count;
307         int ready = 0;
308
309         DEBUG2(ql4_printk(KERN_INFO, ha, "Waiting for Firmware Ready..\n"));
310         for (timeout_count = ADAPTER_INIT_TOV; timeout_count > 0;
311              timeout_count--) {
312                 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
313                         qla4xxx_get_dhcp_ip_address(ha);
314
315                 /* Get firmware state. */
316                 if (qla4xxx_get_firmware_state(ha) != QLA_SUCCESS) {
317                         DEBUG2(printk("scsi%ld: %s: unable to get firmware "
318                                       "state\n", ha->host_no, __func__));
319                         break;
320                 }
321
322                 if (ha->firmware_state & FW_STATE_ERROR) {
323                         DEBUG2(printk("scsi%ld: %s: an unrecoverable error has"
324                                       " occurred\n", ha->host_no, __func__));
325                         break;
326
327                 }
328                 if (ha->firmware_state & FW_STATE_CONFIG_WAIT) {
329                         /*
330                          * The firmware has not yet been issued an Initialize
331                          * Firmware command, so issue it now.
332                          */
333                         if (qla4xxx_initialize_fw_cb(ha) == QLA_ERROR)
334                                 break;
335
336                         /* Go back and test for ready state - no wait. */
337                         continue;
338                 }
339
340                 if (ha->firmware_state & FW_STATE_WAIT_AUTOCONNECT) {
341                         DEBUG2(printk(KERN_INFO "scsi%ld: %s: fwstate:"
342                                       "AUTOCONNECT in progress\n",
343                                       ha->host_no, __func__));
344                 }
345
346                 if (ha->firmware_state & FW_STATE_CONFIGURING_IP) {
347                         DEBUG2(printk(KERN_INFO "scsi%ld: %s: fwstate:"
348                                       " CONFIGURING IP\n",
349                                       ha->host_no, __func__));
350                         /*
351                          * Check for link state after 15 secs and if link is
352                          * still DOWN then, cable is unplugged. Ignore "DHCP
353                          * in Progress/CONFIGURING IP" bit to check if firmware
354                          * is in ready state or not after 15 secs.
355                          * This is applicable for both 2.x & 3.x firmware
356                          */
357                         if (timeout_count <= (ADAPTER_INIT_TOV - 15)) {
358                                 if (ha->addl_fw_state & FW_ADDSTATE_LINK_UP) {
359                                         DEBUG2(printk(KERN_INFO "scsi%ld: %s:"
360                                                   " LINK UP (Cable plugged)\n",
361                                                   ha->host_no, __func__));
362                                 } else if (ha->firmware_state &
363                                           (FW_STATE_CONFIGURING_IP |
364                                                              FW_STATE_READY)) {
365                                         DEBUG2(printk(KERN_INFO "scsi%ld: %s: "
366                                                 "LINK DOWN (Cable unplugged)\n",
367                                                 ha->host_no, __func__));
368                                         ha->firmware_state = FW_STATE_READY;
369                                 }
370                         }
371                 }
372
373                 if (ha->firmware_state == FW_STATE_READY) {
374                         /* If DHCP IP Addr is available, retrieve it now. */
375                         if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR,
376                                                                 &ha->dpc_flags))
377                                 qla4xxx_get_dhcp_ip_address(ha);
378
379                         if (!qla4xxx_wait_for_ip_config(ha) ||
380                                                         timeout_count == 1) {
381                                 DEBUG2(ql4_printk(KERN_INFO, ha,
382                                     "Firmware Ready..\n"));
383                                 /* The firmware is ready to process SCSI
384                                    commands. */
385                                 DEBUG2(ql4_printk(KERN_INFO, ha,
386                                         "scsi%ld: %s: MEDIA TYPE"
387                                         " - %s\n", ha->host_no,
388                                         __func__, (ha->addl_fw_state &
389                                         FW_ADDSTATE_OPTICAL_MEDIA)
390                                         != 0 ? "OPTICAL" : "COPPER"));
391                                 DEBUG2(ql4_printk(KERN_INFO, ha,
392                                         "scsi%ld: %s: DHCPv4 STATE"
393                                         " Enabled %s\n", ha->host_no,
394                                          __func__, (ha->addl_fw_state &
395                                          FW_ADDSTATE_DHCPv4_ENABLED) != 0 ?
396                                         "YES" : "NO"));
397                                 DEBUG2(ql4_printk(KERN_INFO, ha,
398                                         "scsi%ld: %s: LINK %s\n",
399                                         ha->host_no, __func__,
400                                         (ha->addl_fw_state &
401                                          FW_ADDSTATE_LINK_UP) != 0 ?
402                                         "UP" : "DOWN"));
403                                 DEBUG2(ql4_printk(KERN_INFO, ha,
404                                         "scsi%ld: %s: iSNS Service "
405                                         "Started %s\n",
406                                         ha->host_no, __func__,
407                                         (ha->addl_fw_state &
408                                          FW_ADDSTATE_ISNS_SVC_ENABLED) != 0 ?
409                                         "YES" : "NO"));
410
411                                 ready = 1;
412                                 break;
413                         }
414                 }
415                 DEBUG2(printk("scsi%ld: %s: waiting on fw, state=%x:%x - "
416                               "seconds expired= %d\n", ha->host_no, __func__,
417                               ha->firmware_state, ha->addl_fw_state,
418                               timeout_count));
419                 if (is_qla4032(ha) &&
420                         !(ha->addl_fw_state & FW_ADDSTATE_LINK_UP) &&
421                         (timeout_count < ADAPTER_INIT_TOV - 5)) {
422                         break;
423                 }
424
425                 msleep(1000);
426         }                       /* end of for */
427
428         if (timeout_count <= 0)
429                 DEBUG2(printk("scsi%ld: %s: FW Initialization timed out!\n",
430                               ha->host_no, __func__));
431
432         if (ha->firmware_state & FW_STATE_CONFIGURING_IP) {
433                 DEBUG2(printk("scsi%ld: %s: FW initialized, but is reporting "
434                               "it's waiting to configure an IP address\n",
435                                ha->host_no, __func__));
436                 ready = 1;
437         } else if (ha->firmware_state & FW_STATE_WAIT_AUTOCONNECT) {
438                 DEBUG2(printk("scsi%ld: %s: FW initialized, but "
439                               "auto-discovery still in process\n",
440                                ha->host_no, __func__));
441                 ready = 1;
442         }
443
444         return ready;
445 }
446
447 /**
448  * qla4xxx_init_firmware - initializes the firmware.
449  * @ha: pointer to host adapter structure.
450  *
451  **/
452 static int qla4xxx_init_firmware(struct scsi_qla_host *ha)
453 {
454         int status = QLA_ERROR;
455
456         if (is_aer_supported(ha) &&
457             test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
458                 return status;
459
460         /* For 82xx, stop firmware before initializing because if BIOS
461          * has previously initialized firmware, then driver's initialize
462          * firmware will fail. */
463         if (is_qla8022(ha))
464                 qla4_8xxx_stop_firmware(ha);
465
466         ql4_printk(KERN_INFO, ha, "Initializing firmware..\n");
467         if (qla4xxx_initialize_fw_cb(ha) == QLA_ERROR) {
468                 DEBUG2(printk("scsi%ld: %s: Failed to initialize firmware "
469                               "control block\n", ha->host_no, __func__));
470                 return status;
471         }
472         if (!qla4xxx_fw_ready(ha))
473                 return status;
474
475         return qla4xxx_get_firmware_status(ha);
476 }
477
478 static struct ddb_entry* qla4xxx_get_ddb_entry(struct scsi_qla_host *ha,
479                                                 uint32_t fw_ddb_index,
480                                                 uint32_t *new_tgt)
481 {
482         struct dev_db_entry *fw_ddb_entry = NULL;
483         dma_addr_t fw_ddb_entry_dma;
484         struct ddb_entry *ddb_entry = NULL;
485         int found = 0;
486         uint32_t device_state;
487
488         *new_tgt = 0;
489         /* Make sure the dma buffer is valid */
490         fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev,
491                                           sizeof(*fw_ddb_entry),
492                                           &fw_ddb_entry_dma, GFP_KERNEL);
493         if (fw_ddb_entry == NULL) {
494                 DEBUG2(printk("scsi%ld: %s: Unable to allocate dma buffer.\n",
495                               ha->host_no, __func__));
496                 goto exit_get_ddb_entry_no_free;
497         }
498
499         if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index, fw_ddb_entry,
500                                     fw_ddb_entry_dma, NULL, NULL,
501                                     &device_state, NULL, NULL, NULL) ==
502                                     QLA_ERROR) {
503                 DEBUG2(printk("scsi%ld: %s: failed get_ddb_entry for "
504                               "fw_ddb_index %d\n", ha->host_no, __func__,
505                               fw_ddb_index));
506                 goto exit_get_ddb_entry;
507         }
508
509         /* Allocate DDB if not already allocated. */
510         DEBUG2(printk("scsi%ld: %s: Looking for ddb[%d]\n", ha->host_no,
511                       __func__, fw_ddb_index));
512         list_for_each_entry(ddb_entry, &ha->ddb_list, list) {
513                 if ((memcmp(ddb_entry->iscsi_name, fw_ddb_entry->iscsi_name,
514                            ISCSI_NAME_SIZE) == 0) &&
515                         (ddb_entry->tpgt ==
516                                 le32_to_cpu(fw_ddb_entry->tgt_portal_grp)) &&
517                         (memcmp(ddb_entry->isid, fw_ddb_entry->isid,
518                                 sizeof(ddb_entry->isid)) == 0)) {
519                         found++;
520                         break;
521                 }
522         }
523
524         /* if not found allocate new ddb */
525         if (!found) {
526                 DEBUG2(printk("scsi%ld: %s: ddb[%d] not found - allocating "
527                               "new ddb\n", ha->host_no, __func__,
528                               fw_ddb_index));
529                 *new_tgt = 1;
530                 ddb_entry = qla4xxx_alloc_ddb(ha, fw_ddb_index);
531         }
532
533 exit_get_ddb_entry:
534         dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry), fw_ddb_entry,
535                           fw_ddb_entry_dma);
536
537 exit_get_ddb_entry_no_free:
538         return ddb_entry;
539 }
540
541 /**
542  * qla4xxx_update_ddb_entry - update driver's internal ddb
543  * @ha: pointer to host adapter structure.
544  * @ddb_entry: pointer to device database structure to be filled
545  * @fw_ddb_index: index of the ddb entry in fw ddb table
546  *
547  * This routine updates the driver's internal device database entry
548  * with information retrieved from the firmware's device database
549  * entry for the specified device. The ddb_entry->fw_ddb_index field
550  * must be initialized prior to calling this routine
551  *
552  **/
553 static int qla4xxx_update_ddb_entry(struct scsi_qla_host *ha,
554                                     struct ddb_entry *ddb_entry,
555                                     uint32_t fw_ddb_index)
556 {
557         struct dev_db_entry *fw_ddb_entry = NULL;
558         dma_addr_t fw_ddb_entry_dma;
559         int status = QLA_ERROR;
560         uint32_t conn_err;
561
562         if (ddb_entry == NULL) {
563                 DEBUG2(printk("scsi%ld: %s: ddb_entry is NULL\n", ha->host_no,
564                               __func__));
565
566                 goto exit_update_ddb_no_free;
567         }
568
569         /* Make sure the dma buffer is valid */
570         fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev,
571                                           sizeof(*fw_ddb_entry),
572                                           &fw_ddb_entry_dma, GFP_KERNEL);
573         if (fw_ddb_entry == NULL) {
574                 DEBUG2(printk("scsi%ld: %s: Unable to allocate dma buffer.\n",
575                               ha->host_no, __func__));
576
577                 goto exit_update_ddb_no_free;
578         }
579
580         if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index, fw_ddb_entry,
581                                     fw_ddb_entry_dma, NULL, NULL,
582                                     &ddb_entry->fw_ddb_device_state, &conn_err,
583                                     &ddb_entry->tcp_source_port_num,
584                                     &ddb_entry->connection_id) ==
585                                     QLA_ERROR) {
586                 DEBUG2(printk("scsi%ld: %s: failed get_ddb_entry for "
587                               "fw_ddb_index %d\n", ha->host_no, __func__,
588                               fw_ddb_index));
589
590                 goto exit_update_ddb;
591         }
592
593         status = QLA_SUCCESS;
594         ddb_entry->options = le16_to_cpu(fw_ddb_entry->options);
595         ddb_entry->target_session_id = le16_to_cpu(fw_ddb_entry->tsid);
596         ddb_entry->task_mgmt_timeout =
597                 le16_to_cpu(fw_ddb_entry->def_timeout);
598         ddb_entry->CmdSn = 0;
599         ddb_entry->exe_throttle = le16_to_cpu(fw_ddb_entry->exec_throttle);
600         ddb_entry->default_relogin_timeout =
601                 le16_to_cpu(fw_ddb_entry->def_timeout);
602         ddb_entry->default_time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
603
604         /* Update index in case it changed */
605         ddb_entry->fw_ddb_index = fw_ddb_index;
606         ha->fw_ddb_index_map[fw_ddb_index] = ddb_entry;
607
608         ddb_entry->port = le16_to_cpu(fw_ddb_entry->port);
609         ddb_entry->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
610         memcpy(ddb_entry->isid, fw_ddb_entry->isid, sizeof(ddb_entry->isid));
611
612         memcpy(&ddb_entry->iscsi_name[0], &fw_ddb_entry->iscsi_name[0],
613                min(sizeof(ddb_entry->iscsi_name),
614                    sizeof(fw_ddb_entry->iscsi_name)));
615         memcpy(&ddb_entry->iscsi_alias[0], &fw_ddb_entry->iscsi_alias[0],
616                min(sizeof(ddb_entry->iscsi_alias),
617                    sizeof(fw_ddb_entry->iscsi_alias)));
618         memcpy(&ddb_entry->ip_addr[0], &fw_ddb_entry->ip_addr[0],
619                min(sizeof(ddb_entry->ip_addr), sizeof(fw_ddb_entry->ip_addr)));
620
621         ddb_entry->iscsi_max_burst_len = fw_ddb_entry->iscsi_max_burst_len;
622         ddb_entry->iscsi_max_outsnd_r2t = fw_ddb_entry->iscsi_max_outsnd_r2t;
623         ddb_entry->iscsi_first_burst_len = fw_ddb_entry->iscsi_first_burst_len;
624         ddb_entry->iscsi_max_rcv_data_seg_len =
625                                 fw_ddb_entry->iscsi_max_rcv_data_seg_len;
626         ddb_entry->iscsi_max_snd_data_seg_len =
627                                 fw_ddb_entry->iscsi_max_snd_data_seg_len;
628
629         if (ddb_entry->options & DDB_OPT_IPV6_DEVICE) {
630                 memcpy(&ddb_entry->remote_ipv6_addr,
631                         fw_ddb_entry->ip_addr,
632                         min(sizeof(ddb_entry->remote_ipv6_addr),
633                         sizeof(fw_ddb_entry->ip_addr)));
634                 memcpy(&ddb_entry->link_local_ipv6_addr,
635                         fw_ddb_entry->link_local_ipv6_addr,
636                         min(sizeof(ddb_entry->link_local_ipv6_addr),
637                         sizeof(fw_ddb_entry->link_local_ipv6_addr)));
638
639                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: DDB[%d] State %04x"
640                                         " ConnErr %08x IP %pI6 "
641                                         ":%04d \"%s\"\n",
642                                         __func__, fw_ddb_index,
643                                         ddb_entry->fw_ddb_device_state,
644                                         conn_err, fw_ddb_entry->ip_addr,
645                                         le16_to_cpu(fw_ddb_entry->port),
646                                         fw_ddb_entry->iscsi_name));
647         } else
648                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: DDB[%d] State %04x"
649                                         " ConnErr %08x IP %pI4 "
650                                         ":%04d \"%s\"\n",
651                                         __func__, fw_ddb_index,
652                                         ddb_entry->fw_ddb_device_state,
653                                         conn_err, fw_ddb_entry->ip_addr,
654                                         le16_to_cpu(fw_ddb_entry->port),
655                                         fw_ddb_entry->iscsi_name));
656 exit_update_ddb:
657         if (fw_ddb_entry)
658                 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
659                                   fw_ddb_entry, fw_ddb_entry_dma);
660
661 exit_update_ddb_no_free:
662         return status;
663 }
664
665 /**
666  * qla4xxx_alloc_ddb - allocate device database entry
667  * @ha: Pointer to host adapter structure.
668  * @fw_ddb_index: Firmware's device database index
669  *
670  * This routine allocates a ddb_entry, ititializes some values, and
671  * inserts it into the ddb list.
672  **/
673 static struct ddb_entry * qla4xxx_alloc_ddb(struct scsi_qla_host *ha,
674                                             uint32_t fw_ddb_index)
675 {
676         struct ddb_entry *ddb_entry;
677
678         DEBUG2(printk("scsi%ld: %s: fw_ddb_index [%d]\n", ha->host_no,
679                       __func__, fw_ddb_index));
680
681         ddb_entry = qla4xxx_alloc_sess(ha);
682         if (ddb_entry == NULL) {
683                 DEBUG2(printk("scsi%ld: %s: Unable to allocate memory "
684                               "to add fw_ddb_index [%d]\n",
685                               ha->host_no, __func__, fw_ddb_index));
686                 return ddb_entry;
687         }
688
689         ddb_entry->fw_ddb_index = fw_ddb_index;
690         atomic_set(&ddb_entry->retry_relogin_timer, INVALID_ENTRY);
691         atomic_set(&ddb_entry->relogin_timer, 0);
692         atomic_set(&ddb_entry->relogin_retry_count, 0);
693         atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
694         list_add_tail(&ddb_entry->list, &ha->ddb_list);
695         ha->fw_ddb_index_map[fw_ddb_index] = ddb_entry;
696         ha->tot_ddbs++;
697
698         return ddb_entry;
699 }
700
701 /**
702  * qla4_is_relogin_allowed - Are we allowed to login?
703  * @ha: Pointer to host adapter structure.
704  * @conn_err: Last connection error associated with the ddb
705  *
706  * This routine tests the given connection error to determine if
707  * we are allowed to login.
708  **/
709 int qla4_is_relogin_allowed(struct scsi_qla_host *ha, uint32_t conn_err)
710 {
711         uint32_t err_code, login_rsp_sts_class;
712         int relogin = 1;
713
714         err_code = ((conn_err & 0x00ff0000) >> 16);
715         login_rsp_sts_class = ((conn_err & 0x0000ff00) >> 8);
716         if (err_code == 0x1c || err_code == 0x06) {
717                 DEBUG2(ql4_printk(KERN_INFO, ha,
718                     ": conn_err=0x%08x, send target completed"
719                     " or access denied failure\n", conn_err));
720                 relogin = 0;
721         }
722         if ((err_code == 0x08) && (login_rsp_sts_class == 0x02)) {
723                 /* Login Response PDU returned an error.
724                    Login Response Status in Error Code Detail
725                    indicates login should not be retried.*/
726                 DEBUG2(ql4_printk(KERN_INFO, ha,
727                     ": conn_err=0x%08x, do not retry relogin\n",
728                     conn_err));
729                 relogin = 0;
730         }
731
732         return relogin;
733 }
734
735 static void qla4xxx_flush_AENS(struct scsi_qla_host *ha)
736 {
737         unsigned long wtime;
738
739         /* Flush the 0x8014 AEN from the firmware as a result of
740          * Auto connect. We are basically doing get_firmware_ddb()
741          * to determine whether we need to log back in or not.
742          * Trying to do a set ddb before we have processed 0x8014
743          * will result in another set_ddb() for the same ddb. In other
744          * words there will be stale entries in the aen_q.
745          */
746         wtime = jiffies + (2 * HZ);
747         do {
748                 if (qla4xxx_get_firmware_state(ha) == QLA_SUCCESS)
749                         if (ha->firmware_state & (BIT_2 | BIT_0))
750                                 return;
751
752                 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
753                         qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
754
755                 msleep(1000);
756         } while (!time_after_eq(jiffies, wtime));
757 }
758
759 /**
760  * qla4xxx_build_ddb_list - builds driver ddb list
761  * @ha: Pointer to host adapter structure.
762  *
763  * This routine searches for all valid firmware ddb entries and builds
764  * an internal ddb list. Ddbs that are considered valid are those with
765  * a device state of SESSION_ACTIVE.
766  * A relogin (set_ddb) is issued for DDBs that are not online.
767  **/
768 static int qla4xxx_build_ddb_list(struct scsi_qla_host *ha)
769 {
770         int status = QLA_ERROR;
771         uint32_t fw_ddb_index = 0;
772         uint32_t next_fw_ddb_index = 0;
773         uint32_t ddb_state;
774         uint32_t conn_err;
775         struct ddb_entry *ddb_entry;
776         struct dev_db_entry *fw_ddb_entry = NULL;
777         dma_addr_t fw_ddb_entry_dma;
778         uint32_t ipv6_device;
779         uint32_t new_tgt;
780
781         qla4xxx_flush_AENS(ha);
782
783         fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
784                         &fw_ddb_entry_dma, GFP_KERNEL);
785         if (fw_ddb_entry == NULL) {
786                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: DMA alloc failed\n",
787                                 __func__));
788
789                 goto exit_build_ddb_list_no_free;
790         }
791
792         ql4_printk(KERN_INFO, ha, "Initializing DDBs ...\n");
793         for (fw_ddb_index = 0; fw_ddb_index < MAX_DDB_ENTRIES;
794              fw_ddb_index = next_fw_ddb_index) {
795                 /* First, let's see if a device exists here */
796                 if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index, fw_ddb_entry,
797                                             0, NULL, &next_fw_ddb_index,
798                                             &ddb_state, &conn_err,
799                                             NULL, NULL) ==
800                                             QLA_ERROR) {
801                         DEBUG2(printk("scsi%ld: %s: get_ddb_entry, "
802                                       "fw_ddb_index %d failed", ha->host_no,
803                                       __func__, fw_ddb_index));
804                         goto exit_build_ddb_list;
805                 }
806
807                 DEBUG2(printk("scsi%ld: %s: Getting DDB[%d] ddbstate=0x%x, "
808                               "next_fw_ddb_index=%d.\n", ha->host_no, __func__,
809                               fw_ddb_index, ddb_state, next_fw_ddb_index));
810
811                 /* Issue relogin, if necessary. */
812                 if (ddb_state == DDB_DS_SESSION_FAILED ||
813                     ddb_state == DDB_DS_NO_CONNECTION_ACTIVE) {
814                         /* Try and login to device */
815                         DEBUG2(printk("scsi%ld: %s: Login to DDB[%d]\n",
816                                       ha->host_no, __func__, fw_ddb_index));
817                         ipv6_device = le16_to_cpu(fw_ddb_entry->options) &
818                                         DDB_OPT_IPV6_DEVICE;
819                         if (qla4_is_relogin_allowed(ha, conn_err) &&
820                                         ((!ipv6_device &&
821                                           *((uint32_t *)fw_ddb_entry->ip_addr))
822                                          || ipv6_device)) {
823                                 qla4xxx_set_ddb_entry(ha, fw_ddb_index, 0);
824                                 if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index,
825                                                         NULL, 0, NULL,
826                                                         &next_fw_ddb_index,
827                                                         &ddb_state, &conn_err,
828                                                         NULL, NULL)
829                                                 == QLA_ERROR) {
830                                         DEBUG2(printk("scsi%ld: %s:"
831                                                 "get_ddb_entry %d failed\n",
832                                                 ha->host_no,
833                                                 __func__, fw_ddb_index));
834                                         goto exit_build_ddb_list;
835                                 }
836                         }
837                 }
838
839                 if (ddb_state != DDB_DS_SESSION_ACTIVE)
840                         goto next_one;
841                 /*
842                  * if fw_ddb with session active state found,
843                  * add to ddb_list
844                  */
845                 DEBUG2(printk("scsi%ld: %s: DDB[%d] added to list\n",
846                               ha->host_no, __func__, fw_ddb_index));
847
848                 /* Add DDB to internal our ddb list. */
849                 ddb_entry = qla4xxx_get_ddb_entry(ha, fw_ddb_index, &new_tgt);
850                 if (ddb_entry == NULL) {
851                         DEBUG2(printk("scsi%ld: %s: Unable to allocate memory "
852                                       "for device at fw_ddb_index %d\n",
853                                       ha->host_no, __func__, fw_ddb_index));
854                         goto exit_build_ddb_list;
855                 }
856                 /* Fill in the device structure */
857                 if (qla4xxx_update_ddb_entry(ha, ddb_entry, fw_ddb_index) ==
858                     QLA_ERROR) {
859                         ha->fw_ddb_index_map[fw_ddb_index] =
860                                 (struct ddb_entry *)INVALID_ENTRY;
861
862                         DEBUG2(printk("scsi%ld: %s: update_ddb_entry failed "
863                                       "for fw_ddb_index %d.\n",
864                                       ha->host_no, __func__, fw_ddb_index));
865                         goto exit_build_ddb_list;
866                 }
867
868 next_one:
869                 /* We know we've reached the last device when
870                  * next_fw_ddb_index is 0 */
871                 if (next_fw_ddb_index == 0)
872                         break;
873         }
874
875         status = QLA_SUCCESS;
876         ql4_printk(KERN_INFO, ha, "DDB list done..\n");
877
878 exit_build_ddb_list:
879         dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry), fw_ddb_entry,
880                 fw_ddb_entry_dma);
881
882 exit_build_ddb_list_no_free:
883         return status;
884 }
885
886 static int qla4xxx_initialize_ddb_list(struct scsi_qla_host *ha)
887 {
888         uint16_t fw_ddb_index;
889         int status = QLA_SUCCESS;
890
891         /* free the ddb list if is not empty */
892         if (!list_empty(&ha->ddb_list))
893                 qla4xxx_free_ddb_list(ha);
894
895         for (fw_ddb_index = 0; fw_ddb_index < MAX_DDB_ENTRIES; fw_ddb_index++)
896                 ha->fw_ddb_index_map[fw_ddb_index] =
897                     (struct ddb_entry *)INVALID_ENTRY;
898
899         ha->tot_ddbs = 0;
900
901         /* Perform device discovery and build ddb list. */
902         status = qla4xxx_build_ddb_list(ha);
903
904         return status;
905 }
906
907 /**
908  * qla4xxx_reinitialize_ddb_list - update the driver ddb list
909  * @ha: pointer to host adapter structure.
910  *
911  * This routine obtains device information from the F/W database after
912  * firmware or adapter resets.  The device table is preserved.
913  **/
914 int qla4xxx_reinitialize_ddb_list(struct scsi_qla_host *ha)
915 {
916         int status = QLA_SUCCESS;
917         struct ddb_entry *ddb_entry, *detemp;
918
919         /* Update the device information for all devices. */
920         list_for_each_entry_safe(ddb_entry, detemp, &ha->ddb_list, list) {
921                 qla4xxx_update_ddb_entry(ha, ddb_entry,
922                                          ddb_entry->fw_ddb_index);
923                 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
924                         atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
925                         DEBUG2(printk ("scsi%ld: %s: ddb index [%d] marked "
926                                        "ONLINE\n", ha->host_no, __func__,
927                                        ddb_entry->fw_ddb_index));
928                         iscsi_unblock_session(ddb_entry->sess);
929                 } else if (atomic_read(&ddb_entry->state) == DDB_STATE_ONLINE)
930                         qla4xxx_mark_device_missing(ha, ddb_entry);
931         }
932         return status;
933 }
934
935 /**
936  * qla4xxx_relogin_device - re-establish session
937  * @ha: Pointer to host adapter structure.
938  * @ddb_entry: Pointer to device database entry
939  *
940  * This routine does a session relogin with the specified device.
941  * The ddb entry must be assigned prior to making this call.
942  **/
943 int qla4xxx_relogin_device(struct scsi_qla_host *ha,
944                            struct ddb_entry * ddb_entry)
945 {
946         uint16_t relogin_timer;
947
948         relogin_timer = max(ddb_entry->default_relogin_timeout,
949                             (uint16_t)RELOGIN_TOV);
950         atomic_set(&ddb_entry->relogin_timer, relogin_timer);
951
952         DEBUG2(printk("scsi%ld: Relogin ddb [%d]. TOV=%d\n", ha->host_no,
953                       ddb_entry->fw_ddb_index, relogin_timer));
954
955         qla4xxx_set_ddb_entry(ha, ddb_entry->fw_ddb_index, 0);
956
957         return QLA_SUCCESS;
958 }
959
960 static int qla4xxx_config_nvram(struct scsi_qla_host *ha)
961 {
962         unsigned long flags;
963         union external_hw_config_reg extHwConfig;
964
965         DEBUG2(printk("scsi%ld: %s: Get EEProm parameters \n", ha->host_no,
966                       __func__));
967         if (ql4xxx_lock_flash(ha) != QLA_SUCCESS)
968                 return QLA_ERROR;
969         if (ql4xxx_lock_nvram(ha) != QLA_SUCCESS) {
970                 ql4xxx_unlock_flash(ha);
971                 return QLA_ERROR;
972         }
973
974         /* Get EEPRom Parameters from NVRAM and validate */
975         ql4_printk(KERN_INFO, ha, "Configuring NVRAM ...\n");
976         if (qla4xxx_is_nvram_configuration_valid(ha) == QLA_SUCCESS) {
977                 spin_lock_irqsave(&ha->hardware_lock, flags);
978                 extHwConfig.Asuint32_t =
979                         rd_nvram_word(ha, eeprom_ext_hw_conf_offset(ha));
980                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
981         } else {
982                 ql4_printk(KERN_WARNING, ha,
983                     "scsi%ld: %s: EEProm checksum invalid.  "
984                     "Please update your EEPROM\n", ha->host_no,
985                     __func__);
986
987                 /* Attempt to set defaults */
988                 if (is_qla4010(ha))
989                         extHwConfig.Asuint32_t = 0x1912;
990                 else if (is_qla4022(ha) | is_qla4032(ha))
991                         extHwConfig.Asuint32_t = 0x0023;
992                 else
993                         return QLA_ERROR;
994         }
995         DEBUG(printk("scsi%ld: %s: Setting extHwConfig to 0xFFFF%04x\n",
996                      ha->host_no, __func__, extHwConfig.Asuint32_t));
997
998         spin_lock_irqsave(&ha->hardware_lock, flags);
999         writel((0xFFFF << 16) | extHwConfig.Asuint32_t, isp_ext_hw_conf(ha));
1000         readl(isp_ext_hw_conf(ha));
1001         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1002
1003         ql4xxx_unlock_nvram(ha);
1004         ql4xxx_unlock_flash(ha);
1005
1006         return QLA_SUCCESS;
1007 }
1008
1009 /**
1010  * qla4_8xxx_pci_config() - Setup ISP82xx PCI configuration registers.
1011  * @ha: HA context
1012  */
1013 void qla4_8xxx_pci_config(struct scsi_qla_host *ha)
1014 {
1015         pci_set_master(ha->pdev);
1016 }
1017
1018 void qla4xxx_pci_config(struct scsi_qla_host *ha)
1019 {
1020         uint16_t w;
1021         int status;
1022
1023         ql4_printk(KERN_INFO, ha, "Configuring PCI space...\n");
1024
1025         pci_set_master(ha->pdev);
1026         status = pci_set_mwi(ha->pdev);
1027         /*
1028          * We want to respect framework's setting of PCI configuration space
1029          * command register and also want to make sure that all bits of
1030          * interest to us are properly set in command register.
1031          */
1032         pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
1033         w |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
1034         w &= ~PCI_COMMAND_INTX_DISABLE;
1035         pci_write_config_word(ha->pdev, PCI_COMMAND, w);
1036 }
1037
1038 static int qla4xxx_start_firmware_from_flash(struct scsi_qla_host *ha)
1039 {
1040         int status = QLA_ERROR;
1041         unsigned long max_wait_time;
1042         unsigned long flags;
1043         uint32_t mbox_status;
1044
1045         ql4_printk(KERN_INFO, ha, "Starting firmware ...\n");
1046
1047         /*
1048          * Start firmware from flash ROM
1049          *
1050          * WORKAROUND: Stuff a non-constant value that the firmware can
1051          * use as a seed for a random number generator in MB7 prior to
1052          * setting BOOT_ENABLE.  Fixes problem where the TCP
1053          * connections use the same TCP ports after each reboot,
1054          * causing some connections to not get re-established.
1055          */
1056         DEBUG(printk("scsi%d: %s: Start firmware from flash ROM\n",
1057                      ha->host_no, __func__));
1058
1059         spin_lock_irqsave(&ha->hardware_lock, flags);
1060         writel(jiffies, &ha->reg->mailbox[7]);
1061         if (is_qla4022(ha) | is_qla4032(ha))
1062                 writel(set_rmask(NVR_WRITE_ENABLE),
1063                        &ha->reg->u1.isp4022.nvram);
1064
1065         writel(2, &ha->reg->mailbox[6]);
1066         readl(&ha->reg->mailbox[6]);
1067
1068         writel(set_rmask(CSR_BOOT_ENABLE), &ha->reg->ctrl_status);
1069         readl(&ha->reg->ctrl_status);
1070         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1071
1072         /* Wait for firmware to come UP. */
1073         DEBUG2(printk(KERN_INFO "scsi%ld: %s: Wait up to %d seconds for "
1074                       "boot firmware to complete...\n",
1075                       ha->host_no, __func__, FIRMWARE_UP_TOV));
1076         max_wait_time = jiffies + (FIRMWARE_UP_TOV * HZ);
1077         do {
1078                 uint32_t ctrl_status;
1079
1080                 spin_lock_irqsave(&ha->hardware_lock, flags);
1081                 ctrl_status = readw(&ha->reg->ctrl_status);
1082                 mbox_status = readw(&ha->reg->mailbox[0]);
1083                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1084
1085                 if (ctrl_status & set_rmask(CSR_SCSI_PROCESSOR_INTR))
1086                         break;
1087                 if (mbox_status == MBOX_STS_COMMAND_COMPLETE)
1088                         break;
1089
1090                 DEBUG2(printk(KERN_INFO "scsi%ld: %s: Waiting for boot "
1091                     "firmware to complete... ctrl_sts=0x%x, remaining=%ld\n",
1092                     ha->host_no, __func__, ctrl_status, max_wait_time));
1093
1094                 msleep_interruptible(250);
1095         } while (!time_after_eq(jiffies, max_wait_time));
1096
1097         if (mbox_status == MBOX_STS_COMMAND_COMPLETE) {
1098                 DEBUG(printk(KERN_INFO "scsi%ld: %s: Firmware has started\n",
1099                              ha->host_no, __func__));
1100
1101                 spin_lock_irqsave(&ha->hardware_lock, flags);
1102                 writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
1103                        &ha->reg->ctrl_status);
1104                 readl(&ha->reg->ctrl_status);
1105                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1106
1107                 status = QLA_SUCCESS;
1108         } else {
1109                 printk(KERN_INFO "scsi%ld: %s: Boot firmware failed "
1110                        "-  mbox status 0x%x\n", ha->host_no, __func__,
1111                        mbox_status);
1112                 status = QLA_ERROR;
1113         }
1114         return status;
1115 }
1116
1117 int ql4xxx_lock_drvr_wait(struct scsi_qla_host *a)
1118 {
1119 #define QL4_LOCK_DRVR_WAIT      60
1120 #define QL4_LOCK_DRVR_SLEEP     1
1121
1122         int drvr_wait = QL4_LOCK_DRVR_WAIT;
1123         while (drvr_wait) {
1124                 if (ql4xxx_lock_drvr(a) == 0) {
1125                         ssleep(QL4_LOCK_DRVR_SLEEP);
1126                         if (drvr_wait) {
1127                                 DEBUG2(printk("scsi%ld: %s: Waiting for "
1128                                               "Global Init Semaphore(%d)...\n",
1129                                               a->host_no,
1130                                               __func__, drvr_wait));
1131                         }
1132                         drvr_wait -= QL4_LOCK_DRVR_SLEEP;
1133                 } else {
1134                         DEBUG2(printk("scsi%ld: %s: Global Init Semaphore "
1135                                       "acquired\n", a->host_no, __func__));
1136                         return QLA_SUCCESS;
1137                 }
1138         }
1139         return QLA_ERROR;
1140 }
1141
1142 /**
1143  * qla4xxx_start_firmware - starts qla4xxx firmware
1144  * @ha: Pointer to host adapter structure.
1145  *
1146  * This routine performs the necessary steps to start the firmware for
1147  * the QLA4010 adapter.
1148  **/
1149 int qla4xxx_start_firmware(struct scsi_qla_host *ha)
1150 {
1151         unsigned long flags = 0;
1152         uint32_t mbox_status;
1153         int status = QLA_ERROR;
1154         int soft_reset = 1;
1155         int config_chip = 0;
1156
1157         if (is_qla4022(ha) | is_qla4032(ha))
1158                 ql4xxx_set_mac_number(ha);
1159
1160         if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
1161                 return QLA_ERROR;
1162
1163         spin_lock_irqsave(&ha->hardware_lock, flags);
1164
1165         DEBUG2(printk("scsi%ld: %s: port_ctrl   = 0x%08X\n", ha->host_no,
1166                       __func__, readw(isp_port_ctrl(ha))));
1167         DEBUG(printk("scsi%ld: %s: port_status = 0x%08X\n", ha->host_no,
1168                      __func__, readw(isp_port_status(ha))));
1169
1170         /* Is Hardware already initialized? */
1171         if ((readw(isp_port_ctrl(ha)) & 0x8000) != 0) {
1172                 DEBUG(printk("scsi%ld: %s: Hardware has already been "
1173                              "initialized\n", ha->host_no, __func__));
1174
1175                 /* Receive firmware boot acknowledgement */
1176                 mbox_status = readw(&ha->reg->mailbox[0]);
1177
1178                 DEBUG2(printk("scsi%ld: %s: H/W Config complete - mbox[0]= "
1179                               "0x%x\n", ha->host_no, __func__, mbox_status));
1180
1181                 /* Is firmware already booted? */
1182                 if (mbox_status == 0) {
1183                         /* F/W not running, must be config by net driver */
1184                         config_chip = 1;
1185                         soft_reset = 0;
1186                 } else {
1187                         writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
1188                                &ha->reg->ctrl_status);
1189                         readl(&ha->reg->ctrl_status);
1190                         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1191                         if (qla4xxx_get_firmware_state(ha) == QLA_SUCCESS) {
1192                                 DEBUG2(printk("scsi%ld: %s: Get firmware "
1193                                               "state -- state = 0x%x\n",
1194                                               ha->host_no,
1195                                               __func__, ha->firmware_state));
1196                                 /* F/W is running */
1197                                 if (ha->firmware_state &
1198                                     FW_STATE_CONFIG_WAIT) {
1199                                         DEBUG2(printk("scsi%ld: %s: Firmware "
1200                                                       "in known state -- "
1201                                                       "config and "
1202                                                       "boot, state = 0x%x\n",
1203                                                       ha->host_no, __func__,
1204                                                       ha->firmware_state));
1205                                         config_chip = 1;
1206                                         soft_reset = 0;
1207                                 }
1208                         } else {
1209                                 DEBUG2(printk("scsi%ld: %s: Firmware in "
1210                                               "unknown state -- resetting,"
1211                                               " state = "
1212                                               "0x%x\n", ha->host_no, __func__,
1213                                               ha->firmware_state));
1214                         }
1215                         spin_lock_irqsave(&ha->hardware_lock, flags);
1216                 }
1217         } else {
1218                 DEBUG(printk("scsi%ld: %s: H/W initialization hasn't been "
1219                              "started - resetting\n", ha->host_no, __func__));
1220         }
1221         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1222
1223         DEBUG(printk("scsi%ld: %s: Flags soft_rest=%d, config= %d\n ",
1224                      ha->host_no, __func__, soft_reset, config_chip));
1225         if (soft_reset) {
1226                 DEBUG(printk("scsi%ld: %s: Issue Soft Reset\n", ha->host_no,
1227                              __func__));
1228                 status = qla4xxx_soft_reset(ha);        /* NOTE: acquires drvr
1229                                                          * lock again, but ok */
1230                 if (status == QLA_ERROR) {
1231                         DEBUG(printk("scsi%d: %s: Soft Reset failed!\n",
1232                                      ha->host_no, __func__));
1233                         ql4xxx_unlock_drvr(ha);
1234                         return QLA_ERROR;
1235                 }
1236                 config_chip = 1;
1237
1238                 /* Reset clears the semaphore, so acquire again */
1239                 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
1240                         return QLA_ERROR;
1241         }
1242
1243         if (config_chip) {
1244                 if ((status = qla4xxx_config_nvram(ha)) == QLA_SUCCESS)
1245                         status = qla4xxx_start_firmware_from_flash(ha);
1246         }
1247
1248         ql4xxx_unlock_drvr(ha);
1249         if (status == QLA_SUCCESS) {
1250                 if (test_and_clear_bit(AF_GET_CRASH_RECORD, &ha->flags))
1251                         qla4xxx_get_crash_record(ha);
1252         } else {
1253                 DEBUG(printk("scsi%ld: %s: Firmware has NOT started\n",
1254                              ha->host_no, __func__));
1255         }
1256         return status;
1257 }
1258
1259
1260 /**
1261  * qla4xxx_initialize_adapter - initiailizes hba
1262  * @ha: Pointer to host adapter structure.
1263  * @renew_ddb_list: Indicates what to do with the adapter's ddb list
1264  *      after adapter recovery has completed.
1265  *      0=preserve ddb list, 1=destroy and rebuild ddb list
1266  *
1267  * This routine parforms all of the steps necessary to initialize the adapter.
1268  *
1269  **/
1270 int qla4xxx_initialize_adapter(struct scsi_qla_host *ha,
1271                                uint8_t renew_ddb_list)
1272 {
1273         int status = QLA_ERROR;
1274         int8_t ip_address[IP_ADDR_LEN] = {0} ;
1275
1276         ha->eeprom_cmd_data = 0;
1277
1278         ql4_printk(KERN_INFO, ha, "Configuring PCI space...\n");
1279         ha->isp_ops->pci_config(ha);
1280
1281         ha->isp_ops->disable_intrs(ha);
1282
1283         /* Initialize the Host adapter request/response queues and firmware */
1284         if (ha->isp_ops->start_firmware(ha) == QLA_ERROR)
1285                 goto exit_init_hba;
1286
1287         if (qla4xxx_about_firmware(ha) == QLA_ERROR)
1288                 goto exit_init_hba;
1289
1290         if (ha->isp_ops->get_sys_info(ha) == QLA_ERROR)
1291                 goto exit_init_hba;
1292
1293         if (qla4xxx_init_local_data(ha) == QLA_ERROR)
1294                 goto exit_init_hba;
1295
1296         status = qla4xxx_init_firmware(ha);
1297         if (status == QLA_ERROR)
1298                 goto exit_init_hba;
1299
1300         /*
1301          * FW is waiting to get an IP address from DHCP server: Skip building
1302          * the ddb_list and wait for DHCP lease acquired aen to come in
1303          * followed by 0x8014 aen" to trigger the tgt discovery process.
1304          */
1305         if (ha->firmware_state & FW_STATE_CONFIGURING_IP)
1306                 goto exit_init_online;
1307
1308         /* Skip device discovery if ip and subnet is zero */
1309         if (memcmp(ha->ip_config.ip_address, ip_address, IP_ADDR_LEN) == 0 ||
1310             memcmp(ha->ip_config.subnet_mask, ip_address, IP_ADDR_LEN) == 0)
1311                 goto exit_init_online;
1312
1313         if (renew_ddb_list == PRESERVE_DDB_LIST) {
1314                 /*
1315                  * We want to preserve lun states (i.e. suspended, etc.)
1316                  * for recovery initiated by the driver.  So just update
1317                  * the device states for the existing ddb_list.
1318                  */
1319                 qla4xxx_reinitialize_ddb_list(ha);
1320         } else if (renew_ddb_list == REBUILD_DDB_LIST) {
1321                 /*
1322                  * We want to build the ddb_list from scratch during
1323                  * driver initialization and recovery initiated by the
1324                  * INT_HBA_RESET IOCTL.
1325                  */
1326                 status = qla4xxx_initialize_ddb_list(ha);
1327                 if (status == QLA_ERROR) {
1328                         DEBUG2(printk("%s(%ld) Error occurred during build"
1329                                       "ddb list\n", __func__, ha->host_no));
1330                         goto exit_init_hba;
1331                 }
1332
1333         }
1334         if (!ha->tot_ddbs) {
1335                 DEBUG2(printk("scsi%ld: Failed to initialize devices or none "
1336                               "present in Firmware device database\n",
1337                               ha->host_no));
1338         }
1339
1340 exit_init_online:
1341         set_bit(AF_ONLINE, &ha->flags);
1342 exit_init_hba:
1343         if (is_qla8022(ha) && (status == QLA_ERROR)) {
1344                 /* Since interrupts are registered in start_firmware for
1345                  * 82xx, release them here if initialize_adapter fails */
1346                 qla4xxx_free_irqs(ha);
1347         }
1348
1349         DEBUG2(printk("scsi%ld: initialize adapter: %s\n", ha->host_no,
1350             status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
1351         return status;
1352 }
1353
1354 /**
1355  * qla4xxx_add_device_dynamically - ddb addition due to an AEN
1356  * @ha:  Pointer to host adapter structure.
1357  * @fw_ddb_index:  Firmware's device database index
1358  *
1359  * This routine processes adds a device as a result of an 8014h AEN.
1360  **/
1361 static void qla4xxx_add_device_dynamically(struct scsi_qla_host *ha,
1362                                            uint32_t fw_ddb_index)
1363 {
1364         struct ddb_entry * ddb_entry;
1365         uint32_t new_tgt;
1366
1367         /* First allocate a device structure */
1368         ddb_entry = qla4xxx_get_ddb_entry(ha, fw_ddb_index, &new_tgt);
1369         if (ddb_entry == NULL) {
1370                 DEBUG2(printk(KERN_WARNING
1371                               "scsi%ld: Unable to allocate memory to add "
1372                               "fw_ddb_index %d\n", ha->host_no, fw_ddb_index));
1373                 return;
1374         }
1375
1376         if (!new_tgt && (ddb_entry->fw_ddb_index != fw_ddb_index)) {
1377                 /* Target has been bound to a new fw_ddb_index */
1378                 qla4xxx_free_ddb(ha, ddb_entry);
1379                 ddb_entry = qla4xxx_alloc_ddb(ha, fw_ddb_index);
1380                 if (ddb_entry == NULL) {
1381                         DEBUG2(printk(KERN_WARNING
1382                                 "scsi%ld: Unable to allocate memory"
1383                                 " to add fw_ddb_index %d\n",
1384                                 ha->host_no, fw_ddb_index));
1385                         return;
1386                 }
1387         }
1388         if (qla4xxx_update_ddb_entry(ha, ddb_entry, fw_ddb_index) ==
1389                                     QLA_ERROR) {
1390                 ha->fw_ddb_index_map[fw_ddb_index] =
1391                                         (struct ddb_entry *)INVALID_ENTRY;
1392                 DEBUG2(printk(KERN_WARNING
1393                               "scsi%ld: failed to add new device at index "
1394                               "[%d]\n Unable to retrieve fw ddb entry\n",
1395                               ha->host_no, fw_ddb_index));
1396                 qla4xxx_free_ddb(ha, ddb_entry);
1397                 return;
1398         }
1399
1400         if (qla4xxx_add_sess(ddb_entry)) {
1401                 DEBUG2(printk(KERN_WARNING
1402                               "scsi%ld: failed to add new device at index "
1403                               "[%d]\n Unable to add connection and session\n",
1404                               ha->host_no, fw_ddb_index));
1405                 qla4xxx_free_ddb(ha, ddb_entry);
1406         }
1407 }
1408
1409 /**
1410  * qla4xxx_process_ddb_changed - process ddb state change
1411  * @ha - Pointer to host adapter structure.
1412  * @fw_ddb_index - Firmware's device database index
1413  * @state - Device state
1414  *
1415  * This routine processes a Decive Database Changed AEN Event.
1416  **/
1417 int qla4xxx_process_ddb_changed(struct scsi_qla_host *ha, uint32_t fw_ddb_index,
1418                 uint32_t state, uint32_t conn_err)
1419 {
1420         struct ddb_entry * ddb_entry;
1421
1422         /* check for out of range index */
1423         if (fw_ddb_index >= MAX_DDB_ENTRIES)
1424                 return QLA_ERROR;
1425
1426         /* Get the corresponging ddb entry */
1427         ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, fw_ddb_index);
1428         /* Device does not currently exist in our database. */
1429         if (ddb_entry == NULL) {
1430                 if (state == DDB_DS_SESSION_ACTIVE)
1431                         qla4xxx_add_device_dynamically(ha, fw_ddb_index);
1432                 return QLA_SUCCESS;
1433         }
1434
1435         /* Device already exists in our database. */
1436         DEBUG2(printk("scsi%ld: %s DDB - old state= 0x%x, new state=0x%x for "
1437                       "index [%d]\n", ha->host_no, __func__,
1438                       ddb_entry->fw_ddb_device_state, state, fw_ddb_index));
1439
1440         ddb_entry->fw_ddb_device_state = state;
1441         /* Device is back online. */
1442         if ((ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) &&
1443            (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)) {
1444                 atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
1445                 atomic_set(&ddb_entry->relogin_retry_count, 0);
1446                 atomic_set(&ddb_entry->relogin_timer, 0);
1447                 clear_bit(DF_RELOGIN, &ddb_entry->flags);
1448                 iscsi_unblock_session(ddb_entry->sess);
1449                 iscsi_session_event(ddb_entry->sess,
1450                                     ISCSI_KEVENT_CREATE_SESSION);
1451                 /*
1452                  * Change the lun state to READY in case the lun TIMEOUT before
1453                  * the device came back.
1454                  */
1455         } else if (ddb_entry->fw_ddb_device_state != DDB_DS_SESSION_ACTIVE) {
1456                 /* Device went away, mark device missing */
1457                 if (atomic_read(&ddb_entry->state) == DDB_STATE_ONLINE) {
1458                         DEBUG2(ql4_printk(KERN_INFO, ha, "%s mark missing "
1459                                         "ddb_entry 0x%p sess 0x%p conn 0x%p\n",
1460                                         __func__, ddb_entry,
1461                                         ddb_entry->sess, ddb_entry->conn));
1462                         qla4xxx_mark_device_missing(ha, ddb_entry);
1463                 }
1464
1465                 /*
1466                  * Relogin if device state changed to a not active state.
1467                  * However, do not relogin if a RELOGIN is in process, or
1468                  * we are not allowed to relogin to this DDB.
1469                  */
1470                 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_FAILED &&
1471                     !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
1472                     qla4_is_relogin_allowed(ha, conn_err)) {
1473                         /*
1474                          * This triggers a relogin.  After the relogin_timer
1475                          * expires, the relogin gets scheduled.  We must wait a
1476                          * minimum amount of time since receiving an 0x8014 AEN
1477                          * with failed device_state or a logout response before
1478                          * we can issue another relogin.
1479                          */
1480                         /* Firmware pads this timeout: (time2wait +1).
1481                          * Driver retry to login should be longer than F/W.
1482                          * Otherwise F/W will fail
1483                          * set_ddb() mbx cmd with 0x4005 since it still
1484                          * counting down its time2wait.
1485                          */
1486                         atomic_set(&ddb_entry->relogin_timer, 0);
1487                         atomic_set(&ddb_entry->retry_relogin_timer,
1488                                    ddb_entry->default_time2wait + 4);
1489                         DEBUG(printk("scsi%ld: %s: ddb[%d] "
1490                             "initiate relogin after %d seconds\n",
1491                             ha->host_no, __func__,
1492                             ddb_entry->fw_ddb_index,
1493                             ddb_entry->default_time2wait + 4));
1494                 } else {
1495                         DEBUG(printk("scsi%ld: %s: ddb[%d] "
1496                             "relogin not initiated, state = %d, "
1497                             "ddb_entry->flags = 0x%lx\n",
1498                             ha->host_no, __func__,
1499                             ddb_entry->fw_ddb_index,
1500                             ddb_entry->fw_ddb_device_state,
1501                             ddb_entry->flags));
1502                 }
1503         }
1504         return QLA_SUCCESS;
1505 }