Merge master.kernel.org:/pub/scm/linux/kernel/git/willy/parisc-2.6
[linux-2.6] / drivers / scsi / qla2xxx / qla_init.c
1 /*
2  * QLogic Fibre Channel HBA Driver
3  * Copyright (c)  2003-2005 QLogic Corporation
4  *
5  * See LICENSE.qla2xxx for copyright and licensing details.
6  */
7 #include "qla_def.h"
8
9 #include <linux/delay.h>
10 #include <linux/vmalloc.h>
11
12 #include "qla_devtbl.h"
13
14 /* XXX(hch): this is ugly, but we don't want to pull in exioctl.h */
15 #ifndef EXT_IS_LUN_BIT_SET
16 #define EXT_IS_LUN_BIT_SET(P,L) \
17     (((P)->mask[L/8] & (0x80 >> (L%8)))?1:0)
18 #define EXT_SET_LUN_BIT(P,L) \
19     ((P)->mask[L/8] |= (0x80 >> (L%8)))
20 #endif
21
22 /*
23 *  QLogic ISP2x00 Hardware Support Function Prototypes.
24 */
25 static int qla2x00_isp_firmware(scsi_qla_host_t *);
26 static void qla2x00_resize_request_q(scsi_qla_host_t *);
27 static int qla2x00_setup_chip(scsi_qla_host_t *);
28 static void qla2x00_init_response_q_entries(scsi_qla_host_t *);
29 static int qla2x00_init_rings(scsi_qla_host_t *);
30 static int qla2x00_fw_ready(scsi_qla_host_t *);
31 static int qla2x00_configure_hba(scsi_qla_host_t *);
32 static int qla2x00_configure_loop(scsi_qla_host_t *);
33 static int qla2x00_configure_local_loop(scsi_qla_host_t *);
34 static int qla2x00_configure_fabric(scsi_qla_host_t *);
35 static int qla2x00_find_all_fabric_devs(scsi_qla_host_t *, struct list_head *);
36 static int qla2x00_device_resync(scsi_qla_host_t *);
37 static int qla2x00_fabric_dev_login(scsi_qla_host_t *, fc_port_t *,
38     uint16_t *);
39
40 static int qla2x00_restart_isp(scsi_qla_host_t *);
41
42 static int qla2x00_find_new_loop_id(scsi_qla_host_t *ha, fc_port_t *dev);
43
44 /****************************************************************************/
45 /*                QLogic ISP2x00 Hardware Support Functions.                */
46 /****************************************************************************/
47
48 /*
49 * qla2x00_initialize_adapter
50 *      Initialize board.
51 *
52 * Input:
53 *      ha = adapter block pointer.
54 *
55 * Returns:
56 *      0 = success
57 */
58 int
59 qla2x00_initialize_adapter(scsi_qla_host_t *ha)
60 {
61         int     rval;
62         uint8_t restart_risc = 0;
63         uint8_t retry;
64         uint32_t wait_time;
65
66         /* Clear adapter flags. */
67         ha->flags.online = 0;
68         ha->flags.reset_active = 0;
69         atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
70         atomic_set(&ha->loop_state, LOOP_DOWN);
71         ha->device_flags = 0;
72         ha->dpc_flags = 0;
73         ha->flags.management_server_logged_in = 0;
74         ha->marker_needed = 0;
75         ha->mbx_flags = 0;
76         ha->isp_abort_cnt = 0;
77         ha->beacon_blink_led = 0;
78         set_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags);
79
80         qla_printk(KERN_INFO, ha, "Configuring PCI space...\n");
81         rval = ha->isp_ops.pci_config(ha);
82         if (rval) {
83                 DEBUG2(printk("scsi(%ld): Unable to configure PCI space=n",
84                     ha->host_no));
85                 return (rval);
86         }
87
88         ha->isp_ops.reset_chip(ha);
89
90         qla_printk(KERN_INFO, ha, "Configure NVRAM parameters...\n");
91
92         ha->isp_ops.nvram_config(ha);
93
94         if (ha->flags.disable_serdes) {
95                 /* Mask HBA via NVRAM settings? */
96                 qla_printk(KERN_INFO, ha, "Masking HBA WWPN "
97                     "%02x%02x%02x%02x%02x%02x%02x%02x (via NVRAM).\n",
98                     ha->port_name[0], ha->port_name[1],
99                     ha->port_name[2], ha->port_name[3],
100                     ha->port_name[4], ha->port_name[5],
101                     ha->port_name[6], ha->port_name[7]);
102                 return QLA_FUNCTION_FAILED;
103         }
104
105         qla_printk(KERN_INFO, ha, "Verifying loaded RISC code...\n");
106
107         retry = 10;
108         /*
109          * Try to configure the loop.
110          */
111         do {
112                 restart_risc = 0;
113
114                 /* If firmware needs to be loaded */
115                 if (qla2x00_isp_firmware(ha) != QLA_SUCCESS) {
116                         if ((rval = ha->isp_ops.chip_diag(ha)) == QLA_SUCCESS) {
117                                 rval = qla2x00_setup_chip(ha);
118                         }
119                 }
120
121                 if (rval == QLA_SUCCESS &&
122                     (rval = qla2x00_init_rings(ha)) == QLA_SUCCESS) {
123 check_fw_ready_again:
124                         /*
125                          * Wait for a successful LIP up to a maximum
126                          * of (in seconds): RISC login timeout value,
127                          * RISC retry count value, and port down retry
128                          * value OR a minimum of 4 seconds OR If no
129                          * cable, only 5 seconds.
130                          */
131                         rval = qla2x00_fw_ready(ha);
132                         if (rval == QLA_SUCCESS) {
133                                 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
134
135                                 /* Issue a marker after FW becomes ready. */
136                                 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
137
138                                 /*
139                                  * Wait at most MAX_TARGET RSCNs for a stable
140                                  * link.
141                                  */
142                                 wait_time = 256;
143                                 do {
144                                         clear_bit(LOOP_RESYNC_NEEDED,
145                                             &ha->dpc_flags);
146                                         rval = qla2x00_configure_loop(ha);
147
148                                         if (test_and_clear_bit(ISP_ABORT_NEEDED,
149                                             &ha->dpc_flags)) {
150                                                 restart_risc = 1;
151                                                 break;
152                                         }
153
154                                         /*
155                                          * If loop state change while we were
156                                          * discoverying devices then wait for
157                                          * LIP to complete
158                                          */
159
160                                         if (atomic_read(&ha->loop_state) !=
161                                             LOOP_READY && retry--) {
162                                                 goto check_fw_ready_again;
163                                         }
164                                         wait_time--;
165                                 } while (!atomic_read(&ha->loop_down_timer) &&
166                                     retry &&
167                                     wait_time &&
168                                     (test_bit(LOOP_RESYNC_NEEDED,
169                                         &ha->dpc_flags)));
170
171                                 if (wait_time == 0)
172                                         rval = QLA_FUNCTION_FAILED;
173                         } else if (ha->device_flags & DFLG_NO_CABLE)
174                                 /* If no cable, then all is good. */
175                                 rval = QLA_SUCCESS;
176                 }
177         } while (restart_risc && retry--);
178
179         if (rval == QLA_SUCCESS) {
180                 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
181                 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
182                 ha->marker_needed = 0;
183
184                 ha->flags.online = 1;
185         } else {
186                 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
187         }
188
189         return (rval);
190 }
191
192 /**
193  * qla2100_pci_config() - Setup ISP21xx PCI configuration registers.
194  * @ha: HA context
195  *
196  * Returns 0 on success.
197  */
198 int
199 qla2100_pci_config(scsi_qla_host_t *ha)
200 {
201         uint16_t w, mwi;
202         uint32_t d;
203         unsigned long flags;
204         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
205
206         pci_set_master(ha->pdev);
207         mwi = 0;
208         if (pci_set_mwi(ha->pdev))
209                 mwi = PCI_COMMAND_INVALIDATE;
210
211         pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
212         w |= mwi | (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
213         pci_write_config_word(ha->pdev, PCI_COMMAND, w);
214
215         /* Reset expansion ROM address decode enable */
216         pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
217         d &= ~PCI_ROM_ADDRESS_ENABLE;
218         pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
219
220         /* Get PCI bus information. */
221         spin_lock_irqsave(&ha->hardware_lock, flags);
222         ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
223         spin_unlock_irqrestore(&ha->hardware_lock, flags);
224
225         return QLA_SUCCESS;
226 }
227
228 /**
229  * qla2300_pci_config() - Setup ISP23xx PCI configuration registers.
230  * @ha: HA context
231  *
232  * Returns 0 on success.
233  */
234 int
235 qla2300_pci_config(scsi_qla_host_t *ha)
236 {
237         uint16_t        w, mwi;
238         uint32_t        d;
239         unsigned long   flags = 0;
240         uint32_t        cnt;
241         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
242
243         pci_set_master(ha->pdev);
244         mwi = 0;
245         if (pci_set_mwi(ha->pdev))
246                 mwi = PCI_COMMAND_INVALIDATE;
247
248         pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
249         w |= mwi | (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
250
251         if (IS_QLA2322(ha) || IS_QLA6322(ha))
252                 w &= ~PCI_COMMAND_INTX_DISABLE;
253
254         /*
255          * If this is a 2300 card and not 2312, reset the
256          * COMMAND_INVALIDATE due to a bug in the 2300. Unfortunately,
257          * the 2310 also reports itself as a 2300 so we need to get the
258          * fb revision level -- a 6 indicates it really is a 2300 and
259          * not a 2310.
260          */
261         if (IS_QLA2300(ha)) {
262                 spin_lock_irqsave(&ha->hardware_lock, flags);
263
264                 /* Pause RISC. */
265                 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
266                 for (cnt = 0; cnt < 30000; cnt++) {
267                         if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
268                                 break;
269
270                         udelay(10);
271                 }
272
273                 /* Select FPM registers. */
274                 WRT_REG_WORD(&reg->ctrl_status, 0x20);
275                 RD_REG_WORD(&reg->ctrl_status);
276
277                 /* Get the fb rev level */
278                 ha->fb_rev = RD_FB_CMD_REG(ha, reg);
279
280                 if (ha->fb_rev == FPM_2300)
281                         w &= ~PCI_COMMAND_INVALIDATE;
282
283                 /* Deselect FPM registers. */
284                 WRT_REG_WORD(&reg->ctrl_status, 0x0);
285                 RD_REG_WORD(&reg->ctrl_status);
286
287                 /* Release RISC module. */
288                 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
289                 for (cnt = 0; cnt < 30000; cnt++) {
290                         if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) == 0)
291                                 break;
292
293                         udelay(10);
294                 }
295
296                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
297         }
298         pci_write_config_word(ha->pdev, PCI_COMMAND, w);
299
300         pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
301
302         /* Reset expansion ROM address decode enable */
303         pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
304         d &= ~PCI_ROM_ADDRESS_ENABLE;
305         pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
306
307         /* Get PCI bus information. */
308         spin_lock_irqsave(&ha->hardware_lock, flags);
309         ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
310         spin_unlock_irqrestore(&ha->hardware_lock, flags);
311
312         return QLA_SUCCESS;
313 }
314
315 /**
316  * qla24xx_pci_config() - Setup ISP24xx PCI configuration registers.
317  * @ha: HA context
318  *
319  * Returns 0 on success.
320  */
321 int
322 qla24xx_pci_config(scsi_qla_host_t *ha)
323 {
324         uint16_t w, mwi;
325         uint32_t d;
326         unsigned long flags = 0;
327         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
328         int pcix_cmd_reg, pcie_dctl_reg;
329
330         pci_set_master(ha->pdev);
331         mwi = 0;
332         if (pci_set_mwi(ha->pdev))
333                 mwi = PCI_COMMAND_INVALIDATE;
334
335         pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
336         w |= mwi | (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
337         w &= ~PCI_COMMAND_INTX_DISABLE;
338         pci_write_config_word(ha->pdev, PCI_COMMAND, w);
339
340         pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
341
342         /* PCI-X -- adjust Maximum Memory Read Byte Count (2048). */
343         pcix_cmd_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_PCIX);
344         if (pcix_cmd_reg) {
345                 uint16_t pcix_cmd;
346
347                 pcix_cmd_reg += PCI_X_CMD;
348                 pci_read_config_word(ha->pdev, pcix_cmd_reg, &pcix_cmd);
349                 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
350                 pcix_cmd |= 0x0008;
351                 pci_write_config_word(ha->pdev, pcix_cmd_reg, pcix_cmd);
352         }
353
354         /* PCIe -- adjust Maximum Read Request Size (2048). */
355         pcie_dctl_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_EXP);
356         if (pcie_dctl_reg) {
357                 uint16_t pcie_dctl;
358
359                 pcie_dctl_reg += PCI_EXP_DEVCTL;
360                 pci_read_config_word(ha->pdev, pcie_dctl_reg, &pcie_dctl);
361                 pcie_dctl &= ~PCI_EXP_DEVCTL_READRQ;
362                 pcie_dctl |= 0x4000;
363                 pci_write_config_word(ha->pdev, pcie_dctl_reg, pcie_dctl);
364         }
365
366         /* Reset expansion ROM address decode enable */
367         pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
368         d &= ~PCI_ROM_ADDRESS_ENABLE;
369         pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
370
371         /* Get PCI bus information. */
372         spin_lock_irqsave(&ha->hardware_lock, flags);
373         ha->pci_attr = RD_REG_DWORD(&reg->ctrl_status);
374         spin_unlock_irqrestore(&ha->hardware_lock, flags);
375
376         return QLA_SUCCESS;
377 }
378
379 /**
380  * qla2x00_isp_firmware() - Choose firmware image.
381  * @ha: HA context
382  *
383  * Returns 0 on success.
384  */
385 static int
386 qla2x00_isp_firmware(scsi_qla_host_t *ha)
387 {
388         int  rval;
389
390         /* Assume loading risc code */
391         rval = QLA_FUNCTION_FAILED;
392
393         if (ha->flags.disable_risc_code_load) {
394                 DEBUG2(printk("scsi(%ld): RISC CODE NOT loaded\n",
395                     ha->host_no));
396                 qla_printk(KERN_INFO, ha, "RISC CODE NOT loaded\n");
397
398                 /* Verify checksum of loaded RISC code. */
399                 rval = qla2x00_verify_checksum(ha, ha->fw_srisc_address);
400         }
401
402         if (rval) {
403                 DEBUG2_3(printk("scsi(%ld): **** Load RISC code ****\n",
404                     ha->host_no));
405         }
406
407         return (rval);
408 }
409
410 /**
411  * qla2x00_reset_chip() - Reset ISP chip.
412  * @ha: HA context
413  *
414  * Returns 0 on success.
415  */
416 void
417 qla2x00_reset_chip(scsi_qla_host_t *ha)
418 {
419         unsigned long   flags = 0;
420         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
421         uint32_t        cnt;
422         uint16_t        cmd;
423
424         ha->isp_ops.disable_intrs(ha);
425
426         spin_lock_irqsave(&ha->hardware_lock, flags);
427
428         /* Turn off master enable */
429         cmd = 0;
430         pci_read_config_word(ha->pdev, PCI_COMMAND, &cmd);
431         cmd &= ~PCI_COMMAND_MASTER;
432         pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
433
434         if (!IS_QLA2100(ha)) {
435                 /* Pause RISC. */
436                 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
437                 if (IS_QLA2200(ha) || IS_QLA2300(ha)) {
438                         for (cnt = 0; cnt < 30000; cnt++) {
439                                 if ((RD_REG_WORD(&reg->hccr) &
440                                     HCCR_RISC_PAUSE) != 0)
441                                         break;
442                                 udelay(100);
443                         }
444                 } else {
445                         RD_REG_WORD(&reg->hccr);        /* PCI Posting. */
446                         udelay(10);
447                 }
448
449                 /* Select FPM registers. */
450                 WRT_REG_WORD(&reg->ctrl_status, 0x20);
451                 RD_REG_WORD(&reg->ctrl_status);         /* PCI Posting. */
452
453                 /* FPM Soft Reset. */
454                 WRT_REG_WORD(&reg->fpm_diag_config, 0x100);
455                 RD_REG_WORD(&reg->fpm_diag_config);     /* PCI Posting. */
456
457                 /* Toggle Fpm Reset. */
458                 if (!IS_QLA2200(ha)) {
459                         WRT_REG_WORD(&reg->fpm_diag_config, 0x0);
460                         RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
461                 }
462
463                 /* Select frame buffer registers. */
464                 WRT_REG_WORD(&reg->ctrl_status, 0x10);
465                 RD_REG_WORD(&reg->ctrl_status);         /* PCI Posting. */
466
467                 /* Reset frame buffer FIFOs. */
468                 if (IS_QLA2200(ha)) {
469                         WRT_FB_CMD_REG(ha, reg, 0xa000);
470                         RD_FB_CMD_REG(ha, reg);         /* PCI Posting. */
471                 } else {
472                         WRT_FB_CMD_REG(ha, reg, 0x00fc);
473
474                         /* Read back fb_cmd until zero or 3 seconds max */
475                         for (cnt = 0; cnt < 3000; cnt++) {
476                                 if ((RD_FB_CMD_REG(ha, reg) & 0xff) == 0)
477                                         break;
478                                 udelay(100);
479                         }
480                 }
481
482                 /* Select RISC module registers. */
483                 WRT_REG_WORD(&reg->ctrl_status, 0);
484                 RD_REG_WORD(&reg->ctrl_status);         /* PCI Posting. */
485
486                 /* Reset RISC processor. */
487                 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
488                 RD_REG_WORD(&reg->hccr);                /* PCI Posting. */
489
490                 /* Release RISC processor. */
491                 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
492                 RD_REG_WORD(&reg->hccr);                /* PCI Posting. */
493         }
494
495         WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
496         WRT_REG_WORD(&reg->hccr, HCCR_CLR_HOST_INT);
497
498         /* Reset ISP chip. */
499         WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
500
501         /* Wait for RISC to recover from reset. */
502         if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
503                 /*
504                  * It is necessary to for a delay here since the card doesn't
505                  * respond to PCI reads during a reset. On some architectures
506                  * this will result in an MCA.
507                  */
508                 udelay(20);
509                 for (cnt = 30000; cnt; cnt--) {
510                         if ((RD_REG_WORD(&reg->ctrl_status) &
511                             CSR_ISP_SOFT_RESET) == 0)
512                                 break;
513                         udelay(100);
514                 }
515         } else
516                 udelay(10);
517
518         /* Reset RISC processor. */
519         WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
520
521         WRT_REG_WORD(&reg->semaphore, 0);
522
523         /* Release RISC processor. */
524         WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
525         RD_REG_WORD(&reg->hccr);                        /* PCI Posting. */
526
527         if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
528                 for (cnt = 0; cnt < 30000; cnt++) {
529                         if (RD_MAILBOX_REG(ha, reg, 0) != MBS_BUSY)
530                                 break;
531
532                         udelay(100);
533                 }
534         } else
535                 udelay(100);
536
537         /* Turn on master enable */
538         cmd |= PCI_COMMAND_MASTER;
539         pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
540
541         /* Disable RISC pause on FPM parity error. */
542         if (!IS_QLA2100(ha)) {
543                 WRT_REG_WORD(&reg->hccr, HCCR_DISABLE_PARITY_PAUSE);
544                 RD_REG_WORD(&reg->hccr);                /* PCI Posting. */
545         }
546
547         spin_unlock_irqrestore(&ha->hardware_lock, flags);
548 }
549
550 /**
551  * qla24xx_reset_risc() - Perform full reset of ISP24xx RISC.
552  * @ha: HA context
553  *
554  * Returns 0 on success.
555  */
556 static inline void
557 qla24xx_reset_risc(scsi_qla_host_t *ha)
558 {
559         unsigned long flags = 0;
560         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
561         uint32_t cnt, d2;
562         uint16_t wd;
563
564         spin_lock_irqsave(&ha->hardware_lock, flags);
565
566         /* Reset RISC. */
567         WRT_REG_DWORD(&reg->ctrl_status, CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
568         for (cnt = 0; cnt < 30000; cnt++) {
569                 if ((RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE) == 0)
570                         break;
571
572                 udelay(10);
573         }
574
575         WRT_REG_DWORD(&reg->ctrl_status,
576             CSRX_ISP_SOFT_RESET|CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
577         pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
578
579         udelay(100);
580         /* Wait for firmware to complete NVRAM accesses. */
581         d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
582         for (cnt = 10000 ; cnt && d2; cnt--) {
583                 udelay(5);
584                 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
585                 barrier();
586         }
587
588         /* Wait for soft-reset to complete. */
589         d2 = RD_REG_DWORD(&reg->ctrl_status);
590         for (cnt = 6000000 ; cnt && (d2 & CSRX_ISP_SOFT_RESET); cnt--) {
591                 udelay(5);
592                 d2 = RD_REG_DWORD(&reg->ctrl_status);
593                 barrier();
594         }
595
596         WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
597         RD_REG_DWORD(&reg->hccr);
598
599         WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
600         RD_REG_DWORD(&reg->hccr);
601
602         WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_RESET);
603         RD_REG_DWORD(&reg->hccr);
604
605         d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
606         for (cnt = 6000000 ; cnt && d2; cnt--) {
607                 udelay(5);
608                 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
609                 barrier();
610         }
611
612         spin_unlock_irqrestore(&ha->hardware_lock, flags);
613 }
614
615 /**
616  * qla24xx_reset_chip() - Reset ISP24xx chip.
617  * @ha: HA context
618  *
619  * Returns 0 on success.
620  */
621 void
622 qla24xx_reset_chip(scsi_qla_host_t *ha)
623 {
624         ha->isp_ops.disable_intrs(ha);
625
626         /* Perform RISC reset. */
627         qla24xx_reset_risc(ha);
628 }
629
630 /**
631  * qla2x00_chip_diag() - Test chip for proper operation.
632  * @ha: HA context
633  *
634  * Returns 0 on success.
635  */
636 int
637 qla2x00_chip_diag(scsi_qla_host_t *ha)
638 {
639         int             rval;
640         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
641         unsigned long   flags = 0;
642         uint16_t        data;
643         uint32_t        cnt;
644         uint16_t        mb[5];
645
646         /* Assume a failed state */
647         rval = QLA_FUNCTION_FAILED;
648
649         DEBUG3(printk("scsi(%ld): Testing device at %lx.\n",
650             ha->host_no, (u_long)&reg->flash_address));
651
652         spin_lock_irqsave(&ha->hardware_lock, flags);
653
654         /* Reset ISP chip. */
655         WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
656
657         /*
658          * We need to have a delay here since the card will not respond while
659          * in reset causing an MCA on some architectures.
660          */
661         udelay(20);
662         data = qla2x00_debounce_register(&reg->ctrl_status);
663         for (cnt = 6000000 ; cnt && (data & CSR_ISP_SOFT_RESET); cnt--) {
664                 udelay(5);
665                 data = RD_REG_WORD(&reg->ctrl_status);
666                 barrier();
667         }
668
669         if (!cnt)
670                 goto chip_diag_failed;
671
672         DEBUG3(printk("scsi(%ld): Reset register cleared by chip reset\n",
673             ha->host_no));
674
675         /* Reset RISC processor. */
676         WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
677         WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
678
679         /* Workaround for QLA2312 PCI parity error */
680         if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
681                 data = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 0));
682                 for (cnt = 6000000; cnt && (data == MBS_BUSY); cnt--) {
683                         udelay(5);
684                         data = RD_MAILBOX_REG(ha, reg, 0);
685                         barrier();
686                 }
687         } else
688                 udelay(10);
689
690         if (!cnt)
691                 goto chip_diag_failed;
692
693         /* Check product ID of chip */
694         DEBUG3(printk("scsi(%ld): Checking product ID of chip\n", ha->host_no));
695
696         mb[1] = RD_MAILBOX_REG(ha, reg, 1);
697         mb[2] = RD_MAILBOX_REG(ha, reg, 2);
698         mb[3] = RD_MAILBOX_REG(ha, reg, 3);
699         mb[4] = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 4));
700         if (mb[1] != PROD_ID_1 || (mb[2] != PROD_ID_2 && mb[2] != PROD_ID_2a) ||
701             mb[3] != PROD_ID_3) {
702                 qla_printk(KERN_WARNING, ha,
703                     "Wrong product ID = 0x%x,0x%x,0x%x\n", mb[1], mb[2], mb[3]);
704
705                 goto chip_diag_failed;
706         }
707         ha->product_id[0] = mb[1];
708         ha->product_id[1] = mb[2];
709         ha->product_id[2] = mb[3];
710         ha->product_id[3] = mb[4];
711
712         /* Adjust fw RISC transfer size */
713         if (ha->request_q_length > 1024)
714                 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
715         else
716                 ha->fw_transfer_size = REQUEST_ENTRY_SIZE *
717                     ha->request_q_length;
718
719         if (IS_QLA2200(ha) &&
720             RD_MAILBOX_REG(ha, reg, 7) == QLA2200A_RISC_ROM_VER) {
721                 /* Limit firmware transfer size with a 2200A */
722                 DEBUG3(printk("scsi(%ld): Found QLA2200A chip.\n",
723                     ha->host_no));
724
725                 ha->device_type |= DT_ISP2200A;
726                 ha->fw_transfer_size = 128;
727         }
728
729         /* Wrap Incoming Mailboxes Test. */
730         spin_unlock_irqrestore(&ha->hardware_lock, flags);
731
732         DEBUG3(printk("scsi(%ld): Checking mailboxes.\n", ha->host_no));
733         rval = qla2x00_mbx_reg_test(ha);
734         if (rval) {
735                 DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
736                     ha->host_no));
737                 qla_printk(KERN_WARNING, ha,
738                     "Failed mailbox send register test\n");
739         }
740         else {
741                 /* Flag a successful rval */
742                 rval = QLA_SUCCESS;
743         }
744         spin_lock_irqsave(&ha->hardware_lock, flags);
745
746 chip_diag_failed:
747         if (rval)
748                 DEBUG2_3(printk("scsi(%ld): Chip diagnostics **** FAILED "
749                     "****\n", ha->host_no));
750
751         spin_unlock_irqrestore(&ha->hardware_lock, flags);
752
753         return (rval);
754 }
755
756 /**
757  * qla24xx_chip_diag() - Test ISP24xx for proper operation.
758  * @ha: HA context
759  *
760  * Returns 0 on success.
761  */
762 int
763 qla24xx_chip_diag(scsi_qla_host_t *ha)
764 {
765         int rval;
766
767         /* Perform RISC reset. */
768         qla24xx_reset_risc(ha);
769
770         ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
771
772         rval = qla2x00_mbx_reg_test(ha);
773         if (rval) {
774                 DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
775                     ha->host_no));
776                 qla_printk(KERN_WARNING, ha,
777                     "Failed mailbox send register test\n");
778         } else {
779                 /* Flag a successful rval */
780                 rval = QLA_SUCCESS;
781         }
782
783         return rval;
784 }
785
786 void
787 qla2x00_alloc_fw_dump(scsi_qla_host_t *ha)
788 {
789         int rval;
790         uint32_t dump_size, fixed_size, mem_size, req_q_size, rsp_q_size,
791             eft_size;
792         dma_addr_t eft_dma;
793         void *eft;
794
795         if (ha->fw_dump) {
796                 qla_printk(KERN_WARNING, ha,
797                     "Firmware dump previously allocated.\n");
798                 return;
799         }
800
801         ha->fw_dumped = 0;
802         fixed_size = mem_size = eft_size = 0;
803         if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
804                 fixed_size = sizeof(struct qla2100_fw_dump);
805         } else if (IS_QLA23XX(ha)) {
806                 fixed_size = offsetof(struct qla2300_fw_dump, data_ram);
807                 mem_size = (ha->fw_memory_size - 0x11000 + 1) *
808                     sizeof(uint16_t);
809         } else if (IS_QLA24XX(ha) || IS_QLA54XX(ha)) {
810                 fixed_size = offsetof(struct qla24xx_fw_dump, ext_mem);
811                 mem_size = (ha->fw_memory_size - 0x100000 + 1) *
812                     sizeof(uint32_t);
813
814                 /* Allocate memory for Extended Trace Buffer. */
815                 eft = dma_alloc_coherent(&ha->pdev->dev, EFT_SIZE, &eft_dma,
816                     GFP_KERNEL);
817                 if (!eft) {
818                         qla_printk(KERN_WARNING, ha, "Unable to allocate "
819                             "(%d KB) for EFT.\n", EFT_SIZE / 1024);
820                         goto cont_alloc;
821                 }
822
823                 rval = qla2x00_trace_control(ha, TC_ENABLE, eft_dma,
824                     EFT_NUM_BUFFERS);
825                 if (rval) {
826                         qla_printk(KERN_WARNING, ha, "Unable to initialize "
827                             "EFT (%d).\n", rval);
828                         dma_free_coherent(&ha->pdev->dev, EFT_SIZE, eft,
829                             eft_dma);
830                         goto cont_alloc;
831                 }
832
833                 qla_printk(KERN_INFO, ha, "Allocated (%d KB) for EFT...\n",
834                     EFT_SIZE / 1024);
835
836                 eft_size = EFT_SIZE;
837                 memset(eft, 0, eft_size);
838                 ha->eft_dma = eft_dma;
839                 ha->eft = eft;
840         }
841 cont_alloc:
842         req_q_size = ha->request_q_length * sizeof(request_t);
843         rsp_q_size = ha->response_q_length * sizeof(response_t);
844
845         dump_size = offsetof(struct qla2xxx_fw_dump, isp);
846         dump_size += fixed_size + mem_size + req_q_size + rsp_q_size +
847             eft_size;
848
849         ha->fw_dump = vmalloc(dump_size);
850         if (!ha->fw_dump) {
851                 qla_printk(KERN_WARNING, ha, "Unable to allocate (%d KB) for "
852                     "firmware dump!!!\n", dump_size / 1024);
853
854                 if (ha->eft) {
855                         dma_free_coherent(&ha->pdev->dev, eft_size, ha->eft,
856                             ha->eft_dma);
857                         ha->eft = NULL;
858                         ha->eft_dma = 0;
859                 }
860                 return;
861         }
862
863         qla_printk(KERN_INFO, ha, "Allocated (%d KB) for firmware dump...\n",
864             dump_size / 1024);
865
866         ha->fw_dump_len = dump_size;
867         ha->fw_dump->signature[0] = 'Q';
868         ha->fw_dump->signature[1] = 'L';
869         ha->fw_dump->signature[2] = 'G';
870         ha->fw_dump->signature[3] = 'C';
871         ha->fw_dump->version = __constant_htonl(1);
872
873         ha->fw_dump->fixed_size = htonl(fixed_size);
874         ha->fw_dump->mem_size = htonl(mem_size);
875         ha->fw_dump->req_q_size = htonl(req_q_size);
876         ha->fw_dump->rsp_q_size = htonl(rsp_q_size);
877
878         ha->fw_dump->eft_size = htonl(eft_size);
879         ha->fw_dump->eft_addr_l = htonl(LSD(ha->eft_dma));
880         ha->fw_dump->eft_addr_h = htonl(MSD(ha->eft_dma));
881
882         ha->fw_dump->header_size =
883             htonl(offsetof(struct qla2xxx_fw_dump, isp));
884 }
885
886 /**
887  * qla2x00_resize_request_q() - Resize request queue given available ISP memory.
888  * @ha: HA context
889  *
890  * Returns 0 on success.
891  */
892 static void
893 qla2x00_resize_request_q(scsi_qla_host_t *ha)
894 {
895         int rval;
896         uint16_t fw_iocb_cnt = 0;
897         uint16_t request_q_length = REQUEST_ENTRY_CNT_2XXX_EXT_MEM;
898         dma_addr_t request_dma;
899         request_t *request_ring;
900
901         /* Valid only on recent ISPs. */
902         if (IS_QLA2100(ha) || IS_QLA2200(ha))
903                 return;
904
905         /* Retrieve IOCB counts available to the firmware. */
906         rval = qla2x00_get_resource_cnts(ha, NULL, NULL, NULL, &fw_iocb_cnt);
907         if (rval)
908                 return;
909         /* No point in continuing if current settings are sufficient. */
910         if (fw_iocb_cnt < 1024)
911                 return;
912         if (ha->request_q_length >= request_q_length)
913                 return;
914
915         /* Attempt to claim larger area for request queue. */
916         request_ring = dma_alloc_coherent(&ha->pdev->dev,
917             (request_q_length + 1) * sizeof(request_t), &request_dma,
918             GFP_KERNEL);
919         if (request_ring == NULL)
920                 return;
921
922         /* Resize successful, report extensions. */
923         qla_printk(KERN_INFO, ha, "Extended memory detected (%d KB)...\n",
924             (ha->fw_memory_size + 1) / 1024);
925         qla_printk(KERN_INFO, ha, "Resizing request queue depth "
926             "(%d -> %d)...\n", ha->request_q_length, request_q_length);
927
928         /* Clear old allocations. */
929         dma_free_coherent(&ha->pdev->dev,
930             (ha->request_q_length + 1) * sizeof(request_t), ha->request_ring,
931             ha->request_dma);
932
933         /* Begin using larger queue. */
934         ha->request_q_length = request_q_length;
935         ha->request_ring = request_ring;
936         ha->request_dma = request_dma;
937 }
938
939 /**
940  * qla2x00_setup_chip() - Load and start RISC firmware.
941  * @ha: HA context
942  *
943  * Returns 0 on success.
944  */
945 static int
946 qla2x00_setup_chip(scsi_qla_host_t *ha)
947 {
948         int rval;
949         uint32_t srisc_address = 0;
950
951         /* Load firmware sequences */
952         rval = ha->isp_ops.load_risc(ha, &srisc_address);
953         if (rval == QLA_SUCCESS) {
954                 DEBUG(printk("scsi(%ld): Verifying Checksum of loaded RISC "
955                     "code.\n", ha->host_no));
956
957                 rval = qla2x00_verify_checksum(ha, srisc_address);
958                 if (rval == QLA_SUCCESS) {
959                         /* Start firmware execution. */
960                         DEBUG(printk("scsi(%ld): Checksum OK, start "
961                             "firmware.\n", ha->host_no));
962
963                         rval = qla2x00_execute_fw(ha, srisc_address);
964                         /* Retrieve firmware information. */
965                         if (rval == QLA_SUCCESS && ha->fw_major_version == 0) {
966                                 qla2x00_get_fw_version(ha,
967                                     &ha->fw_major_version,
968                                     &ha->fw_minor_version,
969                                     &ha->fw_subminor_version,
970                                     &ha->fw_attributes, &ha->fw_memory_size);
971                                 qla2x00_resize_request_q(ha);
972
973                                 if (ql2xallocfwdump)
974                                         qla2x00_alloc_fw_dump(ha);
975                         }
976                 } else {
977                         DEBUG2(printk(KERN_INFO
978                             "scsi(%ld): ISP Firmware failed checksum.\n",
979                             ha->host_no));
980                 }
981         }
982
983         if (rval) {
984                 DEBUG2_3(printk("scsi(%ld): Setup chip **** FAILED ****.\n",
985                     ha->host_no));
986         }
987
988         return (rval);
989 }
990
991 /**
992  * qla2x00_init_response_q_entries() - Initializes response queue entries.
993  * @ha: HA context
994  *
995  * Beginning of request ring has initialization control block already built
996  * by nvram config routine.
997  *
998  * Returns 0 on success.
999  */
1000 static void
1001 qla2x00_init_response_q_entries(scsi_qla_host_t *ha)
1002 {
1003         uint16_t cnt;
1004         response_t *pkt;
1005
1006         pkt = ha->response_ring_ptr;
1007         for (cnt = 0; cnt < ha->response_q_length; cnt++) {
1008                 pkt->signature = RESPONSE_PROCESSED;
1009                 pkt++;
1010         }
1011
1012 }
1013
1014 /**
1015  * qla2x00_update_fw_options() - Read and process firmware options.
1016  * @ha: HA context
1017  *
1018  * Returns 0 on success.
1019  */
1020 void
1021 qla2x00_update_fw_options(scsi_qla_host_t *ha)
1022 {
1023         uint16_t swing, emphasis, tx_sens, rx_sens;
1024
1025         memset(ha->fw_options, 0, sizeof(ha->fw_options));
1026         qla2x00_get_fw_options(ha, ha->fw_options);
1027
1028         if (IS_QLA2100(ha) || IS_QLA2200(ha))
1029                 return;
1030
1031         /* Serial Link options. */
1032         DEBUG3(printk("scsi(%ld): Serial link options:\n",
1033             ha->host_no));
1034         DEBUG3(qla2x00_dump_buffer((uint8_t *)&ha->fw_seriallink_options,
1035             sizeof(ha->fw_seriallink_options)));
1036
1037         ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
1038         if (ha->fw_seriallink_options[3] & BIT_2) {
1039                 ha->fw_options[1] |= FO1_SET_EMPHASIS_SWING;
1040
1041                 /*  1G settings */
1042                 swing = ha->fw_seriallink_options[2] & (BIT_2 | BIT_1 | BIT_0);
1043                 emphasis = (ha->fw_seriallink_options[2] &
1044                     (BIT_4 | BIT_3)) >> 3;
1045                 tx_sens = ha->fw_seriallink_options[0] &
1046                     (BIT_3 | BIT_2 | BIT_1 | BIT_0);
1047                 rx_sens = (ha->fw_seriallink_options[0] &
1048                     (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
1049                 ha->fw_options[10] = (emphasis << 14) | (swing << 8);
1050                 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
1051                         if (rx_sens == 0x0)
1052                                 rx_sens = 0x3;
1053                         ha->fw_options[10] |= (tx_sens << 4) | rx_sens;
1054                 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
1055                         ha->fw_options[10] |= BIT_5 |
1056                             ((rx_sens & (BIT_1 | BIT_0)) << 2) |
1057                             (tx_sens & (BIT_1 | BIT_0));
1058
1059                 /*  2G settings */
1060                 swing = (ha->fw_seriallink_options[2] &
1061                     (BIT_7 | BIT_6 | BIT_5)) >> 5;
1062                 emphasis = ha->fw_seriallink_options[3] & (BIT_1 | BIT_0);
1063                 tx_sens = ha->fw_seriallink_options[1] &
1064                     (BIT_3 | BIT_2 | BIT_1 | BIT_0);
1065                 rx_sens = (ha->fw_seriallink_options[1] &
1066                     (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
1067                 ha->fw_options[11] = (emphasis << 14) | (swing << 8);
1068                 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
1069                         if (rx_sens == 0x0)
1070                                 rx_sens = 0x3;
1071                         ha->fw_options[11] |= (tx_sens << 4) | rx_sens;
1072                 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
1073                         ha->fw_options[11] |= BIT_5 |
1074                             ((rx_sens & (BIT_1 | BIT_0)) << 2) |
1075                             (tx_sens & (BIT_1 | BIT_0));
1076         }
1077
1078         /* FCP2 options. */
1079         /*  Return command IOCBs without waiting for an ABTS to complete. */
1080         ha->fw_options[3] |= BIT_13;
1081
1082         /* LED scheme. */
1083         if (ha->flags.enable_led_scheme)
1084                 ha->fw_options[2] |= BIT_12;
1085
1086         /* Detect ISP6312. */
1087         if (IS_QLA6312(ha))
1088                 ha->fw_options[2] |= BIT_13;
1089
1090         /* Update firmware options. */
1091         qla2x00_set_fw_options(ha, ha->fw_options);
1092 }
1093
1094 void
1095 qla24xx_update_fw_options(scsi_qla_host_t *ha)
1096 {
1097         int rval;
1098
1099         /* Update Serial Link options. */
1100         if ((le16_to_cpu(ha->fw_seriallink_options24[0]) & BIT_0) == 0)
1101                 return;
1102
1103         rval = qla2x00_set_serdes_params(ha,
1104             le16_to_cpu(ha->fw_seriallink_options24[1]),
1105             le16_to_cpu(ha->fw_seriallink_options24[2]),
1106             le16_to_cpu(ha->fw_seriallink_options24[3]));
1107         if (rval != QLA_SUCCESS) {
1108                 qla_printk(KERN_WARNING, ha,
1109                     "Unable to update Serial Link options (%x).\n", rval);
1110         }
1111 }
1112
1113 void
1114 qla2x00_config_rings(struct scsi_qla_host *ha)
1115 {
1116         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1117
1118         /* Setup ring parameters in initialization control block. */
1119         ha->init_cb->request_q_outpointer = __constant_cpu_to_le16(0);
1120         ha->init_cb->response_q_inpointer = __constant_cpu_to_le16(0);
1121         ha->init_cb->request_q_length = cpu_to_le16(ha->request_q_length);
1122         ha->init_cb->response_q_length = cpu_to_le16(ha->response_q_length);
1123         ha->init_cb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
1124         ha->init_cb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
1125         ha->init_cb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
1126         ha->init_cb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
1127
1128         WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), 0);
1129         WRT_REG_WORD(ISP_REQ_Q_OUT(ha, reg), 0);
1130         WRT_REG_WORD(ISP_RSP_Q_IN(ha, reg), 0);
1131         WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), 0);
1132         RD_REG_WORD(ISP_RSP_Q_OUT(ha, reg));            /* PCI Posting. */
1133 }
1134
1135 void
1136 qla24xx_config_rings(struct scsi_qla_host *ha)
1137 {
1138         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1139         struct init_cb_24xx *icb;
1140
1141         /* Setup ring parameters in initialization control block. */
1142         icb = (struct init_cb_24xx *)ha->init_cb;
1143         icb->request_q_outpointer = __constant_cpu_to_le16(0);
1144         icb->response_q_inpointer = __constant_cpu_to_le16(0);
1145         icb->request_q_length = cpu_to_le16(ha->request_q_length);
1146         icb->response_q_length = cpu_to_le16(ha->response_q_length);
1147         icb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
1148         icb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
1149         icb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
1150         icb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
1151
1152         WRT_REG_DWORD(&reg->req_q_in, 0);
1153         WRT_REG_DWORD(&reg->req_q_out, 0);
1154         WRT_REG_DWORD(&reg->rsp_q_in, 0);
1155         WRT_REG_DWORD(&reg->rsp_q_out, 0);
1156         RD_REG_DWORD(&reg->rsp_q_out);
1157 }
1158
1159 /**
1160  * qla2x00_init_rings() - Initializes firmware.
1161  * @ha: HA context
1162  *
1163  * Beginning of request ring has initialization control block already built
1164  * by nvram config routine.
1165  *
1166  * Returns 0 on success.
1167  */
1168 static int
1169 qla2x00_init_rings(scsi_qla_host_t *ha)
1170 {
1171         int     rval;
1172         unsigned long flags = 0;
1173         int cnt;
1174
1175         spin_lock_irqsave(&ha->hardware_lock, flags);
1176
1177         /* Clear outstanding commands array. */
1178         for (cnt = 0; cnt < MAX_OUTSTANDING_COMMANDS; cnt++)
1179                 ha->outstanding_cmds[cnt] = NULL;
1180
1181         ha->current_outstanding_cmd = 0;
1182
1183         /* Clear RSCN queue. */
1184         ha->rscn_in_ptr = 0;
1185         ha->rscn_out_ptr = 0;
1186
1187         /* Initialize firmware. */
1188         ha->request_ring_ptr  = ha->request_ring;
1189         ha->req_ring_index    = 0;
1190         ha->req_q_cnt         = ha->request_q_length;
1191         ha->response_ring_ptr = ha->response_ring;
1192         ha->rsp_ring_index    = 0;
1193
1194         /* Initialize response queue entries */
1195         qla2x00_init_response_q_entries(ha);
1196
1197         ha->isp_ops.config_rings(ha);
1198
1199         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1200
1201         /* Update any ISP specific firmware options before initialization. */
1202         ha->isp_ops.update_fw_options(ha);
1203
1204         DEBUG(printk("scsi(%ld): Issue init firmware.\n", ha->host_no));
1205         rval = qla2x00_init_firmware(ha, ha->init_cb_size);
1206         if (rval) {
1207                 DEBUG2_3(printk("scsi(%ld): Init firmware **** FAILED ****.\n",
1208                     ha->host_no));
1209         } else {
1210                 DEBUG3(printk("scsi(%ld): Init firmware -- success.\n",
1211                     ha->host_no));
1212         }
1213
1214         return (rval);
1215 }
1216
1217 /**
1218  * qla2x00_fw_ready() - Waits for firmware ready.
1219  * @ha: HA context
1220  *
1221  * Returns 0 on success.
1222  */
1223 static int
1224 qla2x00_fw_ready(scsi_qla_host_t *ha)
1225 {
1226         int             rval;
1227         unsigned long   wtime, mtime;
1228         uint16_t        min_wait;       /* Minimum wait time if loop is down */
1229         uint16_t        wait_time;      /* Wait time if loop is coming ready */
1230         uint16_t        fw_state;
1231
1232         rval = QLA_SUCCESS;
1233
1234         /* 20 seconds for loop down. */
1235         min_wait = 20;
1236
1237         /*
1238          * Firmware should take at most one RATOV to login, plus 5 seconds for
1239          * our own processing.
1240          */
1241         if ((wait_time = (ha->retry_count*ha->login_timeout) + 5) < min_wait) {
1242                 wait_time = min_wait;
1243         }
1244
1245         /* Min wait time if loop down */
1246         mtime = jiffies + (min_wait * HZ);
1247
1248         /* wait time before firmware ready */
1249         wtime = jiffies + (wait_time * HZ);
1250
1251         /* Wait for ISP to finish LIP */
1252         if (!ha->flags.init_done)
1253                 qla_printk(KERN_INFO, ha, "Waiting for LIP to complete...\n");
1254
1255         DEBUG3(printk("scsi(%ld): Waiting for LIP to complete...\n",
1256             ha->host_no));
1257
1258         do {
1259                 rval = qla2x00_get_firmware_state(ha, &fw_state);
1260                 if (rval == QLA_SUCCESS) {
1261                         if (fw_state < FSTATE_LOSS_OF_SYNC) {
1262                                 ha->device_flags &= ~DFLG_NO_CABLE;
1263                         }
1264                         if (fw_state == FSTATE_READY) {
1265                                 DEBUG(printk("scsi(%ld): F/W Ready - OK \n",
1266                                     ha->host_no));
1267
1268                                 qla2x00_get_retry_cnt(ha, &ha->retry_count,
1269                                     &ha->login_timeout, &ha->r_a_tov);
1270
1271                                 rval = QLA_SUCCESS;
1272                                 break;
1273                         }
1274
1275                         rval = QLA_FUNCTION_FAILED;
1276
1277                         if (atomic_read(&ha->loop_down_timer) &&
1278                             fw_state != FSTATE_READY) {
1279                                 /* Loop down. Timeout on min_wait for states
1280                                  * other than Wait for Login.
1281                                  */
1282                                 if (time_after_eq(jiffies, mtime)) {
1283                                         qla_printk(KERN_INFO, ha,
1284                                             "Cable is unplugged...\n");
1285
1286                                         ha->device_flags |= DFLG_NO_CABLE;
1287                                         break;
1288                                 }
1289                         }
1290                 } else {
1291                         /* Mailbox cmd failed. Timeout on min_wait. */
1292                         if (time_after_eq(jiffies, mtime))
1293                                 break;
1294                 }
1295
1296                 if (time_after_eq(jiffies, wtime))
1297                         break;
1298
1299                 /* Delay for a while */
1300                 msleep(500);
1301
1302                 DEBUG3(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
1303                     ha->host_no, fw_state, jiffies));
1304         } while (1);
1305
1306         DEBUG(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
1307             ha->host_no, fw_state, jiffies));
1308
1309         if (rval) {
1310                 DEBUG2_3(printk("scsi(%ld): Firmware ready **** FAILED ****.\n",
1311                     ha->host_no));
1312         }
1313
1314         return (rval);
1315 }
1316
1317 /*
1318 *  qla2x00_configure_hba
1319 *      Setup adapter context.
1320 *
1321 * Input:
1322 *      ha = adapter state pointer.
1323 *
1324 * Returns:
1325 *      0 = success
1326 *
1327 * Context:
1328 *      Kernel context.
1329 */
1330 static int
1331 qla2x00_configure_hba(scsi_qla_host_t *ha)
1332 {
1333         int       rval;
1334         uint16_t      loop_id;
1335         uint16_t      topo;
1336         uint8_t       al_pa;
1337         uint8_t       area;
1338         uint8_t       domain;
1339         char            connect_type[22];
1340
1341         /* Get host addresses. */
1342         rval = qla2x00_get_adapter_id(ha,
1343             &loop_id, &al_pa, &area, &domain, &topo);
1344         if (rval != QLA_SUCCESS) {
1345                 if (LOOP_TRANSITION(ha) || atomic_read(&ha->loop_down_timer) ||
1346                     (rval == QLA_COMMAND_ERROR && loop_id == 0x7)) {
1347                         DEBUG2(printk("%s(%ld) Loop is in a transition state\n",
1348                             __func__, ha->host_no));
1349                 } else {
1350                         qla_printk(KERN_WARNING, ha,
1351                             "ERROR -- Unable to get host loop ID.\n");
1352                         set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
1353                 }
1354                 return (rval);
1355         }
1356
1357         if (topo == 4) {
1358                 qla_printk(KERN_INFO, ha,
1359                         "Cannot get topology - retrying.\n");
1360                 return (QLA_FUNCTION_FAILED);
1361         }
1362
1363         ha->loop_id = loop_id;
1364
1365         /* initialize */
1366         ha->min_external_loopid = SNS_FIRST_LOOP_ID;
1367         ha->operating_mode = LOOP;
1368
1369         switch (topo) {
1370         case 0:
1371                 DEBUG3(printk("scsi(%ld): HBA in NL topology.\n",
1372                     ha->host_no));
1373                 ha->current_topology = ISP_CFG_NL;
1374                 strcpy(connect_type, "(Loop)");
1375                 break;
1376
1377         case 1:
1378                 DEBUG3(printk("scsi(%ld): HBA in FL topology.\n",
1379                     ha->host_no));
1380                 ha->current_topology = ISP_CFG_FL;
1381                 strcpy(connect_type, "(FL_Port)");
1382                 break;
1383
1384         case 2:
1385                 DEBUG3(printk("scsi(%ld): HBA in N P2P topology.\n",
1386                     ha->host_no));
1387                 ha->operating_mode = P2P;
1388                 ha->current_topology = ISP_CFG_N;
1389                 strcpy(connect_type, "(N_Port-to-N_Port)");
1390                 break;
1391
1392         case 3:
1393                 DEBUG3(printk("scsi(%ld): HBA in F P2P topology.\n",
1394                     ha->host_no));
1395                 ha->operating_mode = P2P;
1396                 ha->current_topology = ISP_CFG_F;
1397                 strcpy(connect_type, "(F_Port)");
1398                 break;
1399
1400         default:
1401                 DEBUG3(printk("scsi(%ld): HBA in unknown topology %x. "
1402                     "Using NL.\n",
1403                     ha->host_no, topo));
1404                 ha->current_topology = ISP_CFG_NL;
1405                 strcpy(connect_type, "(Loop)");
1406                 break;
1407         }
1408
1409         /* Save Host port and loop ID. */
1410         /* byte order - Big Endian */
1411         ha->d_id.b.domain = domain;
1412         ha->d_id.b.area = area;
1413         ha->d_id.b.al_pa = al_pa;
1414
1415         if (!ha->flags.init_done)
1416                 qla_printk(KERN_INFO, ha,
1417                     "Topology - %s, Host Loop address 0x%x\n",
1418                     connect_type, ha->loop_id);
1419
1420         if (rval) {
1421                 DEBUG2_3(printk("scsi(%ld): FAILED.\n", ha->host_no));
1422         } else {
1423                 DEBUG3(printk("scsi(%ld): exiting normally.\n", ha->host_no));
1424         }
1425
1426         return(rval);
1427 }
1428
1429 /*
1430 * NVRAM configuration for ISP 2xxx
1431 *
1432 * Input:
1433 *      ha                = adapter block pointer.
1434 *
1435 * Output:
1436 *      initialization control block in response_ring
1437 *      host adapters parameters in host adapter block
1438 *
1439 * Returns:
1440 *      0 = success.
1441 */
1442 int
1443 qla2x00_nvram_config(scsi_qla_host_t *ha)
1444 {
1445         int             rval;
1446         uint8_t         chksum = 0;
1447         uint16_t        cnt;
1448         uint8_t         *dptr1, *dptr2;
1449         init_cb_t       *icb = ha->init_cb;
1450         nvram_t         *nv = (nvram_t *)ha->request_ring;
1451         uint8_t         *ptr = (uint8_t *)ha->request_ring;
1452         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1453
1454         rval = QLA_SUCCESS;
1455
1456         /* Determine NVRAM starting address. */
1457         ha->nvram_size = sizeof(nvram_t);
1458         ha->nvram_base = 0;
1459         if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha))
1460                 if ((RD_REG_WORD(&reg->ctrl_status) >> 14) == 1)
1461                         ha->nvram_base = 0x80;
1462
1463         /* Get NVRAM data and calculate checksum. */
1464         ha->isp_ops.read_nvram(ha, ptr, ha->nvram_base, ha->nvram_size);
1465         for (cnt = 0, chksum = 0; cnt < ha->nvram_size; cnt++)
1466                 chksum += *ptr++;
1467
1468         DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
1469         DEBUG5(qla2x00_dump_buffer((uint8_t *)ha->request_ring,
1470             ha->nvram_size));
1471
1472         /* Bad NVRAM data, set defaults parameters. */
1473         if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' ||
1474             nv->id[2] != 'P' || nv->id[3] != ' ' || nv->nvram_version < 1) {
1475                 /* Reset NVRAM data. */
1476                 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
1477                     "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
1478                     nv->nvram_version);
1479                 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
1480                     "invalid -- WWPN) defaults.\n");
1481
1482                 /*
1483                  * Set default initialization control block.
1484                  */
1485                 memset(nv, 0, ha->nvram_size);
1486                 nv->parameter_block_version = ICB_VERSION;
1487
1488                 if (IS_QLA23XX(ha)) {
1489                         nv->firmware_options[0] = BIT_2 | BIT_1;
1490                         nv->firmware_options[1] = BIT_7 | BIT_5;
1491                         nv->add_firmware_options[0] = BIT_5;
1492                         nv->add_firmware_options[1] = BIT_5 | BIT_4;
1493                         nv->frame_payload_size = __constant_cpu_to_le16(2048);
1494                         nv->special_options[1] = BIT_7;
1495                 } else if (IS_QLA2200(ha)) {
1496                         nv->firmware_options[0] = BIT_2 | BIT_1;
1497                         nv->firmware_options[1] = BIT_7 | BIT_5;
1498                         nv->add_firmware_options[0] = BIT_5;
1499                         nv->add_firmware_options[1] = BIT_5 | BIT_4;
1500                         nv->frame_payload_size = __constant_cpu_to_le16(1024);
1501                 } else if (IS_QLA2100(ha)) {
1502                         nv->firmware_options[0] = BIT_3 | BIT_1;
1503                         nv->firmware_options[1] = BIT_5;
1504                         nv->frame_payload_size = __constant_cpu_to_le16(1024);
1505                 }
1506
1507                 nv->max_iocb_allocation = __constant_cpu_to_le16(256);
1508                 nv->execution_throttle = __constant_cpu_to_le16(16);
1509                 nv->retry_count = 8;
1510                 nv->retry_delay = 1;
1511
1512                 nv->port_name[0] = 33;
1513                 nv->port_name[3] = 224;
1514                 nv->port_name[4] = 139;
1515
1516                 nv->login_timeout = 4;
1517
1518                 /*
1519                  * Set default host adapter parameters
1520                  */
1521                 nv->host_p[1] = BIT_2;
1522                 nv->reset_delay = 5;
1523                 nv->port_down_retry_count = 8;
1524                 nv->max_luns_per_target = __constant_cpu_to_le16(8);
1525                 nv->link_down_timeout = 60;
1526
1527                 rval = 1;
1528         }
1529
1530 #if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
1531         /*
1532          * The SN2 does not provide BIOS emulation which means you can't change
1533          * potentially bogus BIOS settings. Force the use of default settings
1534          * for link rate and frame size.  Hope that the rest of the settings
1535          * are valid.
1536          */
1537         if (ia64_platform_is("sn2")) {
1538                 nv->frame_payload_size = __constant_cpu_to_le16(2048);
1539                 if (IS_QLA23XX(ha))
1540                         nv->special_options[1] = BIT_7;
1541         }
1542 #endif
1543
1544         /* Reset Initialization control block */
1545         memset(icb, 0, ha->init_cb_size);
1546
1547         /*
1548          * Setup driver NVRAM options.
1549          */
1550         nv->firmware_options[0] |= (BIT_6 | BIT_1);
1551         nv->firmware_options[0] &= ~(BIT_5 | BIT_4);
1552         nv->firmware_options[1] |= (BIT_5 | BIT_0);
1553         nv->firmware_options[1] &= ~BIT_4;
1554
1555         if (IS_QLA23XX(ha)) {
1556                 nv->firmware_options[0] |= BIT_2;
1557                 nv->firmware_options[0] &= ~BIT_3;
1558                 nv->add_firmware_options[1] |= BIT_5 | BIT_4;
1559
1560                 if (IS_QLA2300(ha)) {
1561                         if (ha->fb_rev == FPM_2310) {
1562                                 strcpy(ha->model_number, "QLA2310");
1563                         } else {
1564                                 strcpy(ha->model_number, "QLA2300");
1565                         }
1566                 } else {
1567                         if (rval == 0 &&
1568                             memcmp(nv->model_number, BINZERO,
1569                                     sizeof(nv->model_number)) != 0) {
1570                                 char *st, *en;
1571
1572                                 strncpy(ha->model_number, nv->model_number,
1573                                     sizeof(nv->model_number));
1574                                 st = en = ha->model_number;
1575                                 en += sizeof(nv->model_number) - 1;
1576                                 while (en > st) {
1577                                         if (*en != 0x20 && *en != 0x00)
1578                                                 break;
1579                                         *en-- = '\0';
1580                                 }
1581                         } else {
1582                                 uint16_t        index;
1583
1584                                 index = (ha->pdev->subsystem_device & 0xff);
1585                                 if (index < QLA_MODEL_NAMES) {
1586                                         strcpy(ha->model_number,
1587                                             qla2x00_model_name[index * 2]);
1588                                         ha->model_desc =
1589                                             qla2x00_model_name[index * 2 + 1];
1590                                 } else {
1591                                         strcpy(ha->model_number, "QLA23xx");
1592                                 }
1593                         }
1594                 }
1595         } else if (IS_QLA2200(ha)) {
1596                 nv->firmware_options[0] |= BIT_2;
1597                 /*
1598                  * 'Point-to-point preferred, else loop' is not a safe
1599                  * connection mode setting.
1600                  */
1601                 if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
1602                     (BIT_5 | BIT_4)) {
1603                         /* Force 'loop preferred, else point-to-point'. */
1604                         nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
1605                         nv->add_firmware_options[0] |= BIT_5;
1606                 }
1607                 strcpy(ha->model_number, "QLA22xx");
1608         } else /*if (IS_QLA2100(ha))*/ {
1609                 strcpy(ha->model_number, "QLA2100");
1610         }
1611
1612         /*
1613          * Copy over NVRAM RISC parameter block to initialization control block.
1614          */
1615         dptr1 = (uint8_t *)icb;
1616         dptr2 = (uint8_t *)&nv->parameter_block_version;
1617         cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
1618         while (cnt--)
1619                 *dptr1++ = *dptr2++;
1620
1621         /* Copy 2nd half. */
1622         dptr1 = (uint8_t *)icb->add_firmware_options;
1623         cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
1624         while (cnt--)
1625                 *dptr1++ = *dptr2++;
1626
1627         /* Use alternate WWN? */
1628         if (nv->host_p[1] & BIT_7) {
1629                 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
1630                 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
1631         }
1632
1633         /* Prepare nodename */
1634         if ((icb->firmware_options[1] & BIT_6) == 0) {
1635                 /*
1636                  * Firmware will apply the following mask if the nodename was
1637                  * not provided.
1638                  */
1639                 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
1640                 icb->node_name[0] &= 0xF0;
1641         }
1642
1643         /*
1644          * Set host adapter parameters.
1645          */
1646         if (nv->host_p[0] & BIT_7)
1647                 extended_error_logging = 1;
1648         ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
1649         /* Always load RISC code on non ISP2[12]00 chips. */
1650         if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
1651                 ha->flags.disable_risc_code_load = 0;
1652         ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
1653         ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
1654         ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
1655         ha->flags.enable_led_scheme = (nv->special_options[1] & BIT_4) ? 1 : 0;
1656         ha->flags.disable_serdes = 0;
1657
1658         ha->operating_mode =
1659             (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
1660
1661         memcpy(ha->fw_seriallink_options, nv->seriallink_options,
1662             sizeof(ha->fw_seriallink_options));
1663
1664         /* save HBA serial number */
1665         ha->serial0 = icb->port_name[5];
1666         ha->serial1 = icb->port_name[6];
1667         ha->serial2 = icb->port_name[7];
1668         ha->node_name = icb->node_name;
1669         ha->port_name = icb->port_name;
1670
1671         icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
1672
1673         ha->retry_count = nv->retry_count;
1674
1675         /* Set minimum login_timeout to 4 seconds. */
1676         if (nv->login_timeout < ql2xlogintimeout)
1677                 nv->login_timeout = ql2xlogintimeout;
1678         if (nv->login_timeout < 4)
1679                 nv->login_timeout = 4;
1680         ha->login_timeout = nv->login_timeout;
1681         icb->login_timeout = nv->login_timeout;
1682
1683         /* Set minimum RATOV to 200 tenths of a second. */
1684         ha->r_a_tov = 200;
1685
1686         ha->loop_reset_delay = nv->reset_delay;
1687
1688         /* Link Down Timeout = 0:
1689          *
1690          *      When Port Down timer expires we will start returning
1691          *      I/O's to OS with "DID_NO_CONNECT".
1692          *
1693          * Link Down Timeout != 0:
1694          *
1695          *       The driver waits for the link to come up after link down
1696          *       before returning I/Os to OS with "DID_NO_CONNECT".
1697          */
1698         if (nv->link_down_timeout == 0) {
1699                 ha->loop_down_abort_time =
1700                     (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
1701         } else {
1702                 ha->link_down_timeout =  nv->link_down_timeout;
1703                 ha->loop_down_abort_time =
1704                     (LOOP_DOWN_TIME - ha->link_down_timeout);
1705         }
1706
1707         /*
1708          * Need enough time to try and get the port back.
1709          */
1710         ha->port_down_retry_count = nv->port_down_retry_count;
1711         if (qlport_down_retry)
1712                 ha->port_down_retry_count = qlport_down_retry;
1713         /* Set login_retry_count */
1714         ha->login_retry_count  = nv->retry_count;
1715         if (ha->port_down_retry_count == nv->port_down_retry_count &&
1716             ha->port_down_retry_count > 3)
1717                 ha->login_retry_count = ha->port_down_retry_count;
1718         else if (ha->port_down_retry_count > (int)ha->login_retry_count)
1719                 ha->login_retry_count = ha->port_down_retry_count;
1720         if (ql2xloginretrycount)
1721                 ha->login_retry_count = ql2xloginretrycount;
1722
1723         icb->lun_enables = __constant_cpu_to_le16(0);
1724         icb->command_resource_count = 0;
1725         icb->immediate_notify_resource_count = 0;
1726         icb->timeout = __constant_cpu_to_le16(0);
1727
1728         if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
1729                 /* Enable RIO */
1730                 icb->firmware_options[0] &= ~BIT_3;
1731                 icb->add_firmware_options[0] &=
1732                     ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
1733                 icb->add_firmware_options[0] |= BIT_2;
1734                 icb->response_accumulation_timer = 3;
1735                 icb->interrupt_delay_timer = 5;
1736
1737                 ha->flags.process_response_queue = 1;
1738         } else {
1739                 /* Enable ZIO. */
1740                 if (!ha->flags.init_done) {
1741                         ha->zio_mode = icb->add_firmware_options[0] &
1742                             (BIT_3 | BIT_2 | BIT_1 | BIT_0);
1743                         ha->zio_timer = icb->interrupt_delay_timer ?
1744                             icb->interrupt_delay_timer: 2;
1745                 }
1746                 icb->add_firmware_options[0] &=
1747                     ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
1748                 ha->flags.process_response_queue = 0;
1749                 if (ha->zio_mode != QLA_ZIO_DISABLED) {
1750                         ha->zio_mode = QLA_ZIO_MODE_6;
1751
1752                         DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer "
1753                             "delay (%d us).\n", ha->host_no, ha->zio_mode,
1754                             ha->zio_timer * 100));
1755                         qla_printk(KERN_INFO, ha,
1756                             "ZIO mode %d enabled; timer delay (%d us).\n",
1757                             ha->zio_mode, ha->zio_timer * 100);
1758
1759                         icb->add_firmware_options[0] |= (uint8_t)ha->zio_mode;
1760                         icb->interrupt_delay_timer = (uint8_t)ha->zio_timer;
1761                         ha->flags.process_response_queue = 1;
1762                 }
1763         }
1764
1765         if (rval) {
1766                 DEBUG2_3(printk(KERN_WARNING
1767                     "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
1768         }
1769         return (rval);
1770 }
1771
1772 static void
1773 qla2x00_rport_del(void *data)
1774 {
1775         fc_port_t *fcport = data;
1776         struct fc_rport *rport;
1777         unsigned long flags;
1778
1779         spin_lock_irqsave(&fcport->rport_lock, flags);
1780         rport = fcport->drport;
1781         fcport->drport = NULL;
1782         spin_unlock_irqrestore(&fcport->rport_lock, flags);
1783         if (rport)
1784                 fc_remote_port_delete(rport);
1785
1786 }
1787
1788 /**
1789  * qla2x00_alloc_fcport() - Allocate a generic fcport.
1790  * @ha: HA context
1791  * @flags: allocation flags
1792  *
1793  * Returns a pointer to the allocated fcport, or NULL, if none available.
1794  */
1795 static fc_port_t *
1796 qla2x00_alloc_fcport(scsi_qla_host_t *ha, gfp_t flags)
1797 {
1798         fc_port_t *fcport;
1799
1800         fcport = kmalloc(sizeof(fc_port_t), flags);
1801         if (fcport == NULL)
1802                 return (fcport);
1803
1804         /* Setup fcport template structure. */
1805         memset(fcport, 0, sizeof (fc_port_t));
1806         fcport->ha = ha;
1807         fcport->port_type = FCT_UNKNOWN;
1808         fcport->loop_id = FC_NO_LOOP_ID;
1809         atomic_set(&fcport->state, FCS_UNCONFIGURED);
1810         fcport->flags = FCF_RLC_SUPPORT;
1811         fcport->supported_classes = FC_COS_UNSPECIFIED;
1812         spin_lock_init(&fcport->rport_lock);
1813
1814         return (fcport);
1815 }
1816
1817 /*
1818  * qla2x00_configure_loop
1819  *      Updates Fibre Channel Device Database with what is actually on loop.
1820  *
1821  * Input:
1822  *      ha                = adapter block pointer.
1823  *
1824  * Returns:
1825  *      0 = success.
1826  *      1 = error.
1827  *      2 = database was full and device was not configured.
1828  */
1829 static int
1830 qla2x00_configure_loop(scsi_qla_host_t *ha)
1831 {
1832         int  rval;
1833         unsigned long flags, save_flags;
1834
1835         rval = QLA_SUCCESS;
1836
1837         /* Get Initiator ID */
1838         if (test_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags)) {
1839                 rval = qla2x00_configure_hba(ha);
1840                 if (rval != QLA_SUCCESS) {
1841                         DEBUG(printk("scsi(%ld): Unable to configure HBA.\n",
1842                             ha->host_no));
1843                         return (rval);
1844                 }
1845         }
1846
1847         save_flags = flags = ha->dpc_flags;
1848         DEBUG(printk("scsi(%ld): Configure loop -- dpc flags =0x%lx\n",
1849             ha->host_no, flags));
1850
1851         /*
1852          * If we have both an RSCN and PORT UPDATE pending then handle them
1853          * both at the same time.
1854          */
1855         clear_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1856         clear_bit(RSCN_UPDATE, &ha->dpc_flags);
1857
1858         /* Determine what we need to do */
1859         if (ha->current_topology == ISP_CFG_FL &&
1860             (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1861
1862                 ha->flags.rscn_queue_overflow = 1;
1863                 set_bit(RSCN_UPDATE, &flags);
1864
1865         } else if (ha->current_topology == ISP_CFG_F &&
1866             (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1867
1868                 ha->flags.rscn_queue_overflow = 1;
1869                 set_bit(RSCN_UPDATE, &flags);
1870                 clear_bit(LOCAL_LOOP_UPDATE, &flags);
1871
1872         } else if (ha->current_topology == ISP_CFG_N) {
1873                 clear_bit(RSCN_UPDATE, &flags);
1874
1875         } else if (!ha->flags.online ||
1876             (test_bit(ABORT_ISP_ACTIVE, &flags))) {
1877
1878                 ha->flags.rscn_queue_overflow = 1;
1879                 set_bit(RSCN_UPDATE, &flags);
1880                 set_bit(LOCAL_LOOP_UPDATE, &flags);
1881         }
1882
1883         if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
1884                 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1885                         rval = QLA_FUNCTION_FAILED;
1886                 } else {
1887                         rval = qla2x00_configure_local_loop(ha);
1888                 }
1889         }
1890
1891         if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
1892                 if (LOOP_TRANSITION(ha)) {
1893                         rval = QLA_FUNCTION_FAILED;
1894                 } else {
1895                         rval = qla2x00_configure_fabric(ha);
1896                 }
1897         }
1898
1899         if (rval == QLA_SUCCESS) {
1900                 if (atomic_read(&ha->loop_down_timer) ||
1901                     test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1902                         rval = QLA_FUNCTION_FAILED;
1903                 } else {
1904                         atomic_set(&ha->loop_state, LOOP_READY);
1905
1906                         DEBUG(printk("scsi(%ld): LOOP READY\n", ha->host_no));
1907                 }
1908         }
1909
1910         if (rval) {
1911                 DEBUG2_3(printk("%s(%ld): *** FAILED ***\n",
1912                     __func__, ha->host_no));
1913         } else {
1914                 DEBUG3(printk("%s: exiting normally\n", __func__));
1915         }
1916
1917         /* Restore state if a resync event occured during processing */
1918         if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1919                 if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
1920                         set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1921                 if (test_bit(RSCN_UPDATE, &save_flags))
1922                         set_bit(RSCN_UPDATE, &ha->dpc_flags);
1923         }
1924
1925         return (rval);
1926 }
1927
1928
1929
1930 /*
1931  * qla2x00_configure_local_loop
1932  *      Updates Fibre Channel Device Database with local loop devices.
1933  *
1934  * Input:
1935  *      ha = adapter block pointer.
1936  *
1937  * Returns:
1938  *      0 = success.
1939  */
1940 static int
1941 qla2x00_configure_local_loop(scsi_qla_host_t *ha)
1942 {
1943         int             rval, rval2;
1944         int             found_devs;
1945         int             found;
1946         fc_port_t       *fcport, *new_fcport;
1947
1948         uint16_t        index;
1949         uint16_t        entries;
1950         char            *id_iter;
1951         uint16_t        loop_id;
1952         uint8_t         domain, area, al_pa;
1953
1954         found_devs = 0;
1955         new_fcport = NULL;
1956         entries = MAX_FIBRE_DEVICES;
1957
1958         DEBUG3(printk("scsi(%ld): Getting FCAL position map\n", ha->host_no));
1959         DEBUG3(qla2x00_get_fcal_position_map(ha, NULL));
1960
1961         /* Get list of logged in devices. */
1962         memset(ha->gid_list, 0, GID_LIST_SIZE);
1963         rval = qla2x00_get_id_list(ha, ha->gid_list, ha->gid_list_dma,
1964             &entries);
1965         if (rval != QLA_SUCCESS)
1966                 goto cleanup_allocation;
1967
1968         DEBUG3(printk("scsi(%ld): Entries in ID list (%d)\n",
1969             ha->host_no, entries));
1970         DEBUG3(qla2x00_dump_buffer((uint8_t *)ha->gid_list,
1971             entries * sizeof(struct gid_list_info)));
1972
1973         /* Allocate temporary fcport for any new fcports discovered. */
1974         new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
1975         if (new_fcport == NULL) {
1976                 rval = QLA_MEMORY_ALLOC_FAILED;
1977                 goto cleanup_allocation;
1978         }
1979         new_fcport->flags &= ~FCF_FABRIC_DEVICE;
1980
1981         /*
1982          * Mark local devices that were present with FCF_DEVICE_LOST for now.
1983          */
1984         list_for_each_entry(fcport, &ha->fcports, list) {
1985                 if (atomic_read(&fcport->state) == FCS_ONLINE &&
1986                     fcport->port_type != FCT_BROADCAST &&
1987                     (fcport->flags & FCF_FABRIC_DEVICE) == 0) {
1988
1989                         DEBUG(printk("scsi(%ld): Marking port lost, "
1990                             "loop_id=0x%04x\n",
1991                             ha->host_no, fcport->loop_id));
1992
1993                         atomic_set(&fcport->state, FCS_DEVICE_LOST);
1994                         fcport->flags &= ~FCF_FARP_DONE;
1995                 }
1996         }
1997
1998         /* Add devices to port list. */
1999         id_iter = (char *)ha->gid_list;
2000         for (index = 0; index < entries; index++) {
2001                 domain = ((struct gid_list_info *)id_iter)->domain;
2002                 area = ((struct gid_list_info *)id_iter)->area;
2003                 al_pa = ((struct gid_list_info *)id_iter)->al_pa;
2004                 if (IS_QLA2100(ha) || IS_QLA2200(ha))
2005                         loop_id = (uint16_t)
2006                             ((struct gid_list_info *)id_iter)->loop_id_2100;
2007                 else
2008                         loop_id = le16_to_cpu(
2009                             ((struct gid_list_info *)id_iter)->loop_id);
2010                 id_iter += ha->gid_list_info_size;
2011
2012                 /* Bypass reserved domain fields. */
2013                 if ((domain & 0xf0) == 0xf0)
2014                         continue;
2015
2016                 /* Bypass if not same domain and area of adapter. */
2017                 if (area && domain &&
2018                     (area != ha->d_id.b.area || domain != ha->d_id.b.domain))
2019                         continue;
2020
2021                 /* Bypass invalid local loop ID. */
2022                 if (loop_id > LAST_LOCAL_LOOP_ID)
2023                         continue;
2024
2025                 /* Fill in member data. */
2026                 new_fcport->d_id.b.domain = domain;
2027                 new_fcport->d_id.b.area = area;
2028                 new_fcport->d_id.b.al_pa = al_pa;
2029                 new_fcport->loop_id = loop_id;
2030                 rval2 = qla2x00_get_port_database(ha, new_fcport, 0);
2031                 if (rval2 != QLA_SUCCESS) {
2032                         DEBUG2(printk("scsi(%ld): Failed to retrieve fcport "
2033                             "information -- get_port_database=%x, "
2034                             "loop_id=0x%04x\n",
2035                             ha->host_no, rval2, new_fcport->loop_id));
2036                         DEBUG2(printk("scsi(%ld): Scheduling resync...\n",
2037                             ha->host_no));
2038                         set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
2039                         continue;
2040                 }
2041
2042                 /* Check for matching device in port list. */
2043                 found = 0;
2044                 fcport = NULL;
2045                 list_for_each_entry(fcport, &ha->fcports, list) {
2046                         if (memcmp(new_fcport->port_name, fcport->port_name,
2047                             WWN_SIZE))
2048                                 continue;
2049
2050                         fcport->flags &= ~(FCF_FABRIC_DEVICE |
2051                             FCF_PERSISTENT_BOUND);
2052                         fcport->loop_id = new_fcport->loop_id;
2053                         fcport->port_type = new_fcport->port_type;
2054                         fcport->d_id.b24 = new_fcport->d_id.b24;
2055                         memcpy(fcport->node_name, new_fcport->node_name,
2056                             WWN_SIZE);
2057
2058                         found++;
2059                         break;
2060                 }
2061
2062                 if (!found) {
2063                         /* New device, add to fcports list. */
2064                         new_fcport->flags &= ~FCF_PERSISTENT_BOUND;
2065                         list_add_tail(&new_fcport->list, &ha->fcports);
2066
2067                         /* Allocate a new replacement fcport. */
2068                         fcport = new_fcport;
2069                         new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2070                         if (new_fcport == NULL) {
2071                                 rval = QLA_MEMORY_ALLOC_FAILED;
2072                                 goto cleanup_allocation;
2073                         }
2074                         new_fcport->flags &= ~FCF_FABRIC_DEVICE;
2075                 }
2076
2077                 /* Base iIDMA settings on HBA port speed. */
2078                 switch (ha->link_data_rate) {
2079                 case PORT_SPEED_1GB:
2080                         fcport->fp_speed = cpu_to_be16(BIT_15);
2081                         break;
2082                 case PORT_SPEED_2GB:
2083                         fcport->fp_speed = cpu_to_be16(BIT_14);
2084                         break;
2085                 case PORT_SPEED_4GB:
2086                         fcport->fp_speed = cpu_to_be16(BIT_13);
2087                         break;
2088                 }
2089
2090                 qla2x00_update_fcport(ha, fcport);
2091
2092                 found_devs++;
2093         }
2094
2095 cleanup_allocation:
2096         kfree(new_fcport);
2097
2098         if (rval != QLA_SUCCESS) {
2099                 DEBUG2(printk("scsi(%ld): Configure local loop error exit: "
2100                     "rval=%x\n", ha->host_no, rval));
2101         }
2102
2103         if (found_devs) {
2104                 ha->device_flags |= DFLG_LOCAL_DEVICES;
2105                 ha->device_flags &= ~DFLG_RETRY_LOCAL_DEVICES;
2106         }
2107
2108         return (rval);
2109 }
2110
2111 static void
2112 qla2x00_probe_for_all_luns(scsi_qla_host_t *ha)
2113 {
2114         fc_port_t       *fcport;
2115
2116         qla2x00_mark_all_devices_lost(ha, 0);
2117         list_for_each_entry(fcport, &ha->fcports, list) {
2118                 if (fcport->port_type != FCT_TARGET)
2119                         continue;
2120
2121                 qla2x00_update_fcport(ha, fcport);
2122         }
2123 }
2124
2125 static void
2126 qla2x00_iidma_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
2127 {
2128 #define LS_UNKNOWN      2
2129         static char *link_speeds[5] = { "1", "2", "?", "4" };
2130         int rval;
2131         uint16_t port_speed, mb[6];
2132
2133         if (!IS_QLA24XX(ha))
2134                 return;
2135
2136         switch (be16_to_cpu(fcport->fp_speed)) {
2137         case BIT_15:
2138                 port_speed = PORT_SPEED_1GB;
2139                 break;
2140         case BIT_14:
2141                 port_speed = PORT_SPEED_2GB;
2142                 break;
2143         case BIT_13:
2144                 port_speed = PORT_SPEED_4GB;
2145                 break;
2146         default:
2147                 DEBUG2(printk("scsi(%ld): %02x%02x%02x%02x%02x%02x%02x%02x -- "
2148                     "unsupported FM port operating speed (%04x).\n",
2149                     ha->host_no, fcport->port_name[0], fcport->port_name[1],
2150                     fcport->port_name[2], fcport->port_name[3],
2151                     fcport->port_name[4], fcport->port_name[5],
2152                     fcport->port_name[6], fcport->port_name[7],
2153                     be16_to_cpu(fcport->fp_speed)));
2154                 port_speed = PORT_SPEED_UNKNOWN;
2155                 break;
2156         }
2157         if (port_speed == PORT_SPEED_UNKNOWN)
2158                 return;
2159
2160         rval = qla2x00_set_idma_speed(ha, fcport->loop_id, port_speed, mb);
2161         if (rval != QLA_SUCCESS) {
2162                 DEBUG2(printk("scsi(%ld): Unable to adjust iIDMA "
2163                     "%02x%02x%02x%02x%02x%02x%02x%02x -- %04x %x %04x %04x.\n",
2164                     ha->host_no, fcport->port_name[0], fcport->port_name[1],
2165                     fcport->port_name[2], fcport->port_name[3],
2166                     fcport->port_name[4], fcport->port_name[5],
2167                     fcport->port_name[6], fcport->port_name[7], rval,
2168                     port_speed, mb[0], mb[1]));
2169         } else {
2170                 DEBUG2(qla_printk(KERN_INFO, ha,
2171                     "iIDMA adjusted to %s GB/s on "
2172                     "%02x%02x%02x%02x%02x%02x%02x%02x.\n",
2173                     link_speeds[port_speed], fcport->port_name[0],
2174                     fcport->port_name[1], fcport->port_name[2],
2175                     fcport->port_name[3], fcport->port_name[4],
2176                     fcport->port_name[5], fcport->port_name[6],
2177                     fcport->port_name[7]));
2178         }
2179 }
2180
2181 /*
2182  * qla2x00_update_fcport
2183  *      Updates device on list.
2184  *
2185  * Input:
2186  *      ha = adapter block pointer.
2187  *      fcport = port structure pointer.
2188  *
2189  * Return:
2190  *      0  - Success
2191  *  BIT_0 - error
2192  *
2193  * Context:
2194  *      Kernel context.
2195  */
2196 void
2197 qla2x00_update_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
2198 {
2199         fcport->ha = ha;
2200         fcport->login_retry = 0;
2201         fcport->port_login_retry_count = ha->port_down_retry_count *
2202             PORT_RETRY_TIME;
2203         atomic_set(&fcport->port_down_timer, ha->port_down_retry_count *
2204             PORT_RETRY_TIME);
2205         fcport->flags &= ~FCF_LOGIN_NEEDED;
2206
2207         qla2x00_iidma_fcport(ha, fcport);
2208
2209         atomic_set(&fcport->state, FCS_ONLINE);
2210
2211         if (ha->flags.init_done)
2212                 qla2x00_reg_remote_port(ha, fcport);
2213 }
2214
2215 void
2216 qla2x00_reg_remote_port(scsi_qla_host_t *ha, fc_port_t *fcport)
2217 {
2218         struct fc_rport_identifiers rport_ids;
2219         struct fc_rport *rport;
2220         unsigned long flags;
2221
2222         if (fcport->drport)
2223                 qla2x00_rport_del(fcport);
2224         if (fcport->rport)
2225                 return;
2226
2227         rport_ids.node_name = wwn_to_u64(fcport->node_name);
2228         rport_ids.port_name = wwn_to_u64(fcport->port_name);
2229         rport_ids.port_id = fcport->d_id.b.domain << 16 |
2230             fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
2231         rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
2232         rport = fc_remote_port_add(ha->host, 0, &rport_ids);
2233         if (!rport) {
2234                 qla_printk(KERN_WARNING, ha,
2235                     "Unable to allocate fc remote port!\n");
2236                 return;
2237         }
2238         spin_lock_irqsave(&fcport->rport_lock, flags);
2239         fcport->rport = rport;
2240         *((fc_port_t **)rport->dd_data) = fcport;
2241         spin_unlock_irqrestore(&fcport->rport_lock, flags);
2242
2243         rport->supported_classes = fcport->supported_classes;
2244
2245         rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
2246         if (fcport->port_type == FCT_INITIATOR)
2247                 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2248         if (fcport->port_type == FCT_TARGET)
2249                 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
2250         fc_remote_port_rolechg(rport, rport_ids.roles);
2251
2252         if (rport->scsi_target_id != -1 &&
2253             rport->scsi_target_id < ha->host->max_id)
2254                 fcport->os_target_id = rport->scsi_target_id;
2255 }
2256
2257 /*
2258  * qla2x00_configure_fabric
2259  *      Setup SNS devices with loop ID's.
2260  *
2261  * Input:
2262  *      ha = adapter block pointer.
2263  *
2264  * Returns:
2265  *      0 = success.
2266  *      BIT_0 = error
2267  */
2268 static int
2269 qla2x00_configure_fabric(scsi_qla_host_t *ha)
2270 {
2271         int     rval, rval2;
2272         fc_port_t       *fcport, *fcptemp;
2273         uint16_t        next_loopid;
2274         uint16_t        mb[MAILBOX_REGISTER_COUNT];
2275         uint16_t        loop_id;
2276         LIST_HEAD(new_fcports);
2277
2278         /* If FL port exists, then SNS is present */
2279         if (IS_QLA24XX(ha) || IS_QLA54XX(ha))
2280                 loop_id = NPH_F_PORT;
2281         else
2282                 loop_id = SNS_FL_PORT;
2283         rval = qla2x00_get_port_name(ha, loop_id, ha->fabric_node_name, 1);
2284         if (rval != QLA_SUCCESS) {
2285                 DEBUG2(printk("scsi(%ld): MBC_GET_PORT_NAME Failed, No FL "
2286                     "Port\n", ha->host_no));
2287
2288                 ha->device_flags &= ~SWITCH_FOUND;
2289                 return (QLA_SUCCESS);
2290         }
2291         ha->device_flags |= SWITCH_FOUND;
2292
2293         /* Mark devices that need re-synchronization. */
2294         rval2 = qla2x00_device_resync(ha);
2295         if (rval2 == QLA_RSCNS_HANDLED) {
2296                 /* No point doing the scan, just continue. */
2297                 return (QLA_SUCCESS);
2298         }
2299         do {
2300                 /* FDMI support. */
2301                 if (ql2xfdmienable &&
2302                     test_and_clear_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags))
2303                         qla2x00_fdmi_register(ha);
2304
2305                 /* Ensure we are logged into the SNS. */
2306                 if (IS_QLA24XX(ha) || IS_QLA54XX(ha))
2307                         loop_id = NPH_SNS;
2308                 else
2309                         loop_id = SIMPLE_NAME_SERVER;
2310                 ha->isp_ops.fabric_login(ha, loop_id, 0xff, 0xff,
2311                     0xfc, mb, BIT_1 | BIT_0);
2312                 if (mb[0] != MBS_COMMAND_COMPLETE) {
2313                         DEBUG2(qla_printk(KERN_INFO, ha,
2314                             "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
2315                             "mb[2]=%x mb[6]=%x mb[7]=%x\n", loop_id,
2316                             mb[0], mb[1], mb[2], mb[6], mb[7]));
2317                         return (QLA_SUCCESS);
2318                 }
2319
2320                 if (test_and_clear_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags)) {
2321                         if (qla2x00_rft_id(ha)) {
2322                                 /* EMPTY */
2323                                 DEBUG2(printk("scsi(%ld): Register FC-4 "
2324                                     "TYPE failed.\n", ha->host_no));
2325                         }
2326                         if (qla2x00_rff_id(ha)) {
2327                                 /* EMPTY */
2328                                 DEBUG2(printk("scsi(%ld): Register FC-4 "
2329                                     "Features failed.\n", ha->host_no));
2330                         }
2331                         if (qla2x00_rnn_id(ha)) {
2332                                 /* EMPTY */
2333                                 DEBUG2(printk("scsi(%ld): Register Node Name "
2334                                     "failed.\n", ha->host_no));
2335                         } else if (qla2x00_rsnn_nn(ha)) {
2336                                 /* EMPTY */
2337                                 DEBUG2(printk("scsi(%ld): Register Symbolic "
2338                                     "Node Name failed.\n", ha->host_no));
2339                         }
2340                 }
2341
2342                 rval = qla2x00_find_all_fabric_devs(ha, &new_fcports);
2343                 if (rval != QLA_SUCCESS)
2344                         break;
2345
2346                 /*
2347                  * Logout all previous fabric devices marked lost, except
2348                  * tape devices.
2349                  */
2350                 list_for_each_entry(fcport, &ha->fcports, list) {
2351                         if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2352                                 break;
2353
2354                         if ((fcport->flags & FCF_FABRIC_DEVICE) == 0)
2355                                 continue;
2356
2357                         if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
2358                                 qla2x00_mark_device_lost(ha, fcport,
2359                                     ql2xplogiabsentdevice, 0);
2360                                 if (fcport->loop_id != FC_NO_LOOP_ID &&
2361                                     (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2362                                     fcport->port_type != FCT_INITIATOR &&
2363                                     fcport->port_type != FCT_BROADCAST) {
2364                                         ha->isp_ops.fabric_logout(ha,
2365                                             fcport->loop_id,
2366                                             fcport->d_id.b.domain,
2367                                             fcport->d_id.b.area,
2368                                             fcport->d_id.b.al_pa);
2369                                         fcport->loop_id = FC_NO_LOOP_ID;
2370                                 }
2371                         }
2372                 }
2373
2374                 /* Starting free loop ID. */
2375                 next_loopid = ha->min_external_loopid;
2376
2377                 /*
2378                  * Scan through our port list and login entries that need to be
2379                  * logged in.
2380                  */
2381                 list_for_each_entry(fcport, &ha->fcports, list) {
2382                         if (atomic_read(&ha->loop_down_timer) ||
2383                             test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2384                                 break;
2385
2386                         if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2387                             (fcport->flags & FCF_LOGIN_NEEDED) == 0)
2388                                 continue;
2389
2390                         if (fcport->loop_id == FC_NO_LOOP_ID) {
2391                                 fcport->loop_id = next_loopid;
2392                                 rval = qla2x00_find_new_loop_id(ha, fcport);
2393                                 if (rval != QLA_SUCCESS) {
2394                                         /* Ran out of IDs to use */
2395                                         break;
2396                                 }
2397                         }
2398                         /* Login and update database */
2399                         qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
2400                 }
2401
2402                 /* Exit if out of loop IDs. */
2403                 if (rval != QLA_SUCCESS) {
2404                         break;
2405                 }
2406
2407                 /*
2408                  * Login and add the new devices to our port list.
2409                  */
2410                 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2411                         if (atomic_read(&ha->loop_down_timer) ||
2412                             test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2413                                 break;
2414
2415                         /* Find a new loop ID to use. */
2416                         fcport->loop_id = next_loopid;
2417                         rval = qla2x00_find_new_loop_id(ha, fcport);
2418                         if (rval != QLA_SUCCESS) {
2419                                 /* Ran out of IDs to use */
2420                                 break;
2421                         }
2422
2423                         /* Remove device from the new list and add it to DB */
2424                         list_move_tail(&fcport->list, &ha->fcports);
2425
2426                         /* Login and update database */
2427                         qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
2428                 }
2429         } while (0);
2430
2431         /* Free all new device structures not processed. */
2432         list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2433                 list_del(&fcport->list);
2434                 kfree(fcport);
2435         }
2436
2437         if (rval) {
2438                 DEBUG2(printk("scsi(%ld): Configure fabric error exit: "
2439                     "rval=%d\n", ha->host_no, rval));
2440         }
2441
2442         return (rval);
2443 }
2444
2445
2446 /*
2447  * qla2x00_find_all_fabric_devs
2448  *
2449  * Input:
2450  *      ha = adapter block pointer.
2451  *      dev = database device entry pointer.
2452  *
2453  * Returns:
2454  *      0 = success.
2455  *
2456  * Context:
2457  *      Kernel context.
2458  */
2459 static int
2460 qla2x00_find_all_fabric_devs(scsi_qla_host_t *ha, struct list_head *new_fcports)
2461 {
2462         int             rval;
2463         uint16_t        loop_id;
2464         fc_port_t       *fcport, *new_fcport, *fcptemp;
2465         int             found;
2466
2467         sw_info_t       *swl;
2468         int             swl_idx;
2469         int             first_dev, last_dev;
2470         port_id_t       wrap, nxt_d_id;
2471
2472         rval = QLA_SUCCESS;
2473
2474         /* Try GID_PT to get device list, else GAN. */
2475         swl = kmalloc(sizeof(sw_info_t) * MAX_FIBRE_DEVICES, GFP_ATOMIC);
2476         if (swl == NULL) {
2477                 /*EMPTY*/
2478                 DEBUG2(printk("scsi(%ld): GID_PT allocations failed, fallback "
2479                     "on GA_NXT\n", ha->host_no));
2480         } else {
2481                 memset(swl, 0, sizeof(sw_info_t) * MAX_FIBRE_DEVICES);
2482                 if (qla2x00_gid_pt(ha, swl) != QLA_SUCCESS) {
2483                         kfree(swl);
2484                         swl = NULL;
2485                 } else if (qla2x00_gpn_id(ha, swl) != QLA_SUCCESS) {
2486                         kfree(swl);
2487                         swl = NULL;
2488                 } else if (qla2x00_gnn_id(ha, swl) != QLA_SUCCESS) {
2489                         kfree(swl);
2490                         swl = NULL;
2491                 } else if (qla2x00_gfpn_id(ha, swl) == QLA_SUCCESS) {
2492                         qla2x00_gpsc(ha, swl);
2493                 }
2494         }
2495         swl_idx = 0;
2496
2497         /* Allocate temporary fcport for any new fcports discovered. */
2498         new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2499         if (new_fcport == NULL) {
2500                 kfree(swl);
2501                 return (QLA_MEMORY_ALLOC_FAILED);
2502         }
2503         new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2504
2505         /* Set start port ID scan at adapter ID. */
2506         first_dev = 1;
2507         last_dev = 0;
2508
2509         /* Starting free loop ID. */
2510         loop_id = ha->min_external_loopid;
2511         for (; loop_id <= ha->last_loop_id; loop_id++) {
2512                 if (qla2x00_is_reserved_id(ha, loop_id))
2513                         continue;
2514
2515                 if (atomic_read(&ha->loop_down_timer) || LOOP_TRANSITION(ha))
2516                         break;
2517
2518                 if (swl != NULL) {
2519                         if (last_dev) {
2520                                 wrap.b24 = new_fcport->d_id.b24;
2521                         } else {
2522                                 new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
2523                                 memcpy(new_fcport->node_name,
2524                                     swl[swl_idx].node_name, WWN_SIZE);
2525                                 memcpy(new_fcport->port_name,
2526                                     swl[swl_idx].port_name, WWN_SIZE);
2527                                 memcpy(new_fcport->fabric_port_name,
2528                                     swl[swl_idx].fabric_port_name, WWN_SIZE);
2529                                 new_fcport->fp_speed = swl[swl_idx].fp_speed;
2530
2531                                 if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
2532                                         last_dev = 1;
2533                                 }
2534                                 swl_idx++;
2535                         }
2536                 } else {
2537                         /* Send GA_NXT to the switch */
2538                         rval = qla2x00_ga_nxt(ha, new_fcport);
2539                         if (rval != QLA_SUCCESS) {
2540                                 qla_printk(KERN_WARNING, ha,
2541                                     "SNS scan failed -- assuming zero-entry "
2542                                     "result...\n");
2543                                 list_for_each_entry_safe(fcport, fcptemp,
2544                                     new_fcports, list) {
2545                                         list_del(&fcport->list);
2546                                         kfree(fcport);
2547                                 }
2548                                 rval = QLA_SUCCESS;
2549                                 break;
2550                         }
2551                 }
2552
2553                 /* If wrap on switch device list, exit. */
2554                 if (first_dev) {
2555                         wrap.b24 = new_fcport->d_id.b24;
2556                         first_dev = 0;
2557                 } else if (new_fcport->d_id.b24 == wrap.b24) {
2558                         DEBUG2(printk("scsi(%ld): device wrap (%02x%02x%02x)\n",
2559                             ha->host_no, new_fcport->d_id.b.domain,
2560                             new_fcport->d_id.b.area, new_fcport->d_id.b.al_pa));
2561                         break;
2562                 }
2563
2564                 /* Bypass if host adapter. */
2565                 if (new_fcport->d_id.b24 == ha->d_id.b24)
2566                         continue;
2567
2568                 /* Bypass if same domain and area of adapter. */
2569                 if (((new_fcport->d_id.b24 & 0xffff00) ==
2570                     (ha->d_id.b24 & 0xffff00)) && ha->current_topology ==
2571                         ISP_CFG_FL)
2572                             continue;
2573
2574                 /* Bypass reserved domain fields. */
2575                 if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
2576                         continue;
2577
2578                 /* Locate matching device in database. */
2579                 found = 0;
2580                 list_for_each_entry(fcport, &ha->fcports, list) {
2581                         if (memcmp(new_fcport->port_name, fcport->port_name,
2582                             WWN_SIZE))
2583                                 continue;
2584
2585                         found++;
2586
2587                         /* Update port state. */
2588                         memcpy(fcport->fabric_port_name,
2589                             new_fcport->fabric_port_name, WWN_SIZE);
2590                         fcport->fp_speed = new_fcport->fp_speed;
2591
2592                         /*
2593                          * If address the same and state FCS_ONLINE, nothing
2594                          * changed.
2595                          */
2596                         if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
2597                             atomic_read(&fcport->state) == FCS_ONLINE) {
2598                                 break;
2599                         }
2600
2601                         /*
2602                          * If device was not a fabric device before.
2603                          */
2604                         if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
2605                                 fcport->d_id.b24 = new_fcport->d_id.b24;
2606                                 fcport->loop_id = FC_NO_LOOP_ID;
2607                                 fcport->flags |= (FCF_FABRIC_DEVICE |
2608                                     FCF_LOGIN_NEEDED);
2609                                 fcport->flags &= ~FCF_PERSISTENT_BOUND;
2610                                 break;
2611                         }
2612
2613                         /*
2614                          * Port ID changed or device was marked to be updated;
2615                          * Log it out if still logged in and mark it for
2616                          * relogin later.
2617                          */
2618                         fcport->d_id.b24 = new_fcport->d_id.b24;
2619                         fcport->flags |= FCF_LOGIN_NEEDED;
2620                         if (fcport->loop_id != FC_NO_LOOP_ID &&
2621                             (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2622                             fcport->port_type != FCT_INITIATOR &&
2623                             fcport->port_type != FCT_BROADCAST) {
2624                                 ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2625                                     fcport->d_id.b.domain, fcport->d_id.b.area,
2626                                     fcport->d_id.b.al_pa);
2627                                 fcport->loop_id = FC_NO_LOOP_ID;
2628                         }
2629
2630                         break;
2631                 }
2632
2633                 if (found)
2634                         continue;
2635
2636                 /* If device was not in our fcports list, then add it. */
2637                 list_add_tail(&new_fcport->list, new_fcports);
2638
2639                 /* Allocate a new replacement fcport. */
2640                 nxt_d_id.b24 = new_fcport->d_id.b24;
2641                 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2642                 if (new_fcport == NULL) {
2643                         kfree(swl);
2644                         return (QLA_MEMORY_ALLOC_FAILED);
2645                 }
2646                 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2647                 new_fcport->d_id.b24 = nxt_d_id.b24;
2648         }
2649
2650         kfree(swl);
2651         kfree(new_fcport);
2652
2653         if (!list_empty(new_fcports))
2654                 ha->device_flags |= DFLG_FABRIC_DEVICES;
2655
2656         return (rval);
2657 }
2658
2659 /*
2660  * qla2x00_find_new_loop_id
2661  *      Scan through our port list and find a new usable loop ID.
2662  *
2663  * Input:
2664  *      ha:     adapter state pointer.
2665  *      dev:    port structure pointer.
2666  *
2667  * Returns:
2668  *      qla2x00 local function return status code.
2669  *
2670  * Context:
2671  *      Kernel context.
2672  */
2673 static int
2674 qla2x00_find_new_loop_id(scsi_qla_host_t *ha, fc_port_t *dev)
2675 {
2676         int     rval;
2677         int     found;
2678         fc_port_t *fcport;
2679         uint16_t first_loop_id;
2680
2681         rval = QLA_SUCCESS;
2682
2683         /* Save starting loop ID. */
2684         first_loop_id = dev->loop_id;
2685
2686         for (;;) {
2687                 /* Skip loop ID if already used by adapter. */
2688                 if (dev->loop_id == ha->loop_id) {
2689                         dev->loop_id++;
2690                 }
2691
2692                 /* Skip reserved loop IDs. */
2693                 while (qla2x00_is_reserved_id(ha, dev->loop_id)) {
2694                         dev->loop_id++;
2695                 }
2696
2697                 /* Reset loop ID if passed the end. */
2698                 if (dev->loop_id > ha->last_loop_id) {
2699                         /* first loop ID. */
2700                         dev->loop_id = ha->min_external_loopid;
2701                 }
2702
2703                 /* Check for loop ID being already in use. */
2704                 found = 0;
2705                 fcport = NULL;
2706                 list_for_each_entry(fcport, &ha->fcports, list) {
2707                         if (fcport->loop_id == dev->loop_id && fcport != dev) {
2708                                 /* ID possibly in use */
2709                                 found++;
2710                                 break;
2711                         }
2712                 }
2713
2714                 /* If not in use then it is free to use. */
2715                 if (!found) {
2716                         break;
2717                 }
2718
2719                 /* ID in use. Try next value. */
2720                 dev->loop_id++;
2721
2722                 /* If wrap around. No free ID to use. */
2723                 if (dev->loop_id == first_loop_id) {
2724                         dev->loop_id = FC_NO_LOOP_ID;
2725                         rval = QLA_FUNCTION_FAILED;
2726                         break;
2727                 }
2728         }
2729
2730         return (rval);
2731 }
2732
2733 /*
2734  * qla2x00_device_resync
2735  *      Marks devices in the database that needs resynchronization.
2736  *
2737  * Input:
2738  *      ha = adapter block pointer.
2739  *
2740  * Context:
2741  *      Kernel context.
2742  */
2743 static int
2744 qla2x00_device_resync(scsi_qla_host_t *ha)
2745 {
2746         int     rval;
2747         uint32_t mask;
2748         fc_port_t *fcport;
2749         uint32_t rscn_entry;
2750         uint8_t rscn_out_iter;
2751         uint8_t format;
2752         port_id_t d_id;
2753
2754         rval = QLA_RSCNS_HANDLED;
2755
2756         while (ha->rscn_out_ptr != ha->rscn_in_ptr ||
2757             ha->flags.rscn_queue_overflow) {
2758
2759                 rscn_entry = ha->rscn_queue[ha->rscn_out_ptr];
2760                 format = MSB(MSW(rscn_entry));
2761                 d_id.b.domain = LSB(MSW(rscn_entry));
2762                 d_id.b.area = MSB(LSW(rscn_entry));
2763                 d_id.b.al_pa = LSB(LSW(rscn_entry));
2764
2765                 DEBUG(printk("scsi(%ld): RSCN queue entry[%d] = "
2766                     "[%02x/%02x%02x%02x].\n",
2767                     ha->host_no, ha->rscn_out_ptr, format, d_id.b.domain,
2768                     d_id.b.area, d_id.b.al_pa));
2769
2770                 ha->rscn_out_ptr++;
2771                 if (ha->rscn_out_ptr == MAX_RSCN_COUNT)
2772                         ha->rscn_out_ptr = 0;
2773
2774                 /* Skip duplicate entries. */
2775                 for (rscn_out_iter = ha->rscn_out_ptr;
2776                     !ha->flags.rscn_queue_overflow &&
2777                     rscn_out_iter != ha->rscn_in_ptr;
2778                     rscn_out_iter = (rscn_out_iter ==
2779                         (MAX_RSCN_COUNT - 1)) ? 0: rscn_out_iter + 1) {
2780
2781                         if (rscn_entry != ha->rscn_queue[rscn_out_iter])
2782                                 break;
2783
2784                         DEBUG(printk("scsi(%ld): Skipping duplicate RSCN queue "
2785                             "entry found at [%d].\n", ha->host_no,
2786                             rscn_out_iter));
2787
2788                         ha->rscn_out_ptr = rscn_out_iter;
2789                 }
2790
2791                 /* Queue overflow, set switch default case. */
2792                 if (ha->flags.rscn_queue_overflow) {
2793                         DEBUG(printk("scsi(%ld): device_resync: rscn "
2794                             "overflow.\n", ha->host_no));
2795
2796                         format = 3;
2797                         ha->flags.rscn_queue_overflow = 0;
2798                 }
2799
2800                 switch (format) {
2801                 case 0:
2802                         mask = 0xffffff;
2803                         break;
2804                 case 1:
2805                         mask = 0xffff00;
2806                         break;
2807                 case 2:
2808                         mask = 0xff0000;
2809                         break;
2810                 default:
2811                         mask = 0x0;
2812                         d_id.b24 = 0;
2813                         ha->rscn_out_ptr = ha->rscn_in_ptr;
2814                         break;
2815                 }
2816
2817                 rval = QLA_SUCCESS;
2818
2819                 list_for_each_entry(fcport, &ha->fcports, list) {
2820                         if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2821                             (fcport->d_id.b24 & mask) != d_id.b24 ||
2822                             fcport->port_type == FCT_BROADCAST)
2823                                 continue;
2824
2825                         if (atomic_read(&fcport->state) == FCS_ONLINE) {
2826                                 if (format != 3 ||
2827                                     fcport->port_type != FCT_INITIATOR) {
2828                                         qla2x00_mark_device_lost(ha, fcport,
2829                                             0, 0);
2830                                 }
2831                         }
2832                         fcport->flags &= ~FCF_FARP_DONE;
2833                 }
2834         }
2835         return (rval);
2836 }
2837
2838 /*
2839  * qla2x00_fabric_dev_login
2840  *      Login fabric target device and update FC port database.
2841  *
2842  * Input:
2843  *      ha:             adapter state pointer.
2844  *      fcport:         port structure list pointer.
2845  *      next_loopid:    contains value of a new loop ID that can be used
2846  *                      by the next login attempt.
2847  *
2848  * Returns:
2849  *      qla2x00 local function return status code.
2850  *
2851  * Context:
2852  *      Kernel context.
2853  */
2854 static int
2855 qla2x00_fabric_dev_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2856     uint16_t *next_loopid)
2857 {
2858         int     rval;
2859         int     retry;
2860         uint8_t opts;
2861
2862         rval = QLA_SUCCESS;
2863         retry = 0;
2864
2865         rval = qla2x00_fabric_login(ha, fcport, next_loopid);
2866         if (rval == QLA_SUCCESS) {
2867                 /* Send an ADISC to tape devices.*/
2868                 opts = 0;
2869                 if (fcport->flags & FCF_TAPE_PRESENT)
2870                         opts |= BIT_1;
2871                 rval = qla2x00_get_port_database(ha, fcport, opts);
2872                 if (rval != QLA_SUCCESS) {
2873                         ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2874                             fcport->d_id.b.domain, fcport->d_id.b.area,
2875                             fcport->d_id.b.al_pa);
2876                         qla2x00_mark_device_lost(ha, fcport, 1, 0);
2877                 } else {
2878                         qla2x00_update_fcport(ha, fcport);
2879                 }
2880         }
2881
2882         return (rval);
2883 }
2884
2885 /*
2886  * qla2x00_fabric_login
2887  *      Issue fabric login command.
2888  *
2889  * Input:
2890  *      ha = adapter block pointer.
2891  *      device = pointer to FC device type structure.
2892  *
2893  * Returns:
2894  *      0 - Login successfully
2895  *      1 - Login failed
2896  *      2 - Initiator device
2897  *      3 - Fatal error
2898  */
2899 int
2900 qla2x00_fabric_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2901     uint16_t *next_loopid)
2902 {
2903         int     rval;
2904         int     retry;
2905         uint16_t tmp_loopid;
2906         uint16_t mb[MAILBOX_REGISTER_COUNT];
2907
2908         retry = 0;
2909         tmp_loopid = 0;
2910
2911         for (;;) {
2912                 DEBUG(printk("scsi(%ld): Trying Fabric Login w/loop id 0x%04x "
2913                     "for port %02x%02x%02x.\n",
2914                     ha->host_no, fcport->loop_id, fcport->d_id.b.domain,
2915                     fcport->d_id.b.area, fcport->d_id.b.al_pa));
2916
2917                 /* Login fcport on switch. */
2918                 ha->isp_ops.fabric_login(ha, fcport->loop_id,
2919                     fcport->d_id.b.domain, fcport->d_id.b.area,
2920                     fcport->d_id.b.al_pa, mb, BIT_0);
2921                 if (mb[0] == MBS_PORT_ID_USED) {
2922                         /*
2923                          * Device has another loop ID.  The firmware team
2924                          * recommends the driver perform an implicit login with
2925                          * the specified ID again. The ID we just used is save
2926                          * here so we return with an ID that can be tried by
2927                          * the next login.
2928                          */
2929                         retry++;
2930                         tmp_loopid = fcport->loop_id;
2931                         fcport->loop_id = mb[1];
2932
2933                         DEBUG(printk("Fabric Login: port in use - next "
2934                             "loop id=0x%04x, port Id=%02x%02x%02x.\n",
2935                             fcport->loop_id, fcport->d_id.b.domain,
2936                             fcport->d_id.b.area, fcport->d_id.b.al_pa));
2937
2938                 } else if (mb[0] == MBS_COMMAND_COMPLETE) {
2939                         /*
2940                          * Login succeeded.
2941                          */
2942                         if (retry) {
2943                                 /* A retry occurred before. */
2944                                 *next_loopid = tmp_loopid;
2945                         } else {
2946                                 /*
2947                                  * No retry occurred before. Just increment the
2948                                  * ID value for next login.
2949                                  */
2950                                 *next_loopid = (fcport->loop_id + 1);
2951                         }
2952
2953                         if (mb[1] & BIT_0) {
2954                                 fcport->port_type = FCT_INITIATOR;
2955                         } else {
2956                                 fcport->port_type = FCT_TARGET;
2957                                 if (mb[1] & BIT_1) {
2958                                         fcport->flags |= FCF_TAPE_PRESENT;
2959                                 }
2960                         }
2961
2962                         if (mb[10] & BIT_0)
2963                                 fcport->supported_classes |= FC_COS_CLASS2;
2964                         if (mb[10] & BIT_1)
2965                                 fcport->supported_classes |= FC_COS_CLASS3;
2966
2967                         rval = QLA_SUCCESS;
2968                         break;
2969                 } else if (mb[0] == MBS_LOOP_ID_USED) {
2970                         /*
2971                          * Loop ID already used, try next loop ID.
2972                          */
2973                         fcport->loop_id++;
2974                         rval = qla2x00_find_new_loop_id(ha, fcport);
2975                         if (rval != QLA_SUCCESS) {
2976                                 /* Ran out of loop IDs to use */
2977                                 break;
2978                         }
2979                 } else if (mb[0] == MBS_COMMAND_ERROR) {
2980                         /*
2981                          * Firmware possibly timed out during login. If NO
2982                          * retries are left to do then the device is declared
2983                          * dead.
2984                          */
2985                         *next_loopid = fcport->loop_id;
2986                         ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2987                             fcport->d_id.b.domain, fcport->d_id.b.area,
2988                             fcport->d_id.b.al_pa);
2989                         qla2x00_mark_device_lost(ha, fcport, 1, 0);
2990
2991                         rval = 1;
2992                         break;
2993                 } else {
2994                         /*
2995                          * unrecoverable / not handled error
2996                          */
2997                         DEBUG2(printk("%s(%ld): failed=%x port_id=%02x%02x%02x "
2998                             "loop_id=%x jiffies=%lx.\n",
2999                             __func__, ha->host_no, mb[0],
3000                             fcport->d_id.b.domain, fcport->d_id.b.area,
3001                             fcport->d_id.b.al_pa, fcport->loop_id, jiffies));
3002
3003                         *next_loopid = fcport->loop_id;
3004                         ha->isp_ops.fabric_logout(ha, fcport->loop_id,
3005                             fcport->d_id.b.domain, fcport->d_id.b.area,
3006                             fcport->d_id.b.al_pa);
3007                         fcport->loop_id = FC_NO_LOOP_ID;
3008                         fcport->login_retry = 0;
3009
3010                         rval = 3;
3011                         break;
3012                 }
3013         }
3014
3015         return (rval);
3016 }
3017
3018 /*
3019  * qla2x00_local_device_login
3020  *      Issue local device login command.
3021  *
3022  * Input:
3023  *      ha = adapter block pointer.
3024  *      loop_id = loop id of device to login to.
3025  *
3026  * Returns (Where's the #define!!!!):
3027  *      0 - Login successfully
3028  *      1 - Login failed
3029  *      3 - Fatal error
3030  */
3031 int
3032 qla2x00_local_device_login(scsi_qla_host_t *ha, fc_port_t *fcport)
3033 {
3034         int             rval;
3035         uint16_t        mb[MAILBOX_REGISTER_COUNT];
3036
3037         memset(mb, 0, sizeof(mb));
3038         rval = qla2x00_login_local_device(ha, fcport, mb, BIT_0);
3039         if (rval == QLA_SUCCESS) {
3040                 /* Interrogate mailbox registers for any errors */
3041                 if (mb[0] == MBS_COMMAND_ERROR)
3042                         rval = 1;
3043                 else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
3044                         /* device not in PCB table */
3045                         rval = 3;
3046         }
3047
3048         return (rval);
3049 }
3050
3051 /*
3052  *  qla2x00_loop_resync
3053  *      Resync with fibre channel devices.
3054  *
3055  * Input:
3056  *      ha = adapter block pointer.
3057  *
3058  * Returns:
3059  *      0 = success
3060  */
3061 int
3062 qla2x00_loop_resync(scsi_qla_host_t *ha)
3063 {
3064         int   rval;
3065         uint32_t wait_time;
3066
3067         rval = QLA_SUCCESS;
3068
3069         atomic_set(&ha->loop_state, LOOP_UPDATE);
3070         clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3071         if (ha->flags.online) {
3072                 if (!(rval = qla2x00_fw_ready(ha))) {
3073                         /* Wait at most MAX_TARGET RSCNs for a stable link. */
3074                         wait_time = 256;
3075                         do {
3076                                 atomic_set(&ha->loop_state, LOOP_UPDATE);
3077
3078                                 /* Issue a marker after FW becomes ready. */
3079                                 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
3080                                 ha->marker_needed = 0;
3081
3082                                 /* Remap devices on Loop. */
3083                                 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
3084
3085                                 qla2x00_configure_loop(ha);
3086                                 wait_time--;
3087                         } while (!atomic_read(&ha->loop_down_timer) &&
3088                                 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
3089                                 wait_time &&
3090                                 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
3091                 }
3092         }
3093
3094         if (test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
3095                 return (QLA_FUNCTION_FAILED);
3096         }
3097
3098         if (rval) {
3099                 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
3100         }
3101
3102         return (rval);
3103 }
3104
3105 void
3106 qla2x00_rescan_fcports(scsi_qla_host_t *ha)
3107 {
3108         int rescan_done;
3109         fc_port_t *fcport;
3110
3111         rescan_done = 0;
3112         list_for_each_entry(fcport, &ha->fcports, list) {
3113                 if ((fcport->flags & FCF_RESCAN_NEEDED) == 0)
3114                         continue;
3115
3116                 qla2x00_update_fcport(ha, fcport);
3117                 fcport->flags &= ~FCF_RESCAN_NEEDED;
3118
3119                 rescan_done = 1;
3120         }
3121         qla2x00_probe_for_all_luns(ha);
3122 }
3123
3124 void
3125 qla2x00_update_fcports(scsi_qla_host_t *ha)
3126 {
3127         fc_port_t *fcport;
3128
3129         /* Go with deferred removal of rport references. */
3130         list_for_each_entry(fcport, &ha->fcports, list)
3131                 if (fcport->drport)
3132                         qla2x00_rport_del(fcport);
3133 }
3134
3135 /*
3136 *  qla2x00_abort_isp
3137 *      Resets ISP and aborts all outstanding commands.
3138 *
3139 * Input:
3140 *      ha           = adapter block pointer.
3141 *
3142 * Returns:
3143 *      0 = success
3144 */
3145 int
3146 qla2x00_abort_isp(scsi_qla_host_t *ha)
3147 {
3148         int rval;
3149         unsigned long flags = 0;
3150         uint16_t       cnt;
3151         srb_t          *sp;
3152         uint8_t        status = 0;
3153
3154         if (ha->flags.online) {
3155                 ha->flags.online = 0;
3156                 clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
3157
3158                 qla_printk(KERN_INFO, ha,
3159                     "Performing ISP error recovery - ha= %p.\n", ha);
3160                 ha->isp_ops.reset_chip(ha);
3161
3162                 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
3163                 if (atomic_read(&ha->loop_state) != LOOP_DOWN) {
3164                         atomic_set(&ha->loop_state, LOOP_DOWN);
3165                         qla2x00_mark_all_devices_lost(ha, 0);
3166                 } else {
3167                         if (!atomic_read(&ha->loop_down_timer))
3168                                 atomic_set(&ha->loop_down_timer,
3169                                     LOOP_DOWN_TIME);
3170                 }
3171
3172                 spin_lock_irqsave(&ha->hardware_lock, flags);
3173                 /* Requeue all commands in outstanding command list. */
3174                 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
3175                         sp = ha->outstanding_cmds[cnt];
3176                         if (sp) {
3177                                 ha->outstanding_cmds[cnt] = NULL;
3178                                 sp->flags = 0;
3179                                 sp->cmd->result = DID_RESET << 16;
3180                                 sp->cmd->host_scribble = (unsigned char *)NULL;
3181                                 qla2x00_sp_compl(ha, sp);
3182                         }
3183                 }
3184                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3185
3186                 ha->isp_ops.nvram_config(ha);
3187
3188                 if (!qla2x00_restart_isp(ha)) {
3189                         clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3190
3191                         if (!atomic_read(&ha->loop_down_timer)) {
3192                                 /*
3193                                  * Issue marker command only when we are going
3194                                  * to start the I/O .
3195                                  */
3196                                 ha->marker_needed = 1;
3197                         }
3198
3199                         ha->flags.online = 1;
3200
3201                         ha->isp_ops.enable_intrs(ha);
3202
3203                         ha->isp_abort_cnt = 0;
3204                         clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3205
3206                         if (ha->eft) {
3207                                 rval = qla2x00_trace_control(ha, TC_ENABLE,
3208                                     ha->eft_dma, EFT_NUM_BUFFERS);
3209                                 if (rval) {
3210                                         qla_printk(KERN_WARNING, ha,
3211                                             "Unable to reinitialize EFT "
3212                                             "(%d).\n", rval);
3213                                 }
3214                         }
3215                 } else {        /* failed the ISP abort */
3216                         ha->flags.online = 1;
3217                         if (test_bit(ISP_ABORT_RETRY, &ha->dpc_flags)) {
3218                                 if (ha->isp_abort_cnt == 0) {
3219                                         qla_printk(KERN_WARNING, ha,
3220                                             "ISP error recovery failed - "
3221                                             "board disabled\n");
3222                                         /*
3223                                          * The next call disables the board
3224                                          * completely.
3225                                          */
3226                                         ha->isp_ops.reset_adapter(ha);
3227                                         ha->flags.online = 0;
3228                                         clear_bit(ISP_ABORT_RETRY,
3229                                             &ha->dpc_flags);
3230                                         status = 0;
3231                                 } else { /* schedule another ISP abort */
3232                                         ha->isp_abort_cnt--;
3233                                         DEBUG(printk("qla%ld: ISP abort - "
3234                                             "retry remaining %d\n",
3235                                             ha->host_no, ha->isp_abort_cnt));
3236                                         status = 1;
3237                                 }
3238                         } else {
3239                                 ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
3240                                 DEBUG(printk("qla2x00(%ld): ISP error recovery "
3241                                     "- retrying (%d) more times\n",
3242                                     ha->host_no, ha->isp_abort_cnt));
3243                                 set_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3244                                 status = 1;
3245                         }
3246                 }
3247
3248         }
3249
3250         if (status) {
3251                 qla_printk(KERN_INFO, ha,
3252                         "qla2x00_abort_isp: **** FAILED ****\n");
3253         } else {
3254                 DEBUG(printk(KERN_INFO
3255                                 "qla2x00_abort_isp(%ld): exiting.\n",
3256                                 ha->host_no));
3257         }
3258
3259         return(status);
3260 }
3261
3262 /*
3263 *  qla2x00_restart_isp
3264 *      restarts the ISP after a reset
3265 *
3266 * Input:
3267 *      ha = adapter block pointer.
3268 *
3269 * Returns:
3270 *      0 = success
3271 */
3272 static int
3273 qla2x00_restart_isp(scsi_qla_host_t *ha)
3274 {
3275         uint8_t         status = 0;
3276         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
3277         unsigned long   flags = 0;
3278         uint32_t wait_time;
3279
3280         /* If firmware needs to be loaded */
3281         if (qla2x00_isp_firmware(ha)) {
3282                 ha->flags.online = 0;
3283                 if (!(status = ha->isp_ops.chip_diag(ha))) {
3284                         if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
3285                                 status = qla2x00_setup_chip(ha);
3286                                 goto done;
3287                         }
3288
3289                         spin_lock_irqsave(&ha->hardware_lock, flags);
3290
3291                         if (!IS_QLA24XX(ha) && !IS_QLA54XX(ha)) {
3292                                 /*
3293                                  * Disable SRAM, Instruction RAM and GP RAM
3294                                  * parity.
3295                                  */
3296                                 WRT_REG_WORD(&reg->hccr,
3297                                     (HCCR_ENABLE_PARITY + 0x0));
3298                                 RD_REG_WORD(&reg->hccr);
3299                         }
3300
3301                         spin_unlock_irqrestore(&ha->hardware_lock, flags);
3302
3303                         status = qla2x00_setup_chip(ha);
3304
3305                         spin_lock_irqsave(&ha->hardware_lock, flags);
3306
3307                         if (!IS_QLA24XX(ha) && !IS_QLA54XX(ha)) {
3308                                 /* Enable proper parity */
3309                                 if (IS_QLA2300(ha))
3310                                         /* SRAM parity */
3311                                         WRT_REG_WORD(&reg->hccr,
3312                                             (HCCR_ENABLE_PARITY + 0x1));
3313                                 else
3314                                         /*
3315                                          * SRAM, Instruction RAM and GP RAM
3316                                          * parity.
3317                                          */
3318                                         WRT_REG_WORD(&reg->hccr,
3319                                             (HCCR_ENABLE_PARITY + 0x7));
3320                                 RD_REG_WORD(&reg->hccr);
3321                         }
3322
3323                         spin_unlock_irqrestore(&ha->hardware_lock, flags);
3324                 }
3325         }
3326
3327  done:
3328         if (!status && !(status = qla2x00_init_rings(ha))) {
3329                 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3330                 if (!(status = qla2x00_fw_ready(ha))) {
3331                         DEBUG(printk("%s(): Start configure loop, "
3332                             "status = %d\n", __func__, status));
3333
3334                         /* Issue a marker after FW becomes ready. */
3335                         qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
3336
3337                         ha->flags.online = 1;
3338                         /* Wait at most MAX_TARGET RSCNs for a stable link. */
3339                         wait_time = 256;
3340                         do {
3341                                 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
3342                                 qla2x00_configure_loop(ha);
3343                                 wait_time--;
3344                         } while (!atomic_read(&ha->loop_down_timer) &&
3345                                 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
3346                                 wait_time &&
3347                                 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
3348                 }
3349
3350                 /* if no cable then assume it's good */
3351                 if ((ha->device_flags & DFLG_NO_CABLE))
3352                         status = 0;
3353
3354                 DEBUG(printk("%s(): Configure loop done, status = 0x%x\n",
3355                                 __func__,
3356                                 status));
3357         }
3358         return (status);
3359 }
3360
3361 /*
3362 * qla2x00_reset_adapter
3363 *      Reset adapter.
3364 *
3365 * Input:
3366 *      ha = adapter block pointer.
3367 */
3368 void
3369 qla2x00_reset_adapter(scsi_qla_host_t *ha)
3370 {
3371         unsigned long flags = 0;
3372         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
3373
3374         ha->flags.online = 0;
3375         ha->isp_ops.disable_intrs(ha);
3376
3377         spin_lock_irqsave(&ha->hardware_lock, flags);
3378         WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
3379         RD_REG_WORD(&reg->hccr);                        /* PCI Posting. */
3380         WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
3381         RD_REG_WORD(&reg->hccr);                        /* PCI Posting. */
3382         spin_unlock_irqrestore(&ha->hardware_lock, flags);
3383 }
3384
3385 void
3386 qla24xx_reset_adapter(scsi_qla_host_t *ha)
3387 {
3388         unsigned long flags = 0;
3389         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
3390
3391         ha->flags.online = 0;
3392         ha->isp_ops.disable_intrs(ha);
3393
3394         spin_lock_irqsave(&ha->hardware_lock, flags);
3395         WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
3396         RD_REG_DWORD(&reg->hccr);
3397         WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
3398         RD_REG_DWORD(&reg->hccr);
3399         spin_unlock_irqrestore(&ha->hardware_lock, flags);
3400 }
3401
3402 int
3403 qla24xx_nvram_config(scsi_qla_host_t *ha)
3404 {
3405         int   rval;
3406         struct init_cb_24xx *icb;
3407         struct nvram_24xx *nv;
3408         uint32_t *dptr;
3409         uint8_t  *dptr1, *dptr2;
3410         uint32_t chksum;
3411         uint16_t cnt;
3412
3413         rval = QLA_SUCCESS;
3414         icb = (struct init_cb_24xx *)ha->init_cb;
3415         nv = (struct nvram_24xx *)ha->request_ring;
3416
3417         /* Determine NVRAM starting address. */
3418         ha->nvram_size = sizeof(struct nvram_24xx);
3419         ha->nvram_base = FA_NVRAM_FUNC0_ADDR;
3420         ha->vpd_size = FA_NVRAM_VPD_SIZE;
3421         ha->vpd_base = FA_NVRAM_VPD0_ADDR;
3422         if (PCI_FUNC(ha->pdev->devfn)) {
3423                 ha->nvram_base = FA_NVRAM_FUNC1_ADDR;
3424                 ha->vpd_base = FA_NVRAM_VPD1_ADDR;
3425         }
3426
3427         /* Get NVRAM data and calculate checksum. */
3428         dptr = (uint32_t *)nv;
3429         ha->isp_ops.read_nvram(ha, (uint8_t *)dptr, ha->nvram_base,
3430             ha->nvram_size);
3431         for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++)
3432                 chksum += le32_to_cpu(*dptr++);
3433
3434         DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
3435         DEBUG5(qla2x00_dump_buffer((uint8_t *)ha->request_ring,
3436             ha->nvram_size));
3437
3438         /* Bad NVRAM data, set defaults parameters. */
3439         if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
3440             || nv->id[3] != ' ' ||
3441             nv->nvram_version < __constant_cpu_to_le16(ICB_VERSION)) {
3442                 /* Reset NVRAM data. */
3443                 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
3444                     "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
3445                     le16_to_cpu(nv->nvram_version));
3446                 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
3447                     "invalid -- WWPN) defaults.\n");
3448
3449                 /*
3450                  * Set default initialization control block.
3451                  */
3452                 memset(nv, 0, ha->nvram_size);
3453                 nv->nvram_version = __constant_cpu_to_le16(ICB_VERSION);
3454                 nv->version = __constant_cpu_to_le16(ICB_VERSION);
3455                 nv->frame_payload_size = __constant_cpu_to_le16(2048);
3456                 nv->execution_throttle = __constant_cpu_to_le16(0xFFFF);
3457                 nv->exchange_count = __constant_cpu_to_le16(0);
3458                 nv->hard_address = __constant_cpu_to_le16(124);
3459                 nv->port_name[0] = 0x21;
3460                 nv->port_name[1] = 0x00 + PCI_FUNC(ha->pdev->devfn);
3461                 nv->port_name[2] = 0x00;
3462                 nv->port_name[3] = 0xe0;
3463                 nv->port_name[4] = 0x8b;
3464                 nv->port_name[5] = 0x1c;
3465                 nv->port_name[6] = 0x55;
3466                 nv->port_name[7] = 0x86;
3467                 nv->node_name[0] = 0x20;
3468                 nv->node_name[1] = 0x00;
3469                 nv->node_name[2] = 0x00;
3470                 nv->node_name[3] = 0xe0;
3471                 nv->node_name[4] = 0x8b;
3472                 nv->node_name[5] = 0x1c;
3473                 nv->node_name[6] = 0x55;
3474                 nv->node_name[7] = 0x86;
3475                 nv->login_retry_count = __constant_cpu_to_le16(8);
3476                 nv->interrupt_delay_timer = __constant_cpu_to_le16(0);
3477                 nv->login_timeout = __constant_cpu_to_le16(0);
3478                 nv->firmware_options_1 =
3479                     __constant_cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
3480                 nv->firmware_options_2 = __constant_cpu_to_le32(2 << 4);
3481                 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_12);
3482                 nv->firmware_options_3 = __constant_cpu_to_le32(2 << 13);
3483                 nv->host_p = __constant_cpu_to_le32(BIT_11|BIT_10);
3484                 nv->efi_parameters = __constant_cpu_to_le32(0);
3485                 nv->reset_delay = 5;
3486                 nv->max_luns_per_target = __constant_cpu_to_le16(128);
3487                 nv->port_down_retry_count = __constant_cpu_to_le16(30);
3488                 nv->link_down_timeout = __constant_cpu_to_le16(30);
3489
3490                 rval = 1;
3491         }
3492
3493         /* Reset Initialization control block */
3494         memset(icb, 0, sizeof(struct init_cb_24xx));
3495
3496         /* Copy 1st segment. */
3497         dptr1 = (uint8_t *)icb;
3498         dptr2 = (uint8_t *)&nv->version;
3499         cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
3500         while (cnt--)
3501                 *dptr1++ = *dptr2++;
3502
3503         icb->login_retry_count = nv->login_retry_count;
3504         icb->link_down_on_nos = nv->link_down_on_nos;
3505
3506         /* Copy 2nd segment. */
3507         dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
3508         dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
3509         cnt = (uint8_t *)&icb->reserved_3 -
3510             (uint8_t *)&icb->interrupt_delay_timer;
3511         while (cnt--)
3512                 *dptr1++ = *dptr2++;
3513
3514         /*
3515          * Setup driver NVRAM options.
3516          */
3517         if (memcmp(nv->model_name, BINZERO, sizeof(nv->model_name)) != 0) {
3518                 char *st, *en;
3519                 uint16_t index;
3520
3521                 strncpy(ha->model_number, nv->model_name,
3522                     sizeof(nv->model_name));
3523                 st = en = ha->model_number;
3524                 en += sizeof(nv->model_name) - 1;
3525                 while (en > st) {
3526                         if (*en != 0x20 && *en != 0x00)
3527                                 break;
3528                         *en-- = '\0';
3529                 }
3530
3531                 index = (ha->pdev->subsystem_device & 0xff);
3532                 if (index < QLA_MODEL_NAMES)
3533                         ha->model_desc = qla2x00_model_name[index * 2 + 1];
3534         } else
3535                 strcpy(ha->model_number, "QLA2462");
3536
3537         /* Use alternate WWN? */
3538         if (nv->host_p & __constant_cpu_to_le32(BIT_15)) {
3539                 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
3540                 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
3541         }
3542
3543         /* Prepare nodename */
3544         if ((icb->firmware_options_1 & __constant_cpu_to_le32(BIT_14)) == 0) {
3545                 /*
3546                  * Firmware will apply the following mask if the nodename was
3547                  * not provided.
3548                  */
3549                 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
3550                 icb->node_name[0] &= 0xF0;
3551         }
3552
3553         /* Set host adapter parameters. */
3554         ha->flags.disable_risc_code_load = 0;
3555         ha->flags.enable_lip_reset = 1;
3556         ha->flags.enable_lip_full_login = 1;
3557         ha->flags.enable_target_reset = 1;
3558         ha->flags.enable_led_scheme = 0;
3559         ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
3560
3561         ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
3562             (BIT_6 | BIT_5 | BIT_4)) >> 4;
3563
3564         memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
3565             sizeof(ha->fw_seriallink_options24));
3566
3567         /* save HBA serial number */
3568         ha->serial0 = icb->port_name[5];
3569         ha->serial1 = icb->port_name[6];
3570         ha->serial2 = icb->port_name[7];
3571         ha->node_name = icb->node_name;
3572         ha->port_name = icb->port_name;
3573
3574         icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
3575
3576         ha->retry_count = le16_to_cpu(nv->login_retry_count);
3577
3578         /* Set minimum login_timeout to 4 seconds. */
3579         if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
3580                 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
3581         if (le16_to_cpu(nv->login_timeout) < 4)
3582                 nv->login_timeout = __constant_cpu_to_le16(4);
3583         ha->login_timeout = le16_to_cpu(nv->login_timeout);
3584         icb->login_timeout = cpu_to_le16(nv->login_timeout);
3585
3586         /* Set minimum RATOV to 200 tenths of a second. */
3587         ha->r_a_tov = 200;
3588
3589         ha->loop_reset_delay = nv->reset_delay;
3590
3591         /* Link Down Timeout = 0:
3592          *
3593          *      When Port Down timer expires we will start returning
3594          *      I/O's to OS with "DID_NO_CONNECT".
3595          *
3596          * Link Down Timeout != 0:
3597          *
3598          *       The driver waits for the link to come up after link down
3599          *       before returning I/Os to OS with "DID_NO_CONNECT".
3600          */
3601         if (le16_to_cpu(nv->link_down_timeout) == 0) {
3602                 ha->loop_down_abort_time =
3603                     (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
3604         } else {
3605                 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
3606                 ha->loop_down_abort_time =
3607                     (LOOP_DOWN_TIME - ha->link_down_timeout);
3608         }
3609
3610         /* Need enough time to try and get the port back. */
3611         ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
3612         if (qlport_down_retry)
3613                 ha->port_down_retry_count = qlport_down_retry;
3614
3615         /* Set login_retry_count */
3616         ha->login_retry_count  = le16_to_cpu(nv->login_retry_count);
3617         if (ha->port_down_retry_count ==
3618             le16_to_cpu(nv->port_down_retry_count) &&
3619             ha->port_down_retry_count > 3)
3620                 ha->login_retry_count = ha->port_down_retry_count;
3621         else if (ha->port_down_retry_count > (int)ha->login_retry_count)
3622                 ha->login_retry_count = ha->port_down_retry_count;
3623         if (ql2xloginretrycount)
3624                 ha->login_retry_count = ql2xloginretrycount;
3625
3626         /* Enable ZIO. */
3627         if (!ha->flags.init_done) {
3628                 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
3629                     (BIT_3 | BIT_2 | BIT_1 | BIT_0);
3630                 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
3631                     le16_to_cpu(icb->interrupt_delay_timer): 2;
3632         }
3633         icb->firmware_options_2 &= __constant_cpu_to_le32(
3634             ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
3635         ha->flags.process_response_queue = 0;
3636         if (ha->zio_mode != QLA_ZIO_DISABLED) {
3637                 ha->zio_mode = QLA_ZIO_MODE_6;
3638
3639                 DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer delay "
3640                     "(%d us).\n", ha->host_no, ha->zio_mode,
3641                     ha->zio_timer * 100));
3642                 qla_printk(KERN_INFO, ha,
3643                     "ZIO mode %d enabled; timer delay (%d us).\n",
3644                     ha->zio_mode, ha->zio_timer * 100);
3645
3646                 icb->firmware_options_2 |= cpu_to_le32(
3647                     (uint32_t)ha->zio_mode);
3648                 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
3649                 ha->flags.process_response_queue = 1;
3650         }
3651
3652         if (rval) {
3653                 DEBUG2_3(printk(KERN_WARNING
3654                     "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
3655         }
3656         return (rval);
3657 }
3658
3659 static int
3660 qla24xx_load_risc_flash(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3661 {
3662         int     rval;
3663         int     segments, fragment;
3664         uint32_t faddr;
3665         uint32_t *dcode, dlen;
3666         uint32_t risc_addr;
3667         uint32_t risc_size;
3668         uint32_t i;
3669
3670         rval = QLA_SUCCESS;
3671
3672         segments = FA_RISC_CODE_SEGMENTS;
3673         faddr = FA_RISC_CODE_ADDR;
3674         dcode = (uint32_t *)ha->request_ring;
3675         *srisc_addr = 0;
3676
3677         /* Validate firmware image by checking version. */
3678         qla24xx_read_flash_data(ha, dcode, faddr + 4, 4);
3679         for (i = 0; i < 4; i++)
3680                 dcode[i] = be32_to_cpu(dcode[i]);
3681         if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
3682             dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
3683             (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
3684                 dcode[3] == 0)) {
3685                 qla_printk(KERN_WARNING, ha,
3686                     "Unable to verify integrity of flash firmware image!\n");
3687                 qla_printk(KERN_WARNING, ha,
3688                     "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
3689                     dcode[1], dcode[2], dcode[3]);
3690
3691                 return QLA_FUNCTION_FAILED;
3692         }
3693
3694         while (segments && rval == QLA_SUCCESS) {
3695                 /* Read segment's load information. */
3696                 qla24xx_read_flash_data(ha, dcode, faddr, 4);
3697
3698                 risc_addr = be32_to_cpu(dcode[2]);
3699                 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
3700                 risc_size = be32_to_cpu(dcode[3]);
3701
3702                 fragment = 0;
3703                 while (risc_size > 0 && rval == QLA_SUCCESS) {
3704                         dlen = (uint32_t)(ha->fw_transfer_size >> 2);
3705                         if (dlen > risc_size)
3706                                 dlen = risc_size;
3707
3708                         DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3709                             "addr %x, number of dwords 0x%x, offset 0x%x.\n",
3710                             ha->host_no, risc_addr, dlen, faddr));
3711
3712                         qla24xx_read_flash_data(ha, dcode, faddr, dlen);
3713                         for (i = 0; i < dlen; i++)
3714                                 dcode[i] = swab32(dcode[i]);
3715
3716                         rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3717                             dlen);
3718                         if (rval) {
3719                                 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3720                                     "segment %d of firmware\n", ha->host_no,
3721                                     fragment));
3722                                 qla_printk(KERN_WARNING, ha,
3723                                     "[ERROR] Failed to load segment %d of "
3724                                     "firmware\n", fragment);
3725                                 break;
3726                         }
3727
3728                         faddr += dlen;
3729                         risc_addr += dlen;
3730                         risc_size -= dlen;
3731                         fragment++;
3732                 }
3733
3734                 /* Next segment. */
3735                 segments--;
3736         }
3737
3738         return rval;
3739 }
3740
3741 #define QLA_FW_URL "ftp://ftp.qlogic.com/outgoing/linux/firmware/"
3742
3743 int
3744 qla2x00_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3745 {
3746         int     rval;
3747         int     i, fragment;
3748         uint16_t *wcode, *fwcode;
3749         uint32_t risc_addr, risc_size, fwclen, wlen, *seg;
3750         struct fw_blob *blob;
3751
3752         /* Load firmware blob. */
3753         blob = qla2x00_request_firmware(ha);
3754         if (!blob) {
3755                 qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
3756                 qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
3757                     "from: " QLA_FW_URL ".\n");
3758                 return QLA_FUNCTION_FAILED;
3759         }
3760
3761         rval = QLA_SUCCESS;
3762
3763         wcode = (uint16_t *)ha->request_ring;
3764         *srisc_addr = 0;
3765         fwcode = (uint16_t *)blob->fw->data;
3766         fwclen = 0;
3767
3768         /* Validate firmware image by checking version. */
3769         if (blob->fw->size < 8 * sizeof(uint16_t)) {
3770                 qla_printk(KERN_WARNING, ha,
3771                     "Unable to verify integrity of firmware image (%Zd)!\n",
3772                     blob->fw->size);
3773                 goto fail_fw_integrity;
3774         }
3775         for (i = 0; i < 4; i++)
3776                 wcode[i] = be16_to_cpu(fwcode[i + 4]);
3777         if ((wcode[0] == 0xffff && wcode[1] == 0xffff && wcode[2] == 0xffff &&
3778             wcode[3] == 0xffff) || (wcode[0] == 0 && wcode[1] == 0 &&
3779                 wcode[2] == 0 && wcode[3] == 0)) {
3780                 qla_printk(KERN_WARNING, ha,
3781                     "Unable to verify integrity of firmware image!\n");
3782                 qla_printk(KERN_WARNING, ha,
3783                     "Firmware data: %04x %04x %04x %04x!\n", wcode[0],
3784                     wcode[1], wcode[2], wcode[3]);
3785                 goto fail_fw_integrity;
3786         }
3787
3788         seg = blob->segs;
3789         while (*seg && rval == QLA_SUCCESS) {
3790                 risc_addr = *seg;
3791                 *srisc_addr = *srisc_addr == 0 ? *seg : *srisc_addr;
3792                 risc_size = be16_to_cpu(fwcode[3]);
3793
3794                 /* Validate firmware image size. */
3795                 fwclen += risc_size * sizeof(uint16_t);
3796                 if (blob->fw->size < fwclen) {
3797                         qla_printk(KERN_WARNING, ha,
3798                             "Unable to verify integrity of firmware image "
3799                             "(%Zd)!\n", blob->fw->size);
3800                         goto fail_fw_integrity;
3801                 }
3802
3803                 fragment = 0;
3804                 while (risc_size > 0 && rval == QLA_SUCCESS) {
3805                         wlen = (uint16_t)(ha->fw_transfer_size >> 1);
3806                         if (wlen > risc_size)
3807                                 wlen = risc_size;
3808
3809                         DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3810                             "addr %x, number of words 0x%x.\n", ha->host_no,
3811                             risc_addr, wlen));
3812
3813                         for (i = 0; i < wlen; i++)
3814                                 wcode[i] = swab16(fwcode[i]);
3815
3816                         rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3817                             wlen);
3818                         if (rval) {
3819                                 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3820                                     "segment %d of firmware\n", ha->host_no,
3821                                     fragment));
3822                                 qla_printk(KERN_WARNING, ha,
3823                                     "[ERROR] Failed to load segment %d of "
3824                                     "firmware\n", fragment);
3825                                 break;
3826                         }
3827
3828                         fwcode += wlen;
3829                         risc_addr += wlen;
3830                         risc_size -= wlen;
3831                         fragment++;
3832                 }
3833
3834                 /* Next segment. */
3835                 seg++;
3836         }
3837         return rval;
3838
3839 fail_fw_integrity:
3840         return QLA_FUNCTION_FAILED;
3841 }
3842
3843 int
3844 qla24xx_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3845 {
3846         int     rval;
3847         int     segments, fragment;
3848         uint32_t *dcode, dlen;
3849         uint32_t risc_addr;
3850         uint32_t risc_size;
3851         uint32_t i;
3852         struct fw_blob *blob;
3853         uint32_t *fwcode, fwclen;
3854
3855         /* Load firmware blob. */
3856         blob = qla2x00_request_firmware(ha);
3857         if (!blob) {
3858                 qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
3859                 qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
3860                     "from: " QLA_FW_URL ".\n");
3861
3862                 /* Try to load RISC code from flash. */
3863                 qla_printk(KERN_ERR, ha, "Attempting to load (potentially "
3864                     "outdated) firmware from flash.\n");
3865                 return qla24xx_load_risc_flash(ha, srisc_addr);
3866         }
3867
3868         rval = QLA_SUCCESS;
3869
3870         segments = FA_RISC_CODE_SEGMENTS;
3871         dcode = (uint32_t *)ha->request_ring;
3872         *srisc_addr = 0;
3873         fwcode = (uint32_t *)blob->fw->data;
3874         fwclen = 0;
3875
3876         /* Validate firmware image by checking version. */
3877         if (blob->fw->size < 8 * sizeof(uint32_t)) {
3878                 qla_printk(KERN_WARNING, ha,
3879                     "Unable to verify integrity of firmware image (%Zd)!\n",
3880                     blob->fw->size);
3881                 goto fail_fw_integrity;
3882         }
3883         for (i = 0; i < 4; i++)
3884                 dcode[i] = be32_to_cpu(fwcode[i + 4]);
3885         if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
3886             dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
3887             (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
3888                 dcode[3] == 0)) {
3889                 qla_printk(KERN_WARNING, ha,
3890                     "Unable to verify integrity of firmware image!\n");
3891                 qla_printk(KERN_WARNING, ha,
3892                     "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
3893                     dcode[1], dcode[2], dcode[3]);
3894                 goto fail_fw_integrity;
3895         }
3896
3897         while (segments && rval == QLA_SUCCESS) {
3898                 risc_addr = be32_to_cpu(fwcode[2]);
3899                 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
3900                 risc_size = be32_to_cpu(fwcode[3]);
3901
3902                 /* Validate firmware image size. */
3903                 fwclen += risc_size * sizeof(uint32_t);
3904                 if (blob->fw->size < fwclen) {
3905                         qla_printk(KERN_WARNING, ha,
3906                             "Unable to verify integrity of firmware image "
3907                             "(%Zd)!\n", blob->fw->size);
3908
3909                         goto fail_fw_integrity;
3910                 }
3911
3912                 fragment = 0;
3913                 while (risc_size > 0 && rval == QLA_SUCCESS) {
3914                         dlen = (uint32_t)(ha->fw_transfer_size >> 2);
3915                         if (dlen > risc_size)
3916                                 dlen = risc_size;
3917
3918                         DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3919                             "addr %x, number of dwords 0x%x.\n", ha->host_no,
3920                             risc_addr, dlen));
3921
3922                         for (i = 0; i < dlen; i++)
3923                                 dcode[i] = swab32(fwcode[i]);
3924
3925                         rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3926                             dlen);
3927                         if (rval) {
3928                                 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3929                                     "segment %d of firmware\n", ha->host_no,
3930                                     fragment));
3931                                 qla_printk(KERN_WARNING, ha,
3932                                     "[ERROR] Failed to load segment %d of "
3933                                     "firmware\n", fragment);
3934                                 break;
3935                         }
3936
3937                         fwcode += dlen;
3938                         risc_addr += dlen;
3939                         risc_size -= dlen;
3940                         fragment++;
3941                 }
3942
3943                 /* Next segment. */
3944                 segments--;
3945         }
3946         return rval;
3947
3948 fail_fw_integrity:
3949         return QLA_FUNCTION_FAILED;
3950 }