blob: d5254054b2c84d5ed17a29282107c3fbc90675da [file] [log] [blame]
David Somayajuluafaf5a22006-09-19 10:28:00 -07001/*
2 * QLogic iSCSI HBA Driver
3 * Copyright (c) 2003-2006 QLogic Corporation
4 *
5 * See LICENSE.qla4xxx for copyright and licensing details.
6 */
7
Mike Christie26974782007-12-13 12:43:29 -06008#include <scsi/iscsi_if.h>
David Somayajuluafaf5a22006-09-19 10:28:00 -07009#include "ql4_def.h"
David C Somayajulu92b72732007-05-23 17:55:40 -070010#include "ql4_glbl.h"
11#include "ql4_dbg.h"
12#include "ql4_inline.h"
David Somayajuluafaf5a22006-09-19 10:28:00 -070013
Adrian Bunk47975472007-04-26 00:35:16 -070014static struct ddb_entry * qla4xxx_alloc_ddb(struct scsi_qla_host *ha,
15 uint32_t fw_ddb_index);
David Somayajuluafaf5a22006-09-19 10:28:00 -070016
17static 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 **/
Adrian Bunk47975472007-04-26 00:35:16 -070054static void qla4xxx_free_ddb(struct scsi_qla_host *ha,
55 struct ddb_entry *ddb_entry)
David Somayajuluafaf5a22006-09-19 10:28:00 -070056{
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 **/
75void 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 **/
96int 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 **/
136static 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 **/
184static 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
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530192static uint8_t
193qla4xxx_wait_for_ip_config(struct scsi_qla_host *ha)
194{
195 uint8_t ipv4_wait = 0;
196 uint8_t ipv6_wait = 0;
197 int8_t ip_address[IPv6_ADDR_LEN] = {0} ;
198
199 /* If both IPv4 & IPv6 are enabled, possibly only one
200 * IP address may be acquired, so check to see if we
201 * need to wait for another */
202 if (is_ipv4_enabled(ha) && is_ipv6_enabled(ha)) {
203 if (((ha->addl_fw_state & FW_ADDSTATE_DHCPv4_ENABLED) != 0) &&
204 ((ha->addl_fw_state &
205 FW_ADDSTATE_DHCPv4_LEASE_ACQUIRED) == 0)) {
206 ipv4_wait = 1;
207 }
208 if (((ha->ipv6_addl_options &
209 IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) != 0) &&
210 ((ha->ipv6_link_local_state == IP_ADDRSTATE_ACQUIRING) ||
211 (ha->ipv6_addr0_state == IP_ADDRSTATE_ACQUIRING) ||
212 (ha->ipv6_addr1_state == IP_ADDRSTATE_ACQUIRING))) {
213
214 ipv6_wait = 1;
215
216 if ((ha->ipv6_link_local_state ==
217 IP_ADDRSTATE_PREFERRED) ||
218 (ha->ipv6_addr0_state == IP_ADDRSTATE_PREFERRED) ||
219 (ha->ipv6_addr1_state == IP_ADDRSTATE_PREFERRED)) {
220 DEBUG2(printk(KERN_INFO "scsi%ld: %s: "
221 "Preferred IP configured."
222 " Don't wait!\n", ha->host_no,
223 __func__));
224 ipv6_wait = 0;
225 }
226 if (memcmp(&ha->ipv6_default_router_addr, ip_address,
227 IPv6_ADDR_LEN) == 0) {
228 DEBUG2(printk(KERN_INFO "scsi%ld: %s: "
229 "No Router configured. "
230 "Don't wait!\n", ha->host_no,
231 __func__));
232 ipv6_wait = 0;
233 }
234 if ((ha->ipv6_default_router_state ==
235 IPV6_RTRSTATE_MANUAL) &&
236 (ha->ipv6_link_local_state ==
237 IP_ADDRSTATE_TENTATIVE) &&
238 (memcmp(&ha->ipv6_link_local_addr,
239 &ha->ipv6_default_router_addr, 4) == 0)) {
240 DEBUG2(printk("scsi%ld: %s: LinkLocal Router & "
241 "IP configured. Don't wait!\n",
242 ha->host_no, __func__));
243 ipv6_wait = 0;
244 }
245 }
246 if (ipv4_wait || ipv6_wait) {
247 DEBUG2(printk("scsi%ld: %s: Wait for additional "
248 "IP(s) \"", ha->host_no, __func__));
249 if (ipv4_wait)
250 DEBUG2(printk("IPv4 "));
251 if (ha->ipv6_link_local_state == IP_ADDRSTATE_ACQUIRING)
252 DEBUG2(printk("IPv6LinkLocal "));
253 if (ha->ipv6_addr0_state == IP_ADDRSTATE_ACQUIRING)
254 DEBUG2(printk("IPv6Addr0 "));
255 if (ha->ipv6_addr1_state == IP_ADDRSTATE_ACQUIRING)
256 DEBUG2(printk("IPv6Addr1 "));
257 DEBUG2(printk("\"\n"));
258 }
259 }
260
261 return ipv4_wait|ipv6_wait;
262}
263
David Somayajuluafaf5a22006-09-19 10:28:00 -0700264static int qla4xxx_fw_ready(struct scsi_qla_host *ha)
265{
266 uint32_t timeout_count;
267 int ready = 0;
268
269 DEBUG2(dev_info(&ha->pdev->dev, "Waiting for Firmware Ready..\n"));
270 for (timeout_count = ADAPTER_INIT_TOV; timeout_count > 0;
271 timeout_count--) {
272 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
273 qla4xxx_get_dhcp_ip_address(ha);
274
275 /* Get firmware state. */
276 if (qla4xxx_get_firmware_state(ha) != QLA_SUCCESS) {
277 DEBUG2(printk("scsi%ld: %s: unable to get firmware "
278 "state\n", ha->host_no, __func__));
279 break;
280
281 }
282
283 if (ha->firmware_state & FW_STATE_ERROR) {
284 DEBUG2(printk("scsi%ld: %s: an unrecoverable error has"
285 " occurred\n", ha->host_no, __func__));
286 break;
287
288 }
289 if (ha->firmware_state & FW_STATE_CONFIG_WAIT) {
290 /*
291 * The firmware has not yet been issued an Initialize
292 * Firmware command, so issue it now.
293 */
294 if (qla4xxx_initialize_fw_cb(ha) == QLA_ERROR)
295 break;
296
297 /* Go back and test for ready state - no wait. */
298 continue;
299 }
300
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530301 if (ha->firmware_state & FW_STATE_WAIT_AUTOCONNECT) {
302 DEBUG2(printk(KERN_INFO "scsi%ld: %s: fwstate:"
303 "AUTOCONNECT in progress\n",
304 ha->host_no, __func__));
305 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700306
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530307 if (ha->firmware_state & FW_STATE_CONFIGURING_IP) {
308 DEBUG2(printk(KERN_INFO "scsi%ld: %s: fwstate:"
309 " CONFIGURING IP\n",
310 ha->host_no, __func__));
311 /*
312 * Check for link state after 15 secs and if link is
313 * still DOWN then, cable is unplugged. Ignore "DHCP
314 * in Progress/CONFIGURING IP" bit to check if firmware
315 * is in ready state or not after 15 secs.
316 * This is applicable for both 2.x & 3.x firmware
317 */
318 if (timeout_count <= (ADAPTER_INIT_TOV - 15)) {
319 if (ha->addl_fw_state & FW_ADDSTATE_LINK_UP) {
320 DEBUG2(printk(KERN_INFO "scsi%ld: %s:"
321 " LINK UP (Cable plugged)\n",
322 ha->host_no, __func__));
323 } else if (ha->firmware_state &
324 (FW_STATE_CONFIGURING_IP |
325 FW_STATE_READY)) {
326 DEBUG2(printk(KERN_INFO "scsi%ld: %s: "
327 "LINK DOWN (Cable unplugged)\n",
328 ha->host_no, __func__));
329 ha->firmware_state = FW_STATE_READY;
330 }
331 }
332 }
333
334 if (ha->firmware_state == FW_STATE_READY) {
335 /* If DHCP IP Addr is available, retrieve it now. */
336 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR,
337 &ha->dpc_flags))
338 qla4xxx_get_dhcp_ip_address(ha);
339
340 if (!qla4xxx_wait_for_ip_config(ha) ||
341 timeout_count == 1) {
342 DEBUG2(dev_info(&ha->pdev->dev,
343 "Firmware Ready..\n"));
344 /* The firmware is ready to process SCSI
345 commands. */
346 DEBUG2(dev_info(&ha->pdev->dev,
347 "scsi%ld: %s: MEDIA TYPE"
348 " - %s\n", ha->host_no,
349 __func__, (ha->addl_fw_state &
350 FW_ADDSTATE_OPTICAL_MEDIA)
351 != 0 ? "OPTICAL" : "COPPER"));
352 DEBUG2(dev_info(&ha->pdev->dev,
353 "scsi%ld: %s: DHCPv4 STATE"
354 " Enabled %s\n", ha->host_no,
355 __func__, (ha->addl_fw_state &
356 FW_ADDSTATE_DHCPv4_ENABLED) != 0 ?
357 "YES" : "NO"));
358 DEBUG2(dev_info(&ha->pdev->dev,
359 "scsi%ld: %s: LINK %s\n",
360 ha->host_no, __func__,
361 (ha->addl_fw_state &
362 FW_ADDSTATE_LINK_UP) != 0 ?
363 "UP" : "DOWN"));
364 DEBUG2(dev_info(&ha->pdev->dev,
365 "scsi%ld: %s: iSNS Service "
366 "Started %s\n",
367 ha->host_no, __func__,
368 (ha->addl_fw_state &
369 FW_ADDSTATE_ISNS_SVC_ENABLED) != 0 ?
370 "YES" : "NO"));
371
372 ready = 1;
373 break;
374 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700375 }
376 DEBUG2(printk("scsi%ld: %s: waiting on fw, state=%x:%x - "
377 "seconds expired= %d\n", ha->host_no, __func__,
378 ha->firmware_state, ha->addl_fw_state,
379 timeout_count));
David C Somayajulud9150582006-11-15 17:38:40 -0800380 if (is_qla4032(ha) &&
381 !(ha->addl_fw_state & FW_ADDSTATE_LINK_UP) &&
382 (timeout_count < ADAPTER_INIT_TOV - 5)) {
383 break;
384 }
385
David Somayajuluafaf5a22006-09-19 10:28:00 -0700386 msleep(1000);
387 } /* end of for */
388
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530389 if (timeout_count <= 0)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700390 DEBUG2(printk("scsi%ld: %s: FW Initialization timed out!\n",
391 ha->host_no, __func__));
392
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530393 if (ha->firmware_state & FW_STATE_CONFIGURING_IP) {
394 DEBUG2(printk("scsi%ld: %s: FW initialized, but is reporting "
395 "it's waiting to configure an IP address\n",
396 ha->host_no, __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700397 ready = 1;
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530398 } else if (ha->firmware_state & FW_STATE_WAIT_AUTOCONNECT) {
399 DEBUG2(printk("scsi%ld: %s: FW initialized, but "
400 "auto-discovery still in process\n",
401 ha->host_no, __func__));
Vikas Chaudharyb9663462010-07-10 14:49:19 +0530402 ready = 1;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700403 }
404
405 return ready;
406}
407
408/**
409 * qla4xxx_init_firmware - initializes the firmware.
410 * @ha: pointer to host adapter structure.
411 *
412 **/
413static int qla4xxx_init_firmware(struct scsi_qla_host *ha)
414{
415 int status = QLA_ERROR;
416
417 dev_info(&ha->pdev->dev, "Initializing firmware..\n");
418 if (qla4xxx_initialize_fw_cb(ha) == QLA_ERROR) {
419 DEBUG2(printk("scsi%ld: %s: Failed to initialize firmware "
420 "control block\n", ha->host_no, __func__));
421 return status;
422 }
423 if (!qla4xxx_fw_ready(ha))
424 return status;
425
David Somayajuluafaf5a22006-09-19 10:28:00 -0700426 return qla4xxx_get_firmware_status(ha);
427}
428
429static struct ddb_entry* qla4xxx_get_ddb_entry(struct scsi_qla_host *ha,
David C Somayajulu92b72732007-05-23 17:55:40 -0700430 uint32_t fw_ddb_index,
431 uint32_t *new_tgt)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700432{
433 struct dev_db_entry *fw_ddb_entry = NULL;
434 dma_addr_t fw_ddb_entry_dma;
435 struct ddb_entry *ddb_entry = NULL;
436 int found = 0;
437 uint32_t device_state;
438
David C Somayajulu92b72732007-05-23 17:55:40 -0700439 *new_tgt = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700440 /* Make sure the dma buffer is valid */
441 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev,
442 sizeof(*fw_ddb_entry),
443 &fw_ddb_entry_dma, GFP_KERNEL);
444 if (fw_ddb_entry == NULL) {
445 DEBUG2(printk("scsi%ld: %s: Unable to allocate dma buffer.\n",
446 ha->host_no, __func__));
447 return NULL;
448 }
449
450 if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index, fw_ddb_entry,
451 fw_ddb_entry_dma, NULL, NULL,
452 &device_state, NULL, NULL, NULL) ==
453 QLA_ERROR) {
454 DEBUG2(printk("scsi%ld: %s: failed get_ddb_entry for "
455 "fw_ddb_index %d\n", ha->host_no, __func__,
456 fw_ddb_index));
457 return NULL;
458 }
459
460 /* Allocate DDB if not already allocated. */
461 DEBUG2(printk("scsi%ld: %s: Looking for ddb[%d]\n", ha->host_no,
462 __func__, fw_ddb_index));
463 list_for_each_entry(ddb_entry, &ha->ddb_list, list) {
Mike Christie41bbdbe2009-01-16 12:36:52 -0600464 if ((memcmp(ddb_entry->iscsi_name, fw_ddb_entry->iscsi_name,
465 ISCSI_NAME_SIZE) == 0) &&
466 (ddb_entry->tpgt ==
467 le32_to_cpu(fw_ddb_entry->tgt_portal_grp)) &&
468 (memcmp(ddb_entry->isid, fw_ddb_entry->isid,
469 sizeof(ddb_entry->isid)) == 0)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700470 found++;
471 break;
472 }
473 }
474
475 if (!found) {
476 DEBUG2(printk("scsi%ld: %s: ddb[%d] not found - allocating "
477 "new ddb\n", ha->host_no, __func__,
478 fw_ddb_index));
David C Somayajulu92b72732007-05-23 17:55:40 -0700479 *new_tgt = 1;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700480 ddb_entry = qla4xxx_alloc_ddb(ha, fw_ddb_index);
481 }
482
483 /* if not found allocate new ddb */
484 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry), fw_ddb_entry,
485 fw_ddb_entry_dma);
486
487 return ddb_entry;
488}
489
490/**
491 * qla4xxx_update_ddb_entry - update driver's internal ddb
492 * @ha: pointer to host adapter structure.
493 * @ddb_entry: pointer to device database structure to be filled
494 * @fw_ddb_index: index of the ddb entry in fw ddb table
495 *
496 * This routine updates the driver's internal device database entry
497 * with information retrieved from the firmware's device database
498 * entry for the specified device. The ddb_entry->fw_ddb_index field
499 * must be initialized prior to calling this routine
500 *
501 **/
Adrian Bunk47975472007-04-26 00:35:16 -0700502static int qla4xxx_update_ddb_entry(struct scsi_qla_host *ha,
503 struct ddb_entry *ddb_entry,
504 uint32_t fw_ddb_index)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700505{
506 struct dev_db_entry *fw_ddb_entry = NULL;
507 dma_addr_t fw_ddb_entry_dma;
508 int status = QLA_ERROR;
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530509 uint32_t conn_err;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700510
511 if (ddb_entry == NULL) {
512 DEBUG2(printk("scsi%ld: %s: ddb_entry is NULL\n", ha->host_no,
513 __func__));
514 goto exit_update_ddb;
515 }
516
517 /* Make sure the dma buffer is valid */
518 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev,
519 sizeof(*fw_ddb_entry),
520 &fw_ddb_entry_dma, GFP_KERNEL);
521 if (fw_ddb_entry == NULL) {
522 DEBUG2(printk("scsi%ld: %s: Unable to allocate dma buffer.\n",
523 ha->host_no, __func__));
524
525 goto exit_update_ddb;
526 }
527
528 if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index, fw_ddb_entry,
529 fw_ddb_entry_dma, NULL, NULL,
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530530 &ddb_entry->fw_ddb_device_state, &conn_err,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700531 &ddb_entry->tcp_source_port_num,
532 &ddb_entry->connection_id) ==
533 QLA_ERROR) {
534 DEBUG2(printk("scsi%ld: %s: failed get_ddb_entry for "
535 "fw_ddb_index %d\n", ha->host_no, __func__,
536 fw_ddb_index));
537
538 goto exit_update_ddb;
539 }
540
541 status = QLA_SUCCESS;
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530542 ddb_entry->options = le16_to_cpu(fw_ddb_entry->options);
David C Somayajulu92b72732007-05-23 17:55:40 -0700543 ddb_entry->target_session_id = le16_to_cpu(fw_ddb_entry->tsid);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700544 ddb_entry->task_mgmt_timeout =
David C Somayajulu92b72732007-05-23 17:55:40 -0700545 le16_to_cpu(fw_ddb_entry->def_timeout);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700546 ddb_entry->CmdSn = 0;
David C Somayajulu92b72732007-05-23 17:55:40 -0700547 ddb_entry->exe_throttle = le16_to_cpu(fw_ddb_entry->exec_throttle);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700548 ddb_entry->default_relogin_timeout =
David C Somayajulu92b72732007-05-23 17:55:40 -0700549 le16_to_cpu(fw_ddb_entry->def_timeout);
550 ddb_entry->default_time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700551
552 /* Update index in case it changed */
553 ddb_entry->fw_ddb_index = fw_ddb_index;
554 ha->fw_ddb_index_map[fw_ddb_index] = ddb_entry;
555
David C Somayajulu92b72732007-05-23 17:55:40 -0700556 ddb_entry->port = le16_to_cpu(fw_ddb_entry->port);
557 ddb_entry->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
Mike Christie41bbdbe2009-01-16 12:36:52 -0600558 memcpy(ddb_entry->isid, fw_ddb_entry->isid, sizeof(ddb_entry->isid));
559
David C Somayajulu92b72732007-05-23 17:55:40 -0700560 memcpy(&ddb_entry->iscsi_name[0], &fw_ddb_entry->iscsi_name[0],
David Somayajuluafaf5a22006-09-19 10:28:00 -0700561 min(sizeof(ddb_entry->iscsi_name),
David C Somayajulu92b72732007-05-23 17:55:40 -0700562 sizeof(fw_ddb_entry->iscsi_name)));
563 memcpy(&ddb_entry->ip_addr[0], &fw_ddb_entry->ip_addr[0],
564 min(sizeof(ddb_entry->ip_addr), sizeof(fw_ddb_entry->ip_addr)));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700565
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530566 ddb_entry->iscsi_max_burst_len = fw_ddb_entry->iscsi_max_burst_len;
567 ddb_entry->iscsi_max_outsnd_r2t = fw_ddb_entry->iscsi_max_outsnd_r2t;
568 ddb_entry->iscsi_first_burst_len = fw_ddb_entry->iscsi_first_burst_len;
569 ddb_entry->iscsi_max_rcv_data_seg_len =
570 fw_ddb_entry->iscsi_max_rcv_data_seg_len;
571 ddb_entry->iscsi_max_snd_data_seg_len =
572 fw_ddb_entry->iscsi_max_snd_data_seg_len;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700573
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530574 if (ddb_entry->options & DDB_OPT_IPV6_DEVICE) {
575 memcpy(&ddb_entry->remote_ipv6_addr,
576 fw_ddb_entry->ip_addr,
577 min(sizeof(ddb_entry->remote_ipv6_addr),
578 sizeof(fw_ddb_entry->ip_addr)));
579 memcpy(&ddb_entry->link_local_ipv6_addr,
580 fw_ddb_entry->link_local_ipv6_addr,
581 min(sizeof(ddb_entry->link_local_ipv6_addr),
582 sizeof(fw_ddb_entry->link_local_ipv6_addr)));
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530583
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530584 DEBUG2(dev_info(&ha->pdev->dev, "%s: DDB[%d] osIdx = %d "
585 "State %04x ConnErr %08x IP %pI6 "
586 ":%04d \"%s\"\n",
587 __func__, fw_ddb_index,
588 ddb_entry->os_target_id,
589 ddb_entry->fw_ddb_device_state,
590 conn_err, fw_ddb_entry->ip_addr,
591 le16_to_cpu(fw_ddb_entry->port),
592 fw_ddb_entry->iscsi_name));
593 } else
594 DEBUG2(dev_info(&ha->pdev->dev, "%s: DDB[%d] osIdx = %d "
595 "State %04x ConnErr %08x IP %pI4 "
596 ":%04d \"%s\"\n",
597 __func__, fw_ddb_index,
598 ddb_entry->os_target_id,
599 ddb_entry->fw_ddb_device_state,
600 conn_err, fw_ddb_entry->ip_addr,
601 le16_to_cpu(fw_ddb_entry->port),
602 fw_ddb_entry->iscsi_name));
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530603exit_update_ddb:
David Somayajuluafaf5a22006-09-19 10:28:00 -0700604 if (fw_ddb_entry)
605 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
606 fw_ddb_entry, fw_ddb_entry_dma);
607
608 return status;
609}
610
611/**
612 * qla4xxx_alloc_ddb - allocate device database entry
613 * @ha: Pointer to host adapter structure.
614 * @fw_ddb_index: Firmware's device database index
615 *
616 * This routine allocates a ddb_entry, ititializes some values, and
617 * inserts it into the ddb list.
618 **/
Adrian Bunk47975472007-04-26 00:35:16 -0700619static struct ddb_entry * qla4xxx_alloc_ddb(struct scsi_qla_host *ha,
620 uint32_t fw_ddb_index)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700621{
622 struct ddb_entry *ddb_entry;
623
624 DEBUG2(printk("scsi%ld: %s: fw_ddb_index [%d]\n", ha->host_no,
625 __func__, fw_ddb_index));
626
627 ddb_entry = qla4xxx_alloc_sess(ha);
628 if (ddb_entry == NULL) {
629 DEBUG2(printk("scsi%ld: %s: Unable to allocate memory "
630 "to add fw_ddb_index [%d]\n",
631 ha->host_no, __func__, fw_ddb_index));
632 return ddb_entry;
633 }
634
635 ddb_entry->fw_ddb_index = fw_ddb_index;
636 atomic_set(&ddb_entry->port_down_timer, ha->port_down_retry_count);
637 atomic_set(&ddb_entry->retry_relogin_timer, INVALID_ENTRY);
638 atomic_set(&ddb_entry->relogin_timer, 0);
639 atomic_set(&ddb_entry->relogin_retry_count, 0);
640 atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
641 list_add_tail(&ddb_entry->list, &ha->ddb_list);
642 ha->fw_ddb_index_map[fw_ddb_index] = ddb_entry;
643 ha->tot_ddbs++;
644
645 return ddb_entry;
646}
647
648/**
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530649 * qla4_is_relogin_allowed - Are we allowed to login?
650 * @ha: Pointer to host adapter structure.
651 * @conn_err: Last connection error associated with the ddb
652 *
653 * This routine tests the given connection error to determine if
654 * we are allowed to login.
655 **/
656int qla4_is_relogin_allowed(struct scsi_qla_host *ha, uint32_t conn_err)
657{
658 uint32_t err_code, login_rsp_sts_class;
659 int relogin = 1;
660
661 err_code = ((conn_err & 0x00ff0000) >> 16);
662 login_rsp_sts_class = ((conn_err & 0x0000ff00) >> 8);
663 if (err_code == 0x1c || err_code == 0x06) {
664 DEBUG2(dev_info(&ha->pdev->dev,
665 ": conn_err=0x%08x, send target completed"
666 " or access denied failure\n", conn_err));
667 relogin = 0;
668 }
669 if ((err_code == 0x08) && (login_rsp_sts_class == 0x02)) {
670 /* Login Response PDU returned an error.
671 Login Response Status in Error Code Detail
672 indicates login should not be retried.*/
673 DEBUG2(dev_info(&ha->pdev->dev,
674 ": conn_err=0x%08x, do not retry relogin\n",
675 conn_err));
676 relogin = 0;
677 }
678
679 return relogin;
680}
681
682/**
David Somayajuluafaf5a22006-09-19 10:28:00 -0700683 * qla4xxx_configure_ddbs - builds driver ddb list
684 * @ha: Pointer to host adapter structure.
685 *
686 * This routine searches for all valid firmware ddb entries and builds
687 * an internal ddb list. Ddbs that are considered valid are those with
688 * a device state of SESSION_ACTIVE.
689 **/
690static int qla4xxx_build_ddb_list(struct scsi_qla_host *ha)
691{
692 int status = QLA_SUCCESS;
693 uint32_t fw_ddb_index = 0;
694 uint32_t next_fw_ddb_index = 0;
695 uint32_t ddb_state;
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530696 uint32_t conn_err;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700697 struct ddb_entry *ddb_entry;
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530698 struct dev_db_entry *fw_ddb_entry = NULL;
699 dma_addr_t fw_ddb_entry_dma;
700 uint32_t ipv6_device;
David C Somayajulu92b72732007-05-23 17:55:40 -0700701 uint32_t new_tgt;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700702
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530703 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
704 &fw_ddb_entry_dma, GFP_KERNEL);
705 if (fw_ddb_entry == NULL) {
706 DEBUG2(dev_info(&ha->pdev->dev, "%s: DMA alloc failed\n",
707 __func__));
708 return QLA_ERROR;
709 }
710
David Somayajuluafaf5a22006-09-19 10:28:00 -0700711 dev_info(&ha->pdev->dev, "Initializing DDBs ...\n");
712 for (fw_ddb_index = 0; fw_ddb_index < MAX_DDB_ENTRIES;
713 fw_ddb_index = next_fw_ddb_index) {
714 /* First, let's see if a device exists here */
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530715 if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index, fw_ddb_entry,
716 0, NULL, &next_fw_ddb_index,
717 &ddb_state, &conn_err,
718 NULL, NULL) ==
719 QLA_ERROR) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700720 DEBUG2(printk("scsi%ld: %s: get_ddb_entry, "
721 "fw_ddb_index %d failed", ha->host_no,
722 __func__, fw_ddb_index));
723 return QLA_ERROR;
724 }
725
726 DEBUG2(printk("scsi%ld: %s: Getting DDB[%d] ddbstate=0x%x, "
727 "next_fw_ddb_index=%d.\n", ha->host_no, __func__,
728 fw_ddb_index, ddb_state, next_fw_ddb_index));
729
730 /* Issue relogin, if necessary. */
731 if (ddb_state == DDB_DS_SESSION_FAILED ||
732 ddb_state == DDB_DS_NO_CONNECTION_ACTIVE) {
733 /* Try and login to device */
734 DEBUG2(printk("scsi%ld: %s: Login to DDB[%d]\n",
735 ha->host_no, __func__, fw_ddb_index));
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530736 ipv6_device = le16_to_cpu(fw_ddb_entry->options) &
737 DDB_OPT_IPV6_DEVICE;
738 if (qla4_is_relogin_allowed(ha, conn_err) &&
739 ((!ipv6_device &&
740 *((uint32_t *)fw_ddb_entry->ip_addr))
741 || ipv6_device)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700742 qla4xxx_set_ddb_entry(ha, fw_ddb_index, 0);
David C Somayajulu92b72732007-05-23 17:55:40 -0700743 if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index,
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530744 NULL, 0, NULL,
745 &next_fw_ddb_index,
746 &ddb_state, &conn_err,
747 NULL, NULL)
748 == QLA_ERROR) {
David C Somayajulu92b72732007-05-23 17:55:40 -0700749 DEBUG2(printk("scsi%ld: %s:"
750 "get_ddb_entry %d failed\n",
751 ha->host_no,
752 __func__, fw_ddb_index));
753 return QLA_ERROR;
754 }
755 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700756 }
757
758 if (ddb_state != DDB_DS_SESSION_ACTIVE)
759 goto next_one;
760 /*
761 * if fw_ddb with session active state found,
762 * add to ddb_list
763 */
764 DEBUG2(printk("scsi%ld: %s: DDB[%d] added to list\n",
765 ha->host_no, __func__, fw_ddb_index));
766
767 /* Add DDB to internal our ddb list. */
David C Somayajulu92b72732007-05-23 17:55:40 -0700768 ddb_entry = qla4xxx_get_ddb_entry(ha, fw_ddb_index, &new_tgt);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700769 if (ddb_entry == NULL) {
770 DEBUG2(printk("scsi%ld: %s: Unable to allocate memory "
771 "for device at fw_ddb_index %d\n",
772 ha->host_no, __func__, fw_ddb_index));
773 return QLA_ERROR;
774 }
775 /* Fill in the device structure */
776 if (qla4xxx_update_ddb_entry(ha, ddb_entry, fw_ddb_index) ==
777 QLA_ERROR) {
778 ha->fw_ddb_index_map[fw_ddb_index] =
779 (struct ddb_entry *)INVALID_ENTRY;
780
781
782 DEBUG2(printk("scsi%ld: %s: update_ddb_entry failed "
783 "for fw_ddb_index %d.\n",
784 ha->host_no, __func__, fw_ddb_index));
785 return QLA_ERROR;
786 }
787
788next_one:
789 /* We know we've reached the last device when
790 * next_fw_ddb_index is 0 */
791 if (next_fw_ddb_index == 0)
792 break;
793 }
794
795 dev_info(&ha->pdev->dev, "DDB list done..\n");
796
797 return status;
798}
799
800struct qla4_relog_scan {
801 int halt_wait;
802 uint32_t conn_err;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700803 uint32_t fw_ddb_index;
804 uint32_t next_fw_ddb_index;
805 uint32_t fw_ddb_device_state;
806};
807
808static int qla4_test_rdy(struct scsi_qla_host *ha, struct qla4_relog_scan *rs)
809{
810 struct ddb_entry *ddb_entry;
811
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530812 if (qla4_is_relogin_allowed(ha, rs->conn_err)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700813 /* We either have a device that is in
814 * the process of relogging in or a
815 * device that is waiting to be
816 * relogged in */
817 rs->halt_wait = 0;
818
819 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha,
820 rs->fw_ddb_index);
821 if (ddb_entry == NULL)
822 return QLA_ERROR;
823
824 if (ddb_entry->dev_scan_wait_to_start_relogin != 0
825 && time_after_eq(jiffies,
826 ddb_entry->
827 dev_scan_wait_to_start_relogin))
828 {
829 ddb_entry->dev_scan_wait_to_start_relogin = 0;
830 qla4xxx_set_ddb_entry(ha, rs->fw_ddb_index, 0);
831 }
832 }
833 return QLA_SUCCESS;
834}
835
836static int qla4_scan_for_relogin(struct scsi_qla_host *ha,
837 struct qla4_relog_scan *rs)
838{
839 int error;
840
841 /* scan for relogins
842 * ----------------- */
843 for (rs->fw_ddb_index = 0; rs->fw_ddb_index < MAX_DDB_ENTRIES;
844 rs->fw_ddb_index = rs->next_fw_ddb_index) {
845 if (qla4xxx_get_fwddb_entry(ha, rs->fw_ddb_index, NULL, 0,
846 NULL, &rs->next_fw_ddb_index,
847 &rs->fw_ddb_device_state,
848 &rs->conn_err, NULL, NULL)
849 == QLA_ERROR)
850 return QLA_ERROR;
851
852 if (rs->fw_ddb_device_state == DDB_DS_LOGIN_IN_PROCESS)
853 rs->halt_wait = 0;
854
855 if (rs->fw_ddb_device_state == DDB_DS_SESSION_FAILED ||
856 rs->fw_ddb_device_state == DDB_DS_NO_CONNECTION_ACTIVE) {
857 error = qla4_test_rdy(ha, rs);
858 if (error)
859 return error;
860 }
861
862 /* We know we've reached the last device when
863 * next_fw_ddb_index is 0 */
864 if (rs->next_fw_ddb_index == 0)
865 break;
866 }
867 return QLA_SUCCESS;
868}
869
870/**
871 * qla4xxx_devices_ready - wait for target devices to be logged in
872 * @ha: pointer to adapter structure
873 *
874 * This routine waits up to ql4xdiscoverywait seconds
875 * F/W database during driver load time.
876 **/
877static int qla4xxx_devices_ready(struct scsi_qla_host *ha)
878{
879 int error;
880 unsigned long discovery_wtime;
881 struct qla4_relog_scan rs;
882
883 discovery_wtime = jiffies + (ql4xdiscoverywait * HZ);
884
885 DEBUG(printk("Waiting (%d) for devices ...\n", ql4xdiscoverywait));
886 do {
887 /* poll for AEN. */
888 qla4xxx_get_firmware_state(ha);
889 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags)) {
890 /* Set time-between-relogin timer */
891 qla4xxx_process_aen(ha, RELOGIN_DDB_CHANGED_AENS);
892 }
893
894 /* if no relogins active or needed, halt discvery wait */
895 rs.halt_wait = 1;
896
897 error = qla4_scan_for_relogin(ha, &rs);
898
899 if (rs.halt_wait) {
900 DEBUG2(printk("scsi%ld: %s: Delay halted. Devices "
901 "Ready.\n", ha->host_no, __func__));
902 return QLA_SUCCESS;
903 }
904
905 msleep(2000);
906 } while (!time_after_eq(jiffies, discovery_wtime));
907
908 DEBUG3(qla4xxx_get_conn_event_log(ha));
909
910 return QLA_SUCCESS;
911}
912
913static void qla4xxx_flush_AENS(struct scsi_qla_host *ha)
914{
915 unsigned long wtime;
916
917 /* Flush the 0x8014 AEN from the firmware as a result of
918 * Auto connect. We are basically doing get_firmware_ddb()
919 * to determine whether we need to log back in or not.
920 * Trying to do a set ddb before we have processed 0x8014
921 * will result in another set_ddb() for the same ddb. In other
922 * words there will be stale entries in the aen_q.
923 */
924 wtime = jiffies + (2 * HZ);
925 do {
926 if (qla4xxx_get_firmware_state(ha) == QLA_SUCCESS)
927 if (ha->firmware_state & (BIT_2 | BIT_0))
928 return;
929
930 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
931 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
932
933 msleep(1000);
934 } while (!time_after_eq(jiffies, wtime));
935
936}
937
938static int qla4xxx_initialize_ddb_list(struct scsi_qla_host *ha)
939{
940 uint16_t fw_ddb_index;
941 int status = QLA_SUCCESS;
942
943 /* free the ddb list if is not empty */
944 if (!list_empty(&ha->ddb_list))
945 qla4xxx_free_ddb_list(ha);
946
947 for (fw_ddb_index = 0; fw_ddb_index < MAX_DDB_ENTRIES; fw_ddb_index++)
948 ha->fw_ddb_index_map[fw_ddb_index] =
949 (struct ddb_entry *)INVALID_ENTRY;
950
951 ha->tot_ddbs = 0;
952
953 qla4xxx_flush_AENS(ha);
954
955 /*
956 * First perform device discovery for active
957 * fw ddb indexes and build
958 * ddb list.
959 */
960 if ((status = qla4xxx_build_ddb_list(ha)) == QLA_ERROR)
961 return status;
962
963 /* Wait for an AEN */
964 qla4xxx_devices_ready(ha);
965
966 /*
967 * Targets can come online after the inital discovery, so processing
968 * the aens here will catch them.
969 */
970 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
971 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
972
973 return status;
974}
975
976/**
977 * qla4xxx_update_ddb_list - update the driver ddb list
978 * @ha: pointer to host adapter structure.
979 *
980 * This routine obtains device information from the F/W database after
981 * firmware or adapter resets. The device table is preserved.
982 **/
983int qla4xxx_reinitialize_ddb_list(struct scsi_qla_host *ha)
984{
985 int status = QLA_SUCCESS;
986 struct ddb_entry *ddb_entry, *detemp;
987
988 /* Update the device information for all devices. */
989 list_for_each_entry_safe(ddb_entry, detemp, &ha->ddb_list, list) {
990 qla4xxx_update_ddb_entry(ha, ddb_entry,
991 ddb_entry->fw_ddb_index);
992 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
993 atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
994 DEBUG2(printk ("scsi%ld: %s: ddb index [%d] marked "
995 "ONLINE\n", ha->host_no, __func__,
996 ddb_entry->fw_ddb_index));
Vikas Chaudhary36386322010-07-10 14:49:01 +0530997 iscsi_unblock_session(ddb_entry->sess);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700998 } else if (atomic_read(&ddb_entry->state) == DDB_STATE_ONLINE)
999 qla4xxx_mark_device_missing(ha, ddb_entry);
1000 }
1001 return status;
1002}
1003
1004/**
1005 * qla4xxx_relogin_device - re-establish session
1006 * @ha: Pointer to host adapter structure.
1007 * @ddb_entry: Pointer to device database entry
1008 *
1009 * This routine does a session relogin with the specified device.
1010 * The ddb entry must be assigned prior to making this call.
1011 **/
1012int qla4xxx_relogin_device(struct scsi_qla_host *ha,
1013 struct ddb_entry * ddb_entry)
1014{
1015 uint16_t relogin_timer;
1016
1017 relogin_timer = max(ddb_entry->default_relogin_timeout,
1018 (uint16_t)RELOGIN_TOV);
1019 atomic_set(&ddb_entry->relogin_timer, relogin_timer);
1020
1021 DEBUG2(printk("scsi%ld: Relogin index [%d]. TOV=%d\n", ha->host_no,
1022 ddb_entry->fw_ddb_index, relogin_timer));
1023
1024 qla4xxx_set_ddb_entry(ha, ddb_entry->fw_ddb_index, 0);
1025
1026 return QLA_SUCCESS;
1027}
1028
David Somayajuluafaf5a22006-09-19 10:28:00 -07001029static int qla4xxx_config_nvram(struct scsi_qla_host *ha)
1030{
1031 unsigned long flags;
1032 union external_hw_config_reg extHwConfig;
1033
1034 DEBUG2(printk("scsi%ld: %s: Get EEProm parameters \n", ha->host_no,
1035 __func__));
1036 if (ql4xxx_lock_flash(ha) != QLA_SUCCESS)
Mike Christieb3925512010-02-10 16:51:48 -06001037 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001038 if (ql4xxx_lock_nvram(ha) != QLA_SUCCESS) {
1039 ql4xxx_unlock_flash(ha);
Mike Christieb3925512010-02-10 16:51:48 -06001040 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001041 }
1042
1043 /* Get EEPRom Parameters from NVRAM and validate */
1044 dev_info(&ha->pdev->dev, "Configuring NVRAM ...\n");
1045 if (qla4xxx_is_nvram_configuration_valid(ha) == QLA_SUCCESS) {
1046 spin_lock_irqsave(&ha->hardware_lock, flags);
1047 extHwConfig.Asuint32_t =
1048 rd_nvram_word(ha, eeprom_ext_hw_conf_offset(ha));
1049 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1050 } else {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001051 dev_warn(&ha->pdev->dev,
1052 "scsi%ld: %s: EEProm checksum invalid. "
1053 "Please update your EEPROM\n", ha->host_no,
1054 __func__);
1055
Mike Christieb3925512010-02-10 16:51:48 -06001056 /* Attempt to set defaults */
David Somayajuluafaf5a22006-09-19 10:28:00 -07001057 if (is_qla4010(ha))
1058 extHwConfig.Asuint32_t = 0x1912;
David C Somayajulud9150582006-11-15 17:38:40 -08001059 else if (is_qla4022(ha) | is_qla4032(ha))
David Somayajuluafaf5a22006-09-19 10:28:00 -07001060 extHwConfig.Asuint32_t = 0x0023;
Mike Christieb3925512010-02-10 16:51:48 -06001061 else
1062 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001063 }
1064 DEBUG(printk("scsi%ld: %s: Setting extHwConfig to 0xFFFF%04x\n",
1065 ha->host_no, __func__, extHwConfig.Asuint32_t));
1066
1067 spin_lock_irqsave(&ha->hardware_lock, flags);
1068 writel((0xFFFF << 16) | extHwConfig.Asuint32_t, isp_ext_hw_conf(ha));
1069 readl(isp_ext_hw_conf(ha));
1070 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1071
1072 ql4xxx_unlock_nvram(ha);
1073 ql4xxx_unlock_flash(ha);
1074
Mike Christieb3925512010-02-10 16:51:48 -06001075 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001076}
1077
1078static void qla4x00_pci_config(struct scsi_qla_host *ha)
1079{
David C Somayajulu92b72732007-05-23 17:55:40 -07001080 uint16_t w;
David C Somayajulu46235e62007-06-08 17:37:16 -07001081 int status;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001082
1083 dev_info(&ha->pdev->dev, "Configuring PCI space...\n");
1084
1085 pci_set_master(ha->pdev);
David C Somayajulu46235e62007-06-08 17:37:16 -07001086 status = pci_set_mwi(ha->pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001087 /*
1088 * We want to respect framework's setting of PCI configuration space
1089 * command register and also want to make sure that all bits of
1090 * interest to us are properly set in command register.
1091 */
1092 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
David C Somayajulu92b72732007-05-23 17:55:40 -07001093 w |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001094 w &= ~PCI_COMMAND_INTX_DISABLE;
1095 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
1096}
1097
1098static int qla4xxx_start_firmware_from_flash(struct scsi_qla_host *ha)
1099{
1100 int status = QLA_ERROR;
Vikas Chaudharyc301b022010-04-28 11:40:37 +05301101 unsigned long max_wait_time;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001102 unsigned long flags;
1103 uint32_t mbox_status;
1104
1105 dev_info(&ha->pdev->dev, "Starting firmware ...\n");
1106
1107 /*
1108 * Start firmware from flash ROM
1109 *
1110 * WORKAROUND: Stuff a non-constant value that the firmware can
1111 * use as a seed for a random number generator in MB7 prior to
1112 * setting BOOT_ENABLE. Fixes problem where the TCP
1113 * connections use the same TCP ports after each reboot,
1114 * causing some connections to not get re-established.
1115 */
1116 DEBUG(printk("scsi%d: %s: Start firmware from flash ROM\n",
1117 ha->host_no, __func__));
1118
1119 spin_lock_irqsave(&ha->hardware_lock, flags);
1120 writel(jiffies, &ha->reg->mailbox[7]);
David C Somayajulud9150582006-11-15 17:38:40 -08001121 if (is_qla4022(ha) | is_qla4032(ha))
David Somayajuluafaf5a22006-09-19 10:28:00 -07001122 writel(set_rmask(NVR_WRITE_ENABLE),
1123 &ha->reg->u1.isp4022.nvram);
1124
David C Somayajulu92b72732007-05-23 17:55:40 -07001125 writel(2, &ha->reg->mailbox[6]);
1126 readl(&ha->reg->mailbox[6]);
1127
David Somayajuluafaf5a22006-09-19 10:28:00 -07001128 writel(set_rmask(CSR_BOOT_ENABLE), &ha->reg->ctrl_status);
1129 readl(&ha->reg->ctrl_status);
1130 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1131
1132 /* Wait for firmware to come UP. */
Vikas Chaudharyc301b022010-04-28 11:40:37 +05301133 DEBUG2(printk(KERN_INFO "scsi%ld: %s: Wait up to %d seconds for "
1134 "boot firmware to complete...\n",
1135 ha->host_no, __func__, FIRMWARE_UP_TOV));
1136 max_wait_time = jiffies + (FIRMWARE_UP_TOV * HZ);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001137 do {
1138 uint32_t ctrl_status;
1139
1140 spin_lock_irqsave(&ha->hardware_lock, flags);
1141 ctrl_status = readw(&ha->reg->ctrl_status);
1142 mbox_status = readw(&ha->reg->mailbox[0]);
1143 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1144
1145 if (ctrl_status & set_rmask(CSR_SCSI_PROCESSOR_INTR))
1146 break;
1147 if (mbox_status == MBOX_STS_COMMAND_COMPLETE)
1148 break;
1149
Vikas Chaudharyc301b022010-04-28 11:40:37 +05301150 DEBUG2(printk(KERN_INFO "scsi%ld: %s: Waiting for boot "
1151 "firmware to complete... ctrl_sts=0x%x\n",
1152 ha->host_no, __func__, ctrl_status));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001153
Vikas Chaudharyc301b022010-04-28 11:40:37 +05301154 msleep_interruptible(250);
1155 } while (!time_after_eq(jiffies, max_wait_time));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001156
1157 if (mbox_status == MBOX_STS_COMMAND_COMPLETE) {
Vikas Chaudharyc301b022010-04-28 11:40:37 +05301158 DEBUG(printk(KERN_INFO "scsi%ld: %s: Firmware has started\n",
David Somayajuluafaf5a22006-09-19 10:28:00 -07001159 ha->host_no, __func__));
1160
1161 spin_lock_irqsave(&ha->hardware_lock, flags);
1162 writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
1163 &ha->reg->ctrl_status);
1164 readl(&ha->reg->ctrl_status);
1165 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1166
1167 status = QLA_SUCCESS;
1168 } else {
1169 printk(KERN_INFO "scsi%ld: %s: Boot firmware failed "
1170 "- mbox status 0x%x\n", ha->host_no, __func__,
1171 mbox_status);
1172 status = QLA_ERROR;
1173 }
1174 return status;
1175}
1176
David C Somayajulu92b72732007-05-23 17:55:40 -07001177int ql4xxx_lock_drvr_wait(struct scsi_qla_host *a)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001178{
David C Somayajulu92b72732007-05-23 17:55:40 -07001179#define QL4_LOCK_DRVR_WAIT 60
David C Somayajulu477ffb92007-01-22 12:26:11 -08001180#define QL4_LOCK_DRVR_SLEEP 1
David Somayajuluafaf5a22006-09-19 10:28:00 -07001181
1182 int drvr_wait = QL4_LOCK_DRVR_WAIT;
1183 while (drvr_wait) {
David C Somayajulu92b72732007-05-23 17:55:40 -07001184 if (ql4xxx_lock_drvr(a) == 0) {
David C Somayajulu477ffb92007-01-22 12:26:11 -08001185 ssleep(QL4_LOCK_DRVR_SLEEP);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001186 if (drvr_wait) {
1187 DEBUG2(printk("scsi%ld: %s: Waiting for "
David C Somayajulu92b72732007-05-23 17:55:40 -07001188 "Global Init Semaphore(%d)...\n",
1189 a->host_no,
David C Somayajulu477ffb92007-01-22 12:26:11 -08001190 __func__, drvr_wait));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001191 }
1192 drvr_wait -= QL4_LOCK_DRVR_SLEEP;
1193 } else {
1194 DEBUG2(printk("scsi%ld: %s: Global Init Semaphore "
David C Somayajulu92b72732007-05-23 17:55:40 -07001195 "acquired\n", a->host_no, __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001196 return QLA_SUCCESS;
1197 }
1198 }
1199 return QLA_ERROR;
1200}
1201
1202/**
1203 * qla4xxx_start_firmware - starts qla4xxx firmware
1204 * @ha: Pointer to host adapter structure.
1205 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001206 * This routine performs the necessary steps to start the firmware for
David Somayajuluafaf5a22006-09-19 10:28:00 -07001207 * the QLA4010 adapter.
1208 **/
1209static int qla4xxx_start_firmware(struct scsi_qla_host *ha)
1210{
1211 unsigned long flags = 0;
1212 uint32_t mbox_status;
1213 int status = QLA_ERROR;
1214 int soft_reset = 1;
1215 int config_chip = 0;
1216
David C Somayajulud9150582006-11-15 17:38:40 -08001217 if (is_qla4022(ha) | is_qla4032(ha))
David Somayajuluafaf5a22006-09-19 10:28:00 -07001218 ql4xxx_set_mac_number(ha);
1219
1220 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
1221 return QLA_ERROR;
1222
1223 spin_lock_irqsave(&ha->hardware_lock, flags);
1224
1225 DEBUG2(printk("scsi%ld: %s: port_ctrl = 0x%08X\n", ha->host_no,
1226 __func__, readw(isp_port_ctrl(ha))));
1227 DEBUG(printk("scsi%ld: %s: port_status = 0x%08X\n", ha->host_no,
1228 __func__, readw(isp_port_status(ha))));
1229
1230 /* Is Hardware already initialized? */
1231 if ((readw(isp_port_ctrl(ha)) & 0x8000) != 0) {
1232 DEBUG(printk("scsi%ld: %s: Hardware has already been "
1233 "initialized\n", ha->host_no, __func__));
1234
1235 /* Receive firmware boot acknowledgement */
1236 mbox_status = readw(&ha->reg->mailbox[0]);
1237
1238 DEBUG2(printk("scsi%ld: %s: H/W Config complete - mbox[0]= "
1239 "0x%x\n", ha->host_no, __func__, mbox_status));
1240
1241 /* Is firmware already booted? */
1242 if (mbox_status == 0) {
1243 /* F/W not running, must be config by net driver */
1244 config_chip = 1;
1245 soft_reset = 0;
1246 } else {
1247 writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
1248 &ha->reg->ctrl_status);
1249 readl(&ha->reg->ctrl_status);
1250 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1251 if (qla4xxx_get_firmware_state(ha) == QLA_SUCCESS) {
1252 DEBUG2(printk("scsi%ld: %s: Get firmware "
1253 "state -- state = 0x%x\n",
1254 ha->host_no,
1255 __func__, ha->firmware_state));
1256 /* F/W is running */
1257 if (ha->firmware_state &
1258 FW_STATE_CONFIG_WAIT) {
1259 DEBUG2(printk("scsi%ld: %s: Firmware "
1260 "in known state -- "
1261 "config and "
1262 "boot, state = 0x%x\n",
1263 ha->host_no, __func__,
1264 ha->firmware_state));
1265 config_chip = 1;
1266 soft_reset = 0;
1267 }
1268 } else {
1269 DEBUG2(printk("scsi%ld: %s: Firmware in "
1270 "unknown state -- resetting,"
1271 " state = "
1272 "0x%x\n", ha->host_no, __func__,
1273 ha->firmware_state));
1274 }
1275 spin_lock_irqsave(&ha->hardware_lock, flags);
1276 }
1277 } else {
1278 DEBUG(printk("scsi%ld: %s: H/W initialization hasn't been "
1279 "started - resetting\n", ha->host_no, __func__));
1280 }
1281 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1282
1283 DEBUG(printk("scsi%ld: %s: Flags soft_rest=%d, config= %d\n ",
1284 ha->host_no, __func__, soft_reset, config_chip));
1285 if (soft_reset) {
1286 DEBUG(printk("scsi%ld: %s: Issue Soft Reset\n", ha->host_no,
1287 __func__));
1288 status = qla4xxx_soft_reset(ha);
1289 if (status == QLA_ERROR) {
1290 DEBUG(printk("scsi%d: %s: Soft Reset failed!\n",
1291 ha->host_no, __func__));
1292 ql4xxx_unlock_drvr(ha);
1293 return QLA_ERROR;
1294 }
1295 config_chip = 1;
1296
Joe Perchesb1c11812008-02-03 17:28:22 +02001297 /* Reset clears the semaphore, so acquire again */
David Somayajuluafaf5a22006-09-19 10:28:00 -07001298 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
1299 return QLA_ERROR;
1300 }
1301
1302 if (config_chip) {
1303 if ((status = qla4xxx_config_nvram(ha)) == QLA_SUCCESS)
1304 status = qla4xxx_start_firmware_from_flash(ha);
1305 }
1306
1307 ql4xxx_unlock_drvr(ha);
1308 if (status == QLA_SUCCESS) {
1309 qla4xxx_get_fw_version(ha);
1310 if (test_and_clear_bit(AF_GET_CRASH_RECORD, &ha->flags))
1311 qla4xxx_get_crash_record(ha);
1312 } else {
1313 DEBUG(printk("scsi%ld: %s: Firmware has NOT started\n",
1314 ha->host_no, __func__));
1315 }
1316 return status;
1317}
1318
1319
1320/**
1321 * qla4xxx_initialize_adapter - initiailizes hba
1322 * @ha: Pointer to host adapter structure.
1323 * @renew_ddb_list: Indicates what to do with the adapter's ddb list
1324 * after adapter recovery has completed.
1325 * 0=preserve ddb list, 1=destroy and rebuild ddb list
1326 *
1327 * This routine parforms all of the steps necessary to initialize the adapter.
1328 *
1329 **/
1330int qla4xxx_initialize_adapter(struct scsi_qla_host *ha,
1331 uint8_t renew_ddb_list)
1332{
1333 int status = QLA_ERROR;
1334 int8_t ip_address[IP_ADDR_LEN] = {0} ;
1335
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301336 clear_bit(AF_ONLINE, &ha->flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001337 ha->eeprom_cmd_data = 0;
1338
1339 qla4x00_pci_config(ha);
1340
1341 qla4xxx_disable_intrs(ha);
1342
1343 /* Initialize the Host adapter request/response queues and firmware */
1344 if (qla4xxx_start_firmware(ha) == QLA_ERROR)
David C Somayajulu46235e62007-06-08 17:37:16 -07001345 goto exit_init_hba;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001346
1347 if (qla4xxx_validate_mac_address(ha) == QLA_ERROR)
David C Somayajulu46235e62007-06-08 17:37:16 -07001348 goto exit_init_hba;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001349
1350 if (qla4xxx_init_local_data(ha) == QLA_ERROR)
David C Somayajulu46235e62007-06-08 17:37:16 -07001351 goto exit_init_hba;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001352
1353 status = qla4xxx_init_firmware(ha);
1354 if (status == QLA_ERROR)
David C Somayajulu46235e62007-06-08 17:37:16 -07001355 goto exit_init_hba;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001356
1357 /*
1358 * FW is waiting to get an IP address from DHCP server: Skip building
1359 * the ddb_list and wait for DHCP lease acquired aen to come in
1360 * followed by 0x8014 aen" to trigger the tgt discovery process.
1361 */
Vikas Chaudhary2a49a782010-04-28 11:37:07 +05301362 if (ha->firmware_state & FW_STATE_CONFIGURING_IP)
David C Somayajulu46235e62007-06-08 17:37:16 -07001363 goto exit_init_online;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001364
1365 /* Skip device discovery if ip and subnet is zero */
1366 if (memcmp(ha->ip_address, ip_address, IP_ADDR_LEN) == 0 ||
1367 memcmp(ha->subnet_mask, ip_address, IP_ADDR_LEN) == 0)
David C Somayajulu46235e62007-06-08 17:37:16 -07001368 goto exit_init_online;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001369
1370 if (renew_ddb_list == PRESERVE_DDB_LIST) {
1371 /*
1372 * We want to preserve lun states (i.e. suspended, etc.)
1373 * for recovery initiated by the driver. So just update
1374 * the device states for the existing ddb_list.
1375 */
1376 qla4xxx_reinitialize_ddb_list(ha);
1377 } else if (renew_ddb_list == REBUILD_DDB_LIST) {
1378 /*
1379 * We want to build the ddb_list from scratch during
1380 * driver initialization and recovery initiated by the
1381 * INT_HBA_RESET IOCTL.
1382 */
1383 status = qla4xxx_initialize_ddb_list(ha);
1384 if (status == QLA_ERROR) {
1385 DEBUG2(printk("%s(%ld) Error occurred during build"
1386 "ddb list\n", __func__, ha->host_no));
1387 goto exit_init_hba;
1388 }
1389
1390 }
1391 if (!ha->tot_ddbs) {
1392 DEBUG2(printk("scsi%ld: Failed to initialize devices or none "
1393 "present in Firmware device database\n",
1394 ha->host_no));
1395 }
1396
David C Somayajulu46235e62007-06-08 17:37:16 -07001397exit_init_online:
David C Somayajulu92b72732007-05-23 17:55:40 -07001398 set_bit(AF_ONLINE, &ha->flags);
David C Somayajulu46235e62007-06-08 17:37:16 -07001399exit_init_hba:
David Somayajuluafaf5a22006-09-19 10:28:00 -07001400 return status;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001401}
1402
1403/**
1404 * qla4xxx_add_device_dynamically - ddb addition due to an AEN
1405 * @ha: Pointer to host adapter structure.
1406 * @fw_ddb_index: Firmware's device database index
1407 *
1408 * This routine processes adds a device as a result of an 8014h AEN.
1409 **/
1410static void qla4xxx_add_device_dynamically(struct scsi_qla_host *ha,
1411 uint32_t fw_ddb_index)
1412{
1413 struct ddb_entry * ddb_entry;
David C Somayajulu92b72732007-05-23 17:55:40 -07001414 uint32_t new_tgt;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001415
1416 /* First allocate a device structure */
David C Somayajulu92b72732007-05-23 17:55:40 -07001417 ddb_entry = qla4xxx_get_ddb_entry(ha, fw_ddb_index, &new_tgt);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001418 if (ddb_entry == NULL) {
1419 DEBUG2(printk(KERN_WARNING
1420 "scsi%ld: Unable to allocate memory to add "
1421 "fw_ddb_index %d\n", ha->host_no, fw_ddb_index));
1422 return;
1423 }
1424
David C Somayajulu92b72732007-05-23 17:55:40 -07001425 if (!new_tgt && (ddb_entry->fw_ddb_index != fw_ddb_index)) {
1426 /* Target has been bound to a new fw_ddb_index */
1427 qla4xxx_free_ddb(ha, ddb_entry);
1428 ddb_entry = qla4xxx_alloc_ddb(ha, fw_ddb_index);
1429 if (ddb_entry == NULL) {
1430 DEBUG2(printk(KERN_WARNING
1431 "scsi%ld: Unable to allocate memory"
1432 " to add fw_ddb_index %d\n",
1433 ha->host_no, fw_ddb_index));
1434 return;
1435 }
1436 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001437 if (qla4xxx_update_ddb_entry(ha, ddb_entry, fw_ddb_index) ==
1438 QLA_ERROR) {
1439 ha->fw_ddb_index_map[fw_ddb_index] =
1440 (struct ddb_entry *)INVALID_ENTRY;
1441 DEBUG2(printk(KERN_WARNING
1442 "scsi%ld: failed to add new device at index "
1443 "[%d]\n Unable to retrieve fw ddb entry\n",
1444 ha->host_no, fw_ddb_index));
1445 qla4xxx_free_ddb(ha, ddb_entry);
1446 return;
1447 }
1448
1449 if (qla4xxx_add_sess(ddb_entry)) {
1450 DEBUG2(printk(KERN_WARNING
1451 "scsi%ld: failed to add new device at index "
1452 "[%d]\n Unable to add connection and session\n",
1453 ha->host_no, fw_ddb_index));
1454 qla4xxx_free_ddb(ha, ddb_entry);
1455 }
1456}
1457
1458/**
1459 * qla4xxx_process_ddb_changed - process ddb state change
1460 * @ha - Pointer to host adapter structure.
1461 * @fw_ddb_index - Firmware's device database index
1462 * @state - Device state
1463 *
1464 * This routine processes a Decive Database Changed AEN Event.
1465 **/
Vikas Chaudhary821d6e52010-04-28 11:41:21 +05301466int qla4xxx_process_ddb_changed(struct scsi_qla_host *ha, uint32_t fw_ddb_index,
1467 uint32_t state, uint32_t conn_err)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001468{
1469 struct ddb_entry * ddb_entry;
1470 uint32_t old_fw_ddb_device_state;
1471
1472 /* check for out of range index */
1473 if (fw_ddb_index >= MAX_DDB_ENTRIES)
1474 return QLA_ERROR;
1475
1476 /* Get the corresponging ddb entry */
1477 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, fw_ddb_index);
1478 /* Device does not currently exist in our database. */
1479 if (ddb_entry == NULL) {
1480 if (state == DDB_DS_SESSION_ACTIVE)
1481 qla4xxx_add_device_dynamically(ha, fw_ddb_index);
1482 return QLA_SUCCESS;
1483 }
1484
1485 /* Device already exists in our database. */
1486 old_fw_ddb_device_state = ddb_entry->fw_ddb_device_state;
1487 DEBUG2(printk("scsi%ld: %s DDB - old state= 0x%x, new state=0x%x for "
1488 "index [%d]\n", ha->host_no, __func__,
1489 ddb_entry->fw_ddb_device_state, state, fw_ddb_index));
1490 if (old_fw_ddb_device_state == state &&
1491 state == DDB_DS_SESSION_ACTIVE) {
Vikas Chaudharye349fa32010-07-10 14:48:36 +05301492 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
1493 atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
1494 iscsi_unblock_session(ddb_entry->sess);
1495 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001496 return QLA_SUCCESS;
1497 }
1498
1499 ddb_entry->fw_ddb_device_state = state;
1500 /* Device is back online. */
1501 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
Mike Christie50a29ae2008-03-04 13:26:53 -06001502 atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001503 atomic_set(&ddb_entry->port_down_timer,
1504 ha->port_down_retry_count);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001505 atomic_set(&ddb_entry->relogin_retry_count, 0);
1506 atomic_set(&ddb_entry->relogin_timer, 0);
1507 clear_bit(DF_RELOGIN, &ddb_entry->flags);
1508 clear_bit(DF_NO_RELOGIN, &ddb_entry->flags);
Mike Christieb6359302008-01-31 13:36:44 -06001509 iscsi_unblock_session(ddb_entry->sess);
Mike Christie26974782007-12-13 12:43:29 -06001510 iscsi_session_event(ddb_entry->sess,
1511 ISCSI_KEVENT_CREATE_SESSION);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001512 /*
1513 * Change the lun state to READY in case the lun TIMEOUT before
1514 * the device came back.
1515 */
1516 } else {
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301517 /* Device went away, mark device missing */
1518 if (atomic_read(&ddb_entry->state) == DDB_STATE_ONLINE) {
1519 DEBUG2(dev_info(&ha->pdev->dev, "%s mark missing "
1520 "ddb_entry 0x%p sess 0x%p conn 0x%p\n",
1521 __func__, ddb_entry,
1522 ddb_entry->sess, ddb_entry->conn));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001523 qla4xxx_mark_device_missing(ha, ddb_entry);
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301524 }
1525
David Somayajuluafaf5a22006-09-19 10:28:00 -07001526 /*
1527 * Relogin if device state changed to a not active state.
Vikas Chaudhary821d6e52010-04-28 11:41:21 +05301528 * However, do not relogin if a RELOGIN is in process, or
1529 * we are not allowed to relogin to this DDB.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001530 */
1531 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_FAILED &&
1532 !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
1533 !test_bit(DF_NO_RELOGIN, &ddb_entry->flags) &&
Vikas Chaudhary821d6e52010-04-28 11:41:21 +05301534 qla4_is_relogin_allowed(ha, conn_err)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001535 /*
1536 * This triggers a relogin. After the relogin_timer
1537 * expires, the relogin gets scheduled. We must wait a
1538 * minimum amount of time since receiving an 0x8014 AEN
1539 * with failed device_state or a logout response before
1540 * we can issue another relogin.
1541 */
Vikas Chaudhary821d6e52010-04-28 11:41:21 +05301542 /* Firmware pads this timeout: (time2wait +1).
David Somayajuluafaf5a22006-09-19 10:28:00 -07001543 * Driver retry to login should be longer than F/W.
1544 * Otherwise F/W will fail
1545 * set_ddb() mbx cmd with 0x4005 since it still
1546 * counting down its time2wait.
1547 */
1548 atomic_set(&ddb_entry->relogin_timer, 0);
1549 atomic_set(&ddb_entry->retry_relogin_timer,
1550 ddb_entry->default_time2wait + 4);
1551 }
1552 }
1553
1554 return QLA_SUCCESS;
1555}
1556