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 51772Sjl139090 * Common Development and Distribution License (the "License"). 61772Sjl139090 * 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*4845Svikram * Copyright 2007 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) 1191772Sjl139090 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 284*4845Svikram static void 285*4845Svikram dr_get_comp_cond(dr_io_unit_t *ip, dev_info_t *dip) 286*4845Svikram { 287*4845Svikram if (dip == NULL) { 288*4845Svikram ip->sbi_cm.sbdev_cond = SBD_COND_UNKNOWN; 289*4845Svikram return; 290*4845Svikram } 291*4845Svikram 292*4845Svikram if (DEVI(dip)->devi_flags & DEVI_RETIRED) { 293*4845Svikram ip->sbi_cm.sbdev_cond = SBD_COND_FAILED; 294*4845Svikram return; 295*4845Svikram } 296*4845Svikram 297*4845Svikram if (DR_DEV_IS_ATTACHED(&ip->sbi_cm)) { 298*4845Svikram ip->sbi_cm.sbdev_cond = SBD_COND_OK; 299*4845Svikram } else if (DR_DEV_IS_PRESENT(&ip->sbi_cm)) { 300*4845Svikram ip->sbi_cm.sbdev_cond = SBD_COND_OK; 301*4845Svikram } 302*4845Svikram } 303*4845Svikram 3040Sstevel@tonic-gate int 3050Sstevel@tonic-gate dr_io_status(dr_handle_t *hp, dr_devset_t devset, sbd_dev_stat_t *dsp) 3060Sstevel@tonic-gate { 3070Sstevel@tonic-gate int i, ix; 3080Sstevel@tonic-gate dr_board_t *bp; 3090Sstevel@tonic-gate sbd_io_stat_t *isp; 3100Sstevel@tonic-gate dr_io_unit_t *ip; 3110Sstevel@tonic-gate 3120Sstevel@tonic-gate bp = hp->h_bd; 3130Sstevel@tonic-gate 3140Sstevel@tonic-gate /* 3150Sstevel@tonic-gate * Only look for requested devices that are actually present. 3160Sstevel@tonic-gate */ 3170Sstevel@tonic-gate devset &= DR_DEVS_PRESENT(bp); 3180Sstevel@tonic-gate 3190Sstevel@tonic-gate for (i = ix = 0; i < MAX_IO_UNITS_PER_BOARD; i++) { 3200Sstevel@tonic-gate drmachid_t id; 3210Sstevel@tonic-gate dev_info_t *dip; 3220Sstevel@tonic-gate sbd_error_t *err; 3230Sstevel@tonic-gate drmach_status_t pstat; 3240Sstevel@tonic-gate 3250Sstevel@tonic-gate if (DEVSET_IN_SET(devset, SBD_COMP_IO, i) == 0) 3260Sstevel@tonic-gate continue; 3270Sstevel@tonic-gate 3280Sstevel@tonic-gate ip = dr_get_io_unit(bp, i); 3290Sstevel@tonic-gate 3300Sstevel@tonic-gate if (ip->sbi_cm.sbdev_state == DR_STATE_EMPTY) { 3310Sstevel@tonic-gate /* present, but not fully initialized */ 3320Sstevel@tonic-gate continue; 3330Sstevel@tonic-gate } 3340Sstevel@tonic-gate 3350Sstevel@tonic-gate id = ip->sbi_cm.sbdev_id; 3360Sstevel@tonic-gate if (id == (drmachid_t)0) 3370Sstevel@tonic-gate continue; 3380Sstevel@tonic-gate 3390Sstevel@tonic-gate err = drmach_status(ip->sbi_cm.sbdev_id, &pstat); 3400Sstevel@tonic-gate if (err) { 3410Sstevel@tonic-gate DRERR_SET_C(&ip->sbi_cm.sbdev_error, &err); 3420Sstevel@tonic-gate return (-1); 3430Sstevel@tonic-gate } 3440Sstevel@tonic-gate 3450Sstevel@tonic-gate dip = NULL; 3460Sstevel@tonic-gate err = drmach_get_dip(id, &dip); 3470Sstevel@tonic-gate if (err) { 3480Sstevel@tonic-gate /* catch this in debug kernels */ 3490Sstevel@tonic-gate ASSERT(0); 3500Sstevel@tonic-gate 3510Sstevel@tonic-gate sbd_err_clear(&err); 3520Sstevel@tonic-gate continue; 353*4845Svikram } 354*4845Svikram 355*4845Svikram isp = &dsp->d_io; 356*4845Svikram bzero((caddr_t)isp, sizeof (*isp)); 357*4845Svikram 358*4845Svikram isp->is_cm.c_id.c_type = ip->sbi_cm.sbdev_type; 359*4845Svikram isp->is_cm.c_id.c_unit = ip->sbi_cm.sbdev_unum; 360*4845Svikram strncpy(isp->is_cm.c_id.c_name, pstat.type, 361*4845Svikram sizeof (isp->is_cm.c_id.c_name)); 362*4845Svikram 363*4845Svikram dr_get_comp_cond(ip, dip); 364*4845Svikram isp->is_cm.c_cond = ip->sbi_cm.sbdev_cond; 365*4845Svikram isp->is_cm.c_busy = ip->sbi_cm.sbdev_busy | pstat.busy; 366*4845Svikram isp->is_cm.c_time = ip->sbi_cm.sbdev_time; 367*4845Svikram isp->is_cm.c_ostate = ip->sbi_cm.sbdev_ostate; 368*4845Svikram isp->is_cm.c_sflags = 0; 369*4845Svikram 370*4845Svikram if (dip == NULL) { 3710Sstevel@tonic-gate isp->is_pathname[0] = '\0'; 3720Sstevel@tonic-gate isp->is_referenced = 0; 3730Sstevel@tonic-gate isp->is_unsafe_count = 0; 3740Sstevel@tonic-gate } else { 3750Sstevel@tonic-gate int refcount = 0, idx = 0; 3760Sstevel@tonic-gate uint64_t unsafe_devs[SBD_MAX_UNSAFE]; 3770Sstevel@tonic-gate 3780Sstevel@tonic-gate ASSERT(e_ddi_branch_held(dip)); 3790Sstevel@tonic-gate (void) ddi_pathname(dip, isp->is_pathname); 3800Sstevel@tonic-gate 3810Sstevel@tonic-gate /* check reference and unsafe counts on devices */ 3820Sstevel@tonic-gate isp->is_unsafe_count = 0; 3830Sstevel@tonic-gate dr_check_devices(dip, &refcount, hp, unsafe_devs, 3840Sstevel@tonic-gate &idx, SBD_MAX_UNSAFE); 3850Sstevel@tonic-gate while (idx > 0) { 3860Sstevel@tonic-gate isp->is_unsafe_list[idx-1] = unsafe_devs[idx-1]; 3870Sstevel@tonic-gate --idx; 3880Sstevel@tonic-gate } 3890Sstevel@tonic-gate 3900Sstevel@tonic-gate isp->is_referenced = (refcount == 0) ? 0 : 1; 3910Sstevel@tonic-gate 3920Sstevel@tonic-gate hp->h_err = NULL; 3930Sstevel@tonic-gate } 3940Sstevel@tonic-gate ix++; 3950Sstevel@tonic-gate dsp++; 3960Sstevel@tonic-gate } 3970Sstevel@tonic-gate 3980Sstevel@tonic-gate return (ix); 3990Sstevel@tonic-gate } 400