blob: ca6b2c5f0fdd510f30560b51cc06e36f471ff3bf [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 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 *
Manish Rangankarb3a271a2011-07-25 13:48:53 -050051 * This routine marks a DDB entry INVALID
David Somayajuluafaf5a22006-09-19 10:28:00 -070052 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053053void qla4xxx_free_ddb(struct scsi_qla_host *ha,
54 struct ddb_entry *ddb_entry)
David Somayajuluafaf5a22006-09-19 10:28:00 -070055{
David Somayajuluafaf5a22006-09-19 10:28:00 -070056 /* Remove device pointer from index mapping arrays */
57 ha->fw_ddb_index_map[ddb_entry->fw_ddb_index] =
58 (struct ddb_entry *) INVALID_ENTRY;
59 ha->tot_ddbs--;
David Somayajuluafaf5a22006-09-19 10:28:00 -070060}
61
62/**
63 * qla4xxx_free_ddb_list - deallocate all ddbs
64 * @ha: pointer to host adapter structure.
65 *
66 * This routine deallocates and removes all devices on the sppecified adapter.
67 **/
68void qla4xxx_free_ddb_list(struct scsi_qla_host *ha)
69{
70 struct list_head *ptr;
71 struct ddb_entry *ddb_entry;
72
73 while (!list_empty(&ha->ddb_list)) {
74 ptr = ha->ddb_list.next;
75 /* Free memory for device entry and remove */
76 ddb_entry = list_entry(ptr, struct ddb_entry, list);
77 qla4xxx_free_ddb(ha, ddb_entry);
78 }
79}
80
81/**
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053082 * qla4xxx_init_response_q_entries() - Initializes response queue entries.
83 * @ha: HA context
84 *
85 * Beginning of request ring has initialization control block already built
86 * by nvram config routine.
87 **/
88static void qla4xxx_init_response_q_entries(struct scsi_qla_host *ha)
89{
90 uint16_t cnt;
91 struct response *pkt;
92
93 pkt = (struct response *)ha->response_ptr;
94 for (cnt = 0; cnt < RESPONSE_QUEUE_DEPTH; cnt++) {
95 pkt->signature = RESPONSE_PROCESSED;
96 pkt++;
97 }
98}
99
100/**
David Somayajuluafaf5a22006-09-19 10:28:00 -0700101 * qla4xxx_init_rings - initialize hw queues
102 * @ha: pointer to host adapter structure.
103 *
104 * This routine initializes the internal queues for the specified adapter.
105 * The QLA4010 requires us to restart the queues at index 0.
106 * The QLA4000 doesn't care, so just default to QLA4010's requirement.
107 **/
108int qla4xxx_init_rings(struct scsi_qla_host *ha)
109{
110 unsigned long flags = 0;
111
112 /* Initialize request queue. */
113 spin_lock_irqsave(&ha->hardware_lock, flags);
114 ha->request_out = 0;
115 ha->request_in = 0;
116 ha->request_ptr = &ha->request_ring[ha->request_in];
117 ha->req_q_count = REQUEST_QUEUE_DEPTH;
118
119 /* Initialize response queue. */
120 ha->response_in = 0;
121 ha->response_out = 0;
122 ha->response_ptr = &ha->response_ring[ha->response_out];
123
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530124 if (is_qla8022(ha)) {
125 writel(0,
126 (unsigned long __iomem *)&ha->qla4_8xxx_reg->req_q_out);
127 writel(0,
128 (unsigned long __iomem *)&ha->qla4_8xxx_reg->rsp_q_in);
129 writel(0,
130 (unsigned long __iomem *)&ha->qla4_8xxx_reg->rsp_q_out);
131 } else {
132 /*
133 * Initialize DMA Shadow registers. The firmware is really
134 * supposed to take care of this, but on some uniprocessor
135 * systems, the shadow registers aren't cleared-- causing
136 * the interrupt_handler to think there are responses to be
137 * processed when there aren't.
138 */
139 ha->shadow_regs->req_q_out = __constant_cpu_to_le32(0);
140 ha->shadow_regs->rsp_q_in = __constant_cpu_to_le32(0);
141 wmb();
David Somayajuluafaf5a22006-09-19 10:28:00 -0700142
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530143 writel(0, &ha->reg->req_q_in);
144 writel(0, &ha->reg->rsp_q_out);
145 readl(&ha->reg->rsp_q_out);
146 }
147
148 qla4xxx_init_response_q_entries(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700149
150 spin_unlock_irqrestore(&ha->hardware_lock, flags);
151
152 return QLA_SUCCESS;
153}
154
155/**
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530156 * qla4xxx_get_sys_info - validate adapter MAC address(es)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700157 * @ha: pointer to host adapter structure.
158 *
159 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530160int qla4xxx_get_sys_info(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700161{
162 struct flash_sys_info *sys_info;
163 dma_addr_t sys_info_dma;
164 int status = QLA_ERROR;
165
166 sys_info = dma_alloc_coherent(&ha->pdev->dev, sizeof(*sys_info),
167 &sys_info_dma, GFP_KERNEL);
168 if (sys_info == NULL) {
169 DEBUG2(printk("scsi%ld: %s: Unable to allocate dma buffer.\n",
170 ha->host_no, __func__));
171
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530172 goto exit_get_sys_info_no_free;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700173 }
174 memset(sys_info, 0, sizeof(*sys_info));
175
176 /* Get flash sys info */
177 if (qla4xxx_get_flash(ha, sys_info_dma, FLASH_OFFSET_SYS_INFO,
178 sizeof(*sys_info)) != QLA_SUCCESS) {
179 DEBUG2(printk("scsi%ld: %s: get_flash FLASH_OFFSET_SYS_INFO "
180 "failed\n", ha->host_no, __func__));
181
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530182 goto exit_get_sys_info;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700183 }
184
185 /* Save M.A.C. address & serial_number */
186 memcpy(ha->my_mac, &sys_info->physAddr[0].address[0],
187 min(sizeof(ha->my_mac),
188 sizeof(sys_info->physAddr[0].address)));
189 memcpy(ha->serial_number, &sys_info->acSerialNumber,
190 min(sizeof(ha->serial_number),
191 sizeof(sys_info->acSerialNumber)));
192
193 status = QLA_SUCCESS;
194
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530195exit_get_sys_info:
David Somayajuluafaf5a22006-09-19 10:28:00 -0700196 dma_free_coherent(&ha->pdev->dev, sizeof(*sys_info), sys_info,
197 sys_info_dma);
198
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530199exit_get_sys_info_no_free:
David Somayajuluafaf5a22006-09-19 10:28:00 -0700200 return status;
201}
202
203/**
204 * qla4xxx_init_local_data - initialize adapter specific local data
205 * @ha: pointer to host adapter structure.
206 *
207 **/
208static int qla4xxx_init_local_data(struct scsi_qla_host *ha)
209{
Uwe Kleine-König421f91d2010-06-11 12:17:00 +0200210 /* Initialize aen queue */
David Somayajuluafaf5a22006-09-19 10:28:00 -0700211 ha->aen_q_count = MAX_AEN_ENTRIES;
212
213 return qla4xxx_get_firmware_status(ha);
214}
215
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530216static uint8_t
217qla4xxx_wait_for_ip_config(struct scsi_qla_host *ha)
218{
219 uint8_t ipv4_wait = 0;
220 uint8_t ipv6_wait = 0;
221 int8_t ip_address[IPv6_ADDR_LEN] = {0} ;
222
223 /* If both IPv4 & IPv6 are enabled, possibly only one
224 * IP address may be acquired, so check to see if we
225 * need to wait for another */
226 if (is_ipv4_enabled(ha) && is_ipv6_enabled(ha)) {
227 if (((ha->addl_fw_state & FW_ADDSTATE_DHCPv4_ENABLED) != 0) &&
228 ((ha->addl_fw_state &
229 FW_ADDSTATE_DHCPv4_LEASE_ACQUIRED) == 0)) {
230 ipv4_wait = 1;
231 }
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500232 if (((ha->ip_config.ipv6_addl_options &
233 IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) != 0) &&
234 ((ha->ip_config.ipv6_link_local_state ==
235 IP_ADDRSTATE_ACQUIRING) ||
236 (ha->ip_config.ipv6_addr0_state ==
237 IP_ADDRSTATE_ACQUIRING) ||
238 (ha->ip_config.ipv6_addr1_state ==
239 IP_ADDRSTATE_ACQUIRING))) {
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530240
241 ipv6_wait = 1;
242
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500243 if ((ha->ip_config.ipv6_link_local_state ==
244 IP_ADDRSTATE_PREFERRED) ||
245 (ha->ip_config.ipv6_addr0_state ==
246 IP_ADDRSTATE_PREFERRED) ||
247 (ha->ip_config.ipv6_addr1_state ==
248 IP_ADDRSTATE_PREFERRED)) {
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530249 DEBUG2(printk(KERN_INFO "scsi%ld: %s: "
250 "Preferred IP configured."
251 " Don't wait!\n", ha->host_no,
252 __func__));
253 ipv6_wait = 0;
254 }
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500255 if (memcmp(&ha->ip_config.ipv6_default_router_addr,
256 ip_address, IPv6_ADDR_LEN) == 0) {
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530257 DEBUG2(printk(KERN_INFO "scsi%ld: %s: "
258 "No Router configured. "
259 "Don't wait!\n", ha->host_no,
260 __func__));
261 ipv6_wait = 0;
262 }
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500263 if ((ha->ip_config.ipv6_default_router_state ==
264 IPV6_RTRSTATE_MANUAL) &&
265 (ha->ip_config.ipv6_link_local_state ==
266 IP_ADDRSTATE_TENTATIVE) &&
267 (memcmp(&ha->ip_config.ipv6_link_local_addr,
268 &ha->ip_config.ipv6_default_router_addr, 4) ==
269 0)) {
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530270 DEBUG2(printk("scsi%ld: %s: LinkLocal Router & "
271 "IP configured. Don't wait!\n",
272 ha->host_no, __func__));
273 ipv6_wait = 0;
274 }
275 }
276 if (ipv4_wait || ipv6_wait) {
277 DEBUG2(printk("scsi%ld: %s: Wait for additional "
278 "IP(s) \"", ha->host_no, __func__));
279 if (ipv4_wait)
280 DEBUG2(printk("IPv4 "));
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500281 if (ha->ip_config.ipv6_link_local_state ==
282 IP_ADDRSTATE_ACQUIRING)
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530283 DEBUG2(printk("IPv6LinkLocal "));
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500284 if (ha->ip_config.ipv6_addr0_state ==
285 IP_ADDRSTATE_ACQUIRING)
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530286 DEBUG2(printk("IPv6Addr0 "));
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500287 if (ha->ip_config.ipv6_addr1_state ==
288 IP_ADDRSTATE_ACQUIRING)
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530289 DEBUG2(printk("IPv6Addr1 "));
290 DEBUG2(printk("\"\n"));
291 }
292 }
293
294 return ipv4_wait|ipv6_wait;
295}
296
David Somayajuluafaf5a22006-09-19 10:28:00 -0700297static int qla4xxx_fw_ready(struct scsi_qla_host *ha)
298{
299 uint32_t timeout_count;
300 int ready = 0;
301
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530302 DEBUG2(ql4_printk(KERN_INFO, ha, "Waiting for Firmware Ready..\n"));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700303 for (timeout_count = ADAPTER_INIT_TOV; timeout_count > 0;
304 timeout_count--) {
305 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
306 qla4xxx_get_dhcp_ip_address(ha);
307
308 /* Get firmware state. */
309 if (qla4xxx_get_firmware_state(ha) != QLA_SUCCESS) {
310 DEBUG2(printk("scsi%ld: %s: unable to get firmware "
311 "state\n", ha->host_no, __func__));
312 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700313 }
314
315 if (ha->firmware_state & FW_STATE_ERROR) {
316 DEBUG2(printk("scsi%ld: %s: an unrecoverable error has"
317 " occurred\n", ha->host_no, __func__));
318 break;
319
320 }
321 if (ha->firmware_state & FW_STATE_CONFIG_WAIT) {
322 /*
323 * The firmware has not yet been issued an Initialize
324 * Firmware command, so issue it now.
325 */
326 if (qla4xxx_initialize_fw_cb(ha) == QLA_ERROR)
327 break;
328
329 /* Go back and test for ready state - no wait. */
330 continue;
331 }
332
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530333 if (ha->firmware_state & FW_STATE_WAIT_AUTOCONNECT) {
334 DEBUG2(printk(KERN_INFO "scsi%ld: %s: fwstate:"
335 "AUTOCONNECT in progress\n",
336 ha->host_no, __func__));
337 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700338
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530339 if (ha->firmware_state & FW_STATE_CONFIGURING_IP) {
340 DEBUG2(printk(KERN_INFO "scsi%ld: %s: fwstate:"
341 " CONFIGURING IP\n",
342 ha->host_no, __func__));
343 /*
344 * Check for link state after 15 secs and if link is
345 * still DOWN then, cable is unplugged. Ignore "DHCP
346 * in Progress/CONFIGURING IP" bit to check if firmware
347 * is in ready state or not after 15 secs.
348 * This is applicable for both 2.x & 3.x firmware
349 */
350 if (timeout_count <= (ADAPTER_INIT_TOV - 15)) {
351 if (ha->addl_fw_state & FW_ADDSTATE_LINK_UP) {
352 DEBUG2(printk(KERN_INFO "scsi%ld: %s:"
353 " LINK UP (Cable plugged)\n",
354 ha->host_no, __func__));
355 } else if (ha->firmware_state &
356 (FW_STATE_CONFIGURING_IP |
357 FW_STATE_READY)) {
358 DEBUG2(printk(KERN_INFO "scsi%ld: %s: "
359 "LINK DOWN (Cable unplugged)\n",
360 ha->host_no, __func__));
361 ha->firmware_state = FW_STATE_READY;
362 }
363 }
364 }
365
366 if (ha->firmware_state == FW_STATE_READY) {
367 /* If DHCP IP Addr is available, retrieve it now. */
368 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR,
369 &ha->dpc_flags))
370 qla4xxx_get_dhcp_ip_address(ha);
371
372 if (!qla4xxx_wait_for_ip_config(ha) ||
373 timeout_count == 1) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530374 DEBUG2(ql4_printk(KERN_INFO, ha,
375 "Firmware Ready..\n"));
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530376 /* The firmware is ready to process SCSI
377 commands. */
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530378 DEBUG2(ql4_printk(KERN_INFO, ha,
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530379 "scsi%ld: %s: MEDIA TYPE"
380 " - %s\n", ha->host_no,
381 __func__, (ha->addl_fw_state &
382 FW_ADDSTATE_OPTICAL_MEDIA)
383 != 0 ? "OPTICAL" : "COPPER"));
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530384 DEBUG2(ql4_printk(KERN_INFO, ha,
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530385 "scsi%ld: %s: DHCPv4 STATE"
386 " Enabled %s\n", ha->host_no,
387 __func__, (ha->addl_fw_state &
388 FW_ADDSTATE_DHCPv4_ENABLED) != 0 ?
389 "YES" : "NO"));
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530390 DEBUG2(ql4_printk(KERN_INFO, ha,
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530391 "scsi%ld: %s: LINK %s\n",
392 ha->host_no, __func__,
393 (ha->addl_fw_state &
394 FW_ADDSTATE_LINK_UP) != 0 ?
395 "UP" : "DOWN"));
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530396 DEBUG2(ql4_printk(KERN_INFO, ha,
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530397 "scsi%ld: %s: iSNS Service "
398 "Started %s\n",
399 ha->host_no, __func__,
400 (ha->addl_fw_state &
401 FW_ADDSTATE_ISNS_SVC_ENABLED) != 0 ?
402 "YES" : "NO"));
403
404 ready = 1;
405 break;
406 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700407 }
408 DEBUG2(printk("scsi%ld: %s: waiting on fw, state=%x:%x - "
409 "seconds expired= %d\n", ha->host_no, __func__,
410 ha->firmware_state, ha->addl_fw_state,
411 timeout_count));
David C Somayajulud9150582006-11-15 17:38:40 -0800412 if (is_qla4032(ha) &&
413 !(ha->addl_fw_state & FW_ADDSTATE_LINK_UP) &&
414 (timeout_count < ADAPTER_INIT_TOV - 5)) {
415 break;
416 }
417
David Somayajuluafaf5a22006-09-19 10:28:00 -0700418 msleep(1000);
419 } /* end of for */
420
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530421 if (timeout_count <= 0)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700422 DEBUG2(printk("scsi%ld: %s: FW Initialization timed out!\n",
423 ha->host_no, __func__));
424
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530425 if (ha->firmware_state & FW_STATE_CONFIGURING_IP) {
426 DEBUG2(printk("scsi%ld: %s: FW initialized, but is reporting "
427 "it's waiting to configure an IP address\n",
428 ha->host_no, __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700429 ready = 1;
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530430 } else if (ha->firmware_state & FW_STATE_WAIT_AUTOCONNECT) {
431 DEBUG2(printk("scsi%ld: %s: FW initialized, but "
432 "auto-discovery still in process\n",
433 ha->host_no, __func__));
Vikas Chaudharyb9663462010-07-10 14:49:19 +0530434 ready = 1;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700435 }
436
437 return ready;
438}
439
440/**
441 * qla4xxx_init_firmware - initializes the firmware.
442 * @ha: pointer to host adapter structure.
443 *
444 **/
445static int qla4xxx_init_firmware(struct scsi_qla_host *ha)
446{
447 int status = QLA_ERROR;
448
Lalit Chandivade2232be02010-07-30 14:38:47 +0530449 if (is_aer_supported(ha) &&
450 test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
451 return status;
452
Lalit Chandivade9d4946f2010-07-30 14:26:31 +0530453 /* For 82xx, stop firmware before initializing because if BIOS
454 * has previously initialized firmware, then driver's initialize
455 * firmware will fail. */
456 if (is_qla8022(ha))
457 qla4_8xxx_stop_firmware(ha);
458
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530459 ql4_printk(KERN_INFO, ha, "Initializing firmware..\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700460 if (qla4xxx_initialize_fw_cb(ha) == QLA_ERROR) {
461 DEBUG2(printk("scsi%ld: %s: Failed to initialize firmware "
462 "control block\n", ha->host_no, __func__));
463 return status;
464 }
465 if (!qla4xxx_fw_ready(ha))
466 return status;
467
David Somayajuluafaf5a22006-09-19 10:28:00 -0700468 return qla4xxx_get_firmware_status(ha);
469}
470
471static struct ddb_entry* qla4xxx_get_ddb_entry(struct scsi_qla_host *ha,
David C Somayajulu92b72732007-05-23 17:55:40 -0700472 uint32_t fw_ddb_index,
473 uint32_t *new_tgt)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700474{
475 struct dev_db_entry *fw_ddb_entry = NULL;
476 dma_addr_t fw_ddb_entry_dma;
477 struct ddb_entry *ddb_entry = NULL;
478 int found = 0;
479 uint32_t device_state;
480
David C Somayajulu92b72732007-05-23 17:55:40 -0700481 *new_tgt = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700482 /* Make sure the dma buffer is valid */
483 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev,
484 sizeof(*fw_ddb_entry),
485 &fw_ddb_entry_dma, GFP_KERNEL);
486 if (fw_ddb_entry == NULL) {
487 DEBUG2(printk("scsi%ld: %s: Unable to allocate dma buffer.\n",
488 ha->host_no, __func__));
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530489 goto exit_get_ddb_entry_no_free;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700490 }
491
492 if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index, fw_ddb_entry,
493 fw_ddb_entry_dma, NULL, NULL,
494 &device_state, NULL, NULL, NULL) ==
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530495 QLA_ERROR) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700496 DEBUG2(printk("scsi%ld: %s: failed get_ddb_entry for "
497 "fw_ddb_index %d\n", ha->host_no, __func__,
498 fw_ddb_index));
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530499 goto exit_get_ddb_entry;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700500 }
501
502 /* Allocate DDB if not already allocated. */
503 DEBUG2(printk("scsi%ld: %s: Looking for ddb[%d]\n", ha->host_no,
504 __func__, fw_ddb_index));
505 list_for_each_entry(ddb_entry, &ha->ddb_list, list) {
Mike Christie41bbdbe2009-01-16 12:36:52 -0600506 if ((memcmp(ddb_entry->iscsi_name, fw_ddb_entry->iscsi_name,
507 ISCSI_NAME_SIZE) == 0) &&
508 (ddb_entry->tpgt ==
509 le32_to_cpu(fw_ddb_entry->tgt_portal_grp)) &&
510 (memcmp(ddb_entry->isid, fw_ddb_entry->isid,
511 sizeof(ddb_entry->isid)) == 0)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700512 found++;
513 break;
514 }
515 }
516
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530517 /* if not found allocate new ddb */
David Somayajuluafaf5a22006-09-19 10:28:00 -0700518 if (!found) {
519 DEBUG2(printk("scsi%ld: %s: ddb[%d] not found - allocating "
520 "new ddb\n", ha->host_no, __func__,
521 fw_ddb_index));
David C Somayajulu92b72732007-05-23 17:55:40 -0700522 *new_tgt = 1;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700523 ddb_entry = qla4xxx_alloc_ddb(ha, fw_ddb_index);
524 }
525
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530526exit_get_ddb_entry:
David Somayajuluafaf5a22006-09-19 10:28:00 -0700527 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry), fw_ddb_entry,
528 fw_ddb_entry_dma);
529
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530530exit_get_ddb_entry_no_free:
David Somayajuluafaf5a22006-09-19 10:28:00 -0700531 return ddb_entry;
532}
533
534/**
535 * qla4xxx_update_ddb_entry - update driver's internal ddb
536 * @ha: pointer to host adapter structure.
537 * @ddb_entry: pointer to device database structure to be filled
538 * @fw_ddb_index: index of the ddb entry in fw ddb table
539 *
540 * This routine updates the driver's internal device database entry
541 * with information retrieved from the firmware's device database
542 * entry for the specified device. The ddb_entry->fw_ddb_index field
543 * must be initialized prior to calling this routine
544 *
545 **/
Adrian Bunk47975472007-04-26 00:35:16 -0700546static int qla4xxx_update_ddb_entry(struct scsi_qla_host *ha,
547 struct ddb_entry *ddb_entry,
548 uint32_t fw_ddb_index)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700549{
550 struct dev_db_entry *fw_ddb_entry = NULL;
551 dma_addr_t fw_ddb_entry_dma;
552 int status = QLA_ERROR;
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530553 uint32_t conn_err;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700554
555 if (ddb_entry == NULL) {
556 DEBUG2(printk("scsi%ld: %s: ddb_entry is NULL\n", ha->host_no,
557 __func__));
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530558
559 goto exit_update_ddb_no_free;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700560 }
561
562 /* Make sure the dma buffer is valid */
563 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev,
564 sizeof(*fw_ddb_entry),
565 &fw_ddb_entry_dma, GFP_KERNEL);
566 if (fw_ddb_entry == NULL) {
567 DEBUG2(printk("scsi%ld: %s: Unable to allocate dma buffer.\n",
568 ha->host_no, __func__));
569
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530570 goto exit_update_ddb_no_free;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700571 }
572
573 if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index, fw_ddb_entry,
574 fw_ddb_entry_dma, NULL, NULL,
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530575 &ddb_entry->fw_ddb_device_state, &conn_err,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700576 &ddb_entry->tcp_source_port_num,
577 &ddb_entry->connection_id) ==
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530578 QLA_ERROR) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700579 DEBUG2(printk("scsi%ld: %s: failed get_ddb_entry for "
580 "fw_ddb_index %d\n", ha->host_no, __func__,
581 fw_ddb_index));
582
583 goto exit_update_ddb;
584 }
585
586 status = QLA_SUCCESS;
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530587 ddb_entry->options = le16_to_cpu(fw_ddb_entry->options);
David C Somayajulu92b72732007-05-23 17:55:40 -0700588 ddb_entry->target_session_id = le16_to_cpu(fw_ddb_entry->tsid);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700589 ddb_entry->task_mgmt_timeout =
David C Somayajulu92b72732007-05-23 17:55:40 -0700590 le16_to_cpu(fw_ddb_entry->def_timeout);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700591 ddb_entry->CmdSn = 0;
David C Somayajulu92b72732007-05-23 17:55:40 -0700592 ddb_entry->exe_throttle = le16_to_cpu(fw_ddb_entry->exec_throttle);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700593 ddb_entry->default_relogin_timeout =
David C Somayajulu92b72732007-05-23 17:55:40 -0700594 le16_to_cpu(fw_ddb_entry->def_timeout);
595 ddb_entry->default_time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700596
597 /* Update index in case it changed */
598 ddb_entry->fw_ddb_index = fw_ddb_index;
599 ha->fw_ddb_index_map[fw_ddb_index] = ddb_entry;
600
David C Somayajulu92b72732007-05-23 17:55:40 -0700601 ddb_entry->port = le16_to_cpu(fw_ddb_entry->port);
602 ddb_entry->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
Mike Christie41bbdbe2009-01-16 12:36:52 -0600603 memcpy(ddb_entry->isid, fw_ddb_entry->isid, sizeof(ddb_entry->isid));
604
David C Somayajulu92b72732007-05-23 17:55:40 -0700605 memcpy(&ddb_entry->iscsi_name[0], &fw_ddb_entry->iscsi_name[0],
David Somayajuluafaf5a22006-09-19 10:28:00 -0700606 min(sizeof(ddb_entry->iscsi_name),
David C Somayajulu92b72732007-05-23 17:55:40 -0700607 sizeof(fw_ddb_entry->iscsi_name)));
Vikas Chaudhary3b2bef12010-07-10 14:51:30 +0530608 memcpy(&ddb_entry->iscsi_alias[0], &fw_ddb_entry->iscsi_alias[0],
609 min(sizeof(ddb_entry->iscsi_alias),
610 sizeof(fw_ddb_entry->iscsi_alias)));
David C Somayajulu92b72732007-05-23 17:55:40 -0700611 memcpy(&ddb_entry->ip_addr[0], &fw_ddb_entry->ip_addr[0],
612 min(sizeof(ddb_entry->ip_addr), sizeof(fw_ddb_entry->ip_addr)));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700613
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530614 ddb_entry->iscsi_max_burst_len = fw_ddb_entry->iscsi_max_burst_len;
615 ddb_entry->iscsi_max_outsnd_r2t = fw_ddb_entry->iscsi_max_outsnd_r2t;
616 ddb_entry->iscsi_first_burst_len = fw_ddb_entry->iscsi_first_burst_len;
617 ddb_entry->iscsi_max_rcv_data_seg_len =
618 fw_ddb_entry->iscsi_max_rcv_data_seg_len;
619 ddb_entry->iscsi_max_snd_data_seg_len =
620 fw_ddb_entry->iscsi_max_snd_data_seg_len;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700621
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530622 if (ddb_entry->options & DDB_OPT_IPV6_DEVICE) {
623 memcpy(&ddb_entry->remote_ipv6_addr,
624 fw_ddb_entry->ip_addr,
625 min(sizeof(ddb_entry->remote_ipv6_addr),
626 sizeof(fw_ddb_entry->ip_addr)));
627 memcpy(&ddb_entry->link_local_ipv6_addr,
628 fw_ddb_entry->link_local_ipv6_addr,
629 min(sizeof(ddb_entry->link_local_ipv6_addr),
630 sizeof(fw_ddb_entry->link_local_ipv6_addr)));
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530631
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530632 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: DDB[%d] State %04x"
633 " ConnErr %08x IP %pI6 "
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530634 ":%04d \"%s\"\n",
635 __func__, fw_ddb_index,
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530636 ddb_entry->fw_ddb_device_state,
637 conn_err, fw_ddb_entry->ip_addr,
638 le16_to_cpu(fw_ddb_entry->port),
639 fw_ddb_entry->iscsi_name));
640 } else
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530641 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: DDB[%d] State %04x"
642 " ConnErr %08x IP %pI4 "
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530643 ":%04d \"%s\"\n",
644 __func__, fw_ddb_index,
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530645 ddb_entry->fw_ddb_device_state,
646 conn_err, fw_ddb_entry->ip_addr,
647 le16_to_cpu(fw_ddb_entry->port),
648 fw_ddb_entry->iscsi_name));
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530649exit_update_ddb:
David Somayajuluafaf5a22006-09-19 10:28:00 -0700650 if (fw_ddb_entry)
651 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
652 fw_ddb_entry, fw_ddb_entry_dma);
653
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530654exit_update_ddb_no_free:
David Somayajuluafaf5a22006-09-19 10:28:00 -0700655 return status;
656}
657
658/**
659 * qla4xxx_alloc_ddb - allocate device database entry
660 * @ha: Pointer to host adapter structure.
661 * @fw_ddb_index: Firmware's device database index
662 *
663 * This routine allocates a ddb_entry, ititializes some values, and
664 * inserts it into the ddb list.
665 **/
Adrian Bunk47975472007-04-26 00:35:16 -0700666static struct ddb_entry * qla4xxx_alloc_ddb(struct scsi_qla_host *ha,
667 uint32_t fw_ddb_index)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700668{
669 struct ddb_entry *ddb_entry;
670
671 DEBUG2(printk("scsi%ld: %s: fw_ddb_index [%d]\n", ha->host_no,
672 __func__, fw_ddb_index));
673
674 ddb_entry = qla4xxx_alloc_sess(ha);
675 if (ddb_entry == NULL) {
676 DEBUG2(printk("scsi%ld: %s: Unable to allocate memory "
677 "to add fw_ddb_index [%d]\n",
678 ha->host_no, __func__, fw_ddb_index));
679 return ddb_entry;
680 }
681
682 ddb_entry->fw_ddb_index = fw_ddb_index;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700683 atomic_set(&ddb_entry->retry_relogin_timer, INVALID_ENTRY);
684 atomic_set(&ddb_entry->relogin_timer, 0);
685 atomic_set(&ddb_entry->relogin_retry_count, 0);
686 atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
687 list_add_tail(&ddb_entry->list, &ha->ddb_list);
688 ha->fw_ddb_index_map[fw_ddb_index] = ddb_entry;
689 ha->tot_ddbs++;
690
691 return ddb_entry;
692}
693
694/**
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530695 * qla4_is_relogin_allowed - Are we allowed to login?
696 * @ha: Pointer to host adapter structure.
697 * @conn_err: Last connection error associated with the ddb
698 *
699 * This routine tests the given connection error to determine if
700 * we are allowed to login.
701 **/
702int qla4_is_relogin_allowed(struct scsi_qla_host *ha, uint32_t conn_err)
703{
704 uint32_t err_code, login_rsp_sts_class;
705 int relogin = 1;
706
707 err_code = ((conn_err & 0x00ff0000) >> 16);
708 login_rsp_sts_class = ((conn_err & 0x0000ff00) >> 8);
709 if (err_code == 0x1c || err_code == 0x06) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530710 DEBUG2(ql4_printk(KERN_INFO, ha,
711 ": conn_err=0x%08x, send target completed"
712 " or access denied failure\n", conn_err));
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530713 relogin = 0;
714 }
715 if ((err_code == 0x08) && (login_rsp_sts_class == 0x02)) {
716 /* Login Response PDU returned an error.
717 Login Response Status in Error Code Detail
718 indicates login should not be retried.*/
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530719 DEBUG2(ql4_printk(KERN_INFO, ha,
720 ": conn_err=0x%08x, do not retry relogin\n",
721 conn_err));
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530722 relogin = 0;
723 }
724
725 return relogin;
726}
727
Karen Higgins7edd9a72011-03-21 03:34:27 -0700728static void qla4xxx_flush_AENS(struct scsi_qla_host *ha)
729{
730 unsigned long wtime;
731
732 /* Flush the 0x8014 AEN from the firmware as a result of
733 * Auto connect. We are basically doing get_firmware_ddb()
734 * to determine whether we need to log back in or not.
735 * Trying to do a set ddb before we have processed 0x8014
736 * will result in another set_ddb() for the same ddb. In other
737 * words there will be stale entries in the aen_q.
738 */
739 wtime = jiffies + (2 * HZ);
740 do {
741 if (qla4xxx_get_firmware_state(ha) == QLA_SUCCESS)
742 if (ha->firmware_state & (BIT_2 | BIT_0))
743 return;
744
745 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
746 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
747
748 msleep(1000);
749 } while (!time_after_eq(jiffies, wtime));
750}
751
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530752/**
Karen Higgins7edd9a72011-03-21 03:34:27 -0700753 * qla4xxx_build_ddb_list - builds driver ddb list
David Somayajuluafaf5a22006-09-19 10:28:00 -0700754 * @ha: Pointer to host adapter structure.
755 *
756 * This routine searches for all valid firmware ddb entries and builds
757 * an internal ddb list. Ddbs that are considered valid are those with
758 * a device state of SESSION_ACTIVE.
Karen Higgins7edd9a72011-03-21 03:34:27 -0700759 * A relogin (set_ddb) is issued for DDBs that are not online.
David Somayajuluafaf5a22006-09-19 10:28:00 -0700760 **/
761static int qla4xxx_build_ddb_list(struct scsi_qla_host *ha)
762{
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530763 int status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700764 uint32_t fw_ddb_index = 0;
765 uint32_t next_fw_ddb_index = 0;
766 uint32_t ddb_state;
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530767 uint32_t conn_err;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700768 struct ddb_entry *ddb_entry;
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530769 struct dev_db_entry *fw_ddb_entry = NULL;
770 dma_addr_t fw_ddb_entry_dma;
771 uint32_t ipv6_device;
David C Somayajulu92b72732007-05-23 17:55:40 -0700772 uint32_t new_tgt;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700773
Karen Higgins7edd9a72011-03-21 03:34:27 -0700774 qla4xxx_flush_AENS(ha);
775
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530776 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
777 &fw_ddb_entry_dma, GFP_KERNEL);
778 if (fw_ddb_entry == NULL) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530779 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: DMA alloc failed\n",
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530780 __func__));
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530781
782 goto exit_build_ddb_list_no_free;
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530783 }
784
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530785 ql4_printk(KERN_INFO, ha, "Initializing DDBs ...\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700786 for (fw_ddb_index = 0; fw_ddb_index < MAX_DDB_ENTRIES;
787 fw_ddb_index = next_fw_ddb_index) {
788 /* First, let's see if a device exists here */
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530789 if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index, fw_ddb_entry,
790 0, NULL, &next_fw_ddb_index,
791 &ddb_state, &conn_err,
792 NULL, NULL) ==
793 QLA_ERROR) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700794 DEBUG2(printk("scsi%ld: %s: get_ddb_entry, "
795 "fw_ddb_index %d failed", ha->host_no,
796 __func__, fw_ddb_index));
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530797 goto exit_build_ddb_list;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700798 }
799
800 DEBUG2(printk("scsi%ld: %s: Getting DDB[%d] ddbstate=0x%x, "
801 "next_fw_ddb_index=%d.\n", ha->host_no, __func__,
802 fw_ddb_index, ddb_state, next_fw_ddb_index));
803
804 /* Issue relogin, if necessary. */
805 if (ddb_state == DDB_DS_SESSION_FAILED ||
806 ddb_state == DDB_DS_NO_CONNECTION_ACTIVE) {
807 /* Try and login to device */
808 DEBUG2(printk("scsi%ld: %s: Login to DDB[%d]\n",
809 ha->host_no, __func__, fw_ddb_index));
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530810 ipv6_device = le16_to_cpu(fw_ddb_entry->options) &
811 DDB_OPT_IPV6_DEVICE;
812 if (qla4_is_relogin_allowed(ha, conn_err) &&
813 ((!ipv6_device &&
814 *((uint32_t *)fw_ddb_entry->ip_addr))
815 || ipv6_device)) {
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500816 qla4xxx_set_ddb_entry(ha, fw_ddb_index, 0,
817 NULL);
David C Somayajulu92b72732007-05-23 17:55:40 -0700818 if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index,
Vikas Chaudhary821d6e52010-04-28 11:41:21 +0530819 NULL, 0, NULL,
820 &next_fw_ddb_index,
821 &ddb_state, &conn_err,
822 NULL, NULL)
823 == QLA_ERROR) {
David C Somayajulu92b72732007-05-23 17:55:40 -0700824 DEBUG2(printk("scsi%ld: %s:"
825 "get_ddb_entry %d failed\n",
826 ha->host_no,
827 __func__, fw_ddb_index));
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530828 goto exit_build_ddb_list;
David C Somayajulu92b72732007-05-23 17:55:40 -0700829 }
830 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700831 }
832
833 if (ddb_state != DDB_DS_SESSION_ACTIVE)
834 goto next_one;
835 /*
836 * if fw_ddb with session active state found,
837 * add to ddb_list
838 */
839 DEBUG2(printk("scsi%ld: %s: DDB[%d] added to list\n",
840 ha->host_no, __func__, fw_ddb_index));
841
842 /* Add DDB to internal our ddb list. */
David C Somayajulu92b72732007-05-23 17:55:40 -0700843 ddb_entry = qla4xxx_get_ddb_entry(ha, fw_ddb_index, &new_tgt);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700844 if (ddb_entry == NULL) {
845 DEBUG2(printk("scsi%ld: %s: Unable to allocate memory "
846 "for device at fw_ddb_index %d\n",
847 ha->host_no, __func__, fw_ddb_index));
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530848 goto exit_build_ddb_list;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700849 }
850 /* Fill in the device structure */
851 if (qla4xxx_update_ddb_entry(ha, ddb_entry, fw_ddb_index) ==
852 QLA_ERROR) {
853 ha->fw_ddb_index_map[fw_ddb_index] =
854 (struct ddb_entry *)INVALID_ENTRY;
855
David Somayajuluafaf5a22006-09-19 10:28:00 -0700856 DEBUG2(printk("scsi%ld: %s: update_ddb_entry failed "
857 "for fw_ddb_index %d.\n",
858 ha->host_no, __func__, fw_ddb_index));
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530859 goto exit_build_ddb_list;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700860 }
861
862next_one:
863 /* We know we've reached the last device when
864 * next_fw_ddb_index is 0 */
865 if (next_fw_ddb_index == 0)
866 break;
867 }
868
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530869 status = QLA_SUCCESS;
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530870 ql4_printk(KERN_INFO, ha, "DDB list done..\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700871
Prasanna Mumbaibeabe7c2010-07-10 14:49:38 +0530872exit_build_ddb_list:
873 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry), fw_ddb_entry,
874 fw_ddb_entry_dma);
875
876exit_build_ddb_list_no_free:
David Somayajuluafaf5a22006-09-19 10:28:00 -0700877 return status;
878}
879
David Somayajuluafaf5a22006-09-19 10:28:00 -0700880static int qla4xxx_initialize_ddb_list(struct scsi_qla_host *ha)
881{
882 uint16_t fw_ddb_index;
883 int status = QLA_SUCCESS;
884
885 /* free the ddb list if is not empty */
886 if (!list_empty(&ha->ddb_list))
887 qla4xxx_free_ddb_list(ha);
888
889 for (fw_ddb_index = 0; fw_ddb_index < MAX_DDB_ENTRIES; fw_ddb_index++)
890 ha->fw_ddb_index_map[fw_ddb_index] =
Karen Higgins7edd9a72011-03-21 03:34:27 -0700891 (struct ddb_entry *)INVALID_ENTRY;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700892
893 ha->tot_ddbs = 0;
894
Karen Higgins7edd9a72011-03-21 03:34:27 -0700895 /* Perform device discovery and build ddb list. */
896 status = qla4xxx_build_ddb_list(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700897
David Somayajuluafaf5a22006-09-19 10:28:00 -0700898 return status;
899}
900
901/**
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530902 * qla4xxx_reinitialize_ddb_list - update the driver ddb list
David Somayajuluafaf5a22006-09-19 10:28:00 -0700903 * @ha: pointer to host adapter structure.
904 *
905 * This routine obtains device information from the F/W database after
906 * firmware or adapter resets. The device table is preserved.
907 **/
908int qla4xxx_reinitialize_ddb_list(struct scsi_qla_host *ha)
909{
910 int status = QLA_SUCCESS;
911 struct ddb_entry *ddb_entry, *detemp;
912
913 /* Update the device information for all devices. */
914 list_for_each_entry_safe(ddb_entry, detemp, &ha->ddb_list, list) {
915 qla4xxx_update_ddb_entry(ha, ddb_entry,
916 ddb_entry->fw_ddb_index);
917 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
918 atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
919 DEBUG2(printk ("scsi%ld: %s: ddb index [%d] marked "
920 "ONLINE\n", ha->host_no, __func__,
921 ddb_entry->fw_ddb_index));
Vikas Chaudhary36386322010-07-10 14:49:01 +0530922 iscsi_unblock_session(ddb_entry->sess);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700923 } else if (atomic_read(&ddb_entry->state) == DDB_STATE_ONLINE)
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500924 qla4xxx_mark_device_missing(ddb_entry->sess);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700925 }
926 return status;
927}
928
929/**
930 * qla4xxx_relogin_device - re-establish session
931 * @ha: Pointer to host adapter structure.
932 * @ddb_entry: Pointer to device database entry
933 *
934 * This routine does a session relogin with the specified device.
935 * The ddb entry must be assigned prior to making this call.
936 **/
937int qla4xxx_relogin_device(struct scsi_qla_host *ha,
938 struct ddb_entry * ddb_entry)
939{
940 uint16_t relogin_timer;
941
942 relogin_timer = max(ddb_entry->default_relogin_timeout,
943 (uint16_t)RELOGIN_TOV);
944 atomic_set(&ddb_entry->relogin_timer, relogin_timer);
945
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530946 DEBUG2(printk("scsi%ld: Relogin ddb [%d]. TOV=%d\n", ha->host_no,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700947 ddb_entry->fw_ddb_index, relogin_timer));
948
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500949 qla4xxx_set_ddb_entry(ha, ddb_entry->fw_ddb_index, 0, NULL);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700950
951 return QLA_SUCCESS;
952}
953
David Somayajuluafaf5a22006-09-19 10:28:00 -0700954static int qla4xxx_config_nvram(struct scsi_qla_host *ha)
955{
956 unsigned long flags;
957 union external_hw_config_reg extHwConfig;
958
959 DEBUG2(printk("scsi%ld: %s: Get EEProm parameters \n", ha->host_no,
960 __func__));
961 if (ql4xxx_lock_flash(ha) != QLA_SUCCESS)
Mike Christieb3925512010-02-10 16:51:48 -0600962 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700963 if (ql4xxx_lock_nvram(ha) != QLA_SUCCESS) {
964 ql4xxx_unlock_flash(ha);
Mike Christieb3925512010-02-10 16:51:48 -0600965 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700966 }
967
968 /* Get EEPRom Parameters from NVRAM and validate */
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530969 ql4_printk(KERN_INFO, ha, "Configuring NVRAM ...\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700970 if (qla4xxx_is_nvram_configuration_valid(ha) == QLA_SUCCESS) {
971 spin_lock_irqsave(&ha->hardware_lock, flags);
972 extHwConfig.Asuint32_t =
973 rd_nvram_word(ha, eeprom_ext_hw_conf_offset(ha));
974 spin_unlock_irqrestore(&ha->hardware_lock, flags);
975 } else {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530976 ql4_printk(KERN_WARNING, ha,
977 "scsi%ld: %s: EEProm checksum invalid. "
978 "Please update your EEPROM\n", ha->host_no,
979 __func__);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700980
Mike Christieb3925512010-02-10 16:51:48 -0600981 /* Attempt to set defaults */
David Somayajuluafaf5a22006-09-19 10:28:00 -0700982 if (is_qla4010(ha))
983 extHwConfig.Asuint32_t = 0x1912;
David C Somayajulud9150582006-11-15 17:38:40 -0800984 else if (is_qla4022(ha) | is_qla4032(ha))
David Somayajuluafaf5a22006-09-19 10:28:00 -0700985 extHwConfig.Asuint32_t = 0x0023;
Mike Christieb3925512010-02-10 16:51:48 -0600986 else
987 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700988 }
989 DEBUG(printk("scsi%ld: %s: Setting extHwConfig to 0xFFFF%04x\n",
990 ha->host_no, __func__, extHwConfig.Asuint32_t));
991
992 spin_lock_irqsave(&ha->hardware_lock, flags);
993 writel((0xFFFF << 16) | extHwConfig.Asuint32_t, isp_ext_hw_conf(ha));
994 readl(isp_ext_hw_conf(ha));
995 spin_unlock_irqrestore(&ha->hardware_lock, flags);
996
997 ql4xxx_unlock_nvram(ha);
998 ql4xxx_unlock_flash(ha);
999
Mike Christieb3925512010-02-10 16:51:48 -06001000 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001001}
1002
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301003/**
1004 * qla4_8xxx_pci_config() - Setup ISP82xx PCI configuration registers.
1005 * @ha: HA context
1006 */
1007void qla4_8xxx_pci_config(struct scsi_qla_host *ha)
1008{
1009 pci_set_master(ha->pdev);
1010}
1011
1012void qla4xxx_pci_config(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001013{
David C Somayajulu92b72732007-05-23 17:55:40 -07001014 uint16_t w;
David C Somayajulu46235e62007-06-08 17:37:16 -07001015 int status;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001016
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301017 ql4_printk(KERN_INFO, ha, "Configuring PCI space...\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001018
1019 pci_set_master(ha->pdev);
David C Somayajulu46235e62007-06-08 17:37:16 -07001020 status = pci_set_mwi(ha->pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001021 /*
1022 * We want to respect framework's setting of PCI configuration space
1023 * command register and also want to make sure that all bits of
1024 * interest to us are properly set in command register.
1025 */
1026 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
David C Somayajulu92b72732007-05-23 17:55:40 -07001027 w |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001028 w &= ~PCI_COMMAND_INTX_DISABLE;
1029 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
1030}
1031
1032static int qla4xxx_start_firmware_from_flash(struct scsi_qla_host *ha)
1033{
1034 int status = QLA_ERROR;
Vikas Chaudharyc301b022010-04-28 11:40:37 +05301035 unsigned long max_wait_time;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001036 unsigned long flags;
1037 uint32_t mbox_status;
1038
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05301039 ql4_printk(KERN_INFO, ha, "Starting firmware ...\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07001040
1041 /*
1042 * Start firmware from flash ROM
1043 *
1044 * WORKAROUND: Stuff a non-constant value that the firmware can
1045 * use as a seed for a random number generator in MB7 prior to
1046 * setting BOOT_ENABLE. Fixes problem where the TCP
1047 * connections use the same TCP ports after each reboot,
1048 * causing some connections to not get re-established.
1049 */
1050 DEBUG(printk("scsi%d: %s: Start firmware from flash ROM\n",
1051 ha->host_no, __func__));
1052
1053 spin_lock_irqsave(&ha->hardware_lock, flags);
1054 writel(jiffies, &ha->reg->mailbox[7]);
David C Somayajulud9150582006-11-15 17:38:40 -08001055 if (is_qla4022(ha) | is_qla4032(ha))
David Somayajuluafaf5a22006-09-19 10:28:00 -07001056 writel(set_rmask(NVR_WRITE_ENABLE),
1057 &ha->reg->u1.isp4022.nvram);
1058
David C Somayajulu92b72732007-05-23 17:55:40 -07001059 writel(2, &ha->reg->mailbox[6]);
1060 readl(&ha->reg->mailbox[6]);
1061
David Somayajuluafaf5a22006-09-19 10:28:00 -07001062 writel(set_rmask(CSR_BOOT_ENABLE), &ha->reg->ctrl_status);
1063 readl(&ha->reg->ctrl_status);
1064 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1065
1066 /* Wait for firmware to come UP. */
Vikas Chaudharyc301b022010-04-28 11:40:37 +05301067 DEBUG2(printk(KERN_INFO "scsi%ld: %s: Wait up to %d seconds for "
1068 "boot firmware to complete...\n",
1069 ha->host_no, __func__, FIRMWARE_UP_TOV));
1070 max_wait_time = jiffies + (FIRMWARE_UP_TOV * HZ);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001071 do {
1072 uint32_t ctrl_status;
1073
1074 spin_lock_irqsave(&ha->hardware_lock, flags);
1075 ctrl_status = readw(&ha->reg->ctrl_status);
1076 mbox_status = readw(&ha->reg->mailbox[0]);
1077 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1078
1079 if (ctrl_status & set_rmask(CSR_SCSI_PROCESSOR_INTR))
1080 break;
1081 if (mbox_status == MBOX_STS_COMMAND_COMPLETE)
1082 break;
1083
Vikas Chaudharyc301b022010-04-28 11:40:37 +05301084 DEBUG2(printk(KERN_INFO "scsi%ld: %s: Waiting for boot "
Vikas Chaudharyf581a3f2010-10-06 22:47:48 -07001085 "firmware to complete... ctrl_sts=0x%x, remaining=%ld\n",
1086 ha->host_no, __func__, ctrl_status, max_wait_time));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001087
Vikas Chaudharyc301b022010-04-28 11:40:37 +05301088 msleep_interruptible(250);
1089 } while (!time_after_eq(jiffies, max_wait_time));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001090
1091 if (mbox_status == MBOX_STS_COMMAND_COMPLETE) {
Vikas Chaudharyc301b022010-04-28 11:40:37 +05301092 DEBUG(printk(KERN_INFO "scsi%ld: %s: Firmware has started\n",
David Somayajuluafaf5a22006-09-19 10:28:00 -07001093 ha->host_no, __func__));
1094
1095 spin_lock_irqsave(&ha->hardware_lock, flags);
1096 writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
1097 &ha->reg->ctrl_status);
1098 readl(&ha->reg->ctrl_status);
1099 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1100
1101 status = QLA_SUCCESS;
1102 } else {
1103 printk(KERN_INFO "scsi%ld: %s: Boot firmware failed "
1104 "- mbox status 0x%x\n", ha->host_no, __func__,
1105 mbox_status);
1106 status = QLA_ERROR;
1107 }
1108 return status;
1109}
1110
David C Somayajulu92b72732007-05-23 17:55:40 -07001111int ql4xxx_lock_drvr_wait(struct scsi_qla_host *a)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001112{
David C Somayajulu92b72732007-05-23 17:55:40 -07001113#define QL4_LOCK_DRVR_WAIT 60
David C Somayajulu477ffb92007-01-22 12:26:11 -08001114#define QL4_LOCK_DRVR_SLEEP 1
David Somayajuluafaf5a22006-09-19 10:28:00 -07001115
1116 int drvr_wait = QL4_LOCK_DRVR_WAIT;
1117 while (drvr_wait) {
David C Somayajulu92b72732007-05-23 17:55:40 -07001118 if (ql4xxx_lock_drvr(a) == 0) {
David C Somayajulu477ffb92007-01-22 12:26:11 -08001119 ssleep(QL4_LOCK_DRVR_SLEEP);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001120 if (drvr_wait) {
1121 DEBUG2(printk("scsi%ld: %s: Waiting for "
David C Somayajulu92b72732007-05-23 17:55:40 -07001122 "Global Init Semaphore(%d)...\n",
1123 a->host_no,
David C Somayajulu477ffb92007-01-22 12:26:11 -08001124 __func__, drvr_wait));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001125 }
1126 drvr_wait -= QL4_LOCK_DRVR_SLEEP;
1127 } else {
1128 DEBUG2(printk("scsi%ld: %s: Global Init Semaphore "
David C Somayajulu92b72732007-05-23 17:55:40 -07001129 "acquired\n", a->host_no, __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001130 return QLA_SUCCESS;
1131 }
1132 }
1133 return QLA_ERROR;
1134}
1135
1136/**
1137 * qla4xxx_start_firmware - starts qla4xxx firmware
1138 * @ha: Pointer to host adapter structure.
1139 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001140 * This routine performs the necessary steps to start the firmware for
David Somayajuluafaf5a22006-09-19 10:28:00 -07001141 * the QLA4010 adapter.
1142 **/
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301143int qla4xxx_start_firmware(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001144{
1145 unsigned long flags = 0;
1146 uint32_t mbox_status;
1147 int status = QLA_ERROR;
1148 int soft_reset = 1;
1149 int config_chip = 0;
1150
David C Somayajulud9150582006-11-15 17:38:40 -08001151 if (is_qla4022(ha) | is_qla4032(ha))
David Somayajuluafaf5a22006-09-19 10:28:00 -07001152 ql4xxx_set_mac_number(ha);
1153
1154 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
1155 return QLA_ERROR;
1156
1157 spin_lock_irqsave(&ha->hardware_lock, flags);
1158
1159 DEBUG2(printk("scsi%ld: %s: port_ctrl = 0x%08X\n", ha->host_no,
1160 __func__, readw(isp_port_ctrl(ha))));
1161 DEBUG(printk("scsi%ld: %s: port_status = 0x%08X\n", ha->host_no,
1162 __func__, readw(isp_port_status(ha))));
1163
1164 /* Is Hardware already initialized? */
1165 if ((readw(isp_port_ctrl(ha)) & 0x8000) != 0) {
1166 DEBUG(printk("scsi%ld: %s: Hardware has already been "
1167 "initialized\n", ha->host_no, __func__));
1168
1169 /* Receive firmware boot acknowledgement */
1170 mbox_status = readw(&ha->reg->mailbox[0]);
1171
1172 DEBUG2(printk("scsi%ld: %s: H/W Config complete - mbox[0]= "
1173 "0x%x\n", ha->host_no, __func__, mbox_status));
1174
1175 /* Is firmware already booted? */
1176 if (mbox_status == 0) {
1177 /* F/W not running, must be config by net driver */
1178 config_chip = 1;
1179 soft_reset = 0;
1180 } else {
1181 writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
1182 &ha->reg->ctrl_status);
1183 readl(&ha->reg->ctrl_status);
1184 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1185 if (qla4xxx_get_firmware_state(ha) == QLA_SUCCESS) {
1186 DEBUG2(printk("scsi%ld: %s: Get firmware "
1187 "state -- state = 0x%x\n",
1188 ha->host_no,
1189 __func__, ha->firmware_state));
1190 /* F/W is running */
1191 if (ha->firmware_state &
1192 FW_STATE_CONFIG_WAIT) {
1193 DEBUG2(printk("scsi%ld: %s: Firmware "
1194 "in known state -- "
1195 "config and "
1196 "boot, state = 0x%x\n",
1197 ha->host_no, __func__,
1198 ha->firmware_state));
1199 config_chip = 1;
1200 soft_reset = 0;
1201 }
1202 } else {
1203 DEBUG2(printk("scsi%ld: %s: Firmware in "
1204 "unknown state -- resetting,"
1205 " state = "
1206 "0x%x\n", ha->host_no, __func__,
1207 ha->firmware_state));
1208 }
1209 spin_lock_irqsave(&ha->hardware_lock, flags);
1210 }
1211 } else {
1212 DEBUG(printk("scsi%ld: %s: H/W initialization hasn't been "
1213 "started - resetting\n", ha->host_no, __func__));
1214 }
1215 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1216
1217 DEBUG(printk("scsi%ld: %s: Flags soft_rest=%d, config= %d\n ",
1218 ha->host_no, __func__, soft_reset, config_chip));
1219 if (soft_reset) {
1220 DEBUG(printk("scsi%ld: %s: Issue Soft Reset\n", ha->host_no,
1221 __func__));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301222 status = qla4xxx_soft_reset(ha); /* NOTE: acquires drvr
1223 * lock again, but ok */
David Somayajuluafaf5a22006-09-19 10:28:00 -07001224 if (status == QLA_ERROR) {
1225 DEBUG(printk("scsi%d: %s: Soft Reset failed!\n",
1226 ha->host_no, __func__));
1227 ql4xxx_unlock_drvr(ha);
1228 return QLA_ERROR;
1229 }
1230 config_chip = 1;
1231
Joe Perchesb1c11812008-02-03 17:28:22 +02001232 /* Reset clears the semaphore, so acquire again */
David Somayajuluafaf5a22006-09-19 10:28:00 -07001233 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
1234 return QLA_ERROR;
1235 }
1236
1237 if (config_chip) {
1238 if ((status = qla4xxx_config_nvram(ha)) == QLA_SUCCESS)
1239 status = qla4xxx_start_firmware_from_flash(ha);
1240 }
1241
1242 ql4xxx_unlock_drvr(ha);
1243 if (status == QLA_SUCCESS) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07001244 if (test_and_clear_bit(AF_GET_CRASH_RECORD, &ha->flags))
1245 qla4xxx_get_crash_record(ha);
1246 } else {
1247 DEBUG(printk("scsi%ld: %s: Firmware has NOT started\n",
1248 ha->host_no, __func__));
1249 }
1250 return status;
1251}
1252
1253
1254/**
1255 * qla4xxx_initialize_adapter - initiailizes hba
1256 * @ha: Pointer to host adapter structure.
1257 * @renew_ddb_list: Indicates what to do with the adapter's ddb list
1258 * after adapter recovery has completed.
1259 * 0=preserve ddb list, 1=destroy and rebuild ddb list
1260 *
1261 * This routine parforms all of the steps necessary to initialize the adapter.
1262 *
1263 **/
1264int qla4xxx_initialize_adapter(struct scsi_qla_host *ha,
1265 uint8_t renew_ddb_list)
1266{
1267 int status = QLA_ERROR;
1268 int8_t ip_address[IP_ADDR_LEN] = {0} ;
1269
1270 ha->eeprom_cmd_data = 0;
1271
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301272 ql4_printk(KERN_INFO, ha, "Configuring PCI space...\n");
1273 ha->isp_ops->pci_config(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001274
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301275 ha->isp_ops->disable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001276
1277 /* Initialize the Host adapter request/response queues and firmware */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301278 if (ha->isp_ops->start_firmware(ha) == QLA_ERROR)
David C Somayajulu46235e62007-06-08 17:37:16 -07001279 goto exit_init_hba;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001280
Harish Zunjarrao7ad633c2011-05-17 23:17:11 -07001281 if (qla4xxx_about_firmware(ha) == QLA_ERROR)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301282 goto exit_init_hba;
1283
1284 if (ha->isp_ops->get_sys_info(ha) == QLA_ERROR)
David C Somayajulu46235e62007-06-08 17:37:16 -07001285 goto exit_init_hba;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001286
1287 if (qla4xxx_init_local_data(ha) == QLA_ERROR)
David C Somayajulu46235e62007-06-08 17:37:16 -07001288 goto exit_init_hba;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001289
1290 status = qla4xxx_init_firmware(ha);
1291 if (status == QLA_ERROR)
David C Somayajulu46235e62007-06-08 17:37:16 -07001292 goto exit_init_hba;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001293
1294 /*
1295 * FW is waiting to get an IP address from DHCP server: Skip building
1296 * the ddb_list and wait for DHCP lease acquired aen to come in
1297 * followed by 0x8014 aen" to trigger the tgt discovery process.
1298 */
Vikas Chaudhary2a49a782010-04-28 11:37:07 +05301299 if (ha->firmware_state & FW_STATE_CONFIGURING_IP)
David C Somayajulu46235e62007-06-08 17:37:16 -07001300 goto exit_init_online;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001301
1302 /* Skip device discovery if ip and subnet is zero */
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -05001303 if (memcmp(ha->ip_config.ip_address, ip_address, IP_ADDR_LEN) == 0 ||
1304 memcmp(ha->ip_config.subnet_mask, ip_address, IP_ADDR_LEN) == 0)
David C Somayajulu46235e62007-06-08 17:37:16 -07001305 goto exit_init_online;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001306
1307 if (renew_ddb_list == PRESERVE_DDB_LIST) {
1308 /*
1309 * We want to preserve lun states (i.e. suspended, etc.)
1310 * for recovery initiated by the driver. So just update
1311 * the device states for the existing ddb_list.
1312 */
1313 qla4xxx_reinitialize_ddb_list(ha);
1314 } else if (renew_ddb_list == REBUILD_DDB_LIST) {
1315 /*
1316 * We want to build the ddb_list from scratch during
1317 * driver initialization and recovery initiated by the
1318 * INT_HBA_RESET IOCTL.
1319 */
1320 status = qla4xxx_initialize_ddb_list(ha);
1321 if (status == QLA_ERROR) {
1322 DEBUG2(printk("%s(%ld) Error occurred during build"
1323 "ddb list\n", __func__, ha->host_no));
1324 goto exit_init_hba;
1325 }
1326
1327 }
1328 if (!ha->tot_ddbs) {
1329 DEBUG2(printk("scsi%ld: Failed to initialize devices or none "
1330 "present in Firmware device database\n",
1331 ha->host_no));
1332 }
1333
David C Somayajulu46235e62007-06-08 17:37:16 -07001334exit_init_online:
David C Somayajulu92b72732007-05-23 17:55:40 -07001335 set_bit(AF_ONLINE, &ha->flags);
David C Somayajulu46235e62007-06-08 17:37:16 -07001336exit_init_hba:
Vikas Chaudhary3710c602010-10-06 22:49:08 -07001337 if (is_qla8022(ha) && (status == QLA_ERROR)) {
1338 /* Since interrupts are registered in start_firmware for
1339 * 82xx, release them here if initialize_adapter fails */
1340 qla4xxx_free_irqs(ha);
1341 }
1342
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301343 DEBUG2(printk("scsi%ld: initialize adapter: %s\n", ha->host_no,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001344 status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001345 return status;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001346}
1347
1348/**
1349 * qla4xxx_add_device_dynamically - ddb addition due to an AEN
1350 * @ha: Pointer to host adapter structure.
1351 * @fw_ddb_index: Firmware's device database index
1352 *
1353 * This routine processes adds a device as a result of an 8014h AEN.
1354 **/
1355static void qla4xxx_add_device_dynamically(struct scsi_qla_host *ha,
1356 uint32_t fw_ddb_index)
1357{
1358 struct ddb_entry * ddb_entry;
David C Somayajulu92b72732007-05-23 17:55:40 -07001359 uint32_t new_tgt;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001360
1361 /* First allocate a device structure */
David C Somayajulu92b72732007-05-23 17:55:40 -07001362 ddb_entry = qla4xxx_get_ddb_entry(ha, fw_ddb_index, &new_tgt);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001363 if (ddb_entry == NULL) {
1364 DEBUG2(printk(KERN_WARNING
1365 "scsi%ld: Unable to allocate memory to add "
1366 "fw_ddb_index %d\n", ha->host_no, fw_ddb_index));
1367 return;
1368 }
1369
David C Somayajulu92b72732007-05-23 17:55:40 -07001370 if (!new_tgt && (ddb_entry->fw_ddb_index != fw_ddb_index)) {
1371 /* Target has been bound to a new fw_ddb_index */
1372 qla4xxx_free_ddb(ha, ddb_entry);
1373 ddb_entry = qla4xxx_alloc_ddb(ha, fw_ddb_index);
1374 if (ddb_entry == NULL) {
1375 DEBUG2(printk(KERN_WARNING
1376 "scsi%ld: Unable to allocate memory"
1377 " to add fw_ddb_index %d\n",
1378 ha->host_no, fw_ddb_index));
1379 return;
1380 }
1381 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07001382 if (qla4xxx_update_ddb_entry(ha, ddb_entry, fw_ddb_index) ==
1383 QLA_ERROR) {
1384 ha->fw_ddb_index_map[fw_ddb_index] =
1385 (struct ddb_entry *)INVALID_ENTRY;
1386 DEBUG2(printk(KERN_WARNING
1387 "scsi%ld: failed to add new device at index "
1388 "[%d]\n Unable to retrieve fw ddb entry\n",
1389 ha->host_no, fw_ddb_index));
1390 qla4xxx_free_ddb(ha, ddb_entry);
1391 return;
1392 }
1393
1394 if (qla4xxx_add_sess(ddb_entry)) {
1395 DEBUG2(printk(KERN_WARNING
1396 "scsi%ld: failed to add new device at index "
1397 "[%d]\n Unable to add connection and session\n",
1398 ha->host_no, fw_ddb_index));
1399 qla4xxx_free_ddb(ha, ddb_entry);
1400 }
1401}
1402
1403/**
1404 * qla4xxx_process_ddb_changed - process ddb state change
1405 * @ha - Pointer to host adapter structure.
1406 * @fw_ddb_index - Firmware's device database index
1407 * @state - Device state
1408 *
1409 * This routine processes a Decive Database Changed AEN Event.
1410 **/
Vikas Chaudhary821d6e52010-04-28 11:41:21 +05301411int qla4xxx_process_ddb_changed(struct scsi_qla_host *ha, uint32_t fw_ddb_index,
1412 uint32_t state, uint32_t conn_err)
David Somayajuluafaf5a22006-09-19 10:28:00 -07001413{
1414 struct ddb_entry * ddb_entry;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001415 uint32_t old_fw_ddb_device_state;
1416 int status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001417
1418 /* check for out of range index */
1419 if (fw_ddb_index >= MAX_DDB_ENTRIES)
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001420 goto exit_ddb_event;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001421
1422 /* Get the corresponging ddb entry */
1423 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, fw_ddb_index);
1424 /* Device does not currently exist in our database. */
1425 if (ddb_entry == NULL) {
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001426 ql4_printk(KERN_ERR, ha, "%s: No ddb_entry at FW index [%d]\n",
1427 __func__, fw_ddb_index);
1428 goto exit_ddb_event;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001429 }
1430
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001431 old_fw_ddb_device_state = ddb_entry->fw_ddb_device_state;
1432 DEBUG2(ql4_printk(KERN_INFO, ha,
1433 "%s: DDB - old state = 0x%x, new state = 0x%x for "
1434 "index [%d]\n", __func__,
1435 ddb_entry->fw_ddb_device_state, state, fw_ddb_index));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001436
1437 ddb_entry->fw_ddb_device_state = state;
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301438
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001439 switch (old_fw_ddb_device_state) {
1440 case DDB_DS_LOGIN_IN_PROCESS:
1441 switch (state) {
1442 case DDB_DS_SESSION_ACTIVE:
1443 case DDB_DS_DISCOVERY:
1444 iscsi_conn_login_event(ddb_entry->conn,
1445 ISCSI_CONN_STATE_LOGGED_IN);
1446 qla4xxx_update_session_conn_param(ha, ddb_entry);
1447 status = QLA_SUCCESS;
1448 break;
1449 case DDB_DS_SESSION_FAILED:
1450 case DDB_DS_NO_CONNECTION_ACTIVE:
1451 iscsi_conn_login_event(ddb_entry->conn,
1452 ISCSI_CONN_STATE_FREE);
1453 status = QLA_SUCCESS;
1454 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001455 }
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001456 break;
1457 case DDB_DS_SESSION_ACTIVE:
1458 if (state == DDB_DS_SESSION_FAILED) {
1459 /*
1460 * iscsi_session failure will cause userspace to
1461 * stop the connection which in turn would block the
1462 * iscsi_session and start relogin
1463 */
1464 iscsi_session_failure(ddb_entry->sess->dd_data,
1465 ISCSI_ERR_CONN_FAILED);
1466 status = QLA_SUCCESS;
1467 }
1468 break;
1469 default:
1470 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Unknown Event\n",
1471 __func__));
1472 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001473 }
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001474
1475exit_ddb_event:
1476 return status;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001477}