xref: /titanic_50/usr/src/uts/sun4u/ngdr/io/dr_io.c (revision 0e42dee69ed771bf604dd1789fca9d77b5bbe302)
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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * I/O support routines for DR
30  */
31 
32 #include <sys/debug.h>
33 #include <sys/types.h>
34 #include <sys/errno.h>
35 #include <sys/cred.h>
36 #include <sys/dditypes.h>
37 #include <sys/devops.h>
38 #include <sys/modctl.h>
39 #include <sys/poll.h>
40 #include <sys/conf.h>
41 #include <sys/ddi.h>
42 #include <sys/sunddi.h>
43 #include <sys/sunndi.h>
44 #include <sys/ndi_impldefs.h>
45 #include <sys/stat.h>
46 #include <sys/kmem.h>
47 #include <sys/processor.h>
48 #include <sys/cpuvar.h>
49 #include <sys/mem_config.h>
50 #include <sys/promif.h>
51 #include <sys/x_call.h>
52 #include <sys/cpu_sgnblk_defs.h>
53 #include <sys/membar.h>
54 #include <sys/stack.h>
55 #include <sys/sysmacros.h>
56 #include <sys/machsystm.h>
57 #include <sys/spitregs.h>
58 #include <sys/cpupart.h>
59 
60 #include <sys/archsystm.h>
61 #include <vm/hat_sfmmu.h>
62 #include <sys/pte.h>
63 #include <sys/mmu.h>
64 #include <sys/x_call.h>
65 #include <sys/cpu_module.h>
66 
67 #include <sys/cmn_err.h>
68 
69 #include <sys/dr.h>
70 #include <sys/dr_util.h>
71 #include <sys/drmach.h>
72 
73 void
74 dr_init_io_unit(dr_io_unit_t *ip)
75 {
76 	dr_state_t	new_state;
77 
78 	if (DR_DEV_IS_ATTACHED(&ip->sbi_cm)) {
79 		new_state = DR_STATE_CONFIGURED;
80 		ip->sbi_cm.sbdev_cond = SBD_COND_OK;
81 	} else if (DR_DEV_IS_PRESENT(&ip->sbi_cm)) {
82 		new_state = DR_STATE_CONNECTED;
83 		ip->sbi_cm.sbdev_cond = SBD_COND_OK;
84 	} else {
85 		new_state = DR_STATE_EMPTY;
86 	}
87 	dr_device_transition(&ip->sbi_cm, new_state);
88 }
89 
90 /*ARGSUSED*/
91 void
92 dr_attach_io(dr_handle_t *hp, dr_common_unit_t *cp)
93 {
94 	sbd_error_t *err;
95 
96 	dr_lock_status(hp->h_bd);
97 	err = drmach_configure(cp->sbdev_id, 0);
98 	dr_unlock_status(hp->h_bd);
99 
100 	if (!err)
101 		err = drmach_io_post_attach(cp->sbdev_id);
102 
103 	if (err)
104 		DRERR_SET_C(&cp->sbdev_error, &err);
105 }
106 
107 /*
108  * remove device nodes for the branch indicated by cp
109  */
110 /*ARGSUSED*/
111 void
112 dr_detach_io(dr_handle_t *hp, dr_common_unit_t *cp)
113 {
114 	sbd_error_t *err;
115 
116 	err = drmach_unconfigure(cp->sbdev_id, 0);
117 
118 	if (!err)
119 		err = drmach_unconfigure(cp->sbdev_id, DEVI_BRANCH_DESTROY);
120 
121 	if (!err)
122 		err = drmach_io_post_release(cp->sbdev_id);
123 
124 	if (err) {
125 		dr_device_transition(cp, DR_STATE_CONFIGURED);
126 		DRERR_SET_C(&cp->sbdev_error, &err);
127 	}
128 }
129 
130 /*ARGSUSED*/
131 int
132 dr_disconnect_io(dr_io_unit_t *ip)
133 {
134 	return (0);
135 }
136 
137 /*ARGSUSED*/
138 int
139 dr_pre_attach_io(dr_handle_t *hp,
140 	dr_common_unit_t **devlist, int devnum)
141 {
142 	int		d;
143 
144 	for (d = 0; d < devnum; d++) {
145 		dr_common_unit_t *cp = devlist[d];
146 
147 		cmn_err(CE_CONT, "OS configure %s", cp->sbdev_path);
148 	}
149 
150 	return (0);
151 }
152 
153 /*ARGSUSED*/
154 int
155 dr_post_attach_io(dr_handle_t *hp,
156 	dr_common_unit_t **devlist, int devnum)
157 {
158 	return (0);
159 }
160 
161 static int
162 dr_check_io_refs(dr_handle_t *hp, dr_common_unit_t **devlist, int devnum)
163 {
164 	register int	i, reftotal = 0;
165 	static fn_t	f = "dr_check_io_refs";
166 
167 	for (i = 0; i < devnum; i++) {
168 		dr_io_unit_t	*ip = (dr_io_unit_t *)devlist[i];
169 		dev_info_t	*dip;
170 		int		ref;
171 		sbd_error_t	*err;
172 
173 		err = drmach_get_dip(ip->sbi_cm.sbdev_id, &dip);
174 		if (err)
175 			DRERR_SET_C(&ip->sbi_cm.sbdev_error, &err);
176 		else if (dip != NULL) {
177 			ref = 0;
178 			ASSERT(e_ddi_branch_held(dip));
179 			dr_check_devices(dip, &ref, hp, NULL, NULL, 0);
180 			hp->h_err = NULL;
181 			if (ref) {
182 				dr_dev_err(CE_WARN, &ip->sbi_cm, ESBD_BUSY);
183 			}
184 			PR_IO("%s: dip(%s) ref = %d\n",
185 				f, ddi_get_name(dip), ref);
186 			reftotal += ref;
187 		} else {
188 			PR_IO("%s: NO dip for id (0x%x)\n",
189 				f, (uint_t)(uintptr_t)ip->sbi_cm.sbdev_id);
190 		}
191 	}
192 
193 	return (reftotal);
194 }
195 
196 int
197 dr_pre_release_io(dr_handle_t *hp,
198 	dr_common_unit_t **devlist, int devnum)
199 {
200 	static fn_t	f = "dr_pre_release_io";
201 	int	d;
202 
203 	ASSERT(devnum > 0);
204 
205 	/* fail if any I/O device pre-release fails */
206 	for (d = 0; d < devnum; d++) {
207 		dr_io_unit_t *ip = (dr_io_unit_t *)devlist[d];
208 
209 		if ((hp->h_err = drmach_io_pre_release(
210 			ip->sbi_cm.sbdev_id)) != 0) {
211 			return (-1);
212 		}
213 	}
214 
215 	for (d = 0; d < devnum; d++) {
216 		dr_io_unit_t *ip = (dr_io_unit_t *)devlist[d];
217 		sbd_error_t *err;
218 
219 		err = drmach_release(ip->sbi_cm.sbdev_id);
220 		if (err) {
221 			DRERR_SET_C(&ip->sbi_cm.sbdev_error,
222 					&err);
223 			return (-1);
224 		}
225 	}
226 
227 	/* fail if any I/O devices are still referenced */
228 	if (dr_check_io_refs(hp, devlist, devnum) > 0) {
229 		PR_IO("%s: failed - I/O devices ref'd\n", f);
230 
231 		/* recover before return error */
232 		for (d = 0; d < devnum; d++) {
233 			dr_io_unit_t *ip = (dr_io_unit_t *)devlist[d];
234 			sbd_error_t *err;
235 			err = drmach_io_unrelease(ip->sbi_cm.sbdev_id);
236 			if (err) {
237 				DRERR_SET_C(&ip->sbi_cm.sbdev_error, &err);
238 				return (-1);
239 			}
240 		}
241 		return (-1);
242 	}
243 	return (0);
244 }
245 
246 /*ARGSUSED*/
247 int
248 dr_pre_detach_io(dr_handle_t *hp,
249 	dr_common_unit_t **devlist, int devnum)
250 {
251 	int		d;
252 
253 	ASSERT(devnum > 0);
254 
255 	for (d = 0; d < devnum; d++) {
256 		dr_common_unit_t *cp = devlist[d];
257 
258 		cmn_err(CE_CONT, "OS unconfigure %s", cp->sbdev_path);
259 	}
260 
261 	return (0);
262 }
263 
264 /*ARGSUSED*/
265 int
266 dr_post_detach_io(dr_handle_t *hp, dr_common_unit_t **devlist, int devnum)
267 {
268 	register int	i;
269 	int		rv = 0;
270 	static fn_t	f = "dr_post_detach_io";
271 
272 	ASSERT(devnum > 0);
273 	for (i = 0; i < devnum; i++) {
274 		dr_common_unit_t	*cp = devlist[i];
275 		if (cp->sbdev_error != NULL) {
276 			PR_IO("%s: Failed\n", f);
277 			rv = -1;
278 			break;
279 		}
280 	}
281 	return (rv);
282 }
283 
284 int
285 dr_io_status(dr_handle_t *hp, dr_devset_t devset, sbd_dev_stat_t *dsp)
286 {
287 	int		i, ix;
288 	dr_board_t	*bp;
289 	sbd_io_stat_t	*isp;
290 	dr_io_unit_t	*ip;
291 
292 	bp = hp->h_bd;
293 
294 	/*
295 	 * Only look for requested devices that are actually present.
296 	 */
297 	devset &= DR_DEVS_PRESENT(bp);
298 
299 	for (i = ix = 0; i < MAX_IO_UNITS_PER_BOARD; i++) {
300 		drmachid_t	 id;
301 		dev_info_t	*dip;
302 		sbd_error_t	*err;
303 		drmach_status_t	 pstat;
304 
305 		if (DEVSET_IN_SET(devset, SBD_COMP_IO, i) == 0)
306 			continue;
307 
308 		ip = dr_get_io_unit(bp, i);
309 
310 		if (ip->sbi_cm.sbdev_state == DR_STATE_EMPTY) {
311 			/* present, but not fully initialized */
312 			continue;
313 		}
314 
315 		id = ip->sbi_cm.sbdev_id;
316 		if (id == (drmachid_t)0)
317 			continue;
318 
319 		err = drmach_status(ip->sbi_cm.sbdev_id, &pstat);
320 		if (err) {
321 			DRERR_SET_C(&ip->sbi_cm.sbdev_error, &err);
322 			return (-1);
323 		}
324 
325 		isp = &dsp->d_io;
326 		bzero((caddr_t)isp, sizeof (*isp));
327 
328 		isp->is_cm.c_id.c_type = ip->sbi_cm.sbdev_type;
329 		isp->is_cm.c_id.c_unit = ip->sbi_cm.sbdev_unum;
330 		strncpy(isp->is_cm.c_id.c_name, pstat.type,
331 			sizeof (isp->is_cm.c_id.c_name));
332 		isp->is_cm.c_cond = ip->sbi_cm.sbdev_cond;
333 		isp->is_cm.c_busy = ip->sbi_cm.sbdev_busy | pstat.busy;
334 		isp->is_cm.c_time = ip->sbi_cm.sbdev_time;
335 		isp->is_cm.c_ostate = ip->sbi_cm.sbdev_ostate;
336 		isp->is_cm.c_sflags = 0;
337 
338 		dip = NULL;
339 		err = drmach_get_dip(id, &dip);
340 		if (err) {
341 			/* catch this in debug kernels */
342 			ASSERT(0);
343 
344 			sbd_err_clear(&err);
345 			continue;
346 		} else if (dip == NULL) {
347 			isp->is_pathname[0] = '\0';
348 			isp->is_referenced = 0;
349 			isp->is_unsafe_count = 0;
350 		} else {
351 			int		refcount = 0, idx = 0;
352 			uint64_t	unsafe_devs[SBD_MAX_UNSAFE];
353 
354 			ASSERT(e_ddi_branch_held(dip));
355 			(void) ddi_pathname(dip, isp->is_pathname);
356 
357 			/* check reference and unsafe counts on devices */
358 			isp->is_unsafe_count = 0;
359 			dr_check_devices(dip, &refcount, hp, unsafe_devs,
360 				&idx, SBD_MAX_UNSAFE);
361 			while (idx > 0) {
362 				isp->is_unsafe_list[idx-1] = unsafe_devs[idx-1];
363 				--idx;
364 			}
365 
366 			isp->is_referenced = (refcount == 0) ? 0 : 1;
367 
368 			hp->h_err = NULL;
369 		}
370 		ix++;
371 		dsp++;
372 	}
373 
374 	return (ix);
375 }
376