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