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