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