Merge git://git.kernel.org/pub/scm/linux/kernel/git/mchehab/linux-edac
[cascardo/linux.git] / drivers / scsi / qla4xxx / ql4_init.c
1 /*
2  * QLogic iSCSI HBA Driver
3  * Copyright (c)  2003-2012 QLogic Corporation
4  *
5  * See LICENSE.qla4xxx for copyright and licensing details.
6  */
7
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 void ql4xxx_set_mac_number(struct scsi_qla_host *ha)
15 {
16         uint32_t value;
17         uint8_t func_number;
18         unsigned long flags;
19
20         /* Get the function number */
21         spin_lock_irqsave(&ha->hardware_lock, flags);
22         value = readw(&ha->reg->ctrl_status);
23         spin_unlock_irqrestore(&ha->hardware_lock, flags);
24
25         func_number = (uint8_t) ((value >> 4) & 0x30);
26         switch (value & ISP_CONTROL_FN_MASK) {
27         case ISP_CONTROL_FN0_SCSI:
28                 ha->mac_index = 1;
29                 break;
30         case ISP_CONTROL_FN1_SCSI:
31                 ha->mac_index = 3;
32                 break;
33         default:
34                 DEBUG2(printk("scsi%ld: %s: Invalid function number, "
35                               "ispControlStatus = 0x%x\n", ha->host_no,
36                               __func__, value));
37                 break;
38         }
39         DEBUG2(printk("scsi%ld: %s: mac_index %d.\n", ha->host_no, __func__,
40                       ha->mac_index));
41 }
42
43 /**
44  * qla4xxx_free_ddb - deallocate ddb
45  * @ha: pointer to host adapter structure.
46  * @ddb_entry: pointer to device database entry
47  *
48  * This routine marks a DDB entry INVALID
49  **/
50 void qla4xxx_free_ddb(struct scsi_qla_host *ha,
51     struct ddb_entry *ddb_entry)
52 {
53         /* Remove device pointer from index mapping arrays */
54         ha->fw_ddb_index_map[ddb_entry->fw_ddb_index] =
55                 (struct ddb_entry *) INVALID_ENTRY;
56         ha->tot_ddbs--;
57 }
58
59 /**
60  * qla4xxx_init_response_q_entries() - Initializes response queue entries.
61  * @ha: HA context
62  *
63  * Beginning of request ring has initialization control block already built
64  * by nvram config routine.
65  **/
66 static void qla4xxx_init_response_q_entries(struct scsi_qla_host *ha)
67 {
68         uint16_t cnt;
69         struct response *pkt;
70
71         pkt = (struct response *)ha->response_ptr;
72         for (cnt = 0; cnt < RESPONSE_QUEUE_DEPTH; cnt++) {
73                 pkt->signature = RESPONSE_PROCESSED;
74                 pkt++;
75         }
76 }
77
78 /**
79  * qla4xxx_init_rings - initialize hw queues
80  * @ha: pointer to host adapter structure.
81  *
82  * This routine initializes the internal queues for the specified adapter.
83  * The QLA4010 requires us to restart the queues at index 0.
84  * The QLA4000 doesn't care, so just default to QLA4010's requirement.
85  **/
86 int qla4xxx_init_rings(struct scsi_qla_host *ha)
87 {
88         unsigned long flags = 0;
89         int i;
90
91         /* Initialize request queue. */
92         spin_lock_irqsave(&ha->hardware_lock, flags);
93         ha->request_out = 0;
94         ha->request_in = 0;
95         ha->request_ptr = &ha->request_ring[ha->request_in];
96         ha->req_q_count = REQUEST_QUEUE_DEPTH;
97
98         /* Initialize response queue. */
99         ha->response_in = 0;
100         ha->response_out = 0;
101         ha->response_ptr = &ha->response_ring[ha->response_out];
102
103         if (is_qla8022(ha)) {
104                 writel(0,
105                     (unsigned long  __iomem *)&ha->qla4_82xx_reg->req_q_out);
106                 writel(0,
107                     (unsigned long  __iomem *)&ha->qla4_82xx_reg->rsp_q_in);
108                 writel(0,
109                     (unsigned long  __iomem *)&ha->qla4_82xx_reg->rsp_q_out);
110         } else if (is_qla8032(ha)) {
111                 writel(0,
112                        (unsigned long __iomem *)&ha->qla4_83xx_reg->req_q_in);
113                 writel(0,
114                        (unsigned long __iomem *)&ha->qla4_83xx_reg->rsp_q_in);
115                 writel(0,
116                        (unsigned long __iomem *)&ha->qla4_83xx_reg->rsp_q_out);
117         } else {
118                 /*
119                  * Initialize DMA Shadow registers.  The firmware is really
120                  * supposed to take care of this, but on some uniprocessor
121                  * systems, the shadow registers aren't cleared-- causing
122                  * the interrupt_handler to think there are responses to be
123                  * processed when there aren't.
124                  */
125                 ha->shadow_regs->req_q_out = __constant_cpu_to_le32(0);
126                 ha->shadow_regs->rsp_q_in = __constant_cpu_to_le32(0);
127                 wmb();
128
129                 writel(0, &ha->reg->req_q_in);
130                 writel(0, &ha->reg->rsp_q_out);
131                 readl(&ha->reg->rsp_q_out);
132         }
133
134         qla4xxx_init_response_q_entries(ha);
135
136         /* Initialize mailbox active array */
137         for (i = 0; i < MAX_MRB; i++)
138                 ha->active_mrb_array[i] = NULL;
139
140         spin_unlock_irqrestore(&ha->hardware_lock, flags);
141
142         return QLA_SUCCESS;
143 }
144
145 /**
146  * qla4xxx_get_sys_info - validate adapter MAC address(es)
147  * @ha: pointer to host adapter structure.
148  *
149  **/
150 int qla4xxx_get_sys_info(struct scsi_qla_host *ha)
151 {
152         struct flash_sys_info *sys_info;
153         dma_addr_t sys_info_dma;
154         int status = QLA_ERROR;
155
156         sys_info = dma_alloc_coherent(&ha->pdev->dev, sizeof(*sys_info),
157                                       &sys_info_dma, GFP_KERNEL);
158         if (sys_info == NULL) {
159                 DEBUG2(printk("scsi%ld: %s: Unable to allocate dma buffer.\n",
160                               ha->host_no, __func__));
161
162                 goto exit_get_sys_info_no_free;
163         }
164         memset(sys_info, 0, sizeof(*sys_info));
165
166         /* Get flash sys info */
167         if (qla4xxx_get_flash(ha, sys_info_dma, FLASH_OFFSET_SYS_INFO,
168                               sizeof(*sys_info)) != QLA_SUCCESS) {
169                 DEBUG2(printk("scsi%ld: %s: get_flash FLASH_OFFSET_SYS_INFO "
170                               "failed\n", ha->host_no, __func__));
171
172                 goto exit_get_sys_info;
173         }
174
175         /* Save M.A.C. address & serial_number */
176         memcpy(ha->my_mac, &sys_info->physAddr[0].address[0],
177                min(sizeof(ha->my_mac),
178                    sizeof(sys_info->physAddr[0].address)));
179         memcpy(ha->serial_number, &sys_info->acSerialNumber,
180                min(sizeof(ha->serial_number),
181                    sizeof(sys_info->acSerialNumber)));
182
183         status = QLA_SUCCESS;
184
185 exit_get_sys_info:
186         dma_free_coherent(&ha->pdev->dev, sizeof(*sys_info), sys_info,
187                           sys_info_dma);
188
189 exit_get_sys_info_no_free:
190         return status;
191 }
192
193 /**
194  * qla4xxx_init_local_data - initialize adapter specific local data
195  * @ha: pointer to host adapter structure.
196  *
197  **/
198 static int qla4xxx_init_local_data(struct scsi_qla_host *ha)
199 {
200         /* Initialize aen queue */
201         ha->aen_q_count = MAX_AEN_ENTRIES;
202
203         return qla4xxx_get_firmware_status(ha);
204 }
205
206 static uint8_t
207 qla4xxx_wait_for_ip_config(struct scsi_qla_host *ha)
208 {
209         uint8_t ipv4_wait = 0;
210         uint8_t ipv6_wait = 0;
211         int8_t ip_address[IPv6_ADDR_LEN] = {0} ;
212
213         /* If both IPv4 & IPv6 are enabled, possibly only one
214          * IP address may be acquired, so check to see if we
215          * need to wait for another */
216         if (is_ipv4_enabled(ha) && is_ipv6_enabled(ha)) {
217                 if (((ha->addl_fw_state & FW_ADDSTATE_DHCPv4_ENABLED) != 0) &&
218                     ((ha->addl_fw_state &
219                                     FW_ADDSTATE_DHCPv4_LEASE_ACQUIRED) == 0)) {
220                         ipv4_wait = 1;
221                 }
222                 if (((ha->ip_config.ipv6_addl_options &
223                       IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) != 0) &&
224                     ((ha->ip_config.ipv6_link_local_state ==
225                       IP_ADDRSTATE_ACQUIRING) ||
226                      (ha->ip_config.ipv6_addr0_state ==
227                       IP_ADDRSTATE_ACQUIRING) ||
228                      (ha->ip_config.ipv6_addr1_state ==
229                       IP_ADDRSTATE_ACQUIRING))) {
230
231                         ipv6_wait = 1;
232
233                         if ((ha->ip_config.ipv6_link_local_state ==
234                              IP_ADDRSTATE_PREFERRED) ||
235                             (ha->ip_config.ipv6_addr0_state ==
236                              IP_ADDRSTATE_PREFERRED) ||
237                             (ha->ip_config.ipv6_addr1_state ==
238                              IP_ADDRSTATE_PREFERRED)) {
239                                 DEBUG2(printk(KERN_INFO "scsi%ld: %s: "
240                                               "Preferred IP configured."
241                                               " Don't wait!\n", ha->host_no,
242                                               __func__));
243                                 ipv6_wait = 0;
244                         }
245                         if (memcmp(&ha->ip_config.ipv6_default_router_addr,
246                                    ip_address, IPv6_ADDR_LEN) == 0) {
247                                 DEBUG2(printk(KERN_INFO "scsi%ld: %s: "
248                                               "No Router configured. "
249                                               "Don't wait!\n", ha->host_no,
250                                               __func__));
251                                 ipv6_wait = 0;
252                         }
253                         if ((ha->ip_config.ipv6_default_router_state ==
254                              IPV6_RTRSTATE_MANUAL) &&
255                             (ha->ip_config.ipv6_link_local_state ==
256                              IP_ADDRSTATE_TENTATIVE) &&
257                             (memcmp(&ha->ip_config.ipv6_link_local_addr,
258                              &ha->ip_config.ipv6_default_router_addr, 4) ==
259                              0)) {
260                                 DEBUG2(printk("scsi%ld: %s: LinkLocal Router & "
261                                         "IP configured. Don't wait!\n",
262                                         ha->host_no, __func__));
263                                 ipv6_wait = 0;
264                         }
265                 }
266                 if (ipv4_wait || ipv6_wait) {
267                         DEBUG2(printk("scsi%ld: %s: Wait for additional "
268                                       "IP(s) \"", ha->host_no, __func__));
269                         if (ipv4_wait)
270                                 DEBUG2(printk("IPv4 "));
271                         if (ha->ip_config.ipv6_link_local_state ==
272                             IP_ADDRSTATE_ACQUIRING)
273                                 DEBUG2(printk("IPv6LinkLocal "));
274                         if (ha->ip_config.ipv6_addr0_state ==
275                             IP_ADDRSTATE_ACQUIRING)
276                                 DEBUG2(printk("IPv6Addr0 "));
277                         if (ha->ip_config.ipv6_addr1_state ==
278                             IP_ADDRSTATE_ACQUIRING)
279                                 DEBUG2(printk("IPv6Addr1 "));
280                         DEBUG2(printk("\"\n"));
281                 }
282         }
283
284         return ipv4_wait|ipv6_wait;
285 }
286
287 /**
288  * qla4xxx_alloc_fw_dump - Allocate memory for minidump data.
289  * @ha: pointer to host adapter structure.
290  **/
291 void qla4xxx_alloc_fw_dump(struct scsi_qla_host *ha)
292 {
293         int status;
294         uint32_t capture_debug_level;
295         int hdr_entry_bit, k;
296         void *md_tmp;
297         dma_addr_t md_tmp_dma;
298         struct qla4_8xxx_minidump_template_hdr *md_hdr;
299
300         if (ha->fw_dump) {
301                 ql4_printk(KERN_WARNING, ha,
302                            "Firmware dump previously allocated.\n");
303                 return;
304         }
305
306         status = qla4xxx_req_template_size(ha);
307         if (status != QLA_SUCCESS) {
308                 ql4_printk(KERN_INFO, ha,
309                            "scsi%ld: Failed to get template size\n",
310                            ha->host_no);
311                 return;
312         }
313
314         clear_bit(AF_82XX_FW_DUMPED, &ha->flags);
315
316         /* Allocate memory for saving the template */
317         md_tmp = dma_alloc_coherent(&ha->pdev->dev, ha->fw_dump_tmplt_size,
318                                     &md_tmp_dma, GFP_KERNEL);
319
320         /* Request template */
321         status =  qla4xxx_get_minidump_template(ha, md_tmp_dma);
322         if (status != QLA_SUCCESS) {
323                 ql4_printk(KERN_INFO, ha,
324                            "scsi%ld: Failed to get minidump template\n",
325                            ha->host_no);
326                 goto alloc_cleanup;
327         }
328
329         md_hdr = (struct qla4_8xxx_minidump_template_hdr *)md_tmp;
330
331         capture_debug_level = md_hdr->capture_debug_level;
332
333         /* Get capture mask based on module loadtime setting. */
334         if (ql4xmdcapmask >= 0x3 && ql4xmdcapmask <= 0x7F)
335                 ha->fw_dump_capture_mask = ql4xmdcapmask;
336         else
337                 ha->fw_dump_capture_mask = capture_debug_level;
338
339         md_hdr->driver_capture_mask = ha->fw_dump_capture_mask;
340
341         DEBUG2(ql4_printk(KERN_INFO, ha, "Minimum num of entries = %d\n",
342                           md_hdr->num_of_entries));
343         DEBUG2(ql4_printk(KERN_INFO, ha, "Dump template size  = %d\n",
344                           ha->fw_dump_tmplt_size));
345         DEBUG2(ql4_printk(KERN_INFO, ha, "Selected Capture mask =0x%x\n",
346                           ha->fw_dump_capture_mask));
347
348         /* Calculate fw_dump_size */
349         for (hdr_entry_bit = 0x2, k = 1; (hdr_entry_bit & 0xFF);
350              hdr_entry_bit <<= 1, k++) {
351                 if (hdr_entry_bit & ha->fw_dump_capture_mask)
352                         ha->fw_dump_size += md_hdr->capture_size_array[k];
353         }
354
355         /* Total firmware dump size including command header */
356         ha->fw_dump_size += ha->fw_dump_tmplt_size;
357         ha->fw_dump = vmalloc(ha->fw_dump_size);
358         if (!ha->fw_dump)
359                 goto alloc_cleanup;
360
361         DEBUG2(ql4_printk(KERN_INFO, ha,
362                           "Minidump Tempalate Size = 0x%x KB\n",
363                           ha->fw_dump_tmplt_size));
364         DEBUG2(ql4_printk(KERN_INFO, ha,
365                           "Total Minidump size = 0x%x KB\n", ha->fw_dump_size));
366
367         memcpy(ha->fw_dump, md_tmp, ha->fw_dump_tmplt_size);
368         ha->fw_dump_tmplt_hdr = ha->fw_dump;
369
370 alloc_cleanup:
371         dma_free_coherent(&ha->pdev->dev, ha->fw_dump_tmplt_size,
372                           md_tmp, md_tmp_dma);
373 }
374
375 static int qla4xxx_fw_ready(struct scsi_qla_host *ha)
376 {
377         uint32_t timeout_count;
378         int ready = 0;
379
380         DEBUG2(ql4_printk(KERN_INFO, ha, "Waiting for Firmware Ready..\n"));
381         for (timeout_count = ADAPTER_INIT_TOV; timeout_count > 0;
382              timeout_count--) {
383                 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
384                         qla4xxx_get_dhcp_ip_address(ha);
385
386                 /* Get firmware state. */
387                 if (qla4xxx_get_firmware_state(ha) != QLA_SUCCESS) {
388                         DEBUG2(printk("scsi%ld: %s: unable to get firmware "
389                                       "state\n", ha->host_no, __func__));
390                         break;
391                 }
392
393                 if (ha->firmware_state & FW_STATE_ERROR) {
394                         DEBUG2(printk("scsi%ld: %s: an unrecoverable error has"
395                                       " occurred\n", ha->host_no, __func__));
396                         break;
397
398                 }
399                 if (ha->firmware_state & FW_STATE_CONFIG_WAIT) {
400                         /*
401                          * The firmware has not yet been issued an Initialize
402                          * Firmware command, so issue it now.
403                          */
404                         if (qla4xxx_initialize_fw_cb(ha) == QLA_ERROR)
405                                 break;
406
407                         /* Go back and test for ready state - no wait. */
408                         continue;
409                 }
410
411                 if (ha->firmware_state & FW_STATE_WAIT_AUTOCONNECT) {
412                         DEBUG2(printk(KERN_INFO "scsi%ld: %s: fwstate:"
413                                       "AUTOCONNECT in progress\n",
414                                       ha->host_no, __func__));
415                 }
416
417                 if (ha->firmware_state & FW_STATE_CONFIGURING_IP) {
418                         DEBUG2(printk(KERN_INFO "scsi%ld: %s: fwstate:"
419                                       " CONFIGURING IP\n",
420                                       ha->host_no, __func__));
421                         /*
422                          * Check for link state after 15 secs and if link is
423                          * still DOWN then, cable is unplugged. Ignore "DHCP
424                          * in Progress/CONFIGURING IP" bit to check if firmware
425                          * is in ready state or not after 15 secs.
426                          * This is applicable for both 2.x & 3.x firmware
427                          */
428                         if (timeout_count <= (ADAPTER_INIT_TOV - 15)) {
429                                 if (ha->addl_fw_state & FW_ADDSTATE_LINK_UP) {
430                                         DEBUG2(printk(KERN_INFO "scsi%ld: %s:"
431                                                   " LINK UP (Cable plugged)\n",
432                                                   ha->host_no, __func__));
433                                 } else if (ha->firmware_state &
434                                           (FW_STATE_CONFIGURING_IP |
435                                                              FW_STATE_READY)) {
436                                         DEBUG2(printk(KERN_INFO "scsi%ld: %s: "
437                                                 "LINK DOWN (Cable unplugged)\n",
438                                                 ha->host_no, __func__));
439                                         ha->firmware_state = FW_STATE_READY;
440                                 }
441                         }
442                 }
443
444                 if (ha->firmware_state == FW_STATE_READY) {
445                         /* If DHCP IP Addr is available, retrieve it now. */
446                         if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR,
447                                                                 &ha->dpc_flags))
448                                 qla4xxx_get_dhcp_ip_address(ha);
449
450                         if (!qla4xxx_wait_for_ip_config(ha) ||
451                                                         timeout_count == 1) {
452                                 DEBUG2(ql4_printk(KERN_INFO, ha,
453                                     "Firmware Ready..\n"));
454                                 /* The firmware is ready to process SCSI
455                                    commands. */
456                                 DEBUG2(ql4_printk(KERN_INFO, ha,
457                                         "scsi%ld: %s: MEDIA TYPE"
458                                         " - %s\n", ha->host_no,
459                                         __func__, (ha->addl_fw_state &
460                                         FW_ADDSTATE_OPTICAL_MEDIA)
461                                         != 0 ? "OPTICAL" : "COPPER"));
462                                 DEBUG2(ql4_printk(KERN_INFO, ha,
463                                         "scsi%ld: %s: DHCPv4 STATE"
464                                         " Enabled %s\n", ha->host_no,
465                                          __func__, (ha->addl_fw_state &
466                                          FW_ADDSTATE_DHCPv4_ENABLED) != 0 ?
467                                         "YES" : "NO"));
468                                 DEBUG2(ql4_printk(KERN_INFO, ha,
469                                         "scsi%ld: %s: LINK %s\n",
470                                         ha->host_no, __func__,
471                                         (ha->addl_fw_state &
472                                          FW_ADDSTATE_LINK_UP) != 0 ?
473                                         "UP" : "DOWN"));
474                                 DEBUG2(ql4_printk(KERN_INFO, ha,
475                                         "scsi%ld: %s: iSNS Service "
476                                         "Started %s\n",
477                                         ha->host_no, __func__,
478                                         (ha->addl_fw_state &
479                                          FW_ADDSTATE_ISNS_SVC_ENABLED) != 0 ?
480                                         "YES" : "NO"));
481
482                                 ready = 1;
483                                 break;
484                         }
485                 }
486                 DEBUG2(printk("scsi%ld: %s: waiting on fw, state=%x:%x - "
487                               "seconds expired= %d\n", ha->host_no, __func__,
488                               ha->firmware_state, ha->addl_fw_state,
489                               timeout_count));
490                 if (is_qla4032(ha) &&
491                         !(ha->addl_fw_state & FW_ADDSTATE_LINK_UP) &&
492                         (timeout_count < ADAPTER_INIT_TOV - 5)) {
493                         break;
494                 }
495
496                 msleep(1000);
497         }                       /* end of for */
498
499         if (timeout_count <= 0)
500                 DEBUG2(printk("scsi%ld: %s: FW Initialization timed out!\n",
501                               ha->host_no, __func__));
502
503         if (ha->firmware_state & FW_STATE_CONFIGURING_IP) {
504                 DEBUG2(printk("scsi%ld: %s: FW initialized, but is reporting "
505                               "it's waiting to configure an IP address\n",
506                                ha->host_no, __func__));
507                 ready = 1;
508         } else if (ha->firmware_state & FW_STATE_WAIT_AUTOCONNECT) {
509                 DEBUG2(printk("scsi%ld: %s: FW initialized, but "
510                               "auto-discovery still in process\n",
511                                ha->host_no, __func__));
512                 ready = 1;
513         }
514
515         return ready;
516 }
517
518 /**
519  * qla4xxx_init_firmware - initializes the firmware.
520  * @ha: pointer to host adapter structure.
521  *
522  **/
523 static int qla4xxx_init_firmware(struct scsi_qla_host *ha)
524 {
525         int status = QLA_ERROR;
526
527         if (is_aer_supported(ha) &&
528             test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
529                 return status;
530
531         /* For 82xx, stop firmware before initializing because if BIOS
532          * has previously initialized firmware, then driver's initialize
533          * firmware will fail. */
534         if (is_qla80XX(ha))
535                 qla4_8xxx_stop_firmware(ha);
536
537         ql4_printk(KERN_INFO, ha, "Initializing firmware..\n");
538         if (qla4xxx_initialize_fw_cb(ha) == QLA_ERROR) {
539                 DEBUG2(printk("scsi%ld: %s: Failed to initialize firmware "
540                               "control block\n", ha->host_no, __func__));
541                 return status;
542         }
543
544         if (!qla4xxx_fw_ready(ha))
545                 return status;
546
547         if (is_qla80XX(ha) && !test_bit(AF_INIT_DONE, &ha->flags))
548                 qla4xxx_alloc_fw_dump(ha);
549
550         return qla4xxx_get_firmware_status(ha);
551 }
552
553 static void qla4xxx_set_model_info(struct scsi_qla_host *ha)
554 {
555         uint16_t board_id_string[8];
556         int i;
557         int size = sizeof(ha->nvram->isp4022.boardIdStr);
558         int offset = offsetof(struct eeprom_data, isp4022.boardIdStr) / 2;
559
560         for (i = 0; i < (size / 2) ; i++) {
561                 board_id_string[i] = rd_nvram_word(ha, offset);
562                 offset += 1;
563         }
564
565         memcpy(ha->model_name, board_id_string, size);
566 }
567
568 static int qla4xxx_config_nvram(struct scsi_qla_host *ha)
569 {
570         unsigned long flags;
571         union external_hw_config_reg extHwConfig;
572
573         DEBUG2(printk("scsi%ld: %s: Get EEProm parameters \n", ha->host_no,
574                       __func__));
575         if (ql4xxx_lock_flash(ha) != QLA_SUCCESS)
576                 return QLA_ERROR;
577         if (ql4xxx_lock_nvram(ha) != QLA_SUCCESS) {
578                 ql4xxx_unlock_flash(ha);
579                 return QLA_ERROR;
580         }
581
582         /* Get EEPRom Parameters from NVRAM and validate */
583         ql4_printk(KERN_INFO, ha, "Configuring NVRAM ...\n");
584         if (qla4xxx_is_nvram_configuration_valid(ha) == QLA_SUCCESS) {
585                 spin_lock_irqsave(&ha->hardware_lock, flags);
586                 extHwConfig.Asuint32_t =
587                         rd_nvram_word(ha, eeprom_ext_hw_conf_offset(ha));
588                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
589         } else {
590                 ql4_printk(KERN_WARNING, ha,
591                     "scsi%ld: %s: EEProm checksum invalid.  "
592                     "Please update your EEPROM\n", ha->host_no,
593                     __func__);
594
595                 /* Attempt to set defaults */
596                 if (is_qla4010(ha))
597                         extHwConfig.Asuint32_t = 0x1912;
598                 else if (is_qla4022(ha) | is_qla4032(ha))
599                         extHwConfig.Asuint32_t = 0x0023;
600                 else
601                         return QLA_ERROR;
602         }
603
604         if (is_qla4022(ha) || is_qla4032(ha))
605                 qla4xxx_set_model_info(ha);
606         else
607                 strcpy(ha->model_name, "QLA4010");
608
609         DEBUG(printk("scsi%ld: %s: Setting extHwConfig to 0xFFFF%04x\n",
610                      ha->host_no, __func__, extHwConfig.Asuint32_t));
611
612         spin_lock_irqsave(&ha->hardware_lock, flags);
613         writel((0xFFFF << 16) | extHwConfig.Asuint32_t, isp_ext_hw_conf(ha));
614         readl(isp_ext_hw_conf(ha));
615         spin_unlock_irqrestore(&ha->hardware_lock, flags);
616
617         ql4xxx_unlock_nvram(ha);
618         ql4xxx_unlock_flash(ha);
619
620         return QLA_SUCCESS;
621 }
622
623 /**
624  * qla4_8xxx_pci_config() - Setup ISP82xx PCI configuration registers.
625  * @ha: HA context
626  */
627 void qla4_8xxx_pci_config(struct scsi_qla_host *ha)
628 {
629         pci_set_master(ha->pdev);
630 }
631
632 void qla4xxx_pci_config(struct scsi_qla_host *ha)
633 {
634         uint16_t w;
635         int status;
636
637         ql4_printk(KERN_INFO, ha, "Configuring PCI space...\n");
638
639         pci_set_master(ha->pdev);
640         status = pci_set_mwi(ha->pdev);
641         /*
642          * We want to respect framework's setting of PCI configuration space
643          * command register and also want to make sure that all bits of
644          * interest to us are properly set in command register.
645          */
646         pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
647         w |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
648         w &= ~PCI_COMMAND_INTX_DISABLE;
649         pci_write_config_word(ha->pdev, PCI_COMMAND, w);
650 }
651
652 static int qla4xxx_start_firmware_from_flash(struct scsi_qla_host *ha)
653 {
654         int status = QLA_ERROR;
655         unsigned long max_wait_time;
656         unsigned long flags;
657         uint32_t mbox_status;
658
659         ql4_printk(KERN_INFO, ha, "Starting firmware ...\n");
660
661         /*
662          * Start firmware from flash ROM
663          *
664          * WORKAROUND: Stuff a non-constant value that the firmware can
665          * use as a seed for a random number generator in MB7 prior to
666          * setting BOOT_ENABLE.  Fixes problem where the TCP
667          * connections use the same TCP ports after each reboot,
668          * causing some connections to not get re-established.
669          */
670         DEBUG(printk("scsi%d: %s: Start firmware from flash ROM\n",
671                      ha->host_no, __func__));
672
673         spin_lock_irqsave(&ha->hardware_lock, flags);
674         writel(jiffies, &ha->reg->mailbox[7]);
675         if (is_qla4022(ha) | is_qla4032(ha))
676                 writel(set_rmask(NVR_WRITE_ENABLE),
677                        &ha->reg->u1.isp4022.nvram);
678
679         writel(2, &ha->reg->mailbox[6]);
680         readl(&ha->reg->mailbox[6]);
681
682         writel(set_rmask(CSR_BOOT_ENABLE), &ha->reg->ctrl_status);
683         readl(&ha->reg->ctrl_status);
684         spin_unlock_irqrestore(&ha->hardware_lock, flags);
685
686         /* Wait for firmware to come UP. */
687         DEBUG2(printk(KERN_INFO "scsi%ld: %s: Wait up to %d seconds for "
688                       "boot firmware to complete...\n",
689                       ha->host_no, __func__, FIRMWARE_UP_TOV));
690         max_wait_time = jiffies + (FIRMWARE_UP_TOV * HZ);
691         do {
692                 uint32_t ctrl_status;
693
694                 spin_lock_irqsave(&ha->hardware_lock, flags);
695                 ctrl_status = readw(&ha->reg->ctrl_status);
696                 mbox_status = readw(&ha->reg->mailbox[0]);
697                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
698
699                 if (ctrl_status & set_rmask(CSR_SCSI_PROCESSOR_INTR))
700                         break;
701                 if (mbox_status == MBOX_STS_COMMAND_COMPLETE)
702                         break;
703
704                 DEBUG2(printk(KERN_INFO "scsi%ld: %s: Waiting for boot "
705                     "firmware to complete... ctrl_sts=0x%x, remaining=%ld\n",
706                     ha->host_no, __func__, ctrl_status, max_wait_time));
707
708                 msleep_interruptible(250);
709         } while (!time_after_eq(jiffies, max_wait_time));
710
711         if (mbox_status == MBOX_STS_COMMAND_COMPLETE) {
712                 DEBUG(printk(KERN_INFO "scsi%ld: %s: Firmware has started\n",
713                              ha->host_no, __func__));
714
715                 spin_lock_irqsave(&ha->hardware_lock, flags);
716                 writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
717                        &ha->reg->ctrl_status);
718                 readl(&ha->reg->ctrl_status);
719                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
720
721                 status = QLA_SUCCESS;
722         } else {
723                 printk(KERN_INFO "scsi%ld: %s: Boot firmware failed "
724                        "-  mbox status 0x%x\n", ha->host_no, __func__,
725                        mbox_status);
726                 status = QLA_ERROR;
727         }
728         return status;
729 }
730
731 int ql4xxx_lock_drvr_wait(struct scsi_qla_host *a)
732 {
733 #define QL4_LOCK_DRVR_WAIT      60
734 #define QL4_LOCK_DRVR_SLEEP     1
735
736         int drvr_wait = QL4_LOCK_DRVR_WAIT;
737         while (drvr_wait) {
738                 if (ql4xxx_lock_drvr(a) == 0) {
739                         ssleep(QL4_LOCK_DRVR_SLEEP);
740                         if (drvr_wait) {
741                                 DEBUG2(printk("scsi%ld: %s: Waiting for "
742                                               "Global Init Semaphore(%d)...\n",
743                                               a->host_no,
744                                               __func__, drvr_wait));
745                         }
746                         drvr_wait -= QL4_LOCK_DRVR_SLEEP;
747                 } else {
748                         DEBUG2(printk("scsi%ld: %s: Global Init Semaphore "
749                                       "acquired\n", a->host_no, __func__));
750                         return QLA_SUCCESS;
751                 }
752         }
753         return QLA_ERROR;
754 }
755
756 /**
757  * qla4xxx_start_firmware - starts qla4xxx firmware
758  * @ha: Pointer to host adapter structure.
759  *
760  * This routine performs the necessary steps to start the firmware for
761  * the QLA4010 adapter.
762  **/
763 int qla4xxx_start_firmware(struct scsi_qla_host *ha)
764 {
765         unsigned long flags = 0;
766         uint32_t mbox_status;
767         int status = QLA_ERROR;
768         int soft_reset = 1;
769         int config_chip = 0;
770
771         if (is_qla4022(ha) | is_qla4032(ha))
772                 ql4xxx_set_mac_number(ha);
773
774         if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
775                 return QLA_ERROR;
776
777         spin_lock_irqsave(&ha->hardware_lock, flags);
778
779         DEBUG2(printk("scsi%ld: %s: port_ctrl   = 0x%08X\n", ha->host_no,
780                       __func__, readw(isp_port_ctrl(ha))));
781         DEBUG(printk("scsi%ld: %s: port_status = 0x%08X\n", ha->host_no,
782                      __func__, readw(isp_port_status(ha))));
783
784         /* Is Hardware already initialized? */
785         if ((readw(isp_port_ctrl(ha)) & 0x8000) != 0) {
786                 DEBUG(printk("scsi%ld: %s: Hardware has already been "
787                              "initialized\n", ha->host_no, __func__));
788
789                 /* Receive firmware boot acknowledgement */
790                 mbox_status = readw(&ha->reg->mailbox[0]);
791
792                 DEBUG2(printk("scsi%ld: %s: H/W Config complete - mbox[0]= "
793                               "0x%x\n", ha->host_no, __func__, mbox_status));
794
795                 /* Is firmware already booted? */
796                 if (mbox_status == 0) {
797                         /* F/W not running, must be config by net driver */
798                         config_chip = 1;
799                         soft_reset = 0;
800                 } else {
801                         writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
802                                &ha->reg->ctrl_status);
803                         readl(&ha->reg->ctrl_status);
804                         writel(set_rmask(CSR_SCSI_COMPLETION_INTR),
805                                &ha->reg->ctrl_status);
806                         readl(&ha->reg->ctrl_status);
807                         spin_unlock_irqrestore(&ha->hardware_lock, flags);
808                         if (qla4xxx_get_firmware_state(ha) == QLA_SUCCESS) {
809                                 DEBUG2(printk("scsi%ld: %s: Get firmware "
810                                               "state -- state = 0x%x\n",
811                                               ha->host_no,
812                                               __func__, ha->firmware_state));
813                                 /* F/W is running */
814                                 if (ha->firmware_state &
815                                     FW_STATE_CONFIG_WAIT) {
816                                         DEBUG2(printk("scsi%ld: %s: Firmware "
817                                                       "in known state -- "
818                                                       "config and "
819                                                       "boot, state = 0x%x\n",
820                                                       ha->host_no, __func__,
821                                                       ha->firmware_state));
822                                         config_chip = 1;
823                                         soft_reset = 0;
824                                 }
825                         } else {
826                                 DEBUG2(printk("scsi%ld: %s: Firmware in "
827                                               "unknown state -- resetting,"
828                                               " state = "
829                                               "0x%x\n", ha->host_no, __func__,
830                                               ha->firmware_state));
831                         }
832                         spin_lock_irqsave(&ha->hardware_lock, flags);
833                 }
834         } else {
835                 DEBUG(printk("scsi%ld: %s: H/W initialization hasn't been "
836                              "started - resetting\n", ha->host_no, __func__));
837         }
838         spin_unlock_irqrestore(&ha->hardware_lock, flags);
839
840         DEBUG(printk("scsi%ld: %s: Flags soft_rest=%d, config= %d\n ",
841                      ha->host_no, __func__, soft_reset, config_chip));
842         if (soft_reset) {
843                 DEBUG(printk("scsi%ld: %s: Issue Soft Reset\n", ha->host_no,
844                              __func__));
845                 status = qla4xxx_soft_reset(ha);        /* NOTE: acquires drvr
846                                                          * lock again, but ok */
847                 if (status == QLA_ERROR) {
848                         DEBUG(printk("scsi%d: %s: Soft Reset failed!\n",
849                                      ha->host_no, __func__));
850                         ql4xxx_unlock_drvr(ha);
851                         return QLA_ERROR;
852                 }
853                 config_chip = 1;
854
855                 /* Reset clears the semaphore, so acquire again */
856                 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
857                         return QLA_ERROR;
858         }
859
860         if (config_chip) {
861                 if ((status = qla4xxx_config_nvram(ha)) == QLA_SUCCESS)
862                         status = qla4xxx_start_firmware_from_flash(ha);
863         }
864
865         ql4xxx_unlock_drvr(ha);
866         if (status == QLA_SUCCESS) {
867                 if (test_and_clear_bit(AF_GET_CRASH_RECORD, &ha->flags))
868                         qla4xxx_get_crash_record(ha);
869         } else {
870                 DEBUG(printk("scsi%ld: %s: Firmware has NOT started\n",
871                              ha->host_no, __func__));
872         }
873         return status;
874 }
875 /**
876  * qla4xxx_free_ddb_index - Free DDBs reserved by firmware
877  * @ha: pointer to adapter structure
878  *
879  * Since firmware is not running in autoconnect mode the DDB indices should
880  * be freed so that when login happens from user space there are free DDB
881  * indices available.
882  **/
883 void qla4xxx_free_ddb_index(struct scsi_qla_host *ha)
884 {
885         int max_ddbs;
886         int ret;
887         uint32_t idx = 0, next_idx = 0;
888         uint32_t state = 0, conn_err = 0;
889
890         max_ddbs =  is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
891                                      MAX_DEV_DB_ENTRIES;
892
893         for (idx = 0; idx < max_ddbs; idx = next_idx) {
894                 ret = qla4xxx_get_fwddb_entry(ha, idx, NULL, 0, NULL,
895                                               &next_idx, &state, &conn_err,
896                                                 NULL, NULL);
897                 if (ret == QLA_ERROR) {
898                         next_idx++;
899                         continue;
900                 }
901                 if (state == DDB_DS_NO_CONNECTION_ACTIVE ||
902                     state == DDB_DS_SESSION_FAILED) {
903                         DEBUG2(ql4_printk(KERN_INFO, ha,
904                                           "Freeing DDB index = 0x%x\n", idx));
905                         ret = qla4xxx_clear_ddb_entry(ha, idx);
906                         if (ret == QLA_ERROR)
907                                 ql4_printk(KERN_ERR, ha,
908                                            "Unable to clear DDB index = "
909                                            "0x%x\n", idx);
910                 }
911                 if (next_idx == 0)
912                         break;
913         }
914 }
915
916 /**
917  * qla4xxx_initialize_adapter - initiailizes hba
918  * @ha: Pointer to host adapter structure.
919  *
920  * This routine parforms all of the steps necessary to initialize the adapter.
921  *
922  **/
923 int qla4xxx_initialize_adapter(struct scsi_qla_host *ha, int is_reset)
924 {
925         int status = QLA_ERROR;
926
927         ha->eeprom_cmd_data = 0;
928
929         ql4_printk(KERN_INFO, ha, "Configuring PCI space...\n");
930         ha->isp_ops->pci_config(ha);
931
932         ha->isp_ops->disable_intrs(ha);
933
934         /* Initialize the Host adapter request/response queues and firmware */
935         if (ha->isp_ops->start_firmware(ha) == QLA_ERROR)
936                 goto exit_init_hba;
937
938         if (qla4xxx_about_firmware(ha) == QLA_ERROR)
939                 goto exit_init_hba;
940
941         if (ha->isp_ops->get_sys_info(ha) == QLA_ERROR)
942                 goto exit_init_hba;
943
944         if (qla4xxx_init_local_data(ha) == QLA_ERROR)
945                 goto exit_init_hba;
946
947         status = qla4xxx_init_firmware(ha);
948         if (status == QLA_ERROR)
949                 goto exit_init_hba;
950
951         if (is_reset == RESET_ADAPTER)
952                 qla4xxx_build_ddb_list(ha, is_reset);
953
954         set_bit(AF_ONLINE, &ha->flags);
955 exit_init_hba:
956         if (is_qla80XX(ha) && (status == QLA_ERROR)) {
957                 /* Since interrupts are registered in start_firmware for
958                  * 80XX, release them here if initialize_adapter fails */
959                 qla4xxx_free_irqs(ha);
960         }
961
962         DEBUG2(printk("scsi%ld: initialize adapter: %s\n", ha->host_no,
963             status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
964         return status;
965 }
966
967 int qla4xxx_ddb_change(struct scsi_qla_host *ha, uint32_t fw_ddb_index,
968                        struct ddb_entry *ddb_entry, uint32_t state)
969 {
970         uint32_t old_fw_ddb_device_state;
971         int status = QLA_ERROR;
972
973         old_fw_ddb_device_state = ddb_entry->fw_ddb_device_state;
974         DEBUG2(ql4_printk(KERN_INFO, ha,
975                           "%s: DDB - old state = 0x%x, new state = 0x%x for "
976                           "index [%d]\n", __func__,
977                           ddb_entry->fw_ddb_device_state, state, fw_ddb_index));
978
979         ddb_entry->fw_ddb_device_state = state;
980
981         switch (old_fw_ddb_device_state) {
982         case DDB_DS_LOGIN_IN_PROCESS:
983                 switch (state) {
984                 case DDB_DS_SESSION_ACTIVE:
985                 case DDB_DS_DISCOVERY:
986                         qla4xxx_update_session_conn_param(ha, ddb_entry);
987                         ddb_entry->unblock_sess(ddb_entry->sess);
988                         status = QLA_SUCCESS;
989                         break;
990                 case DDB_DS_SESSION_FAILED:
991                 case DDB_DS_NO_CONNECTION_ACTIVE:
992                         iscsi_conn_login_event(ddb_entry->conn,
993                                                ISCSI_CONN_STATE_FREE);
994                         status = QLA_SUCCESS;
995                         break;
996                 }
997                 break;
998         case DDB_DS_SESSION_ACTIVE:
999         case DDB_DS_DISCOVERY:
1000                 switch (state) {
1001                 case DDB_DS_SESSION_FAILED:
1002                         /*
1003                          * iscsi_session failure  will cause userspace to
1004                          * stop the connection which in turn would block the
1005                          * iscsi_session and start relogin
1006                          */
1007                         iscsi_session_failure(ddb_entry->sess->dd_data,
1008                                               ISCSI_ERR_CONN_FAILED);
1009                         status = QLA_SUCCESS;
1010                         break;
1011                 case DDB_DS_NO_CONNECTION_ACTIVE:
1012                         clear_bit(fw_ddb_index, ha->ddb_idx_map);
1013                         status = QLA_SUCCESS;
1014                         break;
1015                 }
1016                 break;
1017         case DDB_DS_SESSION_FAILED:
1018                 switch (state) {
1019                 case DDB_DS_SESSION_ACTIVE:
1020                 case DDB_DS_DISCOVERY:
1021                         ddb_entry->unblock_sess(ddb_entry->sess);
1022                         qla4xxx_update_session_conn_param(ha, ddb_entry);
1023                         status = QLA_SUCCESS;
1024                         break;
1025                 case DDB_DS_SESSION_FAILED:
1026                         iscsi_session_failure(ddb_entry->sess->dd_data,
1027                                               ISCSI_ERR_CONN_FAILED);
1028                         status = QLA_SUCCESS;
1029                         break;
1030                 }
1031                 break;
1032         default:
1033                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Unknown Event\n",
1034                                 __func__));
1035                 break;
1036         }
1037         return status;
1038 }
1039
1040 void qla4xxx_arm_relogin_timer(struct ddb_entry *ddb_entry)
1041 {
1042         /*
1043          * This triggers a relogin.  After the relogin_timer
1044          * expires, the relogin gets scheduled.  We must wait a
1045          * minimum amount of time since receiving an 0x8014 AEN
1046          * with failed device_state or a logout response before
1047          * we can issue another relogin.
1048          *
1049          * Firmware pads this timeout: (time2wait +1).
1050          * Driver retry to login should be longer than F/W.
1051          * Otherwise F/W will fail
1052          * set_ddb() mbx cmd with 0x4005 since it still
1053          * counting down its time2wait.
1054          */
1055         atomic_set(&ddb_entry->relogin_timer, 0);
1056         atomic_set(&ddb_entry->retry_relogin_timer,
1057                    ddb_entry->default_time2wait + 4);
1058
1059 }
1060
1061 int qla4xxx_flash_ddb_change(struct scsi_qla_host *ha, uint32_t fw_ddb_index,
1062                              struct ddb_entry *ddb_entry, uint32_t state)
1063 {
1064         uint32_t old_fw_ddb_device_state;
1065         int status = QLA_ERROR;
1066
1067         old_fw_ddb_device_state = ddb_entry->fw_ddb_device_state;
1068         DEBUG2(ql4_printk(KERN_INFO, ha,
1069                           "%s: DDB - old state = 0x%x, new state = 0x%x for "
1070                           "index [%d]\n", __func__,
1071                           ddb_entry->fw_ddb_device_state, state, fw_ddb_index));
1072
1073         ddb_entry->fw_ddb_device_state = state;
1074
1075         switch (old_fw_ddb_device_state) {
1076         case DDB_DS_LOGIN_IN_PROCESS:
1077         case DDB_DS_NO_CONNECTION_ACTIVE:
1078                 switch (state) {
1079                 case DDB_DS_SESSION_ACTIVE:
1080                         ddb_entry->unblock_sess(ddb_entry->sess);
1081                         qla4xxx_update_session_conn_fwddb_param(ha, ddb_entry);
1082                         status = QLA_SUCCESS;
1083                         break;
1084                 case DDB_DS_SESSION_FAILED:
1085                         iscsi_block_session(ddb_entry->sess);
1086                         if (!test_bit(DF_RELOGIN, &ddb_entry->flags))
1087                                 qla4xxx_arm_relogin_timer(ddb_entry);
1088                         status = QLA_SUCCESS;
1089                         break;
1090                 }
1091                 break;
1092         case DDB_DS_SESSION_ACTIVE:
1093                 switch (state) {
1094                 case DDB_DS_SESSION_FAILED:
1095                         iscsi_block_session(ddb_entry->sess);
1096                         if (!test_bit(DF_RELOGIN, &ddb_entry->flags))
1097                                 qla4xxx_arm_relogin_timer(ddb_entry);
1098                         status = QLA_SUCCESS;
1099                         break;
1100                 }
1101                 break;
1102         case DDB_DS_SESSION_FAILED:
1103                 switch (state) {
1104                 case DDB_DS_SESSION_ACTIVE:
1105                         ddb_entry->unblock_sess(ddb_entry->sess);
1106                         qla4xxx_update_session_conn_fwddb_param(ha, ddb_entry);
1107                         status = QLA_SUCCESS;
1108                         break;
1109                 case DDB_DS_SESSION_FAILED:
1110                         if (!test_bit(DF_RELOGIN, &ddb_entry->flags))
1111                                 qla4xxx_arm_relogin_timer(ddb_entry);
1112                         status = QLA_SUCCESS;
1113                         break;
1114                 }
1115                 break;
1116         default:
1117                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Unknown Event\n",
1118                                   __func__));
1119                 break;
1120         }
1121         return status;
1122 }
1123
1124 /**
1125  * qla4xxx_process_ddb_changed - process ddb state change
1126  * @ha - Pointer to host adapter structure.
1127  * @fw_ddb_index - Firmware's device database index
1128  * @state - Device state
1129  *
1130  * This routine processes a Decive Database Changed AEN Event.
1131  **/
1132 int qla4xxx_process_ddb_changed(struct scsi_qla_host *ha,
1133                                 uint32_t fw_ddb_index,
1134                                 uint32_t state, uint32_t conn_err)
1135 {
1136         struct ddb_entry *ddb_entry;
1137         int status = QLA_ERROR;
1138
1139         /* check for out of range index */
1140         if (fw_ddb_index >= MAX_DDB_ENTRIES)
1141                 goto exit_ddb_event;
1142
1143         /* Get the corresponging ddb entry */
1144         ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, fw_ddb_index);
1145         /* Device does not currently exist in our database. */
1146         if (ddb_entry == NULL) {
1147                 ql4_printk(KERN_ERR, ha, "%s: No ddb_entry at FW index [%d]\n",
1148                            __func__, fw_ddb_index);
1149
1150                 if (state == DDB_DS_NO_CONNECTION_ACTIVE)
1151                         clear_bit(fw_ddb_index, ha->ddb_idx_map);
1152
1153                 goto exit_ddb_event;
1154         }
1155
1156         ddb_entry->ddb_change(ha, fw_ddb_index, ddb_entry, state);
1157
1158 exit_ddb_event:
1159         return status;
1160 }
1161
1162 /**
1163  * qla4xxx_login_flash_ddb - Login to target (DDB)
1164  * @cls_session: Pointer to the session to login
1165  *
1166  * This routine logins to the target.
1167  * Issues setddb and conn open mbx
1168  **/
1169 void qla4xxx_login_flash_ddb(struct iscsi_cls_session *cls_session)
1170 {
1171         struct iscsi_session *sess;
1172         struct ddb_entry *ddb_entry;
1173         struct scsi_qla_host *ha;
1174         struct dev_db_entry *fw_ddb_entry = NULL;
1175         dma_addr_t fw_ddb_dma;
1176         uint32_t mbx_sts = 0;
1177         int ret;
1178
1179         sess = cls_session->dd_data;
1180         ddb_entry = sess->dd_data;
1181         ha =  ddb_entry->ha;
1182
1183         if (!test_bit(AF_LINK_UP, &ha->flags))
1184                 return;
1185
1186         if (ddb_entry->ddb_type != FLASH_DDB) {
1187                 DEBUG2(ql4_printk(KERN_INFO, ha,
1188                                   "Skipping login to non FLASH DB"));
1189                 goto exit_login;
1190         }
1191
1192         fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
1193                                       &fw_ddb_dma);
1194         if (fw_ddb_entry == NULL) {
1195                 DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
1196                 goto exit_login;
1197         }
1198
1199         if (ddb_entry->fw_ddb_index == INVALID_ENTRY) {
1200                 ret = qla4xxx_get_ddb_index(ha, &ddb_entry->fw_ddb_index);
1201                 if (ret == QLA_ERROR)
1202                         goto exit_login;
1203
1204                 ha->fw_ddb_index_map[ddb_entry->fw_ddb_index] = ddb_entry;
1205                 ha->tot_ddbs++;
1206         }
1207
1208         memcpy(fw_ddb_entry, &ddb_entry->fw_ddb_entry,
1209                sizeof(struct dev_db_entry));
1210         ddb_entry->sess->target_id = ddb_entry->fw_ddb_index;
1211
1212         ret = qla4xxx_set_ddb_entry(ha, ddb_entry->fw_ddb_index,
1213                                     fw_ddb_dma, &mbx_sts);
1214         if (ret == QLA_ERROR) {
1215                 DEBUG2(ql4_printk(KERN_ERR, ha, "Set DDB failed\n"));
1216                 goto exit_login;
1217         }
1218
1219         ddb_entry->fw_ddb_device_state = DDB_DS_LOGIN_IN_PROCESS;
1220         ret = qla4xxx_conn_open(ha, ddb_entry->fw_ddb_index);
1221         if (ret == QLA_ERROR) {
1222                 ql4_printk(KERN_ERR, ha, "%s: Login failed: %s\n", __func__,
1223                            sess->targetname);
1224                 goto exit_login;
1225         }
1226
1227 exit_login:
1228         if (fw_ddb_entry)
1229                 dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
1230 }
1231