blob: 30680124c5c1b6b34a2e33c0f18c7c2d3a9a29f1 [file] [log] [blame]
David Somayajuluafaf5a22006-09-19 10:28:00 -07001/*
2 * QLogic iSCSI HBA Driver
Vikas Chaudhary7d01d062010-12-02 22:12:51 -08003 * Copyright (c) 2003-2010 QLogic Corporation
David Somayajuluafaf5a22006-09-19 10:28:00 -07004 *
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 Chaudharyf4f5df232010-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 Chaudharyf4f5df232010-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 Chaudharyf4f5df232010-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 Chaudharyf4f5df232010-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 Chaudharyf4f5df232010-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 Chaudharyf4f5df232010-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 Chaudharyf4f5df232010-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 Chaudharyf4f5df232010-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 Chaudharyf4f5df232010-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 Chaudharyf4f5df232010-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 Chaudharyf4f5df232010-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 }
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500239 if (((ha->ip_config.ipv6_addl_options &
240 IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) != 0) &&
241 ((ha->ip_config.ipv6_link_local_state ==
242 IP_ADDRSTATE_ACQUIRING) ||
243 (ha->ip_config.ipv6_addr0_state ==
244 IP_ADDRSTATE_ACQUIRING) ||
245 (ha->ip_config.ipv6_addr1_state ==
246 IP_ADDRSTATE_ACQUIRING))) {
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530247
248 ipv6_wait = 1;
249
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500250 if ((ha->ip_config.ipv6_link_local_state ==
251 IP_ADDRSTATE_PREFERRED) ||
252 (ha->ip_config.ipv6_addr0_state ==
253 IP_ADDRSTATE_PREFERRED) ||
254 (ha->ip_config.ipv6_addr1_state ==
255 IP_ADDRSTATE_PREFERRED)) {
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530256 DEBUG2(printk(KERN_INFO "scsi%ld: %s: "
257 "Preferred IP configured."
258 " Don't wait!\n", ha->host_no,
259 __func__));
260 ipv6_wait = 0;
261 }
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500262 if (memcmp(&ha->ip_config.ipv6_default_router_addr,
263 ip_address, IPv6_ADDR_LEN) == 0) {
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530264 DEBUG2(printk(KERN_INFO "scsi%ld: %s: "
265 "No Router configured. "
266 "Don't wait!\n", ha->host_no,
267 __func__));
268 ipv6_wait = 0;
269 }
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500270 if ((ha->ip_config.ipv6_default_router_state ==
271 IPV6_RTRSTATE_MANUAL) &&
272 (ha->ip_config.ipv6_link_local_state ==
273 IP_ADDRSTATE_TENTATIVE) &&
274 (memcmp(&ha->ip_config.ipv6_link_local_addr,
275 &ha->ip_config.ipv6_default_router_addr, 4) ==
276 0)) {
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530277 DEBUG2(printk("scsi%ld: %s: LinkLocal Router & "
278 "IP configured. Don't wait!\n",
279 ha->host_no, __func__));
280 ipv6_wait = 0;
281 }
282 }
283 if (ipv4_wait || ipv6_wait) {
284 DEBUG2(printk("scsi%ld: %s: Wait for additional "
285 "IP(s) \"", ha->host_no, __func__));
286 if (ipv4_wait)
287 DEBUG2(printk("IPv4 "));
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500288 if (ha->ip_config.ipv6_link_local_state ==
289 IP_ADDRSTATE_ACQUIRING)
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530290 DEBUG2(printk("IPv6LinkLocal "));
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500291 if (ha->ip_config.ipv6_addr0_state ==
292 IP_ADDRSTATE_ACQUIRING)
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530293 DEBUG2(printk("IPv6Addr0 "));
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500294 if (ha->ip_config.ipv6_addr1_state ==
295 IP_ADDRSTATE_ACQUIRING)
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530296 DEBUG2(printk("IPv6Addr1 "));
297 DEBUG2(printk("\"\n"));
298 }
299 }
300
301 return ipv4_wait|ipv6_wait;
302}
303
David Somayajuluafaf5a22006-09-19 10:28:00 -0700304static int qla4xxx_fw_ready(struct scsi_qla_host *ha)
305{
306 uint32_t timeout_count;
307 int ready = 0;
308
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530309 DEBUG2(ql4_printk(KERN_INFO, ha, "Waiting for Firmware Ready..\n"));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700310 for (timeout_count = ADAPTER_INIT_TOV; timeout_count > 0;
311 timeout_count--) {
312 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
313 qla4xxx_get_dhcp_ip_address(ha);
314
315 /* Get firmware state. */
316 if (qla4xxx_get_firmware_state(ha) != QLA_SUCCESS) {
317 DEBUG2(printk("scsi%ld: %s: unable to get firmware "
318 "state\n", ha->host_no, __func__));
319 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700320 }
321
322 if (ha->firmware_state & FW_STATE_ERROR) {
323 DEBUG2(printk("scsi%ld: %s: an unrecoverable error has"
324 " occurred\n", ha->host_no, __func__));
325 break;
326
327 }
328 if (ha->firmware_state & FW_STATE_CONFIG_WAIT) {
329 /*
330 * The firmware has not yet been issued an Initialize
331 * Firmware command, so issue it now.
332 */
333 if (qla4xxx_initialize_fw_cb(ha) == QLA_ERROR)
334 break;
335
336 /* Go back and test for ready state - no wait. */
337 continue;
338 }
339
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530340 if (ha->firmware_state & FW_STATE_WAIT_AUTOCONNECT) {
341 DEBUG2(printk(KERN_INFO "scsi%ld: %s: fwstate:"
342 "AUTOCONNECT in progress\n",
343 ha->host_no, __func__));
344 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700345
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530346 if (ha->firmware_state & FW_STATE_CONFIGURING_IP) {
347 DEBUG2(printk(KERN_INFO "scsi%ld: %s: fwstate:"
348 " CONFIGURING IP\n",
349 ha->host_no, __func__));
350 /*
351 * Check for link state after 15 secs and if link is
352 * still DOWN then, cable is unplugged. Ignore "DHCP
353 * in Progress/CONFIGURING IP" bit to check if firmware
354 * is in ready state or not after 15 secs.
355 * This is applicable for both 2.x & 3.x firmware
356 */
357 if (timeout_count <= (ADAPTER_INIT_TOV - 15)) {
358 if (ha->addl_fw_state & FW_ADDSTATE_LINK_UP) {
359 DEBUG2(printk(KERN_INFO "scsi%ld: %s:"
360 " LINK UP (Cable plugged)\n",
361 ha->host_no, __func__));
362 } else if (ha->firmware_state &
363 (FW_STATE_CONFIGURING_IP |
364 FW_STATE_READY)) {
365 DEBUG2(printk(KERN_INFO "scsi%ld: %s: "
366 "LINK DOWN (Cable unplugged)\n",
367 ha->host_no, __func__));
368 ha->firmware_state = FW_STATE_READY;
369 }
370 }
371 }
372
373 if (ha->firmware_state == FW_STATE_READY) {
374 /* If DHCP IP Addr is available, retrieve it now. */
375 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR,
376 &ha->dpc_flags))
377 qla4xxx_get_dhcp_ip_address(ha);
378
379 if (!qla4xxx_wait_for_ip_config(ha) ||
380 timeout_count == 1) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530381 DEBUG2(ql4_printk(KERN_INFO, ha,
382 "Firmware Ready..\n"));
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530383 /* The firmware is ready to process SCSI
384 commands. */
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530385 DEBUG2(ql4_printk(KERN_INFO, ha,
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530386 "scsi%ld: %s: MEDIA TYPE"
387 " - %s\n", ha->host_no,
388 __func__, (ha->addl_fw_state &
389 FW_ADDSTATE_OPTICAL_MEDIA)
390 != 0 ? "OPTICAL" : "COPPER"));
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530391 DEBUG2(ql4_printk(KERN_INFO, ha,
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530392 "scsi%ld: %s: DHCPv4 STATE"
393 " Enabled %s\n", ha->host_no,
394 __func__, (ha->addl_fw_state &
395 FW_ADDSTATE_DHCPv4_ENABLED) != 0 ?
396 "YES" : "NO"));
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530397 DEBUG2(ql4_printk(KERN_INFO, ha,
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530398 "scsi%ld: %s: LINK %s\n",
399 ha->host_no, __func__,
400 (ha->addl_fw_state &
401 FW_ADDSTATE_LINK_UP) != 0 ?
402 "UP" : "DOWN"));
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530403 DEBUG2(ql4_printk(KERN_INFO, ha,
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530404 "scsi%ld: %s: iSNS Service "
405 "Started %s\n",
406 ha->host_no, __func__,
407 (ha->addl_fw_state &
408 FW_ADDSTATE_ISNS_SVC_ENABLED) != 0 ?
409 "YES" : "NO"));
410
411 ready = 1;
412 break;
413 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700414 }
415 DEBUG2(printk("scsi%ld: %s: waiting on fw, state=%x:%x - "
416 "seconds expired= %d\n", ha->host_no, __func__,
417 ha->firmware_state, ha->addl_fw_state,
418 timeout_count));
David C Somayajulud9150582006-11-15 17:38:40 -0800419 if (is_qla4032(ha) &&
420 !(ha->addl_fw_state & FW_ADDSTATE_LINK_UP) &&
421 (timeout_count < ADAPTER_INIT_TOV - 5)) {
422 break;
423 }
424
David Somayajuluafaf5a22006-09-19 10:28:00 -0700425 msleep(1000);
426 } /* end of for */
427
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530428 if (timeout_count <= 0)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700429 DEBUG2(printk("scsi%ld: %s: FW Initialization timed out!\n",
430 ha->host_no, __func__));
431
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530432 if (ha->firmware_state & FW_STATE_CONFIGURING_IP) {
433 DEBUG2(printk("scsi%ld: %s: FW initialized, but is reporting "
434 "it's waiting to configure an IP address\n",
435 ha->host_no, __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700436 ready = 1;
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530437 } else if (ha->firmware_state & FW_STATE_WAIT_AUTOCONNECT) {
438 DEBUG2(printk("scsi%ld: %s: FW initialized, but "
439 "auto-discovery still in process\n",
440 ha->host_no, __func__));
Vikas Chaudharyb9663462010-07-10 14:49:19 +0530441 ready = 1;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700442 }
443
444 return ready;
445}
446
447/**
448 * qla4xxx_init_firmware - initializes the firmware.
449 * @ha: pointer to host adapter structure.
450 *
451 **/
452static int qla4xxx_init_firmware(struct scsi_qla_host *ha)
453{
454 int status = QLA_ERROR;
455
Lalit Chandivade2232be02010-07-30 14:38:47 +0530456 if (is_aer_supported(ha) &&
457 test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
458 return status;
459
Lalit Chandivade9d4946f2010-07-30 14:26:31 +0530460 /* For 82xx, stop firmware before initializing because if BIOS
461 * has previously initialized firmware, then driver's initialize
462 * firmware will fail. */
463 if (is_qla8022(ha))
464 qla4_8xxx_stop_firmware(ha);
465
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530466 ql4_printk(KERN_INFO, ha, "Initializing firmware..\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700467 if (qla4xxx_initialize_fw_cb(ha) == QLA_ERROR) {
468 DEBUG2(printk("scsi%ld: %s: Failed to initialize firmware "
469 "control block\n", ha->host_no, __func__));
470 return status;
471 }
472 if (!qla4xxx_fw_ready(ha))
473 return status;
474
David Somayajuluafaf5a22006-09-19 10:28:00 -0700475 return qla4xxx_get_firmware_status(ha);
476}
477
478static struct ddb_entry* qla4xxx_get_ddb_entry(struct scsi_qla_host *ha,
David C Somayajulu92b72732007-05-23 17:55:40 -0700479 uint32_t fw_ddb_index,
480 uint32_t *new_tgt)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700481{
482 struct dev_db_entry *fw_ddb_entry = NULL;
483 dma_addr_t fw_ddb_entry_dma;
484 struct ddb_entry *ddb_entry = NULL;
485 int found = 0;
486 uint32_t device_state;
487
David C Somayajulu92b72732007-05-23 17:55:40 -0700488 *new_tgt = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700489 /* Make sure the dma buffer is valid */
490 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev,
491 sizeof(*fw_ddb_entry),
492 &fw_ddb_entry_dma, GFP_KERNEL);
493 if (fw_ddb_entry == NULL) {
494 DEBUG2(printk("scsi%ld: %s: Unable to allocate dma buffer.\n",
495 ha->host_no, __func__));
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530496 goto exit_get_ddb_entry_no_free;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700497 }
498
499 if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index, fw_ddb_entry,
500 fw_ddb_entry_dma, NULL, NULL,
501 &device_state, NULL, NULL, NULL) ==
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530502 QLA_ERROR) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700503 DEBUG2(printk("scsi%ld: %s: failed get_ddb_entry for "
504 "fw_ddb_index %d\n", ha->host_no, __func__,
505 fw_ddb_index));
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530506 goto exit_get_ddb_entry;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700507 }
508
509 /* Allocate DDB if not already allocated. */
510 DEBUG2(printk("scsi%ld: %s: Looking for ddb[%d]\n", ha->host_no,
511 __func__, fw_ddb_index));
512 list_for_each_entry(ddb_entry, &ha->ddb_list, list) {
Mike Christie41bbdbe2009-01-16 12:36:52 -0600513 if ((memcmp(ddb_entry->iscsi_name, fw_ddb_entry->iscsi_name,
514 ISCSI_NAME_SIZE) == 0) &&
515 (ddb_entry->tpgt ==
516 le32_to_cpu(fw_ddb_entry->tgt_portal_grp)) &&
517 (memcmp(ddb_entry->isid, fw_ddb_entry->isid,
518 sizeof(ddb_entry->isid)) == 0)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700519 found++;
520 break;
521 }
522 }
523
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530524 /* if not found allocate new ddb */
David Somayajuluafaf5a22006-09-19 10:28:00 -0700525 if (!found) {
526 DEBUG2(printk("scsi%ld: %s: ddb[%d] not found - allocating "
527 "new ddb\n", ha->host_no, __func__,
528 fw_ddb_index));
David C Somayajulu92b72732007-05-23 17:55:40 -0700529 *new_tgt = 1;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700530 ddb_entry = qla4xxx_alloc_ddb(ha, fw_ddb_index);
531 }
532
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530533exit_get_ddb_entry:
David Somayajuluafaf5a22006-09-19 10:28:00 -0700534 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry), fw_ddb_entry,
535 fw_ddb_entry_dma);
536
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530537exit_get_ddb_entry_no_free:
David Somayajuluafaf5a22006-09-19 10:28:00 -0700538 return ddb_entry;
539}
540
541/**
542 * qla4xxx_update_ddb_entry - update driver's internal ddb
543 * @ha: pointer to host adapter structure.
544 * @ddb_entry: pointer to device database structure to be filled
545 * @fw_ddb_index: index of the ddb entry in fw ddb table
546 *
547 * This routine updates the driver's internal device database entry
548 * with information retrieved from the firmware's device database
549 * entry for the specified device. The ddb_entry->fw_ddb_index field
550 * must be initialized prior to calling this routine
551 *
552 **/
Adrian Bunk47975472007-04-26 00:35:16 -0700553static int qla4xxx_update_ddb_entry(struct scsi_qla_host *ha,
554 struct ddb_entry *ddb_entry,
555 uint32_t fw_ddb_index)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700556{
557 struct dev_db_entry *fw_ddb_entry = NULL;
558 dma_addr_t fw_ddb_entry_dma;
559 int status = QLA_ERROR;
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530560 uint32_t conn_err;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700561
562 if (ddb_entry == NULL) {
563 DEBUG2(printk("scsi%ld: %s: ddb_entry is NULL\n", ha->host_no,
564 __func__));
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530565
566 goto exit_update_ddb_no_free;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700567 }
568
569 /* Make sure the dma buffer is valid */
570 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev,
571 sizeof(*fw_ddb_entry),
572 &fw_ddb_entry_dma, GFP_KERNEL);
573 if (fw_ddb_entry == NULL) {
574 DEBUG2(printk("scsi%ld: %s: Unable to allocate dma buffer.\n",
575 ha->host_no, __func__));
576
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530577 goto exit_update_ddb_no_free;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700578 }
579
580 if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index, fw_ddb_entry,
581 fw_ddb_entry_dma, NULL, NULL,
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530582 &ddb_entry->fw_ddb_device_state, &conn_err,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700583 &ddb_entry->tcp_source_port_num,
584 &ddb_entry->connection_id) ==
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530585 QLA_ERROR) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700586 DEBUG2(printk("scsi%ld: %s: failed get_ddb_entry for "
587 "fw_ddb_index %d\n", ha->host_no, __func__,
588 fw_ddb_index));
589
590 goto exit_update_ddb;
591 }
592
593 status = QLA_SUCCESS;
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530594 ddb_entry->options = le16_to_cpu(fw_ddb_entry->options);
David C Somayajulu92b72732007-05-23 17:55:40 -0700595 ddb_entry->target_session_id = le16_to_cpu(fw_ddb_entry->tsid);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700596 ddb_entry->task_mgmt_timeout =
David C Somayajulu92b72732007-05-23 17:55:40 -0700597 le16_to_cpu(fw_ddb_entry->def_timeout);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700598 ddb_entry->CmdSn = 0;
David C Somayajulu92b72732007-05-23 17:55:40 -0700599 ddb_entry->exe_throttle = le16_to_cpu(fw_ddb_entry->exec_throttle);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700600 ddb_entry->default_relogin_timeout =
David C Somayajulu92b72732007-05-23 17:55:40 -0700601 le16_to_cpu(fw_ddb_entry->def_timeout);
602 ddb_entry->default_time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700603
604 /* Update index in case it changed */
605 ddb_entry->fw_ddb_index = fw_ddb_index;
606 ha->fw_ddb_index_map[fw_ddb_index] = ddb_entry;
607
David C Somayajulu92b72732007-05-23 17:55:40 -0700608 ddb_entry->port = le16_to_cpu(fw_ddb_entry->port);
609 ddb_entry->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
Mike Christie41bbdbe2009-01-16 12:36:52 -0600610 memcpy(ddb_entry->isid, fw_ddb_entry->isid, sizeof(ddb_entry->isid));
611
David C Somayajulu92b72732007-05-23 17:55:40 -0700612 memcpy(&ddb_entry->iscsi_name[0], &fw_ddb_entry->iscsi_name[0],
David Somayajuluafaf5a22006-09-19 10:28:00 -0700613 min(sizeof(ddb_entry->iscsi_name),
David C Somayajulu92b72732007-05-23 17:55:40 -0700614 sizeof(fw_ddb_entry->iscsi_name)));
Vikas Chaudhary3b2bef12010-07-10 14:51:30 +0530615 memcpy(&ddb_entry->iscsi_alias[0], &fw_ddb_entry->iscsi_alias[0],
616 min(sizeof(ddb_entry->iscsi_alias),
617 sizeof(fw_ddb_entry->iscsi_alias)));
David C Somayajulu92b72732007-05-23 17:55:40 -0700618 memcpy(&ddb_entry->ip_addr[0], &fw_ddb_entry->ip_addr[0],
619 min(sizeof(ddb_entry->ip_addr), sizeof(fw_ddb_entry->ip_addr)));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700620
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530621 ddb_entry->iscsi_max_burst_len = fw_ddb_entry->iscsi_max_burst_len;
622 ddb_entry->iscsi_max_outsnd_r2t = fw_ddb_entry->iscsi_max_outsnd_r2t;
623 ddb_entry->iscsi_first_burst_len = fw_ddb_entry->iscsi_first_burst_len;
624 ddb_entry->iscsi_max_rcv_data_seg_len =
625 fw_ddb_entry->iscsi_max_rcv_data_seg_len;
626 ddb_entry->iscsi_max_snd_data_seg_len =
627 fw_ddb_entry->iscsi_max_snd_data_seg_len;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700628
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530629 if (ddb_entry->options & DDB_OPT_IPV6_DEVICE) {
630 memcpy(&ddb_entry->remote_ipv6_addr,
631 fw_ddb_entry->ip_addr,
632 min(sizeof(ddb_entry->remote_ipv6_addr),
633 sizeof(fw_ddb_entry->ip_addr)));
634 memcpy(&ddb_entry->link_local_ipv6_addr,
635 fw_ddb_entry->link_local_ipv6_addr,
636 min(sizeof(ddb_entry->link_local_ipv6_addr),
637 sizeof(fw_ddb_entry->link_local_ipv6_addr)));
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530638
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530639 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: DDB[%d] State %04x"
640 " ConnErr %08x IP %pI6 "
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530641 ":%04d \"%s\"\n",
642 __func__, fw_ddb_index,
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530643 ddb_entry->fw_ddb_device_state,
644 conn_err, fw_ddb_entry->ip_addr,
645 le16_to_cpu(fw_ddb_entry->port),
646 fw_ddb_entry->iscsi_name));
647 } else
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530648 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: DDB[%d] State %04x"
649 " ConnErr %08x IP %pI4 "
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530650 ":%04d \"%s\"\n",
651 __func__, fw_ddb_index,
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530652 ddb_entry->fw_ddb_device_state,
653 conn_err, fw_ddb_entry->ip_addr,
654 le16_to_cpu(fw_ddb_entry->port),
655 fw_ddb_entry->iscsi_name));
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530656exit_update_ddb:
David Somayajuluafaf5a22006-09-19 10:28:00 -0700657 if (fw_ddb_entry)
658 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
659 fw_ddb_entry, fw_ddb_entry_dma);
660
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530661exit_update_ddb_no_free:
David Somayajuluafaf5a22006-09-19 10:28:00 -0700662 return status;
663}
664
665/**
666 * qla4xxx_alloc_ddb - allocate device database entry
667 * @ha: Pointer to host adapter structure.
668 * @fw_ddb_index: Firmware's device database index
669 *
670 * This routine allocates a ddb_entry, ititializes some values, and
671 * inserts it into the ddb list.
672 **/
Adrian Bunk47975472007-04-26 00:35:16 -0700673static struct ddb_entry * qla4xxx_alloc_ddb(struct scsi_qla_host *ha,
674 uint32_t fw_ddb_index)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700675{
676 struct ddb_entry *ddb_entry;
677
678 DEBUG2(printk("scsi%ld: %s: fw_ddb_index [%d]\n", ha->host_no,
679 __func__, fw_ddb_index));
680
681 ddb_entry = qla4xxx_alloc_sess(ha);
682 if (ddb_entry == NULL) {
683 DEBUG2(printk("scsi%ld: %s: Unable to allocate memory "
684 "to add fw_ddb_index [%d]\n",
685 ha->host_no, __func__, fw_ddb_index));
686 return ddb_entry;
687 }
688
689 ddb_entry->fw_ddb_index = fw_ddb_index;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700690 atomic_set(&ddb_entry->retry_relogin_timer, INVALID_ENTRY);
691 atomic_set(&ddb_entry->relogin_timer, 0);
692 atomic_set(&ddb_entry->relogin_retry_count, 0);
693 atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
694 list_add_tail(&ddb_entry->list, &ha->ddb_list);
695 ha->fw_ddb_index_map[fw_ddb_index] = ddb_entry;
696 ha->tot_ddbs++;
697
698 return ddb_entry;
699}
700
701/**
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530702 * qla4_is_relogin_allowed - Are we allowed to login?
703 * @ha: Pointer to host adapter structure.
704 * @conn_err: Last connection error associated with the ddb
705 *
706 * This routine tests the given connection error to determine if
707 * we are allowed to login.
708 **/
709int qla4_is_relogin_allowed(struct scsi_qla_host *ha, uint32_t conn_err)
710{
711 uint32_t err_code, login_rsp_sts_class;
712 int relogin = 1;
713
714 err_code = ((conn_err & 0x00ff0000) >> 16);
715 login_rsp_sts_class = ((conn_err & 0x0000ff00) >> 8);
716 if (err_code == 0x1c || err_code == 0x06) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530717 DEBUG2(ql4_printk(KERN_INFO, ha,
718 ": conn_err=0x%08x, send target completed"
719 " or access denied failure\n", conn_err));
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530720 relogin = 0;
721 }
722 if ((err_code == 0x08) && (login_rsp_sts_class == 0x02)) {
723 /* Login Response PDU returned an error.
724 Login Response Status in Error Code Detail
725 indicates login should not be retried.*/
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530726 DEBUG2(ql4_printk(KERN_INFO, ha,
727 ": conn_err=0x%08x, do not retry relogin\n",
728 conn_err));
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530729 relogin = 0;
730 }
731
732 return relogin;
733}
734
Karen Higgins7edd9a72011-03-21 03:34:27 -0700735static void qla4xxx_flush_AENS(struct scsi_qla_host *ha)
736{
737 unsigned long wtime;
738
739 /* Flush the 0x8014 AEN from the firmware as a result of
740 * Auto connect. We are basically doing get_firmware_ddb()
741 * to determine whether we need to log back in or not.
742 * Trying to do a set ddb before we have processed 0x8014
743 * will result in another set_ddb() for the same ddb. In other
744 * words there will be stale entries in the aen_q.
745 */
746 wtime = jiffies + (2 * HZ);
747 do {
748 if (qla4xxx_get_firmware_state(ha) == QLA_SUCCESS)
749 if (ha->firmware_state & (BIT_2 | BIT_0))
750 return;
751
752 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
753 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
754
755 msleep(1000);
756 } while (!time_after_eq(jiffies, wtime));
757}
758
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530759/**
Karen Higgins7edd9a72011-03-21 03:34:27 -0700760 * qla4xxx_build_ddb_list - builds driver ddb list
David Somayajuluafaf5a22006-09-19 10:28:00 -0700761 * @ha: Pointer to host adapter structure.
762 *
763 * This routine searches for all valid firmware ddb entries and builds
764 * an internal ddb list. Ddbs that are considered valid are those with
765 * a device state of SESSION_ACTIVE.
Karen Higgins7edd9a72011-03-21 03:34:27 -0700766 * A relogin (set_ddb) is issued for DDBs that are not online.
David Somayajuluafaf5a22006-09-19 10:28:00 -0700767 **/
768static int qla4xxx_build_ddb_list(struct scsi_qla_host *ha)
769{
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530770 int status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700771 uint32_t fw_ddb_index = 0;
772 uint32_t next_fw_ddb_index = 0;
773 uint32_t ddb_state;
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530774 uint32_t conn_err;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700775 struct ddb_entry *ddb_entry;
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530776 struct dev_db_entry *fw_ddb_entry = NULL;
777 dma_addr_t fw_ddb_entry_dma;
778 uint32_t ipv6_device;
David C Somayajulu92b72732007-05-23 17:55:40 -0700779 uint32_t new_tgt;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700780
Karen Higgins7edd9a72011-03-21 03:34:27 -0700781 qla4xxx_flush_AENS(ha);
782
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530783 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
784 &fw_ddb_entry_dma, GFP_KERNEL);
785 if (fw_ddb_entry == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530786 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: DMA alloc failed\n",
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530787 __func__));
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530788
789 goto exit_build_ddb_list_no_free;
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530790 }
791
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530792 ql4_printk(KERN_INFO, ha, "Initializing DDBs ...\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700793 for (fw_ddb_index = 0; fw_ddb_index < MAX_DDB_ENTRIES;
794 fw_ddb_index = next_fw_ddb_index) {
795 /* First, let's see if a device exists here */
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530796 if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index, fw_ddb_entry,
797 0, NULL, &next_fw_ddb_index,
798 &ddb_state, &conn_err,
799 NULL, NULL) ==
800 QLA_ERROR) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700801 DEBUG2(printk("scsi%ld: %s: get_ddb_entry, "
802 "fw_ddb_index %d failed", ha->host_no,
803 __func__, fw_ddb_index));
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530804 goto exit_build_ddb_list;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700805 }
806
807 DEBUG2(printk("scsi%ld: %s: Getting DDB[%d] ddbstate=0x%x, "
808 "next_fw_ddb_index=%d.\n", ha->host_no, __func__,
809 fw_ddb_index, ddb_state, next_fw_ddb_index));
810
811 /* Issue relogin, if necessary. */
812 if (ddb_state == DDB_DS_SESSION_FAILED ||
813 ddb_state == DDB_DS_NO_CONNECTION_ACTIVE) {
814 /* Try and login to device */
815 DEBUG2(printk("scsi%ld: %s: Login to DDB[%d]\n",
816 ha->host_no, __func__, fw_ddb_index));
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530817 ipv6_device = le16_to_cpu(fw_ddb_entry->options) &
818 DDB_OPT_IPV6_DEVICE;
819 if (qla4_is_relogin_allowed(ha, conn_err) &&
820 ((!ipv6_device &&
821 *((uint32_t *)fw_ddb_entry->ip_addr))
822 || ipv6_device)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700823 qla4xxx_set_ddb_entry(ha, fw_ddb_index, 0);
David C Somayajulu92b72732007-05-23 17:55:40 -0700824 if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index,
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530825 NULL, 0, NULL,
826 &next_fw_ddb_index,
827 &ddb_state, &conn_err,
828 NULL, NULL)
829 == QLA_ERROR) {
David C Somayajulu92b72732007-05-23 17:55:40 -0700830 DEBUG2(printk("scsi%ld: %s:"
831 "get_ddb_entry %d failed\n",
832 ha->host_no,
833 __func__, fw_ddb_index));
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530834 goto exit_build_ddb_list;
David C Somayajulu92b72732007-05-23 17:55:40 -0700835 }
836 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700837 }
838
839 if (ddb_state != DDB_DS_SESSION_ACTIVE)
840 goto next_one;
841 /*
842 * if fw_ddb with session active state found,
843 * add to ddb_list
844 */
845 DEBUG2(printk("scsi%ld: %s: DDB[%d] added to list\n",
846 ha->host_no, __func__, fw_ddb_index));
847
848 /* Add DDB to internal our ddb list. */
David C Somayajulu92b72732007-05-23 17:55:40 -0700849 ddb_entry = qla4xxx_get_ddb_entry(ha, fw_ddb_index, &new_tgt);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700850 if (ddb_entry == NULL) {
851 DEBUG2(printk("scsi%ld: %s: Unable to allocate memory "
852 "for device at fw_ddb_index %d\n",
853 ha->host_no, __func__, fw_ddb_index));
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530854 goto exit_build_ddb_list;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700855 }
856 /* Fill in the device structure */
857 if (qla4xxx_update_ddb_entry(ha, ddb_entry, fw_ddb_index) ==
858 QLA_ERROR) {
859 ha->fw_ddb_index_map[fw_ddb_index] =
860 (struct ddb_entry *)INVALID_ENTRY;
861
David Somayajuluafaf5a22006-09-19 10:28:00 -0700862 DEBUG2(printk("scsi%ld: %s: update_ddb_entry failed "
863 "for fw_ddb_index %d.\n",
864 ha->host_no, __func__, fw_ddb_index));
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530865 goto exit_build_ddb_list;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700866 }
867
868next_one:
869 /* We know we've reached the last device when
870 * next_fw_ddb_index is 0 */
871 if (next_fw_ddb_index == 0)
872 break;
873 }
874
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530875 status = QLA_SUCCESS;
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530876 ql4_printk(KERN_INFO, ha, "DDB list done..\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700877
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530878exit_build_ddb_list:
879 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry), fw_ddb_entry,
880 fw_ddb_entry_dma);
881
882exit_build_ddb_list_no_free:
David Somayajuluafaf5a22006-09-19 10:28:00 -0700883 return status;
884}
885
David Somayajuluafaf5a22006-09-19 10:28:00 -0700886static int qla4xxx_initialize_ddb_list(struct scsi_qla_host *ha)
887{
888 uint16_t fw_ddb_index;
889 int status = QLA_SUCCESS;
890
891 /* free the ddb list if is not empty */
892 if (!list_empty(&ha->ddb_list))
893 qla4xxx_free_ddb_list(ha);
894
895 for (fw_ddb_index = 0; fw_ddb_index < MAX_DDB_ENTRIES; fw_ddb_index++)
896 ha->fw_ddb_index_map[fw_ddb_index] =
Karen Higgins7edd9a72011-03-21 03:34:27 -0700897 (struct ddb_entry *)INVALID_ENTRY;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700898
899 ha->tot_ddbs = 0;
900
Karen Higgins7edd9a72011-03-21 03:34:27 -0700901 /* Perform device discovery and build ddb list. */
902 status = qla4xxx_build_ddb_list(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700903
David Somayajuluafaf5a22006-09-19 10:28:00 -0700904 return status;
905}
906
907/**
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530908 * qla4xxx_reinitialize_ddb_list - update the driver ddb list
David Somayajuluafaf5a22006-09-19 10:28:00 -0700909 * @ha: pointer to host adapter structure.
910 *
911 * This routine obtains device information from the F/W database after
912 * firmware or adapter resets. The device table is preserved.
913 **/
914int qla4xxx_reinitialize_ddb_list(struct scsi_qla_host *ha)
915{
916 int status = QLA_SUCCESS;
917 struct ddb_entry *ddb_entry, *detemp;
918
919 /* Update the device information for all devices. */
920 list_for_each_entry_safe(ddb_entry, detemp, &ha->ddb_list, list) {
921 qla4xxx_update_ddb_entry(ha, ddb_entry,
922 ddb_entry->fw_ddb_index);
923 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
924 atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
925 DEBUG2(printk ("scsi%ld: %s: ddb index [%d] marked "
926 "ONLINE\n", ha->host_no, __func__,
927 ddb_entry->fw_ddb_index));
Vikas Chaudhary36386322010-07-10 14:49:01 +0530928 iscsi_unblock_session(ddb_entry->sess);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700929 } else if (atomic_read(&ddb_entry->state) == DDB_STATE_ONLINE)
930 qla4xxx_mark_device_missing(ha, ddb_entry);
931 }
932 return status;
933}
934
935/**
936 * qla4xxx_relogin_device - re-establish session
937 * @ha: Pointer to host adapter structure.
938 * @ddb_entry: Pointer to device database entry
939 *
940 * This routine does a session relogin with the specified device.
941 * The ddb entry must be assigned prior to making this call.
942 **/
943int qla4xxx_relogin_device(struct scsi_qla_host *ha,
944 struct ddb_entry * ddb_entry)
945{
946 uint16_t relogin_timer;
947
948 relogin_timer = max(ddb_entry->default_relogin_timeout,
949 (uint16_t)RELOGIN_TOV);
950 atomic_set(&ddb_entry->relogin_timer, relogin_timer);
951
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530952 DEBUG2(printk("scsi%ld: Relogin ddb [%d]. TOV=%d\n", ha->host_no,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700953 ddb_entry->fw_ddb_index, relogin_timer));
954
955 qla4xxx_set_ddb_entry(ha, ddb_entry->fw_ddb_index, 0);
956
957 return QLA_SUCCESS;
958}
959
David Somayajuluafaf5a22006-09-19 10:28:00 -0700960static int qla4xxx_config_nvram(struct scsi_qla_host *ha)
961{
962 unsigned long flags;
963 union external_hw_config_reg extHwConfig;
964
965 DEBUG2(printk("scsi%ld: %s: Get EEProm parameters \n", ha->host_no,
966 __func__));
967 if (ql4xxx_lock_flash(ha) != QLA_SUCCESS)
Mike Christieb3925512010-02-10 16:51:48 -0600968 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700969 if (ql4xxx_lock_nvram(ha) != QLA_SUCCESS) {
970 ql4xxx_unlock_flash(ha);
Mike Christieb3925512010-02-10 16:51:48 -0600971 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700972 }
973
974 /* Get EEPRom Parameters from NVRAM and validate */
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530975 ql4_printk(KERN_INFO, ha, "Configuring NVRAM ...\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700976 if (qla4xxx_is_nvram_configuration_valid(ha) == QLA_SUCCESS) {
977 spin_lock_irqsave(&ha->hardware_lock, flags);
978 extHwConfig.Asuint32_t =
979 rd_nvram_word(ha, eeprom_ext_hw_conf_offset(ha));
980 spin_unlock_irqrestore(&ha->hardware_lock, flags);
981 } else {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530982 ql4_printk(KERN_WARNING, ha,
983 "scsi%ld: %s: EEProm checksum invalid. "
984 "Please update your EEPROM\n", ha->host_no,
985 __func__);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700986
Mike Christieb3925512010-02-10 16:51:48 -0600987 /* Attempt to set defaults */
David Somayajuluafaf5a22006-09-19 10:28:00 -0700988 if (is_qla4010(ha))
989 extHwConfig.Asuint32_t = 0x1912;
David C Somayajulud9150582006-11-15 17:38:40 -0800990 else if (is_qla4022(ha) | is_qla4032(ha))
David Somayajuluafaf5a22006-09-19 10:28:00 -0700991 extHwConfig.Asuint32_t = 0x0023;
Mike Christieb3925512010-02-10 16:51:48 -0600992 else
993 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700994 }
995 DEBUG(printk("scsi%ld: %s: Setting extHwConfig to 0xFFFF%04x\n",
996 ha->host_no, __func__, extHwConfig.Asuint32_t));
997
998 spin_lock_irqsave(&ha->hardware_lock, flags);
999 writel((0xFFFF << 16) | extHwConfig.Asuint32_t, isp_ext_hw_conf(ha));
1000 readl(isp_ext_hw_conf(ha));
1001 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1002
1003 ql4xxx_unlock_nvram(ha);
1004 ql4xxx_unlock_flash(ha);
1005
Mike Christieb3925512010-02-10 16:51:48 -06001006 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001007}
1008
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301009/**
1010 * qla4_8xxx_pci_config() - Setup ISP82xx PCI configuration registers.
1011 * @ha: HA context
1012 */
1013void qla4_8xxx_pci_config(struct scsi_qla_host *ha)
1014{
1015 pci_set_master(ha->pdev);
1016}
1017
1018void qla4xxx_pci_config(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001019{
David C Somayajulu92b72732007-05-23 17:55:40 -07001020 uint16_t w;
David C Somayajulu46235e62007-06-08 17:37:16 -07001021 int status;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001022
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301023 ql4_printk(KERN_INFO, ha, "Configuring PCI space...\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001024
1025 pci_set_master(ha->pdev);
David C Somayajulu46235e62007-06-08 17:37:16 -07001026 status = pci_set_mwi(ha->pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001027 /*
1028 * We want to respect framework's setting of PCI configuration space
1029 * command register and also want to make sure that all bits of
1030 * interest to us are properly set in command register.
1031 */
1032 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
David C Somayajulu92b72732007-05-23 17:55:40 -07001033 w |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001034 w &= ~PCI_COMMAND_INTX_DISABLE;
1035 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
1036}
1037
1038static int qla4xxx_start_firmware_from_flash(struct scsi_qla_host *ha)
1039{
1040 int status = QLA_ERROR;
Vikas Chaudharyc301b022010-04-28 11:40:37 +05301041 unsigned long max_wait_time;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001042 unsigned long flags;
1043 uint32_t mbox_status;
1044
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301045 ql4_printk(KERN_INFO, ha, "Starting firmware ...\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001046
1047 /*
1048 * Start firmware from flash ROM
1049 *
1050 * WORKAROUND: Stuff a non-constant value that the firmware can
1051 * use as a seed for a random number generator in MB7 prior to
1052 * setting BOOT_ENABLE. Fixes problem where the TCP
1053 * connections use the same TCP ports after each reboot,
1054 * causing some connections to not get re-established.
1055 */
1056 DEBUG(printk("scsi%d: %s: Start firmware from flash ROM\n",
1057 ha->host_no, __func__));
1058
1059 spin_lock_irqsave(&ha->hardware_lock, flags);
1060 writel(jiffies, &ha->reg->mailbox[7]);
David C Somayajulud9150582006-11-15 17:38:40 -08001061 if (is_qla4022(ha) | is_qla4032(ha))
David Somayajuluafaf5a22006-09-19 10:28:00 -07001062 writel(set_rmask(NVR_WRITE_ENABLE),
1063 &ha->reg->u1.isp4022.nvram);
1064
David C Somayajulu92b72732007-05-23 17:55:40 -07001065 writel(2, &ha->reg->mailbox[6]);
1066 readl(&ha->reg->mailbox[6]);
1067
David Somayajuluafaf5a22006-09-19 10:28:00 -07001068 writel(set_rmask(CSR_BOOT_ENABLE), &ha->reg->ctrl_status);
1069 readl(&ha->reg->ctrl_status);
1070 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1071
1072 /* Wait for firmware to come UP. */
Vikas Chaudharyc301b022010-04-28 11:40:37 +05301073 DEBUG2(printk(KERN_INFO "scsi%ld: %s: Wait up to %d seconds for "
1074 "boot firmware to complete...\n",
1075 ha->host_no, __func__, FIRMWARE_UP_TOV));
1076 max_wait_time = jiffies + (FIRMWARE_UP_TOV * HZ);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001077 do {
1078 uint32_t ctrl_status;
1079
1080 spin_lock_irqsave(&ha->hardware_lock, flags);
1081 ctrl_status = readw(&ha->reg->ctrl_status);
1082 mbox_status = readw(&ha->reg->mailbox[0]);
1083 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1084
1085 if (ctrl_status & set_rmask(CSR_SCSI_PROCESSOR_INTR))
1086 break;
1087 if (mbox_status == MBOX_STS_COMMAND_COMPLETE)
1088 break;
1089
Vikas Chaudharyc301b022010-04-28 11:40:37 +05301090 DEBUG2(printk(KERN_INFO "scsi%ld: %s: Waiting for boot "
Vikas Chaudharyf581a3f2010-10-06 22:47:48 -07001091 "firmware to complete... ctrl_sts=0x%x, remaining=%ld\n",
1092 ha->host_no, __func__, ctrl_status, max_wait_time));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001093
Vikas Chaudharyc301b022010-04-28 11:40:37 +05301094 msleep_interruptible(250);
1095 } while (!time_after_eq(jiffies, max_wait_time));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001096
1097 if (mbox_status == MBOX_STS_COMMAND_COMPLETE) {
Vikas Chaudharyc301b022010-04-28 11:40:37 +05301098 DEBUG(printk(KERN_INFO "scsi%ld: %s: Firmware has started\n",
David Somayajuluafaf5a22006-09-19 10:28:00 -07001099 ha->host_no, __func__));
1100
1101 spin_lock_irqsave(&ha->hardware_lock, flags);
1102 writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
1103 &ha->reg->ctrl_status);
1104 readl(&ha->reg->ctrl_status);
1105 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1106
1107 status = QLA_SUCCESS;
1108 } else {
1109 printk(KERN_INFO "scsi%ld: %s: Boot firmware failed "
1110 "- mbox status 0x%x\n", ha->host_no, __func__,
1111 mbox_status);
1112 status = QLA_ERROR;
1113 }
1114 return status;
1115}
1116
David C Somayajulu92b72732007-05-23 17:55:40 -07001117int ql4xxx_lock_drvr_wait(struct scsi_qla_host *a)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001118{
David C Somayajulu92b72732007-05-23 17:55:40 -07001119#define QL4_LOCK_DRVR_WAIT 60
David C Somayajulu477ffb92007-01-22 12:26:11 -08001120#define QL4_LOCK_DRVR_SLEEP 1
David Somayajuluafaf5a22006-09-19 10:28:00 -07001121
1122 int drvr_wait = QL4_LOCK_DRVR_WAIT;
1123 while (drvr_wait) {
David C Somayajulu92b72732007-05-23 17:55:40 -07001124 if (ql4xxx_lock_drvr(a) == 0) {
David C Somayajulu477ffb92007-01-22 12:26:11 -08001125 ssleep(QL4_LOCK_DRVR_SLEEP);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001126 if (drvr_wait) {
1127 DEBUG2(printk("scsi%ld: %s: Waiting for "
David C Somayajulu92b72732007-05-23 17:55:40 -07001128 "Global Init Semaphore(%d)...\n",
1129 a->host_no,
David C Somayajulu477ffb92007-01-22 12:26:11 -08001130 __func__, drvr_wait));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001131 }
1132 drvr_wait -= QL4_LOCK_DRVR_SLEEP;
1133 } else {
1134 DEBUG2(printk("scsi%ld: %s: Global Init Semaphore "
David C Somayajulu92b72732007-05-23 17:55:40 -07001135 "acquired\n", a->host_no, __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001136 return QLA_SUCCESS;
1137 }
1138 }
1139 return QLA_ERROR;
1140}
1141
1142/**
1143 * qla4xxx_start_firmware - starts qla4xxx firmware
1144 * @ha: Pointer to host adapter structure.
1145 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001146 * This routine performs the necessary steps to start the firmware for
David Somayajuluafaf5a22006-09-19 10:28:00 -07001147 * the QLA4010 adapter.
1148 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301149int qla4xxx_start_firmware(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001150{
1151 unsigned long flags = 0;
1152 uint32_t mbox_status;
1153 int status = QLA_ERROR;
1154 int soft_reset = 1;
1155 int config_chip = 0;
1156
David C Somayajulud9150582006-11-15 17:38:40 -08001157 if (is_qla4022(ha) | is_qla4032(ha))
David Somayajuluafaf5a22006-09-19 10:28:00 -07001158 ql4xxx_set_mac_number(ha);
1159
1160 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
1161 return QLA_ERROR;
1162
1163 spin_lock_irqsave(&ha->hardware_lock, flags);
1164
1165 DEBUG2(printk("scsi%ld: %s: port_ctrl = 0x%08X\n", ha->host_no,
1166 __func__, readw(isp_port_ctrl(ha))));
1167 DEBUG(printk("scsi%ld: %s: port_status = 0x%08X\n", ha->host_no,
1168 __func__, readw(isp_port_status(ha))));
1169
1170 /* Is Hardware already initialized? */
1171 if ((readw(isp_port_ctrl(ha)) & 0x8000) != 0) {
1172 DEBUG(printk("scsi%ld: %s: Hardware has already been "
1173 "initialized\n", ha->host_no, __func__));
1174
1175 /* Receive firmware boot acknowledgement */
1176 mbox_status = readw(&ha->reg->mailbox[0]);
1177
1178 DEBUG2(printk("scsi%ld: %s: H/W Config complete - mbox[0]= "
1179 "0x%x\n", ha->host_no, __func__, mbox_status));
1180
1181 /* Is firmware already booted? */
1182 if (mbox_status == 0) {
1183 /* F/W not running, must be config by net driver */
1184 config_chip = 1;
1185 soft_reset = 0;
1186 } else {
1187 writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
1188 &ha->reg->ctrl_status);
1189 readl(&ha->reg->ctrl_status);
1190 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1191 if (qla4xxx_get_firmware_state(ha) == QLA_SUCCESS) {
1192 DEBUG2(printk("scsi%ld: %s: Get firmware "
1193 "state -- state = 0x%x\n",
1194 ha->host_no,
1195 __func__, ha->firmware_state));
1196 /* F/W is running */
1197 if (ha->firmware_state &
1198 FW_STATE_CONFIG_WAIT) {
1199 DEBUG2(printk("scsi%ld: %s: Firmware "
1200 "in known state -- "
1201 "config and "
1202 "boot, state = 0x%x\n",
1203 ha->host_no, __func__,
1204 ha->firmware_state));
1205 config_chip = 1;
1206 soft_reset = 0;
1207 }
1208 } else {
1209 DEBUG2(printk("scsi%ld: %s: Firmware in "
1210 "unknown state -- resetting,"
1211 " state = "
1212 "0x%x\n", ha->host_no, __func__,
1213 ha->firmware_state));
1214 }
1215 spin_lock_irqsave(&ha->hardware_lock, flags);
1216 }
1217 } else {
1218 DEBUG(printk("scsi%ld: %s: H/W initialization hasn't been "
1219 "started - resetting\n", ha->host_no, __func__));
1220 }
1221 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1222
1223 DEBUG(printk("scsi%ld: %s: Flags soft_rest=%d, config= %d\n ",
1224 ha->host_no, __func__, soft_reset, config_chip));
1225 if (soft_reset) {
1226 DEBUG(printk("scsi%ld: %s: Issue Soft Reset\n", ha->host_no,
1227 __func__));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301228 status = qla4xxx_soft_reset(ha); /* NOTE: acquires drvr
1229 * lock again, but ok */
David Somayajuluafaf5a22006-09-19 10:28:00 -07001230 if (status == QLA_ERROR) {
1231 DEBUG(printk("scsi%d: %s: Soft Reset failed!\n",
1232 ha->host_no, __func__));
1233 ql4xxx_unlock_drvr(ha);
1234 return QLA_ERROR;
1235 }
1236 config_chip = 1;
1237
Joe Perchesb1c11812008-02-03 17:28:22 +02001238 /* Reset clears the semaphore, so acquire again */
David Somayajuluafaf5a22006-09-19 10:28:00 -07001239 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
1240 return QLA_ERROR;
1241 }
1242
1243 if (config_chip) {
1244 if ((status = qla4xxx_config_nvram(ha)) == QLA_SUCCESS)
1245 status = qla4xxx_start_firmware_from_flash(ha);
1246 }
1247
1248 ql4xxx_unlock_drvr(ha);
1249 if (status == QLA_SUCCESS) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001250 if (test_and_clear_bit(AF_GET_CRASH_RECORD, &ha->flags))
1251 qla4xxx_get_crash_record(ha);
1252 } else {
1253 DEBUG(printk("scsi%ld: %s: Firmware has NOT started\n",
1254 ha->host_no, __func__));
1255 }
1256 return status;
1257}
1258
1259
1260/**
1261 * qla4xxx_initialize_adapter - initiailizes hba
1262 * @ha: Pointer to host adapter structure.
1263 * @renew_ddb_list: Indicates what to do with the adapter's ddb list
1264 * after adapter recovery has completed.
1265 * 0=preserve ddb list, 1=destroy and rebuild ddb list
1266 *
1267 * This routine parforms all of the steps necessary to initialize the adapter.
1268 *
1269 **/
1270int qla4xxx_initialize_adapter(struct scsi_qla_host *ha,
1271 uint8_t renew_ddb_list)
1272{
1273 int status = QLA_ERROR;
1274 int8_t ip_address[IP_ADDR_LEN] = {0} ;
1275
1276 ha->eeprom_cmd_data = 0;
1277
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301278 ql4_printk(KERN_INFO, ha, "Configuring PCI space...\n");
1279 ha->isp_ops->pci_config(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001280
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301281 ha->isp_ops->disable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001282
1283 /* Initialize the Host adapter request/response queues and firmware */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301284 if (ha->isp_ops->start_firmware(ha) == QLA_ERROR)
David C Somayajulu46235e62007-06-08 17:37:16 -07001285 goto exit_init_hba;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001286
Harish Zunjarrao7ad633c2011-05-17 23:17:11 -07001287 if (qla4xxx_about_firmware(ha) == QLA_ERROR)
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301288 goto exit_init_hba;
1289
1290 if (ha->isp_ops->get_sys_info(ha) == QLA_ERROR)
David C Somayajulu46235e62007-06-08 17:37:16 -07001291 goto exit_init_hba;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001292
1293 if (qla4xxx_init_local_data(ha) == QLA_ERROR)
David C Somayajulu46235e62007-06-08 17:37:16 -07001294 goto exit_init_hba;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001295
1296 status = qla4xxx_init_firmware(ha);
1297 if (status == QLA_ERROR)
David C Somayajulu46235e62007-06-08 17:37:16 -07001298 goto exit_init_hba;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001299
1300 /*
1301 * FW is waiting to get an IP address from DHCP server: Skip building
1302 * the ddb_list and wait for DHCP lease acquired aen to come in
1303 * followed by 0x8014 aen" to trigger the tgt discovery process.
1304 */
Vikas Chaudhary2a49a782010-04-28 11:37:07 +05301305 if (ha->firmware_state & FW_STATE_CONFIGURING_IP)
David C Somayajulu46235e62007-06-08 17:37:16 -07001306 goto exit_init_online;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001307
1308 /* Skip device discovery if ip and subnet is zero */
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -05001309 if (memcmp(ha->ip_config.ip_address, ip_address, IP_ADDR_LEN) == 0 ||
1310 memcmp(ha->ip_config.subnet_mask, ip_address, IP_ADDR_LEN) == 0)
David C Somayajulu46235e62007-06-08 17:37:16 -07001311 goto exit_init_online;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001312
1313 if (renew_ddb_list == PRESERVE_DDB_LIST) {
1314 /*
1315 * We want to preserve lun states (i.e. suspended, etc.)
1316 * for recovery initiated by the driver. So just update
1317 * the device states for the existing ddb_list.
1318 */
1319 qla4xxx_reinitialize_ddb_list(ha);
1320 } else if (renew_ddb_list == REBUILD_DDB_LIST) {
1321 /*
1322 * We want to build the ddb_list from scratch during
1323 * driver initialization and recovery initiated by the
1324 * INT_HBA_RESET IOCTL.
1325 */
1326 status = qla4xxx_initialize_ddb_list(ha);
1327 if (status == QLA_ERROR) {
1328 DEBUG2(printk("%s(%ld) Error occurred during build"
1329 "ddb list\n", __func__, ha->host_no));
1330 goto exit_init_hba;
1331 }
1332
1333 }
1334 if (!ha->tot_ddbs) {
1335 DEBUG2(printk("scsi%ld: Failed to initialize devices or none "
1336 "present in Firmware device database\n",
1337 ha->host_no));
1338 }
1339
David C Somayajulu46235e62007-06-08 17:37:16 -07001340exit_init_online:
David C Somayajulu92b72732007-05-23 17:55:40 -07001341 set_bit(AF_ONLINE, &ha->flags);
David C Somayajulu46235e62007-06-08 17:37:16 -07001342exit_init_hba:
Vikas Chaudhary3710c602010-10-06 22:49:08 -07001343 if (is_qla8022(ha) && (status == QLA_ERROR)) {
1344 /* Since interrupts are registered in start_firmware for
1345 * 82xx, release them here if initialize_adapter fails */
1346 qla4xxx_free_irqs(ha);
1347 }
1348
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301349 DEBUG2(printk("scsi%ld: initialize adapter: %s\n", ha->host_no,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001350 status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001351 return status;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001352}
1353
1354/**
1355 * qla4xxx_add_device_dynamically - ddb addition due to an AEN
1356 * @ha: Pointer to host adapter structure.
1357 * @fw_ddb_index: Firmware's device database index
1358 *
1359 * This routine processes adds a device as a result of an 8014h AEN.
1360 **/
1361static void qla4xxx_add_device_dynamically(struct scsi_qla_host *ha,
1362 uint32_t fw_ddb_index)
1363{
1364 struct ddb_entry * ddb_entry;
David C Somayajulu92b72732007-05-23 17:55:40 -07001365 uint32_t new_tgt;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001366
1367 /* First allocate a device structure */
David C Somayajulu92b72732007-05-23 17:55:40 -07001368 ddb_entry = qla4xxx_get_ddb_entry(ha, fw_ddb_index, &new_tgt);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001369 if (ddb_entry == NULL) {
1370 DEBUG2(printk(KERN_WARNING
1371 "scsi%ld: Unable to allocate memory to add "
1372 "fw_ddb_index %d\n", ha->host_no, fw_ddb_index));
1373 return;
1374 }
1375
David C Somayajulu92b72732007-05-23 17:55:40 -07001376 if (!new_tgt && (ddb_entry->fw_ddb_index != fw_ddb_index)) {
1377 /* Target has been bound to a new fw_ddb_index */
1378 qla4xxx_free_ddb(ha, ddb_entry);
1379 ddb_entry = qla4xxx_alloc_ddb(ha, fw_ddb_index);
1380 if (ddb_entry == NULL) {
1381 DEBUG2(printk(KERN_WARNING
1382 "scsi%ld: Unable to allocate memory"
1383 " to add fw_ddb_index %d\n",
1384 ha->host_no, fw_ddb_index));
1385 return;
1386 }
1387 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001388 if (qla4xxx_update_ddb_entry(ha, ddb_entry, fw_ddb_index) ==
1389 QLA_ERROR) {
1390 ha->fw_ddb_index_map[fw_ddb_index] =
1391 (struct ddb_entry *)INVALID_ENTRY;
1392 DEBUG2(printk(KERN_WARNING
1393 "scsi%ld: failed to add new device at index "
1394 "[%d]\n Unable to retrieve fw ddb entry\n",
1395 ha->host_no, fw_ddb_index));
1396 qla4xxx_free_ddb(ha, ddb_entry);
1397 return;
1398 }
1399
1400 if (qla4xxx_add_sess(ddb_entry)) {
1401 DEBUG2(printk(KERN_WARNING
1402 "scsi%ld: failed to add new device at index "
1403 "[%d]\n Unable to add connection and session\n",
1404 ha->host_no, fw_ddb_index));
1405 qla4xxx_free_ddb(ha, ddb_entry);
1406 }
1407}
1408
1409/**
1410 * qla4xxx_process_ddb_changed - process ddb state change
1411 * @ha - Pointer to host adapter structure.
1412 * @fw_ddb_index - Firmware's device database index
1413 * @state - Device state
1414 *
1415 * This routine processes a Decive Database Changed AEN Event.
1416 **/
Vikas Chaudhary821d6e52010-04-28 11:41:21 +05301417int qla4xxx_process_ddb_changed(struct scsi_qla_host *ha, uint32_t fw_ddb_index,
1418 uint32_t state, uint32_t conn_err)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001419{
1420 struct ddb_entry * ddb_entry;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001421
1422 /* check for out of range index */
1423 if (fw_ddb_index >= MAX_DDB_ENTRIES)
1424 return QLA_ERROR;
1425
1426 /* Get the corresponging ddb entry */
1427 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, fw_ddb_index);
1428 /* Device does not currently exist in our database. */
1429 if (ddb_entry == NULL) {
1430 if (state == DDB_DS_SESSION_ACTIVE)
1431 qla4xxx_add_device_dynamically(ha, fw_ddb_index);
1432 return QLA_SUCCESS;
1433 }
1434
1435 /* Device already exists in our database. */
David Somayajuluafaf5a22006-09-19 10:28:00 -07001436 DEBUG2(printk("scsi%ld: %s DDB - old state= 0x%x, new state=0x%x for "
1437 "index [%d]\n", ha->host_no, __func__,
1438 ddb_entry->fw_ddb_device_state, state, fw_ddb_index));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001439
1440 ddb_entry->fw_ddb_device_state = state;
1441 /* Device is back online. */
Vikas Chaudharyfc7657c2011-03-21 03:34:30 -07001442 if ((ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) &&
1443 (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)) {
Mike Christie50a29ae2008-03-04 13:26:53 -06001444 atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001445 atomic_set(&ddb_entry->relogin_retry_count, 0);
1446 atomic_set(&ddb_entry->relogin_timer, 0);
1447 clear_bit(DF_RELOGIN, &ddb_entry->flags);
Mike Christieb6359302008-01-31 13:36:44 -06001448 iscsi_unblock_session(ddb_entry->sess);
Mike Christie26974782007-12-13 12:43:29 -06001449 iscsi_session_event(ddb_entry->sess,
1450 ISCSI_KEVENT_CREATE_SESSION);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001451 /*
1452 * Change the lun state to READY in case the lun TIMEOUT before
1453 * the device came back.
1454 */
Vikas Chaudharyfc7657c2011-03-21 03:34:30 -07001455 } else if (ddb_entry->fw_ddb_device_state != DDB_DS_SESSION_ACTIVE) {
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301456 /* Device went away, mark device missing */
1457 if (atomic_read(&ddb_entry->state) == DDB_STATE_ONLINE) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301458 DEBUG2(ql4_printk(KERN_INFO, ha, "%s mark missing "
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301459 "ddb_entry 0x%p sess 0x%p conn 0x%p\n",
1460 __func__, ddb_entry,
1461 ddb_entry->sess, ddb_entry->conn));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001462 qla4xxx_mark_device_missing(ha, ddb_entry);
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301463 }
1464
David Somayajuluafaf5a22006-09-19 10:28:00 -07001465 /*
1466 * Relogin if device state changed to a not active state.
Vikas Chaudhary821d6e52010-04-28 11:41:21 +05301467 * However, do not relogin if a RELOGIN is in process, or
1468 * we are not allowed to relogin to this DDB.
David Somayajuluafaf5a22006-09-19 10:28:00 -07001469 */
1470 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_FAILED &&
1471 !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
Vikas Chaudhary821d6e52010-04-28 11:41:21 +05301472 qla4_is_relogin_allowed(ha, conn_err)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001473 /*
1474 * This triggers a relogin. After the relogin_timer
1475 * expires, the relogin gets scheduled. We must wait a
1476 * minimum amount of time since receiving an 0x8014 AEN
1477 * with failed device_state or a logout response before
1478 * we can issue another relogin.
1479 */
Vikas Chaudhary821d6e52010-04-28 11:41:21 +05301480 /* Firmware pads this timeout: (time2wait +1).
David Somayajuluafaf5a22006-09-19 10:28:00 -07001481 * Driver retry to login should be longer than F/W.
1482 * Otherwise F/W will fail
1483 * set_ddb() mbx cmd with 0x4005 since it still
1484 * counting down its time2wait.
1485 */
1486 atomic_set(&ddb_entry->relogin_timer, 0);
1487 atomic_set(&ddb_entry->retry_relogin_timer,
1488 ddb_entry->default_time2wait + 4);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05301489 DEBUG(printk("scsi%ld: %s: ddb[%d] "
1490 "initiate relogin after %d seconds\n",
1491 ha->host_no, __func__,
1492 ddb_entry->fw_ddb_index,
1493 ddb_entry->default_time2wait + 4));
1494 } else {
1495 DEBUG(printk("scsi%ld: %s: ddb[%d] "
1496 "relogin not initiated, state = %d, "
1497 "ddb_entry->flags = 0x%lx\n",
1498 ha->host_no, __func__,
1499 ddb_entry->fw_ddb_index,
1500 ddb_entry->fw_ddb_device_state,
1501 ddb_entry->flags));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001502 }
1503 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001504 return QLA_SUCCESS;
1505}