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