xref: /titanic_50/usr/src/uts/sun4u/io/sbd_io.c (revision 7c2fbfb345896881c631598ee3852ce9ce33fb07)
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 2007 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 #include <sys/debug.h>
29 #include <sys/types.h>
30 #include <sys/errno.h>
31 #include <sys/cred.h>
32 #include <sys/dditypes.h>
33 #include <sys/sunddi.h>
34 #include <sys/sunndi.h>
35 #include <sys/ddi.h>
36 #include <sys/ddi_impldefs.h>
37 #include <sys/ndi_impldefs.h>
38 #include <sys/kmem.h>
39 #include <sys/note.h>
40 
41 #include <sys/sbdpriv.h>
42 #include <sys/sbd_io.h>
43 #include <sys/machsystm.h>
44 
45 
46 extern void sbd_errno_decode(int err, sbderror_t *ep, dev_info_t *dip);
47 extern sbd_state_t ostate_cvt(sbd_istate_t);
48 
49 /*
50  * Given a dev_info_t of a branch root, walk down the
51  * branch to attach drivers
52  */
53 /*ARGSUSED*/
54 void
55 sbd_attach_io(sbd_handle_t *hp, sbderror_t *ep, dev_info_t *dip, int unit)
56 {
57 	sbd_board_t	*sbp = SBDH2BD(hp->h_sbd);
58 
59 	ASSERT(e_ddi_branch_held(dip));
60 
61 	(void) e_ddi_branch_configure(dip, NULL, 0);
62 
63 	ASSERT(sbp->sb_iopath[unit] != NULL);
64 
65 	(void) ddi_pathname(dip, sbp->sb_iopath[unit]);
66 }
67 
68 /*
69  * remove device nodes for the branch indicated by dip
70  * Hold the status lock so that status can safely do ddi_pathname().
71  */
72 /*ARGSUSED*/
73 void
74 sbd_detach_io(sbd_handle_t *hp, sbderror_t *ep, dev_info_t *dip, int unit)
75 {
76 	int rv;
77 	dev_info_t *fdip = NULL;
78 	sbd_board_t *sbp = SBDH2BD(hp->h_sbd);
79 
80 	ASSERT(e_ddi_branch_held(dip));
81 	mutex_enter(&sbp->sb_slock);
82 	rv = e_ddi_branch_unconfigure(dip, &fdip, DEVI_BRANCH_EVENT);
83 	mutex_exit(&sbp->sb_slock);
84 	if (rv) {
85 		/*
86 		 * If non-NULL, fdip is returned held and must be released.
87 		 */
88 		if (fdip != NULL) {
89 			sbd_errno_decode(rv, ep, fdip);
90 			ddi_release_devi(fdip);
91 		} else {
92 			sbd_errno_decode(rv, ep, dip);
93 		}
94 	}
95 }
96 
97 /*ARGSUSED*/
98 void
99 sbd_init_io_unit(sbd_board_t *sbp, int unit)
100 {
101 	sbd_istate_t	new_state;
102 	sbd_io_unit_t	*ip;
103 	dev_info_t	*dip;
104 
105 	ip = SBD_GET_BOARD_IOUNIT(sbp, unit);
106 
107 	if (SBD_DEV_IS_ATTACHED(sbp, SBD_COMP_IO, unit)) {
108 		new_state = SBD_STATE_CONFIGURED;
109 	} else if (SBD_DEV_IS_PRESENT(sbp, SBD_COMP_IO, unit)) {
110 		new_state = SBD_STATE_CONNECTED;
111 	} else {
112 		new_state = SBD_STATE_EMPTY;
113 	}
114 	dip = sbp->sb_devlist[NIX(SBD_COMP_IO)][unit];
115 	ip->sbi_cm.sbdev_cond = sbd_get_comp_cond(dip);
116 
117 	/*
118 	 * Any changes to this io component should be performed above
119 	 * this call to ensure the component is fully initialized
120 	 * before transitioning to the new state.
121 	 */
122 	SBD_DEVICE_TRANSITION(sbp, SBD_COMP_IO, unit, new_state);
123 }
124 
125 /*ARGSUSED*/
126 int
127 sbd_disconnect_io(sbd_handle_t *hp, int unit)
128 {
129 	return (0);
130 }
131 
132 int
133 sbd_pre_attach_io(sbd_handle_t *hp, sbd_devlist_t *devlist, int devnum)
134 {
135 	_NOTE(ARGUNUSED(hp))
136 	_NOTE(ARGUNUSED(devlist))
137 	_NOTE(ARGUNUSED(devnum))
138 
139 	return (0);
140 }
141 
142 /*ARGSUSED*/
143 int
144 sbd_pre_detach_io(sbd_handle_t *hp, sbd_devlist_t *devlist, int devnum)
145 {
146 	fn_t	f = "sbd_pre_detach_io";
147 
148 	PR_IO("%s...\n", f);
149 
150 	if (devnum <= 0)
151 		return (-1);
152 
153 	/* fail if any I/O devices are referenced */
154 	if (sbd_check_io_refs(hp, devlist, devnum) > 0) {
155 		PR_IO("%s: failed - I/O devices ref'd\n", f);
156 		return (-1);
157 	}
158 
159 	return (0);
160 }
161 
162 /*ARGSUSED*/
163 int
164 sbd_post_attach_io(sbd_handle_t *hp, sbd_devlist_t *devlist, int devnum)
165 {
166 	_NOTE(ARGUNUSED(hp))
167 	_NOTE(ARGUNUSED(devlist))
168 	_NOTE(ARGUNUSED(devnum))
169 
170 	return (0);
171 }
172 
173 /*ARGSUSED*/
174 int
175 sbd_post_detach_io(sbd_handle_t *hp, sbd_devlist_t *devlist, int devnum)
176 {
177 	return (0);
178 }
179 
180 /*ARGSUSED*/
181 int
182 sbd_io_status(sbd_handle_t *hp, sbd_devset_t devset, sbd_dev_stat_t *dsp)
183 {
184 	int		i, ix;
185 	sbd_board_t	*sbp;
186 	sbd_io_stat_t	*isp;
187 	sbd_io_unit_t	*ip;
188 	sbd_istate_t	dstate;
189 	sbdp_handle_t	*hdp;
190 	sbderror_t	*ep;
191 	sbd_error_t	*sep;
192 
193 	/*
194 	 * Only look for requested devices that are actually present.
195 	 */
196 	sbp = SBDH2BD(hp->h_sbd);
197 
198 	ep = HD2MACHERR(hp);
199 	sep = kmem_zalloc(sizeof (sbd_error_t), KM_SLEEP);
200 	hdp = sbd_get_sbdp_handle(sbp, hp);
201 
202 	/*
203 	 * Concurrent status and unconfigure, disconnect are allowed.
204 	 * To prevent DR code from accessing stale dips, check the
205 	 * present devset and access the dips with status lock held.
206 	 * Disconnect and unconfigure code change dip state with
207 	 * status lock (sb_slock) held.
208 	 */
209 	mutex_enter(&sbp->sb_slock);
210 
211 	devset &= SBD_DEVS_PRESENT(sbp);
212 
213 	for (i = ix = 0; i < MAX_IO_UNITS_PER_BOARD; i++) {
214 		dev_info_t	*dip;
215 		int		unit;
216 		int		namelen;
217 		int		refcount = 0;
218 
219 		if (DEVSET_IN_SET(devset, SBD_COMP_IO, i) == 0)
220 			continue;
221 		/*
222 		 * Check to make sure the io component is in a state
223 		 * where its fully initialized.
224 		 */
225 		if (SBD_DEVICE_STATE(sbp, SBD_COMP_IO, i) == SBD_STATE_EMPTY)
226 			continue;
227 
228 		dip = sbp->sb_devlist[NIX(SBD_COMP_IO)][i];
229 		if (dip == NULL)
230 			continue;
231 
232 		isp = &dsp->d_io;
233 
234 		bzero((caddr_t)isp, sizeof (*isp));
235 		namelen = sizeof (isp->is_name);
236 		(void) ddi_getlongprop_buf(DDI_DEV_T_ANY, dip,
237 		    DDI_PROP_DONTPASS, OBP_DEVICETYPE,
238 		    (caddr_t)isp->is_name, &namelen);
239 
240 		isp->is_unit = sbdp_get_unit_num(hdp, dip);
241 		if (isp->is_unit < 0) {
242 			if (hp->h_flags & SBD_IOCTL_FLAG_FORCE)
243 				continue;
244 			else {
245 				SBD_GET_PERR(hdp->h_err, SBD_HD2ERR(hp));
246 				break;
247 			}
248 		}
249 		unit = isp->is_unit;
250 
251 		dstate = SBD_DEVICE_STATE(sbp, SBD_COMP_IO, unit);
252 		isp->is_ostate	= ostate_cvt(dstate);
253 		isp->is_type = SBD_COMP_IO;
254 		ip = SBD_GET_BOARD_IOUNIT(sbp, unit);
255 		ip->sbi_cm.sbdev_cond = sbd_get_comp_cond(dip);
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