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