blob: 05484364010e756da304426a8961f5f22cdd9ac5 [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
David Somayajuluafaf5a22006-09-19 10:28:00 -070014static void ql4xxx_set_mac_number(struct scsi_qla_host *ha)
15{
16 uint32_t value;
17 uint8_t func_number;
18 unsigned long flags;
19
20 /* Get the function number */
21 spin_lock_irqsave(&ha->hardware_lock, flags);
22 value = readw(&ha->reg->ctrl_status);
23 spin_unlock_irqrestore(&ha->hardware_lock, flags);
24
25 func_number = (uint8_t) ((value >> 4) & 0x30);
26 switch (value & ISP_CONTROL_FN_MASK) {
27 case ISP_CONTROL_FN0_SCSI:
28 ha->mac_index = 1;
29 break;
30 case ISP_CONTROL_FN1_SCSI:
31 ha->mac_index = 3;
32 break;
33 default:
34 DEBUG2(printk("scsi%ld: %s: Invalid function number, "
35 "ispControlStatus = 0x%x\n", ha->host_no,
36 __func__, value));
37 break;
38 }
39 DEBUG2(printk("scsi%ld: %s: mac_index %d.\n", ha->host_no, __func__,
40 ha->mac_index));
41}
42
43/**
44 * qla4xxx_free_ddb - deallocate ddb
45 * @ha: pointer to host adapter structure.
46 * @ddb_entry: pointer to device database entry
47 *
Manish Rangankarb3a271a2011-07-25 13:48:53 -050048 * This routine marks a DDB entry INVALID
David Somayajuluafaf5a22006-09-19 10:28:00 -070049 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +053050void qla4xxx_free_ddb(struct scsi_qla_host *ha,
51 struct ddb_entry *ddb_entry)
David Somayajuluafaf5a22006-09-19 10:28:00 -070052{
David Somayajuluafaf5a22006-09-19 10:28:00 -070053 /* Remove device pointer from index mapping arrays */
54 ha->fw_ddb_index_map[ddb_entry->fw_ddb_index] =
55 (struct ddb_entry *) INVALID_ENTRY;
56 ha->tot_ddbs--;
David Somayajuluafaf5a22006-09-19 10:28:00 -070057}
58
59/**
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +053060 * qla4xxx_init_response_q_entries() - Initializes response queue entries.
61 * @ha: HA context
62 *
63 * Beginning of request ring has initialization control block already built
64 * by nvram config routine.
65 **/
66static void qla4xxx_init_response_q_entries(struct scsi_qla_host *ha)
67{
68 uint16_t cnt;
69 struct response *pkt;
70
71 pkt = (struct response *)ha->response_ptr;
72 for (cnt = 0; cnt < RESPONSE_QUEUE_DEPTH; cnt++) {
73 pkt->signature = RESPONSE_PROCESSED;
74 pkt++;
75 }
76}
77
78/**
David Somayajuluafaf5a22006-09-19 10:28:00 -070079 * qla4xxx_init_rings - initialize hw queues
80 * @ha: pointer to host adapter structure.
81 *
82 * This routine initializes the internal queues for the specified adapter.
83 * The QLA4010 requires us to restart the queues at index 0.
84 * The QLA4000 doesn't care, so just default to QLA4010's requirement.
85 **/
86int qla4xxx_init_rings(struct scsi_qla_host *ha)
87{
88 unsigned long flags = 0;
Vikas Chaudharyc0b9d3f2012-02-13 18:30:49 +053089 int i;
David Somayajuluafaf5a22006-09-19 10:28:00 -070090
91 /* Initialize request queue. */
92 spin_lock_irqsave(&ha->hardware_lock, flags);
93 ha->request_out = 0;
94 ha->request_in = 0;
95 ha->request_ptr = &ha->request_ring[ha->request_in];
96 ha->req_q_count = REQUEST_QUEUE_DEPTH;
97
98 /* Initialize response queue. */
99 ha->response_in = 0;
100 ha->response_out = 0;
101 ha->response_ptr = &ha->response_ring[ha->response_out];
102
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530103 if (is_qla8022(ha)) {
104 writel(0,
105 (unsigned long __iomem *)&ha->qla4_8xxx_reg->req_q_out);
106 writel(0,
107 (unsigned long __iomem *)&ha->qla4_8xxx_reg->rsp_q_in);
108 writel(0,
109 (unsigned long __iomem *)&ha->qla4_8xxx_reg->rsp_q_out);
110 } else {
111 /*
112 * Initialize DMA Shadow registers. The firmware is really
113 * supposed to take care of this, but on some uniprocessor
114 * systems, the shadow registers aren't cleared-- causing
115 * the interrupt_handler to think there are responses to be
116 * processed when there aren't.
117 */
118 ha->shadow_regs->req_q_out = __constant_cpu_to_le32(0);
119 ha->shadow_regs->rsp_q_in = __constant_cpu_to_le32(0);
120 wmb();
David Somayajuluafaf5a22006-09-19 10:28:00 -0700121
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530122 writel(0, &ha->reg->req_q_in);
123 writel(0, &ha->reg->rsp_q_out);
124 readl(&ha->reg->rsp_q_out);
125 }
126
127 qla4xxx_init_response_q_entries(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700128
Vikas Chaudharyc0b9d3f2012-02-13 18:30:49 +0530129 /* Initialize mabilbox active array */
130 for (i = 0; i < MAX_MRB; i++)
131 ha->active_mrb_array[i] = NULL;
132
David Somayajuluafaf5a22006-09-19 10:28:00 -0700133 spin_unlock_irqrestore(&ha->hardware_lock, flags);
134
135 return QLA_SUCCESS;
136}
137
138/**
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530139 * qla4xxx_get_sys_info - validate adapter MAC address(es)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700140 * @ha: pointer to host adapter structure.
141 *
142 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530143int qla4xxx_get_sys_info(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700144{
145 struct flash_sys_info *sys_info;
146 dma_addr_t sys_info_dma;
147 int status = QLA_ERROR;
148
149 sys_info = dma_alloc_coherent(&ha->pdev->dev, sizeof(*sys_info),
150 &sys_info_dma, GFP_KERNEL);
151 if (sys_info == NULL) {
152 DEBUG2(printk("scsi%ld: %s: Unable to allocate dma buffer.\n",
153 ha->host_no, __func__));
154
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530155 goto exit_get_sys_info_no_free;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700156 }
157 memset(sys_info, 0, sizeof(*sys_info));
158
159 /* Get flash sys info */
160 if (qla4xxx_get_flash(ha, sys_info_dma, FLASH_OFFSET_SYS_INFO,
161 sizeof(*sys_info)) != QLA_SUCCESS) {
162 DEBUG2(printk("scsi%ld: %s: get_flash FLASH_OFFSET_SYS_INFO "
163 "failed\n", ha->host_no, __func__));
164
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530165 goto exit_get_sys_info;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700166 }
167
168 /* Save M.A.C. address & serial_number */
169 memcpy(ha->my_mac, &sys_info->physAddr[0].address[0],
170 min(sizeof(ha->my_mac),
171 sizeof(sys_info->physAddr[0].address)));
172 memcpy(ha->serial_number, &sys_info->acSerialNumber,
173 min(sizeof(ha->serial_number),
174 sizeof(sys_info->acSerialNumber)));
175
176 status = QLA_SUCCESS;
177
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530178exit_get_sys_info:
David Somayajuluafaf5a22006-09-19 10:28:00 -0700179 dma_free_coherent(&ha->pdev->dev, sizeof(*sys_info), sys_info,
180 sys_info_dma);
181
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530182exit_get_sys_info_no_free:
David Somayajuluafaf5a22006-09-19 10:28:00 -0700183 return status;
184}
185
186/**
187 * qla4xxx_init_local_data - initialize adapter specific local data
188 * @ha: pointer to host adapter structure.
189 *
190 **/
191static int qla4xxx_init_local_data(struct scsi_qla_host *ha)
192{
Uwe Kleine-König421f91d2010-06-11 12:17:00 +0200193 /* Initialize aen queue */
David Somayajuluafaf5a22006-09-19 10:28:00 -0700194 ha->aen_q_count = MAX_AEN_ENTRIES;
195
196 return qla4xxx_get_firmware_status(ha);
197}
198
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530199static uint8_t
200qla4xxx_wait_for_ip_config(struct scsi_qla_host *ha)
201{
202 uint8_t ipv4_wait = 0;
203 uint8_t ipv6_wait = 0;
204 int8_t ip_address[IPv6_ADDR_LEN] = {0} ;
205
206 /* If both IPv4 & IPv6 are enabled, possibly only one
207 * IP address may be acquired, so check to see if we
208 * need to wait for another */
209 if (is_ipv4_enabled(ha) && is_ipv6_enabled(ha)) {
210 if (((ha->addl_fw_state & FW_ADDSTATE_DHCPv4_ENABLED) != 0) &&
211 ((ha->addl_fw_state &
212 FW_ADDSTATE_DHCPv4_LEASE_ACQUIRED) == 0)) {
213 ipv4_wait = 1;
214 }
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500215 if (((ha->ip_config.ipv6_addl_options &
216 IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) != 0) &&
217 ((ha->ip_config.ipv6_link_local_state ==
218 IP_ADDRSTATE_ACQUIRING) ||
219 (ha->ip_config.ipv6_addr0_state ==
220 IP_ADDRSTATE_ACQUIRING) ||
221 (ha->ip_config.ipv6_addr1_state ==
222 IP_ADDRSTATE_ACQUIRING))) {
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530223
224 ipv6_wait = 1;
225
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500226 if ((ha->ip_config.ipv6_link_local_state ==
227 IP_ADDRSTATE_PREFERRED) ||
228 (ha->ip_config.ipv6_addr0_state ==
229 IP_ADDRSTATE_PREFERRED) ||
230 (ha->ip_config.ipv6_addr1_state ==
231 IP_ADDRSTATE_PREFERRED)) {
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530232 DEBUG2(printk(KERN_INFO "scsi%ld: %s: "
233 "Preferred IP configured."
234 " Don't wait!\n", ha->host_no,
235 __func__));
236 ipv6_wait = 0;
237 }
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500238 if (memcmp(&ha->ip_config.ipv6_default_router_addr,
239 ip_address, IPv6_ADDR_LEN) == 0) {
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530240 DEBUG2(printk(KERN_INFO "scsi%ld: %s: "
241 "No Router configured. "
242 "Don't wait!\n", ha->host_no,
243 __func__));
244 ipv6_wait = 0;
245 }
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500246 if ((ha->ip_config.ipv6_default_router_state ==
247 IPV6_RTRSTATE_MANUAL) &&
248 (ha->ip_config.ipv6_link_local_state ==
249 IP_ADDRSTATE_TENTATIVE) &&
250 (memcmp(&ha->ip_config.ipv6_link_local_addr,
251 &ha->ip_config.ipv6_default_router_addr, 4) ==
252 0)) {
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530253 DEBUG2(printk("scsi%ld: %s: LinkLocal Router & "
254 "IP configured. Don't wait!\n",
255 ha->host_no, __func__));
256 ipv6_wait = 0;
257 }
258 }
259 if (ipv4_wait || ipv6_wait) {
260 DEBUG2(printk("scsi%ld: %s: Wait for additional "
261 "IP(s) \"", ha->host_no, __func__));
262 if (ipv4_wait)
263 DEBUG2(printk("IPv4 "));
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500264 if (ha->ip_config.ipv6_link_local_state ==
265 IP_ADDRSTATE_ACQUIRING)
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530266 DEBUG2(printk("IPv6LinkLocal "));
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500267 if (ha->ip_config.ipv6_addr0_state ==
268 IP_ADDRSTATE_ACQUIRING)
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530269 DEBUG2(printk("IPv6Addr0 "));
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500270 if (ha->ip_config.ipv6_addr1_state ==
271 IP_ADDRSTATE_ACQUIRING)
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530272 DEBUG2(printk("IPv6Addr1 "));
273 DEBUG2(printk("\"\n"));
274 }
275 }
276
277 return ipv4_wait|ipv6_wait;
278}
279
David Somayajuluafaf5a22006-09-19 10:28:00 -0700280static int qla4xxx_fw_ready(struct scsi_qla_host *ha)
281{
282 uint32_t timeout_count;
283 int ready = 0;
284
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530285 DEBUG2(ql4_printk(KERN_INFO, ha, "Waiting for Firmware Ready..\n"));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700286 for (timeout_count = ADAPTER_INIT_TOV; timeout_count > 0;
287 timeout_count--) {
288 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
289 qla4xxx_get_dhcp_ip_address(ha);
290
291 /* Get firmware state. */
292 if (qla4xxx_get_firmware_state(ha) != QLA_SUCCESS) {
293 DEBUG2(printk("scsi%ld: %s: unable to get firmware "
294 "state\n", ha->host_no, __func__));
295 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700296 }
297
298 if (ha->firmware_state & FW_STATE_ERROR) {
299 DEBUG2(printk("scsi%ld: %s: an unrecoverable error has"
300 " occurred\n", ha->host_no, __func__));
301 break;
302
303 }
304 if (ha->firmware_state & FW_STATE_CONFIG_WAIT) {
305 /*
306 * The firmware has not yet been issued an Initialize
307 * Firmware command, so issue it now.
308 */
309 if (qla4xxx_initialize_fw_cb(ha) == QLA_ERROR)
310 break;
311
312 /* Go back and test for ready state - no wait. */
313 continue;
314 }
315
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530316 if (ha->firmware_state & FW_STATE_WAIT_AUTOCONNECT) {
317 DEBUG2(printk(KERN_INFO "scsi%ld: %s: fwstate:"
318 "AUTOCONNECT in progress\n",
319 ha->host_no, __func__));
320 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700321
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530322 if (ha->firmware_state & FW_STATE_CONFIGURING_IP) {
323 DEBUG2(printk(KERN_INFO "scsi%ld: %s: fwstate:"
324 " CONFIGURING IP\n",
325 ha->host_no, __func__));
326 /*
327 * Check for link state after 15 secs and if link is
328 * still DOWN then, cable is unplugged. Ignore "DHCP
329 * in Progress/CONFIGURING IP" bit to check if firmware
330 * is in ready state or not after 15 secs.
331 * This is applicable for both 2.x & 3.x firmware
332 */
333 if (timeout_count <= (ADAPTER_INIT_TOV - 15)) {
334 if (ha->addl_fw_state & FW_ADDSTATE_LINK_UP) {
335 DEBUG2(printk(KERN_INFO "scsi%ld: %s:"
336 " LINK UP (Cable plugged)\n",
337 ha->host_no, __func__));
338 } else if (ha->firmware_state &
339 (FW_STATE_CONFIGURING_IP |
340 FW_STATE_READY)) {
341 DEBUG2(printk(KERN_INFO "scsi%ld: %s: "
342 "LINK DOWN (Cable unplugged)\n",
343 ha->host_no, __func__));
344 ha->firmware_state = FW_STATE_READY;
345 }
346 }
347 }
348
349 if (ha->firmware_state == FW_STATE_READY) {
350 /* If DHCP IP Addr is available, retrieve it now. */
351 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR,
352 &ha->dpc_flags))
353 qla4xxx_get_dhcp_ip_address(ha);
354
355 if (!qla4xxx_wait_for_ip_config(ha) ||
356 timeout_count == 1) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530357 DEBUG2(ql4_printk(KERN_INFO, ha,
358 "Firmware Ready..\n"));
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530359 /* The firmware is ready to process SCSI
360 commands. */
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530361 DEBUG2(ql4_printk(KERN_INFO, ha,
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530362 "scsi%ld: %s: MEDIA TYPE"
363 " - %s\n", ha->host_no,
364 __func__, (ha->addl_fw_state &
365 FW_ADDSTATE_OPTICAL_MEDIA)
366 != 0 ? "OPTICAL" : "COPPER"));
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530367 DEBUG2(ql4_printk(KERN_INFO, ha,
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530368 "scsi%ld: %s: DHCPv4 STATE"
369 " Enabled %s\n", ha->host_no,
370 __func__, (ha->addl_fw_state &
371 FW_ADDSTATE_DHCPv4_ENABLED) != 0 ?
372 "YES" : "NO"));
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530373 DEBUG2(ql4_printk(KERN_INFO, ha,
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530374 "scsi%ld: %s: LINK %s\n",
375 ha->host_no, __func__,
376 (ha->addl_fw_state &
377 FW_ADDSTATE_LINK_UP) != 0 ?
378 "UP" : "DOWN"));
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530379 DEBUG2(ql4_printk(KERN_INFO, ha,
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530380 "scsi%ld: %s: iSNS Service "
381 "Started %s\n",
382 ha->host_no, __func__,
383 (ha->addl_fw_state &
384 FW_ADDSTATE_ISNS_SVC_ENABLED) != 0 ?
385 "YES" : "NO"));
386
387 ready = 1;
388 break;
389 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700390 }
391 DEBUG2(printk("scsi%ld: %s: waiting on fw, state=%x:%x - "
392 "seconds expired= %d\n", ha->host_no, __func__,
393 ha->firmware_state, ha->addl_fw_state,
394 timeout_count));
David C Somayajulud9150582006-11-15 17:38:40 -0800395 if (is_qla4032(ha) &&
396 !(ha->addl_fw_state & FW_ADDSTATE_LINK_UP) &&
397 (timeout_count < ADAPTER_INIT_TOV - 5)) {
398 break;
399 }
400
David Somayajuluafaf5a22006-09-19 10:28:00 -0700401 msleep(1000);
402 } /* end of for */
403
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530404 if (timeout_count <= 0)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700405 DEBUG2(printk("scsi%ld: %s: FW Initialization timed out!\n",
406 ha->host_no, __func__));
407
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530408 if (ha->firmware_state & FW_STATE_CONFIGURING_IP) {
409 DEBUG2(printk("scsi%ld: %s: FW initialized, but is reporting "
410 "it's waiting to configure an IP address\n",
411 ha->host_no, __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700412 ready = 1;
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530413 } else if (ha->firmware_state & FW_STATE_WAIT_AUTOCONNECT) {
414 DEBUG2(printk("scsi%ld: %s: FW initialized, but "
415 "auto-discovery still in process\n",
416 ha->host_no, __func__));
Vikas Chaudharyb9663462010-07-10 14:49:19 +0530417 ready = 1;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700418 }
419
420 return ready;
421}
422
423/**
424 * qla4xxx_init_firmware - initializes the firmware.
425 * @ha: pointer to host adapter structure.
426 *
427 **/
428static int qla4xxx_init_firmware(struct scsi_qla_host *ha)
429{
430 int status = QLA_ERROR;
431
Lalit Chandivade2232be02010-07-30 14:38:47 +0530432 if (is_aer_supported(ha) &&
433 test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
434 return status;
435
Lalit Chandivade9d4946f2010-07-30 14:26:31 +0530436 /* For 82xx, stop firmware before initializing because if BIOS
437 * has previously initialized firmware, then driver's initialize
438 * firmware will fail. */
439 if (is_qla8022(ha))
440 qla4_8xxx_stop_firmware(ha);
441
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530442 ql4_printk(KERN_INFO, ha, "Initializing firmware..\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700443 if (qla4xxx_initialize_fw_cb(ha) == QLA_ERROR) {
444 DEBUG2(printk("scsi%ld: %s: Failed to initialize firmware "
445 "control block\n", ha->host_no, __func__));
446 return status;
447 }
448 if (!qla4xxx_fw_ready(ha))
449 return status;
450
David Somayajuluafaf5a22006-09-19 10:28:00 -0700451 return qla4xxx_get_firmware_status(ha);
452}
453
Vikas Chaudhary91ec7ce2011-08-01 03:26:17 -0700454static void qla4xxx_set_model_info(struct scsi_qla_host *ha)
455{
456 uint16_t board_id_string[8];
457 int i;
458 int size = sizeof(ha->nvram->isp4022.boardIdStr);
459 int offset = offsetof(struct eeprom_data, isp4022.boardIdStr) / 2;
460
461 for (i = 0; i < (size / 2) ; i++) {
462 board_id_string[i] = rd_nvram_word(ha, offset);
463 offset += 1;
464 }
465
466 memcpy(ha->model_name, board_id_string, size);
467}
468
David Somayajuluafaf5a22006-09-19 10:28:00 -0700469static int qla4xxx_config_nvram(struct scsi_qla_host *ha)
470{
471 unsigned long flags;
472 union external_hw_config_reg extHwConfig;
473
474 DEBUG2(printk("scsi%ld: %s: Get EEProm parameters \n", ha->host_no,
475 __func__));
476 if (ql4xxx_lock_flash(ha) != QLA_SUCCESS)
Mike Christieb3925512010-02-10 16:51:48 -0600477 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700478 if (ql4xxx_lock_nvram(ha) != QLA_SUCCESS) {
479 ql4xxx_unlock_flash(ha);
Mike Christieb3925512010-02-10 16:51:48 -0600480 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700481 }
482
483 /* Get EEPRom Parameters from NVRAM and validate */
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530484 ql4_printk(KERN_INFO, ha, "Configuring NVRAM ...\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700485 if (qla4xxx_is_nvram_configuration_valid(ha) == QLA_SUCCESS) {
486 spin_lock_irqsave(&ha->hardware_lock, flags);
487 extHwConfig.Asuint32_t =
488 rd_nvram_word(ha, eeprom_ext_hw_conf_offset(ha));
489 spin_unlock_irqrestore(&ha->hardware_lock, flags);
490 } else {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530491 ql4_printk(KERN_WARNING, ha,
492 "scsi%ld: %s: EEProm checksum invalid. "
493 "Please update your EEPROM\n", ha->host_no,
494 __func__);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700495
Mike Christieb3925512010-02-10 16:51:48 -0600496 /* Attempt to set defaults */
David Somayajuluafaf5a22006-09-19 10:28:00 -0700497 if (is_qla4010(ha))
498 extHwConfig.Asuint32_t = 0x1912;
David C Somayajulud9150582006-11-15 17:38:40 -0800499 else if (is_qla4022(ha) | is_qla4032(ha))
David Somayajuluafaf5a22006-09-19 10:28:00 -0700500 extHwConfig.Asuint32_t = 0x0023;
Mike Christieb3925512010-02-10 16:51:48 -0600501 else
502 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700503 }
Vikas Chaudhary91ec7ce2011-08-01 03:26:17 -0700504
505 if (is_qla4022(ha) || is_qla4032(ha))
506 qla4xxx_set_model_info(ha);
507 else
508 strcpy(ha->model_name, "QLA4010");
509
David Somayajuluafaf5a22006-09-19 10:28:00 -0700510 DEBUG(printk("scsi%ld: %s: Setting extHwConfig to 0xFFFF%04x\n",
511 ha->host_no, __func__, extHwConfig.Asuint32_t));
512
513 spin_lock_irqsave(&ha->hardware_lock, flags);
514 writel((0xFFFF << 16) | extHwConfig.Asuint32_t, isp_ext_hw_conf(ha));
515 readl(isp_ext_hw_conf(ha));
516 spin_unlock_irqrestore(&ha->hardware_lock, flags);
517
518 ql4xxx_unlock_nvram(ha);
519 ql4xxx_unlock_flash(ha);
520
Mike Christieb3925512010-02-10 16:51:48 -0600521 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700522}
523
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530524/**
525 * qla4_8xxx_pci_config() - Setup ISP82xx PCI configuration registers.
526 * @ha: HA context
527 */
528void qla4_8xxx_pci_config(struct scsi_qla_host *ha)
529{
530 pci_set_master(ha->pdev);
531}
532
533void qla4xxx_pci_config(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700534{
David C Somayajulu92b72732007-05-23 17:55:40 -0700535 uint16_t w;
David C Somayajulu46235e62007-06-08 17:37:16 -0700536 int status;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700537
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530538 ql4_printk(KERN_INFO, ha, "Configuring PCI space...\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700539
540 pci_set_master(ha->pdev);
David C Somayajulu46235e62007-06-08 17:37:16 -0700541 status = pci_set_mwi(ha->pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700542 /*
543 * We want to respect framework's setting of PCI configuration space
544 * command register and also want to make sure that all bits of
545 * interest to us are properly set in command register.
546 */
547 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
David C Somayajulu92b72732007-05-23 17:55:40 -0700548 w |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700549 w &= ~PCI_COMMAND_INTX_DISABLE;
550 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
551}
552
553static int qla4xxx_start_firmware_from_flash(struct scsi_qla_host *ha)
554{
555 int status = QLA_ERROR;
Vikas Chaudharyc301b022010-04-28 11:40:37 +0530556 unsigned long max_wait_time;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700557 unsigned long flags;
558 uint32_t mbox_status;
559
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530560 ql4_printk(KERN_INFO, ha, "Starting firmware ...\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700561
562 /*
563 * Start firmware from flash ROM
564 *
565 * WORKAROUND: Stuff a non-constant value that the firmware can
566 * use as a seed for a random number generator in MB7 prior to
567 * setting BOOT_ENABLE. Fixes problem where the TCP
568 * connections use the same TCP ports after each reboot,
569 * causing some connections to not get re-established.
570 */
571 DEBUG(printk("scsi%d: %s: Start firmware from flash ROM\n",
572 ha->host_no, __func__));
573
574 spin_lock_irqsave(&ha->hardware_lock, flags);
575 writel(jiffies, &ha->reg->mailbox[7]);
David C Somayajulud9150582006-11-15 17:38:40 -0800576 if (is_qla4022(ha) | is_qla4032(ha))
David Somayajuluafaf5a22006-09-19 10:28:00 -0700577 writel(set_rmask(NVR_WRITE_ENABLE),
578 &ha->reg->u1.isp4022.nvram);
579
David C Somayajulu92b72732007-05-23 17:55:40 -0700580 writel(2, &ha->reg->mailbox[6]);
581 readl(&ha->reg->mailbox[6]);
582
David Somayajuluafaf5a22006-09-19 10:28:00 -0700583 writel(set_rmask(CSR_BOOT_ENABLE), &ha->reg->ctrl_status);
584 readl(&ha->reg->ctrl_status);
585 spin_unlock_irqrestore(&ha->hardware_lock, flags);
586
587 /* Wait for firmware to come UP. */
Vikas Chaudharyc301b022010-04-28 11:40:37 +0530588 DEBUG2(printk(KERN_INFO "scsi%ld: %s: Wait up to %d seconds for "
589 "boot firmware to complete...\n",
590 ha->host_no, __func__, FIRMWARE_UP_TOV));
591 max_wait_time = jiffies + (FIRMWARE_UP_TOV * HZ);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700592 do {
593 uint32_t ctrl_status;
594
595 spin_lock_irqsave(&ha->hardware_lock, flags);
596 ctrl_status = readw(&ha->reg->ctrl_status);
597 mbox_status = readw(&ha->reg->mailbox[0]);
598 spin_unlock_irqrestore(&ha->hardware_lock, flags);
599
600 if (ctrl_status & set_rmask(CSR_SCSI_PROCESSOR_INTR))
601 break;
602 if (mbox_status == MBOX_STS_COMMAND_COMPLETE)
603 break;
604
Vikas Chaudharyc301b022010-04-28 11:40:37 +0530605 DEBUG2(printk(KERN_INFO "scsi%ld: %s: Waiting for boot "
Vikas Chaudharyf581a3f2010-10-06 22:47:48 -0700606 "firmware to complete... ctrl_sts=0x%x, remaining=%ld\n",
607 ha->host_no, __func__, ctrl_status, max_wait_time));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700608
Vikas Chaudharyc301b022010-04-28 11:40:37 +0530609 msleep_interruptible(250);
610 } while (!time_after_eq(jiffies, max_wait_time));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700611
612 if (mbox_status == MBOX_STS_COMMAND_COMPLETE) {
Vikas Chaudharyc301b022010-04-28 11:40:37 +0530613 DEBUG(printk(KERN_INFO "scsi%ld: %s: Firmware has started\n",
David Somayajuluafaf5a22006-09-19 10:28:00 -0700614 ha->host_no, __func__));
615
616 spin_lock_irqsave(&ha->hardware_lock, flags);
617 writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
618 &ha->reg->ctrl_status);
619 readl(&ha->reg->ctrl_status);
620 spin_unlock_irqrestore(&ha->hardware_lock, flags);
621
622 status = QLA_SUCCESS;
623 } else {
624 printk(KERN_INFO "scsi%ld: %s: Boot firmware failed "
625 "- mbox status 0x%x\n", ha->host_no, __func__,
626 mbox_status);
627 status = QLA_ERROR;
628 }
629 return status;
630}
631
David C Somayajulu92b72732007-05-23 17:55:40 -0700632int ql4xxx_lock_drvr_wait(struct scsi_qla_host *a)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700633{
David C Somayajulu92b72732007-05-23 17:55:40 -0700634#define QL4_LOCK_DRVR_WAIT 60
David C Somayajulu477ffb92007-01-22 12:26:11 -0800635#define QL4_LOCK_DRVR_SLEEP 1
David Somayajuluafaf5a22006-09-19 10:28:00 -0700636
637 int drvr_wait = QL4_LOCK_DRVR_WAIT;
638 while (drvr_wait) {
David C Somayajulu92b72732007-05-23 17:55:40 -0700639 if (ql4xxx_lock_drvr(a) == 0) {
David C Somayajulu477ffb92007-01-22 12:26:11 -0800640 ssleep(QL4_LOCK_DRVR_SLEEP);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700641 if (drvr_wait) {
642 DEBUG2(printk("scsi%ld: %s: Waiting for "
David C Somayajulu92b72732007-05-23 17:55:40 -0700643 "Global Init Semaphore(%d)...\n",
644 a->host_no,
David C Somayajulu477ffb92007-01-22 12:26:11 -0800645 __func__, drvr_wait));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700646 }
647 drvr_wait -= QL4_LOCK_DRVR_SLEEP;
648 } else {
649 DEBUG2(printk("scsi%ld: %s: Global Init Semaphore "
David C Somayajulu92b72732007-05-23 17:55:40 -0700650 "acquired\n", a->host_no, __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700651 return QLA_SUCCESS;
652 }
653 }
654 return QLA_ERROR;
655}
656
657/**
658 * qla4xxx_start_firmware - starts qla4xxx firmware
659 * @ha: Pointer to host adapter structure.
660 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200661 * This routine performs the necessary steps to start the firmware for
David Somayajuluafaf5a22006-09-19 10:28:00 -0700662 * the QLA4010 adapter.
663 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530664int qla4xxx_start_firmware(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700665{
666 unsigned long flags = 0;
667 uint32_t mbox_status;
668 int status = QLA_ERROR;
669 int soft_reset = 1;
670 int config_chip = 0;
671
David C Somayajulud9150582006-11-15 17:38:40 -0800672 if (is_qla4022(ha) | is_qla4032(ha))
David Somayajuluafaf5a22006-09-19 10:28:00 -0700673 ql4xxx_set_mac_number(ha);
674
675 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
676 return QLA_ERROR;
677
678 spin_lock_irqsave(&ha->hardware_lock, flags);
679
680 DEBUG2(printk("scsi%ld: %s: port_ctrl = 0x%08X\n", ha->host_no,
681 __func__, readw(isp_port_ctrl(ha))));
682 DEBUG(printk("scsi%ld: %s: port_status = 0x%08X\n", ha->host_no,
683 __func__, readw(isp_port_status(ha))));
684
685 /* Is Hardware already initialized? */
686 if ((readw(isp_port_ctrl(ha)) & 0x8000) != 0) {
687 DEBUG(printk("scsi%ld: %s: Hardware has already been "
688 "initialized\n", ha->host_no, __func__));
689
690 /* Receive firmware boot acknowledgement */
691 mbox_status = readw(&ha->reg->mailbox[0]);
692
693 DEBUG2(printk("scsi%ld: %s: H/W Config complete - mbox[0]= "
694 "0x%x\n", ha->host_no, __func__, mbox_status));
695
696 /* Is firmware already booted? */
697 if (mbox_status == 0) {
698 /* F/W not running, must be config by net driver */
699 config_chip = 1;
700 soft_reset = 0;
701 } else {
702 writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
703 &ha->reg->ctrl_status);
704 readl(&ha->reg->ctrl_status);
Sarang Radke78764992012-01-11 02:44:18 -0800705 writel(set_rmask(CSR_SCSI_COMPLETION_INTR),
706 &ha->reg->ctrl_status);
707 readl(&ha->reg->ctrl_status);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700708 spin_unlock_irqrestore(&ha->hardware_lock, flags);
709 if (qla4xxx_get_firmware_state(ha) == QLA_SUCCESS) {
710 DEBUG2(printk("scsi%ld: %s: Get firmware "
711 "state -- state = 0x%x\n",
712 ha->host_no,
713 __func__, ha->firmware_state));
714 /* F/W is running */
715 if (ha->firmware_state &
716 FW_STATE_CONFIG_WAIT) {
717 DEBUG2(printk("scsi%ld: %s: Firmware "
718 "in known state -- "
719 "config and "
720 "boot, state = 0x%x\n",
721 ha->host_no, __func__,
722 ha->firmware_state));
723 config_chip = 1;
724 soft_reset = 0;
725 }
726 } else {
727 DEBUG2(printk("scsi%ld: %s: Firmware in "
728 "unknown state -- resetting,"
729 " state = "
730 "0x%x\n", ha->host_no, __func__,
731 ha->firmware_state));
732 }
733 spin_lock_irqsave(&ha->hardware_lock, flags);
734 }
735 } else {
736 DEBUG(printk("scsi%ld: %s: H/W initialization hasn't been "
737 "started - resetting\n", ha->host_no, __func__));
738 }
739 spin_unlock_irqrestore(&ha->hardware_lock, flags);
740
741 DEBUG(printk("scsi%ld: %s: Flags soft_rest=%d, config= %d\n ",
742 ha->host_no, __func__, soft_reset, config_chip));
743 if (soft_reset) {
744 DEBUG(printk("scsi%ld: %s: Issue Soft Reset\n", ha->host_no,
745 __func__));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530746 status = qla4xxx_soft_reset(ha); /* NOTE: acquires drvr
747 * lock again, but ok */
David Somayajuluafaf5a22006-09-19 10:28:00 -0700748 if (status == QLA_ERROR) {
749 DEBUG(printk("scsi%d: %s: Soft Reset failed!\n",
750 ha->host_no, __func__));
751 ql4xxx_unlock_drvr(ha);
752 return QLA_ERROR;
753 }
754 config_chip = 1;
755
Joe Perchesb1c11812008-02-03 17:28:22 +0200756 /* Reset clears the semaphore, so acquire again */
David Somayajuluafaf5a22006-09-19 10:28:00 -0700757 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
758 return QLA_ERROR;
759 }
760
761 if (config_chip) {
762 if ((status = qla4xxx_config_nvram(ha)) == QLA_SUCCESS)
763 status = qla4xxx_start_firmware_from_flash(ha);
764 }
765
766 ql4xxx_unlock_drvr(ha);
767 if (status == QLA_SUCCESS) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700768 if (test_and_clear_bit(AF_GET_CRASH_RECORD, &ha->flags))
769 qla4xxx_get_crash_record(ha);
770 } else {
771 DEBUG(printk("scsi%ld: %s: Firmware has NOT started\n",
772 ha->host_no, __func__));
773 }
774 return status;
775}
Lalit Chandivade166dd202011-10-07 16:55:45 -0700776/**
777 * qla4xxx_free_ddb_index - Free DDBs reserved by firmware
778 * @ha: pointer to adapter structure
779 *
780 * Since firmware is not running in autoconnect mode the DDB indices should
781 * be freed so that when login happens from user space there are free DDB
782 * indices available.
783 **/
Mike Christie13483732011-12-01 21:38:41 -0600784void qla4xxx_free_ddb_index(struct scsi_qla_host *ha)
Lalit Chandivade166dd202011-10-07 16:55:45 -0700785{
786 int max_ddbs;
787 int ret;
788 uint32_t idx = 0, next_idx = 0;
789 uint32_t state = 0, conn_err = 0;
790
Mike Christie13483732011-12-01 21:38:41 -0600791 max_ddbs = is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
Lalit Chandivade166dd202011-10-07 16:55:45 -0700792 MAX_DEV_DB_ENTRIES;
793
794 for (idx = 0; idx < max_ddbs; idx = next_idx) {
795 ret = qla4xxx_get_fwddb_entry(ha, idx, NULL, 0, NULL,
796 &next_idx, &state, &conn_err,
797 NULL, NULL);
Tomas Henzle1cd89c2011-12-01 21:38:42 -0600798 if (ret == QLA_ERROR) {
799 next_idx++;
Lalit Chandivade166dd202011-10-07 16:55:45 -0700800 continue;
Tomas Henzle1cd89c2011-12-01 21:38:42 -0600801 }
Lalit Chandivade166dd202011-10-07 16:55:45 -0700802 if (state == DDB_DS_NO_CONNECTION_ACTIVE ||
803 state == DDB_DS_SESSION_FAILED) {
804 DEBUG2(ql4_printk(KERN_INFO, ha,
805 "Freeing DDB index = 0x%x\n", idx));
806 ret = qla4xxx_clear_ddb_entry(ha, idx);
807 if (ret == QLA_ERROR)
808 ql4_printk(KERN_ERR, ha,
809 "Unable to clear DDB index = "
810 "0x%x\n", idx);
811 }
812 if (next_idx == 0)
813 break;
814 }
815}
David Somayajuluafaf5a22006-09-19 10:28:00 -0700816
David Somayajuluafaf5a22006-09-19 10:28:00 -0700817/**
818 * qla4xxx_initialize_adapter - initiailizes hba
819 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -0700820 *
821 * This routine parforms all of the steps necessary to initialize the adapter.
822 *
823 **/
Mike Christie13483732011-12-01 21:38:41 -0600824int qla4xxx_initialize_adapter(struct scsi_qla_host *ha, int is_reset)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700825{
826 int status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700827
828 ha->eeprom_cmd_data = 0;
829
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530830 ql4_printk(KERN_INFO, ha, "Configuring PCI space...\n");
831 ha->isp_ops->pci_config(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700832
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530833 ha->isp_ops->disable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700834
835 /* Initialize the Host adapter request/response queues and firmware */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530836 if (ha->isp_ops->start_firmware(ha) == QLA_ERROR)
David C Somayajulu46235e62007-06-08 17:37:16 -0700837 goto exit_init_hba;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700838
Harish Zunjarrao7ad633c2011-05-17 23:17:11 -0700839 if (qla4xxx_about_firmware(ha) == QLA_ERROR)
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530840 goto exit_init_hba;
841
842 if (ha->isp_ops->get_sys_info(ha) == QLA_ERROR)
David C Somayajulu46235e62007-06-08 17:37:16 -0700843 goto exit_init_hba;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700844
845 if (qla4xxx_init_local_data(ha) == QLA_ERROR)
David C Somayajulu46235e62007-06-08 17:37:16 -0700846 goto exit_init_hba;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700847
848 status = qla4xxx_init_firmware(ha);
849 if (status == QLA_ERROR)
David C Somayajulu46235e62007-06-08 17:37:16 -0700850 goto exit_init_hba;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700851
Mike Christie13483732011-12-01 21:38:41 -0600852 if (is_reset == RESET_ADAPTER)
853 qla4xxx_build_ddb_list(ha, is_reset);
Lalit Chandivade166dd202011-10-07 16:55:45 -0700854
David C Somayajulu92b72732007-05-23 17:55:40 -0700855 set_bit(AF_ONLINE, &ha->flags);
David C Somayajulu46235e62007-06-08 17:37:16 -0700856exit_init_hba:
Vikas Chaudhary3710c602010-10-06 22:49:08 -0700857 if (is_qla8022(ha) && (status == QLA_ERROR)) {
858 /* Since interrupts are registered in start_firmware for
859 * 82xx, release them here if initialize_adapter fails */
860 qla4xxx_free_irqs(ha);
861 }
862
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530863 DEBUG2(printk("scsi%ld: initialize adapter: %s\n", ha->host_no,
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300864 status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700865 return status;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700866}
867
Mike Christie13483732011-12-01 21:38:41 -0600868int qla4xxx_ddb_change(struct scsi_qla_host *ha, uint32_t fw_ddb_index,
869 struct ddb_entry *ddb_entry, uint32_t state)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700870{
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500871 uint32_t old_fw_ddb_device_state;
872 int status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700873
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500874 old_fw_ddb_device_state = ddb_entry->fw_ddb_device_state;
875 DEBUG2(ql4_printk(KERN_INFO, ha,
876 "%s: DDB - old state = 0x%x, new state = 0x%x for "
877 "index [%d]\n", __func__,
878 ddb_entry->fw_ddb_device_state, state, fw_ddb_index));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700879
880 ddb_entry->fw_ddb_device_state = state;
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +0530881
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500882 switch (old_fw_ddb_device_state) {
883 case DDB_DS_LOGIN_IN_PROCESS:
884 switch (state) {
885 case DDB_DS_SESSION_ACTIVE:
886 case DDB_DS_DISCOVERY:
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500887 qla4xxx_update_session_conn_param(ha, ddb_entry);
Manish Rangankar3d948e22012-04-23 22:32:33 -0700888 ddb_entry->unblock_sess(ddb_entry->sess);
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500889 status = QLA_SUCCESS;
890 break;
891 case DDB_DS_SESSION_FAILED:
892 case DDB_DS_NO_CONNECTION_ACTIVE:
893 iscsi_conn_login_event(ddb_entry->conn,
894 ISCSI_CONN_STATE_FREE);
895 status = QLA_SUCCESS;
896 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700897 }
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500898 break;
899 case DDB_DS_SESSION_ACTIVE:
Manish Rangankar3d948e22012-04-23 22:32:33 -0700900 case DDB_DS_DISCOVERY:
Manish Rangankar736cf362011-10-07 16:55:46 -0700901 switch (state) {
902 case DDB_DS_SESSION_FAILED:
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500903 /*
904 * iscsi_session failure will cause userspace to
905 * stop the connection which in turn would block the
906 * iscsi_session and start relogin
907 */
908 iscsi_session_failure(ddb_entry->sess->dd_data,
909 ISCSI_ERR_CONN_FAILED);
910 status = QLA_SUCCESS;
Manish Rangankar736cf362011-10-07 16:55:46 -0700911 break;
912 case DDB_DS_NO_CONNECTION_ACTIVE:
913 clear_bit(fw_ddb_index, ha->ddb_idx_map);
914 status = QLA_SUCCESS;
915 break;
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500916 }
917 break;
Manish Rangankar98270ab2011-10-07 16:55:47 -0700918 case DDB_DS_SESSION_FAILED:
919 switch (state) {
920 case DDB_DS_SESSION_ACTIVE:
921 case DDB_DS_DISCOVERY:
Mike Christie13483732011-12-01 21:38:41 -0600922 ddb_entry->unblock_sess(ddb_entry->sess);
Manish Rangankar98270ab2011-10-07 16:55:47 -0700923 qla4xxx_update_session_conn_param(ha, ddb_entry);
924 status = QLA_SUCCESS;
925 break;
926 case DDB_DS_SESSION_FAILED:
927 iscsi_session_failure(ddb_entry->sess->dd_data,
928 ISCSI_ERR_CONN_FAILED);
929 status = QLA_SUCCESS;
930 break;
931 }
932 break;
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500933 default:
934 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Unknown Event\n",
935 __func__));
936 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700937 }
Mike Christie13483732011-12-01 21:38:41 -0600938 return status;
939}
940
941void qla4xxx_arm_relogin_timer(struct ddb_entry *ddb_entry)
942{
943 /*
944 * This triggers a relogin. After the relogin_timer
945 * expires, the relogin gets scheduled. We must wait a
946 * minimum amount of time since receiving an 0x8014 AEN
947 * with failed device_state or a logout response before
948 * we can issue another relogin.
949 *
950 * Firmware pads this timeout: (time2wait +1).
951 * Driver retry to login should be longer than F/W.
952 * Otherwise F/W will fail
953 * set_ddb() mbx cmd with 0x4005 since it still
954 * counting down its time2wait.
955 */
956 atomic_set(&ddb_entry->relogin_timer, 0);
957 atomic_set(&ddb_entry->retry_relogin_timer,
958 ddb_entry->default_time2wait + 4);
959
960}
961
962int qla4xxx_flash_ddb_change(struct scsi_qla_host *ha, uint32_t fw_ddb_index,
963 struct ddb_entry *ddb_entry, uint32_t state)
964{
965 uint32_t old_fw_ddb_device_state;
966 int status = QLA_ERROR;
967
968 old_fw_ddb_device_state = ddb_entry->fw_ddb_device_state;
969 DEBUG2(ql4_printk(KERN_INFO, ha,
970 "%s: DDB - old state = 0x%x, new state = 0x%x for "
971 "index [%d]\n", __func__,
972 ddb_entry->fw_ddb_device_state, state, fw_ddb_index));
973
974 ddb_entry->fw_ddb_device_state = state;
975
976 switch (old_fw_ddb_device_state) {
977 case DDB_DS_LOGIN_IN_PROCESS:
978 case DDB_DS_NO_CONNECTION_ACTIVE:
979 switch (state) {
980 case DDB_DS_SESSION_ACTIVE:
981 ddb_entry->unblock_sess(ddb_entry->sess);
982 qla4xxx_update_session_conn_fwddb_param(ha, ddb_entry);
983 status = QLA_SUCCESS;
984 break;
985 case DDB_DS_SESSION_FAILED:
986 iscsi_block_session(ddb_entry->sess);
987 if (!test_bit(DF_RELOGIN, &ddb_entry->flags))
988 qla4xxx_arm_relogin_timer(ddb_entry);
989 status = QLA_SUCCESS;
990 break;
991 }
992 break;
993 case DDB_DS_SESSION_ACTIVE:
994 switch (state) {
995 case DDB_DS_SESSION_FAILED:
996 iscsi_block_session(ddb_entry->sess);
997 if (!test_bit(DF_RELOGIN, &ddb_entry->flags))
998 qla4xxx_arm_relogin_timer(ddb_entry);
999 status = QLA_SUCCESS;
1000 break;
1001 }
1002 break;
1003 case DDB_DS_SESSION_FAILED:
1004 switch (state) {
1005 case DDB_DS_SESSION_ACTIVE:
1006 ddb_entry->unblock_sess(ddb_entry->sess);
1007 qla4xxx_update_session_conn_fwddb_param(ha, ddb_entry);
1008 status = QLA_SUCCESS;
1009 break;
1010 case DDB_DS_SESSION_FAILED:
1011 if (!test_bit(DF_RELOGIN, &ddb_entry->flags))
1012 qla4xxx_arm_relogin_timer(ddb_entry);
1013 status = QLA_SUCCESS;
1014 break;
1015 }
1016 break;
1017 default:
1018 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Unknown Event\n",
1019 __func__));
1020 break;
1021 }
1022 return status;
1023}
1024
1025/**
1026 * qla4xxx_process_ddb_changed - process ddb state change
1027 * @ha - Pointer to host adapter structure.
1028 * @fw_ddb_index - Firmware's device database index
1029 * @state - Device state
1030 *
1031 * This routine processes a Decive Database Changed AEN Event.
1032 **/
1033int qla4xxx_process_ddb_changed(struct scsi_qla_host *ha,
1034 uint32_t fw_ddb_index,
1035 uint32_t state, uint32_t conn_err)
1036{
1037 struct ddb_entry *ddb_entry;
1038 int status = QLA_ERROR;
1039
1040 /* check for out of range index */
1041 if (fw_ddb_index >= MAX_DDB_ENTRIES)
1042 goto exit_ddb_event;
1043
1044 /* Get the corresponging ddb entry */
1045 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, fw_ddb_index);
1046 /* Device does not currently exist in our database. */
1047 if (ddb_entry == NULL) {
1048 ql4_printk(KERN_ERR, ha, "%s: No ddb_entry at FW index [%d]\n",
1049 __func__, fw_ddb_index);
1050
1051 if (state == DDB_DS_NO_CONNECTION_ACTIVE)
1052 clear_bit(fw_ddb_index, ha->ddb_idx_map);
1053
1054 goto exit_ddb_event;
1055 }
1056
1057 ddb_entry->ddb_change(ha, fw_ddb_index, ddb_entry, state);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001058
1059exit_ddb_event:
1060 return status;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001061}
Mike Christie13483732011-12-01 21:38:41 -06001062
1063/**
1064 * qla4xxx_login_flash_ddb - Login to target (DDB)
1065 * @cls_session: Pointer to the session to login
1066 *
1067 * This routine logins to the target.
1068 * Issues setddb and conn open mbx
1069 **/
1070void qla4xxx_login_flash_ddb(struct iscsi_cls_session *cls_session)
1071{
1072 struct iscsi_session *sess;
1073 struct ddb_entry *ddb_entry;
1074 struct scsi_qla_host *ha;
1075 struct dev_db_entry *fw_ddb_entry = NULL;
1076 dma_addr_t fw_ddb_dma;
1077 uint32_t mbx_sts = 0;
1078 int ret;
1079
1080 sess = cls_session->dd_data;
1081 ddb_entry = sess->dd_data;
1082 ha = ddb_entry->ha;
1083
1084 if (!test_bit(AF_LINK_UP, &ha->flags))
1085 return;
1086
1087 if (ddb_entry->ddb_type != FLASH_DDB) {
1088 DEBUG2(ql4_printk(KERN_INFO, ha,
1089 "Skipping login to non FLASH DB"));
1090 goto exit_login;
1091 }
1092
1093 fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
1094 &fw_ddb_dma);
1095 if (fw_ddb_entry == NULL) {
1096 DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
1097 goto exit_login;
1098 }
1099
1100 if (ddb_entry->fw_ddb_index == INVALID_ENTRY) {
1101 ret = qla4xxx_get_ddb_index(ha, &ddb_entry->fw_ddb_index);
1102 if (ret == QLA_ERROR)
1103 goto exit_login;
1104
1105 ha->fw_ddb_index_map[ddb_entry->fw_ddb_index] = ddb_entry;
1106 ha->tot_ddbs++;
1107 }
1108
1109 memcpy(fw_ddb_entry, &ddb_entry->fw_ddb_entry,
1110 sizeof(struct dev_db_entry));
1111 ddb_entry->sess->target_id = ddb_entry->fw_ddb_index;
1112
1113 ret = qla4xxx_set_ddb_entry(ha, ddb_entry->fw_ddb_index,
1114 fw_ddb_dma, &mbx_sts);
1115 if (ret == QLA_ERROR) {
1116 DEBUG2(ql4_printk(KERN_ERR, ha, "Set DDB failed\n"));
1117 goto exit_login;
1118 }
1119
1120 ddb_entry->fw_ddb_device_state = DDB_DS_LOGIN_IN_PROCESS;
1121 ret = qla4xxx_conn_open(ha, ddb_entry->fw_ddb_index);
1122 if (ret == QLA_ERROR) {
1123 ql4_printk(KERN_ERR, ha, "%s: Login failed: %s\n", __func__,
1124 sess->targetname);
1125 goto exit_login;
1126 }
1127
1128exit_login:
1129 if (fw_ddb_entry)
1130 dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
1131}
1132