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 */ 21*11311SSurya.Prakki@Sun.COM 220Sstevel@tonic-gate /* 23*11311SSurya.Prakki@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate /* 280Sstevel@tonic-gate * I/O support routines for DR 290Sstevel@tonic-gate */ 300Sstevel@tonic-gate 310Sstevel@tonic-gate #include <sys/debug.h> 320Sstevel@tonic-gate #include <sys/types.h> 330Sstevel@tonic-gate #include <sys/errno.h> 340Sstevel@tonic-gate #include <sys/cred.h> 350Sstevel@tonic-gate #include <sys/dditypes.h> 360Sstevel@tonic-gate #include <sys/devops.h> 370Sstevel@tonic-gate #include <sys/modctl.h> 380Sstevel@tonic-gate #include <sys/poll.h> 390Sstevel@tonic-gate #include <sys/conf.h> 400Sstevel@tonic-gate #include <sys/ddi.h> 410Sstevel@tonic-gate #include <sys/sunddi.h> 420Sstevel@tonic-gate #include <sys/sunndi.h> 430Sstevel@tonic-gate #include <sys/ndi_impldefs.h> 440Sstevel@tonic-gate #include <sys/stat.h> 450Sstevel@tonic-gate #include <sys/kmem.h> 460Sstevel@tonic-gate #include <sys/processor.h> 470Sstevel@tonic-gate #include <sys/cpuvar.h> 480Sstevel@tonic-gate #include <sys/mem_config.h> 490Sstevel@tonic-gate #include <sys/promif.h> 500Sstevel@tonic-gate #include <sys/x_call.h> 510Sstevel@tonic-gate #include <sys/cpu_sgnblk_defs.h> 520Sstevel@tonic-gate #include <sys/membar.h> 530Sstevel@tonic-gate #include <sys/stack.h> 540Sstevel@tonic-gate #include <sys/sysmacros.h> 550Sstevel@tonic-gate #include <sys/machsystm.h> 560Sstevel@tonic-gate #include <sys/spitregs.h> 570Sstevel@tonic-gate #include <sys/cpupart.h> 580Sstevel@tonic-gate 590Sstevel@tonic-gate #include <sys/archsystm.h> 600Sstevel@tonic-gate #include <vm/hat_sfmmu.h> 610Sstevel@tonic-gate #include <sys/pte.h> 620Sstevel@tonic-gate #include <sys/mmu.h> 630Sstevel@tonic-gate #include <sys/x_call.h> 640Sstevel@tonic-gate #include <sys/cpu_module.h> 650Sstevel@tonic-gate 660Sstevel@tonic-gate #include <sys/cmn_err.h> 670Sstevel@tonic-gate 680Sstevel@tonic-gate #include <sys/dr.h> 690Sstevel@tonic-gate #include <sys/dr_util.h> 700Sstevel@tonic-gate #include <sys/drmach.h> 710Sstevel@tonic-gate 720Sstevel@tonic-gate void 730Sstevel@tonic-gate dr_init_io_unit(dr_io_unit_t *ip) 740Sstevel@tonic-gate { 750Sstevel@tonic-gate dr_state_t new_state; 760Sstevel@tonic-gate 770Sstevel@tonic-gate if (DR_DEV_IS_ATTACHED(&ip->sbi_cm)) { 780Sstevel@tonic-gate new_state = DR_STATE_CONFIGURED; 790Sstevel@tonic-gate ip->sbi_cm.sbdev_cond = SBD_COND_OK; 800Sstevel@tonic-gate } else if (DR_DEV_IS_PRESENT(&ip->sbi_cm)) { 810Sstevel@tonic-gate new_state = DR_STATE_CONNECTED; 820Sstevel@tonic-gate ip->sbi_cm.sbdev_cond = SBD_COND_OK; 830Sstevel@tonic-gate } else { 840Sstevel@tonic-gate new_state = DR_STATE_EMPTY; 850Sstevel@tonic-gate } 860Sstevel@tonic-gate dr_device_transition(&ip->sbi_cm, new_state); 870Sstevel@tonic-gate } 880Sstevel@tonic-gate 890Sstevel@tonic-gate /*ARGSUSED*/ 900Sstevel@tonic-gate void 910Sstevel@tonic-gate dr_attach_io(dr_handle_t *hp, dr_common_unit_t *cp) 920Sstevel@tonic-gate { 930Sstevel@tonic-gate sbd_error_t *err; 940Sstevel@tonic-gate 950Sstevel@tonic-gate dr_lock_status(hp->h_bd); 960Sstevel@tonic-gate err = drmach_configure(cp->sbdev_id, 0); 970Sstevel@tonic-gate dr_unlock_status(hp->h_bd); 980Sstevel@tonic-gate 990Sstevel@tonic-gate if (!err) 1000Sstevel@tonic-gate err = drmach_io_post_attach(cp->sbdev_id); 1010Sstevel@tonic-gate 1020Sstevel@tonic-gate if (err) 1030Sstevel@tonic-gate DRERR_SET_C(&cp->sbdev_error, &err); 1040Sstevel@tonic-gate } 1050Sstevel@tonic-gate 1060Sstevel@tonic-gate /* 1070Sstevel@tonic-gate * remove device nodes for the branch indicated by cp 1080Sstevel@tonic-gate */ 1090Sstevel@tonic-gate /*ARGSUSED*/ 1100Sstevel@tonic-gate void 1110Sstevel@tonic-gate dr_detach_io(dr_handle_t *hp, dr_common_unit_t *cp) 1120Sstevel@tonic-gate { 1130Sstevel@tonic-gate sbd_error_t *err; 1140Sstevel@tonic-gate 1150Sstevel@tonic-gate err = drmach_unconfigure(cp->sbdev_id, 0); 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate if (!err) 1181772Sjl139090 err = drmach_unconfigure(cp->sbdev_id, DEVI_BRANCH_DESTROY); 1190Sstevel@tonic-gate 1200Sstevel@tonic-gate if (!err) 1210Sstevel@tonic-gate err = drmach_io_post_release(cp->sbdev_id); 1220Sstevel@tonic-gate 1230Sstevel@tonic-gate if (err) { 1240Sstevel@tonic-gate dr_device_transition(cp, DR_STATE_CONFIGURED); 1250Sstevel@tonic-gate DRERR_SET_C(&cp->sbdev_error, &err); 1260Sstevel@tonic-gate } 1270Sstevel@tonic-gate } 1280Sstevel@tonic-gate 1290Sstevel@tonic-gate /*ARGSUSED*/ 1300Sstevel@tonic-gate int 1310Sstevel@tonic-gate dr_disconnect_io(dr_io_unit_t *ip) 1320Sstevel@tonic-gate { 1330Sstevel@tonic-gate return (0); 1340Sstevel@tonic-gate } 1350Sstevel@tonic-gate 1360Sstevel@tonic-gate /*ARGSUSED*/ 1370Sstevel@tonic-gate int 1380Sstevel@tonic-gate dr_pre_attach_io(dr_handle_t *hp, 1390Sstevel@tonic-gate dr_common_unit_t **devlist, int devnum) 1400Sstevel@tonic-gate { 1410Sstevel@tonic-gate int d; 1420Sstevel@tonic-gate 1430Sstevel@tonic-gate for (d = 0; d < devnum; d++) { 1440Sstevel@tonic-gate dr_common_unit_t *cp = devlist[d]; 1450Sstevel@tonic-gate 1460Sstevel@tonic-gate cmn_err(CE_CONT, "OS configure %s", cp->sbdev_path); 1470Sstevel@tonic-gate } 1480Sstevel@tonic-gate 1490Sstevel@tonic-gate return (0); 1500Sstevel@tonic-gate } 1510Sstevel@tonic-gate 1520Sstevel@tonic-gate /*ARGSUSED*/ 1530Sstevel@tonic-gate int 1540Sstevel@tonic-gate dr_post_attach_io(dr_handle_t *hp, 1550Sstevel@tonic-gate dr_common_unit_t **devlist, int devnum) 1560Sstevel@tonic-gate { 1570Sstevel@tonic-gate return (0); 1580Sstevel@tonic-gate } 1590Sstevel@tonic-gate 1600Sstevel@tonic-gate static int 1610Sstevel@tonic-gate dr_check_io_refs(dr_handle_t *hp, dr_common_unit_t **devlist, int devnum) 1620Sstevel@tonic-gate { 1630Sstevel@tonic-gate register int i, reftotal = 0; 1640Sstevel@tonic-gate static fn_t f = "dr_check_io_refs"; 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate for (i = 0; i < devnum; i++) { 1670Sstevel@tonic-gate dr_io_unit_t *ip = (dr_io_unit_t *)devlist[i]; 1680Sstevel@tonic-gate dev_info_t *dip; 1690Sstevel@tonic-gate int ref; 1700Sstevel@tonic-gate sbd_error_t *err; 1710Sstevel@tonic-gate 1720Sstevel@tonic-gate err = drmach_get_dip(ip->sbi_cm.sbdev_id, &dip); 1730Sstevel@tonic-gate if (err) 1740Sstevel@tonic-gate DRERR_SET_C(&ip->sbi_cm.sbdev_error, &err); 1750Sstevel@tonic-gate else if (dip != NULL) { 1760Sstevel@tonic-gate ref = 0; 1770Sstevel@tonic-gate ASSERT(e_ddi_branch_held(dip)); 1780Sstevel@tonic-gate dr_check_devices(dip, &ref, hp, NULL, NULL, 0); 1790Sstevel@tonic-gate hp->h_err = NULL; 1800Sstevel@tonic-gate if (ref) { 1810Sstevel@tonic-gate dr_dev_err(CE_WARN, &ip->sbi_cm, ESBD_BUSY); 1820Sstevel@tonic-gate } 1830Sstevel@tonic-gate PR_IO("%s: dip(%s) ref = %d\n", 184*11311SSurya.Prakki@Sun.COM f, ddi_get_name(dip), ref); 1850Sstevel@tonic-gate reftotal += ref; 1860Sstevel@tonic-gate } else { 1870Sstevel@tonic-gate PR_IO("%s: NO dip for id (0x%x)\n", 188*11311SSurya.Prakki@Sun.COM f, (uint_t)(uintptr_t)ip->sbi_cm.sbdev_id); 1890Sstevel@tonic-gate } 1900Sstevel@tonic-gate } 1910Sstevel@tonic-gate 1920Sstevel@tonic-gate return (reftotal); 1930Sstevel@tonic-gate } 1940Sstevel@tonic-gate 1950Sstevel@tonic-gate int 1960Sstevel@tonic-gate dr_pre_release_io(dr_handle_t *hp, 1970Sstevel@tonic-gate dr_common_unit_t **devlist, int devnum) 1980Sstevel@tonic-gate { 1990Sstevel@tonic-gate static fn_t f = "dr_pre_release_io"; 2000Sstevel@tonic-gate int d; 2010Sstevel@tonic-gate 2020Sstevel@tonic-gate ASSERT(devnum > 0); 2030Sstevel@tonic-gate 2040Sstevel@tonic-gate /* fail if any I/O device pre-release fails */ 2050Sstevel@tonic-gate for (d = 0; d < devnum; d++) { 2060Sstevel@tonic-gate dr_io_unit_t *ip = (dr_io_unit_t *)devlist[d]; 2070Sstevel@tonic-gate 2080Sstevel@tonic-gate if ((hp->h_err = drmach_io_pre_release( 209*11311SSurya.Prakki@Sun.COM ip->sbi_cm.sbdev_id)) != 0) { 2100Sstevel@tonic-gate return (-1); 2110Sstevel@tonic-gate } 2120Sstevel@tonic-gate } 2130Sstevel@tonic-gate 2140Sstevel@tonic-gate for (d = 0; d < devnum; d++) { 2150Sstevel@tonic-gate dr_io_unit_t *ip = (dr_io_unit_t *)devlist[d]; 2160Sstevel@tonic-gate sbd_error_t *err; 2170Sstevel@tonic-gate 2180Sstevel@tonic-gate err = drmach_release(ip->sbi_cm.sbdev_id); 2190Sstevel@tonic-gate if (err) { 2200Sstevel@tonic-gate DRERR_SET_C(&ip->sbi_cm.sbdev_error, 221*11311SSurya.Prakki@Sun.COM &err); 2220Sstevel@tonic-gate return (-1); 2230Sstevel@tonic-gate } 2240Sstevel@tonic-gate } 2250Sstevel@tonic-gate 2260Sstevel@tonic-gate /* fail if any I/O devices are still referenced */ 2270Sstevel@tonic-gate if (dr_check_io_refs(hp, devlist, devnum) > 0) { 2280Sstevel@tonic-gate PR_IO("%s: failed - I/O devices ref'd\n", f); 2290Sstevel@tonic-gate 2300Sstevel@tonic-gate /* recover before return error */ 2310Sstevel@tonic-gate for (d = 0; d < devnum; d++) { 2320Sstevel@tonic-gate dr_io_unit_t *ip = (dr_io_unit_t *)devlist[d]; 2330Sstevel@tonic-gate sbd_error_t *err; 2340Sstevel@tonic-gate err = drmach_io_unrelease(ip->sbi_cm.sbdev_id); 2350Sstevel@tonic-gate if (err) { 2360Sstevel@tonic-gate DRERR_SET_C(&ip->sbi_cm.sbdev_error, &err); 2370Sstevel@tonic-gate return (-1); 2380Sstevel@tonic-gate } 2390Sstevel@tonic-gate } 2400Sstevel@tonic-gate return (-1); 2410Sstevel@tonic-gate } 2420Sstevel@tonic-gate return (0); 2430Sstevel@tonic-gate } 2440Sstevel@tonic-gate 2450Sstevel@tonic-gate /*ARGSUSED*/ 2460Sstevel@tonic-gate int 2470Sstevel@tonic-gate dr_pre_detach_io(dr_handle_t *hp, 2480Sstevel@tonic-gate dr_common_unit_t **devlist, int devnum) 2490Sstevel@tonic-gate { 2500Sstevel@tonic-gate int d; 2510Sstevel@tonic-gate 2520Sstevel@tonic-gate ASSERT(devnum > 0); 2530Sstevel@tonic-gate 2540Sstevel@tonic-gate for (d = 0; d < devnum; d++) { 2550Sstevel@tonic-gate dr_common_unit_t *cp = devlist[d]; 2560Sstevel@tonic-gate 2570Sstevel@tonic-gate cmn_err(CE_CONT, "OS unconfigure %s", cp->sbdev_path); 2580Sstevel@tonic-gate } 2590Sstevel@tonic-gate 2600Sstevel@tonic-gate return (0); 2610Sstevel@tonic-gate } 2620Sstevel@tonic-gate 2630Sstevel@tonic-gate /*ARGSUSED*/ 2640Sstevel@tonic-gate int 2650Sstevel@tonic-gate dr_post_detach_io(dr_handle_t *hp, dr_common_unit_t **devlist, int devnum) 2660Sstevel@tonic-gate { 2670Sstevel@tonic-gate register int i; 2680Sstevel@tonic-gate int rv = 0; 2690Sstevel@tonic-gate static fn_t f = "dr_post_detach_io"; 2700Sstevel@tonic-gate 2710Sstevel@tonic-gate ASSERT(devnum > 0); 2720Sstevel@tonic-gate for (i = 0; i < devnum; i++) { 2730Sstevel@tonic-gate dr_common_unit_t *cp = devlist[i]; 2740Sstevel@tonic-gate if (cp->sbdev_error != NULL) { 2750Sstevel@tonic-gate PR_IO("%s: Failed\n", f); 2760Sstevel@tonic-gate rv = -1; 2770Sstevel@tonic-gate break; 2780Sstevel@tonic-gate } 2790Sstevel@tonic-gate } 2800Sstevel@tonic-gate return (rv); 2810Sstevel@tonic-gate } 2820Sstevel@tonic-gate 2834845Svikram static void 2844845Svikram dr_get_comp_cond(dr_io_unit_t *ip, dev_info_t *dip) 2854845Svikram { 2864845Svikram if (dip == NULL) { 2874845Svikram ip->sbi_cm.sbdev_cond = SBD_COND_UNKNOWN; 2884845Svikram return; 2894845Svikram } 2904845Svikram 2914845Svikram if (DEVI(dip)->devi_flags & DEVI_RETIRED) { 2924845Svikram ip->sbi_cm.sbdev_cond = SBD_COND_FAILED; 2934845Svikram return; 2944845Svikram } 2954845Svikram 2964845Svikram if (DR_DEV_IS_ATTACHED(&ip->sbi_cm)) { 2974845Svikram ip->sbi_cm.sbdev_cond = SBD_COND_OK; 2984845Svikram } else if (DR_DEV_IS_PRESENT(&ip->sbi_cm)) { 2994845Svikram ip->sbi_cm.sbdev_cond = SBD_COND_OK; 3004845Svikram } 3014845Svikram } 3024845Svikram 3030Sstevel@tonic-gate int 3040Sstevel@tonic-gate dr_io_status(dr_handle_t *hp, dr_devset_t devset, sbd_dev_stat_t *dsp) 3050Sstevel@tonic-gate { 3060Sstevel@tonic-gate int i, ix; 3070Sstevel@tonic-gate dr_board_t *bp; 3080Sstevel@tonic-gate sbd_io_stat_t *isp; 3090Sstevel@tonic-gate dr_io_unit_t *ip; 3100Sstevel@tonic-gate 3110Sstevel@tonic-gate bp = hp->h_bd; 3120Sstevel@tonic-gate 3130Sstevel@tonic-gate /* 3140Sstevel@tonic-gate * Only look for requested devices that are actually present. 3150Sstevel@tonic-gate */ 3160Sstevel@tonic-gate devset &= DR_DEVS_PRESENT(bp); 3170Sstevel@tonic-gate 3180Sstevel@tonic-gate for (i = ix = 0; i < MAX_IO_UNITS_PER_BOARD; i++) { 3190Sstevel@tonic-gate drmachid_t id; 3200Sstevel@tonic-gate dev_info_t *dip; 3210Sstevel@tonic-gate sbd_error_t *err; 3220Sstevel@tonic-gate drmach_status_t pstat; 3230Sstevel@tonic-gate 3240Sstevel@tonic-gate if (DEVSET_IN_SET(devset, SBD_COMP_IO, i) == 0) 3250Sstevel@tonic-gate continue; 3260Sstevel@tonic-gate 3270Sstevel@tonic-gate ip = dr_get_io_unit(bp, i); 3280Sstevel@tonic-gate 3290Sstevel@tonic-gate if (ip->sbi_cm.sbdev_state == DR_STATE_EMPTY) { 3300Sstevel@tonic-gate /* present, but not fully initialized */ 3310Sstevel@tonic-gate continue; 3320Sstevel@tonic-gate } 3330Sstevel@tonic-gate 3340Sstevel@tonic-gate id = ip->sbi_cm.sbdev_id; 3350Sstevel@tonic-gate if (id == (drmachid_t)0) 3360Sstevel@tonic-gate continue; 3370Sstevel@tonic-gate 3380Sstevel@tonic-gate err = drmach_status(ip->sbi_cm.sbdev_id, &pstat); 3390Sstevel@tonic-gate if (err) { 3400Sstevel@tonic-gate DRERR_SET_C(&ip->sbi_cm.sbdev_error, &err); 3410Sstevel@tonic-gate return (-1); 3420Sstevel@tonic-gate } 3430Sstevel@tonic-gate 3440Sstevel@tonic-gate dip = NULL; 3450Sstevel@tonic-gate err = drmach_get_dip(id, &dip); 3460Sstevel@tonic-gate if (err) { 3470Sstevel@tonic-gate /* catch this in debug kernels */ 3480Sstevel@tonic-gate ASSERT(0); 3490Sstevel@tonic-gate 3500Sstevel@tonic-gate sbd_err_clear(&err); 3510Sstevel@tonic-gate continue; 3524845Svikram } 3534845Svikram 3544845Svikram isp = &dsp->d_io; 3554845Svikram bzero((caddr_t)isp, sizeof (*isp)); 3564845Svikram 3574845Svikram isp->is_cm.c_id.c_type = ip->sbi_cm.sbdev_type; 3584845Svikram isp->is_cm.c_id.c_unit = ip->sbi_cm.sbdev_unum; 359*11311SSurya.Prakki@Sun.COM (void) strncpy(isp->is_cm.c_id.c_name, pstat.type, 360*11311SSurya.Prakki@Sun.COM sizeof (isp->is_cm.c_id.c_name)); 3614845Svikram 3624845Svikram dr_get_comp_cond(ip, dip); 3634845Svikram isp->is_cm.c_cond = ip->sbi_cm.sbdev_cond; 3644845Svikram isp->is_cm.c_busy = ip->sbi_cm.sbdev_busy | pstat.busy; 3654845Svikram isp->is_cm.c_time = ip->sbi_cm.sbdev_time; 3664845Svikram isp->is_cm.c_ostate = ip->sbi_cm.sbdev_ostate; 3674845Svikram isp->is_cm.c_sflags = 0; 3684845Svikram 3694845Svikram if (dip == NULL) { 3700Sstevel@tonic-gate isp->is_pathname[0] = '\0'; 3710Sstevel@tonic-gate isp->is_referenced = 0; 3720Sstevel@tonic-gate isp->is_unsafe_count = 0; 3730Sstevel@tonic-gate } else { 3740Sstevel@tonic-gate int refcount = 0, idx = 0; 3750Sstevel@tonic-gate uint64_t unsafe_devs[SBD_MAX_UNSAFE]; 3760Sstevel@tonic-gate 3770Sstevel@tonic-gate ASSERT(e_ddi_branch_held(dip)); 3780Sstevel@tonic-gate (void) ddi_pathname(dip, isp->is_pathname); 3790Sstevel@tonic-gate 3800Sstevel@tonic-gate /* check reference and unsafe counts on devices */ 3810Sstevel@tonic-gate isp->is_unsafe_count = 0; 3820Sstevel@tonic-gate dr_check_devices(dip, &refcount, hp, unsafe_devs, 383*11311SSurya.Prakki@Sun.COM &idx, SBD_MAX_UNSAFE); 3840Sstevel@tonic-gate while (idx > 0) { 3850Sstevel@tonic-gate isp->is_unsafe_list[idx-1] = unsafe_devs[idx-1]; 3860Sstevel@tonic-gate --idx; 3870Sstevel@tonic-gate } 3880Sstevel@tonic-gate 3890Sstevel@tonic-gate isp->is_referenced = (refcount == 0) ? 0 : 1; 3900Sstevel@tonic-gate 3910Sstevel@tonic-gate hp->h_err = NULL; 3920Sstevel@tonic-gate } 3930Sstevel@tonic-gate ix++; 3940Sstevel@tonic-gate dsp++; 3950Sstevel@tonic-gate } 3960Sstevel@tonic-gate 3970Sstevel@tonic-gate return (ix); 3980Sstevel@tonic-gate } 399