2 * QLogic iSCSI HBA Driver
3 * Copyright (c) 2003-2006 QLogic Corporation
5 * See LICENSE.qla4xxx for copyright and licensing details.
11 * QLogic ISP4xxx Hardware Support Function Prototypes.
14 static void ql4xxx_set_mac_number(struct scsi_qla_host *ha)
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);
25 func_number = (uint8_t) ((value >> 4) & 0x30);
26 switch (value & ISP_CONTROL_FN_MASK) {
27 case ISP_CONTROL_FN0_SCSI:
30 case ISP_CONTROL_FN1_SCSI:
34 DEBUG2(printk("scsi%ld: %s: Invalid function number, "
35 "ispControlStatus = 0x%x\n", ha->host_no,
39 DEBUG2(printk("scsi%ld: %s: mac_index %d.\n", ha->host_no, __func__,
44 * qla4xxx_free_ddb - deallocate ddb
45 * @ha: pointer to host adapter structure.
46 * @ddb_entry: pointer to device database entry
48 * This routine deallocates and unlinks the specified ddb_entry from the
51 void qla4xxx_free_ddb(struct scsi_qla_host *ha, struct ddb_entry *ddb_entry)
53 /* Remove device entry from list */
54 list_del_init(&ddb_entry->list);
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;
61 /* Free memory and scsi-ml struct for device entry */
62 qla4xxx_destroy_sess(ddb_entry);
66 * qla4xxx_free_ddb_list - deallocate all ddbs
67 * @ha: pointer to host adapter structure.
69 * This routine deallocates and removes all devices on the sppecified adapter.
71 void qla4xxx_free_ddb_list(struct scsi_qla_host *ha)
73 struct list_head *ptr;
74 struct ddb_entry *ddb_entry;
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);
85 * qla4xxx_init_rings - initialize hw queues
86 * @ha: pointer to host adapter structure.
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.
92 int qla4xxx_init_rings(struct scsi_qla_host *ha)
94 unsigned long flags = 0;
96 /* Initialize request queue. */
97 spin_lock_irqsave(&ha->hardware_lock, flags);
100 ha->request_ptr = &ha->request_ring[ha->request_in];
101 ha->req_q_count = REQUEST_QUEUE_DEPTH;
103 /* Initialize response queue. */
105 ha->response_out = 0;
106 ha->response_ptr = &ha->response_ring[ha->response_out];
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.
114 ha->shadow_regs->req_q_out = __constant_cpu_to_le32(0);
115 ha->shadow_regs->rsp_q_in = __constant_cpu_to_le32(0);
118 writel(0, &ha->reg->req_q_in);
119 writel(0, &ha->reg->rsp_q_out);
120 readl(&ha->reg->rsp_q_out);
122 spin_unlock_irqrestore(&ha->hardware_lock, flags);
128 * qla4xxx_validate_mac_address - validate adapter MAC address(es)
129 * @ha: pointer to host adapter structure.
132 static int qla4xxx_validate_mac_address(struct scsi_qla_host *ha)
134 struct flash_sys_info *sys_info;
135 dma_addr_t sys_info_dma;
136 int status = QLA_ERROR;
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__));
144 goto exit_validate_mac_no_free;
146 memset(sys_info, 0, sizeof(*sys_info));
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__));
154 goto exit_validate_mac;
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)));
165 status = QLA_SUCCESS;
168 dma_free_coherent(&ha->pdev->dev, sizeof(*sys_info), sys_info,
171 exit_validate_mac_no_free:
176 * qla4xxx_init_local_data - initialize adapter specific local data
177 * @ha: pointer to host adapter structure.
180 static int qla4xxx_init_local_data(struct scsi_qla_host *ha)
182 /* Initilize aen queue */
183 ha->aen_q_count = MAX_AEN_ENTRIES;
185 return qla4xxx_get_firmware_status(ha);
188 static int qla4xxx_fw_ready(struct scsi_qla_host *ha)
190 uint32_t timeout_count;
193 DEBUG2(dev_info(&ha->pdev->dev, "Waiting for Firmware Ready..\n"));
194 for (timeout_count = ADAPTER_INIT_TOV; timeout_count > 0;
196 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
197 qla4xxx_get_dhcp_ip_address(ha);
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__));
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__));
213 if (ha->firmware_state & FW_STATE_CONFIG_WAIT) {
215 * The firmware has not yet been issued an Initialize
216 * Firmware command, so issue it now.
218 if (qla4xxx_initialize_fw_cb(ha) == QLA_ERROR)
221 /* Go back and test for ready state - no wait. */
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",
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 "
237 ha->host_no, __func__,
239 FW_ADDSTATE_DHCP_ENABLED) != 0 ?
241 DEBUG2(dev_info(&ha->pdev->dev,
242 "scsi%ld: %s: LINK %s\n",
243 ha->host_no, __func__,
245 FW_ADDSTATE_LINK_UP) != 0 ?
247 DEBUG2(dev_info(&ha->pdev->dev,
248 "scsi%ld: %s: iSNS Service "
250 ha->host_no, __func__,
252 FW_ADDSTATE_ISNS_SVC_ENABLED) != 0 ?
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,
262 if (is_qla4032(ha) &&
263 !(ha->addl_fw_state & FW_ADDSTATE_LINK_UP) &&
264 (timeout_count < ADAPTER_INIT_TOV - 5)) {
271 if (timeout_count == 0)
272 DEBUG2(printk("scsi%ld: %s: FW Initialization timed out!\n",
273 ha->host_no, __func__));
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__));
286 * qla4xxx_init_firmware - initializes the firmware.
287 * @ha: pointer to host adapter structure.
290 static int qla4xxx_init_firmware(struct scsi_qla_host *ha)
292 int status = QLA_ERROR;
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__));
300 if (!qla4xxx_fw_ready(ha))
303 set_bit(AF_ONLINE, &ha->flags);
304 return qla4xxx_get_firmware_status(ha);
307 static struct ddb_entry* qla4xxx_get_ddb_entry(struct scsi_qla_host *ha,
308 uint32_t fw_ddb_index)
310 struct dev_db_entry *fw_ddb_entry = NULL;
311 dma_addr_t fw_ddb_entry_dma;
312 struct ddb_entry *ddb_entry = NULL;
314 uint32_t device_state;
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__));
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) ==
330 DEBUG2(printk("scsi%ld: %s: failed get_ddb_entry for "
331 "fw_ddb_index %d\n", ha->host_no, __func__,
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) {
348 DEBUG2(printk("scsi%ld: %s: ddb[%d] not found - allocating "
349 "new ddb\n", ha->host_no, __func__,
351 ddb_entry = qla4xxx_alloc_ddb(ha, fw_ddb_index);
354 /* if not found allocate new ddb */
355 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry), fw_ddb_entry,
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
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
373 int qla4xxx_update_ddb_entry(struct scsi_qla_host *ha,
374 struct ddb_entry *ddb_entry,
375 uint32_t fw_ddb_index)
377 struct dev_db_entry *fw_ddb_entry = NULL;
378 dma_addr_t fw_ddb_entry_dma;
379 int status = QLA_ERROR;
381 if (ddb_entry == NULL) {
382 DEBUG2(printk("scsi%ld: %s: ddb_entry is NULL\n", ha->host_no,
384 goto exit_update_ddb;
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__));
395 goto exit_update_ddb;
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) ==
404 DEBUG2(printk("scsi%ld: %s: failed get_ddb_entry for "
405 "fw_ddb_index %d\n", ha->host_no, __func__,
408 goto exit_update_ddb;
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);
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;
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)));
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));
439 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
440 fw_ddb_entry, fw_ddb_entry_dma);
446 * qla4xxx_alloc_ddb - allocate device database entry
447 * @ha: Pointer to host adapter structure.
448 * @fw_ddb_index: Firmware's device database index
450 * This routine allocates a ddb_entry, ititializes some values, and
451 * inserts it into the ddb list.
453 struct ddb_entry * qla4xxx_alloc_ddb(struct scsi_qla_host *ha,
454 uint32_t fw_ddb_index)
456 struct ddb_entry *ddb_entry;
458 DEBUG2(printk("scsi%ld: %s: fw_ddb_index [%d]\n", ha->host_no,
459 __func__, fw_ddb_index));
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));
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;
483 * qla4xxx_configure_ddbs - builds driver ddb list
484 * @ha: Pointer to host adapter structure.
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.
490 static int qla4xxx_build_ddb_list(struct scsi_qla_host *ha)
492 int status = QLA_SUCCESS;
493 uint32_t fw_ddb_index = 0;
494 uint32_t next_fw_ddb_index = 0;
496 uint32_t conn_err, err_code;
497 struct ddb_entry *ddb_entry;
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) ==
507 DEBUG2(printk("scsi%ld: %s: get_ddb_entry, "
508 "fw_ddb_index %d failed", ha->host_no,
509 __func__, fw_ddb_index));
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));
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 "
527 "or access denied failure\n",
528 ha->host_no, __func__));
530 qla4xxx_set_ddb_entry(ha, fw_ddb_index, 0);
533 if (ddb_state != DDB_DS_SESSION_ACTIVE)
536 * if fw_ddb with session active state found,
539 DEBUG2(printk("scsi%ld: %s: DDB[%d] added to list\n",
540 ha->host_no, __func__, fw_ddb_index));
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));
550 /* Fill in the device structure */
551 if (qla4xxx_update_ddb_entry(ha, ddb_entry, fw_ddb_index) ==
553 ha->fw_ddb_index_map[fw_ddb_index] =
554 (struct ddb_entry *)INVALID_ENTRY;
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));
564 /* We know we've reached the last device when
565 * next_fw_ddb_index is 0 */
566 if (next_fw_ddb_index == 0)
570 dev_info(&ha->pdev->dev, "DDB list done..\n");
575 struct qla4_relog_scan {
579 uint32_t fw_ddb_index;
580 uint32_t next_fw_ddb_index;
581 uint32_t fw_ddb_device_state;
584 static int qla4_test_rdy(struct scsi_qla_host *ha, struct qla4_relog_scan *rs)
586 struct ddb_entry *ddb_entry;
589 * Don't want to do a relogin if connection
592 rs->err_code = ((rs->conn_err & 0x00ff0000) >> 16);
593 if (rs->err_code == 0x1c || rs->err_code == 0x06) {
595 "scsi%ld: %s send target"
597 "access denied failure\n",
598 ha->host_no, __func__));
600 /* We either have a device that is in
601 * the process of relogging in or a
602 * device that is waiting to be
606 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha,
608 if (ddb_entry == NULL)
611 if (ddb_entry->dev_scan_wait_to_start_relogin != 0
612 && time_after_eq(jiffies,
614 dev_scan_wait_to_start_relogin))
616 ddb_entry->dev_scan_wait_to_start_relogin = 0;
617 qla4xxx_set_ddb_entry(ha, rs->fw_ddb_index, 0);
623 static int qla4_scan_for_relogin(struct scsi_qla_host *ha,
624 struct qla4_relog_scan *rs)
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)
639 if (rs->fw_ddb_device_state == DDB_DS_LOGIN_IN_PROCESS)
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);
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)
658 * qla4xxx_devices_ready - wait for target devices to be logged in
659 * @ha: pointer to adapter structure
661 * This routine waits up to ql4xdiscoverywait seconds
662 * F/W database during driver load time.
664 static int qla4xxx_devices_ready(struct scsi_qla_host *ha)
667 unsigned long discovery_wtime;
668 struct qla4_relog_scan rs;
670 discovery_wtime = jiffies + (ql4xdiscoverywait * HZ);
672 DEBUG(printk("Waiting (%d) for devices ...\n", ql4xdiscoverywait));
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);
681 /* if no relogins active or needed, halt discvery wait */
684 error = qla4_scan_for_relogin(ha, &rs);
687 DEBUG2(printk("scsi%ld: %s: Delay halted. Devices "
688 "Ready.\n", ha->host_no, __func__));
693 } while (!time_after_eq(jiffies, discovery_wtime));
695 DEBUG3(qla4xxx_get_conn_event_log(ha));
700 static void qla4xxx_flush_AENS(struct scsi_qla_host *ha)
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.
711 wtime = jiffies + (2 * HZ);
713 if (qla4xxx_get_firmware_state(ha) == QLA_SUCCESS)
714 if (ha->firmware_state & (BIT_2 | BIT_0))
717 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
718 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
721 } while (!time_after_eq(jiffies, wtime));
725 static int qla4xxx_initialize_ddb_list(struct scsi_qla_host *ha)
727 uint16_t fw_ddb_index;
728 int status = QLA_SUCCESS;
730 /* free the ddb list if is not empty */
731 if (!list_empty(&ha->ddb_list))
732 qla4xxx_free_ddb_list(ha);
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;
740 qla4xxx_flush_AENS(ha);
743 * First perform device discovery for active
744 * fw ddb indexes and build
747 if ((status = qla4xxx_build_ddb_list(ha)) == QLA_ERROR)
750 /* Wait for an AEN */
751 qla4xxx_devices_ready(ha);
754 * Targets can come online after the inital discovery, so processing
755 * the aens here will catch them.
757 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
758 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
764 * qla4xxx_update_ddb_list - update the driver ddb list
765 * @ha: pointer to host adapter structure.
767 * This routine obtains device information from the F/W database after
768 * firmware or adapter resets. The device table is preserved.
770 int qla4xxx_reinitialize_ddb_list(struct scsi_qla_host *ha)
772 int status = QLA_SUCCESS;
773 struct ddb_entry *ddb_entry, *detemp;
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);
791 * qla4xxx_relogin_device - re-establish session
792 * @ha: Pointer to host adapter structure.
793 * @ddb_entry: Pointer to device database entry
795 * This routine does a session relogin with the specified device.
796 * The ddb entry must be assigned prior to making this call.
798 int qla4xxx_relogin_device(struct scsi_qla_host *ha,
799 struct ddb_entry * ddb_entry)
801 uint16_t relogin_timer;
803 relogin_timer = max(ddb_entry->default_relogin_timeout,
804 (uint16_t)RELOGIN_TOV);
805 atomic_set(&ddb_entry->relogin_timer, relogin_timer);
807 DEBUG2(printk("scsi%ld: Relogin index [%d]. TOV=%d\n", ha->host_no,
808 ddb_entry->fw_ddb_index, relogin_timer));
810 qla4xxx_set_ddb_entry(ha, ddb_entry->fw_ddb_index, 0);
815 static int qla4xxx_config_nvram(struct scsi_qla_host *ha)
818 union external_hw_config_reg extHwConfig;
820 DEBUG2(printk("scsi%ld: %s: Get EEProm parameters \n", ha->host_no,
822 if (ql4xxx_lock_flash(ha) != QLA_SUCCESS)
824 if (ql4xxx_lock_nvram(ha) != QLA_SUCCESS) {
825 ql4xxx_unlock_flash(ha);
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);
838 * QLogic adapters should always have a valid NVRAM.
839 * If not valid, do not load.
841 dev_warn(&ha->pdev->dev,
842 "scsi%ld: %s: EEProm checksum invalid. "
843 "Please update your EEPROM\n", ha->host_no,
848 extHwConfig.Asuint32_t = 0x1912;
849 else if (is_qla4022(ha) | is_qla4032(ha))
850 extHwConfig.Asuint32_t = 0x0023;
852 DEBUG(printk("scsi%ld: %s: Setting extHwConfig to 0xFFFF%04x\n",
853 ha->host_no, __func__, extHwConfig.Asuint32_t));
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);
860 ql4xxx_unlock_nvram(ha);
861 ql4xxx_unlock_flash(ha);
863 return (QLA_SUCCESS);
866 static void qla4x00_pci_config(struct scsi_qla_host *ha)
870 dev_info(&ha->pdev->dev, "Configuring PCI space...\n");
872 pci_set_master(ha->pdev);
874 if (pci_set_mwi(ha->pdev))
875 mwi = PCI_COMMAND_INVALIDATE;
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.
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);
887 static int qla4xxx_start_firmware_from_flash(struct scsi_qla_host *ha)
889 int status = QLA_ERROR;
890 uint32_t max_wait_time;
892 uint32_t mbox_status;
894 dev_info(&ha->pdev->dev, "Starting firmware ...\n");
897 * Start firmware from flash ROM
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.
905 DEBUG(printk("scsi%d: %s: Start firmware from flash ROM\n",
906 ha->host_no, __func__));
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);
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);
918 /* Wait for firmware to come UP. */
919 max_wait_time = FIRMWARE_UP_TOV * 4;
921 uint32_t ctrl_status;
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);
928 if (ctrl_status & set_rmask(CSR_SCSI_PROCESSOR_INTR))
930 if (mbox_status == MBOX_STS_COMMAND_COMPLETE)
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,
939 } while ((max_wait_time--));
941 if (mbox_status == MBOX_STS_COMMAND_COMPLETE) {
942 DEBUG(printk("scsi%ld: %s: Firmware has started\n",
943 ha->host_no, __func__));
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);
951 status = QLA_SUCCESS;
953 printk(KERN_INFO "scsi%ld: %s: Boot firmware failed "
954 "- mbox status 0x%x\n", ha->host_no, __func__,
961 int ql4xxx_lock_drvr_wait(struct scsi_qla_host *ha)
963 #define QL4_LOCK_DRVR_WAIT 30
964 #define QL4_LOCK_DRVR_SLEEP 1
966 int drvr_wait = QL4_LOCK_DRVR_WAIT;
968 if (ql4xxx_lock_drvr(ha) == 0) {
969 ssleep(QL4_LOCK_DRVR_SLEEP);
971 DEBUG2(printk("scsi%ld: %s: Waiting for "
972 "Global Init Semaphore(%d)...n",
974 __func__, drvr_wait));
976 drvr_wait -= QL4_LOCK_DRVR_SLEEP;
978 DEBUG2(printk("scsi%ld: %s: Global Init Semaphore "
979 "acquired.n", ha->host_no, __func__));
987 * qla4xxx_start_firmware - starts qla4xxx firmware
988 * @ha: Pointer to host adapter structure.
990 * This routine performs the neccessary steps to start the firmware for
991 * the QLA4010 adapter.
993 static int qla4xxx_start_firmware(struct scsi_qla_host *ha)
995 unsigned long flags = 0;
996 uint32_t mbox_status;
997 int status = QLA_ERROR;
1001 if (is_qla4022(ha) | is_qla4032(ha))
1002 ql4xxx_set_mac_number(ha);
1004 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
1007 spin_lock_irqsave(&ha->hardware_lock, flags);
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))));
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__));
1019 /* Receive firmware boot acknowledgement */
1020 mbox_status = readw(&ha->reg->mailbox[0]);
1022 DEBUG2(printk("scsi%ld: %s: H/W Config complete - mbox[0]= "
1023 "0x%x\n", ha->host_no, __func__, mbox_status));
1025 /* Is firmware already booted? */
1026 if (mbox_status == 0) {
1027 /* F/W not running, must be config by net driver */
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",
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 -- "
1046 "boot, state = 0x%x\n",
1047 ha->host_no, __func__,
1048 ha->firmware_state));
1053 DEBUG2(printk("scsi%ld: %s: Firmware in "
1054 "unknown state -- resetting,"
1056 "0x%x\n", ha->host_no, __func__,
1057 ha->firmware_state));
1059 spin_lock_irqsave(&ha->hardware_lock, flags);
1062 DEBUG(printk("scsi%ld: %s: H/W initialization hasn't been "
1063 "started - resetting\n", ha->host_no, __func__));
1065 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1067 DEBUG(printk("scsi%ld: %s: Flags soft_rest=%d, config= %d\n ",
1068 ha->host_no, __func__, soft_reset, config_chip));
1070 DEBUG(printk("scsi%ld: %s: Issue Soft Reset\n", ha->host_no,
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);
1081 /* Reset clears the semaphore, so aquire again */
1082 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
1087 if ((status = qla4xxx_config_nvram(ha)) == QLA_SUCCESS)
1088 status = qla4xxx_start_firmware_from_flash(ha);
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);
1097 DEBUG(printk("scsi%ld: %s: Firmware has NOT started\n",
1098 ha->host_no, __func__));
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
1111 * This routine parforms all of the steps necessary to initialize the adapter.
1114 int qla4xxx_initialize_adapter(struct scsi_qla_host *ha,
1115 uint8_t renew_ddb_list)
1117 int status = QLA_ERROR;
1118 int8_t ip_address[IP_ADDR_LEN] = {0} ;
1120 ha->eeprom_cmd_data = 0;
1122 qla4x00_pci_config(ha);
1124 qla4xxx_disable_intrs(ha);
1126 /* Initialize the Host adapter request/response queues and firmware */
1127 if (qla4xxx_start_firmware(ha) == QLA_ERROR)
1130 if (qla4xxx_validate_mac_address(ha) == QLA_ERROR)
1133 if (qla4xxx_init_local_data(ha) == QLA_ERROR)
1136 status = qla4xxx_init_firmware(ha);
1137 if (status == QLA_ERROR)
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.
1145 if (ha->firmware_state & FW_STATE_DHCP_IN_PROGRESS)
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)
1153 if (renew_ddb_list == PRESERVE_DDB_LIST) {
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.
1159 qla4xxx_reinitialize_ddb_list(ha);
1160 } else if (renew_ddb_list == REBUILD_DDB_LIST) {
1162 * We want to build the ddb_list from scratch during
1163 * driver initialization and recovery initiated by the
1164 * INT_HBA_RESET IOCTL.
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));
1174 if (!ha->tot_ddbs) {
1175 DEBUG2(printk("scsi%ld: Failed to initialize devices or none "
1176 "present in Firmware device database\n",
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
1190 * This routine processes adds a device as a result of an 8014h AEN.
1192 static void qla4xxx_add_device_dynamically(struct scsi_qla_host *ha,
1193 uint32_t fw_ddb_index)
1195 struct ddb_entry * ddb_entry;
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));
1206 if (qla4xxx_update_ddb_entry(ha, ddb_entry, fw_ddb_index) ==
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);
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);
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
1233 * This routine processes a Decive Database Changed AEN Event.
1235 int qla4xxx_process_ddb_changed(struct scsi_qla_host *ha,
1236 uint32_t fw_ddb_index, uint32_t state)
1238 struct ddb_entry * ddb_entry;
1239 uint32_t old_fw_ddb_device_state;
1241 /* check for out of range index */
1242 if (fw_ddb_index >= MAX_DDB_ENTRIES)
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);
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. */
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);
1277 * Change the lun state to READY in case the lun TIMEOUT before
1278 * the device came back.
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);
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.
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)) {
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.
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.
1307 atomic_set(&ddb_entry->relogin_timer, 0);
1308 atomic_set(&ddb_entry->retry_relogin_timer,
1309 ddb_entry->default_time2wait + 4);