bd95f7dc5cfce9a9279d71268952ad354f92b7f9
[linux-2.6.git] / 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 #ifdef CONFIG_SPARC
15 #include <asm/prom.h>
16 #endif
17
18 /* XXX(hch): this is ugly, but we don't want to pull in exioctl.h */
19 #ifndef EXT_IS_LUN_BIT_SET
20 #define EXT_IS_LUN_BIT_SET(P,L) \
21     (((P)->mask[L/8] & (0x80 >> (L%8)))?1:0)
22 #define EXT_SET_LUN_BIT(P,L) \
23     ((P)->mask[L/8] |= (0x80 >> (L%8)))
24 #endif
25
26 /*
27 *  QLogic ISP2x00 Hardware Support Function Prototypes.
28 */
29 static int qla2x00_isp_firmware(scsi_qla_host_t *);
30 static void qla2x00_resize_request_q(scsi_qla_host_t *);
31 static int qla2x00_setup_chip(scsi_qla_host_t *);
32 static void qla2x00_init_response_q_entries(scsi_qla_host_t *);
33 static int qla2x00_init_rings(scsi_qla_host_t *);
34 static int qla2x00_fw_ready(scsi_qla_host_t *);
35 static int qla2x00_configure_hba(scsi_qla_host_t *);
36 static int qla2x00_configure_loop(scsi_qla_host_t *);
37 static int qla2x00_configure_local_loop(scsi_qla_host_t *);
38 static int qla2x00_configure_fabric(scsi_qla_host_t *);
39 static int qla2x00_find_all_fabric_devs(scsi_qla_host_t *, struct list_head *);
40 static int qla2x00_device_resync(scsi_qla_host_t *);
41 static int qla2x00_fabric_dev_login(scsi_qla_host_t *, fc_port_t *,
42     uint16_t *);
43
44 static int qla2x00_restart_isp(scsi_qla_host_t *);
45
46 static int qla2x00_find_new_loop_id(scsi_qla_host_t *ha, fc_port_t *dev);
47
48 /****************************************************************************/
49 /*                QLogic ISP2x00 Hardware Support Functions.                */
50 /****************************************************************************/
51
52 /*
53 * qla2x00_initialize_adapter
54 *      Initialize board.
55 *
56 * Input:
57 *      ha = adapter block pointer.
58 *
59 * Returns:
60 *      0 = success
61 */
62 int
63 qla2x00_initialize_adapter(scsi_qla_host_t *ha)
64 {
65         int     rval;
66
67         /* Clear adapter flags. */
68         ha->flags.online = 0;
69         ha->flags.reset_active = 0;
70         atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
71         atomic_set(&ha->loop_state, LOOP_DOWN);
72         ha->device_flags = DFLG_NO_CABLE;
73         ha->dpc_flags = 0;
74         ha->flags.management_server_logged_in = 0;
75         ha->marker_needed = 0;
76         ha->mbx_flags = 0;
77         ha->isp_abort_cnt = 0;
78         ha->beacon_blink_led = 0;
79         set_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags);
80
81         qla_printk(KERN_INFO, ha, "Configuring PCI space...\n");
82         rval = ha->isp_ops.pci_config(ha);
83         if (rval) {
84                 DEBUG2(printk("scsi(%ld): Unable to configure PCI space.\n",
85                     ha->host_no));
86                 return (rval);
87         }
88
89         ha->isp_ops.reset_chip(ha);
90
91         ha->isp_ops.get_flash_version(ha, ha->request_ring);
92
93         qla_printk(KERN_INFO, ha, "Configure NVRAM parameters...\n");
94
95         ha->isp_ops.nvram_config(ha);
96
97         if (ha->flags.disable_serdes) {
98                 /* Mask HBA via NVRAM settings? */
99                 qla_printk(KERN_INFO, ha, "Masking HBA WWPN "
100                     "%02x%02x%02x%02x%02x%02x%02x%02x (via NVRAM).\n",
101                     ha->port_name[0], ha->port_name[1],
102                     ha->port_name[2], ha->port_name[3],
103                     ha->port_name[4], ha->port_name[5],
104                     ha->port_name[6], ha->port_name[7]);
105                 return QLA_FUNCTION_FAILED;
106         }
107
108         qla_printk(KERN_INFO, ha, "Verifying loaded RISC code...\n");
109
110         if (qla2x00_isp_firmware(ha) != QLA_SUCCESS) {
111                 rval = ha->isp_ops.chip_diag(ha);
112                 if (rval)
113                         return (rval);
114                 rval = qla2x00_setup_chip(ha);
115                 if (rval)
116                         return (rval);
117         }
118         rval = qla2x00_init_rings(ha);
119
120         return (rval);
121 }
122
123 /**
124  * qla2100_pci_config() - Setup ISP21xx PCI configuration registers.
125  * @ha: HA context
126  *
127  * Returns 0 on success.
128  */
129 int
130 qla2100_pci_config(scsi_qla_host_t *ha)
131 {
132         int ret;
133         uint16_t w;
134         uint32_t d;
135         unsigned long flags;
136         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
137
138         pci_set_master(ha->pdev);
139         ret = pci_set_mwi(ha->pdev);
140
141         pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
142         w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
143         pci_write_config_word(ha->pdev, PCI_COMMAND, w);
144
145         /* Reset expansion ROM address decode enable */
146         pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
147         d &= ~PCI_ROM_ADDRESS_ENABLE;
148         pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
149
150         /* Get PCI bus information. */
151         spin_lock_irqsave(&ha->hardware_lock, flags);
152         ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
153         spin_unlock_irqrestore(&ha->hardware_lock, flags);
154
155         return QLA_SUCCESS;
156 }
157
158 /**
159  * qla2300_pci_config() - Setup ISP23xx PCI configuration registers.
160  * @ha: HA context
161  *
162  * Returns 0 on success.
163  */
164 int
165 qla2300_pci_config(scsi_qla_host_t *ha)
166 {
167         int             ret;
168         uint16_t        w;
169         uint32_t        d;
170         unsigned long   flags = 0;
171         uint32_t        cnt;
172         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
173
174         pci_set_master(ha->pdev);
175         ret = pci_set_mwi(ha->pdev);
176
177         pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
178         w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
179
180         if (IS_QLA2322(ha) || IS_QLA6322(ha))
181                 w &= ~PCI_COMMAND_INTX_DISABLE;
182         pci_write_config_word(ha->pdev, PCI_COMMAND, w);
183
184         /*
185          * If this is a 2300 card and not 2312, reset the
186          * COMMAND_INVALIDATE due to a bug in the 2300. Unfortunately,
187          * the 2310 also reports itself as a 2300 so we need to get the
188          * fb revision level -- a 6 indicates it really is a 2300 and
189          * not a 2310.
190          */
191         if (IS_QLA2300(ha)) {
192                 spin_lock_irqsave(&ha->hardware_lock, flags);
193
194                 /* Pause RISC. */
195                 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
196                 for (cnt = 0; cnt < 30000; cnt++) {
197                         if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
198                                 break;
199
200                         udelay(10);
201                 }
202
203                 /* Select FPM registers. */
204                 WRT_REG_WORD(&reg->ctrl_status, 0x20);
205                 RD_REG_WORD(&reg->ctrl_status);
206
207                 /* Get the fb rev level */
208                 ha->fb_rev = RD_FB_CMD_REG(ha, reg);
209
210                 if (ha->fb_rev == FPM_2300)
211                         pci_clear_mwi(ha->pdev);
212
213                 /* Deselect FPM registers. */
214                 WRT_REG_WORD(&reg->ctrl_status, 0x0);
215                 RD_REG_WORD(&reg->ctrl_status);
216
217                 /* Release RISC module. */
218                 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
219                 for (cnt = 0; cnt < 30000; cnt++) {
220                         if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) == 0)
221                                 break;
222
223                         udelay(10);
224                 }
225
226                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
227         }
228
229         pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
230
231         /* Reset expansion ROM address decode enable */
232         pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
233         d &= ~PCI_ROM_ADDRESS_ENABLE;
234         pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
235
236         /* Get PCI bus information. */
237         spin_lock_irqsave(&ha->hardware_lock, flags);
238         ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
239         spin_unlock_irqrestore(&ha->hardware_lock, flags);
240
241         return QLA_SUCCESS;
242 }
243
244 /**
245  * qla24xx_pci_config() - Setup ISP24xx PCI configuration registers.
246  * @ha: HA context
247  *
248  * Returns 0 on success.
249  */
250 int
251 qla24xx_pci_config(scsi_qla_host_t *ha)
252 {
253         int ret;
254         uint16_t w;
255         uint32_t d;
256         unsigned long flags = 0;
257         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
258         int pcix_cmd_reg, pcie_dctl_reg;
259
260         pci_set_master(ha->pdev);
261         ret = pci_set_mwi(ha->pdev);
262
263         pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
264         w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
265         w &= ~PCI_COMMAND_INTX_DISABLE;
266         pci_write_config_word(ha->pdev, PCI_COMMAND, w);
267
268         pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
269
270         /* PCI-X -- adjust Maximum Memory Read Byte Count (2048). */
271         pcix_cmd_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_PCIX);
272         if (pcix_cmd_reg) {
273                 uint16_t pcix_cmd;
274
275                 pcix_cmd_reg += PCI_X_CMD;
276                 pci_read_config_word(ha->pdev, pcix_cmd_reg, &pcix_cmd);
277                 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
278                 pcix_cmd |= 0x0008;
279                 pci_write_config_word(ha->pdev, pcix_cmd_reg, pcix_cmd);
280         }
281
282         /* PCIe -- adjust Maximum Read Request Size (2048). */
283         pcie_dctl_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_EXP);
284         if (pcie_dctl_reg) {
285                 uint16_t pcie_dctl;
286
287                 pcie_dctl_reg += PCI_EXP_DEVCTL;
288                 pci_read_config_word(ha->pdev, pcie_dctl_reg, &pcie_dctl);
289                 pcie_dctl &= ~PCI_EXP_DEVCTL_READRQ;
290                 pcie_dctl |= 0x4000;
291                 pci_write_config_word(ha->pdev, pcie_dctl_reg, pcie_dctl);
292         }
293
294         /* Reset expansion ROM address decode enable */
295         pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
296         d &= ~PCI_ROM_ADDRESS_ENABLE;
297         pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
298
299         pci_read_config_word(ha->pdev, PCI_REVISION_ID, &ha->chip_revision);
300
301         /* Get PCI bus information. */
302         spin_lock_irqsave(&ha->hardware_lock, flags);
303         ha->pci_attr = RD_REG_DWORD(&reg->ctrl_status);
304         spin_unlock_irqrestore(&ha->hardware_lock, flags);
305
306         return QLA_SUCCESS;
307 }
308
309 /**
310  * qla2x00_isp_firmware() - Choose firmware image.
311  * @ha: HA context
312  *
313  * Returns 0 on success.
314  */
315 static int
316 qla2x00_isp_firmware(scsi_qla_host_t *ha)
317 {
318         int  rval;
319
320         /* Assume loading risc code */
321         rval = QLA_FUNCTION_FAILED;
322
323         if (ha->flags.disable_risc_code_load) {
324                 DEBUG2(printk("scsi(%ld): RISC CODE NOT loaded\n",
325                     ha->host_no));
326                 qla_printk(KERN_INFO, ha, "RISC CODE NOT loaded\n");
327
328                 /* Verify checksum of loaded RISC code. */
329                 rval = qla2x00_verify_checksum(ha, ha->fw_srisc_address);
330         }
331
332         if (rval) {
333                 DEBUG2_3(printk("scsi(%ld): **** Load RISC code ****\n",
334                     ha->host_no));
335         }
336
337         return (rval);
338 }
339
340 /**
341  * qla2x00_reset_chip() - Reset ISP chip.
342  * @ha: HA context
343  *
344  * Returns 0 on success.
345  */
346 void
347 qla2x00_reset_chip(scsi_qla_host_t *ha)
348 {
349         unsigned long   flags = 0;
350         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
351         uint32_t        cnt;
352         uint16_t        cmd;
353
354         ha->isp_ops.disable_intrs(ha);
355
356         spin_lock_irqsave(&ha->hardware_lock, flags);
357
358         /* Turn off master enable */
359         cmd = 0;
360         pci_read_config_word(ha->pdev, PCI_COMMAND, &cmd);
361         cmd &= ~PCI_COMMAND_MASTER;
362         pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
363
364         if (!IS_QLA2100(ha)) {
365                 /* Pause RISC. */
366                 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
367                 if (IS_QLA2200(ha) || IS_QLA2300(ha)) {
368                         for (cnt = 0; cnt < 30000; cnt++) {
369                                 if ((RD_REG_WORD(&reg->hccr) &
370                                     HCCR_RISC_PAUSE) != 0)
371                                         break;
372                                 udelay(100);
373                         }
374                 } else {
375                         RD_REG_WORD(&reg->hccr);        /* PCI Posting. */
376                         udelay(10);
377                 }
378
379                 /* Select FPM registers. */
380                 WRT_REG_WORD(&reg->ctrl_status, 0x20);
381                 RD_REG_WORD(&reg->ctrl_status);         /* PCI Posting. */
382
383                 /* FPM Soft Reset. */
384                 WRT_REG_WORD(&reg->fpm_diag_config, 0x100);
385                 RD_REG_WORD(&reg->fpm_diag_config);     /* PCI Posting. */
386
387                 /* Toggle Fpm Reset. */
388                 if (!IS_QLA2200(ha)) {
389                         WRT_REG_WORD(&reg->fpm_diag_config, 0x0);
390                         RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
391                 }
392
393                 /* Select frame buffer registers. */
394                 WRT_REG_WORD(&reg->ctrl_status, 0x10);
395                 RD_REG_WORD(&reg->ctrl_status);         /* PCI Posting. */
396
397                 /* Reset frame buffer FIFOs. */
398                 if (IS_QLA2200(ha)) {
399                         WRT_FB_CMD_REG(ha, reg, 0xa000);
400                         RD_FB_CMD_REG(ha, reg);         /* PCI Posting. */
401                 } else {
402                         WRT_FB_CMD_REG(ha, reg, 0x00fc);
403
404                         /* Read back fb_cmd until zero or 3 seconds max */
405                         for (cnt = 0; cnt < 3000; cnt++) {
406                                 if ((RD_FB_CMD_REG(ha, reg) & 0xff) == 0)
407                                         break;
408                                 udelay(100);
409                         }
410                 }
411
412                 /* Select RISC module registers. */
413                 WRT_REG_WORD(&reg->ctrl_status, 0);
414                 RD_REG_WORD(&reg->ctrl_status);         /* PCI Posting. */
415
416                 /* Reset RISC processor. */
417                 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
418                 RD_REG_WORD(&reg->hccr);                /* PCI Posting. */
419
420                 /* Release RISC processor. */
421                 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
422                 RD_REG_WORD(&reg->hccr);                /* PCI Posting. */
423         }
424
425         WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
426         WRT_REG_WORD(&reg->hccr, HCCR_CLR_HOST_INT);
427
428         /* Reset ISP chip. */
429         WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
430
431         /* Wait for RISC to recover from reset. */
432         if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
433                 /*
434                  * It is necessary to for a delay here since the card doesn't
435                  * respond to PCI reads during a reset. On some architectures
436                  * this will result in an MCA.
437                  */
438                 udelay(20);
439                 for (cnt = 30000; cnt; cnt--) {
440                         if ((RD_REG_WORD(&reg->ctrl_status) &
441                             CSR_ISP_SOFT_RESET) == 0)
442                                 break;
443                         udelay(100);
444                 }
445         } else
446                 udelay(10);
447
448         /* Reset RISC processor. */
449         WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
450
451         WRT_REG_WORD(&reg->semaphore, 0);
452
453         /* Release RISC processor. */
454         WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
455         RD_REG_WORD(&reg->hccr);                        /* PCI Posting. */
456
457         if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
458                 for (cnt = 0; cnt < 30000; cnt++) {
459                         if (RD_MAILBOX_REG(ha, reg, 0) != MBS_BUSY)
460                                 break;
461
462                         udelay(100);
463                 }
464         } else
465                 udelay(100);
466
467         /* Turn on master enable */
468         cmd |= PCI_COMMAND_MASTER;
469         pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
470
471         /* Disable RISC pause on FPM parity error. */
472         if (!IS_QLA2100(ha)) {
473                 WRT_REG_WORD(&reg->hccr, HCCR_DISABLE_PARITY_PAUSE);
474                 RD_REG_WORD(&reg->hccr);                /* PCI Posting. */
475         }
476
477         spin_unlock_irqrestore(&ha->hardware_lock, flags);
478 }
479
480 /**
481  * qla24xx_reset_risc() - Perform full reset of ISP24xx RISC.
482  * @ha: HA context
483  *
484  * Returns 0 on success.
485  */
486 static inline void
487 qla24xx_reset_risc(scsi_qla_host_t *ha)
488 {
489         unsigned long flags = 0;
490         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
491         uint32_t cnt, d2;
492         uint16_t wd;
493
494         spin_lock_irqsave(&ha->hardware_lock, flags);
495
496         /* Reset RISC. */
497         WRT_REG_DWORD(&reg->ctrl_status, CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
498         for (cnt = 0; cnt < 30000; cnt++) {
499                 if ((RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE) == 0)
500                         break;
501
502                 udelay(10);
503         }
504
505         WRT_REG_DWORD(&reg->ctrl_status,
506             CSRX_ISP_SOFT_RESET|CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
507         pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
508
509         udelay(100);
510         /* Wait for firmware to complete NVRAM accesses. */
511         d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
512         for (cnt = 10000 ; cnt && d2; cnt--) {
513                 udelay(5);
514                 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
515                 barrier();
516         }
517
518         /* Wait for soft-reset to complete. */
519         d2 = RD_REG_DWORD(&reg->ctrl_status);
520         for (cnt = 6000000 ; cnt && (d2 & CSRX_ISP_SOFT_RESET); cnt--) {
521                 udelay(5);
522                 d2 = RD_REG_DWORD(&reg->ctrl_status);
523                 barrier();
524         }
525
526         WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
527         RD_REG_DWORD(&reg->hccr);
528
529         WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
530         RD_REG_DWORD(&reg->hccr);
531
532         WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_RESET);
533         RD_REG_DWORD(&reg->hccr);
534
535         d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
536         for (cnt = 6000000 ; cnt && d2; cnt--) {
537                 udelay(5);
538                 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
539                 barrier();
540         }
541
542         spin_unlock_irqrestore(&ha->hardware_lock, flags);
543 }
544
545 /**
546  * qla24xx_reset_chip() - Reset ISP24xx chip.
547  * @ha: HA context
548  *
549  * Returns 0 on success.
550  */
551 void
552 qla24xx_reset_chip(scsi_qla_host_t *ha)
553 {
554         ha->isp_ops.disable_intrs(ha);
555
556         /* Perform RISC reset. */
557         qla24xx_reset_risc(ha);
558 }
559
560 /**
561  * qla2x00_chip_diag() - Test chip for proper operation.
562  * @ha: HA context
563  *
564  * Returns 0 on success.
565  */
566 int
567 qla2x00_chip_diag(scsi_qla_host_t *ha)
568 {
569         int             rval;
570         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
571         unsigned long   flags = 0;
572         uint16_t        data;
573         uint32_t        cnt;
574         uint16_t        mb[5];
575
576         /* Assume a failed state */
577         rval = QLA_FUNCTION_FAILED;
578
579         DEBUG3(printk("scsi(%ld): Testing device at %lx.\n",
580             ha->host_no, (u_long)&reg->flash_address));
581
582         spin_lock_irqsave(&ha->hardware_lock, flags);
583
584         /* Reset ISP chip. */
585         WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
586
587         /*
588          * We need to have a delay here since the card will not respond while
589          * in reset causing an MCA on some architectures.
590          */
591         udelay(20);
592         data = qla2x00_debounce_register(&reg->ctrl_status);
593         for (cnt = 6000000 ; cnt && (data & CSR_ISP_SOFT_RESET); cnt--) {
594                 udelay(5);
595                 data = RD_REG_WORD(&reg->ctrl_status);
596                 barrier();
597         }
598
599         if (!cnt)
600                 goto chip_diag_failed;
601
602         DEBUG3(printk("scsi(%ld): Reset register cleared by chip reset\n",
603             ha->host_no));
604
605         /* Reset RISC processor. */
606         WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
607         WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
608
609         /* Workaround for QLA2312 PCI parity error */
610         if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
611                 data = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 0));
612                 for (cnt = 6000000; cnt && (data == MBS_BUSY); cnt--) {
613                         udelay(5);
614                         data = RD_MAILBOX_REG(ha, reg, 0);
615                         barrier();
616                 }
617         } else
618                 udelay(10);
619
620         if (!cnt)
621                 goto chip_diag_failed;
622
623         /* Check product ID of chip */
624         DEBUG3(printk("scsi(%ld): Checking product ID of chip\n", ha->host_no));
625
626         mb[1] = RD_MAILBOX_REG(ha, reg, 1);
627         mb[2] = RD_MAILBOX_REG(ha, reg, 2);
628         mb[3] = RD_MAILBOX_REG(ha, reg, 3);
629         mb[4] = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 4));
630         if (mb[1] != PROD_ID_1 || (mb[2] != PROD_ID_2 && mb[2] != PROD_ID_2a) ||
631             mb[3] != PROD_ID_3) {
632                 qla_printk(KERN_WARNING, ha,
633                     "Wrong product ID = 0x%x,0x%x,0x%x\n", mb[1], mb[2], mb[3]);
634
635                 goto chip_diag_failed;
636         }
637         ha->product_id[0] = mb[1];
638         ha->product_id[1] = mb[2];
639         ha->product_id[2] = mb[3];
640         ha->product_id[3] = mb[4];
641
642         /* Adjust fw RISC transfer size */
643         if (ha->request_q_length > 1024)
644                 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
645         else
646                 ha->fw_transfer_size = REQUEST_ENTRY_SIZE *
647                     ha->request_q_length;
648
649         if (IS_QLA2200(ha) &&
650             RD_MAILBOX_REG(ha, reg, 7) == QLA2200A_RISC_ROM_VER) {
651                 /* Limit firmware transfer size with a 2200A */
652                 DEBUG3(printk("scsi(%ld): Found QLA2200A chip.\n",
653                     ha->host_no));
654
655                 ha->device_type |= DT_ISP2200A;
656                 ha->fw_transfer_size = 128;
657         }
658
659         /* Wrap Incoming Mailboxes Test. */
660         spin_unlock_irqrestore(&ha->hardware_lock, flags);
661
662         DEBUG3(printk("scsi(%ld): Checking mailboxes.\n", ha->host_no));
663         rval = qla2x00_mbx_reg_test(ha);
664         if (rval) {
665                 DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
666                     ha->host_no));
667                 qla_printk(KERN_WARNING, ha,
668                     "Failed mailbox send register test\n");
669         }
670         else {
671                 /* Flag a successful rval */
672                 rval = QLA_SUCCESS;
673         }
674         spin_lock_irqsave(&ha->hardware_lock, flags);
675
676 chip_diag_failed:
677         if (rval)
678                 DEBUG2_3(printk("scsi(%ld): Chip diagnostics **** FAILED "
679                     "****\n", ha->host_no));
680
681         spin_unlock_irqrestore(&ha->hardware_lock, flags);
682
683         return (rval);
684 }
685
686 /**
687  * qla24xx_chip_diag() - Test ISP24xx for proper operation.
688  * @ha: HA context
689  *
690  * Returns 0 on success.
691  */
692 int
693 qla24xx_chip_diag(scsi_qla_host_t *ha)
694 {
695         int rval;
696
697         /* Perform RISC reset. */
698         qla24xx_reset_risc(ha);
699
700         ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
701
702         rval = qla2x00_mbx_reg_test(ha);
703         if (rval) {
704                 DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
705                     ha->host_no));
706                 qla_printk(KERN_WARNING, ha,
707                     "Failed mailbox send register test\n");
708         } else {
709                 /* Flag a successful rval */
710                 rval = QLA_SUCCESS;
711         }
712
713         return rval;
714 }
715
716 void
717 qla2x00_alloc_fw_dump(scsi_qla_host_t *ha)
718 {
719         int rval;
720         uint32_t dump_size, fixed_size, mem_size, req_q_size, rsp_q_size,
721             eft_size;
722         dma_addr_t eft_dma;
723         void *eft;
724
725         if (ha->fw_dump) {
726                 qla_printk(KERN_WARNING, ha,
727                     "Firmware dump previously allocated.\n");
728                 return;
729         }
730
731         ha->fw_dumped = 0;
732         fixed_size = mem_size = eft_size = 0;
733         if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
734                 fixed_size = sizeof(struct qla2100_fw_dump);
735         } else if (IS_QLA23XX(ha)) {
736                 fixed_size = offsetof(struct qla2300_fw_dump, data_ram);
737                 mem_size = (ha->fw_memory_size - 0x11000 + 1) *
738                     sizeof(uint16_t);
739         } else if (IS_QLA24XX(ha) || IS_QLA54XX(ha)) {
740                 fixed_size = offsetof(struct qla24xx_fw_dump, ext_mem);
741                 mem_size = (ha->fw_memory_size - 0x100000 + 1) *
742                     sizeof(uint32_t);
743
744                 /* Allocate memory for Extended Trace Buffer. */
745                 eft = dma_alloc_coherent(&ha->pdev->dev, EFT_SIZE, &eft_dma,
746                     GFP_KERNEL);
747                 if (!eft) {
748                         qla_printk(KERN_WARNING, ha, "Unable to allocate "
749                             "(%d KB) for EFT.\n", EFT_SIZE / 1024);
750                         goto cont_alloc;
751                 }
752
753                 rval = qla2x00_trace_control(ha, TC_ENABLE, eft_dma,
754                     EFT_NUM_BUFFERS);
755                 if (rval) {
756                         qla_printk(KERN_WARNING, ha, "Unable to initialize "
757                             "EFT (%d).\n", rval);
758                         dma_free_coherent(&ha->pdev->dev, EFT_SIZE, eft,
759                             eft_dma);
760                         goto cont_alloc;
761                 }
762
763                 qla_printk(KERN_INFO, ha, "Allocated (%d KB) for EFT...\n",
764                     EFT_SIZE / 1024);
765
766                 eft_size = EFT_SIZE;
767                 memset(eft, 0, eft_size);
768                 ha->eft_dma = eft_dma;
769                 ha->eft = eft;
770         }
771 cont_alloc:
772         req_q_size = ha->request_q_length * sizeof(request_t);
773         rsp_q_size = ha->response_q_length * sizeof(response_t);
774
775         dump_size = offsetof(struct qla2xxx_fw_dump, isp);
776         dump_size += fixed_size + mem_size + req_q_size + rsp_q_size +
777             eft_size;
778
779         ha->fw_dump = vmalloc(dump_size);
780         if (!ha->fw_dump) {
781                 qla_printk(KERN_WARNING, ha, "Unable to allocate (%d KB) for "
782                     "firmware dump!!!\n", dump_size / 1024);
783
784                 if (ha->eft) {
785                         dma_free_coherent(&ha->pdev->dev, eft_size, ha->eft,
786                             ha->eft_dma);
787                         ha->eft = NULL;
788                         ha->eft_dma = 0;
789                 }
790                 return;
791         }
792
793         qla_printk(KERN_INFO, ha, "Allocated (%d KB) for firmware dump...\n",
794             dump_size / 1024);
795
796         ha->fw_dump_len = dump_size;
797         ha->fw_dump->signature[0] = 'Q';
798         ha->fw_dump->signature[1] = 'L';
799         ha->fw_dump->signature[2] = 'G';
800         ha->fw_dump->signature[3] = 'C';
801         ha->fw_dump->version = __constant_htonl(1);
802
803         ha->fw_dump->fixed_size = htonl(fixed_size);
804         ha->fw_dump->mem_size = htonl(mem_size);
805         ha->fw_dump->req_q_size = htonl(req_q_size);
806         ha->fw_dump->rsp_q_size = htonl(rsp_q_size);
807
808         ha->fw_dump->eft_size = htonl(eft_size);
809         ha->fw_dump->eft_addr_l = htonl(LSD(ha->eft_dma));
810         ha->fw_dump->eft_addr_h = htonl(MSD(ha->eft_dma));
811
812         ha->fw_dump->header_size =
813             htonl(offsetof(struct qla2xxx_fw_dump, isp));
814 }
815
816 /**
817  * qla2x00_resize_request_q() - Resize request queue given available ISP memory.
818  * @ha: HA context
819  *
820  * Returns 0 on success.
821  */
822 static void
823 qla2x00_resize_request_q(scsi_qla_host_t *ha)
824 {
825         int rval;
826         uint16_t fw_iocb_cnt = 0;
827         uint16_t request_q_length = REQUEST_ENTRY_CNT_2XXX_EXT_MEM;
828         dma_addr_t request_dma;
829         request_t *request_ring;
830
831         /* Valid only on recent ISPs. */
832         if (IS_QLA2100(ha) || IS_QLA2200(ha))
833                 return;
834
835         /* Retrieve IOCB counts available to the firmware. */
836         rval = qla2x00_get_resource_cnts(ha, NULL, NULL, NULL, &fw_iocb_cnt);
837         if (rval)
838                 return;
839         /* No point in continuing if current settings are sufficient. */
840         if (fw_iocb_cnt < 1024)
841                 return;
842         if (ha->request_q_length >= request_q_length)
843                 return;
844
845         /* Attempt to claim larger area for request queue. */
846         request_ring = dma_alloc_coherent(&ha->pdev->dev,
847             (request_q_length + 1) * sizeof(request_t), &request_dma,
848             GFP_KERNEL);
849         if (request_ring == NULL)
850                 return;
851
852         /* Resize successful, report extensions. */
853         qla_printk(KERN_INFO, ha, "Extended memory detected (%d KB)...\n",
854             (ha->fw_memory_size + 1) / 1024);
855         qla_printk(KERN_INFO, ha, "Resizing request queue depth "
856             "(%d -> %d)...\n", ha->request_q_length, request_q_length);
857
858         /* Clear old allocations. */
859         dma_free_coherent(&ha->pdev->dev,
860             (ha->request_q_length + 1) * sizeof(request_t), ha->request_ring,
861             ha->request_dma);
862
863         /* Begin using larger queue. */
864         ha->request_q_length = request_q_length;
865         ha->request_ring = request_ring;
866         ha->request_dma = request_dma;
867 }
868
869 /**
870  * qla2x00_setup_chip() - Load and start RISC firmware.
871  * @ha: HA context
872  *
873  * Returns 0 on success.
874  */
875 static int
876 qla2x00_setup_chip(scsi_qla_host_t *ha)
877 {
878         int rval;
879         uint32_t srisc_address = 0;
880
881         /* Load firmware sequences */
882         rval = ha->isp_ops.load_risc(ha, &srisc_address);
883         if (rval == QLA_SUCCESS) {
884                 DEBUG(printk("scsi(%ld): Verifying Checksum of loaded RISC "
885                     "code.\n", ha->host_no));
886
887                 rval = qla2x00_verify_checksum(ha, srisc_address);
888                 if (rval == QLA_SUCCESS) {
889                         /* Start firmware execution. */
890                         DEBUG(printk("scsi(%ld): Checksum OK, start "
891                             "firmware.\n", ha->host_no));
892
893                         rval = qla2x00_execute_fw(ha, srisc_address);
894                         /* Retrieve firmware information. */
895                         if (rval == QLA_SUCCESS && ha->fw_major_version == 0) {
896                                 qla2x00_get_fw_version(ha,
897                                     &ha->fw_major_version,
898                                     &ha->fw_minor_version,
899                                     &ha->fw_subminor_version,
900                                     &ha->fw_attributes, &ha->fw_memory_size);
901                                 qla2x00_resize_request_q(ha);
902                                 ha->flags.npiv_supported = 0;
903                                 if (IS_QLA24XX(ha) &&
904                                     (ha->fw_attributes & BIT_2))
905                                         ha->flags.npiv_supported = 1;
906
907                                 if (ql2xallocfwdump)
908                                         qla2x00_alloc_fw_dump(ha);
909                         }
910                 } else {
911                         DEBUG2(printk(KERN_INFO
912                             "scsi(%ld): ISP Firmware failed checksum.\n",
913                             ha->host_no));
914                 }
915         }
916
917         if (rval) {
918                 DEBUG2_3(printk("scsi(%ld): Setup chip **** FAILED ****.\n",
919                     ha->host_no));
920         }
921
922         return (rval);
923 }
924
925 /**
926  * qla2x00_init_response_q_entries() - Initializes response queue entries.
927  * @ha: HA context
928  *
929  * Beginning of request ring has initialization control block already built
930  * by nvram config routine.
931  *
932  * Returns 0 on success.
933  */
934 static void
935 qla2x00_init_response_q_entries(scsi_qla_host_t *ha)
936 {
937         uint16_t cnt;
938         response_t *pkt;
939
940         pkt = ha->response_ring_ptr;
941         for (cnt = 0; cnt < ha->response_q_length; cnt++) {
942                 pkt->signature = RESPONSE_PROCESSED;
943                 pkt++;
944         }
945
946 }
947
948 /**
949  * qla2x00_update_fw_options() - Read and process firmware options.
950  * @ha: HA context
951  *
952  * Returns 0 on success.
953  */
954 void
955 qla2x00_update_fw_options(scsi_qla_host_t *ha)
956 {
957         uint16_t swing, emphasis, tx_sens, rx_sens;
958
959         memset(ha->fw_options, 0, sizeof(ha->fw_options));
960         qla2x00_get_fw_options(ha, ha->fw_options);
961
962         if (IS_QLA2100(ha) || IS_QLA2200(ha))
963                 return;
964
965         /* Serial Link options. */
966         DEBUG3(printk("scsi(%ld): Serial link options:\n",
967             ha->host_no));
968         DEBUG3(qla2x00_dump_buffer((uint8_t *)&ha->fw_seriallink_options,
969             sizeof(ha->fw_seriallink_options)));
970
971         ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
972         if (ha->fw_seriallink_options[3] & BIT_2) {
973                 ha->fw_options[1] |= FO1_SET_EMPHASIS_SWING;
974
975                 /*  1G settings */
976                 swing = ha->fw_seriallink_options[2] & (BIT_2 | BIT_1 | BIT_0);
977                 emphasis = (ha->fw_seriallink_options[2] &
978                     (BIT_4 | BIT_3)) >> 3;
979                 tx_sens = ha->fw_seriallink_options[0] &
980                     (BIT_3 | BIT_2 | BIT_1 | BIT_0);
981                 rx_sens = (ha->fw_seriallink_options[0] &
982                     (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
983                 ha->fw_options[10] = (emphasis << 14) | (swing << 8);
984                 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
985                         if (rx_sens == 0x0)
986                                 rx_sens = 0x3;
987                         ha->fw_options[10] |= (tx_sens << 4) | rx_sens;
988                 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
989                         ha->fw_options[10] |= BIT_5 |
990                             ((rx_sens & (BIT_1 | BIT_0)) << 2) |
991                             (tx_sens & (BIT_1 | BIT_0));
992
993                 /*  2G settings */
994                 swing = (ha->fw_seriallink_options[2] &
995                     (BIT_7 | BIT_6 | BIT_5)) >> 5;
996                 emphasis = ha->fw_seriallink_options[3] & (BIT_1 | BIT_0);
997                 tx_sens = ha->fw_seriallink_options[1] &
998                     (BIT_3 | BIT_2 | BIT_1 | BIT_0);
999                 rx_sens = (ha->fw_seriallink_options[1] &
1000                     (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
1001                 ha->fw_options[11] = (emphasis << 14) | (swing << 8);
1002                 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
1003                         if (rx_sens == 0x0)
1004                                 rx_sens = 0x3;
1005                         ha->fw_options[11] |= (tx_sens << 4) | rx_sens;
1006                 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
1007                         ha->fw_options[11] |= BIT_5 |
1008                             ((rx_sens & (BIT_1 | BIT_0)) << 2) |
1009                             (tx_sens & (BIT_1 | BIT_0));
1010         }
1011
1012         /* FCP2 options. */
1013         /*  Return command IOCBs without waiting for an ABTS to complete. */
1014         ha->fw_options[3] |= BIT_13;
1015
1016         /* LED scheme. */
1017         if (ha->flags.enable_led_scheme)
1018                 ha->fw_options[2] |= BIT_12;
1019
1020         /* Detect ISP6312. */
1021         if (IS_QLA6312(ha))
1022                 ha->fw_options[2] |= BIT_13;
1023
1024         /* Update firmware options. */
1025         qla2x00_set_fw_options(ha, ha->fw_options);
1026 }
1027
1028 void
1029 qla24xx_update_fw_options(scsi_qla_host_t *ha)
1030 {
1031         int rval;
1032
1033         /* Update Serial Link options. */
1034         if ((le16_to_cpu(ha->fw_seriallink_options24[0]) & BIT_0) == 0)
1035                 return;
1036
1037         rval = qla2x00_set_serdes_params(ha,
1038             le16_to_cpu(ha->fw_seriallink_options24[1]),
1039             le16_to_cpu(ha->fw_seriallink_options24[2]),
1040             le16_to_cpu(ha->fw_seriallink_options24[3]));
1041         if (rval != QLA_SUCCESS) {
1042                 qla_printk(KERN_WARNING, ha,
1043                     "Unable to update Serial Link options (%x).\n", rval);
1044         }
1045 }
1046
1047 void
1048 qla2x00_config_rings(struct scsi_qla_host *ha)
1049 {
1050         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1051
1052         /* Setup ring parameters in initialization control block. */
1053         ha->init_cb->request_q_outpointer = __constant_cpu_to_le16(0);
1054         ha->init_cb->response_q_inpointer = __constant_cpu_to_le16(0);
1055         ha->init_cb->request_q_length = cpu_to_le16(ha->request_q_length);
1056         ha->init_cb->response_q_length = cpu_to_le16(ha->response_q_length);
1057         ha->init_cb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
1058         ha->init_cb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
1059         ha->init_cb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
1060         ha->init_cb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
1061
1062         WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), 0);
1063         WRT_REG_WORD(ISP_REQ_Q_OUT(ha, reg), 0);
1064         WRT_REG_WORD(ISP_RSP_Q_IN(ha, reg), 0);
1065         WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), 0);
1066         RD_REG_WORD(ISP_RSP_Q_OUT(ha, reg));            /* PCI Posting. */
1067 }
1068
1069 void
1070 qla24xx_config_rings(struct scsi_qla_host *ha)
1071 {
1072         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1073         struct init_cb_24xx *icb;
1074
1075         /* Setup ring parameters in initialization control block. */
1076         icb = (struct init_cb_24xx *)ha->init_cb;
1077         icb->request_q_outpointer = __constant_cpu_to_le16(0);
1078         icb->response_q_inpointer = __constant_cpu_to_le16(0);
1079         icb->request_q_length = cpu_to_le16(ha->request_q_length);
1080         icb->response_q_length = cpu_to_le16(ha->response_q_length);
1081         icb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
1082         icb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
1083         icb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
1084         icb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
1085
1086         WRT_REG_DWORD(&reg->req_q_in, 0);
1087         WRT_REG_DWORD(&reg->req_q_out, 0);
1088         WRT_REG_DWORD(&reg->rsp_q_in, 0);
1089         WRT_REG_DWORD(&reg->rsp_q_out, 0);
1090         RD_REG_DWORD(&reg->rsp_q_out);
1091 }
1092
1093 /**
1094  * qla2x00_init_rings() - Initializes firmware.
1095  * @ha: HA context
1096  *
1097  * Beginning of request ring has initialization control block already built
1098  * by nvram config routine.
1099  *
1100  * Returns 0 on success.
1101  */
1102 static int
1103 qla2x00_init_rings(scsi_qla_host_t *ha)
1104 {
1105         int     rval;
1106         unsigned long flags = 0;
1107         int cnt;
1108         struct mid_init_cb_24xx *mid_init_cb =
1109             (struct mid_init_cb_24xx *) ha->init_cb;
1110
1111         spin_lock_irqsave(&ha->hardware_lock, flags);
1112
1113         /* Clear outstanding commands array. */
1114         for (cnt = 0; cnt < MAX_OUTSTANDING_COMMANDS; cnt++)
1115                 ha->outstanding_cmds[cnt] = NULL;
1116
1117         ha->current_outstanding_cmd = 0;
1118
1119         /* Clear RSCN queue. */
1120         ha->rscn_in_ptr = 0;
1121         ha->rscn_out_ptr = 0;
1122
1123         /* Initialize firmware. */
1124         ha->request_ring_ptr  = ha->request_ring;
1125         ha->req_ring_index    = 0;
1126         ha->req_q_cnt         = ha->request_q_length;
1127         ha->response_ring_ptr = ha->response_ring;
1128         ha->rsp_ring_index    = 0;
1129
1130         /* Initialize response queue entries */
1131         qla2x00_init_response_q_entries(ha);
1132
1133         ha->isp_ops.config_rings(ha);
1134
1135         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1136
1137         /* Update any ISP specific firmware options before initialization. */
1138         ha->isp_ops.update_fw_options(ha);
1139
1140         DEBUG(printk("scsi(%ld): Issue init firmware.\n", ha->host_no));
1141
1142         mid_init_cb->count = MAX_NUM_VPORT_FABRIC;
1143         ha->max_npiv_vports = MAX_NUM_VPORT_FABRIC;
1144
1145         rval = qla2x00_init_firmware(ha, ha->init_cb_size);
1146         if (rval) {
1147                 DEBUG2_3(printk("scsi(%ld): Init firmware **** FAILED ****.\n",
1148                     ha->host_no));
1149         } else {
1150                 DEBUG3(printk("scsi(%ld): Init firmware -- success.\n",
1151                     ha->host_no));
1152         }
1153
1154         return (rval);
1155 }
1156
1157 /**
1158  * qla2x00_fw_ready() - Waits for firmware ready.
1159  * @ha: HA context
1160  *
1161  * Returns 0 on success.
1162  */
1163 static int
1164 qla2x00_fw_ready(scsi_qla_host_t *ha)
1165 {
1166         int             rval;
1167         unsigned long   wtime, mtime;
1168         uint16_t        min_wait;       /* Minimum wait time if loop is down */
1169         uint16_t        wait_time;      /* Wait time if loop is coming ready */
1170         uint16_t        fw_state;
1171
1172         rval = QLA_SUCCESS;
1173
1174         /* 20 seconds for loop down. */
1175         min_wait = 20;
1176
1177         /*
1178          * Firmware should take at most one RATOV to login, plus 5 seconds for
1179          * our own processing.
1180          */
1181         if ((wait_time = (ha->retry_count*ha->login_timeout) + 5) < min_wait) {
1182                 wait_time = min_wait;
1183         }
1184
1185         /* Min wait time if loop down */
1186         mtime = jiffies + (min_wait * HZ);
1187
1188         /* wait time before firmware ready */
1189         wtime = jiffies + (wait_time * HZ);
1190
1191         /* Wait for ISP to finish LIP */
1192         if (!ha->flags.init_done)
1193                 qla_printk(KERN_INFO, ha, "Waiting for LIP to complete...\n");
1194
1195         DEBUG3(printk("scsi(%ld): Waiting for LIP to complete...\n",
1196             ha->host_no));
1197
1198         do {
1199                 rval = qla2x00_get_firmware_state(ha, &fw_state);
1200                 if (rval == QLA_SUCCESS) {
1201                         if (fw_state < FSTATE_LOSS_OF_SYNC) {
1202                                 ha->device_flags &= ~DFLG_NO_CABLE;
1203                         }
1204                         if (fw_state == FSTATE_READY) {
1205                                 DEBUG(printk("scsi(%ld): F/W Ready - OK \n",
1206                                     ha->host_no));
1207
1208                                 qla2x00_get_retry_cnt(ha, &ha->retry_count,
1209                                     &ha->login_timeout, &ha->r_a_tov);
1210
1211                                 rval = QLA_SUCCESS;
1212                                 break;
1213                         }
1214
1215                         rval = QLA_FUNCTION_FAILED;
1216
1217                         if (atomic_read(&ha->loop_down_timer) &&
1218                             fw_state != FSTATE_READY) {
1219                                 /* Loop down. Timeout on min_wait for states
1220                                  * other than Wait for Login.
1221                                  */
1222                                 if (time_after_eq(jiffies, mtime)) {
1223                                         qla_printk(KERN_INFO, ha,
1224                                             "Cable is unplugged...\n");
1225
1226                                         ha->device_flags |= DFLG_NO_CABLE;
1227                                         break;
1228                                 }
1229                         }
1230                 } else {
1231                         /* Mailbox cmd failed. Timeout on min_wait. */
1232                         if (time_after_eq(jiffies, mtime))
1233                                 break;
1234                 }
1235
1236                 if (time_after_eq(jiffies, wtime))
1237                         break;
1238
1239                 /* Delay for a while */
1240                 msleep(500);
1241
1242                 DEBUG3(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
1243                     ha->host_no, fw_state, jiffies));
1244         } while (1);
1245
1246         DEBUG(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
1247             ha->host_no, fw_state, jiffies));
1248
1249         if (rval) {
1250                 DEBUG2_3(printk("scsi(%ld): Firmware ready **** FAILED ****.\n",
1251                     ha->host_no));
1252         }
1253
1254         return (rval);
1255 }
1256
1257 /*
1258 *  qla2x00_configure_hba
1259 *      Setup adapter context.
1260 *
1261 * Input:
1262 *      ha = adapter state pointer.
1263 *
1264 * Returns:
1265 *      0 = success
1266 *
1267 * Context:
1268 *      Kernel context.
1269 */
1270 static int
1271 qla2x00_configure_hba(scsi_qla_host_t *ha)
1272 {
1273         int       rval;
1274         uint16_t      loop_id;
1275         uint16_t      topo;
1276         uint16_t      sw_cap;
1277         uint8_t       al_pa;
1278         uint8_t       area;
1279         uint8_t       domain;
1280         char            connect_type[22];
1281
1282         /* Get host addresses. */
1283         rval = qla2x00_get_adapter_id(ha,
1284             &loop_id, &al_pa, &area, &domain, &topo, &sw_cap);
1285         if (rval != QLA_SUCCESS) {
1286                 if (LOOP_TRANSITION(ha) || atomic_read(&ha->loop_down_timer) ||
1287                     (rval == QLA_COMMAND_ERROR && loop_id == 0x7)) {
1288                         DEBUG2(printk("%s(%ld) Loop is in a transition state\n",
1289                             __func__, ha->host_no));
1290                 } else {
1291                         qla_printk(KERN_WARNING, ha,
1292                             "ERROR -- Unable to get host loop ID.\n");
1293                         set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
1294                 }
1295                 return (rval);
1296         }
1297
1298         if (topo == 4) {
1299                 qla_printk(KERN_INFO, ha,
1300                         "Cannot get topology - retrying.\n");
1301                 return (QLA_FUNCTION_FAILED);
1302         }
1303
1304         ha->loop_id = loop_id;
1305
1306         /* initialize */
1307         ha->min_external_loopid = SNS_FIRST_LOOP_ID;
1308         ha->operating_mode = LOOP;
1309         ha->switch_cap = 0;
1310
1311         switch (topo) {
1312         case 0:
1313                 DEBUG3(printk("scsi(%ld): HBA in NL topology.\n",
1314                     ha->host_no));
1315                 ha->current_topology = ISP_CFG_NL;
1316                 strcpy(connect_type, "(Loop)");
1317                 break;
1318
1319         case 1:
1320                 DEBUG3(printk("scsi(%ld): HBA in FL topology.\n",
1321                     ha->host_no));
1322                 ha->switch_cap = sw_cap;
1323                 ha->current_topology = ISP_CFG_FL;
1324                 strcpy(connect_type, "(FL_Port)");
1325                 break;
1326
1327         case 2:
1328                 DEBUG3(printk("scsi(%ld): HBA in N P2P topology.\n",
1329                     ha->host_no));
1330                 ha->operating_mode = P2P;
1331                 ha->current_topology = ISP_CFG_N;
1332                 strcpy(connect_type, "(N_Port-to-N_Port)");
1333                 break;
1334
1335         case 3:
1336                 DEBUG3(printk("scsi(%ld): HBA in F P2P topology.\n",
1337                     ha->host_no));
1338                 ha->switch_cap = sw_cap;
1339                 ha->operating_mode = P2P;
1340                 ha->current_topology = ISP_CFG_F;
1341                 strcpy(connect_type, "(F_Port)");
1342                 break;
1343
1344         default:
1345                 DEBUG3(printk("scsi(%ld): HBA in unknown topology %x. "
1346                     "Using NL.\n",
1347                     ha->host_no, topo));
1348                 ha->current_topology = ISP_CFG_NL;
1349                 strcpy(connect_type, "(Loop)");
1350                 break;
1351         }
1352
1353         /* Save Host port and loop ID. */
1354         /* byte order - Big Endian */
1355         ha->d_id.b.domain = domain;
1356         ha->d_id.b.area = area;
1357         ha->d_id.b.al_pa = al_pa;
1358
1359         if (!ha->flags.init_done)
1360                 qla_printk(KERN_INFO, ha,
1361                     "Topology - %s, Host Loop address 0x%x\n",
1362                     connect_type, ha->loop_id);
1363
1364         if (rval) {
1365                 DEBUG2_3(printk("scsi(%ld): FAILED.\n", ha->host_no));
1366         } else {
1367                 DEBUG3(printk("scsi(%ld): exiting normally.\n", ha->host_no));
1368         }
1369
1370         return(rval);
1371 }
1372
1373 static inline void
1374 qla2x00_set_model_info(scsi_qla_host_t *ha, uint8_t *model, size_t len, char *def)
1375 {
1376         char *st, *en;
1377         uint16_t index;
1378
1379         if (memcmp(model, BINZERO, len) != 0) {
1380                 strncpy(ha->model_number, model, len);
1381                 st = en = ha->model_number;
1382                 en += len - 1;
1383                 while (en > st) {
1384                         if (*en != 0x20 && *en != 0x00)
1385                                 break;
1386                         *en-- = '\0';
1387                 }
1388
1389                 index = (ha->pdev->subsystem_device & 0xff);
1390                 if (ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
1391                     index < QLA_MODEL_NAMES)
1392                         ha->model_desc = qla2x00_model_name[index * 2 + 1];
1393         } else {
1394                 index = (ha->pdev->subsystem_device & 0xff);
1395                 if (ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
1396                     index < QLA_MODEL_NAMES) {
1397                         strcpy(ha->model_number,
1398                             qla2x00_model_name[index * 2]);
1399                         ha->model_desc = qla2x00_model_name[index * 2 + 1];
1400                 } else {
1401                         strcpy(ha->model_number, def);
1402                 }
1403         }
1404 }
1405
1406 /* On sparc systems, obtain port and node WWN from firmware
1407  * properties.
1408  */
1409 static void qla2xxx_nvram_wwn_from_ofw(scsi_qla_host_t *ha, nvram_t *nv)
1410 {
1411 #ifdef CONFIG_SPARC
1412         struct pci_dev *pdev = ha->pdev;
1413         struct device_node *dp = pci_device_to_OF_node(pdev);
1414         const u8 *val;
1415         int len;
1416
1417         val = of_get_property(dp, "port-wwn", &len);
1418         if (val && len >= WWN_SIZE)
1419                 memcpy(nv->port_name, val, WWN_SIZE);
1420
1421         val = of_get_property(dp, "node-wwn", &len);
1422         if (val && len >= WWN_SIZE)
1423                 memcpy(nv->node_name, val, WWN_SIZE);
1424 #endif
1425 }
1426
1427 /*
1428 * NVRAM configuration for ISP 2xxx
1429 *
1430 * Input:
1431 *      ha                = adapter block pointer.
1432 *
1433 * Output:
1434 *      initialization control block in response_ring
1435 *      host adapters parameters in host adapter block
1436 *
1437 * Returns:
1438 *      0 = success.
1439 */
1440 int
1441 qla2x00_nvram_config(scsi_qla_host_t *ha)
1442 {
1443         int             rval;
1444         uint8_t         chksum = 0;
1445         uint16_t        cnt;
1446         uint8_t         *dptr1, *dptr2;
1447         init_cb_t       *icb = ha->init_cb;
1448         nvram_t         *nv = (nvram_t *)ha->request_ring;
1449         uint8_t         *ptr = (uint8_t *)ha->request_ring;
1450         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1451
1452         rval = QLA_SUCCESS;
1453
1454         /* Determine NVRAM starting address. */
1455         ha->nvram_size = sizeof(nvram_t);
1456         ha->nvram_base = 0;
1457         if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha))
1458                 if ((RD_REG_WORD(&reg->ctrl_status) >> 14) == 1)
1459                         ha->nvram_base = 0x80;
1460
1461         /* Get NVRAM data and calculate checksum. */
1462         ha->isp_ops.read_nvram(ha, ptr, ha->nvram_base, ha->nvram_size);
1463         for (cnt = 0, chksum = 0; cnt < ha->nvram_size; cnt++)
1464                 chksum += *ptr++;
1465
1466         DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
1467         DEBUG5(qla2x00_dump_buffer((uint8_t *)ha->request_ring,
1468             ha->nvram_size));
1469
1470         /* Bad NVRAM data, set defaults parameters. */
1471         if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' ||
1472             nv->id[2] != 'P' || nv->id[3] != ' ' || nv->nvram_version < 1) {
1473                 /* Reset NVRAM data. */
1474                 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
1475                     "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
1476                     nv->nvram_version);
1477                 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
1478                     "invalid -- WWPN) defaults.\n");
1479
1480                 /*
1481                  * Set default initialization control block.
1482                  */
1483                 memset(nv, 0, ha->nvram_size);
1484                 nv->parameter_block_version = ICB_VERSION;
1485
1486                 if (IS_QLA23XX(ha)) {
1487                         nv->firmware_options[0] = BIT_2 | BIT_1;
1488                         nv->firmware_options[1] = BIT_7 | BIT_5;
1489                         nv->add_firmware_options[0] = BIT_5;
1490                         nv->add_firmware_options[1] = BIT_5 | BIT_4;
1491                         nv->frame_payload_size = __constant_cpu_to_le16(2048);
1492                         nv->special_options[1] = BIT_7;
1493                 } else if (IS_QLA2200(ha)) {
1494                         nv->firmware_options[0] = BIT_2 | BIT_1;
1495                         nv->firmware_options[1] = BIT_7 | BIT_5;
1496                         nv->add_firmware_options[0] = BIT_5;
1497                         nv->add_firmware_options[1] = BIT_5 | BIT_4;
1498                         nv->frame_payload_size = __constant_cpu_to_le16(1024);
1499                 } else if (IS_QLA2100(ha)) {
1500                         nv->firmware_options[0] = BIT_3 | BIT_1;
1501                         nv->firmware_options[1] = BIT_5;
1502                         nv->frame_payload_size = __constant_cpu_to_le16(1024);
1503                 }
1504
1505                 nv->max_iocb_allocation = __constant_cpu_to_le16(256);
1506                 nv->execution_throttle = __constant_cpu_to_le16(16);
1507                 nv->retry_count = 8;
1508                 nv->retry_delay = 1;
1509
1510                 nv->port_name[0] = 33;
1511                 nv->port_name[3] = 224;
1512                 nv->port_name[4] = 139;
1513
1514                 qla2xxx_nvram_wwn_from_ofw(ha, nv);
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                         qla2x00_set_model_info(ha, nv->model_number,
1568                             sizeof(nv->model_number), "QLA23xx");
1569                 }
1570         } else if (IS_QLA2200(ha)) {
1571                 nv->firmware_options[0] |= BIT_2;
1572                 /*
1573                  * 'Point-to-point preferred, else loop' is not a safe
1574                  * connection mode setting.
1575                  */
1576                 if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
1577                     (BIT_5 | BIT_4)) {
1578                         /* Force 'loop preferred, else point-to-point'. */
1579                         nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
1580                         nv->add_firmware_options[0] |= BIT_5;
1581                 }
1582                 strcpy(ha->model_number, "QLA22xx");
1583         } else /*if (IS_QLA2100(ha))*/ {
1584                 strcpy(ha->model_number, "QLA2100");
1585         }
1586
1587         /*
1588          * Copy over NVRAM RISC parameter block to initialization control block.
1589          */
1590         dptr1 = (uint8_t *)icb;
1591         dptr2 = (uint8_t *)&nv->parameter_block_version;
1592         cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
1593         while (cnt--)
1594                 *dptr1++ = *dptr2++;
1595
1596         /* Copy 2nd half. */
1597         dptr1 = (uint8_t *)icb->add_firmware_options;
1598         cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
1599         while (cnt--)
1600                 *dptr1++ = *dptr2++;
1601
1602         /* Use alternate WWN? */
1603         if (nv->host_p[1] & BIT_7) {
1604                 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
1605                 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
1606         }
1607
1608         /* Prepare nodename */
1609         if ((icb->firmware_options[1] & BIT_6) == 0) {
1610                 /*
1611                  * Firmware will apply the following mask if the nodename was
1612                  * not provided.
1613                  */
1614                 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
1615                 icb->node_name[0] &= 0xF0;
1616         }
1617
1618         /*
1619          * Set host adapter parameters.
1620          */
1621         if (nv->host_p[0] & BIT_7)
1622                 ql2xextended_error_logging = 1;
1623         ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
1624         /* Always load RISC code on non ISP2[12]00 chips. */
1625         if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
1626                 ha->flags.disable_risc_code_load = 0;
1627         ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
1628         ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
1629         ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
1630         ha->flags.enable_led_scheme = (nv->special_options[1] & BIT_4) ? 1 : 0;
1631         ha->flags.disable_serdes = 0;
1632
1633         ha->operating_mode =
1634             (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
1635
1636         memcpy(ha->fw_seriallink_options, nv->seriallink_options,
1637             sizeof(ha->fw_seriallink_options));
1638
1639         /* save HBA serial number */
1640         ha->serial0 = icb->port_name[5];
1641         ha->serial1 = icb->port_name[6];
1642         ha->serial2 = icb->port_name[7];
1643         ha->node_name = icb->node_name;
1644         ha->port_name = icb->port_name;
1645
1646         icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
1647
1648         ha->retry_count = nv->retry_count;
1649
1650         /* Set minimum login_timeout to 4 seconds. */
1651         if (nv->login_timeout < ql2xlogintimeout)
1652                 nv->login_timeout = ql2xlogintimeout;
1653         if (nv->login_timeout < 4)
1654                 nv->login_timeout = 4;
1655         ha->login_timeout = nv->login_timeout;
1656         icb->login_timeout = nv->login_timeout;
1657
1658         /* Set minimum RATOV to 200 tenths of a second. */
1659         ha->r_a_tov = 200;
1660
1661         ha->loop_reset_delay = nv->reset_delay;
1662
1663         /* Link Down Timeout = 0:
1664          *
1665          *      When Port Down timer expires we will start returning
1666          *      I/O's to OS with "DID_NO_CONNECT".
1667          *
1668          * Link Down Timeout != 0:
1669          *
1670          *       The driver waits for the link to come up after link down
1671          *       before returning I/Os to OS with "DID_NO_CONNECT".
1672          */
1673         if (nv->link_down_timeout == 0) {
1674                 ha->loop_down_abort_time =
1675                     (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
1676         } else {
1677                 ha->link_down_timeout =  nv->link_down_timeout;
1678                 ha->loop_down_abort_time =
1679                     (LOOP_DOWN_TIME - ha->link_down_timeout);
1680         }
1681
1682         /*
1683          * Need enough time to try and get the port back.
1684          */
1685         ha->port_down_retry_count = nv->port_down_retry_count;
1686         if (qlport_down_retry)
1687                 ha->port_down_retry_count = qlport_down_retry;
1688         /* Set login_retry_count */
1689         ha->login_retry_count  = nv->retry_count;
1690         if (ha->port_down_retry_count == nv->port_down_retry_count &&
1691             ha->port_down_retry_count > 3)
1692                 ha->login_retry_count = ha->port_down_retry_count;
1693         else if (ha->port_down_retry_count > (int)ha->login_retry_count)
1694                 ha->login_retry_count = ha->port_down_retry_count;
1695         if (ql2xloginretrycount)
1696                 ha->login_retry_count = ql2xloginretrycount;
1697
1698         icb->lun_enables = __constant_cpu_to_le16(0);
1699         icb->command_resource_count = 0;
1700         icb->immediate_notify_resource_count = 0;
1701         icb->timeout = __constant_cpu_to_le16(0);
1702
1703         if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
1704                 /* Enable RIO */
1705                 icb->firmware_options[0] &= ~BIT_3;
1706                 icb->add_firmware_options[0] &=
1707                     ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
1708                 icb->add_firmware_options[0] |= BIT_2;
1709                 icb->response_accumulation_timer = 3;
1710                 icb->interrupt_delay_timer = 5;
1711
1712                 ha->flags.process_response_queue = 1;
1713         } else {
1714                 /* Enable ZIO. */
1715                 if (!ha->flags.init_done) {
1716                         ha->zio_mode = icb->add_firmware_options[0] &
1717                             (BIT_3 | BIT_2 | BIT_1 | BIT_0);
1718                         ha->zio_timer = icb->interrupt_delay_timer ?
1719                             icb->interrupt_delay_timer: 2;
1720                 }
1721                 icb->add_firmware_options[0] &=
1722                     ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
1723                 ha->flags.process_response_queue = 0;
1724                 if (ha->zio_mode != QLA_ZIO_DISABLED) {
1725                         ha->zio_mode = QLA_ZIO_MODE_6;
1726
1727                         DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer "
1728                             "delay (%d us).\n", ha->host_no, ha->zio_mode,
1729                             ha->zio_timer * 100));
1730                         qla_printk(KERN_INFO, ha,
1731                             "ZIO mode %d enabled; timer delay (%d us).\n",
1732                             ha->zio_mode, ha->zio_timer * 100);
1733
1734                         icb->add_firmware_options[0] |= (uint8_t)ha->zio_mode;
1735                         icb->interrupt_delay_timer = (uint8_t)ha->zio_timer;
1736                         ha->flags.process_response_queue = 1;
1737                 }
1738         }
1739
1740         if (rval) {
1741                 DEBUG2_3(printk(KERN_WARNING
1742                     "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
1743         }
1744         return (rval);
1745 }
1746
1747 static void
1748 qla2x00_rport_del(void *data)
1749 {
1750         fc_port_t *fcport = data;
1751         struct fc_rport *rport;
1752         unsigned long flags;
1753
1754         spin_lock_irqsave(&fcport->rport_lock, flags);
1755         rport = fcport->drport;
1756         fcport->drport = NULL;
1757         spin_unlock_irqrestore(&fcport->rport_lock, flags);
1758         if (rport)
1759                 fc_remote_port_delete(rport);
1760 }
1761
1762 /**
1763  * qla2x00_alloc_fcport() - Allocate a generic fcport.
1764  * @ha: HA context
1765  * @flags: allocation flags
1766  *
1767  * Returns a pointer to the allocated fcport, or NULL, if none available.
1768  */
1769 static fc_port_t *
1770 qla2x00_alloc_fcport(scsi_qla_host_t *ha, gfp_t flags)
1771 {
1772         fc_port_t *fcport;
1773
1774         fcport = kmalloc(sizeof(fc_port_t), flags);
1775         if (fcport == NULL)
1776                 return (fcport);
1777
1778         /* Setup fcport template structure. */
1779         memset(fcport, 0, sizeof (fc_port_t));
1780         fcport->ha = ha;
1781         fcport->vp_idx = ha->vp_idx;
1782         fcport->port_type = FCT_UNKNOWN;
1783         fcport->loop_id = FC_NO_LOOP_ID;
1784         atomic_set(&fcport->state, FCS_UNCONFIGURED);
1785         fcport->flags = FCF_RLC_SUPPORT;
1786         fcport->supported_classes = FC_COS_UNSPECIFIED;
1787         spin_lock_init(&fcport->rport_lock);
1788
1789         return (fcport);
1790 }
1791
1792 /*
1793  * qla2x00_configure_loop
1794  *      Updates Fibre Channel Device Database with what is actually on loop.
1795  *
1796  * Input:
1797  *      ha                = adapter block pointer.
1798  *
1799  * Returns:
1800  *      0 = success.
1801  *      1 = error.
1802  *      2 = database was full and device was not configured.
1803  */
1804 static int
1805 qla2x00_configure_loop(scsi_qla_host_t *ha)
1806 {
1807         int  rval;
1808         unsigned long flags, save_flags;
1809
1810         rval = QLA_SUCCESS;
1811
1812         /* Get Initiator ID */
1813         if (test_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags)) {
1814                 rval = qla2x00_configure_hba(ha);
1815                 if (rval != QLA_SUCCESS) {
1816                         DEBUG(printk("scsi(%ld): Unable to configure HBA.\n",
1817                             ha->host_no));
1818                         return (rval);
1819                 }
1820         }
1821
1822         save_flags = flags = ha->dpc_flags;
1823         DEBUG(printk("scsi(%ld): Configure loop -- dpc flags =0x%lx\n",
1824             ha->host_no, flags));
1825
1826         /*
1827          * If we have both an RSCN and PORT UPDATE pending then handle them
1828          * both at the same time.
1829          */
1830         clear_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1831         clear_bit(RSCN_UPDATE, &ha->dpc_flags);
1832
1833         /* Determine what we need to do */
1834         if (ha->current_topology == ISP_CFG_FL &&
1835             (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1836
1837                 ha->flags.rscn_queue_overflow = 1;
1838                 set_bit(RSCN_UPDATE, &flags);
1839
1840         } else if (ha->current_topology == ISP_CFG_F &&
1841             (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1842
1843                 ha->flags.rscn_queue_overflow = 1;
1844                 set_bit(RSCN_UPDATE, &flags);
1845                 clear_bit(LOCAL_LOOP_UPDATE, &flags);
1846
1847         } else if (ha->current_topology == ISP_CFG_N) {
1848                 clear_bit(RSCN_UPDATE, &flags);
1849
1850         } else if (!ha->flags.online ||
1851             (test_bit(ABORT_ISP_ACTIVE, &flags))) {
1852
1853                 ha->flags.rscn_queue_overflow = 1;
1854                 set_bit(RSCN_UPDATE, &flags);
1855                 set_bit(LOCAL_LOOP_UPDATE, &flags);
1856         }
1857
1858         if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
1859                 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1860                         rval = QLA_FUNCTION_FAILED;
1861                 } else {
1862                         rval = qla2x00_configure_local_loop(ha);
1863                 }
1864         }
1865
1866         if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
1867                 if (LOOP_TRANSITION(ha)) {
1868                         rval = QLA_FUNCTION_FAILED;
1869                 } else {
1870                         rval = qla2x00_configure_fabric(ha);
1871                 }
1872         }
1873
1874         if (rval == QLA_SUCCESS) {
1875                 if (atomic_read(&ha->loop_down_timer) ||
1876                     test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1877                         rval = QLA_FUNCTION_FAILED;
1878                 } else {
1879                         atomic_set(&ha->loop_state, LOOP_READY);
1880
1881                         DEBUG(printk("scsi(%ld): LOOP READY\n", ha->host_no));
1882                 }
1883         }
1884
1885         if (rval) {
1886                 DEBUG2_3(printk("%s(%ld): *** FAILED ***\n",
1887                     __func__, ha->host_no));
1888         } else {
1889                 DEBUG3(printk("%s: exiting normally\n", __func__));
1890         }
1891
1892         /* Restore state if a resync event occured during processing */
1893         if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1894                 if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
1895                         set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1896                 if (test_bit(RSCN_UPDATE, &save_flags))
1897                         set_bit(RSCN_UPDATE, &ha->dpc_flags);
1898         }
1899
1900         return (rval);
1901 }
1902
1903
1904
1905 /*
1906  * qla2x00_configure_local_loop
1907  *      Updates Fibre Channel Device Database with local loop devices.
1908  *
1909  * Input:
1910  *      ha = adapter block pointer.
1911  *
1912  * Returns:
1913  *      0 = success.
1914  */
1915 static int
1916 qla2x00_configure_local_loop(scsi_qla_host_t *ha)
1917 {
1918         int             rval, rval2;
1919         int             found_devs;
1920         int             found;
1921         fc_port_t       *fcport, *new_fcport;
1922
1923         uint16_t        index;
1924         uint16_t        entries;
1925         char            *id_iter;
1926         uint16_t        loop_id;
1927         uint8_t         domain, area, al_pa;
1928         scsi_qla_host_t *pha = to_qla_parent(ha);
1929
1930         found_devs = 0;
1931         new_fcport = NULL;
1932         entries = MAX_FIBRE_DEVICES;
1933
1934         DEBUG3(printk("scsi(%ld): Getting FCAL position map\n", ha->host_no));
1935         DEBUG3(qla2x00_get_fcal_position_map(ha, NULL));
1936
1937         /* Get list of logged in devices. */
1938         memset(ha->gid_list, 0, GID_LIST_SIZE);
1939         rval = qla2x00_get_id_list(ha, ha->gid_list, ha->gid_list_dma,
1940             &entries);
1941         if (rval != QLA_SUCCESS)
1942                 goto cleanup_allocation;
1943
1944         DEBUG3(printk("scsi(%ld): Entries in ID list (%d)\n",
1945             ha->host_no, entries));
1946         DEBUG3(qla2x00_dump_buffer((uint8_t *)ha->gid_list,
1947             entries * sizeof(struct gid_list_info)));
1948
1949         /* Allocate temporary fcport for any new fcports discovered. */
1950         new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
1951         if (new_fcport == NULL) {
1952                 rval = QLA_MEMORY_ALLOC_FAILED;
1953                 goto cleanup_allocation;
1954         }
1955         new_fcport->flags &= ~FCF_FABRIC_DEVICE;
1956
1957         /*
1958          * Mark local devices that were present with FCF_DEVICE_LOST for now.
1959          */
1960         list_for_each_entry(fcport, &pha->fcports, list) {
1961                 if (fcport->vp_idx != ha->vp_idx)
1962                         continue;
1963
1964                 if (atomic_read(&fcport->state) == FCS_ONLINE &&
1965                     fcport->port_type != FCT_BROADCAST &&
1966                     (fcport->flags & FCF_FABRIC_DEVICE) == 0) {
1967
1968                         DEBUG(printk("scsi(%ld): Marking port lost, "
1969                             "loop_id=0x%04x\n",
1970                             ha->host_no, fcport->loop_id));
1971
1972                         atomic_set(&fcport->state, FCS_DEVICE_LOST);
1973                         fcport->flags &= ~FCF_FARP_DONE;
1974                 }
1975         }
1976
1977         /* Add devices to port list. */
1978         id_iter = (char *)ha->gid_list;
1979         for (index = 0; index < entries; index++) {
1980                 domain = ((struct gid_list_info *)id_iter)->domain;
1981                 area = ((struct gid_list_info *)id_iter)->area;
1982                 al_pa = ((struct gid_list_info *)id_iter)->al_pa;
1983                 if (IS_QLA2100(ha) || IS_QLA2200(ha))
1984                         loop_id = (uint16_t)
1985                             ((struct gid_list_info *)id_iter)->loop_id_2100;
1986                 else
1987                         loop_id = le16_to_cpu(
1988                             ((struct gid_list_info *)id_iter)->loop_id);
1989                 id_iter += ha->gid_list_info_size;
1990
1991                 /* Bypass reserved domain fields. */
1992                 if ((domain & 0xf0) == 0xf0)
1993                         continue;
1994
1995                 /* Bypass if not same domain and area of adapter. */
1996                 if (area && domain &&
1997                     (area != ha->d_id.b.area || domain != ha->d_id.b.domain))
1998                         continue;
1999
2000                 /* Bypass invalid local loop ID. */
2001                 if (loop_id > LAST_LOCAL_LOOP_ID)
2002                         continue;
2003
2004                 /* Fill in member data. */
2005                 new_fcport->d_id.b.domain = domain;
2006                 new_fcport->d_id.b.area = area;
2007                 new_fcport->d_id.b.al_pa = al_pa;
2008                 new_fcport->loop_id = loop_id;
2009                 new_fcport->vp_idx = ha->vp_idx;
2010                 rval2 = qla2x00_get_port_database(ha, new_fcport, 0);
2011                 if (rval2 != QLA_SUCCESS) {
2012                         DEBUG2(printk("scsi(%ld): Failed to retrieve fcport "
2013                             "information -- get_port_database=%x, "
2014                             "loop_id=0x%04x\n",
2015                             ha->host_no, rval2, new_fcport->loop_id));
2016                         DEBUG2(printk("scsi(%ld): Scheduling resync...\n",
2017                             ha->host_no));
2018                         set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
2019                         continue;
2020                 }
2021
2022                 /* Check for matching device in port list. */
2023                 found = 0;
2024                 fcport = NULL;
2025                 list_for_each_entry(fcport, &pha->fcports, list) {
2026                         if (fcport->vp_idx != ha->vp_idx)
2027                                 continue;
2028
2029                         if (memcmp(new_fcport->port_name, fcport->port_name,
2030                             WWN_SIZE))
2031                                 continue;
2032
2033                         fcport->flags &= ~(FCF_FABRIC_DEVICE |
2034                             FCF_PERSISTENT_BOUND);
2035                         fcport->loop_id = new_fcport->loop_id;
2036                         fcport->port_type = new_fcport->port_type;
2037                         fcport->d_id.b24 = new_fcport->d_id.b24;
2038                         memcpy(fcport->node_name, new_fcport->node_name,
2039                             WWN_SIZE);
2040
2041                         found++;
2042                         break;
2043                 }
2044
2045                 if (!found) {
2046                         /* New device, add to fcports list. */
2047                         new_fcport->flags &= ~FCF_PERSISTENT_BOUND;
2048                         if (ha->parent) {
2049                                 new_fcport->ha = ha;
2050                                 new_fcport->vp_idx = ha->vp_idx;
2051                                 list_add_tail(&new_fcport->vp_fcport,
2052                                     &ha->vp_fcports);
2053                         }
2054                         list_add_tail(&new_fcport->list, &pha->fcports);
2055
2056                         /* Allocate a new replacement fcport. */
2057                         fcport = new_fcport;
2058                         new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2059                         if (new_fcport == NULL) {
2060                                 rval = QLA_MEMORY_ALLOC_FAILED;
2061                                 goto cleanup_allocation;
2062                         }
2063                         new_fcport->flags &= ~FCF_FABRIC_DEVICE;
2064                 }
2065
2066                 /* Base iIDMA settings on HBA port speed. */
2067                 switch (ha->link_data_rate) {
2068                 case PORT_SPEED_1GB:
2069                         fcport->fp_speed = cpu_to_be16(BIT_15);
2070                         break;
2071                 case PORT_SPEED_2GB:
2072                         fcport->fp_speed = cpu_to_be16(BIT_14);
2073                         break;
2074                 case PORT_SPEED_4GB:
2075                         fcport->fp_speed = cpu_to_be16(BIT_13);
2076                         break;
2077                 }
2078
2079                 qla2x00_update_fcport(ha, fcport);
2080
2081                 found_devs++;
2082         }
2083
2084 cleanup_allocation:
2085         kfree(new_fcport);
2086
2087         if (rval != QLA_SUCCESS) {
2088                 DEBUG2(printk("scsi(%ld): Configure local loop error exit: "
2089                     "rval=%x\n", ha->host_no, rval));
2090         }
2091
2092         if (found_devs) {
2093                 ha->device_flags |= DFLG_LOCAL_DEVICES;
2094                 ha->device_flags &= ~DFLG_RETRY_LOCAL_DEVICES;
2095         }
2096
2097         return (rval);
2098 }
2099
2100 static void
2101 qla2x00_probe_for_all_luns(scsi_qla_host_t *ha)
2102 {
2103         fc_port_t       *fcport;
2104
2105         qla2x00_mark_all_devices_lost(ha, 0);
2106         list_for_each_entry(fcport, &ha->fcports, list) {
2107                 if (fcport->port_type != FCT_TARGET)
2108                         continue;
2109
2110                 qla2x00_update_fcport(ha, fcport);
2111         }
2112 }
2113
2114 static void
2115 qla2x00_iidma_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
2116 {
2117 #define LS_UNKNOWN      2
2118         static char *link_speeds[5] = { "1", "2", "?", "4" };
2119         int rval;
2120         uint16_t port_speed, mb[6];
2121
2122         if (!IS_QLA24XX(ha))
2123                 return;
2124
2125         switch (be16_to_cpu(fcport->fp_speed)) {
2126         case BIT_15:
2127                 port_speed = PORT_SPEED_1GB;
2128                 break;
2129         case BIT_14:
2130                 port_speed = PORT_SPEED_2GB;
2131                 break;
2132         case BIT_13:
2133                 port_speed = PORT_SPEED_4GB;
2134                 break;
2135         default:
2136                 DEBUG2(printk("scsi(%ld): %02x%02x%02x%02x%02x%02x%02x%02x -- "
2137                     "unsupported FM port operating speed (%04x).\n",
2138                     ha->host_no, fcport->port_name[0], fcport->port_name[1],
2139                     fcport->port_name[2], fcport->port_name[3],
2140                     fcport->port_name[4], fcport->port_name[5],
2141                     fcport->port_name[6], fcport->port_name[7],
2142                     be16_to_cpu(fcport->fp_speed)));
2143                 port_speed = PORT_SPEED_UNKNOWN;
2144                 break;
2145         }
2146         if (port_speed == PORT_SPEED_UNKNOWN)
2147                 return;
2148
2149         rval = qla2x00_set_idma_speed(ha, fcport->loop_id, port_speed, mb);
2150         if (rval != QLA_SUCCESS) {
2151                 DEBUG2(printk("scsi(%ld): Unable to adjust iIDMA "
2152                     "%02x%02x%02x%02x%02x%02x%02x%02x -- %04x %x %04x %04x.\n",
2153                     ha->host_no, fcport->port_name[0], fcport->port_name[1],
2154                     fcport->port_name[2], fcport->port_name[3],
2155                     fcport->port_name[4], fcport->port_name[5],
2156                     fcport->port_name[6], fcport->port_name[7], rval,
2157                     port_speed, mb[0], mb[1]));
2158         } else {
2159                 DEBUG2(qla_printk(KERN_INFO, ha,
2160                     "iIDMA adjusted to %s GB/s on "
2161                     "%02x%02x%02x%02x%02x%02x%02x%02x.\n",
2162                     link_speeds[port_speed], fcport->port_name[0],
2163                     fcport->port_name[1], fcport->port_name[2],
2164                     fcport->port_name[3], fcport->port_name[4],
2165                     fcport->port_name[5], fcport->port_name[6],
2166                     fcport->port_name[7]));
2167         }
2168 }
2169
2170 static void
2171 qla2x00_reg_remote_port(scsi_qla_host_t *ha, fc_port_t *fcport)
2172 {
2173         struct fc_rport_identifiers rport_ids;
2174         struct fc_rport *rport;
2175         unsigned long flags;
2176
2177         if (fcport->drport)
2178                 qla2x00_rport_del(fcport);
2179         if (fcport->rport)
2180                 return;
2181
2182         rport_ids.node_name = wwn_to_u64(fcport->node_name);
2183         rport_ids.port_name = wwn_to_u64(fcport->port_name);
2184         rport_ids.port_id = fcport->d_id.b.domain << 16 |
2185             fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
2186         rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
2187         rport = fc_remote_port_add(ha->host, 0, &rport_ids);
2188         if (!rport) {
2189                 qla_printk(KERN_WARNING, ha,
2190                     "Unable to allocate fc remote port!\n");
2191                 return;
2192         }
2193         spin_lock_irqsave(&fcport->rport_lock, flags);
2194         fcport->rport = rport;
2195         *((fc_port_t **)rport->dd_data) = fcport;
2196         spin_unlock_irqrestore(&fcport->rport_lock, flags);
2197
2198         rport->supported_classes = fcport->supported_classes;
2199
2200         rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
2201         if (fcport->port_type == FCT_INITIATOR)
2202                 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2203         if (fcport->port_type == FCT_TARGET)
2204                 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
2205         fc_remote_port_rolechg(rport, rport_ids.roles);
2206
2207         if (rport->scsi_target_id != -1 &&
2208             rport->scsi_target_id < ha->host->max_id)
2209                 fcport->os_target_id = rport->scsi_target_id;
2210 }
2211
2212 /*
2213  * qla2x00_update_fcport
2214  *      Updates device on list.
2215  *
2216  * Input:
2217  *      ha = adapter block pointer.
2218  *      fcport = port structure pointer.
2219  *
2220  * Return:
2221  *      0  - Success
2222  *  BIT_0 - error
2223  *
2224  * Context:
2225  *      Kernel context.
2226  */
2227 void
2228 qla2x00_update_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
2229 {
2230         scsi_qla_host_t *pha = to_qla_parent(ha);
2231
2232         fcport->ha = ha;
2233         fcport->login_retry = 0;
2234         fcport->port_login_retry_count = pha->port_down_retry_count *
2235             PORT_RETRY_TIME;
2236         atomic_set(&fcport->port_down_timer, pha->port_down_retry_count *
2237             PORT_RETRY_TIME);
2238         fcport->flags &= ~FCF_LOGIN_NEEDED;
2239
2240         qla2x00_iidma_fcport(ha, fcport);
2241
2242         atomic_set(&fcport->state, FCS_ONLINE);
2243
2244         qla2x00_reg_remote_port(ha, fcport);
2245 }
2246
2247 /*
2248  * qla2x00_configure_fabric
2249  *      Setup SNS devices with loop ID's.
2250  *
2251  * Input:
2252  *      ha = adapter block pointer.
2253  *
2254  * Returns:
2255  *      0 = success.
2256  *      BIT_0 = error
2257  */
2258 static int
2259 qla2x00_configure_fabric(scsi_qla_host_t *ha)
2260 {
2261         int     rval, rval2;
2262         fc_port_t       *fcport, *fcptemp;
2263         uint16_t        next_loopid;
2264         uint16_t        mb[MAILBOX_REGISTER_COUNT];
2265         uint16_t        loop_id;
2266         LIST_HEAD(new_fcports);
2267         scsi_qla_host_t *pha = to_qla_parent(ha);
2268
2269         /* If FL port exists, then SNS is present */
2270         if (IS_QLA24XX(ha) || IS_QLA54XX(ha))
2271                 loop_id = NPH_F_PORT;
2272         else
2273                 loop_id = SNS_FL_PORT;
2274         rval = qla2x00_get_port_name(ha, loop_id, ha->fabric_node_name, 1);
2275         if (rval != QLA_SUCCESS) {
2276                 DEBUG2(printk("scsi(%ld): MBC_GET_PORT_NAME Failed, No FL "
2277                     "Port\n", ha->host_no));
2278
2279                 ha->device_flags &= ~SWITCH_FOUND;
2280                 return (QLA_SUCCESS);
2281         }
2282         ha->device_flags |= SWITCH_FOUND;
2283
2284         /* Mark devices that need re-synchronization. */
2285         rval2 = qla2x00_device_resync(ha);
2286         if (rval2 == QLA_RSCNS_HANDLED) {
2287                 /* No point doing the scan, just continue. */
2288                 return (QLA_SUCCESS);
2289         }
2290         do {
2291                 /* FDMI support. */
2292                 if (ql2xfdmienable &&
2293                     test_and_clear_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags))
2294                         qla2x00_fdmi_register(ha);
2295
2296                 /* Ensure we are logged into the SNS. */
2297                 if (IS_QLA24XX(ha) || IS_QLA54XX(ha))
2298                         loop_id = NPH_SNS;
2299                 else
2300                         loop_id = SIMPLE_NAME_SERVER;
2301                 ha->isp_ops.fabric_login(ha, loop_id, 0xff, 0xff,
2302                     0xfc, mb, BIT_1 | BIT_0);
2303                 if (mb[0] != MBS_COMMAND_COMPLETE) {
2304                         DEBUG2(qla_printk(KERN_INFO, ha,
2305                             "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
2306                             "mb[2]=%x mb[6]=%x mb[7]=%x\n", loop_id,
2307                             mb[0], mb[1], mb[2], mb[6], mb[7]));
2308                         return (QLA_SUCCESS);
2309                 }
2310
2311                 if (test_and_clear_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags)) {
2312                         if (qla2x00_rft_id(ha)) {
2313                                 /* EMPTY */
2314                                 DEBUG2(printk("scsi(%ld): Register FC-4 "
2315                                     "TYPE failed.\n", ha->host_no));
2316                         }
2317                         if (qla2x00_rff_id(ha)) {
2318                                 /* EMPTY */
2319                                 DEBUG2(printk("scsi(%ld): Register FC-4 "
2320                                     "Features failed.\n", ha->host_no));
2321                         }
2322                         if (qla2x00_rnn_id(ha)) {
2323                                 /* EMPTY */
2324                                 DEBUG2(printk("scsi(%ld): Register Node Name "
2325                                     "failed.\n", ha->host_no));
2326                         } else if (qla2x00_rsnn_nn(ha)) {
2327                                 /* EMPTY */
2328                                 DEBUG2(printk("scsi(%ld): Register Symbolic "
2329                                     "Node Name failed.\n", ha->host_no));
2330                         }
2331                 }
2332
2333                 rval = qla2x00_find_all_fabric_devs(ha, &new_fcports);
2334                 if (rval != QLA_SUCCESS)
2335                         break;
2336
2337                 /*
2338                  * Logout all previous fabric devices marked lost, except
2339                  * tape devices.
2340                  */
2341                 list_for_each_entry(fcport, &pha->fcports, list) {
2342                         if (fcport->vp_idx !=ha->vp_idx)
2343                                 continue;
2344
2345                         if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2346                                 break;
2347
2348                         if ((fcport->flags & FCF_FABRIC_DEVICE) == 0)
2349                                 continue;
2350
2351                         if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
2352                                 qla2x00_mark_device_lost(ha, fcport,
2353                                     ql2xplogiabsentdevice, 0);
2354                                 if (fcport->loop_id != FC_NO_LOOP_ID &&
2355                                     (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2356                                     fcport->port_type != FCT_INITIATOR &&
2357                                     fcport->port_type != FCT_BROADCAST) {
2358                                         ha->isp_ops.fabric_logout(ha,
2359                                             fcport->loop_id,
2360                                             fcport->d_id.b.domain,
2361                                             fcport->d_id.b.area,
2362                                             fcport->d_id.b.al_pa);
2363                                         fcport->loop_id = FC_NO_LOOP_ID;
2364                                 }
2365                         }
2366                 }
2367
2368                 /* Starting free loop ID. */
2369                 next_loopid = pha->min_external_loopid;
2370
2371                 /*
2372                  * Scan through our port list and login entries that need to be
2373                  * logged in.
2374                  */
2375                 list_for_each_entry(fcport, &pha->fcports, list) {
2376                         if (fcport->vp_idx != ha->vp_idx)
2377                                 continue;
2378
2379                         if (atomic_read(&ha->loop_down_timer) ||
2380                             test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2381                                 break;
2382
2383                         if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2384                             (fcport->flags & FCF_LOGIN_NEEDED) == 0)
2385                                 continue;
2386
2387                         if (fcport->loop_id == FC_NO_LOOP_ID) {
2388                                 fcport->loop_id = next_loopid;
2389                                 rval = qla2x00_find_new_loop_id(ha, fcport);
2390                                 if (rval != QLA_SUCCESS) {
2391                                         /* Ran out of IDs to use */
2392                                         break;
2393                                 }
2394                         }
2395                         /* Login and update database */
2396                         qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
2397                 }
2398
2399                 /* Exit if out of loop IDs. */
2400                 if (rval != QLA_SUCCESS) {
2401                         break;
2402                 }
2403
2404                 /*
2405                  * Login and add the new devices to our port list.
2406                  */
2407                 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2408                         if (atomic_read(&ha->loop_down_timer) ||
2409                             test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2410                                 break;
2411
2412                         /* Find a new loop ID to use. */
2413                         fcport->loop_id = next_loopid;
2414                         rval = qla2x00_find_new_loop_id(ha, fcport);
2415                         if (rval != QLA_SUCCESS) {
2416                                 /* Ran out of IDs to use */
2417                                 break;
2418                         }
2419
2420                         /* Login and update database */
2421                         qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
2422
2423                         if (ha->parent) {
2424                                 fcport->ha = ha;
2425                                 fcport->vp_idx = ha->vp_idx;
2426                                 list_add_tail(&fcport->vp_fcport,
2427                                     &ha->vp_fcports);
2428                                 list_move_tail(&fcport->list,
2429                                     &ha->parent->fcports);
2430                         } else
2431                                 list_move_tail(&fcport->list, &ha->fcports);
2432                 }
2433         } while (0);
2434
2435         /* Free all new device structures not processed. */
2436         list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2437                 list_del(&fcport->list);
2438                 kfree(fcport);
2439         }
2440
2441         if (rval) {
2442                 DEBUG2(printk("scsi(%ld): Configure fabric error exit: "
2443                     "rval=%d\n", ha->host_no, rval));
2444         }
2445
2446         return (rval);
2447 }
2448
2449
2450 /*
2451  * qla2x00_find_all_fabric_devs
2452  *
2453  * Input:
2454  *      ha = adapter block pointer.
2455  *      dev = database device entry pointer.
2456  *
2457  * Returns:
2458  *      0 = success.
2459  *
2460  * Context:
2461  *      Kernel context.
2462  */
2463 static int
2464 qla2x00_find_all_fabric_devs(scsi_qla_host_t *ha, struct list_head *new_fcports)
2465 {
2466         int             rval;
2467         uint16_t        loop_id;
2468         fc_port_t       *fcport, *new_fcport, *fcptemp;
2469         int             found;
2470
2471         sw_info_t       *swl;
2472         int             swl_idx;
2473         int             first_dev, last_dev;
2474         port_id_t       wrap, nxt_d_id;
2475         int             vp_index;
2476         int             empty_vp_index;
2477         int             found_vp;
2478         scsi_qla_host_t *vha;
2479         scsi_qla_host_t *pha = to_qla_parent(ha);
2480
2481         rval = QLA_SUCCESS;
2482
2483         /* Try GID_PT to get device list, else GAN. */
2484         swl = kmalloc(sizeof(sw_info_t) * MAX_FIBRE_DEVICES, GFP_ATOMIC);
2485         if (swl == NULL) {
2486                 /*EMPTY*/
2487                 DEBUG2(printk("scsi(%ld): GID_PT allocations failed, fallback "
2488                     "on GA_NXT\n", ha->host_no));
2489         } else {
2490                 memset(swl, 0, sizeof(sw_info_t) * MAX_FIBRE_DEVICES);
2491                 if (qla2x00_gid_pt(ha, swl) != QLA_SUCCESS) {
2492                         kfree(swl);
2493                         swl = NULL;
2494                 } else if (qla2x00_gpn_id(ha, swl) != QLA_SUCCESS) {
2495                         kfree(swl);
2496                         swl = NULL;
2497                 } else if (qla2x00_gnn_id(ha, swl) != QLA_SUCCESS) {
2498                         kfree(swl);
2499                         swl = NULL;
2500                 } else if (qla2x00_gfpn_id(ha, swl) == QLA_SUCCESS) {
2501                         qla2x00_gpsc(ha, swl);
2502                 }
2503         }
2504         swl_idx = 0;
2505
2506         /* Allocate temporary fcport for any new fcports discovered. */
2507         new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2508         if (new_fcport == NULL) {
2509                 kfree(swl);
2510                 return (QLA_MEMORY_ALLOC_FAILED);
2511         }
2512         new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2513         new_fcport->vp_idx = ha->vp_idx;
2514         /* Set start port ID scan at adapter ID. */
2515         first_dev = 1;
2516         last_dev = 0;
2517
2518         /* Starting free loop ID. */
2519         loop_id = pha->min_external_loopid;
2520         for (; loop_id <= ha->last_loop_id; loop_id++) {
2521                 if (qla2x00_is_reserved_id(ha, loop_id))
2522                         continue;
2523
2524                 if (atomic_read(&ha->loop_down_timer) || LOOP_TRANSITION(ha))
2525                         break;
2526
2527                 if (swl != NULL) {
2528                         if (last_dev) {
2529                                 wrap.b24 = new_fcport->d_id.b24;
2530                         } else {
2531                                 new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
2532                                 memcpy(new_fcport->node_name,
2533                                     swl[swl_idx].node_name, WWN_SIZE);
2534                                 memcpy(new_fcport->port_name,
2535                                     swl[swl_idx].port_name, WWN_SIZE);
2536                                 memcpy(new_fcport->fabric_port_name,
2537                                     swl[swl_idx].fabric_port_name, WWN_SIZE);
2538                                 new_fcport->fp_speed = swl[swl_idx].fp_speed;
2539
2540                                 if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
2541                                         last_dev = 1;
2542                                 }
2543                                 swl_idx++;
2544                         }
2545                 } else {
2546                         /* Send GA_NXT to the switch */
2547                         rval = qla2x00_ga_nxt(ha, new_fcport);
2548                         if (rval != QLA_SUCCESS) {
2549                                 qla_printk(KERN_WARNING, ha,
2550                                     "SNS scan failed -- assuming zero-entry "
2551                                     "result...\n");
2552                                 list_for_each_entry_safe(fcport, fcptemp,
2553                                     new_fcports, list) {
2554                                         list_del(&fcport->list);
2555                                         kfree(fcport);
2556                                 }
2557                                 rval = QLA_SUCCESS;
2558                                 break;
2559                         }
2560                 }
2561
2562                 /* If wrap on switch device list, exit. */
2563                 if (first_dev) {
2564                         wrap.b24 = new_fcport->d_id.b24;
2565                         first_dev = 0;
2566                 } else if (new_fcport->d_id.b24 == wrap.b24) {
2567                         DEBUG2(printk("scsi(%ld): device wrap (%02x%02x%02x)\n",
2568                             ha->host_no, new_fcport->d_id.b.domain,
2569                             new_fcport->d_id.b.area, new_fcport->d_id.b.al_pa));
2570                         break;
2571                 }
2572
2573                 /* Bypass if same physical adapter. */
2574                 if (new_fcport->d_id.b24 == pha->d_id.b24)
2575                         continue;
2576
2577                 /* Bypass virtual ports of the same host. */
2578                 if (pha->num_vhosts) {
2579                         vp_index = find_next_bit(
2580                             (unsigned long *)pha->vp_idx_map,
2581                             MAX_MULTI_ID_FABRIC + 1, 1);
2582
2583                         for (;vp_index <= MAX_MULTI_ID_FABRIC;
2584                             vp_index = find_next_bit(
2585                             (unsigned long *)pha->vp_idx_map,
2586                             MAX_MULTI_ID_FABRIC + 1, vp_index + 1)) {
2587                                 empty_vp_index = 1;
2588                                 found_vp = 0;
2589                                 list_for_each_entry(vha, &pha->vp_list,
2590                                     vp_list) {
2591                                         if (vp_index == vha->vp_idx) {
2592                                                 empty_vp_index = 0;
2593                                                 found_vp = 1;
2594                                                 break;
2595                                         }
2596                                 }
2597
2598                                 if (empty_vp_index)
2599                                         continue;
2600
2601                                 if (found_vp &&
2602                                     new_fcport->d_id.b24 == vha->d_id.b24)
2603                                         break;
2604                         }
2605                         if (vp_index <= MAX_MULTI_ID_FABRIC)
2606                                 continue;
2607                 }
2608
2609                 /* Bypass if same domain and area of adapter. */
2610                 if (((new_fcport->d_id.b24 & 0xffff00) ==
2611                     (ha->d_id.b24 & 0xffff00)) && ha->current_topology ==
2612                         ISP_CFG_FL)
2613                             continue;
2614
2615                 /* Bypass reserved domain fields. */
2616                 if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
2617                         continue;
2618
2619                 /* Locate matching device in database. */
2620                 found = 0;
2621                 list_for_each_entry(fcport, &pha->fcports, list) {
2622                         if (new_fcport->vp_idx != fcport->vp_idx)
2623                                 continue;
2624                         if (memcmp(new_fcport->port_name, fcport->port_name,
2625                             WWN_SIZE))
2626                                 continue;
2627
2628                         found++;
2629
2630                         /* Update port state. */
2631                         memcpy(fcport->fabric_port_name,
2632                             new_fcport->fabric_port_name, WWN_SIZE);
2633                         fcport->fp_speed = new_fcport->fp_speed;
2634
2635                         /*
2636                          * If address the same and state FCS_ONLINE, nothing
2637                          * changed.
2638                          */
2639                         if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
2640                             atomic_read(&fcport->state) == FCS_ONLINE) {
2641                                 break;
2642                         }
2643
2644                         /*
2645                          * If device was not a fabric device before.
2646                          */
2647                         if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
2648                                 fcport->d_id.b24 = new_fcport->d_id.b24;
2649                                 fcport->loop_id = FC_NO_LOOP_ID;
2650                                 fcport->flags |= (FCF_FABRIC_DEVICE |
2651                                     FCF_LOGIN_NEEDED);
2652                                 fcport->flags &= ~FCF_PERSISTENT_BOUND;
2653                                 break;
2654                         }
2655
2656                         /*
2657                          * Port ID changed or device was marked to be updated;
2658                          * Log it out if still logged in and mark it for
2659                          * relogin later.
2660                          */
2661                         fcport->d_id.b24 = new_fcport->d_id.b24;
2662                         fcport->flags |= FCF_LOGIN_NEEDED;
2663                         if (fcport->loop_id != FC_NO_LOOP_ID &&
2664                             (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2665                             fcport->port_type != FCT_INITIATOR &&
2666                             fcport->port_type != FCT_BROADCAST) {
2667                                 ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2668                                     fcport->d_id.b.domain, fcport->d_id.b.area,
2669                                     fcport->d_id.b.al_pa);
2670                                 fcport->loop_id = FC_NO_LOOP_ID;
2671                         }
2672
2673                         break;
2674                 }
2675
2676                 if (found)
2677                         continue;
2678
2679                 /* If device was not in our fcports list, then add it. */
2680                 list_add_tail(&new_fcport->list, new_fcports);
2681
2682                 /* Allocate a new replacement fcport. */
2683                 nxt_d_id.b24 = new_fcport->d_id.b24;
2684                 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2685                 if (new_fcport == NULL) {
2686                         kfree(swl);
2687                         return (QLA_MEMORY_ALLOC_FAILED);
2688                 }
2689                 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2690                 new_fcport->d_id.b24 = nxt_d_id.b24;
2691                 new_fcport->vp_idx = ha->vp_idx;
2692         }
2693
2694         kfree(swl);
2695         kfree(new_fcport);
2696
2697         if (!list_empty(new_fcports))
2698                 ha->device_flags |= DFLG_FABRIC_DEVICES;
2699
2700         return (rval);
2701 }
2702
2703 /*
2704  * qla2x00_find_new_loop_id
2705  *      Scan through our port list and find a new usable loop ID.
2706  *
2707  * Input:
2708  *      ha:     adapter state pointer.
2709  *      dev:    port structure pointer.
2710  *
2711  * Returns:
2712  *      qla2x00 local function return status code.
2713  *
2714  * Context:
2715  *      Kernel context.
2716  */
2717 static int
2718 qla2x00_find_new_loop_id(scsi_qla_host_t *ha, fc_port_t *dev)
2719 {
2720         int     rval;
2721         int     found;
2722         fc_port_t *fcport;
2723         uint16_t first_loop_id;
2724         scsi_qla_host_t *pha = to_qla_parent(ha);
2725
2726         rval = QLA_SUCCESS;
2727
2728         /* Save starting loop ID. */
2729         first_loop_id = dev->loop_id;
2730
2731         for (;;) {
2732                 /* Skip loop ID if already used by adapter. */
2733                 if (dev->loop_id == ha->loop_id) {
2734                         dev->loop_id++;
2735                 }
2736
2737                 /* Skip reserved loop IDs. */
2738                 while (qla2x00_is_reserved_id(ha, dev->loop_id)) {
2739                         dev->loop_id++;
2740                 }
2741
2742                 /* Reset loop ID if passed the end. */
2743                 if (dev->loop_id > ha->last_loop_id) {
2744                         /* first loop ID. */
2745                         dev->loop_id = ha->min_external_loopid;
2746                 }
2747
2748                 /* Check for loop ID being already in use. */
2749                 found = 0;
2750                 fcport = NULL;
2751                 list_for_each_entry(fcport, &pha->fcports, list) {
2752                         if (fcport->loop_id == dev->loop_id && fcport != dev) {
2753                                 /* ID possibly in use */
2754                                 found++;
2755                                 break;
2756                         }
2757                 }
2758
2759                 /* If not in use then it is free to use. */
2760                 if (!found) {
2761                         break;
2762                 }
2763
2764                 /* ID in use. Try next value. */
2765                 dev->loop_id++;
2766
2767                 /* If wrap around. No free ID to use. */
2768                 if (dev->loop_id == first_loop_id) {
2769                         dev->loop_id = FC_NO_LOOP_ID;
2770                         rval = QLA_FUNCTION_FAILED;
2771                         break;
2772                 }
2773         }
2774
2775         return (rval);
2776 }
2777
2778 /*
2779  * qla2x00_device_resync
2780  *      Marks devices in the database that needs resynchronization.
2781  *
2782  * Input:
2783  *      ha = adapter block pointer.
2784  *
2785  * Context:
2786  *      Kernel context.
2787  */
2788 static int
2789 qla2x00_device_resync(scsi_qla_host_t *ha)
2790 {
2791         int     rval;
2792         uint32_t mask;
2793         fc_port_t *fcport;
2794         uint32_t rscn_entry;
2795         uint8_t rscn_out_iter;
2796         uint8_t format;
2797         port_id_t d_id;
2798         scsi_qla_host_t *pha = to_qla_parent(ha);
2799
2800         rval = QLA_RSCNS_HANDLED;
2801
2802         while (ha->rscn_out_ptr != ha->rscn_in_ptr ||
2803             ha->flags.rscn_queue_overflow) {
2804
2805                 rscn_entry = ha->rscn_queue[ha->rscn_out_ptr];
2806                 format = MSB(MSW(rscn_entry));
2807                 d_id.b.domain = LSB(MSW(rscn_entry));
2808                 d_id.b.area = MSB(LSW(rscn_entry));
2809                 d_id.b.al_pa = LSB(LSW(rscn_entry));
2810
2811                 DEBUG(printk("scsi(%ld): RSCN queue entry[%d] = "
2812                     "[%02x/%02x%02x%02x].\n",
2813                     ha->host_no, ha->rscn_out_ptr, format, d_id.b.domain,
2814                     d_id.b.area, d_id.b.al_pa));
2815
2816                 ha->rscn_out_ptr++;
2817                 if (ha->rscn_out_ptr == MAX_RSCN_COUNT)
2818                         ha->rscn_out_ptr = 0;
2819
2820                 /* Skip duplicate entries. */
2821                 for (rscn_out_iter = ha->rscn_out_ptr;
2822                     !ha->flags.rscn_queue_overflow &&
2823                     rscn_out_iter != ha->rscn_in_ptr;
2824                     rscn_out_iter = (rscn_out_iter ==
2825                         (MAX_RSCN_COUNT - 1)) ? 0: rscn_out_iter + 1) {
2826
2827                         if (rscn_entry != ha->rscn_queue[rscn_out_iter])
2828                                 break;
2829
2830                         DEBUG(printk("scsi(%ld): Skipping duplicate RSCN queue "
2831                             "entry found at [%d].\n", ha->host_no,
2832                             rscn_out_iter));
2833
2834                         ha->rscn_out_ptr = rscn_out_iter;
2835                 }
2836
2837                 /* Queue overflow, set switch default case. */
2838                 if (ha->flags.rscn_queue_overflow) {
2839                         DEBUG(printk("scsi(%ld): device_resync: rscn "
2840                             "overflow.\n", ha->host_no));
2841
2842                         format = 3;
2843                         ha->flags.rscn_queue_overflow = 0;
2844                 }
2845
2846                 switch (format) {
2847                 case 0:
2848                         mask = 0xffffff;
2849                         break;
2850                 case 1:
2851                         mask = 0xffff00;
2852                         break;
2853                 case 2:
2854                         mask = 0xff0000;
2855                         break;
2856                 default:
2857                         mask = 0x0;
2858                         d_id.b24 = 0;
2859                         ha->rscn_out_ptr = ha->rscn_in_ptr;
2860                         break;
2861                 }
2862
2863                 rval = QLA_SUCCESS;
2864
2865                 list_for_each_entry(fcport, &pha->fcports, list) {
2866                         if (fcport->vp_idx != ha->vp_idx)
2867                                 continue;
2868
2869                         if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2870                             (fcport->d_id.b24 & mask) != d_id.b24 ||
2871                             fcport->port_type == FCT_BROADCAST)
2872                                 continue;
2873
2874                         if (atomic_read(&fcport->state) == FCS_ONLINE) {
2875                                 if (format != 3 ||
2876                                     fcport->port_type != FCT_INITIATOR) {
2877                                         qla2x00_mark_device_lost(ha, fcport,
2878                                             0, 0);
2879                                 }
2880                         }
2881                         fcport->flags &= ~FCF_FARP_DONE;
2882                 }
2883         }
2884         return (rval);
2885 }
2886
2887 /*
2888  * qla2x00_fabric_dev_login
2889  *      Login fabric target device and update FC port database.
2890  *
2891  * Input:
2892  *      ha:             adapter state pointer.
2893  *      fcport:         port structure list pointer.
2894  *      next_loopid:    contains value of a new loop ID that can be used
2895  *                      by the next login attempt.
2896  *
2897  * Returns:
2898  *      qla2x00 local function return status code.
2899  *
2900  * Context:
2901  *      Kernel context.
2902  */
2903 static int
2904 qla2x00_fabric_dev_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2905     uint16_t *next_loopid)
2906 {
2907         int     rval;
2908         int     retry;
2909         uint8_t opts;
2910
2911         rval = QLA_SUCCESS;
2912         retry = 0;
2913
2914         rval = qla2x00_fabric_login(ha, fcport, next_loopid);
2915         if (rval == QLA_SUCCESS) {
2916                 /* Send an ADISC to tape devices.*/
2917                 opts = 0;
2918                 if (fcport->flags & FCF_TAPE_PRESENT)
2919                         opts |= BIT_1;
2920                 rval = qla2x00_get_port_database(ha, fcport, opts);
2921                 if (rval != QLA_SUCCESS) {
2922                         ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2923                             fcport->d_id.b.domain, fcport->d_id.b.area,
2924                             fcport->d_id.b.al_pa);
2925                         qla2x00_mark_device_lost(ha, fcport, 1, 0);
2926                 } else {
2927                         qla2x00_update_fcport(ha, fcport);
2928                 }
2929         }
2930
2931         return (rval);
2932 }
2933
2934 /*
2935  * qla2x00_fabric_login
2936  *      Issue fabric login command.
2937  *
2938  * Input:
2939  *      ha = adapter block pointer.
2940  *      device = pointer to FC device type structure.
2941  *
2942  * Returns:
2943  *      0 - Login successfully
2944  *      1 - Login failed
2945  *      2 - Initiator device
2946  *      3 - Fatal error
2947  */
2948 int
2949 qla2x00_fabric_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2950     uint16_t *next_loopid)
2951 {
2952         int     rval;
2953         int     retry;
2954         uint16_t tmp_loopid;
2955         uint16_t mb[MAILBOX_REGISTER_COUNT];
2956
2957         retry = 0;
2958         tmp_loopid = 0;
2959
2960         for (;;) {
2961                 DEBUG(printk("scsi(%ld): Trying Fabric Login w/loop id 0x%04x "
2962                     "for port %02x%02x%02x.\n",
2963                     ha->host_no, fcport->loop_id, fcport->d_id.b.domain,
2964                     fcport->d_id.b.area, fcport->d_id.b.al_pa));
2965
2966                 /* Login fcport on switch. */
2967                 ha->isp_ops.fabric_login(ha, fcport->loop_id,
2968                     fcport->d_id.b.domain, fcport->d_id.b.area,
2969                     fcport->d_id.b.al_pa, mb, BIT_0);
2970                 if (mb[0] == MBS_PORT_ID_USED) {
2971                         /*
2972                          * Device has another loop ID.  The firmware team
2973                          * recommends the driver perform an implicit login with
2974                          * the specified ID again. The ID we just used is save
2975                          * here so we return with an ID that can be tried by
2976                          * the next login.
2977                          */
2978                         retry++;
2979                         tmp_loopid = fcport->loop_id;
2980                         fcport->loop_id = mb[1];
2981
2982                         DEBUG(printk("Fabric Login: port in use - next "
2983                             "loop id=0x%04x, port Id=%02x%02x%02x.\n",
2984                             fcport->loop_id, fcport->d_id.b.domain,
2985                             fcport->d_id.b.area, fcport->d_id.b.al_pa));
2986
2987                 } else if (mb[0] == MBS_COMMAND_COMPLETE) {
2988                         /*
2989                          * Login succeeded.
2990                          */
2991                         if (retry) {
2992                                 /* A retry occurred before. */
2993                                 *next_loopid = tmp_loopid;
2994                         } else {
2995                                 /*
2996                                  * No retry occurred before. Just increment the
2997                                  * ID value for next login.
2998                                  */
2999                                 *next_loopid = (fcport->loop_id + 1);
3000                         }
3001
3002                         if (mb[1] & BIT_0) {
3003                                 fcport->port_type = FCT_INITIATOR;
3004                         } else {
3005                                 fcport->port_type = FCT_TARGET;
3006                                 if (mb[1] & BIT_1) {
3007                                         fcport->flags |= FCF_TAPE_PRESENT;
3008                                 }
3009                         }
3010
3011                         if (mb[10] & BIT_0)
3012                                 fcport->supported_classes |= FC_COS_CLASS2;
3013                         if (mb[10] & BIT_1)
3014                                 fcport->supported_classes |= FC_COS_CLASS3;
3015
3016                         rval = QLA_SUCCESS;
3017                         break;
3018                 } else if (mb[0] == MBS_LOOP_ID_USED) {
3019                         /*
3020                          * Loop ID already used, try next loop ID.
3021                          */
3022                         fcport->loop_id++;
3023                         rval = qla2x00_find_new_loop_id(ha, fcport);
3024                         if (rval != QLA_SUCCESS) {
3025                                 /* Ran out of loop IDs to use */
3026                                 break;
3027                         }
3028                 } else if (mb[0] == MBS_COMMAND_ERROR) {
3029                         /*
3030                          * Firmware possibly timed out during login. If NO
3031                          * retries are left to do then the device is declared
3032                          * dead.
3033                          */
3034                         *next_loopid = fcport->loop_id;
3035                         ha->isp_ops.fabric_logout(ha, fcport->loop_id,
3036                             fcport->d_id.b.domain, fcport->d_id.b.area,
3037                             fcport->d_id.b.al_pa);
3038                         qla2x00_mark_device_lost(ha, fcport, 1, 0);
3039
3040                         rval = 1;
3041                         break;
3042                 } else {
3043                         /*
3044                          * unrecoverable / not handled error
3045                          */
3046                         DEBUG2(printk("%s(%ld): failed=%x port_id=%02x%02x%02x "
3047                             "loop_id=%x jiffies=%lx.\n",
3048                             __func__, ha->host_no, mb[0],
3049                             fcport->d_id.b.domain, fcport->d_id.b.area,
3050                             fcport->d_id.b.al_pa, fcport->loop_id, jiffies));
3051
3052                         *next_loopid = fcport->loop_id;
3053                         ha->isp_ops.fabric_logout(ha, fcport->loop_id,
3054                             fcport->d_id.b.domain, fcport->d_id.b.area,
3055                             fcport->d_id.b.al_pa);
3056                         fcport->loop_id = FC_NO_LOOP_ID;
3057                         fcport->login_retry = 0;
3058
3059                         rval = 3;
3060                         break;
3061                 }
3062         }
3063
3064         return (rval);
3065 }
3066
3067 /*
3068  * qla2x00_local_device_login
3069  *      Issue local device login command.
3070  *
3071  * Input:
3072  *      ha = adapter block pointer.
3073  *      loop_id = loop id of device to login to.
3074  *
3075  * Returns (Where's the #define!!!!):
3076  *      0 - Login successfully
3077  *      1 - Login failed
3078  *      3 - Fatal error
3079  */
3080 int
3081 qla2x00_local_device_login(scsi_qla_host_t *ha, fc_port_t *fcport)
3082 {
3083         int             rval;
3084         uint16_t        mb[MAILBOX_REGISTER_COUNT];
3085
3086         memset(mb, 0, sizeof(mb));
3087         rval = qla2x00_login_local_device(ha, fcport, mb, BIT_0);
3088         if (rval == QLA_SUCCESS) {
3089                 /* Interrogate mailbox registers for any errors */
3090                 if (mb[0] == MBS_COMMAND_ERROR)
3091                         rval = 1;
3092                 else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
3093                         /* device not in PCB table */
3094                         rval = 3;
3095         }
3096
3097         return (rval);
3098 }
3099
3100 /*
3101  *  qla2x00_loop_resync
3102  *      Resync with fibre channel devices.
3103  *
3104  * Input:
3105  *      ha = adapter block pointer.
3106  *
3107  * Returns:
3108  *      0 = success
3109  */
3110 int
3111 qla2x00_loop_resync(scsi_qla_host_t *ha)
3112 {
3113         int   rval;
3114         uint32_t wait_time;
3115
3116         rval = QLA_SUCCESS;
3117
3118         atomic_set(&ha->loop_state, LOOP_UPDATE);
3119         clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3120         if (ha->flags.online) {
3121                 if (!(rval = qla2x00_fw_ready(ha))) {
3122                         /* Wait at most MAX_TARGET RSCNs for a stable link. */
3123                         wait_time = 256;
3124                         do {
3125                                 atomic_set(&ha->loop_state, LOOP_UPDATE);
3126
3127                                 /* Issue a marker after FW becomes ready. */
3128                                 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
3129                                 ha->marker_needed = 0;
3130
3131                                 /* Remap devices on Loop. */
3132                                 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
3133
3134 &