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