blob: 39048d9339ac77a369481d21924a4884c17d5b3e [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
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053014static 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 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053054void 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/**
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053089 * qla4xxx_init_response_q_entries() - Initializes response queue entries.
90 * @ha: HA context
91 *
92 * Beginning of request ring has initialization control block already built
93 * by nvram config routine.
94 **/
95static void qla4xxx_init_response_q_entries(struct scsi_qla_host *ha)
96{
97 uint16_t cnt;
98 struct response *pkt;
99
100 pkt = (struct response *)ha->response_ptr;
101 for (cnt = 0; cnt < RESPONSE_QUEUE_DEPTH; cnt++) {
102 pkt->signature = RESPONSE_PROCESSED;
103 pkt++;
104 }
105}
106
107/**
David Somayajuluafaf5a22006-09-19 10:28:00 -0700108 * qla4xxx_init_rings - initialize hw queues
109 * @ha: pointer to host adapter structure.
110 *
111 * This routine initializes the internal queues for the specified adapter.
112 * The QLA4010 requires us to restart the queues at index 0.
113 * The QLA4000 doesn't care, so just default to QLA4010's requirement.
114 **/
115int qla4xxx_init_rings(struct scsi_qla_host *ha)
116{
117 unsigned long flags = 0;
118
119 /* Initialize request queue. */
120 spin_lock_irqsave(&ha->hardware_lock, flags);
121 ha->request_out = 0;
122 ha->request_in = 0;
123 ha->request_ptr = &ha->request_ring[ha->request_in];
124 ha->req_q_count = REQUEST_QUEUE_DEPTH;
125
126 /* Initialize response queue. */
127 ha->response_in = 0;
128 ha->response_out = 0;
129 ha->response_ptr = &ha->response_ring[ha->response_out];
130
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530131 if (is_qla8022(ha)) {
132 writel(0,
133 (unsigned long __iomem *)&ha->qla4_8xxx_reg->req_q_out);
134 writel(0,
135 (unsigned long __iomem *)&ha->qla4_8xxx_reg->rsp_q_in);
136 writel(0,
137 (unsigned long __iomem *)&ha->qla4_8xxx_reg->rsp_q_out);
138 } else {
139 /*
140 * Initialize DMA Shadow registers. The firmware is really
141 * supposed to take care of this, but on some uniprocessor
142 * systems, the shadow registers aren't cleared-- causing
143 * the interrupt_handler to think there are responses to be
144 * processed when there aren't.
145 */
146 ha->shadow_regs->req_q_out = __constant_cpu_to_le32(0);
147 ha->shadow_regs->rsp_q_in = __constant_cpu_to_le32(0);
148 wmb();
David Somayajuluafaf5a22006-09-19 10:28:00 -0700149
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530150 writel(0, &ha->reg->req_q_in);
151 writel(0, &ha->reg->rsp_q_out);
152 readl(&ha->reg->rsp_q_out);
153 }
154
155 qla4xxx_init_response_q_entries(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700156
157 spin_unlock_irqrestore(&ha->hardware_lock, flags);
158
159 return QLA_SUCCESS;
160}
161
162/**
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530163 * qla4xxx_get_sys_info - validate adapter MAC address(es)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700164 * @ha: pointer to host adapter structure.
165 *
166 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530167int qla4xxx_get_sys_info(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700168{
169 struct flash_sys_info *sys_info;
170 dma_addr_t sys_info_dma;
171 int status = QLA_ERROR;
172
173 sys_info = dma_alloc_coherent(&ha->pdev->dev, sizeof(*sys_info),
174 &sys_info_dma, GFP_KERNEL);
175 if (sys_info == NULL) {
176 DEBUG2(printk("scsi%ld: %s: Unable to allocate dma buffer.\n",
177 ha->host_no, __func__));
178
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530179 goto exit_get_sys_info_no_free;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700180 }
181 memset(sys_info, 0, sizeof(*sys_info));
182
183 /* Get flash sys info */
184 if (qla4xxx_get_flash(ha, sys_info_dma, FLASH_OFFSET_SYS_INFO,
185 sizeof(*sys_info)) != QLA_SUCCESS) {
186 DEBUG2(printk("scsi%ld: %s: get_flash FLASH_OFFSET_SYS_INFO "
187 "failed\n", ha->host_no, __func__));
188
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530189 goto exit_get_sys_info;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700190 }
191
192 /* Save M.A.C. address & serial_number */
193 memcpy(ha->my_mac, &sys_info->physAddr[0].address[0],
194 min(sizeof(ha->my_mac),
195 sizeof(sys_info->physAddr[0].address)));
196 memcpy(ha->serial_number, &sys_info->acSerialNumber,
197 min(sizeof(ha->serial_number),
198 sizeof(sys_info->acSerialNumber)));
199
200 status = QLA_SUCCESS;
201
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530202exit_get_sys_info:
David Somayajuluafaf5a22006-09-19 10:28:00 -0700203 dma_free_coherent(&ha->pdev->dev, sizeof(*sys_info), sys_info,
204 sys_info_dma);
205
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530206exit_get_sys_info_no_free:
David Somayajuluafaf5a22006-09-19 10:28:00 -0700207 return status;
208}
209
210/**
211 * qla4xxx_init_local_data - initialize adapter specific local data
212 * @ha: pointer to host adapter structure.
213 *
214 **/
215static int qla4xxx_init_local_data(struct scsi_qla_host *ha)
216{
Uwe Kleine-König421f91d2010-06-11 12:17:00 +0200217 /* Initialize aen queue */
David Somayajuluafaf5a22006-09-19 10:28:00 -0700218 ha->aen_q_count = MAX_AEN_ENTRIES;
219
220 return qla4xxx_get_firmware_status(ha);
221}
222
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530223static uint8_t
224qla4xxx_wait_for_ip_config(struct scsi_qla_host *ha)
225{
226 uint8_t ipv4_wait = 0;
227 uint8_t ipv6_wait = 0;
228 int8_t ip_address[IPv6_ADDR_LEN] = {0} ;
229
230 /* If both IPv4 & IPv6 are enabled, possibly only one
231 * IP address may be acquired, so check to see if we
232 * need to wait for another */
233 if (is_ipv4_enabled(ha) && is_ipv6_enabled(ha)) {
234 if (((ha->addl_fw_state & FW_ADDSTATE_DHCPv4_ENABLED) != 0) &&
235 ((ha->addl_fw_state &
236 FW_ADDSTATE_DHCPv4_LEASE_ACQUIRED) == 0)) {
237 ipv4_wait = 1;
238 }
239 if (((ha->ipv6_addl_options &
240 IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) != 0) &&
241 ((ha->ipv6_link_local_state == IP_ADDRSTATE_ACQUIRING) ||
242 (ha->ipv6_addr0_state == IP_ADDRSTATE_ACQUIRING) ||
243 (ha->ipv6_addr1_state == IP_ADDRSTATE_ACQUIRING))) {
244
245 ipv6_wait = 1;
246
247 if ((ha->ipv6_link_local_state ==
248 IP_ADDRSTATE_PREFERRED) ||
249 (ha->ipv6_addr0_state == IP_ADDRSTATE_PREFERRED) ||
250 (ha->ipv6_addr1_state == IP_ADDRSTATE_PREFERRED)) {
251 DEBUG2(printk(KERN_INFO "scsi%ld: %s: "
252 "Preferred IP configured."
253 " Don't wait!\n", ha->host_no,
254 __func__));
255 ipv6_wait = 0;
256 }
257 if (memcmp(&ha->ipv6_default_router_addr, ip_address,
258 IPv6_ADDR_LEN) == 0) {
259 DEBUG2(printk(KERN_INFO "scsi%ld: %s: "
260 "No Router configured. "
261 "Don't wait!\n", ha->host_no,
262 __func__));
263 ipv6_wait = 0;
264 }
265 if ((ha->ipv6_default_router_state ==
266 IPV6_RTRSTATE_MANUAL) &&
267 (ha->ipv6_link_local_state ==
268 IP_ADDRSTATE_TENTATIVE) &&
269 (memcmp(&ha->ipv6_link_local_addr,
270 &ha->ipv6_default_router_addr, 4) == 0)) {
271 DEBUG2(printk("scsi%ld: %s: LinkLocal Router & "
272 "IP configured. Don't wait!\n",
273 ha->host_no, __func__));
274 ipv6_wait = 0;
275 }
276 }
277 if (ipv4_wait || ipv6_wait) {
278 DEBUG2(printk("scsi%ld: %s: Wait for additional "
279 "IP(s) \"", ha->host_no, __func__));
280 if (ipv4_wait)
281 DEBUG2(printk("IPv4 "));
282 if (ha->ipv6_link_local_state == IP_ADDRSTATE_ACQUIRING)
283 DEBUG2(printk("IPv6LinkLocal "));
284 if (ha->ipv6_addr0_state == IP_ADDRSTATE_ACQUIRING)
285 DEBUG2(printk("IPv6Addr0 "));
286 if (ha->ipv6_addr1_state == IP_ADDRSTATE_ACQUIRING)
287 DEBUG2(printk("IPv6Addr1 "));
288 DEBUG2(printk("\"\n"));
289 }
290 }
291
292 return ipv4_wait|ipv6_wait;
293}
294
David Somayajuluafaf5a22006-09-19 10:28:00 -0700295static int qla4xxx_fw_ready(struct scsi_qla_host *ha)
296{
297 uint32_t timeout_count;
298 int ready = 0;
299
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530300 DEBUG2(ql4_printk(KERN_INFO, ha, "Waiting for Firmware Ready..\n"));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700301 for (timeout_count = ADAPTER_INIT_TOV; timeout_count > 0;
302 timeout_count--) {
303 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
304 qla4xxx_get_dhcp_ip_address(ha);
305
306 /* Get firmware state. */
307 if (qla4xxx_get_firmware_state(ha) != QLA_SUCCESS) {
308 DEBUG2(printk("scsi%ld: %s: unable to get firmware "
309 "state\n", ha->host_no, __func__));
310 break;
311
312 }
313
314 if (ha->firmware_state & FW_STATE_ERROR) {
315 DEBUG2(printk("scsi%ld: %s: an unrecoverable error has"
316 " occurred\n", ha->host_no, __func__));
317 break;
318
319 }
320 if (ha->firmware_state & FW_STATE_CONFIG_WAIT) {
321 /*
322 * The firmware has not yet been issued an Initialize
323 * Firmware command, so issue it now.
324 */
325 if (qla4xxx_initialize_fw_cb(ha) == QLA_ERROR)
326 break;
327
328 /* Go back and test for ready state - no wait. */
329 continue;
330 }
331
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530332 if (ha->firmware_state & FW_STATE_WAIT_AUTOCONNECT) {
333 DEBUG2(printk(KERN_INFO "scsi%ld: %s: fwstate:"
334 "AUTOCONNECT in progress\n",
335 ha->host_no, __func__));
336 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700337
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530338 if (ha->firmware_state & FW_STATE_CONFIGURING_IP) {
339 DEBUG2(printk(KERN_INFO "scsi%ld: %s: fwstate:"
340 " CONFIGURING IP\n",
341 ha->host_no, __func__));
342 /*
343 * Check for link state after 15 secs and if link is
344 * still DOWN then, cable is unplugged. Ignore "DHCP
345 * in Progress/CONFIGURING IP" bit to check if firmware
346 * is in ready state or not after 15 secs.
347 * This is applicable for both 2.x & 3.x firmware
348 */
349 if (timeout_count <= (ADAPTER_INIT_TOV - 15)) {
350 if (ha->addl_fw_state & FW_ADDSTATE_LINK_UP) {
351 DEBUG2(printk(KERN_INFO "scsi%ld: %s:"
352 " LINK UP (Cable plugged)\n",
353 ha->host_no, __func__));
354 } else if (ha->firmware_state &
355 (FW_STATE_CONFIGURING_IP |
356 FW_STATE_READY)) {
357 DEBUG2(printk(KERN_INFO "scsi%ld: %s: "
358 "LINK DOWN (Cable unplugged)\n",
359 ha->host_no, __func__));
360 ha->firmware_state = FW_STATE_READY;
361 }
362 }
363 }
364
365 if (ha->firmware_state == FW_STATE_READY) {
366 /* If DHCP IP Addr is available, retrieve it now. */
367 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR,
368 &ha->dpc_flags))
369 qla4xxx_get_dhcp_ip_address(ha);
370
371 if (!qla4xxx_wait_for_ip_config(ha) ||
372 timeout_count == 1) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530373 DEBUG2(ql4_printk(KERN_INFO, ha,
374 "Firmware Ready..\n"));
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530375 /* The firmware is ready to process SCSI
376 commands. */
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530377 DEBUG2(ql4_printk(KERN_INFO, ha,
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530378 "scsi%ld: %s: MEDIA TYPE"
379 " - %s\n", ha->host_no,
380 __func__, (ha->addl_fw_state &
381 FW_ADDSTATE_OPTICAL_MEDIA)
382 != 0 ? "OPTICAL" : "COPPER"));
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530383 DEBUG2(ql4_printk(KERN_INFO, ha,
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530384 "scsi%ld: %s: DHCPv4 STATE"
385 " Enabled %s\n", ha->host_no,
386 __func__, (ha->addl_fw_state &
387 FW_ADDSTATE_DHCPv4_ENABLED) != 0 ?
388 "YES" : "NO"));
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530389 DEBUG2(ql4_printk(KERN_INFO, ha,
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530390 "scsi%ld: %s: LINK %s\n",
391 ha->host_no, __func__,
392 (ha->addl_fw_state &
393 FW_ADDSTATE_LINK_UP) != 0 ?
394 "UP" : "DOWN"));
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530395 DEBUG2(ql4_printk(KERN_INFO, ha,
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530396 "scsi%ld: %s: iSNS Service "
397 "Started %s\n",
398 ha->host_no, __func__,
399 (ha->addl_fw_state &
400 FW_ADDSTATE_ISNS_SVC_ENABLED) != 0 ?
401 "YES" : "NO"));
402
403 ready = 1;
404 break;
405 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700406 }
407 DEBUG2(printk("scsi%ld: %s: waiting on fw, state=%x:%x - "
408 "seconds expired= %d\n", ha->host_no, __func__,
409 ha->firmware_state, ha->addl_fw_state,
410 timeout_count));
David C Somayajulud9150582006-11-15 17:38:40 -0800411 if (is_qla4032(ha) &&
412 !(ha->addl_fw_state & FW_ADDSTATE_LINK_UP) &&
413 (timeout_count < ADAPTER_INIT_TOV - 5)) {
414 break;
415 }
416
David Somayajuluafaf5a22006-09-19 10:28:00 -0700417 msleep(1000);
418 } /* end of for */
419
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530420 if (timeout_count <= 0)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700421 DEBUG2(printk("scsi%ld: %s: FW Initialization timed out!\n",
422 ha->host_no, __func__));
423
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530424 if (ha->firmware_state & FW_STATE_CONFIGURING_IP) {
425 DEBUG2(printk("scsi%ld: %s: FW initialized, but is reporting "
426 "it's waiting to configure an IP address\n",
427 ha->host_no, __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700428 ready = 1;
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530429 } else if (ha->firmware_state & FW_STATE_WAIT_AUTOCONNECT) {
430 DEBUG2(printk("scsi%ld: %s: FW initialized, but "
431 "auto-discovery still in process\n",
432 ha->host_no, __func__));
Vikas Chaudharyb9663462010-07-10 14:49:19 +0530433 ready = 1;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700434 }
435
436 return ready;
437}
438
439/**
440 * qla4xxx_init_firmware - initializes the firmware.
441 * @ha: pointer to host adapter structure.
442 *
443 **/
444static int qla4xxx_init_firmware(struct scsi_qla_host *ha)
445{
446 int status = QLA_ERROR;
447
Lalit Chandivade9d4946f2010-07-30 14:26:31 +0530448 /* For 82xx, stop firmware before initializing because if BIOS
449 * has previously initialized firmware, then driver's initialize
450 * firmware will fail. */
451 if (is_qla8022(ha))
452 qla4_8xxx_stop_firmware(ha);
453
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530454 ql4_printk(KERN_INFO, ha, "Initializing firmware..\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700455 if (qla4xxx_initialize_fw_cb(ha) == QLA_ERROR) {
456 DEBUG2(printk("scsi%ld: %s: Failed to initialize firmware "
457 "control block\n", ha->host_no, __func__));
458 return status;
459 }
460 if (!qla4xxx_fw_ready(ha))
461 return status;
462
David Somayajuluafaf5a22006-09-19 10:28:00 -0700463 return qla4xxx_get_firmware_status(ha);
464}
465
466static struct ddb_entry* qla4xxx_get_ddb_entry(struct scsi_qla_host *ha,
David C Somayajulu92b72732007-05-23 17:55:40 -0700467 uint32_t fw_ddb_index,
468 uint32_t *new_tgt)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700469{
470 struct dev_db_entry *fw_ddb_entry = NULL;
471 dma_addr_t fw_ddb_entry_dma;
472 struct ddb_entry *ddb_entry = NULL;
473 int found = 0;
474 uint32_t device_state;
475
David C Somayajulu92b72732007-05-23 17:55:40 -0700476 *new_tgt = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700477 /* Make sure the dma buffer is valid */
478 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev,
479 sizeof(*fw_ddb_entry),
480 &fw_ddb_entry_dma, GFP_KERNEL);
481 if (fw_ddb_entry == NULL) {
482 DEBUG2(printk("scsi%ld: %s: Unable to allocate dma buffer.\n",
483 ha->host_no, __func__));
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530484 goto exit_get_ddb_entry_no_free;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700485 }
486
487 if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index, fw_ddb_entry,
488 fw_ddb_entry_dma, NULL, NULL,
489 &device_state, NULL, NULL, NULL) ==
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530490 QLA_ERROR) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700491 DEBUG2(printk("scsi%ld: %s: failed get_ddb_entry for "
492 "fw_ddb_index %d\n", ha->host_no, __func__,
493 fw_ddb_index));
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530494 goto exit_get_ddb_entry;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700495 }
496
497 /* Allocate DDB if not already allocated. */
498 DEBUG2(printk("scsi%ld: %s: Looking for ddb[%d]\n", ha->host_no,
499 __func__, fw_ddb_index));
500 list_for_each_entry(ddb_entry, &ha->ddb_list, list) {
Mike Christie41bbdbe2009-01-16 12:36:52 -0600501 if ((memcmp(ddb_entry->iscsi_name, fw_ddb_entry->iscsi_name,
502 ISCSI_NAME_SIZE) == 0) &&
503 (ddb_entry->tpgt ==
504 le32_to_cpu(fw_ddb_entry->tgt_portal_grp)) &&
505 (memcmp(ddb_entry->isid, fw_ddb_entry->isid,
506 sizeof(ddb_entry->isid)) == 0)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700507 found++;
508 break;
509 }
510 }
511
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530512 /* if not found allocate new ddb */
David Somayajuluafaf5a22006-09-19 10:28:00 -0700513 if (!found) {
514 DEBUG2(printk("scsi%ld: %s: ddb[%d] not found - allocating "
515 "new ddb\n", ha->host_no, __func__,
516 fw_ddb_index));
David C Somayajulu92b72732007-05-23 17:55:40 -0700517 *new_tgt = 1;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700518 ddb_entry = qla4xxx_alloc_ddb(ha, fw_ddb_index);
519 }
520
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530521exit_get_ddb_entry:
David Somayajuluafaf5a22006-09-19 10:28:00 -0700522 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry), fw_ddb_entry,
523 fw_ddb_entry_dma);
524
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530525exit_get_ddb_entry_no_free:
David Somayajuluafaf5a22006-09-19 10:28:00 -0700526 return ddb_entry;
527}
528
529/**
530 * qla4xxx_update_ddb_entry - update driver's internal ddb
531 * @ha: pointer to host adapter structure.
532 * @ddb_entry: pointer to device database structure to be filled
533 * @fw_ddb_index: index of the ddb entry in fw ddb table
534 *
535 * This routine updates the driver's internal device database entry
536 * with information retrieved from the firmware's device database
537 * entry for the specified device. The ddb_entry->fw_ddb_index field
538 * must be initialized prior to calling this routine
539 *
540 **/
Adrian Bunk47975472007-04-26 00:35:16 -0700541static int qla4xxx_update_ddb_entry(struct scsi_qla_host *ha,
542 struct ddb_entry *ddb_entry,
543 uint32_t fw_ddb_index)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700544{
545 struct dev_db_entry *fw_ddb_entry = NULL;
546 dma_addr_t fw_ddb_entry_dma;
547 int status = QLA_ERROR;
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530548 uint32_t conn_err;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700549
550 if (ddb_entry == NULL) {
551 DEBUG2(printk("scsi%ld: %s: ddb_entry is NULL\n", ha->host_no,
552 __func__));
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530553
554 goto exit_update_ddb_no_free;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700555 }
556
557 /* Make sure the dma buffer is valid */
558 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev,
559 sizeof(*fw_ddb_entry),
560 &fw_ddb_entry_dma, GFP_KERNEL);
561 if (fw_ddb_entry == NULL) {
562 DEBUG2(printk("scsi%ld: %s: Unable to allocate dma buffer.\n",
563 ha->host_no, __func__));
564
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530565 goto exit_update_ddb_no_free;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700566 }
567
568 if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index, fw_ddb_entry,
569 fw_ddb_entry_dma, NULL, NULL,
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530570 &ddb_entry->fw_ddb_device_state, &conn_err,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700571 &ddb_entry->tcp_source_port_num,
572 &ddb_entry->connection_id) ==
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530573 QLA_ERROR) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700574 DEBUG2(printk("scsi%ld: %s: failed get_ddb_entry for "
575 "fw_ddb_index %d\n", ha->host_no, __func__,
576 fw_ddb_index));
577
578 goto exit_update_ddb;
579 }
580
581 status = QLA_SUCCESS;
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530582 ddb_entry->options = le16_to_cpu(fw_ddb_entry->options);
David C Somayajulu92b72732007-05-23 17:55:40 -0700583 ddb_entry->target_session_id = le16_to_cpu(fw_ddb_entry->tsid);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700584 ddb_entry->task_mgmt_timeout =
David C Somayajulu92b72732007-05-23 17:55:40 -0700585 le16_to_cpu(fw_ddb_entry->def_timeout);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700586 ddb_entry->CmdSn = 0;
David C Somayajulu92b72732007-05-23 17:55:40 -0700587 ddb_entry->exe_throttle = le16_to_cpu(fw_ddb_entry->exec_throttle);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700588 ddb_entry->default_relogin_timeout =
David C Somayajulu92b72732007-05-23 17:55:40 -0700589 le16_to_cpu(fw_ddb_entry->def_timeout);
590 ddb_entry->default_time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700591
592 /* Update index in case it changed */
593 ddb_entry->fw_ddb_index = fw_ddb_index;
594 ha->fw_ddb_index_map[fw_ddb_index] = ddb_entry;
595
David C Somayajulu92b72732007-05-23 17:55:40 -0700596 ddb_entry->port = le16_to_cpu(fw_ddb_entry->port);
597 ddb_entry->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
Mike Christie41bbdbe2009-01-16 12:36:52 -0600598 memcpy(ddb_entry->isid, fw_ddb_entry->isid, sizeof(ddb_entry->isid));
599
David C Somayajulu92b72732007-05-23 17:55:40 -0700600 memcpy(&ddb_entry->iscsi_name[0], &fw_ddb_entry->iscsi_name[0],
David Somayajuluafaf5a22006-09-19 10:28:00 -0700601 min(sizeof(ddb_entry->iscsi_name),
David C Somayajulu92b72732007-05-23 17:55:40 -0700602 sizeof(fw_ddb_entry->iscsi_name)));
Vikas Chaudhary3b2bef12010-07-10 14:51:30 +0530603 memcpy(&ddb_entry->iscsi_alias[0], &fw_ddb_entry->iscsi_alias[0],
604 min(sizeof(ddb_entry->iscsi_alias),
605 sizeof(fw_ddb_entry->iscsi_alias)));
David C Somayajulu92b72732007-05-23 17:55:40 -0700606 memcpy(&ddb_entry->ip_addr[0], &fw_ddb_entry->ip_addr[0],
607 min(sizeof(ddb_entry->ip_addr), sizeof(fw_ddb_entry->ip_addr)));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700608
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530609 ddb_entry->iscsi_max_burst_len = fw_ddb_entry->iscsi_max_burst_len;
610 ddb_entry->iscsi_max_outsnd_r2t = fw_ddb_entry->iscsi_max_outsnd_r2t;
611 ddb_entry->iscsi_first_burst_len = fw_ddb_entry->iscsi_first_burst_len;
612 ddb_entry->iscsi_max_rcv_data_seg_len =
613 fw_ddb_entry->iscsi_max_rcv_data_seg_len;
614 ddb_entry->iscsi_max_snd_data_seg_len =
615 fw_ddb_entry->iscsi_max_snd_data_seg_len;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700616
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530617 if (ddb_entry->options & DDB_OPT_IPV6_DEVICE) {
618 memcpy(&ddb_entry->remote_ipv6_addr,
619 fw_ddb_entry->ip_addr,
620 min(sizeof(ddb_entry->remote_ipv6_addr),
621 sizeof(fw_ddb_entry->ip_addr)));
622 memcpy(&ddb_entry->link_local_ipv6_addr,
623 fw_ddb_entry->link_local_ipv6_addr,
624 min(sizeof(ddb_entry->link_local_ipv6_addr),
625 sizeof(fw_ddb_entry->link_local_ipv6_addr)));
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530626
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530627 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: DDB[%d] State %04x"
628 " ConnErr %08x IP %pI6 "
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530629 ":%04d \"%s\"\n",
630 __func__, fw_ddb_index,
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530631 ddb_entry->fw_ddb_device_state,
632 conn_err, fw_ddb_entry->ip_addr,
633 le16_to_cpu(fw_ddb_entry->port),
634 fw_ddb_entry->iscsi_name));
635 } else
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530636 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: DDB[%d] State %04x"
637 " ConnErr %08x IP %pI4 "
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530638 ":%04d \"%s\"\n",
639 __func__, fw_ddb_index,
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530640 ddb_entry->fw_ddb_device_state,
641 conn_err, fw_ddb_entry->ip_addr,
642 le16_to_cpu(fw_ddb_entry->port),
643 fw_ddb_entry->iscsi_name));
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530644exit_update_ddb:
David Somayajuluafaf5a22006-09-19 10:28:00 -0700645 if (fw_ddb_entry)
646 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
647 fw_ddb_entry, fw_ddb_entry_dma);
648
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530649exit_update_ddb_no_free:
David Somayajuluafaf5a22006-09-19 10:28:00 -0700650 return status;
651}
652
653/**
654 * qla4xxx_alloc_ddb - allocate device database entry
655 * @ha: Pointer to host adapter structure.
656 * @fw_ddb_index: Firmware's device database index
657 *
658 * This routine allocates a ddb_entry, ititializes some values, and
659 * inserts it into the ddb list.
660 **/
Adrian Bunk47975472007-04-26 00:35:16 -0700661static struct ddb_entry * qla4xxx_alloc_ddb(struct scsi_qla_host *ha,
662 uint32_t fw_ddb_index)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700663{
664 struct ddb_entry *ddb_entry;
665
666 DEBUG2(printk("scsi%ld: %s: fw_ddb_index [%d]\n", ha->host_no,
667 __func__, fw_ddb_index));
668
669 ddb_entry = qla4xxx_alloc_sess(ha);
670 if (ddb_entry == NULL) {
671 DEBUG2(printk("scsi%ld: %s: Unable to allocate memory "
672 "to add fw_ddb_index [%d]\n",
673 ha->host_no, __func__, fw_ddb_index));
674 return ddb_entry;
675 }
676
677 ddb_entry->fw_ddb_index = fw_ddb_index;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700678 atomic_set(&ddb_entry->retry_relogin_timer, INVALID_ENTRY);
679 atomic_set(&ddb_entry->relogin_timer, 0);
680 atomic_set(&ddb_entry->relogin_retry_count, 0);
681 atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
682 list_add_tail(&ddb_entry->list, &ha->ddb_list);
683 ha->fw_ddb_index_map[fw_ddb_index] = ddb_entry;
684 ha->tot_ddbs++;
685
686 return ddb_entry;
687}
688
689/**
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530690 * qla4_is_relogin_allowed - Are we allowed to login?
691 * @ha: Pointer to host adapter structure.
692 * @conn_err: Last connection error associated with the ddb
693 *
694 * This routine tests the given connection error to determine if
695 * we are allowed to login.
696 **/
697int qla4_is_relogin_allowed(struct scsi_qla_host *ha, uint32_t conn_err)
698{
699 uint32_t err_code, login_rsp_sts_class;
700 int relogin = 1;
701
702 err_code = ((conn_err & 0x00ff0000) >> 16);
703 login_rsp_sts_class = ((conn_err & 0x0000ff00) >> 8);
704 if (err_code == 0x1c || err_code == 0x06) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530705 DEBUG2(ql4_printk(KERN_INFO, ha,
706 ": conn_err=0x%08x, send target completed"
707 " or access denied failure\n", conn_err));
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530708 relogin = 0;
709 }
710 if ((err_code == 0x08) && (login_rsp_sts_class == 0x02)) {
711 /* Login Response PDU returned an error.
712 Login Response Status in Error Code Detail
713 indicates login should not be retried.*/
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530714 DEBUG2(ql4_printk(KERN_INFO, ha,
715 ": conn_err=0x%08x, do not retry relogin\n",
716 conn_err));
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530717 relogin = 0;
718 }
719
720 return relogin;
721}
722
723/**
David Somayajuluafaf5a22006-09-19 10:28:00 -0700724 * qla4xxx_configure_ddbs - builds driver ddb list
725 * @ha: Pointer to host adapter structure.
726 *
727 * This routine searches for all valid firmware ddb entries and builds
728 * an internal ddb list. Ddbs that are considered valid are those with
729 * a device state of SESSION_ACTIVE.
730 **/
731static int qla4xxx_build_ddb_list(struct scsi_qla_host *ha)
732{
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530733 int status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700734 uint32_t fw_ddb_index = 0;
735 uint32_t next_fw_ddb_index = 0;
736 uint32_t ddb_state;
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530737 uint32_t conn_err;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700738 struct ddb_entry *ddb_entry;
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530739 struct dev_db_entry *fw_ddb_entry = NULL;
740 dma_addr_t fw_ddb_entry_dma;
741 uint32_t ipv6_device;
David C Somayajulu92b72732007-05-23 17:55:40 -0700742 uint32_t new_tgt;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700743
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530744 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
745 &fw_ddb_entry_dma, GFP_KERNEL);
746 if (fw_ddb_entry == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530747 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: DMA alloc failed\n",
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530748 __func__));
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530749
750 goto exit_build_ddb_list_no_free;
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530751 }
752
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530753 ql4_printk(KERN_INFO, ha, "Initializing DDBs ...\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700754 for (fw_ddb_index = 0; fw_ddb_index < MAX_DDB_ENTRIES;
755 fw_ddb_index = next_fw_ddb_index) {
756 /* First, let's see if a device exists here */
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530757 if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index, fw_ddb_entry,
758 0, NULL, &next_fw_ddb_index,
759 &ddb_state, &conn_err,
760 NULL, NULL) ==
761 QLA_ERROR) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700762 DEBUG2(printk("scsi%ld: %s: get_ddb_entry, "
763 "fw_ddb_index %d failed", ha->host_no,
764 __func__, fw_ddb_index));
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530765 goto exit_build_ddb_list;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700766 }
767
768 DEBUG2(printk("scsi%ld: %s: Getting DDB[%d] ddbstate=0x%x, "
769 "next_fw_ddb_index=%d.\n", ha->host_no, __func__,
770 fw_ddb_index, ddb_state, next_fw_ddb_index));
771
772 /* Issue relogin, if necessary. */
773 if (ddb_state == DDB_DS_SESSION_FAILED ||
774 ddb_state == DDB_DS_NO_CONNECTION_ACTIVE) {
775 /* Try and login to device */
776 DEBUG2(printk("scsi%ld: %s: Login to DDB[%d]\n",
777 ha->host_no, __func__, fw_ddb_index));
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530778 ipv6_device = le16_to_cpu(fw_ddb_entry->options) &
779 DDB_OPT_IPV6_DEVICE;
780 if (qla4_is_relogin_allowed(ha, conn_err) &&
781 ((!ipv6_device &&
782 *((uint32_t *)fw_ddb_entry->ip_addr))
783 || ipv6_device)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700784 qla4xxx_set_ddb_entry(ha, fw_ddb_index, 0);
David C Somayajulu92b72732007-05-23 17:55:40 -0700785 if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index,
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530786 NULL, 0, NULL,
787 &next_fw_ddb_index,
788 &ddb_state, &conn_err,
789 NULL, NULL)
790 == QLA_ERROR) {
David C Somayajulu92b72732007-05-23 17:55:40 -0700791 DEBUG2(printk("scsi%ld: %s:"
792 "get_ddb_entry %d failed\n",
793 ha->host_no,
794 __func__, fw_ddb_index));
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530795 goto exit_build_ddb_list;
David C Somayajulu92b72732007-05-23 17:55:40 -0700796 }
797 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700798 }
799
800 if (ddb_state != DDB_DS_SESSION_ACTIVE)
801 goto next_one;
802 /*
803 * if fw_ddb with session active state found,
804 * add to ddb_list
805 */
806 DEBUG2(printk("scsi%ld: %s: DDB[%d] added to list\n",
807 ha->host_no, __func__, fw_ddb_index));
808
809 /* Add DDB to internal our ddb list. */
David C Somayajulu92b72732007-05-23 17:55:40 -0700810 ddb_entry = qla4xxx_get_ddb_entry(ha, fw_ddb_index, &new_tgt);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700811 if (ddb_entry == NULL) {
812 DEBUG2(printk("scsi%ld: %s: Unable to allocate memory "
813 "for device at fw_ddb_index %d\n",
814 ha->host_no, __func__, fw_ddb_index));
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530815 goto exit_build_ddb_list;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700816 }
817 /* Fill in the device structure */
818 if (qla4xxx_update_ddb_entry(ha, ddb_entry, fw_ddb_index) ==
819 QLA_ERROR) {
820 ha->fw_ddb_index_map[fw_ddb_index] =
821 (struct ddb_entry *)INVALID_ENTRY;
822
David Somayajuluafaf5a22006-09-19 10:28:00 -0700823 DEBUG2(printk("scsi%ld: %s: update_ddb_entry failed "
824 "for fw_ddb_index %d.\n",
825 ha->host_no, __func__, fw_ddb_index));
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530826 goto exit_build_ddb_list;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700827 }
828
829next_one:
830 /* We know we've reached the last device when
831 * next_fw_ddb_index is 0 */
832 if (next_fw_ddb_index == 0)
833 break;
834 }
835
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530836 status = QLA_SUCCESS;
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530837 ql4_printk(KERN_INFO, ha, "DDB list done..\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700838
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530839exit_build_ddb_list:
840 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry), fw_ddb_entry,
841 fw_ddb_entry_dma);
842
843exit_build_ddb_list_no_free:
David Somayajuluafaf5a22006-09-19 10:28:00 -0700844 return status;
845}
846
847struct qla4_relog_scan {
848 int halt_wait;
849 uint32_t conn_err;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700850 uint32_t fw_ddb_index;
851 uint32_t next_fw_ddb_index;
852 uint32_t fw_ddb_device_state;
853};
854
855static int qla4_test_rdy(struct scsi_qla_host *ha, struct qla4_relog_scan *rs)
856{
857 struct ddb_entry *ddb_entry;
858
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530859 if (qla4_is_relogin_allowed(ha, rs->conn_err)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700860 /* We either have a device that is in
861 * the process of relogging in or a
862 * device that is waiting to be
863 * relogged in */
864 rs->halt_wait = 0;
865
866 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha,
867 rs->fw_ddb_index);
868 if (ddb_entry == NULL)
869 return QLA_ERROR;
870
871 if (ddb_entry->dev_scan_wait_to_start_relogin != 0
872 && time_after_eq(jiffies,
873 ddb_entry->
874 dev_scan_wait_to_start_relogin))
875 {
876 ddb_entry->dev_scan_wait_to_start_relogin = 0;
877 qla4xxx_set_ddb_entry(ha, rs->fw_ddb_index, 0);
878 }
879 }
880 return QLA_SUCCESS;
881}
882
883static int qla4_scan_for_relogin(struct scsi_qla_host *ha,
884 struct qla4_relog_scan *rs)
885{
886 int error;
887
888 /* scan for relogins
889 * ----------------- */
890 for (rs->fw_ddb_index = 0; rs->fw_ddb_index < MAX_DDB_ENTRIES;
891 rs->fw_ddb_index = rs->next_fw_ddb_index) {
892 if (qla4xxx_get_fwddb_entry(ha, rs->fw_ddb_index, NULL, 0,
893 NULL, &rs->next_fw_ddb_index,
894 &rs->fw_ddb_device_state,
895 &rs->conn_err, NULL, NULL)
896 == QLA_ERROR)
897 return QLA_ERROR;
898
899 if (rs->fw_ddb_device_state == DDB_DS_LOGIN_IN_PROCESS)
900 rs->halt_wait = 0;
901
902 if (rs->fw_ddb_device_state == DDB_DS_SESSION_FAILED ||
903 rs->fw_ddb_device_state == DDB_DS_NO_CONNECTION_ACTIVE) {
904 error = qla4_test_rdy(ha, rs);
905 if (error)
906 return error;
907 }
908
909 /* We know we've reached the last device when
910 * next_fw_ddb_index is 0 */
911 if (rs->next_fw_ddb_index == 0)
912 break;
913 }
914 return QLA_SUCCESS;
915}
916
917/**
918 * qla4xxx_devices_ready - wait for target devices to be logged in
919 * @ha: pointer to adapter structure
920 *
921 * This routine waits up to ql4xdiscoverywait seconds
922 * F/W database during driver load time.
923 **/
924static int qla4xxx_devices_ready(struct scsi_qla_host *ha)
925{
926 int error;
927 unsigned long discovery_wtime;
928 struct qla4_relog_scan rs;
929
930 discovery_wtime = jiffies + (ql4xdiscoverywait * HZ);
931
932 DEBUG(printk("Waiting (%d) for devices ...\n", ql4xdiscoverywait));
933 do {
934 /* poll for AEN. */
935 qla4xxx_get_firmware_state(ha);
936 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags)) {
937 /* Set time-between-relogin timer */
938 qla4xxx_process_aen(ha, RELOGIN_DDB_CHANGED_AENS);
939 }
940
941 /* if no relogins active or needed, halt discvery wait */
942 rs.halt_wait = 1;
943
944 error = qla4_scan_for_relogin(ha, &rs);
945
946 if (rs.halt_wait) {
947 DEBUG2(printk("scsi%ld: %s: Delay halted. Devices "
948 "Ready.\n", ha->host_no, __func__));
949 return QLA_SUCCESS;
950 }
951
952 msleep(2000);
953 } while (!time_after_eq(jiffies, discovery_wtime));
954
955 DEBUG3(qla4xxx_get_conn_event_log(ha));
956
957 return QLA_SUCCESS;
958}
959
960static void qla4xxx_flush_AENS(struct scsi_qla_host *ha)
961{
962 unsigned long wtime;
963
964 /* Flush the 0x8014 AEN from the firmware as a result of
965 * Auto connect. We are basically doing get_firmware_ddb()
966 * to determine whether we need to log back in or not.
967 * Trying to do a set ddb before we have processed 0x8014
968 * will result in another set_ddb() for the same ddb. In other
969 * words there will be stale entries in the aen_q.
970 */
971 wtime = jiffies + (2 * HZ);
972 do {
973 if (qla4xxx_get_firmware_state(ha) == QLA_SUCCESS)
974 if (ha->firmware_state & (BIT_2 | BIT_0))
975 return;
976
977 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
978 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
979
980 msleep(1000);
981 } while (!time_after_eq(jiffies, wtime));
982
983}
984
985static int qla4xxx_initialize_ddb_list(struct scsi_qla_host *ha)
986{
987 uint16_t fw_ddb_index;
988 int status = QLA_SUCCESS;
989
990 /* free the ddb list if is not empty */
991 if (!list_empty(&ha->ddb_list))
992 qla4xxx_free_ddb_list(ha);
993
994 for (fw_ddb_index = 0; fw_ddb_index < MAX_DDB_ENTRIES; fw_ddb_index++)
995 ha->fw_ddb_index_map[fw_ddb_index] =
996 (struct ddb_entry *)INVALID_ENTRY;
997
998 ha->tot_ddbs = 0;
999
1000 qla4xxx_flush_AENS(ha);
1001
Karen Higginsbb6f7d52010-07-10 14:51:17 +05301002 /* Wait for an AEN */
1003 qla4xxx_devices_ready(ha);
1004
David Somayajuluafaf5a22006-09-19 10:28:00 -07001005 /*
1006 * First perform device discovery for active
1007 * fw ddb indexes and build
1008 * ddb list.
1009 */
1010 if ((status = qla4xxx_build_ddb_list(ha)) == QLA_ERROR)
1011 return status;
1012
David Somayajuluafaf5a22006-09-19 10:28:00 -07001013 /*
1014 * Targets can come online after the inital discovery, so processing
1015 * the aens here will catch them.
1016 */
1017 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
1018 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
1019
1020 return status;
1021}
1022
1023/**
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301024 * qla4xxx_reinitialize_ddb_list - update the driver ddb list
David Somayajuluafaf5a22006-09-19 10:28:00 -07001025 * @ha: pointer to host adapter structure.
1026 *
1027 * This routine obtains device information from the F/W database after
1028 * firmware or adapter resets. The device table is preserved.
1029 **/
1030int qla4xxx_reinitialize_ddb_list(struct scsi_qla_host *ha)
1031{
1032 int status = QLA_SUCCESS;
1033 struct ddb_entry *ddb_entry, *detemp;
1034
1035 /* Update the device information for all devices. */
1036 list_for_each_entry_safe(ddb_entry, detemp, &ha->ddb_list, list) {
1037 qla4xxx_update_ddb_entry(ha, ddb_entry,
1038 ddb_entry->fw_ddb_index);
1039 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
1040 atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
1041 DEBUG2(printk ("scsi%ld: %s: ddb index [%d] marked "
1042 "ONLINE\n", ha->host_no, __func__,
1043 ddb_entry->fw_ddb_index));
Vikas Chaudhary36386322010-07-10 14:49:01 +05301044 iscsi_unblock_session(ddb_entry->sess);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001045 } else if (atomic_read(&ddb_entry->state) == DDB_STATE_ONLINE)
1046 qla4xxx_mark_device_missing(ha, ddb_entry);
1047 }
1048 return status;
1049}
1050
1051/**
1052 * qla4xxx_relogin_device - re-establish session
1053 * @ha: Pointer to host adapter structure.
1054 * @ddb_entry: Pointer to device database entry
1055 *
1056 * This routine does a session relogin with the specified device.
1057 * The ddb entry must be assigned prior to making this call.
1058 **/
1059int qla4xxx_relogin_device(struct scsi_qla_host *ha,
1060 struct ddb_entry * ddb_entry)
1061{
1062 uint16_t relogin_timer;
1063
1064 relogin_timer = max(ddb_entry->default_relogin_timeout,
1065 (uint16_t)RELOGIN_TOV);
1066 atomic_set(&ddb_entry->relogin_timer, relogin_timer);
1067
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301068 DEBUG2(printk("scsi%ld: Relogin ddb [%d]. TOV=%d\n", ha->host_no,
David Somayajuluafaf5a22006-09-19 10:28:00 -07001069 ddb_entry->fw_ddb_index, relogin_timer));
1070
1071 qla4xxx_set_ddb_entry(ha, ddb_entry->fw_ddb_index, 0);
1072
1073 return QLA_SUCCESS;
1074}
1075
David Somayajuluafaf5a22006-09-19 10:28:00 -07001076static int qla4xxx_config_nvram(struct scsi_qla_host *ha)
1077{
1078 unsigned long flags;
1079 union external_hw_config_reg extHwConfig;
1080
1081 DEBUG2(printk("scsi%ld: %s: Get EEProm parameters \n", ha->host_no,
1082 __func__));
1083 if (ql4xxx_lock_flash(ha) != QLA_SUCCESS)
Mike Christieb3925512010-02-10 16:51:48 -06001084 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001085 if (ql4xxx_lock_nvram(ha) != QLA_SUCCESS) {
1086 ql4xxx_unlock_flash(ha);
Mike Christieb3925512010-02-10 16:51:48 -06001087 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001088 }
1089
1090 /* Get EEPRom Parameters from NVRAM and validate */
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301091 ql4_printk(KERN_INFO, ha, "Configuring NVRAM ...\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001092 if (qla4xxx_is_nvram_configuration_valid(ha) == QLA_SUCCESS) {
1093 spin_lock_irqsave(&ha->hardware_lock, flags);
1094 extHwConfig.Asuint32_t =
1095 rd_nvram_word(ha, eeprom_ext_hw_conf_offset(ha));
1096 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1097 } else {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301098 ql4_printk(KERN_WARNING, ha,
1099 "scsi%ld: %s: EEProm checksum invalid. "
1100 "Please update your EEPROM\n", ha->host_no,
1101 __func__);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001102
Mike Christieb3925512010-02-10 16:51:48 -06001103 /* Attempt to set defaults */
David Somayajuluafaf5a22006-09-19 10:28:00 -07001104 if (is_qla4010(ha))
1105 extHwConfig.Asuint32_t = 0x1912;
David C Somayajulud9150582006-11-15 17:38:40 -08001106 else if (is_qla4022(ha) | is_qla4032(ha))
David Somayajuluafaf5a22006-09-19 10:28:00 -07001107 extHwConfig.Asuint32_t = 0x0023;
Mike Christieb3925512010-02-10 16:51:48 -06001108 else
1109 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001110 }
1111 DEBUG(printk("scsi%ld: %s: Setting extHwConfig to 0xFFFF%04x\n",
1112 ha->host_no, __func__, extHwConfig.Asuint32_t));
1113
1114 spin_lock_irqsave(&ha->hardware_lock, flags);
1115 writel((0xFFFF << 16) | extHwConfig.Asuint32_t, isp_ext_hw_conf(ha));
1116 readl(isp_ext_hw_conf(ha));
1117 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1118
1119 ql4xxx_unlock_nvram(ha);
1120 ql4xxx_unlock_flash(ha);
1121
Mike Christieb3925512010-02-10 16:51:48 -06001122 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001123}
1124
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301125/**
1126 * qla4_8xxx_pci_config() - Setup ISP82xx PCI configuration registers.
1127 * @ha: HA context
1128 */
1129void qla4_8xxx_pci_config(struct scsi_qla_host *ha)
1130{
1131 pci_set_master(ha->pdev);
1132}
1133
1134void qla4xxx_pci_config(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001135{
David C Somayajulu92b72732007-05-23 17:55:40 -07001136 uint16_t w;
David C Somayajulu46235e62007-06-08 17:37:16 -07001137 int status;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001138
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301139 ql4_printk(KERN_INFO, ha, "Configuring PCI space...\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001140
1141 pci_set_master(ha->pdev);
David C Somayajulu46235e62007-06-08 17:37:16 -07001142 status = pci_set_mwi(ha->pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001143 /*
1144 * We want to respect framework's setting of PCI configuration space
1145 * command register and also want to make sure that all bits of
1146 * interest to us are properly set in command register.
1147 */
1148 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
David C Somayajulu92b72732007-05-23 17:55:40 -07001149 w |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001150 w &= ~PCI_COMMAND_INTX_DISABLE;
1151 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
1152}
1153
1154static int qla4xxx_start_firmware_from_flash(struct scsi_qla_host *ha)
1155{
1156 int status = QLA_ERROR;
Vikas Chaudharyc301b022010-04-28 11:40:37 +05301157 unsigned long max_wait_time;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001158 unsigned long flags;
1159 uint32_t mbox_status;
1160
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301161 ql4_printk(KERN_INFO, ha, "Starting firmware ...\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001162
1163 /*
1164 * Start firmware from flash ROM
1165 *
1166 * WORKAROUND: Stuff a non-constant value that the firmware can
1167 * use as a seed for a random number generator in MB7 prior to
1168 * setting BOOT_ENABLE. Fixes problem where the TCP
1169 * connections use the same TCP ports after each reboot,
1170 * causing some connections to not get re-established.
1171 */
1172 DEBUG(printk("scsi%d: %s: Start firmware from flash ROM\n",
1173 ha->host_no, __func__));
1174
1175 spin_lock_irqsave(&ha->hardware_lock, flags);
1176 writel(jiffies, &ha->reg->mailbox[7]);
David C Somayajulud9150582006-11-15 17:38:40 -08001177 if (is_qla4022(ha) | is_qla4032(ha))
David Somayajuluafaf5a22006-09-19 10:28:00 -07001178 writel(set_rmask(NVR_WRITE_ENABLE),
1179 &ha->reg->u1.isp4022.nvram);
1180
David C Somayajulu92b72732007-05-23 17:55:40 -07001181 writel(2, &ha->reg->mailbox[6]);
1182 readl(&ha->reg->mailbox[6]);
1183
David Somayajuluafaf5a22006-09-19 10:28:00 -07001184 writel(set_rmask(CSR_BOOT_ENABLE), &ha->reg->ctrl_status);
1185 readl(&ha->reg->ctrl_status);
1186 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1187
1188 /* Wait for firmware to come UP. */
Vikas Chaudharyc301b022010-04-28 11:40:37 +05301189 DEBUG2(printk(KERN_INFO "scsi%ld: %s: Wait up to %d seconds for "
1190 "boot firmware to complete...\n",
1191 ha->host_no, __func__, FIRMWARE_UP_TOV));
1192 max_wait_time = jiffies + (FIRMWARE_UP_TOV * HZ);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001193 do {
1194 uint32_t ctrl_status;
1195
1196 spin_lock_irqsave(&ha->hardware_lock, flags);
1197 ctrl_status = readw(&ha->reg->ctrl_status);
1198 mbox_status = readw(&ha->reg->mailbox[0]);
1199 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1200
1201 if (ctrl_status & set_rmask(CSR_SCSI_PROCESSOR_INTR))
1202 break;
1203 if (mbox_status == MBOX_STS_COMMAND_COMPLETE)
1204 break;
1205
Vikas Chaudharyc301b022010-04-28 11:40:37 +05301206 DEBUG2(printk(KERN_INFO "scsi%ld: %s: Waiting for boot "
1207 "firmware to complete... ctrl_sts=0x%x\n",
1208 ha->host_no, __func__, ctrl_status));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001209
Vikas Chaudharyc301b022010-04-28 11:40:37 +05301210 msleep_interruptible(250);
1211 } while (!time_after_eq(jiffies, max_wait_time));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001212
1213 if (mbox_status == MBOX_STS_COMMAND_COMPLETE) {
Vikas Chaudharyc301b022010-04-28 11:40:37 +05301214 DEBUG(printk(KERN_INFO "scsi%ld: %s: Firmware has started\n",
David Somayajuluafaf5a22006-09-19 10:28:00 -07001215 ha->host_no, __func__));
1216
1217 spin_lock_irqsave(&ha->hardware_lock, flags);
1218 writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
1219 &ha->reg->ctrl_status);
1220 readl(&ha->reg->ctrl_status);
1221 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1222
1223 status = QLA_SUCCESS;
1224 } else {
1225 printk(KERN_INFO "scsi%ld: %s: Boot firmware failed "
1226 "- mbox status 0x%x\n", ha->host_no, __func__,
1227 mbox_status);
1228 status = QLA_ERROR;
1229 }
1230 return status;
1231}
1232
David C Somayajulu92b72732007-05-23 17:55:40 -07001233int ql4xxx_lock_drvr_wait(struct scsi_qla_host *a)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001234{
David C Somayajulu92b72732007-05-23 17:55:40 -07001235#define QL4_LOCK_DRVR_WAIT 60
David C Somayajulu477ffb92007-01-22 12:26:11 -08001236#define QL4_LOCK_DRVR_SLEEP 1
David Somayajuluafaf5a22006-09-19 10:28:00 -07001237
1238 int drvr_wait = QL4_LOCK_DRVR_WAIT;
1239 while (drvr_wait) {
David C Somayajulu92b72732007-05-23 17:55:40 -07001240 if (ql4xxx_lock_drvr(a) == 0) {
David C Somayajulu477ffb92007-01-22 12:26:11 -08001241 ssleep(QL4_LOCK_DRVR_SLEEP);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001242 if (drvr_wait) {
1243 DEBUG2(printk("scsi%ld: %s: Waiting for "
David C Somayajulu92b72732007-05-23 17:55:40 -07001244 "Global Init Semaphore(%d)...\n",
1245 a->host_no,
David C Somayajulu477ffb92007-01-22 12:26:11 -08001246 __func__, drvr_wait));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001247 }
1248 drvr_wait -= QL4_LOCK_DRVR_SLEEP;
1249 } else {
1250 DEBUG2(printk("scsi%ld: %s: Global Init Semaphore "
David C Somayajulu92b72732007-05-23 17:55:40 -07001251 "acquired\n", a->host_no, __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001252 return QLA_SUCCESS;
1253 }
1254 }
1255 return QLA_ERROR;
1256}
1257
1258/**
1259 * qla4xxx_start_firmware - starts qla4xxx firmware
1260 * @ha: Pointer to host adapter structure.
1261 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001262 * This routine performs the necessary steps to start the firmware for
David Somayajuluafaf5a22006-09-19 10:28:00 -07001263 * the QLA4010 adapter.
1264 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301265int qla4xxx_start_firmware(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001266{
1267 unsigned long flags = 0;
1268 uint32_t mbox_status;
1269 int status = QLA_ERROR;
1270 int soft_reset = 1;
1271 int config_chip = 0;
1272
David C Somayajulud9150582006-11-15 17:38:40 -08001273 if (is_qla4022(ha) | is_qla4032(ha))
David Somayajuluafaf5a22006-09-19 10:28:00 -07001274 ql4xxx_set_mac_number(ha);
1275
1276 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
1277 return QLA_ERROR;
1278
1279 spin_lock_irqsave(&ha->hardware_lock, flags);
1280
1281 DEBUG2(printk("scsi%ld: %s: port_ctrl = 0x%08X\n", ha->host_no,
1282 __func__, readw(isp_port_ctrl(ha))));
1283 DEBUG(printk("scsi%ld: %s: port_status = 0x%08X\n", ha->host_no,
1284 __func__, readw(isp_port_status(ha))));
1285
1286 /* Is Hardware already initialized? */
1287 if ((readw(isp_port_ctrl(ha)) & 0x8000) != 0) {
1288 DEBUG(printk("scsi%ld: %s: Hardware has already been "
1289 "initialized\n", ha->host_no, __func__));
1290
1291 /* Receive firmware boot acknowledgement */
1292 mbox_status = readw(&ha->reg->mailbox[0]);
1293
1294 DEBUG2(printk("scsi%ld: %s: H/W Config complete - mbox[0]= "
1295 "0x%x\n", ha->host_no, __func__, mbox_status));
1296
1297 /* Is firmware already booted? */
1298 if (mbox_status == 0) {
1299 /* F/W not running, must be config by net driver */
1300 config_chip = 1;
1301 soft_reset = 0;
1302 } else {
1303 writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
1304 &ha->reg->ctrl_status);
1305 readl(&ha->reg->ctrl_status);
1306 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1307 if (qla4xxx_get_firmware_state(ha) == QLA_SUCCESS) {
1308 DEBUG2(printk("scsi%ld: %s: Get firmware "
1309 "state -- state = 0x%x\n",
1310 ha->host_no,
1311 __func__, ha->firmware_state));
1312 /* F/W is running */
1313 if (ha->firmware_state &
1314 FW_STATE_CONFIG_WAIT) {
1315 DEBUG2(printk("scsi%ld: %s: Firmware "
1316 "in known state -- "
1317 "config and "
1318 "boot, state = 0x%x\n",
1319 ha->host_no, __func__,
1320 ha->firmware_state));
1321 config_chip = 1;
1322 soft_reset = 0;
1323 }
1324 } else {
1325 DEBUG2(printk("scsi%ld: %s: Firmware in "
1326 "unknown state -- resetting,"
1327 " state = "
1328 "0x%x\n", ha->host_no, __func__,
1329 ha->firmware_state));
1330 }
1331 spin_lock_irqsave(&ha->hardware_lock, flags);
1332 }
1333 } else {
1334 DEBUG(printk("scsi%ld: %s: H/W initialization hasn't been "
1335 "started - resetting\n", ha->host_no, __func__));
1336 }
1337 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1338
1339 DEBUG(printk("scsi%ld: %s: Flags soft_rest=%d, config= %d\n ",
1340 ha->host_no, __func__, soft_reset, config_chip));
1341 if (soft_reset) {
1342 DEBUG(printk("scsi%ld: %s: Issue Soft Reset\n", ha->host_no,
1343 __func__));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301344 status = qla4xxx_soft_reset(ha); /* NOTE: acquires drvr
1345 * lock again, but ok */
David Somayajuluafaf5a22006-09-19 10:28:00 -07001346 if (status == QLA_ERROR) {
1347 DEBUG(printk("scsi%d: %s: Soft Reset failed!\n",
1348 ha->host_no, __func__));
1349 ql4xxx_unlock_drvr(ha);
1350 return QLA_ERROR;
1351 }
1352 config_chip = 1;
1353
Joe Perchesb1c11812008-02-03 17:28:22 +02001354 /* Reset clears the semaphore, so acquire again */
David Somayajuluafaf5a22006-09-19 10:28:00 -07001355 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
1356 return QLA_ERROR;
1357 }
1358
1359 if (config_chip) {
1360 if ((status = qla4xxx_config_nvram(ha)) == QLA_SUCCESS)
1361 status = qla4xxx_start_firmware_from_flash(ha);
1362 }
1363
1364 ql4xxx_unlock_drvr(ha);
1365 if (status == QLA_SUCCESS) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001366 if (test_and_clear_bit(AF_GET_CRASH_RECORD, &ha->flags))
1367 qla4xxx_get_crash_record(ha);
1368 } else {
1369 DEBUG(printk("scsi%ld: %s: Firmware has NOT started\n",
1370 ha->host_no, __func__));
1371 }
1372 return status;
1373}
1374
1375
1376/**
1377 * qla4xxx_initialize_adapter - initiailizes hba
1378 * @ha: Pointer to host adapter structure.
1379 * @renew_ddb_list: Indicates what to do with the adapter's ddb list
1380 * after adapter recovery has completed.
1381 * 0=preserve ddb list, 1=destroy and rebuild ddb list
1382 *
1383 * This routine parforms all of the steps necessary to initialize the adapter.
1384 *
1385 **/
1386int qla4xxx_initialize_adapter(struct scsi_qla_host *ha,
1387 uint8_t renew_ddb_list)
1388{
1389 int status = QLA_ERROR;
1390 int8_t ip_address[IP_ADDR_LEN] = {0} ;
1391
1392 ha->eeprom_cmd_data = 0;
1393
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301394 ql4_printk(KERN_INFO, ha, "Configuring PCI space...\n");
1395 ha->isp_ops->pci_config(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001396
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301397 ha->isp_ops->disable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001398
1399 /* Initialize the Host adapter request/response queues and firmware */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301400 if (ha->isp_ops->start_firmware(ha) == QLA_ERROR)
David C Somayajulu46235e62007-06-08 17:37:16 -07001401 goto exit_init_hba;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001402
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301403 if (qla4xxx_get_fw_version(ha) == QLA_ERROR)
1404 goto exit_init_hba;
1405
1406 if (ha->isp_ops->get_sys_info(ha) == QLA_ERROR)
David C Somayajulu46235e62007-06-08 17:37:16 -07001407 goto exit_init_hba;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001408
1409 if (qla4xxx_init_local_data(ha) == QLA_ERROR)
David C Somayajulu46235e62007-06-08 17:37:16 -07001410 goto exit_init_hba;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001411
1412 status = qla4xxx_init_firmware(ha);
1413 if (status == QLA_ERROR)
David C Somayajulu46235e62007-06-08 17:37:16 -07001414 goto exit_init_hba;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001415
1416 /*
1417 * FW is waiting to get an IP address from DHCP server: Skip building
1418 * the ddb_list and wait for DHCP lease acquired aen to come in
1419 * followed by 0x8014 aen" to trigger the tgt discovery process.
1420 */
Vikas Chaudhary2a49a782010-04-28 11:37:07 +05301421 if (ha->firmware_state & FW_STATE_CONFIGURING_IP)
David C Somayajulu46235e62007-06-08 17:37:16 -07001422 goto exit_init_online;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001423
1424 /* Skip device discovery if ip and subnet is zero */
1425 if (memcmp(ha->ip_address, ip_address, IP_ADDR_LEN) == 0 ||
1426 memcmp(ha->subnet_mask, ip_address, IP_ADDR_LEN) == 0)
David C Somayajulu46235e62007-06-08 17:37:16 -07001427 goto exit_init_online;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001428
1429 if (renew_ddb_list == PRESERVE_DDB_LIST) {
1430 /*
1431 * We want to preserve lun states (i.e. suspended, etc.)
1432 * for recovery initiated by the driver. So just update
1433 * the device states for the existing ddb_list.
1434 */
1435 qla4xxx_reinitialize_ddb_list(ha);
1436 } else if (renew_ddb_list == REBUILD_DDB_LIST) {
1437 /*
1438 * We want to build the ddb_list from scratch during
1439 * driver initialization and recovery initiated by the
1440 * INT_HBA_RESET IOCTL.
1441 */
1442 status = qla4xxx_initialize_ddb_list(ha);
1443 if (status == QLA_ERROR) {
1444 DEBUG2(printk("%s(%ld) Error occurred during build"
1445 "ddb list\n", __func__, ha->host_no));
1446 goto exit_init_hba;
1447 }
1448
1449 }
1450 if (!ha->tot_ddbs) {
1451 DEBUG2(printk("scsi%ld: Failed to initialize devices or none "
1452 "present in Firmware device database\n",
1453 ha->host_no));
1454 }
1455
David C Somayajulu46235e62007-06-08 17:37:16 -07001456exit_init_online:
David C Somayajulu92b72732007-05-23 17:55:40 -07001457 set_bit(AF_ONLINE, &ha->flags);
David C Somayajulu46235e62007-06-08 17:37:16 -07001458exit_init_hba:
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301459 DEBUG2(printk("scsi%ld: initialize adapter: %s\n", ha->host_no,
1460 status == QLA_ERROR ? "FAILED" : "SUCCEDED"));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001461 return status;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001462}
1463
1464/**
1465 * qla4xxx_add_device_dynamically - ddb addition due to an AEN
1466 * @ha: Pointer to host adapter structure.
1467 * @fw_ddb_index: Firmware's device database index
1468 *
1469 * This routine processes adds a device as a result of an 8014h AEN.
1470 **/
1471static void qla4xxx_add_device_dynamically(struct scsi_qla_host *ha,
1472 uint32_t fw_ddb_index)
1473{
1474 struct ddb_entry * ddb_entry;
David C Somayajulu92b72732007-05-23 17:55:40 -07001475 uint32_t new_tgt;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001476
1477 /* First allocate a device structure */
David C Somayajulu92b72732007-05-23 17:55:40 -07001478 ddb_entry = qla4xxx_get_ddb_entry(ha, fw_ddb_index, &new_tgt);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001479 if (ddb_entry == NULL) {
1480 DEBUG2(printk(KERN_WARNING
1481 "scsi%ld: Unable to allocate memory to add "
1482 "fw_ddb_index %d\n", ha->host_no, fw_ddb_index));
1483 return;
1484 }
1485
David C Somayajulu92b72732007-05-23 17:55:40 -07001486 if (!new_tgt && (ddb_entry->fw_ddb_index != fw_ddb_index)) {
1487 /* Target has been bound to a new fw_ddb_index */
1488 qla4xxx_free_ddb(ha, ddb_entry);
1489 ddb_entry = qla4xxx_alloc_ddb(ha, fw_ddb_index);
1490 if (ddb_entry == NULL) {
1491 DEBUG2(printk(KERN_WARNING
1492 "scsi%ld: Unable to allocate memory"
1493 " to add fw_ddb_index %d\n",
1494 ha->host_no, fw_ddb_index));
1495 return;
1496 }
1497 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001498 if (qla4xxx_update_ddb_entry(ha, ddb_entry, fw_ddb_index) ==
1499 QLA_ERROR) {
1500 ha->fw_ddb_index_map[fw_ddb_index] =
1501 (struct ddb_entry *)INVALID_ENTRY;
1502 DEBUG2(printk(KERN_WARNING
1503 "scsi%ld: failed to add new device at index "
1504 "[%d]\n Unable to retrieve fw ddb entry\n",
1505 ha->host_no, fw_ddb_index));
1506 qla4xxx_free_ddb(ha, ddb_entry);
1507 return;
1508 }
1509
1510 if (qla4xxx_add_sess(ddb_entry)) {
1511 DEBUG2(printk(KERN_WARNING
1512 "scsi%ld: failed to add new device at index "
1513 "[%d]\n Unable to add connection and session\n",
1514 ha->host_no, fw_ddb_index));
1515 qla4xxx_free_ddb(ha, ddb_entry);
1516 }
1517}
1518
1519/**
1520 * qla4xxx_process_ddb_changed - process ddb state change
1521 * @ha - Pointer to host adapter structure.
1522 * @fw_ddb_index - Firmware's device database index
1523 * @state - Device state
1524 *
1525 * This routine processes a Decive Database Changed AEN Event.
1526 **/
Vikas Chaudhary821d6e52010-04-28 11:41:21 +05301527int qla4xxx_process_ddb_changed(struct scsi_qla_host *ha, uint32_t fw_ddb_index,
1528 uint32_t state, uint32_t conn_err)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001529{
1530 struct ddb_entry * ddb_entry;
1531 uint32_t old_fw_ddb_device_state;
1532
1533 /* check for out of range index */
1534 if (fw_ddb_index >= MAX_DDB_ENTRIES)
1535 return QLA_ERROR;
1536
1537 /* Get the corresponging ddb entry */
1538 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, fw_ddb_index);
1539 /* Device does not currently exist in our database. */
1540 if (ddb_entry == NULL) {
1541 if (state == DDB_DS_SESSION_ACTIVE)
1542 qla4xxx_add_device_dynamically(ha, fw_ddb_index);
1543 return QLA_SUCCESS;
1544 }
1545
1546 /* Device already exists in our database. */
1547 old_fw_ddb_device_state = ddb_entry->fw_ddb_device_state;
1548 DEBUG2(printk("scsi%ld: %s DDB - old state= 0x%x, new state=0x%x for "
1549 "index [%d]\n", ha->host_no, __func__,
1550 ddb_entry->fw_ddb_device_state, state, fw_ddb_index));
1551 if (old_fw_ddb_device_state == state &&
1552 state == DDB_DS_SESSION_ACTIVE) {
Vikas Chaudharye349fa32010-07-10 14:48:36 +05301553 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
1554 atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
1555 iscsi_unblock_session(ddb_entry->sess);
1556 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001557 return QLA_SUCCESS;
1558 }
1559
1560 ddb_entry->fw_ddb_device_state = state;
1561 /* Device is back online. */
1562 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
Mike Christie50a29ae2008-03-04 13:26:53 -06001563 atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001564 atomic_set(&ddb_entry->relogin_retry_count, 0);
1565 atomic_set(&ddb_entry->relogin_timer, 0);
1566 clear_bit(DF_RELOGIN, &ddb_entry->flags);
1567 clear_bit(DF_NO_RELOGIN, &ddb_entry->flags);
Mike Christieb6359302008-01-31 13:36:44 -06001568 iscsi_unblock_session(ddb_entry->sess);
Mike Christie26974782007-12-13 12:43:29 -06001569 iscsi_session_event(ddb_entry->sess,
1570 ISCSI_KEVENT_CREATE_SESSION);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001571 /*
1572 * Change the lun state to READY in case the lun TIMEOUT before
1573 * the device came back.
1574 */
1575 } else {
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301576 /* Device went away, mark device missing */
1577 if (atomic_read(&ddb_entry->state) == DDB_STATE_ONLINE) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301578 DEBUG2(ql4_printk(KERN_INFO, ha, "%s mark missing "
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301579 "ddb_entry 0x%p sess 0x%p conn 0x%p\n",
1580 __func__, ddb_entry,
1581 ddb_entry->sess, ddb_entry->conn));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001582 qla4xxx_mark_device_missing(ha, ddb_entry);
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301583 }
1584
David Somayajuluafaf5a22006-09-19 10:28:00 -07001585 /*
1586 * Relogin if device state changed to a not active state.
Vikas Chaudhary821d6e52010-04-28 11:41:21 +05301587 * However, do not relogin if a RELOGIN is in process, or
1588 * we are not allowed to relogin to this DDB.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001589 */
1590 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_FAILED &&
1591 !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
1592 !test_bit(DF_NO_RELOGIN, &ddb_entry->flags) &&
Vikas Chaudhary821d6e52010-04-28 11:41:21 +05301593 qla4_is_relogin_allowed(ha, conn_err)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001594 /*
1595 * This triggers a relogin. After the relogin_timer
1596 * expires, the relogin gets scheduled. We must wait a
1597 * minimum amount of time since receiving an 0x8014 AEN
1598 * with failed device_state or a logout response before
1599 * we can issue another relogin.
1600 */
Vikas Chaudhary821d6e52010-04-28 11:41:21 +05301601 /* Firmware pads this timeout: (time2wait +1).
David Somayajuluafaf5a22006-09-19 10:28:00 -07001602 * Driver retry to login should be longer than F/W.
1603 * Otherwise F/W will fail
1604 * set_ddb() mbx cmd with 0x4005 since it still
1605 * counting down its time2wait.
1606 */
1607 atomic_set(&ddb_entry->relogin_timer, 0);
1608 atomic_set(&ddb_entry->retry_relogin_timer,
1609 ddb_entry->default_time2wait + 4);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301610 DEBUG(printk("scsi%ld: %s: ddb[%d] "
1611 "initiate relogin after %d seconds\n",
1612 ha->host_no, __func__,
1613 ddb_entry->fw_ddb_index,
1614 ddb_entry->default_time2wait + 4));
1615 } else {
1616 DEBUG(printk("scsi%ld: %s: ddb[%d] "
1617 "relogin not initiated, state = %d, "
1618 "ddb_entry->flags = 0x%lx\n",
1619 ha->host_no, __func__,
1620 ddb_entry->fw_ddb_index,
1621 ddb_entry->fw_ddb_device_state,
1622 ddb_entry->flags));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001623 }
1624 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001625 return QLA_SUCCESS;
1626}