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