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