blob: b69c5736cb4361baea656e7bd2355b845c3c2733 [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
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530448 ql4_printk(KERN_INFO, ha, "Initializing firmware..\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700449 if (qla4xxx_initialize_fw_cb(ha) == QLA_ERROR) {
450 DEBUG2(printk("scsi%ld: %s: Failed to initialize firmware "
451 "control block\n", ha->host_no, __func__));
452 return status;
453 }
454 if (!qla4xxx_fw_ready(ha))
455 return status;
456
David Somayajuluafaf5a22006-09-19 10:28:00 -0700457 return qla4xxx_get_firmware_status(ha);
458}
459
460static struct ddb_entry* qla4xxx_get_ddb_entry(struct scsi_qla_host *ha,
David C Somayajulu92b72732007-05-23 17:55:40 -0700461 uint32_t fw_ddb_index,
462 uint32_t *new_tgt)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700463{
464 struct dev_db_entry *fw_ddb_entry = NULL;
465 dma_addr_t fw_ddb_entry_dma;
466 struct ddb_entry *ddb_entry = NULL;
467 int found = 0;
468 uint32_t device_state;
469
David C Somayajulu92b72732007-05-23 17:55:40 -0700470 *new_tgt = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700471 /* Make sure the dma buffer is valid */
472 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev,
473 sizeof(*fw_ddb_entry),
474 &fw_ddb_entry_dma, GFP_KERNEL);
475 if (fw_ddb_entry == NULL) {
476 DEBUG2(printk("scsi%ld: %s: Unable to allocate dma buffer.\n",
477 ha->host_no, __func__));
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530478 goto exit_get_ddb_entry_no_free;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700479 }
480
481 if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index, fw_ddb_entry,
482 fw_ddb_entry_dma, NULL, NULL,
483 &device_state, NULL, NULL, NULL) ==
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530484 QLA_ERROR) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700485 DEBUG2(printk("scsi%ld: %s: failed get_ddb_entry for "
486 "fw_ddb_index %d\n", ha->host_no, __func__,
487 fw_ddb_index));
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530488 goto exit_get_ddb_entry;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700489 }
490
491 /* Allocate DDB if not already allocated. */
492 DEBUG2(printk("scsi%ld: %s: Looking for ddb[%d]\n", ha->host_no,
493 __func__, fw_ddb_index));
494 list_for_each_entry(ddb_entry, &ha->ddb_list, list) {
Mike Christie41bbdbe2009-01-16 12:36:52 -0600495 if ((memcmp(ddb_entry->iscsi_name, fw_ddb_entry->iscsi_name,
496 ISCSI_NAME_SIZE) == 0) &&
497 (ddb_entry->tpgt ==
498 le32_to_cpu(fw_ddb_entry->tgt_portal_grp)) &&
499 (memcmp(ddb_entry->isid, fw_ddb_entry->isid,
500 sizeof(ddb_entry->isid)) == 0)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700501 found++;
502 break;
503 }
504 }
505
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530506 /* if not found allocate new ddb */
David Somayajuluafaf5a22006-09-19 10:28:00 -0700507 if (!found) {
508 DEBUG2(printk("scsi%ld: %s: ddb[%d] not found - allocating "
509 "new ddb\n", ha->host_no, __func__,
510 fw_ddb_index));
David C Somayajulu92b72732007-05-23 17:55:40 -0700511 *new_tgt = 1;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700512 ddb_entry = qla4xxx_alloc_ddb(ha, fw_ddb_index);
513 }
514
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530515exit_get_ddb_entry:
David Somayajuluafaf5a22006-09-19 10:28:00 -0700516 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry), fw_ddb_entry,
517 fw_ddb_entry_dma);
518
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530519exit_get_ddb_entry_no_free:
David Somayajuluafaf5a22006-09-19 10:28:00 -0700520 return ddb_entry;
521}
522
523/**
524 * qla4xxx_update_ddb_entry - update driver's internal ddb
525 * @ha: pointer to host adapter structure.
526 * @ddb_entry: pointer to device database structure to be filled
527 * @fw_ddb_index: index of the ddb entry in fw ddb table
528 *
529 * This routine updates the driver's internal device database entry
530 * with information retrieved from the firmware's device database
531 * entry for the specified device. The ddb_entry->fw_ddb_index field
532 * must be initialized prior to calling this routine
533 *
534 **/
Adrian Bunk47975472007-04-26 00:35:16 -0700535static int qla4xxx_update_ddb_entry(struct scsi_qla_host *ha,
536 struct ddb_entry *ddb_entry,
537 uint32_t fw_ddb_index)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700538{
539 struct dev_db_entry *fw_ddb_entry = NULL;
540 dma_addr_t fw_ddb_entry_dma;
541 int status = QLA_ERROR;
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530542 uint32_t conn_err;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700543
544 if (ddb_entry == NULL) {
545 DEBUG2(printk("scsi%ld: %s: ddb_entry is NULL\n", ha->host_no,
546 __func__));
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530547
548 goto exit_update_ddb_no_free;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700549 }
550
551 /* Make sure the dma buffer is valid */
552 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev,
553 sizeof(*fw_ddb_entry),
554 &fw_ddb_entry_dma, GFP_KERNEL);
555 if (fw_ddb_entry == NULL) {
556 DEBUG2(printk("scsi%ld: %s: Unable to allocate dma buffer.\n",
557 ha->host_no, __func__));
558
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530559 goto exit_update_ddb_no_free;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700560 }
561
562 if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index, fw_ddb_entry,
563 fw_ddb_entry_dma, NULL, NULL,
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530564 &ddb_entry->fw_ddb_device_state, &conn_err,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700565 &ddb_entry->tcp_source_port_num,
566 &ddb_entry->connection_id) ==
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530567 QLA_ERROR) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700568 DEBUG2(printk("scsi%ld: %s: failed get_ddb_entry for "
569 "fw_ddb_index %d\n", ha->host_no, __func__,
570 fw_ddb_index));
571
572 goto exit_update_ddb;
573 }
574
575 status = QLA_SUCCESS;
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530576 ddb_entry->options = le16_to_cpu(fw_ddb_entry->options);
David C Somayajulu92b72732007-05-23 17:55:40 -0700577 ddb_entry->target_session_id = le16_to_cpu(fw_ddb_entry->tsid);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700578 ddb_entry->task_mgmt_timeout =
David C Somayajulu92b72732007-05-23 17:55:40 -0700579 le16_to_cpu(fw_ddb_entry->def_timeout);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700580 ddb_entry->CmdSn = 0;
David C Somayajulu92b72732007-05-23 17:55:40 -0700581 ddb_entry->exe_throttle = le16_to_cpu(fw_ddb_entry->exec_throttle);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700582 ddb_entry->default_relogin_timeout =
David C Somayajulu92b72732007-05-23 17:55:40 -0700583 le16_to_cpu(fw_ddb_entry->def_timeout);
584 ddb_entry->default_time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700585
586 /* Update index in case it changed */
587 ddb_entry->fw_ddb_index = fw_ddb_index;
588 ha->fw_ddb_index_map[fw_ddb_index] = ddb_entry;
589
David C Somayajulu92b72732007-05-23 17:55:40 -0700590 ddb_entry->port = le16_to_cpu(fw_ddb_entry->port);
591 ddb_entry->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
Mike Christie41bbdbe2009-01-16 12:36:52 -0600592 memcpy(ddb_entry->isid, fw_ddb_entry->isid, sizeof(ddb_entry->isid));
593
David C Somayajulu92b72732007-05-23 17:55:40 -0700594 memcpy(&ddb_entry->iscsi_name[0], &fw_ddb_entry->iscsi_name[0],
David Somayajuluafaf5a22006-09-19 10:28:00 -0700595 min(sizeof(ddb_entry->iscsi_name),
David C Somayajulu92b72732007-05-23 17:55:40 -0700596 sizeof(fw_ddb_entry->iscsi_name)));
Vikas Chaudhary3b2bef12010-07-10 14:51:30 +0530597 memcpy(&ddb_entry->iscsi_alias[0], &fw_ddb_entry->iscsi_alias[0],
598 min(sizeof(ddb_entry->iscsi_alias),
599 sizeof(fw_ddb_entry->iscsi_alias)));
David C Somayajulu92b72732007-05-23 17:55:40 -0700600 memcpy(&ddb_entry->ip_addr[0], &fw_ddb_entry->ip_addr[0],
601 min(sizeof(ddb_entry->ip_addr), sizeof(fw_ddb_entry->ip_addr)));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700602
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530603 ddb_entry->iscsi_max_burst_len = fw_ddb_entry->iscsi_max_burst_len;
604 ddb_entry->iscsi_max_outsnd_r2t = fw_ddb_entry->iscsi_max_outsnd_r2t;
605 ddb_entry->iscsi_first_burst_len = fw_ddb_entry->iscsi_first_burst_len;
606 ddb_entry->iscsi_max_rcv_data_seg_len =
607 fw_ddb_entry->iscsi_max_rcv_data_seg_len;
608 ddb_entry->iscsi_max_snd_data_seg_len =
609 fw_ddb_entry->iscsi_max_snd_data_seg_len;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700610
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530611 if (ddb_entry->options & DDB_OPT_IPV6_DEVICE) {
612 memcpy(&ddb_entry->remote_ipv6_addr,
613 fw_ddb_entry->ip_addr,
614 min(sizeof(ddb_entry->remote_ipv6_addr),
615 sizeof(fw_ddb_entry->ip_addr)));
616 memcpy(&ddb_entry->link_local_ipv6_addr,
617 fw_ddb_entry->link_local_ipv6_addr,
618 min(sizeof(ddb_entry->link_local_ipv6_addr),
619 sizeof(fw_ddb_entry->link_local_ipv6_addr)));
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530620
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530621 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: DDB[%d] State %04x"
622 " ConnErr %08x IP %pI6 "
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530623 ":%04d \"%s\"\n",
624 __func__, fw_ddb_index,
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530625 ddb_entry->fw_ddb_device_state,
626 conn_err, fw_ddb_entry->ip_addr,
627 le16_to_cpu(fw_ddb_entry->port),
628 fw_ddb_entry->iscsi_name));
629 } else
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530630 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: DDB[%d] State %04x"
631 " ConnErr %08x IP %pI4 "
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530632 ":%04d \"%s\"\n",
633 __func__, fw_ddb_index,
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530634 ddb_entry->fw_ddb_device_state,
635 conn_err, fw_ddb_entry->ip_addr,
636 le16_to_cpu(fw_ddb_entry->port),
637 fw_ddb_entry->iscsi_name));
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530638exit_update_ddb:
David Somayajuluafaf5a22006-09-19 10:28:00 -0700639 if (fw_ddb_entry)
640 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
641 fw_ddb_entry, fw_ddb_entry_dma);
642
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530643exit_update_ddb_no_free:
David Somayajuluafaf5a22006-09-19 10:28:00 -0700644 return status;
645}
646
647/**
648 * qla4xxx_alloc_ddb - allocate device database entry
649 * @ha: Pointer to host adapter structure.
650 * @fw_ddb_index: Firmware's device database index
651 *
652 * This routine allocates a ddb_entry, ititializes some values, and
653 * inserts it into the ddb list.
654 **/
Adrian Bunk47975472007-04-26 00:35:16 -0700655static struct ddb_entry * qla4xxx_alloc_ddb(struct scsi_qla_host *ha,
656 uint32_t fw_ddb_index)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700657{
658 struct ddb_entry *ddb_entry;
659
660 DEBUG2(printk("scsi%ld: %s: fw_ddb_index [%d]\n", ha->host_no,
661 __func__, fw_ddb_index));
662
663 ddb_entry = qla4xxx_alloc_sess(ha);
664 if (ddb_entry == NULL) {
665 DEBUG2(printk("scsi%ld: %s: Unable to allocate memory "
666 "to add fw_ddb_index [%d]\n",
667 ha->host_no, __func__, fw_ddb_index));
668 return ddb_entry;
669 }
670
671 ddb_entry->fw_ddb_index = fw_ddb_index;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700672 atomic_set(&ddb_entry->retry_relogin_timer, INVALID_ENTRY);
673 atomic_set(&ddb_entry->relogin_timer, 0);
674 atomic_set(&ddb_entry->relogin_retry_count, 0);
675 atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
676 list_add_tail(&ddb_entry->list, &ha->ddb_list);
677 ha->fw_ddb_index_map[fw_ddb_index] = ddb_entry;
678 ha->tot_ddbs++;
679
680 return ddb_entry;
681}
682
683/**
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530684 * qla4_is_relogin_allowed - Are we allowed to login?
685 * @ha: Pointer to host adapter structure.
686 * @conn_err: Last connection error associated with the ddb
687 *
688 * This routine tests the given connection error to determine if
689 * we are allowed to login.
690 **/
691int qla4_is_relogin_allowed(struct scsi_qla_host *ha, uint32_t conn_err)
692{
693 uint32_t err_code, login_rsp_sts_class;
694 int relogin = 1;
695
696 err_code = ((conn_err & 0x00ff0000) >> 16);
697 login_rsp_sts_class = ((conn_err & 0x0000ff00) >> 8);
698 if (err_code == 0x1c || err_code == 0x06) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530699 DEBUG2(ql4_printk(KERN_INFO, ha,
700 ": conn_err=0x%08x, send target completed"
701 " or access denied failure\n", conn_err));
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530702 relogin = 0;
703 }
704 if ((err_code == 0x08) && (login_rsp_sts_class == 0x02)) {
705 /* Login Response PDU returned an error.
706 Login Response Status in Error Code Detail
707 indicates login should not be retried.*/
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530708 DEBUG2(ql4_printk(KERN_INFO, ha,
709 ": conn_err=0x%08x, do not retry relogin\n",
710 conn_err));
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530711 relogin = 0;
712 }
713
714 return relogin;
715}
716
717/**
David Somayajuluafaf5a22006-09-19 10:28:00 -0700718 * qla4xxx_configure_ddbs - builds driver ddb list
719 * @ha: Pointer to host adapter structure.
720 *
721 * This routine searches for all valid firmware ddb entries and builds
722 * an internal ddb list. Ddbs that are considered valid are those with
723 * a device state of SESSION_ACTIVE.
724 **/
725static int qla4xxx_build_ddb_list(struct scsi_qla_host *ha)
726{
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530727 int status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700728 uint32_t fw_ddb_index = 0;
729 uint32_t next_fw_ddb_index = 0;
730 uint32_t ddb_state;
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530731 uint32_t conn_err;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700732 struct ddb_entry *ddb_entry;
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530733 struct dev_db_entry *fw_ddb_entry = NULL;
734 dma_addr_t fw_ddb_entry_dma;
735 uint32_t ipv6_device;
David C Somayajulu92b72732007-05-23 17:55:40 -0700736 uint32_t new_tgt;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700737
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530738 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
739 &fw_ddb_entry_dma, GFP_KERNEL);
740 if (fw_ddb_entry == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530741 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: DMA alloc failed\n",
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530742 __func__));
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530743
744 goto exit_build_ddb_list_no_free;
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530745 }
746
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530747 ql4_printk(KERN_INFO, ha, "Initializing DDBs ...\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700748 for (fw_ddb_index = 0; fw_ddb_index < MAX_DDB_ENTRIES;
749 fw_ddb_index = next_fw_ddb_index) {
750 /* First, let's see if a device exists here */
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530751 if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index, fw_ddb_entry,
752 0, NULL, &next_fw_ddb_index,
753 &ddb_state, &conn_err,
754 NULL, NULL) ==
755 QLA_ERROR) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700756 DEBUG2(printk("scsi%ld: %s: get_ddb_entry, "
757 "fw_ddb_index %d failed", ha->host_no,
758 __func__, fw_ddb_index));
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530759 goto exit_build_ddb_list;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700760 }
761
762 DEBUG2(printk("scsi%ld: %s: Getting DDB[%d] ddbstate=0x%x, "
763 "next_fw_ddb_index=%d.\n", ha->host_no, __func__,
764 fw_ddb_index, ddb_state, next_fw_ddb_index));
765
766 /* Issue relogin, if necessary. */
767 if (ddb_state == DDB_DS_SESSION_FAILED ||
768 ddb_state == DDB_DS_NO_CONNECTION_ACTIVE) {
769 /* Try and login to device */
770 DEBUG2(printk("scsi%ld: %s: Login to DDB[%d]\n",
771 ha->host_no, __func__, fw_ddb_index));
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530772 ipv6_device = le16_to_cpu(fw_ddb_entry->options) &
773 DDB_OPT_IPV6_DEVICE;
774 if (qla4_is_relogin_allowed(ha, conn_err) &&
775 ((!ipv6_device &&
776 *((uint32_t *)fw_ddb_entry->ip_addr))
777 || ipv6_device)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700778 qla4xxx_set_ddb_entry(ha, fw_ddb_index, 0);
David C Somayajulu92b72732007-05-23 17:55:40 -0700779 if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index,
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530780 NULL, 0, NULL,
781 &next_fw_ddb_index,
782 &ddb_state, &conn_err,
783 NULL, NULL)
784 == QLA_ERROR) {
David C Somayajulu92b72732007-05-23 17:55:40 -0700785 DEBUG2(printk("scsi%ld: %s:"
786 "get_ddb_entry %d failed\n",
787 ha->host_no,
788 __func__, fw_ddb_index));
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530789 goto exit_build_ddb_list;
David C Somayajulu92b72732007-05-23 17:55:40 -0700790 }
791 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700792 }
793
794 if (ddb_state != DDB_DS_SESSION_ACTIVE)
795 goto next_one;
796 /*
797 * if fw_ddb with session active state found,
798 * add to ddb_list
799 */
800 DEBUG2(printk("scsi%ld: %s: DDB[%d] added to list\n",
801 ha->host_no, __func__, fw_ddb_index));
802
803 /* Add DDB to internal our ddb list. */
David C Somayajulu92b72732007-05-23 17:55:40 -0700804 ddb_entry = qla4xxx_get_ddb_entry(ha, fw_ddb_index, &new_tgt);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700805 if (ddb_entry == NULL) {
806 DEBUG2(printk("scsi%ld: %s: Unable to allocate memory "
807 "for device at fw_ddb_index %d\n",
808 ha->host_no, __func__, fw_ddb_index));
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530809 goto exit_build_ddb_list;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700810 }
811 /* Fill in the device structure */
812 if (qla4xxx_update_ddb_entry(ha, ddb_entry, fw_ddb_index) ==
813 QLA_ERROR) {
814 ha->fw_ddb_index_map[fw_ddb_index] =
815 (struct ddb_entry *)INVALID_ENTRY;
816
David Somayajuluafaf5a22006-09-19 10:28:00 -0700817 DEBUG2(printk("scsi%ld: %s: update_ddb_entry failed "
818 "for fw_ddb_index %d.\n",
819 ha->host_no, __func__, fw_ddb_index));
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530820 goto exit_build_ddb_list;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700821 }
822
823next_one:
824 /* We know we've reached the last device when
825 * next_fw_ddb_index is 0 */
826 if (next_fw_ddb_index == 0)
827 break;
828 }
829
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530830 status = QLA_SUCCESS;
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530831 ql4_printk(KERN_INFO, ha, "DDB list done..\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700832
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530833exit_build_ddb_list:
834 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry), fw_ddb_entry,
835 fw_ddb_entry_dma);
836
837exit_build_ddb_list_no_free:
David Somayajuluafaf5a22006-09-19 10:28:00 -0700838 return status;
839}
840
841struct qla4_relog_scan {
842 int halt_wait;
843 uint32_t conn_err;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700844 uint32_t fw_ddb_index;
845 uint32_t next_fw_ddb_index;
846 uint32_t fw_ddb_device_state;
847};
848
849static int qla4_test_rdy(struct scsi_qla_host *ha, struct qla4_relog_scan *rs)
850{
851 struct ddb_entry *ddb_entry;
852
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530853 if (qla4_is_relogin_allowed(ha, rs->conn_err)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700854 /* We either have a device that is in
855 * the process of relogging in or a
856 * device that is waiting to be
857 * relogged in */
858 rs->halt_wait = 0;
859
860 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha,
861 rs->fw_ddb_index);
862 if (ddb_entry == NULL)
863 return QLA_ERROR;
864
865 if (ddb_entry->dev_scan_wait_to_start_relogin != 0
866 && time_after_eq(jiffies,
867 ddb_entry->
868 dev_scan_wait_to_start_relogin))
869 {
870 ddb_entry->dev_scan_wait_to_start_relogin = 0;
871 qla4xxx_set_ddb_entry(ha, rs->fw_ddb_index, 0);
872 }
873 }
874 return QLA_SUCCESS;
875}
876
877static int qla4_scan_for_relogin(struct scsi_qla_host *ha,
878 struct qla4_relog_scan *rs)
879{
880 int error;
881
882 /* scan for relogins
883 * ----------------- */
884 for (rs->fw_ddb_index = 0; rs->fw_ddb_index < MAX_DDB_ENTRIES;
885 rs->fw_ddb_index = rs->next_fw_ddb_index) {
886 if (qla4xxx_get_fwddb_entry(ha, rs->fw_ddb_index, NULL, 0,
887 NULL, &rs->next_fw_ddb_index,
888 &rs->fw_ddb_device_state,
889 &rs->conn_err, NULL, NULL)
890 == QLA_ERROR)
891 return QLA_ERROR;
892
893 if (rs->fw_ddb_device_state == DDB_DS_LOGIN_IN_PROCESS)
894 rs->halt_wait = 0;
895
896 if (rs->fw_ddb_device_state == DDB_DS_SESSION_FAILED ||
897 rs->fw_ddb_device_state == DDB_DS_NO_CONNECTION_ACTIVE) {
898 error = qla4_test_rdy(ha, rs);
899 if (error)
900 return error;
901 }
902
903 /* We know we've reached the last device when
904 * next_fw_ddb_index is 0 */
905 if (rs->next_fw_ddb_index == 0)
906 break;
907 }
908 return QLA_SUCCESS;
909}
910
911/**
912 * qla4xxx_devices_ready - wait for target devices to be logged in
913 * @ha: pointer to adapter structure
914 *
915 * This routine waits up to ql4xdiscoverywait seconds
916 * F/W database during driver load time.
917 **/
918static int qla4xxx_devices_ready(struct scsi_qla_host *ha)
919{
920 int error;
921 unsigned long discovery_wtime;
922 struct qla4_relog_scan rs;
923
924 discovery_wtime = jiffies + (ql4xdiscoverywait * HZ);
925
926 DEBUG(printk("Waiting (%d) for devices ...\n", ql4xdiscoverywait));
927 do {
928 /* poll for AEN. */
929 qla4xxx_get_firmware_state(ha);
930 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags)) {
931 /* Set time-between-relogin timer */
932 qla4xxx_process_aen(ha, RELOGIN_DDB_CHANGED_AENS);
933 }
934
935 /* if no relogins active or needed, halt discvery wait */
936 rs.halt_wait = 1;
937
938 error = qla4_scan_for_relogin(ha, &rs);
939
940 if (rs.halt_wait) {
941 DEBUG2(printk("scsi%ld: %s: Delay halted. Devices "
942 "Ready.\n", ha->host_no, __func__));
943 return QLA_SUCCESS;
944 }
945
946 msleep(2000);
947 } while (!time_after_eq(jiffies, discovery_wtime));
948
949 DEBUG3(qla4xxx_get_conn_event_log(ha));
950
951 return QLA_SUCCESS;
952}
953
954static void qla4xxx_flush_AENS(struct scsi_qla_host *ha)
955{
956 unsigned long wtime;
957
958 /* Flush the 0x8014 AEN from the firmware as a result of
959 * Auto connect. We are basically doing get_firmware_ddb()
960 * to determine whether we need to log back in or not.
961 * Trying to do a set ddb before we have processed 0x8014
962 * will result in another set_ddb() for the same ddb. In other
963 * words there will be stale entries in the aen_q.
964 */
965 wtime = jiffies + (2 * HZ);
966 do {
967 if (qla4xxx_get_firmware_state(ha) == QLA_SUCCESS)
968 if (ha->firmware_state & (BIT_2 | BIT_0))
969 return;
970
971 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
972 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
973
974 msleep(1000);
975 } while (!time_after_eq(jiffies, wtime));
976
977}
978
979static int qla4xxx_initialize_ddb_list(struct scsi_qla_host *ha)
980{
981 uint16_t fw_ddb_index;
982 int status = QLA_SUCCESS;
983
984 /* free the ddb list if is not empty */
985 if (!list_empty(&ha->ddb_list))
986 qla4xxx_free_ddb_list(ha);
987
988 for (fw_ddb_index = 0; fw_ddb_index < MAX_DDB_ENTRIES; fw_ddb_index++)
989 ha->fw_ddb_index_map[fw_ddb_index] =
990 (struct ddb_entry *)INVALID_ENTRY;
991
992 ha->tot_ddbs = 0;
993
994 qla4xxx_flush_AENS(ha);
995
Karen Higginsbb6f7d52010-07-10 14:51:17 +0530996 /* Wait for an AEN */
997 qla4xxx_devices_ready(ha);
998
David Somayajuluafaf5a22006-09-19 10:28:00 -0700999 /*
1000 * First perform device discovery for active
1001 * fw ddb indexes and build
1002 * ddb list.
1003 */
1004 if ((status = qla4xxx_build_ddb_list(ha)) == QLA_ERROR)
1005 return status;
1006
David Somayajuluafaf5a22006-09-19 10:28:00 -07001007 /*
1008 * Targets can come online after the inital discovery, so processing
1009 * the aens here will catch them.
1010 */
1011 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
1012 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
1013
1014 return status;
1015}
1016
1017/**
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301018 * qla4xxx_reinitialize_ddb_list - update the driver ddb list
David Somayajuluafaf5a22006-09-19 10:28:00 -07001019 * @ha: pointer to host adapter structure.
1020 *
1021 * This routine obtains device information from the F/W database after
1022 * firmware or adapter resets. The device table is preserved.
1023 **/
1024int qla4xxx_reinitialize_ddb_list(struct scsi_qla_host *ha)
1025{
1026 int status = QLA_SUCCESS;
1027 struct ddb_entry *ddb_entry, *detemp;
1028
1029 /* Update the device information for all devices. */
1030 list_for_each_entry_safe(ddb_entry, detemp, &ha->ddb_list, list) {
1031 qla4xxx_update_ddb_entry(ha, ddb_entry,
1032 ddb_entry->fw_ddb_index);
1033 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
1034 atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
1035 DEBUG2(printk ("scsi%ld: %s: ddb index [%d] marked "
1036 "ONLINE\n", ha->host_no, __func__,
1037 ddb_entry->fw_ddb_index));
Vikas Chaudhary36386322010-07-10 14:49:01 +05301038 iscsi_unblock_session(ddb_entry->sess);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001039 } else if (atomic_read(&ddb_entry->state) == DDB_STATE_ONLINE)
1040 qla4xxx_mark_device_missing(ha, ddb_entry);
1041 }
1042 return status;
1043}
1044
1045/**
1046 * qla4xxx_relogin_device - re-establish session
1047 * @ha: Pointer to host adapter structure.
1048 * @ddb_entry: Pointer to device database entry
1049 *
1050 * This routine does a session relogin with the specified device.
1051 * The ddb entry must be assigned prior to making this call.
1052 **/
1053int qla4xxx_relogin_device(struct scsi_qla_host *ha,
1054 struct ddb_entry * ddb_entry)
1055{
1056 uint16_t relogin_timer;
1057
1058 relogin_timer = max(ddb_entry->default_relogin_timeout,
1059 (uint16_t)RELOGIN_TOV);
1060 atomic_set(&ddb_entry->relogin_timer, relogin_timer);
1061
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301062 DEBUG2(printk("scsi%ld: Relogin ddb [%d]. TOV=%d\n", ha->host_no,
David Somayajuluafaf5a22006-09-19 10:28:00 -07001063 ddb_entry->fw_ddb_index, relogin_timer));
1064
1065 qla4xxx_set_ddb_entry(ha, ddb_entry->fw_ddb_index, 0);
1066
1067 return QLA_SUCCESS;
1068}
1069
David Somayajuluafaf5a22006-09-19 10:28:00 -07001070static int qla4xxx_config_nvram(struct scsi_qla_host *ha)
1071{
1072 unsigned long flags;
1073 union external_hw_config_reg extHwConfig;
1074
1075 DEBUG2(printk("scsi%ld: %s: Get EEProm parameters \n", ha->host_no,
1076 __func__));
1077 if (ql4xxx_lock_flash(ha) != QLA_SUCCESS)
Mike Christieb3925512010-02-10 16:51:48 -06001078 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001079 if (ql4xxx_lock_nvram(ha) != QLA_SUCCESS) {
1080 ql4xxx_unlock_flash(ha);
Mike Christieb3925512010-02-10 16:51:48 -06001081 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001082 }
1083
1084 /* Get EEPRom Parameters from NVRAM and validate */
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301085 ql4_printk(KERN_INFO, ha, "Configuring NVRAM ...\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001086 if (qla4xxx_is_nvram_configuration_valid(ha) == QLA_SUCCESS) {
1087 spin_lock_irqsave(&ha->hardware_lock, flags);
1088 extHwConfig.Asuint32_t =
1089 rd_nvram_word(ha, eeprom_ext_hw_conf_offset(ha));
1090 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1091 } else {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301092 ql4_printk(KERN_WARNING, ha,
1093 "scsi%ld: %s: EEProm checksum invalid. "
1094 "Please update your EEPROM\n", ha->host_no,
1095 __func__);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001096
Mike Christieb3925512010-02-10 16:51:48 -06001097 /* Attempt to set defaults */
David Somayajuluafaf5a22006-09-19 10:28:00 -07001098 if (is_qla4010(ha))
1099 extHwConfig.Asuint32_t = 0x1912;
David C Somayajulud9150582006-11-15 17:38:40 -08001100 else if (is_qla4022(ha) | is_qla4032(ha))
David Somayajuluafaf5a22006-09-19 10:28:00 -07001101 extHwConfig.Asuint32_t = 0x0023;
Mike Christieb3925512010-02-10 16:51:48 -06001102 else
1103 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001104 }
1105 DEBUG(printk("scsi%ld: %s: Setting extHwConfig to 0xFFFF%04x\n",
1106 ha->host_no, __func__, extHwConfig.Asuint32_t));
1107
1108 spin_lock_irqsave(&ha->hardware_lock, flags);
1109 writel((0xFFFF << 16) | extHwConfig.Asuint32_t, isp_ext_hw_conf(ha));
1110 readl(isp_ext_hw_conf(ha));
1111 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1112
1113 ql4xxx_unlock_nvram(ha);
1114 ql4xxx_unlock_flash(ha);
1115
Mike Christieb3925512010-02-10 16:51:48 -06001116 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001117}
1118
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301119/**
1120 * qla4_8xxx_pci_config() - Setup ISP82xx PCI configuration registers.
1121 * @ha: HA context
1122 */
1123void qla4_8xxx_pci_config(struct scsi_qla_host *ha)
1124{
1125 pci_set_master(ha->pdev);
1126}
1127
1128void qla4xxx_pci_config(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001129{
David C Somayajulu92b72732007-05-23 17:55:40 -07001130 uint16_t w;
David C Somayajulu46235e62007-06-08 17:37:16 -07001131 int status;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001132
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301133 ql4_printk(KERN_INFO, ha, "Configuring PCI space...\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001134
1135 pci_set_master(ha->pdev);
David C Somayajulu46235e62007-06-08 17:37:16 -07001136 status = pci_set_mwi(ha->pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001137 /*
1138 * We want to respect framework's setting of PCI configuration space
1139 * command register and also want to make sure that all bits of
1140 * interest to us are properly set in command register.
1141 */
1142 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
David C Somayajulu92b72732007-05-23 17:55:40 -07001143 w |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001144 w &= ~PCI_COMMAND_INTX_DISABLE;
1145 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
1146}
1147
1148static int qla4xxx_start_firmware_from_flash(struct scsi_qla_host *ha)
1149{
1150 int status = QLA_ERROR;
Vikas Chaudharyc301b022010-04-28 11:40:37 +05301151 unsigned long max_wait_time;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001152 unsigned long flags;
1153 uint32_t mbox_status;
1154
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301155 ql4_printk(KERN_INFO, ha, "Starting firmware ...\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001156
1157 /*
1158 * Start firmware from flash ROM
1159 *
1160 * WORKAROUND: Stuff a non-constant value that the firmware can
1161 * use as a seed for a random number generator in MB7 prior to
1162 * setting BOOT_ENABLE. Fixes problem where the TCP
1163 * connections use the same TCP ports after each reboot,
1164 * causing some connections to not get re-established.
1165 */
1166 DEBUG(printk("scsi%d: %s: Start firmware from flash ROM\n",
1167 ha->host_no, __func__));
1168
1169 spin_lock_irqsave(&ha->hardware_lock, flags);
1170 writel(jiffies, &ha->reg->mailbox[7]);
David C Somayajulud9150582006-11-15 17:38:40 -08001171 if (is_qla4022(ha) | is_qla4032(ha))
David Somayajuluafaf5a22006-09-19 10:28:00 -07001172 writel(set_rmask(NVR_WRITE_ENABLE),
1173 &ha->reg->u1.isp4022.nvram);
1174
David C Somayajulu92b72732007-05-23 17:55:40 -07001175 writel(2, &ha->reg->mailbox[6]);
1176 readl(&ha->reg->mailbox[6]);
1177
David Somayajuluafaf5a22006-09-19 10:28:00 -07001178 writel(set_rmask(CSR_BOOT_ENABLE), &ha->reg->ctrl_status);
1179 readl(&ha->reg->ctrl_status);
1180 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1181
1182 /* Wait for firmware to come UP. */
Vikas Chaudharyc301b022010-04-28 11:40:37 +05301183 DEBUG2(printk(KERN_INFO "scsi%ld: %s: Wait up to %d seconds for "
1184 "boot firmware to complete...\n",
1185 ha->host_no, __func__, FIRMWARE_UP_TOV));
1186 max_wait_time = jiffies + (FIRMWARE_UP_TOV * HZ);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001187 do {
1188 uint32_t ctrl_status;
1189
1190 spin_lock_irqsave(&ha->hardware_lock, flags);
1191 ctrl_status = readw(&ha->reg->ctrl_status);
1192 mbox_status = readw(&ha->reg->mailbox[0]);
1193 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1194
1195 if (ctrl_status & set_rmask(CSR_SCSI_PROCESSOR_INTR))
1196 break;
1197 if (mbox_status == MBOX_STS_COMMAND_COMPLETE)
1198 break;
1199
Vikas Chaudharyc301b022010-04-28 11:40:37 +05301200 DEBUG2(printk(KERN_INFO "scsi%ld: %s: Waiting for boot "
1201 "firmware to complete... ctrl_sts=0x%x\n",
1202 ha->host_no, __func__, ctrl_status));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001203
Vikas Chaudharyc301b022010-04-28 11:40:37 +05301204 msleep_interruptible(250);
1205 } while (!time_after_eq(jiffies, max_wait_time));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001206
1207 if (mbox_status == MBOX_STS_COMMAND_COMPLETE) {
Vikas Chaudharyc301b022010-04-28 11:40:37 +05301208 DEBUG(printk(KERN_INFO "scsi%ld: %s: Firmware has started\n",
David Somayajuluafaf5a22006-09-19 10:28:00 -07001209 ha->host_no, __func__));
1210
1211 spin_lock_irqsave(&ha->hardware_lock, flags);
1212 writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
1213 &ha->reg->ctrl_status);
1214 readl(&ha->reg->ctrl_status);
1215 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1216
1217 status = QLA_SUCCESS;
1218 } else {
1219 printk(KERN_INFO "scsi%ld: %s: Boot firmware failed "
1220 "- mbox status 0x%x\n", ha->host_no, __func__,
1221 mbox_status);
1222 status = QLA_ERROR;
1223 }
1224 return status;
1225}
1226
David C Somayajulu92b72732007-05-23 17:55:40 -07001227int ql4xxx_lock_drvr_wait(struct scsi_qla_host *a)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001228{
David C Somayajulu92b72732007-05-23 17:55:40 -07001229#define QL4_LOCK_DRVR_WAIT 60
David C Somayajulu477ffb92007-01-22 12:26:11 -08001230#define QL4_LOCK_DRVR_SLEEP 1
David Somayajuluafaf5a22006-09-19 10:28:00 -07001231
1232 int drvr_wait = QL4_LOCK_DRVR_WAIT;
1233 while (drvr_wait) {
David C Somayajulu92b72732007-05-23 17:55:40 -07001234 if (ql4xxx_lock_drvr(a) == 0) {
David C Somayajulu477ffb92007-01-22 12:26:11 -08001235 ssleep(QL4_LOCK_DRVR_SLEEP);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001236 if (drvr_wait) {
1237 DEBUG2(printk("scsi%ld: %s: Waiting for "
David C Somayajulu92b72732007-05-23 17:55:40 -07001238 "Global Init Semaphore(%d)...\n",
1239 a->host_no,
David C Somayajulu477ffb92007-01-22 12:26:11 -08001240 __func__, drvr_wait));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001241 }
1242 drvr_wait -= QL4_LOCK_DRVR_SLEEP;
1243 } else {
1244 DEBUG2(printk("scsi%ld: %s: Global Init Semaphore "
David C Somayajulu92b72732007-05-23 17:55:40 -07001245 "acquired\n", a->host_no, __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001246 return QLA_SUCCESS;
1247 }
1248 }
1249 return QLA_ERROR;
1250}
1251
1252/**
1253 * qla4xxx_start_firmware - starts qla4xxx firmware
1254 * @ha: Pointer to host adapter structure.
1255 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001256 * This routine performs the necessary steps to start the firmware for
David Somayajuluafaf5a22006-09-19 10:28:00 -07001257 * the QLA4010 adapter.
1258 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301259int qla4xxx_start_firmware(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001260{
1261 unsigned long flags = 0;
1262 uint32_t mbox_status;
1263 int status = QLA_ERROR;
1264 int soft_reset = 1;
1265 int config_chip = 0;
1266
David C Somayajulud9150582006-11-15 17:38:40 -08001267 if (is_qla4022(ha) | is_qla4032(ha))
David Somayajuluafaf5a22006-09-19 10:28:00 -07001268 ql4xxx_set_mac_number(ha);
1269
1270 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
1271 return QLA_ERROR;
1272
1273 spin_lock_irqsave(&ha->hardware_lock, flags);
1274
1275 DEBUG2(printk("scsi%ld: %s: port_ctrl = 0x%08X\n", ha->host_no,
1276 __func__, readw(isp_port_ctrl(ha))));
1277 DEBUG(printk("scsi%ld: %s: port_status = 0x%08X\n", ha->host_no,
1278 __func__, readw(isp_port_status(ha))));
1279
1280 /* Is Hardware already initialized? */
1281 if ((readw(isp_port_ctrl(ha)) & 0x8000) != 0) {
1282 DEBUG(printk("scsi%ld: %s: Hardware has already been "
1283 "initialized\n", ha->host_no, __func__));
1284
1285 /* Receive firmware boot acknowledgement */
1286 mbox_status = readw(&ha->reg->mailbox[0]);
1287
1288 DEBUG2(printk("scsi%ld: %s: H/W Config complete - mbox[0]= "
1289 "0x%x\n", ha->host_no, __func__, mbox_status));
1290
1291 /* Is firmware already booted? */
1292 if (mbox_status == 0) {
1293 /* F/W not running, must be config by net driver */
1294 config_chip = 1;
1295 soft_reset = 0;
1296 } else {
1297 writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
1298 &ha->reg->ctrl_status);
1299 readl(&ha->reg->ctrl_status);
1300 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1301 if (qla4xxx_get_firmware_state(ha) == QLA_SUCCESS) {
1302 DEBUG2(printk("scsi%ld: %s: Get firmware "
1303 "state -- state = 0x%x\n",
1304 ha->host_no,
1305 __func__, ha->firmware_state));
1306 /* F/W is running */
1307 if (ha->firmware_state &
1308 FW_STATE_CONFIG_WAIT) {
1309 DEBUG2(printk("scsi%ld: %s: Firmware "
1310 "in known state -- "
1311 "config and "
1312 "boot, state = 0x%x\n",
1313 ha->host_no, __func__,
1314 ha->firmware_state));
1315 config_chip = 1;
1316 soft_reset = 0;
1317 }
1318 } else {
1319 DEBUG2(printk("scsi%ld: %s: Firmware in "
1320 "unknown state -- resetting,"
1321 " state = "
1322 "0x%x\n", ha->host_no, __func__,
1323 ha->firmware_state));
1324 }
1325 spin_lock_irqsave(&ha->hardware_lock, flags);
1326 }
1327 } else {
1328 DEBUG(printk("scsi%ld: %s: H/W initialization hasn't been "
1329 "started - resetting\n", ha->host_no, __func__));
1330 }
1331 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1332
1333 DEBUG(printk("scsi%ld: %s: Flags soft_rest=%d, config= %d\n ",
1334 ha->host_no, __func__, soft_reset, config_chip));
1335 if (soft_reset) {
1336 DEBUG(printk("scsi%ld: %s: Issue Soft Reset\n", ha->host_no,
1337 __func__));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301338 status = qla4xxx_soft_reset(ha); /* NOTE: acquires drvr
1339 * lock again, but ok */
David Somayajuluafaf5a22006-09-19 10:28:00 -07001340 if (status == QLA_ERROR) {
1341 DEBUG(printk("scsi%d: %s: Soft Reset failed!\n",
1342 ha->host_no, __func__));
1343 ql4xxx_unlock_drvr(ha);
1344 return QLA_ERROR;
1345 }
1346 config_chip = 1;
1347
Joe Perchesb1c11812008-02-03 17:28:22 +02001348 /* Reset clears the semaphore, so acquire again */
David Somayajuluafaf5a22006-09-19 10:28:00 -07001349 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
1350 return QLA_ERROR;
1351 }
1352
1353 if (config_chip) {
1354 if ((status = qla4xxx_config_nvram(ha)) == QLA_SUCCESS)
1355 status = qla4xxx_start_firmware_from_flash(ha);
1356 }
1357
1358 ql4xxx_unlock_drvr(ha);
1359 if (status == QLA_SUCCESS) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001360 if (test_and_clear_bit(AF_GET_CRASH_RECORD, &ha->flags))
1361 qla4xxx_get_crash_record(ha);
1362 } else {
1363 DEBUG(printk("scsi%ld: %s: Firmware has NOT started\n",
1364 ha->host_no, __func__));
1365 }
1366 return status;
1367}
1368
1369
1370/**
1371 * qla4xxx_initialize_adapter - initiailizes hba
1372 * @ha: Pointer to host adapter structure.
1373 * @renew_ddb_list: Indicates what to do with the adapter's ddb list
1374 * after adapter recovery has completed.
1375 * 0=preserve ddb list, 1=destroy and rebuild ddb list
1376 *
1377 * This routine parforms all of the steps necessary to initialize the adapter.
1378 *
1379 **/
1380int qla4xxx_initialize_adapter(struct scsi_qla_host *ha,
1381 uint8_t renew_ddb_list)
1382{
1383 int status = QLA_ERROR;
1384 int8_t ip_address[IP_ADDR_LEN] = {0} ;
1385
1386 ha->eeprom_cmd_data = 0;
1387
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301388 ql4_printk(KERN_INFO, ha, "Configuring PCI space...\n");
1389 ha->isp_ops->pci_config(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001390
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301391 ha->isp_ops->disable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001392
1393 /* Initialize the Host adapter request/response queues and firmware */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301394 if (ha->isp_ops->start_firmware(ha) == QLA_ERROR)
David C Somayajulu46235e62007-06-08 17:37:16 -07001395 goto exit_init_hba;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001396
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301397 if (qla4xxx_get_fw_version(ha) == QLA_ERROR)
1398 goto exit_init_hba;
1399
1400 if (ha->isp_ops->get_sys_info(ha) == QLA_ERROR)
David C Somayajulu46235e62007-06-08 17:37:16 -07001401 goto exit_init_hba;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001402
1403 if (qla4xxx_init_local_data(ha) == QLA_ERROR)
David C Somayajulu46235e62007-06-08 17:37:16 -07001404 goto exit_init_hba;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001405
1406 status = qla4xxx_init_firmware(ha);
1407 if (status == QLA_ERROR)
David C Somayajulu46235e62007-06-08 17:37:16 -07001408 goto exit_init_hba;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001409
1410 /*
1411 * FW is waiting to get an IP address from DHCP server: Skip building
1412 * the ddb_list and wait for DHCP lease acquired aen to come in
1413 * followed by 0x8014 aen" to trigger the tgt discovery process.
1414 */
Vikas Chaudhary2a49a782010-04-28 11:37:07 +05301415 if (ha->firmware_state & FW_STATE_CONFIGURING_IP)
David C Somayajulu46235e62007-06-08 17:37:16 -07001416 goto exit_init_online;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001417
1418 /* Skip device discovery if ip and subnet is zero */
1419 if (memcmp(ha->ip_address, ip_address, IP_ADDR_LEN) == 0 ||
1420 memcmp(ha->subnet_mask, ip_address, IP_ADDR_LEN) == 0)
David C Somayajulu46235e62007-06-08 17:37:16 -07001421 goto exit_init_online;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001422
1423 if (renew_ddb_list == PRESERVE_DDB_LIST) {
1424 /*
1425 * We want to preserve lun states (i.e. suspended, etc.)
1426 * for recovery initiated by the driver. So just update
1427 * the device states for the existing ddb_list.
1428 */
1429 qla4xxx_reinitialize_ddb_list(ha);
1430 } else if (renew_ddb_list == REBUILD_DDB_LIST) {
1431 /*
1432 * We want to build the ddb_list from scratch during
1433 * driver initialization and recovery initiated by the
1434 * INT_HBA_RESET IOCTL.
1435 */
1436 status = qla4xxx_initialize_ddb_list(ha);
1437 if (status == QLA_ERROR) {
1438 DEBUG2(printk("%s(%ld) Error occurred during build"
1439 "ddb list\n", __func__, ha->host_no));
1440 goto exit_init_hba;
1441 }
1442
1443 }
1444 if (!ha->tot_ddbs) {
1445 DEBUG2(printk("scsi%ld: Failed to initialize devices or none "
1446 "present in Firmware device database\n",
1447 ha->host_no));
1448 }
1449
David C Somayajulu46235e62007-06-08 17:37:16 -07001450exit_init_online:
David C Somayajulu92b72732007-05-23 17:55:40 -07001451 set_bit(AF_ONLINE, &ha->flags);
David C Somayajulu46235e62007-06-08 17:37:16 -07001452exit_init_hba:
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301453 DEBUG2(printk("scsi%ld: initialize adapter: %s\n", ha->host_no,
1454 status == QLA_ERROR ? "FAILED" : "SUCCEDED"));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001455 return status;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001456}
1457
1458/**
1459 * qla4xxx_add_device_dynamically - ddb addition due to an AEN
1460 * @ha: Pointer to host adapter structure.
1461 * @fw_ddb_index: Firmware's device database index
1462 *
1463 * This routine processes adds a device as a result of an 8014h AEN.
1464 **/
1465static void qla4xxx_add_device_dynamically(struct scsi_qla_host *ha,
1466 uint32_t fw_ddb_index)
1467{
1468 struct ddb_entry * ddb_entry;
David C Somayajulu92b72732007-05-23 17:55:40 -07001469 uint32_t new_tgt;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001470
1471 /* First allocate a device structure */
David C Somayajulu92b72732007-05-23 17:55:40 -07001472 ddb_entry = qla4xxx_get_ddb_entry(ha, fw_ddb_index, &new_tgt);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001473 if (ddb_entry == NULL) {
1474 DEBUG2(printk(KERN_WARNING
1475 "scsi%ld: Unable to allocate memory to add "
1476 "fw_ddb_index %d\n", ha->host_no, fw_ddb_index));
1477 return;
1478 }
1479
David C Somayajulu92b72732007-05-23 17:55:40 -07001480 if (!new_tgt && (ddb_entry->fw_ddb_index != fw_ddb_index)) {
1481 /* Target has been bound to a new fw_ddb_index */
1482 qla4xxx_free_ddb(ha, ddb_entry);
1483 ddb_entry = qla4xxx_alloc_ddb(ha, fw_ddb_index);
1484 if (ddb_entry == NULL) {
1485 DEBUG2(printk(KERN_WARNING
1486 "scsi%ld: Unable to allocate memory"
1487 " to add fw_ddb_index %d\n",
1488 ha->host_no, fw_ddb_index));
1489 return;
1490 }
1491 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001492 if (qla4xxx_update_ddb_entry(ha, ddb_entry, fw_ddb_index) ==
1493 QLA_ERROR) {
1494 ha->fw_ddb_index_map[fw_ddb_index] =
1495 (struct ddb_entry *)INVALID_ENTRY;
1496 DEBUG2(printk(KERN_WARNING
1497 "scsi%ld: failed to add new device at index "
1498 "[%d]\n Unable to retrieve fw ddb entry\n",
1499 ha->host_no, fw_ddb_index));
1500 qla4xxx_free_ddb(ha, ddb_entry);
1501 return;
1502 }
1503
1504 if (qla4xxx_add_sess(ddb_entry)) {
1505 DEBUG2(printk(KERN_WARNING
1506 "scsi%ld: failed to add new device at index "
1507 "[%d]\n Unable to add connection and session\n",
1508 ha->host_no, fw_ddb_index));
1509 qla4xxx_free_ddb(ha, ddb_entry);
1510 }
1511}
1512
1513/**
1514 * qla4xxx_process_ddb_changed - process ddb state change
1515 * @ha - Pointer to host adapter structure.
1516 * @fw_ddb_index - Firmware's device database index
1517 * @state - Device state
1518 *
1519 * This routine processes a Decive Database Changed AEN Event.
1520 **/
Vikas Chaudhary821d6e52010-04-28 11:41:21 +05301521int qla4xxx_process_ddb_changed(struct scsi_qla_host *ha, uint32_t fw_ddb_index,
1522 uint32_t state, uint32_t conn_err)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001523{
1524 struct ddb_entry * ddb_entry;
1525 uint32_t old_fw_ddb_device_state;
1526
1527 /* check for out of range index */
1528 if (fw_ddb_index >= MAX_DDB_ENTRIES)
1529 return QLA_ERROR;
1530
1531 /* Get the corresponging ddb entry */
1532 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, fw_ddb_index);
1533 /* Device does not currently exist in our database. */
1534 if (ddb_entry == NULL) {
1535 if (state == DDB_DS_SESSION_ACTIVE)
1536 qla4xxx_add_device_dynamically(ha, fw_ddb_index);
1537 return QLA_SUCCESS;
1538 }
1539
1540 /* Device already exists in our database. */
1541 old_fw_ddb_device_state = ddb_entry->fw_ddb_device_state;
1542 DEBUG2(printk("scsi%ld: %s DDB - old state= 0x%x, new state=0x%x for "
1543 "index [%d]\n", ha->host_no, __func__,
1544 ddb_entry->fw_ddb_device_state, state, fw_ddb_index));
1545 if (old_fw_ddb_device_state == state &&
1546 state == DDB_DS_SESSION_ACTIVE) {
Vikas Chaudharye349fa32010-07-10 14:48:36 +05301547 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
1548 atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
1549 iscsi_unblock_session(ddb_entry->sess);
1550 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001551 return QLA_SUCCESS;
1552 }
1553
1554 ddb_entry->fw_ddb_device_state = state;
1555 /* Device is back online. */
1556 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
Mike Christie50a29ae2008-03-04 13:26:53 -06001557 atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001558 atomic_set(&ddb_entry->relogin_retry_count, 0);
1559 atomic_set(&ddb_entry->relogin_timer, 0);
1560 clear_bit(DF_RELOGIN, &ddb_entry->flags);
1561 clear_bit(DF_NO_RELOGIN, &ddb_entry->flags);
Mike Christieb6359302008-01-31 13:36:44 -06001562 iscsi_unblock_session(ddb_entry->sess);
Mike Christie26974782007-12-13 12:43:29 -06001563 iscsi_session_event(ddb_entry->sess,
1564 ISCSI_KEVENT_CREATE_SESSION);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001565 /*
1566 * Change the lun state to READY in case the lun TIMEOUT before
1567 * the device came back.
1568 */
1569 } else {
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301570 /* Device went away, mark device missing */
1571 if (atomic_read(&ddb_entry->state) == DDB_STATE_ONLINE) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301572 DEBUG2(ql4_printk(KERN_INFO, ha, "%s mark missing "
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301573 "ddb_entry 0x%p sess 0x%p conn 0x%p\n",
1574 __func__, ddb_entry,
1575 ddb_entry->sess, ddb_entry->conn));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001576 qla4xxx_mark_device_missing(ha, ddb_entry);
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301577 }
1578
David Somayajuluafaf5a22006-09-19 10:28:00 -07001579 /*
1580 * Relogin if device state changed to a not active state.
Vikas Chaudhary821d6e52010-04-28 11:41:21 +05301581 * However, do not relogin if a RELOGIN is in process, or
1582 * we are not allowed to relogin to this DDB.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001583 */
1584 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_FAILED &&
1585 !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
1586 !test_bit(DF_NO_RELOGIN, &ddb_entry->flags) &&
Vikas Chaudhary821d6e52010-04-28 11:41:21 +05301587 qla4_is_relogin_allowed(ha, conn_err)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001588 /*
1589 * This triggers a relogin. After the relogin_timer
1590 * expires, the relogin gets scheduled. We must wait a
1591 * minimum amount of time since receiving an 0x8014 AEN
1592 * with failed device_state or a logout response before
1593 * we can issue another relogin.
1594 */
Vikas Chaudhary821d6e52010-04-28 11:41:21 +05301595 /* Firmware pads this timeout: (time2wait +1).
David Somayajuluafaf5a22006-09-19 10:28:00 -07001596 * Driver retry to login should be longer than F/W.
1597 * Otherwise F/W will fail
1598 * set_ddb() mbx cmd with 0x4005 since it still
1599 * counting down its time2wait.
1600 */
1601 atomic_set(&ddb_entry->relogin_timer, 0);
1602 atomic_set(&ddb_entry->retry_relogin_timer,
1603 ddb_entry->default_time2wait + 4);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301604 DEBUG(printk("scsi%ld: %s: ddb[%d] "
1605 "initiate relogin after %d seconds\n",
1606 ha->host_no, __func__,
1607 ddb_entry->fw_ddb_index,
1608 ddb_entry->default_time2wait + 4));
1609 } else {
1610 DEBUG(printk("scsi%ld: %s: ddb[%d] "
1611 "relogin not initiated, state = %d, "
1612 "ddb_entry->flags = 0x%lx\n",
1613 ha->host_no, __func__,
1614 ddb_entry->fw_ddb_index,
1615 ddb_entry->fw_ddb_device_state,
1616 ddb_entry->flags));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001617 }
1618 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001619 return QLA_SUCCESS;
1620}