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