Printk cleanups and minor fixes
[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(KERN_INFO "scsi(%ld): Firmware not ready "
1436                     "(%x).\n", 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
2010         spin_lock_irq(fcport->ha->host->host_lock);
2011         rport = fcport->drport;
2012         fcport->drport = NULL;
2013         spin_unlock_irq(fcport->ha->host->host_lock);
2014         if (rport) {
2015                 fc_remote_port_delete(rport);
2016 #ifdef CONFIG_SCSI_QLA2XXX_TARGET
2017                 if (qla_target.tgt_fc_port_deleted)
2018                         qla_target.tgt_fc_port_deleted(fcport->ha, fcport);
2019 #endif
2020         }
2021 }
2022
2023 /**
2024  * qla2x00_alloc_fcport() - Allocate a generic fcport.
2025  * @ha: HA context
2026  * @flags: allocation flags
2027  *
2028  * Returns a pointer to the allocated fcport, or NULL, if none available.
2029  */
2030 static fc_port_t *
2031 qla2x00_alloc_fcport(scsi_qla_host_t *ha, gfp_t flags)
2032 {
2033         fc_port_t *fcport;
2034
2035         fcport = kzalloc(sizeof(fc_port_t), flags);
2036         if (!fcport)
2037                 return NULL;
2038
2039         /* Setup fcport template structure. */
2040         fcport->ha = ha;
2041         fcport->vp_idx = ha->vp_idx;
2042         fcport->port_type = FCT_UNKNOWN;
2043         fcport->loop_id = FC_NO_LOOP_ID;
2044         atomic_set(&fcport->state, FCS_UNCONFIGURED);
2045         fcport->flags = FCF_RLC_SUPPORT;
2046         fcport->supported_classes = FC_COS_UNSPECIFIED;
2047
2048         return fcport;
2049 }
2050
2051 /*
2052  * qla2x00_configure_loop
2053  *      Updates Fibre Channel Device Database with what is actually on loop.
2054  *
2055  * Input:
2056  *      ha                = adapter block pointer.
2057  *
2058  * Returns:
2059  *      0 = success.
2060  *      1 = error.
2061  *      2 = database was full and device was not configured.
2062  */
2063  int
2064 qla2x00_configure_loop(scsi_qla_host_t *ha)
2065 {
2066         int  rval;
2067         unsigned long flags, save_flags;
2068
2069         rval = QLA_SUCCESS;
2070
2071         /* Get Initiator ID */
2072         if (test_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags)) {
2073                 rval = qla2x00_configure_hba(ha);
2074                 if (rval != QLA_SUCCESS) {
2075                         DEBUG(printk("scsi(%ld): Unable to configure HBA.\n",
2076                             ha->host_no));
2077                         return (rval);
2078                 }
2079         }
2080
2081         save_flags = flags = ha->dpc_flags;
2082         DEBUG(printk("scsi(%ld): Configure loop -- dpc flags =0x%lx\n",
2083             ha->host_no, flags));
2084
2085         /*
2086          * If we have both an RSCN and PORT UPDATE pending then handle them
2087          * both at the same time.
2088          */
2089         clear_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
2090         clear_bit(RSCN_UPDATE, &ha->dpc_flags);
2091
2092         /* Determine what we need to do */
2093         if (ha->current_topology == ISP_CFG_FL &&
2094             (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
2095
2096                 ha->flags.rscn_queue_overflow = 1;
2097                 set_bit(RSCN_UPDATE, &flags);
2098
2099         } else if (ha->current_topology == ISP_CFG_F &&
2100             (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
2101
2102                 ha->flags.rscn_queue_overflow = 1;
2103                 set_bit(RSCN_UPDATE, &flags);
2104                 clear_bit(LOCAL_LOOP_UPDATE, &flags);
2105
2106         } else if (ha->current_topology == ISP_CFG_N) {
2107                 clear_bit(RSCN_UPDATE, &flags);
2108
2109         } else if (!ha->flags.online ||
2110             (test_bit(ABORT_ISP_ACTIVE, &flags))) {
2111
2112                 ha->flags.rscn_queue_overflow = 1;
2113                 set_bit(RSCN_UPDATE, &flags);
2114                 set_bit(LOCAL_LOOP_UPDATE, &flags);
2115         }
2116
2117         if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
2118                 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
2119                         rval = QLA_FUNCTION_FAILED;
2120                 } else {
2121                         rval = qla2x00_configure_local_loop(ha);
2122                 }
2123         }
2124
2125         if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
2126                 if (LOOP_TRANSITION(ha)) {
2127                         rval = QLA_FUNCTION_FAILED;
2128                 } else {
2129                         rval = qla2x00_configure_fabric(ha);
2130                 }
2131         }
2132
2133         if (rval == QLA_SUCCESS) {
2134                 if (atomic_read(&ha->loop_down_timer) ||
2135                     test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
2136                         rval = QLA_FUNCTION_FAILED;
2137                 } else {
2138                         atomic_set(&ha->loop_state, LOOP_READY);
2139
2140                         DEBUG(printk("scsi(%ld): LOOP READY\n", ha->host_no));
2141                 }
2142         }
2143
2144         if (rval) {
2145                 DEBUG2_3(printk("%s(%ld): *** FAILED ***\n",
2146                     __func__, ha->host_no));
2147         } else {
2148                 DEBUG3(printk("%s: exiting normally\n", __func__));
2149         }
2150
2151         /* Restore state if a resync event occured during processing */
2152         if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
2153                 if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
2154                         set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
2155                 if (test_bit(RSCN_UPDATE, &save_flags))
2156                         set_bit(RSCN_UPDATE, &ha->dpc_flags);
2157         }
2158
2159         return (rval);
2160 }
2161
2162
2163
2164 /*
2165  * qla2x00_configure_local_loop
2166  *      Updates Fibre Channel Device Database with local loop devices.
2167  *
2168  * Input:
2169  *      ha = adapter block pointer.
2170  *
2171  * Returns:
2172  *      0 = success.
2173  */
2174  int
2175 qla2x00_configure_local_loop(scsi_qla_host_t *ha)
2176 {
2177         int             rval, rval2;
2178         int             found_devs;
2179         int             found;
2180         fc_port_t       *fcport, *new_fcport;
2181
2182         uint16_t        index;
2183         uint16_t        entries;
2184         char            *id_iter;
2185         uint16_t        loop_id;
2186         uint8_t         domain, area, al_pa;
2187         scsi_qla_host_t *pha = to_qla_parent(ha);
2188
2189         found_devs = 0;
2190         new_fcport = NULL;
2191         entries = MAX_FIBRE_DEVICES;
2192
2193         DEBUG3(printk("scsi(%ld): Getting FCAL position map\n", ha->host_no));
2194         DEBUG3(qla2x00_get_fcal_position_map(ha, NULL));
2195
2196         /* Get list of logged in devices. */
2197         memset(ha->gid_list, 0, GID_LIST_SIZE);
2198         rval = qla2x00_get_id_list(ha, ha->gid_list, ha->gid_list_dma,
2199             &entries);
2200         if (rval != QLA_SUCCESS)
2201                 goto cleanup_allocation;
2202
2203         DEBUG3(printk("scsi(%ld): Entries in ID list (%d)\n",
2204             ha->host_no, entries));
2205         DEBUG3(qla2x00_dump_buffer((uint8_t *)ha->gid_list,
2206             entries * sizeof(struct gid_list_info)));
2207
2208         /* Allocate temporary fcport for any new fcports discovered. */
2209         new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2210         if (new_fcport == NULL) {
2211                 rval = QLA_MEMORY_ALLOC_FAILED;
2212                 goto cleanup_allocation;
2213         }
2214         new_fcport->flags &= ~FCF_FABRIC_DEVICE;
2215
2216         /*
2217          * Mark local devices that were present with FCF_DEVICE_LOST for now.
2218          */
2219         list_for_each_entry_rcu(fcport, &pha->fcports, list) {
2220                 if (fcport->vp_idx != ha->vp_idx)
2221                         continue;
2222
2223                 if (atomic_read(&fcport->state) == FCS_ONLINE &&
2224                     fcport->port_type != FCT_BROADCAST &&
2225                     (fcport->flags & FCF_FABRIC_DEVICE) == 0) {
2226
2227                         DEBUG(printk("scsi(%ld): Marking port lost, "
2228                             "loop_id=0x%04x\n",
2229                             ha->host_no, fcport->loop_id));
2230
2231                         atomic_set(&fcport->state, FCS_DEVICE_LOST);
2232                         fcport->flags &= ~FCF_FARP_DONE;
2233                 }
2234         }
2235
2236         /* Add devices to port list. */
2237         id_iter = (char *)ha->gid_list;
2238         for (index = 0; index < entries; index++) {
2239                 domain = ((struct gid_list_info *)id_iter)->domain;
2240                 area = ((struct gid_list_info *)id_iter)->area;
2241                 al_pa = ((struct gid_list_info *)id_iter)->al_pa;
2242                 if (IS_QLA2100(ha) || IS_QLA2200(ha))
2243                         loop_id = (uint16_t)
2244                             ((struct gid_list_info *)id_iter)->loop_id_2100;
2245                 else
2246                         loop_id = le16_to_cpu(
2247                             ((struct gid_list_info *)id_iter)->loop_id);
2248                 id_iter += ha->gid_list_info_size;
2249
2250                 /* Bypass reserved domain fields. */
2251                 if ((domain & 0xf0) == 0xf0)
2252                         continue;
2253
2254                 /* Bypass if not same domain and area of adapter. */
2255                 if (area && domain &&
2256                     (area != ha->d_id.b.area || domain != ha->d_id.b.domain))
2257                         continue;
2258
2259                 /* Bypass invalid local loop ID. */
2260                 if (loop_id > LAST_LOCAL_LOOP_ID)
2261                         continue;
2262
2263                 /* Fill in member data. */
2264                 new_fcport->d_id.b.domain = domain;
2265                 new_fcport->d_id.b.area = area;
2266                 new_fcport->d_id.b.al_pa = al_pa;
2267                 new_fcport->loop_id = loop_id;
2268                 new_fcport->vp_idx = ha->vp_idx;
2269                 rval2 = qla2x00_get_port_database(ha, new_fcport, 0);
2270                 if (rval2 != QLA_SUCCESS) {
2271                         DEBUG2(printk("scsi(%ld): Failed to retrieve fcport "
2272                             "information -- get_port_database=%x, "
2273                             "loop_id=0x%04x\n",
2274                             ha->host_no, rval2, new_fcport->loop_id));
2275                         DEBUG2(printk("scsi(%ld): Scheduling resync...\n",
2276                             ha->host_no));
2277                         set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
2278                         continue;
2279                 }
2280
2281                 /* Check for matching device in port list. */
2282                 found = 0;
2283                 fcport = NULL;
2284                 list_for_each_entry_rcu(fcport, &pha->fcports, list) {
2285                         if (fcport->vp_idx != ha->vp_idx)
2286                                 continue;
2287
2288                         if (memcmp(new_fcport->port_name, fcport->port_name,
2289                             WWN_SIZE))
2290                                 continue;
2291
2292                         fcport->flags &= ~(FCF_FABRIC_DEVICE |
2293                             FCF_PERSISTENT_BOUND);
2294                         fcport->loop_id = new_fcport->loop_id;
2295                         fcport->port_type = new_fcport->port_type;
2296                         fcport->d_id.b24 = new_fcport->d_id.b24;
2297                         memcpy(fcport->node_name, new_fcport->node_name,
2298                             WWN_SIZE);
2299
2300                         found++;
2301                         break;
2302                 }
2303
2304                 if (!found) {
2305                         /* New device, add to fcports list. */
2306                         new_fcport->flags &= ~FCF_PERSISTENT_BOUND;
2307                         if (ha->parent) {
2308                                 new_fcport->ha = ha;
2309                                 new_fcport->vp_idx = ha->vp_idx;
2310                                 list_add_tail(&new_fcport->vp_fcport,
2311                                     &ha->vp_fcports);
2312                         }
2313                         list_add_tail_rcu(&new_fcport->list, &pha->fcports);
2314
2315                         /* Allocate a new replacement fcport. */
2316                         fcport = new_fcport;
2317                         new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2318                         if (new_fcport == NULL) {
2319                                 rval = QLA_MEMORY_ALLOC_FAILED;
2320                                 goto cleanup_allocation;
2321                         }
2322                         new_fcport->flags &= ~FCF_FABRIC_DEVICE;
2323                 }
2324
2325                 /* Base iIDMA settings on HBA port speed. */
2326                 fcport->fp_speed = ha->link_data_rate;
2327
2328                 qla2x00_update_fcport(ha, fcport);
2329
2330                 found_devs++;
2331         }
2332
2333 cleanup_allocation:
2334         kfree(new_fcport);
2335
2336         if (rval != QLA_SUCCESS) {
2337                 DEBUG2(printk("scsi(%ld): Configure local loop error exit: "
2338                     "rval=%x\n", ha->host_no, rval));
2339         }
2340
2341         if (found_devs) {
2342                 ha->device_flags |= DFLG_LOCAL_DEVICES;
2343                 ha->device_flags &= ~DFLG_RETRY_LOCAL_DEVICES;
2344         }
2345
2346         return (rval);
2347 }
2348
2349 static void
2350 qla2x00_iidma_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
2351 {
2352 #define LS_UNKNOWN      2
2353         static char *link_speeds[5] = { "1", "2", "?", "4", "8" };
2354         int rval;
2355         uint16_t mb[6];
2356
2357         if (!IS_IIDMA_CAPABLE(ha))
2358                 return;
2359
2360         if (fcport->fp_speed == PORT_SPEED_UNKNOWN ||
2361             fcport->fp_speed > ha->link_data_rate)
2362                 return;
2363
2364         rval = qla2x00_set_idma_speed(ha, fcport->loop_id, fcport->fp_speed,
2365             mb);
2366         if (rval != QLA_SUCCESS) {
2367                 DEBUG2(printk("scsi(%ld): Unable to adjust iIDMA "
2368                     "%02x%02x%02x%02x%02x%02x%02x%02x -- %04x %x %04x %04x.\n",
2369                     ha->host_no, fcport->port_name[0], fcport->port_name[1],
2370                     fcport->port_name[2], fcport->port_name[3],
2371                     fcport->port_name[4], fcport->port_name[5],
2372                     fcport->port_name[6], fcport->port_name[7], rval,
2373                     fcport->fp_speed, mb[0], mb[1]));
2374         } else {
2375                 DEBUG2(qla_printk(KERN_INFO, ha,
2376                     "iIDMA adjusted to %s GB/s on "
2377                     "%02x%02x%02x%02x%02x%02x%02x%02x.\n",
2378                     link_speeds[fcport->fp_speed], fcport->port_name[0],
2379                     fcport->port_name[1], fcport->port_name[2],
2380                     fcport->port_name[3], fcport->port_name[4],
2381                     fcport->port_name[5], fcport->port_name[6],
2382                     fcport->port_name[7]));
2383         }
2384 }
2385
2386 static void
2387 qla2x00_reg_remote_port(scsi_qla_host_t *ha, fc_port_t *fcport)
2388 {
2389         struct fc_rport_identifiers rport_ids;
2390         struct fc_rport *rport;
2391
2392         if (fcport->drport)
2393                 qla2x00_rport_del(fcport);
2394
2395         rport_ids.node_name = wwn_to_u64(fcport->node_name);
2396         rport_ids.port_name = wwn_to_u64(fcport->port_name);
2397         rport_ids.port_id = fcport->d_id.b.domain << 16 |
2398             fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
2399         rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
2400         fcport->rport = rport = fc_remote_port_add(ha->host, 0, &rport_ids);
2401         if (!rport) {
2402                 qla_printk(KERN_WARNING, ha,
2403                     "Unable to allocate fc remote port!\n");
2404                 return;
2405         }
2406 #ifdef CONFIG_SCSI_QLA2XXX_TARGET
2407         if (qla_target.tgt_fc_port_added)
2408                 qla_target.tgt_fc_port_added(ha, fcport);
2409 #endif
2410         spin_lock_irq(fcport->ha->host->host_lock);
2411         *((fc_port_t **)rport->dd_data) = fcport;
2412         spin_unlock_irq(fcport->ha->host->host_lock);
2413
2414         rport->supported_classes = fcport->supported_classes;
2415
2416         rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
2417         if (fcport->port_type == FCT_INITIATOR)
2418                 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2419         if (fcport->port_type == FCT_TARGET)
2420                 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
2421         fc_remote_port_rolechg(rport, rport_ids.roles);
2422 }
2423
2424 /*
2425  * qla2x00_update_fcport
2426  *      Updates device on list.
2427  *
2428  * Input:
2429  *      ha = adapter block pointer.
2430  *      fcport = port structure pointer.
2431  *
2432  * Return:
2433  *      0  - Success
2434  *  BIT_0 - error
2435  *
2436  * Context:
2437  *      Kernel context.
2438  */
2439 void
2440 qla2x00_update_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
2441 {
2442         scsi_qla_host_t *pha = to_qla_parent(ha);
2443
2444         fcport->ha = ha;
2445         fcport->login_retry = 0;
2446         fcport->port_login_retry_count = pha->port_down_retry_count *
2447             PORT_RETRY_TIME;
2448         atomic_set(&fcport->port_down_timer, pha->port_down_retry_count *
2449             PORT_RETRY_TIME);
2450         fcport->flags &= ~FCF_LOGIN_NEEDED;
2451
2452         qla2x00_iidma_fcport(ha, fcport);
2453
2454         atomic_set(&fcport->state, FCS_ONLINE);
2455
2456         qla2x00_reg_remote_port(ha, fcport);
2457 }
2458
2459 /*
2460  * qla2x00_configure_fabric
2461  *      Setup SNS devices with loop ID's.
2462  *
2463  * Input:
2464  *      ha = adapter block pointer.
2465  *
2466  * Returns:
2467  *      0 = success.
2468  *      BIT_0 = error
2469  */
2470  int
2471 qla2x00_configure_fabric(scsi_qla_host_t *ha)
2472 {
2473         int     rval, rval2;
2474         fc_port_t       *fcport, *fcptemp;
2475         uint16_t        next_loopid;
2476         uint16_t        mb[MAILBOX_REGISTER_COUNT];
2477         uint16_t        loop_id;
2478         LIST_HEAD(new_fcports);
2479         scsi_qla_host_t *pha = to_qla_parent(ha);
2480
2481         /* If FL port exists, then SNS is present */
2482         if (IS_FWI2_CAPABLE(ha))
2483                 loop_id = NPH_F_PORT;
2484         else
2485                 loop_id = SNS_FL_PORT;
2486         rval = qla2x00_get_port_name(ha, loop_id, ha->fabric_node_name, 1);
2487         if (rval != QLA_SUCCESS) {
2488                 DEBUG2(printk("scsi(%ld): MBC_GET_PORT_NAME Failed, No FL "
2489                     "Port\n", ha->host_no));
2490
2491                 ha->device_flags &= ~SWITCH_FOUND;
2492                 return (QLA_SUCCESS);
2493         }
2494         ha->device_flags |= SWITCH_FOUND;
2495
2496         /* Mark devices that need re-synchronization. */
2497         rval2 = qla2x00_device_resync(ha);
2498         if (rval2 == QLA_RSCNS_HANDLED) {
2499                 /* No point doing the scan, just continue. */
2500                 return (QLA_SUCCESS);
2501         }
2502         do {
2503                 /* FDMI support. */
2504                 if (ql2xfdmienable &&
2505                     test_and_clear_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags))
2506                         qla2x00_fdmi_register(ha);
2507
2508                 /* Ensure we are logged into the SNS. */
2509                 if (IS_FWI2_CAPABLE(ha))
2510                         loop_id = NPH_SNS;
2511                 else
2512                         loop_id = SIMPLE_NAME_SERVER;
2513                 ha->isp_ops->fabric_login(ha, loop_id, 0xff, 0xff,
2514                     0xfc, mb, BIT_1 | BIT_0);
2515                 if (mb[0] != MBS_COMMAND_COMPLETE) {
2516                         DEBUG2(qla_printk(KERN_INFO, ha,
2517                             "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
2518                             "mb[2]=%x mb[6]=%x mb[7]=%x\n", loop_id,
2519                             mb[0], mb[1], mb[2], mb[6], mb[7]));
2520                         return (QLA_SUCCESS);
2521                 }
2522
2523                 if (test_and_clear_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags)) {
2524                         if (qla2x00_rft_id(ha)) {
2525                                 /* EMPTY */
2526                                 DEBUG2(printk("scsi(%ld): Register FC-4 "
2527                                     "TYPE failed.\n", ha->host_no));
2528                         }
2529                         if (qla2x00_rff_id(ha)) {
2530                                 /* EMPTY */
2531                                 DEBUG2(printk("scsi(%ld): Register FC-4 "
2532                                     "Features failed.\n", ha->host_no));
2533                         }
2534                         if (qla2x00_rnn_id(ha)) {
2535                                 /* EMPTY */
2536                                 DEBUG2(printk("scsi(%ld): Register Node Name "
2537                                     "failed.\n", ha->host_no));
2538                         } else if (qla2x00_rsnn_nn(ha)) {
2539                                 /* EMPTY */
2540                                 DEBUG2(printk("scsi(%ld): Register Symbolic "
2541                                     "Node Name failed.\n", ha->host_no));
2542                         }
2543                 }
2544
2545                 rval = qla2x00_find_all_fabric_devs(ha, &new_fcports);
2546                 if (rval != QLA_SUCCESS) {
2547                         break;
2548                 }
2549
2550                 /*
2551                  * Logout all previous fabric devices marked lost, except
2552                  * tape devices.
2553                  */
2554                 list_for_each_entry_rcu(fcport, &pha->fcports, list) {
2555                         if (fcport->vp_idx !=ha->vp_idx)
2556                                 continue;
2557
2558                         if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2559                                 break;
2560
2561                         if ((fcport->flags & FCF_FABRIC_DEVICE) == 0)
2562                                 continue;
2563
2564                         if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
2565                                 qla2x00_mark_device_lost(ha, fcport,
2566                                     ql2xplogiabsentdevice, 0);
2567                                 if (fcport->loop_id != FC_NO_LOOP_ID &&
2568                                     (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2569                                     fcport->port_type != FCT_INITIATOR &&
2570                                     fcport->port_type != FCT_BROADCAST) {
2571                                         ha->isp_ops->fabric_logout(ha,
2572                                             fcport->loop_id,
2573                                             fcport->d_id.b.domain,
2574                                             fcport->d_id.b.area,
2575                                             fcport->d_id.b.al_pa);
2576                                         fcport->loop_id = FC_NO_LOOP_ID;
2577                                 }
2578                         }
2579                 }
2580
2581                 /* Starting free loop ID. */
2582                 next_loopid = pha->min_external_loopid;
2583
2584                 /*
2585                  * Scan through our port list and login entries that need to be
2586                  * logged in.
2587                  */
2588                 list_for_each_entry_rcu(fcport, &pha->fcports, list) {
2589                         if (fcport->vp_idx != ha->vp_idx)
2590                                 continue;
2591
2592                         if (atomic_read(&ha->loop_down_timer) ||
2593                             test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2594                                 break;
2595
2596                         if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2597                             (fcport->flags & FCF_LOGIN_NEEDED) == 0)
2598                                 continue;
2599
2600                         if (fcport->loop_id == FC_NO_LOOP_ID) {
2601                                 fcport->loop_id = next_loopid;
2602                                 rval = qla2x00_find_new_loop_id(
2603                                     to_qla_parent(ha), fcport);
2604                                 if (rval != QLA_SUCCESS) {
2605                                         /* Ran out of IDs to use */
2606                                         break;
2607                                 }
2608                         }
2609                         /* Login and update database */
2610                         qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
2611                 }
2612
2613                 /* Exit if out of loop IDs. */
2614                 if (rval != QLA_SUCCESS) {
2615                         break;
2616                 }
2617
2618                 /*
2619                  * Login and add the new devices to our port list.
2620                  */
2621                 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2622                         if (atomic_read(&ha->loop_down_timer) ||
2623                             test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2624                                 break;
2625
2626                         /* Find a new loop ID to use. */
2627                         fcport->loop_id = next_loopid;
2628                         rval = qla2x00_find_new_loop_id(to_qla_parent(ha),
2629                             fcport);
2630                         if (rval != QLA_SUCCESS) {
2631                                 /* Ran out of IDs to use */
2632                                 break;
2633                         }
2634
2635                         /* Login and update database */
2636                         qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
2637
2638                         if (ha->parent) {
2639                                 fcport->ha = ha;
2640                                 fcport->vp_idx = ha->vp_idx;
2641                                 list_add_tail(&fcport->vp_fcport,
2642                                     &ha->vp_fcports);
2643                                 list_del(&fcport->list);
2644                                 list_add_tail_rcu(&fcport->list,
2645                                     &ha->parent->fcports);
2646                         } else {
2647                                 list_del(&fcport->list);
2648                                 list_add_tail_rcu(&fcport->list, &ha->fcports);
2649                         }
2650                 }
2651         } while (0);
2652
2653         /* Free all new device structures not processed. */
2654         list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2655                 list_del(&fcport->list);
2656                 kfree(fcport);
2657         }
2658
2659         if (rval) {
2660                 DEBUG2(printk("scsi(%ld): Configure fabric error exit: "
2661                     "rval=%d\n", ha->host_no, rval));
2662         }
2663
2664         return (rval);
2665 }
2666
2667
2668 /*
2669  * qla2x00_find_all_fabric_devs
2670  *
2671  * Input:
2672  *      ha = adapter block pointer.
2673  *      dev = database device entry pointer.
2674  *
2675  * Returns:
2676  *      0 = success.
2677  *
2678  * Context:
2679  *      Kernel context.
2680  */
2681 static int
2682 qla2x00_find_all_fabric_devs(scsi_qla_host_t *ha, struct list_head *new_fcports)
2683 {
2684         int             rval;
2685         uint16_t        loop_id;
2686         fc_port_t       *fcport, *new_fcport, *fcptemp;
2687         int             found;
2688
2689         sw_info_t       *swl;
2690         int             swl_idx;
2691         int             first_dev, last_dev;
2692         port_id_t       wrap, nxt_d_id;
2693         int             vp_index;
2694         int             empty_vp_index;
2695         int             found_vp;
2696         scsi_qla_host_t *vha;
2697         scsi_qla_host_t *pha = to_qla_parent(ha);
2698
2699         rval = QLA_SUCCESS;
2700
2701         /* Try GID_PT to get device list, else GAN. */
2702         swl = kcalloc(MAX_FIBRE_DEVICES, sizeof(sw_info_t), GFP_ATOMIC);
2703         if (!swl) {
2704                 /*EMPTY*/
2705                 DEBUG2(printk("scsi(%ld): GID_PT allocations failed, fallback "
2706                     "on GA_NXT\n", ha->host_no));
2707         } else {
2708                 if (qla2x00_gid_pt(ha, swl) != QLA_SUCCESS) {
2709                         kfree(swl);
2710                         swl = NULL;
2711                 } else if (qla2x00_gpn_id(ha, swl) != QLA_SUCCESS) {
2712                         kfree(swl);
2713                         swl = NULL;
2714                 } else if (qla2x00_gnn_id(ha, swl) != QLA_SUCCESS) {
2715                         kfree(swl);
2716                         swl = NULL;
2717                 } else if (qla2x00_gfpn_id(ha, swl) == QLA_SUCCESS) {
2718                         qla2x00_gpsc(ha, swl);
2719                 }
2720         }
2721         swl_idx = 0;
2722
2723         /* Allocate temporary fcport for any new fcports discovered. */
2724         new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2725         if (new_fcport == NULL) {
2726                 kfree(swl);
2727                 return (QLA_MEMORY_ALLOC_FAILED);
2728         }
2729         new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2730         new_fcport->vp_idx = ha->vp_idx;
2731         /* Set start port ID scan at adapter ID. */
2732         first_dev = 1;
2733         last_dev = 0;
2734
2735         /* Starting free loop ID. */
2736         loop_id = pha->min_external_loopid;
2737         for (; loop_id <= ha->last_loop_id; loop_id++) {
2738                 if (qla2x00_is_reserved_id(ha, loop_id))
2739                         continue;
2740
2741                 if (atomic_read(&ha->loop_down_timer) || LOOP_TRANSITION(ha))
2742                         break;
2743
2744                 if (swl != NULL) {
2745                         if (last_dev) {
2746                                 wrap.b24 = new_fcport->d_id.b24;
2747                         } else {
2748                                 new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
2749                                 memcpy(new_fcport->node_name,
2750                                     swl[swl_idx].node_name, WWN_SIZE);
2751                                 memcpy(new_fcport->port_name,
2752                                     swl[swl_idx].port_name, WWN_SIZE);
2753                                 memcpy(new_fcport->fabric_port_name,
2754                                     swl[swl_idx].fabric_port_name, WWN_SIZE);
2755                                 new_fcport->fp_speed = swl[swl_idx].fp_speed;
2756
2757                                 if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
2758                                         last_dev = 1;
2759                                 }
2760                                 swl_idx++;
2761                         }
2762                 } else {
2763                         /* Send GA_NXT to the switch */
2764                         rval = qla2x00_ga_nxt(ha, new_fcport);
2765                         if (rval != QLA_SUCCESS) {
2766                                 qla_printk(KERN_WARNING, ha,
2767                                     "SNS scan failed -- assuming zero-entry "
2768                                     "result...\n");
2769                                 list_for_each_entry_safe(fcport, fcptemp,
2770                                     new_fcports, list) {
2771                                         list_del(&fcport->list);
2772                                         kfree(fcport);
2773                                 }
2774                                 rval = QLA_SUCCESS;
2775                                 break;
2776                         }
2777                 }
2778
2779                 /* If wrap on switch device list, exit. */
2780                 if (first_dev) {
2781                         wrap.b24 = new_fcport->d_id.b24;
2782                         first_dev = 0;
2783                 } else if (new_fcport->d_id.b24 == wrap.b24) {
2784                         DEBUG2(printk("scsi(%ld): device wrap (%02x%02x%02x)\n",
2785                             ha->host_no, new_fcport->d_id.b.domain,
2786                             new_fcport->d_id.b.area, new_fcport->d_id.b.al_pa));
2787                         break;
2788                 }
2789
2790                 /* Bypass if same physical adapter. */
2791                 if (new_fcport->d_id.b24 == pha->d_id.b24)
2792                         continue;
2793
2794                 /* Bypass virtual ports of the same host. */
2795                 if (pha->num_vhosts) {
2796                         for_each_mapped_vp_idx(pha, vp_index) {
2797                                 empty_vp_index = 1;
2798                                 found_vp = 0;
2799                                 list_for_each_entry(vha, &pha->vp_list,
2800                                     vp_list) {
2801                                         if (vp_index == vha->vp_idx) {
2802                                                 empty_vp_index = 0;
2803                                                 found_vp = 1;
2804                                                 break;
2805                                         }
2806                                 }
2807
2808                                 if (empty_vp_index)
2809                                         continue;
2810
2811                                 if (found_vp &&
2812                                     new_fcport->d_id.b24 == vha->d_id.b24)
2813                                         break;
2814                         }
2815
2816                         if (vp_index <= pha->max_npiv_vports)
2817                                 continue;
2818                 }
2819
2820                 /* Bypass if same domain and area of adapter. */
2821                 if (((new_fcport->d_id.b24 & 0xffff00) ==
2822                     (ha->d_id.b24 & 0xffff00)) && ha->current_topology ==
2823                         ISP_CFG_FL)
2824                             continue;
2825
2826                 /* Bypass reserved domain fields. */
2827                 if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
2828                         continue;
2829
2830                 /* Locate matching device in database. */
2831                 found = 0;
2832                 list_for_each_entry_rcu(fcport, &pha->fcports, list) {
2833                         if (new_fcport->vp_idx != fcport->vp_idx)
2834                                 continue;
2835                         if (memcmp(new_fcport->port_name, fcport->port_name,
2836                             WWN_SIZE))
2837                                 continue;
2838
2839                         found++;
2840
2841                         /* Update port state. */
2842                         memcpy(fcport->fabric_port_name,
2843                             new_fcport->fabric_port_name, WWN_SIZE);
2844                         fcport->fp_speed = new_fcport->fp_speed;
2845
2846                         /*
2847                          * If address the same and state FCS_ONLINE, nothing
2848                          * changed.
2849                          */
2850                         if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
2851                             atomic_read(&fcport->state) == FCS_ONLINE) {
2852                                 break;
2853                         }
2854
2855                         /*
2856                          * If device was not a fabric device before.
2857                          */
2858                         if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
2859                                 fcport->d_id.b24 = new_fcport->d_id.b24;
2860                                 fcport->loop_id = FC_NO_LOOP_ID;
2861                                 fcport->flags |= (FCF_FABRIC_DEVICE |
2862                                     FCF_LOGIN_NEEDED);
2863                                 fcport->flags &= ~FCF_PERSISTENT_BOUND;
2864                                 break;
2865                         }
2866
2867                         /*
2868                          * Port ID changed or device was marked to be updated;
2869                          * Log it out if still logged in and mark it for
2870                          * relogin later.
2871                          */
2872                         fcport->d_id.b24 = new_fcport->d_id.b24;
2873                         fcport->flags |= FCF_LOGIN_NEEDED;
2874                         if (fcport->loop_id != FC_NO_LOOP_ID &&
2875                             (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2876                             fcport->port_type != FCT_INITIATOR &&
2877                             fcport->port_type != FCT_BROADCAST) {
2878                                 ha->isp_ops->fabric_logout(ha, fcport->loop_id,
2879                                     fcport->d_id.b.domain, fcport->d_id.b.area,
2880                                     fcport->d_id.b.al_pa);
2881                                 fcport->loop_id = FC_NO_LOOP_ID;
2882                         }
2883
2884                         break;
2885                 }
2886
2887                 if (found)
2888                         continue;
2889
2890                 /* If device was not in our fcports list, then add it. */
2891                 list_add_tail_rcu(&new_fcport->list, new_fcports);
2892
2893                 /* Allocate a new replacement fcport. */
2894                 nxt_d_id.b24 = new_fcport->d_id.b24;
2895                 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2896                 if (new_fcport == NULL) {
2897                         kfree(swl);
2898                         return (QLA_MEMORY_ALLOC_FAILED);
2899                 }
2900                 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2901                 new_fcport->d_id.b24 = nxt_d_id.b24;
2902                 new_fcport->vp_idx = ha->vp_idx;
2903         }
2904
2905         kfree(swl);
2906         kfree(new_fcport);
2907
2908         if (!list_empty(new_fcports))
2909                 ha->device_flags |= DFLG_FABRIC_DEVICES;
2910
2911         return (rval);
2912 }
2913
2914 /*
2915  * qla2x00_find_new_loop_id
2916  *      Scan through our port list and find a new usable loop ID.
2917  *
2918  * Input:
2919  *      ha:     adapter state pointer.
2920  *      dev:    port structure pointer.
2921  *
2922  * Returns:
2923  *      qla2x00 local function return status code.
2924  *
2925  * Context:
2926  *      Kernel context.
2927  */
2928 static int
2929 qla2x00_find_new_loop_id(scsi_qla_host_t *ha, fc_port_t *dev)
2930 {
2931         int     rval;
2932         int     found;
2933         fc_port_t *fcport;
2934         uint16_t first_loop_id;
2935         scsi_qla_host_t *pha = to_qla_parent(ha);
2936
2937         rval = QLA_SUCCESS;
2938
2939         /* Save starting loop ID. */
2940         first_loop_id = dev->loop_id;
2941
2942         for (;;) {
2943                 /* Skip loop ID if already used by adapter. */
2944                 if (dev->loop_id == ha->loop_id) {
2945                         dev->loop_id++;
2946                 }
2947
2948                 /* Skip reserved loop IDs. */
2949                 while (qla2x00_is_reserved_id(ha, dev->loop_id)) {
2950                         dev->loop_id++;
2951                 }
2952
2953                 /* Reset loop ID if passed the end. */
2954                 if (dev->loop_id > ha->last_loop_id) {
2955                         /* first loop ID. */
2956                         dev->loop_id = ha->min_external_loopid;
2957                 }
2958
2959                 /* Check for loop ID being already in use. */
2960                 found = 0;
2961                 fcport = NULL;
2962                 list_for_each_entry_rcu(fcport, &pha->fcports, list) {
2963                         if (fcport->loop_id == dev->loop_id && fcport != dev) {
2964                                 /* ID possibly in use */
2965                                 found++;
2966                                 break;
2967                         }
2968                 }
2969
2970                 /* If not in use then it is free to use. */
2971                 if (!found) {
2972                         break;
2973                 }
2974
2975                 /* ID in use. Try next value. */
2976                 dev->loop_id++;
2977
2978                 /* If wrap around. No free ID to use. */
2979                 if (dev->loop_id == first_loop_id) {
2980                         dev->loop_id = FC_NO_LOOP_ID;
2981                         rval = QLA_FUNCTION_FAILED;
2982                         break;
2983                 }
2984         }
2985
2986         return (rval);
2987 }
2988
2989 /*
2990  * qla2x00_device_resync
2991  *      Marks devices in the database that needs resynchronization.
2992  *
2993  * Input:
2994  *      ha = adapter block pointer.
2995  *
2996  * Context:
2997  *      Kernel context.
2998  */
2999 static int
3000 qla2x00_device_resync(scsi_qla_host_t *ha)
3001 {
3002         int     rval;
3003         uint32_t mask;
3004         fc_port_t *fcport;
3005         uint32_t rscn_entry;
3006         uint8_t rscn_out_iter;
3007         uint8_t format;
3008         port_id_t d_id;
3009         scsi_qla_host_t *pha = to_qla_parent(ha);
3010
3011         rval = QLA_RSCNS_HANDLED;
3012
3013         while (ha->rscn_out_ptr != ha->rscn_in_ptr ||
3014             ha->flags.rscn_queue_overflow) {
3015
3016                 rscn_entry = ha->rscn_queue[ha->rscn_out_ptr];
3017                 format = MSB(MSW(rscn_entry));
3018                 d_id.b.domain = LSB(MSW(rscn_entry));
3019                 d_id.b.area = MSB(LSW(rscn_entry));
3020                 d_id.b.al_pa = LSB(LSW(rscn_entry));
3021
3022                 DEBUG(printk("scsi(%ld): RSCN queue entry[%d] = "
3023                     "[%02x/%02x%02x%02x].\n",
3024                     ha->host_no, ha->rscn_out_ptr, format, d_id.b.domain,
3025                     d_id.b.area, d_id.b.al_pa));
3026
3027                 ha->rscn_out_ptr++;
3028                 if (ha->rscn_out_ptr == MAX_RSCN_COUNT)
3029                         ha->rscn_out_ptr = 0;
3030
3031                 /* Skip duplicate entries. */
3032                 for (rscn_out_iter = ha->rscn_out_ptr;
3033                     !ha->flags.rscn_queue_overflow &&
3034                     rscn_out_iter != ha->rscn_in_ptr;
3035                     rscn_out_iter = (rscn_out_iter ==
3036                         (MAX_RSCN_COUNT - 1)) ? 0: rscn_out_iter + 1) {
3037
3038                         if (rscn_entry != ha->rscn_queue[rscn_out_iter])
3039                                 break;
3040
3041                         DEBUG(printk("scsi(%ld): Skipping duplicate RSCN queue "
3042                             "entry found at [%d].\n", ha->host_no,
3043                             rscn_out_iter));
3044
3045                         ha->rscn_out_ptr = rscn_out_iter;
3046                 }
3047
3048                 /* Queue overflow, set switch default case. */
3049                 if (ha->flags.rscn_queue_overflow) {
3050                         DEBUG(printk("scsi(%ld): device_resync: rscn "
3051                             "overflow.\n", ha->host_no));
3052
3053                         format = 3;
3054                         ha->flags.rscn_queue_overflow = 0;
3055                 }
3056
3057                 switch (format) {
3058                 case 0:
3059                         mask = 0xffffff;
3060                         break;
3061                 case 1:
3062                         mask = 0xffff00;
3063                         break;
3064                 case 2:
3065                         mask = 0xff0000;
3066                         break;
3067                 default:
3068                         mask = 0x0;
3069                         d_id.b24 = 0;
3070                         ha->rscn_out_ptr = ha->rscn_in_ptr;
3071                         break;
3072                 }
3073
3074                 rval = QLA_SUCCESS;
3075
3076                 list_for_each_entry_rcu(fcport, &pha->fcports, list) {
3077                         if (fcport->vp_idx != ha->vp_idx)
3078                                 continue;
3079
3080                         if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
3081                             (fcport->d_id.b24 & mask) != d_id.b24 ||
3082                             fcport->port_type == FCT_BROADCAST)
3083                                 continue;
3084
3085                         if (atomic_read(&fcport->state) == FCS_ONLINE) {
3086 #ifndef CONFIG_SCSI_QLA2XXX_TARGET
3087                                 if (format != 3 ||
3088                                     fcport->port_type != FCT_INITIATOR)
3089 #endif
3090                                         qla2x00_mark_device_lost(ha, fcport,
3091                                             0, 0);
3092                         }
3093                         fcport->flags &= ~FCF_FARP_DONE;
3094                 }
3095         }
3096         return (rval);
3097 }
3098
3099 /*
3100  * qla2x00_fabric_dev_login
3101  *      Login fabric target device and update FC port database.
3102  *
3103  * Input:
3104  *      ha:             adapter state pointer.
3105  *      fcport:         port structure list pointer.
3106  *      next_loopid:    contains value of a new loop ID that can be used
3107  *                      by the next login attempt.
3108  *
3109  * Returns:
3110  *      qla2x00 local function return status code.
3111  *
3112  * Context:
3113  *      Kernel context.
3114  */
3115 static int
3116 qla2x00_fabric_dev_login(scsi_qla_host_t *ha, fc_port_t *fcport,
3117     uint16_t *next_loopid)
3118 {
3119         int     rval;
3120         int     retry;
3121         uint8_t opts;
3122
3123         rval = QLA_SUCCESS;
3124         retry = 0;
3125
3126         rval = qla2x00_fabric_login(ha, fcport, next_loopid);
3127         if (rval == QLA_SUCCESS) {
3128                 /* Send an ADISC to tape devices.*/
3129                 opts = 0;
3130                 if (fcport->flags & FCF_TAPE_PRESENT)
3131                         opts |= BIT_1;
3132                 rval = qla2x00_get_port_database(ha, fcport, opts);
3133                 if (rval != QLA_SUCCESS) {
3134                         ha->isp_ops->fabric_logout(ha, fcport->loop_id,
3135                             fcport->d_id.b.domain, fcport->d_id.b.area,
3136                             fcport->d_id.b.al_pa);
3137                         qla2x00_mark_device_lost(ha, fcport, 1, 0);
3138                 } else {
3139                         qla2x00_update_fcport(ha, fcport);
3140                 }
3141         }
3142
3143         return (rval);
3144 }
3145
3146 /*
3147  * qla2x00_fabric_login
3148  *      Issue fabric login command.
3149  *
3150  * Input:
3151  *      ha = adapter block pointer.
3152  *      device = pointer to FC device type structure.
3153  *
3154  * Returns:
3155  *      0 - Login successfully
3156  *      1 - Login failed
3157  *      2 - Initiator device
3158  *      3 - Fatal error
3159  */
3160 int
3161 qla2x00_fabric_login(scsi_qla_host_t *ha, fc_port_t *fcport,
3162     uint16_t *next_loopid)
3163 {
3164         int     rval;
3165         int     retry;
3166         uint16_t tmp_loopid;
3167         uint16_t mb[MAILBOX_REGISTER_COUNT];
3168
3169         retry = 0;
3170         tmp_loopid = 0;
3171
3172         for (;;) {
3173                 DEBUG(printk("scsi(%ld): Trying Fabric Login w/loop id 0x%04x "
3174                     "for port %02x%02x%02x.\n",
3175                     ha->host_no, fcport->loop_id, fcport->d_id.b.domain,
3176                     fcport->d_id.b.area, fcport->d_id.b.al_pa));
3177
3178                 /* Login fcport on switch. */
3179                 ha->isp_ops->fabric_login(ha, fcport->loop_id,
3180                     fcport->d_id.b.domain, fcport->d_id.b.area,
3181                     fcport->d_id.b.al_pa, mb, BIT_0);
3182                 if (mb[0] == MBS_PORT_ID_USED) {
3183                         /*
3184                          * Device has another loop ID.  The firmware team
3185                          * recommends the driver perform an implicit login with
3186                          * the specified ID again. The ID we just used is save
3187                          * here so we return with an ID that can be tried by
3188                          * the next login.
3189                          */
3190                         retry++;
3191                         tmp_loopid = fcport->loop_id;
3192                         fcport->loop_id = mb[1];
3193
3194                         DEBUG(printk("Fabric Login: port in use - next "
3195                             "loop id=0x%04x, port Id=%02x%02x%02x.\n",
3196                             fcport->loop_id, fcport->d_id.b.domain,
3197                             fcport->d_id.b.area, fcport->d_id.b.al_pa));
3198
3199                 } else if (mb[0] == MBS_COMMAND_COMPLETE) {
3200                         /*
3201                          * Login succeeded.
3202                          */
3203                         if (retry) {
3204                                 /* A retry occurred before. */
3205                                 *next_loopid = tmp_loopid;
3206                         } else {
3207                                 /*
3208                                  * No retry occurred before. Just increment the
3209                                  * ID value for next login.
3210                                  */
3211                                 *next_loopid = (fcport->loop_id + 1);
3212                         }
3213
3214                         if (mb[1] & BIT_0) {
3215                                 fcport->port_type = FCT_INITIATOR;
3216                         } else {
3217                                 fcport->port_type = FCT_TARGET;
3218                                 if (mb[1] & BIT_1) {
3219                                         fcport->flags |= FCF_TAPE_PRESENT;
3220                                 }
3221                         }
3222
3223                         if (mb[10] & BIT_0)
3224                                 fcport->supported_classes |= FC_COS_CLASS2;
3225                         if (mb[10] & BIT_1)
3226                                 fcport->supported_classes |= FC_COS_CLASS3;
3227
3228                         if (IS_FWI2_CAPABLE(ha)) {
3229                                 if (mb[10] & BIT_7)
3230                                         fcport->conf_compl_supported = 1;
3231                         } else {
3232                                 /* mb[10] bits are undocumented, ToDo */
3233                         }
3234
3235                         rval = QLA_SUCCESS;
3236                         break;
3237                 } else if (mb[0] == MBS_LOOP_ID_USED) {
3238                         /*
3239                          * Loop ID already used, try next loop ID.
3240                          */
3241                         fcport->loop_id++;
3242                         rval = qla2x00_find_new_loop_id(ha, fcport);
3243                         if (rval != QLA_SUCCESS) {
3244                                 /* Ran out of loop IDs to use */
3245                                 break;
3246                         }
3247                 } else if (mb[0] == MBS_COMMAND_ERROR) {
3248                         /*
3249                          * Firmware possibly timed out during login. If NO
3250                          * retries are left to do then the device is declared
3251                          * dead.
3252                          */
3253                         *next_loopid = fcport->loop_id;
3254                         ha->isp_ops->fabric_logout(ha, fcport->loop_id,
3255                             fcport->d_id.b.domain, fcport->d_id.b.area,
3256                             fcport->d_id.b.al_pa);
3257                         qla2x00_mark_device_lost(ha, fcport, 1, 0);
3258
3259                         rval = 1;
3260                         break;
3261                 } else {
3262                         /*
3263                          * unrecoverable / not handled error
3264                          */
3265                         DEBUG2(printk("%s(%ld): failed=%x port_id=%02x%02x%02x "
3266                             "loop_id=%x jiffies=%lx.\n",
3267                             __func__, ha->host_no, mb[0],
3268                             fcport->d_id.b.domain, fcport->d_id.b.area,
3269                             fcport->d_id.b.al_pa, fcport->loop_id, jiffies));
3270
3271                         *next_loopid = fcport->loop_id;
3272                         ha->isp_ops->fabric_logout(ha, fcport->loop_id,
3273                             fcport->d_id.b.domain, fcport->d_id.b.area,
3274                             fcport->d_id.b.al_pa);
3275                         fcport->loop_id = FC_NO_LOOP_ID;
3276                         fcport->login_retry = 0;
3277
3278                         rval = 3;
3279                         break;
3280                 }
3281         }
3282
3283         return (rval);
3284 }
3285
3286 /*
3287  * qla2x00_local_device_login
3288  *      Issue local device login command.
3289  *
3290  * Input:
3291  *      ha = adapter block pointer.
3292  *      loop_id = loop id of device to login to.
3293  *
3294  * Returns (Where's the #define!!!!):
3295  *      0 - Login successfully
3296  *      1 - Login failed
3297  *      3 - Fatal error
3298  */
3299 int
3300 qla2x00_local_device_login(scsi_qla_host_t *ha, fc_port_t *fcport)
3301 {
3302         int             rval;
3303         uint16_t        mb[MAILBOX_REGISTER_COUNT];
3304
3305         memset(mb, 0, sizeof(mb));
3306         rval = qla2x00_login_local_device(ha, fcport, mb, BIT_0);
3307         if (rval == QLA_SUCCESS) {
3308                 /* Interrogate mailbox registers for any errors */
3309                 if (mb[0] == MBS_COMMAND_ERROR)
3310                         rval = 1;
3311                 else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
3312                         /* device not in PCB table */
3313                         rval = 3;
3314         }
3315
3316         return (rval);
3317 }
3318
3319 /*
3320  *  qla2x00_loop_resync
3321  *      Resync with fibre channel devices.
3322  *
3323  * Input:
3324  *      ha = adapter block pointer.
3325  *
3326  * Returns:
3327  *      0 = success
3328  */
3329 int
3330 qla2x00_loop_resync(scsi_qla_host_t *ha)
3331 {
3332         int   rval;
3333         uint32_t wait_time;
3334
3335         rval = QLA_SUCCESS;
3336
3337         atomic_set(&ha->loop_state, LOOP_UPDATE);
3338         clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3339         if (ha->flags.online) {
3340                 if (!(rval = qla2x00_fw_ready(ha))) {
3341                         /* Wait at most MAX_TARGET RSCNs for a stable link. */
3342                         wait_time = 256;
3343                         do {
3344                                 atomic_set(&ha->loop_state, LOOP_UPDATE);
3345
3346                                 /* Issue a marker after FW becomes ready. */
3347                                 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
3348                                 ha->marker_needed = 0;
3349
3350                                 /* Remap devices on Loop. */
3351                                 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
3352
3353                                 qla2x00_configure_loop(ha);
3354                                 wait_time--;
3355                         } while (!atomic_read(&ha->loop_down_timer) &&
3356                                 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
3357                                 wait_time &&
3358                                 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
3359                 }
3360         }
3361
3362         if (test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
3363                 return (QLA_FUNCTION_FAILED);
3364         }
3365
3366         if (rval != QLA_SUCCESS)
3367                 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
3368
3369         return (rval);
3370 }
3371
3372 void
3373 qla2x00_update_fcports(scsi_qla_host_t *ha)
3374 {
3375         fc_port_t *fcport;
3376
3377         /* Go with deferred removal of rport references. */
3378         list_for_each_entry_rcu(fcport, &ha->fcports, list) {
3379                 if (fcport->drport)
3380                         qla2x00_rport_del(fcport);
3381         }
3382 }
3383
3384 /*
3385 *  qla2x00_abort_isp
3386 *      Resets ISP and aborts all outstanding commands.
3387 *
3388 * Input:
3389 *      ha           = adapter block pointer.
3390 *
3391 * Returns:
3392 *      0 = success
3393 */
3394 int
3395 qla2x00_abort_isp(scsi_qla_host_t *ha)
3396 {
3397         int rval;
3398         uint8_t        status = 0;
3399
3400         if (ha->flags.online) {
3401                 ha->flags.online = 0;
3402                 clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
3403
3404                 qla_printk(KERN_INFO, ha,
3405                     "Performing ISP abort - ha= %p.\n", ha);
3406                 ha->isp_ops->reset_chip(ha);
3407
3408                 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
3409                 if (atomic_read(&ha->loop_state) != LOOP_DOWN) {
3410                         atomic_set(&ha->loop_state, LOOP_DOWN);
3411                         qla2x00_mark_all_devices_lost(ha, 0);
3412                 } else {
3413                         if (!atomic_read(&ha->loop_down_timer))
3414                                 atomic_set(&ha->loop_down_timer,
3415                                     LOOP_DOWN_TIME);
3416                 }
3417
3418                 /* Requeue all commands in outstanding command list. */
3419                 qla2x00_abort_all_cmds(ha, DID_RESET << 16);
3420
3421                 ha->isp_ops->get_flash_version(ha, ha->request_ring);
3422
3423                 ha->isp_ops->nvram_config(ha);
3424
3425                 if (!qla2x00_restart_isp(ha)) {
3426                         clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3427
3428                         if (!atomic_read(&ha->loop_down_timer)) {
3429                                 /*
3430                                  * Issue marker command only when we are going
3431                                  * to start the I/O .
3432                                  */
3433                                 ha->marker_needed = 1;
3434                         }
3435
3436                         ha->flags.online = 1;
3437
3438 #ifdef CONFIG_SCSI_QLA2XXX_TARGET
3439                         /* Enable target response to SCSI bus. */
3440                         if (qla_tgt_mode_enabled(ha))
3441                                 qla2x00_send_enable_lun(ha, true);
3442 #endif
3443
3444                         ha->isp_ops->enable_intrs(ha);
3445
3446                         ha->isp_abort_cnt = 0;
3447                         clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3448
3449                         if (ha->eft) {
3450                                 memset(ha->eft, 0, EFT_SIZE);
3451                                 rval = qla2x00_enable_eft_trace(ha,
3452                                     ha->eft_dma, EFT_NUM_BUFFERS);
3453                                 if (rval) {
3454                                         qla_printk(KERN_WARNING, ha,
3455                                             "Unable to reinitialize EFT "
3456                                             "(%d).\n", rval);
3457                                 }
3458                         }
3459
3460                         if (ha->fce) {
3461                                 ha->flags.fce_enabled = 1;
3462                                 memset(ha->fce, 0,
3463                                     fce_calc_size(ha->fce_bufs));
3464                                 rval = qla2x00_enable_fce_trace(ha,
3465                                     ha->fce_dma, ha->fce_bufs, ha->fce_mb,
3466                                     &ha->fce_bufs);
3467                                 if (rval) {
3468                                         qla_printk(KERN_WARNING, ha,
3469                                             "Unable to reinitialize FCE "
3470                                             "(%d).\n", rval);
3471                                         ha->flags.fce_enabled = 0;
3472                                 }
3473                         }
3474                 } else {        /* failed the ISP abort */
3475                         ha->flags.online = 1;
3476                         if (test_bit(ISP_ABORT_RETRY, &ha->dpc_flags)) {
3477                                 if (ha->isp_abort_cnt == 0) {
3478                                         qla_printk(KERN_WARNING, ha,
3479                                             "ISP error recovery failed - "
3480                                             "board disabled\n");
3481                                         /*
3482                                          * The next call disables the board
3483                                          * completely.
3484                                          */
3485                                         ha->isp_ops->reset_adapter(ha);
3486                                         ha->flags.online = 0;
3487                                         clear_bit(ISP_ABORT_RETRY,
3488                                             &ha->dpc_flags);
3489                                         status = 0;
3490                                 } else { /* schedule another ISP abort */
3491                                         ha->isp_abort_cnt--;
3492                                         DEBUG(printk("qla%ld: ISP abort - "
3493                                             "retry remaining %d\n",
3494                                             ha->host_no, ha->isp_abort_cnt));
3495                                         status = 1;
3496                                 }
3497                         } else {
3498                                 ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
3499                                 DEBUG(printk("qla2x00(%ld): ISP error recovery "
3500                                     "- retrying (%d) more times\n",
3501                                     ha->host_no, ha->isp_abort_cnt));
3502                                 set_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3503                                 status = 1;
3504                         }
3505                 }
3506
3507         }
3508
3509         if (status) {
3510                 qla_printk(KERN_INFO, ha,
3511                         "qla2x00_abort_isp: **** FAILED ****\n");
3512         } else {
3513                 DEBUG(printk(KERN_INFO
3514                                 "qla2x00_abort_isp(%ld): exiting.\n",
3515                                 ha->host_no));
3516         }
3517
3518         return(status);
3519 }
3520
3521 /*
3522 *  qla2x00_restart_isp
3523 *      restarts the ISP after a reset
3524 *
3525 * Input:
3526 *      ha = adapter block pointer.
3527 *
3528 * Returns:
3529 *      0 = success
3530 */
3531 static int
3532 qla2x00_restart_isp(scsi_qla_host_t *ha)
3533 {
3534         uint8_t         status = 0;
3535         uint32_t wait_time;
3536
3537         /* If firmware needs to be loaded */
3538         if (qla2x00_isp_firmware(ha)) {
3539                 ha->flags.online = 0;
3540                 if (!(status = ha->isp_ops->chip_diag(ha)))
3541                         status = qla2x00_setup_chip(ha);
3542         }
3543
3544         if (!status && !(status = qla2x00_init_rings(ha))) {
3545                 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3546                 if (!(status = qla2x00_fw_ready(ha))) {
3547                         DEBUG(printk("%s(): Start configure loop, "
3548                             "status = %d\n", __func__, status));
3549
3550                         /* Issue a marker after FW becomes ready. */
3551                         qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
3552
3553                         ha->flags.online = 1;
3554                         /* Wait at most MAX_TARGET RSCNs for a stable link. */
3555                         wait_time = 256;
3556                         do {
3557                                 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
3558                                 qla2x00_configure_loop(ha);
3559                                 wait_time--;
3560                         } while (!atomic_read(&ha->loop_down_timer) &&
3561                                 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
3562                                 wait_time &&
3563                                 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
3564                 }
3565
3566                 /* if no cable then assume it's good */
3567                 if ((ha->device_flags & DFLG_NO_CABLE))
3568                         status = 0;