xref: /illumos-gate/usr/src/uts/common/io/1394/adapters/hci1394_detach.c (revision d48be21240dfd051b689384ce2b23479d757f2d8)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 
27 /*
28  * hci1394_detach.c
29  *    HBA detach() routine with associated funtions.
30  */
31 
32 #include <sys/types.h>
33 #include <sys/kmem.h>
34 #include <sys/conf.h>
35 #include <sys/ddi.h>
36 #include <sys/modctl.h>
37 #include <sys/stat.h>
38 #include <sys/sunddi.h>
39 
40 #include <sys/1394/h1394.h>
41 #include <sys/1394/adapters/hci1394.h>
42 #include <sys/1394/adapters/hci1394_extern.h>
43 
44 
45 
46 int
47 hci1394_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
48 {
49 	hci1394_state_t *soft_state;
50 
51 	soft_state = ddi_get_soft_state(hci1394_statep, ddi_get_instance(dip));
52 	if (soft_state == NULL) {
53 		return (DDI_FAILURE);
54 	}
55 
56 	switch (cmd) {
57 	case DDI_DETACH:
58 		/* Don't allow the HW to generate any more interrupts */
59 		hci1394_ohci_intr_master_disable(soft_state->ohci);
60 		hci1394_ohci_it_intr_disable(soft_state->ohci, 0xFFFFFFFF);
61 		hci1394_ohci_ir_intr_disable(soft_state->ohci, 0xFFFFFFFF);
62 
63 		/* Clear any pending interrupts - no longer valid */
64 		hci1394_ohci_intr_clear(soft_state->ohci, 0xFFFFFFFF);
65 		hci1394_ohci_it_intr_clear(soft_state->ohci, 0xFFFFFFFF);
66 		hci1394_ohci_ir_intr_clear(soft_state->ohci, 0xFFFFFFFF);
67 
68 		/* Make sure we tell others on the bus we are dropping out */
69 		(void) hci1394_ohci_phy_clr(soft_state->ohci, 4, 0xc0);
70 		ddi_put32(soft_state->ohci->ohci_reg_handle,
71 		    &soft_state->ohci->ohci_regs->link_ctrl_clr,
72 		    0xFFFFFFFF);
73 
74 		/* unregister interrupt handler */
75 		hci1394_isr_handler_fini(soft_state);
76 
77 		/* don't accept anymore commands from services layer */
78 		(void) hci1394_state_set(&soft_state->drvinfo,
79 		    HCI1394_SHUTDOWN);
80 
81 		/* Do a long reset on the bus so every one knows we are gone */
82 		(void) hci1394_ohci_bus_reset_nroot(soft_state->ohci);
83 
84 		/* Reset the OHCI HW */
85 		(void) hci1394_ohci_soft_reset(soft_state->ohci);
86 
87 		/* Flush out async DMA Q's (cancels pendingQ timeouts too) */
88 		hci1394_async_flush(soft_state->async);
89 
90 		(void) h1394_detach(&soft_state->drvinfo.di_sl_private,
91 		    DDI_DETACH);
92 
93 		/* remove the minor node */
94 		ddi_remove_minor_node(dip, "devctl");
95 
96 		/* cleanup */
97 		hci1394_detach_hardware(soft_state);
98 
99 		/* cleanup Solaris interrupt stuff */
100 		hci1394_isr_fini(soft_state);
101 
102 		/* cleanup soft state stuff */
103 		hci1394_soft_state_fini(soft_state);
104 
105 		/* free soft state */
106 		ddi_soft_state_free(hci1394_statep,
107 		    soft_state->drvinfo.di_instance);
108 
109 		return (DDI_SUCCESS);
110 
111 	case DDI_SUSPEND:
112 		/* Don't allow the HW to generate any more interrupts */
113 		hci1394_ohci_intr_master_disable(soft_state->ohci);
114 		hci1394_ohci_it_intr_disable(soft_state->ohci, 0xFFFFFFFF);
115 		hci1394_ohci_ir_intr_disable(soft_state->ohci, 0xFFFFFFFF);
116 
117 		/* Clear any pending interrupts - no longer valid */
118 		hci1394_ohci_intr_clear(soft_state->ohci, 0xFFFFFFFF);
119 		hci1394_ohci_it_intr_clear(soft_state->ohci, 0xFFFFFFFF);
120 		hci1394_ohci_ir_intr_clear(soft_state->ohci, 0xFFFFFFFF);
121 
122 		/* Make sure we tell others on the bus we are dropping out */
123 		(void) hci1394_ohci_phy_clr(soft_state->ohci, 4, 0xc0);
124 		ddi_put32(soft_state->ohci->ohci_reg_handle,
125 		    &soft_state->ohci->ohci_regs->link_ctrl_clr,
126 		    0xFFFFFFFF);
127 
128 		/* don't accept anymore commands from services layer */
129 		(void) hci1394_state_set(&soft_state->drvinfo,
130 		    HCI1394_SHUTDOWN);
131 
132 		/* Do a long reset on the bus so every one knows we are gone */
133 		(void) hci1394_ohci_bus_reset_nroot(soft_state->ohci);
134 
135 		/* Reset the OHCI HW */
136 		(void) hci1394_ohci_soft_reset(soft_state->ohci);
137 
138 		/* Make sure async engine is ready to suspend */
139 		hci1394_async_suspend(soft_state->async);
140 
141 		(void) h1394_detach(&soft_state->drvinfo.di_sl_private,
142 		    DDI_SUSPEND);
143 
144 		return (DDI_SUCCESS);
145 
146 	default:
147 		break;
148 	}
149 
150 	return (DDI_FAILURE);
151 }
152 
153 /*
154  * quiesce(9E) entry point.
155  *
156  * This function is called when the system is single-threaded at high
157  * PIL with preemption disabled. Therefore, this function must not be
158  * blocked.
159  *
160  * This function returns DDI_SUCCESS on success, or DDI_FAILURE on failure.
161  * DDI_FAILURE indicates an error condition and should almost never happen.
162  */
163 int
164 hci1394_quiesce(dev_info_t *dip)
165 {
166 	hci1394_state_t *soft_state;
167 
168 	soft_state = ddi_get_soft_state(hci1394_statep, ddi_get_instance(dip));
169 
170 	if (soft_state == NULL) {
171 		return (DDI_FAILURE);
172 	}
173 
174 	/* Don't allow the HW to generate any more interrupts */
175 	hci1394_ohci_intr_master_disable(soft_state->ohci);
176 	hci1394_ohci_it_intr_disable(soft_state->ohci, 0xFFFFFFFF);
177 	hci1394_ohci_ir_intr_disable(soft_state->ohci, 0xFFFFFFFF);
178 
179 	/* Clear any pending interrupts - no longer valid */
180 	hci1394_ohci_intr_clear(soft_state->ohci, 0xFFFFFFFF);
181 	hci1394_ohci_it_intr_clear(soft_state->ohci, 0xFFFFFFFF);
182 	hci1394_ohci_ir_intr_clear(soft_state->ohci, 0xFFFFFFFF);
183 
184 	/* Make sure we tell others on the bus we are dropping out */
185 	(void) hci1394_ohci_phy_clr(soft_state->ohci, 4, 0xc0);
186 	ddi_put32(soft_state->ohci->ohci_reg_handle,
187 	    &soft_state->ohci->ohci_regs->link_ctrl_clr, 0xFFFFFFFF);
188 
189 	/* Do a long reset on the bus so every one knows we are gone */
190 	(void) hci1394_ohci_bus_reset_nroot(soft_state->ohci);
191 
192 	/* Reset the OHCI HW */
193 	(void) hci1394_ohci_soft_reset(soft_state->ohci);
194 
195 	return (DDI_SUCCESS);
196 }
197 
198 void
199 hci1394_detach_hardware(hci1394_state_t *soft_state)
200 {
201 	ASSERT(soft_state != NULL);
202 
203 	/* free up vendor specific registers */
204 	hci1394_vendor_fini(&soft_state->vendor);
205 
206 	/* cleanup isoch layer */
207 	hci1394_isoch_fini(&soft_state->isoch);
208 
209 	/* cleanup async layer */
210 	hci1394_async_fini(&soft_state->async);
211 
212 	/* Free up csr register space */
213 	hci1394_csr_fini(&soft_state->csr);
214 
215 	/* free up OpenHCI registers */
216 	hci1394_ohci_fini(&soft_state->ohci);
217 
218 	/* free up PCI config space */
219 	hci1394_pci_fini(soft_state);
220 }
221 
222 
223 /*
224  * hci1394_pci_fini()
225  *    Cleanup after a PCI init.
226  */
227 void
228 hci1394_pci_fini(hci1394_state_t *soft_state)
229 {
230 	ASSERT(soft_state != NULL);
231 	pci_config_teardown(&soft_state->pci_config);
232 }
233 
234 
235 /*
236  * hci1394_soft_state_fini()
237  *    Cleanup any mutex's, etc. in soft_state.
238  */
239 void
240 hci1394_soft_state_fini(hci1394_state_t *soft_state)
241 {
242 	ASSERT(soft_state != NULL);
243 	mutex_destroy(&soft_state->drvinfo.di_drvstate.ds_mutex);
244 }
245