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