11709Smlf /* 21709Smlf * CDDL HEADER START 31709Smlf * 41709Smlf * The contents of this file are subject to the terms of the 53525Sshidokht * Common Development and Distribution License (the "License"). 63525Sshidokht * You may not use this file except in compliance with the License. 71709Smlf * 81709Smlf * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 91709Smlf * or http://www.opensolaris.org/os/licensing. 101709Smlf * See the License for the specific language governing permissions 111709Smlf * and limitations under the License. 121709Smlf * 131709Smlf * When distributing Covered Code, include this CDDL HEADER in each 141709Smlf * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 151709Smlf * If applicable, add the following below this CDDL HEADER, with the 161709Smlf * fields enclosed by brackets "[]" replaced with your own identifying 171709Smlf * information: Portions Copyright [yyyy] [name of copyright owner] 181709Smlf * 191709Smlf * CDDL HEADER END 201709Smlf */ 211709Smlf /* 226318Sedp * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 231709Smlf * Use is subject to license terms. 241709Smlf */ 251709Smlf 261709Smlf #pragma ident "%Z%%M% %I% %E% SMI" 271709Smlf 281709Smlf #include <sys/scsi/scsi.h> 291709Smlf #include <sys/dktp/cm.h> 301709Smlf #include <sys/dktp/quetypes.h> 311709Smlf #include <sys/dktp/queue.h> 321709Smlf #include <sys/dktp/fctypes.h> 331709Smlf #include <sys/dktp/flowctrl.h> 341709Smlf #include <sys/dktp/cmdev.h> 351709Smlf #include <sys/dkio.h> 361709Smlf #include <sys/dktp/tgdk.h> 371709Smlf #include <sys/dktp/dadk.h> 381709Smlf #include <sys/dktp/bbh.h> 391709Smlf #include <sys/dktp/altsctr.h> 401709Smlf #include <sys/dktp/cmdk.h> 411709Smlf 421709Smlf #include <sys/stat.h> 431709Smlf #include <sys/vtoc.h> 441709Smlf #include <sys/file.h> 451709Smlf #include <sys/dktp/dadkio.h> 461709Smlf #include <sys/aio_req.h> 471709Smlf 481709Smlf #include <sys/cmlb.h> 491709Smlf 501709Smlf /* 511709Smlf * Local Static Data 521709Smlf */ 531709Smlf #ifdef CMDK_DEBUG 541709Smlf #define DENT 0x0001 551709Smlf #define DIO 0x0002 561709Smlf 571709Smlf static int cmdk_debug = DIO; 581709Smlf #endif 591709Smlf 601709Smlf #ifndef TRUE 611709Smlf #define TRUE 1 621709Smlf #endif 631709Smlf 641709Smlf #ifndef FALSE 651709Smlf #define FALSE 0 661709Smlf #endif 671709Smlf 681709Smlf /* 691709Smlf * NDKMAP is the base number for accessing the fdisk partitions. 701709Smlf * c?d?p0 --> cmdk@?,?:q 711709Smlf */ 721709Smlf #define PARTITION0_INDEX (NDKMAP + 0) 731709Smlf 741709Smlf #define DKTP_DATA (dkp->dk_tgobjp)->tg_data 751709Smlf #define DKTP_EXT (dkp->dk_tgobjp)->tg_ext 761709Smlf 771709Smlf static void *cmdk_state; 781709Smlf 791709Smlf /* 801709Smlf * the cmdk_attach_mutex protects cmdk_max_instance in multi-threaded 811709Smlf * attach situations 821709Smlf */ 831709Smlf static kmutex_t cmdk_attach_mutex; 841709Smlf static int cmdk_max_instance = 0; 851709Smlf 861709Smlf /* 871709Smlf * Panic dumpsys state 881709Smlf * There is only a single flag that is not mutex locked since 891709Smlf * the system is prevented from thread switching and cmdk_dump 901709Smlf * will only be called in a single threaded operation. 911709Smlf */ 921709Smlf static int cmdk_indump; 931709Smlf 941709Smlf /* 951709Smlf * Local Function Prototypes 961709Smlf */ 971709Smlf static int cmdk_create_obj(dev_info_t *dip, struct cmdk *dkp); 981709Smlf static void cmdk_destroy_obj(dev_info_t *dip, struct cmdk *dkp); 991709Smlf static void cmdkmin(struct buf *bp); 1001709Smlf static int cmdkrw(dev_t dev, struct uio *uio, int flag); 1011709Smlf static int cmdkarw(dev_t dev, struct aio_req *aio, int flag); 1021709Smlf 1031709Smlf /* 1041709Smlf * Bad Block Handling Functions Prototypes 1051709Smlf */ 1061709Smlf static void cmdk_bbh_reopen(struct cmdk *dkp); 1071709Smlf static opaque_t cmdk_bbh_gethandle(opaque_t bbh_data, struct buf *bp); 1081709Smlf static bbh_cookie_t cmdk_bbh_htoc(opaque_t bbh_data, opaque_t handle); 1091709Smlf static void cmdk_bbh_freehandle(opaque_t bbh_data, opaque_t handle); 1101709Smlf static void cmdk_bbh_close(struct cmdk *dkp); 1111709Smlf static void cmdk_bbh_setalts_idx(struct cmdk *dkp); 1121709Smlf static int cmdk_bbh_bsearch(struct alts_ent *buf, int cnt, daddr32_t key); 1131709Smlf 1141709Smlf static struct bbh_objops cmdk_bbh_ops = { 1151709Smlf nulldev, 1161709Smlf nulldev, 1171709Smlf cmdk_bbh_gethandle, 1181709Smlf cmdk_bbh_htoc, 1191709Smlf cmdk_bbh_freehandle, 1201709Smlf 0, 0 1211709Smlf }; 1221709Smlf 1231709Smlf static int cmdkopen(dev_t *dev_p, int flag, int otyp, cred_t *credp); 1241709Smlf static int cmdkclose(dev_t dev, int flag, int otyp, cred_t *credp); 1251709Smlf static int cmdkstrategy(struct buf *bp); 1261709Smlf static int cmdkdump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk); 1271709Smlf static int cmdkioctl(dev_t, int, intptr_t, int, cred_t *, int *); 1281709Smlf static int cmdkread(dev_t dev, struct uio *uio, cred_t *credp); 1291709Smlf static int cmdkwrite(dev_t dev, struct uio *uio, cred_t *credp); 1301709Smlf static int cmdk_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, 1311709Smlf int mod_flags, char *name, caddr_t valuep, int *lengthp); 1321709Smlf static int cmdkaread(dev_t dev, struct aio_req *aio, cred_t *credp); 1331709Smlf static int cmdkawrite(dev_t dev, struct aio_req *aio, cred_t *credp); 1341709Smlf 1351709Smlf /* 1361709Smlf * Device driver ops vector 1371709Smlf */ 1381709Smlf 1391709Smlf static struct cb_ops cmdk_cb_ops = { 1401709Smlf cmdkopen, /* open */ 1411709Smlf cmdkclose, /* close */ 1421709Smlf cmdkstrategy, /* strategy */ 1431709Smlf nodev, /* print */ 1441709Smlf cmdkdump, /* dump */ 1451709Smlf cmdkread, /* read */ 1461709Smlf cmdkwrite, /* write */ 1471709Smlf cmdkioctl, /* ioctl */ 1481709Smlf nodev, /* devmap */ 1491709Smlf nodev, /* mmap */ 1501709Smlf nodev, /* segmap */ 1511709Smlf nochpoll, /* poll */ 1521709Smlf cmdk_prop_op, /* cb_prop_op */ 1531709Smlf 0, /* streamtab */ 1541709Smlf D_64BIT | D_MP | D_NEW, /* Driver comaptibility flag */ 1551709Smlf CB_REV, /* cb_rev */ 1561709Smlf cmdkaread, /* async read */ 1571709Smlf cmdkawrite /* async write */ 1581709Smlf }; 1591709Smlf 1601709Smlf static int cmdkinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, 1611709Smlf void **result); 1621709Smlf static int cmdkprobe(dev_info_t *dip); 1631709Smlf static int cmdkattach(dev_info_t *dip, ddi_attach_cmd_t cmd); 1641709Smlf static int cmdkdetach(dev_info_t *dip, ddi_detach_cmd_t cmd); 1651709Smlf 1665295Srandyf static void cmdk_setup_pm(dev_info_t *dip, struct cmdk *dkp); 1675295Srandyf static int cmdkresume(dev_info_t *dip); 1685295Srandyf static int cmdksuspend(dev_info_t *dip); 1695295Srandyf static int cmdkpower(dev_info_t *dip, int component, int level); 1705295Srandyf 1711709Smlf struct dev_ops cmdk_ops = { 1721709Smlf DEVO_REV, /* devo_rev, */ 1731709Smlf 0, /* refcnt */ 1741709Smlf cmdkinfo, /* info */ 1751709Smlf nulldev, /* identify */ 1761709Smlf cmdkprobe, /* probe */ 1771709Smlf cmdkattach, /* attach */ 1781709Smlf cmdkdetach, /* detach */ 1791709Smlf nodev, /* reset */ 1801709Smlf &cmdk_cb_ops, /* driver operations */ 1815295Srandyf (struct bus_ops *)0, /* bus operations */ 1825295Srandyf cmdkpower /* power */ 1831709Smlf }; 1841709Smlf 1851709Smlf /* 1861709Smlf * This is the loadable module wrapper. 1871709Smlf */ 1881709Smlf #include <sys/modctl.h> 1891709Smlf 1901709Smlf extern struct mod_ops mod_driverops; 1911709Smlf 1921709Smlf static struct modldrv modldrv = { 1931709Smlf &mod_driverops, /* Type of module. This one is a driver */ 1941709Smlf "Common Direct Access Disk %I%", 1951709Smlf &cmdk_ops, /* driver ops */ 1961709Smlf }; 1971709Smlf 1981709Smlf static struct modlinkage modlinkage = { 1991709Smlf MODREV_1, (void *)&modldrv, NULL 2001709Smlf }; 2011709Smlf 2021709Smlf /* Function prototypes for cmlb callbacks */ 2031709Smlf 2041709Smlf static int cmdk_lb_rdwr(dev_info_t *dip, uchar_t cmd, void *bufaddr, 2053525Sshidokht diskaddr_t start, size_t length, void *tg_cookie); 2063525Sshidokht 2073525Sshidokht static int cmdk_lb_getinfo(dev_info_t *dip, int cmd, void *arg, 2083525Sshidokht void *tg_cookie); 2091709Smlf 2101709Smlf static void cmdk_devid_setup(struct cmdk *dkp); 2111709Smlf static int cmdk_devid_modser(struct cmdk *dkp); 2121709Smlf static int cmdk_get_modser(struct cmdk *dkp, int ioccmd, char *buf, int len); 2131709Smlf static int cmdk_devid_fabricate(struct cmdk *dkp); 2141709Smlf static int cmdk_devid_read(struct cmdk *dkp); 2151709Smlf 2161709Smlf static cmlb_tg_ops_t cmdk_lb_ops = { 2173525Sshidokht TG_DK_OPS_VERSION_1, 2181709Smlf cmdk_lb_rdwr, 2193525Sshidokht cmdk_lb_getinfo 2201709Smlf }; 2211709Smlf 2226318Sedp static boolean_t 2236318Sedp cmdk_isopen(struct cmdk *dkp, dev_t dev) 2246318Sedp { 2256318Sedp int part, otyp; 2266318Sedp ulong_t partbit; 2276318Sedp 2286318Sedp ASSERT(MUTEX_HELD((&dkp->dk_mutex))); 2296318Sedp 2306318Sedp part = CMDKPART(dev); 2316318Sedp partbit = 1 << part; 2326318Sedp 2336318Sedp /* account for close */ 2346318Sedp if (dkp->dk_open_lyr[part] != 0) 2356318Sedp return (B_TRUE); 2366318Sedp for (otyp = 0; otyp < OTYPCNT; otyp++) 2376318Sedp if (dkp->dk_open_reg[otyp] & partbit) 2386318Sedp return (B_TRUE); 2396318Sedp return (B_FALSE); 2406318Sedp } 2416318Sedp 2421709Smlf int 2431709Smlf _init(void) 2441709Smlf { 2451709Smlf int rval; 2461709Smlf 2471709Smlf if (rval = ddi_soft_state_init(&cmdk_state, sizeof (struct cmdk), 7)) 2481709Smlf return (rval); 2491709Smlf 2501709Smlf mutex_init(&cmdk_attach_mutex, NULL, MUTEX_DRIVER, NULL); 2511709Smlf if ((rval = mod_install(&modlinkage)) != 0) { 2521709Smlf mutex_destroy(&cmdk_attach_mutex); 2531709Smlf ddi_soft_state_fini(&cmdk_state); 2541709Smlf } 2551709Smlf return (rval); 2561709Smlf } 2571709Smlf 2581709Smlf int 2591709Smlf _fini(void) 2601709Smlf { 2611709Smlf return (EBUSY); 2621709Smlf 2631709Smlf /* 2641709Smlf * This has been commented out until cmdk is a true 2651709Smlf * unloadable module. Right now x86's are panicking on 2661709Smlf * a diskless reconfig boot. 2671709Smlf */ 2681709Smlf 2691709Smlf #if 0 /* bugid 1186679 */ 2701709Smlf int rval; 2711709Smlf 2721709Smlf rval = mod_remove(&modlinkage); 2731709Smlf if (rval != 0) 2741709Smlf return (rval); 2751709Smlf 2761709Smlf mutex_destroy(&cmdk_attach_mutex); 2771709Smlf ddi_soft_state_fini(&cmdk_state); 2781709Smlf 2791709Smlf return (0); 2801709Smlf #endif 2811709Smlf } 2821709Smlf 2831709Smlf int 2841709Smlf _info(struct modinfo *modinfop) 2851709Smlf { 2861709Smlf return (mod_info(&modlinkage, modinfop)); 2871709Smlf } 2881709Smlf 2891709Smlf /* 2901709Smlf * Autoconfiguration Routines 2911709Smlf */ 2921709Smlf static int 2931709Smlf cmdkprobe(dev_info_t *dip) 2941709Smlf { 2951709Smlf int instance; 2961709Smlf int status; 2971709Smlf struct cmdk *dkp; 2981709Smlf 2991709Smlf instance = ddi_get_instance(dip); 3001709Smlf 3011709Smlf if (ddi_get_soft_state(cmdk_state, instance)) 3021709Smlf return (DDI_PROBE_PARTIAL); 3031709Smlf 3041709Smlf if ((ddi_soft_state_zalloc(cmdk_state, instance) != DDI_SUCCESS) || 3051709Smlf ((dkp = ddi_get_soft_state(cmdk_state, instance)) == NULL)) 3061709Smlf return (DDI_PROBE_PARTIAL); 3071709Smlf 3081709Smlf mutex_init(&dkp->dk_mutex, NULL, MUTEX_DRIVER, NULL); 3091709Smlf rw_init(&dkp->dk_bbh_mutex, NULL, RW_DRIVER, NULL); 3101709Smlf dkp->dk_dip = dip; 3111709Smlf mutex_enter(&dkp->dk_mutex); 3121709Smlf 3131709Smlf dkp->dk_dev = makedevice(ddi_driver_major(dip), 3141709Smlf ddi_get_instance(dip) << CMDK_UNITSHF); 3151709Smlf 3161709Smlf /* linkage to dadk and strategy */ 3171709Smlf if (cmdk_create_obj(dip, dkp) != DDI_SUCCESS) { 3181709Smlf mutex_exit(&dkp->dk_mutex); 3191709Smlf mutex_destroy(&dkp->dk_mutex); 3201709Smlf rw_destroy(&dkp->dk_bbh_mutex); 3211709Smlf ddi_soft_state_free(cmdk_state, instance); 3221709Smlf return (DDI_PROBE_PARTIAL); 3231709Smlf } 3241709Smlf 3251709Smlf status = dadk_probe(DKTP_DATA, KM_NOSLEEP); 3261709Smlf if (status != DDI_PROBE_SUCCESS) { 3271709Smlf cmdk_destroy_obj(dip, dkp); /* dadk/strategy linkage */ 3281709Smlf mutex_exit(&dkp->dk_mutex); 3291709Smlf mutex_destroy(&dkp->dk_mutex); 3301709Smlf rw_destroy(&dkp->dk_bbh_mutex); 3311709Smlf ddi_soft_state_free(cmdk_state, instance); 3321709Smlf return (status); 3331709Smlf } 3341709Smlf 3351709Smlf mutex_exit(&dkp->dk_mutex); 3361709Smlf #ifdef CMDK_DEBUG 3371709Smlf if (cmdk_debug & DENT) 3381709Smlf PRF("cmdkprobe: instance= %d name= `%s`\n", 3391709Smlf instance, ddi_get_name_addr(dip)); 3401709Smlf #endif 3411709Smlf return (status); 3421709Smlf } 3431709Smlf 3441709Smlf static int 3451709Smlf cmdkattach(dev_info_t *dip, ddi_attach_cmd_t cmd) 3461709Smlf { 3471709Smlf int instance; 3481709Smlf struct cmdk *dkp; 3491709Smlf char *node_type; 3501709Smlf 3515295Srandyf switch (cmd) { 3525295Srandyf case DDI_ATTACH: 3535295Srandyf break; 3545295Srandyf case DDI_RESUME: 3555295Srandyf return (cmdkresume(dip)); 3565295Srandyf default: 3571709Smlf return (DDI_FAILURE); 3585295Srandyf } 3591709Smlf 3601709Smlf instance = ddi_get_instance(dip); 3611709Smlf if (!(dkp = ddi_get_soft_state(cmdk_state, instance))) 3621709Smlf return (DDI_FAILURE); 3631709Smlf 3645295Srandyf dkp->dk_pm_level = CMDK_SPINDLE_UNINIT; 3655295Srandyf mutex_init(&dkp->dk_mutex, NULL, MUTEX_DRIVER, NULL); 3665295Srandyf 3671709Smlf mutex_enter(&dkp->dk_mutex); 3681709Smlf 3691709Smlf /* dadk_attach is an empty function that only returns SUCCESS */ 3701709Smlf (void) dadk_attach(DKTP_DATA); 3711709Smlf 3721709Smlf node_type = (DKTP_EXT->tg_nodetype); 3731709Smlf 3741709Smlf /* 3751709Smlf * this open allows cmlb to read the device 3761709Smlf * and determine the label types 3771709Smlf * so that cmlb can create minor nodes for device 3781709Smlf */ 3791709Smlf 3801709Smlf /* open the target disk */ 3811709Smlf if (dadk_open(DKTP_DATA, 0) != DDI_SUCCESS) 3821709Smlf goto fail2; 3831709Smlf 3841709Smlf /* mark as having opened target */ 3851709Smlf dkp->dk_flag |= CMDK_TGDK_OPEN; 3861709Smlf 3871709Smlf cmlb_alloc_handle((cmlb_handle_t *)&dkp->dk_cmlbhandle); 3881709Smlf 3891709Smlf if (cmlb_attach(dip, 3901709Smlf &cmdk_lb_ops, 3911709Smlf DTYPE_DIRECT, /* device_type */ 3921709Smlf 0, /* removable */ 3933525Sshidokht 0, /* hot pluggable XXX */ 3941709Smlf node_type, 3951709Smlf CMLB_CREATE_ALTSLICE_VTOC_16_DTYPE_DIRECT, /* alter_behaviour */ 3963525Sshidokht dkp->dk_cmlbhandle, 3973525Sshidokht 0) != 0) 3981709Smlf goto fail1; 3991709Smlf 4001709Smlf /* Calling validate will create minor nodes according to disk label */ 4013525Sshidokht (void) cmlb_validate(dkp->dk_cmlbhandle, 0, 0); 4021709Smlf 4031709Smlf /* set bbh (Bad Block Handling) */ 4041709Smlf cmdk_bbh_reopen(dkp); 4051709Smlf 4061709Smlf /* setup devid string */ 4071709Smlf cmdk_devid_setup(dkp); 4081709Smlf 4091709Smlf mutex_enter(&cmdk_attach_mutex); 4101709Smlf if (instance > cmdk_max_instance) 4111709Smlf cmdk_max_instance = instance; 4121709Smlf mutex_exit(&cmdk_attach_mutex); 4131709Smlf 4141709Smlf mutex_exit(&dkp->dk_mutex); 4151709Smlf 4161709Smlf /* 4171709Smlf * Add a zero-length attribute to tell the world we support 4181709Smlf * kernel ioctls (for layered drivers) 4191709Smlf */ 4201709Smlf (void) ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP, 4211709Smlf DDI_KERNEL_IOCTL, NULL, 0); 4221709Smlf ddi_report_dev(dip); 4231709Smlf 4245295Srandyf /* 4255295Srandyf * Initialize power management 4265295Srandyf */ 4275295Srandyf mutex_init(&dkp->dk_pm_mutex, NULL, MUTEX_DRIVER, NULL); 4285295Srandyf cv_init(&dkp->dk_suspend_cv, NULL, CV_DRIVER, NULL); 4295295Srandyf cmdk_setup_pm(dip, dkp); 4305295Srandyf 4311709Smlf return (DDI_SUCCESS); 4321709Smlf 4331709Smlf fail1: 4341709Smlf cmlb_free_handle(&dkp->dk_cmlbhandle); 4351709Smlf (void) dadk_close(DKTP_DATA); 4361709Smlf fail2: 4371709Smlf cmdk_destroy_obj(dip, dkp); 4381709Smlf rw_destroy(&dkp->dk_bbh_mutex); 4391709Smlf mutex_exit(&dkp->dk_mutex); 4401709Smlf mutex_destroy(&dkp->dk_mutex); 4411709Smlf ddi_soft_state_free(cmdk_state, instance); 4421709Smlf return (DDI_FAILURE); 4431709Smlf } 4441709Smlf 4451709Smlf 4461709Smlf static int 4471709Smlf cmdkdetach(dev_info_t *dip, ddi_detach_cmd_t cmd) 4481709Smlf { 4491709Smlf struct cmdk *dkp; 4501709Smlf int instance; 4511709Smlf int max_instance; 4521709Smlf 4535295Srandyf switch (cmd) { 4545295Srandyf case DDI_DETACH: 4555295Srandyf /* return (DDI_FAILURE); */ 4565295Srandyf break; 4575295Srandyf case DDI_SUSPEND: 4585295Srandyf return (cmdksuspend(dip)); 4595295Srandyf default: 4601709Smlf #ifdef CMDK_DEBUG 4611709Smlf if (cmdk_debug & DIO) { 4621709Smlf PRF("cmdkdetach: cmd = %d unknown\n", cmd); 4631709Smlf } 4641709Smlf #endif 4651709Smlf return (DDI_FAILURE); 4661709Smlf } 4671709Smlf 4681709Smlf mutex_enter(&cmdk_attach_mutex); 4691709Smlf max_instance = cmdk_max_instance; 4701709Smlf mutex_exit(&cmdk_attach_mutex); 4711709Smlf 4721709Smlf /* check if any instance of driver is open */ 4731709Smlf for (instance = 0; instance < max_instance; instance++) { 4741709Smlf dkp = ddi_get_soft_state(cmdk_state, instance); 4751709Smlf if (!dkp) 4761709Smlf continue; 4771709Smlf if (dkp->dk_flag & CMDK_OPEN) 4781709Smlf return (DDI_FAILURE); 4791709Smlf } 4801709Smlf 4811709Smlf instance = ddi_get_instance(dip); 4821709Smlf if (!(dkp = ddi_get_soft_state(cmdk_state, instance))) 4831709Smlf return (DDI_SUCCESS); 4841709Smlf 4851709Smlf mutex_enter(&dkp->dk_mutex); 4861709Smlf 4871709Smlf /* 4881709Smlf * The cmdk_part_info call at the end of cmdkattach may have 4891709Smlf * caused cmdk_reopen to do a TGDK_OPEN, make sure we close on 4901709Smlf * detach for case when cmdkopen/cmdkclose never occurs. 4911709Smlf */ 4921709Smlf if (dkp->dk_flag & CMDK_TGDK_OPEN) { 4931709Smlf dkp->dk_flag &= ~CMDK_TGDK_OPEN; 4941709Smlf (void) dadk_close(DKTP_DATA); 4951709Smlf } 4961709Smlf 4973525Sshidokht cmlb_detach(dkp->dk_cmlbhandle, 0); 4981709Smlf cmlb_free_handle(&dkp->dk_cmlbhandle); 4991709Smlf ddi_prop_remove_all(dip); 5001709Smlf 5011709Smlf cmdk_destroy_obj(dip, dkp); /* dadk/strategy linkage */ 5021709Smlf mutex_exit(&dkp->dk_mutex); 5031709Smlf mutex_destroy(&dkp->dk_mutex); 5041709Smlf rw_destroy(&dkp->dk_bbh_mutex); 5055295Srandyf mutex_destroy(&dkp->dk_pm_mutex); 5065295Srandyf cv_destroy(&dkp->dk_suspend_cv); 5071709Smlf ddi_soft_state_free(cmdk_state, instance); 5081709Smlf 5091709Smlf return (DDI_SUCCESS); 5101709Smlf } 5111709Smlf 5121709Smlf static int 5131709Smlf cmdkinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 5141709Smlf { 5151709Smlf dev_t dev = (dev_t)arg; 5161709Smlf int instance; 5171709Smlf struct cmdk *dkp; 5181709Smlf 5191709Smlf #ifdef lint 5201709Smlf dip = dip; /* no one ever uses this */ 5211709Smlf #endif 5221709Smlf #ifdef CMDK_DEBUG 5231709Smlf if (cmdk_debug & DENT) 5241709Smlf PRF("cmdkinfo: call\n"); 5251709Smlf #endif 5261709Smlf instance = CMDKUNIT(dev); 5271709Smlf 5281709Smlf switch (infocmd) { 5291709Smlf case DDI_INFO_DEVT2DEVINFO: 5301709Smlf if (!(dkp = ddi_get_soft_state(cmdk_state, instance))) 5311709Smlf return (DDI_FAILURE); 5321709Smlf *result = (void *) dkp->dk_dip; 5331709Smlf break; 5341709Smlf case DDI_INFO_DEVT2INSTANCE: 5351709Smlf *result = (void *)(intptr_t)instance; 5361709Smlf break; 5371709Smlf default: 5381709Smlf return (DDI_FAILURE); 5391709Smlf } 5401709Smlf return (DDI_SUCCESS); 5411709Smlf } 5421709Smlf 5435295Srandyf /* 5445295Srandyf * Initialize the power management components 5455295Srandyf */ 5465295Srandyf static void 5475295Srandyf cmdk_setup_pm(dev_info_t *dip, struct cmdk *dkp) 5485295Srandyf { 5495295Srandyf char *pm_comp[] = { "NAME=cmdk", "0=off", "1=on", NULL }; 5505295Srandyf 5515295Srandyf /* 5525295Srandyf * Since the cmdk device does not the 'reg' property, 5535295Srandyf * cpr will not call its DDI_SUSPEND/DDI_RESUME entries. 5545295Srandyf * The following code is to tell cpr that this device 5555295Srandyf * DOES need to be suspended and resumed. 5565295Srandyf */ 5575295Srandyf (void) ddi_prop_update_string(DDI_DEV_T_NONE, dip, 5585295Srandyf "pm-hardware-state", "needs-suspend-resume"); 5595295Srandyf 5605295Srandyf if (ddi_prop_update_string_array(DDI_DEV_T_NONE, dip, 5615295Srandyf "pm-components", pm_comp, 3) == DDI_PROP_SUCCESS) { 5625295Srandyf if (pm_raise_power(dip, 0, CMDK_SPINDLE_ON) == DDI_SUCCESS) { 5635295Srandyf mutex_enter(&dkp->dk_pm_mutex); 5645295Srandyf dkp->dk_pm_level = CMDK_SPINDLE_ON; 5655295Srandyf dkp->dk_pm_is_enabled = 1; 5665295Srandyf mutex_exit(&dkp->dk_pm_mutex); 5675295Srandyf } else { 5685295Srandyf mutex_enter(&dkp->dk_pm_mutex); 5695295Srandyf dkp->dk_pm_level = CMDK_SPINDLE_OFF; 5705295Srandyf dkp->dk_pm_is_enabled = 0; 5715295Srandyf mutex_exit(&dkp->dk_pm_mutex); 5725295Srandyf } 5735295Srandyf } else { 5745295Srandyf mutex_enter(&dkp->dk_pm_mutex); 5755295Srandyf dkp->dk_pm_level = CMDK_SPINDLE_UNINIT; 5765295Srandyf dkp->dk_pm_is_enabled = 0; 5775295Srandyf mutex_exit(&dkp->dk_pm_mutex); 5785295Srandyf } 5795295Srandyf } 5805295Srandyf 5815295Srandyf /* 5825295Srandyf * suspend routine, it will be run when get the command 5835295Srandyf * DDI_SUSPEND at detach(9E) from system power management 5845295Srandyf */ 5855295Srandyf static int 5865295Srandyf cmdksuspend(dev_info_t *dip) 5875295Srandyf { 5885295Srandyf struct cmdk *dkp; 5895295Srandyf int instance; 5905295Srandyf clock_t count = 0; 5915295Srandyf 5925295Srandyf instance = ddi_get_instance(dip); 5935295Srandyf if (!(dkp = ddi_get_soft_state(cmdk_state, instance))) 5945295Srandyf return (DDI_FAILURE); 5955295Srandyf mutex_enter(&dkp->dk_mutex); 5965295Srandyf if (dkp->dk_flag & CMDK_SUSPEND) { 5975295Srandyf mutex_exit(&dkp->dk_mutex); 5985295Srandyf return (DDI_SUCCESS); 5995295Srandyf } 6005295Srandyf dkp->dk_flag |= CMDK_SUSPEND; 6015295Srandyf 6025295Srandyf /* need to wait a while */ 6035295Srandyf while (dadk_getcmds(DKTP_DATA) != 0) { 6045295Srandyf delay(drv_usectohz(1000000)); 6055295Srandyf if (count > 60) { 6065295Srandyf dkp->dk_flag &= ~CMDK_SUSPEND; 6075295Srandyf cv_broadcast(&dkp->dk_suspend_cv); 6085295Srandyf mutex_exit(&dkp->dk_mutex); 6095295Srandyf return (DDI_FAILURE); 6105295Srandyf } 6115295Srandyf count++; 6125295Srandyf } 6135295Srandyf mutex_exit(&dkp->dk_mutex); 6145295Srandyf return (DDI_SUCCESS); 6155295Srandyf } 6165295Srandyf 6175295Srandyf /* 6185295Srandyf * resume routine, it will be run when get the command 6195295Srandyf * DDI_RESUME at attach(9E) from system power management 6205295Srandyf */ 6215295Srandyf static int 6225295Srandyf cmdkresume(dev_info_t *dip) 6235295Srandyf { 6245295Srandyf struct cmdk *dkp; 6255295Srandyf int instance; 6265295Srandyf 6275295Srandyf instance = ddi_get_instance(dip); 6285295Srandyf if (!(dkp = ddi_get_soft_state(cmdk_state, instance))) 6295295Srandyf return (DDI_FAILURE); 6305295Srandyf mutex_enter(&dkp->dk_mutex); 6315295Srandyf if (!(dkp->dk_flag & CMDK_SUSPEND)) { 6325295Srandyf mutex_exit(&dkp->dk_mutex); 6335295Srandyf return (DDI_FAILURE); 6345295Srandyf } 6355295Srandyf dkp->dk_pm_level = CMDK_SPINDLE_ON; 6365295Srandyf dkp->dk_flag &= ~CMDK_SUSPEND; 6375295Srandyf cv_broadcast(&dkp->dk_suspend_cv); 6385295Srandyf mutex_exit(&dkp->dk_mutex); 6395295Srandyf return (DDI_SUCCESS); 6405295Srandyf 6415295Srandyf } 6425295Srandyf 6435295Srandyf /* 6445295Srandyf * power management entry point, it was used to 6455295Srandyf * change power management component. 6465295Srandyf * Actually, the real hard drive suspend/resume 6475295Srandyf * was handled in ata, so this function is not 6485295Srandyf * doing any real work other than verifying that 6495295Srandyf * the disk is idle. 6505295Srandyf */ 6515295Srandyf static int 6525295Srandyf cmdkpower(dev_info_t *dip, int component, int level) 6535295Srandyf { 6545295Srandyf struct cmdk *dkp; 6555295Srandyf int instance; 6565295Srandyf 6575295Srandyf instance = ddi_get_instance(dip); 6585295Srandyf if (!(dkp = ddi_get_soft_state(cmdk_state, instance)) || 6595295Srandyf component != 0 || level > CMDK_SPINDLE_ON || 6605295Srandyf level < CMDK_SPINDLE_OFF) { 6615295Srandyf return (DDI_FAILURE); 6625295Srandyf } 6635295Srandyf 6645295Srandyf mutex_enter(&dkp->dk_pm_mutex); 6655295Srandyf if (dkp->dk_pm_is_enabled && dkp->dk_pm_level == level) { 6665295Srandyf mutex_exit(&dkp->dk_pm_mutex); 6675295Srandyf return (DDI_SUCCESS); 6685295Srandyf } 6695295Srandyf mutex_exit(&dkp->dk_pm_mutex); 6705295Srandyf 6715295Srandyf if ((level == CMDK_SPINDLE_OFF) && 6725295Srandyf (dadk_getcmds(DKTP_DATA) != 0)) { 6735295Srandyf return (DDI_FAILURE); 6745295Srandyf } 6755295Srandyf 6765295Srandyf mutex_enter(&dkp->dk_pm_mutex); 6775295Srandyf dkp->dk_pm_level = level; 6785295Srandyf mutex_exit(&dkp->dk_pm_mutex); 6795295Srandyf return (DDI_SUCCESS); 6805295Srandyf } 6815295Srandyf 6821709Smlf static int 6831709Smlf cmdk_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags, 6841709Smlf char *name, caddr_t valuep, int *lengthp) 6851709Smlf { 6861709Smlf struct cmdk *dkp; 6871709Smlf 6881709Smlf #ifdef CMDK_DEBUG 6891709Smlf if (cmdk_debug & DENT) 6901709Smlf PRF("cmdk_prop_op: call\n"); 6911709Smlf #endif 6921709Smlf 6931709Smlf dkp = ddi_get_soft_state(cmdk_state, ddi_get_instance(dip)); 694*7224Scth if (dkp == NULL) 695*7224Scth return (ddi_prop_op(dev, dip, prop_op, mod_flags, 696*7224Scth name, valuep, lengthp)); 6971709Smlf 698*7224Scth return (cmlb_prop_op(dkp->dk_cmlbhandle, 699*7224Scth dev, dip, prop_op, mod_flags, name, valuep, lengthp, 700*7224Scth CMDKPART(dev), NULL)); 7011709Smlf } 7021709Smlf 7031709Smlf /* 7041709Smlf * dump routine 7051709Smlf */ 7061709Smlf static int 7071709Smlf cmdkdump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk) 7081709Smlf { 7091709Smlf int instance; 7101709Smlf struct cmdk *dkp; 7111709Smlf diskaddr_t p_lblksrt; 7121709Smlf diskaddr_t p_lblkcnt; 7131709Smlf struct buf local; 7141709Smlf struct buf *bp; 7151709Smlf 7161709Smlf #ifdef CMDK_DEBUG 7171709Smlf if (cmdk_debug & DENT) 7181709Smlf PRF("cmdkdump: call\n"); 7191709Smlf #endif 7201709Smlf instance = CMDKUNIT(dev); 7211709Smlf if (!(dkp = ddi_get_soft_state(cmdk_state, instance)) || (blkno < 0)) 7221709Smlf return (ENXIO); 7231709Smlf 7241709Smlf if (cmlb_partinfo( 7251709Smlf dkp->dk_cmlbhandle, 7261709Smlf CMDKPART(dev), 7271709Smlf &p_lblkcnt, 7281709Smlf &p_lblksrt, 7291709Smlf NULL, 7303525Sshidokht NULL, 7313525Sshidokht 0)) { 7321709Smlf return (ENXIO); 7331709Smlf } 7341709Smlf 7351709Smlf if ((blkno+nblk) > p_lblkcnt) 7361709Smlf return (EINVAL); 7371709Smlf 7381709Smlf cmdk_indump = 1; /* Tell disk targets we are panic dumpping */ 7391709Smlf 7401709Smlf bp = &local; 7411709Smlf bzero(bp, sizeof (*bp)); 7421709Smlf bp->b_flags = B_BUSY; 7431709Smlf bp->b_un.b_addr = addr; 7441709Smlf bp->b_bcount = nblk << SCTRSHFT; 7451709Smlf SET_BP_SEC(bp, ((ulong_t)(p_lblksrt + blkno))); 7461709Smlf 7471709Smlf (void) dadk_dump(DKTP_DATA, bp); 7481709Smlf return (bp->b_error); 7491709Smlf } 7501709Smlf 7511709Smlf /* 7521709Smlf * Copy in the dadkio_rwcmd according to the user's data model. If needed, 7531709Smlf * convert it for our internal use. 7541709Smlf */ 7551709Smlf static int 7561709Smlf rwcmd_copyin(struct dadkio_rwcmd *rwcmdp, caddr_t inaddr, int flag) 7571709Smlf { 7581709Smlf switch (ddi_model_convert_from(flag)) { 7591709Smlf case DDI_MODEL_ILP32: { 7601709Smlf struct dadkio_rwcmd32 cmd32; 7611709Smlf 7621709Smlf if (ddi_copyin(inaddr, &cmd32, 7631709Smlf sizeof (struct dadkio_rwcmd32), flag)) { 7641709Smlf return (EFAULT); 7651709Smlf } 7661709Smlf 7671709Smlf rwcmdp->cmd = cmd32.cmd; 7681709Smlf rwcmdp->flags = cmd32.flags; 7691709Smlf rwcmdp->blkaddr = (daddr_t)cmd32.blkaddr; 7701709Smlf rwcmdp->buflen = cmd32.buflen; 7711709Smlf rwcmdp->bufaddr = (caddr_t)(intptr_t)cmd32.bufaddr; 7721709Smlf /* 7731709Smlf * Note: we do not convert the 'status' field, 7741709Smlf * as it should not contain valid data at this 7751709Smlf * point. 7761709Smlf */ 7771709Smlf bzero(&rwcmdp->status, sizeof (rwcmdp->status)); 7781709Smlf break; 7791709Smlf } 7801709Smlf case DDI_MODEL_NONE: { 7811709Smlf if (ddi_copyin(inaddr, rwcmdp, 7821709Smlf sizeof (struct dadkio_rwcmd), flag)) { 7831709Smlf return (EFAULT); 7841709Smlf } 7851709Smlf } 7861709Smlf } 7871709Smlf return (0); 7881709Smlf } 7891709Smlf 7901709Smlf /* 7911709Smlf * If necessary, convert the internal rwcmdp and status to the appropriate 7921709Smlf * data model and copy it out to the user. 7931709Smlf */ 7941709Smlf static int 7951709Smlf rwcmd_copyout(struct dadkio_rwcmd *rwcmdp, caddr_t outaddr, int flag) 7961709Smlf { 7971709Smlf switch (ddi_model_convert_from(flag)) { 7981709Smlf case DDI_MODEL_ILP32: { 7991709Smlf struct dadkio_rwcmd32 cmd32; 8001709Smlf 8011709Smlf cmd32.cmd = rwcmdp->cmd; 8021709Smlf cmd32.flags = rwcmdp->flags; 8031709Smlf cmd32.blkaddr = rwcmdp->blkaddr; 8041709Smlf cmd32.buflen = rwcmdp->buflen; 8051709Smlf ASSERT64(((uintptr_t)rwcmdp->bufaddr >> 32) == 0); 8061709Smlf cmd32.bufaddr = (caddr32_t)(uintptr_t)rwcmdp->bufaddr; 8071709Smlf 8081709Smlf cmd32.status.status = rwcmdp->status.status; 8091709Smlf cmd32.status.resid = rwcmdp->status.resid; 8101709Smlf cmd32.status.failed_blk_is_valid = 8111709Smlf rwcmdp->status.failed_blk_is_valid; 8121709Smlf cmd32.status.failed_blk = rwcmdp->status.failed_blk; 8131709Smlf cmd32.status.fru_code_is_valid = 8141709Smlf rwcmdp->status.fru_code_is_valid; 8151709Smlf cmd32.status.fru_code = rwcmdp->status.fru_code; 8161709Smlf 8171709Smlf bcopy(rwcmdp->status.add_error_info, 8181709Smlf cmd32.status.add_error_info, DADKIO_ERROR_INFO_LEN); 8191709Smlf 8201709Smlf if (ddi_copyout(&cmd32, outaddr, 8211709Smlf sizeof (struct dadkio_rwcmd32), flag)) 8221709Smlf return (EFAULT); 8231709Smlf break; 8241709Smlf } 8251709Smlf case DDI_MODEL_NONE: { 8261709Smlf if (ddi_copyout(rwcmdp, outaddr, 8271709Smlf sizeof (struct dadkio_rwcmd), flag)) 8281709Smlf return (EFAULT); 8291709Smlf } 8301709Smlf } 8311709Smlf return (0); 8321709Smlf } 8331709Smlf 8341709Smlf /* 8351709Smlf * ioctl routine 8361709Smlf */ 8371709Smlf static int 8381709Smlf cmdkioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *credp, int *rvalp) 8391709Smlf { 8401709Smlf int instance; 8411709Smlf struct scsi_device *devp; 8421709Smlf struct cmdk *dkp; 8431709Smlf char data[NBPSCTR]; 8441709Smlf 8451709Smlf instance = CMDKUNIT(dev); 8461709Smlf if (!(dkp = ddi_get_soft_state(cmdk_state, instance))) 8471709Smlf return (ENXIO); 8481709Smlf 8495295Srandyf mutex_enter(&dkp->dk_mutex); 8505295Srandyf while (dkp->dk_flag & CMDK_SUSPEND) { 8515295Srandyf cv_wait(&dkp->dk_suspend_cv, &dkp->dk_mutex); 8525295Srandyf } 8535295Srandyf mutex_exit(&dkp->dk_mutex); 8545295Srandyf 8551709Smlf bzero(data, sizeof (data)); 8561709Smlf 8571709Smlf switch (cmd) { 8581709Smlf 8591709Smlf case DKIOCGMEDIAINFO: { 8601709Smlf struct dk_minfo media_info; 8611709Smlf struct tgdk_geom phyg; 8621709Smlf 8631709Smlf /* dadk_getphygeom always returns success */ 8641709Smlf (void) dadk_getphygeom(DKTP_DATA, &phyg); 8651709Smlf 8661709Smlf media_info.dki_lbsize = phyg.g_secsiz; 8671709Smlf media_info.dki_capacity = phyg.g_cap; 8681709Smlf media_info.dki_media_type = DK_FIXED_DISK; 8691709Smlf 8701709Smlf if (ddi_copyout(&media_info, (void *)arg, 8711709Smlf sizeof (struct dk_minfo), flag)) { 8721709Smlf return (EFAULT); 8731709Smlf } else { 8741709Smlf return (0); 8751709Smlf } 8761709Smlf } 8771709Smlf 8781709Smlf case DKIOCINFO: { 8791709Smlf struct dk_cinfo *info = (struct dk_cinfo *)data; 8801709Smlf 8811709Smlf /* controller information */ 8821709Smlf info->dki_ctype = (DKTP_EXT->tg_ctype); 8831709Smlf info->dki_cnum = ddi_get_instance(ddi_get_parent(dkp->dk_dip)); 8841709Smlf (void) strcpy(info->dki_cname, 8851709Smlf ddi_get_name(ddi_get_parent(dkp->dk_dip))); 8861709Smlf 8871709Smlf /* Unit Information */ 8881709Smlf info->dki_unit = ddi_get_instance(dkp->dk_dip); 8891709Smlf devp = ddi_get_driver_private(dkp->dk_dip); 8901709Smlf info->dki_slave = (CMDEV_TARG(devp)<<3) | CMDEV_LUN(devp); 8911709Smlf (void) strcpy(info->dki_dname, ddi_driver_name(dkp->dk_dip)); 8921709Smlf info->dki_flags = DKI_FMTVOL; 8931709Smlf info->dki_partition = CMDKPART(dev); 8941709Smlf 8951709Smlf info->dki_maxtransfer = maxphys / DEV_BSIZE; 8961709Smlf info->dki_addr = 1; 8971709Smlf info->dki_space = 0; 8981709Smlf info->dki_prio = 0; 8991709Smlf info->dki_vec = 0; 9001709Smlf 9011709Smlf if (ddi_copyout(data, (void *)arg, sizeof (*info), flag)) 9021709Smlf return (EFAULT); 9031709Smlf else 9041709Smlf return (0); 9051709Smlf } 9061709Smlf 9071709Smlf case DKIOCSTATE: { 9081709Smlf int state; 9091709Smlf int rval; 9101709Smlf diskaddr_t p_lblksrt; 9111709Smlf diskaddr_t p_lblkcnt; 9121709Smlf 9131709Smlf if (ddi_copyin((void *)arg, &state, sizeof (int), flag)) 9141709Smlf return (EFAULT); 9151709Smlf 9161709Smlf /* dadk_check_media blocks until state changes */ 9171709Smlf if (rval = dadk_check_media(DKTP_DATA, &state)) 9181709Smlf return (rval); 9191709Smlf 9201709Smlf if (state == DKIO_INSERTED) { 9211709Smlf 9223525Sshidokht if (cmlb_validate(dkp->dk_cmlbhandle, 0, 0) != 0) 9231709Smlf return (ENXIO); 9241709Smlf 9251709Smlf if (cmlb_partinfo(dkp->dk_cmlbhandle, CMDKPART(dev), 9263525Sshidokht &p_lblkcnt, &p_lblksrt, NULL, NULL, 0)) 9271709Smlf return (ENXIO); 9281709Smlf 9291709Smlf if (p_lblkcnt <= 0) 9301709Smlf return (ENXIO); 9311709Smlf } 9321709Smlf 9331709Smlf if (ddi_copyout(&state, (caddr_t)arg, sizeof (int), flag)) 9341709Smlf return (EFAULT); 9351709Smlf 9361709Smlf return (0); 9371709Smlf } 9381709Smlf 9391709Smlf /* 9401709Smlf * is media removable? 9411709Smlf */ 9421709Smlf case DKIOCREMOVABLE: { 9431709Smlf int i; 9441709Smlf 9451709Smlf i = (DKTP_EXT->tg_rmb) ? 1 : 0; 9461709Smlf 9471709Smlf if (ddi_copyout(&i, (caddr_t)arg, sizeof (int), flag)) 9481709Smlf return (EFAULT); 9491709Smlf 9501709Smlf return (0); 9511709Smlf } 9521709Smlf 9531709Smlf case DKIOCADDBAD: 9541709Smlf /* 9551709Smlf * This is not an update mechanism to add bad blocks 9561709Smlf * to the bad block structures stored on disk. 9571709Smlf * 9581709Smlf * addbadsec(1M) will update the bad block data on disk 9591709Smlf * and use this ioctl to force the driver to re-initialize 9601709Smlf * the list of bad blocks in the driver. 9611709Smlf */ 9621709Smlf 9631709Smlf /* start BBH */ 9641709Smlf cmdk_bbh_reopen(dkp); 9651709Smlf return (0); 9661709Smlf 9671709Smlf case DKIOCG_PHYGEOM: 9681709Smlf case DKIOCG_VIRTGEOM: 9691709Smlf case DKIOCGGEOM: 9701709Smlf case DKIOCSGEOM: 9711709Smlf case DKIOCGAPART: 9721709Smlf case DKIOCSAPART: 9731709Smlf case DKIOCGVTOC: 9741709Smlf case DKIOCSVTOC: 9751709Smlf case DKIOCPARTINFO: 9761709Smlf case DKIOCGMBOOT: 9771709Smlf case DKIOCSMBOOT: 9781709Smlf case DKIOCGETEFI: 9791709Smlf case DKIOCSETEFI: 9801709Smlf case DKIOCPARTITION: 9811709Smlf { 9821709Smlf int rc; 9831709Smlf 9843525Sshidokht rc = cmlb_ioctl(dkp->dk_cmlbhandle, dev, cmd, arg, flag, 9853525Sshidokht credp, rvalp, 0); 9861709Smlf if (cmd == DKIOCSVTOC) 9871709Smlf cmdk_devid_setup(dkp); 9881709Smlf return (rc); 9891709Smlf } 9901709Smlf 9911709Smlf case DIOCTL_RWCMD: { 9921709Smlf struct dadkio_rwcmd *rwcmdp; 9931709Smlf int status; 9941709Smlf 9951709Smlf rwcmdp = kmem_alloc(sizeof (struct dadkio_rwcmd), KM_SLEEP); 9961709Smlf 9971709Smlf status = rwcmd_copyin(rwcmdp, (caddr_t)arg, flag); 9981709Smlf 9991709Smlf if (status == 0) { 10001709Smlf bzero(&(rwcmdp->status), sizeof (struct dadkio_status)); 10011709Smlf status = dadk_ioctl(DKTP_DATA, 10021709Smlf dev, 10031709Smlf cmd, 10041709Smlf (uintptr_t)rwcmdp, 10051709Smlf flag, 10061709Smlf credp, 10071709Smlf rvalp); 10081709Smlf } 10091709Smlf if (status == 0) 10101709Smlf status = rwcmd_copyout(rwcmdp, (caddr_t)arg, flag); 10111709Smlf 10121709Smlf kmem_free(rwcmdp, sizeof (struct dadkio_rwcmd)); 10131709Smlf return (status); 10141709Smlf } 10151709Smlf 10161709Smlf default: 10171709Smlf return (dadk_ioctl(DKTP_DATA, 10181709Smlf dev, 10191709Smlf cmd, 10201709Smlf arg, 10211709Smlf flag, 10221709Smlf credp, 10231709Smlf rvalp)); 10241709Smlf } 10251709Smlf } 10261709Smlf 10271709Smlf /*ARGSUSED1*/ 10281709Smlf static int 10291709Smlf cmdkclose(dev_t dev, int flag, int otyp, cred_t *credp) 10301709Smlf { 10311709Smlf int part; 10321709Smlf ulong_t partbit; 10331709Smlf int instance; 10341709Smlf struct cmdk *dkp; 10351709Smlf int lastclose = 1; 10361709Smlf int i; 10371709Smlf 10381709Smlf instance = CMDKUNIT(dev); 10391709Smlf if (!(dkp = ddi_get_soft_state(cmdk_state, instance)) || 10401709Smlf (otyp >= OTYPCNT)) 10411709Smlf return (ENXIO); 10421709Smlf 10431709Smlf mutex_enter(&dkp->dk_mutex); 10441709Smlf 10451709Smlf /* check if device has been opened */ 10466318Sedp ASSERT(cmdk_isopen(dkp, dev)); 10471709Smlf if (!(dkp->dk_flag & CMDK_OPEN)) { 10481709Smlf mutex_exit(&dkp->dk_mutex); 10491709Smlf return (ENXIO); 10501709Smlf } 10511709Smlf 10525295Srandyf while (dkp->dk_flag & CMDK_SUSPEND) { 10535295Srandyf cv_wait(&dkp->dk_suspend_cv, &dkp->dk_mutex); 10545295Srandyf } 10555295Srandyf 10561709Smlf part = CMDKPART(dev); 10571709Smlf partbit = 1 << part; 10581709Smlf 10591709Smlf /* account for close */ 10601709Smlf if (otyp == OTYP_LYR) { 10616318Sedp ASSERT(dkp->dk_open_lyr[part] > 0); 10621709Smlf if (dkp->dk_open_lyr[part]) 10631709Smlf dkp->dk_open_lyr[part]--; 10646318Sedp } else { 10656318Sedp ASSERT((dkp->dk_open_reg[otyp] & partbit) != 0); 10661709Smlf dkp->dk_open_reg[otyp] &= ~partbit; 10676318Sedp } 10681709Smlf dkp->dk_open_exl &= ~partbit; 10691709Smlf 10701709Smlf for (i = 0; i < CMDK_MAXPART; i++) 10711709Smlf if (dkp->dk_open_lyr[i] != 0) { 10721709Smlf lastclose = 0; 10731709Smlf break; 10741709Smlf } 10751709Smlf 10761709Smlf if (lastclose) 10771709Smlf for (i = 0; i < OTYPCNT; i++) 10781709Smlf if (dkp->dk_open_reg[i] != 0) { 10791709Smlf lastclose = 0; 10801709Smlf break; 10811709Smlf } 10821709Smlf 10831709Smlf mutex_exit(&dkp->dk_mutex); 10841709Smlf 10851709Smlf if (lastclose) 10863525Sshidokht cmlb_invalidate(dkp->dk_cmlbhandle, 0); 10871709Smlf 10881709Smlf return (DDI_SUCCESS); 10891709Smlf } 10901709Smlf 10911709Smlf /*ARGSUSED3*/ 10921709Smlf static int 10931709Smlf cmdkopen(dev_t *dev_p, int flag, int otyp, cred_t *credp) 10941709Smlf { 10951709Smlf dev_t dev = *dev_p; 10961709Smlf int part; 10971709Smlf ulong_t partbit; 10981709Smlf int instance; 10991709Smlf struct cmdk *dkp; 11001709Smlf diskaddr_t p_lblksrt; 11011709Smlf diskaddr_t p_lblkcnt; 11021709Smlf int i; 11031709Smlf int nodelay; 11041709Smlf 11051709Smlf instance = CMDKUNIT(dev); 11061709Smlf if (!(dkp = ddi_get_soft_state(cmdk_state, instance))) 11071709Smlf return (ENXIO); 11081709Smlf 11091709Smlf if (otyp >= OTYPCNT) 11101709Smlf return (EINVAL); 11111709Smlf 11125295Srandyf mutex_enter(&dkp->dk_mutex); 11135295Srandyf while (dkp->dk_flag & CMDK_SUSPEND) { 11145295Srandyf cv_wait(&dkp->dk_suspend_cv, &dkp->dk_mutex); 11155295Srandyf } 11165295Srandyf mutex_exit(&dkp->dk_mutex); 11175295Srandyf 11181709Smlf part = CMDKPART(dev); 11191709Smlf partbit = 1 << part; 11201709Smlf nodelay = (flag & (FNDELAY | FNONBLOCK)); 11211709Smlf 11221709Smlf mutex_enter(&dkp->dk_mutex); 11231709Smlf 11243525Sshidokht if (cmlb_validate(dkp->dk_cmlbhandle, 0, 0) != 0) { 11251709Smlf 11261709Smlf /* fail if not doing non block open */ 11271709Smlf if (!nodelay) { 11281709Smlf mutex_exit(&dkp->dk_mutex); 11291709Smlf return (ENXIO); 11301709Smlf } 11311709Smlf } else if (cmlb_partinfo(dkp->dk_cmlbhandle, part, &p_lblkcnt, 11323525Sshidokht &p_lblksrt, NULL, NULL, 0) == 0) { 11331709Smlf 11341709Smlf if (p_lblkcnt <= 0 && (!nodelay || otyp != OTYP_CHR)) { 11351709Smlf mutex_exit(&dkp->dk_mutex); 11361709Smlf return (ENXIO); 11371709Smlf } 11381709Smlf } else { 11391709Smlf /* fail if not doing non block open */ 11401709Smlf if (!nodelay) { 11411709Smlf mutex_exit(&dkp->dk_mutex); 11421709Smlf return (ENXIO); 11431709Smlf } 11441709Smlf } 11451709Smlf 11461709Smlf if ((DKTP_EXT->tg_rdonly) && (flag & FWRITE)) { 11471709Smlf mutex_exit(&dkp->dk_mutex); 11481709Smlf return (EROFS); 11491709Smlf } 11501709Smlf 11511709Smlf /* check for part already opend exclusively */ 11521709Smlf if (dkp->dk_open_exl & partbit) 11531709Smlf goto excl_open_fail; 11541709Smlf 11551709Smlf /* check if we can establish exclusive open */ 11561709Smlf if (flag & FEXCL) { 11571709Smlf if (dkp->dk_open_lyr[part]) 11581709Smlf goto excl_open_fail; 11591709Smlf for (i = 0; i < OTYPCNT; i++) { 11601709Smlf if (dkp->dk_open_reg[i] & partbit) 11611709Smlf goto excl_open_fail; 11621709Smlf } 11631709Smlf } 11641709Smlf 11651709Smlf /* open will succeed, account for open */ 11661709Smlf dkp->dk_flag |= CMDK_OPEN; 11671709Smlf if (otyp == OTYP_LYR) 11681709Smlf dkp->dk_open_lyr[part]++; 11691709Smlf else 11701709Smlf dkp->dk_open_reg[otyp] |= partbit; 11711709Smlf if (flag & FEXCL) 11721709Smlf dkp->dk_open_exl |= partbit; 11731709Smlf 11741709Smlf mutex_exit(&dkp->dk_mutex); 11751709Smlf return (DDI_SUCCESS); 11761709Smlf 11771709Smlf excl_open_fail: 11781709Smlf mutex_exit(&dkp->dk_mutex); 11791709Smlf return (EBUSY); 11801709Smlf } 11811709Smlf 11821709Smlf /* 11831709Smlf * read routine 11841709Smlf */ 11851709Smlf /*ARGSUSED2*/ 11861709Smlf static int 11871709Smlf cmdkread(dev_t dev, struct uio *uio, cred_t *credp) 11881709Smlf { 11891709Smlf return (cmdkrw(dev, uio, B_READ)); 11901709Smlf } 11911709Smlf 11921709Smlf /* 11931709Smlf * async read routine 11941709Smlf */ 11951709Smlf /*ARGSUSED2*/ 11961709Smlf static int 11971709Smlf cmdkaread(dev_t dev, struct aio_req *aio, cred_t *credp) 11981709Smlf { 11991709Smlf return (cmdkarw(dev, aio, B_READ)); 12001709Smlf } 12011709Smlf 12021709Smlf /* 12031709Smlf * write routine 12041709Smlf */ 12051709Smlf /*ARGSUSED2*/ 12061709Smlf static int 12071709Smlf cmdkwrite(dev_t dev, struct uio *uio, cred_t *credp) 12081709Smlf { 12091709Smlf return (cmdkrw(dev, uio, B_WRITE)); 12101709Smlf } 12111709Smlf 12121709Smlf /* 12131709Smlf * async write routine 12141709Smlf */ 12151709Smlf /*ARGSUSED2*/ 12161709Smlf static int 12171709Smlf cmdkawrite(dev_t dev, struct aio_req *aio, cred_t *credp) 12181709Smlf { 12191709Smlf return (cmdkarw(dev, aio, B_WRITE)); 12201709Smlf } 12211709Smlf 12221709Smlf static void 12231709Smlf cmdkmin(struct buf *bp) 12241709Smlf { 12251709Smlf if (bp->b_bcount > DK_MAXRECSIZE) 12261709Smlf bp->b_bcount = DK_MAXRECSIZE; 12271709Smlf } 12281709Smlf 12291709Smlf static int 12301709Smlf cmdkrw(dev_t dev, struct uio *uio, int flag) 12311709Smlf { 12325295Srandyf int instance; 12335295Srandyf struct cmdk *dkp; 12345295Srandyf 12355295Srandyf instance = CMDKUNIT(dev); 12365295Srandyf if (!(dkp = ddi_get_soft_state(cmdk_state, instance))) 12375295Srandyf return (ENXIO); 12385295Srandyf 12395295Srandyf mutex_enter(&dkp->dk_mutex); 12405295Srandyf while (dkp->dk_flag & CMDK_SUSPEND) { 12415295Srandyf cv_wait(&dkp->dk_suspend_cv, &dkp->dk_mutex); 12425295Srandyf } 12435295Srandyf mutex_exit(&dkp->dk_mutex); 12445295Srandyf 12451709Smlf return (physio(cmdkstrategy, (struct buf *)0, dev, flag, cmdkmin, uio)); 12461709Smlf } 12471709Smlf 12481709Smlf static int 12491709Smlf cmdkarw(dev_t dev, struct aio_req *aio, int flag) 12501709Smlf { 12515295Srandyf int instance; 12525295Srandyf struct cmdk *dkp; 12535295Srandyf 12545295Srandyf instance = CMDKUNIT(dev); 12555295Srandyf if (!(dkp = ddi_get_soft_state(cmdk_state, instance))) 12565295Srandyf return (ENXIO); 12575295Srandyf 12585295Srandyf mutex_enter(&dkp->dk_mutex); 12595295Srandyf while (dkp->dk_flag & CMDK_SUSPEND) { 12605295Srandyf cv_wait(&dkp->dk_suspend_cv, &dkp->dk_mutex); 12615295Srandyf } 12625295Srandyf mutex_exit(&dkp->dk_mutex); 12635295Srandyf 12641709Smlf return (aphysio(cmdkstrategy, anocancel, dev, flag, cmdkmin, aio)); 12651709Smlf } 12661709Smlf 12671709Smlf /* 12681709Smlf * strategy routine 12691709Smlf */ 12701709Smlf static int 12711709Smlf cmdkstrategy(struct buf *bp) 12721709Smlf { 12731709Smlf int instance; 12741709Smlf struct cmdk *dkp; 12751709Smlf long d_cnt; 12761709Smlf diskaddr_t p_lblksrt; 12771709Smlf diskaddr_t p_lblkcnt; 12781709Smlf 12791709Smlf instance = CMDKUNIT(bp->b_edev); 12801709Smlf if (cmdk_indump || !(dkp = ddi_get_soft_state(cmdk_state, instance)) || 12811709Smlf (dkblock(bp) < 0)) { 12821709Smlf bp->b_resid = bp->b_bcount; 12831709Smlf SETBPERR(bp, ENXIO); 12841709Smlf biodone(bp); 12851709Smlf return (0); 12861709Smlf } 12871709Smlf 12885295Srandyf mutex_enter(&dkp->dk_mutex); 12896318Sedp ASSERT(cmdk_isopen(dkp, bp->b_edev)); 12905295Srandyf while (dkp->dk_flag & CMDK_SUSPEND) { 12915295Srandyf cv_wait(&dkp->dk_suspend_cv, &dkp->dk_mutex); 12925295Srandyf } 12935295Srandyf mutex_exit(&dkp->dk_mutex); 12945295Srandyf 12951709Smlf bp->b_flags &= ~(B_DONE|B_ERROR); 12961709Smlf bp->b_resid = 0; 12971709Smlf bp->av_back = NULL; 12981709Smlf 12991709Smlf /* 13001709Smlf * only re-read the vtoc if necessary (force == FALSE) 13011709Smlf */ 13023525Sshidokht if (cmlb_partinfo(dkp->dk_cmlbhandle, CMDKPART(bp->b_edev), 13033525Sshidokht &p_lblkcnt, &p_lblksrt, NULL, NULL, 0)) { 13041709Smlf SETBPERR(bp, ENXIO); 13051709Smlf } 13061709Smlf 13071709Smlf if ((bp->b_bcount & (NBPSCTR-1)) || (dkblock(bp) > p_lblkcnt)) 13081709Smlf SETBPERR(bp, ENXIO); 13091709Smlf 13101709Smlf if ((bp->b_flags & B_ERROR) || (dkblock(bp) == p_lblkcnt)) { 13111709Smlf bp->b_resid = bp->b_bcount; 13121709Smlf biodone(bp); 13131709Smlf return (0); 13141709Smlf } 13151709Smlf 13161709Smlf d_cnt = bp->b_bcount >> SCTRSHFT; 13171709Smlf if ((dkblock(bp) + d_cnt) > p_lblkcnt) { 13181709Smlf bp->b_resid = ((dkblock(bp) + d_cnt) - p_lblkcnt) << SCTRSHFT; 13191709Smlf bp->b_bcount -= bp->b_resid; 13201709Smlf } 13211709Smlf 13221709Smlf SET_BP_SEC(bp, ((ulong_t)(p_lblksrt + dkblock(bp)))); 13231709Smlf if (dadk_strategy(DKTP_DATA, bp) != DDI_SUCCESS) { 13241709Smlf bp->b_resid += bp->b_bcount; 13251709Smlf biodone(bp); 13261709Smlf } 13271709Smlf return (0); 13281709Smlf } 13291709Smlf 13301709Smlf static int 13311709Smlf cmdk_create_obj(dev_info_t *dip, struct cmdk *dkp) 13321709Smlf { 13331709Smlf struct scsi_device *devp; 13341709Smlf opaque_t queobjp = NULL; 13351709Smlf opaque_t flcobjp = NULL; 13361709Smlf char que_keyvalp[64]; 13371709Smlf int que_keylen; 13381709Smlf char flc_keyvalp[64]; 13391709Smlf int flc_keylen; 13401709Smlf 13411709Smlf ASSERT(mutex_owned(&dkp->dk_mutex)); 13421709Smlf 13431709Smlf /* Create linkage to queueing routines based on property */ 13441709Smlf que_keylen = sizeof (que_keyvalp); 13451709Smlf if (ddi_prop_op(DDI_DEV_T_NONE, dip, PROP_LEN_AND_VAL_BUF, 13461709Smlf DDI_PROP_CANSLEEP, "queue", que_keyvalp, &que_keylen) != 13471709Smlf DDI_PROP_SUCCESS) { 13481709Smlf cmn_err(CE_WARN, "cmdk_create_obj: queue property undefined"); 13491709Smlf return (DDI_FAILURE); 13501709Smlf } 13511709Smlf que_keyvalp[que_keylen] = (char)0; 13521709Smlf 13531709Smlf if (strcmp(que_keyvalp, "qfifo") == 0) { 13541709Smlf queobjp = (opaque_t)qfifo_create(); 13551709Smlf } else if (strcmp(que_keyvalp, "qsort") == 0) { 13561709Smlf queobjp = (opaque_t)qsort_create(); 13571709Smlf } else { 13581709Smlf return (DDI_FAILURE); 13591709Smlf } 13601709Smlf 13611709Smlf /* Create linkage to dequeueing routines based on property */ 13621709Smlf flc_keylen = sizeof (flc_keyvalp); 13631709Smlf if (ddi_prop_op(DDI_DEV_T_NONE, dip, PROP_LEN_AND_VAL_BUF, 13641709Smlf DDI_PROP_CANSLEEP, "flow_control", flc_keyvalp, &flc_keylen) != 13651709Smlf DDI_PROP_SUCCESS) { 13661709Smlf cmn_err(CE_WARN, 13671709Smlf "cmdk_create_obj: flow-control property undefined"); 13681709Smlf return (DDI_FAILURE); 13691709Smlf } 13701709Smlf 13711709Smlf flc_keyvalp[flc_keylen] = (char)0; 13721709Smlf 13731709Smlf if (strcmp(flc_keyvalp, "dsngl") == 0) { 13741709Smlf flcobjp = (opaque_t)dsngl_create(); 13751709Smlf } else if (strcmp(flc_keyvalp, "dmult") == 0) { 13761709Smlf flcobjp = (opaque_t)dmult_create(); 13771709Smlf } else { 13781709Smlf return (DDI_FAILURE); 13791709Smlf } 13801709Smlf 13811709Smlf /* populate bbh_obj object stored in dkp */ 13821709Smlf dkp->dk_bbh_obj.bbh_data = dkp; 13831709Smlf dkp->dk_bbh_obj.bbh_ops = &cmdk_bbh_ops; 13841709Smlf 13851709Smlf /* create linkage to dadk */ 13861709Smlf dkp->dk_tgobjp = (opaque_t)dadk_create(); 13871709Smlf 13881709Smlf devp = ddi_get_driver_private(dip); 13891709Smlf (void) dadk_init(DKTP_DATA, devp, flcobjp, queobjp, &dkp->dk_bbh_obj, 13901709Smlf NULL); 13911709Smlf 13921709Smlf return (DDI_SUCCESS); 13931709Smlf } 13941709Smlf 13951709Smlf static void 13961709Smlf cmdk_destroy_obj(dev_info_t *dip, struct cmdk *dkp) 13971709Smlf { 13981709Smlf char que_keyvalp[64]; 13991709Smlf int que_keylen; 14001709Smlf char flc_keyvalp[64]; 14011709Smlf int flc_keylen; 14021709Smlf 14031709Smlf ASSERT(mutex_owned(&dkp->dk_mutex)); 14041709Smlf 14051709Smlf (void) dadk_free((dkp->dk_tgobjp)); 14061709Smlf dkp->dk_tgobjp = NULL; 14071709Smlf 14081709Smlf que_keylen = sizeof (que_keyvalp); 14091709Smlf if (ddi_prop_op(DDI_DEV_T_NONE, dip, PROP_LEN_AND_VAL_BUF, 14101709Smlf DDI_PROP_CANSLEEP, "queue", que_keyvalp, &que_keylen) != 14111709Smlf DDI_PROP_SUCCESS) { 14121709Smlf cmn_err(CE_WARN, "cmdk_destroy_obj: queue property undefined"); 14131709Smlf return; 14141709Smlf } 14151709Smlf que_keyvalp[que_keylen] = (char)0; 14161709Smlf 14171709Smlf flc_keylen = sizeof (flc_keyvalp); 14181709Smlf if (ddi_prop_op(DDI_DEV_T_NONE, dip, PROP_LEN_AND_VAL_BUF, 14191709Smlf DDI_PROP_CANSLEEP, "flow_control", flc_keyvalp, &flc_keylen) != 14201709Smlf DDI_PROP_SUCCESS) { 14211709Smlf cmn_err(CE_WARN, 14221709Smlf "cmdk_destroy_obj: flow-control property undefined"); 14231709Smlf return; 14241709Smlf } 14251709Smlf flc_keyvalp[flc_keylen] = (char)0; 14261709Smlf } 14273525Sshidokht /*ARGSUSED5*/ 14281709Smlf static int 14293525Sshidokht cmdk_lb_rdwr(dev_info_t *dip, uchar_t cmd, void *bufaddr, 14303525Sshidokht diskaddr_t start, size_t count, void *tg_cookie) 14311709Smlf { 14321709Smlf struct cmdk *dkp; 14331709Smlf opaque_t handle; 14341709Smlf int rc = 0; 14351709Smlf char *bufa; 14361709Smlf 14371709Smlf dkp = ddi_get_soft_state(cmdk_state, ddi_get_instance(dip)); 14381709Smlf if (dkp == NULL) 14391709Smlf return (ENXIO); 14401709Smlf 14411709Smlf if (cmd != TG_READ && cmd != TG_WRITE) 14421709Smlf return (EINVAL); 14431709Smlf 14441709Smlf /* count must be multiple of 512 */ 14451709Smlf count = (count + NBPSCTR - 1) & -NBPSCTR; 14461709Smlf handle = dadk_iob_alloc(DKTP_DATA, start, count, KM_SLEEP); 14471709Smlf if (!handle) 14481709Smlf return (ENOMEM); 14491709Smlf 14501709Smlf if (cmd == TG_READ) { 14511709Smlf bufa = dadk_iob_xfer(DKTP_DATA, handle, B_READ); 14521709Smlf if (!bufa) 14531709Smlf rc = EIO; 14541709Smlf else 14551709Smlf bcopy(bufa, bufaddr, count); 14561709Smlf } else { 14571709Smlf bufa = dadk_iob_htoc(DKTP_DATA, handle); 14581709Smlf bcopy(bufaddr, bufa, count); 14591709Smlf bufa = dadk_iob_xfer(DKTP_DATA, handle, B_WRITE); 14601709Smlf if (!bufa) 14611709Smlf rc = EIO; 14621709Smlf } 14631709Smlf (void) dadk_iob_free(DKTP_DATA, handle); 14641709Smlf 14651709Smlf return (rc); 14661709Smlf } 14671709Smlf 14683525Sshidokht /*ARGSUSED3*/ 14691709Smlf static int 14703525Sshidokht cmdk_lb_getinfo(dev_info_t *dip, int cmd, void *arg, void *tg_cookie) 14711709Smlf { 14723525Sshidokht 14731709Smlf struct cmdk *dkp; 14741709Smlf struct tgdk_geom phyg; 14751709Smlf 14761709Smlf 14771709Smlf dkp = ddi_get_soft_state(cmdk_state, ddi_get_instance(dip)); 14781709Smlf if (dkp == NULL) 14791709Smlf return (ENXIO); 14801709Smlf 14813525Sshidokht switch (cmd) { 14823525Sshidokht case TG_GETPHYGEOM: { 14833525Sshidokht cmlb_geom_t *phygeomp = (cmlb_geom_t *)arg; 14843525Sshidokht 14853525Sshidokht /* dadk_getphygeom always returns success */ 14863525Sshidokht (void) dadk_getphygeom(DKTP_DATA, &phyg); 14873525Sshidokht 14883525Sshidokht phygeomp->g_capacity = phyg.g_cap; 14893525Sshidokht phygeomp->g_nsect = phyg.g_sec; 14903525Sshidokht phygeomp->g_nhead = phyg.g_head; 14913525Sshidokht phygeomp->g_acyl = phyg.g_acyl; 14923525Sshidokht phygeomp->g_ncyl = phyg.g_cyl; 14933525Sshidokht phygeomp->g_secsize = phyg.g_secsiz; 14943525Sshidokht phygeomp->g_intrlv = 1; 14953525Sshidokht phygeomp->g_rpm = 3600; 14961709Smlf 14973525Sshidokht return (0); 14983525Sshidokht } 14993525Sshidokht 15003525Sshidokht case TG_GETVIRTGEOM: { 15013525Sshidokht cmlb_geom_t *virtgeomp = (cmlb_geom_t *)arg; 15023525Sshidokht diskaddr_t capacity; 15033525Sshidokht 15043525Sshidokht (void) dadk_getgeom(DKTP_DATA, &phyg); 15053525Sshidokht capacity = phyg.g_cap; 15063525Sshidokht 15073525Sshidokht /* 15083525Sshidokht * If the controller returned us something that doesn't 15093525Sshidokht * really fit into an Int 13/function 8 geometry 15103525Sshidokht * result, just fail the ioctl. See PSARC 1998/313. 15113525Sshidokht */ 15123525Sshidokht if (capacity < 0 || capacity >= 63 * 254 * 1024) 15133525Sshidokht return (EINVAL); 15141709Smlf 15153525Sshidokht virtgeomp->g_capacity = capacity; 15163525Sshidokht virtgeomp->g_nsect = 63; 15173525Sshidokht virtgeomp->g_nhead = 254; 15183525Sshidokht virtgeomp->g_ncyl = capacity / (63 * 254); 15193525Sshidokht virtgeomp->g_acyl = 0; 15203525Sshidokht virtgeomp->g_secsize = 512; 15213525Sshidokht virtgeomp->g_intrlv = 1; 15223525Sshidokht virtgeomp->g_rpm = 3600; 15233525Sshidokht 15243525Sshidokht return (0); 15253525Sshidokht } 15263525Sshidokht 15273525Sshidokht case TG_GETCAPACITY: 15283525Sshidokht case TG_GETBLOCKSIZE: 15293525Sshidokht { 15301709Smlf 15313525Sshidokht /* dadk_getphygeom always returns success */ 15323525Sshidokht (void) dadk_getphygeom(DKTP_DATA, &phyg); 15333525Sshidokht if (cmd == TG_GETCAPACITY) 15343525Sshidokht *(diskaddr_t *)arg = phyg.g_cap; 15353525Sshidokht else 15363525Sshidokht *(uint32_t *)arg = (uint32_t)phyg.g_secsiz; 15373525Sshidokht 15383525Sshidokht return (0); 15393525Sshidokht } 15403525Sshidokht 15413525Sshidokht case TG_GETATTR: { 15423525Sshidokht tg_attribute_t *tgattribute = (tg_attribute_t *)arg; 15433525Sshidokht if ((DKTP_EXT->tg_rdonly)) 15443525Sshidokht tgattribute->media_is_writable = FALSE; 15453525Sshidokht else 15463525Sshidokht tgattribute->media_is_writable = TRUE; 15473525Sshidokht 15483525Sshidokht return (0); 15493525Sshidokht } 15503525Sshidokht 15513525Sshidokht default: 15523525Sshidokht return (ENOTTY); 15533525Sshidokht } 15541709Smlf } 15551709Smlf 15561709Smlf 15571709Smlf 15581709Smlf 15591709Smlf 15601709Smlf /* 15611709Smlf * Create and register the devid. 15621709Smlf * There are 4 different ways we can get a device id: 15631709Smlf * 1. Already have one - nothing to do 15641709Smlf * 2. Build one from the drive's model and serial numbers 15651709Smlf * 3. Read one from the disk (first sector of last track) 15661709Smlf * 4. Fabricate one and write it on the disk. 15671709Smlf * If any of these succeeds, register the deviceid 15681709Smlf */ 15691709Smlf static void 15701709Smlf cmdk_devid_setup(struct cmdk *dkp) 15711709Smlf { 15721709Smlf int rc; 15731709Smlf 15741709Smlf /* Try options until one succeeds, or all have failed */ 15751709Smlf 15761709Smlf /* 1. All done if already registered */ 15771709Smlf if (dkp->dk_devid != NULL) 15781709Smlf return; 15791709Smlf 15801709Smlf /* 2. Build a devid from the model and serial number */ 15811709Smlf rc = cmdk_devid_modser(dkp); 15821709Smlf if (rc != DDI_SUCCESS) { 15831709Smlf /* 3. Read devid from the disk, if present */ 15841709Smlf rc = cmdk_devid_read(dkp); 15851709Smlf 15861709Smlf /* 4. otherwise make one up and write it on the disk */ 15871709Smlf if (rc != DDI_SUCCESS) 15881709Smlf rc = cmdk_devid_fabricate(dkp); 15891709Smlf } 15901709Smlf 15911709Smlf /* If we managed to get a devid any of the above ways, register it */ 15921709Smlf if (rc == DDI_SUCCESS) 15931709Smlf (void) ddi_devid_register(dkp->dk_dip, dkp->dk_devid); 15941709Smlf 15951709Smlf } 15961709Smlf 15971709Smlf /* 15981709Smlf * Build a devid from the model and serial number 15991709Smlf * Return DDI_SUCCESS or DDI_FAILURE. 16001709Smlf */ 16011709Smlf static int 16021709Smlf cmdk_devid_modser(struct cmdk *dkp) 16031709Smlf { 16041709Smlf int rc = DDI_FAILURE; 16051709Smlf char *hwid; 16061709Smlf int modlen; 16071709Smlf int serlen; 16081709Smlf 16091709Smlf /* 16101709Smlf * device ID is a concatenation of model number, '=', serial number. 16111709Smlf */ 16121709Smlf hwid = kmem_alloc(CMDK_HWIDLEN, KM_SLEEP); 16131709Smlf modlen = cmdk_get_modser(dkp, DIOCTL_GETMODEL, hwid, CMDK_HWIDLEN); 16141709Smlf if (modlen == 0) { 16151709Smlf rc = DDI_FAILURE; 16161709Smlf goto err; 16171709Smlf } 16181709Smlf hwid[modlen++] = '='; 16191709Smlf serlen = cmdk_get_modser(dkp, DIOCTL_GETSERIAL, 16201709Smlf hwid + modlen, CMDK_HWIDLEN - modlen); 16211709Smlf if (serlen == 0) { 16221709Smlf rc = DDI_FAILURE; 16231709Smlf goto err; 16241709Smlf } 16251709Smlf hwid[modlen + serlen] = 0; 16261709Smlf 16271709Smlf /* Initialize the device ID, trailing NULL not included */ 16281709Smlf rc = ddi_devid_init(dkp->dk_dip, DEVID_ATA_SERIAL, modlen + serlen, 16291709Smlf hwid, (ddi_devid_t *)&dkp->dk_devid); 16301709Smlf if (rc != DDI_SUCCESS) { 16311709Smlf rc = DDI_FAILURE; 16321709Smlf goto err; 16331709Smlf } 16341709Smlf 16351709Smlf rc = DDI_SUCCESS; 16361709Smlf 16371709Smlf err: 16381709Smlf kmem_free(hwid, CMDK_HWIDLEN); 16391709Smlf return (rc); 16401709Smlf } 16411709Smlf 16421709Smlf static int 16431709Smlf cmdk_get_modser(struct cmdk *dkp, int ioccmd, char *buf, int len) 16441709Smlf { 16451709Smlf dadk_ioc_string_t strarg; 16461709Smlf int rval; 16471709Smlf char *s; 16481709Smlf char ch; 16491709Smlf boolean_t ret; 16501709Smlf int i; 16511709Smlf int tb; 16521709Smlf 16531709Smlf strarg.is_buf = buf; 16541709Smlf strarg.is_size = len; 16551709Smlf if (dadk_ioctl(DKTP_DATA, 16561709Smlf dkp->dk_dev, 16571709Smlf ioccmd, 16581709Smlf (uintptr_t)&strarg, 16591709Smlf FNATIVE | FKIOCTL, 16601709Smlf NULL, 16611709Smlf &rval) != 0) 16621709Smlf return (0); 16631709Smlf 16641709Smlf /* 16651709Smlf * valid model/serial string must contain a non-zero non-space 16661709Smlf * trim trailing spaces/NULL 16671709Smlf */ 16681709Smlf ret = B_FALSE; 16691709Smlf s = buf; 16701709Smlf for (i = 0; i < strarg.is_size; i++) { 16711709Smlf ch = *s++; 16721709Smlf if (ch != ' ' && ch != '\0') 16731709Smlf tb = i + 1; 16741709Smlf if (ch != ' ' && ch != '\0' && ch != '0') 16751709Smlf ret = B_TRUE; 16761709Smlf } 16771709Smlf 16781709Smlf if (ret == B_FALSE) 16791709Smlf return (0); 16801709Smlf 16811709Smlf return (tb); 16821709Smlf } 16831709Smlf 16841709Smlf /* 16851709Smlf * Read a devid from on the first block of the last track of 16861709Smlf * the last cylinder. Make sure what we read is a valid devid. 16871709Smlf * Return DDI_SUCCESS or DDI_FAILURE. 16881709Smlf */ 16891709Smlf static int 16901709Smlf cmdk_devid_read(struct cmdk *dkp) 16911709Smlf { 16921709Smlf diskaddr_t blk; 16931709Smlf struct dk_devid *dkdevidp; 16941709Smlf uint_t *ip; 16951709Smlf int chksum; 16961709Smlf int i, sz; 16971709Smlf tgdk_iob_handle handle; 16981709Smlf int rc = DDI_FAILURE; 16991709Smlf 17003525Sshidokht if (cmlb_get_devid_block(dkp->dk_cmlbhandle, &blk, 0)) 17011709Smlf goto err; 17021709Smlf 17031709Smlf /* read the devid */ 17041709Smlf handle = dadk_iob_alloc(DKTP_DATA, blk, NBPSCTR, KM_SLEEP); 17051709Smlf if (handle == NULL) 17061709Smlf goto err; 17071709Smlf 17081709Smlf dkdevidp = (struct dk_devid *)dadk_iob_xfer(DKTP_DATA, handle, B_READ); 17091709Smlf if (dkdevidp == NULL) 17101709Smlf goto err; 17111709Smlf 17121709Smlf /* Validate the revision */ 17131709Smlf if ((dkdevidp->dkd_rev_hi != DK_DEVID_REV_MSB) || 17141709Smlf (dkdevidp->dkd_rev_lo != DK_DEVID_REV_LSB)) 17151709Smlf goto err; 17161709Smlf 17171709Smlf /* Calculate the checksum */ 17181709Smlf chksum = 0; 17191709Smlf ip = (uint_t *)dkdevidp; 17201709Smlf for (i = 0; i < ((NBPSCTR - sizeof (int))/sizeof (int)); i++) 17211709Smlf chksum ^= ip[i]; 17221709Smlf if (DKD_GETCHKSUM(dkdevidp) != chksum) 17231709Smlf goto err; 17241709Smlf 17251709Smlf /* Validate the device id */ 17261709Smlf if (ddi_devid_valid((ddi_devid_t)dkdevidp->dkd_devid) != DDI_SUCCESS) 17271709Smlf goto err; 17281709Smlf 17291709Smlf /* keep a copy of the device id */ 17301709Smlf sz = ddi_devid_sizeof((ddi_devid_t)dkdevidp->dkd_devid); 17311709Smlf dkp->dk_devid = kmem_alloc(sz, KM_SLEEP); 17321709Smlf bcopy(dkdevidp->dkd_devid, dkp->dk_devid, sz); 17331709Smlf 17341709Smlf rc = DDI_SUCCESS; 17351709Smlf 17361709Smlf err: 17371709Smlf if (handle != NULL) 17381709Smlf (void) dadk_iob_free(DKTP_DATA, handle); 17391709Smlf return (rc); 17401709Smlf } 17411709Smlf 17421709Smlf /* 17431709Smlf * Create a devid and write it on the first block of the last track of 17441709Smlf * the last cylinder. 17451709Smlf * Return DDI_SUCCESS or DDI_FAILURE. 17461709Smlf */ 17471709Smlf static int 17481709Smlf cmdk_devid_fabricate(struct cmdk *dkp) 17491709Smlf { 17501709Smlf ddi_devid_t devid = NULL; /* devid made by ddi_devid_init */ 17511709Smlf struct dk_devid *dkdevidp; /* devid struct stored on disk */ 17521709Smlf diskaddr_t blk; 17531709Smlf tgdk_iob_handle handle = NULL; 17541709Smlf uint_t *ip, chksum; 17551709Smlf int i; 17561709Smlf int rc; 17571709Smlf 17581709Smlf rc = ddi_devid_init(dkp->dk_dip, DEVID_FAB, 0, NULL, &devid); 17591709Smlf if (rc != DDI_SUCCESS) 17601709Smlf goto err; 17611709Smlf 17623525Sshidokht if (cmlb_get_devid_block(dkp->dk_cmlbhandle, &blk, 0)) { 17631709Smlf /* no device id block address */ 17641709Smlf return (DDI_FAILURE); 17651709Smlf } 17661709Smlf 17671709Smlf handle = dadk_iob_alloc(DKTP_DATA, blk, NBPSCTR, KM_SLEEP); 17681709Smlf if (!handle) 17691709Smlf goto err; 17701709Smlf 17711709Smlf /* Locate the buffer */ 17721709Smlf dkdevidp = (struct dk_devid *)dadk_iob_htoc(DKTP_DATA, handle); 17731709Smlf 17741709Smlf /* Fill in the revision */ 17751709Smlf bzero(dkdevidp, NBPSCTR); 17761709Smlf dkdevidp->dkd_rev_hi = DK_DEVID_REV_MSB; 17771709Smlf dkdevidp->dkd_rev_lo = DK_DEVID_REV_LSB; 17781709Smlf 17791709Smlf /* Copy in the device id */ 17801709Smlf i = ddi_devid_sizeof(devid); 17811709Smlf if (i > DK_DEVID_SIZE) 17821709Smlf goto err; 17831709Smlf bcopy(devid, dkdevidp->dkd_devid, i); 17841709Smlf 17851709Smlf /* Calculate the chksum */ 17861709Smlf chksum = 0; 17871709Smlf ip = (uint_t *)dkdevidp; 17881709Smlf for (i = 0; i < ((NBPSCTR - sizeof (int))/sizeof (int)); i++) 17891709Smlf chksum ^= ip[i]; 17901709Smlf 17911709Smlf /* Fill in the checksum */ 17921709Smlf DKD_FORMCHKSUM(chksum, dkdevidp); 17931709Smlf 17941709Smlf /* write the devid */ 17951709Smlf (void) dadk_iob_xfer(DKTP_DATA, handle, B_WRITE); 17961709Smlf 17971709Smlf dkp->dk_devid = devid; 17981709Smlf 17991709Smlf rc = DDI_SUCCESS; 18001709Smlf 18011709Smlf err: 18021709Smlf if (handle != NULL) 18031709Smlf (void) dadk_iob_free(DKTP_DATA, handle); 18041709Smlf 18051709Smlf if (rc != DDI_SUCCESS && devid != NULL) 18061709Smlf ddi_devid_free(devid); 18071709Smlf 18081709Smlf return (rc); 18091709Smlf } 18101709Smlf 18111709Smlf static void 18121709Smlf cmdk_bbh_free_alts(struct cmdk *dkp) 18131709Smlf { 18141709Smlf if (dkp->dk_alts_hdl) { 18151709Smlf (void) dadk_iob_free(DKTP_DATA, dkp->dk_alts_hdl); 18161709Smlf kmem_free(dkp->dk_slc_cnt, 18171709Smlf NDKMAP * (sizeof (uint32_t) + sizeof (struct alts_ent *))); 18181709Smlf dkp->dk_alts_hdl = NULL; 18191709Smlf } 18201709Smlf } 18211709Smlf 18221709Smlf static void 18231709Smlf cmdk_bbh_reopen(struct cmdk *dkp) 18241709Smlf { 18251709Smlf tgdk_iob_handle handle = NULL; 18261709Smlf diskaddr_t slcb, slcn, slce; 18271709Smlf struct alts_parttbl *ap; 18281709Smlf struct alts_ent *enttblp; 18291709Smlf uint32_t altused; 18301709Smlf uint32_t altbase; 18311709Smlf uint32_t altlast; 18321709Smlf int alts; 18331709Smlf uint16_t vtoctag; 18341709Smlf int i, j; 18351709Smlf 18361709Smlf /* find slice with V_ALTSCTR tag */ 18371709Smlf for (alts = 0; alts < NDKMAP; alts++) { 18381709Smlf if (cmlb_partinfo( 18391709Smlf dkp->dk_cmlbhandle, 18401709Smlf alts, 18411709Smlf &slcn, 18421709Smlf &slcb, 18431709Smlf NULL, 18443525Sshidokht &vtoctag, 18453525Sshidokht 0)) { 18461709Smlf goto empty; /* no partition table exists */ 18471709Smlf } 18481709Smlf 18491709Smlf if (vtoctag == V_ALTSCTR && slcn > 1) 18501709Smlf break; 18511709Smlf } 18521709Smlf if (alts >= NDKMAP) { 18531709Smlf goto empty; /* no V_ALTSCTR slice defined */ 18541709Smlf } 18551709Smlf 18561709Smlf /* read in ALTS label block */ 18571709Smlf handle = dadk_iob_alloc(DKTP_DATA, slcb, NBPSCTR, KM_SLEEP); 18581709Smlf if (!handle) { 18591709Smlf goto empty; 18601709Smlf } 18611709Smlf 18621709Smlf ap = (struct alts_parttbl *)dadk_iob_xfer(DKTP_DATA, handle, B_READ); 18631709Smlf if (!ap || (ap->alts_sanity != ALTS_SANITY)) { 18641709Smlf goto empty; 18651709Smlf } 18661709Smlf 18671709Smlf altused = ap->alts_ent_used; /* number of BB entries */ 18681709Smlf altbase = ap->alts_ent_base; /* blk offset from begin slice */ 18691709Smlf altlast = ap->alts_ent_end; /* blk offset to last block */ 18701709Smlf /* ((altused * sizeof (struct alts_ent) + NBPSCTR - 1) & ~NBPSCTR) */ 18711709Smlf 18721709Smlf if (altused == 0 || 18731709Smlf altbase < 1 || 18741709Smlf altbase > altlast || 18751709Smlf altlast >= slcn) { 18761709Smlf goto empty; 18771709Smlf } 18781709Smlf (void) dadk_iob_free(DKTP_DATA, handle); 18791709Smlf 18801709Smlf /* read in ALTS remapping table */ 18811709Smlf handle = dadk_iob_alloc(DKTP_DATA, 18821709Smlf slcb + altbase, 18831709Smlf (altlast - altbase + 1) << SCTRSHFT, KM_SLEEP); 18841709Smlf if (!handle) { 18851709Smlf goto empty; 18861709Smlf } 18871709Smlf 18881709Smlf enttblp = (struct alts_ent *)dadk_iob_xfer(DKTP_DATA, handle, B_READ); 18891709Smlf if (!enttblp) { 18901709Smlf goto empty; 18911709Smlf } 18921709Smlf 18931709Smlf rw_enter(&dkp->dk_bbh_mutex, RW_WRITER); 18941709Smlf 18951709Smlf /* allocate space for dk_slc_cnt and dk_slc_ent tables */ 18961709Smlf if (dkp->dk_slc_cnt == NULL) { 18971709Smlf dkp->dk_slc_cnt = kmem_alloc(NDKMAP * 18981709Smlf (sizeof (long) + sizeof (struct alts_ent *)), KM_SLEEP); 18991709Smlf } 19001709Smlf dkp->dk_slc_ent = (struct alts_ent **)(dkp->dk_slc_cnt + NDKMAP); 19011709Smlf 19021709Smlf /* free previous BB table (if any) */ 19031709Smlf if (dkp->dk_alts_hdl) { 19041709Smlf (void) dadk_iob_free(DKTP_DATA, dkp->dk_alts_hdl); 19051709Smlf dkp->dk_alts_hdl = NULL; 19061709Smlf dkp->dk_altused = 0; 19071709Smlf } 19081709Smlf 19091709Smlf /* save linkage to new BB table */ 19101709Smlf dkp->dk_alts_hdl = handle; 19111709Smlf dkp->dk_altused = altused; 19121709Smlf 19131709Smlf /* 19141709Smlf * build indexes to BB table by slice 19151709Smlf * effectively we have 19161709Smlf * struct alts_ent *enttblp[altused]; 19171709Smlf * 19181709Smlf * uint32_t dk_slc_cnt[NDKMAP]; 19191709Smlf * struct alts_ent *dk_slc_ent[NDKMAP]; 19201709Smlf */ 19211709Smlf for (i = 0; i < NDKMAP; i++) { 19221709Smlf if (cmlb_partinfo( 19231709Smlf dkp->dk_cmlbhandle, 19241709Smlf i, 19251709Smlf &slcn, 19261709Smlf &slcb, 19271709Smlf NULL, 19283525Sshidokht NULL, 19293525Sshidokht 0)) { 19301709Smlf goto empty1; 19311709Smlf } 19321709Smlf 19331709Smlf dkp->dk_slc_cnt[i] = 0; 19341709Smlf if (slcn == 0) 19351709Smlf continue; /* slice is not allocated */ 19361709Smlf 19371709Smlf /* last block in slice */ 19381709Smlf slce = slcb + slcn - 1; 19391709Smlf 19401709Smlf /* find first remap entry in after beginnning of slice */ 19411709Smlf for (j = 0; j < altused; j++) { 19421709Smlf if (enttblp[j].bad_start + enttblp[j].bad_end >= slcb) 19431709Smlf break; 19441709Smlf } 19451709Smlf dkp->dk_slc_ent[i] = enttblp + j; 19461709Smlf 19471709Smlf /* count remap entrys until end of slice */ 19481709Smlf for (; j < altused && enttblp[j].bad_start <= slce; j++) { 19491709Smlf dkp->dk_slc_cnt[i] += 1; 19501709Smlf } 19511709Smlf } 19521709Smlf 19531709Smlf rw_exit(&dkp->dk_bbh_mutex); 19541709Smlf return; 19551709Smlf 19561709Smlf empty: 19571709Smlf rw_enter(&dkp->dk_bbh_mutex, RW_WRITER); 19581709Smlf empty1: 19591709Smlf if (handle && handle != dkp->dk_alts_hdl) 19601709Smlf (void) dadk_iob_free(DKTP_DATA, handle); 19611709Smlf 19621709Smlf if (dkp->dk_alts_hdl) { 19631709Smlf (void) dadk_iob_free(DKTP_DATA, dkp->dk_alts_hdl); 19641709Smlf dkp->dk_alts_hdl = NULL; 19651709Smlf } 19661709Smlf 19671709Smlf rw_exit(&dkp->dk_bbh_mutex); 19681709Smlf } 19691709Smlf 19701709Smlf /*ARGSUSED*/ 19711709Smlf static bbh_cookie_t 19721709Smlf cmdk_bbh_htoc(opaque_t bbh_data, opaque_t handle) 19731709Smlf { 19741709Smlf struct bbh_handle *hp; 19751709Smlf bbh_cookie_t ckp; 19761709Smlf 19771709Smlf hp = (struct bbh_handle *)handle; 19781709Smlf ckp = hp->h_cktab + hp->h_idx; 19791709Smlf hp->h_idx++; 19801709Smlf return (ckp); 19811709Smlf } 19821709Smlf 19831709Smlf /*ARGSUSED*/ 19841709Smlf static void 19851709Smlf cmdk_bbh_freehandle(opaque_t bbh_data, opaque_t handle) 19861709Smlf { 19871709Smlf struct bbh_handle *hp; 19881709Smlf 19891709Smlf hp = (struct bbh_handle *)handle; 19901709Smlf kmem_free(handle, (sizeof (struct bbh_handle) + 19911709Smlf (hp->h_totck * (sizeof (struct bbh_cookie))))); 19921709Smlf } 19931709Smlf 19941709Smlf 19951709Smlf /* 19961709Smlf * cmdk_bbh_gethandle remaps the bad sectors to alternates. 19971709Smlf * There are 7 different cases when the comparison is made 19981709Smlf * between the bad sector cluster and the disk section. 19991709Smlf * 20001709Smlf * bad sector cluster gggggggggggbbbbbbbggggggggggg 20011709Smlf * case 1: ddddd 20021709Smlf * case 2: -d----- 20031709Smlf * case 3: ddddd 20041709Smlf * case 4: dddddddddddd 20051709Smlf * case 5: ddddddd----- 20061709Smlf * case 6: ---ddddddd 20071709Smlf * case 7: ddddddd 20081709Smlf * 20091709Smlf * where: g = good sector, b = bad sector 20101709Smlf * d = sector in disk section 20111709Smlf * - = disk section may be extended to cover those disk area 20121709Smlf */ 20131709Smlf 20141709Smlf static opaque_t 20151709Smlf cmdk_bbh_gethandle(opaque_t bbh_data, struct buf *bp) 20161709Smlf { 20171709Smlf struct cmdk *dkp = (struct cmdk *)bbh_data; 20181709Smlf struct bbh_handle *hp; 20191709Smlf struct bbh_cookie *ckp; 20201709Smlf struct alts_ent *altp; 20211709Smlf uint32_t alts_used; 20221709Smlf uint32_t part = CMDKPART(bp->b_edev); 20231709Smlf daddr32_t lastsec; 20241709Smlf long d_count; 20251709Smlf int i; 20261709Smlf int idx; 20271709Smlf int cnt; 20281709Smlf 20291709Smlf if (part >= V_NUMPAR) 20301709Smlf return (NULL); 20311709Smlf 20321709Smlf /* 20331709Smlf * This if statement is atomic and it will succeed 20341709Smlf * if there are no bad blocks (almost always) 20351709Smlf * 20361709Smlf * so this if is performed outside of the rw_enter for speed 20371709Smlf * and then repeated inside the rw_enter for safety 20381709Smlf */ 20391709Smlf if (!dkp->dk_alts_hdl) { 20401709Smlf return (NULL); 20411709Smlf } 20421709Smlf 20431709Smlf rw_enter(&dkp->dk_bbh_mutex, RW_READER); 20441709Smlf 20451709Smlf if (dkp->dk_alts_hdl == NULL) { 20461709Smlf rw_exit(&dkp->dk_bbh_mutex); 20471709Smlf return (NULL); 20481709Smlf } 20491709Smlf 20501709Smlf alts_used = dkp->dk_slc_cnt[part]; 20511709Smlf if (alts_used == 0) { 20521709Smlf rw_exit(&dkp->dk_bbh_mutex); 20531709Smlf return (NULL); 20541709Smlf } 20551709Smlf altp = dkp->dk_slc_ent[part]; 20561709Smlf 20571709Smlf /* 20581709Smlf * binary search for the largest bad sector index in the alternate 20591709Smlf * entry table which overlaps or larger than the starting d_sec 20601709Smlf */ 20611709Smlf i = cmdk_bbh_bsearch(altp, alts_used, GET_BP_SEC(bp)); 20621709Smlf /* if starting sector is > the largest bad sector, return */ 20631709Smlf if (i == -1) { 20641709Smlf rw_exit(&dkp->dk_bbh_mutex); 20651709Smlf return (NULL); 20661709Smlf } 20671709Smlf /* i is the starting index. Set altp to the starting entry addr */ 20681709Smlf altp += i; 20691709Smlf 20701709Smlf d_count = bp->b_bcount >> SCTRSHFT; 20711709Smlf lastsec = GET_BP_SEC(bp) + d_count - 1; 20721709Smlf 20731709Smlf /* calculate the number of bad sectors */ 20741709Smlf for (idx = i, cnt = 0; idx < alts_used; idx++, altp++, cnt++) { 20751709Smlf if (lastsec < altp->bad_start) 20761709Smlf break; 20771709Smlf } 20781709Smlf 20791709Smlf if (!cnt) { 20801709Smlf rw_exit(&dkp->dk_bbh_mutex); 20811709Smlf return (NULL); 20821709Smlf } 20831709Smlf 20841709Smlf /* calculate the maximum number of reserved cookies */ 20851709Smlf cnt <<= 1; 20861709Smlf cnt++; 20871709Smlf 20881709Smlf /* allocate the handle */ 20891709Smlf hp = (struct bbh_handle *)kmem_zalloc((sizeof (*hp) + 20901709Smlf (cnt * sizeof (*ckp))), KM_SLEEP); 20911709Smlf 20921709Smlf hp->h_idx = 0; 20931709Smlf hp->h_totck = cnt; 20941709Smlf ckp = hp->h_cktab = (struct bbh_cookie *)(hp + 1); 20951709Smlf ckp[0].ck_sector = GET_BP_SEC(bp); 20961709Smlf ckp[0].ck_seclen = d_count; 20971709Smlf 20981709Smlf altp = dkp->dk_slc_ent[part]; 20991709Smlf altp += i; 21001709Smlf for (idx = 0; i < alts_used; i++, altp++) { 21011709Smlf /* CASE 1: */ 21021709Smlf if (lastsec < altp->bad_start) 21031709Smlf break; 21041709Smlf 21051709Smlf /* CASE 3: */ 21061709Smlf if (ckp[idx].ck_sector > altp->bad_end) 21071709Smlf continue; 21081709Smlf 21091709Smlf /* CASE 2 and 7: */ 21101709Smlf if ((ckp[idx].ck_sector >= altp->bad_start) && 21111709Smlf (lastsec <= altp->bad_end)) { 21121709Smlf ckp[idx].ck_sector = altp->good_start + 21131709Smlf ckp[idx].ck_sector - altp->bad_start; 21141709Smlf break; 21151709Smlf } 21161709Smlf 21171709Smlf /* at least one bad sector in our section. break it. */ 21181709Smlf /* CASE 5: */ 21191709Smlf if ((lastsec >= altp->bad_start) && 21205295Srandyf (lastsec <= altp->bad_end)) { 21211709Smlf ckp[idx+1].ck_seclen = lastsec - altp->bad_start + 1; 21221709Smlf ckp[idx].ck_seclen -= ckp[idx+1].ck_seclen; 21231709Smlf ckp[idx+1].ck_sector = altp->good_start; 21241709Smlf break; 21251709Smlf } 21261709Smlf /* CASE 6: */ 21271709Smlf if ((ckp[idx].ck_sector <= altp->bad_end) && 21281709Smlf (ckp[idx].ck_sector >= altp->bad_start)) { 21291709Smlf ckp[idx+1].ck_seclen = ckp[idx].ck_seclen; 21301709Smlf ckp[idx].ck_seclen = altp->bad_end - 21311709Smlf ckp[idx].ck_sector + 1; 21321709Smlf ckp[idx+1].ck_seclen -= ckp[idx].ck_seclen; 21331709Smlf ckp[idx].ck_sector = altp->good_start + 21341709Smlf ckp[idx].ck_sector - altp->bad_start; 21351709Smlf idx++; 21361709Smlf ckp[idx].ck_sector = altp->bad_end + 1; 21371709Smlf continue; /* check rest of section */ 21381709Smlf } 21391709Smlf 21401709Smlf /* CASE 4: */ 21411709Smlf ckp[idx].ck_seclen = altp->bad_start - ckp[idx].ck_sector; 21421709Smlf ckp[idx+1].ck_sector = altp->good_start; 21431709Smlf ckp[idx+1].ck_seclen = altp->bad_end - altp->bad_start + 1; 21441709Smlf idx += 2; 21451709Smlf ckp[idx].ck_sector = altp->bad_end + 1; 21461709Smlf ckp[idx].ck_seclen = lastsec - altp->bad_end; 21471709Smlf } 21481709Smlf 21491709Smlf rw_exit(&dkp->dk_bbh_mutex); 21501709Smlf return ((opaque_t)hp); 21511709Smlf } 21521709Smlf 21531709Smlf static int 21541709Smlf cmdk_bbh_bsearch(struct alts_ent *buf, int cnt, daddr32_t key) 21551709Smlf { 21561709Smlf int i; 21571709Smlf int ind; 21581709Smlf int interval; 21591709Smlf int mystatus = -1; 21601709Smlf 21611709Smlf if (!cnt) 21621709Smlf return (mystatus); 21631709Smlf 21641709Smlf ind = 1; /* compiler complains about possible uninitialized var */ 21651709Smlf for (i = 1; i <= cnt; i <<= 1) 21661709Smlf ind = i; 21671709Smlf 21681709Smlf for (interval = ind; interval; ) { 21691709Smlf if ((key >= buf[ind-1].bad_start) && 21701709Smlf (key <= buf[ind-1].bad_end)) { 21711709Smlf return (ind-1); 21721709Smlf } else { 21731709Smlf interval >>= 1; 21741709Smlf if (key < buf[ind-1].bad_start) { 21751709Smlf /* record the largest bad sector index */ 21761709Smlf mystatus = ind-1; 21771709Smlf if (!interval) 21781709Smlf break; 21791709Smlf ind = ind - interval; 21801709Smlf } else { 21811709Smlf /* 21821709Smlf * if key is larger than the last element 21831709Smlf * then break 21841709Smlf */ 21851709Smlf if ((ind == cnt) || !interval) 21861709Smlf break; 21871709Smlf if ((ind+interval) <= cnt) 21881709Smlf ind += interval; 21891709Smlf } 21901709Smlf } 21911709Smlf } 21921709Smlf return (mystatus); 21931709Smlf } 2194