17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*ae85eacfSBarry Harding  * Common Development and Distribution License (the "License").
6*ae85eacfSBarry Harding  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*ae85eacfSBarry Harding  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * hci1394_attach.c
287c478bd9Sstevel@tonic-gate  *    HBA attach() routine with associated funtions.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
337c478bd9Sstevel@tonic-gate #include <sys/conf.h>
347c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
357c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
367c478bd9Sstevel@tonic-gate #include <sys/stat.h>
377c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
387c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
397c478bd9Sstevel@tonic-gate #include <sys/pci.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include <sys/1394/h1394.h>
427c478bd9Sstevel@tonic-gate #include <sys/1394/adapters/hci1394.h>
437c478bd9Sstevel@tonic-gate #include <sys/1394/adapters/hci1394_extern.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate  * Attach State Information. These states are used to track the status of the
487c478bd9Sstevel@tonic-gate  * attach.  They are bit offsets.
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate #define	STATE_ZALLOC		0
517c478bd9Sstevel@tonic-gate #define	STATE_ISR_INIT		1
527c478bd9Sstevel@tonic-gate #define	STATE_MINOR_NODE	2
537c478bd9Sstevel@tonic-gate #define	STATE_HW_INIT		3
547c478bd9Sstevel@tonic-gate #define	STATE_PHASE2		4
557c478bd9Sstevel@tonic-gate #define	STATE_POWER_INIT	5
567c478bd9Sstevel@tonic-gate #define	STATE_H1394_ATTACH	6
577c478bd9Sstevel@tonic-gate #define	STATE_ISR_HANDLER	7
587c478bd9Sstevel@tonic-gate #define	STATE_STARTUP		8
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate static void hci1394_statebit_set(uint64_t *state, uint_t statebit);
617c478bd9Sstevel@tonic-gate static boolean_t hci1394_statebit_tst(uint64_t state, uint_t statebit);
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate static void hci1394_cleanup(hci1394_state_t *soft_state, uint64_t attach_state);
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate static int hci1394_hardware_init(hci1394_state_t *soft_state);
667c478bd9Sstevel@tonic-gate static int hci1394_hardware_resume(hci1394_state_t *soft_state);
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate static int hci1394_pci_init(hci1394_state_t *soft_state);
697c478bd9Sstevel@tonic-gate static void hci1394_pci_resume(hci1394_state_t *soft_state);
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate static void hci1394_soft_state_phase1_init(hci1394_state_t *soft_state,
727c478bd9Sstevel@tonic-gate     dev_info_t *dip, int instance);
737c478bd9Sstevel@tonic-gate static void hci1394_soft_state_phase2_init(hci1394_state_t *soft_state);
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate static int hci1394_resmap_get(hci1394_state_t *soft_state);
767c478bd9Sstevel@tonic-gate static void hci1394_resmap_free(hci1394_state_t *soft_state);
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate int
hci1394_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)817c478bd9Sstevel@tonic-gate hci1394_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
827c478bd9Sstevel@tonic-gate {
837c478bd9Sstevel@tonic-gate 	hci1394_state_t *soft_state;
847c478bd9Sstevel@tonic-gate 	uint64_t attach_state = 0;
857c478bd9Sstevel@tonic-gate 	int instance;
867c478bd9Sstevel@tonic-gate 	int status;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	switch (cmd) {
897c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
907c478bd9Sstevel@tonic-gate 		instance = ddi_get_instance(dip);
917c478bd9Sstevel@tonic-gate 		status = ddi_soft_state_zalloc(hci1394_statep, instance);
927c478bd9Sstevel@tonic-gate 		if (status != DDI_SUCCESS) {
937c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
947c478bd9Sstevel@tonic-gate 		}
957c478bd9Sstevel@tonic-gate 		soft_state = ddi_get_soft_state(hci1394_statep, instance);
967c478bd9Sstevel@tonic-gate 		if (soft_state == NULL) {
977c478bd9Sstevel@tonic-gate 			ddi_soft_state_free(hci1394_statep, instance);
987c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
997c478bd9Sstevel@tonic-gate 		}
1007c478bd9Sstevel@tonic-gate 		hci1394_statebit_set(&attach_state, STATE_ZALLOC);
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 		hci1394_soft_state_phase1_init(soft_state, dip, instance);
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 		/* get iblock cookie, other interrupt init stuff */
1057c478bd9Sstevel@tonic-gate 		status = hci1394_isr_init(soft_state);
1067c478bd9Sstevel@tonic-gate 		if (status != DDI_SUCCESS) {
1077c478bd9Sstevel@tonic-gate 			hci1394_cleanup(soft_state, attach_state);
1087c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
1097c478bd9Sstevel@tonic-gate 		}
1107c478bd9Sstevel@tonic-gate 		hci1394_statebit_set(&attach_state, STATE_ISR_INIT);
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 		status = ddi_create_minor_node(dip, "devctl", S_IFCHR,
1137c478bd9Sstevel@tonic-gate 		    instance, DDI_NT_NEXUS, 0);
1147c478bd9Sstevel@tonic-gate 		if (status != DDI_SUCCESS) {
1157c478bd9Sstevel@tonic-gate 			hci1394_cleanup(soft_state, attach_state);
1167c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
1177c478bd9Sstevel@tonic-gate 		}
1187c478bd9Sstevel@tonic-gate 		hci1394_statebit_set(&attach_state, STATE_MINOR_NODE);
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 		status = hci1394_hardware_init(soft_state);
1217c478bd9Sstevel@tonic-gate 		if (status != DDI_SUCCESS) {
1227c478bd9Sstevel@tonic-gate 			hci1394_cleanup(soft_state, attach_state);
1237c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
1247c478bd9Sstevel@tonic-gate 		}
1257c478bd9Sstevel@tonic-gate 		hci1394_statebit_set(&attach_state, STATE_HW_INIT);
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 		hci1394_soft_state_phase2_init(soft_state);
1287c478bd9Sstevel@tonic-gate 		hci1394_statebit_set(&attach_state, STATE_PHASE2);
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 		/* build up the reserved addresses map */
1317c478bd9Sstevel@tonic-gate 		status = hci1394_resmap_get(soft_state);
1327c478bd9Sstevel@tonic-gate 		if (status != DDI_SUCCESS) {
1337c478bd9Sstevel@tonic-gate 			hci1394_cleanup(soft_state, attach_state);
1347c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
1357c478bd9Sstevel@tonic-gate 		}
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 		/* "attach" to the Services Layer */
1387c478bd9Sstevel@tonic-gate 		status = h1394_attach(&soft_state->halinfo, DDI_ATTACH,
1397c478bd9Sstevel@tonic-gate 		    &soft_state->drvinfo.di_sl_private);
1407c478bd9Sstevel@tonic-gate 		if (status != DDI_SUCCESS) {
1417c478bd9Sstevel@tonic-gate 			hci1394_resmap_free(soft_state);
1427c478bd9Sstevel@tonic-gate 			hci1394_cleanup(soft_state, attach_state);
1437c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
1447c478bd9Sstevel@tonic-gate 		}
1457c478bd9Sstevel@tonic-gate 		/* free the reserved addresses map */
1467c478bd9Sstevel@tonic-gate 		hci1394_resmap_free(soft_state);
1477c478bd9Sstevel@tonic-gate 
148*ae85eacfSBarry Harding 		hci1394_statebit_set(&attach_state, STATE_H1394_ATTACH);
1497c478bd9Sstevel@tonic-gate 		status = hci1394_isr_handler_init(soft_state);
1507c478bd9Sstevel@tonic-gate 		if (status != DDI_SUCCESS) {
1517c478bd9Sstevel@tonic-gate 			hci1394_cleanup(soft_state, attach_state);
1527c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
1537c478bd9Sstevel@tonic-gate 		}
1547c478bd9Sstevel@tonic-gate 		hci1394_statebit_set(&attach_state, STATE_ISR_HANDLER);
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 		/* Report that driver was loaded */
1577c478bd9Sstevel@tonic-gate 		ddi_report_dev(dip);
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 		/*
1607c478bd9Sstevel@tonic-gate 		 * Turn on link, Reset Bus, enable interrupts.  Should be the
1617c478bd9Sstevel@tonic-gate 		 * last routine called in attach. The statebit for starup must
1627c478bd9Sstevel@tonic-gate 		 * be set before startup is called since startup enables
1637c478bd9Sstevel@tonic-gate 		 * interrupts.
1647c478bd9Sstevel@tonic-gate 		 */
1657c478bd9Sstevel@tonic-gate 		hci1394_statebit_set(&attach_state, STATE_STARTUP);
1667c478bd9Sstevel@tonic-gate 		status = hci1394_ohci_startup(soft_state->ohci);
1677c478bd9Sstevel@tonic-gate 		if (status != DDI_SUCCESS) {
1687c478bd9Sstevel@tonic-gate 			hci1394_cleanup(soft_state, attach_state);
1697c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
1707c478bd9Sstevel@tonic-gate 		}
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	case DDI_RESUME:
1757c478bd9Sstevel@tonic-gate 		instance = ddi_get_instance(dip);
1767c478bd9Sstevel@tonic-gate 		soft_state = ddi_get_soft_state(hci1394_statep, instance);
1777c478bd9Sstevel@tonic-gate 		if (soft_state == NULL) {
1787c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
1797c478bd9Sstevel@tonic-gate 		}
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 		status = hci1394_hardware_resume(soft_state);
1827c478bd9Sstevel@tonic-gate 		if (status != DDI_SUCCESS) {
1837c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
1847c478bd9Sstevel@tonic-gate 		}
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 		/*
1877c478bd9Sstevel@tonic-gate 		 * set our state back to initial.  The next bus reset were
1887c478bd9Sstevel@tonic-gate 		 * about to generate will set us in motion.
1897c478bd9Sstevel@tonic-gate 		 */
1907c478bd9Sstevel@tonic-gate 		soft_state->drvinfo.di_drvstate.ds_state = HCI1394_INITIAL;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 		/* turn on the link, enable interrupts, reset the bus */
1937c478bd9Sstevel@tonic-gate 		status = hci1394_ohci_startup(soft_state->ohci);
1947c478bd9Sstevel@tonic-gate 		if (status != DDI_SUCCESS) {
1957c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
1967c478bd9Sstevel@tonic-gate 		}
1977c478bd9Sstevel@tonic-gate 
198*ae85eacfSBarry Harding 		/* tell the Services Layer that we are resuming */
199*ae85eacfSBarry Harding 		status = h1394_attach(&soft_state->halinfo, DDI_RESUME,
200*ae85eacfSBarry Harding 		    &soft_state->drvinfo.di_sl_private);
201*ae85eacfSBarry Harding 		if (status != DDI_SUCCESS) {
202*ae85eacfSBarry Harding 			return (DDI_FAILURE);
203*ae85eacfSBarry Harding 		}
204*ae85eacfSBarry Harding 
2057c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	default:
2087c478bd9Sstevel@tonic-gate 		break;
2097c478bd9Sstevel@tonic-gate 	}
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate /*
2167c478bd9Sstevel@tonic-gate  * hci1394_soft_state_phase1_init()
2177c478bd9Sstevel@tonic-gate  *    First part soft_state initialization.  This should be called before any
2187c478bd9Sstevel@tonic-gate  *    other initialization routines are called.  Anything that requires cleanup
2197c478bd9Sstevel@tonic-gate  *    on detach or after an attach failure should be setup in phase2 init (i.e.
2207c478bd9Sstevel@tonic-gate  *    mutex's, cv's, etc.)
2217c478bd9Sstevel@tonic-gate  */
2227c478bd9Sstevel@tonic-gate static void
hci1394_soft_state_phase1_init(hci1394_state_t * soft_state,dev_info_t * dip,int instance)2237c478bd9Sstevel@tonic-gate hci1394_soft_state_phase1_init(hci1394_state_t *soft_state, dev_info_t *dip,
2247c478bd9Sstevel@tonic-gate     int instance)
2257c478bd9Sstevel@tonic-gate {
2267c478bd9Sstevel@tonic-gate 	ASSERT(soft_state != NULL);
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	soft_state->drvinfo.di_dip = dip;
2297c478bd9Sstevel@tonic-gate 	soft_state->drvinfo.di_instance = instance;
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	/* current bus generation */
2327c478bd9Sstevel@tonic-gate 	soft_state->drvinfo.di_gencnt = 0;
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	soft_state->drvinfo.di_sl_private = NULL;
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	/* initialize statistics */
2377c478bd9Sstevel@tonic-gate 	soft_state->drvinfo.di_stats.st_bus_reset_count = 0;
2387c478bd9Sstevel@tonic-gate 	soft_state->drvinfo.di_stats.st_selfid_count = 0;
2397c478bd9Sstevel@tonic-gate 	soft_state->drvinfo.di_stats.st_phy_isr = 0;
2407c478bd9Sstevel@tonic-gate 	soft_state->drvinfo.di_stats.st_phy_loop_err = 0;
2417c478bd9Sstevel@tonic-gate 	soft_state->drvinfo.di_stats.st_phy_pwrfail_err = 0;
2427c478bd9Sstevel@tonic-gate 	soft_state->drvinfo.di_stats.st_phy_timeout_err = 0;
2437c478bd9Sstevel@tonic-gate 	soft_state->drvinfo.di_stats.st_phy_portevt_err = 0;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	soft_state->swap_data = B_FALSE;
2467c478bd9Sstevel@tonic-gate 	soft_state->sl_selfid_buf = NULL;
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	/* halinfo is what is passed up to the Services Layer */
2497c478bd9Sstevel@tonic-gate 	soft_state->halinfo.hal_private = soft_state;
2507c478bd9Sstevel@tonic-gate 	soft_state->halinfo.dip = soft_state->drvinfo.di_dip;
2517c478bd9Sstevel@tonic-gate 	soft_state->halinfo.hal_events = hci1394_evts;
2527c478bd9Sstevel@tonic-gate 	soft_state->halinfo.max_generation = OHCI_BUSGEN_MAX;
2537c478bd9Sstevel@tonic-gate 	soft_state->halinfo.addr_map_num_entries = HCI1394_ADDR_MAP_SIZE;
2547c478bd9Sstevel@tonic-gate 	soft_state->halinfo.addr_map = hci1394_addr_map;
2557c478bd9Sstevel@tonic-gate 	hci1394_buf_attr_get(&soft_state->halinfo.dma_attr);
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate /*
2607c478bd9Sstevel@tonic-gate  * hci1394_soft_state_phase2_init()
2617c478bd9Sstevel@tonic-gate  *    Second part of soft_state initialization.  This should be called after a
2627c478bd9Sstevel@tonic-gate  *    successful hardware_init() and before the call to h1394_attach().
2637c478bd9Sstevel@tonic-gate  */
2647c478bd9Sstevel@tonic-gate static void
hci1394_soft_state_phase2_init(hci1394_state_t * soft_state)2657c478bd9Sstevel@tonic-gate hci1394_soft_state_phase2_init(hci1394_state_t *soft_state)
2667c478bd9Sstevel@tonic-gate {
2677c478bd9Sstevel@tonic-gate 	ASSERT(soft_state != NULL);
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	/*
2707c478bd9Sstevel@tonic-gate 	 * Setup our initial driver state.  This requires the HW iblock
2717c478bd9Sstevel@tonic-gate 	 * cookie so this must be setup in phase2_init()
2727c478bd9Sstevel@tonic-gate 	 */
2737c478bd9Sstevel@tonic-gate 	soft_state->drvinfo.di_drvstate.ds_state = HCI1394_INITIAL;
2747c478bd9Sstevel@tonic-gate 	mutex_init(&soft_state->drvinfo.di_drvstate.ds_mutex, NULL,
2757c478bd9Sstevel@tonic-gate 	    MUTEX_DRIVER, soft_state->drvinfo.di_iblock_cookie);
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	/*
2787c478bd9Sstevel@tonic-gate 	 * halinfo.acc_attr tells the services layer what our buffer access
2797c478bd9Sstevel@tonic-gate 	 * attributes are.  drvinfo.di_buf_attr it initialized in pci_init so
2807c478bd9Sstevel@tonic-gate 	 * this must be setup in phase2_init()
2817c478bd9Sstevel@tonic-gate 	 */
2827c478bd9Sstevel@tonic-gate 	soft_state->halinfo.acc_attr = soft_state->drvinfo.di_buf_attr;
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	/*
2857c478bd9Sstevel@tonic-gate 	 * halinfo.hw_interrupt tells the services layer what our
2867c478bd9Sstevel@tonic-gate 	 * iblock_cookie is. drvinfo.di_iblock_cookie is setup in isr_init so
2877c478bd9Sstevel@tonic-gate 	 * this must be setup in phase2_init()
2887c478bd9Sstevel@tonic-gate 	 */
2897c478bd9Sstevel@tonic-gate 	soft_state->halinfo.hw_interrupt = soft_state->drvinfo.di_iblock_cookie;
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	/*
2927c478bd9Sstevel@tonic-gate 	 * Read in our node capabilities.  Since we are calling into csr
2937c478bd9Sstevel@tonic-gate 	 * we must have first called hardware_init().  Therefore, this must
2947c478bd9Sstevel@tonic-gate 	 * be in phase2_init().
2957c478bd9Sstevel@tonic-gate 	 */
2967c478bd9Sstevel@tonic-gate 	hci1394_csr_node_capabilities(soft_state->csr,
2977c478bd9Sstevel@tonic-gate 	    &soft_state->halinfo.node_capabilities);
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	/*
3007c478bd9Sstevel@tonic-gate 	 * Read in our bus capabilities.  Since we are calling into ohci
3017c478bd9Sstevel@tonic-gate 	 * we must have first called hardware_init().  Therefore, this must
3027c478bd9Sstevel@tonic-gate 	 * be in phase2_init().
3037c478bd9Sstevel@tonic-gate 	 */
3047c478bd9Sstevel@tonic-gate 	hci1394_ohci_bus_capabilities(soft_state->ohci,
3057c478bd9Sstevel@tonic-gate 	    &soft_state->halinfo.bus_capabilities);
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	/*
3087c478bd9Sstevel@tonic-gate 	 * Setup our async command overhead. When a target driver or the ARREQ
3097c478bd9Sstevel@tonic-gate 	 * engine allocates a command, the services layer will tack on space
3107c478bd9Sstevel@tonic-gate 	 * for itself and the HAL so we do not have to manage memory for every
3117c478bd9Sstevel@tonic-gate 	 * command.  hal_overhead is how much memory the hal requires to track
3127c478bd9Sstevel@tonic-gate 	 * an async command. Since we are calling into async we must have first
3137c478bd9Sstevel@tonic-gate 	 * called hardware_init().  Therefore, this must be in phase2_init().
3147c478bd9Sstevel@tonic-gate 	 */
3157c478bd9Sstevel@tonic-gate 	soft_state->halinfo.hal_overhead = hci1394_async_cmd_overhead();
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate /*
3207c478bd9Sstevel@tonic-gate  * hci1394_hardware_init()
3217c478bd9Sstevel@tonic-gate  *    Initialize the adapter hardware.  This should be called during
3227c478bd9Sstevel@tonic-gate  *    the initial attach().
3237c478bd9Sstevel@tonic-gate  */
3247c478bd9Sstevel@tonic-gate static int
hci1394_hardware_init(hci1394_state_t * soft_state)3257c478bd9Sstevel@tonic-gate hci1394_hardware_init(hci1394_state_t *soft_state)
3267c478bd9Sstevel@tonic-gate {
3277c478bd9Sstevel@tonic-gate 	int status;
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	ASSERT(soft_state != NULL);
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	/* Initialize PCI config registers */
3337c478bd9Sstevel@tonic-gate 	status = hci1394_pci_init(soft_state);
3347c478bd9Sstevel@tonic-gate 	if (status != DDI_SUCCESS) {
3357c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3367c478bd9Sstevel@tonic-gate 	}
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	/* Initialize the OpenHCI Hardware */
3397c478bd9Sstevel@tonic-gate 	status = hci1394_ohci_init(soft_state, &soft_state->drvinfo,
3407c478bd9Sstevel@tonic-gate 	    &soft_state->ohci);
3417c478bd9Sstevel@tonic-gate 	if (status != DDI_SUCCESS) {
3427c478bd9Sstevel@tonic-gate 		hci1394_pci_fini(soft_state);
3437c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3447c478bd9Sstevel@tonic-gate 	}
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	/* Initialize SW based CSR registers */
3477c478bd9Sstevel@tonic-gate 	hci1394_csr_init(&soft_state->drvinfo, soft_state->ohci,
3487c478bd9Sstevel@tonic-gate 	    &soft_state->csr);
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	/* Initialize the Asynchronous Q's */
3517c478bd9Sstevel@tonic-gate 	status = hci1394_async_init(&soft_state->drvinfo, soft_state->ohci,
3527c478bd9Sstevel@tonic-gate 	    soft_state->csr, &soft_state->async);
3537c478bd9Sstevel@tonic-gate 	if (status != DDI_SUCCESS) {
3547c478bd9Sstevel@tonic-gate 		hci1394_csr_fini(&soft_state->csr);
3557c478bd9Sstevel@tonic-gate 		hci1394_ohci_fini(&soft_state->ohci);
3567c478bd9Sstevel@tonic-gate 		hci1394_pci_fini(soft_state);
3577c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3587c478bd9Sstevel@tonic-gate 	}
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	/* Initialize the Isochronous logic */
3617c478bd9Sstevel@tonic-gate 	hci1394_isoch_init(&soft_state->drvinfo, soft_state->ohci,
3627c478bd9Sstevel@tonic-gate 	    &soft_state->isoch);
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	/* Initialize any Vendor Specific Registers */
3657c478bd9Sstevel@tonic-gate 	status = hci1394_vendor_init(&soft_state->drvinfo, soft_state->ohci,
3667c478bd9Sstevel@tonic-gate 	    &soft_state->vendor_info, &soft_state->vendor);
3677c478bd9Sstevel@tonic-gate 	if (status != DDI_SUCCESS) {
3687c478bd9Sstevel@tonic-gate 		hci1394_isoch_fini(&soft_state->isoch);
3697c478bd9Sstevel@tonic-gate 		hci1394_async_fini(&soft_state->async);
3707c478bd9Sstevel@tonic-gate 		hci1394_csr_fini(&soft_state->csr);
3717c478bd9Sstevel@tonic-gate 		hci1394_ohci_fini(&soft_state->ohci);
3727c478bd9Sstevel@tonic-gate 		hci1394_pci_fini(soft_state);
3737c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3747c478bd9Sstevel@tonic-gate 	}
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
3777c478bd9Sstevel@tonic-gate }
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate /*
3817c478bd9Sstevel@tonic-gate  * hci1394_hardware_resume()
3827c478bd9Sstevel@tonic-gate  *    Resume the adapter HW.  This routine will be called during resume after
3837c478bd9Sstevel@tonic-gate  *    a successful system suspend.  All memory should be in the state it was
3847c478bd9Sstevel@tonic-gate  *    before the suspend.  All we have to do is re-setup the HW.
3857c478bd9Sstevel@tonic-gate  */
3867c478bd9Sstevel@tonic-gate static int
hci1394_hardware_resume(hci1394_state_t * soft_state)3877c478bd9Sstevel@tonic-gate hci1394_hardware_resume(hci1394_state_t *soft_state)
3887c478bd9Sstevel@tonic-gate {
3897c478bd9Sstevel@tonic-gate 	int status;
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	ASSERT(soft_state != NULL);
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	/* re-enable global byte swap (if we using it) */
3957c478bd9Sstevel@tonic-gate 	hci1394_pci_resume(soft_state);
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	/* Re-init the OpenHCI HW */
3987c478bd9Sstevel@tonic-gate 	status = hci1394_ohci_resume(soft_state->ohci);
3997c478bd9Sstevel@tonic-gate 	if (status != DDI_SUCCESS) {
4007c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4017c478bd9Sstevel@tonic-gate 	}
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	/* re-setup our SW based CSR registers */
4047c478bd9Sstevel@tonic-gate 	hci1394_csr_resume(soft_state->csr);
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	/* Re-setup the Async Q's */
4077c478bd9Sstevel@tonic-gate 	status = hci1394_async_resume(soft_state->async);
4087c478bd9Sstevel@tonic-gate 	if (status != DDI_SUCCESS) {
4097c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4107c478bd9Sstevel@tonic-gate 	}
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	/* Re-setup any Vendor Specific Registers */
4137c478bd9Sstevel@tonic-gate 	status = hci1394_vendor_resume(soft_state->vendor);
4147c478bd9Sstevel@tonic-gate 	if (status != DDI_SUCCESS) {
4157c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4167c478bd9Sstevel@tonic-gate 	}
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
4197c478bd9Sstevel@tonic-gate }
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate /*
4237c478bd9Sstevel@tonic-gate  * hci1394_pci_init()
4247c478bd9Sstevel@tonic-gate  *    Map in PCI config space and initialize PCI config space registers.
4257c478bd9Sstevel@tonic-gate  */
4267c478bd9Sstevel@tonic-gate static int
hci1394_pci_init(hci1394_state_t * soft_state)4277c478bd9Sstevel@tonic-gate hci1394_pci_init(hci1394_state_t *soft_state)
4287c478bd9Sstevel@tonic-gate {
4297c478bd9Sstevel@tonic-gate 	int status;
4307c478bd9Sstevel@tonic-gate #ifndef _LITTLE_ENDIAN
4317c478bd9Sstevel@tonic-gate 	uint32_t global_swap;
4327c478bd9Sstevel@tonic-gate #endif
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	ASSERT(soft_state != NULL);
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	/* Setup PCI configuration space */
4387c478bd9Sstevel@tonic-gate 	status = pci_config_setup(soft_state->drvinfo.di_dip,
4397c478bd9Sstevel@tonic-gate 	    &soft_state->pci_config);
4407c478bd9Sstevel@tonic-gate 	if (status != DDI_SUCCESS) {
4417c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4427c478bd9Sstevel@tonic-gate 	}
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate #ifdef _LITTLE_ENDIAN
4467c478bd9Sstevel@tonic-gate 	/* Start of little endian specific code */
4477c478bd9Sstevel@tonic-gate 	soft_state->drvinfo.di_reg_attr.devacc_attr_version =
4487c478bd9Sstevel@tonic-gate 	    DDI_DEVICE_ATTR_V0;
4497c478bd9Sstevel@tonic-gate 	soft_state->drvinfo.di_reg_attr.devacc_attr_endian_flags =
4507c478bd9Sstevel@tonic-gate 	    DDI_STRUCTURE_LE_ACC;
4517c478bd9Sstevel@tonic-gate 	soft_state->drvinfo.di_reg_attr.devacc_attr_dataorder =
4527c478bd9Sstevel@tonic-gate 	    DDI_STRICTORDER_ACC;
4537c478bd9Sstevel@tonic-gate 	soft_state->drvinfo.di_buf_attr.devacc_attr_version =
4547c478bd9Sstevel@tonic-gate 	    DDI_DEVICE_ATTR_V0;
4557c478bd9Sstevel@tonic-gate 	soft_state->drvinfo.di_buf_attr.devacc_attr_endian_flags =
4567c478bd9Sstevel@tonic-gate 	    DDI_STRUCTURE_LE_ACC;
4577c478bd9Sstevel@tonic-gate 	soft_state->drvinfo.di_buf_attr.devacc_attr_dataorder =
4587c478bd9Sstevel@tonic-gate 	    DDI_STRICTORDER_ACC;
4597c478bd9Sstevel@tonic-gate 	soft_state->swap_data = B_TRUE;
4607c478bd9Sstevel@tonic-gate 	/* End of little endian specific code */
4617c478bd9Sstevel@tonic-gate #else
4627c478bd9Sstevel@tonic-gate 	/* Start of big endian specific code */
4637c478bd9Sstevel@tonic-gate 	/* If PCI_Global_Swap bit is not set, try to set it */
4647c478bd9Sstevel@tonic-gate 	global_swap = pci_config_get32(soft_state->pci_config,
4657c478bd9Sstevel@tonic-gate 	    OHCI_PCI_HCI_CONTROL_REG);
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	/* Lets see if the global byte swap feature is supported */
4687c478bd9Sstevel@tonic-gate 	if ((global_swap & OHCI_PCI_GLOBAL_SWAP) == 0) {
4697c478bd9Sstevel@tonic-gate 		global_swap = global_swap | OHCI_PCI_GLOBAL_SWAP;
4707c478bd9Sstevel@tonic-gate 		pci_config_put32(soft_state->pci_config,
4717c478bd9Sstevel@tonic-gate 		    OHCI_PCI_HCI_CONTROL_REG, global_swap);
4727c478bd9Sstevel@tonic-gate 	}
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 	global_swap = pci_config_get32(soft_state->pci_config,
4757c478bd9Sstevel@tonic-gate 	    OHCI_PCI_HCI_CONTROL_REG);
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	/* If PCI_Global_Swap bit is not set, it is unsupported */
4787c478bd9Sstevel@tonic-gate 	if ((global_swap & OHCI_PCI_GLOBAL_SWAP) == 0) {
4797c478bd9Sstevel@tonic-gate 		soft_state->drvinfo.di_reg_attr.devacc_attr_version =
4807c478bd9Sstevel@tonic-gate 		    DDI_DEVICE_ATTR_V0;
4817c478bd9Sstevel@tonic-gate 		soft_state->drvinfo.di_reg_attr.devacc_attr_endian_flags =
4827c478bd9Sstevel@tonic-gate 		    DDI_STRUCTURE_LE_ACC;
4837c478bd9Sstevel@tonic-gate 		soft_state->drvinfo.di_reg_attr.devacc_attr_dataorder =
4847c478bd9Sstevel@tonic-gate 		    DDI_STRICTORDER_ACC;
4857c478bd9Sstevel@tonic-gate 		soft_state->drvinfo.di_buf_attr.devacc_attr_version =
4867c478bd9Sstevel@tonic-gate 		    DDI_DEVICE_ATTR_V0;
4877c478bd9Sstevel@tonic-gate 		soft_state->drvinfo.di_buf_attr.devacc_attr_endian_flags =
4887c478bd9Sstevel@tonic-gate 		    DDI_STRUCTURE_LE_ACC;
4897c478bd9Sstevel@tonic-gate 		soft_state->drvinfo.di_buf_attr.devacc_attr_dataorder =
4907c478bd9Sstevel@tonic-gate 		    DDI_STRICTORDER_ACC;
4917c478bd9Sstevel@tonic-gate 		soft_state->swap_data = B_TRUE;
4927c478bd9Sstevel@tonic-gate 	/*
4937c478bd9Sstevel@tonic-gate 	 * global byte swap is supported.  This should be the case
4947c478bd9Sstevel@tonic-gate 	 * for almost all of the adapters.
4957c478bd9Sstevel@tonic-gate 	 */
4967c478bd9Sstevel@tonic-gate 	} else {
4977c478bd9Sstevel@tonic-gate 		soft_state->drvinfo.di_reg_attr.devacc_attr_version =
4987c478bd9Sstevel@tonic-gate 		    DDI_DEVICE_ATTR_V0;
4997c478bd9Sstevel@tonic-gate 		soft_state->drvinfo.di_reg_attr.devacc_attr_endian_flags =
5007c478bd9Sstevel@tonic-gate 		    DDI_STRUCTURE_BE_ACC;
5017c478bd9Sstevel@tonic-gate 		soft_state->drvinfo.di_reg_attr.devacc_attr_dataorder =
5027c478bd9Sstevel@tonic-gate 		    DDI_STRICTORDER_ACC;
5037c478bd9Sstevel@tonic-gate 		soft_state->drvinfo.di_buf_attr.devacc_attr_version =
5047c478bd9Sstevel@tonic-gate 		    DDI_DEVICE_ATTR_V0;
5057c478bd9Sstevel@tonic-gate 		soft_state->drvinfo.di_buf_attr.devacc_attr_endian_flags =
5067c478bd9Sstevel@tonic-gate 		    DDI_STRUCTURE_BE_ACC;
5077c478bd9Sstevel@tonic-gate 		soft_state->drvinfo.di_buf_attr.devacc_attr_dataorder =
5087c478bd9Sstevel@tonic-gate 		    DDI_STRICTORDER_ACC;
5097c478bd9Sstevel@tonic-gate 		soft_state->swap_data = B_FALSE;
5107c478bd9Sstevel@tonic-gate 	}
5117c478bd9Sstevel@tonic-gate 	/* End of big endian specific code */
5127c478bd9Sstevel@tonic-gate #endif
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	/* read in vendor Information */
5157c478bd9Sstevel@tonic-gate 	soft_state->vendor_info.vendor_id =
5167c478bd9Sstevel@tonic-gate 	    (uint_t)pci_config_get16(soft_state->pci_config, PCI_CONF_VENID);
5177c478bd9Sstevel@tonic-gate 	soft_state->vendor_info.device_id =
5187c478bd9Sstevel@tonic-gate 	    (uint_t)pci_config_get16(soft_state->pci_config, PCI_CONF_DEVID);
5197c478bd9Sstevel@tonic-gate 	soft_state->vendor_info.revision_id =
5207c478bd9Sstevel@tonic-gate 	    (uint_t)pci_config_get8(soft_state->pci_config, PCI_CONF_REVID);
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
5237c478bd9Sstevel@tonic-gate }
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate /*
5277c478bd9Sstevel@tonic-gate  * hci1394_pci_resume()
5287c478bd9Sstevel@tonic-gate  *    Re-Initialize PCI config space registers during a resume.
5297c478bd9Sstevel@tonic-gate  */
5307c478bd9Sstevel@tonic-gate /* ARGSUSED */
5317c478bd9Sstevel@tonic-gate static void
hci1394_pci_resume(hci1394_state_t * soft_state)5327c478bd9Sstevel@tonic-gate hci1394_pci_resume(hci1394_state_t *soft_state)
5337c478bd9Sstevel@tonic-gate {
5347c478bd9Sstevel@tonic-gate #ifndef _LITTLE_ENDIAN
5357c478bd9Sstevel@tonic-gate 	uint32_t global_swap;
5367c478bd9Sstevel@tonic-gate #endif
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	ASSERT(soft_state != NULL);
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate #ifdef _LITTLE_ENDIAN
5427c478bd9Sstevel@tonic-gate 	/* Start of little endian specific code */
5437c478bd9Sstevel@tonic-gate 	/* nothing to do here yet.  Maybe later?? */
5447c478bd9Sstevel@tonic-gate 	/* End of little endian specific code */
5457c478bd9Sstevel@tonic-gate #else
5467c478bd9Sstevel@tonic-gate 	/* Start of big endian specific code */
5477c478bd9Sstevel@tonic-gate 	/* If PCI_Global_Swap bit is not set, try to set it */
5487c478bd9Sstevel@tonic-gate 	global_swap = pci_config_get32(soft_state->pci_config,
5497c478bd9Sstevel@tonic-gate 	    OHCI_PCI_HCI_CONTROL_REG);
5507c478bd9Sstevel@tonic-gate 	/* Try and set GlobalByteSwap */
5517c478bd9Sstevel@tonic-gate 	if ((global_swap & OHCI_PCI_GLOBAL_SWAP) == 0) {
5527c478bd9Sstevel@tonic-gate 		global_swap = global_swap | OHCI_PCI_GLOBAL_SWAP;
5537c478bd9Sstevel@tonic-gate 		pci_config_put32(soft_state->pci_config,
5547c478bd9Sstevel@tonic-gate 		    OHCI_PCI_HCI_CONTROL_REG, global_swap);
5557c478bd9Sstevel@tonic-gate 	}
5567c478bd9Sstevel@tonic-gate 	/* End of big endian specific code */
5577c478bd9Sstevel@tonic-gate #endif
5587c478bd9Sstevel@tonic-gate }
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate /*
5627c478bd9Sstevel@tonic-gate  * hci1394_resmap_get()
5637c478bd9Sstevel@tonic-gate  *    Look for adapter property "reserved-addresses".  This property is used to
5647c478bd9Sstevel@tonic-gate  *    reserve 1394 address space so that it will not randomly be given to a
5657c478bd9Sstevel@tonic-gate  *    target driver during a 1394 address space alloc.  Some protocols hard
5667c478bd9Sstevel@tonic-gate  *    code addresses which make us do this.  The target driver must specifically
5677c478bd9Sstevel@tonic-gate  *    ask for these addresses.  This routine should be called before the
5687c478bd9Sstevel@tonic-gate  *    call to h1394_attach().
5697c478bd9Sstevel@tonic-gate  */
5707c478bd9Sstevel@tonic-gate static int
hci1394_resmap_get(hci1394_state_t * soft_state)5717c478bd9Sstevel@tonic-gate hci1394_resmap_get(hci1394_state_t *soft_state)
5727c478bd9Sstevel@tonic-gate {
5737c478bd9Sstevel@tonic-gate 	h1394_addr_map_t *resv_map;
5747c478bd9Sstevel@tonic-gate 	int resv_num;
5757c478bd9Sstevel@tonic-gate 	int status;
5767c478bd9Sstevel@tonic-gate 	int reslen;
5777c478bd9Sstevel@tonic-gate 	uint32_t *resptr;
5787c478bd9Sstevel@tonic-gate 	int rescnt;
5797c478bd9Sstevel@tonic-gate 	int mapcnt;
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 	ASSERT(soft_state != NULL);
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	/*
5857c478bd9Sstevel@tonic-gate 	 * See if the "reserved-addresses" property is defined.  The format
5867c478bd9Sstevel@tonic-gate 	 * should be:
5877c478bd9Sstevel@tonic-gate 	 *
5887c478bd9Sstevel@tonic-gate 	 * reserved-addresses=	0x0000ffff,0xf0000B00,0x200,
5897c478bd9Sstevel@tonic-gate 	 * 			0x0000ffff,0xf0000D00,0x200,
5907c478bd9Sstevel@tonic-gate 	 * 			0x0000ffff,0xf0000234,0x4;
5917c478bd9Sstevel@tonic-gate 	 * You can have multiple reserved addresses.  Each reserved address
5927c478bd9Sstevel@tonic-gate 	 * takes up 3 integers.
5937c478bd9Sstevel@tonic-gate 	 *    MSWofAddr,LSWofAddr,ByteCount
5947c478bd9Sstevel@tonic-gate 	 */
5957c478bd9Sstevel@tonic-gate 	status = ddi_prop_lookup_int_array(DDI_DEV_T_ANY,
5967c478bd9Sstevel@tonic-gate 	    soft_state->drvinfo.di_dip, DDI_PROP_DONTPASS, "reserved-addresses",
5977c478bd9Sstevel@tonic-gate 	    (int **)&resptr, (uint_t *)&reslen);
5987c478bd9Sstevel@tonic-gate 	if (status != DDI_PROP_SUCCESS) {
5997c478bd9Sstevel@tonic-gate 		/* the property is not defined,  0 reserved addresses */
6007c478bd9Sstevel@tonic-gate 		soft_state->halinfo.resv_map_num_entries = 0;
6017c478bd9Sstevel@tonic-gate 		soft_state->halinfo.resv_map = NULL;
6027c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
6037c478bd9Sstevel@tonic-gate 	} else if ((reslen < 3) || ((reslen % 3) != 0)) {
6047c478bd9Sstevel@tonic-gate 		/*
6057c478bd9Sstevel@tonic-gate 		 * the property is defined but the correct number of integers
6067c478bd9Sstevel@tonic-gate 		 * is not present.
6077c478bd9Sstevel@tonic-gate 		 */
6087c478bd9Sstevel@tonic-gate 		resv_num = 0;
6097c478bd9Sstevel@tonic-gate 		resv_map = NULL;
6107c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!%s(%d): Invalid reserved-addresses property."
6117c478bd9Sstevel@tonic-gate 		    " Property ignored", ddi_node_name(
6127c478bd9Sstevel@tonic-gate 		    soft_state->drvinfo.di_dip), ddi_get_instance(
6137c478bd9Sstevel@tonic-gate 		    soft_state->drvinfo.di_dip));
6147c478bd9Sstevel@tonic-gate 	} else {
6157c478bd9Sstevel@tonic-gate 		/* the property is defined. Alloc space to copy data into */
6167c478bd9Sstevel@tonic-gate 		resv_num = reslen / 3;
6177c478bd9Sstevel@tonic-gate 		resv_map = kmem_alloc((sizeof (h1394_addr_map_t) * (resv_num)),
6187c478bd9Sstevel@tonic-gate 		    KM_SLEEP);
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 		/* read in the address, length, and set the type to reserved */
6217c478bd9Sstevel@tonic-gate 		rescnt = 0;
6227c478bd9Sstevel@tonic-gate 		mapcnt = 0;
6237c478bd9Sstevel@tonic-gate 		while (rescnt < reslen) {
6247c478bd9Sstevel@tonic-gate 			resv_map[mapcnt].address =
6257c478bd9Sstevel@tonic-gate 			    (uint64_t)resptr[rescnt] << 32;
6267c478bd9Sstevel@tonic-gate 			rescnt++;
6277c478bd9Sstevel@tonic-gate 			resv_map[mapcnt].address |= (uint64_t)resptr[rescnt];
6287c478bd9Sstevel@tonic-gate 			rescnt++;
6297c478bd9Sstevel@tonic-gate 			resv_map[mapcnt].length = (uint64_t)resptr[rescnt];
6307c478bd9Sstevel@tonic-gate 			rescnt++;
6317c478bd9Sstevel@tonic-gate 			resv_map[mapcnt].addr_type = H1394_ADDR_RESERVED;
6327c478bd9Sstevel@tonic-gate 			mapcnt++;
6337c478bd9Sstevel@tonic-gate 		}
6347c478bd9Sstevel@tonic-gate 	}
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	ddi_prop_free(resptr);
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	/*
6397c478bd9Sstevel@tonic-gate 	 * copy the number of reserved address ranges and a pointer to the map
6407c478bd9Sstevel@tonic-gate 	 * into halinfo so we can tell the services layer about them in
6417c478bd9Sstevel@tonic-gate 	 * h1394_attach()
6427c478bd9Sstevel@tonic-gate 	 */
6437c478bd9Sstevel@tonic-gate 	soft_state->halinfo.resv_map_num_entries = resv_num;
6447c478bd9Sstevel@tonic-gate 	soft_state->halinfo.resv_map = resv_map;
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
6477c478bd9Sstevel@tonic-gate }
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate /*
6517c478bd9Sstevel@tonic-gate  * hci1394_resmap_free()
6527c478bd9Sstevel@tonic-gate  *    Free up the space alloced in hci1394_resmap_get().  This routine should
6537c478bd9Sstevel@tonic-gate  *    be called after h1394_attach().  The HAL does not need this information
6547c478bd9Sstevel@tonic-gate  *    and the services layer only uses it for a calculation during attach and
6557c478bd9Sstevel@tonic-gate  *    should not refer to the pointer after it returns from h1394_attach().
6567c478bd9Sstevel@tonic-gate  */
6577c478bd9Sstevel@tonic-gate static void
hci1394_resmap_free(hci1394_state_t * soft_state)6587c478bd9Sstevel@tonic-gate hci1394_resmap_free(hci1394_state_t *soft_state)
6597c478bd9Sstevel@tonic-gate {
6607c478bd9Sstevel@tonic-gate 	ASSERT(soft_state != NULL);
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 	/*
6637c478bd9Sstevel@tonic-gate 	 * if we have one or more reserved map entries, free up the space that
6647c478bd9Sstevel@tonic-gate 	 * was allocated to store them
6657c478bd9Sstevel@tonic-gate 	 */
6667c478bd9Sstevel@tonic-gate 	if (soft_state->halinfo.resv_map_num_entries > 0) {
6677c478bd9Sstevel@tonic-gate 		ASSERT(soft_state->halinfo.resv_map != NULL);
6687c478bd9Sstevel@tonic-gate 		kmem_free(soft_state->halinfo.resv_map,
6697c478bd9Sstevel@tonic-gate 		    (sizeof (h1394_addr_map_t) *
6707c478bd9Sstevel@tonic-gate 		    soft_state->halinfo.resv_map_num_entries));
6717c478bd9Sstevel@tonic-gate 	}
6727c478bd9Sstevel@tonic-gate }
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate /*
6767c478bd9Sstevel@tonic-gate  * hci1394_statebit_set()
6777c478bd9Sstevel@tonic-gate  *     Set bit "statebit" in "state"
6787c478bd9Sstevel@tonic-gate  */
6797c478bd9Sstevel@tonic-gate static void
hci1394_statebit_set(uint64_t * state,uint_t statebit)6807c478bd9Sstevel@tonic-gate hci1394_statebit_set(uint64_t *state, uint_t statebit)
6817c478bd9Sstevel@tonic-gate {
6827c478bd9Sstevel@tonic-gate 	ASSERT(state != NULL);
6837c478bd9Sstevel@tonic-gate 	ASSERT(statebit < 64);
6847c478bd9Sstevel@tonic-gate 	*state |= (uint64_t)0x1 << statebit;
6857c478bd9Sstevel@tonic-gate }
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate /*
6897c478bd9Sstevel@tonic-gate  * hci1394_statebit_tst()
6907c478bd9Sstevel@tonic-gate  *    Return status of bit "statebit".  Is it set or not?
6917c478bd9Sstevel@tonic-gate  */
6927c478bd9Sstevel@tonic-gate static boolean_t
hci1394_statebit_tst(uint64_t state,uint_t statebit)6937c478bd9Sstevel@tonic-gate hci1394_statebit_tst(uint64_t state, uint_t statebit)
6947c478bd9Sstevel@tonic-gate {
6957c478bd9Sstevel@tonic-gate 	uint64_t bitset;
6967c478bd9Sstevel@tonic-gate 	int status;
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 	ASSERT(statebit < 64);
7007c478bd9Sstevel@tonic-gate 	bitset = state & ((uint64_t)0x1 << statebit);
7017c478bd9Sstevel@tonic-gate 	if (bitset == 0) {
7027c478bd9Sstevel@tonic-gate 		status = B_FALSE;
7037c478bd9Sstevel@tonic-gate 	} else {
7047c478bd9Sstevel@tonic-gate 		status = B_TRUE;
7057c478bd9Sstevel@tonic-gate 	}
7067c478bd9Sstevel@tonic-gate 	return (status);
7077c478bd9Sstevel@tonic-gate }
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate /*
7117c478bd9Sstevel@tonic-gate  * hci1394_cleanup()
7127c478bd9Sstevel@tonic-gate  *    Cleanup after a failed attach
7137c478bd9Sstevel@tonic-gate  */
7147c478bd9Sstevel@tonic-gate static void
hci1394_cleanup(hci1394_state_t * soft_state,uint64_t attach_state)7157c478bd9Sstevel@tonic-gate hci1394_cleanup(hci1394_state_t *soft_state, uint64_t attach_state)
7167c478bd9Sstevel@tonic-gate {
7177c478bd9Sstevel@tonic-gate 	int status;
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 	ASSERT(soft_state != NULL);
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 	status = hci1394_statebit_tst(attach_state, STATE_STARTUP);
7237c478bd9Sstevel@tonic-gate 	if (status == B_TRUE) {
7247c478bd9Sstevel@tonic-gate 		/* Don't allow the HW to generate any more interrupts */
7257c478bd9Sstevel@tonic-gate 		hci1394_ohci_intr_master_disable(soft_state->ohci);
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 		/* don't accept anymore commands from services layer */
7287c478bd9Sstevel@tonic-gate 		(void) hci1394_state_set(&soft_state->drvinfo,
7297c478bd9Sstevel@tonic-gate 		    HCI1394_SHUTDOWN);
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 		/* Reset the chip */
7327c478bd9Sstevel@tonic-gate 		(void) hci1394_ohci_soft_reset(soft_state->ohci);
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 		/* Flush out async DMA Q's (cancels pendingQ timeouts too) */
7357c478bd9Sstevel@tonic-gate 		hci1394_async_flush(soft_state->async);
7367c478bd9Sstevel@tonic-gate 	}
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 	status = hci1394_statebit_tst(attach_state, STATE_ISR_HANDLER);
7397c478bd9Sstevel@tonic-gate 	if (status == B_TRUE) {
7407c478bd9Sstevel@tonic-gate 		hci1394_isr_handler_fini(soft_state);
7417c478bd9Sstevel@tonic-gate 	}
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 	status = hci1394_statebit_tst(attach_state, STATE_H1394_ATTACH);
7447c478bd9Sstevel@tonic-gate 	if (status == B_TRUE) {
7457c478bd9Sstevel@tonic-gate 		(void) h1394_detach(&soft_state->drvinfo.di_sl_private,
7467c478bd9Sstevel@tonic-gate 		    DDI_DETACH);
7477c478bd9Sstevel@tonic-gate 	}
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	status = hci1394_statebit_tst(attach_state, STATE_HW_INIT);
7507c478bd9Sstevel@tonic-gate 	if (status == B_TRUE) {
7517c478bd9Sstevel@tonic-gate 		hci1394_detach_hardware(soft_state);
7527c478bd9Sstevel@tonic-gate 	}
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	status = hci1394_statebit_tst(attach_state, STATE_MINOR_NODE);
7557c478bd9Sstevel@tonic-gate 	if (status == B_TRUE) {
7567c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(soft_state->drvinfo.di_dip, "devctl");
7577c478bd9Sstevel@tonic-gate 	}
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	status = hci1394_statebit_tst(attach_state, STATE_ISR_INIT);
7607c478bd9Sstevel@tonic-gate 	if (status == B_TRUE) {
7617c478bd9Sstevel@tonic-gate 		hci1394_isr_fini(soft_state);
7627c478bd9Sstevel@tonic-gate 	}
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 	status = hci1394_statebit_tst(attach_state, STATE_PHASE2);
7657c478bd9Sstevel@tonic-gate 	if (status == B_TRUE) {
7667c478bd9Sstevel@tonic-gate 		hci1394_soft_state_fini(soft_state);
7677c478bd9Sstevel@tonic-gate 	}
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 	status = hci1394_statebit_tst(attach_state, STATE_ZALLOC);
7707c478bd9Sstevel@tonic-gate 	if (status == B_TRUE) {
7717c478bd9Sstevel@tonic-gate 		ddi_soft_state_free(hci1394_statep,
7727c478bd9Sstevel@tonic-gate 		    soft_state->drvinfo.di_instance);
7737c478bd9Sstevel@tonic-gate 	}
7747c478bd9Sstevel@tonic-gate }
775