xref: /onnv-gate/usr/src/uts/sun4u/ngdr/io/dr_io.c (revision 1772:78cca3d2cc4b)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*1772Sjl139090  * Common Development and Distribution License (the "License").
6*1772Sjl139090  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*1772Sjl139090  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * I/O support routines for DR
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <sys/debug.h>
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate #include <sys/errno.h>
350Sstevel@tonic-gate #include <sys/cred.h>
360Sstevel@tonic-gate #include <sys/dditypes.h>
370Sstevel@tonic-gate #include <sys/devops.h>
380Sstevel@tonic-gate #include <sys/modctl.h>
390Sstevel@tonic-gate #include <sys/poll.h>
400Sstevel@tonic-gate #include <sys/conf.h>
410Sstevel@tonic-gate #include <sys/ddi.h>
420Sstevel@tonic-gate #include <sys/sunddi.h>
430Sstevel@tonic-gate #include <sys/sunndi.h>
440Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
450Sstevel@tonic-gate #include <sys/stat.h>
460Sstevel@tonic-gate #include <sys/kmem.h>
470Sstevel@tonic-gate #include <sys/processor.h>
480Sstevel@tonic-gate #include <sys/cpuvar.h>
490Sstevel@tonic-gate #include <sys/mem_config.h>
500Sstevel@tonic-gate #include <sys/promif.h>
510Sstevel@tonic-gate #include <sys/x_call.h>
520Sstevel@tonic-gate #include <sys/cpu_sgnblk_defs.h>
530Sstevel@tonic-gate #include <sys/membar.h>
540Sstevel@tonic-gate #include <sys/stack.h>
550Sstevel@tonic-gate #include <sys/sysmacros.h>
560Sstevel@tonic-gate #include <sys/machsystm.h>
570Sstevel@tonic-gate #include <sys/spitregs.h>
580Sstevel@tonic-gate #include <sys/cpupart.h>
590Sstevel@tonic-gate 
600Sstevel@tonic-gate #include <sys/archsystm.h>
610Sstevel@tonic-gate #include <vm/hat_sfmmu.h>
620Sstevel@tonic-gate #include <sys/pte.h>
630Sstevel@tonic-gate #include <sys/mmu.h>
640Sstevel@tonic-gate #include <sys/x_call.h>
650Sstevel@tonic-gate #include <sys/cpu_module.h>
660Sstevel@tonic-gate 
670Sstevel@tonic-gate #include <sys/cmn_err.h>
680Sstevel@tonic-gate 
690Sstevel@tonic-gate #include <sys/dr.h>
700Sstevel@tonic-gate #include <sys/dr_util.h>
710Sstevel@tonic-gate #include <sys/drmach.h>
720Sstevel@tonic-gate 
730Sstevel@tonic-gate void
740Sstevel@tonic-gate dr_init_io_unit(dr_io_unit_t *ip)
750Sstevel@tonic-gate {
760Sstevel@tonic-gate 	dr_state_t	new_state;
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 	if (DR_DEV_IS_ATTACHED(&ip->sbi_cm)) {
790Sstevel@tonic-gate 		new_state = DR_STATE_CONFIGURED;
800Sstevel@tonic-gate 		ip->sbi_cm.sbdev_cond = SBD_COND_OK;
810Sstevel@tonic-gate 	} else if (DR_DEV_IS_PRESENT(&ip->sbi_cm)) {
820Sstevel@tonic-gate 		new_state = DR_STATE_CONNECTED;
830Sstevel@tonic-gate 		ip->sbi_cm.sbdev_cond = SBD_COND_OK;
840Sstevel@tonic-gate 	} else {
850Sstevel@tonic-gate 		new_state = DR_STATE_EMPTY;
860Sstevel@tonic-gate 	}
870Sstevel@tonic-gate 	dr_device_transition(&ip->sbi_cm, new_state);
880Sstevel@tonic-gate }
890Sstevel@tonic-gate 
900Sstevel@tonic-gate /*ARGSUSED*/
910Sstevel@tonic-gate void
920Sstevel@tonic-gate dr_attach_io(dr_handle_t *hp, dr_common_unit_t *cp)
930Sstevel@tonic-gate {
940Sstevel@tonic-gate 	sbd_error_t *err;
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	dr_lock_status(hp->h_bd);
970Sstevel@tonic-gate 	err = drmach_configure(cp->sbdev_id, 0);
980Sstevel@tonic-gate 	dr_unlock_status(hp->h_bd);
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	if (!err)
1010Sstevel@tonic-gate 		err = drmach_io_post_attach(cp->sbdev_id);
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	if (err)
1040Sstevel@tonic-gate 		DRERR_SET_C(&cp->sbdev_error, &err);
1050Sstevel@tonic-gate }
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate /*
1080Sstevel@tonic-gate  * remove device nodes for the branch indicated by cp
1090Sstevel@tonic-gate  */
1100Sstevel@tonic-gate /*ARGSUSED*/
1110Sstevel@tonic-gate void
1120Sstevel@tonic-gate dr_detach_io(dr_handle_t *hp, dr_common_unit_t *cp)
1130Sstevel@tonic-gate {
1140Sstevel@tonic-gate 	sbd_error_t *err;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	err = drmach_unconfigure(cp->sbdev_id, 0);
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	if (!err)
119*1772Sjl139090 		err = drmach_unconfigure(cp->sbdev_id, DEVI_BRANCH_DESTROY);
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	if (!err)
1220Sstevel@tonic-gate 		err = drmach_io_post_release(cp->sbdev_id);
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	if (err) {
1250Sstevel@tonic-gate 		dr_device_transition(cp, DR_STATE_CONFIGURED);
1260Sstevel@tonic-gate 		DRERR_SET_C(&cp->sbdev_error, &err);
1270Sstevel@tonic-gate 	}
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate /*ARGSUSED*/
1310Sstevel@tonic-gate int
1320Sstevel@tonic-gate dr_disconnect_io(dr_io_unit_t *ip)
1330Sstevel@tonic-gate {
1340Sstevel@tonic-gate 	return (0);
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate /*ARGSUSED*/
1380Sstevel@tonic-gate int
1390Sstevel@tonic-gate dr_pre_attach_io(dr_handle_t *hp,
1400Sstevel@tonic-gate 	dr_common_unit_t **devlist, int devnum)
1410Sstevel@tonic-gate {
1420Sstevel@tonic-gate 	int		d;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	for (d = 0; d < devnum; d++) {
1450Sstevel@tonic-gate 		dr_common_unit_t *cp = devlist[d];
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 		cmn_err(CE_CONT, "OS configure %s", cp->sbdev_path);
1480Sstevel@tonic-gate 	}
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	return (0);
1510Sstevel@tonic-gate }
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate /*ARGSUSED*/
1540Sstevel@tonic-gate int
1550Sstevel@tonic-gate dr_post_attach_io(dr_handle_t *hp,
1560Sstevel@tonic-gate 	dr_common_unit_t **devlist, int devnum)
1570Sstevel@tonic-gate {
1580Sstevel@tonic-gate 	return (0);
1590Sstevel@tonic-gate }
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate static int
1620Sstevel@tonic-gate dr_check_io_refs(dr_handle_t *hp, dr_common_unit_t **devlist, int devnum)
1630Sstevel@tonic-gate {
1640Sstevel@tonic-gate 	register int	i, reftotal = 0;
1650Sstevel@tonic-gate 	static fn_t	f = "dr_check_io_refs";
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	for (i = 0; i < devnum; i++) {
1680Sstevel@tonic-gate 		dr_io_unit_t	*ip = (dr_io_unit_t *)devlist[i];
1690Sstevel@tonic-gate 		dev_info_t	*dip;
1700Sstevel@tonic-gate 		int		ref;
1710Sstevel@tonic-gate 		sbd_error_t	*err;
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 		err = drmach_get_dip(ip->sbi_cm.sbdev_id, &dip);
1740Sstevel@tonic-gate 		if (err)
1750Sstevel@tonic-gate 			DRERR_SET_C(&ip->sbi_cm.sbdev_error, &err);
1760Sstevel@tonic-gate 		else if (dip != NULL) {
1770Sstevel@tonic-gate 			ref = 0;
1780Sstevel@tonic-gate 			ASSERT(e_ddi_branch_held(dip));
1790Sstevel@tonic-gate 			dr_check_devices(dip, &ref, hp, NULL, NULL, 0);
1800Sstevel@tonic-gate 			hp->h_err = NULL;
1810Sstevel@tonic-gate 			if (ref) {
1820Sstevel@tonic-gate 				dr_dev_err(CE_WARN, &ip->sbi_cm, ESBD_BUSY);
1830Sstevel@tonic-gate 			}
1840Sstevel@tonic-gate 			PR_IO("%s: dip(%s) ref = %d\n",
1850Sstevel@tonic-gate 				f, ddi_get_name(dip), ref);
1860Sstevel@tonic-gate 			reftotal += ref;
1870Sstevel@tonic-gate 		} else {
1880Sstevel@tonic-gate 			PR_IO("%s: NO dip for id (0x%x)\n",
189930Smathue 				f, (uint_t)(uintptr_t)ip->sbi_cm.sbdev_id);
1900Sstevel@tonic-gate 		}
1910Sstevel@tonic-gate 	}
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	return (reftotal);
1940Sstevel@tonic-gate }
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate int
1970Sstevel@tonic-gate dr_pre_release_io(dr_handle_t *hp,
1980Sstevel@tonic-gate 	dr_common_unit_t **devlist, int devnum)
1990Sstevel@tonic-gate {
2000Sstevel@tonic-gate 	static fn_t	f = "dr_pre_release_io";
2010Sstevel@tonic-gate 	int	d;
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	ASSERT(devnum > 0);
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	/* fail if any I/O device pre-release fails */
2060Sstevel@tonic-gate 	for (d = 0; d < devnum; d++) {
2070Sstevel@tonic-gate 		dr_io_unit_t *ip = (dr_io_unit_t *)devlist[d];
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 		if ((hp->h_err = drmach_io_pre_release(
2100Sstevel@tonic-gate 			ip->sbi_cm.sbdev_id)) != 0) {
2110Sstevel@tonic-gate 			return (-1);
2120Sstevel@tonic-gate 		}
2130Sstevel@tonic-gate 	}
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	for (d = 0; d < devnum; d++) {
2160Sstevel@tonic-gate 		dr_io_unit_t *ip = (dr_io_unit_t *)devlist[d];
2170Sstevel@tonic-gate 		sbd_error_t *err;
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 		err = drmach_release(ip->sbi_cm.sbdev_id);
2200Sstevel@tonic-gate 		if (err) {
2210Sstevel@tonic-gate 			DRERR_SET_C(&ip->sbi_cm.sbdev_error,
2220Sstevel@tonic-gate 					&err);
2230Sstevel@tonic-gate 			return (-1);
2240Sstevel@tonic-gate 		}
2250Sstevel@tonic-gate 	}
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	/* fail if any I/O devices are still referenced */
2280Sstevel@tonic-gate 	if (dr_check_io_refs(hp, devlist, devnum) > 0) {
2290Sstevel@tonic-gate 		PR_IO("%s: failed - I/O devices ref'd\n", f);
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 		/* recover before return error */
2320Sstevel@tonic-gate 		for (d = 0; d < devnum; d++) {
2330Sstevel@tonic-gate 			dr_io_unit_t *ip = (dr_io_unit_t *)devlist[d];
2340Sstevel@tonic-gate 			sbd_error_t *err;
2350Sstevel@tonic-gate 			err = drmach_io_unrelease(ip->sbi_cm.sbdev_id);
2360Sstevel@tonic-gate 			if (err) {
2370Sstevel@tonic-gate 				DRERR_SET_C(&ip->sbi_cm.sbdev_error, &err);
2380Sstevel@tonic-gate 				return (-1);
2390Sstevel@tonic-gate 			}
2400Sstevel@tonic-gate 		}
2410Sstevel@tonic-gate 		return (-1);
2420Sstevel@tonic-gate 	}
2430Sstevel@tonic-gate 	return (0);
2440Sstevel@tonic-gate }
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate /*ARGSUSED*/
2470Sstevel@tonic-gate int
2480Sstevel@tonic-gate dr_pre_detach_io(dr_handle_t *hp,
2490Sstevel@tonic-gate 	dr_common_unit_t **devlist, int devnum)
2500Sstevel@tonic-gate {
2510Sstevel@tonic-gate 	int		d;
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	ASSERT(devnum > 0);
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	for (d = 0; d < devnum; d++) {
2560Sstevel@tonic-gate 		dr_common_unit_t *cp = devlist[d];
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 		cmn_err(CE_CONT, "OS unconfigure %s", cp->sbdev_path);
2590Sstevel@tonic-gate 	}
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	return (0);
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate /*ARGSUSED*/
2650Sstevel@tonic-gate int
2660Sstevel@tonic-gate dr_post_detach_io(dr_handle_t *hp, dr_common_unit_t **devlist, int devnum)
2670Sstevel@tonic-gate {
2680Sstevel@tonic-gate 	register int	i;
2690Sstevel@tonic-gate 	int		rv = 0;
2700Sstevel@tonic-gate 	static fn_t	f = "dr_post_detach_io";
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	ASSERT(devnum > 0);
2730Sstevel@tonic-gate 	for (i = 0; i < devnum; i++) {
2740Sstevel@tonic-gate 		dr_common_unit_t	*cp = devlist[i];
2750Sstevel@tonic-gate 		if (cp->sbdev_error != NULL) {
2760Sstevel@tonic-gate 			PR_IO("%s: Failed\n", f);
2770Sstevel@tonic-gate 			rv = -1;
2780Sstevel@tonic-gate 			break;
2790Sstevel@tonic-gate 		}
2800Sstevel@tonic-gate 	}
2810Sstevel@tonic-gate 	return (rv);
2820Sstevel@tonic-gate }
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate int
2850Sstevel@tonic-gate dr_io_status(dr_handle_t *hp, dr_devset_t devset, sbd_dev_stat_t *dsp)
2860Sstevel@tonic-gate {
2870Sstevel@tonic-gate 	int		i, ix;
2880Sstevel@tonic-gate 	dr_board_t	*bp;
2890Sstevel@tonic-gate 	sbd_io_stat_t	*isp;
2900Sstevel@tonic-gate 	dr_io_unit_t	*ip;
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	bp = hp->h_bd;
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	/*
2950Sstevel@tonic-gate 	 * Only look for requested devices that are actually present.
2960Sstevel@tonic-gate 	 */
2970Sstevel@tonic-gate 	devset &= DR_DEVS_PRESENT(bp);
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	for (i = ix = 0; i < MAX_IO_UNITS_PER_BOARD; i++) {
3000Sstevel@tonic-gate 		drmachid_t	 id;
3010Sstevel@tonic-gate 		dev_info_t	*dip;
3020Sstevel@tonic-gate 		sbd_error_t	*err;
3030Sstevel@tonic-gate 		drmach_status_t	 pstat;
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 		if (DEVSET_IN_SET(devset, SBD_COMP_IO, i) == 0)
3060Sstevel@tonic-gate 			continue;
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 		ip = dr_get_io_unit(bp, i);
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 		if (ip->sbi_cm.sbdev_state == DR_STATE_EMPTY) {
3110Sstevel@tonic-gate 			/* present, but not fully initialized */
3120Sstevel@tonic-gate 			continue;
3130Sstevel@tonic-gate 		}
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 		id = ip->sbi_cm.sbdev_id;
3160Sstevel@tonic-gate 		if (id == (drmachid_t)0)
3170Sstevel@tonic-gate 			continue;
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 		err = drmach_status(ip->sbi_cm.sbdev_id, &pstat);
3200Sstevel@tonic-gate 		if (err) {
3210Sstevel@tonic-gate 			DRERR_SET_C(&ip->sbi_cm.sbdev_error, &err);
3220Sstevel@tonic-gate 			return (-1);
3230Sstevel@tonic-gate 		}
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 		isp = &dsp->d_io;
3260Sstevel@tonic-gate 		bzero((caddr_t)isp, sizeof (*isp));
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 		isp->is_cm.c_id.c_type = ip->sbi_cm.sbdev_type;
3290Sstevel@tonic-gate 		isp->is_cm.c_id.c_unit = ip->sbi_cm.sbdev_unum;
3300Sstevel@tonic-gate 		strncpy(isp->is_cm.c_id.c_name, pstat.type,
3310Sstevel@tonic-gate 			sizeof (isp->is_cm.c_id.c_name));
3320Sstevel@tonic-gate 		isp->is_cm.c_cond = ip->sbi_cm.sbdev_cond;
3330Sstevel@tonic-gate 		isp->is_cm.c_busy = ip->sbi_cm.sbdev_busy | pstat.busy;
3340Sstevel@tonic-gate 		isp->is_cm.c_time = ip->sbi_cm.sbdev_time;
3350Sstevel@tonic-gate 		isp->is_cm.c_ostate = ip->sbi_cm.sbdev_ostate;
3360Sstevel@tonic-gate 		isp->is_cm.c_sflags = 0;
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 		dip = NULL;
3390Sstevel@tonic-gate 		err = drmach_get_dip(id, &dip);
3400Sstevel@tonic-gate 		if (err) {
3410Sstevel@tonic-gate 			/* catch this in debug kernels */
3420Sstevel@tonic-gate 			ASSERT(0);
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 			sbd_err_clear(&err);
3450Sstevel@tonic-gate 			continue;
3460Sstevel@tonic-gate 		} else if (dip == NULL) {
3470Sstevel@tonic-gate 			isp->is_pathname[0] = '\0';
3480Sstevel@tonic-gate 			isp->is_referenced = 0;
3490Sstevel@tonic-gate 			isp->is_unsafe_count = 0;
3500Sstevel@tonic-gate 		} else {
3510Sstevel@tonic-gate 			int		refcount = 0, idx = 0;
3520Sstevel@tonic-gate 			uint64_t	unsafe_devs[SBD_MAX_UNSAFE];
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 			ASSERT(e_ddi_branch_held(dip));
3550Sstevel@tonic-gate 			(void) ddi_pathname(dip, isp->is_pathname);
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 			/* check reference and unsafe counts on devices */
3580Sstevel@tonic-gate 			isp->is_unsafe_count = 0;
3590Sstevel@tonic-gate 			dr_check_devices(dip, &refcount, hp, unsafe_devs,
3600Sstevel@tonic-gate 				&idx, SBD_MAX_UNSAFE);
3610Sstevel@tonic-gate 			while (idx > 0) {
3620Sstevel@tonic-gate 				isp->is_unsafe_list[idx-1] = unsafe_devs[idx-1];
3630Sstevel@tonic-gate 				--idx;
3640Sstevel@tonic-gate 			}
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 			isp->is_referenced = (refcount == 0) ? 0 : 1;
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 			hp->h_err = NULL;
3690Sstevel@tonic-gate 		}
3700Sstevel@tonic-gate 		ix++;
3710Sstevel@tonic-gate 		dsp++;
3720Sstevel@tonic-gate 	}
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 	return (ix);
3750Sstevel@tonic-gate }
376