blob: 4a332c32d71e082887ee3dc0f5d56859549f2b9d [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__));
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530447 goto exit_get_ddb_entry_no_free;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700448 }
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) ==
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530453 QLA_ERROR) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700454 DEBUG2(printk("scsi%ld: %s: failed get_ddb_entry for "
455 "fw_ddb_index %d\n", ha->host_no, __func__,
456 fw_ddb_index));
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530457 goto exit_get_ddb_entry;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700458 }
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
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530475 /* if not found allocate new ddb */
David Somayajuluafaf5a22006-09-19 10:28:00 -0700476 if (!found) {
477 DEBUG2(printk("scsi%ld: %s: ddb[%d] not found - allocating "
478 "new ddb\n", ha->host_no, __func__,
479 fw_ddb_index));
David C Somayajulu92b72732007-05-23 17:55:40 -0700480 *new_tgt = 1;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700481 ddb_entry = qla4xxx_alloc_ddb(ha, fw_ddb_index);
482 }
483
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530484exit_get_ddb_entry:
David Somayajuluafaf5a22006-09-19 10:28:00 -0700485 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry), fw_ddb_entry,
486 fw_ddb_entry_dma);
487
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530488exit_get_ddb_entry_no_free:
David Somayajuluafaf5a22006-09-19 10:28:00 -0700489 return ddb_entry;
490}
491
492/**
493 * qla4xxx_update_ddb_entry - update driver's internal ddb
494 * @ha: pointer to host adapter structure.
495 * @ddb_entry: pointer to device database structure to be filled
496 * @fw_ddb_index: index of the ddb entry in fw ddb table
497 *
498 * This routine updates the driver's internal device database entry
499 * with information retrieved from the firmware's device database
500 * entry for the specified device. The ddb_entry->fw_ddb_index field
501 * must be initialized prior to calling this routine
502 *
503 **/
Adrian Bunk47975472007-04-26 00:35:16 -0700504static int qla4xxx_update_ddb_entry(struct scsi_qla_host *ha,
505 struct ddb_entry *ddb_entry,
506 uint32_t fw_ddb_index)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700507{
508 struct dev_db_entry *fw_ddb_entry = NULL;
509 dma_addr_t fw_ddb_entry_dma;
510 int status = QLA_ERROR;
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530511 uint32_t conn_err;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700512
513 if (ddb_entry == NULL) {
514 DEBUG2(printk("scsi%ld: %s: ddb_entry is NULL\n", ha->host_no,
515 __func__));
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530516
517 goto exit_update_ddb_no_free;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700518 }
519
520 /* Make sure the dma buffer is valid */
521 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev,
522 sizeof(*fw_ddb_entry),
523 &fw_ddb_entry_dma, GFP_KERNEL);
524 if (fw_ddb_entry == NULL) {
525 DEBUG2(printk("scsi%ld: %s: Unable to allocate dma buffer.\n",
526 ha->host_no, __func__));
527
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530528 goto exit_update_ddb_no_free;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700529 }
530
531 if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index, fw_ddb_entry,
532 fw_ddb_entry_dma, NULL, NULL,
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530533 &ddb_entry->fw_ddb_device_state, &conn_err,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700534 &ddb_entry->tcp_source_port_num,
535 &ddb_entry->connection_id) ==
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530536 QLA_ERROR) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700537 DEBUG2(printk("scsi%ld: %s: failed get_ddb_entry for "
538 "fw_ddb_index %d\n", ha->host_no, __func__,
539 fw_ddb_index));
540
541 goto exit_update_ddb;
542 }
543
544 status = QLA_SUCCESS;
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530545 ddb_entry->options = le16_to_cpu(fw_ddb_entry->options);
David C Somayajulu92b72732007-05-23 17:55:40 -0700546 ddb_entry->target_session_id = le16_to_cpu(fw_ddb_entry->tsid);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700547 ddb_entry->task_mgmt_timeout =
David C Somayajulu92b72732007-05-23 17:55:40 -0700548 le16_to_cpu(fw_ddb_entry->def_timeout);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700549 ddb_entry->CmdSn = 0;
David C Somayajulu92b72732007-05-23 17:55:40 -0700550 ddb_entry->exe_throttle = le16_to_cpu(fw_ddb_entry->exec_throttle);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700551 ddb_entry->default_relogin_timeout =
David C Somayajulu92b72732007-05-23 17:55:40 -0700552 le16_to_cpu(fw_ddb_entry->def_timeout);
553 ddb_entry->default_time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700554
555 /* Update index in case it changed */
556 ddb_entry->fw_ddb_index = fw_ddb_index;
557 ha->fw_ddb_index_map[fw_ddb_index] = ddb_entry;
558
David C Somayajulu92b72732007-05-23 17:55:40 -0700559 ddb_entry->port = le16_to_cpu(fw_ddb_entry->port);
560 ddb_entry->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
Mike Christie41bbdbe2009-01-16 12:36:52 -0600561 memcpy(ddb_entry->isid, fw_ddb_entry->isid, sizeof(ddb_entry->isid));
562
David C Somayajulu92b72732007-05-23 17:55:40 -0700563 memcpy(&ddb_entry->iscsi_name[0], &fw_ddb_entry->iscsi_name[0],
David Somayajuluafaf5a22006-09-19 10:28:00 -0700564 min(sizeof(ddb_entry->iscsi_name),
David C Somayajulu92b72732007-05-23 17:55:40 -0700565 sizeof(fw_ddb_entry->iscsi_name)));
566 memcpy(&ddb_entry->ip_addr[0], &fw_ddb_entry->ip_addr[0],
567 min(sizeof(ddb_entry->ip_addr), sizeof(fw_ddb_entry->ip_addr)));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700568
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530569 ddb_entry->iscsi_max_burst_len = fw_ddb_entry->iscsi_max_burst_len;
570 ddb_entry->iscsi_max_outsnd_r2t = fw_ddb_entry->iscsi_max_outsnd_r2t;
571 ddb_entry->iscsi_first_burst_len = fw_ddb_entry->iscsi_first_burst_len;
572 ddb_entry->iscsi_max_rcv_data_seg_len =
573 fw_ddb_entry->iscsi_max_rcv_data_seg_len;
574 ddb_entry->iscsi_max_snd_data_seg_len =
575 fw_ddb_entry->iscsi_max_snd_data_seg_len;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700576
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530577 if (ddb_entry->options & DDB_OPT_IPV6_DEVICE) {
578 memcpy(&ddb_entry->remote_ipv6_addr,
579 fw_ddb_entry->ip_addr,
580 min(sizeof(ddb_entry->remote_ipv6_addr),
581 sizeof(fw_ddb_entry->ip_addr)));
582 memcpy(&ddb_entry->link_local_ipv6_addr,
583 fw_ddb_entry->link_local_ipv6_addr,
584 min(sizeof(ddb_entry->link_local_ipv6_addr),
585 sizeof(fw_ddb_entry->link_local_ipv6_addr)));
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530586
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530587 DEBUG2(dev_info(&ha->pdev->dev, "%s: DDB[%d] osIdx = %d "
588 "State %04x ConnErr %08x IP %pI6 "
589 ":%04d \"%s\"\n",
590 __func__, fw_ddb_index,
591 ddb_entry->os_target_id,
592 ddb_entry->fw_ddb_device_state,
593 conn_err, fw_ddb_entry->ip_addr,
594 le16_to_cpu(fw_ddb_entry->port),
595 fw_ddb_entry->iscsi_name));
596 } else
597 DEBUG2(dev_info(&ha->pdev->dev, "%s: DDB[%d] osIdx = %d "
598 "State %04x ConnErr %08x IP %pI4 "
599 ":%04d \"%s\"\n",
600 __func__, fw_ddb_index,
601 ddb_entry->os_target_id,
602 ddb_entry->fw_ddb_device_state,
603 conn_err, fw_ddb_entry->ip_addr,
604 le16_to_cpu(fw_ddb_entry->port),
605 fw_ddb_entry->iscsi_name));
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530606exit_update_ddb:
David Somayajuluafaf5a22006-09-19 10:28:00 -0700607 if (fw_ddb_entry)
608 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
609 fw_ddb_entry, fw_ddb_entry_dma);
610
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530611exit_update_ddb_no_free:
David Somayajuluafaf5a22006-09-19 10:28:00 -0700612 return status;
613}
614
615/**
616 * qla4xxx_alloc_ddb - allocate device database entry
617 * @ha: Pointer to host adapter structure.
618 * @fw_ddb_index: Firmware's device database index
619 *
620 * This routine allocates a ddb_entry, ititializes some values, and
621 * inserts it into the ddb list.
622 **/
Adrian Bunk47975472007-04-26 00:35:16 -0700623static struct ddb_entry * qla4xxx_alloc_ddb(struct scsi_qla_host *ha,
624 uint32_t fw_ddb_index)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700625{
626 struct ddb_entry *ddb_entry;
627
628 DEBUG2(printk("scsi%ld: %s: fw_ddb_index [%d]\n", ha->host_no,
629 __func__, fw_ddb_index));
630
631 ddb_entry = qla4xxx_alloc_sess(ha);
632 if (ddb_entry == NULL) {
633 DEBUG2(printk("scsi%ld: %s: Unable to allocate memory "
634 "to add fw_ddb_index [%d]\n",
635 ha->host_no, __func__, fw_ddb_index));
636 return ddb_entry;
637 }
638
639 ddb_entry->fw_ddb_index = fw_ddb_index;
640 atomic_set(&ddb_entry->port_down_timer, ha->port_down_retry_count);
641 atomic_set(&ddb_entry->retry_relogin_timer, INVALID_ENTRY);
642 atomic_set(&ddb_entry->relogin_timer, 0);
643 atomic_set(&ddb_entry->relogin_retry_count, 0);
644 atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
645 list_add_tail(&ddb_entry->list, &ha->ddb_list);
646 ha->fw_ddb_index_map[fw_ddb_index] = ddb_entry;
647 ha->tot_ddbs++;
648
649 return ddb_entry;
650}
651
652/**
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530653 * qla4_is_relogin_allowed - Are we allowed to login?
654 * @ha: Pointer to host adapter structure.
655 * @conn_err: Last connection error associated with the ddb
656 *
657 * This routine tests the given connection error to determine if
658 * we are allowed to login.
659 **/
660int qla4_is_relogin_allowed(struct scsi_qla_host *ha, uint32_t conn_err)
661{
662 uint32_t err_code, login_rsp_sts_class;
663 int relogin = 1;
664
665 err_code = ((conn_err & 0x00ff0000) >> 16);
666 login_rsp_sts_class = ((conn_err & 0x0000ff00) >> 8);
667 if (err_code == 0x1c || err_code == 0x06) {
668 DEBUG2(dev_info(&ha->pdev->dev,
669 ": conn_err=0x%08x, send target completed"
670 " or access denied failure\n", conn_err));
671 relogin = 0;
672 }
673 if ((err_code == 0x08) && (login_rsp_sts_class == 0x02)) {
674 /* Login Response PDU returned an error.
675 Login Response Status in Error Code Detail
676 indicates login should not be retried.*/
677 DEBUG2(dev_info(&ha->pdev->dev,
678 ": conn_err=0x%08x, do not retry relogin\n",
679 conn_err));
680 relogin = 0;
681 }
682
683 return relogin;
684}
685
686/**
David Somayajuluafaf5a22006-09-19 10:28:00 -0700687 * qla4xxx_configure_ddbs - builds driver ddb list
688 * @ha: Pointer to host adapter structure.
689 *
690 * This routine searches for all valid firmware ddb entries and builds
691 * an internal ddb list. Ddbs that are considered valid are those with
692 * a device state of SESSION_ACTIVE.
693 **/
694static int qla4xxx_build_ddb_list(struct scsi_qla_host *ha)
695{
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530696 int status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700697 uint32_t fw_ddb_index = 0;
698 uint32_t next_fw_ddb_index = 0;
699 uint32_t ddb_state;
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530700 uint32_t conn_err;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700701 struct ddb_entry *ddb_entry;
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530702 struct dev_db_entry *fw_ddb_entry = NULL;
703 dma_addr_t fw_ddb_entry_dma;
704 uint32_t ipv6_device;
David C Somayajulu92b72732007-05-23 17:55:40 -0700705 uint32_t new_tgt;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700706
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530707 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
708 &fw_ddb_entry_dma, GFP_KERNEL);
709 if (fw_ddb_entry == NULL) {
710 DEBUG2(dev_info(&ha->pdev->dev, "%s: DMA alloc failed\n",
711 __func__));
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530712
713 goto exit_build_ddb_list_no_free;
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530714 }
715
David Somayajuluafaf5a22006-09-19 10:28:00 -0700716 dev_info(&ha->pdev->dev, "Initializing DDBs ...\n");
717 for (fw_ddb_index = 0; fw_ddb_index < MAX_DDB_ENTRIES;
718 fw_ddb_index = next_fw_ddb_index) {
719 /* First, let's see if a device exists here */
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530720 if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index, fw_ddb_entry,
721 0, NULL, &next_fw_ddb_index,
722 &ddb_state, &conn_err,
723 NULL, NULL) ==
724 QLA_ERROR) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700725 DEBUG2(printk("scsi%ld: %s: get_ddb_entry, "
726 "fw_ddb_index %d failed", ha->host_no,
727 __func__, fw_ddb_index));
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530728 goto exit_build_ddb_list;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700729 }
730
731 DEBUG2(printk("scsi%ld: %s: Getting DDB[%d] ddbstate=0x%x, "
732 "next_fw_ddb_index=%d.\n", ha->host_no, __func__,
733 fw_ddb_index, ddb_state, next_fw_ddb_index));
734
735 /* Issue relogin, if necessary. */
736 if (ddb_state == DDB_DS_SESSION_FAILED ||
737 ddb_state == DDB_DS_NO_CONNECTION_ACTIVE) {
738 /* Try and login to device */
739 DEBUG2(printk("scsi%ld: %s: Login to DDB[%d]\n",
740 ha->host_no, __func__, fw_ddb_index));
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530741 ipv6_device = le16_to_cpu(fw_ddb_entry->options) &
742 DDB_OPT_IPV6_DEVICE;
743 if (qla4_is_relogin_allowed(ha, conn_err) &&
744 ((!ipv6_device &&
745 *((uint32_t *)fw_ddb_entry->ip_addr))
746 || ipv6_device)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700747 qla4xxx_set_ddb_entry(ha, fw_ddb_index, 0);
David C Somayajulu92b72732007-05-23 17:55:40 -0700748 if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index,
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530749 NULL, 0, NULL,
750 &next_fw_ddb_index,
751 &ddb_state, &conn_err,
752 NULL, NULL)
753 == QLA_ERROR) {
David C Somayajulu92b72732007-05-23 17:55:40 -0700754 DEBUG2(printk("scsi%ld: %s:"
755 "get_ddb_entry %d failed\n",
756 ha->host_no,
757 __func__, fw_ddb_index));
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530758 goto exit_build_ddb_list;
David C Somayajulu92b72732007-05-23 17:55:40 -0700759 }
760 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700761 }
762
763 if (ddb_state != DDB_DS_SESSION_ACTIVE)
764 goto next_one;
765 /*
766 * if fw_ddb with session active state found,
767 * add to ddb_list
768 */
769 DEBUG2(printk("scsi%ld: %s: DDB[%d] added to list\n",
770 ha->host_no, __func__, fw_ddb_index));
771
772 /* Add DDB to internal our ddb list. */
David C Somayajulu92b72732007-05-23 17:55:40 -0700773 ddb_entry = qla4xxx_get_ddb_entry(ha, fw_ddb_index, &new_tgt);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700774 if (ddb_entry == NULL) {
775 DEBUG2(printk("scsi%ld: %s: Unable to allocate memory "
776 "for device at fw_ddb_index %d\n",
777 ha->host_no, __func__, fw_ddb_index));
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530778 goto exit_build_ddb_list;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700779 }
780 /* Fill in the device structure */
781 if (qla4xxx_update_ddb_entry(ha, ddb_entry, fw_ddb_index) ==
782 QLA_ERROR) {
783 ha->fw_ddb_index_map[fw_ddb_index] =
784 (struct ddb_entry *)INVALID_ENTRY;
785
David Somayajuluafaf5a22006-09-19 10:28:00 -0700786 DEBUG2(printk("scsi%ld: %s: update_ddb_entry failed "
787 "for fw_ddb_index %d.\n",
788 ha->host_no, __func__, fw_ddb_index));
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530789 goto exit_build_ddb_list;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700790 }
791
792next_one:
793 /* We know we've reached the last device when
794 * next_fw_ddb_index is 0 */
795 if (next_fw_ddb_index == 0)
796 break;
797 }
798
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530799 status = QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700800 dev_info(&ha->pdev->dev, "DDB list done..\n");
801
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530802exit_build_ddb_list:
803 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry), fw_ddb_entry,
804 fw_ddb_entry_dma);
805
806exit_build_ddb_list_no_free:
David Somayajuluafaf5a22006-09-19 10:28:00 -0700807 return status;
808}
809
810struct qla4_relog_scan {
811 int halt_wait;
812 uint32_t conn_err;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700813 uint32_t fw_ddb_index;
814 uint32_t next_fw_ddb_index;
815 uint32_t fw_ddb_device_state;
816};
817
818static int qla4_test_rdy(struct scsi_qla_host *ha, struct qla4_relog_scan *rs)
819{
820 struct ddb_entry *ddb_entry;
821
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530822 if (qla4_is_relogin_allowed(ha, rs->conn_err)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700823 /* We either have a device that is in
824 * the process of relogging in or a
825 * device that is waiting to be
826 * relogged in */
827 rs->halt_wait = 0;
828
829 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha,
830 rs->fw_ddb_index);
831 if (ddb_entry == NULL)
832 return QLA_ERROR;
833
834 if (ddb_entry->dev_scan_wait_to_start_relogin != 0
835 && time_after_eq(jiffies,
836 ddb_entry->
837 dev_scan_wait_to_start_relogin))
838 {
839 ddb_entry->dev_scan_wait_to_start_relogin = 0;
840 qla4xxx_set_ddb_entry(ha, rs->fw_ddb_index, 0);
841 }
842 }
843 return QLA_SUCCESS;
844}
845
846static int qla4_scan_for_relogin(struct scsi_qla_host *ha,
847 struct qla4_relog_scan *rs)
848{
849 int error;
850
851 /* scan for relogins
852 * ----------------- */
853 for (rs->fw_ddb_index = 0; rs->fw_ddb_index < MAX_DDB_ENTRIES;
854 rs->fw_ddb_index = rs->next_fw_ddb_index) {
855 if (qla4xxx_get_fwddb_entry(ha, rs->fw_ddb_index, NULL, 0,
856 NULL, &rs->next_fw_ddb_index,
857 &rs->fw_ddb_device_state,
858 &rs->conn_err, NULL, NULL)
859 == QLA_ERROR)
860 return QLA_ERROR;
861
862 if (rs->fw_ddb_device_state == DDB_DS_LOGIN_IN_PROCESS)
863 rs->halt_wait = 0;
864
865 if (rs->fw_ddb_device_state == DDB_DS_SESSION_FAILED ||
866 rs->fw_ddb_device_state == DDB_DS_NO_CONNECTION_ACTIVE) {
867 error = qla4_test_rdy(ha, rs);
868 if (error)
869 return error;
870 }
871
872 /* We know we've reached the last device when
873 * next_fw_ddb_index is 0 */
874 if (rs->next_fw_ddb_index == 0)
875 break;
876 }
877 return QLA_SUCCESS;
878}
879
880/**
881 * qla4xxx_devices_ready - wait for target devices to be logged in
882 * @ha: pointer to adapter structure
883 *
884 * This routine waits up to ql4xdiscoverywait seconds
885 * F/W database during driver load time.
886 **/
887static int qla4xxx_devices_ready(struct scsi_qla_host *ha)
888{
889 int error;
890 unsigned long discovery_wtime;
891 struct qla4_relog_scan rs;
892
893 discovery_wtime = jiffies + (ql4xdiscoverywait * HZ);
894
895 DEBUG(printk("Waiting (%d) for devices ...\n", ql4xdiscoverywait));
896 do {
897 /* poll for AEN. */
898 qla4xxx_get_firmware_state(ha);
899 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags)) {
900 /* Set time-between-relogin timer */
901 qla4xxx_process_aen(ha, RELOGIN_DDB_CHANGED_AENS);
902 }
903
904 /* if no relogins active or needed, halt discvery wait */
905 rs.halt_wait = 1;
906
907 error = qla4_scan_for_relogin(ha, &rs);
908
909 if (rs.halt_wait) {
910 DEBUG2(printk("scsi%ld: %s: Delay halted. Devices "
911 "Ready.\n", ha->host_no, __func__));
912 return QLA_SUCCESS;
913 }
914
915 msleep(2000);
916 } while (!time_after_eq(jiffies, discovery_wtime));
917
918 DEBUG3(qla4xxx_get_conn_event_log(ha));
919
920 return QLA_SUCCESS;
921}
922
923static void qla4xxx_flush_AENS(struct scsi_qla_host *ha)
924{
925 unsigned long wtime;
926
927 /* Flush the 0x8014 AEN from the firmware as a result of
928 * Auto connect. We are basically doing get_firmware_ddb()
929 * to determine whether we need to log back in or not.
930 * Trying to do a set ddb before we have processed 0x8014
931 * will result in another set_ddb() for the same ddb. In other
932 * words there will be stale entries in the aen_q.
933 */
934 wtime = jiffies + (2 * HZ);
935 do {
936 if (qla4xxx_get_firmware_state(ha) == QLA_SUCCESS)
937 if (ha->firmware_state & (BIT_2 | BIT_0))
938 return;
939
940 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
941 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
942
943 msleep(1000);
944 } while (!time_after_eq(jiffies, wtime));
945
946}
947
948static int qla4xxx_initialize_ddb_list(struct scsi_qla_host *ha)
949{
950 uint16_t fw_ddb_index;
951 int status = QLA_SUCCESS;
952
953 /* free the ddb list if is not empty */
954 if (!list_empty(&ha->ddb_list))
955 qla4xxx_free_ddb_list(ha);
956
957 for (fw_ddb_index = 0; fw_ddb_index < MAX_DDB_ENTRIES; fw_ddb_index++)
958 ha->fw_ddb_index_map[fw_ddb_index] =
959 (struct ddb_entry *)INVALID_ENTRY;
960
961 ha->tot_ddbs = 0;
962
963 qla4xxx_flush_AENS(ha);
964
965 /*
966 * First perform device discovery for active
967 * fw ddb indexes and build
968 * ddb list.
969 */
970 if ((status = qla4xxx_build_ddb_list(ha)) == QLA_ERROR)
971 return status;
972
973 /* Wait for an AEN */
974 qla4xxx_devices_ready(ha);
975
976 /*
977 * Targets can come online after the inital discovery, so processing
978 * the aens here will catch them.
979 */
980 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
981 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
982
983 return status;
984}
985
986/**
987 * qla4xxx_update_ddb_list - update the driver ddb list
988 * @ha: pointer to host adapter structure.
989 *
990 * This routine obtains device information from the F/W database after
991 * firmware or adapter resets. The device table is preserved.
992 **/
993int qla4xxx_reinitialize_ddb_list(struct scsi_qla_host *ha)
994{
995 int status = QLA_SUCCESS;
996 struct ddb_entry *ddb_entry, *detemp;
997
998 /* Update the device information for all devices. */
999 list_for_each_entry_safe(ddb_entry, detemp, &ha->ddb_list, list) {
1000 qla4xxx_update_ddb_entry(ha, ddb_entry,
1001 ddb_entry->fw_ddb_index);
1002 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
1003 atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
1004 DEBUG2(printk ("scsi%ld: %s: ddb index [%d] marked "
1005 "ONLINE\n", ha->host_no, __func__,
1006 ddb_entry->fw_ddb_index));
Vikas Chaudhary36386322010-07-10 14:49:01 +05301007 iscsi_unblock_session(ddb_entry->sess);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001008 } else if (atomic_read(&ddb_entry->state) == DDB_STATE_ONLINE)
1009 qla4xxx_mark_device_missing(ha, ddb_entry);
1010 }
1011 return status;
1012}
1013
1014/**
1015 * qla4xxx_relogin_device - re-establish session
1016 * @ha: Pointer to host adapter structure.
1017 * @ddb_entry: Pointer to device database entry
1018 *
1019 * This routine does a session relogin with the specified device.
1020 * The ddb entry must be assigned prior to making this call.
1021 **/
1022int qla4xxx_relogin_device(struct scsi_qla_host *ha,
1023 struct ddb_entry * ddb_entry)
1024{
1025 uint16_t relogin_timer;
1026
1027 relogin_timer = max(ddb_entry->default_relogin_timeout,
1028 (uint16_t)RELOGIN_TOV);
1029 atomic_set(&ddb_entry->relogin_timer, relogin_timer);
1030
1031 DEBUG2(printk("scsi%ld: Relogin index [%d]. TOV=%d\n", ha->host_no,
1032 ddb_entry->fw_ddb_index, relogin_timer));
1033
1034 qla4xxx_set_ddb_entry(ha, ddb_entry->fw_ddb_index, 0);
1035
1036 return QLA_SUCCESS;
1037}
1038
David Somayajuluafaf5a22006-09-19 10:28:00 -07001039static int qla4xxx_config_nvram(struct scsi_qla_host *ha)
1040{
1041 unsigned long flags;
1042 union external_hw_config_reg extHwConfig;
1043
1044 DEBUG2(printk("scsi%ld: %s: Get EEProm parameters \n", ha->host_no,
1045 __func__));
1046 if (ql4xxx_lock_flash(ha) != QLA_SUCCESS)
Mike Christieb3925512010-02-10 16:51:48 -06001047 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001048 if (ql4xxx_lock_nvram(ha) != QLA_SUCCESS) {
1049 ql4xxx_unlock_flash(ha);
Mike Christieb3925512010-02-10 16:51:48 -06001050 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001051 }
1052
1053 /* Get EEPRom Parameters from NVRAM and validate */
1054 dev_info(&ha->pdev->dev, "Configuring NVRAM ...\n");
1055 if (qla4xxx_is_nvram_configuration_valid(ha) == QLA_SUCCESS) {
1056 spin_lock_irqsave(&ha->hardware_lock, flags);
1057 extHwConfig.Asuint32_t =
1058 rd_nvram_word(ha, eeprom_ext_hw_conf_offset(ha));
1059 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1060 } else {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001061 dev_warn(&ha->pdev->dev,
1062 "scsi%ld: %s: EEProm checksum invalid. "
1063 "Please update your EEPROM\n", ha->host_no,
1064 __func__);
1065
Mike Christieb3925512010-02-10 16:51:48 -06001066 /* Attempt to set defaults */
David Somayajuluafaf5a22006-09-19 10:28:00 -07001067 if (is_qla4010(ha))
1068 extHwConfig.Asuint32_t = 0x1912;
David C Somayajulud9150582006-11-15 17:38:40 -08001069 else if (is_qla4022(ha) | is_qla4032(ha))
David Somayajuluafaf5a22006-09-19 10:28:00 -07001070 extHwConfig.Asuint32_t = 0x0023;
Mike Christieb3925512010-02-10 16:51:48 -06001071 else
1072 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001073 }
1074 DEBUG(printk("scsi%ld: %s: Setting extHwConfig to 0xFFFF%04x\n",
1075 ha->host_no, __func__, extHwConfig.Asuint32_t));
1076
1077 spin_lock_irqsave(&ha->hardware_lock, flags);
1078 writel((0xFFFF << 16) | extHwConfig.Asuint32_t, isp_ext_hw_conf(ha));
1079 readl(isp_ext_hw_conf(ha));
1080 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1081
1082 ql4xxx_unlock_nvram(ha);
1083 ql4xxx_unlock_flash(ha);
1084
Mike Christieb3925512010-02-10 16:51:48 -06001085 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001086}
1087
1088static void qla4x00_pci_config(struct scsi_qla_host *ha)
1089{
David C Somayajulu92b72732007-05-23 17:55:40 -07001090 uint16_t w;
David C Somayajulu46235e62007-06-08 17:37:16 -07001091 int status;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001092
1093 dev_info(&ha->pdev->dev, "Configuring PCI space...\n");
1094
1095 pci_set_master(ha->pdev);
David C Somayajulu46235e62007-06-08 17:37:16 -07001096 status = pci_set_mwi(ha->pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001097 /*
1098 * We want to respect framework's setting of PCI configuration space
1099 * command register and also want to make sure that all bits of
1100 * interest to us are properly set in command register.
1101 */
1102 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
David C Somayajulu92b72732007-05-23 17:55:40 -07001103 w |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001104 w &= ~PCI_COMMAND_INTX_DISABLE;
1105 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
1106}
1107
1108static int qla4xxx_start_firmware_from_flash(struct scsi_qla_host *ha)
1109{
1110 int status = QLA_ERROR;
Vikas Chaudharyc301b022010-04-28 11:40:37 +05301111 unsigned long max_wait_time;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001112 unsigned long flags;
1113 uint32_t mbox_status;
1114
1115 dev_info(&ha->pdev->dev, "Starting firmware ...\n");
1116
1117 /*
1118 * Start firmware from flash ROM
1119 *
1120 * WORKAROUND: Stuff a non-constant value that the firmware can
1121 * use as a seed for a random number generator in MB7 prior to
1122 * setting BOOT_ENABLE. Fixes problem where the TCP
1123 * connections use the same TCP ports after each reboot,
1124 * causing some connections to not get re-established.
1125 */
1126 DEBUG(printk("scsi%d: %s: Start firmware from flash ROM\n",
1127 ha->host_no, __func__));
1128
1129 spin_lock_irqsave(&ha->hardware_lock, flags);
1130 writel(jiffies, &ha->reg->mailbox[7]);
David C Somayajulud9150582006-11-15 17:38:40 -08001131 if (is_qla4022(ha) | is_qla4032(ha))
David Somayajuluafaf5a22006-09-19 10:28:00 -07001132 writel(set_rmask(NVR_WRITE_ENABLE),
1133 &ha->reg->u1.isp4022.nvram);
1134
David C Somayajulu92b72732007-05-23 17:55:40 -07001135 writel(2, &ha->reg->mailbox[6]);
1136 readl(&ha->reg->mailbox[6]);
1137
David Somayajuluafaf5a22006-09-19 10:28:00 -07001138 writel(set_rmask(CSR_BOOT_ENABLE), &ha->reg->ctrl_status);
1139 readl(&ha->reg->ctrl_status);
1140 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1141
1142 /* Wait for firmware to come UP. */
Vikas Chaudharyc301b022010-04-28 11:40:37 +05301143 DEBUG2(printk(KERN_INFO "scsi%ld: %s: Wait up to %d seconds for "
1144 "boot firmware to complete...\n",
1145 ha->host_no, __func__, FIRMWARE_UP_TOV));
1146 max_wait_time = jiffies + (FIRMWARE_UP_TOV * HZ);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001147 do {
1148 uint32_t ctrl_status;
1149
1150 spin_lock_irqsave(&ha->hardware_lock, flags);
1151 ctrl_status = readw(&ha->reg->ctrl_status);
1152 mbox_status = readw(&ha->reg->mailbox[0]);
1153 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1154
1155 if (ctrl_status & set_rmask(CSR_SCSI_PROCESSOR_INTR))
1156 break;
1157 if (mbox_status == MBOX_STS_COMMAND_COMPLETE)
1158 break;
1159
Vikas Chaudharyc301b022010-04-28 11:40:37 +05301160 DEBUG2(printk(KERN_INFO "scsi%ld: %s: Waiting for boot "
1161 "firmware to complete... ctrl_sts=0x%x\n",
1162 ha->host_no, __func__, ctrl_status));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001163
Vikas Chaudharyc301b022010-04-28 11:40:37 +05301164 msleep_interruptible(250);
1165 } while (!time_after_eq(jiffies, max_wait_time));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001166
1167 if (mbox_status == MBOX_STS_COMMAND_COMPLETE) {
Vikas Chaudharyc301b022010-04-28 11:40:37 +05301168 DEBUG(printk(KERN_INFO "scsi%ld: %s: Firmware has started\n",
David Somayajuluafaf5a22006-09-19 10:28:00 -07001169 ha->host_no, __func__));
1170
1171 spin_lock_irqsave(&ha->hardware_lock, flags);
1172 writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
1173 &ha->reg->ctrl_status);
1174 readl(&ha->reg->ctrl_status);
1175 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1176
1177 status = QLA_SUCCESS;
1178 } else {
1179 printk(KERN_INFO "scsi%ld: %s: Boot firmware failed "
1180 "- mbox status 0x%x\n", ha->host_no, __func__,
1181 mbox_status);
1182 status = QLA_ERROR;
1183 }
1184 return status;
1185}
1186
David C Somayajulu92b72732007-05-23 17:55:40 -07001187int ql4xxx_lock_drvr_wait(struct scsi_qla_host *a)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001188{
David C Somayajulu92b72732007-05-23 17:55:40 -07001189#define QL4_LOCK_DRVR_WAIT 60
David C Somayajulu477ffb92007-01-22 12:26:11 -08001190#define QL4_LOCK_DRVR_SLEEP 1
David Somayajuluafaf5a22006-09-19 10:28:00 -07001191
1192 int drvr_wait = QL4_LOCK_DRVR_WAIT;
1193 while (drvr_wait) {
David C Somayajulu92b72732007-05-23 17:55:40 -07001194 if (ql4xxx_lock_drvr(a) == 0) {
David C Somayajulu477ffb92007-01-22 12:26:11 -08001195 ssleep(QL4_LOCK_DRVR_SLEEP);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001196 if (drvr_wait) {
1197 DEBUG2(printk("scsi%ld: %s: Waiting for "
David C Somayajulu92b72732007-05-23 17:55:40 -07001198 "Global Init Semaphore(%d)...\n",
1199 a->host_no,
David C Somayajulu477ffb92007-01-22 12:26:11 -08001200 __func__, drvr_wait));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001201 }
1202 drvr_wait -= QL4_LOCK_DRVR_SLEEP;
1203 } else {
1204 DEBUG2(printk("scsi%ld: %s: Global Init Semaphore "
David C Somayajulu92b72732007-05-23 17:55:40 -07001205 "acquired\n", a->host_no, __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001206 return QLA_SUCCESS;
1207 }
1208 }
1209 return QLA_ERROR;
1210}
1211
1212/**
1213 * qla4xxx_start_firmware - starts qla4xxx firmware
1214 * @ha: Pointer to host adapter structure.
1215 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001216 * This routine performs the necessary steps to start the firmware for
David Somayajuluafaf5a22006-09-19 10:28:00 -07001217 * the QLA4010 adapter.
1218 **/
1219static int qla4xxx_start_firmware(struct scsi_qla_host *ha)
1220{
1221 unsigned long flags = 0;
1222 uint32_t mbox_status;
1223 int status = QLA_ERROR;
1224 int soft_reset = 1;
1225 int config_chip = 0;
1226
David C Somayajulud9150582006-11-15 17:38:40 -08001227 if (is_qla4022(ha) | is_qla4032(ha))
David Somayajuluafaf5a22006-09-19 10:28:00 -07001228 ql4xxx_set_mac_number(ha);
1229
1230 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
1231 return QLA_ERROR;
1232
1233 spin_lock_irqsave(&ha->hardware_lock, flags);
1234
1235 DEBUG2(printk("scsi%ld: %s: port_ctrl = 0x%08X\n", ha->host_no,
1236 __func__, readw(isp_port_ctrl(ha))));
1237 DEBUG(printk("scsi%ld: %s: port_status = 0x%08X\n", ha->host_no,
1238 __func__, readw(isp_port_status(ha))));
1239
1240 /* Is Hardware already initialized? */
1241 if ((readw(isp_port_ctrl(ha)) & 0x8000) != 0) {
1242 DEBUG(printk("scsi%ld: %s: Hardware has already been "
1243 "initialized\n", ha->host_no, __func__));
1244
1245 /* Receive firmware boot acknowledgement */
1246 mbox_status = readw(&ha->reg->mailbox[0]);
1247
1248 DEBUG2(printk("scsi%ld: %s: H/W Config complete - mbox[0]= "
1249 "0x%x\n", ha->host_no, __func__, mbox_status));
1250
1251 /* Is firmware already booted? */
1252 if (mbox_status == 0) {
1253 /* F/W not running, must be config by net driver */
1254 config_chip = 1;
1255 soft_reset = 0;
1256 } else {
1257 writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
1258 &ha->reg->ctrl_status);
1259 readl(&ha->reg->ctrl_status);
1260 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1261 if (qla4xxx_get_firmware_state(ha) == QLA_SUCCESS) {
1262 DEBUG2(printk("scsi%ld: %s: Get firmware "
1263 "state -- state = 0x%x\n",
1264 ha->host_no,
1265 __func__, ha->firmware_state));
1266 /* F/W is running */
1267 if (ha->firmware_state &
1268 FW_STATE_CONFIG_WAIT) {
1269 DEBUG2(printk("scsi%ld: %s: Firmware "
1270 "in known state -- "
1271 "config and "
1272 "boot, state = 0x%x\n",
1273 ha->host_no, __func__,
1274 ha->firmware_state));
1275 config_chip = 1;
1276 soft_reset = 0;
1277 }
1278 } else {
1279 DEBUG2(printk("scsi%ld: %s: Firmware in "
1280 "unknown state -- resetting,"
1281 " state = "
1282 "0x%x\n", ha->host_no, __func__,
1283 ha->firmware_state));
1284 }
1285 spin_lock_irqsave(&ha->hardware_lock, flags);
1286 }
1287 } else {
1288 DEBUG(printk("scsi%ld: %s: H/W initialization hasn't been "
1289 "started - resetting\n", ha->host_no, __func__));
1290 }
1291 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1292
1293 DEBUG(printk("scsi%ld: %s: Flags soft_rest=%d, config= %d\n ",
1294 ha->host_no, __func__, soft_reset, config_chip));
1295 if (soft_reset) {
1296 DEBUG(printk("scsi%ld: %s: Issue Soft Reset\n", ha->host_no,
1297 __func__));
1298 status = qla4xxx_soft_reset(ha);
1299 if (status == QLA_ERROR) {
1300 DEBUG(printk("scsi%d: %s: Soft Reset failed!\n",
1301 ha->host_no, __func__));
1302 ql4xxx_unlock_drvr(ha);
1303 return QLA_ERROR;
1304 }
1305 config_chip = 1;
1306
Joe Perchesb1c11812008-02-03 17:28:22 +02001307 /* Reset clears the semaphore, so acquire again */
David Somayajuluafaf5a22006-09-19 10:28:00 -07001308 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
1309 return QLA_ERROR;
1310 }
1311
1312 if (config_chip) {
1313 if ((status = qla4xxx_config_nvram(ha)) == QLA_SUCCESS)
1314 status = qla4xxx_start_firmware_from_flash(ha);
1315 }
1316
1317 ql4xxx_unlock_drvr(ha);
1318 if (status == QLA_SUCCESS) {
1319 qla4xxx_get_fw_version(ha);
1320 if (test_and_clear_bit(AF_GET_CRASH_RECORD, &ha->flags))
1321 qla4xxx_get_crash_record(ha);
1322 } else {
1323 DEBUG(printk("scsi%ld: %s: Firmware has NOT started\n",
1324 ha->host_no, __func__));
1325 }
1326 return status;
1327}
1328
1329
1330/**
1331 * qla4xxx_initialize_adapter - initiailizes hba
1332 * @ha: Pointer to host adapter structure.
1333 * @renew_ddb_list: Indicates what to do with the adapter's ddb list
1334 * after adapter recovery has completed.
1335 * 0=preserve ddb list, 1=destroy and rebuild ddb list
1336 *
1337 * This routine parforms all of the steps necessary to initialize the adapter.
1338 *
1339 **/
1340int qla4xxx_initialize_adapter(struct scsi_qla_host *ha,
1341 uint8_t renew_ddb_list)
1342{
1343 int status = QLA_ERROR;
1344 int8_t ip_address[IP_ADDR_LEN] = {0} ;
1345
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301346 clear_bit(AF_ONLINE, &ha->flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001347 ha->eeprom_cmd_data = 0;
1348
1349 qla4x00_pci_config(ha);
1350
1351 qla4xxx_disable_intrs(ha);
1352
1353 /* Initialize the Host adapter request/response queues and firmware */
1354 if (qla4xxx_start_firmware(ha) == QLA_ERROR)
David C Somayajulu46235e62007-06-08 17:37:16 -07001355 goto exit_init_hba;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001356
1357 if (qla4xxx_validate_mac_address(ha) == QLA_ERROR)
David C Somayajulu46235e62007-06-08 17:37:16 -07001358 goto exit_init_hba;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001359
1360 if (qla4xxx_init_local_data(ha) == QLA_ERROR)
David C Somayajulu46235e62007-06-08 17:37:16 -07001361 goto exit_init_hba;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001362
1363 status = qla4xxx_init_firmware(ha);
1364 if (status == QLA_ERROR)
David C Somayajulu46235e62007-06-08 17:37:16 -07001365 goto exit_init_hba;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001366
1367 /*
1368 * FW is waiting to get an IP address from DHCP server: Skip building
1369 * the ddb_list and wait for DHCP lease acquired aen to come in
1370 * followed by 0x8014 aen" to trigger the tgt discovery process.
1371 */
Vikas Chaudhary2a49a782010-04-28 11:37:07 +05301372 if (ha->firmware_state & FW_STATE_CONFIGURING_IP)
David C Somayajulu46235e62007-06-08 17:37:16 -07001373 goto exit_init_online;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001374
1375 /* Skip device discovery if ip and subnet is zero */
1376 if (memcmp(ha->ip_address, ip_address, IP_ADDR_LEN) == 0 ||
1377 memcmp(ha->subnet_mask, ip_address, IP_ADDR_LEN) == 0)
David C Somayajulu46235e62007-06-08 17:37:16 -07001378 goto exit_init_online;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001379
1380 if (renew_ddb_list == PRESERVE_DDB_LIST) {
1381 /*
1382 * We want to preserve lun states (i.e. suspended, etc.)
1383 * for recovery initiated by the driver. So just update
1384 * the device states for the existing ddb_list.
1385 */
1386 qla4xxx_reinitialize_ddb_list(ha);
1387 } else if (renew_ddb_list == REBUILD_DDB_LIST) {
1388 /*
1389 * We want to build the ddb_list from scratch during
1390 * driver initialization and recovery initiated by the
1391 * INT_HBA_RESET IOCTL.
1392 */
1393 status = qla4xxx_initialize_ddb_list(ha);
1394 if (status == QLA_ERROR) {
1395 DEBUG2(printk("%s(%ld) Error occurred during build"
1396 "ddb list\n", __func__, ha->host_no));
1397 goto exit_init_hba;
1398 }
1399
1400 }
1401 if (!ha->tot_ddbs) {
1402 DEBUG2(printk("scsi%ld: Failed to initialize devices or none "
1403 "present in Firmware device database\n",
1404 ha->host_no));
1405 }
1406
David C Somayajulu46235e62007-06-08 17:37:16 -07001407exit_init_online:
David C Somayajulu92b72732007-05-23 17:55:40 -07001408 set_bit(AF_ONLINE, &ha->flags);
David C Somayajulu46235e62007-06-08 17:37:16 -07001409exit_init_hba:
David Somayajuluafaf5a22006-09-19 10:28:00 -07001410 return status;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001411}
1412
1413/**
1414 * qla4xxx_add_device_dynamically - ddb addition due to an AEN
1415 * @ha: Pointer to host adapter structure.
1416 * @fw_ddb_index: Firmware's device database index
1417 *
1418 * This routine processes adds a device as a result of an 8014h AEN.
1419 **/
1420static void qla4xxx_add_device_dynamically(struct scsi_qla_host *ha,
1421 uint32_t fw_ddb_index)
1422{
1423 struct ddb_entry * ddb_entry;
David C Somayajulu92b72732007-05-23 17:55:40 -07001424 uint32_t new_tgt;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001425
1426 /* First allocate a device structure */
David C Somayajulu92b72732007-05-23 17:55:40 -07001427 ddb_entry = qla4xxx_get_ddb_entry(ha, fw_ddb_index, &new_tgt);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001428 if (ddb_entry == NULL) {
1429 DEBUG2(printk(KERN_WARNING
1430 "scsi%ld: Unable to allocate memory to add "
1431 "fw_ddb_index %d\n", ha->host_no, fw_ddb_index));
1432 return;
1433 }
1434
David C Somayajulu92b72732007-05-23 17:55:40 -07001435 if (!new_tgt && (ddb_entry->fw_ddb_index != fw_ddb_index)) {
1436 /* Target has been bound to a new fw_ddb_index */
1437 qla4xxx_free_ddb(ha, ddb_entry);
1438 ddb_entry = qla4xxx_alloc_ddb(ha, fw_ddb_index);
1439 if (ddb_entry == NULL) {
1440 DEBUG2(printk(KERN_WARNING
1441 "scsi%ld: Unable to allocate memory"
1442 " to add fw_ddb_index %d\n",
1443 ha->host_no, fw_ddb_index));
1444 return;
1445 }
1446 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001447 if (qla4xxx_update_ddb_entry(ha, ddb_entry, fw_ddb_index) ==
1448 QLA_ERROR) {
1449 ha->fw_ddb_index_map[fw_ddb_index] =
1450 (struct ddb_entry *)INVALID_ENTRY;
1451 DEBUG2(printk(KERN_WARNING
1452 "scsi%ld: failed to add new device at index "
1453 "[%d]\n Unable to retrieve fw ddb entry\n",
1454 ha->host_no, fw_ddb_index));
1455 qla4xxx_free_ddb(ha, ddb_entry);
1456 return;
1457 }
1458
1459 if (qla4xxx_add_sess(ddb_entry)) {
1460 DEBUG2(printk(KERN_WARNING
1461 "scsi%ld: failed to add new device at index "
1462 "[%d]\n Unable to add connection and session\n",
1463 ha->host_no, fw_ddb_index));
1464 qla4xxx_free_ddb(ha, ddb_entry);
1465 }
1466}
1467
1468/**
1469 * qla4xxx_process_ddb_changed - process ddb state change
1470 * @ha - Pointer to host adapter structure.
1471 * @fw_ddb_index - Firmware's device database index
1472 * @state - Device state
1473 *
1474 * This routine processes a Decive Database Changed AEN Event.
1475 **/
Vikas Chaudhary821d6e52010-04-28 11:41:21 +05301476int qla4xxx_process_ddb_changed(struct scsi_qla_host *ha, uint32_t fw_ddb_index,
1477 uint32_t state, uint32_t conn_err)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001478{
1479 struct ddb_entry * ddb_entry;
1480 uint32_t old_fw_ddb_device_state;
1481
1482 /* check for out of range index */
1483 if (fw_ddb_index >= MAX_DDB_ENTRIES)
1484 return QLA_ERROR;
1485
1486 /* Get the corresponging ddb entry */
1487 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, fw_ddb_index);
1488 /* Device does not currently exist in our database. */
1489 if (ddb_entry == NULL) {
1490 if (state == DDB_DS_SESSION_ACTIVE)
1491 qla4xxx_add_device_dynamically(ha, fw_ddb_index);
1492 return QLA_SUCCESS;
1493 }
1494
1495 /* Device already exists in our database. */
1496 old_fw_ddb_device_state = ddb_entry->fw_ddb_device_state;
1497 DEBUG2(printk("scsi%ld: %s DDB - old state= 0x%x, new state=0x%x for "
1498 "index [%d]\n", ha->host_no, __func__,
1499 ddb_entry->fw_ddb_device_state, state, fw_ddb_index));
1500 if (old_fw_ddb_device_state == state &&
1501 state == DDB_DS_SESSION_ACTIVE) {
Vikas Chaudharye349fa32010-07-10 14:48:36 +05301502 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
1503 atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
1504 iscsi_unblock_session(ddb_entry->sess);
1505 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001506 return QLA_SUCCESS;
1507 }
1508
1509 ddb_entry->fw_ddb_device_state = state;
1510 /* Device is back online. */
1511 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
Mike Christie50a29ae2008-03-04 13:26:53 -06001512 atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001513 atomic_set(&ddb_entry->port_down_timer,
1514 ha->port_down_retry_count);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001515 atomic_set(&ddb_entry->relogin_retry_count, 0);
1516 atomic_set(&ddb_entry->relogin_timer, 0);
1517 clear_bit(DF_RELOGIN, &ddb_entry->flags);
1518 clear_bit(DF_NO_RELOGIN, &ddb_entry->flags);
Mike Christieb6359302008-01-31 13:36:44 -06001519 iscsi_unblock_session(ddb_entry->sess);
Mike Christie26974782007-12-13 12:43:29 -06001520 iscsi_session_event(ddb_entry->sess,
1521 ISCSI_KEVENT_CREATE_SESSION);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001522 /*
1523 * Change the lun state to READY in case the lun TIMEOUT before
1524 * the device came back.
1525 */
1526 } else {
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301527 /* Device went away, mark device missing */
1528 if (atomic_read(&ddb_entry->state) == DDB_STATE_ONLINE) {
1529 DEBUG2(dev_info(&ha->pdev->dev, "%s mark missing "
1530 "ddb_entry 0x%p sess 0x%p conn 0x%p\n",
1531 __func__, ddb_entry,
1532 ddb_entry->sess, ddb_entry->conn));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001533 qla4xxx_mark_device_missing(ha, ddb_entry);
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301534 }
1535
David Somayajuluafaf5a22006-09-19 10:28:00 -07001536 /*
1537 * Relogin if device state changed to a not active state.
Vikas Chaudhary821d6e52010-04-28 11:41:21 +05301538 * However, do not relogin if a RELOGIN is in process, or
1539 * we are not allowed to relogin to this DDB.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001540 */
1541 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_FAILED &&
1542 !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
1543 !test_bit(DF_NO_RELOGIN, &ddb_entry->flags) &&
Vikas Chaudhary821d6e52010-04-28 11:41:21 +05301544 qla4_is_relogin_allowed(ha, conn_err)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001545 /*
1546 * This triggers a relogin. After the relogin_timer
1547 * expires, the relogin gets scheduled. We must wait a
1548 * minimum amount of time since receiving an 0x8014 AEN
1549 * with failed device_state or a logout response before
1550 * we can issue another relogin.
1551 */
Vikas Chaudhary821d6e52010-04-28 11:41:21 +05301552 /* Firmware pads this timeout: (time2wait +1).
David Somayajuluafaf5a22006-09-19 10:28:00 -07001553 * Driver retry to login should be longer than F/W.
1554 * Otherwise F/W will fail
1555 * set_ddb() mbx cmd with 0x4005 since it still
1556 * counting down its time2wait.
1557 */
1558 atomic_set(&ddb_entry->relogin_timer, 0);
1559 atomic_set(&ddb_entry->retry_relogin_timer,
1560 ddb_entry->default_time2wait + 4);
1561 }
1562 }
1563
1564 return QLA_SUCCESS;
1565}
1566