cgroup: superblock can't be released with active dentries
[firefly-linux-kernel-4.4.55.git] / drivers / scsi / qla4xxx / ql4_init.c
1 /*
2  * QLogic iSCSI HBA Driver
3  * Copyright (c)  2003-2010 QLogic Corporation
4  *
5  * See LICENSE.qla4xxx for copyright and licensing details.
6  */
7
8 #include <scsi/iscsi_if.h>
9 #include "ql4_def.h"
10 #include "ql4_glbl.h"
11 #include "ql4_dbg.h"
12 #include "ql4_inline.h"
13
14 static 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  *
48  * This routine marks a DDB entry INVALID
49  **/
50 void qla4xxx_free_ddb(struct scsi_qla_host *ha,
51     struct ddb_entry *ddb_entry)
52 {
53         /* 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--;
57 }
58
59 /**
60  * 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  **/
66 static 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 /**
79  * 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  **/
86 int qla4xxx_init_rings(struct scsi_qla_host *ha)
87 {
88         unsigned long flags = 0;
89         int i;
90
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
103         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();
121
122                 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);
128
129         /* Initialize mabilbox active array */
130         for (i = 0; i < MAX_MRB; i++)
131                 ha->active_mrb_array[i] = NULL;
132
133         spin_unlock_irqrestore(&ha->hardware_lock, flags);
134
135         return QLA_SUCCESS;
136 }
137
138 /**
139  * qla4xxx_get_sys_info - validate adapter MAC address(es)
140  * @ha: pointer to host adapter structure.
141  *
142  **/
143 int qla4xxx_get_sys_info(struct scsi_qla_host *ha)
144 {
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
155                 goto exit_get_sys_info_no_free;
156         }
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
165                 goto exit_get_sys_info;
166         }
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
178 exit_get_sys_info:
179         dma_free_coherent(&ha->pdev->dev, sizeof(*sys_info), sys_info,
180                           sys_info_dma);
181
182 exit_get_sys_info_no_free:
183         return status;
184 }
185
186 /**
187  * qla4xxx_init_local_data - initialize adapter specific local data
188  * @ha: pointer to host adapter structure.
189  *
190  **/
191 static int qla4xxx_init_local_data(struct scsi_qla_host *ha)
192 {
193         /* Initialize aen queue */
194         ha->aen_q_count = MAX_AEN_ENTRIES;
195
196         return qla4xxx_get_firmware_status(ha);
197 }
198
199 static uint8_t
200 qla4xxx_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                 }
215                 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))) {
223
224                         ipv6_wait = 1;
225
226                         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)) {
232                                 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                         }
238                         if (memcmp(&ha->ip_config.ipv6_default_router_addr,
239                                    ip_address, IPv6_ADDR_LEN) == 0) {
240                                 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                         }
246                         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)) {
253                                 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 "));
264                         if (ha->ip_config.ipv6_link_local_state ==
265                             IP_ADDRSTATE_ACQUIRING)
266                                 DEBUG2(printk("IPv6LinkLocal "));
267                         if (ha->ip_config.ipv6_addr0_state ==
268                             IP_ADDRSTATE_ACQUIRING)
269                                 DEBUG2(printk("IPv6Addr0 "));
270                         if (ha->ip_config.ipv6_addr1_state ==
271                             IP_ADDRSTATE_ACQUIRING)
272                                 DEBUG2(printk("IPv6Addr1 "));
273                         DEBUG2(printk("\"\n"));
274                 }
275         }
276
277         return ipv4_wait|ipv6_wait;
278 }
279
280 static int qla4xxx_fw_ready(struct scsi_qla_host *ha)
281 {
282         uint32_t timeout_count;
283         int ready = 0;
284
285         DEBUG2(ql4_printk(KERN_INFO, ha, "Waiting for Firmware Ready..\n"));
286         for (timeout_count = ADAPTER_INIT_TOV; timeout_count > 0;
287              timeout_count--) {
288                 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
289                         qla4xxx_get_dhcp_ip_address(ha);
290
291                 /* Get firmware state. */
292                 if (qla4xxx_get_firmware_state(ha) != QLA_SUCCESS) {
293                         DEBUG2(printk("scsi%ld: %s: unable to get firmware "
294                                       "state\n", ha->host_no, __func__));
295                         break;
296                 }
297
298                 if (ha->firmware_state & FW_STATE_ERROR) {
299                         DEBUG2(printk("scsi%ld: %s: an unrecoverable error has"
300                                       " occurred\n", ha->host_no, __func__));
301                         break;
302
303                 }
304                 if (ha->firmware_state & FW_STATE_CONFIG_WAIT) {
305                         /*
306                          * The firmware has not yet been issued an Initialize
307                          * Firmware command, so issue it now.
308                          */
309                         if (qla4xxx_initialize_fw_cb(ha) == QLA_ERROR)
310                                 break;
311
312                         /* Go back and test for ready state - no wait. */
313                         continue;
314                 }
315
316                 if (ha->firmware_state & FW_STATE_WAIT_AUTOCONNECT) {
317                         DEBUG2(printk(KERN_INFO "scsi%ld: %s: fwstate:"
318                                       "AUTOCONNECT in progress\n",
319                                       ha->host_no, __func__));
320                 }
321
322                 if (ha->firmware_state & FW_STATE_CONFIGURING_IP) {
323                         DEBUG2(printk(KERN_INFO "scsi%ld: %s: fwstate:"
324                                       " CONFIGURING IP\n",
325                                       ha->host_no, __func__));
326                         /*
327                          * Check for link state after 15 secs and if link is
328                          * still DOWN then, cable is unplugged. Ignore "DHCP
329                          * in Progress/CONFIGURING IP" bit to check if firmware
330                          * is in ready state or not after 15 secs.
331                          * This is applicable for both 2.x & 3.x firmware
332                          */
333                         if (timeout_count <= (ADAPTER_INIT_TOV - 15)) {
334                                 if (ha->addl_fw_state & FW_ADDSTATE_LINK_UP) {
335                                         DEBUG2(printk(KERN_INFO "scsi%ld: %s:"
336                                                   " LINK UP (Cable plugged)\n",
337                                                   ha->host_no, __func__));
338                                 } else if (ha->firmware_state &
339                                           (FW_STATE_CONFIGURING_IP |
340                                                              FW_STATE_READY)) {
341                                         DEBUG2(printk(KERN_INFO "scsi%ld: %s: "
342                                                 "LINK DOWN (Cable unplugged)\n",
343                                                 ha->host_no, __func__));
344                                         ha->firmware_state = FW_STATE_READY;
345                                 }
346                         }
347                 }
348
349                 if (ha->firmware_state == FW_STATE_READY) {
350                         /* If DHCP IP Addr is available, retrieve it now. */
351                         if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR,
352                                                                 &ha->dpc_flags))
353                                 qla4xxx_get_dhcp_ip_address(ha);
354
355                         if (!qla4xxx_wait_for_ip_config(ha) ||
356                                                         timeout_count == 1) {
357                                 DEBUG2(ql4_printk(KERN_INFO, ha,
358                                     "Firmware Ready..\n"));
359                                 /* The firmware is ready to process SCSI
360                                    commands. */
361                                 DEBUG2(ql4_printk(KERN_INFO, ha,
362                                         "scsi%ld: %s: MEDIA TYPE"
363                                         " - %s\n", ha->host_no,
364                                         __func__, (ha->addl_fw_state &
365                                         FW_ADDSTATE_OPTICAL_MEDIA)
366                                         != 0 ? "OPTICAL" : "COPPER"));
367                                 DEBUG2(ql4_printk(KERN_INFO, ha,
368                                         "scsi%ld: %s: DHCPv4 STATE"
369                                         " Enabled %s\n", ha->host_no,
370                                          __func__, (ha->addl_fw_state &
371                                          FW_ADDSTATE_DHCPv4_ENABLED) != 0 ?
372                                         "YES" : "NO"));
373                                 DEBUG2(ql4_printk(KERN_INFO, ha,
374                                         "scsi%ld: %s: LINK %s\n",
375                                         ha->host_no, __func__,
376                                         (ha->addl_fw_state &
377                                          FW_ADDSTATE_LINK_UP) != 0 ?
378                                         "UP" : "DOWN"));
379                                 DEBUG2(ql4_printk(KERN_INFO, ha,
380                                         "scsi%ld: %s: iSNS Service "
381                                         "Started %s\n",
382                                         ha->host_no, __func__,
383                                         (ha->addl_fw_state &
384                                          FW_ADDSTATE_ISNS_SVC_ENABLED) != 0 ?
385                                         "YES" : "NO"));
386
387                                 ready = 1;
388                                 break;
389                         }
390                 }
391                 DEBUG2(printk("scsi%ld: %s: waiting on fw, state=%x:%x - "
392                               "seconds expired= %d\n", ha->host_no, __func__,
393                               ha->firmware_state, ha->addl_fw_state,
394                               timeout_count));
395                 if (is_qla4032(ha) &&
396                         !(ha->addl_fw_state & FW_ADDSTATE_LINK_UP) &&
397                         (timeout_count < ADAPTER_INIT_TOV - 5)) {
398                         break;
399                 }
400
401                 msleep(1000);
402         }                       /* end of for */
403
404         if (timeout_count <= 0)
405                 DEBUG2(printk("scsi%ld: %s: FW Initialization timed out!\n",
406                               ha->host_no, __func__));
407
408         if (ha->firmware_state & FW_STATE_CONFIGURING_IP) {
409                 DEBUG2(printk("scsi%ld: %s: FW initialized, but is reporting "
410                               "it's waiting to configure an IP address\n",
411                                ha->host_no, __func__));
412                 ready = 1;
413         } else if (ha->firmware_state & FW_STATE_WAIT_AUTOCONNECT) {
414                 DEBUG2(printk("scsi%ld: %s: FW initialized, but "
415                               "auto-discovery still in process\n",
416                                ha->host_no, __func__));
417                 ready = 1;
418         }
419
420         return ready;
421 }
422
423 /**
424  * qla4xxx_init_firmware - initializes the firmware.
425  * @ha: pointer to host adapter structure.
426  *
427  **/
428 static int qla4xxx_init_firmware(struct scsi_qla_host *ha)
429 {
430         int status = QLA_ERROR;
431
432         if (is_aer_supported(ha) &&
433             test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
434                 return status;
435
436         /* For 82xx, stop firmware before initializing because if BIOS
437          * has previously initialized firmware, then driver's initialize
438          * firmware will fail. */
439         if (is_qla8022(ha))
440                 qla4_8xxx_stop_firmware(ha);
441
442         ql4_printk(KERN_INFO, ha, "Initializing firmware..\n");
443         if (qla4xxx_initialize_fw_cb(ha) == QLA_ERROR) {
444                 DEBUG2(printk("scsi%ld: %s: Failed to initialize firmware "
445                               "control block\n", ha->host_no, __func__));
446                 return status;
447         }
448         if (!qla4xxx_fw_ready(ha))
449                 return status;
450
451         return qla4xxx_get_firmware_status(ha);
452 }
453
454 static void qla4xxx_set_model_info(struct scsi_qla_host *ha)
455 {
456         uint16_t board_id_string[8];
457         int i;
458         int size = sizeof(ha->nvram->isp4022.boardIdStr);
459         int offset = offsetof(struct eeprom_data, isp4022.boardIdStr) / 2;
460
461         for (i = 0; i < (size / 2) ; i++) {
462                 board_id_string[i] = rd_nvram_word(ha, offset);
463                 offset += 1;
464         }
465
466         memcpy(ha->model_name, board_id_string, size);
467 }
468
469 static int qla4xxx_config_nvram(struct scsi_qla_host *ha)
470 {
471         unsigned long flags;
472         union external_hw_config_reg extHwConfig;
473
474         DEBUG2(printk("scsi%ld: %s: Get EEProm parameters \n", ha->host_no,
475                       __func__));
476         if (ql4xxx_lock_flash(ha) != QLA_SUCCESS)
477                 return QLA_ERROR;
478         if (ql4xxx_lock_nvram(ha) != QLA_SUCCESS) {
479                 ql4xxx_unlock_flash(ha);
480                 return QLA_ERROR;
481         }
482
483         /* Get EEPRom Parameters from NVRAM and validate */
484         ql4_printk(KERN_INFO, ha, "Configuring NVRAM ...\n");
485         if (qla4xxx_is_nvram_configuration_valid(ha) == QLA_SUCCESS) {
486                 spin_lock_irqsave(&ha->hardware_lock, flags);
487                 extHwConfig.Asuint32_t =
488                         rd_nvram_word(ha, eeprom_ext_hw_conf_offset(ha));
489                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
490         } else {
491                 ql4_printk(KERN_WARNING, ha,
492                     "scsi%ld: %s: EEProm checksum invalid.  "
493                     "Please update your EEPROM\n", ha->host_no,
494                     __func__);
495
496                 /* Attempt to set defaults */
497                 if (is_qla4010(ha))
498                         extHwConfig.Asuint32_t = 0x1912;
499                 else if (is_qla4022(ha) | is_qla4032(ha))
500                         extHwConfig.Asuint32_t = 0x0023;
501                 else
502                         return QLA_ERROR;
503         }
504
505         if (is_qla4022(ha) || is_qla4032(ha))
506                 qla4xxx_set_model_info(ha);
507         else
508                 strcpy(ha->model_name, "QLA4010");
509
510         DEBUG(printk("scsi%ld: %s: Setting extHwConfig to 0xFFFF%04x\n",
511                      ha->host_no, __func__, extHwConfig.Asuint32_t));
512
513         spin_lock_irqsave(&ha->hardware_lock, flags);
514         writel((0xFFFF << 16) | extHwConfig.Asuint32_t, isp_ext_hw_conf(ha));
515         readl(isp_ext_hw_conf(ha));
516         spin_unlock_irqrestore(&ha->hardware_lock, flags);
517
518         ql4xxx_unlock_nvram(ha);
519         ql4xxx_unlock_flash(ha);
520
521         return QLA_SUCCESS;
522 }
523
524 /**
525  * qla4_8xxx_pci_config() - Setup ISP82xx PCI configuration registers.
526  * @ha: HA context
527  */
528 void qla4_8xxx_pci_config(struct scsi_qla_host *ha)
529 {
530         pci_set_master(ha->pdev);
531 }
532
533 void qla4xxx_pci_config(struct scsi_qla_host *ha)
534 {
535         uint16_t w;
536         int status;
537
538         ql4_printk(KERN_INFO, ha, "Configuring PCI space...\n");
539
540         pci_set_master(ha->pdev);
541         status = pci_set_mwi(ha->pdev);
542         /*
543          * We want to respect framework's setting of PCI configuration space
544          * command register and also want to make sure that all bits of
545          * interest to us are properly set in command register.
546          */
547         pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
548         w |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
549         w &= ~PCI_COMMAND_INTX_DISABLE;
550         pci_write_config_word(ha->pdev, PCI_COMMAND, w);
551 }
552
553 static int qla4xxx_start_firmware_from_flash(struct scsi_qla_host *ha)
554 {
555         int status = QLA_ERROR;
556         unsigned long max_wait_time;
557         unsigned long flags;
558         uint32_t mbox_status;
559
560         ql4_printk(KERN_INFO, ha, "Starting firmware ...\n");
561
562         /*
563          * Start firmware from flash ROM
564          *
565          * WORKAROUND: Stuff a non-constant value that the firmware can
566          * use as a seed for a random number generator in MB7 prior to
567          * setting BOOT_ENABLE.  Fixes problem where the TCP
568          * connections use the same TCP ports after each reboot,
569          * causing some connections to not get re-established.
570          */
571         DEBUG(printk("scsi%d: %s: Start firmware from flash ROM\n",
572                      ha->host_no, __func__));
573
574         spin_lock_irqsave(&ha->hardware_lock, flags);
575         writel(jiffies, &ha->reg->mailbox[7]);
576         if (is_qla4022(ha) | is_qla4032(ha))
577                 writel(set_rmask(NVR_WRITE_ENABLE),
578                        &ha->reg->u1.isp4022.nvram);
579
580         writel(2, &ha->reg->mailbox[6]);
581         readl(&ha->reg->mailbox[6]);
582
583         writel(set_rmask(CSR_BOOT_ENABLE), &ha->reg->ctrl_status);
584         readl(&ha->reg->ctrl_status);
585         spin_unlock_irqrestore(&ha->hardware_lock, flags);
586
587         /* Wait for firmware to come UP. */
588         DEBUG2(printk(KERN_INFO "scsi%ld: %s: Wait up to %d seconds for "
589                       "boot firmware to complete...\n",
590                       ha->host_no, __func__, FIRMWARE_UP_TOV));
591         max_wait_time = jiffies + (FIRMWARE_UP_TOV * HZ);
592         do {
593                 uint32_t ctrl_status;
594
595                 spin_lock_irqsave(&ha->hardware_lock, flags);
596                 ctrl_status = readw(&ha->reg->ctrl_status);
597                 mbox_status = readw(&ha->reg->mailbox[0]);
598                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
599
600                 if (ctrl_status & set_rmask(CSR_SCSI_PROCESSOR_INTR))
601                         break;
602                 if (mbox_status == MBOX_STS_COMMAND_COMPLETE)
603                         break;
604
605                 DEBUG2(printk(KERN_INFO "scsi%ld: %s: Waiting for boot "
606                     "firmware to complete... ctrl_sts=0x%x, remaining=%ld\n",
607                     ha->host_no, __func__, ctrl_status, max_wait_time));
608
609                 msleep_interruptible(250);
610         } while (!time_after_eq(jiffies, max_wait_time));
611
612         if (mbox_status == MBOX_STS_COMMAND_COMPLETE) {
613                 DEBUG(printk(KERN_INFO "scsi%ld: %s: Firmware has started\n",
614                              ha->host_no, __func__));
615
616                 spin_lock_irqsave(&ha->hardware_lock, flags);
617                 writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
618                        &ha->reg->ctrl_status);
619                 readl(&ha->reg->ctrl_status);
620                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
621
622                 status = QLA_SUCCESS;
623         } else {
624                 printk(KERN_INFO "scsi%ld: %s: Boot firmware failed "
625                        "-  mbox status 0x%x\n", ha->host_no, __func__,
626                        mbox_status);
627                 status = QLA_ERROR;
628         }
629         return status;
630 }
631
632 int ql4xxx_lock_drvr_wait(struct scsi_qla_host *a)
633 {
634 #define QL4_LOCK_DRVR_WAIT      60
635 #define QL4_LOCK_DRVR_SLEEP     1
636
637         int drvr_wait = QL4_LOCK_DRVR_WAIT;
638         while (drvr_wait) {
639                 if (ql4xxx_lock_drvr(a) == 0) {
640                         ssleep(QL4_LOCK_DRVR_SLEEP);
641                         if (drvr_wait) {
642                                 DEBUG2(printk("scsi%ld: %s: Waiting for "
643                                               "Global Init Semaphore(%d)...\n",
644                                               a->host_no,
645                                               __func__, drvr_wait));
646                         }
647                         drvr_wait -= QL4_LOCK_DRVR_SLEEP;
648                 } else {
649                         DEBUG2(printk("scsi%ld: %s: Global Init Semaphore "
650                                       "acquired\n", a->host_no, __func__));
651                         return QLA_SUCCESS;
652                 }
653         }
654         return QLA_ERROR;
655 }
656
657 /**
658  * qla4xxx_start_firmware - starts qla4xxx firmware
659  * @ha: Pointer to host adapter structure.
660  *
661  * This routine performs the necessary steps to start the firmware for
662  * the QLA4010 adapter.
663  **/
664 int qla4xxx_start_firmware(struct scsi_qla_host *ha)
665 {
666         unsigned long flags = 0;
667         uint32_t mbox_status;
668         int status = QLA_ERROR;
669         int soft_reset = 1;
670         int config_chip = 0;
671
672         if (is_qla4022(ha) | is_qla4032(ha))
673                 ql4xxx_set_mac_number(ha);
674
675         if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
676                 return QLA_ERROR;
677
678         spin_lock_irqsave(&ha->hardware_lock, flags);
679
680         DEBUG2(printk("scsi%ld: %s: port_ctrl   = 0x%08X\n", ha->host_no,
681                       __func__, readw(isp_port_ctrl(ha))));
682         DEBUG(printk("scsi%ld: %s: port_status = 0x%08X\n", ha->host_no,
683                      __func__, readw(isp_port_status(ha))));
684
685         /* Is Hardware already initialized? */
686         if ((readw(isp_port_ctrl(ha)) & 0x8000) != 0) {
687                 DEBUG(printk("scsi%ld: %s: Hardware has already been "
688                              "initialized\n", ha->host_no, __func__));
689
690                 /* Receive firmware boot acknowledgement */
691                 mbox_status = readw(&ha->reg->mailbox[0]);
692
693                 DEBUG2(printk("scsi%ld: %s: H/W Config complete - mbox[0]= "
694                               "0x%x\n", ha->host_no, __func__, mbox_status));
695
696                 /* Is firmware already booted? */
697                 if (mbox_status == 0) {
698                         /* F/W not running, must be config by net driver */
699                         config_chip = 1;
700                         soft_reset = 0;
701                 } else {
702                         writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
703                                &ha->reg->ctrl_status);
704                         readl(&ha->reg->ctrl_status);
705                         writel(set_rmask(CSR_SCSI_COMPLETION_INTR),
706                                &ha->reg->ctrl_status);
707                         readl(&ha->reg->ctrl_status);
708                         spin_unlock_irqrestore(&ha->hardware_lock, flags);
709                         if (qla4xxx_get_firmware_state(ha) == QLA_SUCCESS) {
710                                 DEBUG2(printk("scsi%ld: %s: Get firmware "
711                                               "state -- state = 0x%x\n",
712                                               ha->host_no,
713                                               __func__, ha->firmware_state));
714                                 /* F/W is running */
715                                 if (ha->firmware_state &
716                                     FW_STATE_CONFIG_WAIT) {
717                                         DEBUG2(printk("scsi%ld: %s: Firmware "
718                                                       "in known state -- "
719                                                       "config and "
720                                                       "boot, state = 0x%x\n",
721                                                       ha->host_no, __func__,
722                                                       ha->firmware_state));
723                                         config_chip = 1;
724                                         soft_reset = 0;
725                                 }
726                         } else {
727                                 DEBUG2(printk("scsi%ld: %s: Firmware in "
728                                               "unknown state -- resetting,"
729                                               " state = "
730                                               "0x%x\n", ha->host_no, __func__,
731                                               ha->firmware_state));
732                         }
733                         spin_lock_irqsave(&ha->hardware_lock, flags);
734                 }
735         } else {
736                 DEBUG(printk("scsi%ld: %s: H/W initialization hasn't been "
737                              "started - resetting\n", ha->host_no, __func__));
738         }
739         spin_unlock_irqrestore(&ha->hardware_lock, flags);
740
741         DEBUG(printk("scsi%ld: %s: Flags soft_rest=%d, config= %d\n ",
742                      ha->host_no, __func__, soft_reset, config_chip));
743         if (soft_reset) {
744                 DEBUG(printk("scsi%ld: %s: Issue Soft Reset\n", ha->host_no,
745                              __func__));
746                 status = qla4xxx_soft_reset(ha);        /* NOTE: acquires drvr
747                                                          * lock again, but ok */
748                 if (status == QLA_ERROR) {
749                         DEBUG(printk("scsi%d: %s: Soft Reset failed!\n",
750                                      ha->host_no, __func__));
751                         ql4xxx_unlock_drvr(ha);
752                         return QLA_ERROR;
753                 }
754                 config_chip = 1;
755
756                 /* Reset clears the semaphore, so acquire again */
757                 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
758                         return QLA_ERROR;
759         }
760
761         if (config_chip) {
762                 if ((status = qla4xxx_config_nvram(ha)) == QLA_SUCCESS)
763                         status = qla4xxx_start_firmware_from_flash(ha);
764         }
765
766         ql4xxx_unlock_drvr(ha);
767         if (status == QLA_SUCCESS) {
768                 if (test_and_clear_bit(AF_GET_CRASH_RECORD, &ha->flags))
769                         qla4xxx_get_crash_record(ha);
770         } else {
771                 DEBUG(printk("scsi%ld: %s: Firmware has NOT started\n",
772                              ha->host_no, __func__));
773         }
774         return status;
775 }
776 /**
777  * qla4xxx_free_ddb_index - Free DDBs reserved by firmware
778  * @ha: pointer to adapter structure
779  *
780  * Since firmware is not running in autoconnect mode the DDB indices should
781  * be freed so that when login happens from user space there are free DDB
782  * indices available.
783  **/
784 void qla4xxx_free_ddb_index(struct scsi_qla_host *ha)
785 {
786         int max_ddbs;
787         int ret;
788         uint32_t idx = 0, next_idx = 0;
789         uint32_t state = 0, conn_err = 0;
790
791         max_ddbs =  is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
792                                      MAX_DEV_DB_ENTRIES;
793
794         for (idx = 0; idx < max_ddbs; idx = next_idx) {
795                 ret = qla4xxx_get_fwddb_entry(ha, idx, NULL, 0, NULL,
796                                               &next_idx, &state, &conn_err,
797                                                 NULL, NULL);
798                 if (ret == QLA_ERROR) {
799                         next_idx++;
800                         continue;
801                 }
802                 if (state == DDB_DS_NO_CONNECTION_ACTIVE ||
803                     state == DDB_DS_SESSION_FAILED) {
804                         DEBUG2(ql4_printk(KERN_INFO, ha,
805                                           "Freeing DDB index = 0x%x\n", idx));
806                         ret = qla4xxx_clear_ddb_entry(ha, idx);
807                         if (ret == QLA_ERROR)
808                                 ql4_printk(KERN_ERR, ha,
809                                            "Unable to clear DDB index = "
810                                            "0x%x\n", idx);
811                 }
812                 if (next_idx == 0)
813                         break;
814         }
815 }
816
817 /**
818  * qla4xxx_initialize_adapter - initiailizes hba
819  * @ha: Pointer to host adapter structure.
820  *
821  * This routine parforms all of the steps necessary to initialize the adapter.
822  *
823  **/
824 int qla4xxx_initialize_adapter(struct scsi_qla_host *ha, int is_reset)
825 {
826         int status = QLA_ERROR;
827
828         ha->eeprom_cmd_data = 0;
829
830         ql4_printk(KERN_INFO, ha, "Configuring PCI space...\n");
831         ha->isp_ops->pci_config(ha);
832
833         ha->isp_ops->disable_intrs(ha);
834
835         /* Initialize the Host adapter request/response queues and firmware */
836         if (ha->isp_ops->start_firmware(ha) == QLA_ERROR)
837                 goto exit_init_hba;
838
839         if (qla4xxx_about_firmware(ha) == QLA_ERROR)
840                 goto exit_init_hba;
841
842         if (ha->isp_ops->get_sys_info(ha) == QLA_ERROR)
843                 goto exit_init_hba;
844
845         if (qla4xxx_init_local_data(ha) == QLA_ERROR)
846                 goto exit_init_hba;
847
848         status = qla4xxx_init_firmware(ha);
849         if (status == QLA_ERROR)
850                 goto exit_init_hba;
851
852         if (is_reset == RESET_ADAPTER)
853                 qla4xxx_build_ddb_list(ha, is_reset);
854
855         set_bit(AF_ONLINE, &ha->flags);
856 exit_init_hba:
857         if (is_qla8022(ha) && (status == QLA_ERROR)) {
858                 /* Since interrupts are registered in start_firmware for
859                  * 82xx, release them here if initialize_adapter fails */
860                 qla4xxx_free_irqs(ha);
861         }
862
863         DEBUG2(printk("scsi%ld: initialize adapter: %s\n", ha->host_no,
864             status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
865         return status;
866 }
867
868 int qla4xxx_ddb_change(struct scsi_qla_host *ha, uint32_t fw_ddb_index,
869                        struct ddb_entry *ddb_entry, uint32_t state)
870 {
871         uint32_t old_fw_ddb_device_state;
872         int status = QLA_ERROR;
873
874         old_fw_ddb_device_state = ddb_entry->fw_ddb_device_state;
875         DEBUG2(ql4_printk(KERN_INFO, ha,
876                           "%s: DDB - old state = 0x%x, new state = 0x%x for "
877                           "index [%d]\n", __func__,
878                           ddb_entry->fw_ddb_device_state, state, fw_ddb_index));
879
880         ddb_entry->fw_ddb_device_state = state;
881
882         switch (old_fw_ddb_device_state) {
883         case DDB_DS_LOGIN_IN_PROCESS:
884                 switch (state) {
885                 case DDB_DS_SESSION_ACTIVE:
886                 case DDB_DS_DISCOVERY:
887                         ddb_entry->unblock_sess(ddb_entry->sess);
888                         qla4xxx_update_session_conn_param(ha, ddb_entry);
889                         status = QLA_SUCCESS;
890                         break;
891                 case DDB_DS_SESSION_FAILED:
892                 case DDB_DS_NO_CONNECTION_ACTIVE:
893                         iscsi_conn_login_event(ddb_entry->conn,
894                                                ISCSI_CONN_STATE_FREE);
895                         status = QLA_SUCCESS;
896                         break;
897                 }
898                 break;
899         case DDB_DS_SESSION_ACTIVE:
900                 switch (state) {
901                 case DDB_DS_SESSION_FAILED:
902                         /*
903                          * iscsi_session failure  will cause userspace to
904                          * stop the connection which in turn would block the
905                          * iscsi_session and start relogin
906                          */
907                         iscsi_session_failure(ddb_entry->sess->dd_data,
908                                               ISCSI_ERR_CONN_FAILED);
909                         status = QLA_SUCCESS;
910                         break;
911                 case DDB_DS_NO_CONNECTION_ACTIVE:
912                         clear_bit(fw_ddb_index, ha->ddb_idx_map);
913                         status = QLA_SUCCESS;
914                         break;
915                 }
916                 break;
917         case DDB_DS_SESSION_FAILED:
918                 switch (state) {
919                 case DDB_DS_SESSION_ACTIVE:
920                 case DDB_DS_DISCOVERY:
921                         ddb_entry->unblock_sess(ddb_entry->sess);
922                         qla4xxx_update_session_conn_param(ha, ddb_entry);
923                         status = QLA_SUCCESS;
924                         break;
925                 case DDB_DS_SESSION_FAILED:
926                         iscsi_session_failure(ddb_entry->sess->dd_data,
927                                               ISCSI_ERR_CONN_FAILED);
928                         status = QLA_SUCCESS;
929                         break;
930                 }
931                 break;
932         default:
933                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Unknown Event\n",
934                                 __func__));
935                 break;
936         }
937         return status;
938 }
939
940 void qla4xxx_arm_relogin_timer(struct ddb_entry *ddb_entry)
941 {
942         /*
943          * This triggers a relogin.  After the relogin_timer
944          * expires, the relogin gets scheduled.  We must wait a
945          * minimum amount of time since receiving an 0x8014 AEN
946          * with failed device_state or a logout response before
947          * we can issue another relogin.
948          *
949          * Firmware pads this timeout: (time2wait +1).
950          * Driver retry to login should be longer than F/W.
951          * Otherwise F/W will fail
952          * set_ddb() mbx cmd with 0x4005 since it still
953          * counting down its time2wait.
954          */
955         atomic_set(&ddb_entry->relogin_timer, 0);
956         atomic_set(&ddb_entry->retry_relogin_timer,
957                    ddb_entry->default_time2wait + 4);
958
959 }
960
961 int qla4xxx_flash_ddb_change(struct scsi_qla_host *ha, uint32_t fw_ddb_index,
962                              struct ddb_entry *ddb_entry, uint32_t state)
963 {
964         uint32_t old_fw_ddb_device_state;
965         int status = QLA_ERROR;
966
967         old_fw_ddb_device_state = ddb_entry->fw_ddb_device_state;
968         DEBUG2(ql4_printk(KERN_INFO, ha,
969                           "%s: DDB - old state = 0x%x, new state = 0x%x for "
970                           "index [%d]\n", __func__,
971                           ddb_entry->fw_ddb_device_state, state, fw_ddb_index));
972
973         ddb_entry->fw_ddb_device_state = state;
974
975         switch (old_fw_ddb_device_state) {
976         case DDB_DS_LOGIN_IN_PROCESS:
977         case DDB_DS_NO_CONNECTION_ACTIVE:
978                 switch (state) {
979                 case DDB_DS_SESSION_ACTIVE:
980                         ddb_entry->unblock_sess(ddb_entry->sess);
981                         qla4xxx_update_session_conn_fwddb_param(ha, ddb_entry);
982                         status = QLA_SUCCESS;
983                         break;
984                 case DDB_DS_SESSION_FAILED:
985                         iscsi_block_session(ddb_entry->sess);
986                         if (!test_bit(DF_RELOGIN, &ddb_entry->flags))
987                                 qla4xxx_arm_relogin_timer(ddb_entry);
988                         status = QLA_SUCCESS;
989                         break;
990                 }
991                 break;
992         case DDB_DS_SESSION_ACTIVE:
993                 switch (state) {
994                 case DDB_DS_SESSION_FAILED:
995                         iscsi_block_session(ddb_entry->sess);
996                         if (!test_bit(DF_RELOGIN, &ddb_entry->flags))
997                                 qla4xxx_arm_relogin_timer(ddb_entry);
998                         status = QLA_SUCCESS;
999                         break;
1000                 }
1001                 break;
1002         case DDB_DS_SESSION_FAILED:
1003                 switch (state) {
1004                 case DDB_DS_SESSION_ACTIVE:
1005                         ddb_entry->unblock_sess(ddb_entry->sess);
1006                         qla4xxx_update_session_conn_fwddb_param(ha, ddb_entry);
1007                         status = QLA_SUCCESS;
1008                         break;
1009                 case DDB_DS_SESSION_FAILED:
1010                         if (!test_bit(DF_RELOGIN, &ddb_entry->flags))
1011                                 qla4xxx_arm_relogin_timer(ddb_entry);
1012                         status = QLA_SUCCESS;
1013                         break;
1014                 }
1015                 break;
1016         default:
1017                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Unknown Event\n",
1018                                   __func__));
1019                 break;
1020         }
1021         return status;
1022 }
1023
1024 /**
1025  * qla4xxx_process_ddb_changed - process ddb state change
1026  * @ha - Pointer to host adapter structure.
1027  * @fw_ddb_index - Firmware's device database index
1028  * @state - Device state
1029  *
1030  * This routine processes a Decive Database Changed AEN Event.
1031  **/
1032 int qla4xxx_process_ddb_changed(struct scsi_qla_host *ha,
1033                                 uint32_t fw_ddb_index,
1034                                 uint32_t state, uint32_t conn_err)
1035 {
1036         struct ddb_entry *ddb_entry;
1037         int status = QLA_ERROR;
1038
1039         /* check for out of range index */
1040         if (fw_ddb_index >= MAX_DDB_ENTRIES)
1041                 goto exit_ddb_event;
1042
1043         /* Get the corresponging ddb entry */
1044         ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, fw_ddb_index);
1045         /* Device does not currently exist in our database. */
1046         if (ddb_entry == NULL) {
1047                 ql4_printk(KERN_ERR, ha, "%s: No ddb_entry at FW index [%d]\n",
1048                            __func__, fw_ddb_index);
1049
1050                 if (state == DDB_DS_NO_CONNECTION_ACTIVE)
1051                         clear_bit(fw_ddb_index, ha->ddb_idx_map);
1052
1053                 goto exit_ddb_event;
1054         }
1055
1056         ddb_entry->ddb_change(ha, fw_ddb_index, ddb_entry, state);
1057
1058 exit_ddb_event:
1059         return status;
1060 }
1061
1062 /**
1063  * qla4xxx_login_flash_ddb - Login to target (DDB)
1064  * @cls_session: Pointer to the session to login
1065  *
1066  * This routine logins to the target.
1067  * Issues setddb and conn open mbx
1068  **/
1069 void qla4xxx_login_flash_ddb(struct iscsi_cls_session *cls_session)
1070 {
1071         struct iscsi_session *sess;
1072         struct ddb_entry *ddb_entry;
1073         struct scsi_qla_host *ha;
1074         struct dev_db_entry *fw_ddb_entry = NULL;
1075         dma_addr_t fw_ddb_dma;
1076         uint32_t mbx_sts = 0;
1077         int ret;
1078
1079         sess = cls_session->dd_data;
1080         ddb_entry = sess->dd_data;
1081         ha =  ddb_entry->ha;
1082
1083         if (!test_bit(AF_LINK_UP, &ha->flags))
1084                 return;
1085
1086         if (ddb_entry->ddb_type != FLASH_DDB) {
1087                 DEBUG2(ql4_printk(KERN_INFO, ha,
1088                                   "Skipping login to non FLASH DB"));
1089                 goto exit_login;
1090         }
1091
1092         fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
1093                                       &fw_ddb_dma);
1094         if (fw_ddb_entry == NULL) {
1095                 DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
1096                 goto exit_login;
1097         }
1098
1099         if (ddb_entry->fw_ddb_index == INVALID_ENTRY) {
1100                 ret = qla4xxx_get_ddb_index(ha, &ddb_entry->fw_ddb_index);
1101                 if (ret == QLA_ERROR)
1102                         goto exit_login;
1103
1104                 ha->fw_ddb_index_map[ddb_entry->fw_ddb_index] = ddb_entry;
1105                 ha->tot_ddbs++;
1106         }
1107
1108         memcpy(fw_ddb_entry, &ddb_entry->fw_ddb_entry,
1109                sizeof(struct dev_db_entry));
1110         ddb_entry->sess->target_id = ddb_entry->fw_ddb_index;
1111
1112         ret = qla4xxx_set_ddb_entry(ha, ddb_entry->fw_ddb_index,
1113                                     fw_ddb_dma, &mbx_sts);
1114         if (ret == QLA_ERROR) {
1115                 DEBUG2(ql4_printk(KERN_ERR, ha, "Set DDB failed\n"));
1116                 goto exit_login;
1117         }
1118
1119         ddb_entry->fw_ddb_device_state = DDB_DS_LOGIN_IN_PROCESS;
1120         ret = qla4xxx_conn_open(ha, ddb_entry->fw_ddb_index);
1121         if (ret == QLA_ERROR) {
1122                 ql4_printk(KERN_ERR, ha, "%s: Login failed: %s\n", __func__,
1123                            sess->targetname);
1124                 goto exit_login;
1125         }
1126
1127 exit_login:
1128         if (fw_ddb_entry)
1129                 dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
1130 }
1131