blob: 6f12f859b11db787e3b8add134f4d7515cf5ecf4 [file] [log] [blame]
David Somayajuluafaf5a22006-09-19 10:28:00 -07001/*
2 * QLogic iSCSI HBA Driver
Vikas Chaudhary4a4f51e2013-08-16 07:03:04 -04003 * Copyright (c) 2003-2013 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,
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -0400105 (unsigned long __iomem *)&ha->qla4_82xx_reg->req_q_out);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530106 writel(0,
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -0400107 (unsigned long __iomem *)&ha->qla4_82xx_reg->rsp_q_in);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530108 writel(0,
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -0400109 (unsigned long __iomem *)&ha->qla4_82xx_reg->rsp_q_out);
Vikas Chaudharyb37ca412013-08-16 07:03:02 -0400110 } else if (is_qla8032(ha) || is_qla8042(ha)) {
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -0400111 writel(0,
112 (unsigned long __iomem *)&ha->qla4_83xx_reg->req_q_in);
113 writel(0,
114 (unsigned long __iomem *)&ha->qla4_83xx_reg->rsp_q_in);
115 writel(0,
116 (unsigned long __iomem *)&ha->qla4_83xx_reg->rsp_q_out);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530117 } else {
118 /*
119 * Initialize DMA Shadow registers. The firmware is really
120 * supposed to take care of this, but on some uniprocessor
121 * systems, the shadow registers aren't cleared-- causing
122 * the interrupt_handler to think there are responses to be
123 * processed when there aren't.
124 */
125 ha->shadow_regs->req_q_out = __constant_cpu_to_le32(0);
126 ha->shadow_regs->rsp_q_in = __constant_cpu_to_le32(0);
127 wmb();
David Somayajuluafaf5a22006-09-19 10:28:00 -0700128
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530129 writel(0, &ha->reg->req_q_in);
130 writel(0, &ha->reg->rsp_q_out);
131 readl(&ha->reg->rsp_q_out);
132 }
133
134 qla4xxx_init_response_q_entries(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700135
Vikas Chaudhary18e2df92012-06-14 10:13:53 -0400136 /* Initialize mailbox active array */
Vikas Chaudharyc0b9d3f2012-02-13 18:30:49 +0530137 for (i = 0; i < MAX_MRB; i++)
138 ha->active_mrb_array[i] = NULL;
139
David Somayajuluafaf5a22006-09-19 10:28:00 -0700140 spin_unlock_irqrestore(&ha->hardware_lock, flags);
141
142 return QLA_SUCCESS;
143}
144
145/**
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530146 * qla4xxx_get_sys_info - validate adapter MAC address(es)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700147 * @ha: pointer to host adapter structure.
148 *
149 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530150int qla4xxx_get_sys_info(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700151{
152 struct flash_sys_info *sys_info;
153 dma_addr_t sys_info_dma;
154 int status = QLA_ERROR;
155
156 sys_info = dma_alloc_coherent(&ha->pdev->dev, sizeof(*sys_info),
157 &sys_info_dma, GFP_KERNEL);
158 if (sys_info == NULL) {
159 DEBUG2(printk("scsi%ld: %s: Unable to allocate dma buffer.\n",
160 ha->host_no, __func__));
161
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530162 goto exit_get_sys_info_no_free;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700163 }
164 memset(sys_info, 0, sizeof(*sys_info));
165
166 /* Get flash sys info */
167 if (qla4xxx_get_flash(ha, sys_info_dma, FLASH_OFFSET_SYS_INFO,
168 sizeof(*sys_info)) != QLA_SUCCESS) {
169 DEBUG2(printk("scsi%ld: %s: get_flash FLASH_OFFSET_SYS_INFO "
170 "failed\n", ha->host_no, __func__));
171
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530172 goto exit_get_sys_info;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700173 }
174
175 /* Save M.A.C. address & serial_number */
176 memcpy(ha->my_mac, &sys_info->physAddr[0].address[0],
177 min(sizeof(ha->my_mac),
178 sizeof(sys_info->physAddr[0].address)));
179 memcpy(ha->serial_number, &sys_info->acSerialNumber,
180 min(sizeof(ha->serial_number),
181 sizeof(sys_info->acSerialNumber)));
182
183 status = QLA_SUCCESS;
184
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530185exit_get_sys_info:
David Somayajuluafaf5a22006-09-19 10:28:00 -0700186 dma_free_coherent(&ha->pdev->dev, sizeof(*sys_info), sys_info,
187 sys_info_dma);
188
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530189exit_get_sys_info_no_free:
David Somayajuluafaf5a22006-09-19 10:28:00 -0700190 return status;
191}
192
193/**
194 * qla4xxx_init_local_data - initialize adapter specific local data
195 * @ha: pointer to host adapter structure.
196 *
197 **/
Karen Higginsff4108d2013-01-20 23:50:59 -0500198static void qla4xxx_init_local_data(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700199{
Uwe Kleine-König421f91d2010-06-11 12:17:00 +0200200 /* Initialize aen queue */
David Somayajuluafaf5a22006-09-19 10:28:00 -0700201 ha->aen_q_count = MAX_AEN_ENTRIES;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700202}
203
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530204static uint8_t
205qla4xxx_wait_for_ip_config(struct scsi_qla_host *ha)
206{
207 uint8_t ipv4_wait = 0;
208 uint8_t ipv6_wait = 0;
209 int8_t ip_address[IPv6_ADDR_LEN] = {0} ;
210
211 /* If both IPv4 & IPv6 are enabled, possibly only one
212 * IP address may be acquired, so check to see if we
213 * need to wait for another */
214 if (is_ipv4_enabled(ha) && is_ipv6_enabled(ha)) {
215 if (((ha->addl_fw_state & FW_ADDSTATE_DHCPv4_ENABLED) != 0) &&
216 ((ha->addl_fw_state &
217 FW_ADDSTATE_DHCPv4_LEASE_ACQUIRED) == 0)) {
218 ipv4_wait = 1;
219 }
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500220 if (((ha->ip_config.ipv6_addl_options &
221 IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) != 0) &&
222 ((ha->ip_config.ipv6_link_local_state ==
223 IP_ADDRSTATE_ACQUIRING) ||
224 (ha->ip_config.ipv6_addr0_state ==
225 IP_ADDRSTATE_ACQUIRING) ||
226 (ha->ip_config.ipv6_addr1_state ==
227 IP_ADDRSTATE_ACQUIRING))) {
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530228
229 ipv6_wait = 1;
230
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500231 if ((ha->ip_config.ipv6_link_local_state ==
232 IP_ADDRSTATE_PREFERRED) ||
233 (ha->ip_config.ipv6_addr0_state ==
234 IP_ADDRSTATE_PREFERRED) ||
235 (ha->ip_config.ipv6_addr1_state ==
236 IP_ADDRSTATE_PREFERRED)) {
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530237 DEBUG2(printk(KERN_INFO "scsi%ld: %s: "
238 "Preferred IP configured."
239 " Don't wait!\n", ha->host_no,
240 __func__));
241 ipv6_wait = 0;
242 }
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500243 if (memcmp(&ha->ip_config.ipv6_default_router_addr,
244 ip_address, IPv6_ADDR_LEN) == 0) {
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530245 DEBUG2(printk(KERN_INFO "scsi%ld: %s: "
246 "No Router configured. "
247 "Don't wait!\n", ha->host_no,
248 __func__));
249 ipv6_wait = 0;
250 }
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500251 if ((ha->ip_config.ipv6_default_router_state ==
252 IPV6_RTRSTATE_MANUAL) &&
253 (ha->ip_config.ipv6_link_local_state ==
254 IP_ADDRSTATE_TENTATIVE) &&
255 (memcmp(&ha->ip_config.ipv6_link_local_addr,
256 &ha->ip_config.ipv6_default_router_addr, 4) ==
257 0)) {
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530258 DEBUG2(printk("scsi%ld: %s: LinkLocal Router & "
259 "IP configured. Don't wait!\n",
260 ha->host_no, __func__));
261 ipv6_wait = 0;
262 }
263 }
264 if (ipv4_wait || ipv6_wait) {
265 DEBUG2(printk("scsi%ld: %s: Wait for additional "
266 "IP(s) \"", ha->host_no, __func__));
267 if (ipv4_wait)
268 DEBUG2(printk("IPv4 "));
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500269 if (ha->ip_config.ipv6_link_local_state ==
270 IP_ADDRSTATE_ACQUIRING)
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530271 DEBUG2(printk("IPv6LinkLocal "));
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500272 if (ha->ip_config.ipv6_addr0_state ==
273 IP_ADDRSTATE_ACQUIRING)
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530274 DEBUG2(printk("IPv6Addr0 "));
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -0500275 if (ha->ip_config.ipv6_addr1_state ==
276 IP_ADDRSTATE_ACQUIRING)
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530277 DEBUG2(printk("IPv6Addr1 "));
278 DEBUG2(printk("\"\n"));
279 }
280 }
281
282 return ipv4_wait|ipv6_wait;
283}
284
Tej Parkashb4109822014-02-24 22:06:58 -0500285static int qla4_80xx_is_minidump_dma_capable(struct scsi_qla_host *ha,
286 struct qla4_8xxx_minidump_template_hdr *md_hdr)
287{
288 int offset = (is_qla8022(ha)) ? QLA8022_TEMPLATE_CAP_OFFSET :
289 QLA83XX_TEMPLATE_CAP_OFFSET;
290 int rval = 1;
291 uint32_t *cap_offset;
292
293 cap_offset = (uint32_t *)((char *)md_hdr + offset);
294
295 if (!(le32_to_cpu(*cap_offset) & BIT_0)) {
296 ql4_printk(KERN_INFO, ha, "PEX DMA Not supported %d\n",
297 *cap_offset);
298 rval = 0;
299 }
300
301 return rval;
302}
303
Tej Parkash068237c82012-05-18 04:41:44 -0400304/**
305 * qla4xxx_alloc_fw_dump - Allocate memory for minidump data.
306 * @ha: pointer to host adapter structure.
307 **/
308void qla4xxx_alloc_fw_dump(struct scsi_qla_host *ha)
309{
310 int status;
311 uint32_t capture_debug_level;
312 int hdr_entry_bit, k;
313 void *md_tmp;
314 dma_addr_t md_tmp_dma;
315 struct qla4_8xxx_minidump_template_hdr *md_hdr;
Tej Parkashb4109822014-02-24 22:06:58 -0500316 int dma_capable;
Tej Parkash068237c82012-05-18 04:41:44 -0400317
318 if (ha->fw_dump) {
319 ql4_printk(KERN_WARNING, ha,
320 "Firmware dump previously allocated.\n");
321 return;
322 }
323
324 status = qla4xxx_req_template_size(ha);
325 if (status != QLA_SUCCESS) {
326 ql4_printk(KERN_INFO, ha,
327 "scsi%ld: Failed to get template size\n",
328 ha->host_no);
329 return;
330 }
331
332 clear_bit(AF_82XX_FW_DUMPED, &ha->flags);
333
334 /* Allocate memory for saving the template */
335 md_tmp = dma_alloc_coherent(&ha->pdev->dev, ha->fw_dump_tmplt_size,
336 &md_tmp_dma, GFP_KERNEL);
337
338 /* Request template */
339 status = qla4xxx_get_minidump_template(ha, md_tmp_dma);
340 if (status != QLA_SUCCESS) {
341 ql4_printk(KERN_INFO, ha,
342 "scsi%ld: Failed to get minidump template\n",
343 ha->host_no);
344 goto alloc_cleanup;
345 }
346
347 md_hdr = (struct qla4_8xxx_minidump_template_hdr *)md_tmp;
348
Tej Parkashb4109822014-02-24 22:06:58 -0500349 dma_capable = qla4_80xx_is_minidump_dma_capable(ha, md_hdr);
350
Tej Parkash068237c82012-05-18 04:41:44 -0400351 capture_debug_level = md_hdr->capture_debug_level;
352
353 /* Get capture mask based on module loadtime setting. */
Tej Parkashb4109822014-02-24 22:06:58 -0500354 if ((ql4xmdcapmask >= 0x3 && ql4xmdcapmask <= 0x7F) ||
355 (ql4xmdcapmask == 0xFF && dma_capable)) {
Tej Parkash068237c82012-05-18 04:41:44 -0400356 ha->fw_dump_capture_mask = ql4xmdcapmask;
Tej Parkashb4109822014-02-24 22:06:58 -0500357 } else {
358 if (ql4xmdcapmask == 0xFF)
359 ql4_printk(KERN_INFO, ha, "Falling back to default capture mask, as PEX DMA is not supported\n");
Tej Parkash068237c82012-05-18 04:41:44 -0400360 ha->fw_dump_capture_mask = capture_debug_level;
Tej Parkashb4109822014-02-24 22:06:58 -0500361 }
Tej Parkash068237c82012-05-18 04:41:44 -0400362
363 md_hdr->driver_capture_mask = ha->fw_dump_capture_mask;
364
365 DEBUG2(ql4_printk(KERN_INFO, ha, "Minimum num of entries = %d\n",
366 md_hdr->num_of_entries));
367 DEBUG2(ql4_printk(KERN_INFO, ha, "Dump template size = %d\n",
368 ha->fw_dump_tmplt_size));
369 DEBUG2(ql4_printk(KERN_INFO, ha, "Selected Capture mask =0x%x\n",
370 ha->fw_dump_capture_mask));
371
372 /* Calculate fw_dump_size */
373 for (hdr_entry_bit = 0x2, k = 1; (hdr_entry_bit & 0xFF);
374 hdr_entry_bit <<= 1, k++) {
375 if (hdr_entry_bit & ha->fw_dump_capture_mask)
376 ha->fw_dump_size += md_hdr->capture_size_array[k];
377 }
378
379 /* Total firmware dump size including command header */
380 ha->fw_dump_size += ha->fw_dump_tmplt_size;
381 ha->fw_dump = vmalloc(ha->fw_dump_size);
382 if (!ha->fw_dump)
383 goto alloc_cleanup;
384
385 DEBUG2(ql4_printk(KERN_INFO, ha,
386 "Minidump Tempalate Size = 0x%x KB\n",
387 ha->fw_dump_tmplt_size));
388 DEBUG2(ql4_printk(KERN_INFO, ha,
389 "Total Minidump size = 0x%x KB\n", ha->fw_dump_size));
390
391 memcpy(ha->fw_dump, md_tmp, ha->fw_dump_tmplt_size);
392 ha->fw_dump_tmplt_hdr = ha->fw_dump;
393
394alloc_cleanup:
395 dma_free_coherent(&ha->pdev->dev, ha->fw_dump_tmplt_size,
396 md_tmp, md_tmp_dma);
397}
398
David Somayajuluafaf5a22006-09-19 10:28:00 -0700399static int qla4xxx_fw_ready(struct scsi_qla_host *ha)
400{
401 uint32_t timeout_count;
402 int ready = 0;
403
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530404 DEBUG2(ql4_printk(KERN_INFO, ha, "Waiting for Firmware Ready..\n"));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700405 for (timeout_count = ADAPTER_INIT_TOV; timeout_count > 0;
406 timeout_count--) {
407 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
408 qla4xxx_get_dhcp_ip_address(ha);
409
410 /* Get firmware state. */
411 if (qla4xxx_get_firmware_state(ha) != QLA_SUCCESS) {
412 DEBUG2(printk("scsi%ld: %s: unable to get firmware "
413 "state\n", ha->host_no, __func__));
414 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700415 }
416
417 if (ha->firmware_state & FW_STATE_ERROR) {
418 DEBUG2(printk("scsi%ld: %s: an unrecoverable error has"
419 " occurred\n", ha->host_no, __func__));
420 break;
421
422 }
423 if (ha->firmware_state & FW_STATE_CONFIG_WAIT) {
424 /*
425 * The firmware has not yet been issued an Initialize
426 * Firmware command, so issue it now.
427 */
428 if (qla4xxx_initialize_fw_cb(ha) == QLA_ERROR)
429 break;
430
431 /* Go back and test for ready state - no wait. */
432 continue;
433 }
434
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530435 if (ha->firmware_state & FW_STATE_WAIT_AUTOCONNECT) {
436 DEBUG2(printk(KERN_INFO "scsi%ld: %s: fwstate:"
437 "AUTOCONNECT in progress\n",
438 ha->host_no, __func__));
439 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700440
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530441 if (ha->firmware_state & FW_STATE_CONFIGURING_IP) {
442 DEBUG2(printk(KERN_INFO "scsi%ld: %s: fwstate:"
443 " CONFIGURING IP\n",
444 ha->host_no, __func__));
445 /*
446 * Check for link state after 15 secs and if link is
447 * still DOWN then, cable is unplugged. Ignore "DHCP
448 * in Progress/CONFIGURING IP" bit to check if firmware
449 * is in ready state or not after 15 secs.
450 * This is applicable for both 2.x & 3.x firmware
451 */
452 if (timeout_count <= (ADAPTER_INIT_TOV - 15)) {
453 if (ha->addl_fw_state & FW_ADDSTATE_LINK_UP) {
454 DEBUG2(printk(KERN_INFO "scsi%ld: %s:"
455 " LINK UP (Cable plugged)\n",
456 ha->host_no, __func__));
457 } else if (ha->firmware_state &
458 (FW_STATE_CONFIGURING_IP |
459 FW_STATE_READY)) {
460 DEBUG2(printk(KERN_INFO "scsi%ld: %s: "
461 "LINK DOWN (Cable unplugged)\n",
462 ha->host_no, __func__));
463 ha->firmware_state = FW_STATE_READY;
464 }
465 }
466 }
467
468 if (ha->firmware_state == FW_STATE_READY) {
469 /* If DHCP IP Addr is available, retrieve it now. */
470 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR,
471 &ha->dpc_flags))
472 qla4xxx_get_dhcp_ip_address(ha);
473
474 if (!qla4xxx_wait_for_ip_config(ha) ||
475 timeout_count == 1) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530476 DEBUG2(ql4_printk(KERN_INFO, ha,
477 "Firmware Ready..\n"));
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530478 /* The firmware is ready to process SCSI
479 commands. */
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530480 DEBUG2(ql4_printk(KERN_INFO, ha,
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530481 "scsi%ld: %s: MEDIA TYPE"
482 " - %s\n", ha->host_no,
483 __func__, (ha->addl_fw_state &
484 FW_ADDSTATE_OPTICAL_MEDIA)
485 != 0 ? "OPTICAL" : "COPPER"));
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530486 DEBUG2(ql4_printk(KERN_INFO, ha,
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530487 "scsi%ld: %s: DHCPv4 STATE"
488 " Enabled %s\n", ha->host_no,
489 __func__, (ha->addl_fw_state &
490 FW_ADDSTATE_DHCPv4_ENABLED) != 0 ?
491 "YES" : "NO"));
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530492 DEBUG2(ql4_printk(KERN_INFO, ha,
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530493 "scsi%ld: %s: LINK %s\n",
494 ha->host_no, __func__,
495 (ha->addl_fw_state &
496 FW_ADDSTATE_LINK_UP) != 0 ?
497 "UP" : "DOWN"));
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530498 DEBUG2(ql4_printk(KERN_INFO, ha,
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530499 "scsi%ld: %s: iSNS Service "
500 "Started %s\n",
501 ha->host_no, __func__,
502 (ha->addl_fw_state &
503 FW_ADDSTATE_ISNS_SVC_ENABLED) != 0 ?
504 "YES" : "NO"));
505
506 ready = 1;
507 break;
508 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700509 }
510 DEBUG2(printk("scsi%ld: %s: waiting on fw, state=%x:%x - "
511 "seconds expired= %d\n", ha->host_no, __func__,
512 ha->firmware_state, ha->addl_fw_state,
513 timeout_count));
David C Somayajulud9150582006-11-15 17:38:40 -0800514 if (is_qla4032(ha) &&
515 !(ha->addl_fw_state & FW_ADDSTATE_LINK_UP) &&
516 (timeout_count < ADAPTER_INIT_TOV - 5)) {
517 break;
518 }
519
David Somayajuluafaf5a22006-09-19 10:28:00 -0700520 msleep(1000);
521 } /* end of for */
522
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530523 if (timeout_count <= 0)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700524 DEBUG2(printk("scsi%ld: %s: FW Initialization timed out!\n",
525 ha->host_no, __func__));
526
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530527 if (ha->firmware_state & FW_STATE_CONFIGURING_IP) {
528 DEBUG2(printk("scsi%ld: %s: FW initialized, but is reporting "
529 "it's waiting to configure an IP address\n",
530 ha->host_no, __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700531 ready = 1;
Vikas Chaudhary2a49a782010-04-28 11:37:07 +0530532 } else if (ha->firmware_state & FW_STATE_WAIT_AUTOCONNECT) {
533 DEBUG2(printk("scsi%ld: %s: FW initialized, but "
534 "auto-discovery still in process\n",
535 ha->host_no, __func__));
Vikas Chaudharyb9663462010-07-10 14:49:19 +0530536 ready = 1;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700537 }
538
539 return ready;
540}
541
542/**
543 * qla4xxx_init_firmware - initializes the firmware.
544 * @ha: pointer to host adapter structure.
545 *
546 **/
547static int qla4xxx_init_firmware(struct scsi_qla_host *ha)
548{
549 int status = QLA_ERROR;
550
Lalit Chandivade2232be02010-07-30 14:38:47 +0530551 if (is_aer_supported(ha) &&
552 test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
553 return status;
554
Lalit Chandivade9d4946f2010-07-30 14:26:31 +0530555 /* For 82xx, stop firmware before initializing because if BIOS
556 * has previously initialized firmware, then driver's initialize
557 * firmware will fail. */
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -0400558 if (is_qla80XX(ha))
Lalit Chandivade9d4946f2010-07-30 14:26:31 +0530559 qla4_8xxx_stop_firmware(ha);
560
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530561 ql4_printk(KERN_INFO, ha, "Initializing firmware..\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700562 if (qla4xxx_initialize_fw_cb(ha) == QLA_ERROR) {
563 DEBUG2(printk("scsi%ld: %s: Failed to initialize firmware "
564 "control block\n", ha->host_no, __func__));
565 return status;
566 }
Tej Parkash068237c82012-05-18 04:41:44 -0400567
David Somayajuluafaf5a22006-09-19 10:28:00 -0700568 if (!qla4xxx_fw_ready(ha))
569 return status;
570
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -0400571 if (is_qla80XX(ha) && !test_bit(AF_INIT_DONE, &ha->flags))
Tej Parkash068237c82012-05-18 04:41:44 -0400572 qla4xxx_alloc_fw_dump(ha);
573
David Somayajuluafaf5a22006-09-19 10:28:00 -0700574 return qla4xxx_get_firmware_status(ha);
575}
576
Vikas Chaudhary91ec7ce2011-08-01 03:26:17 -0700577static void qla4xxx_set_model_info(struct scsi_qla_host *ha)
578{
579 uint16_t board_id_string[8];
580 int i;
581 int size = sizeof(ha->nvram->isp4022.boardIdStr);
582 int offset = offsetof(struct eeprom_data, isp4022.boardIdStr) / 2;
583
584 for (i = 0; i < (size / 2) ; i++) {
585 board_id_string[i] = rd_nvram_word(ha, offset);
586 offset += 1;
587 }
588
589 memcpy(ha->model_name, board_id_string, size);
590}
591
David Somayajuluafaf5a22006-09-19 10:28:00 -0700592static int qla4xxx_config_nvram(struct scsi_qla_host *ha)
593{
594 unsigned long flags;
595 union external_hw_config_reg extHwConfig;
596
597 DEBUG2(printk("scsi%ld: %s: Get EEProm parameters \n", ha->host_no,
598 __func__));
599 if (ql4xxx_lock_flash(ha) != QLA_SUCCESS)
Mike Christieb3925512010-02-10 16:51:48 -0600600 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700601 if (ql4xxx_lock_nvram(ha) != QLA_SUCCESS) {
602 ql4xxx_unlock_flash(ha);
Mike Christieb3925512010-02-10 16:51:48 -0600603 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700604 }
605
606 /* Get EEPRom Parameters from NVRAM and validate */
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530607 ql4_printk(KERN_INFO, ha, "Configuring NVRAM ...\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700608 if (qla4xxx_is_nvram_configuration_valid(ha) == QLA_SUCCESS) {
609 spin_lock_irqsave(&ha->hardware_lock, flags);
610 extHwConfig.Asuint32_t =
611 rd_nvram_word(ha, eeprom_ext_hw_conf_offset(ha));
612 spin_unlock_irqrestore(&ha->hardware_lock, flags);
613 } else {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530614 ql4_printk(KERN_WARNING, ha,
615 "scsi%ld: %s: EEProm checksum invalid. "
616 "Please update your EEPROM\n", ha->host_no,
617 __func__);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700618
Mike Christieb3925512010-02-10 16:51:48 -0600619 /* Attempt to set defaults */
David Somayajuluafaf5a22006-09-19 10:28:00 -0700620 if (is_qla4010(ha))
621 extHwConfig.Asuint32_t = 0x1912;
David C Somayajulud9150582006-11-15 17:38:40 -0800622 else if (is_qla4022(ha) | is_qla4032(ha))
David Somayajuluafaf5a22006-09-19 10:28:00 -0700623 extHwConfig.Asuint32_t = 0x0023;
Mike Christieb3925512010-02-10 16:51:48 -0600624 else
625 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700626 }
Vikas Chaudhary91ec7ce2011-08-01 03:26:17 -0700627
628 if (is_qla4022(ha) || is_qla4032(ha))
629 qla4xxx_set_model_info(ha);
630 else
631 strcpy(ha->model_name, "QLA4010");
632
David Somayajuluafaf5a22006-09-19 10:28:00 -0700633 DEBUG(printk("scsi%ld: %s: Setting extHwConfig to 0xFFFF%04x\n",
634 ha->host_no, __func__, extHwConfig.Asuint32_t));
635
636 spin_lock_irqsave(&ha->hardware_lock, flags);
637 writel((0xFFFF << 16) | extHwConfig.Asuint32_t, isp_ext_hw_conf(ha));
638 readl(isp_ext_hw_conf(ha));
639 spin_unlock_irqrestore(&ha->hardware_lock, flags);
640
641 ql4xxx_unlock_nvram(ha);
642 ql4xxx_unlock_flash(ha);
643
Mike Christieb3925512010-02-10 16:51:48 -0600644 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700645}
646
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530647/**
648 * qla4_8xxx_pci_config() - Setup ISP82xx PCI configuration registers.
649 * @ha: HA context
650 */
651void qla4_8xxx_pci_config(struct scsi_qla_host *ha)
652{
653 pci_set_master(ha->pdev);
654}
655
656void qla4xxx_pci_config(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700657{
David C Somayajulu92b72732007-05-23 17:55:40 -0700658 uint16_t w;
David C Somayajulu46235e62007-06-08 17:37:16 -0700659 int status;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700660
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530661 ql4_printk(KERN_INFO, ha, "Configuring PCI space...\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700662
663 pci_set_master(ha->pdev);
David C Somayajulu46235e62007-06-08 17:37:16 -0700664 status = pci_set_mwi(ha->pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700665 /*
666 * We want to respect framework's setting of PCI configuration space
667 * command register and also want to make sure that all bits of
668 * interest to us are properly set in command register.
669 */
670 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
David C Somayajulu92b72732007-05-23 17:55:40 -0700671 w |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700672 w &= ~PCI_COMMAND_INTX_DISABLE;
673 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
674}
675
676static int qla4xxx_start_firmware_from_flash(struct scsi_qla_host *ha)
677{
678 int status = QLA_ERROR;
Vikas Chaudharyc301b022010-04-28 11:40:37 +0530679 unsigned long max_wait_time;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700680 unsigned long flags;
681 uint32_t mbox_status;
682
Vikas Chaudharyc2660df2010-07-10 14:51:02 +0530683 ql4_printk(KERN_INFO, ha, "Starting firmware ...\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -0700684
685 /*
686 * Start firmware from flash ROM
687 *
688 * WORKAROUND: Stuff a non-constant value that the firmware can
689 * use as a seed for a random number generator in MB7 prior to
690 * setting BOOT_ENABLE. Fixes problem where the TCP
691 * connections use the same TCP ports after each reboot,
692 * causing some connections to not get re-established.
693 */
694 DEBUG(printk("scsi%d: %s: Start firmware from flash ROM\n",
695 ha->host_no, __func__));
696
697 spin_lock_irqsave(&ha->hardware_lock, flags);
698 writel(jiffies, &ha->reg->mailbox[7]);
David C Somayajulud9150582006-11-15 17:38:40 -0800699 if (is_qla4022(ha) | is_qla4032(ha))
David Somayajuluafaf5a22006-09-19 10:28:00 -0700700 writel(set_rmask(NVR_WRITE_ENABLE),
701 &ha->reg->u1.isp4022.nvram);
702
David C Somayajulu92b72732007-05-23 17:55:40 -0700703 writel(2, &ha->reg->mailbox[6]);
704 readl(&ha->reg->mailbox[6]);
705
David Somayajuluafaf5a22006-09-19 10:28:00 -0700706 writel(set_rmask(CSR_BOOT_ENABLE), &ha->reg->ctrl_status);
707 readl(&ha->reg->ctrl_status);
708 spin_unlock_irqrestore(&ha->hardware_lock, flags);
709
710 /* Wait for firmware to come UP. */
Vikas Chaudharyc301b022010-04-28 11:40:37 +0530711 DEBUG2(printk(KERN_INFO "scsi%ld: %s: Wait up to %d seconds for "
712 "boot firmware to complete...\n",
713 ha->host_no, __func__, FIRMWARE_UP_TOV));
714 max_wait_time = jiffies + (FIRMWARE_UP_TOV * HZ);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700715 do {
716 uint32_t ctrl_status;
717
718 spin_lock_irqsave(&ha->hardware_lock, flags);
719 ctrl_status = readw(&ha->reg->ctrl_status);
720 mbox_status = readw(&ha->reg->mailbox[0]);
721 spin_unlock_irqrestore(&ha->hardware_lock, flags);
722
723 if (ctrl_status & set_rmask(CSR_SCSI_PROCESSOR_INTR))
724 break;
725 if (mbox_status == MBOX_STS_COMMAND_COMPLETE)
726 break;
727
Vikas Chaudharyc301b022010-04-28 11:40:37 +0530728 DEBUG2(printk(KERN_INFO "scsi%ld: %s: Waiting for boot "
Vikas Chaudharyf581a3f2010-10-06 22:47:48 -0700729 "firmware to complete... ctrl_sts=0x%x, remaining=%ld\n",
730 ha->host_no, __func__, ctrl_status, max_wait_time));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700731
Vikas Chaudharyc301b022010-04-28 11:40:37 +0530732 msleep_interruptible(250);
733 } while (!time_after_eq(jiffies, max_wait_time));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700734
735 if (mbox_status == MBOX_STS_COMMAND_COMPLETE) {
Vikas Chaudharyc301b022010-04-28 11:40:37 +0530736 DEBUG(printk(KERN_INFO "scsi%ld: %s: Firmware has started\n",
David Somayajuluafaf5a22006-09-19 10:28:00 -0700737 ha->host_no, __func__));
738
739 spin_lock_irqsave(&ha->hardware_lock, flags);
740 writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
741 &ha->reg->ctrl_status);
742 readl(&ha->reg->ctrl_status);
743 spin_unlock_irqrestore(&ha->hardware_lock, flags);
744
745 status = QLA_SUCCESS;
746 } else {
747 printk(KERN_INFO "scsi%ld: %s: Boot firmware failed "
748 "- mbox status 0x%x\n", ha->host_no, __func__,
749 mbox_status);
750 status = QLA_ERROR;
751 }
752 return status;
753}
754
David C Somayajulu92b72732007-05-23 17:55:40 -0700755int ql4xxx_lock_drvr_wait(struct scsi_qla_host *a)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700756{
David C Somayajulu92b72732007-05-23 17:55:40 -0700757#define QL4_LOCK_DRVR_WAIT 60
David C Somayajulu477ffb92007-01-22 12:26:11 -0800758#define QL4_LOCK_DRVR_SLEEP 1
David Somayajuluafaf5a22006-09-19 10:28:00 -0700759
760 int drvr_wait = QL4_LOCK_DRVR_WAIT;
761 while (drvr_wait) {
David C Somayajulu92b72732007-05-23 17:55:40 -0700762 if (ql4xxx_lock_drvr(a) == 0) {
David C Somayajulu477ffb92007-01-22 12:26:11 -0800763 ssleep(QL4_LOCK_DRVR_SLEEP);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700764 if (drvr_wait) {
765 DEBUG2(printk("scsi%ld: %s: Waiting for "
David C Somayajulu92b72732007-05-23 17:55:40 -0700766 "Global Init Semaphore(%d)...\n",
767 a->host_no,
David C Somayajulu477ffb92007-01-22 12:26:11 -0800768 __func__, drvr_wait));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700769 }
770 drvr_wait -= QL4_LOCK_DRVR_SLEEP;
771 } else {
772 DEBUG2(printk("scsi%ld: %s: Global Init Semaphore "
David C Somayajulu92b72732007-05-23 17:55:40 -0700773 "acquired\n", a->host_no, __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700774 return QLA_SUCCESS;
775 }
776 }
777 return QLA_ERROR;
778}
779
780/**
781 * qla4xxx_start_firmware - starts qla4xxx firmware
782 * @ha: Pointer to host adapter structure.
783 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200784 * This routine performs the necessary steps to start the firmware for
David Somayajuluafaf5a22006-09-19 10:28:00 -0700785 * the QLA4010 adapter.
786 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530787int qla4xxx_start_firmware(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700788{
789 unsigned long flags = 0;
790 uint32_t mbox_status;
791 int status = QLA_ERROR;
792 int soft_reset = 1;
793 int config_chip = 0;
794
David C Somayajulud9150582006-11-15 17:38:40 -0800795 if (is_qla4022(ha) | is_qla4032(ha))
David Somayajuluafaf5a22006-09-19 10:28:00 -0700796 ql4xxx_set_mac_number(ha);
797
798 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
799 return QLA_ERROR;
800
801 spin_lock_irqsave(&ha->hardware_lock, flags);
802
803 DEBUG2(printk("scsi%ld: %s: port_ctrl = 0x%08X\n", ha->host_no,
804 __func__, readw(isp_port_ctrl(ha))));
805 DEBUG(printk("scsi%ld: %s: port_status = 0x%08X\n", ha->host_no,
806 __func__, readw(isp_port_status(ha))));
807
808 /* Is Hardware already initialized? */
809 if ((readw(isp_port_ctrl(ha)) & 0x8000) != 0) {
810 DEBUG(printk("scsi%ld: %s: Hardware has already been "
811 "initialized\n", ha->host_no, __func__));
812
813 /* Receive firmware boot acknowledgement */
814 mbox_status = readw(&ha->reg->mailbox[0]);
815
816 DEBUG2(printk("scsi%ld: %s: H/W Config complete - mbox[0]= "
817 "0x%x\n", ha->host_no, __func__, mbox_status));
818
819 /* Is firmware already booted? */
820 if (mbox_status == 0) {
821 /* F/W not running, must be config by net driver */
822 config_chip = 1;
823 soft_reset = 0;
824 } else {
825 writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
826 &ha->reg->ctrl_status);
827 readl(&ha->reg->ctrl_status);
Sarang Radke78764992012-01-11 02:44:18 -0800828 writel(set_rmask(CSR_SCSI_COMPLETION_INTR),
829 &ha->reg->ctrl_status);
830 readl(&ha->reg->ctrl_status);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700831 spin_unlock_irqrestore(&ha->hardware_lock, flags);
832 if (qla4xxx_get_firmware_state(ha) == QLA_SUCCESS) {
833 DEBUG2(printk("scsi%ld: %s: Get firmware "
834 "state -- state = 0x%x\n",
835 ha->host_no,
836 __func__, ha->firmware_state));
837 /* F/W is running */
838 if (ha->firmware_state &
839 FW_STATE_CONFIG_WAIT) {
840 DEBUG2(printk("scsi%ld: %s: Firmware "
841 "in known state -- "
842 "config and "
843 "boot, state = 0x%x\n",
844 ha->host_no, __func__,
845 ha->firmware_state));
846 config_chip = 1;
847 soft_reset = 0;
848 }
849 } else {
850 DEBUG2(printk("scsi%ld: %s: Firmware in "
851 "unknown state -- resetting,"
852 " state = "
853 "0x%x\n", ha->host_no, __func__,
854 ha->firmware_state));
855 }
856 spin_lock_irqsave(&ha->hardware_lock, flags);
857 }
858 } else {
859 DEBUG(printk("scsi%ld: %s: H/W initialization hasn't been "
860 "started - resetting\n", ha->host_no, __func__));
861 }
862 spin_unlock_irqrestore(&ha->hardware_lock, flags);
863
864 DEBUG(printk("scsi%ld: %s: Flags soft_rest=%d, config= %d\n ",
865 ha->host_no, __func__, soft_reset, config_chip));
866 if (soft_reset) {
867 DEBUG(printk("scsi%ld: %s: Issue Soft Reset\n", ha->host_no,
868 __func__));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530869 status = qla4xxx_soft_reset(ha); /* NOTE: acquires drvr
870 * lock again, but ok */
David Somayajuluafaf5a22006-09-19 10:28:00 -0700871 if (status == QLA_ERROR) {
872 DEBUG(printk("scsi%d: %s: Soft Reset failed!\n",
873 ha->host_no, __func__));
874 ql4xxx_unlock_drvr(ha);
875 return QLA_ERROR;
876 }
877 config_chip = 1;
878
Joe Perchesb1c11812008-02-03 17:28:22 +0200879 /* Reset clears the semaphore, so acquire again */
David Somayajuluafaf5a22006-09-19 10:28:00 -0700880 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
881 return QLA_ERROR;
882 }
883
884 if (config_chip) {
885 if ((status = qla4xxx_config_nvram(ha)) == QLA_SUCCESS)
886 status = qla4xxx_start_firmware_from_flash(ha);
887 }
888
889 ql4xxx_unlock_drvr(ha);
890 if (status == QLA_SUCCESS) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700891 if (test_and_clear_bit(AF_GET_CRASH_RECORD, &ha->flags))
892 qla4xxx_get_crash_record(ha);
Vikas Chaudharyaa6db9b2014-02-24 22:07:03 -0500893
894 qla4xxx_init_rings(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700895 } else {
896 DEBUG(printk("scsi%ld: %s: Firmware has NOT started\n",
897 ha->host_no, __func__));
898 }
899 return status;
900}
Lalit Chandivade166dd202011-10-07 16:55:45 -0700901/**
902 * qla4xxx_free_ddb_index - Free DDBs reserved by firmware
903 * @ha: pointer to adapter structure
904 *
905 * Since firmware is not running in autoconnect mode the DDB indices should
906 * be freed so that when login happens from user space there are free DDB
907 * indices available.
908 **/
Mike Christie13483732011-12-01 21:38:41 -0600909void qla4xxx_free_ddb_index(struct scsi_qla_host *ha)
Lalit Chandivade166dd202011-10-07 16:55:45 -0700910{
911 int max_ddbs;
912 int ret;
913 uint32_t idx = 0, next_idx = 0;
914 uint32_t state = 0, conn_err = 0;
915
Mike Christie13483732011-12-01 21:38:41 -0600916 max_ddbs = is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
Lalit Chandivade166dd202011-10-07 16:55:45 -0700917 MAX_DEV_DB_ENTRIES;
918
919 for (idx = 0; idx < max_ddbs; idx = next_idx) {
920 ret = qla4xxx_get_fwddb_entry(ha, idx, NULL, 0, NULL,
921 &next_idx, &state, &conn_err,
922 NULL, NULL);
Tomas Henzle1cd89c2011-12-01 21:38:42 -0600923 if (ret == QLA_ERROR) {
924 next_idx++;
Lalit Chandivade166dd202011-10-07 16:55:45 -0700925 continue;
Tomas Henzle1cd89c2011-12-01 21:38:42 -0600926 }
Lalit Chandivade166dd202011-10-07 16:55:45 -0700927 if (state == DDB_DS_NO_CONNECTION_ACTIVE ||
928 state == DDB_DS_SESSION_FAILED) {
929 DEBUG2(ql4_printk(KERN_INFO, ha,
930 "Freeing DDB index = 0x%x\n", idx));
931 ret = qla4xxx_clear_ddb_entry(ha, idx);
932 if (ret == QLA_ERROR)
933 ql4_printk(KERN_ERR, ha,
934 "Unable to clear DDB index = "
935 "0x%x\n", idx);
936 }
937 if (next_idx == 0)
938 break;
939 }
940}
David Somayajuluafaf5a22006-09-19 10:28:00 -0700941
David Somayajuluafaf5a22006-09-19 10:28:00 -0700942/**
943 * qla4xxx_initialize_adapter - initiailizes hba
944 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -0700945 *
946 * This routine parforms all of the steps necessary to initialize the adapter.
947 *
948 **/
Mike Christie13483732011-12-01 21:38:41 -0600949int qla4xxx_initialize_adapter(struct scsi_qla_host *ha, int is_reset)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700950{
951 int status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700952
953 ha->eeprom_cmd_data = 0;
954
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530955 ql4_printk(KERN_INFO, ha, "Configuring PCI space...\n");
956 ha->isp_ops->pci_config(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700957
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530958 ha->isp_ops->disable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700959
960 /* Initialize the Host adapter request/response queues and firmware */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530961 if (ha->isp_ops->start_firmware(ha) == QLA_ERROR)
David C Somayajulu46235e62007-06-08 17:37:16 -0700962 goto exit_init_hba;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700963
Vikas Chaudhary5c19b922012-11-23 06:58:38 -0500964 /*
965 * For ISP83XX, mailbox and IOCB interrupts are enabled separately.
966 * Mailbox interrupts must be enabled prior to issuing any mailbox
967 * command in order to prevent the possibility of losing interrupts
968 * while switching from polling to interrupt mode. IOCB interrupts are
969 * enabled via isp_ops->enable_intrs.
970 */
Vikas Chaudharyb37ca412013-08-16 07:03:02 -0400971 if (is_qla8032(ha) || is_qla8042(ha))
Vikas Chaudhary5c19b922012-11-23 06:58:38 -0500972 qla4_83xx_enable_mbox_intrs(ha);
973
Harish Zunjarrao7ad633c2011-05-17 23:17:11 -0700974 if (qla4xxx_about_firmware(ha) == QLA_ERROR)
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530975 goto exit_init_hba;
976
977 if (ha->isp_ops->get_sys_info(ha) == QLA_ERROR)
David C Somayajulu46235e62007-06-08 17:37:16 -0700978 goto exit_init_hba;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700979
Karen Higginsff4108d2013-01-20 23:50:59 -0500980 qla4xxx_init_local_data(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700981
982 status = qla4xxx_init_firmware(ha);
983 if (status == QLA_ERROR)
David C Somayajulu46235e62007-06-08 17:37:16 -0700984 goto exit_init_hba;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700985
Mike Christie13483732011-12-01 21:38:41 -0600986 if (is_reset == RESET_ADAPTER)
987 qla4xxx_build_ddb_list(ha, is_reset);
Lalit Chandivade166dd202011-10-07 16:55:45 -0700988
David C Somayajulu92b72732007-05-23 17:55:40 -0700989 set_bit(AF_ONLINE, &ha->flags);
Vikas Chaudhary3710c602010-10-06 22:49:08 -0700990
Nilesh Javali37418cc2013-12-16 06:49:31 -0500991exit_init_hba:
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530992 DEBUG2(printk("scsi%ld: initialize adapter: %s\n", ha->host_no,
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300993 status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700994 return status;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700995}
996
Mike Christie13483732011-12-01 21:38:41 -0600997int qla4xxx_ddb_change(struct scsi_qla_host *ha, uint32_t fw_ddb_index,
998 struct ddb_entry *ddb_entry, uint32_t state)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700999{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001000 uint32_t old_fw_ddb_device_state;
1001 int status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001002
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001003 old_fw_ddb_device_state = ddb_entry->fw_ddb_device_state;
1004 DEBUG2(ql4_printk(KERN_INFO, ha,
1005 "%s: DDB - old state = 0x%x, new state = 0x%x for "
1006 "index [%d]\n", __func__,
1007 ddb_entry->fw_ddb_device_state, state, fw_ddb_index));
David Somayajuluafaf5a22006-09-19 10:28:00 -07001008
1009 ddb_entry->fw_ddb_device_state = state;
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05301010
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001011 switch (old_fw_ddb_device_state) {
1012 case DDB_DS_LOGIN_IN_PROCESS:
1013 switch (state) {
1014 case DDB_DS_SESSION_ACTIVE:
1015 case DDB_DS_DISCOVERY:
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001016 qla4xxx_update_session_conn_param(ha, ddb_entry);
Manish Rangankar3d948e22012-04-23 22:32:33 -07001017 ddb_entry->unblock_sess(ddb_entry->sess);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001018 status = QLA_SUCCESS;
1019 break;
1020 case DDB_DS_SESSION_FAILED:
1021 case DDB_DS_NO_CONNECTION_ACTIVE:
1022 iscsi_conn_login_event(ddb_entry->conn,
1023 ISCSI_CONN_STATE_FREE);
1024 status = QLA_SUCCESS;
1025 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001026 }
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001027 break;
1028 case DDB_DS_SESSION_ACTIVE:
Manish Rangankar3d948e22012-04-23 22:32:33 -07001029 case DDB_DS_DISCOVERY:
Manish Rangankar736cf362011-10-07 16:55:46 -07001030 switch (state) {
1031 case DDB_DS_SESSION_FAILED:
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001032 /*
1033 * iscsi_session failure will cause userspace to
1034 * stop the connection which in turn would block the
1035 * iscsi_session and start relogin
1036 */
1037 iscsi_session_failure(ddb_entry->sess->dd_data,
1038 ISCSI_ERR_CONN_FAILED);
1039 status = QLA_SUCCESS;
Manish Rangankar736cf362011-10-07 16:55:46 -07001040 break;
1041 case DDB_DS_NO_CONNECTION_ACTIVE:
1042 clear_bit(fw_ddb_index, ha->ddb_idx_map);
1043 status = QLA_SUCCESS;
1044 break;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001045 }
1046 break;
Manish Rangankar98270ab2011-10-07 16:55:47 -07001047 case DDB_DS_SESSION_FAILED:
1048 switch (state) {
1049 case DDB_DS_SESSION_ACTIVE:
1050 case DDB_DS_DISCOVERY:
Mike Christie13483732011-12-01 21:38:41 -06001051 ddb_entry->unblock_sess(ddb_entry->sess);
Manish Rangankar98270ab2011-10-07 16:55:47 -07001052 qla4xxx_update_session_conn_param(ha, ddb_entry);
1053 status = QLA_SUCCESS;
1054 break;
1055 case DDB_DS_SESSION_FAILED:
1056 iscsi_session_failure(ddb_entry->sess->dd_data,
1057 ISCSI_ERR_CONN_FAILED);
1058 status = QLA_SUCCESS;
1059 break;
1060 }
1061 break;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001062 default:
1063 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Unknown Event\n",
1064 __func__));
1065 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001066 }
Mike Christie13483732011-12-01 21:38:41 -06001067 return status;
1068}
1069
1070void qla4xxx_arm_relogin_timer(struct ddb_entry *ddb_entry)
1071{
1072 /*
1073 * This triggers a relogin. After the relogin_timer
1074 * expires, the relogin gets scheduled. We must wait a
1075 * minimum amount of time since receiving an 0x8014 AEN
1076 * with failed device_state or a logout response before
1077 * we can issue another relogin.
1078 *
1079 * Firmware pads this timeout: (time2wait +1).
1080 * Driver retry to login should be longer than F/W.
1081 * Otherwise F/W will fail
1082 * set_ddb() mbx cmd with 0x4005 since it still
1083 * counting down its time2wait.
1084 */
1085 atomic_set(&ddb_entry->relogin_timer, 0);
1086 atomic_set(&ddb_entry->retry_relogin_timer,
1087 ddb_entry->default_time2wait + 4);
1088
1089}
1090
1091int qla4xxx_flash_ddb_change(struct scsi_qla_host *ha, uint32_t fw_ddb_index,
1092 struct ddb_entry *ddb_entry, uint32_t state)
1093{
1094 uint32_t old_fw_ddb_device_state;
1095 int status = QLA_ERROR;
1096
1097 old_fw_ddb_device_state = ddb_entry->fw_ddb_device_state;
1098 DEBUG2(ql4_printk(KERN_INFO, ha,
1099 "%s: DDB - old state = 0x%x, new state = 0x%x for "
1100 "index [%d]\n", __func__,
1101 ddb_entry->fw_ddb_device_state, state, fw_ddb_index));
1102
1103 ddb_entry->fw_ddb_device_state = state;
1104
1105 switch (old_fw_ddb_device_state) {
1106 case DDB_DS_LOGIN_IN_PROCESS:
1107 case DDB_DS_NO_CONNECTION_ACTIVE:
1108 switch (state) {
1109 case DDB_DS_SESSION_ACTIVE:
1110 ddb_entry->unblock_sess(ddb_entry->sess);
1111 qla4xxx_update_session_conn_fwddb_param(ha, ddb_entry);
1112 status = QLA_SUCCESS;
1113 break;
1114 case DDB_DS_SESSION_FAILED:
1115 iscsi_block_session(ddb_entry->sess);
1116 if (!test_bit(DF_RELOGIN, &ddb_entry->flags))
1117 qla4xxx_arm_relogin_timer(ddb_entry);
1118 status = QLA_SUCCESS;
1119 break;
1120 }
1121 break;
1122 case DDB_DS_SESSION_ACTIVE:
1123 switch (state) {
1124 case DDB_DS_SESSION_FAILED:
1125 iscsi_block_session(ddb_entry->sess);
1126 if (!test_bit(DF_RELOGIN, &ddb_entry->flags))
1127 qla4xxx_arm_relogin_timer(ddb_entry);
1128 status = QLA_SUCCESS;
1129 break;
1130 }
1131 break;
1132 case DDB_DS_SESSION_FAILED:
1133 switch (state) {
1134 case DDB_DS_SESSION_ACTIVE:
1135 ddb_entry->unblock_sess(ddb_entry->sess);
1136 qla4xxx_update_session_conn_fwddb_param(ha, ddb_entry);
1137 status = QLA_SUCCESS;
1138 break;
1139 case DDB_DS_SESSION_FAILED:
1140 if (!test_bit(DF_RELOGIN, &ddb_entry->flags))
1141 qla4xxx_arm_relogin_timer(ddb_entry);
1142 status = QLA_SUCCESS;
1143 break;
1144 }
1145 break;
1146 default:
1147 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Unknown Event\n",
1148 __func__));
1149 break;
1150 }
1151 return status;
1152}
1153
1154/**
1155 * qla4xxx_process_ddb_changed - process ddb state change
1156 * @ha - Pointer to host adapter structure.
1157 * @fw_ddb_index - Firmware's device database index
1158 * @state - Device state
1159 *
1160 * This routine processes a Decive Database Changed AEN Event.
1161 **/
1162int qla4xxx_process_ddb_changed(struct scsi_qla_host *ha,
1163 uint32_t fw_ddb_index,
1164 uint32_t state, uint32_t conn_err)
1165{
1166 struct ddb_entry *ddb_entry;
1167 int status = QLA_ERROR;
1168
1169 /* check for out of range index */
1170 if (fw_ddb_index >= MAX_DDB_ENTRIES)
1171 goto exit_ddb_event;
1172
1173 /* Get the corresponging ddb entry */
1174 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, fw_ddb_index);
1175 /* Device does not currently exist in our database. */
1176 if (ddb_entry == NULL) {
1177 ql4_printk(KERN_ERR, ha, "%s: No ddb_entry at FW index [%d]\n",
1178 __func__, fw_ddb_index);
1179
1180 if (state == DDB_DS_NO_CONNECTION_ACTIVE)
1181 clear_bit(fw_ddb_index, ha->ddb_idx_map);
1182
1183 goto exit_ddb_event;
1184 }
1185
1186 ddb_entry->ddb_change(ha, fw_ddb_index, ddb_entry, state);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001187
1188exit_ddb_event:
1189 return status;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001190}
Mike Christie13483732011-12-01 21:38:41 -06001191
1192/**
1193 * qla4xxx_login_flash_ddb - Login to target (DDB)
1194 * @cls_session: Pointer to the session to login
1195 *
1196 * This routine logins to the target.
1197 * Issues setddb and conn open mbx
1198 **/
1199void qla4xxx_login_flash_ddb(struct iscsi_cls_session *cls_session)
1200{
1201 struct iscsi_session *sess;
1202 struct ddb_entry *ddb_entry;
1203 struct scsi_qla_host *ha;
1204 struct dev_db_entry *fw_ddb_entry = NULL;
1205 dma_addr_t fw_ddb_dma;
1206 uint32_t mbx_sts = 0;
1207 int ret;
1208
1209 sess = cls_session->dd_data;
1210 ddb_entry = sess->dd_data;
1211 ha = ddb_entry->ha;
1212
1213 if (!test_bit(AF_LINK_UP, &ha->flags))
1214 return;
1215
1216 if (ddb_entry->ddb_type != FLASH_DDB) {
1217 DEBUG2(ql4_printk(KERN_INFO, ha,
1218 "Skipping login to non FLASH DB"));
1219 goto exit_login;
1220 }
1221
1222 fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
1223 &fw_ddb_dma);
1224 if (fw_ddb_entry == NULL) {
1225 DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
1226 goto exit_login;
1227 }
1228
1229 if (ddb_entry->fw_ddb_index == INVALID_ENTRY) {
1230 ret = qla4xxx_get_ddb_index(ha, &ddb_entry->fw_ddb_index);
1231 if (ret == QLA_ERROR)
1232 goto exit_login;
1233
1234 ha->fw_ddb_index_map[ddb_entry->fw_ddb_index] = ddb_entry;
1235 ha->tot_ddbs++;
1236 }
1237
1238 memcpy(fw_ddb_entry, &ddb_entry->fw_ddb_entry,
1239 sizeof(struct dev_db_entry));
1240 ddb_entry->sess->target_id = ddb_entry->fw_ddb_index;
1241
1242 ret = qla4xxx_set_ddb_entry(ha, ddb_entry->fw_ddb_index,
1243 fw_ddb_dma, &mbx_sts);
1244 if (ret == QLA_ERROR) {
1245 DEBUG2(ql4_printk(KERN_ERR, ha, "Set DDB failed\n"));
1246 goto exit_login;
1247 }
1248
1249 ddb_entry->fw_ddb_device_state = DDB_DS_LOGIN_IN_PROCESS;
1250 ret = qla4xxx_conn_open(ha, ddb_entry->fw_ddb_index);
1251 if (ret == QLA_ERROR) {
1252 ql4_printk(KERN_ERR, ha, "%s: Login failed: %s\n", __func__,
1253 sess->targetname);
1254 goto exit_login;
1255 }
1256
1257exit_login:
1258 if (fw_ddb_entry)
1259 dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
1260}
1261