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