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