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