xref: /titanic_51/usr/src/uts/sun4u/io/sbd_io.c (revision 672986541be54a7a471bb088e60780c37e371d7e)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/debug.h>
30 #include <sys/types.h>
31 #include <sys/errno.h>
32 #include <sys/cred.h>
33 #include <sys/dditypes.h>
34 #include <sys/sunddi.h>
35 #include <sys/sunndi.h>
36 #include <sys/ddi.h>
37 #include <sys/ddi_impldefs.h>
38 #include <sys/ndi_impldefs.h>
39 #include <sys/kmem.h>
40 #include <sys/note.h>
41 
42 #include <sys/sbdpriv.h>
43 #include <sys/sbd_io.h>
44 #include <sys/machsystm.h>
45 
46 
47 extern void sbd_errno_decode(int err, sbderror_t *ep, dev_info_t *dip);
48 extern sbd_state_t ostate_cvt(sbd_istate_t);
49 
50 /*
51  * Given a dev_info_t of a branch root, walk down the
52  * branch to attach drivers
53  */
54 /*ARGSUSED*/
55 void
56 sbd_attach_io(sbd_handle_t *hp, sbderror_t *ep, dev_info_t *dip, int unit)
57 {
58 	sbd_board_t	*sbp = SBDH2BD(hp->h_sbd);
59 
60 	ASSERT(e_ddi_branch_held(dip));
61 
62 	(void) e_ddi_branch_configure(dip, NULL, 0);
63 
64 	ASSERT(sbp->sb_iopath[unit] != NULL);
65 
66 	(void) ddi_pathname(dip, sbp->sb_iopath[unit]);
67 }
68 
69 /*
70  * remove device nodes for the branch indicated by dip
71  * Hold the status lock so that status can safely do ddi_pathname().
72  */
73 /*ARGSUSED*/
74 void
75 sbd_detach_io(sbd_handle_t *hp, sbderror_t *ep, dev_info_t *dip, int unit)
76 {
77 	int rv;
78 	dev_info_t *fdip = NULL;
79 	sbd_board_t *sbp = SBDH2BD(hp->h_sbd);
80 
81 	ASSERT(e_ddi_branch_held(dip));
82 	mutex_enter(&sbp->sb_slock);
83 	rv = e_ddi_branch_unconfigure(dip, &fdip, DEVI_BRANCH_EVENT);
84 	mutex_exit(&sbp->sb_slock);
85 	if (rv) {
86 		/*
87 		 * If non-NULL, fdip is returned held and must be released.
88 		 */
89 		if (fdip != NULL) {
90 			sbd_errno_decode(rv, ep, fdip);
91 			ddi_release_devi(fdip);
92 		} else {
93 			sbd_errno_decode(rv, ep, dip);
94 		}
95 	}
96 }
97 
98 /*ARGSUSED*/
99 void
100 sbd_init_io_unit(sbd_board_t *sbp, int unit)
101 {
102 	sbd_istate_t	new_state;
103 	sbd_io_unit_t	*ip;
104 	dev_info_t	*dip;
105 
106 	ip = SBD_GET_BOARD_IOUNIT(sbp, unit);
107 
108 	if (SBD_DEV_IS_ATTACHED(sbp, SBD_COMP_IO, unit)) {
109 		new_state = SBD_STATE_CONFIGURED;
110 	} else if (SBD_DEV_IS_PRESENT(sbp, SBD_COMP_IO, unit)) {
111 		new_state = SBD_STATE_CONNECTED;
112 	} else {
113 		new_state = SBD_STATE_EMPTY;
114 	}
115 	dip = sbp->sb_devlist[NIX(SBD_COMP_IO)][unit];
116 	ip->sbi_cm.sbdev_cond = sbd_get_comp_cond(dip);
117 
118 	/*
119 	 * Any changes to this io component should be performed above
120 	 * this call to ensure the component is fully initialized
121 	 * before transitioning to the new state.
122 	 */
123 	SBD_DEVICE_TRANSITION(sbp, SBD_COMP_IO, unit, new_state);
124 }
125 
126 /*ARGSUSED*/
127 int
128 sbd_disconnect_io(sbd_handle_t *hp, int unit)
129 {
130 	return (0);
131 }
132 
133 int
134 sbd_pre_attach_io(sbd_handle_t *hp, sbd_devlist_t *devlist, int devnum)
135 {
136 	_NOTE(ARGUNUSED(hp))
137 	_NOTE(ARGUNUSED(devlist))
138 	_NOTE(ARGUNUSED(devnum))
139 
140 	return (0);
141 }
142 
143 /*ARGSUSED*/
144 int
145 sbd_pre_detach_io(sbd_handle_t *hp, sbd_devlist_t *devlist, int devnum)
146 {
147 	fn_t	f = "sbd_pre_detach_io";
148 
149 	PR_IO("%s...\n", f);
150 
151 	if (devnum <= 0)
152 		return (-1);
153 
154 	/* fail if any I/O devices are referenced */
155 	if (sbd_check_io_refs(hp, devlist, devnum) > 0) {
156 		PR_IO("%s: failed - I/O devices ref'd\n", f);
157 		return (-1);
158 	}
159 
160 	return (0);
161 }
162 
163 /*ARGSUSED*/
164 int
165 sbd_post_attach_io(sbd_handle_t *hp, sbd_devlist_t *devlist, int devnum)
166 {
167 	_NOTE(ARGUNUSED(hp))
168 	_NOTE(ARGUNUSED(devlist))
169 	_NOTE(ARGUNUSED(devnum))
170 
171 	return (0);
172 }
173 
174 /*ARGSUSED*/
175 int
176 sbd_post_detach_io(sbd_handle_t *hp, sbd_devlist_t *devlist, int devnum)
177 {
178 	return (0);
179 }
180 
181 /*ARGSUSED*/
182 int
183 sbd_io_status(sbd_handle_t *hp, sbd_devset_t devset, sbd_dev_stat_t *dsp)
184 {
185 	int		i, ix;
186 	sbd_board_t	*sbp;
187 	sbd_io_stat_t	*isp;
188 	sbd_io_unit_t	*ip;
189 	sbd_istate_t	dstate;
190 	sbdp_handle_t	*hdp;
191 	sbderror_t	*ep;
192 	sbd_error_t	*sep;
193 
194 	/*
195 	 * Only look for requested devices that are actually present.
196 	 */
197 	sbp = SBDH2BD(hp->h_sbd);
198 
199 	ep = HD2MACHERR(hp);
200 	sep = kmem_zalloc(sizeof (sbd_error_t), KM_SLEEP);
201 	hdp = sbd_get_sbdp_handle(sbp, hp);
202 
203 	/*
204 	 * Concurrent status and unconfigure, disconnect are allowed.
205 	 * To prevent DR code from accessing stale dips, check the
206 	 * present devset and access the dips with status lock held.
207 	 * Disconnect and unconfigure code change dip state with
208 	 * status lock (sb_slock) held.
209 	 */
210 	mutex_enter(&sbp->sb_slock);
211 
212 	devset &= SBD_DEVS_PRESENT(sbp);
213 
214 	for (i = ix = 0; i < MAX_IO_UNITS_PER_BOARD; i++) {
215 		dev_info_t	*dip;
216 		int		unit;
217 		int		namelen;
218 		int		refcount = 0;
219 
220 		if (DEVSET_IN_SET(devset, SBD_COMP_IO, i) == 0)
221 			continue;
222 		/*
223 		 * Check to make sure the io component is in a state
224 		 * where its fully initialized.
225 		 */
226 		if (SBD_DEVICE_STATE(sbp, SBD_COMP_IO, i) == SBD_STATE_EMPTY)
227 			continue;
228 
229 		dip = sbp->sb_devlist[NIX(SBD_COMP_IO)][i];
230 		if (dip == NULL)
231 			continue;
232 
233 		isp = &dsp->d_io;
234 
235 		bzero((caddr_t)isp, sizeof (*isp));
236 		namelen = sizeof (isp->is_name);
237 		(void) ddi_getlongprop_buf(DDI_DEV_T_ANY, dip,
238 		    DDI_PROP_DONTPASS, OBP_DEVICETYPE,
239 		    (caddr_t)isp->is_name, &namelen);
240 
241 		isp->is_unit = sbdp_get_unit_num(hdp, dip);
242 		if (isp->is_unit < 0) {
243 			if (hp->h_flags & SBD_IOCTL_FLAG_FORCE)
244 				continue;
245 			else {
246 				SBD_GET_PERR(hdp->h_err, SBD_HD2ERR(hp));
247 				break;
248 			}
249 		}
250 		unit = isp->is_unit;
251 
252 		dstate = SBD_DEVICE_STATE(sbp, SBD_COMP_IO, unit);
253 		isp->is_ostate	= ostate_cvt(dstate);
254 		isp->is_type = SBD_COMP_IO;
255 		ip = SBD_GET_BOARD_IOUNIT(sbp, unit);
256 		isp->is_cm.c_cond = ip->sbi_cm.sbdev_cond;
257 		isp->is_cm.c_busy = ip->sbi_cm.sbdev_busy;
258 		isp->is_cm.c_time = ip->sbi_cm.sbdev_time;
259 
260 
261 		/*
262 		 * This is safe to do as unconfigure and disconnect
263 		 * hold the status lock while changing dip state.
264 		 */
265 		(void) ddi_pathname(dip, isp->is_pathname);
266 
267 		/*
268 		 * We use a dummy handle in which to collect
269 		 * the major numbers of unsafe devices.
270 		 */
271 		sbdp_check_devices(dip, &refcount, sep);
272 
273 		isp->is_referenced = (refcount == 0) ? 0 : 1;
274 
275 		isp->is_unsafe_count = 0;
276 
277 		/*
278 		 * Reset error field since we don't care about
279 		 * errors at this level.  The unsafe devices
280 		 * will be reported in the structure.
281 		 */
282 		SBD_SET_ERR(ep, ESBD_NOERROR);
283 		ep->e_rsc[0] = '\0';
284 
285 		ix++;
286 		dsp++;
287 	}
288 
289 	mutex_exit(&sbp->sb_slock);
290 
291 	kmem_free(sep, sizeof (sbd_error_t));
292 	sbd_release_sbdp_handle(hdp);
293 
294 	return (ix);
295 }
296 
297 /*ARGSUSED*/
298 int
299 sbd_io_cnt(sbd_handle_t *hp, sbd_devset_t devset)
300 {
301 	int		i, ix;
302 	sbd_board_t	*sbp;
303 
304 	sbp = SBDH2BD(hp->h_sbd);
305 
306 	/*
307 	 * Only look for requested devices that are actually present.
308 	 */
309 	devset &= SBD_DEVS_PRESENT(sbp);
310 
311 	for (i = ix = 0; i < MAX_IO_UNITS_PER_BOARD; i++) {
312 		dev_info_t	*dip;
313 
314 		if (DEVSET_IN_SET(devset, SBD_COMP_IO, i) == 0)
315 			continue;
316 
317 		dip = sbp->sb_devlist[NIX(SBD_COMP_IO)][i];
318 		if (dip == NULL)
319 			continue;
320 
321 		ix++;
322 	}
323 
324 	return (ix);
325 }
326 
327 int
328 sbd_check_io_refs(sbd_handle_t *hp, sbd_devlist_t devlist[], int devnum)
329 {
330 	register int	i, reftotal = 0;
331 	fn_t	f = "sbd_check_io_refs";
332 	sbd_error_t *sep;
333 	sbderror_t *ep;
334 
335 	sep = kmem_zalloc(sizeof (sbd_error_t), KM_SLEEP);
336 	ep = HD2MACHERR(hp);
337 
338 	for (i = 0; i < devnum; i++) {
339 		dev_info_t	*dip;
340 		int		ref;
341 
342 		dip = devlist[i].dv_dip;
343 		ref = 0;
344 		sbdp_check_devices(dip, &ref, sep);
345 		if (ref) {
346 			if (SBD_GET_ERR(ep) == 0) {
347 				SBD_GET_PERR(sep, ep);
348 			}
349 			SBD_GET_PERR(sep, &devlist[i].dv_error);
350 		}
351 		PR_IO("%s: dip(%s) ref = %d\n",
352 			f, ddi_get_name(dip), ref);
353 		reftotal += ref;
354 	}
355 
356 	kmem_free(sep, sizeof (sbd_error_t));
357 
358 	return (reftotal);
359 }
360 
361 int
362 sbd_check_io_attached(dev_info_t *dip, void *arg)
363 {
364 	dev_info_t **tdip;
365 
366 	tdip = (dev_info_t **)arg;
367 
368 	if (dip == *tdip) {
369 		int state;
370 
371 		state = ddi_get_devstate(dip);
372 		if (i_ddi_devi_attached(dip) || (state == DDI_DEVSTATE_UP)) {
373 			*tdip = NULL;
374 			return (DDI_WALK_TERMINATE);
375 		}
376 	}
377 	return (DDI_WALK_CONTINUE);
378 }
379 
380 int
381 sbd_pre_release_io(sbd_handle_t *hp,
382 	sbd_devlist_t *devlist, int devnum)
383 {
384 	fn_t	f = "sbd_pre_release_io";
385 	int	rv = 0;
386 	int	i;
387 
388 	ASSERT(devnum > 0);
389 
390 	/* fail if any I/O devices are referenced */
391 	if ((rv = sbd_check_io_refs(hp, devlist, devnum)) > 0) {
392 		/*
393 		 * One of the devices may have failed check to see which
394 		 * and set in the main handle
395 		 */
396 		for (i = 0; i < devnum; i++) {
397 			if (SBD_GET_ERR(&devlist[i].dv_error) != 0) {
398 				(void) sbd_set_err_in_hdl(hp,
399 				    &devlist[i].dv_error);
400 				break;
401 			}
402 		}
403 		PR_IO("%s: failed - I/O devices ref'd\n", f);
404 	}
405 
406 	return (rv);
407 }
408