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