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