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