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