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