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