2 * QLogic iSCSI HBA Driver
3 * Copyright (c) 2003-2006 QLogic Corporation
5 * See LICENSE.qla4xxx for copyright and licensing details.
11 #include "ql4_inline.h"
13 static struct ddb_entry * qla4xxx_alloc_ddb(struct scsi_qla_host *ha,
14 uint32_t fw_ddb_index);
16 static void ql4xxx_set_mac_number(struct scsi_qla_host *ha)
22 /* Get the function number */
23 spin_lock_irqsave(&ha->hardware_lock, flags);
24 value = readw(&ha->reg->ctrl_status);
25 spin_unlock_irqrestore(&ha->hardware_lock, flags);
27 func_number = (uint8_t) ((value >> 4) & 0x30);
28 switch (value & ISP_CONTROL_FN_MASK) {
29 case ISP_CONTROL_FN0_SCSI:
32 case ISP_CONTROL_FN1_SCSI:
36 DEBUG2(printk("scsi%ld: %s: Invalid function number, "
37 "ispControlStatus = 0x%x\n", ha->host_no,
41 DEBUG2(printk("scsi%ld: %s: mac_index %d.\n", ha->host_no, __func__,
46 * qla4xxx_free_ddb - deallocate ddb
47 * @ha: pointer to host adapter structure.
48 * @ddb_entry: pointer to device database entry
50 * This routine deallocates and unlinks the specified ddb_entry from the
53 static void qla4xxx_free_ddb(struct scsi_qla_host *ha,
54 struct ddb_entry *ddb_entry)
56 /* Remove device entry from list */
57 list_del_init(&ddb_entry->list);
59 /* Remove device pointer from index mapping arrays */
60 ha->fw_ddb_index_map[ddb_entry->fw_ddb_index] =
61 (struct ddb_entry *) INVALID_ENTRY;
64 /* Free memory and scsi-ml struct for device entry */
65 qla4xxx_destroy_sess(ddb_entry);
69 * qla4xxx_free_ddb_list - deallocate all ddbs
70 * @ha: pointer to host adapter structure.
72 * This routine deallocates and removes all devices on the sppecified adapter.
74 void qla4xxx_free_ddb_list(struct scsi_qla_host *ha)
76 struct list_head *ptr;
77 struct ddb_entry *ddb_entry;
79 while (!list_empty(&ha->ddb_list)) {
80 ptr = ha->ddb_list.next;
81 /* Free memory for device entry and remove */
82 ddb_entry = list_entry(ptr, struct ddb_entry, list);
83 qla4xxx_free_ddb(ha, ddb_entry);
88 * qla4xxx_init_rings - initialize hw queues
89 * @ha: pointer to host adapter structure.
91 * This routine initializes the internal queues for the specified adapter.
92 * The QLA4010 requires us to restart the queues at index 0.
93 * The QLA4000 doesn't care, so just default to QLA4010's requirement.
95 int qla4xxx_init_rings(struct scsi_qla_host *ha)
97 unsigned long flags = 0;
99 /* Initialize request queue. */
100 spin_lock_irqsave(&ha->hardware_lock, flags);
103 ha->request_ptr = &ha->request_ring[ha->request_in];
104 ha->req_q_count = REQUEST_QUEUE_DEPTH;
106 /* Initialize response queue. */
108 ha->response_out = 0;
109 ha->response_ptr = &ha->response_ring[ha->response_out];
112 * Initialize DMA Shadow registers. The firmware is really supposed to
113 * take care of this, but on some uniprocessor systems, the shadow
114 * registers aren't cleared-- causing the interrupt_handler to think
115 * there are responses to be processed when there aren't.
117 ha->shadow_regs->req_q_out = __constant_cpu_to_le32(0);
118 ha->shadow_regs->rsp_q_in = __constant_cpu_to_le32(0);
121 writel(0, &ha->reg->req_q_in);
122 writel(0, &ha->reg->rsp_q_out);
123 readl(&ha->reg->rsp_q_out);
125 spin_unlock_irqrestore(&ha->hardware_lock, flags);
131 * qla4xxx_validate_mac_address - validate adapter MAC address(es)
132 * @ha: pointer to host adapter structure.
135 static int qla4xxx_validate_mac_address(struct scsi_qla_host *ha)
137 struct flash_sys_info *sys_info;
138 dma_addr_t sys_info_dma;
139 int status = QLA_ERROR;
141 sys_info = dma_alloc_coherent(&ha->pdev->dev, sizeof(*sys_info),
142 &sys_info_dma, GFP_KERNEL);
143 if (sys_info == NULL) {
144 DEBUG2(printk("scsi%ld: %s: Unable to allocate dma buffer.\n",
145 ha->host_no, __func__));
147 goto exit_validate_mac_no_free;
149 memset(sys_info, 0, sizeof(*sys_info));
151 /* Get flash sys info */
152 if (qla4xxx_get_flash(ha, sys_info_dma, FLASH_OFFSET_SYS_INFO,
153 sizeof(*sys_info)) != QLA_SUCCESS) {
154 DEBUG2(printk("scsi%ld: %s: get_flash FLASH_OFFSET_SYS_INFO "
155 "failed\n", ha->host_no, __func__));
157 goto exit_validate_mac;
160 /* Save M.A.C. address & serial_number */
161 memcpy(ha->my_mac, &sys_info->physAddr[0].address[0],
162 min(sizeof(ha->my_mac),
163 sizeof(sys_info->physAddr[0].address)));
164 memcpy(ha->serial_number, &sys_info->acSerialNumber,
165 min(sizeof(ha->serial_number),
166 sizeof(sys_info->acSerialNumber)));
168 status = QLA_SUCCESS;
171 dma_free_coherent(&ha->pdev->dev, sizeof(*sys_info), sys_info,
174 exit_validate_mac_no_free:
179 * qla4xxx_init_local_data - initialize adapter specific local data
180 * @ha: pointer to host adapter structure.
183 static int qla4xxx_init_local_data(struct scsi_qla_host *ha)
185 /* Initilize aen queue */
186 ha->aen_q_count = MAX_AEN_ENTRIES;
188 return qla4xxx_get_firmware_status(ha);
191 static int qla4xxx_fw_ready(struct scsi_qla_host *ha)
193 uint32_t timeout_count;
196 DEBUG2(dev_info(&ha->pdev->dev, "Waiting for Firmware Ready..\n"));
197 for (timeout_count = ADAPTER_INIT_TOV; timeout_count > 0;
199 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
200 qla4xxx_get_dhcp_ip_address(ha);
202 /* Get firmware state. */
203 if (qla4xxx_get_firmware_state(ha) != QLA_SUCCESS) {
204 DEBUG2(printk("scsi%ld: %s: unable to get firmware "
205 "state\n", ha->host_no, __func__));
210 if (ha->firmware_state & FW_STATE_ERROR) {
211 DEBUG2(printk("scsi%ld: %s: an unrecoverable error has"
212 " occurred\n", ha->host_no, __func__));
216 if (ha->firmware_state & FW_STATE_CONFIG_WAIT) {
218 * The firmware has not yet been issued an Initialize
219 * Firmware command, so issue it now.
221 if (qla4xxx_initialize_fw_cb(ha) == QLA_ERROR)
224 /* Go back and test for ready state - no wait. */
228 if (ha->firmware_state == FW_STATE_READY) {
229 DEBUG2(dev_info(&ha->pdev->dev, "Firmware Ready..\n"));
230 /* The firmware is ready to process SCSI commands. */
231 DEBUG2(dev_info(&ha->pdev->dev,
232 "scsi%ld: %s: MEDIA TYPE - %s\n",
234 __func__, (ha->addl_fw_state &
235 FW_ADDSTATE_OPTICAL_MEDIA)
236 != 0 ? "OPTICAL" : "COPPER"));
237 DEBUG2(dev_info(&ha->pdev->dev,
238 "scsi%ld: %s: DHCP STATE Enabled "
240 ha->host_no, __func__,
242 FW_ADDSTATE_DHCP_ENABLED) != 0 ?
244 DEBUG2(dev_info(&ha->pdev->dev,
245 "scsi%ld: %s: LINK %s\n",
246 ha->host_no, __func__,
248 FW_ADDSTATE_LINK_UP) != 0 ?
250 DEBUG2(dev_info(&ha->pdev->dev,
251 "scsi%ld: %s: iSNS Service "
253 ha->host_no, __func__,
255 FW_ADDSTATE_ISNS_SVC_ENABLED) != 0 ?
261 DEBUG2(printk("scsi%ld: %s: waiting on fw, state=%x:%x - "
262 "seconds expired= %d\n", ha->host_no, __func__,
263 ha->firmware_state, ha->addl_fw_state,
265 if (is_qla4032(ha) &&
266 !(ha->addl_fw_state & FW_ADDSTATE_LINK_UP) &&
267 (timeout_count < ADAPTER_INIT_TOV - 5)) {
274 if (timeout_count == 0)
275 DEBUG2(printk("scsi%ld: %s: FW Initialization timed out!\n",
276 ha->host_no, __func__));
278 if (ha->firmware_state & FW_STATE_DHCP_IN_PROGRESS) {
279 DEBUG2(printk("scsi%ld: %s: FW is reporting its waiting to"
280 " grab an IP address from DHCP server\n",
281 ha->host_no, __func__));
289 * qla4xxx_init_firmware - initializes the firmware.
290 * @ha: pointer to host adapter structure.
293 static int qla4xxx_init_firmware(struct scsi_qla_host *ha)
295 int status = QLA_ERROR;
297 dev_info(&ha->pdev->dev, "Initializing firmware..\n");
298 if (qla4xxx_initialize_fw_cb(ha) == QLA_ERROR) {
299 DEBUG2(printk("scsi%ld: %s: Failed to initialize firmware "
300 "control block\n", ha->host_no, __func__));
303 if (!qla4xxx_fw_ready(ha))
306 return qla4xxx_get_firmware_status(ha);
309 static struct ddb_entry* qla4xxx_get_ddb_entry(struct scsi_qla_host *ha,
310 uint32_t fw_ddb_index,
313 struct dev_db_entry *fw_ddb_entry = NULL;
314 dma_addr_t fw_ddb_entry_dma;
315 struct ddb_entry *ddb_entry = NULL;
317 uint32_t device_state;
320 /* Make sure the dma buffer is valid */
321 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev,
322 sizeof(*fw_ddb_entry),
323 &fw_ddb_entry_dma, GFP_KERNEL);
324 if (fw_ddb_entry == NULL) {
325 DEBUG2(printk("scsi%ld: %s: Unable to allocate dma buffer.\n",
326 ha->host_no, __func__));
330 if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index, fw_ddb_entry,
331 fw_ddb_entry_dma, NULL, NULL,
332 &device_state, NULL, NULL, NULL) ==
334 DEBUG2(printk("scsi%ld: %s: failed get_ddb_entry for "
335 "fw_ddb_index %d\n", ha->host_no, __func__,
340 /* Allocate DDB if not already allocated. */
341 DEBUG2(printk("scsi%ld: %s: Looking for ddb[%d]\n", ha->host_no,
342 __func__, fw_ddb_index));
343 list_for_each_entry(ddb_entry, &ha->ddb_list, list) {
344 if (memcmp(ddb_entry->iscsi_name, fw_ddb_entry->iscsi_name,
345 ISCSI_NAME_SIZE) == 0) {
352 DEBUG2(printk("scsi%ld: %s: ddb[%d] not found - allocating "
353 "new ddb\n", ha->host_no, __func__,
356 ddb_entry = qla4xxx_alloc_ddb(ha, fw_ddb_index);
359 /* if not found allocate new ddb */
360 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry), fw_ddb_entry,
367 * qla4xxx_update_ddb_entry - update driver's internal ddb
368 * @ha: pointer to host adapter structure.
369 * @ddb_entry: pointer to device database structure to be filled
370 * @fw_ddb_index: index of the ddb entry in fw ddb table
372 * This routine updates the driver's internal device database entry
373 * with information retrieved from the firmware's device database
374 * entry for the specified device. The ddb_entry->fw_ddb_index field
375 * must be initialized prior to calling this routine
378 static int qla4xxx_update_ddb_entry(struct scsi_qla_host *ha,
379 struct ddb_entry *ddb_entry,
380 uint32_t fw_ddb_index)
382 struct dev_db_entry *fw_ddb_entry = NULL;
383 dma_addr_t fw_ddb_entry_dma;
384 int status = QLA_ERROR;
386 if (ddb_entry == NULL) {
387 DEBUG2(printk("scsi%ld: %s: ddb_entry is NULL\n", ha->host_no,
389 goto exit_update_ddb;
392 /* Make sure the dma buffer is valid */
393 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev,
394 sizeof(*fw_ddb_entry),
395 &fw_ddb_entry_dma, GFP_KERNEL);
396 if (fw_ddb_entry == NULL) {
397 DEBUG2(printk("scsi%ld: %s: Unable to allocate dma buffer.\n",
398 ha->host_no, __func__));
400 goto exit_update_ddb;
403 if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index, fw_ddb_entry,
404 fw_ddb_entry_dma, NULL, NULL,
405 &ddb_entry->fw_ddb_device_state, NULL,
406 &ddb_entry->tcp_source_port_num,
407 &ddb_entry->connection_id) ==
409 DEBUG2(printk("scsi%ld: %s: failed get_ddb_entry for "
410 "fw_ddb_index %d\n", ha->host_no, __func__,
413 goto exit_update_ddb;
416 status = QLA_SUCCESS;
417 ddb_entry->target_session_id = le16_to_cpu(fw_ddb_entry->tsid);
418 ddb_entry->task_mgmt_timeout =
419 le16_to_cpu(fw_ddb_entry->def_timeout);
420 ddb_entry->CmdSn = 0;
421 ddb_entry->exe_throttle = le16_to_cpu(fw_ddb_entry->exec_throttle);
422 ddb_entry->default_relogin_timeout =
423 le16_to_cpu(fw_ddb_entry->def_timeout);
424 ddb_entry->default_time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
426 /* Update index in case it changed */
427 ddb_entry->fw_ddb_index = fw_ddb_index;
428 ha->fw_ddb_index_map[fw_ddb_index] = ddb_entry;
430 ddb_entry->port = le16_to_cpu(fw_ddb_entry->port);
431 ddb_entry->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
432 memcpy(&ddb_entry->iscsi_name[0], &fw_ddb_entry->iscsi_name[0],
433 min(sizeof(ddb_entry->iscsi_name),
434 sizeof(fw_ddb_entry->iscsi_name)));
435 memcpy(&ddb_entry->ip_addr[0], &fw_ddb_entry->ip_addr[0],
436 min(sizeof(ddb_entry->ip_addr), sizeof(fw_ddb_entry->ip_addr)));
438 DEBUG2(printk("scsi%ld: %s: ddb[%d] - State= %x status= %d.\n",
439 ha->host_no, __func__, fw_ddb_index,
440 ddb_entry->fw_ddb_device_state, status));
444 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
445 fw_ddb_entry, fw_ddb_entry_dma);
451 * qla4xxx_alloc_ddb - allocate device database entry
452 * @ha: Pointer to host adapter structure.
453 * @fw_ddb_index: Firmware's device database index
455 * This routine allocates a ddb_entry, ititializes some values, and
456 * inserts it into the ddb list.
458 static struct ddb_entry * qla4xxx_alloc_ddb(struct scsi_qla_host *ha,
459 uint32_t fw_ddb_index)
461 struct ddb_entry *ddb_entry;
463 DEBUG2(printk("scsi%ld: %s: fw_ddb_index [%d]\n", ha->host_no,
464 __func__, fw_ddb_index));
466 ddb_entry = qla4xxx_alloc_sess(ha);
467 if (ddb_entry == NULL) {
468 DEBUG2(printk("scsi%ld: %s: Unable to allocate memory "
469 "to add fw_ddb_index [%d]\n",
470 ha->host_no, __func__, fw_ddb_index));
474 ddb_entry->fw_ddb_index = fw_ddb_index;
475 atomic_set(&ddb_entry->port_down_timer, ha->port_down_retry_count);
476 atomic_set(&ddb_entry->retry_relogin_timer, INVALID_ENTRY);
477 atomic_set(&ddb_entry->relogin_timer, 0);
478 atomic_set(&ddb_entry->relogin_retry_count, 0);
479 atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
480 list_add_tail(&ddb_entry->list, &ha->ddb_list);
481 ha->fw_ddb_index_map[fw_ddb_index] = ddb_entry;
488 * qla4xxx_configure_ddbs - builds driver ddb list
489 * @ha: Pointer to host adapter structure.
491 * This routine searches for all valid firmware ddb entries and builds
492 * an internal ddb list. Ddbs that are considered valid are those with
493 * a device state of SESSION_ACTIVE.
495 static int qla4xxx_build_ddb_list(struct scsi_qla_host *ha)
497 int status = QLA_SUCCESS;
498 uint32_t fw_ddb_index = 0;
499 uint32_t next_fw_ddb_index = 0;
501 uint32_t conn_err, err_code;
502 struct ddb_entry *ddb_entry;
505 dev_info(&ha->pdev->dev, "Initializing DDBs ...\n");
506 for (fw_ddb_index = 0; fw_ddb_index < MAX_DDB_ENTRIES;
507 fw_ddb_index = next_fw_ddb_index) {
508 /* First, let's see if a device exists here */
509 if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index, NULL, 0, NULL,
510 &next_fw_ddb_index, &ddb_state,
511 &conn_err, NULL, NULL) ==
513 DEBUG2(printk("scsi%ld: %s: get_ddb_entry, "
514 "fw_ddb_index %d failed", ha->host_no,
515 __func__, fw_ddb_index));
519 DEBUG2(printk("scsi%ld: %s: Getting DDB[%d] ddbstate=0x%x, "
520 "next_fw_ddb_index=%d.\n", ha->host_no, __func__,
521 fw_ddb_index, ddb_state, next_fw_ddb_index));
523 /* Issue relogin, if necessary. */
524 if (ddb_state == DDB_DS_SESSION_FAILED ||
525 ddb_state == DDB_DS_NO_CONNECTION_ACTIVE) {
526 /* Try and login to device */
527 DEBUG2(printk("scsi%ld: %s: Login to DDB[%d]\n",
528 ha->host_no, __func__, fw_ddb_index));
529 err_code = ((conn_err & 0x00ff0000) >> 16);
530 if (err_code == 0x1c || err_code == 0x06) {
531 DEBUG2(printk("scsi%ld: %s send target "
533 "or access denied failure\n",
534 ha->host_no, __func__));
536 qla4xxx_set_ddb_entry(ha, fw_ddb_index, 0);
537 if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index,
538 NULL, 0, NULL, &next_fw_ddb_index,
539 &ddb_state, &conn_err, NULL, NULL)
541 DEBUG2(printk("scsi%ld: %s:"
542 "get_ddb_entry %d failed\n",
544 __func__, fw_ddb_index));
550 if (ddb_state != DDB_DS_SESSION_ACTIVE)
553 * if fw_ddb with session active state found,
556 DEBUG2(printk("scsi%ld: %s: DDB[%d] added to list\n",
557 ha->host_no, __func__, fw_ddb_index));
559 /* Add DDB to internal our ddb list. */
560 ddb_entry = qla4xxx_get_ddb_entry(ha, fw_ddb_index, &new_tgt);
561 if (ddb_entry == NULL) {
562 DEBUG2(printk("scsi%ld: %s: Unable to allocate memory "
563 "for device at fw_ddb_index %d\n",
564 ha->host_no, __func__, fw_ddb_index));
567 /* Fill in the device structure */
568 if (qla4xxx_update_ddb_entry(ha, ddb_entry, fw_ddb_index) ==
570 ha->fw_ddb_index_map[fw_ddb_index] =
571 (struct ddb_entry *)INVALID_ENTRY;
574 DEBUG2(printk("scsi%ld: %s: update_ddb_entry failed "
575 "for fw_ddb_index %d.\n",
576 ha->host_no, __func__, fw_ddb_index));
581 /* We know we've reached the last device when
582 * next_fw_ddb_index is 0 */
583 if (next_fw_ddb_index == 0)
587 dev_info(&ha->pdev->dev, "DDB list done..\n");
592 struct qla4_relog_scan {
596 uint32_t fw_ddb_index;
597 uint32_t next_fw_ddb_index;
598 uint32_t fw_ddb_device_state;
601 static int qla4_test_rdy(struct scsi_qla_host *ha, struct qla4_relog_scan *rs)
603 struct ddb_entry *ddb_entry;
606 * Don't want to do a relogin if connection
609 rs->err_code = ((rs->conn_err & 0x00ff0000) >> 16);
610 if (rs->err_code == 0x1c || rs->err_code == 0x06) {
612 "scsi%ld: %s send target"
614 "access denied failure\n",
615 ha->host_no, __func__));
617 /* We either have a device that is in
618 * the process of relogging in or a
619 * device that is waiting to be
623 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha,
625 if (ddb_entry == NULL)
628 if (ddb_entry->dev_scan_wait_to_start_relogin != 0
629 && time_after_eq(jiffies,
631 dev_scan_wait_to_start_relogin))
633 ddb_entry->dev_scan_wait_to_start_relogin = 0;
634 qla4xxx_set_ddb_entry(ha, rs->fw_ddb_index, 0);
640 static int qla4_scan_for_relogin(struct scsi_qla_host *ha,
641 struct qla4_relog_scan *rs)
646 * ----------------- */
647 for (rs->fw_ddb_index = 0; rs->fw_ddb_index < MAX_DDB_ENTRIES;
648 rs->fw_ddb_index = rs->next_fw_ddb_index) {
649 if (qla4xxx_get_fwddb_entry(ha, rs->fw_ddb_index, NULL, 0,
650 NULL, &rs->next_fw_ddb_index,
651 &rs->fw_ddb_device_state,
652 &rs->conn_err, NULL, NULL)
656 if (rs->fw_ddb_device_state == DDB_DS_LOGIN_IN_PROCESS)
659 if (rs->fw_ddb_device_state == DDB_DS_SESSION_FAILED ||
660 rs->fw_ddb_device_state == DDB_DS_NO_CONNECTION_ACTIVE) {
661 error = qla4_test_rdy(ha, rs);
666 /* We know we've reached the last device when
667 * next_fw_ddb_index is 0 */
668 if (rs->next_fw_ddb_index == 0)
675 * qla4xxx_devices_ready - wait for target devices to be logged in
676 * @ha: pointer to adapter structure
678 * This routine waits up to ql4xdiscoverywait seconds
679 * F/W database during driver load time.
681 static int qla4xxx_devices_ready(struct scsi_qla_host *ha)
684 unsigned long discovery_wtime;
685 struct qla4_relog_scan rs;
687 discovery_wtime = jiffies + (ql4xdiscoverywait * HZ);
689 DEBUG(printk("Waiting (%d) for devices ...\n", ql4xdiscoverywait));
692 qla4xxx_get_firmware_state(ha);
693 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags)) {
694 /* Set time-between-relogin timer */
695 qla4xxx_process_aen(ha, RELOGIN_DDB_CHANGED_AENS);
698 /* if no relogins active or needed, halt discvery wait */
701 error = qla4_scan_for_relogin(ha, &rs);
704 DEBUG2(printk("scsi%ld: %s: Delay halted. Devices "
705 "Ready.\n", ha->host_no, __func__));
710 } while (!time_after_eq(jiffies, discovery_wtime));
712 DEBUG3(qla4xxx_get_conn_event_log(ha));
717 static void qla4xxx_flush_AENS(struct scsi_qla_host *ha)
721 /* Flush the 0x8014 AEN from the firmware as a result of
722 * Auto connect. We are basically doing get_firmware_ddb()
723 * to determine whether we need to log back in or not.
724 * Trying to do a set ddb before we have processed 0x8014
725 * will result in another set_ddb() for the same ddb. In other
726 * words there will be stale entries in the aen_q.
728 wtime = jiffies + (2 * HZ);
730 if (qla4xxx_get_firmware_state(ha) == QLA_SUCCESS)
731 if (ha->firmware_state & (BIT_2 | BIT_0))
734 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
735 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
738 } while (!time_after_eq(jiffies, wtime));
742 static int qla4xxx_initialize_ddb_list(struct scsi_qla_host *ha)
744 uint16_t fw_ddb_index;
745 int status = QLA_SUCCESS;
747 /* free the ddb list if is not empty */
748 if (!list_empty(&ha->ddb_list))
749 qla4xxx_free_ddb_list(ha);
751 for (fw_ddb_index = 0; fw_ddb_index < MAX_DDB_ENTRIES; fw_ddb_index++)
752 ha->fw_ddb_index_map[fw_ddb_index] =
753 (struct ddb_entry *)INVALID_ENTRY;
757 qla4xxx_flush_AENS(ha);
760 * First perform device discovery for active
761 * fw ddb indexes and build
764 if ((status = qla4xxx_build_ddb_list(ha)) == QLA_ERROR)
767 /* Wait for an AEN */
768 qla4xxx_devices_ready(ha);
771 * Targets can come online after the inital discovery, so processing
772 * the aens here will catch them.
774 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
775 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
781 * qla4xxx_update_ddb_list - update the driver ddb list
782 * @ha: pointer to host adapter structure.
784 * This routine obtains device information from the F/W database after
785 * firmware or adapter resets. The device table is preserved.
787 int qla4xxx_reinitialize_ddb_list(struct scsi_qla_host *ha)
789 int status = QLA_SUCCESS;
790 struct ddb_entry *ddb_entry, *detemp;
792 /* Update the device information for all devices. */
793 list_for_each_entry_safe(ddb_entry, detemp, &ha->ddb_list, list) {
794 qla4xxx_update_ddb_entry(ha, ddb_entry,
795 ddb_entry->fw_ddb_index);
796 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
797 atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
798 DEBUG2(printk ("scsi%ld: %s: ddb index [%d] marked "
799 "ONLINE\n", ha->host_no, __func__,
800 ddb_entry->fw_ddb_index));
801 } else if (atomic_read(&ddb_entry->state) == DDB_STATE_ONLINE)
802 qla4xxx_mark_device_missing(ha, ddb_entry);
808 * qla4xxx_relogin_device - re-establish session
809 * @ha: Pointer to host adapter structure.
810 * @ddb_entry: Pointer to device database entry
812 * This routine does a session relogin with the specified device.
813 * The ddb entry must be assigned prior to making this call.
815 int qla4xxx_relogin_device(struct scsi_qla_host *ha,
816 struct ddb_entry * ddb_entry)
818 uint16_t relogin_timer;
820 relogin_timer = max(ddb_entry->default_relogin_timeout,
821 (uint16_t)RELOGIN_TOV);
822 atomic_set(&ddb_entry->relogin_timer, relogin_timer);
824 DEBUG2(printk("scsi%ld: Relogin index [%d]. TOV=%d\n", ha->host_no,
825 ddb_entry->fw_ddb_index, relogin_timer));
827 qla4xxx_set_ddb_entry(ha, ddb_entry->fw_ddb_index, 0);
832 static int qla4xxx_config_nvram(struct scsi_qla_host *ha)
835 union external_hw_config_reg extHwConfig;
837 DEBUG2(printk("scsi%ld: %s: Get EEProm parameters \n", ha->host_no,
839 if (ql4xxx_lock_flash(ha) != QLA_SUCCESS)
841 if (ql4xxx_lock_nvram(ha) != QLA_SUCCESS) {
842 ql4xxx_unlock_flash(ha);
846 /* Get EEPRom Parameters from NVRAM and validate */
847 dev_info(&ha->pdev->dev, "Configuring NVRAM ...\n");
848 if (qla4xxx_is_nvram_configuration_valid(ha) == QLA_SUCCESS) {
849 spin_lock_irqsave(&ha->hardware_lock, flags);
850 extHwConfig.Asuint32_t =
851 rd_nvram_word(ha, eeprom_ext_hw_conf_offset(ha));
852 spin_unlock_irqrestore(&ha->hardware_lock, flags);
855 * QLogic adapters should always have a valid NVRAM.
856 * If not valid, do not load.
858 dev_warn(&ha->pdev->dev,
859 "scsi%ld: %s: EEProm checksum invalid. "
860 "Please update your EEPROM\n", ha->host_no,
865 extHwConfig.Asuint32_t = 0x1912;
866 else if (is_qla4022(ha) | is_qla4032(ha))
867 extHwConfig.Asuint32_t = 0x0023;
869 DEBUG(printk("scsi%ld: %s: Setting extHwConfig to 0xFFFF%04x\n",
870 ha->host_no, __func__, extHwConfig.Asuint32_t));
872 spin_lock_irqsave(&ha->hardware_lock, flags);
873 writel((0xFFFF << 16) | extHwConfig.Asuint32_t, isp_ext_hw_conf(ha));
874 readl(isp_ext_hw_conf(ha));
875 spin_unlock_irqrestore(&ha->hardware_lock, flags);
877 ql4xxx_unlock_nvram(ha);
878 ql4xxx_unlock_flash(ha);
880 return (QLA_SUCCESS);
883 static void qla4x00_pci_config(struct scsi_qla_host *ha)
888 dev_info(&ha->pdev->dev, "Configuring PCI space...\n");
890 pci_set_master(ha->pdev);
891 status = pci_set_mwi(ha->pdev);
893 * We want to respect framework's setting of PCI configuration space
894 * command register and also want to make sure that all bits of
895 * interest to us are properly set in command register.
897 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
898 w |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
899 w &= ~PCI_COMMAND_INTX_DISABLE;
900 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
903 static int qla4xxx_start_firmware_from_flash(struct scsi_qla_host *ha)
905 int status = QLA_ERROR;
906 uint32_t max_wait_time;
908 uint32_t mbox_status;
910 dev_info(&ha->pdev->dev, "Starting firmware ...\n");
913 * Start firmware from flash ROM
915 * WORKAROUND: Stuff a non-constant value that the firmware can
916 * use as a seed for a random number generator in MB7 prior to
917 * setting BOOT_ENABLE. Fixes problem where the TCP
918 * connections use the same TCP ports after each reboot,
919 * causing some connections to not get re-established.
921 DEBUG(printk("scsi%d: %s: Start firmware from flash ROM\n",
922 ha->host_no, __func__));
924 spin_lock_irqsave(&ha->hardware_lock, flags);
925 writel(jiffies, &ha->reg->mailbox[7]);
926 if (is_qla4022(ha) | is_qla4032(ha))
927 writel(set_rmask(NVR_WRITE_ENABLE),
928 &ha->reg->u1.isp4022.nvram);
930 writel(2, &ha->reg->mailbox[6]);
931 readl(&ha->reg->mailbox[6]);
933 writel(set_rmask(CSR_BOOT_ENABLE), &ha->reg->ctrl_status);
934 readl(&ha->reg->ctrl_status);
935 spin_unlock_irqrestore(&ha->hardware_lock, flags);
937 /* Wait for firmware to come UP. */
938 max_wait_time = FIRMWARE_UP_TOV * 4;
940 uint32_t ctrl_status;
942 spin_lock_irqsave(&ha->hardware_lock, flags);
943 ctrl_status = readw(&ha->reg->ctrl_status);
944 mbox_status = readw(&ha->reg->mailbox[0]);
945 spin_unlock_irqrestore(&ha->hardware_lock, flags);
947 if (ctrl_status & set_rmask(CSR_SCSI_PROCESSOR_INTR))
949 if (mbox_status == MBOX_STS_COMMAND_COMPLETE)
952 DEBUG2(printk("scsi%ld: %s: Waiting for boot firmware to "
953 "complete... ctrl_sts=0x%x, remaining=%d\n",
954 ha->host_no, __func__, ctrl_status,
958 } while ((max_wait_time--));
960 if (mbox_status == MBOX_STS_COMMAND_COMPLETE) {
961 DEBUG(printk("scsi%ld: %s: Firmware has started\n",
962 ha->host_no, __func__));
964 spin_lock_irqsave(&ha->hardware_lock, flags);
965 writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
966 &ha->reg->ctrl_status);
967 readl(&ha->reg->ctrl_status);
968 spin_unlock_irqrestore(&ha->hardware_lock, flags);
970 status = QLA_SUCCESS;
972 printk(KERN_INFO "scsi%ld: %s: Boot firmware failed "
973 "- mbox status 0x%x\n", ha->host_no, __func__,
980 int ql4xxx_lock_drvr_wait(struct scsi_qla_host *a)
982 #define QL4_LOCK_DRVR_WAIT 60
983 #define QL4_LOCK_DRVR_SLEEP 1
985 int drvr_wait = QL4_LOCK_DRVR_WAIT;
987 if (ql4xxx_lock_drvr(a) == 0) {
988 ssleep(QL4_LOCK_DRVR_SLEEP);
990 DEBUG2(printk("scsi%ld: %s: Waiting for "
991 "Global Init Semaphore(%d)...\n",
993 __func__, drvr_wait));
995 drvr_wait -= QL4_LOCK_DRVR_SLEEP;
997 DEBUG2(printk("scsi%ld: %s: Global Init Semaphore "
998 "acquired\n", a->host_no, __func__));
1006 * qla4xxx_start_firmware - starts qla4xxx firmware
1007 * @ha: Pointer to host adapter structure.
1009 * This routine performs the necessary steps to start the firmware for
1010 * the QLA4010 adapter.
1012 static int qla4xxx_start_firmware(struct scsi_qla_host *ha)
1014 unsigned long flags = 0;
1015 uint32_t mbox_status;
1016 int status = QLA_ERROR;
1018 int config_chip = 0;
1020 if (is_qla4022(ha) | is_qla4032(ha))
1021 ql4xxx_set_mac_number(ha);
1023 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
1026 spin_lock_irqsave(&ha->hardware_lock, flags);
1028 DEBUG2(printk("scsi%ld: %s: port_ctrl = 0x%08X\n", ha->host_no,
1029 __func__, readw(isp_port_ctrl(ha))));
1030 DEBUG(printk("scsi%ld: %s: port_status = 0x%08X\n", ha->host_no,
1031 __func__, readw(isp_port_status(ha))));
1033 /* Is Hardware already initialized? */
1034 if ((readw(isp_port_ctrl(ha)) & 0x8000) != 0) {
1035 DEBUG(printk("scsi%ld: %s: Hardware has already been "
1036 "initialized\n", ha->host_no, __func__));
1038 /* Receive firmware boot acknowledgement */
1039 mbox_status = readw(&ha->reg->mailbox[0]);
1041 DEBUG2(printk("scsi%ld: %s: H/W Config complete - mbox[0]= "
1042 "0x%x\n", ha->host_no, __func__, mbox_status));
1044 /* Is firmware already booted? */
1045 if (mbox_status == 0) {
1046 /* F/W not running, must be config by net driver */
1050 writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
1051 &ha->reg->ctrl_status);
1052 readl(&ha->reg->ctrl_status);
1053 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1054 if (qla4xxx_get_firmware_state(ha) == QLA_SUCCESS) {
1055 DEBUG2(printk("scsi%ld: %s: Get firmware "
1056 "state -- state = 0x%x\n",
1058 __func__, ha->firmware_state));
1059 /* F/W is running */
1060 if (ha->firmware_state &
1061 FW_STATE_CONFIG_WAIT) {
1062 DEBUG2(printk("scsi%ld: %s: Firmware "
1063 "in known state -- "
1065 "boot, state = 0x%x\n",
1066 ha->host_no, __func__,
1067 ha->firmware_state));
1072 DEBUG2(printk("scsi%ld: %s: Firmware in "
1073 "unknown state -- resetting,"
1075 "0x%x\n", ha->host_no, __func__,
1076 ha->firmware_state));
1078 spin_lock_irqsave(&ha->hardware_lock, flags);
1081 DEBUG(printk("scsi%ld: %s: H/W initialization hasn't been "
1082 "started - resetting\n", ha->host_no, __func__));
1084 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1086 DEBUG(printk("scsi%ld: %s: Flags soft_rest=%d, config= %d\n ",
1087 ha->host_no, __func__, soft_reset, config_chip));
1089 DEBUG(printk("scsi%ld: %s: Issue Soft Reset\n", ha->host_no,
1091 status = qla4xxx_soft_reset(ha);
1092 if (status == QLA_ERROR) {
1093 DEBUG(printk("scsi%d: %s: Soft Reset failed!\n",
1094 ha->host_no, __func__));
1095 ql4xxx_unlock_drvr(ha);
1100 /* Reset clears the semaphore, so aquire again */
1101 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
1106 if ((status = qla4xxx_config_nvram(ha)) == QLA_SUCCESS)
1107 status = qla4xxx_start_firmware_from_flash(ha);
1110 ql4xxx_unlock_drvr(ha);
1111 if (status == QLA_SUCCESS) {
1112 qla4xxx_get_fw_version(ha);
1113 if (test_and_clear_bit(AF_GET_CRASH_RECORD, &ha->flags))
1114 qla4xxx_get_crash_record(ha);
1116 DEBUG(printk("scsi%ld: %s: Firmware has NOT started\n",
1117 ha->host_no, __func__));
1124 * qla4xxx_initialize_adapter - initiailizes hba
1125 * @ha: Pointer to host adapter structure.
1126 * @renew_ddb_list: Indicates what to do with the adapter's ddb list
1127 * after adapter recovery has completed.
1128 * 0=preserve ddb list, 1=destroy and rebuild ddb list
1130 * This routine parforms all of the steps necessary to initialize the adapter.
1133 int qla4xxx_initialize_adapter(struct scsi_qla_host *ha,
1134 uint8_t renew_ddb_list)
1136 int status = QLA_ERROR;
1137 int8_t ip_address[IP_ADDR_LEN] = {0} ;
1139 ha->eeprom_cmd_data = 0;
1141 qla4x00_pci_config(ha);
1143 qla4xxx_disable_intrs(ha);
1145 /* Initialize the Host adapter request/response queues and firmware */
1146 if (qla4xxx_start_firmware(ha) == QLA_ERROR)
1149 if (qla4xxx_validate_mac_address(ha) == QLA_ERROR)
1152 if (qla4xxx_init_local_data(ha) == QLA_ERROR)
1155 status = qla4xxx_init_firmware(ha);
1156 if (status == QLA_ERROR)
1160 * FW is waiting to get an IP address from DHCP server: Skip building
1161 * the ddb_list and wait for DHCP lease acquired aen to come in
1162 * followed by 0x8014 aen" to trigger the tgt discovery process.
1164 if (ha->firmware_state & FW_STATE_DHCP_IN_PROGRESS)
1165 goto exit_init_online;
1167 /* Skip device discovery if ip and subnet is zero */
1168 if (memcmp(ha->ip_address, ip_address, IP_ADDR_LEN) == 0 ||
1169 memcmp(ha->subnet_mask, ip_address, IP_ADDR_LEN) == 0)
1170 goto exit_init_online;
1172 if (renew_ddb_list == PRESERVE_DDB_LIST) {
1174 * We want to preserve lun states (i.e. suspended, etc.)
1175 * for recovery initiated by the driver. So just update
1176 * the device states for the existing ddb_list.
1178 qla4xxx_reinitialize_ddb_list(ha);
1179 } else if (renew_ddb_list == REBUILD_DDB_LIST) {
1181 * We want to build the ddb_list from scratch during
1182 * driver initialization and recovery initiated by the
1183 * INT_HBA_RESET IOCTL.
1185 status = qla4xxx_initialize_ddb_list(ha);
1186 if (status == QLA_ERROR) {
1187 DEBUG2(printk("%s(%ld) Error occurred during build"
1188 "ddb list\n", __func__, ha->host_no));
1193 if (!ha->tot_ddbs) {
1194 DEBUG2(printk("scsi%ld: Failed to initialize devices or none "
1195 "present in Firmware device database\n",
1200 set_bit(AF_ONLINE, &ha->flags);
1206 * qla4xxx_add_device_dynamically - ddb addition due to an AEN
1207 * @ha: Pointer to host adapter structure.
1208 * @fw_ddb_index: Firmware's device database index
1210 * This routine processes adds a device as a result of an 8014h AEN.
1212 static void qla4xxx_add_device_dynamically(struct scsi_qla_host *ha,
1213 uint32_t fw_ddb_index)
1215 struct ddb_entry * ddb_entry;
1218 /* First allocate a device structure */
1219 ddb_entry = qla4xxx_get_ddb_entry(ha, fw_ddb_index, &new_tgt);
1220 if (ddb_entry == NULL) {
1221 DEBUG2(printk(KERN_WARNING
1222 "scsi%ld: Unable to allocate memory to add "
1223 "fw_ddb_index %d\n", ha->host_no, fw_ddb_index));
1227 if (!new_tgt && (ddb_entry->fw_ddb_index != fw_ddb_index)) {
1228 /* Target has been bound to a new fw_ddb_index */
1229 qla4xxx_free_ddb(ha, ddb_entry);
1230 ddb_entry = qla4xxx_alloc_ddb(ha, fw_ddb_index);
1231 if (ddb_entry == NULL) {
1232 DEBUG2(printk(KERN_WARNING
1233 "scsi%ld: Unable to allocate memory"
1234 " to add fw_ddb_index %d\n",
1235 ha->host_no, fw_ddb_index));
1239 if (qla4xxx_update_ddb_entry(ha, ddb_entry, fw_ddb_index) ==
1241 ha->fw_ddb_index_map[fw_ddb_index] =
1242 (struct ddb_entry *)INVALID_ENTRY;
1243 DEBUG2(printk(KERN_WARNING
1244 "scsi%ld: failed to add new device at index "
1245 "[%d]\n Unable to retrieve fw ddb entry\n",
1246 ha->host_no, fw_ddb_index));
1247 qla4xxx_free_ddb(ha, ddb_entry);
1251 if (qla4xxx_add_sess(ddb_entry)) {
1252 DEBUG2(printk(KERN_WARNING
1253 "scsi%ld: failed to add new device at index "
1254 "[%d]\n Unable to add connection and session\n",
1255 ha->host_no, fw_ddb_index));
1256 qla4xxx_free_ddb(ha, ddb_entry);
1261 * qla4xxx_process_ddb_changed - process ddb state change
1262 * @ha - Pointer to host adapter structure.
1263 * @fw_ddb_index - Firmware's device database index
1264 * @state - Device state
1266 * This routine processes a Decive Database Changed AEN Event.
1268 int qla4xxx_process_ddb_changed(struct scsi_qla_host *ha,
1269 uint32_t fw_ddb_index, uint32_t state)
1271 struct ddb_entry * ddb_entry;
1272 uint32_t old_fw_ddb_device_state;
1274 /* check for out of range index */
1275 if (fw_ddb_index >= MAX_DDB_ENTRIES)
1278 /* Get the corresponging ddb entry */
1279 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, fw_ddb_index);
1280 /* Device does not currently exist in our database. */
1281 if (ddb_entry == NULL) {
1282 if (state == DDB_DS_SESSION_ACTIVE)
1283 qla4xxx_add_device_dynamically(ha, fw_ddb_index);
1287 /* Device already exists in our database. */
1288 old_fw_ddb_device_state = ddb_entry->fw_ddb_device_state;
1289 DEBUG2(printk("scsi%ld: %s DDB - old state= 0x%x, new state=0x%x for "
1290 "index [%d]\n", ha->host_no, __func__,
1291 ddb_entry->fw_ddb_device_state, state, fw_ddb_index));
1292 if (old_fw_ddb_device_state == state &&
1293 state == DDB_DS_SESSION_ACTIVE) {
1294 /* Do nothing, state not changed. */
1298 ddb_entry->fw_ddb_device_state = state;
1299 /* Device is back online. */
1300 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
1301 atomic_set(&ddb_entry->port_down_timer,
1302 ha->port_down_retry_count);
1303 atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
1304 atomic_set(&ddb_entry->relogin_retry_count, 0);
1305 atomic_set(&ddb_entry->relogin_timer, 0);
1306 clear_bit(DF_RELOGIN, &ddb_entry->flags);
1307 clear_bit(DF_NO_RELOGIN, &ddb_entry->flags);
1308 iscsi_if_create_session_done(ddb_entry->conn);
1310 * Change the lun state to READY in case the lun TIMEOUT before
1311 * the device came back.
1314 /* Device went away, try to relogin. */
1315 /* Mark device missing */
1316 if (atomic_read(&ddb_entry->state) == DDB_STATE_ONLINE)
1317 qla4xxx_mark_device_missing(ha, ddb_entry);
1319 * Relogin if device state changed to a not active state.
1320 * However, do not relogin if this aen is a result of an IOCTL
1321 * logout (DF_NO_RELOGIN) or if this is a discovered device.
1323 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_FAILED &&
1324 !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
1325 !test_bit(DF_NO_RELOGIN, &ddb_entry->flags) &&
1326 !test_bit(DF_ISNS_DISCOVERED, &ddb_entry->flags)) {
1328 * This triggers a relogin. After the relogin_timer
1329 * expires, the relogin gets scheduled. We must wait a
1330 * minimum amount of time since receiving an 0x8014 AEN
1331 * with failed device_state or a logout response before
1332 * we can issue another relogin.
1334 /* Firmware padds this timeout: (time2wait +1).
1335 * Driver retry to login should be longer than F/W.
1336 * Otherwise F/W will fail
1337 * set_ddb() mbx cmd with 0x4005 since it still
1338 * counting down its time2wait.
1340 atomic_set(&ddb_entry->relogin_timer, 0);
1341 atomic_set(&ddb_entry->retry_relogin_timer,
1342 ddb_entry->default_time2wait + 4);