blob: bf36723b84e10cff0a01a3925f5d5bcafaa27fb9 [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
Tej Parkash068237c82012-05-18 04:41:44 -0400280/**
281 * qla4xxx_alloc_fw_dump - Allocate memory for minidump data.
282 * @ha: pointer to host adapter structure.
283 **/
284void qla4xxx_alloc_fw_dump(struct scsi_qla_host *ha)
285{
286 int status;
287 uint32_t capture_debug_level;
288 int hdr_entry_bit, k;
289 void *md_tmp;
290 dma_addr_t md_tmp_dma;
291 struct qla4_8xxx_minidump_template_hdr *md_hdr;
292
293 if (ha->fw_dump) {
294 ql4_printk(KERN_WARNING, ha,
295 "Firmware dump previously allocated.\n");
296 return;
297 }
298
299 status = qla4xxx_req_template_size(ha);
300 if (status != QLA_SUCCESS) {
301 ql4_printk(KERN_INFO, ha,
302 "scsi%ld: Failed to get template size\n",
303 ha->host_no);
304 return;
305 }
306
307 clear_bit(AF_82XX_FW_DUMPED, &ha->flags);
308
309 /* Allocate memory for saving the template */
310 md_tmp = dma_alloc_coherent(&ha->pdev->dev, ha->fw_dump_tmplt_size,
311 &md_tmp_dma, GFP_KERNEL);
312
313 /* Request template */
314 status = qla4xxx_get_minidump_template(ha, md_tmp_dma);
315 if (status != QLA_SUCCESS) {
316 ql4_printk(KERN_INFO, ha,
317 "scsi%ld: Failed to get minidump template\n",
318 ha->host_no);
319 goto alloc_cleanup;
320 }
321
322 md_hdr = (struct qla4_8xxx_minidump_template_hdr *)md_tmp;
323
324 capture_debug_level = md_hdr->capture_debug_level;
325
326 /* Get capture mask based on module loadtime setting. */
327 if (ql4xmdcapmask >= 0x3 && ql4xmdcapmask <= 0x7F)
328 ha->fw_dump_capture_mask = ql4xmdcapmask;
329 else
330 ha->fw_dump_capture_mask = capture_debug_level;
331
332 md_hdr->driver_capture_mask = ha->fw_dump_capture_mask;
333
334 DEBUG2(ql4_printk(KERN_INFO, ha, "Minimum num of entries = %d\n",
335 md_hdr->num_of_entries));
336 DEBUG2(ql4_printk(KERN_INFO, ha, "Dump template size = %d\n",
337 ha->fw_dump_tmplt_size));
338 DEBUG2(ql4_printk(KERN_INFO, ha, "Selected Capture mask =0x%x\n",
339 ha->fw_dump_capture_mask));
340
341 /* Calculate fw_dump_size */
342 for (hdr_entry_bit = 0x2, k = 1; (hdr_entry_bit & 0xFF);
343 hdr_entry_bit <<= 1, k++) {
344 if (hdr_entry_bit & ha->fw_dump_capture_mask)
345 ha->fw_dump_size += md_hdr->capture_size_array[k];
346 }
347
348 /* Total firmware dump size including command header */
349 ha->fw_dump_size += ha->fw_dump_tmplt_size;
350 ha->fw_dump = vmalloc(ha->fw_dump_size);
351 if (!ha->fw_dump)
352 goto alloc_cleanup;
353
354 DEBUG2(ql4_printk(KERN_INFO, ha,
355 "Minidump Tempalate Size = 0x%x KB\n",
356 ha->fw_dump_tmplt_size));
357 DEBUG2(ql4_printk(KERN_INFO, ha,
358 "Total Minidump size = 0x%x KB\n", ha->fw_dump_size));
359
360 memcpy(ha->fw_dump, md_tmp, ha->fw_dump_tmplt_size);
361 ha->fw_dump_tmplt_hdr = ha->fw_dump;
362
363alloc_cleanup:
364 dma_free_coherent(&ha->pdev->dev, ha->fw_dump_tmplt_size,
365 md_tmp, md_tmp_dma);
366}
367
David Somayajuluafaf5a22006-09-19 10:28:00 -0700368static int qla4xxx_fw_ready(struct scsi_qla_host *ha)
369{
370 uint32_t timeout_count;
371 int ready = 0;
372
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530373 DEBUG2(ql4_printk(KERN_INFO, ha, "Waiting for Firmware Ready..\n"));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700374 for (timeout_count = ADAPTER_INIT_TOV; timeout_count > 0;
375 timeout_count--) {
376 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
377 qla4xxx_get_dhcp_ip_address(ha);
378
379 /* Get firmware state. */
380 if (qla4xxx_get_firmware_state(ha) != QLA_SUCCESS) {
381 DEBUG2(printk("scsi%ld: %s: unable to get firmware "
382 "state\n", ha->host_no, __func__));
383 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700384 }
385
386 if (ha->firmware_state & FW_STATE_ERROR) {
387 DEBUG2(printk("scsi%ld: %s: an unrecoverable error has"
388 " occurred\n", ha->host_no, __func__));
389 break;
390
391 }
392 if (ha->firmware_state & FW_STATE_CONFIG_WAIT) {
393 /*
394 * The firmware has not yet been issued an Initialize
395 * Firmware command, so issue it now.
396 */
397 if (qla4xxx_initialize_fw_cb(ha) == QLA_ERROR)
398 break;
399
400 /* Go back and test for ready state - no wait. */
401 continue;
402 }
403
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530404 if (ha->firmware_state & FW_STATE_WAIT_AUTOCONNECT) {
405 DEBUG2(printk(KERN_INFO "scsi%ld: %s: fwstate:"
406 "AUTOCONNECT in progress\n",
407 ha->host_no, __func__));
408 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700409
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530410 if (ha->firmware_state & FW_STATE_CONFIGURING_IP) {
411 DEBUG2(printk(KERN_INFO "scsi%ld: %s: fwstate:"
412 " CONFIGURING IP\n",
413 ha->host_no, __func__));
414 /*
415 * Check for link state after 15 secs and if link is
416 * still DOWN then, cable is unplugged. Ignore "DHCP
417 * in Progress/CONFIGURING IP" bit to check if firmware
418 * is in ready state or not after 15 secs.
419 * This is applicable for both 2.x & 3.x firmware
420 */
421 if (timeout_count <= (ADAPTER_INIT_TOV - 15)) {
422 if (ha->addl_fw_state & FW_ADDSTATE_LINK_UP) {
423 DEBUG2(printk(KERN_INFO "scsi%ld: %s:"
424 " LINK UP (Cable plugged)\n",
425 ha->host_no, __func__));
426 } else if (ha->firmware_state &
427 (FW_STATE_CONFIGURING_IP |
428 FW_STATE_READY)) {
429 DEBUG2(printk(KERN_INFO "scsi%ld: %s: "
430 "LINK DOWN (Cable unplugged)\n",
431 ha->host_no, __func__));
432 ha->firmware_state = FW_STATE_READY;
433 }
434 }
435 }
436
437 if (ha->firmware_state == FW_STATE_READY) {
438 /* If DHCP IP Addr is available, retrieve it now. */
439 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR,
440 &ha->dpc_flags))
441 qla4xxx_get_dhcp_ip_address(ha);
442
443 if (!qla4xxx_wait_for_ip_config(ha) ||
444 timeout_count == 1) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530445 DEBUG2(ql4_printk(KERN_INFO, ha,
446 "Firmware Ready..\n"));
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530447 /* The firmware is ready to process SCSI
448 commands. */
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530449 DEBUG2(ql4_printk(KERN_INFO, ha,
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530450 "scsi%ld: %s: MEDIA TYPE"
451 " - %s\n", ha->host_no,
452 __func__, (ha->addl_fw_state &
453 FW_ADDSTATE_OPTICAL_MEDIA)
454 != 0 ? "OPTICAL" : "COPPER"));
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530455 DEBUG2(ql4_printk(KERN_INFO, ha,
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530456 "scsi%ld: %s: DHCPv4 STATE"
457 " Enabled %s\n", ha->host_no,
458 __func__, (ha->addl_fw_state &
459 FW_ADDSTATE_DHCPv4_ENABLED) != 0 ?
460 "YES" : "NO"));
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530461 DEBUG2(ql4_printk(KERN_INFO, ha,
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530462 "scsi%ld: %s: LINK %s\n",
463 ha->host_no, __func__,
464 (ha->addl_fw_state &
465 FW_ADDSTATE_LINK_UP) != 0 ?
466 "UP" : "DOWN"));
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530467 DEBUG2(ql4_printk(KERN_INFO, ha,
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530468 "scsi%ld: %s: iSNS Service "
469 "Started %s\n",
470 ha->host_no, __func__,
471 (ha->addl_fw_state &
472 FW_ADDSTATE_ISNS_SVC_ENABLED) != 0 ?
473 "YES" : "NO"));
474
475 ready = 1;
476 break;
477 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700478 }
479 DEBUG2(printk("scsi%ld: %s: waiting on fw, state=%x:%x - "
480 "seconds expired= %d\n", ha->host_no, __func__,
481 ha->firmware_state, ha->addl_fw_state,
482 timeout_count));
David C Somayajulud9150582006-11-15 17:38:40 -0800483 if (is_qla4032(ha) &&
484 !(ha->addl_fw_state & FW_ADDSTATE_LINK_UP) &&
485 (timeout_count < ADAPTER_INIT_TOV - 5)) {
486 break;
487 }
488
David Somayajuluafaf5a22006-09-19 10:28:00 -0700489 msleep(1000);
490 } /* end of for */
491
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530492 if (timeout_count <= 0)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700493 DEBUG2(printk("scsi%ld: %s: FW Initialization timed out!\n",
494 ha->host_no, __func__));
495
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530496 if (ha->firmware_state & FW_STATE_CONFIGURING_IP) {
497 DEBUG2(printk("scsi%ld: %s: FW initialized, but is reporting "
498 "it's waiting to configure an IP address\n",
499 ha->host_no, __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700500 ready = 1;
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530501 } else if (ha->firmware_state & FW_STATE_WAIT_AUTOCONNECT) {
502 DEBUG2(printk("scsi%ld: %s: FW initialized, but "
503 "auto-discovery still in process\n",
504 ha->host_no, __func__));
Vikas Chaudharyb9663462010-07-10 14:49:19 +0530505 ready = 1;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700506 }
507
508 return ready;
509}
510
511/**
512 * qla4xxx_init_firmware - initializes the firmware.
513 * @ha: pointer to host adapter structure.
514 *
515 **/
516static int qla4xxx_init_firmware(struct scsi_qla_host *ha)
517{
518 int status = QLA_ERROR;
519
Lalit Chandivade2232be02010-07-30 14:38:47 +0530520 if (is_aer_supported(ha) &&
521 test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
522 return status;
523
Lalit Chandivade9d4946f2010-07-30 14:26:31 +0530524 /* For 82xx, stop firmware before initializing because if BIOS
525 * has previously initialized firmware, then driver's initialize
526 * firmware will fail. */
527 if (is_qla8022(ha))
528 qla4_8xxx_stop_firmware(ha);
529
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530530 ql4_printk(KERN_INFO, ha, "Initializing firmware..\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700531 if (qla4xxx_initialize_fw_cb(ha) == QLA_ERROR) {
532 DEBUG2(printk("scsi%ld: %s: Failed to initialize firmware "
533 "control block\n", ha->host_no, __func__));
534 return status;
535 }
Tej Parkash068237c82012-05-18 04:41:44 -0400536
David Somayajuluafaf5a22006-09-19 10:28:00 -0700537 if (!qla4xxx_fw_ready(ha))
538 return status;
539
Tej Parkash068237c82012-05-18 04:41:44 -0400540 if (is_qla8022(ha) && !test_bit(AF_INIT_DONE, &ha->flags))
541 qla4xxx_alloc_fw_dump(ha);
542
David Somayajuluafaf5a22006-09-19 10:28:00 -0700543 return qla4xxx_get_firmware_status(ha);
544}
545
Vikas Chaudhary91ec7ce2011-08-01 03:26:17 -0700546static void qla4xxx_set_model_info(struct scsi_qla_host *ha)
547{
548 uint16_t board_id_string[8];
549 int i;
550 int size = sizeof(ha->nvram->isp4022.boardIdStr);
551 int offset = offsetof(struct eeprom_data, isp4022.boardIdStr) / 2;
552
553 for (i = 0; i < (size / 2) ; i++) {
554 board_id_string[i] = rd_nvram_word(ha, offset);
555 offset += 1;
556 }
557
558 memcpy(ha->model_name, board_id_string, size);
559}
560
David Somayajuluafaf5a22006-09-19 10:28:00 -0700561static int qla4xxx_config_nvram(struct scsi_qla_host *ha)
562{
563 unsigned long flags;
564 union external_hw_config_reg extHwConfig;
565
566 DEBUG2(printk("scsi%ld: %s: Get EEProm parameters \n", ha->host_no,
567 __func__));
568 if (ql4xxx_lock_flash(ha) != QLA_SUCCESS)
Mike Christieb3925512010-02-10 16:51:48 -0600569 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700570 if (ql4xxx_lock_nvram(ha) != QLA_SUCCESS) {
571 ql4xxx_unlock_flash(ha);
Mike Christieb3925512010-02-10 16:51:48 -0600572 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700573 }
574
575 /* Get EEPRom Parameters from NVRAM and validate */
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530576 ql4_printk(KERN_INFO, ha, "Configuring NVRAM ...\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700577 if (qla4xxx_is_nvram_configuration_valid(ha) == QLA_SUCCESS) {
578 spin_lock_irqsave(&ha->hardware_lock, flags);
579 extHwConfig.Asuint32_t =
580 rd_nvram_word(ha, eeprom_ext_hw_conf_offset(ha));
581 spin_unlock_irqrestore(&ha->hardware_lock, flags);
582 } else {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530583 ql4_printk(KERN_WARNING, ha,
584 "scsi%ld: %s: EEProm checksum invalid. "
585 "Please update your EEPROM\n", ha->host_no,
586 __func__);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700587
Mike Christieb3925512010-02-10 16:51:48 -0600588 /* Attempt to set defaults */
David Somayajuluafaf5a22006-09-19 10:28:00 -0700589 if (is_qla4010(ha))
590 extHwConfig.Asuint32_t = 0x1912;
David C Somayajulud9150582006-11-15 17:38:40 -0800591 else if (is_qla4022(ha) | is_qla4032(ha))
David Somayajuluafaf5a22006-09-19 10:28:00 -0700592 extHwConfig.Asuint32_t = 0x0023;
Mike Christieb3925512010-02-10 16:51:48 -0600593 else
594 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700595 }
Vikas Chaudhary91ec7ce2011-08-01 03:26:17 -0700596
597 if (is_qla4022(ha) || is_qla4032(ha))
598 qla4xxx_set_model_info(ha);
599 else
600 strcpy(ha->model_name, "QLA4010");
601
David Somayajuluafaf5a22006-09-19 10:28:00 -0700602 DEBUG(printk("scsi%ld: %s: Setting extHwConfig to 0xFFFF%04x\n",
603 ha->host_no, __func__, extHwConfig.Asuint32_t));
604
605 spin_lock_irqsave(&ha->hardware_lock, flags);
606 writel((0xFFFF << 16) | extHwConfig.Asuint32_t, isp_ext_hw_conf(ha));
607 readl(isp_ext_hw_conf(ha));
608 spin_unlock_irqrestore(&ha->hardware_lock, flags);
609
610 ql4xxx_unlock_nvram(ha);
611 ql4xxx_unlock_flash(ha);
612
Mike Christieb3925512010-02-10 16:51:48 -0600613 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700614}
615
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530616/**
617 * qla4_8xxx_pci_config() - Setup ISP82xx PCI configuration registers.
618 * @ha: HA context
619 */
620void qla4_8xxx_pci_config(struct scsi_qla_host *ha)
621{
622 pci_set_master(ha->pdev);
623}
624
625void qla4xxx_pci_config(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700626{
David C Somayajulu92b72732007-05-23 17:55:40 -0700627 uint16_t w;
David C Somayajulu46235e62007-06-08 17:37:16 -0700628 int status;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700629
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530630 ql4_printk(KERN_INFO, ha, "Configuring PCI space...\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700631
632 pci_set_master(ha->pdev);
David C Somayajulu46235e62007-06-08 17:37:16 -0700633 status = pci_set_mwi(ha->pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700634 /*
635 * We want to respect framework's setting of PCI configuration space
636 * command register and also want to make sure that all bits of
637 * interest to us are properly set in command register.
638 */
639 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
David C Somayajulu92b72732007-05-23 17:55:40 -0700640 w |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700641 w &= ~PCI_COMMAND_INTX_DISABLE;
642 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
643}
644
645static int qla4xxx_start_firmware_from_flash(struct scsi_qla_host *ha)
646{
647 int status = QLA_ERROR;
Vikas Chaudharyc301b022010-04-28 11:40:37 +0530648 unsigned long max_wait_time;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700649 unsigned long flags;
650 uint32_t mbox_status;
651
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530652 ql4_printk(KERN_INFO, ha, "Starting firmware ...\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700653
654 /*
655 * Start firmware from flash ROM
656 *
657 * WORKAROUND: Stuff a non-constant value that the firmware can
658 * use as a seed for a random number generator in MB7 prior to
659 * setting BOOT_ENABLE. Fixes problem where the TCP
660 * connections use the same TCP ports after each reboot,
661 * causing some connections to not get re-established.
662 */
663 DEBUG(printk("scsi%d: %s: Start firmware from flash ROM\n",
664 ha->host_no, __func__));
665
666 spin_lock_irqsave(&ha->hardware_lock, flags);
667 writel(jiffies, &ha->reg->mailbox[7]);
David C Somayajulud9150582006-11-15 17:38:40 -0800668 if (is_qla4022(ha) | is_qla4032(ha))
David Somayajuluafaf5a22006-09-19 10:28:00 -0700669 writel(set_rmask(NVR_WRITE_ENABLE),
670 &ha->reg->u1.isp4022.nvram);
671
David C Somayajulu92b72732007-05-23 17:55:40 -0700672 writel(2, &ha->reg->mailbox[6]);
673 readl(&ha->reg->mailbox[6]);
674
David Somayajuluafaf5a22006-09-19 10:28:00 -0700675 writel(set_rmask(CSR_BOOT_ENABLE), &ha->reg->ctrl_status);
676 readl(&ha->reg->ctrl_status);
677 spin_unlock_irqrestore(&ha->hardware_lock, flags);
678
679 /* Wait for firmware to come UP. */
Vikas Chaudharyc301b022010-04-28 11:40:37 +0530680 DEBUG2(printk(KERN_INFO "scsi%ld: %s: Wait up to %d seconds for "
681 "boot firmware to complete...\n",
682 ha->host_no, __func__, FIRMWARE_UP_TOV));
683 max_wait_time = jiffies + (FIRMWARE_UP_TOV * HZ);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700684 do {
685 uint32_t ctrl_status;
686
687 spin_lock_irqsave(&ha->hardware_lock, flags);
688 ctrl_status = readw(&ha->reg->ctrl_status);
689 mbox_status = readw(&ha->reg->mailbox[0]);
690 spin_unlock_irqrestore(&ha->hardware_lock, flags);
691
692 if (ctrl_status & set_rmask(CSR_SCSI_PROCESSOR_INTR))
693 break;
694 if (mbox_status == MBOX_STS_COMMAND_COMPLETE)
695 break;
696
Vikas Chaudharyc301b022010-04-28 11:40:37 +0530697 DEBUG2(printk(KERN_INFO "scsi%ld: %s: Waiting for boot "
Vikas Chaudharyf581a3f2010-10-06 22:47:48 -0700698 "firmware to complete... ctrl_sts=0x%x, remaining=%ld\n",
699 ha->host_no, __func__, ctrl_status, max_wait_time));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700700
Vikas Chaudharyc301b022010-04-28 11:40:37 +0530701 msleep_interruptible(250);
702 } while (!time_after_eq(jiffies, max_wait_time));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700703
704 if (mbox_status == MBOX_STS_COMMAND_COMPLETE) {
Vikas Chaudharyc301b022010-04-28 11:40:37 +0530705 DEBUG(printk(KERN_INFO "scsi%ld: %s: Firmware has started\n",
David Somayajuluafaf5a22006-09-19 10:28:00 -0700706 ha->host_no, __func__));
707
708 spin_lock_irqsave(&ha->hardware_lock, flags);
709 writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
710 &ha->reg->ctrl_status);
711 readl(&ha->reg->ctrl_status);
712 spin_unlock_irqrestore(&ha->hardware_lock, flags);
713
714 status = QLA_SUCCESS;
715 } else {
716 printk(KERN_INFO "scsi%ld: %s: Boot firmware failed "
717 "- mbox status 0x%x\n", ha->host_no, __func__,
718 mbox_status);
719 status = QLA_ERROR;
720 }
721 return status;
722}
723
David C Somayajulu92b72732007-05-23 17:55:40 -0700724int ql4xxx_lock_drvr_wait(struct scsi_qla_host *a)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700725{
David C Somayajulu92b72732007-05-23 17:55:40 -0700726#define QL4_LOCK_DRVR_WAIT 60
David C Somayajulu477ffb92007-01-22 12:26:11 -0800727#define QL4_LOCK_DRVR_SLEEP 1
David Somayajuluafaf5a22006-09-19 10:28:00 -0700728
729 int drvr_wait = QL4_LOCK_DRVR_WAIT;
730 while (drvr_wait) {
David C Somayajulu92b72732007-05-23 17:55:40 -0700731 if (ql4xxx_lock_drvr(a) == 0) {
David C Somayajulu477ffb92007-01-22 12:26:11 -0800732 ssleep(QL4_LOCK_DRVR_SLEEP);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700733 if (drvr_wait) {
734 DEBUG2(printk("scsi%ld: %s: Waiting for "
David C Somayajulu92b72732007-05-23 17:55:40 -0700735 "Global Init Semaphore(%d)...\n",
736 a->host_no,
David C Somayajulu477ffb92007-01-22 12:26:11 -0800737 __func__, drvr_wait));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700738 }
739 drvr_wait -= QL4_LOCK_DRVR_SLEEP;
740 } else {
741 DEBUG2(printk("scsi%ld: %s: Global Init Semaphore "
David C Somayajulu92b72732007-05-23 17:55:40 -0700742 "acquired\n", a->host_no, __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700743 return QLA_SUCCESS;
744 }
745 }
746 return QLA_ERROR;
747}
748
749/**
750 * qla4xxx_start_firmware - starts qla4xxx firmware
751 * @ha: Pointer to host adapter structure.
752 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200753 * This routine performs the necessary steps to start the firmware for
David Somayajuluafaf5a22006-09-19 10:28:00 -0700754 * the QLA4010 adapter.
755 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530756int qla4xxx_start_firmware(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700757{
758 unsigned long flags = 0;
759 uint32_t mbox_status;
760 int status = QLA_ERROR;
761 int soft_reset = 1;
762 int config_chip = 0;
763
David C Somayajulud9150582006-11-15 17:38:40 -0800764 if (is_qla4022(ha) | is_qla4032(ha))
David Somayajuluafaf5a22006-09-19 10:28:00 -0700765 ql4xxx_set_mac_number(ha);
766
767 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
768 return QLA_ERROR;
769
770 spin_lock_irqsave(&ha->hardware_lock, flags);
771
772 DEBUG2(printk("scsi%ld: %s: port_ctrl = 0x%08X\n", ha->host_no,
773 __func__, readw(isp_port_ctrl(ha))));
774 DEBUG(printk("scsi%ld: %s: port_status = 0x%08X\n", ha->host_no,
775 __func__, readw(isp_port_status(ha))));
776
777 /* Is Hardware already initialized? */
778 if ((readw(isp_port_ctrl(ha)) & 0x8000) != 0) {
779 DEBUG(printk("scsi%ld: %s: Hardware has already been "
780 "initialized\n", ha->host_no, __func__));
781
782 /* Receive firmware boot acknowledgement */
783 mbox_status = readw(&ha->reg->mailbox[0]);
784
785 DEBUG2(printk("scsi%ld: %s: H/W Config complete - mbox[0]= "
786 "0x%x\n", ha->host_no, __func__, mbox_status));
787
788 /* Is firmware already booted? */
789 if (mbox_status == 0) {
790 /* F/W not running, must be config by net driver */
791 config_chip = 1;
792 soft_reset = 0;
793 } else {
794 writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
795 &ha->reg->ctrl_status);
796 readl(&ha->reg->ctrl_status);
Sarang Radke78764992012-01-11 02:44:18 -0800797 writel(set_rmask(CSR_SCSI_COMPLETION_INTR),
798 &ha->reg->ctrl_status);
799 readl(&ha->reg->ctrl_status);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700800 spin_unlock_irqrestore(&ha->hardware_lock, flags);
801 if (qla4xxx_get_firmware_state(ha) == QLA_SUCCESS) {
802 DEBUG2(printk("scsi%ld: %s: Get firmware "
803 "state -- state = 0x%x\n",
804 ha->host_no,
805 __func__, ha->firmware_state));
806 /* F/W is running */
807 if (ha->firmware_state &
808 FW_STATE_CONFIG_WAIT) {
809 DEBUG2(printk("scsi%ld: %s: Firmware "
810 "in known state -- "
811 "config and "
812 "boot, state = 0x%x\n",
813 ha->host_no, __func__,
814 ha->firmware_state));
815 config_chip = 1;
816 soft_reset = 0;
817 }
818 } else {
819 DEBUG2(printk("scsi%ld: %s: Firmware in "
820 "unknown state -- resetting,"
821 " state = "
822 "0x%x\n", ha->host_no, __func__,
823 ha->firmware_state));
824 }
825 spin_lock_irqsave(&ha->hardware_lock, flags);
826 }
827 } else {
828 DEBUG(printk("scsi%ld: %s: H/W initialization hasn't been "
829 "started - resetting\n", ha->host_no, __func__));
830 }
831 spin_unlock_irqrestore(&ha->hardware_lock, flags);
832
833 DEBUG(printk("scsi%ld: %s: Flags soft_rest=%d, config= %d\n ",
834 ha->host_no, __func__, soft_reset, config_chip));
835 if (soft_reset) {
836 DEBUG(printk("scsi%ld: %s: Issue Soft Reset\n", ha->host_no,
837 __func__));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530838 status = qla4xxx_soft_reset(ha); /* NOTE: acquires drvr
839 * lock again, but ok */
David Somayajuluafaf5a22006-09-19 10:28:00 -0700840 if (status == QLA_ERROR) {
841 DEBUG(printk("scsi%d: %s: Soft Reset failed!\n",
842 ha->host_no, __func__));
843 ql4xxx_unlock_drvr(ha);
844 return QLA_ERROR;
845 }
846 config_chip = 1;
847
Joe Perchesb1c11812008-02-03 17:28:22 +0200848 /* Reset clears the semaphore, so acquire again */
David Somayajuluafaf5a22006-09-19 10:28:00 -0700849 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
850 return QLA_ERROR;
851 }
852
853 if (config_chip) {
854 if ((status = qla4xxx_config_nvram(ha)) == QLA_SUCCESS)
855 status = qla4xxx_start_firmware_from_flash(ha);
856 }
857
858 ql4xxx_unlock_drvr(ha);
859 if (status == QLA_SUCCESS) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700860 if (test_and_clear_bit(AF_GET_CRASH_RECORD, &ha->flags))
861 qla4xxx_get_crash_record(ha);
862 } else {
863 DEBUG(printk("scsi%ld: %s: Firmware has NOT started\n",
864 ha->host_no, __func__));
865 }
866 return status;
867}
Lalit Chandivade166dd202011-10-07 16:55:45 -0700868/**
869 * qla4xxx_free_ddb_index - Free DDBs reserved by firmware
870 * @ha: pointer to adapter structure
871 *
872 * Since firmware is not running in autoconnect mode the DDB indices should
873 * be freed so that when login happens from user space there are free DDB
874 * indices available.
875 **/
Mike Christie13483732011-12-01 21:38:41 -0600876void qla4xxx_free_ddb_index(struct scsi_qla_host *ha)
Lalit Chandivade166dd202011-10-07 16:55:45 -0700877{
878 int max_ddbs;
879 int ret;
880 uint32_t idx = 0, next_idx = 0;
881 uint32_t state = 0, conn_err = 0;
882
Mike Christie13483732011-12-01 21:38:41 -0600883 max_ddbs = is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
Lalit Chandivade166dd202011-10-07 16:55:45 -0700884 MAX_DEV_DB_ENTRIES;
885
886 for (idx = 0; idx < max_ddbs; idx = next_idx) {
887 ret = qla4xxx_get_fwddb_entry(ha, idx, NULL, 0, NULL,
888 &next_idx, &state, &conn_err,
889 NULL, NULL);
Tomas Henzle1cd89c2011-12-01 21:38:42 -0600890 if (ret == QLA_ERROR) {
891 next_idx++;
Lalit Chandivade166dd202011-10-07 16:55:45 -0700892 continue;
Tomas Henzle1cd89c2011-12-01 21:38:42 -0600893 }
Lalit Chandivade166dd202011-10-07 16:55:45 -0700894 if (state == DDB_DS_NO_CONNECTION_ACTIVE ||
895 state == DDB_DS_SESSION_FAILED) {
896 DEBUG2(ql4_printk(KERN_INFO, ha,
897 "Freeing DDB index = 0x%x\n", idx));
898 ret = qla4xxx_clear_ddb_entry(ha, idx);
899 if (ret == QLA_ERROR)
900 ql4_printk(KERN_ERR, ha,
901 "Unable to clear DDB index = "
902 "0x%x\n", idx);
903 }
904 if (next_idx == 0)
905 break;
906 }
907}
David Somayajuluafaf5a22006-09-19 10:28:00 -0700908
David Somayajuluafaf5a22006-09-19 10:28:00 -0700909/**
910 * qla4xxx_initialize_adapter - initiailizes hba
911 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -0700912 *
913 * This routine parforms all of the steps necessary to initialize the adapter.
914 *
915 **/
Mike Christie13483732011-12-01 21:38:41 -0600916int qla4xxx_initialize_adapter(struct scsi_qla_host *ha, int is_reset)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700917{
918 int status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700919
920 ha->eeprom_cmd_data = 0;
921
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530922 ql4_printk(KERN_INFO, ha, "Configuring PCI space...\n");
923 ha->isp_ops->pci_config(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700924
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530925 ha->isp_ops->disable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700926
927 /* Initialize the Host adapter request/response queues and firmware */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530928 if (ha->isp_ops->start_firmware(ha) == QLA_ERROR)
David C Somayajulu46235e62007-06-08 17:37:16 -0700929 goto exit_init_hba;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700930
Harish Zunjarrao7ad633c2011-05-17 23:17:11 -0700931 if (qla4xxx_about_firmware(ha) == QLA_ERROR)
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530932 goto exit_init_hba;
933
934 if (ha->isp_ops->get_sys_info(ha) == QLA_ERROR)
David C Somayajulu46235e62007-06-08 17:37:16 -0700935 goto exit_init_hba;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700936
937 if (qla4xxx_init_local_data(ha) == QLA_ERROR)
David C Somayajulu46235e62007-06-08 17:37:16 -0700938 goto exit_init_hba;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700939
940 status = qla4xxx_init_firmware(ha);
941 if (status == QLA_ERROR)
David C Somayajulu46235e62007-06-08 17:37:16 -0700942 goto exit_init_hba;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700943
Mike Christie13483732011-12-01 21:38:41 -0600944 if (is_reset == RESET_ADAPTER)
945 qla4xxx_build_ddb_list(ha, is_reset);
Lalit Chandivade166dd202011-10-07 16:55:45 -0700946
David C Somayajulu92b72732007-05-23 17:55:40 -0700947 set_bit(AF_ONLINE, &ha->flags);
David C Somayajulu46235e62007-06-08 17:37:16 -0700948exit_init_hba:
Vikas Chaudhary3710c602010-10-06 22:49:08 -0700949 if (is_qla8022(ha) && (status == QLA_ERROR)) {
950 /* Since interrupts are registered in start_firmware for
951 * 82xx, release them here if initialize_adapter fails */
952 qla4xxx_free_irqs(ha);
953 }
954
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530955 DEBUG2(printk("scsi%ld: initialize adapter: %s\n", ha->host_no,
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300956 status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700957 return status;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700958}
959
Mike Christie13483732011-12-01 21:38:41 -0600960int qla4xxx_ddb_change(struct scsi_qla_host *ha, uint32_t fw_ddb_index,
961 struct ddb_entry *ddb_entry, uint32_t state)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700962{
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500963 uint32_t old_fw_ddb_device_state;
964 int status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700965
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500966 old_fw_ddb_device_state = ddb_entry->fw_ddb_device_state;
967 DEBUG2(ql4_printk(KERN_INFO, ha,
968 "%s: DDB - old state = 0x%x, new state = 0x%x for "
969 "index [%d]\n", __func__,
970 ddb_entry->fw_ddb_device_state, state, fw_ddb_index));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700971
972 ddb_entry->fw_ddb_device_state = state;
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +0530973
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500974 switch (old_fw_ddb_device_state) {
975 case DDB_DS_LOGIN_IN_PROCESS:
976 switch (state) {
977 case DDB_DS_SESSION_ACTIVE:
978 case DDB_DS_DISCOVERY:
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500979 qla4xxx_update_session_conn_param(ha, ddb_entry);
Manish Rangankar3d948e22012-04-23 22:32:33 -0700980 ddb_entry->unblock_sess(ddb_entry->sess);
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500981 status = QLA_SUCCESS;
982 break;
983 case DDB_DS_SESSION_FAILED:
984 case DDB_DS_NO_CONNECTION_ACTIVE:
985 iscsi_conn_login_event(ddb_entry->conn,
986 ISCSI_CONN_STATE_FREE);
987 status = QLA_SUCCESS;
988 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700989 }
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500990 break;
991 case DDB_DS_SESSION_ACTIVE:
Manish Rangankar3d948e22012-04-23 22:32:33 -0700992 case DDB_DS_DISCOVERY:
Manish Rangankar736cf362011-10-07 16:55:46 -0700993 switch (state) {
994 case DDB_DS_SESSION_FAILED:
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500995 /*
996 * iscsi_session failure will cause userspace to
997 * stop the connection which in turn would block the
998 * iscsi_session and start relogin
999 */
1000 iscsi_session_failure(ddb_entry->sess->dd_data,
1001 ISCSI_ERR_CONN_FAILED);
1002 status = QLA_SUCCESS;
Manish Rangankar736cf362011-10-07 16:55:46 -07001003 break;
1004 case DDB_DS_NO_CONNECTION_ACTIVE:
1005 clear_bit(fw_ddb_index, ha->ddb_idx_map);
1006 status = QLA_SUCCESS;
1007 break;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001008 }
1009 break;
Manish Rangankar98270ab2011-10-07 16:55:47 -07001010 case DDB_DS_SESSION_FAILED:
1011 switch (state) {
1012 case DDB_DS_SESSION_ACTIVE:
1013 case DDB_DS_DISCOVERY:
Mike Christie13483732011-12-01 21:38:41 -06001014 ddb_entry->unblock_sess(ddb_entry->sess);
Manish Rangankar98270ab2011-10-07 16:55:47 -07001015 qla4xxx_update_session_conn_param(ha, ddb_entry);
1016 status = QLA_SUCCESS;
1017 break;
1018 case DDB_DS_SESSION_FAILED:
1019 iscsi_session_failure(ddb_entry->sess->dd_data,
1020 ISCSI_ERR_CONN_FAILED);
1021 status = QLA_SUCCESS;
1022 break;
1023 }
1024 break;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001025 default:
1026 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Unknown Event\n",
1027 __func__));
1028 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001029 }
Mike Christie13483732011-12-01 21:38:41 -06001030 return status;
1031}
1032
1033void qla4xxx_arm_relogin_timer(struct ddb_entry *ddb_entry)
1034{
1035 /*
1036 * This triggers a relogin. After the relogin_timer
1037 * expires, the relogin gets scheduled. We must wait a
1038 * minimum amount of time since receiving an 0x8014 AEN
1039 * with failed device_state or a logout response before
1040 * we can issue another relogin.
1041 *
1042 * Firmware pads this timeout: (time2wait +1).
1043 * Driver retry to login should be longer than F/W.
1044 * Otherwise F/W will fail
1045 * set_ddb() mbx cmd with 0x4005 since it still
1046 * counting down its time2wait.
1047 */
1048 atomic_set(&ddb_entry->relogin_timer, 0);
1049 atomic_set(&ddb_entry->retry_relogin_timer,
1050 ddb_entry->default_time2wait + 4);
1051
1052}
1053
1054int qla4xxx_flash_ddb_change(struct scsi_qla_host *ha, uint32_t fw_ddb_index,
1055 struct ddb_entry *ddb_entry, uint32_t state)
1056{
1057 uint32_t old_fw_ddb_device_state;
1058 int status = QLA_ERROR;
1059
1060 old_fw_ddb_device_state = ddb_entry->fw_ddb_device_state;
1061 DEBUG2(ql4_printk(KERN_INFO, ha,
1062 "%s: DDB - old state = 0x%x, new state = 0x%x for "
1063 "index [%d]\n", __func__,
1064 ddb_entry->fw_ddb_device_state, state, fw_ddb_index));
1065
1066 ddb_entry->fw_ddb_device_state = state;
1067
1068 switch (old_fw_ddb_device_state) {
1069 case DDB_DS_LOGIN_IN_PROCESS:
1070 case DDB_DS_NO_CONNECTION_ACTIVE:
1071 switch (state) {
1072 case DDB_DS_SESSION_ACTIVE:
1073 ddb_entry->unblock_sess(ddb_entry->sess);
1074 qla4xxx_update_session_conn_fwddb_param(ha, ddb_entry);
1075 status = QLA_SUCCESS;
1076 break;
1077 case DDB_DS_SESSION_FAILED:
1078 iscsi_block_session(ddb_entry->sess);
1079 if (!test_bit(DF_RELOGIN, &ddb_entry->flags))
1080 qla4xxx_arm_relogin_timer(ddb_entry);
1081 status = QLA_SUCCESS;
1082 break;
1083 }
1084 break;
1085 case DDB_DS_SESSION_ACTIVE:
1086 switch (state) {
1087 case DDB_DS_SESSION_FAILED:
1088 iscsi_block_session(ddb_entry->sess);
1089 if (!test_bit(DF_RELOGIN, &ddb_entry->flags))
1090 qla4xxx_arm_relogin_timer(ddb_entry);
1091 status = QLA_SUCCESS;
1092 break;
1093 }
1094 break;
1095 case DDB_DS_SESSION_FAILED:
1096 switch (state) {
1097 case DDB_DS_SESSION_ACTIVE:
1098 ddb_entry->unblock_sess(ddb_entry->sess);
1099 qla4xxx_update_session_conn_fwddb_param(ha, ddb_entry);
1100 status = QLA_SUCCESS;
1101 break;
1102 case DDB_DS_SESSION_FAILED:
1103 if (!test_bit(DF_RELOGIN, &ddb_entry->flags))
1104 qla4xxx_arm_relogin_timer(ddb_entry);
1105 status = QLA_SUCCESS;
1106 break;
1107 }
1108 break;
1109 default:
1110 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Unknown Event\n",
1111 __func__));
1112 break;
1113 }
1114 return status;
1115}
1116
1117/**
1118 * qla4xxx_process_ddb_changed - process ddb state change
1119 * @ha - Pointer to host adapter structure.
1120 * @fw_ddb_index - Firmware's device database index
1121 * @state - Device state
1122 *
1123 * This routine processes a Decive Database Changed AEN Event.
1124 **/
1125int qla4xxx_process_ddb_changed(struct scsi_qla_host *ha,
1126 uint32_t fw_ddb_index,
1127 uint32_t state, uint32_t conn_err)
1128{
1129 struct ddb_entry *ddb_entry;
1130 int status = QLA_ERROR;
1131
1132 /* check for out of range index */
1133 if (fw_ddb_index >= MAX_DDB_ENTRIES)
1134 goto exit_ddb_event;
1135
1136 /* Get the corresponging ddb entry */
1137 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, fw_ddb_index);
1138 /* Device does not currently exist in our database. */
1139 if (ddb_entry == NULL) {
1140 ql4_printk(KERN_ERR, ha, "%s: No ddb_entry at FW index [%d]\n",
1141 __func__, fw_ddb_index);
1142
1143 if (state == DDB_DS_NO_CONNECTION_ACTIVE)
1144 clear_bit(fw_ddb_index, ha->ddb_idx_map);
1145
1146 goto exit_ddb_event;
1147 }
1148
1149 ddb_entry->ddb_change(ha, fw_ddb_index, ddb_entry, state);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001150
1151exit_ddb_event:
1152 return status;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001153}
Mike Christie13483732011-12-01 21:38:41 -06001154
1155/**
1156 * qla4xxx_login_flash_ddb - Login to target (DDB)
1157 * @cls_session: Pointer to the session to login
1158 *
1159 * This routine logins to the target.
1160 * Issues setddb and conn open mbx
1161 **/
1162void qla4xxx_login_flash_ddb(struct iscsi_cls_session *cls_session)
1163{
1164 struct iscsi_session *sess;
1165 struct ddb_entry *ddb_entry;
1166 struct scsi_qla_host *ha;
1167 struct dev_db_entry *fw_ddb_entry = NULL;
1168 dma_addr_t fw_ddb_dma;
1169 uint32_t mbx_sts = 0;
1170 int ret;
1171
1172 sess = cls_session->dd_data;
1173 ddb_entry = sess->dd_data;
1174 ha = ddb_entry->ha;
1175
1176 if (!test_bit(AF_LINK_UP, &ha->flags))
1177 return;
1178
1179 if (ddb_entry->ddb_type != FLASH_DDB) {
1180 DEBUG2(ql4_printk(KERN_INFO, ha,
1181 "Skipping login to non FLASH DB"));
1182 goto exit_login;
1183 }
1184
1185 fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
1186 &fw_ddb_dma);
1187 if (fw_ddb_entry == NULL) {
1188 DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
1189 goto exit_login;
1190 }
1191
1192 if (ddb_entry->fw_ddb_index == INVALID_ENTRY) {
1193 ret = qla4xxx_get_ddb_index(ha, &ddb_entry->fw_ddb_index);
1194 if (ret == QLA_ERROR)
1195 goto exit_login;
1196
1197 ha->fw_ddb_index_map[ddb_entry->fw_ddb_index] = ddb_entry;
1198 ha->tot_ddbs++;
1199 }
1200
1201 memcpy(fw_ddb_entry, &ddb_entry->fw_ddb_entry,
1202 sizeof(struct dev_db_entry));
1203 ddb_entry->sess->target_id = ddb_entry->fw_ddb_index;
1204
1205 ret = qla4xxx_set_ddb_entry(ha, ddb_entry->fw_ddb_index,
1206 fw_ddb_dma, &mbx_sts);
1207 if (ret == QLA_ERROR) {
1208 DEBUG2(ql4_printk(KERN_ERR, ha, "Set DDB failed\n"));
1209 goto exit_login;
1210 }
1211
1212 ddb_entry->fw_ddb_device_state = DDB_DS_LOGIN_IN_PROCESS;
1213 ret = qla4xxx_conn_open(ha, ddb_entry->fw_ddb_index);
1214 if (ret == QLA_ERROR) {
1215 ql4_printk(KERN_ERR, ha, "%s: Login failed: %s\n", __func__,
1216 sess->targetname);
1217 goto exit_login;
1218 }
1219
1220exit_login:
1221 if (fw_ddb_entry)
1222 dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
1223}
1224