]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/scsi/qla4xxx/ql4_init.c
hwmon: (via-cputemp) Remove bogus "SHOW" global variable
[net-next-2.6.git] / drivers / scsi / qla4xxx / ql4_init.c
1 /*
2  * QLogic iSCSI HBA Driver
3  * Copyright (c)  2003-2006 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 struct ddb_entry *qla4xxx_alloc_ddb(struct scsi_qla_host *ha,
15                                            uint32_t fw_ddb_index);
16
17 static void ql4xxx_set_mac_number(struct scsi_qla_host *ha)
18 {
19         uint32_t value;
20         uint8_t func_number;
21         unsigned long flags;
22
23         /* Get the function number */
24         spin_lock_irqsave(&ha->hardware_lock, flags);
25         value = readw(&ha->reg->ctrl_status);
26         spin_unlock_irqrestore(&ha->hardware_lock, flags);
27
28         func_number = (uint8_t) ((value >> 4) & 0x30);
29         switch (value & ISP_CONTROL_FN_MASK) {
30         case ISP_CONTROL_FN0_SCSI:
31                 ha->mac_index = 1;
32                 break;
33         case ISP_CONTROL_FN1_SCSI:
34                 ha->mac_index = 3;
35                 break;
36         default:
37                 DEBUG2(printk("scsi%ld: %s: Invalid function number, "
38                               "ispControlStatus = 0x%x\n", ha->host_no,
39                               __func__, value));
40                 break;
41         }
42         DEBUG2(printk("scsi%ld: %s: mac_index %d.\n", ha->host_no, __func__,
43                       ha->mac_index));
44 }
45
46 /**
47  * qla4xxx_free_ddb - deallocate ddb
48  * @ha: pointer to host adapter structure.
49  * @ddb_entry: pointer to device database entry
50  *
51  * This routine deallocates and unlinks the specified ddb_entry from the
52  * adapter's
53  **/
54 void qla4xxx_free_ddb(struct scsi_qla_host *ha,
55     struct ddb_entry *ddb_entry)
56 {
57         /* Remove device entry from list */
58         list_del_init(&ddb_entry->list);
59
60         /* Remove device pointer from index mapping arrays */
61         ha->fw_ddb_index_map[ddb_entry->fw_ddb_index] =
62                 (struct ddb_entry *) INVALID_ENTRY;
63         ha->tot_ddbs--;
64
65         /* Free memory and scsi-ml struct for device entry */
66         qla4xxx_destroy_sess(ddb_entry);
67 }
68
69 /**
70  * qla4xxx_free_ddb_list - deallocate all ddbs
71  * @ha: pointer to host adapter structure.
72  *
73  * This routine deallocates and removes all devices on the sppecified adapter.
74  **/
75 void qla4xxx_free_ddb_list(struct scsi_qla_host *ha)
76 {
77         struct list_head *ptr;
78         struct ddb_entry *ddb_entry;
79
80         while (!list_empty(&ha->ddb_list)) {
81                 ptr = ha->ddb_list.next;
82                 /* Free memory for device entry and remove */
83                 ddb_entry = list_entry(ptr, struct ddb_entry, list);
84                 qla4xxx_free_ddb(ha, ddb_entry);
85         }
86 }
87
88 /**
89  * qla4xxx_init_response_q_entries() - Initializes response queue entries.
90  * @ha: HA context
91  *
92  * Beginning of request ring has initialization control block already built
93  * by nvram config routine.
94  **/
95 static void qla4xxx_init_response_q_entries(struct scsi_qla_host *ha)
96 {
97         uint16_t cnt;
98         struct response *pkt;
99
100         pkt = (struct response *)ha->response_ptr;
101         for (cnt = 0; cnt < RESPONSE_QUEUE_DEPTH; cnt++) {
102                 pkt->signature = RESPONSE_PROCESSED;
103                 pkt++;
104         }
105 }
106
107 /**
108  * qla4xxx_init_rings - initialize hw queues
109  * @ha: pointer to host adapter structure.
110  *
111  * This routine initializes the internal queues for the specified adapter.
112  * The QLA4010 requires us to restart the queues at index 0.
113  * The QLA4000 doesn't care, so just default to QLA4010's requirement.
114  **/
115 int qla4xxx_init_rings(struct scsi_qla_host *ha)
116 {
117         unsigned long flags = 0;
118
119         /* Initialize request queue. */
120         spin_lock_irqsave(&ha->hardware_lock, flags);
121         ha->request_out = 0;
122         ha->request_in = 0;
123         ha->request_ptr = &ha->request_ring[ha->request_in];
124         ha->req_q_count = REQUEST_QUEUE_DEPTH;
125
126         /* Initialize response queue. */
127         ha->response_in = 0;
128         ha->response_out = 0;
129         ha->response_ptr = &ha->response_ring[ha->response_out];
130
131         if (is_qla8022(ha)) {
132                 writel(0,
133                     (unsigned long  __iomem *)&ha->qla4_8xxx_reg->req_q_out);
134                 writel(0,
135                     (unsigned long  __iomem *)&ha->qla4_8xxx_reg->rsp_q_in);
136                 writel(0,
137                     (unsigned long  __iomem *)&ha->qla4_8xxx_reg->rsp_q_out);
138         } else {
139                 /*
140                  * Initialize DMA Shadow registers.  The firmware is really
141                  * supposed to take care of this, but on some uniprocessor
142                  * systems, the shadow registers aren't cleared-- causing
143                  * the interrupt_handler to think there are responses to be
144                  * processed when there aren't.
145                  */
146                 ha->shadow_regs->req_q_out = __constant_cpu_to_le32(0);
147                 ha->shadow_regs->rsp_q_in = __constant_cpu_to_le32(0);
148                 wmb();
149
150                 writel(0, &ha->reg->req_q_in);
151                 writel(0, &ha->reg->rsp_q_out);
152                 readl(&ha->reg->rsp_q_out);
153         }
154
155         qla4xxx_init_response_q_entries(ha);
156
157         spin_unlock_irqrestore(&ha->hardware_lock, flags);
158
159         return QLA_SUCCESS;
160 }
161
162 /**
163  * qla4xxx_get_sys_info - validate adapter MAC address(es)
164  * @ha: pointer to host adapter structure.
165  *
166  **/
167 int qla4xxx_get_sys_info(struct scsi_qla_host *ha)
168 {
169         struct flash_sys_info *sys_info;
170         dma_addr_t sys_info_dma;
171         int status = QLA_ERROR;
172
173         sys_info = dma_alloc_coherent(&ha->pdev->dev, sizeof(*sys_info),
174                                       &sys_info_dma, GFP_KERNEL);
175         if (sys_info == NULL) {
176                 DEBUG2(printk("scsi%ld: %s: Unable to allocate dma buffer.\n",
177                               ha->host_no, __func__));
178
179                 goto exit_get_sys_info_no_free;
180         }
181         memset(sys_info, 0, sizeof(*sys_info));
182
183         /* Get flash sys info */
184         if (qla4xxx_get_flash(ha, sys_info_dma, FLASH_OFFSET_SYS_INFO,
185                               sizeof(*sys_info)) != QLA_SUCCESS) {
186                 DEBUG2(printk("scsi%ld: %s: get_flash FLASH_OFFSET_SYS_INFO "
187                               "failed\n", ha->host_no, __func__));
188
189                 goto exit_get_sys_info;
190         }
191
192         /* Save M.A.C. address & serial_number */
193         memcpy(ha->my_mac, &sys_info->physAddr[0].address[0],
194                min(sizeof(ha->my_mac),
195                    sizeof(sys_info->physAddr[0].address)));
196         memcpy(ha->serial_number, &sys_info->acSerialNumber,
197                min(sizeof(ha->serial_number),
198                    sizeof(sys_info->acSerialNumber)));
199
200         status = QLA_SUCCESS;
201
202 exit_get_sys_info:
203         dma_free_coherent(&ha->pdev->dev, sizeof(*sys_info), sys_info,
204                           sys_info_dma);
205
206 exit_get_sys_info_no_free:
207         return status;
208 }
209
210 /**
211  * qla4xxx_init_local_data - initialize adapter specific local data
212  * @ha: pointer to host adapter structure.
213  *
214  **/
215 static int qla4xxx_init_local_data(struct scsi_qla_host *ha)
216 {
217         /* Initialize aen queue */
218         ha->aen_q_count = MAX_AEN_ENTRIES;
219
220         return qla4xxx_get_firmware_status(ha);
221 }
222
223 static uint8_t
224 qla4xxx_wait_for_ip_config(struct scsi_qla_host *ha)
225 {
226         uint8_t ipv4_wait = 0;
227         uint8_t ipv6_wait = 0;
228         int8_t ip_address[IPv6_ADDR_LEN] = {0} ;
229
230         /* If both IPv4 & IPv6 are enabled, possibly only one
231          * IP address may be acquired, so check to see if we
232          * need to wait for another */
233         if (is_ipv4_enabled(ha) && is_ipv6_enabled(ha)) {
234                 if (((ha->addl_fw_state & FW_ADDSTATE_DHCPv4_ENABLED) != 0) &&
235                     ((ha->addl_fw_state &
236                                     FW_ADDSTATE_DHCPv4_LEASE_ACQUIRED) == 0)) {
237                         ipv4_wait = 1;
238                 }
239                 if (((ha->ipv6_addl_options &
240                             IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) != 0) &&
241                     ((ha->ipv6_link_local_state == IP_ADDRSTATE_ACQUIRING) ||
242                      (ha->ipv6_addr0_state == IP_ADDRSTATE_ACQUIRING) ||
243                      (ha->ipv6_addr1_state == IP_ADDRSTATE_ACQUIRING))) {
244
245                         ipv6_wait = 1;
246
247                         if ((ha->ipv6_link_local_state ==
248                                                      IP_ADDRSTATE_PREFERRED) ||
249                             (ha->ipv6_addr0_state == IP_ADDRSTATE_PREFERRED) ||
250                             (ha->ipv6_addr1_state == IP_ADDRSTATE_PREFERRED)) {
251                                 DEBUG2(printk(KERN_INFO "scsi%ld: %s: "
252                                               "Preferred IP configured."
253                                               " Don't wait!\n", ha->host_no,
254                                               __func__));
255                                 ipv6_wait = 0;
256                         }
257                         if (memcmp(&ha->ipv6_default_router_addr, ip_address,
258                                 IPv6_ADDR_LEN) == 0) {
259                                 DEBUG2(printk(KERN_INFO "scsi%ld: %s: "
260                                               "No Router configured. "
261                                               "Don't wait!\n", ha->host_no,
262                                               __func__));
263                                 ipv6_wait = 0;
264                         }
265                         if ((ha->ipv6_default_router_state ==
266                                                 IPV6_RTRSTATE_MANUAL) &&
267                             (ha->ipv6_link_local_state ==
268                                                 IP_ADDRSTATE_TENTATIVE) &&
269                             (memcmp(&ha->ipv6_link_local_addr,
270                                     &ha->ipv6_default_router_addr, 4) == 0)) {
271                                 DEBUG2(printk("scsi%ld: %s: LinkLocal Router & "
272                                         "IP configured. Don't wait!\n",
273                                         ha->host_no, __func__));
274                                 ipv6_wait = 0;
275                         }
276                 }
277                 if (ipv4_wait || ipv6_wait) {
278                         DEBUG2(printk("scsi%ld: %s: Wait for additional "
279                                       "IP(s) \"", ha->host_no, __func__));
280                         if (ipv4_wait)
281                                 DEBUG2(printk("IPv4 "));
282                         if (ha->ipv6_link_local_state == IP_ADDRSTATE_ACQUIRING)
283                                 DEBUG2(printk("IPv6LinkLocal "));
284                         if (ha->ipv6_addr0_state == IP_ADDRSTATE_ACQUIRING)
285                                 DEBUG2(printk("IPv6Addr0 "));
286                         if (ha->ipv6_addr1_state == IP_ADDRSTATE_ACQUIRING)
287                                 DEBUG2(printk("IPv6Addr1 "));
288                         DEBUG2(printk("\"\n"));
289                 }
290         }
291
292         return ipv4_wait|ipv6_wait;
293 }
294
295 static int qla4xxx_fw_ready(struct scsi_qla_host *ha)
296 {
297         uint32_t timeout_count;
298         int ready = 0;
299
300         DEBUG2(ql4_printk(KERN_INFO, ha, "Waiting for Firmware Ready..\n"));
301         for (timeout_count = ADAPTER_INIT_TOV; timeout_count > 0;
302              timeout_count--) {
303                 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
304                         qla4xxx_get_dhcp_ip_address(ha);
305
306                 /* Get firmware state. */
307                 if (qla4xxx_get_firmware_state(ha) != QLA_SUCCESS) {
308                         DEBUG2(printk("scsi%ld: %s: unable to get firmware "
309                                       "state\n", ha->host_no, __func__));
310                         break;
311
312                 }
313
314                 if (ha->firmware_state & FW_STATE_ERROR) {
315                         DEBUG2(printk("scsi%ld: %s: an unrecoverable error has"
316                                       " occurred\n", ha->host_no, __func__));
317                         break;
318
319                 }
320                 if (ha->firmware_state & FW_STATE_CONFIG_WAIT) {
321                         /*
322                          * The firmware has not yet been issued an Initialize
323                          * Firmware command, so issue it now.
324                          */
325                         if (qla4xxx_initialize_fw_cb(ha) == QLA_ERROR)
326                                 break;
327
328                         /* Go back and test for ready state - no wait. */
329                         continue;
330                 }
331
332                 if (ha->firmware_state & FW_STATE_WAIT_AUTOCONNECT) {
333                         DEBUG2(printk(KERN_INFO "scsi%ld: %s: fwstate:"
334                                       "AUTOCONNECT in progress\n",
335                                       ha->host_no, __func__));
336                 }
337
338                 if (ha->firmware_state & FW_STATE_CONFIGURING_IP) {
339                         DEBUG2(printk(KERN_INFO "scsi%ld: %s: fwstate:"
340                                       " CONFIGURING IP\n",
341                                       ha->host_no, __func__));
342                         /*
343                          * Check for link state after 15 secs and if link is
344                          * still DOWN then, cable is unplugged. Ignore "DHCP
345                          * in Progress/CONFIGURING IP" bit to check if firmware
346                          * is in ready state or not after 15 secs.
347                          * This is applicable for both 2.x & 3.x firmware
348                          */
349                         if (timeout_count <= (ADAPTER_INIT_TOV - 15)) {
350                                 if (ha->addl_fw_state & FW_ADDSTATE_LINK_UP) {
351                                         DEBUG2(printk(KERN_INFO "scsi%ld: %s:"
352                                                   " LINK UP (Cable plugged)\n",
353                                                   ha->host_no, __func__));
354                                 } else if (ha->firmware_state &
355                                           (FW_STATE_CONFIGURING_IP |
356                                                              FW_STATE_READY)) {
357                                         DEBUG2(printk(KERN_INFO "scsi%ld: %s: "
358                                                 "LINK DOWN (Cable unplugged)\n",
359                                                 ha->host_no, __func__));
360                                         ha->firmware_state = FW_STATE_READY;
361                                 }
362                         }
363                 }
364
365                 if (ha->firmware_state == FW_STATE_READY) {
366                         /* If DHCP IP Addr is available, retrieve it now. */
367                         if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR,
368                                                                 &ha->dpc_flags))
369                                 qla4xxx_get_dhcp_ip_address(ha);
370
371                         if (!qla4xxx_wait_for_ip_config(ha) ||
372                                                         timeout_count == 1) {
373                                 DEBUG2(ql4_printk(KERN_INFO, ha,
374                                     "Firmware Ready..\n"));
375                                 /* The firmware is ready to process SCSI
376                                    commands. */
377                                 DEBUG2(ql4_printk(KERN_INFO, ha,
378                                         "scsi%ld: %s: MEDIA TYPE"
379                                         " - %s\n", ha->host_no,
380                                         __func__, (ha->addl_fw_state &
381                                         FW_ADDSTATE_OPTICAL_MEDIA)
382                                         != 0 ? "OPTICAL" : "COPPER"));
383                                 DEBUG2(ql4_printk(KERN_INFO, ha,
384                                         "scsi%ld: %s: DHCPv4 STATE"
385                                         " Enabled %s\n", ha->host_no,
386                                          __func__, (ha->addl_fw_state &
387                                          FW_ADDSTATE_DHCPv4_ENABLED) != 0 ?
388                                         "YES" : "NO"));
389                                 DEBUG2(ql4_printk(KERN_INFO, ha,
390                                         "scsi%ld: %s: LINK %s\n",
391                                         ha->host_no, __func__,
392                                         (ha->addl_fw_state &
393                                          FW_ADDSTATE_LINK_UP) != 0 ?
394                                         "UP" : "DOWN"));
395                                 DEBUG2(ql4_printk(KERN_INFO, ha,
396                                         "scsi%ld: %s: iSNS Service "
397                                         "Started %s\n",
398                                         ha->host_no, __func__,
399                                         (ha->addl_fw_state &
400                                          FW_ADDSTATE_ISNS_SVC_ENABLED) != 0 ?
401                                         "YES" : "NO"));
402
403                                 ready = 1;
404                                 break;
405                         }
406                 }
407                 DEBUG2(printk("scsi%ld: %s: waiting on fw, state=%x:%x - "
408                               "seconds expired= %d\n", ha->host_no, __func__,
409                               ha->firmware_state, ha->addl_fw_state,
410                               timeout_count));
411                 if (is_qla4032(ha) &&
412                         !(ha->addl_fw_state & FW_ADDSTATE_LINK_UP) &&
413                         (timeout_count < ADAPTER_INIT_TOV - 5)) {
414                         break;
415                 }
416
417                 msleep(1000);
418         }                       /* end of for */
419
420         if (timeout_count <= 0)
421                 DEBUG2(printk("scsi%ld: %s: FW Initialization timed out!\n",
422                               ha->host_no, __func__));
423
424         if (ha->firmware_state & FW_STATE_CONFIGURING_IP) {
425                 DEBUG2(printk("scsi%ld: %s: FW initialized, but is reporting "
426                               "it's waiting to configure an IP address\n",
427                                ha->host_no, __func__));
428                 ready = 1;
429         } else if (ha->firmware_state & FW_STATE_WAIT_AUTOCONNECT) {
430                 DEBUG2(printk("scsi%ld: %s: FW initialized, but "
431                               "auto-discovery still in process\n",
432                                ha->host_no, __func__));
433                 ready = 1;
434         }
435
436         return ready;
437 }
438
439 /**
440  * qla4xxx_init_firmware - initializes the firmware.
441  * @ha: pointer to host adapter structure.
442  *
443  **/
444 static int qla4xxx_init_firmware(struct scsi_qla_host *ha)
445 {
446         int status = QLA_ERROR;
447
448         ql4_printk(KERN_INFO, ha, "Initializing firmware..\n");
449         if (qla4xxx_initialize_fw_cb(ha) == QLA_ERROR) {
450                 DEBUG2(printk("scsi%ld: %s: Failed to initialize firmware "
451                               "control block\n", ha->host_no, __func__));
452                 return status;
453         }
454         if (!qla4xxx_fw_ready(ha))
455                 return status;
456
457         return qla4xxx_get_firmware_status(ha);
458 }
459
460 static struct ddb_entry* qla4xxx_get_ddb_entry(struct scsi_qla_host *ha,
461                                                 uint32_t fw_ddb_index,
462                                                 uint32_t *new_tgt)
463 {
464         struct dev_db_entry *fw_ddb_entry = NULL;
465         dma_addr_t fw_ddb_entry_dma;
466         struct ddb_entry *ddb_entry = NULL;
467         int found = 0;
468         uint32_t device_state;
469
470         *new_tgt = 0;
471         /* Make sure the dma buffer is valid */
472         fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev,
473                                           sizeof(*fw_ddb_entry),
474                                           &fw_ddb_entry_dma, GFP_KERNEL);
475         if (fw_ddb_entry == NULL) {
476                 DEBUG2(printk("scsi%ld: %s: Unable to allocate dma buffer.\n",
477                               ha->host_no, __func__));
478                 goto exit_get_ddb_entry_no_free;
479         }
480
481         if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index, fw_ddb_entry,
482                                     fw_ddb_entry_dma, NULL, NULL,
483                                     &device_state, NULL, NULL, NULL) ==
484                                     QLA_ERROR) {
485                 DEBUG2(printk("scsi%ld: %s: failed get_ddb_entry for "
486                               "fw_ddb_index %d\n", ha->host_no, __func__,
487                               fw_ddb_index));
488                 goto exit_get_ddb_entry;
489         }
490
491         /* Allocate DDB if not already allocated. */
492         DEBUG2(printk("scsi%ld: %s: Looking for ddb[%d]\n", ha->host_no,
493                       __func__, fw_ddb_index));
494         list_for_each_entry(ddb_entry, &ha->ddb_list, list) {
495                 if ((memcmp(ddb_entry->iscsi_name, fw_ddb_entry->iscsi_name,
496                            ISCSI_NAME_SIZE) == 0) &&
497                         (ddb_entry->tpgt ==
498                                 le32_to_cpu(fw_ddb_entry->tgt_portal_grp)) &&
499                         (memcmp(ddb_entry->isid, fw_ddb_entry->isid,
500                                 sizeof(ddb_entry->isid)) == 0)) {
501                         found++;
502                         break;
503                 }
504         }
505
506         /* if not found allocate new ddb */
507         if (!found) {
508                 DEBUG2(printk("scsi%ld: %s: ddb[%d] not found - allocating "
509                               "new ddb\n", ha->host_no, __func__,
510                               fw_ddb_index));
511                 *new_tgt = 1;
512                 ddb_entry = qla4xxx_alloc_ddb(ha, fw_ddb_index);
513         }
514
515 exit_get_ddb_entry:
516         dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry), fw_ddb_entry,
517                           fw_ddb_entry_dma);
518
519 exit_get_ddb_entry_no_free:
520         return ddb_entry;
521 }
522
523 /**
524  * qla4xxx_update_ddb_entry - update driver's internal ddb
525  * @ha: pointer to host adapter structure.
526  * @ddb_entry: pointer to device database structure to be filled
527  * @fw_ddb_index: index of the ddb entry in fw ddb table
528  *
529  * This routine updates the driver's internal device database entry
530  * with information retrieved from the firmware's device database
531  * entry for the specified device. The ddb_entry->fw_ddb_index field
532  * must be initialized prior to calling this routine
533  *
534  **/
535 static int qla4xxx_update_ddb_entry(struct scsi_qla_host *ha,
536                                     struct ddb_entry *ddb_entry,
537                                     uint32_t fw_ddb_index)
538 {
539         struct dev_db_entry *fw_ddb_entry = NULL;
540         dma_addr_t fw_ddb_entry_dma;
541         int status = QLA_ERROR;
542         uint32_t conn_err;
543
544         if (ddb_entry == NULL) {
545                 DEBUG2(printk("scsi%ld: %s: ddb_entry is NULL\n", ha->host_no,
546                               __func__));
547
548                 goto exit_update_ddb_no_free;
549         }
550
551         /* Make sure the dma buffer is valid */
552         fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev,
553                                           sizeof(*fw_ddb_entry),
554                                           &fw_ddb_entry_dma, GFP_KERNEL);
555         if (fw_ddb_entry == NULL) {
556                 DEBUG2(printk("scsi%ld: %s: Unable to allocate dma buffer.\n",
557                               ha->host_no, __func__));
558
559                 goto exit_update_ddb_no_free;
560         }
561
562         if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index, fw_ddb_entry,
563                                     fw_ddb_entry_dma, NULL, NULL,
564                                     &ddb_entry->fw_ddb_device_state, &conn_err,
565                                     &ddb_entry->tcp_source_port_num,
566                                     &ddb_entry->connection_id) ==
567                                     QLA_ERROR) {
568                 DEBUG2(printk("scsi%ld: %s: failed get_ddb_entry for "
569                               "fw_ddb_index %d\n", ha->host_no, __func__,
570                               fw_ddb_index));
571
572                 goto exit_update_ddb;
573         }
574
575         status = QLA_SUCCESS;
576         ddb_entry->options = le16_to_cpu(fw_ddb_entry->options);
577         ddb_entry->target_session_id = le16_to_cpu(fw_ddb_entry->tsid);
578         ddb_entry->task_mgmt_timeout =
579                 le16_to_cpu(fw_ddb_entry->def_timeout);
580         ddb_entry->CmdSn = 0;
581         ddb_entry->exe_throttle = le16_to_cpu(fw_ddb_entry->exec_throttle);
582         ddb_entry->default_relogin_timeout =
583                 le16_to_cpu(fw_ddb_entry->def_timeout);
584         ddb_entry->default_time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
585
586         /* Update index in case it changed */
587         ddb_entry->fw_ddb_index = fw_ddb_index;
588         ha->fw_ddb_index_map[fw_ddb_index] = ddb_entry;
589
590         ddb_entry->port = le16_to_cpu(fw_ddb_entry->port);
591         ddb_entry->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
592         memcpy(ddb_entry->isid, fw_ddb_entry->isid, sizeof(ddb_entry->isid));
593
594         memcpy(&ddb_entry->iscsi_name[0], &fw_ddb_entry->iscsi_name[0],
595                min(sizeof(ddb_entry->iscsi_name),
596                    sizeof(fw_ddb_entry->iscsi_name)));
597         memcpy(&ddb_entry->iscsi_alias[0], &fw_ddb_entry->iscsi_alias[0],
598                min(sizeof(ddb_entry->iscsi_alias),
599                    sizeof(fw_ddb_entry->iscsi_alias)));
600         memcpy(&ddb_entry->ip_addr[0], &fw_ddb_entry->ip_addr[0],
601                min(sizeof(ddb_entry->ip_addr), sizeof(fw_ddb_entry->ip_addr)));
602
603         ddb_entry->iscsi_max_burst_len = fw_ddb_entry->iscsi_max_burst_len;
604         ddb_entry->iscsi_max_outsnd_r2t = fw_ddb_entry->iscsi_max_outsnd_r2t;
605         ddb_entry->iscsi_first_burst_len = fw_ddb_entry->iscsi_first_burst_len;
606         ddb_entry->iscsi_max_rcv_data_seg_len =
607                                 fw_ddb_entry->iscsi_max_rcv_data_seg_len;
608         ddb_entry->iscsi_max_snd_data_seg_len =
609                                 fw_ddb_entry->iscsi_max_snd_data_seg_len;
610
611         if (ddb_entry->options & DDB_OPT_IPV6_DEVICE) {
612                 memcpy(&ddb_entry->remote_ipv6_addr,
613                         fw_ddb_entry->ip_addr,
614                         min(sizeof(ddb_entry->remote_ipv6_addr),
615                         sizeof(fw_ddb_entry->ip_addr)));
616                 memcpy(&ddb_entry->link_local_ipv6_addr,
617                         fw_ddb_entry->link_local_ipv6_addr,
618                         min(sizeof(ddb_entry->link_local_ipv6_addr),
619                         sizeof(fw_ddb_entry->link_local_ipv6_addr)));
620
621                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: DDB[%d] State %04x"
622                                         " ConnErr %08x IP %pI6 "
623                                         ":%04d \"%s\"\n",
624                                         __func__, fw_ddb_index,
625                                         ddb_entry->fw_ddb_device_state,
626                                         conn_err, fw_ddb_entry->ip_addr,
627                                         le16_to_cpu(fw_ddb_entry->port),
628                                         fw_ddb_entry->iscsi_name));
629         } else
630                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: DDB[%d] State %04x"
631                                         " ConnErr %08x IP %pI4 "
632                                         ":%04d \"%s\"\n",
633                                         __func__, fw_ddb_index,
634                                         ddb_entry->fw_ddb_device_state,
635                                         conn_err, fw_ddb_entry->ip_addr,
636                                         le16_to_cpu(fw_ddb_entry->port),
637                                         fw_ddb_entry->iscsi_name));
638 exit_update_ddb:
639         if (fw_ddb_entry)
640                 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
641                                   fw_ddb_entry, fw_ddb_entry_dma);
642
643 exit_update_ddb_no_free:
644         return status;
645 }
646
647 /**
648  * qla4xxx_alloc_ddb - allocate device database entry
649  * @ha: Pointer to host adapter structure.
650  * @fw_ddb_index: Firmware's device database index
651  *
652  * This routine allocates a ddb_entry, ititializes some values, and
653  * inserts it into the ddb list.
654  **/
655 static struct ddb_entry * qla4xxx_alloc_ddb(struct scsi_qla_host *ha,
656                                             uint32_t fw_ddb_index)
657 {
658         struct ddb_entry *ddb_entry;
659
660         DEBUG2(printk("scsi%ld: %s: fw_ddb_index [%d]\n", ha->host_no,
661                       __func__, fw_ddb_index));
662
663         ddb_entry = qla4xxx_alloc_sess(ha);
664         if (ddb_entry == NULL) {
665                 DEBUG2(printk("scsi%ld: %s: Unable to allocate memory "
666                               "to add fw_ddb_index [%d]\n",
667                               ha->host_no, __func__, fw_ddb_index));
668                 return ddb_entry;
669         }
670
671         ddb_entry->fw_ddb_index = fw_ddb_index;
672         atomic_set(&ddb_entry->port_down_timer, ha->port_down_retry_count);
673         atomic_set(&ddb_entry->retry_relogin_timer, INVALID_ENTRY);
674         atomic_set(&ddb_entry->relogin_timer, 0);
675         atomic_set(&ddb_entry->relogin_retry_count, 0);
676         atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
677         list_add_tail(&ddb_entry->list, &ha->ddb_list);
678         ha->fw_ddb_index_map[fw_ddb_index] = ddb_entry;
679         ha->tot_ddbs++;
680
681         return ddb_entry;
682 }
683
684 /**
685  * qla4_is_relogin_allowed - Are we allowed to login?
686  * @ha: Pointer to host adapter structure.
687  * @conn_err: Last connection error associated with the ddb
688  *
689  * This routine tests the given connection error to determine if
690  * we are allowed to login.
691  **/
692 int qla4_is_relogin_allowed(struct scsi_qla_host *ha, uint32_t conn_err)
693 {
694         uint32_t err_code, login_rsp_sts_class;
695         int relogin = 1;
696
697         err_code = ((conn_err & 0x00ff0000) >> 16);
698         login_rsp_sts_class = ((conn_err & 0x0000ff00) >> 8);
699         if (err_code == 0x1c || err_code == 0x06) {
700                 DEBUG2(ql4_printk(KERN_INFO, ha,
701                     ": conn_err=0x%08x, send target completed"
702                     " or access denied failure\n", conn_err));
703                 relogin = 0;
704         }
705         if ((err_code == 0x08) && (login_rsp_sts_class == 0x02)) {
706                 /* Login Response PDU returned an error.
707                    Login Response Status in Error Code Detail
708                    indicates login should not be retried.*/
709                 DEBUG2(ql4_printk(KERN_INFO, ha,
710                     ": conn_err=0x%08x, do not retry relogin\n",
711                     conn_err));
712                 relogin = 0;
713         }
714
715         return relogin;
716 }
717
718 /**
719  * qla4xxx_configure_ddbs - builds driver ddb list
720  * @ha: Pointer to host adapter structure.
721  *
722  * This routine searches for all valid firmware ddb entries and builds
723  * an internal ddb list. Ddbs that are considered valid are those with
724  * a device state of SESSION_ACTIVE.
725  **/
726 static int qla4xxx_build_ddb_list(struct scsi_qla_host *ha)
727 {
728         int status = QLA_ERROR;
729         uint32_t fw_ddb_index = 0;
730         uint32_t next_fw_ddb_index = 0;
731         uint32_t ddb_state;
732         uint32_t conn_err;
733         struct ddb_entry *ddb_entry;
734         struct dev_db_entry *fw_ddb_entry = NULL;
735         dma_addr_t fw_ddb_entry_dma;
736         uint32_t ipv6_device;
737         uint32_t new_tgt;
738
739         fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
740                         &fw_ddb_entry_dma, GFP_KERNEL);
741         if (fw_ddb_entry == NULL) {
742                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: DMA alloc failed\n",
743                                 __func__));
744
745                 goto exit_build_ddb_list_no_free;
746         }
747
748         ql4_printk(KERN_INFO, ha, "Initializing DDBs ...\n");
749         for (fw_ddb_index = 0; fw_ddb_index < MAX_DDB_ENTRIES;
750              fw_ddb_index = next_fw_ddb_index) {
751                 /* First, let's see if a device exists here */
752                 if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index, fw_ddb_entry,
753                                             0, NULL, &next_fw_ddb_index,
754                                             &ddb_state, &conn_err,
755                                             NULL, NULL) ==
756                                             QLA_ERROR) {
757                         DEBUG2(printk("scsi%ld: %s: get_ddb_entry, "
758                                       "fw_ddb_index %d failed", ha->host_no,
759                                       __func__, fw_ddb_index));
760                         goto exit_build_ddb_list;
761                 }
762
763                 DEBUG2(printk("scsi%ld: %s: Getting DDB[%d] ddbstate=0x%x, "
764                               "next_fw_ddb_index=%d.\n", ha->host_no, __func__,
765                               fw_ddb_index, ddb_state, next_fw_ddb_index));
766
767                 /* Issue relogin, if necessary. */
768                 if (ddb_state == DDB_DS_SESSION_FAILED ||
769                     ddb_state == DDB_DS_NO_CONNECTION_ACTIVE) {
770                         /* Try and login to device */
771                         DEBUG2(printk("scsi%ld: %s: Login to DDB[%d]\n",
772                                       ha->host_no, __func__, fw_ddb_index));
773                         ipv6_device = le16_to_cpu(fw_ddb_entry->options) &
774                                         DDB_OPT_IPV6_DEVICE;
775                         if (qla4_is_relogin_allowed(ha, conn_err) &&
776                                         ((!ipv6_device &&
777                                           *((uint32_t *)fw_ddb_entry->ip_addr))
778                                          || ipv6_device)) {
779                                 qla4xxx_set_ddb_entry(ha, fw_ddb_index, 0);
780                                 if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index,
781                                                         NULL, 0, NULL,
782                                                         &next_fw_ddb_index,
783                                                         &ddb_state, &conn_err,
784                                                         NULL, NULL)
785                                                 == QLA_ERROR) {
786                                         DEBUG2(printk("scsi%ld: %s:"
787                                                 "get_ddb_entry %d failed\n",
788                                                 ha->host_no,
789                                                 __func__, fw_ddb_index));
790                                         goto exit_build_ddb_list;
791                                 }
792                         }
793                 }
794
795                 if (ddb_state != DDB_DS_SESSION_ACTIVE)
796                         goto next_one;
797                 /*
798                  * if fw_ddb with session active state found,
799                  * add to ddb_list
800                  */
801                 DEBUG2(printk("scsi%ld: %s: DDB[%d] added to list\n",
802                               ha->host_no, __func__, fw_ddb_index));
803
804                 /* Add DDB to internal our ddb list. */
805                 ddb_entry = qla4xxx_get_ddb_entry(ha, fw_ddb_index, &new_tgt);
806                 if (ddb_entry == NULL) {
807                         DEBUG2(printk("scsi%ld: %s: Unable to allocate memory "
808                                       "for device at fw_ddb_index %d\n",
809                                       ha->host_no, __func__, fw_ddb_index));
810                         goto exit_build_ddb_list;
811                 }
812                 /* Fill in the device structure */
813                 if (qla4xxx_update_ddb_entry(ha, ddb_entry, fw_ddb_index) ==
814                     QLA_ERROR) {
815                         ha->fw_ddb_index_map[fw_ddb_index] =
816                                 (struct ddb_entry *)INVALID_ENTRY;
817
818                         DEBUG2(printk("scsi%ld: %s: update_ddb_entry failed "
819                                       "for fw_ddb_index %d.\n",
820                                       ha->host_no, __func__, fw_ddb_index));
821                         goto exit_build_ddb_list;
822                 }
823
824 next_one:
825                 /* We know we've reached the last device when
826                  * next_fw_ddb_index is 0 */
827                 if (next_fw_ddb_index == 0)
828                         break;
829         }
830
831         status = QLA_SUCCESS;
832         ql4_printk(KERN_INFO, ha, "DDB list done..\n");
833
834 exit_build_ddb_list:
835         dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry), fw_ddb_entry,
836                 fw_ddb_entry_dma);
837
838 exit_build_ddb_list_no_free:
839         return status;
840 }
841
842 struct qla4_relog_scan {
843         int halt_wait;
844         uint32_t conn_err;
845         uint32_t fw_ddb_index;
846         uint32_t next_fw_ddb_index;
847         uint32_t fw_ddb_device_state;
848 };
849
850 static int qla4_test_rdy(struct scsi_qla_host *ha, struct qla4_relog_scan *rs)
851 {
852         struct ddb_entry *ddb_entry;
853
854         if (qla4_is_relogin_allowed(ha, rs->conn_err)) {
855                 /* We either have a device that is in
856                  * the process of relogging in or a
857                  * device that is waiting to be
858                  * relogged in */
859                 rs->halt_wait = 0;
860
861                 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha,
862                                                            rs->fw_ddb_index);
863                 if (ddb_entry == NULL)
864                         return QLA_ERROR;
865
866                 if (ddb_entry->dev_scan_wait_to_start_relogin != 0
867                     && time_after_eq(jiffies,
868                                      ddb_entry->
869                                      dev_scan_wait_to_start_relogin))
870                 {
871                         ddb_entry->dev_scan_wait_to_start_relogin = 0;
872                         qla4xxx_set_ddb_entry(ha, rs->fw_ddb_index, 0);
873                 }
874         }
875         return QLA_SUCCESS;
876 }
877
878 static int qla4_scan_for_relogin(struct scsi_qla_host *ha,
879                                  struct qla4_relog_scan *rs)
880 {
881         int error;
882
883         /* scan for relogins
884          * ----------------- */
885         for (rs->fw_ddb_index = 0; rs->fw_ddb_index < MAX_DDB_ENTRIES;
886              rs->fw_ddb_index = rs->next_fw_ddb_index) {
887                 if (qla4xxx_get_fwddb_entry(ha, rs->fw_ddb_index, NULL, 0,
888                                             NULL, &rs->next_fw_ddb_index,
889                                             &rs->fw_ddb_device_state,
890                                             &rs->conn_err, NULL, NULL)
891                     == QLA_ERROR)
892                         return QLA_ERROR;
893
894                 if (rs->fw_ddb_device_state == DDB_DS_LOGIN_IN_PROCESS)
895                         rs->halt_wait = 0;
896
897                 if (rs->fw_ddb_device_state == DDB_DS_SESSION_FAILED ||
898                     rs->fw_ddb_device_state == DDB_DS_NO_CONNECTION_ACTIVE) {
899                         error = qla4_test_rdy(ha, rs);
900                         if (error)
901                                 return error;
902                 }
903
904                 /* We know we've reached the last device when
905                  * next_fw_ddb_index is 0 */
906                 if (rs->next_fw_ddb_index == 0)
907                         break;
908         }
909         return QLA_SUCCESS;
910 }
911
912 /**
913  * qla4xxx_devices_ready - wait for target devices to be logged in
914  * @ha: pointer to adapter structure
915  *
916  * This routine waits up to ql4xdiscoverywait seconds
917  * F/W database during driver load time.
918  **/
919 static int qla4xxx_devices_ready(struct scsi_qla_host *ha)
920 {
921         int error;
922         unsigned long discovery_wtime;
923         struct qla4_relog_scan rs;
924
925         discovery_wtime = jiffies + (ql4xdiscoverywait * HZ);
926
927         DEBUG(printk("Waiting (%d) for devices ...\n", ql4xdiscoverywait));
928         do {
929                 /* poll for AEN. */
930                 qla4xxx_get_firmware_state(ha);
931                 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags)) {
932                         /* Set time-between-relogin timer */
933                         qla4xxx_process_aen(ha, RELOGIN_DDB_CHANGED_AENS);
934                 }
935
936                 /* if no relogins active or needed, halt discvery wait */
937                 rs.halt_wait = 1;
938
939                 error = qla4_scan_for_relogin(ha, &rs);
940
941                 if (rs.halt_wait) {
942                         DEBUG2(printk("scsi%ld: %s: Delay halted.  Devices "
943                                       "Ready.\n", ha->host_no, __func__));
944                         return QLA_SUCCESS;
945                 }
946
947                 msleep(2000);
948         } while (!time_after_eq(jiffies, discovery_wtime));
949
950         DEBUG3(qla4xxx_get_conn_event_log(ha));
951
952         return QLA_SUCCESS;
953 }
954
955 static void qla4xxx_flush_AENS(struct scsi_qla_host *ha)
956 {
957         unsigned long wtime;
958
959         /* Flush the 0x8014 AEN from the firmware as a result of
960          * Auto connect. We are basically doing get_firmware_ddb()
961          * to determine whether we need to log back in or not.
962          *  Trying to do a set ddb before we have processed 0x8014
963          *  will result in another set_ddb() for the same ddb. In other
964          *  words there will be stale entries in the aen_q.
965          */
966         wtime = jiffies + (2 * HZ);
967         do {
968                 if (qla4xxx_get_firmware_state(ha) == QLA_SUCCESS)
969                         if (ha->firmware_state & (BIT_2 | BIT_0))
970                                 return;
971
972                 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
973                         qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
974
975                 msleep(1000);
976         } while (!time_after_eq(jiffies, wtime));
977
978 }
979
980 static int qla4xxx_initialize_ddb_list(struct scsi_qla_host *ha)
981 {
982         uint16_t fw_ddb_index;
983         int status = QLA_SUCCESS;
984
985         /* free the ddb list if is not empty */
986         if (!list_empty(&ha->ddb_list))
987                 qla4xxx_free_ddb_list(ha);
988
989         for (fw_ddb_index = 0; fw_ddb_index < MAX_DDB_ENTRIES; fw_ddb_index++)
990                 ha->fw_ddb_index_map[fw_ddb_index] =
991                         (struct ddb_entry *)INVALID_ENTRY;
992
993         ha->tot_ddbs = 0;
994
995         qla4xxx_flush_AENS(ha);
996
997         /* Wait for an AEN */
998         qla4xxx_devices_ready(ha);
999
1000         /*
1001          * First perform device discovery for active
1002          * fw ddb indexes and build
1003          * ddb list.
1004          */
1005         if ((status = qla4xxx_build_ddb_list(ha)) == QLA_ERROR)
1006                 return status;
1007
1008         /*
1009          * Targets can come online after the inital discovery, so processing
1010          * the aens here will catch them.
1011          */
1012         if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
1013                 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
1014
1015         return status;
1016 }
1017
1018 /**
1019  * qla4xxx_reinitialize_ddb_list - update the driver ddb list
1020  * @ha: pointer to host adapter structure.
1021  *
1022  * This routine obtains device information from the F/W database after
1023  * firmware or adapter resets.  The device table is preserved.
1024  **/
1025 int qla4xxx_reinitialize_ddb_list(struct scsi_qla_host *ha)
1026 {
1027         int status = QLA_SUCCESS;
1028         struct ddb_entry *ddb_entry, *detemp;
1029
1030         /* Update the device information for all devices. */
1031         list_for_each_entry_safe(ddb_entry, detemp, &ha->ddb_list, list) {
1032                 qla4xxx_update_ddb_entry(ha, ddb_entry,
1033                                          ddb_entry->fw_ddb_index);
1034                 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
1035                         atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
1036                         DEBUG2(printk ("scsi%ld: %s: ddb index [%d] marked "
1037                                        "ONLINE\n", ha->host_no, __func__,
1038                                        ddb_entry->fw_ddb_index));
1039                         iscsi_unblock_session(ddb_entry->sess);
1040                 } else if (atomic_read(&ddb_entry->state) == DDB_STATE_ONLINE)
1041                         qla4xxx_mark_device_missing(ha, ddb_entry);
1042         }
1043         return status;
1044 }
1045
1046 /**
1047  * qla4xxx_relogin_device - re-establish session
1048  * @ha: Pointer to host adapter structure.
1049  * @ddb_entry: Pointer to device database entry
1050  *
1051  * This routine does a session relogin with the specified device.
1052  * The ddb entry must be assigned prior to making this call.
1053  **/
1054 int qla4xxx_relogin_device(struct scsi_qla_host *ha,
1055                            struct ddb_entry * ddb_entry)
1056 {
1057         uint16_t relogin_timer;
1058
1059         relogin_timer = max(ddb_entry->default_relogin_timeout,
1060                             (uint16_t)RELOGIN_TOV);
1061         atomic_set(&ddb_entry->relogin_timer, relogin_timer);
1062
1063         DEBUG2(printk("scsi%ld: Relogin ddb [%d]. TOV=%d\n", ha->host_no,
1064                       ddb_entry->fw_ddb_index, relogin_timer));
1065
1066         qla4xxx_set_ddb_entry(ha, ddb_entry->fw_ddb_index, 0);
1067
1068         return QLA_SUCCESS;
1069 }
1070
1071 static int qla4xxx_config_nvram(struct scsi_qla_host *ha)
1072 {
1073         unsigned long flags;
1074         union external_hw_config_reg extHwConfig;
1075
1076         DEBUG2(printk("scsi%ld: %s: Get EEProm parameters \n", ha->host_no,
1077                       __func__));
1078         if (ql4xxx_lock_flash(ha) != QLA_SUCCESS)
1079                 return QLA_ERROR;
1080         if (ql4xxx_lock_nvram(ha) != QLA_SUCCESS) {
1081                 ql4xxx_unlock_flash(ha);
1082                 return QLA_ERROR;
1083         }
1084
1085         /* Get EEPRom Parameters from NVRAM and validate */
1086         ql4_printk(KERN_INFO, ha, "Configuring NVRAM ...\n");
1087         if (qla4xxx_is_nvram_configuration_valid(ha) == QLA_SUCCESS) {
1088                 spin_lock_irqsave(&ha->hardware_lock, flags);
1089                 extHwConfig.Asuint32_t =
1090                         rd_nvram_word(ha, eeprom_ext_hw_conf_offset(ha));
1091                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1092         } else {
1093                 ql4_printk(KERN_WARNING, ha,
1094                     "scsi%ld: %s: EEProm checksum invalid.  "
1095                     "Please update your EEPROM\n", ha->host_no,
1096                     __func__);
1097
1098                 /* Attempt to set defaults */
1099                 if (is_qla4010(ha))
1100                         extHwConfig.Asuint32_t = 0x1912;
1101                 else if (is_qla4022(ha) | is_qla4032(ha))
1102                         extHwConfig.Asuint32_t = 0x0023;
1103                 else
1104                         return QLA_ERROR;
1105         }
1106         DEBUG(printk("scsi%ld: %s: Setting extHwConfig to 0xFFFF%04x\n",
1107                      ha->host_no, __func__, extHwConfig.Asuint32_t));
1108
1109         spin_lock_irqsave(&ha->hardware_lock, flags);
1110         writel((0xFFFF << 16) | extHwConfig.Asuint32_t, isp_ext_hw_conf(ha));
1111         readl(isp_ext_hw_conf(ha));
1112         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1113
1114         ql4xxx_unlock_nvram(ha);
1115         ql4xxx_unlock_flash(ha);
1116
1117         return QLA_SUCCESS;
1118 }
1119
1120 /**
1121  * qla4_8xxx_pci_config() - Setup ISP82xx PCI configuration registers.
1122  * @ha: HA context
1123  */
1124 void qla4_8xxx_pci_config(struct scsi_qla_host *ha)
1125 {
1126         pci_set_master(ha->pdev);
1127 }
1128
1129 void qla4xxx_pci_config(struct scsi_qla_host *ha)
1130 {
1131         uint16_t w;
1132         int status;
1133
1134         ql4_printk(KERN_INFO, ha, "Configuring PCI space...\n");
1135
1136         pci_set_master(ha->pdev);
1137         status = pci_set_mwi(ha->pdev);
1138         /*
1139          * We want to respect framework's setting of PCI configuration space
1140          * command register and also want to make sure that all bits of
1141          * interest to us are properly set in command register.
1142          */
1143         pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
1144         w |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
1145         w &= ~PCI_COMMAND_INTX_DISABLE;
1146         pci_write_config_word(ha->pdev, PCI_COMMAND, w);
1147 }
1148
1149 static int qla4xxx_start_firmware_from_flash(struct scsi_qla_host *ha)
1150 {
1151         int status = QLA_ERROR;
1152         unsigned long max_wait_time;
1153         unsigned long flags;
1154         uint32_t mbox_status;
1155
1156         ql4_printk(KERN_INFO, ha, "Starting firmware ...\n");
1157
1158         /*
1159          * Start firmware from flash ROM
1160          *
1161          * WORKAROUND: Stuff a non-constant value that the firmware can
1162          * use as a seed for a random number generator in MB7 prior to
1163          * setting BOOT_ENABLE.  Fixes problem where the TCP
1164          * connections use the same TCP ports after each reboot,
1165          * causing some connections to not get re-established.
1166          */
1167         DEBUG(printk("scsi%d: %s: Start firmware from flash ROM\n",
1168                      ha->host_no, __func__));
1169
1170         spin_lock_irqsave(&ha->hardware_lock, flags);
1171         writel(jiffies, &ha->reg->mailbox[7]);
1172         if (is_qla4022(ha) | is_qla4032(ha))
1173                 writel(set_rmask(NVR_WRITE_ENABLE),
1174                        &ha->reg->u1.isp4022.nvram);
1175
1176         writel(2, &ha->reg->mailbox[6]);
1177         readl(&ha->reg->mailbox[6]);
1178
1179         writel(set_rmask(CSR_BOOT_ENABLE), &ha->reg->ctrl_status);
1180         readl(&ha->reg->ctrl_status);
1181         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1182
1183         /* Wait for firmware to come UP. */
1184         DEBUG2(printk(KERN_INFO "scsi%ld: %s: Wait up to %d seconds for "
1185                       "boot firmware to complete...\n",
1186                       ha->host_no, __func__, FIRMWARE_UP_TOV));
1187         max_wait_time = jiffies + (FIRMWARE_UP_TOV * HZ);
1188         do {
1189                 uint32_t ctrl_status;
1190
1191                 spin_lock_irqsave(&ha->hardware_lock, flags);
1192                 ctrl_status = readw(&ha->reg->ctrl_status);
1193                 mbox_status = readw(&ha->reg->mailbox[0]);
1194                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1195
1196                 if (ctrl_status & set_rmask(CSR_SCSI_PROCESSOR_INTR))
1197                         break;
1198                 if (mbox_status == MBOX_STS_COMMAND_COMPLETE)
1199                         break;
1200
1201                 DEBUG2(printk(KERN_INFO "scsi%ld: %s: Waiting for boot "
1202                               "firmware to complete... ctrl_sts=0x%x\n",
1203                               ha->host_no, __func__, ctrl_status));
1204
1205                 msleep_interruptible(250);
1206         } while (!time_after_eq(jiffies, max_wait_time));
1207
1208         if (mbox_status == MBOX_STS_COMMAND_COMPLETE) {
1209                 DEBUG(printk(KERN_INFO "scsi%ld: %s: Firmware has started\n",
1210                              ha->host_no, __func__));
1211
1212                 spin_lock_irqsave(&ha->hardware_lock, flags);
1213                 writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
1214                        &ha->reg->ctrl_status);
1215                 readl(&ha->reg->ctrl_status);
1216                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1217
1218                 status = QLA_SUCCESS;
1219         } else {
1220                 printk(KERN_INFO "scsi%ld: %s: Boot firmware failed "
1221                        "-  mbox status 0x%x\n", ha->host_no, __func__,
1222                        mbox_status);
1223                 status = QLA_ERROR;
1224         }
1225         return status;
1226 }
1227
1228 int ql4xxx_lock_drvr_wait(struct scsi_qla_host *a)
1229 {
1230 #define QL4_LOCK_DRVR_WAIT      60
1231 #define QL4_LOCK_DRVR_SLEEP     1
1232
1233         int drvr_wait = QL4_LOCK_DRVR_WAIT;
1234         while (drvr_wait) {
1235                 if (ql4xxx_lock_drvr(a) == 0) {
1236                         ssleep(QL4_LOCK_DRVR_SLEEP);
1237                         if (drvr_wait) {
1238                                 DEBUG2(printk("scsi%ld: %s: Waiting for "
1239                                               "Global Init Semaphore(%d)...\n",
1240                                               a->host_no,
1241                                               __func__, drvr_wait));
1242                         }
1243                         drvr_wait -= QL4_LOCK_DRVR_SLEEP;
1244                 } else {
1245                         DEBUG2(printk("scsi%ld: %s: Global Init Semaphore "
1246                                       "acquired\n", a->host_no, __func__));
1247                         return QLA_SUCCESS;
1248                 }
1249         }
1250         return QLA_ERROR;
1251 }
1252
1253 /**
1254  * qla4xxx_start_firmware - starts qla4xxx firmware
1255  * @ha: Pointer to host adapter structure.
1256  *
1257  * This routine performs the necessary steps to start the firmware for
1258  * the QLA4010 adapter.
1259  **/
1260 int qla4xxx_start_firmware(struct scsi_qla_host *ha)
1261 {
1262         unsigned long flags = 0;
1263         uint32_t mbox_status;
1264         int status = QLA_ERROR;
1265         int soft_reset = 1;
1266         int config_chip = 0;
1267
1268         if (is_qla4022(ha) | is_qla4032(ha))
1269                 ql4xxx_set_mac_number(ha);
1270
1271         if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
1272                 return QLA_ERROR;
1273
1274         spin_lock_irqsave(&ha->hardware_lock, flags);
1275
1276         DEBUG2(printk("scsi%ld: %s: port_ctrl   = 0x%08X\n", ha->host_no,
1277                       __func__, readw(isp_port_ctrl(ha))));
1278         DEBUG(printk("scsi%ld: %s: port_status = 0x%08X\n", ha->host_no,
1279                      __func__, readw(isp_port_status(ha))));
1280
1281         /* Is Hardware already initialized? */
1282         if ((readw(isp_port_ctrl(ha)) & 0x8000) != 0) {
1283                 DEBUG(printk("scsi%ld: %s: Hardware has already been "
1284                              "initialized\n", ha->host_no, __func__));
1285
1286                 /* Receive firmware boot acknowledgement */
1287                 mbox_status = readw(&ha->reg->mailbox[0]);
1288
1289                 DEBUG2(printk("scsi%ld: %s: H/W Config complete - mbox[0]= "
1290                               "0x%x\n", ha->host_no, __func__, mbox_status));
1291
1292                 /* Is firmware already booted? */
1293                 if (mbox_status == 0) {
1294                         /* F/W not running, must be config by net driver */
1295                         config_chip = 1;
1296                         soft_reset = 0;
1297                 } else {
1298                         writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
1299                                &ha->reg->ctrl_status);
1300                         readl(&ha->reg->ctrl_status);
1301                         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1302                         if (qla4xxx_get_firmware_state(ha) == QLA_SUCCESS) {
1303                                 DEBUG2(printk("scsi%ld: %s: Get firmware "
1304                                               "state -- state = 0x%x\n",
1305                                               ha->host_no,
1306                                               __func__, ha->firmware_state));
1307                                 /* F/W is running */
1308                                 if (ha->firmware_state &
1309                                     FW_STATE_CONFIG_WAIT) {
1310                                         DEBUG2(printk("scsi%ld: %s: Firmware "
1311                                                       "in known state -- "
1312                                                       "config and "
1313                                                       "boot, state = 0x%x\n",
1314                                                       ha->host_no, __func__,
1315                                                       ha->firmware_state));
1316                                         config_chip = 1;
1317                                         soft_reset = 0;
1318                                 }
1319                         } else {
1320                                 DEBUG2(printk("scsi%ld: %s: Firmware in "
1321                                               "unknown state -- resetting,"
1322                                               " state = "
1323                                               "0x%x\n", ha->host_no, __func__,
1324                                               ha->firmware_state));
1325                         }
1326                         spin_lock_irqsave(&ha->hardware_lock, flags);
1327                 }
1328         } else {
1329                 DEBUG(printk("scsi%ld: %s: H/W initialization hasn't been "
1330                              "started - resetting\n", ha->host_no, __func__));
1331         }
1332         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1333
1334         DEBUG(printk("scsi%ld: %s: Flags soft_rest=%d, config= %d\n ",
1335                      ha->host_no, __func__, soft_reset, config_chip));
1336         if (soft_reset) {
1337                 DEBUG(printk("scsi%ld: %s: Issue Soft Reset\n", ha->host_no,
1338                              __func__));
1339                 status = qla4xxx_soft_reset(ha);        /* NOTE: acquires drvr
1340                                                          * lock again, but ok */
1341                 if (status == QLA_ERROR) {
1342                         DEBUG(printk("scsi%d: %s: Soft Reset failed!\n",
1343                                      ha->host_no, __func__));
1344                         ql4xxx_unlock_drvr(ha);
1345                         return QLA_ERROR;
1346                 }
1347                 config_chip = 1;
1348
1349                 /* Reset clears the semaphore, so acquire again */
1350                 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
1351                         return QLA_ERROR;
1352         }
1353
1354         if (config_chip) {
1355                 if ((status = qla4xxx_config_nvram(ha)) == QLA_SUCCESS)
1356                         status = qla4xxx_start_firmware_from_flash(ha);
1357         }
1358
1359         ql4xxx_unlock_drvr(ha);
1360         if (status == QLA_SUCCESS) {
1361                 if (test_and_clear_bit(AF_GET_CRASH_RECORD, &ha->flags))
1362                         qla4xxx_get_crash_record(ha);
1363         } else {
1364                 DEBUG(printk("scsi%ld: %s: Firmware has NOT started\n",
1365                              ha->host_no, __func__));
1366         }
1367         return status;
1368 }
1369
1370
1371 /**
1372  * qla4xxx_initialize_adapter - initiailizes hba
1373  * @ha: Pointer to host adapter structure.
1374  * @renew_ddb_list: Indicates what to do with the adapter's ddb list
1375  *      after adapter recovery has completed.
1376  *      0=preserve ddb list, 1=destroy and rebuild ddb list
1377  *
1378  * This routine parforms all of the steps necessary to initialize the adapter.
1379  *
1380  **/
1381 int qla4xxx_initialize_adapter(struct scsi_qla_host *ha,
1382                                uint8_t renew_ddb_list)
1383 {
1384         int status = QLA_ERROR;
1385         int8_t ip_address[IP_ADDR_LEN] = {0} ;
1386
1387         ha->eeprom_cmd_data = 0;
1388
1389         ql4_printk(KERN_INFO, ha, "Configuring PCI space...\n");
1390         ha->isp_ops->pci_config(ha);
1391
1392         ha->isp_ops->disable_intrs(ha);
1393
1394         /* Initialize the Host adapter request/response queues and firmware */
1395         if (ha->isp_ops->start_firmware(ha) == QLA_ERROR)
1396                 goto exit_init_hba;
1397
1398         if (qla4xxx_get_fw_version(ha) == QLA_ERROR)
1399                 goto exit_init_hba;
1400
1401         if (ha->isp_ops->get_sys_info(ha) == QLA_ERROR)
1402                 goto exit_init_hba;
1403
1404         if (qla4xxx_init_local_data(ha) == QLA_ERROR)
1405                 goto exit_init_hba;
1406
1407         status = qla4xxx_init_firmware(ha);
1408         if (status == QLA_ERROR)
1409                 goto exit_init_hba;
1410
1411         /*
1412          * FW is waiting to get an IP address from DHCP server: Skip building
1413          * the ddb_list and wait for DHCP lease acquired aen to come in
1414          * followed by 0x8014 aen" to trigger the tgt discovery process.
1415          */
1416         if (ha->firmware_state & FW_STATE_CONFIGURING_IP)
1417                 goto exit_init_online;
1418
1419         /* Skip device discovery if ip and subnet is zero */
1420         if (memcmp(ha->ip_address, ip_address, IP_ADDR_LEN) == 0 ||
1421             memcmp(ha->subnet_mask, ip_address, IP_ADDR_LEN) == 0)
1422                 goto exit_init_online;
1423
1424         if (renew_ddb_list == PRESERVE_DDB_LIST) {
1425                 /*
1426                  * We want to preserve lun states (i.e. suspended, etc.)
1427                  * for recovery initiated by the driver.  So just update
1428                  * the device states for the existing ddb_list.
1429                  */
1430                 qla4xxx_reinitialize_ddb_list(ha);
1431         } else if (renew_ddb_list == REBUILD_DDB_LIST) {
1432                 /*
1433                  * We want to build the ddb_list from scratch during
1434                  * driver initialization and recovery initiated by the
1435                  * INT_HBA_RESET IOCTL.
1436                  */
1437                 status = qla4xxx_initialize_ddb_list(ha);
1438                 if (status == QLA_ERROR) {
1439                         DEBUG2(printk("%s(%ld) Error occurred during build"
1440                                       "ddb list\n", __func__, ha->host_no));
1441                         goto exit_init_hba;
1442                 }
1443
1444         }
1445         if (!ha->tot_ddbs) {
1446                 DEBUG2(printk("scsi%ld: Failed to initialize devices or none "
1447                               "present in Firmware device database\n",
1448                               ha->host_no));
1449         }
1450
1451 exit_init_online:
1452         set_bit(AF_ONLINE, &ha->flags);
1453 exit_init_hba:
1454         DEBUG2(printk("scsi%ld: initialize adapter: %s\n", ha->host_no,
1455             status == QLA_ERROR ? "FAILED" : "SUCCEDED"));
1456         return status;
1457 }
1458
1459 /**
1460  * qla4xxx_add_device_dynamically - ddb addition due to an AEN
1461  * @ha:  Pointer to host adapter structure.
1462  * @fw_ddb_index:  Firmware's device database index
1463  *
1464  * This routine processes adds a device as a result of an 8014h AEN.
1465  **/
1466 static void qla4xxx_add_device_dynamically(struct scsi_qla_host *ha,
1467                                            uint32_t fw_ddb_index)
1468 {
1469         struct ddb_entry * ddb_entry;
1470         uint32_t new_tgt;
1471
1472         /* First allocate a device structure */
1473         ddb_entry = qla4xxx_get_ddb_entry(ha, fw_ddb_index, &new_tgt);
1474         if (ddb_entry == NULL) {
1475                 DEBUG2(printk(KERN_WARNING
1476                               "scsi%ld: Unable to allocate memory to add "
1477                               "fw_ddb_index %d\n", ha->host_no, fw_ddb_index));
1478                 return;
1479         }
1480
1481         if (!new_tgt && (ddb_entry->fw_ddb_index != fw_ddb_index)) {
1482                 /* Target has been bound to a new fw_ddb_index */
1483                 qla4xxx_free_ddb(ha, ddb_entry);
1484                 ddb_entry = qla4xxx_alloc_ddb(ha, fw_ddb_index);
1485                 if (ddb_entry == NULL) {
1486                         DEBUG2(printk(KERN_WARNING
1487                                 "scsi%ld: Unable to allocate memory"
1488                                 " to add fw_ddb_index %d\n",
1489                                 ha->host_no, fw_ddb_index));
1490                         return;
1491                 }
1492         }
1493         if (qla4xxx_update_ddb_entry(ha, ddb_entry, fw_ddb_index) ==
1494                                     QLA_ERROR) {
1495                 ha->fw_ddb_index_map[fw_ddb_index] =
1496                                         (struct ddb_entry *)INVALID_ENTRY;
1497                 DEBUG2(printk(KERN_WARNING
1498                               "scsi%ld: failed to add new device at index "
1499                               "[%d]\n Unable to retrieve fw ddb entry\n",
1500                               ha->host_no, fw_ddb_index));
1501                 qla4xxx_free_ddb(ha, ddb_entry);
1502                 return;
1503         }
1504
1505         if (qla4xxx_add_sess(ddb_entry)) {
1506                 DEBUG2(printk(KERN_WARNING
1507                               "scsi%ld: failed to add new device at index "
1508                               "[%d]\n Unable to add connection and session\n",
1509                               ha->host_no, fw_ddb_index));
1510                 qla4xxx_free_ddb(ha, ddb_entry);
1511         }
1512 }
1513
1514 /**
1515  * qla4xxx_process_ddb_changed - process ddb state change
1516  * @ha - Pointer to host adapter structure.
1517  * @fw_ddb_index - Firmware's device database index
1518  * @state - Device state
1519  *
1520  * This routine processes a Decive Database Changed AEN Event.
1521  **/
1522 int qla4xxx_process_ddb_changed(struct scsi_qla_host *ha, uint32_t fw_ddb_index,
1523                 uint32_t state, uint32_t conn_err)
1524 {
1525         struct ddb_entry * ddb_entry;
1526         uint32_t old_fw_ddb_device_state;
1527
1528         /* check for out of range index */
1529         if (fw_ddb_index >= MAX_DDB_ENTRIES)
1530                 return QLA_ERROR;
1531
1532         /* Get the corresponging ddb entry */
1533         ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, fw_ddb_index);
1534         /* Device does not currently exist in our database. */
1535         if (ddb_entry == NULL) {
1536                 if (state == DDB_DS_SESSION_ACTIVE)
1537                         qla4xxx_add_device_dynamically(ha, fw_ddb_index);
1538                 return QLA_SUCCESS;
1539         }
1540
1541         /* Device already exists in our database. */
1542         old_fw_ddb_device_state = ddb_entry->fw_ddb_device_state;
1543         DEBUG2(printk("scsi%ld: %s DDB - old state= 0x%x, new state=0x%x for "
1544                       "index [%d]\n", ha->host_no, __func__,
1545                       ddb_entry->fw_ddb_device_state, state, fw_ddb_index));
1546         if (old_fw_ddb_device_state == state &&
1547             state == DDB_DS_SESSION_ACTIVE) {
1548                 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
1549                         atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
1550                         iscsi_unblock_session(ddb_entry->sess);
1551                 }
1552                 return QLA_SUCCESS;
1553         }
1554
1555         ddb_entry->fw_ddb_device_state = state;
1556         /* Device is back online. */
1557         if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
1558                 atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
1559                 atomic_set(&ddb_entry->port_down_timer,
1560                            ha->port_down_retry_count);
1561                 atomic_set(&ddb_entry->relogin_retry_count, 0);
1562                 atomic_set(&ddb_entry->relogin_timer, 0);
1563                 clear_bit(DF_RELOGIN, &ddb_entry->flags);
1564                 clear_bit(DF_NO_RELOGIN, &ddb_entry->flags);
1565                 iscsi_unblock_session(ddb_entry->sess);
1566                 iscsi_session_event(ddb_entry->sess,
1567                                     ISCSI_KEVENT_CREATE_SESSION);
1568                 /*
1569                  * Change the lun state to READY in case the lun TIMEOUT before
1570                  * the device came back.
1571                  */
1572         } else {
1573                 /* Device went away, mark device missing */
1574                 if (atomic_read(&ddb_entry->state) == DDB_STATE_ONLINE) {
1575                         DEBUG2(ql4_printk(KERN_INFO, ha, "%s mark missing "
1576                                         "ddb_entry 0x%p sess 0x%p conn 0x%p\n",
1577                                         __func__, ddb_entry,
1578                                         ddb_entry->sess, ddb_entry->conn));
1579                         qla4xxx_mark_device_missing(ha, ddb_entry);
1580                 }
1581
1582                 /*
1583                  * Relogin if device state changed to a not active state.
1584                  * However, do not relogin if a RELOGIN is in process, or
1585                  * we are not allowed to relogin to this DDB.
1586                  */
1587                 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_FAILED &&
1588                     !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
1589                     !test_bit(DF_NO_RELOGIN, &ddb_entry->flags) &&
1590                     qla4_is_relogin_allowed(ha, conn_err)) {
1591                         /*
1592                          * This triggers a relogin.  After the relogin_timer
1593                          * expires, the relogin gets scheduled.  We must wait a
1594                          * minimum amount of time since receiving an 0x8014 AEN
1595                          * with failed device_state or a logout response before
1596                          * we can issue another relogin.
1597                          */
1598                         /* Firmware pads this timeout: (time2wait +1).
1599                          * Driver retry to login should be longer than F/W.
1600                          * Otherwise F/W will fail
1601                          * set_ddb() mbx cmd with 0x4005 since it still
1602                          * counting down its time2wait.
1603                          */
1604                         atomic_set(&ddb_entry->relogin_timer, 0);
1605                         atomic_set(&ddb_entry->retry_relogin_timer,
1606                                    ddb_entry->default_time2wait + 4);
1607                         DEBUG(printk("scsi%ld: %s: ddb[%d] "
1608                             "initiate relogin after %d seconds\n",
1609                             ha->host_no, __func__,
1610                             ddb_entry->fw_ddb_index,
1611                             ddb_entry->default_time2wait + 4));
1612                 } else {
1613                         DEBUG(printk("scsi%ld: %s: ddb[%d] "
1614                             "relogin not initiated, state = %d, "
1615                             "ddb_entry->flags = 0x%lx\n",
1616                             ha->host_no, __func__,
1617                             ddb_entry->fw_ddb_index,
1618                             ddb_entry->fw_ddb_device_state,
1619                             ddb_entry->flags));
1620                 }
1621         }
1622         return QLA_SUCCESS;
1623 }